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