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