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