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