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