xref: /linux/drivers/net/wireless/ath/ath10k/mac.c (revision 2d87650a3bf1b80f7d0d150ee1af3f8a89e5b7aa)
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 
30 /**********/
31 /* Crypto */
32 /**********/
33 
34 static int ath10k_send_key(struct ath10k_vif *arvif,
35 			   struct ieee80211_key_conf *key,
36 			   enum set_key_cmd cmd,
37 			   const u8 *macaddr)
38 {
39 	struct wmi_vdev_install_key_arg arg = {
40 		.vdev_id = arvif->vdev_id,
41 		.key_idx = key->keyidx,
42 		.key_len = key->keylen,
43 		.key_data = key->key,
44 		.macaddr = macaddr,
45 	};
46 
47 	lockdep_assert_held(&arvif->ar->conf_mutex);
48 
49 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
50 		arg.key_flags = WMI_KEY_PAIRWISE;
51 	else
52 		arg.key_flags = WMI_KEY_GROUP;
53 
54 	switch (key->cipher) {
55 	case WLAN_CIPHER_SUITE_CCMP:
56 		arg.key_cipher = WMI_CIPHER_AES_CCM;
57 		key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
58 		break;
59 	case WLAN_CIPHER_SUITE_TKIP:
60 		arg.key_cipher = WMI_CIPHER_TKIP;
61 		arg.key_txmic_len = 8;
62 		arg.key_rxmic_len = 8;
63 		break;
64 	case WLAN_CIPHER_SUITE_WEP40:
65 	case WLAN_CIPHER_SUITE_WEP104:
66 		arg.key_cipher = WMI_CIPHER_WEP;
67 		/* AP/IBSS mode requires self-key to be groupwise
68 		 * Otherwise pairwise key must be set */
69 		if (memcmp(macaddr, arvif->vif->addr, ETH_ALEN))
70 			arg.key_flags = WMI_KEY_PAIRWISE;
71 		break;
72 	default:
73 		ath10k_warn("cipher %d is not supported\n", key->cipher);
74 		return -EOPNOTSUPP;
75 	}
76 
77 	if (cmd == DISABLE_KEY) {
78 		arg.key_cipher = WMI_CIPHER_NONE;
79 		arg.key_data = NULL;
80 	}
81 
82 	return ath10k_wmi_vdev_install_key(arvif->ar, &arg);
83 }
84 
85 static int ath10k_install_key(struct ath10k_vif *arvif,
86 			      struct ieee80211_key_conf *key,
87 			      enum set_key_cmd cmd,
88 			      const u8 *macaddr)
89 {
90 	struct ath10k *ar = arvif->ar;
91 	int ret;
92 
93 	lockdep_assert_held(&ar->conf_mutex);
94 
95 	reinit_completion(&ar->install_key_done);
96 
97 	ret = ath10k_send_key(arvif, key, cmd, macaddr);
98 	if (ret)
99 		return ret;
100 
101 	ret = wait_for_completion_timeout(&ar->install_key_done, 3*HZ);
102 	if (ret == 0)
103 		return -ETIMEDOUT;
104 
105 	return 0;
106 }
107 
108 static int ath10k_install_peer_wep_keys(struct ath10k_vif *arvif,
109 					const u8 *addr)
110 {
111 	struct ath10k *ar = arvif->ar;
112 	struct ath10k_peer *peer;
113 	int ret;
114 	int i;
115 
116 	lockdep_assert_held(&ar->conf_mutex);
117 
118 	spin_lock_bh(&ar->data_lock);
119 	peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
120 	spin_unlock_bh(&ar->data_lock);
121 
122 	if (!peer)
123 		return -ENOENT;
124 
125 	for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
126 		if (arvif->wep_keys[i] == NULL)
127 			continue;
128 
129 		ret = ath10k_install_key(arvif, arvif->wep_keys[i], SET_KEY,
130 					 addr);
131 		if (ret)
132 			return ret;
133 
134 		peer->keys[i] = arvif->wep_keys[i];
135 	}
136 
137 	return 0;
138 }
139 
140 static int ath10k_clear_peer_keys(struct ath10k_vif *arvif,
141 				  const u8 *addr)
142 {
143 	struct ath10k *ar = arvif->ar;
144 	struct ath10k_peer *peer;
145 	int first_errno = 0;
146 	int ret;
147 	int i;
148 
149 	lockdep_assert_held(&ar->conf_mutex);
150 
151 	spin_lock_bh(&ar->data_lock);
152 	peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
153 	spin_unlock_bh(&ar->data_lock);
154 
155 	if (!peer)
156 		return -ENOENT;
157 
158 	for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
159 		if (peer->keys[i] == NULL)
160 			continue;
161 
162 		ret = ath10k_install_key(arvif, peer->keys[i],
163 					 DISABLE_KEY, addr);
164 		if (ret && first_errno == 0)
165 			first_errno = ret;
166 
167 		if (ret)
168 			ath10k_warn("could not remove peer wep key %d (%d)\n",
169 				    i, ret);
170 
171 		peer->keys[i] = NULL;
172 	}
173 
174 	return first_errno;
175 }
176 
177 static int ath10k_clear_vdev_key(struct ath10k_vif *arvif,
178 				 struct ieee80211_key_conf *key)
179 {
180 	struct ath10k *ar = arvif->ar;
181 	struct ath10k_peer *peer;
182 	u8 addr[ETH_ALEN];
183 	int first_errno = 0;
184 	int ret;
185 	int i;
186 
187 	lockdep_assert_held(&ar->conf_mutex);
188 
189 	for (;;) {
190 		/* since ath10k_install_key we can't hold data_lock all the
191 		 * time, so we try to remove the keys incrementally */
192 		spin_lock_bh(&ar->data_lock);
193 		i = 0;
194 		list_for_each_entry(peer, &ar->peers, list) {
195 			for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
196 				if (peer->keys[i] == key) {
197 					memcpy(addr, peer->addr, ETH_ALEN);
198 					peer->keys[i] = NULL;
199 					break;
200 				}
201 			}
202 
203 			if (i < ARRAY_SIZE(peer->keys))
204 				break;
205 		}
206 		spin_unlock_bh(&ar->data_lock);
207 
208 		if (i == ARRAY_SIZE(peer->keys))
209 			break;
210 
211 		ret = ath10k_install_key(arvif, key, DISABLE_KEY, addr);
212 		if (ret && first_errno == 0)
213 			first_errno = ret;
214 
215 		if (ret)
216 			ath10k_warn("could not remove key for %pM\n", addr);
217 	}
218 
219 	return first_errno;
220 }
221 
222 
223 /*********************/
224 /* General utilities */
225 /*********************/
226 
227 static inline enum wmi_phy_mode
228 chan_to_phymode(const struct cfg80211_chan_def *chandef)
229 {
230 	enum wmi_phy_mode phymode = MODE_UNKNOWN;
231 
232 	switch (chandef->chan->band) {
233 	case IEEE80211_BAND_2GHZ:
234 		switch (chandef->width) {
235 		case NL80211_CHAN_WIDTH_20_NOHT:
236 			phymode = MODE_11G;
237 			break;
238 		case NL80211_CHAN_WIDTH_20:
239 			phymode = MODE_11NG_HT20;
240 			break;
241 		case NL80211_CHAN_WIDTH_40:
242 			phymode = MODE_11NG_HT40;
243 			break;
244 		case NL80211_CHAN_WIDTH_5:
245 		case NL80211_CHAN_WIDTH_10:
246 		case NL80211_CHAN_WIDTH_80:
247 		case NL80211_CHAN_WIDTH_80P80:
248 		case NL80211_CHAN_WIDTH_160:
249 			phymode = MODE_UNKNOWN;
250 			break;
251 		}
252 		break;
253 	case IEEE80211_BAND_5GHZ:
254 		switch (chandef->width) {
255 		case NL80211_CHAN_WIDTH_20_NOHT:
256 			phymode = MODE_11A;
257 			break;
258 		case NL80211_CHAN_WIDTH_20:
259 			phymode = MODE_11NA_HT20;
260 			break;
261 		case NL80211_CHAN_WIDTH_40:
262 			phymode = MODE_11NA_HT40;
263 			break;
264 		case NL80211_CHAN_WIDTH_80:
265 			phymode = MODE_11AC_VHT80;
266 			break;
267 		case NL80211_CHAN_WIDTH_5:
268 		case NL80211_CHAN_WIDTH_10:
269 		case NL80211_CHAN_WIDTH_80P80:
270 		case NL80211_CHAN_WIDTH_160:
271 			phymode = MODE_UNKNOWN;
272 			break;
273 		}
274 		break;
275 	default:
276 		break;
277 	}
278 
279 	WARN_ON(phymode == MODE_UNKNOWN);
280 	return phymode;
281 }
282 
283 static u8 ath10k_parse_mpdudensity(u8 mpdudensity)
284 {
285 /*
286  * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
287  *   0 for no restriction
288  *   1 for 1/4 us
289  *   2 for 1/2 us
290  *   3 for 1 us
291  *   4 for 2 us
292  *   5 for 4 us
293  *   6 for 8 us
294  *   7 for 16 us
295  */
296 	switch (mpdudensity) {
297 	case 0:
298 		return 0;
299 	case 1:
300 	case 2:
301 	case 3:
302 	/* Our lower layer calculations limit our precision to
303 	   1 microsecond */
304 		return 1;
305 	case 4:
306 		return 2;
307 	case 5:
308 		return 4;
309 	case 6:
310 		return 8;
311 	case 7:
312 		return 16;
313 	default:
314 		return 0;
315 	}
316 }
317 
318 static int ath10k_peer_create(struct ath10k *ar, u32 vdev_id, const u8 *addr)
319 {
320 	int ret;
321 
322 	lockdep_assert_held(&ar->conf_mutex);
323 
324 	ret = ath10k_wmi_peer_create(ar, vdev_id, addr);
325 	if (ret) {
326 		ath10k_warn("Failed to create wmi peer: %i\n", ret);
327 		return ret;
328 	}
329 
330 	ret = ath10k_wait_for_peer_created(ar, vdev_id, addr);
331 	if (ret) {
332 		ath10k_warn("Failed to wait for created wmi peer: %i\n", ret);
333 		return ret;
334 	}
335 
336 	return 0;
337 }
338 
339 static int  ath10k_mac_set_rts(struct ath10k_vif *arvif, u32 value)
340 {
341 	struct ath10k *ar = arvif->ar;
342 	u32 vdev_param;
343 
344 	if (value != 0xFFFFFFFF)
345 		value = min_t(u32, arvif->ar->hw->wiphy->rts_threshold,
346 			      ATH10K_RTS_MAX);
347 
348 	vdev_param = ar->wmi.vdev_param->rts_threshold;
349 	return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
350 }
351 
352 static int ath10k_mac_set_frag(struct ath10k_vif *arvif, u32 value)
353 {
354 	struct ath10k *ar = arvif->ar;
355 	u32 vdev_param;
356 
357 	if (value != 0xFFFFFFFF)
358 		value = clamp_t(u32, arvif->ar->hw->wiphy->frag_threshold,
359 				ATH10K_FRAGMT_THRESHOLD_MIN,
360 				ATH10K_FRAGMT_THRESHOLD_MAX);
361 
362 	vdev_param = ar->wmi.vdev_param->fragmentation_threshold;
363 	return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
364 }
365 
366 static int ath10k_peer_delete(struct ath10k *ar, u32 vdev_id, const u8 *addr)
367 {
368 	int ret;
369 
370 	lockdep_assert_held(&ar->conf_mutex);
371 
372 	ret = ath10k_wmi_peer_delete(ar, vdev_id, addr);
373 	if (ret)
374 		return ret;
375 
376 	ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
377 	if (ret)
378 		return ret;
379 
380 	return 0;
381 }
382 
383 static void ath10k_peer_cleanup(struct ath10k *ar, u32 vdev_id)
384 {
385 	struct ath10k_peer *peer, *tmp;
386 
387 	lockdep_assert_held(&ar->conf_mutex);
388 
389 	spin_lock_bh(&ar->data_lock);
390 	list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
391 		if (peer->vdev_id != vdev_id)
392 			continue;
393 
394 		ath10k_warn("removing stale peer %pM from vdev_id %d\n",
395 			    peer->addr, vdev_id);
396 
397 		list_del(&peer->list);
398 		kfree(peer);
399 	}
400 	spin_unlock_bh(&ar->data_lock);
401 }
402 
403 static void ath10k_peer_cleanup_all(struct ath10k *ar)
404 {
405 	struct ath10k_peer *peer, *tmp;
406 
407 	lockdep_assert_held(&ar->conf_mutex);
408 
409 	spin_lock_bh(&ar->data_lock);
410 	list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
411 		list_del(&peer->list);
412 		kfree(peer);
413 	}
414 	spin_unlock_bh(&ar->data_lock);
415 }
416 
417 /************************/
418 /* Interface management */
419 /************************/
420 
421 static inline int ath10k_vdev_setup_sync(struct ath10k *ar)
422 {
423 	int ret;
424 
425 	lockdep_assert_held(&ar->conf_mutex);
426 
427 	ret = wait_for_completion_timeout(&ar->vdev_setup_done,
428 					  ATH10K_VDEV_SETUP_TIMEOUT_HZ);
429 	if (ret == 0)
430 		return -ETIMEDOUT;
431 
432 	return 0;
433 }
434 
435 static int ath10k_vdev_start(struct ath10k_vif *arvif)
436 {
437 	struct ath10k *ar = arvif->ar;
438 	struct ieee80211_conf *conf = &ar->hw->conf;
439 	struct ieee80211_channel *channel = conf->chandef.chan;
440 	struct wmi_vdev_start_request_arg arg = {};
441 	int ret = 0;
442 
443 	lockdep_assert_held(&ar->conf_mutex);
444 
445 	reinit_completion(&ar->vdev_setup_done);
446 
447 	arg.vdev_id = arvif->vdev_id;
448 	arg.dtim_period = arvif->dtim_period;
449 	arg.bcn_intval = arvif->beacon_interval;
450 
451 	arg.channel.freq = channel->center_freq;
452 
453 	arg.channel.band_center_freq1 = conf->chandef.center_freq1;
454 
455 	arg.channel.mode = chan_to_phymode(&conf->chandef);
456 
457 	arg.channel.min_power = 0;
458 	arg.channel.max_power = channel->max_power * 2;
459 	arg.channel.max_reg_power = channel->max_reg_power * 2;
460 	arg.channel.max_antenna_gain = channel->max_antenna_gain * 2;
461 
462 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
463 		arg.ssid = arvif->u.ap.ssid;
464 		arg.ssid_len = arvif->u.ap.ssid_len;
465 		arg.hidden_ssid = arvif->u.ap.hidden_ssid;
466 
467 		/* For now allow DFS for AP mode */
468 		arg.channel.chan_radar =
469 			!!(channel->flags & IEEE80211_CHAN_RADAR);
470 	} else if (arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
471 		arg.ssid = arvif->vif->bss_conf.ssid;
472 		arg.ssid_len = arvif->vif->bss_conf.ssid_len;
473 	}
474 
475 	ath10k_dbg(ATH10K_DBG_MAC,
476 		   "mac vdev %d start center_freq %d phymode %s\n",
477 		   arg.vdev_id, arg.channel.freq,
478 		   ath10k_wmi_phymode_str(arg.channel.mode));
479 
480 	ret = ath10k_wmi_vdev_start(ar, &arg);
481 	if (ret) {
482 		ath10k_warn("WMI vdev start failed: ret %d\n", ret);
483 		return ret;
484 	}
485 
486 	ret = ath10k_vdev_setup_sync(ar);
487 	if (ret) {
488 		ath10k_warn("vdev setup failed %d\n", ret);
489 		return ret;
490 	}
491 
492 	return ret;
493 }
494 
495 static int ath10k_vdev_stop(struct ath10k_vif *arvif)
496 {
497 	struct ath10k *ar = arvif->ar;
498 	int ret;
499 
500 	lockdep_assert_held(&ar->conf_mutex);
501 
502 	reinit_completion(&ar->vdev_setup_done);
503 
504 	ret = ath10k_wmi_vdev_stop(ar, arvif->vdev_id);
505 	if (ret) {
506 		ath10k_warn("WMI vdev stop failed: ret %d\n", ret);
507 		return ret;
508 	}
509 
510 	ret = ath10k_vdev_setup_sync(ar);
511 	if (ret) {
512 		ath10k_warn("vdev setup failed %d\n", ret);
513 		return ret;
514 	}
515 
516 	return ret;
517 }
518 
519 static int ath10k_monitor_start(struct ath10k *ar, int vdev_id)
520 {
521 	struct ieee80211_channel *channel = ar->hw->conf.chandef.chan;
522 	struct wmi_vdev_start_request_arg arg = {};
523 	int ret = 0;
524 
525 	lockdep_assert_held(&ar->conf_mutex);
526 
527 	if (!ar->monitor_present) {
528 		ath10k_warn("mac montor stop -- monitor is not present\n");
529 		return -EINVAL;
530 	}
531 
532 	arg.vdev_id = vdev_id;
533 	arg.channel.freq = channel->center_freq;
534 	arg.channel.band_center_freq1 = ar->hw->conf.chandef.center_freq1;
535 
536 	/* TODO setup this dynamically, what in case we
537 	   don't have any vifs? */
538 	arg.channel.mode = chan_to_phymode(&ar->hw->conf.chandef);
539 	arg.channel.chan_radar =
540 			!!(channel->flags & IEEE80211_CHAN_RADAR);
541 
542 	arg.channel.min_power = 0;
543 	arg.channel.max_power = channel->max_power * 2;
544 	arg.channel.max_reg_power = channel->max_reg_power * 2;
545 	arg.channel.max_antenna_gain = channel->max_antenna_gain * 2;
546 
547 	ret = ath10k_wmi_vdev_start(ar, &arg);
548 	if (ret) {
549 		ath10k_warn("Monitor vdev start failed: ret %d\n", ret);
550 		return ret;
551 	}
552 
553 	ret = ath10k_vdev_setup_sync(ar);
554 	if (ret) {
555 		ath10k_warn("Monitor vdev setup failed %d\n", ret);
556 		return ret;
557 	}
558 
559 	ret = ath10k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
560 	if (ret) {
561 		ath10k_warn("Monitor vdev up failed: %d\n", ret);
562 		goto vdev_stop;
563 	}
564 
565 	ar->monitor_vdev_id = vdev_id;
566 	ar->monitor_enabled = true;
567 
568 	return 0;
569 
570 vdev_stop:
571 	ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
572 	if (ret)
573 		ath10k_warn("Monitor vdev stop failed: %d\n", ret);
574 
575 	return ret;
576 }
577 
578 static int ath10k_monitor_stop(struct ath10k *ar)
579 {
580 	int ret = 0;
581 
582 	lockdep_assert_held(&ar->conf_mutex);
583 
584 	if (!ar->monitor_present) {
585 		ath10k_warn("mac montor stop -- monitor is not present\n");
586 		return -EINVAL;
587 	}
588 
589 	if (!ar->monitor_enabled) {
590 		ath10k_warn("mac montor stop -- monitor is not enabled\n");
591 		return -EINVAL;
592 	}
593 
594 	ret = ath10k_wmi_vdev_down(ar, ar->monitor_vdev_id);
595 	if (ret)
596 		ath10k_warn("Monitor vdev down failed: %d\n", ret);
597 
598 	ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
599 	if (ret)
600 		ath10k_warn("Monitor vdev stop failed: %d\n", ret);
601 
602 	ret = ath10k_vdev_setup_sync(ar);
603 	if (ret)
604 		ath10k_warn("Monitor_down sync failed: %d\n", ret);
605 
606 	ar->monitor_enabled = false;
607 	return ret;
608 }
609 
610 static int ath10k_monitor_create(struct ath10k *ar)
611 {
612 	int bit, ret = 0;
613 
614 	lockdep_assert_held(&ar->conf_mutex);
615 
616 	if (ar->monitor_present) {
617 		ath10k_warn("Monitor mode already enabled\n");
618 		return 0;
619 	}
620 
621 	bit = ffs(ar->free_vdev_map);
622 	if (bit == 0) {
623 		ath10k_warn("No free VDEV slots\n");
624 		return -ENOMEM;
625 	}
626 
627 	ar->monitor_vdev_id = bit - 1;
628 	ar->free_vdev_map &= ~(1 << ar->monitor_vdev_id);
629 
630 	ret = ath10k_wmi_vdev_create(ar, ar->monitor_vdev_id,
631 				     WMI_VDEV_TYPE_MONITOR,
632 				     0, ar->mac_addr);
633 	if (ret) {
634 		ath10k_warn("WMI vdev monitor create failed: ret %d\n", ret);
635 		goto vdev_fail;
636 	}
637 
638 	ath10k_dbg(ATH10K_DBG_MAC, "mac monitor vdev %d created\n",
639 		   ar->monitor_vdev_id);
640 
641 	ar->monitor_present = true;
642 	return 0;
643 
644 vdev_fail:
645 	/*
646 	 * Restore the ID to the global map.
647 	 */
648 	ar->free_vdev_map |= 1 << (ar->monitor_vdev_id);
649 	return ret;
650 }
651 
652 static int ath10k_monitor_destroy(struct ath10k *ar)
653 {
654 	int ret = 0;
655 
656 	lockdep_assert_held(&ar->conf_mutex);
657 
658 	if (!ar->monitor_present)
659 		return 0;
660 
661 	ret = ath10k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
662 	if (ret) {
663 		ath10k_warn("WMI vdev monitor delete failed: %d\n", ret);
664 		return ret;
665 	}
666 
667 	ar->free_vdev_map |= 1 << (ar->monitor_vdev_id);
668 	ar->monitor_present = false;
669 
670 	ath10k_dbg(ATH10K_DBG_MAC, "mac monitor vdev %d deleted\n",
671 		   ar->monitor_vdev_id);
672 	return ret;
673 }
674 
675 static int ath10k_start_cac(struct ath10k *ar)
676 {
677 	int ret;
678 
679 	lockdep_assert_held(&ar->conf_mutex);
680 
681 	set_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
682 
683 	ret = ath10k_monitor_create(ar);
684 	if (ret) {
685 		clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
686 		return ret;
687 	}
688 
689 	ret = ath10k_monitor_start(ar, ar->monitor_vdev_id);
690 	if (ret) {
691 		clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
692 		ath10k_monitor_destroy(ar);
693 		return ret;
694 	}
695 
696 	ath10k_dbg(ATH10K_DBG_MAC, "mac cac start monitor vdev %d\n",
697 		   ar->monitor_vdev_id);
698 
699 	return 0;
700 }
701 
702 static int ath10k_stop_cac(struct ath10k *ar)
703 {
704 	lockdep_assert_held(&ar->conf_mutex);
705 
706 	/* CAC is not running - do nothing */
707 	if (!test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags))
708 		return 0;
709 
710 	ath10k_monitor_stop(ar);
711 	ath10k_monitor_destroy(ar);
712 	clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
713 
714 	ath10k_dbg(ATH10K_DBG_MAC, "mac cac finished\n");
715 
716 	return 0;
717 }
718 
719 static const char *ath10k_dfs_state(enum nl80211_dfs_state dfs_state)
720 {
721 	switch (dfs_state) {
722 	case NL80211_DFS_USABLE:
723 		return "USABLE";
724 	case NL80211_DFS_UNAVAILABLE:
725 		return "UNAVAILABLE";
726 	case NL80211_DFS_AVAILABLE:
727 		return "AVAILABLE";
728 	default:
729 		WARN_ON(1);
730 		return "bug";
731 	}
732 }
733 
734 static void ath10k_config_radar_detection(struct ath10k *ar)
735 {
736 	struct ieee80211_channel *chan = ar->hw->conf.chandef.chan;
737 	bool radar = ar->hw->conf.radar_enabled;
738 	bool chan_radar = !!(chan->flags & IEEE80211_CHAN_RADAR);
739 	enum nl80211_dfs_state dfs_state = chan->dfs_state;
740 	int ret;
741 
742 	lockdep_assert_held(&ar->conf_mutex);
743 
744 	ath10k_dbg(ATH10K_DBG_MAC,
745 		   "mac radar config update: chan %dMHz radar %d chan radar %d chan state %s\n",
746 		   chan->center_freq, radar, chan_radar,
747 		   ath10k_dfs_state(dfs_state));
748 
749 	/*
750 	 * It's safe to call it even if CAC is not started.
751 	 * This call here guarantees changing channel, etc. will stop CAC.
752 	 */
753 	ath10k_stop_cac(ar);
754 
755 	if (!radar)
756 		return;
757 
758 	if (!chan_radar)
759 		return;
760 
761 	if (dfs_state != NL80211_DFS_USABLE)
762 		return;
763 
764 	ret = ath10k_start_cac(ar);
765 	if (ret) {
766 		/*
767 		 * Not possible to start CAC on current channel so starting
768 		 * radiation is not allowed, make this channel DFS_UNAVAILABLE
769 		 * by indicating that radar was detected.
770 		 */
771 		ath10k_warn("failed to start CAC (%d)\n", ret);
772 		ieee80211_radar_detected(ar->hw);
773 	}
774 }
775 
776 static void ath10k_control_beaconing(struct ath10k_vif *arvif,
777 				struct ieee80211_bss_conf *info)
778 {
779 	int ret = 0;
780 
781 	lockdep_assert_held(&arvif->ar->conf_mutex);
782 
783 	if (!info->enable_beacon) {
784 		ath10k_vdev_stop(arvif);
785 		return;
786 	}
787 
788 	arvif->tx_seq_no = 0x1000;
789 
790 	ret = ath10k_vdev_start(arvif);
791 	if (ret)
792 		return;
793 
794 	ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, 0, info->bssid);
795 	if (ret) {
796 		ath10k_warn("Failed to bring up VDEV: %d\n",
797 			    arvif->vdev_id);
798 		return;
799 	}
800 	ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
801 }
802 
803 static void ath10k_control_ibss(struct ath10k_vif *arvif,
804 				struct ieee80211_bss_conf *info,
805 				const u8 self_peer[ETH_ALEN])
806 {
807 	u32 vdev_param;
808 	int ret = 0;
809 
810 	lockdep_assert_held(&arvif->ar->conf_mutex);
811 
812 	if (!info->ibss_joined) {
813 		ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, self_peer);
814 		if (ret)
815 			ath10k_warn("Failed to delete IBSS self peer:%pM for VDEV:%d ret:%d\n",
816 				    self_peer, arvif->vdev_id, ret);
817 
818 		if (is_zero_ether_addr(arvif->u.ibss.bssid))
819 			return;
820 
821 		ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id,
822 					 arvif->u.ibss.bssid);
823 		if (ret) {
824 			ath10k_warn("Failed to delete IBSS BSSID peer:%pM for VDEV:%d ret:%d\n",
825 				    arvif->u.ibss.bssid, arvif->vdev_id, ret);
826 			return;
827 		}
828 
829 		memset(arvif->u.ibss.bssid, 0, ETH_ALEN);
830 
831 		return;
832 	}
833 
834 	ret = ath10k_peer_create(arvif->ar, arvif->vdev_id, self_peer);
835 	if (ret) {
836 		ath10k_warn("Failed to create IBSS self peer:%pM for VDEV:%d ret:%d\n",
837 			    self_peer, arvif->vdev_id, ret);
838 		return;
839 	}
840 
841 	vdev_param = arvif->ar->wmi.vdev_param->atim_window;
842 	ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id, vdev_param,
843 					ATH10K_DEFAULT_ATIM);
844 	if (ret)
845 		ath10k_warn("Failed to set IBSS ATIM for VDEV:%d ret:%d\n",
846 			    arvif->vdev_id, ret);
847 }
848 
849 /*
850  * Review this when mac80211 gains per-interface powersave support.
851  */
852 static int ath10k_mac_vif_setup_ps(struct ath10k_vif *arvif)
853 {
854 	struct ath10k *ar = arvif->ar;
855 	struct ieee80211_conf *conf = &ar->hw->conf;
856 	enum wmi_sta_powersave_param param;
857 	enum wmi_sta_ps_mode psmode;
858 	int ret;
859 
860 	lockdep_assert_held(&arvif->ar->conf_mutex);
861 
862 	if (arvif->vif->type != NL80211_IFTYPE_STATION)
863 		return 0;
864 
865 	if (conf->flags & IEEE80211_CONF_PS) {
866 		psmode = WMI_STA_PS_MODE_ENABLED;
867 		param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
868 
869 		ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
870 						  conf->dynamic_ps_timeout);
871 		if (ret) {
872 			ath10k_warn("Failed to set inactivity time for VDEV: %d\n",
873 				    arvif->vdev_id);
874 			return ret;
875 		}
876 	} else {
877 		psmode = WMI_STA_PS_MODE_DISABLED;
878 	}
879 
880 	ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d psmode %s\n",
881 		   arvif->vdev_id, psmode ? "enable" : "disable");
882 
883 	ret = ath10k_wmi_set_psmode(ar, arvif->vdev_id, psmode);
884 	if (ret) {
885 		ath10k_warn("Failed to set PS Mode: %d for VDEV: %d\n",
886 			    psmode, arvif->vdev_id);
887 		return ret;
888 	}
889 
890 	return 0;
891 }
892 
893 /**********************/
894 /* Station management */
895 /**********************/
896 
897 static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
898 				      struct ath10k_vif *arvif,
899 				      struct ieee80211_sta *sta,
900 				      struct ieee80211_bss_conf *bss_conf,
901 				      struct wmi_peer_assoc_complete_arg *arg)
902 {
903 	lockdep_assert_held(&ar->conf_mutex);
904 
905 	memcpy(arg->addr, sta->addr, ETH_ALEN);
906 	arg->vdev_id = arvif->vdev_id;
907 	arg->peer_aid = sta->aid;
908 	arg->peer_flags |= WMI_PEER_AUTH;
909 
910 	if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
911 		/*
912 		 * Seems FW have problems with Power Save in STA
913 		 * mode when we setup this parameter to high (eg. 5).
914 		 * Often we see that FW don't send NULL (with clean P flags)
915 		 * frame even there is info about buffered frames in beacons.
916 		 * Sometimes we have to wait more than 10 seconds before FW
917 		 * will wakeup. Often sending one ping from AP to our device
918 		 * just fail (more than 50%).
919 		 *
920 		 * Seems setting this FW parameter to 1 couse FW
921 		 * will check every beacon and will wakup immediately
922 		 * after detection buffered data.
923 		 */
924 		arg->peer_listen_intval = 1;
925 	else
926 		arg->peer_listen_intval = ar->hw->conf.listen_interval;
927 
928 	arg->peer_num_spatial_streams = 1;
929 
930 	/*
931 	 * The assoc capabilities are available only in managed mode.
932 	 */
933 	if (arvif->vdev_type == WMI_VDEV_TYPE_STA && bss_conf)
934 		arg->peer_caps = bss_conf->assoc_capability;
935 }
936 
937 static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
938 				       struct ath10k_vif *arvif,
939 				       struct wmi_peer_assoc_complete_arg *arg)
940 {
941 	struct ieee80211_vif *vif = arvif->vif;
942 	struct ieee80211_bss_conf *info = &vif->bss_conf;
943 	struct cfg80211_bss *bss;
944 	const u8 *rsnie = NULL;
945 	const u8 *wpaie = NULL;
946 
947 	lockdep_assert_held(&ar->conf_mutex);
948 
949 	bss = cfg80211_get_bss(ar->hw->wiphy, ar->hw->conf.chandef.chan,
950 			       info->bssid, NULL, 0, 0, 0);
951 	if (bss) {
952 		const struct cfg80211_bss_ies *ies;
953 
954 		rcu_read_lock();
955 		rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
956 
957 		ies = rcu_dereference(bss->ies);
958 
959 		wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
960 				WLAN_OUI_TYPE_MICROSOFT_WPA,
961 				ies->data,
962 				ies->len);
963 		rcu_read_unlock();
964 		cfg80211_put_bss(ar->hw->wiphy, bss);
965 	}
966 
967 	/* FIXME: base on RSN IE/WPA IE is a correct idea? */
968 	if (rsnie || wpaie) {
969 		ath10k_dbg(ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
970 		arg->peer_flags |= WMI_PEER_NEED_PTK_4_WAY;
971 	}
972 
973 	if (wpaie) {
974 		ath10k_dbg(ATH10K_DBG_WMI, "%s: wpa ie found\n", __func__);
975 		arg->peer_flags |= WMI_PEER_NEED_GTK_2_WAY;
976 	}
977 }
978 
979 static void ath10k_peer_assoc_h_rates(struct ath10k *ar,
980 				      struct ieee80211_sta *sta,
981 				      struct wmi_peer_assoc_complete_arg *arg)
982 {
983 	struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
984 	const struct ieee80211_supported_band *sband;
985 	const struct ieee80211_rate *rates;
986 	u32 ratemask;
987 	int i;
988 
989 	lockdep_assert_held(&ar->conf_mutex);
990 
991 	sband = ar->hw->wiphy->bands[ar->hw->conf.chandef.chan->band];
992 	ratemask = sta->supp_rates[ar->hw->conf.chandef.chan->band];
993 	rates = sband->bitrates;
994 
995 	rateset->num_rates = 0;
996 
997 	for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
998 		if (!(ratemask & 1))
999 			continue;
1000 
1001 		rateset->rates[rateset->num_rates] = rates->hw_value;
1002 		rateset->num_rates++;
1003 	}
1004 }
1005 
1006 static void ath10k_peer_assoc_h_ht(struct ath10k *ar,
1007 				   struct ieee80211_sta *sta,
1008 				   struct wmi_peer_assoc_complete_arg *arg)
1009 {
1010 	const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1011 	int smps;
1012 	int i, n;
1013 
1014 	lockdep_assert_held(&ar->conf_mutex);
1015 
1016 	if (!ht_cap->ht_supported)
1017 		return;
1018 
1019 	arg->peer_flags |= WMI_PEER_HT;
1020 	arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1021 				    ht_cap->ampdu_factor)) - 1;
1022 
1023 	arg->peer_mpdu_density =
1024 		ath10k_parse_mpdudensity(ht_cap->ampdu_density);
1025 
1026 	arg->peer_ht_caps = ht_cap->cap;
1027 	arg->peer_rate_caps |= WMI_RC_HT_FLAG;
1028 
1029 	if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
1030 		arg->peer_flags |= WMI_PEER_LDPC;
1031 
1032 	if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
1033 		arg->peer_flags |= WMI_PEER_40MHZ;
1034 		arg->peer_rate_caps |= WMI_RC_CW40_FLAG;
1035 	}
1036 
1037 	if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
1038 		arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
1039 
1040 	if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
1041 		arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
1042 
1043 	if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
1044 		arg->peer_rate_caps |= WMI_RC_TX_STBC_FLAG;
1045 		arg->peer_flags |= WMI_PEER_STBC;
1046 	}
1047 
1048 	if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
1049 		u32 stbc;
1050 		stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
1051 		stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
1052 		stbc = stbc << WMI_RC_RX_STBC_FLAG_S;
1053 		arg->peer_rate_caps |= stbc;
1054 		arg->peer_flags |= WMI_PEER_STBC;
1055 	}
1056 
1057 	smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
1058 	smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
1059 
1060 	if (smps == WLAN_HT_CAP_SM_PS_STATIC) {
1061 		arg->peer_flags |= WMI_PEER_SPATIAL_MUX;
1062 		arg->peer_flags |= WMI_PEER_STATIC_MIMOPS;
1063 	} else if (smps == WLAN_HT_CAP_SM_PS_DYNAMIC) {
1064 		arg->peer_flags |= WMI_PEER_SPATIAL_MUX;
1065 		arg->peer_flags |= WMI_PEER_DYN_MIMOPS;
1066 	}
1067 
1068 	if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
1069 		arg->peer_rate_caps |= WMI_RC_TS_FLAG;
1070 	else if (ht_cap->mcs.rx_mask[1])
1071 		arg->peer_rate_caps |= WMI_RC_DS_FLAG;
1072 
1073 	for (i = 0, n = 0; i < IEEE80211_HT_MCS_MASK_LEN*8; i++)
1074 		if (ht_cap->mcs.rx_mask[i/8] & (1 << i%8))
1075 			arg->peer_ht_rates.rates[n++] = i;
1076 
1077 	arg->peer_ht_rates.num_rates = n;
1078 	arg->peer_num_spatial_streams = max((n+7) / 8, 1);
1079 
1080 	ath10k_dbg(ATH10K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
1081 		   arg->addr,
1082 		   arg->peer_ht_rates.num_rates,
1083 		   arg->peer_num_spatial_streams);
1084 }
1085 
1086 static void ath10k_peer_assoc_h_qos_ap(struct ath10k *ar,
1087 				       struct ath10k_vif *arvif,
1088 				       struct ieee80211_sta *sta,
1089 				       struct ieee80211_bss_conf *bss_conf,
1090 				       struct wmi_peer_assoc_complete_arg *arg)
1091 {
1092 	u32 uapsd = 0;
1093 	u32 max_sp = 0;
1094 
1095 	lockdep_assert_held(&ar->conf_mutex);
1096 
1097 	if (sta->wme)
1098 		arg->peer_flags |= WMI_PEER_QOS;
1099 
1100 	if (sta->wme && sta->uapsd_queues) {
1101 		ath10k_dbg(ATH10K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
1102 			   sta->uapsd_queues, sta->max_sp);
1103 
1104 		arg->peer_flags |= WMI_PEER_APSD;
1105 		arg->peer_rate_caps |= WMI_RC_UAPSD_FLAG;
1106 
1107 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1108 			uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
1109 				 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
1110 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1111 			uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
1112 				 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
1113 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1114 			uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
1115 				 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
1116 		if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1117 			uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
1118 				 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
1119 
1120 
1121 		if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
1122 			max_sp = sta->max_sp;
1123 
1124 		ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1125 					   sta->addr,
1126 					   WMI_AP_PS_PEER_PARAM_UAPSD,
1127 					   uapsd);
1128 
1129 		ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1130 					   sta->addr,
1131 					   WMI_AP_PS_PEER_PARAM_MAX_SP,
1132 					   max_sp);
1133 
1134 		/* TODO setup this based on STA listen interval and
1135 		   beacon interval. Currently we don't know
1136 		   sta->listen_interval - mac80211 patch required.
1137 		   Currently use 10 seconds */
1138 		ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1139 					   sta->addr,
1140 					   WMI_AP_PS_PEER_PARAM_AGEOUT_TIME,
1141 					   10);
1142 	}
1143 }
1144 
1145 static void ath10k_peer_assoc_h_qos_sta(struct ath10k *ar,
1146 					struct ath10k_vif *arvif,
1147 					struct ieee80211_sta *sta,
1148 					struct ieee80211_bss_conf *bss_conf,
1149 					struct wmi_peer_assoc_complete_arg *arg)
1150 {
1151 	if (bss_conf->qos)
1152 		arg->peer_flags |= WMI_PEER_QOS;
1153 }
1154 
1155 static void ath10k_peer_assoc_h_vht(struct ath10k *ar,
1156 				    struct ieee80211_sta *sta,
1157 				    struct wmi_peer_assoc_complete_arg *arg)
1158 {
1159 	const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
1160 	u8 ampdu_factor;
1161 
1162 	if (!vht_cap->vht_supported)
1163 		return;
1164 
1165 	arg->peer_flags |= WMI_PEER_VHT;
1166 	arg->peer_vht_caps = vht_cap->cap;
1167 
1168 
1169 	ampdu_factor = (vht_cap->cap &
1170 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
1171 		       IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
1172 
1173 	/* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
1174 	 * zero in VHT IE. Using it would result in degraded throughput.
1175 	 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
1176 	 * it if VHT max_mpdu is smaller. */
1177 	arg->peer_max_mpdu = max(arg->peer_max_mpdu,
1178 				 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1179 					ampdu_factor)) - 1);
1180 
1181 	if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1182 		arg->peer_flags |= WMI_PEER_80MHZ;
1183 
1184 	arg->peer_vht_rates.rx_max_rate =
1185 		__le16_to_cpu(vht_cap->vht_mcs.rx_highest);
1186 	arg->peer_vht_rates.rx_mcs_set =
1187 		__le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
1188 	arg->peer_vht_rates.tx_max_rate =
1189 		__le16_to_cpu(vht_cap->vht_mcs.tx_highest);
1190 	arg->peer_vht_rates.tx_mcs_set =
1191 		__le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
1192 
1193 	ath10k_dbg(ATH10K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
1194 		   sta->addr, arg->peer_max_mpdu, arg->peer_flags);
1195 }
1196 
1197 static void ath10k_peer_assoc_h_qos(struct ath10k *ar,
1198 				    struct ath10k_vif *arvif,
1199 				    struct ieee80211_sta *sta,
1200 				    struct ieee80211_bss_conf *bss_conf,
1201 				    struct wmi_peer_assoc_complete_arg *arg)
1202 {
1203 	switch (arvif->vdev_type) {
1204 	case WMI_VDEV_TYPE_AP:
1205 		ath10k_peer_assoc_h_qos_ap(ar, arvif, sta, bss_conf, arg);
1206 		break;
1207 	case WMI_VDEV_TYPE_STA:
1208 		ath10k_peer_assoc_h_qos_sta(ar, arvif, sta, bss_conf, arg);
1209 		break;
1210 	default:
1211 		break;
1212 	}
1213 }
1214 
1215 static void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
1216 					struct ath10k_vif *arvif,
1217 					struct ieee80211_sta *sta,
1218 					struct wmi_peer_assoc_complete_arg *arg)
1219 {
1220 	enum wmi_phy_mode phymode = MODE_UNKNOWN;
1221 
1222 	switch (ar->hw->conf.chandef.chan->band) {
1223 	case IEEE80211_BAND_2GHZ:
1224 		if (sta->ht_cap.ht_supported) {
1225 			if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1226 				phymode = MODE_11NG_HT40;
1227 			else
1228 				phymode = MODE_11NG_HT20;
1229 		} else {
1230 			phymode = MODE_11G;
1231 		}
1232 
1233 		break;
1234 	case IEEE80211_BAND_5GHZ:
1235 		/*
1236 		 * Check VHT first.
1237 		 */
1238 		if (sta->vht_cap.vht_supported) {
1239 			if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1240 				phymode = MODE_11AC_VHT80;
1241 			else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1242 				phymode = MODE_11AC_VHT40;
1243 			else if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
1244 				phymode = MODE_11AC_VHT20;
1245 		} else if (sta->ht_cap.ht_supported) {
1246 			if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1247 				phymode = MODE_11NA_HT40;
1248 			else
1249 				phymode = MODE_11NA_HT20;
1250 		} else {
1251 			phymode = MODE_11A;
1252 		}
1253 
1254 		break;
1255 	default:
1256 		break;
1257 	}
1258 
1259 	ath10k_dbg(ATH10K_DBG_MAC, "mac peer %pM phymode %s\n",
1260 		   sta->addr, ath10k_wmi_phymode_str(phymode));
1261 
1262 	arg->peer_phymode = phymode;
1263 	WARN_ON(phymode == MODE_UNKNOWN);
1264 }
1265 
1266 static int ath10k_peer_assoc_prepare(struct ath10k *ar,
1267 				     struct ath10k_vif *arvif,
1268 				     struct ieee80211_sta *sta,
1269 				     struct ieee80211_bss_conf *bss_conf,
1270 				     struct wmi_peer_assoc_complete_arg *arg)
1271 {
1272 	lockdep_assert_held(&ar->conf_mutex);
1273 
1274 	memset(arg, 0, sizeof(*arg));
1275 
1276 	ath10k_peer_assoc_h_basic(ar, arvif, sta, bss_conf, arg);
1277 	ath10k_peer_assoc_h_crypto(ar, arvif, arg);
1278 	ath10k_peer_assoc_h_rates(ar, sta, arg);
1279 	ath10k_peer_assoc_h_ht(ar, sta, arg);
1280 	ath10k_peer_assoc_h_vht(ar, sta, arg);
1281 	ath10k_peer_assoc_h_qos(ar, arvif, sta, bss_conf, arg);
1282 	ath10k_peer_assoc_h_phymode(ar, arvif, sta, arg);
1283 
1284 	return 0;
1285 }
1286 
1287 /* can be called only in mac80211 callbacks due to `key_count` usage */
1288 static void ath10k_bss_assoc(struct ieee80211_hw *hw,
1289 			     struct ieee80211_vif *vif,
1290 			     struct ieee80211_bss_conf *bss_conf)
1291 {
1292 	struct ath10k *ar = hw->priv;
1293 	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1294 	struct wmi_peer_assoc_complete_arg peer_arg;
1295 	struct ieee80211_sta *ap_sta;
1296 	int ret;
1297 
1298 	lockdep_assert_held(&ar->conf_mutex);
1299 
1300 	rcu_read_lock();
1301 
1302 	ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
1303 	if (!ap_sta) {
1304 		ath10k_warn("Failed to find station entry for %pM\n",
1305 			    bss_conf->bssid);
1306 		rcu_read_unlock();
1307 		return;
1308 	}
1309 
1310 	ret = ath10k_peer_assoc_prepare(ar, arvif, ap_sta,
1311 					bss_conf, &peer_arg);
1312 	if (ret) {
1313 		ath10k_warn("Peer assoc prepare failed for %pM\n: %d",
1314 			    bss_conf->bssid, ret);
1315 		rcu_read_unlock();
1316 		return;
1317 	}
1318 
1319 	rcu_read_unlock();
1320 
1321 	ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
1322 	if (ret) {
1323 		ath10k_warn("Peer assoc failed for %pM\n: %d",
1324 			    bss_conf->bssid, ret);
1325 		return;
1326 	}
1327 
1328 	ath10k_dbg(ATH10K_DBG_MAC,
1329 		   "mac vdev %d up (associated) bssid %pM aid %d\n",
1330 		   arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
1331 
1332 	ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, bss_conf->aid,
1333 				 bss_conf->bssid);
1334 	if (ret)
1335 		ath10k_warn("VDEV: %d up failed: ret %d\n",
1336 			    arvif->vdev_id, ret);
1337 }
1338 
1339 /*
1340  * FIXME: flush TIDs
1341  */
1342 static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
1343 				struct ieee80211_vif *vif)
1344 {
1345 	struct ath10k *ar = hw->priv;
1346 	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1347 	int ret;
1348 
1349 	lockdep_assert_held(&ar->conf_mutex);
1350 
1351 	/*
1352 	 * For some reason, calling VDEV-DOWN before VDEV-STOP
1353 	 * makes the FW to send frames via HTT after disassociation.
1354 	 * No idea why this happens, even though VDEV-DOWN is supposed
1355 	 * to be analogous to link down, so just stop the VDEV.
1356 	 */
1357 	ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d stop (disassociated\n",
1358 		   arvif->vdev_id);
1359 
1360 	/* FIXME: check return value */
1361 	ret = ath10k_vdev_stop(arvif);
1362 
1363 	/*
1364 	 * If we don't call VDEV-DOWN after VDEV-STOP FW will remain active and
1365 	 * report beacons from previously associated network through HTT.
1366 	 * This in turn would spam mac80211 WARN_ON if we bring down all
1367 	 * interfaces as it expects there is no rx when no interface is
1368 	 * running.
1369 	 */
1370 	ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d down\n", arvif->vdev_id);
1371 
1372 	/* FIXME: why don't we print error if wmi call fails? */
1373 	ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
1374 
1375 	arvif->def_wep_key_idx = 0;
1376 }
1377 
1378 static int ath10k_station_assoc(struct ath10k *ar, struct ath10k_vif *arvif,
1379 				struct ieee80211_sta *sta)
1380 {
1381 	struct wmi_peer_assoc_complete_arg peer_arg;
1382 	int ret = 0;
1383 
1384 	lockdep_assert_held(&ar->conf_mutex);
1385 
1386 	ret = ath10k_peer_assoc_prepare(ar, arvif, sta, NULL, &peer_arg);
1387 	if (ret) {
1388 		ath10k_warn("WMI peer assoc prepare failed for %pM\n",
1389 			    sta->addr);
1390 		return ret;
1391 	}
1392 
1393 	ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
1394 	if (ret) {
1395 		ath10k_warn("Peer assoc failed for STA %pM\n: %d",
1396 			    sta->addr, ret);
1397 		return ret;
1398 	}
1399 
1400 	ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
1401 	if (ret) {
1402 		ath10k_warn("could not install peer wep keys (%d)\n", ret);
1403 		return ret;
1404 	}
1405 
1406 	return ret;
1407 }
1408 
1409 static int ath10k_station_disassoc(struct ath10k *ar, struct ath10k_vif *arvif,
1410 				   struct ieee80211_sta *sta)
1411 {
1412 	int ret = 0;
1413 
1414 	lockdep_assert_held(&ar->conf_mutex);
1415 
1416 	ret = ath10k_clear_peer_keys(arvif, sta->addr);
1417 	if (ret) {
1418 		ath10k_warn("could not clear all peer wep keys (%d)\n", ret);
1419 		return ret;
1420 	}
1421 
1422 	return ret;
1423 }
1424 
1425 /**************/
1426 /* Regulatory */
1427 /**************/
1428 
1429 static int ath10k_update_channel_list(struct ath10k *ar)
1430 {
1431 	struct ieee80211_hw *hw = ar->hw;
1432 	struct ieee80211_supported_band **bands;
1433 	enum ieee80211_band band;
1434 	struct ieee80211_channel *channel;
1435 	struct wmi_scan_chan_list_arg arg = {0};
1436 	struct wmi_channel_arg *ch;
1437 	bool passive;
1438 	int len;
1439 	int ret;
1440 	int i;
1441 
1442 	lockdep_assert_held(&ar->conf_mutex);
1443 
1444 	bands = hw->wiphy->bands;
1445 	for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1446 		if (!bands[band])
1447 			continue;
1448 
1449 		for (i = 0; i < bands[band]->n_channels; i++) {
1450 			if (bands[band]->channels[i].flags &
1451 			    IEEE80211_CHAN_DISABLED)
1452 				continue;
1453 
1454 			arg.n_channels++;
1455 		}
1456 	}
1457 
1458 	len = sizeof(struct wmi_channel_arg) * arg.n_channels;
1459 	arg.channels = kzalloc(len, GFP_KERNEL);
1460 	if (!arg.channels)
1461 		return -ENOMEM;
1462 
1463 	ch = arg.channels;
1464 	for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1465 		if (!bands[band])
1466 			continue;
1467 
1468 		for (i = 0; i < bands[band]->n_channels; i++) {
1469 			channel = &bands[band]->channels[i];
1470 
1471 			if (channel->flags & IEEE80211_CHAN_DISABLED)
1472 				continue;
1473 
1474 			ch->allow_ht   = true;
1475 
1476 			/* FIXME: when should we really allow VHT? */
1477 			ch->allow_vht = true;
1478 
1479 			ch->allow_ibss =
1480 				!(channel->flags & IEEE80211_CHAN_NO_IR);
1481 
1482 			ch->ht40plus =
1483 				!(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
1484 
1485 			ch->chan_radar =
1486 				!!(channel->flags & IEEE80211_CHAN_RADAR);
1487 
1488 			passive = channel->flags & IEEE80211_CHAN_NO_IR;
1489 			ch->passive = passive;
1490 
1491 			ch->freq = channel->center_freq;
1492 			ch->min_power = 0;
1493 			ch->max_power = channel->max_power * 2;
1494 			ch->max_reg_power = channel->max_reg_power * 2;
1495 			ch->max_antenna_gain = channel->max_antenna_gain * 2;
1496 			ch->reg_class_id = 0; /* FIXME */
1497 
1498 			/* FIXME: why use only legacy modes, why not any
1499 			 * HT/VHT modes? Would that even make any
1500 			 * difference? */
1501 			if (channel->band == IEEE80211_BAND_2GHZ)
1502 				ch->mode = MODE_11G;
1503 			else
1504 				ch->mode = MODE_11A;
1505 
1506 			if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
1507 				continue;
1508 
1509 			ath10k_dbg(ATH10K_DBG_WMI,
1510 				   "mac channel [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
1511 				    ch - arg.channels, arg.n_channels,
1512 				   ch->freq, ch->max_power, ch->max_reg_power,
1513 				   ch->max_antenna_gain, ch->mode);
1514 
1515 			ch++;
1516 		}
1517 	}
1518 
1519 	ret = ath10k_wmi_scan_chan_list(ar, &arg);
1520 	kfree(arg.channels);
1521 
1522 	return ret;
1523 }
1524 
1525 static void ath10k_regd_update(struct ath10k *ar)
1526 {
1527 	struct reg_dmn_pair_mapping *regpair;
1528 	int ret;
1529 
1530 	lockdep_assert_held(&ar->conf_mutex);
1531 
1532 	ret = ath10k_update_channel_list(ar);
1533 	if (ret)
1534 		ath10k_warn("could not update channel list (%d)\n", ret);
1535 
1536 	regpair = ar->ath_common.regulatory.regpair;
1537 
1538 	/* Target allows setting up per-band regdomain but ath_common provides
1539 	 * a combined one only */
1540 	ret = ath10k_wmi_pdev_set_regdomain(ar,
1541 					    regpair->regDmnEnum,
1542 					    regpair->regDmnEnum, /* 2ghz */
1543 					    regpair->regDmnEnum, /* 5ghz */
1544 					    regpair->reg_2ghz_ctl,
1545 					    regpair->reg_5ghz_ctl);
1546 	if (ret)
1547 		ath10k_warn("could not set pdev regdomain (%d)\n", ret);
1548 }
1549 
1550 static void ath10k_reg_notifier(struct wiphy *wiphy,
1551 				struct regulatory_request *request)
1552 {
1553 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
1554 	struct ath10k *ar = hw->priv;
1555 	bool result;
1556 
1557 	ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
1558 
1559 	if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
1560 		ath10k_dbg(ATH10K_DBG_REGULATORY, "dfs region 0x%x\n",
1561 			   request->dfs_region);
1562 		result = ar->dfs_detector->set_dfs_domain(ar->dfs_detector,
1563 							  request->dfs_region);
1564 		if (!result)
1565 			ath10k_warn("dfs region 0x%X not supported, will trigger radar for every pulse\n",
1566 				    request->dfs_region);
1567 	}
1568 
1569 	mutex_lock(&ar->conf_mutex);
1570 	if (ar->state == ATH10K_STATE_ON)
1571 		ath10k_regd_update(ar);
1572 	mutex_unlock(&ar->conf_mutex);
1573 }
1574 
1575 /***************/
1576 /* TX handlers */
1577 /***************/
1578 
1579 static u8 ath10k_tx_h_get_tid(struct ieee80211_hdr *hdr)
1580 {
1581 	if (ieee80211_is_mgmt(hdr->frame_control))
1582 		return HTT_DATA_TX_EXT_TID_MGMT;
1583 
1584 	if (!ieee80211_is_data_qos(hdr->frame_control))
1585 		return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
1586 
1587 	if (!is_unicast_ether_addr(ieee80211_get_DA(hdr)))
1588 		return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
1589 
1590 	return ieee80211_get_qos_ctl(hdr)[0] & IEEE80211_QOS_CTL_TID_MASK;
1591 }
1592 
1593 static u8 ath10k_tx_h_get_vdev_id(struct ath10k *ar,
1594 				  struct ieee80211_tx_info *info)
1595 {
1596 	if (info->control.vif)
1597 		return ath10k_vif_to_arvif(info->control.vif)->vdev_id;
1598 
1599 	if (ar->monitor_enabled)
1600 		return ar->monitor_vdev_id;
1601 
1602 	ath10k_warn("could not resolve vdev id\n");
1603 	return 0;
1604 }
1605 
1606 /*
1607  * Frames sent to the FW have to be in "Native Wifi" format.
1608  * Strip the QoS field from the 802.11 header.
1609  */
1610 static void ath10k_tx_h_qos_workaround(struct ieee80211_hw *hw,
1611 				       struct ieee80211_tx_control *control,
1612 				       struct sk_buff *skb)
1613 {
1614 	struct ieee80211_hdr *hdr = (void *)skb->data;
1615 	u8 *qos_ctl;
1616 
1617 	if (!ieee80211_is_data_qos(hdr->frame_control))
1618 		return;
1619 
1620 	qos_ctl = ieee80211_get_qos_ctl(hdr);
1621 	memmove(skb->data + IEEE80211_QOS_CTL_LEN,
1622 		skb->data, (void *)qos_ctl - (void *)skb->data);
1623 	skb_pull(skb, IEEE80211_QOS_CTL_LEN);
1624 }
1625 
1626 static void ath10k_tx_wep_key_work(struct work_struct *work)
1627 {
1628 	struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
1629 						wep_key_work);
1630 	int ret, keyidx = arvif->def_wep_key_newidx;
1631 
1632 	if (arvif->def_wep_key_idx == keyidx)
1633 		return;
1634 
1635 	ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
1636 		   arvif->vdev_id, keyidx);
1637 
1638 	ret = ath10k_wmi_vdev_set_param(arvif->ar,
1639 					arvif->vdev_id,
1640 					arvif->ar->wmi.vdev_param->def_keyid,
1641 					keyidx);
1642 	if (ret) {
1643 		ath10k_warn("could not update wep keyidx (%d)\n", ret);
1644 		return;
1645 	}
1646 
1647 	arvif->def_wep_key_idx = keyidx;
1648 }
1649 
1650 static void ath10k_tx_h_update_wep_key(struct sk_buff *skb)
1651 {
1652 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1653 	struct ieee80211_vif *vif = info->control.vif;
1654 	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1655 	struct ath10k *ar = arvif->ar;
1656 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1657 	struct ieee80211_key_conf *key = info->control.hw_key;
1658 
1659 	if (!ieee80211_has_protected(hdr->frame_control))
1660 		return;
1661 
1662 	if (!key)
1663 		return;
1664 
1665 	if (key->cipher != WLAN_CIPHER_SUITE_WEP40 &&
1666 	    key->cipher != WLAN_CIPHER_SUITE_WEP104)
1667 		return;
1668 
1669 	if (key->keyidx == arvif->def_wep_key_idx)
1670 		return;
1671 
1672 	/* FIXME: Most likely a few frames will be TXed with an old key. Simply
1673 	 * queueing frames until key index is updated is not an option because
1674 	 * sk_buff may need more processing to be done, e.g. offchannel */
1675 	arvif->def_wep_key_newidx = key->keyidx;
1676 	ieee80211_queue_work(ar->hw, &arvif->wep_key_work);
1677 }
1678 
1679 static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar, struct sk_buff *skb)
1680 {
1681 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1682 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1683 	struct ieee80211_vif *vif = info->control.vif;
1684 	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1685 
1686 	/* This is case only for P2P_GO */
1687 	if (arvif->vdev_type != WMI_VDEV_TYPE_AP ||
1688 	    arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
1689 		return;
1690 
1691 	if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
1692 		spin_lock_bh(&ar->data_lock);
1693 		if (arvif->u.ap.noa_data)
1694 			if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
1695 					      GFP_ATOMIC))
1696 				memcpy(skb_put(skb, arvif->u.ap.noa_len),
1697 				       arvif->u.ap.noa_data,
1698 				       arvif->u.ap.noa_len);
1699 		spin_unlock_bh(&ar->data_lock);
1700 	}
1701 }
1702 
1703 static void ath10k_tx_htt(struct ath10k *ar, struct sk_buff *skb)
1704 {
1705 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1706 	int ret = 0;
1707 
1708 	if (ar->htt.target_version_major >= 3) {
1709 		/* Since HTT 3.0 there is no separate mgmt tx command */
1710 		ret = ath10k_htt_tx(&ar->htt, skb);
1711 		goto exit;
1712 	}
1713 
1714 	if (ieee80211_is_mgmt(hdr->frame_control)) {
1715 		if (test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
1716 			     ar->fw_features)) {
1717 			if (skb_queue_len(&ar->wmi_mgmt_tx_queue) >=
1718 			    ATH10K_MAX_NUM_MGMT_PENDING) {
1719 				ath10k_warn("wmi mgmt_tx queue limit reached\n");
1720 				ret = -EBUSY;
1721 				goto exit;
1722 			}
1723 
1724 			skb_queue_tail(&ar->wmi_mgmt_tx_queue, skb);
1725 			ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
1726 		} else {
1727 			ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
1728 		}
1729 	} else if (!test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
1730 			     ar->fw_features) &&
1731 		   ieee80211_is_nullfunc(hdr->frame_control)) {
1732 		/* FW does not report tx status properly for NullFunc frames
1733 		 * unless they are sent through mgmt tx path. mac80211 sends
1734 		 * those frames when it detects link/beacon loss and depends
1735 		 * on the tx status to be correct. */
1736 		ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
1737 	} else {
1738 		ret = ath10k_htt_tx(&ar->htt, skb);
1739 	}
1740 
1741 exit:
1742 	if (ret) {
1743 		ath10k_warn("tx failed (%d). dropping packet.\n", ret);
1744 		ieee80211_free_txskb(ar->hw, skb);
1745 	}
1746 }
1747 
1748 void ath10k_offchan_tx_purge(struct ath10k *ar)
1749 {
1750 	struct sk_buff *skb;
1751 
1752 	for (;;) {
1753 		skb = skb_dequeue(&ar->offchan_tx_queue);
1754 		if (!skb)
1755 			break;
1756 
1757 		ieee80211_free_txskb(ar->hw, skb);
1758 	}
1759 }
1760 
1761 void ath10k_offchan_tx_work(struct work_struct *work)
1762 {
1763 	struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
1764 	struct ath10k_peer *peer;
1765 	struct ieee80211_hdr *hdr;
1766 	struct sk_buff *skb;
1767 	const u8 *peer_addr;
1768 	int vdev_id;
1769 	int ret;
1770 
1771 	/* FW requirement: We must create a peer before FW will send out
1772 	 * an offchannel frame. Otherwise the frame will be stuck and
1773 	 * never transmitted. We delete the peer upon tx completion.
1774 	 * It is unlikely that a peer for offchannel tx will already be
1775 	 * present. However it may be in some rare cases so account for that.
1776 	 * Otherwise we might remove a legitimate peer and break stuff. */
1777 
1778 	for (;;) {
1779 		skb = skb_dequeue(&ar->offchan_tx_queue);
1780 		if (!skb)
1781 			break;
1782 
1783 		mutex_lock(&ar->conf_mutex);
1784 
1785 		ath10k_dbg(ATH10K_DBG_MAC, "mac offchannel skb %p\n",
1786 			   skb);
1787 
1788 		hdr = (struct ieee80211_hdr *)skb->data;
1789 		peer_addr = ieee80211_get_DA(hdr);
1790 		vdev_id = ATH10K_SKB_CB(skb)->vdev_id;
1791 
1792 		spin_lock_bh(&ar->data_lock);
1793 		peer = ath10k_peer_find(ar, vdev_id, peer_addr);
1794 		spin_unlock_bh(&ar->data_lock);
1795 
1796 		if (peer)
1797 			/* FIXME: should this use ath10k_warn()? */
1798 			ath10k_dbg(ATH10K_DBG_MAC, "peer %pM on vdev %d already present\n",
1799 				   peer_addr, vdev_id);
1800 
1801 		if (!peer) {
1802 			ret = ath10k_peer_create(ar, vdev_id, peer_addr);
1803 			if (ret)
1804 				ath10k_warn("peer %pM on vdev %d not created (%d)\n",
1805 					    peer_addr, vdev_id, ret);
1806 		}
1807 
1808 		spin_lock_bh(&ar->data_lock);
1809 		reinit_completion(&ar->offchan_tx_completed);
1810 		ar->offchan_tx_skb = skb;
1811 		spin_unlock_bh(&ar->data_lock);
1812 
1813 		ath10k_tx_htt(ar, skb);
1814 
1815 		ret = wait_for_completion_timeout(&ar->offchan_tx_completed,
1816 						  3 * HZ);
1817 		if (ret <= 0)
1818 			ath10k_warn("timed out waiting for offchannel skb %p\n",
1819 				    skb);
1820 
1821 		if (!peer) {
1822 			ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
1823 			if (ret)
1824 				ath10k_warn("peer %pM on vdev %d not deleted (%d)\n",
1825 					    peer_addr, vdev_id, ret);
1826 		}
1827 
1828 		mutex_unlock(&ar->conf_mutex);
1829 	}
1830 }
1831 
1832 void ath10k_mgmt_over_wmi_tx_purge(struct ath10k *ar)
1833 {
1834 	struct sk_buff *skb;
1835 
1836 	for (;;) {
1837 		skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
1838 		if (!skb)
1839 			break;
1840 
1841 		ieee80211_free_txskb(ar->hw, skb);
1842 	}
1843 }
1844 
1845 void ath10k_mgmt_over_wmi_tx_work(struct work_struct *work)
1846 {
1847 	struct ath10k *ar = container_of(work, struct ath10k, wmi_mgmt_tx_work);
1848 	struct sk_buff *skb;
1849 	int ret;
1850 
1851 	for (;;) {
1852 		skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
1853 		if (!skb)
1854 			break;
1855 
1856 		ret = ath10k_wmi_mgmt_tx(ar, skb);
1857 		if (ret) {
1858 			ath10k_warn("wmi mgmt_tx failed (%d)\n", ret);
1859 			ieee80211_free_txskb(ar->hw, skb);
1860 		}
1861 	}
1862 }
1863 
1864 /************/
1865 /* Scanning */
1866 /************/
1867 
1868 /*
1869  * This gets called if we dont get a heart-beat during scan.
1870  * This may indicate the FW has hung and we need to abort the
1871  * scan manually to prevent cancel_hw_scan() from deadlocking
1872  */
1873 void ath10k_reset_scan(unsigned long ptr)
1874 {
1875 	struct ath10k *ar = (struct ath10k *)ptr;
1876 
1877 	spin_lock_bh(&ar->data_lock);
1878 	if (!ar->scan.in_progress) {
1879 		spin_unlock_bh(&ar->data_lock);
1880 		return;
1881 	}
1882 
1883 	ath10k_warn("scan timeout. resetting. fw issue?\n");
1884 
1885 	if (ar->scan.is_roc)
1886 		ieee80211_remain_on_channel_expired(ar->hw);
1887 	else
1888 		ieee80211_scan_completed(ar->hw, 1 /* aborted */);
1889 
1890 	ar->scan.in_progress = false;
1891 	complete_all(&ar->scan.completed);
1892 	spin_unlock_bh(&ar->data_lock);
1893 }
1894 
1895 static int ath10k_abort_scan(struct ath10k *ar)
1896 {
1897 	struct wmi_stop_scan_arg arg = {
1898 		.req_id = 1, /* FIXME */
1899 		.req_type = WMI_SCAN_STOP_ONE,
1900 		.u.scan_id = ATH10K_SCAN_ID,
1901 	};
1902 	int ret;
1903 
1904 	lockdep_assert_held(&ar->conf_mutex);
1905 
1906 	del_timer_sync(&ar->scan.timeout);
1907 
1908 	spin_lock_bh(&ar->data_lock);
1909 	if (!ar->scan.in_progress) {
1910 		spin_unlock_bh(&ar->data_lock);
1911 		return 0;
1912 	}
1913 
1914 	ar->scan.aborting = true;
1915 	spin_unlock_bh(&ar->data_lock);
1916 
1917 	ret = ath10k_wmi_stop_scan(ar, &arg);
1918 	if (ret) {
1919 		ath10k_warn("could not submit wmi stop scan (%d)\n", ret);
1920 		spin_lock_bh(&ar->data_lock);
1921 		ar->scan.in_progress = false;
1922 		ath10k_offchan_tx_purge(ar);
1923 		spin_unlock_bh(&ar->data_lock);
1924 		return -EIO;
1925 	}
1926 
1927 	ret = wait_for_completion_timeout(&ar->scan.completed, 3*HZ);
1928 	if (ret == 0)
1929 		ath10k_warn("timed out while waiting for scan to stop\n");
1930 
1931 	/* scan completion may be done right after we timeout here, so let's
1932 	 * check the in_progress and tell mac80211 scan is completed. if we
1933 	 * don't do that and FW fails to send us scan completion indication
1934 	 * then userspace won't be able to scan anymore */
1935 	ret = 0;
1936 
1937 	spin_lock_bh(&ar->data_lock);
1938 	if (ar->scan.in_progress) {
1939 		ath10k_warn("could not stop scan. its still in progress\n");
1940 		ar->scan.in_progress = false;
1941 		ath10k_offchan_tx_purge(ar);
1942 		ret = -ETIMEDOUT;
1943 	}
1944 	spin_unlock_bh(&ar->data_lock);
1945 
1946 	return ret;
1947 }
1948 
1949 static int ath10k_start_scan(struct ath10k *ar,
1950 			     const struct wmi_start_scan_arg *arg)
1951 {
1952 	int ret;
1953 
1954 	lockdep_assert_held(&ar->conf_mutex);
1955 
1956 	ret = ath10k_wmi_start_scan(ar, arg);
1957 	if (ret)
1958 		return ret;
1959 
1960 	ret = wait_for_completion_timeout(&ar->scan.started, 1*HZ);
1961 	if (ret == 0) {
1962 		ath10k_abort_scan(ar);
1963 		return ret;
1964 	}
1965 
1966 	/* the scan can complete earlier, before we even
1967 	 * start the timer. in that case the timer handler
1968 	 * checks ar->scan.in_progress and bails out if its
1969 	 * false. Add a 200ms margin to account event/command
1970 	 * processing. */
1971 	mod_timer(&ar->scan.timeout, jiffies +
1972 		  msecs_to_jiffies(arg->max_scan_time+200));
1973 	return 0;
1974 }
1975 
1976 /**********************/
1977 /* mac80211 callbacks */
1978 /**********************/
1979 
1980 static void ath10k_tx(struct ieee80211_hw *hw,
1981 		      struct ieee80211_tx_control *control,
1982 		      struct sk_buff *skb)
1983 {
1984 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1985 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1986 	struct ath10k *ar = hw->priv;
1987 	u8 tid, vdev_id;
1988 
1989 	/* We should disable CCK RATE due to P2P */
1990 	if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
1991 		ath10k_dbg(ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
1992 
1993 	/* we must calculate tid before we apply qos workaround
1994 	 * as we'd lose the qos control field */
1995 	tid = ath10k_tx_h_get_tid(hdr);
1996 	vdev_id = ath10k_tx_h_get_vdev_id(ar, info);
1997 
1998 	/* it makes no sense to process injected frames like that */
1999 	if (info->control.vif &&
2000 	    info->control.vif->type != NL80211_IFTYPE_MONITOR) {
2001 		ath10k_tx_h_qos_workaround(hw, control, skb);
2002 		ath10k_tx_h_update_wep_key(skb);
2003 		ath10k_tx_h_add_p2p_noa_ie(ar, skb);
2004 		ath10k_tx_h_seq_no(skb);
2005 	}
2006 
2007 	ATH10K_SKB_CB(skb)->vdev_id = vdev_id;
2008 	ATH10K_SKB_CB(skb)->htt.is_offchan = false;
2009 	ATH10K_SKB_CB(skb)->htt.tid = tid;
2010 
2011 	if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
2012 		spin_lock_bh(&ar->data_lock);
2013 		ATH10K_SKB_CB(skb)->htt.is_offchan = true;
2014 		ATH10K_SKB_CB(skb)->vdev_id = ar->scan.vdev_id;
2015 		spin_unlock_bh(&ar->data_lock);
2016 
2017 		ath10k_dbg(ATH10K_DBG_MAC, "queued offchannel skb %p\n", skb);
2018 
2019 		skb_queue_tail(&ar->offchan_tx_queue, skb);
2020 		ieee80211_queue_work(hw, &ar->offchan_tx_work);
2021 		return;
2022 	}
2023 
2024 	ath10k_tx_htt(ar, skb);
2025 }
2026 
2027 /*
2028  * Initialize various parameters with default vaules.
2029  */
2030 void ath10k_halt(struct ath10k *ar)
2031 {
2032 	lockdep_assert_held(&ar->conf_mutex);
2033 
2034 	ath10k_stop_cac(ar);
2035 	del_timer_sync(&ar->scan.timeout);
2036 	ath10k_offchan_tx_purge(ar);
2037 	ath10k_mgmt_over_wmi_tx_purge(ar);
2038 	ath10k_peer_cleanup_all(ar);
2039 	ath10k_core_stop(ar);
2040 	ath10k_hif_power_down(ar);
2041 
2042 	spin_lock_bh(&ar->data_lock);
2043 	if (ar->scan.in_progress) {
2044 		del_timer(&ar->scan.timeout);
2045 		ar->scan.in_progress = false;
2046 		ieee80211_scan_completed(ar->hw, true);
2047 	}
2048 	spin_unlock_bh(&ar->data_lock);
2049 }
2050 
2051 static int ath10k_start(struct ieee80211_hw *hw)
2052 {
2053 	struct ath10k *ar = hw->priv;
2054 	int ret = 0;
2055 
2056 	mutex_lock(&ar->conf_mutex);
2057 
2058 	if (ar->state != ATH10K_STATE_OFF &&
2059 	    ar->state != ATH10K_STATE_RESTARTING) {
2060 		ret = -EINVAL;
2061 		goto exit;
2062 	}
2063 
2064 	ret = ath10k_hif_power_up(ar);
2065 	if (ret) {
2066 		ath10k_err("could not init hif (%d)\n", ret);
2067 		ar->state = ATH10K_STATE_OFF;
2068 		goto exit;
2069 	}
2070 
2071 	ret = ath10k_core_start(ar);
2072 	if (ret) {
2073 		ath10k_err("could not init core (%d)\n", ret);
2074 		ath10k_hif_power_down(ar);
2075 		ar->state = ATH10K_STATE_OFF;
2076 		goto exit;
2077 	}
2078 
2079 	if (ar->state == ATH10K_STATE_OFF)
2080 		ar->state = ATH10K_STATE_ON;
2081 	else if (ar->state == ATH10K_STATE_RESTARTING)
2082 		ar->state = ATH10K_STATE_RESTARTED;
2083 
2084 	ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->pmf_qos, 1);
2085 	if (ret)
2086 		ath10k_warn("could not enable WMI_PDEV_PARAM_PMF_QOS (%d)\n",
2087 			    ret);
2088 
2089 	ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->dynamic_bw, 1);
2090 	if (ret)
2091 		ath10k_warn("could not init WMI_PDEV_PARAM_DYNAMIC_BW (%d)\n",
2092 			    ret);
2093 
2094 	ath10k_regd_update(ar);
2095 
2096 exit:
2097 	mutex_unlock(&ar->conf_mutex);
2098 	return 0;
2099 }
2100 
2101 static void ath10k_stop(struct ieee80211_hw *hw)
2102 {
2103 	struct ath10k *ar = hw->priv;
2104 
2105 	mutex_lock(&ar->conf_mutex);
2106 	if (ar->state == ATH10K_STATE_ON ||
2107 	    ar->state == ATH10K_STATE_RESTARTED ||
2108 	    ar->state == ATH10K_STATE_WEDGED)
2109 		ath10k_halt(ar);
2110 
2111 	ar->state = ATH10K_STATE_OFF;
2112 	mutex_unlock(&ar->conf_mutex);
2113 
2114 	ath10k_mgmt_over_wmi_tx_purge(ar);
2115 
2116 	cancel_work_sync(&ar->offchan_tx_work);
2117 	cancel_work_sync(&ar->wmi_mgmt_tx_work);
2118 	cancel_work_sync(&ar->restart_work);
2119 }
2120 
2121 static int ath10k_config_ps(struct ath10k *ar)
2122 {
2123 	struct ath10k_vif *arvif;
2124 	int ret = 0;
2125 
2126 	lockdep_assert_held(&ar->conf_mutex);
2127 
2128 	list_for_each_entry(arvif, &ar->arvifs, list) {
2129 		ret = ath10k_mac_vif_setup_ps(arvif);
2130 		if (ret) {
2131 			ath10k_warn("could not setup powersave (%d)\n", ret);
2132 			break;
2133 		}
2134 	}
2135 
2136 	return ret;
2137 }
2138 
2139 static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
2140 {
2141 	struct ath10k *ar = hw->priv;
2142 	struct ieee80211_conf *conf = &hw->conf;
2143 	int ret = 0;
2144 	u32 param;
2145 
2146 	mutex_lock(&ar->conf_mutex);
2147 
2148 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
2149 		ath10k_dbg(ATH10K_DBG_MAC,
2150 			   "mac config channel %d mhz flags 0x%x\n",
2151 			   conf->chandef.chan->center_freq,
2152 			   conf->chandef.chan->flags);
2153 
2154 		spin_lock_bh(&ar->data_lock);
2155 		ar->rx_channel = conf->chandef.chan;
2156 		spin_unlock_bh(&ar->data_lock);
2157 
2158 		ath10k_config_radar_detection(ar);
2159 	}
2160 
2161 	if (changed & IEEE80211_CONF_CHANGE_POWER) {
2162 		ath10k_dbg(ATH10K_DBG_MAC, "mac config power %d\n",
2163 			   hw->conf.power_level);
2164 
2165 		param = ar->wmi.pdev_param->txpower_limit2g;
2166 		ret = ath10k_wmi_pdev_set_param(ar, param,
2167 						hw->conf.power_level * 2);
2168 		if (ret)
2169 			ath10k_warn("mac failed to set 2g txpower %d (%d)\n",
2170 				    hw->conf.power_level, ret);
2171 
2172 		param = ar->wmi.pdev_param->txpower_limit5g;
2173 		ret = ath10k_wmi_pdev_set_param(ar, param,
2174 						hw->conf.power_level * 2);
2175 		if (ret)
2176 			ath10k_warn("mac failed to set 5g txpower %d (%d)\n",
2177 				    hw->conf.power_level, ret);
2178 	}
2179 
2180 	if (changed & IEEE80211_CONF_CHANGE_PS)
2181 		ath10k_config_ps(ar);
2182 
2183 	if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
2184 		if (conf->flags & IEEE80211_CONF_MONITOR)
2185 			ret = ath10k_monitor_create(ar);
2186 		else
2187 			ret = ath10k_monitor_destroy(ar);
2188 	}
2189 
2190 	mutex_unlock(&ar->conf_mutex);
2191 	return ret;
2192 }
2193 
2194 /*
2195  * TODO:
2196  * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
2197  * because we will send mgmt frames without CCK. This requirement
2198  * for P2P_FIND/GO_NEG should be handled by checking CCK flag
2199  * in the TX packet.
2200  */
2201 static int ath10k_add_interface(struct ieee80211_hw *hw,
2202 				struct ieee80211_vif *vif)
2203 {
2204 	struct ath10k *ar = hw->priv;
2205 	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2206 	enum wmi_sta_powersave_param param;
2207 	int ret = 0;
2208 	u32 value;
2209 	int bit;
2210 	u32 vdev_param;
2211 
2212 	mutex_lock(&ar->conf_mutex);
2213 
2214 	memset(arvif, 0, sizeof(*arvif));
2215 
2216 	arvif->ar = ar;
2217 	arvif->vif = vif;
2218 
2219 	INIT_WORK(&arvif->wep_key_work, ath10k_tx_wep_key_work);
2220 	INIT_LIST_HEAD(&arvif->list);
2221 
2222 	if ((vif->type == NL80211_IFTYPE_MONITOR) && ar->monitor_present) {
2223 		ath10k_warn("Only one monitor interface allowed\n");
2224 		ret = -EBUSY;
2225 		goto err;
2226 	}
2227 
2228 	bit = ffs(ar->free_vdev_map);
2229 	if (bit == 0) {
2230 		ret = -EBUSY;
2231 		goto err;
2232 	}
2233 
2234 	arvif->vdev_id = bit - 1;
2235 	arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
2236 
2237 	if (ar->p2p)
2238 		arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
2239 
2240 	switch (vif->type) {
2241 	case NL80211_IFTYPE_UNSPECIFIED:
2242 	case NL80211_IFTYPE_STATION:
2243 		arvif->vdev_type = WMI_VDEV_TYPE_STA;
2244 		if (vif->p2p)
2245 			arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
2246 		break;
2247 	case NL80211_IFTYPE_ADHOC:
2248 		arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
2249 		break;
2250 	case NL80211_IFTYPE_AP:
2251 		arvif->vdev_type = WMI_VDEV_TYPE_AP;
2252 
2253 		if (vif->p2p)
2254 			arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
2255 		break;
2256 	case NL80211_IFTYPE_MONITOR:
2257 		arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
2258 		break;
2259 	default:
2260 		WARN_ON(1);
2261 		break;
2262 	}
2263 
2264 	ath10k_dbg(ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d\n",
2265 		   arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype);
2266 
2267 	ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
2268 				     arvif->vdev_subtype, vif->addr);
2269 	if (ret) {
2270 		ath10k_warn("WMI vdev create failed: ret %d\n", ret);
2271 		goto err;
2272 	}
2273 
2274 	ar->free_vdev_map &= ~BIT(arvif->vdev_id);
2275 	list_add(&arvif->list, &ar->arvifs);
2276 
2277 	vdev_param = ar->wmi.vdev_param->def_keyid;
2278 	ret = ath10k_wmi_vdev_set_param(ar, 0, vdev_param,
2279 					arvif->def_wep_key_idx);
2280 	if (ret) {
2281 		ath10k_warn("Failed to set default keyid: %d\n", ret);
2282 		goto err_vdev_delete;
2283 	}
2284 
2285 	vdev_param = ar->wmi.vdev_param->tx_encap_type;
2286 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
2287 					ATH10K_HW_TXRX_NATIVE_WIFI);
2288 	/* 10.X firmware does not support this VDEV parameter. Do not warn */
2289 	if (ret && ret != -EOPNOTSUPP) {
2290 		ath10k_warn("Failed to set TX encap: %d\n", ret);
2291 		goto err_vdev_delete;
2292 	}
2293 
2294 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
2295 		ret = ath10k_peer_create(ar, arvif->vdev_id, vif->addr);
2296 		if (ret) {
2297 			ath10k_warn("Failed to create peer for AP: %d\n", ret);
2298 			goto err_vdev_delete;
2299 		}
2300 	}
2301 
2302 	if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
2303 		param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
2304 		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
2305 		ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
2306 						  param, value);
2307 		if (ret) {
2308 			ath10k_warn("Failed to set RX wake policy: %d\n", ret);
2309 			goto err_peer_delete;
2310 		}
2311 
2312 		param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
2313 		value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
2314 		ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
2315 						  param, value);
2316 		if (ret) {
2317 			ath10k_warn("Failed to set TX wake thresh: %d\n", ret);
2318 			goto err_peer_delete;
2319 		}
2320 
2321 		param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
2322 		value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
2323 		ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
2324 						  param, value);
2325 		if (ret) {
2326 			ath10k_warn("Failed to set PSPOLL count: %d\n", ret);
2327 			goto err_peer_delete;
2328 		}
2329 	}
2330 
2331 	ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
2332 	if (ret) {
2333 		ath10k_warn("failed to set rts threshold for vdev %d (%d)\n",
2334 			    arvif->vdev_id, ret);
2335 		goto err_peer_delete;
2336 	}
2337 
2338 	ret = ath10k_mac_set_frag(arvif, ar->hw->wiphy->frag_threshold);
2339 	if (ret) {
2340 		ath10k_warn("failed to set frag threshold for vdev %d (%d)\n",
2341 			    arvif->vdev_id, ret);
2342 		goto err_peer_delete;
2343 	}
2344 
2345 	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
2346 		ar->monitor_present = true;
2347 
2348 	mutex_unlock(&ar->conf_mutex);
2349 	return 0;
2350 
2351 err_peer_delete:
2352 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP)
2353 		ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
2354 
2355 err_vdev_delete:
2356 	ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
2357 	ar->free_vdev_map &= ~BIT(arvif->vdev_id);
2358 	list_del(&arvif->list);
2359 
2360 err:
2361 	mutex_unlock(&ar->conf_mutex);
2362 
2363 	return ret;
2364 }
2365 
2366 static void ath10k_remove_interface(struct ieee80211_hw *hw,
2367 				    struct ieee80211_vif *vif)
2368 {
2369 	struct ath10k *ar = hw->priv;
2370 	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2371 	int ret;
2372 
2373 	mutex_lock(&ar->conf_mutex);
2374 
2375 	cancel_work_sync(&arvif->wep_key_work);
2376 
2377 	spin_lock_bh(&ar->data_lock);
2378 	if (arvif->beacon) {
2379 		dev_kfree_skb_any(arvif->beacon);
2380 		arvif->beacon = NULL;
2381 	}
2382 	spin_unlock_bh(&ar->data_lock);
2383 
2384 	ar->free_vdev_map |= 1 << (arvif->vdev_id);
2385 	list_del(&arvif->list);
2386 
2387 	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
2388 		ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, vif->addr);
2389 		if (ret)
2390 			ath10k_warn("Failed to remove peer for AP: %d\n", ret);
2391 
2392 		kfree(arvif->u.ap.noa_data);
2393 	}
2394 
2395 	ath10k_dbg(ATH10K_DBG_MAC, "mac vdev delete %d (remove interface)\n",
2396 		   arvif->vdev_id);
2397 
2398 	ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
2399 	if (ret)
2400 		ath10k_warn("WMI vdev delete failed: %d\n", ret);
2401 
2402 	if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
2403 		ar->monitor_present = false;
2404 
2405 	ath10k_peer_cleanup(ar, arvif->vdev_id);
2406 
2407 	mutex_unlock(&ar->conf_mutex);
2408 }
2409 
2410 /*
2411  * FIXME: Has to be verified.
2412  */
2413 #define SUPPORTED_FILTERS			\
2414 	(FIF_PROMISC_IN_BSS |			\
2415 	FIF_ALLMULTI |				\
2416 	FIF_CONTROL |				\
2417 	FIF_PSPOLL |				\
2418 	FIF_OTHER_BSS |				\
2419 	FIF_BCN_PRBRESP_PROMISC |		\
2420 	FIF_PROBE_REQ |				\
2421 	FIF_FCSFAIL)
2422 
2423 static void ath10k_configure_filter(struct ieee80211_hw *hw,
2424 				    unsigned int changed_flags,
2425 				    unsigned int *total_flags,
2426 				    u64 multicast)
2427 {
2428 	struct ath10k *ar = hw->priv;
2429 	int ret;
2430 
2431 	mutex_lock(&ar->conf_mutex);
2432 
2433 	changed_flags &= SUPPORTED_FILTERS;
2434 	*total_flags &= SUPPORTED_FILTERS;
2435 	ar->filter_flags = *total_flags;
2436 
2437 	/* Monitor must not be started if it wasn't created first.
2438 	 * Promiscuous mode may be started on a non-monitor interface - in
2439 	 * such case the monitor vdev is not created so starting the
2440 	 * monitor makes no sense. Since ath10k uses no special RX filters
2441 	 * (only BSS filter in STA mode) there's no need for any special
2442 	 * action here. */
2443 	if ((ar->filter_flags & FIF_PROMISC_IN_BSS) &&
2444 	    !ar->monitor_enabled && ar->monitor_present) {
2445 		ath10k_dbg(ATH10K_DBG_MAC, "mac monitor %d start\n",
2446 			   ar->monitor_vdev_id);
2447 
2448 		ret = ath10k_monitor_start(ar, ar->monitor_vdev_id);
2449 		if (ret)
2450 			ath10k_warn("Unable to start monitor mode\n");
2451 	} else if (!(ar->filter_flags & FIF_PROMISC_IN_BSS) &&
2452 		   ar->monitor_enabled && ar->monitor_present) {
2453 		ath10k_dbg(ATH10K_DBG_MAC, "mac monitor %d stop\n",
2454 			   ar->monitor_vdev_id);
2455 
2456 		ret = ath10k_monitor_stop(ar);
2457 		if (ret)
2458 			ath10k_warn("Unable to stop monitor mode\n");
2459 	}
2460 
2461 	mutex_unlock(&ar->conf_mutex);
2462 }
2463 
2464 static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
2465 				    struct ieee80211_vif *vif,
2466 				    struct ieee80211_bss_conf *info,
2467 				    u32 changed)
2468 {
2469 	struct ath10k *ar = hw->priv;
2470 	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2471 	int ret = 0;
2472 	u32 vdev_param, pdev_param;
2473 
2474 	mutex_lock(&ar->conf_mutex);
2475 
2476 	if (changed & BSS_CHANGED_IBSS)
2477 		ath10k_control_ibss(arvif, info, vif->addr);
2478 
2479 	if (changed & BSS_CHANGED_BEACON_INT) {
2480 		arvif->beacon_interval = info->beacon_int;
2481 		vdev_param = ar->wmi.vdev_param->beacon_interval;
2482 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
2483 						arvif->beacon_interval);
2484 		ath10k_dbg(ATH10K_DBG_MAC,
2485 			   "mac vdev %d beacon_interval %d\n",
2486 			   arvif->vdev_id, arvif->beacon_interval);
2487 
2488 		if (ret)
2489 			ath10k_warn("Failed to set beacon interval for VDEV: %d\n",
2490 				    arvif->vdev_id);
2491 	}
2492 
2493 	if (changed & BSS_CHANGED_BEACON) {
2494 		ath10k_dbg(ATH10K_DBG_MAC,
2495 			   "vdev %d set beacon tx mode to staggered\n",
2496 			   arvif->vdev_id);
2497 
2498 		pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
2499 		ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
2500 						WMI_BEACON_STAGGERED_MODE);
2501 		if (ret)
2502 			ath10k_warn("Failed to set beacon mode for VDEV: %d\n",
2503 				    arvif->vdev_id);
2504 	}
2505 
2506 	if (changed & BSS_CHANGED_BEACON_INFO) {
2507 		arvif->dtim_period = info->dtim_period;
2508 
2509 		ath10k_dbg(ATH10K_DBG_MAC,
2510 			   "mac vdev %d dtim_period %d\n",
2511 			   arvif->vdev_id, arvif->dtim_period);
2512 
2513 		vdev_param = ar->wmi.vdev_param->dtim_period;
2514 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
2515 						arvif->dtim_period);
2516 		if (ret)
2517 			ath10k_warn("Failed to set dtim period for VDEV: %d\n",
2518 				    arvif->vdev_id);
2519 	}
2520 
2521 	if (changed & BSS_CHANGED_SSID &&
2522 	    vif->type == NL80211_IFTYPE_AP) {
2523 		arvif->u.ap.ssid_len = info->ssid_len;
2524 		if (info->ssid_len)
2525 			memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
2526 		arvif->u.ap.hidden_ssid = info->hidden_ssid;
2527 	}
2528 
2529 	if (changed & BSS_CHANGED_BSSID) {
2530 		if (!is_zero_ether_addr(info->bssid)) {
2531 			ath10k_dbg(ATH10K_DBG_MAC,
2532 				   "mac vdev %d create peer %pM\n",
2533 				   arvif->vdev_id, info->bssid);
2534 
2535 			ret = ath10k_peer_create(ar, arvif->vdev_id,
2536 						 info->bssid);
2537 			if (ret)
2538 				ath10k_warn("Failed to add peer %pM for vdev %d when changin bssid: %i\n",
2539 					    info->bssid, arvif->vdev_id, ret);
2540 
2541 			if (vif->type == NL80211_IFTYPE_STATION) {
2542 				/*
2543 				 * this is never erased as we it for crypto key
2544 				 * clearing; this is FW requirement
2545 				 */
2546 				memcpy(arvif->u.sta.bssid, info->bssid,
2547 				       ETH_ALEN);
2548 
2549 				ath10k_dbg(ATH10K_DBG_MAC,
2550 					   "mac vdev %d start %pM\n",
2551 					   arvif->vdev_id, info->bssid);
2552 
2553 				/* FIXME: check return value */
2554 				ret = ath10k_vdev_start(arvif);
2555 			}
2556 
2557 			/*
2558 			 * Mac80211 does not keep IBSS bssid when leaving IBSS,
2559 			 * so driver need to store it. It is needed when leaving
2560 			 * IBSS in order to remove BSSID peer.
2561 			 */
2562 			if (vif->type == NL80211_IFTYPE_ADHOC)
2563 				memcpy(arvif->u.ibss.bssid, info->bssid,
2564 				       ETH_ALEN);
2565 		}
2566 	}
2567 
2568 	if (changed & BSS_CHANGED_BEACON_ENABLED)
2569 		ath10k_control_beaconing(arvif, info);
2570 
2571 	if (changed & BSS_CHANGED_ERP_CTS_PROT) {
2572 		u32 cts_prot;
2573 		if (info->use_cts_prot)
2574 			cts_prot = 1;
2575 		else
2576 			cts_prot = 0;
2577 
2578 		ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d cts_prot %d\n",
2579 			   arvif->vdev_id, cts_prot);
2580 
2581 		vdev_param = ar->wmi.vdev_param->enable_rtscts;
2582 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
2583 						cts_prot);
2584 		if (ret)
2585 			ath10k_warn("Failed to set CTS prot for VDEV: %d\n",
2586 				    arvif->vdev_id);
2587 	}
2588 
2589 	if (changed & BSS_CHANGED_ERP_SLOT) {
2590 		u32 slottime;
2591 		if (info->use_short_slot)
2592 			slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
2593 
2594 		else
2595 			slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
2596 
2597 		ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
2598 			   arvif->vdev_id, slottime);
2599 
2600 		vdev_param = ar->wmi.vdev_param->slot_time;
2601 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
2602 						slottime);
2603 		if (ret)
2604 			ath10k_warn("Failed to set erp slot for VDEV: %d\n",
2605 				    arvif->vdev_id);
2606 	}
2607 
2608 	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
2609 		u32 preamble;
2610 		if (info->use_short_preamble)
2611 			preamble = WMI_VDEV_PREAMBLE_SHORT;
2612 		else
2613 			preamble = WMI_VDEV_PREAMBLE_LONG;
2614 
2615 		ath10k_dbg(ATH10K_DBG_MAC,
2616 			   "mac vdev %d preamble %dn",
2617 			   arvif->vdev_id, preamble);
2618 
2619 		vdev_param = ar->wmi.vdev_param->preamble;
2620 		ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
2621 						preamble);
2622 		if (ret)
2623 			ath10k_warn("Failed to set preamble for VDEV: %d\n",
2624 				    arvif->vdev_id);
2625 	}
2626 
2627 	if (changed & BSS_CHANGED_ASSOC) {
2628 		if (info->assoc)
2629 			ath10k_bss_assoc(hw, vif, info);
2630 	}
2631 
2632 	mutex_unlock(&ar->conf_mutex);
2633 }
2634 
2635 static int ath10k_hw_scan(struct ieee80211_hw *hw,
2636 			  struct ieee80211_vif *vif,
2637 			  struct cfg80211_scan_request *req)
2638 {
2639 	struct ath10k *ar = hw->priv;
2640 	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2641 	struct wmi_start_scan_arg arg;
2642 	int ret = 0;
2643 	int i;
2644 
2645 	mutex_lock(&ar->conf_mutex);
2646 
2647 	spin_lock_bh(&ar->data_lock);
2648 	if (ar->scan.in_progress) {
2649 		spin_unlock_bh(&ar->data_lock);
2650 		ret = -EBUSY;
2651 		goto exit;
2652 	}
2653 
2654 	reinit_completion(&ar->scan.started);
2655 	reinit_completion(&ar->scan.completed);
2656 	ar->scan.in_progress = true;
2657 	ar->scan.aborting = false;
2658 	ar->scan.is_roc = false;
2659 	ar->scan.vdev_id = arvif->vdev_id;
2660 	spin_unlock_bh(&ar->data_lock);
2661 
2662 	memset(&arg, 0, sizeof(arg));
2663 	ath10k_wmi_start_scan_init(ar, &arg);
2664 	arg.vdev_id = arvif->vdev_id;
2665 	arg.scan_id = ATH10K_SCAN_ID;
2666 
2667 	if (!req->no_cck)
2668 		arg.scan_ctrl_flags |= WMI_SCAN_ADD_CCK_RATES;
2669 
2670 	if (req->ie_len) {
2671 		arg.ie_len = req->ie_len;
2672 		memcpy(arg.ie, req->ie, arg.ie_len);
2673 	}
2674 
2675 	if (req->n_ssids) {
2676 		arg.n_ssids = req->n_ssids;
2677 		for (i = 0; i < arg.n_ssids; i++) {
2678 			arg.ssids[i].len  = req->ssids[i].ssid_len;
2679 			arg.ssids[i].ssid = req->ssids[i].ssid;
2680 		}
2681 	} else {
2682 		arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
2683 	}
2684 
2685 	if (req->n_channels) {
2686 		arg.n_channels = req->n_channels;
2687 		for (i = 0; i < arg.n_channels; i++)
2688 			arg.channels[i] = req->channels[i]->center_freq;
2689 	}
2690 
2691 	ret = ath10k_start_scan(ar, &arg);
2692 	if (ret) {
2693 		ath10k_warn("could not start hw scan (%d)\n", ret);
2694 		spin_lock_bh(&ar->data_lock);
2695 		ar->scan.in_progress = false;
2696 		spin_unlock_bh(&ar->data_lock);
2697 	}
2698 
2699 exit:
2700 	mutex_unlock(&ar->conf_mutex);
2701 	return ret;
2702 }
2703 
2704 static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
2705 				  struct ieee80211_vif *vif)
2706 {
2707 	struct ath10k *ar = hw->priv;
2708 	int ret;
2709 
2710 	mutex_lock(&ar->conf_mutex);
2711 	ret = ath10k_abort_scan(ar);
2712 	if (ret) {
2713 		ath10k_warn("couldn't abort scan (%d). forcefully sending scan completion to mac80211\n",
2714 			    ret);
2715 		ieee80211_scan_completed(hw, 1 /* aborted */);
2716 	}
2717 	mutex_unlock(&ar->conf_mutex);
2718 }
2719 
2720 static void ath10k_set_key_h_def_keyidx(struct ath10k *ar,
2721 					struct ath10k_vif *arvif,
2722 					enum set_key_cmd cmd,
2723 					struct ieee80211_key_conf *key)
2724 {
2725 	u32 vdev_param = arvif->ar->wmi.vdev_param->def_keyid;
2726 	int ret;
2727 
2728 	/* 10.1 firmware branch requires default key index to be set to group
2729 	 * key index after installing it. Otherwise FW/HW Txes corrupted
2730 	 * frames with multi-vif APs. This is not required for main firmware
2731 	 * branch (e.g. 636).
2732 	 *
2733 	 * FIXME: This has been tested only in AP. It remains unknown if this
2734 	 * is required for multi-vif STA interfaces on 10.1 */
2735 
2736 	if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
2737 		return;
2738 
2739 	if (key->cipher == WLAN_CIPHER_SUITE_WEP40)
2740 		return;
2741 
2742 	if (key->cipher == WLAN_CIPHER_SUITE_WEP104)
2743 		return;
2744 
2745 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
2746 		return;
2747 
2748 	if (cmd != SET_KEY)
2749 		return;
2750 
2751 	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
2752 					key->keyidx);
2753 	if (ret)
2754 		ath10k_warn("failed to set group key as default key: %d\n",
2755 			    ret);
2756 }
2757 
2758 static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
2759 			  struct ieee80211_vif *vif, struct ieee80211_sta *sta,
2760 			  struct ieee80211_key_conf *key)
2761 {
2762 	struct ath10k *ar = hw->priv;
2763 	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2764 	struct ath10k_peer *peer;
2765 	const u8 *peer_addr;
2766 	bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
2767 		      key->cipher == WLAN_CIPHER_SUITE_WEP104;
2768 	int ret = 0;
2769 
2770 	if (key->keyidx > WMI_MAX_KEY_INDEX)
2771 		return -ENOSPC;
2772 
2773 	mutex_lock(&ar->conf_mutex);
2774 
2775 	if (sta)
2776 		peer_addr = sta->addr;
2777 	else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
2778 		peer_addr = vif->bss_conf.bssid;
2779 	else
2780 		peer_addr = vif->addr;
2781 
2782 	key->hw_key_idx = key->keyidx;
2783 
2784 	/* the peer should not disappear in mid-way (unless FW goes awry) since
2785 	 * we already hold conf_mutex. we just make sure its there now. */
2786 	spin_lock_bh(&ar->data_lock);
2787 	peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
2788 	spin_unlock_bh(&ar->data_lock);
2789 
2790 	if (!peer) {
2791 		if (cmd == SET_KEY) {
2792 			ath10k_warn("cannot install key for non-existent peer %pM\n",
2793 				    peer_addr);
2794 			ret = -EOPNOTSUPP;
2795 			goto exit;
2796 		} else {
2797 			/* if the peer doesn't exist there is no key to disable
2798 			 * anymore */
2799 			goto exit;
2800 		}
2801 	}
2802 
2803 	if (is_wep) {
2804 		if (cmd == SET_KEY)
2805 			arvif->wep_keys[key->keyidx] = key;
2806 		else
2807 			arvif->wep_keys[key->keyidx] = NULL;
2808 
2809 		if (cmd == DISABLE_KEY)
2810 			ath10k_clear_vdev_key(arvif, key);
2811 	}
2812 
2813 	ret = ath10k_install_key(arvif, key, cmd, peer_addr);
2814 	if (ret) {
2815 		ath10k_warn("ath10k_install_key failed (%d)\n", ret);
2816 		goto exit;
2817 	}
2818 
2819 	ath10k_set_key_h_def_keyidx(ar, arvif, cmd, key);
2820 
2821 	spin_lock_bh(&ar->data_lock);
2822 	peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
2823 	if (peer && cmd == SET_KEY)
2824 		peer->keys[key->keyidx] = key;
2825 	else if (peer && cmd == DISABLE_KEY)
2826 		peer->keys[key->keyidx] = NULL;
2827 	else if (peer == NULL)
2828 		/* impossible unless FW goes crazy */
2829 		ath10k_warn("peer %pM disappeared!\n", peer_addr);
2830 	spin_unlock_bh(&ar->data_lock);
2831 
2832 exit:
2833 	mutex_unlock(&ar->conf_mutex);
2834 	return ret;
2835 }
2836 
2837 static int ath10k_sta_state(struct ieee80211_hw *hw,
2838 			    struct ieee80211_vif *vif,
2839 			    struct ieee80211_sta *sta,
2840 			    enum ieee80211_sta_state old_state,
2841 			    enum ieee80211_sta_state new_state)
2842 {
2843 	struct ath10k *ar = hw->priv;
2844 	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2845 	int ret = 0;
2846 
2847 	mutex_lock(&ar->conf_mutex);
2848 
2849 	if (old_state == IEEE80211_STA_NOTEXIST &&
2850 	    new_state == IEEE80211_STA_NONE &&
2851 	    vif->type != NL80211_IFTYPE_STATION) {
2852 		/*
2853 		 * New station addition.
2854 		 */
2855 		ath10k_dbg(ATH10K_DBG_MAC,
2856 			   "mac vdev %d peer create %pM (new sta)\n",
2857 			   arvif->vdev_id, sta->addr);
2858 
2859 		ret = ath10k_peer_create(ar, arvif->vdev_id, sta->addr);
2860 		if (ret)
2861 			ath10k_warn("Failed to add peer %pM for vdev %d when adding a new sta: %i\n",
2862 				    sta->addr, arvif->vdev_id, ret);
2863 	} else if ((old_state == IEEE80211_STA_NONE &&
2864 		    new_state == IEEE80211_STA_NOTEXIST)) {
2865 		/*
2866 		 * Existing station deletion.
2867 		 */
2868 		ath10k_dbg(ATH10K_DBG_MAC,
2869 			   "mac vdev %d peer delete %pM (sta gone)\n",
2870 			   arvif->vdev_id, sta->addr);
2871 		ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
2872 		if (ret)
2873 			ath10k_warn("Failed to delete peer: %pM for VDEV: %d\n",
2874 				    sta->addr, arvif->vdev_id);
2875 
2876 		if (vif->type == NL80211_IFTYPE_STATION)
2877 			ath10k_bss_disassoc(hw, vif);
2878 	} else if (old_state == IEEE80211_STA_AUTH &&
2879 		   new_state == IEEE80211_STA_ASSOC &&
2880 		   (vif->type == NL80211_IFTYPE_AP ||
2881 		    vif->type == NL80211_IFTYPE_ADHOC)) {
2882 		/*
2883 		 * New association.
2884 		 */
2885 		ath10k_dbg(ATH10K_DBG_MAC, "mac sta %pM associated\n",
2886 			   sta->addr);
2887 
2888 		ret = ath10k_station_assoc(ar, arvif, sta);
2889 		if (ret)
2890 			ath10k_warn("Failed to associate station: %pM\n",
2891 				    sta->addr);
2892 	} else if (old_state == IEEE80211_STA_ASSOC &&
2893 		   new_state == IEEE80211_STA_AUTH &&
2894 		   (vif->type == NL80211_IFTYPE_AP ||
2895 		    vif->type == NL80211_IFTYPE_ADHOC)) {
2896 		/*
2897 		 * Disassociation.
2898 		 */
2899 		ath10k_dbg(ATH10K_DBG_MAC, "mac sta %pM disassociated\n",
2900 			   sta->addr);
2901 
2902 		ret = ath10k_station_disassoc(ar, arvif, sta);
2903 		if (ret)
2904 			ath10k_warn("Failed to disassociate station: %pM\n",
2905 				    sta->addr);
2906 	}
2907 
2908 	mutex_unlock(&ar->conf_mutex);
2909 	return ret;
2910 }
2911 
2912 static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
2913 				 u16 ac, bool enable)
2914 {
2915 	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2916 	u32 value = 0;
2917 	int ret = 0;
2918 
2919 	lockdep_assert_held(&ar->conf_mutex);
2920 
2921 	if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
2922 		return 0;
2923 
2924 	switch (ac) {
2925 	case IEEE80211_AC_VO:
2926 		value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
2927 			WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
2928 		break;
2929 	case IEEE80211_AC_VI:
2930 		value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
2931 			WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
2932 		break;
2933 	case IEEE80211_AC_BE:
2934 		value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
2935 			WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
2936 		break;
2937 	case IEEE80211_AC_BK:
2938 		value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
2939 			WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
2940 		break;
2941 	}
2942 
2943 	if (enable)
2944 		arvif->u.sta.uapsd |= value;
2945 	else
2946 		arvif->u.sta.uapsd &= ~value;
2947 
2948 	ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
2949 					  WMI_STA_PS_PARAM_UAPSD,
2950 					  arvif->u.sta.uapsd);
2951 	if (ret) {
2952 		ath10k_warn("could not set uapsd params %d\n", ret);
2953 		goto exit;
2954 	}
2955 
2956 	if (arvif->u.sta.uapsd)
2957 		value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
2958 	else
2959 		value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
2960 
2961 	ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
2962 					  WMI_STA_PS_PARAM_RX_WAKE_POLICY,
2963 					  value);
2964 	if (ret)
2965 		ath10k_warn("could not set rx wake param %d\n", ret);
2966 
2967 exit:
2968 	return ret;
2969 }
2970 
2971 static int ath10k_conf_tx(struct ieee80211_hw *hw,
2972 			  struct ieee80211_vif *vif, u16 ac,
2973 			  const struct ieee80211_tx_queue_params *params)
2974 {
2975 	struct ath10k *ar = hw->priv;
2976 	struct wmi_wmm_params_arg *p = NULL;
2977 	int ret;
2978 
2979 	mutex_lock(&ar->conf_mutex);
2980 
2981 	switch (ac) {
2982 	case IEEE80211_AC_VO:
2983 		p = &ar->wmm_params.ac_vo;
2984 		break;
2985 	case IEEE80211_AC_VI:
2986 		p = &ar->wmm_params.ac_vi;
2987 		break;
2988 	case IEEE80211_AC_BE:
2989 		p = &ar->wmm_params.ac_be;
2990 		break;
2991 	case IEEE80211_AC_BK:
2992 		p = &ar->wmm_params.ac_bk;
2993 		break;
2994 	}
2995 
2996 	if (WARN_ON(!p)) {
2997 		ret = -EINVAL;
2998 		goto exit;
2999 	}
3000 
3001 	p->cwmin = params->cw_min;
3002 	p->cwmax = params->cw_max;
3003 	p->aifs = params->aifs;
3004 
3005 	/*
3006 	 * The channel time duration programmed in the HW is in absolute
3007 	 * microseconds, while mac80211 gives the txop in units of
3008 	 * 32 microseconds.
3009 	 */
3010 	p->txop = params->txop * 32;
3011 
3012 	/* FIXME: FW accepts wmm params per hw, not per vif */
3013 	ret = ath10k_wmi_pdev_set_wmm_params(ar, &ar->wmm_params);
3014 	if (ret) {
3015 		ath10k_warn("could not set wmm params %d\n", ret);
3016 		goto exit;
3017 	}
3018 
3019 	ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
3020 	if (ret)
3021 		ath10k_warn("could not set sta uapsd %d\n", ret);
3022 
3023 exit:
3024 	mutex_unlock(&ar->conf_mutex);
3025 	return ret;
3026 }
3027 
3028 #define ATH10K_ROC_TIMEOUT_HZ (2*HZ)
3029 
3030 static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
3031 				    struct ieee80211_vif *vif,
3032 				    struct ieee80211_channel *chan,
3033 				    int duration,
3034 				    enum ieee80211_roc_type type)
3035 {
3036 	struct ath10k *ar = hw->priv;
3037 	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3038 	struct wmi_start_scan_arg arg;
3039 	int ret;
3040 
3041 	mutex_lock(&ar->conf_mutex);
3042 
3043 	spin_lock_bh(&ar->data_lock);
3044 	if (ar->scan.in_progress) {
3045 		spin_unlock_bh(&ar->data_lock);
3046 		ret = -EBUSY;
3047 		goto exit;
3048 	}
3049 
3050 	reinit_completion(&ar->scan.started);
3051 	reinit_completion(&ar->scan.completed);
3052 	reinit_completion(&ar->scan.on_channel);
3053 	ar->scan.in_progress = true;
3054 	ar->scan.aborting = false;
3055 	ar->scan.is_roc = true;
3056 	ar->scan.vdev_id = arvif->vdev_id;
3057 	ar->scan.roc_freq = chan->center_freq;
3058 	spin_unlock_bh(&ar->data_lock);
3059 
3060 	memset(&arg, 0, sizeof(arg));
3061 	ath10k_wmi_start_scan_init(ar, &arg);
3062 	arg.vdev_id = arvif->vdev_id;
3063 	arg.scan_id = ATH10K_SCAN_ID;
3064 	arg.n_channels = 1;
3065 	arg.channels[0] = chan->center_freq;
3066 	arg.dwell_time_active = duration;
3067 	arg.dwell_time_passive = duration;
3068 	arg.max_scan_time = 2 * duration;
3069 	arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
3070 	arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
3071 
3072 	ret = ath10k_start_scan(ar, &arg);
3073 	if (ret) {
3074 		ath10k_warn("could not start roc scan (%d)\n", ret);
3075 		spin_lock_bh(&ar->data_lock);
3076 		ar->scan.in_progress = false;
3077 		spin_unlock_bh(&ar->data_lock);
3078 		goto exit;
3079 	}
3080 
3081 	ret = wait_for_completion_timeout(&ar->scan.on_channel, 3*HZ);
3082 	if (ret == 0) {
3083 		ath10k_warn("could not switch to channel for roc scan\n");
3084 		ath10k_abort_scan(ar);
3085 		ret = -ETIMEDOUT;
3086 		goto exit;
3087 	}
3088 
3089 	ret = 0;
3090 exit:
3091 	mutex_unlock(&ar->conf_mutex);
3092 	return ret;
3093 }
3094 
3095 static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw)
3096 {
3097 	struct ath10k *ar = hw->priv;
3098 
3099 	mutex_lock(&ar->conf_mutex);
3100 	ath10k_abort_scan(ar);
3101 	mutex_unlock(&ar->conf_mutex);
3102 
3103 	return 0;
3104 }
3105 
3106 /*
3107  * Both RTS and Fragmentation threshold are interface-specific
3108  * in ath10k, but device-specific in mac80211.
3109  */
3110 
3111 static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3112 {
3113 	struct ath10k *ar = hw->priv;
3114 	struct ath10k_vif *arvif;
3115 	int ret = 0;
3116 
3117 	mutex_lock(&ar->conf_mutex);
3118 	list_for_each_entry(arvif, &ar->arvifs, list) {
3119 		ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
3120 			   arvif->vdev_id, value);
3121 
3122 		ret = ath10k_mac_set_rts(arvif, value);
3123 		if (ret) {
3124 			ath10k_warn("could not set rts threshold for vdev %d (%d)\n",
3125 				    arvif->vdev_id, ret);
3126 			break;
3127 		}
3128 	}
3129 	mutex_unlock(&ar->conf_mutex);
3130 
3131 	return ret;
3132 }
3133 
3134 static int ath10k_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
3135 {
3136 	struct ath10k *ar = hw->priv;
3137 	struct ath10k_vif *arvif;
3138 	int ret = 0;
3139 
3140 	mutex_lock(&ar->conf_mutex);
3141 	list_for_each_entry(arvif, &ar->arvifs, list) {
3142 		ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d fragmentation threshold %d\n",
3143 			   arvif->vdev_id, value);
3144 
3145 		ret = ath10k_mac_set_rts(arvif, value);
3146 		if (ret) {
3147 			ath10k_warn("could not set fragmentation threshold for vdev %d (%d)\n",
3148 				    arvif->vdev_id, ret);
3149 			break;
3150 		}
3151 	}
3152 	mutex_unlock(&ar->conf_mutex);
3153 
3154 	return ret;
3155 }
3156 
3157 static void ath10k_flush(struct ieee80211_hw *hw, u32 queues, bool drop)
3158 {
3159 	struct ath10k *ar = hw->priv;
3160 	bool skip;
3161 	int ret;
3162 
3163 	/* mac80211 doesn't care if we really xmit queued frames or not
3164 	 * we'll collect those frames either way if we stop/delete vdevs */
3165 	if (drop)
3166 		return;
3167 
3168 	mutex_lock(&ar->conf_mutex);
3169 
3170 	if (ar->state == ATH10K_STATE_WEDGED)
3171 		goto skip;
3172 
3173 	ret = wait_event_timeout(ar->htt.empty_tx_wq, ({
3174 			bool empty;
3175 
3176 			spin_lock_bh(&ar->htt.tx_lock);
3177 			empty = (ar->htt.num_pending_tx == 0);
3178 			spin_unlock_bh(&ar->htt.tx_lock);
3179 
3180 			skip = (ar->state == ATH10K_STATE_WEDGED);
3181 
3182 			(empty || skip);
3183 		}), ATH10K_FLUSH_TIMEOUT_HZ);
3184 
3185 	if (ret <= 0 || skip)
3186 		ath10k_warn("tx not flushed\n");
3187 
3188 skip:
3189 	mutex_unlock(&ar->conf_mutex);
3190 }
3191 
3192 /* TODO: Implement this function properly
3193  * For now it is needed to reply to Probe Requests in IBSS mode.
3194  * Propably we need this information from FW.
3195  */
3196 static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
3197 {
3198 	return 1;
3199 }
3200 
3201 #ifdef CONFIG_PM
3202 static int ath10k_suspend(struct ieee80211_hw *hw,
3203 			  struct cfg80211_wowlan *wowlan)
3204 {
3205 	struct ath10k *ar = hw->priv;
3206 	int ret;
3207 
3208 	ar->is_target_paused = false;
3209 
3210 	ret = ath10k_wmi_pdev_suspend_target(ar);
3211 	if (ret) {
3212 		ath10k_warn("could not suspend target (%d)\n", ret);
3213 		return 1;
3214 	}
3215 
3216 	ret = wait_event_interruptible_timeout(ar->event_queue,
3217 					       ar->is_target_paused == true,
3218 					       1 * HZ);
3219 	if (ret < 0) {
3220 		ath10k_warn("suspend interrupted (%d)\n", ret);
3221 		goto resume;
3222 	} else if (ret == 0) {
3223 		ath10k_warn("suspend timed out - target pause event never came\n");
3224 		goto resume;
3225 	}
3226 
3227 	ret = ath10k_hif_suspend(ar);
3228 	if (ret) {
3229 		ath10k_warn("could not suspend hif (%d)\n", ret);
3230 		goto resume;
3231 	}
3232 
3233 	return 0;
3234 resume:
3235 	ret = ath10k_wmi_pdev_resume_target(ar);
3236 	if (ret)
3237 		ath10k_warn("could not resume target (%d)\n", ret);
3238 	return 1;
3239 }
3240 
3241 static int ath10k_resume(struct ieee80211_hw *hw)
3242 {
3243 	struct ath10k *ar = hw->priv;
3244 	int ret;
3245 
3246 	ret = ath10k_hif_resume(ar);
3247 	if (ret) {
3248 		ath10k_warn("could not resume hif (%d)\n", ret);
3249 		return 1;
3250 	}
3251 
3252 	ret = ath10k_wmi_pdev_resume_target(ar);
3253 	if (ret) {
3254 		ath10k_warn("could not resume target (%d)\n", ret);
3255 		return 1;
3256 	}
3257 
3258 	return 0;
3259 }
3260 #endif
3261 
3262 static void ath10k_restart_complete(struct ieee80211_hw *hw)
3263 {
3264 	struct ath10k *ar = hw->priv;
3265 
3266 	mutex_lock(&ar->conf_mutex);
3267 
3268 	/* If device failed to restart it will be in a different state, e.g.
3269 	 * ATH10K_STATE_WEDGED */
3270 	if (ar->state == ATH10K_STATE_RESTARTED) {
3271 		ath10k_info("device successfully recovered\n");
3272 		ar->state = ATH10K_STATE_ON;
3273 	}
3274 
3275 	mutex_unlock(&ar->conf_mutex);
3276 }
3277 
3278 static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
3279 			     struct survey_info *survey)
3280 {
3281 	struct ath10k *ar = hw->priv;
3282 	struct ieee80211_supported_band *sband;
3283 	struct survey_info *ar_survey = &ar->survey[idx];
3284 	int ret = 0;
3285 
3286 	mutex_lock(&ar->conf_mutex);
3287 
3288 	sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
3289 	if (sband && idx >= sband->n_channels) {
3290 		idx -= sband->n_channels;
3291 		sband = NULL;
3292 	}
3293 
3294 	if (!sband)
3295 		sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
3296 
3297 	if (!sband || idx >= sband->n_channels) {
3298 		ret = -ENOENT;
3299 		goto exit;
3300 	}
3301 
3302 	spin_lock_bh(&ar->data_lock);
3303 	memcpy(survey, ar_survey, sizeof(*survey));
3304 	spin_unlock_bh(&ar->data_lock);
3305 
3306 	survey->channel = &sband->channels[idx];
3307 
3308 exit:
3309 	mutex_unlock(&ar->conf_mutex);
3310 	return ret;
3311 }
3312 
3313 static const struct ieee80211_ops ath10k_ops = {
3314 	.tx				= ath10k_tx,
3315 	.start				= ath10k_start,
3316 	.stop				= ath10k_stop,
3317 	.config				= ath10k_config,
3318 	.add_interface			= ath10k_add_interface,
3319 	.remove_interface		= ath10k_remove_interface,
3320 	.configure_filter		= ath10k_configure_filter,
3321 	.bss_info_changed		= ath10k_bss_info_changed,
3322 	.hw_scan			= ath10k_hw_scan,
3323 	.cancel_hw_scan			= ath10k_cancel_hw_scan,
3324 	.set_key			= ath10k_set_key,
3325 	.sta_state			= ath10k_sta_state,
3326 	.conf_tx			= ath10k_conf_tx,
3327 	.remain_on_channel		= ath10k_remain_on_channel,
3328 	.cancel_remain_on_channel	= ath10k_cancel_remain_on_channel,
3329 	.set_rts_threshold		= ath10k_set_rts_threshold,
3330 	.set_frag_threshold		= ath10k_set_frag_threshold,
3331 	.flush				= ath10k_flush,
3332 	.tx_last_beacon			= ath10k_tx_last_beacon,
3333 	.restart_complete		= ath10k_restart_complete,
3334 	.get_survey			= ath10k_get_survey,
3335 #ifdef CONFIG_PM
3336 	.suspend			= ath10k_suspend,
3337 	.resume				= ath10k_resume,
3338 #endif
3339 };
3340 
3341 #define RATETAB_ENT(_rate, _rateid, _flags) { \
3342 	.bitrate		= (_rate), \
3343 	.flags			= (_flags), \
3344 	.hw_value		= (_rateid), \
3345 }
3346 
3347 #define CHAN2G(_channel, _freq, _flags) { \
3348 	.band			= IEEE80211_BAND_2GHZ, \
3349 	.hw_value		= (_channel), \
3350 	.center_freq		= (_freq), \
3351 	.flags			= (_flags), \
3352 	.max_antenna_gain	= 0, \
3353 	.max_power		= 30, \
3354 }
3355 
3356 #define CHAN5G(_channel, _freq, _flags) { \
3357 	.band			= IEEE80211_BAND_5GHZ, \
3358 	.hw_value		= (_channel), \
3359 	.center_freq		= (_freq), \
3360 	.flags			= (_flags), \
3361 	.max_antenna_gain	= 0, \
3362 	.max_power		= 30, \
3363 }
3364 
3365 static const struct ieee80211_channel ath10k_2ghz_channels[] = {
3366 	CHAN2G(1, 2412, 0),
3367 	CHAN2G(2, 2417, 0),
3368 	CHAN2G(3, 2422, 0),
3369 	CHAN2G(4, 2427, 0),
3370 	CHAN2G(5, 2432, 0),
3371 	CHAN2G(6, 2437, 0),
3372 	CHAN2G(7, 2442, 0),
3373 	CHAN2G(8, 2447, 0),
3374 	CHAN2G(9, 2452, 0),
3375 	CHAN2G(10, 2457, 0),
3376 	CHAN2G(11, 2462, 0),
3377 	CHAN2G(12, 2467, 0),
3378 	CHAN2G(13, 2472, 0),
3379 	CHAN2G(14, 2484, 0),
3380 };
3381 
3382 static const struct ieee80211_channel ath10k_5ghz_channels[] = {
3383 	CHAN5G(36, 5180, 0),
3384 	CHAN5G(40, 5200, 0),
3385 	CHAN5G(44, 5220, 0),
3386 	CHAN5G(48, 5240, 0),
3387 	CHAN5G(52, 5260, 0),
3388 	CHAN5G(56, 5280, 0),
3389 	CHAN5G(60, 5300, 0),
3390 	CHAN5G(64, 5320, 0),
3391 	CHAN5G(100, 5500, 0),
3392 	CHAN5G(104, 5520, 0),
3393 	CHAN5G(108, 5540, 0),
3394 	CHAN5G(112, 5560, 0),
3395 	CHAN5G(116, 5580, 0),
3396 	CHAN5G(120, 5600, 0),
3397 	CHAN5G(124, 5620, 0),
3398 	CHAN5G(128, 5640, 0),
3399 	CHAN5G(132, 5660, 0),
3400 	CHAN5G(136, 5680, 0),
3401 	CHAN5G(140, 5700, 0),
3402 	CHAN5G(149, 5745, 0),
3403 	CHAN5G(153, 5765, 0),
3404 	CHAN5G(157, 5785, 0),
3405 	CHAN5G(161, 5805, 0),
3406 	CHAN5G(165, 5825, 0),
3407 };
3408 
3409 static struct ieee80211_rate ath10k_rates[] = {
3410 	/* CCK */
3411 	RATETAB_ENT(10,  0x82, 0),
3412 	RATETAB_ENT(20,  0x84, 0),
3413 	RATETAB_ENT(55,  0x8b, 0),
3414 	RATETAB_ENT(110, 0x96, 0),
3415 	/* OFDM */
3416 	RATETAB_ENT(60,  0x0c, 0),
3417 	RATETAB_ENT(90,  0x12, 0),
3418 	RATETAB_ENT(120, 0x18, 0),
3419 	RATETAB_ENT(180, 0x24, 0),
3420 	RATETAB_ENT(240, 0x30, 0),
3421 	RATETAB_ENT(360, 0x48, 0),
3422 	RATETAB_ENT(480, 0x60, 0),
3423 	RATETAB_ENT(540, 0x6c, 0),
3424 };
3425 
3426 #define ath10k_a_rates (ath10k_rates + 4)
3427 #define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - 4)
3428 #define ath10k_g_rates (ath10k_rates + 0)
3429 #define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
3430 
3431 struct ath10k *ath10k_mac_create(void)
3432 {
3433 	struct ieee80211_hw *hw;
3434 	struct ath10k *ar;
3435 
3436 	hw = ieee80211_alloc_hw(sizeof(struct ath10k), &ath10k_ops);
3437 	if (!hw)
3438 		return NULL;
3439 
3440 	ar = hw->priv;
3441 	ar->hw = hw;
3442 
3443 	return ar;
3444 }
3445 
3446 void ath10k_mac_destroy(struct ath10k *ar)
3447 {
3448 	ieee80211_free_hw(ar->hw);
3449 }
3450 
3451 static const struct ieee80211_iface_limit ath10k_if_limits[] = {
3452 	{
3453 	.max	= 8,
3454 	.types	= BIT(NL80211_IFTYPE_STATION)
3455 		| BIT(NL80211_IFTYPE_P2P_CLIENT)
3456 	},
3457 	{
3458 	.max	= 3,
3459 	.types	= BIT(NL80211_IFTYPE_P2P_GO)
3460 	},
3461 	{
3462 	.max	= 7,
3463 	.types	= BIT(NL80211_IFTYPE_AP)
3464 	},
3465 };
3466 
3467 #ifdef CONFIG_ATH10K_DFS_CERTIFIED
3468 static const struct ieee80211_iface_limit ath10k_if_dfs_limits[] = {
3469 	{
3470 	.max	= 8,
3471 	.types	= BIT(NL80211_IFTYPE_AP)
3472 	},
3473 };
3474 #endif
3475 
3476 static const struct ieee80211_iface_combination ath10k_if_comb[] = {
3477 	{
3478 		.limits = ath10k_if_limits,
3479 		.n_limits = ARRAY_SIZE(ath10k_if_limits),
3480 		.max_interfaces = 8,
3481 		.num_different_channels = 1,
3482 		.beacon_int_infra_match = true,
3483 	},
3484 #ifdef CONFIG_ATH10K_DFS_CERTIFIED
3485 	{
3486 		.limits = ath10k_if_dfs_limits,
3487 		.n_limits = ARRAY_SIZE(ath10k_if_dfs_limits),
3488 		.max_interfaces = 8,
3489 		.num_different_channels = 1,
3490 		.beacon_int_infra_match = true,
3491 		.radar_detect_widths =	BIT(NL80211_CHAN_WIDTH_20_NOHT) |
3492 					BIT(NL80211_CHAN_WIDTH_20) |
3493 					BIT(NL80211_CHAN_WIDTH_40) |
3494 					BIT(NL80211_CHAN_WIDTH_80),
3495 	}
3496 #endif
3497 };
3498 
3499 static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
3500 {
3501 	struct ieee80211_sta_vht_cap vht_cap = {0};
3502 	u16 mcs_map;
3503 	int i;
3504 
3505 	vht_cap.vht_supported = 1;
3506 	vht_cap.cap = ar->vht_cap_info;
3507 
3508 	mcs_map = 0;
3509 	for (i = 0; i < 8; i++) {
3510 		if (i < ar->num_rf_chains)
3511 			mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i*2);
3512 		else
3513 			mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i*2);
3514 	}
3515 
3516 	vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
3517 	vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
3518 
3519 	return vht_cap;
3520 }
3521 
3522 static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
3523 {
3524 	int i;
3525 	struct ieee80211_sta_ht_cap ht_cap = {0};
3526 
3527 	if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
3528 		return ht_cap;
3529 
3530 	ht_cap.ht_supported = 1;
3531 	ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
3532 	ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
3533 	ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
3534 	ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
3535 	ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
3536 
3537 	if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
3538 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
3539 
3540 	if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
3541 		ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
3542 
3543 	if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
3544 		u32 smps;
3545 
3546 		smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
3547 		smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
3548 
3549 		ht_cap.cap |= smps;
3550 	}
3551 
3552 	if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC)
3553 		ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
3554 
3555 	if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
3556 		u32 stbc;
3557 
3558 		stbc   = ar->ht_cap_info;
3559 		stbc  &= WMI_HT_CAP_RX_STBC;
3560 		stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
3561 		stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
3562 		stbc  &= IEEE80211_HT_CAP_RX_STBC;
3563 
3564 		ht_cap.cap |= stbc;
3565 	}
3566 
3567 	if (ar->ht_cap_info & WMI_HT_CAP_LDPC)
3568 		ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
3569 
3570 	if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
3571 		ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
3572 
3573 	/* max AMSDU is implicitly taken from vht_cap_info */
3574 	if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
3575 		ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
3576 
3577 	for (i = 0; i < ar->num_rf_chains; i++)
3578 		ht_cap.mcs.rx_mask[i] = 0xFF;
3579 
3580 	ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
3581 
3582 	return ht_cap;
3583 }
3584 
3585 
3586 static void ath10k_get_arvif_iter(void *data, u8 *mac,
3587 				  struct ieee80211_vif *vif)
3588 {
3589 	struct ath10k_vif_iter *arvif_iter = data;
3590 	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3591 
3592 	if (arvif->vdev_id == arvif_iter->vdev_id)
3593 		arvif_iter->arvif = arvif;
3594 }
3595 
3596 struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
3597 {
3598 	struct ath10k_vif_iter arvif_iter;
3599 	u32 flags;
3600 
3601 	memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
3602 	arvif_iter.vdev_id = vdev_id;
3603 
3604 	flags = IEEE80211_IFACE_ITER_RESUME_ALL;
3605 	ieee80211_iterate_active_interfaces_atomic(ar->hw,
3606 						   flags,
3607 						   ath10k_get_arvif_iter,
3608 						   &arvif_iter);
3609 	if (!arvif_iter.arvif) {
3610 		ath10k_warn("No VIF found for VDEV: %d\n", vdev_id);
3611 		return NULL;
3612 	}
3613 
3614 	return arvif_iter.arvif;
3615 }
3616 
3617 int ath10k_mac_register(struct ath10k *ar)
3618 {
3619 	struct ieee80211_supported_band *band;
3620 	struct ieee80211_sta_vht_cap vht_cap;
3621 	struct ieee80211_sta_ht_cap ht_cap;
3622 	void *channels;
3623 	int ret;
3624 
3625 	SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
3626 
3627 	SET_IEEE80211_DEV(ar->hw, ar->dev);
3628 
3629 	ht_cap = ath10k_get_ht_cap(ar);
3630 	vht_cap = ath10k_create_vht_cap(ar);
3631 
3632 	if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
3633 		channels = kmemdup(ath10k_2ghz_channels,
3634 				   sizeof(ath10k_2ghz_channels),
3635 				   GFP_KERNEL);
3636 		if (!channels) {
3637 			ret = -ENOMEM;
3638 			goto err_free;
3639 		}
3640 
3641 		band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
3642 		band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
3643 		band->channels = channels;
3644 		band->n_bitrates = ath10k_g_rates_size;
3645 		band->bitrates = ath10k_g_rates;
3646 		band->ht_cap = ht_cap;
3647 
3648 		/* vht is not supported in 2.4 GHz */
3649 
3650 		ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
3651 	}
3652 
3653 	if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
3654 		channels = kmemdup(ath10k_5ghz_channels,
3655 				   sizeof(ath10k_5ghz_channels),
3656 				   GFP_KERNEL);
3657 		if (!channels) {
3658 			ret = -ENOMEM;
3659 			goto err_free;
3660 		}
3661 
3662 		band = &ar->mac.sbands[IEEE80211_BAND_5GHZ];
3663 		band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
3664 		band->channels = channels;
3665 		band->n_bitrates = ath10k_a_rates_size;
3666 		band->bitrates = ath10k_a_rates;
3667 		band->ht_cap = ht_cap;
3668 		band->vht_cap = vht_cap;
3669 		ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
3670 	}
3671 
3672 	ar->hw->wiphy->interface_modes =
3673 		BIT(NL80211_IFTYPE_STATION) |
3674 		BIT(NL80211_IFTYPE_ADHOC) |
3675 		BIT(NL80211_IFTYPE_AP) |
3676 		BIT(NL80211_IFTYPE_P2P_CLIENT) |
3677 		BIT(NL80211_IFTYPE_P2P_GO);
3678 
3679 	ar->hw->flags = IEEE80211_HW_SIGNAL_DBM |
3680 			IEEE80211_HW_SUPPORTS_PS |
3681 			IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
3682 			IEEE80211_HW_SUPPORTS_UAPSD |
3683 			IEEE80211_HW_MFP_CAPABLE |
3684 			IEEE80211_HW_REPORTS_TX_ACK_STATUS |
3685 			IEEE80211_HW_HAS_RATE_CONTROL |
3686 			IEEE80211_HW_SUPPORTS_STATIC_SMPS |
3687 			IEEE80211_HW_WANT_MONITOR_VIF |
3688 			IEEE80211_HW_AP_LINK_PS;
3689 
3690 	/* MSDU can have HTT TX fragment pushed in front. The additional 4
3691 	 * bytes is used for padding/alignment if necessary. */
3692 	ar->hw->extra_tx_headroom += sizeof(struct htt_data_tx_desc_frag)*2 + 4;
3693 
3694 	if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
3695 		ar->hw->flags |= IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS;
3696 
3697 	if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
3698 		ar->hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
3699 		ar->hw->flags |= IEEE80211_HW_TX_AMPDU_SETUP_IN_HW;
3700 	}
3701 
3702 	ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
3703 	ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
3704 
3705 	ar->hw->vif_data_size = sizeof(struct ath10k_vif);
3706 
3707 	ar->hw->channel_change_time = 5000;
3708 	ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
3709 
3710 	ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
3711 	ar->hw->wiphy->max_remain_on_channel_duration = 5000;
3712 
3713 	ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
3714 	/*
3715 	 * on LL hardware queues are managed entirely by the FW
3716 	 * so we only advertise to mac we can do the queues thing
3717 	 */
3718 	ar->hw->queues = 4;
3719 
3720 	ar->hw->wiphy->iface_combinations = ath10k_if_comb;
3721 	ar->hw->wiphy->n_iface_combinations = ARRAY_SIZE(ath10k_if_comb);
3722 
3723 	ar->hw->netdev_features = NETIF_F_HW_CSUM;
3724 
3725 	if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED)) {
3726 		/* Init ath dfs pattern detector */
3727 		ar->ath_common.debug_mask = ATH_DBG_DFS;
3728 		ar->dfs_detector = dfs_pattern_detector_init(&ar->ath_common,
3729 							     NL80211_DFS_UNSET);
3730 
3731 		if (!ar->dfs_detector)
3732 			ath10k_warn("dfs pattern detector init failed\n");
3733 	}
3734 
3735 	ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
3736 			    ath10k_reg_notifier);
3737 	if (ret) {
3738 		ath10k_err("Regulatory initialization failed\n");
3739 		goto err_free;
3740 	}
3741 
3742 	ret = ieee80211_register_hw(ar->hw);
3743 	if (ret) {
3744 		ath10k_err("ieee80211 registration failed: %d\n", ret);
3745 		goto err_free;
3746 	}
3747 
3748 	if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
3749 		ret = regulatory_hint(ar->hw->wiphy,
3750 				      ar->ath_common.regulatory.alpha2);
3751 		if (ret)
3752 			goto err_unregister;
3753 	}
3754 
3755 	return 0;
3756 
3757 err_unregister:
3758 	ieee80211_unregister_hw(ar->hw);
3759 err_free:
3760 	kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
3761 	kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
3762 
3763 	return ret;
3764 }
3765 
3766 void ath10k_mac_unregister(struct ath10k *ar)
3767 {
3768 	ieee80211_unregister_hw(ar->hw);
3769 
3770 	if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
3771 		ar->dfs_detector->exit(ar->dfs_detector);
3772 
3773 	kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
3774 	kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
3775 
3776 	SET_IEEE80211_DEV(ar->hw, NULL);
3777 }
3778