xref: /openbmc/linux/drivers/net/wireless/ath/ath10k/mac.c (revision d5e7cafd)
1 /*
2  * Copyright (c) 2005-2011 Atheros Communications Inc.
3  * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include "mac.h"
19 
20 #include <net/mac80211.h>
21 #include <linux/etherdevice.h>
22 
23 #include "hif.h"
24 #include "core.h"
25 #include "debug.h"
26 #include "wmi.h"
27 #include "htt.h"
28 #include "txrx.h"
29 #include "testmode.h"
30 #include "wmi.h"
31 #include "wmi-ops.h"
32 
33 /**********/
34 /* Crypto */
35 /**********/
36 
37 static int ath10k_send_key(struct ath10k_vif *arvif,
38 			   struct ieee80211_key_conf *key,
39 			   enum set_key_cmd cmd,
40 			   const u8 *macaddr, bool def_idx)
41 {
42 	struct ath10k *ar = arvif->ar;
43 	struct wmi_vdev_install_key_arg arg = {
44 		.vdev_id = arvif->vdev_id,
45 		.key_idx = key->keyidx,
46 		.key_len = key->keylen,
47 		.key_data = key->key,
48 		.macaddr = macaddr,
49 	};
50 
51 	lockdep_assert_held(&arvif->ar->conf_mutex);
52 
53 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
54 		arg.key_flags = WMI_KEY_PAIRWISE;
55 	else
56 		arg.key_flags = WMI_KEY_GROUP;
57 
58 	switch (key->cipher) {
59 	case WLAN_CIPHER_SUITE_CCMP:
60 		arg.key_cipher = WMI_CIPHER_AES_CCM;
61 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
62 		break;
63 	case WLAN_CIPHER_SUITE_TKIP:
64 		arg.key_cipher = WMI_CIPHER_TKIP;
65 		arg.key_txmic_len = 8;
66 		arg.key_rxmic_len = 8;
67 		break;
68 	case WLAN_CIPHER_SUITE_WEP40:
69 	case WLAN_CIPHER_SUITE_WEP104:
70 		arg.key_cipher = WMI_CIPHER_WEP;
71 		/* AP/IBSS mode requires self-key to be groupwise
72 		 * Otherwise pairwise key must be set */
73 		if (memcmp(macaddr, arvif->vif->addr, ETH_ALEN))
74 			arg.key_flags = WMI_KEY_PAIRWISE;
75 
76 		if (def_idx)
77 			arg.key_flags |= WMI_KEY_TX_USAGE;
78 		break;
79 	case WLAN_CIPHER_SUITE_AES_CMAC:
80 		/* this one needs to be done in software */
81 		return 1;
82 	default:
83 		ath10k_warn(ar, "cipher %d is not supported\n", key->cipher);
84 		return -EOPNOTSUPP;
85 	}
86 
87 	if (cmd == DISABLE_KEY) {
88 		arg.key_cipher = WMI_CIPHER_NONE;
89 		arg.key_data = NULL;
90 	}
91 
92 	return ath10k_wmi_vdev_install_key(arvif->ar, &arg);
93 }
94 
95 static int ath10k_install_key(struct ath10k_vif *arvif,
96 			      struct ieee80211_key_conf *key,
97 			      enum set_key_cmd cmd,
98 			      const u8 *macaddr, bool def_idx)
99 {
100 	struct ath10k *ar = arvif->ar;
101 	int ret;
102 
103 	lockdep_assert_held(&ar->conf_mutex);
104 
105 	reinit_completion(&ar->install_key_done);
106 
107 	ret = ath10k_send_key(arvif, key, cmd, macaddr, def_idx);
108 	if (ret)
109 		return ret;
110 
111 	ret = wait_for_completion_timeout(&ar->install_key_done, 3*HZ);
112 	if (ret == 0)
113 		return -ETIMEDOUT;
114 
115 	return 0;
116 }
117 
118 static int ath10k_install_peer_wep_keys(struct ath10k_vif *arvif,
119 					const u8 *addr)
120 {
121 	struct ath10k *ar = arvif->ar;
122 	struct ath10k_peer *peer;
123 	int ret;
124 	int i;
125 	bool def_idx;
126 
127 	lockdep_assert_held(&ar->conf_mutex);
128 
129 	spin_lock_bh(&ar->data_lock);
130 	peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
131 	spin_unlock_bh(&ar->data_lock);
132 
133 	if (!peer)
134 		return -ENOENT;
135 
136 	for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
137 		if (arvif->wep_keys[i] == NULL)
138 			continue;
139 		/* set TX_USAGE flag for default key id */
140 		if (arvif->def_wep_key_idx == i)
141 			def_idx = true;
142 		else
143 			def_idx = false;
144 
145 		ret = ath10k_install_key(arvif, arvif->wep_keys[i], SET_KEY,
146 					 addr, def_idx);
147 		if (ret)
148 			return ret;
149 
150 		spin_lock_bh(&ar->data_lock);
151 		peer->keys[i] = arvif->wep_keys[i];
152 		spin_unlock_bh(&ar->data_lock);
153 	}
154 
155 	return 0;
156 }
157 
158 static int ath10k_clear_peer_keys(struct ath10k_vif *arvif,
159 				  const u8 *addr)
160 {
161 	struct ath10k *ar = arvif->ar;
162 	struct ath10k_peer *peer;
163 	int first_errno = 0;
164 	int ret;
165 	int i;
166 
167 	lockdep_assert_held(&ar->conf_mutex);
168 
169 	spin_lock_bh(&ar->data_lock);
170 	peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
171 	spin_unlock_bh(&ar->data_lock);
172 
173 	if (!peer)
174 		return -ENOENT;
175 
176 	for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
177 		if (peer->keys[i] == NULL)
178 			continue;
179 
180 		/* key flags are not required to delete the key */
181 		ret = ath10k_install_key(arvif, peer->keys[i],
182 					 DISABLE_KEY, addr, false);
183 		if (ret && first_errno == 0)
184 			first_errno = ret;
185 
186 		if (ret)
187 			ath10k_warn(ar, "failed to remove peer wep key %d: %d\n",
188 				    i, ret);
189 
190 		spin_lock_bh(&ar->data_lock);
191 		peer->keys[i] = NULL;
192 		spin_unlock_bh(&ar->data_lock);
193 	}
194 
195 	return first_errno;
196 }
197 
198 bool ath10k_mac_is_peer_wep_key_set(struct ath10k *ar, const u8 *addr,
199 				    u8 keyidx)
200 {
201 	struct ath10k_peer *peer;
202 	int i;
203 
204 	lockdep_assert_held(&ar->data_lock);
205 
206 	/* We don't know which vdev this peer belongs to,
207 	 * since WMI doesn't give us that information.
208 	 *
209 	 * FIXME: multi-bss needs to be handled.
210 	 */
211 	peer = ath10k_peer_find(ar, 0, addr);
212 	if (!peer)
213 		return false;
214 
215 	for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
216 		if (peer->keys[i] && peer->keys[i]->keyidx == keyidx)
217 			return true;
218 	}
219 
220 	return false;
221 }
222 
223 static int ath10k_clear_vdev_key(struct ath10k_vif *arvif,
224 				 struct ieee80211_key_conf *key)
225 {
226 	struct ath10k *ar = arvif->ar;
227 	struct ath10k_peer *peer;
228 	u8 addr[ETH_ALEN];
229 	int first_errno = 0;
230 	int ret;
231 	int i;
232 
233 	lockdep_assert_held(&ar->conf_mutex);
234 
235 	for (;;) {
236 		/* since ath10k_install_key we can't hold data_lock all the
237 		 * time, so we try to remove the keys incrementally */
238 		spin_lock_bh(&ar->data_lock);
239 		i = 0;
240 		list_for_each_entry(peer, &ar->peers, list) {
241 			for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
242 				if (peer->keys[i] == key) {
243 					ether_addr_copy(addr, peer->addr);
244 					peer->keys[i] = NULL;
245 					break;
246 				}
247 			}
248 
249 			if (i < ARRAY_SIZE(peer->keys))
250 				break;
251 		}
252 		spin_unlock_bh(&ar->data_lock);
253 
254 		if (i == ARRAY_SIZE(peer->keys))
255 			break;
256 		/* key flags are not required to delete the key */
257 		ret = ath10k_install_key(arvif, key, DISABLE_KEY, addr, false);
258 		if (ret && first_errno == 0)
259 			first_errno = ret;
260 
261 		if (ret)
262 			ath10k_warn(ar, "failed to remove key for %pM: %d\n",
263 				    addr, ret);
264 	}
265 
266 	return first_errno;
267 }
268 
269 /*********************/
270 /* General utilities */
271 /*********************/
272 
273 static inline enum wmi_phy_mode
274 chan_to_phymode(const struct cfg80211_chan_def *chandef)
275 {
276 	enum wmi_phy_mode phymode = MODE_UNKNOWN;
277 
278 	switch (chandef->chan->band) {
279 	case IEEE80211_BAND_2GHZ:
280 		switch (chandef->width) {
281 		case NL80211_CHAN_WIDTH_20_NOHT:
282 			if (chandef->chan->flags & IEEE80211_CHAN_NO_OFDM)
283 				phymode = MODE_11B;
284 			else
285 				phymode = MODE_11G;
286 			break;
287 		case NL80211_CHAN_WIDTH_20:
288 			phymode = MODE_11NG_HT20;
289 			break;
290 		case NL80211_CHAN_WIDTH_40:
291 			phymode = MODE_11NG_HT40;
292 			break;
293 		case NL80211_CHAN_WIDTH_5:
294 		case NL80211_CHAN_WIDTH_10:
295 		case NL80211_CHAN_WIDTH_80:
296 		case NL80211_CHAN_WIDTH_80P80:
297 		case NL80211_CHAN_WIDTH_160:
298 			phymode = MODE_UNKNOWN;
299 			break;
300 		}
301 		break;
302 	case IEEE80211_BAND_5GHZ:
303 		switch (chandef->width) {
304 		case NL80211_CHAN_WIDTH_20_NOHT:
305 			phymode = MODE_11A;
306 			break;
307 		case NL80211_CHAN_WIDTH_20:
308 			phymode = MODE_11NA_HT20;
309 			break;
310 		case NL80211_CHAN_WIDTH_40:
311 			phymode = MODE_11NA_HT40;
312 			break;
313 		case NL80211_CHAN_WIDTH_80:
314 			phymode = MODE_11AC_VHT80;
315 			break;
316 		case NL80211_CHAN_WIDTH_5:
317 		case NL80211_CHAN_WIDTH_10:
318 		case NL80211_CHAN_WIDTH_80P80:
319 		case NL80211_CHAN_WIDTH_160:
320 			phymode = MODE_UNKNOWN;
321 			break;
322 		}
323 		break;
324 	default:
325 		break;
326 	}
327 
328 	WARN_ON(phymode == MODE_UNKNOWN);
329 	return phymode;
330 }
331 
332 static u8 ath10k_parse_mpdudensity(u8 mpdudensity)
333 {
334 /*
335  * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
336  *   0 for no restriction
337  *   1 for 1/4 us
338  *   2 for 1/2 us
339  *   3 for 1 us
340  *   4 for 2 us
341  *   5 for 4 us
342  *   6 for 8 us
343  *   7 for 16 us
344  */
345 	switch (mpdudensity) {
346 	case 0:
347 		return 0;
348 	case 1:
349 	case 2:
350 	case 3:
351 	/* Our lower layer calculations limit our precision to
352 	   1 microsecond */
353 		return 1;
354 	case 4:
355 		return 2;
356 	case 5:
357 		return 4;
358 	case 6:
359 		return 8;
360 	case 7:
361 		return 16;
362 	default:
363 		return 0;
364 	}
365 }
366 
367 static int ath10k_peer_create(struct ath10k *ar, u32 vdev_id, const u8 *addr)
368 {
369 	int ret;
370 
371 	lockdep_assert_held(&ar->conf_mutex);
372 
373 	if (ar->num_peers >= ar->max_num_peers)
374 		return -ENOBUFS;
375 
376 	ret = ath10k_wmi_peer_create(ar, vdev_id, addr);
377 	if (ret) {
378 		ath10k_warn(ar, "failed to create wmi peer %pM on vdev %i: %i\n",
379 			    addr, vdev_id, ret);
380 		return ret;
381 	}
382 
383 	ret = ath10k_wait_for_peer_created(ar, vdev_id, addr);
384 	if (ret) {
385 		ath10k_warn(ar, "failed to wait for created wmi peer %pM on vdev %i: %i\n",
386 			    addr, vdev_id, ret);
387 		return ret;
388 	}
389 
390 	ar->num_peers++;
391 
392 	return 0;
393 }
394 
395 static int ath10k_mac_set_kickout(struct ath10k_vif *arvif)
396 {
397 	struct ath10k *ar = arvif->ar;
398 	u32 param;
399 	int ret;
400 
401 	param = ar->wmi.pdev_param->sta_kickout_th;
402 	ret = ath10k_wmi_pdev_set_param(ar, param,
403 					ATH10K_KICKOUT_THRESHOLD);
404 	if (ret) {
405 		ath10k_warn(ar, "failed to set kickout threshold on vdev %i: %d\n",
406 			    arvif->vdev_id, ret);
407 		return ret;
408 	}
409 
410 	param = ar->wmi.vdev_param->ap_keepalive_min_idle_inactive_time_secs;
411 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
412 					ATH10K_KEEPALIVE_MIN_IDLE);
413 	if (ret) {
414 		ath10k_warn(ar, "failed to set keepalive minimum idle time on vdev %i: %d\n",
415 			    arvif->vdev_id, ret);
416 		return ret;
417 	}
418 
419 	param = ar->wmi.vdev_param->ap_keepalive_max_idle_inactive_time_secs;
420 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
421 					ATH10K_KEEPALIVE_MAX_IDLE);
422 	if (ret) {
423 		ath10k_warn(ar, "failed to set keepalive maximum idle time on vdev %i: %d\n",
424 			    arvif->vdev_id, ret);
425 		return ret;
426 	}
427 
428 	param = ar->wmi.vdev_param->ap_keepalive_max_unresponsive_time_secs;
429 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
430 					ATH10K_KEEPALIVE_MAX_UNRESPONSIVE);
431 	if (ret) {
432 		ath10k_warn(ar, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
433 			    arvif->vdev_id, ret);
434 		return ret;
435 	}
436 
437 	return 0;
438 }
439 
440 static int ath10k_mac_set_rts(struct ath10k_vif *arvif, u32 value)
441 {
442 	struct ath10k *ar = arvif->ar;
443 	u32 vdev_param;
444 
445 	vdev_param = ar->wmi.vdev_param->rts_threshold;
446 	return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
447 }
448 
449 static int ath10k_mac_set_frag(struct ath10k_vif *arvif, u32 value)
450 {
451 	struct ath10k *ar = arvif->ar;
452 	u32 vdev_param;
453 
454 	if (value != 0xFFFFFFFF)
455 		value = clamp_t(u32, arvif->ar->hw->wiphy->frag_threshold,
456 				ATH10K_FRAGMT_THRESHOLD_MIN,
457 				ATH10K_FRAGMT_THRESHOLD_MAX);
458 
459 	vdev_param = ar->wmi.vdev_param->fragmentation_threshold;
460 	return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
461 }
462 
463 static int ath10k_peer_delete(struct ath10k *ar, u32 vdev_id, const u8 *addr)
464 {
465 	int ret;
466 
467 	lockdep_assert_held(&ar->conf_mutex);
468 
469 	ret = ath10k_wmi_peer_delete(ar, vdev_id, addr);
470 	if (ret)
471 		return ret;
472 
473 	ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
474 	if (ret)
475 		return ret;
476 
477 	ar->num_peers--;
478 
479 	return 0;
480 }
481 
482 static void ath10k_peer_cleanup(struct ath10k *ar, u32 vdev_id)
483 {
484 	struct ath10k_peer *peer, *tmp;
485 
486 	lockdep_assert_held(&ar->conf_mutex);
487 
488 	spin_lock_bh(&ar->data_lock);
489 	list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
490 		if (peer->vdev_id != vdev_id)
491 			continue;
492 
493 		ath10k_warn(ar, "removing stale peer %pM from vdev_id %d\n",
494 			    peer->addr, vdev_id);
495 
496 		list_del(&peer->list);
497 		kfree(peer);
498 		ar->num_peers--;
499 	}
500 	spin_unlock_bh(&ar->data_lock);
501 }
502 
503 static void ath10k_peer_cleanup_all(struct ath10k *ar)
504 {
505 	struct ath10k_peer *peer, *tmp;
506 
507 	lockdep_assert_held(&ar->conf_mutex);
508 
509 	spin_lock_bh(&ar->data_lock);
510 	list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
511 		list_del(&peer->list);
512 		kfree(peer);
513 	}
514 	spin_unlock_bh(&ar->data_lock);
515 
516 	ar->num_peers = 0;
517 	ar->num_stations = 0;
518 }
519 
520 /************************/
521 /* Interface management */
522 /************************/
523 
524 void ath10k_mac_vif_beacon_free(struct ath10k_vif *arvif)
525 {
526 	struct ath10k *ar = arvif->ar;
527 
528 	lockdep_assert_held(&ar->data_lock);
529 
530 	if (!arvif->beacon)
531 		return;
532 
533 	if (!arvif->beacon_buf)
534 		dma_unmap_single(ar->dev, ATH10K_SKB_CB(arvif->beacon)->paddr,
535 				 arvif->beacon->len, DMA_TO_DEVICE);
536 
537 	if (WARN_ON(arvif->beacon_state != ATH10K_BEACON_SCHEDULED &&
538 		    arvif->beacon_state != ATH10K_BEACON_SENT))
539 		return;
540 
541 	dev_kfree_skb_any(arvif->beacon);
542 
543 	arvif->beacon = NULL;
544 	arvif->beacon_state = ATH10K_BEACON_SCHEDULED;
545 }
546 
547 static void ath10k_mac_vif_beacon_cleanup(struct ath10k_vif *arvif)
548 {
549 	struct ath10k *ar = arvif->ar;
550 
551 	lockdep_assert_held(&ar->data_lock);
552 
553 	ath10k_mac_vif_beacon_free(arvif);
554 
555 	if (arvif->beacon_buf) {
556 		dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
557 				  arvif->beacon_buf, arvif->beacon_paddr);
558 		arvif->beacon_buf = NULL;
559 	}
560 }
561 
562 static inline int ath10k_vdev_setup_sync(struct ath10k *ar)
563 {
564 	int ret;
565 
566 	lockdep_assert_held(&ar->conf_mutex);
567 
568 	if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags))
569 		return -ESHUTDOWN;
570 
571 	ret = wait_for_completion_timeout(&ar->vdev_setup_done,
572 					  ATH10K_VDEV_SETUP_TIMEOUT_HZ);
573 	if (ret == 0)
574 		return -ETIMEDOUT;
575 
576 	return 0;
577 }
578 
579 static int ath10k_monitor_vdev_start(struct ath10k *ar, int vdev_id)
580 {
581 	struct cfg80211_chan_def *chandef = &ar->chandef;
582 	struct ieee80211_channel *channel = chandef->chan;
583 	struct wmi_vdev_start_request_arg arg = {};
584 	int ret = 0;
585 
586 	lockdep_assert_held(&ar->conf_mutex);
587 
588 	arg.vdev_id = vdev_id;
589 	arg.channel.freq = channel->center_freq;
590 	arg.channel.band_center_freq1 = chandef->center_freq1;
591 
592 	/* TODO setup this dynamically, what in case we
593 	   don't have any vifs? */
594 	arg.channel.mode = chan_to_phymode(chandef);
595 	arg.channel.chan_radar =
596 			!!(channel->flags & IEEE80211_CHAN_RADAR);
597 
598 	arg.channel.min_power = 0;
599 	arg.channel.max_power = channel->max_power * 2;
600 	arg.channel.max_reg_power = channel->max_reg_power * 2;
601 	arg.channel.max_antenna_gain = channel->max_antenna_gain * 2;
602 
603 	reinit_completion(&ar->vdev_setup_done);
604 
605 	ret = ath10k_wmi_vdev_start(ar, &arg);
606 	if (ret) {
607 		ath10k_warn(ar, "failed to request monitor vdev %i start: %d\n",
608 			    vdev_id, ret);
609 		return ret;
610 	}
611 
612 	ret = ath10k_vdev_setup_sync(ar);
613 	if (ret) {
614 		ath10k_warn(ar, "failed to synchronize setup for monitor vdev %i: %d\n",
615 			    vdev_id, ret);
616 		return ret;
617 	}
618 
619 	ret = ath10k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
620 	if (ret) {
621 		ath10k_warn(ar, "failed to put up monitor vdev %i: %d\n",
622 			    vdev_id, ret);
623 		goto vdev_stop;
624 	}
625 
626 	ar->monitor_vdev_id = vdev_id;
627 
628 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i started\n",
629 		   ar->monitor_vdev_id);
630 	return 0;
631 
632 vdev_stop:
633 	ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
634 	if (ret)
635 		ath10k_warn(ar, "failed to stop monitor vdev %i after start failure: %d\n",
636 			    ar->monitor_vdev_id, ret);
637 
638 	return ret;
639 }
640 
641 static int ath10k_monitor_vdev_stop(struct ath10k *ar)
642 {
643 	int ret = 0;
644 
645 	lockdep_assert_held(&ar->conf_mutex);
646 
647 	ret = ath10k_wmi_vdev_down(ar, ar->monitor_vdev_id);
648 	if (ret)
649 		ath10k_warn(ar, "failed to put down monitor vdev %i: %d\n",
650 			    ar->monitor_vdev_id, ret);
651 
652 	reinit_completion(&ar->vdev_setup_done);
653 
654 	ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
655 	if (ret)
656 		ath10k_warn(ar, "failed to to request monitor vdev %i stop: %d\n",
657 			    ar->monitor_vdev_id, ret);
658 
659 	ret = ath10k_vdev_setup_sync(ar);
660 	if (ret)
661 		ath10k_warn(ar, "failed to synchronise monitor vdev %i: %d\n",
662 			    ar->monitor_vdev_id, ret);
663 
664 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i stopped\n",
665 		   ar->monitor_vdev_id);
666 	return ret;
667 }
668 
669 static int ath10k_monitor_vdev_create(struct ath10k *ar)
670 {
671 	int bit, ret = 0;
672 
673 	lockdep_assert_held(&ar->conf_mutex);
674 
675 	if (ar->free_vdev_map == 0) {
676 		ath10k_warn(ar, "failed to find free vdev id for monitor vdev\n");
677 		return -ENOMEM;
678 	}
679 
680 	bit = __ffs64(ar->free_vdev_map);
681 
682 	ar->monitor_vdev_id = bit;
683 
684 	ret = ath10k_wmi_vdev_create(ar, ar->monitor_vdev_id,
685 				     WMI_VDEV_TYPE_MONITOR,
686 				     0, ar->mac_addr);
687 	if (ret) {
688 		ath10k_warn(ar, "failed to request monitor vdev %i creation: %d\n",
689 			    ar->monitor_vdev_id, ret);
690 		return ret;
691 	}
692 
693 	ar->free_vdev_map &= ~(1LL << ar->monitor_vdev_id);
694 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d created\n",
695 		   ar->monitor_vdev_id);
696 
697 	return 0;
698 }
699 
700 static int ath10k_monitor_vdev_delete(struct ath10k *ar)
701 {
702 	int ret = 0;
703 
704 	lockdep_assert_held(&ar->conf_mutex);
705 
706 	ret = ath10k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
707 	if (ret) {
708 		ath10k_warn(ar, "failed to request wmi monitor vdev %i removal: %d\n",
709 			    ar->monitor_vdev_id, ret);
710 		return ret;
711 	}
712 
713 	ar->free_vdev_map |= 1LL << ar->monitor_vdev_id;
714 
715 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d deleted\n",
716 		   ar->monitor_vdev_id);
717 	return ret;
718 }
719 
720 static int ath10k_monitor_start(struct ath10k *ar)
721 {
722 	int ret;
723 
724 	lockdep_assert_held(&ar->conf_mutex);
725 
726 	ret = ath10k_monitor_vdev_create(ar);
727 	if (ret) {
728 		ath10k_warn(ar, "failed to create monitor vdev: %d\n", ret);
729 		return ret;
730 	}
731 
732 	ret = ath10k_monitor_vdev_start(ar, ar->monitor_vdev_id);
733 	if (ret) {
734 		ath10k_warn(ar, "failed to start monitor vdev: %d\n", ret);
735 		ath10k_monitor_vdev_delete(ar);
736 		return ret;
737 	}
738 
739 	ar->monitor_started = true;
740 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor started\n");
741 
742 	return 0;
743 }
744 
745 static int ath10k_monitor_stop(struct ath10k *ar)
746 {
747 	int ret;
748 
749 	lockdep_assert_held(&ar->conf_mutex);
750 
751 	ret = ath10k_monitor_vdev_stop(ar);
752 	if (ret) {
753 		ath10k_warn(ar, "failed to stop monitor vdev: %d\n", ret);
754 		return ret;
755 	}
756 
757 	ret = ath10k_monitor_vdev_delete(ar);
758 	if (ret) {
759 		ath10k_warn(ar, "failed to delete monitor vdev: %d\n", ret);
760 		return ret;
761 	}
762 
763 	ar->monitor_started = false;
764 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopped\n");
765 
766 	return 0;
767 }
768 
769 static int ath10k_monitor_recalc(struct ath10k *ar)
770 {
771 	bool should_start;
772 
773 	lockdep_assert_held(&ar->conf_mutex);
774 
775 	should_start = ar->monitor ||
776 		       ar->filter_flags & FIF_PROMISC_IN_BSS ||
777 		       test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
778 
779 	ath10k_dbg(ar, ATH10K_DBG_MAC,
780 		   "mac monitor recalc started? %d should? %d\n",
781 		   ar->monitor_started, should_start);
782 
783 	if (should_start == ar->monitor_started)
784 		return 0;
785 
786 	if (should_start)
787 		return ath10k_monitor_start(ar);
788 
789 	return ath10k_monitor_stop(ar);
790 }
791 
792 static int ath10k_recalc_rtscts_prot(struct ath10k_vif *arvif)
793 {
794 	struct ath10k *ar = arvif->ar;
795 	u32 vdev_param, rts_cts = 0;
796 
797 	lockdep_assert_held(&ar->conf_mutex);
798 
799 	vdev_param = ar->wmi.vdev_param->enable_rtscts;
800 
801 	if (arvif->use_cts_prot || arvif->num_legacy_stations > 0)
802 		rts_cts |= SM(WMI_RTSCTS_ENABLED, WMI_RTSCTS_SET);
803 
804 	if (arvif->num_legacy_stations > 0)
805 		rts_cts |= SM(WMI_RTSCTS_ACROSS_SW_RETRIES,
806 			      WMI_RTSCTS_PROFILE);
807 
808 	return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
809 					 rts_cts);
810 }
811 
812 static int ath10k_start_cac(struct ath10k *ar)
813 {
814 	int ret;
815 
816 	lockdep_assert_held(&ar->conf_mutex);
817 
818 	set_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
819 
820 	ret = ath10k_monitor_recalc(ar);
821 	if (ret) {
822 		ath10k_warn(ar, "failed to start monitor (cac): %d\n", ret);
823 		clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
824 		return ret;
825 	}
826 
827 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac start monitor vdev %d\n",
828 		   ar->monitor_vdev_id);
829 
830 	return 0;
831 }
832 
833 static int ath10k_stop_cac(struct ath10k *ar)
834 {
835 	lockdep_assert_held(&ar->conf_mutex);
836 
837 	/* CAC is not running - do nothing */
838 	if (!test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags))
839 		return 0;
840 
841 	clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
842 	ath10k_monitor_stop(ar);
843 
844 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac finished\n");
845 
846 	return 0;
847 }
848 
849 static void ath10k_recalc_radar_detection(struct ath10k *ar)
850 {
851 	int ret;
852 
853 	lockdep_assert_held(&ar->conf_mutex);
854 
855 	ath10k_stop_cac(ar);
856 
857 	if (!ar->radar_enabled)
858 		return;
859 
860 	if (ar->num_started_vdevs > 0)
861 		return;
862 
863 	ret = ath10k_start_cac(ar);
864 	if (ret) {
865 		/*
866 		 * Not possible to start CAC on current channel so starting
867 		 * radiation is not allowed, make this channel DFS_UNAVAILABLE
868 		 * by indicating that radar was detected.
869 		 */
870 		ath10k_warn(ar, "failed to start CAC: %d\n", ret);
871 		ieee80211_radar_detected(ar->hw);
872 	}
873 }
874 
875 static int ath10k_vdev_start_restart(struct ath10k_vif *arvif, bool restart)
876 {
877 	struct ath10k *ar = arvif->ar;
878 	struct cfg80211_chan_def *chandef = &ar->chandef;
879 	struct wmi_vdev_start_request_arg arg = {};
880 	int ret = 0;
881 
882 	lockdep_assert_held(&ar->conf_mutex);
883 
884 	reinit_completion(&ar->vdev_setup_done);
885 
886 	arg.vdev_id = arvif->vdev_id;
887 	arg.dtim_period = arvif->dtim_period;
888 	arg.bcn_intval = arvif->beacon_interval;
889 
890 	arg.channel.freq = chandef->chan->center_freq;
891 	arg.channel.band_center_freq1 = chandef->center_freq1;
892 	arg.channel.mode = chan_to_phymode(chandef);
893 
894 	arg.channel.min_power = 0;
895 	arg.channel.max_power = chandef->chan->max_power * 2;
896 	arg.channel.max_reg_power = chandef->chan->max_reg_power * 2;
897 	arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
898 
899 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
900 		arg.ssid = arvif->u.ap.ssid;
901 		arg.ssid_len = arvif->u.ap.ssid_len;
902 		arg.hidden_ssid = arvif->u.ap.hidden_ssid;
903 
904 		/* For now allow DFS for AP mode */
905 		arg.channel.chan_radar =
906 			!!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
907 	} else if (arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
908 		arg.ssid = arvif->vif->bss_conf.ssid;
909 		arg.ssid_len = arvif->vif->bss_conf.ssid_len;
910 	}
911 
912 	ath10k_dbg(ar, ATH10K_DBG_MAC,
913 		   "mac vdev %d start center_freq %d phymode %s\n",
914 		   arg.vdev_id, arg.channel.freq,
915 		   ath10k_wmi_phymode_str(arg.channel.mode));
916 
917 	if (restart)
918 		ret = ath10k_wmi_vdev_restart(ar, &arg);
919 	else
920 		ret = ath10k_wmi_vdev_start(ar, &arg);
921 
922 	if (ret) {
923 		ath10k_warn(ar, "failed to start WMI vdev %i: %d\n",
924 			    arg.vdev_id, ret);
925 		return ret;
926 	}
927 
928 	ret = ath10k_vdev_setup_sync(ar);
929 	if (ret) {
930 		ath10k_warn(ar, "failed to synchronise setup for vdev %i: %d\n",
931 			    arg.vdev_id, ret);
932 		return ret;
933 	}
934 
935 	ar->num_started_vdevs++;
936 	ath10k_recalc_radar_detection(ar);
937 
938 	return ret;
939 }
940 
941 static int ath10k_vdev_start(struct ath10k_vif *arvif)
942 {
943 	return ath10k_vdev_start_restart(arvif, false);
944 }
945 
946 static int ath10k_vdev_restart(struct ath10k_vif *arvif)
947 {
948 	return ath10k_vdev_start_restart(arvif, true);
949 }
950 
951 static int ath10k_vdev_stop(struct ath10k_vif *arvif)
952 {
953 	struct ath10k *ar = arvif->ar;
954 	int ret;
955 
956 	lockdep_assert_held(&ar->conf_mutex);
957 
958 	reinit_completion(&ar->vdev_setup_done);
959 
960 	ret = ath10k_wmi_vdev_stop(ar, arvif->vdev_id);
961 	if (ret) {
962 		ath10k_warn(ar, "failed to stop WMI vdev %i: %d\n",
963 			    arvif->vdev_id, ret);
964 		return ret;
965 	}
966 
967 	ret = ath10k_vdev_setup_sync(ar);
968 	if (ret) {
969 		ath10k_warn(ar, "failed to syncronise setup for vdev %i: %d\n",
970 			    arvif->vdev_id, ret);
971 		return ret;
972 	}
973 
974 	WARN_ON(ar->num_started_vdevs == 0);
975 
976 	if (ar->num_started_vdevs != 0) {
977 		ar->num_started_vdevs--;
978 		ath10k_recalc_radar_detection(ar);
979 	}
980 
981 	return ret;
982 }
983 
984 static int ath10k_mac_setup_bcn_p2p_ie(struct ath10k_vif *arvif,
985 				       struct sk_buff *bcn)
986 {
987 	struct ath10k *ar = arvif->ar;
988 	struct ieee80211_mgmt *mgmt;
989 	const u8 *p2p_ie;
990 	int ret;
991 
992 	if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
993 		return 0;
994 
995 	if (arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
996 		return 0;
997 
998 	mgmt = (void *)bcn->data;
999 	p2p_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1000 					 mgmt->u.beacon.variable,
1001 					 bcn->len - (mgmt->u.beacon.variable -
1002 						     bcn->data));
1003 	if (!p2p_ie)
1004 		return -ENOENT;
1005 
1006 	ret = ath10k_wmi_p2p_go_bcn_ie(ar, arvif->vdev_id, p2p_ie);
1007 	if (ret) {
1008 		ath10k_warn(ar, "failed to submit p2p go bcn ie for vdev %i: %d\n",
1009 			    arvif->vdev_id, ret);
1010 		return ret;
1011 	}
1012 
1013 	return 0;
1014 }
1015 
1016 static int ath10k_mac_remove_vendor_ie(struct sk_buff *skb, unsigned int oui,
1017 				       u8 oui_type, size_t ie_offset)
1018 {
1019 	size_t len;
1020 	const u8 *next;
1021 	const u8 *end;
1022 	u8 *ie;
1023 
1024 	if (WARN_ON(skb->len < ie_offset))
1025 		return -EINVAL;
1026 
1027 	ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
1028 					   skb->data + ie_offset,
1029 					   skb->len - ie_offset);
1030 	if (!ie)
1031 		return -ENOENT;
1032 
1033 	len = ie[1] + 2;
1034 	end = skb->data + skb->len;
1035 	next = ie + len;
1036 
1037 	if (WARN_ON(next > end))
1038 		return -EINVAL;
1039 
1040 	memmove(ie, next, end - next);
1041 	skb_trim(skb, skb->len - len);
1042 
1043 	return 0;
1044 }
1045 
1046 static int ath10k_mac_setup_bcn_tmpl(struct ath10k_vif *arvif)
1047 {
1048 	struct ath10k *ar = arvif->ar;
1049 	struct ieee80211_hw *hw = ar->hw;
1050 	struct ieee80211_vif *vif = arvif->vif;
1051 	struct ieee80211_mutable_offsets offs = {};
1052 	struct sk_buff *bcn;
1053 	int ret;
1054 
1055 	if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1056 		return 0;
1057 
1058 	bcn = ieee80211_beacon_get_template(hw, vif, &offs);
1059 	if (!bcn) {
1060 		ath10k_warn(ar, "failed to get beacon template from mac80211\n");
1061 		return -EPERM;
1062 	}
1063 
1064 	ret = ath10k_mac_setup_bcn_p2p_ie(arvif, bcn);
1065 	if (ret) {
1066 		ath10k_warn(ar, "failed to setup p2p go bcn ie: %d\n", ret);
1067 		kfree_skb(bcn);
1068 		return ret;
1069 	}
1070 
1071 	/* P2P IE is inserted by firmware automatically (as configured above)
1072 	 * so remove it from the base beacon template to avoid duplicate P2P
1073 	 * IEs in beacon frames.
1074 	 */
1075 	ath10k_mac_remove_vendor_ie(bcn, WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1076 				    offsetof(struct ieee80211_mgmt,
1077 					     u.beacon.variable));
1078 
1079 	ret = ath10k_wmi_bcn_tmpl(ar, arvif->vdev_id, offs.tim_offset, bcn, 0,
1080 				  0, NULL, 0);
1081 	kfree_skb(bcn);
1082 
1083 	if (ret) {
1084 		ath10k_warn(ar, "failed to submit beacon template command: %d\n",
1085 			    ret);
1086 		return ret;
1087 	}
1088 
1089 	return 0;
1090 }
1091 
1092 static int ath10k_mac_setup_prb_tmpl(struct ath10k_vif *arvif)
1093 {
1094 	struct ath10k *ar = arvif->ar;
1095 	struct ieee80211_hw *hw = ar->hw;
1096 	struct ieee80211_vif *vif = arvif->vif;
1097 	struct sk_buff *prb;
1098 	int ret;
1099 
1100 	if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1101 		return 0;
1102 
1103 	prb = ieee80211_proberesp_get(hw, vif);
1104 	if (!prb) {
1105 		ath10k_warn(ar, "failed to get probe resp template from mac80211\n");
1106 		return -EPERM;
1107 	}
1108 
1109 	ret = ath10k_wmi_prb_tmpl(ar, arvif->vdev_id, prb);
1110 	kfree_skb(prb);
1111 
1112 	if (ret) {
1113 		ath10k_warn(ar, "failed to submit probe resp template command: %d\n",
1114 			    ret);
1115 		return ret;
1116 	}
1117 
1118 	return 0;
1119 }
1120 
1121 static void ath10k_control_beaconing(struct ath10k_vif *arvif,
1122 				     struct ieee80211_bss_conf *info)
1123 {
1124 	struct ath10k *ar = arvif->ar;
1125 	int ret = 0;
1126 
1127 	lockdep_assert_held(&arvif->ar->conf_mutex);
1128 
1129 	if (!info->enable_beacon) {
1130 		ath10k_vdev_stop(arvif);
1131 
1132 		arvif->is_started = false;
1133 		arvif->is_up = false;
1134 
1135 		spin_lock_bh(&arvif->ar->data_lock);
1136 		ath10k_mac_vif_beacon_free(arvif);
1137 		spin_unlock_bh(&arvif->ar->data_lock);
1138 
1139 		return;
1140 	}
1141 
1142 	arvif->tx_seq_no = 0x1000;
1143 
1144 	ret = ath10k_vdev_start(arvif);
1145 	if (ret)
1146 		return;
1147 
1148 	arvif->aid = 0;
1149 	ether_addr_copy(arvif->bssid, info->bssid);
1150 
1151 	ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
1152 				 arvif->bssid);
1153 	if (ret) {
1154 		ath10k_warn(ar, "failed to bring up vdev %d: %i\n",
1155 			    arvif->vdev_id, ret);
1156 		ath10k_vdev_stop(arvif);
1157 		return;
1158 	}
1159 
1160 	arvif->is_started = true;
1161 	arvif->is_up = true;
1162 
1163 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
1164 }
1165 
1166 static void ath10k_control_ibss(struct ath10k_vif *arvif,
1167 				struct ieee80211_bss_conf *info,
1168 				const u8 self_peer[ETH_ALEN])
1169 {
1170 	struct ath10k *ar = arvif->ar;
1171 	u32 vdev_param;
1172 	int ret = 0;
1173 
1174 	lockdep_assert_held(&arvif->ar->conf_mutex);
1175 
1176 	if (!info->ibss_joined) {
1177 		ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, self_peer);
1178 		if (ret)
1179 			ath10k_warn(ar, "failed to delete IBSS self peer %pM for vdev %d: %d\n",
1180 				    self_peer, arvif->vdev_id, ret);
1181 
1182 		if (is_zero_ether_addr(arvif->bssid))
1183 			return;
1184 
1185 		memset(arvif->bssid, 0, ETH_ALEN);
1186 
1187 		return;
1188 	}
1189 
1190 	ret = ath10k_peer_create(arvif->ar, arvif->vdev_id, self_peer);
1191 	if (ret) {
1192 		ath10k_warn(ar, "failed to create IBSS self peer %pM for vdev %d: %d\n",
1193 			    self_peer, arvif->vdev_id, ret);
1194 		return;
1195 	}
1196 
1197 	vdev_param = arvif->ar->wmi.vdev_param->atim_window;
1198 	ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id, vdev_param,
1199 					ATH10K_DEFAULT_ATIM);
1200 	if (ret)
1201 		ath10k_warn(ar, "failed to set IBSS ATIM for vdev %d: %d\n",
1202 			    arvif->vdev_id, ret);
1203 }
1204 
1205 static int ath10k_mac_vif_recalc_ps_wake_threshold(struct ath10k_vif *arvif)
1206 {
1207 	struct ath10k *ar = arvif->ar;
1208 	u32 param;
1209 	u32 value;
1210 	int ret;
1211 
1212 	lockdep_assert_held(&arvif->ar->conf_mutex);
1213 
1214 	if (arvif->u.sta.uapsd)
1215 		value = WMI_STA_PS_TX_WAKE_THRESHOLD_NEVER;
1216 	else
1217 		value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
1218 
1219 	param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
1220 	ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param, value);
1221 	if (ret) {
1222 		ath10k_warn(ar, "failed to submit ps wake threshold %u on vdev %i: %d\n",
1223 			    value, arvif->vdev_id, ret);
1224 		return ret;
1225 	}
1226 
1227 	return 0;
1228 }
1229 
1230 static int ath10k_mac_vif_recalc_ps_poll_count(struct ath10k_vif *arvif)
1231 {
1232 	struct ath10k *ar = arvif->ar;
1233 	u32 param;
1234 	u32 value;
1235 	int ret;
1236 
1237 	lockdep_assert_held(&arvif->ar->conf_mutex);
1238 
1239 	if (arvif->u.sta.uapsd)
1240 		value = WMI_STA_PS_PSPOLL_COUNT_UAPSD;
1241 	else
1242 		value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
1243 
1244 	param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
1245 	ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
1246 					  param, value);
1247 	if (ret) {
1248 		ath10k_warn(ar, "failed to submit ps poll count %u on vdev %i: %d\n",
1249 			    value, arvif->vdev_id, ret);
1250 		return ret;
1251 	}
1252 
1253 	return 0;
1254 }
1255 
1256 static int ath10k_mac_vif_setup_ps(struct ath10k_vif *arvif)
1257 {
1258 	struct ath10k *ar = arvif->ar;
1259 	struct ieee80211_vif *vif = arvif->vif;
1260 	struct ieee80211_conf *conf = &ar->hw->conf;
1261 	enum wmi_sta_powersave_param param;
1262 	enum wmi_sta_ps_mode psmode;
1263 	int ret;
1264 	int ps_timeout;
1265 
1266 	lockdep_assert_held(&arvif->ar->conf_mutex);
1267 
1268 	if (arvif->vif->type != NL80211_IFTYPE_STATION)
1269 		return 0;
1270 
1271 	if (vif->bss_conf.ps) {
1272 		psmode = WMI_STA_PS_MODE_ENABLED;
1273 		param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
1274 
1275 		ps_timeout = conf->dynamic_ps_timeout;
1276 		if (ps_timeout == 0) {
1277 			/* Firmware doesn't like 0 */
1278 			ps_timeout = ieee80211_tu_to_usec(
1279 				vif->bss_conf.beacon_int) / 1000;
1280 		}
1281 
1282 		ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
1283 						  ps_timeout);
1284 		if (ret) {
1285 			ath10k_warn(ar, "failed to set inactivity time for vdev %d: %i\n",
1286 				    arvif->vdev_id, ret);
1287 			return ret;
1288 		}
1289 	} else {
1290 		psmode = WMI_STA_PS_MODE_DISABLED;
1291 	}
1292 
1293 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d psmode %s\n",
1294 		   arvif->vdev_id, psmode ? "enable" : "disable");
1295 
1296 	ret = ath10k_wmi_set_psmode(ar, arvif->vdev_id, psmode);
1297 	if (ret) {
1298 		ath10k_warn(ar, "failed to set PS Mode %d for vdev %d: %d\n",
1299 			    psmode, arvif->vdev_id, ret);
1300 		return ret;
1301 	}
1302 
1303 	return 0;
1304 }
1305 
1306 static int ath10k_mac_vif_disable_keepalive(struct ath10k_vif *arvif)
1307 {
1308 	struct ath10k *ar = arvif->ar;
1309 	struct wmi_sta_keepalive_arg arg = {};
1310 	int ret;
1311 
1312 	lockdep_assert_held(&arvif->ar->conf_mutex);
1313 
1314 	if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
1315 		return 0;
1316 
1317 	if (!test_bit(WMI_SERVICE_STA_KEEP_ALIVE, ar->wmi.svc_map))
1318 		return 0;
1319 
1320 	/* Some firmware revisions have a bug and ignore the `enabled` field.
1321 	 * Instead use the interval to disable the keepalive.
1322 	 */
1323 	arg.vdev_id = arvif->vdev_id;
1324 	arg.enabled = 1;
1325 	arg.method = WMI_STA_KEEPALIVE_METHOD_NULL_FRAME;
1326 	arg.interval = WMI_STA_KEEPALIVE_INTERVAL_DISABLE;
1327 
1328 	ret = ath10k_wmi_sta_keepalive(ar, &arg);
1329 	if (ret) {
1330 		ath10k_warn(ar, "failed to submit keepalive on vdev %i: %d\n",
1331 			    arvif->vdev_id, ret);
1332 		return ret;
1333 	}
1334 
1335 	return 0;
1336 }
1337 
1338 /**********************/
1339 /* Station management */
1340 /**********************/
1341 
1342 static u32 ath10k_peer_assoc_h_listen_intval(struct ath10k *ar,
1343 					     struct ieee80211_vif *vif)
1344 {
1345 	/* Some firmware revisions have unstable STA powersave when listen
1346 	 * interval is set too high (e.g. 5). The symptoms are firmware doesn't
1347 	 * generate NullFunc frames properly even if buffered frames have been
1348 	 * indicated in Beacon TIM. Firmware would seldom wake up to pull
1349 	 * buffered frames. Often pinging the device from AP would simply fail.
1350 	 *
1351 	 * As a workaround set it to 1.
1352 	 */
1353 	if (vif->type == NL80211_IFTYPE_STATION)
1354 		return 1;
1355 
1356 	return ar->hw->conf.listen_interval;
1357 }
1358 
1359 static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
1360 				      struct ieee80211_vif *vif,
1361 				      struct ieee80211_sta *sta,
1362 				      struct wmi_peer_assoc_complete_arg *arg)
1363 {
1364 	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1365 
1366 	lockdep_assert_held(&ar->conf_mutex);
1367 
1368 	ether_addr_copy(arg->addr, sta->addr);
1369 	arg->vdev_id = arvif->vdev_id;
1370 	arg->peer_aid = sta->aid;
1371 	arg->peer_flags |= WMI_PEER_AUTH;
1372 	arg->peer_listen_intval = ath10k_peer_assoc_h_listen_intval(ar, vif);
1373 	arg->peer_num_spatial_streams = 1;
1374 	arg->peer_caps = vif->bss_conf.assoc_capability;
1375 }
1376 
1377 static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
1378 				       struct ieee80211_vif *vif,
1379 				       struct wmi_peer_assoc_complete_arg *arg)
1380 {
1381 	struct ieee80211_bss_conf *info = &vif->bss_conf;
1382 	struct cfg80211_bss *bss;
1383 	const u8 *rsnie = NULL;
1384 	const u8 *wpaie = NULL;
1385 
1386 	lockdep_assert_held(&ar->conf_mutex);
1387 
1388 	bss = cfg80211_get_bss(ar->hw->wiphy, ar->hw->conf.chandef.chan,
1389 			       info->bssid, NULL, 0, 0, 0);
1390 	if (bss) {
1391 		const struct cfg80211_bss_ies *ies;
1392 
1393 		rcu_read_lock();
1394 		rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
1395 
1396 		ies = rcu_dereference(bss->ies);
1397 
1398 		wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
1399 						WLAN_OUI_TYPE_MICROSOFT_WPA,
1400 						ies->data,
1401 						ies->len);
1402 		rcu_read_unlock();
1403 		cfg80211_put_bss(ar->hw->wiphy, bss);
1404 	}
1405 
1406 	/* FIXME: base on RSN IE/WPA IE is a correct idea? */
1407 	if (rsnie || wpaie) {
1408 		ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
1409 		arg->peer_flags |= WMI_PEER_NEED_PTK_4_WAY;
1410 	}
1411 
1412 	if (wpaie) {
1413 		ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: wpa ie found\n", __func__);
1414 		arg->peer_flags |= WMI_PEER_NEED_GTK_2_WAY;
1415 	}
1416 }
1417 
1418 static void ath10k_peer_assoc_h_rates(struct ath10k *ar,
1419 				      struct ieee80211_sta *sta,
1420 				      struct wmi_peer_assoc_complete_arg *arg)
1421 {
1422 	struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
1423 	const struct ieee80211_supported_band *sband;
1424 	const struct ieee80211_rate *rates;
1425 	u32 ratemask;
1426 	int i;
1427 
1428 	lockdep_assert_held(&ar->conf_mutex);
1429 
1430 	sband = ar->hw->wiphy->bands[ar->hw->conf.chandef.chan->band];
1431 	ratemask = sta->supp_rates[ar->hw->conf.chandef.chan->band];
1432 	rates = sband->bitrates;
1433 
1434 	rateset->num_rates = 0;
1435 
1436 	for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
1437 		if (!(ratemask & 1))
1438 			continue;
1439 
1440 		rateset->rates[rateset->num_rates] = rates->hw_value;
1441 		rateset->num_rates++;
1442 	}
1443 }
1444 
1445 static void ath10k_peer_assoc_h_ht(struct ath10k *ar,
1446 				   struct ieee80211_sta *sta,
1447 				   struct wmi_peer_assoc_complete_arg *arg)
1448 {
1449 	const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1450 	int i, n;
1451 	u32 stbc;
1452 
1453 	lockdep_assert_held(&ar->conf_mutex);
1454 
1455 	if (!ht_cap->ht_supported)
1456 		return;
1457 
1458 	arg->peer_flags |= WMI_PEER_HT;
1459 	arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1460 				    ht_cap->ampdu_factor)) - 1;
1461 
1462 	arg->peer_mpdu_density =
1463 		ath10k_parse_mpdudensity(ht_cap->ampdu_density);
1464 
1465 	arg->peer_ht_caps = ht_cap->cap;
1466 	arg->peer_rate_caps |= WMI_RC_HT_FLAG;
1467 
1468 	if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
1469 		arg->peer_flags |= WMI_PEER_LDPC;
1470 
1471 	if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
1472 		arg->peer_flags |= WMI_PEER_40MHZ;
1473 		arg->peer_rate_caps |= WMI_RC_CW40_FLAG;
1474 	}
1475 
1476 	if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
1477 		arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
1478 
1479 	if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
1480 		arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
1481 
1482 	if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
1483 		arg->peer_rate_caps |= WMI_RC_TX_STBC_FLAG;
1484 		arg->peer_flags |= WMI_PEER_STBC;
1485 	}
1486 
1487 	if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
1488 		stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
1489 		stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
1490 		stbc = stbc << WMI_RC_RX_STBC_FLAG_S;
1491 		arg->peer_rate_caps |= stbc;
1492 		arg->peer_flags |= WMI_PEER_STBC;
1493 	}
1494 
1495 	if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
1496 		arg->peer_rate_caps |= WMI_RC_TS_FLAG;
1497 	else if (ht_cap->mcs.rx_mask[1])
1498 		arg->peer_rate_caps |= WMI_RC_DS_FLAG;
1499 
1500 	for (i = 0, n = 0; i < IEEE80211_HT_MCS_MASK_LEN*8; i++)
1501 		if (ht_cap->mcs.rx_mask[i/8] & (1 << i%8))
1502 			arg->peer_ht_rates.rates[n++] = i;
1503 
1504 	/*
1505 	 * This is a workaround for HT-enabled STAs which break the spec
1506 	 * and have no HT capabilities RX mask (no HT RX MCS map).
1507 	 *
1508 	 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
1509 	 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
1510 	 *
1511 	 * Firmware asserts if such situation occurs.
1512 	 */
1513 	if (n == 0) {
1514 		arg->peer_ht_rates.num_rates = 8;
1515 		for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
1516 			arg->peer_ht_rates.rates[i] = i;
1517 	} else {
1518 		arg->peer_ht_rates.num_rates = n;
1519 		arg->peer_num_spatial_streams = sta->rx_nss;
1520 	}
1521 
1522 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
1523 		   arg->addr,
1524 		   arg->peer_ht_rates.num_rates,
1525 		   arg->peer_num_spatial_streams);
1526 }
1527 
1528 static int ath10k_peer_assoc_qos_ap(struct ath10k *ar,
1529 				    struct ath10k_vif *arvif,
1530 				    struct ieee80211_sta *sta)
1531 {
1532 	u32 uapsd = 0;
1533 	u32 max_sp = 0;
1534 	int ret = 0;
1535 
1536 	lockdep_assert_held(&ar->conf_mutex);
1537 
1538 	if (sta->wme && sta->uapsd_queues) {
1539 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
1540 			   sta->uapsd_queues, sta->max_sp);
1541 
1542 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1543 			uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
1544 				 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
1545 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1546 			uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
1547 				 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
1548 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1549 			uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
1550 				 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
1551 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1552 			uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
1553 				 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
1554 
1555 		if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
1556 			max_sp = sta->max_sp;
1557 
1558 		ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1559 						 sta->addr,
1560 						 WMI_AP_PS_PEER_PARAM_UAPSD,
1561 						 uapsd);
1562 		if (ret) {
1563 			ath10k_warn(ar, "failed to set ap ps peer param uapsd for vdev %i: %d\n",
1564 				    arvif->vdev_id, ret);
1565 			return ret;
1566 		}
1567 
1568 		ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1569 						 sta->addr,
1570 						 WMI_AP_PS_PEER_PARAM_MAX_SP,
1571 						 max_sp);
1572 		if (ret) {
1573 			ath10k_warn(ar, "failed to set ap ps peer param max sp for vdev %i: %d\n",
1574 				    arvif->vdev_id, ret);
1575 			return ret;
1576 		}
1577 
1578 		/* TODO setup this based on STA listen interval and
1579 		   beacon interval. Currently we don't know
1580 		   sta->listen_interval - mac80211 patch required.
1581 		   Currently use 10 seconds */
1582 		ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id, sta->addr,
1583 						 WMI_AP_PS_PEER_PARAM_AGEOUT_TIME,
1584 						 10);
1585 		if (ret) {
1586 			ath10k_warn(ar, "failed to set ap ps peer param ageout time for vdev %i: %d\n",
1587 				    arvif->vdev_id, ret);
1588 			return ret;
1589 		}
1590 	}
1591 
1592 	return 0;
1593 }
1594 
1595 static void ath10k_peer_assoc_h_vht(struct ath10k *ar,
1596 				    struct ieee80211_sta *sta,
1597 				    struct wmi_peer_assoc_complete_arg *arg)
1598 {
1599 	const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
1600 	u8 ampdu_factor;
1601 
1602 	if (!vht_cap->vht_supported)
1603 		return;
1604 
1605 	arg->peer_flags |= WMI_PEER_VHT;
1606 
1607 	if (ar->hw->conf.chandef.chan->band == IEEE80211_BAND_2GHZ)
1608 		arg->peer_flags |= WMI_PEER_VHT_2G;
1609 
1610 	arg->peer_vht_caps = vht_cap->cap;
1611 
1612 	ampdu_factor = (vht_cap->cap &
1613 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
1614 		       IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
1615 
1616 	/* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
1617 	 * zero in VHT IE. Using it would result in degraded throughput.
1618 	 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
1619 	 * it if VHT max_mpdu is smaller. */
1620 	arg->peer_max_mpdu = max(arg->peer_max_mpdu,
1621 				 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1622 					ampdu_factor)) - 1);
1623 
1624 	if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1625 		arg->peer_flags |= WMI_PEER_80MHZ;
1626 
1627 	arg->peer_vht_rates.rx_max_rate =
1628 		__le16_to_cpu(vht_cap->vht_mcs.rx_highest);
1629 	arg->peer_vht_rates.rx_mcs_set =
1630 		__le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
1631 	arg->peer_vht_rates.tx_max_rate =
1632 		__le16_to_cpu(vht_cap->vht_mcs.tx_highest);
1633 	arg->peer_vht_rates.tx_mcs_set =
1634 		__le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
1635 
1636 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
1637 		   sta->addr, arg->peer_max_mpdu, arg->peer_flags);
1638 }
1639 
1640 static void ath10k_peer_assoc_h_qos(struct ath10k *ar,
1641 				    struct ieee80211_vif *vif,
1642 				    struct ieee80211_sta *sta,
1643 				    struct wmi_peer_assoc_complete_arg *arg)
1644 {
1645 	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1646 
1647 	switch (arvif->vdev_type) {
1648 	case WMI_VDEV_TYPE_AP:
1649 		if (sta->wme)
1650 			arg->peer_flags |= WMI_PEER_QOS;
1651 
1652 		if (sta->wme && sta->uapsd_queues) {
1653 			arg->peer_flags |= WMI_PEER_APSD;
1654 			arg->peer_rate_caps |= WMI_RC_UAPSD_FLAG;
1655 		}
1656 		break;
1657 	case WMI_VDEV_TYPE_STA:
1658 		if (vif->bss_conf.qos)
1659 			arg->peer_flags |= WMI_PEER_QOS;
1660 		break;
1661 	case WMI_VDEV_TYPE_IBSS:
1662 		if (sta->wme)
1663 			arg->peer_flags |= WMI_PEER_QOS;
1664 		break;
1665 	default:
1666 		break;
1667 	}
1668 
1669 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM qos %d\n",
1670 		   sta->addr, !!(arg->peer_flags & WMI_PEER_QOS));
1671 }
1672 
1673 static bool ath10k_mac_sta_has_11g_rates(struct ieee80211_sta *sta)
1674 {
1675 	/* First 4 rates in ath10k_rates are CCK (11b) rates. */
1676 	return sta->supp_rates[IEEE80211_BAND_2GHZ] >> 4;
1677 }
1678 
1679 static void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
1680 					struct ieee80211_vif *vif,
1681 					struct ieee80211_sta *sta,
1682 					struct wmi_peer_assoc_complete_arg *arg)
1683 {
1684 	enum wmi_phy_mode phymode = MODE_UNKNOWN;
1685 
1686 	switch (ar->hw->conf.chandef.chan->band) {
1687 	case IEEE80211_BAND_2GHZ:
1688 		if (sta->vht_cap.vht_supported) {
1689 			if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1690 				phymode = MODE_11AC_VHT40;
1691 			else
1692 				phymode = MODE_11AC_VHT20;
1693 		} else if (sta->ht_cap.ht_supported) {
1694 			if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1695 				phymode = MODE_11NG_HT40;
1696 			else
1697 				phymode = MODE_11NG_HT20;
1698 		} else if (ath10k_mac_sta_has_11g_rates(sta)) {
1699 			phymode = MODE_11G;
1700 		} else {
1701 			phymode = MODE_11B;
1702 		}
1703 
1704 		break;
1705 	case IEEE80211_BAND_5GHZ:
1706 		/*
1707 		 * Check VHT first.
1708 		 */
1709 		if (sta->vht_cap.vht_supported) {
1710 			if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1711 				phymode = MODE_11AC_VHT80;
1712 			else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1713 				phymode = MODE_11AC_VHT40;
1714 			else if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
1715 				phymode = MODE_11AC_VHT20;
1716 		} else if (sta->ht_cap.ht_supported) {
1717 			if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1718 				phymode = MODE_11NA_HT40;
1719 			else
1720 				phymode = MODE_11NA_HT20;
1721 		} else {
1722 			phymode = MODE_11A;
1723 		}
1724 
1725 		break;
1726 	default:
1727 		break;
1728 	}
1729 
1730 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM phymode %s\n",
1731 		   sta->addr, ath10k_wmi_phymode_str(phymode));
1732 
1733 	arg->peer_phymode = phymode;
1734 	WARN_ON(phymode == MODE_UNKNOWN);
1735 }
1736 
1737 static int ath10k_peer_assoc_prepare(struct ath10k *ar,
1738 				     struct ieee80211_vif *vif,
1739 				     struct ieee80211_sta *sta,
1740 				     struct wmi_peer_assoc_complete_arg *arg)
1741 {
1742 	lockdep_assert_held(&ar->conf_mutex);
1743 
1744 	memset(arg, 0, sizeof(*arg));
1745 
1746 	ath10k_peer_assoc_h_basic(ar, vif, sta, arg);
1747 	ath10k_peer_assoc_h_crypto(ar, vif, arg);
1748 	ath10k_peer_assoc_h_rates(ar, sta, arg);
1749 	ath10k_peer_assoc_h_ht(ar, sta, arg);
1750 	ath10k_peer_assoc_h_vht(ar, sta, arg);
1751 	ath10k_peer_assoc_h_qos(ar, vif, sta, arg);
1752 	ath10k_peer_assoc_h_phymode(ar, vif, sta, arg);
1753 
1754 	return 0;
1755 }
1756 
1757 static const u32 ath10k_smps_map[] = {
1758 	[WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
1759 	[WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
1760 	[WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
1761 	[WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
1762 };
1763 
1764 static int ath10k_setup_peer_smps(struct ath10k *ar, struct ath10k_vif *arvif,
1765 				  const u8 *addr,
1766 				  const struct ieee80211_sta_ht_cap *ht_cap)
1767 {
1768 	int smps;
1769 
1770 	if (!ht_cap->ht_supported)
1771 		return 0;
1772 
1773 	smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
1774 	smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
1775 
1776 	if (smps >= ARRAY_SIZE(ath10k_smps_map))
1777 		return -EINVAL;
1778 
1779 	return ath10k_wmi_peer_set_param(ar, arvif->vdev_id, addr,
1780 					 WMI_PEER_SMPS_STATE,
1781 					 ath10k_smps_map[smps]);
1782 }
1783 
1784 /* can be called only in mac80211 callbacks due to `key_count` usage */
1785 static void ath10k_bss_assoc(struct ieee80211_hw *hw,
1786 			     struct ieee80211_vif *vif,
1787 			     struct ieee80211_bss_conf *bss_conf)
1788 {
1789 	struct ath10k *ar = hw->priv;
1790 	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1791 	struct ieee80211_sta_ht_cap ht_cap;
1792 	struct wmi_peer_assoc_complete_arg peer_arg;
1793 	struct ieee80211_sta *ap_sta;
1794 	int ret;
1795 
1796 	lockdep_assert_held(&ar->conf_mutex);
1797 
1798 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
1799 		   arvif->vdev_id, arvif->bssid, arvif->aid);
1800 
1801 	rcu_read_lock();
1802 
1803 	ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
1804 	if (!ap_sta) {
1805 		ath10k_warn(ar, "failed to find station entry for bss %pM vdev %i\n",
1806 			    bss_conf->bssid, arvif->vdev_id);
1807 		rcu_read_unlock();
1808 		return;
1809 	}
1810 
1811 	/* ap_sta must be accessed only within rcu section which must be left
1812 	 * before calling ath10k_setup_peer_smps() which might sleep. */
1813 	ht_cap = ap_sta->ht_cap;
1814 
1815 	ret = ath10k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg);
1816 	if (ret) {
1817 		ath10k_warn(ar, "failed to prepare peer assoc for %pM vdev %i: %d\n",
1818 			    bss_conf->bssid, arvif->vdev_id, ret);
1819 		rcu_read_unlock();
1820 		return;
1821 	}
1822 
1823 	rcu_read_unlock();
1824 
1825 	ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
1826 	if (ret) {
1827 		ath10k_warn(ar, "failed to run peer assoc for %pM vdev %i: %d\n",
1828 			    bss_conf->bssid, arvif->vdev_id, ret);
1829 		return;
1830 	}
1831 
1832 	ret = ath10k_setup_peer_smps(ar, arvif, bss_conf->bssid, &ht_cap);
1833 	if (ret) {
1834 		ath10k_warn(ar, "failed to setup peer SMPS for vdev %i: %d\n",
1835 			    arvif->vdev_id, ret);
1836 		return;
1837 	}
1838 
1839 	ath10k_dbg(ar, ATH10K_DBG_MAC,
1840 		   "mac vdev %d up (associated) bssid %pM aid %d\n",
1841 		   arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
1842 
1843 	WARN_ON(arvif->is_up);
1844 
1845 	arvif->aid = bss_conf->aid;
1846 	ether_addr_copy(arvif->bssid, bss_conf->bssid);
1847 
1848 	ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
1849 	if (ret) {
1850 		ath10k_warn(ar, "failed to set vdev %d up: %d\n",
1851 			    arvif->vdev_id, ret);
1852 		return;
1853 	}
1854 
1855 	arvif->is_up = true;
1856 }
1857 
1858 static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
1859 				struct ieee80211_vif *vif)
1860 {
1861 	struct ath10k *ar = hw->priv;
1862 	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1863 	int ret;
1864 
1865 	lockdep_assert_held(&ar->conf_mutex);
1866 
1867 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
1868 		   arvif->vdev_id, arvif->bssid);
1869 
1870 	ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
1871 	if (ret)
1872 		ath10k_warn(ar, "faield to down vdev %i: %d\n",
1873 			    arvif->vdev_id, ret);
1874 
1875 	arvif->def_wep_key_idx = -1;
1876 
1877 	arvif->is_up = false;
1878 }
1879 
1880 static int ath10k_station_assoc(struct ath10k *ar,
1881 				struct ieee80211_vif *vif,
1882 				struct ieee80211_sta *sta,
1883 				bool reassoc)
1884 {
1885 	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1886 	struct wmi_peer_assoc_complete_arg peer_arg;
1887 	int ret = 0;
1888 
1889 	lockdep_assert_held(&ar->conf_mutex);
1890 
1891 	ret = ath10k_peer_assoc_prepare(ar, vif, sta, &peer_arg);
1892 	if (ret) {
1893 		ath10k_warn(ar, "failed to prepare WMI peer assoc for %pM vdev %i: %i\n",
1894 			    sta->addr, arvif->vdev_id, ret);
1895 		return ret;
1896 	}
1897 
1898 	peer_arg.peer_reassoc = reassoc;
1899 	ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
1900 	if (ret) {
1901 		ath10k_warn(ar, "failed to run peer assoc for STA %pM vdev %i: %d\n",
1902 			    sta->addr, arvif->vdev_id, ret);
1903 		return ret;
1904 	}
1905 
1906 	/* Re-assoc is run only to update supported rates for given station. It
1907 	 * doesn't make much sense to reconfigure the peer completely.
1908 	 */
1909 	if (!reassoc) {
1910 		ret = ath10k_setup_peer_smps(ar, arvif, sta->addr,
1911 					     &sta->ht_cap);
1912 		if (ret) {
1913 			ath10k_warn(ar, "failed to setup peer SMPS for vdev %d: %d\n",
1914 				    arvif->vdev_id, ret);
1915 			return ret;
1916 		}
1917 
1918 		ret = ath10k_peer_assoc_qos_ap(ar, arvif, sta);
1919 		if (ret) {
1920 			ath10k_warn(ar, "failed to set qos params for STA %pM for vdev %i: %d\n",
1921 				    sta->addr, arvif->vdev_id, ret);
1922 			return ret;
1923 		}
1924 
1925 		if (!sta->wme) {
1926 			arvif->num_legacy_stations++;
1927 			ret  = ath10k_recalc_rtscts_prot(arvif);
1928 			if (ret) {
1929 				ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
1930 					    arvif->vdev_id, ret);
1931 				return ret;
1932 			}
1933 		}
1934 
1935 		/* Plumb cached keys only for static WEP */
1936 		if (arvif->def_wep_key_idx != -1) {
1937 			ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
1938 			if (ret) {
1939 				ath10k_warn(ar, "failed to install peer wep keys for vdev %i: %d\n",
1940 					    arvif->vdev_id, ret);
1941 				return ret;
1942 			}
1943 		}
1944 	}
1945 
1946 	return ret;
1947 }
1948 
1949 static int ath10k_station_disassoc(struct ath10k *ar,
1950 				   struct ieee80211_vif *vif,
1951 				   struct ieee80211_sta *sta)
1952 {
1953 	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1954 	int ret = 0;
1955 
1956 	lockdep_assert_held(&ar->conf_mutex);
1957 
1958 	if (!sta->wme) {
1959 		arvif->num_legacy_stations--;
1960 		ret = ath10k_recalc_rtscts_prot(arvif);
1961 		if (ret) {
1962 			ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
1963 				    arvif->vdev_id, ret);
1964 			return ret;
1965 		}
1966 	}
1967 
1968 	ret = ath10k_clear_peer_keys(arvif, sta->addr);
1969 	if (ret) {
1970 		ath10k_warn(ar, "failed to clear all peer wep keys for vdev %i: %d\n",
1971 			    arvif->vdev_id, ret);
1972 		return ret;
1973 	}
1974 
1975 	return ret;
1976 }
1977 
1978 /**************/
1979 /* Regulatory */
1980 /**************/
1981 
1982 static int ath10k_update_channel_list(struct ath10k *ar)
1983 {
1984 	struct ieee80211_hw *hw = ar->hw;
1985 	struct ieee80211_supported_band **bands;
1986 	enum ieee80211_band band;
1987 	struct ieee80211_channel *channel;
1988 	struct wmi_scan_chan_list_arg arg = {0};
1989 	struct wmi_channel_arg *ch;
1990 	bool passive;
1991 	int len;
1992 	int ret;
1993 	int i;
1994 
1995 	lockdep_assert_held(&ar->conf_mutex);
1996 
1997 	bands = hw->wiphy->bands;
1998 	for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1999 		if (!bands[band])
2000 			continue;
2001 
2002 		for (i = 0; i < bands[band]->n_channels; i++) {
2003 			if (bands[band]->channels[i].flags &
2004 			    IEEE80211_CHAN_DISABLED)
2005 				continue;
2006 
2007 			arg.n_channels++;
2008 		}
2009 	}
2010 
2011 	len = sizeof(struct wmi_channel_arg) * arg.n_channels;
2012 	arg.channels = kzalloc(len, GFP_KERNEL);
2013 	if (!arg.channels)
2014 		return -ENOMEM;
2015 
2016 	ch = arg.channels;
2017 	for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2018 		if (!bands[band])
2019 			continue;
2020 
2021 		for (i = 0; i < bands[band]->n_channels; i++) {
2022 			channel = &bands[band]->channels[i];
2023 
2024 			if (channel->flags & IEEE80211_CHAN_DISABLED)
2025 				continue;
2026 
2027 			ch->allow_ht   = true;
2028 
2029 			/* FIXME: when should we really allow VHT? */
2030 			ch->allow_vht = true;
2031 
2032 			ch->allow_ibss =
2033 				!(channel->flags & IEEE80211_CHAN_NO_IR);
2034 
2035 			ch->ht40plus =
2036 				!(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
2037 
2038 			ch->chan_radar =
2039 				!!(channel->flags & IEEE80211_CHAN_RADAR);
2040 
2041 			passive = channel->flags & IEEE80211_CHAN_NO_IR;
2042 			ch->passive = passive;
2043 
2044 			ch->freq = channel->center_freq;
2045 			ch->band_center_freq1 = channel->center_freq;
2046 			ch->min_power = 0;
2047 			ch->max_power = channel->max_power * 2;
2048 			ch->max_reg_power = channel->max_reg_power * 2;
2049 			ch->max_antenna_gain = channel->max_antenna_gain * 2;
2050 			ch->reg_class_id = 0; /* FIXME */
2051 
2052 			/* FIXME: why use only legacy modes, why not any
2053 			 * HT/VHT modes? Would that even make any
2054 			 * difference? */
2055 			if (channel->band == IEEE80211_BAND_2GHZ)
2056 				ch->mode = MODE_11G;
2057 			else
2058 				ch->mode = MODE_11A;
2059 
2060 			if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
2061 				continue;
2062 
2063 			ath10k_dbg(ar, ATH10K_DBG_WMI,
2064 				   "mac channel [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
2065 				    ch - arg.channels, arg.n_channels,
2066 				   ch->freq, ch->max_power, ch->max_reg_power,
2067 				   ch->max_antenna_gain, ch->mode);
2068 
2069 			ch++;
2070 		}
2071 	}
2072 
2073 	ret = ath10k_wmi_scan_chan_list(ar, &arg);
2074 	kfree(arg.channels);
2075 
2076 	return ret;
2077 }
2078 
2079 static enum wmi_dfs_region
2080 ath10k_mac_get_dfs_region(enum nl80211_dfs_regions dfs_region)
2081 {
2082 	switch (dfs_region) {
2083 	case NL80211_DFS_UNSET:
2084 		return WMI_UNINIT_DFS_DOMAIN;
2085 	case NL80211_DFS_FCC:
2086 		return WMI_FCC_DFS_DOMAIN;
2087 	case NL80211_DFS_ETSI:
2088 		return WMI_ETSI_DFS_DOMAIN;
2089 	case NL80211_DFS_JP:
2090 		return WMI_MKK4_DFS_DOMAIN;
2091 	}
2092 	return WMI_UNINIT_DFS_DOMAIN;
2093 }
2094 
2095 static void ath10k_regd_update(struct ath10k *ar)
2096 {
2097 	struct reg_dmn_pair_mapping *regpair;
2098 	int ret;
2099 	enum wmi_dfs_region wmi_dfs_reg;
2100 	enum nl80211_dfs_regions nl_dfs_reg;
2101 
2102 	lockdep_assert_held(&ar->conf_mutex);
2103 
2104 	ret = ath10k_update_channel_list(ar);
2105 	if (ret)
2106 		ath10k_warn(ar, "failed to update channel list: %d\n", ret);
2107 
2108 	regpair = ar->ath_common.regulatory.regpair;
2109 
2110 	if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
2111 		nl_dfs_reg = ar->dfs_detector->region;
2112 		wmi_dfs_reg = ath10k_mac_get_dfs_region(nl_dfs_reg);
2113 	} else {
2114 		wmi_dfs_reg = WMI_UNINIT_DFS_DOMAIN;
2115 	}
2116 
2117 	/* Target allows setting up per-band regdomain but ath_common provides
2118 	 * a combined one only */
2119 	ret = ath10k_wmi_pdev_set_regdomain(ar,
2120 					    regpair->reg_domain,
2121 					    regpair->reg_domain, /* 2ghz */
2122 					    regpair->reg_domain, /* 5ghz */
2123 					    regpair->reg_2ghz_ctl,
2124 					    regpair->reg_5ghz_ctl,
2125 					    wmi_dfs_reg);
2126 	if (ret)
2127 		ath10k_warn(ar, "failed to set pdev regdomain: %d\n", ret);
2128 }
2129 
2130 static void ath10k_reg_notifier(struct wiphy *wiphy,
2131 				struct regulatory_request *request)
2132 {
2133 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
2134 	struct ath10k *ar = hw->priv;
2135 	bool result;
2136 
2137 	ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
2138 
2139 	if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
2140 		ath10k_dbg(ar, ATH10K_DBG_REGULATORY, "dfs region 0x%x\n",
2141 			   request->dfs_region);
2142 		result = ar->dfs_detector->set_dfs_domain(ar->dfs_detector,
2143 							  request->dfs_region);
2144 		if (!result)
2145 			ath10k_warn(ar, "DFS region 0x%X not supported, will trigger radar for every pulse\n",
2146 				    request->dfs_region);
2147 	}
2148 
2149 	mutex_lock(&ar->conf_mutex);
2150 	if (ar->state == ATH10K_STATE_ON)
2151 		ath10k_regd_update(ar);
2152 	mutex_unlock(&ar->conf_mutex);
2153 }
2154 
2155 /***************/
2156 /* TX handlers */
2157 /***************/
2158 
2159 static u8 ath10k_tx_h_get_tid(struct ieee80211_hdr *hdr)
2160 {
2161 	if (ieee80211_is_mgmt(hdr->frame_control))
2162 		return HTT_DATA_TX_EXT_TID_MGMT;
2163 
2164 	if (!ieee80211_is_data_qos(hdr->frame_control))
2165 		return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
2166 
2167 	if (!is_unicast_ether_addr(ieee80211_get_DA(hdr)))
2168 		return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
2169 
2170 	return ieee80211_get_qos_ctl(hdr)[0] & IEEE80211_QOS_CTL_TID_MASK;
2171 }
2172 
2173 static u8 ath10k_tx_h_get_vdev_id(struct ath10k *ar, struct ieee80211_vif *vif)
2174 {
2175 	if (vif)
2176 		return ath10k_vif_to_arvif(vif)->vdev_id;
2177 
2178 	if (ar->monitor_started)
2179 		return ar->monitor_vdev_id;
2180 
2181 	ath10k_warn(ar, "failed to resolve vdev id\n");
2182 	return 0;
2183 }
2184 
2185 /* HTT Tx uses Native Wifi tx mode which expects 802.11 frames without QoS
2186  * Control in the header.
2187  */
2188 static void ath10k_tx_h_nwifi(struct ieee80211_hw *hw, struct sk_buff *skb)
2189 {
2190 	struct ieee80211_hdr *hdr = (void *)skb->data;
2191 	struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
2192 	u8 *qos_ctl;
2193 
2194 	if (!ieee80211_is_data_qos(hdr->frame_control))
2195 		return;
2196 
2197 	qos_ctl = ieee80211_get_qos_ctl(hdr);
2198 	memmove(skb->data + IEEE80211_QOS_CTL_LEN,
2199 		skb->data, (void *)qos_ctl - (void *)skb->data);
2200 	skb_pull(skb, IEEE80211_QOS_CTL_LEN);
2201 
2202 	/* Fw/Hw generates a corrupted QoS Control Field for QoS NullFunc
2203 	 * frames. Powersave is handled by the fw/hw so QoS NyllFunc frames are
2204 	 * used only for CQM purposes (e.g. hostapd station keepalive ping) so
2205 	 * it is safe to downgrade to NullFunc.
2206 	 */
2207 	hdr = (void *)skb->data;
2208 	if (ieee80211_is_qos_nullfunc(hdr->frame_control)) {
2209 		hdr->frame_control &= ~__cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
2210 		cb->htt.tid = HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
2211 	}
2212 }
2213 
2214 static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar,
2215 				       struct ieee80211_vif *vif,
2216 				       struct sk_buff *skb)
2217 {
2218 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2219 	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2220 
2221 	/* This is case only for P2P_GO */
2222 	if (arvif->vdev_type != WMI_VDEV_TYPE_AP ||
2223 	    arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
2224 		return;
2225 
2226 	if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
2227 		spin_lock_bh(&ar->data_lock);
2228 		if (arvif->u.ap.noa_data)
2229 			if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
2230 					      GFP_ATOMIC))
2231 				memcpy(skb_put(skb, arvif->u.ap.noa_len),
2232 				       arvif->u.ap.noa_data,
2233 				       arvif->u.ap.noa_len);
2234 		spin_unlock_bh(&ar->data_lock);
2235 	}
2236 }
2237 
2238 static bool ath10k_mac_need_offchan_tx_work(struct ath10k *ar)
2239 {
2240 	/* FIXME: Not really sure since when the behaviour changed. At some
2241 	 * point new firmware stopped requiring creation of peer entries for
2242 	 * offchannel tx (and actually creating them causes issues with wmi-htc
2243 	 * tx credit replenishment and reliability). Assuming it's at least 3.4
2244 	 * because that's when the `freq` was introduced to TX_FRM HTT command.
2245 	 */
2246 	return !(ar->htt.target_version_major >= 3 &&
2247 		 ar->htt.target_version_minor >= 4);
2248 }
2249 
2250 static void ath10k_tx_htt(struct ath10k *ar, struct sk_buff *skb)
2251 {
2252 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2253 	int ret = 0;
2254 
2255 	if (ar->htt.target_version_major >= 3) {
2256 		/* Since HTT 3.0 there is no separate mgmt tx command */
2257 		ret = ath10k_htt_tx(&ar->htt, skb);
2258 		goto exit;
2259 	}
2260 
2261 	if (ieee80211_is_mgmt(hdr->frame_control)) {
2262 		if (test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
2263 			     ar->fw_features)) {
2264 			if (skb_queue_len(&ar->wmi_mgmt_tx_queue) >=
2265 			    ATH10K_MAX_NUM_MGMT_PENDING) {
2266 				ath10k_warn(ar, "reached WMI management transmit queue limit\n");
2267 				ret = -EBUSY;
2268 				goto exit;
2269 			}
2270 
2271 			skb_queue_tail(&ar->wmi_mgmt_tx_queue, skb);
2272 			ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
2273 		} else {
2274 			ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
2275 		}
2276 	} else if (!test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
2277 			     ar->fw_features) &&
2278 		   ieee80211_is_nullfunc(hdr->frame_control)) {
2279 		/* FW does not report tx status properly for NullFunc frames
2280 		 * unless they are sent through mgmt tx path. mac80211 sends
2281 		 * those frames when it detects link/beacon loss and depends
2282 		 * on the tx status to be correct. */
2283 		ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
2284 	} else {
2285 		ret = ath10k_htt_tx(&ar->htt, skb);
2286 	}
2287 
2288 exit:
2289 	if (ret) {
2290 		ath10k_warn(ar, "failed to transmit packet, dropping: %d\n",
2291 			    ret);
2292 		ieee80211_free_txskb(ar->hw, skb);
2293 	}
2294 }
2295 
2296 void ath10k_offchan_tx_purge(struct ath10k *ar)
2297 {
2298 	struct sk_buff *skb;
2299 
2300 	for (;;) {
2301 		skb = skb_dequeue(&ar->offchan_tx_queue);
2302 		if (!skb)
2303 			break;
2304 
2305 		ieee80211_free_txskb(ar->hw, skb);
2306 	}
2307 }
2308 
2309 void ath10k_offchan_tx_work(struct work_struct *work)
2310 {
2311 	struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
2312 	struct ath10k_peer *peer;
2313 	struct ieee80211_hdr *hdr;
2314 	struct sk_buff *skb;
2315 	const u8 *peer_addr;
2316 	int vdev_id;
2317 	int ret;
2318 
2319 	/* FW requirement: We must create a peer before FW will send out
2320 	 * an offchannel frame. Otherwise the frame will be stuck and
2321 	 * never transmitted. We delete the peer upon tx completion.
2322 	 * It is unlikely that a peer for offchannel tx will already be
2323 	 * present. However it may be in some rare cases so account for that.
2324 	 * Otherwise we might remove a legitimate peer and break stuff. */
2325 
2326 	for (;;) {
2327 		skb = skb_dequeue(&ar->offchan_tx_queue);
2328 		if (!skb)
2329 			break;
2330 
2331 		mutex_lock(&ar->conf_mutex);
2332 
2333 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac offchannel skb %p\n",
2334 			   skb);
2335 
2336 		hdr = (struct ieee80211_hdr *)skb->data;
2337 		peer_addr = ieee80211_get_DA(hdr);
2338 		vdev_id = ATH10K_SKB_CB(skb)->vdev_id;
2339 
2340 		spin_lock_bh(&ar->data_lock);
2341 		peer = ath10k_peer_find(ar, vdev_id, peer_addr);
2342 		spin_unlock_bh(&ar->data_lock);
2343 
2344 		if (peer)
2345 			/* FIXME: should this use ath10k_warn()? */
2346 			ath10k_dbg(ar, ATH10K_DBG_MAC, "peer %pM on vdev %d already present\n",
2347 				   peer_addr, vdev_id);
2348 
2349 		if (!peer) {
2350 			ret = ath10k_peer_create(ar, vdev_id, peer_addr);
2351 			if (ret)
2352 				ath10k_warn(ar, "failed to create peer %pM on vdev %d: %d\n",
2353 					    peer_addr, vdev_id, ret);
2354 		}
2355 
2356 		spin_lock_bh(&ar->data_lock);
2357 		reinit_completion(&ar->offchan_tx_completed);
2358 		ar->offchan_tx_skb = skb;
2359 		spin_unlock_bh(&ar->data_lock);
2360 
2361 		ath10k_tx_htt(ar, skb);
2362 
2363 		ret = wait_for_completion_timeout(&ar->offchan_tx_completed,
2364 						  3 * HZ);
2365 		if (ret == 0)
2366 			ath10k_warn(ar, "timed out waiting for offchannel skb %p\n",
2367 				    skb);
2368 
2369 		if (!peer) {
2370 			ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
2371 			if (ret)
2372 				ath10k_warn(ar, "failed to delete peer %pM on vdev %d: %d\n",
2373 					    peer_addr, vdev_id, ret);
2374 		}
2375 
2376 		mutex_unlock(&ar->conf_mutex);
2377 	}
2378 }
2379 
2380 void ath10k_mgmt_over_wmi_tx_purge(struct ath10k *ar)
2381 {
2382 	struct sk_buff *skb;
2383 
2384 	for (;;) {
2385 		skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
2386 		if (!skb)
2387 			break;
2388 
2389 		ieee80211_free_txskb(ar->hw, skb);
2390 	}
2391 }
2392 
2393 void ath10k_mgmt_over_wmi_tx_work(struct work_struct *work)
2394 {
2395 	struct ath10k *ar = container_of(work, struct ath10k, wmi_mgmt_tx_work);
2396 	struct sk_buff *skb;
2397 	int ret;
2398 
2399 	for (;;) {
2400 		skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
2401 		if (!skb)
2402 			break;
2403 
2404 		ret = ath10k_wmi_mgmt_tx(ar, skb);
2405 		if (ret) {
2406 			ath10k_warn(ar, "failed to transmit management frame via WMI: %d\n",
2407 				    ret);
2408 			ieee80211_free_txskb(ar->hw, skb);
2409 		}
2410 	}
2411 }
2412 
2413 /************/
2414 /* Scanning */
2415 /************/
2416 
2417 void __ath10k_scan_finish(struct ath10k *ar)
2418 {
2419 	lockdep_assert_held(&ar->data_lock);
2420 
2421 	switch (ar->scan.state) {
2422 	case ATH10K_SCAN_IDLE:
2423 		break;
2424 	case ATH10K_SCAN_RUNNING:
2425 		if (ar->scan.is_roc)
2426 			ieee80211_remain_on_channel_expired(ar->hw);
2427 		/* fall through */
2428 	case ATH10K_SCAN_ABORTING:
2429 		if (!ar->scan.is_roc)
2430 			ieee80211_scan_completed(ar->hw,
2431 						 (ar->scan.state ==
2432 						  ATH10K_SCAN_ABORTING));
2433 		/* fall through */
2434 	case ATH10K_SCAN_STARTING:
2435 		ar->scan.state = ATH10K_SCAN_IDLE;
2436 		ar->scan_channel = NULL;
2437 		ath10k_offchan_tx_purge(ar);
2438 		cancel_delayed_work(&ar->scan.timeout);
2439 		complete_all(&ar->scan.completed);
2440 		break;
2441 	}
2442 }
2443 
2444 void ath10k_scan_finish(struct ath10k *ar)
2445 {
2446 	spin_lock_bh(&ar->data_lock);
2447 	__ath10k_scan_finish(ar);
2448 	spin_unlock_bh(&ar->data_lock);
2449 }
2450 
2451 static int ath10k_scan_stop(struct ath10k *ar)
2452 {
2453 	struct wmi_stop_scan_arg arg = {
2454 		.req_id = 1, /* FIXME */
2455 		.req_type = WMI_SCAN_STOP_ONE,
2456 		.u.scan_id = ATH10K_SCAN_ID,
2457 	};
2458 	int ret;
2459 
2460 	lockdep_assert_held(&ar->conf_mutex);
2461 
2462 	ret = ath10k_wmi_stop_scan(ar, &arg);
2463 	if (ret) {
2464 		ath10k_warn(ar, "failed to stop wmi scan: %d\n", ret);
2465 		goto out;
2466 	}
2467 
2468 	ret = wait_for_completion_timeout(&ar->scan.completed, 3*HZ);
2469 	if (ret == 0) {
2470 		ath10k_warn(ar, "failed to receive scan abortion completion: timed out\n");
2471 		ret = -ETIMEDOUT;
2472 	} else if (ret > 0) {
2473 		ret = 0;
2474 	}
2475 
2476 out:
2477 	/* Scan state should be updated upon scan completion but in case
2478 	 * firmware fails to deliver the event (for whatever reason) it is
2479 	 * desired to clean up scan state anyway. Firmware may have just
2480 	 * dropped the scan completion event delivery due to transport pipe
2481 	 * being overflown with data and/or it can recover on its own before
2482 	 * next scan request is submitted.
2483 	 */
2484 	spin_lock_bh(&ar->data_lock);
2485 	if (ar->scan.state != ATH10K_SCAN_IDLE)
2486 		__ath10k_scan_finish(ar);
2487 	spin_unlock_bh(&ar->data_lock);
2488 
2489 	return ret;
2490 }
2491 
2492 static void ath10k_scan_abort(struct ath10k *ar)
2493 {
2494 	int ret;
2495 
2496 	lockdep_assert_held(&ar->conf_mutex);
2497 
2498 	spin_lock_bh(&ar->data_lock);
2499 
2500 	switch (ar->scan.state) {
2501 	case ATH10K_SCAN_IDLE:
2502 		/* This can happen if timeout worker kicked in and called
2503 		 * abortion while scan completion was being processed.
2504 		 */
2505 		break;
2506 	case ATH10K_SCAN_STARTING:
2507 	case ATH10K_SCAN_ABORTING:
2508 		ath10k_warn(ar, "refusing scan abortion due to invalid scan state: %s (%d)\n",
2509 			    ath10k_scan_state_str(ar->scan.state),
2510 			    ar->scan.state);
2511 		break;
2512 	case ATH10K_SCAN_RUNNING:
2513 		ar->scan.state = ATH10K_SCAN_ABORTING;
2514 		spin_unlock_bh(&ar->data_lock);
2515 
2516 		ret = ath10k_scan_stop(ar);
2517 		if (ret)
2518 			ath10k_warn(ar, "failed to abort scan: %d\n", ret);
2519 
2520 		spin_lock_bh(&ar->data_lock);
2521 		break;
2522 	}
2523 
2524 	spin_unlock_bh(&ar->data_lock);
2525 }
2526 
2527 void ath10k_scan_timeout_work(struct work_struct *work)
2528 {
2529 	struct ath10k *ar = container_of(work, struct ath10k,
2530 					 scan.timeout.work);
2531 
2532 	mutex_lock(&ar->conf_mutex);
2533 	ath10k_scan_abort(ar);
2534 	mutex_unlock(&ar->conf_mutex);
2535 }
2536 
2537 static int ath10k_start_scan(struct ath10k *ar,
2538 			     const struct wmi_start_scan_arg *arg)
2539 {
2540 	int ret;
2541 
2542 	lockdep_assert_held(&ar->conf_mutex);
2543 
2544 	ret = ath10k_wmi_start_scan(ar, arg);
2545 	if (ret)
2546 		return ret;
2547 
2548 	ret = wait_for_completion_timeout(&ar->scan.started, 1*HZ);
2549 	if (ret == 0) {
2550 		ret = ath10k_scan_stop(ar);
2551 		if (ret)
2552 			ath10k_warn(ar, "failed to stop scan: %d\n", ret);
2553 
2554 		return -ETIMEDOUT;
2555 	}
2556 
2557 	/* Add a 200ms margin to account for event/command processing */
2558 	ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
2559 				     msecs_to_jiffies(arg->max_scan_time+200));
2560 	return 0;
2561 }
2562 
2563 /**********************/
2564 /* mac80211 callbacks */
2565 /**********************/
2566 
2567 static void ath10k_tx(struct ieee80211_hw *hw,
2568 		      struct ieee80211_tx_control *control,
2569 		      struct sk_buff *skb)
2570 {
2571 	struct ath10k *ar = hw->priv;
2572 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2573 	struct ieee80211_vif *vif = info->control.vif;
2574 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2575 
2576 	/* We should disable CCK RATE due to P2P */
2577 	if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
2578 		ath10k_dbg(ar, ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
2579 
2580 	ATH10K_SKB_CB(skb)->htt.is_offchan = false;
2581 	ATH10K_SKB_CB(skb)->htt.tid = ath10k_tx_h_get_tid(hdr);
2582 	ATH10K_SKB_CB(skb)->vdev_id = ath10k_tx_h_get_vdev_id(ar, vif);
2583 
2584 	/* it makes no sense to process injected frames like that */
2585 	if (vif && vif->type != NL80211_IFTYPE_MONITOR) {
2586 		ath10k_tx_h_nwifi(hw, skb);
2587 		ath10k_tx_h_add_p2p_noa_ie(ar, vif, skb);
2588 		ath10k_tx_h_seq_no(vif, skb);
2589 	}
2590 
2591 	if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
2592 		spin_lock_bh(&ar->data_lock);
2593 		ATH10K_SKB_CB(skb)->htt.freq = ar->scan.roc_freq;
2594 		ATH10K_SKB_CB(skb)->vdev_id = ar->scan.vdev_id;
2595 		spin_unlock_bh(&ar->data_lock);
2596 
2597 		if (ath10k_mac_need_offchan_tx_work(ar)) {
2598 			ATH10K_SKB_CB(skb)->htt.freq = 0;
2599 			ATH10K_SKB_CB(skb)->htt.is_offchan = true;
2600 
2601 			ath10k_dbg(ar, ATH10K_DBG_MAC, "queued offchannel skb %p\n",
2602 				   skb);
2603 
2604 			skb_queue_tail(&ar->offchan_tx_queue, skb);
2605 			ieee80211_queue_work(hw, &ar->offchan_tx_work);
2606 			return;
2607 		}
2608 	}
2609 
2610 	ath10k_tx_htt(ar, skb);
2611 }
2612 
2613 /* Must not be called with conf_mutex held as workers can use that also. */
2614 void ath10k_drain_tx(struct ath10k *ar)
2615 {
2616 	/* make sure rcu-protected mac80211 tx path itself is drained */
2617 	synchronize_net();
2618 
2619 	ath10k_offchan_tx_purge(ar);
2620 	ath10k_mgmt_over_wmi_tx_purge(ar);
2621 
2622 	cancel_work_sync(&ar->offchan_tx_work);
2623 	cancel_work_sync(&ar->wmi_mgmt_tx_work);
2624 }
2625 
2626 void ath10k_halt(struct ath10k *ar)
2627 {
2628 	struct ath10k_vif *arvif;
2629 
2630 	lockdep_assert_held(&ar->conf_mutex);
2631 
2632 	clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
2633 	ar->filter_flags = 0;
2634 	ar->monitor = false;
2635 
2636 	if (ar->monitor_started)
2637 		ath10k_monitor_stop(ar);
2638 
2639 	ar->monitor_started = false;
2640 
2641 	ath10k_scan_finish(ar);
2642 	ath10k_peer_cleanup_all(ar);
2643 	ath10k_core_stop(ar);
2644 	ath10k_hif_power_down(ar);
2645 
2646 	spin_lock_bh(&ar->data_lock);
2647 	list_for_each_entry(arvif, &ar->arvifs, list)
2648 		ath10k_mac_vif_beacon_cleanup(arvif);
2649 	spin_unlock_bh(&ar->data_lock);
2650 }
2651 
2652 static int ath10k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
2653 {
2654 	struct ath10k *ar = hw->priv;
2655 
2656 	mutex_lock(&ar->conf_mutex);
2657 
2658 	if (ar->cfg_tx_chainmask) {
2659 		*tx_ant = ar->cfg_tx_chainmask;
2660 		*rx_ant = ar->cfg_rx_chainmask;
2661 	} else {
2662 		*tx_ant = ar->supp_tx_chainmask;
2663 		*rx_ant = ar->supp_rx_chainmask;
2664 	}
2665 
2666 	mutex_unlock(&ar->conf_mutex);
2667 
2668 	return 0;
2669 }
2670 
2671 static void ath10k_check_chain_mask(struct ath10k *ar, u32 cm, const char *dbg)
2672 {
2673 	/* It is not clear that allowing gaps in chainmask
2674 	 * is helpful.  Probably it will not do what user
2675 	 * is hoping for, so warn in that case.
2676 	 */
2677 	if (cm == 15 || cm == 7 || cm == 3 || cm == 1 || cm == 0)
2678 		return;
2679 
2680 	ath10k_warn(ar, "mac %s antenna chainmask may be invalid: 0x%x.  Suggested values: 15, 7, 3, 1 or 0.\n",
2681 		    dbg, cm);
2682 }
2683 
2684 static int __ath10k_set_antenna(struct ath10k *ar, u32 tx_ant, u32 rx_ant)
2685 {
2686 	int ret;
2687 
2688 	lockdep_assert_held(&ar->conf_mutex);
2689 
2690 	ath10k_check_chain_mask(ar, tx_ant, "tx");
2691 	ath10k_check_chain_mask(ar, rx_ant, "rx");
2692 
2693 	ar->cfg_tx_chainmask = tx_ant;
2694 	ar->cfg_rx_chainmask = rx_ant;
2695 
2696 	if ((ar->state != ATH10K_STATE_ON) &&
2697 	    (ar->state != ATH10K_STATE_RESTARTED))
2698 		return 0;
2699 
2700 	ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->tx_chain_mask,
2701 					tx_ant);
2702 	if (ret) {
2703 		ath10k_warn(ar, "failed to set tx-chainmask: %d, req 0x%x\n",
2704 			    ret, tx_ant);
2705 		return ret;
2706 	}
2707 
2708 	ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->rx_chain_mask,
2709 					rx_ant);
2710 	if (ret) {
2711 		ath10k_warn(ar, "failed to set rx-chainmask: %d, req 0x%x\n",
2712 			    ret, rx_ant);
2713 		return ret;
2714 	}
2715 
2716 	return 0;
2717 }
2718 
2719 static int ath10k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
2720 {
2721 	struct ath10k *ar = hw->priv;
2722 	int ret;
2723 
2724 	mutex_lock(&ar->conf_mutex);
2725 	ret = __ath10k_set_antenna(ar, tx_ant, rx_ant);
2726 	mutex_unlock(&ar->conf_mutex);
2727 	return ret;
2728 }
2729 
2730 static int ath10k_start(struct ieee80211_hw *hw)
2731 {
2732 	struct ath10k *ar = hw->priv;
2733 	int ret = 0;
2734 
2735 	/*
2736 	 * This makes sense only when restarting hw. It is harmless to call
2737 	 * uncoditionally. This is necessary to make sure no HTT/WMI tx
2738 	 * commands will be submitted while restarting.
2739 	 */
2740 	ath10k_drain_tx(ar);
2741 
2742 	mutex_lock(&ar->conf_mutex);
2743 
2744 	switch (ar->state) {
2745 	case ATH10K_STATE_OFF:
2746 		ar->state = ATH10K_STATE_ON;
2747 		break;
2748 	case ATH10K_STATE_RESTARTING:
2749 		ath10k_halt(ar);
2750 		ar->state = ATH10K_STATE_RESTARTED;
2751 		break;
2752 	case ATH10K_STATE_ON:
2753 	case ATH10K_STATE_RESTARTED:
2754 	case ATH10K_STATE_WEDGED:
2755 		WARN_ON(1);
2756 		ret = -EINVAL;
2757 		goto err;
2758 	case ATH10K_STATE_UTF:
2759 		ret = -EBUSY;
2760 		goto err;
2761 	}
2762 
2763 	ret = ath10k_hif_power_up(ar);
2764 	if (ret) {
2765 		ath10k_err(ar, "Could not init hif: %d\n", ret);
2766 		goto err_off;
2767 	}
2768 
2769 	ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_NORMAL);
2770 	if (ret) {
2771 		ath10k_err(ar, "Could not init core: %d\n", ret);
2772 		goto err_power_down;
2773 	}
2774 
2775 	ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->pmf_qos, 1);
2776 	if (ret) {
2777 		ath10k_warn(ar, "failed to enable PMF QOS: %d\n", ret);
2778 		goto err_core_stop;
2779 	}
2780 
2781 	ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->dynamic_bw, 1);
2782 	if (ret) {
2783 		ath10k_warn(ar, "failed to enable dynamic BW: %d\n", ret);
2784 		goto err_core_stop;
2785 	}
2786 
2787 	if (ar->cfg_tx_chainmask)
2788 		__ath10k_set_antenna(ar, ar->cfg_tx_chainmask,
2789 				     ar->cfg_rx_chainmask);
2790 
2791 	/*
2792 	 * By default FW set ARP frames ac to voice (6). In that case ARP
2793 	 * exchange is not working properly for UAPSD enabled AP. ARP requests
2794 	 * which arrives with access category 0 are processed by network stack
2795 	 * and send back with access category 0, but FW changes access category
2796 	 * to 6. Set ARP frames access category to best effort (0) solves
2797 	 * this problem.
2798 	 */
2799 
2800 	ret = ath10k_wmi_pdev_set_param(ar,
2801 					ar->wmi.pdev_param->arp_ac_override, 0);
2802 	if (ret) {
2803 		ath10k_warn(ar, "failed to set arp ac override parameter: %d\n",
2804 			    ret);
2805 		goto err_core_stop;
2806 	}
2807 
2808 	ar->num_started_vdevs = 0;
2809 	ath10k_regd_update(ar);
2810 
2811 	ath10k_spectral_start(ar);
2812 
2813 	mutex_unlock(&ar->conf_mutex);
2814 	return 0;
2815 
2816 err_core_stop:
2817 	ath10k_core_stop(ar);
2818 
2819 err_power_down:
2820 	ath10k_hif_power_down(ar);
2821 
2822 err_off:
2823 	ar->state = ATH10K_STATE_OFF;
2824 
2825 err:
2826 	mutex_unlock(&ar->conf_mutex);
2827 	return ret;
2828 }
2829 
2830 static void ath10k_stop(struct ieee80211_hw *hw)
2831 {
2832 	struct ath10k *ar = hw->priv;
2833 
2834 	ath10k_drain_tx(ar);
2835 
2836 	mutex_lock(&ar->conf_mutex);
2837 	if (ar->state != ATH10K_STATE_OFF) {
2838 		ath10k_halt(ar);
2839 		ar->state = ATH10K_STATE_OFF;
2840 	}
2841 	mutex_unlock(&ar->conf_mutex);
2842 
2843 	cancel_delayed_work_sync(&ar->scan.timeout);
2844 	cancel_work_sync(&ar->restart_work);
2845 }
2846 
2847 static int ath10k_config_ps(struct ath10k *ar)
2848 {
2849 	struct ath10k_vif *arvif;
2850 	int ret = 0;
2851 
2852 	lockdep_assert_held(&ar->conf_mutex);
2853 
2854 	list_for_each_entry(arvif, &ar->arvifs, list) {
2855 		ret = ath10k_mac_vif_setup_ps(arvif);
2856 		if (ret) {
2857 			ath10k_warn(ar, "failed to setup powersave: %d\n", ret);
2858 			break;
2859 		}
2860 	}
2861 
2862 	return ret;
2863 }
2864 
2865 static const char *chandef_get_width(enum nl80211_chan_width width)
2866 {
2867 	switch (width) {
2868 	case NL80211_CHAN_WIDTH_20_NOHT:
2869 		return "20 (noht)";
2870 	case NL80211_CHAN_WIDTH_20:
2871 		return "20";
2872 	case NL80211_CHAN_WIDTH_40:
2873 		return "40";
2874 	case NL80211_CHAN_WIDTH_80:
2875 		return "80";
2876 	case NL80211_CHAN_WIDTH_80P80:
2877 		return "80+80";
2878 	case NL80211_CHAN_WIDTH_160:
2879 		return "160";
2880 	case NL80211_CHAN_WIDTH_5:
2881 		return "5";
2882 	case NL80211_CHAN_WIDTH_10:
2883 		return "10";
2884 	}
2885 	return "?";
2886 }
2887 
2888 static void ath10k_config_chan(struct ath10k *ar)
2889 {
2890 	struct ath10k_vif *arvif;
2891 	int ret;
2892 
2893 	lockdep_assert_held(&ar->conf_mutex);
2894 
2895 	ath10k_dbg(ar, ATH10K_DBG_MAC,
2896 		   "mac config channel to %dMHz (cf1 %dMHz cf2 %dMHz width %s)\n",
2897 		   ar->chandef.chan->center_freq,
2898 		   ar->chandef.center_freq1,
2899 		   ar->chandef.center_freq2,
2900 		   chandef_get_width(ar->chandef.width));
2901 
2902 	/* First stop monitor interface. Some FW versions crash if there's a
2903 	 * lone monitor interface. */
2904 	if (ar->monitor_started)
2905 		ath10k_monitor_stop(ar);
2906 
2907 	list_for_each_entry(arvif, &ar->arvifs, list) {
2908 		if (!arvif->is_started)
2909 			continue;
2910 
2911 		if (!arvif->is_up)
2912 			continue;
2913 
2914 		if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
2915 			continue;
2916 
2917 		ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
2918 		if (ret) {
2919 			ath10k_warn(ar, "failed to down vdev %d: %d\n",
2920 				    arvif->vdev_id, ret);
2921 			continue;
2922 		}
2923 	}
2924 
2925 	/* all vdevs are downed now - attempt to restart and re-up them */
2926 
2927 	list_for_each_entry(arvif, &ar->arvifs, list) {
2928 		if (!arvif->is_started)
2929 			continue;
2930 
2931 		if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
2932 			continue;
2933 
2934 		ret = ath10k_vdev_restart(arvif);
2935 		if (ret) {
2936 			ath10k_warn(ar, "failed to restart vdev %d: %d\n",
2937 				    arvif->vdev_id, ret);
2938 			continue;
2939 		}
2940 
2941 		if (!arvif->is_up)
2942 			continue;
2943 
2944 		ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
2945 					 arvif->bssid);
2946 		if (ret) {
2947 			ath10k_warn(ar, "failed to bring vdev up %d: %d\n",
2948 				    arvif->vdev_id, ret);
2949 			continue;
2950 		}
2951 	}
2952 
2953 	ath10k_monitor_recalc(ar);
2954 }
2955 
2956 static int ath10k_mac_txpower_setup(struct ath10k *ar, int txpower)
2957 {
2958 	int ret;
2959 	u32 param;
2960 
2961 	lockdep_assert_held(&ar->conf_mutex);
2962 
2963 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac txpower %d\n", txpower);
2964 
2965 	param = ar->wmi.pdev_param->txpower_limit2g;
2966 	ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
2967 	if (ret) {
2968 		ath10k_warn(ar, "failed to set 2g txpower %d: %d\n",
2969 			    txpower, ret);
2970 		return ret;
2971 	}
2972 
2973 	param = ar->wmi.pdev_param->txpower_limit5g;
2974 	ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
2975 	if (ret) {
2976 		ath10k_warn(ar, "failed to set 5g txpower %d: %d\n",
2977 			    txpower, ret);
2978 		return ret;
2979 	}
2980 
2981 	return 0;
2982 }
2983 
2984 static int ath10k_mac_txpower_recalc(struct ath10k *ar)
2985 {
2986 	struct ath10k_vif *arvif;
2987 	int ret, txpower = -1;
2988 
2989 	lockdep_assert_held(&ar->conf_mutex);
2990 
2991 	list_for_each_entry(arvif, &ar->arvifs, list) {
2992 		WARN_ON(arvif->txpower < 0);
2993 
2994 		if (txpower == -1)
2995 			txpower = arvif->txpower;
2996 		else
2997 			txpower = min(txpower, arvif->txpower);
2998 	}
2999 
3000 	if (WARN_ON(txpower == -1))
3001 		return -EINVAL;
3002 
3003 	ret = ath10k_mac_txpower_setup(ar, txpower);
3004 	if (ret) {
3005 		ath10k_warn(ar, "failed to setup tx power %d: %d\n",
3006 			    txpower, ret);
3007 		return ret;
3008 	}
3009 
3010 	return 0;
3011 }
3012 
3013 static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
3014 {
3015 	struct ath10k *ar = hw->priv;
3016 	struct ieee80211_conf *conf = &hw->conf;
3017 	int ret = 0;
3018 
3019 	mutex_lock(&ar->conf_mutex);
3020 
3021 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
3022 		ath10k_dbg(ar, ATH10K_DBG_MAC,
3023 			   "mac config channel %dMHz flags 0x%x radar %d\n",
3024 			   conf->chandef.chan->center_freq,
3025 			   conf->chandef.chan->flags,
3026 			   conf->radar_enabled);
3027 
3028 		spin_lock_bh(&ar->data_lock);
3029 		ar->rx_channel = conf->chandef.chan;
3030 		spin_unlock_bh(&ar->data_lock);
3031 
3032 		ar->radar_enabled = conf->radar_enabled;
3033 		ath10k_recalc_radar_detection(ar);
3034 
3035 		if (!cfg80211_chandef_identical(&ar->chandef, &conf->chandef)) {
3036 			ar->chandef = conf->chandef;
3037 			ath10k_config_chan(ar);
3038 		}
3039 	}
3040 
3041 	if (changed & IEEE80211_CONF_CHANGE_PS)
3042 		ath10k_config_ps(ar);
3043 
3044 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
3045 		ar->monitor = conf->flags & IEEE80211_CONF_MONITOR;
3046 		ret = ath10k_monitor_recalc(ar);
3047 		if (ret)
3048 			ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
3049 	}
3050 
3051 	mutex_unlock(&ar->conf_mutex);
3052 	return ret;
3053 }
3054 
3055 static u32 get_nss_from_chainmask(u16 chain_mask)
3056 {
3057 	if ((chain_mask & 0x15) == 0x15)
3058 		return 4;
3059 	else if ((chain_mask & 0x7) == 0x7)
3060 		return 3;
3061 	else if ((chain_mask & 0x3) == 0x3)
3062 		return 2;
3063 	return 1;
3064 }
3065 
3066 /*
3067  * TODO:
3068  * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
3069  * because we will send mgmt frames without CCK. This requirement
3070  * for P2P_FIND/GO_NEG should be handled by checking CCK flag
3071  * in the TX packet.
3072  */
3073 static int ath10k_add_interface(struct ieee80211_hw *hw,
3074 				struct ieee80211_vif *vif)
3075 {
3076 	struct ath10k *ar = hw->priv;
3077 	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3078 	enum wmi_sta_powersave_param param;
3079 	int ret = 0;
3080 	u32 value;
3081 	int bit;
3082 	u32 vdev_param;
3083 
3084 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
3085 
3086 	mutex_lock(&ar->conf_mutex);
3087 
3088 	memset(arvif, 0, sizeof(*arvif));
3089 
3090 	arvif->ar = ar;
3091 	arvif->vif = vif;
3092 
3093 	INIT_LIST_HEAD(&arvif->list);
3094 
3095 	if (ar->free_vdev_map == 0) {
3096 		ath10k_warn(ar, "Free vdev map is empty, no more interfaces allowed.\n");
3097 		ret = -EBUSY;
3098 		goto err;
3099 	}
3100 	bit = __ffs64(ar->free_vdev_map);
3101 
3102 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac create vdev %i map %llx\n",
3103 		   bit, ar->free_vdev_map);
3104 
3105 	arvif->vdev_id = bit;
3106 	arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
3107 
3108 	switch (vif->type) {
3109 	case NL80211_IFTYPE_P2P_DEVICE:
3110 		arvif->vdev_type = WMI_VDEV_TYPE_STA;
3111 		arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
3112 		break;
3113 	case NL80211_IFTYPE_UNSPECIFIED:
3114 	case NL80211_IFTYPE_STATION:
3115 		arvif->vdev_type = WMI_VDEV_TYPE_STA;
3116 		if (vif->p2p)
3117 			arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
3118 		break;
3119 	case NL80211_IFTYPE_ADHOC:
3120 		arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
3121 		break;
3122 	case NL80211_IFTYPE_AP:
3123 		arvif->vdev_type = WMI_VDEV_TYPE_AP;
3124 
3125 		if (vif->p2p)
3126 			arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
3127 		break;
3128 	case NL80211_IFTYPE_MONITOR:
3129 		arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
3130 		break;
3131 	default:
3132 		WARN_ON(1);
3133 		break;
3134 	}
3135 
3136 	/* Some firmware revisions don't wait for beacon tx completion before
3137 	 * sending another SWBA event. This could lead to hardware using old
3138 	 * (freed) beacon data in some cases, e.g. tx credit starvation
3139 	 * combined with missed TBTT. This is very very rare.
3140 	 *
3141 	 * On non-IOMMU-enabled hosts this could be a possible security issue
3142 	 * because hw could beacon some random data on the air.  On
3143 	 * IOMMU-enabled hosts DMAR faults would occur in most cases and target
3144 	 * device would crash.
3145 	 *
3146 	 * Since there are no beacon tx completions (implicit nor explicit)
3147 	 * propagated to host the only workaround for this is to allocate a
3148 	 * DMA-coherent buffer for a lifetime of a vif and use it for all
3149 	 * beacon tx commands. Worst case for this approach is some beacons may
3150 	 * become corrupted, e.g. have garbled IEs or out-of-date TIM bitmap.
3151 	 */
3152 	if (vif->type == NL80211_IFTYPE_ADHOC ||
3153 	    vif->type == NL80211_IFTYPE_AP) {
3154 		arvif->beacon_buf = dma_zalloc_coherent(ar->dev,
3155 							IEEE80211_MAX_FRAME_LEN,
3156 							&arvif->beacon_paddr,
3157 							GFP_ATOMIC);
3158 		if (!arvif->beacon_buf) {
3159 			ret = -ENOMEM;
3160 			ath10k_warn(ar, "failed to allocate beacon buffer: %d\n",
3161 				    ret);
3162 			goto err;
3163 		}
3164 	}
3165 
3166 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d bcnmode %s\n",
3167 		   arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
3168 		   arvif->beacon_buf ? "single-buf" : "per-skb");
3169 
3170 	ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
3171 				     arvif->vdev_subtype, vif->addr);
3172 	if (ret) {
3173 		ath10k_warn(ar, "failed to create WMI vdev %i: %d\n",
3174 			    arvif->vdev_id, ret);
3175 		goto err;
3176 	}
3177 
3178 	ar->free_vdev_map &= ~(1LL << arvif->vdev_id);
3179 	list_add(&arvif->list, &ar->arvifs);
3180 
3181 	/* It makes no sense to have firmware do keepalives. mac80211 already
3182 	 * takes care of this with idle connection polling.
3183 	 */
3184 	ret = ath10k_mac_vif_disable_keepalive(arvif);
3185 	if (ret) {
3186 		ath10k_warn(ar, "failed to disable keepalive on vdev %i: %d\n",
3187 			    arvif->vdev_id, ret);
3188 		goto err_vdev_delete;
3189 	}
3190 
3191 	arvif->def_wep_key_idx = -1;
3192 
3193 	vdev_param = ar->wmi.vdev_param->tx_encap_type;
3194 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3195 					ATH10K_HW_TXRX_NATIVE_WIFI);
3196 	/* 10.X firmware does not support this VDEV parameter. Do not warn */
3197 	if (ret && ret != -EOPNOTSUPP) {
3198 		ath10k_warn(ar, "failed to set vdev %i TX encapsulation: %d\n",
3199 			    arvif->vdev_id, ret);
3200 		goto err_vdev_delete;
3201 	}
3202 
3203 	if (ar->cfg_tx_chainmask) {
3204 		u16 nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
3205 
3206 		vdev_param = ar->wmi.vdev_param->nss;
3207 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3208 						nss);
3209 		if (ret) {
3210 			ath10k_warn(ar, "failed to set vdev %i chainmask 0x%x, nss %i: %d\n",
3211 				    arvif->vdev_id, ar->cfg_tx_chainmask, nss,
3212 				    ret);
3213 			goto err_vdev_delete;
3214 		}
3215 	}
3216 
3217 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3218 		ret = ath10k_peer_create(ar, arvif->vdev_id, vif->addr);
3219 		if (ret) {
3220 			ath10k_warn(ar, "failed to create vdev %i peer for AP: %d\n",
3221 				    arvif->vdev_id, ret);
3222 			goto err_vdev_delete;
3223 		}
3224 
3225 		ret = ath10k_mac_set_kickout(arvif);
3226 		if (ret) {
3227 			ath10k_warn(ar, "failed to set vdev %i kickout parameters: %d\n",
3228 				    arvif->vdev_id, ret);
3229 			goto err_peer_delete;
3230 		}
3231 	}
3232 
3233 	if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
3234 		param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
3235 		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
3236 		ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3237 						  param, value);
3238 		if (ret) {
3239 			ath10k_warn(ar, "failed to set vdev %i RX wake policy: %d\n",
3240 				    arvif->vdev_id, ret);
3241 			goto err_peer_delete;
3242 		}
3243 
3244 		ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
3245 		if (ret) {
3246 			ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
3247 				    arvif->vdev_id, ret);
3248 			goto err_peer_delete;
3249 		}
3250 
3251 		ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
3252 		if (ret) {
3253 			ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
3254 				    arvif->vdev_id, ret);
3255 			goto err_peer_delete;
3256 		}
3257 	}
3258 
3259 	ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
3260 	if (ret) {
3261 		ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
3262 			    arvif->vdev_id, ret);
3263 		goto err_peer_delete;
3264 	}
3265 
3266 	ret = ath10k_mac_set_frag(arvif, ar->hw->wiphy->frag_threshold);
3267 	if (ret) {
3268 		ath10k_warn(ar, "failed to set frag threshold for vdev %d: %d\n",
3269 			    arvif->vdev_id, ret);
3270 		goto err_peer_delete;
3271 	}
3272 
3273 	arvif->txpower = vif->bss_conf.txpower;
3274 	ret = ath10k_mac_txpower_recalc(ar);
3275 	if (ret) {
3276 		ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
3277 		goto err_peer_delete;
3278 	}
3279 
3280 	mutex_unlock(&ar->conf_mutex);
3281 	return 0;
3282 
3283 err_peer_delete:
3284 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP)
3285 		ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
3286 
3287 err_vdev_delete:
3288 	ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
3289 	ar->free_vdev_map |= 1LL << arvif->vdev_id;
3290 	list_del(&arvif->list);
3291 
3292 err:
3293 	if (arvif->beacon_buf) {
3294 		dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
3295 				  arvif->beacon_buf, arvif->beacon_paddr);
3296 		arvif->beacon_buf = NULL;
3297 	}
3298 
3299 	mutex_unlock(&ar->conf_mutex);
3300 
3301 	return ret;
3302 }
3303 
3304 static void ath10k_remove_interface(struct ieee80211_hw *hw,
3305 				    struct ieee80211_vif *vif)
3306 {
3307 	struct ath10k *ar = hw->priv;
3308 	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3309 	int ret;
3310 
3311 	mutex_lock(&ar->conf_mutex);
3312 
3313 	spin_lock_bh(&ar->data_lock);
3314 	ath10k_mac_vif_beacon_cleanup(arvif);
3315 	spin_unlock_bh(&ar->data_lock);
3316 
3317 	ret = ath10k_spectral_vif_stop(arvif);
3318 	if (ret)
3319 		ath10k_warn(ar, "failed to stop spectral for vdev %i: %d\n",
3320 			    arvif->vdev_id, ret);
3321 
3322 	ar->free_vdev_map |= 1LL << arvif->vdev_id;
3323 	list_del(&arvif->list);
3324 
3325 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3326 		ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, vif->addr);
3327 		if (ret)
3328 			ath10k_warn(ar, "failed to remove peer for AP vdev %i: %d\n",
3329 				    arvif->vdev_id, ret);
3330 
3331 		kfree(arvif->u.ap.noa_data);
3332 	}
3333 
3334 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i delete (remove interface)\n",
3335 		   arvif->vdev_id);
3336 
3337 	ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
3338 	if (ret)
3339 		ath10k_warn(ar, "failed to delete WMI vdev %i: %d\n",
3340 			    arvif->vdev_id, ret);
3341 
3342 	ath10k_peer_cleanup(ar, arvif->vdev_id);
3343 
3344 	mutex_unlock(&ar->conf_mutex);
3345 }
3346 
3347 /*
3348  * FIXME: Has to be verified.
3349  */
3350 #define SUPPORTED_FILTERS			\
3351 	(FIF_PROMISC_IN_BSS |			\
3352 	FIF_ALLMULTI |				\
3353 	FIF_CONTROL |				\
3354 	FIF_PSPOLL |				\
3355 	FIF_OTHER_BSS |				\
3356 	FIF_BCN_PRBRESP_PROMISC |		\
3357 	FIF_PROBE_REQ |				\
3358 	FIF_FCSFAIL)
3359 
3360 static void ath10k_configure_filter(struct ieee80211_hw *hw,
3361 				    unsigned int changed_flags,
3362 				    unsigned int *total_flags,
3363 				    u64 multicast)
3364 {
3365 	struct ath10k *ar = hw->priv;
3366 	int ret;
3367 
3368 	mutex_lock(&ar->conf_mutex);
3369 
3370 	changed_flags &= SUPPORTED_FILTERS;
3371 	*total_flags &= SUPPORTED_FILTERS;
3372 	ar->filter_flags = *total_flags;
3373 
3374 	ret = ath10k_monitor_recalc(ar);
3375 	if (ret)
3376 		ath10k_warn(ar, "failed to recalc montior: %d\n", ret);
3377 
3378 	mutex_unlock(&ar->conf_mutex);
3379 }
3380 
3381 static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
3382 				    struct ieee80211_vif *vif,
3383 				    struct ieee80211_bss_conf *info,
3384 				    u32 changed)
3385 {
3386 	struct ath10k *ar = hw->priv;
3387 	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3388 	int ret = 0;
3389 	u32 vdev_param, pdev_param, slottime, preamble;
3390 
3391 	mutex_lock(&ar->conf_mutex);
3392 
3393 	if (changed & BSS_CHANGED_IBSS)
3394 		ath10k_control_ibss(arvif, info, vif->addr);
3395 
3396 	if (changed & BSS_CHANGED_BEACON_INT) {
3397 		arvif->beacon_interval = info->beacon_int;
3398 		vdev_param = ar->wmi.vdev_param->beacon_interval;
3399 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3400 						arvif->beacon_interval);
3401 		ath10k_dbg(ar, ATH10K_DBG_MAC,
3402 			   "mac vdev %d beacon_interval %d\n",
3403 			   arvif->vdev_id, arvif->beacon_interval);
3404 
3405 		if (ret)
3406 			ath10k_warn(ar, "failed to set beacon interval for vdev %d: %i\n",
3407 				    arvif->vdev_id, ret);
3408 	}
3409 
3410 	if (changed & BSS_CHANGED_BEACON) {
3411 		ath10k_dbg(ar, ATH10K_DBG_MAC,
3412 			   "vdev %d set beacon tx mode to staggered\n",
3413 			   arvif->vdev_id);
3414 
3415 		pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
3416 		ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
3417 						WMI_BEACON_STAGGERED_MODE);
3418 		if (ret)
3419 			ath10k_warn(ar, "failed to set beacon mode for vdev %d: %i\n",
3420 				    arvif->vdev_id, ret);
3421 
3422 		ret = ath10k_mac_setup_bcn_tmpl(arvif);
3423 		if (ret)
3424 			ath10k_warn(ar, "failed to update beacon template: %d\n",
3425 				    ret);
3426 	}
3427 
3428 	if (changed & BSS_CHANGED_AP_PROBE_RESP) {
3429 		ret = ath10k_mac_setup_prb_tmpl(arvif);
3430 		if (ret)
3431 			ath10k_warn(ar, "failed to setup probe resp template on vdev %i: %d\n",
3432 				    arvif->vdev_id, ret);
3433 	}
3434 
3435 	if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
3436 		arvif->dtim_period = info->dtim_period;
3437 
3438 		ath10k_dbg(ar, ATH10K_DBG_MAC,
3439 			   "mac vdev %d dtim_period %d\n",
3440 			   arvif->vdev_id, arvif->dtim_period);
3441 
3442 		vdev_param = ar->wmi.vdev_param->dtim_period;
3443 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3444 						arvif->dtim_period);
3445 		if (ret)
3446 			ath10k_warn(ar, "failed to set dtim period for vdev %d: %i\n",
3447 				    arvif->vdev_id, ret);
3448 	}
3449 
3450 	if (changed & BSS_CHANGED_SSID &&
3451 	    vif->type == NL80211_IFTYPE_AP) {
3452 		arvif->u.ap.ssid_len = info->ssid_len;
3453 		if (info->ssid_len)
3454 			memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
3455 		arvif->u.ap.hidden_ssid = info->hidden_ssid;
3456 	}
3457 
3458 	if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
3459 		ether_addr_copy(arvif->bssid, info->bssid);
3460 
3461 	if (changed & BSS_CHANGED_BEACON_ENABLED)
3462 		ath10k_control_beaconing(arvif, info);
3463 
3464 	if (changed & BSS_CHANGED_ERP_CTS_PROT) {
3465 		arvif->use_cts_prot = info->use_cts_prot;
3466 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d cts_prot %d\n",
3467 			   arvif->vdev_id, info->use_cts_prot);
3468 
3469 		ret = ath10k_recalc_rtscts_prot(arvif);
3470 		if (ret)
3471 			ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
3472 				    arvif->vdev_id, ret);
3473 	}
3474 
3475 	if (changed & BSS_CHANGED_ERP_SLOT) {
3476 		if (info->use_short_slot)
3477 			slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
3478 
3479 		else
3480 			slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
3481 
3482 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
3483 			   arvif->vdev_id, slottime);
3484 
3485 		vdev_param = ar->wmi.vdev_param->slot_time;
3486 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3487 						slottime);
3488 		if (ret)
3489 			ath10k_warn(ar, "failed to set erp slot for vdev %d: %i\n",
3490 				    arvif->vdev_id, ret);
3491 	}
3492 
3493 	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
3494 		if (info->use_short_preamble)
3495 			preamble = WMI_VDEV_PREAMBLE_SHORT;
3496 		else
3497 			preamble = WMI_VDEV_PREAMBLE_LONG;
3498 
3499 		ath10k_dbg(ar, ATH10K_DBG_MAC,
3500 			   "mac vdev %d preamble %dn",
3501 			   arvif->vdev_id, preamble);
3502 
3503 		vdev_param = ar->wmi.vdev_param->preamble;
3504 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3505 						preamble);
3506 		if (ret)
3507 			ath10k_warn(ar, "failed to set preamble for vdev %d: %i\n",
3508 				    arvif->vdev_id, ret);
3509 	}
3510 
3511 	if (changed & BSS_CHANGED_ASSOC) {
3512 		if (info->assoc) {
3513 			/* Workaround: Make sure monitor vdev is not running
3514 			 * when associating to prevent some firmware revisions
3515 			 * (e.g. 10.1 and 10.2) from crashing.
3516 			 */
3517 			if (ar->monitor_started)
3518 				ath10k_monitor_stop(ar);
3519 			ath10k_bss_assoc(hw, vif, info);
3520 			ath10k_monitor_recalc(ar);
3521 		} else {
3522 			ath10k_bss_disassoc(hw, vif);
3523 		}
3524 	}
3525 
3526 	if (changed & BSS_CHANGED_TXPOWER) {
3527 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev_id %i txpower %d\n",
3528 			   arvif->vdev_id, info->txpower);
3529 
3530 		arvif->txpower = info->txpower;
3531 		ret = ath10k_mac_txpower_recalc(ar);
3532 		if (ret)
3533 			ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
3534 	}
3535 
3536 	if (changed & BSS_CHANGED_PS) {
3537 		ret = ath10k_mac_vif_setup_ps(arvif);
3538 		if (ret)
3539 			ath10k_warn(ar, "failed to setup ps on vdev %i: %d\n",
3540 				    arvif->vdev_id, ret);
3541 	}
3542 
3543 	mutex_unlock(&ar->conf_mutex);
3544 }
3545 
3546 static int ath10k_hw_scan(struct ieee80211_hw *hw,
3547 			  struct ieee80211_vif *vif,
3548 			  struct ieee80211_scan_request *hw_req)
3549 {
3550 	struct ath10k *ar = hw->priv;
3551 	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3552 	struct cfg80211_scan_request *req = &hw_req->req;
3553 	struct wmi_start_scan_arg arg;
3554 	int ret = 0;
3555 	int i;
3556 
3557 	mutex_lock(&ar->conf_mutex);
3558 
3559 	spin_lock_bh(&ar->data_lock);
3560 	switch (ar->scan.state) {
3561 	case ATH10K_SCAN_IDLE:
3562 		reinit_completion(&ar->scan.started);
3563 		reinit_completion(&ar->scan.completed);
3564 		ar->scan.state = ATH10K_SCAN_STARTING;
3565 		ar->scan.is_roc = false;
3566 		ar->scan.vdev_id = arvif->vdev_id;
3567 		ret = 0;
3568 		break;
3569 	case ATH10K_SCAN_STARTING:
3570 	case ATH10K_SCAN_RUNNING:
3571 	case ATH10K_SCAN_ABORTING:
3572 		ret = -EBUSY;
3573 		break;
3574 	}
3575 	spin_unlock_bh(&ar->data_lock);
3576 
3577 	if (ret)
3578 		goto exit;
3579 
3580 	memset(&arg, 0, sizeof(arg));
3581 	ath10k_wmi_start_scan_init(ar, &arg);
3582 	arg.vdev_id = arvif->vdev_id;
3583 	arg.scan_id = ATH10K_SCAN_ID;
3584 
3585 	if (!req->no_cck)
3586 		arg.scan_ctrl_flags |= WMI_SCAN_ADD_CCK_RATES;
3587 
3588 	if (req->ie_len) {
3589 		arg.ie_len = req->ie_len;
3590 		memcpy(arg.ie, req->ie, arg.ie_len);
3591 	}
3592 
3593 	if (req->n_ssids) {
3594 		arg.n_ssids = req->n_ssids;
3595 		for (i = 0; i < arg.n_ssids; i++) {
3596 			arg.ssids[i].len  = req->ssids[i].ssid_len;
3597 			arg.ssids[i].ssid = req->ssids[i].ssid;
3598 		}
3599 	} else {
3600 		arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
3601 	}
3602 
3603 	if (req->n_channels) {
3604 		arg.n_channels = req->n_channels;
3605 		for (i = 0; i < arg.n_channels; i++)
3606 			arg.channels[i] = req->channels[i]->center_freq;
3607 	}
3608 
3609 	ret = ath10k_start_scan(ar, &arg);
3610 	if (ret) {
3611 		ath10k_warn(ar, "failed to start hw scan: %d\n", ret);
3612 		spin_lock_bh(&ar->data_lock);
3613 		ar->scan.state = ATH10K_SCAN_IDLE;
3614 		spin_unlock_bh(&ar->data_lock);
3615 	}
3616 
3617 exit:
3618 	mutex_unlock(&ar->conf_mutex);
3619 	return ret;
3620 }
3621 
3622 static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
3623 				  struct ieee80211_vif *vif)
3624 {
3625 	struct ath10k *ar = hw->priv;
3626 
3627 	mutex_lock(&ar->conf_mutex);
3628 	ath10k_scan_abort(ar);
3629 	mutex_unlock(&ar->conf_mutex);
3630 
3631 	cancel_delayed_work_sync(&ar->scan.timeout);
3632 }
3633 
3634 static void ath10k_set_key_h_def_keyidx(struct ath10k *ar,
3635 					struct ath10k_vif *arvif,
3636 					enum set_key_cmd cmd,
3637 					struct ieee80211_key_conf *key)
3638 {
3639 	u32 vdev_param = arvif->ar->wmi.vdev_param->def_keyid;
3640 	int ret;
3641 
3642 	/* 10.1 firmware branch requires default key index to be set to group
3643 	 * key index after installing it. Otherwise FW/HW Txes corrupted
3644 	 * frames with multi-vif APs. This is not required for main firmware
3645 	 * branch (e.g. 636).
3646 	 *
3647 	 * FIXME: This has been tested only in AP. It remains unknown if this
3648 	 * is required for multi-vif STA interfaces on 10.1 */
3649 
3650 	if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
3651 		return;
3652 
3653 	if (key->cipher == WLAN_CIPHER_SUITE_WEP40)
3654 		return;
3655 
3656 	if (key->cipher == WLAN_CIPHER_SUITE_WEP104)
3657 		return;
3658 
3659 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3660 		return;
3661 
3662 	if (cmd != SET_KEY)
3663 		return;
3664 
3665 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3666 					key->keyidx);
3667 	if (ret)
3668 		ath10k_warn(ar, "failed to set vdev %i group key as default key: %d\n",
3669 			    arvif->vdev_id, ret);
3670 }
3671 
3672 static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
3673 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
3674 			  struct ieee80211_key_conf *key)
3675 {
3676 	struct ath10k *ar = hw->priv;
3677 	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3678 	struct ath10k_peer *peer;
3679 	const u8 *peer_addr;
3680 	bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3681 		      key->cipher == WLAN_CIPHER_SUITE_WEP104;
3682 	bool def_idx = false;
3683 	int ret = 0;
3684 
3685 	if (key->keyidx > WMI_MAX_KEY_INDEX)
3686 		return -ENOSPC;
3687 
3688 	mutex_lock(&ar->conf_mutex);
3689 
3690 	if (sta)
3691 		peer_addr = sta->addr;
3692 	else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
3693 		peer_addr = vif->bss_conf.bssid;
3694 	else
3695 		peer_addr = vif->addr;
3696 
3697 	key->hw_key_idx = key->keyidx;
3698 
3699 	/* the peer should not disappear in mid-way (unless FW goes awry) since
3700 	 * we already hold conf_mutex. we just make sure its there now. */
3701 	spin_lock_bh(&ar->data_lock);
3702 	peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
3703 	spin_unlock_bh(&ar->data_lock);
3704 
3705 	if (!peer) {
3706 		if (cmd == SET_KEY) {
3707 			ath10k_warn(ar, "failed to install key for non-existent peer %pM\n",
3708 				    peer_addr);
3709 			ret = -EOPNOTSUPP;
3710 			goto exit;
3711 		} else {
3712 			/* if the peer doesn't exist there is no key to disable
3713 			 * anymore */
3714 			goto exit;
3715 		}
3716 	}
3717 
3718 	if (is_wep) {
3719 		if (cmd == SET_KEY)
3720 			arvif->wep_keys[key->keyidx] = key;
3721 		else
3722 			arvif->wep_keys[key->keyidx] = NULL;
3723 
3724 		if (cmd == DISABLE_KEY)
3725 			ath10k_clear_vdev_key(arvif, key);
3726 	}
3727 
3728 	/* set TX_USAGE flag for all the keys incase of dot1x-WEP. For
3729 	 * static WEP, do not set this flag for the keys whose key id
3730 	 * is  greater than default key id.
3731 	 */
3732 	if (arvif->def_wep_key_idx == -1)
3733 		def_idx = true;
3734 
3735 	ret = ath10k_install_key(arvif, key, cmd, peer_addr, def_idx);
3736 	if (ret) {
3737 		ath10k_warn(ar, "failed to install key for vdev %i peer %pM: %d\n",
3738 			    arvif->vdev_id, peer_addr, ret);
3739 		goto exit;
3740 	}
3741 
3742 	ath10k_set_key_h_def_keyidx(ar, arvif, cmd, key);
3743 
3744 	spin_lock_bh(&ar->data_lock);
3745 	peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
3746 	if (peer && cmd == SET_KEY)
3747 		peer->keys[key->keyidx] = key;
3748 	else if (peer && cmd == DISABLE_KEY)
3749 		peer->keys[key->keyidx] = NULL;
3750 	else if (peer == NULL)
3751 		/* impossible unless FW goes crazy */
3752 		ath10k_warn(ar, "Peer %pM disappeared!\n", peer_addr);
3753 	spin_unlock_bh(&ar->data_lock);
3754 
3755 exit:
3756 	mutex_unlock(&ar->conf_mutex);
3757 	return ret;
3758 }
3759 
3760 static void ath10k_set_default_unicast_key(struct ieee80211_hw *hw,
3761 					   struct ieee80211_vif *vif,
3762 					   int keyidx)
3763 {
3764 	struct ath10k *ar = hw->priv;
3765 	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3766 	int ret;
3767 
3768 	mutex_lock(&arvif->ar->conf_mutex);
3769 
3770 	if (arvif->ar->state != ATH10K_STATE_ON)
3771 		goto unlock;
3772 
3773 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
3774 		   arvif->vdev_id, keyidx);
3775 
3776 	ret = ath10k_wmi_vdev_set_param(arvif->ar,
3777 					arvif->vdev_id,
3778 					arvif->ar->wmi.vdev_param->def_keyid,
3779 					keyidx);
3780 
3781 	if (ret) {
3782 		ath10k_warn(ar, "failed to update wep key index for vdev %d: %d\n",
3783 			    arvif->vdev_id,
3784 			    ret);
3785 		goto unlock;
3786 	}
3787 
3788 	arvif->def_wep_key_idx = keyidx;
3789 unlock:
3790 	mutex_unlock(&arvif->ar->conf_mutex);
3791 }
3792 
3793 static void ath10k_sta_rc_update_wk(struct work_struct *wk)
3794 {
3795 	struct ath10k *ar;
3796 	struct ath10k_vif *arvif;
3797 	struct ath10k_sta *arsta;
3798 	struct ieee80211_sta *sta;
3799 	u32 changed, bw, nss, smps;
3800 	int err;
3801 
3802 	arsta = container_of(wk, struct ath10k_sta, update_wk);
3803 	sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
3804 	arvif = arsta->arvif;
3805 	ar = arvif->ar;
3806 
3807 	spin_lock_bh(&ar->data_lock);
3808 
3809 	changed = arsta->changed;
3810 	arsta->changed = 0;
3811 
3812 	bw = arsta->bw;
3813 	nss = arsta->nss;
3814 	smps = arsta->smps;
3815 
3816 	spin_unlock_bh(&ar->data_lock);
3817 
3818 	mutex_lock(&ar->conf_mutex);
3819 
3820 	if (changed & IEEE80211_RC_BW_CHANGED) {
3821 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM peer bw %d\n",
3822 			   sta->addr, bw);
3823 
3824 		err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
3825 						WMI_PEER_CHAN_WIDTH, bw);
3826 		if (err)
3827 			ath10k_warn(ar, "failed to update STA %pM peer bw %d: %d\n",
3828 				    sta->addr, bw, err);
3829 	}
3830 
3831 	if (changed & IEEE80211_RC_NSS_CHANGED) {
3832 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM nss %d\n",
3833 			   sta->addr, nss);
3834 
3835 		err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
3836 						WMI_PEER_NSS, nss);
3837 		if (err)
3838 			ath10k_warn(ar, "failed to update STA %pM nss %d: %d\n",
3839 				    sta->addr, nss, err);
3840 	}
3841 
3842 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
3843 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM smps %d\n",
3844 			   sta->addr, smps);
3845 
3846 		err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
3847 						WMI_PEER_SMPS_STATE, smps);
3848 		if (err)
3849 			ath10k_warn(ar, "failed to update STA %pM smps %d: %d\n",
3850 				    sta->addr, smps, err);
3851 	}
3852 
3853 	if (changed & IEEE80211_RC_SUPP_RATES_CHANGED ||
3854 	    changed & IEEE80211_RC_NSS_CHANGED) {
3855 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM supp rates/nss\n",
3856 			   sta->addr);
3857 
3858 		err = ath10k_station_assoc(ar, arvif->vif, sta, true);
3859 		if (err)
3860 			ath10k_warn(ar, "failed to reassociate station: %pM\n",
3861 				    sta->addr);
3862 	}
3863 
3864 	mutex_unlock(&ar->conf_mutex);
3865 }
3866 
3867 static int ath10k_mac_inc_num_stations(struct ath10k_vif *arvif)
3868 {
3869 	struct ath10k *ar = arvif->ar;
3870 
3871 	lockdep_assert_held(&ar->conf_mutex);
3872 
3873 	if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
3874 	    arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
3875 		return 0;
3876 
3877 	if (ar->num_stations >= ar->max_num_stations)
3878 		return -ENOBUFS;
3879 
3880 	ar->num_stations++;
3881 
3882 	return 0;
3883 }
3884 
3885 static void ath10k_mac_dec_num_stations(struct ath10k_vif *arvif)
3886 {
3887 	struct ath10k *ar = arvif->ar;
3888 
3889 	lockdep_assert_held(&ar->conf_mutex);
3890 
3891 	if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
3892 	    arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
3893 		return;
3894 
3895 	ar->num_stations--;
3896 }
3897 
3898 static int ath10k_sta_state(struct ieee80211_hw *hw,
3899 			    struct ieee80211_vif *vif,
3900 			    struct ieee80211_sta *sta,
3901 			    enum ieee80211_sta_state old_state,
3902 			    enum ieee80211_sta_state new_state)
3903 {
3904 	struct ath10k *ar = hw->priv;
3905 	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3906 	struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
3907 	int ret = 0;
3908 
3909 	if (old_state == IEEE80211_STA_NOTEXIST &&
3910 	    new_state == IEEE80211_STA_NONE) {
3911 		memset(arsta, 0, sizeof(*arsta));
3912 		arsta->arvif = arvif;
3913 		INIT_WORK(&arsta->update_wk, ath10k_sta_rc_update_wk);
3914 	}
3915 
3916 	/* cancel must be done outside the mutex to avoid deadlock */
3917 	if ((old_state == IEEE80211_STA_NONE &&
3918 	     new_state == IEEE80211_STA_NOTEXIST))
3919 		cancel_work_sync(&arsta->update_wk);
3920 
3921 	mutex_lock(&ar->conf_mutex);
3922 
3923 	if (old_state == IEEE80211_STA_NOTEXIST &&
3924 	    new_state == IEEE80211_STA_NONE) {
3925 		/*
3926 		 * New station addition.
3927 		 */
3928 		ath10k_dbg(ar, ATH10K_DBG_MAC,
3929 			   "mac vdev %d peer create %pM (new sta) sta %d / %d peer %d / %d\n",
3930 			   arvif->vdev_id, sta->addr,
3931 			   ar->num_stations + 1, ar->max_num_stations,
3932 			   ar->num_peers + 1, ar->max_num_peers);
3933 
3934 		ret = ath10k_mac_inc_num_stations(arvif);
3935 		if (ret) {
3936 			ath10k_warn(ar, "refusing to associate station: too many connected already (%d)\n",
3937 				    ar->max_num_stations);
3938 			goto exit;
3939 		}
3940 
3941 		ret = ath10k_peer_create(ar, arvif->vdev_id, sta->addr);
3942 		if (ret) {
3943 			ath10k_warn(ar, "failed to add peer %pM for vdev %d when adding a new sta: %i\n",
3944 				    sta->addr, arvif->vdev_id, ret);
3945 			ath10k_mac_dec_num_stations(arvif);
3946 			goto exit;
3947 		}
3948 
3949 		if (vif->type == NL80211_IFTYPE_STATION) {
3950 			WARN_ON(arvif->is_started);
3951 
3952 			ret = ath10k_vdev_start(arvif);
3953 			if (ret) {
3954 				ath10k_warn(ar, "failed to start vdev %i: %d\n",
3955 					    arvif->vdev_id, ret);
3956 				WARN_ON(ath10k_peer_delete(ar, arvif->vdev_id,
3957 							   sta->addr));
3958 				ath10k_mac_dec_num_stations(arvif);
3959 				goto exit;
3960 			}
3961 
3962 			arvif->is_started = true;
3963 		}
3964 	} else if ((old_state == IEEE80211_STA_NONE &&
3965 		    new_state == IEEE80211_STA_NOTEXIST)) {
3966 		/*
3967 		 * Existing station deletion.
3968 		 */
3969 		ath10k_dbg(ar, ATH10K_DBG_MAC,
3970 			   "mac vdev %d peer delete %pM (sta gone)\n",
3971 			   arvif->vdev_id, sta->addr);
3972 
3973 		if (vif->type == NL80211_IFTYPE_STATION) {
3974 			WARN_ON(!arvif->is_started);
3975 
3976 			ret = ath10k_vdev_stop(arvif);
3977 			if (ret)
3978 				ath10k_warn(ar, "failed to stop vdev %i: %d\n",
3979 					    arvif->vdev_id, ret);
3980 
3981 			arvif->is_started = false;
3982 		}
3983 
3984 		ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
3985 		if (ret)
3986 			ath10k_warn(ar, "failed to delete peer %pM for vdev %d: %i\n",
3987 				    sta->addr, arvif->vdev_id, ret);
3988 
3989 		ath10k_mac_dec_num_stations(arvif);
3990 	} else if (old_state == IEEE80211_STA_AUTH &&
3991 		   new_state == IEEE80211_STA_ASSOC &&
3992 		   (vif->type == NL80211_IFTYPE_AP ||
3993 		    vif->type == NL80211_IFTYPE_ADHOC)) {
3994 		/*
3995 		 * New association.
3996 		 */
3997 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM associated\n",
3998 			   sta->addr);
3999 
4000 		ret = ath10k_station_assoc(ar, vif, sta, false);
4001 		if (ret)
4002 			ath10k_warn(ar, "failed to associate station %pM for vdev %i: %i\n",
4003 				    sta->addr, arvif->vdev_id, ret);
4004 	} else if (old_state == IEEE80211_STA_ASSOC &&
4005 		   new_state == IEEE80211_STA_AUTH &&
4006 		   (vif->type == NL80211_IFTYPE_AP ||
4007 		    vif->type == NL80211_IFTYPE_ADHOC)) {
4008 		/*
4009 		 * Disassociation.
4010 		 */
4011 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM disassociated\n",
4012 			   sta->addr);
4013 
4014 		ret = ath10k_station_disassoc(ar, vif, sta);
4015 		if (ret)
4016 			ath10k_warn(ar, "failed to disassociate station: %pM vdev %i: %i\n",
4017 				    sta->addr, arvif->vdev_id, ret);
4018 	}
4019 exit:
4020 	mutex_unlock(&ar->conf_mutex);
4021 	return ret;
4022 }
4023 
4024 static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
4025 				u16 ac, bool enable)
4026 {
4027 	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4028 	struct wmi_sta_uapsd_auto_trig_arg arg = {};
4029 	u32 prio = 0, acc = 0;
4030 	u32 value = 0;
4031 	int ret = 0;
4032 
4033 	lockdep_assert_held(&ar->conf_mutex);
4034 
4035 	if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
4036 		return 0;
4037 
4038 	switch (ac) {
4039 	case IEEE80211_AC_VO:
4040 		value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
4041 			WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
4042 		prio = 7;
4043 		acc = 3;
4044 		break;
4045 	case IEEE80211_AC_VI:
4046 		value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
4047 			WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
4048 		prio = 5;
4049 		acc = 2;
4050 		break;
4051 	case IEEE80211_AC_BE:
4052 		value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
4053 			WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
4054 		prio = 2;
4055 		acc = 1;
4056 		break;
4057 	case IEEE80211_AC_BK:
4058 		value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
4059 			WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
4060 		prio = 0;
4061 		acc = 0;
4062 		break;
4063 	}
4064 
4065 	if (enable)
4066 		arvif->u.sta.uapsd |= value;
4067 	else
4068 		arvif->u.sta.uapsd &= ~value;
4069 
4070 	ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4071 					  WMI_STA_PS_PARAM_UAPSD,
4072 					  arvif->u.sta.uapsd);
4073 	if (ret) {
4074 		ath10k_warn(ar, "failed to set uapsd params: %d\n", ret);
4075 		goto exit;
4076 	}
4077 
4078 	if (arvif->u.sta.uapsd)
4079 		value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
4080 	else
4081 		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
4082 
4083 	ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4084 					  WMI_STA_PS_PARAM_RX_WAKE_POLICY,
4085 					  value);
4086 	if (ret)
4087 		ath10k_warn(ar, "failed to set rx wake param: %d\n", ret);
4088 
4089 	ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
4090 	if (ret) {
4091 		ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
4092 			    arvif->vdev_id, ret);
4093 		return ret;
4094 	}
4095 
4096 	ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
4097 	if (ret) {
4098 		ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
4099 			    arvif->vdev_id, ret);
4100 		return ret;
4101 	}
4102 
4103 	if (test_bit(WMI_SERVICE_STA_UAPSD_BASIC_AUTO_TRIG, ar->wmi.svc_map) ||
4104 	    test_bit(WMI_SERVICE_STA_UAPSD_VAR_AUTO_TRIG, ar->wmi.svc_map)) {
4105 		/* Only userspace can make an educated decision when to send
4106 		 * trigger frame. The following effectively disables u-UAPSD
4107 		 * autotrigger in firmware (which is enabled by default
4108 		 * provided the autotrigger service is available).
4109 		 */
4110 
4111 		arg.wmm_ac = acc;
4112 		arg.user_priority = prio;
4113 		arg.service_interval = 0;
4114 		arg.suspend_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
4115 		arg.delay_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
4116 
4117 		ret = ath10k_wmi_vdev_sta_uapsd(ar, arvif->vdev_id,
4118 						arvif->bssid, &arg, 1);
4119 		if (ret) {
4120 			ath10k_warn(ar, "failed to set uapsd auto trigger %d\n",
4121 				    ret);
4122 			return ret;
4123 		}
4124 	}
4125 
4126 exit:
4127 	return ret;
4128 }
4129 
4130 static int ath10k_conf_tx(struct ieee80211_hw *hw,
4131 			  struct ieee80211_vif *vif, u16 ac,
4132 			  const struct ieee80211_tx_queue_params *params)
4133 {
4134 	struct ath10k *ar = hw->priv;
4135 	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4136 	struct wmi_wmm_params_arg *p = NULL;
4137 	int ret;
4138 
4139 	mutex_lock(&ar->conf_mutex);
4140 
4141 	switch (ac) {
4142 	case IEEE80211_AC_VO:
4143 		p = &arvif->wmm_params.ac_vo;
4144 		break;
4145 	case IEEE80211_AC_VI:
4146 		p = &arvif->wmm_params.ac_vi;
4147 		break;
4148 	case IEEE80211_AC_BE:
4149 		p = &arvif->wmm_params.ac_be;
4150 		break;
4151 	case IEEE80211_AC_BK:
4152 		p = &arvif->wmm_params.ac_bk;
4153 		break;
4154 	}
4155 
4156 	if (WARN_ON(!p)) {
4157 		ret = -EINVAL;
4158 		goto exit;
4159 	}
4160 
4161 	p->cwmin = params->cw_min;
4162 	p->cwmax = params->cw_max;
4163 	p->aifs = params->aifs;
4164 
4165 	/*
4166 	 * The channel time duration programmed in the HW is in absolute
4167 	 * microseconds, while mac80211 gives the txop in units of
4168 	 * 32 microseconds.
4169 	 */
4170 	p->txop = params->txop * 32;
4171 
4172 	if (ar->wmi.ops->gen_vdev_wmm_conf) {
4173 		ret = ath10k_wmi_vdev_wmm_conf(ar, arvif->vdev_id,
4174 					       &arvif->wmm_params);
4175 		if (ret) {
4176 			ath10k_warn(ar, "failed to set vdev wmm params on vdev %i: %d\n",
4177 				    arvif->vdev_id, ret);
4178 			goto exit;
4179 		}
4180 	} else {
4181 		/* This won't work well with multi-interface cases but it's
4182 		 * better than nothing.
4183 		 */
4184 		ret = ath10k_wmi_pdev_set_wmm_params(ar, &arvif->wmm_params);
4185 		if (ret) {
4186 			ath10k_warn(ar, "failed to set wmm params: %d\n", ret);
4187 			goto exit;
4188 		}
4189 	}
4190 
4191 	ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
4192 	if (ret)
4193 		ath10k_warn(ar, "failed to set sta uapsd: %d\n", ret);
4194 
4195 exit:
4196 	mutex_unlock(&ar->conf_mutex);
4197 	return ret;
4198 }
4199 
4200 #define ATH10K_ROC_TIMEOUT_HZ (2*HZ)
4201 
4202 static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
4203 				    struct ieee80211_vif *vif,
4204 				    struct ieee80211_channel *chan,
4205 				    int duration,
4206 				    enum ieee80211_roc_type type)
4207 {
4208 	struct ath10k *ar = hw->priv;
4209 	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4210 	struct wmi_start_scan_arg arg;
4211 	int ret = 0;
4212 
4213 	mutex_lock(&ar->conf_mutex);
4214 
4215 	spin_lock_bh(&ar->data_lock);
4216 	switch (ar->scan.state) {
4217 	case ATH10K_SCAN_IDLE:
4218 		reinit_completion(&ar->scan.started);
4219 		reinit_completion(&ar->scan.completed);
4220 		reinit_completion(&ar->scan.on_channel);
4221 		ar->scan.state = ATH10K_SCAN_STARTING;
4222 		ar->scan.is_roc = true;
4223 		ar->scan.vdev_id = arvif->vdev_id;
4224 		ar->scan.roc_freq = chan->center_freq;
4225 		ret = 0;
4226 		break;
4227 	case ATH10K_SCAN_STARTING:
4228 	case ATH10K_SCAN_RUNNING:
4229 	case ATH10K_SCAN_ABORTING:
4230 		ret = -EBUSY;
4231 		break;
4232 	}
4233 	spin_unlock_bh(&ar->data_lock);
4234 
4235 	if (ret)
4236 		goto exit;
4237 
4238 	duration = max(duration, WMI_SCAN_CHAN_MIN_TIME_MSEC);
4239 
4240 	memset(&arg, 0, sizeof(arg));
4241 	ath10k_wmi_start_scan_init(ar, &arg);
4242 	arg.vdev_id = arvif->vdev_id;
4243 	arg.scan_id = ATH10K_SCAN_ID;
4244 	arg.n_channels = 1;
4245 	arg.channels[0] = chan->center_freq;
4246 	arg.dwell_time_active = duration;
4247 	arg.dwell_time_passive = duration;
4248 	arg.max_scan_time = 2 * duration;
4249 	arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
4250 	arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
4251 
4252 	ret = ath10k_start_scan(ar, &arg);
4253 	if (ret) {
4254 		ath10k_warn(ar, "failed to start roc scan: %d\n", ret);
4255 		spin_lock_bh(&ar->data_lock);
4256 		ar->scan.state = ATH10K_SCAN_IDLE;
4257 		spin_unlock_bh(&ar->data_lock);
4258 		goto exit;
4259 	}
4260 
4261 	ret = wait_for_completion_timeout(&ar->scan.on_channel, 3*HZ);
4262 	if (ret == 0) {
4263 		ath10k_warn(ar, "failed to switch to channel for roc scan\n");
4264 
4265 		ret = ath10k_scan_stop(ar);
4266 		if (ret)
4267 			ath10k_warn(ar, "failed to stop scan: %d\n", ret);
4268 
4269 		ret = -ETIMEDOUT;
4270 		goto exit;
4271 	}
4272 
4273 	ret = 0;
4274 exit:
4275 	mutex_unlock(&ar->conf_mutex);
4276 	return ret;
4277 }
4278 
4279 static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw)
4280 {
4281 	struct ath10k *ar = hw->priv;
4282 
4283 	mutex_lock(&ar->conf_mutex);
4284 	ath10k_scan_abort(ar);
4285 	mutex_unlock(&ar->conf_mutex);
4286 
4287 	cancel_delayed_work_sync(&ar->scan.timeout);
4288 
4289 	return 0;
4290 }
4291 
4292 /*
4293  * Both RTS and Fragmentation threshold are interface-specific
4294  * in ath10k, but device-specific in mac80211.
4295  */
4296 
4297 static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
4298 {
4299 	struct ath10k *ar = hw->priv;
4300 	struct ath10k_vif *arvif;
4301 	int ret = 0;
4302 
4303 	mutex_lock(&ar->conf_mutex);
4304 	list_for_each_entry(arvif, &ar->arvifs, list) {
4305 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
4306 			   arvif->vdev_id, value);
4307 
4308 		ret = ath10k_mac_set_rts(arvif, value);
4309 		if (ret) {
4310 			ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
4311 				    arvif->vdev_id, ret);
4312 			break;
4313 		}
4314 	}
4315 	mutex_unlock(&ar->conf_mutex);
4316 
4317 	return ret;
4318 }
4319 
4320 static void ath10k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4321 			 u32 queues, bool drop)
4322 {
4323 	struct ath10k *ar = hw->priv;
4324 	bool skip;
4325 	int ret;
4326 
4327 	/* mac80211 doesn't care if we really xmit queued frames or not
4328 	 * we'll collect those frames either way if we stop/delete vdevs */
4329 	if (drop)
4330 		return;
4331 
4332 	mutex_lock(&ar->conf_mutex);
4333 
4334 	if (ar->state == ATH10K_STATE_WEDGED)
4335 		goto skip;
4336 
4337 	ret = wait_event_timeout(ar->htt.empty_tx_wq, ({
4338 			bool empty;
4339 
4340 			spin_lock_bh(&ar->htt.tx_lock);
4341 			empty = (ar->htt.num_pending_tx == 0);
4342 			spin_unlock_bh(&ar->htt.tx_lock);
4343 
4344 			skip = (ar->state == ATH10K_STATE_WEDGED) ||
4345 			       test_bit(ATH10K_FLAG_CRASH_FLUSH,
4346 					&ar->dev_flags);
4347 
4348 			(empty || skip);
4349 		}), ATH10K_FLUSH_TIMEOUT_HZ);
4350 
4351 	if (ret <= 0 || skip)
4352 		ath10k_warn(ar, "failed to flush transmit queue (skip %i ar-state %i): %i\n",
4353 			    skip, ar->state, ret);
4354 
4355 skip:
4356 	mutex_unlock(&ar->conf_mutex);
4357 }
4358 
4359 /* TODO: Implement this function properly
4360  * For now it is needed to reply to Probe Requests in IBSS mode.
4361  * Propably we need this information from FW.
4362  */
4363 static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
4364 {
4365 	return 1;
4366 }
4367 
4368 #ifdef CONFIG_PM
4369 static int ath10k_suspend(struct ieee80211_hw *hw,
4370 			  struct cfg80211_wowlan *wowlan)
4371 {
4372 	struct ath10k *ar = hw->priv;
4373 	int ret;
4374 
4375 	mutex_lock(&ar->conf_mutex);
4376 
4377 	ret = ath10k_wait_for_suspend(ar, WMI_PDEV_SUSPEND);
4378 	if (ret) {
4379 		if (ret == -ETIMEDOUT)
4380 			goto resume;
4381 		ret = 1;
4382 		goto exit;
4383 	}
4384 
4385 	ret = ath10k_hif_suspend(ar);
4386 	if (ret) {
4387 		ath10k_warn(ar, "failed to suspend hif: %d\n", ret);
4388 		goto resume;
4389 	}
4390 
4391 	ret = 0;
4392 	goto exit;
4393 resume:
4394 	ret = ath10k_wmi_pdev_resume_target(ar);
4395 	if (ret)
4396 		ath10k_warn(ar, "failed to resume target: %d\n", ret);
4397 
4398 	ret = 1;
4399 exit:
4400 	mutex_unlock(&ar->conf_mutex);
4401 	return ret;
4402 }
4403 
4404 static int ath10k_resume(struct ieee80211_hw *hw)
4405 {
4406 	struct ath10k *ar = hw->priv;
4407 	int ret;
4408 
4409 	mutex_lock(&ar->conf_mutex);
4410 
4411 	ret = ath10k_hif_resume(ar);
4412 	if (ret) {
4413 		ath10k_warn(ar, "failed to resume hif: %d\n", ret);
4414 		ret = 1;
4415 		goto exit;
4416 	}
4417 
4418 	ret = ath10k_wmi_pdev_resume_target(ar);
4419 	if (ret) {
4420 		ath10k_warn(ar, "failed to resume target: %d\n", ret);
4421 		ret = 1;
4422 		goto exit;
4423 	}
4424 
4425 	ret = 0;
4426 exit:
4427 	mutex_unlock(&ar->conf_mutex);
4428 	return ret;
4429 }
4430 #endif
4431 
4432 static void ath10k_reconfig_complete(struct ieee80211_hw *hw,
4433 				     enum ieee80211_reconfig_type reconfig_type)
4434 {
4435 	struct ath10k *ar = hw->priv;
4436 
4437 	if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
4438 		return;
4439 
4440 	mutex_lock(&ar->conf_mutex);
4441 
4442 	/* If device failed to restart it will be in a different state, e.g.
4443 	 * ATH10K_STATE_WEDGED */
4444 	if (ar->state == ATH10K_STATE_RESTARTED) {
4445 		ath10k_info(ar, "device successfully recovered\n");
4446 		ar->state = ATH10K_STATE_ON;
4447 		ieee80211_wake_queues(ar->hw);
4448 	}
4449 
4450 	mutex_unlock(&ar->conf_mutex);
4451 }
4452 
4453 static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
4454 			     struct survey_info *survey)
4455 {
4456 	struct ath10k *ar = hw->priv;
4457 	struct ieee80211_supported_band *sband;
4458 	struct survey_info *ar_survey = &ar->survey[idx];
4459 	int ret = 0;
4460 
4461 	mutex_lock(&ar->conf_mutex);
4462 
4463 	sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
4464 	if (sband && idx >= sband->n_channels) {
4465 		idx -= sband->n_channels;
4466 		sband = NULL;
4467 	}
4468 
4469 	if (!sband)
4470 		sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
4471 
4472 	if (!sband || idx >= sband->n_channels) {
4473 		ret = -ENOENT;
4474 		goto exit;
4475 	}
4476 
4477 	spin_lock_bh(&ar->data_lock);
4478 	memcpy(survey, ar_survey, sizeof(*survey));
4479 	spin_unlock_bh(&ar->data_lock);
4480 
4481 	survey->channel = &sband->channels[idx];
4482 
4483 	if (ar->rx_channel == survey->channel)
4484 		survey->filled |= SURVEY_INFO_IN_USE;
4485 
4486 exit:
4487 	mutex_unlock(&ar->conf_mutex);
4488 	return ret;
4489 }
4490 
4491 /* Helper table for legacy fixed_rate/bitrate_mask */
4492 static const u8 cck_ofdm_rate[] = {
4493 	/* CCK */
4494 	3, /* 1Mbps */
4495 	2, /* 2Mbps */
4496 	1, /* 5.5Mbps */
4497 	0, /* 11Mbps */
4498 	/* OFDM */
4499 	3, /* 6Mbps */
4500 	7, /* 9Mbps */
4501 	2, /* 12Mbps */
4502 	6, /* 18Mbps */
4503 	1, /* 24Mbps */
4504 	5, /* 36Mbps */
4505 	0, /* 48Mbps */
4506 	4, /* 54Mbps */
4507 };
4508 
4509 /* Check if only one bit set */
4510 static int ath10k_check_single_mask(u32 mask)
4511 {
4512 	int bit;
4513 
4514 	bit = ffs(mask);
4515 	if (!bit)
4516 		return 0;
4517 
4518 	mask &= ~BIT(bit - 1);
4519 	if (mask)
4520 		return 2;
4521 
4522 	return 1;
4523 }
4524 
4525 static bool
4526 ath10k_default_bitrate_mask(struct ath10k *ar,
4527 			    enum ieee80211_band band,
4528 			    const struct cfg80211_bitrate_mask *mask)
4529 {
4530 	u32 legacy = 0x00ff;
4531 	u8 ht = 0xff, i;
4532 	u16 vht = 0x3ff;
4533 	u16 nrf = ar->num_rf_chains;
4534 
4535 	if (ar->cfg_tx_chainmask)
4536 		nrf = get_nss_from_chainmask(ar->cfg_tx_chainmask);
4537 
4538 	switch (band) {
4539 	case IEEE80211_BAND_2GHZ:
4540 		legacy = 0x00fff;
4541 		vht = 0;
4542 		break;
4543 	case IEEE80211_BAND_5GHZ:
4544 		break;
4545 	default:
4546 		return false;
4547 	}
4548 
4549 	if (mask->control[band].legacy != legacy)
4550 		return false;
4551 
4552 	for (i = 0; i < nrf; i++)
4553 		if (mask->control[band].ht_mcs[i] != ht)
4554 			return false;
4555 
4556 	for (i = 0; i < nrf; i++)
4557 		if (mask->control[band].vht_mcs[i] != vht)
4558 			return false;
4559 
4560 	return true;
4561 }
4562 
4563 static bool
4564 ath10k_bitrate_mask_nss(const struct cfg80211_bitrate_mask *mask,
4565 			enum ieee80211_band band,
4566 			u8 *fixed_nss)
4567 {
4568 	int ht_nss = 0, vht_nss = 0, i;
4569 
4570 	/* check legacy */
4571 	if (ath10k_check_single_mask(mask->control[band].legacy))
4572 		return false;
4573 
4574 	/* check HT */
4575 	for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
4576 		if (mask->control[band].ht_mcs[i] == 0xff)
4577 			continue;
4578 		else if (mask->control[band].ht_mcs[i] == 0x00)
4579 			break;
4580 
4581 		return false;
4582 	}
4583 
4584 	ht_nss = i;
4585 
4586 	/* check VHT */
4587 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
4588 		if (mask->control[band].vht_mcs[i] == 0x03ff)
4589 			continue;
4590 		else if (mask->control[band].vht_mcs[i] == 0x0000)
4591 			break;
4592 
4593 		return false;
4594 	}
4595 
4596 	vht_nss = i;
4597 
4598 	if (ht_nss > 0 && vht_nss > 0)
4599 		return false;
4600 
4601 	if (ht_nss)
4602 		*fixed_nss = ht_nss;
4603 	else if (vht_nss)
4604 		*fixed_nss = vht_nss;
4605 	else
4606 		return false;
4607 
4608 	return true;
4609 }
4610 
4611 static bool
4612 ath10k_bitrate_mask_correct(const struct cfg80211_bitrate_mask *mask,
4613 			    enum ieee80211_band band,
4614 			    enum wmi_rate_preamble *preamble)
4615 {
4616 	int legacy = 0, ht = 0, vht = 0, i;
4617 
4618 	*preamble = WMI_RATE_PREAMBLE_OFDM;
4619 
4620 	/* check legacy */
4621 	legacy = ath10k_check_single_mask(mask->control[band].legacy);
4622 	if (legacy > 1)
4623 		return false;
4624 
4625 	/* check HT */
4626 	for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
4627 		ht += ath10k_check_single_mask(mask->control[band].ht_mcs[i]);
4628 	if (ht > 1)
4629 		return false;
4630 
4631 	/* check VHT */
4632 	for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
4633 		vht += ath10k_check_single_mask(mask->control[band].vht_mcs[i]);
4634 	if (vht > 1)
4635 		return false;
4636 
4637 	/* Currently we support only one fixed_rate */
4638 	if ((legacy + ht + vht) != 1)
4639 		return false;
4640 
4641 	if (ht)
4642 		*preamble = WMI_RATE_PREAMBLE_HT;
4643 	else if (vht)
4644 		*preamble = WMI_RATE_PREAMBLE_VHT;
4645 
4646 	return true;
4647 }
4648 
4649 static bool
4650 ath10k_bitrate_mask_rate(struct ath10k *ar,
4651 			 const struct cfg80211_bitrate_mask *mask,
4652 			 enum ieee80211_band band,
4653 			 u8 *fixed_rate,
4654 			 u8 *fixed_nss)
4655 {
4656 	u8 rate = 0, pream = 0, nss = 0, i;
4657 	enum wmi_rate_preamble preamble;
4658 
4659 	/* Check if single rate correct */
4660 	if (!ath10k_bitrate_mask_correct(mask, band, &preamble))
4661 		return false;
4662 
4663 	pream = preamble;
4664 
4665 	switch (preamble) {
4666 	case WMI_RATE_PREAMBLE_CCK:
4667 	case WMI_RATE_PREAMBLE_OFDM:
4668 		i = ffs(mask->control[band].legacy) - 1;
4669 
4670 		if (band == IEEE80211_BAND_2GHZ && i < 4)
4671 			pream = WMI_RATE_PREAMBLE_CCK;
4672 
4673 		if (band == IEEE80211_BAND_5GHZ)
4674 			i += 4;
4675 
4676 		if (i >= ARRAY_SIZE(cck_ofdm_rate))
4677 			return false;
4678 
4679 		rate = cck_ofdm_rate[i];
4680 		break;
4681 	case WMI_RATE_PREAMBLE_HT:
4682 		for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
4683 			if (mask->control[band].ht_mcs[i])
4684 				break;
4685 
4686 		if (i == IEEE80211_HT_MCS_MASK_LEN)
4687 			return false;
4688 
4689 		rate = ffs(mask->control[band].ht_mcs[i]) - 1;
4690 		nss = i;
4691 		break;
4692 	case WMI_RATE_PREAMBLE_VHT:
4693 		for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
4694 			if (mask->control[band].vht_mcs[i])
4695 				break;
4696 
4697 		if (i == NL80211_VHT_NSS_MAX)
4698 			return false;
4699 
4700 		rate = ffs(mask->control[band].vht_mcs[i]) - 1;
4701 		nss = i;
4702 		break;
4703 	}
4704 
4705 	*fixed_nss = nss + 1;
4706 	nss <<= 4;
4707 	pream <<= 6;
4708 
4709 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac fixed rate pream 0x%02x nss 0x%02x rate 0x%02x\n",
4710 		   pream, nss, rate);
4711 
4712 	*fixed_rate = pream | nss | rate;
4713 
4714 	return true;
4715 }
4716 
4717 static bool ath10k_get_fixed_rate_nss(struct ath10k *ar,
4718 				      const struct cfg80211_bitrate_mask *mask,
4719 				      enum ieee80211_band band,
4720 				      u8 *fixed_rate,
4721 				      u8 *fixed_nss)
4722 {
4723 	/* First check full NSS mask, if we can simply limit NSS */
4724 	if (ath10k_bitrate_mask_nss(mask, band, fixed_nss))
4725 		return true;
4726 
4727 	/* Next Check single rate is set */
4728 	return ath10k_bitrate_mask_rate(ar, mask, band, fixed_rate, fixed_nss);
4729 }
4730 
4731 static int ath10k_set_fixed_rate_param(struct ath10k_vif *arvif,
4732 				       u8 fixed_rate,
4733 				       u8 fixed_nss,
4734 				       u8 force_sgi)
4735 {
4736 	struct ath10k *ar = arvif->ar;
4737 	u32 vdev_param;
4738 	int ret = 0;
4739 
4740 	mutex_lock(&ar->conf_mutex);
4741 
4742 	if (arvif->fixed_rate == fixed_rate &&
4743 	    arvif->fixed_nss == fixed_nss &&
4744 	    arvif->force_sgi == force_sgi)
4745 		goto exit;
4746 
4747 	if (fixed_rate == WMI_FIXED_RATE_NONE)
4748 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac disable fixed bitrate mask\n");
4749 
4750 	if (force_sgi)
4751 		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac force sgi\n");
4752 
4753 	vdev_param = ar->wmi.vdev_param->fixed_rate;
4754 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
4755 					vdev_param, fixed_rate);
4756 	if (ret) {
4757 		ath10k_warn(ar, "failed to set fixed rate param 0x%02x: %d\n",
4758 			    fixed_rate, ret);
4759 		ret = -EINVAL;
4760 		goto exit;
4761 	}
4762 
4763 	arvif->fixed_rate = fixed_rate;
4764 
4765 	vdev_param = ar->wmi.vdev_param->nss;
4766 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
4767 					vdev_param, fixed_nss);
4768 
4769 	if (ret) {
4770 		ath10k_warn(ar, "failed to set fixed nss param %d: %d\n",
4771 			    fixed_nss, ret);
4772 		ret = -EINVAL;
4773 		goto exit;
4774 	}
4775 
4776 	arvif->fixed_nss = fixed_nss;
4777 
4778 	vdev_param = ar->wmi.vdev_param->sgi;
4779 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
4780 					force_sgi);
4781 
4782 	if (ret) {
4783 		ath10k_warn(ar, "failed to set sgi param %d: %d\n",
4784 			    force_sgi, ret);
4785 		ret = -EINVAL;
4786 		goto exit;
4787 	}
4788 
4789 	arvif->force_sgi = force_sgi;
4790 
4791 exit:
4792 	mutex_unlock(&ar->conf_mutex);
4793 	return ret;
4794 }
4795 
4796 static int ath10k_set_bitrate_mask(struct ieee80211_hw *hw,
4797 				   struct ieee80211_vif *vif,
4798 				   const struct cfg80211_bitrate_mask *mask)
4799 {
4800 	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4801 	struct ath10k *ar = arvif->ar;
4802 	enum ieee80211_band band = ar->hw->conf.chandef.chan->band;
4803 	u8 fixed_rate = WMI_FIXED_RATE_NONE;
4804 	u8 fixed_nss = ar->num_rf_chains;
4805 	u8 force_sgi;
4806 
4807 	if (ar->cfg_tx_chainmask)
4808 		fixed_nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
4809 
4810 	force_sgi = mask->control[band].gi;
4811 	if (force_sgi == NL80211_TXRATE_FORCE_LGI)
4812 		return -EINVAL;
4813 
4814 	if (!ath10k_default_bitrate_mask(ar, band, mask)) {
4815 		if (!ath10k_get_fixed_rate_nss(ar, mask, band,
4816 					       &fixed_rate,
4817 					       &fixed_nss))
4818 			return -EINVAL;
4819 	}
4820 
4821 	if (fixed_rate == WMI_FIXED_RATE_NONE && force_sgi) {
4822 		ath10k_warn(ar, "failed to force SGI usage for default rate settings\n");
4823 		return -EINVAL;
4824 	}
4825 
4826 	return ath10k_set_fixed_rate_param(arvif, fixed_rate,
4827 					   fixed_nss, force_sgi);
4828 }
4829 
4830 static void ath10k_sta_rc_update(struct ieee80211_hw *hw,
4831 				 struct ieee80211_vif *vif,
4832 				 struct ieee80211_sta *sta,
4833 				 u32 changed)
4834 {
4835 	struct ath10k *ar = hw->priv;
4836 	struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
4837 	u32 bw, smps;
4838 
4839 	spin_lock_bh(&ar->data_lock);
4840 
4841 	ath10k_dbg(ar, ATH10K_DBG_MAC,
4842 		   "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
4843 		   sta->addr, changed, sta->bandwidth, sta->rx_nss,
4844 		   sta->smps_mode);
4845 
4846 	if (changed & IEEE80211_RC_BW_CHANGED) {
4847 		bw = WMI_PEER_CHWIDTH_20MHZ;
4848 
4849 		switch (sta->bandwidth) {
4850 		case IEEE80211_STA_RX_BW_20:
4851 			bw = WMI_PEER_CHWIDTH_20MHZ;
4852 			break;
4853 		case IEEE80211_STA_RX_BW_40:
4854 			bw = WMI_PEER_CHWIDTH_40MHZ;
4855 			break;
4856 		case IEEE80211_STA_RX_BW_80:
4857 			bw = WMI_PEER_CHWIDTH_80MHZ;
4858 			break;
4859 		case IEEE80211_STA_RX_BW_160:
4860 			ath10k_warn(ar, "Invalid bandwith %d in rc update for %pM\n",
4861 				    sta->bandwidth, sta->addr);
4862 			bw = WMI_PEER_CHWIDTH_20MHZ;
4863 			break;
4864 		}
4865 
4866 		arsta->bw = bw;
4867 	}
4868 
4869 	if (changed & IEEE80211_RC_NSS_CHANGED)
4870 		arsta->nss = sta->rx_nss;
4871 
4872 	if (changed & IEEE80211_RC_SMPS_CHANGED) {
4873 		smps = WMI_PEER_SMPS_PS_NONE;
4874 
4875 		switch (sta->smps_mode) {
4876 		case IEEE80211_SMPS_AUTOMATIC:
4877 		case IEEE80211_SMPS_OFF:
4878 			smps = WMI_PEER_SMPS_PS_NONE;
4879 			break;
4880 		case IEEE80211_SMPS_STATIC:
4881 			smps = WMI_PEER_SMPS_STATIC;
4882 			break;
4883 		case IEEE80211_SMPS_DYNAMIC:
4884 			smps = WMI_PEER_SMPS_DYNAMIC;
4885 			break;
4886 		case IEEE80211_SMPS_NUM_MODES:
4887 			ath10k_warn(ar, "Invalid smps %d in sta rc update for %pM\n",
4888 				    sta->smps_mode, sta->addr);
4889 			smps = WMI_PEER_SMPS_PS_NONE;
4890 			break;
4891 		}
4892 
4893 		arsta->smps = smps;
4894 	}
4895 
4896 	arsta->changed |= changed;
4897 
4898 	spin_unlock_bh(&ar->data_lock);
4899 
4900 	ieee80211_queue_work(hw, &arsta->update_wk);
4901 }
4902 
4903 static u64 ath10k_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
4904 {
4905 	/*
4906 	 * FIXME: Return 0 for time being. Need to figure out whether FW
4907 	 * has the API to fetch 64-bit local TSF
4908 	 */
4909 
4910 	return 0;
4911 }
4912 
4913 static int ath10k_ampdu_action(struct ieee80211_hw *hw,
4914 			       struct ieee80211_vif *vif,
4915 			       enum ieee80211_ampdu_mlme_action action,
4916 			       struct ieee80211_sta *sta, u16 tid, u16 *ssn,
4917 			       u8 buf_size)
4918 {
4919 	struct ath10k *ar = hw->priv;
4920 	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4921 
4922 	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ampdu vdev_id %i sta %pM tid %hu action %d\n",
4923 		   arvif->vdev_id, sta->addr, tid, action);
4924 
4925 	switch (action) {
4926 	case IEEE80211_AMPDU_RX_START:
4927 	case IEEE80211_AMPDU_RX_STOP:
4928 		/* HTT AddBa/DelBa events trigger mac80211 Rx BA session
4929 		 * creation/removal. Do we need to verify this?
4930 		 */
4931 		return 0;
4932 	case IEEE80211_AMPDU_TX_START:
4933 	case IEEE80211_AMPDU_TX_STOP_CONT:
4934 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
4935 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
4936 	case IEEE80211_AMPDU_TX_OPERATIONAL:
4937 		/* Firmware offloads Tx aggregation entirely so deny mac80211
4938 		 * Tx aggregation requests.
4939 		 */
4940 		return -EOPNOTSUPP;
4941 	}
4942 
4943 	return -EINVAL;
4944 }
4945 
4946 static const struct ieee80211_ops ath10k_ops = {
4947 	.tx				= ath10k_tx,
4948 	.start				= ath10k_start,
4949 	.stop				= ath10k_stop,
4950 	.config				= ath10k_config,
4951 	.add_interface			= ath10k_add_interface,
4952 	.remove_interface		= ath10k_remove_interface,
4953 	.configure_filter		= ath10k_configure_filter,
4954 	.bss_info_changed		= ath10k_bss_info_changed,
4955 	.hw_scan			= ath10k_hw_scan,
4956 	.cancel_hw_scan			= ath10k_cancel_hw_scan,
4957 	.set_key			= ath10k_set_key,
4958 	.set_default_unicast_key        = ath10k_set_default_unicast_key,
4959 	.sta_state			= ath10k_sta_state,
4960 	.conf_tx			= ath10k_conf_tx,
4961 	.remain_on_channel		= ath10k_remain_on_channel,
4962 	.cancel_remain_on_channel	= ath10k_cancel_remain_on_channel,
4963 	.set_rts_threshold		= ath10k_set_rts_threshold,
4964 	.flush				= ath10k_flush,
4965 	.tx_last_beacon			= ath10k_tx_last_beacon,
4966 	.set_antenna			= ath10k_set_antenna,
4967 	.get_antenna			= ath10k_get_antenna,
4968 	.reconfig_complete		= ath10k_reconfig_complete,
4969 	.get_survey			= ath10k_get_survey,
4970 	.set_bitrate_mask		= ath10k_set_bitrate_mask,
4971 	.sta_rc_update			= ath10k_sta_rc_update,
4972 	.get_tsf			= ath10k_get_tsf,
4973 	.ampdu_action			= ath10k_ampdu_action,
4974 	.get_et_sset_count		= ath10k_debug_get_et_sset_count,
4975 	.get_et_stats			= ath10k_debug_get_et_stats,
4976 	.get_et_strings			= ath10k_debug_get_et_strings,
4977 
4978 	CFG80211_TESTMODE_CMD(ath10k_tm_cmd)
4979 
4980 #ifdef CONFIG_PM
4981 	.suspend			= ath10k_suspend,
4982 	.resume				= ath10k_resume,
4983 #endif
4984 #ifdef CONFIG_MAC80211_DEBUGFS
4985 	.sta_add_debugfs		= ath10k_sta_add_debugfs,
4986 #endif
4987 };
4988 
4989 #define RATETAB_ENT(_rate, _rateid, _flags) { \
4990 	.bitrate		= (_rate), \
4991 	.flags			= (_flags), \
4992 	.hw_value		= (_rateid), \
4993 }
4994 
4995 #define CHAN2G(_channel, _freq, _flags) { \
4996 	.band			= IEEE80211_BAND_2GHZ, \
4997 	.hw_value		= (_channel), \
4998 	.center_freq		= (_freq), \
4999 	.flags			= (_flags), \
5000 	.max_antenna_gain	= 0, \
5001 	.max_power		= 30, \
5002 }
5003 
5004 #define CHAN5G(_channel, _freq, _flags) { \
5005 	.band			= IEEE80211_BAND_5GHZ, \
5006 	.hw_value		= (_channel), \
5007 	.center_freq		= (_freq), \
5008 	.flags			= (_flags), \
5009 	.max_antenna_gain	= 0, \
5010 	.max_power		= 30, \
5011 }
5012 
5013 static const struct ieee80211_channel ath10k_2ghz_channels[] = {
5014 	CHAN2G(1, 2412, 0),
5015 	CHAN2G(2, 2417, 0),
5016 	CHAN2G(3, 2422, 0),
5017 	CHAN2G(4, 2427, 0),
5018 	CHAN2G(5, 2432, 0),
5019 	CHAN2G(6, 2437, 0),
5020 	CHAN2G(7, 2442, 0),
5021 	CHAN2G(8, 2447, 0),
5022 	CHAN2G(9, 2452, 0),
5023 	CHAN2G(10, 2457, 0),
5024 	CHAN2G(11, 2462, 0),
5025 	CHAN2G(12, 2467, 0),
5026 	CHAN2G(13, 2472, 0),
5027 	CHAN2G(14, 2484, 0),
5028 };
5029 
5030 static const struct ieee80211_channel ath10k_5ghz_channels[] = {
5031 	CHAN5G(36, 5180, 0),
5032 	CHAN5G(40, 5200, 0),
5033 	CHAN5G(44, 5220, 0),
5034 	CHAN5G(48, 5240, 0),
5035 	CHAN5G(52, 5260, 0),
5036 	CHAN5G(56, 5280, 0),
5037 	CHAN5G(60, 5300, 0),
5038 	CHAN5G(64, 5320, 0),
5039 	CHAN5G(100, 5500, 0),
5040 	CHAN5G(104, 5520, 0),
5041 	CHAN5G(108, 5540, 0),
5042 	CHAN5G(112, 5560, 0),
5043 	CHAN5G(116, 5580, 0),
5044 	CHAN5G(120, 5600, 0),
5045 	CHAN5G(124, 5620, 0),
5046 	CHAN5G(128, 5640, 0),
5047 	CHAN5G(132, 5660, 0),
5048 	CHAN5G(136, 5680, 0),
5049 	CHAN5G(140, 5700, 0),
5050 	CHAN5G(149, 5745, 0),
5051 	CHAN5G(153, 5765, 0),
5052 	CHAN5G(157, 5785, 0),
5053 	CHAN5G(161, 5805, 0),
5054 	CHAN5G(165, 5825, 0),
5055 };
5056 
5057 /* Note: Be careful if you re-order these. There is code which depends on this
5058  * ordering.
5059  */
5060 static struct ieee80211_rate ath10k_rates[] = {
5061 	/* CCK */
5062 	RATETAB_ENT(10,  0x82, 0),
5063 	RATETAB_ENT(20,  0x84, 0),
5064 	RATETAB_ENT(55,  0x8b, 0),
5065 	RATETAB_ENT(110, 0x96, 0),
5066 	/* OFDM */
5067 	RATETAB_ENT(60,  0x0c, 0),
5068 	RATETAB_ENT(90,  0x12, 0),
5069 	RATETAB_ENT(120, 0x18, 0),
5070 	RATETAB_ENT(180, 0x24, 0),
5071 	RATETAB_ENT(240, 0x30, 0),
5072 	RATETAB_ENT(360, 0x48, 0),
5073 	RATETAB_ENT(480, 0x60, 0),
5074 	RATETAB_ENT(540, 0x6c, 0),
5075 };
5076 
5077 #define ath10k_a_rates (ath10k_rates + 4)
5078 #define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - 4)
5079 #define ath10k_g_rates (ath10k_rates + 0)
5080 #define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
5081 
5082 struct ath10k *ath10k_mac_create(size_t priv_size)
5083 {
5084 	struct ieee80211_hw *hw;
5085 	struct ath10k *ar;
5086 
5087 	hw = ieee80211_alloc_hw(sizeof(struct ath10k) + priv_size, &ath10k_ops);
5088 	if (!hw)
5089 		return NULL;
5090 
5091 	ar = hw->priv;
5092 	ar->hw = hw;
5093 
5094 	return ar;
5095 }
5096 
5097 void ath10k_mac_destroy(struct ath10k *ar)
5098 {
5099 	ieee80211_free_hw(ar->hw);
5100 }
5101 
5102 static const struct ieee80211_iface_limit ath10k_if_limits[] = {
5103 	{
5104 	.max	= 8,
5105 	.types	= BIT(NL80211_IFTYPE_STATION)
5106 		| BIT(NL80211_IFTYPE_P2P_CLIENT)
5107 	},
5108 	{
5109 	.max	= 3,
5110 	.types	= BIT(NL80211_IFTYPE_P2P_GO)
5111 	},
5112 	{
5113 	.max	= 1,
5114 	.types	= BIT(NL80211_IFTYPE_P2P_DEVICE)
5115 	},
5116 	{
5117 	.max	= 7,
5118 	.types	= BIT(NL80211_IFTYPE_AP)
5119 	},
5120 };
5121 
5122 static const struct ieee80211_iface_limit ath10k_10x_if_limits[] = {
5123 	{
5124 	.max	= 8,
5125 	.types	= BIT(NL80211_IFTYPE_AP)
5126 	},
5127 };
5128 
5129 static const struct ieee80211_iface_combination ath10k_if_comb[] = {
5130 	{
5131 		.limits = ath10k_if_limits,
5132 		.n_limits = ARRAY_SIZE(ath10k_if_limits),
5133 		.max_interfaces = 8,
5134 		.num_different_channels = 1,
5135 		.beacon_int_infra_match = true,
5136 	},
5137 };
5138 
5139 static const struct ieee80211_iface_combination ath10k_10x_if_comb[] = {
5140 	{
5141 		.limits = ath10k_10x_if_limits,
5142 		.n_limits = ARRAY_SIZE(ath10k_10x_if_limits),
5143 		.max_interfaces = 8,
5144 		.num_different_channels = 1,
5145 		.beacon_int_infra_match = true,
5146 #ifdef CONFIG_ATH10K_DFS_CERTIFIED
5147 		.radar_detect_widths =	BIT(NL80211_CHAN_WIDTH_20_NOHT) |
5148 					BIT(NL80211_CHAN_WIDTH_20) |
5149 					BIT(NL80211_CHAN_WIDTH_40) |
5150 					BIT(NL80211_CHAN_WIDTH_80),
5151 #endif
5152 	},
5153 };
5154 
5155 static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
5156 {
5157 	struct ieee80211_sta_vht_cap vht_cap = {0};
5158 	u16 mcs_map;
5159 	int i;
5160 
5161 	vht_cap.vht_supported = 1;
5162 	vht_cap.cap = ar->vht_cap_info;
5163 
5164 	mcs_map = 0;
5165 	for (i = 0; i < 8; i++) {
5166 		if (i < ar->num_rf_chains)
5167 			mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i*2);
5168 		else
5169 			mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i*2);
5170 	}
5171 
5172 	vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
5173 	vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
5174 
5175 	return vht_cap;
5176 }
5177 
5178 static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
5179 {
5180 	int i;
5181 	struct ieee80211_sta_ht_cap ht_cap = {0};
5182 
5183 	if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
5184 		return ht_cap;
5185 
5186 	ht_cap.ht_supported = 1;
5187 	ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
5188 	ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
5189 	ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
5190 	ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
5191 	ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
5192 
5193 	if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
5194 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
5195 
5196 	if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
5197 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
5198 
5199 	if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
5200 		u32 smps;
5201 
5202 		smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
5203 		smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
5204 
5205 		ht_cap.cap |= smps;
5206 	}
5207 
5208 	if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC)
5209 		ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
5210 
5211 	if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
5212 		u32 stbc;
5213 
5214 		stbc   = ar->ht_cap_info;
5215 		stbc  &= WMI_HT_CAP_RX_STBC;
5216 		stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
5217 		stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
5218 		stbc  &= IEEE80211_HT_CAP_RX_STBC;
5219 
5220 		ht_cap.cap |= stbc;
5221 	}
5222 
5223 	if (ar->ht_cap_info & WMI_HT_CAP_LDPC)
5224 		ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
5225 
5226 	if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
5227 		ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
5228 
5229 	/* max AMSDU is implicitly taken from vht_cap_info */
5230 	if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
5231 		ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
5232 
5233 	for (i = 0; i < ar->num_rf_chains; i++)
5234 		ht_cap.mcs.rx_mask[i] = 0xFF;
5235 
5236 	ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
5237 
5238 	return ht_cap;
5239 }
5240 
5241 static void ath10k_get_arvif_iter(void *data, u8 *mac,
5242 				  struct ieee80211_vif *vif)
5243 {
5244 	struct ath10k_vif_iter *arvif_iter = data;
5245 	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5246 
5247 	if (arvif->vdev_id == arvif_iter->vdev_id)
5248 		arvif_iter->arvif = arvif;
5249 }
5250 
5251 struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
5252 {
5253 	struct ath10k_vif_iter arvif_iter;
5254 	u32 flags;
5255 
5256 	memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
5257 	arvif_iter.vdev_id = vdev_id;
5258 
5259 	flags = IEEE80211_IFACE_ITER_RESUME_ALL;
5260 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
5261 						   flags,
5262 						   ath10k_get_arvif_iter,
5263 						   &arvif_iter);
5264 	if (!arvif_iter.arvif) {
5265 		ath10k_warn(ar, "No VIF found for vdev %d\n", vdev_id);
5266 		return NULL;
5267 	}
5268 
5269 	return arvif_iter.arvif;
5270 }
5271 
5272 int ath10k_mac_register(struct ath10k *ar)
5273 {
5274 	static const u32 cipher_suites[] = {
5275 		WLAN_CIPHER_SUITE_WEP40,
5276 		WLAN_CIPHER_SUITE_WEP104,
5277 		WLAN_CIPHER_SUITE_TKIP,
5278 		WLAN_CIPHER_SUITE_CCMP,
5279 		WLAN_CIPHER_SUITE_AES_CMAC,
5280 	};
5281 	struct ieee80211_supported_band *band;
5282 	struct ieee80211_sta_vht_cap vht_cap;
5283 	struct ieee80211_sta_ht_cap ht_cap;
5284 	void *channels;
5285 	int ret;
5286 
5287 	SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
5288 
5289 	SET_IEEE80211_DEV(ar->hw, ar->dev);
5290 
5291 	ht_cap = ath10k_get_ht_cap(ar);
5292 	vht_cap = ath10k_create_vht_cap(ar);
5293 
5294 	if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
5295 		channels = kmemdup(ath10k_2ghz_channels,
5296 				   sizeof(ath10k_2ghz_channels),
5297 				   GFP_KERNEL);
5298 		if (!channels) {
5299 			ret = -ENOMEM;
5300 			goto err_free;
5301 		}
5302 
5303 		band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
5304 		band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
5305 		band->channels = channels;
5306 		band->n_bitrates = ath10k_g_rates_size;
5307 		band->bitrates = ath10k_g_rates;
5308 		band->ht_cap = ht_cap;
5309 
5310 		/* Enable the VHT support at 2.4 GHz */
5311 		band->vht_cap = vht_cap;
5312 
5313 		ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
5314 	}
5315 
5316 	if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
5317 		channels = kmemdup(ath10k_5ghz_channels,
5318 				   sizeof(ath10k_5ghz_channels),
5319 				   GFP_KERNEL);
5320 		if (!channels) {
5321 			ret = -ENOMEM;
5322 			goto err_free;
5323 		}
5324 
5325 		band = &ar->mac.sbands[IEEE80211_BAND_5GHZ];
5326 		band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
5327 		band->channels = channels;
5328 		band->n_bitrates = ath10k_a_rates_size;
5329 		band->bitrates = ath10k_a_rates;
5330 		band->ht_cap = ht_cap;
5331 		band->vht_cap = vht_cap;
5332 		ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
5333 	}
5334 
5335 	ar->hw->wiphy->interface_modes =
5336 		BIT(NL80211_IFTYPE_STATION) |
5337 		BIT(NL80211_IFTYPE_AP);
5338 
5339 	ar->hw->wiphy->available_antennas_rx = ar->supp_rx_chainmask;
5340 	ar->hw->wiphy->available_antennas_tx = ar->supp_tx_chainmask;
5341 
5342 	if (!test_bit(ATH10K_FW_FEATURE_NO_P2P, ar->fw_features))
5343 		ar->hw->wiphy->interface_modes |=
5344 			BIT(NL80211_IFTYPE_P2P_DEVICE) |
5345 			BIT(NL80211_IFTYPE_P2P_CLIENT) |
5346 			BIT(NL80211_IFTYPE_P2P_GO);
5347 
5348 	ar->hw->flags = IEEE80211_HW_SIGNAL_DBM |
5349 			IEEE80211_HW_SUPPORTS_PS |
5350 			IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
5351 			IEEE80211_HW_MFP_CAPABLE |
5352 			IEEE80211_HW_REPORTS_TX_ACK_STATUS |
5353 			IEEE80211_HW_HAS_RATE_CONTROL |
5354 			IEEE80211_HW_AP_LINK_PS |
5355 			IEEE80211_HW_SPECTRUM_MGMT |
5356 			IEEE80211_HW_SW_CRYPTO_CONTROL;
5357 
5358 	ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
5359 
5360 	if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
5361 		ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
5362 
5363 	if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
5364 		ar->hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
5365 		ar->hw->flags |= IEEE80211_HW_TX_AMPDU_SETUP_IN_HW;
5366 	}
5367 
5368 	ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
5369 	ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
5370 
5371 	ar->hw->vif_data_size = sizeof(struct ath10k_vif);
5372 	ar->hw->sta_data_size = sizeof(struct ath10k_sta);
5373 
5374 	ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
5375 
5376 	if (test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)) {
5377 		ar->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
5378 
5379 		/* Firmware delivers WPS/P2P Probe Requests frames to driver so
5380 		 * that userspace (e.g. wpa_supplicant/hostapd) can generate
5381 		 * correct Probe Responses. This is more of a hack advert..
5382 		 */
5383 		ar->hw->wiphy->probe_resp_offload |=
5384 			NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
5385 			NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
5386 			NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
5387 	}
5388 
5389 	ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
5390 	ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
5391 	ar->hw->wiphy->max_remain_on_channel_duration = 5000;
5392 
5393 	ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
5394 	ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE;
5395 
5396 	/*
5397 	 * on LL hardware queues are managed entirely by the FW
5398 	 * so we only advertise to mac we can do the queues thing
5399 	 */
5400 	ar->hw->queues = 4;
5401 
5402 	switch (ar->wmi.op_version) {
5403 	case ATH10K_FW_WMI_OP_VERSION_MAIN:
5404 	case ATH10K_FW_WMI_OP_VERSION_TLV:
5405 		ar->hw->wiphy->iface_combinations = ath10k_if_comb;
5406 		ar->hw->wiphy->n_iface_combinations =
5407 			ARRAY_SIZE(ath10k_if_comb);
5408 		ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
5409 		break;
5410 	case ATH10K_FW_WMI_OP_VERSION_10_1:
5411 	case ATH10K_FW_WMI_OP_VERSION_10_2:
5412 	case ATH10K_FW_WMI_OP_VERSION_10_2_4:
5413 		ar->hw->wiphy->iface_combinations = ath10k_10x_if_comb;
5414 		ar->hw->wiphy->n_iface_combinations =
5415 			ARRAY_SIZE(ath10k_10x_if_comb);
5416 		break;
5417 	case ATH10K_FW_WMI_OP_VERSION_UNSET:
5418 	case ATH10K_FW_WMI_OP_VERSION_MAX:
5419 		WARN_ON(1);
5420 		ret = -EINVAL;
5421 		goto err_free;
5422 	}
5423 
5424 	ar->hw->netdev_features = NETIF_F_HW_CSUM;
5425 
5426 	if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED)) {
5427 		/* Init ath dfs pattern detector */
5428 		ar->ath_common.debug_mask = ATH_DBG_DFS;
5429 		ar->dfs_detector = dfs_pattern_detector_init(&ar->ath_common,
5430 							     NL80211_DFS_UNSET);
5431 
5432 		if (!ar->dfs_detector)
5433 			ath10k_warn(ar, "failed to initialise DFS pattern detector\n");
5434 	}
5435 
5436 	ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
5437 			    ath10k_reg_notifier);
5438 	if (ret) {
5439 		ath10k_err(ar, "failed to initialise regulatory: %i\n", ret);
5440 		goto err_free;
5441 	}
5442 
5443 	ar->hw->wiphy->cipher_suites = cipher_suites;
5444 	ar->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
5445 
5446 	ret = ieee80211_register_hw(ar->hw);
5447 	if (ret) {
5448 		ath10k_err(ar, "failed to register ieee80211: %d\n", ret);
5449 		goto err_free;
5450 	}
5451 
5452 	if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
5453 		ret = regulatory_hint(ar->hw->wiphy,
5454 				      ar->ath_common.regulatory.alpha2);
5455 		if (ret)
5456 			goto err_unregister;
5457 	}
5458 
5459 	return 0;
5460 
5461 err_unregister:
5462 	ieee80211_unregister_hw(ar->hw);
5463 err_free:
5464 	kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
5465 	kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
5466 
5467 	return ret;
5468 }
5469 
5470 void ath10k_mac_unregister(struct ath10k *ar)
5471 {
5472 	ieee80211_unregister_hw(ar->hw);
5473 
5474 	if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
5475 		ar->dfs_detector->exit(ar->dfs_detector);
5476 
5477 	kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
5478 	kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
5479 
5480 	SET_IEEE80211_DEV(ar->hw, NULL);
5481 }
5482