ath10k: migrate to mac80211 txq scheduling
[platform/kernel/linux-rpi.git] / drivers / net / wireless / ath / ath10k / core.h
1 /*
2  * Copyright (c) 2005-2011 Atheros Communications Inc.
3  * Copyright (c) 2011-2017 Qualcomm Atheros, Inc.
4  * Copyright (c) 2018, The Linux Foundation. All rights reserved.
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18
19 #ifndef _CORE_H_
20 #define _CORE_H_
21
22 #include <linux/completion.h>
23 #include <linux/if_ether.h>
24 #include <linux/types.h>
25 #include <linux/pci.h>
26 #include <linux/uuid.h>
27 #include <linux/time.h>
28
29 #include "htt.h"
30 #include "htc.h"
31 #include "hw.h"
32 #include "targaddrs.h"
33 #include "wmi.h"
34 #include "../ath.h"
35 #include "../regd.h"
36 #include "../dfs_pattern_detector.h"
37 #include "spectral.h"
38 #include "thermal.h"
39 #include "wow.h"
40 #include "swap.h"
41
42 #define MS(_v, _f) (((_v) & _f##_MASK) >> _f##_LSB)
43 #define SM(_v, _f) (((_v) << _f##_LSB) & _f##_MASK)
44 #define WO(_f)      ((_f##_OFFSET) >> 2)
45
46 #define ATH10K_SCAN_ID 0
47 #define ATH10K_SCAN_CHANNEL_SWITCH_WMI_EVT_OVERHEAD 10 /* msec */
48 #define WMI_READY_TIMEOUT (5 * HZ)
49 #define ATH10K_FLUSH_TIMEOUT_HZ (5 * HZ)
50 #define ATH10K_CONNECTION_LOSS_HZ (3 * HZ)
51 #define ATH10K_NUM_CHANS 41
52 #define ATH10K_MAX_5G_CHAN 173
53
54 /* Antenna noise floor */
55 #define ATH10K_DEFAULT_NOISE_FLOOR -95
56
57 #define ATH10K_INVALID_RSSI 128
58
59 #define ATH10K_MAX_NUM_MGMT_PENDING 128
60
61 /* number of failed packets (20 packets with 16 sw reties each) */
62 #define ATH10K_KICKOUT_THRESHOLD (20 * 16)
63
64 /*
65  * Use insanely high numbers to make sure that the firmware implementation
66  * won't start, we have the same functionality already in hostapd. Unit
67  * is seconds.
68  */
69 #define ATH10K_KEEPALIVE_MIN_IDLE 3747
70 #define ATH10K_KEEPALIVE_MAX_IDLE 3895
71 #define ATH10K_KEEPALIVE_MAX_UNRESPONSIVE 3900
72
73 /* NAPI poll budget */
74 #define ATH10K_NAPI_BUDGET      64
75
76 /* SMBIOS type containing Board Data File Name Extension */
77 #define ATH10K_SMBIOS_BDF_EXT_TYPE 0xF8
78
79 /* SMBIOS type structure length (excluding strings-set) */
80 #define ATH10K_SMBIOS_BDF_EXT_LENGTH 0x9
81
82 /* Offset pointing to Board Data File Name Extension */
83 #define ATH10K_SMBIOS_BDF_EXT_OFFSET 0x8
84
85 /* Board Data File Name Extension string length.
86  * String format: BDF_<Customer ID>_<Extension>\0
87  */
88 #define ATH10K_SMBIOS_BDF_EXT_STR_LENGTH 0x20
89
90 /* The magic used by QCA spec */
91 #define ATH10K_SMBIOS_BDF_EXT_MAGIC "BDF_"
92
93 /* Default Airtime weight multipler (Tuned for multiclient performance) */
94 #define ATH10K_AIRTIME_WEIGHT_MULTIPLIER  4
95
96 struct ath10k;
97
98 static inline const char *ath10k_bus_str(enum ath10k_bus bus)
99 {
100         switch (bus) {
101         case ATH10K_BUS_PCI:
102                 return "pci";
103         case ATH10K_BUS_AHB:
104                 return "ahb";
105         case ATH10K_BUS_SDIO:
106                 return "sdio";
107         case ATH10K_BUS_USB:
108                 return "usb";
109         case ATH10K_BUS_SNOC:
110                 return "snoc";
111         }
112
113         return "unknown";
114 }
115
116 enum ath10k_skb_flags {
117         ATH10K_SKB_F_NO_HWCRYPT = BIT(0),
118         ATH10K_SKB_F_DTIM_ZERO = BIT(1),
119         ATH10K_SKB_F_DELIVER_CAB = BIT(2),
120         ATH10K_SKB_F_MGMT = BIT(3),
121         ATH10K_SKB_F_QOS = BIT(4),
122 };
123
124 struct ath10k_skb_cb {
125         dma_addr_t paddr;
126         u8 flags;
127         u8 eid;
128         u16 msdu_id;
129         struct ieee80211_vif *vif;
130         struct ieee80211_txq *txq;
131 } __packed;
132
133 struct ath10k_skb_rxcb {
134         dma_addr_t paddr;
135         struct hlist_node hlist;
136 };
137
138 static inline struct ath10k_skb_cb *ATH10K_SKB_CB(struct sk_buff *skb)
139 {
140         BUILD_BUG_ON(sizeof(struct ath10k_skb_cb) >
141                      IEEE80211_TX_INFO_DRIVER_DATA_SIZE);
142         return (struct ath10k_skb_cb *)&IEEE80211_SKB_CB(skb)->driver_data;
143 }
144
145 static inline struct ath10k_skb_rxcb *ATH10K_SKB_RXCB(struct sk_buff *skb)
146 {
147         BUILD_BUG_ON(sizeof(struct ath10k_skb_rxcb) > sizeof(skb->cb));
148         return (struct ath10k_skb_rxcb *)skb->cb;
149 }
150
151 #define ATH10K_RXCB_SKB(rxcb) \
152                 container_of((void *)rxcb, struct sk_buff, cb)
153
154 static inline u32 host_interest_item_address(u32 item_offset)
155 {
156         return QCA988X_HOST_INTEREST_ADDRESS + item_offset;
157 }
158
159 struct ath10k_bmi {
160         bool done_sent;
161 };
162
163 struct ath10k_mem_chunk {
164         void *vaddr;
165         dma_addr_t paddr;
166         u32 len;
167         u32 req_id;
168 };
169
170 struct ath10k_wmi {
171         enum ath10k_htc_ep_id eid;
172         struct completion service_ready;
173         struct completion unified_ready;
174         struct completion barrier;
175         struct completion radar_confirm;
176         wait_queue_head_t tx_credits_wq;
177         DECLARE_BITMAP(svc_map, WMI_SERVICE_MAX);
178         struct wmi_cmd_map *cmd;
179         struct wmi_vdev_param_map *vdev_param;
180         struct wmi_pdev_param_map *pdev_param;
181         const struct wmi_ops *ops;
182         const struct wmi_peer_flags_map *peer_flags;
183
184         u32 mgmt_max_num_pending_tx;
185
186         /* Protected by data_lock */
187         struct idr mgmt_pending_tx;
188
189         u32 num_mem_chunks;
190         u32 rx_decap_mode;
191         struct ath10k_mem_chunk mem_chunks[WMI_MAX_MEM_REQS];
192 };
193
194 struct ath10k_fw_stats_peer {
195         struct list_head list;
196
197         u8 peer_macaddr[ETH_ALEN];
198         u32 peer_rssi;
199         u32 peer_tx_rate;
200         u32 peer_rx_rate; /* 10x only */
201         u32 rx_duration;
202 };
203
204 struct ath10k_fw_extd_stats_peer {
205         struct list_head list;
206
207         u8 peer_macaddr[ETH_ALEN];
208         u32 rx_duration;
209 };
210
211 struct ath10k_fw_stats_vdev {
212         struct list_head list;
213
214         u32 vdev_id;
215         u32 beacon_snr;
216         u32 data_snr;
217         u32 num_tx_frames[4];
218         u32 num_rx_frames;
219         u32 num_tx_frames_retries[4];
220         u32 num_tx_frames_failures[4];
221         u32 num_rts_fail;
222         u32 num_rts_success;
223         u32 num_rx_err;
224         u32 num_rx_discard;
225         u32 num_tx_not_acked;
226         u32 tx_rate_history[10];
227         u32 beacon_rssi_history[10];
228 };
229
230 struct ath10k_fw_stats_vdev_extd {
231         struct list_head list;
232
233         u32 vdev_id;
234         u32 ppdu_aggr_cnt;
235         u32 ppdu_noack;
236         u32 mpdu_queued;
237         u32 ppdu_nonaggr_cnt;
238         u32 mpdu_sw_requeued;
239         u32 mpdu_suc_retry;
240         u32 mpdu_suc_multitry;
241         u32 mpdu_fail_retry;
242         u32 tx_ftm_suc;
243         u32 tx_ftm_suc_retry;
244         u32 tx_ftm_fail;
245         u32 rx_ftmr_cnt;
246         u32 rx_ftmr_dup_cnt;
247         u32 rx_iftmr_cnt;
248         u32 rx_iftmr_dup_cnt;
249 };
250
251 struct ath10k_fw_stats_pdev {
252         struct list_head list;
253
254         /* PDEV stats */
255         s32 ch_noise_floor;
256         u32 tx_frame_count; /* Cycles spent transmitting frames */
257         u32 rx_frame_count; /* Cycles spent receiving frames */
258         u32 rx_clear_count; /* Total channel busy time, evidently */
259         u32 cycle_count; /* Total on-channel time */
260         u32 phy_err_count;
261         u32 chan_tx_power;
262         u32 ack_rx_bad;
263         u32 rts_bad;
264         u32 rts_good;
265         u32 fcs_bad;
266         u32 no_beacons;
267         u32 mib_int_count;
268
269         /* PDEV TX stats */
270         s32 comp_queued;
271         s32 comp_delivered;
272         s32 msdu_enqued;
273         s32 mpdu_enqued;
274         s32 wmm_drop;
275         s32 local_enqued;
276         s32 local_freed;
277         s32 hw_queued;
278         s32 hw_reaped;
279         s32 underrun;
280         u32 hw_paused;
281         s32 tx_abort;
282         s32 mpdus_requed;
283         u32 tx_ko;
284         u32 data_rc;
285         u32 self_triggers;
286         u32 sw_retry_failure;
287         u32 illgl_rate_phy_err;
288         u32 pdev_cont_xretry;
289         u32 pdev_tx_timeout;
290         u32 pdev_resets;
291         u32 phy_underrun;
292         u32 txop_ovf;
293         u32 seq_posted;
294         u32 seq_failed_queueing;
295         u32 seq_completed;
296         u32 seq_restarted;
297         u32 mu_seq_posted;
298         u32 mpdus_sw_flush;
299         u32 mpdus_hw_filter;
300         u32 mpdus_truncated;
301         u32 mpdus_ack_failed;
302         u32 mpdus_expired;
303
304         /* PDEV RX stats */
305         s32 mid_ppdu_route_change;
306         s32 status_rcvd;
307         s32 r0_frags;
308         s32 r1_frags;
309         s32 r2_frags;
310         s32 r3_frags;
311         s32 htt_msdus;
312         s32 htt_mpdus;
313         s32 loc_msdus;
314         s32 loc_mpdus;
315         s32 oversize_amsdu;
316         s32 phy_errs;
317         s32 phy_err_drop;
318         s32 mpdu_errs;
319         s32 rx_ovfl_errs;
320 };
321
322 struct ath10k_fw_stats {
323         bool extended;
324         struct list_head pdevs;
325         struct list_head vdevs;
326         struct list_head peers;
327         struct list_head peers_extd;
328 };
329
330 #define ATH10K_TPC_TABLE_TYPE_FLAG      1
331 #define ATH10K_TPC_PREAM_TABLE_END      0xFFFF
332
333 struct ath10k_tpc_table {
334         u32 pream_idx[WMI_TPC_RATE_MAX];
335         u8 rate_code[WMI_TPC_RATE_MAX];
336         char tpc_value[WMI_TPC_RATE_MAX][WMI_TPC_TX_N_CHAIN * WMI_TPC_BUF_SIZE];
337 };
338
339 struct ath10k_tpc_stats {
340         u32 reg_domain;
341         u32 chan_freq;
342         u32 phy_mode;
343         u32 twice_antenna_reduction;
344         u32 twice_max_rd_power;
345         s32 twice_antenna_gain;
346         u32 power_limit;
347         u32 num_tx_chain;
348         u32 ctl;
349         u32 rate_max;
350         u8 flag[WMI_TPC_FLAG];
351         struct ath10k_tpc_table tpc_table[WMI_TPC_FLAG];
352 };
353
354 struct ath10k_tpc_table_final {
355         u32 pream_idx[WMI_TPC_FINAL_RATE_MAX];
356         u8 rate_code[WMI_TPC_FINAL_RATE_MAX];
357         char tpc_value[WMI_TPC_FINAL_RATE_MAX][WMI_TPC_TX_N_CHAIN * WMI_TPC_BUF_SIZE];
358 };
359
360 struct ath10k_tpc_stats_final {
361         u32 reg_domain;
362         u32 chan_freq;
363         u32 phy_mode;
364         u32 twice_antenna_reduction;
365         u32 twice_max_rd_power;
366         s32 twice_antenna_gain;
367         u32 power_limit;
368         u32 num_tx_chain;
369         u32 ctl;
370         u32 rate_max;
371         u8 flag[WMI_TPC_FLAG];
372         struct ath10k_tpc_table_final tpc_table_final[WMI_TPC_FLAG];
373 };
374
375 struct ath10k_dfs_stats {
376         u32 phy_errors;
377         u32 pulses_total;
378         u32 pulses_detected;
379         u32 pulses_discarded;
380         u32 radar_detected;
381 };
382
383 enum ath10k_radar_confirmation_state {
384         ATH10K_RADAR_CONFIRMATION_IDLE = 0,
385         ATH10K_RADAR_CONFIRMATION_INPROGRESS,
386         ATH10K_RADAR_CONFIRMATION_STOPPED,
387 };
388
389 struct ath10k_radar_found_info {
390         u32 pri_min;
391         u32 pri_max;
392         u32 width_min;
393         u32 width_max;
394         u32 sidx_min;
395         u32 sidx_max;
396 };
397
398 #define ATH10K_MAX_NUM_PEER_IDS (1 << 11) /* htt rx_desc limit */
399
400 struct ath10k_peer {
401         struct list_head list;
402         struct ieee80211_vif *vif;
403         struct ieee80211_sta *sta;
404
405         bool removed;
406         int vdev_id;
407         u8 addr[ETH_ALEN];
408         DECLARE_BITMAP(peer_ids, ATH10K_MAX_NUM_PEER_IDS);
409
410         /* protected by ar->data_lock */
411         struct ieee80211_key_conf *keys[WMI_MAX_KEY_INDEX + 1];
412 };
413
414 struct ath10k_txq {
415         struct list_head list;
416         unsigned long num_fw_queued;
417         unsigned long num_push_allowed;
418 };
419
420 enum ath10k_pkt_rx_err {
421         ATH10K_PKT_RX_ERR_FCS,
422         ATH10K_PKT_RX_ERR_TKIP,
423         ATH10K_PKT_RX_ERR_CRYPT,
424         ATH10K_PKT_RX_ERR_PEER_IDX_INVAL,
425         ATH10K_PKT_RX_ERR_MAX,
426 };
427
428 enum ath10k_ampdu_subfrm_num {
429         ATH10K_AMPDU_SUBFRM_NUM_10,
430         ATH10K_AMPDU_SUBFRM_NUM_20,
431         ATH10K_AMPDU_SUBFRM_NUM_30,
432         ATH10K_AMPDU_SUBFRM_NUM_40,
433         ATH10K_AMPDU_SUBFRM_NUM_50,
434         ATH10K_AMPDU_SUBFRM_NUM_60,
435         ATH10K_AMPDU_SUBFRM_NUM_MORE,
436         ATH10K_AMPDU_SUBFRM_NUM_MAX,
437 };
438
439 enum ath10k_amsdu_subfrm_num {
440         ATH10K_AMSDU_SUBFRM_NUM_1,
441         ATH10K_AMSDU_SUBFRM_NUM_2,
442         ATH10K_AMSDU_SUBFRM_NUM_3,
443         ATH10K_AMSDU_SUBFRM_NUM_4,
444         ATH10K_AMSDU_SUBFRM_NUM_MORE,
445         ATH10K_AMSDU_SUBFRM_NUM_MAX,
446 };
447
448 struct ath10k_sta_tid_stats {
449         unsigned long int rx_pkt_from_fw;
450         unsigned long int rx_pkt_unchained;
451         unsigned long int rx_pkt_drop_chained;
452         unsigned long int rx_pkt_drop_filter;
453         unsigned long int rx_pkt_err[ATH10K_PKT_RX_ERR_MAX];
454         unsigned long int rx_pkt_queued_for_mac;
455         unsigned long int rx_pkt_ampdu[ATH10K_AMPDU_SUBFRM_NUM_MAX];
456         unsigned long int rx_pkt_amsdu[ATH10K_AMSDU_SUBFRM_NUM_MAX];
457 };
458
459 enum ath10k_counter_type {
460         ATH10K_COUNTER_TYPE_BYTES,
461         ATH10K_COUNTER_TYPE_PKTS,
462         ATH10K_COUNTER_TYPE_MAX,
463 };
464
465 enum ath10k_stats_type {
466         ATH10K_STATS_TYPE_SUCC,
467         ATH10K_STATS_TYPE_FAIL,
468         ATH10K_STATS_TYPE_RETRY,
469         ATH10K_STATS_TYPE_AMPDU,
470         ATH10K_STATS_TYPE_MAX,
471 };
472
473 struct ath10k_htt_data_stats {
474         u64 legacy[ATH10K_COUNTER_TYPE_MAX][ATH10K_LEGACY_NUM];
475         u64 ht[ATH10K_COUNTER_TYPE_MAX][ATH10K_HT_MCS_NUM];
476         u64 vht[ATH10K_COUNTER_TYPE_MAX][ATH10K_VHT_MCS_NUM];
477         u64 bw[ATH10K_COUNTER_TYPE_MAX][ATH10K_BW_NUM];
478         u64 nss[ATH10K_COUNTER_TYPE_MAX][ATH10K_NSS_NUM];
479         u64 gi[ATH10K_COUNTER_TYPE_MAX][ATH10K_GI_NUM];
480         u64 rate_table[ATH10K_COUNTER_TYPE_MAX][ATH10K_RATE_TABLE_NUM];
481 };
482
483 struct ath10k_htt_tx_stats {
484         struct ath10k_htt_data_stats stats[ATH10K_STATS_TYPE_MAX];
485         u64 tx_duration;
486         u64 ba_fails;
487         u64 ack_fails;
488 };
489
490 struct ath10k_sta {
491         struct ath10k_vif *arvif;
492
493         /* the following are protected by ar->data_lock */
494         u32 changed; /* IEEE80211_RC_* */
495         u32 bw;
496         u32 nss;
497         u32 smps;
498         u16 peer_id;
499         struct rate_info txrate;
500         struct ieee80211_tx_info tx_info;
501
502         struct work_struct update_wk;
503         u64 rx_duration;
504         struct ath10k_htt_tx_stats *tx_stats;
505
506 #ifdef CONFIG_MAC80211_DEBUGFS
507         /* protected by conf_mutex */
508         bool aggr_mode;
509
510         /* Protected with ar->data_lock */
511         struct ath10k_sta_tid_stats tid_stats[IEEE80211_NUM_TIDS + 1];
512 #endif
513         /* Protected with ar->data_lock */
514         u32 peer_ps_state;
515 };
516
517 #define ATH10K_VDEV_SETUP_TIMEOUT_HZ (5 * HZ)
518
519 enum ath10k_beacon_state {
520         ATH10K_BEACON_SCHEDULED = 0,
521         ATH10K_BEACON_SENDING,
522         ATH10K_BEACON_SENT,
523 };
524
525 struct ath10k_vif {
526         struct list_head list;
527
528         u32 vdev_id;
529         u16 peer_id;
530         enum wmi_vdev_type vdev_type;
531         enum wmi_vdev_subtype vdev_subtype;
532         u32 beacon_interval;
533         u32 dtim_period;
534         struct sk_buff *beacon;
535         /* protected by data_lock */
536         enum ath10k_beacon_state beacon_state;
537         void *beacon_buf;
538         dma_addr_t beacon_paddr;
539         unsigned long tx_paused; /* arbitrary values defined by target */
540
541         struct ath10k *ar;
542         struct ieee80211_vif *vif;
543
544         bool is_started;
545         bool is_up;
546         bool spectral_enabled;
547         bool ps;
548         u32 aid;
549         u8 bssid[ETH_ALEN];
550
551         struct ieee80211_key_conf *wep_keys[WMI_MAX_KEY_INDEX + 1];
552         s8 def_wep_key_idx;
553
554         u16 tx_seq_no;
555
556         union {
557                 struct {
558                         u32 uapsd;
559                 } sta;
560                 struct {
561                         /* 512 stations */
562                         u8 tim_bitmap[64];
563                         u8 tim_len;
564                         u32 ssid_len;
565                         u8 ssid[IEEE80211_MAX_SSID_LEN];
566                         bool hidden_ssid;
567                         /* P2P_IE with NoA attribute for P2P_GO case */
568                         u32 noa_len;
569                         u8 *noa_data;
570                 } ap;
571         } u;
572
573         bool use_cts_prot;
574         bool nohwcrypt;
575         int num_legacy_stations;
576         int txpower;
577         struct wmi_wmm_params_all_arg wmm_params;
578         struct work_struct ap_csa_work;
579         struct delayed_work connection_loss_work;
580         struct cfg80211_bitrate_mask bitrate_mask;
581 };
582
583 struct ath10k_vif_iter {
584         u32 vdev_id;
585         struct ath10k_vif *arvif;
586 };
587
588 /* Copy Engine register dump, protected by ce-lock */
589 struct ath10k_ce_crash_data {
590         __le32 base_addr;
591         __le32 src_wr_idx;
592         __le32 src_r_idx;
593         __le32 dst_wr_idx;
594         __le32 dst_r_idx;
595 };
596
597 struct ath10k_ce_crash_hdr {
598         __le32 ce_count;
599         __le32 reserved[3]; /* for future use */
600         struct ath10k_ce_crash_data entries[];
601 };
602
603 #define MAX_MEM_DUMP_TYPE       5
604
605 /* used for crash-dump storage, protected by data-lock */
606 struct ath10k_fw_crash_data {
607         guid_t guid;
608         struct timespec64 timestamp;
609         __le32 registers[REG_DUMP_COUNT_QCA988X];
610         struct ath10k_ce_crash_data ce_crash_data[CE_COUNT_MAX];
611
612         u8 *ramdump_buf;
613         size_t ramdump_buf_len;
614 };
615
616 struct ath10k_debug {
617         struct dentry *debugfs_phy;
618
619         struct ath10k_fw_stats fw_stats;
620         struct completion fw_stats_complete;
621         bool fw_stats_done;
622
623         unsigned long htt_stats_mask;
624         struct delayed_work htt_stats_dwork;
625         struct ath10k_dfs_stats dfs_stats;
626         struct ath_dfs_pool_stats dfs_pool_stats;
627
628         /* used for tpc-dump storage, protected by data-lock */
629         struct ath10k_tpc_stats *tpc_stats;
630         struct ath10k_tpc_stats_final *tpc_stats_final;
631
632         struct completion tpc_complete;
633
634         /* protected by conf_mutex */
635         u64 fw_dbglog_mask;
636         u32 fw_dbglog_level;
637         u32 reg_addr;
638         u32 nf_cal_period;
639         void *cal_data;
640         u32 enable_extd_tx_stats;
641 };
642
643 enum ath10k_state {
644         ATH10K_STATE_OFF = 0,
645         ATH10K_STATE_ON,
646
647         /* When doing firmware recovery the device is first powered down.
648          * mac80211 is supposed to call in to start() hook later on. It is
649          * however possible that driver unloading and firmware crash overlap.
650          * mac80211 can wait on conf_mutex in stop() while the device is
651          * stopped in ath10k_core_restart() work holding conf_mutex. The state
652          * RESTARTED means that the device is up and mac80211 has started hw
653          * reconfiguration. Once mac80211 is done with the reconfiguration we
654          * set the state to STATE_ON in reconfig_complete().
655          */
656         ATH10K_STATE_RESTARTING,
657         ATH10K_STATE_RESTARTED,
658
659         /* The device has crashed while restarting hw. This state is like ON
660          * but commands are blocked in HTC and -ECOMM response is given. This
661          * prevents completion timeouts and makes the driver more responsive to
662          * userspace commands. This is also prevents recursive recovery.
663          */
664         ATH10K_STATE_WEDGED,
665
666         /* factory tests */
667         ATH10K_STATE_UTF,
668 };
669
670 enum ath10k_firmware_mode {
671         /* the default mode, standard 802.11 functionality */
672         ATH10K_FIRMWARE_MODE_NORMAL,
673
674         /* factory tests etc */
675         ATH10K_FIRMWARE_MODE_UTF,
676 };
677
678 enum ath10k_fw_features {
679         /* wmi_mgmt_rx_hdr contains extra RSSI information */
680         ATH10K_FW_FEATURE_EXT_WMI_MGMT_RX = 0,
681
682         /* Firmware from 10X branch. Deprecated, don't use in new code. */
683         ATH10K_FW_FEATURE_WMI_10X = 1,
684
685         /* firmware support tx frame management over WMI, otherwise it's HTT */
686         ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX = 2,
687
688         /* Firmware does not support P2P */
689         ATH10K_FW_FEATURE_NO_P2P = 3,
690
691         /* Firmware 10.2 feature bit. The ATH10K_FW_FEATURE_WMI_10X feature
692          * bit is required to be set as well. Deprecated, don't use in new
693          * code.
694          */
695         ATH10K_FW_FEATURE_WMI_10_2 = 4,
696
697         /* Some firmware revisions lack proper multi-interface client powersave
698          * implementation. Enabling PS could result in connection drops,
699          * traffic stalls, etc.
700          */
701         ATH10K_FW_FEATURE_MULTI_VIF_PS_SUPPORT = 5,
702
703         /* Some firmware revisions have an incomplete WoWLAN implementation
704          * despite WMI service bit being advertised. This feature flag is used
705          * to distinguish whether WoWLAN is really supported or not.
706          */
707         ATH10K_FW_FEATURE_WOWLAN_SUPPORT = 6,
708
709         /* Don't trust error code from otp.bin */
710         ATH10K_FW_FEATURE_IGNORE_OTP_RESULT = 7,
711
712         /* Some firmware revisions pad 4th hw address to 4 byte boundary making
713          * it 8 bytes long in Native Wifi Rx decap.
714          */
715         ATH10K_FW_FEATURE_NO_NWIFI_DECAP_4ADDR_PADDING = 8,
716
717         /* Firmware supports bypassing PLL setting on init. */
718         ATH10K_FW_FEATURE_SUPPORTS_SKIP_CLOCK_INIT = 9,
719
720         /* Raw mode support. If supported, FW supports receiving and trasmitting
721          * frames in raw mode.
722          */
723         ATH10K_FW_FEATURE_RAW_MODE_SUPPORT = 10,
724
725         /* Firmware Supports Adaptive CCA*/
726         ATH10K_FW_FEATURE_SUPPORTS_ADAPTIVE_CCA = 11,
727
728         /* Firmware supports management frame protection */
729         ATH10K_FW_FEATURE_MFP_SUPPORT = 12,
730
731         /* Firmware supports pull-push model where host shares it's software
732          * queue state with firmware and firmware generates fetch requests
733          * telling host which queues to dequeue tx from.
734          *
735          * Primary function of this is improved MU-MIMO performance with
736          * multiple clients.
737          */
738         ATH10K_FW_FEATURE_PEER_FLOW_CONTROL = 13,
739
740         /* Firmware supports BT-Coex without reloading firmware via pdev param.
741          * To support Bluetooth coexistence pdev param, WMI_COEX_GPIO_SUPPORT of
742          * extended resource config should be enabled always. This firmware IE
743          * is used to configure WMI_COEX_GPIO_SUPPORT.
744          */
745         ATH10K_FW_FEATURE_BTCOEX_PARAM = 14,
746
747         /* Unused flag and proven to be not working, enable this if you want
748          * to experiment sending NULL func data frames in HTT TX
749          */
750         ATH10K_FW_FEATURE_SKIP_NULL_FUNC_WAR = 15,
751
752         /* Firmware allow other BSS mesh broadcast/multicast frames without
753          * creating monitor interface. Appropriate rxfilters are programmed for
754          * mesh vdev by firmware itself. This feature flags will be used for
755          * not creating monitor vdev while configuring mesh node.
756          */
757         ATH10K_FW_FEATURE_ALLOWS_MESH_BCAST = 16,
758
759         /* Firmware does not support power save in station mode. */
760         ATH10K_FW_FEATURE_NO_PS = 17,
761
762         /* Firmware allows management tx by reference instead of by value. */
763         ATH10K_FW_FEATURE_MGMT_TX_BY_REF = 18,
764
765         /* Firmware load is done externally, not by bmi */
766         ATH10K_FW_FEATURE_NON_BMI = 19,
767
768         /* Firmware sends only one chan_info event per channel */
769         ATH10K_FW_FEATURE_SINGLE_CHAN_INFO_PER_CHANNEL = 20,
770
771         /* keep last */
772         ATH10K_FW_FEATURE_COUNT,
773 };
774
775 enum ath10k_dev_flags {
776         /* Indicates that ath10k device is during CAC phase of DFS */
777         ATH10K_CAC_RUNNING,
778         ATH10K_FLAG_CORE_REGISTERED,
779
780         /* Device has crashed and needs to restart. This indicates any pending
781          * waiters should immediately cancel instead of waiting for a time out.
782          */
783         ATH10K_FLAG_CRASH_FLUSH,
784
785         /* Use Raw mode instead of native WiFi Tx/Rx encap mode.
786          * Raw mode supports both hardware and software crypto. Native WiFi only
787          * supports hardware crypto.
788          */
789         ATH10K_FLAG_RAW_MODE,
790
791         /* Disable HW crypto engine */
792         ATH10K_FLAG_HW_CRYPTO_DISABLED,
793
794         /* Bluetooth coexistance enabled */
795         ATH10K_FLAG_BTCOEX,
796
797         /* Per Station statistics service */
798         ATH10K_FLAG_PEER_STATS,
799 };
800
801 enum ath10k_cal_mode {
802         ATH10K_CAL_MODE_FILE,
803         ATH10K_CAL_MODE_OTP,
804         ATH10K_CAL_MODE_DT,
805         ATH10K_PRE_CAL_MODE_FILE,
806         ATH10K_PRE_CAL_MODE_DT,
807         ATH10K_CAL_MODE_EEPROM,
808 };
809
810 enum ath10k_crypt_mode {
811         /* Only use hardware crypto engine */
812         ATH10K_CRYPT_MODE_HW,
813         /* Only use software crypto engine */
814         ATH10K_CRYPT_MODE_SW,
815 };
816
817 static inline const char *ath10k_cal_mode_str(enum ath10k_cal_mode mode)
818 {
819         switch (mode) {
820         case ATH10K_CAL_MODE_FILE:
821                 return "file";
822         case ATH10K_CAL_MODE_OTP:
823                 return "otp";
824         case ATH10K_CAL_MODE_DT:
825                 return "dt";
826         case ATH10K_PRE_CAL_MODE_FILE:
827                 return "pre-cal-file";
828         case ATH10K_PRE_CAL_MODE_DT:
829                 return "pre-cal-dt";
830         case ATH10K_CAL_MODE_EEPROM:
831                 return "eeprom";
832         }
833
834         return "unknown";
835 }
836
837 enum ath10k_scan_state {
838         ATH10K_SCAN_IDLE,
839         ATH10K_SCAN_STARTING,
840         ATH10K_SCAN_RUNNING,
841         ATH10K_SCAN_ABORTING,
842 };
843
844 static inline const char *ath10k_scan_state_str(enum ath10k_scan_state state)
845 {
846         switch (state) {
847         case ATH10K_SCAN_IDLE:
848                 return "idle";
849         case ATH10K_SCAN_STARTING:
850                 return "starting";
851         case ATH10K_SCAN_RUNNING:
852                 return "running";
853         case ATH10K_SCAN_ABORTING:
854                 return "aborting";
855         }
856
857         return "unknown";
858 }
859
860 enum ath10k_tx_pause_reason {
861         ATH10K_TX_PAUSE_Q_FULL,
862         ATH10K_TX_PAUSE_MAX,
863 };
864
865 struct ath10k_fw_file {
866         const struct firmware *firmware;
867
868         char fw_version[ETHTOOL_FWVERS_LEN];
869
870         DECLARE_BITMAP(fw_features, ATH10K_FW_FEATURE_COUNT);
871
872         enum ath10k_fw_wmi_op_version wmi_op_version;
873         enum ath10k_fw_htt_op_version htt_op_version;
874
875         const void *firmware_data;
876         size_t firmware_len;
877
878         const void *otp_data;
879         size_t otp_len;
880
881         const void *codeswap_data;
882         size_t codeswap_len;
883
884         /* The original idea of struct ath10k_fw_file was that it only
885          * contains struct firmware and pointers to various parts (actual
886          * firmware binary, otp, metadata etc) of the file. This seg_info
887          * is actually created separate but as this is used similarly as
888          * the other firmware components it's more convenient to have it
889          * here.
890          */
891         struct ath10k_swap_code_seg_info *firmware_swap_code_seg_info;
892 };
893
894 struct ath10k_fw_components {
895         const struct firmware *board;
896         const void *board_data;
897         size_t board_len;
898         const struct firmware *ext_board;
899         const void *ext_board_data;
900         size_t ext_board_len;
901
902         struct ath10k_fw_file fw_file;
903 };
904
905 struct ath10k_per_peer_tx_stats {
906         u32     succ_bytes;
907         u32     retry_bytes;
908         u32     failed_bytes;
909         u8      ratecode;
910         u8      flags;
911         u16     peer_id;
912         u16     succ_pkts;
913         u16     retry_pkts;
914         u16     failed_pkts;
915         u16     duration;
916         u32     reserved1;
917         u32     reserved2;
918 };
919
920 enum ath10k_dev_type {
921         ATH10K_DEV_TYPE_LL,
922         ATH10K_DEV_TYPE_HL,
923 };
924
925 struct ath10k_bus_params {
926         u32 chip_id;
927         enum ath10k_dev_type dev_type;
928         bool link_can_suspend;
929 };
930
931 struct ath10k {
932         struct ath_common ath_common;
933         struct ieee80211_hw *hw;
934         struct ieee80211_ops *ops;
935         struct device *dev;
936         u8 mac_addr[ETH_ALEN];
937
938         enum ath10k_hw_rev hw_rev;
939         u16 dev_id;
940         u32 chip_id;
941         enum ath10k_dev_type dev_type;
942         u32 target_version;
943         u8 fw_version_major;
944         u32 fw_version_minor;
945         u16 fw_version_release;
946         u16 fw_version_build;
947         u32 fw_stats_req_mask;
948         u32 phy_capability;
949         u32 hw_min_tx_power;
950         u32 hw_max_tx_power;
951         u32 hw_eeprom_rd;
952         u32 ht_cap_info;
953         u32 vht_cap_info;
954         u32 num_rf_chains;
955         u32 max_spatial_stream;
956         /* protected by conf_mutex */
957         u32 low_5ghz_chan;
958         u32 high_5ghz_chan;
959         bool ani_enabled;
960         /* protected by conf_mutex */
961         u8 ps_state_enable;
962
963         bool nlo_enabled;
964         bool p2p;
965
966         struct {
967                 enum ath10k_bus bus;
968                 const struct ath10k_hif_ops *ops;
969         } hif;
970
971         struct completion target_suspend;
972         struct completion driver_recovery;
973
974         const struct ath10k_hw_regs *regs;
975         const struct ath10k_hw_ce_regs *hw_ce_regs;
976         const struct ath10k_hw_values *hw_values;
977         struct ath10k_bmi bmi;
978         struct ath10k_wmi wmi;
979         struct ath10k_htc htc;
980         struct ath10k_htt htt;
981
982         struct ath10k_hw_params hw_params;
983
984         /* contains the firmware images used with ATH10K_FIRMWARE_MODE_NORMAL */
985         struct ath10k_fw_components normal_mode_fw;
986
987         /* READ-ONLY images of the running firmware, which can be either
988          * normal or UTF. Do not modify, release etc!
989          */
990         const struct ath10k_fw_components *running_fw;
991
992         const struct firmware *pre_cal_file;
993         const struct firmware *cal_file;
994
995         struct {
996                 u32 vendor;
997                 u32 device;
998                 u32 subsystem_vendor;
999                 u32 subsystem_device;
1000
1001                 bool bmi_ids_valid;
1002                 bool qmi_ids_valid;
1003                 u32 qmi_board_id;
1004                 u8 bmi_board_id;
1005                 u8 bmi_eboard_id;
1006                 u8 bmi_chip_id;
1007                 bool ext_bid_supported;
1008
1009                 char bdf_ext[ATH10K_SMBIOS_BDF_EXT_STR_LENGTH];
1010         } id;
1011
1012         int fw_api;
1013         int bd_api;
1014         enum ath10k_cal_mode cal_mode;
1015
1016         struct {
1017                 struct completion started;
1018                 struct completion completed;
1019                 struct completion on_channel;
1020                 struct delayed_work timeout;
1021                 enum ath10k_scan_state state;
1022                 bool is_roc;
1023                 int vdev_id;
1024                 int roc_freq;
1025                 bool roc_notify;
1026         } scan;
1027
1028         struct {
1029                 struct ieee80211_supported_band sbands[NUM_NL80211_BANDS];
1030         } mac;
1031
1032         /* should never be NULL; needed for regular htt rx */
1033         struct ieee80211_channel *rx_channel;
1034
1035         /* valid during scan; needed for mgmt rx during scan */
1036         struct ieee80211_channel *scan_channel;
1037
1038         /* current operating channel definition */
1039         struct cfg80211_chan_def chandef;
1040
1041         /* currently configured operating channel in firmware */
1042         struct ieee80211_channel *tgt_oper_chan;
1043
1044         unsigned long long free_vdev_map;
1045         struct ath10k_vif *monitor_arvif;
1046         bool monitor;
1047         int monitor_vdev_id;
1048         bool monitor_started;
1049         unsigned int filter_flags;
1050         unsigned long dev_flags;
1051         bool dfs_block_radar_events;
1052
1053         /* protected by conf_mutex */
1054         bool radar_enabled;
1055         int num_started_vdevs;
1056
1057         /* Protected by conf-mutex */
1058         u8 cfg_tx_chainmask;
1059         u8 cfg_rx_chainmask;
1060
1061         struct completion install_key_done;
1062
1063         int last_wmi_vdev_start_status;
1064         struct completion vdev_setup_done;
1065
1066         struct workqueue_struct *workqueue;
1067         /* Auxiliary workqueue */
1068         struct workqueue_struct *workqueue_aux;
1069
1070         /* prevents concurrent FW reconfiguration */
1071         struct mutex conf_mutex;
1072
1073         /* protects shared structure data */
1074         spinlock_t data_lock;
1075
1076         struct list_head arvifs;
1077         struct list_head peers;
1078         struct ath10k_peer *peer_map[ATH10K_MAX_NUM_PEER_IDS];
1079         wait_queue_head_t peer_mapping_wq;
1080
1081         /* protected by conf_mutex */
1082         int num_peers;
1083         int num_stations;
1084
1085         int max_num_peers;
1086         int max_num_stations;
1087         int max_num_vdevs;
1088         int max_num_tdls_vdevs;
1089         int num_active_peers;
1090         int num_tids;
1091
1092         struct work_struct svc_rdy_work;
1093         struct sk_buff *svc_rdy_skb;
1094
1095         struct work_struct offchan_tx_work;
1096         struct sk_buff_head offchan_tx_queue;
1097         struct completion offchan_tx_completed;
1098         struct sk_buff *offchan_tx_skb;
1099
1100         struct work_struct wmi_mgmt_tx_work;
1101         struct sk_buff_head wmi_mgmt_tx_queue;
1102
1103         enum ath10k_state state;
1104
1105         struct work_struct register_work;
1106         struct work_struct restart_work;
1107
1108         /* cycle count is reported twice for each visited channel during scan.
1109          * access protected by data_lock
1110          */
1111         u32 survey_last_rx_clear_count;
1112         u32 survey_last_cycle_count;
1113         struct survey_info survey[ATH10K_NUM_CHANS];
1114
1115         /* Channel info events are expected to come in pairs without and with
1116          * COMPLETE flag set respectively for each channel visit during scan.
1117          *
1118          * However there are deviations from this rule. This flag is used to
1119          * avoid reporting garbage data.
1120          */
1121         bool ch_info_can_report_survey;
1122         struct completion bss_survey_done;
1123
1124         struct dfs_pattern_detector *dfs_detector;
1125
1126         unsigned long tx_paused; /* see ATH10K_TX_PAUSE_ */
1127
1128 #ifdef CONFIG_ATH10K_DEBUGFS
1129         struct ath10k_debug debug;
1130         struct {
1131                 /* relay(fs) channel for spectral scan */
1132                 struct rchan *rfs_chan_spec_scan;
1133
1134                 /* spectral_mode and spec_config are protected by conf_mutex */
1135                 enum ath10k_spectral_mode mode;
1136                 struct ath10k_spec_scan config;
1137         } spectral;
1138 #endif
1139
1140         u32 pktlog_filter;
1141
1142 #ifdef CONFIG_DEV_COREDUMP
1143         struct {
1144                 struct ath10k_fw_crash_data *fw_crash_data;
1145         } coredump;
1146 #endif
1147
1148         struct {
1149                 /* protected by conf_mutex */
1150                 struct ath10k_fw_components utf_mode_fw;
1151
1152                 /* protected by data_lock */
1153                 bool utf_monitor;
1154         } testmode;
1155
1156         struct {
1157                 /* protected by data_lock */
1158                 u32 fw_crash_counter;
1159                 u32 fw_warm_reset_counter;
1160                 u32 fw_cold_reset_counter;
1161         } stats;
1162
1163         struct ath10k_thermal thermal;
1164         struct ath10k_wow wow;
1165         struct ath10k_per_peer_tx_stats peer_tx_stats;
1166
1167         /* NAPI */
1168         struct net_device napi_dev;
1169         struct napi_struct napi;
1170
1171         struct work_struct set_coverage_class_work;
1172         /* protected by conf_mutex */
1173         struct {
1174                 /* writing also protected by data_lock */
1175                 s16 coverage_class;
1176
1177                 u32 reg_phyclk;
1178                 u32 reg_slottime_conf;
1179                 u32 reg_slottime_orig;
1180                 u32 reg_ack_cts_timeout_conf;
1181                 u32 reg_ack_cts_timeout_orig;
1182         } fw_coverage;
1183
1184         u32 ampdu_reference;
1185
1186         const u8 *wmi_key_cipher;
1187         void *ce_priv;
1188
1189         u32 sta_tid_stats_mask;
1190
1191         /* protected by data_lock */
1192         enum ath10k_radar_confirmation_state radar_conf_state;
1193         struct ath10k_radar_found_info last_radar_info;
1194         struct work_struct radar_confirmation_work;
1195         struct ath10k_bus_params bus_param;
1196
1197         /* must be last */
1198         u8 drv_priv[0] __aligned(sizeof(void *));
1199 };
1200
1201 static inline bool ath10k_peer_stats_enabled(struct ath10k *ar)
1202 {
1203         if (test_bit(ATH10K_FLAG_PEER_STATS, &ar->dev_flags) &&
1204             test_bit(WMI_SERVICE_PEER_STATS, ar->wmi.svc_map))
1205                 return true;
1206
1207         return false;
1208 }
1209
1210 extern unsigned long ath10k_coredump_mask;
1211
1212 struct ath10k *ath10k_core_create(size_t priv_size, struct device *dev,
1213                                   enum ath10k_bus bus,
1214                                   enum ath10k_hw_rev hw_rev,
1215                                   const struct ath10k_hif_ops *hif_ops);
1216 void ath10k_core_destroy(struct ath10k *ar);
1217 void ath10k_core_get_fw_features_str(struct ath10k *ar,
1218                                      char *buf,
1219                                      size_t max_len);
1220 int ath10k_core_fetch_firmware_api_n(struct ath10k *ar, const char *name,
1221                                      struct ath10k_fw_file *fw_file);
1222
1223 int ath10k_core_start(struct ath10k *ar, enum ath10k_firmware_mode mode,
1224                       const struct ath10k_fw_components *fw_components);
1225 int ath10k_wait_for_suspend(struct ath10k *ar, u32 suspend_opt);
1226 void ath10k_core_stop(struct ath10k *ar);
1227 int ath10k_core_register(struct ath10k *ar,
1228                          const struct ath10k_bus_params *bus_params);
1229 void ath10k_core_unregister(struct ath10k *ar);
1230 int ath10k_core_fetch_board_file(struct ath10k *ar, int bd_ie_type);
1231 void ath10k_core_free_board_files(struct ath10k *ar);
1232
1233 #endif /* _CORE_H_ */