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