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