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