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