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