blob: 97ac8c87cba232646f5f82617ea396b8c2573870 [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
Wolfram Sang16735d02013-11-14 14:32:02 -080095 reinit_completion(&ar->install_key_done);
Kalle Valo5e3dd152013-06-12 20:52:10 +030096
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
Michal Kazior424121c2013-07-22 14:13:31 +0200335static int ath10k_mac_set_rts(struct ath10k_vif *arvif, u32 value)
336{
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200337 struct ath10k *ar = arvif->ar;
338 u32 vdev_param;
339
Michal Kazior424121c2013-07-22 14:13:31 +0200340 if (value != 0xFFFFFFFF)
341 value = min_t(u32, arvif->ar->hw->wiphy->rts_threshold,
342 ATH10K_RTS_MAX);
343
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200344 vdev_param = ar->wmi.vdev_param->rts_threshold;
345 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
Michal Kazior424121c2013-07-22 14:13:31 +0200346}
347
348static int ath10k_mac_set_frag(struct ath10k_vif *arvif, u32 value)
349{
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200350 struct ath10k *ar = arvif->ar;
351 u32 vdev_param;
352
Michal Kazior424121c2013-07-22 14:13:31 +0200353 if (value != 0xFFFFFFFF)
354 value = clamp_t(u32, arvif->ar->hw->wiphy->frag_threshold,
355 ATH10K_FRAGMT_THRESHOLD_MIN,
356 ATH10K_FRAGMT_THRESHOLD_MAX);
357
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200358 vdev_param = ar->wmi.vdev_param->fragmentation_threshold;
359 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
Michal Kazior424121c2013-07-22 14:13:31 +0200360}
361
Kalle Valo5e3dd152013-06-12 20:52:10 +0300362static int ath10k_peer_delete(struct ath10k *ar, u32 vdev_id, const u8 *addr)
363{
364 int ret;
365
366 lockdep_assert_held(&ar->conf_mutex);
367
368 ret = ath10k_wmi_peer_delete(ar, vdev_id, addr);
369 if (ret)
370 return ret;
371
372 ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
373 if (ret)
374 return ret;
375
376 return 0;
377}
378
379static void ath10k_peer_cleanup(struct ath10k *ar, u32 vdev_id)
380{
381 struct ath10k_peer *peer, *tmp;
382
383 lockdep_assert_held(&ar->conf_mutex);
384
385 spin_lock_bh(&ar->data_lock);
386 list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
387 if (peer->vdev_id != vdev_id)
388 continue;
389
390 ath10k_warn("removing stale peer %pM from vdev_id %d\n",
391 peer->addr, vdev_id);
392
393 list_del(&peer->list);
394 kfree(peer);
395 }
396 spin_unlock_bh(&ar->data_lock);
397}
398
Michal Kaziora96d7742013-07-16 09:38:56 +0200399static void ath10k_peer_cleanup_all(struct ath10k *ar)
400{
401 struct ath10k_peer *peer, *tmp;
402
403 lockdep_assert_held(&ar->conf_mutex);
404
405 spin_lock_bh(&ar->data_lock);
406 list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
407 list_del(&peer->list);
408 kfree(peer);
409 }
410 spin_unlock_bh(&ar->data_lock);
411}
412
Kalle Valo5e3dd152013-06-12 20:52:10 +0300413/************************/
414/* Interface management */
415/************************/
416
417static inline int ath10k_vdev_setup_sync(struct ath10k *ar)
418{
419 int ret;
420
Michal Kazior548db542013-07-05 16:15:15 +0300421 lockdep_assert_held(&ar->conf_mutex);
422
Kalle Valo5e3dd152013-06-12 20:52:10 +0300423 ret = wait_for_completion_timeout(&ar->vdev_setup_done,
424 ATH10K_VDEV_SETUP_TIMEOUT_HZ);
425 if (ret == 0)
426 return -ETIMEDOUT;
427
428 return 0;
429}
430
431static int ath10k_vdev_start(struct ath10k_vif *arvif)
432{
433 struct ath10k *ar = arvif->ar;
434 struct ieee80211_conf *conf = &ar->hw->conf;
435 struct ieee80211_channel *channel = conf->chandef.chan;
436 struct wmi_vdev_start_request_arg arg = {};
437 int ret = 0;
438
439 lockdep_assert_held(&ar->conf_mutex);
440
Wolfram Sang16735d02013-11-14 14:32:02 -0800441 reinit_completion(&ar->vdev_setup_done);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300442
443 arg.vdev_id = arvif->vdev_id;
444 arg.dtim_period = arvif->dtim_period;
445 arg.bcn_intval = arvif->beacon_interval;
446
447 arg.channel.freq = channel->center_freq;
448
449 arg.channel.band_center_freq1 = conf->chandef.center_freq1;
450
451 arg.channel.mode = chan_to_phymode(&conf->chandef);
452
453 arg.channel.min_power = channel->max_power * 3;
454 arg.channel.max_power = channel->max_power * 4;
455 arg.channel.max_reg_power = channel->max_reg_power * 4;
456 arg.channel.max_antenna_gain = channel->max_antenna_gain;
457
458 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
459 arg.ssid = arvif->u.ap.ssid;
460 arg.ssid_len = arvif->u.ap.ssid_len;
461 arg.hidden_ssid = arvif->u.ap.hidden_ssid;
462 } else if (arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
463 arg.ssid = arvif->vif->bss_conf.ssid;
464 arg.ssid_len = arvif->vif->bss_conf.ssid_len;
465 }
466
Kalle Valo38a1d472013-09-08 17:56:14 +0300467 ath10k_dbg(ATH10K_DBG_MAC,
468 "mac vdev %d start center_freq %d phymode %s\n",
469 arg.vdev_id, arg.channel.freq,
470 ath10k_wmi_phymode_str(arg.channel.mode));
471
Kalle Valo5e3dd152013-06-12 20:52:10 +0300472 ret = ath10k_wmi_vdev_start(ar, &arg);
473 if (ret) {
474 ath10k_warn("WMI vdev start failed: ret %d\n", ret);
475 return ret;
476 }
477
478 ret = ath10k_vdev_setup_sync(ar);
479 if (ret) {
480 ath10k_warn("vdev setup failed %d\n", ret);
481 return ret;
482 }
483
484 return ret;
485}
486
487static int ath10k_vdev_stop(struct ath10k_vif *arvif)
488{
489 struct ath10k *ar = arvif->ar;
490 int ret;
491
492 lockdep_assert_held(&ar->conf_mutex);
493
Wolfram Sang16735d02013-11-14 14:32:02 -0800494 reinit_completion(&ar->vdev_setup_done);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300495
496 ret = ath10k_wmi_vdev_stop(ar, arvif->vdev_id);
497 if (ret) {
498 ath10k_warn("WMI vdev stop failed: ret %d\n", ret);
499 return ret;
500 }
501
502 ret = ath10k_vdev_setup_sync(ar);
503 if (ret) {
504 ath10k_warn("vdev setup failed %d\n", ret);
505 return ret;
506 }
507
508 return ret;
509}
510
511static int ath10k_monitor_start(struct ath10k *ar, int vdev_id)
512{
513 struct ieee80211_channel *channel = ar->hw->conf.chandef.chan;
514 struct wmi_vdev_start_request_arg arg = {};
Kalle Valo5e3dd152013-06-12 20:52:10 +0300515 int ret = 0;
516
517 lockdep_assert_held(&ar->conf_mutex);
518
Kalle Valo5e3dd152013-06-12 20:52:10 +0300519 arg.vdev_id = vdev_id;
520 arg.channel.freq = channel->center_freq;
521 arg.channel.band_center_freq1 = ar->hw->conf.chandef.center_freq1;
522
523 /* TODO setup this dynamically, what in case we
524 don't have any vifs? */
525 arg.channel.mode = chan_to_phymode(&ar->hw->conf.chandef);
526
527 arg.channel.min_power = channel->max_power * 3;
528 arg.channel.max_power = channel->max_power * 4;
529 arg.channel.max_reg_power = channel->max_reg_power * 4;
530 arg.channel.max_antenna_gain = channel->max_antenna_gain;
531
532 ret = ath10k_wmi_vdev_start(ar, &arg);
533 if (ret) {
534 ath10k_warn("Monitor vdev start failed: ret %d\n", ret);
535 return ret;
536 }
537
538 ret = ath10k_vdev_setup_sync(ar);
539 if (ret) {
540 ath10k_warn("Monitor vdev setup failed %d\n", ret);
541 return ret;
542 }
543
544 ret = ath10k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
545 if (ret) {
546 ath10k_warn("Monitor vdev up failed: %d\n", ret);
547 goto vdev_stop;
548 }
549
550 ar->monitor_vdev_id = vdev_id;
551 ar->monitor_enabled = true;
552
553 return 0;
554
555vdev_stop:
556 ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
557 if (ret)
558 ath10k_warn("Monitor vdev stop failed: %d\n", ret);
559
560 return ret;
561}
562
563static int ath10k_monitor_stop(struct ath10k *ar)
564{
565 int ret = 0;
566
567 lockdep_assert_held(&ar->conf_mutex);
568
Marek Puzyniak52fa0192013-09-24 14:06:24 +0200569 ret = ath10k_wmi_vdev_down(ar, ar->monitor_vdev_id);
570 if (ret)
571 ath10k_warn("Monitor vdev down failed: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300572
573 ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
574 if (ret)
575 ath10k_warn("Monitor vdev stop failed: %d\n", ret);
576
577 ret = ath10k_vdev_setup_sync(ar);
578 if (ret)
579 ath10k_warn("Monitor_down sync failed: %d\n", ret);
580
581 ar->monitor_enabled = false;
582 return ret;
583}
584
585static int ath10k_monitor_create(struct ath10k *ar)
586{
587 int bit, ret = 0;
588
589 lockdep_assert_held(&ar->conf_mutex);
590
591 if (ar->monitor_present) {
592 ath10k_warn("Monitor mode already enabled\n");
593 return 0;
594 }
595
596 bit = ffs(ar->free_vdev_map);
597 if (bit == 0) {
598 ath10k_warn("No free VDEV slots\n");
599 return -ENOMEM;
600 }
601
602 ar->monitor_vdev_id = bit - 1;
603 ar->free_vdev_map &= ~(1 << ar->monitor_vdev_id);
604
605 ret = ath10k_wmi_vdev_create(ar, ar->monitor_vdev_id,
606 WMI_VDEV_TYPE_MONITOR,
607 0, ar->mac_addr);
608 if (ret) {
609 ath10k_warn("WMI vdev monitor create failed: ret %d\n", ret);
610 goto vdev_fail;
611 }
612
Kalle Valo60c3daa2013-09-08 17:56:07 +0300613 ath10k_dbg(ATH10K_DBG_MAC, "mac monitor vdev %d created\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300614 ar->monitor_vdev_id);
615
616 ar->monitor_present = true;
617 return 0;
618
619vdev_fail:
620 /*
621 * Restore the ID to the global map.
622 */
623 ar->free_vdev_map |= 1 << (ar->monitor_vdev_id);
624 return ret;
625}
626
627static int ath10k_monitor_destroy(struct ath10k *ar)
628{
629 int ret = 0;
630
631 lockdep_assert_held(&ar->conf_mutex);
632
633 if (!ar->monitor_present)
634 return 0;
635
636 ret = ath10k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
637 if (ret) {
638 ath10k_warn("WMI vdev monitor delete failed: %d\n", ret);
639 return ret;
640 }
641
642 ar->free_vdev_map |= 1 << (ar->monitor_vdev_id);
643 ar->monitor_present = false;
644
Kalle Valo60c3daa2013-09-08 17:56:07 +0300645 ath10k_dbg(ATH10K_DBG_MAC, "mac monitor vdev %d deleted\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300646 ar->monitor_vdev_id);
647 return ret;
648}
649
650static void ath10k_control_beaconing(struct ath10k_vif *arvif,
651 struct ieee80211_bss_conf *info)
652{
653 int ret = 0;
654
Michal Kazior548db542013-07-05 16:15:15 +0300655 lockdep_assert_held(&arvif->ar->conf_mutex);
656
Kalle Valo5e3dd152013-06-12 20:52:10 +0300657 if (!info->enable_beacon) {
658 ath10k_vdev_stop(arvif);
659 return;
660 }
661
662 arvif->tx_seq_no = 0x1000;
663
664 ret = ath10k_vdev_start(arvif);
665 if (ret)
666 return;
667
668 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, 0, info->bssid);
669 if (ret) {
670 ath10k_warn("Failed to bring up VDEV: %d\n",
671 arvif->vdev_id);
672 return;
673 }
Kalle Valo60c3daa2013-09-08 17:56:07 +0300674 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300675}
676
677static void ath10k_control_ibss(struct ath10k_vif *arvif,
678 struct ieee80211_bss_conf *info,
679 const u8 self_peer[ETH_ALEN])
680{
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200681 u32 vdev_param;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300682 int ret = 0;
683
Michal Kazior548db542013-07-05 16:15:15 +0300684 lockdep_assert_held(&arvif->ar->conf_mutex);
685
Kalle Valo5e3dd152013-06-12 20:52:10 +0300686 if (!info->ibss_joined) {
687 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, self_peer);
688 if (ret)
689 ath10k_warn("Failed to delete IBSS self peer:%pM for VDEV:%d ret:%d\n",
690 self_peer, arvif->vdev_id, ret);
691
692 if (is_zero_ether_addr(arvif->u.ibss.bssid))
693 return;
694
695 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id,
696 arvif->u.ibss.bssid);
697 if (ret) {
698 ath10k_warn("Failed to delete IBSS BSSID peer:%pM for VDEV:%d ret:%d\n",
699 arvif->u.ibss.bssid, arvif->vdev_id, ret);
700 return;
701 }
702
703 memset(arvif->u.ibss.bssid, 0, ETH_ALEN);
704
705 return;
706 }
707
708 ret = ath10k_peer_create(arvif->ar, arvif->vdev_id, self_peer);
709 if (ret) {
710 ath10k_warn("Failed to create IBSS self peer:%pM for VDEV:%d ret:%d\n",
711 self_peer, arvif->vdev_id, ret);
712 return;
713 }
714
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200715 vdev_param = arvif->ar->wmi.vdev_param->atim_window;
716 ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +0300717 ATH10K_DEFAULT_ATIM);
718 if (ret)
719 ath10k_warn("Failed to set IBSS ATIM for VDEV:%d ret:%d\n",
720 arvif->vdev_id, ret);
721}
722
723/*
724 * Review this when mac80211 gains per-interface powersave support.
725 */
Michal Kaziorad088bf2013-10-16 15:44:46 +0300726static int ath10k_mac_vif_setup_ps(struct ath10k_vif *arvif)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300727{
Michal Kaziorad088bf2013-10-16 15:44:46 +0300728 struct ath10k *ar = arvif->ar;
729 struct ieee80211_conf *conf = &ar->hw->conf;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300730 enum wmi_sta_powersave_param param;
731 enum wmi_sta_ps_mode psmode;
732 int ret;
733
Michal Kazior548db542013-07-05 16:15:15 +0300734 lockdep_assert_held(&arvif->ar->conf_mutex);
735
Michal Kaziorad088bf2013-10-16 15:44:46 +0300736 if (arvif->vif->type != NL80211_IFTYPE_STATION)
737 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300738
739 if (conf->flags & IEEE80211_CONF_PS) {
740 psmode = WMI_STA_PS_MODE_ENABLED;
741 param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
742
Michal Kaziorad088bf2013-10-16 15:44:46 +0300743 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
Kalle Valo5e3dd152013-06-12 20:52:10 +0300744 conf->dynamic_ps_timeout);
745 if (ret) {
746 ath10k_warn("Failed to set inactivity time for VDEV: %d\n",
747 arvif->vdev_id);
Michal Kaziorad088bf2013-10-16 15:44:46 +0300748 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300749 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300750 } else {
751 psmode = WMI_STA_PS_MODE_DISABLED;
752 }
753
Kalle Valo60c3daa2013-09-08 17:56:07 +0300754 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d psmode %s\n",
755 arvif->vdev_id, psmode ? "enable" : "disable");
756
Michal Kaziorad088bf2013-10-16 15:44:46 +0300757 ret = ath10k_wmi_set_psmode(ar, arvif->vdev_id, psmode);
758 if (ret) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300759 ath10k_warn("Failed to set PS Mode: %d for VDEV: %d\n",
760 psmode, arvif->vdev_id);
Michal Kaziorad088bf2013-10-16 15:44:46 +0300761 return ret;
762 }
763
764 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300765}
766
767/**********************/
768/* Station management */
769/**********************/
770
771static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
772 struct ath10k_vif *arvif,
773 struct ieee80211_sta *sta,
774 struct ieee80211_bss_conf *bss_conf,
775 struct wmi_peer_assoc_complete_arg *arg)
776{
Michal Kazior548db542013-07-05 16:15:15 +0300777 lockdep_assert_held(&ar->conf_mutex);
778
Kalle Valo5e3dd152013-06-12 20:52:10 +0300779 memcpy(arg->addr, sta->addr, ETH_ALEN);
780 arg->vdev_id = arvif->vdev_id;
781 arg->peer_aid = sta->aid;
782 arg->peer_flags |= WMI_PEER_AUTH;
783
784 if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
785 /*
786 * Seems FW have problems with Power Save in STA
787 * mode when we setup this parameter to high (eg. 5).
788 * Often we see that FW don't send NULL (with clean P flags)
789 * frame even there is info about buffered frames in beacons.
790 * Sometimes we have to wait more than 10 seconds before FW
791 * will wakeup. Often sending one ping from AP to our device
792 * just fail (more than 50%).
793 *
794 * Seems setting this FW parameter to 1 couse FW
795 * will check every beacon and will wakup immediately
796 * after detection buffered data.
797 */
798 arg->peer_listen_intval = 1;
799 else
800 arg->peer_listen_intval = ar->hw->conf.listen_interval;
801
802 arg->peer_num_spatial_streams = 1;
803
804 /*
805 * The assoc capabilities are available only in managed mode.
806 */
807 if (arvif->vdev_type == WMI_VDEV_TYPE_STA && bss_conf)
808 arg->peer_caps = bss_conf->assoc_capability;
809}
810
811static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
812 struct ath10k_vif *arvif,
813 struct wmi_peer_assoc_complete_arg *arg)
814{
815 struct ieee80211_vif *vif = arvif->vif;
816 struct ieee80211_bss_conf *info = &vif->bss_conf;
817 struct cfg80211_bss *bss;
818 const u8 *rsnie = NULL;
819 const u8 *wpaie = NULL;
820
Michal Kazior548db542013-07-05 16:15:15 +0300821 lockdep_assert_held(&ar->conf_mutex);
822
Kalle Valo5e3dd152013-06-12 20:52:10 +0300823 bss = cfg80211_get_bss(ar->hw->wiphy, ar->hw->conf.chandef.chan,
824 info->bssid, NULL, 0, 0, 0);
825 if (bss) {
826 const struct cfg80211_bss_ies *ies;
827
828 rcu_read_lock();
829 rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
830
831 ies = rcu_dereference(bss->ies);
832
833 wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
834 WLAN_OUI_TYPE_MICROSOFT_WPA,
835 ies->data,
836 ies->len);
837 rcu_read_unlock();
838 cfg80211_put_bss(ar->hw->wiphy, bss);
839 }
840
841 /* FIXME: base on RSN IE/WPA IE is a correct idea? */
842 if (rsnie || wpaie) {
843 ath10k_dbg(ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
844 arg->peer_flags |= WMI_PEER_NEED_PTK_4_WAY;
845 }
846
847 if (wpaie) {
848 ath10k_dbg(ATH10K_DBG_WMI, "%s: wpa ie found\n", __func__);
849 arg->peer_flags |= WMI_PEER_NEED_GTK_2_WAY;
850 }
851}
852
853static void ath10k_peer_assoc_h_rates(struct ath10k *ar,
854 struct ieee80211_sta *sta,
855 struct wmi_peer_assoc_complete_arg *arg)
856{
857 struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
858 const struct ieee80211_supported_band *sband;
859 const struct ieee80211_rate *rates;
860 u32 ratemask;
861 int i;
862
Michal Kazior548db542013-07-05 16:15:15 +0300863 lockdep_assert_held(&ar->conf_mutex);
864
Kalle Valo5e3dd152013-06-12 20:52:10 +0300865 sband = ar->hw->wiphy->bands[ar->hw->conf.chandef.chan->band];
866 ratemask = sta->supp_rates[ar->hw->conf.chandef.chan->band];
867 rates = sband->bitrates;
868
869 rateset->num_rates = 0;
870
871 for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
872 if (!(ratemask & 1))
873 continue;
874
875 rateset->rates[rateset->num_rates] = rates->hw_value;
876 rateset->num_rates++;
877 }
878}
879
880static void ath10k_peer_assoc_h_ht(struct ath10k *ar,
881 struct ieee80211_sta *sta,
882 struct wmi_peer_assoc_complete_arg *arg)
883{
884 const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
885 int smps;
886 int i, n;
887
Michal Kazior548db542013-07-05 16:15:15 +0300888 lockdep_assert_held(&ar->conf_mutex);
889
Kalle Valo5e3dd152013-06-12 20:52:10 +0300890 if (!ht_cap->ht_supported)
891 return;
892
893 arg->peer_flags |= WMI_PEER_HT;
894 arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
895 ht_cap->ampdu_factor)) - 1;
896
897 arg->peer_mpdu_density =
898 ath10k_parse_mpdudensity(ht_cap->ampdu_density);
899
900 arg->peer_ht_caps = ht_cap->cap;
901 arg->peer_rate_caps |= WMI_RC_HT_FLAG;
902
903 if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
904 arg->peer_flags |= WMI_PEER_LDPC;
905
906 if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
907 arg->peer_flags |= WMI_PEER_40MHZ;
908 arg->peer_rate_caps |= WMI_RC_CW40_FLAG;
909 }
910
911 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
912 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
913
914 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
915 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
916
917 if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
918 arg->peer_rate_caps |= WMI_RC_TX_STBC_FLAG;
919 arg->peer_flags |= WMI_PEER_STBC;
920 }
921
922 if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
923 u32 stbc;
924 stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
925 stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
926 stbc = stbc << WMI_RC_RX_STBC_FLAG_S;
927 arg->peer_rate_caps |= stbc;
928 arg->peer_flags |= WMI_PEER_STBC;
929 }
930
931 smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
932 smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
933
934 if (smps == WLAN_HT_CAP_SM_PS_STATIC) {
935 arg->peer_flags |= WMI_PEER_SPATIAL_MUX;
936 arg->peer_flags |= WMI_PEER_STATIC_MIMOPS;
937 } else if (smps == WLAN_HT_CAP_SM_PS_DYNAMIC) {
938 arg->peer_flags |= WMI_PEER_SPATIAL_MUX;
939 arg->peer_flags |= WMI_PEER_DYN_MIMOPS;
940 }
941
942 if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
943 arg->peer_rate_caps |= WMI_RC_TS_FLAG;
944 else if (ht_cap->mcs.rx_mask[1])
945 arg->peer_rate_caps |= WMI_RC_DS_FLAG;
946
947 for (i = 0, n = 0; i < IEEE80211_HT_MCS_MASK_LEN*8; i++)
948 if (ht_cap->mcs.rx_mask[i/8] & (1 << i%8))
949 arg->peer_ht_rates.rates[n++] = i;
950
951 arg->peer_ht_rates.num_rates = n;
952 arg->peer_num_spatial_streams = max((n+7) / 8, 1);
953
Kalle Valo60c3daa2013-09-08 17:56:07 +0300954 ath10k_dbg(ATH10K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
955 arg->addr,
Kalle Valo5e3dd152013-06-12 20:52:10 +0300956 arg->peer_ht_rates.num_rates,
957 arg->peer_num_spatial_streams);
958}
959
960static void ath10k_peer_assoc_h_qos_ap(struct ath10k *ar,
961 struct ath10k_vif *arvif,
962 struct ieee80211_sta *sta,
963 struct ieee80211_bss_conf *bss_conf,
964 struct wmi_peer_assoc_complete_arg *arg)
965{
966 u32 uapsd = 0;
967 u32 max_sp = 0;
968
Michal Kazior548db542013-07-05 16:15:15 +0300969 lockdep_assert_held(&ar->conf_mutex);
970
Kalle Valo5e3dd152013-06-12 20:52:10 +0300971 if (sta->wme)
972 arg->peer_flags |= WMI_PEER_QOS;
973
974 if (sta->wme && sta->uapsd_queues) {
Kalle Valo60c3daa2013-09-08 17:56:07 +0300975 ath10k_dbg(ATH10K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300976 sta->uapsd_queues, sta->max_sp);
977
978 arg->peer_flags |= WMI_PEER_APSD;
Janusz Dziedzicc69029b2013-08-07 12:10:49 +0200979 arg->peer_rate_caps |= WMI_RC_UAPSD_FLAG;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300980
981 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
982 uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
983 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
984 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
985 uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
986 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
987 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
988 uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
989 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
990 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
991 uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
992 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
993
994
995 if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
996 max_sp = sta->max_sp;
997
998 ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
999 sta->addr,
1000 WMI_AP_PS_PEER_PARAM_UAPSD,
1001 uapsd);
1002
1003 ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1004 sta->addr,
1005 WMI_AP_PS_PEER_PARAM_MAX_SP,
1006 max_sp);
1007
1008 /* TODO setup this based on STA listen interval and
1009 beacon interval. Currently we don't know
1010 sta->listen_interval - mac80211 patch required.
1011 Currently use 10 seconds */
1012 ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1013 sta->addr,
1014 WMI_AP_PS_PEER_PARAM_AGEOUT_TIME,
1015 10);
1016 }
1017}
1018
1019static void ath10k_peer_assoc_h_qos_sta(struct ath10k *ar,
1020 struct ath10k_vif *arvif,
1021 struct ieee80211_sta *sta,
1022 struct ieee80211_bss_conf *bss_conf,
1023 struct wmi_peer_assoc_complete_arg *arg)
1024{
1025 if (bss_conf->qos)
1026 arg->peer_flags |= WMI_PEER_QOS;
1027}
1028
1029static void ath10k_peer_assoc_h_vht(struct ath10k *ar,
1030 struct ieee80211_sta *sta,
1031 struct wmi_peer_assoc_complete_arg *arg)
1032{
1033 const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
Sujith Manoharana24b88b2013-10-07 19:51:57 -07001034 u8 ampdu_factor;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001035
1036 if (!vht_cap->vht_supported)
1037 return;
1038
1039 arg->peer_flags |= WMI_PEER_VHT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001040 arg->peer_vht_caps = vht_cap->cap;
1041
Sujith Manoharana24b88b2013-10-07 19:51:57 -07001042
1043 ampdu_factor = (vht_cap->cap &
1044 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
1045 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
1046
1047 /* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
1048 * zero in VHT IE. Using it would result in degraded throughput.
1049 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
1050 * it if VHT max_mpdu is smaller. */
1051 arg->peer_max_mpdu = max(arg->peer_max_mpdu,
1052 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1053 ampdu_factor)) - 1);
1054
Kalle Valo5e3dd152013-06-12 20:52:10 +03001055 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1056 arg->peer_flags |= WMI_PEER_80MHZ;
1057
1058 arg->peer_vht_rates.rx_max_rate =
1059 __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
1060 arg->peer_vht_rates.rx_mcs_set =
1061 __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
1062 arg->peer_vht_rates.tx_max_rate =
1063 __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
1064 arg->peer_vht_rates.tx_mcs_set =
1065 __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
1066
Kalle Valo60c3daa2013-09-08 17:56:07 +03001067 ath10k_dbg(ATH10K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
1068 sta->addr, arg->peer_max_mpdu, arg->peer_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001069}
1070
1071static void ath10k_peer_assoc_h_qos(struct ath10k *ar,
1072 struct ath10k_vif *arvif,
1073 struct ieee80211_sta *sta,
1074 struct ieee80211_bss_conf *bss_conf,
1075 struct wmi_peer_assoc_complete_arg *arg)
1076{
1077 switch (arvif->vdev_type) {
1078 case WMI_VDEV_TYPE_AP:
1079 ath10k_peer_assoc_h_qos_ap(ar, arvif, sta, bss_conf, arg);
1080 break;
1081 case WMI_VDEV_TYPE_STA:
1082 ath10k_peer_assoc_h_qos_sta(ar, arvif, sta, bss_conf, arg);
1083 break;
1084 default:
1085 break;
1086 }
1087}
1088
1089static void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
1090 struct ath10k_vif *arvif,
1091 struct ieee80211_sta *sta,
1092 struct wmi_peer_assoc_complete_arg *arg)
1093{
1094 enum wmi_phy_mode phymode = MODE_UNKNOWN;
1095
Kalle Valo5e3dd152013-06-12 20:52:10 +03001096 switch (ar->hw->conf.chandef.chan->band) {
1097 case IEEE80211_BAND_2GHZ:
1098 if (sta->ht_cap.ht_supported) {
1099 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1100 phymode = MODE_11NG_HT40;
1101 else
1102 phymode = MODE_11NG_HT20;
1103 } else {
1104 phymode = MODE_11G;
1105 }
1106
1107 break;
1108 case IEEE80211_BAND_5GHZ:
Sujith Manoharan7cc45e92013-09-08 18:19:55 +03001109 /*
1110 * Check VHT first.
1111 */
1112 if (sta->vht_cap.vht_supported) {
1113 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1114 phymode = MODE_11AC_VHT80;
1115 else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1116 phymode = MODE_11AC_VHT40;
1117 else if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
1118 phymode = MODE_11AC_VHT20;
1119 } else if (sta->ht_cap.ht_supported) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001120 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1121 phymode = MODE_11NA_HT40;
1122 else
1123 phymode = MODE_11NA_HT20;
1124 } else {
1125 phymode = MODE_11A;
1126 }
1127
1128 break;
1129 default:
1130 break;
1131 }
1132
Kalle Valo38a1d472013-09-08 17:56:14 +03001133 ath10k_dbg(ATH10K_DBG_MAC, "mac peer %pM phymode %s\n",
1134 sta->addr, ath10k_wmi_phymode_str(phymode));
Kalle Valo60c3daa2013-09-08 17:56:07 +03001135
Kalle Valo5e3dd152013-06-12 20:52:10 +03001136 arg->peer_phymode = phymode;
1137 WARN_ON(phymode == MODE_UNKNOWN);
1138}
1139
Kalle Valob9ada652013-10-16 15:44:46 +03001140static int ath10k_peer_assoc_prepare(struct ath10k *ar,
1141 struct ath10k_vif *arvif,
1142 struct ieee80211_sta *sta,
1143 struct ieee80211_bss_conf *bss_conf,
1144 struct wmi_peer_assoc_complete_arg *arg)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001145{
Michal Kazior548db542013-07-05 16:15:15 +03001146 lockdep_assert_held(&ar->conf_mutex);
1147
Kalle Valob9ada652013-10-16 15:44:46 +03001148 memset(arg, 0, sizeof(*arg));
Kalle Valo5e3dd152013-06-12 20:52:10 +03001149
Kalle Valob9ada652013-10-16 15:44:46 +03001150 ath10k_peer_assoc_h_basic(ar, arvif, sta, bss_conf, arg);
1151 ath10k_peer_assoc_h_crypto(ar, arvif, arg);
1152 ath10k_peer_assoc_h_rates(ar, sta, arg);
1153 ath10k_peer_assoc_h_ht(ar, sta, arg);
1154 ath10k_peer_assoc_h_vht(ar, sta, arg);
1155 ath10k_peer_assoc_h_qos(ar, arvif, sta, bss_conf, arg);
1156 ath10k_peer_assoc_h_phymode(ar, arvif, sta, arg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001157
Kalle Valob9ada652013-10-16 15:44:46 +03001158 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001159}
1160
1161/* can be called only in mac80211 callbacks due to `key_count` usage */
1162static void ath10k_bss_assoc(struct ieee80211_hw *hw,
1163 struct ieee80211_vif *vif,
1164 struct ieee80211_bss_conf *bss_conf)
1165{
1166 struct ath10k *ar = hw->priv;
1167 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Kalle Valob9ada652013-10-16 15:44:46 +03001168 struct wmi_peer_assoc_complete_arg peer_arg;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001169 struct ieee80211_sta *ap_sta;
1170 int ret;
1171
Michal Kazior548db542013-07-05 16:15:15 +03001172 lockdep_assert_held(&ar->conf_mutex);
1173
Kalle Valo5e3dd152013-06-12 20:52:10 +03001174 rcu_read_lock();
1175
1176 ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
1177 if (!ap_sta) {
1178 ath10k_warn("Failed to find station entry for %pM\n",
1179 bss_conf->bssid);
1180 rcu_read_unlock();
1181 return;
1182 }
1183
Kalle Valob9ada652013-10-16 15:44:46 +03001184 ret = ath10k_peer_assoc_prepare(ar, arvif, ap_sta,
1185 bss_conf, &peer_arg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001186 if (ret) {
Kalle Valob9ada652013-10-16 15:44:46 +03001187 ath10k_warn("Peer assoc prepare failed for %pM\n: %d",
1188 bss_conf->bssid, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001189 rcu_read_unlock();
1190 return;
1191 }
1192
1193 rcu_read_unlock();
1194
Kalle Valob9ada652013-10-16 15:44:46 +03001195 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
1196 if (ret) {
1197 ath10k_warn("Peer assoc failed for %pM\n: %d",
1198 bss_conf->bssid, ret);
1199 return;
1200 }
1201
Kalle Valo60c3daa2013-09-08 17:56:07 +03001202 ath10k_dbg(ATH10K_DBG_MAC,
1203 "mac vdev %d up (associated) bssid %pM aid %d\n",
1204 arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
1205
Kalle Valo5e3dd152013-06-12 20:52:10 +03001206 ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, bss_conf->aid,
1207 bss_conf->bssid);
1208 if (ret)
1209 ath10k_warn("VDEV: %d up failed: ret %d\n",
1210 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001211}
1212
1213/*
1214 * FIXME: flush TIDs
1215 */
1216static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
1217 struct ieee80211_vif *vif)
1218{
1219 struct ath10k *ar = hw->priv;
1220 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1221 int ret;
1222
Michal Kazior548db542013-07-05 16:15:15 +03001223 lockdep_assert_held(&ar->conf_mutex);
1224
Kalle Valo5e3dd152013-06-12 20:52:10 +03001225 /*
1226 * For some reason, calling VDEV-DOWN before VDEV-STOP
1227 * makes the FW to send frames via HTT after disassociation.
1228 * No idea why this happens, even though VDEV-DOWN is supposed
1229 * to be analogous to link down, so just stop the VDEV.
1230 */
Kalle Valo60c3daa2013-09-08 17:56:07 +03001231 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d stop (disassociated\n",
1232 arvif->vdev_id);
1233
1234 /* FIXME: check return value */
Kalle Valo5e3dd152013-06-12 20:52:10 +03001235 ret = ath10k_vdev_stop(arvif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001236
1237 /*
1238 * If we don't call VDEV-DOWN after VDEV-STOP FW will remain active and
1239 * report beacons from previously associated network through HTT.
1240 * This in turn would spam mac80211 WARN_ON if we bring down all
1241 * interfaces as it expects there is no rx when no interface is
1242 * running.
1243 */
Kalle Valo60c3daa2013-09-08 17:56:07 +03001244 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d down\n", arvif->vdev_id);
1245
1246 /* FIXME: why don't we print error if wmi call fails? */
Kalle Valo5e3dd152013-06-12 20:52:10 +03001247 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001248
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001249 arvif->def_wep_key_idx = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001250}
1251
1252static int ath10k_station_assoc(struct ath10k *ar, struct ath10k_vif *arvif,
1253 struct ieee80211_sta *sta)
1254{
Kalle Valob9ada652013-10-16 15:44:46 +03001255 struct wmi_peer_assoc_complete_arg peer_arg;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001256 int ret = 0;
1257
Michal Kazior548db542013-07-05 16:15:15 +03001258 lockdep_assert_held(&ar->conf_mutex);
1259
Kalle Valob9ada652013-10-16 15:44:46 +03001260 ret = ath10k_peer_assoc_prepare(ar, arvif, sta, NULL, &peer_arg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001261 if (ret) {
Kalle Valob9ada652013-10-16 15:44:46 +03001262 ath10k_warn("WMI peer assoc prepare failed for %pM\n",
1263 sta->addr);
1264 return ret;
1265 }
1266
1267 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
1268 if (ret) {
1269 ath10k_warn("Peer assoc failed for STA %pM\n: %d",
1270 sta->addr, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001271 return ret;
1272 }
1273
1274 ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
1275 if (ret) {
1276 ath10k_warn("could not install peer wep keys (%d)\n", ret);
1277 return ret;
1278 }
1279
1280 return ret;
1281}
1282
1283static int ath10k_station_disassoc(struct ath10k *ar, struct ath10k_vif *arvif,
1284 struct ieee80211_sta *sta)
1285{
1286 int ret = 0;
1287
Michal Kazior548db542013-07-05 16:15:15 +03001288 lockdep_assert_held(&ar->conf_mutex);
1289
Kalle Valo5e3dd152013-06-12 20:52:10 +03001290 ret = ath10k_clear_peer_keys(arvif, sta->addr);
1291 if (ret) {
1292 ath10k_warn("could not clear all peer wep keys (%d)\n", ret);
1293 return ret;
1294 }
1295
1296 return ret;
1297}
1298
1299/**************/
1300/* Regulatory */
1301/**************/
1302
1303static int ath10k_update_channel_list(struct ath10k *ar)
1304{
1305 struct ieee80211_hw *hw = ar->hw;
1306 struct ieee80211_supported_band **bands;
1307 enum ieee80211_band band;
1308 struct ieee80211_channel *channel;
1309 struct wmi_scan_chan_list_arg arg = {0};
1310 struct wmi_channel_arg *ch;
1311 bool passive;
1312 int len;
1313 int ret;
1314 int i;
1315
Michal Kazior548db542013-07-05 16:15:15 +03001316 lockdep_assert_held(&ar->conf_mutex);
1317
Kalle Valo5e3dd152013-06-12 20:52:10 +03001318 bands = hw->wiphy->bands;
1319 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1320 if (!bands[band])
1321 continue;
1322
1323 for (i = 0; i < bands[band]->n_channels; i++) {
1324 if (bands[band]->channels[i].flags &
1325 IEEE80211_CHAN_DISABLED)
1326 continue;
1327
1328 arg.n_channels++;
1329 }
1330 }
1331
1332 len = sizeof(struct wmi_channel_arg) * arg.n_channels;
1333 arg.channels = kzalloc(len, GFP_KERNEL);
1334 if (!arg.channels)
1335 return -ENOMEM;
1336
1337 ch = arg.channels;
1338 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1339 if (!bands[band])
1340 continue;
1341
1342 for (i = 0; i < bands[band]->n_channels; i++) {
1343 channel = &bands[band]->channels[i];
1344
1345 if (channel->flags & IEEE80211_CHAN_DISABLED)
1346 continue;
1347
1348 ch->allow_ht = true;
1349
1350 /* FIXME: when should we really allow VHT? */
1351 ch->allow_vht = true;
1352
1353 ch->allow_ibss =
1354 !(channel->flags & IEEE80211_CHAN_NO_IBSS);
1355
1356 ch->ht40plus =
1357 !(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
1358
1359 passive = channel->flags & IEEE80211_CHAN_PASSIVE_SCAN;
1360 ch->passive = passive;
1361
1362 ch->freq = channel->center_freq;
1363 ch->min_power = channel->max_power * 3;
1364 ch->max_power = channel->max_power * 4;
1365 ch->max_reg_power = channel->max_reg_power * 4;
1366 ch->max_antenna_gain = channel->max_antenna_gain;
1367 ch->reg_class_id = 0; /* FIXME */
1368
1369 /* FIXME: why use only legacy modes, why not any
1370 * HT/VHT modes? Would that even make any
1371 * difference? */
1372 if (channel->band == IEEE80211_BAND_2GHZ)
1373 ch->mode = MODE_11G;
1374 else
1375 ch->mode = MODE_11A;
1376
1377 if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
1378 continue;
1379
1380 ath10k_dbg(ATH10K_DBG_WMI,
Kalle Valo60c3daa2013-09-08 17:56:07 +03001381 "mac channel [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
1382 ch - arg.channels, arg.n_channels,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001383 ch->freq, ch->max_power, ch->max_reg_power,
1384 ch->max_antenna_gain, ch->mode);
1385
1386 ch++;
1387 }
1388 }
1389
1390 ret = ath10k_wmi_scan_chan_list(ar, &arg);
1391 kfree(arg.channels);
1392
1393 return ret;
1394}
1395
Michal Kaziorf7843d72013-07-16 09:38:52 +02001396static void ath10k_regd_update(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001397{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001398 struct reg_dmn_pair_mapping *regpair;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001399 int ret;
1400
Michal Kaziorf7843d72013-07-16 09:38:52 +02001401 lockdep_assert_held(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001402
1403 ret = ath10k_update_channel_list(ar);
1404 if (ret)
1405 ath10k_warn("could not update channel list (%d)\n", ret);
1406
1407 regpair = ar->ath_common.regulatory.regpair;
Michal Kaziorf7843d72013-07-16 09:38:52 +02001408
Kalle Valo5e3dd152013-06-12 20:52:10 +03001409 /* Target allows setting up per-band regdomain but ath_common provides
1410 * a combined one only */
1411 ret = ath10k_wmi_pdev_set_regdomain(ar,
1412 regpair->regDmnEnum,
1413 regpair->regDmnEnum, /* 2ghz */
1414 regpair->regDmnEnum, /* 5ghz */
1415 regpair->reg_2ghz_ctl,
1416 regpair->reg_5ghz_ctl);
1417 if (ret)
1418 ath10k_warn("could not set pdev regdomain (%d)\n", ret);
Michal Kaziorf7843d72013-07-16 09:38:52 +02001419}
Michal Kazior548db542013-07-05 16:15:15 +03001420
Michal Kaziorf7843d72013-07-16 09:38:52 +02001421static void ath10k_reg_notifier(struct wiphy *wiphy,
1422 struct regulatory_request *request)
1423{
1424 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
1425 struct ath10k *ar = hw->priv;
1426
1427 ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
1428
1429 mutex_lock(&ar->conf_mutex);
1430 if (ar->state == ATH10K_STATE_ON)
1431 ath10k_regd_update(ar);
Michal Kazior548db542013-07-05 16:15:15 +03001432 mutex_unlock(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001433}
1434
1435/***************/
1436/* TX handlers */
1437/***************/
1438
Michal Kazior42c3aa62013-10-02 11:03:38 +02001439static u8 ath10k_tx_h_get_tid(struct ieee80211_hdr *hdr)
1440{
1441 if (ieee80211_is_mgmt(hdr->frame_control))
1442 return HTT_DATA_TX_EXT_TID_MGMT;
1443
1444 if (!ieee80211_is_data_qos(hdr->frame_control))
1445 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
1446
1447 if (!is_unicast_ether_addr(ieee80211_get_DA(hdr)))
1448 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
1449
1450 return ieee80211_get_qos_ctl(hdr)[0] & IEEE80211_QOS_CTL_TID_MASK;
1451}
1452
Michal Kaziorddb6ad72013-10-02 11:03:39 +02001453static u8 ath10k_tx_h_get_vdev_id(struct ath10k *ar,
1454 struct ieee80211_tx_info *info)
1455{
1456 if (info->control.vif)
1457 return ath10k_vif_to_arvif(info->control.vif)->vdev_id;
1458
1459 if (ar->monitor_enabled)
1460 return ar->monitor_vdev_id;
1461
1462 ath10k_warn("could not resolve vdev id\n");
1463 return 0;
1464}
1465
Kalle Valo5e3dd152013-06-12 20:52:10 +03001466/*
1467 * Frames sent to the FW have to be in "Native Wifi" format.
1468 * Strip the QoS field from the 802.11 header.
1469 */
1470static void ath10k_tx_h_qos_workaround(struct ieee80211_hw *hw,
1471 struct ieee80211_tx_control *control,
1472 struct sk_buff *skb)
1473{
1474 struct ieee80211_hdr *hdr = (void *)skb->data;
1475 u8 *qos_ctl;
1476
1477 if (!ieee80211_is_data_qos(hdr->frame_control))
1478 return;
1479
1480 qos_ctl = ieee80211_get_qos_ctl(hdr);
Michal Kaziorba0ccd72013-07-22 14:25:28 +02001481 memmove(skb->data + IEEE80211_QOS_CTL_LEN,
1482 skb->data, (void *)qos_ctl - (void *)skb->data);
1483 skb_pull(skb, IEEE80211_QOS_CTL_LEN);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001484}
1485
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001486static void ath10k_tx_wep_key_work(struct work_struct *work)
1487{
1488 struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
1489 wep_key_work);
1490 int ret, keyidx = arvif->def_wep_key_newidx;
1491
1492 if (arvif->def_wep_key_idx == keyidx)
1493 return;
1494
1495 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
1496 arvif->vdev_id, keyidx);
1497
1498 ret = ath10k_wmi_vdev_set_param(arvif->ar,
1499 arvif->vdev_id,
1500 arvif->ar->wmi.vdev_param->def_keyid,
1501 keyidx);
1502 if (ret) {
1503 ath10k_warn("could not update wep keyidx (%d)\n", ret);
1504 return;
1505 }
1506
1507 arvif->def_wep_key_idx = keyidx;
1508}
1509
Kalle Valo5e3dd152013-06-12 20:52:10 +03001510static void ath10k_tx_h_update_wep_key(struct sk_buff *skb)
1511{
1512 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1513 struct ieee80211_vif *vif = info->control.vif;
1514 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1515 struct ath10k *ar = arvif->ar;
1516 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1517 struct ieee80211_key_conf *key = info->control.hw_key;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001518
Kalle Valo5e3dd152013-06-12 20:52:10 +03001519 if (!ieee80211_has_protected(hdr->frame_control))
1520 return;
1521
1522 if (!key)
1523 return;
1524
1525 if (key->cipher != WLAN_CIPHER_SUITE_WEP40 &&
1526 key->cipher != WLAN_CIPHER_SUITE_WEP104)
1527 return;
1528
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001529 if (key->keyidx == arvif->def_wep_key_idx)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001530 return;
1531
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001532 /* FIXME: Most likely a few frames will be TXed with an old key. Simply
1533 * queueing frames until key index is updated is not an option because
1534 * sk_buff may need more processing to be done, e.g. offchannel */
1535 arvif->def_wep_key_newidx = key->keyidx;
1536 ieee80211_queue_work(ar->hw, &arvif->wep_key_work);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001537}
1538
1539static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar, struct sk_buff *skb)
1540{
1541 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1542 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1543 struct ieee80211_vif *vif = info->control.vif;
1544 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1545
1546 /* This is case only for P2P_GO */
1547 if (arvif->vdev_type != WMI_VDEV_TYPE_AP ||
1548 arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
1549 return;
1550
1551 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
1552 spin_lock_bh(&ar->data_lock);
1553 if (arvif->u.ap.noa_data)
1554 if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
1555 GFP_ATOMIC))
1556 memcpy(skb_put(skb, arvif->u.ap.noa_len),
1557 arvif->u.ap.noa_data,
1558 arvif->u.ap.noa_len);
1559 spin_unlock_bh(&ar->data_lock);
1560 }
1561}
1562
1563static void ath10k_tx_htt(struct ath10k *ar, struct sk_buff *skb)
1564{
1565 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Bartosz Markowski5e00d312013-09-26 17:47:12 +02001566 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001567
Michal Kazior961d4c32013-08-09 10:13:34 +02001568 if (ar->htt.target_version_major >= 3) {
1569 /* Since HTT 3.0 there is no separate mgmt tx command */
1570 ret = ath10k_htt_tx(&ar->htt, skb);
1571 goto exit;
1572 }
1573
Bartosz Markowski5e00d312013-09-26 17:47:12 +02001574 if (ieee80211_is_mgmt(hdr->frame_control)) {
1575 if (test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
1576 ar->fw_features)) {
1577 if (skb_queue_len(&ar->wmi_mgmt_tx_queue) >=
1578 ATH10K_MAX_NUM_MGMT_PENDING) {
1579 ath10k_warn("wmi mgmt_tx queue limit reached\n");
1580 ret = -EBUSY;
1581 goto exit;
1582 }
1583
1584 skb_queue_tail(&ar->wmi_mgmt_tx_queue, skb);
1585 ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
1586 } else {
1587 ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
1588 }
1589 } else if (!test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
1590 ar->fw_features) &&
1591 ieee80211_is_nullfunc(hdr->frame_control)) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001592 /* FW does not report tx status properly for NullFunc frames
1593 * unless they are sent through mgmt tx path. mac80211 sends
Bartosz Markowski5e00d312013-09-26 17:47:12 +02001594 * those frames when it detects link/beacon loss and depends
1595 * on the tx status to be correct. */
Michal Kazioredb82362013-07-05 16:15:14 +03001596 ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02001597 } else {
Michal Kazioredb82362013-07-05 16:15:14 +03001598 ret = ath10k_htt_tx(&ar->htt, skb);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02001599 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001600
Michal Kazior961d4c32013-08-09 10:13:34 +02001601exit:
Kalle Valo5e3dd152013-06-12 20:52:10 +03001602 if (ret) {
1603 ath10k_warn("tx failed (%d). dropping packet.\n", ret);
1604 ieee80211_free_txskb(ar->hw, skb);
1605 }
1606}
1607
1608void ath10k_offchan_tx_purge(struct ath10k *ar)
1609{
1610 struct sk_buff *skb;
1611
1612 for (;;) {
1613 skb = skb_dequeue(&ar->offchan_tx_queue);
1614 if (!skb)
1615 break;
1616
1617 ieee80211_free_txskb(ar->hw, skb);
1618 }
1619}
1620
1621void ath10k_offchan_tx_work(struct work_struct *work)
1622{
1623 struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
1624 struct ath10k_peer *peer;
1625 struct ieee80211_hdr *hdr;
1626 struct sk_buff *skb;
1627 const u8 *peer_addr;
1628 int vdev_id;
1629 int ret;
1630
1631 /* FW requirement: We must create a peer before FW will send out
1632 * an offchannel frame. Otherwise the frame will be stuck and
1633 * never transmitted. We delete the peer upon tx completion.
1634 * It is unlikely that a peer for offchannel tx will already be
1635 * present. However it may be in some rare cases so account for that.
1636 * Otherwise we might remove a legitimate peer and break stuff. */
1637
1638 for (;;) {
1639 skb = skb_dequeue(&ar->offchan_tx_queue);
1640 if (!skb)
1641 break;
1642
1643 mutex_lock(&ar->conf_mutex);
1644
Kalle Valo60c3daa2013-09-08 17:56:07 +03001645 ath10k_dbg(ATH10K_DBG_MAC, "mac offchannel skb %p\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001646 skb);
1647
1648 hdr = (struct ieee80211_hdr *)skb->data;
1649 peer_addr = ieee80211_get_DA(hdr);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02001650 vdev_id = ATH10K_SKB_CB(skb)->vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001651
1652 spin_lock_bh(&ar->data_lock);
1653 peer = ath10k_peer_find(ar, vdev_id, peer_addr);
1654 spin_unlock_bh(&ar->data_lock);
1655
1656 if (peer)
Kalle Valo60c3daa2013-09-08 17:56:07 +03001657 /* FIXME: should this use ath10k_warn()? */
Kalle Valo5e3dd152013-06-12 20:52:10 +03001658 ath10k_dbg(ATH10K_DBG_MAC, "peer %pM on vdev %d already present\n",
1659 peer_addr, vdev_id);
1660
1661 if (!peer) {
1662 ret = ath10k_peer_create(ar, vdev_id, peer_addr);
1663 if (ret)
1664 ath10k_warn("peer %pM on vdev %d not created (%d)\n",
1665 peer_addr, vdev_id, ret);
1666 }
1667
1668 spin_lock_bh(&ar->data_lock);
Wolfram Sang16735d02013-11-14 14:32:02 -08001669 reinit_completion(&ar->offchan_tx_completed);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001670 ar->offchan_tx_skb = skb;
1671 spin_unlock_bh(&ar->data_lock);
1672
1673 ath10k_tx_htt(ar, skb);
1674
1675 ret = wait_for_completion_timeout(&ar->offchan_tx_completed,
1676 3 * HZ);
1677 if (ret <= 0)
1678 ath10k_warn("timed out waiting for offchannel skb %p\n",
1679 skb);
1680
1681 if (!peer) {
1682 ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
1683 if (ret)
1684 ath10k_warn("peer %pM on vdev %d not deleted (%d)\n",
1685 peer_addr, vdev_id, ret);
1686 }
1687
1688 mutex_unlock(&ar->conf_mutex);
1689 }
1690}
1691
Bartosz Markowski5e00d312013-09-26 17:47:12 +02001692void ath10k_mgmt_over_wmi_tx_purge(struct ath10k *ar)
1693{
1694 struct sk_buff *skb;
1695
1696 for (;;) {
1697 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
1698 if (!skb)
1699 break;
1700
1701 ieee80211_free_txskb(ar->hw, skb);
1702 }
1703}
1704
1705void ath10k_mgmt_over_wmi_tx_work(struct work_struct *work)
1706{
1707 struct ath10k *ar = container_of(work, struct ath10k, wmi_mgmt_tx_work);
1708 struct sk_buff *skb;
1709 int ret;
1710
1711 for (;;) {
1712 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
1713 if (!skb)
1714 break;
1715
1716 ret = ath10k_wmi_mgmt_tx(ar, skb);
1717 if (ret)
1718 ath10k_warn("wmi mgmt_tx failed (%d)\n", ret);
1719 }
1720}
1721
Kalle Valo5e3dd152013-06-12 20:52:10 +03001722/************/
1723/* Scanning */
1724/************/
1725
1726/*
1727 * This gets called if we dont get a heart-beat during scan.
1728 * This may indicate the FW has hung and we need to abort the
1729 * scan manually to prevent cancel_hw_scan() from deadlocking
1730 */
1731void ath10k_reset_scan(unsigned long ptr)
1732{
1733 struct ath10k *ar = (struct ath10k *)ptr;
1734
1735 spin_lock_bh(&ar->data_lock);
1736 if (!ar->scan.in_progress) {
1737 spin_unlock_bh(&ar->data_lock);
1738 return;
1739 }
1740
1741 ath10k_warn("scan timeout. resetting. fw issue?\n");
1742
1743 if (ar->scan.is_roc)
1744 ieee80211_remain_on_channel_expired(ar->hw);
1745 else
1746 ieee80211_scan_completed(ar->hw, 1 /* aborted */);
1747
1748 ar->scan.in_progress = false;
1749 complete_all(&ar->scan.completed);
1750 spin_unlock_bh(&ar->data_lock);
1751}
1752
1753static int ath10k_abort_scan(struct ath10k *ar)
1754{
1755 struct wmi_stop_scan_arg arg = {
1756 .req_id = 1, /* FIXME */
1757 .req_type = WMI_SCAN_STOP_ONE,
1758 .u.scan_id = ATH10K_SCAN_ID,
1759 };
1760 int ret;
1761
1762 lockdep_assert_held(&ar->conf_mutex);
1763
1764 del_timer_sync(&ar->scan.timeout);
1765
1766 spin_lock_bh(&ar->data_lock);
1767 if (!ar->scan.in_progress) {
1768 spin_unlock_bh(&ar->data_lock);
1769 return 0;
1770 }
1771
1772 ar->scan.aborting = true;
1773 spin_unlock_bh(&ar->data_lock);
1774
1775 ret = ath10k_wmi_stop_scan(ar, &arg);
1776 if (ret) {
1777 ath10k_warn("could not submit wmi stop scan (%d)\n", ret);
Michal Kazioradb8c9b2013-07-05 16:15:16 +03001778 spin_lock_bh(&ar->data_lock);
1779 ar->scan.in_progress = false;
1780 ath10k_offchan_tx_purge(ar);
1781 spin_unlock_bh(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001782 return -EIO;
1783 }
1784
Kalle Valo5e3dd152013-06-12 20:52:10 +03001785 ret = wait_for_completion_timeout(&ar->scan.completed, 3*HZ);
1786 if (ret == 0)
1787 ath10k_warn("timed out while waiting for scan to stop\n");
1788
1789 /* scan completion may be done right after we timeout here, so let's
1790 * check the in_progress and tell mac80211 scan is completed. if we
1791 * don't do that and FW fails to send us scan completion indication
1792 * then userspace won't be able to scan anymore */
1793 ret = 0;
1794
1795 spin_lock_bh(&ar->data_lock);
1796 if (ar->scan.in_progress) {
1797 ath10k_warn("could not stop scan. its still in progress\n");
1798 ar->scan.in_progress = false;
1799 ath10k_offchan_tx_purge(ar);
1800 ret = -ETIMEDOUT;
1801 }
1802 spin_unlock_bh(&ar->data_lock);
1803
1804 return ret;
1805}
1806
1807static int ath10k_start_scan(struct ath10k *ar,
1808 const struct wmi_start_scan_arg *arg)
1809{
1810 int ret;
1811
1812 lockdep_assert_held(&ar->conf_mutex);
1813
1814 ret = ath10k_wmi_start_scan(ar, arg);
1815 if (ret)
1816 return ret;
1817
Kalle Valo5e3dd152013-06-12 20:52:10 +03001818 ret = wait_for_completion_timeout(&ar->scan.started, 1*HZ);
1819 if (ret == 0) {
1820 ath10k_abort_scan(ar);
1821 return ret;
1822 }
1823
1824 /* the scan can complete earlier, before we even
1825 * start the timer. in that case the timer handler
1826 * checks ar->scan.in_progress and bails out if its
1827 * false. Add a 200ms margin to account event/command
1828 * processing. */
1829 mod_timer(&ar->scan.timeout, jiffies +
1830 msecs_to_jiffies(arg->max_scan_time+200));
1831 return 0;
1832}
1833
1834/**********************/
1835/* mac80211 callbacks */
1836/**********************/
1837
1838static void ath10k_tx(struct ieee80211_hw *hw,
1839 struct ieee80211_tx_control *control,
1840 struct sk_buff *skb)
1841{
1842 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1843 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1844 struct ath10k *ar = hw->priv;
Michal Kaziorddb6ad72013-10-02 11:03:39 +02001845 u8 tid, vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001846
1847 /* We should disable CCK RATE due to P2P */
1848 if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
1849 ath10k_dbg(ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
1850
1851 /* we must calculate tid before we apply qos workaround
1852 * as we'd lose the qos control field */
Michal Kazior42c3aa62013-10-02 11:03:38 +02001853 tid = ath10k_tx_h_get_tid(hdr);
Michal Kaziorddb6ad72013-10-02 11:03:39 +02001854 vdev_id = ath10k_tx_h_get_vdev_id(ar, info);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001855
Michal Kaziorcf84bd42013-07-16 11:04:54 +02001856 /* it makes no sense to process injected frames like that */
1857 if (info->control.vif &&
1858 info->control.vif->type != NL80211_IFTYPE_MONITOR) {
1859 ath10k_tx_h_qos_workaround(hw, control, skb);
1860 ath10k_tx_h_update_wep_key(skb);
1861 ath10k_tx_h_add_p2p_noa_ie(ar, skb);
1862 ath10k_tx_h_seq_no(skb);
1863 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001864
Bartosz Markowski5e00d312013-09-26 17:47:12 +02001865 ATH10K_SKB_CB(skb)->vdev_id = vdev_id;
Michal Kazior27bb1782013-09-18 14:43:19 +02001866 ATH10K_SKB_CB(skb)->htt.is_offchan = false;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001867 ATH10K_SKB_CB(skb)->htt.tid = tid;
1868
1869 if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
1870 spin_lock_bh(&ar->data_lock);
1871 ATH10K_SKB_CB(skb)->htt.is_offchan = true;
Bartosz Markowski5e00d312013-09-26 17:47:12 +02001872 ATH10K_SKB_CB(skb)->vdev_id = ar->scan.vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001873 spin_unlock_bh(&ar->data_lock);
1874
1875 ath10k_dbg(ATH10K_DBG_MAC, "queued offchannel skb %p\n", skb);
1876
1877 skb_queue_tail(&ar->offchan_tx_queue, skb);
1878 ieee80211_queue_work(hw, &ar->offchan_tx_work);
1879 return;
1880 }
1881
1882 ath10k_tx_htt(ar, skb);
1883}
1884
1885/*
1886 * Initialize various parameters with default vaules.
1887 */
Michal Kazioraffd3212013-07-16 09:54:35 +02001888void ath10k_halt(struct ath10k *ar)
Michal Kazior818bdd12013-07-16 09:38:57 +02001889{
1890 lockdep_assert_held(&ar->conf_mutex);
1891
1892 del_timer_sync(&ar->scan.timeout);
1893 ath10k_offchan_tx_purge(ar);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02001894 ath10k_mgmt_over_wmi_tx_purge(ar);
Michal Kazior818bdd12013-07-16 09:38:57 +02001895 ath10k_peer_cleanup_all(ar);
1896 ath10k_core_stop(ar);
1897 ath10k_hif_power_down(ar);
1898
1899 spin_lock_bh(&ar->data_lock);
1900 if (ar->scan.in_progress) {
1901 del_timer(&ar->scan.timeout);
1902 ar->scan.in_progress = false;
1903 ieee80211_scan_completed(ar->hw, true);
1904 }
1905 spin_unlock_bh(&ar->data_lock);
1906}
1907
Kalle Valo5e3dd152013-06-12 20:52:10 +03001908static int ath10k_start(struct ieee80211_hw *hw)
1909{
1910 struct ath10k *ar = hw->priv;
Michal Kazior818bdd12013-07-16 09:38:57 +02001911 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001912
Michal Kazior548db542013-07-05 16:15:15 +03001913 mutex_lock(&ar->conf_mutex);
1914
Michal Kazioraffd3212013-07-16 09:54:35 +02001915 if (ar->state != ATH10K_STATE_OFF &&
1916 ar->state != ATH10K_STATE_RESTARTING) {
Michal Kazior818bdd12013-07-16 09:38:57 +02001917 ret = -EINVAL;
1918 goto exit;
1919 }
1920
1921 ret = ath10k_hif_power_up(ar);
1922 if (ret) {
1923 ath10k_err("could not init hif (%d)\n", ret);
1924 ar->state = ATH10K_STATE_OFF;
1925 goto exit;
1926 }
1927
1928 ret = ath10k_core_start(ar);
1929 if (ret) {
1930 ath10k_err("could not init core (%d)\n", ret);
1931 ath10k_hif_power_down(ar);
1932 ar->state = ATH10K_STATE_OFF;
1933 goto exit;
1934 }
1935
Michal Kazioraffd3212013-07-16 09:54:35 +02001936 if (ar->state == ATH10K_STATE_OFF)
1937 ar->state = ATH10K_STATE_ON;
1938 else if (ar->state == ATH10K_STATE_RESTARTING)
1939 ar->state = ATH10K_STATE_RESTARTED;
1940
Bartosz Markowski226a3392013-09-26 17:47:16 +02001941 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->pmf_qos, 1);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001942 if (ret)
1943 ath10k_warn("could not enable WMI_PDEV_PARAM_PMF_QOS (%d)\n",
1944 ret);
1945
Bartosz Markowski226a3392013-09-26 17:47:16 +02001946 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->dynamic_bw, 0);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001947 if (ret)
1948 ath10k_warn("could not init WMI_PDEV_PARAM_DYNAMIC_BW (%d)\n",
1949 ret);
1950
Michal Kaziorf7843d72013-07-16 09:38:52 +02001951 ath10k_regd_update(ar);
1952
Michal Kazior818bdd12013-07-16 09:38:57 +02001953exit:
Michal Kazior548db542013-07-05 16:15:15 +03001954 mutex_unlock(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001955 return 0;
1956}
1957
1958static void ath10k_stop(struct ieee80211_hw *hw)
1959{
1960 struct ath10k *ar = hw->priv;
1961
Michal Kazior548db542013-07-05 16:15:15 +03001962 mutex_lock(&ar->conf_mutex);
Michal Kazioraffd3212013-07-16 09:54:35 +02001963 if (ar->state == ATH10K_STATE_ON ||
1964 ar->state == ATH10K_STATE_RESTARTED ||
1965 ar->state == ATH10K_STATE_WEDGED)
Michal Kazior818bdd12013-07-16 09:38:57 +02001966 ath10k_halt(ar);
Michal Kaziora96d7742013-07-16 09:38:56 +02001967
Michal Kaziorf7843d72013-07-16 09:38:52 +02001968 ar->state = ATH10K_STATE_OFF;
Michal Kazior548db542013-07-05 16:15:15 +03001969 mutex_unlock(&ar->conf_mutex);
1970
Bartosz Markowski5e00d312013-09-26 17:47:12 +02001971 ath10k_mgmt_over_wmi_tx_purge(ar);
1972
Michal Kazior548db542013-07-05 16:15:15 +03001973 cancel_work_sync(&ar->offchan_tx_work);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02001974 cancel_work_sync(&ar->wmi_mgmt_tx_work);
Michal Kazioraffd3212013-07-16 09:54:35 +02001975 cancel_work_sync(&ar->restart_work);
1976}
1977
Michal Kaziorad088bf2013-10-16 15:44:46 +03001978static int ath10k_config_ps(struct ath10k *ar)
Michal Kazioraffd3212013-07-16 09:54:35 +02001979{
Michal Kaziorad088bf2013-10-16 15:44:46 +03001980 struct ath10k_vif *arvif;
1981 int ret = 0;
Michal Kazioraffd3212013-07-16 09:54:35 +02001982
1983 lockdep_assert_held(&ar->conf_mutex);
1984
Michal Kaziorad088bf2013-10-16 15:44:46 +03001985 list_for_each_entry(arvif, &ar->arvifs, list) {
1986 ret = ath10k_mac_vif_setup_ps(arvif);
1987 if (ret) {
1988 ath10k_warn("could not setup powersave (%d)\n", ret);
1989 break;
1990 }
1991 }
Michal Kazioraffd3212013-07-16 09:54:35 +02001992
Michal Kaziorad088bf2013-10-16 15:44:46 +03001993 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001994}
1995
1996static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
1997{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001998 struct ath10k *ar = hw->priv;
1999 struct ieee80211_conf *conf = &hw->conf;
2000 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002001
2002 mutex_lock(&ar->conf_mutex);
2003
2004 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
Kalle Valo60c3daa2013-09-08 17:56:07 +03002005 ath10k_dbg(ATH10K_DBG_MAC, "mac config channel %d mhz\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002006 conf->chandef.chan->center_freq);
2007 spin_lock_bh(&ar->data_lock);
2008 ar->rx_channel = conf->chandef.chan;
2009 spin_unlock_bh(&ar->data_lock);
2010 }
2011
Michal Kazioraffd3212013-07-16 09:54:35 +02002012 if (changed & IEEE80211_CONF_CHANGE_PS)
2013 ath10k_config_ps(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002014
2015 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
2016 if (conf->flags & IEEE80211_CONF_MONITOR)
2017 ret = ath10k_monitor_create(ar);
2018 else
2019 ret = ath10k_monitor_destroy(ar);
2020 }
2021
2022 mutex_unlock(&ar->conf_mutex);
2023 return ret;
2024}
2025
2026/*
2027 * TODO:
2028 * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
2029 * because we will send mgmt frames without CCK. This requirement
2030 * for P2P_FIND/GO_NEG should be handled by checking CCK flag
2031 * in the TX packet.
2032 */
2033static int ath10k_add_interface(struct ieee80211_hw *hw,
2034 struct ieee80211_vif *vif)
2035{
2036 struct ath10k *ar = hw->priv;
2037 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2038 enum wmi_sta_powersave_param param;
2039 int ret = 0;
Michal Kazior424121c2013-07-22 14:13:31 +02002040 u32 value;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002041 int bit;
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02002042 u32 vdev_param;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002043
2044 mutex_lock(&ar->conf_mutex);
2045
Michal Kazior0dbd09e2013-07-31 10:55:14 +02002046 memset(arvif, 0, sizeof(*arvif));
2047
Kalle Valo5e3dd152013-06-12 20:52:10 +03002048 arvif->ar = ar;
2049 arvif->vif = vif;
2050
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002051 INIT_WORK(&arvif->wep_key_work, ath10k_tx_wep_key_work);
2052
Kalle Valo5e3dd152013-06-12 20:52:10 +03002053 if ((vif->type == NL80211_IFTYPE_MONITOR) && ar->monitor_present) {
2054 ath10k_warn("Only one monitor interface allowed\n");
2055 ret = -EBUSY;
Michal Kazior9dad14a2013-10-16 15:44:45 +03002056 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002057 }
2058
2059 bit = ffs(ar->free_vdev_map);
2060 if (bit == 0) {
2061 ret = -EBUSY;
Michal Kazior9dad14a2013-10-16 15:44:45 +03002062 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002063 }
2064
2065 arvif->vdev_id = bit - 1;
2066 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002067
2068 if (ar->p2p)
2069 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
2070
2071 switch (vif->type) {
2072 case NL80211_IFTYPE_UNSPECIFIED:
2073 case NL80211_IFTYPE_STATION:
2074 arvif->vdev_type = WMI_VDEV_TYPE_STA;
2075 if (vif->p2p)
2076 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
2077 break;
2078 case NL80211_IFTYPE_ADHOC:
2079 arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
2080 break;
2081 case NL80211_IFTYPE_AP:
2082 arvif->vdev_type = WMI_VDEV_TYPE_AP;
2083
2084 if (vif->p2p)
2085 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
2086 break;
2087 case NL80211_IFTYPE_MONITOR:
2088 arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
2089 break;
2090 default:
2091 WARN_ON(1);
2092 break;
2093 }
2094
Kalle Valo60c3daa2013-09-08 17:56:07 +03002095 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002096 arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype);
2097
2098 ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
2099 arvif->vdev_subtype, vif->addr);
2100 if (ret) {
2101 ath10k_warn("WMI vdev create failed: ret %d\n", ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002102 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002103 }
2104
Michal Kazior9dad14a2013-10-16 15:44:45 +03002105 ar->free_vdev_map &= ~BIT(arvif->vdev_id);
Michal Kazior05791192013-10-16 15:44:45 +03002106 list_add(&arvif->list, &ar->arvifs);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002107
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02002108 vdev_param = ar->wmi.vdev_param->def_keyid;
2109 ret = ath10k_wmi_vdev_set_param(ar, 0, vdev_param,
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002110 arvif->def_wep_key_idx);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002111 if (ret) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002112 ath10k_warn("Failed to set default keyid: %d\n", ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002113 goto err_vdev_delete;
2114 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002115
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02002116 vdev_param = ar->wmi.vdev_param->tx_encap_type;
2117 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002118 ATH10K_HW_TXRX_NATIVE_WIFI);
Bartosz Markowskiebc9abd2013-10-15 09:26:20 +02002119 /* 10.X firmware does not support this VDEV parameter. Do not warn */
Michal Kazior9dad14a2013-10-16 15:44:45 +03002120 if (ret && ret != -EOPNOTSUPP) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002121 ath10k_warn("Failed to set TX encap: %d\n", ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002122 goto err_vdev_delete;
2123 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002124
2125 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
2126 ret = ath10k_peer_create(ar, arvif->vdev_id, vif->addr);
2127 if (ret) {
2128 ath10k_warn("Failed to create peer for AP: %d\n", ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002129 goto err_vdev_delete;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002130 }
2131 }
2132
2133 if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
2134 param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
2135 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
2136 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
2137 param, value);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002138 if (ret) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002139 ath10k_warn("Failed to set RX wake policy: %d\n", ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002140 goto err_peer_delete;
2141 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002142
2143 param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
2144 value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
2145 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
2146 param, value);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002147 if (ret) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002148 ath10k_warn("Failed to set TX wake thresh: %d\n", ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002149 goto err_peer_delete;
2150 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002151
2152 param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
2153 value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
2154 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
2155 param, value);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002156 if (ret) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002157 ath10k_warn("Failed to set PSPOLL count: %d\n", ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002158 goto err_peer_delete;
2159 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002160 }
2161
Michal Kazior424121c2013-07-22 14:13:31 +02002162 ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002163 if (ret) {
Michal Kazior679c54a2013-07-05 16:15:04 +03002164 ath10k_warn("failed to set rts threshold for vdev %d (%d)\n",
2165 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002166 goto err_peer_delete;
2167 }
Michal Kazior679c54a2013-07-05 16:15:04 +03002168
Michal Kazior424121c2013-07-22 14:13:31 +02002169 ret = ath10k_mac_set_frag(arvif, ar->hw->wiphy->frag_threshold);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002170 if (ret) {
Michal Kazior679c54a2013-07-05 16:15:04 +03002171 ath10k_warn("failed to set frag threshold for vdev %d (%d)\n",
2172 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002173 goto err_peer_delete;
2174 }
Michal Kazior679c54a2013-07-05 16:15:04 +03002175
Kalle Valo5e3dd152013-06-12 20:52:10 +03002176 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
2177 ar->monitor_present = true;
2178
Kalle Valo5e3dd152013-06-12 20:52:10 +03002179 mutex_unlock(&ar->conf_mutex);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002180 return 0;
2181
2182err_peer_delete:
2183 if (arvif->vdev_type == WMI_VDEV_TYPE_AP)
2184 ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
2185
2186err_vdev_delete:
2187 ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
2188 ar->free_vdev_map &= ~BIT(arvif->vdev_id);
Michal Kazior05791192013-10-16 15:44:45 +03002189 list_del(&arvif->list);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002190
2191err:
2192 mutex_unlock(&ar->conf_mutex);
2193
Kalle Valo5e3dd152013-06-12 20:52:10 +03002194 return ret;
2195}
2196
2197static void ath10k_remove_interface(struct ieee80211_hw *hw,
2198 struct ieee80211_vif *vif)
2199{
2200 struct ath10k *ar = hw->priv;
2201 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2202 int ret;
2203
2204 mutex_lock(&ar->conf_mutex);
2205
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002206 cancel_work_sync(&arvif->wep_key_work);
2207
Michal Kaziored543882013-09-13 14:16:56 +02002208 spin_lock_bh(&ar->data_lock);
2209 if (arvif->beacon) {
2210 dev_kfree_skb_any(arvif->beacon);
2211 arvif->beacon = NULL;
2212 }
2213 spin_unlock_bh(&ar->data_lock);
2214
Kalle Valo5e3dd152013-06-12 20:52:10 +03002215 ar->free_vdev_map |= 1 << (arvif->vdev_id);
Michal Kazior05791192013-10-16 15:44:45 +03002216 list_del(&arvif->list);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002217
2218 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
2219 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, vif->addr);
2220 if (ret)
2221 ath10k_warn("Failed to remove peer for AP: %d\n", ret);
2222
2223 kfree(arvif->u.ap.noa_data);
2224 }
2225
Kalle Valo60c3daa2013-09-08 17:56:07 +03002226 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev delete %d (remove interface)\n",
2227 arvif->vdev_id);
2228
Kalle Valo5e3dd152013-06-12 20:52:10 +03002229 ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
2230 if (ret)
2231 ath10k_warn("WMI vdev delete failed: %d\n", ret);
2232
2233 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
2234 ar->monitor_present = false;
2235
2236 ath10k_peer_cleanup(ar, arvif->vdev_id);
2237
2238 mutex_unlock(&ar->conf_mutex);
2239}
2240
2241/*
2242 * FIXME: Has to be verified.
2243 */
2244#define SUPPORTED_FILTERS \
2245 (FIF_PROMISC_IN_BSS | \
2246 FIF_ALLMULTI | \
2247 FIF_CONTROL | \
2248 FIF_PSPOLL | \
2249 FIF_OTHER_BSS | \
2250 FIF_BCN_PRBRESP_PROMISC | \
2251 FIF_PROBE_REQ | \
2252 FIF_FCSFAIL)
2253
2254static void ath10k_configure_filter(struct ieee80211_hw *hw,
2255 unsigned int changed_flags,
2256 unsigned int *total_flags,
2257 u64 multicast)
2258{
2259 struct ath10k *ar = hw->priv;
2260 int ret;
2261
2262 mutex_lock(&ar->conf_mutex);
2263
2264 changed_flags &= SUPPORTED_FILTERS;
2265 *total_flags &= SUPPORTED_FILTERS;
2266 ar->filter_flags = *total_flags;
2267
2268 if ((ar->filter_flags & FIF_PROMISC_IN_BSS) &&
2269 !ar->monitor_enabled) {
Kalle Valo60c3daa2013-09-08 17:56:07 +03002270 ath10k_dbg(ATH10K_DBG_MAC, "mac monitor %d start\n",
2271 ar->monitor_vdev_id);
2272
Kalle Valo5e3dd152013-06-12 20:52:10 +03002273 ret = ath10k_monitor_start(ar, ar->monitor_vdev_id);
2274 if (ret)
2275 ath10k_warn("Unable to start monitor mode\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002276 } else if (!(ar->filter_flags & FIF_PROMISC_IN_BSS) &&
2277 ar->monitor_enabled) {
Kalle Valo60c3daa2013-09-08 17:56:07 +03002278 ath10k_dbg(ATH10K_DBG_MAC, "mac monitor %d stop\n",
2279 ar->monitor_vdev_id);
2280
Kalle Valo5e3dd152013-06-12 20:52:10 +03002281 ret = ath10k_monitor_stop(ar);
2282 if (ret)
2283 ath10k_warn("Unable to stop monitor mode\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002284 }
2285
2286 mutex_unlock(&ar->conf_mutex);
2287}
2288
2289static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
2290 struct ieee80211_vif *vif,
2291 struct ieee80211_bss_conf *info,
2292 u32 changed)
2293{
2294 struct ath10k *ar = hw->priv;
2295 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2296 int ret = 0;
Bartosz Markowski226a3392013-09-26 17:47:16 +02002297 u32 vdev_param, pdev_param;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002298
2299 mutex_lock(&ar->conf_mutex);
2300
2301 if (changed & BSS_CHANGED_IBSS)
2302 ath10k_control_ibss(arvif, info, vif->addr);
2303
2304 if (changed & BSS_CHANGED_BEACON_INT) {
2305 arvif->beacon_interval = info->beacon_int;
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02002306 vdev_param = ar->wmi.vdev_param->beacon_interval;
2307 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002308 arvif->beacon_interval);
Kalle Valo60c3daa2013-09-08 17:56:07 +03002309 ath10k_dbg(ATH10K_DBG_MAC,
2310 "mac vdev %d beacon_interval %d\n",
2311 arvif->vdev_id, arvif->beacon_interval);
2312
Kalle Valo5e3dd152013-06-12 20:52:10 +03002313 if (ret)
2314 ath10k_warn("Failed to set beacon interval for VDEV: %d\n",
2315 arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002316 }
2317
2318 if (changed & BSS_CHANGED_BEACON) {
Kalle Valo60c3daa2013-09-08 17:56:07 +03002319 ath10k_dbg(ATH10K_DBG_MAC,
2320 "vdev %d set beacon tx mode to staggered\n",
2321 arvif->vdev_id);
2322
Bartosz Markowski226a3392013-09-26 17:47:16 +02002323 pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
2324 ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002325 WMI_BEACON_STAGGERED_MODE);
2326 if (ret)
2327 ath10k_warn("Failed to set beacon mode for VDEV: %d\n",
2328 arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002329 }
2330
John W. Linvilleb70727e2013-06-13 13:34:29 -04002331 if (changed & BSS_CHANGED_BEACON_INFO) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002332 arvif->dtim_period = info->dtim_period;
2333
Kalle Valo60c3daa2013-09-08 17:56:07 +03002334 ath10k_dbg(ATH10K_DBG_MAC,
2335 "mac vdev %d dtim_period %d\n",
2336 arvif->vdev_id, arvif->dtim_period);
2337
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02002338 vdev_param = ar->wmi.vdev_param->dtim_period;
2339 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002340 arvif->dtim_period);
2341 if (ret)
2342 ath10k_warn("Failed to set dtim period for VDEV: %d\n",
2343 arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002344 }
2345
2346 if (changed & BSS_CHANGED_SSID &&
2347 vif->type == NL80211_IFTYPE_AP) {
2348 arvif->u.ap.ssid_len = info->ssid_len;
2349 if (info->ssid_len)
2350 memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
2351 arvif->u.ap.hidden_ssid = info->hidden_ssid;
2352 }
2353
2354 if (changed & BSS_CHANGED_BSSID) {
2355 if (!is_zero_ether_addr(info->bssid)) {
Kalle Valo60c3daa2013-09-08 17:56:07 +03002356 ath10k_dbg(ATH10K_DBG_MAC,
2357 "mac vdev %d create peer %pM\n",
2358 arvif->vdev_id, info->bssid);
2359
Kalle Valo5e3dd152013-06-12 20:52:10 +03002360 ret = ath10k_peer_create(ar, arvif->vdev_id,
2361 info->bssid);
2362 if (ret)
2363 ath10k_warn("Failed to add peer: %pM for VDEV: %d\n",
2364 info->bssid, arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002365
2366 if (vif->type == NL80211_IFTYPE_STATION) {
2367 /*
2368 * this is never erased as we it for crypto key
2369 * clearing; this is FW requirement
2370 */
2371 memcpy(arvif->u.sta.bssid, info->bssid,
2372 ETH_ALEN);
2373
Kalle Valo60c3daa2013-09-08 17:56:07 +03002374 ath10k_dbg(ATH10K_DBG_MAC,
2375 "mac vdev %d start %pM\n",
2376 arvif->vdev_id, info->bssid);
2377
2378 /* FIXME: check return value */
Kalle Valo5e3dd152013-06-12 20:52:10 +03002379 ret = ath10k_vdev_start(arvif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002380 }
2381
2382 /*
2383 * Mac80211 does not keep IBSS bssid when leaving IBSS,
2384 * so driver need to store it. It is needed when leaving
2385 * IBSS in order to remove BSSID peer.
2386 */
2387 if (vif->type == NL80211_IFTYPE_ADHOC)
2388 memcpy(arvif->u.ibss.bssid, info->bssid,
2389 ETH_ALEN);
2390 }
2391 }
2392
2393 if (changed & BSS_CHANGED_BEACON_ENABLED)
2394 ath10k_control_beaconing(arvif, info);
2395
2396 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
2397 u32 cts_prot;
2398 if (info->use_cts_prot)
2399 cts_prot = 1;
2400 else
2401 cts_prot = 0;
2402
Kalle Valo60c3daa2013-09-08 17:56:07 +03002403 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d cts_prot %d\n",
2404 arvif->vdev_id, cts_prot);
2405
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02002406 vdev_param = ar->wmi.vdev_param->enable_rtscts;
2407 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002408 cts_prot);
2409 if (ret)
2410 ath10k_warn("Failed to set CTS prot for VDEV: %d\n",
2411 arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002412 }
2413
2414 if (changed & BSS_CHANGED_ERP_SLOT) {
2415 u32 slottime;
2416 if (info->use_short_slot)
2417 slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
2418
2419 else
2420 slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
2421
Kalle Valo60c3daa2013-09-08 17:56:07 +03002422 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
2423 arvif->vdev_id, slottime);
2424
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02002425 vdev_param = ar->wmi.vdev_param->slot_time;
2426 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002427 slottime);
2428 if (ret)
2429 ath10k_warn("Failed to set erp slot for VDEV: %d\n",
2430 arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002431 }
2432
2433 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
2434 u32 preamble;
2435 if (info->use_short_preamble)
2436 preamble = WMI_VDEV_PREAMBLE_SHORT;
2437 else
2438 preamble = WMI_VDEV_PREAMBLE_LONG;
2439
Kalle Valo60c3daa2013-09-08 17:56:07 +03002440 ath10k_dbg(ATH10K_DBG_MAC,
2441 "mac vdev %d preamble %dn",
2442 arvif->vdev_id, preamble);
2443
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02002444 vdev_param = ar->wmi.vdev_param->preamble;
2445 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002446 preamble);
2447 if (ret)
2448 ath10k_warn("Failed to set preamble for VDEV: %d\n",
2449 arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002450 }
2451
2452 if (changed & BSS_CHANGED_ASSOC) {
2453 if (info->assoc)
2454 ath10k_bss_assoc(hw, vif, info);
2455 }
2456
2457 mutex_unlock(&ar->conf_mutex);
2458}
2459
2460static int ath10k_hw_scan(struct ieee80211_hw *hw,
2461 struct ieee80211_vif *vif,
2462 struct cfg80211_scan_request *req)
2463{
2464 struct ath10k *ar = hw->priv;
2465 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2466 struct wmi_start_scan_arg arg;
2467 int ret = 0;
2468 int i;
2469
2470 mutex_lock(&ar->conf_mutex);
2471
2472 spin_lock_bh(&ar->data_lock);
2473 if (ar->scan.in_progress) {
2474 spin_unlock_bh(&ar->data_lock);
2475 ret = -EBUSY;
2476 goto exit;
2477 }
2478
Wolfram Sang16735d02013-11-14 14:32:02 -08002479 reinit_completion(&ar->scan.started);
2480 reinit_completion(&ar->scan.completed);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002481 ar->scan.in_progress = true;
2482 ar->scan.aborting = false;
2483 ar->scan.is_roc = false;
2484 ar->scan.vdev_id = arvif->vdev_id;
2485 spin_unlock_bh(&ar->data_lock);
2486
2487 memset(&arg, 0, sizeof(arg));
2488 ath10k_wmi_start_scan_init(ar, &arg);
2489 arg.vdev_id = arvif->vdev_id;
2490 arg.scan_id = ATH10K_SCAN_ID;
2491
2492 if (!req->no_cck)
2493 arg.scan_ctrl_flags |= WMI_SCAN_ADD_CCK_RATES;
2494
2495 if (req->ie_len) {
2496 arg.ie_len = req->ie_len;
2497 memcpy(arg.ie, req->ie, arg.ie_len);
2498 }
2499
2500 if (req->n_ssids) {
2501 arg.n_ssids = req->n_ssids;
2502 for (i = 0; i < arg.n_ssids; i++) {
2503 arg.ssids[i].len = req->ssids[i].ssid_len;
2504 arg.ssids[i].ssid = req->ssids[i].ssid;
2505 }
Michal Kaziordcd4a562013-07-31 10:55:12 +02002506 } else {
2507 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002508 }
2509
2510 if (req->n_channels) {
2511 arg.n_channels = req->n_channels;
2512 for (i = 0; i < arg.n_channels; i++)
2513 arg.channels[i] = req->channels[i]->center_freq;
2514 }
2515
2516 ret = ath10k_start_scan(ar, &arg);
2517 if (ret) {
2518 ath10k_warn("could not start hw scan (%d)\n", ret);
2519 spin_lock_bh(&ar->data_lock);
2520 ar->scan.in_progress = false;
2521 spin_unlock_bh(&ar->data_lock);
2522 }
2523
2524exit:
2525 mutex_unlock(&ar->conf_mutex);
2526 return ret;
2527}
2528
2529static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
2530 struct ieee80211_vif *vif)
2531{
2532 struct ath10k *ar = hw->priv;
2533 int ret;
2534
2535 mutex_lock(&ar->conf_mutex);
2536 ret = ath10k_abort_scan(ar);
2537 if (ret) {
2538 ath10k_warn("couldn't abort scan (%d). forcefully sending scan completion to mac80211\n",
2539 ret);
2540 ieee80211_scan_completed(hw, 1 /* aborted */);
2541 }
2542 mutex_unlock(&ar->conf_mutex);
2543}
2544
2545static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
2546 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
2547 struct ieee80211_key_conf *key)
2548{
2549 struct ath10k *ar = hw->priv;
2550 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2551 struct ath10k_peer *peer;
2552 const u8 *peer_addr;
2553 bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
2554 key->cipher == WLAN_CIPHER_SUITE_WEP104;
2555 int ret = 0;
2556
2557 if (key->keyidx > WMI_MAX_KEY_INDEX)
2558 return -ENOSPC;
2559
2560 mutex_lock(&ar->conf_mutex);
2561
2562 if (sta)
2563 peer_addr = sta->addr;
2564 else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
2565 peer_addr = vif->bss_conf.bssid;
2566 else
2567 peer_addr = vif->addr;
2568
2569 key->hw_key_idx = key->keyidx;
2570
2571 /* the peer should not disappear in mid-way (unless FW goes awry) since
2572 * we already hold conf_mutex. we just make sure its there now. */
2573 spin_lock_bh(&ar->data_lock);
2574 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
2575 spin_unlock_bh(&ar->data_lock);
2576
2577 if (!peer) {
2578 if (cmd == SET_KEY) {
2579 ath10k_warn("cannot install key for non-existent peer %pM\n",
2580 peer_addr);
2581 ret = -EOPNOTSUPP;
2582 goto exit;
2583 } else {
2584 /* if the peer doesn't exist there is no key to disable
2585 * anymore */
2586 goto exit;
2587 }
2588 }
2589
2590 if (is_wep) {
2591 if (cmd == SET_KEY)
2592 arvif->wep_keys[key->keyidx] = key;
2593 else
2594 arvif->wep_keys[key->keyidx] = NULL;
2595
2596 if (cmd == DISABLE_KEY)
2597 ath10k_clear_vdev_key(arvif, key);
2598 }
2599
2600 ret = ath10k_install_key(arvif, key, cmd, peer_addr);
2601 if (ret) {
2602 ath10k_warn("ath10k_install_key failed (%d)\n", ret);
2603 goto exit;
2604 }
2605
2606 spin_lock_bh(&ar->data_lock);
2607 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
2608 if (peer && cmd == SET_KEY)
2609 peer->keys[key->keyidx] = key;
2610 else if (peer && cmd == DISABLE_KEY)
2611 peer->keys[key->keyidx] = NULL;
2612 else if (peer == NULL)
2613 /* impossible unless FW goes crazy */
2614 ath10k_warn("peer %pM disappeared!\n", peer_addr);
2615 spin_unlock_bh(&ar->data_lock);
2616
2617exit:
2618 mutex_unlock(&ar->conf_mutex);
2619 return ret;
2620}
2621
2622static int ath10k_sta_state(struct ieee80211_hw *hw,
2623 struct ieee80211_vif *vif,
2624 struct ieee80211_sta *sta,
2625 enum ieee80211_sta_state old_state,
2626 enum ieee80211_sta_state new_state)
2627{
2628 struct ath10k *ar = hw->priv;
2629 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2630 int ret = 0;
2631
2632 mutex_lock(&ar->conf_mutex);
2633
2634 if (old_state == IEEE80211_STA_NOTEXIST &&
2635 new_state == IEEE80211_STA_NONE &&
2636 vif->type != NL80211_IFTYPE_STATION) {
2637 /*
2638 * New station addition.
2639 */
Kalle Valo60c3daa2013-09-08 17:56:07 +03002640 ath10k_dbg(ATH10K_DBG_MAC,
2641 "mac vdev %d peer create %pM (new sta)\n",
2642 arvif->vdev_id, sta->addr);
2643
Kalle Valo5e3dd152013-06-12 20:52:10 +03002644 ret = ath10k_peer_create(ar, arvif->vdev_id, sta->addr);
2645 if (ret)
2646 ath10k_warn("Failed to add peer: %pM for VDEV: %d\n",
2647 sta->addr, arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002648 } else if ((old_state == IEEE80211_STA_NONE &&
2649 new_state == IEEE80211_STA_NOTEXIST)) {
2650 /*
2651 * Existing station deletion.
2652 */
Kalle Valo60c3daa2013-09-08 17:56:07 +03002653 ath10k_dbg(ATH10K_DBG_MAC,
2654 "mac vdev %d peer delete %pM (sta gone)\n",
2655 arvif->vdev_id, sta->addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002656 ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
2657 if (ret)
2658 ath10k_warn("Failed to delete peer: %pM for VDEV: %d\n",
2659 sta->addr, arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002660
2661 if (vif->type == NL80211_IFTYPE_STATION)
2662 ath10k_bss_disassoc(hw, vif);
2663 } else if (old_state == IEEE80211_STA_AUTH &&
2664 new_state == IEEE80211_STA_ASSOC &&
2665 (vif->type == NL80211_IFTYPE_AP ||
2666 vif->type == NL80211_IFTYPE_ADHOC)) {
2667 /*
2668 * New association.
2669 */
Kalle Valo60c3daa2013-09-08 17:56:07 +03002670 ath10k_dbg(ATH10K_DBG_MAC, "mac sta %pM associated\n",
2671 sta->addr);
2672
Kalle Valo5e3dd152013-06-12 20:52:10 +03002673 ret = ath10k_station_assoc(ar, arvif, sta);
2674 if (ret)
2675 ath10k_warn("Failed to associate station: %pM\n",
2676 sta->addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002677 } else if (old_state == IEEE80211_STA_ASSOC &&
2678 new_state == IEEE80211_STA_AUTH &&
2679 (vif->type == NL80211_IFTYPE_AP ||
2680 vif->type == NL80211_IFTYPE_ADHOC)) {
2681 /*
2682 * Disassociation.
2683 */
Kalle Valo60c3daa2013-09-08 17:56:07 +03002684 ath10k_dbg(ATH10K_DBG_MAC, "mac sta %pM disassociated\n",
2685 sta->addr);
2686
Kalle Valo5e3dd152013-06-12 20:52:10 +03002687 ret = ath10k_station_disassoc(ar, arvif, sta);
2688 if (ret)
2689 ath10k_warn("Failed to disassociate station: %pM\n",
2690 sta->addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002691 }
2692
2693 mutex_unlock(&ar->conf_mutex);
2694 return ret;
2695}
2696
2697static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
2698 u16 ac, bool enable)
2699{
2700 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2701 u32 value = 0;
2702 int ret = 0;
2703
Michal Kazior548db542013-07-05 16:15:15 +03002704 lockdep_assert_held(&ar->conf_mutex);
2705
Kalle Valo5e3dd152013-06-12 20:52:10 +03002706 if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
2707 return 0;
2708
2709 switch (ac) {
2710 case IEEE80211_AC_VO:
2711 value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
2712 WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
2713 break;
2714 case IEEE80211_AC_VI:
2715 value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
2716 WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
2717 break;
2718 case IEEE80211_AC_BE:
2719 value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
2720 WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
2721 break;
2722 case IEEE80211_AC_BK:
2723 value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
2724 WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
2725 break;
2726 }
2727
2728 if (enable)
2729 arvif->u.sta.uapsd |= value;
2730 else
2731 arvif->u.sta.uapsd &= ~value;
2732
2733 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
2734 WMI_STA_PS_PARAM_UAPSD,
2735 arvif->u.sta.uapsd);
2736 if (ret) {
2737 ath10k_warn("could not set uapsd params %d\n", ret);
2738 goto exit;
2739 }
2740
2741 if (arvif->u.sta.uapsd)
2742 value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
2743 else
2744 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
2745
2746 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
2747 WMI_STA_PS_PARAM_RX_WAKE_POLICY,
2748 value);
2749 if (ret)
2750 ath10k_warn("could not set rx wake param %d\n", ret);
2751
2752exit:
2753 return ret;
2754}
2755
2756static int ath10k_conf_tx(struct ieee80211_hw *hw,
2757 struct ieee80211_vif *vif, u16 ac,
2758 const struct ieee80211_tx_queue_params *params)
2759{
2760 struct ath10k *ar = hw->priv;
2761 struct wmi_wmm_params_arg *p = NULL;
2762 int ret;
2763
2764 mutex_lock(&ar->conf_mutex);
2765
2766 switch (ac) {
2767 case IEEE80211_AC_VO:
2768 p = &ar->wmm_params.ac_vo;
2769 break;
2770 case IEEE80211_AC_VI:
2771 p = &ar->wmm_params.ac_vi;
2772 break;
2773 case IEEE80211_AC_BE:
2774 p = &ar->wmm_params.ac_be;
2775 break;
2776 case IEEE80211_AC_BK:
2777 p = &ar->wmm_params.ac_bk;
2778 break;
2779 }
2780
2781 if (WARN_ON(!p)) {
2782 ret = -EINVAL;
2783 goto exit;
2784 }
2785
2786 p->cwmin = params->cw_min;
2787 p->cwmax = params->cw_max;
2788 p->aifs = params->aifs;
2789
2790 /*
2791 * The channel time duration programmed in the HW is in absolute
2792 * microseconds, while mac80211 gives the txop in units of
2793 * 32 microseconds.
2794 */
2795 p->txop = params->txop * 32;
2796
2797 /* FIXME: FW accepts wmm params per hw, not per vif */
2798 ret = ath10k_wmi_pdev_set_wmm_params(ar, &ar->wmm_params);
2799 if (ret) {
2800 ath10k_warn("could not set wmm params %d\n", ret);
2801 goto exit;
2802 }
2803
2804 ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
2805 if (ret)
2806 ath10k_warn("could not set sta uapsd %d\n", ret);
2807
2808exit:
2809 mutex_unlock(&ar->conf_mutex);
2810 return ret;
2811}
2812
2813#define ATH10K_ROC_TIMEOUT_HZ (2*HZ)
2814
2815static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
2816 struct ieee80211_vif *vif,
2817 struct ieee80211_channel *chan,
2818 int duration,
2819 enum ieee80211_roc_type type)
2820{
2821 struct ath10k *ar = hw->priv;
2822 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2823 struct wmi_start_scan_arg arg;
2824 int ret;
2825
2826 mutex_lock(&ar->conf_mutex);
2827
2828 spin_lock_bh(&ar->data_lock);
2829 if (ar->scan.in_progress) {
2830 spin_unlock_bh(&ar->data_lock);
2831 ret = -EBUSY;
2832 goto exit;
2833 }
2834
Wolfram Sang16735d02013-11-14 14:32:02 -08002835 reinit_completion(&ar->scan.started);
2836 reinit_completion(&ar->scan.completed);
2837 reinit_completion(&ar->scan.on_channel);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002838 ar->scan.in_progress = true;
2839 ar->scan.aborting = false;
2840 ar->scan.is_roc = true;
2841 ar->scan.vdev_id = arvif->vdev_id;
2842 ar->scan.roc_freq = chan->center_freq;
2843 spin_unlock_bh(&ar->data_lock);
2844
2845 memset(&arg, 0, sizeof(arg));
2846 ath10k_wmi_start_scan_init(ar, &arg);
2847 arg.vdev_id = arvif->vdev_id;
2848 arg.scan_id = ATH10K_SCAN_ID;
2849 arg.n_channels = 1;
2850 arg.channels[0] = chan->center_freq;
2851 arg.dwell_time_active = duration;
2852 arg.dwell_time_passive = duration;
2853 arg.max_scan_time = 2 * duration;
2854 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
2855 arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
2856
2857 ret = ath10k_start_scan(ar, &arg);
2858 if (ret) {
2859 ath10k_warn("could not start roc scan (%d)\n", ret);
2860 spin_lock_bh(&ar->data_lock);
2861 ar->scan.in_progress = false;
2862 spin_unlock_bh(&ar->data_lock);
2863 goto exit;
2864 }
2865
2866 ret = wait_for_completion_timeout(&ar->scan.on_channel, 3*HZ);
2867 if (ret == 0) {
2868 ath10k_warn("could not switch to channel for roc scan\n");
2869 ath10k_abort_scan(ar);
2870 ret = -ETIMEDOUT;
2871 goto exit;
2872 }
2873
2874 ret = 0;
2875exit:
2876 mutex_unlock(&ar->conf_mutex);
2877 return ret;
2878}
2879
2880static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw)
2881{
2882 struct ath10k *ar = hw->priv;
2883
2884 mutex_lock(&ar->conf_mutex);
2885 ath10k_abort_scan(ar);
2886 mutex_unlock(&ar->conf_mutex);
2887
2888 return 0;
2889}
2890
2891/*
2892 * Both RTS and Fragmentation threshold are interface-specific
2893 * in ath10k, but device-specific in mac80211.
2894 */
Kalle Valo5e3dd152013-06-12 20:52:10 +03002895
2896static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
2897{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002898 struct ath10k *ar = hw->priv;
Michal Kaziorad088bf2013-10-16 15:44:46 +03002899 struct ath10k_vif *arvif;
2900 int ret = 0;
Michal Kazior548db542013-07-05 16:15:15 +03002901
Michal Kaziorad088bf2013-10-16 15:44:46 +03002902 mutex_lock(&ar->conf_mutex);
2903 list_for_each_entry(arvif, &ar->arvifs, list) {
2904 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
2905 arvif->vdev_id, value);
Kalle Valo60c3daa2013-09-08 17:56:07 +03002906
Michal Kaziorad088bf2013-10-16 15:44:46 +03002907 ret = ath10k_mac_set_rts(arvif, value);
2908 if (ret) {
2909 ath10k_warn("could not set rts threshold for vdev %d (%d)\n",
2910 arvif->vdev_id, ret);
2911 break;
2912 }
2913 }
2914 mutex_unlock(&ar->conf_mutex);
2915
2916 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002917}
2918
2919static int ath10k_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
2920{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002921 struct ath10k *ar = hw->priv;
Michal Kaziorad088bf2013-10-16 15:44:46 +03002922 struct ath10k_vif *arvif;
2923 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002924
Kalle Valo5e3dd152013-06-12 20:52:10 +03002925 mutex_lock(&ar->conf_mutex);
Michal Kaziorad088bf2013-10-16 15:44:46 +03002926 list_for_each_entry(arvif, &ar->arvifs, list) {
2927 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d fragmentation threshold %d\n",
2928 arvif->vdev_id, value);
2929
2930 ret = ath10k_mac_set_rts(arvif, value);
2931 if (ret) {
2932 ath10k_warn("could not set fragmentation threshold for vdev %d (%d)\n",
2933 arvif->vdev_id, ret);
2934 break;
2935 }
2936 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002937 mutex_unlock(&ar->conf_mutex);
2938
Michal Kaziorad088bf2013-10-16 15:44:46 +03002939 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002940}
2941
2942static void ath10k_flush(struct ieee80211_hw *hw, u32 queues, bool drop)
2943{
2944 struct ath10k *ar = hw->priv;
Michal Kazioraffd3212013-07-16 09:54:35 +02002945 bool skip;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002946 int ret;
2947
2948 /* mac80211 doesn't care if we really xmit queued frames or not
2949 * we'll collect those frames either way if we stop/delete vdevs */
2950 if (drop)
2951 return;
2952
Michal Kazior548db542013-07-05 16:15:15 +03002953 mutex_lock(&ar->conf_mutex);
2954
Michal Kazioraffd3212013-07-16 09:54:35 +02002955 if (ar->state == ATH10K_STATE_WEDGED)
2956 goto skip;
2957
Michal Kazioredb82362013-07-05 16:15:14 +03002958 ret = wait_event_timeout(ar->htt.empty_tx_wq, ({
Kalle Valo5e3dd152013-06-12 20:52:10 +03002959 bool empty;
Michal Kazioraffd3212013-07-16 09:54:35 +02002960
Michal Kazioredb82362013-07-05 16:15:14 +03002961 spin_lock_bh(&ar->htt.tx_lock);
Michal Kazior0945baf2013-09-18 14:43:18 +02002962 empty = (ar->htt.num_pending_tx == 0);
Michal Kazioredb82362013-07-05 16:15:14 +03002963 spin_unlock_bh(&ar->htt.tx_lock);
Michal Kazioraffd3212013-07-16 09:54:35 +02002964
2965 skip = (ar->state == ATH10K_STATE_WEDGED);
2966
2967 (empty || skip);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002968 }), ATH10K_FLUSH_TIMEOUT_HZ);
Michal Kazioraffd3212013-07-16 09:54:35 +02002969
2970 if (ret <= 0 || skip)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002971 ath10k_warn("tx not flushed\n");
Michal Kazior548db542013-07-05 16:15:15 +03002972
Michal Kazioraffd3212013-07-16 09:54:35 +02002973skip:
Michal Kazior548db542013-07-05 16:15:15 +03002974 mutex_unlock(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002975}
2976
2977/* TODO: Implement this function properly
2978 * For now it is needed to reply to Probe Requests in IBSS mode.
2979 * Propably we need this information from FW.
2980 */
2981static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
2982{
2983 return 1;
2984}
2985
Michal Kazior8cd13ca2013-07-16 09:38:54 +02002986#ifdef CONFIG_PM
2987static int ath10k_suspend(struct ieee80211_hw *hw,
2988 struct cfg80211_wowlan *wowlan)
2989{
2990 struct ath10k *ar = hw->priv;
2991 int ret;
2992
2993 ar->is_target_paused = false;
2994
2995 ret = ath10k_wmi_pdev_suspend_target(ar);
2996 if (ret) {
2997 ath10k_warn("could not suspend target (%d)\n", ret);
2998 return 1;
2999 }
3000
3001 ret = wait_event_interruptible_timeout(ar->event_queue,
3002 ar->is_target_paused == true,
3003 1 * HZ);
3004 if (ret < 0) {
3005 ath10k_warn("suspend interrupted (%d)\n", ret);
3006 goto resume;
3007 } else if (ret == 0) {
3008 ath10k_warn("suspend timed out - target pause event never came\n");
3009 goto resume;
3010 }
3011
3012 ret = ath10k_hif_suspend(ar);
3013 if (ret) {
3014 ath10k_warn("could not suspend hif (%d)\n", ret);
3015 goto resume;
3016 }
3017
3018 return 0;
3019resume:
3020 ret = ath10k_wmi_pdev_resume_target(ar);
3021 if (ret)
3022 ath10k_warn("could not resume target (%d)\n", ret);
3023 return 1;
3024}
3025
3026static int ath10k_resume(struct ieee80211_hw *hw)
3027{
3028 struct ath10k *ar = hw->priv;
3029 int ret;
3030
3031 ret = ath10k_hif_resume(ar);
3032 if (ret) {
3033 ath10k_warn("could not resume hif (%d)\n", ret);
3034 return 1;
3035 }
3036
3037 ret = ath10k_wmi_pdev_resume_target(ar);
3038 if (ret) {
3039 ath10k_warn("could not resume target (%d)\n", ret);
3040 return 1;
3041 }
3042
3043 return 0;
3044}
3045#endif
3046
Michal Kazioraffd3212013-07-16 09:54:35 +02003047static void ath10k_restart_complete(struct ieee80211_hw *hw)
3048{
3049 struct ath10k *ar = hw->priv;
3050
3051 mutex_lock(&ar->conf_mutex);
3052
3053 /* If device failed to restart it will be in a different state, e.g.
3054 * ATH10K_STATE_WEDGED */
3055 if (ar->state == ATH10K_STATE_RESTARTED) {
3056 ath10k_info("device successfully recovered\n");
3057 ar->state = ATH10K_STATE_ON;
3058 }
3059
3060 mutex_unlock(&ar->conf_mutex);
3061}
3062
Michal Kazior2e1dea42013-07-31 10:32:40 +02003063static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
3064 struct survey_info *survey)
3065{
3066 struct ath10k *ar = hw->priv;
3067 struct ieee80211_supported_band *sband;
3068 struct survey_info *ar_survey = &ar->survey[idx];
3069 int ret = 0;
3070
3071 mutex_lock(&ar->conf_mutex);
3072
3073 sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
3074 if (sband && idx >= sband->n_channels) {
3075 idx -= sband->n_channels;
3076 sband = NULL;
3077 }
3078
3079 if (!sband)
3080 sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
3081
3082 if (!sband || idx >= sband->n_channels) {
3083 ret = -ENOENT;
3084 goto exit;
3085 }
3086
3087 spin_lock_bh(&ar->data_lock);
3088 memcpy(survey, ar_survey, sizeof(*survey));
3089 spin_unlock_bh(&ar->data_lock);
3090
3091 survey->channel = &sband->channels[idx];
3092
3093exit:
3094 mutex_unlock(&ar->conf_mutex);
3095 return ret;
3096}
3097
Kalle Valo5e3dd152013-06-12 20:52:10 +03003098static const struct ieee80211_ops ath10k_ops = {
3099 .tx = ath10k_tx,
3100 .start = ath10k_start,
3101 .stop = ath10k_stop,
3102 .config = ath10k_config,
3103 .add_interface = ath10k_add_interface,
3104 .remove_interface = ath10k_remove_interface,
3105 .configure_filter = ath10k_configure_filter,
3106 .bss_info_changed = ath10k_bss_info_changed,
3107 .hw_scan = ath10k_hw_scan,
3108 .cancel_hw_scan = ath10k_cancel_hw_scan,
3109 .set_key = ath10k_set_key,
3110 .sta_state = ath10k_sta_state,
3111 .conf_tx = ath10k_conf_tx,
3112 .remain_on_channel = ath10k_remain_on_channel,
3113 .cancel_remain_on_channel = ath10k_cancel_remain_on_channel,
3114 .set_rts_threshold = ath10k_set_rts_threshold,
3115 .set_frag_threshold = ath10k_set_frag_threshold,
3116 .flush = ath10k_flush,
3117 .tx_last_beacon = ath10k_tx_last_beacon,
Michal Kazioraffd3212013-07-16 09:54:35 +02003118 .restart_complete = ath10k_restart_complete,
Michal Kazior2e1dea42013-07-31 10:32:40 +02003119 .get_survey = ath10k_get_survey,
Michal Kazior8cd13ca2013-07-16 09:38:54 +02003120#ifdef CONFIG_PM
3121 .suspend = ath10k_suspend,
3122 .resume = ath10k_resume,
3123#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03003124};
3125
3126#define RATETAB_ENT(_rate, _rateid, _flags) { \
3127 .bitrate = (_rate), \
3128 .flags = (_flags), \
3129 .hw_value = (_rateid), \
3130}
3131
3132#define CHAN2G(_channel, _freq, _flags) { \
3133 .band = IEEE80211_BAND_2GHZ, \
3134 .hw_value = (_channel), \
3135 .center_freq = (_freq), \
3136 .flags = (_flags), \
3137 .max_antenna_gain = 0, \
3138 .max_power = 30, \
3139}
3140
3141#define CHAN5G(_channel, _freq, _flags) { \
3142 .band = IEEE80211_BAND_5GHZ, \
3143 .hw_value = (_channel), \
3144 .center_freq = (_freq), \
3145 .flags = (_flags), \
3146 .max_antenna_gain = 0, \
3147 .max_power = 30, \
3148}
3149
3150static const struct ieee80211_channel ath10k_2ghz_channels[] = {
3151 CHAN2G(1, 2412, 0),
3152 CHAN2G(2, 2417, 0),
3153 CHAN2G(3, 2422, 0),
3154 CHAN2G(4, 2427, 0),
3155 CHAN2G(5, 2432, 0),
3156 CHAN2G(6, 2437, 0),
3157 CHAN2G(7, 2442, 0),
3158 CHAN2G(8, 2447, 0),
3159 CHAN2G(9, 2452, 0),
3160 CHAN2G(10, 2457, 0),
3161 CHAN2G(11, 2462, 0),
3162 CHAN2G(12, 2467, 0),
3163 CHAN2G(13, 2472, 0),
3164 CHAN2G(14, 2484, 0),
3165};
3166
3167static const struct ieee80211_channel ath10k_5ghz_channels[] = {
Michal Kazior429ff562013-06-26 08:54:54 +02003168 CHAN5G(36, 5180, 0),
3169 CHAN5G(40, 5200, 0),
3170 CHAN5G(44, 5220, 0),
3171 CHAN5G(48, 5240, 0),
3172 CHAN5G(52, 5260, 0),
3173 CHAN5G(56, 5280, 0),
3174 CHAN5G(60, 5300, 0),
3175 CHAN5G(64, 5320, 0),
3176 CHAN5G(100, 5500, 0),
3177 CHAN5G(104, 5520, 0),
3178 CHAN5G(108, 5540, 0),
3179 CHAN5G(112, 5560, 0),
3180 CHAN5G(116, 5580, 0),
3181 CHAN5G(120, 5600, 0),
3182 CHAN5G(124, 5620, 0),
3183 CHAN5G(128, 5640, 0),
3184 CHAN5G(132, 5660, 0),
3185 CHAN5G(136, 5680, 0),
3186 CHAN5G(140, 5700, 0),
3187 CHAN5G(149, 5745, 0),
3188 CHAN5G(153, 5765, 0),
3189 CHAN5G(157, 5785, 0),
3190 CHAN5G(161, 5805, 0),
3191 CHAN5G(165, 5825, 0),
Kalle Valo5e3dd152013-06-12 20:52:10 +03003192};
3193
3194static struct ieee80211_rate ath10k_rates[] = {
3195 /* CCK */
3196 RATETAB_ENT(10, 0x82, 0),
3197 RATETAB_ENT(20, 0x84, 0),
3198 RATETAB_ENT(55, 0x8b, 0),
3199 RATETAB_ENT(110, 0x96, 0),
3200 /* OFDM */
3201 RATETAB_ENT(60, 0x0c, 0),
3202 RATETAB_ENT(90, 0x12, 0),
3203 RATETAB_ENT(120, 0x18, 0),
3204 RATETAB_ENT(180, 0x24, 0),
3205 RATETAB_ENT(240, 0x30, 0),
3206 RATETAB_ENT(360, 0x48, 0),
3207 RATETAB_ENT(480, 0x60, 0),
3208 RATETAB_ENT(540, 0x6c, 0),
3209};
3210
3211#define ath10k_a_rates (ath10k_rates + 4)
3212#define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - 4)
3213#define ath10k_g_rates (ath10k_rates + 0)
3214#define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
3215
3216struct ath10k *ath10k_mac_create(void)
3217{
3218 struct ieee80211_hw *hw;
3219 struct ath10k *ar;
3220
3221 hw = ieee80211_alloc_hw(sizeof(struct ath10k), &ath10k_ops);
3222 if (!hw)
3223 return NULL;
3224
3225 ar = hw->priv;
3226 ar->hw = hw;
3227
3228 return ar;
3229}
3230
3231void ath10k_mac_destroy(struct ath10k *ar)
3232{
3233 ieee80211_free_hw(ar->hw);
3234}
3235
3236static const struct ieee80211_iface_limit ath10k_if_limits[] = {
3237 {
3238 .max = 8,
3239 .types = BIT(NL80211_IFTYPE_STATION)
3240 | BIT(NL80211_IFTYPE_P2P_CLIENT)
Michal Kaziord531cb82013-07-31 10:55:13 +02003241 },
3242 {
3243 .max = 3,
3244 .types = BIT(NL80211_IFTYPE_P2P_GO)
3245 },
3246 {
3247 .max = 7,
3248 .types = BIT(NL80211_IFTYPE_AP)
3249 },
Kalle Valo5e3dd152013-06-12 20:52:10 +03003250};
3251
3252static const struct ieee80211_iface_combination ath10k_if_comb = {
3253 .limits = ath10k_if_limits,
3254 .n_limits = ARRAY_SIZE(ath10k_if_limits),
3255 .max_interfaces = 8,
3256 .num_different_channels = 1,
3257 .beacon_int_infra_match = true,
3258};
3259
3260static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
3261{
3262 struct ieee80211_sta_vht_cap vht_cap = {0};
3263 u16 mcs_map;
Michal Kazior8865bee42013-07-24 12:36:46 +02003264 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003265
3266 vht_cap.vht_supported = 1;
3267 vht_cap.cap = ar->vht_cap_info;
3268
Michal Kazior8865bee42013-07-24 12:36:46 +02003269 mcs_map = 0;
3270 for (i = 0; i < 8; i++) {
3271 if (i < ar->num_rf_chains)
3272 mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i*2);
3273 else
3274 mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i*2);
3275 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003276
3277 vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
3278 vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
3279
3280 return vht_cap;
3281}
3282
3283static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
3284{
3285 int i;
3286 struct ieee80211_sta_ht_cap ht_cap = {0};
3287
3288 if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
3289 return ht_cap;
3290
3291 ht_cap.ht_supported = 1;
3292 ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
3293 ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
3294 ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
3295 ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
3296 ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
3297
3298 if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
3299 ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
3300
3301 if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
3302 ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
3303
3304 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
3305 u32 smps;
3306
3307 smps = WLAN_HT_CAP_SM_PS_DYNAMIC;
3308 smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
3309
3310 ht_cap.cap |= smps;
3311 }
3312
3313 if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC)
3314 ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
3315
3316 if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
3317 u32 stbc;
3318
3319 stbc = ar->ht_cap_info;
3320 stbc &= WMI_HT_CAP_RX_STBC;
3321 stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
3322 stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
3323 stbc &= IEEE80211_HT_CAP_RX_STBC;
3324
3325 ht_cap.cap |= stbc;
3326 }
3327
3328 if (ar->ht_cap_info & WMI_HT_CAP_LDPC)
3329 ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
3330
3331 if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
3332 ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
3333
3334 /* max AMSDU is implicitly taken from vht_cap_info */
3335 if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
3336 ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
3337
Michal Kazior8865bee42013-07-24 12:36:46 +02003338 for (i = 0; i < ar->num_rf_chains; i++)
Kalle Valo5e3dd152013-06-12 20:52:10 +03003339 ht_cap.mcs.rx_mask[i] = 0xFF;
3340
3341 ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
3342
3343 return ht_cap;
3344}
3345
3346
3347static void ath10k_get_arvif_iter(void *data, u8 *mac,
3348 struct ieee80211_vif *vif)
3349{
3350 struct ath10k_vif_iter *arvif_iter = data;
3351 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3352
3353 if (arvif->vdev_id == arvif_iter->vdev_id)
3354 arvif_iter->arvif = arvif;
3355}
3356
3357struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
3358{
3359 struct ath10k_vif_iter arvif_iter;
3360 u32 flags;
3361
3362 memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
3363 arvif_iter.vdev_id = vdev_id;
3364
3365 flags = IEEE80211_IFACE_ITER_RESUME_ALL;
3366 ieee80211_iterate_active_interfaces_atomic(ar->hw,
3367 flags,
3368 ath10k_get_arvif_iter,
3369 &arvif_iter);
3370 if (!arvif_iter.arvif) {
3371 ath10k_warn("No VIF found for VDEV: %d\n", vdev_id);
3372 return NULL;
3373 }
3374
3375 return arvif_iter.arvif;
3376}
3377
3378int ath10k_mac_register(struct ath10k *ar)
3379{
3380 struct ieee80211_supported_band *band;
3381 struct ieee80211_sta_vht_cap vht_cap;
3382 struct ieee80211_sta_ht_cap ht_cap;
3383 void *channels;
3384 int ret;
3385
3386 SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
3387
3388 SET_IEEE80211_DEV(ar->hw, ar->dev);
3389
3390 ht_cap = ath10k_get_ht_cap(ar);
3391 vht_cap = ath10k_create_vht_cap(ar);
3392
3393 if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
3394 channels = kmemdup(ath10k_2ghz_channels,
3395 sizeof(ath10k_2ghz_channels),
3396 GFP_KERNEL);
Michal Kaziord6015b22013-07-22 14:13:30 +02003397 if (!channels) {
3398 ret = -ENOMEM;
3399 goto err_free;
3400 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003401
3402 band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
3403 band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
3404 band->channels = channels;
3405 band->n_bitrates = ath10k_g_rates_size;
3406 band->bitrates = ath10k_g_rates;
3407 band->ht_cap = ht_cap;
3408
3409 /* vht is not supported in 2.4 GHz */
3410
3411 ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
3412 }
3413
3414 if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
3415 channels = kmemdup(ath10k_5ghz_channels,
3416 sizeof(ath10k_5ghz_channels),
3417 GFP_KERNEL);
3418 if (!channels) {
Michal Kaziord6015b22013-07-22 14:13:30 +02003419 ret = -ENOMEM;
3420 goto err_free;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003421 }
3422
3423 band = &ar->mac.sbands[IEEE80211_BAND_5GHZ];
3424 band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
3425 band->channels = channels;
3426 band->n_bitrates = ath10k_a_rates_size;
3427 band->bitrates = ath10k_a_rates;
3428 band->ht_cap = ht_cap;
3429 band->vht_cap = vht_cap;
3430 ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
3431 }
3432
3433 ar->hw->wiphy->interface_modes =
3434 BIT(NL80211_IFTYPE_STATION) |
3435 BIT(NL80211_IFTYPE_ADHOC) |
3436 BIT(NL80211_IFTYPE_AP) |
3437 BIT(NL80211_IFTYPE_P2P_CLIENT) |
3438 BIT(NL80211_IFTYPE_P2P_GO);
3439
3440 ar->hw->flags = IEEE80211_HW_SIGNAL_DBM |
3441 IEEE80211_HW_SUPPORTS_PS |
3442 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
3443 IEEE80211_HW_SUPPORTS_UAPSD |
3444 IEEE80211_HW_MFP_CAPABLE |
3445 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
3446 IEEE80211_HW_HAS_RATE_CONTROL |
3447 IEEE80211_HW_SUPPORTS_STATIC_SMPS |
3448 IEEE80211_HW_WANT_MONITOR_VIF |
3449 IEEE80211_HW_AP_LINK_PS;
3450
Michal Kazior1f8bb152013-09-18 14:43:22 +02003451 /* MSDU can have HTT TX fragment pushed in front. The additional 4
3452 * bytes is used for padding/alignment if necessary. */
3453 ar->hw->extra_tx_headroom += sizeof(struct htt_data_tx_desc_frag)*2 + 4;
3454
Kalle Valo5e3dd152013-06-12 20:52:10 +03003455 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
3456 ar->hw->flags |= IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS;
3457
3458 if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
3459 ar->hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
3460 ar->hw->flags |= IEEE80211_HW_TX_AMPDU_SETUP_IN_HW;
3461 }
3462
3463 ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
3464 ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
3465
3466 ar->hw->vif_data_size = sizeof(struct ath10k_vif);
3467
3468 ar->hw->channel_change_time = 5000;
3469 ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
3470
3471 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
3472 ar->hw->wiphy->max_remain_on_channel_duration = 5000;
3473
3474 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
3475 /*
3476 * on LL hardware queues are managed entirely by the FW
3477 * so we only advertise to mac we can do the queues thing
3478 */
3479 ar->hw->queues = 4;
3480
3481 ar->hw->wiphy->iface_combinations = &ath10k_if_comb;
3482 ar->hw->wiphy->n_iface_combinations = 1;
3483
Michal Kazior7c199992013-07-31 10:47:57 +02003484 ar->hw->netdev_features = NETIF_F_HW_CSUM;
3485
Kalle Valo5e3dd152013-06-12 20:52:10 +03003486 ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
3487 ath10k_reg_notifier);
3488 if (ret) {
3489 ath10k_err("Regulatory initialization failed\n");
Michal Kaziord6015b22013-07-22 14:13:30 +02003490 goto err_free;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003491 }
3492
3493 ret = ieee80211_register_hw(ar->hw);
3494 if (ret) {
3495 ath10k_err("ieee80211 registration failed: %d\n", ret);
Michal Kaziord6015b22013-07-22 14:13:30 +02003496 goto err_free;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003497 }
3498
3499 if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
3500 ret = regulatory_hint(ar->hw->wiphy,
3501 ar->ath_common.regulatory.alpha2);
3502 if (ret)
Michal Kaziord6015b22013-07-22 14:13:30 +02003503 goto err_unregister;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003504 }
3505
3506 return 0;
Michal Kaziord6015b22013-07-22 14:13:30 +02003507
3508err_unregister:
Kalle Valo5e3dd152013-06-12 20:52:10 +03003509 ieee80211_unregister_hw(ar->hw);
Michal Kaziord6015b22013-07-22 14:13:30 +02003510err_free:
3511 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
3512 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
3513
Kalle Valo5e3dd152013-06-12 20:52:10 +03003514 return ret;
3515}
3516
3517void ath10k_mac_unregister(struct ath10k *ar)
3518{
3519 ieee80211_unregister_hw(ar->hw);
3520
3521 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
3522 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
3523
3524 SET_IEEE80211_DEV(ar->hw, NULL);
3525}