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