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