Merge ath-next from ath.git
[linux-2.6-block.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
5c81c7fd
MK
2554 /* Add a 200ms margin to account for event/command processing */
2555 ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
2556 msecs_to_jiffies(arg->max_scan_time+200));
5e3dd157
KV
2557 return 0;
2558}
2559
2560/**********************/
2561/* mac80211 callbacks */
2562/**********************/
2563
2564static void ath10k_tx(struct ieee80211_hw *hw,
2565 struct ieee80211_tx_control *control,
2566 struct sk_buff *skb)
2567{
4b604558 2568 struct ath10k *ar = hw->priv;
5e3dd157 2569 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4b604558 2570 struct ieee80211_vif *vif = info->control.vif;
5e3dd157 2571 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
5e3dd157
KV
2572
2573 /* We should disable CCK RATE due to P2P */
2574 if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
7aa7a72a 2575 ath10k_dbg(ar, ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
5e3dd157 2576
4b604558
MK
2577 ATH10K_SKB_CB(skb)->htt.is_offchan = false;
2578 ATH10K_SKB_CB(skb)->htt.tid = ath10k_tx_h_get_tid(hdr);
2b37c295 2579 ATH10K_SKB_CB(skb)->vdev_id = ath10k_tx_h_get_vdev_id(ar, vif);
5e3dd157 2580
cf84bd4d 2581 /* it makes no sense to process injected frames like that */
4b604558
MK
2582 if (vif && vif->type != NL80211_IFTYPE_MONITOR) {
2583 ath10k_tx_h_nwifi(hw, skb);
4b604558
MK
2584 ath10k_tx_h_add_p2p_noa_ie(ar, vif, skb);
2585 ath10k_tx_h_seq_no(vif, skb);
cf84bd4d 2586 }
5e3dd157 2587
5e3dd157
KV
2588 if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
2589 spin_lock_bh(&ar->data_lock);
8d6d3624 2590 ATH10K_SKB_CB(skb)->htt.freq = ar->scan.roc_freq;
5e00d31a 2591 ATH10K_SKB_CB(skb)->vdev_id = ar->scan.vdev_id;
5e3dd157
KV
2592 spin_unlock_bh(&ar->data_lock);
2593
8d6d3624
MK
2594 if (ath10k_mac_need_offchan_tx_work(ar)) {
2595 ATH10K_SKB_CB(skb)->htt.freq = 0;
2596 ATH10K_SKB_CB(skb)->htt.is_offchan = true;
5e3dd157 2597
8d6d3624
MK
2598 ath10k_dbg(ar, ATH10K_DBG_MAC, "queued offchannel skb %p\n",
2599 skb);
2600
2601 skb_queue_tail(&ar->offchan_tx_queue, skb);
2602 ieee80211_queue_work(hw, &ar->offchan_tx_work);
2603 return;
2604 }
5e3dd157
KV
2605 }
2606
2607 ath10k_tx_htt(ar, skb);
2608}
2609
bca7bafb 2610/* Must not be called with conf_mutex held as workers can use that also. */
7962b0d8 2611void ath10k_drain_tx(struct ath10k *ar)
bca7bafb
MK
2612{
2613 /* make sure rcu-protected mac80211 tx path itself is drained */
2614 synchronize_net();
2615
2616 ath10k_offchan_tx_purge(ar);
2617 ath10k_mgmt_over_wmi_tx_purge(ar);
2618
2619 cancel_work_sync(&ar->offchan_tx_work);
2620 cancel_work_sync(&ar->wmi_mgmt_tx_work);
2621}
2622
affd3217 2623void ath10k_halt(struct ath10k *ar)
818bdd16 2624{
d9bc4b9b
MK
2625 struct ath10k_vif *arvif;
2626
818bdd16
MK
2627 lockdep_assert_held(&ar->conf_mutex);
2628
1933747f
MK
2629 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
2630 ar->filter_flags = 0;
2631 ar->monitor = false;
2632
2633 if (ar->monitor_started)
1bbc0975 2634 ath10k_monitor_stop(ar);
1933747f
MK
2635
2636 ar->monitor_started = false;
1bbc0975 2637
5c81c7fd 2638 ath10k_scan_finish(ar);
818bdd16
MK
2639 ath10k_peer_cleanup_all(ar);
2640 ath10k_core_stop(ar);
2641 ath10k_hif_power_down(ar);
2642
2643 spin_lock_bh(&ar->data_lock);
64badcb6
MK
2644 list_for_each_entry(arvif, &ar->arvifs, list)
2645 ath10k_mac_vif_beacon_cleanup(arvif);
818bdd16
MK
2646 spin_unlock_bh(&ar->data_lock);
2647}
2648
46acf7bb
BG
2649static int ath10k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
2650{
2651 struct ath10k *ar = hw->priv;
2652
2653 mutex_lock(&ar->conf_mutex);
2654
2655 if (ar->cfg_tx_chainmask) {
2656 *tx_ant = ar->cfg_tx_chainmask;
2657 *rx_ant = ar->cfg_rx_chainmask;
2658 } else {
2659 *tx_ant = ar->supp_tx_chainmask;
2660 *rx_ant = ar->supp_rx_chainmask;
2661 }
2662
2663 mutex_unlock(&ar->conf_mutex);
2664
2665 return 0;
2666}
2667
5572a95b
BG
2668static void ath10k_check_chain_mask(struct ath10k *ar, u32 cm, const char *dbg)
2669{
2670 /* It is not clear that allowing gaps in chainmask
2671 * is helpful. Probably it will not do what user
2672 * is hoping for, so warn in that case.
2673 */
2674 if (cm == 15 || cm == 7 || cm == 3 || cm == 1 || cm == 0)
2675 return;
2676
2677 ath10k_warn(ar, "mac %s antenna chainmask may be invalid: 0x%x. Suggested values: 15, 7, 3, 1 or 0.\n",
2678 dbg, cm);
2679}
2680
46acf7bb
BG
2681static int __ath10k_set_antenna(struct ath10k *ar, u32 tx_ant, u32 rx_ant)
2682{
2683 int ret;
2684
2685 lockdep_assert_held(&ar->conf_mutex);
2686
5572a95b
BG
2687 ath10k_check_chain_mask(ar, tx_ant, "tx");
2688 ath10k_check_chain_mask(ar, rx_ant, "rx");
2689
46acf7bb
BG
2690 ar->cfg_tx_chainmask = tx_ant;
2691 ar->cfg_rx_chainmask = rx_ant;
2692
2693 if ((ar->state != ATH10K_STATE_ON) &&
2694 (ar->state != ATH10K_STATE_RESTARTED))
2695 return 0;
2696
2697 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->tx_chain_mask,
2698 tx_ant);
2699 if (ret) {
7aa7a72a 2700 ath10k_warn(ar, "failed to set tx-chainmask: %d, req 0x%x\n",
46acf7bb
BG
2701 ret, tx_ant);
2702 return ret;
2703 }
2704
2705 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->rx_chain_mask,
2706 rx_ant);
2707 if (ret) {
7aa7a72a 2708 ath10k_warn(ar, "failed to set rx-chainmask: %d, req 0x%x\n",
46acf7bb
BG
2709 ret, rx_ant);
2710 return ret;
2711 }
2712
2713 return 0;
2714}
2715
2716static int ath10k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
2717{
2718 struct ath10k *ar = hw->priv;
2719 int ret;
2720
2721 mutex_lock(&ar->conf_mutex);
2722 ret = __ath10k_set_antenna(ar, tx_ant, rx_ant);
2723 mutex_unlock(&ar->conf_mutex);
2724 return ret;
2725}
2726
5e3dd157
KV
2727static int ath10k_start(struct ieee80211_hw *hw)
2728{
2729 struct ath10k *ar = hw->priv;
818bdd16 2730 int ret = 0;
5e3dd157 2731
bca7bafb
MK
2732 /*
2733 * This makes sense only when restarting hw. It is harmless to call
2734 * uncoditionally. This is necessary to make sure no HTT/WMI tx
2735 * commands will be submitted while restarting.
2736 */
2737 ath10k_drain_tx(ar);
2738
548db54c
MK
2739 mutex_lock(&ar->conf_mutex);
2740
c5058f5b
MK
2741 switch (ar->state) {
2742 case ATH10K_STATE_OFF:
2743 ar->state = ATH10K_STATE_ON;
2744 break;
2745 case ATH10K_STATE_RESTARTING:
2746 ath10k_halt(ar);
2747 ar->state = ATH10K_STATE_RESTARTED;
2748 break;
2749 case ATH10K_STATE_ON:
2750 case ATH10K_STATE_RESTARTED:
2751 case ATH10K_STATE_WEDGED:
2752 WARN_ON(1);
818bdd16 2753 ret = -EINVAL;
ae254433 2754 goto err;
43d2a30f
KV
2755 case ATH10K_STATE_UTF:
2756 ret = -EBUSY;
2757 goto err;
818bdd16
MK
2758 }
2759
2760 ret = ath10k_hif_power_up(ar);
2761 if (ret) {
7aa7a72a 2762 ath10k_err(ar, "Could not init hif: %d\n", ret);
ae254433 2763 goto err_off;
818bdd16
MK
2764 }
2765
43d2a30f 2766 ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_NORMAL);
818bdd16 2767 if (ret) {
7aa7a72a 2768 ath10k_err(ar, "Could not init core: %d\n", ret);
ae254433 2769 goto err_power_down;
818bdd16
MK
2770 }
2771
226a339b 2772 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->pmf_qos, 1);
ae254433 2773 if (ret) {
7aa7a72a 2774 ath10k_warn(ar, "failed to enable PMF QOS: %d\n", ret);
ae254433
MK
2775 goto err_core_stop;
2776 }
5e3dd157 2777
c4dd0d01 2778 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->dynamic_bw, 1);
ae254433 2779 if (ret) {
7aa7a72a 2780 ath10k_warn(ar, "failed to enable dynamic BW: %d\n", ret);
ae254433
MK
2781 goto err_core_stop;
2782 }
5e3dd157 2783
46acf7bb
BG
2784 if (ar->cfg_tx_chainmask)
2785 __ath10k_set_antenna(ar, ar->cfg_tx_chainmask,
2786 ar->cfg_rx_chainmask);
2787
ab6258ed
MP
2788 /*
2789 * By default FW set ARP frames ac to voice (6). In that case ARP
2790 * exchange is not working properly for UAPSD enabled AP. ARP requests
2791 * which arrives with access category 0 are processed by network stack
2792 * and send back with access category 0, but FW changes access category
2793 * to 6. Set ARP frames access category to best effort (0) solves
2794 * this problem.
2795 */
2796
2797 ret = ath10k_wmi_pdev_set_param(ar,
2798 ar->wmi.pdev_param->arp_ac_override, 0);
2799 if (ret) {
7aa7a72a 2800 ath10k_warn(ar, "failed to set arp ac override parameter: %d\n",
ab6258ed 2801 ret);
ae254433 2802 goto err_core_stop;
ab6258ed
MP
2803 }
2804
d650097b 2805 ar->num_started_vdevs = 0;
f7843d7f
MK
2806 ath10k_regd_update(ar);
2807
855aed12
SW
2808 ath10k_spectral_start(ar);
2809
ae254433
MK
2810 mutex_unlock(&ar->conf_mutex);
2811 return 0;
2812
2813err_core_stop:
2814 ath10k_core_stop(ar);
2815
2816err_power_down:
2817 ath10k_hif_power_down(ar);
2818
2819err_off:
2820 ar->state = ATH10K_STATE_OFF;
2821
2822err:
548db54c 2823 mutex_unlock(&ar->conf_mutex);
c60bdd83 2824 return ret;
5e3dd157
KV
2825}
2826
2827static void ath10k_stop(struct ieee80211_hw *hw)
2828{
2829 struct ath10k *ar = hw->priv;
2830
bca7bafb
MK
2831 ath10k_drain_tx(ar);
2832
548db54c 2833 mutex_lock(&ar->conf_mutex);
c5058f5b 2834 if (ar->state != ATH10K_STATE_OFF) {
818bdd16 2835 ath10k_halt(ar);
c5058f5b
MK
2836 ar->state = ATH10K_STATE_OFF;
2837 }
548db54c
MK
2838 mutex_unlock(&ar->conf_mutex);
2839
5c81c7fd 2840 cancel_delayed_work_sync(&ar->scan.timeout);
affd3217 2841 cancel_work_sync(&ar->restart_work);
5e3dd157
KV
2842}
2843
ad088bfa 2844static int ath10k_config_ps(struct ath10k *ar)
5e3dd157 2845{
ad088bfa
MK
2846 struct ath10k_vif *arvif;
2847 int ret = 0;
affd3217
MK
2848
2849 lockdep_assert_held(&ar->conf_mutex);
2850
ad088bfa
MK
2851 list_for_each_entry(arvif, &ar->arvifs, list) {
2852 ret = ath10k_mac_vif_setup_ps(arvif);
2853 if (ret) {
7aa7a72a 2854 ath10k_warn(ar, "failed to setup powersave: %d\n", ret);
ad088bfa
MK
2855 break;
2856 }
2857 }
affd3217 2858
ad088bfa 2859 return ret;
affd3217
MK
2860}
2861
c930f744
MK
2862static const char *chandef_get_width(enum nl80211_chan_width width)
2863{
2864 switch (width) {
2865 case NL80211_CHAN_WIDTH_20_NOHT:
2866 return "20 (noht)";
2867 case NL80211_CHAN_WIDTH_20:
2868 return "20";
2869 case NL80211_CHAN_WIDTH_40:
2870 return "40";
2871 case NL80211_CHAN_WIDTH_80:
2872 return "80";
2873 case NL80211_CHAN_WIDTH_80P80:
2874 return "80+80";
2875 case NL80211_CHAN_WIDTH_160:
2876 return "160";
2877 case NL80211_CHAN_WIDTH_5:
2878 return "5";
2879 case NL80211_CHAN_WIDTH_10:
2880 return "10";
2881 }
2882 return "?";
2883}
2884
2885static void ath10k_config_chan(struct ath10k *ar)
2886{
2887 struct ath10k_vif *arvif;
c930f744
MK
2888 int ret;
2889
2890 lockdep_assert_held(&ar->conf_mutex);
2891
7aa7a72a 2892 ath10k_dbg(ar, ATH10K_DBG_MAC,
c930f744
MK
2893 "mac config channel to %dMHz (cf1 %dMHz cf2 %dMHz width %s)\n",
2894 ar->chandef.chan->center_freq,
2895 ar->chandef.center_freq1,
2896 ar->chandef.center_freq2,
2897 chandef_get_width(ar->chandef.width));
2898
2899 /* First stop monitor interface. Some FW versions crash if there's a
2900 * lone monitor interface. */
1bbc0975 2901 if (ar->monitor_started)
1933747f 2902 ath10k_monitor_stop(ar);
c930f744
MK
2903
2904 list_for_each_entry(arvif, &ar->arvifs, list) {
2905 if (!arvif->is_started)
2906 continue;
2907
dc55e307
MK
2908 if (!arvif->is_up)
2909 continue;
2910
c930f744
MK
2911 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
2912 continue;
2913
dc55e307 2914 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
c930f744 2915 if (ret) {
7aa7a72a 2916 ath10k_warn(ar, "failed to down vdev %d: %d\n",
c930f744
MK
2917 arvif->vdev_id, ret);
2918 continue;
2919 }
2920 }
2921
dc55e307 2922 /* all vdevs are downed now - attempt to restart and re-up them */
c930f744
MK
2923
2924 list_for_each_entry(arvif, &ar->arvifs, list) {
2925 if (!arvif->is_started)
2926 continue;
2927
2928 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
2929 continue;
2930
dc55e307 2931 ret = ath10k_vdev_restart(arvif);
c930f744 2932 if (ret) {
7aa7a72a 2933 ath10k_warn(ar, "failed to restart vdev %d: %d\n",
c930f744
MK
2934 arvif->vdev_id, ret);
2935 continue;
2936 }
2937
2938 if (!arvif->is_up)
2939 continue;
2940
2941 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
2942 arvif->bssid);
2943 if (ret) {
7aa7a72a 2944 ath10k_warn(ar, "failed to bring vdev up %d: %d\n",
c930f744
MK
2945 arvif->vdev_id, ret);
2946 continue;
2947 }
2948 }
2949
1933747f 2950 ath10k_monitor_recalc(ar);
c930f744
MK
2951}
2952
7d9d5587
MK
2953static int ath10k_mac_txpower_setup(struct ath10k *ar, int txpower)
2954{
2955 int ret;
2956 u32 param;
2957
2958 lockdep_assert_held(&ar->conf_mutex);
2959
2960 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac txpower %d\n", txpower);
2961
2962 param = ar->wmi.pdev_param->txpower_limit2g;
2963 ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
2964 if (ret) {
2965 ath10k_warn(ar, "failed to set 2g txpower %d: %d\n",
2966 txpower, ret);
2967 return ret;
2968 }
2969
2970 param = ar->wmi.pdev_param->txpower_limit5g;
2971 ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
2972 if (ret) {
2973 ath10k_warn(ar, "failed to set 5g txpower %d: %d\n",
2974 txpower, ret);
2975 return ret;
2976 }
2977
2978 return 0;
2979}
2980
2981static int ath10k_mac_txpower_recalc(struct ath10k *ar)
2982{
2983 struct ath10k_vif *arvif;
2984 int ret, txpower = -1;
2985
2986 lockdep_assert_held(&ar->conf_mutex);
2987
2988 list_for_each_entry(arvif, &ar->arvifs, list) {
2989 WARN_ON(arvif->txpower < 0);
2990
2991 if (txpower == -1)
2992 txpower = arvif->txpower;
2993 else
2994 txpower = min(txpower, arvif->txpower);
2995 }
2996
2997 if (WARN_ON(txpower == -1))
2998 return -EINVAL;
2999
3000 ret = ath10k_mac_txpower_setup(ar, txpower);
3001 if (ret) {
3002 ath10k_warn(ar, "failed to setup tx power %d: %d\n",
3003 txpower, ret);
3004 return ret;
3005 }
3006
3007 return 0;
3008}
3009
affd3217
MK
3010static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
3011{
5e3dd157
KV
3012 struct ath10k *ar = hw->priv;
3013 struct ieee80211_conf *conf = &hw->conf;
3014 int ret = 0;
5e3dd157
KV
3015
3016 mutex_lock(&ar->conf_mutex);
3017
3018 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
7aa7a72a 3019 ath10k_dbg(ar, ATH10K_DBG_MAC,
d650097b 3020 "mac config channel %dMHz flags 0x%x radar %d\n",
e8a50f8b 3021 conf->chandef.chan->center_freq,
d650097b
MK
3022 conf->chandef.chan->flags,
3023 conf->radar_enabled);
e8a50f8b 3024
5e3dd157
KV
3025 spin_lock_bh(&ar->data_lock);
3026 ar->rx_channel = conf->chandef.chan;
3027 spin_unlock_bh(&ar->data_lock);
e8a50f8b 3028
d650097b
MK
3029 ar->radar_enabled = conf->radar_enabled;
3030 ath10k_recalc_radar_detection(ar);
c930f744
MK
3031
3032 if (!cfg80211_chandef_identical(&ar->chandef, &conf->chandef)) {
3033 ar->chandef = conf->chandef;
3034 ath10k_config_chan(ar);
3035 }
5e3dd157
KV
3036 }
3037
affd3217
MK
3038 if (changed & IEEE80211_CONF_CHANGE_PS)
3039 ath10k_config_ps(ar);
5e3dd157
KV
3040
3041 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
1933747f
MK
3042 ar->monitor = conf->flags & IEEE80211_CONF_MONITOR;
3043 ret = ath10k_monitor_recalc(ar);
3044 if (ret)
3045 ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
5e3dd157
KV
3046 }
3047
3048 mutex_unlock(&ar->conf_mutex);
3049 return ret;
3050}
3051
5572a95b
BG
3052static u32 get_nss_from_chainmask(u16 chain_mask)
3053{
3054 if ((chain_mask & 0x15) == 0x15)
3055 return 4;
3056 else if ((chain_mask & 0x7) == 0x7)
3057 return 3;
3058 else if ((chain_mask & 0x3) == 0x3)
3059 return 2;
3060 return 1;
3061}
3062
5e3dd157
KV
3063/*
3064 * TODO:
3065 * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
3066 * because we will send mgmt frames without CCK. This requirement
3067 * for P2P_FIND/GO_NEG should be handled by checking CCK flag
3068 * in the TX packet.
3069 */
3070static int ath10k_add_interface(struct ieee80211_hw *hw,
3071 struct ieee80211_vif *vif)
3072{
3073 struct ath10k *ar = hw->priv;
3074 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3075 enum wmi_sta_powersave_param param;
3076 int ret = 0;
5a13e76e 3077 u32 value;
5e3dd157 3078 int bit;
6d1506e7 3079 u32 vdev_param;
5e3dd157 3080
848955cc
JB
3081 vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
3082
5e3dd157
KV
3083 mutex_lock(&ar->conf_mutex);
3084
0dbd09e6
MK
3085 memset(arvif, 0, sizeof(*arvif));
3086
5e3dd157
KV
3087 arvif->ar = ar;
3088 arvif->vif = vif;
3089
e63b33f3 3090 INIT_LIST_HEAD(&arvif->list);
cc4827b9 3091
a9aefb3b 3092 if (ar->free_vdev_map == 0) {
7aa7a72a 3093 ath10k_warn(ar, "Free vdev map is empty, no more interfaces allowed.\n");
5e3dd157 3094 ret = -EBUSY;
9dad14ae 3095 goto err;
5e3dd157 3096 }
16c11176 3097 bit = __ffs64(ar->free_vdev_map);
5e3dd157 3098
16c11176
BG
3099 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac create vdev %i map %llx\n",
3100 bit, ar->free_vdev_map);
5e3dd157 3101
16c11176 3102 arvif->vdev_id = bit;
5e3dd157 3103 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
5e3dd157 3104
5e3dd157 3105 switch (vif->type) {
75d2bd48
MK
3106 case NL80211_IFTYPE_P2P_DEVICE:
3107 arvif->vdev_type = WMI_VDEV_TYPE_STA;
3108 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
3109 break;
5e3dd157
KV
3110 case NL80211_IFTYPE_UNSPECIFIED:
3111 case NL80211_IFTYPE_STATION:
3112 arvif->vdev_type = WMI_VDEV_TYPE_STA;
3113 if (vif->p2p)
3114 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
3115 break;
3116 case NL80211_IFTYPE_ADHOC:
3117 arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
3118 break;
3119 case NL80211_IFTYPE_AP:
3120 arvif->vdev_type = WMI_VDEV_TYPE_AP;
3121
3122 if (vif->p2p)
3123 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
3124 break;
3125 case NL80211_IFTYPE_MONITOR:
3126 arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
3127 break;
3128 default:
3129 WARN_ON(1);
3130 break;
3131 }
3132
64badcb6
MK
3133 /* Some firmware revisions don't wait for beacon tx completion before
3134 * sending another SWBA event. This could lead to hardware using old
3135 * (freed) beacon data in some cases, e.g. tx credit starvation
3136 * combined with missed TBTT. This is very very rare.
3137 *
3138 * On non-IOMMU-enabled hosts this could be a possible security issue
3139 * because hw could beacon some random data on the air. On
3140 * IOMMU-enabled hosts DMAR faults would occur in most cases and target
3141 * device would crash.
3142 *
3143 * Since there are no beacon tx completions (implicit nor explicit)
3144 * propagated to host the only workaround for this is to allocate a
3145 * DMA-coherent buffer for a lifetime of a vif and use it for all
3146 * beacon tx commands. Worst case for this approach is some beacons may
3147 * become corrupted, e.g. have garbled IEs or out-of-date TIM bitmap.
3148 */
3149 if (vif->type == NL80211_IFTYPE_ADHOC ||
3150 vif->type == NL80211_IFTYPE_AP) {
3151 arvif->beacon_buf = dma_zalloc_coherent(ar->dev,
3152 IEEE80211_MAX_FRAME_LEN,
3153 &arvif->beacon_paddr,
82d7aba7 3154 GFP_ATOMIC);
64badcb6
MK
3155 if (!arvif->beacon_buf) {
3156 ret = -ENOMEM;
3157 ath10k_warn(ar, "failed to allocate beacon buffer: %d\n",
3158 ret);
3159 goto err;
3160 }
3161 }
3162
3163 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d bcnmode %s\n",
3164 arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
3165 arvif->beacon_buf ? "single-buf" : "per-skb");
5e3dd157
KV
3166
3167 ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
3168 arvif->vdev_subtype, vif->addr);
3169 if (ret) {
7aa7a72a 3170 ath10k_warn(ar, "failed to create WMI vdev %i: %d\n",
69244e56 3171 arvif->vdev_id, ret);
9dad14ae 3172 goto err;
5e3dd157
KV
3173 }
3174
16c11176 3175 ar->free_vdev_map &= ~(1LL << arvif->vdev_id);
0579119f 3176 list_add(&arvif->list, &ar->arvifs);
9dad14ae 3177
46725b15
MK
3178 /* It makes no sense to have firmware do keepalives. mac80211 already
3179 * takes care of this with idle connection polling.
3180 */
3181 ret = ath10k_mac_vif_disable_keepalive(arvif);
9dad14ae 3182 if (ret) {
46725b15 3183 ath10k_warn(ar, "failed to disable keepalive on vdev %i: %d\n",
69244e56 3184 arvif->vdev_id, ret);
9dad14ae
MK
3185 goto err_vdev_delete;
3186 }
5e3dd157 3187
627613f8 3188 arvif->def_wep_key_idx = -1;
5e3dd157 3189
6d1506e7
BM
3190 vdev_param = ar->wmi.vdev_param->tx_encap_type;
3191 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5e3dd157 3192 ATH10K_HW_TXRX_NATIVE_WIFI);
ebc9abdd 3193 /* 10.X firmware does not support this VDEV parameter. Do not warn */
9dad14ae 3194 if (ret && ret != -EOPNOTSUPP) {
7aa7a72a 3195 ath10k_warn(ar, "failed to set vdev %i TX encapsulation: %d\n",
69244e56 3196 arvif->vdev_id, ret);
9dad14ae
MK
3197 goto err_vdev_delete;
3198 }
5e3dd157 3199
5572a95b
BG
3200 if (ar->cfg_tx_chainmask) {
3201 u16 nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
3202
3203 vdev_param = ar->wmi.vdev_param->nss;
3204 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3205 nss);
3206 if (ret) {
3207 ath10k_warn(ar, "failed to set vdev %i chainmask 0x%x, nss %i: %d\n",
3208 arvif->vdev_id, ar->cfg_tx_chainmask, nss,
3209 ret);
3210 goto err_vdev_delete;
3211 }
3212 }
3213
5e3dd157
KV
3214 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3215 ret = ath10k_peer_create(ar, arvif->vdev_id, vif->addr);
3216 if (ret) {
7aa7a72a 3217 ath10k_warn(ar, "failed to create vdev %i peer for AP: %d\n",
69244e56 3218 arvif->vdev_id, ret);
9dad14ae 3219 goto err_vdev_delete;
5e3dd157 3220 }
cdf07409 3221
5a13e76e
KV
3222 ret = ath10k_mac_set_kickout(arvif);
3223 if (ret) {
7aa7a72a 3224 ath10k_warn(ar, "failed to set vdev %i kickout parameters: %d\n",
69244e56 3225 arvif->vdev_id, ret);
5a13e76e
KV
3226 goto err_peer_delete;
3227 }
5e3dd157
KV
3228 }
3229
3230 if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
3231 param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
3232 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
3233 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3234 param, value);
9dad14ae 3235 if (ret) {
7aa7a72a 3236 ath10k_warn(ar, "failed to set vdev %i RX wake policy: %d\n",
69244e56 3237 arvif->vdev_id, ret);
9dad14ae
MK
3238 goto err_peer_delete;
3239 }
5e3dd157 3240
9f9b5746 3241 ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
9dad14ae 3242 if (ret) {
9f9b5746 3243 ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
69244e56 3244 arvif->vdev_id, ret);
9dad14ae
MK
3245 goto err_peer_delete;
3246 }
5e3dd157 3247
9f9b5746 3248 ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
9dad14ae 3249 if (ret) {
9f9b5746 3250 ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
69244e56 3251 arvif->vdev_id, ret);
9dad14ae
MK
3252 goto err_peer_delete;
3253 }
5e3dd157
KV
3254 }
3255
424121c3 3256 ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
9dad14ae 3257 if (ret) {
7aa7a72a 3258 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
679c54a6 3259 arvif->vdev_id, ret);
9dad14ae
MK
3260 goto err_peer_delete;
3261 }
679c54a6 3262
424121c3 3263 ret = ath10k_mac_set_frag(arvif, ar->hw->wiphy->frag_threshold);
9dad14ae 3264 if (ret) {
7aa7a72a 3265 ath10k_warn(ar, "failed to set frag threshold for vdev %d: %d\n",
679c54a6 3266 arvif->vdev_id, ret);
9dad14ae
MK
3267 goto err_peer_delete;
3268 }
679c54a6 3269
7d9d5587
MK
3270 arvif->txpower = vif->bss_conf.txpower;
3271 ret = ath10k_mac_txpower_recalc(ar);
3272 if (ret) {
3273 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
3274 goto err_peer_delete;
3275 }
3276
5e3dd157 3277 mutex_unlock(&ar->conf_mutex);
9dad14ae
MK
3278 return 0;
3279
3280err_peer_delete:
3281 if (arvif->vdev_type == WMI_VDEV_TYPE_AP)
3282 ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
3283
3284err_vdev_delete:
3285 ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
16c11176 3286 ar->free_vdev_map |= 1LL << arvif->vdev_id;
0579119f 3287 list_del(&arvif->list);
9dad14ae
MK
3288
3289err:
64badcb6
MK
3290 if (arvif->beacon_buf) {
3291 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
3292 arvif->beacon_buf, arvif->beacon_paddr);
3293 arvif->beacon_buf = NULL;
3294 }
3295
9dad14ae
MK
3296 mutex_unlock(&ar->conf_mutex);
3297
5e3dd157
KV
3298 return ret;
3299}
3300
3301static void ath10k_remove_interface(struct ieee80211_hw *hw,
3302 struct ieee80211_vif *vif)
3303{
3304 struct ath10k *ar = hw->priv;
3305 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3306 int ret;
3307
5d011f5c
SM
3308 mutex_lock(&ar->conf_mutex);
3309
ed54388a 3310 spin_lock_bh(&ar->data_lock);
64badcb6 3311 ath10k_mac_vif_beacon_cleanup(arvif);
ed54388a
MK
3312 spin_unlock_bh(&ar->data_lock);
3313
855aed12
SW
3314 ret = ath10k_spectral_vif_stop(arvif);
3315 if (ret)
7aa7a72a 3316 ath10k_warn(ar, "failed to stop spectral for vdev %i: %d\n",
855aed12
SW
3317 arvif->vdev_id, ret);
3318
16c11176 3319 ar->free_vdev_map |= 1LL << arvif->vdev_id;
0579119f 3320 list_del(&arvif->list);
5e3dd157
KV
3321
3322 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3323 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, vif->addr);
3324 if (ret)
7aa7a72a 3325 ath10k_warn(ar, "failed to remove peer for AP vdev %i: %d\n",
69244e56 3326 arvif->vdev_id, ret);
5e3dd157
KV
3327
3328 kfree(arvif->u.ap.noa_data);
3329 }
3330
7aa7a72a 3331 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i delete (remove interface)\n",
60c3daa8
KV
3332 arvif->vdev_id);
3333
5e3dd157
KV
3334 ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
3335 if (ret)
7aa7a72a 3336 ath10k_warn(ar, "failed to delete WMI vdev %i: %d\n",
69244e56 3337 arvif->vdev_id, ret);
5e3dd157 3338
5e3dd157
KV
3339 ath10k_peer_cleanup(ar, arvif->vdev_id);
3340
3341 mutex_unlock(&ar->conf_mutex);
3342}
3343
3344/*
3345 * FIXME: Has to be verified.
3346 */
3347#define SUPPORTED_FILTERS \
3348 (FIF_PROMISC_IN_BSS | \
3349 FIF_ALLMULTI | \
3350 FIF_CONTROL | \
3351 FIF_PSPOLL | \
3352 FIF_OTHER_BSS | \
3353 FIF_BCN_PRBRESP_PROMISC | \
3354 FIF_PROBE_REQ | \
3355 FIF_FCSFAIL)
3356
3357static void ath10k_configure_filter(struct ieee80211_hw *hw,
3358 unsigned int changed_flags,
3359 unsigned int *total_flags,
3360 u64 multicast)
3361{
3362 struct ath10k *ar = hw->priv;
3363 int ret;
3364
3365 mutex_lock(&ar->conf_mutex);
3366
3367 changed_flags &= SUPPORTED_FILTERS;
3368 *total_flags &= SUPPORTED_FILTERS;
3369 ar->filter_flags = *total_flags;
3370
1933747f
MK
3371 ret = ath10k_monitor_recalc(ar);
3372 if (ret)
3373 ath10k_warn(ar, "failed to recalc montior: %d\n", ret);
5e3dd157
KV
3374
3375 mutex_unlock(&ar->conf_mutex);
3376}
3377
3378static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
3379 struct ieee80211_vif *vif,
3380 struct ieee80211_bss_conf *info,
3381 u32 changed)
3382{
3383 struct ath10k *ar = hw->priv;
3384 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3385 int ret = 0;
af762c0b 3386 u32 vdev_param, pdev_param, slottime, preamble;
5e3dd157
KV
3387
3388 mutex_lock(&ar->conf_mutex);
3389
3390 if (changed & BSS_CHANGED_IBSS)
3391 ath10k_control_ibss(arvif, info, vif->addr);
3392
3393 if (changed & BSS_CHANGED_BEACON_INT) {
3394 arvif->beacon_interval = info->beacon_int;
6d1506e7
BM
3395 vdev_param = ar->wmi.vdev_param->beacon_interval;
3396 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5e3dd157 3397 arvif->beacon_interval);
7aa7a72a 3398 ath10k_dbg(ar, ATH10K_DBG_MAC,
60c3daa8
KV
3399 "mac vdev %d beacon_interval %d\n",
3400 arvif->vdev_id, arvif->beacon_interval);
3401
5e3dd157 3402 if (ret)
7aa7a72a 3403 ath10k_warn(ar, "failed to set beacon interval for vdev %d: %i\n",
69244e56 3404 arvif->vdev_id, ret);
5e3dd157
KV
3405 }
3406
3407 if (changed & BSS_CHANGED_BEACON) {
7aa7a72a 3408 ath10k_dbg(ar, ATH10K_DBG_MAC,
60c3daa8
KV
3409 "vdev %d set beacon tx mode to staggered\n",
3410 arvif->vdev_id);
3411
226a339b
BM
3412 pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
3413 ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
5e3dd157
KV
3414 WMI_BEACON_STAGGERED_MODE);
3415 if (ret)
7aa7a72a 3416 ath10k_warn(ar, "failed to set beacon mode for vdev %d: %i\n",
69244e56 3417 arvif->vdev_id, ret);
fbb8f1b7
MK
3418
3419 ret = ath10k_mac_setup_bcn_tmpl(arvif);
3420 if (ret)
3421 ath10k_warn(ar, "failed to update beacon template: %d\n",
3422 ret);
3423 }
3424
3425 if (changed & BSS_CHANGED_AP_PROBE_RESP) {
3426 ret = ath10k_mac_setup_prb_tmpl(arvif);
3427 if (ret)
3428 ath10k_warn(ar, "failed to setup probe resp template on vdev %i: %d\n",
3429 arvif->vdev_id, ret);
5e3dd157
KV
3430 }
3431
ba2479fe 3432 if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
5e3dd157
KV
3433 arvif->dtim_period = info->dtim_period;
3434
7aa7a72a 3435 ath10k_dbg(ar, ATH10K_DBG_MAC,
60c3daa8
KV
3436 "mac vdev %d dtim_period %d\n",
3437 arvif->vdev_id, arvif->dtim_period);
3438
6d1506e7
BM
3439 vdev_param = ar->wmi.vdev_param->dtim_period;
3440 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5e3dd157
KV
3441 arvif->dtim_period);
3442 if (ret)
7aa7a72a 3443 ath10k_warn(ar, "failed to set dtim period for vdev %d: %i\n",
69244e56 3444 arvif->vdev_id, ret);
5e3dd157
KV
3445 }
3446
3447 if (changed & BSS_CHANGED_SSID &&
3448 vif->type == NL80211_IFTYPE_AP) {
3449 arvif->u.ap.ssid_len = info->ssid_len;
3450 if (info->ssid_len)
3451 memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
3452 arvif->u.ap.hidden_ssid = info->hidden_ssid;
3453 }
3454
077efc8c
MK
3455 if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
3456 ether_addr_copy(arvif->bssid, info->bssid);
5e3dd157
KV
3457
3458 if (changed & BSS_CHANGED_BEACON_ENABLED)
3459 ath10k_control_beaconing(arvif, info);
3460
3461 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
e81bd104 3462 arvif->use_cts_prot = info->use_cts_prot;
7aa7a72a 3463 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d cts_prot %d\n",
e81bd104 3464 arvif->vdev_id, info->use_cts_prot);
60c3daa8 3465
e81bd104 3466 ret = ath10k_recalc_rtscts_prot(arvif);
5e3dd157 3467 if (ret)
7aa7a72a 3468 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
69244e56 3469 arvif->vdev_id, ret);
5e3dd157
KV
3470 }
3471
3472 if (changed & BSS_CHANGED_ERP_SLOT) {
5e3dd157
KV
3473 if (info->use_short_slot)
3474 slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
3475
3476 else
3477 slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
3478
7aa7a72a 3479 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
60c3daa8
KV
3480 arvif->vdev_id, slottime);
3481
6d1506e7
BM
3482 vdev_param = ar->wmi.vdev_param->slot_time;
3483 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5e3dd157
KV
3484 slottime);
3485 if (ret)
7aa7a72a 3486 ath10k_warn(ar, "failed to set erp slot for vdev %d: %i\n",
69244e56 3487 arvif->vdev_id, ret);
5e3dd157
KV
3488 }
3489
3490 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
5e3dd157
KV
3491 if (info->use_short_preamble)
3492 preamble = WMI_VDEV_PREAMBLE_SHORT;
3493 else
3494 preamble = WMI_VDEV_PREAMBLE_LONG;
3495
7aa7a72a 3496 ath10k_dbg(ar, ATH10K_DBG_MAC,
60c3daa8
KV
3497 "mac vdev %d preamble %dn",
3498 arvif->vdev_id, preamble);
3499
6d1506e7
BM
3500 vdev_param = ar->wmi.vdev_param->preamble;
3501 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5e3dd157
KV
3502 preamble);
3503 if (ret)
7aa7a72a 3504 ath10k_warn(ar, "failed to set preamble for vdev %d: %i\n",
69244e56 3505 arvif->vdev_id, ret);
5e3dd157
KV
3506 }
3507
3508 if (changed & BSS_CHANGED_ASSOC) {
e556f111
MK
3509 if (info->assoc) {
3510 /* Workaround: Make sure monitor vdev is not running
3511 * when associating to prevent some firmware revisions
3512 * (e.g. 10.1 and 10.2) from crashing.
3513 */
3514 if (ar->monitor_started)
3515 ath10k_monitor_stop(ar);
5e3dd157 3516 ath10k_bss_assoc(hw, vif, info);
e556f111 3517 ath10k_monitor_recalc(ar);
077efc8c
MK
3518 } else {
3519 ath10k_bss_disassoc(hw, vif);
e556f111 3520 }
5e3dd157
KV
3521 }
3522
7d9d5587
MK
3523 if (changed & BSS_CHANGED_TXPOWER) {
3524 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev_id %i txpower %d\n",
3525 arvif->vdev_id, info->txpower);
3526
3527 arvif->txpower = info->txpower;
3528 ret = ath10k_mac_txpower_recalc(ar);
3529 if (ret)
3530 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
3531 }
3532
bf14e65c
MK
3533 if (changed & BSS_CHANGED_PS) {
3534 ret = ath10k_mac_vif_setup_ps(arvif);
3535 if (ret)
3536 ath10k_warn(ar, "failed to setup ps on vdev %i: %d\n",
3537 arvif->vdev_id, ret);
3538 }
3539
5e3dd157
KV
3540 mutex_unlock(&ar->conf_mutex);
3541}
3542
3543static int ath10k_hw_scan(struct ieee80211_hw *hw,
3544 struct ieee80211_vif *vif,
c56ef672 3545 struct ieee80211_scan_request *hw_req)
5e3dd157
KV
3546{
3547 struct ath10k *ar = hw->priv;
3548 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
c56ef672 3549 struct cfg80211_scan_request *req = &hw_req->req;
5e3dd157
KV
3550 struct wmi_start_scan_arg arg;
3551 int ret = 0;
3552 int i;
3553
3554 mutex_lock(&ar->conf_mutex);
3555
3556 spin_lock_bh(&ar->data_lock);
5c81c7fd
MK
3557 switch (ar->scan.state) {
3558 case ATH10K_SCAN_IDLE:
3559 reinit_completion(&ar->scan.started);
3560 reinit_completion(&ar->scan.completed);
3561 ar->scan.state = ATH10K_SCAN_STARTING;
3562 ar->scan.is_roc = false;
3563 ar->scan.vdev_id = arvif->vdev_id;
3564 ret = 0;
3565 break;
3566 case ATH10K_SCAN_STARTING:
3567 case ATH10K_SCAN_RUNNING:
3568 case ATH10K_SCAN_ABORTING:
5e3dd157 3569 ret = -EBUSY;
5c81c7fd 3570 break;
5e3dd157 3571 }
5e3dd157
KV
3572 spin_unlock_bh(&ar->data_lock);
3573
5c81c7fd
MK
3574 if (ret)
3575 goto exit;
3576
5e3dd157
KV
3577 memset(&arg, 0, sizeof(arg));
3578 ath10k_wmi_start_scan_init(ar, &arg);
3579 arg.vdev_id = arvif->vdev_id;
3580 arg.scan_id = ATH10K_SCAN_ID;
3581
3582 if (!req->no_cck)
3583 arg.scan_ctrl_flags |= WMI_SCAN_ADD_CCK_RATES;
3584
3585 if (req->ie_len) {
3586 arg.ie_len = req->ie_len;
3587 memcpy(arg.ie, req->ie, arg.ie_len);
3588 }
3589
3590 if (req->n_ssids) {
3591 arg.n_ssids = req->n_ssids;
3592 for (i = 0; i < arg.n_ssids; i++) {
3593 arg.ssids[i].len = req->ssids[i].ssid_len;
3594 arg.ssids[i].ssid = req->ssids[i].ssid;
3595 }
dcd4a561
MK
3596 } else {
3597 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
5e3dd157
KV
3598 }
3599
3600 if (req->n_channels) {
3601 arg.n_channels = req->n_channels;
3602 for (i = 0; i < arg.n_channels; i++)
3603 arg.channels[i] = req->channels[i]->center_freq;
3604 }
3605
3606 ret = ath10k_start_scan(ar, &arg);
3607 if (ret) {
7aa7a72a 3608 ath10k_warn(ar, "failed to start hw scan: %d\n", ret);
5e3dd157 3609 spin_lock_bh(&ar->data_lock);
5c81c7fd 3610 ar->scan.state = ATH10K_SCAN_IDLE;
5e3dd157
KV
3611 spin_unlock_bh(&ar->data_lock);
3612 }
3613
3614exit:
3615 mutex_unlock(&ar->conf_mutex);
3616 return ret;
3617}
3618
3619static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
3620 struct ieee80211_vif *vif)
3621{
3622 struct ath10k *ar = hw->priv;
5e3dd157
KV
3623
3624 mutex_lock(&ar->conf_mutex);
5c81c7fd 3625 ath10k_scan_abort(ar);
5e3dd157 3626 mutex_unlock(&ar->conf_mutex);
4eb2e164
MK
3627
3628 cancel_delayed_work_sync(&ar->scan.timeout);
5e3dd157
KV
3629}
3630
cfb27d29
MK
3631static void ath10k_set_key_h_def_keyidx(struct ath10k *ar,
3632 struct ath10k_vif *arvif,
3633 enum set_key_cmd cmd,
3634 struct ieee80211_key_conf *key)
3635{
3636 u32 vdev_param = arvif->ar->wmi.vdev_param->def_keyid;
3637 int ret;
3638
3639 /* 10.1 firmware branch requires default key index to be set to group
3640 * key index after installing it. Otherwise FW/HW Txes corrupted
3641 * frames with multi-vif APs. This is not required for main firmware
3642 * branch (e.g. 636).
3643 *
3644 * FIXME: This has been tested only in AP. It remains unknown if this
3645 * is required for multi-vif STA interfaces on 10.1 */
3646
3647 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
3648 return;
3649
3650 if (key->cipher == WLAN_CIPHER_SUITE_WEP40)
3651 return;
3652
3653 if (key->cipher == WLAN_CIPHER_SUITE_WEP104)
3654 return;
3655
3656 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3657 return;
3658
3659 if (cmd != SET_KEY)
3660 return;
3661
3662 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3663 key->keyidx);
3664 if (ret)
7aa7a72a 3665 ath10k_warn(ar, "failed to set vdev %i group key as default key: %d\n",
69244e56 3666 arvif->vdev_id, ret);
cfb27d29
MK
3667}
3668
5e3dd157
KV
3669static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
3670 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
3671 struct ieee80211_key_conf *key)
3672{
3673 struct ath10k *ar = hw->priv;
3674 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3675 struct ath10k_peer *peer;
3676 const u8 *peer_addr;
3677 bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3678 key->cipher == WLAN_CIPHER_SUITE_WEP104;
627613f8 3679 bool def_idx = false;
5e3dd157
KV
3680 int ret = 0;
3681
3682 if (key->keyidx > WMI_MAX_KEY_INDEX)
3683 return -ENOSPC;
3684
3685 mutex_lock(&ar->conf_mutex);
3686
3687 if (sta)
3688 peer_addr = sta->addr;
3689 else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
3690 peer_addr = vif->bss_conf.bssid;
3691 else
3692 peer_addr = vif->addr;
3693
3694 key->hw_key_idx = key->keyidx;
3695
3696 /* the peer should not disappear in mid-way (unless FW goes awry) since
3697 * we already hold conf_mutex. we just make sure its there now. */
3698 spin_lock_bh(&ar->data_lock);
3699 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
3700 spin_unlock_bh(&ar->data_lock);
3701
3702 if (!peer) {
3703 if (cmd == SET_KEY) {
7aa7a72a 3704 ath10k_warn(ar, "failed to install key for non-existent peer %pM\n",
5e3dd157
KV
3705 peer_addr);
3706 ret = -EOPNOTSUPP;
3707 goto exit;
3708 } else {
3709 /* if the peer doesn't exist there is no key to disable
3710 * anymore */
3711 goto exit;
3712 }
3713 }
3714
3715 if (is_wep) {
3716 if (cmd == SET_KEY)
3717 arvif->wep_keys[key->keyidx] = key;
3718 else
3719 arvif->wep_keys[key->keyidx] = NULL;
3720
3721 if (cmd == DISABLE_KEY)
3722 ath10k_clear_vdev_key(arvif, key);
3723 }
3724
627613f8
SJ
3725 /* set TX_USAGE flag for all the keys incase of dot1x-WEP. For
3726 * static WEP, do not set this flag for the keys whose key id
3727 * is greater than default key id.
3728 */
3729 if (arvif->def_wep_key_idx == -1)
3730 def_idx = true;
3731
3732 ret = ath10k_install_key(arvif, key, cmd, peer_addr, def_idx);
5e3dd157 3733 if (ret) {
7aa7a72a 3734 ath10k_warn(ar, "failed to install key for vdev %i peer %pM: %d\n",
69244e56 3735 arvif->vdev_id, peer_addr, ret);
5e3dd157
KV
3736 goto exit;
3737 }
3738
cfb27d29
MK
3739 ath10k_set_key_h_def_keyidx(ar, arvif, cmd, key);
3740
5e3dd157
KV
3741 spin_lock_bh(&ar->data_lock);
3742 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
3743 if (peer && cmd == SET_KEY)
3744 peer->keys[key->keyidx] = key;
3745 else if (peer && cmd == DISABLE_KEY)
3746 peer->keys[key->keyidx] = NULL;
3747 else if (peer == NULL)
3748 /* impossible unless FW goes crazy */
7aa7a72a 3749 ath10k_warn(ar, "Peer %pM disappeared!\n", peer_addr);
5e3dd157
KV
3750 spin_unlock_bh(&ar->data_lock);
3751
3752exit:
3753 mutex_unlock(&ar->conf_mutex);
3754 return ret;
3755}
3756
627613f8
SJ
3757static void ath10k_set_default_unicast_key(struct ieee80211_hw *hw,
3758 struct ieee80211_vif *vif,
3759 int keyidx)
3760{
3761 struct ath10k *ar = hw->priv;
3762 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3763 int ret;
3764
3765 mutex_lock(&arvif->ar->conf_mutex);
3766
3767 if (arvif->ar->state != ATH10K_STATE_ON)
3768 goto unlock;
3769
3770 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
3771 arvif->vdev_id, keyidx);
3772
3773 ret = ath10k_wmi_vdev_set_param(arvif->ar,
3774 arvif->vdev_id,
3775 arvif->ar->wmi.vdev_param->def_keyid,
3776 keyidx);
3777
3778 if (ret) {
3779 ath10k_warn(ar, "failed to update wep key index for vdev %d: %d\n",
3780 arvif->vdev_id,
3781 ret);
3782 goto unlock;
3783 }
3784
3785 arvif->def_wep_key_idx = keyidx;
3786unlock:
3787 mutex_unlock(&arvif->ar->conf_mutex);
3788}
3789
9797febc
MK
3790static void ath10k_sta_rc_update_wk(struct work_struct *wk)
3791{
3792 struct ath10k *ar;
3793 struct ath10k_vif *arvif;
3794 struct ath10k_sta *arsta;
3795 struct ieee80211_sta *sta;
3796 u32 changed, bw, nss, smps;
3797 int err;
3798
3799 arsta = container_of(wk, struct ath10k_sta, update_wk);
3800 sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
3801 arvif = arsta->arvif;
3802 ar = arvif->ar;
3803
3804 spin_lock_bh(&ar->data_lock);
3805
3806 changed = arsta->changed;
3807 arsta->changed = 0;
3808
3809 bw = arsta->bw;
3810 nss = arsta->nss;
3811 smps = arsta->smps;
3812
3813 spin_unlock_bh(&ar->data_lock);
3814
3815 mutex_lock(&ar->conf_mutex);
3816
3817 if (changed & IEEE80211_RC_BW_CHANGED) {
7aa7a72a 3818 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM peer bw %d\n",
9797febc
MK
3819 sta->addr, bw);
3820
3821 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
3822 WMI_PEER_CHAN_WIDTH, bw);
3823 if (err)
7aa7a72a 3824 ath10k_warn(ar, "failed to update STA %pM peer bw %d: %d\n",
9797febc
MK
3825 sta->addr, bw, err);
3826 }
3827
3828 if (changed & IEEE80211_RC_NSS_CHANGED) {
7aa7a72a 3829 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM nss %d\n",
9797febc
MK
3830 sta->addr, nss);
3831
3832 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
3833 WMI_PEER_NSS, nss);
3834 if (err)
7aa7a72a 3835 ath10k_warn(ar, "failed to update STA %pM nss %d: %d\n",
9797febc
MK
3836 sta->addr, nss, err);
3837 }
3838
3839 if (changed & IEEE80211_RC_SMPS_CHANGED) {
7aa7a72a 3840 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM smps %d\n",
9797febc
MK
3841 sta->addr, smps);
3842
3843 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
3844 WMI_PEER_SMPS_STATE, smps);
3845 if (err)
7aa7a72a 3846 ath10k_warn(ar, "failed to update STA %pM smps %d: %d\n",
9797febc
MK
3847 sta->addr, smps, err);
3848 }
3849
55884c04
JD
3850 if (changed & IEEE80211_RC_SUPP_RATES_CHANGED ||
3851 changed & IEEE80211_RC_NSS_CHANGED) {
3852 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM supp rates/nss\n",
44d6fa90
CYY
3853 sta->addr);
3854
590922a8 3855 err = ath10k_station_assoc(ar, arvif->vif, sta, true);
44d6fa90 3856 if (err)
7aa7a72a 3857 ath10k_warn(ar, "failed to reassociate station: %pM\n",
44d6fa90
CYY
3858 sta->addr);
3859 }
3860
9797febc
MK
3861 mutex_unlock(&ar->conf_mutex);
3862}
3863
cfd1061e
MK
3864static int ath10k_mac_inc_num_stations(struct ath10k_vif *arvif)
3865{
3866 struct ath10k *ar = arvif->ar;
3867
3868 lockdep_assert_held(&ar->conf_mutex);
3869
3870 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
3871 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
3872 return 0;
3873
3874 if (ar->num_stations >= ar->max_num_stations)
3875 return -ENOBUFS;
3876
3877 ar->num_stations++;
3878
3879 return 0;
3880}
3881
3882static void ath10k_mac_dec_num_stations(struct ath10k_vif *arvif)
3883{
3884 struct ath10k *ar = arvif->ar;
3885
3886 lockdep_assert_held(&ar->conf_mutex);
3887
3888 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
3889 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
3890 return;
3891
3892 ar->num_stations--;
3893}
3894
5e3dd157
KV
3895static int ath10k_sta_state(struct ieee80211_hw *hw,
3896 struct ieee80211_vif *vif,
3897 struct ieee80211_sta *sta,
3898 enum ieee80211_sta_state old_state,
3899 enum ieee80211_sta_state new_state)
3900{
3901 struct ath10k *ar = hw->priv;
3902 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
9797febc 3903 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
5e3dd157
KV
3904 int ret = 0;
3905
76f90024
MK
3906 if (old_state == IEEE80211_STA_NOTEXIST &&
3907 new_state == IEEE80211_STA_NONE) {
3908 memset(arsta, 0, sizeof(*arsta));
3909 arsta->arvif = arvif;
3910 INIT_WORK(&arsta->update_wk, ath10k_sta_rc_update_wk);
3911 }
3912
9797febc
MK
3913 /* cancel must be done outside the mutex to avoid deadlock */
3914 if ((old_state == IEEE80211_STA_NONE &&
3915 new_state == IEEE80211_STA_NOTEXIST))
3916 cancel_work_sync(&arsta->update_wk);
3917
5e3dd157
KV
3918 mutex_lock(&ar->conf_mutex);
3919
3920 if (old_state == IEEE80211_STA_NOTEXIST &&
077efc8c 3921 new_state == IEEE80211_STA_NONE) {
5e3dd157
KV
3922 /*
3923 * New station addition.
3924 */
cfd1061e
MK
3925 ath10k_dbg(ar, ATH10K_DBG_MAC,
3926 "mac vdev %d peer create %pM (new sta) sta %d / %d peer %d / %d\n",
3927 arvif->vdev_id, sta->addr,
3928 ar->num_stations + 1, ar->max_num_stations,
3929 ar->num_peers + 1, ar->max_num_peers);
0e759f36 3930
cfd1061e
MK
3931 ret = ath10k_mac_inc_num_stations(arvif);
3932 if (ret) {
3933 ath10k_warn(ar, "refusing to associate station: too many connected already (%d)\n",
3934 ar->max_num_stations);
0e759f36
BM
3935 goto exit;
3936 }
3937
5e3dd157 3938 ret = ath10k_peer_create(ar, arvif->vdev_id, sta->addr);
a52c0282 3939 if (ret) {
7aa7a72a 3940 ath10k_warn(ar, "failed to add peer %pM for vdev %d when adding a new sta: %i\n",
479398b0 3941 sta->addr, arvif->vdev_id, ret);
cfd1061e 3942 ath10k_mac_dec_num_stations(arvif);
a52c0282
MK
3943 goto exit;
3944 }
077efc8c
MK
3945
3946 if (vif->type == NL80211_IFTYPE_STATION) {
3947 WARN_ON(arvif->is_started);
3948
3949 ret = ath10k_vdev_start(arvif);
3950 if (ret) {
3951 ath10k_warn(ar, "failed to start vdev %i: %d\n",
3952 arvif->vdev_id, ret);
3953 WARN_ON(ath10k_peer_delete(ar, arvif->vdev_id,
3954 sta->addr));
cfd1061e 3955 ath10k_mac_dec_num_stations(arvif);
077efc8c
MK
3956 goto exit;
3957 }
3958
3959 arvif->is_started = true;
3960 }
5e3dd157
KV
3961 } else if ((old_state == IEEE80211_STA_NONE &&
3962 new_state == IEEE80211_STA_NOTEXIST)) {
3963 /*
3964 * Existing station deletion.
3965 */
7aa7a72a 3966 ath10k_dbg(ar, ATH10K_DBG_MAC,
60c3daa8
KV
3967 "mac vdev %d peer delete %pM (sta gone)\n",
3968 arvif->vdev_id, sta->addr);
077efc8c
MK
3969
3970 if (vif->type == NL80211_IFTYPE_STATION) {
3971 WARN_ON(!arvif->is_started);
3972
3973 ret = ath10k_vdev_stop(arvif);
3974 if (ret)
3975 ath10k_warn(ar, "failed to stop vdev %i: %d\n",
3976 arvif->vdev_id, ret);
3977
3978 arvif->is_started = false;
3979 }
3980
5e3dd157
KV
3981 ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
3982 if (ret)
7aa7a72a 3983 ath10k_warn(ar, "failed to delete peer %pM for vdev %d: %i\n",
69244e56 3984 sta->addr, arvif->vdev_id, ret);
5e3dd157 3985
cfd1061e 3986 ath10k_mac_dec_num_stations(arvif);
5e3dd157
KV
3987 } else if (old_state == IEEE80211_STA_AUTH &&
3988 new_state == IEEE80211_STA_ASSOC &&
3989 (vif->type == NL80211_IFTYPE_AP ||
3990 vif->type == NL80211_IFTYPE_ADHOC)) {
3991 /*
3992 * New association.
3993 */
7aa7a72a 3994 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM associated\n",
60c3daa8
KV
3995 sta->addr);
3996
590922a8 3997 ret = ath10k_station_assoc(ar, vif, sta, false);
5e3dd157 3998 if (ret)
7aa7a72a 3999 ath10k_warn(ar, "failed to associate station %pM for vdev %i: %i\n",
69244e56 4000 sta->addr, arvif->vdev_id, ret);
5e3dd157
KV
4001 } else if (old_state == IEEE80211_STA_ASSOC &&
4002 new_state == IEEE80211_STA_AUTH &&
4003 (vif->type == NL80211_IFTYPE_AP ||
4004 vif->type == NL80211_IFTYPE_ADHOC)) {
4005 /*
4006 * Disassociation.
4007 */
7aa7a72a 4008 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM disassociated\n",
60c3daa8
KV
4009 sta->addr);
4010
590922a8 4011 ret = ath10k_station_disassoc(ar, vif, sta);
5e3dd157 4012 if (ret)
7aa7a72a 4013 ath10k_warn(ar, "failed to disassociate station: %pM vdev %i: %i\n",
69244e56 4014 sta->addr, arvif->vdev_id, ret);
5e3dd157 4015 }
0e759f36 4016exit:
5e3dd157
KV
4017 mutex_unlock(&ar->conf_mutex);
4018 return ret;
4019}
4020
4021static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
5b07e07f 4022 u16 ac, bool enable)
5e3dd157
KV
4023{
4024 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
b0e56154
MK
4025 struct wmi_sta_uapsd_auto_trig_arg arg = {};
4026 u32 prio = 0, acc = 0;
5e3dd157
KV
4027 u32 value = 0;
4028 int ret = 0;
4029
548db54c
MK
4030 lockdep_assert_held(&ar->conf_mutex);
4031
5e3dd157
KV
4032 if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
4033 return 0;
4034
4035 switch (ac) {
4036 case IEEE80211_AC_VO:
4037 value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
4038 WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
b0e56154
MK
4039 prio = 7;
4040 acc = 3;
5e3dd157
KV
4041 break;
4042 case IEEE80211_AC_VI:
4043 value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
4044 WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
b0e56154
MK
4045 prio = 5;
4046 acc = 2;
5e3dd157
KV
4047 break;
4048 case IEEE80211_AC_BE:
4049 value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
4050 WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
b0e56154
MK
4051 prio = 2;
4052 acc = 1;
5e3dd157
KV
4053 break;
4054 case IEEE80211_AC_BK:
4055 value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
4056 WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
b0e56154
MK
4057 prio = 0;
4058 acc = 0;
5e3dd157
KV
4059 break;
4060 }
4061
4062 if (enable)
4063 arvif->u.sta.uapsd |= value;
4064 else
4065 arvif->u.sta.uapsd &= ~value;
4066
4067 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4068 WMI_STA_PS_PARAM_UAPSD,
4069 arvif->u.sta.uapsd);
4070 if (ret) {
7aa7a72a 4071 ath10k_warn(ar, "failed to set uapsd params: %d\n", ret);
5e3dd157
KV
4072 goto exit;
4073 }
4074
4075 if (arvif->u.sta.uapsd)
4076 value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
4077 else
4078 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
4079
4080 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4081 WMI_STA_PS_PARAM_RX_WAKE_POLICY,
4082 value);
4083 if (ret)
7aa7a72a 4084 ath10k_warn(ar, "failed to set rx wake param: %d\n", ret);
5e3dd157 4085
9f9b5746
MK
4086 ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
4087 if (ret) {
4088 ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
4089 arvif->vdev_id, ret);
4090 return ret;
4091 }
4092
4093 ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
4094 if (ret) {
4095 ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
4096 arvif->vdev_id, ret);
4097 return ret;
4098 }
4099
b0e56154
MK
4100 if (test_bit(WMI_SERVICE_STA_UAPSD_BASIC_AUTO_TRIG, ar->wmi.svc_map) ||
4101 test_bit(WMI_SERVICE_STA_UAPSD_VAR_AUTO_TRIG, ar->wmi.svc_map)) {
4102 /* Only userspace can make an educated decision when to send
4103 * trigger frame. The following effectively disables u-UAPSD
4104 * autotrigger in firmware (which is enabled by default
4105 * provided the autotrigger service is available).
4106 */
4107
4108 arg.wmm_ac = acc;
4109 arg.user_priority = prio;
4110 arg.service_interval = 0;
4111 arg.suspend_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
4112 arg.delay_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
4113
4114 ret = ath10k_wmi_vdev_sta_uapsd(ar, arvif->vdev_id,
4115 arvif->bssid, &arg, 1);
4116 if (ret) {
4117 ath10k_warn(ar, "failed to set uapsd auto trigger %d\n",
4118 ret);
4119 return ret;
4120 }
4121 }
4122
5e3dd157
KV
4123exit:
4124 return ret;
4125}
4126
4127static int ath10k_conf_tx(struct ieee80211_hw *hw,
4128 struct ieee80211_vif *vif, u16 ac,
4129 const struct ieee80211_tx_queue_params *params)
4130{
4131 struct ath10k *ar = hw->priv;
5e752e42 4132 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5e3dd157
KV
4133 struct wmi_wmm_params_arg *p = NULL;
4134 int ret;
4135
4136 mutex_lock(&ar->conf_mutex);
4137
4138 switch (ac) {
4139 case IEEE80211_AC_VO:
5e752e42 4140 p = &arvif->wmm_params.ac_vo;
5e3dd157
KV
4141 break;
4142 case IEEE80211_AC_VI:
5e752e42 4143 p = &arvif->wmm_params.ac_vi;
5e3dd157
KV
4144 break;
4145 case IEEE80211_AC_BE:
5e752e42 4146 p = &arvif->wmm_params.ac_be;
5e3dd157
KV
4147 break;
4148 case IEEE80211_AC_BK:
5e752e42 4149 p = &arvif->wmm_params.ac_bk;
5e3dd157
KV
4150 break;
4151 }
4152
4153 if (WARN_ON(!p)) {
4154 ret = -EINVAL;
4155 goto exit;
4156 }
4157
4158 p->cwmin = params->cw_min;
4159 p->cwmax = params->cw_max;
4160 p->aifs = params->aifs;
4161
4162 /*
4163 * The channel time duration programmed in the HW is in absolute
4164 * microseconds, while mac80211 gives the txop in units of
4165 * 32 microseconds.
4166 */
4167 p->txop = params->txop * 32;
4168
7fc979a7
MK
4169 if (ar->wmi.ops->gen_vdev_wmm_conf) {
4170 ret = ath10k_wmi_vdev_wmm_conf(ar, arvif->vdev_id,
4171 &arvif->wmm_params);
4172 if (ret) {
4173 ath10k_warn(ar, "failed to set vdev wmm params on vdev %i: %d\n",
4174 arvif->vdev_id, ret);
4175 goto exit;
4176 }
4177 } else {
4178 /* This won't work well with multi-interface cases but it's
4179 * better than nothing.
4180 */
4181 ret = ath10k_wmi_pdev_set_wmm_params(ar, &arvif->wmm_params);
4182 if (ret) {
4183 ath10k_warn(ar, "failed to set wmm params: %d\n", ret);
4184 goto exit;
4185 }
5e3dd157
KV
4186 }
4187
4188 ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
4189 if (ret)
7aa7a72a 4190 ath10k_warn(ar, "failed to set sta uapsd: %d\n", ret);
5e3dd157
KV
4191
4192exit:
4193 mutex_unlock(&ar->conf_mutex);
4194 return ret;
4195}
4196
4197#define ATH10K_ROC_TIMEOUT_HZ (2*HZ)
4198
4199static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
4200 struct ieee80211_vif *vif,
4201 struct ieee80211_channel *chan,
4202 int duration,
4203 enum ieee80211_roc_type type)
4204{
4205 struct ath10k *ar = hw->priv;
4206 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4207 struct wmi_start_scan_arg arg;
5c81c7fd 4208 int ret = 0;
5e3dd157
KV
4209
4210 mutex_lock(&ar->conf_mutex);
4211
4212 spin_lock_bh(&ar->data_lock);
5c81c7fd
MK
4213 switch (ar->scan.state) {
4214 case ATH10K_SCAN_IDLE:
4215 reinit_completion(&ar->scan.started);
4216 reinit_completion(&ar->scan.completed);
4217 reinit_completion(&ar->scan.on_channel);
4218 ar->scan.state = ATH10K_SCAN_STARTING;
4219 ar->scan.is_roc = true;
4220 ar->scan.vdev_id = arvif->vdev_id;
4221 ar->scan.roc_freq = chan->center_freq;
4222 ret = 0;
4223 break;
4224 case ATH10K_SCAN_STARTING:
4225 case ATH10K_SCAN_RUNNING:
4226 case ATH10K_SCAN_ABORTING:
5e3dd157 4227 ret = -EBUSY;
5c81c7fd 4228 break;
5e3dd157 4229 }
5e3dd157
KV
4230 spin_unlock_bh(&ar->data_lock);
4231
5c81c7fd
MK
4232 if (ret)
4233 goto exit;
4234
dcca0bdb
MK
4235 duration = max(duration, WMI_SCAN_CHAN_MIN_TIME_MSEC);
4236
5e3dd157
KV
4237 memset(&arg, 0, sizeof(arg));
4238 ath10k_wmi_start_scan_init(ar, &arg);
4239 arg.vdev_id = arvif->vdev_id;
4240 arg.scan_id = ATH10K_SCAN_ID;
4241 arg.n_channels = 1;
4242 arg.channels[0] = chan->center_freq;
4243 arg.dwell_time_active = duration;
4244 arg.dwell_time_passive = duration;
4245 arg.max_scan_time = 2 * duration;
4246 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
4247 arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
4248
4249 ret = ath10k_start_scan(ar, &arg);
4250 if (ret) {
7aa7a72a 4251 ath10k_warn(ar, "failed to start roc scan: %d\n", ret);
5e3dd157 4252 spin_lock_bh(&ar->data_lock);
5c81c7fd 4253 ar->scan.state = ATH10K_SCAN_IDLE;
5e3dd157
KV
4254 spin_unlock_bh(&ar->data_lock);
4255 goto exit;
4256 }
4257
4258 ret = wait_for_completion_timeout(&ar->scan.on_channel, 3*HZ);
4259 if (ret == 0) {
7aa7a72a 4260 ath10k_warn(ar, "failed to switch to channel for roc scan\n");
5c81c7fd
MK
4261
4262 ret = ath10k_scan_stop(ar);
4263 if (ret)
7aa7a72a 4264 ath10k_warn(ar, "failed to stop scan: %d\n", ret);
5c81c7fd 4265
5e3dd157
KV
4266 ret = -ETIMEDOUT;
4267 goto exit;
4268 }
4269
4270 ret = 0;
4271exit:
4272 mutex_unlock(&ar->conf_mutex);
4273 return ret;
4274}
4275
4276static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw)
4277{
4278 struct ath10k *ar = hw->priv;
4279
4280 mutex_lock(&ar->conf_mutex);
5c81c7fd 4281 ath10k_scan_abort(ar);
5e3dd157
KV
4282 mutex_unlock(&ar->conf_mutex);
4283
4eb2e164
MK
4284 cancel_delayed_work_sync(&ar->scan.timeout);
4285
5e3dd157
KV
4286 return 0;
4287}
4288
4289/*
4290 * Both RTS and Fragmentation threshold are interface-specific
4291 * in ath10k, but device-specific in mac80211.
4292 */
5e3dd157 4293
ad088bfa
MK
4294static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
4295{
4296 struct ath10k *ar = hw->priv;
4297 struct ath10k_vif *arvif;
4298 int ret = 0;
548db54c 4299
5e3dd157 4300 mutex_lock(&ar->conf_mutex);
ad088bfa 4301 list_for_each_entry(arvif, &ar->arvifs, list) {
7aa7a72a 4302 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
ad088bfa
MK
4303 arvif->vdev_id, value);
4304
4305 ret = ath10k_mac_set_rts(arvif, value);
4306 if (ret) {
7aa7a72a 4307 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
ad088bfa
MK
4308 arvif->vdev_id, ret);
4309 break;
4310 }
4311 }
5e3dd157
KV
4312 mutex_unlock(&ar->conf_mutex);
4313
ad088bfa 4314 return ret;
5e3dd157
KV
4315}
4316
77be2c54
EG
4317static void ath10k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4318 u32 queues, bool drop)
5e3dd157
KV
4319{
4320 struct ath10k *ar = hw->priv;
affd3217 4321 bool skip;
5e3dd157
KV
4322 int ret;
4323
4324 /* mac80211 doesn't care if we really xmit queued frames or not
4325 * we'll collect those frames either way if we stop/delete vdevs */
4326 if (drop)
4327 return;
4328
548db54c
MK
4329 mutex_lock(&ar->conf_mutex);
4330
affd3217
MK
4331 if (ar->state == ATH10K_STATE_WEDGED)
4332 goto skip;
4333
edb8236d 4334 ret = wait_event_timeout(ar->htt.empty_tx_wq, ({
5e3dd157 4335 bool empty;
affd3217 4336
edb8236d 4337 spin_lock_bh(&ar->htt.tx_lock);
0945baf7 4338 empty = (ar->htt.num_pending_tx == 0);
edb8236d 4339 spin_unlock_bh(&ar->htt.tx_lock);
affd3217 4340
7962b0d8
MK
4341 skip = (ar->state == ATH10K_STATE_WEDGED) ||
4342 test_bit(ATH10K_FLAG_CRASH_FLUSH,
4343 &ar->dev_flags);
affd3217
MK
4344
4345 (empty || skip);
5e3dd157 4346 }), ATH10K_FLUSH_TIMEOUT_HZ);
affd3217
MK
4347
4348 if (ret <= 0 || skip)
7aa7a72a 4349 ath10k_warn(ar, "failed to flush transmit queue (skip %i ar-state %i): %i\n",
9ba4c787 4350 skip, ar->state, ret);
548db54c 4351
affd3217 4352skip:
548db54c 4353 mutex_unlock(&ar->conf_mutex);
5e3dd157
KV
4354}
4355
4356/* TODO: Implement this function properly
4357 * For now it is needed to reply to Probe Requests in IBSS mode.
4358 * Propably we need this information from FW.
4359 */
4360static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
4361{
4362 return 1;
4363}
4364
8cd13cad
MK
4365#ifdef CONFIG_PM
4366static int ath10k_suspend(struct ieee80211_hw *hw,
4367 struct cfg80211_wowlan *wowlan)
4368{
4369 struct ath10k *ar = hw->priv;
4370 int ret;
4371
9042e17d
MP
4372 mutex_lock(&ar->conf_mutex);
4373
00f5482b 4374 ret = ath10k_wait_for_suspend(ar, WMI_PDEV_SUSPEND);
8cd13cad 4375 if (ret) {
00f5482b
MP
4376 if (ret == -ETIMEDOUT)
4377 goto resume;
9042e17d
MP
4378 ret = 1;
4379 goto exit;
8cd13cad
MK
4380 }
4381
8cd13cad
MK
4382 ret = ath10k_hif_suspend(ar);
4383 if (ret) {
7aa7a72a 4384 ath10k_warn(ar, "failed to suspend hif: %d\n", ret);
8cd13cad
MK
4385 goto resume;
4386 }
4387
9042e17d
MP
4388 ret = 0;
4389 goto exit;
8cd13cad
MK
4390resume:
4391 ret = ath10k_wmi_pdev_resume_target(ar);
4392 if (ret)
7aa7a72a 4393 ath10k_warn(ar, "failed to resume target: %d\n", ret);
9042e17d
MP
4394
4395 ret = 1;
4396exit:
4397 mutex_unlock(&ar->conf_mutex);
4398 return ret;
8cd13cad
MK
4399}
4400
4401static int ath10k_resume(struct ieee80211_hw *hw)
4402{
4403 struct ath10k *ar = hw->priv;
4404 int ret;
4405
9042e17d
MP
4406 mutex_lock(&ar->conf_mutex);
4407
8cd13cad
MK
4408 ret = ath10k_hif_resume(ar);
4409 if (ret) {
7aa7a72a 4410 ath10k_warn(ar, "failed to resume hif: %d\n", ret);
9042e17d
MP
4411 ret = 1;
4412 goto exit;
8cd13cad
MK
4413 }
4414
4415 ret = ath10k_wmi_pdev_resume_target(ar);
4416 if (ret) {
7aa7a72a 4417 ath10k_warn(ar, "failed to resume target: %d\n", ret);
9042e17d
MP
4418 ret = 1;
4419 goto exit;
8cd13cad
MK
4420 }
4421
9042e17d
MP
4422 ret = 0;
4423exit:
4424 mutex_unlock(&ar->conf_mutex);
4425 return ret;
8cd13cad
MK
4426}
4427#endif
4428
cf2c92d8
EP
4429static void ath10k_reconfig_complete(struct ieee80211_hw *hw,
4430 enum ieee80211_reconfig_type reconfig_type)
affd3217
MK
4431{
4432 struct ath10k *ar = hw->priv;
4433
cf2c92d8
EP
4434 if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
4435 return;
4436
affd3217
MK
4437 mutex_lock(&ar->conf_mutex);
4438
4439 /* If device failed to restart it will be in a different state, e.g.
4440 * ATH10K_STATE_WEDGED */
4441 if (ar->state == ATH10K_STATE_RESTARTED) {
7aa7a72a 4442 ath10k_info(ar, "device successfully recovered\n");
affd3217 4443 ar->state = ATH10K_STATE_ON;
7962b0d8 4444 ieee80211_wake_queues(ar->hw);
affd3217
MK
4445 }
4446
4447 mutex_unlock(&ar->conf_mutex);
4448}
4449
2e1dea40
MK
4450static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
4451 struct survey_info *survey)
4452{
4453 struct ath10k *ar = hw->priv;
4454 struct ieee80211_supported_band *sband;
4455 struct survey_info *ar_survey = &ar->survey[idx];
4456 int ret = 0;
4457
4458 mutex_lock(&ar->conf_mutex);
4459
4460 sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
4461 if (sband && idx >= sband->n_channels) {
4462 idx -= sband->n_channels;
4463 sband = NULL;
4464 }
4465
4466 if (!sband)
4467 sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
4468
4469 if (!sband || idx >= sband->n_channels) {
4470 ret = -ENOENT;
4471 goto exit;
4472 }
4473
4474 spin_lock_bh(&ar->data_lock);
4475 memcpy(survey, ar_survey, sizeof(*survey));
4476 spin_unlock_bh(&ar->data_lock);
4477
4478 survey->channel = &sband->channels[idx];
4479
fa1d4df8
FF
4480 if (ar->rx_channel == survey->channel)
4481 survey->filled |= SURVEY_INFO_IN_USE;
4482
2e1dea40
MK
4483exit:
4484 mutex_unlock(&ar->conf_mutex);
4485 return ret;
4486}
4487
51ab1a0a
JD
4488/* Helper table for legacy fixed_rate/bitrate_mask */
4489static const u8 cck_ofdm_rate[] = {
4490 /* CCK */
4491 3, /* 1Mbps */
4492 2, /* 2Mbps */
4493 1, /* 5.5Mbps */
4494 0, /* 11Mbps */
4495 /* OFDM */
4496 3, /* 6Mbps */
4497 7, /* 9Mbps */
4498 2, /* 12Mbps */
4499 6, /* 18Mbps */
4500 1, /* 24Mbps */
4501 5, /* 36Mbps */
4502 0, /* 48Mbps */
4503 4, /* 54Mbps */
4504};
4505
4506/* Check if only one bit set */
4507static int ath10k_check_single_mask(u32 mask)
4508{
4509 int bit;
4510
4511 bit = ffs(mask);
4512 if (!bit)
4513 return 0;
4514
4515 mask &= ~BIT(bit - 1);
4516 if (mask)
4517 return 2;
4518
4519 return 1;
4520}
4521
4522static bool
4523ath10k_default_bitrate_mask(struct ath10k *ar,
4524 enum ieee80211_band band,
4525 const struct cfg80211_bitrate_mask *mask)
4526{
4527 u32 legacy = 0x00ff;
4528 u8 ht = 0xff, i;
4529 u16 vht = 0x3ff;
b116ea19
BG
4530 u16 nrf = ar->num_rf_chains;
4531
4532 if (ar->cfg_tx_chainmask)
4533 nrf = get_nss_from_chainmask(ar->cfg_tx_chainmask);
51ab1a0a
JD
4534
4535 switch (band) {
4536 case IEEE80211_BAND_2GHZ:
4537 legacy = 0x00fff;
4538 vht = 0;
4539 break;
4540 case IEEE80211_BAND_5GHZ:
4541 break;
4542 default:
4543 return false;
4544 }
4545
4546 if (mask->control[band].legacy != legacy)
4547 return false;
4548
b116ea19 4549 for (i = 0; i < nrf; i++)
51ab1a0a
JD
4550 if (mask->control[band].ht_mcs[i] != ht)
4551 return false;
4552
b116ea19 4553 for (i = 0; i < nrf; i++)
51ab1a0a
JD
4554 if (mask->control[band].vht_mcs[i] != vht)
4555 return false;
4556
4557 return true;
4558}
4559
4560static bool
4561ath10k_bitrate_mask_nss(const struct cfg80211_bitrate_mask *mask,
4562 enum ieee80211_band band,
4563 u8 *fixed_nss)
4564{
4565 int ht_nss = 0, vht_nss = 0, i;
4566
4567 /* check legacy */
4568 if (ath10k_check_single_mask(mask->control[band].legacy))
4569 return false;
4570
4571 /* check HT */
4572 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
4573 if (mask->control[band].ht_mcs[i] == 0xff)
4574 continue;
4575 else if (mask->control[band].ht_mcs[i] == 0x00)
4576 break;
d8bb26b9
KV
4577
4578 return false;
51ab1a0a
JD
4579 }
4580
4581 ht_nss = i;
4582
4583 /* check VHT */
4584 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
4585 if (mask->control[band].vht_mcs[i] == 0x03ff)
4586 continue;
4587 else if (mask->control[band].vht_mcs[i] == 0x0000)
4588 break;
d8bb26b9
KV
4589
4590 return false;
51ab1a0a
JD
4591 }
4592
4593 vht_nss = i;
4594
4595 if (ht_nss > 0 && vht_nss > 0)
4596 return false;
4597
4598 if (ht_nss)
4599 *fixed_nss = ht_nss;
4600 else if (vht_nss)
4601 *fixed_nss = vht_nss;
4602 else
4603 return false;
4604
4605 return true;
4606}
4607
4608static bool
4609ath10k_bitrate_mask_correct(const struct cfg80211_bitrate_mask *mask,
4610 enum ieee80211_band band,
4611 enum wmi_rate_preamble *preamble)
4612{
4613 int legacy = 0, ht = 0, vht = 0, i;
4614
4615 *preamble = WMI_RATE_PREAMBLE_OFDM;
4616
4617 /* check legacy */
4618 legacy = ath10k_check_single_mask(mask->control[band].legacy);
4619 if (legacy > 1)
4620 return false;
4621
4622 /* check HT */
4623 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
4624 ht += ath10k_check_single_mask(mask->control[band].ht_mcs[i]);
4625 if (ht > 1)
4626 return false;
4627
4628 /* check VHT */
4629 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
4630 vht += ath10k_check_single_mask(mask->control[band].vht_mcs[i]);
4631 if (vht > 1)
4632 return false;
4633
4634 /* Currently we support only one fixed_rate */
4635 if ((legacy + ht + vht) != 1)
4636 return false;
4637
4638 if (ht)
4639 *preamble = WMI_RATE_PREAMBLE_HT;
4640 else if (vht)
4641 *preamble = WMI_RATE_PREAMBLE_VHT;
4642
4643 return true;
4644}
4645
4646static bool
7aa7a72a
MK
4647ath10k_bitrate_mask_rate(struct ath10k *ar,
4648 const struct cfg80211_bitrate_mask *mask,
51ab1a0a
JD
4649 enum ieee80211_band band,
4650 u8 *fixed_rate,
4651 u8 *fixed_nss)
4652{
4653 u8 rate = 0, pream = 0, nss = 0, i;
4654 enum wmi_rate_preamble preamble;
4655
4656 /* Check if single rate correct */
4657 if (!ath10k_bitrate_mask_correct(mask, band, &preamble))
4658 return false;
4659
4660 pream = preamble;
4661
4662 switch (preamble) {
4663 case WMI_RATE_PREAMBLE_CCK:
4664 case WMI_RATE_PREAMBLE_OFDM:
4665 i = ffs(mask->control[band].legacy) - 1;
4666
4667 if (band == IEEE80211_BAND_2GHZ && i < 4)
4668 pream = WMI_RATE_PREAMBLE_CCK;
4669
4670 if (band == IEEE80211_BAND_5GHZ)
4671 i += 4;
4672
4673 if (i >= ARRAY_SIZE(cck_ofdm_rate))
4674 return false;
4675
4676 rate = cck_ofdm_rate[i];
4677 break;
4678 case WMI_RATE_PREAMBLE_HT:
4679 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
4680 if (mask->control[band].ht_mcs[i])
4681 break;
4682
4683 if (i == IEEE80211_HT_MCS_MASK_LEN)
4684 return false;
4685
4686 rate = ffs(mask->control[band].ht_mcs[i]) - 1;
4687 nss = i;
4688 break;
4689 case WMI_RATE_PREAMBLE_VHT:
4690 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
4691 if (mask->control[band].vht_mcs[i])
4692 break;
4693
4694 if (i == NL80211_VHT_NSS_MAX)
4695 return false;
4696
4697 rate = ffs(mask->control[band].vht_mcs[i]) - 1;
4698 nss = i;
4699 break;
4700 }
4701
4702 *fixed_nss = nss + 1;
4703 nss <<= 4;
4704 pream <<= 6;
4705
7aa7a72a 4706 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac fixed rate pream 0x%02x nss 0x%02x rate 0x%02x\n",
51ab1a0a
JD
4707 pream, nss, rate);
4708
4709 *fixed_rate = pream | nss | rate;
4710
4711 return true;
4712}
4713
7aa7a72a
MK
4714static bool ath10k_get_fixed_rate_nss(struct ath10k *ar,
4715 const struct cfg80211_bitrate_mask *mask,
51ab1a0a
JD
4716 enum ieee80211_band band,
4717 u8 *fixed_rate,
4718 u8 *fixed_nss)
4719{
4720 /* First check full NSS mask, if we can simply limit NSS */
4721 if (ath10k_bitrate_mask_nss(mask, band, fixed_nss))
4722 return true;
4723
4724 /* Next Check single rate is set */
7aa7a72a 4725 return ath10k_bitrate_mask_rate(ar, mask, band, fixed_rate, fixed_nss);
51ab1a0a
JD
4726}
4727
4728static int ath10k_set_fixed_rate_param(struct ath10k_vif *arvif,
4729 u8 fixed_rate,
9f81f725
JD
4730 u8 fixed_nss,
4731 u8 force_sgi)
51ab1a0a
JD
4732{
4733 struct ath10k *ar = arvif->ar;
4734 u32 vdev_param;
4735 int ret = 0;
4736
4737 mutex_lock(&ar->conf_mutex);
4738
4739 if (arvif->fixed_rate == fixed_rate &&
9f81f725
JD
4740 arvif->fixed_nss == fixed_nss &&
4741 arvif->force_sgi == force_sgi)
51ab1a0a
JD
4742 goto exit;
4743
4744 if (fixed_rate == WMI_FIXED_RATE_NONE)
7aa7a72a 4745 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac disable fixed bitrate mask\n");
51ab1a0a 4746
9f81f725 4747 if (force_sgi)
7aa7a72a 4748 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac force sgi\n");
9f81f725 4749
51ab1a0a
JD
4750 vdev_param = ar->wmi.vdev_param->fixed_rate;
4751 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
4752 vdev_param, fixed_rate);
4753 if (ret) {
7aa7a72a 4754 ath10k_warn(ar, "failed to set fixed rate param 0x%02x: %d\n",
51ab1a0a
JD
4755 fixed_rate, ret);
4756 ret = -EINVAL;
4757 goto exit;
4758 }
4759
4760 arvif->fixed_rate = fixed_rate;
4761
4762 vdev_param = ar->wmi.vdev_param->nss;
4763 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
4764 vdev_param, fixed_nss);
4765
4766 if (ret) {
7aa7a72a 4767 ath10k_warn(ar, "failed to set fixed nss param %d: %d\n",
51ab1a0a
JD
4768 fixed_nss, ret);
4769 ret = -EINVAL;
4770 goto exit;
4771 }
4772
4773 arvif->fixed_nss = fixed_nss;
4774
9f81f725
JD
4775 vdev_param = ar->wmi.vdev_param->sgi;
4776 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
4777 force_sgi);
4778
4779 if (ret) {
7aa7a72a 4780 ath10k_warn(ar, "failed to set sgi param %d: %d\n",
9f81f725
JD
4781 force_sgi, ret);
4782 ret = -EINVAL;
4783 goto exit;
4784 }
4785
4786 arvif->force_sgi = force_sgi;
4787
51ab1a0a
JD
4788exit:
4789 mutex_unlock(&ar->conf_mutex);
4790 return ret;
4791}
4792
4793static int ath10k_set_bitrate_mask(struct ieee80211_hw *hw,
4794 struct ieee80211_vif *vif,
4795 const struct cfg80211_bitrate_mask *mask)
4796{
4797 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4798 struct ath10k *ar = arvif->ar;
4799 enum ieee80211_band band = ar->hw->conf.chandef.chan->band;
4800 u8 fixed_rate = WMI_FIXED_RATE_NONE;
4801 u8 fixed_nss = ar->num_rf_chains;
9f81f725
JD
4802 u8 force_sgi;
4803
b116ea19
BG
4804 if (ar->cfg_tx_chainmask)
4805 fixed_nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
4806
9f81f725
JD
4807 force_sgi = mask->control[band].gi;
4808 if (force_sgi == NL80211_TXRATE_FORCE_LGI)
4809 return -EINVAL;
51ab1a0a
JD
4810
4811 if (!ath10k_default_bitrate_mask(ar, band, mask)) {
7aa7a72a 4812 if (!ath10k_get_fixed_rate_nss(ar, mask, band,
51ab1a0a
JD
4813 &fixed_rate,
4814 &fixed_nss))
4815 return -EINVAL;
4816 }
4817
9f81f725 4818 if (fixed_rate == WMI_FIXED_RATE_NONE && force_sgi) {
7aa7a72a 4819 ath10k_warn(ar, "failed to force SGI usage for default rate settings\n");
9f81f725
JD
4820 return -EINVAL;
4821 }
4822
4823 return ath10k_set_fixed_rate_param(arvif, fixed_rate,
4824 fixed_nss, force_sgi);
51ab1a0a
JD
4825}
4826
9797febc
MK
4827static void ath10k_sta_rc_update(struct ieee80211_hw *hw,
4828 struct ieee80211_vif *vif,
4829 struct ieee80211_sta *sta,
4830 u32 changed)
4831{
4832 struct ath10k *ar = hw->priv;
4833 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
4834 u32 bw, smps;
4835
4836 spin_lock_bh(&ar->data_lock);
4837
7aa7a72a 4838 ath10k_dbg(ar, ATH10K_DBG_MAC,
9797febc
MK
4839 "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
4840 sta->addr, changed, sta->bandwidth, sta->rx_nss,
4841 sta->smps_mode);
4842
4843 if (changed & IEEE80211_RC_BW_CHANGED) {
4844 bw = WMI_PEER_CHWIDTH_20MHZ;
4845
4846 switch (sta->bandwidth) {
4847 case IEEE80211_STA_RX_BW_20:
4848 bw = WMI_PEER_CHWIDTH_20MHZ;
4849 break;
4850 case IEEE80211_STA_RX_BW_40:
4851 bw = WMI_PEER_CHWIDTH_40MHZ;
4852 break;
4853 case IEEE80211_STA_RX_BW_80:
4854 bw = WMI_PEER_CHWIDTH_80MHZ;
4855 break;
4856 case IEEE80211_STA_RX_BW_160:
7aa7a72a 4857 ath10k_warn(ar, "Invalid bandwith %d in rc update for %pM\n",
be6546fc 4858 sta->bandwidth, sta->addr);
9797febc
MK
4859 bw = WMI_PEER_CHWIDTH_20MHZ;
4860 break;
4861 }
4862
4863 arsta->bw = bw;
4864 }
4865
4866 if (changed & IEEE80211_RC_NSS_CHANGED)
4867 arsta->nss = sta->rx_nss;
4868
4869 if (changed & IEEE80211_RC_SMPS_CHANGED) {
4870 smps = WMI_PEER_SMPS_PS_NONE;
4871
4872 switch (sta->smps_mode) {
4873 case IEEE80211_SMPS_AUTOMATIC:
4874 case IEEE80211_SMPS_OFF:
4875 smps = WMI_PEER_SMPS_PS_NONE;
4876 break;
4877 case IEEE80211_SMPS_STATIC:
4878 smps = WMI_PEER_SMPS_STATIC;
4879 break;
4880 case IEEE80211_SMPS_DYNAMIC:
4881 smps = WMI_PEER_SMPS_DYNAMIC;
4882 break;
4883 case IEEE80211_SMPS_NUM_MODES:
7aa7a72a 4884 ath10k_warn(ar, "Invalid smps %d in sta rc update for %pM\n",
be6546fc 4885 sta->smps_mode, sta->addr);
9797febc
MK
4886 smps = WMI_PEER_SMPS_PS_NONE;
4887 break;
4888 }
4889
4890 arsta->smps = smps;
4891 }
4892
9797febc
MK
4893 arsta->changed |= changed;
4894
4895 spin_unlock_bh(&ar->data_lock);
4896
4897 ieee80211_queue_work(hw, &arsta->update_wk);
4898}
4899
26ebbccf
CYY
4900static u64 ath10k_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
4901{
4902 /*
4903 * FIXME: Return 0 for time being. Need to figure out whether FW
4904 * has the API to fetch 64-bit local TSF
4905 */
4906
4907 return 0;
4908}
4909
aa5b4fbc
MK
4910static int ath10k_ampdu_action(struct ieee80211_hw *hw,
4911 struct ieee80211_vif *vif,
4912 enum ieee80211_ampdu_mlme_action action,
4913 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
4914 u8 buf_size)
4915{
7aa7a72a 4916 struct ath10k *ar = hw->priv;
aa5b4fbc
MK
4917 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4918
7aa7a72a 4919 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ampdu vdev_id %i sta %pM tid %hu action %d\n",
aa5b4fbc
MK
4920 arvif->vdev_id, sta->addr, tid, action);
4921
4922 switch (action) {
4923 case IEEE80211_AMPDU_RX_START:
4924 case IEEE80211_AMPDU_RX_STOP:
4925 /* HTT AddBa/DelBa events trigger mac80211 Rx BA session
4926 * creation/removal. Do we need to verify this?
4927 */
4928 return 0;
4929 case IEEE80211_AMPDU_TX_START:
4930 case IEEE80211_AMPDU_TX_STOP_CONT:
4931 case IEEE80211_AMPDU_TX_STOP_FLUSH:
4932 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
4933 case IEEE80211_AMPDU_TX_OPERATIONAL:
4934 /* Firmware offloads Tx aggregation entirely so deny mac80211
4935 * Tx aggregation requests.
4936 */
4937 return -EOPNOTSUPP;
4938 }
4939
4940 return -EINVAL;
4941}
4942
5e3dd157
KV
4943static const struct ieee80211_ops ath10k_ops = {
4944 .tx = ath10k_tx,
4945 .start = ath10k_start,
4946 .stop = ath10k_stop,
4947 .config = ath10k_config,
4948 .add_interface = ath10k_add_interface,
4949 .remove_interface = ath10k_remove_interface,
4950 .configure_filter = ath10k_configure_filter,
4951 .bss_info_changed = ath10k_bss_info_changed,
4952 .hw_scan = ath10k_hw_scan,
4953 .cancel_hw_scan = ath10k_cancel_hw_scan,
4954 .set_key = ath10k_set_key,
627613f8 4955 .set_default_unicast_key = ath10k_set_default_unicast_key,
5e3dd157
KV
4956 .sta_state = ath10k_sta_state,
4957 .conf_tx = ath10k_conf_tx,
4958 .remain_on_channel = ath10k_remain_on_channel,
4959 .cancel_remain_on_channel = ath10k_cancel_remain_on_channel,
4960 .set_rts_threshold = ath10k_set_rts_threshold,
5e3dd157
KV
4961 .flush = ath10k_flush,
4962 .tx_last_beacon = ath10k_tx_last_beacon,
46acf7bb
BG
4963 .set_antenna = ath10k_set_antenna,
4964 .get_antenna = ath10k_get_antenna,
cf2c92d8 4965 .reconfig_complete = ath10k_reconfig_complete,
2e1dea40 4966 .get_survey = ath10k_get_survey,
51ab1a0a 4967 .set_bitrate_mask = ath10k_set_bitrate_mask,
9797febc 4968 .sta_rc_update = ath10k_sta_rc_update,
26ebbccf 4969 .get_tsf = ath10k_get_tsf,
aa5b4fbc 4970 .ampdu_action = ath10k_ampdu_action,
6cddcc7a
BG
4971 .get_et_sset_count = ath10k_debug_get_et_sset_count,
4972 .get_et_stats = ath10k_debug_get_et_stats,
4973 .get_et_strings = ath10k_debug_get_et_strings,
43d2a30f
KV
4974
4975 CFG80211_TESTMODE_CMD(ath10k_tm_cmd)
4976
8cd13cad
MK
4977#ifdef CONFIG_PM
4978 .suspend = ath10k_suspend,
4979 .resume = ath10k_resume,
4980#endif
f5045988
RM
4981#ifdef CONFIG_MAC80211_DEBUGFS
4982 .sta_add_debugfs = ath10k_sta_add_debugfs,
4983#endif
5e3dd157
KV
4984};
4985
4986#define RATETAB_ENT(_rate, _rateid, _flags) { \
4987 .bitrate = (_rate), \
4988 .flags = (_flags), \
4989 .hw_value = (_rateid), \
4990}
4991
4992#define CHAN2G(_channel, _freq, _flags) { \
4993 .band = IEEE80211_BAND_2GHZ, \
4994 .hw_value = (_channel), \
4995 .center_freq = (_freq), \
4996 .flags = (_flags), \
4997 .max_antenna_gain = 0, \
4998 .max_power = 30, \
4999}
5000
5001#define CHAN5G(_channel, _freq, _flags) { \
5002 .band = IEEE80211_BAND_5GHZ, \
5003 .hw_value = (_channel), \
5004 .center_freq = (_freq), \
5005 .flags = (_flags), \
5006 .max_antenna_gain = 0, \
5007 .max_power = 30, \
5008}
5009
5010static const struct ieee80211_channel ath10k_2ghz_channels[] = {
5011 CHAN2G(1, 2412, 0),
5012 CHAN2G(2, 2417, 0),
5013 CHAN2G(3, 2422, 0),
5014 CHAN2G(4, 2427, 0),
5015 CHAN2G(5, 2432, 0),
5016 CHAN2G(6, 2437, 0),
5017 CHAN2G(7, 2442, 0),
5018 CHAN2G(8, 2447, 0),
5019 CHAN2G(9, 2452, 0),
5020 CHAN2G(10, 2457, 0),
5021 CHAN2G(11, 2462, 0),
5022 CHAN2G(12, 2467, 0),
5023 CHAN2G(13, 2472, 0),
5024 CHAN2G(14, 2484, 0),
5025};
5026
5027static const struct ieee80211_channel ath10k_5ghz_channels[] = {
429ff56a
MK
5028 CHAN5G(36, 5180, 0),
5029 CHAN5G(40, 5200, 0),
5030 CHAN5G(44, 5220, 0),
5031 CHAN5G(48, 5240, 0),
5032 CHAN5G(52, 5260, 0),
5033 CHAN5G(56, 5280, 0),
5034 CHAN5G(60, 5300, 0),
5035 CHAN5G(64, 5320, 0),
5036 CHAN5G(100, 5500, 0),
5037 CHAN5G(104, 5520, 0),
5038 CHAN5G(108, 5540, 0),
5039 CHAN5G(112, 5560, 0),
5040 CHAN5G(116, 5580, 0),
5041 CHAN5G(120, 5600, 0),
5042 CHAN5G(124, 5620, 0),
5043 CHAN5G(128, 5640, 0),
5044 CHAN5G(132, 5660, 0),
5045 CHAN5G(136, 5680, 0),
5046 CHAN5G(140, 5700, 0),
5047 CHAN5G(149, 5745, 0),
5048 CHAN5G(153, 5765, 0),
5049 CHAN5G(157, 5785, 0),
5050 CHAN5G(161, 5805, 0),
5051 CHAN5G(165, 5825, 0),
5e3dd157
KV
5052};
5053
91b12089
MK
5054/* Note: Be careful if you re-order these. There is code which depends on this
5055 * ordering.
5056 */
5e3dd157
KV
5057static struct ieee80211_rate ath10k_rates[] = {
5058 /* CCK */
5059 RATETAB_ENT(10, 0x82, 0),
5060 RATETAB_ENT(20, 0x84, 0),
5061 RATETAB_ENT(55, 0x8b, 0),
5062 RATETAB_ENT(110, 0x96, 0),
5063 /* OFDM */
5064 RATETAB_ENT(60, 0x0c, 0),
5065 RATETAB_ENT(90, 0x12, 0),
5066 RATETAB_ENT(120, 0x18, 0),
5067 RATETAB_ENT(180, 0x24, 0),
5068 RATETAB_ENT(240, 0x30, 0),
5069 RATETAB_ENT(360, 0x48, 0),
5070 RATETAB_ENT(480, 0x60, 0),
5071 RATETAB_ENT(540, 0x6c, 0),
5072};
5073
5074#define ath10k_a_rates (ath10k_rates + 4)
5075#define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - 4)
5076#define ath10k_g_rates (ath10k_rates + 0)
5077#define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
5078
e7b54194 5079struct ath10k *ath10k_mac_create(size_t priv_size)
5e3dd157
KV
5080{
5081 struct ieee80211_hw *hw;
5082 struct ath10k *ar;
5083
e7b54194 5084 hw = ieee80211_alloc_hw(sizeof(struct ath10k) + priv_size, &ath10k_ops);
5e3dd157
KV
5085 if (!hw)
5086 return NULL;
5087
5088 ar = hw->priv;
5089 ar->hw = hw;
5090
5091 return ar;
5092}
5093
5094void ath10k_mac_destroy(struct ath10k *ar)
5095{
5096 ieee80211_free_hw(ar->hw);
5097}
5098
5099static const struct ieee80211_iface_limit ath10k_if_limits[] = {
5100 {
5101 .max = 8,
5102 .types = BIT(NL80211_IFTYPE_STATION)
5103 | BIT(NL80211_IFTYPE_P2P_CLIENT)
d531cb85
MK
5104 },
5105 {
5106 .max = 3,
5107 .types = BIT(NL80211_IFTYPE_P2P_GO)
5108 },
5109 {
75d2bd48
MK
5110 .max = 1,
5111 .types = BIT(NL80211_IFTYPE_P2P_DEVICE)
5112 },
5113 {
d531cb85
MK
5114 .max = 7,
5115 .types = BIT(NL80211_IFTYPE_AP)
5116 },
5e3dd157
KV
5117};
5118
f259509b 5119static const struct ieee80211_iface_limit ath10k_10x_if_limits[] = {
e8a50f8b
MP
5120 {
5121 .max = 8,
5122 .types = BIT(NL80211_IFTYPE_AP)
5123 },
5124};
e8a50f8b
MP
5125
5126static const struct ieee80211_iface_combination ath10k_if_comb[] = {
5127 {
5128 .limits = ath10k_if_limits,
5129 .n_limits = ARRAY_SIZE(ath10k_if_limits),
5130 .max_interfaces = 8,
5131 .num_different_channels = 1,
5132 .beacon_int_infra_match = true,
5133 },
f259509b
BM
5134};
5135
5136static const struct ieee80211_iface_combination ath10k_10x_if_comb[] = {
e8a50f8b 5137 {
f259509b
BM
5138 .limits = ath10k_10x_if_limits,
5139 .n_limits = ARRAY_SIZE(ath10k_10x_if_limits),
e8a50f8b
MP
5140 .max_interfaces = 8,
5141 .num_different_channels = 1,
5142 .beacon_int_infra_match = true,
f259509b 5143#ifdef CONFIG_ATH10K_DFS_CERTIFIED
e8a50f8b
MP
5144 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
5145 BIT(NL80211_CHAN_WIDTH_20) |
5146 BIT(NL80211_CHAN_WIDTH_40) |
5147 BIT(NL80211_CHAN_WIDTH_80),
e8a50f8b 5148#endif
f259509b 5149 },
5e3dd157
KV
5150};
5151
5152static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
5153{
5154 struct ieee80211_sta_vht_cap vht_cap = {0};
5155 u16 mcs_map;
8865bee4 5156 int i;
5e3dd157
KV
5157
5158 vht_cap.vht_supported = 1;
5159 vht_cap.cap = ar->vht_cap_info;
5160
8865bee4
MK
5161 mcs_map = 0;
5162 for (i = 0; i < 8; i++) {
5163 if (i < ar->num_rf_chains)
5164 mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i*2);
5165 else
5166 mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i*2);
5167 }
5e3dd157
KV
5168
5169 vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
5170 vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
5171
5172 return vht_cap;
5173}
5174
5175static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
5176{
5177 int i;
5178 struct ieee80211_sta_ht_cap ht_cap = {0};
5179
5180 if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
5181 return ht_cap;
5182
5183 ht_cap.ht_supported = 1;
5184 ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
5185 ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
5186 ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
5187 ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
5188 ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
5189
5190 if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
5191 ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
5192
5193 if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
5194 ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
5195
5196 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
5197 u32 smps;
5198
5199 smps = WLAN_HT_CAP_SM_PS_DYNAMIC;
5200 smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
5201
5202 ht_cap.cap |= smps;
5203 }
5204
5205 if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC)
5206 ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
5207
5208 if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
5209 u32 stbc;
5210
5211 stbc = ar->ht_cap_info;
5212 stbc &= WMI_HT_CAP_RX_STBC;
5213 stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
5214 stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
5215 stbc &= IEEE80211_HT_CAP_RX_STBC;
5216
5217 ht_cap.cap |= stbc;
5218 }
5219
5220 if (ar->ht_cap_info & WMI_HT_CAP_LDPC)
5221 ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
5222
5223 if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
5224 ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
5225
5226 /* max AMSDU is implicitly taken from vht_cap_info */
5227 if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
5228 ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
5229
8865bee4 5230 for (i = 0; i < ar->num_rf_chains; i++)
5e3dd157
KV
5231 ht_cap.mcs.rx_mask[i] = 0xFF;
5232
5233 ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
5234
5235 return ht_cap;
5236}
5237
5e3dd157
KV
5238static void ath10k_get_arvif_iter(void *data, u8 *mac,
5239 struct ieee80211_vif *vif)
5240{
5241 struct ath10k_vif_iter *arvif_iter = data;
5242 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5243
5244 if (arvif->vdev_id == arvif_iter->vdev_id)
5245 arvif_iter->arvif = arvif;
5246}
5247
5248struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
5249{
5250 struct ath10k_vif_iter arvif_iter;
5251 u32 flags;
5252
5253 memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
5254 arvif_iter.vdev_id = vdev_id;
5255
5256 flags = IEEE80211_IFACE_ITER_RESUME_ALL;
5257 ieee80211_iterate_active_interfaces_atomic(ar->hw,
5258 flags,
5259 ath10k_get_arvif_iter,
5260 &arvif_iter);
5261 if (!arvif_iter.arvif) {
7aa7a72a 5262 ath10k_warn(ar, "No VIF found for vdev %d\n", vdev_id);
5e3dd157
KV
5263 return NULL;
5264 }
5265
5266 return arvif_iter.arvif;
5267}
5268
5269int ath10k_mac_register(struct ath10k *ar)
5270{
5271 struct ieee80211_supported_band *band;
5272 struct ieee80211_sta_vht_cap vht_cap;
5273 struct ieee80211_sta_ht_cap ht_cap;
5274 void *channels;
5275 int ret;
5276
5277 SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
5278
5279 SET_IEEE80211_DEV(ar->hw, ar->dev);
5280
5281 ht_cap = ath10k_get_ht_cap(ar);
5282 vht_cap = ath10k_create_vht_cap(ar);
5283
5284 if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
5285 channels = kmemdup(ath10k_2ghz_channels,
5286 sizeof(ath10k_2ghz_channels),
5287 GFP_KERNEL);
d6015b27
MK
5288 if (!channels) {
5289 ret = -ENOMEM;
5290 goto err_free;
5291 }
5e3dd157
KV
5292
5293 band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
5294 band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
5295 band->channels = channels;
5296 band->n_bitrates = ath10k_g_rates_size;
5297 band->bitrates = ath10k_g_rates;
5298 band->ht_cap = ht_cap;
5299
d68bb12a
YL
5300 /* Enable the VHT support at 2.4 GHz */
5301 band->vht_cap = vht_cap;
5e3dd157
KV
5302
5303 ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
5304 }
5305
5306 if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
5307 channels = kmemdup(ath10k_5ghz_channels,
5308 sizeof(ath10k_5ghz_channels),
5309 GFP_KERNEL);
5310 if (!channels) {
d6015b27
MK
5311 ret = -ENOMEM;
5312 goto err_free;
5e3dd157
KV
5313 }
5314
5315 band = &ar->mac.sbands[IEEE80211_BAND_5GHZ];
5316 band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
5317 band->channels = channels;
5318 band->n_bitrates = ath10k_a_rates_size;
5319 band->bitrates = ath10k_a_rates;
5320 band->ht_cap = ht_cap;
5321 band->vht_cap = vht_cap;
5322 ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
5323 }
5324
5325 ar->hw->wiphy->interface_modes =
5326 BIT(NL80211_IFTYPE_STATION) |
d354181f
BM
5327 BIT(NL80211_IFTYPE_AP);
5328
46acf7bb
BG
5329 ar->hw->wiphy->available_antennas_rx = ar->supp_rx_chainmask;
5330 ar->hw->wiphy->available_antennas_tx = ar->supp_tx_chainmask;
5331
d354181f
BM
5332 if (!test_bit(ATH10K_FW_FEATURE_NO_P2P, ar->fw_features))
5333 ar->hw->wiphy->interface_modes |=
75d2bd48 5334 BIT(NL80211_IFTYPE_P2P_DEVICE) |
d354181f
BM
5335 BIT(NL80211_IFTYPE_P2P_CLIENT) |
5336 BIT(NL80211_IFTYPE_P2P_GO);
5e3dd157
KV
5337
5338 ar->hw->flags = IEEE80211_HW_SIGNAL_DBM |
5339 IEEE80211_HW_SUPPORTS_PS |
5340 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
5e3dd157
KV
5341 IEEE80211_HW_MFP_CAPABLE |
5342 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
5343 IEEE80211_HW_HAS_RATE_CONTROL |
2f0f1121
JD
5344 IEEE80211_HW_AP_LINK_PS |
5345 IEEE80211_HW_SPECTRUM_MGMT;
5e3dd157 5346
0d8614b4
EP
5347 ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
5348
5e3dd157 5349 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
0d8614b4 5350 ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
5e3dd157
KV
5351
5352 if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
5353 ar->hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
5354 ar->hw->flags |= IEEE80211_HW_TX_AMPDU_SETUP_IN_HW;
5355 }
5356
5357 ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
5358 ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
5359
5360 ar->hw->vif_data_size = sizeof(struct ath10k_vif);
9797febc 5361 ar->hw->sta_data_size = sizeof(struct ath10k_sta);
5e3dd157 5362
5e3dd157
KV
5363 ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
5364
fbb8f1b7
MK
5365 if (test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)) {
5366 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
5367
5368 /* Firmware delivers WPS/P2P Probe Requests frames to driver so
5369 * that userspace (e.g. wpa_supplicant/hostapd) can generate
5370 * correct Probe Responses. This is more of a hack advert..
5371 */
5372 ar->hw->wiphy->probe_resp_offload |=
5373 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
5374 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
5375 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
5376 }
5377
5e3dd157 5378 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
c2df44b3 5379 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
5e3dd157
KV
5380 ar->hw->wiphy->max_remain_on_channel_duration = 5000;
5381
5382 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
78157a1c
RM
5383 ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE;
5384
5e3dd157
KV
5385 /*
5386 * on LL hardware queues are managed entirely by the FW
5387 * so we only advertise to mac we can do the queues thing
5388 */
5389 ar->hw->queues = 4;
5390
5cc7caf4
KV
5391 switch (ar->wmi.op_version) {
5392 case ATH10K_FW_WMI_OP_VERSION_MAIN:
5393 case ATH10K_FW_WMI_OP_VERSION_TLV:
f259509b
BM
5394 ar->hw->wiphy->iface_combinations = ath10k_if_comb;
5395 ar->hw->wiphy->n_iface_combinations =
5396 ARRAY_SIZE(ath10k_if_comb);
cf850d1d 5397 ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
5cc7caf4
KV
5398 break;
5399 case ATH10K_FW_WMI_OP_VERSION_10_1:
5400 case ATH10K_FW_WMI_OP_VERSION_10_2:
4a16fbec 5401 case ATH10K_FW_WMI_OP_VERSION_10_2_4:
5cc7caf4
KV
5402 ar->hw->wiphy->iface_combinations = ath10k_10x_if_comb;
5403 ar->hw->wiphy->n_iface_combinations =
5404 ARRAY_SIZE(ath10k_10x_if_comb);
5405 break;
5406 case ATH10K_FW_WMI_OP_VERSION_UNSET:
5407 case ATH10K_FW_WMI_OP_VERSION_MAX:
5408 WARN_ON(1);
5409 ret = -EINVAL;
5410 goto err_free;
f259509b 5411 }
5e3dd157 5412
7c199997
MK
5413 ar->hw->netdev_features = NETIF_F_HW_CSUM;
5414
9702c686
JD
5415 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED)) {
5416 /* Init ath dfs pattern detector */
5417 ar->ath_common.debug_mask = ATH_DBG_DFS;
5418 ar->dfs_detector = dfs_pattern_detector_init(&ar->ath_common,
5419 NL80211_DFS_UNSET);
5420
5421 if (!ar->dfs_detector)
7aa7a72a 5422 ath10k_warn(ar, "failed to initialise DFS pattern detector\n");
9702c686
JD
5423 }
5424
5e3dd157
KV
5425 ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
5426 ath10k_reg_notifier);
5427 if (ret) {
7aa7a72a 5428 ath10k_err(ar, "failed to initialise regulatory: %i\n", ret);
d6015b27 5429 goto err_free;
5e3dd157
KV
5430 }
5431
5432 ret = ieee80211_register_hw(ar->hw);
5433 if (ret) {
7aa7a72a 5434 ath10k_err(ar, "failed to register ieee80211: %d\n", ret);
d6015b27 5435 goto err_free;
5e3dd157
KV
5436 }
5437
5438 if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
5439 ret = regulatory_hint(ar->hw->wiphy,
5440 ar->ath_common.regulatory.alpha2);
5441 if (ret)
d6015b27 5442 goto err_unregister;
5e3dd157
KV
5443 }
5444
5445 return 0;
d6015b27
MK
5446
5447err_unregister:
5e3dd157 5448 ieee80211_unregister_hw(ar->hw);
d6015b27
MK
5449err_free:
5450 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
5451 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
5452
5e3dd157
KV
5453 return ret;
5454}
5455
5456void ath10k_mac_unregister(struct ath10k *ar)
5457{
5458 ieee80211_unregister_hw(ar->hw);
5459
9702c686
JD
5460 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
5461 ar->dfs_detector->exit(ar->dfs_detector);
5462
5e3dd157
KV
5463 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
5464 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
5465
5466 SET_IEEE80211_DEV(ar->hw, NULL);
5467}