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