xref: /openbmc/linux/drivers/net/wireless/ath/ath10k/mac.c (revision 51ad5b54)
1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (c) 2005-2011 Atheros Communications Inc.
4  * Copyright (c) 2011-2017 Qualcomm Atheros, Inc.
5  * Copyright (c) 2018-2019, The Linux Foundation. All rights reserved.
6  */
7 
8 #include "mac.h"
9 
10 #include <net/cfg80211.h>
11 #include <net/mac80211.h>
12 #include <linux/etherdevice.h>
13 #include <linux/acpi.h>
14 #include <linux/of.h>
15 #include <linux/bitfield.h>
16 
17 #include "hif.h"
18 #include "core.h"
19 #include "debug.h"
20 #include "wmi.h"
21 #include "htt.h"
22 #include "txrx.h"
23 #include "testmode.h"
24 #include "wmi-tlv.h"
25 #include "wmi-ops.h"
26 #include "wow.h"
27 
28 /*********/
29 /* Rates */
30 /*********/
31 
32 static struct ieee80211_rate ath10k_rates[] = {
33 	{ .bitrate = 10,
34 	  .hw_value = ATH10K_HW_RATE_CCK_LP_1M },
35 	{ .bitrate = 20,
36 	  .hw_value = ATH10K_HW_RATE_CCK_LP_2M,
37 	  .hw_value_short = ATH10K_HW_RATE_CCK_SP_2M,
38 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
39 	{ .bitrate = 55,
40 	  .hw_value = ATH10K_HW_RATE_CCK_LP_5_5M,
41 	  .hw_value_short = ATH10K_HW_RATE_CCK_SP_5_5M,
42 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
43 	{ .bitrate = 110,
44 	  .hw_value = ATH10K_HW_RATE_CCK_LP_11M,
45 	  .hw_value_short = ATH10K_HW_RATE_CCK_SP_11M,
46 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
47 
48 	{ .bitrate = 60, .hw_value = ATH10K_HW_RATE_OFDM_6M },
49 	{ .bitrate = 90, .hw_value = ATH10K_HW_RATE_OFDM_9M },
50 	{ .bitrate = 120, .hw_value = ATH10K_HW_RATE_OFDM_12M },
51 	{ .bitrate = 180, .hw_value = ATH10K_HW_RATE_OFDM_18M },
52 	{ .bitrate = 240, .hw_value = ATH10K_HW_RATE_OFDM_24M },
53 	{ .bitrate = 360, .hw_value = ATH10K_HW_RATE_OFDM_36M },
54 	{ .bitrate = 480, .hw_value = ATH10K_HW_RATE_OFDM_48M },
55 	{ .bitrate = 540, .hw_value = ATH10K_HW_RATE_OFDM_54M },
56 };
57 
58 static struct ieee80211_rate ath10k_rates_rev2[] = {
59 	{ .bitrate = 10,
60 	  .hw_value = ATH10K_HW_RATE_REV2_CCK_LP_1M },
61 	{ .bitrate = 20,
62 	  .hw_value = ATH10K_HW_RATE_REV2_CCK_LP_2M,
63 	  .hw_value_short = ATH10K_HW_RATE_REV2_CCK_SP_2M,
64 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
65 	{ .bitrate = 55,
66 	  .hw_value = ATH10K_HW_RATE_REV2_CCK_LP_5_5M,
67 	  .hw_value_short = ATH10K_HW_RATE_REV2_CCK_SP_5_5M,
68 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
69 	{ .bitrate = 110,
70 	  .hw_value = ATH10K_HW_RATE_REV2_CCK_LP_11M,
71 	  .hw_value_short = ATH10K_HW_RATE_REV2_CCK_SP_11M,
72 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
73 
74 	{ .bitrate = 60, .hw_value = ATH10K_HW_RATE_OFDM_6M },
75 	{ .bitrate = 90, .hw_value = ATH10K_HW_RATE_OFDM_9M },
76 	{ .bitrate = 120, .hw_value = ATH10K_HW_RATE_OFDM_12M },
77 	{ .bitrate = 180, .hw_value = ATH10K_HW_RATE_OFDM_18M },
78 	{ .bitrate = 240, .hw_value = ATH10K_HW_RATE_OFDM_24M },
79 	{ .bitrate = 360, .hw_value = ATH10K_HW_RATE_OFDM_36M },
80 	{ .bitrate = 480, .hw_value = ATH10K_HW_RATE_OFDM_48M },
81 	{ .bitrate = 540, .hw_value = ATH10K_HW_RATE_OFDM_54M },
82 };
83 
84 #define ATH10K_MAC_FIRST_OFDM_RATE_IDX 4
85 
86 #define ath10k_a_rates (ath10k_rates + ATH10K_MAC_FIRST_OFDM_RATE_IDX)
87 #define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - \
88 			     ATH10K_MAC_FIRST_OFDM_RATE_IDX)
89 #define ath10k_g_rates (ath10k_rates + 0)
90 #define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
91 
92 #define ath10k_g_rates_rev2 (ath10k_rates_rev2 + 0)
93 #define ath10k_g_rates_rev2_size (ARRAY_SIZE(ath10k_rates_rev2))
94 
95 #define ath10k_wmi_legacy_rates ath10k_rates
96 
97 static bool ath10k_mac_bitrate_is_cck(int bitrate)
98 {
99 	switch (bitrate) {
100 	case 10:
101 	case 20:
102 	case 55:
103 	case 110:
104 		return true;
105 	}
106 
107 	return false;
108 }
109 
110 static u8 ath10k_mac_bitrate_to_rate(int bitrate)
111 {
112 	return DIV_ROUND_UP(bitrate, 5) |
113 	       (ath10k_mac_bitrate_is_cck(bitrate) ? BIT(7) : 0);
114 }
115 
116 u8 ath10k_mac_hw_rate_to_idx(const struct ieee80211_supported_band *sband,
117 			     u8 hw_rate, bool cck)
118 {
119 	const struct ieee80211_rate *rate;
120 	int i;
121 
122 	for (i = 0; i < sband->n_bitrates; i++) {
123 		rate = &sband->bitrates[i];
124 
125 		if (ath10k_mac_bitrate_is_cck(rate->bitrate) != cck)
126 			continue;
127 
128 		if (rate->hw_value == hw_rate)
129 			return i;
130 		else if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE &&
131 			 rate->hw_value_short == hw_rate)
132 			return i;
133 	}
134 
135 	return 0;
136 }
137 
138 u8 ath10k_mac_bitrate_to_idx(const struct ieee80211_supported_band *sband,
139 			     u32 bitrate)
140 {
141 	int i;
142 
143 	for (i = 0; i < sband->n_bitrates; i++)
144 		if (sband->bitrates[i].bitrate == bitrate)
145 			return i;
146 
147 	return 0;
148 }
149 
150 static int ath10k_mac_get_rate_hw_value(int bitrate)
151 {
152 	int i;
153 	u8 hw_value_prefix = 0;
154 
155 	if (ath10k_mac_bitrate_is_cck(bitrate))
156 		hw_value_prefix = WMI_RATE_PREAMBLE_CCK << 6;
157 
158 	for (i = 0; i < ARRAY_SIZE(ath10k_rates); i++) {
159 		if (ath10k_rates[i].bitrate == bitrate)
160 			return hw_value_prefix | ath10k_rates[i].hw_value;
161 	}
162 
163 	return -EINVAL;
164 }
165 
166 static int ath10k_mac_get_max_vht_mcs_map(u16 mcs_map, int nss)
167 {
168 	switch ((mcs_map >> (2 * nss)) & 0x3) {
169 	case IEEE80211_VHT_MCS_SUPPORT_0_7: return BIT(8) - 1;
170 	case IEEE80211_VHT_MCS_SUPPORT_0_8: return BIT(9) - 1;
171 	case IEEE80211_VHT_MCS_SUPPORT_0_9: return BIT(10) - 1;
172 	}
173 	return 0;
174 }
175 
176 static u32
177 ath10k_mac_max_ht_nss(const u8 ht_mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
178 {
179 	int nss;
180 
181 	for (nss = IEEE80211_HT_MCS_MASK_LEN - 1; nss >= 0; nss--)
182 		if (ht_mcs_mask[nss])
183 			return nss + 1;
184 
185 	return 1;
186 }
187 
188 static u32
189 ath10k_mac_max_vht_nss(const u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
190 {
191 	int nss;
192 
193 	for (nss = NL80211_VHT_NSS_MAX - 1; nss >= 0; nss--)
194 		if (vht_mcs_mask[nss])
195 			return nss + 1;
196 
197 	return 1;
198 }
199 
200 int ath10k_mac_ext_resource_config(struct ath10k *ar, u32 val)
201 {
202 	enum wmi_host_platform_type platform_type;
203 	int ret;
204 
205 	if (test_bit(WMI_SERVICE_TX_MODE_DYNAMIC, ar->wmi.svc_map))
206 		platform_type = WMI_HOST_PLATFORM_LOW_PERF;
207 	else
208 		platform_type = WMI_HOST_PLATFORM_HIGH_PERF;
209 
210 	ret = ath10k_wmi_ext_resource_config(ar, platform_type, val);
211 
212 	if (ret && ret != -EOPNOTSUPP) {
213 		ath10k_warn(ar, "failed to configure ext resource: %d\n", ret);
214 		return ret;
215 	}
216 
217 	return 0;
218 }
219 
220 /**********/
221 /* Crypto */
222 /**********/
223 
224 static int ath10k_send_key(struct ath10k_vif *arvif,
225 			   struct ieee80211_key_conf *key,
226 			   enum set_key_cmd cmd,
227 			   const u8 *macaddr, u32 flags)
228 {
229 	struct ath10k *ar = arvif->ar;
230 	struct wmi_vdev_install_key_arg arg = {
231 		.vdev_id = arvif->vdev_id,
232 		.key_idx = key->keyidx,
233 		.key_len = key->keylen,
234 		.key_data = key->key,
235 		.key_flags = flags,
236 		.macaddr = macaddr,
237 	};
238 
239 	lockdep_assert_held(&arvif->ar->conf_mutex);
240 
241 	switch (key->cipher) {
242 	case WLAN_CIPHER_SUITE_CCMP:
243 		arg.key_cipher = ar->wmi_key_cipher[WMI_CIPHER_AES_CCM];
244 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
245 		break;
246 	case WLAN_CIPHER_SUITE_TKIP:
247 		arg.key_cipher = ar->wmi_key_cipher[WMI_CIPHER_TKIP];
248 		arg.key_txmic_len = 8;
249 		arg.key_rxmic_len = 8;
250 		break;
251 	case WLAN_CIPHER_SUITE_WEP40:
252 	case WLAN_CIPHER_SUITE_WEP104:
253 		arg.key_cipher = ar->wmi_key_cipher[WMI_CIPHER_WEP];
254 		break;
255 	case WLAN_CIPHER_SUITE_CCMP_256:
256 		arg.key_cipher = ar->wmi_key_cipher[WMI_CIPHER_AES_CCM];
257 		break;
258 	case WLAN_CIPHER_SUITE_GCMP:
259 	case WLAN_CIPHER_SUITE_GCMP_256:
260 		arg.key_cipher = ar->wmi_key_cipher[WMI_CIPHER_AES_GCM];
261 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
262 		break;
263 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
264 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
265 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
266 	case WLAN_CIPHER_SUITE_AES_CMAC:
267 		WARN_ON(1);
268 		return -EINVAL;
269 	default:
270 		ath10k_warn(ar, "cipher %d is not supported\n", key->cipher);
271 		return -EOPNOTSUPP;
272 	}
273 
274 	if (test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
275 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
276 
277 	if (cmd == DISABLE_KEY) {
278 		arg.key_cipher = ar->wmi_key_cipher[WMI_CIPHER_NONE];
279 		arg.key_data = NULL;
280 	}
281 
282 	return ath10k_wmi_vdev_install_key(arvif->ar, &arg);
283 }
284 
285 static int ath10k_install_key(struct ath10k_vif *arvif,
286 			      struct ieee80211_key_conf *key,
287 			      enum set_key_cmd cmd,
288 			      const u8 *macaddr, u32 flags)
289 {
290 	struct ath10k *ar = arvif->ar;
291 	int ret;
292 	unsigned long time_left;
293 
294 	lockdep_assert_held(&ar->conf_mutex);
295 
296 	reinit_completion(&ar->install_key_done);
297 
298 	if (arvif->nohwcrypt)
299 		return 1;
300 
301 	ret = ath10k_send_key(arvif, key, cmd, macaddr, flags);
302 	if (ret)
303 		return ret;
304 
305 	time_left = wait_for_completion_timeout(&ar->install_key_done, 3 * HZ);
306 	if (time_left == 0)
307 		return -ETIMEDOUT;
308 
309 	return 0;
310 }
311 
312 static int ath10k_install_peer_wep_keys(struct ath10k_vif *arvif,
313 					const u8 *addr)
314 {
315 	struct ath10k *ar = arvif->ar;
316 	struct ath10k_peer *peer;
317 	int ret;
318 	int i;
319 	u32 flags;
320 
321 	lockdep_assert_held(&ar->conf_mutex);
322 
323 	if (WARN_ON(arvif->vif->type != NL80211_IFTYPE_AP &&
324 		    arvif->vif->type != NL80211_IFTYPE_ADHOC &&
325 		    arvif->vif->type != NL80211_IFTYPE_MESH_POINT))
326 		return -EINVAL;
327 
328 	spin_lock_bh(&ar->data_lock);
329 	peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
330 	spin_unlock_bh(&ar->data_lock);
331 
332 	if (!peer)
333 		return -ENOENT;
334 
335 	for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
336 		if (arvif->wep_keys[i] == NULL)
337 			continue;
338 
339 		switch (arvif->vif->type) {
340 		case NL80211_IFTYPE_AP:
341 			flags = WMI_KEY_PAIRWISE;
342 
343 			if (arvif->def_wep_key_idx == i)
344 				flags |= WMI_KEY_TX_USAGE;
345 
346 			ret = ath10k_install_key(arvif, arvif->wep_keys[i],
347 						 SET_KEY, addr, flags);
348 			if (ret < 0)
349 				return ret;
350 			break;
351 		case NL80211_IFTYPE_ADHOC:
352 			ret = ath10k_install_key(arvif, arvif->wep_keys[i],
353 						 SET_KEY, addr,
354 						 WMI_KEY_PAIRWISE);
355 			if (ret < 0)
356 				return ret;
357 
358 			ret = ath10k_install_key(arvif, arvif->wep_keys[i],
359 						 SET_KEY, addr, WMI_KEY_GROUP);
360 			if (ret < 0)
361 				return ret;
362 			break;
363 		default:
364 			WARN_ON(1);
365 			return -EINVAL;
366 		}
367 
368 		spin_lock_bh(&ar->data_lock);
369 		peer->keys[i] = arvif->wep_keys[i];
370 		spin_unlock_bh(&ar->data_lock);
371 	}
372 
373 	/* In some cases (notably with static WEP IBSS with multiple keys)
374 	 * multicast Tx becomes broken. Both pairwise and groupwise keys are
375 	 * installed already. Using WMI_KEY_TX_USAGE in different combinations
376 	 * didn't seem help. Using def_keyid vdev parameter seems to be
377 	 * effective so use that.
378 	 *
379 	 * FIXME: Revisit. Perhaps this can be done in a less hacky way.
380 	 */
381 	if (arvif->vif->type != NL80211_IFTYPE_ADHOC)
382 		return 0;
383 
384 	if (arvif->def_wep_key_idx == -1)
385 		return 0;
386 
387 	ret = ath10k_wmi_vdev_set_param(arvif->ar,
388 					arvif->vdev_id,
389 					arvif->ar->wmi.vdev_param->def_keyid,
390 					arvif->def_wep_key_idx);
391 	if (ret) {
392 		ath10k_warn(ar, "failed to re-set def wpa key idxon vdev %i: %d\n",
393 			    arvif->vdev_id, ret);
394 		return ret;
395 	}
396 
397 	return 0;
398 }
399 
400 static int ath10k_clear_peer_keys(struct ath10k_vif *arvif,
401 				  const u8 *addr)
402 {
403 	struct ath10k *ar = arvif->ar;
404 	struct ath10k_peer *peer;
405 	int first_errno = 0;
406 	int ret;
407 	int i;
408 	u32 flags = 0;
409 
410 	lockdep_assert_held(&ar->conf_mutex);
411 
412 	spin_lock_bh(&ar->data_lock);
413 	peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
414 	spin_unlock_bh(&ar->data_lock);
415 
416 	if (!peer)
417 		return -ENOENT;
418 
419 	for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
420 		if (peer->keys[i] == NULL)
421 			continue;
422 
423 		/* key flags are not required to delete the key */
424 		ret = ath10k_install_key(arvif, peer->keys[i],
425 					 DISABLE_KEY, addr, flags);
426 		if (ret < 0 && first_errno == 0)
427 			first_errno = ret;
428 
429 		if (ret < 0)
430 			ath10k_warn(ar, "failed to remove peer wep key %d: %d\n",
431 				    i, ret);
432 
433 		spin_lock_bh(&ar->data_lock);
434 		peer->keys[i] = NULL;
435 		spin_unlock_bh(&ar->data_lock);
436 	}
437 
438 	return first_errno;
439 }
440 
441 bool ath10k_mac_is_peer_wep_key_set(struct ath10k *ar, const u8 *addr,
442 				    u8 keyidx)
443 {
444 	struct ath10k_peer *peer;
445 	int i;
446 
447 	lockdep_assert_held(&ar->data_lock);
448 
449 	/* We don't know which vdev this peer belongs to,
450 	 * since WMI doesn't give us that information.
451 	 *
452 	 * FIXME: multi-bss needs to be handled.
453 	 */
454 	peer = ath10k_peer_find(ar, 0, addr);
455 	if (!peer)
456 		return false;
457 
458 	for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
459 		if (peer->keys[i] && peer->keys[i]->keyidx == keyidx)
460 			return true;
461 	}
462 
463 	return false;
464 }
465 
466 static int ath10k_clear_vdev_key(struct ath10k_vif *arvif,
467 				 struct ieee80211_key_conf *key)
468 {
469 	struct ath10k *ar = arvif->ar;
470 	struct ath10k_peer *peer;
471 	u8 addr[ETH_ALEN];
472 	int first_errno = 0;
473 	int ret;
474 	int i;
475 	u32 flags = 0;
476 
477 	lockdep_assert_held(&ar->conf_mutex);
478 
479 	for (;;) {
480 		/* since ath10k_install_key we can't hold data_lock all the
481 		 * time, so we try to remove the keys incrementally
482 		 */
483 		spin_lock_bh(&ar->data_lock);
484 		i = 0;
485 		list_for_each_entry(peer, &ar->peers, list) {
486 			for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
487 				if (peer->keys[i] == key) {
488 					ether_addr_copy(addr, peer->addr);
489 					peer->keys[i] = NULL;
490 					break;
491 				}
492 			}
493 
494 			if (i < ARRAY_SIZE(peer->keys))
495 				break;
496 		}
497 		spin_unlock_bh(&ar->data_lock);
498 
499 		if (i == ARRAY_SIZE(peer->keys))
500 			break;
501 		/* key flags are not required to delete the key */
502 		ret = ath10k_install_key(arvif, key, DISABLE_KEY, addr, flags);
503 		if (ret < 0 && first_errno == 0)
504 			first_errno = ret;
505 
506 		if (ret)
507 			ath10k_warn(ar, "failed to remove key for %pM: %d\n",
508 				    addr, ret);
509 	}
510 
511 	return first_errno;
512 }
513 
514 static int ath10k_mac_vif_update_wep_key(struct ath10k_vif *arvif,
515 					 struct ieee80211_key_conf *key)
516 {
517 	struct ath10k *ar = arvif->ar;
518 	struct ath10k_peer *peer;
519 	int ret;
520 
521 	lockdep_assert_held(&ar->conf_mutex);
522 
523 	list_for_each_entry(peer, &ar->peers, list) {
524 		if (ether_addr_equal(peer->addr, arvif->vif->addr))
525 			continue;
526 
527 		if (ether_addr_equal(peer->addr, arvif->bssid))
528 			continue;
529 
530 		if (peer->keys[key->keyidx] == key)
531 			continue;
532 
533 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vif vdev %i update key %i needs update\n",
534 			   arvif->vdev_id, key->keyidx);
535 
536 		ret = ath10k_install_peer_wep_keys(arvif, peer->addr);
537 		if (ret) {
538 			ath10k_warn(ar, "failed to update wep keys on vdev %i for peer %pM: %d\n",
539 				    arvif->vdev_id, peer->addr, ret);
540 			return ret;
541 		}
542 	}
543 
544 	return 0;
545 }
546 
547 /*********************/
548 /* General utilities */
549 /*********************/
550 
551 static inline enum wmi_phy_mode
552 chan_to_phymode(const struct cfg80211_chan_def *chandef)
553 {
554 	enum wmi_phy_mode phymode = MODE_UNKNOWN;
555 
556 	switch (chandef->chan->band) {
557 	case NL80211_BAND_2GHZ:
558 		switch (chandef->width) {
559 		case NL80211_CHAN_WIDTH_20_NOHT:
560 			if (chandef->chan->flags & IEEE80211_CHAN_NO_OFDM)
561 				phymode = MODE_11B;
562 			else
563 				phymode = MODE_11G;
564 			break;
565 		case NL80211_CHAN_WIDTH_20:
566 			phymode = MODE_11NG_HT20;
567 			break;
568 		case NL80211_CHAN_WIDTH_40:
569 			phymode = MODE_11NG_HT40;
570 			break;
571 		case NL80211_CHAN_WIDTH_5:
572 		case NL80211_CHAN_WIDTH_10:
573 		case NL80211_CHAN_WIDTH_80:
574 		case NL80211_CHAN_WIDTH_80P80:
575 		case NL80211_CHAN_WIDTH_160:
576 			phymode = MODE_UNKNOWN;
577 			break;
578 		}
579 		break;
580 	case NL80211_BAND_5GHZ:
581 		switch (chandef->width) {
582 		case NL80211_CHAN_WIDTH_20_NOHT:
583 			phymode = MODE_11A;
584 			break;
585 		case NL80211_CHAN_WIDTH_20:
586 			phymode = MODE_11NA_HT20;
587 			break;
588 		case NL80211_CHAN_WIDTH_40:
589 			phymode = MODE_11NA_HT40;
590 			break;
591 		case NL80211_CHAN_WIDTH_80:
592 			phymode = MODE_11AC_VHT80;
593 			break;
594 		case NL80211_CHAN_WIDTH_160:
595 			phymode = MODE_11AC_VHT160;
596 			break;
597 		case NL80211_CHAN_WIDTH_80P80:
598 			phymode = MODE_11AC_VHT80_80;
599 			break;
600 		case NL80211_CHAN_WIDTH_5:
601 		case NL80211_CHAN_WIDTH_10:
602 			phymode = MODE_UNKNOWN;
603 			break;
604 		}
605 		break;
606 	default:
607 		break;
608 	}
609 
610 	WARN_ON(phymode == MODE_UNKNOWN);
611 	return phymode;
612 }
613 
614 static u8 ath10k_parse_mpdudensity(u8 mpdudensity)
615 {
616 /*
617  * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
618  *   0 for no restriction
619  *   1 for 1/4 us
620  *   2 for 1/2 us
621  *   3 for 1 us
622  *   4 for 2 us
623  *   5 for 4 us
624  *   6 for 8 us
625  *   7 for 16 us
626  */
627 	switch (mpdudensity) {
628 	case 0:
629 		return 0;
630 	case 1:
631 	case 2:
632 	case 3:
633 	/* Our lower layer calculations limit our precision to
634 	 * 1 microsecond
635 	 */
636 		return 1;
637 	case 4:
638 		return 2;
639 	case 5:
640 		return 4;
641 	case 6:
642 		return 8;
643 	case 7:
644 		return 16;
645 	default:
646 		return 0;
647 	}
648 }
649 
650 int ath10k_mac_vif_chan(struct ieee80211_vif *vif,
651 			struct cfg80211_chan_def *def)
652 {
653 	struct ieee80211_chanctx_conf *conf;
654 
655 	rcu_read_lock();
656 	conf = rcu_dereference(vif->chanctx_conf);
657 	if (!conf) {
658 		rcu_read_unlock();
659 		return -ENOENT;
660 	}
661 
662 	*def = conf->def;
663 	rcu_read_unlock();
664 
665 	return 0;
666 }
667 
668 static void ath10k_mac_num_chanctxs_iter(struct ieee80211_hw *hw,
669 					 struct ieee80211_chanctx_conf *conf,
670 					 void *data)
671 {
672 	int *num = data;
673 
674 	(*num)++;
675 }
676 
677 static int ath10k_mac_num_chanctxs(struct ath10k *ar)
678 {
679 	int num = 0;
680 
681 	ieee80211_iter_chan_contexts_atomic(ar->hw,
682 					    ath10k_mac_num_chanctxs_iter,
683 					    &num);
684 
685 	return num;
686 }
687 
688 static void
689 ath10k_mac_get_any_chandef_iter(struct ieee80211_hw *hw,
690 				struct ieee80211_chanctx_conf *conf,
691 				void *data)
692 {
693 	struct cfg80211_chan_def **def = data;
694 
695 	*def = &conf->def;
696 }
697 
698 static void ath10k_wait_for_peer_delete_done(struct ath10k *ar, u32 vdev_id,
699 					     const u8 *addr)
700 {
701 	unsigned long time_left;
702 	int ret;
703 
704 	if (test_bit(WMI_SERVICE_SYNC_DELETE_CMDS, ar->wmi.svc_map)) {
705 		ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
706 		if (ret) {
707 			ath10k_warn(ar, "failed wait for peer deleted");
708 			return;
709 		}
710 
711 		time_left = wait_for_completion_timeout(&ar->peer_delete_done,
712 							5 * HZ);
713 		if (!time_left)
714 			ath10k_warn(ar, "Timeout in receiving peer delete response\n");
715 	}
716 }
717 
718 static int ath10k_peer_create(struct ath10k *ar,
719 			      struct ieee80211_vif *vif,
720 			      struct ieee80211_sta *sta,
721 			      u32 vdev_id,
722 			      const u8 *addr,
723 			      enum wmi_peer_type peer_type)
724 {
725 	struct ath10k_vif *arvif;
726 	struct ath10k_peer *peer;
727 	int num_peers = 0;
728 	int ret;
729 
730 	lockdep_assert_held(&ar->conf_mutex);
731 
732 	num_peers = ar->num_peers;
733 
734 	/* Each vdev consumes a peer entry as well */
735 	list_for_each_entry(arvif, &ar->arvifs, list)
736 		num_peers++;
737 
738 	if (num_peers >= ar->max_num_peers)
739 		return -ENOBUFS;
740 
741 	ret = ath10k_wmi_peer_create(ar, vdev_id, addr, peer_type);
742 	if (ret) {
743 		ath10k_warn(ar, "failed to create wmi peer %pM on vdev %i: %i\n",
744 			    addr, vdev_id, ret);
745 		return ret;
746 	}
747 
748 	ret = ath10k_wait_for_peer_created(ar, vdev_id, addr);
749 	if (ret) {
750 		ath10k_warn(ar, "failed to wait for created wmi peer %pM on vdev %i: %i\n",
751 			    addr, vdev_id, ret);
752 		return ret;
753 	}
754 
755 	spin_lock_bh(&ar->data_lock);
756 
757 	peer = ath10k_peer_find(ar, vdev_id, addr);
758 	if (!peer) {
759 		spin_unlock_bh(&ar->data_lock);
760 		ath10k_warn(ar, "failed to find peer %pM on vdev %i after creation\n",
761 			    addr, vdev_id);
762 		ath10k_wait_for_peer_delete_done(ar, vdev_id, addr);
763 		return -ENOENT;
764 	}
765 
766 	peer->vif = vif;
767 	peer->sta = sta;
768 
769 	spin_unlock_bh(&ar->data_lock);
770 
771 	ar->num_peers++;
772 
773 	return 0;
774 }
775 
776 static int ath10k_mac_set_kickout(struct ath10k_vif *arvif)
777 {
778 	struct ath10k *ar = arvif->ar;
779 	u32 param;
780 	int ret;
781 
782 	param = ar->wmi.pdev_param->sta_kickout_th;
783 	ret = ath10k_wmi_pdev_set_param(ar, param,
784 					ATH10K_KICKOUT_THRESHOLD);
785 	if (ret) {
786 		ath10k_warn(ar, "failed to set kickout threshold on vdev %i: %d\n",
787 			    arvif->vdev_id, ret);
788 		return ret;
789 	}
790 
791 	param = ar->wmi.vdev_param->ap_keepalive_min_idle_inactive_time_secs;
792 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
793 					ATH10K_KEEPALIVE_MIN_IDLE);
794 	if (ret) {
795 		ath10k_warn(ar, "failed to set keepalive minimum idle time on vdev %i: %d\n",
796 			    arvif->vdev_id, ret);
797 		return ret;
798 	}
799 
800 	param = ar->wmi.vdev_param->ap_keepalive_max_idle_inactive_time_secs;
801 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
802 					ATH10K_KEEPALIVE_MAX_IDLE);
803 	if (ret) {
804 		ath10k_warn(ar, "failed to set keepalive maximum idle time on vdev %i: %d\n",
805 			    arvif->vdev_id, ret);
806 		return ret;
807 	}
808 
809 	param = ar->wmi.vdev_param->ap_keepalive_max_unresponsive_time_secs;
810 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
811 					ATH10K_KEEPALIVE_MAX_UNRESPONSIVE);
812 	if (ret) {
813 		ath10k_warn(ar, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
814 			    arvif->vdev_id, ret);
815 		return ret;
816 	}
817 
818 	return 0;
819 }
820 
821 static int ath10k_mac_set_rts(struct ath10k_vif *arvif, u32 value)
822 {
823 	struct ath10k *ar = arvif->ar;
824 	u32 vdev_param;
825 
826 	vdev_param = ar->wmi.vdev_param->rts_threshold;
827 	return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
828 }
829 
830 static int ath10k_peer_delete(struct ath10k *ar, u32 vdev_id, const u8 *addr)
831 {
832 	int ret;
833 
834 	lockdep_assert_held(&ar->conf_mutex);
835 
836 	ret = ath10k_wmi_peer_delete(ar, vdev_id, addr);
837 	if (ret)
838 		return ret;
839 
840 	ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
841 	if (ret)
842 		return ret;
843 
844 	if (test_bit(WMI_SERVICE_SYNC_DELETE_CMDS, ar->wmi.svc_map)) {
845 		unsigned long time_left;
846 
847 		time_left = wait_for_completion_timeout
848 			    (&ar->peer_delete_done, 5 * HZ);
849 
850 		if (!time_left) {
851 			ath10k_warn(ar, "Timeout in receiving peer delete response\n");
852 			return -ETIMEDOUT;
853 		}
854 	}
855 
856 	ar->num_peers--;
857 
858 	return 0;
859 }
860 
861 static void ath10k_peer_cleanup(struct ath10k *ar, u32 vdev_id)
862 {
863 	struct ath10k_peer *peer, *tmp;
864 	int peer_id;
865 	int i;
866 
867 	lockdep_assert_held(&ar->conf_mutex);
868 
869 	spin_lock_bh(&ar->data_lock);
870 	list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
871 		if (peer->vdev_id != vdev_id)
872 			continue;
873 
874 		ath10k_warn(ar, "removing stale peer %pM from vdev_id %d\n",
875 			    peer->addr, vdev_id);
876 
877 		for_each_set_bit(peer_id, peer->peer_ids,
878 				 ATH10K_MAX_NUM_PEER_IDS) {
879 			ar->peer_map[peer_id] = NULL;
880 		}
881 
882 		/* Double check that peer is properly un-referenced from
883 		 * the peer_map
884 		 */
885 		for (i = 0; i < ARRAY_SIZE(ar->peer_map); i++) {
886 			if (ar->peer_map[i] == peer) {
887 				ath10k_warn(ar, "removing stale peer_map entry for %pM (ptr %pK idx %d)\n",
888 					    peer->addr, peer, i);
889 				ar->peer_map[i] = NULL;
890 			}
891 		}
892 
893 		list_del(&peer->list);
894 		kfree(peer);
895 		ar->num_peers--;
896 	}
897 	spin_unlock_bh(&ar->data_lock);
898 }
899 
900 static void ath10k_peer_cleanup_all(struct ath10k *ar)
901 {
902 	struct ath10k_peer *peer, *tmp;
903 	int i;
904 
905 	lockdep_assert_held(&ar->conf_mutex);
906 
907 	spin_lock_bh(&ar->data_lock);
908 	list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
909 		list_del(&peer->list);
910 		kfree(peer);
911 	}
912 
913 	for (i = 0; i < ARRAY_SIZE(ar->peer_map); i++)
914 		ar->peer_map[i] = NULL;
915 
916 	spin_unlock_bh(&ar->data_lock);
917 
918 	ar->num_peers = 0;
919 	ar->num_stations = 0;
920 }
921 
922 static int ath10k_mac_tdls_peer_update(struct ath10k *ar, u32 vdev_id,
923 				       struct ieee80211_sta *sta,
924 				       enum wmi_tdls_peer_state state)
925 {
926 	int ret;
927 	struct wmi_tdls_peer_update_cmd_arg arg = {};
928 	struct wmi_tdls_peer_capab_arg cap = {};
929 	struct wmi_channel_arg chan_arg = {};
930 
931 	lockdep_assert_held(&ar->conf_mutex);
932 
933 	arg.vdev_id = vdev_id;
934 	arg.peer_state = state;
935 	ether_addr_copy(arg.addr, sta->addr);
936 
937 	cap.peer_max_sp = sta->max_sp;
938 	cap.peer_uapsd_queues = sta->uapsd_queues;
939 
940 	if (state == WMI_TDLS_PEER_STATE_CONNECTED &&
941 	    !sta->tdls_initiator)
942 		cap.is_peer_responder = 1;
943 
944 	ret = ath10k_wmi_tdls_peer_update(ar, &arg, &cap, &chan_arg);
945 	if (ret) {
946 		ath10k_warn(ar, "failed to update tdls peer %pM on vdev %i: %i\n",
947 			    arg.addr, vdev_id, ret);
948 		return ret;
949 	}
950 
951 	return 0;
952 }
953 
954 /************************/
955 /* Interface management */
956 /************************/
957 
958 void ath10k_mac_vif_beacon_free(struct ath10k_vif *arvif)
959 {
960 	struct ath10k *ar = arvif->ar;
961 
962 	lockdep_assert_held(&ar->data_lock);
963 
964 	if (!arvif->beacon)
965 		return;
966 
967 	if (!arvif->beacon_buf)
968 		dma_unmap_single(ar->dev, ATH10K_SKB_CB(arvif->beacon)->paddr,
969 				 arvif->beacon->len, DMA_TO_DEVICE);
970 
971 	if (WARN_ON(arvif->beacon_state != ATH10K_BEACON_SCHEDULED &&
972 		    arvif->beacon_state != ATH10K_BEACON_SENT))
973 		return;
974 
975 	dev_kfree_skb_any(arvif->beacon);
976 
977 	arvif->beacon = NULL;
978 	arvif->beacon_state = ATH10K_BEACON_SCHEDULED;
979 }
980 
981 static void ath10k_mac_vif_beacon_cleanup(struct ath10k_vif *arvif)
982 {
983 	struct ath10k *ar = arvif->ar;
984 
985 	lockdep_assert_held(&ar->data_lock);
986 
987 	ath10k_mac_vif_beacon_free(arvif);
988 
989 	if (arvif->beacon_buf) {
990 		dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
991 				  arvif->beacon_buf, arvif->beacon_paddr);
992 		arvif->beacon_buf = NULL;
993 	}
994 }
995 
996 static inline int ath10k_vdev_setup_sync(struct ath10k *ar)
997 {
998 	unsigned long time_left;
999 
1000 	lockdep_assert_held(&ar->conf_mutex);
1001 
1002 	if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags))
1003 		return -ESHUTDOWN;
1004 
1005 	time_left = wait_for_completion_timeout(&ar->vdev_setup_done,
1006 						ATH10K_VDEV_SETUP_TIMEOUT_HZ);
1007 	if (time_left == 0)
1008 		return -ETIMEDOUT;
1009 
1010 	return ar->last_wmi_vdev_start_status;
1011 }
1012 
1013 static int ath10k_monitor_vdev_start(struct ath10k *ar, int vdev_id)
1014 {
1015 	struct cfg80211_chan_def *chandef = NULL;
1016 	struct ieee80211_channel *channel = NULL;
1017 	struct wmi_vdev_start_request_arg arg = {};
1018 	int ret = 0;
1019 
1020 	lockdep_assert_held(&ar->conf_mutex);
1021 
1022 	ieee80211_iter_chan_contexts_atomic(ar->hw,
1023 					    ath10k_mac_get_any_chandef_iter,
1024 					    &chandef);
1025 	if (WARN_ON_ONCE(!chandef))
1026 		return -ENOENT;
1027 
1028 	channel = chandef->chan;
1029 
1030 	arg.vdev_id = vdev_id;
1031 	arg.channel.freq = channel->center_freq;
1032 	arg.channel.band_center_freq1 = chandef->center_freq1;
1033 	arg.channel.band_center_freq2 = chandef->center_freq2;
1034 
1035 	/* TODO setup this dynamically, what in case we
1036 	 * don't have any vifs?
1037 	 */
1038 	arg.channel.mode = chan_to_phymode(chandef);
1039 	arg.channel.chan_radar =
1040 			!!(channel->flags & IEEE80211_CHAN_RADAR);
1041 
1042 	arg.channel.min_power = 0;
1043 	arg.channel.max_power = channel->max_power * 2;
1044 	arg.channel.max_reg_power = channel->max_reg_power * 2;
1045 	arg.channel.max_antenna_gain = channel->max_antenna_gain * 2;
1046 
1047 	reinit_completion(&ar->vdev_setup_done);
1048 	reinit_completion(&ar->vdev_delete_done);
1049 
1050 	ret = ath10k_wmi_vdev_start(ar, &arg);
1051 	if (ret) {
1052 		ath10k_warn(ar, "failed to request monitor vdev %i start: %d\n",
1053 			    vdev_id, ret);
1054 		return ret;
1055 	}
1056 
1057 	ret = ath10k_vdev_setup_sync(ar);
1058 	if (ret) {
1059 		ath10k_warn(ar, "failed to synchronize setup for monitor vdev %i start: %d\n",
1060 			    vdev_id, ret);
1061 		return ret;
1062 	}
1063 
1064 	ret = ath10k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
1065 	if (ret) {
1066 		ath10k_warn(ar, "failed to put up monitor vdev %i: %d\n",
1067 			    vdev_id, ret);
1068 		goto vdev_stop;
1069 	}
1070 
1071 	ar->monitor_vdev_id = vdev_id;
1072 
1073 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i started\n",
1074 		   ar->monitor_vdev_id);
1075 	return 0;
1076 
1077 vdev_stop:
1078 	ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
1079 	if (ret)
1080 		ath10k_warn(ar, "failed to stop monitor vdev %i after start failure: %d\n",
1081 			    ar->monitor_vdev_id, ret);
1082 
1083 	return ret;
1084 }
1085 
1086 static int ath10k_monitor_vdev_stop(struct ath10k *ar)
1087 {
1088 	int ret = 0;
1089 
1090 	lockdep_assert_held(&ar->conf_mutex);
1091 
1092 	ret = ath10k_wmi_vdev_down(ar, ar->monitor_vdev_id);
1093 	if (ret)
1094 		ath10k_warn(ar, "failed to put down monitor vdev %i: %d\n",
1095 			    ar->monitor_vdev_id, ret);
1096 
1097 	reinit_completion(&ar->vdev_setup_done);
1098 	reinit_completion(&ar->vdev_delete_done);
1099 
1100 	ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
1101 	if (ret)
1102 		ath10k_warn(ar, "failed to request monitor vdev %i stop: %d\n",
1103 			    ar->monitor_vdev_id, ret);
1104 
1105 	ret = ath10k_vdev_setup_sync(ar);
1106 	if (ret)
1107 		ath10k_warn(ar, "failed to synchronize monitor vdev %i stop: %d\n",
1108 			    ar->monitor_vdev_id, ret);
1109 
1110 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i stopped\n",
1111 		   ar->monitor_vdev_id);
1112 	return ret;
1113 }
1114 
1115 static int ath10k_monitor_vdev_create(struct ath10k *ar)
1116 {
1117 	int bit, ret = 0;
1118 
1119 	lockdep_assert_held(&ar->conf_mutex);
1120 
1121 	if (ar->free_vdev_map == 0) {
1122 		ath10k_warn(ar, "failed to find free vdev id for monitor vdev\n");
1123 		return -ENOMEM;
1124 	}
1125 
1126 	bit = __ffs64(ar->free_vdev_map);
1127 
1128 	ar->monitor_vdev_id = bit;
1129 
1130 	ret = ath10k_wmi_vdev_create(ar, ar->monitor_vdev_id,
1131 				     WMI_VDEV_TYPE_MONITOR,
1132 				     0, ar->mac_addr);
1133 	if (ret) {
1134 		ath10k_warn(ar, "failed to request monitor vdev %i creation: %d\n",
1135 			    ar->monitor_vdev_id, ret);
1136 		return ret;
1137 	}
1138 
1139 	ar->free_vdev_map &= ~(1LL << ar->monitor_vdev_id);
1140 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d created\n",
1141 		   ar->monitor_vdev_id);
1142 
1143 	return 0;
1144 }
1145 
1146 static int ath10k_monitor_vdev_delete(struct ath10k *ar)
1147 {
1148 	int ret = 0;
1149 
1150 	lockdep_assert_held(&ar->conf_mutex);
1151 
1152 	ret = ath10k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
1153 	if (ret) {
1154 		ath10k_warn(ar, "failed to request wmi monitor vdev %i removal: %d\n",
1155 			    ar->monitor_vdev_id, ret);
1156 		return ret;
1157 	}
1158 
1159 	ar->free_vdev_map |= 1LL << ar->monitor_vdev_id;
1160 
1161 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d deleted\n",
1162 		   ar->monitor_vdev_id);
1163 	return ret;
1164 }
1165 
1166 static int ath10k_monitor_start(struct ath10k *ar)
1167 {
1168 	int ret;
1169 
1170 	lockdep_assert_held(&ar->conf_mutex);
1171 
1172 	ret = ath10k_monitor_vdev_create(ar);
1173 	if (ret) {
1174 		ath10k_warn(ar, "failed to create monitor vdev: %d\n", ret);
1175 		return ret;
1176 	}
1177 
1178 	ret = ath10k_monitor_vdev_start(ar, ar->monitor_vdev_id);
1179 	if (ret) {
1180 		ath10k_warn(ar, "failed to start monitor vdev: %d\n", ret);
1181 		ath10k_monitor_vdev_delete(ar);
1182 		return ret;
1183 	}
1184 
1185 	ar->monitor_started = true;
1186 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor started\n");
1187 
1188 	return 0;
1189 }
1190 
1191 static int ath10k_monitor_stop(struct ath10k *ar)
1192 {
1193 	int ret;
1194 
1195 	lockdep_assert_held(&ar->conf_mutex);
1196 
1197 	ret = ath10k_monitor_vdev_stop(ar);
1198 	if (ret) {
1199 		ath10k_warn(ar, "failed to stop monitor vdev: %d\n", ret);
1200 		return ret;
1201 	}
1202 
1203 	ret = ath10k_monitor_vdev_delete(ar);
1204 	if (ret) {
1205 		ath10k_warn(ar, "failed to delete monitor vdev: %d\n", ret);
1206 		return ret;
1207 	}
1208 
1209 	ar->monitor_started = false;
1210 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopped\n");
1211 
1212 	return 0;
1213 }
1214 
1215 static bool ath10k_mac_monitor_vdev_is_needed(struct ath10k *ar)
1216 {
1217 	int num_ctx;
1218 
1219 	/* At least one chanctx is required to derive a channel to start
1220 	 * monitor vdev on.
1221 	 */
1222 	num_ctx = ath10k_mac_num_chanctxs(ar);
1223 	if (num_ctx == 0)
1224 		return false;
1225 
1226 	/* If there's already an existing special monitor interface then don't
1227 	 * bother creating another monitor vdev.
1228 	 */
1229 	if (ar->monitor_arvif)
1230 		return false;
1231 
1232 	return ar->monitor ||
1233 	       (!test_bit(ATH10K_FW_FEATURE_ALLOWS_MESH_BCAST,
1234 			  ar->running_fw->fw_file.fw_features) &&
1235 		(ar->filter_flags & FIF_OTHER_BSS)) ||
1236 	       test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1237 }
1238 
1239 static bool ath10k_mac_monitor_vdev_is_allowed(struct ath10k *ar)
1240 {
1241 	int num_ctx;
1242 
1243 	num_ctx = ath10k_mac_num_chanctxs(ar);
1244 
1245 	/* FIXME: Current interface combinations and cfg80211/mac80211 code
1246 	 * shouldn't allow this but make sure to prevent handling the following
1247 	 * case anyway since multi-channel DFS hasn't been tested at all.
1248 	 */
1249 	if (test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags) && num_ctx > 1)
1250 		return false;
1251 
1252 	return true;
1253 }
1254 
1255 static int ath10k_monitor_recalc(struct ath10k *ar)
1256 {
1257 	bool needed;
1258 	bool allowed;
1259 	int ret;
1260 
1261 	lockdep_assert_held(&ar->conf_mutex);
1262 
1263 	needed = ath10k_mac_monitor_vdev_is_needed(ar);
1264 	allowed = ath10k_mac_monitor_vdev_is_allowed(ar);
1265 
1266 	ath10k_dbg(ar, ATH10K_DBG_MAC,
1267 		   "mac monitor recalc started? %d needed? %d allowed? %d\n",
1268 		   ar->monitor_started, needed, allowed);
1269 
1270 	if (WARN_ON(needed && !allowed)) {
1271 		if (ar->monitor_started) {
1272 			ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopping disallowed monitor\n");
1273 
1274 			ret = ath10k_monitor_stop(ar);
1275 			if (ret)
1276 				ath10k_warn(ar, "failed to stop disallowed monitor: %d\n",
1277 					    ret);
1278 				/* not serious */
1279 		}
1280 
1281 		return -EPERM;
1282 	}
1283 
1284 	if (needed == ar->monitor_started)
1285 		return 0;
1286 
1287 	if (needed)
1288 		return ath10k_monitor_start(ar);
1289 	else
1290 		return ath10k_monitor_stop(ar);
1291 }
1292 
1293 static bool ath10k_mac_can_set_cts_prot(struct ath10k_vif *arvif)
1294 {
1295 	struct ath10k *ar = arvif->ar;
1296 
1297 	lockdep_assert_held(&ar->conf_mutex);
1298 
1299 	if (!arvif->is_started) {
1300 		ath10k_dbg(ar, ATH10K_DBG_MAC, "defer cts setup, vdev is not ready yet\n");
1301 		return false;
1302 	}
1303 
1304 	return true;
1305 }
1306 
1307 static int ath10k_mac_set_cts_prot(struct ath10k_vif *arvif)
1308 {
1309 	struct ath10k *ar = arvif->ar;
1310 	u32 vdev_param;
1311 
1312 	lockdep_assert_held(&ar->conf_mutex);
1313 
1314 	vdev_param = ar->wmi.vdev_param->protection_mode;
1315 
1316 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d cts_protection %d\n",
1317 		   arvif->vdev_id, arvif->use_cts_prot);
1318 
1319 	return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
1320 					 arvif->use_cts_prot ? 1 : 0);
1321 }
1322 
1323 static int ath10k_recalc_rtscts_prot(struct ath10k_vif *arvif)
1324 {
1325 	struct ath10k *ar = arvif->ar;
1326 	u32 vdev_param, rts_cts = 0;
1327 
1328 	lockdep_assert_held(&ar->conf_mutex);
1329 
1330 	vdev_param = ar->wmi.vdev_param->enable_rtscts;
1331 
1332 	rts_cts |= SM(WMI_RTSCTS_ENABLED, WMI_RTSCTS_SET);
1333 
1334 	if (arvif->num_legacy_stations > 0)
1335 		rts_cts |= SM(WMI_RTSCTS_ACROSS_SW_RETRIES,
1336 			      WMI_RTSCTS_PROFILE);
1337 	else
1338 		rts_cts |= SM(WMI_RTSCTS_FOR_SECOND_RATESERIES,
1339 			      WMI_RTSCTS_PROFILE);
1340 
1341 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d recalc rts/cts prot %d\n",
1342 		   arvif->vdev_id, rts_cts);
1343 
1344 	return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
1345 					 rts_cts);
1346 }
1347 
1348 static int ath10k_start_cac(struct ath10k *ar)
1349 {
1350 	int ret;
1351 
1352 	lockdep_assert_held(&ar->conf_mutex);
1353 
1354 	set_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1355 
1356 	ret = ath10k_monitor_recalc(ar);
1357 	if (ret) {
1358 		ath10k_warn(ar, "failed to start monitor (cac): %d\n", ret);
1359 		clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1360 		return ret;
1361 	}
1362 
1363 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac start monitor vdev %d\n",
1364 		   ar->monitor_vdev_id);
1365 
1366 	return 0;
1367 }
1368 
1369 static int ath10k_stop_cac(struct ath10k *ar)
1370 {
1371 	lockdep_assert_held(&ar->conf_mutex);
1372 
1373 	/* CAC is not running - do nothing */
1374 	if (!test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags))
1375 		return 0;
1376 
1377 	clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1378 	ath10k_monitor_stop(ar);
1379 
1380 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac finished\n");
1381 
1382 	return 0;
1383 }
1384 
1385 static void ath10k_mac_has_radar_iter(struct ieee80211_hw *hw,
1386 				      struct ieee80211_chanctx_conf *conf,
1387 				      void *data)
1388 {
1389 	bool *ret = data;
1390 
1391 	if (!*ret && conf->radar_enabled)
1392 		*ret = true;
1393 }
1394 
1395 static bool ath10k_mac_has_radar_enabled(struct ath10k *ar)
1396 {
1397 	bool has_radar = false;
1398 
1399 	ieee80211_iter_chan_contexts_atomic(ar->hw,
1400 					    ath10k_mac_has_radar_iter,
1401 					    &has_radar);
1402 
1403 	return has_radar;
1404 }
1405 
1406 static void ath10k_recalc_radar_detection(struct ath10k *ar)
1407 {
1408 	int ret;
1409 
1410 	lockdep_assert_held(&ar->conf_mutex);
1411 
1412 	ath10k_stop_cac(ar);
1413 
1414 	if (!ath10k_mac_has_radar_enabled(ar))
1415 		return;
1416 
1417 	if (ar->num_started_vdevs > 0)
1418 		return;
1419 
1420 	ret = ath10k_start_cac(ar);
1421 	if (ret) {
1422 		/*
1423 		 * Not possible to start CAC on current channel so starting
1424 		 * radiation is not allowed, make this channel DFS_UNAVAILABLE
1425 		 * by indicating that radar was detected.
1426 		 */
1427 		ath10k_warn(ar, "failed to start CAC: %d\n", ret);
1428 		ieee80211_radar_detected(ar->hw);
1429 	}
1430 }
1431 
1432 static int ath10k_vdev_stop(struct ath10k_vif *arvif)
1433 {
1434 	struct ath10k *ar = arvif->ar;
1435 	int ret;
1436 
1437 	lockdep_assert_held(&ar->conf_mutex);
1438 
1439 	reinit_completion(&ar->vdev_setup_done);
1440 	reinit_completion(&ar->vdev_delete_done);
1441 
1442 	ret = ath10k_wmi_vdev_stop(ar, arvif->vdev_id);
1443 	if (ret) {
1444 		ath10k_warn(ar, "failed to stop WMI vdev %i: %d\n",
1445 			    arvif->vdev_id, ret);
1446 		return ret;
1447 	}
1448 
1449 	ret = ath10k_vdev_setup_sync(ar);
1450 	if (ret) {
1451 		ath10k_warn(ar, "failed to synchronize setup for vdev %i: %d\n",
1452 			    arvif->vdev_id, ret);
1453 		return ret;
1454 	}
1455 
1456 	WARN_ON(ar->num_started_vdevs == 0);
1457 
1458 	if (ar->num_started_vdevs != 0) {
1459 		ar->num_started_vdevs--;
1460 		ath10k_recalc_radar_detection(ar);
1461 	}
1462 
1463 	return ret;
1464 }
1465 
1466 static int ath10k_vdev_start_restart(struct ath10k_vif *arvif,
1467 				     const struct cfg80211_chan_def *chandef,
1468 				     bool restart)
1469 {
1470 	struct ath10k *ar = arvif->ar;
1471 	struct wmi_vdev_start_request_arg arg = {};
1472 	int ret = 0;
1473 
1474 	lockdep_assert_held(&ar->conf_mutex);
1475 
1476 	reinit_completion(&ar->vdev_setup_done);
1477 	reinit_completion(&ar->vdev_delete_done);
1478 
1479 	arg.vdev_id = arvif->vdev_id;
1480 	arg.dtim_period = arvif->dtim_period;
1481 	arg.bcn_intval = arvif->beacon_interval;
1482 
1483 	arg.channel.freq = chandef->chan->center_freq;
1484 	arg.channel.band_center_freq1 = chandef->center_freq1;
1485 	arg.channel.band_center_freq2 = chandef->center_freq2;
1486 	arg.channel.mode = chan_to_phymode(chandef);
1487 
1488 	arg.channel.min_power = 0;
1489 	arg.channel.max_power = chandef->chan->max_power * 2;
1490 	arg.channel.max_reg_power = chandef->chan->max_reg_power * 2;
1491 	arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
1492 
1493 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
1494 		arg.ssid = arvif->u.ap.ssid;
1495 		arg.ssid_len = arvif->u.ap.ssid_len;
1496 		arg.hidden_ssid = arvif->u.ap.hidden_ssid;
1497 
1498 		/* For now allow DFS for AP mode */
1499 		arg.channel.chan_radar =
1500 			!!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
1501 	} else if (arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
1502 		arg.ssid = arvif->vif->bss_conf.ssid;
1503 		arg.ssid_len = arvif->vif->bss_conf.ssid_len;
1504 	}
1505 
1506 	ath10k_dbg(ar, ATH10K_DBG_MAC,
1507 		   "mac vdev %d start center_freq %d phymode %s\n",
1508 		   arg.vdev_id, arg.channel.freq,
1509 		   ath10k_wmi_phymode_str(arg.channel.mode));
1510 
1511 	if (restart)
1512 		ret = ath10k_wmi_vdev_restart(ar, &arg);
1513 	else
1514 		ret = ath10k_wmi_vdev_start(ar, &arg);
1515 
1516 	if (ret) {
1517 		ath10k_warn(ar, "failed to start WMI vdev %i: %d\n",
1518 			    arg.vdev_id, ret);
1519 		return ret;
1520 	}
1521 
1522 	ret = ath10k_vdev_setup_sync(ar);
1523 	if (ret) {
1524 		ath10k_warn(ar,
1525 			    "failed to synchronize setup for vdev %i restart %d: %d\n",
1526 			    arg.vdev_id, restart, ret);
1527 		return ret;
1528 	}
1529 
1530 	ar->num_started_vdevs++;
1531 	ath10k_recalc_radar_detection(ar);
1532 
1533 	return ret;
1534 }
1535 
1536 static int ath10k_vdev_start(struct ath10k_vif *arvif,
1537 			     const struct cfg80211_chan_def *def)
1538 {
1539 	return ath10k_vdev_start_restart(arvif, def, false);
1540 }
1541 
1542 static int ath10k_vdev_restart(struct ath10k_vif *arvif,
1543 			       const struct cfg80211_chan_def *def)
1544 {
1545 	return ath10k_vdev_start_restart(arvif, def, true);
1546 }
1547 
1548 static int ath10k_mac_setup_bcn_p2p_ie(struct ath10k_vif *arvif,
1549 				       struct sk_buff *bcn)
1550 {
1551 	struct ath10k *ar = arvif->ar;
1552 	struct ieee80211_mgmt *mgmt;
1553 	const u8 *p2p_ie;
1554 	int ret;
1555 
1556 	if (arvif->vif->type != NL80211_IFTYPE_AP || !arvif->vif->p2p)
1557 		return 0;
1558 
1559 	mgmt = (void *)bcn->data;
1560 	p2p_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1561 					 mgmt->u.beacon.variable,
1562 					 bcn->len - (mgmt->u.beacon.variable -
1563 						     bcn->data));
1564 	if (!p2p_ie)
1565 		return -ENOENT;
1566 
1567 	ret = ath10k_wmi_p2p_go_bcn_ie(ar, arvif->vdev_id, p2p_ie);
1568 	if (ret) {
1569 		ath10k_warn(ar, "failed to submit p2p go bcn ie for vdev %i: %d\n",
1570 			    arvif->vdev_id, ret);
1571 		return ret;
1572 	}
1573 
1574 	return 0;
1575 }
1576 
1577 static int ath10k_mac_remove_vendor_ie(struct sk_buff *skb, unsigned int oui,
1578 				       u8 oui_type, size_t ie_offset)
1579 {
1580 	size_t len;
1581 	const u8 *next;
1582 	const u8 *end;
1583 	u8 *ie;
1584 
1585 	if (WARN_ON(skb->len < ie_offset))
1586 		return -EINVAL;
1587 
1588 	ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
1589 					   skb->data + ie_offset,
1590 					   skb->len - ie_offset);
1591 	if (!ie)
1592 		return -ENOENT;
1593 
1594 	len = ie[1] + 2;
1595 	end = skb->data + skb->len;
1596 	next = ie + len;
1597 
1598 	if (WARN_ON(next > end))
1599 		return -EINVAL;
1600 
1601 	memmove(ie, next, end - next);
1602 	skb_trim(skb, skb->len - len);
1603 
1604 	return 0;
1605 }
1606 
1607 static int ath10k_mac_setup_bcn_tmpl(struct ath10k_vif *arvif)
1608 {
1609 	struct ath10k *ar = arvif->ar;
1610 	struct ieee80211_hw *hw = ar->hw;
1611 	struct ieee80211_vif *vif = arvif->vif;
1612 	struct ieee80211_mutable_offsets offs = {};
1613 	struct sk_buff *bcn;
1614 	int ret;
1615 
1616 	if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1617 		return 0;
1618 
1619 	if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
1620 	    arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
1621 		return 0;
1622 
1623 	bcn = ieee80211_beacon_get_template(hw, vif, &offs);
1624 	if (!bcn) {
1625 		ath10k_warn(ar, "failed to get beacon template from mac80211\n");
1626 		return -EPERM;
1627 	}
1628 
1629 	ret = ath10k_mac_setup_bcn_p2p_ie(arvif, bcn);
1630 	if (ret) {
1631 		ath10k_warn(ar, "failed to setup p2p go bcn ie: %d\n", ret);
1632 		kfree_skb(bcn);
1633 		return ret;
1634 	}
1635 
1636 	/* P2P IE is inserted by firmware automatically (as configured above)
1637 	 * so remove it from the base beacon template to avoid duplicate P2P
1638 	 * IEs in beacon frames.
1639 	 */
1640 	ath10k_mac_remove_vendor_ie(bcn, WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1641 				    offsetof(struct ieee80211_mgmt,
1642 					     u.beacon.variable));
1643 
1644 	ret = ath10k_wmi_bcn_tmpl(ar, arvif->vdev_id, offs.tim_offset, bcn, 0,
1645 				  0, NULL, 0);
1646 	kfree_skb(bcn);
1647 
1648 	if (ret) {
1649 		ath10k_warn(ar, "failed to submit beacon template command: %d\n",
1650 			    ret);
1651 		return ret;
1652 	}
1653 
1654 	return 0;
1655 }
1656 
1657 static int ath10k_mac_setup_prb_tmpl(struct ath10k_vif *arvif)
1658 {
1659 	struct ath10k *ar = arvif->ar;
1660 	struct ieee80211_hw *hw = ar->hw;
1661 	struct ieee80211_vif *vif = arvif->vif;
1662 	struct sk_buff *prb;
1663 	int ret;
1664 
1665 	if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1666 		return 0;
1667 
1668 	if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1669 		return 0;
1670 
1671 	 /* For mesh, probe response and beacon share the same template */
1672 	if (ieee80211_vif_is_mesh(vif))
1673 		return 0;
1674 
1675 	prb = ieee80211_proberesp_get(hw, vif);
1676 	if (!prb) {
1677 		ath10k_warn(ar, "failed to get probe resp template from mac80211\n");
1678 		return -EPERM;
1679 	}
1680 
1681 	ret = ath10k_wmi_prb_tmpl(ar, arvif->vdev_id, prb);
1682 	kfree_skb(prb);
1683 
1684 	if (ret) {
1685 		ath10k_warn(ar, "failed to submit probe resp template command: %d\n",
1686 			    ret);
1687 		return ret;
1688 	}
1689 
1690 	return 0;
1691 }
1692 
1693 static int ath10k_mac_vif_fix_hidden_ssid(struct ath10k_vif *arvif)
1694 {
1695 	struct ath10k *ar = arvif->ar;
1696 	struct cfg80211_chan_def def;
1697 	int ret;
1698 
1699 	/* When originally vdev is started during assign_vif_chanctx() some
1700 	 * information is missing, notably SSID. Firmware revisions with beacon
1701 	 * offloading require the SSID to be provided during vdev (re)start to
1702 	 * handle hidden SSID properly.
1703 	 *
1704 	 * Vdev restart must be done after vdev has been both started and
1705 	 * upped. Otherwise some firmware revisions (at least 10.2) fail to
1706 	 * deliver vdev restart response event causing timeouts during vdev
1707 	 * syncing in ath10k.
1708 	 *
1709 	 * Note: The vdev down/up and template reinstallation could be skipped
1710 	 * since only wmi-tlv firmware are known to have beacon offload and
1711 	 * wmi-tlv doesn't seem to misbehave like 10.2 wrt vdev restart
1712 	 * response delivery. It's probably more robust to keep it as is.
1713 	 */
1714 	if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1715 		return 0;
1716 
1717 	if (WARN_ON(!arvif->is_started))
1718 		return -EINVAL;
1719 
1720 	if (WARN_ON(!arvif->is_up))
1721 		return -EINVAL;
1722 
1723 	if (WARN_ON(ath10k_mac_vif_chan(arvif->vif, &def)))
1724 		return -EINVAL;
1725 
1726 	ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
1727 	if (ret) {
1728 		ath10k_warn(ar, "failed to bring down ap vdev %i: %d\n",
1729 			    arvif->vdev_id, ret);
1730 		return ret;
1731 	}
1732 
1733 	/* Vdev down reset beacon & presp templates. Reinstall them. Otherwise
1734 	 * firmware will crash upon vdev up.
1735 	 */
1736 
1737 	ret = ath10k_mac_setup_bcn_tmpl(arvif);
1738 	if (ret) {
1739 		ath10k_warn(ar, "failed to update beacon template: %d\n", ret);
1740 		return ret;
1741 	}
1742 
1743 	ret = ath10k_mac_setup_prb_tmpl(arvif);
1744 	if (ret) {
1745 		ath10k_warn(ar, "failed to update presp template: %d\n", ret);
1746 		return ret;
1747 	}
1748 
1749 	ret = ath10k_vdev_restart(arvif, &def);
1750 	if (ret) {
1751 		ath10k_warn(ar, "failed to restart ap vdev %i: %d\n",
1752 			    arvif->vdev_id, ret);
1753 		return ret;
1754 	}
1755 
1756 	ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
1757 				 arvif->bssid);
1758 	if (ret) {
1759 		ath10k_warn(ar, "failed to bring up ap vdev %i: %d\n",
1760 			    arvif->vdev_id, ret);
1761 		return ret;
1762 	}
1763 
1764 	return 0;
1765 }
1766 
1767 static void ath10k_control_beaconing(struct ath10k_vif *arvif,
1768 				     struct ieee80211_bss_conf *info)
1769 {
1770 	struct ath10k *ar = arvif->ar;
1771 	int ret = 0;
1772 
1773 	lockdep_assert_held(&arvif->ar->conf_mutex);
1774 
1775 	if (!info->enable_beacon) {
1776 		ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
1777 		if (ret)
1778 			ath10k_warn(ar, "failed to down vdev_id %i: %d\n",
1779 				    arvif->vdev_id, ret);
1780 
1781 		arvif->is_up = false;
1782 
1783 		spin_lock_bh(&arvif->ar->data_lock);
1784 		ath10k_mac_vif_beacon_free(arvif);
1785 		spin_unlock_bh(&arvif->ar->data_lock);
1786 
1787 		return;
1788 	}
1789 
1790 	arvif->tx_seq_no = 0x1000;
1791 
1792 	arvif->aid = 0;
1793 	ether_addr_copy(arvif->bssid, info->bssid);
1794 
1795 	ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
1796 				 arvif->bssid);
1797 	if (ret) {
1798 		ath10k_warn(ar, "failed to bring up vdev %d: %i\n",
1799 			    arvif->vdev_id, ret);
1800 		return;
1801 	}
1802 
1803 	arvif->is_up = true;
1804 
1805 	ret = ath10k_mac_vif_fix_hidden_ssid(arvif);
1806 	if (ret) {
1807 		ath10k_warn(ar, "failed to fix hidden ssid for vdev %i, expect trouble: %d\n",
1808 			    arvif->vdev_id, ret);
1809 		return;
1810 	}
1811 
1812 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
1813 }
1814 
1815 static void ath10k_control_ibss(struct ath10k_vif *arvif,
1816 				struct ieee80211_bss_conf *info,
1817 				const u8 self_peer[ETH_ALEN])
1818 {
1819 	struct ath10k *ar = arvif->ar;
1820 	u32 vdev_param;
1821 	int ret = 0;
1822 
1823 	lockdep_assert_held(&arvif->ar->conf_mutex);
1824 
1825 	if (!info->ibss_joined) {
1826 		if (is_zero_ether_addr(arvif->bssid))
1827 			return;
1828 
1829 		eth_zero_addr(arvif->bssid);
1830 
1831 		return;
1832 	}
1833 
1834 	vdev_param = arvif->ar->wmi.vdev_param->atim_window;
1835 	ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id, vdev_param,
1836 					ATH10K_DEFAULT_ATIM);
1837 	if (ret)
1838 		ath10k_warn(ar, "failed to set IBSS ATIM for vdev %d: %d\n",
1839 			    arvif->vdev_id, ret);
1840 }
1841 
1842 static int ath10k_mac_vif_recalc_ps_wake_threshold(struct ath10k_vif *arvif)
1843 {
1844 	struct ath10k *ar = arvif->ar;
1845 	u32 param;
1846 	u32 value;
1847 	int ret;
1848 
1849 	lockdep_assert_held(&arvif->ar->conf_mutex);
1850 
1851 	if (arvif->u.sta.uapsd)
1852 		value = WMI_STA_PS_TX_WAKE_THRESHOLD_NEVER;
1853 	else
1854 		value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
1855 
1856 	param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
1857 	ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param, value);
1858 	if (ret) {
1859 		ath10k_warn(ar, "failed to submit ps wake threshold %u on vdev %i: %d\n",
1860 			    value, arvif->vdev_id, ret);
1861 		return ret;
1862 	}
1863 
1864 	return 0;
1865 }
1866 
1867 static int ath10k_mac_vif_recalc_ps_poll_count(struct ath10k_vif *arvif)
1868 {
1869 	struct ath10k *ar = arvif->ar;
1870 	u32 param;
1871 	u32 value;
1872 	int ret;
1873 
1874 	lockdep_assert_held(&arvif->ar->conf_mutex);
1875 
1876 	if (arvif->u.sta.uapsd)
1877 		value = WMI_STA_PS_PSPOLL_COUNT_UAPSD;
1878 	else
1879 		value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
1880 
1881 	param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
1882 	ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
1883 					  param, value);
1884 	if (ret) {
1885 		ath10k_warn(ar, "failed to submit ps poll count %u on vdev %i: %d\n",
1886 			    value, arvif->vdev_id, ret);
1887 		return ret;
1888 	}
1889 
1890 	return 0;
1891 }
1892 
1893 static int ath10k_mac_num_vifs_started(struct ath10k *ar)
1894 {
1895 	struct ath10k_vif *arvif;
1896 	int num = 0;
1897 
1898 	lockdep_assert_held(&ar->conf_mutex);
1899 
1900 	list_for_each_entry(arvif, &ar->arvifs, list)
1901 		if (arvif->is_started)
1902 			num++;
1903 
1904 	return num;
1905 }
1906 
1907 static int ath10k_mac_vif_setup_ps(struct ath10k_vif *arvif)
1908 {
1909 	struct ath10k *ar = arvif->ar;
1910 	struct ieee80211_vif *vif = arvif->vif;
1911 	struct ieee80211_conf *conf = &ar->hw->conf;
1912 	enum wmi_sta_powersave_param param;
1913 	enum wmi_sta_ps_mode psmode;
1914 	int ret;
1915 	int ps_timeout;
1916 	bool enable_ps;
1917 
1918 	lockdep_assert_held(&arvif->ar->conf_mutex);
1919 
1920 	if (arvif->vif->type != NL80211_IFTYPE_STATION)
1921 		return 0;
1922 
1923 	enable_ps = arvif->ps;
1924 
1925 	if (enable_ps && ath10k_mac_num_vifs_started(ar) > 1 &&
1926 	    !test_bit(ATH10K_FW_FEATURE_MULTI_VIF_PS_SUPPORT,
1927 		      ar->running_fw->fw_file.fw_features)) {
1928 		ath10k_warn(ar, "refusing to enable ps on vdev %i: not supported by fw\n",
1929 			    arvif->vdev_id);
1930 		enable_ps = false;
1931 	}
1932 
1933 	if (!arvif->is_started) {
1934 		/* mac80211 can update vif powersave state while disconnected.
1935 		 * Firmware doesn't behave nicely and consumes more power than
1936 		 * necessary if PS is disabled on a non-started vdev. Hence
1937 		 * force-enable PS for non-running vdevs.
1938 		 */
1939 		psmode = WMI_STA_PS_MODE_ENABLED;
1940 	} else if (enable_ps) {
1941 		psmode = WMI_STA_PS_MODE_ENABLED;
1942 		param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
1943 
1944 		ps_timeout = conf->dynamic_ps_timeout;
1945 		if (ps_timeout == 0) {
1946 			/* Firmware doesn't like 0 */
1947 			ps_timeout = ieee80211_tu_to_usec(
1948 				vif->bss_conf.beacon_int) / 1000;
1949 		}
1950 
1951 		ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
1952 						  ps_timeout);
1953 		if (ret) {
1954 			ath10k_warn(ar, "failed to set inactivity time for vdev %d: %i\n",
1955 				    arvif->vdev_id, ret);
1956 			return ret;
1957 		}
1958 	} else {
1959 		psmode = WMI_STA_PS_MODE_DISABLED;
1960 	}
1961 
1962 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d psmode %s\n",
1963 		   arvif->vdev_id, psmode ? "enable" : "disable");
1964 
1965 	ret = ath10k_wmi_set_psmode(ar, arvif->vdev_id, psmode);
1966 	if (ret) {
1967 		ath10k_warn(ar, "failed to set PS Mode %d for vdev %d: %d\n",
1968 			    psmode, arvif->vdev_id, ret);
1969 		return ret;
1970 	}
1971 
1972 	return 0;
1973 }
1974 
1975 static int ath10k_mac_vif_disable_keepalive(struct ath10k_vif *arvif)
1976 {
1977 	struct ath10k *ar = arvif->ar;
1978 	struct wmi_sta_keepalive_arg arg = {};
1979 	int ret;
1980 
1981 	lockdep_assert_held(&arvif->ar->conf_mutex);
1982 
1983 	if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
1984 		return 0;
1985 
1986 	if (!test_bit(WMI_SERVICE_STA_KEEP_ALIVE, ar->wmi.svc_map))
1987 		return 0;
1988 
1989 	/* Some firmware revisions have a bug and ignore the `enabled` field.
1990 	 * Instead use the interval to disable the keepalive.
1991 	 */
1992 	arg.vdev_id = arvif->vdev_id;
1993 	arg.enabled = 1;
1994 	arg.method = WMI_STA_KEEPALIVE_METHOD_NULL_FRAME;
1995 	arg.interval = WMI_STA_KEEPALIVE_INTERVAL_DISABLE;
1996 
1997 	ret = ath10k_wmi_sta_keepalive(ar, &arg);
1998 	if (ret) {
1999 		ath10k_warn(ar, "failed to submit keepalive on vdev %i: %d\n",
2000 			    arvif->vdev_id, ret);
2001 		return ret;
2002 	}
2003 
2004 	return 0;
2005 }
2006 
2007 static void ath10k_mac_vif_ap_csa_count_down(struct ath10k_vif *arvif)
2008 {
2009 	struct ath10k *ar = arvif->ar;
2010 	struct ieee80211_vif *vif = arvif->vif;
2011 	int ret;
2012 
2013 	lockdep_assert_held(&arvif->ar->conf_mutex);
2014 
2015 	if (WARN_ON(!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)))
2016 		return;
2017 
2018 	if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
2019 		return;
2020 
2021 	if (!vif->csa_active)
2022 		return;
2023 
2024 	if (!arvif->is_up)
2025 		return;
2026 
2027 	if (!ieee80211_csa_is_complete(vif)) {
2028 		ieee80211_csa_update_counter(vif);
2029 
2030 		ret = ath10k_mac_setup_bcn_tmpl(arvif);
2031 		if (ret)
2032 			ath10k_warn(ar, "failed to update bcn tmpl during csa: %d\n",
2033 				    ret);
2034 
2035 		ret = ath10k_mac_setup_prb_tmpl(arvif);
2036 		if (ret)
2037 			ath10k_warn(ar, "failed to update prb tmpl during csa: %d\n",
2038 				    ret);
2039 	} else {
2040 		ieee80211_csa_finish(vif);
2041 	}
2042 }
2043 
2044 static void ath10k_mac_vif_ap_csa_work(struct work_struct *work)
2045 {
2046 	struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
2047 						ap_csa_work);
2048 	struct ath10k *ar = arvif->ar;
2049 
2050 	mutex_lock(&ar->conf_mutex);
2051 	ath10k_mac_vif_ap_csa_count_down(arvif);
2052 	mutex_unlock(&ar->conf_mutex);
2053 }
2054 
2055 static void ath10k_mac_handle_beacon_iter(void *data, u8 *mac,
2056 					  struct ieee80211_vif *vif)
2057 {
2058 	struct sk_buff *skb = data;
2059 	struct ieee80211_mgmt *mgmt = (void *)skb->data;
2060 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
2061 
2062 	if (vif->type != NL80211_IFTYPE_STATION)
2063 		return;
2064 
2065 	if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
2066 		return;
2067 
2068 	cancel_delayed_work(&arvif->connection_loss_work);
2069 }
2070 
2071 void ath10k_mac_handle_beacon(struct ath10k *ar, struct sk_buff *skb)
2072 {
2073 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
2074 						   IEEE80211_IFACE_ITER_NORMAL,
2075 						   ath10k_mac_handle_beacon_iter,
2076 						   skb);
2077 }
2078 
2079 static void ath10k_mac_handle_beacon_miss_iter(void *data, u8 *mac,
2080 					       struct ieee80211_vif *vif)
2081 {
2082 	u32 *vdev_id = data;
2083 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
2084 	struct ath10k *ar = arvif->ar;
2085 	struct ieee80211_hw *hw = ar->hw;
2086 
2087 	if (arvif->vdev_id != *vdev_id)
2088 		return;
2089 
2090 	if (!arvif->is_up)
2091 		return;
2092 
2093 	ieee80211_beacon_loss(vif);
2094 
2095 	/* Firmware doesn't report beacon loss events repeatedly. If AP probe
2096 	 * (done by mac80211) succeeds but beacons do not resume then it
2097 	 * doesn't make sense to continue operation. Queue connection loss work
2098 	 * which can be cancelled when beacon is received.
2099 	 */
2100 	ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
2101 				     ATH10K_CONNECTION_LOSS_HZ);
2102 }
2103 
2104 void ath10k_mac_handle_beacon_miss(struct ath10k *ar, u32 vdev_id)
2105 {
2106 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
2107 						   IEEE80211_IFACE_ITER_NORMAL,
2108 						   ath10k_mac_handle_beacon_miss_iter,
2109 						   &vdev_id);
2110 }
2111 
2112 static void ath10k_mac_vif_sta_connection_loss_work(struct work_struct *work)
2113 {
2114 	struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
2115 						connection_loss_work.work);
2116 	struct ieee80211_vif *vif = arvif->vif;
2117 
2118 	if (!arvif->is_up)
2119 		return;
2120 
2121 	ieee80211_connection_loss(vif);
2122 }
2123 
2124 /**********************/
2125 /* Station management */
2126 /**********************/
2127 
2128 static u32 ath10k_peer_assoc_h_listen_intval(struct ath10k *ar,
2129 					     struct ieee80211_vif *vif)
2130 {
2131 	/* Some firmware revisions have unstable STA powersave when listen
2132 	 * interval is set too high (e.g. 5). The symptoms are firmware doesn't
2133 	 * generate NullFunc frames properly even if buffered frames have been
2134 	 * indicated in Beacon TIM. Firmware would seldom wake up to pull
2135 	 * buffered frames. Often pinging the device from AP would simply fail.
2136 	 *
2137 	 * As a workaround set it to 1.
2138 	 */
2139 	if (vif->type == NL80211_IFTYPE_STATION)
2140 		return 1;
2141 
2142 	return ar->hw->conf.listen_interval;
2143 }
2144 
2145 static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
2146 				      struct ieee80211_vif *vif,
2147 				      struct ieee80211_sta *sta,
2148 				      struct wmi_peer_assoc_complete_arg *arg)
2149 {
2150 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
2151 	u32 aid;
2152 
2153 	lockdep_assert_held(&ar->conf_mutex);
2154 
2155 	if (vif->type == NL80211_IFTYPE_STATION)
2156 		aid = vif->bss_conf.aid;
2157 	else
2158 		aid = sta->aid;
2159 
2160 	ether_addr_copy(arg->addr, sta->addr);
2161 	arg->vdev_id = arvif->vdev_id;
2162 	arg->peer_aid = aid;
2163 	arg->peer_flags |= arvif->ar->wmi.peer_flags->auth;
2164 	arg->peer_listen_intval = ath10k_peer_assoc_h_listen_intval(ar, vif);
2165 	arg->peer_num_spatial_streams = 1;
2166 	arg->peer_caps = vif->bss_conf.assoc_capability;
2167 }
2168 
2169 static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
2170 				       struct ieee80211_vif *vif,
2171 				       struct ieee80211_sta *sta,
2172 				       struct wmi_peer_assoc_complete_arg *arg)
2173 {
2174 	struct ieee80211_bss_conf *info = &vif->bss_conf;
2175 	struct cfg80211_chan_def def;
2176 	struct cfg80211_bss *bss;
2177 	const u8 *rsnie = NULL;
2178 	const u8 *wpaie = NULL;
2179 
2180 	lockdep_assert_held(&ar->conf_mutex);
2181 
2182 	if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2183 		return;
2184 
2185 	bss = cfg80211_get_bss(ar->hw->wiphy, def.chan, info->bssid, NULL, 0,
2186 			       IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
2187 	if (bss) {
2188 		const struct cfg80211_bss_ies *ies;
2189 
2190 		rcu_read_lock();
2191 		rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
2192 
2193 		ies = rcu_dereference(bss->ies);
2194 
2195 		wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
2196 						WLAN_OUI_TYPE_MICROSOFT_WPA,
2197 						ies->data,
2198 						ies->len);
2199 		rcu_read_unlock();
2200 		cfg80211_put_bss(ar->hw->wiphy, bss);
2201 	}
2202 
2203 	/* FIXME: base on RSN IE/WPA IE is a correct idea? */
2204 	if (rsnie || wpaie) {
2205 		ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
2206 		arg->peer_flags |= ar->wmi.peer_flags->need_ptk_4_way;
2207 	}
2208 
2209 	if (wpaie) {
2210 		ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: wpa ie found\n", __func__);
2211 		arg->peer_flags |= ar->wmi.peer_flags->need_gtk_2_way;
2212 	}
2213 
2214 	if (sta->mfp &&
2215 	    test_bit(ATH10K_FW_FEATURE_MFP_SUPPORT,
2216 		     ar->running_fw->fw_file.fw_features)) {
2217 		arg->peer_flags |= ar->wmi.peer_flags->pmf;
2218 	}
2219 }
2220 
2221 static void ath10k_peer_assoc_h_rates(struct ath10k *ar,
2222 				      struct ieee80211_vif *vif,
2223 				      struct ieee80211_sta *sta,
2224 				      struct wmi_peer_assoc_complete_arg *arg)
2225 {
2226 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
2227 	struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
2228 	struct cfg80211_chan_def def;
2229 	const struct ieee80211_supported_band *sband;
2230 	const struct ieee80211_rate *rates;
2231 	enum nl80211_band band;
2232 	u32 ratemask;
2233 	u8 rate;
2234 	int i;
2235 
2236 	lockdep_assert_held(&ar->conf_mutex);
2237 
2238 	if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2239 		return;
2240 
2241 	band = def.chan->band;
2242 	sband = ar->hw->wiphy->bands[band];
2243 	ratemask = sta->supp_rates[band];
2244 	ratemask &= arvif->bitrate_mask.control[band].legacy;
2245 	rates = sband->bitrates;
2246 
2247 	rateset->num_rates = 0;
2248 
2249 	for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
2250 		if (!(ratemask & 1))
2251 			continue;
2252 
2253 		rate = ath10k_mac_bitrate_to_rate(rates->bitrate);
2254 		rateset->rates[rateset->num_rates] = rate;
2255 		rateset->num_rates++;
2256 	}
2257 }
2258 
2259 static bool
2260 ath10k_peer_assoc_h_ht_masked(const u8 ht_mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
2261 {
2262 	int nss;
2263 
2264 	for (nss = 0; nss < IEEE80211_HT_MCS_MASK_LEN; nss++)
2265 		if (ht_mcs_mask[nss])
2266 			return false;
2267 
2268 	return true;
2269 }
2270 
2271 static bool
2272 ath10k_peer_assoc_h_vht_masked(const u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
2273 {
2274 	int nss;
2275 
2276 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++)
2277 		if (vht_mcs_mask[nss])
2278 			return false;
2279 
2280 	return true;
2281 }
2282 
2283 static void ath10k_peer_assoc_h_ht(struct ath10k *ar,
2284 				   struct ieee80211_vif *vif,
2285 				   struct ieee80211_sta *sta,
2286 				   struct wmi_peer_assoc_complete_arg *arg)
2287 {
2288 	const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
2289 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
2290 	struct cfg80211_chan_def def;
2291 	enum nl80211_band band;
2292 	const u8 *ht_mcs_mask;
2293 	const u16 *vht_mcs_mask;
2294 	int i, n;
2295 	u8 max_nss;
2296 	u32 stbc;
2297 
2298 	lockdep_assert_held(&ar->conf_mutex);
2299 
2300 	if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2301 		return;
2302 
2303 	if (!ht_cap->ht_supported)
2304 		return;
2305 
2306 	band = def.chan->band;
2307 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2308 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2309 
2310 	if (ath10k_peer_assoc_h_ht_masked(ht_mcs_mask) &&
2311 	    ath10k_peer_assoc_h_vht_masked(vht_mcs_mask))
2312 		return;
2313 
2314 	arg->peer_flags |= ar->wmi.peer_flags->ht;
2315 	arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2316 				    ht_cap->ampdu_factor)) - 1;
2317 
2318 	arg->peer_mpdu_density =
2319 		ath10k_parse_mpdudensity(ht_cap->ampdu_density);
2320 
2321 	arg->peer_ht_caps = ht_cap->cap;
2322 	arg->peer_rate_caps |= WMI_RC_HT_FLAG;
2323 
2324 	if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
2325 		arg->peer_flags |= ar->wmi.peer_flags->ldbc;
2326 
2327 	if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
2328 		arg->peer_flags |= ar->wmi.peer_flags->bw40;
2329 		arg->peer_rate_caps |= WMI_RC_CW40_FLAG;
2330 	}
2331 
2332 	if (arvif->bitrate_mask.control[band].gi != NL80211_TXRATE_FORCE_LGI) {
2333 		if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
2334 			arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
2335 
2336 		if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
2337 			arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
2338 	}
2339 
2340 	if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
2341 		arg->peer_rate_caps |= WMI_RC_TX_STBC_FLAG;
2342 		arg->peer_flags |= ar->wmi.peer_flags->stbc;
2343 	}
2344 
2345 	if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
2346 		stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
2347 		stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
2348 		stbc = stbc << WMI_RC_RX_STBC_FLAG_S;
2349 		arg->peer_rate_caps |= stbc;
2350 		arg->peer_flags |= ar->wmi.peer_flags->stbc;
2351 	}
2352 
2353 	if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
2354 		arg->peer_rate_caps |= WMI_RC_TS_FLAG;
2355 	else if (ht_cap->mcs.rx_mask[1])
2356 		arg->peer_rate_caps |= WMI_RC_DS_FLAG;
2357 
2358 	for (i = 0, n = 0, max_nss = 0; i < IEEE80211_HT_MCS_MASK_LEN * 8; i++)
2359 		if ((ht_cap->mcs.rx_mask[i / 8] & BIT(i % 8)) &&
2360 		    (ht_mcs_mask[i / 8] & BIT(i % 8))) {
2361 			max_nss = (i / 8) + 1;
2362 			arg->peer_ht_rates.rates[n++] = i;
2363 		}
2364 
2365 	/*
2366 	 * This is a workaround for HT-enabled STAs which break the spec
2367 	 * and have no HT capabilities RX mask (no HT RX MCS map).
2368 	 *
2369 	 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
2370 	 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
2371 	 *
2372 	 * Firmware asserts if such situation occurs.
2373 	 */
2374 	if (n == 0) {
2375 		arg->peer_ht_rates.num_rates = 8;
2376 		for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
2377 			arg->peer_ht_rates.rates[i] = i;
2378 	} else {
2379 		arg->peer_ht_rates.num_rates = n;
2380 		arg->peer_num_spatial_streams = min(sta->rx_nss, max_nss);
2381 	}
2382 
2383 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
2384 		   arg->addr,
2385 		   arg->peer_ht_rates.num_rates,
2386 		   arg->peer_num_spatial_streams);
2387 }
2388 
2389 static int ath10k_peer_assoc_qos_ap(struct ath10k *ar,
2390 				    struct ath10k_vif *arvif,
2391 				    struct ieee80211_sta *sta)
2392 {
2393 	u32 uapsd = 0;
2394 	u32 max_sp = 0;
2395 	int ret = 0;
2396 
2397 	lockdep_assert_held(&ar->conf_mutex);
2398 
2399 	if (sta->wme && sta->uapsd_queues) {
2400 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
2401 			   sta->uapsd_queues, sta->max_sp);
2402 
2403 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
2404 			uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
2405 				 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
2406 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
2407 			uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
2408 				 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
2409 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
2410 			uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
2411 				 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
2412 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
2413 			uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
2414 				 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
2415 
2416 		if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
2417 			max_sp = sta->max_sp;
2418 
2419 		ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
2420 						 sta->addr,
2421 						 WMI_AP_PS_PEER_PARAM_UAPSD,
2422 						 uapsd);
2423 		if (ret) {
2424 			ath10k_warn(ar, "failed to set ap ps peer param uapsd for vdev %i: %d\n",
2425 				    arvif->vdev_id, ret);
2426 			return ret;
2427 		}
2428 
2429 		ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
2430 						 sta->addr,
2431 						 WMI_AP_PS_PEER_PARAM_MAX_SP,
2432 						 max_sp);
2433 		if (ret) {
2434 			ath10k_warn(ar, "failed to set ap ps peer param max sp for vdev %i: %d\n",
2435 				    arvif->vdev_id, ret);
2436 			return ret;
2437 		}
2438 
2439 		/* TODO setup this based on STA listen interval and
2440 		 * beacon interval. Currently we don't know
2441 		 * sta->listen_interval - mac80211 patch required.
2442 		 * Currently use 10 seconds
2443 		 */
2444 		ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id, sta->addr,
2445 						 WMI_AP_PS_PEER_PARAM_AGEOUT_TIME,
2446 						 10);
2447 		if (ret) {
2448 			ath10k_warn(ar, "failed to set ap ps peer param ageout time for vdev %i: %d\n",
2449 				    arvif->vdev_id, ret);
2450 			return ret;
2451 		}
2452 	}
2453 
2454 	return 0;
2455 }
2456 
2457 static u16
2458 ath10k_peer_assoc_h_vht_limit(u16 tx_mcs_set,
2459 			      const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])
2460 {
2461 	int idx_limit;
2462 	int nss;
2463 	u16 mcs_map;
2464 	u16 mcs;
2465 
2466 	for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
2467 		mcs_map = ath10k_mac_get_max_vht_mcs_map(tx_mcs_set, nss) &
2468 			  vht_mcs_limit[nss];
2469 
2470 		if (mcs_map)
2471 			idx_limit = fls(mcs_map) - 1;
2472 		else
2473 			idx_limit = -1;
2474 
2475 		switch (idx_limit) {
2476 		case 0: /* fall through */
2477 		case 1: /* fall through */
2478 		case 2: /* fall through */
2479 		case 3: /* fall through */
2480 		case 4: /* fall through */
2481 		case 5: /* fall through */
2482 		case 6: /* fall through */
2483 		default:
2484 			/* see ath10k_mac_can_set_bitrate_mask() */
2485 			WARN_ON(1);
2486 			/* fall through */
2487 		case -1:
2488 			mcs = IEEE80211_VHT_MCS_NOT_SUPPORTED;
2489 			break;
2490 		case 7:
2491 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_7;
2492 			break;
2493 		case 8:
2494 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_8;
2495 			break;
2496 		case 9:
2497 			mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
2498 			break;
2499 		}
2500 
2501 		tx_mcs_set &= ~(0x3 << (nss * 2));
2502 		tx_mcs_set |= mcs << (nss * 2);
2503 	}
2504 
2505 	return tx_mcs_set;
2506 }
2507 
2508 static u32 get_160mhz_nss_from_maxrate(int rate)
2509 {
2510 	u32 nss;
2511 
2512 	switch (rate) {
2513 	case 780:
2514 		nss = 1;
2515 		break;
2516 	case 1560:
2517 		nss = 2;
2518 		break;
2519 	case 2106:
2520 		nss = 3; /* not support MCS9 from spec*/
2521 		break;
2522 	case 3120:
2523 		nss = 4;
2524 		break;
2525 	default:
2526 		 nss = 1;
2527 	}
2528 
2529 	return nss;
2530 }
2531 
2532 static void ath10k_peer_assoc_h_vht(struct ath10k *ar,
2533 				    struct ieee80211_vif *vif,
2534 				    struct ieee80211_sta *sta,
2535 				    struct wmi_peer_assoc_complete_arg *arg)
2536 {
2537 	const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
2538 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
2539 	struct ath10k_hw_params *hw = &ar->hw_params;
2540 	struct cfg80211_chan_def def;
2541 	enum nl80211_band band;
2542 	const u16 *vht_mcs_mask;
2543 	u8 ampdu_factor;
2544 	u8 max_nss, vht_mcs;
2545 	int i;
2546 
2547 	if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2548 		return;
2549 
2550 	if (!vht_cap->vht_supported)
2551 		return;
2552 
2553 	band = def.chan->band;
2554 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2555 
2556 	if (ath10k_peer_assoc_h_vht_masked(vht_mcs_mask))
2557 		return;
2558 
2559 	arg->peer_flags |= ar->wmi.peer_flags->vht;
2560 
2561 	if (def.chan->band == NL80211_BAND_2GHZ)
2562 		arg->peer_flags |= ar->wmi.peer_flags->vht_2g;
2563 
2564 	arg->peer_vht_caps = vht_cap->cap;
2565 
2566 	ampdu_factor = (vht_cap->cap &
2567 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
2568 		       IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
2569 
2570 	/* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
2571 	 * zero in VHT IE. Using it would result in degraded throughput.
2572 	 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
2573 	 * it if VHT max_mpdu is smaller.
2574 	 */
2575 	arg->peer_max_mpdu = max(arg->peer_max_mpdu,
2576 				 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2577 					ampdu_factor)) - 1);
2578 
2579 	if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
2580 		arg->peer_flags |= ar->wmi.peer_flags->bw80;
2581 
2582 	if (sta->bandwidth == IEEE80211_STA_RX_BW_160)
2583 		arg->peer_flags |= ar->wmi.peer_flags->bw160;
2584 
2585 	/* Calculate peer NSS capability from VHT capabilities if STA
2586 	 * supports VHT.
2587 	 */
2588 	for (i = 0, max_nss = 0, vht_mcs = 0; i < NL80211_VHT_NSS_MAX; i++) {
2589 		vht_mcs = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) >>
2590 			  (2 * i) & 3;
2591 
2592 		if ((vht_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED) &&
2593 		    vht_mcs_mask[i])
2594 			max_nss = i + 1;
2595 	}
2596 	arg->peer_num_spatial_streams = min(sta->rx_nss, max_nss);
2597 	arg->peer_vht_rates.rx_max_rate =
2598 		__le16_to_cpu(vht_cap->vht_mcs.rx_highest);
2599 	arg->peer_vht_rates.rx_mcs_set =
2600 		__le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
2601 	arg->peer_vht_rates.tx_max_rate =
2602 		__le16_to_cpu(vht_cap->vht_mcs.tx_highest);
2603 	arg->peer_vht_rates.tx_mcs_set = ath10k_peer_assoc_h_vht_limit(
2604 		__le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map), vht_mcs_mask);
2605 
2606 	/* Configure bandwidth-NSS mapping to FW
2607 	 * for the chip's tx chains setting on 160Mhz bw
2608 	 */
2609 	if (arg->peer_phymode == MODE_11AC_VHT160 ||
2610 	    arg->peer_phymode == MODE_11AC_VHT80_80) {
2611 		u32 rx_nss;
2612 		u32 max_rate;
2613 
2614 		max_rate = arg->peer_vht_rates.rx_max_rate;
2615 		rx_nss = get_160mhz_nss_from_maxrate(max_rate);
2616 
2617 		if (rx_nss == 0)
2618 			rx_nss = arg->peer_num_spatial_streams;
2619 		else
2620 			rx_nss = min(arg->peer_num_spatial_streams, rx_nss);
2621 
2622 		max_rate = hw->vht160_mcs_tx_highest;
2623 		rx_nss = min(rx_nss, get_160mhz_nss_from_maxrate(max_rate));
2624 
2625 		arg->peer_bw_rxnss_override =
2626 			FIELD_PREP(WMI_PEER_NSS_MAP_ENABLE, 1) |
2627 			FIELD_PREP(WMI_PEER_NSS_160MHZ_MASK, (rx_nss - 1));
2628 
2629 		if (arg->peer_phymode == MODE_11AC_VHT80_80) {
2630 			arg->peer_bw_rxnss_override |=
2631 			FIELD_PREP(WMI_PEER_NSS_80_80MHZ_MASK, (rx_nss - 1));
2632 		}
2633 	}
2634 	ath10k_dbg(ar, ATH10K_DBG_MAC,
2635 		   "mac vht peer %pM max_mpdu %d flags 0x%x peer_rx_nss_override 0x%x\n",
2636 		   sta->addr, arg->peer_max_mpdu,
2637 		   arg->peer_flags, arg->peer_bw_rxnss_override);
2638 }
2639 
2640 static void ath10k_peer_assoc_h_qos(struct ath10k *ar,
2641 				    struct ieee80211_vif *vif,
2642 				    struct ieee80211_sta *sta,
2643 				    struct wmi_peer_assoc_complete_arg *arg)
2644 {
2645 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
2646 
2647 	switch (arvif->vdev_type) {
2648 	case WMI_VDEV_TYPE_AP:
2649 		if (sta->wme)
2650 			arg->peer_flags |= arvif->ar->wmi.peer_flags->qos;
2651 
2652 		if (sta->wme && sta->uapsd_queues) {
2653 			arg->peer_flags |= arvif->ar->wmi.peer_flags->apsd;
2654 			arg->peer_rate_caps |= WMI_RC_UAPSD_FLAG;
2655 		}
2656 		break;
2657 	case WMI_VDEV_TYPE_STA:
2658 		if (sta->wme)
2659 			arg->peer_flags |= arvif->ar->wmi.peer_flags->qos;
2660 		break;
2661 	case WMI_VDEV_TYPE_IBSS:
2662 		if (sta->wme)
2663 			arg->peer_flags |= arvif->ar->wmi.peer_flags->qos;
2664 		break;
2665 	default:
2666 		break;
2667 	}
2668 
2669 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM qos %d\n",
2670 		   sta->addr, !!(arg->peer_flags &
2671 		   arvif->ar->wmi.peer_flags->qos));
2672 }
2673 
2674 static bool ath10k_mac_sta_has_ofdm_only(struct ieee80211_sta *sta)
2675 {
2676 	return sta->supp_rates[NL80211_BAND_2GHZ] >>
2677 	       ATH10K_MAC_FIRST_OFDM_RATE_IDX;
2678 }
2679 
2680 static enum wmi_phy_mode ath10k_mac_get_phymode_vht(struct ath10k *ar,
2681 						    struct ieee80211_sta *sta)
2682 {
2683 	if (sta->bandwidth == IEEE80211_STA_RX_BW_160) {
2684 		switch (sta->vht_cap.cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK) {
2685 		case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
2686 			return MODE_11AC_VHT160;
2687 		case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
2688 			return MODE_11AC_VHT80_80;
2689 		default:
2690 			/* not sure if this is a valid case? */
2691 			return MODE_11AC_VHT160;
2692 		}
2693 	}
2694 
2695 	if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
2696 		return MODE_11AC_VHT80;
2697 
2698 	if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2699 		return MODE_11AC_VHT40;
2700 
2701 	if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
2702 		return MODE_11AC_VHT20;
2703 
2704 	return MODE_UNKNOWN;
2705 }
2706 
2707 static void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
2708 					struct ieee80211_vif *vif,
2709 					struct ieee80211_sta *sta,
2710 					struct wmi_peer_assoc_complete_arg *arg)
2711 {
2712 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
2713 	struct cfg80211_chan_def def;
2714 	enum nl80211_band band;
2715 	const u8 *ht_mcs_mask;
2716 	const u16 *vht_mcs_mask;
2717 	enum wmi_phy_mode phymode = MODE_UNKNOWN;
2718 
2719 	if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2720 		return;
2721 
2722 	band = def.chan->band;
2723 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2724 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2725 
2726 	switch (band) {
2727 	case NL80211_BAND_2GHZ:
2728 		if (sta->vht_cap.vht_supported &&
2729 		    !ath10k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
2730 			if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2731 				phymode = MODE_11AC_VHT40;
2732 			else
2733 				phymode = MODE_11AC_VHT20;
2734 		} else if (sta->ht_cap.ht_supported &&
2735 			   !ath10k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
2736 			if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2737 				phymode = MODE_11NG_HT40;
2738 			else
2739 				phymode = MODE_11NG_HT20;
2740 		} else if (ath10k_mac_sta_has_ofdm_only(sta)) {
2741 			phymode = MODE_11G;
2742 		} else {
2743 			phymode = MODE_11B;
2744 		}
2745 
2746 		break;
2747 	case NL80211_BAND_5GHZ:
2748 		/*
2749 		 * Check VHT first.
2750 		 */
2751 		if (sta->vht_cap.vht_supported &&
2752 		    !ath10k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
2753 			phymode = ath10k_mac_get_phymode_vht(ar, sta);
2754 		} else if (sta->ht_cap.ht_supported &&
2755 			   !ath10k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
2756 			if (sta->bandwidth >= IEEE80211_STA_RX_BW_40)
2757 				phymode = MODE_11NA_HT40;
2758 			else
2759 				phymode = MODE_11NA_HT20;
2760 		} else {
2761 			phymode = MODE_11A;
2762 		}
2763 
2764 		break;
2765 	default:
2766 		break;
2767 	}
2768 
2769 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM phymode %s\n",
2770 		   sta->addr, ath10k_wmi_phymode_str(phymode));
2771 
2772 	arg->peer_phymode = phymode;
2773 	WARN_ON(phymode == MODE_UNKNOWN);
2774 }
2775 
2776 static int ath10k_peer_assoc_prepare(struct ath10k *ar,
2777 				     struct ieee80211_vif *vif,
2778 				     struct ieee80211_sta *sta,
2779 				     struct wmi_peer_assoc_complete_arg *arg)
2780 {
2781 	lockdep_assert_held(&ar->conf_mutex);
2782 
2783 	memset(arg, 0, sizeof(*arg));
2784 
2785 	ath10k_peer_assoc_h_basic(ar, vif, sta, arg);
2786 	ath10k_peer_assoc_h_crypto(ar, vif, sta, arg);
2787 	ath10k_peer_assoc_h_rates(ar, vif, sta, arg);
2788 	ath10k_peer_assoc_h_ht(ar, vif, sta, arg);
2789 	ath10k_peer_assoc_h_phymode(ar, vif, sta, arg);
2790 	ath10k_peer_assoc_h_vht(ar, vif, sta, arg);
2791 	ath10k_peer_assoc_h_qos(ar, vif, sta, arg);
2792 
2793 	return 0;
2794 }
2795 
2796 static const u32 ath10k_smps_map[] = {
2797 	[WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
2798 	[WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
2799 	[WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
2800 	[WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
2801 };
2802 
2803 static int ath10k_setup_peer_smps(struct ath10k *ar, struct ath10k_vif *arvif,
2804 				  const u8 *addr,
2805 				  const struct ieee80211_sta_ht_cap *ht_cap)
2806 {
2807 	int smps;
2808 
2809 	if (!ht_cap->ht_supported)
2810 		return 0;
2811 
2812 	smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
2813 	smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
2814 
2815 	if (smps >= ARRAY_SIZE(ath10k_smps_map))
2816 		return -EINVAL;
2817 
2818 	return ath10k_wmi_peer_set_param(ar, arvif->vdev_id, addr,
2819 					 ar->wmi.peer_param->smps_state,
2820 					 ath10k_smps_map[smps]);
2821 }
2822 
2823 static int ath10k_mac_vif_recalc_txbf(struct ath10k *ar,
2824 				      struct ieee80211_vif *vif,
2825 				      struct ieee80211_sta_vht_cap vht_cap)
2826 {
2827 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
2828 	int ret;
2829 	u32 param;
2830 	u32 value;
2831 
2832 	if (ath10k_wmi_get_txbf_conf_scheme(ar) != WMI_TXBF_CONF_AFTER_ASSOC)
2833 		return 0;
2834 
2835 	if (!(ar->vht_cap_info &
2836 	      (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
2837 	       IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
2838 	       IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
2839 	       IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)))
2840 		return 0;
2841 
2842 	param = ar->wmi.vdev_param->txbf;
2843 	value = 0;
2844 
2845 	if (WARN_ON(param == WMI_VDEV_PARAM_UNSUPPORTED))
2846 		return 0;
2847 
2848 	/* The following logic is correct. If a remote STA advertises support
2849 	 * for being a beamformer then we should enable us being a beamformee.
2850 	 */
2851 
2852 	if (ar->vht_cap_info &
2853 	    (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
2854 	     IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)) {
2855 		if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)
2856 			value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
2857 
2858 		if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)
2859 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
2860 	}
2861 
2862 	if (ar->vht_cap_info &
2863 	    (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
2864 	     IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)) {
2865 		if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)
2866 			value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
2867 
2868 		if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)
2869 			value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
2870 	}
2871 
2872 	if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFEE)
2873 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
2874 
2875 	if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFER)
2876 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
2877 
2878 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param, value);
2879 	if (ret) {
2880 		ath10k_warn(ar, "failed to submit vdev param txbf 0x%x: %d\n",
2881 			    value, ret);
2882 		return ret;
2883 	}
2884 
2885 	return 0;
2886 }
2887 
2888 /* can be called only in mac80211 callbacks due to `key_count` usage */
2889 static void ath10k_bss_assoc(struct ieee80211_hw *hw,
2890 			     struct ieee80211_vif *vif,
2891 			     struct ieee80211_bss_conf *bss_conf)
2892 {
2893 	struct ath10k *ar = hw->priv;
2894 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
2895 	struct ieee80211_sta_ht_cap ht_cap;
2896 	struct ieee80211_sta_vht_cap vht_cap;
2897 	struct wmi_peer_assoc_complete_arg peer_arg;
2898 	struct ieee80211_sta *ap_sta;
2899 	int ret;
2900 
2901 	lockdep_assert_held(&ar->conf_mutex);
2902 
2903 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
2904 		   arvif->vdev_id, arvif->bssid, arvif->aid);
2905 
2906 	rcu_read_lock();
2907 
2908 	ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
2909 	if (!ap_sta) {
2910 		ath10k_warn(ar, "failed to find station entry for bss %pM vdev %i\n",
2911 			    bss_conf->bssid, arvif->vdev_id);
2912 		rcu_read_unlock();
2913 		return;
2914 	}
2915 
2916 	/* ap_sta must be accessed only within rcu section which must be left
2917 	 * before calling ath10k_setup_peer_smps() which might sleep.
2918 	 */
2919 	ht_cap = ap_sta->ht_cap;
2920 	vht_cap = ap_sta->vht_cap;
2921 
2922 	ret = ath10k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg);
2923 	if (ret) {
2924 		ath10k_warn(ar, "failed to prepare peer assoc for %pM vdev %i: %d\n",
2925 			    bss_conf->bssid, arvif->vdev_id, ret);
2926 		rcu_read_unlock();
2927 		return;
2928 	}
2929 
2930 	rcu_read_unlock();
2931 
2932 	ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
2933 	if (ret) {
2934 		ath10k_warn(ar, "failed to run peer assoc for %pM vdev %i: %d\n",
2935 			    bss_conf->bssid, arvif->vdev_id, ret);
2936 		return;
2937 	}
2938 
2939 	ret = ath10k_setup_peer_smps(ar, arvif, bss_conf->bssid, &ht_cap);
2940 	if (ret) {
2941 		ath10k_warn(ar, "failed to setup peer SMPS for vdev %i: %d\n",
2942 			    arvif->vdev_id, ret);
2943 		return;
2944 	}
2945 
2946 	ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
2947 	if (ret) {
2948 		ath10k_warn(ar, "failed to recalc txbf for vdev %i on bss %pM: %d\n",
2949 			    arvif->vdev_id, bss_conf->bssid, ret);
2950 		return;
2951 	}
2952 
2953 	ath10k_dbg(ar, ATH10K_DBG_MAC,
2954 		   "mac vdev %d up (associated) bssid %pM aid %d\n",
2955 		   arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
2956 
2957 	WARN_ON(arvif->is_up);
2958 
2959 	arvif->aid = bss_conf->aid;
2960 	ether_addr_copy(arvif->bssid, bss_conf->bssid);
2961 
2962 	ret = ath10k_wmi_pdev_set_param(ar,
2963 					ar->wmi.pdev_param->peer_stats_info_enable, 1);
2964 	if (ret)
2965 		ath10k_warn(ar, "failed to enable peer stats info: %d\n", ret);
2966 
2967 	ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
2968 	if (ret) {
2969 		ath10k_warn(ar, "failed to set vdev %d up: %d\n",
2970 			    arvif->vdev_id, ret);
2971 		return;
2972 	}
2973 
2974 	arvif->is_up = true;
2975 
2976 	/* Workaround: Some firmware revisions (tested with qca6174
2977 	 * WLAN.RM.2.0-00073) have buggy powersave state machine and must be
2978 	 * poked with peer param command.
2979 	 */
2980 	ret = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, arvif->bssid,
2981 					ar->wmi.peer_param->dummy_var, 1);
2982 	if (ret) {
2983 		ath10k_warn(ar, "failed to poke peer %pM param for ps workaround on vdev %i: %d\n",
2984 			    arvif->bssid, arvif->vdev_id, ret);
2985 		return;
2986 	}
2987 }
2988 
2989 static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
2990 				struct ieee80211_vif *vif)
2991 {
2992 	struct ath10k *ar = hw->priv;
2993 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
2994 	struct ieee80211_sta_vht_cap vht_cap = {};
2995 	int ret;
2996 
2997 	lockdep_assert_held(&ar->conf_mutex);
2998 
2999 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
3000 		   arvif->vdev_id, arvif->bssid);
3001 
3002 	ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
3003 	if (ret)
3004 		ath10k_warn(ar, "failed to down vdev %i: %d\n",
3005 			    arvif->vdev_id, ret);
3006 
3007 	arvif->def_wep_key_idx = -1;
3008 
3009 	ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
3010 	if (ret) {
3011 		ath10k_warn(ar, "failed to recalc txbf for vdev %i: %d\n",
3012 			    arvif->vdev_id, ret);
3013 		return;
3014 	}
3015 
3016 	arvif->is_up = false;
3017 
3018 	cancel_delayed_work_sync(&arvif->connection_loss_work);
3019 }
3020 
3021 static int ath10k_station_assoc(struct ath10k *ar,
3022 				struct ieee80211_vif *vif,
3023 				struct ieee80211_sta *sta,
3024 				bool reassoc)
3025 {
3026 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
3027 	struct wmi_peer_assoc_complete_arg peer_arg;
3028 	int ret = 0;
3029 
3030 	lockdep_assert_held(&ar->conf_mutex);
3031 
3032 	ret = ath10k_peer_assoc_prepare(ar, vif, sta, &peer_arg);
3033 	if (ret) {
3034 		ath10k_warn(ar, "failed to prepare WMI peer assoc for %pM vdev %i: %i\n",
3035 			    sta->addr, arvif->vdev_id, ret);
3036 		return ret;
3037 	}
3038 
3039 	ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
3040 	if (ret) {
3041 		ath10k_warn(ar, "failed to run peer assoc for STA %pM vdev %i: %d\n",
3042 			    sta->addr, arvif->vdev_id, ret);
3043 		return ret;
3044 	}
3045 
3046 	/* Re-assoc is run only to update supported rates for given station. It
3047 	 * doesn't make much sense to reconfigure the peer completely.
3048 	 */
3049 	if (!reassoc) {
3050 		ret = ath10k_setup_peer_smps(ar, arvif, sta->addr,
3051 					     &sta->ht_cap);
3052 		if (ret) {
3053 			ath10k_warn(ar, "failed to setup peer SMPS for vdev %d: %d\n",
3054 				    arvif->vdev_id, ret);
3055 			return ret;
3056 		}
3057 
3058 		ret = ath10k_peer_assoc_qos_ap(ar, arvif, sta);
3059 		if (ret) {
3060 			ath10k_warn(ar, "failed to set qos params for STA %pM for vdev %i: %d\n",
3061 				    sta->addr, arvif->vdev_id, ret);
3062 			return ret;
3063 		}
3064 
3065 		if (!sta->wme) {
3066 			arvif->num_legacy_stations++;
3067 			ret  = ath10k_recalc_rtscts_prot(arvif);
3068 			if (ret) {
3069 				ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
3070 					    arvif->vdev_id, ret);
3071 				return ret;
3072 			}
3073 		}
3074 
3075 		/* Plumb cached keys only for static WEP */
3076 		if ((arvif->def_wep_key_idx != -1) && (!sta->tdls)) {
3077 			ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
3078 			if (ret) {
3079 				ath10k_warn(ar, "failed to install peer wep keys for vdev %i: %d\n",
3080 					    arvif->vdev_id, ret);
3081 				return ret;
3082 			}
3083 		}
3084 	}
3085 
3086 	return ret;
3087 }
3088 
3089 static int ath10k_station_disassoc(struct ath10k *ar,
3090 				   struct ieee80211_vif *vif,
3091 				   struct ieee80211_sta *sta)
3092 {
3093 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
3094 	int ret = 0;
3095 
3096 	lockdep_assert_held(&ar->conf_mutex);
3097 
3098 	if (!sta->wme) {
3099 		arvif->num_legacy_stations--;
3100 		ret = ath10k_recalc_rtscts_prot(arvif);
3101 		if (ret) {
3102 			ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
3103 				    arvif->vdev_id, ret);
3104 			return ret;
3105 		}
3106 	}
3107 
3108 	ret = ath10k_clear_peer_keys(arvif, sta->addr);
3109 	if (ret) {
3110 		ath10k_warn(ar, "failed to clear all peer wep keys for vdev %i: %d\n",
3111 			    arvif->vdev_id, ret);
3112 		return ret;
3113 	}
3114 
3115 	return ret;
3116 }
3117 
3118 /**************/
3119 /* Regulatory */
3120 /**************/
3121 
3122 static int ath10k_update_channel_list(struct ath10k *ar)
3123 {
3124 	struct ieee80211_hw *hw = ar->hw;
3125 	struct ieee80211_supported_band **bands;
3126 	enum nl80211_band band;
3127 	struct ieee80211_channel *channel;
3128 	struct wmi_scan_chan_list_arg arg = {0};
3129 	struct wmi_channel_arg *ch;
3130 	bool passive;
3131 	int len;
3132 	int ret;
3133 	int i;
3134 
3135 	lockdep_assert_held(&ar->conf_mutex);
3136 
3137 	bands = hw->wiphy->bands;
3138 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
3139 		if (!bands[band])
3140 			continue;
3141 
3142 		for (i = 0; i < bands[band]->n_channels; i++) {
3143 			if (bands[band]->channels[i].flags &
3144 			    IEEE80211_CHAN_DISABLED)
3145 				continue;
3146 
3147 			arg.n_channels++;
3148 		}
3149 	}
3150 
3151 	len = sizeof(struct wmi_channel_arg) * arg.n_channels;
3152 	arg.channels = kzalloc(len, GFP_KERNEL);
3153 	if (!arg.channels)
3154 		return -ENOMEM;
3155 
3156 	ch = arg.channels;
3157 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
3158 		if (!bands[band])
3159 			continue;
3160 
3161 		for (i = 0; i < bands[band]->n_channels; i++) {
3162 			channel = &bands[band]->channels[i];
3163 
3164 			if (channel->flags & IEEE80211_CHAN_DISABLED)
3165 				continue;
3166 
3167 			ch->allow_ht = true;
3168 
3169 			/* FIXME: when should we really allow VHT? */
3170 			ch->allow_vht = true;
3171 
3172 			ch->allow_ibss =
3173 				!(channel->flags & IEEE80211_CHAN_NO_IR);
3174 
3175 			ch->ht40plus =
3176 				!(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
3177 
3178 			ch->chan_radar =
3179 				!!(channel->flags & IEEE80211_CHAN_RADAR);
3180 
3181 			passive = channel->flags & IEEE80211_CHAN_NO_IR;
3182 			ch->passive = passive;
3183 
3184 			/* the firmware is ignoring the "radar" flag of the
3185 			 * channel and is scanning actively using Probe Requests
3186 			 * on "Radar detection"/DFS channels which are not
3187 			 * marked as "available"
3188 			 */
3189 			ch->passive |= ch->chan_radar;
3190 
3191 			ch->freq = channel->center_freq;
3192 			ch->band_center_freq1 = channel->center_freq;
3193 			ch->min_power = 0;
3194 			ch->max_power = channel->max_power * 2;
3195 			ch->max_reg_power = channel->max_reg_power * 2;
3196 			ch->max_antenna_gain = channel->max_antenna_gain * 2;
3197 			ch->reg_class_id = 0; /* FIXME */
3198 
3199 			/* FIXME: why use only legacy modes, why not any
3200 			 * HT/VHT modes? Would that even make any
3201 			 * difference?
3202 			 */
3203 			if (channel->band == NL80211_BAND_2GHZ)
3204 				ch->mode = MODE_11G;
3205 			else
3206 				ch->mode = MODE_11A;
3207 
3208 			if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
3209 				continue;
3210 
3211 			ath10k_dbg(ar, ATH10K_DBG_WMI,
3212 				   "mac channel [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
3213 				    ch - arg.channels, arg.n_channels,
3214 				   ch->freq, ch->max_power, ch->max_reg_power,
3215 				   ch->max_antenna_gain, ch->mode);
3216 
3217 			ch++;
3218 		}
3219 	}
3220 
3221 	ret = ath10k_wmi_scan_chan_list(ar, &arg);
3222 	kfree(arg.channels);
3223 
3224 	return ret;
3225 }
3226 
3227 static enum wmi_dfs_region
3228 ath10k_mac_get_dfs_region(enum nl80211_dfs_regions dfs_region)
3229 {
3230 	switch (dfs_region) {
3231 	case NL80211_DFS_UNSET:
3232 		return WMI_UNINIT_DFS_DOMAIN;
3233 	case NL80211_DFS_FCC:
3234 		return WMI_FCC_DFS_DOMAIN;
3235 	case NL80211_DFS_ETSI:
3236 		return WMI_ETSI_DFS_DOMAIN;
3237 	case NL80211_DFS_JP:
3238 		return WMI_MKK4_DFS_DOMAIN;
3239 	}
3240 	return WMI_UNINIT_DFS_DOMAIN;
3241 }
3242 
3243 static void ath10k_regd_update(struct ath10k *ar)
3244 {
3245 	struct reg_dmn_pair_mapping *regpair;
3246 	int ret;
3247 	enum wmi_dfs_region wmi_dfs_reg;
3248 	enum nl80211_dfs_regions nl_dfs_reg;
3249 
3250 	lockdep_assert_held(&ar->conf_mutex);
3251 
3252 	ret = ath10k_update_channel_list(ar);
3253 	if (ret)
3254 		ath10k_warn(ar, "failed to update channel list: %d\n", ret);
3255 
3256 	regpair = ar->ath_common.regulatory.regpair;
3257 
3258 	if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
3259 		nl_dfs_reg = ar->dfs_detector->region;
3260 		wmi_dfs_reg = ath10k_mac_get_dfs_region(nl_dfs_reg);
3261 	} else {
3262 		wmi_dfs_reg = WMI_UNINIT_DFS_DOMAIN;
3263 	}
3264 
3265 	/* Target allows setting up per-band regdomain but ath_common provides
3266 	 * a combined one only
3267 	 */
3268 	ret = ath10k_wmi_pdev_set_regdomain(ar,
3269 					    regpair->reg_domain,
3270 					    regpair->reg_domain, /* 2ghz */
3271 					    regpair->reg_domain, /* 5ghz */
3272 					    regpair->reg_2ghz_ctl,
3273 					    regpair->reg_5ghz_ctl,
3274 					    wmi_dfs_reg);
3275 	if (ret)
3276 		ath10k_warn(ar, "failed to set pdev regdomain: %d\n", ret);
3277 }
3278 
3279 static void ath10k_mac_update_channel_list(struct ath10k *ar,
3280 					   struct ieee80211_supported_band *band)
3281 {
3282 	int i;
3283 
3284 	if (ar->low_5ghz_chan && ar->high_5ghz_chan) {
3285 		for (i = 0; i < band->n_channels; i++) {
3286 			if (band->channels[i].center_freq < ar->low_5ghz_chan ||
3287 			    band->channels[i].center_freq > ar->high_5ghz_chan)
3288 				band->channels[i].flags |=
3289 					IEEE80211_CHAN_DISABLED;
3290 		}
3291 	}
3292 }
3293 
3294 static void ath10k_reg_notifier(struct wiphy *wiphy,
3295 				struct regulatory_request *request)
3296 {
3297 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
3298 	struct ath10k *ar = hw->priv;
3299 	bool result;
3300 
3301 	ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
3302 
3303 	if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
3304 		ath10k_dbg(ar, ATH10K_DBG_REGULATORY, "dfs region 0x%x\n",
3305 			   request->dfs_region);
3306 		result = ar->dfs_detector->set_dfs_domain(ar->dfs_detector,
3307 							  request->dfs_region);
3308 		if (!result)
3309 			ath10k_warn(ar, "DFS region 0x%X not supported, will trigger radar for every pulse\n",
3310 				    request->dfs_region);
3311 	}
3312 
3313 	mutex_lock(&ar->conf_mutex);
3314 	if (ar->state == ATH10K_STATE_ON)
3315 		ath10k_regd_update(ar);
3316 	mutex_unlock(&ar->conf_mutex);
3317 
3318 	if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY)
3319 		ath10k_mac_update_channel_list(ar,
3320 					       ar->hw->wiphy->bands[NL80211_BAND_5GHZ]);
3321 }
3322 
3323 static void ath10k_stop_radar_confirmation(struct ath10k *ar)
3324 {
3325 	spin_lock_bh(&ar->data_lock);
3326 	ar->radar_conf_state = ATH10K_RADAR_CONFIRMATION_STOPPED;
3327 	spin_unlock_bh(&ar->data_lock);
3328 
3329 	cancel_work_sync(&ar->radar_confirmation_work);
3330 }
3331 
3332 /***************/
3333 /* TX handlers */
3334 /***************/
3335 
3336 enum ath10k_mac_tx_path {
3337 	ATH10K_MAC_TX_HTT,
3338 	ATH10K_MAC_TX_HTT_MGMT,
3339 	ATH10K_MAC_TX_WMI_MGMT,
3340 	ATH10K_MAC_TX_UNKNOWN,
3341 };
3342 
3343 void ath10k_mac_tx_lock(struct ath10k *ar, int reason)
3344 {
3345 	lockdep_assert_held(&ar->htt.tx_lock);
3346 
3347 	WARN_ON(reason >= ATH10K_TX_PAUSE_MAX);
3348 	ar->tx_paused |= BIT(reason);
3349 	ieee80211_stop_queues(ar->hw);
3350 }
3351 
3352 static void ath10k_mac_tx_unlock_iter(void *data, u8 *mac,
3353 				      struct ieee80211_vif *vif)
3354 {
3355 	struct ath10k *ar = data;
3356 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
3357 
3358 	if (arvif->tx_paused)
3359 		return;
3360 
3361 	ieee80211_wake_queue(ar->hw, arvif->vdev_id);
3362 }
3363 
3364 void ath10k_mac_tx_unlock(struct ath10k *ar, int reason)
3365 {
3366 	lockdep_assert_held(&ar->htt.tx_lock);
3367 
3368 	WARN_ON(reason >= ATH10K_TX_PAUSE_MAX);
3369 	ar->tx_paused &= ~BIT(reason);
3370 
3371 	if (ar->tx_paused)
3372 		return;
3373 
3374 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
3375 						   IEEE80211_IFACE_ITER_RESUME_ALL,
3376 						   ath10k_mac_tx_unlock_iter,
3377 						   ar);
3378 
3379 	ieee80211_wake_queue(ar->hw, ar->hw->offchannel_tx_hw_queue);
3380 }
3381 
3382 void ath10k_mac_vif_tx_lock(struct ath10k_vif *arvif, int reason)
3383 {
3384 	struct ath10k *ar = arvif->ar;
3385 
3386 	lockdep_assert_held(&ar->htt.tx_lock);
3387 
3388 	WARN_ON(reason >= BITS_PER_LONG);
3389 	arvif->tx_paused |= BIT(reason);
3390 	ieee80211_stop_queue(ar->hw, arvif->vdev_id);
3391 }
3392 
3393 void ath10k_mac_vif_tx_unlock(struct ath10k_vif *arvif, int reason)
3394 {
3395 	struct ath10k *ar = arvif->ar;
3396 
3397 	lockdep_assert_held(&ar->htt.tx_lock);
3398 
3399 	WARN_ON(reason >= BITS_PER_LONG);
3400 	arvif->tx_paused &= ~BIT(reason);
3401 
3402 	if (ar->tx_paused)
3403 		return;
3404 
3405 	if (arvif->tx_paused)
3406 		return;
3407 
3408 	ieee80211_wake_queue(ar->hw, arvif->vdev_id);
3409 }
3410 
3411 static void ath10k_mac_vif_handle_tx_pause(struct ath10k_vif *arvif,
3412 					   enum wmi_tlv_tx_pause_id pause_id,
3413 					   enum wmi_tlv_tx_pause_action action)
3414 {
3415 	struct ath10k *ar = arvif->ar;
3416 
3417 	lockdep_assert_held(&ar->htt.tx_lock);
3418 
3419 	switch (action) {
3420 	case WMI_TLV_TX_PAUSE_ACTION_STOP:
3421 		ath10k_mac_vif_tx_lock(arvif, pause_id);
3422 		break;
3423 	case WMI_TLV_TX_PAUSE_ACTION_WAKE:
3424 		ath10k_mac_vif_tx_unlock(arvif, pause_id);
3425 		break;
3426 	default:
3427 		ath10k_dbg(ar, ATH10K_DBG_BOOT,
3428 			   "received unknown tx pause action %d on vdev %i, ignoring\n",
3429 			    action, arvif->vdev_id);
3430 		break;
3431 	}
3432 }
3433 
3434 struct ath10k_mac_tx_pause {
3435 	u32 vdev_id;
3436 	enum wmi_tlv_tx_pause_id pause_id;
3437 	enum wmi_tlv_tx_pause_action action;
3438 };
3439 
3440 static void ath10k_mac_handle_tx_pause_iter(void *data, u8 *mac,
3441 					    struct ieee80211_vif *vif)
3442 {
3443 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
3444 	struct ath10k_mac_tx_pause *arg = data;
3445 
3446 	if (arvif->vdev_id != arg->vdev_id)
3447 		return;
3448 
3449 	ath10k_mac_vif_handle_tx_pause(arvif, arg->pause_id, arg->action);
3450 }
3451 
3452 void ath10k_mac_handle_tx_pause_vdev(struct ath10k *ar, u32 vdev_id,
3453 				     enum wmi_tlv_tx_pause_id pause_id,
3454 				     enum wmi_tlv_tx_pause_action action)
3455 {
3456 	struct ath10k_mac_tx_pause arg = {
3457 		.vdev_id = vdev_id,
3458 		.pause_id = pause_id,
3459 		.action = action,
3460 	};
3461 
3462 	spin_lock_bh(&ar->htt.tx_lock);
3463 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
3464 						   IEEE80211_IFACE_ITER_RESUME_ALL,
3465 						   ath10k_mac_handle_tx_pause_iter,
3466 						   &arg);
3467 	spin_unlock_bh(&ar->htt.tx_lock);
3468 }
3469 
3470 static enum ath10k_hw_txrx_mode
3471 ath10k_mac_tx_h_get_txmode(struct ath10k *ar,
3472 			   struct ieee80211_vif *vif,
3473 			   struct ieee80211_sta *sta,
3474 			   struct sk_buff *skb)
3475 {
3476 	const struct ieee80211_hdr *hdr = (void *)skb->data;
3477 	const struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(skb);
3478 	__le16 fc = hdr->frame_control;
3479 
3480 	if (!vif || vif->type == NL80211_IFTYPE_MONITOR)
3481 		return ATH10K_HW_TXRX_RAW;
3482 
3483 	if (ieee80211_is_mgmt(fc))
3484 		return ATH10K_HW_TXRX_MGMT;
3485 
3486 	/* Workaround:
3487 	 *
3488 	 * NullFunc frames are mostly used to ping if a client or AP are still
3489 	 * reachable and responsive. This implies tx status reports must be
3490 	 * accurate - otherwise either mac80211 or userspace (e.g. hostapd) can
3491 	 * come to a conclusion that the other end disappeared and tear down
3492 	 * BSS connection or it can never disconnect from BSS/client (which is
3493 	 * the case).
3494 	 *
3495 	 * Firmware with HTT older than 3.0 delivers incorrect tx status for
3496 	 * NullFunc frames to driver. However there's a HTT Mgmt Tx command
3497 	 * which seems to deliver correct tx reports for NullFunc frames. The
3498 	 * downside of using it is it ignores client powersave state so it can
3499 	 * end up disconnecting sleeping clients in AP mode. It should fix STA
3500 	 * mode though because AP don't sleep.
3501 	 */
3502 	if (ar->htt.target_version_major < 3 &&
3503 	    (ieee80211_is_nullfunc(fc) || ieee80211_is_qos_nullfunc(fc)) &&
3504 	    !test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
3505 		      ar->running_fw->fw_file.fw_features))
3506 		return ATH10K_HW_TXRX_MGMT;
3507 
3508 	/* Workaround:
3509 	 *
3510 	 * Some wmi-tlv firmwares for qca6174 have broken Tx key selection for
3511 	 * NativeWifi txmode - it selects AP key instead of peer key. It seems
3512 	 * to work with Ethernet txmode so use it.
3513 	 *
3514 	 * FIXME: Check if raw mode works with TDLS.
3515 	 */
3516 	if (ieee80211_is_data_present(fc) && sta && sta->tdls)
3517 		return ATH10K_HW_TXRX_ETHERNET;
3518 
3519 	if (test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags) ||
3520 	    skb_cb->flags & ATH10K_SKB_F_RAW_TX)
3521 		return ATH10K_HW_TXRX_RAW;
3522 
3523 	return ATH10K_HW_TXRX_NATIVE_WIFI;
3524 }
3525 
3526 static bool ath10k_tx_h_use_hwcrypto(struct ieee80211_vif *vif,
3527 				     struct sk_buff *skb)
3528 {
3529 	const struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3530 	const struct ieee80211_hdr *hdr = (void *)skb->data;
3531 	const u32 mask = IEEE80211_TX_INTFL_DONT_ENCRYPT |
3532 			 IEEE80211_TX_CTL_INJECTED;
3533 
3534 	if (!ieee80211_has_protected(hdr->frame_control))
3535 		return false;
3536 
3537 	if ((info->flags & mask) == mask)
3538 		return false;
3539 
3540 	if (vif)
3541 		return !((struct ath10k_vif *)vif->drv_priv)->nohwcrypt;
3542 
3543 	return true;
3544 }
3545 
3546 /* HTT Tx uses Native Wifi tx mode which expects 802.11 frames without QoS
3547  * Control in the header.
3548  */
3549 static void ath10k_tx_h_nwifi(struct ieee80211_hw *hw, struct sk_buff *skb)
3550 {
3551 	struct ieee80211_hdr *hdr = (void *)skb->data;
3552 	struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
3553 	u8 *qos_ctl;
3554 
3555 	if (!ieee80211_is_data_qos(hdr->frame_control))
3556 		return;
3557 
3558 	qos_ctl = ieee80211_get_qos_ctl(hdr);
3559 	memmove(skb->data + IEEE80211_QOS_CTL_LEN,
3560 		skb->data, (void *)qos_ctl - (void *)skb->data);
3561 	skb_pull(skb, IEEE80211_QOS_CTL_LEN);
3562 
3563 	/* Some firmware revisions don't handle sending QoS NullFunc well.
3564 	 * These frames are mainly used for CQM purposes so it doesn't really
3565 	 * matter whether QoS NullFunc or NullFunc are sent.
3566 	 */
3567 	hdr = (void *)skb->data;
3568 	if (ieee80211_is_qos_nullfunc(hdr->frame_control))
3569 		cb->flags &= ~ATH10K_SKB_F_QOS;
3570 
3571 	hdr->frame_control &= ~__cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
3572 }
3573 
3574 static void ath10k_tx_h_8023(struct sk_buff *skb)
3575 {
3576 	struct ieee80211_hdr *hdr;
3577 	struct rfc1042_hdr *rfc1042;
3578 	struct ethhdr *eth;
3579 	size_t hdrlen;
3580 	u8 da[ETH_ALEN];
3581 	u8 sa[ETH_ALEN];
3582 	__be16 type;
3583 
3584 	hdr = (void *)skb->data;
3585 	hdrlen = ieee80211_hdrlen(hdr->frame_control);
3586 	rfc1042 = (void *)skb->data + hdrlen;
3587 
3588 	ether_addr_copy(da, ieee80211_get_DA(hdr));
3589 	ether_addr_copy(sa, ieee80211_get_SA(hdr));
3590 	type = rfc1042->snap_type;
3591 
3592 	skb_pull(skb, hdrlen + sizeof(*rfc1042));
3593 	skb_push(skb, sizeof(*eth));
3594 
3595 	eth = (void *)skb->data;
3596 	ether_addr_copy(eth->h_dest, da);
3597 	ether_addr_copy(eth->h_source, sa);
3598 	eth->h_proto = type;
3599 }
3600 
3601 static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar,
3602 				       struct ieee80211_vif *vif,
3603 				       struct sk_buff *skb)
3604 {
3605 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
3606 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
3607 
3608 	/* This is case only for P2P_GO */
3609 	if (vif->type != NL80211_IFTYPE_AP || !vif->p2p)
3610 		return;
3611 
3612 	if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
3613 		spin_lock_bh(&ar->data_lock);
3614 		if (arvif->u.ap.noa_data)
3615 			if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
3616 					      GFP_ATOMIC))
3617 				skb_put_data(skb, arvif->u.ap.noa_data,
3618 					     arvif->u.ap.noa_len);
3619 		spin_unlock_bh(&ar->data_lock);
3620 	}
3621 }
3622 
3623 static void ath10k_mac_tx_h_fill_cb(struct ath10k *ar,
3624 				    struct ieee80211_vif *vif,
3625 				    struct ieee80211_txq *txq,
3626 				    struct ieee80211_sta *sta,
3627 				    struct sk_buff *skb, u16 airtime)
3628 {
3629 	struct ieee80211_hdr *hdr = (void *)skb->data;
3630 	struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
3631 	const struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3632 	bool is_data = ieee80211_is_data(hdr->frame_control) ||
3633 			ieee80211_is_data_qos(hdr->frame_control);
3634 	struct ath10k_sta *arsta;
3635 
3636 	cb->flags = 0;
3637 	if (!ath10k_tx_h_use_hwcrypto(vif, skb))
3638 		cb->flags |= ATH10K_SKB_F_NO_HWCRYPT;
3639 
3640 	if (ieee80211_is_mgmt(hdr->frame_control))
3641 		cb->flags |= ATH10K_SKB_F_MGMT;
3642 
3643 	if (ieee80211_is_data_qos(hdr->frame_control))
3644 		cb->flags |= ATH10K_SKB_F_QOS;
3645 
3646 	/* Data frames encrypted in software will be posted to firmware
3647 	 * with tx encap mode set to RAW. Ex: Multicast traffic generated
3648 	 * for a specific VLAN group will always be encrypted in software.
3649 	 */
3650 	if (is_data && ieee80211_has_protected(hdr->frame_control) &&
3651 	    !info->control.hw_key) {
3652 		cb->flags |= ATH10K_SKB_F_NO_HWCRYPT;
3653 		cb->flags |= ATH10K_SKB_F_RAW_TX;
3654 	}
3655 
3656 	cb->vif = vif;
3657 	cb->txq = txq;
3658 	cb->airtime_est = airtime;
3659 	if (sta) {
3660 		arsta = (struct ath10k_sta *)sta->drv_priv;
3661 		spin_lock_bh(&ar->data_lock);
3662 		cb->ucast_cipher = arsta->ucast_cipher;
3663 		spin_unlock_bh(&ar->data_lock);
3664 	}
3665 }
3666 
3667 bool ath10k_mac_tx_frm_has_freq(struct ath10k *ar)
3668 {
3669 	/* FIXME: Not really sure since when the behaviour changed. At some
3670 	 * point new firmware stopped requiring creation of peer entries for
3671 	 * offchannel tx (and actually creating them causes issues with wmi-htc
3672 	 * tx credit replenishment and reliability). Assuming it's at least 3.4
3673 	 * because that's when the `freq` was introduced to TX_FRM HTT command.
3674 	 */
3675 	return (ar->htt.target_version_major >= 3 &&
3676 		ar->htt.target_version_minor >= 4 &&
3677 		ar->running_fw->fw_file.htt_op_version == ATH10K_FW_HTT_OP_VERSION_TLV);
3678 }
3679 
3680 static int ath10k_mac_tx_wmi_mgmt(struct ath10k *ar, struct sk_buff *skb)
3681 {
3682 	struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
3683 	int ret = 0;
3684 
3685 	spin_lock_bh(&ar->data_lock);
3686 
3687 	if (skb_queue_len(q) == ATH10K_MAX_NUM_MGMT_PENDING) {
3688 		ath10k_warn(ar, "wmi mgmt tx queue is full\n");
3689 		ret = -ENOSPC;
3690 		goto unlock;
3691 	}
3692 
3693 	__skb_queue_tail(q, skb);
3694 	ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
3695 
3696 unlock:
3697 	spin_unlock_bh(&ar->data_lock);
3698 
3699 	return ret;
3700 }
3701 
3702 static enum ath10k_mac_tx_path
3703 ath10k_mac_tx_h_get_txpath(struct ath10k *ar,
3704 			   struct sk_buff *skb,
3705 			   enum ath10k_hw_txrx_mode txmode)
3706 {
3707 	switch (txmode) {
3708 	case ATH10K_HW_TXRX_RAW:
3709 	case ATH10K_HW_TXRX_NATIVE_WIFI:
3710 	case ATH10K_HW_TXRX_ETHERNET:
3711 		return ATH10K_MAC_TX_HTT;
3712 	case ATH10K_HW_TXRX_MGMT:
3713 		if (test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
3714 			     ar->running_fw->fw_file.fw_features) ||
3715 			     test_bit(WMI_SERVICE_MGMT_TX_WMI,
3716 				      ar->wmi.svc_map))
3717 			return ATH10K_MAC_TX_WMI_MGMT;
3718 		else if (ar->htt.target_version_major >= 3)
3719 			return ATH10K_MAC_TX_HTT;
3720 		else
3721 			return ATH10K_MAC_TX_HTT_MGMT;
3722 	}
3723 
3724 	return ATH10K_MAC_TX_UNKNOWN;
3725 }
3726 
3727 static int ath10k_mac_tx_submit(struct ath10k *ar,
3728 				enum ath10k_hw_txrx_mode txmode,
3729 				enum ath10k_mac_tx_path txpath,
3730 				struct sk_buff *skb)
3731 {
3732 	struct ath10k_htt *htt = &ar->htt;
3733 	int ret = -EINVAL;
3734 
3735 	switch (txpath) {
3736 	case ATH10K_MAC_TX_HTT:
3737 		ret = ath10k_htt_tx(htt, txmode, skb);
3738 		break;
3739 	case ATH10K_MAC_TX_HTT_MGMT:
3740 		ret = ath10k_htt_mgmt_tx(htt, skb);
3741 		break;
3742 	case ATH10K_MAC_TX_WMI_MGMT:
3743 		ret = ath10k_mac_tx_wmi_mgmt(ar, skb);
3744 		break;
3745 	case ATH10K_MAC_TX_UNKNOWN:
3746 		WARN_ON_ONCE(1);
3747 		ret = -EINVAL;
3748 		break;
3749 	}
3750 
3751 	if (ret) {
3752 		ath10k_warn(ar, "failed to transmit packet, dropping: %d\n",
3753 			    ret);
3754 		ieee80211_free_txskb(ar->hw, skb);
3755 	}
3756 
3757 	return ret;
3758 }
3759 
3760 /* This function consumes the sk_buff regardless of return value as far as
3761  * caller is concerned so no freeing is necessary afterwards.
3762  */
3763 static int ath10k_mac_tx(struct ath10k *ar,
3764 			 struct ieee80211_vif *vif,
3765 			 enum ath10k_hw_txrx_mode txmode,
3766 			 enum ath10k_mac_tx_path txpath,
3767 			 struct sk_buff *skb, bool noque_offchan)
3768 {
3769 	struct ieee80211_hw *hw = ar->hw;
3770 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3771 	const struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(skb);
3772 	int ret;
3773 
3774 	/* We should disable CCK RATE due to P2P */
3775 	if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
3776 		ath10k_dbg(ar, ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
3777 
3778 	switch (txmode) {
3779 	case ATH10K_HW_TXRX_MGMT:
3780 	case ATH10K_HW_TXRX_NATIVE_WIFI:
3781 		ath10k_tx_h_nwifi(hw, skb);
3782 		ath10k_tx_h_add_p2p_noa_ie(ar, vif, skb);
3783 		ath10k_tx_h_seq_no(vif, skb);
3784 		break;
3785 	case ATH10K_HW_TXRX_ETHERNET:
3786 		ath10k_tx_h_8023(skb);
3787 		break;
3788 	case ATH10K_HW_TXRX_RAW:
3789 		if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags) &&
3790 		    !(skb_cb->flags & ATH10K_SKB_F_RAW_TX)) {
3791 			WARN_ON_ONCE(1);
3792 			ieee80211_free_txskb(hw, skb);
3793 			return -ENOTSUPP;
3794 		}
3795 	}
3796 
3797 	if (!noque_offchan && info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
3798 		if (!ath10k_mac_tx_frm_has_freq(ar)) {
3799 			ath10k_dbg(ar, ATH10K_DBG_MAC, "mac queued offchannel skb %pK len %d\n",
3800 				   skb, skb->len);
3801 
3802 			skb_queue_tail(&ar->offchan_tx_queue, skb);
3803 			ieee80211_queue_work(hw, &ar->offchan_tx_work);
3804 			return 0;
3805 		}
3806 	}
3807 
3808 	ret = ath10k_mac_tx_submit(ar, txmode, txpath, skb);
3809 	if (ret) {
3810 		ath10k_warn(ar, "failed to submit frame: %d\n", ret);
3811 		return ret;
3812 	}
3813 
3814 	return 0;
3815 }
3816 
3817 void ath10k_offchan_tx_purge(struct ath10k *ar)
3818 {
3819 	struct sk_buff *skb;
3820 
3821 	for (;;) {
3822 		skb = skb_dequeue(&ar->offchan_tx_queue);
3823 		if (!skb)
3824 			break;
3825 
3826 		ieee80211_free_txskb(ar->hw, skb);
3827 	}
3828 }
3829 
3830 void ath10k_offchan_tx_work(struct work_struct *work)
3831 {
3832 	struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
3833 	struct ath10k_peer *peer;
3834 	struct ath10k_vif *arvif;
3835 	enum ath10k_hw_txrx_mode txmode;
3836 	enum ath10k_mac_tx_path txpath;
3837 	struct ieee80211_hdr *hdr;
3838 	struct ieee80211_vif *vif;
3839 	struct ieee80211_sta *sta;
3840 	struct sk_buff *skb;
3841 	const u8 *peer_addr;
3842 	int vdev_id;
3843 	int ret;
3844 	unsigned long time_left;
3845 	bool tmp_peer_created = false;
3846 
3847 	/* FW requirement: We must create a peer before FW will send out
3848 	 * an offchannel frame. Otherwise the frame will be stuck and
3849 	 * never transmitted. We delete the peer upon tx completion.
3850 	 * It is unlikely that a peer for offchannel tx will already be
3851 	 * present. However it may be in some rare cases so account for that.
3852 	 * Otherwise we might remove a legitimate peer and break stuff.
3853 	 */
3854 
3855 	for (;;) {
3856 		skb = skb_dequeue(&ar->offchan_tx_queue);
3857 		if (!skb)
3858 			break;
3859 
3860 		mutex_lock(&ar->conf_mutex);
3861 
3862 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac offchannel skb %pK len %d\n",
3863 			   skb, skb->len);
3864 
3865 		hdr = (struct ieee80211_hdr *)skb->data;
3866 		peer_addr = ieee80211_get_DA(hdr);
3867 
3868 		spin_lock_bh(&ar->data_lock);
3869 		vdev_id = ar->scan.vdev_id;
3870 		peer = ath10k_peer_find(ar, vdev_id, peer_addr);
3871 		spin_unlock_bh(&ar->data_lock);
3872 
3873 		if (peer)
3874 			/* FIXME: should this use ath10k_warn()? */
3875 			ath10k_dbg(ar, ATH10K_DBG_MAC, "peer %pM on vdev %d already present\n",
3876 				   peer_addr, vdev_id);
3877 
3878 		if (!peer) {
3879 			ret = ath10k_peer_create(ar, NULL, NULL, vdev_id,
3880 						 peer_addr,
3881 						 WMI_PEER_TYPE_DEFAULT);
3882 			if (ret)
3883 				ath10k_warn(ar, "failed to create peer %pM on vdev %d: %d\n",
3884 					    peer_addr, vdev_id, ret);
3885 			tmp_peer_created = (ret == 0);
3886 		}
3887 
3888 		spin_lock_bh(&ar->data_lock);
3889 		reinit_completion(&ar->offchan_tx_completed);
3890 		ar->offchan_tx_skb = skb;
3891 		spin_unlock_bh(&ar->data_lock);
3892 
3893 		/* It's safe to access vif and sta - conf_mutex guarantees that
3894 		 * sta_state() and remove_interface() are locked exclusively
3895 		 * out wrt to this offchannel worker.
3896 		 */
3897 		arvif = ath10k_get_arvif(ar, vdev_id);
3898 		if (arvif) {
3899 			vif = arvif->vif;
3900 			sta = ieee80211_find_sta(vif, peer_addr);
3901 		} else {
3902 			vif = NULL;
3903 			sta = NULL;
3904 		}
3905 
3906 		txmode = ath10k_mac_tx_h_get_txmode(ar, vif, sta, skb);
3907 		txpath = ath10k_mac_tx_h_get_txpath(ar, skb, txmode);
3908 
3909 		ret = ath10k_mac_tx(ar, vif, txmode, txpath, skb, true);
3910 		if (ret) {
3911 			ath10k_warn(ar, "failed to transmit offchannel frame: %d\n",
3912 				    ret);
3913 			/* not serious */
3914 		}
3915 
3916 		time_left =
3917 		wait_for_completion_timeout(&ar->offchan_tx_completed, 3 * HZ);
3918 		if (time_left == 0)
3919 			ath10k_warn(ar, "timed out waiting for offchannel skb %pK, len: %d\n",
3920 				    skb, skb->len);
3921 
3922 		if (!peer && tmp_peer_created) {
3923 			ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
3924 			if (ret)
3925 				ath10k_warn(ar, "failed to delete peer %pM on vdev %d: %d\n",
3926 					    peer_addr, vdev_id, ret);
3927 		}
3928 
3929 		mutex_unlock(&ar->conf_mutex);
3930 	}
3931 }
3932 
3933 void ath10k_mgmt_over_wmi_tx_purge(struct ath10k *ar)
3934 {
3935 	struct sk_buff *skb;
3936 
3937 	for (;;) {
3938 		skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
3939 		if (!skb)
3940 			break;
3941 
3942 		ieee80211_free_txskb(ar->hw, skb);
3943 	}
3944 }
3945 
3946 void ath10k_mgmt_over_wmi_tx_work(struct work_struct *work)
3947 {
3948 	struct ath10k *ar = container_of(work, struct ath10k, wmi_mgmt_tx_work);
3949 	struct sk_buff *skb;
3950 	dma_addr_t paddr;
3951 	int ret;
3952 
3953 	for (;;) {
3954 		skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
3955 		if (!skb)
3956 			break;
3957 
3958 		if (test_bit(ATH10K_FW_FEATURE_MGMT_TX_BY_REF,
3959 			     ar->running_fw->fw_file.fw_features)) {
3960 			paddr = dma_map_single(ar->dev, skb->data,
3961 					       skb->len, DMA_TO_DEVICE);
3962 			if (dma_mapping_error(ar->dev, paddr)) {
3963 				ieee80211_free_txskb(ar->hw, skb);
3964 				continue;
3965 			}
3966 			ret = ath10k_wmi_mgmt_tx_send(ar, skb, paddr);
3967 			if (ret) {
3968 				ath10k_warn(ar, "failed to transmit management frame by ref via WMI: %d\n",
3969 					    ret);
3970 				/* remove this msdu from idr tracking */
3971 				ath10k_wmi_cleanup_mgmt_tx_send(ar, skb);
3972 
3973 				dma_unmap_single(ar->dev, paddr, skb->len,
3974 						 DMA_TO_DEVICE);
3975 				ieee80211_free_txskb(ar->hw, skb);
3976 			}
3977 		} else {
3978 			ret = ath10k_wmi_mgmt_tx(ar, skb);
3979 			if (ret) {
3980 				ath10k_warn(ar, "failed to transmit management frame via WMI: %d\n",
3981 					    ret);
3982 				ieee80211_free_txskb(ar->hw, skb);
3983 			}
3984 		}
3985 	}
3986 }
3987 
3988 static void ath10k_mac_txq_init(struct ieee80211_txq *txq)
3989 {
3990 	struct ath10k_txq *artxq;
3991 
3992 	if (!txq)
3993 		return;
3994 
3995 	artxq = (void *)txq->drv_priv;
3996 	INIT_LIST_HEAD(&artxq->list);
3997 }
3998 
3999 static void ath10k_mac_txq_unref(struct ath10k *ar, struct ieee80211_txq *txq)
4000 {
4001 	struct ath10k_skb_cb *cb;
4002 	struct sk_buff *msdu;
4003 	int msdu_id;
4004 
4005 	if (!txq)
4006 		return;
4007 
4008 	spin_lock_bh(&ar->htt.tx_lock);
4009 	idr_for_each_entry(&ar->htt.pending_tx, msdu, msdu_id) {
4010 		cb = ATH10K_SKB_CB(msdu);
4011 		if (cb->txq == txq)
4012 			cb->txq = NULL;
4013 	}
4014 	spin_unlock_bh(&ar->htt.tx_lock);
4015 }
4016 
4017 struct ieee80211_txq *ath10k_mac_txq_lookup(struct ath10k *ar,
4018 					    u16 peer_id,
4019 					    u8 tid)
4020 {
4021 	struct ath10k_peer *peer;
4022 
4023 	lockdep_assert_held(&ar->data_lock);
4024 
4025 	peer = ar->peer_map[peer_id];
4026 	if (!peer)
4027 		return NULL;
4028 
4029 	if (peer->removed)
4030 		return NULL;
4031 
4032 	if (peer->sta)
4033 		return peer->sta->txq[tid];
4034 	else if (peer->vif)
4035 		return peer->vif->txq;
4036 	else
4037 		return NULL;
4038 }
4039 
4040 static bool ath10k_mac_tx_can_push(struct ieee80211_hw *hw,
4041 				   struct ieee80211_txq *txq)
4042 {
4043 	struct ath10k *ar = hw->priv;
4044 	struct ath10k_txq *artxq = (void *)txq->drv_priv;
4045 
4046 	/* No need to get locks */
4047 	if (ar->htt.tx_q_state.mode == HTT_TX_MODE_SWITCH_PUSH)
4048 		return true;
4049 
4050 	if (ar->htt.num_pending_tx < ar->htt.tx_q_state.num_push_allowed)
4051 		return true;
4052 
4053 	if (artxq->num_fw_queued < artxq->num_push_allowed)
4054 		return true;
4055 
4056 	return false;
4057 }
4058 
4059 /* Return estimated airtime in microsecond, which is calculated using last
4060  * reported TX rate. This is just a rough estimation because host driver has no
4061  * knowledge of the actual transmit rate, retries or aggregation. If actual
4062  * airtime can be reported by firmware, then delta between estimated and actual
4063  * airtime can be adjusted from deficit.
4064  */
4065 #define IEEE80211_ATF_OVERHEAD		100	/* IFS + some slot time */
4066 #define IEEE80211_ATF_OVERHEAD_IFS	16	/* IFS only */
4067 static u16 ath10k_mac_update_airtime(struct ath10k *ar,
4068 				     struct ieee80211_txq *txq,
4069 				     struct sk_buff *skb)
4070 {
4071 	struct ath10k_sta *arsta;
4072 	u32 pktlen;
4073 	u16 airtime = 0;
4074 
4075 	if (!txq || !txq->sta)
4076 		return airtime;
4077 
4078 	if (test_bit(WMI_SERVICE_REPORT_AIRTIME, ar->wmi.svc_map))
4079 		return airtime;
4080 
4081 	spin_lock_bh(&ar->data_lock);
4082 	arsta = (struct ath10k_sta *)txq->sta->drv_priv;
4083 
4084 	pktlen = skb->len + 38; /* Assume MAC header 30, SNAP 8 for most case */
4085 	if (arsta->last_tx_bitrate) {
4086 		/* airtime in us, last_tx_bitrate in 100kbps */
4087 		airtime = (pktlen * 8 * (1000 / 100))
4088 				/ arsta->last_tx_bitrate;
4089 		/* overhead for media access time and IFS */
4090 		airtime += IEEE80211_ATF_OVERHEAD_IFS;
4091 	} else {
4092 		/* This is mostly for throttle excessive BC/MC frames, and the
4093 		 * airtime/rate doesn't need be exact. Airtime of BC/MC frames
4094 		 * in 2G get some discount, which helps prevent very low rate
4095 		 * frames from being blocked for too long.
4096 		 */
4097 		airtime = (pktlen * 8 * (1000 / 100)) / 60; /* 6M */
4098 		airtime += IEEE80211_ATF_OVERHEAD;
4099 	}
4100 	spin_unlock_bh(&ar->data_lock);
4101 
4102 	return airtime;
4103 }
4104 
4105 int ath10k_mac_tx_push_txq(struct ieee80211_hw *hw,
4106 			   struct ieee80211_txq *txq)
4107 {
4108 	struct ath10k *ar = hw->priv;
4109 	struct ath10k_htt *htt = &ar->htt;
4110 	struct ath10k_txq *artxq = (void *)txq->drv_priv;
4111 	struct ieee80211_vif *vif = txq->vif;
4112 	struct ieee80211_sta *sta = txq->sta;
4113 	enum ath10k_hw_txrx_mode txmode;
4114 	enum ath10k_mac_tx_path txpath;
4115 	struct sk_buff *skb;
4116 	struct ieee80211_hdr *hdr;
4117 	size_t skb_len;
4118 	bool is_mgmt, is_presp;
4119 	int ret;
4120 	u16 airtime;
4121 
4122 	spin_lock_bh(&ar->htt.tx_lock);
4123 	ret = ath10k_htt_tx_inc_pending(htt);
4124 	spin_unlock_bh(&ar->htt.tx_lock);
4125 
4126 	if (ret)
4127 		return ret;
4128 
4129 	skb = ieee80211_tx_dequeue_ni(hw, txq);
4130 	if (!skb) {
4131 		spin_lock_bh(&ar->htt.tx_lock);
4132 		ath10k_htt_tx_dec_pending(htt);
4133 		spin_unlock_bh(&ar->htt.tx_lock);
4134 
4135 		return -ENOENT;
4136 	}
4137 
4138 	airtime = ath10k_mac_update_airtime(ar, txq, skb);
4139 	ath10k_mac_tx_h_fill_cb(ar, vif, txq, sta, skb, airtime);
4140 
4141 	skb_len = skb->len;
4142 	txmode = ath10k_mac_tx_h_get_txmode(ar, vif, sta, skb);
4143 	txpath = ath10k_mac_tx_h_get_txpath(ar, skb, txmode);
4144 	is_mgmt = (txpath == ATH10K_MAC_TX_HTT_MGMT);
4145 
4146 	if (is_mgmt) {
4147 		hdr = (struct ieee80211_hdr *)skb->data;
4148 		is_presp = ieee80211_is_probe_resp(hdr->frame_control);
4149 
4150 		spin_lock_bh(&ar->htt.tx_lock);
4151 		ret = ath10k_htt_tx_mgmt_inc_pending(htt, is_mgmt, is_presp);
4152 
4153 		if (ret) {
4154 			ath10k_htt_tx_dec_pending(htt);
4155 			spin_unlock_bh(&ar->htt.tx_lock);
4156 			return ret;
4157 		}
4158 		spin_unlock_bh(&ar->htt.tx_lock);
4159 	}
4160 
4161 	ret = ath10k_mac_tx(ar, vif, txmode, txpath, skb, false);
4162 	if (unlikely(ret)) {
4163 		ath10k_warn(ar, "failed to push frame: %d\n", ret);
4164 
4165 		spin_lock_bh(&ar->htt.tx_lock);
4166 		ath10k_htt_tx_dec_pending(htt);
4167 		if (is_mgmt)
4168 			ath10k_htt_tx_mgmt_dec_pending(htt);
4169 		spin_unlock_bh(&ar->htt.tx_lock);
4170 
4171 		return ret;
4172 	}
4173 
4174 	spin_lock_bh(&ar->htt.tx_lock);
4175 	artxq->num_fw_queued++;
4176 	spin_unlock_bh(&ar->htt.tx_lock);
4177 
4178 	return skb_len;
4179 }
4180 
4181 static int ath10k_mac_schedule_txq(struct ieee80211_hw *hw, u32 ac)
4182 {
4183 	struct ieee80211_txq *txq;
4184 	int ret = 0;
4185 
4186 	ieee80211_txq_schedule_start(hw, ac);
4187 	while ((txq = ieee80211_next_txq(hw, ac))) {
4188 		while (ath10k_mac_tx_can_push(hw, txq)) {
4189 			ret = ath10k_mac_tx_push_txq(hw, txq);
4190 			if (ret < 0)
4191 				break;
4192 		}
4193 		ieee80211_return_txq(hw, txq, false);
4194 		ath10k_htt_tx_txq_update(hw, txq);
4195 		if (ret == -EBUSY)
4196 			break;
4197 	}
4198 	ieee80211_txq_schedule_end(hw, ac);
4199 
4200 	return ret;
4201 }
4202 
4203 void ath10k_mac_tx_push_pending(struct ath10k *ar)
4204 {
4205 	struct ieee80211_hw *hw = ar->hw;
4206 	u32 ac;
4207 
4208 	if (ar->htt.tx_q_state.mode != HTT_TX_MODE_SWITCH_PUSH)
4209 		return;
4210 
4211 	if (ar->htt.num_pending_tx >= (ar->htt.max_num_pending_tx / 2))
4212 		return;
4213 
4214 	rcu_read_lock();
4215 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
4216 		if (ath10k_mac_schedule_txq(hw, ac) == -EBUSY)
4217 			break;
4218 	}
4219 	rcu_read_unlock();
4220 }
4221 EXPORT_SYMBOL(ath10k_mac_tx_push_pending);
4222 
4223 /************/
4224 /* Scanning */
4225 /************/
4226 
4227 void __ath10k_scan_finish(struct ath10k *ar)
4228 {
4229 	lockdep_assert_held(&ar->data_lock);
4230 
4231 	switch (ar->scan.state) {
4232 	case ATH10K_SCAN_IDLE:
4233 		break;
4234 	case ATH10K_SCAN_RUNNING:
4235 	case ATH10K_SCAN_ABORTING:
4236 		if (!ar->scan.is_roc) {
4237 			struct cfg80211_scan_info info = {
4238 				.aborted = (ar->scan.state ==
4239 					    ATH10K_SCAN_ABORTING),
4240 			};
4241 
4242 			ieee80211_scan_completed(ar->hw, &info);
4243 		} else if (ar->scan.roc_notify) {
4244 			ieee80211_remain_on_channel_expired(ar->hw);
4245 		}
4246 		/* fall through */
4247 	case ATH10K_SCAN_STARTING:
4248 		ar->scan.state = ATH10K_SCAN_IDLE;
4249 		ar->scan_channel = NULL;
4250 		ar->scan.roc_freq = 0;
4251 		ath10k_offchan_tx_purge(ar);
4252 		cancel_delayed_work(&ar->scan.timeout);
4253 		complete(&ar->scan.completed);
4254 		break;
4255 	}
4256 }
4257 
4258 void ath10k_scan_finish(struct ath10k *ar)
4259 {
4260 	spin_lock_bh(&ar->data_lock);
4261 	__ath10k_scan_finish(ar);
4262 	spin_unlock_bh(&ar->data_lock);
4263 }
4264 
4265 static int ath10k_scan_stop(struct ath10k *ar)
4266 {
4267 	struct wmi_stop_scan_arg arg = {
4268 		.req_id = 1, /* FIXME */
4269 		.req_type = WMI_SCAN_STOP_ONE,
4270 		.u.scan_id = ATH10K_SCAN_ID,
4271 	};
4272 	int ret;
4273 
4274 	lockdep_assert_held(&ar->conf_mutex);
4275 
4276 	ret = ath10k_wmi_stop_scan(ar, &arg);
4277 	if (ret) {
4278 		ath10k_warn(ar, "failed to stop wmi scan: %d\n", ret);
4279 		goto out;
4280 	}
4281 
4282 	ret = wait_for_completion_timeout(&ar->scan.completed, 3 * HZ);
4283 	if (ret == 0) {
4284 		ath10k_warn(ar, "failed to receive scan abortion completion: timed out\n");
4285 		ret = -ETIMEDOUT;
4286 	} else if (ret > 0) {
4287 		ret = 0;
4288 	}
4289 
4290 out:
4291 	/* Scan state should be updated upon scan completion but in case
4292 	 * firmware fails to deliver the event (for whatever reason) it is
4293 	 * desired to clean up scan state anyway. Firmware may have just
4294 	 * dropped the scan completion event delivery due to transport pipe
4295 	 * being overflown with data and/or it can recover on its own before
4296 	 * next scan request is submitted.
4297 	 */
4298 	spin_lock_bh(&ar->data_lock);
4299 	if (ar->scan.state != ATH10K_SCAN_IDLE)
4300 		__ath10k_scan_finish(ar);
4301 	spin_unlock_bh(&ar->data_lock);
4302 
4303 	return ret;
4304 }
4305 
4306 static void ath10k_scan_abort(struct ath10k *ar)
4307 {
4308 	int ret;
4309 
4310 	lockdep_assert_held(&ar->conf_mutex);
4311 
4312 	spin_lock_bh(&ar->data_lock);
4313 
4314 	switch (ar->scan.state) {
4315 	case ATH10K_SCAN_IDLE:
4316 		/* This can happen if timeout worker kicked in and called
4317 		 * abortion while scan completion was being processed.
4318 		 */
4319 		break;
4320 	case ATH10K_SCAN_STARTING:
4321 	case ATH10K_SCAN_ABORTING:
4322 		ath10k_warn(ar, "refusing scan abortion due to invalid scan state: %s (%d)\n",
4323 			    ath10k_scan_state_str(ar->scan.state),
4324 			    ar->scan.state);
4325 		break;
4326 	case ATH10K_SCAN_RUNNING:
4327 		ar->scan.state = ATH10K_SCAN_ABORTING;
4328 		spin_unlock_bh(&ar->data_lock);
4329 
4330 		ret = ath10k_scan_stop(ar);
4331 		if (ret)
4332 			ath10k_warn(ar, "failed to abort scan: %d\n", ret);
4333 
4334 		spin_lock_bh(&ar->data_lock);
4335 		break;
4336 	}
4337 
4338 	spin_unlock_bh(&ar->data_lock);
4339 }
4340 
4341 void ath10k_scan_timeout_work(struct work_struct *work)
4342 {
4343 	struct ath10k *ar = container_of(work, struct ath10k,
4344 					 scan.timeout.work);
4345 
4346 	mutex_lock(&ar->conf_mutex);
4347 	ath10k_scan_abort(ar);
4348 	mutex_unlock(&ar->conf_mutex);
4349 }
4350 
4351 static int ath10k_start_scan(struct ath10k *ar,
4352 			     const struct wmi_start_scan_arg *arg)
4353 {
4354 	int ret;
4355 
4356 	lockdep_assert_held(&ar->conf_mutex);
4357 
4358 	ret = ath10k_wmi_start_scan(ar, arg);
4359 	if (ret)
4360 		return ret;
4361 
4362 	ret = wait_for_completion_timeout(&ar->scan.started, 1 * HZ);
4363 	if (ret == 0) {
4364 		ret = ath10k_scan_stop(ar);
4365 		if (ret)
4366 			ath10k_warn(ar, "failed to stop scan: %d\n", ret);
4367 
4368 		return -ETIMEDOUT;
4369 	}
4370 
4371 	/* If we failed to start the scan, return error code at
4372 	 * this point.  This is probably due to some issue in the
4373 	 * firmware, but no need to wedge the driver due to that...
4374 	 */
4375 	spin_lock_bh(&ar->data_lock);
4376 	if (ar->scan.state == ATH10K_SCAN_IDLE) {
4377 		spin_unlock_bh(&ar->data_lock);
4378 		return -EINVAL;
4379 	}
4380 	spin_unlock_bh(&ar->data_lock);
4381 
4382 	return 0;
4383 }
4384 
4385 /**********************/
4386 /* mac80211 callbacks */
4387 /**********************/
4388 
4389 static void ath10k_mac_op_tx(struct ieee80211_hw *hw,
4390 			     struct ieee80211_tx_control *control,
4391 			     struct sk_buff *skb)
4392 {
4393 	struct ath10k *ar = hw->priv;
4394 	struct ath10k_htt *htt = &ar->htt;
4395 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4396 	struct ieee80211_vif *vif = info->control.vif;
4397 	struct ieee80211_sta *sta = control->sta;
4398 	struct ieee80211_txq *txq = NULL;
4399 	struct ieee80211_hdr *hdr = (void *)skb->data;
4400 	enum ath10k_hw_txrx_mode txmode;
4401 	enum ath10k_mac_tx_path txpath;
4402 	bool is_htt;
4403 	bool is_mgmt;
4404 	bool is_presp;
4405 	int ret;
4406 	u16 airtime;
4407 
4408 	airtime = ath10k_mac_update_airtime(ar, txq, skb);
4409 	ath10k_mac_tx_h_fill_cb(ar, vif, txq, sta, skb, airtime);
4410 
4411 	txmode = ath10k_mac_tx_h_get_txmode(ar, vif, sta, skb);
4412 	txpath = ath10k_mac_tx_h_get_txpath(ar, skb, txmode);
4413 	is_htt = (txpath == ATH10K_MAC_TX_HTT ||
4414 		  txpath == ATH10K_MAC_TX_HTT_MGMT);
4415 	is_mgmt = (txpath == ATH10K_MAC_TX_HTT_MGMT);
4416 
4417 	if (is_htt) {
4418 		spin_lock_bh(&ar->htt.tx_lock);
4419 		is_presp = ieee80211_is_probe_resp(hdr->frame_control);
4420 
4421 		ret = ath10k_htt_tx_inc_pending(htt);
4422 		if (ret) {
4423 			ath10k_warn(ar, "failed to increase tx pending count: %d, dropping\n",
4424 				    ret);
4425 			spin_unlock_bh(&ar->htt.tx_lock);
4426 			ieee80211_free_txskb(ar->hw, skb);
4427 			return;
4428 		}
4429 
4430 		ret = ath10k_htt_tx_mgmt_inc_pending(htt, is_mgmt, is_presp);
4431 		if (ret) {
4432 			ath10k_dbg(ar, ATH10K_DBG_MAC, "failed to increase tx mgmt pending count: %d, dropping\n",
4433 				   ret);
4434 			ath10k_htt_tx_dec_pending(htt);
4435 			spin_unlock_bh(&ar->htt.tx_lock);
4436 			ieee80211_free_txskb(ar->hw, skb);
4437 			return;
4438 		}
4439 		spin_unlock_bh(&ar->htt.tx_lock);
4440 	}
4441 
4442 	ret = ath10k_mac_tx(ar, vif, txmode, txpath, skb, false);
4443 	if (ret) {
4444 		ath10k_warn(ar, "failed to transmit frame: %d\n", ret);
4445 		if (is_htt) {
4446 			spin_lock_bh(&ar->htt.tx_lock);
4447 			ath10k_htt_tx_dec_pending(htt);
4448 			if (is_mgmt)
4449 				ath10k_htt_tx_mgmt_dec_pending(htt);
4450 			spin_unlock_bh(&ar->htt.tx_lock);
4451 		}
4452 		return;
4453 	}
4454 }
4455 
4456 static void ath10k_mac_op_wake_tx_queue(struct ieee80211_hw *hw,
4457 					struct ieee80211_txq *txq)
4458 {
4459 	struct ath10k *ar = hw->priv;
4460 	int ret;
4461 	u8 ac;
4462 
4463 	ath10k_htt_tx_txq_update(hw, txq);
4464 	if (ar->htt.tx_q_state.mode != HTT_TX_MODE_SWITCH_PUSH)
4465 		return;
4466 
4467 	ac = txq->ac;
4468 	ieee80211_txq_schedule_start(hw, ac);
4469 	txq = ieee80211_next_txq(hw, ac);
4470 	if (!txq)
4471 		goto out;
4472 
4473 	while (ath10k_mac_tx_can_push(hw, txq)) {
4474 		ret = ath10k_mac_tx_push_txq(hw, txq);
4475 		if (ret < 0)
4476 			break;
4477 	}
4478 	ieee80211_return_txq(hw, txq, false);
4479 	ath10k_htt_tx_txq_update(hw, txq);
4480 out:
4481 	ieee80211_txq_schedule_end(hw, ac);
4482 }
4483 
4484 /* Must not be called with conf_mutex held as workers can use that also. */
4485 void ath10k_drain_tx(struct ath10k *ar)
4486 {
4487 	/* make sure rcu-protected mac80211 tx path itself is drained */
4488 	synchronize_net();
4489 
4490 	ath10k_offchan_tx_purge(ar);
4491 	ath10k_mgmt_over_wmi_tx_purge(ar);
4492 
4493 	cancel_work_sync(&ar->offchan_tx_work);
4494 	cancel_work_sync(&ar->wmi_mgmt_tx_work);
4495 }
4496 
4497 void ath10k_halt(struct ath10k *ar)
4498 {
4499 	struct ath10k_vif *arvif;
4500 
4501 	lockdep_assert_held(&ar->conf_mutex);
4502 
4503 	clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
4504 	ar->filter_flags = 0;
4505 	ar->monitor = false;
4506 	ar->monitor_arvif = NULL;
4507 
4508 	if (ar->monitor_started)
4509 		ath10k_monitor_stop(ar);
4510 
4511 	ar->monitor_started = false;
4512 	ar->tx_paused = 0;
4513 
4514 	ath10k_scan_finish(ar);
4515 	ath10k_peer_cleanup_all(ar);
4516 	ath10k_stop_radar_confirmation(ar);
4517 	ath10k_core_stop(ar);
4518 	ath10k_hif_power_down(ar);
4519 
4520 	spin_lock_bh(&ar->data_lock);
4521 	list_for_each_entry(arvif, &ar->arvifs, list)
4522 		ath10k_mac_vif_beacon_cleanup(arvif);
4523 	spin_unlock_bh(&ar->data_lock);
4524 }
4525 
4526 static int ath10k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
4527 {
4528 	struct ath10k *ar = hw->priv;
4529 
4530 	mutex_lock(&ar->conf_mutex);
4531 
4532 	*tx_ant = ar->cfg_tx_chainmask;
4533 	*rx_ant = ar->cfg_rx_chainmask;
4534 
4535 	mutex_unlock(&ar->conf_mutex);
4536 
4537 	return 0;
4538 }
4539 
4540 static bool ath10k_check_chain_mask(struct ath10k *ar, u32 cm, const char *dbg)
4541 {
4542 	/* It is not clear that allowing gaps in chainmask
4543 	 * is helpful.  Probably it will not do what user
4544 	 * is hoping for, so warn in that case.
4545 	 */
4546 	if (cm == 15 || cm == 7 || cm == 3 || cm == 1 || cm == 0)
4547 		return true;
4548 
4549 	ath10k_warn(ar, "mac %s antenna chainmask is invalid: 0x%x.  Suggested values: 15, 7, 3, 1 or 0.\n",
4550 		    dbg, cm);
4551 	return false;
4552 }
4553 
4554 static int ath10k_mac_get_vht_cap_bf_sts(struct ath10k *ar)
4555 {
4556 	int nsts = ar->vht_cap_info;
4557 
4558 	nsts &= IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
4559 	nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
4560 
4561 	/* If firmware does not deliver to host number of space-time
4562 	 * streams supported, assume it support up to 4 BF STS and return
4563 	 * the value for VHT CAP: nsts-1)
4564 	 */
4565 	if (nsts == 0)
4566 		return 3;
4567 
4568 	return nsts;
4569 }
4570 
4571 static int ath10k_mac_get_vht_cap_bf_sound_dim(struct ath10k *ar)
4572 {
4573 	int sound_dim = ar->vht_cap_info;
4574 
4575 	sound_dim &= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
4576 	sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
4577 
4578 	/* If the sounding dimension is not advertised by the firmware,
4579 	 * let's use a default value of 1
4580 	 */
4581 	if (sound_dim == 0)
4582 		return 1;
4583 
4584 	return sound_dim;
4585 }
4586 
4587 static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
4588 {
4589 	struct ieee80211_sta_vht_cap vht_cap = {0};
4590 	struct ath10k_hw_params *hw = &ar->hw_params;
4591 	u16 mcs_map;
4592 	u32 val;
4593 	int i;
4594 
4595 	vht_cap.vht_supported = 1;
4596 	vht_cap.cap = ar->vht_cap_info;
4597 
4598 	if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
4599 				IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)) {
4600 		val = ath10k_mac_get_vht_cap_bf_sts(ar);
4601 		val <<= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
4602 		val &= IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
4603 
4604 		vht_cap.cap |= val;
4605 	}
4606 
4607 	if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
4608 				IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)) {
4609 		val = ath10k_mac_get_vht_cap_bf_sound_dim(ar);
4610 		val <<= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
4611 		val &= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
4612 
4613 		vht_cap.cap |= val;
4614 	}
4615 
4616 	mcs_map = 0;
4617 	for (i = 0; i < 8; i++) {
4618 		if ((i < ar->num_rf_chains) && (ar->cfg_tx_chainmask & BIT(i)))
4619 			mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
4620 		else
4621 			mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
4622 	}
4623 
4624 	if (ar->cfg_tx_chainmask <= 1)
4625 		vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
4626 
4627 	vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
4628 	vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
4629 
4630 	/* If we are supporting 160Mhz or 80+80, then the NIC may be able to do
4631 	 * a restricted NSS for 160 or 80+80 vs what it can do for 80Mhz.  Give
4632 	 * user-space a clue if that is the case.
4633 	 */
4634 	if ((vht_cap.cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK) &&
4635 	    (hw->vht160_mcs_rx_highest != 0 ||
4636 	     hw->vht160_mcs_tx_highest != 0)) {
4637 		vht_cap.vht_mcs.rx_highest = cpu_to_le16(hw->vht160_mcs_rx_highest);
4638 		vht_cap.vht_mcs.tx_highest = cpu_to_le16(hw->vht160_mcs_tx_highest);
4639 	}
4640 
4641 	return vht_cap;
4642 }
4643 
4644 static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
4645 {
4646 	int i;
4647 	struct ieee80211_sta_ht_cap ht_cap = {0};
4648 
4649 	if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
4650 		return ht_cap;
4651 
4652 	ht_cap.ht_supported = 1;
4653 	ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
4654 	ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
4655 	ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
4656 	ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
4657 	ht_cap.cap |=
4658 		WLAN_HT_CAP_SM_PS_DISABLED << IEEE80211_HT_CAP_SM_PS_SHIFT;
4659 
4660 	if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
4661 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
4662 
4663 	if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
4664 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
4665 
4666 	if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
4667 		u32 smps;
4668 
4669 		smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
4670 		smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
4671 
4672 		ht_cap.cap |= smps;
4673 	}
4674 
4675 	if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC && (ar->cfg_tx_chainmask > 1))
4676 		ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
4677 
4678 	if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
4679 		u32 stbc;
4680 
4681 		stbc   = ar->ht_cap_info;
4682 		stbc  &= WMI_HT_CAP_RX_STBC;
4683 		stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
4684 		stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
4685 		stbc  &= IEEE80211_HT_CAP_RX_STBC;
4686 
4687 		ht_cap.cap |= stbc;
4688 	}
4689 
4690 	if (ar->ht_cap_info & WMI_HT_CAP_LDPC || (ar->ht_cap_info &
4691 	    WMI_HT_CAP_RX_LDPC && (ar->ht_cap_info & WMI_HT_CAP_TX_LDPC)))
4692 		ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
4693 
4694 	if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
4695 		ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
4696 
4697 	/* max AMSDU is implicitly taken from vht_cap_info */
4698 	if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
4699 		ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
4700 
4701 	for (i = 0; i < ar->num_rf_chains; i++) {
4702 		if (ar->cfg_rx_chainmask & BIT(i))
4703 			ht_cap.mcs.rx_mask[i] = 0xFF;
4704 	}
4705 
4706 	ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
4707 
4708 	return ht_cap;
4709 }
4710 
4711 static void ath10k_mac_setup_ht_vht_cap(struct ath10k *ar)
4712 {
4713 	struct ieee80211_supported_band *band;
4714 	struct ieee80211_sta_vht_cap vht_cap;
4715 	struct ieee80211_sta_ht_cap ht_cap;
4716 
4717 	ht_cap = ath10k_get_ht_cap(ar);
4718 	vht_cap = ath10k_create_vht_cap(ar);
4719 
4720 	if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
4721 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
4722 		band->ht_cap = ht_cap;
4723 	}
4724 	if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
4725 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
4726 		band->ht_cap = ht_cap;
4727 		band->vht_cap = vht_cap;
4728 	}
4729 }
4730 
4731 static int __ath10k_set_antenna(struct ath10k *ar, u32 tx_ant, u32 rx_ant)
4732 {
4733 	int ret;
4734 	bool is_valid_tx_chain_mask, is_valid_rx_chain_mask;
4735 
4736 	lockdep_assert_held(&ar->conf_mutex);
4737 
4738 	is_valid_tx_chain_mask = ath10k_check_chain_mask(ar, tx_ant, "tx");
4739 	is_valid_rx_chain_mask = ath10k_check_chain_mask(ar, rx_ant, "rx");
4740 
4741 	if (!is_valid_tx_chain_mask || !is_valid_rx_chain_mask)
4742 		return -EINVAL;
4743 
4744 	ar->cfg_tx_chainmask = tx_ant;
4745 	ar->cfg_rx_chainmask = rx_ant;
4746 
4747 	if ((ar->state != ATH10K_STATE_ON) &&
4748 	    (ar->state != ATH10K_STATE_RESTARTED))
4749 		return 0;
4750 
4751 	ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->tx_chain_mask,
4752 					tx_ant);
4753 	if (ret) {
4754 		ath10k_warn(ar, "failed to set tx-chainmask: %d, req 0x%x\n",
4755 			    ret, tx_ant);
4756 		return ret;
4757 	}
4758 
4759 	ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->rx_chain_mask,
4760 					rx_ant);
4761 	if (ret) {
4762 		ath10k_warn(ar, "failed to set rx-chainmask: %d, req 0x%x\n",
4763 			    ret, rx_ant);
4764 		return ret;
4765 	}
4766 
4767 	/* Reload HT/VHT capability */
4768 	ath10k_mac_setup_ht_vht_cap(ar);
4769 
4770 	return 0;
4771 }
4772 
4773 static int ath10k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
4774 {
4775 	struct ath10k *ar = hw->priv;
4776 	int ret;
4777 
4778 	mutex_lock(&ar->conf_mutex);
4779 	ret = __ath10k_set_antenna(ar, tx_ant, rx_ant);
4780 	mutex_unlock(&ar->conf_mutex);
4781 	return ret;
4782 }
4783 
4784 static int __ath10k_fetch_bb_timing_dt(struct ath10k *ar,
4785 				       struct wmi_bb_timing_cfg_arg *bb_timing)
4786 {
4787 	struct device_node *node;
4788 	const char *fem_name;
4789 	int ret;
4790 
4791 	node = ar->dev->of_node;
4792 	if (!node)
4793 		return -ENOENT;
4794 
4795 	ret = of_property_read_string_index(node, "ext-fem-name", 0, &fem_name);
4796 	if (ret)
4797 		return -ENOENT;
4798 
4799 	/*
4800 	 * If external Front End module used in hardware, then default base band timing
4801 	 * parameter cannot be used since they were fine tuned for reference hardware,
4802 	 * so choosing different value suitable for that external FEM.
4803 	 */
4804 	if (!strcmp("microsemi-lx5586", fem_name)) {
4805 		bb_timing->bb_tx_timing = 0x00;
4806 		bb_timing->bb_xpa_timing = 0x0101;
4807 	} else {
4808 		return -ENOENT;
4809 	}
4810 
4811 	ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot bb_tx_timing 0x%x bb_xpa_timing 0x%x\n",
4812 		   bb_timing->bb_tx_timing, bb_timing->bb_xpa_timing);
4813 	return 0;
4814 }
4815 
4816 static int ath10k_mac_rfkill_config(struct ath10k *ar)
4817 {
4818 	u32 param;
4819 	int ret;
4820 
4821 	if (ar->hw_values->rfkill_pin == 0) {
4822 		ath10k_warn(ar, "ath10k does not support hardware rfkill with this device\n");
4823 		return -EOPNOTSUPP;
4824 	}
4825 
4826 	ath10k_dbg(ar, ATH10K_DBG_MAC,
4827 		   "mac rfkill_pin %d rfkill_cfg %d rfkill_on_level %d",
4828 		   ar->hw_values->rfkill_pin, ar->hw_values->rfkill_cfg,
4829 		   ar->hw_values->rfkill_on_level);
4830 
4831 	param = FIELD_PREP(WMI_TLV_RFKILL_CFG_RADIO_LEVEL,
4832 			   ar->hw_values->rfkill_on_level) |
4833 		FIELD_PREP(WMI_TLV_RFKILL_CFG_GPIO_PIN_NUM,
4834 			   ar->hw_values->rfkill_pin) |
4835 		FIELD_PREP(WMI_TLV_RFKILL_CFG_PIN_AS_GPIO,
4836 			   ar->hw_values->rfkill_cfg);
4837 
4838 	ret = ath10k_wmi_pdev_set_param(ar,
4839 					ar->wmi.pdev_param->rfkill_config,
4840 					param);
4841 	if (ret) {
4842 		ath10k_warn(ar,
4843 			    "failed to set rfkill config 0x%x: %d\n",
4844 			    param, ret);
4845 		return ret;
4846 	}
4847 	return 0;
4848 }
4849 
4850 int ath10k_mac_rfkill_enable_radio(struct ath10k *ar, bool enable)
4851 {
4852 	enum wmi_tlv_rfkill_enable_radio param;
4853 	int ret;
4854 
4855 	if (enable)
4856 		param = WMI_TLV_RFKILL_ENABLE_RADIO_ON;
4857 	else
4858 		param = WMI_TLV_RFKILL_ENABLE_RADIO_OFF;
4859 
4860 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac rfkill enable %d", param);
4861 
4862 	ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->rfkill_enable,
4863 					param);
4864 	if (ret) {
4865 		ath10k_warn(ar, "failed to set rfkill enable param %d: %d\n",
4866 			    param, ret);
4867 		return ret;
4868 	}
4869 
4870 	return 0;
4871 }
4872 
4873 static int ath10k_start(struct ieee80211_hw *hw)
4874 {
4875 	struct ath10k *ar = hw->priv;
4876 	u32 param;
4877 	int ret = 0;
4878 	struct wmi_bb_timing_cfg_arg bb_timing = {0};
4879 
4880 	/*
4881 	 * This makes sense only when restarting hw. It is harmless to call
4882 	 * unconditionally. This is necessary to make sure no HTT/WMI tx
4883 	 * commands will be submitted while restarting.
4884 	 */
4885 	ath10k_drain_tx(ar);
4886 
4887 	mutex_lock(&ar->conf_mutex);
4888 
4889 	switch (ar->state) {
4890 	case ATH10K_STATE_OFF:
4891 		ar->state = ATH10K_STATE_ON;
4892 		break;
4893 	case ATH10K_STATE_RESTARTING:
4894 		ar->state = ATH10K_STATE_RESTARTED;
4895 		break;
4896 	case ATH10K_STATE_ON:
4897 	case ATH10K_STATE_RESTARTED:
4898 	case ATH10K_STATE_WEDGED:
4899 		WARN_ON(1);
4900 		ret = -EINVAL;
4901 		goto err;
4902 	case ATH10K_STATE_UTF:
4903 		ret = -EBUSY;
4904 		goto err;
4905 	}
4906 
4907 	spin_lock_bh(&ar->data_lock);
4908 
4909 	if (ar->hw_rfkill_on) {
4910 		ar->hw_rfkill_on = false;
4911 		spin_unlock_bh(&ar->data_lock);
4912 		goto err;
4913 	}
4914 
4915 	spin_unlock_bh(&ar->data_lock);
4916 
4917 	ret = ath10k_hif_power_up(ar, ATH10K_FIRMWARE_MODE_NORMAL);
4918 	if (ret) {
4919 		ath10k_err(ar, "Could not init hif: %d\n", ret);
4920 		goto err_off;
4921 	}
4922 
4923 	ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_NORMAL,
4924 				&ar->normal_mode_fw);
4925 	if (ret) {
4926 		ath10k_err(ar, "Could not init core: %d\n", ret);
4927 		goto err_power_down;
4928 	}
4929 
4930 	if (ar->sys_cap_info & WMI_TLV_SYS_CAP_INFO_RFKILL) {
4931 		ret = ath10k_mac_rfkill_config(ar);
4932 		if (ret && ret != -EOPNOTSUPP) {
4933 			ath10k_warn(ar, "failed to configure rfkill: %d", ret);
4934 			goto err_core_stop;
4935 		}
4936 	}
4937 
4938 	param = ar->wmi.pdev_param->pmf_qos;
4939 	ret = ath10k_wmi_pdev_set_param(ar, param, 1);
4940 	if (ret) {
4941 		ath10k_warn(ar, "failed to enable PMF QOS: %d\n", ret);
4942 		goto err_core_stop;
4943 	}
4944 
4945 	param = ar->wmi.pdev_param->dynamic_bw;
4946 	ret = ath10k_wmi_pdev_set_param(ar, param, 1);
4947 	if (ret) {
4948 		ath10k_warn(ar, "failed to enable dynamic BW: %d\n", ret);
4949 		goto err_core_stop;
4950 	}
4951 
4952 	if (test_bit(WMI_SERVICE_SPOOF_MAC_SUPPORT, ar->wmi.svc_map)) {
4953 		ret = ath10k_wmi_scan_prob_req_oui(ar, ar->mac_addr);
4954 		if (ret) {
4955 			ath10k_err(ar, "failed to set prob req oui: %i\n", ret);
4956 			goto err_core_stop;
4957 		}
4958 	}
4959 
4960 	if (test_bit(WMI_SERVICE_ADAPTIVE_OCS, ar->wmi.svc_map)) {
4961 		ret = ath10k_wmi_adaptive_qcs(ar, true);
4962 		if (ret) {
4963 			ath10k_warn(ar, "failed to enable adaptive qcs: %d\n",
4964 				    ret);
4965 			goto err_core_stop;
4966 		}
4967 	}
4968 
4969 	if (test_bit(WMI_SERVICE_BURST, ar->wmi.svc_map)) {
4970 		param = ar->wmi.pdev_param->burst_enable;
4971 		ret = ath10k_wmi_pdev_set_param(ar, param, 0);
4972 		if (ret) {
4973 			ath10k_warn(ar, "failed to disable burst: %d\n", ret);
4974 			goto err_core_stop;
4975 		}
4976 	}
4977 
4978 	param = ar->wmi.pdev_param->idle_ps_config;
4979 	ret = ath10k_wmi_pdev_set_param(ar, param, 1);
4980 	if (ret && ret != -EOPNOTSUPP) {
4981 		ath10k_warn(ar, "failed to enable idle_ps_config: %d\n", ret);
4982 		goto err_core_stop;
4983 	}
4984 
4985 	__ath10k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
4986 
4987 	/*
4988 	 * By default FW set ARP frames ac to voice (6). In that case ARP
4989 	 * exchange is not working properly for UAPSD enabled AP. ARP requests
4990 	 * which arrives with access category 0 are processed by network stack
4991 	 * and send back with access category 0, but FW changes access category
4992 	 * to 6. Set ARP frames access category to best effort (0) solves
4993 	 * this problem.
4994 	 */
4995 
4996 	param = ar->wmi.pdev_param->arp_ac_override;
4997 	ret = ath10k_wmi_pdev_set_param(ar, param, 0);
4998 	if (ret) {
4999 		ath10k_warn(ar, "failed to set arp ac override parameter: %d\n",
5000 			    ret);
5001 		goto err_core_stop;
5002 	}
5003 
5004 	if (test_bit(ATH10K_FW_FEATURE_SUPPORTS_ADAPTIVE_CCA,
5005 		     ar->running_fw->fw_file.fw_features)) {
5006 		ret = ath10k_wmi_pdev_enable_adaptive_cca(ar, 1,
5007 							  WMI_CCA_DETECT_LEVEL_AUTO,
5008 							  WMI_CCA_DETECT_MARGIN_AUTO);
5009 		if (ret) {
5010 			ath10k_warn(ar, "failed to enable adaptive cca: %d\n",
5011 				    ret);
5012 			goto err_core_stop;
5013 		}
5014 	}
5015 
5016 	param = ar->wmi.pdev_param->ani_enable;
5017 	ret = ath10k_wmi_pdev_set_param(ar, param, 1);
5018 	if (ret) {
5019 		ath10k_warn(ar, "failed to enable ani by default: %d\n",
5020 			    ret);
5021 		goto err_core_stop;
5022 	}
5023 
5024 	ar->ani_enabled = true;
5025 
5026 	if (ath10k_peer_stats_enabled(ar)) {
5027 		param = ar->wmi.pdev_param->peer_stats_update_period;
5028 		ret = ath10k_wmi_pdev_set_param(ar, param,
5029 						PEER_DEFAULT_STATS_UPDATE_PERIOD);
5030 		if (ret) {
5031 			ath10k_warn(ar,
5032 				    "failed to set peer stats period : %d\n",
5033 				    ret);
5034 			goto err_core_stop;
5035 		}
5036 	}
5037 
5038 	param = ar->wmi.pdev_param->enable_btcoex;
5039 	if (test_bit(WMI_SERVICE_COEX_GPIO, ar->wmi.svc_map) &&
5040 	    test_bit(ATH10K_FW_FEATURE_BTCOEX_PARAM,
5041 		     ar->running_fw->fw_file.fw_features) &&
5042 	    ar->coex_support) {
5043 		ret = ath10k_wmi_pdev_set_param(ar, param, 0);
5044 		if (ret) {
5045 			ath10k_warn(ar,
5046 				    "failed to set btcoex param: %d\n", ret);
5047 			goto err_core_stop;
5048 		}
5049 		clear_bit(ATH10K_FLAG_BTCOEX, &ar->dev_flags);
5050 	}
5051 
5052 	if (test_bit(WMI_SERVICE_BB_TIMING_CONFIG_SUPPORT, ar->wmi.svc_map)) {
5053 		ret = __ath10k_fetch_bb_timing_dt(ar, &bb_timing);
5054 		if (!ret) {
5055 			ret = ath10k_wmi_pdev_bb_timing(ar, &bb_timing);
5056 			if (ret) {
5057 				ath10k_warn(ar,
5058 					    "failed to set bb timings: %d\n",
5059 					    ret);
5060 				goto err_core_stop;
5061 			}
5062 		}
5063 	}
5064 
5065 	ar->num_started_vdevs = 0;
5066 	ath10k_regd_update(ar);
5067 
5068 	ath10k_spectral_start(ar);
5069 	ath10k_thermal_set_throttling(ar);
5070 
5071 	ar->radar_conf_state = ATH10K_RADAR_CONFIRMATION_IDLE;
5072 
5073 	mutex_unlock(&ar->conf_mutex);
5074 	return 0;
5075 
5076 err_core_stop:
5077 	ath10k_core_stop(ar);
5078 
5079 err_power_down:
5080 	ath10k_hif_power_down(ar);
5081 
5082 err_off:
5083 	ar->state = ATH10K_STATE_OFF;
5084 
5085 err:
5086 	mutex_unlock(&ar->conf_mutex);
5087 	return ret;
5088 }
5089 
5090 static void ath10k_stop(struct ieee80211_hw *hw)
5091 {
5092 	struct ath10k *ar = hw->priv;
5093 
5094 	ath10k_drain_tx(ar);
5095 
5096 	mutex_lock(&ar->conf_mutex);
5097 	if (ar->state != ATH10K_STATE_OFF) {
5098 		if (!ar->hw_rfkill_on)
5099 			ath10k_halt(ar);
5100 		ar->state = ATH10K_STATE_OFF;
5101 	}
5102 	mutex_unlock(&ar->conf_mutex);
5103 
5104 	cancel_work_sync(&ar->set_coverage_class_work);
5105 	cancel_delayed_work_sync(&ar->scan.timeout);
5106 	cancel_work_sync(&ar->restart_work);
5107 }
5108 
5109 static int ath10k_config_ps(struct ath10k *ar)
5110 {
5111 	struct ath10k_vif *arvif;
5112 	int ret = 0;
5113 
5114 	lockdep_assert_held(&ar->conf_mutex);
5115 
5116 	list_for_each_entry(arvif, &ar->arvifs, list) {
5117 		ret = ath10k_mac_vif_setup_ps(arvif);
5118 		if (ret) {
5119 			ath10k_warn(ar, "failed to setup powersave: %d\n", ret);
5120 			break;
5121 		}
5122 	}
5123 
5124 	return ret;
5125 }
5126 
5127 static int ath10k_mac_txpower_setup(struct ath10k *ar, int txpower)
5128 {
5129 	int ret;
5130 	u32 param;
5131 
5132 	lockdep_assert_held(&ar->conf_mutex);
5133 
5134 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac txpower %d\n", txpower);
5135 
5136 	param = ar->wmi.pdev_param->txpower_limit2g;
5137 	ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
5138 	if (ret) {
5139 		ath10k_warn(ar, "failed to set 2g txpower %d: %d\n",
5140 			    txpower, ret);
5141 		return ret;
5142 	}
5143 
5144 	param = ar->wmi.pdev_param->txpower_limit5g;
5145 	ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
5146 	if (ret) {
5147 		ath10k_warn(ar, "failed to set 5g txpower %d: %d\n",
5148 			    txpower, ret);
5149 		return ret;
5150 	}
5151 
5152 	return 0;
5153 }
5154 
5155 static int ath10k_mac_txpower_recalc(struct ath10k *ar)
5156 {
5157 	struct ath10k_vif *arvif;
5158 	int ret, txpower = -1;
5159 
5160 	lockdep_assert_held(&ar->conf_mutex);
5161 
5162 	list_for_each_entry(arvif, &ar->arvifs, list) {
5163 		/* txpower not initialized yet? */
5164 		if (arvif->txpower == INT_MIN)
5165 			continue;
5166 
5167 		if (txpower == -1)
5168 			txpower = arvif->txpower;
5169 		else
5170 			txpower = min(txpower, arvif->txpower);
5171 	}
5172 
5173 	if (txpower == -1)
5174 		return 0;
5175 
5176 	ret = ath10k_mac_txpower_setup(ar, txpower);
5177 	if (ret) {
5178 		ath10k_warn(ar, "failed to setup tx power %d: %d\n",
5179 			    txpower, ret);
5180 		return ret;
5181 	}
5182 
5183 	return 0;
5184 }
5185 
5186 static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
5187 {
5188 	struct ath10k *ar = hw->priv;
5189 	struct ieee80211_conf *conf = &hw->conf;
5190 	int ret = 0;
5191 
5192 	mutex_lock(&ar->conf_mutex);
5193 
5194 	if (changed & IEEE80211_CONF_CHANGE_PS)
5195 		ath10k_config_ps(ar);
5196 
5197 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
5198 		ar->monitor = conf->flags & IEEE80211_CONF_MONITOR;
5199 		ret = ath10k_monitor_recalc(ar);
5200 		if (ret)
5201 			ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
5202 	}
5203 
5204 	mutex_unlock(&ar->conf_mutex);
5205 	return ret;
5206 }
5207 
5208 static u32 get_nss_from_chainmask(u16 chain_mask)
5209 {
5210 	if ((chain_mask & 0xf) == 0xf)
5211 		return 4;
5212 	else if ((chain_mask & 0x7) == 0x7)
5213 		return 3;
5214 	else if ((chain_mask & 0x3) == 0x3)
5215 		return 2;
5216 	return 1;
5217 }
5218 
5219 static int ath10k_mac_set_txbf_conf(struct ath10k_vif *arvif)
5220 {
5221 	u32 value = 0;
5222 	struct ath10k *ar = arvif->ar;
5223 	int nsts;
5224 	int sound_dim;
5225 
5226 	if (ath10k_wmi_get_txbf_conf_scheme(ar) != WMI_TXBF_CONF_BEFORE_ASSOC)
5227 		return 0;
5228 
5229 	nsts = ath10k_mac_get_vht_cap_bf_sts(ar);
5230 	if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
5231 				IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE))
5232 		value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
5233 
5234 	sound_dim = ath10k_mac_get_vht_cap_bf_sound_dim(ar);
5235 	if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
5236 				IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE))
5237 		value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
5238 
5239 	if (!value)
5240 		return 0;
5241 
5242 	if (ar->vht_cap_info & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)
5243 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
5244 
5245 	if (ar->vht_cap_info & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)
5246 		value |= (WMI_VDEV_PARAM_TXBF_MU_TX_BFER |
5247 			  WMI_VDEV_PARAM_TXBF_SU_TX_BFER);
5248 
5249 	if (ar->vht_cap_info & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)
5250 		value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
5251 
5252 	if (ar->vht_cap_info & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)
5253 		value |= (WMI_VDEV_PARAM_TXBF_MU_TX_BFEE |
5254 			  WMI_VDEV_PARAM_TXBF_SU_TX_BFEE);
5255 
5256 	return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
5257 					 ar->wmi.vdev_param->txbf, value);
5258 }
5259 
5260 /*
5261  * TODO:
5262  * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
5263  * because we will send mgmt frames without CCK. This requirement
5264  * for P2P_FIND/GO_NEG should be handled by checking CCK flag
5265  * in the TX packet.
5266  */
5267 static int ath10k_add_interface(struct ieee80211_hw *hw,
5268 				struct ieee80211_vif *vif)
5269 {
5270 	struct ath10k *ar = hw->priv;
5271 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
5272 	struct ath10k_peer *peer;
5273 	enum wmi_sta_powersave_param param;
5274 	int ret = 0;
5275 	u32 value;
5276 	int bit;
5277 	int i;
5278 	u32 vdev_param;
5279 
5280 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
5281 
5282 	mutex_lock(&ar->conf_mutex);
5283 
5284 	memset(arvif, 0, sizeof(*arvif));
5285 	ath10k_mac_txq_init(vif->txq);
5286 
5287 	arvif->ar = ar;
5288 	arvif->vif = vif;
5289 
5290 	INIT_LIST_HEAD(&arvif->list);
5291 	INIT_WORK(&arvif->ap_csa_work, ath10k_mac_vif_ap_csa_work);
5292 	INIT_DELAYED_WORK(&arvif->connection_loss_work,
5293 			  ath10k_mac_vif_sta_connection_loss_work);
5294 
5295 	for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
5296 		arvif->bitrate_mask.control[i].legacy = 0xffffffff;
5297 		memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
5298 		       sizeof(arvif->bitrate_mask.control[i].ht_mcs));
5299 		memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
5300 		       sizeof(arvif->bitrate_mask.control[i].vht_mcs));
5301 	}
5302 
5303 	if (ar->num_peers >= ar->max_num_peers) {
5304 		ath10k_warn(ar, "refusing vdev creation due to insufficient peer entry resources in firmware\n");
5305 		ret = -ENOBUFS;
5306 		goto err;
5307 	}
5308 
5309 	if (ar->free_vdev_map == 0) {
5310 		ath10k_warn(ar, "Free vdev map is empty, no more interfaces allowed.\n");
5311 		ret = -EBUSY;
5312 		goto err;
5313 	}
5314 	bit = __ffs64(ar->free_vdev_map);
5315 
5316 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac create vdev %i map %llx\n",
5317 		   bit, ar->free_vdev_map);
5318 
5319 	arvif->vdev_id = bit;
5320 	arvif->vdev_subtype =
5321 		ath10k_wmi_get_vdev_subtype(ar, WMI_VDEV_SUBTYPE_NONE);
5322 
5323 	switch (vif->type) {
5324 	case NL80211_IFTYPE_P2P_DEVICE:
5325 		arvif->vdev_type = WMI_VDEV_TYPE_STA;
5326 		arvif->vdev_subtype = ath10k_wmi_get_vdev_subtype
5327 					(ar, WMI_VDEV_SUBTYPE_P2P_DEVICE);
5328 		break;
5329 	case NL80211_IFTYPE_UNSPECIFIED:
5330 	case NL80211_IFTYPE_STATION:
5331 		arvif->vdev_type = WMI_VDEV_TYPE_STA;
5332 		if (vif->p2p)
5333 			arvif->vdev_subtype = ath10k_wmi_get_vdev_subtype
5334 					(ar, WMI_VDEV_SUBTYPE_P2P_CLIENT);
5335 		break;
5336 	case NL80211_IFTYPE_ADHOC:
5337 		arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
5338 		break;
5339 	case NL80211_IFTYPE_MESH_POINT:
5340 		if (test_bit(WMI_SERVICE_MESH_11S, ar->wmi.svc_map)) {
5341 			arvif->vdev_subtype = ath10k_wmi_get_vdev_subtype
5342 						(ar, WMI_VDEV_SUBTYPE_MESH_11S);
5343 		} else if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags)) {
5344 			ret = -EINVAL;
5345 			ath10k_warn(ar, "must load driver with rawmode=1 to add mesh interfaces\n");
5346 			goto err;
5347 		}
5348 		arvif->vdev_type = WMI_VDEV_TYPE_AP;
5349 		break;
5350 	case NL80211_IFTYPE_AP:
5351 		arvif->vdev_type = WMI_VDEV_TYPE_AP;
5352 
5353 		if (vif->p2p)
5354 			arvif->vdev_subtype = ath10k_wmi_get_vdev_subtype
5355 						(ar, WMI_VDEV_SUBTYPE_P2P_GO);
5356 		break;
5357 	case NL80211_IFTYPE_MONITOR:
5358 		arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
5359 		break;
5360 	default:
5361 		WARN_ON(1);
5362 		break;
5363 	}
5364 
5365 	/* Using vdev_id as queue number will make it very easy to do per-vif
5366 	 * tx queue locking. This shouldn't wrap due to interface combinations
5367 	 * but do a modulo for correctness sake and prevent using offchannel tx
5368 	 * queues for regular vif tx.
5369 	 */
5370 	vif->cab_queue = arvif->vdev_id % (IEEE80211_MAX_QUEUES - 1);
5371 	for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
5372 		vif->hw_queue[i] = arvif->vdev_id % (IEEE80211_MAX_QUEUES - 1);
5373 
5374 	/* Some firmware revisions don't wait for beacon tx completion before
5375 	 * sending another SWBA event. This could lead to hardware using old
5376 	 * (freed) beacon data in some cases, e.g. tx credit starvation
5377 	 * combined with missed TBTT. This is very very rare.
5378 	 *
5379 	 * On non-IOMMU-enabled hosts this could be a possible security issue
5380 	 * because hw could beacon some random data on the air.  On
5381 	 * IOMMU-enabled hosts DMAR faults would occur in most cases and target
5382 	 * device would crash.
5383 	 *
5384 	 * Since there are no beacon tx completions (implicit nor explicit)
5385 	 * propagated to host the only workaround for this is to allocate a
5386 	 * DMA-coherent buffer for a lifetime of a vif and use it for all
5387 	 * beacon tx commands. Worst case for this approach is some beacons may
5388 	 * become corrupted, e.g. have garbled IEs or out-of-date TIM bitmap.
5389 	 */
5390 	if (vif->type == NL80211_IFTYPE_ADHOC ||
5391 	    vif->type == NL80211_IFTYPE_MESH_POINT ||
5392 	    vif->type == NL80211_IFTYPE_AP) {
5393 		arvif->beacon_buf = dma_alloc_coherent(ar->dev,
5394 						       IEEE80211_MAX_FRAME_LEN,
5395 						       &arvif->beacon_paddr,
5396 						       GFP_ATOMIC);
5397 		if (!arvif->beacon_buf) {
5398 			ret = -ENOMEM;
5399 			ath10k_warn(ar, "failed to allocate beacon buffer: %d\n",
5400 				    ret);
5401 			goto err;
5402 		}
5403 	}
5404 	if (test_bit(ATH10K_FLAG_HW_CRYPTO_DISABLED, &ar->dev_flags))
5405 		arvif->nohwcrypt = true;
5406 
5407 	if (arvif->nohwcrypt &&
5408 	    !test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags)) {
5409 		ath10k_warn(ar, "cryptmode module param needed for sw crypto\n");
5410 		goto err;
5411 	}
5412 
5413 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d bcnmode %s\n",
5414 		   arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
5415 		   arvif->beacon_buf ? "single-buf" : "per-skb");
5416 
5417 	ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
5418 				     arvif->vdev_subtype, vif->addr);
5419 	if (ret) {
5420 		ath10k_warn(ar, "failed to create WMI vdev %i: %d\n",
5421 			    arvif->vdev_id, ret);
5422 		goto err;
5423 	}
5424 
5425 	if (test_bit(WMI_SERVICE_VDEV_DISABLE_4_ADDR_SRC_LRN_SUPPORT,
5426 		     ar->wmi.svc_map)) {
5427 		vdev_param = ar->wmi.vdev_param->disable_4addr_src_lrn;
5428 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5429 						WMI_VDEV_DISABLE_4_ADDR_SRC_LRN);
5430 		if (ret && ret != -EOPNOTSUPP) {
5431 			ath10k_warn(ar, "failed to disable 4addr src lrn vdev %i: %d\n",
5432 				    arvif->vdev_id, ret);
5433 		}
5434 	}
5435 
5436 	ar->free_vdev_map &= ~(1LL << arvif->vdev_id);
5437 	spin_lock_bh(&ar->data_lock);
5438 	list_add(&arvif->list, &ar->arvifs);
5439 	spin_unlock_bh(&ar->data_lock);
5440 
5441 	/* It makes no sense to have firmware do keepalives. mac80211 already
5442 	 * takes care of this with idle connection polling.
5443 	 */
5444 	ret = ath10k_mac_vif_disable_keepalive(arvif);
5445 	if (ret) {
5446 		ath10k_warn(ar, "failed to disable keepalive on vdev %i: %d\n",
5447 			    arvif->vdev_id, ret);
5448 		goto err_vdev_delete;
5449 	}
5450 
5451 	arvif->def_wep_key_idx = -1;
5452 
5453 	vdev_param = ar->wmi.vdev_param->tx_encap_type;
5454 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5455 					ATH10K_HW_TXRX_NATIVE_WIFI);
5456 	/* 10.X firmware does not support this VDEV parameter. Do not warn */
5457 	if (ret && ret != -EOPNOTSUPP) {
5458 		ath10k_warn(ar, "failed to set vdev %i TX encapsulation: %d\n",
5459 			    arvif->vdev_id, ret);
5460 		goto err_vdev_delete;
5461 	}
5462 
5463 	/* Configuring number of spatial stream for monitor interface is causing
5464 	 * target assert in qca9888 and qca6174.
5465 	 */
5466 	if (ar->cfg_tx_chainmask && (vif->type != NL80211_IFTYPE_MONITOR)) {
5467 		u16 nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
5468 
5469 		vdev_param = ar->wmi.vdev_param->nss;
5470 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5471 						nss);
5472 		if (ret) {
5473 			ath10k_warn(ar, "failed to set vdev %i chainmask 0x%x, nss %i: %d\n",
5474 				    arvif->vdev_id, ar->cfg_tx_chainmask, nss,
5475 				    ret);
5476 			goto err_vdev_delete;
5477 		}
5478 	}
5479 
5480 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
5481 	    arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
5482 		ret = ath10k_peer_create(ar, vif, NULL, arvif->vdev_id,
5483 					 vif->addr, WMI_PEER_TYPE_DEFAULT);
5484 		if (ret) {
5485 			ath10k_warn(ar, "failed to create vdev %i peer for AP/IBSS: %d\n",
5486 				    arvif->vdev_id, ret);
5487 			goto err_vdev_delete;
5488 		}
5489 
5490 		spin_lock_bh(&ar->data_lock);
5491 
5492 		peer = ath10k_peer_find(ar, arvif->vdev_id, vif->addr);
5493 		if (!peer) {
5494 			ath10k_warn(ar, "failed to lookup peer %pM on vdev %i\n",
5495 				    vif->addr, arvif->vdev_id);
5496 			spin_unlock_bh(&ar->data_lock);
5497 			ret = -ENOENT;
5498 			goto err_peer_delete;
5499 		}
5500 
5501 		arvif->peer_id = find_first_bit(peer->peer_ids,
5502 						ATH10K_MAX_NUM_PEER_IDS);
5503 
5504 		spin_unlock_bh(&ar->data_lock);
5505 	} else {
5506 		arvif->peer_id = HTT_INVALID_PEERID;
5507 	}
5508 
5509 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
5510 		ret = ath10k_mac_set_kickout(arvif);
5511 		if (ret) {
5512 			ath10k_warn(ar, "failed to set vdev %i kickout parameters: %d\n",
5513 				    arvif->vdev_id, ret);
5514 			goto err_peer_delete;
5515 		}
5516 	}
5517 
5518 	if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
5519 		param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
5520 		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
5521 		ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
5522 						  param, value);
5523 		if (ret) {
5524 			ath10k_warn(ar, "failed to set vdev %i RX wake policy: %d\n",
5525 				    arvif->vdev_id, ret);
5526 			goto err_peer_delete;
5527 		}
5528 
5529 		ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
5530 		if (ret) {
5531 			ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
5532 				    arvif->vdev_id, ret);
5533 			goto err_peer_delete;
5534 		}
5535 
5536 		ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
5537 		if (ret) {
5538 			ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
5539 				    arvif->vdev_id, ret);
5540 			goto err_peer_delete;
5541 		}
5542 	}
5543 
5544 	ret = ath10k_mac_set_txbf_conf(arvif);
5545 	if (ret) {
5546 		ath10k_warn(ar, "failed to set txbf for vdev %d: %d\n",
5547 			    arvif->vdev_id, ret);
5548 		goto err_peer_delete;
5549 	}
5550 
5551 	ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
5552 	if (ret) {
5553 		ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
5554 			    arvif->vdev_id, ret);
5555 		goto err_peer_delete;
5556 	}
5557 
5558 	arvif->txpower = vif->bss_conf.txpower;
5559 	ret = ath10k_mac_txpower_recalc(ar);
5560 	if (ret) {
5561 		ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
5562 		goto err_peer_delete;
5563 	}
5564 
5565 	if (test_bit(WMI_SERVICE_RTT_RESPONDER_ROLE, ar->wmi.svc_map)) {
5566 		vdev_param = ar->wmi.vdev_param->rtt_responder_role;
5567 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5568 						arvif->ftm_responder);
5569 
5570 		/* It is harmless to not set FTM role. Do not warn */
5571 		if (ret && ret != -EOPNOTSUPP)
5572 			ath10k_warn(ar, "failed to set vdev %i FTM Responder: %d\n",
5573 				    arvif->vdev_id, ret);
5574 	}
5575 
5576 	if (vif->type == NL80211_IFTYPE_MONITOR) {
5577 		ar->monitor_arvif = arvif;
5578 		ret = ath10k_monitor_recalc(ar);
5579 		if (ret) {
5580 			ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
5581 			goto err_peer_delete;
5582 		}
5583 	}
5584 
5585 	spin_lock_bh(&ar->htt.tx_lock);
5586 	if (!ar->tx_paused)
5587 		ieee80211_wake_queue(ar->hw, arvif->vdev_id);
5588 	spin_unlock_bh(&ar->htt.tx_lock);
5589 
5590 	mutex_unlock(&ar->conf_mutex);
5591 	return 0;
5592 
5593 err_peer_delete:
5594 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
5595 	    arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
5596 		ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
5597 		ath10k_wait_for_peer_delete_done(ar, arvif->vdev_id,
5598 						 vif->addr);
5599 	}
5600 
5601 err_vdev_delete:
5602 	ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
5603 	ar->free_vdev_map |= 1LL << arvif->vdev_id;
5604 	spin_lock_bh(&ar->data_lock);
5605 	list_del(&arvif->list);
5606 	spin_unlock_bh(&ar->data_lock);
5607 
5608 err:
5609 	if (arvif->beacon_buf) {
5610 		dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
5611 				  arvif->beacon_buf, arvif->beacon_paddr);
5612 		arvif->beacon_buf = NULL;
5613 	}
5614 
5615 	mutex_unlock(&ar->conf_mutex);
5616 
5617 	return ret;
5618 }
5619 
5620 static void ath10k_mac_vif_tx_unlock_all(struct ath10k_vif *arvif)
5621 {
5622 	int i;
5623 
5624 	for (i = 0; i < BITS_PER_LONG; i++)
5625 		ath10k_mac_vif_tx_unlock(arvif, i);
5626 }
5627 
5628 static void ath10k_remove_interface(struct ieee80211_hw *hw,
5629 				    struct ieee80211_vif *vif)
5630 {
5631 	struct ath10k *ar = hw->priv;
5632 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
5633 	struct ath10k_peer *peer;
5634 	unsigned long time_left;
5635 	int ret;
5636 	int i;
5637 
5638 	cancel_work_sync(&arvif->ap_csa_work);
5639 	cancel_delayed_work_sync(&arvif->connection_loss_work);
5640 
5641 	mutex_lock(&ar->conf_mutex);
5642 
5643 	ret = ath10k_spectral_vif_stop(arvif);
5644 	if (ret)
5645 		ath10k_warn(ar, "failed to stop spectral for vdev %i: %d\n",
5646 			    arvif->vdev_id, ret);
5647 
5648 	ar->free_vdev_map |= 1LL << arvif->vdev_id;
5649 	spin_lock_bh(&ar->data_lock);
5650 	list_del(&arvif->list);
5651 	spin_unlock_bh(&ar->data_lock);
5652 
5653 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
5654 	    arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
5655 		ret = ath10k_wmi_peer_delete(arvif->ar, arvif->vdev_id,
5656 					     vif->addr);
5657 		if (ret)
5658 			ath10k_warn(ar, "failed to submit AP/IBSS self-peer removal on vdev %i: %d\n",
5659 				    arvif->vdev_id, ret);
5660 
5661 		ath10k_wait_for_peer_delete_done(ar, arvif->vdev_id,
5662 						 vif->addr);
5663 		kfree(arvif->u.ap.noa_data);
5664 	}
5665 
5666 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i delete (remove interface)\n",
5667 		   arvif->vdev_id);
5668 
5669 	ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
5670 	if (ret)
5671 		ath10k_warn(ar, "failed to delete WMI vdev %i: %d\n",
5672 			    arvif->vdev_id, ret);
5673 
5674 	if (test_bit(WMI_SERVICE_SYNC_DELETE_CMDS, ar->wmi.svc_map)) {
5675 		time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
5676 							ATH10K_VDEV_DELETE_TIMEOUT_HZ);
5677 		if (time_left == 0) {
5678 			ath10k_warn(ar, "Timeout in receiving vdev delete response\n");
5679 			goto out;
5680 		}
5681 	}
5682 
5683 	/* Some firmware revisions don't notify host about self-peer removal
5684 	 * until after associated vdev is deleted.
5685 	 */
5686 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
5687 	    arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
5688 		ret = ath10k_wait_for_peer_deleted(ar, arvif->vdev_id,
5689 						   vif->addr);
5690 		if (ret)
5691 			ath10k_warn(ar, "failed to remove AP self-peer on vdev %i: %d\n",
5692 				    arvif->vdev_id, ret);
5693 
5694 		spin_lock_bh(&ar->data_lock);
5695 		ar->num_peers--;
5696 		spin_unlock_bh(&ar->data_lock);
5697 	}
5698 
5699 	spin_lock_bh(&ar->data_lock);
5700 	for (i = 0; i < ARRAY_SIZE(ar->peer_map); i++) {
5701 		peer = ar->peer_map[i];
5702 		if (!peer)
5703 			continue;
5704 
5705 		if (peer->vif == vif) {
5706 			ath10k_warn(ar, "found vif peer %pM entry on vdev %i after it was supposedly removed\n",
5707 				    vif->addr, arvif->vdev_id);
5708 			peer->vif = NULL;
5709 		}
5710 	}
5711 
5712 	/* Clean this up late, less opportunity for firmware to access
5713 	 * DMA memory we have deleted.
5714 	 */
5715 	ath10k_mac_vif_beacon_cleanup(arvif);
5716 	spin_unlock_bh(&ar->data_lock);
5717 
5718 	ath10k_peer_cleanup(ar, arvif->vdev_id);
5719 	ath10k_mac_txq_unref(ar, vif->txq);
5720 
5721 	if (vif->type == NL80211_IFTYPE_MONITOR) {
5722 		ar->monitor_arvif = NULL;
5723 		ret = ath10k_monitor_recalc(ar);
5724 		if (ret)
5725 			ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
5726 	}
5727 
5728 	ret = ath10k_mac_txpower_recalc(ar);
5729 	if (ret)
5730 		ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
5731 
5732 	spin_lock_bh(&ar->htt.tx_lock);
5733 	ath10k_mac_vif_tx_unlock_all(arvif);
5734 	spin_unlock_bh(&ar->htt.tx_lock);
5735 
5736 	ath10k_mac_txq_unref(ar, vif->txq);
5737 
5738 out:
5739 	mutex_unlock(&ar->conf_mutex);
5740 }
5741 
5742 /*
5743  * FIXME: Has to be verified.
5744  */
5745 #define SUPPORTED_FILTERS			\
5746 	(FIF_ALLMULTI |				\
5747 	FIF_CONTROL |				\
5748 	FIF_PSPOLL |				\
5749 	FIF_OTHER_BSS |				\
5750 	FIF_BCN_PRBRESP_PROMISC |		\
5751 	FIF_PROBE_REQ |				\
5752 	FIF_FCSFAIL)
5753 
5754 static void ath10k_configure_filter(struct ieee80211_hw *hw,
5755 				    unsigned int changed_flags,
5756 				    unsigned int *total_flags,
5757 				    u64 multicast)
5758 {
5759 	struct ath10k *ar = hw->priv;
5760 	int ret;
5761 
5762 	mutex_lock(&ar->conf_mutex);
5763 
5764 	changed_flags &= SUPPORTED_FILTERS;
5765 	*total_flags &= SUPPORTED_FILTERS;
5766 	ar->filter_flags = *total_flags;
5767 
5768 	ret = ath10k_monitor_recalc(ar);
5769 	if (ret)
5770 		ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
5771 
5772 	mutex_unlock(&ar->conf_mutex);
5773 }
5774 
5775 static void ath10k_recalculate_mgmt_rate(struct ath10k *ar,
5776 					 struct ieee80211_vif *vif,
5777 					 struct cfg80211_chan_def *def)
5778 {
5779 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
5780 	const struct ieee80211_supported_band *sband;
5781 	u8 basic_rate_idx;
5782 	int hw_rate_code;
5783 	u32 vdev_param;
5784 	u16 bitrate;
5785 	int ret;
5786 
5787 	lockdep_assert_held(&ar->conf_mutex);
5788 
5789 	sband = ar->hw->wiphy->bands[def->chan->band];
5790 	basic_rate_idx = ffs(vif->bss_conf.basic_rates) - 1;
5791 	bitrate = sband->bitrates[basic_rate_idx].bitrate;
5792 
5793 	hw_rate_code = ath10k_mac_get_rate_hw_value(bitrate);
5794 	if (hw_rate_code < 0) {
5795 		ath10k_warn(ar, "bitrate not supported %d\n", bitrate);
5796 		return;
5797 	}
5798 
5799 	vdev_param = ar->wmi.vdev_param->mgmt_rate;
5800 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5801 					hw_rate_code);
5802 	if (ret)
5803 		ath10k_warn(ar, "failed to set mgmt tx rate %d\n", ret);
5804 }
5805 
5806 static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
5807 				    struct ieee80211_vif *vif,
5808 				    struct ieee80211_bss_conf *info,
5809 				    u32 changed)
5810 {
5811 	struct ath10k *ar = hw->priv;
5812 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
5813 	struct cfg80211_chan_def def;
5814 	u32 vdev_param, pdev_param, slottime, preamble;
5815 	u16 bitrate, hw_value;
5816 	u8 rate, rateidx;
5817 	int ret = 0, mcast_rate;
5818 	enum nl80211_band band;
5819 
5820 	mutex_lock(&ar->conf_mutex);
5821 
5822 	if (changed & BSS_CHANGED_IBSS)
5823 		ath10k_control_ibss(arvif, info, vif->addr);
5824 
5825 	if (changed & BSS_CHANGED_BEACON_INT) {
5826 		arvif->beacon_interval = info->beacon_int;
5827 		vdev_param = ar->wmi.vdev_param->beacon_interval;
5828 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5829 						arvif->beacon_interval);
5830 		ath10k_dbg(ar, ATH10K_DBG_MAC,
5831 			   "mac vdev %d beacon_interval %d\n",
5832 			   arvif->vdev_id, arvif->beacon_interval);
5833 
5834 		if (ret)
5835 			ath10k_warn(ar, "failed to set beacon interval for vdev %d: %i\n",
5836 				    arvif->vdev_id, ret);
5837 	}
5838 
5839 	if (changed & BSS_CHANGED_BEACON) {
5840 		ath10k_dbg(ar, ATH10K_DBG_MAC,
5841 			   "vdev %d set beacon tx mode to staggered\n",
5842 			   arvif->vdev_id);
5843 
5844 		pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
5845 		ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
5846 						WMI_BEACON_STAGGERED_MODE);
5847 		if (ret)
5848 			ath10k_warn(ar, "failed to set beacon mode for vdev %d: %i\n",
5849 				    arvif->vdev_id, ret);
5850 
5851 		ret = ath10k_mac_setup_bcn_tmpl(arvif);
5852 		if (ret)
5853 			ath10k_warn(ar, "failed to update beacon template: %d\n",
5854 				    ret);
5855 
5856 		if (ieee80211_vif_is_mesh(vif)) {
5857 			/* mesh doesn't use SSID but firmware needs it */
5858 			strncpy(arvif->u.ap.ssid, "mesh",
5859 				sizeof(arvif->u.ap.ssid));
5860 			arvif->u.ap.ssid_len = 4;
5861 		}
5862 	}
5863 
5864 	if (changed & BSS_CHANGED_AP_PROBE_RESP) {
5865 		ret = ath10k_mac_setup_prb_tmpl(arvif);
5866 		if (ret)
5867 			ath10k_warn(ar, "failed to setup probe resp template on vdev %i: %d\n",
5868 				    arvif->vdev_id, ret);
5869 	}
5870 
5871 	if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
5872 		arvif->dtim_period = info->dtim_period;
5873 
5874 		ath10k_dbg(ar, ATH10K_DBG_MAC,
5875 			   "mac vdev %d dtim_period %d\n",
5876 			   arvif->vdev_id, arvif->dtim_period);
5877 
5878 		vdev_param = ar->wmi.vdev_param->dtim_period;
5879 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5880 						arvif->dtim_period);
5881 		if (ret)
5882 			ath10k_warn(ar, "failed to set dtim period for vdev %d: %i\n",
5883 				    arvif->vdev_id, ret);
5884 	}
5885 
5886 	if (changed & BSS_CHANGED_SSID &&
5887 	    vif->type == NL80211_IFTYPE_AP) {
5888 		arvif->u.ap.ssid_len = info->ssid_len;
5889 		if (info->ssid_len)
5890 			memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
5891 		arvif->u.ap.hidden_ssid = info->hidden_ssid;
5892 	}
5893 
5894 	if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
5895 		ether_addr_copy(arvif->bssid, info->bssid);
5896 
5897 	if (changed & BSS_CHANGED_FTM_RESPONDER &&
5898 	    arvif->ftm_responder != info->ftm_responder &&
5899 	    test_bit(WMI_SERVICE_RTT_RESPONDER_ROLE, ar->wmi.svc_map)) {
5900 		arvif->ftm_responder = info->ftm_responder;
5901 
5902 		vdev_param = ar->wmi.vdev_param->rtt_responder_role;
5903 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5904 						arvif->ftm_responder);
5905 
5906 		ath10k_dbg(ar, ATH10K_DBG_MAC,
5907 			   "mac vdev %d ftm_responder %d:ret %d\n",
5908 			   arvif->vdev_id, arvif->ftm_responder, ret);
5909 	}
5910 
5911 	if (changed & BSS_CHANGED_BEACON_ENABLED)
5912 		ath10k_control_beaconing(arvif, info);
5913 
5914 	if (changed & BSS_CHANGED_ERP_CTS_PROT) {
5915 		arvif->use_cts_prot = info->use_cts_prot;
5916 
5917 		ret = ath10k_recalc_rtscts_prot(arvif);
5918 		if (ret)
5919 			ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
5920 				    arvif->vdev_id, ret);
5921 
5922 		if (ath10k_mac_can_set_cts_prot(arvif)) {
5923 			ret = ath10k_mac_set_cts_prot(arvif);
5924 			if (ret)
5925 				ath10k_warn(ar, "failed to set cts protection for vdev %d: %d\n",
5926 					    arvif->vdev_id, ret);
5927 		}
5928 	}
5929 
5930 	if (changed & BSS_CHANGED_ERP_SLOT) {
5931 		if (info->use_short_slot)
5932 			slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
5933 
5934 		else
5935 			slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
5936 
5937 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
5938 			   arvif->vdev_id, slottime);
5939 
5940 		vdev_param = ar->wmi.vdev_param->slot_time;
5941 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5942 						slottime);
5943 		if (ret)
5944 			ath10k_warn(ar, "failed to set erp slot for vdev %d: %i\n",
5945 				    arvif->vdev_id, ret);
5946 	}
5947 
5948 	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
5949 		if (info->use_short_preamble)
5950 			preamble = WMI_VDEV_PREAMBLE_SHORT;
5951 		else
5952 			preamble = WMI_VDEV_PREAMBLE_LONG;
5953 
5954 		ath10k_dbg(ar, ATH10K_DBG_MAC,
5955 			   "mac vdev %d preamble %dn",
5956 			   arvif->vdev_id, preamble);
5957 
5958 		vdev_param = ar->wmi.vdev_param->preamble;
5959 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5960 						preamble);
5961 		if (ret)
5962 			ath10k_warn(ar, "failed to set preamble for vdev %d: %i\n",
5963 				    arvif->vdev_id, ret);
5964 	}
5965 
5966 	if (changed & BSS_CHANGED_ASSOC) {
5967 		if (info->assoc) {
5968 			/* Workaround: Make sure monitor vdev is not running
5969 			 * when associating to prevent some firmware revisions
5970 			 * (e.g. 10.1 and 10.2) from crashing.
5971 			 */
5972 			if (ar->monitor_started)
5973 				ath10k_monitor_stop(ar);
5974 			ath10k_bss_assoc(hw, vif, info);
5975 			ath10k_monitor_recalc(ar);
5976 		} else {
5977 			ath10k_bss_disassoc(hw, vif);
5978 		}
5979 	}
5980 
5981 	if (changed & BSS_CHANGED_TXPOWER) {
5982 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev_id %i txpower %d\n",
5983 			   arvif->vdev_id, info->txpower);
5984 
5985 		arvif->txpower = info->txpower;
5986 		ret = ath10k_mac_txpower_recalc(ar);
5987 		if (ret)
5988 			ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
5989 	}
5990 
5991 	if (changed & BSS_CHANGED_PS) {
5992 		arvif->ps = vif->bss_conf.ps;
5993 
5994 		ret = ath10k_config_ps(ar);
5995 		if (ret)
5996 			ath10k_warn(ar, "failed to setup ps on vdev %i: %d\n",
5997 				    arvif->vdev_id, ret);
5998 	}
5999 
6000 	if (changed & BSS_CHANGED_MCAST_RATE &&
6001 	    !ath10k_mac_vif_chan(arvif->vif, &def)) {
6002 		band = def.chan->band;
6003 		mcast_rate = vif->bss_conf.mcast_rate[band];
6004 		if (mcast_rate > 0)
6005 			rateidx = mcast_rate - 1;
6006 		else
6007 			rateidx = ffs(vif->bss_conf.basic_rates) - 1;
6008 
6009 		if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY)
6010 			rateidx += ATH10K_MAC_FIRST_OFDM_RATE_IDX;
6011 
6012 		bitrate = ath10k_wmi_legacy_rates[rateidx].bitrate;
6013 		hw_value = ath10k_wmi_legacy_rates[rateidx].hw_value;
6014 		if (ath10k_mac_bitrate_is_cck(bitrate))
6015 			preamble = WMI_RATE_PREAMBLE_CCK;
6016 		else
6017 			preamble = WMI_RATE_PREAMBLE_OFDM;
6018 
6019 		rate = ATH10K_HW_RATECODE(hw_value, 0, preamble);
6020 
6021 		ath10k_dbg(ar, ATH10K_DBG_MAC,
6022 			   "mac vdev %d mcast_rate %x\n",
6023 			   arvif->vdev_id, rate);
6024 
6025 		vdev_param = ar->wmi.vdev_param->mcast_data_rate;
6026 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
6027 						vdev_param, rate);
6028 		if (ret)
6029 			ath10k_warn(ar,
6030 				    "failed to set mcast rate on vdev %i: %d\n",
6031 				    arvif->vdev_id,  ret);
6032 
6033 		vdev_param = ar->wmi.vdev_param->bcast_data_rate;
6034 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
6035 						vdev_param, rate);
6036 		if (ret)
6037 			ath10k_warn(ar,
6038 				    "failed to set bcast rate on vdev %i: %d\n",
6039 				    arvif->vdev_id,  ret);
6040 	}
6041 
6042 	if (changed & BSS_CHANGED_BASIC_RATES &&
6043 	    !ath10k_mac_vif_chan(arvif->vif, &def))
6044 		ath10k_recalculate_mgmt_rate(ar, vif, &def);
6045 
6046 	mutex_unlock(&ar->conf_mutex);
6047 }
6048 
6049 static void ath10k_mac_op_set_coverage_class(struct ieee80211_hw *hw, s16 value)
6050 {
6051 	struct ath10k *ar = hw->priv;
6052 
6053 	/* This function should never be called if setting the coverage class
6054 	 * is not supported on this hardware.
6055 	 */
6056 	if (!ar->hw_params.hw_ops->set_coverage_class) {
6057 		WARN_ON_ONCE(1);
6058 		return;
6059 	}
6060 	ar->hw_params.hw_ops->set_coverage_class(ar, value);
6061 }
6062 
6063 struct ath10k_mac_tdls_iter_data {
6064 	u32 num_tdls_stations;
6065 	struct ieee80211_vif *curr_vif;
6066 };
6067 
6068 static void ath10k_mac_tdls_vif_stations_count_iter(void *data,
6069 						    struct ieee80211_sta *sta)
6070 {
6071 	struct ath10k_mac_tdls_iter_data *iter_data = data;
6072 	struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
6073 	struct ieee80211_vif *sta_vif = arsta->arvif->vif;
6074 
6075 	if (sta->tdls && sta_vif == iter_data->curr_vif)
6076 		iter_data->num_tdls_stations++;
6077 }
6078 
6079 static int ath10k_mac_tdls_vif_stations_count(struct ieee80211_hw *hw,
6080 					      struct ieee80211_vif *vif)
6081 {
6082 	struct ath10k_mac_tdls_iter_data data = {};
6083 
6084 	data.curr_vif = vif;
6085 
6086 	ieee80211_iterate_stations_atomic(hw,
6087 					  ath10k_mac_tdls_vif_stations_count_iter,
6088 					  &data);
6089 	return data.num_tdls_stations;
6090 }
6091 
6092 static int ath10k_hw_scan(struct ieee80211_hw *hw,
6093 			  struct ieee80211_vif *vif,
6094 			  struct ieee80211_scan_request *hw_req)
6095 {
6096 	struct ath10k *ar = hw->priv;
6097 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
6098 	struct cfg80211_scan_request *req = &hw_req->req;
6099 	struct wmi_start_scan_arg arg;
6100 	int ret = 0;
6101 	int i;
6102 	u32 scan_timeout;
6103 
6104 	mutex_lock(&ar->conf_mutex);
6105 
6106 	if (ath10k_mac_tdls_vif_stations_count(hw, vif) > 0) {
6107 		ret = -EBUSY;
6108 		goto exit;
6109 	}
6110 
6111 	spin_lock_bh(&ar->data_lock);
6112 	switch (ar->scan.state) {
6113 	case ATH10K_SCAN_IDLE:
6114 		reinit_completion(&ar->scan.started);
6115 		reinit_completion(&ar->scan.completed);
6116 		ar->scan.state = ATH10K_SCAN_STARTING;
6117 		ar->scan.is_roc = false;
6118 		ar->scan.vdev_id = arvif->vdev_id;
6119 		ret = 0;
6120 		break;
6121 	case ATH10K_SCAN_STARTING:
6122 	case ATH10K_SCAN_RUNNING:
6123 	case ATH10K_SCAN_ABORTING:
6124 		ret = -EBUSY;
6125 		break;
6126 	}
6127 	spin_unlock_bh(&ar->data_lock);
6128 
6129 	if (ret)
6130 		goto exit;
6131 
6132 	memset(&arg, 0, sizeof(arg));
6133 	ath10k_wmi_start_scan_init(ar, &arg);
6134 	arg.vdev_id = arvif->vdev_id;
6135 	arg.scan_id = ATH10K_SCAN_ID;
6136 
6137 	if (req->ie_len) {
6138 		arg.ie_len = req->ie_len;
6139 		memcpy(arg.ie, req->ie, arg.ie_len);
6140 	}
6141 
6142 	if (req->n_ssids) {
6143 		arg.n_ssids = req->n_ssids;
6144 		for (i = 0; i < arg.n_ssids; i++) {
6145 			arg.ssids[i].len  = req->ssids[i].ssid_len;
6146 			arg.ssids[i].ssid = req->ssids[i].ssid;
6147 		}
6148 	} else {
6149 		arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
6150 	}
6151 
6152 	if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
6153 		arg.scan_ctrl_flags |=  WMI_SCAN_ADD_SPOOFED_MAC_IN_PROBE_REQ;
6154 		ether_addr_copy(arg.mac_addr.addr, req->mac_addr);
6155 		ether_addr_copy(arg.mac_mask.addr, req->mac_addr_mask);
6156 	}
6157 
6158 	if (req->n_channels) {
6159 		arg.n_channels = req->n_channels;
6160 		for (i = 0; i < arg.n_channels; i++)
6161 			arg.channels[i] = req->channels[i]->center_freq;
6162 	}
6163 
6164 	/* if duration is set, default dwell times will be overwritten */
6165 	if (req->duration) {
6166 		arg.dwell_time_active = req->duration;
6167 		arg.dwell_time_passive = req->duration;
6168 		arg.burst_duration_ms = req->duration;
6169 
6170 		scan_timeout = min_t(u32, arg.max_rest_time *
6171 				(arg.n_channels - 1) + (req->duration +
6172 				ATH10K_SCAN_CHANNEL_SWITCH_WMI_EVT_OVERHEAD) *
6173 				arg.n_channels, arg.max_scan_time + 200);
6174 
6175 	} else {
6176 		/* Add a 200ms margin to account for event/command processing */
6177 		scan_timeout = arg.max_scan_time + 200;
6178 	}
6179 
6180 	ret = ath10k_start_scan(ar, &arg);
6181 	if (ret) {
6182 		ath10k_warn(ar, "failed to start hw scan: %d\n", ret);
6183 		spin_lock_bh(&ar->data_lock);
6184 		ar->scan.state = ATH10K_SCAN_IDLE;
6185 		spin_unlock_bh(&ar->data_lock);
6186 	}
6187 
6188 	ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
6189 				     msecs_to_jiffies(scan_timeout));
6190 
6191 exit:
6192 	mutex_unlock(&ar->conf_mutex);
6193 	return ret;
6194 }
6195 
6196 static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
6197 				  struct ieee80211_vif *vif)
6198 {
6199 	struct ath10k *ar = hw->priv;
6200 
6201 	mutex_lock(&ar->conf_mutex);
6202 	ath10k_scan_abort(ar);
6203 	mutex_unlock(&ar->conf_mutex);
6204 
6205 	cancel_delayed_work_sync(&ar->scan.timeout);
6206 }
6207 
6208 static void ath10k_set_key_h_def_keyidx(struct ath10k *ar,
6209 					struct ath10k_vif *arvif,
6210 					enum set_key_cmd cmd,
6211 					struct ieee80211_key_conf *key)
6212 {
6213 	u32 vdev_param = arvif->ar->wmi.vdev_param->def_keyid;
6214 	int ret;
6215 
6216 	/* 10.1 firmware branch requires default key index to be set to group
6217 	 * key index after installing it. Otherwise FW/HW Txes corrupted
6218 	 * frames with multi-vif APs. This is not required for main firmware
6219 	 * branch (e.g. 636).
6220 	 *
6221 	 * This is also needed for 636 fw for IBSS-RSN to work more reliably.
6222 	 *
6223 	 * FIXME: It remains unknown if this is required for multi-vif STA
6224 	 * interfaces on 10.1.
6225 	 */
6226 
6227 	if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
6228 	    arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
6229 		return;
6230 
6231 	if (key->cipher == WLAN_CIPHER_SUITE_WEP40)
6232 		return;
6233 
6234 	if (key->cipher == WLAN_CIPHER_SUITE_WEP104)
6235 		return;
6236 
6237 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
6238 		return;
6239 
6240 	if (cmd != SET_KEY)
6241 		return;
6242 
6243 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
6244 					key->keyidx);
6245 	if (ret)
6246 		ath10k_warn(ar, "failed to set vdev %i group key as default key: %d\n",
6247 			    arvif->vdev_id, ret);
6248 }
6249 
6250 static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
6251 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
6252 			  struct ieee80211_key_conf *key)
6253 {
6254 	struct ath10k *ar = hw->priv;
6255 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
6256 	struct ath10k_sta *arsta;
6257 	struct ath10k_peer *peer;
6258 	const u8 *peer_addr;
6259 	bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
6260 		      key->cipher == WLAN_CIPHER_SUITE_WEP104;
6261 	int ret = 0;
6262 	int ret2;
6263 	u32 flags = 0;
6264 	u32 flags2;
6265 
6266 	/* this one needs to be done in software */
6267 	if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
6268 	    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
6269 	    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256 ||
6270 	    key->cipher == WLAN_CIPHER_SUITE_BIP_CMAC_256)
6271 		return 1;
6272 
6273 	if (arvif->nohwcrypt)
6274 		return 1;
6275 
6276 	if (key->keyidx > WMI_MAX_KEY_INDEX)
6277 		return -ENOSPC;
6278 
6279 	mutex_lock(&ar->conf_mutex);
6280 
6281 	if (sta) {
6282 		arsta = (struct ath10k_sta *)sta->drv_priv;
6283 		peer_addr = sta->addr;
6284 		spin_lock_bh(&ar->data_lock);
6285 		arsta->ucast_cipher = key->cipher;
6286 		spin_unlock_bh(&ar->data_lock);
6287 	} else if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
6288 		peer_addr = vif->bss_conf.bssid;
6289 	} else {
6290 		peer_addr = vif->addr;
6291 	}
6292 
6293 	key->hw_key_idx = key->keyidx;
6294 
6295 	if (is_wep) {
6296 		if (cmd == SET_KEY)
6297 			arvif->wep_keys[key->keyidx] = key;
6298 		else
6299 			arvif->wep_keys[key->keyidx] = NULL;
6300 	}
6301 
6302 	/* the peer should not disappear in mid-way (unless FW goes awry) since
6303 	 * we already hold conf_mutex. we just make sure its there now.
6304 	 */
6305 	spin_lock_bh(&ar->data_lock);
6306 	peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
6307 	spin_unlock_bh(&ar->data_lock);
6308 
6309 	if (!peer) {
6310 		if (cmd == SET_KEY) {
6311 			ath10k_warn(ar, "failed to install key for non-existent peer %pM\n",
6312 				    peer_addr);
6313 			ret = -EOPNOTSUPP;
6314 			goto exit;
6315 		} else {
6316 			/* if the peer doesn't exist there is no key to disable anymore */
6317 			goto exit;
6318 		}
6319 	}
6320 
6321 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
6322 		flags |= WMI_KEY_PAIRWISE;
6323 	else
6324 		flags |= WMI_KEY_GROUP;
6325 
6326 	if (is_wep) {
6327 		if (cmd == DISABLE_KEY)
6328 			ath10k_clear_vdev_key(arvif, key);
6329 
6330 		/* When WEP keys are uploaded it's possible that there are
6331 		 * stations associated already (e.g. when merging) without any
6332 		 * keys. Static WEP needs an explicit per-peer key upload.
6333 		 */
6334 		if (vif->type == NL80211_IFTYPE_ADHOC &&
6335 		    cmd == SET_KEY)
6336 			ath10k_mac_vif_update_wep_key(arvif, key);
6337 
6338 		/* 802.1x never sets the def_wep_key_idx so each set_key()
6339 		 * call changes default tx key.
6340 		 *
6341 		 * Static WEP sets def_wep_key_idx via .set_default_unicast_key
6342 		 * after first set_key().
6343 		 */
6344 		if (cmd == SET_KEY && arvif->def_wep_key_idx == -1)
6345 			flags |= WMI_KEY_TX_USAGE;
6346 	}
6347 
6348 	ret = ath10k_install_key(arvif, key, cmd, peer_addr, flags);
6349 	if (ret) {
6350 		WARN_ON(ret > 0);
6351 		ath10k_warn(ar, "failed to install key for vdev %i peer %pM: %d\n",
6352 			    arvif->vdev_id, peer_addr, ret);
6353 		goto exit;
6354 	}
6355 
6356 	/* mac80211 sets static WEP keys as groupwise while firmware requires
6357 	 * them to be installed twice as both pairwise and groupwise.
6358 	 */
6359 	if (is_wep && !sta && vif->type == NL80211_IFTYPE_STATION) {
6360 		flags2 = flags;
6361 		flags2 &= ~WMI_KEY_GROUP;
6362 		flags2 |= WMI_KEY_PAIRWISE;
6363 
6364 		ret = ath10k_install_key(arvif, key, cmd, peer_addr, flags2);
6365 		if (ret) {
6366 			WARN_ON(ret > 0);
6367 			ath10k_warn(ar, "failed to install (ucast) key for vdev %i peer %pM: %d\n",
6368 				    arvif->vdev_id, peer_addr, ret);
6369 			ret2 = ath10k_install_key(arvif, key, DISABLE_KEY,
6370 						  peer_addr, flags);
6371 			if (ret2) {
6372 				WARN_ON(ret2 > 0);
6373 				ath10k_warn(ar, "failed to disable (mcast) key for vdev %i peer %pM: %d\n",
6374 					    arvif->vdev_id, peer_addr, ret2);
6375 			}
6376 			goto exit;
6377 		}
6378 	}
6379 
6380 	ath10k_set_key_h_def_keyidx(ar, arvif, cmd, key);
6381 
6382 	spin_lock_bh(&ar->data_lock);
6383 	peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
6384 	if (peer && cmd == SET_KEY)
6385 		peer->keys[key->keyidx] = key;
6386 	else if (peer && cmd == DISABLE_KEY)
6387 		peer->keys[key->keyidx] = NULL;
6388 	else if (peer == NULL)
6389 		/* impossible unless FW goes crazy */
6390 		ath10k_warn(ar, "Peer %pM disappeared!\n", peer_addr);
6391 	spin_unlock_bh(&ar->data_lock);
6392 
6393 	if (sta && sta->tdls)
6394 		ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
6395 					  ar->wmi.peer_param->authorize, 1);
6396 	else if (sta && cmd == SET_KEY && (key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
6397 		ath10k_wmi_peer_set_param(ar, arvif->vdev_id, peer_addr,
6398 					  ar->wmi.peer_param->authorize, 1);
6399 
6400 exit:
6401 	mutex_unlock(&ar->conf_mutex);
6402 	return ret;
6403 }
6404 
6405 static void ath10k_set_default_unicast_key(struct ieee80211_hw *hw,
6406 					   struct ieee80211_vif *vif,
6407 					   int keyidx)
6408 {
6409 	struct ath10k *ar = hw->priv;
6410 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
6411 	int ret;
6412 
6413 	mutex_lock(&arvif->ar->conf_mutex);
6414 
6415 	if (arvif->ar->state != ATH10K_STATE_ON)
6416 		goto unlock;
6417 
6418 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
6419 		   arvif->vdev_id, keyidx);
6420 
6421 	ret = ath10k_wmi_vdev_set_param(arvif->ar,
6422 					arvif->vdev_id,
6423 					arvif->ar->wmi.vdev_param->def_keyid,
6424 					keyidx);
6425 
6426 	if (ret) {
6427 		ath10k_warn(ar, "failed to update wep key index for vdev %d: %d\n",
6428 			    arvif->vdev_id,
6429 			    ret);
6430 		goto unlock;
6431 	}
6432 
6433 	arvif->def_wep_key_idx = keyidx;
6434 
6435 unlock:
6436 	mutex_unlock(&arvif->ar->conf_mutex);
6437 }
6438 
6439 static void ath10k_sta_rc_update_wk(struct work_struct *wk)
6440 {
6441 	struct ath10k *ar;
6442 	struct ath10k_vif *arvif;
6443 	struct ath10k_sta *arsta;
6444 	struct ieee80211_sta *sta;
6445 	struct cfg80211_chan_def def;
6446 	enum nl80211_band band;
6447 	const u8 *ht_mcs_mask;
6448 	const u16 *vht_mcs_mask;
6449 	u32 changed, bw, nss, smps;
6450 	int err;
6451 
6452 	arsta = container_of(wk, struct ath10k_sta, update_wk);
6453 	sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
6454 	arvif = arsta->arvif;
6455 	ar = arvif->ar;
6456 
6457 	if (WARN_ON(ath10k_mac_vif_chan(arvif->vif, &def)))
6458 		return;
6459 
6460 	band = def.chan->band;
6461 	ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
6462 	vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
6463 
6464 	spin_lock_bh(&ar->data_lock);
6465 
6466 	changed = arsta->changed;
6467 	arsta->changed = 0;
6468 
6469 	bw = arsta->bw;
6470 	nss = arsta->nss;
6471 	smps = arsta->smps;
6472 
6473 	spin_unlock_bh(&ar->data_lock);
6474 
6475 	mutex_lock(&ar->conf_mutex);
6476 
6477 	nss = max_t(u32, 1, nss);
6478 	nss = min(nss, max(ath10k_mac_max_ht_nss(ht_mcs_mask),
6479 			   ath10k_mac_max_vht_nss(vht_mcs_mask)));
6480 
6481 	if (changed & IEEE80211_RC_BW_CHANGED) {
6482 		enum wmi_phy_mode mode;
6483 
6484 		mode = chan_to_phymode(&def);
6485 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM peer bw %d phymode %d\n",
6486 			   sta->addr, bw, mode);
6487 
6488 		err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
6489 						ar->wmi.peer_param->phymode, mode);
6490 		if (err) {
6491 			ath10k_warn(ar, "failed to update STA %pM peer phymode %d: %d\n",
6492 				    sta->addr, mode, err);
6493 			goto exit;
6494 		}
6495 
6496 		err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
6497 						ar->wmi.peer_param->chan_width, bw);
6498 		if (err)
6499 			ath10k_warn(ar, "failed to update STA %pM peer bw %d: %d\n",
6500 				    sta->addr, bw, err);
6501 	}
6502 
6503 	if (changed & IEEE80211_RC_NSS_CHANGED) {
6504 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM nss %d\n",
6505 			   sta->addr, nss);
6506 
6507 		err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
6508 						ar->wmi.peer_param->nss, nss);
6509 		if (err)
6510 			ath10k_warn(ar, "failed to update STA %pM nss %d: %d\n",
6511 				    sta->addr, nss, err);
6512 	}
6513 
6514 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
6515 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM smps %d\n",
6516 			   sta->addr, smps);
6517 
6518 		err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
6519 						ar->wmi.peer_param->smps_state, smps);
6520 		if (err)
6521 			ath10k_warn(ar, "failed to update STA %pM smps %d: %d\n",
6522 				    sta->addr, smps, err);
6523 	}
6524 
6525 	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
6526 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM supp rates\n",
6527 			   sta->addr);
6528 
6529 		err = ath10k_station_assoc(ar, arvif->vif, sta, true);
6530 		if (err)
6531 			ath10k_warn(ar, "failed to reassociate station: %pM\n",
6532 				    sta->addr);
6533 	}
6534 
6535 exit:
6536 	mutex_unlock(&ar->conf_mutex);
6537 }
6538 
6539 static int ath10k_mac_inc_num_stations(struct ath10k_vif *arvif,
6540 				       struct ieee80211_sta *sta)
6541 {
6542 	struct ath10k *ar = arvif->ar;
6543 
6544 	lockdep_assert_held(&ar->conf_mutex);
6545 
6546 	if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6547 		return 0;
6548 
6549 	if (ar->num_stations >= ar->max_num_stations)
6550 		return -ENOBUFS;
6551 
6552 	ar->num_stations++;
6553 
6554 	return 0;
6555 }
6556 
6557 static void ath10k_mac_dec_num_stations(struct ath10k_vif *arvif,
6558 					struct ieee80211_sta *sta)
6559 {
6560 	struct ath10k *ar = arvif->ar;
6561 
6562 	lockdep_assert_held(&ar->conf_mutex);
6563 
6564 	if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6565 		return;
6566 
6567 	ar->num_stations--;
6568 }
6569 
6570 static int ath10k_sta_set_txpwr(struct ieee80211_hw *hw,
6571 				struct ieee80211_vif *vif,
6572 				struct ieee80211_sta *sta)
6573 {
6574 	struct ath10k *ar = hw->priv;
6575 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
6576 	int ret = 0;
6577 	s16 txpwr;
6578 
6579 	if (sta->txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
6580 		txpwr = 0;
6581 	} else {
6582 		txpwr = sta->txpwr.power;
6583 		if (!txpwr)
6584 			return -EINVAL;
6585 	}
6586 
6587 	if (txpwr > ATH10K_TX_POWER_MAX_VAL || txpwr < ATH10K_TX_POWER_MIN_VAL)
6588 		return -EINVAL;
6589 
6590 	mutex_lock(&ar->conf_mutex);
6591 
6592 	ret = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
6593 					ar->wmi.peer_param->use_fixed_power, txpwr);
6594 	if (ret) {
6595 		ath10k_warn(ar, "failed to set tx power for station ret: %d\n",
6596 			    ret);
6597 		goto out;
6598 	}
6599 
6600 out:
6601 	mutex_unlock(&ar->conf_mutex);
6602 	return ret;
6603 }
6604 
6605 static int ath10k_sta_state(struct ieee80211_hw *hw,
6606 			    struct ieee80211_vif *vif,
6607 			    struct ieee80211_sta *sta,
6608 			    enum ieee80211_sta_state old_state,
6609 			    enum ieee80211_sta_state new_state)
6610 {
6611 	struct ath10k *ar = hw->priv;
6612 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
6613 	struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
6614 	struct ath10k_peer *peer;
6615 	int ret = 0;
6616 	int i;
6617 
6618 	if (old_state == IEEE80211_STA_NOTEXIST &&
6619 	    new_state == IEEE80211_STA_NONE) {
6620 		memset(arsta, 0, sizeof(*arsta));
6621 		arsta->arvif = arvif;
6622 		arsta->peer_ps_state = WMI_PEER_PS_STATE_DISABLED;
6623 		INIT_WORK(&arsta->update_wk, ath10k_sta_rc_update_wk);
6624 
6625 		for (i = 0; i < ARRAY_SIZE(sta->txq); i++)
6626 			ath10k_mac_txq_init(sta->txq[i]);
6627 	}
6628 
6629 	/* cancel must be done outside the mutex to avoid deadlock */
6630 	if ((old_state == IEEE80211_STA_NONE &&
6631 	     new_state == IEEE80211_STA_NOTEXIST))
6632 		cancel_work_sync(&arsta->update_wk);
6633 
6634 	mutex_lock(&ar->conf_mutex);
6635 
6636 	if (old_state == IEEE80211_STA_NOTEXIST &&
6637 	    new_state == IEEE80211_STA_NONE) {
6638 		/*
6639 		 * New station addition.
6640 		 */
6641 		enum wmi_peer_type peer_type = WMI_PEER_TYPE_DEFAULT;
6642 		u32 num_tdls_stations;
6643 
6644 		ath10k_dbg(ar, ATH10K_DBG_MAC,
6645 			   "mac vdev %d peer create %pM (new sta) sta %d / %d peer %d / %d\n",
6646 			   arvif->vdev_id, sta->addr,
6647 			   ar->num_stations + 1, ar->max_num_stations,
6648 			   ar->num_peers + 1, ar->max_num_peers);
6649 
6650 		num_tdls_stations = ath10k_mac_tdls_vif_stations_count(hw, vif);
6651 
6652 		if (sta->tdls) {
6653 			if (num_tdls_stations >= ar->max_num_tdls_vdevs) {
6654 				ath10k_warn(ar, "vdev %i exceeded maximum number of tdls vdevs %i\n",
6655 					    arvif->vdev_id,
6656 					    ar->max_num_tdls_vdevs);
6657 				ret = -ELNRNG;
6658 				goto exit;
6659 			}
6660 			peer_type = WMI_PEER_TYPE_TDLS;
6661 		}
6662 
6663 		ret = ath10k_mac_inc_num_stations(arvif, sta);
6664 		if (ret) {
6665 			ath10k_warn(ar, "refusing to associate station: too many connected already (%d)\n",
6666 				    ar->max_num_stations);
6667 			goto exit;
6668 		}
6669 
6670 		if (ath10k_debug_is_extd_tx_stats_enabled(ar)) {
6671 			arsta->tx_stats = kzalloc(sizeof(*arsta->tx_stats),
6672 						  GFP_KERNEL);
6673 			if (!arsta->tx_stats) {
6674 				ath10k_mac_dec_num_stations(arvif, sta);
6675 				ret = -ENOMEM;
6676 				goto exit;
6677 			}
6678 		}
6679 
6680 		ret = ath10k_peer_create(ar, vif, sta, arvif->vdev_id,
6681 					 sta->addr, peer_type);
6682 		if (ret) {
6683 			ath10k_warn(ar, "failed to add peer %pM for vdev %d when adding a new sta: %i\n",
6684 				    sta->addr, arvif->vdev_id, ret);
6685 			ath10k_mac_dec_num_stations(arvif, sta);
6686 			kfree(arsta->tx_stats);
6687 			goto exit;
6688 		}
6689 
6690 		spin_lock_bh(&ar->data_lock);
6691 
6692 		peer = ath10k_peer_find(ar, arvif->vdev_id, sta->addr);
6693 		if (!peer) {
6694 			ath10k_warn(ar, "failed to lookup peer %pM on vdev %i\n",
6695 				    vif->addr, arvif->vdev_id);
6696 			spin_unlock_bh(&ar->data_lock);
6697 			ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
6698 			ath10k_mac_dec_num_stations(arvif, sta);
6699 			kfree(arsta->tx_stats);
6700 			ret = -ENOENT;
6701 			goto exit;
6702 		}
6703 
6704 		arsta->peer_id = find_first_bit(peer->peer_ids,
6705 						ATH10K_MAX_NUM_PEER_IDS);
6706 
6707 		spin_unlock_bh(&ar->data_lock);
6708 
6709 		if (!sta->tdls)
6710 			goto exit;
6711 
6712 		ret = ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
6713 						      WMI_TDLS_ENABLE_ACTIVE);
6714 		if (ret) {
6715 			ath10k_warn(ar, "failed to update fw tdls state on vdev %i: %i\n",
6716 				    arvif->vdev_id, ret);
6717 			ath10k_peer_delete(ar, arvif->vdev_id,
6718 					   sta->addr);
6719 			ath10k_mac_dec_num_stations(arvif, sta);
6720 			kfree(arsta->tx_stats);
6721 			goto exit;
6722 		}
6723 
6724 		ret = ath10k_mac_tdls_peer_update(ar, arvif->vdev_id, sta,
6725 						  WMI_TDLS_PEER_STATE_PEERING);
6726 		if (ret) {
6727 			ath10k_warn(ar,
6728 				    "failed to update tdls peer %pM for vdev %d when adding a new sta: %i\n",
6729 				    sta->addr, arvif->vdev_id, ret);
6730 			ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
6731 			ath10k_mac_dec_num_stations(arvif, sta);
6732 			kfree(arsta->tx_stats);
6733 
6734 			if (num_tdls_stations != 0)
6735 				goto exit;
6736 			ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
6737 							WMI_TDLS_DISABLE);
6738 		}
6739 	} else if ((old_state == IEEE80211_STA_NONE &&
6740 		    new_state == IEEE80211_STA_NOTEXIST)) {
6741 		/*
6742 		 * Existing station deletion.
6743 		 */
6744 		ath10k_dbg(ar, ATH10K_DBG_MAC,
6745 			   "mac vdev %d peer delete %pM sta %pK (sta gone)\n",
6746 			   arvif->vdev_id, sta->addr, sta);
6747 
6748 		if (sta->tdls) {
6749 			ret = ath10k_mac_tdls_peer_update(ar, arvif->vdev_id,
6750 							  sta,
6751 							  WMI_TDLS_PEER_STATE_TEARDOWN);
6752 			if (ret)
6753 				ath10k_warn(ar, "failed to update tdls peer state for %pM state %d: %i\n",
6754 					    sta->addr,
6755 					    WMI_TDLS_PEER_STATE_TEARDOWN, ret);
6756 		}
6757 
6758 		ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
6759 		if (ret)
6760 			ath10k_warn(ar, "failed to delete peer %pM for vdev %d: %i\n",
6761 				    sta->addr, arvif->vdev_id, ret);
6762 
6763 		ath10k_mac_dec_num_stations(arvif, sta);
6764 
6765 		spin_lock_bh(&ar->data_lock);
6766 		for (i = 0; i < ARRAY_SIZE(ar->peer_map); i++) {
6767 			peer = ar->peer_map[i];
6768 			if (!peer)
6769 				continue;
6770 
6771 			if (peer->sta == sta) {
6772 				ath10k_warn(ar, "found sta peer %pM (ptr %pK id %d) entry on vdev %i after it was supposedly removed\n",
6773 					    sta->addr, peer, i, arvif->vdev_id);
6774 				peer->sta = NULL;
6775 
6776 				/* Clean up the peer object as well since we
6777 				 * must have failed to do this above.
6778 				 */
6779 				list_del(&peer->list);
6780 				ar->peer_map[i] = NULL;
6781 				kfree(peer);
6782 				ar->num_peers--;
6783 			}
6784 		}
6785 		spin_unlock_bh(&ar->data_lock);
6786 
6787 		if (ath10k_debug_is_extd_tx_stats_enabled(ar)) {
6788 			kfree(arsta->tx_stats);
6789 			arsta->tx_stats = NULL;
6790 		}
6791 
6792 		for (i = 0; i < ARRAY_SIZE(sta->txq); i++)
6793 			ath10k_mac_txq_unref(ar, sta->txq[i]);
6794 
6795 		if (!sta->tdls)
6796 			goto exit;
6797 
6798 		if (ath10k_mac_tdls_vif_stations_count(hw, vif))
6799 			goto exit;
6800 
6801 		/* This was the last tdls peer in current vif */
6802 		ret = ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
6803 						      WMI_TDLS_DISABLE);
6804 		if (ret) {
6805 			ath10k_warn(ar, "failed to update fw tdls state on vdev %i: %i\n",
6806 				    arvif->vdev_id, ret);
6807 		}
6808 	} else if (old_state == IEEE80211_STA_AUTH &&
6809 		   new_state == IEEE80211_STA_ASSOC &&
6810 		   (vif->type == NL80211_IFTYPE_AP ||
6811 		    vif->type == NL80211_IFTYPE_MESH_POINT ||
6812 		    vif->type == NL80211_IFTYPE_ADHOC)) {
6813 		/*
6814 		 * New association.
6815 		 */
6816 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM associated\n",
6817 			   sta->addr);
6818 
6819 		ret = ath10k_station_assoc(ar, vif, sta, false);
6820 		if (ret)
6821 			ath10k_warn(ar, "failed to associate station %pM for vdev %i: %i\n",
6822 				    sta->addr, arvif->vdev_id, ret);
6823 	} else if (old_state == IEEE80211_STA_ASSOC &&
6824 		   new_state == IEEE80211_STA_AUTHORIZED &&
6825 		   sta->tdls) {
6826 		/*
6827 		 * Tdls station authorized.
6828 		 */
6829 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac tdls sta %pM authorized\n",
6830 			   sta->addr);
6831 
6832 		ret = ath10k_station_assoc(ar, vif, sta, false);
6833 		if (ret) {
6834 			ath10k_warn(ar, "failed to associate tdls station %pM for vdev %i: %i\n",
6835 				    sta->addr, arvif->vdev_id, ret);
6836 			goto exit;
6837 		}
6838 
6839 		ret = ath10k_mac_tdls_peer_update(ar, arvif->vdev_id, sta,
6840 						  WMI_TDLS_PEER_STATE_CONNECTED);
6841 		if (ret)
6842 			ath10k_warn(ar, "failed to update tdls peer %pM for vdev %i: %i\n",
6843 				    sta->addr, arvif->vdev_id, ret);
6844 	} else if (old_state == IEEE80211_STA_ASSOC &&
6845 		    new_state == IEEE80211_STA_AUTH &&
6846 		    (vif->type == NL80211_IFTYPE_AP ||
6847 		     vif->type == NL80211_IFTYPE_MESH_POINT ||
6848 		     vif->type == NL80211_IFTYPE_ADHOC)) {
6849 		/*
6850 		 * Disassociation.
6851 		 */
6852 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM disassociated\n",
6853 			   sta->addr);
6854 
6855 		ret = ath10k_station_disassoc(ar, vif, sta);
6856 		if (ret)
6857 			ath10k_warn(ar, "failed to disassociate station: %pM vdev %i: %i\n",
6858 				    sta->addr, arvif->vdev_id, ret);
6859 	}
6860 exit:
6861 	mutex_unlock(&ar->conf_mutex);
6862 	return ret;
6863 }
6864 
6865 static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
6866 				u16 ac, bool enable)
6867 {
6868 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
6869 	struct wmi_sta_uapsd_auto_trig_arg arg = {};
6870 	u32 prio = 0, acc = 0;
6871 	u32 value = 0;
6872 	int ret = 0;
6873 
6874 	lockdep_assert_held(&ar->conf_mutex);
6875 
6876 	if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
6877 		return 0;
6878 
6879 	switch (ac) {
6880 	case IEEE80211_AC_VO:
6881 		value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
6882 			WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
6883 		prio = 7;
6884 		acc = 3;
6885 		break;
6886 	case IEEE80211_AC_VI:
6887 		value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
6888 			WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
6889 		prio = 5;
6890 		acc = 2;
6891 		break;
6892 	case IEEE80211_AC_BE:
6893 		value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
6894 			WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
6895 		prio = 2;
6896 		acc = 1;
6897 		break;
6898 	case IEEE80211_AC_BK:
6899 		value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
6900 			WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
6901 		prio = 0;
6902 		acc = 0;
6903 		break;
6904 	}
6905 
6906 	if (enable)
6907 		arvif->u.sta.uapsd |= value;
6908 	else
6909 		arvif->u.sta.uapsd &= ~value;
6910 
6911 	ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
6912 					  WMI_STA_PS_PARAM_UAPSD,
6913 					  arvif->u.sta.uapsd);
6914 	if (ret) {
6915 		ath10k_warn(ar, "failed to set uapsd params: %d\n", ret);
6916 		goto exit;
6917 	}
6918 
6919 	if (arvif->u.sta.uapsd)
6920 		value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
6921 	else
6922 		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
6923 
6924 	ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
6925 					  WMI_STA_PS_PARAM_RX_WAKE_POLICY,
6926 					  value);
6927 	if (ret)
6928 		ath10k_warn(ar, "failed to set rx wake param: %d\n", ret);
6929 
6930 	ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
6931 	if (ret) {
6932 		ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
6933 			    arvif->vdev_id, ret);
6934 		return ret;
6935 	}
6936 
6937 	ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
6938 	if (ret) {
6939 		ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
6940 			    arvif->vdev_id, ret);
6941 		return ret;
6942 	}
6943 
6944 	if (test_bit(WMI_SERVICE_STA_UAPSD_BASIC_AUTO_TRIG, ar->wmi.svc_map) ||
6945 	    test_bit(WMI_SERVICE_STA_UAPSD_VAR_AUTO_TRIG, ar->wmi.svc_map)) {
6946 		/* Only userspace can make an educated decision when to send
6947 		 * trigger frame. The following effectively disables u-UAPSD
6948 		 * autotrigger in firmware (which is enabled by default
6949 		 * provided the autotrigger service is available).
6950 		 */
6951 
6952 		arg.wmm_ac = acc;
6953 		arg.user_priority = prio;
6954 		arg.service_interval = 0;
6955 		arg.suspend_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
6956 		arg.delay_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
6957 
6958 		ret = ath10k_wmi_vdev_sta_uapsd(ar, arvif->vdev_id,
6959 						arvif->bssid, &arg, 1);
6960 		if (ret) {
6961 			ath10k_warn(ar, "failed to set uapsd auto trigger %d\n",
6962 				    ret);
6963 			return ret;
6964 		}
6965 	}
6966 
6967 exit:
6968 	return ret;
6969 }
6970 
6971 static int ath10k_conf_tx(struct ieee80211_hw *hw,
6972 			  struct ieee80211_vif *vif, u16 ac,
6973 			  const struct ieee80211_tx_queue_params *params)
6974 {
6975 	struct ath10k *ar = hw->priv;
6976 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
6977 	struct wmi_wmm_params_arg *p = NULL;
6978 	int ret;
6979 
6980 	mutex_lock(&ar->conf_mutex);
6981 
6982 	switch (ac) {
6983 	case IEEE80211_AC_VO:
6984 		p = &arvif->wmm_params.ac_vo;
6985 		break;
6986 	case IEEE80211_AC_VI:
6987 		p = &arvif->wmm_params.ac_vi;
6988 		break;
6989 	case IEEE80211_AC_BE:
6990 		p = &arvif->wmm_params.ac_be;
6991 		break;
6992 	case IEEE80211_AC_BK:
6993 		p = &arvif->wmm_params.ac_bk;
6994 		break;
6995 	}
6996 
6997 	if (WARN_ON(!p)) {
6998 		ret = -EINVAL;
6999 		goto exit;
7000 	}
7001 
7002 	p->cwmin = params->cw_min;
7003 	p->cwmax = params->cw_max;
7004 	p->aifs = params->aifs;
7005 
7006 	/*
7007 	 * The channel time duration programmed in the HW is in absolute
7008 	 * microseconds, while mac80211 gives the txop in units of
7009 	 * 32 microseconds.
7010 	 */
7011 	p->txop = params->txop * 32;
7012 
7013 	if (ar->wmi.ops->gen_vdev_wmm_conf) {
7014 		ret = ath10k_wmi_vdev_wmm_conf(ar, arvif->vdev_id,
7015 					       &arvif->wmm_params);
7016 		if (ret) {
7017 			ath10k_warn(ar, "failed to set vdev wmm params on vdev %i: %d\n",
7018 				    arvif->vdev_id, ret);
7019 			goto exit;
7020 		}
7021 	} else {
7022 		/* This won't work well with multi-interface cases but it's
7023 		 * better than nothing.
7024 		 */
7025 		ret = ath10k_wmi_pdev_set_wmm_params(ar, &arvif->wmm_params);
7026 		if (ret) {
7027 			ath10k_warn(ar, "failed to set wmm params: %d\n", ret);
7028 			goto exit;
7029 		}
7030 	}
7031 
7032 	ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
7033 	if (ret)
7034 		ath10k_warn(ar, "failed to set sta uapsd: %d\n", ret);
7035 
7036 exit:
7037 	mutex_unlock(&ar->conf_mutex);
7038 	return ret;
7039 }
7040 
7041 #define ATH10K_ROC_TIMEOUT_HZ (2 * HZ)
7042 
7043 static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
7044 				    struct ieee80211_vif *vif,
7045 				    struct ieee80211_channel *chan,
7046 				    int duration,
7047 				    enum ieee80211_roc_type type)
7048 {
7049 	struct ath10k *ar = hw->priv;
7050 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
7051 	struct wmi_start_scan_arg arg;
7052 	int ret = 0;
7053 	u32 scan_time_msec;
7054 
7055 	mutex_lock(&ar->conf_mutex);
7056 
7057 	if (ath10k_mac_tdls_vif_stations_count(hw, vif) > 0) {
7058 		ret = -EBUSY;
7059 		goto exit;
7060 	}
7061 
7062 	spin_lock_bh(&ar->data_lock);
7063 	switch (ar->scan.state) {
7064 	case ATH10K_SCAN_IDLE:
7065 		reinit_completion(&ar->scan.started);
7066 		reinit_completion(&ar->scan.completed);
7067 		reinit_completion(&ar->scan.on_channel);
7068 		ar->scan.state = ATH10K_SCAN_STARTING;
7069 		ar->scan.is_roc = true;
7070 		ar->scan.vdev_id = arvif->vdev_id;
7071 		ar->scan.roc_freq = chan->center_freq;
7072 		ar->scan.roc_notify = true;
7073 		ret = 0;
7074 		break;
7075 	case ATH10K_SCAN_STARTING:
7076 	case ATH10K_SCAN_RUNNING:
7077 	case ATH10K_SCAN_ABORTING:
7078 		ret = -EBUSY;
7079 		break;
7080 	}
7081 	spin_unlock_bh(&ar->data_lock);
7082 
7083 	if (ret)
7084 		goto exit;
7085 
7086 	scan_time_msec = ar->hw->wiphy->max_remain_on_channel_duration * 2;
7087 
7088 	memset(&arg, 0, sizeof(arg));
7089 	ath10k_wmi_start_scan_init(ar, &arg);
7090 	arg.vdev_id = arvif->vdev_id;
7091 	arg.scan_id = ATH10K_SCAN_ID;
7092 	arg.n_channels = 1;
7093 	arg.channels[0] = chan->center_freq;
7094 	arg.dwell_time_active = scan_time_msec;
7095 	arg.dwell_time_passive = scan_time_msec;
7096 	arg.max_scan_time = scan_time_msec;
7097 	arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
7098 	arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
7099 	arg.burst_duration_ms = duration;
7100 
7101 	ret = ath10k_start_scan(ar, &arg);
7102 	if (ret) {
7103 		ath10k_warn(ar, "failed to start roc scan: %d\n", ret);
7104 		spin_lock_bh(&ar->data_lock);
7105 		ar->scan.state = ATH10K_SCAN_IDLE;
7106 		spin_unlock_bh(&ar->data_lock);
7107 		goto exit;
7108 	}
7109 
7110 	ret = wait_for_completion_timeout(&ar->scan.on_channel, 3 * HZ);
7111 	if (ret == 0) {
7112 		ath10k_warn(ar, "failed to switch to channel for roc scan\n");
7113 
7114 		ret = ath10k_scan_stop(ar);
7115 		if (ret)
7116 			ath10k_warn(ar, "failed to stop scan: %d\n", ret);
7117 
7118 		ret = -ETIMEDOUT;
7119 		goto exit;
7120 	}
7121 
7122 	ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
7123 				     msecs_to_jiffies(duration));
7124 
7125 	ret = 0;
7126 exit:
7127 	mutex_unlock(&ar->conf_mutex);
7128 	return ret;
7129 }
7130 
7131 static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw,
7132 					   struct ieee80211_vif *vif)
7133 {
7134 	struct ath10k *ar = hw->priv;
7135 
7136 	mutex_lock(&ar->conf_mutex);
7137 
7138 	spin_lock_bh(&ar->data_lock);
7139 	ar->scan.roc_notify = false;
7140 	spin_unlock_bh(&ar->data_lock);
7141 
7142 	ath10k_scan_abort(ar);
7143 
7144 	mutex_unlock(&ar->conf_mutex);
7145 
7146 	cancel_delayed_work_sync(&ar->scan.timeout);
7147 
7148 	return 0;
7149 }
7150 
7151 /*
7152  * Both RTS and Fragmentation threshold are interface-specific
7153  * in ath10k, but device-specific in mac80211.
7154  */
7155 
7156 static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
7157 {
7158 	struct ath10k *ar = hw->priv;
7159 	struct ath10k_vif *arvif;
7160 	int ret = 0;
7161 
7162 	mutex_lock(&ar->conf_mutex);
7163 	list_for_each_entry(arvif, &ar->arvifs, list) {
7164 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
7165 			   arvif->vdev_id, value);
7166 
7167 		ret = ath10k_mac_set_rts(arvif, value);
7168 		if (ret) {
7169 			ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
7170 				    arvif->vdev_id, ret);
7171 			break;
7172 		}
7173 	}
7174 	mutex_unlock(&ar->conf_mutex);
7175 
7176 	return ret;
7177 }
7178 
7179 static int ath10k_mac_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
7180 {
7181 	/* Even though there's a WMI enum for fragmentation threshold no known
7182 	 * firmware actually implements it. Moreover it is not possible to rely
7183 	 * frame fragmentation to mac80211 because firmware clears the "more
7184 	 * fragments" bit in frame control making it impossible for remote
7185 	 * devices to reassemble frames.
7186 	 *
7187 	 * Hence implement a dummy callback just to say fragmentation isn't
7188 	 * supported. This effectively prevents mac80211 from doing frame
7189 	 * fragmentation in software.
7190 	 */
7191 	return -EOPNOTSUPP;
7192 }
7193 
7194 void ath10k_mac_wait_tx_complete(struct ath10k *ar)
7195 {
7196 	bool skip;
7197 	long time_left;
7198 
7199 	/* mac80211 doesn't care if we really xmit queued frames or not
7200 	 * we'll collect those frames either way if we stop/delete vdevs
7201 	 */
7202 
7203 	if (ar->state == ATH10K_STATE_WEDGED)
7204 		return;
7205 
7206 	time_left = wait_event_timeout(ar->htt.empty_tx_wq, ({
7207 			bool empty;
7208 
7209 			spin_lock_bh(&ar->htt.tx_lock);
7210 			empty = (ar->htt.num_pending_tx == 0);
7211 			spin_unlock_bh(&ar->htt.tx_lock);
7212 
7213 			skip = (ar->state == ATH10K_STATE_WEDGED) ||
7214 			       test_bit(ATH10K_FLAG_CRASH_FLUSH,
7215 					&ar->dev_flags);
7216 
7217 			(empty || skip);
7218 		}), ATH10K_FLUSH_TIMEOUT_HZ);
7219 
7220 	if (time_left == 0 || skip)
7221 		ath10k_warn(ar, "failed to flush transmit queue (skip %i ar-state %i): %ld\n",
7222 			    skip, ar->state, time_left);
7223 }
7224 
7225 static void ath10k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
7226 			 u32 queues, bool drop)
7227 {
7228 	struct ath10k *ar = hw->priv;
7229 	struct ath10k_vif *arvif;
7230 	u32 bitmap;
7231 
7232 	if (drop) {
7233 		if (vif && vif->type == NL80211_IFTYPE_STATION) {
7234 			bitmap = ~(1 << WMI_MGMT_TID);
7235 			list_for_each_entry(arvif, &ar->arvifs, list) {
7236 				if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
7237 					ath10k_wmi_peer_flush(ar, arvif->vdev_id,
7238 							      arvif->bssid, bitmap);
7239 			}
7240 			ath10k_htt_flush_tx(&ar->htt);
7241 		}
7242 		return;
7243 	}
7244 
7245 	mutex_lock(&ar->conf_mutex);
7246 	ath10k_mac_wait_tx_complete(ar);
7247 	mutex_unlock(&ar->conf_mutex);
7248 }
7249 
7250 /* TODO: Implement this function properly
7251  * For now it is needed to reply to Probe Requests in IBSS mode.
7252  * Propably we need this information from FW.
7253  */
7254 static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
7255 {
7256 	return 1;
7257 }
7258 
7259 static void ath10k_reconfig_complete(struct ieee80211_hw *hw,
7260 				     enum ieee80211_reconfig_type reconfig_type)
7261 {
7262 	struct ath10k *ar = hw->priv;
7263 
7264 	if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
7265 		return;
7266 
7267 	mutex_lock(&ar->conf_mutex);
7268 
7269 	/* If device failed to restart it will be in a different state, e.g.
7270 	 * ATH10K_STATE_WEDGED
7271 	 */
7272 	if (ar->state == ATH10K_STATE_RESTARTED) {
7273 		ath10k_info(ar, "device successfully recovered\n");
7274 		ar->state = ATH10K_STATE_ON;
7275 		ieee80211_wake_queues(ar->hw);
7276 	}
7277 
7278 	mutex_unlock(&ar->conf_mutex);
7279 }
7280 
7281 static void
7282 ath10k_mac_update_bss_chan_survey(struct ath10k *ar,
7283 				  struct ieee80211_channel *channel)
7284 {
7285 	int ret;
7286 	enum wmi_bss_survey_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ_CLEAR;
7287 
7288 	lockdep_assert_held(&ar->conf_mutex);
7289 
7290 	if (!test_bit(WMI_SERVICE_BSS_CHANNEL_INFO_64, ar->wmi.svc_map) ||
7291 	    (ar->rx_channel != channel))
7292 		return;
7293 
7294 	if (ar->scan.state != ATH10K_SCAN_IDLE) {
7295 		ath10k_dbg(ar, ATH10K_DBG_MAC, "ignoring bss chan info request while scanning..\n");
7296 		return;
7297 	}
7298 
7299 	reinit_completion(&ar->bss_survey_done);
7300 
7301 	ret = ath10k_wmi_pdev_bss_chan_info_request(ar, type);
7302 	if (ret) {
7303 		ath10k_warn(ar, "failed to send pdev bss chan info request\n");
7304 		return;
7305 	}
7306 
7307 	ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
7308 	if (!ret) {
7309 		ath10k_warn(ar, "bss channel survey timed out\n");
7310 		return;
7311 	}
7312 }
7313 
7314 static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
7315 			     struct survey_info *survey)
7316 {
7317 	struct ath10k *ar = hw->priv;
7318 	struct ieee80211_supported_band *sband;
7319 	struct survey_info *ar_survey = &ar->survey[idx];
7320 	int ret = 0;
7321 
7322 	mutex_lock(&ar->conf_mutex);
7323 
7324 	sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
7325 	if (sband && idx >= sband->n_channels) {
7326 		idx -= sband->n_channels;
7327 		sband = NULL;
7328 	}
7329 
7330 	if (!sband)
7331 		sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
7332 
7333 	if (!sband || idx >= sband->n_channels) {
7334 		ret = -ENOENT;
7335 		goto exit;
7336 	}
7337 
7338 	ath10k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
7339 
7340 	spin_lock_bh(&ar->data_lock);
7341 	memcpy(survey, ar_survey, sizeof(*survey));
7342 	spin_unlock_bh(&ar->data_lock);
7343 
7344 	survey->channel = &sband->channels[idx];
7345 
7346 	if (ar->rx_channel == survey->channel)
7347 		survey->filled |= SURVEY_INFO_IN_USE;
7348 
7349 exit:
7350 	mutex_unlock(&ar->conf_mutex);
7351 	return ret;
7352 }
7353 
7354 static bool
7355 ath10k_mac_bitrate_mask_has_single_rate(struct ath10k *ar,
7356 					enum nl80211_band band,
7357 					const struct cfg80211_bitrate_mask *mask,
7358 					int *vht_num_rates)
7359 {
7360 	int num_rates = 0;
7361 	int i, tmp;
7362 
7363 	num_rates += hweight32(mask->control[band].legacy);
7364 
7365 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
7366 		num_rates += hweight8(mask->control[band].ht_mcs[i]);
7367 
7368 	*vht_num_rates = 0;
7369 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
7370 		tmp = hweight16(mask->control[band].vht_mcs[i]);
7371 		num_rates += tmp;
7372 		*vht_num_rates += tmp;
7373 	}
7374 
7375 	return num_rates == 1;
7376 }
7377 
7378 static bool
7379 ath10k_mac_bitrate_mask_get_single_nss(struct ath10k *ar,
7380 				       enum nl80211_band band,
7381 				       const struct cfg80211_bitrate_mask *mask,
7382 				       int *nss)
7383 {
7384 	struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
7385 	u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
7386 	u8 ht_nss_mask = 0;
7387 	u8 vht_nss_mask = 0;
7388 	int i;
7389 
7390 	if (mask->control[band].legacy)
7391 		return false;
7392 
7393 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
7394 		if (mask->control[band].ht_mcs[i] == 0)
7395 			continue;
7396 		else if (mask->control[band].ht_mcs[i] ==
7397 			 sband->ht_cap.mcs.rx_mask[i])
7398 			ht_nss_mask |= BIT(i);
7399 		else
7400 			return false;
7401 	}
7402 
7403 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
7404 		if (mask->control[band].vht_mcs[i] == 0)
7405 			continue;
7406 		else if (mask->control[band].vht_mcs[i] ==
7407 			 ath10k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
7408 			vht_nss_mask |= BIT(i);
7409 		else
7410 			return false;
7411 	}
7412 
7413 	if (ht_nss_mask != vht_nss_mask)
7414 		return false;
7415 
7416 	if (ht_nss_mask == 0)
7417 		return false;
7418 
7419 	if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
7420 		return false;
7421 
7422 	*nss = fls(ht_nss_mask);
7423 
7424 	return true;
7425 }
7426 
7427 static int
7428 ath10k_mac_bitrate_mask_get_single_rate(struct ath10k *ar,
7429 					enum nl80211_band band,
7430 					const struct cfg80211_bitrate_mask *mask,
7431 					u8 *rate, u8 *nss, bool vht_only)
7432 {
7433 	int rate_idx;
7434 	int i;
7435 	u16 bitrate;
7436 	u8 preamble;
7437 	u8 hw_rate;
7438 
7439 	if (vht_only)
7440 		goto next;
7441 
7442 	if (hweight32(mask->control[band].legacy) == 1) {
7443 		rate_idx = ffs(mask->control[band].legacy) - 1;
7444 
7445 		if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY)
7446 			rate_idx += ATH10K_MAC_FIRST_OFDM_RATE_IDX;
7447 
7448 		hw_rate = ath10k_wmi_legacy_rates[rate_idx].hw_value;
7449 		bitrate = ath10k_wmi_legacy_rates[rate_idx].bitrate;
7450 
7451 		if (ath10k_mac_bitrate_is_cck(bitrate))
7452 			preamble = WMI_RATE_PREAMBLE_CCK;
7453 		else
7454 			preamble = WMI_RATE_PREAMBLE_OFDM;
7455 
7456 		*nss = 1;
7457 		*rate = preamble << 6 |
7458 			(*nss - 1) << 4 |
7459 			hw_rate << 0;
7460 
7461 		return 0;
7462 	}
7463 
7464 	for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
7465 		if (hweight8(mask->control[band].ht_mcs[i]) == 1) {
7466 			*nss = i + 1;
7467 			*rate = WMI_RATE_PREAMBLE_HT << 6 |
7468 				(*nss - 1) << 4 |
7469 				(ffs(mask->control[band].ht_mcs[i]) - 1);
7470 
7471 			return 0;
7472 		}
7473 	}
7474 
7475 next:
7476 	for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
7477 		if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
7478 			*nss = i + 1;
7479 			*rate = WMI_RATE_PREAMBLE_VHT << 6 |
7480 				(*nss - 1) << 4 |
7481 				(ffs(mask->control[band].vht_mcs[i]) - 1);
7482 
7483 			return 0;
7484 		}
7485 	}
7486 
7487 	return -EINVAL;
7488 }
7489 
7490 static int ath10k_mac_set_fixed_rate_params(struct ath10k_vif *arvif,
7491 					    u8 rate, u8 nss, u8 sgi, u8 ldpc)
7492 {
7493 	struct ath10k *ar = arvif->ar;
7494 	u32 vdev_param;
7495 	int ret;
7496 
7497 	lockdep_assert_held(&ar->conf_mutex);
7498 
7499 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac set fixed rate params vdev %i rate 0x%02hhx nss %hhu sgi %hhu\n",
7500 		   arvif->vdev_id, rate, nss, sgi);
7501 
7502 	vdev_param = ar->wmi.vdev_param->fixed_rate;
7503 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, rate);
7504 	if (ret) {
7505 		ath10k_warn(ar, "failed to set fixed rate param 0x%02x: %d\n",
7506 			    rate, ret);
7507 		return ret;
7508 	}
7509 
7510 	vdev_param = ar->wmi.vdev_param->nss;
7511 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, nss);
7512 	if (ret) {
7513 		ath10k_warn(ar, "failed to set nss param %d: %d\n", nss, ret);
7514 		return ret;
7515 	}
7516 
7517 	vdev_param = ar->wmi.vdev_param->sgi;
7518 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, sgi);
7519 	if (ret) {
7520 		ath10k_warn(ar, "failed to set sgi param %d: %d\n", sgi, ret);
7521 		return ret;
7522 	}
7523 
7524 	vdev_param = ar->wmi.vdev_param->ldpc;
7525 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, ldpc);
7526 	if (ret) {
7527 		ath10k_warn(ar, "failed to set ldpc param %d: %d\n", ldpc, ret);
7528 		return ret;
7529 	}
7530 
7531 	return 0;
7532 }
7533 
7534 static bool
7535 ath10k_mac_can_set_bitrate_mask(struct ath10k *ar,
7536 				enum nl80211_band band,
7537 				const struct cfg80211_bitrate_mask *mask,
7538 				bool allow_pfr)
7539 {
7540 	int i;
7541 	u16 vht_mcs;
7542 
7543 	/* Due to firmware limitation in WMI_PEER_ASSOC_CMDID it is impossible
7544 	 * to express all VHT MCS rate masks. Effectively only the following
7545 	 * ranges can be used: none, 0-7, 0-8 and 0-9.
7546 	 */
7547 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
7548 		vht_mcs = mask->control[band].vht_mcs[i];
7549 
7550 		switch (vht_mcs) {
7551 		case 0:
7552 		case BIT(8) - 1:
7553 		case BIT(9) - 1:
7554 		case BIT(10) - 1:
7555 			break;
7556 		default:
7557 			if (!allow_pfr)
7558 				ath10k_warn(ar, "refusing bitrate mask with missing 0-7 VHT MCS rates\n");
7559 			return false;
7560 		}
7561 	}
7562 
7563 	return true;
7564 }
7565 
7566 static bool ath10k_mac_set_vht_bitrate_mask_fixup(struct ath10k *ar,
7567 						  struct ath10k_vif *arvif,
7568 						  struct ieee80211_sta *sta)
7569 {
7570 	int err;
7571 	u8 rate = arvif->vht_pfr;
7572 
7573 	/* skip non vht and multiple rate peers */
7574 	if (!sta->vht_cap.vht_supported || arvif->vht_num_rates != 1)
7575 		return false;
7576 
7577 	err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
7578 					WMI_PEER_PARAM_FIXED_RATE, rate);
7579 	if (err)
7580 		ath10k_warn(ar, "failed to enable STA %pM peer fixed rate: %d\n",
7581 			    sta->addr, err);
7582 
7583 	return true;
7584 }
7585 
7586 static void ath10k_mac_set_bitrate_mask_iter(void *data,
7587 					     struct ieee80211_sta *sta)
7588 {
7589 	struct ath10k_vif *arvif = data;
7590 	struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
7591 	struct ath10k *ar = arvif->ar;
7592 
7593 	if (arsta->arvif != arvif)
7594 		return;
7595 
7596 	if (ath10k_mac_set_vht_bitrate_mask_fixup(ar, arvif, sta))
7597 		return;
7598 
7599 	spin_lock_bh(&ar->data_lock);
7600 	arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
7601 	spin_unlock_bh(&ar->data_lock);
7602 
7603 	ieee80211_queue_work(ar->hw, &arsta->update_wk);
7604 }
7605 
7606 static void ath10k_mac_clr_bitrate_mask_iter(void *data,
7607 					     struct ieee80211_sta *sta)
7608 {
7609 	struct ath10k_vif *arvif = data;
7610 	struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
7611 	struct ath10k *ar = arvif->ar;
7612 	int err;
7613 
7614 	/* clear vht peers only */
7615 	if (arsta->arvif != arvif || !sta->vht_cap.vht_supported)
7616 		return;
7617 
7618 	err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
7619 					WMI_PEER_PARAM_FIXED_RATE,
7620 					WMI_FIXED_RATE_NONE);
7621 	if (err)
7622 		ath10k_warn(ar, "failed to clear STA %pM peer fixed rate: %d\n",
7623 			    sta->addr, err);
7624 }
7625 
7626 static int ath10k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
7627 					  struct ieee80211_vif *vif,
7628 					  const struct cfg80211_bitrate_mask *mask)
7629 {
7630 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
7631 	struct cfg80211_chan_def def;
7632 	struct ath10k *ar = arvif->ar;
7633 	enum nl80211_band band;
7634 	const u8 *ht_mcs_mask;
7635 	const u16 *vht_mcs_mask;
7636 	u8 rate;
7637 	u8 nss;
7638 	u8 sgi;
7639 	u8 ldpc;
7640 	int single_nss;
7641 	int ret;
7642 	int vht_num_rates, allow_pfr;
7643 	u8 vht_pfr;
7644 	bool update_bitrate_mask = true;
7645 
7646 	if (ath10k_mac_vif_chan(vif, &def))
7647 		return -EPERM;
7648 
7649 	band = def.chan->band;
7650 	ht_mcs_mask = mask->control[band].ht_mcs;
7651 	vht_mcs_mask = mask->control[band].vht_mcs;
7652 	ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
7653 
7654 	sgi = mask->control[band].gi;
7655 	if (sgi == NL80211_TXRATE_FORCE_LGI)
7656 		return -EINVAL;
7657 
7658 	allow_pfr = test_bit(ATH10K_FW_FEATURE_PEER_FIXED_RATE,
7659 			     ar->normal_mode_fw.fw_file.fw_features);
7660 	if (allow_pfr) {
7661 		mutex_lock(&ar->conf_mutex);
7662 		ieee80211_iterate_stations_atomic(ar->hw,
7663 						  ath10k_mac_clr_bitrate_mask_iter,
7664 						  arvif);
7665 		mutex_unlock(&ar->conf_mutex);
7666 	}
7667 
7668 	if (ath10k_mac_bitrate_mask_has_single_rate(ar, band, mask,
7669 						    &vht_num_rates)) {
7670 		ret = ath10k_mac_bitrate_mask_get_single_rate(ar, band, mask,
7671 							      &rate, &nss,
7672 							      false);
7673 		if (ret) {
7674 			ath10k_warn(ar, "failed to get single rate for vdev %i: %d\n",
7675 				    arvif->vdev_id, ret);
7676 			return ret;
7677 		}
7678 	} else if (ath10k_mac_bitrate_mask_get_single_nss(ar, band, mask,
7679 							  &single_nss)) {
7680 		rate = WMI_FIXED_RATE_NONE;
7681 		nss = single_nss;
7682 	} else {
7683 		rate = WMI_FIXED_RATE_NONE;
7684 		nss = min(ar->num_rf_chains,
7685 			  max(ath10k_mac_max_ht_nss(ht_mcs_mask),
7686 			      ath10k_mac_max_vht_nss(vht_mcs_mask)));
7687 
7688 		if (!ath10k_mac_can_set_bitrate_mask(ar, band, mask,
7689 						     allow_pfr)) {
7690 			u8 vht_nss;
7691 
7692 			if (!allow_pfr || vht_num_rates != 1)
7693 				return -EINVAL;
7694 
7695 			/* Reach here, firmware supports peer fixed rate and has
7696 			 * single vht rate, and don't update vif birate_mask, as
7697 			 * the rate only for specific peer.
7698 			 */
7699 			ath10k_mac_bitrate_mask_get_single_rate(ar, band, mask,
7700 								&vht_pfr,
7701 								&vht_nss,
7702 								true);
7703 			update_bitrate_mask = false;
7704 		} else {
7705 			vht_pfr = 0;
7706 		}
7707 
7708 		mutex_lock(&ar->conf_mutex);
7709 
7710 		if (update_bitrate_mask)
7711 			arvif->bitrate_mask = *mask;
7712 		arvif->vht_num_rates = vht_num_rates;
7713 		arvif->vht_pfr = vht_pfr;
7714 		ieee80211_iterate_stations_atomic(ar->hw,
7715 						  ath10k_mac_set_bitrate_mask_iter,
7716 						  arvif);
7717 
7718 		mutex_unlock(&ar->conf_mutex);
7719 	}
7720 
7721 	mutex_lock(&ar->conf_mutex);
7722 
7723 	ret = ath10k_mac_set_fixed_rate_params(arvif, rate, nss, sgi, ldpc);
7724 	if (ret) {
7725 		ath10k_warn(ar, "failed to set fixed rate params on vdev %i: %d\n",
7726 			    arvif->vdev_id, ret);
7727 		goto exit;
7728 	}
7729 
7730 exit:
7731 	mutex_unlock(&ar->conf_mutex);
7732 
7733 	return ret;
7734 }
7735 
7736 static void ath10k_sta_rc_update(struct ieee80211_hw *hw,
7737 				 struct ieee80211_vif *vif,
7738 				 struct ieee80211_sta *sta,
7739 				 u32 changed)
7740 {
7741 	struct ath10k *ar = hw->priv;
7742 	struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
7743 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
7744 	struct ath10k_peer *peer;
7745 	u32 bw, smps;
7746 
7747 	spin_lock_bh(&ar->data_lock);
7748 
7749 	peer = ath10k_peer_find(ar, arvif->vdev_id, sta->addr);
7750 	if (!peer) {
7751 		spin_unlock_bh(&ar->data_lock);
7752 		ath10k_warn(ar, "mac sta rc update failed to find peer %pM on vdev %i\n",
7753 			    sta->addr, arvif->vdev_id);
7754 		return;
7755 	}
7756 
7757 	ath10k_dbg(ar, ATH10K_DBG_MAC,
7758 		   "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
7759 		   sta->addr, changed, sta->bandwidth, sta->rx_nss,
7760 		   sta->smps_mode);
7761 
7762 	if (changed & IEEE80211_RC_BW_CHANGED) {
7763 		bw = WMI_PEER_CHWIDTH_20MHZ;
7764 
7765 		switch (sta->bandwidth) {
7766 		case IEEE80211_STA_RX_BW_20:
7767 			bw = WMI_PEER_CHWIDTH_20MHZ;
7768 			break;
7769 		case IEEE80211_STA_RX_BW_40:
7770 			bw = WMI_PEER_CHWIDTH_40MHZ;
7771 			break;
7772 		case IEEE80211_STA_RX_BW_80:
7773 			bw = WMI_PEER_CHWIDTH_80MHZ;
7774 			break;
7775 		case IEEE80211_STA_RX_BW_160:
7776 			bw = WMI_PEER_CHWIDTH_160MHZ;
7777 			break;
7778 		default:
7779 			ath10k_warn(ar, "Invalid bandwidth %d in rc update for %pM\n",
7780 				    sta->bandwidth, sta->addr);
7781 			bw = WMI_PEER_CHWIDTH_20MHZ;
7782 			break;
7783 		}
7784 
7785 		arsta->bw = bw;
7786 	}
7787 
7788 	if (changed & IEEE80211_RC_NSS_CHANGED)
7789 		arsta->nss = sta->rx_nss;
7790 
7791 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
7792 		smps = WMI_PEER_SMPS_PS_NONE;
7793 
7794 		switch (sta->smps_mode) {
7795 		case IEEE80211_SMPS_AUTOMATIC:
7796 		case IEEE80211_SMPS_OFF:
7797 			smps = WMI_PEER_SMPS_PS_NONE;
7798 			break;
7799 		case IEEE80211_SMPS_STATIC:
7800 			smps = WMI_PEER_SMPS_STATIC;
7801 			break;
7802 		case IEEE80211_SMPS_DYNAMIC:
7803 			smps = WMI_PEER_SMPS_DYNAMIC;
7804 			break;
7805 		case IEEE80211_SMPS_NUM_MODES:
7806 			ath10k_warn(ar, "Invalid smps %d in sta rc update for %pM\n",
7807 				    sta->smps_mode, sta->addr);
7808 			smps = WMI_PEER_SMPS_PS_NONE;
7809 			break;
7810 		}
7811 
7812 		arsta->smps = smps;
7813 	}
7814 
7815 	arsta->changed |= changed;
7816 
7817 	spin_unlock_bh(&ar->data_lock);
7818 
7819 	ieee80211_queue_work(hw, &arsta->update_wk);
7820 }
7821 
7822 static void ath10k_offset_tsf(struct ieee80211_hw *hw,
7823 			      struct ieee80211_vif *vif, s64 tsf_offset)
7824 {
7825 	struct ath10k *ar = hw->priv;
7826 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
7827 	u32 offset, vdev_param;
7828 	int ret;
7829 
7830 	if (tsf_offset < 0) {
7831 		vdev_param = ar->wmi.vdev_param->dec_tsf;
7832 		offset = -tsf_offset;
7833 	} else {
7834 		vdev_param = ar->wmi.vdev_param->inc_tsf;
7835 		offset = tsf_offset;
7836 	}
7837 
7838 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
7839 					vdev_param, offset);
7840 
7841 	if (ret && ret != -EOPNOTSUPP)
7842 		ath10k_warn(ar, "failed to set tsf offset %d cmd %d: %d\n",
7843 			    offset, vdev_param, ret);
7844 }
7845 
7846 static int ath10k_ampdu_action(struct ieee80211_hw *hw,
7847 			       struct ieee80211_vif *vif,
7848 			       struct ieee80211_ampdu_params *params)
7849 {
7850 	struct ath10k *ar = hw->priv;
7851 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
7852 	struct ieee80211_sta *sta = params->sta;
7853 	enum ieee80211_ampdu_mlme_action action = params->action;
7854 	u16 tid = params->tid;
7855 
7856 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ampdu vdev_id %i sta %pM tid %hu action %d\n",
7857 		   arvif->vdev_id, sta->addr, tid, action);
7858 
7859 	switch (action) {
7860 	case IEEE80211_AMPDU_RX_START:
7861 	case IEEE80211_AMPDU_RX_STOP:
7862 		/* HTT AddBa/DelBa events trigger mac80211 Rx BA session
7863 		 * creation/removal. Do we need to verify this?
7864 		 */
7865 		return 0;
7866 	case IEEE80211_AMPDU_TX_START:
7867 	case IEEE80211_AMPDU_TX_STOP_CONT:
7868 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
7869 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
7870 	case IEEE80211_AMPDU_TX_OPERATIONAL:
7871 		/* Firmware offloads Tx aggregation entirely so deny mac80211
7872 		 * Tx aggregation requests.
7873 		 */
7874 		return -EOPNOTSUPP;
7875 	}
7876 
7877 	return -EINVAL;
7878 }
7879 
7880 static void
7881 ath10k_mac_update_rx_channel(struct ath10k *ar,
7882 			     struct ieee80211_chanctx_conf *ctx,
7883 			     struct ieee80211_vif_chanctx_switch *vifs,
7884 			     int n_vifs)
7885 {
7886 	struct cfg80211_chan_def *def = NULL;
7887 
7888 	/* Both locks are required because ar->rx_channel is modified. This
7889 	 * allows readers to hold either lock.
7890 	 */
7891 	lockdep_assert_held(&ar->conf_mutex);
7892 	lockdep_assert_held(&ar->data_lock);
7893 
7894 	WARN_ON(ctx && vifs);
7895 	WARN_ON(vifs && !n_vifs);
7896 
7897 	/* FIXME: Sort of an optimization and a workaround. Peers and vifs are
7898 	 * on a linked list now. Doing a lookup peer -> vif -> chanctx for each
7899 	 * ppdu on Rx may reduce performance on low-end systems. It should be
7900 	 * possible to make tables/hashmaps to speed the lookup up (be vary of
7901 	 * cpu data cache lines though regarding sizes) but to keep the initial
7902 	 * implementation simple and less intrusive fallback to the slow lookup
7903 	 * only for multi-channel cases. Single-channel cases will remain to
7904 	 * use the old channel derival and thus performance should not be
7905 	 * affected much.
7906 	 */
7907 	rcu_read_lock();
7908 	if (!ctx && ath10k_mac_num_chanctxs(ar) == 1) {
7909 		ieee80211_iter_chan_contexts_atomic(ar->hw,
7910 						    ath10k_mac_get_any_chandef_iter,
7911 						    &def);
7912 
7913 		if (vifs)
7914 			def = &vifs[0].new_ctx->def;
7915 
7916 		ar->rx_channel = def->chan;
7917 	} else if ((ctx && ath10k_mac_num_chanctxs(ar) == 0) ||
7918 		   (ctx && (ar->state == ATH10K_STATE_RESTARTED))) {
7919 		/* During driver restart due to firmware assert, since mac80211
7920 		 * already has valid channel context for given radio, channel
7921 		 * context iteration return num_chanctx > 0. So fix rx_channel
7922 		 * when restart is in progress.
7923 		 */
7924 		ar->rx_channel = ctx->def.chan;
7925 	} else {
7926 		ar->rx_channel = NULL;
7927 	}
7928 	rcu_read_unlock();
7929 }
7930 
7931 static void
7932 ath10k_mac_update_vif_chan(struct ath10k *ar,
7933 			   struct ieee80211_vif_chanctx_switch *vifs,
7934 			   int n_vifs)
7935 {
7936 	struct ath10k_vif *arvif;
7937 	int ret;
7938 	int i;
7939 
7940 	lockdep_assert_held(&ar->conf_mutex);
7941 
7942 	/* First stop monitor interface. Some FW versions crash if there's a
7943 	 * lone monitor interface.
7944 	 */
7945 	if (ar->monitor_started)
7946 		ath10k_monitor_stop(ar);
7947 
7948 	for (i = 0; i < n_vifs; i++) {
7949 		arvif = (void *)vifs[i].vif->drv_priv;
7950 
7951 		ath10k_dbg(ar, ATH10K_DBG_MAC,
7952 			   "mac chanctx switch vdev_id %i freq %hu->%hu width %d->%d\n",
7953 			   arvif->vdev_id,
7954 			   vifs[i].old_ctx->def.chan->center_freq,
7955 			   vifs[i].new_ctx->def.chan->center_freq,
7956 			   vifs[i].old_ctx->def.width,
7957 			   vifs[i].new_ctx->def.width);
7958 
7959 		if (WARN_ON(!arvif->is_started))
7960 			continue;
7961 
7962 		if (WARN_ON(!arvif->is_up))
7963 			continue;
7964 
7965 		ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
7966 		if (ret) {
7967 			ath10k_warn(ar, "failed to down vdev %d: %d\n",
7968 				    arvif->vdev_id, ret);
7969 			continue;
7970 		}
7971 	}
7972 
7973 	/* All relevant vdevs are downed and associated channel resources
7974 	 * should be available for the channel switch now.
7975 	 */
7976 
7977 	spin_lock_bh(&ar->data_lock);
7978 	ath10k_mac_update_rx_channel(ar, NULL, vifs, n_vifs);
7979 	spin_unlock_bh(&ar->data_lock);
7980 
7981 	for (i = 0; i < n_vifs; i++) {
7982 		arvif = (void *)vifs[i].vif->drv_priv;
7983 
7984 		if (WARN_ON(!arvif->is_started))
7985 			continue;
7986 
7987 		if (WARN_ON(!arvif->is_up))
7988 			continue;
7989 
7990 		ret = ath10k_mac_setup_bcn_tmpl(arvif);
7991 		if (ret)
7992 			ath10k_warn(ar, "failed to update bcn tmpl during csa: %d\n",
7993 				    ret);
7994 
7995 		ret = ath10k_mac_setup_prb_tmpl(arvif);
7996 		if (ret)
7997 			ath10k_warn(ar, "failed to update prb tmpl during csa: %d\n",
7998 				    ret);
7999 
8000 		ret = ath10k_vdev_restart(arvif, &vifs[i].new_ctx->def);
8001 		if (ret) {
8002 			ath10k_warn(ar, "failed to restart vdev %d: %d\n",
8003 				    arvif->vdev_id, ret);
8004 			continue;
8005 		}
8006 
8007 		ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
8008 					 arvif->bssid);
8009 		if (ret) {
8010 			ath10k_warn(ar, "failed to bring vdev up %d: %d\n",
8011 				    arvif->vdev_id, ret);
8012 			continue;
8013 		}
8014 	}
8015 
8016 	ath10k_monitor_recalc(ar);
8017 }
8018 
8019 static int
8020 ath10k_mac_op_add_chanctx(struct ieee80211_hw *hw,
8021 			  struct ieee80211_chanctx_conf *ctx)
8022 {
8023 	struct ath10k *ar = hw->priv;
8024 
8025 	ath10k_dbg(ar, ATH10K_DBG_MAC,
8026 		   "mac chanctx add freq %hu width %d ptr %pK\n",
8027 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
8028 
8029 	mutex_lock(&ar->conf_mutex);
8030 
8031 	spin_lock_bh(&ar->data_lock);
8032 	ath10k_mac_update_rx_channel(ar, ctx, NULL, 0);
8033 	spin_unlock_bh(&ar->data_lock);
8034 
8035 	ath10k_recalc_radar_detection(ar);
8036 	ath10k_monitor_recalc(ar);
8037 
8038 	mutex_unlock(&ar->conf_mutex);
8039 
8040 	return 0;
8041 }
8042 
8043 static void
8044 ath10k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
8045 			     struct ieee80211_chanctx_conf *ctx)
8046 {
8047 	struct ath10k *ar = hw->priv;
8048 
8049 	ath10k_dbg(ar, ATH10K_DBG_MAC,
8050 		   "mac chanctx remove freq %hu width %d ptr %pK\n",
8051 		   ctx->def.chan->center_freq, ctx->def.width, ctx);
8052 
8053 	mutex_lock(&ar->conf_mutex);
8054 
8055 	spin_lock_bh(&ar->data_lock);
8056 	ath10k_mac_update_rx_channel(ar, NULL, NULL, 0);
8057 	spin_unlock_bh(&ar->data_lock);
8058 
8059 	ath10k_recalc_radar_detection(ar);
8060 	ath10k_monitor_recalc(ar);
8061 
8062 	mutex_unlock(&ar->conf_mutex);
8063 }
8064 
8065 struct ath10k_mac_change_chanctx_arg {
8066 	struct ieee80211_chanctx_conf *ctx;
8067 	struct ieee80211_vif_chanctx_switch *vifs;
8068 	int n_vifs;
8069 	int next_vif;
8070 };
8071 
8072 static void
8073 ath10k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
8074 				   struct ieee80211_vif *vif)
8075 {
8076 	struct ath10k_mac_change_chanctx_arg *arg = data;
8077 
8078 	if (rcu_access_pointer(vif->chanctx_conf) != arg->ctx)
8079 		return;
8080 
8081 	arg->n_vifs++;
8082 }
8083 
8084 static void
8085 ath10k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
8086 				    struct ieee80211_vif *vif)
8087 {
8088 	struct ath10k_mac_change_chanctx_arg *arg = data;
8089 	struct ieee80211_chanctx_conf *ctx;
8090 
8091 	ctx = rcu_access_pointer(vif->chanctx_conf);
8092 	if (ctx != arg->ctx)
8093 		return;
8094 
8095 	if (WARN_ON(arg->next_vif == arg->n_vifs))
8096 		return;
8097 
8098 	arg->vifs[arg->next_vif].vif = vif;
8099 	arg->vifs[arg->next_vif].old_ctx = ctx;
8100 	arg->vifs[arg->next_vif].new_ctx = ctx;
8101 	arg->next_vif++;
8102 }
8103 
8104 static void
8105 ath10k_mac_op_change_chanctx(struct ieee80211_hw *hw,
8106 			     struct ieee80211_chanctx_conf *ctx,
8107 			     u32 changed)
8108 {
8109 	struct ath10k *ar = hw->priv;
8110 	struct ath10k_mac_change_chanctx_arg arg = { .ctx = ctx };
8111 
8112 	mutex_lock(&ar->conf_mutex);
8113 
8114 	ath10k_dbg(ar, ATH10K_DBG_MAC,
8115 		   "mac chanctx change freq %hu width %d ptr %pK changed %x\n",
8116 		   ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
8117 
8118 	/* This shouldn't really happen because channel switching should use
8119 	 * switch_vif_chanctx().
8120 	 */
8121 	if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
8122 		goto unlock;
8123 
8124 	if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH) {
8125 		ieee80211_iterate_active_interfaces_atomic(
8126 					hw,
8127 					IEEE80211_IFACE_ITER_NORMAL,
8128 					ath10k_mac_change_chanctx_cnt_iter,
8129 					&arg);
8130 		if (arg.n_vifs == 0)
8131 			goto radar;
8132 
8133 		arg.vifs = kcalloc(arg.n_vifs, sizeof(arg.vifs[0]),
8134 				   GFP_KERNEL);
8135 		if (!arg.vifs)
8136 			goto radar;
8137 
8138 		ieee80211_iterate_active_interfaces_atomic(
8139 					hw,
8140 					IEEE80211_IFACE_ITER_NORMAL,
8141 					ath10k_mac_change_chanctx_fill_iter,
8142 					&arg);
8143 		ath10k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
8144 		kfree(arg.vifs);
8145 	}
8146 
8147 radar:
8148 	ath10k_recalc_radar_detection(ar);
8149 
8150 	/* FIXME: How to configure Rx chains properly? */
8151 
8152 	/* No other actions are actually necessary. Firmware maintains channel
8153 	 * definitions per vdev internally and there's no host-side channel
8154 	 * context abstraction to configure, e.g. channel width.
8155 	 */
8156 
8157 unlock:
8158 	mutex_unlock(&ar->conf_mutex);
8159 }
8160 
8161 static int
8162 ath10k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
8163 				 struct ieee80211_vif *vif,
8164 				 struct ieee80211_chanctx_conf *ctx)
8165 {
8166 	struct ath10k *ar = hw->priv;
8167 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
8168 	int ret;
8169 
8170 	mutex_lock(&ar->conf_mutex);
8171 
8172 	ath10k_dbg(ar, ATH10K_DBG_MAC,
8173 		   "mac chanctx assign ptr %pK vdev_id %i\n",
8174 		   ctx, arvif->vdev_id);
8175 
8176 	if (WARN_ON(arvif->is_started)) {
8177 		mutex_unlock(&ar->conf_mutex);
8178 		return -EBUSY;
8179 	}
8180 
8181 	ret = ath10k_vdev_start(arvif, &ctx->def);
8182 	if (ret) {
8183 		ath10k_warn(ar, "failed to start vdev %i addr %pM on freq %d: %d\n",
8184 			    arvif->vdev_id, vif->addr,
8185 			    ctx->def.chan->center_freq, ret);
8186 		goto err;
8187 	}
8188 
8189 	arvif->is_started = true;
8190 
8191 	ret = ath10k_mac_vif_setup_ps(arvif);
8192 	if (ret) {
8193 		ath10k_warn(ar, "failed to update vdev %i ps: %d\n",
8194 			    arvif->vdev_id, ret);
8195 		goto err_stop;
8196 	}
8197 
8198 	if (vif->type == NL80211_IFTYPE_MONITOR) {
8199 		ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, 0, vif->addr);
8200 		if (ret) {
8201 			ath10k_warn(ar, "failed to up monitor vdev %i: %d\n",
8202 				    arvif->vdev_id, ret);
8203 			goto err_stop;
8204 		}
8205 
8206 		arvif->is_up = true;
8207 	}
8208 
8209 	if (ath10k_mac_can_set_cts_prot(arvif)) {
8210 		ret = ath10k_mac_set_cts_prot(arvif);
8211 		if (ret)
8212 			ath10k_warn(ar, "failed to set cts protection for vdev %d: %d\n",
8213 				    arvif->vdev_id, ret);
8214 	}
8215 
8216 	if (ath10k_peer_stats_enabled(ar) &&
8217 	    ar->hw_params.tx_stats_over_pktlog) {
8218 		ar->pktlog_filter |= ATH10K_PKTLOG_PEER_STATS;
8219 		ret = ath10k_wmi_pdev_pktlog_enable(ar,
8220 						    ar->pktlog_filter);
8221 		if (ret) {
8222 			ath10k_warn(ar, "failed to enable pktlog %d\n", ret);
8223 			goto err_stop;
8224 		}
8225 	}
8226 
8227 	mutex_unlock(&ar->conf_mutex);
8228 	return 0;
8229 
8230 err_stop:
8231 	ath10k_vdev_stop(arvif);
8232 	arvif->is_started = false;
8233 	ath10k_mac_vif_setup_ps(arvif);
8234 
8235 err:
8236 	mutex_unlock(&ar->conf_mutex);
8237 	return ret;
8238 }
8239 
8240 static void
8241 ath10k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
8242 				   struct ieee80211_vif *vif,
8243 				   struct ieee80211_chanctx_conf *ctx)
8244 {
8245 	struct ath10k *ar = hw->priv;
8246 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
8247 	int ret;
8248 
8249 	mutex_lock(&ar->conf_mutex);
8250 
8251 	ath10k_dbg(ar, ATH10K_DBG_MAC,
8252 		   "mac chanctx unassign ptr %pK vdev_id %i\n",
8253 		   ctx, arvif->vdev_id);
8254 
8255 	WARN_ON(!arvif->is_started);
8256 
8257 	if (vif->type == NL80211_IFTYPE_MONITOR) {
8258 		WARN_ON(!arvif->is_up);
8259 
8260 		ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
8261 		if (ret)
8262 			ath10k_warn(ar, "failed to down monitor vdev %i: %d\n",
8263 				    arvif->vdev_id, ret);
8264 
8265 		arvif->is_up = false;
8266 	}
8267 
8268 	ret = ath10k_vdev_stop(arvif);
8269 	if (ret)
8270 		ath10k_warn(ar, "failed to stop vdev %i: %d\n",
8271 			    arvif->vdev_id, ret);
8272 
8273 	arvif->is_started = false;
8274 
8275 	mutex_unlock(&ar->conf_mutex);
8276 }
8277 
8278 static int
8279 ath10k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
8280 				 struct ieee80211_vif_chanctx_switch *vifs,
8281 				 int n_vifs,
8282 				 enum ieee80211_chanctx_switch_mode mode)
8283 {
8284 	struct ath10k *ar = hw->priv;
8285 
8286 	mutex_lock(&ar->conf_mutex);
8287 
8288 	ath10k_dbg(ar, ATH10K_DBG_MAC,
8289 		   "mac chanctx switch n_vifs %d mode %d\n",
8290 		   n_vifs, mode);
8291 	ath10k_mac_update_vif_chan(ar, vifs, n_vifs);
8292 
8293 	mutex_unlock(&ar->conf_mutex);
8294 	return 0;
8295 }
8296 
8297 static void ath10k_mac_op_sta_pre_rcu_remove(struct ieee80211_hw *hw,
8298 					     struct ieee80211_vif *vif,
8299 					     struct ieee80211_sta *sta)
8300 {
8301 	struct ath10k *ar;
8302 	struct ath10k_peer *peer;
8303 
8304 	ar = hw->priv;
8305 
8306 	list_for_each_entry(peer, &ar->peers, list)
8307 		if (peer->sta == sta)
8308 			peer->removed = true;
8309 }
8310 
8311 /* HT MCS parameters with Nss = 1 */
8312 static const struct ath10k_index_ht_data_rate_type supported_ht_mcs_rate_nss1[] = {
8313 	/* MCS  L20   L40   S20  S40 */
8314 	{0,  { 65,  135,  72,  150} },
8315 	{1,  { 130, 270,  144, 300} },
8316 	{2,  { 195, 405,  217, 450} },
8317 	{3,  { 260, 540,  289, 600} },
8318 	{4,  { 390, 810,  433, 900} },
8319 	{5,  { 520, 1080, 578, 1200} },
8320 	{6,  { 585, 1215, 650, 1350} },
8321 	{7,  { 650, 1350, 722, 1500} }
8322 };
8323 
8324 /* HT MCS parameters with Nss = 2 */
8325 static const struct ath10k_index_ht_data_rate_type supported_ht_mcs_rate_nss2[] = {
8326 	/* MCS  L20    L40   S20   S40 */
8327 	{0,  {130,  270,  144,  300} },
8328 	{1,  {260,  540,  289,  600} },
8329 	{2,  {390,  810,  433,  900} },
8330 	{3,  {520,  1080, 578,  1200} },
8331 	{4,  {780,  1620, 867,  1800} },
8332 	{5,  {1040, 2160, 1156, 2400} },
8333 	{6,  {1170, 2430, 1300, 2700} },
8334 	{7,  {1300, 2700, 1444, 3000} }
8335 };
8336 
8337 /* MCS parameters with Nss = 1 */
8338 static const struct ath10k_index_vht_data_rate_type supported_vht_mcs_rate_nss1[] = {
8339 	/* MCS  L80    S80     L40   S40    L20   S20 */
8340 	{0,  {293,  325},  {135,  150},  {65,   72} },
8341 	{1,  {585,  650},  {270,  300},  {130,  144} },
8342 	{2,  {878,  975},  {405,  450},  {195,  217} },
8343 	{3,  {1170, 1300}, {540,  600},  {260,  289} },
8344 	{4,  {1755, 1950}, {810,  900},  {390,  433} },
8345 	{5,  {2340, 2600}, {1080, 1200}, {520,  578} },
8346 	{6,  {2633, 2925}, {1215, 1350}, {585,  650} },
8347 	{7,  {2925, 3250}, {1350, 1500}, {650,  722} },
8348 	{8,  {3510, 3900}, {1620, 1800}, {780,  867} },
8349 	{9,  {3900, 4333}, {1800, 2000}, {780,  867} }
8350 };
8351 
8352 /*MCS parameters with Nss = 2 */
8353 static const struct ath10k_index_vht_data_rate_type supported_vht_mcs_rate_nss2[] = {
8354 	/* MCS  L80    S80     L40   S40    L20   S20 */
8355 	{0,  {585,  650},  {270,  300},  {130,  144} },
8356 	{1,  {1170, 1300}, {540,  600},  {260,  289} },
8357 	{2,  {1755, 1950}, {810,  900},  {390,  433} },
8358 	{3,  {2340, 2600}, {1080, 1200}, {520,  578} },
8359 	{4,  {3510, 3900}, {1620, 1800}, {780,  867} },
8360 	{5,  {4680, 5200}, {2160, 2400}, {1040, 1156} },
8361 	{6,  {5265, 5850}, {2430, 2700}, {1170, 1300} },
8362 	{7,  {5850, 6500}, {2700, 3000}, {1300, 1444} },
8363 	{8,  {7020, 7800}, {3240, 3600}, {1560, 1733} },
8364 	{9,  {7800, 8667}, {3600, 4000}, {1560, 1733} }
8365 };
8366 
8367 static void ath10k_mac_get_rate_flags_ht(struct ath10k *ar, u32 rate, u8 nss, u8 mcs,
8368 					 u8 *flags, u8 *bw)
8369 {
8370 	struct ath10k_index_ht_data_rate_type *mcs_rate;
8371 
8372 	mcs_rate = (struct ath10k_index_ht_data_rate_type *)
8373 		   ((nss == 1) ? &supported_ht_mcs_rate_nss1 :
8374 		   &supported_ht_mcs_rate_nss2);
8375 
8376 	if (rate == mcs_rate[mcs].supported_rate[0]) {
8377 		*bw = RATE_INFO_BW_20;
8378 	} else if (rate == mcs_rate[mcs].supported_rate[1]) {
8379 		*bw |= RATE_INFO_BW_40;
8380 	} else if (rate == mcs_rate[mcs].supported_rate[2]) {
8381 		*bw |= RATE_INFO_BW_20;
8382 		*flags |= RATE_INFO_FLAGS_SHORT_GI;
8383 	} else if (rate == mcs_rate[mcs].supported_rate[3]) {
8384 		*bw |= RATE_INFO_BW_40;
8385 		*flags |= RATE_INFO_FLAGS_SHORT_GI;
8386 	} else {
8387 		ath10k_warn(ar, "invalid ht params rate %d 100kbps nss %d mcs %d",
8388 			    rate, nss, mcs);
8389 	}
8390 }
8391 
8392 static void ath10k_mac_get_rate_flags_vht(struct ath10k *ar, u32 rate, u8 nss, u8 mcs,
8393 					  u8 *flags, u8 *bw)
8394 {
8395 	struct ath10k_index_vht_data_rate_type *mcs_rate;
8396 
8397 	mcs_rate = (struct ath10k_index_vht_data_rate_type *)
8398 		   ((nss == 1) ? &supported_vht_mcs_rate_nss1 :
8399 		   &supported_vht_mcs_rate_nss2);
8400 
8401 	if (rate == mcs_rate[mcs].supported_VHT80_rate[0]) {
8402 		*bw = RATE_INFO_BW_80;
8403 	} else if (rate == mcs_rate[mcs].supported_VHT80_rate[1]) {
8404 		*bw = RATE_INFO_BW_80;
8405 		*flags |= RATE_INFO_FLAGS_SHORT_GI;
8406 	} else if (rate == mcs_rate[mcs].supported_VHT40_rate[0]) {
8407 		*bw = RATE_INFO_BW_40;
8408 	} else if (rate == mcs_rate[mcs].supported_VHT40_rate[1]) {
8409 		*bw = RATE_INFO_BW_40;
8410 		*flags |= RATE_INFO_FLAGS_SHORT_GI;
8411 	} else if (rate == mcs_rate[mcs].supported_VHT20_rate[0]) {
8412 		*bw = RATE_INFO_BW_20;
8413 	} else if (rate == mcs_rate[mcs].supported_VHT20_rate[1]) {
8414 		*bw = RATE_INFO_BW_20;
8415 		*flags |= RATE_INFO_FLAGS_SHORT_GI;
8416 	} else {
8417 		ath10k_warn(ar, "invalid vht params rate %d 100kbps nss %d mcs %d",
8418 			    rate, nss, mcs);
8419 	}
8420 }
8421 
8422 static void ath10k_mac_get_rate_flags(struct ath10k *ar, u32 rate,
8423 				      enum ath10k_phy_mode mode, u8 nss, u8 mcs,
8424 				      u8 *flags, u8 *bw)
8425 {
8426 	if (mode == ATH10K_PHY_MODE_HT) {
8427 		*flags = RATE_INFO_FLAGS_MCS;
8428 		ath10k_mac_get_rate_flags_ht(ar, rate, nss, mcs, flags, bw);
8429 	} else if (mode == ATH10K_PHY_MODE_VHT) {
8430 		*flags = RATE_INFO_FLAGS_VHT_MCS;
8431 		ath10k_mac_get_rate_flags_vht(ar, rate, nss, mcs, flags, bw);
8432 	}
8433 }
8434 
8435 static void ath10k_mac_parse_bitrate(struct ath10k *ar, u32 rate_code,
8436 				     u32 bitrate_kbps, struct rate_info *rate)
8437 {
8438 	enum ath10k_phy_mode mode = ATH10K_PHY_MODE_LEGACY;
8439 	enum wmi_rate_preamble preamble = WMI_TLV_GET_HW_RC_PREAM_V1(rate_code);
8440 	u8 nss = WMI_TLV_GET_HW_RC_NSS_V1(rate_code) + 1;
8441 	u8 mcs = WMI_TLV_GET_HW_RC_RATE_V1(rate_code);
8442 	u8 flags = 0, bw = 0;
8443 
8444 	if (preamble == WMI_RATE_PREAMBLE_HT)
8445 		mode = ATH10K_PHY_MODE_HT;
8446 	else if (preamble == WMI_RATE_PREAMBLE_VHT)
8447 		mode = ATH10K_PHY_MODE_VHT;
8448 
8449 	ath10k_mac_get_rate_flags(ar, bitrate_kbps / 100, mode, nss, mcs, &flags, &bw);
8450 
8451 	ath10k_dbg(ar, ATH10K_DBG_MAC,
8452 		   "mac parse bitrate preamble %d mode %d nss %d mcs %d flags %x bw %d\n",
8453 		   preamble, mode, nss, mcs, flags, bw);
8454 
8455 	rate->flags = flags;
8456 	rate->bw = bw;
8457 	rate->legacy = bitrate_kbps / 100;
8458 	rate->nss = nss;
8459 	rate->mcs = mcs;
8460 }
8461 
8462 static void ath10k_mac_sta_get_peer_stats_info(struct ath10k *ar,
8463 					       struct ieee80211_sta *sta,
8464 					       struct station_info *sinfo)
8465 {
8466 	struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
8467 	struct ath10k_peer *peer;
8468 	unsigned long time_left;
8469 	int ret;
8470 
8471 	if (!(ar->hw_params.supports_peer_stats_info &&
8472 	      arsta->arvif->vdev_type == WMI_VDEV_TYPE_STA))
8473 		return;
8474 
8475 	spin_lock_bh(&ar->data_lock);
8476 	peer = ath10k_peer_find(ar, arsta->arvif->vdev_id, sta->addr);
8477 	spin_unlock_bh(&ar->data_lock);
8478 	if (!peer)
8479 		return;
8480 
8481 	reinit_completion(&ar->peer_stats_info_complete);
8482 
8483 	ret = ath10k_wmi_request_peer_stats_info(ar,
8484 						 arsta->arvif->vdev_id,
8485 						 WMI_REQUEST_ONE_PEER_STATS_INFO,
8486 						 arsta->arvif->bssid,
8487 						 0);
8488 	if (ret && ret != -EOPNOTSUPP) {
8489 		ath10k_warn(ar, "could not request peer stats info: %d\n", ret);
8490 		return;
8491 	}
8492 
8493 	time_left = wait_for_completion_timeout(&ar->peer_stats_info_complete, 3 * HZ);
8494 	if (time_left == 0) {
8495 		ath10k_warn(ar, "timed out waiting peer stats info\n");
8496 		return;
8497 	}
8498 
8499 	if (arsta->rx_rate_code != 0 && arsta->rx_bitrate_kbps != 0) {
8500 		ath10k_mac_parse_bitrate(ar, arsta->rx_rate_code,
8501 					 arsta->rx_bitrate_kbps,
8502 					 &sinfo->rxrate);
8503 
8504 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
8505 		arsta->rx_rate_code = 0;
8506 		arsta->rx_bitrate_kbps = 0;
8507 	}
8508 
8509 	if (arsta->tx_rate_code != 0 && arsta->tx_bitrate_kbps != 0) {
8510 		ath10k_mac_parse_bitrate(ar, arsta->tx_rate_code,
8511 					 arsta->tx_bitrate_kbps,
8512 					 &sinfo->txrate);
8513 
8514 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
8515 		arsta->tx_rate_code = 0;
8516 		arsta->tx_bitrate_kbps = 0;
8517 	}
8518 }
8519 
8520 static void ath10k_sta_statistics(struct ieee80211_hw *hw,
8521 				  struct ieee80211_vif *vif,
8522 				  struct ieee80211_sta *sta,
8523 				  struct station_info *sinfo)
8524 {
8525 	struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
8526 	struct ath10k *ar = arsta->arvif->ar;
8527 
8528 	if (!ath10k_peer_stats_enabled(ar))
8529 		return;
8530 
8531 	ath10k_debug_fw_stats_request(ar);
8532 
8533 	sinfo->rx_duration = arsta->rx_duration;
8534 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
8535 
8536 	if (!arsta->txrate.legacy && !arsta->txrate.nss)
8537 		return;
8538 
8539 	if (arsta->txrate.legacy) {
8540 		sinfo->txrate.legacy = arsta->txrate.legacy;
8541 	} else {
8542 		sinfo->txrate.mcs = arsta->txrate.mcs;
8543 		sinfo->txrate.nss = arsta->txrate.nss;
8544 		sinfo->txrate.bw = arsta->txrate.bw;
8545 	}
8546 	sinfo->txrate.flags = arsta->txrate.flags;
8547 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
8548 
8549 	if (ar->htt.disable_tx_comp) {
8550 		sinfo->tx_retries = arsta->tx_retries;
8551 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
8552 		sinfo->tx_failed = arsta->tx_failed;
8553 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
8554 	}
8555 
8556 	ath10k_mac_sta_get_peer_stats_info(ar, sta, sinfo);
8557 }
8558 
8559 static const struct ieee80211_ops ath10k_ops = {
8560 	.tx				= ath10k_mac_op_tx,
8561 	.wake_tx_queue			= ath10k_mac_op_wake_tx_queue,
8562 	.start				= ath10k_start,
8563 	.stop				= ath10k_stop,
8564 	.config				= ath10k_config,
8565 	.add_interface			= ath10k_add_interface,
8566 	.remove_interface		= ath10k_remove_interface,
8567 	.configure_filter		= ath10k_configure_filter,
8568 	.bss_info_changed		= ath10k_bss_info_changed,
8569 	.set_coverage_class		= ath10k_mac_op_set_coverage_class,
8570 	.hw_scan			= ath10k_hw_scan,
8571 	.cancel_hw_scan			= ath10k_cancel_hw_scan,
8572 	.set_key			= ath10k_set_key,
8573 	.set_default_unicast_key        = ath10k_set_default_unicast_key,
8574 	.sta_state			= ath10k_sta_state,
8575 	.sta_set_txpwr			= ath10k_sta_set_txpwr,
8576 	.conf_tx			= ath10k_conf_tx,
8577 	.remain_on_channel		= ath10k_remain_on_channel,
8578 	.cancel_remain_on_channel	= ath10k_cancel_remain_on_channel,
8579 	.set_rts_threshold		= ath10k_set_rts_threshold,
8580 	.set_frag_threshold		= ath10k_mac_op_set_frag_threshold,
8581 	.flush				= ath10k_flush,
8582 	.tx_last_beacon			= ath10k_tx_last_beacon,
8583 	.set_antenna			= ath10k_set_antenna,
8584 	.get_antenna			= ath10k_get_antenna,
8585 	.reconfig_complete		= ath10k_reconfig_complete,
8586 	.get_survey			= ath10k_get_survey,
8587 	.set_bitrate_mask		= ath10k_mac_op_set_bitrate_mask,
8588 	.sta_rc_update			= ath10k_sta_rc_update,
8589 	.offset_tsf			= ath10k_offset_tsf,
8590 	.ampdu_action			= ath10k_ampdu_action,
8591 	.get_et_sset_count		= ath10k_debug_get_et_sset_count,
8592 	.get_et_stats			= ath10k_debug_get_et_stats,
8593 	.get_et_strings			= ath10k_debug_get_et_strings,
8594 	.add_chanctx			= ath10k_mac_op_add_chanctx,
8595 	.remove_chanctx			= ath10k_mac_op_remove_chanctx,
8596 	.change_chanctx			= ath10k_mac_op_change_chanctx,
8597 	.assign_vif_chanctx		= ath10k_mac_op_assign_vif_chanctx,
8598 	.unassign_vif_chanctx		= ath10k_mac_op_unassign_vif_chanctx,
8599 	.switch_vif_chanctx		= ath10k_mac_op_switch_vif_chanctx,
8600 	.sta_pre_rcu_remove		= ath10k_mac_op_sta_pre_rcu_remove,
8601 	.sta_statistics			= ath10k_sta_statistics,
8602 
8603 	CFG80211_TESTMODE_CMD(ath10k_tm_cmd)
8604 
8605 #ifdef CONFIG_PM
8606 	.suspend			= ath10k_wow_op_suspend,
8607 	.resume				= ath10k_wow_op_resume,
8608 	.set_wakeup			= ath10k_wow_op_set_wakeup,
8609 #endif
8610 #ifdef CONFIG_MAC80211_DEBUGFS
8611 	.sta_add_debugfs		= ath10k_sta_add_debugfs,
8612 #endif
8613 };
8614 
8615 #define CHAN2G(_channel, _freq, _flags) { \
8616 	.band			= NL80211_BAND_2GHZ, \
8617 	.hw_value		= (_channel), \
8618 	.center_freq		= (_freq), \
8619 	.flags			= (_flags), \
8620 	.max_antenna_gain	= 0, \
8621 	.max_power		= 30, \
8622 }
8623 
8624 #define CHAN5G(_channel, _freq, _flags) { \
8625 	.band			= NL80211_BAND_5GHZ, \
8626 	.hw_value		= (_channel), \
8627 	.center_freq		= (_freq), \
8628 	.flags			= (_flags), \
8629 	.max_antenna_gain	= 0, \
8630 	.max_power		= 30, \
8631 }
8632 
8633 static const struct ieee80211_channel ath10k_2ghz_channels[] = {
8634 	CHAN2G(1, 2412, 0),
8635 	CHAN2G(2, 2417, 0),
8636 	CHAN2G(3, 2422, 0),
8637 	CHAN2G(4, 2427, 0),
8638 	CHAN2G(5, 2432, 0),
8639 	CHAN2G(6, 2437, 0),
8640 	CHAN2G(7, 2442, 0),
8641 	CHAN2G(8, 2447, 0),
8642 	CHAN2G(9, 2452, 0),
8643 	CHAN2G(10, 2457, 0),
8644 	CHAN2G(11, 2462, 0),
8645 	CHAN2G(12, 2467, 0),
8646 	CHAN2G(13, 2472, 0),
8647 	CHAN2G(14, 2484, 0),
8648 };
8649 
8650 static const struct ieee80211_channel ath10k_5ghz_channels[] = {
8651 	CHAN5G(36, 5180, 0),
8652 	CHAN5G(40, 5200, 0),
8653 	CHAN5G(44, 5220, 0),
8654 	CHAN5G(48, 5240, 0),
8655 	CHAN5G(52, 5260, 0),
8656 	CHAN5G(56, 5280, 0),
8657 	CHAN5G(60, 5300, 0),
8658 	CHAN5G(64, 5320, 0),
8659 	CHAN5G(100, 5500, 0),
8660 	CHAN5G(104, 5520, 0),
8661 	CHAN5G(108, 5540, 0),
8662 	CHAN5G(112, 5560, 0),
8663 	CHAN5G(116, 5580, 0),
8664 	CHAN5G(120, 5600, 0),
8665 	CHAN5G(124, 5620, 0),
8666 	CHAN5G(128, 5640, 0),
8667 	CHAN5G(132, 5660, 0),
8668 	CHAN5G(136, 5680, 0),
8669 	CHAN5G(140, 5700, 0),
8670 	CHAN5G(144, 5720, 0),
8671 	CHAN5G(149, 5745, 0),
8672 	CHAN5G(153, 5765, 0),
8673 	CHAN5G(157, 5785, 0),
8674 	CHAN5G(161, 5805, 0),
8675 	CHAN5G(165, 5825, 0),
8676 	CHAN5G(169, 5845, 0),
8677 	CHAN5G(173, 5865, 0),
8678 	/* If you add more, you may need to change ATH10K_MAX_5G_CHAN */
8679 	/* And you will definitely need to change ATH10K_NUM_CHANS in core.h */
8680 };
8681 
8682 struct ath10k *ath10k_mac_create(size_t priv_size)
8683 {
8684 	struct ieee80211_hw *hw;
8685 	struct ieee80211_ops *ops;
8686 	struct ath10k *ar;
8687 
8688 	ops = kmemdup(&ath10k_ops, sizeof(ath10k_ops), GFP_KERNEL);
8689 	if (!ops)
8690 		return NULL;
8691 
8692 	hw = ieee80211_alloc_hw(sizeof(struct ath10k) + priv_size, ops);
8693 	if (!hw) {
8694 		kfree(ops);
8695 		return NULL;
8696 	}
8697 
8698 	ar = hw->priv;
8699 	ar->hw = hw;
8700 	ar->ops = ops;
8701 
8702 	return ar;
8703 }
8704 
8705 void ath10k_mac_destroy(struct ath10k *ar)
8706 {
8707 	struct ieee80211_ops *ops = ar->ops;
8708 
8709 	ieee80211_free_hw(ar->hw);
8710 	kfree(ops);
8711 }
8712 
8713 static const struct ieee80211_iface_limit ath10k_if_limits[] = {
8714 	{
8715 		.max	= 8,
8716 		.types	= BIT(NL80211_IFTYPE_STATION)
8717 			| BIT(NL80211_IFTYPE_P2P_CLIENT)
8718 	},
8719 	{
8720 		.max	= 3,
8721 		.types	= BIT(NL80211_IFTYPE_P2P_GO)
8722 	},
8723 	{
8724 		.max	= 1,
8725 		.types	= BIT(NL80211_IFTYPE_P2P_DEVICE)
8726 	},
8727 	{
8728 		.max	= 7,
8729 		.types	= BIT(NL80211_IFTYPE_AP)
8730 #ifdef CONFIG_MAC80211_MESH
8731 			| BIT(NL80211_IFTYPE_MESH_POINT)
8732 #endif
8733 	},
8734 };
8735 
8736 static const struct ieee80211_iface_limit ath10k_10x_if_limits[] = {
8737 	{
8738 		.max	= 8,
8739 		.types	= BIT(NL80211_IFTYPE_AP)
8740 #ifdef CONFIG_MAC80211_MESH
8741 			| BIT(NL80211_IFTYPE_MESH_POINT)
8742 #endif
8743 	},
8744 	{
8745 		.max	= 1,
8746 		.types	= BIT(NL80211_IFTYPE_STATION)
8747 	},
8748 };
8749 
8750 static const struct ieee80211_iface_combination ath10k_if_comb[] = {
8751 	{
8752 		.limits = ath10k_if_limits,
8753 		.n_limits = ARRAY_SIZE(ath10k_if_limits),
8754 		.max_interfaces = 8,
8755 		.num_different_channels = 1,
8756 		.beacon_int_infra_match = true,
8757 	},
8758 };
8759 
8760 static const struct ieee80211_iface_combination ath10k_10x_if_comb[] = {
8761 	{
8762 		.limits = ath10k_10x_if_limits,
8763 		.n_limits = ARRAY_SIZE(ath10k_10x_if_limits),
8764 		.max_interfaces = 8,
8765 		.num_different_channels = 1,
8766 		.beacon_int_infra_match = true,
8767 		.beacon_int_min_gcd = 1,
8768 #ifdef CONFIG_ATH10K_DFS_CERTIFIED
8769 		.radar_detect_widths =	BIT(NL80211_CHAN_WIDTH_20_NOHT) |
8770 					BIT(NL80211_CHAN_WIDTH_20) |
8771 					BIT(NL80211_CHAN_WIDTH_40) |
8772 					BIT(NL80211_CHAN_WIDTH_80),
8773 #endif
8774 	},
8775 };
8776 
8777 static const struct ieee80211_iface_limit ath10k_tlv_if_limit[] = {
8778 	{
8779 		.max = 2,
8780 		.types = BIT(NL80211_IFTYPE_STATION),
8781 	},
8782 	{
8783 		.max = 2,
8784 		.types = BIT(NL80211_IFTYPE_AP) |
8785 #ifdef CONFIG_MAC80211_MESH
8786 			 BIT(NL80211_IFTYPE_MESH_POINT) |
8787 #endif
8788 			 BIT(NL80211_IFTYPE_P2P_CLIENT) |
8789 			 BIT(NL80211_IFTYPE_P2P_GO),
8790 	},
8791 	{
8792 		.max = 1,
8793 		.types = BIT(NL80211_IFTYPE_P2P_DEVICE),
8794 	},
8795 };
8796 
8797 static const struct ieee80211_iface_limit ath10k_tlv_qcs_if_limit[] = {
8798 	{
8799 		.max = 2,
8800 		.types = BIT(NL80211_IFTYPE_STATION),
8801 	},
8802 	{
8803 		.max = 2,
8804 		.types = BIT(NL80211_IFTYPE_P2P_CLIENT),
8805 	},
8806 	{
8807 		.max = 1,
8808 		.types = BIT(NL80211_IFTYPE_AP) |
8809 #ifdef CONFIG_MAC80211_MESH
8810 			 BIT(NL80211_IFTYPE_MESH_POINT) |
8811 #endif
8812 			 BIT(NL80211_IFTYPE_P2P_GO),
8813 	},
8814 	{
8815 		.max = 1,
8816 		.types = BIT(NL80211_IFTYPE_P2P_DEVICE),
8817 	},
8818 };
8819 
8820 static const struct ieee80211_iface_limit ath10k_tlv_if_limit_ibss[] = {
8821 	{
8822 		.max = 1,
8823 		.types = BIT(NL80211_IFTYPE_STATION),
8824 	},
8825 	{
8826 		.max = 1,
8827 		.types = BIT(NL80211_IFTYPE_ADHOC),
8828 	},
8829 };
8830 
8831 /* FIXME: This is not thouroughly tested. These combinations may over- or
8832  * underestimate hw/fw capabilities.
8833  */
8834 static struct ieee80211_iface_combination ath10k_tlv_if_comb[] = {
8835 	{
8836 		.limits = ath10k_tlv_if_limit,
8837 		.num_different_channels = 1,
8838 		.max_interfaces = 4,
8839 		.n_limits = ARRAY_SIZE(ath10k_tlv_if_limit),
8840 	},
8841 	{
8842 		.limits = ath10k_tlv_if_limit_ibss,
8843 		.num_different_channels = 1,
8844 		.max_interfaces = 2,
8845 		.n_limits = ARRAY_SIZE(ath10k_tlv_if_limit_ibss),
8846 	},
8847 };
8848 
8849 static struct ieee80211_iface_combination ath10k_tlv_qcs_if_comb[] = {
8850 	{
8851 		.limits = ath10k_tlv_if_limit,
8852 		.num_different_channels = 1,
8853 		.max_interfaces = 4,
8854 		.n_limits = ARRAY_SIZE(ath10k_tlv_if_limit),
8855 	},
8856 	{
8857 		.limits = ath10k_tlv_qcs_if_limit,
8858 		.num_different_channels = 2,
8859 		.max_interfaces = 4,
8860 		.n_limits = ARRAY_SIZE(ath10k_tlv_qcs_if_limit),
8861 	},
8862 	{
8863 		.limits = ath10k_tlv_if_limit_ibss,
8864 		.num_different_channels = 1,
8865 		.max_interfaces = 2,
8866 		.n_limits = ARRAY_SIZE(ath10k_tlv_if_limit_ibss),
8867 	},
8868 };
8869 
8870 static const struct ieee80211_iface_limit ath10k_10_4_if_limits[] = {
8871 	{
8872 		.max = 1,
8873 		.types = BIT(NL80211_IFTYPE_STATION),
8874 	},
8875 	{
8876 		.max	= 16,
8877 		.types	= BIT(NL80211_IFTYPE_AP)
8878 #ifdef CONFIG_MAC80211_MESH
8879 			| BIT(NL80211_IFTYPE_MESH_POINT)
8880 #endif
8881 	},
8882 };
8883 
8884 static const struct ieee80211_iface_combination ath10k_10_4_if_comb[] = {
8885 	{
8886 		.limits = ath10k_10_4_if_limits,
8887 		.n_limits = ARRAY_SIZE(ath10k_10_4_if_limits),
8888 		.max_interfaces = 16,
8889 		.num_different_channels = 1,
8890 		.beacon_int_infra_match = true,
8891 		.beacon_int_min_gcd = 1,
8892 #ifdef CONFIG_ATH10K_DFS_CERTIFIED
8893 		.radar_detect_widths =	BIT(NL80211_CHAN_WIDTH_20_NOHT) |
8894 					BIT(NL80211_CHAN_WIDTH_20) |
8895 					BIT(NL80211_CHAN_WIDTH_40) |
8896 					BIT(NL80211_CHAN_WIDTH_80) |
8897 					BIT(NL80211_CHAN_WIDTH_80P80) |
8898 					BIT(NL80211_CHAN_WIDTH_160),
8899 #endif
8900 	},
8901 };
8902 
8903 static const struct
8904 ieee80211_iface_combination ath10k_10_4_bcn_int_if_comb[] = {
8905 	{
8906 		.limits = ath10k_10_4_if_limits,
8907 		.n_limits = ARRAY_SIZE(ath10k_10_4_if_limits),
8908 		.max_interfaces = 16,
8909 		.num_different_channels = 1,
8910 		.beacon_int_infra_match = true,
8911 		.beacon_int_min_gcd = 100,
8912 #ifdef CONFIG_ATH10K_DFS_CERTIFIED
8913 		.radar_detect_widths =  BIT(NL80211_CHAN_WIDTH_20_NOHT) |
8914 					BIT(NL80211_CHAN_WIDTH_20) |
8915 					BIT(NL80211_CHAN_WIDTH_40) |
8916 					BIT(NL80211_CHAN_WIDTH_80) |
8917 					BIT(NL80211_CHAN_WIDTH_80P80) |
8918 					BIT(NL80211_CHAN_WIDTH_160),
8919 #endif
8920 	},
8921 };
8922 
8923 static void ath10k_get_arvif_iter(void *data, u8 *mac,
8924 				  struct ieee80211_vif *vif)
8925 {
8926 	struct ath10k_vif_iter *arvif_iter = data;
8927 	struct ath10k_vif *arvif = (void *)vif->drv_priv;
8928 
8929 	if (arvif->vdev_id == arvif_iter->vdev_id)
8930 		arvif_iter->arvif = arvif;
8931 }
8932 
8933 struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
8934 {
8935 	struct ath10k_vif_iter arvif_iter;
8936 	u32 flags;
8937 
8938 	memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
8939 	arvif_iter.vdev_id = vdev_id;
8940 
8941 	flags = IEEE80211_IFACE_ITER_RESUME_ALL;
8942 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
8943 						   flags,
8944 						   ath10k_get_arvif_iter,
8945 						   &arvif_iter);
8946 	if (!arvif_iter.arvif) {
8947 		ath10k_warn(ar, "No VIF found for vdev %d\n", vdev_id);
8948 		return NULL;
8949 	}
8950 
8951 	return arvif_iter.arvif;
8952 }
8953 
8954 #define WRD_METHOD "WRDD"
8955 #define WRDD_WIFI  (0x07)
8956 
8957 static u32 ath10k_mac_wrdd_get_mcc(struct ath10k *ar, union acpi_object *wrdd)
8958 {
8959 	union acpi_object *mcc_pkg;
8960 	union acpi_object *domain_type;
8961 	union acpi_object *mcc_value;
8962 	u32 i;
8963 
8964 	if (wrdd->type != ACPI_TYPE_PACKAGE ||
8965 	    wrdd->package.count < 2 ||
8966 	    wrdd->package.elements[0].type != ACPI_TYPE_INTEGER ||
8967 	    wrdd->package.elements[0].integer.value != 0) {
8968 		ath10k_warn(ar, "ignoring malformed/unsupported wrdd structure\n");
8969 		return 0;
8970 	}
8971 
8972 	for (i = 1; i < wrdd->package.count; ++i) {
8973 		mcc_pkg = &wrdd->package.elements[i];
8974 
8975 		if (mcc_pkg->type != ACPI_TYPE_PACKAGE)
8976 			continue;
8977 		if (mcc_pkg->package.count < 2)
8978 			continue;
8979 		if (mcc_pkg->package.elements[0].type != ACPI_TYPE_INTEGER ||
8980 		    mcc_pkg->package.elements[1].type != ACPI_TYPE_INTEGER)
8981 			continue;
8982 
8983 		domain_type = &mcc_pkg->package.elements[0];
8984 		if (domain_type->integer.value != WRDD_WIFI)
8985 			continue;
8986 
8987 		mcc_value = &mcc_pkg->package.elements[1];
8988 		return mcc_value->integer.value;
8989 	}
8990 	return 0;
8991 }
8992 
8993 static int ath10k_mac_get_wrdd_regulatory(struct ath10k *ar, u16 *rd)
8994 {
8995 	acpi_handle root_handle;
8996 	acpi_handle handle;
8997 	struct acpi_buffer wrdd = {ACPI_ALLOCATE_BUFFER, NULL};
8998 	acpi_status status;
8999 	u32 alpha2_code;
9000 	char alpha2[3];
9001 
9002 	root_handle = ACPI_HANDLE(ar->dev);
9003 	if (!root_handle)
9004 		return -EOPNOTSUPP;
9005 
9006 	status = acpi_get_handle(root_handle, (acpi_string)WRD_METHOD, &handle);
9007 	if (ACPI_FAILURE(status)) {
9008 		ath10k_dbg(ar, ATH10K_DBG_BOOT,
9009 			   "failed to get wrd method %d\n", status);
9010 		return -EIO;
9011 	}
9012 
9013 	status = acpi_evaluate_object(handle, NULL, NULL, &wrdd);
9014 	if (ACPI_FAILURE(status)) {
9015 		ath10k_dbg(ar, ATH10K_DBG_BOOT,
9016 			   "failed to call wrdc %d\n", status);
9017 		return -EIO;
9018 	}
9019 
9020 	alpha2_code = ath10k_mac_wrdd_get_mcc(ar, wrdd.pointer);
9021 	kfree(wrdd.pointer);
9022 	if (!alpha2_code)
9023 		return -EIO;
9024 
9025 	alpha2[0] = (alpha2_code >> 8) & 0xff;
9026 	alpha2[1] = (alpha2_code >> 0) & 0xff;
9027 	alpha2[2] = '\0';
9028 
9029 	ath10k_dbg(ar, ATH10K_DBG_BOOT,
9030 		   "regulatory hint from WRDD (alpha2-code): %s\n", alpha2);
9031 
9032 	*rd = ath_regd_find_country_by_name(alpha2);
9033 	if (*rd == 0xffff)
9034 		return -EIO;
9035 
9036 	*rd |= COUNTRY_ERD_FLAG;
9037 	return 0;
9038 }
9039 
9040 static int ath10k_mac_init_rd(struct ath10k *ar)
9041 {
9042 	int ret;
9043 	u16 rd;
9044 
9045 	ret = ath10k_mac_get_wrdd_regulatory(ar, &rd);
9046 	if (ret) {
9047 		ath10k_dbg(ar, ATH10K_DBG_BOOT,
9048 			   "fallback to eeprom programmed regulatory settings\n");
9049 		rd = ar->hw_eeprom_rd;
9050 	}
9051 
9052 	ar->ath_common.regulatory.current_rd = rd;
9053 	return 0;
9054 }
9055 
9056 int ath10k_mac_register(struct ath10k *ar)
9057 {
9058 	static const u32 cipher_suites[] = {
9059 		WLAN_CIPHER_SUITE_WEP40,
9060 		WLAN_CIPHER_SUITE_WEP104,
9061 		WLAN_CIPHER_SUITE_TKIP,
9062 		WLAN_CIPHER_SUITE_CCMP,
9063 
9064 		/* Do not add hardware supported ciphers before this line.
9065 		 * Allow software encryption for all chips. Don't forget to
9066 		 * update n_cipher_suites below.
9067 		 */
9068 		WLAN_CIPHER_SUITE_AES_CMAC,
9069 		WLAN_CIPHER_SUITE_BIP_CMAC_256,
9070 		WLAN_CIPHER_SUITE_BIP_GMAC_128,
9071 		WLAN_CIPHER_SUITE_BIP_GMAC_256,
9072 
9073 		/* Only QCA99x0 and QCA4019 varients support GCMP-128, GCMP-256
9074 		 * and CCMP-256 in hardware.
9075 		 */
9076 		WLAN_CIPHER_SUITE_GCMP,
9077 		WLAN_CIPHER_SUITE_GCMP_256,
9078 		WLAN_CIPHER_SUITE_CCMP_256,
9079 	};
9080 	struct ieee80211_supported_band *band;
9081 	void *channels;
9082 	int ret;
9083 
9084 	if (!is_valid_ether_addr(ar->mac_addr)) {
9085 		ath10k_warn(ar, "invalid MAC address; choosing random\n");
9086 		eth_random_addr(ar->mac_addr);
9087 	}
9088 	SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
9089 
9090 	SET_IEEE80211_DEV(ar->hw, ar->dev);
9091 
9092 	BUILD_BUG_ON((ARRAY_SIZE(ath10k_2ghz_channels) +
9093 		      ARRAY_SIZE(ath10k_5ghz_channels)) !=
9094 		     ATH10K_NUM_CHANS);
9095 
9096 	if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
9097 		channels = kmemdup(ath10k_2ghz_channels,
9098 				   sizeof(ath10k_2ghz_channels),
9099 				   GFP_KERNEL);
9100 		if (!channels) {
9101 			ret = -ENOMEM;
9102 			goto err_free;
9103 		}
9104 
9105 		band = &ar->mac.sbands[NL80211_BAND_2GHZ];
9106 		band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
9107 		band->channels = channels;
9108 
9109 		if (ar->hw_params.cck_rate_map_rev2) {
9110 			band->n_bitrates = ath10k_g_rates_rev2_size;
9111 			band->bitrates = ath10k_g_rates_rev2;
9112 		} else {
9113 			band->n_bitrates = ath10k_g_rates_size;
9114 			band->bitrates = ath10k_g_rates;
9115 		}
9116 
9117 		ar->hw->wiphy->bands[NL80211_BAND_2GHZ] = band;
9118 	}
9119 
9120 	if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
9121 		channels = kmemdup(ath10k_5ghz_channels,
9122 				   sizeof(ath10k_5ghz_channels),
9123 				   GFP_KERNEL);
9124 		if (!channels) {
9125 			ret = -ENOMEM;
9126 			goto err_free;
9127 		}
9128 
9129 		band = &ar->mac.sbands[NL80211_BAND_5GHZ];
9130 		band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
9131 		band->channels = channels;
9132 		band->n_bitrates = ath10k_a_rates_size;
9133 		band->bitrates = ath10k_a_rates;
9134 		ar->hw->wiphy->bands[NL80211_BAND_5GHZ] = band;
9135 	}
9136 
9137 	wiphy_read_of_freq_limits(ar->hw->wiphy);
9138 	ath10k_mac_setup_ht_vht_cap(ar);
9139 
9140 	ar->hw->wiphy->interface_modes =
9141 		BIT(NL80211_IFTYPE_STATION) |
9142 		BIT(NL80211_IFTYPE_AP) |
9143 		BIT(NL80211_IFTYPE_MESH_POINT);
9144 
9145 	ar->hw->wiphy->available_antennas_rx = ar->cfg_rx_chainmask;
9146 	ar->hw->wiphy->available_antennas_tx = ar->cfg_tx_chainmask;
9147 
9148 	if (!test_bit(ATH10K_FW_FEATURE_NO_P2P, ar->normal_mode_fw.fw_file.fw_features))
9149 		ar->hw->wiphy->interface_modes |=
9150 			BIT(NL80211_IFTYPE_P2P_DEVICE) |
9151 			BIT(NL80211_IFTYPE_P2P_CLIENT) |
9152 			BIT(NL80211_IFTYPE_P2P_GO);
9153 
9154 	ieee80211_hw_set(ar->hw, SIGNAL_DBM);
9155 
9156 	if (!test_bit(ATH10K_FW_FEATURE_NO_PS,
9157 		      ar->running_fw->fw_file.fw_features)) {
9158 		ieee80211_hw_set(ar->hw, SUPPORTS_PS);
9159 		ieee80211_hw_set(ar->hw, SUPPORTS_DYNAMIC_PS);
9160 	}
9161 
9162 	ieee80211_hw_set(ar->hw, MFP_CAPABLE);
9163 	ieee80211_hw_set(ar->hw, REPORTS_TX_ACK_STATUS);
9164 	ieee80211_hw_set(ar->hw, HAS_RATE_CONTROL);
9165 	ieee80211_hw_set(ar->hw, AP_LINK_PS);
9166 	ieee80211_hw_set(ar->hw, SPECTRUM_MGMT);
9167 	ieee80211_hw_set(ar->hw, SUPPORT_FAST_XMIT);
9168 	ieee80211_hw_set(ar->hw, CONNECTION_MONITOR);
9169 	ieee80211_hw_set(ar->hw, SUPPORTS_PER_STA_GTK);
9170 	ieee80211_hw_set(ar->hw, WANT_MONITOR_VIF);
9171 	ieee80211_hw_set(ar->hw, CHANCTX_STA_CSA);
9172 	ieee80211_hw_set(ar->hw, QUEUE_CONTROL);
9173 	ieee80211_hw_set(ar->hw, SUPPORTS_TX_FRAG);
9174 	ieee80211_hw_set(ar->hw, REPORTS_LOW_ACK);
9175 
9176 	if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
9177 		ieee80211_hw_set(ar->hw, SW_CRYPTO_CONTROL);
9178 
9179 	ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
9180 	ar->hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
9181 
9182 	if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
9183 		ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
9184 
9185 	if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
9186 		ieee80211_hw_set(ar->hw, AMPDU_AGGREGATION);
9187 		ieee80211_hw_set(ar->hw, TX_AMPDU_SETUP_IN_HW);
9188 	}
9189 
9190 	ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
9191 	ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
9192 
9193 	if (test_bit(WMI_SERVICE_NLO, ar->wmi.svc_map)) {
9194 		ar->hw->wiphy->max_sched_scan_ssids = WMI_PNO_MAX_SUPP_NETWORKS;
9195 		ar->hw->wiphy->max_match_sets = WMI_PNO_MAX_SUPP_NETWORKS;
9196 		ar->hw->wiphy->max_sched_scan_ie_len = WMI_PNO_MAX_IE_LENGTH;
9197 		ar->hw->wiphy->max_sched_scan_plans = WMI_PNO_MAX_SCHED_SCAN_PLANS;
9198 		ar->hw->wiphy->max_sched_scan_plan_interval =
9199 			WMI_PNO_MAX_SCHED_SCAN_PLAN_INT;
9200 		ar->hw->wiphy->max_sched_scan_plan_iterations =
9201 			WMI_PNO_MAX_SCHED_SCAN_PLAN_ITRNS;
9202 		ar->hw->wiphy->features |= NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
9203 	}
9204 
9205 	ar->hw->vif_data_size = sizeof(struct ath10k_vif);
9206 	ar->hw->sta_data_size = sizeof(struct ath10k_sta);
9207 	ar->hw->txq_data_size = sizeof(struct ath10k_txq);
9208 
9209 	ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
9210 
9211 	if (test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)) {
9212 		ar->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
9213 
9214 		/* Firmware delivers WPS/P2P Probe Requests frames to driver so
9215 		 * that userspace (e.g. wpa_supplicant/hostapd) can generate
9216 		 * correct Probe Responses. This is more of a hack advert..
9217 		 */
9218 		ar->hw->wiphy->probe_resp_offload |=
9219 			NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
9220 			NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
9221 			NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
9222 	}
9223 
9224 	if (test_bit(WMI_SERVICE_TDLS, ar->wmi.svc_map) ||
9225 	    test_bit(WMI_SERVICE_TDLS_EXPLICIT_MODE_ONLY, ar->wmi.svc_map)) {
9226 		ar->hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
9227 		if (test_bit(WMI_SERVICE_TDLS_WIDER_BANDWIDTH, ar->wmi.svc_map))
9228 			ieee80211_hw_set(ar->hw, TDLS_WIDER_BW);
9229 	}
9230 
9231 	if (test_bit(WMI_SERVICE_TDLS_UAPSD_BUFFER_STA, ar->wmi.svc_map))
9232 		ieee80211_hw_set(ar->hw, SUPPORTS_TDLS_BUFFER_STA);
9233 
9234 	ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
9235 	ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
9236 	ar->hw->wiphy->max_remain_on_channel_duration = 5000;
9237 
9238 	ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
9239 	ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
9240 				   NL80211_FEATURE_AP_SCAN;
9241 
9242 	ar->hw->wiphy->max_ap_assoc_sta = ar->max_num_stations;
9243 
9244 	ret = ath10k_wow_init(ar);
9245 	if (ret) {
9246 		ath10k_warn(ar, "failed to init wow: %d\n", ret);
9247 		goto err_free;
9248 	}
9249 
9250 	wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
9251 	wiphy_ext_feature_set(ar->hw->wiphy,
9252 			      NL80211_EXT_FEATURE_SET_SCAN_DWELL);
9253 	wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_AQL);
9254 
9255 	if (test_bit(WMI_SERVICE_TX_DATA_ACK_RSSI, ar->wmi.svc_map) ||
9256 	    test_bit(WMI_SERVICE_HTT_MGMT_TX_COMP_VALID_FLAGS, ar->wmi.svc_map))
9257 		wiphy_ext_feature_set(ar->hw->wiphy,
9258 				      NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT);
9259 
9260 	if (ath10k_peer_stats_enabled(ar) ||
9261 	    test_bit(WMI_SERVICE_REPORT_AIRTIME, ar->wmi.svc_map))
9262 		wiphy_ext_feature_set(ar->hw->wiphy,
9263 				      NL80211_EXT_FEATURE_AIRTIME_FAIRNESS);
9264 
9265 	if (test_bit(WMI_SERVICE_RTT_RESPONDER_ROLE, ar->wmi.svc_map))
9266 		wiphy_ext_feature_set(ar->hw->wiphy,
9267 				      NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER);
9268 
9269 	if (test_bit(WMI_SERVICE_TX_PWR_PER_PEER, ar->wmi.svc_map))
9270 		wiphy_ext_feature_set(ar->hw->wiphy,
9271 				      NL80211_EXT_FEATURE_STA_TX_PWR);
9272 	/*
9273 	 * on LL hardware queues are managed entirely by the FW
9274 	 * so we only advertise to mac we can do the queues thing
9275 	 */
9276 	ar->hw->queues = IEEE80211_MAX_QUEUES;
9277 
9278 	/* vdev_ids are used as hw queue numbers. Make sure offchan tx queue is
9279 	 * something that vdev_ids can't reach so that we don't stop the queue
9280 	 * accidentally.
9281 	 */
9282 	ar->hw->offchannel_tx_hw_queue = IEEE80211_MAX_QUEUES - 1;
9283 
9284 	switch (ar->running_fw->fw_file.wmi_op_version) {
9285 	case ATH10K_FW_WMI_OP_VERSION_MAIN:
9286 		ar->hw->wiphy->iface_combinations = ath10k_if_comb;
9287 		ar->hw->wiphy->n_iface_combinations =
9288 			ARRAY_SIZE(ath10k_if_comb);
9289 		ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
9290 		break;
9291 	case ATH10K_FW_WMI_OP_VERSION_TLV:
9292 		if (test_bit(WMI_SERVICE_ADAPTIVE_OCS, ar->wmi.svc_map)) {
9293 			ar->hw->wiphy->iface_combinations =
9294 				ath10k_tlv_qcs_if_comb;
9295 			ar->hw->wiphy->n_iface_combinations =
9296 				ARRAY_SIZE(ath10k_tlv_qcs_if_comb);
9297 		} else {
9298 			ar->hw->wiphy->iface_combinations = ath10k_tlv_if_comb;
9299 			ar->hw->wiphy->n_iface_combinations =
9300 				ARRAY_SIZE(ath10k_tlv_if_comb);
9301 		}
9302 		ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
9303 		break;
9304 	case ATH10K_FW_WMI_OP_VERSION_10_1:
9305 	case ATH10K_FW_WMI_OP_VERSION_10_2:
9306 	case ATH10K_FW_WMI_OP_VERSION_10_2_4:
9307 		ar->hw->wiphy->iface_combinations = ath10k_10x_if_comb;
9308 		ar->hw->wiphy->n_iface_combinations =
9309 			ARRAY_SIZE(ath10k_10x_if_comb);
9310 		break;
9311 	case ATH10K_FW_WMI_OP_VERSION_10_4:
9312 		ar->hw->wiphy->iface_combinations = ath10k_10_4_if_comb;
9313 		ar->hw->wiphy->n_iface_combinations =
9314 			ARRAY_SIZE(ath10k_10_4_if_comb);
9315 		if (test_bit(WMI_SERVICE_VDEV_DIFFERENT_BEACON_INTERVAL_SUPPORT,
9316 			     ar->wmi.svc_map)) {
9317 			ar->hw->wiphy->iface_combinations =
9318 				ath10k_10_4_bcn_int_if_comb;
9319 			ar->hw->wiphy->n_iface_combinations =
9320 				ARRAY_SIZE(ath10k_10_4_bcn_int_if_comb);
9321 		}
9322 		break;
9323 	case ATH10K_FW_WMI_OP_VERSION_UNSET:
9324 	case ATH10K_FW_WMI_OP_VERSION_MAX:
9325 		WARN_ON(1);
9326 		ret = -EINVAL;
9327 		goto err_free;
9328 	}
9329 
9330 	if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
9331 		ar->hw->netdev_features = NETIF_F_HW_CSUM;
9332 
9333 	if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED)) {
9334 		/* Init ath dfs pattern detector */
9335 		ar->ath_common.debug_mask = ATH_DBG_DFS;
9336 		ar->dfs_detector = dfs_pattern_detector_init(&ar->ath_common,
9337 							     NL80211_DFS_UNSET);
9338 
9339 		if (!ar->dfs_detector)
9340 			ath10k_warn(ar, "failed to initialise DFS pattern detector\n");
9341 	}
9342 
9343 	ret = ath10k_mac_init_rd(ar);
9344 	if (ret) {
9345 		ath10k_err(ar, "failed to derive regdom: %d\n", ret);
9346 		goto err_dfs_detector_exit;
9347 	}
9348 
9349 	/* Disable set_coverage_class for chipsets that do not support it. */
9350 	if (!ar->hw_params.hw_ops->set_coverage_class)
9351 		ar->ops->set_coverage_class = NULL;
9352 
9353 	ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
9354 			    ath10k_reg_notifier);
9355 	if (ret) {
9356 		ath10k_err(ar, "failed to initialise regulatory: %i\n", ret);
9357 		goto err_dfs_detector_exit;
9358 	}
9359 
9360 	if (test_bit(WMI_SERVICE_SPOOF_MAC_SUPPORT, ar->wmi.svc_map)) {
9361 		ar->hw->wiphy->features |=
9362 			NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
9363 	}
9364 
9365 	ar->hw->wiphy->cipher_suites = cipher_suites;
9366 
9367 	/* QCA988x and QCA6174 family chips do not support CCMP-256, GCMP-128
9368 	 * and GCMP-256 ciphers in hardware. Fetch number of ciphers supported
9369 	 * from chip specific hw_param table.
9370 	 */
9371 	if (!ar->hw_params.n_cipher_suites ||
9372 	    ar->hw_params.n_cipher_suites > ARRAY_SIZE(cipher_suites)) {
9373 		ath10k_err(ar, "invalid hw_params.n_cipher_suites %d\n",
9374 			   ar->hw_params.n_cipher_suites);
9375 		ar->hw_params.n_cipher_suites = 8;
9376 	}
9377 	ar->hw->wiphy->n_cipher_suites = ar->hw_params.n_cipher_suites;
9378 
9379 	wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
9380 
9381 	ar->hw->weight_multiplier = ATH10K_AIRTIME_WEIGHT_MULTIPLIER;
9382 
9383 	ret = ieee80211_register_hw(ar->hw);
9384 	if (ret) {
9385 		ath10k_err(ar, "failed to register ieee80211: %d\n", ret);
9386 		goto err_dfs_detector_exit;
9387 	}
9388 
9389 	if (test_bit(WMI_SERVICE_PER_PACKET_SW_ENCRYPT, ar->wmi.svc_map)) {
9390 		ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP_VLAN);
9391 		ar->hw->wiphy->software_iftypes |= BIT(NL80211_IFTYPE_AP_VLAN);
9392 	}
9393 
9394 	if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
9395 		ret = regulatory_hint(ar->hw->wiphy,
9396 				      ar->ath_common.regulatory.alpha2);
9397 		if (ret)
9398 			goto err_unregister;
9399 	}
9400 
9401 	return 0;
9402 
9403 err_unregister:
9404 	ieee80211_unregister_hw(ar->hw);
9405 
9406 err_dfs_detector_exit:
9407 	if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
9408 		ar->dfs_detector->exit(ar->dfs_detector);
9409 
9410 err_free:
9411 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
9412 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
9413 
9414 	SET_IEEE80211_DEV(ar->hw, NULL);
9415 	return ret;
9416 }
9417 
9418 void ath10k_mac_unregister(struct ath10k *ar)
9419 {
9420 	ieee80211_unregister_hw(ar->hw);
9421 
9422 	if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
9423 		ar->dfs_detector->exit(ar->dfs_detector);
9424 
9425 	kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
9426 	kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
9427 
9428 	SET_IEEE80211_DEV(ar->hw, NULL);
9429 }
9430