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