blob: 2dfd446251b1fbf6f9e44847711d425ff3e2555c [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 */
Michal Kazior818bdd12013-07-16 09:38:57 +02001741static void ath10k_halt(struct ath10k *ar)
1742{
1743 lockdep_assert_held(&ar->conf_mutex);
1744
1745 del_timer_sync(&ar->scan.timeout);
1746 ath10k_offchan_tx_purge(ar);
1747 ath10k_peer_cleanup_all(ar);
1748 ath10k_core_stop(ar);
1749 ath10k_hif_power_down(ar);
1750
1751 spin_lock_bh(&ar->data_lock);
1752 if (ar->scan.in_progress) {
1753 del_timer(&ar->scan.timeout);
1754 ar->scan.in_progress = false;
1755 ieee80211_scan_completed(ar->hw, true);
1756 }
1757 spin_unlock_bh(&ar->data_lock);
1758}
1759
Kalle Valo5e3dd152013-06-12 20:52:10 +03001760static int ath10k_start(struct ieee80211_hw *hw)
1761{
1762 struct ath10k *ar = hw->priv;
Michal Kazior818bdd12013-07-16 09:38:57 +02001763 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001764
Michal Kazior548db542013-07-05 16:15:15 +03001765 mutex_lock(&ar->conf_mutex);
1766
Michal Kazior818bdd12013-07-16 09:38:57 +02001767 if (ar->state != ATH10K_STATE_OFF) {
1768 ret = -EINVAL;
1769 goto exit;
1770 }
1771
1772 ret = ath10k_hif_power_up(ar);
1773 if (ret) {
1774 ath10k_err("could not init hif (%d)\n", ret);
1775 ar->state = ATH10K_STATE_OFF;
1776 goto exit;
1777 }
1778
1779 ret = ath10k_core_start(ar);
1780 if (ret) {
1781 ath10k_err("could not init core (%d)\n", ret);
1782 ath10k_hif_power_down(ar);
1783 ar->state = ATH10K_STATE_OFF;
1784 goto exit;
1785 }
1786
Kalle Valo5e3dd152013-06-12 20:52:10 +03001787 ret = ath10k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_PMF_QOS, 1);
1788 if (ret)
1789 ath10k_warn("could not enable WMI_PDEV_PARAM_PMF_QOS (%d)\n",
1790 ret);
1791
1792 ret = ath10k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_DYNAMIC_BW, 0);
1793 if (ret)
1794 ath10k_warn("could not init WMI_PDEV_PARAM_DYNAMIC_BW (%d)\n",
1795 ret);
1796
Michal Kaziorf7843d72013-07-16 09:38:52 +02001797 ath10k_regd_update(ar);
1798
Michal Kazior818bdd12013-07-16 09:38:57 +02001799exit:
Michal Kazior548db542013-07-05 16:15:15 +03001800 mutex_unlock(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001801 return 0;
1802}
1803
1804static void ath10k_stop(struct ieee80211_hw *hw)
1805{
1806 struct ath10k *ar = hw->priv;
1807
Michal Kazior548db542013-07-05 16:15:15 +03001808 mutex_lock(&ar->conf_mutex);
Michal Kazior818bdd12013-07-16 09:38:57 +02001809 if (ar->state == ATH10K_STATE_ON)
1810 ath10k_halt(ar);
Michal Kaziora96d7742013-07-16 09:38:56 +02001811
Michal Kaziorf7843d72013-07-16 09:38:52 +02001812 ar->state = ATH10K_STATE_OFF;
Michal Kazior548db542013-07-05 16:15:15 +03001813 mutex_unlock(&ar->conf_mutex);
1814
1815 cancel_work_sync(&ar->offchan_tx_work);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001816}
1817
1818static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
1819{
1820 struct ath10k_generic_iter ar_iter;
1821 struct ath10k *ar = hw->priv;
1822 struct ieee80211_conf *conf = &hw->conf;
1823 int ret = 0;
1824 u32 flags;
1825
1826 mutex_lock(&ar->conf_mutex);
1827
1828 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
1829 ath10k_dbg(ATH10K_DBG_MAC, "Config channel %d mhz\n",
1830 conf->chandef.chan->center_freq);
1831 spin_lock_bh(&ar->data_lock);
1832 ar->rx_channel = conf->chandef.chan;
1833 spin_unlock_bh(&ar->data_lock);
1834 }
1835
1836 if (changed & IEEE80211_CONF_CHANGE_PS) {
1837 memset(&ar_iter, 0, sizeof(struct ath10k_generic_iter));
1838 ar_iter.ar = ar;
1839 flags = IEEE80211_IFACE_ITER_RESUME_ALL;
1840
1841 ieee80211_iterate_active_interfaces_atomic(hw,
1842 flags,
1843 ath10k_ps_iter,
1844 &ar_iter);
1845
1846 ret = ar_iter.ret;
1847 }
1848
1849 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
1850 if (conf->flags & IEEE80211_CONF_MONITOR)
1851 ret = ath10k_monitor_create(ar);
1852 else
1853 ret = ath10k_monitor_destroy(ar);
1854 }
1855
1856 mutex_unlock(&ar->conf_mutex);
1857 return ret;
1858}
1859
1860/*
1861 * TODO:
1862 * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
1863 * because we will send mgmt frames without CCK. This requirement
1864 * for P2P_FIND/GO_NEG should be handled by checking CCK flag
1865 * in the TX packet.
1866 */
1867static int ath10k_add_interface(struct ieee80211_hw *hw,
1868 struct ieee80211_vif *vif)
1869{
1870 struct ath10k *ar = hw->priv;
1871 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1872 enum wmi_sta_powersave_param param;
1873 int ret = 0;
Michal Kazior679c54a2013-07-05 16:15:04 +03001874 u32 value, rts, frag;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001875 int bit;
1876
1877 mutex_lock(&ar->conf_mutex);
1878
1879 arvif->ar = ar;
1880 arvif->vif = vif;
1881
1882 if ((vif->type == NL80211_IFTYPE_MONITOR) && ar->monitor_present) {
1883 ath10k_warn("Only one monitor interface allowed\n");
1884 ret = -EBUSY;
1885 goto exit;
1886 }
1887
1888 bit = ffs(ar->free_vdev_map);
1889 if (bit == 0) {
1890 ret = -EBUSY;
1891 goto exit;
1892 }
1893
1894 arvif->vdev_id = bit - 1;
1895 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
1896 ar->free_vdev_map &= ~(1 << arvif->vdev_id);
1897
1898 if (ar->p2p)
1899 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
1900
1901 switch (vif->type) {
1902 case NL80211_IFTYPE_UNSPECIFIED:
1903 case NL80211_IFTYPE_STATION:
1904 arvif->vdev_type = WMI_VDEV_TYPE_STA;
1905 if (vif->p2p)
1906 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
1907 break;
1908 case NL80211_IFTYPE_ADHOC:
1909 arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
1910 break;
1911 case NL80211_IFTYPE_AP:
1912 arvif->vdev_type = WMI_VDEV_TYPE_AP;
1913
1914 if (vif->p2p)
1915 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
1916 break;
1917 case NL80211_IFTYPE_MONITOR:
1918 arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
1919 break;
1920 default:
1921 WARN_ON(1);
1922 break;
1923 }
1924
1925 ath10k_dbg(ATH10K_DBG_MAC, "Add interface: id %d type %d subtype %d\n",
1926 arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype);
1927
1928 ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
1929 arvif->vdev_subtype, vif->addr);
1930 if (ret) {
1931 ath10k_warn("WMI vdev create failed: ret %d\n", ret);
1932 goto exit;
1933 }
1934
1935 ret = ath10k_wmi_vdev_set_param(ar, 0, WMI_VDEV_PARAM_DEF_KEYID,
1936 arvif->def_wep_key_index);
1937 if (ret)
1938 ath10k_warn("Failed to set default keyid: %d\n", ret);
1939
1940 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
1941 WMI_VDEV_PARAM_TX_ENCAP_TYPE,
1942 ATH10K_HW_TXRX_NATIVE_WIFI);
1943 if (ret)
1944 ath10k_warn("Failed to set TX encap: %d\n", ret);
1945
1946 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
1947 ret = ath10k_peer_create(ar, arvif->vdev_id, vif->addr);
1948 if (ret) {
1949 ath10k_warn("Failed to create peer for AP: %d\n", ret);
1950 goto exit;
1951 }
1952 }
1953
1954 if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
1955 param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
1956 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
1957 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
1958 param, value);
1959 if (ret)
1960 ath10k_warn("Failed to set RX wake policy: %d\n", ret);
1961
1962 param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
1963 value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
1964 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
1965 param, value);
1966 if (ret)
1967 ath10k_warn("Failed to set TX wake thresh: %d\n", ret);
1968
1969 param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
1970 value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
1971 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
1972 param, value);
1973 if (ret)
1974 ath10k_warn("Failed to set PSPOLL count: %d\n", ret);
1975 }
1976
Michal Kazior679c54a2013-07-05 16:15:04 +03001977 rts = min_t(u32, ar->hw->wiphy->rts_threshold, ATH10K_RTS_MAX);
1978 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
1979 WMI_VDEV_PARAM_RTS_THRESHOLD,
1980 rts);
1981 if (ret)
1982 ath10k_warn("failed to set rts threshold for vdev %d (%d)\n",
1983 arvif->vdev_id, ret);
1984
1985 frag = clamp_t(u32, ar->hw->wiphy->frag_threshold,
1986 ATH10K_FRAGMT_THRESHOLD_MIN,
1987 ATH10K_FRAGMT_THRESHOLD_MAX);
1988 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
1989 WMI_VDEV_PARAM_FRAGMENTATION_THRESHOLD,
1990 frag);
1991 if (ret)
1992 ath10k_warn("failed to set frag threshold for vdev %d (%d)\n",
1993 arvif->vdev_id, ret);
1994
Kalle Valo5e3dd152013-06-12 20:52:10 +03001995 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
1996 ar->monitor_present = true;
1997
1998exit:
1999 mutex_unlock(&ar->conf_mutex);
2000 return ret;
2001}
2002
2003static void ath10k_remove_interface(struct ieee80211_hw *hw,
2004 struct ieee80211_vif *vif)
2005{
2006 struct ath10k *ar = hw->priv;
2007 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2008 int ret;
2009
2010 mutex_lock(&ar->conf_mutex);
2011
2012 ath10k_dbg(ATH10K_DBG_MAC, "Remove interface: id %d\n", arvif->vdev_id);
2013
2014 ar->free_vdev_map |= 1 << (arvif->vdev_id);
2015
2016 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
2017 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, vif->addr);
2018 if (ret)
2019 ath10k_warn("Failed to remove peer for AP: %d\n", ret);
2020
2021 kfree(arvif->u.ap.noa_data);
2022 }
2023
2024 ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
2025 if (ret)
2026 ath10k_warn("WMI vdev delete failed: %d\n", ret);
2027
2028 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
2029 ar->monitor_present = false;
2030
2031 ath10k_peer_cleanup(ar, arvif->vdev_id);
2032
2033 mutex_unlock(&ar->conf_mutex);
2034}
2035
2036/*
2037 * FIXME: Has to be verified.
2038 */
2039#define SUPPORTED_FILTERS \
2040 (FIF_PROMISC_IN_BSS | \
2041 FIF_ALLMULTI | \
2042 FIF_CONTROL | \
2043 FIF_PSPOLL | \
2044 FIF_OTHER_BSS | \
2045 FIF_BCN_PRBRESP_PROMISC | \
2046 FIF_PROBE_REQ | \
2047 FIF_FCSFAIL)
2048
2049static void ath10k_configure_filter(struct ieee80211_hw *hw,
2050 unsigned int changed_flags,
2051 unsigned int *total_flags,
2052 u64 multicast)
2053{
2054 struct ath10k *ar = hw->priv;
2055 int ret;
2056
2057 mutex_lock(&ar->conf_mutex);
2058
2059 changed_flags &= SUPPORTED_FILTERS;
2060 *total_flags &= SUPPORTED_FILTERS;
2061 ar->filter_flags = *total_flags;
2062
2063 if ((ar->filter_flags & FIF_PROMISC_IN_BSS) &&
2064 !ar->monitor_enabled) {
2065 ret = ath10k_monitor_start(ar, ar->monitor_vdev_id);
2066 if (ret)
2067 ath10k_warn("Unable to start monitor mode\n");
2068 else
2069 ath10k_dbg(ATH10K_DBG_MAC, "Monitor mode started\n");
2070 } else if (!(ar->filter_flags & FIF_PROMISC_IN_BSS) &&
2071 ar->monitor_enabled) {
2072 ret = ath10k_monitor_stop(ar);
2073 if (ret)
2074 ath10k_warn("Unable to stop monitor mode\n");
2075 else
2076 ath10k_dbg(ATH10K_DBG_MAC, "Monitor mode stopped\n");
2077 }
2078
2079 mutex_unlock(&ar->conf_mutex);
2080}
2081
2082static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
2083 struct ieee80211_vif *vif,
2084 struct ieee80211_bss_conf *info,
2085 u32 changed)
2086{
2087 struct ath10k *ar = hw->priv;
2088 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2089 int ret = 0;
2090
2091 mutex_lock(&ar->conf_mutex);
2092
2093 if (changed & BSS_CHANGED_IBSS)
2094 ath10k_control_ibss(arvif, info, vif->addr);
2095
2096 if (changed & BSS_CHANGED_BEACON_INT) {
2097 arvif->beacon_interval = info->beacon_int;
2098 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
2099 WMI_VDEV_PARAM_BEACON_INTERVAL,
2100 arvif->beacon_interval);
2101 if (ret)
2102 ath10k_warn("Failed to set beacon interval for VDEV: %d\n",
2103 arvif->vdev_id);
2104 else
2105 ath10k_dbg(ATH10K_DBG_MAC,
2106 "Beacon interval: %d set for VDEV: %d\n",
2107 arvif->beacon_interval, arvif->vdev_id);
2108 }
2109
2110 if (changed & BSS_CHANGED_BEACON) {
2111 ret = ath10k_wmi_pdev_set_param(ar,
2112 WMI_PDEV_PARAM_BEACON_TX_MODE,
2113 WMI_BEACON_STAGGERED_MODE);
2114 if (ret)
2115 ath10k_warn("Failed to set beacon mode for VDEV: %d\n",
2116 arvif->vdev_id);
2117 else
2118 ath10k_dbg(ATH10K_DBG_MAC,
2119 "Set staggered beacon mode for VDEV: %d\n",
2120 arvif->vdev_id);
2121 }
2122
John W. Linvilleb70727e82013-06-13 13:34:29 -04002123 if (changed & BSS_CHANGED_BEACON_INFO) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002124 arvif->dtim_period = info->dtim_period;
2125
2126 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
2127 WMI_VDEV_PARAM_DTIM_PERIOD,
2128 arvif->dtim_period);
2129 if (ret)
2130 ath10k_warn("Failed to set dtim period for VDEV: %d\n",
2131 arvif->vdev_id);
2132 else
2133 ath10k_dbg(ATH10K_DBG_MAC,
2134 "Set dtim period: %d for VDEV: %d\n",
2135 arvif->dtim_period, arvif->vdev_id);
2136 }
2137
2138 if (changed & BSS_CHANGED_SSID &&
2139 vif->type == NL80211_IFTYPE_AP) {
2140 arvif->u.ap.ssid_len = info->ssid_len;
2141 if (info->ssid_len)
2142 memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
2143 arvif->u.ap.hidden_ssid = info->hidden_ssid;
2144 }
2145
2146 if (changed & BSS_CHANGED_BSSID) {
2147 if (!is_zero_ether_addr(info->bssid)) {
2148 ret = ath10k_peer_create(ar, arvif->vdev_id,
2149 info->bssid);
2150 if (ret)
2151 ath10k_warn("Failed to add peer: %pM for VDEV: %d\n",
2152 info->bssid, arvif->vdev_id);
2153 else
2154 ath10k_dbg(ATH10K_DBG_MAC,
2155 "Added peer: %pM for VDEV: %d\n",
2156 info->bssid, arvif->vdev_id);
2157
2158
2159 if (vif->type == NL80211_IFTYPE_STATION) {
2160 /*
2161 * this is never erased as we it for crypto key
2162 * clearing; this is FW requirement
2163 */
2164 memcpy(arvif->u.sta.bssid, info->bssid,
2165 ETH_ALEN);
2166
2167 ret = ath10k_vdev_start(arvif);
2168 if (!ret)
2169 ath10k_dbg(ATH10K_DBG_MAC,
2170 "VDEV: %d started with BSSID: %pM\n",
2171 arvif->vdev_id, info->bssid);
2172 }
2173
2174 /*
2175 * Mac80211 does not keep IBSS bssid when leaving IBSS,
2176 * so driver need to store it. It is needed when leaving
2177 * IBSS in order to remove BSSID peer.
2178 */
2179 if (vif->type == NL80211_IFTYPE_ADHOC)
2180 memcpy(arvif->u.ibss.bssid, info->bssid,
2181 ETH_ALEN);
2182 }
2183 }
2184
2185 if (changed & BSS_CHANGED_BEACON_ENABLED)
2186 ath10k_control_beaconing(arvif, info);
2187
2188 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
2189 u32 cts_prot;
2190 if (info->use_cts_prot)
2191 cts_prot = 1;
2192 else
2193 cts_prot = 0;
2194
2195 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
2196 WMI_VDEV_PARAM_ENABLE_RTSCTS,
2197 cts_prot);
2198 if (ret)
2199 ath10k_warn("Failed to set CTS prot for VDEV: %d\n",
2200 arvif->vdev_id);
2201 else
2202 ath10k_dbg(ATH10K_DBG_MAC,
2203 "Set CTS prot: %d for VDEV: %d\n",
2204 cts_prot, arvif->vdev_id);
2205 }
2206
2207 if (changed & BSS_CHANGED_ERP_SLOT) {
2208 u32 slottime;
2209 if (info->use_short_slot)
2210 slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
2211
2212 else
2213 slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
2214
2215 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
2216 WMI_VDEV_PARAM_SLOT_TIME,
2217 slottime);
2218 if (ret)
2219 ath10k_warn("Failed to set erp slot for VDEV: %d\n",
2220 arvif->vdev_id);
2221 else
2222 ath10k_dbg(ATH10K_DBG_MAC,
2223 "Set slottime: %d for VDEV: %d\n",
2224 slottime, arvif->vdev_id);
2225 }
2226
2227 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
2228 u32 preamble;
2229 if (info->use_short_preamble)
2230 preamble = WMI_VDEV_PREAMBLE_SHORT;
2231 else
2232 preamble = WMI_VDEV_PREAMBLE_LONG;
2233
2234 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
2235 WMI_VDEV_PARAM_PREAMBLE,
2236 preamble);
2237 if (ret)
2238 ath10k_warn("Failed to set preamble for VDEV: %d\n",
2239 arvif->vdev_id);
2240 else
2241 ath10k_dbg(ATH10K_DBG_MAC,
2242 "Set preamble: %d for VDEV: %d\n",
2243 preamble, arvif->vdev_id);
2244 }
2245
2246 if (changed & BSS_CHANGED_ASSOC) {
2247 if (info->assoc)
2248 ath10k_bss_assoc(hw, vif, info);
2249 }
2250
2251 mutex_unlock(&ar->conf_mutex);
2252}
2253
2254static int ath10k_hw_scan(struct ieee80211_hw *hw,
2255 struct ieee80211_vif *vif,
2256 struct cfg80211_scan_request *req)
2257{
2258 struct ath10k *ar = hw->priv;
2259 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2260 struct wmi_start_scan_arg arg;
2261 int ret = 0;
2262 int i;
2263
2264 mutex_lock(&ar->conf_mutex);
2265
2266 spin_lock_bh(&ar->data_lock);
2267 if (ar->scan.in_progress) {
2268 spin_unlock_bh(&ar->data_lock);
2269 ret = -EBUSY;
2270 goto exit;
2271 }
2272
2273 INIT_COMPLETION(ar->scan.started);
2274 INIT_COMPLETION(ar->scan.completed);
2275 ar->scan.in_progress = true;
2276 ar->scan.aborting = false;
2277 ar->scan.is_roc = false;
2278 ar->scan.vdev_id = arvif->vdev_id;
2279 spin_unlock_bh(&ar->data_lock);
2280
2281 memset(&arg, 0, sizeof(arg));
2282 ath10k_wmi_start_scan_init(ar, &arg);
2283 arg.vdev_id = arvif->vdev_id;
2284 arg.scan_id = ATH10K_SCAN_ID;
2285
2286 if (!req->no_cck)
2287 arg.scan_ctrl_flags |= WMI_SCAN_ADD_CCK_RATES;
2288
2289 if (req->ie_len) {
2290 arg.ie_len = req->ie_len;
2291 memcpy(arg.ie, req->ie, arg.ie_len);
2292 }
2293
2294 if (req->n_ssids) {
2295 arg.n_ssids = req->n_ssids;
2296 for (i = 0; i < arg.n_ssids; i++) {
2297 arg.ssids[i].len = req->ssids[i].ssid_len;
2298 arg.ssids[i].ssid = req->ssids[i].ssid;
2299 }
2300 }
2301
2302 if (req->n_channels) {
2303 arg.n_channels = req->n_channels;
2304 for (i = 0; i < arg.n_channels; i++)
2305 arg.channels[i] = req->channels[i]->center_freq;
2306 }
2307
2308 ret = ath10k_start_scan(ar, &arg);
2309 if (ret) {
2310 ath10k_warn("could not start hw scan (%d)\n", ret);
2311 spin_lock_bh(&ar->data_lock);
2312 ar->scan.in_progress = false;
2313 spin_unlock_bh(&ar->data_lock);
2314 }
2315
2316exit:
2317 mutex_unlock(&ar->conf_mutex);
2318 return ret;
2319}
2320
2321static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
2322 struct ieee80211_vif *vif)
2323{
2324 struct ath10k *ar = hw->priv;
2325 int ret;
2326
2327 mutex_lock(&ar->conf_mutex);
2328 ret = ath10k_abort_scan(ar);
2329 if (ret) {
2330 ath10k_warn("couldn't abort scan (%d). forcefully sending scan completion to mac80211\n",
2331 ret);
2332 ieee80211_scan_completed(hw, 1 /* aborted */);
2333 }
2334 mutex_unlock(&ar->conf_mutex);
2335}
2336
2337static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
2338 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
2339 struct ieee80211_key_conf *key)
2340{
2341 struct ath10k *ar = hw->priv;
2342 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2343 struct ath10k_peer *peer;
2344 const u8 *peer_addr;
2345 bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
2346 key->cipher == WLAN_CIPHER_SUITE_WEP104;
2347 int ret = 0;
2348
2349 if (key->keyidx > WMI_MAX_KEY_INDEX)
2350 return -ENOSPC;
2351
2352 mutex_lock(&ar->conf_mutex);
2353
2354 if (sta)
2355 peer_addr = sta->addr;
2356 else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
2357 peer_addr = vif->bss_conf.bssid;
2358 else
2359 peer_addr = vif->addr;
2360
2361 key->hw_key_idx = key->keyidx;
2362
2363 /* the peer should not disappear in mid-way (unless FW goes awry) since
2364 * we already hold conf_mutex. we just make sure its there now. */
2365 spin_lock_bh(&ar->data_lock);
2366 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
2367 spin_unlock_bh(&ar->data_lock);
2368
2369 if (!peer) {
2370 if (cmd == SET_KEY) {
2371 ath10k_warn("cannot install key for non-existent peer %pM\n",
2372 peer_addr);
2373 ret = -EOPNOTSUPP;
2374 goto exit;
2375 } else {
2376 /* if the peer doesn't exist there is no key to disable
2377 * anymore */
2378 goto exit;
2379 }
2380 }
2381
2382 if (is_wep) {
2383 if (cmd == SET_KEY)
2384 arvif->wep_keys[key->keyidx] = key;
2385 else
2386 arvif->wep_keys[key->keyidx] = NULL;
2387
2388 if (cmd == DISABLE_KEY)
2389 ath10k_clear_vdev_key(arvif, key);
2390 }
2391
2392 ret = ath10k_install_key(arvif, key, cmd, peer_addr);
2393 if (ret) {
2394 ath10k_warn("ath10k_install_key failed (%d)\n", ret);
2395 goto exit;
2396 }
2397
2398 spin_lock_bh(&ar->data_lock);
2399 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
2400 if (peer && cmd == SET_KEY)
2401 peer->keys[key->keyidx] = key;
2402 else if (peer && cmd == DISABLE_KEY)
2403 peer->keys[key->keyidx] = NULL;
2404 else if (peer == NULL)
2405 /* impossible unless FW goes crazy */
2406 ath10k_warn("peer %pM disappeared!\n", peer_addr);
2407 spin_unlock_bh(&ar->data_lock);
2408
2409exit:
2410 mutex_unlock(&ar->conf_mutex);
2411 return ret;
2412}
2413
2414static int ath10k_sta_state(struct ieee80211_hw *hw,
2415 struct ieee80211_vif *vif,
2416 struct ieee80211_sta *sta,
2417 enum ieee80211_sta_state old_state,
2418 enum ieee80211_sta_state new_state)
2419{
2420 struct ath10k *ar = hw->priv;
2421 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2422 int ret = 0;
2423
2424 mutex_lock(&ar->conf_mutex);
2425
2426 if (old_state == IEEE80211_STA_NOTEXIST &&
2427 new_state == IEEE80211_STA_NONE &&
2428 vif->type != NL80211_IFTYPE_STATION) {
2429 /*
2430 * New station addition.
2431 */
2432 ret = ath10k_peer_create(ar, arvif->vdev_id, sta->addr);
2433 if (ret)
2434 ath10k_warn("Failed to add peer: %pM for VDEV: %d\n",
2435 sta->addr, arvif->vdev_id);
2436 else
2437 ath10k_dbg(ATH10K_DBG_MAC,
2438 "Added peer: %pM for VDEV: %d\n",
2439 sta->addr, arvif->vdev_id);
2440 } else if ((old_state == IEEE80211_STA_NONE &&
2441 new_state == IEEE80211_STA_NOTEXIST)) {
2442 /*
2443 * Existing station deletion.
2444 */
2445 ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
2446 if (ret)
2447 ath10k_warn("Failed to delete peer: %pM for VDEV: %d\n",
2448 sta->addr, arvif->vdev_id);
2449 else
2450 ath10k_dbg(ATH10K_DBG_MAC,
2451 "Removed peer: %pM for VDEV: %d\n",
2452 sta->addr, arvif->vdev_id);
2453
2454 if (vif->type == NL80211_IFTYPE_STATION)
2455 ath10k_bss_disassoc(hw, vif);
2456 } else if (old_state == IEEE80211_STA_AUTH &&
2457 new_state == IEEE80211_STA_ASSOC &&
2458 (vif->type == NL80211_IFTYPE_AP ||
2459 vif->type == NL80211_IFTYPE_ADHOC)) {
2460 /*
2461 * New association.
2462 */
2463 ret = ath10k_station_assoc(ar, arvif, sta);
2464 if (ret)
2465 ath10k_warn("Failed to associate station: %pM\n",
2466 sta->addr);
2467 else
2468 ath10k_dbg(ATH10K_DBG_MAC,
2469 "Station %pM moved to assoc state\n",
2470 sta->addr);
2471 } else if (old_state == IEEE80211_STA_ASSOC &&
2472 new_state == IEEE80211_STA_AUTH &&
2473 (vif->type == NL80211_IFTYPE_AP ||
2474 vif->type == NL80211_IFTYPE_ADHOC)) {
2475 /*
2476 * Disassociation.
2477 */
2478 ret = ath10k_station_disassoc(ar, arvif, sta);
2479 if (ret)
2480 ath10k_warn("Failed to disassociate station: %pM\n",
2481 sta->addr);
2482 else
2483 ath10k_dbg(ATH10K_DBG_MAC,
2484 "Station %pM moved to disassociated state\n",
2485 sta->addr);
2486 }
2487
2488 mutex_unlock(&ar->conf_mutex);
2489 return ret;
2490}
2491
2492static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
2493 u16 ac, bool enable)
2494{
2495 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2496 u32 value = 0;
2497 int ret = 0;
2498
Michal Kazior548db542013-07-05 16:15:15 +03002499 lockdep_assert_held(&ar->conf_mutex);
2500
Kalle Valo5e3dd152013-06-12 20:52:10 +03002501 if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
2502 return 0;
2503
2504 switch (ac) {
2505 case IEEE80211_AC_VO:
2506 value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
2507 WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
2508 break;
2509 case IEEE80211_AC_VI:
2510 value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
2511 WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
2512 break;
2513 case IEEE80211_AC_BE:
2514 value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
2515 WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
2516 break;
2517 case IEEE80211_AC_BK:
2518 value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
2519 WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
2520 break;
2521 }
2522
2523 if (enable)
2524 arvif->u.sta.uapsd |= value;
2525 else
2526 arvif->u.sta.uapsd &= ~value;
2527
2528 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
2529 WMI_STA_PS_PARAM_UAPSD,
2530 arvif->u.sta.uapsd);
2531 if (ret) {
2532 ath10k_warn("could not set uapsd params %d\n", ret);
2533 goto exit;
2534 }
2535
2536 if (arvif->u.sta.uapsd)
2537 value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
2538 else
2539 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
2540
2541 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
2542 WMI_STA_PS_PARAM_RX_WAKE_POLICY,
2543 value);
2544 if (ret)
2545 ath10k_warn("could not set rx wake param %d\n", ret);
2546
2547exit:
2548 return ret;
2549}
2550
2551static int ath10k_conf_tx(struct ieee80211_hw *hw,
2552 struct ieee80211_vif *vif, u16 ac,
2553 const struct ieee80211_tx_queue_params *params)
2554{
2555 struct ath10k *ar = hw->priv;
2556 struct wmi_wmm_params_arg *p = NULL;
2557 int ret;
2558
2559 mutex_lock(&ar->conf_mutex);
2560
2561 switch (ac) {
2562 case IEEE80211_AC_VO:
2563 p = &ar->wmm_params.ac_vo;
2564 break;
2565 case IEEE80211_AC_VI:
2566 p = &ar->wmm_params.ac_vi;
2567 break;
2568 case IEEE80211_AC_BE:
2569 p = &ar->wmm_params.ac_be;
2570 break;
2571 case IEEE80211_AC_BK:
2572 p = &ar->wmm_params.ac_bk;
2573 break;
2574 }
2575
2576 if (WARN_ON(!p)) {
2577 ret = -EINVAL;
2578 goto exit;
2579 }
2580
2581 p->cwmin = params->cw_min;
2582 p->cwmax = params->cw_max;
2583 p->aifs = params->aifs;
2584
2585 /*
2586 * The channel time duration programmed in the HW is in absolute
2587 * microseconds, while mac80211 gives the txop in units of
2588 * 32 microseconds.
2589 */
2590 p->txop = params->txop * 32;
2591
2592 /* FIXME: FW accepts wmm params per hw, not per vif */
2593 ret = ath10k_wmi_pdev_set_wmm_params(ar, &ar->wmm_params);
2594 if (ret) {
2595 ath10k_warn("could not set wmm params %d\n", ret);
2596 goto exit;
2597 }
2598
2599 ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
2600 if (ret)
2601 ath10k_warn("could not set sta uapsd %d\n", ret);
2602
2603exit:
2604 mutex_unlock(&ar->conf_mutex);
2605 return ret;
2606}
2607
2608#define ATH10K_ROC_TIMEOUT_HZ (2*HZ)
2609
2610static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
2611 struct ieee80211_vif *vif,
2612 struct ieee80211_channel *chan,
2613 int duration,
2614 enum ieee80211_roc_type type)
2615{
2616 struct ath10k *ar = hw->priv;
2617 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2618 struct wmi_start_scan_arg arg;
2619 int ret;
2620
2621 mutex_lock(&ar->conf_mutex);
2622
2623 spin_lock_bh(&ar->data_lock);
2624 if (ar->scan.in_progress) {
2625 spin_unlock_bh(&ar->data_lock);
2626 ret = -EBUSY;
2627 goto exit;
2628 }
2629
2630 INIT_COMPLETION(ar->scan.started);
2631 INIT_COMPLETION(ar->scan.completed);
2632 INIT_COMPLETION(ar->scan.on_channel);
2633 ar->scan.in_progress = true;
2634 ar->scan.aborting = false;
2635 ar->scan.is_roc = true;
2636 ar->scan.vdev_id = arvif->vdev_id;
2637 ar->scan.roc_freq = chan->center_freq;
2638 spin_unlock_bh(&ar->data_lock);
2639
2640 memset(&arg, 0, sizeof(arg));
2641 ath10k_wmi_start_scan_init(ar, &arg);
2642 arg.vdev_id = arvif->vdev_id;
2643 arg.scan_id = ATH10K_SCAN_ID;
2644 arg.n_channels = 1;
2645 arg.channels[0] = chan->center_freq;
2646 arg.dwell_time_active = duration;
2647 arg.dwell_time_passive = duration;
2648 arg.max_scan_time = 2 * duration;
2649 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
2650 arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
2651
2652 ret = ath10k_start_scan(ar, &arg);
2653 if (ret) {
2654 ath10k_warn("could not start roc scan (%d)\n", ret);
2655 spin_lock_bh(&ar->data_lock);
2656 ar->scan.in_progress = false;
2657 spin_unlock_bh(&ar->data_lock);
2658 goto exit;
2659 }
2660
2661 ret = wait_for_completion_timeout(&ar->scan.on_channel, 3*HZ);
2662 if (ret == 0) {
2663 ath10k_warn("could not switch to channel for roc scan\n");
2664 ath10k_abort_scan(ar);
2665 ret = -ETIMEDOUT;
2666 goto exit;
2667 }
2668
2669 ret = 0;
2670exit:
2671 mutex_unlock(&ar->conf_mutex);
2672 return ret;
2673}
2674
2675static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw)
2676{
2677 struct ath10k *ar = hw->priv;
2678
2679 mutex_lock(&ar->conf_mutex);
2680 ath10k_abort_scan(ar);
2681 mutex_unlock(&ar->conf_mutex);
2682
2683 return 0;
2684}
2685
2686/*
2687 * Both RTS and Fragmentation threshold are interface-specific
2688 * in ath10k, but device-specific in mac80211.
2689 */
2690static void ath10k_set_rts_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
2691{
2692 struct ath10k_generic_iter *ar_iter = data;
2693 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2694 u32 rts = ar_iter->ar->hw->wiphy->rts_threshold;
2695
Michal Kazior548db542013-07-05 16:15:15 +03002696 lockdep_assert_held(&arvif->ar->conf_mutex);
2697
Kalle Valo5e3dd152013-06-12 20:52:10 +03002698 rts = min_t(u32, rts, ATH10K_RTS_MAX);
2699
2700 ar_iter->ret = ath10k_wmi_vdev_set_param(ar_iter->ar, arvif->vdev_id,
2701 WMI_VDEV_PARAM_RTS_THRESHOLD,
2702 rts);
2703 if (ar_iter->ret)
2704 ath10k_warn("Failed to set RTS threshold for VDEV: %d\n",
2705 arvif->vdev_id);
2706 else
2707 ath10k_dbg(ATH10K_DBG_MAC,
2708 "Set RTS threshold: %d for VDEV: %d\n",
2709 rts, arvif->vdev_id);
2710}
2711
2712static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
2713{
2714 struct ath10k_generic_iter ar_iter;
2715 struct ath10k *ar = hw->priv;
2716
2717 memset(&ar_iter, 0, sizeof(struct ath10k_generic_iter));
2718 ar_iter.ar = ar;
2719
2720 mutex_lock(&ar->conf_mutex);
Michal Kazior80c78c62013-07-05 16:15:03 +03002721 ieee80211_iterate_active_interfaces_atomic(
Michal Kazior671b96d2013-07-05 16:15:05 +03002722 hw, IEEE80211_IFACE_ITER_NORMAL,
Michal Kazior80c78c62013-07-05 16:15:03 +03002723 ath10k_set_rts_iter, &ar_iter);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002724 mutex_unlock(&ar->conf_mutex);
2725
2726 return ar_iter.ret;
2727}
2728
2729static void ath10k_set_frag_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
2730{
2731 struct ath10k_generic_iter *ar_iter = data;
2732 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2733 u32 frag = ar_iter->ar->hw->wiphy->frag_threshold;
2734 int ret;
2735
Michal Kazior548db542013-07-05 16:15:15 +03002736 lockdep_assert_held(&arvif->ar->conf_mutex);
2737
Kalle Valo5e3dd152013-06-12 20:52:10 +03002738 frag = clamp_t(u32, frag,
2739 ATH10K_FRAGMT_THRESHOLD_MIN,
2740 ATH10K_FRAGMT_THRESHOLD_MAX);
2741
2742 ret = ath10k_wmi_vdev_set_param(ar_iter->ar, arvif->vdev_id,
2743 WMI_VDEV_PARAM_FRAGMENTATION_THRESHOLD,
2744 frag);
2745
2746 ar_iter->ret = ret;
2747 if (ar_iter->ret)
2748 ath10k_warn("Failed to set frag threshold for VDEV: %d\n",
2749 arvif->vdev_id);
2750 else
2751 ath10k_dbg(ATH10K_DBG_MAC,
2752 "Set frag threshold: %d for VDEV: %d\n",
2753 frag, arvif->vdev_id);
2754}
2755
2756static int ath10k_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
2757{
2758 struct ath10k_generic_iter ar_iter;
2759 struct ath10k *ar = hw->priv;
2760
2761 memset(&ar_iter, 0, sizeof(struct ath10k_generic_iter));
2762 ar_iter.ar = ar;
2763
2764 mutex_lock(&ar->conf_mutex);
Michal Kazior80c78c62013-07-05 16:15:03 +03002765 ieee80211_iterate_active_interfaces_atomic(
Michal Kazior671b96d2013-07-05 16:15:05 +03002766 hw, IEEE80211_IFACE_ITER_NORMAL,
Michal Kazior80c78c62013-07-05 16:15:03 +03002767 ath10k_set_frag_iter, &ar_iter);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002768 mutex_unlock(&ar->conf_mutex);
2769
2770 return ar_iter.ret;
2771}
2772
2773static void ath10k_flush(struct ieee80211_hw *hw, u32 queues, bool drop)
2774{
2775 struct ath10k *ar = hw->priv;
2776 int ret;
2777
2778 /* mac80211 doesn't care if we really xmit queued frames or not
2779 * we'll collect those frames either way if we stop/delete vdevs */
2780 if (drop)
2781 return;
2782
Michal Kazior548db542013-07-05 16:15:15 +03002783 mutex_lock(&ar->conf_mutex);
2784
Michal Kazioredb82362013-07-05 16:15:14 +03002785 ret = wait_event_timeout(ar->htt.empty_tx_wq, ({
Kalle Valo5e3dd152013-06-12 20:52:10 +03002786 bool empty;
Michal Kazioredb82362013-07-05 16:15:14 +03002787 spin_lock_bh(&ar->htt.tx_lock);
2788 empty = bitmap_empty(ar->htt.used_msdu_ids,
2789 ar->htt.max_num_pending_tx);
2790 spin_unlock_bh(&ar->htt.tx_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002791 (empty);
2792 }), ATH10K_FLUSH_TIMEOUT_HZ);
2793 if (ret <= 0)
2794 ath10k_warn("tx not flushed\n");
Michal Kazior548db542013-07-05 16:15:15 +03002795
2796 mutex_unlock(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002797}
2798
2799/* TODO: Implement this function properly
2800 * For now it is needed to reply to Probe Requests in IBSS mode.
2801 * Propably we need this information from FW.
2802 */
2803static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
2804{
2805 return 1;
2806}
2807
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002808#ifdef CONFIG_PM
2809static int ath10k_suspend(struct ieee80211_hw *hw,
2810 struct cfg80211_wowlan *wowlan)
2811{
2812 struct ath10k *ar = hw->priv;
2813 int ret;
2814
2815 ar->is_target_paused = false;
2816
2817 ret = ath10k_wmi_pdev_suspend_target(ar);
2818 if (ret) {
2819 ath10k_warn("could not suspend target (%d)\n", ret);
2820 return 1;
2821 }
2822
2823 ret = wait_event_interruptible_timeout(ar->event_queue,
2824 ar->is_target_paused == true,
2825 1 * HZ);
2826 if (ret < 0) {
2827 ath10k_warn("suspend interrupted (%d)\n", ret);
2828 goto resume;
2829 } else if (ret == 0) {
2830 ath10k_warn("suspend timed out - target pause event never came\n");
2831 goto resume;
2832 }
2833
2834 ret = ath10k_hif_suspend(ar);
2835 if (ret) {
2836 ath10k_warn("could not suspend hif (%d)\n", ret);
2837 goto resume;
2838 }
2839
2840 return 0;
2841resume:
2842 ret = ath10k_wmi_pdev_resume_target(ar);
2843 if (ret)
2844 ath10k_warn("could not resume target (%d)\n", ret);
2845 return 1;
2846}
2847
2848static int ath10k_resume(struct ieee80211_hw *hw)
2849{
2850 struct ath10k *ar = hw->priv;
2851 int ret;
2852
2853 ret = ath10k_hif_resume(ar);
2854 if (ret) {
2855 ath10k_warn("could not resume hif (%d)\n", ret);
2856 return 1;
2857 }
2858
2859 ret = ath10k_wmi_pdev_resume_target(ar);
2860 if (ret) {
2861 ath10k_warn("could not resume target (%d)\n", ret);
2862 return 1;
2863 }
2864
2865 return 0;
2866}
2867#endif
2868
Kalle Valo5e3dd152013-06-12 20:52:10 +03002869static const struct ieee80211_ops ath10k_ops = {
2870 .tx = ath10k_tx,
2871 .start = ath10k_start,
2872 .stop = ath10k_stop,
2873 .config = ath10k_config,
2874 .add_interface = ath10k_add_interface,
2875 .remove_interface = ath10k_remove_interface,
2876 .configure_filter = ath10k_configure_filter,
2877 .bss_info_changed = ath10k_bss_info_changed,
2878 .hw_scan = ath10k_hw_scan,
2879 .cancel_hw_scan = ath10k_cancel_hw_scan,
2880 .set_key = ath10k_set_key,
2881 .sta_state = ath10k_sta_state,
2882 .conf_tx = ath10k_conf_tx,
2883 .remain_on_channel = ath10k_remain_on_channel,
2884 .cancel_remain_on_channel = ath10k_cancel_remain_on_channel,
2885 .set_rts_threshold = ath10k_set_rts_threshold,
2886 .set_frag_threshold = ath10k_set_frag_threshold,
2887 .flush = ath10k_flush,
2888 .tx_last_beacon = ath10k_tx_last_beacon,
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002889#ifdef CONFIG_PM
2890 .suspend = ath10k_suspend,
2891 .resume = ath10k_resume,
2892#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03002893};
2894
2895#define RATETAB_ENT(_rate, _rateid, _flags) { \
2896 .bitrate = (_rate), \
2897 .flags = (_flags), \
2898 .hw_value = (_rateid), \
2899}
2900
2901#define CHAN2G(_channel, _freq, _flags) { \
2902 .band = IEEE80211_BAND_2GHZ, \
2903 .hw_value = (_channel), \
2904 .center_freq = (_freq), \
2905 .flags = (_flags), \
2906 .max_antenna_gain = 0, \
2907 .max_power = 30, \
2908}
2909
2910#define CHAN5G(_channel, _freq, _flags) { \
2911 .band = IEEE80211_BAND_5GHZ, \
2912 .hw_value = (_channel), \
2913 .center_freq = (_freq), \
2914 .flags = (_flags), \
2915 .max_antenna_gain = 0, \
2916 .max_power = 30, \
2917}
2918
2919static const struct ieee80211_channel ath10k_2ghz_channels[] = {
2920 CHAN2G(1, 2412, 0),
2921 CHAN2G(2, 2417, 0),
2922 CHAN2G(3, 2422, 0),
2923 CHAN2G(4, 2427, 0),
2924 CHAN2G(5, 2432, 0),
2925 CHAN2G(6, 2437, 0),
2926 CHAN2G(7, 2442, 0),
2927 CHAN2G(8, 2447, 0),
2928 CHAN2G(9, 2452, 0),
2929 CHAN2G(10, 2457, 0),
2930 CHAN2G(11, 2462, 0),
2931 CHAN2G(12, 2467, 0),
2932 CHAN2G(13, 2472, 0),
2933 CHAN2G(14, 2484, 0),
2934};
2935
2936static const struct ieee80211_channel ath10k_5ghz_channels[] = {
Michal Kazior429ff562013-06-26 08:54:54 +02002937 CHAN5G(36, 5180, 0),
2938 CHAN5G(40, 5200, 0),
2939 CHAN5G(44, 5220, 0),
2940 CHAN5G(48, 5240, 0),
2941 CHAN5G(52, 5260, 0),
2942 CHAN5G(56, 5280, 0),
2943 CHAN5G(60, 5300, 0),
2944 CHAN5G(64, 5320, 0),
2945 CHAN5G(100, 5500, 0),
2946 CHAN5G(104, 5520, 0),
2947 CHAN5G(108, 5540, 0),
2948 CHAN5G(112, 5560, 0),
2949 CHAN5G(116, 5580, 0),
2950 CHAN5G(120, 5600, 0),
2951 CHAN5G(124, 5620, 0),
2952 CHAN5G(128, 5640, 0),
2953 CHAN5G(132, 5660, 0),
2954 CHAN5G(136, 5680, 0),
2955 CHAN5G(140, 5700, 0),
2956 CHAN5G(149, 5745, 0),
2957 CHAN5G(153, 5765, 0),
2958 CHAN5G(157, 5785, 0),
2959 CHAN5G(161, 5805, 0),
2960 CHAN5G(165, 5825, 0),
Kalle Valo5e3dd152013-06-12 20:52:10 +03002961};
2962
2963static struct ieee80211_rate ath10k_rates[] = {
2964 /* CCK */
2965 RATETAB_ENT(10, 0x82, 0),
2966 RATETAB_ENT(20, 0x84, 0),
2967 RATETAB_ENT(55, 0x8b, 0),
2968 RATETAB_ENT(110, 0x96, 0),
2969 /* OFDM */
2970 RATETAB_ENT(60, 0x0c, 0),
2971 RATETAB_ENT(90, 0x12, 0),
2972 RATETAB_ENT(120, 0x18, 0),
2973 RATETAB_ENT(180, 0x24, 0),
2974 RATETAB_ENT(240, 0x30, 0),
2975 RATETAB_ENT(360, 0x48, 0),
2976 RATETAB_ENT(480, 0x60, 0),
2977 RATETAB_ENT(540, 0x6c, 0),
2978};
2979
2980#define ath10k_a_rates (ath10k_rates + 4)
2981#define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - 4)
2982#define ath10k_g_rates (ath10k_rates + 0)
2983#define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
2984
2985struct ath10k *ath10k_mac_create(void)
2986{
2987 struct ieee80211_hw *hw;
2988 struct ath10k *ar;
2989
2990 hw = ieee80211_alloc_hw(sizeof(struct ath10k), &ath10k_ops);
2991 if (!hw)
2992 return NULL;
2993
2994 ar = hw->priv;
2995 ar->hw = hw;
2996
2997 return ar;
2998}
2999
3000void ath10k_mac_destroy(struct ath10k *ar)
3001{
3002 ieee80211_free_hw(ar->hw);
3003}
3004
3005static const struct ieee80211_iface_limit ath10k_if_limits[] = {
3006 {
3007 .max = 8,
3008 .types = BIT(NL80211_IFTYPE_STATION)
3009 | BIT(NL80211_IFTYPE_P2P_CLIENT)
3010 | BIT(NL80211_IFTYPE_P2P_GO)
3011 | BIT(NL80211_IFTYPE_AP)
3012 }
3013};
3014
3015static const struct ieee80211_iface_combination ath10k_if_comb = {
3016 .limits = ath10k_if_limits,
3017 .n_limits = ARRAY_SIZE(ath10k_if_limits),
3018 .max_interfaces = 8,
3019 .num_different_channels = 1,
3020 .beacon_int_infra_match = true,
3021};
3022
3023static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
3024{
3025 struct ieee80211_sta_vht_cap vht_cap = {0};
3026 u16 mcs_map;
3027
3028 vht_cap.vht_supported = 1;
3029 vht_cap.cap = ar->vht_cap_info;
3030
3031 /* FIXME: check dynamically how many streams board supports */
3032 mcs_map = IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
3033 IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 |
3034 IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 |
3035 IEEE80211_VHT_MCS_NOT_SUPPORTED << 6 |
3036 IEEE80211_VHT_MCS_NOT_SUPPORTED << 8 |
3037 IEEE80211_VHT_MCS_NOT_SUPPORTED << 10 |
3038 IEEE80211_VHT_MCS_NOT_SUPPORTED << 12 |
3039 IEEE80211_VHT_MCS_NOT_SUPPORTED << 14;
3040
3041 vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
3042 vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
3043
3044 return vht_cap;
3045}
3046
3047static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
3048{
3049 int i;
3050 struct ieee80211_sta_ht_cap ht_cap = {0};
3051
3052 if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
3053 return ht_cap;
3054
3055 ht_cap.ht_supported = 1;
3056 ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
3057 ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
3058 ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
3059 ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
3060 ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
3061
3062 if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
3063 ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
3064
3065 if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
3066 ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
3067
3068 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
3069 u32 smps;
3070
3071 smps = WLAN_HT_CAP_SM_PS_DYNAMIC;
3072 smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
3073
3074 ht_cap.cap |= smps;
3075 }
3076
3077 if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC)
3078 ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
3079
3080 if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
3081 u32 stbc;
3082
3083 stbc = ar->ht_cap_info;
3084 stbc &= WMI_HT_CAP_RX_STBC;
3085 stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
3086 stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
3087 stbc &= IEEE80211_HT_CAP_RX_STBC;
3088
3089 ht_cap.cap |= stbc;
3090 }
3091
3092 if (ar->ht_cap_info & WMI_HT_CAP_LDPC)
3093 ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
3094
3095 if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
3096 ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
3097
3098 /* max AMSDU is implicitly taken from vht_cap_info */
3099 if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
3100 ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
3101
3102 for (i = 0; i < WMI_MAX_SPATIAL_STREAM; i++)
3103 ht_cap.mcs.rx_mask[i] = 0xFF;
3104
3105 ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
3106
3107 return ht_cap;
3108}
3109
3110
3111static void ath10k_get_arvif_iter(void *data, u8 *mac,
3112 struct ieee80211_vif *vif)
3113{
3114 struct ath10k_vif_iter *arvif_iter = data;
3115 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3116
3117 if (arvif->vdev_id == arvif_iter->vdev_id)
3118 arvif_iter->arvif = arvif;
3119}
3120
3121struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
3122{
3123 struct ath10k_vif_iter arvif_iter;
3124 u32 flags;
3125
3126 memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
3127 arvif_iter.vdev_id = vdev_id;
3128
3129 flags = IEEE80211_IFACE_ITER_RESUME_ALL;
3130 ieee80211_iterate_active_interfaces_atomic(ar->hw,
3131 flags,
3132 ath10k_get_arvif_iter,
3133 &arvif_iter);
3134 if (!arvif_iter.arvif) {
3135 ath10k_warn("No VIF found for VDEV: %d\n", vdev_id);
3136 return NULL;
3137 }
3138
3139 return arvif_iter.arvif;
3140}
3141
3142int ath10k_mac_register(struct ath10k *ar)
3143{
3144 struct ieee80211_supported_band *band;
3145 struct ieee80211_sta_vht_cap vht_cap;
3146 struct ieee80211_sta_ht_cap ht_cap;
3147 void *channels;
3148 int ret;
3149
3150 SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
3151
3152 SET_IEEE80211_DEV(ar->hw, ar->dev);
3153
3154 ht_cap = ath10k_get_ht_cap(ar);
3155 vht_cap = ath10k_create_vht_cap(ar);
3156
3157 if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
3158 channels = kmemdup(ath10k_2ghz_channels,
3159 sizeof(ath10k_2ghz_channels),
3160 GFP_KERNEL);
3161 if (!channels)
3162 return -ENOMEM;
3163
3164 band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
3165 band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
3166 band->channels = channels;
3167 band->n_bitrates = ath10k_g_rates_size;
3168 band->bitrates = ath10k_g_rates;
3169 band->ht_cap = ht_cap;
3170
3171 /* vht is not supported in 2.4 GHz */
3172
3173 ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
3174 }
3175
3176 if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
3177 channels = kmemdup(ath10k_5ghz_channels,
3178 sizeof(ath10k_5ghz_channels),
3179 GFP_KERNEL);
3180 if (!channels) {
3181 if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
3182 band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
3183 kfree(band->channels);
3184 }
3185 return -ENOMEM;
3186 }
3187
3188 band = &ar->mac.sbands[IEEE80211_BAND_5GHZ];
3189 band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
3190 band->channels = channels;
3191 band->n_bitrates = ath10k_a_rates_size;
3192 band->bitrates = ath10k_a_rates;
3193 band->ht_cap = ht_cap;
3194 band->vht_cap = vht_cap;
3195 ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
3196 }
3197
3198 ar->hw->wiphy->interface_modes =
3199 BIT(NL80211_IFTYPE_STATION) |
3200 BIT(NL80211_IFTYPE_ADHOC) |
3201 BIT(NL80211_IFTYPE_AP) |
3202 BIT(NL80211_IFTYPE_P2P_CLIENT) |
3203 BIT(NL80211_IFTYPE_P2P_GO);
3204
3205 ar->hw->flags = IEEE80211_HW_SIGNAL_DBM |
3206 IEEE80211_HW_SUPPORTS_PS |
3207 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
3208 IEEE80211_HW_SUPPORTS_UAPSD |
3209 IEEE80211_HW_MFP_CAPABLE |
3210 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
3211 IEEE80211_HW_HAS_RATE_CONTROL |
3212 IEEE80211_HW_SUPPORTS_STATIC_SMPS |
3213 IEEE80211_HW_WANT_MONITOR_VIF |
3214 IEEE80211_HW_AP_LINK_PS;
3215
3216 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
3217 ar->hw->flags |= IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS;
3218
3219 if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
3220 ar->hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
3221 ar->hw->flags |= IEEE80211_HW_TX_AMPDU_SETUP_IN_HW;
3222 }
3223
3224 ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
3225 ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
3226
3227 ar->hw->vif_data_size = sizeof(struct ath10k_vif);
3228
3229 ar->hw->channel_change_time = 5000;
3230 ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
3231
3232 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
3233 ar->hw->wiphy->max_remain_on_channel_duration = 5000;
3234
3235 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
3236 /*
3237 * on LL hardware queues are managed entirely by the FW
3238 * so we only advertise to mac we can do the queues thing
3239 */
3240 ar->hw->queues = 4;
3241
3242 ar->hw->wiphy->iface_combinations = &ath10k_if_comb;
3243 ar->hw->wiphy->n_iface_combinations = 1;
3244
3245 ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
3246 ath10k_reg_notifier);
3247 if (ret) {
3248 ath10k_err("Regulatory initialization failed\n");
3249 return ret;
3250 }
3251
3252 ret = ieee80211_register_hw(ar->hw);
3253 if (ret) {
3254 ath10k_err("ieee80211 registration failed: %d\n", ret);
3255 return ret;
3256 }
3257
3258 if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
3259 ret = regulatory_hint(ar->hw->wiphy,
3260 ar->ath_common.regulatory.alpha2);
3261 if (ret)
3262 goto exit;
3263 }
3264
3265 return 0;
3266exit:
3267 ieee80211_unregister_hw(ar->hw);
3268 return ret;
3269}
3270
3271void ath10k_mac_unregister(struct ath10k *ar)
3272{
3273 ieee80211_unregister_hw(ar->hw);
3274
3275 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
3276 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
3277
3278 SET_IEEE80211_DEV(ar->hw, NULL);
3279}