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