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