ath10k: introduce dynamic pdev parameters
[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
30 /**********/
31 /* Crypto */
32 /**********/
33
34 static int ath10k_send_key(struct ath10k_vif *arvif,
35 struct ieee80211_key_conf *key,
36 enum set_key_cmd cmd,
37 const u8 *macaddr)
38 {
39 struct wmi_vdev_install_key_arg arg = {
40 .vdev_id = arvif->vdev_id,
41 .key_idx = key->keyidx,
42 .key_len = key->keylen,
43 .key_data = key->key,
44 .macaddr = macaddr,
45 };
46
47 lockdep_assert_held(&arvif->ar->conf_mutex);
48
49 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
50 arg.key_flags = WMI_KEY_PAIRWISE;
51 else
52 arg.key_flags = WMI_KEY_GROUP;
53
54 switch (key->cipher) {
55 case WLAN_CIPHER_SUITE_CCMP:
56 arg.key_cipher = WMI_CIPHER_AES_CCM;
57 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
58 break;
59 case WLAN_CIPHER_SUITE_TKIP:
60 arg.key_cipher = WMI_CIPHER_TKIP;
61 arg.key_txmic_len = 8;
62 arg.key_rxmic_len = 8;
63 break;
64 case WLAN_CIPHER_SUITE_WEP40:
65 case WLAN_CIPHER_SUITE_WEP104:
66 arg.key_cipher = WMI_CIPHER_WEP;
67 /* AP/IBSS mode requires self-key to be groupwise
68 * Otherwise pairwise key must be set */
69 if (memcmp(macaddr, arvif->vif->addr, ETH_ALEN))
70 arg.key_flags = WMI_KEY_PAIRWISE;
71 break;
72 default:
73 ath10k_warn("cipher %d is not supported\n", key->cipher);
74 return -EOPNOTSUPP;
75 }
76
77 if (cmd == DISABLE_KEY) {
78 arg.key_cipher = WMI_CIPHER_NONE;
79 arg.key_data = NULL;
80 }
81
82 return ath10k_wmi_vdev_install_key(arvif->ar, &arg);
83 }
84
85 static int ath10k_install_key(struct ath10k_vif *arvif,
86 struct ieee80211_key_conf *key,
87 enum set_key_cmd cmd,
88 const u8 *macaddr)
89 {
90 struct ath10k *ar = arvif->ar;
91 int ret;
92
93 lockdep_assert_held(&ar->conf_mutex);
94
95 INIT_COMPLETION(ar->install_key_done);
96
97 ret = ath10k_send_key(arvif, key, cmd, macaddr);
98 if (ret)
99 return ret;
100
101 ret = wait_for_completion_timeout(&ar->install_key_done, 3*HZ);
102 if (ret == 0)
103 return -ETIMEDOUT;
104
105 return 0;
106 }
107
108 static int ath10k_install_peer_wep_keys(struct ath10k_vif *arvif,
109 const u8 *addr)
110 {
111 struct ath10k *ar = arvif->ar;
112 struct ath10k_peer *peer;
113 int ret;
114 int i;
115
116 lockdep_assert_held(&ar->conf_mutex);
117
118 spin_lock_bh(&ar->data_lock);
119 peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
120 spin_unlock_bh(&ar->data_lock);
121
122 if (!peer)
123 return -ENOENT;
124
125 for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
126 if (arvif->wep_keys[i] == NULL)
127 continue;
128
129 ret = ath10k_install_key(arvif, arvif->wep_keys[i], SET_KEY,
130 addr);
131 if (ret)
132 return ret;
133
134 peer->keys[i] = arvif->wep_keys[i];
135 }
136
137 return 0;
138 }
139
140 static int ath10k_clear_peer_keys(struct ath10k_vif *arvif,
141 const u8 *addr)
142 {
143 struct ath10k *ar = arvif->ar;
144 struct ath10k_peer *peer;
145 int first_errno = 0;
146 int ret;
147 int i;
148
149 lockdep_assert_held(&ar->conf_mutex);
150
151 spin_lock_bh(&ar->data_lock);
152 peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
153 spin_unlock_bh(&ar->data_lock);
154
155 if (!peer)
156 return -ENOENT;
157
158 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
159 if (peer->keys[i] == NULL)
160 continue;
161
162 ret = ath10k_install_key(arvif, peer->keys[i],
163 DISABLE_KEY, addr);
164 if (ret && first_errno == 0)
165 first_errno = ret;
166
167 if (ret)
168 ath10k_warn("could not remove peer wep key %d (%d)\n",
169 i, ret);
170
171 peer->keys[i] = NULL;
172 }
173
174 return first_errno;
175 }
176
177 static int ath10k_clear_vdev_key(struct ath10k_vif *arvif,
178 struct ieee80211_key_conf *key)
179 {
180 struct ath10k *ar = arvif->ar;
181 struct ath10k_peer *peer;
182 u8 addr[ETH_ALEN];
183 int first_errno = 0;
184 int ret;
185 int i;
186
187 lockdep_assert_held(&ar->conf_mutex);
188
189 for (;;) {
190 /* since ath10k_install_key we can't hold data_lock all the
191 * time, so we try to remove the keys incrementally */
192 spin_lock_bh(&ar->data_lock);
193 i = 0;
194 list_for_each_entry(peer, &ar->peers, list) {
195 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
196 if (peer->keys[i] == key) {
197 memcpy(addr, peer->addr, ETH_ALEN);
198 peer->keys[i] = NULL;
199 break;
200 }
201 }
202
203 if (i < ARRAY_SIZE(peer->keys))
204 break;
205 }
206 spin_unlock_bh(&ar->data_lock);
207
208 if (i == ARRAY_SIZE(peer->keys))
209 break;
210
211 ret = ath10k_install_key(arvif, key, DISABLE_KEY, addr);
212 if (ret && first_errno == 0)
213 first_errno = ret;
214
215 if (ret)
216 ath10k_warn("could not remove key for %pM\n", addr);
217 }
218
219 return first_errno;
220 }
221
222
223 /*********************/
224 /* General utilities */
225 /*********************/
226
227 static inline enum wmi_phy_mode
228 chan_to_phymode(const struct cfg80211_chan_def *chandef)
229 {
230 enum wmi_phy_mode phymode = MODE_UNKNOWN;
231
232 switch (chandef->chan->band) {
233 case IEEE80211_BAND_2GHZ:
234 switch (chandef->width) {
235 case NL80211_CHAN_WIDTH_20_NOHT:
236 phymode = MODE_11G;
237 break;
238 case NL80211_CHAN_WIDTH_20:
239 phymode = MODE_11NG_HT20;
240 break;
241 case NL80211_CHAN_WIDTH_40:
242 phymode = MODE_11NG_HT40;
243 break;
244 case NL80211_CHAN_WIDTH_5:
245 case NL80211_CHAN_WIDTH_10:
246 case NL80211_CHAN_WIDTH_80:
247 case NL80211_CHAN_WIDTH_80P80:
248 case NL80211_CHAN_WIDTH_160:
249 phymode = MODE_UNKNOWN;
250 break;
251 }
252 break;
253 case IEEE80211_BAND_5GHZ:
254 switch (chandef->width) {
255 case NL80211_CHAN_WIDTH_20_NOHT:
256 phymode = MODE_11A;
257 break;
258 case NL80211_CHAN_WIDTH_20:
259 phymode = MODE_11NA_HT20;
260 break;
261 case NL80211_CHAN_WIDTH_40:
262 phymode = MODE_11NA_HT40;
263 break;
264 case NL80211_CHAN_WIDTH_80:
265 phymode = MODE_11AC_VHT80;
266 break;
267 case NL80211_CHAN_WIDTH_5:
268 case NL80211_CHAN_WIDTH_10:
269 case NL80211_CHAN_WIDTH_80P80:
270 case NL80211_CHAN_WIDTH_160:
271 phymode = MODE_UNKNOWN;
272 break;
273 }
274 break;
275 default:
276 break;
277 }
278
279 WARN_ON(phymode == MODE_UNKNOWN);
280 return phymode;
281 }
282
283 static u8 ath10k_parse_mpdudensity(u8 mpdudensity)
284 {
285 /*
286 * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
287 * 0 for no restriction
288 * 1 for 1/4 us
289 * 2 for 1/2 us
290 * 3 for 1 us
291 * 4 for 2 us
292 * 5 for 4 us
293 * 6 for 8 us
294 * 7 for 16 us
295 */
296 switch (mpdudensity) {
297 case 0:
298 return 0;
299 case 1:
300 case 2:
301 case 3:
302 /* Our lower layer calculations limit our precision to
303 1 microsecond */
304 return 1;
305 case 4:
306 return 2;
307 case 5:
308 return 4;
309 case 6:
310 return 8;
311 case 7:
312 return 16;
313 default:
314 return 0;
315 }
316 }
317
318 static int ath10k_peer_create(struct ath10k *ar, u32 vdev_id, const u8 *addr)
319 {
320 int ret;
321
322 lockdep_assert_held(&ar->conf_mutex);
323
324 ret = ath10k_wmi_peer_create(ar, vdev_id, addr);
325 if (ret)
326 return ret;
327
328 ret = ath10k_wait_for_peer_created(ar, vdev_id, addr);
329 if (ret)
330 return ret;
331
332 return 0;
333 }
334
335 static int ath10k_mac_set_rts(struct ath10k_vif *arvif, u32 value)
336 {
337 struct ath10k *ar = arvif->ar;
338 u32 vdev_param;
339
340 if (value != 0xFFFFFFFF)
341 value = min_t(u32, arvif->ar->hw->wiphy->rts_threshold,
342 ATH10K_RTS_MAX);
343
344 vdev_param = ar->wmi.vdev_param->rts_threshold;
345 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
346 }
347
348 static int ath10k_mac_set_frag(struct ath10k_vif *arvif, u32 value)
349 {
350 struct ath10k *ar = arvif->ar;
351 u32 vdev_param;
352
353 if (value != 0xFFFFFFFF)
354 value = clamp_t(u32, arvif->ar->hw->wiphy->frag_threshold,
355 ATH10K_FRAGMT_THRESHOLD_MIN,
356 ATH10K_FRAGMT_THRESHOLD_MAX);
357
358 vdev_param = ar->wmi.vdev_param->fragmentation_threshold;
359 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
360 }
361
362 static int ath10k_peer_delete(struct ath10k *ar, u32 vdev_id, const u8 *addr)
363 {
364 int ret;
365
366 lockdep_assert_held(&ar->conf_mutex);
367
368 ret = ath10k_wmi_peer_delete(ar, vdev_id, addr);
369 if (ret)
370 return ret;
371
372 ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
373 if (ret)
374 return ret;
375
376 return 0;
377 }
378
379 static void ath10k_peer_cleanup(struct ath10k *ar, u32 vdev_id)
380 {
381 struct ath10k_peer *peer, *tmp;
382
383 lockdep_assert_held(&ar->conf_mutex);
384
385 spin_lock_bh(&ar->data_lock);
386 list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
387 if (peer->vdev_id != vdev_id)
388 continue;
389
390 ath10k_warn("removing stale peer %pM from vdev_id %d\n",
391 peer->addr, vdev_id);
392
393 list_del(&peer->list);
394 kfree(peer);
395 }
396 spin_unlock_bh(&ar->data_lock);
397 }
398
399 static void ath10k_peer_cleanup_all(struct ath10k *ar)
400 {
401 struct ath10k_peer *peer, *tmp;
402
403 lockdep_assert_held(&ar->conf_mutex);
404
405 spin_lock_bh(&ar->data_lock);
406 list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
407 list_del(&peer->list);
408 kfree(peer);
409 }
410 spin_unlock_bh(&ar->data_lock);
411 }
412
413 /************************/
414 /* Interface management */
415 /************************/
416
417 static inline int ath10k_vdev_setup_sync(struct ath10k *ar)
418 {
419 int ret;
420
421 lockdep_assert_held(&ar->conf_mutex);
422
423 ret = wait_for_completion_timeout(&ar->vdev_setup_done,
424 ATH10K_VDEV_SETUP_TIMEOUT_HZ);
425 if (ret == 0)
426 return -ETIMEDOUT;
427
428 return 0;
429 }
430
431 static int ath10k_vdev_start(struct ath10k_vif *arvif)
432 {
433 struct ath10k *ar = arvif->ar;
434 struct ieee80211_conf *conf = &ar->hw->conf;
435 struct ieee80211_channel *channel = conf->chandef.chan;
436 struct wmi_vdev_start_request_arg arg = {};
437 int ret = 0;
438
439 lockdep_assert_held(&ar->conf_mutex);
440
441 INIT_COMPLETION(ar->vdev_setup_done);
442
443 arg.vdev_id = arvif->vdev_id;
444 arg.dtim_period = arvif->dtim_period;
445 arg.bcn_intval = arvif->beacon_interval;
446
447 arg.channel.freq = channel->center_freq;
448
449 arg.channel.band_center_freq1 = conf->chandef.center_freq1;
450
451 arg.channel.mode = chan_to_phymode(&conf->chandef);
452
453 arg.channel.min_power = channel->max_power * 3;
454 arg.channel.max_power = channel->max_power * 4;
455 arg.channel.max_reg_power = channel->max_reg_power * 4;
456 arg.channel.max_antenna_gain = channel->max_antenna_gain;
457
458 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
459 arg.ssid = arvif->u.ap.ssid;
460 arg.ssid_len = arvif->u.ap.ssid_len;
461 arg.hidden_ssid = arvif->u.ap.hidden_ssid;
462 } else if (arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
463 arg.ssid = arvif->vif->bss_conf.ssid;
464 arg.ssid_len = arvif->vif->bss_conf.ssid_len;
465 }
466
467 ath10k_dbg(ATH10K_DBG_MAC,
468 "mac vdev %d start center_freq %d phymode %s\n",
469 arg.vdev_id, arg.channel.freq,
470 ath10k_wmi_phymode_str(arg.channel.mode));
471
472 ret = ath10k_wmi_vdev_start(ar, &arg);
473 if (ret) {
474 ath10k_warn("WMI vdev start failed: ret %d\n", ret);
475 return ret;
476 }
477
478 ret = ath10k_vdev_setup_sync(ar);
479 if (ret) {
480 ath10k_warn("vdev setup failed %d\n", ret);
481 return ret;
482 }
483
484 return ret;
485 }
486
487 static int ath10k_vdev_stop(struct ath10k_vif *arvif)
488 {
489 struct ath10k *ar = arvif->ar;
490 int ret;
491
492 lockdep_assert_held(&ar->conf_mutex);
493
494 INIT_COMPLETION(ar->vdev_setup_done);
495
496 ret = ath10k_wmi_vdev_stop(ar, arvif->vdev_id);
497 if (ret) {
498 ath10k_warn("WMI vdev stop failed: ret %d\n", ret);
499 return ret;
500 }
501
502 ret = ath10k_vdev_setup_sync(ar);
503 if (ret) {
504 ath10k_warn("vdev setup failed %d\n", ret);
505 return ret;
506 }
507
508 return ret;
509 }
510
511 static int ath10k_monitor_start(struct ath10k *ar, int vdev_id)
512 {
513 struct ieee80211_channel *channel = ar->hw->conf.chandef.chan;
514 struct wmi_vdev_start_request_arg arg = {};
515 int ret = 0;
516
517 lockdep_assert_held(&ar->conf_mutex);
518
519 arg.vdev_id = vdev_id;
520 arg.channel.freq = channel->center_freq;
521 arg.channel.band_center_freq1 = ar->hw->conf.chandef.center_freq1;
522
523 /* TODO setup this dynamically, what in case we
524 don't have any vifs? */
525 arg.channel.mode = chan_to_phymode(&ar->hw->conf.chandef);
526
527 arg.channel.min_power = channel->max_power * 3;
528 arg.channel.max_power = channel->max_power * 4;
529 arg.channel.max_reg_power = channel->max_reg_power * 4;
530 arg.channel.max_antenna_gain = channel->max_antenna_gain;
531
532 ret = ath10k_wmi_vdev_start(ar, &arg);
533 if (ret) {
534 ath10k_warn("Monitor vdev start failed: ret %d\n", ret);
535 return ret;
536 }
537
538 ret = ath10k_vdev_setup_sync(ar);
539 if (ret) {
540 ath10k_warn("Monitor vdev setup failed %d\n", ret);
541 return ret;
542 }
543
544 ret = ath10k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
545 if (ret) {
546 ath10k_warn("Monitor vdev up failed: %d\n", ret);
547 goto vdev_stop;
548 }
549
550 ar->monitor_vdev_id = vdev_id;
551 ar->monitor_enabled = true;
552
553 return 0;
554
555 vdev_stop:
556 ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
557 if (ret)
558 ath10k_warn("Monitor vdev stop failed: %d\n", ret);
559
560 return ret;
561 }
562
563 static int ath10k_monitor_stop(struct ath10k *ar)
564 {
565 int ret = 0;
566
567 lockdep_assert_held(&ar->conf_mutex);
568
569 ret = ath10k_wmi_vdev_down(ar, ar->monitor_vdev_id);
570 if (ret)
571 ath10k_warn("Monitor vdev down failed: %d\n", ret);
572
573 ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
574 if (ret)
575 ath10k_warn("Monitor vdev stop failed: %d\n", ret);
576
577 ret = ath10k_vdev_setup_sync(ar);
578 if (ret)
579 ath10k_warn("Monitor_down sync failed: %d\n", ret);
580
581 ar->monitor_enabled = false;
582 return ret;
583 }
584
585 static int ath10k_monitor_create(struct ath10k *ar)
586 {
587 int bit, ret = 0;
588
589 lockdep_assert_held(&ar->conf_mutex);
590
591 if (ar->monitor_present) {
592 ath10k_warn("Monitor mode already enabled\n");
593 return 0;
594 }
595
596 bit = ffs(ar->free_vdev_map);
597 if (bit == 0) {
598 ath10k_warn("No free VDEV slots\n");
599 return -ENOMEM;
600 }
601
602 ar->monitor_vdev_id = bit - 1;
603 ar->free_vdev_map &= ~(1 << ar->monitor_vdev_id);
604
605 ret = ath10k_wmi_vdev_create(ar, ar->monitor_vdev_id,
606 WMI_VDEV_TYPE_MONITOR,
607 0, ar->mac_addr);
608 if (ret) {
609 ath10k_warn("WMI vdev monitor create failed: ret %d\n", ret);
610 goto vdev_fail;
611 }
612
613 ath10k_dbg(ATH10K_DBG_MAC, "mac monitor vdev %d created\n",
614 ar->monitor_vdev_id);
615
616 ar->monitor_present = true;
617 return 0;
618
619 vdev_fail:
620 /*
621 * Restore the ID to the global map.
622 */
623 ar->free_vdev_map |= 1 << (ar->monitor_vdev_id);
624 return ret;
625 }
626
627 static int ath10k_monitor_destroy(struct ath10k *ar)
628 {
629 int ret = 0;
630
631 lockdep_assert_held(&ar->conf_mutex);
632
633 if (!ar->monitor_present)
634 return 0;
635
636 ret = ath10k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
637 if (ret) {
638 ath10k_warn("WMI vdev monitor delete failed: %d\n", ret);
639 return ret;
640 }
641
642 ar->free_vdev_map |= 1 << (ar->monitor_vdev_id);
643 ar->monitor_present = false;
644
645 ath10k_dbg(ATH10K_DBG_MAC, "mac monitor vdev %d deleted\n",
646 ar->monitor_vdev_id);
647 return ret;
648 }
649
650 static void ath10k_control_beaconing(struct ath10k_vif *arvif,
651 struct ieee80211_bss_conf *info)
652 {
653 int ret = 0;
654
655 lockdep_assert_held(&arvif->ar->conf_mutex);
656
657 if (!info->enable_beacon) {
658 ath10k_vdev_stop(arvif);
659 return;
660 }
661
662 arvif->tx_seq_no = 0x1000;
663
664 ret = ath10k_vdev_start(arvif);
665 if (ret)
666 return;
667
668 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, 0, info->bssid);
669 if (ret) {
670 ath10k_warn("Failed to bring up VDEV: %d\n",
671 arvif->vdev_id);
672 return;
673 }
674 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
675 }
676
677 static void ath10k_control_ibss(struct ath10k_vif *arvif,
678 struct ieee80211_bss_conf *info,
679 const u8 self_peer[ETH_ALEN])
680 {
681 u32 vdev_param;
682 int ret = 0;
683
684 lockdep_assert_held(&arvif->ar->conf_mutex);
685
686 if (!info->ibss_joined) {
687 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, self_peer);
688 if (ret)
689 ath10k_warn("Failed to delete IBSS self peer:%pM for VDEV:%d ret:%d\n",
690 self_peer, arvif->vdev_id, ret);
691
692 if (is_zero_ether_addr(arvif->u.ibss.bssid))
693 return;
694
695 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id,
696 arvif->u.ibss.bssid);
697 if (ret) {
698 ath10k_warn("Failed to delete IBSS BSSID peer:%pM for VDEV:%d ret:%d\n",
699 arvif->u.ibss.bssid, arvif->vdev_id, ret);
700 return;
701 }
702
703 memset(arvif->u.ibss.bssid, 0, ETH_ALEN);
704
705 return;
706 }
707
708 ret = ath10k_peer_create(arvif->ar, arvif->vdev_id, self_peer);
709 if (ret) {
710 ath10k_warn("Failed to create IBSS self peer:%pM for VDEV:%d ret:%d\n",
711 self_peer, arvif->vdev_id, ret);
712 return;
713 }
714
715 vdev_param = arvif->ar->wmi.vdev_param->atim_window;
716 ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id, vdev_param,
717 ATH10K_DEFAULT_ATIM);
718 if (ret)
719 ath10k_warn("Failed to set IBSS ATIM for VDEV:%d ret:%d\n",
720 arvif->vdev_id, ret);
721 }
722
723 /*
724 * Review this when mac80211 gains per-interface powersave support.
725 */
726 static void ath10k_ps_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
727 {
728 struct ath10k_generic_iter *ar_iter = data;
729 struct ieee80211_conf *conf = &ar_iter->ar->hw->conf;
730 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
731 enum wmi_sta_powersave_param param;
732 enum wmi_sta_ps_mode psmode;
733 int ret;
734
735 lockdep_assert_held(&arvif->ar->conf_mutex);
736
737 if (vif->type != NL80211_IFTYPE_STATION)
738 return;
739
740 if (conf->flags & IEEE80211_CONF_PS) {
741 psmode = WMI_STA_PS_MODE_ENABLED;
742 param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
743
744 ret = ath10k_wmi_set_sta_ps_param(ar_iter->ar,
745 arvif->vdev_id,
746 param,
747 conf->dynamic_ps_timeout);
748 if (ret) {
749 ath10k_warn("Failed to set inactivity time for VDEV: %d\n",
750 arvif->vdev_id);
751 return;
752 }
753
754 ar_iter->ret = ret;
755 } else {
756 psmode = WMI_STA_PS_MODE_DISABLED;
757 }
758
759 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d psmode %s\n",
760 arvif->vdev_id, psmode ? "enable" : "disable");
761
762 ar_iter->ret = ath10k_wmi_set_psmode(ar_iter->ar, arvif->vdev_id,
763 psmode);
764 if (ar_iter->ret)
765 ath10k_warn("Failed to set PS Mode: %d for VDEV: %d\n",
766 psmode, arvif->vdev_id);
767 }
768
769 /**********************/
770 /* Station management */
771 /**********************/
772
773 static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
774 struct ath10k_vif *arvif,
775 struct ieee80211_sta *sta,
776 struct ieee80211_bss_conf *bss_conf,
777 struct wmi_peer_assoc_complete_arg *arg)
778 {
779 lockdep_assert_held(&ar->conf_mutex);
780
781 memcpy(arg->addr, sta->addr, ETH_ALEN);
782 arg->vdev_id = arvif->vdev_id;
783 arg->peer_aid = sta->aid;
784 arg->peer_flags |= WMI_PEER_AUTH;
785
786 if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
787 /*
788 * Seems FW have problems with Power Save in STA
789 * mode when we setup this parameter to high (eg. 5).
790 * Often we see that FW don't send NULL (with clean P flags)
791 * frame even there is info about buffered frames in beacons.
792 * Sometimes we have to wait more than 10 seconds before FW
793 * will wakeup. Often sending one ping from AP to our device
794 * just fail (more than 50%).
795 *
796 * Seems setting this FW parameter to 1 couse FW
797 * will check every beacon and will wakup immediately
798 * after detection buffered data.
799 */
800 arg->peer_listen_intval = 1;
801 else
802 arg->peer_listen_intval = ar->hw->conf.listen_interval;
803
804 arg->peer_num_spatial_streams = 1;
805
806 /*
807 * The assoc capabilities are available only in managed mode.
808 */
809 if (arvif->vdev_type == WMI_VDEV_TYPE_STA && bss_conf)
810 arg->peer_caps = bss_conf->assoc_capability;
811 }
812
813 static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
814 struct ath10k_vif *arvif,
815 struct wmi_peer_assoc_complete_arg *arg)
816 {
817 struct ieee80211_vif *vif = arvif->vif;
818 struct ieee80211_bss_conf *info = &vif->bss_conf;
819 struct cfg80211_bss *bss;
820 const u8 *rsnie = NULL;
821 const u8 *wpaie = NULL;
822
823 lockdep_assert_held(&ar->conf_mutex);
824
825 bss = cfg80211_get_bss(ar->hw->wiphy, ar->hw->conf.chandef.chan,
826 info->bssid, NULL, 0, 0, 0);
827 if (bss) {
828 const struct cfg80211_bss_ies *ies;
829
830 rcu_read_lock();
831 rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
832
833 ies = rcu_dereference(bss->ies);
834
835 wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
836 WLAN_OUI_TYPE_MICROSOFT_WPA,
837 ies->data,
838 ies->len);
839 rcu_read_unlock();
840 cfg80211_put_bss(ar->hw->wiphy, bss);
841 }
842
843 /* FIXME: base on RSN IE/WPA IE is a correct idea? */
844 if (rsnie || wpaie) {
845 ath10k_dbg(ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
846 arg->peer_flags |= WMI_PEER_NEED_PTK_4_WAY;
847 }
848
849 if (wpaie) {
850 ath10k_dbg(ATH10K_DBG_WMI, "%s: wpa ie found\n", __func__);
851 arg->peer_flags |= WMI_PEER_NEED_GTK_2_WAY;
852 }
853 }
854
855 static void ath10k_peer_assoc_h_rates(struct ath10k *ar,
856 struct ieee80211_sta *sta,
857 struct wmi_peer_assoc_complete_arg *arg)
858 {
859 struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
860 const struct ieee80211_supported_band *sband;
861 const struct ieee80211_rate *rates;
862 u32 ratemask;
863 int i;
864
865 lockdep_assert_held(&ar->conf_mutex);
866
867 sband = ar->hw->wiphy->bands[ar->hw->conf.chandef.chan->band];
868 ratemask = sta->supp_rates[ar->hw->conf.chandef.chan->band];
869 rates = sband->bitrates;
870
871 rateset->num_rates = 0;
872
873 for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
874 if (!(ratemask & 1))
875 continue;
876
877 rateset->rates[rateset->num_rates] = rates->hw_value;
878 rateset->num_rates++;
879 }
880 }
881
882 static void ath10k_peer_assoc_h_ht(struct ath10k *ar,
883 struct ieee80211_sta *sta,
884 struct wmi_peer_assoc_complete_arg *arg)
885 {
886 const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
887 int smps;
888 int i, n;
889
890 lockdep_assert_held(&ar->conf_mutex);
891
892 if (!ht_cap->ht_supported)
893 return;
894
895 arg->peer_flags |= WMI_PEER_HT;
896 arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
897 ht_cap->ampdu_factor)) - 1;
898
899 arg->peer_mpdu_density =
900 ath10k_parse_mpdudensity(ht_cap->ampdu_density);
901
902 arg->peer_ht_caps = ht_cap->cap;
903 arg->peer_rate_caps |= WMI_RC_HT_FLAG;
904
905 if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
906 arg->peer_flags |= WMI_PEER_LDPC;
907
908 if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
909 arg->peer_flags |= WMI_PEER_40MHZ;
910 arg->peer_rate_caps |= WMI_RC_CW40_FLAG;
911 }
912
913 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
914 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
915
916 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
917 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
918
919 if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
920 arg->peer_rate_caps |= WMI_RC_TX_STBC_FLAG;
921 arg->peer_flags |= WMI_PEER_STBC;
922 }
923
924 if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
925 u32 stbc;
926 stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
927 stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
928 stbc = stbc << WMI_RC_RX_STBC_FLAG_S;
929 arg->peer_rate_caps |= stbc;
930 arg->peer_flags |= WMI_PEER_STBC;
931 }
932
933 smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
934 smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
935
936 if (smps == WLAN_HT_CAP_SM_PS_STATIC) {
937 arg->peer_flags |= WMI_PEER_SPATIAL_MUX;
938 arg->peer_flags |= WMI_PEER_STATIC_MIMOPS;
939 } else if (smps == WLAN_HT_CAP_SM_PS_DYNAMIC) {
940 arg->peer_flags |= WMI_PEER_SPATIAL_MUX;
941 arg->peer_flags |= WMI_PEER_DYN_MIMOPS;
942 }
943
944 if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
945 arg->peer_rate_caps |= WMI_RC_TS_FLAG;
946 else if (ht_cap->mcs.rx_mask[1])
947 arg->peer_rate_caps |= WMI_RC_DS_FLAG;
948
949 for (i = 0, n = 0; i < IEEE80211_HT_MCS_MASK_LEN*8; i++)
950 if (ht_cap->mcs.rx_mask[i/8] & (1 << i%8))
951 arg->peer_ht_rates.rates[n++] = i;
952
953 arg->peer_ht_rates.num_rates = n;
954 arg->peer_num_spatial_streams = max((n+7) / 8, 1);
955
956 ath10k_dbg(ATH10K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
957 arg->addr,
958 arg->peer_ht_rates.num_rates,
959 arg->peer_num_spatial_streams);
960 }
961
962 static void ath10k_peer_assoc_h_qos_ap(struct ath10k *ar,
963 struct ath10k_vif *arvif,
964 struct ieee80211_sta *sta,
965 struct ieee80211_bss_conf *bss_conf,
966 struct wmi_peer_assoc_complete_arg *arg)
967 {
968 u32 uapsd = 0;
969 u32 max_sp = 0;
970
971 lockdep_assert_held(&ar->conf_mutex);
972
973 if (sta->wme)
974 arg->peer_flags |= WMI_PEER_QOS;
975
976 if (sta->wme && sta->uapsd_queues) {
977 ath10k_dbg(ATH10K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
978 sta->uapsd_queues, sta->max_sp);
979
980 arg->peer_flags |= WMI_PEER_APSD;
981 arg->peer_rate_caps |= WMI_RC_UAPSD_FLAG;
982
983 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
984 uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
985 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
986 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
987 uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
988 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
989 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
990 uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
991 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
992 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
993 uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
994 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
995
996
997 if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
998 max_sp = sta->max_sp;
999
1000 ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1001 sta->addr,
1002 WMI_AP_PS_PEER_PARAM_UAPSD,
1003 uapsd);
1004
1005 ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1006 sta->addr,
1007 WMI_AP_PS_PEER_PARAM_MAX_SP,
1008 max_sp);
1009
1010 /* TODO setup this based on STA listen interval and
1011 beacon interval. Currently we don't know
1012 sta->listen_interval - mac80211 patch required.
1013 Currently use 10 seconds */
1014 ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1015 sta->addr,
1016 WMI_AP_PS_PEER_PARAM_AGEOUT_TIME,
1017 10);
1018 }
1019 }
1020
1021 static void ath10k_peer_assoc_h_qos_sta(struct ath10k *ar,
1022 struct ath10k_vif *arvif,
1023 struct ieee80211_sta *sta,
1024 struct ieee80211_bss_conf *bss_conf,
1025 struct wmi_peer_assoc_complete_arg *arg)
1026 {
1027 if (bss_conf->qos)
1028 arg->peer_flags |= WMI_PEER_QOS;
1029 }
1030
1031 static void ath10k_peer_assoc_h_vht(struct ath10k *ar,
1032 struct ieee80211_sta *sta,
1033 struct wmi_peer_assoc_complete_arg *arg)
1034 {
1035 const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
1036
1037 if (!vht_cap->vht_supported)
1038 return;
1039
1040 arg->peer_flags |= WMI_PEER_VHT;
1041
1042 arg->peer_vht_caps = vht_cap->cap;
1043
1044 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1045 arg->peer_flags |= WMI_PEER_80MHZ;
1046
1047 arg->peer_vht_rates.rx_max_rate =
1048 __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
1049 arg->peer_vht_rates.rx_mcs_set =
1050 __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
1051 arg->peer_vht_rates.tx_max_rate =
1052 __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
1053 arg->peer_vht_rates.tx_mcs_set =
1054 __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
1055
1056 ath10k_dbg(ATH10K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
1057 sta->addr, arg->peer_max_mpdu, arg->peer_flags);
1058 }
1059
1060 static void ath10k_peer_assoc_h_qos(struct ath10k *ar,
1061 struct ath10k_vif *arvif,
1062 struct ieee80211_sta *sta,
1063 struct ieee80211_bss_conf *bss_conf,
1064 struct wmi_peer_assoc_complete_arg *arg)
1065 {
1066 switch (arvif->vdev_type) {
1067 case WMI_VDEV_TYPE_AP:
1068 ath10k_peer_assoc_h_qos_ap(ar, arvif, sta, bss_conf, arg);
1069 break;
1070 case WMI_VDEV_TYPE_STA:
1071 ath10k_peer_assoc_h_qos_sta(ar, arvif, sta, bss_conf, arg);
1072 break;
1073 default:
1074 break;
1075 }
1076 }
1077
1078 static void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
1079 struct ath10k_vif *arvif,
1080 struct ieee80211_sta *sta,
1081 struct wmi_peer_assoc_complete_arg *arg)
1082 {
1083 enum wmi_phy_mode phymode = MODE_UNKNOWN;
1084
1085 switch (ar->hw->conf.chandef.chan->band) {
1086 case IEEE80211_BAND_2GHZ:
1087 if (sta->ht_cap.ht_supported) {
1088 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1089 phymode = MODE_11NG_HT40;
1090 else
1091 phymode = MODE_11NG_HT20;
1092 } else {
1093 phymode = MODE_11G;
1094 }
1095
1096 break;
1097 case IEEE80211_BAND_5GHZ:
1098 /*
1099 * Check VHT first.
1100 */
1101 if (sta->vht_cap.vht_supported) {
1102 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1103 phymode = MODE_11AC_VHT80;
1104 else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1105 phymode = MODE_11AC_VHT40;
1106 else if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
1107 phymode = MODE_11AC_VHT20;
1108 } else if (sta->ht_cap.ht_supported) {
1109 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1110 phymode = MODE_11NA_HT40;
1111 else
1112 phymode = MODE_11NA_HT20;
1113 } else {
1114 phymode = MODE_11A;
1115 }
1116
1117 break;
1118 default:
1119 break;
1120 }
1121
1122 ath10k_dbg(ATH10K_DBG_MAC, "mac peer %pM phymode %s\n",
1123 sta->addr, ath10k_wmi_phymode_str(phymode));
1124
1125 arg->peer_phymode = phymode;
1126 WARN_ON(phymode == MODE_UNKNOWN);
1127 }
1128
1129 static int ath10k_peer_assoc(struct ath10k *ar,
1130 struct ath10k_vif *arvif,
1131 struct ieee80211_sta *sta,
1132 struct ieee80211_bss_conf *bss_conf)
1133 {
1134 struct wmi_peer_assoc_complete_arg arg;
1135
1136 lockdep_assert_held(&ar->conf_mutex);
1137
1138 memset(&arg, 0, sizeof(struct wmi_peer_assoc_complete_arg));
1139
1140 ath10k_peer_assoc_h_basic(ar, arvif, sta, bss_conf, &arg);
1141 ath10k_peer_assoc_h_crypto(ar, arvif, &arg);
1142 ath10k_peer_assoc_h_rates(ar, sta, &arg);
1143 ath10k_peer_assoc_h_ht(ar, sta, &arg);
1144 ath10k_peer_assoc_h_vht(ar, sta, &arg);
1145 ath10k_peer_assoc_h_qos(ar, arvif, sta, bss_conf, &arg);
1146 ath10k_peer_assoc_h_phymode(ar, arvif, sta, &arg);
1147
1148 return ath10k_wmi_peer_assoc(ar, &arg);
1149 }
1150
1151 /* can be called only in mac80211 callbacks due to `key_count` usage */
1152 static void ath10k_bss_assoc(struct ieee80211_hw *hw,
1153 struct ieee80211_vif *vif,
1154 struct ieee80211_bss_conf *bss_conf)
1155 {
1156 struct ath10k *ar = hw->priv;
1157 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1158 struct ieee80211_sta *ap_sta;
1159 int ret;
1160
1161 lockdep_assert_held(&ar->conf_mutex);
1162
1163 rcu_read_lock();
1164
1165 ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
1166 if (!ap_sta) {
1167 ath10k_warn("Failed to find station entry for %pM\n",
1168 bss_conf->bssid);
1169 rcu_read_unlock();
1170 return;
1171 }
1172
1173 ret = ath10k_peer_assoc(ar, arvif, ap_sta, bss_conf);
1174 if (ret) {
1175 ath10k_warn("Peer assoc failed for %pM\n", bss_conf->bssid);
1176 rcu_read_unlock();
1177 return;
1178 }
1179
1180 rcu_read_unlock();
1181
1182 ath10k_dbg(ATH10K_DBG_MAC,
1183 "mac vdev %d up (associated) bssid %pM aid %d\n",
1184 arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
1185
1186 ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, bss_conf->aid,
1187 bss_conf->bssid);
1188 if (ret)
1189 ath10k_warn("VDEV: %d up failed: ret %d\n",
1190 arvif->vdev_id, ret);
1191 }
1192
1193 /*
1194 * FIXME: flush TIDs
1195 */
1196 static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
1197 struct ieee80211_vif *vif)
1198 {
1199 struct ath10k *ar = hw->priv;
1200 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1201 int ret;
1202
1203 lockdep_assert_held(&ar->conf_mutex);
1204
1205 /*
1206 * For some reason, calling VDEV-DOWN before VDEV-STOP
1207 * makes the FW to send frames via HTT after disassociation.
1208 * No idea why this happens, even though VDEV-DOWN is supposed
1209 * to be analogous to link down, so just stop the VDEV.
1210 */
1211 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d stop (disassociated\n",
1212 arvif->vdev_id);
1213
1214 /* FIXME: check return value */
1215 ret = ath10k_vdev_stop(arvif);
1216
1217 /*
1218 * If we don't call VDEV-DOWN after VDEV-STOP FW will remain active and
1219 * report beacons from previously associated network through HTT.
1220 * This in turn would spam mac80211 WARN_ON if we bring down all
1221 * interfaces as it expects there is no rx when no interface is
1222 * running.
1223 */
1224 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d down\n", arvif->vdev_id);
1225
1226 /* FIXME: why don't we print error if wmi call fails? */
1227 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
1228
1229 arvif->def_wep_key_index = 0;
1230 }
1231
1232 static int ath10k_station_assoc(struct ath10k *ar, struct ath10k_vif *arvif,
1233 struct ieee80211_sta *sta)
1234 {
1235 int ret = 0;
1236
1237 lockdep_assert_held(&ar->conf_mutex);
1238
1239 ret = ath10k_peer_assoc(ar, arvif, sta, NULL);
1240 if (ret) {
1241 ath10k_warn("WMI peer assoc failed for %pM\n", sta->addr);
1242 return ret;
1243 }
1244
1245 ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
1246 if (ret) {
1247 ath10k_warn("could not install peer wep keys (%d)\n", ret);
1248 return ret;
1249 }
1250
1251 return ret;
1252 }
1253
1254 static int ath10k_station_disassoc(struct ath10k *ar, struct ath10k_vif *arvif,
1255 struct ieee80211_sta *sta)
1256 {
1257 int ret = 0;
1258
1259 lockdep_assert_held(&ar->conf_mutex);
1260
1261 ret = ath10k_clear_peer_keys(arvif, sta->addr);
1262 if (ret) {
1263 ath10k_warn("could not clear all peer wep keys (%d)\n", ret);
1264 return ret;
1265 }
1266
1267 return ret;
1268 }
1269
1270 /**************/
1271 /* Regulatory */
1272 /**************/
1273
1274 static int ath10k_update_channel_list(struct ath10k *ar)
1275 {
1276 struct ieee80211_hw *hw = ar->hw;
1277 struct ieee80211_supported_band **bands;
1278 enum ieee80211_band band;
1279 struct ieee80211_channel *channel;
1280 struct wmi_scan_chan_list_arg arg = {0};
1281 struct wmi_channel_arg *ch;
1282 bool passive;
1283 int len;
1284 int ret;
1285 int i;
1286
1287 lockdep_assert_held(&ar->conf_mutex);
1288
1289 bands = hw->wiphy->bands;
1290 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1291 if (!bands[band])
1292 continue;
1293
1294 for (i = 0; i < bands[band]->n_channels; i++) {
1295 if (bands[band]->channels[i].flags &
1296 IEEE80211_CHAN_DISABLED)
1297 continue;
1298
1299 arg.n_channels++;
1300 }
1301 }
1302
1303 len = sizeof(struct wmi_channel_arg) * arg.n_channels;
1304 arg.channels = kzalloc(len, GFP_KERNEL);
1305 if (!arg.channels)
1306 return -ENOMEM;
1307
1308 ch = arg.channels;
1309 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1310 if (!bands[band])
1311 continue;
1312
1313 for (i = 0; i < bands[band]->n_channels; i++) {
1314 channel = &bands[band]->channels[i];
1315
1316 if (channel->flags & IEEE80211_CHAN_DISABLED)
1317 continue;
1318
1319 ch->allow_ht = true;
1320
1321 /* FIXME: when should we really allow VHT? */
1322 ch->allow_vht = true;
1323
1324 ch->allow_ibss =
1325 !(channel->flags & IEEE80211_CHAN_NO_IBSS);
1326
1327 ch->ht40plus =
1328 !(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
1329
1330 passive = channel->flags & IEEE80211_CHAN_PASSIVE_SCAN;
1331 ch->passive = passive;
1332
1333 ch->freq = channel->center_freq;
1334 ch->min_power = channel->max_power * 3;
1335 ch->max_power = channel->max_power * 4;
1336 ch->max_reg_power = channel->max_reg_power * 4;
1337 ch->max_antenna_gain = channel->max_antenna_gain;
1338 ch->reg_class_id = 0; /* FIXME */
1339
1340 /* FIXME: why use only legacy modes, why not any
1341 * HT/VHT modes? Would that even make any
1342 * difference? */
1343 if (channel->band == IEEE80211_BAND_2GHZ)
1344 ch->mode = MODE_11G;
1345 else
1346 ch->mode = MODE_11A;
1347
1348 if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
1349 continue;
1350
1351 ath10k_dbg(ATH10K_DBG_WMI,
1352 "mac channel [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
1353 ch - arg.channels, arg.n_channels,
1354 ch->freq, ch->max_power, ch->max_reg_power,
1355 ch->max_antenna_gain, ch->mode);
1356
1357 ch++;
1358 }
1359 }
1360
1361 ret = ath10k_wmi_scan_chan_list(ar, &arg);
1362 kfree(arg.channels);
1363
1364 return ret;
1365 }
1366
1367 static void ath10k_regd_update(struct ath10k *ar)
1368 {
1369 struct reg_dmn_pair_mapping *regpair;
1370 int ret;
1371
1372 lockdep_assert_held(&ar->conf_mutex);
1373
1374 ret = ath10k_update_channel_list(ar);
1375 if (ret)
1376 ath10k_warn("could not update channel list (%d)\n", ret);
1377
1378 regpair = ar->ath_common.regulatory.regpair;
1379
1380 /* Target allows setting up per-band regdomain but ath_common provides
1381 * a combined one only */
1382 ret = ath10k_wmi_pdev_set_regdomain(ar,
1383 regpair->regDmnEnum,
1384 regpair->regDmnEnum, /* 2ghz */
1385 regpair->regDmnEnum, /* 5ghz */
1386 regpair->reg_2ghz_ctl,
1387 regpair->reg_5ghz_ctl);
1388 if (ret)
1389 ath10k_warn("could not set pdev regdomain (%d)\n", ret);
1390 }
1391
1392 static void ath10k_reg_notifier(struct wiphy *wiphy,
1393 struct regulatory_request *request)
1394 {
1395 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
1396 struct ath10k *ar = hw->priv;
1397
1398 ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
1399
1400 mutex_lock(&ar->conf_mutex);
1401 if (ar->state == ATH10K_STATE_ON)
1402 ath10k_regd_update(ar);
1403 mutex_unlock(&ar->conf_mutex);
1404 }
1405
1406 /***************/
1407 /* TX handlers */
1408 /***************/
1409
1410 /*
1411 * Frames sent to the FW have to be in "Native Wifi" format.
1412 * Strip the QoS field from the 802.11 header.
1413 */
1414 static void ath10k_tx_h_qos_workaround(struct ieee80211_hw *hw,
1415 struct ieee80211_tx_control *control,
1416 struct sk_buff *skb)
1417 {
1418 struct ieee80211_hdr *hdr = (void *)skb->data;
1419 u8 *qos_ctl;
1420
1421 if (!ieee80211_is_data_qos(hdr->frame_control))
1422 return;
1423
1424 qos_ctl = ieee80211_get_qos_ctl(hdr);
1425 memmove(skb->data + IEEE80211_QOS_CTL_LEN,
1426 skb->data, (void *)qos_ctl - (void *)skb->data);
1427 skb_pull(skb, IEEE80211_QOS_CTL_LEN);
1428 }
1429
1430 static void ath10k_tx_h_update_wep_key(struct sk_buff *skb)
1431 {
1432 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1433 struct ieee80211_vif *vif = info->control.vif;
1434 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1435 struct ath10k *ar = arvif->ar;
1436 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1437 struct ieee80211_key_conf *key = info->control.hw_key;
1438 u32 vdev_param;
1439 int ret;
1440
1441 if (!ieee80211_has_protected(hdr->frame_control))
1442 return;
1443
1444 if (!key)
1445 return;
1446
1447 if (key->cipher != WLAN_CIPHER_SUITE_WEP40 &&
1448 key->cipher != WLAN_CIPHER_SUITE_WEP104)
1449 return;
1450
1451 if (key->keyidx == arvif->def_wep_key_index)
1452 return;
1453
1454 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d keyidx %d\n",
1455 arvif->vdev_id, key->keyidx);
1456
1457 vdev_param = ar->wmi.vdev_param->def_keyid;
1458 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
1459 key->keyidx);
1460 if (ret) {
1461 ath10k_warn("could not update wep keyidx (%d)\n", ret);
1462 return;
1463 }
1464
1465 arvif->def_wep_key_index = key->keyidx;
1466 }
1467
1468 static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar, struct sk_buff *skb)
1469 {
1470 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1471 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1472 struct ieee80211_vif *vif = info->control.vif;
1473 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1474
1475 /* This is case only for P2P_GO */
1476 if (arvif->vdev_type != WMI_VDEV_TYPE_AP ||
1477 arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
1478 return;
1479
1480 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
1481 spin_lock_bh(&ar->data_lock);
1482 if (arvif->u.ap.noa_data)
1483 if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
1484 GFP_ATOMIC))
1485 memcpy(skb_put(skb, arvif->u.ap.noa_len),
1486 arvif->u.ap.noa_data,
1487 arvif->u.ap.noa_len);
1488 spin_unlock_bh(&ar->data_lock);
1489 }
1490 }
1491
1492 static void ath10k_tx_htt(struct ath10k *ar, struct sk_buff *skb)
1493 {
1494 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1495 int ret = 0;
1496
1497 if (ar->htt.target_version_major >= 3) {
1498 /* Since HTT 3.0 there is no separate mgmt tx command */
1499 ret = ath10k_htt_tx(&ar->htt, skb);
1500 goto exit;
1501 }
1502
1503 if (ieee80211_is_mgmt(hdr->frame_control)) {
1504 if (test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
1505 ar->fw_features)) {
1506 if (skb_queue_len(&ar->wmi_mgmt_tx_queue) >=
1507 ATH10K_MAX_NUM_MGMT_PENDING) {
1508 ath10k_warn("wmi mgmt_tx queue limit reached\n");
1509 ret = -EBUSY;
1510 goto exit;
1511 }
1512
1513 skb_queue_tail(&ar->wmi_mgmt_tx_queue, skb);
1514 ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
1515 } else {
1516 ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
1517 }
1518 } else if (!test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
1519 ar->fw_features) &&
1520 ieee80211_is_nullfunc(hdr->frame_control)) {
1521 /* FW does not report tx status properly for NullFunc frames
1522 * unless they are sent through mgmt tx path. mac80211 sends
1523 * those frames when it detects link/beacon loss and depends
1524 * on the tx status to be correct. */
1525 ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
1526 } else {
1527 ret = ath10k_htt_tx(&ar->htt, skb);
1528 }
1529
1530 exit:
1531 if (ret) {
1532 ath10k_warn("tx failed (%d). dropping packet.\n", ret);
1533 ieee80211_free_txskb(ar->hw, skb);
1534 }
1535 }
1536
1537 void ath10k_offchan_tx_purge(struct ath10k *ar)
1538 {
1539 struct sk_buff *skb;
1540
1541 for (;;) {
1542 skb = skb_dequeue(&ar->offchan_tx_queue);
1543 if (!skb)
1544 break;
1545
1546 ieee80211_free_txskb(ar->hw, skb);
1547 }
1548 }
1549
1550 void ath10k_offchan_tx_work(struct work_struct *work)
1551 {
1552 struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
1553 struct ath10k_peer *peer;
1554 struct ieee80211_hdr *hdr;
1555 struct sk_buff *skb;
1556 const u8 *peer_addr;
1557 int vdev_id;
1558 int ret;
1559
1560 /* FW requirement: We must create a peer before FW will send out
1561 * an offchannel frame. Otherwise the frame will be stuck and
1562 * never transmitted. We delete the peer upon tx completion.
1563 * It is unlikely that a peer for offchannel tx will already be
1564 * present. However it may be in some rare cases so account for that.
1565 * Otherwise we might remove a legitimate peer and break stuff. */
1566
1567 for (;;) {
1568 skb = skb_dequeue(&ar->offchan_tx_queue);
1569 if (!skb)
1570 break;
1571
1572 mutex_lock(&ar->conf_mutex);
1573
1574 ath10k_dbg(ATH10K_DBG_MAC, "mac offchannel skb %p\n",
1575 skb);
1576
1577 hdr = (struct ieee80211_hdr *)skb->data;
1578 peer_addr = ieee80211_get_DA(hdr);
1579 vdev_id = ATH10K_SKB_CB(skb)->vdev_id;
1580
1581 spin_lock_bh(&ar->data_lock);
1582 peer = ath10k_peer_find(ar, vdev_id, peer_addr);
1583 spin_unlock_bh(&ar->data_lock);
1584
1585 if (peer)
1586 /* FIXME: should this use ath10k_warn()? */
1587 ath10k_dbg(ATH10K_DBG_MAC, "peer %pM on vdev %d already present\n",
1588 peer_addr, vdev_id);
1589
1590 if (!peer) {
1591 ret = ath10k_peer_create(ar, vdev_id, peer_addr);
1592 if (ret)
1593 ath10k_warn("peer %pM on vdev %d not created (%d)\n",
1594 peer_addr, vdev_id, ret);
1595 }
1596
1597 spin_lock_bh(&ar->data_lock);
1598 INIT_COMPLETION(ar->offchan_tx_completed);
1599 ar->offchan_tx_skb = skb;
1600 spin_unlock_bh(&ar->data_lock);
1601
1602 ath10k_tx_htt(ar, skb);
1603
1604 ret = wait_for_completion_timeout(&ar->offchan_tx_completed,
1605 3 * HZ);
1606 if (ret <= 0)
1607 ath10k_warn("timed out waiting for offchannel skb %p\n",
1608 skb);
1609
1610 if (!peer) {
1611 ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
1612 if (ret)
1613 ath10k_warn("peer %pM on vdev %d not deleted (%d)\n",
1614 peer_addr, vdev_id, ret);
1615 }
1616
1617 mutex_unlock(&ar->conf_mutex);
1618 }
1619 }
1620
1621 void ath10k_mgmt_over_wmi_tx_purge(struct ath10k *ar)
1622 {
1623 struct sk_buff *skb;
1624
1625 for (;;) {
1626 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
1627 if (!skb)
1628 break;
1629
1630 ieee80211_free_txskb(ar->hw, skb);
1631 }
1632 }
1633
1634 void ath10k_mgmt_over_wmi_tx_work(struct work_struct *work)
1635 {
1636 struct ath10k *ar = container_of(work, struct ath10k, wmi_mgmt_tx_work);
1637 struct sk_buff *skb;
1638 int ret;
1639
1640 for (;;) {
1641 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
1642 if (!skb)
1643 break;
1644
1645 ret = ath10k_wmi_mgmt_tx(ar, skb);
1646 if (ret)
1647 ath10k_warn("wmi mgmt_tx failed (%d)\n", ret);
1648 }
1649 }
1650
1651 /************/
1652 /* Scanning */
1653 /************/
1654
1655 /*
1656 * This gets called if we dont get a heart-beat during scan.
1657 * This may indicate the FW has hung and we need to abort the
1658 * scan manually to prevent cancel_hw_scan() from deadlocking
1659 */
1660 void ath10k_reset_scan(unsigned long ptr)
1661 {
1662 struct ath10k *ar = (struct ath10k *)ptr;
1663
1664 spin_lock_bh(&ar->data_lock);
1665 if (!ar->scan.in_progress) {
1666 spin_unlock_bh(&ar->data_lock);
1667 return;
1668 }
1669
1670 ath10k_warn("scan timeout. resetting. fw issue?\n");
1671
1672 if (ar->scan.is_roc)
1673 ieee80211_remain_on_channel_expired(ar->hw);
1674 else
1675 ieee80211_scan_completed(ar->hw, 1 /* aborted */);
1676
1677 ar->scan.in_progress = false;
1678 complete_all(&ar->scan.completed);
1679 spin_unlock_bh(&ar->data_lock);
1680 }
1681
1682 static int ath10k_abort_scan(struct ath10k *ar)
1683 {
1684 struct wmi_stop_scan_arg arg = {
1685 .req_id = 1, /* FIXME */
1686 .req_type = WMI_SCAN_STOP_ONE,
1687 .u.scan_id = ATH10K_SCAN_ID,
1688 };
1689 int ret;
1690
1691 lockdep_assert_held(&ar->conf_mutex);
1692
1693 del_timer_sync(&ar->scan.timeout);
1694
1695 spin_lock_bh(&ar->data_lock);
1696 if (!ar->scan.in_progress) {
1697 spin_unlock_bh(&ar->data_lock);
1698 return 0;
1699 }
1700
1701 ar->scan.aborting = true;
1702 spin_unlock_bh(&ar->data_lock);
1703
1704 ret = ath10k_wmi_stop_scan(ar, &arg);
1705 if (ret) {
1706 ath10k_warn("could not submit wmi stop scan (%d)\n", ret);
1707 spin_lock_bh(&ar->data_lock);
1708 ar->scan.in_progress = false;
1709 ath10k_offchan_tx_purge(ar);
1710 spin_unlock_bh(&ar->data_lock);
1711 return -EIO;
1712 }
1713
1714 ret = wait_for_completion_timeout(&ar->scan.completed, 3*HZ);
1715 if (ret == 0)
1716 ath10k_warn("timed out while waiting for scan to stop\n");
1717
1718 /* scan completion may be done right after we timeout here, so let's
1719 * check the in_progress and tell mac80211 scan is completed. if we
1720 * don't do that and FW fails to send us scan completion indication
1721 * then userspace won't be able to scan anymore */
1722 ret = 0;
1723
1724 spin_lock_bh(&ar->data_lock);
1725 if (ar->scan.in_progress) {
1726 ath10k_warn("could not stop scan. its still in progress\n");
1727 ar->scan.in_progress = false;
1728 ath10k_offchan_tx_purge(ar);
1729 ret = -ETIMEDOUT;
1730 }
1731 spin_unlock_bh(&ar->data_lock);
1732
1733 return ret;
1734 }
1735
1736 static int ath10k_start_scan(struct ath10k *ar,
1737 const struct wmi_start_scan_arg *arg)
1738 {
1739 int ret;
1740
1741 lockdep_assert_held(&ar->conf_mutex);
1742
1743 ret = ath10k_wmi_start_scan(ar, arg);
1744 if (ret)
1745 return ret;
1746
1747 ret = wait_for_completion_timeout(&ar->scan.started, 1*HZ);
1748 if (ret == 0) {
1749 ath10k_abort_scan(ar);
1750 return ret;
1751 }
1752
1753 /* the scan can complete earlier, before we even
1754 * start the timer. in that case the timer handler
1755 * checks ar->scan.in_progress and bails out if its
1756 * false. Add a 200ms margin to account event/command
1757 * processing. */
1758 mod_timer(&ar->scan.timeout, jiffies +
1759 msecs_to_jiffies(arg->max_scan_time+200));
1760 return 0;
1761 }
1762
1763 /**********************/
1764 /* mac80211 callbacks */
1765 /**********************/
1766
1767 static void ath10k_tx(struct ieee80211_hw *hw,
1768 struct ieee80211_tx_control *control,
1769 struct sk_buff *skb)
1770 {
1771 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1772 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1773 struct ath10k *ar = hw->priv;
1774 struct ath10k_vif *arvif = NULL;
1775 u32 vdev_id = 0;
1776 u8 tid;
1777
1778 if (info->control.vif) {
1779 arvif = ath10k_vif_to_arvif(info->control.vif);
1780 vdev_id = arvif->vdev_id;
1781 } else if (ar->monitor_enabled) {
1782 vdev_id = ar->monitor_vdev_id;
1783 }
1784
1785 /* We should disable CCK RATE due to P2P */
1786 if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
1787 ath10k_dbg(ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
1788
1789 /* we must calculate tid before we apply qos workaround
1790 * as we'd lose the qos control field */
1791 tid = HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
1792 if (ieee80211_is_mgmt(hdr->frame_control)) {
1793 tid = HTT_DATA_TX_EXT_TID_MGMT;
1794 } else if (ieee80211_is_data_qos(hdr->frame_control) &&
1795 is_unicast_ether_addr(ieee80211_get_DA(hdr))) {
1796 u8 *qc = ieee80211_get_qos_ctl(hdr);
1797 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
1798 }
1799
1800 /* it makes no sense to process injected frames like that */
1801 if (info->control.vif &&
1802 info->control.vif->type != NL80211_IFTYPE_MONITOR) {
1803 ath10k_tx_h_qos_workaround(hw, control, skb);
1804 ath10k_tx_h_update_wep_key(skb);
1805 ath10k_tx_h_add_p2p_noa_ie(ar, skb);
1806 ath10k_tx_h_seq_no(skb);
1807 }
1808
1809 ATH10K_SKB_CB(skb)->vdev_id = vdev_id;
1810 ATH10K_SKB_CB(skb)->htt.is_offchan = false;
1811 ATH10K_SKB_CB(skb)->htt.tid = tid;
1812
1813 if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
1814 spin_lock_bh(&ar->data_lock);
1815 ATH10K_SKB_CB(skb)->htt.is_offchan = true;
1816 ATH10K_SKB_CB(skb)->vdev_id = ar->scan.vdev_id;
1817 spin_unlock_bh(&ar->data_lock);
1818
1819 ath10k_dbg(ATH10K_DBG_MAC, "queued offchannel skb %p\n", skb);
1820
1821 skb_queue_tail(&ar->offchan_tx_queue, skb);
1822 ieee80211_queue_work(hw, &ar->offchan_tx_work);
1823 return;
1824 }
1825
1826 ath10k_tx_htt(ar, skb);
1827 }
1828
1829 /*
1830 * Initialize various parameters with default vaules.
1831 */
1832 void ath10k_halt(struct ath10k *ar)
1833 {
1834 lockdep_assert_held(&ar->conf_mutex);
1835
1836 del_timer_sync(&ar->scan.timeout);
1837 ath10k_offchan_tx_purge(ar);
1838 ath10k_mgmt_over_wmi_tx_purge(ar);
1839 ath10k_peer_cleanup_all(ar);
1840 ath10k_core_stop(ar);
1841 ath10k_hif_power_down(ar);
1842
1843 spin_lock_bh(&ar->data_lock);
1844 if (ar->scan.in_progress) {
1845 del_timer(&ar->scan.timeout);
1846 ar->scan.in_progress = false;
1847 ieee80211_scan_completed(ar->hw, true);
1848 }
1849 spin_unlock_bh(&ar->data_lock);
1850 }
1851
1852 static int ath10k_start(struct ieee80211_hw *hw)
1853 {
1854 struct ath10k *ar = hw->priv;
1855 int ret = 0;
1856
1857 mutex_lock(&ar->conf_mutex);
1858
1859 if (ar->state != ATH10K_STATE_OFF &&
1860 ar->state != ATH10K_STATE_RESTARTING) {
1861 ret = -EINVAL;
1862 goto exit;
1863 }
1864
1865 ret = ath10k_hif_power_up(ar);
1866 if (ret) {
1867 ath10k_err("could not init hif (%d)\n", ret);
1868 ar->state = ATH10K_STATE_OFF;
1869 goto exit;
1870 }
1871
1872 ret = ath10k_core_start(ar);
1873 if (ret) {
1874 ath10k_err("could not init core (%d)\n", ret);
1875 ath10k_hif_power_down(ar);
1876 ar->state = ATH10K_STATE_OFF;
1877 goto exit;
1878 }
1879
1880 if (ar->state == ATH10K_STATE_OFF)
1881 ar->state = ATH10K_STATE_ON;
1882 else if (ar->state == ATH10K_STATE_RESTARTING)
1883 ar->state = ATH10K_STATE_RESTARTED;
1884
1885 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->pmf_qos, 1);
1886 if (ret)
1887 ath10k_warn("could not enable WMI_PDEV_PARAM_PMF_QOS (%d)\n",
1888 ret);
1889
1890 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->dynamic_bw, 0);
1891 if (ret)
1892 ath10k_warn("could not init WMI_PDEV_PARAM_DYNAMIC_BW (%d)\n",
1893 ret);
1894
1895 ath10k_regd_update(ar);
1896
1897 exit:
1898 mutex_unlock(&ar->conf_mutex);
1899 return 0;
1900 }
1901
1902 static void ath10k_stop(struct ieee80211_hw *hw)
1903 {
1904 struct ath10k *ar = hw->priv;
1905
1906 mutex_lock(&ar->conf_mutex);
1907 if (ar->state == ATH10K_STATE_ON ||
1908 ar->state == ATH10K_STATE_RESTARTED ||
1909 ar->state == ATH10K_STATE_WEDGED)
1910 ath10k_halt(ar);
1911
1912 ar->state = ATH10K_STATE_OFF;
1913 mutex_unlock(&ar->conf_mutex);
1914
1915 ath10k_mgmt_over_wmi_tx_purge(ar);
1916
1917 cancel_work_sync(&ar->offchan_tx_work);
1918 cancel_work_sync(&ar->wmi_mgmt_tx_work);
1919 cancel_work_sync(&ar->restart_work);
1920 }
1921
1922 static void ath10k_config_ps(struct ath10k *ar)
1923 {
1924 struct ath10k_generic_iter ar_iter;
1925
1926 lockdep_assert_held(&ar->conf_mutex);
1927
1928 /* During HW reconfiguration mac80211 reports all interfaces that were
1929 * running until reconfiguration was started. Since FW doesn't have any
1930 * vdevs at this point we must not iterate over this interface list.
1931 * This setting will be updated upon add_interface(). */
1932 if (ar->state == ATH10K_STATE_RESTARTED)
1933 return;
1934
1935 memset(&ar_iter, 0, sizeof(struct ath10k_generic_iter));
1936 ar_iter.ar = ar;
1937
1938 ieee80211_iterate_active_interfaces_atomic(
1939 ar->hw, IEEE80211_IFACE_ITER_NORMAL,
1940 ath10k_ps_iter, &ar_iter);
1941
1942 if (ar_iter.ret)
1943 ath10k_warn("failed to set ps config (%d)\n", ar_iter.ret);
1944 }
1945
1946 static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
1947 {
1948 struct ath10k *ar = hw->priv;
1949 struct ieee80211_conf *conf = &hw->conf;
1950 int ret = 0;
1951
1952 mutex_lock(&ar->conf_mutex);
1953
1954 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
1955 ath10k_dbg(ATH10K_DBG_MAC, "mac config channel %d mhz\n",
1956 conf->chandef.chan->center_freq);
1957 spin_lock_bh(&ar->data_lock);
1958 ar->rx_channel = conf->chandef.chan;
1959 spin_unlock_bh(&ar->data_lock);
1960 }
1961
1962 if (changed & IEEE80211_CONF_CHANGE_PS)
1963 ath10k_config_ps(ar);
1964
1965 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
1966 if (conf->flags & IEEE80211_CONF_MONITOR)
1967 ret = ath10k_monitor_create(ar);
1968 else
1969 ret = ath10k_monitor_destroy(ar);
1970 }
1971
1972 mutex_unlock(&ar->conf_mutex);
1973 return ret;
1974 }
1975
1976 /*
1977 * TODO:
1978 * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
1979 * because we will send mgmt frames without CCK. This requirement
1980 * for P2P_FIND/GO_NEG should be handled by checking CCK flag
1981 * in the TX packet.
1982 */
1983 static int ath10k_add_interface(struct ieee80211_hw *hw,
1984 struct ieee80211_vif *vif)
1985 {
1986 struct ath10k *ar = hw->priv;
1987 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1988 enum wmi_sta_powersave_param param;
1989 int ret = 0;
1990 u32 value;
1991 int bit;
1992 u32 vdev_param;
1993
1994 mutex_lock(&ar->conf_mutex);
1995
1996 memset(arvif, 0, sizeof(*arvif));
1997
1998 arvif->ar = ar;
1999 arvif->vif = vif;
2000
2001 if ((vif->type == NL80211_IFTYPE_MONITOR) && ar->monitor_present) {
2002 ath10k_warn("Only one monitor interface allowed\n");
2003 ret = -EBUSY;
2004 goto exit;
2005 }
2006
2007 bit = ffs(ar->free_vdev_map);
2008 if (bit == 0) {
2009 ret = -EBUSY;
2010 goto exit;
2011 }
2012
2013 arvif->vdev_id = bit - 1;
2014 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
2015 ar->free_vdev_map &= ~(1 << arvif->vdev_id);
2016
2017 if (ar->p2p)
2018 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
2019
2020 switch (vif->type) {
2021 case NL80211_IFTYPE_UNSPECIFIED:
2022 case NL80211_IFTYPE_STATION:
2023 arvif->vdev_type = WMI_VDEV_TYPE_STA;
2024 if (vif->p2p)
2025 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
2026 break;
2027 case NL80211_IFTYPE_ADHOC:
2028 arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
2029 break;
2030 case NL80211_IFTYPE_AP:
2031 arvif->vdev_type = WMI_VDEV_TYPE_AP;
2032
2033 if (vif->p2p)
2034 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
2035 break;
2036 case NL80211_IFTYPE_MONITOR:
2037 arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
2038 break;
2039 default:
2040 WARN_ON(1);
2041 break;
2042 }
2043
2044 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d\n",
2045 arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype);
2046
2047 ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
2048 arvif->vdev_subtype, vif->addr);
2049 if (ret) {
2050 ath10k_warn("WMI vdev create failed: ret %d\n", ret);
2051 goto exit;
2052 }
2053
2054 vdev_param = ar->wmi.vdev_param->def_keyid;
2055 ret = ath10k_wmi_vdev_set_param(ar, 0, vdev_param,
2056 arvif->def_wep_key_index);
2057 if (ret)
2058 ath10k_warn("Failed to set default keyid: %d\n", ret);
2059
2060 vdev_param = ar->wmi.vdev_param->tx_encap_type;
2061 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
2062 ATH10K_HW_TXRX_NATIVE_WIFI);
2063 if (ret)
2064 ath10k_warn("Failed to set TX encap: %d\n", ret);
2065
2066 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
2067 ret = ath10k_peer_create(ar, arvif->vdev_id, vif->addr);
2068 if (ret) {
2069 ath10k_warn("Failed to create peer for AP: %d\n", ret);
2070 goto exit;
2071 }
2072 }
2073
2074 if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
2075 param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
2076 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
2077 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
2078 param, value);
2079 if (ret)
2080 ath10k_warn("Failed to set RX wake policy: %d\n", ret);
2081
2082 param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
2083 value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
2084 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
2085 param, value);
2086 if (ret)
2087 ath10k_warn("Failed to set TX wake thresh: %d\n", ret);
2088
2089 param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
2090 value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
2091 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
2092 param, value);
2093 if (ret)
2094 ath10k_warn("Failed to set PSPOLL count: %d\n", ret);
2095 }
2096
2097 ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
2098 if (ret)
2099 ath10k_warn("failed to set rts threshold for vdev %d (%d)\n",
2100 arvif->vdev_id, ret);
2101
2102 ret = ath10k_mac_set_frag(arvif, ar->hw->wiphy->frag_threshold);
2103 if (ret)
2104 ath10k_warn("failed to set frag threshold for vdev %d (%d)\n",
2105 arvif->vdev_id, ret);
2106
2107 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
2108 ar->monitor_present = true;
2109
2110 exit:
2111 mutex_unlock(&ar->conf_mutex);
2112 return ret;
2113 }
2114
2115 static void ath10k_remove_interface(struct ieee80211_hw *hw,
2116 struct ieee80211_vif *vif)
2117 {
2118 struct ath10k *ar = hw->priv;
2119 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2120 int ret;
2121
2122 mutex_lock(&ar->conf_mutex);
2123
2124 spin_lock_bh(&ar->data_lock);
2125 if (arvif->beacon) {
2126 dev_kfree_skb_any(arvif->beacon);
2127 arvif->beacon = NULL;
2128 }
2129 spin_unlock_bh(&ar->data_lock);
2130
2131 ar->free_vdev_map |= 1 << (arvif->vdev_id);
2132
2133 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
2134 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, vif->addr);
2135 if (ret)
2136 ath10k_warn("Failed to remove peer for AP: %d\n", ret);
2137
2138 kfree(arvif->u.ap.noa_data);
2139 }
2140
2141 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev delete %d (remove interface)\n",
2142 arvif->vdev_id);
2143
2144 ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
2145 if (ret)
2146 ath10k_warn("WMI vdev delete failed: %d\n", ret);
2147
2148 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
2149 ar->monitor_present = false;
2150
2151 ath10k_peer_cleanup(ar, arvif->vdev_id);
2152
2153 mutex_unlock(&ar->conf_mutex);
2154 }
2155
2156 /*
2157 * FIXME: Has to be verified.
2158 */
2159 #define SUPPORTED_FILTERS \
2160 (FIF_PROMISC_IN_BSS | \
2161 FIF_ALLMULTI | \
2162 FIF_CONTROL | \
2163 FIF_PSPOLL | \
2164 FIF_OTHER_BSS | \
2165 FIF_BCN_PRBRESP_PROMISC | \
2166 FIF_PROBE_REQ | \
2167 FIF_FCSFAIL)
2168
2169 static void ath10k_configure_filter(struct ieee80211_hw *hw,
2170 unsigned int changed_flags,
2171 unsigned int *total_flags,
2172 u64 multicast)
2173 {
2174 struct ath10k *ar = hw->priv;
2175 int ret;
2176
2177 mutex_lock(&ar->conf_mutex);
2178
2179 changed_flags &= SUPPORTED_FILTERS;
2180 *total_flags &= SUPPORTED_FILTERS;
2181 ar->filter_flags = *total_flags;
2182
2183 if ((ar->filter_flags & FIF_PROMISC_IN_BSS) &&
2184 !ar->monitor_enabled) {
2185 ath10k_dbg(ATH10K_DBG_MAC, "mac monitor %d start\n",
2186 ar->monitor_vdev_id);
2187
2188 ret = ath10k_monitor_start(ar, ar->monitor_vdev_id);
2189 if (ret)
2190 ath10k_warn("Unable to start monitor mode\n");
2191 } else if (!(ar->filter_flags & FIF_PROMISC_IN_BSS) &&
2192 ar->monitor_enabled) {
2193 ath10k_dbg(ATH10K_DBG_MAC, "mac monitor %d stop\n",
2194 ar->monitor_vdev_id);
2195
2196 ret = ath10k_monitor_stop(ar);
2197 if (ret)
2198 ath10k_warn("Unable to stop monitor mode\n");
2199 }
2200
2201 mutex_unlock(&ar->conf_mutex);
2202 }
2203
2204 static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
2205 struct ieee80211_vif *vif,
2206 struct ieee80211_bss_conf *info,
2207 u32 changed)
2208 {
2209 struct ath10k *ar = hw->priv;
2210 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2211 int ret = 0;
2212 u32 vdev_param, pdev_param;
2213
2214 mutex_lock(&ar->conf_mutex);
2215
2216 if (changed & BSS_CHANGED_IBSS)
2217 ath10k_control_ibss(arvif, info, vif->addr);
2218
2219 if (changed & BSS_CHANGED_BEACON_INT) {
2220 arvif->beacon_interval = info->beacon_int;
2221 vdev_param = ar->wmi.vdev_param->beacon_interval;
2222 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
2223 arvif->beacon_interval);
2224 ath10k_dbg(ATH10K_DBG_MAC,
2225 "mac vdev %d beacon_interval %d\n",
2226 arvif->vdev_id, arvif->beacon_interval);
2227
2228 if (ret)
2229 ath10k_warn("Failed to set beacon interval for VDEV: %d\n",
2230 arvif->vdev_id);
2231 }
2232
2233 if (changed & BSS_CHANGED_BEACON) {
2234 ath10k_dbg(ATH10K_DBG_MAC,
2235 "vdev %d set beacon tx mode to staggered\n",
2236 arvif->vdev_id);
2237
2238 pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
2239 ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
2240 WMI_BEACON_STAGGERED_MODE);
2241 if (ret)
2242 ath10k_warn("Failed to set beacon mode for VDEV: %d\n",
2243 arvif->vdev_id);
2244 }
2245
2246 if (changed & BSS_CHANGED_BEACON_INFO) {
2247 arvif->dtim_period = info->dtim_period;
2248
2249 ath10k_dbg(ATH10K_DBG_MAC,
2250 "mac vdev %d dtim_period %d\n",
2251 arvif->vdev_id, arvif->dtim_period);
2252
2253 vdev_param = ar->wmi.vdev_param->dtim_period;
2254 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
2255 arvif->dtim_period);
2256 if (ret)
2257 ath10k_warn("Failed to set dtim period for VDEV: %d\n",
2258 arvif->vdev_id);
2259 }
2260
2261 if (changed & BSS_CHANGED_SSID &&
2262 vif->type == NL80211_IFTYPE_AP) {
2263 arvif->u.ap.ssid_len = info->ssid_len;
2264 if (info->ssid_len)
2265 memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
2266 arvif->u.ap.hidden_ssid = info->hidden_ssid;
2267 }
2268
2269 if (changed & BSS_CHANGED_BSSID) {
2270 if (!is_zero_ether_addr(info->bssid)) {
2271 ath10k_dbg(ATH10K_DBG_MAC,
2272 "mac vdev %d create peer %pM\n",
2273 arvif->vdev_id, info->bssid);
2274
2275 ret = ath10k_peer_create(ar, arvif->vdev_id,
2276 info->bssid);
2277 if (ret)
2278 ath10k_warn("Failed to add peer: %pM for VDEV: %d\n",
2279 info->bssid, arvif->vdev_id);
2280
2281 if (vif->type == NL80211_IFTYPE_STATION) {
2282 /*
2283 * this is never erased as we it for crypto key
2284 * clearing; this is FW requirement
2285 */
2286 memcpy(arvif->u.sta.bssid, info->bssid,
2287 ETH_ALEN);
2288
2289 ath10k_dbg(ATH10K_DBG_MAC,
2290 "mac vdev %d start %pM\n",
2291 arvif->vdev_id, info->bssid);
2292
2293 /* FIXME: check return value */
2294 ret = ath10k_vdev_start(arvif);
2295 }
2296
2297 /*
2298 * Mac80211 does not keep IBSS bssid when leaving IBSS,
2299 * so driver need to store it. It is needed when leaving
2300 * IBSS in order to remove BSSID peer.
2301 */
2302 if (vif->type == NL80211_IFTYPE_ADHOC)
2303 memcpy(arvif->u.ibss.bssid, info->bssid,
2304 ETH_ALEN);
2305 }
2306 }
2307
2308 if (changed & BSS_CHANGED_BEACON_ENABLED)
2309 ath10k_control_beaconing(arvif, info);
2310
2311 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
2312 u32 cts_prot;
2313 if (info->use_cts_prot)
2314 cts_prot = 1;
2315 else
2316 cts_prot = 0;
2317
2318 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d cts_prot %d\n",
2319 arvif->vdev_id, cts_prot);
2320
2321 vdev_param = ar->wmi.vdev_param->enable_rtscts;
2322 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
2323 cts_prot);
2324 if (ret)
2325 ath10k_warn("Failed to set CTS prot for VDEV: %d\n",
2326 arvif->vdev_id);
2327 }
2328
2329 if (changed & BSS_CHANGED_ERP_SLOT) {
2330 u32 slottime;
2331 if (info->use_short_slot)
2332 slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
2333
2334 else
2335 slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
2336
2337 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
2338 arvif->vdev_id, slottime);
2339
2340 vdev_param = ar->wmi.vdev_param->slot_time;
2341 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
2342 slottime);
2343 if (ret)
2344 ath10k_warn("Failed to set erp slot for VDEV: %d\n",
2345 arvif->vdev_id);
2346 }
2347
2348 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
2349 u32 preamble;
2350 if (info->use_short_preamble)
2351 preamble = WMI_VDEV_PREAMBLE_SHORT;
2352 else
2353 preamble = WMI_VDEV_PREAMBLE_LONG;
2354
2355 ath10k_dbg(ATH10K_DBG_MAC,
2356 "mac vdev %d preamble %dn",
2357 arvif->vdev_id, preamble);
2358
2359 vdev_param = ar->wmi.vdev_param->preamble;
2360 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
2361 preamble);
2362 if (ret)
2363 ath10k_warn("Failed to set preamble for VDEV: %d\n",
2364 arvif->vdev_id);
2365 }
2366
2367 if (changed & BSS_CHANGED_ASSOC) {
2368 if (info->assoc)
2369 ath10k_bss_assoc(hw, vif, info);
2370 }
2371
2372 mutex_unlock(&ar->conf_mutex);
2373 }
2374
2375 static int ath10k_hw_scan(struct ieee80211_hw *hw,
2376 struct ieee80211_vif *vif,
2377 struct cfg80211_scan_request *req)
2378 {
2379 struct ath10k *ar = hw->priv;
2380 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2381 struct wmi_start_scan_arg arg;
2382 int ret = 0;
2383 int i;
2384
2385 mutex_lock(&ar->conf_mutex);
2386
2387 spin_lock_bh(&ar->data_lock);
2388 if (ar->scan.in_progress) {
2389 spin_unlock_bh(&ar->data_lock);
2390 ret = -EBUSY;
2391 goto exit;
2392 }
2393
2394 INIT_COMPLETION(ar->scan.started);
2395 INIT_COMPLETION(ar->scan.completed);
2396 ar->scan.in_progress = true;
2397 ar->scan.aborting = false;
2398 ar->scan.is_roc = false;
2399 ar->scan.vdev_id = arvif->vdev_id;
2400 spin_unlock_bh(&ar->data_lock);
2401
2402 memset(&arg, 0, sizeof(arg));
2403 ath10k_wmi_start_scan_init(ar, &arg);
2404 arg.vdev_id = arvif->vdev_id;
2405 arg.scan_id = ATH10K_SCAN_ID;
2406
2407 if (!req->no_cck)
2408 arg.scan_ctrl_flags |= WMI_SCAN_ADD_CCK_RATES;
2409
2410 if (req->ie_len) {
2411 arg.ie_len = req->ie_len;
2412 memcpy(arg.ie, req->ie, arg.ie_len);
2413 }
2414
2415 if (req->n_ssids) {
2416 arg.n_ssids = req->n_ssids;
2417 for (i = 0; i < arg.n_ssids; i++) {
2418 arg.ssids[i].len = req->ssids[i].ssid_len;
2419 arg.ssids[i].ssid = req->ssids[i].ssid;
2420 }
2421 } else {
2422 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
2423 }
2424
2425 if (req->n_channels) {
2426 arg.n_channels = req->n_channels;
2427 for (i = 0; i < arg.n_channels; i++)
2428 arg.channels[i] = req->channels[i]->center_freq;
2429 }
2430
2431 ret = ath10k_start_scan(ar, &arg);
2432 if (ret) {
2433 ath10k_warn("could not start hw scan (%d)\n", ret);
2434 spin_lock_bh(&ar->data_lock);
2435 ar->scan.in_progress = false;
2436 spin_unlock_bh(&ar->data_lock);
2437 }
2438
2439 exit:
2440 mutex_unlock(&ar->conf_mutex);
2441 return ret;
2442 }
2443
2444 static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
2445 struct ieee80211_vif *vif)
2446 {
2447 struct ath10k *ar = hw->priv;
2448 int ret;
2449
2450 mutex_lock(&ar->conf_mutex);
2451 ret = ath10k_abort_scan(ar);
2452 if (ret) {
2453 ath10k_warn("couldn't abort scan (%d). forcefully sending scan completion to mac80211\n",
2454 ret);
2455 ieee80211_scan_completed(hw, 1 /* aborted */);
2456 }
2457 mutex_unlock(&ar->conf_mutex);
2458 }
2459
2460 static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
2461 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
2462 struct ieee80211_key_conf *key)
2463 {
2464 struct ath10k *ar = hw->priv;
2465 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2466 struct ath10k_peer *peer;
2467 const u8 *peer_addr;
2468 bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
2469 key->cipher == WLAN_CIPHER_SUITE_WEP104;
2470 int ret = 0;
2471
2472 if (key->keyidx > WMI_MAX_KEY_INDEX)
2473 return -ENOSPC;
2474
2475 mutex_lock(&ar->conf_mutex);
2476
2477 if (sta)
2478 peer_addr = sta->addr;
2479 else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
2480 peer_addr = vif->bss_conf.bssid;
2481 else
2482 peer_addr = vif->addr;
2483
2484 key->hw_key_idx = key->keyidx;
2485
2486 /* the peer should not disappear in mid-way (unless FW goes awry) since
2487 * we already hold conf_mutex. we just make sure its there now. */
2488 spin_lock_bh(&ar->data_lock);
2489 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
2490 spin_unlock_bh(&ar->data_lock);
2491
2492 if (!peer) {
2493 if (cmd == SET_KEY) {
2494 ath10k_warn("cannot install key for non-existent peer %pM\n",
2495 peer_addr);
2496 ret = -EOPNOTSUPP;
2497 goto exit;
2498 } else {
2499 /* if the peer doesn't exist there is no key to disable
2500 * anymore */
2501 goto exit;
2502 }
2503 }
2504
2505 if (is_wep) {
2506 if (cmd == SET_KEY)
2507 arvif->wep_keys[key->keyidx] = key;
2508 else
2509 arvif->wep_keys[key->keyidx] = NULL;
2510
2511 if (cmd == DISABLE_KEY)
2512 ath10k_clear_vdev_key(arvif, key);
2513 }
2514
2515 ret = ath10k_install_key(arvif, key, cmd, peer_addr);
2516 if (ret) {
2517 ath10k_warn("ath10k_install_key failed (%d)\n", ret);
2518 goto exit;
2519 }
2520
2521 spin_lock_bh(&ar->data_lock);
2522 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
2523 if (peer && cmd == SET_KEY)
2524 peer->keys[key->keyidx] = key;
2525 else if (peer && cmd == DISABLE_KEY)
2526 peer->keys[key->keyidx] = NULL;
2527 else if (peer == NULL)
2528 /* impossible unless FW goes crazy */
2529 ath10k_warn("peer %pM disappeared!\n", peer_addr);
2530 spin_unlock_bh(&ar->data_lock);
2531
2532 exit:
2533 mutex_unlock(&ar->conf_mutex);
2534 return ret;
2535 }
2536
2537 static int ath10k_sta_state(struct ieee80211_hw *hw,
2538 struct ieee80211_vif *vif,
2539 struct ieee80211_sta *sta,
2540 enum ieee80211_sta_state old_state,
2541 enum ieee80211_sta_state new_state)
2542 {
2543 struct ath10k *ar = hw->priv;
2544 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2545 int ret = 0;
2546
2547 mutex_lock(&ar->conf_mutex);
2548
2549 if (old_state == IEEE80211_STA_NOTEXIST &&
2550 new_state == IEEE80211_STA_NONE &&
2551 vif->type != NL80211_IFTYPE_STATION) {
2552 /*
2553 * New station addition.
2554 */
2555 ath10k_dbg(ATH10K_DBG_MAC,
2556 "mac vdev %d peer create %pM (new sta)\n",
2557 arvif->vdev_id, sta->addr);
2558
2559 ret = ath10k_peer_create(ar, arvif->vdev_id, sta->addr);
2560 if (ret)
2561 ath10k_warn("Failed to add peer: %pM for VDEV: %d\n",
2562 sta->addr, arvif->vdev_id);
2563 } else if ((old_state == IEEE80211_STA_NONE &&
2564 new_state == IEEE80211_STA_NOTEXIST)) {
2565 /*
2566 * Existing station deletion.
2567 */
2568 ath10k_dbg(ATH10K_DBG_MAC,
2569 "mac vdev %d peer delete %pM (sta gone)\n",
2570 arvif->vdev_id, sta->addr);
2571 ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
2572 if (ret)
2573 ath10k_warn("Failed to delete peer: %pM for VDEV: %d\n",
2574 sta->addr, arvif->vdev_id);
2575
2576 if (vif->type == NL80211_IFTYPE_STATION)
2577 ath10k_bss_disassoc(hw, vif);
2578 } else if (old_state == IEEE80211_STA_AUTH &&
2579 new_state == IEEE80211_STA_ASSOC &&
2580 (vif->type == NL80211_IFTYPE_AP ||
2581 vif->type == NL80211_IFTYPE_ADHOC)) {
2582 /*
2583 * New association.
2584 */
2585 ath10k_dbg(ATH10K_DBG_MAC, "mac sta %pM associated\n",
2586 sta->addr);
2587
2588 ret = ath10k_station_assoc(ar, arvif, sta);
2589 if (ret)
2590 ath10k_warn("Failed to associate station: %pM\n",
2591 sta->addr);
2592 } else if (old_state == IEEE80211_STA_ASSOC &&
2593 new_state == IEEE80211_STA_AUTH &&
2594 (vif->type == NL80211_IFTYPE_AP ||
2595 vif->type == NL80211_IFTYPE_ADHOC)) {
2596 /*
2597 * Disassociation.
2598 */
2599 ath10k_dbg(ATH10K_DBG_MAC, "mac sta %pM disassociated\n",
2600 sta->addr);
2601
2602 ret = ath10k_station_disassoc(ar, arvif, sta);
2603 if (ret)
2604 ath10k_warn("Failed to disassociate station: %pM\n",
2605 sta->addr);
2606 }
2607
2608 mutex_unlock(&ar->conf_mutex);
2609 return ret;
2610 }
2611
2612 static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
2613 u16 ac, bool enable)
2614 {
2615 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2616 u32 value = 0;
2617 int ret = 0;
2618
2619 lockdep_assert_held(&ar->conf_mutex);
2620
2621 if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
2622 return 0;
2623
2624 switch (ac) {
2625 case IEEE80211_AC_VO:
2626 value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
2627 WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
2628 break;
2629 case IEEE80211_AC_VI:
2630 value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
2631 WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
2632 break;
2633 case IEEE80211_AC_BE:
2634 value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
2635 WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
2636 break;
2637 case IEEE80211_AC_BK:
2638 value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
2639 WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
2640 break;
2641 }
2642
2643 if (enable)
2644 arvif->u.sta.uapsd |= value;
2645 else
2646 arvif->u.sta.uapsd &= ~value;
2647
2648 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
2649 WMI_STA_PS_PARAM_UAPSD,
2650 arvif->u.sta.uapsd);
2651 if (ret) {
2652 ath10k_warn("could not set uapsd params %d\n", ret);
2653 goto exit;
2654 }
2655
2656 if (arvif->u.sta.uapsd)
2657 value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
2658 else
2659 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
2660
2661 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
2662 WMI_STA_PS_PARAM_RX_WAKE_POLICY,
2663 value);
2664 if (ret)
2665 ath10k_warn("could not set rx wake param %d\n", ret);
2666
2667 exit:
2668 return ret;
2669 }
2670
2671 static int ath10k_conf_tx(struct ieee80211_hw *hw,
2672 struct ieee80211_vif *vif, u16 ac,
2673 const struct ieee80211_tx_queue_params *params)
2674 {
2675 struct ath10k *ar = hw->priv;
2676 struct wmi_wmm_params_arg *p = NULL;
2677 int ret;
2678
2679 mutex_lock(&ar->conf_mutex);
2680
2681 switch (ac) {
2682 case IEEE80211_AC_VO:
2683 p = &ar->wmm_params.ac_vo;
2684 break;
2685 case IEEE80211_AC_VI:
2686 p = &ar->wmm_params.ac_vi;
2687 break;
2688 case IEEE80211_AC_BE:
2689 p = &ar->wmm_params.ac_be;
2690 break;
2691 case IEEE80211_AC_BK:
2692 p = &ar->wmm_params.ac_bk;
2693 break;
2694 }
2695
2696 if (WARN_ON(!p)) {
2697 ret = -EINVAL;
2698 goto exit;
2699 }
2700
2701 p->cwmin = params->cw_min;
2702 p->cwmax = params->cw_max;
2703 p->aifs = params->aifs;
2704
2705 /*
2706 * The channel time duration programmed in the HW is in absolute
2707 * microseconds, while mac80211 gives the txop in units of
2708 * 32 microseconds.
2709 */
2710 p->txop = params->txop * 32;
2711
2712 /* FIXME: FW accepts wmm params per hw, not per vif */
2713 ret = ath10k_wmi_pdev_set_wmm_params(ar, &ar->wmm_params);
2714 if (ret) {
2715 ath10k_warn("could not set wmm params %d\n", ret);
2716 goto exit;
2717 }
2718
2719 ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
2720 if (ret)
2721 ath10k_warn("could not set sta uapsd %d\n", ret);
2722
2723 exit:
2724 mutex_unlock(&ar->conf_mutex);
2725 return ret;
2726 }
2727
2728 #define ATH10K_ROC_TIMEOUT_HZ (2*HZ)
2729
2730 static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
2731 struct ieee80211_vif *vif,
2732 struct ieee80211_channel *chan,
2733 int duration,
2734 enum ieee80211_roc_type type)
2735 {
2736 struct ath10k *ar = hw->priv;
2737 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2738 struct wmi_start_scan_arg arg;
2739 int ret;
2740
2741 mutex_lock(&ar->conf_mutex);
2742
2743 spin_lock_bh(&ar->data_lock);
2744 if (ar->scan.in_progress) {
2745 spin_unlock_bh(&ar->data_lock);
2746 ret = -EBUSY;
2747 goto exit;
2748 }
2749
2750 INIT_COMPLETION(ar->scan.started);
2751 INIT_COMPLETION(ar->scan.completed);
2752 INIT_COMPLETION(ar->scan.on_channel);
2753 ar->scan.in_progress = true;
2754 ar->scan.aborting = false;
2755 ar->scan.is_roc = true;
2756 ar->scan.vdev_id = arvif->vdev_id;
2757 ar->scan.roc_freq = chan->center_freq;
2758 spin_unlock_bh(&ar->data_lock);
2759
2760 memset(&arg, 0, sizeof(arg));
2761 ath10k_wmi_start_scan_init(ar, &arg);
2762 arg.vdev_id = arvif->vdev_id;
2763 arg.scan_id = ATH10K_SCAN_ID;
2764 arg.n_channels = 1;
2765 arg.channels[0] = chan->center_freq;
2766 arg.dwell_time_active = duration;
2767 arg.dwell_time_passive = duration;
2768 arg.max_scan_time = 2 * duration;
2769 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
2770 arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
2771
2772 ret = ath10k_start_scan(ar, &arg);
2773 if (ret) {
2774 ath10k_warn("could not start roc scan (%d)\n", ret);
2775 spin_lock_bh(&ar->data_lock);
2776 ar->scan.in_progress = false;
2777 spin_unlock_bh(&ar->data_lock);
2778 goto exit;
2779 }
2780
2781 ret = wait_for_completion_timeout(&ar->scan.on_channel, 3*HZ);
2782 if (ret == 0) {
2783 ath10k_warn("could not switch to channel for roc scan\n");
2784 ath10k_abort_scan(ar);
2785 ret = -ETIMEDOUT;
2786 goto exit;
2787 }
2788
2789 ret = 0;
2790 exit:
2791 mutex_unlock(&ar->conf_mutex);
2792 return ret;
2793 }
2794
2795 static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw)
2796 {
2797 struct ath10k *ar = hw->priv;
2798
2799 mutex_lock(&ar->conf_mutex);
2800 ath10k_abort_scan(ar);
2801 mutex_unlock(&ar->conf_mutex);
2802
2803 return 0;
2804 }
2805
2806 /*
2807 * Both RTS and Fragmentation threshold are interface-specific
2808 * in ath10k, but device-specific in mac80211.
2809 */
2810 static void ath10k_set_rts_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
2811 {
2812 struct ath10k_generic_iter *ar_iter = data;
2813 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2814 u32 rts = ar_iter->ar->hw->wiphy->rts_threshold;
2815
2816 lockdep_assert_held(&arvif->ar->conf_mutex);
2817
2818 /* During HW reconfiguration mac80211 reports all interfaces that were
2819 * running until reconfiguration was started. Since FW doesn't have any
2820 * vdevs at this point we must not iterate over this interface list.
2821 * This setting will be updated upon add_interface(). */
2822 if (ar_iter->ar->state == ATH10K_STATE_RESTARTED)
2823 return;
2824
2825 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d rts_threshold %d\n",
2826 arvif->vdev_id, rts);
2827
2828 ar_iter->ret = ath10k_mac_set_rts(arvif, rts);
2829 if (ar_iter->ret)
2830 ath10k_warn("Failed to set RTS threshold for VDEV: %d\n",
2831 arvif->vdev_id);
2832 }
2833
2834 static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
2835 {
2836 struct ath10k_generic_iter ar_iter;
2837 struct ath10k *ar = hw->priv;
2838
2839 memset(&ar_iter, 0, sizeof(struct ath10k_generic_iter));
2840 ar_iter.ar = ar;
2841
2842 mutex_lock(&ar->conf_mutex);
2843 ieee80211_iterate_active_interfaces_atomic(
2844 hw, IEEE80211_IFACE_ITER_NORMAL,
2845 ath10k_set_rts_iter, &ar_iter);
2846 mutex_unlock(&ar->conf_mutex);
2847
2848 return ar_iter.ret;
2849 }
2850
2851 static void ath10k_set_frag_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
2852 {
2853 struct ath10k_generic_iter *ar_iter = data;
2854 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2855 u32 frag = ar_iter->ar->hw->wiphy->frag_threshold;
2856
2857 lockdep_assert_held(&arvif->ar->conf_mutex);
2858
2859 /* During HW reconfiguration mac80211 reports all interfaces that were
2860 * running until reconfiguration was started. Since FW doesn't have any
2861 * vdevs at this point we must not iterate over this interface list.
2862 * This setting will be updated upon add_interface(). */
2863 if (ar_iter->ar->state == ATH10K_STATE_RESTARTED)
2864 return;
2865
2866 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d fragmentation_threshold %d\n",
2867 arvif->vdev_id, frag);
2868
2869 ar_iter->ret = ath10k_mac_set_frag(arvif, frag);
2870 if (ar_iter->ret)
2871 ath10k_warn("Failed to set frag threshold for VDEV: %d\n",
2872 arvif->vdev_id);
2873 }
2874
2875 static int ath10k_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
2876 {
2877 struct ath10k_generic_iter ar_iter;
2878 struct ath10k *ar = hw->priv;
2879
2880 memset(&ar_iter, 0, sizeof(struct ath10k_generic_iter));
2881 ar_iter.ar = ar;
2882
2883 mutex_lock(&ar->conf_mutex);
2884 ieee80211_iterate_active_interfaces_atomic(
2885 hw, IEEE80211_IFACE_ITER_NORMAL,
2886 ath10k_set_frag_iter, &ar_iter);
2887 mutex_unlock(&ar->conf_mutex);
2888
2889 return ar_iter.ret;
2890 }
2891
2892 static void ath10k_flush(struct ieee80211_hw *hw, u32 queues, bool drop)
2893 {
2894 struct ath10k *ar = hw->priv;
2895 bool skip;
2896 int ret;
2897
2898 /* mac80211 doesn't care if we really xmit queued frames or not
2899 * we'll collect those frames either way if we stop/delete vdevs */
2900 if (drop)
2901 return;
2902
2903 mutex_lock(&ar->conf_mutex);
2904
2905 if (ar->state == ATH10K_STATE_WEDGED)
2906 goto skip;
2907
2908 ret = wait_event_timeout(ar->htt.empty_tx_wq, ({
2909 bool empty;
2910
2911 spin_lock_bh(&ar->htt.tx_lock);
2912 empty = (ar->htt.num_pending_tx == 0);
2913 spin_unlock_bh(&ar->htt.tx_lock);
2914
2915 skip = (ar->state == ATH10K_STATE_WEDGED);
2916
2917 (empty || skip);
2918 }), ATH10K_FLUSH_TIMEOUT_HZ);
2919
2920 if (ret <= 0 || skip)
2921 ath10k_warn("tx not flushed\n");
2922
2923 skip:
2924 mutex_unlock(&ar->conf_mutex);
2925 }
2926
2927 /* TODO: Implement this function properly
2928 * For now it is needed to reply to Probe Requests in IBSS mode.
2929 * Propably we need this information from FW.
2930 */
2931 static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
2932 {
2933 return 1;
2934 }
2935
2936 #ifdef CONFIG_PM
2937 static int ath10k_suspend(struct ieee80211_hw *hw,
2938 struct cfg80211_wowlan *wowlan)
2939 {
2940 struct ath10k *ar = hw->priv;
2941 int ret;
2942
2943 ar->is_target_paused = false;
2944
2945 ret = ath10k_wmi_pdev_suspend_target(ar);
2946 if (ret) {
2947 ath10k_warn("could not suspend target (%d)\n", ret);
2948 return 1;
2949 }
2950
2951 ret = wait_event_interruptible_timeout(ar->event_queue,
2952 ar->is_target_paused == true,
2953 1 * HZ);
2954 if (ret < 0) {
2955 ath10k_warn("suspend interrupted (%d)\n", ret);
2956 goto resume;
2957 } else if (ret == 0) {
2958 ath10k_warn("suspend timed out - target pause event never came\n");
2959 goto resume;
2960 }
2961
2962 ret = ath10k_hif_suspend(ar);
2963 if (ret) {
2964 ath10k_warn("could not suspend hif (%d)\n", ret);
2965 goto resume;
2966 }
2967
2968 return 0;
2969 resume:
2970 ret = ath10k_wmi_pdev_resume_target(ar);
2971 if (ret)
2972 ath10k_warn("could not resume target (%d)\n", ret);
2973 return 1;
2974 }
2975
2976 static int ath10k_resume(struct ieee80211_hw *hw)
2977 {
2978 struct ath10k *ar = hw->priv;
2979 int ret;
2980
2981 ret = ath10k_hif_resume(ar);
2982 if (ret) {
2983 ath10k_warn("could not resume hif (%d)\n", ret);
2984 return 1;
2985 }
2986
2987 ret = ath10k_wmi_pdev_resume_target(ar);
2988 if (ret) {
2989 ath10k_warn("could not resume target (%d)\n", ret);
2990 return 1;
2991 }
2992
2993 return 0;
2994 }
2995 #endif
2996
2997 static void ath10k_restart_complete(struct ieee80211_hw *hw)
2998 {
2999 struct ath10k *ar = hw->priv;
3000
3001 mutex_lock(&ar->conf_mutex);
3002
3003 /* If device failed to restart it will be in a different state, e.g.
3004 * ATH10K_STATE_WEDGED */
3005 if (ar->state == ATH10K_STATE_RESTARTED) {
3006 ath10k_info("device successfully recovered\n");
3007 ar->state = ATH10K_STATE_ON;
3008 }
3009
3010 mutex_unlock(&ar->conf_mutex);
3011 }
3012
3013 static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
3014 struct survey_info *survey)
3015 {
3016 struct ath10k *ar = hw->priv;
3017 struct ieee80211_supported_band *sband;
3018 struct survey_info *ar_survey = &ar->survey[idx];
3019 int ret = 0;
3020
3021 mutex_lock(&ar->conf_mutex);
3022
3023 sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
3024 if (sband && idx >= sband->n_channels) {
3025 idx -= sband->n_channels;
3026 sband = NULL;
3027 }
3028
3029 if (!sband)
3030 sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
3031
3032 if (!sband || idx >= sband->n_channels) {
3033 ret = -ENOENT;
3034 goto exit;
3035 }
3036
3037 spin_lock_bh(&ar->data_lock);
3038 memcpy(survey, ar_survey, sizeof(*survey));
3039 spin_unlock_bh(&ar->data_lock);
3040
3041 survey->channel = &sband->channels[idx];
3042
3043 exit:
3044 mutex_unlock(&ar->conf_mutex);
3045 return ret;
3046 }
3047
3048 static const struct ieee80211_ops ath10k_ops = {
3049 .tx = ath10k_tx,
3050 .start = ath10k_start,
3051 .stop = ath10k_stop,
3052 .config = ath10k_config,
3053 .add_interface = ath10k_add_interface,
3054 .remove_interface = ath10k_remove_interface,
3055 .configure_filter = ath10k_configure_filter,
3056 .bss_info_changed = ath10k_bss_info_changed,
3057 .hw_scan = ath10k_hw_scan,
3058 .cancel_hw_scan = ath10k_cancel_hw_scan,
3059 .set_key = ath10k_set_key,
3060 .sta_state = ath10k_sta_state,
3061 .conf_tx = ath10k_conf_tx,
3062 .remain_on_channel = ath10k_remain_on_channel,
3063 .cancel_remain_on_channel = ath10k_cancel_remain_on_channel,
3064 .set_rts_threshold = ath10k_set_rts_threshold,
3065 .set_frag_threshold = ath10k_set_frag_threshold,
3066 .flush = ath10k_flush,
3067 .tx_last_beacon = ath10k_tx_last_beacon,
3068 .restart_complete = ath10k_restart_complete,
3069 .get_survey = ath10k_get_survey,
3070 #ifdef CONFIG_PM
3071 .suspend = ath10k_suspend,
3072 .resume = ath10k_resume,
3073 #endif
3074 };
3075
3076 #define RATETAB_ENT(_rate, _rateid, _flags) { \
3077 .bitrate = (_rate), \
3078 .flags = (_flags), \
3079 .hw_value = (_rateid), \
3080 }
3081
3082 #define CHAN2G(_channel, _freq, _flags) { \
3083 .band = IEEE80211_BAND_2GHZ, \
3084 .hw_value = (_channel), \
3085 .center_freq = (_freq), \
3086 .flags = (_flags), \
3087 .max_antenna_gain = 0, \
3088 .max_power = 30, \
3089 }
3090
3091 #define CHAN5G(_channel, _freq, _flags) { \
3092 .band = IEEE80211_BAND_5GHZ, \
3093 .hw_value = (_channel), \
3094 .center_freq = (_freq), \
3095 .flags = (_flags), \
3096 .max_antenna_gain = 0, \
3097 .max_power = 30, \
3098 }
3099
3100 static const struct ieee80211_channel ath10k_2ghz_channels[] = {
3101 CHAN2G(1, 2412, 0),
3102 CHAN2G(2, 2417, 0),
3103 CHAN2G(3, 2422, 0),
3104 CHAN2G(4, 2427, 0),
3105 CHAN2G(5, 2432, 0),
3106 CHAN2G(6, 2437, 0),
3107 CHAN2G(7, 2442, 0),
3108 CHAN2G(8, 2447, 0),
3109 CHAN2G(9, 2452, 0),
3110 CHAN2G(10, 2457, 0),
3111 CHAN2G(11, 2462, 0),
3112 CHAN2G(12, 2467, 0),
3113 CHAN2G(13, 2472, 0),
3114 CHAN2G(14, 2484, 0),
3115 };
3116
3117 static const struct ieee80211_channel ath10k_5ghz_channels[] = {
3118 CHAN5G(36, 5180, 0),
3119 CHAN5G(40, 5200, 0),
3120 CHAN5G(44, 5220, 0),
3121 CHAN5G(48, 5240, 0),
3122 CHAN5G(52, 5260, 0),
3123 CHAN5G(56, 5280, 0),
3124 CHAN5G(60, 5300, 0),
3125 CHAN5G(64, 5320, 0),
3126 CHAN5G(100, 5500, 0),
3127 CHAN5G(104, 5520, 0),
3128 CHAN5G(108, 5540, 0),
3129 CHAN5G(112, 5560, 0),
3130 CHAN5G(116, 5580, 0),
3131 CHAN5G(120, 5600, 0),
3132 CHAN5G(124, 5620, 0),
3133 CHAN5G(128, 5640, 0),
3134 CHAN5G(132, 5660, 0),
3135 CHAN5G(136, 5680, 0),
3136 CHAN5G(140, 5700, 0),
3137 CHAN5G(149, 5745, 0),
3138 CHAN5G(153, 5765, 0),
3139 CHAN5G(157, 5785, 0),
3140 CHAN5G(161, 5805, 0),
3141 CHAN5G(165, 5825, 0),
3142 };
3143
3144 static struct ieee80211_rate ath10k_rates[] = {
3145 /* CCK */
3146 RATETAB_ENT(10, 0x82, 0),
3147 RATETAB_ENT(20, 0x84, 0),
3148 RATETAB_ENT(55, 0x8b, 0),
3149 RATETAB_ENT(110, 0x96, 0),
3150 /* OFDM */
3151 RATETAB_ENT(60, 0x0c, 0),
3152 RATETAB_ENT(90, 0x12, 0),
3153 RATETAB_ENT(120, 0x18, 0),
3154 RATETAB_ENT(180, 0x24, 0),
3155 RATETAB_ENT(240, 0x30, 0),
3156 RATETAB_ENT(360, 0x48, 0),
3157 RATETAB_ENT(480, 0x60, 0),
3158 RATETAB_ENT(540, 0x6c, 0),
3159 };
3160
3161 #define ath10k_a_rates (ath10k_rates + 4)
3162 #define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - 4)
3163 #define ath10k_g_rates (ath10k_rates + 0)
3164 #define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
3165
3166 struct ath10k *ath10k_mac_create(void)
3167 {
3168 struct ieee80211_hw *hw;
3169 struct ath10k *ar;
3170
3171 hw = ieee80211_alloc_hw(sizeof(struct ath10k), &ath10k_ops);
3172 if (!hw)
3173 return NULL;
3174
3175 ar = hw->priv;
3176 ar->hw = hw;
3177
3178 return ar;
3179 }
3180
3181 void ath10k_mac_destroy(struct ath10k *ar)
3182 {
3183 ieee80211_free_hw(ar->hw);
3184 }
3185
3186 static const struct ieee80211_iface_limit ath10k_if_limits[] = {
3187 {
3188 .max = 8,
3189 .types = BIT(NL80211_IFTYPE_STATION)
3190 | BIT(NL80211_IFTYPE_P2P_CLIENT)
3191 },
3192 {
3193 .max = 3,
3194 .types = BIT(NL80211_IFTYPE_P2P_GO)
3195 },
3196 {
3197 .max = 7,
3198 .types = BIT(NL80211_IFTYPE_AP)
3199 },
3200 };
3201
3202 static const struct ieee80211_iface_combination ath10k_if_comb = {
3203 .limits = ath10k_if_limits,
3204 .n_limits = ARRAY_SIZE(ath10k_if_limits),
3205 .max_interfaces = 8,
3206 .num_different_channels = 1,
3207 .beacon_int_infra_match = true,
3208 };
3209
3210 static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
3211 {
3212 struct ieee80211_sta_vht_cap vht_cap = {0};
3213 u16 mcs_map;
3214 int i;
3215
3216 vht_cap.vht_supported = 1;
3217 vht_cap.cap = ar->vht_cap_info;
3218
3219 mcs_map = 0;
3220 for (i = 0; i < 8; i++) {
3221 if (i < ar->num_rf_chains)
3222 mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i*2);
3223 else
3224 mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i*2);
3225 }
3226
3227 vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
3228 vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
3229
3230 return vht_cap;
3231 }
3232
3233 static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
3234 {
3235 int i;
3236 struct ieee80211_sta_ht_cap ht_cap = {0};
3237
3238 if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
3239 return ht_cap;
3240
3241 ht_cap.ht_supported = 1;
3242 ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
3243 ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
3244 ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
3245 ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
3246 ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
3247
3248 if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
3249 ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
3250
3251 if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
3252 ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
3253
3254 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
3255 u32 smps;
3256
3257 smps = WLAN_HT_CAP_SM_PS_DYNAMIC;
3258 smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
3259
3260 ht_cap.cap |= smps;
3261 }
3262
3263 if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC)
3264 ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
3265
3266 if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
3267 u32 stbc;
3268
3269 stbc = ar->ht_cap_info;
3270 stbc &= WMI_HT_CAP_RX_STBC;
3271 stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
3272 stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
3273 stbc &= IEEE80211_HT_CAP_RX_STBC;
3274
3275 ht_cap.cap |= stbc;
3276 }
3277
3278 if (ar->ht_cap_info & WMI_HT_CAP_LDPC)
3279 ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
3280
3281 if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
3282 ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
3283
3284 /* max AMSDU is implicitly taken from vht_cap_info */
3285 if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
3286 ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
3287
3288 for (i = 0; i < ar->num_rf_chains; i++)
3289 ht_cap.mcs.rx_mask[i] = 0xFF;
3290
3291 ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
3292
3293 return ht_cap;
3294 }
3295
3296
3297 static void ath10k_get_arvif_iter(void *data, u8 *mac,
3298 struct ieee80211_vif *vif)
3299 {
3300 struct ath10k_vif_iter *arvif_iter = data;
3301 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3302
3303 if (arvif->vdev_id == arvif_iter->vdev_id)
3304 arvif_iter->arvif = arvif;
3305 }
3306
3307 struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
3308 {
3309 struct ath10k_vif_iter arvif_iter;
3310 u32 flags;
3311
3312 memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
3313 arvif_iter.vdev_id = vdev_id;
3314
3315 flags = IEEE80211_IFACE_ITER_RESUME_ALL;
3316 ieee80211_iterate_active_interfaces_atomic(ar->hw,
3317 flags,
3318 ath10k_get_arvif_iter,
3319 &arvif_iter);
3320 if (!arvif_iter.arvif) {
3321 ath10k_warn("No VIF found for VDEV: %d\n", vdev_id);
3322 return NULL;
3323 }
3324
3325 return arvif_iter.arvif;
3326 }
3327
3328 int ath10k_mac_register(struct ath10k *ar)
3329 {
3330 struct ieee80211_supported_band *band;
3331 struct ieee80211_sta_vht_cap vht_cap;
3332 struct ieee80211_sta_ht_cap ht_cap;
3333 void *channels;
3334 int ret;
3335
3336 SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
3337
3338 SET_IEEE80211_DEV(ar->hw, ar->dev);
3339
3340 ht_cap = ath10k_get_ht_cap(ar);
3341 vht_cap = ath10k_create_vht_cap(ar);
3342
3343 if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
3344 channels = kmemdup(ath10k_2ghz_channels,
3345 sizeof(ath10k_2ghz_channels),
3346 GFP_KERNEL);
3347 if (!channels) {
3348 ret = -ENOMEM;
3349 goto err_free;
3350 }
3351
3352 band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
3353 band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
3354 band->channels = channels;
3355 band->n_bitrates = ath10k_g_rates_size;
3356 band->bitrates = ath10k_g_rates;
3357 band->ht_cap = ht_cap;
3358
3359 /* vht is not supported in 2.4 GHz */
3360
3361 ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
3362 }
3363
3364 if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
3365 channels = kmemdup(ath10k_5ghz_channels,
3366 sizeof(ath10k_5ghz_channels),
3367 GFP_KERNEL);
3368 if (!channels) {
3369 ret = -ENOMEM;
3370 goto err_free;
3371 }
3372
3373 band = &ar->mac.sbands[IEEE80211_BAND_5GHZ];
3374 band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
3375 band->channels = channels;
3376 band->n_bitrates = ath10k_a_rates_size;
3377 band->bitrates = ath10k_a_rates;
3378 band->ht_cap = ht_cap;
3379 band->vht_cap = vht_cap;
3380 ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
3381 }
3382
3383 ar->hw->wiphy->interface_modes =
3384 BIT(NL80211_IFTYPE_STATION) |
3385 BIT(NL80211_IFTYPE_ADHOC) |
3386 BIT(NL80211_IFTYPE_AP) |
3387 BIT(NL80211_IFTYPE_P2P_CLIENT) |
3388 BIT(NL80211_IFTYPE_P2P_GO);
3389
3390 ar->hw->flags = IEEE80211_HW_SIGNAL_DBM |
3391 IEEE80211_HW_SUPPORTS_PS |
3392 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
3393 IEEE80211_HW_SUPPORTS_UAPSD |
3394 IEEE80211_HW_MFP_CAPABLE |
3395 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
3396 IEEE80211_HW_HAS_RATE_CONTROL |
3397 IEEE80211_HW_SUPPORTS_STATIC_SMPS |
3398 IEEE80211_HW_WANT_MONITOR_VIF |
3399 IEEE80211_HW_AP_LINK_PS;
3400
3401 /* MSDU can have HTT TX fragment pushed in front. The additional 4
3402 * bytes is used for padding/alignment if necessary. */
3403 ar->hw->extra_tx_headroom += sizeof(struct htt_data_tx_desc_frag)*2 + 4;
3404
3405 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
3406 ar->hw->flags |= IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS;
3407
3408 if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
3409 ar->hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
3410 ar->hw->flags |= IEEE80211_HW_TX_AMPDU_SETUP_IN_HW;
3411 }
3412
3413 ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
3414 ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
3415
3416 ar->hw->vif_data_size = sizeof(struct ath10k_vif);
3417
3418 ar->hw->channel_change_time = 5000;
3419 ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
3420
3421 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
3422 ar->hw->wiphy->max_remain_on_channel_duration = 5000;
3423
3424 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
3425 /*
3426 * on LL hardware queues are managed entirely by the FW
3427 * so we only advertise to mac we can do the queues thing
3428 */
3429 ar->hw->queues = 4;
3430
3431 ar->hw->wiphy->iface_combinations = &ath10k_if_comb;
3432 ar->hw->wiphy->n_iface_combinations = 1;
3433
3434 ar->hw->netdev_features = NETIF_F_HW_CSUM;
3435
3436 ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
3437 ath10k_reg_notifier);
3438 if (ret) {
3439 ath10k_err("Regulatory initialization failed\n");
3440 goto err_free;
3441 }
3442
3443 ret = ieee80211_register_hw(ar->hw);
3444 if (ret) {
3445 ath10k_err("ieee80211 registration failed: %d\n", ret);
3446 goto err_free;
3447 }
3448
3449 if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
3450 ret = regulatory_hint(ar->hw->wiphy,
3451 ar->ath_common.regulatory.alpha2);
3452 if (ret)
3453 goto err_unregister;
3454 }
3455
3456 return 0;
3457
3458 err_unregister:
3459 ieee80211_unregister_hw(ar->hw);
3460 err_free:
3461 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
3462 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
3463
3464 return ret;
3465 }
3466
3467 void ath10k_mac_unregister(struct ath10k *ar)
3468 {
3469 ieee80211_unregister_hw(ar->hw);
3470
3471 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
3472 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
3473
3474 SET_IEEE80211_DEV(ar->hw, NULL);
3475 }
This page took 0.129739 seconds and 5 git commands to generate.