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