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