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