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