ath10k: use hw rate definitions for fixed rate
[deliverable/linux.git] / drivers / net / wireless / ath / ath10k / mac.c
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 #include "testmode.h"
30 #include "wmi.h"
31 #include "wmi-ops.h"
32 #include "wow.h"
33
34 /*********/
35 /* Rates */
36 /*********/
37
38 static struct ieee80211_rate ath10k_rates[] = {
39 /* CCK */
40 { .bitrate = 10 },
41 { .bitrate = 20 },
42 { .bitrate = 55 },
43 { .bitrate = 110 },
44
45 /* OFDM */
46 { .bitrate = 60 },
47 { .bitrate = 90 },
48 { .bitrate = 120 },
49 { .bitrate = 180 },
50 { .bitrate = 240 },
51 { .bitrate = 360 },
52 { .bitrate = 480 },
53 { .bitrate = 540 },
54 };
55
56 #define ath10k_a_rates (ath10k_rates + 4)
57 #define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - 4)
58 #define ath10k_g_rates (ath10k_rates + 0)
59 #define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
60
61 static bool ath10k_mac_bitrate_is_cck(int bitrate)
62 {
63 switch (bitrate) {
64 case 10:
65 case 20:
66 case 55:
67 case 110:
68 return true;
69 }
70
71 return false;
72 }
73
74 static u8 ath10k_mac_bitrate_to_rate(int bitrate)
75 {
76 return DIV_ROUND_UP(bitrate, 5) |
77 (ath10k_mac_bitrate_is_cck(bitrate) ? BIT(7) : 0);
78 }
79
80 /**********/
81 /* Crypto */
82 /**********/
83
84 static int ath10k_send_key(struct ath10k_vif *arvif,
85 struct ieee80211_key_conf *key,
86 enum set_key_cmd cmd,
87 const u8 *macaddr, u32 flags)
88 {
89 struct ath10k *ar = arvif->ar;
90 struct wmi_vdev_install_key_arg arg = {
91 .vdev_id = arvif->vdev_id,
92 .key_idx = key->keyidx,
93 .key_len = key->keylen,
94 .key_data = key->key,
95 .key_flags = flags,
96 .macaddr = macaddr,
97 };
98
99 lockdep_assert_held(&arvif->ar->conf_mutex);
100
101 switch (key->cipher) {
102 case WLAN_CIPHER_SUITE_CCMP:
103 arg.key_cipher = WMI_CIPHER_AES_CCM;
104 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
105 break;
106 case WLAN_CIPHER_SUITE_TKIP:
107 arg.key_cipher = WMI_CIPHER_TKIP;
108 arg.key_txmic_len = 8;
109 arg.key_rxmic_len = 8;
110 break;
111 case WLAN_CIPHER_SUITE_WEP40:
112 case WLAN_CIPHER_SUITE_WEP104:
113 arg.key_cipher = WMI_CIPHER_WEP;
114 break;
115 case WLAN_CIPHER_SUITE_AES_CMAC:
116 WARN_ON(1);
117 return -EINVAL;
118 default:
119 ath10k_warn(ar, "cipher %d is not supported\n", key->cipher);
120 return -EOPNOTSUPP;
121 }
122
123 if (cmd == DISABLE_KEY) {
124 arg.key_cipher = WMI_CIPHER_NONE;
125 arg.key_data = NULL;
126 }
127
128 return ath10k_wmi_vdev_install_key(arvif->ar, &arg);
129 }
130
131 static int ath10k_install_key(struct ath10k_vif *arvif,
132 struct ieee80211_key_conf *key,
133 enum set_key_cmd cmd,
134 const u8 *macaddr, u32 flags)
135 {
136 struct ath10k *ar = arvif->ar;
137 int ret;
138
139 lockdep_assert_held(&ar->conf_mutex);
140
141 reinit_completion(&ar->install_key_done);
142
143 ret = ath10k_send_key(arvif, key, cmd, macaddr, flags);
144 if (ret)
145 return ret;
146
147 ret = wait_for_completion_timeout(&ar->install_key_done, 3*HZ);
148 if (ret == 0)
149 return -ETIMEDOUT;
150
151 return 0;
152 }
153
154 static int ath10k_install_peer_wep_keys(struct ath10k_vif *arvif,
155 const u8 *addr)
156 {
157 struct ath10k *ar = arvif->ar;
158 struct ath10k_peer *peer;
159 int ret;
160 int i;
161 u32 flags;
162
163 lockdep_assert_held(&ar->conf_mutex);
164
165 spin_lock_bh(&ar->data_lock);
166 peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
167 spin_unlock_bh(&ar->data_lock);
168
169 if (!peer)
170 return -ENOENT;
171
172 for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
173 if (arvif->wep_keys[i] == NULL)
174 continue;
175
176 flags = 0;
177 flags |= WMI_KEY_PAIRWISE;
178
179 /* set TX_USAGE flag for default key id */
180 if (arvif->def_wep_key_idx == i)
181 flags |= WMI_KEY_TX_USAGE;
182
183 ret = ath10k_install_key(arvif, arvif->wep_keys[i], SET_KEY,
184 addr, flags);
185 if (ret)
186 return ret;
187
188 spin_lock_bh(&ar->data_lock);
189 peer->keys[i] = arvif->wep_keys[i];
190 spin_unlock_bh(&ar->data_lock);
191 }
192
193 return 0;
194 }
195
196 static int ath10k_clear_peer_keys(struct ath10k_vif *arvif,
197 const u8 *addr)
198 {
199 struct ath10k *ar = arvif->ar;
200 struct ath10k_peer *peer;
201 int first_errno = 0;
202 int ret;
203 int i;
204 u32 flags = 0;
205
206 lockdep_assert_held(&ar->conf_mutex);
207
208 spin_lock_bh(&ar->data_lock);
209 peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
210 spin_unlock_bh(&ar->data_lock);
211
212 if (!peer)
213 return -ENOENT;
214
215 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
216 if (peer->keys[i] == NULL)
217 continue;
218
219 /* key flags are not required to delete the key */
220 ret = ath10k_install_key(arvif, peer->keys[i],
221 DISABLE_KEY, addr, flags);
222 if (ret && first_errno == 0)
223 first_errno = ret;
224
225 if (ret)
226 ath10k_warn(ar, "failed to remove peer wep key %d: %d\n",
227 i, ret);
228
229 spin_lock_bh(&ar->data_lock);
230 peer->keys[i] = NULL;
231 spin_unlock_bh(&ar->data_lock);
232 }
233
234 return first_errno;
235 }
236
237 bool ath10k_mac_is_peer_wep_key_set(struct ath10k *ar, const u8 *addr,
238 u8 keyidx)
239 {
240 struct ath10k_peer *peer;
241 int i;
242
243 lockdep_assert_held(&ar->data_lock);
244
245 /* We don't know which vdev this peer belongs to,
246 * since WMI doesn't give us that information.
247 *
248 * FIXME: multi-bss needs to be handled.
249 */
250 peer = ath10k_peer_find(ar, 0, addr);
251 if (!peer)
252 return false;
253
254 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
255 if (peer->keys[i] && peer->keys[i]->keyidx == keyidx)
256 return true;
257 }
258
259 return false;
260 }
261
262 static int ath10k_clear_vdev_key(struct ath10k_vif *arvif,
263 struct ieee80211_key_conf *key)
264 {
265 struct ath10k *ar = arvif->ar;
266 struct ath10k_peer *peer;
267 u8 addr[ETH_ALEN];
268 int first_errno = 0;
269 int ret;
270 int i;
271 u32 flags = 0;
272
273 lockdep_assert_held(&ar->conf_mutex);
274
275 for (;;) {
276 /* since ath10k_install_key we can't hold data_lock all the
277 * time, so we try to remove the keys incrementally */
278 spin_lock_bh(&ar->data_lock);
279 i = 0;
280 list_for_each_entry(peer, &ar->peers, list) {
281 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
282 if (peer->keys[i] == key) {
283 ether_addr_copy(addr, peer->addr);
284 peer->keys[i] = NULL;
285 break;
286 }
287 }
288
289 if (i < ARRAY_SIZE(peer->keys))
290 break;
291 }
292 spin_unlock_bh(&ar->data_lock);
293
294 if (i == ARRAY_SIZE(peer->keys))
295 break;
296 /* key flags are not required to delete the key */
297 ret = ath10k_install_key(arvif, key, DISABLE_KEY, addr, flags);
298 if (ret && first_errno == 0)
299 first_errno = ret;
300
301 if (ret)
302 ath10k_warn(ar, "failed to remove key for %pM: %d\n",
303 addr, ret);
304 }
305
306 return first_errno;
307 }
308
309 static int ath10k_mac_vif_sta_fix_wep_key(struct ath10k_vif *arvif)
310 {
311 struct ath10k *ar = arvif->ar;
312 enum nl80211_iftype iftype = arvif->vif->type;
313 struct ieee80211_key_conf *key;
314 u32 flags = 0;
315 int num = 0;
316 int i;
317 int ret;
318
319 lockdep_assert_held(&ar->conf_mutex);
320
321 if (iftype != NL80211_IFTYPE_STATION)
322 return 0;
323
324 for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
325 if (arvif->wep_keys[i]) {
326 key = arvif->wep_keys[i];
327 ++num;
328 }
329 }
330
331 if (num != 1)
332 return 0;
333
334 flags |= WMI_KEY_PAIRWISE;
335 flags |= WMI_KEY_TX_USAGE;
336
337 ret = ath10k_install_key(arvif, key, SET_KEY, arvif->bssid, flags);
338 if (ret) {
339 ath10k_warn(ar, "failed to install key %i on vdev %i: %d\n",
340 key->keyidx, arvif->vdev_id, ret);
341 return ret;
342 }
343
344 return 0;
345 }
346
347 static int ath10k_mac_vif_update_wep_key(struct ath10k_vif *arvif,
348 struct ieee80211_key_conf *key)
349 {
350 struct ath10k *ar = arvif->ar;
351 struct ath10k_peer *peer;
352 int ret;
353
354 lockdep_assert_held(&ar->conf_mutex);
355
356 list_for_each_entry(peer, &ar->peers, list) {
357 if (!memcmp(peer->addr, arvif->vif->addr, ETH_ALEN))
358 continue;
359
360 if (!memcmp(peer->addr, arvif->bssid, ETH_ALEN))
361 continue;
362
363 if (peer->keys[key->keyidx] == key)
364 continue;
365
366 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vif vdev %i update key %i needs update\n",
367 arvif->vdev_id, key->keyidx);
368
369 ret = ath10k_install_peer_wep_keys(arvif, peer->addr);
370 if (ret) {
371 ath10k_warn(ar, "failed to update wep keys on vdev %i for peer %pM: %d\n",
372 arvif->vdev_id, peer->addr, ret);
373 return ret;
374 }
375 }
376
377 return 0;
378 }
379
380 /*********************/
381 /* General utilities */
382 /*********************/
383
384 static inline enum wmi_phy_mode
385 chan_to_phymode(const struct cfg80211_chan_def *chandef)
386 {
387 enum wmi_phy_mode phymode = MODE_UNKNOWN;
388
389 switch (chandef->chan->band) {
390 case IEEE80211_BAND_2GHZ:
391 switch (chandef->width) {
392 case NL80211_CHAN_WIDTH_20_NOHT:
393 if (chandef->chan->flags & IEEE80211_CHAN_NO_OFDM)
394 phymode = MODE_11B;
395 else
396 phymode = MODE_11G;
397 break;
398 case NL80211_CHAN_WIDTH_20:
399 phymode = MODE_11NG_HT20;
400 break;
401 case NL80211_CHAN_WIDTH_40:
402 phymode = MODE_11NG_HT40;
403 break;
404 case NL80211_CHAN_WIDTH_5:
405 case NL80211_CHAN_WIDTH_10:
406 case NL80211_CHAN_WIDTH_80:
407 case NL80211_CHAN_WIDTH_80P80:
408 case NL80211_CHAN_WIDTH_160:
409 phymode = MODE_UNKNOWN;
410 break;
411 }
412 break;
413 case IEEE80211_BAND_5GHZ:
414 switch (chandef->width) {
415 case NL80211_CHAN_WIDTH_20_NOHT:
416 phymode = MODE_11A;
417 break;
418 case NL80211_CHAN_WIDTH_20:
419 phymode = MODE_11NA_HT20;
420 break;
421 case NL80211_CHAN_WIDTH_40:
422 phymode = MODE_11NA_HT40;
423 break;
424 case NL80211_CHAN_WIDTH_80:
425 phymode = MODE_11AC_VHT80;
426 break;
427 case NL80211_CHAN_WIDTH_5:
428 case NL80211_CHAN_WIDTH_10:
429 case NL80211_CHAN_WIDTH_80P80:
430 case NL80211_CHAN_WIDTH_160:
431 phymode = MODE_UNKNOWN;
432 break;
433 }
434 break;
435 default:
436 break;
437 }
438
439 WARN_ON(phymode == MODE_UNKNOWN);
440 return phymode;
441 }
442
443 static u8 ath10k_parse_mpdudensity(u8 mpdudensity)
444 {
445 /*
446 * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
447 * 0 for no restriction
448 * 1 for 1/4 us
449 * 2 for 1/2 us
450 * 3 for 1 us
451 * 4 for 2 us
452 * 5 for 4 us
453 * 6 for 8 us
454 * 7 for 16 us
455 */
456 switch (mpdudensity) {
457 case 0:
458 return 0;
459 case 1:
460 case 2:
461 case 3:
462 /* Our lower layer calculations limit our precision to
463 1 microsecond */
464 return 1;
465 case 4:
466 return 2;
467 case 5:
468 return 4;
469 case 6:
470 return 8;
471 case 7:
472 return 16;
473 default:
474 return 0;
475 }
476 }
477
478 static int ath10k_peer_create(struct ath10k *ar, u32 vdev_id, const u8 *addr,
479 enum wmi_peer_type peer_type)
480 {
481 int ret;
482
483 lockdep_assert_held(&ar->conf_mutex);
484
485 if (ar->num_peers >= ar->max_num_peers)
486 return -ENOBUFS;
487
488 ret = ath10k_wmi_peer_create(ar, vdev_id, addr, peer_type);
489 if (ret) {
490 ath10k_warn(ar, "failed to create wmi peer %pM on vdev %i: %i\n",
491 addr, vdev_id, ret);
492 return ret;
493 }
494
495 ret = ath10k_wait_for_peer_created(ar, vdev_id, addr);
496 if (ret) {
497 ath10k_warn(ar, "failed to wait for created wmi peer %pM on vdev %i: %i\n",
498 addr, vdev_id, ret);
499 return ret;
500 }
501
502 ar->num_peers++;
503
504 return 0;
505 }
506
507 static int ath10k_mac_set_kickout(struct ath10k_vif *arvif)
508 {
509 struct ath10k *ar = arvif->ar;
510 u32 param;
511 int ret;
512
513 param = ar->wmi.pdev_param->sta_kickout_th;
514 ret = ath10k_wmi_pdev_set_param(ar, param,
515 ATH10K_KICKOUT_THRESHOLD);
516 if (ret) {
517 ath10k_warn(ar, "failed to set kickout threshold on vdev %i: %d\n",
518 arvif->vdev_id, ret);
519 return ret;
520 }
521
522 param = ar->wmi.vdev_param->ap_keepalive_min_idle_inactive_time_secs;
523 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
524 ATH10K_KEEPALIVE_MIN_IDLE);
525 if (ret) {
526 ath10k_warn(ar, "failed to set keepalive minimum idle time on vdev %i: %d\n",
527 arvif->vdev_id, ret);
528 return ret;
529 }
530
531 param = ar->wmi.vdev_param->ap_keepalive_max_idle_inactive_time_secs;
532 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
533 ATH10K_KEEPALIVE_MAX_IDLE);
534 if (ret) {
535 ath10k_warn(ar, "failed to set keepalive maximum idle time on vdev %i: %d\n",
536 arvif->vdev_id, ret);
537 return ret;
538 }
539
540 param = ar->wmi.vdev_param->ap_keepalive_max_unresponsive_time_secs;
541 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
542 ATH10K_KEEPALIVE_MAX_UNRESPONSIVE);
543 if (ret) {
544 ath10k_warn(ar, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
545 arvif->vdev_id, ret);
546 return ret;
547 }
548
549 return 0;
550 }
551
552 static int ath10k_mac_set_rts(struct ath10k_vif *arvif, u32 value)
553 {
554 struct ath10k *ar = arvif->ar;
555 u32 vdev_param;
556
557 vdev_param = ar->wmi.vdev_param->rts_threshold;
558 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
559 }
560
561 static int ath10k_mac_set_frag(struct ath10k_vif *arvif, u32 value)
562 {
563 struct ath10k *ar = arvif->ar;
564 u32 vdev_param;
565
566 if (value != 0xFFFFFFFF)
567 value = clamp_t(u32, arvif->ar->hw->wiphy->frag_threshold,
568 ATH10K_FRAGMT_THRESHOLD_MIN,
569 ATH10K_FRAGMT_THRESHOLD_MAX);
570
571 vdev_param = ar->wmi.vdev_param->fragmentation_threshold;
572 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
573 }
574
575 static int ath10k_peer_delete(struct ath10k *ar, u32 vdev_id, const u8 *addr)
576 {
577 int ret;
578
579 lockdep_assert_held(&ar->conf_mutex);
580
581 ret = ath10k_wmi_peer_delete(ar, vdev_id, addr);
582 if (ret)
583 return ret;
584
585 ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
586 if (ret)
587 return ret;
588
589 ar->num_peers--;
590
591 return 0;
592 }
593
594 static void ath10k_peer_cleanup(struct ath10k *ar, u32 vdev_id)
595 {
596 struct ath10k_peer *peer, *tmp;
597
598 lockdep_assert_held(&ar->conf_mutex);
599
600 spin_lock_bh(&ar->data_lock);
601 list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
602 if (peer->vdev_id != vdev_id)
603 continue;
604
605 ath10k_warn(ar, "removing stale peer %pM from vdev_id %d\n",
606 peer->addr, vdev_id);
607
608 list_del(&peer->list);
609 kfree(peer);
610 ar->num_peers--;
611 }
612 spin_unlock_bh(&ar->data_lock);
613 }
614
615 static void ath10k_peer_cleanup_all(struct ath10k *ar)
616 {
617 struct ath10k_peer *peer, *tmp;
618
619 lockdep_assert_held(&ar->conf_mutex);
620
621 spin_lock_bh(&ar->data_lock);
622 list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
623 list_del(&peer->list);
624 kfree(peer);
625 }
626 spin_unlock_bh(&ar->data_lock);
627
628 ar->num_peers = 0;
629 ar->num_stations = 0;
630 }
631
632 static int ath10k_mac_tdls_peer_update(struct ath10k *ar, u32 vdev_id,
633 struct ieee80211_sta *sta,
634 enum wmi_tdls_peer_state state)
635 {
636 int ret;
637 struct wmi_tdls_peer_update_cmd_arg arg = {};
638 struct wmi_tdls_peer_capab_arg cap = {};
639 struct wmi_channel_arg chan_arg = {};
640
641 lockdep_assert_held(&ar->conf_mutex);
642
643 arg.vdev_id = vdev_id;
644 arg.peer_state = state;
645 ether_addr_copy(arg.addr, sta->addr);
646
647 cap.peer_max_sp = sta->max_sp;
648 cap.peer_uapsd_queues = sta->uapsd_queues;
649
650 if (state == WMI_TDLS_PEER_STATE_CONNECTED &&
651 !sta->tdls_initiator)
652 cap.is_peer_responder = 1;
653
654 ret = ath10k_wmi_tdls_peer_update(ar, &arg, &cap, &chan_arg);
655 if (ret) {
656 ath10k_warn(ar, "failed to update tdls peer %pM on vdev %i: %i\n",
657 arg.addr, vdev_id, ret);
658 return ret;
659 }
660
661 return 0;
662 }
663
664 /************************/
665 /* Interface management */
666 /************************/
667
668 void ath10k_mac_vif_beacon_free(struct ath10k_vif *arvif)
669 {
670 struct ath10k *ar = arvif->ar;
671
672 lockdep_assert_held(&ar->data_lock);
673
674 if (!arvif->beacon)
675 return;
676
677 if (!arvif->beacon_buf)
678 dma_unmap_single(ar->dev, ATH10K_SKB_CB(arvif->beacon)->paddr,
679 arvif->beacon->len, DMA_TO_DEVICE);
680
681 if (WARN_ON(arvif->beacon_state != ATH10K_BEACON_SCHEDULED &&
682 arvif->beacon_state != ATH10K_BEACON_SENT))
683 return;
684
685 dev_kfree_skb_any(arvif->beacon);
686
687 arvif->beacon = NULL;
688 arvif->beacon_state = ATH10K_BEACON_SCHEDULED;
689 }
690
691 static void ath10k_mac_vif_beacon_cleanup(struct ath10k_vif *arvif)
692 {
693 struct ath10k *ar = arvif->ar;
694
695 lockdep_assert_held(&ar->data_lock);
696
697 ath10k_mac_vif_beacon_free(arvif);
698
699 if (arvif->beacon_buf) {
700 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
701 arvif->beacon_buf, arvif->beacon_paddr);
702 arvif->beacon_buf = NULL;
703 }
704 }
705
706 static inline int ath10k_vdev_setup_sync(struct ath10k *ar)
707 {
708 int ret;
709
710 lockdep_assert_held(&ar->conf_mutex);
711
712 if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags))
713 return -ESHUTDOWN;
714
715 ret = wait_for_completion_timeout(&ar->vdev_setup_done,
716 ATH10K_VDEV_SETUP_TIMEOUT_HZ);
717 if (ret == 0)
718 return -ETIMEDOUT;
719
720 return 0;
721 }
722
723 static int ath10k_monitor_vdev_start(struct ath10k *ar, int vdev_id)
724 {
725 struct cfg80211_chan_def *chandef = &ar->chandef;
726 struct ieee80211_channel *channel = chandef->chan;
727 struct wmi_vdev_start_request_arg arg = {};
728 int ret = 0;
729
730 lockdep_assert_held(&ar->conf_mutex);
731
732 arg.vdev_id = vdev_id;
733 arg.channel.freq = channel->center_freq;
734 arg.channel.band_center_freq1 = chandef->center_freq1;
735
736 /* TODO setup this dynamically, what in case we
737 don't have any vifs? */
738 arg.channel.mode = chan_to_phymode(chandef);
739 arg.channel.chan_radar =
740 !!(channel->flags & IEEE80211_CHAN_RADAR);
741
742 arg.channel.min_power = 0;
743 arg.channel.max_power = channel->max_power * 2;
744 arg.channel.max_reg_power = channel->max_reg_power * 2;
745 arg.channel.max_antenna_gain = channel->max_antenna_gain * 2;
746
747 reinit_completion(&ar->vdev_setup_done);
748
749 ret = ath10k_wmi_vdev_start(ar, &arg);
750 if (ret) {
751 ath10k_warn(ar, "failed to request monitor vdev %i start: %d\n",
752 vdev_id, ret);
753 return ret;
754 }
755
756 ret = ath10k_vdev_setup_sync(ar);
757 if (ret) {
758 ath10k_warn(ar, "failed to synchronize setup for monitor vdev %i start: %d\n",
759 vdev_id, ret);
760 return ret;
761 }
762
763 ret = ath10k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
764 if (ret) {
765 ath10k_warn(ar, "failed to put up monitor vdev %i: %d\n",
766 vdev_id, ret);
767 goto vdev_stop;
768 }
769
770 ar->monitor_vdev_id = vdev_id;
771
772 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i started\n",
773 ar->monitor_vdev_id);
774 return 0;
775
776 vdev_stop:
777 ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
778 if (ret)
779 ath10k_warn(ar, "failed to stop monitor vdev %i after start failure: %d\n",
780 ar->monitor_vdev_id, ret);
781
782 return ret;
783 }
784
785 static int ath10k_monitor_vdev_stop(struct ath10k *ar)
786 {
787 int ret = 0;
788
789 lockdep_assert_held(&ar->conf_mutex);
790
791 ret = ath10k_wmi_vdev_down(ar, ar->monitor_vdev_id);
792 if (ret)
793 ath10k_warn(ar, "failed to put down monitor vdev %i: %d\n",
794 ar->monitor_vdev_id, ret);
795
796 reinit_completion(&ar->vdev_setup_done);
797
798 ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
799 if (ret)
800 ath10k_warn(ar, "failed to to request monitor vdev %i stop: %d\n",
801 ar->monitor_vdev_id, ret);
802
803 ret = ath10k_vdev_setup_sync(ar);
804 if (ret)
805 ath10k_warn(ar, "failed to synchronize monitor vdev %i stop: %d\n",
806 ar->monitor_vdev_id, ret);
807
808 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i stopped\n",
809 ar->monitor_vdev_id);
810 return ret;
811 }
812
813 static int ath10k_monitor_vdev_create(struct ath10k *ar)
814 {
815 int bit, ret = 0;
816
817 lockdep_assert_held(&ar->conf_mutex);
818
819 if (ar->free_vdev_map == 0) {
820 ath10k_warn(ar, "failed to find free vdev id for monitor vdev\n");
821 return -ENOMEM;
822 }
823
824 bit = __ffs64(ar->free_vdev_map);
825
826 ar->monitor_vdev_id = bit;
827
828 ret = ath10k_wmi_vdev_create(ar, ar->monitor_vdev_id,
829 WMI_VDEV_TYPE_MONITOR,
830 0, ar->mac_addr);
831 if (ret) {
832 ath10k_warn(ar, "failed to request monitor vdev %i creation: %d\n",
833 ar->monitor_vdev_id, ret);
834 return ret;
835 }
836
837 ar->free_vdev_map &= ~(1LL << ar->monitor_vdev_id);
838 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d created\n",
839 ar->monitor_vdev_id);
840
841 return 0;
842 }
843
844 static int ath10k_monitor_vdev_delete(struct ath10k *ar)
845 {
846 int ret = 0;
847
848 lockdep_assert_held(&ar->conf_mutex);
849
850 ret = ath10k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
851 if (ret) {
852 ath10k_warn(ar, "failed to request wmi monitor vdev %i removal: %d\n",
853 ar->monitor_vdev_id, ret);
854 return ret;
855 }
856
857 ar->free_vdev_map |= 1LL << ar->monitor_vdev_id;
858
859 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d deleted\n",
860 ar->monitor_vdev_id);
861 return ret;
862 }
863
864 static int ath10k_monitor_start(struct ath10k *ar)
865 {
866 int ret;
867
868 lockdep_assert_held(&ar->conf_mutex);
869
870 ret = ath10k_monitor_vdev_create(ar);
871 if (ret) {
872 ath10k_warn(ar, "failed to create monitor vdev: %d\n", ret);
873 return ret;
874 }
875
876 ret = ath10k_monitor_vdev_start(ar, ar->monitor_vdev_id);
877 if (ret) {
878 ath10k_warn(ar, "failed to start monitor vdev: %d\n", ret);
879 ath10k_monitor_vdev_delete(ar);
880 return ret;
881 }
882
883 ar->monitor_started = true;
884 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor started\n");
885
886 return 0;
887 }
888
889 static int ath10k_monitor_stop(struct ath10k *ar)
890 {
891 int ret;
892
893 lockdep_assert_held(&ar->conf_mutex);
894
895 ret = ath10k_monitor_vdev_stop(ar);
896 if (ret) {
897 ath10k_warn(ar, "failed to stop monitor vdev: %d\n", ret);
898 return ret;
899 }
900
901 ret = ath10k_monitor_vdev_delete(ar);
902 if (ret) {
903 ath10k_warn(ar, "failed to delete monitor vdev: %d\n", ret);
904 return ret;
905 }
906
907 ar->monitor_started = false;
908 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopped\n");
909
910 return 0;
911 }
912
913 static bool ath10k_mac_should_disable_promisc(struct ath10k *ar)
914 {
915 struct ath10k_vif *arvif;
916
917 if (!(ar->filter_flags & FIF_PROMISC_IN_BSS))
918 return true;
919
920 if (!ar->num_started_vdevs)
921 return false;
922
923 list_for_each_entry(arvif, &ar->arvifs, list)
924 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
925 return false;
926
927 ath10k_dbg(ar, ATH10K_DBG_MAC,
928 "mac disabling promiscuous mode because vdev is started\n");
929 return true;
930 }
931
932 static int ath10k_monitor_recalc(struct ath10k *ar)
933 {
934 bool should_start;
935
936 lockdep_assert_held(&ar->conf_mutex);
937
938 should_start = ar->monitor ||
939 !ath10k_mac_should_disable_promisc(ar) ||
940 test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
941
942 ath10k_dbg(ar, ATH10K_DBG_MAC,
943 "mac monitor recalc started? %d should? %d\n",
944 ar->monitor_started, should_start);
945
946 if (should_start == ar->monitor_started)
947 return 0;
948
949 if (should_start)
950 return ath10k_monitor_start(ar);
951
952 return ath10k_monitor_stop(ar);
953 }
954
955 static int ath10k_recalc_rtscts_prot(struct ath10k_vif *arvif)
956 {
957 struct ath10k *ar = arvif->ar;
958 u32 vdev_param, rts_cts = 0;
959
960 lockdep_assert_held(&ar->conf_mutex);
961
962 vdev_param = ar->wmi.vdev_param->enable_rtscts;
963
964 rts_cts |= SM(WMI_RTSCTS_ENABLED, WMI_RTSCTS_SET);
965
966 if (arvif->num_legacy_stations > 0)
967 rts_cts |= SM(WMI_RTSCTS_ACROSS_SW_RETRIES,
968 WMI_RTSCTS_PROFILE);
969 else
970 rts_cts |= SM(WMI_RTSCTS_FOR_SECOND_RATESERIES,
971 WMI_RTSCTS_PROFILE);
972
973 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
974 rts_cts);
975 }
976
977 static int ath10k_start_cac(struct ath10k *ar)
978 {
979 int ret;
980
981 lockdep_assert_held(&ar->conf_mutex);
982
983 set_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
984
985 ret = ath10k_monitor_recalc(ar);
986 if (ret) {
987 ath10k_warn(ar, "failed to start monitor (cac): %d\n", ret);
988 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
989 return ret;
990 }
991
992 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac start monitor vdev %d\n",
993 ar->monitor_vdev_id);
994
995 return 0;
996 }
997
998 static int ath10k_stop_cac(struct ath10k *ar)
999 {
1000 lockdep_assert_held(&ar->conf_mutex);
1001
1002 /* CAC is not running - do nothing */
1003 if (!test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags))
1004 return 0;
1005
1006 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1007 ath10k_monitor_stop(ar);
1008
1009 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac finished\n");
1010
1011 return 0;
1012 }
1013
1014 static void ath10k_recalc_radar_detection(struct ath10k *ar)
1015 {
1016 int ret;
1017
1018 lockdep_assert_held(&ar->conf_mutex);
1019
1020 ath10k_stop_cac(ar);
1021
1022 if (!ar->radar_enabled)
1023 return;
1024
1025 if (ar->num_started_vdevs > 0)
1026 return;
1027
1028 ret = ath10k_start_cac(ar);
1029 if (ret) {
1030 /*
1031 * Not possible to start CAC on current channel so starting
1032 * radiation is not allowed, make this channel DFS_UNAVAILABLE
1033 * by indicating that radar was detected.
1034 */
1035 ath10k_warn(ar, "failed to start CAC: %d\n", ret);
1036 ieee80211_radar_detected(ar->hw);
1037 }
1038 }
1039
1040 static int ath10k_vdev_stop(struct ath10k_vif *arvif)
1041 {
1042 struct ath10k *ar = arvif->ar;
1043 int ret;
1044
1045 lockdep_assert_held(&ar->conf_mutex);
1046
1047 reinit_completion(&ar->vdev_setup_done);
1048
1049 ret = ath10k_wmi_vdev_stop(ar, arvif->vdev_id);
1050 if (ret) {
1051 ath10k_warn(ar, "failed to stop WMI vdev %i: %d\n",
1052 arvif->vdev_id, ret);
1053 return ret;
1054 }
1055
1056 ret = ath10k_vdev_setup_sync(ar);
1057 if (ret) {
1058 ath10k_warn(ar, "failed to syncronise setup for vdev %i: %d\n",
1059 arvif->vdev_id, ret);
1060 return ret;
1061 }
1062
1063 WARN_ON(ar->num_started_vdevs == 0);
1064
1065 if (ar->num_started_vdevs != 0) {
1066 ar->num_started_vdevs--;
1067 ath10k_recalc_radar_detection(ar);
1068 }
1069
1070 return ret;
1071 }
1072
1073 static int ath10k_vdev_start_restart(struct ath10k_vif *arvif, bool restart)
1074 {
1075 struct ath10k *ar = arvif->ar;
1076 struct cfg80211_chan_def *chandef = &ar->chandef;
1077 struct wmi_vdev_start_request_arg arg = {};
1078 int ret = 0, ret2;
1079
1080 lockdep_assert_held(&ar->conf_mutex);
1081
1082 reinit_completion(&ar->vdev_setup_done);
1083
1084 arg.vdev_id = arvif->vdev_id;
1085 arg.dtim_period = arvif->dtim_period;
1086 arg.bcn_intval = arvif->beacon_interval;
1087
1088 arg.channel.freq = chandef->chan->center_freq;
1089 arg.channel.band_center_freq1 = chandef->center_freq1;
1090 arg.channel.mode = chan_to_phymode(chandef);
1091
1092 arg.channel.min_power = 0;
1093 arg.channel.max_power = chandef->chan->max_power * 2;
1094 arg.channel.max_reg_power = chandef->chan->max_reg_power * 2;
1095 arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
1096
1097 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
1098 arg.ssid = arvif->u.ap.ssid;
1099 arg.ssid_len = arvif->u.ap.ssid_len;
1100 arg.hidden_ssid = arvif->u.ap.hidden_ssid;
1101
1102 /* For now allow DFS for AP mode */
1103 arg.channel.chan_radar =
1104 !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
1105 } else if (arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
1106 arg.ssid = arvif->vif->bss_conf.ssid;
1107 arg.ssid_len = arvif->vif->bss_conf.ssid_len;
1108 }
1109
1110 ath10k_dbg(ar, ATH10K_DBG_MAC,
1111 "mac vdev %d start center_freq %d phymode %s\n",
1112 arg.vdev_id, arg.channel.freq,
1113 ath10k_wmi_phymode_str(arg.channel.mode));
1114
1115 if (restart)
1116 ret = ath10k_wmi_vdev_restart(ar, &arg);
1117 else
1118 ret = ath10k_wmi_vdev_start(ar, &arg);
1119
1120 if (ret) {
1121 ath10k_warn(ar, "failed to start WMI vdev %i: %d\n",
1122 arg.vdev_id, ret);
1123 return ret;
1124 }
1125
1126 ret = ath10k_vdev_setup_sync(ar);
1127 if (ret) {
1128 ath10k_warn(ar,
1129 "failed to synchronize setup for vdev %i restart %d: %d\n",
1130 arg.vdev_id, restart, ret);
1131 return ret;
1132 }
1133
1134 ar->num_started_vdevs++;
1135 ath10k_recalc_radar_detection(ar);
1136
1137 ret = ath10k_monitor_recalc(ar);
1138 if (ret) {
1139 ath10k_warn(ar, "mac failed to recalc monitor for vdev %i restart %d: %d\n",
1140 arg.vdev_id, restart, ret);
1141 ret2 = ath10k_vdev_stop(arvif);
1142 if (ret2)
1143 ath10k_warn(ar, "mac failed to stop vdev %i restart %d: %d\n",
1144 arg.vdev_id, restart, ret2);
1145 }
1146
1147 return ret;
1148 }
1149
1150 static int ath10k_vdev_start(struct ath10k_vif *arvif)
1151 {
1152 return ath10k_vdev_start_restart(arvif, false);
1153 }
1154
1155 static int ath10k_vdev_restart(struct ath10k_vif *arvif)
1156 {
1157 return ath10k_vdev_start_restart(arvif, true);
1158 }
1159
1160 static int ath10k_mac_setup_bcn_p2p_ie(struct ath10k_vif *arvif,
1161 struct sk_buff *bcn)
1162 {
1163 struct ath10k *ar = arvif->ar;
1164 struct ieee80211_mgmt *mgmt;
1165 const u8 *p2p_ie;
1166 int ret;
1167
1168 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1169 return 0;
1170
1171 if (arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
1172 return 0;
1173
1174 mgmt = (void *)bcn->data;
1175 p2p_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1176 mgmt->u.beacon.variable,
1177 bcn->len - (mgmt->u.beacon.variable -
1178 bcn->data));
1179 if (!p2p_ie)
1180 return -ENOENT;
1181
1182 ret = ath10k_wmi_p2p_go_bcn_ie(ar, arvif->vdev_id, p2p_ie);
1183 if (ret) {
1184 ath10k_warn(ar, "failed to submit p2p go bcn ie for vdev %i: %d\n",
1185 arvif->vdev_id, ret);
1186 return ret;
1187 }
1188
1189 return 0;
1190 }
1191
1192 static int ath10k_mac_remove_vendor_ie(struct sk_buff *skb, unsigned int oui,
1193 u8 oui_type, size_t ie_offset)
1194 {
1195 size_t len;
1196 const u8 *next;
1197 const u8 *end;
1198 u8 *ie;
1199
1200 if (WARN_ON(skb->len < ie_offset))
1201 return -EINVAL;
1202
1203 ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
1204 skb->data + ie_offset,
1205 skb->len - ie_offset);
1206 if (!ie)
1207 return -ENOENT;
1208
1209 len = ie[1] + 2;
1210 end = skb->data + skb->len;
1211 next = ie + len;
1212
1213 if (WARN_ON(next > end))
1214 return -EINVAL;
1215
1216 memmove(ie, next, end - next);
1217 skb_trim(skb, skb->len - len);
1218
1219 return 0;
1220 }
1221
1222 static int ath10k_mac_setup_bcn_tmpl(struct ath10k_vif *arvif)
1223 {
1224 struct ath10k *ar = arvif->ar;
1225 struct ieee80211_hw *hw = ar->hw;
1226 struct ieee80211_vif *vif = arvif->vif;
1227 struct ieee80211_mutable_offsets offs = {};
1228 struct sk_buff *bcn;
1229 int ret;
1230
1231 if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1232 return 0;
1233
1234 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
1235 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
1236 return 0;
1237
1238 bcn = ieee80211_beacon_get_template(hw, vif, &offs);
1239 if (!bcn) {
1240 ath10k_warn(ar, "failed to get beacon template from mac80211\n");
1241 return -EPERM;
1242 }
1243
1244 ret = ath10k_mac_setup_bcn_p2p_ie(arvif, bcn);
1245 if (ret) {
1246 ath10k_warn(ar, "failed to setup p2p go bcn ie: %d\n", ret);
1247 kfree_skb(bcn);
1248 return ret;
1249 }
1250
1251 /* P2P IE is inserted by firmware automatically (as configured above)
1252 * so remove it from the base beacon template to avoid duplicate P2P
1253 * IEs in beacon frames.
1254 */
1255 ath10k_mac_remove_vendor_ie(bcn, WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1256 offsetof(struct ieee80211_mgmt,
1257 u.beacon.variable));
1258
1259 ret = ath10k_wmi_bcn_tmpl(ar, arvif->vdev_id, offs.tim_offset, bcn, 0,
1260 0, NULL, 0);
1261 kfree_skb(bcn);
1262
1263 if (ret) {
1264 ath10k_warn(ar, "failed to submit beacon template command: %d\n",
1265 ret);
1266 return ret;
1267 }
1268
1269 return 0;
1270 }
1271
1272 static int ath10k_mac_setup_prb_tmpl(struct ath10k_vif *arvif)
1273 {
1274 struct ath10k *ar = arvif->ar;
1275 struct ieee80211_hw *hw = ar->hw;
1276 struct ieee80211_vif *vif = arvif->vif;
1277 struct sk_buff *prb;
1278 int ret;
1279
1280 if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1281 return 0;
1282
1283 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1284 return 0;
1285
1286 prb = ieee80211_proberesp_get(hw, vif);
1287 if (!prb) {
1288 ath10k_warn(ar, "failed to get probe resp template from mac80211\n");
1289 return -EPERM;
1290 }
1291
1292 ret = ath10k_wmi_prb_tmpl(ar, arvif->vdev_id, prb);
1293 kfree_skb(prb);
1294
1295 if (ret) {
1296 ath10k_warn(ar, "failed to submit probe resp template command: %d\n",
1297 ret);
1298 return ret;
1299 }
1300
1301 return 0;
1302 }
1303
1304 static void ath10k_control_beaconing(struct ath10k_vif *arvif,
1305 struct ieee80211_bss_conf *info)
1306 {
1307 struct ath10k *ar = arvif->ar;
1308 int ret = 0;
1309
1310 lockdep_assert_held(&arvif->ar->conf_mutex);
1311
1312 if (!info->enable_beacon) {
1313 ath10k_vdev_stop(arvif);
1314
1315 arvif->is_started = false;
1316 arvif->is_up = false;
1317
1318 spin_lock_bh(&arvif->ar->data_lock);
1319 ath10k_mac_vif_beacon_free(arvif);
1320 spin_unlock_bh(&arvif->ar->data_lock);
1321
1322 return;
1323 }
1324
1325 arvif->tx_seq_no = 0x1000;
1326
1327 ret = ath10k_vdev_start(arvif);
1328 if (ret)
1329 return;
1330
1331 arvif->aid = 0;
1332 ether_addr_copy(arvif->bssid, info->bssid);
1333
1334 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
1335 arvif->bssid);
1336 if (ret) {
1337 ath10k_warn(ar, "failed to bring up vdev %d: %i\n",
1338 arvif->vdev_id, ret);
1339 ath10k_vdev_stop(arvif);
1340 return;
1341 }
1342
1343 arvif->is_started = true;
1344 arvif->is_up = true;
1345
1346 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
1347 }
1348
1349 static void ath10k_control_ibss(struct ath10k_vif *arvif,
1350 struct ieee80211_bss_conf *info,
1351 const u8 self_peer[ETH_ALEN])
1352 {
1353 struct ath10k *ar = arvif->ar;
1354 u32 vdev_param;
1355 int ret = 0;
1356
1357 lockdep_assert_held(&arvif->ar->conf_mutex);
1358
1359 if (!info->ibss_joined) {
1360 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, self_peer);
1361 if (ret)
1362 ath10k_warn(ar, "failed to delete IBSS self peer %pM for vdev %d: %d\n",
1363 self_peer, arvif->vdev_id, ret);
1364
1365 if (is_zero_ether_addr(arvif->bssid))
1366 return;
1367
1368 memset(arvif->bssid, 0, ETH_ALEN);
1369
1370 return;
1371 }
1372
1373 ret = ath10k_peer_create(arvif->ar, arvif->vdev_id, self_peer,
1374 WMI_PEER_TYPE_DEFAULT);
1375 if (ret) {
1376 ath10k_warn(ar, "failed to create IBSS self peer %pM for vdev %d: %d\n",
1377 self_peer, arvif->vdev_id, ret);
1378 return;
1379 }
1380
1381 vdev_param = arvif->ar->wmi.vdev_param->atim_window;
1382 ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id, vdev_param,
1383 ATH10K_DEFAULT_ATIM);
1384 if (ret)
1385 ath10k_warn(ar, "failed to set IBSS ATIM for vdev %d: %d\n",
1386 arvif->vdev_id, ret);
1387 }
1388
1389 static int ath10k_mac_vif_recalc_ps_wake_threshold(struct ath10k_vif *arvif)
1390 {
1391 struct ath10k *ar = arvif->ar;
1392 u32 param;
1393 u32 value;
1394 int ret;
1395
1396 lockdep_assert_held(&arvif->ar->conf_mutex);
1397
1398 if (arvif->u.sta.uapsd)
1399 value = WMI_STA_PS_TX_WAKE_THRESHOLD_NEVER;
1400 else
1401 value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
1402
1403 param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
1404 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param, value);
1405 if (ret) {
1406 ath10k_warn(ar, "failed to submit ps wake threshold %u on vdev %i: %d\n",
1407 value, arvif->vdev_id, ret);
1408 return ret;
1409 }
1410
1411 return 0;
1412 }
1413
1414 static int ath10k_mac_vif_recalc_ps_poll_count(struct ath10k_vif *arvif)
1415 {
1416 struct ath10k *ar = arvif->ar;
1417 u32 param;
1418 u32 value;
1419 int ret;
1420
1421 lockdep_assert_held(&arvif->ar->conf_mutex);
1422
1423 if (arvif->u.sta.uapsd)
1424 value = WMI_STA_PS_PSPOLL_COUNT_UAPSD;
1425 else
1426 value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
1427
1428 param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
1429 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
1430 param, value);
1431 if (ret) {
1432 ath10k_warn(ar, "failed to submit ps poll count %u on vdev %i: %d\n",
1433 value, arvif->vdev_id, ret);
1434 return ret;
1435 }
1436
1437 return 0;
1438 }
1439
1440 static int ath10k_mac_ps_vif_count(struct ath10k *ar)
1441 {
1442 struct ath10k_vif *arvif;
1443 int num = 0;
1444
1445 lockdep_assert_held(&ar->conf_mutex);
1446
1447 list_for_each_entry(arvif, &ar->arvifs, list)
1448 if (arvif->ps)
1449 num++;
1450
1451 return num;
1452 }
1453
1454 static int ath10k_mac_vif_setup_ps(struct ath10k_vif *arvif)
1455 {
1456 struct ath10k *ar = arvif->ar;
1457 struct ieee80211_vif *vif = arvif->vif;
1458 struct ieee80211_conf *conf = &ar->hw->conf;
1459 enum wmi_sta_powersave_param param;
1460 enum wmi_sta_ps_mode psmode;
1461 int ret;
1462 int ps_timeout;
1463 bool enable_ps;
1464
1465 lockdep_assert_held(&arvif->ar->conf_mutex);
1466
1467 if (arvif->vif->type != NL80211_IFTYPE_STATION)
1468 return 0;
1469
1470 enable_ps = arvif->ps;
1471
1472 if (enable_ps && ath10k_mac_ps_vif_count(ar) > 1 &&
1473 !test_bit(ATH10K_FW_FEATURE_MULTI_VIF_PS_SUPPORT,
1474 ar->fw_features)) {
1475 ath10k_warn(ar, "refusing to enable ps on vdev %i: not supported by fw\n",
1476 arvif->vdev_id);
1477 enable_ps = false;
1478 }
1479
1480 if (enable_ps) {
1481 psmode = WMI_STA_PS_MODE_ENABLED;
1482 param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
1483
1484 ps_timeout = conf->dynamic_ps_timeout;
1485 if (ps_timeout == 0) {
1486 /* Firmware doesn't like 0 */
1487 ps_timeout = ieee80211_tu_to_usec(
1488 vif->bss_conf.beacon_int) / 1000;
1489 }
1490
1491 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
1492 ps_timeout);
1493 if (ret) {
1494 ath10k_warn(ar, "failed to set inactivity time for vdev %d: %i\n",
1495 arvif->vdev_id, ret);
1496 return ret;
1497 }
1498 } else {
1499 psmode = WMI_STA_PS_MODE_DISABLED;
1500 }
1501
1502 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d psmode %s\n",
1503 arvif->vdev_id, psmode ? "enable" : "disable");
1504
1505 ret = ath10k_wmi_set_psmode(ar, arvif->vdev_id, psmode);
1506 if (ret) {
1507 ath10k_warn(ar, "failed to set PS Mode %d for vdev %d: %d\n",
1508 psmode, arvif->vdev_id, ret);
1509 return ret;
1510 }
1511
1512 return 0;
1513 }
1514
1515 static int ath10k_mac_vif_disable_keepalive(struct ath10k_vif *arvif)
1516 {
1517 struct ath10k *ar = arvif->ar;
1518 struct wmi_sta_keepalive_arg arg = {};
1519 int ret;
1520
1521 lockdep_assert_held(&arvif->ar->conf_mutex);
1522
1523 if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
1524 return 0;
1525
1526 if (!test_bit(WMI_SERVICE_STA_KEEP_ALIVE, ar->wmi.svc_map))
1527 return 0;
1528
1529 /* Some firmware revisions have a bug and ignore the `enabled` field.
1530 * Instead use the interval to disable the keepalive.
1531 */
1532 arg.vdev_id = arvif->vdev_id;
1533 arg.enabled = 1;
1534 arg.method = WMI_STA_KEEPALIVE_METHOD_NULL_FRAME;
1535 arg.interval = WMI_STA_KEEPALIVE_INTERVAL_DISABLE;
1536
1537 ret = ath10k_wmi_sta_keepalive(ar, &arg);
1538 if (ret) {
1539 ath10k_warn(ar, "failed to submit keepalive on vdev %i: %d\n",
1540 arvif->vdev_id, ret);
1541 return ret;
1542 }
1543
1544 return 0;
1545 }
1546
1547 static void ath10k_mac_vif_ap_csa_count_down(struct ath10k_vif *arvif)
1548 {
1549 struct ath10k *ar = arvif->ar;
1550 struct ieee80211_vif *vif = arvif->vif;
1551 int ret;
1552
1553 lockdep_assert_held(&arvif->ar->conf_mutex);
1554
1555 if (WARN_ON(!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)))
1556 return;
1557
1558 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1559 return;
1560
1561 if (!vif->csa_active)
1562 return;
1563
1564 if (!arvif->is_up)
1565 return;
1566
1567 if (!ieee80211_csa_is_complete(vif)) {
1568 ieee80211_csa_update_counter(vif);
1569
1570 ret = ath10k_mac_setup_bcn_tmpl(arvif);
1571 if (ret)
1572 ath10k_warn(ar, "failed to update bcn tmpl during csa: %d\n",
1573 ret);
1574
1575 ret = ath10k_mac_setup_prb_tmpl(arvif);
1576 if (ret)
1577 ath10k_warn(ar, "failed to update prb tmpl during csa: %d\n",
1578 ret);
1579 } else {
1580 ieee80211_csa_finish(vif);
1581 }
1582 }
1583
1584 static void ath10k_mac_vif_ap_csa_work(struct work_struct *work)
1585 {
1586 struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
1587 ap_csa_work);
1588 struct ath10k *ar = arvif->ar;
1589
1590 mutex_lock(&ar->conf_mutex);
1591 ath10k_mac_vif_ap_csa_count_down(arvif);
1592 mutex_unlock(&ar->conf_mutex);
1593 }
1594
1595 static void ath10k_mac_handle_beacon_iter(void *data, u8 *mac,
1596 struct ieee80211_vif *vif)
1597 {
1598 struct sk_buff *skb = data;
1599 struct ieee80211_mgmt *mgmt = (void *)skb->data;
1600 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1601
1602 if (vif->type != NL80211_IFTYPE_STATION)
1603 return;
1604
1605 if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
1606 return;
1607
1608 cancel_delayed_work(&arvif->connection_loss_work);
1609 }
1610
1611 void ath10k_mac_handle_beacon(struct ath10k *ar, struct sk_buff *skb)
1612 {
1613 ieee80211_iterate_active_interfaces_atomic(ar->hw,
1614 IEEE80211_IFACE_ITER_NORMAL,
1615 ath10k_mac_handle_beacon_iter,
1616 skb);
1617 }
1618
1619 static void ath10k_mac_handle_beacon_miss_iter(void *data, u8 *mac,
1620 struct ieee80211_vif *vif)
1621 {
1622 u32 *vdev_id = data;
1623 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1624 struct ath10k *ar = arvif->ar;
1625 struct ieee80211_hw *hw = ar->hw;
1626
1627 if (arvif->vdev_id != *vdev_id)
1628 return;
1629
1630 if (!arvif->is_up)
1631 return;
1632
1633 ieee80211_beacon_loss(vif);
1634
1635 /* Firmware doesn't report beacon loss events repeatedly. If AP probe
1636 * (done by mac80211) succeeds but beacons do not resume then it
1637 * doesn't make sense to continue operation. Queue connection loss work
1638 * which can be cancelled when beacon is received.
1639 */
1640 ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
1641 ATH10K_CONNECTION_LOSS_HZ);
1642 }
1643
1644 void ath10k_mac_handle_beacon_miss(struct ath10k *ar, u32 vdev_id)
1645 {
1646 ieee80211_iterate_active_interfaces_atomic(ar->hw,
1647 IEEE80211_IFACE_ITER_NORMAL,
1648 ath10k_mac_handle_beacon_miss_iter,
1649 &vdev_id);
1650 }
1651
1652 static void ath10k_mac_vif_sta_connection_loss_work(struct work_struct *work)
1653 {
1654 struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
1655 connection_loss_work.work);
1656 struct ieee80211_vif *vif = arvif->vif;
1657
1658 if (!arvif->is_up)
1659 return;
1660
1661 ieee80211_connection_loss(vif);
1662 }
1663
1664 /**********************/
1665 /* Station management */
1666 /**********************/
1667
1668 static u32 ath10k_peer_assoc_h_listen_intval(struct ath10k *ar,
1669 struct ieee80211_vif *vif)
1670 {
1671 /* Some firmware revisions have unstable STA powersave when listen
1672 * interval is set too high (e.g. 5). The symptoms are firmware doesn't
1673 * generate NullFunc frames properly even if buffered frames have been
1674 * indicated in Beacon TIM. Firmware would seldom wake up to pull
1675 * buffered frames. Often pinging the device from AP would simply fail.
1676 *
1677 * As a workaround set it to 1.
1678 */
1679 if (vif->type == NL80211_IFTYPE_STATION)
1680 return 1;
1681
1682 return ar->hw->conf.listen_interval;
1683 }
1684
1685 static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
1686 struct ieee80211_vif *vif,
1687 struct ieee80211_sta *sta,
1688 struct wmi_peer_assoc_complete_arg *arg)
1689 {
1690 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1691
1692 lockdep_assert_held(&ar->conf_mutex);
1693
1694 ether_addr_copy(arg->addr, sta->addr);
1695 arg->vdev_id = arvif->vdev_id;
1696 arg->peer_aid = sta->aid;
1697 arg->peer_flags |= WMI_PEER_AUTH;
1698 arg->peer_listen_intval = ath10k_peer_assoc_h_listen_intval(ar, vif);
1699 arg->peer_num_spatial_streams = 1;
1700 arg->peer_caps = vif->bss_conf.assoc_capability;
1701 }
1702
1703 static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
1704 struct ieee80211_vif *vif,
1705 struct wmi_peer_assoc_complete_arg *arg)
1706 {
1707 struct ieee80211_bss_conf *info = &vif->bss_conf;
1708 struct cfg80211_bss *bss;
1709 const u8 *rsnie = NULL;
1710 const u8 *wpaie = NULL;
1711
1712 lockdep_assert_held(&ar->conf_mutex);
1713
1714 bss = cfg80211_get_bss(ar->hw->wiphy, ar->hw->conf.chandef.chan,
1715 info->bssid, NULL, 0, IEEE80211_BSS_TYPE_ANY,
1716 IEEE80211_PRIVACY_ANY);
1717 if (bss) {
1718 const struct cfg80211_bss_ies *ies;
1719
1720 rcu_read_lock();
1721 rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
1722
1723 ies = rcu_dereference(bss->ies);
1724
1725 wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
1726 WLAN_OUI_TYPE_MICROSOFT_WPA,
1727 ies->data,
1728 ies->len);
1729 rcu_read_unlock();
1730 cfg80211_put_bss(ar->hw->wiphy, bss);
1731 }
1732
1733 /* FIXME: base on RSN IE/WPA IE is a correct idea? */
1734 if (rsnie || wpaie) {
1735 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
1736 arg->peer_flags |= WMI_PEER_NEED_PTK_4_WAY;
1737 }
1738
1739 if (wpaie) {
1740 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: wpa ie found\n", __func__);
1741 arg->peer_flags |= WMI_PEER_NEED_GTK_2_WAY;
1742 }
1743 }
1744
1745 static void ath10k_peer_assoc_h_rates(struct ath10k *ar,
1746 struct ieee80211_sta *sta,
1747 struct wmi_peer_assoc_complete_arg *arg)
1748 {
1749 struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
1750 const struct ieee80211_supported_band *sband;
1751 const struct ieee80211_rate *rates;
1752 u32 ratemask;
1753 u8 rate;
1754 int i;
1755
1756 lockdep_assert_held(&ar->conf_mutex);
1757
1758 sband = ar->hw->wiphy->bands[ar->hw->conf.chandef.chan->band];
1759 ratemask = sta->supp_rates[ar->hw->conf.chandef.chan->band];
1760 rates = sband->bitrates;
1761
1762 rateset->num_rates = 0;
1763
1764 for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
1765 if (!(ratemask & 1))
1766 continue;
1767
1768 rate = ath10k_mac_bitrate_to_rate(rates->bitrate);
1769 rateset->rates[rateset->num_rates] = rate;
1770 rateset->num_rates++;
1771 }
1772 }
1773
1774 static void ath10k_peer_assoc_h_ht(struct ath10k *ar,
1775 struct ieee80211_sta *sta,
1776 struct wmi_peer_assoc_complete_arg *arg)
1777 {
1778 const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1779 int i, n;
1780 u32 stbc;
1781
1782 lockdep_assert_held(&ar->conf_mutex);
1783
1784 if (!ht_cap->ht_supported)
1785 return;
1786
1787 arg->peer_flags |= WMI_PEER_HT;
1788 arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1789 ht_cap->ampdu_factor)) - 1;
1790
1791 arg->peer_mpdu_density =
1792 ath10k_parse_mpdudensity(ht_cap->ampdu_density);
1793
1794 arg->peer_ht_caps = ht_cap->cap;
1795 arg->peer_rate_caps |= WMI_RC_HT_FLAG;
1796
1797 if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
1798 arg->peer_flags |= WMI_PEER_LDPC;
1799
1800 if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
1801 arg->peer_flags |= WMI_PEER_40MHZ;
1802 arg->peer_rate_caps |= WMI_RC_CW40_FLAG;
1803 }
1804
1805 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
1806 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
1807
1808 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
1809 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
1810
1811 if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
1812 arg->peer_rate_caps |= WMI_RC_TX_STBC_FLAG;
1813 arg->peer_flags |= WMI_PEER_STBC;
1814 }
1815
1816 if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
1817 stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
1818 stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
1819 stbc = stbc << WMI_RC_RX_STBC_FLAG_S;
1820 arg->peer_rate_caps |= stbc;
1821 arg->peer_flags |= WMI_PEER_STBC;
1822 }
1823
1824 if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
1825 arg->peer_rate_caps |= WMI_RC_TS_FLAG;
1826 else if (ht_cap->mcs.rx_mask[1])
1827 arg->peer_rate_caps |= WMI_RC_DS_FLAG;
1828
1829 for (i = 0, n = 0; i < IEEE80211_HT_MCS_MASK_LEN*8; i++)
1830 if (ht_cap->mcs.rx_mask[i/8] & (1 << i%8))
1831 arg->peer_ht_rates.rates[n++] = i;
1832
1833 /*
1834 * This is a workaround for HT-enabled STAs which break the spec
1835 * and have no HT capabilities RX mask (no HT RX MCS map).
1836 *
1837 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
1838 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
1839 *
1840 * Firmware asserts if such situation occurs.
1841 */
1842 if (n == 0) {
1843 arg->peer_ht_rates.num_rates = 8;
1844 for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
1845 arg->peer_ht_rates.rates[i] = i;
1846 } else {
1847 arg->peer_ht_rates.num_rates = n;
1848 arg->peer_num_spatial_streams = sta->rx_nss;
1849 }
1850
1851 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
1852 arg->addr,
1853 arg->peer_ht_rates.num_rates,
1854 arg->peer_num_spatial_streams);
1855 }
1856
1857 static int ath10k_peer_assoc_qos_ap(struct ath10k *ar,
1858 struct ath10k_vif *arvif,
1859 struct ieee80211_sta *sta)
1860 {
1861 u32 uapsd = 0;
1862 u32 max_sp = 0;
1863 int ret = 0;
1864
1865 lockdep_assert_held(&ar->conf_mutex);
1866
1867 if (sta->wme && sta->uapsd_queues) {
1868 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
1869 sta->uapsd_queues, sta->max_sp);
1870
1871 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1872 uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
1873 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
1874 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1875 uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
1876 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
1877 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1878 uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
1879 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
1880 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1881 uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
1882 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
1883
1884 if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
1885 max_sp = sta->max_sp;
1886
1887 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1888 sta->addr,
1889 WMI_AP_PS_PEER_PARAM_UAPSD,
1890 uapsd);
1891 if (ret) {
1892 ath10k_warn(ar, "failed to set ap ps peer param uapsd for vdev %i: %d\n",
1893 arvif->vdev_id, ret);
1894 return ret;
1895 }
1896
1897 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1898 sta->addr,
1899 WMI_AP_PS_PEER_PARAM_MAX_SP,
1900 max_sp);
1901 if (ret) {
1902 ath10k_warn(ar, "failed to set ap ps peer param max sp for vdev %i: %d\n",
1903 arvif->vdev_id, ret);
1904 return ret;
1905 }
1906
1907 /* TODO setup this based on STA listen interval and
1908 beacon interval. Currently we don't know
1909 sta->listen_interval - mac80211 patch required.
1910 Currently use 10 seconds */
1911 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id, sta->addr,
1912 WMI_AP_PS_PEER_PARAM_AGEOUT_TIME,
1913 10);
1914 if (ret) {
1915 ath10k_warn(ar, "failed to set ap ps peer param ageout time for vdev %i: %d\n",
1916 arvif->vdev_id, ret);
1917 return ret;
1918 }
1919 }
1920
1921 return 0;
1922 }
1923
1924 static void ath10k_peer_assoc_h_vht(struct ath10k *ar,
1925 struct ieee80211_sta *sta,
1926 struct wmi_peer_assoc_complete_arg *arg)
1927 {
1928 const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
1929 u8 ampdu_factor;
1930
1931 if (!vht_cap->vht_supported)
1932 return;
1933
1934 arg->peer_flags |= WMI_PEER_VHT;
1935
1936 if (ar->hw->conf.chandef.chan->band == IEEE80211_BAND_2GHZ)
1937 arg->peer_flags |= WMI_PEER_VHT_2G;
1938
1939 arg->peer_vht_caps = vht_cap->cap;
1940
1941 ampdu_factor = (vht_cap->cap &
1942 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
1943 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
1944
1945 /* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
1946 * zero in VHT IE. Using it would result in degraded throughput.
1947 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
1948 * it if VHT max_mpdu is smaller. */
1949 arg->peer_max_mpdu = max(arg->peer_max_mpdu,
1950 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1951 ampdu_factor)) - 1);
1952
1953 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1954 arg->peer_flags |= WMI_PEER_80MHZ;
1955
1956 arg->peer_vht_rates.rx_max_rate =
1957 __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
1958 arg->peer_vht_rates.rx_mcs_set =
1959 __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
1960 arg->peer_vht_rates.tx_max_rate =
1961 __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
1962 arg->peer_vht_rates.tx_mcs_set =
1963 __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
1964
1965 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
1966 sta->addr, arg->peer_max_mpdu, arg->peer_flags);
1967 }
1968
1969 static void ath10k_peer_assoc_h_qos(struct ath10k *ar,
1970 struct ieee80211_vif *vif,
1971 struct ieee80211_sta *sta,
1972 struct wmi_peer_assoc_complete_arg *arg)
1973 {
1974 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1975
1976 switch (arvif->vdev_type) {
1977 case WMI_VDEV_TYPE_AP:
1978 if (sta->wme)
1979 arg->peer_flags |= WMI_PEER_QOS;
1980
1981 if (sta->wme && sta->uapsd_queues) {
1982 arg->peer_flags |= WMI_PEER_APSD;
1983 arg->peer_rate_caps |= WMI_RC_UAPSD_FLAG;
1984 }
1985 break;
1986 case WMI_VDEV_TYPE_STA:
1987 if (vif->bss_conf.qos)
1988 arg->peer_flags |= WMI_PEER_QOS;
1989 break;
1990 case WMI_VDEV_TYPE_IBSS:
1991 if (sta->wme)
1992 arg->peer_flags |= WMI_PEER_QOS;
1993 break;
1994 default:
1995 break;
1996 }
1997
1998 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM qos %d\n",
1999 sta->addr, !!(arg->peer_flags & WMI_PEER_QOS));
2000 }
2001
2002 static bool ath10k_mac_sta_has_11g_rates(struct ieee80211_sta *sta)
2003 {
2004 /* First 4 rates in ath10k_rates are CCK (11b) rates. */
2005 return sta->supp_rates[IEEE80211_BAND_2GHZ] >> 4;
2006 }
2007
2008 static void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
2009 struct ieee80211_vif *vif,
2010 struct ieee80211_sta *sta,
2011 struct wmi_peer_assoc_complete_arg *arg)
2012 {
2013 enum wmi_phy_mode phymode = MODE_UNKNOWN;
2014
2015 switch (ar->hw->conf.chandef.chan->band) {
2016 case IEEE80211_BAND_2GHZ:
2017 if (sta->vht_cap.vht_supported) {
2018 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2019 phymode = MODE_11AC_VHT40;
2020 else
2021 phymode = MODE_11AC_VHT20;
2022 } else if (sta->ht_cap.ht_supported) {
2023 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2024 phymode = MODE_11NG_HT40;
2025 else
2026 phymode = MODE_11NG_HT20;
2027 } else if (ath10k_mac_sta_has_11g_rates(sta)) {
2028 phymode = MODE_11G;
2029 } else {
2030 phymode = MODE_11B;
2031 }
2032
2033 break;
2034 case IEEE80211_BAND_5GHZ:
2035 /*
2036 * Check VHT first.
2037 */
2038 if (sta->vht_cap.vht_supported) {
2039 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
2040 phymode = MODE_11AC_VHT80;
2041 else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2042 phymode = MODE_11AC_VHT40;
2043 else if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
2044 phymode = MODE_11AC_VHT20;
2045 } else if (sta->ht_cap.ht_supported) {
2046 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2047 phymode = MODE_11NA_HT40;
2048 else
2049 phymode = MODE_11NA_HT20;
2050 } else {
2051 phymode = MODE_11A;
2052 }
2053
2054 break;
2055 default:
2056 break;
2057 }
2058
2059 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM phymode %s\n",
2060 sta->addr, ath10k_wmi_phymode_str(phymode));
2061
2062 arg->peer_phymode = phymode;
2063 WARN_ON(phymode == MODE_UNKNOWN);
2064 }
2065
2066 static int ath10k_peer_assoc_prepare(struct ath10k *ar,
2067 struct ieee80211_vif *vif,
2068 struct ieee80211_sta *sta,
2069 struct wmi_peer_assoc_complete_arg *arg)
2070 {
2071 lockdep_assert_held(&ar->conf_mutex);
2072
2073 memset(arg, 0, sizeof(*arg));
2074
2075 ath10k_peer_assoc_h_basic(ar, vif, sta, arg);
2076 ath10k_peer_assoc_h_crypto(ar, vif, arg);
2077 ath10k_peer_assoc_h_rates(ar, sta, arg);
2078 ath10k_peer_assoc_h_ht(ar, sta, arg);
2079 ath10k_peer_assoc_h_vht(ar, sta, arg);
2080 ath10k_peer_assoc_h_qos(ar, vif, sta, arg);
2081 ath10k_peer_assoc_h_phymode(ar, vif, sta, arg);
2082
2083 return 0;
2084 }
2085
2086 static const u32 ath10k_smps_map[] = {
2087 [WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
2088 [WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
2089 [WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
2090 [WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
2091 };
2092
2093 static int ath10k_setup_peer_smps(struct ath10k *ar, struct ath10k_vif *arvif,
2094 const u8 *addr,
2095 const struct ieee80211_sta_ht_cap *ht_cap)
2096 {
2097 int smps;
2098
2099 if (!ht_cap->ht_supported)
2100 return 0;
2101
2102 smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
2103 smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
2104
2105 if (smps >= ARRAY_SIZE(ath10k_smps_map))
2106 return -EINVAL;
2107
2108 return ath10k_wmi_peer_set_param(ar, arvif->vdev_id, addr,
2109 WMI_PEER_SMPS_STATE,
2110 ath10k_smps_map[smps]);
2111 }
2112
2113 static int ath10k_mac_vif_recalc_txbf(struct ath10k *ar,
2114 struct ieee80211_vif *vif,
2115 struct ieee80211_sta_vht_cap vht_cap)
2116 {
2117 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2118 int ret;
2119 u32 param;
2120 u32 value;
2121
2122 if (!(ar->vht_cap_info &
2123 (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
2124 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
2125 IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
2126 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)))
2127 return 0;
2128
2129 param = ar->wmi.vdev_param->txbf;
2130 value = 0;
2131
2132 if (WARN_ON(param == WMI_VDEV_PARAM_UNSUPPORTED))
2133 return 0;
2134
2135 /* The following logic is correct. If a remote STA advertises support
2136 * for being a beamformer then we should enable us being a beamformee.
2137 */
2138
2139 if (ar->vht_cap_info &
2140 (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
2141 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)) {
2142 if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)
2143 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
2144
2145 if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)
2146 value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
2147 }
2148
2149 if (ar->vht_cap_info &
2150 (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
2151 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)) {
2152 if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)
2153 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
2154
2155 if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)
2156 value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
2157 }
2158
2159 if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFEE)
2160 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
2161
2162 if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFER)
2163 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
2164
2165 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param, value);
2166 if (ret) {
2167 ath10k_warn(ar, "failed to submit vdev param txbf 0x%x: %d\n",
2168 value, ret);
2169 return ret;
2170 }
2171
2172 return 0;
2173 }
2174
2175 /* can be called only in mac80211 callbacks due to `key_count` usage */
2176 static void ath10k_bss_assoc(struct ieee80211_hw *hw,
2177 struct ieee80211_vif *vif,
2178 struct ieee80211_bss_conf *bss_conf)
2179 {
2180 struct ath10k *ar = hw->priv;
2181 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2182 struct ieee80211_sta_ht_cap ht_cap;
2183 struct ieee80211_sta_vht_cap vht_cap;
2184 struct wmi_peer_assoc_complete_arg peer_arg;
2185 struct ieee80211_sta *ap_sta;
2186 int ret;
2187
2188 lockdep_assert_held(&ar->conf_mutex);
2189
2190 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
2191 arvif->vdev_id, arvif->bssid, arvif->aid);
2192
2193 rcu_read_lock();
2194
2195 ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
2196 if (!ap_sta) {
2197 ath10k_warn(ar, "failed to find station entry for bss %pM vdev %i\n",
2198 bss_conf->bssid, arvif->vdev_id);
2199 rcu_read_unlock();
2200 return;
2201 }
2202
2203 /* ap_sta must be accessed only within rcu section which must be left
2204 * before calling ath10k_setup_peer_smps() which might sleep. */
2205 ht_cap = ap_sta->ht_cap;
2206 vht_cap = ap_sta->vht_cap;
2207
2208 ret = ath10k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg);
2209 if (ret) {
2210 ath10k_warn(ar, "failed to prepare peer assoc for %pM vdev %i: %d\n",
2211 bss_conf->bssid, arvif->vdev_id, ret);
2212 rcu_read_unlock();
2213 return;
2214 }
2215
2216 rcu_read_unlock();
2217
2218 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
2219 if (ret) {
2220 ath10k_warn(ar, "failed to run peer assoc for %pM vdev %i: %d\n",
2221 bss_conf->bssid, arvif->vdev_id, ret);
2222 return;
2223 }
2224
2225 ret = ath10k_setup_peer_smps(ar, arvif, bss_conf->bssid, &ht_cap);
2226 if (ret) {
2227 ath10k_warn(ar, "failed to setup peer SMPS for vdev %i: %d\n",
2228 arvif->vdev_id, ret);
2229 return;
2230 }
2231
2232 ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
2233 if (ret) {
2234 ath10k_warn(ar, "failed to recalc txbf for vdev %i on bss %pM: %d\n",
2235 arvif->vdev_id, bss_conf->bssid, ret);
2236 return;
2237 }
2238
2239 ath10k_dbg(ar, ATH10K_DBG_MAC,
2240 "mac vdev %d up (associated) bssid %pM aid %d\n",
2241 arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
2242
2243 WARN_ON(arvif->is_up);
2244
2245 arvif->aid = bss_conf->aid;
2246 ether_addr_copy(arvif->bssid, bss_conf->bssid);
2247
2248 ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
2249 if (ret) {
2250 ath10k_warn(ar, "failed to set vdev %d up: %d\n",
2251 arvif->vdev_id, ret);
2252 return;
2253 }
2254
2255 arvif->is_up = true;
2256
2257 /* Workaround: Some firmware revisions (tested with qca6174
2258 * WLAN.RM.2.0-00073) have buggy powersave state machine and must be
2259 * poked with peer param command.
2260 */
2261 ret = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, arvif->bssid,
2262 WMI_PEER_DUMMY_VAR, 1);
2263 if (ret) {
2264 ath10k_warn(ar, "failed to poke peer %pM param for ps workaround on vdev %i: %d\n",
2265 arvif->bssid, arvif->vdev_id, ret);
2266 return;
2267 }
2268 }
2269
2270 static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
2271 struct ieee80211_vif *vif)
2272 {
2273 struct ath10k *ar = hw->priv;
2274 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2275 struct ieee80211_sta_vht_cap vht_cap = {};
2276 int ret;
2277
2278 lockdep_assert_held(&ar->conf_mutex);
2279
2280 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
2281 arvif->vdev_id, arvif->bssid);
2282
2283 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
2284 if (ret)
2285 ath10k_warn(ar, "faield to down vdev %i: %d\n",
2286 arvif->vdev_id, ret);
2287
2288 arvif->def_wep_key_idx = -1;
2289
2290 ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
2291 if (ret) {
2292 ath10k_warn(ar, "failed to recalc txbf for vdev %i: %d\n",
2293 arvif->vdev_id, ret);
2294 return;
2295 }
2296
2297 arvif->is_up = false;
2298
2299 cancel_delayed_work_sync(&arvif->connection_loss_work);
2300 }
2301
2302 static int ath10k_station_assoc(struct ath10k *ar,
2303 struct ieee80211_vif *vif,
2304 struct ieee80211_sta *sta,
2305 bool reassoc)
2306 {
2307 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2308 struct wmi_peer_assoc_complete_arg peer_arg;
2309 int ret = 0;
2310
2311 lockdep_assert_held(&ar->conf_mutex);
2312
2313 ret = ath10k_peer_assoc_prepare(ar, vif, sta, &peer_arg);
2314 if (ret) {
2315 ath10k_warn(ar, "failed to prepare WMI peer assoc for %pM vdev %i: %i\n",
2316 sta->addr, arvif->vdev_id, ret);
2317 return ret;
2318 }
2319
2320 peer_arg.peer_reassoc = reassoc;
2321 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
2322 if (ret) {
2323 ath10k_warn(ar, "failed to run peer assoc for STA %pM vdev %i: %d\n",
2324 sta->addr, arvif->vdev_id, ret);
2325 return ret;
2326 }
2327
2328 /* Re-assoc is run only to update supported rates for given station. It
2329 * doesn't make much sense to reconfigure the peer completely.
2330 */
2331 if (!reassoc) {
2332 ret = ath10k_setup_peer_smps(ar, arvif, sta->addr,
2333 &sta->ht_cap);
2334 if (ret) {
2335 ath10k_warn(ar, "failed to setup peer SMPS for vdev %d: %d\n",
2336 arvif->vdev_id, ret);
2337 return ret;
2338 }
2339
2340 ret = ath10k_peer_assoc_qos_ap(ar, arvif, sta);
2341 if (ret) {
2342 ath10k_warn(ar, "failed to set qos params for STA %pM for vdev %i: %d\n",
2343 sta->addr, arvif->vdev_id, ret);
2344 return ret;
2345 }
2346
2347 if (!sta->wme) {
2348 arvif->num_legacy_stations++;
2349 ret = ath10k_recalc_rtscts_prot(arvif);
2350 if (ret) {
2351 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
2352 arvif->vdev_id, ret);
2353 return ret;
2354 }
2355 }
2356
2357 /* Plumb cached keys only for static WEP */
2358 if (arvif->def_wep_key_idx != -1) {
2359 ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
2360 if (ret) {
2361 ath10k_warn(ar, "failed to install peer wep keys for vdev %i: %d\n",
2362 arvif->vdev_id, ret);
2363 return ret;
2364 }
2365 }
2366 }
2367
2368 return ret;
2369 }
2370
2371 static int ath10k_station_disassoc(struct ath10k *ar,
2372 struct ieee80211_vif *vif,
2373 struct ieee80211_sta *sta)
2374 {
2375 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2376 int ret = 0;
2377
2378 lockdep_assert_held(&ar->conf_mutex);
2379
2380 if (!sta->wme) {
2381 arvif->num_legacy_stations--;
2382 ret = ath10k_recalc_rtscts_prot(arvif);
2383 if (ret) {
2384 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
2385 arvif->vdev_id, ret);
2386 return ret;
2387 }
2388 }
2389
2390 ret = ath10k_clear_peer_keys(arvif, sta->addr);
2391 if (ret) {
2392 ath10k_warn(ar, "failed to clear all peer wep keys for vdev %i: %d\n",
2393 arvif->vdev_id, ret);
2394 return ret;
2395 }
2396
2397 return ret;
2398 }
2399
2400 /**************/
2401 /* Regulatory */
2402 /**************/
2403
2404 static int ath10k_update_channel_list(struct ath10k *ar)
2405 {
2406 struct ieee80211_hw *hw = ar->hw;
2407 struct ieee80211_supported_band **bands;
2408 enum ieee80211_band band;
2409 struct ieee80211_channel *channel;
2410 struct wmi_scan_chan_list_arg arg = {0};
2411 struct wmi_channel_arg *ch;
2412 bool passive;
2413 int len;
2414 int ret;
2415 int i;
2416
2417 lockdep_assert_held(&ar->conf_mutex);
2418
2419 bands = hw->wiphy->bands;
2420 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2421 if (!bands[band])
2422 continue;
2423
2424 for (i = 0; i < bands[band]->n_channels; i++) {
2425 if (bands[band]->channels[i].flags &
2426 IEEE80211_CHAN_DISABLED)
2427 continue;
2428
2429 arg.n_channels++;
2430 }
2431 }
2432
2433 len = sizeof(struct wmi_channel_arg) * arg.n_channels;
2434 arg.channels = kzalloc(len, GFP_KERNEL);
2435 if (!arg.channels)
2436 return -ENOMEM;
2437
2438 ch = arg.channels;
2439 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2440 if (!bands[band])
2441 continue;
2442
2443 for (i = 0; i < bands[band]->n_channels; i++) {
2444 channel = &bands[band]->channels[i];
2445
2446 if (channel->flags & IEEE80211_CHAN_DISABLED)
2447 continue;
2448
2449 ch->allow_ht = true;
2450
2451 /* FIXME: when should we really allow VHT? */
2452 ch->allow_vht = true;
2453
2454 ch->allow_ibss =
2455 !(channel->flags & IEEE80211_CHAN_NO_IR);
2456
2457 ch->ht40plus =
2458 !(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
2459
2460 ch->chan_radar =
2461 !!(channel->flags & IEEE80211_CHAN_RADAR);
2462
2463 passive = channel->flags & IEEE80211_CHAN_NO_IR;
2464 ch->passive = passive;
2465
2466 ch->freq = channel->center_freq;
2467 ch->band_center_freq1 = channel->center_freq;
2468 ch->min_power = 0;
2469 ch->max_power = channel->max_power * 2;
2470 ch->max_reg_power = channel->max_reg_power * 2;
2471 ch->max_antenna_gain = channel->max_antenna_gain * 2;
2472 ch->reg_class_id = 0; /* FIXME */
2473
2474 /* FIXME: why use only legacy modes, why not any
2475 * HT/VHT modes? Would that even make any
2476 * difference? */
2477 if (channel->band == IEEE80211_BAND_2GHZ)
2478 ch->mode = MODE_11G;
2479 else
2480 ch->mode = MODE_11A;
2481
2482 if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
2483 continue;
2484
2485 ath10k_dbg(ar, ATH10K_DBG_WMI,
2486 "mac channel [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
2487 ch - arg.channels, arg.n_channels,
2488 ch->freq, ch->max_power, ch->max_reg_power,
2489 ch->max_antenna_gain, ch->mode);
2490
2491 ch++;
2492 }
2493 }
2494
2495 ret = ath10k_wmi_scan_chan_list(ar, &arg);
2496 kfree(arg.channels);
2497
2498 return ret;
2499 }
2500
2501 static enum wmi_dfs_region
2502 ath10k_mac_get_dfs_region(enum nl80211_dfs_regions dfs_region)
2503 {
2504 switch (dfs_region) {
2505 case NL80211_DFS_UNSET:
2506 return WMI_UNINIT_DFS_DOMAIN;
2507 case NL80211_DFS_FCC:
2508 return WMI_FCC_DFS_DOMAIN;
2509 case NL80211_DFS_ETSI:
2510 return WMI_ETSI_DFS_DOMAIN;
2511 case NL80211_DFS_JP:
2512 return WMI_MKK4_DFS_DOMAIN;
2513 }
2514 return WMI_UNINIT_DFS_DOMAIN;
2515 }
2516
2517 static void ath10k_regd_update(struct ath10k *ar)
2518 {
2519 struct reg_dmn_pair_mapping *regpair;
2520 int ret;
2521 enum wmi_dfs_region wmi_dfs_reg;
2522 enum nl80211_dfs_regions nl_dfs_reg;
2523
2524 lockdep_assert_held(&ar->conf_mutex);
2525
2526 ret = ath10k_update_channel_list(ar);
2527 if (ret)
2528 ath10k_warn(ar, "failed to update channel list: %d\n", ret);
2529
2530 regpair = ar->ath_common.regulatory.regpair;
2531
2532 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
2533 nl_dfs_reg = ar->dfs_detector->region;
2534 wmi_dfs_reg = ath10k_mac_get_dfs_region(nl_dfs_reg);
2535 } else {
2536 wmi_dfs_reg = WMI_UNINIT_DFS_DOMAIN;
2537 }
2538
2539 /* Target allows setting up per-band regdomain but ath_common provides
2540 * a combined one only */
2541 ret = ath10k_wmi_pdev_set_regdomain(ar,
2542 regpair->reg_domain,
2543 regpair->reg_domain, /* 2ghz */
2544 regpair->reg_domain, /* 5ghz */
2545 regpair->reg_2ghz_ctl,
2546 regpair->reg_5ghz_ctl,
2547 wmi_dfs_reg);
2548 if (ret)
2549 ath10k_warn(ar, "failed to set pdev regdomain: %d\n", ret);
2550 }
2551
2552 static void ath10k_reg_notifier(struct wiphy *wiphy,
2553 struct regulatory_request *request)
2554 {
2555 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
2556 struct ath10k *ar = hw->priv;
2557 bool result;
2558
2559 ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
2560
2561 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
2562 ath10k_dbg(ar, ATH10K_DBG_REGULATORY, "dfs region 0x%x\n",
2563 request->dfs_region);
2564 result = ar->dfs_detector->set_dfs_domain(ar->dfs_detector,
2565 request->dfs_region);
2566 if (!result)
2567 ath10k_warn(ar, "DFS region 0x%X not supported, will trigger radar for every pulse\n",
2568 request->dfs_region);
2569 }
2570
2571 mutex_lock(&ar->conf_mutex);
2572 if (ar->state == ATH10K_STATE_ON)
2573 ath10k_regd_update(ar);
2574 mutex_unlock(&ar->conf_mutex);
2575 }
2576
2577 /***************/
2578 /* TX handlers */
2579 /***************/
2580
2581 static u8 ath10k_tx_h_get_tid(struct ieee80211_hdr *hdr)
2582 {
2583 if (ieee80211_is_mgmt(hdr->frame_control))
2584 return HTT_DATA_TX_EXT_TID_MGMT;
2585
2586 if (!ieee80211_is_data_qos(hdr->frame_control))
2587 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
2588
2589 if (!is_unicast_ether_addr(ieee80211_get_DA(hdr)))
2590 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
2591
2592 return ieee80211_get_qos_ctl(hdr)[0] & IEEE80211_QOS_CTL_TID_MASK;
2593 }
2594
2595 static u8 ath10k_tx_h_get_vdev_id(struct ath10k *ar, struct ieee80211_vif *vif)
2596 {
2597 if (vif)
2598 return ath10k_vif_to_arvif(vif)->vdev_id;
2599
2600 if (ar->monitor_started)
2601 return ar->monitor_vdev_id;
2602
2603 ath10k_warn(ar, "failed to resolve vdev id\n");
2604 return 0;
2605 }
2606
2607 static enum ath10k_hw_txrx_mode
2608 ath10k_tx_h_get_txmode(struct ath10k *ar, struct ieee80211_vif *vif,
2609 struct ieee80211_sta *sta, struct sk_buff *skb)
2610 {
2611 const struct ieee80211_hdr *hdr = (void *)skb->data;
2612 __le16 fc = hdr->frame_control;
2613
2614 if (!vif || vif->type == NL80211_IFTYPE_MONITOR)
2615 return ATH10K_HW_TXRX_RAW;
2616
2617 if (ieee80211_is_mgmt(fc))
2618 return ATH10K_HW_TXRX_MGMT;
2619
2620 /* Workaround:
2621 *
2622 * NullFunc frames are mostly used to ping if a client or AP are still
2623 * reachable and responsive. This implies tx status reports must be
2624 * accurate - otherwise either mac80211 or userspace (e.g. hostapd) can
2625 * come to a conclusion that the other end disappeared and tear down
2626 * BSS connection or it can never disconnect from BSS/client (which is
2627 * the case).
2628 *
2629 * Firmware with HTT older than 3.0 delivers incorrect tx status for
2630 * NullFunc frames to driver. However there's a HTT Mgmt Tx command
2631 * which seems to deliver correct tx reports for NullFunc frames. The
2632 * downside of using it is it ignores client powersave state so it can
2633 * end up disconnecting sleeping clients in AP mode. It should fix STA
2634 * mode though because AP don't sleep.
2635 */
2636 if (ar->htt.target_version_major < 3 &&
2637 (ieee80211_is_nullfunc(fc) || ieee80211_is_qos_nullfunc(fc)) &&
2638 !test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX, ar->fw_features))
2639 return ATH10K_HW_TXRX_MGMT;
2640
2641 /* Workaround:
2642 *
2643 * Some wmi-tlv firmwares for qca6174 have broken Tx key selection for
2644 * NativeWifi txmode - it selects AP key instead of peer key. It seems
2645 * to work with Ethernet txmode so use it.
2646 */
2647 if (ieee80211_is_data_present(fc) && sta && sta->tdls)
2648 return ATH10K_HW_TXRX_ETHERNET;
2649
2650 return ATH10K_HW_TXRX_NATIVE_WIFI;
2651 }
2652
2653 /* HTT Tx uses Native Wifi tx mode which expects 802.11 frames without QoS
2654 * Control in the header.
2655 */
2656 static void ath10k_tx_h_nwifi(struct ieee80211_hw *hw, struct sk_buff *skb)
2657 {
2658 struct ieee80211_hdr *hdr = (void *)skb->data;
2659 struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
2660 u8 *qos_ctl;
2661
2662 if (!ieee80211_is_data_qos(hdr->frame_control))
2663 return;
2664
2665 qos_ctl = ieee80211_get_qos_ctl(hdr);
2666 memmove(skb->data + IEEE80211_QOS_CTL_LEN,
2667 skb->data, (void *)qos_ctl - (void *)skb->data);
2668 skb_pull(skb, IEEE80211_QOS_CTL_LEN);
2669
2670 /* Some firmware revisions don't handle sending QoS NullFunc well.
2671 * These frames are mainly used for CQM purposes so it doesn't really
2672 * matter whether QoS NullFunc or NullFunc are sent.
2673 */
2674 hdr = (void *)skb->data;
2675 if (ieee80211_is_qos_nullfunc(hdr->frame_control))
2676 cb->htt.tid = HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
2677
2678 hdr->frame_control &= ~__cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
2679 }
2680
2681 static void ath10k_tx_h_8023(struct sk_buff *skb)
2682 {
2683 struct ieee80211_hdr *hdr;
2684 struct rfc1042_hdr *rfc1042;
2685 struct ethhdr *eth;
2686 size_t hdrlen;
2687 u8 da[ETH_ALEN];
2688 u8 sa[ETH_ALEN];
2689 __be16 type;
2690
2691 hdr = (void *)skb->data;
2692 hdrlen = ieee80211_hdrlen(hdr->frame_control);
2693 rfc1042 = (void *)skb->data + hdrlen;
2694
2695 ether_addr_copy(da, ieee80211_get_DA(hdr));
2696 ether_addr_copy(sa, ieee80211_get_SA(hdr));
2697 type = rfc1042->snap_type;
2698
2699 skb_pull(skb, hdrlen + sizeof(*rfc1042));
2700 skb_push(skb, sizeof(*eth));
2701
2702 eth = (void *)skb->data;
2703 ether_addr_copy(eth->h_dest, da);
2704 ether_addr_copy(eth->h_source, sa);
2705 eth->h_proto = type;
2706 }
2707
2708 static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar,
2709 struct ieee80211_vif *vif,
2710 struct sk_buff *skb)
2711 {
2712 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2713 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2714
2715 /* This is case only for P2P_GO */
2716 if (arvif->vdev_type != WMI_VDEV_TYPE_AP ||
2717 arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
2718 return;
2719
2720 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
2721 spin_lock_bh(&ar->data_lock);
2722 if (arvif->u.ap.noa_data)
2723 if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
2724 GFP_ATOMIC))
2725 memcpy(skb_put(skb, arvif->u.ap.noa_len),
2726 arvif->u.ap.noa_data,
2727 arvif->u.ap.noa_len);
2728 spin_unlock_bh(&ar->data_lock);
2729 }
2730 }
2731
2732 static bool ath10k_mac_need_offchan_tx_work(struct ath10k *ar)
2733 {
2734 /* FIXME: Not really sure since when the behaviour changed. At some
2735 * point new firmware stopped requiring creation of peer entries for
2736 * offchannel tx (and actually creating them causes issues with wmi-htc
2737 * tx credit replenishment and reliability). Assuming it's at least 3.4
2738 * because that's when the `freq` was introduced to TX_FRM HTT command.
2739 */
2740 return !(ar->htt.target_version_major >= 3 &&
2741 ar->htt.target_version_minor >= 4);
2742 }
2743
2744 static int ath10k_mac_tx_wmi_mgmt(struct ath10k *ar, struct sk_buff *skb)
2745 {
2746 struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
2747 int ret = 0;
2748
2749 spin_lock_bh(&ar->data_lock);
2750
2751 if (skb_queue_len(q) == ATH10K_MAX_NUM_MGMT_PENDING) {
2752 ath10k_warn(ar, "wmi mgmt tx queue is full\n");
2753 ret = -ENOSPC;
2754 goto unlock;
2755 }
2756
2757 __skb_queue_tail(q, skb);
2758 ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
2759
2760 unlock:
2761 spin_unlock_bh(&ar->data_lock);
2762
2763 return ret;
2764 }
2765
2766 static void ath10k_mac_tx(struct ath10k *ar, struct sk_buff *skb)
2767 {
2768 struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
2769 struct ath10k_htt *htt = &ar->htt;
2770 int ret = 0;
2771
2772 switch (cb->txmode) {
2773 case ATH10K_HW_TXRX_RAW:
2774 case ATH10K_HW_TXRX_NATIVE_WIFI:
2775 case ATH10K_HW_TXRX_ETHERNET:
2776 ret = ath10k_htt_tx(htt, skb);
2777 break;
2778 case ATH10K_HW_TXRX_MGMT:
2779 if (test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
2780 ar->fw_features))
2781 ret = ath10k_mac_tx_wmi_mgmt(ar, skb);
2782 else if (ar->htt.target_version_major >= 3)
2783 ret = ath10k_htt_tx(htt, skb);
2784 else
2785 ret = ath10k_htt_mgmt_tx(htt, skb);
2786 break;
2787 }
2788
2789 if (ret) {
2790 ath10k_warn(ar, "failed to transmit packet, dropping: %d\n",
2791 ret);
2792 ieee80211_free_txskb(ar->hw, skb);
2793 }
2794 }
2795
2796 void ath10k_offchan_tx_purge(struct ath10k *ar)
2797 {
2798 struct sk_buff *skb;
2799
2800 for (;;) {
2801 skb = skb_dequeue(&ar->offchan_tx_queue);
2802 if (!skb)
2803 break;
2804
2805 ieee80211_free_txskb(ar->hw, skb);
2806 }
2807 }
2808
2809 void ath10k_offchan_tx_work(struct work_struct *work)
2810 {
2811 struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
2812 struct ath10k_peer *peer;
2813 struct ieee80211_hdr *hdr;
2814 struct sk_buff *skb;
2815 const u8 *peer_addr;
2816 int vdev_id;
2817 int ret;
2818
2819 /* FW requirement: We must create a peer before FW will send out
2820 * an offchannel frame. Otherwise the frame will be stuck and
2821 * never transmitted. We delete the peer upon tx completion.
2822 * It is unlikely that a peer for offchannel tx will already be
2823 * present. However it may be in some rare cases so account for that.
2824 * Otherwise we might remove a legitimate peer and break stuff. */
2825
2826 for (;;) {
2827 skb = skb_dequeue(&ar->offchan_tx_queue);
2828 if (!skb)
2829 break;
2830
2831 mutex_lock(&ar->conf_mutex);
2832
2833 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac offchannel skb %p\n",
2834 skb);
2835
2836 hdr = (struct ieee80211_hdr *)skb->data;
2837 peer_addr = ieee80211_get_DA(hdr);
2838 vdev_id = ATH10K_SKB_CB(skb)->vdev_id;
2839
2840 spin_lock_bh(&ar->data_lock);
2841 peer = ath10k_peer_find(ar, vdev_id, peer_addr);
2842 spin_unlock_bh(&ar->data_lock);
2843
2844 if (peer)
2845 /* FIXME: should this use ath10k_warn()? */
2846 ath10k_dbg(ar, ATH10K_DBG_MAC, "peer %pM on vdev %d already present\n",
2847 peer_addr, vdev_id);
2848
2849 if (!peer) {
2850 ret = ath10k_peer_create(ar, vdev_id, peer_addr,
2851 WMI_PEER_TYPE_DEFAULT);
2852 if (ret)
2853 ath10k_warn(ar, "failed to create peer %pM on vdev %d: %d\n",
2854 peer_addr, vdev_id, ret);
2855 }
2856
2857 spin_lock_bh(&ar->data_lock);
2858 reinit_completion(&ar->offchan_tx_completed);
2859 ar->offchan_tx_skb = skb;
2860 spin_unlock_bh(&ar->data_lock);
2861
2862 ath10k_mac_tx(ar, skb);
2863
2864 ret = wait_for_completion_timeout(&ar->offchan_tx_completed,
2865 3 * HZ);
2866 if (ret == 0)
2867 ath10k_warn(ar, "timed out waiting for offchannel skb %p\n",
2868 skb);
2869
2870 if (!peer) {
2871 ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
2872 if (ret)
2873 ath10k_warn(ar, "failed to delete peer %pM on vdev %d: %d\n",
2874 peer_addr, vdev_id, ret);
2875 }
2876
2877 mutex_unlock(&ar->conf_mutex);
2878 }
2879 }
2880
2881 void ath10k_mgmt_over_wmi_tx_purge(struct ath10k *ar)
2882 {
2883 struct sk_buff *skb;
2884
2885 for (;;) {
2886 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
2887 if (!skb)
2888 break;
2889
2890 ieee80211_free_txskb(ar->hw, skb);
2891 }
2892 }
2893
2894 void ath10k_mgmt_over_wmi_tx_work(struct work_struct *work)
2895 {
2896 struct ath10k *ar = container_of(work, struct ath10k, wmi_mgmt_tx_work);
2897 struct sk_buff *skb;
2898 int ret;
2899
2900 for (;;) {
2901 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
2902 if (!skb)
2903 break;
2904
2905 ret = ath10k_wmi_mgmt_tx(ar, skb);
2906 if (ret) {
2907 ath10k_warn(ar, "failed to transmit management frame via WMI: %d\n",
2908 ret);
2909 ieee80211_free_txskb(ar->hw, skb);
2910 }
2911 }
2912 }
2913
2914 /************/
2915 /* Scanning */
2916 /************/
2917
2918 void __ath10k_scan_finish(struct ath10k *ar)
2919 {
2920 lockdep_assert_held(&ar->data_lock);
2921
2922 switch (ar->scan.state) {
2923 case ATH10K_SCAN_IDLE:
2924 break;
2925 case ATH10K_SCAN_RUNNING:
2926 if (ar->scan.is_roc)
2927 ieee80211_remain_on_channel_expired(ar->hw);
2928 /* fall through */
2929 case ATH10K_SCAN_ABORTING:
2930 if (!ar->scan.is_roc)
2931 ieee80211_scan_completed(ar->hw,
2932 (ar->scan.state ==
2933 ATH10K_SCAN_ABORTING));
2934 /* fall through */
2935 case ATH10K_SCAN_STARTING:
2936 ar->scan.state = ATH10K_SCAN_IDLE;
2937 ar->scan_channel = NULL;
2938 ath10k_offchan_tx_purge(ar);
2939 cancel_delayed_work(&ar->scan.timeout);
2940 complete_all(&ar->scan.completed);
2941 break;
2942 }
2943 }
2944
2945 void ath10k_scan_finish(struct ath10k *ar)
2946 {
2947 spin_lock_bh(&ar->data_lock);
2948 __ath10k_scan_finish(ar);
2949 spin_unlock_bh(&ar->data_lock);
2950 }
2951
2952 static int ath10k_scan_stop(struct ath10k *ar)
2953 {
2954 struct wmi_stop_scan_arg arg = {
2955 .req_id = 1, /* FIXME */
2956 .req_type = WMI_SCAN_STOP_ONE,
2957 .u.scan_id = ATH10K_SCAN_ID,
2958 };
2959 int ret;
2960
2961 lockdep_assert_held(&ar->conf_mutex);
2962
2963 ret = ath10k_wmi_stop_scan(ar, &arg);
2964 if (ret) {
2965 ath10k_warn(ar, "failed to stop wmi scan: %d\n", ret);
2966 goto out;
2967 }
2968
2969 ret = wait_for_completion_timeout(&ar->scan.completed, 3*HZ);
2970 if (ret == 0) {
2971 ath10k_warn(ar, "failed to receive scan abortion completion: timed out\n");
2972 ret = -ETIMEDOUT;
2973 } else if (ret > 0) {
2974 ret = 0;
2975 }
2976
2977 out:
2978 /* Scan state should be updated upon scan completion but in case
2979 * firmware fails to deliver the event (for whatever reason) it is
2980 * desired to clean up scan state anyway. Firmware may have just
2981 * dropped the scan completion event delivery due to transport pipe
2982 * being overflown with data and/or it can recover on its own before
2983 * next scan request is submitted.
2984 */
2985 spin_lock_bh(&ar->data_lock);
2986 if (ar->scan.state != ATH10K_SCAN_IDLE)
2987 __ath10k_scan_finish(ar);
2988 spin_unlock_bh(&ar->data_lock);
2989
2990 return ret;
2991 }
2992
2993 static void ath10k_scan_abort(struct ath10k *ar)
2994 {
2995 int ret;
2996
2997 lockdep_assert_held(&ar->conf_mutex);
2998
2999 spin_lock_bh(&ar->data_lock);
3000
3001 switch (ar->scan.state) {
3002 case ATH10K_SCAN_IDLE:
3003 /* This can happen if timeout worker kicked in and called
3004 * abortion while scan completion was being processed.
3005 */
3006 break;
3007 case ATH10K_SCAN_STARTING:
3008 case ATH10K_SCAN_ABORTING:
3009 ath10k_warn(ar, "refusing scan abortion due to invalid scan state: %s (%d)\n",
3010 ath10k_scan_state_str(ar->scan.state),
3011 ar->scan.state);
3012 break;
3013 case ATH10K_SCAN_RUNNING:
3014 ar->scan.state = ATH10K_SCAN_ABORTING;
3015 spin_unlock_bh(&ar->data_lock);
3016
3017 ret = ath10k_scan_stop(ar);
3018 if (ret)
3019 ath10k_warn(ar, "failed to abort scan: %d\n", ret);
3020
3021 spin_lock_bh(&ar->data_lock);
3022 break;
3023 }
3024
3025 spin_unlock_bh(&ar->data_lock);
3026 }
3027
3028 void ath10k_scan_timeout_work(struct work_struct *work)
3029 {
3030 struct ath10k *ar = container_of(work, struct ath10k,
3031 scan.timeout.work);
3032
3033 mutex_lock(&ar->conf_mutex);
3034 ath10k_scan_abort(ar);
3035 mutex_unlock(&ar->conf_mutex);
3036 }
3037
3038 static int ath10k_start_scan(struct ath10k *ar,
3039 const struct wmi_start_scan_arg *arg)
3040 {
3041 int ret;
3042
3043 lockdep_assert_held(&ar->conf_mutex);
3044
3045 ret = ath10k_wmi_start_scan(ar, arg);
3046 if (ret)
3047 return ret;
3048
3049 ret = wait_for_completion_timeout(&ar->scan.started, 1*HZ);
3050 if (ret == 0) {
3051 ret = ath10k_scan_stop(ar);
3052 if (ret)
3053 ath10k_warn(ar, "failed to stop scan: %d\n", ret);
3054
3055 return -ETIMEDOUT;
3056 }
3057
3058 /* If we failed to start the scan, return error code at
3059 * this point. This is probably due to some issue in the
3060 * firmware, but no need to wedge the driver due to that...
3061 */
3062 spin_lock_bh(&ar->data_lock);
3063 if (ar->scan.state == ATH10K_SCAN_IDLE) {
3064 spin_unlock_bh(&ar->data_lock);
3065 return -EINVAL;
3066 }
3067 spin_unlock_bh(&ar->data_lock);
3068
3069 /* Add a 200ms margin to account for event/command processing */
3070 ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
3071 msecs_to_jiffies(arg->max_scan_time+200));
3072 return 0;
3073 }
3074
3075 /**********************/
3076 /* mac80211 callbacks */
3077 /**********************/
3078
3079 static void ath10k_tx(struct ieee80211_hw *hw,
3080 struct ieee80211_tx_control *control,
3081 struct sk_buff *skb)
3082 {
3083 struct ath10k *ar = hw->priv;
3084 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3085 struct ieee80211_vif *vif = info->control.vif;
3086 struct ieee80211_sta *sta = control->sta;
3087 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
3088 __le16 fc = hdr->frame_control;
3089
3090 /* We should disable CCK RATE due to P2P */
3091 if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
3092 ath10k_dbg(ar, ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
3093
3094 ATH10K_SKB_CB(skb)->htt.is_offchan = false;
3095 ATH10K_SKB_CB(skb)->htt.freq = 0;
3096 ATH10K_SKB_CB(skb)->htt.tid = ath10k_tx_h_get_tid(hdr);
3097 ATH10K_SKB_CB(skb)->vdev_id = ath10k_tx_h_get_vdev_id(ar, vif);
3098 ATH10K_SKB_CB(skb)->txmode = ath10k_tx_h_get_txmode(ar, vif, sta, skb);
3099 ATH10K_SKB_CB(skb)->is_protected = ieee80211_has_protected(fc);
3100
3101 switch (ATH10K_SKB_CB(skb)->txmode) {
3102 case ATH10K_HW_TXRX_MGMT:
3103 case ATH10K_HW_TXRX_NATIVE_WIFI:
3104 ath10k_tx_h_nwifi(hw, skb);
3105 ath10k_tx_h_add_p2p_noa_ie(ar, vif, skb);
3106 ath10k_tx_h_seq_no(vif, skb);
3107 break;
3108 case ATH10K_HW_TXRX_ETHERNET:
3109 ath10k_tx_h_8023(skb);
3110 break;
3111 case ATH10K_HW_TXRX_RAW:
3112 /* FIXME: Packet injection isn't implemented. It should be
3113 * doable with firmware 10.2 on qca988x.
3114 */
3115 WARN_ON_ONCE(1);
3116 ieee80211_free_txskb(hw, skb);
3117 return;
3118 }
3119
3120 if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
3121 spin_lock_bh(&ar->data_lock);
3122 ATH10K_SKB_CB(skb)->htt.freq = ar->scan.roc_freq;
3123 ATH10K_SKB_CB(skb)->vdev_id = ar->scan.vdev_id;
3124 spin_unlock_bh(&ar->data_lock);
3125
3126 if (ath10k_mac_need_offchan_tx_work(ar)) {
3127 ATH10K_SKB_CB(skb)->htt.freq = 0;
3128 ATH10K_SKB_CB(skb)->htt.is_offchan = true;
3129
3130 ath10k_dbg(ar, ATH10K_DBG_MAC, "queued offchannel skb %p\n",
3131 skb);
3132
3133 skb_queue_tail(&ar->offchan_tx_queue, skb);
3134 ieee80211_queue_work(hw, &ar->offchan_tx_work);
3135 return;
3136 }
3137 }
3138
3139 ath10k_mac_tx(ar, skb);
3140 }
3141
3142 /* Must not be called with conf_mutex held as workers can use that also. */
3143 void ath10k_drain_tx(struct ath10k *ar)
3144 {
3145 /* make sure rcu-protected mac80211 tx path itself is drained */
3146 synchronize_net();
3147
3148 ath10k_offchan_tx_purge(ar);
3149 ath10k_mgmt_over_wmi_tx_purge(ar);
3150
3151 cancel_work_sync(&ar->offchan_tx_work);
3152 cancel_work_sync(&ar->wmi_mgmt_tx_work);
3153 }
3154
3155 void ath10k_halt(struct ath10k *ar)
3156 {
3157 struct ath10k_vif *arvif;
3158
3159 lockdep_assert_held(&ar->conf_mutex);
3160
3161 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
3162 ar->filter_flags = 0;
3163 ar->monitor = false;
3164
3165 if (ar->monitor_started)
3166 ath10k_monitor_stop(ar);
3167
3168 ar->monitor_started = false;
3169
3170 ath10k_scan_finish(ar);
3171 ath10k_peer_cleanup_all(ar);
3172 ath10k_core_stop(ar);
3173 ath10k_hif_power_down(ar);
3174
3175 spin_lock_bh(&ar->data_lock);
3176 list_for_each_entry(arvif, &ar->arvifs, list)
3177 ath10k_mac_vif_beacon_cleanup(arvif);
3178 spin_unlock_bh(&ar->data_lock);
3179 }
3180
3181 static int ath10k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
3182 {
3183 struct ath10k *ar = hw->priv;
3184
3185 mutex_lock(&ar->conf_mutex);
3186
3187 if (ar->cfg_tx_chainmask) {
3188 *tx_ant = ar->cfg_tx_chainmask;
3189 *rx_ant = ar->cfg_rx_chainmask;
3190 } else {
3191 *tx_ant = ar->supp_tx_chainmask;
3192 *rx_ant = ar->supp_rx_chainmask;
3193 }
3194
3195 mutex_unlock(&ar->conf_mutex);
3196
3197 return 0;
3198 }
3199
3200 static void ath10k_check_chain_mask(struct ath10k *ar, u32 cm, const char *dbg)
3201 {
3202 /* It is not clear that allowing gaps in chainmask
3203 * is helpful. Probably it will not do what user
3204 * is hoping for, so warn in that case.
3205 */
3206 if (cm == 15 || cm == 7 || cm == 3 || cm == 1 || cm == 0)
3207 return;
3208
3209 ath10k_warn(ar, "mac %s antenna chainmask may be invalid: 0x%x. Suggested values: 15, 7, 3, 1 or 0.\n",
3210 dbg, cm);
3211 }
3212
3213 static int __ath10k_set_antenna(struct ath10k *ar, u32 tx_ant, u32 rx_ant)
3214 {
3215 int ret;
3216
3217 lockdep_assert_held(&ar->conf_mutex);
3218
3219 ath10k_check_chain_mask(ar, tx_ant, "tx");
3220 ath10k_check_chain_mask(ar, rx_ant, "rx");
3221
3222 ar->cfg_tx_chainmask = tx_ant;
3223 ar->cfg_rx_chainmask = rx_ant;
3224
3225 if ((ar->state != ATH10K_STATE_ON) &&
3226 (ar->state != ATH10K_STATE_RESTARTED))
3227 return 0;
3228
3229 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->tx_chain_mask,
3230 tx_ant);
3231 if (ret) {
3232 ath10k_warn(ar, "failed to set tx-chainmask: %d, req 0x%x\n",
3233 ret, tx_ant);
3234 return ret;
3235 }
3236
3237 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->rx_chain_mask,
3238 rx_ant);
3239 if (ret) {
3240 ath10k_warn(ar, "failed to set rx-chainmask: %d, req 0x%x\n",
3241 ret, rx_ant);
3242 return ret;
3243 }
3244
3245 return 0;
3246 }
3247
3248 static int ath10k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
3249 {
3250 struct ath10k *ar = hw->priv;
3251 int ret;
3252
3253 mutex_lock(&ar->conf_mutex);
3254 ret = __ath10k_set_antenna(ar, tx_ant, rx_ant);
3255 mutex_unlock(&ar->conf_mutex);
3256 return ret;
3257 }
3258
3259 static int ath10k_start(struct ieee80211_hw *hw)
3260 {
3261 struct ath10k *ar = hw->priv;
3262 int ret = 0;
3263
3264 /*
3265 * This makes sense only when restarting hw. It is harmless to call
3266 * uncoditionally. This is necessary to make sure no HTT/WMI tx
3267 * commands will be submitted while restarting.
3268 */
3269 ath10k_drain_tx(ar);
3270
3271 mutex_lock(&ar->conf_mutex);
3272
3273 switch (ar->state) {
3274 case ATH10K_STATE_OFF:
3275 ar->state = ATH10K_STATE_ON;
3276 break;
3277 case ATH10K_STATE_RESTARTING:
3278 ath10k_halt(ar);
3279 ar->state = ATH10K_STATE_RESTARTED;
3280 break;
3281 case ATH10K_STATE_ON:
3282 case ATH10K_STATE_RESTARTED:
3283 case ATH10K_STATE_WEDGED:
3284 WARN_ON(1);
3285 ret = -EINVAL;
3286 goto err;
3287 case ATH10K_STATE_UTF:
3288 ret = -EBUSY;
3289 goto err;
3290 }
3291
3292 ret = ath10k_hif_power_up(ar);
3293 if (ret) {
3294 ath10k_err(ar, "Could not init hif: %d\n", ret);
3295 goto err_off;
3296 }
3297
3298 ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_NORMAL);
3299 if (ret) {
3300 ath10k_err(ar, "Could not init core: %d\n", ret);
3301 goto err_power_down;
3302 }
3303
3304 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->pmf_qos, 1);
3305 if (ret) {
3306 ath10k_warn(ar, "failed to enable PMF QOS: %d\n", ret);
3307 goto err_core_stop;
3308 }
3309
3310 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->dynamic_bw, 1);
3311 if (ret) {
3312 ath10k_warn(ar, "failed to enable dynamic BW: %d\n", ret);
3313 goto err_core_stop;
3314 }
3315
3316 if (ar->cfg_tx_chainmask)
3317 __ath10k_set_antenna(ar, ar->cfg_tx_chainmask,
3318 ar->cfg_rx_chainmask);
3319
3320 /*
3321 * By default FW set ARP frames ac to voice (6). In that case ARP
3322 * exchange is not working properly for UAPSD enabled AP. ARP requests
3323 * which arrives with access category 0 are processed by network stack
3324 * and send back with access category 0, but FW changes access category
3325 * to 6. Set ARP frames access category to best effort (0) solves
3326 * this problem.
3327 */
3328
3329 ret = ath10k_wmi_pdev_set_param(ar,
3330 ar->wmi.pdev_param->arp_ac_override, 0);
3331 if (ret) {
3332 ath10k_warn(ar, "failed to set arp ac override parameter: %d\n",
3333 ret);
3334 goto err_core_stop;
3335 }
3336
3337 ret = ath10k_wmi_pdev_set_param(ar,
3338 ar->wmi.pdev_param->ani_enable, 1);
3339 if (ret) {
3340 ath10k_warn(ar, "failed to enable ani by default: %d\n",
3341 ret);
3342 goto err_core_stop;
3343 }
3344
3345 ar->ani_enabled = true;
3346
3347 ar->num_started_vdevs = 0;
3348 ath10k_regd_update(ar);
3349
3350 ath10k_spectral_start(ar);
3351 ath10k_thermal_set_throttling(ar);
3352
3353 mutex_unlock(&ar->conf_mutex);
3354 return 0;
3355
3356 err_core_stop:
3357 ath10k_core_stop(ar);
3358
3359 err_power_down:
3360 ath10k_hif_power_down(ar);
3361
3362 err_off:
3363 ar->state = ATH10K_STATE_OFF;
3364
3365 err:
3366 mutex_unlock(&ar->conf_mutex);
3367 return ret;
3368 }
3369
3370 static void ath10k_stop(struct ieee80211_hw *hw)
3371 {
3372 struct ath10k *ar = hw->priv;
3373
3374 ath10k_drain_tx(ar);
3375
3376 mutex_lock(&ar->conf_mutex);
3377 if (ar->state != ATH10K_STATE_OFF) {
3378 ath10k_halt(ar);
3379 ar->state = ATH10K_STATE_OFF;
3380 }
3381 mutex_unlock(&ar->conf_mutex);
3382
3383 cancel_delayed_work_sync(&ar->scan.timeout);
3384 cancel_work_sync(&ar->restart_work);
3385 }
3386
3387 static int ath10k_config_ps(struct ath10k *ar)
3388 {
3389 struct ath10k_vif *arvif;
3390 int ret = 0;
3391
3392 lockdep_assert_held(&ar->conf_mutex);
3393
3394 list_for_each_entry(arvif, &ar->arvifs, list) {
3395 ret = ath10k_mac_vif_setup_ps(arvif);
3396 if (ret) {
3397 ath10k_warn(ar, "failed to setup powersave: %d\n", ret);
3398 break;
3399 }
3400 }
3401
3402 return ret;
3403 }
3404
3405 static const char *chandef_get_width(enum nl80211_chan_width width)
3406 {
3407 switch (width) {
3408 case NL80211_CHAN_WIDTH_20_NOHT:
3409 return "20 (noht)";
3410 case NL80211_CHAN_WIDTH_20:
3411 return "20";
3412 case NL80211_CHAN_WIDTH_40:
3413 return "40";
3414 case NL80211_CHAN_WIDTH_80:
3415 return "80";
3416 case NL80211_CHAN_WIDTH_80P80:
3417 return "80+80";
3418 case NL80211_CHAN_WIDTH_160:
3419 return "160";
3420 case NL80211_CHAN_WIDTH_5:
3421 return "5";
3422 case NL80211_CHAN_WIDTH_10:
3423 return "10";
3424 }
3425 return "?";
3426 }
3427
3428 static void ath10k_config_chan(struct ath10k *ar)
3429 {
3430 struct ath10k_vif *arvif;
3431 int ret;
3432
3433 lockdep_assert_held(&ar->conf_mutex);
3434
3435 ath10k_dbg(ar, ATH10K_DBG_MAC,
3436 "mac config channel to %dMHz (cf1 %dMHz cf2 %dMHz width %s)\n",
3437 ar->chandef.chan->center_freq,
3438 ar->chandef.center_freq1,
3439 ar->chandef.center_freq2,
3440 chandef_get_width(ar->chandef.width));
3441
3442 /* First stop monitor interface. Some FW versions crash if there's a
3443 * lone monitor interface. */
3444 if (ar->monitor_started)
3445 ath10k_monitor_stop(ar);
3446
3447 list_for_each_entry(arvif, &ar->arvifs, list) {
3448 if (!arvif->is_started)
3449 continue;
3450
3451 if (!arvif->is_up)
3452 continue;
3453
3454 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
3455 continue;
3456
3457 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
3458 if (ret) {
3459 ath10k_warn(ar, "failed to down vdev %d: %d\n",
3460 arvif->vdev_id, ret);
3461 continue;
3462 }
3463 }
3464
3465 /* all vdevs are downed now - attempt to restart and re-up them */
3466
3467 list_for_each_entry(arvif, &ar->arvifs, list) {
3468 if (!arvif->is_started)
3469 continue;
3470
3471 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
3472 continue;
3473
3474 ret = ath10k_mac_setup_bcn_tmpl(arvif);
3475 if (ret)
3476 ath10k_warn(ar, "failed to update bcn tmpl during csa: %d\n",
3477 ret);
3478
3479 ret = ath10k_mac_setup_prb_tmpl(arvif);
3480 if (ret)
3481 ath10k_warn(ar, "failed to update prb tmpl during csa: %d\n",
3482 ret);
3483
3484 ret = ath10k_vdev_restart(arvif);
3485 if (ret) {
3486 ath10k_warn(ar, "failed to restart vdev %d: %d\n",
3487 arvif->vdev_id, ret);
3488 continue;
3489 }
3490
3491 if (!arvif->is_up)
3492 continue;
3493
3494 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
3495 arvif->bssid);
3496 if (ret) {
3497 ath10k_warn(ar, "failed to bring vdev up %d: %d\n",
3498 arvif->vdev_id, ret);
3499 continue;
3500 }
3501 }
3502
3503 ath10k_monitor_recalc(ar);
3504 }
3505
3506 static int ath10k_mac_txpower_setup(struct ath10k *ar, int txpower)
3507 {
3508 int ret;
3509 u32 param;
3510
3511 lockdep_assert_held(&ar->conf_mutex);
3512
3513 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac txpower %d\n", txpower);
3514
3515 param = ar->wmi.pdev_param->txpower_limit2g;
3516 ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
3517 if (ret) {
3518 ath10k_warn(ar, "failed to set 2g txpower %d: %d\n",
3519 txpower, ret);
3520 return ret;
3521 }
3522
3523 param = ar->wmi.pdev_param->txpower_limit5g;
3524 ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
3525 if (ret) {
3526 ath10k_warn(ar, "failed to set 5g txpower %d: %d\n",
3527 txpower, ret);
3528 return ret;
3529 }
3530
3531 return 0;
3532 }
3533
3534 static int ath10k_mac_txpower_recalc(struct ath10k *ar)
3535 {
3536 struct ath10k_vif *arvif;
3537 int ret, txpower = -1;
3538
3539 lockdep_assert_held(&ar->conf_mutex);
3540
3541 list_for_each_entry(arvif, &ar->arvifs, list) {
3542 WARN_ON(arvif->txpower < 0);
3543
3544 if (txpower == -1)
3545 txpower = arvif->txpower;
3546 else
3547 txpower = min(txpower, arvif->txpower);
3548 }
3549
3550 if (WARN_ON(txpower == -1))
3551 return -EINVAL;
3552
3553 ret = ath10k_mac_txpower_setup(ar, txpower);
3554 if (ret) {
3555 ath10k_warn(ar, "failed to setup tx power %d: %d\n",
3556 txpower, ret);
3557 return ret;
3558 }
3559
3560 return 0;
3561 }
3562
3563 static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
3564 {
3565 struct ath10k *ar = hw->priv;
3566 struct ieee80211_conf *conf = &hw->conf;
3567 int ret = 0;
3568
3569 mutex_lock(&ar->conf_mutex);
3570
3571 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
3572 ath10k_dbg(ar, ATH10K_DBG_MAC,
3573 "mac config channel %dMHz flags 0x%x radar %d\n",
3574 conf->chandef.chan->center_freq,
3575 conf->chandef.chan->flags,
3576 conf->radar_enabled);
3577
3578 spin_lock_bh(&ar->data_lock);
3579 ar->rx_channel = conf->chandef.chan;
3580 spin_unlock_bh(&ar->data_lock);
3581
3582 ar->radar_enabled = conf->radar_enabled;
3583 ath10k_recalc_radar_detection(ar);
3584
3585 if (!cfg80211_chandef_identical(&ar->chandef, &conf->chandef)) {
3586 ar->chandef = conf->chandef;
3587 ath10k_config_chan(ar);
3588 }
3589 }
3590
3591 if (changed & IEEE80211_CONF_CHANGE_PS)
3592 ath10k_config_ps(ar);
3593
3594 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
3595 ar->monitor = conf->flags & IEEE80211_CONF_MONITOR;
3596 ret = ath10k_monitor_recalc(ar);
3597 if (ret)
3598 ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
3599 }
3600
3601 mutex_unlock(&ar->conf_mutex);
3602 return ret;
3603 }
3604
3605 static u32 get_nss_from_chainmask(u16 chain_mask)
3606 {
3607 if ((chain_mask & 0x15) == 0x15)
3608 return 4;
3609 else if ((chain_mask & 0x7) == 0x7)
3610 return 3;
3611 else if ((chain_mask & 0x3) == 0x3)
3612 return 2;
3613 return 1;
3614 }
3615
3616 /*
3617 * TODO:
3618 * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
3619 * because we will send mgmt frames without CCK. This requirement
3620 * for P2P_FIND/GO_NEG should be handled by checking CCK flag
3621 * in the TX packet.
3622 */
3623 static int ath10k_add_interface(struct ieee80211_hw *hw,
3624 struct ieee80211_vif *vif)
3625 {
3626 struct ath10k *ar = hw->priv;
3627 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3628 enum wmi_sta_powersave_param param;
3629 int ret = 0;
3630 u32 value;
3631 int bit;
3632 u32 vdev_param;
3633
3634 vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
3635
3636 mutex_lock(&ar->conf_mutex);
3637
3638 memset(arvif, 0, sizeof(*arvif));
3639
3640 arvif->ar = ar;
3641 arvif->vif = vif;
3642
3643 INIT_LIST_HEAD(&arvif->list);
3644 INIT_WORK(&arvif->ap_csa_work, ath10k_mac_vif_ap_csa_work);
3645 INIT_DELAYED_WORK(&arvif->connection_loss_work,
3646 ath10k_mac_vif_sta_connection_loss_work);
3647
3648 if (ar->free_vdev_map == 0) {
3649 ath10k_warn(ar, "Free vdev map is empty, no more interfaces allowed.\n");
3650 ret = -EBUSY;
3651 goto err;
3652 }
3653 bit = __ffs64(ar->free_vdev_map);
3654
3655 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac create vdev %i map %llx\n",
3656 bit, ar->free_vdev_map);
3657
3658 arvif->vdev_id = bit;
3659 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
3660
3661 switch (vif->type) {
3662 case NL80211_IFTYPE_P2P_DEVICE:
3663 arvif->vdev_type = WMI_VDEV_TYPE_STA;
3664 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
3665 break;
3666 case NL80211_IFTYPE_UNSPECIFIED:
3667 case NL80211_IFTYPE_STATION:
3668 arvif->vdev_type = WMI_VDEV_TYPE_STA;
3669 if (vif->p2p)
3670 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
3671 break;
3672 case NL80211_IFTYPE_ADHOC:
3673 arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
3674 break;
3675 case NL80211_IFTYPE_AP:
3676 arvif->vdev_type = WMI_VDEV_TYPE_AP;
3677
3678 if (vif->p2p)
3679 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
3680 break;
3681 case NL80211_IFTYPE_MONITOR:
3682 arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
3683 break;
3684 default:
3685 WARN_ON(1);
3686 break;
3687 }
3688
3689 /* Some firmware revisions don't wait for beacon tx completion before
3690 * sending another SWBA event. This could lead to hardware using old
3691 * (freed) beacon data in some cases, e.g. tx credit starvation
3692 * combined with missed TBTT. This is very very rare.
3693 *
3694 * On non-IOMMU-enabled hosts this could be a possible security issue
3695 * because hw could beacon some random data on the air. On
3696 * IOMMU-enabled hosts DMAR faults would occur in most cases and target
3697 * device would crash.
3698 *
3699 * Since there are no beacon tx completions (implicit nor explicit)
3700 * propagated to host the only workaround for this is to allocate a
3701 * DMA-coherent buffer for a lifetime of a vif and use it for all
3702 * beacon tx commands. Worst case for this approach is some beacons may
3703 * become corrupted, e.g. have garbled IEs or out-of-date TIM bitmap.
3704 */
3705 if (vif->type == NL80211_IFTYPE_ADHOC ||
3706 vif->type == NL80211_IFTYPE_AP) {
3707 arvif->beacon_buf = dma_zalloc_coherent(ar->dev,
3708 IEEE80211_MAX_FRAME_LEN,
3709 &arvif->beacon_paddr,
3710 GFP_ATOMIC);
3711 if (!arvif->beacon_buf) {
3712 ret = -ENOMEM;
3713 ath10k_warn(ar, "failed to allocate beacon buffer: %d\n",
3714 ret);
3715 goto err;
3716 }
3717 }
3718
3719 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d bcnmode %s\n",
3720 arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
3721 arvif->beacon_buf ? "single-buf" : "per-skb");
3722
3723 ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
3724 arvif->vdev_subtype, vif->addr);
3725 if (ret) {
3726 ath10k_warn(ar, "failed to create WMI vdev %i: %d\n",
3727 arvif->vdev_id, ret);
3728 goto err;
3729 }
3730
3731 ar->free_vdev_map &= ~(1LL << arvif->vdev_id);
3732 list_add(&arvif->list, &ar->arvifs);
3733
3734 /* It makes no sense to have firmware do keepalives. mac80211 already
3735 * takes care of this with idle connection polling.
3736 */
3737 ret = ath10k_mac_vif_disable_keepalive(arvif);
3738 if (ret) {
3739 ath10k_warn(ar, "failed to disable keepalive on vdev %i: %d\n",
3740 arvif->vdev_id, ret);
3741 goto err_vdev_delete;
3742 }
3743
3744 arvif->def_wep_key_idx = -1;
3745
3746 vdev_param = ar->wmi.vdev_param->tx_encap_type;
3747 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3748 ATH10K_HW_TXRX_NATIVE_WIFI);
3749 /* 10.X firmware does not support this VDEV parameter. Do not warn */
3750 if (ret && ret != -EOPNOTSUPP) {
3751 ath10k_warn(ar, "failed to set vdev %i TX encapsulation: %d\n",
3752 arvif->vdev_id, ret);
3753 goto err_vdev_delete;
3754 }
3755
3756 if (ar->cfg_tx_chainmask) {
3757 u16 nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
3758
3759 vdev_param = ar->wmi.vdev_param->nss;
3760 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3761 nss);
3762 if (ret) {
3763 ath10k_warn(ar, "failed to set vdev %i chainmask 0x%x, nss %i: %d\n",
3764 arvif->vdev_id, ar->cfg_tx_chainmask, nss,
3765 ret);
3766 goto err_vdev_delete;
3767 }
3768 }
3769
3770 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3771 ret = ath10k_peer_create(ar, arvif->vdev_id, vif->addr,
3772 WMI_PEER_TYPE_DEFAULT);
3773 if (ret) {
3774 ath10k_warn(ar, "failed to create vdev %i peer for AP: %d\n",
3775 arvif->vdev_id, ret);
3776 goto err_vdev_delete;
3777 }
3778
3779 ret = ath10k_mac_set_kickout(arvif);
3780 if (ret) {
3781 ath10k_warn(ar, "failed to set vdev %i kickout parameters: %d\n",
3782 arvif->vdev_id, ret);
3783 goto err_peer_delete;
3784 }
3785 }
3786
3787 if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
3788 param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
3789 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
3790 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3791 param, value);
3792 if (ret) {
3793 ath10k_warn(ar, "failed to set vdev %i RX wake policy: %d\n",
3794 arvif->vdev_id, ret);
3795 goto err_peer_delete;
3796 }
3797
3798 ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
3799 if (ret) {
3800 ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
3801 arvif->vdev_id, ret);
3802 goto err_peer_delete;
3803 }
3804
3805 ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
3806 if (ret) {
3807 ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
3808 arvif->vdev_id, ret);
3809 goto err_peer_delete;
3810 }
3811 }
3812
3813 ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
3814 if (ret) {
3815 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
3816 arvif->vdev_id, ret);
3817 goto err_peer_delete;
3818 }
3819
3820 ret = ath10k_mac_set_frag(arvif, ar->hw->wiphy->frag_threshold);
3821 if (ret) {
3822 ath10k_warn(ar, "failed to set frag threshold for vdev %d: %d\n",
3823 arvif->vdev_id, ret);
3824 goto err_peer_delete;
3825 }
3826
3827 arvif->txpower = vif->bss_conf.txpower;
3828 ret = ath10k_mac_txpower_recalc(ar);
3829 if (ret) {
3830 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
3831 goto err_peer_delete;
3832 }
3833
3834 mutex_unlock(&ar->conf_mutex);
3835 return 0;
3836
3837 err_peer_delete:
3838 if (arvif->vdev_type == WMI_VDEV_TYPE_AP)
3839 ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
3840
3841 err_vdev_delete:
3842 ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
3843 ar->free_vdev_map |= 1LL << arvif->vdev_id;
3844 list_del(&arvif->list);
3845
3846 err:
3847 if (arvif->beacon_buf) {
3848 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
3849 arvif->beacon_buf, arvif->beacon_paddr);
3850 arvif->beacon_buf = NULL;
3851 }
3852
3853 mutex_unlock(&ar->conf_mutex);
3854
3855 return ret;
3856 }
3857
3858 static void ath10k_remove_interface(struct ieee80211_hw *hw,
3859 struct ieee80211_vif *vif)
3860 {
3861 struct ath10k *ar = hw->priv;
3862 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3863 int ret;
3864
3865 cancel_work_sync(&arvif->ap_csa_work);
3866 cancel_delayed_work_sync(&arvif->connection_loss_work);
3867
3868 mutex_lock(&ar->conf_mutex);
3869
3870 spin_lock_bh(&ar->data_lock);
3871 ath10k_mac_vif_beacon_cleanup(arvif);
3872 spin_unlock_bh(&ar->data_lock);
3873
3874 ret = ath10k_spectral_vif_stop(arvif);
3875 if (ret)
3876 ath10k_warn(ar, "failed to stop spectral for vdev %i: %d\n",
3877 arvif->vdev_id, ret);
3878
3879 ar->free_vdev_map |= 1LL << arvif->vdev_id;
3880 list_del(&arvif->list);
3881
3882 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3883 ret = ath10k_wmi_peer_delete(arvif->ar, arvif->vdev_id,
3884 vif->addr);
3885 if (ret)
3886 ath10k_warn(ar, "failed to submit AP self-peer removal on vdev %i: %d\n",
3887 arvif->vdev_id, ret);
3888
3889 kfree(arvif->u.ap.noa_data);
3890 }
3891
3892 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i delete (remove interface)\n",
3893 arvif->vdev_id);
3894
3895 ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
3896 if (ret)
3897 ath10k_warn(ar, "failed to delete WMI vdev %i: %d\n",
3898 arvif->vdev_id, ret);
3899
3900 /* Some firmware revisions don't notify host about self-peer removal
3901 * until after associated vdev is deleted.
3902 */
3903 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3904 ret = ath10k_wait_for_peer_deleted(ar, arvif->vdev_id,
3905 vif->addr);
3906 if (ret)
3907 ath10k_warn(ar, "failed to remove AP self-peer on vdev %i: %d\n",
3908 arvif->vdev_id, ret);
3909
3910 spin_lock_bh(&ar->data_lock);
3911 ar->num_peers--;
3912 spin_unlock_bh(&ar->data_lock);
3913 }
3914
3915 ath10k_peer_cleanup(ar, arvif->vdev_id);
3916
3917 mutex_unlock(&ar->conf_mutex);
3918 }
3919
3920 /*
3921 * FIXME: Has to be verified.
3922 */
3923 #define SUPPORTED_FILTERS \
3924 (FIF_PROMISC_IN_BSS | \
3925 FIF_ALLMULTI | \
3926 FIF_CONTROL | \
3927 FIF_PSPOLL | \
3928 FIF_OTHER_BSS | \
3929 FIF_BCN_PRBRESP_PROMISC | \
3930 FIF_PROBE_REQ | \
3931 FIF_FCSFAIL)
3932
3933 static void ath10k_configure_filter(struct ieee80211_hw *hw,
3934 unsigned int changed_flags,
3935 unsigned int *total_flags,
3936 u64 multicast)
3937 {
3938 struct ath10k *ar = hw->priv;
3939 int ret;
3940
3941 mutex_lock(&ar->conf_mutex);
3942
3943 changed_flags &= SUPPORTED_FILTERS;
3944 *total_flags &= SUPPORTED_FILTERS;
3945 ar->filter_flags = *total_flags;
3946
3947 ret = ath10k_monitor_recalc(ar);
3948 if (ret)
3949 ath10k_warn(ar, "failed to recalc montior: %d\n", ret);
3950
3951 mutex_unlock(&ar->conf_mutex);
3952 }
3953
3954 static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
3955 struct ieee80211_vif *vif,
3956 struct ieee80211_bss_conf *info,
3957 u32 changed)
3958 {
3959 struct ath10k *ar = hw->priv;
3960 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3961 int ret = 0;
3962 u32 vdev_param, pdev_param, slottime, preamble;
3963
3964 mutex_lock(&ar->conf_mutex);
3965
3966 if (changed & BSS_CHANGED_IBSS)
3967 ath10k_control_ibss(arvif, info, vif->addr);
3968
3969 if (changed & BSS_CHANGED_BEACON_INT) {
3970 arvif->beacon_interval = info->beacon_int;
3971 vdev_param = ar->wmi.vdev_param->beacon_interval;
3972 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3973 arvif->beacon_interval);
3974 ath10k_dbg(ar, ATH10K_DBG_MAC,
3975 "mac vdev %d beacon_interval %d\n",
3976 arvif->vdev_id, arvif->beacon_interval);
3977
3978 if (ret)
3979 ath10k_warn(ar, "failed to set beacon interval for vdev %d: %i\n",
3980 arvif->vdev_id, ret);
3981 }
3982
3983 if (changed & BSS_CHANGED_BEACON) {
3984 ath10k_dbg(ar, ATH10K_DBG_MAC,
3985 "vdev %d set beacon tx mode to staggered\n",
3986 arvif->vdev_id);
3987
3988 pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
3989 ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
3990 WMI_BEACON_STAGGERED_MODE);
3991 if (ret)
3992 ath10k_warn(ar, "failed to set beacon mode for vdev %d: %i\n",
3993 arvif->vdev_id, ret);
3994
3995 ret = ath10k_mac_setup_bcn_tmpl(arvif);
3996 if (ret)
3997 ath10k_warn(ar, "failed to update beacon template: %d\n",
3998 ret);
3999 }
4000
4001 if (changed & BSS_CHANGED_AP_PROBE_RESP) {
4002 ret = ath10k_mac_setup_prb_tmpl(arvif);
4003 if (ret)
4004 ath10k_warn(ar, "failed to setup probe resp template on vdev %i: %d\n",
4005 arvif->vdev_id, ret);
4006 }
4007
4008 if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
4009 arvif->dtim_period = info->dtim_period;
4010
4011 ath10k_dbg(ar, ATH10K_DBG_MAC,
4012 "mac vdev %d dtim_period %d\n",
4013 arvif->vdev_id, arvif->dtim_period);
4014
4015 vdev_param = ar->wmi.vdev_param->dtim_period;
4016 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
4017 arvif->dtim_period);
4018 if (ret)
4019 ath10k_warn(ar, "failed to set dtim period for vdev %d: %i\n",
4020 arvif->vdev_id, ret);
4021 }
4022
4023 if (changed & BSS_CHANGED_SSID &&
4024 vif->type == NL80211_IFTYPE_AP) {
4025 arvif->u.ap.ssid_len = info->ssid_len;
4026 if (info->ssid_len)
4027 memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
4028 arvif->u.ap.hidden_ssid = info->hidden_ssid;
4029 }
4030
4031 if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
4032 ether_addr_copy(arvif->bssid, info->bssid);
4033
4034 if (changed & BSS_CHANGED_BEACON_ENABLED)
4035 ath10k_control_beaconing(arvif, info);
4036
4037 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
4038 arvif->use_cts_prot = info->use_cts_prot;
4039 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d cts_prot %d\n",
4040 arvif->vdev_id, info->use_cts_prot);
4041
4042 ret = ath10k_recalc_rtscts_prot(arvif);
4043 if (ret)
4044 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
4045 arvif->vdev_id, ret);
4046
4047 vdev_param = ar->wmi.vdev_param->protection_mode;
4048 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
4049 info->use_cts_prot ? 1 : 0);
4050 if (ret)
4051 ath10k_warn(ar, "failed to set protection mode %d on vdev %i: %d\n",
4052 info->use_cts_prot, arvif->vdev_id, ret);
4053 }
4054
4055 if (changed & BSS_CHANGED_ERP_SLOT) {
4056 if (info->use_short_slot)
4057 slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
4058
4059 else
4060 slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
4061
4062 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
4063 arvif->vdev_id, slottime);
4064
4065 vdev_param = ar->wmi.vdev_param->slot_time;
4066 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
4067 slottime);
4068 if (ret)
4069 ath10k_warn(ar, "failed to set erp slot for vdev %d: %i\n",
4070 arvif->vdev_id, ret);
4071 }
4072
4073 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
4074 if (info->use_short_preamble)
4075 preamble = WMI_VDEV_PREAMBLE_SHORT;
4076 else
4077 preamble = WMI_VDEV_PREAMBLE_LONG;
4078
4079 ath10k_dbg(ar, ATH10K_DBG_MAC,
4080 "mac vdev %d preamble %dn",
4081 arvif->vdev_id, preamble);
4082
4083 vdev_param = ar->wmi.vdev_param->preamble;
4084 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
4085 preamble);
4086 if (ret)
4087 ath10k_warn(ar, "failed to set preamble for vdev %d: %i\n",
4088 arvif->vdev_id, ret);
4089 }
4090
4091 if (changed & BSS_CHANGED_ASSOC) {
4092 if (info->assoc) {
4093 /* Workaround: Make sure monitor vdev is not running
4094 * when associating to prevent some firmware revisions
4095 * (e.g. 10.1 and 10.2) from crashing.
4096 */
4097 if (ar->monitor_started)
4098 ath10k_monitor_stop(ar);
4099 ath10k_bss_assoc(hw, vif, info);
4100 ath10k_monitor_recalc(ar);
4101 } else {
4102 ath10k_bss_disassoc(hw, vif);
4103 }
4104 }
4105
4106 if (changed & BSS_CHANGED_TXPOWER) {
4107 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev_id %i txpower %d\n",
4108 arvif->vdev_id, info->txpower);
4109
4110 arvif->txpower = info->txpower;
4111 ret = ath10k_mac_txpower_recalc(ar);
4112 if (ret)
4113 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
4114 }
4115
4116 if (changed & BSS_CHANGED_PS) {
4117 arvif->ps = vif->bss_conf.ps;
4118
4119 ret = ath10k_config_ps(ar);
4120 if (ret)
4121 ath10k_warn(ar, "failed to setup ps on vdev %i: %d\n",
4122 arvif->vdev_id, ret);
4123 }
4124
4125 mutex_unlock(&ar->conf_mutex);
4126 }
4127
4128 static int ath10k_hw_scan(struct ieee80211_hw *hw,
4129 struct ieee80211_vif *vif,
4130 struct ieee80211_scan_request *hw_req)
4131 {
4132 struct ath10k *ar = hw->priv;
4133 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4134 struct cfg80211_scan_request *req = &hw_req->req;
4135 struct wmi_start_scan_arg arg;
4136 int ret = 0;
4137 int i;
4138
4139 mutex_lock(&ar->conf_mutex);
4140
4141 spin_lock_bh(&ar->data_lock);
4142 switch (ar->scan.state) {
4143 case ATH10K_SCAN_IDLE:
4144 reinit_completion(&ar->scan.started);
4145 reinit_completion(&ar->scan.completed);
4146 ar->scan.state = ATH10K_SCAN_STARTING;
4147 ar->scan.is_roc = false;
4148 ar->scan.vdev_id = arvif->vdev_id;
4149 ret = 0;
4150 break;
4151 case ATH10K_SCAN_STARTING:
4152 case ATH10K_SCAN_RUNNING:
4153 case ATH10K_SCAN_ABORTING:
4154 ret = -EBUSY;
4155 break;
4156 }
4157 spin_unlock_bh(&ar->data_lock);
4158
4159 if (ret)
4160 goto exit;
4161
4162 memset(&arg, 0, sizeof(arg));
4163 ath10k_wmi_start_scan_init(ar, &arg);
4164 arg.vdev_id = arvif->vdev_id;
4165 arg.scan_id = ATH10K_SCAN_ID;
4166
4167 if (!req->no_cck)
4168 arg.scan_ctrl_flags |= WMI_SCAN_ADD_CCK_RATES;
4169
4170 if (req->ie_len) {
4171 arg.ie_len = req->ie_len;
4172 memcpy(arg.ie, req->ie, arg.ie_len);
4173 }
4174
4175 if (req->n_ssids) {
4176 arg.n_ssids = req->n_ssids;
4177 for (i = 0; i < arg.n_ssids; i++) {
4178 arg.ssids[i].len = req->ssids[i].ssid_len;
4179 arg.ssids[i].ssid = req->ssids[i].ssid;
4180 }
4181 } else {
4182 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
4183 }
4184
4185 if (req->n_channels) {
4186 arg.n_channels = req->n_channels;
4187 for (i = 0; i < arg.n_channels; i++)
4188 arg.channels[i] = req->channels[i]->center_freq;
4189 }
4190
4191 ret = ath10k_start_scan(ar, &arg);
4192 if (ret) {
4193 ath10k_warn(ar, "failed to start hw scan: %d\n", ret);
4194 spin_lock_bh(&ar->data_lock);
4195 ar->scan.state = ATH10K_SCAN_IDLE;
4196 spin_unlock_bh(&ar->data_lock);
4197 }
4198
4199 exit:
4200 mutex_unlock(&ar->conf_mutex);
4201 return ret;
4202 }
4203
4204 static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
4205 struct ieee80211_vif *vif)
4206 {
4207 struct ath10k *ar = hw->priv;
4208
4209 mutex_lock(&ar->conf_mutex);
4210 ath10k_scan_abort(ar);
4211 mutex_unlock(&ar->conf_mutex);
4212
4213 cancel_delayed_work_sync(&ar->scan.timeout);
4214 }
4215
4216 static void ath10k_set_key_h_def_keyidx(struct ath10k *ar,
4217 struct ath10k_vif *arvif,
4218 enum set_key_cmd cmd,
4219 struct ieee80211_key_conf *key)
4220 {
4221 u32 vdev_param = arvif->ar->wmi.vdev_param->def_keyid;
4222 int ret;
4223
4224 /* 10.1 firmware branch requires default key index to be set to group
4225 * key index after installing it. Otherwise FW/HW Txes corrupted
4226 * frames with multi-vif APs. This is not required for main firmware
4227 * branch (e.g. 636).
4228 *
4229 * FIXME: This has been tested only in AP. It remains unknown if this
4230 * is required for multi-vif STA interfaces on 10.1 */
4231
4232 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
4233 return;
4234
4235 if (key->cipher == WLAN_CIPHER_SUITE_WEP40)
4236 return;
4237
4238 if (key->cipher == WLAN_CIPHER_SUITE_WEP104)
4239 return;
4240
4241 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4242 return;
4243
4244 if (cmd != SET_KEY)
4245 return;
4246
4247 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
4248 key->keyidx);
4249 if (ret)
4250 ath10k_warn(ar, "failed to set vdev %i group key as default key: %d\n",
4251 arvif->vdev_id, ret);
4252 }
4253
4254 static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
4255 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
4256 struct ieee80211_key_conf *key)
4257 {
4258 struct ath10k *ar = hw->priv;
4259 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4260 struct ath10k_peer *peer;
4261 const u8 *peer_addr;
4262 bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
4263 key->cipher == WLAN_CIPHER_SUITE_WEP104;
4264 int ret = 0;
4265 u32 flags = 0;
4266
4267 /* this one needs to be done in software */
4268 if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
4269 return 1;
4270
4271 if (key->keyidx > WMI_MAX_KEY_INDEX)
4272 return -ENOSPC;
4273
4274 mutex_lock(&ar->conf_mutex);
4275
4276 if (sta)
4277 peer_addr = sta->addr;
4278 else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
4279 peer_addr = vif->bss_conf.bssid;
4280 else
4281 peer_addr = vif->addr;
4282
4283 key->hw_key_idx = key->keyidx;
4284
4285 /* the peer should not disappear in mid-way (unless FW goes awry) since
4286 * we already hold conf_mutex. we just make sure its there now. */
4287 spin_lock_bh(&ar->data_lock);
4288 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
4289 spin_unlock_bh(&ar->data_lock);
4290
4291 if (!peer) {
4292 if (cmd == SET_KEY) {
4293 ath10k_warn(ar, "failed to install key for non-existent peer %pM\n",
4294 peer_addr);
4295 ret = -EOPNOTSUPP;
4296 goto exit;
4297 } else {
4298 /* if the peer doesn't exist there is no key to disable
4299 * anymore */
4300 goto exit;
4301 }
4302 }
4303
4304 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4305 flags |= WMI_KEY_PAIRWISE;
4306 else
4307 flags |= WMI_KEY_GROUP;
4308
4309 if (is_wep) {
4310 if (cmd == SET_KEY)
4311 arvif->wep_keys[key->keyidx] = key;
4312 else
4313 arvif->wep_keys[key->keyidx] = NULL;
4314
4315 if (cmd == DISABLE_KEY)
4316 ath10k_clear_vdev_key(arvif, key);
4317
4318 /* When WEP keys are uploaded it's possible that there are
4319 * stations associated already (e.g. when merging) without any
4320 * keys. Static WEP needs an explicit per-peer key upload.
4321 */
4322 if (vif->type == NL80211_IFTYPE_ADHOC &&
4323 cmd == SET_KEY)
4324 ath10k_mac_vif_update_wep_key(arvif, key);
4325
4326 /* 802.1x never sets the def_wep_key_idx so each set_key()
4327 * call changes default tx key.
4328 *
4329 * Static WEP sets def_wep_key_idx via .set_default_unicast_key
4330 * after first set_key().
4331 */
4332 if (cmd == SET_KEY && arvif->def_wep_key_idx == -1)
4333 flags |= WMI_KEY_TX_USAGE;
4334
4335 /* mac80211 uploads static WEP keys as groupwise while fw/hw
4336 * requires pairwise keys for non-self peers, i.e. BSSID in STA
4337 * mode and associated stations in AP/IBSS.
4338 *
4339 * Static WEP keys for peer_addr=vif->addr and 802.1X WEP keys
4340 * work fine when mapped directly from mac80211.
4341 *
4342 * Note: When installing first static WEP groupwise key (which
4343 * should be pairwise) def_wep_key_idx isn't known yet (it's
4344 * equal to -1). Since .set_default_unicast_key is called only
4345 * for static WEP it's used to re-upload the key as pairwise.
4346 */
4347 if (arvif->def_wep_key_idx >= 0 &&
4348 memcmp(peer_addr, arvif->vif->addr, ETH_ALEN)) {
4349 flags &= ~WMI_KEY_GROUP;
4350 flags |= WMI_KEY_PAIRWISE;
4351 }
4352 }
4353
4354 ret = ath10k_install_key(arvif, key, cmd, peer_addr, flags);
4355 if (ret) {
4356 ath10k_warn(ar, "failed to install key for vdev %i peer %pM: %d\n",
4357 arvif->vdev_id, peer_addr, ret);
4358 goto exit;
4359 }
4360
4361 ath10k_set_key_h_def_keyidx(ar, arvif, cmd, key);
4362
4363 spin_lock_bh(&ar->data_lock);
4364 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
4365 if (peer && cmd == SET_KEY)
4366 peer->keys[key->keyidx] = key;
4367 else if (peer && cmd == DISABLE_KEY)
4368 peer->keys[key->keyidx] = NULL;
4369 else if (peer == NULL)
4370 /* impossible unless FW goes crazy */
4371 ath10k_warn(ar, "Peer %pM disappeared!\n", peer_addr);
4372 spin_unlock_bh(&ar->data_lock);
4373
4374 exit:
4375 mutex_unlock(&ar->conf_mutex);
4376 return ret;
4377 }
4378
4379 static void ath10k_set_default_unicast_key(struct ieee80211_hw *hw,
4380 struct ieee80211_vif *vif,
4381 int keyidx)
4382 {
4383 struct ath10k *ar = hw->priv;
4384 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4385 int ret;
4386
4387 mutex_lock(&arvif->ar->conf_mutex);
4388
4389 if (arvif->ar->state != ATH10K_STATE_ON)
4390 goto unlock;
4391
4392 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
4393 arvif->vdev_id, keyidx);
4394
4395 ret = ath10k_wmi_vdev_set_param(arvif->ar,
4396 arvif->vdev_id,
4397 arvif->ar->wmi.vdev_param->def_keyid,
4398 keyidx);
4399
4400 if (ret) {
4401 ath10k_warn(ar, "failed to update wep key index for vdev %d: %d\n",
4402 arvif->vdev_id,
4403 ret);
4404 goto unlock;
4405 }
4406
4407 arvif->def_wep_key_idx = keyidx;
4408
4409 ret = ath10k_mac_vif_sta_fix_wep_key(arvif);
4410 if (ret) {
4411 ath10k_warn(ar, "failed to fix sta wep key on vdev %i: %d\n",
4412 arvif->vdev_id, ret);
4413 goto unlock;
4414 }
4415
4416 unlock:
4417 mutex_unlock(&arvif->ar->conf_mutex);
4418 }
4419
4420 static void ath10k_sta_rc_update_wk(struct work_struct *wk)
4421 {
4422 struct ath10k *ar;
4423 struct ath10k_vif *arvif;
4424 struct ath10k_sta *arsta;
4425 struct ieee80211_sta *sta;
4426 u32 changed, bw, nss, smps;
4427 int err;
4428
4429 arsta = container_of(wk, struct ath10k_sta, update_wk);
4430 sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
4431 arvif = arsta->arvif;
4432 ar = arvif->ar;
4433
4434 spin_lock_bh(&ar->data_lock);
4435
4436 changed = arsta->changed;
4437 arsta->changed = 0;
4438
4439 bw = arsta->bw;
4440 nss = arsta->nss;
4441 smps = arsta->smps;
4442
4443 spin_unlock_bh(&ar->data_lock);
4444
4445 mutex_lock(&ar->conf_mutex);
4446
4447 if (changed & IEEE80211_RC_BW_CHANGED) {
4448 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM peer bw %d\n",
4449 sta->addr, bw);
4450
4451 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
4452 WMI_PEER_CHAN_WIDTH, bw);
4453 if (err)
4454 ath10k_warn(ar, "failed to update STA %pM peer bw %d: %d\n",
4455 sta->addr, bw, err);
4456 }
4457
4458 if (changed & IEEE80211_RC_NSS_CHANGED) {
4459 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM nss %d\n",
4460 sta->addr, nss);
4461
4462 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
4463 WMI_PEER_NSS, nss);
4464 if (err)
4465 ath10k_warn(ar, "failed to update STA %pM nss %d: %d\n",
4466 sta->addr, nss, err);
4467 }
4468
4469 if (changed & IEEE80211_RC_SMPS_CHANGED) {
4470 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM smps %d\n",
4471 sta->addr, smps);
4472
4473 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
4474 WMI_PEER_SMPS_STATE, smps);
4475 if (err)
4476 ath10k_warn(ar, "failed to update STA %pM smps %d: %d\n",
4477 sta->addr, smps, err);
4478 }
4479
4480 if (changed & IEEE80211_RC_SUPP_RATES_CHANGED ||
4481 changed & IEEE80211_RC_NSS_CHANGED) {
4482 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM supp rates/nss\n",
4483 sta->addr);
4484
4485 err = ath10k_station_assoc(ar, arvif->vif, sta, true);
4486 if (err)
4487 ath10k_warn(ar, "failed to reassociate station: %pM\n",
4488 sta->addr);
4489 }
4490
4491 mutex_unlock(&ar->conf_mutex);
4492 }
4493
4494 static int ath10k_mac_inc_num_stations(struct ath10k_vif *arvif,
4495 struct ieee80211_sta *sta)
4496 {
4497 struct ath10k *ar = arvif->ar;
4498
4499 lockdep_assert_held(&ar->conf_mutex);
4500
4501 if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
4502 return 0;
4503
4504 if (ar->num_stations >= ar->max_num_stations)
4505 return -ENOBUFS;
4506
4507 ar->num_stations++;
4508
4509 return 0;
4510 }
4511
4512 static void ath10k_mac_dec_num_stations(struct ath10k_vif *arvif,
4513 struct ieee80211_sta *sta)
4514 {
4515 struct ath10k *ar = arvif->ar;
4516
4517 lockdep_assert_held(&ar->conf_mutex);
4518
4519 if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
4520 return;
4521
4522 ar->num_stations--;
4523 }
4524
4525 struct ath10k_mac_tdls_iter_data {
4526 u32 num_tdls_stations;
4527 struct ieee80211_vif *curr_vif;
4528 };
4529
4530 static void ath10k_mac_tdls_vif_stations_count_iter(void *data,
4531 struct ieee80211_sta *sta)
4532 {
4533 struct ath10k_mac_tdls_iter_data *iter_data = data;
4534 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
4535 struct ieee80211_vif *sta_vif = arsta->arvif->vif;
4536
4537 if (sta->tdls && sta_vif == iter_data->curr_vif)
4538 iter_data->num_tdls_stations++;
4539 }
4540
4541 static int ath10k_mac_tdls_vif_stations_count(struct ieee80211_hw *hw,
4542 struct ieee80211_vif *vif)
4543 {
4544 struct ath10k_mac_tdls_iter_data data = {};
4545
4546 data.curr_vif = vif;
4547
4548 ieee80211_iterate_stations_atomic(hw,
4549 ath10k_mac_tdls_vif_stations_count_iter,
4550 &data);
4551 return data.num_tdls_stations;
4552 }
4553
4554 static void ath10k_mac_tdls_vifs_count_iter(void *data, u8 *mac,
4555 struct ieee80211_vif *vif)
4556 {
4557 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4558 int *num_tdls_vifs = data;
4559
4560 if (vif->type != NL80211_IFTYPE_STATION)
4561 return;
4562
4563 if (ath10k_mac_tdls_vif_stations_count(arvif->ar->hw, vif) > 0)
4564 (*num_tdls_vifs)++;
4565 }
4566
4567 static int ath10k_mac_tdls_vifs_count(struct ieee80211_hw *hw)
4568 {
4569 int num_tdls_vifs = 0;
4570
4571 ieee80211_iterate_active_interfaces_atomic(hw,
4572 IEEE80211_IFACE_ITER_NORMAL,
4573 ath10k_mac_tdls_vifs_count_iter,
4574 &num_tdls_vifs);
4575 return num_tdls_vifs;
4576 }
4577
4578 static int ath10k_sta_state(struct ieee80211_hw *hw,
4579 struct ieee80211_vif *vif,
4580 struct ieee80211_sta *sta,
4581 enum ieee80211_sta_state old_state,
4582 enum ieee80211_sta_state new_state)
4583 {
4584 struct ath10k *ar = hw->priv;
4585 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4586 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
4587 int ret = 0;
4588
4589 if (old_state == IEEE80211_STA_NOTEXIST &&
4590 new_state == IEEE80211_STA_NONE) {
4591 memset(arsta, 0, sizeof(*arsta));
4592 arsta->arvif = arvif;
4593 INIT_WORK(&arsta->update_wk, ath10k_sta_rc_update_wk);
4594 }
4595
4596 /* cancel must be done outside the mutex to avoid deadlock */
4597 if ((old_state == IEEE80211_STA_NONE &&
4598 new_state == IEEE80211_STA_NOTEXIST))
4599 cancel_work_sync(&arsta->update_wk);
4600
4601 mutex_lock(&ar->conf_mutex);
4602
4603 if (old_state == IEEE80211_STA_NOTEXIST &&
4604 new_state == IEEE80211_STA_NONE) {
4605 /*
4606 * New station addition.
4607 */
4608 enum wmi_peer_type peer_type = WMI_PEER_TYPE_DEFAULT;
4609 u32 num_tdls_stations;
4610 u32 num_tdls_vifs;
4611
4612 ath10k_dbg(ar, ATH10K_DBG_MAC,
4613 "mac vdev %d peer create %pM (new sta) sta %d / %d peer %d / %d\n",
4614 arvif->vdev_id, sta->addr,
4615 ar->num_stations + 1, ar->max_num_stations,
4616 ar->num_peers + 1, ar->max_num_peers);
4617
4618 ret = ath10k_mac_inc_num_stations(arvif, sta);
4619 if (ret) {
4620 ath10k_warn(ar, "refusing to associate station: too many connected already (%d)\n",
4621 ar->max_num_stations);
4622 goto exit;
4623 }
4624
4625 if (sta->tdls)
4626 peer_type = WMI_PEER_TYPE_TDLS;
4627
4628 ret = ath10k_peer_create(ar, arvif->vdev_id, sta->addr,
4629 peer_type);
4630 if (ret) {
4631 ath10k_warn(ar, "failed to add peer %pM for vdev %d when adding a new sta: %i\n",
4632 sta->addr, arvif->vdev_id, ret);
4633 ath10k_mac_dec_num_stations(arvif, sta);
4634 goto exit;
4635 }
4636
4637 if (vif->type == NL80211_IFTYPE_STATION &&
4638 !sta->tdls) {
4639 WARN_ON(arvif->is_started);
4640
4641 ret = ath10k_vdev_start(arvif);
4642 if (ret) {
4643 ath10k_warn(ar, "failed to start vdev %i: %d\n",
4644 arvif->vdev_id, ret);
4645 WARN_ON(ath10k_peer_delete(ar, arvif->vdev_id,
4646 sta->addr));
4647 ath10k_mac_dec_num_stations(arvif, sta);
4648 goto exit;
4649 }
4650
4651 arvif->is_started = true;
4652 }
4653
4654 if (!sta->tdls)
4655 goto exit;
4656
4657 num_tdls_stations = ath10k_mac_tdls_vif_stations_count(hw, vif);
4658 num_tdls_vifs = ath10k_mac_tdls_vifs_count(hw);
4659
4660 if (num_tdls_vifs >= ar->max_num_tdls_vdevs &&
4661 num_tdls_stations == 0) {
4662 ath10k_warn(ar, "vdev %i exceeded maximum number of tdls vdevs %i\n",
4663 arvif->vdev_id, ar->max_num_tdls_vdevs);
4664 ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
4665 ath10k_mac_dec_num_stations(arvif, sta);
4666 ret = -ENOBUFS;
4667 goto exit;
4668 }
4669
4670 if (num_tdls_stations == 0) {
4671 /* This is the first tdls peer in current vif */
4672 enum wmi_tdls_state state = WMI_TDLS_ENABLE_ACTIVE;
4673
4674 ret = ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
4675 state);
4676 if (ret) {
4677 ath10k_warn(ar, "failed to update fw tdls state on vdev %i: %i\n",
4678 arvif->vdev_id, ret);
4679 ath10k_peer_delete(ar, arvif->vdev_id,
4680 sta->addr);
4681 ath10k_mac_dec_num_stations(arvif, sta);
4682 goto exit;
4683 }
4684 }
4685
4686 ret = ath10k_mac_tdls_peer_update(ar, arvif->vdev_id, sta,
4687 WMI_TDLS_PEER_STATE_PEERING);
4688 if (ret) {
4689 ath10k_warn(ar,
4690 "failed to update tdls peer %pM for vdev %d when adding a new sta: %i\n",
4691 sta->addr, arvif->vdev_id, ret);
4692 ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
4693 ath10k_mac_dec_num_stations(arvif, sta);
4694
4695 if (num_tdls_stations != 0)
4696 goto exit;
4697 ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
4698 WMI_TDLS_DISABLE);
4699 }
4700 } else if ((old_state == IEEE80211_STA_NONE &&
4701 new_state == IEEE80211_STA_NOTEXIST)) {
4702 /*
4703 * Existing station deletion.
4704 */
4705 ath10k_dbg(ar, ATH10K_DBG_MAC,
4706 "mac vdev %d peer delete %pM (sta gone)\n",
4707 arvif->vdev_id, sta->addr);
4708
4709 if (vif->type == NL80211_IFTYPE_STATION &&
4710 !sta->tdls) {
4711 WARN_ON(!arvif->is_started);
4712
4713 ret = ath10k_vdev_stop(arvif);
4714 if (ret)
4715 ath10k_warn(ar, "failed to stop vdev %i: %d\n",
4716 arvif->vdev_id, ret);
4717
4718 arvif->is_started = false;
4719 }
4720
4721 ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
4722 if (ret)
4723 ath10k_warn(ar, "failed to delete peer %pM for vdev %d: %i\n",
4724 sta->addr, arvif->vdev_id, ret);
4725
4726 ath10k_mac_dec_num_stations(arvif, sta);
4727
4728 if (!sta->tdls)
4729 goto exit;
4730
4731 if (ath10k_mac_tdls_vif_stations_count(hw, vif))
4732 goto exit;
4733
4734 /* This was the last tdls peer in current vif */
4735 ret = ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
4736 WMI_TDLS_DISABLE);
4737 if (ret) {
4738 ath10k_warn(ar, "failed to update fw tdls state on vdev %i: %i\n",
4739 arvif->vdev_id, ret);
4740 }
4741 } else if (old_state == IEEE80211_STA_AUTH &&
4742 new_state == IEEE80211_STA_ASSOC &&
4743 (vif->type == NL80211_IFTYPE_AP ||
4744 vif->type == NL80211_IFTYPE_ADHOC)) {
4745 /*
4746 * New association.
4747 */
4748 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM associated\n",
4749 sta->addr);
4750
4751 ret = ath10k_station_assoc(ar, vif, sta, false);
4752 if (ret)
4753 ath10k_warn(ar, "failed to associate station %pM for vdev %i: %i\n",
4754 sta->addr, arvif->vdev_id, ret);
4755 } else if (old_state == IEEE80211_STA_ASSOC &&
4756 new_state == IEEE80211_STA_AUTHORIZED &&
4757 sta->tdls) {
4758 /*
4759 * Tdls station authorized.
4760 */
4761 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac tdls sta %pM authorized\n",
4762 sta->addr);
4763
4764 ret = ath10k_station_assoc(ar, vif, sta, false);
4765 if (ret) {
4766 ath10k_warn(ar, "failed to associate tdls station %pM for vdev %i: %i\n",
4767 sta->addr, arvif->vdev_id, ret);
4768 goto exit;
4769 }
4770
4771 ret = ath10k_mac_tdls_peer_update(ar, arvif->vdev_id, sta,
4772 WMI_TDLS_PEER_STATE_CONNECTED);
4773 if (ret)
4774 ath10k_warn(ar, "failed to update tdls peer %pM for vdev %i: %i\n",
4775 sta->addr, arvif->vdev_id, ret);
4776 } else if (old_state == IEEE80211_STA_ASSOC &&
4777 new_state == IEEE80211_STA_AUTH &&
4778 (vif->type == NL80211_IFTYPE_AP ||
4779 vif->type == NL80211_IFTYPE_ADHOC)) {
4780 /*
4781 * Disassociation.
4782 */
4783 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM disassociated\n",
4784 sta->addr);
4785
4786 ret = ath10k_station_disassoc(ar, vif, sta);
4787 if (ret)
4788 ath10k_warn(ar, "failed to disassociate station: %pM vdev %i: %i\n",
4789 sta->addr, arvif->vdev_id, ret);
4790 }
4791 exit:
4792 mutex_unlock(&ar->conf_mutex);
4793 return ret;
4794 }
4795
4796 static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
4797 u16 ac, bool enable)
4798 {
4799 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4800 struct wmi_sta_uapsd_auto_trig_arg arg = {};
4801 u32 prio = 0, acc = 0;
4802 u32 value = 0;
4803 int ret = 0;
4804
4805 lockdep_assert_held(&ar->conf_mutex);
4806
4807 if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
4808 return 0;
4809
4810 switch (ac) {
4811 case IEEE80211_AC_VO:
4812 value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
4813 WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
4814 prio = 7;
4815 acc = 3;
4816 break;
4817 case IEEE80211_AC_VI:
4818 value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
4819 WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
4820 prio = 5;
4821 acc = 2;
4822 break;
4823 case IEEE80211_AC_BE:
4824 value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
4825 WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
4826 prio = 2;
4827 acc = 1;
4828 break;
4829 case IEEE80211_AC_BK:
4830 value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
4831 WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
4832 prio = 0;
4833 acc = 0;
4834 break;
4835 }
4836
4837 if (enable)
4838 arvif->u.sta.uapsd |= value;
4839 else
4840 arvif->u.sta.uapsd &= ~value;
4841
4842 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4843 WMI_STA_PS_PARAM_UAPSD,
4844 arvif->u.sta.uapsd);
4845 if (ret) {
4846 ath10k_warn(ar, "failed to set uapsd params: %d\n", ret);
4847 goto exit;
4848 }
4849
4850 if (arvif->u.sta.uapsd)
4851 value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
4852 else
4853 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
4854
4855 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4856 WMI_STA_PS_PARAM_RX_WAKE_POLICY,
4857 value);
4858 if (ret)
4859 ath10k_warn(ar, "failed to set rx wake param: %d\n", ret);
4860
4861 ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
4862 if (ret) {
4863 ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
4864 arvif->vdev_id, ret);
4865 return ret;
4866 }
4867
4868 ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
4869 if (ret) {
4870 ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
4871 arvif->vdev_id, ret);
4872 return ret;
4873 }
4874
4875 if (test_bit(WMI_SERVICE_STA_UAPSD_BASIC_AUTO_TRIG, ar->wmi.svc_map) ||
4876 test_bit(WMI_SERVICE_STA_UAPSD_VAR_AUTO_TRIG, ar->wmi.svc_map)) {
4877 /* Only userspace can make an educated decision when to send
4878 * trigger frame. The following effectively disables u-UAPSD
4879 * autotrigger in firmware (which is enabled by default
4880 * provided the autotrigger service is available).
4881 */
4882
4883 arg.wmm_ac = acc;
4884 arg.user_priority = prio;
4885 arg.service_interval = 0;
4886 arg.suspend_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
4887 arg.delay_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
4888
4889 ret = ath10k_wmi_vdev_sta_uapsd(ar, arvif->vdev_id,
4890 arvif->bssid, &arg, 1);
4891 if (ret) {
4892 ath10k_warn(ar, "failed to set uapsd auto trigger %d\n",
4893 ret);
4894 return ret;
4895 }
4896 }
4897
4898 exit:
4899 return ret;
4900 }
4901
4902 static int ath10k_conf_tx(struct ieee80211_hw *hw,
4903 struct ieee80211_vif *vif, u16 ac,
4904 const struct ieee80211_tx_queue_params *params)
4905 {
4906 struct ath10k *ar = hw->priv;
4907 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4908 struct wmi_wmm_params_arg *p = NULL;
4909 int ret;
4910
4911 mutex_lock(&ar->conf_mutex);
4912
4913 switch (ac) {
4914 case IEEE80211_AC_VO:
4915 p = &arvif->wmm_params.ac_vo;
4916 break;
4917 case IEEE80211_AC_VI:
4918 p = &arvif->wmm_params.ac_vi;
4919 break;
4920 case IEEE80211_AC_BE:
4921 p = &arvif->wmm_params.ac_be;
4922 break;
4923 case IEEE80211_AC_BK:
4924 p = &arvif->wmm_params.ac_bk;
4925 break;
4926 }
4927
4928 if (WARN_ON(!p)) {
4929 ret = -EINVAL;
4930 goto exit;
4931 }
4932
4933 p->cwmin = params->cw_min;
4934 p->cwmax = params->cw_max;
4935 p->aifs = params->aifs;
4936
4937 /*
4938 * The channel time duration programmed in the HW is in absolute
4939 * microseconds, while mac80211 gives the txop in units of
4940 * 32 microseconds.
4941 */
4942 p->txop = params->txop * 32;
4943
4944 if (ar->wmi.ops->gen_vdev_wmm_conf) {
4945 ret = ath10k_wmi_vdev_wmm_conf(ar, arvif->vdev_id,
4946 &arvif->wmm_params);
4947 if (ret) {
4948 ath10k_warn(ar, "failed to set vdev wmm params on vdev %i: %d\n",
4949 arvif->vdev_id, ret);
4950 goto exit;
4951 }
4952 } else {
4953 /* This won't work well with multi-interface cases but it's
4954 * better than nothing.
4955 */
4956 ret = ath10k_wmi_pdev_set_wmm_params(ar, &arvif->wmm_params);
4957 if (ret) {
4958 ath10k_warn(ar, "failed to set wmm params: %d\n", ret);
4959 goto exit;
4960 }
4961 }
4962
4963 ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
4964 if (ret)
4965 ath10k_warn(ar, "failed to set sta uapsd: %d\n", ret);
4966
4967 exit:
4968 mutex_unlock(&ar->conf_mutex);
4969 return ret;
4970 }
4971
4972 #define ATH10K_ROC_TIMEOUT_HZ (2*HZ)
4973
4974 static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
4975 struct ieee80211_vif *vif,
4976 struct ieee80211_channel *chan,
4977 int duration,
4978 enum ieee80211_roc_type type)
4979 {
4980 struct ath10k *ar = hw->priv;
4981 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4982 struct wmi_start_scan_arg arg;
4983 int ret = 0;
4984
4985 mutex_lock(&ar->conf_mutex);
4986
4987 spin_lock_bh(&ar->data_lock);
4988 switch (ar->scan.state) {
4989 case ATH10K_SCAN_IDLE:
4990 reinit_completion(&ar->scan.started);
4991 reinit_completion(&ar->scan.completed);
4992 reinit_completion(&ar->scan.on_channel);
4993 ar->scan.state = ATH10K_SCAN_STARTING;
4994 ar->scan.is_roc = true;
4995 ar->scan.vdev_id = arvif->vdev_id;
4996 ar->scan.roc_freq = chan->center_freq;
4997 ret = 0;
4998 break;
4999 case ATH10K_SCAN_STARTING:
5000 case ATH10K_SCAN_RUNNING:
5001 case ATH10K_SCAN_ABORTING:
5002 ret = -EBUSY;
5003 break;
5004 }
5005 spin_unlock_bh(&ar->data_lock);
5006
5007 if (ret)
5008 goto exit;
5009
5010 duration = max(duration, WMI_SCAN_CHAN_MIN_TIME_MSEC);
5011
5012 memset(&arg, 0, sizeof(arg));
5013 ath10k_wmi_start_scan_init(ar, &arg);
5014 arg.vdev_id = arvif->vdev_id;
5015 arg.scan_id = ATH10K_SCAN_ID;
5016 arg.n_channels = 1;
5017 arg.channels[0] = chan->center_freq;
5018 arg.dwell_time_active = duration;
5019 arg.dwell_time_passive = duration;
5020 arg.max_scan_time = 2 * duration;
5021 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
5022 arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
5023
5024 ret = ath10k_start_scan(ar, &arg);
5025 if (ret) {
5026 ath10k_warn(ar, "failed to start roc scan: %d\n", ret);
5027 spin_lock_bh(&ar->data_lock);
5028 ar->scan.state = ATH10K_SCAN_IDLE;
5029 spin_unlock_bh(&ar->data_lock);
5030 goto exit;
5031 }
5032
5033 ret = wait_for_completion_timeout(&ar->scan.on_channel, 3*HZ);
5034 if (ret == 0) {
5035 ath10k_warn(ar, "failed to switch to channel for roc scan\n");
5036
5037 ret = ath10k_scan_stop(ar);
5038 if (ret)
5039 ath10k_warn(ar, "failed to stop scan: %d\n", ret);
5040
5041 ret = -ETIMEDOUT;
5042 goto exit;
5043 }
5044
5045 ret = 0;
5046 exit:
5047 mutex_unlock(&ar->conf_mutex);
5048 return ret;
5049 }
5050
5051 static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw)
5052 {
5053 struct ath10k *ar = hw->priv;
5054
5055 mutex_lock(&ar->conf_mutex);
5056 ath10k_scan_abort(ar);
5057 mutex_unlock(&ar->conf_mutex);
5058
5059 cancel_delayed_work_sync(&ar->scan.timeout);
5060
5061 return 0;
5062 }
5063
5064 /*
5065 * Both RTS and Fragmentation threshold are interface-specific
5066 * in ath10k, but device-specific in mac80211.
5067 */
5068
5069 static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
5070 {
5071 struct ath10k *ar = hw->priv;
5072 struct ath10k_vif *arvif;
5073 int ret = 0;
5074
5075 mutex_lock(&ar->conf_mutex);
5076 list_for_each_entry(arvif, &ar->arvifs, list) {
5077 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
5078 arvif->vdev_id, value);
5079
5080 ret = ath10k_mac_set_rts(arvif, value);
5081 if (ret) {
5082 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
5083 arvif->vdev_id, ret);
5084 break;
5085 }
5086 }
5087 mutex_unlock(&ar->conf_mutex);
5088
5089 return ret;
5090 }
5091
5092 static void ath10k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5093 u32 queues, bool drop)
5094 {
5095 struct ath10k *ar = hw->priv;
5096 bool skip;
5097 int ret;
5098
5099 /* mac80211 doesn't care if we really xmit queued frames or not
5100 * we'll collect those frames either way if we stop/delete vdevs */
5101 if (drop)
5102 return;
5103
5104 mutex_lock(&ar->conf_mutex);
5105
5106 if (ar->state == ATH10K_STATE_WEDGED)
5107 goto skip;
5108
5109 ret = wait_event_timeout(ar->htt.empty_tx_wq, ({
5110 bool empty;
5111
5112 spin_lock_bh(&ar->htt.tx_lock);
5113 empty = (ar->htt.num_pending_tx == 0);
5114 spin_unlock_bh(&ar->htt.tx_lock);
5115
5116 skip = (ar->state == ATH10K_STATE_WEDGED) ||
5117 test_bit(ATH10K_FLAG_CRASH_FLUSH,
5118 &ar->dev_flags);
5119
5120 (empty || skip);
5121 }), ATH10K_FLUSH_TIMEOUT_HZ);
5122
5123 if (ret <= 0 || skip)
5124 ath10k_warn(ar, "failed to flush transmit queue (skip %i ar-state %i): %i\n",
5125 skip, ar->state, ret);
5126
5127 skip:
5128 mutex_unlock(&ar->conf_mutex);
5129 }
5130
5131 /* TODO: Implement this function properly
5132 * For now it is needed to reply to Probe Requests in IBSS mode.
5133 * Propably we need this information from FW.
5134 */
5135 static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
5136 {
5137 return 1;
5138 }
5139
5140 static void ath10k_reconfig_complete(struct ieee80211_hw *hw,
5141 enum ieee80211_reconfig_type reconfig_type)
5142 {
5143 struct ath10k *ar = hw->priv;
5144
5145 if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
5146 return;
5147
5148 mutex_lock(&ar->conf_mutex);
5149
5150 /* If device failed to restart it will be in a different state, e.g.
5151 * ATH10K_STATE_WEDGED */
5152 if (ar->state == ATH10K_STATE_RESTARTED) {
5153 ath10k_info(ar, "device successfully recovered\n");
5154 ar->state = ATH10K_STATE_ON;
5155 ieee80211_wake_queues(ar->hw);
5156 }
5157
5158 mutex_unlock(&ar->conf_mutex);
5159 }
5160
5161 static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
5162 struct survey_info *survey)
5163 {
5164 struct ath10k *ar = hw->priv;
5165 struct ieee80211_supported_band *sband;
5166 struct survey_info *ar_survey = &ar->survey[idx];
5167 int ret = 0;
5168
5169 mutex_lock(&ar->conf_mutex);
5170
5171 sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
5172 if (sband && idx >= sband->n_channels) {
5173 idx -= sband->n_channels;
5174 sband = NULL;
5175 }
5176
5177 if (!sband)
5178 sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
5179
5180 if (!sband || idx >= sband->n_channels) {
5181 ret = -ENOENT;
5182 goto exit;
5183 }
5184
5185 spin_lock_bh(&ar->data_lock);
5186 memcpy(survey, ar_survey, sizeof(*survey));
5187 spin_unlock_bh(&ar->data_lock);
5188
5189 survey->channel = &sband->channels[idx];
5190
5191 if (ar->rx_channel == survey->channel)
5192 survey->filled |= SURVEY_INFO_IN_USE;
5193
5194 exit:
5195 mutex_unlock(&ar->conf_mutex);
5196 return ret;
5197 }
5198
5199 /* Helper table for legacy fixed_rate/bitrate_mask */
5200 static const u8 cck_ofdm_rate[] = {
5201 ATH10K_HW_RATE_CCK_LP_1M,
5202 ATH10K_HW_RATE_CCK_LP_2M,
5203 ATH10K_HW_RATE_CCK_LP_5_5M,
5204 ATH10K_HW_RATE_CCK_LP_11M,
5205 ATH10K_HW_RATE_OFDM_6M,
5206 ATH10K_HW_RATE_OFDM_9M,
5207 ATH10K_HW_RATE_OFDM_12M,
5208 ATH10K_HW_RATE_OFDM_18M,
5209 ATH10K_HW_RATE_OFDM_24M,
5210 ATH10K_HW_RATE_OFDM_36M,
5211 ATH10K_HW_RATE_OFDM_48M,
5212 ATH10K_HW_RATE_OFDM_54M,
5213 };
5214
5215 /* Check if only one bit set */
5216 static int ath10k_check_single_mask(u32 mask)
5217 {
5218 int bit;
5219
5220 bit = ffs(mask);
5221 if (!bit)
5222 return 0;
5223
5224 mask &= ~BIT(bit - 1);
5225 if (mask)
5226 return 2;
5227
5228 return 1;
5229 }
5230
5231 static bool
5232 ath10k_default_bitrate_mask(struct ath10k *ar,
5233 enum ieee80211_band band,
5234 const struct cfg80211_bitrate_mask *mask)
5235 {
5236 u32 legacy = 0x00ff;
5237 u8 ht = 0xff, i;
5238 u16 vht = 0x3ff;
5239 u16 nrf = ar->num_rf_chains;
5240
5241 if (ar->cfg_tx_chainmask)
5242 nrf = get_nss_from_chainmask(ar->cfg_tx_chainmask);
5243
5244 switch (band) {
5245 case IEEE80211_BAND_2GHZ:
5246 legacy = 0x00fff;
5247 vht = 0;
5248 break;
5249 case IEEE80211_BAND_5GHZ:
5250 break;
5251 default:
5252 return false;
5253 }
5254
5255 if (mask->control[band].legacy != legacy)
5256 return false;
5257
5258 for (i = 0; i < nrf; i++)
5259 if (mask->control[band].ht_mcs[i] != ht)
5260 return false;
5261
5262 for (i = 0; i < nrf; i++)
5263 if (mask->control[band].vht_mcs[i] != vht)
5264 return false;
5265
5266 return true;
5267 }
5268
5269 static bool
5270 ath10k_bitrate_mask_nss(const struct cfg80211_bitrate_mask *mask,
5271 enum ieee80211_band band,
5272 u8 *fixed_nss)
5273 {
5274 int ht_nss = 0, vht_nss = 0, i;
5275
5276 /* check legacy */
5277 if (ath10k_check_single_mask(mask->control[band].legacy))
5278 return false;
5279
5280 /* check HT */
5281 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
5282 if (mask->control[band].ht_mcs[i] == 0xff)
5283 continue;
5284 else if (mask->control[band].ht_mcs[i] == 0x00)
5285 break;
5286
5287 return false;
5288 }
5289
5290 ht_nss = i;
5291
5292 /* check VHT */
5293 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
5294 if (mask->control[band].vht_mcs[i] == 0x03ff)
5295 continue;
5296 else if (mask->control[band].vht_mcs[i] == 0x0000)
5297 break;
5298
5299 return false;
5300 }
5301
5302 vht_nss = i;
5303
5304 if (ht_nss > 0 && vht_nss > 0)
5305 return false;
5306
5307 if (ht_nss)
5308 *fixed_nss = ht_nss;
5309 else if (vht_nss)
5310 *fixed_nss = vht_nss;
5311 else
5312 return false;
5313
5314 return true;
5315 }
5316
5317 static bool
5318 ath10k_bitrate_mask_correct(const struct cfg80211_bitrate_mask *mask,
5319 enum ieee80211_band band,
5320 enum wmi_rate_preamble *preamble)
5321 {
5322 int legacy = 0, ht = 0, vht = 0, i;
5323
5324 *preamble = WMI_RATE_PREAMBLE_OFDM;
5325
5326 /* check legacy */
5327 legacy = ath10k_check_single_mask(mask->control[band].legacy);
5328 if (legacy > 1)
5329 return false;
5330
5331 /* check HT */
5332 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5333 ht += ath10k_check_single_mask(mask->control[band].ht_mcs[i]);
5334 if (ht > 1)
5335 return false;
5336
5337 /* check VHT */
5338 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
5339 vht += ath10k_check_single_mask(mask->control[band].vht_mcs[i]);
5340 if (vht > 1)
5341 return false;
5342
5343 /* Currently we support only one fixed_rate */
5344 if ((legacy + ht + vht) != 1)
5345 return false;
5346
5347 if (ht)
5348 *preamble = WMI_RATE_PREAMBLE_HT;
5349 else if (vht)
5350 *preamble = WMI_RATE_PREAMBLE_VHT;
5351
5352 return true;
5353 }
5354
5355 static bool
5356 ath10k_bitrate_mask_rate(struct ath10k *ar,
5357 const struct cfg80211_bitrate_mask *mask,
5358 enum ieee80211_band band,
5359 u8 *fixed_rate,
5360 u8 *fixed_nss)
5361 {
5362 u8 rate = 0, pream = 0, nss = 0, i;
5363 enum wmi_rate_preamble preamble;
5364
5365 /* Check if single rate correct */
5366 if (!ath10k_bitrate_mask_correct(mask, band, &preamble))
5367 return false;
5368
5369 pream = preamble;
5370
5371 switch (preamble) {
5372 case WMI_RATE_PREAMBLE_CCK:
5373 case WMI_RATE_PREAMBLE_OFDM:
5374 i = ffs(mask->control[band].legacy) - 1;
5375
5376 if (band == IEEE80211_BAND_2GHZ && i < 4)
5377 pream = WMI_RATE_PREAMBLE_CCK;
5378
5379 if (band == IEEE80211_BAND_5GHZ)
5380 i += 4;
5381
5382 if (i >= ARRAY_SIZE(cck_ofdm_rate))
5383 return false;
5384
5385 rate = cck_ofdm_rate[i];
5386 break;
5387 case WMI_RATE_PREAMBLE_HT:
5388 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5389 if (mask->control[band].ht_mcs[i])
5390 break;
5391
5392 if (i == IEEE80211_HT_MCS_MASK_LEN)
5393 return false;
5394
5395 rate = ffs(mask->control[band].ht_mcs[i]) - 1;
5396 nss = i;
5397 break;
5398 case WMI_RATE_PREAMBLE_VHT:
5399 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
5400 if (mask->control[band].vht_mcs[i])
5401 break;
5402
5403 if (i == NL80211_VHT_NSS_MAX)
5404 return false;
5405
5406 rate = ffs(mask->control[band].vht_mcs[i]) - 1;
5407 nss = i;
5408 break;
5409 }
5410
5411 *fixed_nss = nss + 1;
5412 nss <<= 4;
5413 pream <<= 6;
5414
5415 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac fixed rate pream 0x%02x nss 0x%02x rate 0x%02x\n",
5416 pream, nss, rate);
5417
5418 *fixed_rate = pream | nss | rate;
5419
5420 return true;
5421 }
5422
5423 static bool ath10k_get_fixed_rate_nss(struct ath10k *ar,
5424 const struct cfg80211_bitrate_mask *mask,
5425 enum ieee80211_band band,
5426 u8 *fixed_rate,
5427 u8 *fixed_nss)
5428 {
5429 /* First check full NSS mask, if we can simply limit NSS */
5430 if (ath10k_bitrate_mask_nss(mask, band, fixed_nss))
5431 return true;
5432
5433 /* Next Check single rate is set */
5434 return ath10k_bitrate_mask_rate(ar, mask, band, fixed_rate, fixed_nss);
5435 }
5436
5437 static int ath10k_set_fixed_rate_param(struct ath10k_vif *arvif,
5438 u8 fixed_rate,
5439 u8 fixed_nss,
5440 u8 force_sgi)
5441 {
5442 struct ath10k *ar = arvif->ar;
5443 u32 vdev_param;
5444 int ret = 0;
5445
5446 mutex_lock(&ar->conf_mutex);
5447
5448 if (arvif->fixed_rate == fixed_rate &&
5449 arvif->fixed_nss == fixed_nss &&
5450 arvif->force_sgi == force_sgi)
5451 goto exit;
5452
5453 if (fixed_rate == WMI_FIXED_RATE_NONE)
5454 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac disable fixed bitrate mask\n");
5455
5456 if (force_sgi)
5457 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac force sgi\n");
5458
5459 vdev_param = ar->wmi.vdev_param->fixed_rate;
5460 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
5461 vdev_param, fixed_rate);
5462 if (ret) {
5463 ath10k_warn(ar, "failed to set fixed rate param 0x%02x: %d\n",
5464 fixed_rate, ret);
5465 ret = -EINVAL;
5466 goto exit;
5467 }
5468
5469 arvif->fixed_rate = fixed_rate;
5470
5471 vdev_param = ar->wmi.vdev_param->nss;
5472 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
5473 vdev_param, fixed_nss);
5474
5475 if (ret) {
5476 ath10k_warn(ar, "failed to set fixed nss param %d: %d\n",
5477 fixed_nss, ret);
5478 ret = -EINVAL;
5479 goto exit;
5480 }
5481
5482 arvif->fixed_nss = fixed_nss;
5483
5484 vdev_param = ar->wmi.vdev_param->sgi;
5485 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5486 force_sgi);
5487
5488 if (ret) {
5489 ath10k_warn(ar, "failed to set sgi param %d: %d\n",
5490 force_sgi, ret);
5491 ret = -EINVAL;
5492 goto exit;
5493 }
5494
5495 arvif->force_sgi = force_sgi;
5496
5497 exit:
5498 mutex_unlock(&ar->conf_mutex);
5499 return ret;
5500 }
5501
5502 static int ath10k_set_bitrate_mask(struct ieee80211_hw *hw,
5503 struct ieee80211_vif *vif,
5504 const struct cfg80211_bitrate_mask *mask)
5505 {
5506 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5507 struct ath10k *ar = arvif->ar;
5508 enum ieee80211_band band = ar->hw->conf.chandef.chan->band;
5509 u8 fixed_rate = WMI_FIXED_RATE_NONE;
5510 u8 fixed_nss = ar->num_rf_chains;
5511 u8 force_sgi;
5512
5513 if (ar->cfg_tx_chainmask)
5514 fixed_nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
5515
5516 force_sgi = mask->control[band].gi;
5517 if (force_sgi == NL80211_TXRATE_FORCE_LGI)
5518 return -EINVAL;
5519
5520 if (!ath10k_default_bitrate_mask(ar, band, mask)) {
5521 if (!ath10k_get_fixed_rate_nss(ar, mask, band,
5522 &fixed_rate,
5523 &fixed_nss))
5524 return -EINVAL;
5525 }
5526
5527 if (fixed_rate == WMI_FIXED_RATE_NONE && force_sgi) {
5528 ath10k_warn(ar, "failed to force SGI usage for default rate settings\n");
5529 return -EINVAL;
5530 }
5531
5532 return ath10k_set_fixed_rate_param(arvif, fixed_rate,
5533 fixed_nss, force_sgi);
5534 }
5535
5536 static void ath10k_sta_rc_update(struct ieee80211_hw *hw,
5537 struct ieee80211_vif *vif,
5538 struct ieee80211_sta *sta,
5539 u32 changed)
5540 {
5541 struct ath10k *ar = hw->priv;
5542 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
5543 u32 bw, smps;
5544
5545 spin_lock_bh(&ar->data_lock);
5546
5547 ath10k_dbg(ar, ATH10K_DBG_MAC,
5548 "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
5549 sta->addr, changed, sta->bandwidth, sta->rx_nss,
5550 sta->smps_mode);
5551
5552 if (changed & IEEE80211_RC_BW_CHANGED) {
5553 bw = WMI_PEER_CHWIDTH_20MHZ;
5554
5555 switch (sta->bandwidth) {
5556 case IEEE80211_STA_RX_BW_20:
5557 bw = WMI_PEER_CHWIDTH_20MHZ;
5558 break;
5559 case IEEE80211_STA_RX_BW_40:
5560 bw = WMI_PEER_CHWIDTH_40MHZ;
5561 break;
5562 case IEEE80211_STA_RX_BW_80:
5563 bw = WMI_PEER_CHWIDTH_80MHZ;
5564 break;
5565 case IEEE80211_STA_RX_BW_160:
5566 ath10k_warn(ar, "Invalid bandwith %d in rc update for %pM\n",
5567 sta->bandwidth, sta->addr);
5568 bw = WMI_PEER_CHWIDTH_20MHZ;
5569 break;
5570 }
5571
5572 arsta->bw = bw;
5573 }
5574
5575 if (changed & IEEE80211_RC_NSS_CHANGED)
5576 arsta->nss = sta->rx_nss;
5577
5578 if (changed & IEEE80211_RC_SMPS_CHANGED) {
5579 smps = WMI_PEER_SMPS_PS_NONE;
5580
5581 switch (sta->smps_mode) {
5582 case IEEE80211_SMPS_AUTOMATIC:
5583 case IEEE80211_SMPS_OFF:
5584 smps = WMI_PEER_SMPS_PS_NONE;
5585 break;
5586 case IEEE80211_SMPS_STATIC:
5587 smps = WMI_PEER_SMPS_STATIC;
5588 break;
5589 case IEEE80211_SMPS_DYNAMIC:
5590 smps = WMI_PEER_SMPS_DYNAMIC;
5591 break;
5592 case IEEE80211_SMPS_NUM_MODES:
5593 ath10k_warn(ar, "Invalid smps %d in sta rc update for %pM\n",
5594 sta->smps_mode, sta->addr);
5595 smps = WMI_PEER_SMPS_PS_NONE;
5596 break;
5597 }
5598
5599 arsta->smps = smps;
5600 }
5601
5602 arsta->changed |= changed;
5603
5604 spin_unlock_bh(&ar->data_lock);
5605
5606 ieee80211_queue_work(hw, &arsta->update_wk);
5607 }
5608
5609 static u64 ath10k_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
5610 {
5611 /*
5612 * FIXME: Return 0 for time being. Need to figure out whether FW
5613 * has the API to fetch 64-bit local TSF
5614 */
5615
5616 return 0;
5617 }
5618
5619 static int ath10k_ampdu_action(struct ieee80211_hw *hw,
5620 struct ieee80211_vif *vif,
5621 enum ieee80211_ampdu_mlme_action action,
5622 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
5623 u8 buf_size)
5624 {
5625 struct ath10k *ar = hw->priv;
5626 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5627
5628 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ampdu vdev_id %i sta %pM tid %hu action %d\n",
5629 arvif->vdev_id, sta->addr, tid, action);
5630
5631 switch (action) {
5632 case IEEE80211_AMPDU_RX_START:
5633 case IEEE80211_AMPDU_RX_STOP:
5634 /* HTT AddBa/DelBa events trigger mac80211 Rx BA session
5635 * creation/removal. Do we need to verify this?
5636 */
5637 return 0;
5638 case IEEE80211_AMPDU_TX_START:
5639 case IEEE80211_AMPDU_TX_STOP_CONT:
5640 case IEEE80211_AMPDU_TX_STOP_FLUSH:
5641 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
5642 case IEEE80211_AMPDU_TX_OPERATIONAL:
5643 /* Firmware offloads Tx aggregation entirely so deny mac80211
5644 * Tx aggregation requests.
5645 */
5646 return -EOPNOTSUPP;
5647 }
5648
5649 return -EINVAL;
5650 }
5651
5652 static const struct ieee80211_ops ath10k_ops = {
5653 .tx = ath10k_tx,
5654 .start = ath10k_start,
5655 .stop = ath10k_stop,
5656 .config = ath10k_config,
5657 .add_interface = ath10k_add_interface,
5658 .remove_interface = ath10k_remove_interface,
5659 .configure_filter = ath10k_configure_filter,
5660 .bss_info_changed = ath10k_bss_info_changed,
5661 .hw_scan = ath10k_hw_scan,
5662 .cancel_hw_scan = ath10k_cancel_hw_scan,
5663 .set_key = ath10k_set_key,
5664 .set_default_unicast_key = ath10k_set_default_unicast_key,
5665 .sta_state = ath10k_sta_state,
5666 .conf_tx = ath10k_conf_tx,
5667 .remain_on_channel = ath10k_remain_on_channel,
5668 .cancel_remain_on_channel = ath10k_cancel_remain_on_channel,
5669 .set_rts_threshold = ath10k_set_rts_threshold,
5670 .flush = ath10k_flush,
5671 .tx_last_beacon = ath10k_tx_last_beacon,
5672 .set_antenna = ath10k_set_antenna,
5673 .get_antenna = ath10k_get_antenna,
5674 .reconfig_complete = ath10k_reconfig_complete,
5675 .get_survey = ath10k_get_survey,
5676 .set_bitrate_mask = ath10k_set_bitrate_mask,
5677 .sta_rc_update = ath10k_sta_rc_update,
5678 .get_tsf = ath10k_get_tsf,
5679 .ampdu_action = ath10k_ampdu_action,
5680 .get_et_sset_count = ath10k_debug_get_et_sset_count,
5681 .get_et_stats = ath10k_debug_get_et_stats,
5682 .get_et_strings = ath10k_debug_get_et_strings,
5683
5684 CFG80211_TESTMODE_CMD(ath10k_tm_cmd)
5685
5686 #ifdef CONFIG_PM
5687 .suspend = ath10k_wow_op_suspend,
5688 .resume = ath10k_wow_op_resume,
5689 #endif
5690 #ifdef CONFIG_MAC80211_DEBUGFS
5691 .sta_add_debugfs = ath10k_sta_add_debugfs,
5692 #endif
5693 };
5694
5695 #define CHAN2G(_channel, _freq, _flags) { \
5696 .band = IEEE80211_BAND_2GHZ, \
5697 .hw_value = (_channel), \
5698 .center_freq = (_freq), \
5699 .flags = (_flags), \
5700 .max_antenna_gain = 0, \
5701 .max_power = 30, \
5702 }
5703
5704 #define CHAN5G(_channel, _freq, _flags) { \
5705 .band = IEEE80211_BAND_5GHZ, \
5706 .hw_value = (_channel), \
5707 .center_freq = (_freq), \
5708 .flags = (_flags), \
5709 .max_antenna_gain = 0, \
5710 .max_power = 30, \
5711 }
5712
5713 static const struct ieee80211_channel ath10k_2ghz_channels[] = {
5714 CHAN2G(1, 2412, 0),
5715 CHAN2G(2, 2417, 0),
5716 CHAN2G(3, 2422, 0),
5717 CHAN2G(4, 2427, 0),
5718 CHAN2G(5, 2432, 0),
5719 CHAN2G(6, 2437, 0),
5720 CHAN2G(7, 2442, 0),
5721 CHAN2G(8, 2447, 0),
5722 CHAN2G(9, 2452, 0),
5723 CHAN2G(10, 2457, 0),
5724 CHAN2G(11, 2462, 0),
5725 CHAN2G(12, 2467, 0),
5726 CHAN2G(13, 2472, 0),
5727 CHAN2G(14, 2484, 0),
5728 };
5729
5730 static const struct ieee80211_channel ath10k_5ghz_channels[] = {
5731 CHAN5G(36, 5180, 0),
5732 CHAN5G(40, 5200, 0),
5733 CHAN5G(44, 5220, 0),
5734 CHAN5G(48, 5240, 0),
5735 CHAN5G(52, 5260, 0),
5736 CHAN5G(56, 5280, 0),
5737 CHAN5G(60, 5300, 0),
5738 CHAN5G(64, 5320, 0),
5739 CHAN5G(100, 5500, 0),
5740 CHAN5G(104, 5520, 0),
5741 CHAN5G(108, 5540, 0),
5742 CHAN5G(112, 5560, 0),
5743 CHAN5G(116, 5580, 0),
5744 CHAN5G(120, 5600, 0),
5745 CHAN5G(124, 5620, 0),
5746 CHAN5G(128, 5640, 0),
5747 CHAN5G(132, 5660, 0),
5748 CHAN5G(136, 5680, 0),
5749 CHAN5G(140, 5700, 0),
5750 CHAN5G(144, 5720, 0),
5751 CHAN5G(149, 5745, 0),
5752 CHAN5G(153, 5765, 0),
5753 CHAN5G(157, 5785, 0),
5754 CHAN5G(161, 5805, 0),
5755 CHAN5G(165, 5825, 0),
5756 };
5757
5758 struct ath10k *ath10k_mac_create(size_t priv_size)
5759 {
5760 struct ieee80211_hw *hw;
5761 struct ath10k *ar;
5762
5763 hw = ieee80211_alloc_hw(sizeof(struct ath10k) + priv_size, &ath10k_ops);
5764 if (!hw)
5765 return NULL;
5766
5767 ar = hw->priv;
5768 ar->hw = hw;
5769
5770 return ar;
5771 }
5772
5773 void ath10k_mac_destroy(struct ath10k *ar)
5774 {
5775 ieee80211_free_hw(ar->hw);
5776 }
5777
5778 static const struct ieee80211_iface_limit ath10k_if_limits[] = {
5779 {
5780 .max = 8,
5781 .types = BIT(NL80211_IFTYPE_STATION)
5782 | BIT(NL80211_IFTYPE_P2P_CLIENT)
5783 },
5784 {
5785 .max = 3,
5786 .types = BIT(NL80211_IFTYPE_P2P_GO)
5787 },
5788 {
5789 .max = 1,
5790 .types = BIT(NL80211_IFTYPE_P2P_DEVICE)
5791 },
5792 {
5793 .max = 7,
5794 .types = BIT(NL80211_IFTYPE_AP)
5795 },
5796 };
5797
5798 static const struct ieee80211_iface_limit ath10k_10x_if_limits[] = {
5799 {
5800 .max = 8,
5801 .types = BIT(NL80211_IFTYPE_AP)
5802 },
5803 };
5804
5805 static const struct ieee80211_iface_combination ath10k_if_comb[] = {
5806 {
5807 .limits = ath10k_if_limits,
5808 .n_limits = ARRAY_SIZE(ath10k_if_limits),
5809 .max_interfaces = 8,
5810 .num_different_channels = 1,
5811 .beacon_int_infra_match = true,
5812 },
5813 };
5814
5815 static const struct ieee80211_iface_combination ath10k_10x_if_comb[] = {
5816 {
5817 .limits = ath10k_10x_if_limits,
5818 .n_limits = ARRAY_SIZE(ath10k_10x_if_limits),
5819 .max_interfaces = 8,
5820 .num_different_channels = 1,
5821 .beacon_int_infra_match = true,
5822 #ifdef CONFIG_ATH10K_DFS_CERTIFIED
5823 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
5824 BIT(NL80211_CHAN_WIDTH_20) |
5825 BIT(NL80211_CHAN_WIDTH_40) |
5826 BIT(NL80211_CHAN_WIDTH_80),
5827 #endif
5828 },
5829 };
5830
5831 static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
5832 {
5833 struct ieee80211_sta_vht_cap vht_cap = {0};
5834 u16 mcs_map;
5835 u32 val;
5836 int i;
5837
5838 vht_cap.vht_supported = 1;
5839 vht_cap.cap = ar->vht_cap_info;
5840
5841 if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
5842 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)) {
5843 val = ar->num_rf_chains - 1;
5844 val <<= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
5845 val &= IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
5846
5847 vht_cap.cap |= val;
5848 }
5849
5850 if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
5851 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)) {
5852 val = ar->num_rf_chains - 1;
5853 val <<= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
5854 val &= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
5855
5856 vht_cap.cap |= val;
5857 }
5858
5859 mcs_map = 0;
5860 for (i = 0; i < 8; i++) {
5861 if (i < ar->num_rf_chains)
5862 mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i*2);
5863 else
5864 mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i*2);
5865 }
5866
5867 vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
5868 vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
5869
5870 return vht_cap;
5871 }
5872
5873 static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
5874 {
5875 int i;
5876 struct ieee80211_sta_ht_cap ht_cap = {0};
5877
5878 if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
5879 return ht_cap;
5880
5881 ht_cap.ht_supported = 1;
5882 ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
5883 ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
5884 ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
5885 ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
5886 ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
5887
5888 if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
5889 ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
5890
5891 if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
5892 ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
5893
5894 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
5895 u32 smps;
5896
5897 smps = WLAN_HT_CAP_SM_PS_DYNAMIC;
5898 smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
5899
5900 ht_cap.cap |= smps;
5901 }
5902
5903 if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC)
5904 ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
5905
5906 if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
5907 u32 stbc;
5908
5909 stbc = ar->ht_cap_info;
5910 stbc &= WMI_HT_CAP_RX_STBC;
5911 stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
5912 stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
5913 stbc &= IEEE80211_HT_CAP_RX_STBC;
5914
5915 ht_cap.cap |= stbc;
5916 }
5917
5918 if (ar->ht_cap_info & WMI_HT_CAP_LDPC)
5919 ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
5920
5921 if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
5922 ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
5923
5924 /* max AMSDU is implicitly taken from vht_cap_info */
5925 if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
5926 ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
5927
5928 for (i = 0; i < ar->num_rf_chains; i++)
5929 ht_cap.mcs.rx_mask[i] = 0xFF;
5930
5931 ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
5932
5933 return ht_cap;
5934 }
5935
5936 static void ath10k_get_arvif_iter(void *data, u8 *mac,
5937 struct ieee80211_vif *vif)
5938 {
5939 struct ath10k_vif_iter *arvif_iter = data;
5940 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5941
5942 if (arvif->vdev_id == arvif_iter->vdev_id)
5943 arvif_iter->arvif = arvif;
5944 }
5945
5946 struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
5947 {
5948 struct ath10k_vif_iter arvif_iter;
5949 u32 flags;
5950
5951 memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
5952 arvif_iter.vdev_id = vdev_id;
5953
5954 flags = IEEE80211_IFACE_ITER_RESUME_ALL;
5955 ieee80211_iterate_active_interfaces_atomic(ar->hw,
5956 flags,
5957 ath10k_get_arvif_iter,
5958 &arvif_iter);
5959 if (!arvif_iter.arvif) {
5960 ath10k_warn(ar, "No VIF found for vdev %d\n", vdev_id);
5961 return NULL;
5962 }
5963
5964 return arvif_iter.arvif;
5965 }
5966
5967 int ath10k_mac_register(struct ath10k *ar)
5968 {
5969 static const u32 cipher_suites[] = {
5970 WLAN_CIPHER_SUITE_WEP40,
5971 WLAN_CIPHER_SUITE_WEP104,
5972 WLAN_CIPHER_SUITE_TKIP,
5973 WLAN_CIPHER_SUITE_CCMP,
5974 WLAN_CIPHER_SUITE_AES_CMAC,
5975 };
5976 struct ieee80211_supported_band *band;
5977 struct ieee80211_sta_vht_cap vht_cap;
5978 struct ieee80211_sta_ht_cap ht_cap;
5979 void *channels;
5980 int ret;
5981
5982 SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
5983
5984 SET_IEEE80211_DEV(ar->hw, ar->dev);
5985
5986 ht_cap = ath10k_get_ht_cap(ar);
5987 vht_cap = ath10k_create_vht_cap(ar);
5988
5989 if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
5990 channels = kmemdup(ath10k_2ghz_channels,
5991 sizeof(ath10k_2ghz_channels),
5992 GFP_KERNEL);
5993 if (!channels) {
5994 ret = -ENOMEM;
5995 goto err_free;
5996 }
5997
5998 band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
5999 band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
6000 band->channels = channels;
6001 band->n_bitrates = ath10k_g_rates_size;
6002 band->bitrates = ath10k_g_rates;
6003 band->ht_cap = ht_cap;
6004
6005 /* Enable the VHT support at 2.4 GHz */
6006 band->vht_cap = vht_cap;
6007
6008 ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
6009 }
6010
6011 if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
6012 channels = kmemdup(ath10k_5ghz_channels,
6013 sizeof(ath10k_5ghz_channels),
6014 GFP_KERNEL);
6015 if (!channels) {
6016 ret = -ENOMEM;
6017 goto err_free;
6018 }
6019
6020 band = &ar->mac.sbands[IEEE80211_BAND_5GHZ];
6021 band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
6022 band->channels = channels;
6023 band->n_bitrates = ath10k_a_rates_size;
6024 band->bitrates = ath10k_a_rates;
6025 band->ht_cap = ht_cap;
6026 band->vht_cap = vht_cap;
6027 ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
6028 }
6029
6030 ar->hw->wiphy->interface_modes =
6031 BIT(NL80211_IFTYPE_STATION) |
6032 BIT(NL80211_IFTYPE_AP);
6033
6034 ar->hw->wiphy->available_antennas_rx = ar->supp_rx_chainmask;
6035 ar->hw->wiphy->available_antennas_tx = ar->supp_tx_chainmask;
6036
6037 if (!test_bit(ATH10K_FW_FEATURE_NO_P2P, ar->fw_features))
6038 ar->hw->wiphy->interface_modes |=
6039 BIT(NL80211_IFTYPE_P2P_DEVICE) |
6040 BIT(NL80211_IFTYPE_P2P_CLIENT) |
6041 BIT(NL80211_IFTYPE_P2P_GO);
6042
6043 ar->hw->flags = IEEE80211_HW_SIGNAL_DBM |
6044 IEEE80211_HW_SUPPORTS_PS |
6045 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
6046 IEEE80211_HW_MFP_CAPABLE |
6047 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
6048 IEEE80211_HW_HAS_RATE_CONTROL |
6049 IEEE80211_HW_AP_LINK_PS |
6050 IEEE80211_HW_SPECTRUM_MGMT |
6051 IEEE80211_HW_SW_CRYPTO_CONTROL |
6052 IEEE80211_HW_CONNECTION_MONITOR;
6053
6054 ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
6055
6056 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
6057 ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
6058
6059 if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
6060 ar->hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
6061 ar->hw->flags |= IEEE80211_HW_TX_AMPDU_SETUP_IN_HW;
6062 }
6063
6064 ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
6065 ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
6066
6067 ar->hw->vif_data_size = sizeof(struct ath10k_vif);
6068 ar->hw->sta_data_size = sizeof(struct ath10k_sta);
6069
6070 ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
6071
6072 if (test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)) {
6073 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
6074
6075 /* Firmware delivers WPS/P2P Probe Requests frames to driver so
6076 * that userspace (e.g. wpa_supplicant/hostapd) can generate
6077 * correct Probe Responses. This is more of a hack advert..
6078 */
6079 ar->hw->wiphy->probe_resp_offload |=
6080 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
6081 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
6082 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
6083 }
6084
6085 if (test_bit(WMI_SERVICE_TDLS, ar->wmi.svc_map))
6086 ar->hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
6087
6088 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
6089 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
6090 ar->hw->wiphy->max_remain_on_channel_duration = 5000;
6091
6092 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
6093 ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE;
6094
6095 ar->hw->wiphy->max_ap_assoc_sta = ar->max_num_stations;
6096
6097 ret = ath10k_wow_init(ar);
6098 if (ret) {
6099 ath10k_warn(ar, "failed to init wow: %d\n", ret);
6100 goto err_free;
6101 }
6102
6103 /*
6104 * on LL hardware queues are managed entirely by the FW
6105 * so we only advertise to mac we can do the queues thing
6106 */
6107 ar->hw->queues = 4;
6108
6109 switch (ar->wmi.op_version) {
6110 case ATH10K_FW_WMI_OP_VERSION_MAIN:
6111 case ATH10K_FW_WMI_OP_VERSION_TLV:
6112 ar->hw->wiphy->iface_combinations = ath10k_if_comb;
6113 ar->hw->wiphy->n_iface_combinations =
6114 ARRAY_SIZE(ath10k_if_comb);
6115 ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
6116 break;
6117 case ATH10K_FW_WMI_OP_VERSION_10_1:
6118 case ATH10K_FW_WMI_OP_VERSION_10_2:
6119 case ATH10K_FW_WMI_OP_VERSION_10_2_4:
6120 ar->hw->wiphy->iface_combinations = ath10k_10x_if_comb;
6121 ar->hw->wiphy->n_iface_combinations =
6122 ARRAY_SIZE(ath10k_10x_if_comb);
6123 break;
6124 case ATH10K_FW_WMI_OP_VERSION_UNSET:
6125 case ATH10K_FW_WMI_OP_VERSION_MAX:
6126 WARN_ON(1);
6127 ret = -EINVAL;
6128 goto err_free;
6129 }
6130
6131 ar->hw->netdev_features = NETIF_F_HW_CSUM;
6132
6133 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED)) {
6134 /* Init ath dfs pattern detector */
6135 ar->ath_common.debug_mask = ATH_DBG_DFS;
6136 ar->dfs_detector = dfs_pattern_detector_init(&ar->ath_common,
6137 NL80211_DFS_UNSET);
6138
6139 if (!ar->dfs_detector)
6140 ath10k_warn(ar, "failed to initialise DFS pattern detector\n");
6141 }
6142
6143 ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
6144 ath10k_reg_notifier);
6145 if (ret) {
6146 ath10k_err(ar, "failed to initialise regulatory: %i\n", ret);
6147 goto err_free;
6148 }
6149
6150 ar->hw->wiphy->cipher_suites = cipher_suites;
6151 ar->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
6152
6153 ret = ieee80211_register_hw(ar->hw);
6154 if (ret) {
6155 ath10k_err(ar, "failed to register ieee80211: %d\n", ret);
6156 goto err_free;
6157 }
6158
6159 if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
6160 ret = regulatory_hint(ar->hw->wiphy,
6161 ar->ath_common.regulatory.alpha2);
6162 if (ret)
6163 goto err_unregister;
6164 }
6165
6166 return 0;
6167
6168 err_unregister:
6169 ieee80211_unregister_hw(ar->hw);
6170 err_free:
6171 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
6172 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
6173
6174 return ret;
6175 }
6176
6177 void ath10k_mac_unregister(struct ath10k *ar)
6178 {
6179 ieee80211_unregister_hw(ar->hw);
6180
6181 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
6182 ar->dfs_detector->exit(ar->dfs_detector);
6183
6184 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
6185 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
6186
6187 SET_IEEE80211_DEV(ar->hw, NULL);
6188 }
This page took 0.217652 seconds and 5 git commands to generate.