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