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