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