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