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