xref: /linux/drivers/net/wireless/ath/ath11k/mac.c (revision 7bb377107c72a40ab7505341f8626c8eb79a0cb7)
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3  * Copyright (c) 2018-2019 The Linux Foundation. All rights reserved.
4  */
5 
6 #include <net/mac80211.h>
7 #include <linux/etherdevice.h>
8 #include "mac.h"
9 #include "core.h"
10 #include "debug.h"
11 #include "wmi.h"
12 #include "hw.h"
13 #include "dp_tx.h"
14 #include "dp_rx.h"
15 #include "testmode.h"
16 #include "peer.h"
17 
18 #define CHAN2G(_channel, _freq, _flags) { \
19 	.band                   = NL80211_BAND_2GHZ, \
20 	.hw_value               = (_channel), \
21 	.center_freq            = (_freq), \
22 	.flags                  = (_flags), \
23 	.max_antenna_gain       = 0, \
24 	.max_power              = 30, \
25 }
26 
27 #define CHAN5G(_channel, _freq, _flags) { \
28 	.band                   = NL80211_BAND_5GHZ, \
29 	.hw_value               = (_channel), \
30 	.center_freq            = (_freq), \
31 	.flags                  = (_flags), \
32 	.max_antenna_gain       = 0, \
33 	.max_power              = 30, \
34 }
35 
36 /* frame mode values are mapped as per enum ath11k_hw_txrx_mode */
37 static unsigned int ath11k_frame_mode = ATH11K_HW_TXRX_NATIVE_WIFI;
38 module_param_named(frame_mode, ath11k_frame_mode, uint, 0644);
39 MODULE_PARM_DESC(frame_mode,
40 		 "Datapath frame mode (0: raw, 1: native wifi (default), 2: ethernet)");
41 
42 static const struct ieee80211_channel ath11k_2ghz_channels[] = {
43 	CHAN2G(1, 2412, 0),
44 	CHAN2G(2, 2417, 0),
45 	CHAN2G(3, 2422, 0),
46 	CHAN2G(4, 2427, 0),
47 	CHAN2G(5, 2432, 0),
48 	CHAN2G(6, 2437, 0),
49 	CHAN2G(7, 2442, 0),
50 	CHAN2G(8, 2447, 0),
51 	CHAN2G(9, 2452, 0),
52 	CHAN2G(10, 2457, 0),
53 	CHAN2G(11, 2462, 0),
54 	CHAN2G(12, 2467, 0),
55 	CHAN2G(13, 2472, 0),
56 	CHAN2G(14, 2484, 0),
57 };
58 
59 static const struct ieee80211_channel ath11k_5ghz_channels[] = {
60 	CHAN5G(36, 5180, 0),
61 	CHAN5G(40, 5200, 0),
62 	CHAN5G(44, 5220, 0),
63 	CHAN5G(48, 5240, 0),
64 	CHAN5G(52, 5260, 0),
65 	CHAN5G(56, 5280, 0),
66 	CHAN5G(60, 5300, 0),
67 	CHAN5G(64, 5320, 0),
68 	CHAN5G(100, 5500, 0),
69 	CHAN5G(104, 5520, 0),
70 	CHAN5G(108, 5540, 0),
71 	CHAN5G(112, 5560, 0),
72 	CHAN5G(116, 5580, 0),
73 	CHAN5G(120, 5600, 0),
74 	CHAN5G(124, 5620, 0),
75 	CHAN5G(128, 5640, 0),
76 	CHAN5G(132, 5660, 0),
77 	CHAN5G(136, 5680, 0),
78 	CHAN5G(140, 5700, 0),
79 	CHAN5G(144, 5720, 0),
80 	CHAN5G(149, 5745, 0),
81 	CHAN5G(153, 5765, 0),
82 	CHAN5G(157, 5785, 0),
83 	CHAN5G(161, 5805, 0),
84 	CHAN5G(165, 5825, 0),
85 	CHAN5G(169, 5845, 0),
86 	CHAN5G(173, 5865, 0),
87 };
88 
89 static struct ieee80211_rate ath11k_legacy_rates[] = {
90 	{ .bitrate = 10,
91 	  .hw_value = ATH11K_HW_RATE_CCK_LP_1M },
92 	{ .bitrate = 20,
93 	  .hw_value = ATH11K_HW_RATE_CCK_LP_2M,
94 	  .hw_value_short = ATH11K_HW_RATE_CCK_SP_2M,
95 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
96 	{ .bitrate = 55,
97 	  .hw_value = ATH11K_HW_RATE_CCK_LP_5_5M,
98 	  .hw_value_short = ATH11K_HW_RATE_CCK_SP_5_5M,
99 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
100 	{ .bitrate = 110,
101 	  .hw_value = ATH11K_HW_RATE_CCK_LP_11M,
102 	  .hw_value_short = ATH11K_HW_RATE_CCK_SP_11M,
103 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
104 
105 	{ .bitrate = 60, .hw_value = ATH11K_HW_RATE_OFDM_6M },
106 	{ .bitrate = 90, .hw_value = ATH11K_HW_RATE_OFDM_9M },
107 	{ .bitrate = 120, .hw_value = ATH11K_HW_RATE_OFDM_12M },
108 	{ .bitrate = 180, .hw_value = ATH11K_HW_RATE_OFDM_18M },
109 	{ .bitrate = 240, .hw_value = ATH11K_HW_RATE_OFDM_24M },
110 	{ .bitrate = 360, .hw_value = ATH11K_HW_RATE_OFDM_36M },
111 	{ .bitrate = 480, .hw_value = ATH11K_HW_RATE_OFDM_48M },
112 	{ .bitrate = 540, .hw_value = ATH11K_HW_RATE_OFDM_54M },
113 };
114 
115 static const int
116 ath11k_phymodes[NUM_NL80211_BANDS][ATH11K_CHAN_WIDTH_NUM] = {
117 	[NL80211_BAND_2GHZ] = {
118 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
119 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
120 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11AX_HE20_2G,
121 			[NL80211_CHAN_WIDTH_20] = MODE_11AX_HE20_2G,
122 			[NL80211_CHAN_WIDTH_40] = MODE_11AX_HE40_2G,
123 			[NL80211_CHAN_WIDTH_80] = MODE_11AX_HE80_2G,
124 			[NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
125 			[NL80211_CHAN_WIDTH_160] = MODE_UNKNOWN,
126 	},
127 	[NL80211_BAND_5GHZ] = {
128 			[NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
129 			[NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
130 			[NL80211_CHAN_WIDTH_20_NOHT] = MODE_11AX_HE20,
131 			[NL80211_CHAN_WIDTH_20] = MODE_11AX_HE20,
132 			[NL80211_CHAN_WIDTH_40] = MODE_11AX_HE40,
133 			[NL80211_CHAN_WIDTH_80] = MODE_11AX_HE80,
134 			[NL80211_CHAN_WIDTH_160] = MODE_11AX_HE160,
135 			[NL80211_CHAN_WIDTH_80P80] = MODE_11AX_HE80_80,
136 	},
137 };
138 
139 const struct htt_rx_ring_tlv_filter ath11k_mac_mon_status_filter_default = {
140 	.rx_filter = HTT_RX_FILTER_TLV_FLAGS_MPDU_START |
141 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_END |
142 		     HTT_RX_FILTER_TLV_FLAGS_PPDU_END_STATUS_DONE,
143 	.pkt_filter_flags0 = HTT_RX_FP_MGMT_FILTER_FLAGS0,
144 	.pkt_filter_flags1 = HTT_RX_FP_MGMT_FILTER_FLAGS1,
145 	.pkt_filter_flags2 = HTT_RX_FP_CTRL_FILTER_FLASG2,
146 	.pkt_filter_flags3 = HTT_RX_FP_DATA_FILTER_FLASG3 |
147 			     HTT_RX_FP_CTRL_FILTER_FLASG3
148 };
149 
150 #define ATH11K_MAC_FIRST_OFDM_RATE_IDX 4
151 #define ath11k_g_rates ath11k_legacy_rates
152 #define ath11k_g_rates_size (ARRAY_SIZE(ath11k_legacy_rates))
153 #define ath11k_a_rates (ath11k_legacy_rates + 4)
154 #define ath11k_a_rates_size (ARRAY_SIZE(ath11k_legacy_rates) - 4)
155 
156 #define ATH11K_MAC_SCAN_TIMEOUT_MSECS 200 /* in msecs */
157 
158 static const u32 ath11k_smps_map[] = {
159 	[WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
160 	[WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
161 	[WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
162 	[WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
163 };
164 
165 u8 ath11k_mac_bw_to_mac80211_bw(u8 bw)
166 {
167 	u8 ret = 0;
168 
169 	switch (bw) {
170 	case ATH11K_BW_20:
171 		ret = RATE_INFO_BW_20;
172 		break;
173 	case ATH11K_BW_40:
174 		ret = RATE_INFO_BW_40;
175 		break;
176 	case ATH11K_BW_80:
177 		ret = RATE_INFO_BW_80;
178 		break;
179 	case ATH11K_BW_160:
180 		ret = RATE_INFO_BW_160;
181 		break;
182 	}
183 
184 	return ret;
185 }
186 
187 enum ath11k_supported_bw ath11k_mac_mac80211_bw_to_ath11k_bw(enum rate_info_bw bw)
188 {
189 	switch (bw) {
190 	case RATE_INFO_BW_20:
191 		return ATH11K_BW_20;
192 	case RATE_INFO_BW_40:
193 		return ATH11K_BW_40;
194 	case RATE_INFO_BW_80:
195 		return ATH11K_BW_80;
196 	case RATE_INFO_BW_160:
197 		return ATH11K_BW_160;
198 	default:
199 		return ATH11K_BW_20;
200 	}
201 }
202 
203 int ath11k_mac_hw_ratecode_to_legacy_rate(u8 hw_rc, u8 preamble, u8 *rateidx,
204 					  u16 *rate)
205 {
206 	/* As default, it is OFDM rates */
207 	int i = ATH11K_MAC_FIRST_OFDM_RATE_IDX;
208 	int max_rates_idx = ath11k_g_rates_size;
209 
210 	if (preamble == WMI_RATE_PREAMBLE_CCK) {
211 		hw_rc &= ~ATH11k_HW_RATECODE_CCK_SHORT_PREAM_MASK;
212 		i = 0;
213 		max_rates_idx = ATH11K_MAC_FIRST_OFDM_RATE_IDX;
214 	}
215 
216 	while (i < max_rates_idx) {
217 		if (hw_rc == ath11k_legacy_rates[i].hw_value) {
218 			*rateidx = i;
219 			*rate = ath11k_legacy_rates[i].bitrate;
220 			return 0;
221 		}
222 		i++;
223 	}
224 
225 	return -EINVAL;
226 }
227 
228 static int get_num_chains(u32 mask)
229 {
230 	int num_chains = 0;
231 
232 	while (mask) {
233 		if (mask & BIT(0))
234 			num_chains++;
235 		mask >>= 1;
236 	}
237 
238 	return num_chains;
239 }
240 
241 u8 ath11k_mac_bitrate_to_idx(const struct ieee80211_supported_band *sband,
242 			     u32 bitrate)
243 {
244 	int i;
245 
246 	for (i = 0; i < sband->n_bitrates; i++)
247 		if (sband->bitrates[i].bitrate == bitrate)
248 			return i;
249 
250 	return 0;
251 }
252 
253 static u32
254 ath11k_mac_max_ht_nss(const u8 ht_mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
255 {
256 	int nss;
257 
258 	for (nss = IEEE80211_HT_MCS_MASK_LEN - 1; nss >= 0; nss--)
259 		if (ht_mcs_mask[nss])
260 			return nss + 1;
261 
262 	return 1;
263 }
264 
265 static u32
266 ath11k_mac_max_vht_nss(const u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
267 {
268 	int nss;
269 
270 	for (nss = NL80211_VHT_NSS_MAX - 1; nss >= 0; nss--)
271 		if (vht_mcs_mask[nss])
272 			return nss + 1;
273 
274 	return 1;
275 }
276 
277 static u8 ath11k_parse_mpdudensity(u8 mpdudensity)
278 {
279 /* 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
280  *   0 for no restriction
281  *   1 for 1/4 us
282  *   2 for 1/2 us
283  *   3 for 1 us
284  *   4 for 2 us
285  *   5 for 4 us
286  *   6 for 8 us
287  *   7 for 16 us
288  */
289 	switch (mpdudensity) {
290 	case 0:
291 		return 0;
292 	case 1:
293 	case 2:
294 	case 3:
295 	/* Our lower layer calculations limit our precision to
296 	 * 1 microsecond
297 	 */
298 		return 1;
299 	case 4:
300 		return 2;
301 	case 5:
302 		return 4;
303 	case 6:
304 		return 8;
305 	case 7:
306 		return 16;
307 	default:
308 		return 0;
309 	}
310 }
311 
312 static int ath11k_mac_vif_chan(struct ieee80211_vif *vif,
313 			       struct cfg80211_chan_def *def)
314 {
315 	struct ieee80211_chanctx_conf *conf;
316 
317 	rcu_read_lock();
318 	conf = rcu_dereference(vif->chanctx_conf);
319 	if (!conf) {
320 		rcu_read_unlock();
321 		return -ENOENT;
322 	}
323 
324 	*def = conf->def;
325 	rcu_read_unlock();
326 
327 	return 0;
328 }
329 
330 static bool ath11k_mac_bitrate_is_cck(int bitrate)
331 {
332 	switch (bitrate) {
333 	case 10:
334 	case 20:
335 	case 55:
336 	case 110:
337 		return true;
338 	}
339 
340 	return false;
341 }
342 
343 u8 ath11k_mac_hw_rate_to_idx(const struct ieee80211_supported_band *sband,
344 			     u8 hw_rate, bool cck)
345 {
346 	const struct ieee80211_rate *rate;
347 	int i;
348 
349 	for (i = 0; i < sband->n_bitrates; i++) {
350 		rate = &sband->bitrates[i];
351 
352 		if (ath11k_mac_bitrate_is_cck(rate->bitrate) != cck)
353 			continue;
354 
355 		if (rate->hw_value == hw_rate)
356 			return i;
357 		else if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE &&
358 			 rate->hw_value_short == hw_rate)
359 			return i;
360 	}
361 
362 	return 0;
363 }
364 
365 static u8 ath11k_mac_bitrate_to_rate(int bitrate)
366 {
367 	return DIV_ROUND_UP(bitrate, 5) |
368 	       (ath11k_mac_bitrate_is_cck(bitrate) ? BIT(7) : 0);
369 }
370 
371 static void ath11k_get_arvif_iter(void *data, u8 *mac,
372 				  struct ieee80211_vif *vif)
373 {
374 	struct ath11k_vif_iter *arvif_iter = data;
375 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
376 
377 	if (arvif->vdev_id == arvif_iter->vdev_id)
378 		arvif_iter->arvif = arvif;
379 }
380 
381 struct ath11k_vif *ath11k_mac_get_arvif(struct ath11k *ar, u32 vdev_id)
382 {
383 	struct ath11k_vif_iter arvif_iter;
384 	u32 flags;
385 
386 	memset(&arvif_iter, 0, sizeof(struct ath11k_vif_iter));
387 	arvif_iter.vdev_id = vdev_id;
388 
389 	flags = IEEE80211_IFACE_ITER_RESUME_ALL;
390 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
391 						   flags,
392 						   ath11k_get_arvif_iter,
393 						   &arvif_iter);
394 	if (!arvif_iter.arvif) {
395 		ath11k_warn(ar->ab, "No VIF found for vdev %d\n", vdev_id);
396 		return NULL;
397 	}
398 
399 	return arvif_iter.arvif;
400 }
401 
402 struct ath11k_vif *ath11k_mac_get_arvif_by_vdev_id(struct ath11k_base *ab,
403 						   u32 vdev_id)
404 {
405 	int i;
406 	struct ath11k_pdev *pdev;
407 	struct ath11k_vif *arvif;
408 
409 	for (i = 0; i < ab->num_radios; i++) {
410 		pdev = rcu_dereference(ab->pdevs_active[i]);
411 		if (pdev && pdev->ar) {
412 			arvif = ath11k_mac_get_arvif(pdev->ar, vdev_id);
413 			if (arvif)
414 				return arvif;
415 		}
416 	}
417 
418 	return NULL;
419 }
420 
421 struct ath11k *ath11k_mac_get_ar_by_vdev_id(struct ath11k_base *ab, u32 vdev_id)
422 {
423 	int i;
424 	struct ath11k_pdev *pdev;
425 
426 	for (i = 0; i < ab->num_radios; i++) {
427 		pdev = rcu_dereference(ab->pdevs_active[i]);
428 		if (pdev && pdev->ar) {
429 			if (pdev->ar->allocated_vdev_map & (1LL << vdev_id))
430 				return pdev->ar;
431 		}
432 	}
433 
434 	return NULL;
435 }
436 
437 struct ath11k *ath11k_mac_get_ar_by_pdev_id(struct ath11k_base *ab, u32 pdev_id)
438 {
439 	int i;
440 	struct ath11k_pdev *pdev;
441 
442 	if (WARN_ON(pdev_id > ab->num_radios))
443 		return NULL;
444 
445 	for (i = 0; i < ab->num_radios; i++) {
446 		pdev = rcu_dereference(ab->pdevs_active[i]);
447 
448 		if (pdev && pdev->pdev_id == pdev_id)
449 			return (pdev->ar ? pdev->ar : NULL);
450 	}
451 
452 	return NULL;
453 }
454 
455 struct ath11k *ath11k_mac_get_ar_vdev_stop_status(struct ath11k_base *ab,
456 						  u32 vdev_id)
457 {
458 	int i;
459 	struct ath11k_pdev *pdev;
460 	struct ath11k *ar;
461 
462 	for (i = 0; i < ab->num_radios; i++) {
463 		pdev = rcu_dereference(ab->pdevs_active[i]);
464 		if (pdev && pdev->ar) {
465 			ar = pdev->ar;
466 
467 			spin_lock_bh(&ar->data_lock);
468 			if (ar->vdev_stop_status.stop_in_progress &&
469 			    ar->vdev_stop_status.vdev_id == vdev_id) {
470 				ar->vdev_stop_status.stop_in_progress = false;
471 				spin_unlock_bh(&ar->data_lock);
472 				return ar;
473 			}
474 			spin_unlock_bh(&ar->data_lock);
475 		}
476 	}
477 	return NULL;
478 }
479 
480 static void ath11k_pdev_caps_update(struct ath11k *ar)
481 {
482 	struct ath11k_base *ab = ar->ab;
483 
484 	ar->max_tx_power = ab->target_caps.hw_max_tx_power;
485 
486 	/* FIXME Set min_tx_power to ab->target_caps.hw_min_tx_power.
487 	 * But since the received value in svcrdy is same as hw_max_tx_power,
488 	 * we can set ar->min_tx_power to 0 currently until
489 	 * this is fixed in firmware
490 	 */
491 	ar->min_tx_power = 0;
492 
493 	ar->txpower_limit_2g = ar->max_tx_power;
494 	ar->txpower_limit_5g = ar->max_tx_power;
495 	ar->txpower_scale = WMI_HOST_TP_SCALE_MAX;
496 }
497 
498 static int ath11k_mac_txpower_recalc(struct ath11k *ar)
499 {
500 	struct ath11k_pdev *pdev = ar->pdev;
501 	struct ath11k_vif *arvif;
502 	int ret, txpower = -1;
503 	u32 param;
504 
505 	lockdep_assert_held(&ar->conf_mutex);
506 
507 	list_for_each_entry(arvif, &ar->arvifs, list) {
508 		if (arvif->txpower <= 0)
509 			continue;
510 
511 		if (txpower == -1)
512 			txpower = arvif->txpower;
513 		else
514 			txpower = min(txpower, arvif->txpower);
515 	}
516 
517 	if (txpower == -1)
518 		return 0;
519 
520 	/* txpwr is set as 2 units per dBm in FW*/
521 	txpower = min_t(u32, max_t(u32, ar->min_tx_power, txpower),
522 			ar->max_tx_power) * 2;
523 
524 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "txpower to set in hw %d\n",
525 		   txpower / 2);
526 
527 	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP) &&
528 	    ar->txpower_limit_2g != txpower) {
529 		param = WMI_PDEV_PARAM_TXPOWER_LIMIT2G;
530 		ret = ath11k_wmi_pdev_set_param(ar, param,
531 						txpower, ar->pdev->pdev_id);
532 		if (ret)
533 			goto fail;
534 		ar->txpower_limit_2g = txpower;
535 	}
536 
537 	if ((pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP) &&
538 	    ar->txpower_limit_5g != txpower) {
539 		param = WMI_PDEV_PARAM_TXPOWER_LIMIT5G;
540 		ret = ath11k_wmi_pdev_set_param(ar, param,
541 						txpower, ar->pdev->pdev_id);
542 		if (ret)
543 			goto fail;
544 		ar->txpower_limit_5g = txpower;
545 	}
546 
547 	return 0;
548 
549 fail:
550 	ath11k_warn(ar->ab, "failed to recalc txpower limit %d using pdev param %d: %d\n",
551 		    txpower / 2, param, ret);
552 	return ret;
553 }
554 
555 static int ath11k_recalc_rtscts_prot(struct ath11k_vif *arvif)
556 {
557 	struct ath11k *ar = arvif->ar;
558 	u32 vdev_param, rts_cts = 0;
559 	int ret;
560 
561 	lockdep_assert_held(&ar->conf_mutex);
562 
563 	vdev_param = WMI_VDEV_PARAM_ENABLE_RTSCTS;
564 
565 	/* Enable RTS/CTS protection for sw retries (when legacy stations
566 	 * are in BSS) or by default only for second rate series.
567 	 * TODO: Check if we need to enable CTS 2 Self in any case
568 	 */
569 	rts_cts = WMI_USE_RTS_CTS;
570 
571 	if (arvif->num_legacy_stations > 0)
572 		rts_cts |= WMI_RTSCTS_ACROSS_SW_RETRIES << 4;
573 	else
574 		rts_cts |= WMI_RTSCTS_FOR_SECOND_RATESERIES << 4;
575 
576 	/* Need not send duplicate param value to firmware */
577 	if (arvif->rtscts_prot_mode == rts_cts)
578 		return 0;
579 
580 	arvif->rtscts_prot_mode = rts_cts;
581 
582 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev %d recalc rts/cts prot %d\n",
583 		   arvif->vdev_id, rts_cts);
584 
585 	ret =  ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
586 					     vdev_param, rts_cts);
587 	if (ret)
588 		ath11k_warn(ar->ab, "failed to recalculate rts/cts prot for vdev %d: %d\n",
589 			    arvif->vdev_id, ret);
590 
591 	return ret;
592 }
593 
594 static int ath11k_mac_set_kickout(struct ath11k_vif *arvif)
595 {
596 	struct ath11k *ar = arvif->ar;
597 	u32 param;
598 	int ret;
599 
600 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_STA_KICKOUT_TH,
601 					ATH11K_KICKOUT_THRESHOLD,
602 					ar->pdev->pdev_id);
603 	if (ret) {
604 		ath11k_warn(ar->ab, "failed to set kickout threshold on vdev %i: %d\n",
605 			    arvif->vdev_id, ret);
606 		return ret;
607 	}
608 
609 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MIN_IDLE_INACTIVE_TIME_SECS;
610 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
611 					    ATH11K_KEEPALIVE_MIN_IDLE);
612 	if (ret) {
613 		ath11k_warn(ar->ab, "failed to set keepalive minimum idle time on vdev %i: %d\n",
614 			    arvif->vdev_id, ret);
615 		return ret;
616 	}
617 
618 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_IDLE_INACTIVE_TIME_SECS;
619 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
620 					    ATH11K_KEEPALIVE_MAX_IDLE);
621 	if (ret) {
622 		ath11k_warn(ar->ab, "failed to set keepalive maximum idle time on vdev %i: %d\n",
623 			    arvif->vdev_id, ret);
624 		return ret;
625 	}
626 
627 	param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_UNRESPONSIVE_TIME_SECS;
628 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
629 					    ATH11K_KEEPALIVE_MAX_UNRESPONSIVE);
630 	if (ret) {
631 		ath11k_warn(ar->ab, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
632 			    arvif->vdev_id, ret);
633 		return ret;
634 	}
635 
636 	return 0;
637 }
638 
639 void ath11k_mac_peer_cleanup_all(struct ath11k *ar)
640 {
641 	struct ath11k_peer *peer, *tmp;
642 	struct ath11k_base *ab = ar->ab;
643 
644 	lockdep_assert_held(&ar->conf_mutex);
645 
646 	spin_lock_bh(&ab->base_lock);
647 	list_for_each_entry_safe(peer, tmp, &ab->peers, list) {
648 		ath11k_peer_rx_tid_cleanup(ar, peer);
649 		list_del(&peer->list);
650 		kfree(peer);
651 	}
652 	spin_unlock_bh(&ab->base_lock);
653 
654 	ar->num_peers = 0;
655 	ar->num_stations = 0;
656 }
657 
658 static int ath11k_monitor_vdev_up(struct ath11k *ar, int vdev_id)
659 {
660 	int ret = 0;
661 
662 	ret = ath11k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
663 	if (ret) {
664 		ath11k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
665 			    vdev_id, ret);
666 		return ret;
667 	}
668 
669 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac monitor vdev %i started\n",
670 		   vdev_id);
671 	return 0;
672 }
673 
674 static int ath11k_mac_op_config(struct ieee80211_hw *hw, u32 changed)
675 {
676 	struct ath11k *ar = hw->priv;
677 	int ret = 0;
678 
679 	/* mac80211 requires this op to be present and that's why
680 	 * there's an empty function, this can be extended when
681 	 * required.
682 	 */
683 
684 	mutex_lock(&ar->conf_mutex);
685 
686 	/* TODO: Handle configuration changes as appropriate */
687 
688 	mutex_unlock(&ar->conf_mutex);
689 
690 	return ret;
691 }
692 
693 static int ath11k_mac_setup_bcn_tmpl(struct ath11k_vif *arvif)
694 {
695 	struct ath11k *ar = arvif->ar;
696 	struct ath11k_base *ab = ar->ab;
697 	struct ieee80211_hw *hw = ar->hw;
698 	struct ieee80211_vif *vif = arvif->vif;
699 	struct ieee80211_mutable_offsets offs = {};
700 	struct sk_buff *bcn;
701 	int ret;
702 
703 	if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
704 		return 0;
705 
706 	bcn = ieee80211_beacon_get_template(hw, vif, &offs);
707 	if (!bcn) {
708 		ath11k_warn(ab, "failed to get beacon template from mac80211\n");
709 		return -EPERM;
710 	}
711 
712 	ret = ath11k_wmi_bcn_tmpl(ar, arvif->vdev_id, &offs, bcn);
713 
714 	kfree_skb(bcn);
715 
716 	if (ret)
717 		ath11k_warn(ab, "failed to submit beacon template command: %d\n",
718 			    ret);
719 
720 	return ret;
721 }
722 
723 static void ath11k_control_beaconing(struct ath11k_vif *arvif,
724 				     struct ieee80211_bss_conf *info)
725 {
726 	struct ath11k *ar = arvif->ar;
727 	int ret = 0;
728 
729 	lockdep_assert_held(&arvif->ar->conf_mutex);
730 
731 	if (!info->enable_beacon) {
732 		ret = ath11k_wmi_vdev_down(ar, arvif->vdev_id);
733 		if (ret)
734 			ath11k_warn(ar->ab, "failed to down vdev_id %i: %d\n",
735 				    arvif->vdev_id, ret);
736 
737 		arvif->is_up = false;
738 		return;
739 	}
740 
741 	/* Install the beacon template to the FW */
742 	ret = ath11k_mac_setup_bcn_tmpl(arvif);
743 	if (ret) {
744 		ath11k_warn(ar->ab, "failed to update bcn tmpl during vdev up: %d\n",
745 			    ret);
746 		return;
747 	}
748 
749 	arvif->tx_seq_no = 0x1000;
750 
751 	arvif->aid = 0;
752 
753 	ether_addr_copy(arvif->bssid, info->bssid);
754 
755 	ret = ath11k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
756 				 arvif->bssid);
757 	if (ret) {
758 		ath11k_warn(ar->ab, "failed to bring up vdev %d: %i\n",
759 			    arvif->vdev_id, ret);
760 		return;
761 	}
762 
763 	arvif->is_up = true;
764 
765 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
766 }
767 
768 static void ath11k_peer_assoc_h_basic(struct ath11k *ar,
769 				      struct ieee80211_vif *vif,
770 				      struct ieee80211_sta *sta,
771 				      struct peer_assoc_params *arg)
772 {
773 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
774 	u32 aid;
775 
776 	lockdep_assert_held(&ar->conf_mutex);
777 
778 	if (vif->type == NL80211_IFTYPE_STATION)
779 		aid = vif->bss_conf.aid;
780 	else
781 		aid = sta->aid;
782 
783 	ether_addr_copy(arg->peer_mac, sta->addr);
784 	arg->vdev_id = arvif->vdev_id;
785 	arg->peer_associd = aid;
786 	arg->auth_flag = true;
787 	/* TODO: STA WAR in ath10k for listen interval required? */
788 	arg->peer_listen_intval = ar->hw->conf.listen_interval;
789 	arg->peer_nss = 1;
790 	arg->peer_caps = vif->bss_conf.assoc_capability;
791 }
792 
793 static void ath11k_peer_assoc_h_crypto(struct ath11k *ar,
794 				       struct ieee80211_vif *vif,
795 				       struct ieee80211_sta *sta,
796 				       struct peer_assoc_params *arg)
797 {
798 	struct ieee80211_bss_conf *info = &vif->bss_conf;
799 	struct cfg80211_chan_def def;
800 	struct cfg80211_bss *bss;
801 	const u8 *rsnie = NULL;
802 	const u8 *wpaie = NULL;
803 
804 	lockdep_assert_held(&ar->conf_mutex);
805 
806 	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
807 		return;
808 
809 	bss = cfg80211_get_bss(ar->hw->wiphy, def.chan, info->bssid, NULL, 0,
810 			       IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
811 	if (bss) {
812 		const struct cfg80211_bss_ies *ies;
813 
814 		rcu_read_lock();
815 		rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
816 
817 		ies = rcu_dereference(bss->ies);
818 
819 		wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
820 						WLAN_OUI_TYPE_MICROSOFT_WPA,
821 						ies->data,
822 						ies->len);
823 		rcu_read_unlock();
824 		cfg80211_put_bss(ar->hw->wiphy, bss);
825 	}
826 
827 	/* FIXME: base on RSN IE/WPA IE is a correct idea? */
828 	if (rsnie || wpaie) {
829 		ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
830 			   "%s: rsn ie found\n", __func__);
831 		arg->need_ptk_4_way = true;
832 	}
833 
834 	if (wpaie) {
835 		ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
836 			   "%s: wpa ie found\n", __func__);
837 		arg->need_gtk_2_way = true;
838 	}
839 
840 	if (sta->mfp) {
841 		/* TODO: Need to check if FW supports PMF? */
842 		arg->is_pmf_enabled = true;
843 	}
844 
845 	/* TODO: safe_mode_enabled (bypass 4-way handshake) flag req? */
846 }
847 
848 static void ath11k_peer_assoc_h_rates(struct ath11k *ar,
849 				      struct ieee80211_vif *vif,
850 				      struct ieee80211_sta *sta,
851 				      struct peer_assoc_params *arg)
852 {
853 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
854 	struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
855 	struct cfg80211_chan_def def;
856 	const struct ieee80211_supported_band *sband;
857 	const struct ieee80211_rate *rates;
858 	enum nl80211_band band;
859 	u32 ratemask;
860 	u8 rate;
861 	int i;
862 
863 	lockdep_assert_held(&ar->conf_mutex);
864 
865 	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
866 		return;
867 
868 	band = def.chan->band;
869 	sband = ar->hw->wiphy->bands[band];
870 	ratemask = sta->supp_rates[band];
871 	ratemask &= arvif->bitrate_mask.control[band].legacy;
872 	rates = sband->bitrates;
873 
874 	rateset->num_rates = 0;
875 
876 	for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
877 		if (!(ratemask & 1))
878 			continue;
879 
880 		rate = ath11k_mac_bitrate_to_rate(rates->bitrate);
881 		rateset->rates[rateset->num_rates] = rate;
882 		rateset->num_rates++;
883 	}
884 }
885 
886 static bool
887 ath11k_peer_assoc_h_ht_masked(const u8 ht_mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
888 {
889 	int nss;
890 
891 	for (nss = 0; nss < IEEE80211_HT_MCS_MASK_LEN; nss++)
892 		if (ht_mcs_mask[nss])
893 			return false;
894 
895 	return true;
896 }
897 
898 static bool
899 ath11k_peer_assoc_h_vht_masked(const u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
900 {
901 	int nss;
902 
903 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++)
904 		if (vht_mcs_mask[nss])
905 			return false;
906 
907 	return true;
908 }
909 
910 static void ath11k_peer_assoc_h_ht(struct ath11k *ar,
911 				   struct ieee80211_vif *vif,
912 				   struct ieee80211_sta *sta,
913 				   struct peer_assoc_params *arg)
914 {
915 	const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
916 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
917 	struct cfg80211_chan_def def;
918 	enum nl80211_band band;
919 	const u8 *ht_mcs_mask;
920 	int i, n;
921 	u8 max_nss;
922 	u32 stbc;
923 
924 	lockdep_assert_held(&ar->conf_mutex);
925 
926 	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
927 		return;
928 
929 	if (!ht_cap->ht_supported)
930 		return;
931 
932 	band = def.chan->band;
933 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
934 
935 	if (ath11k_peer_assoc_h_ht_masked(ht_mcs_mask))
936 		return;
937 
938 	arg->ht_flag = true;
939 
940 	arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
941 				    ht_cap->ampdu_factor)) - 1;
942 
943 	arg->peer_mpdu_density =
944 		ath11k_parse_mpdudensity(ht_cap->ampdu_density);
945 
946 	arg->peer_ht_caps = ht_cap->cap;
947 	arg->peer_rate_caps |= WMI_HOST_RC_HT_FLAG;
948 
949 	if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
950 		arg->ldpc_flag = true;
951 
952 	if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
953 		arg->bw_40 = true;
954 		arg->peer_rate_caps |= WMI_HOST_RC_CW40_FLAG;
955 	}
956 
957 	if (arvif->bitrate_mask.control[band].gi != NL80211_TXRATE_FORCE_LGI) {
958 		if (ht_cap->cap & (IEEE80211_HT_CAP_SGI_20 |
959 		    IEEE80211_HT_CAP_SGI_40))
960 			arg->peer_rate_caps |= WMI_HOST_RC_SGI_FLAG;
961 	}
962 
963 	if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
964 		arg->peer_rate_caps |= WMI_HOST_RC_TX_STBC_FLAG;
965 		arg->stbc_flag = true;
966 	}
967 
968 	if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
969 		stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
970 		stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
971 		stbc = stbc << WMI_HOST_RC_RX_STBC_FLAG_S;
972 		arg->peer_rate_caps |= stbc;
973 		arg->stbc_flag = true;
974 	}
975 
976 	if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
977 		arg->peer_rate_caps |= WMI_HOST_RC_TS_FLAG;
978 	else if (ht_cap->mcs.rx_mask[1])
979 		arg->peer_rate_caps |= WMI_HOST_RC_DS_FLAG;
980 
981 	for (i = 0, n = 0, max_nss = 0; i < IEEE80211_HT_MCS_MASK_LEN * 8; i++)
982 		if ((ht_cap->mcs.rx_mask[i / 8] & BIT(i % 8)) &&
983 		    (ht_mcs_mask[i / 8] & BIT(i % 8))) {
984 			max_nss = (i / 8) + 1;
985 			arg->peer_ht_rates.rates[n++] = i;
986 		}
987 
988 	/* This is a workaround for HT-enabled STAs which break the spec
989 	 * and have no HT capabilities RX mask (no HT RX MCS map).
990 	 *
991 	 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
992 	 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
993 	 *
994 	 * Firmware asserts if such situation occurs.
995 	 */
996 	if (n == 0) {
997 		arg->peer_ht_rates.num_rates = 8;
998 		for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
999 			arg->peer_ht_rates.rates[i] = i;
1000 	} else {
1001 		arg->peer_ht_rates.num_rates = n;
1002 		arg->peer_nss = min(sta->rx_nss, max_nss);
1003 	}
1004 
1005 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
1006 		   arg->peer_mac,
1007 		   arg->peer_ht_rates.num_rates,
1008 		   arg->peer_nss);
1009 }
1010 
1011 static int ath11k_mac_get_max_vht_mcs_map(u16 mcs_map, int nss)
1012 {
1013 	switch ((mcs_map >> (2 * nss)) & 0x3) {
1014 	case IEEE80211_VHT_MCS_SUPPORT_0_7: return BIT(8) - 1;
1015 	case IEEE80211_VHT_MCS_SUPPORT_0_8: return BIT(9) - 1;
1016 	case IEEE80211_VHT_MCS_SUPPORT_0_9: return BIT(10) - 1;
1017 	}
1018 	return 0;
1019 }
1020 
1021 static u16
1022 ath11k_peer_assoc_h_vht_limit(u16 tx_mcs_set,
1023 			      const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])
1024 {
1025 	int idx_limit;
1026 	int nss;
1027 	u16 mcs_map;
1028 	u16 mcs;
1029 
1030 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
1031 		mcs_map = ath11k_mac_get_max_vht_mcs_map(tx_mcs_set, nss) &
1032 			  vht_mcs_limit[nss];
1033 
1034 		if (mcs_map)
1035 			idx_limit = fls(mcs_map) - 1;
1036 		else
1037 			idx_limit = -1;
1038 
1039 		switch (idx_limit) {
1040 		case 0: /* fall through */
1041 		case 1: /* fall through */
1042 		case 2: /* fall through */
1043 		case 3: /* fall through */
1044 		case 4: /* fall through */
1045 		case 5: /* fall through */
1046 		case 6: /* fall through */
1047 		case 7:
1048 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_7;
1049 			break;
1050 		case 8:
1051 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_8;
1052 			break;
1053 		case 9:
1054 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
1055 			break;
1056 		default:
1057 			WARN_ON(1);
1058 			/* fall through */
1059 		case -1:
1060 			mcs = IEEE80211_VHT_MCS_NOT_SUPPORTED;
1061 			break;
1062 		}
1063 
1064 		tx_mcs_set &= ~(0x3 << (nss * 2));
1065 		tx_mcs_set |= mcs << (nss * 2);
1066 	}
1067 
1068 	return tx_mcs_set;
1069 }
1070 
1071 static void ath11k_peer_assoc_h_vht(struct ath11k *ar,
1072 				    struct ieee80211_vif *vif,
1073 				    struct ieee80211_sta *sta,
1074 				    struct peer_assoc_params *arg)
1075 {
1076 	const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
1077 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
1078 	struct cfg80211_chan_def def;
1079 	enum nl80211_band band;
1080 	const u16 *vht_mcs_mask;
1081 	u8 ampdu_factor;
1082 	u8 max_nss, vht_mcs;
1083 	int i;
1084 
1085 	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
1086 		return;
1087 
1088 	if (!vht_cap->vht_supported)
1089 		return;
1090 
1091 	band = def.chan->band;
1092 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
1093 
1094 	if (ath11k_peer_assoc_h_vht_masked(vht_mcs_mask))
1095 		return;
1096 
1097 	arg->vht_flag = true;
1098 
1099 	/* TODO: similar flags required? */
1100 	arg->vht_capable = true;
1101 
1102 	if (def.chan->band == NL80211_BAND_2GHZ)
1103 		arg->vht_ng_flag = true;
1104 
1105 	arg->peer_vht_caps = vht_cap->cap;
1106 
1107 	ampdu_factor = (vht_cap->cap &
1108 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
1109 		       IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
1110 
1111 	/* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
1112 	 * zero in VHT IE. Using it would result in degraded throughput.
1113 	 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
1114 	 * it if VHT max_mpdu is smaller.
1115 	 */
1116 	arg->peer_max_mpdu = max(arg->peer_max_mpdu,
1117 				 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1118 					ampdu_factor)) - 1);
1119 
1120 	if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1121 		arg->bw_80 = true;
1122 
1123 	if (sta->bandwidth == IEEE80211_STA_RX_BW_160)
1124 		arg->bw_160 = true;
1125 
1126 	/* Calculate peer NSS capability from VHT capabilities if STA
1127 	 * supports VHT.
1128 	 */
1129 	for (i = 0, max_nss = 0, vht_mcs = 0; i < NL80211_VHT_NSS_MAX; i++) {
1130 		vht_mcs = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) >>
1131 			  (2 * i) & 3;
1132 
1133 		if (vht_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED &&
1134 		    vht_mcs_mask[i])
1135 			max_nss = i + 1;
1136 	}
1137 	arg->peer_nss = min(sta->rx_nss, max_nss);
1138 	arg->rx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
1139 	arg->rx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
1140 	arg->tx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
1141 	arg->tx_mcs_set = ath11k_peer_assoc_h_vht_limit(
1142 		__le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map), vht_mcs_mask);
1143 
1144 	/* In IPQ8074 platform, VHT mcs rate 10 and 11 is enabled by default.
1145 	 * VHT mcs rate 10 and 11 is not suppoerted in 11ac standard.
1146 	 * so explicitly disable the VHT MCS rate 10 and 11 in 11ac mode.
1147 	 */
1148 	arg->tx_mcs_set &= ~IEEE80211_VHT_MCS_SUPPORT_0_11_MASK;
1149 	arg->tx_mcs_set |= IEEE80211_DISABLE_VHT_MCS_SUPPORT_0_11;
1150 
1151 	if ((arg->tx_mcs_set & IEEE80211_VHT_MCS_NOT_SUPPORTED) ==
1152 			IEEE80211_VHT_MCS_NOT_SUPPORTED)
1153 		arg->peer_vht_caps &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
1154 
1155 	/* TODO:  Check */
1156 	arg->tx_max_mcs_nss = 0xFF;
1157 
1158 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
1159 		   sta->addr, arg->peer_max_mpdu, arg->peer_flags);
1160 
1161 	/* TODO: rxnss_override */
1162 }
1163 
1164 static void ath11k_peer_assoc_h_he(struct ath11k *ar,
1165 				   struct ieee80211_vif *vif,
1166 				   struct ieee80211_sta *sta,
1167 				   struct peer_assoc_params *arg)
1168 {
1169 	const struct ieee80211_sta_he_cap *he_cap = &sta->he_cap;
1170 	u16 v;
1171 
1172 	if (!he_cap->has_he)
1173 		return;
1174 
1175 	arg->he_flag = true;
1176 
1177 	memcpy(&arg->peer_he_cap_macinfo, he_cap->he_cap_elem.mac_cap_info,
1178 	       sizeof(arg->peer_he_cap_macinfo));
1179 	memcpy(&arg->peer_he_cap_phyinfo, he_cap->he_cap_elem.phy_cap_info,
1180 	       sizeof(arg->peer_he_cap_phyinfo));
1181 	memcpy(&arg->peer_he_ops, &vif->bss_conf.he_operation,
1182 	       sizeof(arg->peer_he_ops));
1183 
1184 	/* the top most byte is used to indicate BSS color info */
1185 	arg->peer_he_ops &= 0xffffff;
1186 
1187 	if (he_cap->he_cap_elem.phy_cap_info[6] &
1188 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
1189 		int bit = 7;
1190 		int nss, ru;
1191 
1192 		arg->peer_ppet.numss_m1 = he_cap->ppe_thres[0] &
1193 					  IEEE80211_PPE_THRES_NSS_MASK;
1194 		arg->peer_ppet.ru_bit_mask =
1195 			(he_cap->ppe_thres[0] &
1196 			 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK) >>
1197 			IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS;
1198 
1199 		for (nss = 0; nss <= arg->peer_ppet.numss_m1; nss++) {
1200 			for (ru = 0; ru < 4; ru++) {
1201 				u32 val = 0;
1202 				int i;
1203 
1204 				if ((arg->peer_ppet.ru_bit_mask & BIT(ru)) == 0)
1205 					continue;
1206 				for (i = 0; i < 6; i++) {
1207 					val >>= 1;
1208 					val |= ((he_cap->ppe_thres[bit / 8] >>
1209 						 (bit % 8)) & 0x1) << 5;
1210 					bit++;
1211 				}
1212 				arg->peer_ppet.ppet16_ppet8_ru3_ru0[nss] |=
1213 								val << (ru * 6);
1214 			}
1215 		}
1216 	}
1217 
1218 	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_RES)
1219 		arg->twt_responder = true;
1220 	if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_REQ)
1221 		arg->twt_requester = true;
1222 
1223 	switch (sta->bandwidth) {
1224 	case IEEE80211_STA_RX_BW_160:
1225 		if (he_cap->he_cap_elem.phy_cap_info[0] &
1226 		    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G) {
1227 			v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80p80);
1228 			arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80_80] = v;
1229 
1230 			v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80p80);
1231 			arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80_80] = v;
1232 
1233 			arg->peer_he_mcs_count++;
1234 		}
1235 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
1236 		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
1237 
1238 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_160);
1239 		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
1240 
1241 		arg->peer_he_mcs_count++;
1242 		/* fall through */
1243 
1244 	default:
1245 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
1246 		arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
1247 
1248 		v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80);
1249 		arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
1250 
1251 		arg->peer_he_mcs_count++;
1252 		break;
1253 	}
1254 }
1255 
1256 static void ath11k_peer_assoc_h_smps(struct ieee80211_sta *sta,
1257 				     struct peer_assoc_params *arg)
1258 {
1259 	const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1260 	int smps;
1261 
1262 	if (!ht_cap->ht_supported)
1263 		return;
1264 
1265 	smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
1266 	smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
1267 
1268 	switch (smps) {
1269 	case WLAN_HT_CAP_SM_PS_STATIC:
1270 		arg->static_mimops_flag = true;
1271 		break;
1272 	case WLAN_HT_CAP_SM_PS_DYNAMIC:
1273 		arg->dynamic_mimops_flag = true;
1274 		break;
1275 	case WLAN_HT_CAP_SM_PS_DISABLED:
1276 		arg->spatial_mux_flag = true;
1277 		break;
1278 	default:
1279 		break;
1280 	}
1281 }
1282 
1283 static void ath11k_peer_assoc_h_qos(struct ath11k *ar,
1284 				    struct ieee80211_vif *vif,
1285 				    struct ieee80211_sta *sta,
1286 				    struct peer_assoc_params *arg)
1287 {
1288 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
1289 
1290 	switch (arvif->vdev_type) {
1291 	case WMI_VDEV_TYPE_AP:
1292 		if (sta->wme) {
1293 			/* TODO: Check WME vs QoS */
1294 			arg->is_wme_set = true;
1295 			arg->qos_flag = true;
1296 		}
1297 
1298 		if (sta->wme && sta->uapsd_queues) {
1299 			/* TODO: Check WME vs QoS */
1300 			arg->is_wme_set = true;
1301 			arg->apsd_flag = true;
1302 			arg->peer_rate_caps |= WMI_HOST_RC_UAPSD_FLAG;
1303 		}
1304 		break;
1305 	case WMI_VDEV_TYPE_STA:
1306 		if (sta->wme) {
1307 			arg->is_wme_set = true;
1308 			arg->qos_flag = true;
1309 		}
1310 		break;
1311 	default:
1312 		break;
1313 	}
1314 
1315 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac peer %pM qos %d\n",
1316 		   sta->addr, arg->qos_flag);
1317 }
1318 
1319 static int ath11k_peer_assoc_qos_ap(struct ath11k *ar,
1320 				    struct ath11k_vif *arvif,
1321 				    struct ieee80211_sta *sta)
1322 {
1323 	struct ap_ps_params params;
1324 	u32 max_sp;
1325 	u32 uapsd;
1326 	int ret;
1327 
1328 	lockdep_assert_held(&ar->conf_mutex);
1329 
1330 	params.vdev_id = arvif->vdev_id;
1331 
1332 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
1333 		   sta->uapsd_queues, sta->max_sp);
1334 
1335 	uapsd = 0;
1336 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1337 		uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
1338 			 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
1339 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1340 		uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
1341 			 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
1342 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1343 		uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
1344 			 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
1345 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1346 		uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
1347 			 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
1348 
1349 	max_sp = 0;
1350 	if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
1351 		max_sp = sta->max_sp;
1352 
1353 	params.param = WMI_AP_PS_PEER_PARAM_UAPSD;
1354 	params.value = uapsd;
1355 	ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
1356 	if (ret)
1357 		goto err;
1358 
1359 	params.param = WMI_AP_PS_PEER_PARAM_MAX_SP;
1360 	params.value = max_sp;
1361 	ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
1362 	if (ret)
1363 		goto err;
1364 
1365 	/* TODO revisit during testing */
1366 	params.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_FRMTYPE;
1367 	params.value = DISABLE_SIFS_RESPONSE_TRIGGER;
1368 	ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
1369 	if (ret)
1370 		goto err;
1371 
1372 	params.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_UAPSD;
1373 	params.value = DISABLE_SIFS_RESPONSE_TRIGGER;
1374 	ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
1375 	if (ret)
1376 		goto err;
1377 
1378 	return 0;
1379 
1380 err:
1381 	ath11k_warn(ar->ab, "failed to set ap ps peer param %d for vdev %i: %d\n",
1382 		    params.param, arvif->vdev_id, ret);
1383 	return ret;
1384 }
1385 
1386 static bool ath11k_mac_sta_has_ofdm_only(struct ieee80211_sta *sta)
1387 {
1388 	return sta->supp_rates[NL80211_BAND_2GHZ] >>
1389 	       ATH11K_MAC_FIRST_OFDM_RATE_IDX;
1390 }
1391 
1392 static enum wmi_phy_mode ath11k_mac_get_phymode_vht(struct ath11k *ar,
1393 						    struct ieee80211_sta *sta)
1394 {
1395 	if (sta->bandwidth == IEEE80211_STA_RX_BW_160) {
1396 		switch (sta->vht_cap.cap &
1397 			IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK) {
1398 		case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
1399 			return MODE_11AC_VHT160;
1400 		case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
1401 			return MODE_11AC_VHT80_80;
1402 		default:
1403 			/* not sure if this is a valid case? */
1404 			return MODE_11AC_VHT160;
1405 		}
1406 	}
1407 
1408 	if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1409 		return MODE_11AC_VHT80;
1410 
1411 	if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1412 		return MODE_11AC_VHT40;
1413 
1414 	if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
1415 		return MODE_11AC_VHT20;
1416 
1417 	return MODE_UNKNOWN;
1418 }
1419 
1420 static enum wmi_phy_mode ath11k_mac_get_phymode_he(struct ath11k *ar,
1421 						   struct ieee80211_sta *sta)
1422 {
1423 	if (sta->bandwidth == IEEE80211_STA_RX_BW_160) {
1424 		if (sta->he_cap.he_cap_elem.phy_cap_info[0] &
1425 		     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
1426 			return MODE_11AX_HE160;
1427 		else if (sta->he_cap.he_cap_elem.phy_cap_info[0] &
1428 		     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
1429 			return MODE_11AX_HE80_80;
1430 		/* not sure if this is a valid case? */
1431 		return MODE_11AX_HE160;
1432 	}
1433 
1434 	if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1435 		return MODE_11AX_HE80;
1436 
1437 	if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1438 		return MODE_11AX_HE40;
1439 
1440 	if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
1441 		return MODE_11AX_HE20;
1442 
1443 	return MODE_UNKNOWN;
1444 }
1445 
1446 static void ath11k_peer_assoc_h_phymode(struct ath11k *ar,
1447 					struct ieee80211_vif *vif,
1448 					struct ieee80211_sta *sta,
1449 					struct peer_assoc_params *arg)
1450 {
1451 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
1452 	struct cfg80211_chan_def def;
1453 	enum nl80211_band band;
1454 	const u8 *ht_mcs_mask;
1455 	const u16 *vht_mcs_mask;
1456 	enum wmi_phy_mode phymode = MODE_UNKNOWN;
1457 
1458 	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
1459 		return;
1460 
1461 	band = def.chan->band;
1462 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
1463 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
1464 
1465 	switch (band) {
1466 	case NL80211_BAND_2GHZ:
1467 		if (sta->he_cap.has_he) {
1468 			if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1469 				phymode = MODE_11AX_HE80_2G;
1470 			else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1471 				phymode = MODE_11AX_HE40_2G;
1472 			else
1473 				phymode = MODE_11AX_HE20_2G;
1474 		} else if (sta->vht_cap.vht_supported &&
1475 		    !ath11k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
1476 			if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1477 				phymode = MODE_11AC_VHT40;
1478 			else
1479 				phymode = MODE_11AC_VHT20;
1480 		} else if (sta->ht_cap.ht_supported &&
1481 			   !ath11k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
1482 			if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1483 				phymode = MODE_11NG_HT40;
1484 			else
1485 				phymode = MODE_11NG_HT20;
1486 		} else if (ath11k_mac_sta_has_ofdm_only(sta)) {
1487 			phymode = MODE_11G;
1488 		} else {
1489 			phymode = MODE_11B;
1490 		}
1491 		break;
1492 	case NL80211_BAND_5GHZ:
1493 		/* Check HE first */
1494 		if (sta->he_cap.has_he) {
1495 			phymode = ath11k_mac_get_phymode_he(ar, sta);
1496 		} else if (sta->vht_cap.vht_supported &&
1497 		    !ath11k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
1498 			phymode = ath11k_mac_get_phymode_vht(ar, sta);
1499 		} else if (sta->ht_cap.ht_supported &&
1500 			   !ath11k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
1501 			if (sta->bandwidth >= IEEE80211_STA_RX_BW_40)
1502 				phymode = MODE_11NA_HT40;
1503 			else
1504 				phymode = MODE_11NA_HT20;
1505 		} else {
1506 			phymode = MODE_11A;
1507 		}
1508 		break;
1509 	default:
1510 		break;
1511 	}
1512 
1513 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac peer %pM phymode %s\n",
1514 		   sta->addr, ath11k_wmi_phymode_str(phymode));
1515 
1516 	arg->peer_phymode = phymode;
1517 	WARN_ON(phymode == MODE_UNKNOWN);
1518 }
1519 
1520 static void ath11k_peer_assoc_prepare(struct ath11k *ar,
1521 				      struct ieee80211_vif *vif,
1522 				      struct ieee80211_sta *sta,
1523 				      struct peer_assoc_params *arg,
1524 				      bool reassoc)
1525 {
1526 	lockdep_assert_held(&ar->conf_mutex);
1527 
1528 	memset(arg, 0, sizeof(*arg));
1529 
1530 	reinit_completion(&ar->peer_assoc_done);
1531 
1532 	arg->peer_new_assoc = !reassoc;
1533 	ath11k_peer_assoc_h_basic(ar, vif, sta, arg);
1534 	ath11k_peer_assoc_h_crypto(ar, vif, sta, arg);
1535 	ath11k_peer_assoc_h_rates(ar, vif, sta, arg);
1536 	ath11k_peer_assoc_h_ht(ar, vif, sta, arg);
1537 	ath11k_peer_assoc_h_vht(ar, vif, sta, arg);
1538 	ath11k_peer_assoc_h_he(ar, vif, sta, arg);
1539 	ath11k_peer_assoc_h_qos(ar, vif, sta, arg);
1540 	ath11k_peer_assoc_h_phymode(ar, vif, sta, arg);
1541 	ath11k_peer_assoc_h_smps(sta, arg);
1542 
1543 	/* TODO: amsdu_disable req? */
1544 }
1545 
1546 static int ath11k_setup_peer_smps(struct ath11k *ar, struct ath11k_vif *arvif,
1547 				  const u8 *addr,
1548 				  const struct ieee80211_sta_ht_cap *ht_cap)
1549 {
1550 	int smps;
1551 
1552 	if (!ht_cap->ht_supported)
1553 		return 0;
1554 
1555 	smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
1556 	smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
1557 
1558 	if (smps >= ARRAY_SIZE(ath11k_smps_map))
1559 		return -EINVAL;
1560 
1561 	return ath11k_wmi_set_peer_param(ar, addr, arvif->vdev_id,
1562 					 WMI_PEER_MIMO_PS_STATE,
1563 					 ath11k_smps_map[smps]);
1564 }
1565 
1566 static void ath11k_bss_assoc(struct ieee80211_hw *hw,
1567 			     struct ieee80211_vif *vif,
1568 			     struct ieee80211_bss_conf *bss_conf)
1569 {
1570 	struct ath11k *ar = hw->priv;
1571 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
1572 	struct peer_assoc_params peer_arg;
1573 	struct ieee80211_sta *ap_sta;
1574 	int ret;
1575 
1576 	lockdep_assert_held(&ar->conf_mutex);
1577 
1578 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
1579 		   arvif->vdev_id, arvif->bssid, arvif->aid);
1580 
1581 	rcu_read_lock();
1582 
1583 	ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
1584 	if (!ap_sta) {
1585 		ath11k_warn(ar->ab, "failed to find station entry for bss %pM vdev %i\n",
1586 			    bss_conf->bssid, arvif->vdev_id);
1587 		rcu_read_unlock();
1588 		return;
1589 	}
1590 
1591 	ath11k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg, false);
1592 
1593 	rcu_read_unlock();
1594 
1595 	ret = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
1596 	if (ret) {
1597 		ath11k_warn(ar->ab, "failed to run peer assoc for %pM vdev %i: %d\n",
1598 			    bss_conf->bssid, arvif->vdev_id, ret);
1599 		return;
1600 	}
1601 
1602 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
1603 		ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
1604 			    bss_conf->bssid, arvif->vdev_id);
1605 		return;
1606 	}
1607 
1608 	ret = ath11k_setup_peer_smps(ar, arvif, bss_conf->bssid,
1609 				     &ap_sta->ht_cap);
1610 	if (ret) {
1611 		ath11k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
1612 			    arvif->vdev_id, ret);
1613 		return;
1614 	}
1615 
1616 	WARN_ON(arvif->is_up);
1617 
1618 	arvif->aid = bss_conf->aid;
1619 	ether_addr_copy(arvif->bssid, bss_conf->bssid);
1620 
1621 	ret = ath11k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
1622 	if (ret) {
1623 		ath11k_warn(ar->ab, "failed to set vdev %d up: %d\n",
1624 			    arvif->vdev_id, ret);
1625 		return;
1626 	}
1627 
1628 	arvif->is_up = true;
1629 
1630 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
1631 		   "mac vdev %d up (associated) bssid %pM aid %d\n",
1632 		   arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
1633 
1634 	/* Authorize BSS Peer */
1635 	ret = ath11k_wmi_set_peer_param(ar, arvif->bssid,
1636 					arvif->vdev_id,
1637 					WMI_PEER_AUTHORIZE,
1638 					1);
1639 	if (ret)
1640 		ath11k_warn(ar->ab, "Unable to authorize BSS peer: %d\n", ret);
1641 
1642 	ret = ath11k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
1643 					   &bss_conf->he_obss_pd);
1644 	if (ret)
1645 		ath11k_warn(ar->ab, "failed to set vdev %i OBSS PD parameters: %d\n",
1646 			    arvif->vdev_id, ret);
1647 }
1648 
1649 static void ath11k_bss_disassoc(struct ieee80211_hw *hw,
1650 				struct ieee80211_vif *vif)
1651 {
1652 	struct ath11k *ar = hw->priv;
1653 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
1654 	int ret;
1655 
1656 	lockdep_assert_held(&ar->conf_mutex);
1657 
1658 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
1659 		   arvif->vdev_id, arvif->bssid);
1660 
1661 	ret = ath11k_wmi_vdev_down(ar, arvif->vdev_id);
1662 	if (ret)
1663 		ath11k_warn(ar->ab, "failed to down vdev %i: %d\n",
1664 			    arvif->vdev_id, ret);
1665 
1666 	arvif->is_up = false;
1667 
1668 	/* TODO: cancel connection_loss_work */
1669 }
1670 
1671 static u32 ath11k_mac_get_rate_hw_value(int bitrate)
1672 {
1673 	u32 preamble;
1674 	u16 hw_value;
1675 	int rate;
1676 	size_t i;
1677 
1678 	if (ath11k_mac_bitrate_is_cck(bitrate))
1679 		preamble = WMI_RATE_PREAMBLE_CCK;
1680 	else
1681 		preamble = WMI_RATE_PREAMBLE_OFDM;
1682 
1683 	for (i = 0; i < ARRAY_SIZE(ath11k_legacy_rates); i++) {
1684 		if (ath11k_legacy_rates[i].bitrate != bitrate)
1685 			continue;
1686 
1687 		hw_value = ath11k_legacy_rates[i].hw_value;
1688 		rate = ATH11K_HW_RATE_CODE(hw_value, 0, preamble);
1689 
1690 		return rate;
1691 	}
1692 
1693 	return -EINVAL;
1694 }
1695 
1696 static void ath11k_recalculate_mgmt_rate(struct ath11k *ar,
1697 					 struct ieee80211_vif *vif,
1698 					 struct cfg80211_chan_def *def)
1699 {
1700 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
1701 	const struct ieee80211_supported_band *sband;
1702 	u8 basic_rate_idx;
1703 	int hw_rate_code;
1704 	u32 vdev_param;
1705 	u16 bitrate;
1706 	int ret;
1707 
1708 	lockdep_assert_held(&ar->conf_mutex);
1709 
1710 	sband = ar->hw->wiphy->bands[def->chan->band];
1711 	basic_rate_idx = ffs(vif->bss_conf.basic_rates) - 1;
1712 	bitrate = sband->bitrates[basic_rate_idx].bitrate;
1713 
1714 	hw_rate_code = ath11k_mac_get_rate_hw_value(bitrate);
1715 	if (hw_rate_code < 0) {
1716 		ath11k_warn(ar->ab, "bitrate not supported %d\n", bitrate);
1717 		return;
1718 	}
1719 
1720 	vdev_param = WMI_VDEV_PARAM_MGMT_RATE;
1721 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
1722 					    hw_rate_code);
1723 	if (ret)
1724 		ath11k_warn(ar->ab, "failed to set mgmt tx rate %d\n", ret);
1725 
1726 	vdev_param = WMI_VDEV_PARAM_BEACON_RATE;
1727 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
1728 					    hw_rate_code);
1729 	if (ret)
1730 		ath11k_warn(ar->ab, "failed to set beacon tx rate %d\n", ret);
1731 }
1732 
1733 static void ath11k_mac_op_bss_info_changed(struct ieee80211_hw *hw,
1734 					   struct ieee80211_vif *vif,
1735 					   struct ieee80211_bss_conf *info,
1736 					   u32 changed)
1737 {
1738 	struct ath11k *ar = hw->priv;
1739 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
1740 	struct cfg80211_chan_def def;
1741 	u32 param_id, param_value;
1742 	enum nl80211_band band;
1743 	u32 vdev_param;
1744 	int mcast_rate;
1745 	u32 preamble;
1746 	u16 hw_value;
1747 	u16 bitrate;
1748 	int ret = 0;
1749 	u8 rateidx;
1750 	u32 rate;
1751 
1752 	mutex_lock(&ar->conf_mutex);
1753 
1754 	if (changed & BSS_CHANGED_BEACON_INT) {
1755 		arvif->beacon_interval = info->beacon_int;
1756 
1757 		param_id = WMI_VDEV_PARAM_BEACON_INTERVAL;
1758 		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
1759 						    param_id,
1760 						    arvif->beacon_interval);
1761 		if (ret)
1762 			ath11k_warn(ar->ab, "Failed to set beacon interval for VDEV: %d\n",
1763 				    arvif->vdev_id);
1764 		else
1765 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
1766 				   "Beacon interval: %d set for VDEV: %d\n",
1767 				   arvif->beacon_interval, arvif->vdev_id);
1768 	}
1769 
1770 	if (changed & BSS_CHANGED_BEACON) {
1771 		param_id = WMI_PDEV_PARAM_BEACON_TX_MODE;
1772 		param_value = WMI_BEACON_STAGGERED_MODE;
1773 		ret = ath11k_wmi_pdev_set_param(ar, param_id,
1774 						param_value, ar->pdev->pdev_id);
1775 		if (ret)
1776 			ath11k_warn(ar->ab, "Failed to set beacon mode for VDEV: %d\n",
1777 				    arvif->vdev_id);
1778 		else
1779 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
1780 				   "Set staggered beacon mode for VDEV: %d\n",
1781 				   arvif->vdev_id);
1782 
1783 		ret = ath11k_mac_setup_bcn_tmpl(arvif);
1784 		if (ret)
1785 			ath11k_warn(ar->ab, "failed to update bcn template: %d\n",
1786 				    ret);
1787 
1788 		if (vif->bss_conf.he_support) {
1789 			ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
1790 							    WMI_VDEV_PARAM_BA_MODE,
1791 							    WMI_BA_MODE_BUFFER_SIZE_256);
1792 			if (ret)
1793 				ath11k_warn(ar->ab,
1794 					    "failed to set BA BUFFER SIZE 256 for vdev: %d\n",
1795 					    arvif->vdev_id);
1796 			else
1797 				ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
1798 					   "Set BA BUFFER SIZE 256 for VDEV: %d\n",
1799 					   arvif->vdev_id);
1800 		}
1801 	}
1802 
1803 	if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
1804 		arvif->dtim_period = info->dtim_period;
1805 
1806 		param_id = WMI_VDEV_PARAM_DTIM_PERIOD;
1807 		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
1808 						    param_id,
1809 						    arvif->dtim_period);
1810 
1811 		if (ret)
1812 			ath11k_warn(ar->ab, "Failed to set dtim period for VDEV %d: %i\n",
1813 				    arvif->vdev_id, ret);
1814 		else
1815 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
1816 				   "DTIM period: %d set for VDEV: %d\n",
1817 				   arvif->dtim_period, arvif->vdev_id);
1818 	}
1819 
1820 	if (changed & BSS_CHANGED_SSID &&
1821 	    vif->type == NL80211_IFTYPE_AP) {
1822 		arvif->u.ap.ssid_len = info->ssid_len;
1823 		if (info->ssid_len)
1824 			memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
1825 		arvif->u.ap.hidden_ssid = info->hidden_ssid;
1826 	}
1827 
1828 	if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
1829 		ether_addr_copy(arvif->bssid, info->bssid);
1830 
1831 	if (changed & BSS_CHANGED_BEACON_ENABLED)
1832 		ath11k_control_beaconing(arvif, info);
1833 
1834 	if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1835 		u32 cts_prot;
1836 
1837 		cts_prot = !!(info->use_cts_prot);
1838 		param_id = WMI_VDEV_PARAM_PROTECTION_MODE;
1839 
1840 		if (arvif->is_started) {
1841 			ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
1842 							    param_id, cts_prot);
1843 			if (ret)
1844 				ath11k_warn(ar->ab, "Failed to set CTS prot for VDEV: %d\n",
1845 					    arvif->vdev_id);
1846 			else
1847 				ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "Set CTS prot: %d for VDEV: %d\n",
1848 					   cts_prot, arvif->vdev_id);
1849 		} else {
1850 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "defer protection mode setup, vdev is not ready yet\n");
1851 		}
1852 	}
1853 
1854 	if (changed & BSS_CHANGED_ERP_SLOT) {
1855 		u32 slottime;
1856 
1857 		if (info->use_short_slot)
1858 			slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
1859 
1860 		else
1861 			slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
1862 
1863 		param_id = WMI_VDEV_PARAM_SLOT_TIME;
1864 		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
1865 						    param_id, slottime);
1866 		if (ret)
1867 			ath11k_warn(ar->ab, "Failed to set erp slot for VDEV: %d\n",
1868 				    arvif->vdev_id);
1869 		else
1870 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
1871 				   "Set slottime: %d for VDEV: %d\n",
1872 				   slottime, arvif->vdev_id);
1873 	}
1874 
1875 	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1876 		u32 preamble;
1877 
1878 		if (info->use_short_preamble)
1879 			preamble = WMI_VDEV_PREAMBLE_SHORT;
1880 		else
1881 			preamble = WMI_VDEV_PREAMBLE_LONG;
1882 
1883 		param_id = WMI_VDEV_PARAM_PREAMBLE;
1884 		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
1885 						    param_id, preamble);
1886 		if (ret)
1887 			ath11k_warn(ar->ab, "Failed to set preamble for VDEV: %d\n",
1888 				    arvif->vdev_id);
1889 		else
1890 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
1891 				   "Set preamble: %d for VDEV: %d\n",
1892 				   preamble, arvif->vdev_id);
1893 	}
1894 
1895 	if (changed & BSS_CHANGED_ASSOC) {
1896 		if (info->assoc)
1897 			ath11k_bss_assoc(hw, vif, info);
1898 		else
1899 			ath11k_bss_disassoc(hw, vif);
1900 	}
1901 
1902 	if (changed & BSS_CHANGED_TXPOWER) {
1903 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev_id %i txpower %d\n",
1904 			   arvif->vdev_id, info->txpower);
1905 
1906 		arvif->txpower = info->txpower;
1907 		ath11k_mac_txpower_recalc(ar);
1908 	}
1909 
1910 	if (changed & BSS_CHANGED_MCAST_RATE &&
1911 	    !ath11k_mac_vif_chan(arvif->vif, &def)) {
1912 		band = def.chan->band;
1913 		mcast_rate = vif->bss_conf.mcast_rate[band];
1914 
1915 		if (mcast_rate > 0)
1916 			rateidx = mcast_rate - 1;
1917 		else
1918 			rateidx = ffs(vif->bss_conf.basic_rates) - 1;
1919 
1920 		if (ar->pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP)
1921 			rateidx += ATH11K_MAC_FIRST_OFDM_RATE_IDX;
1922 
1923 		bitrate = ath11k_legacy_rates[rateidx].bitrate;
1924 		hw_value = ath11k_legacy_rates[rateidx].hw_value;
1925 
1926 		if (ath11k_mac_bitrate_is_cck(bitrate))
1927 			preamble = WMI_RATE_PREAMBLE_CCK;
1928 		else
1929 			preamble = WMI_RATE_PREAMBLE_OFDM;
1930 
1931 		rate = ATH11K_HW_RATE_CODE(hw_value, 0, preamble);
1932 
1933 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
1934 			   "mac vdev %d mcast_rate %x\n",
1935 			   arvif->vdev_id, rate);
1936 
1937 		vdev_param = WMI_VDEV_PARAM_MCAST_DATA_RATE;
1938 		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
1939 						    vdev_param, rate);
1940 		if (ret)
1941 			ath11k_warn(ar->ab,
1942 				    "failed to set mcast rate on vdev %i: %d\n",
1943 				    arvif->vdev_id,  ret);
1944 
1945 		vdev_param = WMI_VDEV_PARAM_BCAST_DATA_RATE;
1946 		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
1947 						    vdev_param, rate);
1948 		if (ret)
1949 			ath11k_warn(ar->ab,
1950 				    "failed to set bcast rate on vdev %i: %d\n",
1951 				    arvif->vdev_id,  ret);
1952 	}
1953 
1954 	if (changed & BSS_CHANGED_BASIC_RATES &&
1955 	    !ath11k_mac_vif_chan(arvif->vif, &def))
1956 		ath11k_recalculate_mgmt_rate(ar, vif, &def);
1957 
1958 	if (changed & BSS_CHANGED_TWT) {
1959 		if (info->twt_requester || info->twt_responder)
1960 			ath11k_wmi_send_twt_enable_cmd(ar, ar->pdev->pdev_id);
1961 		else
1962 			ath11k_wmi_send_twt_disable_cmd(ar, ar->pdev->pdev_id);
1963 	}
1964 
1965 	if (changed & BSS_CHANGED_HE_OBSS_PD)
1966 		ath11k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
1967 					     &info->he_obss_pd);
1968 
1969 	if (changed & BSS_CHANGED_HE_BSS_COLOR) {
1970 		if (vif->type == NL80211_IFTYPE_AP) {
1971 			ret = ath11k_wmi_send_obss_color_collision_cfg_cmd(
1972 				ar, arvif->vdev_id, info->he_bss_color.color,
1973 				ATH11K_BSS_COLOR_COLLISION_DETECTION_AP_PERIOD_MS,
1974 				!info->he_bss_color.disabled);
1975 			if (ret)
1976 				ath11k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
1977 					    arvif->vdev_id,  ret);
1978 		} else if (vif->type == NL80211_IFTYPE_STATION) {
1979 			ret = ath11k_wmi_send_bss_color_change_enable_cmd(ar,
1980 									  arvif->vdev_id,
1981 									  1);
1982 			if (ret)
1983 				ath11k_warn(ar->ab, "failed to enable bss color change on vdev %i: %d\n",
1984 					    arvif->vdev_id,  ret);
1985 			ret = ath11k_wmi_send_obss_color_collision_cfg_cmd(
1986 				ar, arvif->vdev_id, 0,
1987 				ATH11K_BSS_COLOR_COLLISION_DETECTION_STA_PERIOD_MS, 1);
1988 			if (ret)
1989 				ath11k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
1990 					    arvif->vdev_id,  ret);
1991 		}
1992 	}
1993 
1994 	mutex_unlock(&ar->conf_mutex);
1995 }
1996 
1997 void __ath11k_mac_scan_finish(struct ath11k *ar)
1998 {
1999 	lockdep_assert_held(&ar->data_lock);
2000 
2001 	switch (ar->scan.state) {
2002 	case ATH11K_SCAN_IDLE:
2003 		break;
2004 	case ATH11K_SCAN_RUNNING:
2005 	case ATH11K_SCAN_ABORTING:
2006 		if (!ar->scan.is_roc) {
2007 			struct cfg80211_scan_info info = {
2008 				.aborted = (ar->scan.state ==
2009 					    ATH11K_SCAN_ABORTING),
2010 			};
2011 
2012 			ieee80211_scan_completed(ar->hw, &info);
2013 		} else if (ar->scan.roc_notify) {
2014 			ieee80211_remain_on_channel_expired(ar->hw);
2015 		}
2016 		/* fall through */
2017 	case ATH11K_SCAN_STARTING:
2018 		ar->scan.state = ATH11K_SCAN_IDLE;
2019 		ar->scan_channel = NULL;
2020 		ar->scan.roc_freq = 0;
2021 		cancel_delayed_work(&ar->scan.timeout);
2022 		complete(&ar->scan.completed);
2023 		break;
2024 	}
2025 }
2026 
2027 void ath11k_mac_scan_finish(struct ath11k *ar)
2028 {
2029 	spin_lock_bh(&ar->data_lock);
2030 	__ath11k_mac_scan_finish(ar);
2031 	spin_unlock_bh(&ar->data_lock);
2032 }
2033 
2034 static int ath11k_scan_stop(struct ath11k *ar)
2035 {
2036 	struct scan_cancel_param arg = {
2037 		.req_type = WLAN_SCAN_CANCEL_SINGLE,
2038 		.scan_id = ATH11K_SCAN_ID,
2039 	};
2040 	int ret;
2041 
2042 	lockdep_assert_held(&ar->conf_mutex);
2043 
2044 	/* TODO: Fill other STOP Params */
2045 	arg.pdev_id = ar->pdev->pdev_id;
2046 
2047 	ret = ath11k_wmi_send_scan_stop_cmd(ar, &arg);
2048 	if (ret) {
2049 		ath11k_warn(ar->ab, "failed to stop wmi scan: %d\n", ret);
2050 		goto out;
2051 	}
2052 
2053 	ret = wait_for_completion_timeout(&ar->scan.completed, 3 * HZ);
2054 	if (ret == 0) {
2055 		ath11k_warn(ar->ab,
2056 			    "failed to receive scan abort comple: timed out\n");
2057 		ret = -ETIMEDOUT;
2058 	} else if (ret > 0) {
2059 		ret = 0;
2060 	}
2061 
2062 out:
2063 	/* Scan state should be updated upon scan completion but in case
2064 	 * firmware fails to deliver the event (for whatever reason) it is
2065 	 * desired to clean up scan state anyway. Firmware may have just
2066 	 * dropped the scan completion event delivery due to transport pipe
2067 	 * being overflown with data and/or it can recover on its own before
2068 	 * next scan request is submitted.
2069 	 */
2070 	spin_lock_bh(&ar->data_lock);
2071 	if (ar->scan.state != ATH11K_SCAN_IDLE)
2072 		__ath11k_mac_scan_finish(ar);
2073 	spin_unlock_bh(&ar->data_lock);
2074 
2075 	return ret;
2076 }
2077 
2078 static void ath11k_scan_abort(struct ath11k *ar)
2079 {
2080 	int ret;
2081 
2082 	lockdep_assert_held(&ar->conf_mutex);
2083 
2084 	spin_lock_bh(&ar->data_lock);
2085 
2086 	switch (ar->scan.state) {
2087 	case ATH11K_SCAN_IDLE:
2088 		/* This can happen if timeout worker kicked in and called
2089 		 * abortion while scan completion was being processed.
2090 		 */
2091 		break;
2092 	case ATH11K_SCAN_STARTING:
2093 	case ATH11K_SCAN_ABORTING:
2094 		ath11k_warn(ar->ab, "refusing scan abortion due to invalid scan state: %d\n",
2095 			    ar->scan.state);
2096 		break;
2097 	case ATH11K_SCAN_RUNNING:
2098 		ar->scan.state = ATH11K_SCAN_ABORTING;
2099 		spin_unlock_bh(&ar->data_lock);
2100 
2101 		ret = ath11k_scan_stop(ar);
2102 		if (ret)
2103 			ath11k_warn(ar->ab, "failed to abort scan: %d\n", ret);
2104 
2105 		spin_lock_bh(&ar->data_lock);
2106 		break;
2107 	}
2108 
2109 	spin_unlock_bh(&ar->data_lock);
2110 }
2111 
2112 static void ath11k_scan_timeout_work(struct work_struct *work)
2113 {
2114 	struct ath11k *ar = container_of(work, struct ath11k,
2115 					 scan.timeout.work);
2116 
2117 	mutex_lock(&ar->conf_mutex);
2118 	ath11k_scan_abort(ar);
2119 	mutex_unlock(&ar->conf_mutex);
2120 }
2121 
2122 static int ath11k_start_scan(struct ath11k *ar,
2123 			     struct scan_req_params *arg)
2124 {
2125 	int ret;
2126 
2127 	lockdep_assert_held(&ar->conf_mutex);
2128 
2129 	ret = ath11k_wmi_send_scan_start_cmd(ar, arg);
2130 	if (ret)
2131 		return ret;
2132 
2133 	ret = wait_for_completion_timeout(&ar->scan.started, 1 * HZ);
2134 	if (ret == 0) {
2135 		ret = ath11k_scan_stop(ar);
2136 		if (ret)
2137 			ath11k_warn(ar->ab, "failed to stop scan: %d\n", ret);
2138 
2139 		return -ETIMEDOUT;
2140 	}
2141 
2142 	/* If we failed to start the scan, return error code at
2143 	 * this point.  This is probably due to some issue in the
2144 	 * firmware, but no need to wedge the driver due to that...
2145 	 */
2146 	spin_lock_bh(&ar->data_lock);
2147 	if (ar->scan.state == ATH11K_SCAN_IDLE) {
2148 		spin_unlock_bh(&ar->data_lock);
2149 		return -EINVAL;
2150 	}
2151 	spin_unlock_bh(&ar->data_lock);
2152 
2153 	return 0;
2154 }
2155 
2156 static int ath11k_mac_op_hw_scan(struct ieee80211_hw *hw,
2157 				 struct ieee80211_vif *vif,
2158 				 struct ieee80211_scan_request *hw_req)
2159 {
2160 	struct ath11k *ar = hw->priv;
2161 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
2162 	struct cfg80211_scan_request *req = &hw_req->req;
2163 	struct scan_req_params arg;
2164 	int ret = 0;
2165 	int i;
2166 
2167 	mutex_lock(&ar->conf_mutex);
2168 
2169 	spin_lock_bh(&ar->data_lock);
2170 	switch (ar->scan.state) {
2171 	case ATH11K_SCAN_IDLE:
2172 		reinit_completion(&ar->scan.started);
2173 		reinit_completion(&ar->scan.completed);
2174 		ar->scan.state = ATH11K_SCAN_STARTING;
2175 		ar->scan.is_roc = false;
2176 		ar->scan.vdev_id = arvif->vdev_id;
2177 		ret = 0;
2178 		break;
2179 	case ATH11K_SCAN_STARTING:
2180 	case ATH11K_SCAN_RUNNING:
2181 	case ATH11K_SCAN_ABORTING:
2182 		ret = -EBUSY;
2183 		break;
2184 	}
2185 	spin_unlock_bh(&ar->data_lock);
2186 
2187 	if (ret)
2188 		goto exit;
2189 
2190 	memset(&arg, 0, sizeof(arg));
2191 	ath11k_wmi_start_scan_init(ar, &arg);
2192 	arg.vdev_id = arvif->vdev_id;
2193 	arg.scan_id = ATH11K_SCAN_ID;
2194 
2195 	if (req->ie_len) {
2196 		arg.extraie.len = req->ie_len;
2197 		arg.extraie.ptr = kzalloc(req->ie_len, GFP_KERNEL);
2198 		memcpy(arg.extraie.ptr, req->ie, req->ie_len);
2199 	}
2200 
2201 	if (req->n_ssids) {
2202 		arg.num_ssids = req->n_ssids;
2203 		for (i = 0; i < arg.num_ssids; i++) {
2204 			arg.ssid[i].length  = req->ssids[i].ssid_len;
2205 			memcpy(&arg.ssid[i].ssid, req->ssids[i].ssid,
2206 			       req->ssids[i].ssid_len);
2207 		}
2208 	} else {
2209 		arg.scan_flags |= WMI_SCAN_FLAG_PASSIVE;
2210 	}
2211 
2212 	if (req->n_channels) {
2213 		arg.num_chan = req->n_channels;
2214 		for (i = 0; i < arg.num_chan; i++)
2215 			arg.chan_list[i] = req->channels[i]->center_freq;
2216 	}
2217 
2218 	ret = ath11k_start_scan(ar, &arg);
2219 	if (ret) {
2220 		ath11k_warn(ar->ab, "failed to start hw scan: %d\n", ret);
2221 		spin_lock_bh(&ar->data_lock);
2222 		ar->scan.state = ATH11K_SCAN_IDLE;
2223 		spin_unlock_bh(&ar->data_lock);
2224 	}
2225 
2226 	/* Add a 200ms margin to account for event/command processing */
2227 	ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
2228 				     msecs_to_jiffies(arg.max_scan_time +
2229 						      ATH11K_MAC_SCAN_TIMEOUT_MSECS));
2230 
2231 exit:
2232 	if (req->ie_len)
2233 		kfree(arg.extraie.ptr);
2234 
2235 	mutex_unlock(&ar->conf_mutex);
2236 	return ret;
2237 }
2238 
2239 static void ath11k_mac_op_cancel_hw_scan(struct ieee80211_hw *hw,
2240 					 struct ieee80211_vif *vif)
2241 {
2242 	struct ath11k *ar = hw->priv;
2243 
2244 	mutex_lock(&ar->conf_mutex);
2245 	ath11k_scan_abort(ar);
2246 	mutex_unlock(&ar->conf_mutex);
2247 
2248 	cancel_delayed_work_sync(&ar->scan.timeout);
2249 }
2250 
2251 static int ath11k_install_key(struct ath11k_vif *arvif,
2252 			      struct ieee80211_key_conf *key,
2253 			      enum set_key_cmd cmd,
2254 			      const u8 *macaddr, u32 flags)
2255 {
2256 	int ret;
2257 	struct ath11k *ar = arvif->ar;
2258 	struct wmi_vdev_install_key_arg arg = {
2259 		.vdev_id = arvif->vdev_id,
2260 		.key_idx = key->keyidx,
2261 		.key_len = key->keylen,
2262 		.key_data = key->key,
2263 		.key_flags = flags,
2264 		.macaddr = macaddr,
2265 	};
2266 
2267 	lockdep_assert_held(&arvif->ar->conf_mutex);
2268 
2269 	reinit_completion(&ar->install_key_done);
2270 
2271 	if (cmd == DISABLE_KEY) {
2272 		/* TODO: Check if FW expects  value other than NONE for del */
2273 		/* arg.key_cipher = WMI_CIPHER_NONE; */
2274 		arg.key_len = 0;
2275 		arg.key_data = NULL;
2276 		goto install;
2277 	}
2278 
2279 	switch (key->cipher) {
2280 	case WLAN_CIPHER_SUITE_CCMP:
2281 		arg.key_cipher = WMI_CIPHER_AES_CCM;
2282 		/* TODO: Re-check if flag is valid */
2283 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
2284 		break;
2285 	case WLAN_CIPHER_SUITE_TKIP:
2286 		arg.key_cipher = WMI_CIPHER_TKIP;
2287 		arg.key_txmic_len = 8;
2288 		arg.key_rxmic_len = 8;
2289 		break;
2290 	case WLAN_CIPHER_SUITE_CCMP_256:
2291 		arg.key_cipher = WMI_CIPHER_AES_CCM;
2292 		break;
2293 	case WLAN_CIPHER_SUITE_GCMP:
2294 	case WLAN_CIPHER_SUITE_GCMP_256:
2295 		arg.key_cipher = WMI_CIPHER_AES_GCM;
2296 		break;
2297 	default:
2298 		ath11k_warn(ar->ab, "cipher %d is not supported\n", key->cipher);
2299 		return -EOPNOTSUPP;
2300 	}
2301 
2302 install:
2303 	ret = ath11k_wmi_vdev_install_key(arvif->ar, &arg);
2304 	if (ret)
2305 		return ret;
2306 
2307 	if (!wait_for_completion_timeout(&ar->install_key_done, 1 * HZ))
2308 		return -ETIMEDOUT;
2309 
2310 	return ar->install_key_status ? -EINVAL : 0;
2311 }
2312 
2313 static int ath11k_clear_peer_keys(struct ath11k_vif *arvif,
2314 				  const u8 *addr)
2315 {
2316 	struct ath11k *ar = arvif->ar;
2317 	struct ath11k_base *ab = ar->ab;
2318 	struct ath11k_peer *peer;
2319 	int first_errno = 0;
2320 	int ret;
2321 	int i;
2322 	u32 flags = 0;
2323 
2324 	lockdep_assert_held(&ar->conf_mutex);
2325 
2326 	spin_lock_bh(&ab->base_lock);
2327 	peer = ath11k_peer_find(ab, arvif->vdev_id, addr);
2328 	spin_unlock_bh(&ab->base_lock);
2329 
2330 	if (!peer)
2331 		return -ENOENT;
2332 
2333 	for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
2334 		if (!peer->keys[i])
2335 			continue;
2336 
2337 		/* key flags are not required to delete the key */
2338 		ret = ath11k_install_key(arvif, peer->keys[i],
2339 					 DISABLE_KEY, addr, flags);
2340 		if (ret < 0 && first_errno == 0)
2341 			first_errno = ret;
2342 
2343 		if (ret < 0)
2344 			ath11k_warn(ab, "failed to remove peer key %d: %d\n",
2345 				    i, ret);
2346 
2347 		spin_lock_bh(&ab->base_lock);
2348 		peer->keys[i] = NULL;
2349 		spin_unlock_bh(&ab->base_lock);
2350 	}
2351 
2352 	return first_errno;
2353 }
2354 
2355 static int ath11k_mac_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
2356 				 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
2357 				 struct ieee80211_key_conf *key)
2358 {
2359 	struct ath11k *ar = hw->priv;
2360 	struct ath11k_base *ab = ar->ab;
2361 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
2362 	struct ath11k_peer *peer;
2363 	struct ath11k_sta *arsta;
2364 	const u8 *peer_addr;
2365 	int ret = 0;
2366 	u32 flags = 0;
2367 
2368 	/* BIP needs to be done in software */
2369 	if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
2370 	    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
2371 	    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256 ||
2372 	    key->cipher == WLAN_CIPHER_SUITE_BIP_CMAC_256)
2373 		return 1;
2374 
2375 	if (key->keyidx > WMI_MAX_KEY_INDEX)
2376 		return -ENOSPC;
2377 
2378 	mutex_lock(&ar->conf_mutex);
2379 
2380 	if (sta)
2381 		peer_addr = sta->addr;
2382 	else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
2383 		peer_addr = vif->bss_conf.bssid;
2384 	else
2385 		peer_addr = vif->addr;
2386 
2387 	key->hw_key_idx = key->keyidx;
2388 
2389 	/* the peer should not disappear in mid-way (unless FW goes awry) since
2390 	 * we already hold conf_mutex. we just make sure its there now.
2391 	 */
2392 	spin_lock_bh(&ab->base_lock);
2393 	peer = ath11k_peer_find(ab, arvif->vdev_id, peer_addr);
2394 	spin_unlock_bh(&ab->base_lock);
2395 
2396 	if (!peer) {
2397 		if (cmd == SET_KEY) {
2398 			ath11k_warn(ab, "cannot install key for non-existent peer %pM\n",
2399 				    peer_addr);
2400 			ret = -EOPNOTSUPP;
2401 			goto exit;
2402 		} else {
2403 			/* if the peer doesn't exist there is no key to disable
2404 			 * anymore
2405 			 */
2406 			goto exit;
2407 		}
2408 	}
2409 
2410 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
2411 		flags |= WMI_KEY_PAIRWISE;
2412 	else
2413 		flags |= WMI_KEY_GROUP;
2414 
2415 	ret = ath11k_install_key(arvif, key, cmd, peer_addr, flags);
2416 	if (ret) {
2417 		ath11k_warn(ab, "ath11k_install_key failed (%d)\n", ret);
2418 		goto exit;
2419 	}
2420 
2421 	ret = ath11k_dp_peer_rx_pn_replay_config(arvif, peer_addr, cmd, key);
2422 	if (ret) {
2423 		ath11k_warn(ab, "failed to offload PN replay detection %d\n", ret);
2424 		goto exit;
2425 	}
2426 
2427 	spin_lock_bh(&ab->base_lock);
2428 	peer = ath11k_peer_find(ab, arvif->vdev_id, peer_addr);
2429 	if (peer && cmd == SET_KEY) {
2430 		peer->keys[key->keyidx] = key;
2431 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
2432 			peer->ucast_keyidx = key->keyidx;
2433 			peer->sec_type = ath11k_dp_tx_get_encrypt_type(key->cipher);
2434 		} else {
2435 			peer->mcast_keyidx = key->keyidx;
2436 			peer->sec_type_grp = ath11k_dp_tx_get_encrypt_type(key->cipher);
2437 		}
2438 	} else if (peer && cmd == DISABLE_KEY) {
2439 		peer->keys[key->keyidx] = NULL;
2440 		if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
2441 			peer->ucast_keyidx = 0;
2442 		else
2443 			peer->mcast_keyidx = 0;
2444 	} else if (!peer)
2445 		/* impossible unless FW goes crazy */
2446 		ath11k_warn(ab, "peer %pM disappeared!\n", peer_addr);
2447 
2448 	if (sta) {
2449 		arsta = (struct ath11k_sta *)sta->drv_priv;
2450 
2451 		switch (key->cipher) {
2452 		case WLAN_CIPHER_SUITE_TKIP:
2453 		case WLAN_CIPHER_SUITE_CCMP:
2454 		case WLAN_CIPHER_SUITE_CCMP_256:
2455 		case WLAN_CIPHER_SUITE_GCMP:
2456 		case WLAN_CIPHER_SUITE_GCMP_256:
2457 			if (cmd == SET_KEY)
2458 				arsta->pn_type = HAL_PN_TYPE_WPA;
2459 			else
2460 				arsta->pn_type = HAL_PN_TYPE_NONE;
2461 			break;
2462 		default:
2463 			arsta->pn_type = HAL_PN_TYPE_NONE;
2464 			break;
2465 		}
2466 	}
2467 
2468 	spin_unlock_bh(&ab->base_lock);
2469 
2470 exit:
2471 	mutex_unlock(&ar->conf_mutex);
2472 	return ret;
2473 }
2474 
2475 static int
2476 ath11k_mac_bitrate_mask_num_vht_rates(struct ath11k *ar,
2477 				      enum nl80211_band band,
2478 				      const struct cfg80211_bitrate_mask *mask)
2479 {
2480 	int num_rates = 0;
2481 	int i;
2482 
2483 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
2484 		num_rates += hweight16(mask->control[band].vht_mcs[i]);
2485 
2486 	return num_rates;
2487 }
2488 
2489 static int
2490 ath11k_mac_set_peer_vht_fixed_rate(struct ath11k_vif *arvif,
2491 				   struct ieee80211_sta *sta,
2492 				   const struct cfg80211_bitrate_mask *mask,
2493 				   enum nl80211_band band)
2494 {
2495 	struct ath11k *ar = arvif->ar;
2496 	u8 vht_rate, nss;
2497 	u32 rate_code;
2498 	int ret, i;
2499 
2500 	lockdep_assert_held(&ar->conf_mutex);
2501 
2502 	nss = 0;
2503 
2504 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
2505 		if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
2506 			nss = i + 1;
2507 			vht_rate = ffs(mask->control[band].vht_mcs[i]) - 1;
2508 		}
2509 	}
2510 
2511 	if (!nss) {
2512 		ath11k_warn(ar->ab, "No single VHT Fixed rate found to set for %pM",
2513 			    sta->addr);
2514 		return -EINVAL;
2515 	}
2516 
2517 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2518 		   "Setting Fixed VHT Rate for peer %pM. Device will not switch to any other selected rates",
2519 		   sta->addr);
2520 
2521 	rate_code = ATH11K_HW_RATE_CODE(vht_rate, nss - 1,
2522 					WMI_RATE_PREAMBLE_VHT);
2523 	ret = ath11k_wmi_set_peer_param(ar, sta->addr,
2524 					arvif->vdev_id,
2525 					WMI_PEER_PARAM_FIXED_RATE,
2526 					rate_code);
2527 	if (ret)
2528 		ath11k_warn(ar->ab,
2529 			    "failed to update STA %pM Fixed Rate %d: %d\n",
2530 			     sta->addr, rate_code, ret);
2531 
2532 	return ret;
2533 }
2534 
2535 static int ath11k_station_assoc(struct ath11k *ar,
2536 				struct ieee80211_vif *vif,
2537 				struct ieee80211_sta *sta,
2538 				bool reassoc)
2539 {
2540 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
2541 	struct peer_assoc_params peer_arg;
2542 	int ret = 0;
2543 	struct cfg80211_chan_def def;
2544 	enum nl80211_band band;
2545 	struct cfg80211_bitrate_mask *mask;
2546 	u8 num_vht_rates;
2547 
2548 	lockdep_assert_held(&ar->conf_mutex);
2549 
2550 	if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
2551 		return -EPERM;
2552 
2553 	band = def.chan->band;
2554 	mask = &arvif->bitrate_mask;
2555 
2556 	ath11k_peer_assoc_prepare(ar, vif, sta, &peer_arg, reassoc);
2557 
2558 	ret = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
2559 	if (ret) {
2560 		ath11k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
2561 			    sta->addr, arvif->vdev_id, ret);
2562 		return ret;
2563 	}
2564 
2565 	if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
2566 		ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
2567 			    sta->addr, arvif->vdev_id);
2568 		return -ETIMEDOUT;
2569 	}
2570 
2571 	num_vht_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band, mask);
2572 
2573 	/* If single VHT rate is configured (by set_bitrate_mask()),
2574 	 * peer_assoc will disable VHT. This is now enabled by a peer specific
2575 	 * fixed param.
2576 	 * Note that all other rates and NSS will be disabled for this peer.
2577 	 */
2578 	if (sta->vht_cap.vht_supported && num_vht_rates == 1) {
2579 		ret = ath11k_mac_set_peer_vht_fixed_rate(arvif, sta, mask,
2580 							 band);
2581 		if (ret)
2582 			return ret;
2583 	}
2584 
2585 	/* Re-assoc is run only to update supported rates for given station. It
2586 	 * doesn't make much sense to reconfigure the peer completely.
2587 	 */
2588 	if (reassoc)
2589 		return 0;
2590 
2591 	ret = ath11k_setup_peer_smps(ar, arvif, sta->addr,
2592 				     &sta->ht_cap);
2593 	if (ret) {
2594 		ath11k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
2595 			    arvif->vdev_id, ret);
2596 		return ret;
2597 	}
2598 
2599 	if (!sta->wme) {
2600 		arvif->num_legacy_stations++;
2601 		ret = ath11k_recalc_rtscts_prot(arvif);
2602 		if (ret)
2603 			return ret;
2604 	}
2605 
2606 	if (sta->wme && sta->uapsd_queues) {
2607 		ret = ath11k_peer_assoc_qos_ap(ar, arvif, sta);
2608 		if (ret) {
2609 			ath11k_warn(ar->ab, "failed to set qos params for STA %pM for vdev %i: %d\n",
2610 				    sta->addr, arvif->vdev_id, ret);
2611 			return ret;
2612 		}
2613 	}
2614 
2615 	return 0;
2616 }
2617 
2618 static int ath11k_station_disassoc(struct ath11k *ar,
2619 				   struct ieee80211_vif *vif,
2620 				   struct ieee80211_sta *sta)
2621 {
2622 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
2623 	int ret = 0;
2624 
2625 	lockdep_assert_held(&ar->conf_mutex);
2626 
2627 	if (!sta->wme) {
2628 		arvif->num_legacy_stations--;
2629 		ret = ath11k_recalc_rtscts_prot(arvif);
2630 		if (ret)
2631 			return ret;
2632 	}
2633 
2634 	ret = ath11k_clear_peer_keys(arvif, sta->addr);
2635 	if (ret) {
2636 		ath11k_warn(ar->ab, "failed to clear all peer keys for vdev %i: %d\n",
2637 			    arvif->vdev_id, ret);
2638 		return ret;
2639 	}
2640 	return 0;
2641 }
2642 
2643 static void ath11k_sta_rc_update_wk(struct work_struct *wk)
2644 {
2645 	struct ath11k *ar;
2646 	struct ath11k_vif *arvif;
2647 	struct ath11k_sta *arsta;
2648 	struct ieee80211_sta *sta;
2649 	struct cfg80211_chan_def def;
2650 	enum nl80211_band band;
2651 	const u8 *ht_mcs_mask;
2652 	const u16 *vht_mcs_mask;
2653 	u32 changed, bw, nss, smps;
2654 	int err, num_vht_rates;
2655 	const struct cfg80211_bitrate_mask *mask;
2656 	struct peer_assoc_params peer_arg;
2657 
2658 	arsta = container_of(wk, struct ath11k_sta, update_wk);
2659 	sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
2660 	arvif = arsta->arvif;
2661 	ar = arvif->ar;
2662 
2663 	if (WARN_ON(ath11k_mac_vif_chan(arvif->vif, &def)))
2664 		return;
2665 
2666 	band = def.chan->band;
2667 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2668 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2669 
2670 	spin_lock_bh(&ar->data_lock);
2671 
2672 	changed = arsta->changed;
2673 	arsta->changed = 0;
2674 
2675 	bw = arsta->bw;
2676 	nss = arsta->nss;
2677 	smps = arsta->smps;
2678 
2679 	spin_unlock_bh(&ar->data_lock);
2680 
2681 	mutex_lock(&ar->conf_mutex);
2682 
2683 	nss = max_t(u32, 1, nss);
2684 	nss = min(nss, max(ath11k_mac_max_ht_nss(ht_mcs_mask),
2685 			   ath11k_mac_max_vht_nss(vht_mcs_mask)));
2686 
2687 	if (changed & IEEE80211_RC_BW_CHANGED) {
2688 		err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
2689 						WMI_PEER_CHWIDTH, bw);
2690 		if (err)
2691 			ath11k_warn(ar->ab, "failed to update STA %pM peer bw %d: %d\n",
2692 				    sta->addr, bw, err);
2693 	}
2694 
2695 	if (changed & IEEE80211_RC_NSS_CHANGED) {
2696 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac update sta %pM nss %d\n",
2697 			   sta->addr, nss);
2698 
2699 		err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
2700 						WMI_PEER_NSS, nss);
2701 		if (err)
2702 			ath11k_warn(ar->ab, "failed to update STA %pM nss %d: %d\n",
2703 				    sta->addr, nss, err);
2704 	}
2705 
2706 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
2707 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac update sta %pM smps %d\n",
2708 			   sta->addr, smps);
2709 
2710 		err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
2711 						WMI_PEER_MIMO_PS_STATE, smps);
2712 		if (err)
2713 			ath11k_warn(ar->ab, "failed to update STA %pM smps %d: %d\n",
2714 				    sta->addr, smps, err);
2715 	}
2716 
2717 	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
2718 		mask = &arvif->bitrate_mask;
2719 		num_vht_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band,
2720 								      mask);
2721 
2722 		/* Peer_assoc_prepare will reject vht rates in
2723 		 * bitrate_mask if its not available in range format and
2724 		 * sets vht tx_rateset as unsupported. So multiple VHT MCS
2725 		 * setting(eg. MCS 4,5,6) per peer is not supported here.
2726 		 * But, Single rate in VHT mask can be set as per-peer
2727 		 * fixed rate. But even if any HT rates are configured in
2728 		 * the bitrate mask, device will not switch to those rates
2729 		 * when per-peer Fixed rate is set.
2730 		 * TODO: Check RATEMASK_CMDID to support auto rates selection
2731 		 * across HT/VHT and for multiple VHT MCS support.
2732 		 */
2733 		if (sta->vht_cap.vht_supported && num_vht_rates == 1) {
2734 			ath11k_mac_set_peer_vht_fixed_rate(arvif, sta, mask,
2735 							   band);
2736 		} else {
2737 			/* If the peer is non-VHT or no fixed VHT rate
2738 			 * is provided in the new bitrate mask we set the
2739 			 * other rates using peer_assoc command.
2740 			 */
2741 			ath11k_peer_assoc_prepare(ar, arvif->vif, sta,
2742 						  &peer_arg, true);
2743 
2744 			err = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
2745 			if (err)
2746 				ath11k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
2747 					    sta->addr, arvif->vdev_id, err);
2748 
2749 			if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ))
2750 				ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
2751 					    sta->addr, arvif->vdev_id);
2752 		}
2753 	}
2754 
2755 	mutex_unlock(&ar->conf_mutex);
2756 }
2757 
2758 static int ath11k_mac_inc_num_stations(struct ath11k_vif *arvif,
2759 				       struct ieee80211_sta *sta)
2760 {
2761 	struct ath11k *ar = arvif->ar;
2762 
2763 	lockdep_assert_held(&ar->conf_mutex);
2764 
2765 	if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
2766 		return 0;
2767 
2768 	if (ar->num_stations >= ar->max_num_stations)
2769 		return -ENOBUFS;
2770 
2771 	ar->num_stations++;
2772 
2773 	return 0;
2774 }
2775 
2776 static void ath11k_mac_dec_num_stations(struct ath11k_vif *arvif,
2777 					struct ieee80211_sta *sta)
2778 {
2779 	struct ath11k *ar = arvif->ar;
2780 
2781 	lockdep_assert_held(&ar->conf_mutex);
2782 
2783 	if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
2784 		return;
2785 
2786 	ar->num_stations--;
2787 }
2788 
2789 static int ath11k_mac_station_add(struct ath11k *ar,
2790 				  struct ieee80211_vif *vif,
2791 				  struct ieee80211_sta *sta)
2792 {
2793 	struct ath11k_base *ab = ar->ab;
2794 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
2795 	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
2796 	struct peer_create_params peer_param;
2797 	int ret;
2798 
2799 	lockdep_assert_held(&ar->conf_mutex);
2800 
2801 	ret = ath11k_mac_inc_num_stations(arvif, sta);
2802 	if (ret) {
2803 		ath11k_warn(ab, "refusing to associate station: too many connected already (%d)\n",
2804 			    ar->max_num_stations);
2805 		goto exit;
2806 	}
2807 
2808 	arsta->rx_stats = kzalloc(sizeof(*arsta->rx_stats), GFP_KERNEL);
2809 	if (!arsta->rx_stats) {
2810 		ret = -ENOMEM;
2811 		goto dec_num_station;
2812 	}
2813 
2814 	peer_param.vdev_id = arvif->vdev_id;
2815 	peer_param.peer_addr = sta->addr;
2816 	peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
2817 
2818 	ret = ath11k_peer_create(ar, arvif, sta, &peer_param);
2819 	if (ret) {
2820 		ath11k_warn(ab, "Failed to add peer: %pM for VDEV: %d\n",
2821 			    sta->addr, arvif->vdev_id);
2822 		goto free_rx_stats;
2823 	}
2824 
2825 	ath11k_dbg(ab, ATH11K_DBG_MAC, "Added peer: %pM for VDEV: %d\n",
2826 		   sta->addr, arvif->vdev_id);
2827 
2828 	if (ath11k_debug_is_extd_tx_stats_enabled(ar)) {
2829 		arsta->tx_stats = kzalloc(sizeof(*arsta->tx_stats), GFP_KERNEL);
2830 		if (!arsta->tx_stats) {
2831 			ret = -ENOMEM;
2832 			goto free_peer;
2833 		}
2834 	}
2835 
2836 	if (ieee80211_vif_is_mesh(vif)) {
2837 		ret = ath11k_wmi_set_peer_param(ar, sta->addr,
2838 						arvif->vdev_id,
2839 						WMI_PEER_USE_4ADDR, 1);
2840 		if (ret) {
2841 			ath11k_warn(ab, "failed to STA %pM 4addr capability: %d\n",
2842 				    sta->addr, ret);
2843 			goto free_tx_stats;
2844 		}
2845 	}
2846 
2847 	ret = ath11k_dp_peer_setup(ar, arvif->vdev_id, sta->addr);
2848 	if (ret) {
2849 		ath11k_warn(ab, "failed to setup dp for peer %pM on vdev %i (%d)\n",
2850 			    sta->addr, arvif->vdev_id, ret);
2851 		goto free_tx_stats;
2852 	}
2853 
2854 	return 0;
2855 
2856 free_tx_stats:
2857 	kfree(arsta->tx_stats);
2858 	arsta->tx_stats = NULL;
2859 free_peer:
2860 	ath11k_peer_delete(ar, arvif->vdev_id, sta->addr);
2861 free_rx_stats:
2862 	kfree(arsta->rx_stats);
2863 	arsta->rx_stats = NULL;
2864 dec_num_station:
2865 	ath11k_mac_dec_num_stations(arvif, sta);
2866 exit:
2867 	return ret;
2868 }
2869 
2870 static int ath11k_mac_op_sta_state(struct ieee80211_hw *hw,
2871 				   struct ieee80211_vif *vif,
2872 				   struct ieee80211_sta *sta,
2873 				   enum ieee80211_sta_state old_state,
2874 				   enum ieee80211_sta_state new_state)
2875 {
2876 	struct ath11k *ar = hw->priv;
2877 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
2878 	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
2879 	struct ath11k_peer *peer;
2880 	int ret = 0;
2881 
2882 	/* cancel must be done outside the mutex to avoid deadlock */
2883 	if ((old_state == IEEE80211_STA_NONE &&
2884 	     new_state == IEEE80211_STA_NOTEXIST))
2885 		cancel_work_sync(&arsta->update_wk);
2886 
2887 	mutex_lock(&ar->conf_mutex);
2888 
2889 	if (old_state == IEEE80211_STA_NOTEXIST &&
2890 	    new_state == IEEE80211_STA_NONE) {
2891 		memset(arsta, 0, sizeof(*arsta));
2892 		arsta->arvif = arvif;
2893 		INIT_WORK(&arsta->update_wk, ath11k_sta_rc_update_wk);
2894 
2895 		ret = ath11k_mac_station_add(ar, vif, sta);
2896 		if (ret)
2897 			ath11k_warn(ar->ab, "Failed to add station: %pM for VDEV: %d\n",
2898 				    sta->addr, arvif->vdev_id);
2899 	} else if ((old_state == IEEE80211_STA_NONE &&
2900 		    new_state == IEEE80211_STA_NOTEXIST)) {
2901 		ath11k_dp_peer_cleanup(ar, arvif->vdev_id, sta->addr);
2902 
2903 		ret = ath11k_peer_delete(ar, arvif->vdev_id, sta->addr);
2904 		if (ret)
2905 			ath11k_warn(ar->ab, "Failed to delete peer: %pM for VDEV: %d\n",
2906 				    sta->addr, arvif->vdev_id);
2907 		else
2908 			ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "Removed peer: %pM for VDEV: %d\n",
2909 				   sta->addr, arvif->vdev_id);
2910 
2911 		ath11k_mac_dec_num_stations(arvif, sta);
2912 		spin_lock_bh(&ar->ab->base_lock);
2913 		peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
2914 		if (peer && peer->sta == sta) {
2915 			ath11k_warn(ar->ab, "Found peer entry %pM n vdev %i after it was supposedly removed\n",
2916 				    vif->addr, arvif->vdev_id);
2917 			peer->sta = NULL;
2918 			list_del(&peer->list);
2919 			kfree(peer);
2920 			ar->num_peers--;
2921 		}
2922 		spin_unlock_bh(&ar->ab->base_lock);
2923 
2924 		kfree(arsta->tx_stats);
2925 		arsta->tx_stats = NULL;
2926 
2927 		kfree(arsta->rx_stats);
2928 		arsta->rx_stats = NULL;
2929 	} else if (old_state == IEEE80211_STA_AUTH &&
2930 		   new_state == IEEE80211_STA_ASSOC &&
2931 		   (vif->type == NL80211_IFTYPE_AP ||
2932 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
2933 		    vif->type == NL80211_IFTYPE_ADHOC)) {
2934 		ret = ath11k_station_assoc(ar, vif, sta, false);
2935 		if (ret)
2936 			ath11k_warn(ar->ab, "Failed to associate station: %pM\n",
2937 				    sta->addr);
2938 		else
2939 			ath11k_info(ar->ab,
2940 				    "Station %pM moved to assoc state\n",
2941 				    sta->addr);
2942 	} else if (old_state == IEEE80211_STA_ASSOC &&
2943 		   new_state == IEEE80211_STA_AUTH &&
2944 		   (vif->type == NL80211_IFTYPE_AP ||
2945 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
2946 		    vif->type == NL80211_IFTYPE_ADHOC)) {
2947 		ret = ath11k_station_disassoc(ar, vif, sta);
2948 		if (ret)
2949 			ath11k_warn(ar->ab, "Failed to disassociate station: %pM\n",
2950 				    sta->addr);
2951 		else
2952 			ath11k_info(ar->ab,
2953 				    "Station %pM moved to disassociated state\n",
2954 				    sta->addr);
2955 	}
2956 
2957 	mutex_unlock(&ar->conf_mutex);
2958 	return ret;
2959 }
2960 
2961 static int ath11k_mac_op_sta_set_txpwr(struct ieee80211_hw *hw,
2962 				       struct ieee80211_vif *vif,
2963 				       struct ieee80211_sta *sta)
2964 {
2965 	struct ath11k *ar = hw->priv;
2966 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
2967 	int ret = 0;
2968 	s16 txpwr;
2969 
2970 	if (sta->txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
2971 		txpwr = 0;
2972 	} else {
2973 		txpwr = sta->txpwr.power;
2974 		if (!txpwr)
2975 			return -EINVAL;
2976 	}
2977 
2978 	if (txpwr > ATH11K_TX_POWER_MAX_VAL || txpwr < ATH11K_TX_POWER_MIN_VAL)
2979 		return -EINVAL;
2980 
2981 	mutex_lock(&ar->conf_mutex);
2982 
2983 	ret = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
2984 					WMI_PEER_USE_FIXED_PWR, txpwr);
2985 	if (ret) {
2986 		ath11k_warn(ar->ab, "failed to set tx power for station ret: %d\n",
2987 			    ret);
2988 		goto out;
2989 	}
2990 
2991 out:
2992 	mutex_unlock(&ar->conf_mutex);
2993 	return ret;
2994 }
2995 
2996 static void ath11k_mac_op_sta_rc_update(struct ieee80211_hw *hw,
2997 					struct ieee80211_vif *vif,
2998 					struct ieee80211_sta *sta,
2999 					u32 changed)
3000 {
3001 	struct ath11k *ar = hw->priv;
3002 	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
3003 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
3004 	struct ath11k_peer *peer;
3005 	u32 bw, smps;
3006 
3007 	spin_lock_bh(&ar->ab->base_lock);
3008 
3009 	peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
3010 	if (!peer) {
3011 		spin_unlock_bh(&ar->ab->base_lock);
3012 		ath11k_warn(ar->ab, "mac sta rc update failed to find peer %pM on vdev %i\n",
3013 			    sta->addr, arvif->vdev_id);
3014 		return;
3015 	}
3016 
3017 	spin_unlock_bh(&ar->ab->base_lock);
3018 
3019 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3020 		   "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
3021 		   sta->addr, changed, sta->bandwidth, sta->rx_nss,
3022 		   sta->smps_mode);
3023 
3024 	spin_lock_bh(&ar->data_lock);
3025 
3026 	if (changed & IEEE80211_RC_BW_CHANGED) {
3027 		bw = WMI_PEER_CHWIDTH_20MHZ;
3028 
3029 		switch (sta->bandwidth) {
3030 		case IEEE80211_STA_RX_BW_20:
3031 			bw = WMI_PEER_CHWIDTH_20MHZ;
3032 			break;
3033 		case IEEE80211_STA_RX_BW_40:
3034 			bw = WMI_PEER_CHWIDTH_40MHZ;
3035 			break;
3036 		case IEEE80211_STA_RX_BW_80:
3037 			bw = WMI_PEER_CHWIDTH_80MHZ;
3038 			break;
3039 		case IEEE80211_STA_RX_BW_160:
3040 			bw = WMI_PEER_CHWIDTH_160MHZ;
3041 			break;
3042 		default:
3043 			ath11k_warn(ar->ab, "Invalid bandwidth %d in rc update for %pM\n",
3044 				    sta->bandwidth, sta->addr);
3045 			bw = WMI_PEER_CHWIDTH_20MHZ;
3046 			break;
3047 		}
3048 
3049 		arsta->bw = bw;
3050 	}
3051 
3052 	if (changed & IEEE80211_RC_NSS_CHANGED)
3053 		arsta->nss = sta->rx_nss;
3054 
3055 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
3056 		smps = WMI_PEER_SMPS_PS_NONE;
3057 
3058 		switch (sta->smps_mode) {
3059 		case IEEE80211_SMPS_AUTOMATIC:
3060 		case IEEE80211_SMPS_OFF:
3061 			smps = WMI_PEER_SMPS_PS_NONE;
3062 			break;
3063 		case IEEE80211_SMPS_STATIC:
3064 			smps = WMI_PEER_SMPS_STATIC;
3065 			break;
3066 		case IEEE80211_SMPS_DYNAMIC:
3067 			smps = WMI_PEER_SMPS_DYNAMIC;
3068 			break;
3069 		default:
3070 			ath11k_warn(ar->ab, "Invalid smps %d in sta rc update for %pM\n",
3071 				    sta->smps_mode, sta->addr);
3072 			smps = WMI_PEER_SMPS_PS_NONE;
3073 			break;
3074 		}
3075 
3076 		arsta->smps = smps;
3077 	}
3078 
3079 	arsta->changed |= changed;
3080 
3081 	spin_unlock_bh(&ar->data_lock);
3082 
3083 	ieee80211_queue_work(hw, &arsta->update_wk);
3084 }
3085 
3086 static int ath11k_conf_tx_uapsd(struct ath11k *ar, struct ieee80211_vif *vif,
3087 				u16 ac, bool enable)
3088 {
3089 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
3090 	u32 value = 0;
3091 	int ret = 0;
3092 
3093 	if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
3094 		return 0;
3095 
3096 	switch (ac) {
3097 	case IEEE80211_AC_VO:
3098 		value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
3099 			WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
3100 		break;
3101 	case IEEE80211_AC_VI:
3102 		value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
3103 			WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
3104 		break;
3105 	case IEEE80211_AC_BE:
3106 		value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
3107 			WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
3108 		break;
3109 	case IEEE80211_AC_BK:
3110 		value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
3111 			WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
3112 		break;
3113 	}
3114 
3115 	if (enable)
3116 		arvif->u.sta.uapsd |= value;
3117 	else
3118 		arvif->u.sta.uapsd &= ~value;
3119 
3120 	ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3121 					  WMI_STA_PS_PARAM_UAPSD,
3122 					  arvif->u.sta.uapsd);
3123 	if (ret) {
3124 		ath11k_warn(ar->ab, "could not set uapsd params %d\n", ret);
3125 		goto exit;
3126 	}
3127 
3128 	if (arvif->u.sta.uapsd)
3129 		value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
3130 	else
3131 		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
3132 
3133 	ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3134 					  WMI_STA_PS_PARAM_RX_WAKE_POLICY,
3135 					  value);
3136 	if (ret)
3137 		ath11k_warn(ar->ab, "could not set rx wake param %d\n", ret);
3138 
3139 exit:
3140 	return ret;
3141 }
3142 
3143 static int ath11k_mac_op_conf_tx(struct ieee80211_hw *hw,
3144 				 struct ieee80211_vif *vif, u16 ac,
3145 				 const struct ieee80211_tx_queue_params *params)
3146 {
3147 	struct ath11k *ar = hw->priv;
3148 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
3149 	struct wmi_wmm_params_arg *p = NULL;
3150 	int ret;
3151 
3152 	mutex_lock(&ar->conf_mutex);
3153 
3154 	switch (ac) {
3155 	case IEEE80211_AC_VO:
3156 		p = &arvif->wmm_params.ac_vo;
3157 		break;
3158 	case IEEE80211_AC_VI:
3159 		p = &arvif->wmm_params.ac_vi;
3160 		break;
3161 	case IEEE80211_AC_BE:
3162 		p = &arvif->wmm_params.ac_be;
3163 		break;
3164 	case IEEE80211_AC_BK:
3165 		p = &arvif->wmm_params.ac_bk;
3166 		break;
3167 	}
3168 
3169 	if (WARN_ON(!p)) {
3170 		ret = -EINVAL;
3171 		goto exit;
3172 	}
3173 
3174 	p->cwmin = params->cw_min;
3175 	p->cwmax = params->cw_max;
3176 	p->aifs = params->aifs;
3177 	p->txop = params->txop;
3178 
3179 	ret = ath11k_wmi_send_wmm_update_cmd_tlv(ar, arvif->vdev_id,
3180 						 &arvif->wmm_params);
3181 	if (ret) {
3182 		ath11k_warn(ar->ab, "failed to set wmm params: %d\n", ret);
3183 		goto exit;
3184 	}
3185 
3186 	ret = ath11k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
3187 
3188 	if (ret)
3189 		ath11k_warn(ar->ab, "failed to set sta uapsd: %d\n", ret);
3190 
3191 exit:
3192 	mutex_unlock(&ar->conf_mutex);
3193 	return ret;
3194 }
3195 
3196 static struct ieee80211_sta_ht_cap
3197 ath11k_create_ht_cap(struct ath11k *ar, u32 ar_ht_cap, u32 rate_cap_rx_chainmask)
3198 {
3199 	int i;
3200 	struct ieee80211_sta_ht_cap ht_cap = {0};
3201 	u32 ar_vht_cap = ar->pdev->cap.vht_cap;
3202 
3203 	if (!(ar_ht_cap & WMI_HT_CAP_ENABLED))
3204 		return ht_cap;
3205 
3206 	ht_cap.ht_supported = 1;
3207 	ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
3208 	ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
3209 	ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
3210 	ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
3211 	ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
3212 
3213 	if (ar_ht_cap & WMI_HT_CAP_HT20_SGI)
3214 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
3215 
3216 	if (ar_ht_cap & WMI_HT_CAP_HT40_SGI)
3217 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
3218 
3219 	if (ar_ht_cap & WMI_HT_CAP_DYNAMIC_SMPS) {
3220 		u32 smps;
3221 
3222 		smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
3223 		smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
3224 
3225 		ht_cap.cap |= smps;
3226 	}
3227 
3228 	if (ar_ht_cap & WMI_HT_CAP_TX_STBC)
3229 		ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
3230 
3231 	if (ar_ht_cap & WMI_HT_CAP_RX_STBC) {
3232 		u32 stbc;
3233 
3234 		stbc   = ar_ht_cap;
3235 		stbc  &= WMI_HT_CAP_RX_STBC;
3236 		stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
3237 		stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
3238 		stbc  &= IEEE80211_HT_CAP_RX_STBC;
3239 
3240 		ht_cap.cap |= stbc;
3241 	}
3242 
3243 	if (ar_ht_cap & WMI_HT_CAP_RX_LDPC)
3244 		ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
3245 
3246 	if (ar_ht_cap & WMI_HT_CAP_L_SIG_TXOP_PROT)
3247 		ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
3248 
3249 	if (ar_vht_cap & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
3250 		ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
3251 
3252 	for (i = 0; i < ar->num_rx_chains; i++) {
3253 		if (rate_cap_rx_chainmask & BIT(i))
3254 			ht_cap.mcs.rx_mask[i] = 0xFF;
3255 	}
3256 
3257 	ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
3258 
3259 	return ht_cap;
3260 }
3261 
3262 static int ath11k_mac_set_txbf_conf(struct ath11k_vif *arvif)
3263 {
3264 	u32 value = 0;
3265 	struct ath11k *ar = arvif->ar;
3266 	int nsts;
3267 	int sound_dim;
3268 	u32 vht_cap = ar->pdev->cap.vht_cap;
3269 	u32 vdev_param = WMI_VDEV_PARAM_TXBF;
3270 
3271 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)) {
3272 		nsts = vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
3273 		nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
3274 		value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
3275 	}
3276 
3277 	if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)) {
3278 		sound_dim = vht_cap &
3279 			    IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
3280 		sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
3281 		if (sound_dim > (ar->num_tx_chains - 1))
3282 			sound_dim = ar->num_tx_chains - 1;
3283 		value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
3284 	}
3285 
3286 	if (!value)
3287 		return 0;
3288 
3289 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE) {
3290 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
3291 
3292 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) &&
3293 		    arvif->vdev_type == WMI_VDEV_TYPE_AP)
3294 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
3295 	}
3296 
3297 	/* TODO: SUBFEE not validated in HK, disable here until validated? */
3298 
3299 	if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE) {
3300 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
3301 
3302 		if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) &&
3303 		    arvif->vdev_type == WMI_VDEV_TYPE_STA)
3304 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
3305 	}
3306 
3307 	return ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3308 					     vdev_param, value);
3309 }
3310 
3311 static void ath11k_set_vht_txbf_cap(struct ath11k *ar, u32 *vht_cap)
3312 {
3313 	bool subfer, subfee;
3314 	int sound_dim = 0;
3315 
3316 	subfer = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE));
3317 	subfee = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE));
3318 
3319 	if (ar->num_tx_chains < 2) {
3320 		*vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
3321 		subfer = false;
3322 	}
3323 
3324 	/* If SU Beaformer is not set, then disable MU Beamformer Capability */
3325 	if (!subfer)
3326 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
3327 
3328 	/* If SU Beaformee is not set, then disable MU Beamformee Capability */
3329 	if (!subfee)
3330 		*vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
3331 
3332 	sound_dim = (*vht_cap & IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
3333 	sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
3334 	*vht_cap &= ~IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
3335 
3336 	/* TODO: Need to check invalid STS and Sound_dim values set by FW? */
3337 
3338 	/* Enable Sounding Dimension Field only if SU BF is enabled */
3339 	if (subfer) {
3340 		if (sound_dim > (ar->num_tx_chains - 1))
3341 			sound_dim = ar->num_tx_chains - 1;
3342 
3343 		sound_dim <<= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
3344 		sound_dim &=  IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
3345 		*vht_cap |= sound_dim;
3346 	}
3347 
3348 	/* Use the STS advertised by FW unless SU Beamformee is not supported*/
3349 	if (!subfee)
3350 		*vht_cap &= ~(IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
3351 }
3352 
3353 static struct ieee80211_sta_vht_cap
3354 ath11k_create_vht_cap(struct ath11k *ar, u32 rate_cap_tx_chainmask,
3355 		      u32 rate_cap_rx_chainmask)
3356 {
3357 	struct ieee80211_sta_vht_cap vht_cap = {0};
3358 	u16 txmcs_map, rxmcs_map;
3359 	int i;
3360 
3361 	vht_cap.vht_supported = 1;
3362 	vht_cap.cap = ar->pdev->cap.vht_cap;
3363 
3364 	ath11k_set_vht_txbf_cap(ar, &vht_cap.cap);
3365 
3366 	/* TODO: Enable back VHT160 mode once association issues are fixed */
3367 	/* Disabling VHT160 and VHT80+80 modes */
3368 	vht_cap.cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
3369 	vht_cap.cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
3370 
3371 	rxmcs_map = 0;
3372 	txmcs_map = 0;
3373 	for (i = 0; i < 8; i++) {
3374 		if (i < ar->num_tx_chains && rate_cap_tx_chainmask & BIT(i))
3375 			txmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
3376 		else
3377 			txmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
3378 
3379 		if (i < ar->num_rx_chains && rate_cap_rx_chainmask & BIT(i))
3380 			rxmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
3381 		else
3382 			rxmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
3383 	}
3384 
3385 	if (rate_cap_tx_chainmask <= 1)
3386 		vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
3387 
3388 	vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_map);
3389 	vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_map);
3390 
3391 	return vht_cap;
3392 }
3393 
3394 static void ath11k_mac_setup_ht_vht_cap(struct ath11k *ar,
3395 					struct ath11k_pdev_cap *cap,
3396 					u32 *ht_cap_info)
3397 {
3398 	struct ieee80211_supported_band *band;
3399 	u32 rate_cap_tx_chainmask;
3400 	u32 rate_cap_rx_chainmask;
3401 	u32 ht_cap;
3402 
3403 	rate_cap_tx_chainmask = ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift;
3404 	rate_cap_rx_chainmask = ar->cfg_rx_chainmask >> cap->rx_chain_mask_shift;
3405 
3406 	if (cap->supported_bands & WMI_HOST_WLAN_2G_CAP) {
3407 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
3408 		ht_cap = cap->band[NL80211_BAND_2GHZ].ht_cap_info;
3409 		if (ht_cap_info)
3410 			*ht_cap_info = ht_cap;
3411 		band->ht_cap = ath11k_create_ht_cap(ar, ht_cap,
3412 						    rate_cap_rx_chainmask);
3413 	}
3414 
3415 	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP) {
3416 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
3417 		ht_cap = cap->band[NL80211_BAND_5GHZ].ht_cap_info;
3418 		if (ht_cap_info)
3419 			*ht_cap_info = ht_cap;
3420 		band->ht_cap = ath11k_create_ht_cap(ar, ht_cap,
3421 						    rate_cap_rx_chainmask);
3422 		band->vht_cap = ath11k_create_vht_cap(ar, rate_cap_tx_chainmask,
3423 						      rate_cap_rx_chainmask);
3424 	}
3425 }
3426 
3427 static int ath11k_check_chain_mask(struct ath11k *ar, u32 ant, bool is_tx_ant)
3428 {
3429 	/* TODO: Check the request chainmask against the supported
3430 	 * chainmask table which is advertised in extented_service_ready event
3431 	 */
3432 
3433 	return 0;
3434 }
3435 
3436 static void ath11k_gen_ppe_thresh(struct ath11k_ppe_threshold *fw_ppet,
3437 				  u8 *he_ppet)
3438 {
3439 	int nss, ru;
3440 	u8 bit = 7;
3441 
3442 	he_ppet[0] = fw_ppet->numss_m1 & IEEE80211_PPE_THRES_NSS_MASK;
3443 	he_ppet[0] |= (fw_ppet->ru_bit_mask <<
3444 		       IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS) &
3445 		      IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK;
3446 	for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
3447 		for (ru = 0; ru < 4; ru++) {
3448 			u8 val;
3449 			int i;
3450 
3451 			if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
3452 				continue;
3453 			val = (fw_ppet->ppet16_ppet8_ru3_ru0[nss] >> (ru * 6)) &
3454 			       0x3f;
3455 			val = ((val >> 3) & 0x7) | ((val & 0x7) << 3);
3456 			for (i = 5; i >= 0; i--) {
3457 				he_ppet[bit / 8] |=
3458 					((val >> i) & 0x1) << ((bit % 8));
3459 				bit++;
3460 			}
3461 		}
3462 	}
3463 }
3464 
3465 static void
3466 ath11k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem *he_cap_elem)
3467 {
3468 	u8 m;
3469 
3470 	m = IEEE80211_HE_MAC_CAP0_TWT_RES |
3471 	    IEEE80211_HE_MAC_CAP0_TWT_REQ;
3472 	he_cap_elem->mac_cap_info[0] &= ~m;
3473 
3474 	m = IEEE80211_HE_MAC_CAP2_TRS |
3475 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
3476 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
3477 	he_cap_elem->mac_cap_info[2] &= ~m;
3478 
3479 	m = IEEE80211_HE_MAC_CAP3_FLEX_TWT_SCHED |
3480 	    IEEE80211_HE_MAC_CAP2_BCAST_TWT |
3481 	    IEEE80211_HE_MAC_CAP2_MU_CASCADING;
3482 	he_cap_elem->mac_cap_info[3] &= ~m;
3483 
3484 	m = IEEE80211_HE_MAC_CAP4_BSRP_BQRP_A_MPDU_AGG |
3485 	    IEEE80211_HE_MAC_CAP4_BQR;
3486 	he_cap_elem->mac_cap_info[4] &= ~m;
3487 
3488 	m = IEEE80211_HE_MAC_CAP5_SUBCHAN_SELECVITE_TRANSMISSION |
3489 	    IEEE80211_HE_MAC_CAP5_UL_2x996_TONE_RU |
3490 	    IEEE80211_HE_MAC_CAP5_PUNCTURED_SOUNDING |
3491 	    IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
3492 	he_cap_elem->mac_cap_info[5] &= ~m;
3493 
3494 	m = IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
3495 	    IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
3496 	he_cap_elem->phy_cap_info[2] &= ~m;
3497 
3498 	m = IEEE80211_HE_PHY_CAP3_RX_HE_MU_PPDU_FROM_NON_AP_STA |
3499 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK |
3500 	    IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_MASK;
3501 	he_cap_elem->phy_cap_info[3] &= ~m;
3502 
3503 	m = IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
3504 	he_cap_elem->phy_cap_info[4] &= ~m;
3505 
3506 	m = IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
3507 	he_cap_elem->phy_cap_info[5] &= ~m;
3508 
3509 	m = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
3510 	    IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMER_FB |
3511 	    IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
3512 	    IEEE80211_HE_PHY_CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO;
3513 	he_cap_elem->phy_cap_info[6] &= ~m;
3514 
3515 	m = IEEE80211_HE_PHY_CAP7_SRP_BASED_SR |
3516 	    IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_AR |
3517 	    IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ |
3518 	    IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ;
3519 	he_cap_elem->phy_cap_info[7] &= ~m;
3520 
3521 	m = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
3522 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
3523 	    IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
3524 	    IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
3525 	he_cap_elem->phy_cap_info[8] &= ~m;
3526 
3527 	m = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
3528 	    IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
3529 	    IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
3530 	    IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
3531 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
3532 	    IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
3533 	he_cap_elem->phy_cap_info[9] &= ~m;
3534 }
3535 
3536 static int ath11k_mac_copy_he_cap(struct ath11k *ar,
3537 				  struct ath11k_pdev_cap *cap,
3538 				  struct ieee80211_sband_iftype_data *data,
3539 				  int band)
3540 {
3541 	int i, idx = 0;
3542 
3543 	for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
3544 		struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
3545 		struct ath11k_band_cap *band_cap = &cap->band[band];
3546 		struct ieee80211_he_cap_elem *he_cap_elem =
3547 				&he_cap->he_cap_elem;
3548 
3549 		switch (i) {
3550 		case NL80211_IFTYPE_STATION:
3551 		case NL80211_IFTYPE_AP:
3552 		case NL80211_IFTYPE_MESH_POINT:
3553 			break;
3554 
3555 		default:
3556 			continue;
3557 		}
3558 
3559 		data[idx].types_mask = BIT(i);
3560 		he_cap->has_he = true;
3561 		memcpy(he_cap_elem->mac_cap_info, band_cap->he_cap_info,
3562 		       sizeof(he_cap_elem->mac_cap_info));
3563 		memcpy(he_cap_elem->phy_cap_info, band_cap->he_cap_phy_info,
3564 		       sizeof(he_cap_elem->phy_cap_info));
3565 
3566 		he_cap_elem->mac_cap_info[1] |=
3567 			IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_MASK;
3568 		he_cap_elem->phy_cap_info[4] &=
3569 			~IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_UNDER_80MHZ_MASK;
3570 		he_cap_elem->phy_cap_info[4] &=
3571 			~IEEE80211_HE_PHY_CAP4_BEAMFORMEE_MAX_STS_ABOVE_80MHZ_MASK;
3572 		he_cap_elem->phy_cap_info[4] |= (ar->num_tx_chains - 1) << 2;
3573 
3574 		he_cap_elem->phy_cap_info[5] &=
3575 			~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK;
3576 		he_cap_elem->phy_cap_info[5] &=
3577 			~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_ABOVE_80MHZ_MASK;
3578 		he_cap_elem->phy_cap_info[5] |= ar->num_tx_chains - 1;
3579 
3580 		switch (i) {
3581 		case NL80211_IFTYPE_AP:
3582 			he_cap_elem->phy_cap_info[9] |=
3583 				IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
3584 			break;
3585 		case NL80211_IFTYPE_STATION:
3586 			he_cap_elem->mac_cap_info[0] &=
3587 				~IEEE80211_HE_MAC_CAP0_TWT_RES;
3588 			he_cap_elem->mac_cap_info[0] |=
3589 				IEEE80211_HE_MAC_CAP0_TWT_REQ;
3590 			he_cap_elem->phy_cap_info[9] |=
3591 				IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
3592 			break;
3593 		case NL80211_IFTYPE_MESH_POINT:
3594 			ath11k_mac_filter_he_cap_mesh(he_cap_elem);
3595 			break;
3596 		}
3597 
3598 		he_cap->he_mcs_nss_supp.rx_mcs_80 =
3599 			cpu_to_le16(band_cap->he_mcs & 0xffff);
3600 		he_cap->he_mcs_nss_supp.tx_mcs_80 =
3601 			cpu_to_le16(band_cap->he_mcs & 0xffff);
3602 		he_cap->he_mcs_nss_supp.rx_mcs_160 =
3603 			cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
3604 		he_cap->he_mcs_nss_supp.tx_mcs_160 =
3605 			cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
3606 		he_cap->he_mcs_nss_supp.rx_mcs_80p80 =
3607 			cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
3608 		he_cap->he_mcs_nss_supp.tx_mcs_80p80 =
3609 			cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
3610 
3611 		memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
3612 		if (he_cap_elem->phy_cap_info[6] &
3613 		    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
3614 			ath11k_gen_ppe_thresh(&band_cap->he_ppet,
3615 					      he_cap->ppe_thres);
3616 		idx++;
3617 	}
3618 
3619 	return idx;
3620 }
3621 
3622 static void ath11k_mac_setup_he_cap(struct ath11k *ar,
3623 				    struct ath11k_pdev_cap *cap)
3624 {
3625 	struct ieee80211_supported_band *band;
3626 	int count;
3627 
3628 	if (cap->supported_bands & WMI_HOST_WLAN_2G_CAP) {
3629 		count = ath11k_mac_copy_he_cap(ar, cap,
3630 					       ar->mac.iftype[NL80211_BAND_2GHZ],
3631 					       NL80211_BAND_2GHZ);
3632 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
3633 		band->iftype_data = ar->mac.iftype[NL80211_BAND_2GHZ];
3634 		band->n_iftype_data = count;
3635 	}
3636 
3637 	if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP) {
3638 		count = ath11k_mac_copy_he_cap(ar, cap,
3639 					       ar->mac.iftype[NL80211_BAND_5GHZ],
3640 					       NL80211_BAND_5GHZ);
3641 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
3642 		band->iftype_data = ar->mac.iftype[NL80211_BAND_5GHZ];
3643 		band->n_iftype_data = count;
3644 	}
3645 }
3646 
3647 static int __ath11k_set_antenna(struct ath11k *ar, u32 tx_ant, u32 rx_ant)
3648 {
3649 	int ret;
3650 
3651 	lockdep_assert_held(&ar->conf_mutex);
3652 
3653 	if (ath11k_check_chain_mask(ar, tx_ant, true))
3654 		return -EINVAL;
3655 
3656 	if (ath11k_check_chain_mask(ar, rx_ant, false))
3657 		return -EINVAL;
3658 
3659 	ar->cfg_tx_chainmask = tx_ant;
3660 	ar->cfg_rx_chainmask = rx_ant;
3661 
3662 	if (ar->state != ATH11K_STATE_ON &&
3663 	    ar->state != ATH11K_STATE_RESTARTED)
3664 		return 0;
3665 
3666 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_TX_CHAIN_MASK,
3667 					tx_ant, ar->pdev->pdev_id);
3668 	if (ret) {
3669 		ath11k_warn(ar->ab, "failed to set tx-chainmask: %d, req 0x%x\n",
3670 			    ret, tx_ant);
3671 		return ret;
3672 	}
3673 
3674 	ar->num_tx_chains = get_num_chains(tx_ant);
3675 
3676 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_RX_CHAIN_MASK,
3677 					rx_ant, ar->pdev->pdev_id);
3678 	if (ret) {
3679 		ath11k_warn(ar->ab, "failed to set rx-chainmask: %d, req 0x%x\n",
3680 			    ret, rx_ant);
3681 		return ret;
3682 	}
3683 
3684 	ar->num_rx_chains = get_num_chains(rx_ant);
3685 
3686 	/* Reload HT/VHT/HE capability */
3687 	ath11k_mac_setup_ht_vht_cap(ar, &ar->pdev->cap, NULL);
3688 	ath11k_mac_setup_he_cap(ar, &ar->pdev->cap);
3689 
3690 	return 0;
3691 }
3692 
3693 int ath11k_mac_tx_mgmt_pending_free(int buf_id, void *skb, void *ctx)
3694 {
3695 	struct sk_buff *msdu = skb;
3696 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(msdu);
3697 	struct ath11k *ar = ctx;
3698 	struct ath11k_base *ab = ar->ab;
3699 
3700 	spin_lock_bh(&ar->txmgmt_idr_lock);
3701 	idr_remove(&ar->txmgmt_idr, buf_id);
3702 	spin_unlock_bh(&ar->txmgmt_idr_lock);
3703 	dma_unmap_single(ab->dev, ATH11K_SKB_CB(msdu)->paddr, msdu->len,
3704 			 DMA_TO_DEVICE);
3705 
3706 	info = IEEE80211_SKB_CB(msdu);
3707 	memset(&info->status, 0, sizeof(info->status));
3708 
3709 	ieee80211_free_txskb(ar->hw, msdu);
3710 
3711 	return 0;
3712 }
3713 
3714 static int ath11k_mac_vif_txmgmt_idr_remove(int buf_id, void *skb, void *ctx)
3715 {
3716 	struct ieee80211_vif *vif = ctx;
3717 	struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB((struct sk_buff *)skb);
3718 	struct sk_buff *msdu = skb;
3719 	struct ath11k *ar = skb_cb->ar;
3720 	struct ath11k_base *ab = ar->ab;
3721 
3722 	if (skb_cb->vif == vif) {
3723 		spin_lock_bh(&ar->txmgmt_idr_lock);
3724 		idr_remove(&ar->txmgmt_idr, buf_id);
3725 		spin_unlock_bh(&ar->txmgmt_idr_lock);
3726 		dma_unmap_single(ab->dev, skb_cb->paddr, msdu->len,
3727 				 DMA_TO_DEVICE);
3728 	}
3729 
3730 	return 0;
3731 }
3732 
3733 static int ath11k_mac_mgmt_tx_wmi(struct ath11k *ar, struct ath11k_vif *arvif,
3734 				  struct sk_buff *skb)
3735 {
3736 	struct ath11k_base *ab = ar->ab;
3737 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
3738 	struct ieee80211_tx_info *info;
3739 	dma_addr_t paddr;
3740 	int buf_id;
3741 	int ret;
3742 
3743 	spin_lock_bh(&ar->txmgmt_idr_lock);
3744 	buf_id = idr_alloc(&ar->txmgmt_idr, skb, 0,
3745 			   ATH11K_TX_MGMT_NUM_PENDING_MAX, GFP_ATOMIC);
3746 	spin_unlock_bh(&ar->txmgmt_idr_lock);
3747 	if (buf_id < 0)
3748 		return -ENOSPC;
3749 
3750 	info = IEEE80211_SKB_CB(skb);
3751 	if (!(info->control.flags & IEEE80211_TX_CTRL_HW_80211_ENCAP)) {
3752 		if ((ieee80211_is_action(hdr->frame_control) ||
3753 		     ieee80211_is_deauth(hdr->frame_control) ||
3754 		     ieee80211_is_disassoc(hdr->frame_control)) &&
3755 		     ieee80211_has_protected(hdr->frame_control)) {
3756 			skb_put(skb, IEEE80211_CCMP_MIC_LEN);
3757 		}
3758 	}
3759 
3760 	paddr = dma_map_single(ab->dev, skb->data, skb->len, DMA_TO_DEVICE);
3761 	if (dma_mapping_error(ab->dev, paddr)) {
3762 		ath11k_warn(ab, "failed to DMA map mgmt Tx buffer\n");
3763 		ret = -EIO;
3764 		goto err_free_idr;
3765 	}
3766 
3767 	ATH11K_SKB_CB(skb)->paddr = paddr;
3768 
3769 	ret = ath11k_wmi_mgmt_send(ar, arvif->vdev_id, buf_id, skb);
3770 	if (ret) {
3771 		ath11k_warn(ar->ab, "failed to send mgmt frame: %d\n", ret);
3772 		goto err_unmap_buf;
3773 	}
3774 
3775 	return 0;
3776 
3777 err_unmap_buf:
3778 	dma_unmap_single(ab->dev, ATH11K_SKB_CB(skb)->paddr,
3779 			 skb->len, DMA_TO_DEVICE);
3780 err_free_idr:
3781 	spin_lock_bh(&ar->txmgmt_idr_lock);
3782 	idr_remove(&ar->txmgmt_idr, buf_id);
3783 	spin_unlock_bh(&ar->txmgmt_idr_lock);
3784 
3785 	return ret;
3786 }
3787 
3788 static void ath11k_mgmt_over_wmi_tx_purge(struct ath11k *ar)
3789 {
3790 	struct sk_buff *skb;
3791 
3792 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL)
3793 		ieee80211_free_txskb(ar->hw, skb);
3794 }
3795 
3796 static void ath11k_mgmt_over_wmi_tx_work(struct work_struct *work)
3797 {
3798 	struct ath11k *ar = container_of(work, struct ath11k, wmi_mgmt_tx_work);
3799 	struct ieee80211_tx_info *info;
3800 	struct ath11k_vif *arvif;
3801 	struct sk_buff *skb;
3802 	int ret;
3803 
3804 	while ((skb = skb_dequeue(&ar->wmi_mgmt_tx_queue)) != NULL) {
3805 		info = IEEE80211_SKB_CB(skb);
3806 		if (!info->control.vif) {
3807 			ath11k_warn(ar->ab, "no vif found for mgmt frame, flags 0x%x\n",
3808 				    info->control.flags);
3809 			ieee80211_free_txskb(ar->hw, skb);
3810 			continue;
3811 		}
3812 
3813 		arvif = ath11k_vif_to_arvif(info->control.vif);
3814 		if (ar->allocated_vdev_map & (1LL << arvif->vdev_id) &&
3815 		    arvif->is_started) {
3816 			ret = ath11k_mac_mgmt_tx_wmi(ar, arvif, skb);
3817 			if (ret) {
3818 				ath11k_warn(ar->ab, "failed to tx mgmt frame, vdev_id %d :%d\n",
3819 					    arvif->vdev_id, ret);
3820 				ieee80211_free_txskb(ar->hw, skb);
3821 			} else {
3822 				atomic_inc(&ar->num_pending_mgmt_tx);
3823 			}
3824 		} else {
3825 			ath11k_warn(ar->ab,
3826 				    "dropping mgmt frame for vdev %d, flags 0x%x is_started %d\n",
3827 				    arvif->vdev_id, info->control.flags,
3828 				    arvif->is_started);
3829 			ieee80211_free_txskb(ar->hw, skb);
3830 		}
3831 	}
3832 }
3833 
3834 static int ath11k_mac_mgmt_tx(struct ath11k *ar, struct sk_buff *skb,
3835 			      bool is_prb_rsp)
3836 {
3837 	struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
3838 
3839 	if (test_bit(ATH11K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
3840 		return -ESHUTDOWN;
3841 
3842 	/* Drop probe response packets when the pending management tx
3843 	 * count has reached a certain threshold, so as to prioritize
3844 	 * other mgmt packets like auth and assoc to be sent on time
3845 	 * for establishing successful connections.
3846 	 */
3847 	if (is_prb_rsp &&
3848 	    atomic_read(&ar->num_pending_mgmt_tx) > ATH11K_PRB_RSP_DROP_THRESHOLD) {
3849 		ath11k_warn(ar->ab,
3850 			    "dropping probe response as pending queue is almost full\n");
3851 		return -ENOSPC;
3852 	}
3853 
3854 	if (skb_queue_len(q) == ATH11K_TX_MGMT_NUM_PENDING_MAX) {
3855 		ath11k_warn(ar->ab, "mgmt tx queue is full\n");
3856 		return -ENOSPC;
3857 	}
3858 
3859 	skb_queue_tail(q, skb);
3860 	ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
3861 
3862 	return 0;
3863 }
3864 
3865 static void ath11k_mac_op_tx(struct ieee80211_hw *hw,
3866 			     struct ieee80211_tx_control *control,
3867 			     struct sk_buff *skb)
3868 {
3869 	struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB(skb);
3870 	struct ath11k *ar = hw->priv;
3871 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3872 	struct ieee80211_vif *vif = info->control.vif;
3873 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
3874 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
3875 	bool is_prb_rsp;
3876 	int ret;
3877 
3878 	if (info->control.flags & IEEE80211_TX_CTRL_HW_80211_ENCAP) {
3879 		skb_cb->flags |= ATH11K_SKB_HW_80211_ENCAP;
3880 	} else if (ieee80211_is_mgmt(hdr->frame_control)) {
3881 		is_prb_rsp = ieee80211_is_probe_resp(hdr->frame_control);
3882 		ret = ath11k_mac_mgmt_tx(ar, skb, is_prb_rsp);
3883 		if (ret) {
3884 			ath11k_warn(ar->ab, "failed to queue management frame %d\n",
3885 				    ret);
3886 			ieee80211_free_txskb(ar->hw, skb);
3887 		}
3888 		return;
3889 	}
3890 
3891 	ret = ath11k_dp_tx(ar, arvif, skb);
3892 	if (ret) {
3893 		ath11k_warn(ar->ab, "failed to transmit frame %d\n", ret);
3894 		ieee80211_free_txskb(ar->hw, skb);
3895 	}
3896 }
3897 
3898 void ath11k_mac_drain_tx(struct ath11k *ar)
3899 {
3900 	/* make sure rcu-protected mac80211 tx path itself is drained */
3901 	synchronize_net();
3902 
3903 	cancel_work_sync(&ar->wmi_mgmt_tx_work);
3904 	ath11k_mgmt_over_wmi_tx_purge(ar);
3905 }
3906 
3907 static int ath11k_mac_config_mon_status_default(struct ath11k *ar, bool enable)
3908 {
3909 	struct htt_rx_ring_tlv_filter tlv_filter = {0};
3910 	u32 ring_id;
3911 
3912 	if (enable) {
3913 		tlv_filter = ath11k_mac_mon_status_filter_default;
3914 		tlv_filter.rx_filter = ath11k_debug_rx_filter(ar);
3915 	}
3916 
3917 	ring_id = ar->dp.rx_mon_status_refill_ring.refill_buf_ring.ring_id;
3918 
3919 	return ath11k_dp_tx_htt_rx_filter_setup(ar->ab, ring_id, ar->dp.mac_id,
3920 						HAL_RXDMA_MONITOR_STATUS,
3921 						DP_RX_BUFFER_SIZE, &tlv_filter);
3922 }
3923 
3924 static int ath11k_mac_op_start(struct ieee80211_hw *hw)
3925 {
3926 	struct ath11k *ar = hw->priv;
3927 	struct ath11k_base *ab = ar->ab;
3928 	struct ath11k_pdev *pdev = ar->pdev;
3929 	int ret;
3930 
3931 	ath11k_mac_drain_tx(ar);
3932 	mutex_lock(&ar->conf_mutex);
3933 
3934 	switch (ar->state) {
3935 	case ATH11K_STATE_OFF:
3936 		ar->state = ATH11K_STATE_ON;
3937 		break;
3938 	case ATH11K_STATE_RESTARTING:
3939 		ar->state = ATH11K_STATE_RESTARTED;
3940 		break;
3941 	case ATH11K_STATE_RESTARTED:
3942 	case ATH11K_STATE_WEDGED:
3943 	case ATH11K_STATE_ON:
3944 		WARN_ON(1);
3945 		ret = -EINVAL;
3946 		goto err;
3947 	}
3948 
3949 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_PMF_QOS,
3950 					1, pdev->pdev_id);
3951 
3952 	if (ret) {
3953 		ath11k_err(ar->ab, "failed to enable PMF QOS: (%d\n", ret);
3954 		goto err;
3955 	}
3956 
3957 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_DYNAMIC_BW, 1,
3958 					pdev->pdev_id);
3959 	if (ret) {
3960 		ath11k_err(ar->ab, "failed to enable dynamic bw: %d\n", ret);
3961 		goto err;
3962 	}
3963 
3964 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_ARP_AC_OVERRIDE,
3965 					0, pdev->pdev_id);
3966 	if (ret) {
3967 		ath11k_err(ab, "failed to set ac override for ARP: %d\n",
3968 			   ret);
3969 		goto err;
3970 	}
3971 
3972 	ret = ath11k_wmi_send_dfs_phyerr_offload_enable_cmd(ar, pdev->pdev_id);
3973 	if (ret) {
3974 		ath11k_err(ab, "failed to offload radar detection: %d\n",
3975 			   ret);
3976 		goto err;
3977 	}
3978 
3979 	ret = ath11k_dp_tx_htt_h2t_ppdu_stats_req(ar,
3980 						  HTT_PPDU_STATS_TAG_DEFAULT);
3981 	if (ret) {
3982 		ath11k_err(ab, "failed to req ppdu stats: %d\n", ret);
3983 		goto err;
3984 	}
3985 
3986 	ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_MESH_MCAST_ENABLE,
3987 					1, pdev->pdev_id);
3988 
3989 	if (ret) {
3990 		ath11k_err(ar->ab, "failed to enable MESH MCAST ENABLE: (%d\n", ret);
3991 		goto err;
3992 	}
3993 
3994 	__ath11k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
3995 
3996 	/* TODO: Do we need to enable ANI? */
3997 
3998 	ath11k_reg_update_chan_list(ar);
3999 
4000 	ar->num_started_vdevs = 0;
4001 	ar->num_created_vdevs = 0;
4002 	ar->num_peers = 0;
4003 	ar->allocated_vdev_map = 0;
4004 
4005 	/* Configure monitor status ring with default rx_filter to get rx status
4006 	 * such as rssi, rx_duration.
4007 	 */
4008 	ret = ath11k_mac_config_mon_status_default(ar, true);
4009 	if (ret) {
4010 		ath11k_err(ab, "failed to configure monitor status ring with default rx_filter: (%d)\n",
4011 			   ret);
4012 		goto err;
4013 	}
4014 
4015 	/* Configure the hash seed for hash based reo dest ring selection */
4016 	ath11k_wmi_pdev_lro_cfg(ar, ar->pdev->pdev_id);
4017 
4018 	mutex_unlock(&ar->conf_mutex);
4019 
4020 	rcu_assign_pointer(ab->pdevs_active[ar->pdev_idx],
4021 			   &ab->pdevs[ar->pdev_idx]);
4022 
4023 	return 0;
4024 
4025 err:
4026 	ar->state = ATH11K_STATE_OFF;
4027 	mutex_unlock(&ar->conf_mutex);
4028 
4029 	return ret;
4030 }
4031 
4032 static void ath11k_mac_op_stop(struct ieee80211_hw *hw)
4033 {
4034 	struct ath11k *ar = hw->priv;
4035 	struct htt_ppdu_stats_info *ppdu_stats, *tmp;
4036 	int ret;
4037 
4038 	ath11k_mac_drain_tx(ar);
4039 
4040 	mutex_lock(&ar->conf_mutex);
4041 	ret = ath11k_mac_config_mon_status_default(ar, false);
4042 	if (ret)
4043 		ath11k_err(ar->ab, "failed to clear rx_filter for monitor status ring: (%d)\n",
4044 			   ret);
4045 
4046 	clear_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
4047 	ar->state = ATH11K_STATE_OFF;
4048 	mutex_unlock(&ar->conf_mutex);
4049 
4050 	cancel_delayed_work_sync(&ar->scan.timeout);
4051 	cancel_work_sync(&ar->regd_update_work);
4052 
4053 	spin_lock_bh(&ar->data_lock);
4054 	list_for_each_entry_safe(ppdu_stats, tmp, &ar->ppdu_stats_info, list) {
4055 		list_del(&ppdu_stats->list);
4056 		kfree(ppdu_stats);
4057 	}
4058 	spin_unlock_bh(&ar->data_lock);
4059 
4060 	rcu_assign_pointer(ar->ab->pdevs_active[ar->pdev_idx], NULL);
4061 
4062 	synchronize_rcu();
4063 
4064 	atomic_set(&ar->num_pending_mgmt_tx, 0);
4065 }
4066 
4067 static void
4068 ath11k_mac_setup_vdev_create_params(struct ath11k_vif *arvif,
4069 				    struct vdev_create_params *params)
4070 {
4071 	struct ath11k *ar = arvif->ar;
4072 	struct ath11k_pdev *pdev = ar->pdev;
4073 
4074 	params->if_id = arvif->vdev_id;
4075 	params->type = arvif->vdev_type;
4076 	params->subtype = arvif->vdev_subtype;
4077 	params->pdev_id = pdev->pdev_id;
4078 
4079 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP) {
4080 		params->chains[NL80211_BAND_2GHZ].tx = ar->num_tx_chains;
4081 		params->chains[NL80211_BAND_2GHZ].rx = ar->num_rx_chains;
4082 	}
4083 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP) {
4084 		params->chains[NL80211_BAND_5GHZ].tx = ar->num_tx_chains;
4085 		params->chains[NL80211_BAND_5GHZ].rx = ar->num_rx_chains;
4086 	}
4087 }
4088 
4089 static u32
4090 ath11k_mac_prepare_he_mode(struct ath11k_pdev *pdev, u32 viftype)
4091 {
4092 	struct ath11k_pdev_cap *pdev_cap = &pdev->cap;
4093 	struct ath11k_band_cap *cap_band = NULL;
4094 	u32 *hecap_phy_ptr = NULL;
4095 	u32 hemode = 0;
4096 
4097 	if (pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP)
4098 		cap_band = &pdev_cap->band[NL80211_BAND_2GHZ];
4099 	else
4100 		cap_band = &pdev_cap->band[NL80211_BAND_5GHZ];
4101 
4102 	hecap_phy_ptr = &cap_band->he_cap_phy_info[0];
4103 
4104 	hemode = FIELD_PREP(HE_MODE_SU_TX_BFEE, HE_SU_BFEE_ENABLE) |
4105 		 FIELD_PREP(HE_MODE_SU_TX_BFER, HECAP_PHY_SUBFMR_GET(hecap_phy_ptr)) |
4106 		 FIELD_PREP(HE_MODE_UL_MUMIMO, HECAP_PHY_ULMUMIMO_GET(hecap_phy_ptr));
4107 
4108 	/* TODO WDS and other modes */
4109 	if (viftype == NL80211_IFTYPE_AP) {
4110 		hemode |= FIELD_PREP(HE_MODE_MU_TX_BFER,
4111 			  HECAP_PHY_MUBFMR_GET(hecap_phy_ptr)) |
4112 			  FIELD_PREP(HE_MODE_DL_OFDMA, HE_DL_MUOFDMA_ENABLE) |
4113 			  FIELD_PREP(HE_MODE_UL_OFDMA, HE_UL_MUOFDMA_ENABLE);
4114 	} else {
4115 		hemode |= FIELD_PREP(HE_MODE_MU_TX_BFEE, HE_MU_BFEE_ENABLE);
4116 	}
4117 
4118 	return hemode;
4119 }
4120 
4121 static int ath11k_set_he_mu_sounding_mode(struct ath11k *ar,
4122 					  struct ath11k_vif *arvif)
4123 {
4124 	u32 param_id, param_value;
4125 	struct ath11k_base *ab = ar->ab;
4126 	int ret = 0;
4127 
4128 	param_id = WMI_VDEV_PARAM_SET_HEMU_MODE;
4129 	param_value = ath11k_mac_prepare_he_mode(ar->pdev, arvif->vif->type);
4130 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4131 					    param_id, param_value);
4132 	if (ret) {
4133 		ath11k_warn(ab, "failed to set vdev %d HE MU mode: %d param_value %x\n",
4134 			    arvif->vdev_id, ret, param_value);
4135 		return ret;
4136 	}
4137 	param_id = WMI_VDEV_PARAM_SET_HE_SOUNDING_MODE;
4138 	param_value =
4139 		FIELD_PREP(HE_VHT_SOUNDING_MODE, HE_VHT_SOUNDING_MODE_ENABLE) |
4140 		FIELD_PREP(HE_TRIG_NONTRIG_SOUNDING_MODE,
4141 			   HE_TRIG_NONTRIG_SOUNDING_MODE_ENABLE);
4142 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4143 					    param_id, param_value);
4144 	if (ret) {
4145 		ath11k_warn(ab, "failed to set vdev %d HE MU mode: %d\n",
4146 			    arvif->vdev_id, ret);
4147 		return ret;
4148 	}
4149 	return ret;
4150 }
4151 
4152 static int ath11k_mac_op_add_interface(struct ieee80211_hw *hw,
4153 				       struct ieee80211_vif *vif)
4154 {
4155 	struct ath11k *ar = hw->priv;
4156 	struct ath11k_base *ab = ar->ab;
4157 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4158 	struct vdev_create_params vdev_param = {0};
4159 	struct peer_create_params peer_param;
4160 	u32 param_id, param_value;
4161 	int hw_encap = 0;
4162 	u16 nss;
4163 	int i;
4164 	int ret;
4165 	int bit;
4166 
4167 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
4168 
4169 	mutex_lock(&ar->conf_mutex);
4170 
4171 	if (vif->type == NL80211_IFTYPE_AP &&
4172 	    ar->num_peers > (ar->max_num_peers - 1)) {
4173 		ath11k_warn(ab, "failed to create vdev due to insufficient peer entry resource in firmware\n");
4174 		ret = -ENOBUFS;
4175 		goto err;
4176 	}
4177 
4178 	if (ar->num_created_vdevs > (TARGET_NUM_VDEVS - 1)) {
4179 		ath11k_warn(ab, "failed to create vdev, reached max vdev limit %d\n",
4180 			    TARGET_NUM_VDEVS);
4181 		ret = -EBUSY;
4182 		goto err;
4183 	}
4184 
4185 	memset(arvif, 0, sizeof(*arvif));
4186 
4187 	arvif->ar = ar;
4188 	arvif->vif = vif;
4189 
4190 	INIT_LIST_HEAD(&arvif->list);
4191 
4192 	/* Should we initialize any worker to handle connection loss indication
4193 	 * from firmware in sta mode?
4194 	 */
4195 
4196 	for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
4197 		arvif->bitrate_mask.control[i].legacy = 0xffffffff;
4198 		memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
4199 		       sizeof(arvif->bitrate_mask.control[i].ht_mcs));
4200 		memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
4201 		       sizeof(arvif->bitrate_mask.control[i].vht_mcs));
4202 	}
4203 
4204 	bit = __ffs64(ab->free_vdev_map);
4205 
4206 	arvif->vdev_id = bit;
4207 	arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
4208 
4209 	switch (vif->type) {
4210 	case NL80211_IFTYPE_UNSPECIFIED:
4211 	case NL80211_IFTYPE_STATION:
4212 		arvif->vdev_type = WMI_VDEV_TYPE_STA;
4213 		break;
4214 	case NL80211_IFTYPE_MESH_POINT:
4215 		arvif->vdev_subtype = WMI_VDEV_SUBTYPE_MESH_11S;
4216 		/* fall through */
4217 	case NL80211_IFTYPE_AP:
4218 		arvif->vdev_type = WMI_VDEV_TYPE_AP;
4219 		break;
4220 	case NL80211_IFTYPE_MONITOR:
4221 		arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
4222 		break;
4223 	default:
4224 		WARN_ON(1);
4225 		break;
4226 	}
4227 
4228 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac add interface id %d type %d subtype %d map %llx\n",
4229 		   arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
4230 		   ab->free_vdev_map);
4231 
4232 	vif->cab_queue = arvif->vdev_id % (ATH11K_HW_MAX_QUEUES - 1);
4233 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
4234 		vif->hw_queue[i] = i % (ATH11K_HW_MAX_QUEUES - 1);
4235 
4236 	ath11k_mac_setup_vdev_create_params(arvif, &vdev_param);
4237 
4238 	ret = ath11k_wmi_vdev_create(ar, vif->addr, &vdev_param);
4239 	if (ret) {
4240 		ath11k_warn(ab, "failed to create WMI vdev %d: %d\n",
4241 			    arvif->vdev_id, ret);
4242 		goto err;
4243 	}
4244 
4245 	ar->num_created_vdevs++;
4246 	ath11k_dbg(ab, ATH11K_DBG_MAC, "vdev %pM created, vdev_id %d\n",
4247 		   vif->addr, arvif->vdev_id);
4248 	ar->allocated_vdev_map |= 1LL << arvif->vdev_id;
4249 	ab->free_vdev_map &= ~(1LL << arvif->vdev_id);
4250 
4251 	spin_lock_bh(&ar->data_lock);
4252 	list_add(&arvif->list, &ar->arvifs);
4253 	spin_unlock_bh(&ar->data_lock);
4254 
4255 	param_id = WMI_VDEV_PARAM_TX_ENCAP_TYPE;
4256 	if (ath11k_frame_mode == ATH11K_HW_TXRX_ETHERNET)
4257 		switch (vif->type) {
4258 		case NL80211_IFTYPE_STATION:
4259 		case NL80211_IFTYPE_AP_VLAN:
4260 		case NL80211_IFTYPE_AP:
4261 			hw_encap = 1;
4262 			break;
4263 		default:
4264 			break;
4265 		}
4266 
4267 	if (ieee80211_set_hw_80211_encap(vif, hw_encap))
4268 		param_value = ATH11K_HW_TXRX_ETHERNET;
4269 	else
4270 		param_value = ATH11K_HW_TXRX_NATIVE_WIFI;
4271 
4272 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4273 					    param_id, param_value);
4274 	if (ret) {
4275 		ath11k_warn(ab, "failed to set vdev %d tx encap mode: %d\n",
4276 			    arvif->vdev_id, ret);
4277 		goto err_vdev_del;
4278 	}
4279 
4280 	nss = get_num_chains(ar->cfg_tx_chainmask) ? : 1;
4281 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4282 					    WMI_VDEV_PARAM_NSS, nss);
4283 	if (ret) {
4284 		ath11k_warn(ab, "failed to set vdev %d chainmask 0x%x, nss %d :%d\n",
4285 			    arvif->vdev_id, ar->cfg_tx_chainmask, nss, ret);
4286 		goto err_vdev_del;
4287 	}
4288 
4289 	switch (arvif->vdev_type) {
4290 	case WMI_VDEV_TYPE_AP:
4291 		peer_param.vdev_id = arvif->vdev_id;
4292 		peer_param.peer_addr = vif->addr;
4293 		peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
4294 		ret = ath11k_peer_create(ar, arvif, NULL, &peer_param);
4295 		if (ret) {
4296 			ath11k_warn(ab, "failed to vdev %d create peer for AP: %d\n",
4297 				    arvif->vdev_id, ret);
4298 			goto err_vdev_del;
4299 		}
4300 
4301 		ret = ath11k_mac_set_kickout(arvif);
4302 		if (ret) {
4303 			ath11k_warn(ar->ab, "failed to set vdev %i kickout parameters: %d\n",
4304 				    arvif->vdev_id, ret);
4305 			goto err_peer_del;
4306 		}
4307 		break;
4308 	case WMI_VDEV_TYPE_STA:
4309 		param_id = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
4310 		param_value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
4311 		ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4312 						  param_id, param_value);
4313 		if (ret) {
4314 			ath11k_warn(ar->ab, "failed to set vdev %d RX wake policy: %d\n",
4315 				    arvif->vdev_id, ret);
4316 			goto err_peer_del;
4317 		}
4318 
4319 		param_id = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
4320 		param_value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
4321 		ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4322 						  param_id, param_value);
4323 		if (ret) {
4324 			ath11k_warn(ar->ab, "failed to set vdev %d TX wake threshold: %d\n",
4325 				    arvif->vdev_id, ret);
4326 			goto err_peer_del;
4327 		}
4328 
4329 		param_id = WMI_STA_PS_PARAM_PSPOLL_COUNT;
4330 		param_value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
4331 		ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4332 						  param_id, param_value);
4333 		if (ret) {
4334 			ath11k_warn(ar->ab, "failed to set vdev %d pspoll count: %d\n",
4335 				    arvif->vdev_id, ret);
4336 			goto err_peer_del;
4337 		}
4338 
4339 		ret = ath11k_wmi_pdev_set_ps_mode(ar, arvif->vdev_id, false);
4340 		if (ret) {
4341 			ath11k_warn(ar->ab, "failed to disable vdev %d ps mode: %d\n",
4342 				    arvif->vdev_id, ret);
4343 			goto err_peer_del;
4344 		}
4345 		break;
4346 	default:
4347 		break;
4348 	}
4349 
4350 	arvif->txpower = vif->bss_conf.txpower;
4351 	ret = ath11k_mac_txpower_recalc(ar);
4352 	if (ret)
4353 		goto err_peer_del;
4354 
4355 	param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
4356 	param_value = ar->hw->wiphy->rts_threshold;
4357 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
4358 					    param_id, param_value);
4359 	if (ret) {
4360 		ath11k_warn(ar->ab, "failed to set rts threshold for vdev %d: %d\n",
4361 			    arvif->vdev_id, ret);
4362 	}
4363 
4364 	ath11k_dp_vdev_tx_attach(ar, arvif);
4365 
4366 	mutex_unlock(&ar->conf_mutex);
4367 
4368 	return 0;
4369 
4370 err_peer_del:
4371 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
4372 		ar->num_peers--;
4373 		ath11k_wmi_send_peer_delete_cmd(ar, vif->addr, arvif->vdev_id);
4374 	}
4375 
4376 err_vdev_del:
4377 	ath11k_wmi_vdev_delete(ar, arvif->vdev_id);
4378 	ar->num_created_vdevs--;
4379 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
4380 	ab->free_vdev_map |= 1LL << arvif->vdev_id;
4381 	spin_lock_bh(&ar->data_lock);
4382 	list_del(&arvif->list);
4383 	spin_unlock_bh(&ar->data_lock);
4384 
4385 err:
4386 	mutex_unlock(&ar->conf_mutex);
4387 
4388 	return ret;
4389 }
4390 
4391 static int ath11k_mac_vif_unref(int buf_id, void *skb, void *ctx)
4392 {
4393 	struct ieee80211_vif *vif = (struct ieee80211_vif *)ctx;
4394 	struct ath11k_skb_cb *skb_cb = ATH11K_SKB_CB((struct sk_buff *)skb);
4395 
4396 	if (skb_cb->vif == vif)
4397 		skb_cb->vif = NULL;
4398 
4399 	return 0;
4400 }
4401 
4402 static void ath11k_mac_op_remove_interface(struct ieee80211_hw *hw,
4403 					   struct ieee80211_vif *vif)
4404 {
4405 	struct ath11k *ar = hw->priv;
4406 	struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
4407 	struct ath11k_base *ab = ar->ab;
4408 	int ret;
4409 	int i;
4410 
4411 	mutex_lock(&ar->conf_mutex);
4412 
4413 	ath11k_dbg(ab, ATH11K_DBG_MAC, "mac remove interface (vdev %d)\n",
4414 		   arvif->vdev_id);
4415 
4416 	spin_lock_bh(&ar->data_lock);
4417 	list_del(&arvif->list);
4418 	spin_unlock_bh(&ar->data_lock);
4419 
4420 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
4421 		ret = ath11k_peer_delete(ar, arvif->vdev_id, vif->addr);
4422 		if (ret)
4423 			ath11k_warn(ab, "failed to submit AP self-peer removal on vdev %d: %d\n",
4424 				    arvif->vdev_id, ret);
4425 	}
4426 
4427 	ret = ath11k_wmi_vdev_delete(ar, arvif->vdev_id);
4428 	if (ret)
4429 		ath11k_warn(ab, "failed to delete WMI vdev %d: %d\n",
4430 			    arvif->vdev_id, ret);
4431 
4432 	ar->num_created_vdevs--;
4433 	ath11k_dbg(ab, ATH11K_DBG_MAC, "vdev %pM deleted, vdev_id %d\n",
4434 		   vif->addr, arvif->vdev_id);
4435 	ar->allocated_vdev_map &= ~(1LL << arvif->vdev_id);
4436 	ab->free_vdev_map |= 1LL << (arvif->vdev_id);
4437 
4438 	ath11k_peer_cleanup(ar, arvif->vdev_id);
4439 
4440 	idr_for_each(&ar->txmgmt_idr,
4441 		     ath11k_mac_vif_txmgmt_idr_remove, vif);
4442 
4443 	for (i = 0; i < DP_TCL_NUM_RING_MAX; i++) {
4444 		spin_lock_bh(&ab->dp.tx_ring[i].tx_idr_lock);
4445 		idr_for_each(&ab->dp.tx_ring[i].txbuf_idr,
4446 			     ath11k_mac_vif_unref, vif);
4447 		spin_unlock_bh(&ab->dp.tx_ring[i].tx_idr_lock);
4448 	}
4449 
4450 	/* Recalc txpower for remaining vdev */
4451 	ath11k_mac_txpower_recalc(ar);
4452 	clear_bit(ATH11K_FLAG_MONITOR_ENABLED, &ar->monitor_flags);
4453 
4454 	/* TODO: recal traffic pause state based on the available vdevs */
4455 
4456 	mutex_unlock(&ar->conf_mutex);
4457 }
4458 
4459 /* FIXME: Has to be verified. */
4460 #define SUPPORTED_FILTERS			\
4461 	(FIF_ALLMULTI |				\
4462 	FIF_CONTROL |				\
4463 	FIF_PSPOLL |				\
4464 	FIF_OTHER_BSS |				\
4465 	FIF_BCN_PRBRESP_PROMISC |		\
4466 	FIF_PROBE_REQ |				\
4467 	FIF_FCSFAIL)
4468 
4469 static void ath11k_mac_op_configure_filter(struct ieee80211_hw *hw,
4470 					   unsigned int changed_flags,
4471 					   unsigned int *total_flags,
4472 					   u64 multicast)
4473 {
4474 	struct ath11k *ar = hw->priv;
4475 	bool reset_flag = false;
4476 	int ret = 0;
4477 
4478 	mutex_lock(&ar->conf_mutex);
4479 
4480 	changed_flags &= SUPPORTED_FILTERS;
4481 	*total_flags &= SUPPORTED_FILTERS;
4482 	ar->filter_flags = *total_flags;
4483 
4484 	/* For monitor mode */
4485 	reset_flag = !(ar->filter_flags & FIF_BCN_PRBRESP_PROMISC);
4486 
4487 	ret = ath11k_dp_tx_htt_monitor_mode_ring_config(ar, reset_flag);
4488 	if (!ret) {
4489 		if (!reset_flag)
4490 			set_bit(ATH11K_FLAG_MONITOR_ENABLED, &ar->monitor_flags);
4491 		else
4492 			clear_bit(ATH11K_FLAG_MONITOR_ENABLED, &ar->monitor_flags);
4493 	} else {
4494 		ath11k_warn(ar->ab,
4495 			    "fail to set monitor filter: %d\n", ret);
4496 	}
4497 	mutex_unlock(&ar->conf_mutex);
4498 }
4499 
4500 static int ath11k_mac_op_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
4501 {
4502 	struct ath11k *ar = hw->priv;
4503 
4504 	mutex_lock(&ar->conf_mutex);
4505 
4506 	*tx_ant = ar->cfg_tx_chainmask;
4507 	*rx_ant = ar->cfg_rx_chainmask;
4508 
4509 	mutex_unlock(&ar->conf_mutex);
4510 
4511 	return 0;
4512 }
4513 
4514 static int ath11k_mac_op_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
4515 {
4516 	struct ath11k *ar = hw->priv;
4517 	int ret;
4518 
4519 	mutex_lock(&ar->conf_mutex);
4520 	ret = __ath11k_set_antenna(ar, tx_ant, rx_ant);
4521 	mutex_unlock(&ar->conf_mutex);
4522 
4523 	return ret;
4524 }
4525 
4526 static int ath11k_mac_op_ampdu_action(struct ieee80211_hw *hw,
4527 				      struct ieee80211_vif *vif,
4528 				      struct ieee80211_ampdu_params *params)
4529 {
4530 	struct ath11k *ar = hw->priv;
4531 	int ret = -EINVAL;
4532 
4533 	mutex_lock(&ar->conf_mutex);
4534 
4535 	switch (params->action) {
4536 	case IEEE80211_AMPDU_RX_START:
4537 		ret = ath11k_dp_rx_ampdu_start(ar, params);
4538 		break;
4539 	case IEEE80211_AMPDU_RX_STOP:
4540 		ret = ath11k_dp_rx_ampdu_stop(ar, params);
4541 		break;
4542 	case IEEE80211_AMPDU_TX_START:
4543 	case IEEE80211_AMPDU_TX_STOP_CONT:
4544 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
4545 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
4546 	case IEEE80211_AMPDU_TX_OPERATIONAL:
4547 		/* Tx A-MPDU aggregation offloaded to hw/fw so deny mac80211
4548 		 * Tx aggregation requests.
4549 		 */
4550 		ret = -EOPNOTSUPP;
4551 		break;
4552 	}
4553 
4554 	mutex_unlock(&ar->conf_mutex);
4555 
4556 	return ret;
4557 }
4558 
4559 static int ath11k_mac_op_add_chanctx(struct ieee80211_hw *hw,
4560 				     struct ieee80211_chanctx_conf *ctx)
4561 {
4562 	struct ath11k *ar = hw->priv;
4563 	struct ath11k_base *ab = ar->ab;
4564 
4565 	ath11k_dbg(ab, ATH11K_DBG_MAC,
4566 		   "mac chanctx add freq %hu width %d ptr %pK\n",
4567 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
4568 
4569 	mutex_lock(&ar->conf_mutex);
4570 
4571 	spin_lock_bh(&ar->data_lock);
4572 	/* TODO: In case of multiple channel context, populate rx_channel from
4573 	 * Rx PPDU desc information.
4574 	 */
4575 	ar->rx_channel = ctx->def.chan;
4576 	spin_unlock_bh(&ar->data_lock);
4577 
4578 	mutex_unlock(&ar->conf_mutex);
4579 
4580 	return 0;
4581 }
4582 
4583 static void ath11k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
4584 					 struct ieee80211_chanctx_conf *ctx)
4585 {
4586 	struct ath11k *ar = hw->priv;
4587 	struct ath11k_base *ab = ar->ab;
4588 
4589 	ath11k_dbg(ab, ATH11K_DBG_MAC,
4590 		   "mac chanctx remove freq %hu width %d ptr %pK\n",
4591 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
4592 
4593 	mutex_lock(&ar->conf_mutex);
4594 
4595 	spin_lock_bh(&ar->data_lock);
4596 	/* TODO: In case of there is one more channel context left, populate
4597 	 * rx_channel with the channel of that remaining channel context.
4598 	 */
4599 	ar->rx_channel = NULL;
4600 	spin_unlock_bh(&ar->data_lock);
4601 
4602 	mutex_unlock(&ar->conf_mutex);
4603 }
4604 
4605 static inline int ath11k_mac_vdev_setup_sync(struct ath11k *ar)
4606 {
4607 	lockdep_assert_held(&ar->conf_mutex);
4608 
4609 	if (test_bit(ATH11K_FLAG_CRASH_FLUSH, &ar->ab->dev_flags))
4610 		return -ESHUTDOWN;
4611 
4612 	if (!wait_for_completion_timeout(&ar->vdev_setup_done,
4613 					 ATH11K_VDEV_SETUP_TIMEOUT_HZ))
4614 		return -ETIMEDOUT;
4615 
4616 	return ar->last_wmi_vdev_start_status ? -EINVAL : 0;
4617 }
4618 
4619 static int
4620 ath11k_mac_vdev_start_restart(struct ath11k_vif *arvif,
4621 			      const struct cfg80211_chan_def *chandef,
4622 			      bool restart)
4623 {
4624 	struct ath11k *ar = arvif->ar;
4625 	struct ath11k_base *ab = ar->ab;
4626 	struct wmi_vdev_start_req_arg arg = {};
4627 	int he_support = arvif->vif->bss_conf.he_support;
4628 	int ret = 0;
4629 
4630 	lockdep_assert_held(&ar->conf_mutex);
4631 
4632 	reinit_completion(&ar->vdev_setup_done);
4633 
4634 	arg.vdev_id = arvif->vdev_id;
4635 	arg.dtim_period = arvif->dtim_period;
4636 	arg.bcn_intval = arvif->beacon_interval;
4637 
4638 	arg.channel.freq = chandef->chan->center_freq;
4639 	arg.channel.band_center_freq1 = chandef->center_freq1;
4640 	arg.channel.band_center_freq2 = chandef->center_freq2;
4641 	arg.channel.mode =
4642 		ath11k_phymodes[chandef->chan->band][chandef->width];
4643 
4644 	arg.channel.min_power = 0;
4645 	arg.channel.max_power = chandef->chan->max_power * 2;
4646 	arg.channel.max_reg_power = chandef->chan->max_reg_power * 2;
4647 	arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
4648 
4649 	arg.pref_tx_streams = ar->num_tx_chains;
4650 	arg.pref_rx_streams = ar->num_rx_chains;
4651 
4652 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
4653 		arg.ssid = arvif->u.ap.ssid;
4654 		arg.ssid_len = arvif->u.ap.ssid_len;
4655 		arg.hidden_ssid = arvif->u.ap.hidden_ssid;
4656 
4657 		/* For now allow DFS for AP mode */
4658 		arg.channel.chan_radar =
4659 			!!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
4660 
4661 		arg.channel.passive = arg.channel.chan_radar;
4662 
4663 		spin_lock_bh(&ab->base_lock);
4664 		arg.regdomain = ar->ab->dfs_region;
4665 		spin_unlock_bh(&ab->base_lock);
4666 
4667 		/* TODO: Notify if secondary 80Mhz also needs radar detection */
4668 		if (he_support) {
4669 			ret = ath11k_set_he_mu_sounding_mode(ar, arvif);
4670 			if (ret) {
4671 				ath11k_warn(ar->ab, "failed to set he mode vdev %i\n",
4672 					    arg.vdev_id);
4673 				return ret;
4674 			}
4675 		}
4676 	}
4677 
4678 	arg.channel.passive |= !!(chandef->chan->flags & IEEE80211_CHAN_NO_IR);
4679 
4680 	ath11k_dbg(ab, ATH11K_DBG_MAC,
4681 		   "mac vdev %d start center_freq %d phymode %s\n",
4682 		   arg.vdev_id, arg.channel.freq,
4683 		   ath11k_wmi_phymode_str(arg.channel.mode));
4684 
4685 	ret = ath11k_wmi_vdev_start(ar, &arg, restart);
4686 	if (ret) {
4687 		ath11k_warn(ar->ab, "failed to %s WMI vdev %i\n",
4688 			    restart ? "restart" : "start", arg.vdev_id);
4689 		return ret;
4690 	}
4691 
4692 	ret = ath11k_mac_vdev_setup_sync(ar);
4693 	if (ret) {
4694 		ath11k_warn(ab, "failed to synchronize setup for vdev %i %s: %d\n",
4695 			    arg.vdev_id, restart ? "restart" : "start", ret);
4696 		return ret;
4697 	}
4698 
4699 	ar->num_started_vdevs++;
4700 	ath11k_dbg(ab, ATH11K_DBG_MAC,  "vdev %pM started, vdev_id %d\n",
4701 		   arvif->vif->addr, arvif->vdev_id);
4702 
4703 	/* Enable CAC Flag in the driver by checking the channel DFS cac time,
4704 	 * i.e dfs_cac_ms value which will be valid only for radar channels
4705 	 * and state as NL80211_DFS_USABLE which indicates CAC needs to be
4706 	 * done before channel usage. This flags is used to drop rx packets.
4707 	 * during CAC.
4708 	 */
4709 	/* TODO Set the flag for other interface types as required */
4710 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP &&
4711 	    chandef->chan->dfs_cac_ms &&
4712 	    chandef->chan->dfs_state == NL80211_DFS_USABLE) {
4713 		set_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
4714 		ath11k_dbg(ab, ATH11K_DBG_MAC,
4715 			   "CAC Started in chan_freq %d for vdev %d\n",
4716 			   arg.channel.freq, arg.vdev_id);
4717 	}
4718 
4719 	ret = ath11k_mac_set_txbf_conf(arvif);
4720 	if (ret)
4721 		ath11k_warn(ab, "failed to set txbf conf for vdev %d: %d\n",
4722 			    arvif->vdev_id, ret);
4723 
4724 	return 0;
4725 }
4726 
4727 static int ath11k_mac_vdev_stop(struct ath11k_vif *arvif)
4728 {
4729 	struct ath11k *ar = arvif->ar;
4730 	int ret;
4731 
4732 	lockdep_assert_held(&ar->conf_mutex);
4733 
4734 	reinit_completion(&ar->vdev_setup_done);
4735 
4736 	spin_lock_bh(&ar->data_lock);
4737 
4738 	ar->vdev_stop_status.stop_in_progress = true;
4739 	ar->vdev_stop_status.vdev_id = arvif->vdev_id;
4740 
4741 	spin_unlock_bh(&ar->data_lock);
4742 
4743 	ret = ath11k_wmi_vdev_stop(ar, arvif->vdev_id);
4744 	if (ret) {
4745 		ath11k_warn(ar->ab, "failed to stop WMI vdev %i: %d\n",
4746 			    arvif->vdev_id, ret);
4747 		goto err;
4748 	}
4749 
4750 	ret = ath11k_mac_vdev_setup_sync(ar);
4751 	if (ret) {
4752 		ath11k_warn(ar->ab, "failed to synchronize setup for vdev %i: %d\n",
4753 			    arvif->vdev_id, ret);
4754 		goto err;
4755 	}
4756 
4757 	WARN_ON(ar->num_started_vdevs == 0);
4758 
4759 	ar->num_started_vdevs--;
4760 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "vdev %pM stopped, vdev_id %d\n",
4761 		   arvif->vif->addr, arvif->vdev_id);
4762 
4763 	if (test_bit(ATH11K_CAC_RUNNING, &ar->dev_flags)) {
4764 		clear_bit(ATH11K_CAC_RUNNING, &ar->dev_flags);
4765 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "CAC Stopped for vdev %d\n",
4766 			   arvif->vdev_id);
4767 	}
4768 
4769 	return 0;
4770 err:
4771 	spin_lock_bh(&ar->data_lock);
4772 	ar->vdev_stop_status.stop_in_progress = false;
4773 	spin_unlock_bh(&ar->data_lock);
4774 
4775 	return ret;
4776 }
4777 
4778 static int ath11k_mac_vdev_start(struct ath11k_vif *arvif,
4779 				 const struct cfg80211_chan_def *chandef)
4780 {
4781 	return ath11k_mac_vdev_start_restart(arvif, chandef, false);
4782 }
4783 
4784 static int ath11k_mac_vdev_restart(struct ath11k_vif *arvif,
4785 				   const struct cfg80211_chan_def *chandef)
4786 {
4787 	return ath11k_mac_vdev_start_restart(arvif, chandef, true);
4788 }
4789 
4790 struct ath11k_mac_change_chanctx_arg {
4791 	struct ieee80211_chanctx_conf *ctx;
4792 	struct ieee80211_vif_chanctx_switch *vifs;
4793 	int n_vifs;
4794 	int next_vif;
4795 };
4796 
4797 static void
4798 ath11k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
4799 				   struct ieee80211_vif *vif)
4800 {
4801 	struct ath11k_mac_change_chanctx_arg *arg = data;
4802 
4803 	if (rcu_access_pointer(vif->chanctx_conf) != arg->ctx)
4804 		return;
4805 
4806 	arg->n_vifs++;
4807 }
4808 
4809 static void
4810 ath11k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
4811 				    struct ieee80211_vif *vif)
4812 {
4813 	struct ath11k_mac_change_chanctx_arg *arg = data;
4814 	struct ieee80211_chanctx_conf *ctx;
4815 
4816 	ctx = rcu_access_pointer(vif->chanctx_conf);
4817 	if (ctx != arg->ctx)
4818 		return;
4819 
4820 	if (WARN_ON(arg->next_vif == arg->n_vifs))
4821 		return;
4822 
4823 	arg->vifs[arg->next_vif].vif = vif;
4824 	arg->vifs[arg->next_vif].old_ctx = ctx;
4825 	arg->vifs[arg->next_vif].new_ctx = ctx;
4826 	arg->next_vif++;
4827 }
4828 
4829 static void
4830 ath11k_mac_update_vif_chan(struct ath11k *ar,
4831 			   struct ieee80211_vif_chanctx_switch *vifs,
4832 			   int n_vifs)
4833 {
4834 	struct ath11k_base *ab = ar->ab;
4835 	struct ath11k_vif *arvif;
4836 	int ret;
4837 	int i;
4838 
4839 	lockdep_assert_held(&ar->conf_mutex);
4840 
4841 	for (i = 0; i < n_vifs; i++) {
4842 		arvif = (void *)vifs[i].vif->drv_priv;
4843 
4844 		ath11k_dbg(ab, ATH11K_DBG_MAC,
4845 			   "mac chanctx switch vdev_id %i freq %hu->%hu width %d->%d\n",
4846 			   arvif->vdev_id,
4847 			   vifs[i].old_ctx->def.chan->center_freq,
4848 			   vifs[i].new_ctx->def.chan->center_freq,
4849 			   vifs[i].old_ctx->def.width,
4850 			   vifs[i].new_ctx->def.width);
4851 
4852 		if (WARN_ON(!arvif->is_started))
4853 			continue;
4854 
4855 		if (WARN_ON(!arvif->is_up))
4856 			continue;
4857 
4858 		ret = ath11k_wmi_vdev_down(ar, arvif->vdev_id);
4859 		if (ret) {
4860 			ath11k_warn(ab, "failed to down vdev %d: %d\n",
4861 				    arvif->vdev_id, ret);
4862 			continue;
4863 		}
4864 	}
4865 
4866 	/* All relevant vdevs are downed and associated channel resources
4867 	 * should be available for the channel switch now.
4868 	 */
4869 
4870 	/* TODO: Update ar->rx_channel */
4871 
4872 	for (i = 0; i < n_vifs; i++) {
4873 		arvif = (void *)vifs[i].vif->drv_priv;
4874 
4875 		if (WARN_ON(!arvif->is_started))
4876 			continue;
4877 
4878 		if (WARN_ON(!arvif->is_up))
4879 			continue;
4880 
4881 		ret = ath11k_mac_setup_bcn_tmpl(arvif);
4882 		if (ret)
4883 			ath11k_warn(ab, "failed to update bcn tmpl during csa: %d\n",
4884 				    ret);
4885 
4886 		ret = ath11k_mac_vdev_restart(arvif, &vifs[i].new_ctx->def);
4887 		if (ret) {
4888 			ath11k_warn(ab, "failed to restart vdev %d: %d\n",
4889 				    arvif->vdev_id, ret);
4890 			continue;
4891 		}
4892 
4893 		ret = ath11k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
4894 					 arvif->bssid);
4895 		if (ret) {
4896 			ath11k_warn(ab, "failed to bring vdev up %d: %d\n",
4897 				    arvif->vdev_id, ret);
4898 			continue;
4899 		}
4900 	}
4901 }
4902 
4903 static void
4904 ath11k_mac_update_active_vif_chan(struct ath11k *ar,
4905 				  struct ieee80211_chanctx_conf *ctx)
4906 {
4907 	struct ath11k_mac_change_chanctx_arg arg = { .ctx = ctx };
4908 
4909 	lockdep_assert_held(&ar->conf_mutex);
4910 
4911 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
4912 						   IEEE80211_IFACE_ITER_NORMAL,
4913 						   ath11k_mac_change_chanctx_cnt_iter,
4914 						   &arg);
4915 	if (arg.n_vifs == 0)
4916 		return;
4917 
4918 	arg.vifs = kcalloc(arg.n_vifs, sizeof(arg.vifs[0]), GFP_KERNEL);
4919 	if (!arg.vifs)
4920 		return;
4921 
4922 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
4923 						   IEEE80211_IFACE_ITER_NORMAL,
4924 						   ath11k_mac_change_chanctx_fill_iter,
4925 						   &arg);
4926 
4927 	ath11k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
4928 
4929 	kfree(arg.vifs);
4930 }
4931 
4932 static void ath11k_mac_op_change_chanctx(struct ieee80211_hw *hw,
4933 					 struct ieee80211_chanctx_conf *ctx,
4934 					 u32 changed)
4935 {
4936 	struct ath11k *ar = hw->priv;
4937 	struct ath11k_base *ab = ar->ab;
4938 
4939 	mutex_lock(&ar->conf_mutex);
4940 
4941 	ath11k_dbg(ab, ATH11K_DBG_MAC,
4942 		   "mac chanctx change freq %hu width %d ptr %pK changed %x\n",
4943 		   ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
4944 
4945 	/* This shouldn't really happen because channel switching should use
4946 	 * switch_vif_chanctx().
4947 	 */
4948 	if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
4949 		goto unlock;
4950 
4951 	if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH)
4952 		ath11k_mac_update_active_vif_chan(ar, ctx);
4953 
4954 	/* TODO: Recalc radar detection */
4955 
4956 unlock:
4957 	mutex_unlock(&ar->conf_mutex);
4958 }
4959 
4960 static int
4961 ath11k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
4962 				 struct ieee80211_vif *vif,
4963 				 struct ieee80211_chanctx_conf *ctx)
4964 {
4965 	struct ath11k *ar = hw->priv;
4966 	struct ath11k_base *ab = ar->ab;
4967 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
4968 	int ret;
4969 
4970 	mutex_lock(&ar->conf_mutex);
4971 
4972 	ath11k_dbg(ab, ATH11K_DBG_MAC,
4973 		   "mac chanctx assign ptr %pK vdev_id %i\n",
4974 		   ctx, arvif->vdev_id);
4975 
4976 	if (WARN_ON(arvif->is_started)) {
4977 		mutex_unlock(&ar->conf_mutex);
4978 		return -EBUSY;
4979 	}
4980 
4981 	ret = ath11k_mac_vdev_start(arvif, &ctx->def);
4982 	if (ret) {
4983 		ath11k_warn(ab, "failed to start vdev %i addr %pM on freq %d: %d\n",
4984 			    arvif->vdev_id, vif->addr,
4985 			    ctx->def.chan->center_freq, ret);
4986 		goto err;
4987 	}
4988 	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR) {
4989 		ret = ath11k_monitor_vdev_up(ar, arvif->vdev_id);
4990 		if (ret)
4991 			goto err;
4992 	}
4993 
4994 	arvif->is_started = true;
4995 
4996 	/* TODO: Setup ps and cts/rts protection */
4997 
4998 	mutex_unlock(&ar->conf_mutex);
4999 
5000 	return 0;
5001 
5002 err:
5003 	mutex_unlock(&ar->conf_mutex);
5004 
5005 	return ret;
5006 }
5007 
5008 static void
5009 ath11k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
5010 				   struct ieee80211_vif *vif,
5011 				   struct ieee80211_chanctx_conf *ctx)
5012 {
5013 	struct ath11k *ar = hw->priv;
5014 	struct ath11k_base *ab = ar->ab;
5015 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
5016 	int ret;
5017 
5018 	mutex_lock(&ar->conf_mutex);
5019 
5020 	ath11k_dbg(ab, ATH11K_DBG_MAC,
5021 		   "mac chanctx unassign ptr %pK vdev_id %i\n",
5022 		   ctx, arvif->vdev_id);
5023 
5024 	WARN_ON(!arvif->is_started);
5025 
5026 	ret = ath11k_mac_vdev_stop(arvif);
5027 	if (ret)
5028 		ath11k_warn(ab, "failed to stop vdev %i: %d\n",
5029 			    arvif->vdev_id, ret);
5030 
5031 	arvif->is_started = false;
5032 
5033 	mutex_unlock(&ar->conf_mutex);
5034 }
5035 
5036 static int
5037 ath11k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
5038 				 struct ieee80211_vif_chanctx_switch *vifs,
5039 				 int n_vifs,
5040 				 enum ieee80211_chanctx_switch_mode mode)
5041 {
5042 	struct ath11k *ar = hw->priv;
5043 
5044 	mutex_lock(&ar->conf_mutex);
5045 
5046 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
5047 		   "mac chanctx switch n_vifs %d mode %d\n",
5048 		   n_vifs, mode);
5049 	ath11k_mac_update_vif_chan(ar, vifs, n_vifs);
5050 
5051 	mutex_unlock(&ar->conf_mutex);
5052 
5053 	return 0;
5054 }
5055 
5056 static int
5057 ath11k_set_vdev_param_to_all_vifs(struct ath11k *ar, int param, u32 value)
5058 {
5059 	struct ath11k_vif *arvif;
5060 	int ret = 0;
5061 
5062 	mutex_lock(&ar->conf_mutex);
5063 	list_for_each_entry(arvif, &ar->arvifs, list) {
5064 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "setting mac vdev %d param %d value %d\n",
5065 			   param, arvif->vdev_id, value);
5066 
5067 		ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5068 						    param, value);
5069 		if (ret) {
5070 			ath11k_warn(ar->ab, "failed to set param %d for vdev %d: %d\n",
5071 				    param, arvif->vdev_id, ret);
5072 			break;
5073 		}
5074 	}
5075 	mutex_unlock(&ar->conf_mutex);
5076 	return ret;
5077 }
5078 
5079 /* mac80211 stores device specific RTS/Fragmentation threshold value,
5080  * this is set interface specific to firmware from ath11k driver
5081  */
5082 static int ath11k_mac_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
5083 {
5084 	struct ath11k *ar = hw->priv;
5085 	int param_id = WMI_VDEV_PARAM_RTS_THRESHOLD;
5086 
5087 	return ath11k_set_vdev_param_to_all_vifs(ar, param_id, value);
5088 }
5089 
5090 static int ath11k_mac_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
5091 {
5092 	/* Even though there's a WMI vdev param for fragmentation threshold no
5093 	 * known firmware actually implements it. Moreover it is not possible to
5094 	 * rely frame fragmentation to mac80211 because firmware clears the
5095 	 * "more fragments" bit in frame control making it impossible for remote
5096 	 * devices to reassemble frames.
5097 	 *
5098 	 * Hence implement a dummy callback just to say fragmentation isn't
5099 	 * supported. This effectively prevents mac80211 from doing frame
5100 	 * fragmentation in software.
5101 	 */
5102 	return -EOPNOTSUPP;
5103 }
5104 
5105 static void ath11k_mac_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5106 				u32 queues, bool drop)
5107 {
5108 	struct ath11k *ar = hw->priv;
5109 	long time_left;
5110 
5111 	if (drop)
5112 		return;
5113 
5114 	time_left = wait_event_timeout(ar->dp.tx_empty_waitq,
5115 				       (atomic_read(&ar->dp.num_tx_pending) == 0),
5116 				       ATH11K_FLUSH_TIMEOUT);
5117 	if (time_left == 0)
5118 		ath11k_warn(ar->ab, "failed to flush transmit queue %ld\n", time_left);
5119 }
5120 
5121 static int
5122 ath11k_mac_bitrate_mask_num_ht_rates(struct ath11k *ar,
5123 				     enum nl80211_band band,
5124 				     const struct cfg80211_bitrate_mask *mask)
5125 {
5126 	int num_rates = 0;
5127 	int i;
5128 
5129 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
5130 		num_rates += hweight16(mask->control[band].ht_mcs[i]);
5131 
5132 	return num_rates;
5133 }
5134 
5135 static bool
5136 ath11k_mac_has_single_legacy_rate(struct ath11k *ar,
5137 				  enum nl80211_band band,
5138 				  const struct cfg80211_bitrate_mask *mask)
5139 {
5140 	int num_rates = 0;
5141 
5142 	num_rates = hweight32(mask->control[band].legacy);
5143 
5144 	if (ath11k_mac_bitrate_mask_num_ht_rates(ar, band, mask))
5145 		return false;
5146 
5147 	if (ath11k_mac_bitrate_mask_num_vht_rates(ar, band, mask))
5148 		return false;
5149 
5150 	return num_rates == 1;
5151 }
5152 
5153 static bool
5154 ath11k_mac_bitrate_mask_get_single_nss(struct ath11k *ar,
5155 				       enum nl80211_band band,
5156 				       const struct cfg80211_bitrate_mask *mask,
5157 				       int *nss)
5158 {
5159 	struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
5160 	u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
5161 	u8 ht_nss_mask = 0;
5162 	u8 vht_nss_mask = 0;
5163 	int i;
5164 
5165 	/* No need to consider legacy here. Basic rates are always present
5166 	 * in bitrate mask
5167 	 */
5168 
5169 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
5170 		if (mask->control[band].ht_mcs[i] == 0)
5171 			continue;
5172 		else if (mask->control[band].ht_mcs[i] ==
5173 			 sband->ht_cap.mcs.rx_mask[i])
5174 			ht_nss_mask |= BIT(i);
5175 		else
5176 			return false;
5177 	}
5178 
5179 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
5180 		if (mask->control[band].vht_mcs[i] == 0)
5181 			continue;
5182 		else if (mask->control[band].vht_mcs[i] ==
5183 			 ath11k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
5184 			vht_nss_mask |= BIT(i);
5185 		else
5186 			return false;
5187 	}
5188 
5189 	if (ht_nss_mask != vht_nss_mask)
5190 		return false;
5191 
5192 	if (ht_nss_mask == 0)
5193 		return false;
5194 
5195 	if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
5196 		return false;
5197 
5198 	*nss = fls(ht_nss_mask);
5199 
5200 	return true;
5201 }
5202 
5203 static int
5204 ath11k_mac_get_single_legacy_rate(struct ath11k *ar,
5205 				  enum nl80211_band band,
5206 				  const struct cfg80211_bitrate_mask *mask,
5207 				  u32 *rate, u8 *nss)
5208 {
5209 	int rate_idx;
5210 	u16 bitrate;
5211 	u8 preamble;
5212 	u8 hw_rate;
5213 
5214 	if (hweight32(mask->control[band].legacy) != 1)
5215 		return -EINVAL;
5216 
5217 	rate_idx = ffs(mask->control[band].legacy) - 1;
5218 
5219 	if (band == NL80211_BAND_5GHZ)
5220 		rate_idx += ATH11K_MAC_FIRST_OFDM_RATE_IDX;
5221 
5222 	hw_rate = ath11k_legacy_rates[rate_idx].hw_value;
5223 	bitrate = ath11k_legacy_rates[rate_idx].bitrate;
5224 
5225 	if (ath11k_mac_bitrate_is_cck(bitrate))
5226 		preamble = WMI_RATE_PREAMBLE_CCK;
5227 	else
5228 		preamble = WMI_RATE_PREAMBLE_OFDM;
5229 
5230 	*nss = 1;
5231 	*rate = ATH11K_HW_RATE_CODE(hw_rate, 0, preamble);
5232 
5233 	return 0;
5234 }
5235 
5236 static int ath11k_mac_set_fixed_rate_params(struct ath11k_vif *arvif,
5237 					    u32 rate, u8 nss, u8 sgi, u8 ldpc)
5238 {
5239 	struct ath11k *ar = arvif->ar;
5240 	u32 vdev_param;
5241 	int ret;
5242 
5243 	lockdep_assert_held(&ar->conf_mutex);
5244 
5245 	ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac set fixed rate params vdev %i rate 0x%02hhx nss %hhu sgi %hhu\n",
5246 		   arvif->vdev_id, rate, nss, sgi);
5247 
5248 	vdev_param = WMI_VDEV_PARAM_FIXED_RATE;
5249 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5250 					    vdev_param, rate);
5251 	if (ret) {
5252 		ath11k_warn(ar->ab, "failed to set fixed rate param 0x%02x: %d\n",
5253 			    rate, ret);
5254 		return ret;
5255 	}
5256 
5257 	vdev_param = WMI_VDEV_PARAM_NSS;
5258 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5259 					    vdev_param, nss);
5260 	if (ret) {
5261 		ath11k_warn(ar->ab, "failed to set nss param %d: %d\n",
5262 			    nss, ret);
5263 		return ret;
5264 	}
5265 
5266 	vdev_param = WMI_VDEV_PARAM_SGI;
5267 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5268 					    vdev_param, sgi);
5269 	if (ret) {
5270 		ath11k_warn(ar->ab, "failed to set sgi param %d: %d\n",
5271 			    sgi, ret);
5272 		return ret;
5273 	}
5274 
5275 	vdev_param = WMI_VDEV_PARAM_LDPC;
5276 	ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
5277 					    vdev_param, ldpc);
5278 	if (ret) {
5279 		ath11k_warn(ar->ab, "failed to set ldpc param %d: %d\n",
5280 			    ldpc, ret);
5281 		return ret;
5282 	}
5283 
5284 	return 0;
5285 }
5286 
5287 static bool
5288 ath11k_mac_vht_mcs_range_present(struct ath11k *ar,
5289 				 enum nl80211_band band,
5290 				 const struct cfg80211_bitrate_mask *mask)
5291 {
5292 	int i;
5293 	u16 vht_mcs;
5294 
5295 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
5296 		vht_mcs = mask->control[band].vht_mcs[i];
5297 
5298 		switch (vht_mcs) {
5299 		case 0:
5300 		case BIT(8) - 1:
5301 		case BIT(9) - 1:
5302 		case BIT(10) - 1:
5303 			break;
5304 		default:
5305 			return false;
5306 		}
5307 	}
5308 
5309 	return true;
5310 }
5311 
5312 static void ath11k_mac_set_bitrate_mask_iter(void *data,
5313 					     struct ieee80211_sta *sta)
5314 {
5315 	struct ath11k_vif *arvif = data;
5316 	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
5317 	struct ath11k *ar = arvif->ar;
5318 
5319 	spin_lock_bh(&ar->data_lock);
5320 	arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
5321 	spin_unlock_bh(&ar->data_lock);
5322 
5323 	ieee80211_queue_work(ar->hw, &arsta->update_wk);
5324 }
5325 
5326 static void ath11k_mac_disable_peer_fixed_rate(void *data,
5327 					       struct ieee80211_sta *sta)
5328 {
5329 	struct ath11k_vif *arvif = data;
5330 	struct ath11k *ar = arvif->ar;
5331 	int ret;
5332 
5333 	ret = ath11k_wmi_set_peer_param(ar, sta->addr,
5334 					arvif->vdev_id,
5335 					WMI_PEER_PARAM_FIXED_RATE,
5336 					WMI_FIXED_RATE_NONE);
5337 	if (ret)
5338 		ath11k_warn(ar->ab,
5339 			    "failed to disable peer fixed rate for STA %pM ret %d\n",
5340 			    sta->addr, ret);
5341 }
5342 
5343 static int
5344 ath11k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
5345 			       struct ieee80211_vif *vif,
5346 			       const struct cfg80211_bitrate_mask *mask)
5347 {
5348 	struct ath11k_vif *arvif = (void *)vif->drv_priv;
5349 	struct cfg80211_chan_def def;
5350 	struct ath11k *ar = arvif->ar;
5351 	enum nl80211_band band;
5352 	const u8 *ht_mcs_mask;
5353 	const u16 *vht_mcs_mask;
5354 	u32 rate;
5355 	u8 nss;
5356 	u8 sgi;
5357 	u8 ldpc;
5358 	int single_nss;
5359 	int ret;
5360 	int num_rates;
5361 
5362 	if (ath11k_mac_vif_chan(vif, &def))
5363 		return -EPERM;
5364 
5365 	band = def.chan->band;
5366 	ht_mcs_mask = mask->control[band].ht_mcs;
5367 	vht_mcs_mask = mask->control[band].vht_mcs;
5368 	ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
5369 
5370 	sgi = mask->control[band].gi;
5371 	if (sgi == NL80211_TXRATE_FORCE_LGI)
5372 		return -EINVAL;
5373 
5374 	/* mac80211 doesn't support sending a fixed HT/VHT MCS alone, rather it
5375 	 * requires passing atleast one of used basic rates along with them.
5376 	 * Fixed rate setting across different preambles(legacy, HT, VHT) is
5377 	 * not supported by the FW. Hence use of FIXED_RATE vdev param is not
5378 	 * suitable for setting single HT/VHT rates.
5379 	 * But, there could be a single basic rate passed from userspace which
5380 	 * can be done through the FIXED_RATE param.
5381 	 */
5382 	if (ath11k_mac_has_single_legacy_rate(ar, band, mask)) {
5383 		ret = ath11k_mac_get_single_legacy_rate(ar, band, mask, &rate,
5384 							&nss);
5385 		if (ret) {
5386 			ath11k_warn(ar->ab, "failed to get single legacy rate for vdev %i: %d\n",
5387 				    arvif->vdev_id, ret);
5388 			return ret;
5389 		}
5390 		ieee80211_iterate_stations_atomic(ar->hw,
5391 						  ath11k_mac_disable_peer_fixed_rate,
5392 						  arvif);
5393 	} else if (ath11k_mac_bitrate_mask_get_single_nss(ar, band, mask,
5394 							  &single_nss)) {
5395 		rate = WMI_FIXED_RATE_NONE;
5396 		nss = single_nss;
5397 	} else {
5398 		rate = WMI_FIXED_RATE_NONE;
5399 		nss = min_t(u32, ar->num_tx_chains,
5400 			    max(ath11k_mac_max_ht_nss(ht_mcs_mask),
5401 				ath11k_mac_max_vht_nss(vht_mcs_mask)));
5402 
5403 		/* If multiple rates across different preambles are given
5404 		 * we can reconfigure this info with all peers using PEER_ASSOC
5405 		 * command with the below exception cases.
5406 		 * - Single VHT Rate : peer_assoc command accommodates only MCS
5407 		 * range values i.e 0-7, 0-8, 0-9 for VHT. Though mac80211
5408 		 * mandates passing basic rates along with HT/VHT rates, FW
5409 		 * doesn't allow switching from VHT to Legacy. Hence instead of
5410 		 * setting legacy and VHT rates using RATEMASK_CMD vdev cmd,
5411 		 * we could set this VHT rate as peer fixed rate param, which
5412 		 * will override FIXED rate and FW rate control algorithm.
5413 		 * If single VHT rate is passed along with HT rates, we select
5414 		 * the VHT rate as fixed rate for vht peers.
5415 		 * - Multiple VHT Rates : When Multiple VHT rates are given,this
5416 		 * can be set using RATEMASK CMD which uses FW rate-ctl alg.
5417 		 * TODO: Setting multiple VHT MCS and replacing peer_assoc with
5418 		 * RATEMASK_CMDID can cover all use cases of setting rates
5419 		 * across multiple preambles and rates within same type.
5420 		 * But requires more validation of the command at this point.
5421 		 */
5422 
5423 		num_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band,
5424 								  mask);
5425 
5426 		if (!ath11k_mac_vht_mcs_range_present(ar, band, mask) &&
5427 		    num_rates > 1) {
5428 			/* TODO: Handle multiple VHT MCS values setting using
5429 			 * RATEMASK CMD
5430 			 */
5431 			ath11k_warn(ar->ab,
5432 				    "Setting more than one MCS Value in bitrate mask not supported\n");
5433 			return -EINVAL;
5434 		}
5435 
5436 		ieee80211_iterate_stations_atomic(ar->hw,
5437 						  ath11k_mac_disable_peer_fixed_rate,
5438 						  arvif);
5439 
5440 		mutex_lock(&ar->conf_mutex);
5441 
5442 		arvif->bitrate_mask = *mask;
5443 		ieee80211_iterate_stations_atomic(ar->hw,
5444 						  ath11k_mac_set_bitrate_mask_iter,
5445 						  arvif);
5446 
5447 		mutex_unlock(&ar->conf_mutex);
5448 	}
5449 
5450 	mutex_lock(&ar->conf_mutex);
5451 
5452 	ret = ath11k_mac_set_fixed_rate_params(arvif, rate, nss, sgi, ldpc);
5453 	if (ret) {
5454 		ath11k_warn(ar->ab, "failed to set fixed rate params on vdev %i: %d\n",
5455 			    arvif->vdev_id, ret);
5456 	}
5457 
5458 	mutex_unlock(&ar->conf_mutex);
5459 
5460 	return ret;
5461 }
5462 
5463 static void
5464 ath11k_mac_op_reconfig_complete(struct ieee80211_hw *hw,
5465 				enum ieee80211_reconfig_type reconfig_type)
5466 {
5467 	struct ath11k *ar = hw->priv;
5468 
5469 	if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
5470 		return;
5471 
5472 	mutex_lock(&ar->conf_mutex);
5473 
5474 	if (ar->state == ATH11K_STATE_RESTARTED) {
5475 		ath11k_warn(ar->ab, "pdev %d successfully recovered\n",
5476 			    ar->pdev->pdev_id);
5477 		ar->state = ATH11K_STATE_ON;
5478 		ieee80211_wake_queues(ar->hw);
5479 	}
5480 
5481 	mutex_unlock(&ar->conf_mutex);
5482 }
5483 
5484 static void
5485 ath11k_mac_update_bss_chan_survey(struct ath11k *ar,
5486 				  struct ieee80211_channel *channel)
5487 {
5488 	int ret;
5489 	enum wmi_bss_chan_info_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
5490 
5491 	lockdep_assert_held(&ar->conf_mutex);
5492 
5493 	if (!test_bit(WMI_TLV_SERVICE_BSS_CHANNEL_INFO_64, ar->ab->wmi_ab.svc_map) ||
5494 	    ar->rx_channel != channel)
5495 		return;
5496 
5497 	if (ar->scan.state != ATH11K_SCAN_IDLE) {
5498 		ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
5499 			   "ignoring bss chan info req while scanning..\n");
5500 		return;
5501 	}
5502 
5503 	reinit_completion(&ar->bss_survey_done);
5504 
5505 	ret = ath11k_wmi_pdev_bss_chan_info_request(ar, type);
5506 	if (ret) {
5507 		ath11k_warn(ar->ab, "failed to send pdev bss chan info request\n");
5508 		return;
5509 	}
5510 
5511 	ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
5512 	if (ret == 0)
5513 		ath11k_warn(ar->ab, "bss channel survey timed out\n");
5514 }
5515 
5516 static int ath11k_mac_op_get_survey(struct ieee80211_hw *hw, int idx,
5517 				    struct survey_info *survey)
5518 {
5519 	struct ath11k *ar = hw->priv;
5520 	struct ieee80211_supported_band *sband;
5521 	struct survey_info *ar_survey;
5522 	int ret = 0;
5523 
5524 	if (idx >= ATH11K_NUM_CHANS)
5525 		return -ENOENT;
5526 
5527 	ar_survey = &ar->survey[idx];
5528 
5529 	mutex_lock(&ar->conf_mutex);
5530 
5531 	sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
5532 	if (sband && idx >= sband->n_channels) {
5533 		idx -= sband->n_channels;
5534 		sband = NULL;
5535 	}
5536 
5537 	if (!sband)
5538 		sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
5539 
5540 	if (!sband || idx >= sband->n_channels) {
5541 		ret = -ENOENT;
5542 		goto exit;
5543 	}
5544 
5545 	ath11k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
5546 
5547 	spin_lock_bh(&ar->data_lock);
5548 	memcpy(survey, ar_survey, sizeof(*survey));
5549 	spin_unlock_bh(&ar->data_lock);
5550 
5551 	survey->channel = &sband->channels[idx];
5552 
5553 	if (ar->rx_channel == survey->channel)
5554 		survey->filled |= SURVEY_INFO_IN_USE;
5555 
5556 exit:
5557 	mutex_unlock(&ar->conf_mutex);
5558 	return ret;
5559 }
5560 
5561 static void ath11k_mac_op_sta_statistics(struct ieee80211_hw *hw,
5562 					 struct ieee80211_vif *vif,
5563 					 struct ieee80211_sta *sta,
5564 					 struct station_info *sinfo)
5565 {
5566 	struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
5567 
5568 	sinfo->rx_duration = arsta->rx_duration;
5569 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
5570 
5571 	sinfo->tx_duration = arsta->tx_duration;
5572 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_DURATION);
5573 
5574 	if (!arsta->txrate.legacy && !arsta->txrate.nss)
5575 		return;
5576 
5577 	if (arsta->txrate.legacy) {
5578 		sinfo->txrate.legacy = arsta->txrate.legacy;
5579 	} else {
5580 		sinfo->txrate.mcs = arsta->txrate.mcs;
5581 		sinfo->txrate.nss = arsta->txrate.nss;
5582 		sinfo->txrate.bw = arsta->txrate.bw;
5583 		sinfo->txrate.he_gi = arsta->txrate.he_gi;
5584 		sinfo->txrate.he_dcm = arsta->txrate.he_dcm;
5585 		sinfo->txrate.he_ru_alloc = arsta->txrate.he_ru_alloc;
5586 	}
5587 	sinfo->txrate.flags = arsta->txrate.flags;
5588 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
5589 
5590 	/* TODO: Use real NF instead of default one. */
5591 	sinfo->signal = arsta->rssi_comb + ATH11K_DEFAULT_NOISE_FLOOR;
5592 }
5593 
5594 static const struct ieee80211_ops ath11k_ops = {
5595 	.tx				= ath11k_mac_op_tx,
5596 	.start                          = ath11k_mac_op_start,
5597 	.stop                           = ath11k_mac_op_stop,
5598 	.reconfig_complete              = ath11k_mac_op_reconfig_complete,
5599 	.add_interface                  = ath11k_mac_op_add_interface,
5600 	.remove_interface		= ath11k_mac_op_remove_interface,
5601 	.config                         = ath11k_mac_op_config,
5602 	.bss_info_changed               = ath11k_mac_op_bss_info_changed,
5603 	.configure_filter		= ath11k_mac_op_configure_filter,
5604 	.hw_scan                        = ath11k_mac_op_hw_scan,
5605 	.cancel_hw_scan                 = ath11k_mac_op_cancel_hw_scan,
5606 	.set_key                        = ath11k_mac_op_set_key,
5607 	.sta_state                      = ath11k_mac_op_sta_state,
5608 	.sta_set_txpwr			= ath11k_mac_op_sta_set_txpwr,
5609 	.sta_rc_update			= ath11k_mac_op_sta_rc_update,
5610 	.conf_tx                        = ath11k_mac_op_conf_tx,
5611 	.set_antenna			= ath11k_mac_op_set_antenna,
5612 	.get_antenna			= ath11k_mac_op_get_antenna,
5613 	.ampdu_action			= ath11k_mac_op_ampdu_action,
5614 	.add_chanctx			= ath11k_mac_op_add_chanctx,
5615 	.remove_chanctx			= ath11k_mac_op_remove_chanctx,
5616 	.change_chanctx			= ath11k_mac_op_change_chanctx,
5617 	.assign_vif_chanctx		= ath11k_mac_op_assign_vif_chanctx,
5618 	.unassign_vif_chanctx		= ath11k_mac_op_unassign_vif_chanctx,
5619 	.switch_vif_chanctx		= ath11k_mac_op_switch_vif_chanctx,
5620 	.set_rts_threshold		= ath11k_mac_op_set_rts_threshold,
5621 	.set_frag_threshold		= ath11k_mac_op_set_frag_threshold,
5622 	.set_bitrate_mask		= ath11k_mac_op_set_bitrate_mask,
5623 	.get_survey			= ath11k_mac_op_get_survey,
5624 	.flush				= ath11k_mac_op_flush,
5625 	.sta_statistics			= ath11k_mac_op_sta_statistics,
5626 	CFG80211_TESTMODE_CMD(ath11k_tm_cmd)
5627 #ifdef CONFIG_ATH11K_DEBUGFS
5628 	.sta_add_debugfs		= ath11k_sta_add_debugfs,
5629 #endif
5630 };
5631 
5632 static const struct ieee80211_iface_limit ath11k_if_limits[] = {
5633 	{
5634 		.max = 1,
5635 		.types = BIT(NL80211_IFTYPE_STATION),
5636 	},
5637 	{
5638 		.max    = 16,
5639 		.types  = BIT(NL80211_IFTYPE_AP)
5640 #ifdef CONFIG_MAC80211_MESH
5641 			| BIT(NL80211_IFTYPE_MESH_POINT)
5642 #endif
5643 	},
5644 };
5645 
5646 static const struct ieee80211_iface_combination ath11k_if_comb[] = {
5647 	{
5648 		.limits = ath11k_if_limits,
5649 		.n_limits = ARRAY_SIZE(ath11k_if_limits),
5650 		.max_interfaces = 16,
5651 		.num_different_channels = 1,
5652 		.beacon_int_infra_match = true,
5653 		.beacon_int_min_gcd = 100,
5654 		.radar_detect_widths =	BIT(NL80211_CHAN_WIDTH_20_NOHT) |
5655 					BIT(NL80211_CHAN_WIDTH_20) |
5656 					BIT(NL80211_CHAN_WIDTH_40) |
5657 					BIT(NL80211_CHAN_WIDTH_80),
5658 	},
5659 };
5660 
5661 static void ath11k_mac_update_ch_list(struct ath11k *ar,
5662 				      struct ieee80211_supported_band *band,
5663 				      u32 freq_low, u32 freq_high)
5664 {
5665 	int i;
5666 
5667 	if (!(freq_low && freq_high))
5668 		return;
5669 
5670 	for (i = 0; i < band->n_channels; i++) {
5671 		if (band->channels[i].center_freq < freq_low ||
5672 		    band->channels[i].center_freq > freq_high)
5673 			band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
5674 	}
5675 }
5676 
5677 static int ath11k_mac_setup_channels_rates(struct ath11k *ar,
5678 					   u32 supported_bands)
5679 {
5680 	struct ieee80211_supported_band *band;
5681 	struct ath11k_hal_reg_capabilities_ext *reg_cap;
5682 	void *channels;
5683 
5684 	BUILD_BUG_ON((ARRAY_SIZE(ath11k_2ghz_channels) +
5685 		      ARRAY_SIZE(ath11k_5ghz_channels)) !=
5686 		     ATH11K_NUM_CHANS);
5687 
5688 	reg_cap = &ar->ab->hal_reg_cap[ar->pdev_idx];
5689 
5690 	if (supported_bands & WMI_HOST_WLAN_2G_CAP) {
5691 		channels = kmemdup(ath11k_2ghz_channels,
5692 				   sizeof(ath11k_2ghz_channels),
5693 				   GFP_KERNEL);
5694 		if (!channels)
5695 			return -ENOMEM;
5696 
5697 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
5698 		band->n_channels = ARRAY_SIZE(ath11k_2ghz_channels);
5699 		band->channels = channels;
5700 		band->n_bitrates = ath11k_g_rates_size;
5701 		band->bitrates = ath11k_g_rates;
5702 		ar->hw->wiphy->bands[NL80211_BAND_2GHZ] = band;
5703 		ath11k_mac_update_ch_list(ar, band,
5704 					  reg_cap->low_2ghz_chan,
5705 					  reg_cap->high_2ghz_chan);
5706 	}
5707 
5708 	if (supported_bands & WMI_HOST_WLAN_5G_CAP) {
5709 		channels = kmemdup(ath11k_5ghz_channels,
5710 				   sizeof(ath11k_5ghz_channels),
5711 				   GFP_KERNEL);
5712 		if (!channels) {
5713 			kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
5714 			return -ENOMEM;
5715 		}
5716 
5717 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
5718 		band->n_channels = ARRAY_SIZE(ath11k_5ghz_channels);
5719 		band->channels = channels;
5720 		band->n_bitrates = ath11k_a_rates_size;
5721 		band->bitrates = ath11k_a_rates;
5722 		ar->hw->wiphy->bands[NL80211_BAND_5GHZ] = band;
5723 		ath11k_mac_update_ch_list(ar, band,
5724 					  reg_cap->low_5ghz_chan,
5725 					  reg_cap->high_5ghz_chan);
5726 	}
5727 
5728 	return 0;
5729 }
5730 
5731 static const u8 ath11k_if_types_ext_capa[] = {
5732 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
5733 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
5734 };
5735 
5736 static const u8 ath11k_if_types_ext_capa_sta[] = {
5737 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
5738 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
5739 	[9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
5740 };
5741 
5742 static const u8 ath11k_if_types_ext_capa_ap[] = {
5743 	[0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
5744 	[7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
5745 	[9] = WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT,
5746 };
5747 
5748 static const struct wiphy_iftype_ext_capab ath11k_iftypes_ext_capa[] = {
5749 	{
5750 		.extended_capabilities = ath11k_if_types_ext_capa,
5751 		.extended_capabilities_mask = ath11k_if_types_ext_capa,
5752 		.extended_capabilities_len = sizeof(ath11k_if_types_ext_capa),
5753 	}, {
5754 		.iftype = NL80211_IFTYPE_STATION,
5755 		.extended_capabilities = ath11k_if_types_ext_capa_sta,
5756 		.extended_capabilities_mask = ath11k_if_types_ext_capa_sta,
5757 		.extended_capabilities_len =
5758 				sizeof(ath11k_if_types_ext_capa_sta),
5759 	}, {
5760 		.iftype = NL80211_IFTYPE_AP,
5761 		.extended_capabilities = ath11k_if_types_ext_capa_ap,
5762 		.extended_capabilities_mask = ath11k_if_types_ext_capa_ap,
5763 		.extended_capabilities_len =
5764 				sizeof(ath11k_if_types_ext_capa_ap),
5765 	},
5766 };
5767 
5768 static void __ath11k_mac_unregister(struct ath11k *ar)
5769 {
5770 	cancel_work_sync(&ar->regd_update_work);
5771 
5772 	ieee80211_unregister_hw(ar->hw);
5773 
5774 	idr_for_each(&ar->txmgmt_idr, ath11k_mac_tx_mgmt_pending_free, ar);
5775 	idr_destroy(&ar->txmgmt_idr);
5776 
5777 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
5778 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
5779 
5780 	SET_IEEE80211_DEV(ar->hw, NULL);
5781 }
5782 
5783 void ath11k_mac_unregister(struct ath11k_base *ab)
5784 {
5785 	struct ath11k *ar;
5786 	struct ath11k_pdev *pdev;
5787 	int i;
5788 
5789 	for (i = 0; i < ab->num_radios; i++) {
5790 		pdev = &ab->pdevs[i];
5791 		ar = pdev->ar;
5792 		if (!ar)
5793 			continue;
5794 
5795 		__ath11k_mac_unregister(ar);
5796 	}
5797 }
5798 
5799 static int __ath11k_mac_register(struct ath11k *ar)
5800 {
5801 	struct ath11k_base *ab = ar->ab;
5802 	struct ath11k_pdev_cap *cap = &ar->pdev->cap;
5803 	static const u32 cipher_suites[] = {
5804 		WLAN_CIPHER_SUITE_TKIP,
5805 		WLAN_CIPHER_SUITE_CCMP,
5806 		WLAN_CIPHER_SUITE_AES_CMAC,
5807 		WLAN_CIPHER_SUITE_BIP_CMAC_256,
5808 		WLAN_CIPHER_SUITE_BIP_GMAC_128,
5809 		WLAN_CIPHER_SUITE_BIP_GMAC_256,
5810 		WLAN_CIPHER_SUITE_GCMP,
5811 		WLAN_CIPHER_SUITE_GCMP_256,
5812 		WLAN_CIPHER_SUITE_CCMP_256,
5813 	};
5814 	int ret;
5815 	u32 ht_cap = 0;
5816 
5817 	ath11k_pdev_caps_update(ar);
5818 
5819 	SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
5820 
5821 	SET_IEEE80211_DEV(ar->hw, ab->dev);
5822 
5823 	ret = ath11k_mac_setup_channels_rates(ar,
5824 					      cap->supported_bands);
5825 	if (ret)
5826 		goto err_free;
5827 
5828 	ath11k_mac_setup_ht_vht_cap(ar, cap, &ht_cap);
5829 	ath11k_mac_setup_he_cap(ar, cap);
5830 
5831 	ar->hw->wiphy->available_antennas_rx = cap->rx_chain_mask;
5832 	ar->hw->wiphy->available_antennas_tx = cap->tx_chain_mask;
5833 
5834 	ar->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
5835 					 BIT(NL80211_IFTYPE_AP) |
5836 					 BIT(NL80211_IFTYPE_MESH_POINT);
5837 
5838 	ieee80211_hw_set(ar->hw, SIGNAL_DBM);
5839 	ieee80211_hw_set(ar->hw, SUPPORTS_PS);
5840 	ieee80211_hw_set(ar->hw, SUPPORTS_DYNAMIC_PS);
5841 	ieee80211_hw_set(ar->hw, MFP_CAPABLE);
5842 	ieee80211_hw_set(ar->hw, REPORTS_TX_ACK_STATUS);
5843 	ieee80211_hw_set(ar->hw, HAS_RATE_CONTROL);
5844 	ieee80211_hw_set(ar->hw, AP_LINK_PS);
5845 	ieee80211_hw_set(ar->hw, SPECTRUM_MGMT);
5846 	ieee80211_hw_set(ar->hw, SUPPORT_FAST_XMIT);
5847 	ieee80211_hw_set(ar->hw, CONNECTION_MONITOR);
5848 	ieee80211_hw_set(ar->hw, SUPPORTS_PER_STA_GTK);
5849 	ieee80211_hw_set(ar->hw, WANT_MONITOR_VIF);
5850 	ieee80211_hw_set(ar->hw, CHANCTX_STA_CSA);
5851 	ieee80211_hw_set(ar->hw, QUEUE_CONTROL);
5852 	ieee80211_hw_set(ar->hw, SUPPORTS_TX_FRAG);
5853 	ieee80211_hw_set(ar->hw, REPORTS_LOW_ACK);
5854 	if (ht_cap & WMI_HT_CAP_ENABLED) {
5855 		ieee80211_hw_set(ar->hw, AMPDU_AGGREGATION);
5856 		ieee80211_hw_set(ar->hw, TX_AMPDU_SETUP_IN_HW);
5857 		ieee80211_hw_set(ar->hw, SUPPORTS_REORDERING_BUFFER);
5858 		ieee80211_hw_set(ar->hw, SUPPORTS_AMSDU_IN_AMPDU);
5859 		ieee80211_hw_set(ar->hw, USES_RSS);
5860 	}
5861 
5862 	ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
5863 	ar->hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
5864 
5865 	/* TODO: Check if HT capability advertised from firmware is different
5866 	 * for each band for a dual band capable radio. It will be tricky to
5867 	 * handle it when the ht capability different for each band.
5868 	 */
5869 	if (ht_cap & WMI_HT_CAP_DYNAMIC_SMPS)
5870 		ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
5871 
5872 	ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
5873 	ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
5874 
5875 	ar->hw->max_listen_interval = ATH11K_MAX_HW_LISTEN_INTERVAL;
5876 
5877 	ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
5878 	ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
5879 	ar->hw->wiphy->max_remain_on_channel_duration = 5000;
5880 
5881 	ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
5882 	ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
5883 				   NL80211_FEATURE_AP_SCAN;
5884 
5885 	ar->max_num_stations = TARGET_NUM_STATIONS;
5886 	ar->max_num_peers = TARGET_NUM_PEERS_PDEV;
5887 
5888 	ar->hw->wiphy->max_ap_assoc_sta = ar->max_num_stations;
5889 
5890 	ar->hw->queues = ATH11K_HW_MAX_QUEUES;
5891 	ar->hw->wiphy->tx_queue_len = ATH11K_QUEUE_LEN;
5892 	ar->hw->offchannel_tx_hw_queue = ATH11K_HW_MAX_QUEUES - 1;
5893 	ar->hw->max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF;
5894 
5895 	ar->hw->vif_data_size = sizeof(struct ath11k_vif);
5896 	ar->hw->sta_data_size = sizeof(struct ath11k_sta);
5897 
5898 	ar->hw->wiphy->iface_combinations = ath11k_if_comb;
5899 	ar->hw->wiphy->n_iface_combinations = ARRAY_SIZE(ath11k_if_comb);
5900 
5901 	wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
5902 	wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_STA_TX_PWR);
5903 
5904 	ar->hw->wiphy->cipher_suites = cipher_suites;
5905 	ar->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
5906 
5907 	ar->hw->wiphy->iftype_ext_capab = ath11k_iftypes_ext_capa;
5908 	ar->hw->wiphy->num_iftype_ext_capab =
5909 		ARRAY_SIZE(ath11k_iftypes_ext_capa);
5910 
5911 	ath11k_reg_init(ar);
5912 
5913 	/* advertise HW checksum offload capabilities */
5914 	ar->hw->netdev_features = NETIF_F_HW_CSUM;
5915 
5916 	ret = ieee80211_register_hw(ar->hw);
5917 	if (ret) {
5918 		ath11k_err(ar->ab, "ieee80211 registration failed: %d\n", ret);
5919 		goto err_free;
5920 	}
5921 
5922 	/* Apply the regd received during initialization */
5923 	ret = ath11k_regd_update(ar, true);
5924 	if (ret) {
5925 		ath11k_err(ar->ab, "ath11k regd update failed: %d\n", ret);
5926 		goto err_free;
5927 	}
5928 
5929 	ret = ath11k_debug_register(ar);
5930 	if (ret) {
5931 		ath11k_err(ar->ab, "debugfs registration failed: %d\n", ret);
5932 		goto err_free;
5933 	}
5934 
5935 	return 0;
5936 
5937 err_free:
5938 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
5939 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
5940 
5941 	SET_IEEE80211_DEV(ar->hw, NULL);
5942 	return ret;
5943 }
5944 
5945 int ath11k_mac_register(struct ath11k_base *ab)
5946 {
5947 	struct ath11k *ar;
5948 	struct ath11k_pdev *pdev;
5949 	int i;
5950 	int ret;
5951 
5952 	if (test_bit(ATH11K_FLAG_REGISTERED, &ab->dev_flags))
5953 		return 0;
5954 
5955 	for (i = 0; i < ab->num_radios; i++) {
5956 		pdev = &ab->pdevs[i];
5957 		ar = pdev->ar;
5958 		if (ab->pdevs_macaddr_valid) {
5959 			ether_addr_copy(ar->mac_addr, pdev->mac_addr);
5960 		} else {
5961 			ether_addr_copy(ar->mac_addr, ab->mac_addr);
5962 			ar->mac_addr[4] += i;
5963 		}
5964 
5965 		ret = __ath11k_mac_register(ar);
5966 		if (ret)
5967 			goto err_cleanup;
5968 
5969 		idr_init(&ar->txmgmt_idr);
5970 		spin_lock_init(&ar->txmgmt_idr_lock);
5971 	}
5972 
5973 	/* Initialize channel counters frequency value in hertz */
5974 	ab->cc_freq_hz = IPQ8074_CC_FREQ_HERTZ;
5975 	ab->free_vdev_map = (1LL << (ab->num_radios * TARGET_NUM_VDEVS)) - 1;
5976 
5977 	return 0;
5978 
5979 err_cleanup:
5980 	for (i = i - 1; i >= 0; i--) {
5981 		pdev = &ab->pdevs[i];
5982 		ar = pdev->ar;
5983 		__ath11k_mac_unregister(ar);
5984 	}
5985 
5986 	return ret;
5987 }
5988 
5989 int ath11k_mac_allocate(struct ath11k_base *ab)
5990 {
5991 	struct ieee80211_hw *hw;
5992 	struct ath11k *ar;
5993 	struct ath11k_pdev *pdev;
5994 	int ret;
5995 	int i;
5996 
5997 	if (test_bit(ATH11K_FLAG_REGISTERED, &ab->dev_flags))
5998 		return 0;
5999 
6000 	for (i = 0; i < ab->num_radios; i++) {
6001 		pdev = &ab->pdevs[i];
6002 		hw = ieee80211_alloc_hw(sizeof(struct ath11k), &ath11k_ops);
6003 		if (!hw) {
6004 			ath11k_warn(ab, "failed to allocate mac80211 hw device\n");
6005 			ret = -ENOMEM;
6006 			goto err_free_mac;
6007 		}
6008 
6009 		ar = hw->priv;
6010 		ar->hw = hw;
6011 		ar->ab = ab;
6012 		ar->pdev = pdev;
6013 		ar->pdev_idx = i;
6014 		ar->lmac_id = ath11k_core_get_hw_mac_id(ab, i);
6015 
6016 		ar->wmi = &ab->wmi_ab.wmi[i];
6017 		/* FIXME wmi[0] is already initialized during attach,
6018 		 * Should we do this again?
6019 		 */
6020 		ath11k_wmi_pdev_attach(ab, i);
6021 
6022 		ar->cfg_tx_chainmask = pdev->cap.tx_chain_mask;
6023 		ar->cfg_rx_chainmask = pdev->cap.rx_chain_mask;
6024 		ar->num_tx_chains = get_num_chains(pdev->cap.tx_chain_mask);
6025 		ar->num_rx_chains = get_num_chains(pdev->cap.rx_chain_mask);
6026 
6027 		pdev->ar = ar;
6028 		spin_lock_init(&ar->data_lock);
6029 		INIT_LIST_HEAD(&ar->arvifs);
6030 		INIT_LIST_HEAD(&ar->ppdu_stats_info);
6031 		mutex_init(&ar->conf_mutex);
6032 		init_completion(&ar->vdev_setup_done);
6033 		init_completion(&ar->peer_assoc_done);
6034 		init_completion(&ar->install_key_done);
6035 		init_completion(&ar->bss_survey_done);
6036 		init_completion(&ar->scan.started);
6037 		init_completion(&ar->scan.completed);
6038 		init_completion(&ar->thermal.wmi_sync);
6039 
6040 		INIT_DELAYED_WORK(&ar->scan.timeout, ath11k_scan_timeout_work);
6041 		INIT_WORK(&ar->regd_update_work, ath11k_regd_update_work);
6042 
6043 		INIT_WORK(&ar->wmi_mgmt_tx_work, ath11k_mgmt_over_wmi_tx_work);
6044 		skb_queue_head_init(&ar->wmi_mgmt_tx_queue);
6045 		clear_bit(ATH11K_FLAG_MONITOR_ENABLED, &ar->monitor_flags);
6046 	}
6047 
6048 	return 0;
6049 
6050 err_free_mac:
6051 	ath11k_mac_destroy(ab);
6052 
6053 	return ret;
6054 }
6055 
6056 void ath11k_mac_destroy(struct ath11k_base *ab)
6057 {
6058 	struct ath11k *ar;
6059 	struct ath11k_pdev *pdev;
6060 	int i;
6061 
6062 	for (i = 0; i < ab->num_radios; i++) {
6063 		pdev = &ab->pdevs[i];
6064 		ar = pdev->ar;
6065 		if (!ar)
6066 			continue;
6067 
6068 		ieee80211_free_hw(ar->hw);
6069 		pdev->ar = NULL;
6070 	}
6071 }
6072