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