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