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