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