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