blob: 56f030ed357a2cad57c7651e0280eb31e5f88e66 [file] [log] [blame]
Kalle Valo5e3dd152013-06-12 20:52:10 +03001/*
2 * Copyright (c) 2005-2011 Atheros Communications Inc.
3 * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include "mac.h"
19
20#include <net/mac80211.h>
21#include <linux/etherdevice.h>
22
Michal Kazior8cd13ca2013-07-16 09:38:54 +020023#include "hif.h"
Kalle Valo5e3dd152013-06-12 20:52:10 +030024#include "core.h"
25#include "debug.h"
26#include "wmi.h"
27#include "htt.h"
28#include "txrx.h"
29
30/**********/
31/* Crypto */
32/**********/
33
34static int ath10k_send_key(struct ath10k_vif *arvif,
35 struct ieee80211_key_conf *key,
36 enum set_key_cmd cmd,
37 const u8 *macaddr)
38{
39 struct wmi_vdev_install_key_arg arg = {
40 .vdev_id = arvif->vdev_id,
41 .key_idx = key->keyidx,
42 .key_len = key->keylen,
43 .key_data = key->key,
44 .macaddr = macaddr,
45 };
46
Michal Kazior548db542013-07-05 16:15:15 +030047 lockdep_assert_held(&arvif->ar->conf_mutex);
48
Kalle Valo5e3dd152013-06-12 20:52:10 +030049 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
50 arg.key_flags = WMI_KEY_PAIRWISE;
51 else
52 arg.key_flags = WMI_KEY_GROUP;
53
54 switch (key->cipher) {
55 case WLAN_CIPHER_SUITE_CCMP:
56 arg.key_cipher = WMI_CIPHER_AES_CCM;
57 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
58 break;
59 case WLAN_CIPHER_SUITE_TKIP:
Kalle Valo5e3dd152013-06-12 20:52:10 +030060 arg.key_cipher = WMI_CIPHER_TKIP;
61 arg.key_txmic_len = 8;
62 arg.key_rxmic_len = 8;
63 break;
64 case WLAN_CIPHER_SUITE_WEP40:
65 case WLAN_CIPHER_SUITE_WEP104:
66 arg.key_cipher = WMI_CIPHER_WEP;
67 /* AP/IBSS mode requires self-key to be groupwise
68 * Otherwise pairwise key must be set */
69 if (memcmp(macaddr, arvif->vif->addr, ETH_ALEN))
70 arg.key_flags = WMI_KEY_PAIRWISE;
71 break;
72 default:
73 ath10k_warn("cipher %d is not supported\n", key->cipher);
74 return -EOPNOTSUPP;
75 }
76
77 if (cmd == DISABLE_KEY) {
78 arg.key_cipher = WMI_CIPHER_NONE;
79 arg.key_data = NULL;
80 }
81
82 return ath10k_wmi_vdev_install_key(arvif->ar, &arg);
83}
84
85static int ath10k_install_key(struct ath10k_vif *arvif,
86 struct ieee80211_key_conf *key,
87 enum set_key_cmd cmd,
88 const u8 *macaddr)
89{
90 struct ath10k *ar = arvif->ar;
91 int ret;
92
Michal Kazior548db542013-07-05 16:15:15 +030093 lockdep_assert_held(&ar->conf_mutex);
94
Kalle Valo5e3dd152013-06-12 20:52:10 +030095 INIT_COMPLETION(ar->install_key_done);
96
97 ret = ath10k_send_key(arvif, key, cmd, macaddr);
98 if (ret)
99 return ret;
100
101 ret = wait_for_completion_timeout(&ar->install_key_done, 3*HZ);
102 if (ret == 0)
103 return -ETIMEDOUT;
104
105 return 0;
106}
107
108static int ath10k_install_peer_wep_keys(struct ath10k_vif *arvif,
109 const u8 *addr)
110{
111 struct ath10k *ar = arvif->ar;
112 struct ath10k_peer *peer;
113 int ret;
114 int i;
115
116 lockdep_assert_held(&ar->conf_mutex);
117
118 spin_lock_bh(&ar->data_lock);
119 peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
120 spin_unlock_bh(&ar->data_lock);
121
122 if (!peer)
123 return -ENOENT;
124
125 for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
126 if (arvif->wep_keys[i] == NULL)
127 continue;
128
129 ret = ath10k_install_key(arvif, arvif->wep_keys[i], SET_KEY,
130 addr);
131 if (ret)
132 return ret;
133
134 peer->keys[i] = arvif->wep_keys[i];
135 }
136
137 return 0;
138}
139
140static int ath10k_clear_peer_keys(struct ath10k_vif *arvif,
141 const u8 *addr)
142{
143 struct ath10k *ar = arvif->ar;
144 struct ath10k_peer *peer;
145 int first_errno = 0;
146 int ret;
147 int i;
148
149 lockdep_assert_held(&ar->conf_mutex);
150
151 spin_lock_bh(&ar->data_lock);
152 peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
153 spin_unlock_bh(&ar->data_lock);
154
155 if (!peer)
156 return -ENOENT;
157
158 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
159 if (peer->keys[i] == NULL)
160 continue;
161
162 ret = ath10k_install_key(arvif, peer->keys[i],
163 DISABLE_KEY, addr);
164 if (ret && first_errno == 0)
165 first_errno = ret;
166
167 if (ret)
168 ath10k_warn("could not remove peer wep key %d (%d)\n",
169 i, ret);
170
171 peer->keys[i] = NULL;
172 }
173
174 return first_errno;
175}
176
177static int ath10k_clear_vdev_key(struct ath10k_vif *arvif,
178 struct ieee80211_key_conf *key)
179{
180 struct ath10k *ar = arvif->ar;
181 struct ath10k_peer *peer;
182 u8 addr[ETH_ALEN];
183 int first_errno = 0;
184 int ret;
185 int i;
186
187 lockdep_assert_held(&ar->conf_mutex);
188
189 for (;;) {
190 /* since ath10k_install_key we can't hold data_lock all the
191 * time, so we try to remove the keys incrementally */
192 spin_lock_bh(&ar->data_lock);
193 i = 0;
194 list_for_each_entry(peer, &ar->peers, list) {
195 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
196 if (peer->keys[i] == key) {
197 memcpy(addr, peer->addr, ETH_ALEN);
198 peer->keys[i] = NULL;
199 break;
200 }
201 }
202
203 if (i < ARRAY_SIZE(peer->keys))
204 break;
205 }
206 spin_unlock_bh(&ar->data_lock);
207
208 if (i == ARRAY_SIZE(peer->keys))
209 break;
210
211 ret = ath10k_install_key(arvif, key, DISABLE_KEY, addr);
212 if (ret && first_errno == 0)
213 first_errno = ret;
214
215 if (ret)
216 ath10k_warn("could not remove key for %pM\n", addr);
217 }
218
219 return first_errno;
220}
221
222
223/*********************/
224/* General utilities */
225/*********************/
226
227static inline enum wmi_phy_mode
228chan_to_phymode(const struct cfg80211_chan_def *chandef)
229{
230 enum wmi_phy_mode phymode = MODE_UNKNOWN;
231
232 switch (chandef->chan->band) {
233 case IEEE80211_BAND_2GHZ:
234 switch (chandef->width) {
235 case NL80211_CHAN_WIDTH_20_NOHT:
236 phymode = MODE_11G;
237 break;
238 case NL80211_CHAN_WIDTH_20:
239 phymode = MODE_11NG_HT20;
240 break;
241 case NL80211_CHAN_WIDTH_40:
242 phymode = MODE_11NG_HT40;
243 break;
John W. Linville0f817ed2013-06-27 13:50:09 -0400244 case NL80211_CHAN_WIDTH_5:
245 case NL80211_CHAN_WIDTH_10:
Kalle Valo5e3dd152013-06-12 20:52:10 +0300246 case NL80211_CHAN_WIDTH_80:
247 case NL80211_CHAN_WIDTH_80P80:
248 case NL80211_CHAN_WIDTH_160:
249 phymode = MODE_UNKNOWN;
250 break;
251 }
252 break;
253 case IEEE80211_BAND_5GHZ:
254 switch (chandef->width) {
255 case NL80211_CHAN_WIDTH_20_NOHT:
256 phymode = MODE_11A;
257 break;
258 case NL80211_CHAN_WIDTH_20:
259 phymode = MODE_11NA_HT20;
260 break;
261 case NL80211_CHAN_WIDTH_40:
262 phymode = MODE_11NA_HT40;
263 break;
264 case NL80211_CHAN_WIDTH_80:
265 phymode = MODE_11AC_VHT80;
266 break;
John W. Linville0f817ed2013-06-27 13:50:09 -0400267 case NL80211_CHAN_WIDTH_5:
268 case NL80211_CHAN_WIDTH_10:
Kalle Valo5e3dd152013-06-12 20:52:10 +0300269 case NL80211_CHAN_WIDTH_80P80:
270 case NL80211_CHAN_WIDTH_160:
271 phymode = MODE_UNKNOWN;
272 break;
273 }
274 break;
275 default:
276 break;
277 }
278
279 WARN_ON(phymode == MODE_UNKNOWN);
280 return phymode;
281}
282
283static u8 ath10k_parse_mpdudensity(u8 mpdudensity)
284{
285/*
286 * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
287 * 0 for no restriction
288 * 1 for 1/4 us
289 * 2 for 1/2 us
290 * 3 for 1 us
291 * 4 for 2 us
292 * 5 for 4 us
293 * 6 for 8 us
294 * 7 for 16 us
295 */
296 switch (mpdudensity) {
297 case 0:
298 return 0;
299 case 1:
300 case 2:
301 case 3:
302 /* Our lower layer calculations limit our precision to
303 1 microsecond */
304 return 1;
305 case 4:
306 return 2;
307 case 5:
308 return 4;
309 case 6:
310 return 8;
311 case 7:
312 return 16;
313 default:
314 return 0;
315 }
316}
317
318static int ath10k_peer_create(struct ath10k *ar, u32 vdev_id, const u8 *addr)
319{
320 int ret;
321
322 lockdep_assert_held(&ar->conf_mutex);
323
324 ret = ath10k_wmi_peer_create(ar, vdev_id, addr);
Ben Greear479398b2013-11-04 09:19:34 -0800325 if (ret) {
326 ath10k_warn("Failed to create wmi peer: %i\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300327 return ret;
Ben Greear479398b2013-11-04 09:19:34 -0800328 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300329
330 ret = ath10k_wait_for_peer_created(ar, vdev_id, addr);
Ben Greear479398b2013-11-04 09:19:34 -0800331 if (ret) {
332 ath10k_warn("Failed to wait for created wmi peer: %i\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300333 return ret;
Ben Greear479398b2013-11-04 09:19:34 -0800334 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300335
336 return 0;
337}
338
Michal Kazior424121c2013-07-22 14:13:31 +0200339static int ath10k_mac_set_rts(struct ath10k_vif *arvif, u32 value)
340{
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200341 struct ath10k *ar = arvif->ar;
342 u32 vdev_param;
343
Michal Kazior424121c2013-07-22 14:13:31 +0200344 if (value != 0xFFFFFFFF)
345 value = min_t(u32, arvif->ar->hw->wiphy->rts_threshold,
346 ATH10K_RTS_MAX);
347
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200348 vdev_param = ar->wmi.vdev_param->rts_threshold;
349 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
Michal Kazior424121c2013-07-22 14:13:31 +0200350}
351
352static int ath10k_mac_set_frag(struct ath10k_vif *arvif, u32 value)
353{
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200354 struct ath10k *ar = arvif->ar;
355 u32 vdev_param;
356
Michal Kazior424121c2013-07-22 14:13:31 +0200357 if (value != 0xFFFFFFFF)
358 value = clamp_t(u32, arvif->ar->hw->wiphy->frag_threshold,
359 ATH10K_FRAGMT_THRESHOLD_MIN,
360 ATH10K_FRAGMT_THRESHOLD_MAX);
361
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200362 vdev_param = ar->wmi.vdev_param->fragmentation_threshold;
363 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
Michal Kazior424121c2013-07-22 14:13:31 +0200364}
365
Kalle Valo5e3dd152013-06-12 20:52:10 +0300366static int ath10k_peer_delete(struct ath10k *ar, u32 vdev_id, const u8 *addr)
367{
368 int ret;
369
370 lockdep_assert_held(&ar->conf_mutex);
371
372 ret = ath10k_wmi_peer_delete(ar, vdev_id, addr);
373 if (ret)
374 return ret;
375
376 ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
377 if (ret)
378 return ret;
379
380 return 0;
381}
382
383static void ath10k_peer_cleanup(struct ath10k *ar, u32 vdev_id)
384{
385 struct ath10k_peer *peer, *tmp;
386
387 lockdep_assert_held(&ar->conf_mutex);
388
389 spin_lock_bh(&ar->data_lock);
390 list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
391 if (peer->vdev_id != vdev_id)
392 continue;
393
394 ath10k_warn("removing stale peer %pM from vdev_id %d\n",
395 peer->addr, vdev_id);
396
397 list_del(&peer->list);
398 kfree(peer);
399 }
400 spin_unlock_bh(&ar->data_lock);
401}
402
Michal Kaziora96d7742013-07-16 09:38:56 +0200403static void ath10k_peer_cleanup_all(struct ath10k *ar)
404{
405 struct ath10k_peer *peer, *tmp;
406
407 lockdep_assert_held(&ar->conf_mutex);
408
409 spin_lock_bh(&ar->data_lock);
410 list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
411 list_del(&peer->list);
412 kfree(peer);
413 }
414 spin_unlock_bh(&ar->data_lock);
415}
416
Kalle Valo5e3dd152013-06-12 20:52:10 +0300417/************************/
418/* Interface management */
419/************************/
420
421static inline int ath10k_vdev_setup_sync(struct ath10k *ar)
422{
423 int ret;
424
Michal Kazior548db542013-07-05 16:15:15 +0300425 lockdep_assert_held(&ar->conf_mutex);
426
Kalle Valo5e3dd152013-06-12 20:52:10 +0300427 ret = wait_for_completion_timeout(&ar->vdev_setup_done,
428 ATH10K_VDEV_SETUP_TIMEOUT_HZ);
429 if (ret == 0)
430 return -ETIMEDOUT;
431
432 return 0;
433}
434
435static int ath10k_vdev_start(struct ath10k_vif *arvif)
436{
437 struct ath10k *ar = arvif->ar;
438 struct ieee80211_conf *conf = &ar->hw->conf;
439 struct ieee80211_channel *channel = conf->chandef.chan;
440 struct wmi_vdev_start_request_arg arg = {};
441 int ret = 0;
442
443 lockdep_assert_held(&ar->conf_mutex);
444
445 INIT_COMPLETION(ar->vdev_setup_done);
446
447 arg.vdev_id = arvif->vdev_id;
448 arg.dtim_period = arvif->dtim_period;
449 arg.bcn_intval = arvif->beacon_interval;
450
451 arg.channel.freq = channel->center_freq;
452
453 arg.channel.band_center_freq1 = conf->chandef.center_freq1;
454
455 arg.channel.mode = chan_to_phymode(&conf->chandef);
456
Michal Kazior89c5c842013-10-23 04:02:13 -0700457 arg.channel.min_power = 0;
Michal Kazior02256932013-10-23 04:02:14 -0700458 arg.channel.max_power = channel->max_power * 2;
459 arg.channel.max_reg_power = channel->max_reg_power * 2;
460 arg.channel.max_antenna_gain = channel->max_antenna_gain * 2;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300461
462 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
463 arg.ssid = arvif->u.ap.ssid;
464 arg.ssid_len = arvif->u.ap.ssid_len;
465 arg.hidden_ssid = arvif->u.ap.hidden_ssid;
466 } else if (arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
467 arg.ssid = arvif->vif->bss_conf.ssid;
468 arg.ssid_len = arvif->vif->bss_conf.ssid_len;
469 }
470
Kalle Valo38a1d472013-09-08 17:56:14 +0300471 ath10k_dbg(ATH10K_DBG_MAC,
472 "mac vdev %d start center_freq %d phymode %s\n",
473 arg.vdev_id, arg.channel.freq,
474 ath10k_wmi_phymode_str(arg.channel.mode));
475
Kalle Valo5e3dd152013-06-12 20:52:10 +0300476 ret = ath10k_wmi_vdev_start(ar, &arg);
477 if (ret) {
478 ath10k_warn("WMI vdev start failed: ret %d\n", ret);
479 return ret;
480 }
481
482 ret = ath10k_vdev_setup_sync(ar);
483 if (ret) {
484 ath10k_warn("vdev setup failed %d\n", ret);
485 return ret;
486 }
487
488 return ret;
489}
490
491static int ath10k_vdev_stop(struct ath10k_vif *arvif)
492{
493 struct ath10k *ar = arvif->ar;
494 int ret;
495
496 lockdep_assert_held(&ar->conf_mutex);
497
498 INIT_COMPLETION(ar->vdev_setup_done);
499
500 ret = ath10k_wmi_vdev_stop(ar, arvif->vdev_id);
501 if (ret) {
502 ath10k_warn("WMI vdev stop failed: ret %d\n", ret);
503 return ret;
504 }
505
506 ret = ath10k_vdev_setup_sync(ar);
507 if (ret) {
508 ath10k_warn("vdev setup failed %d\n", ret);
509 return ret;
510 }
511
512 return ret;
513}
514
515static int ath10k_monitor_start(struct ath10k *ar, int vdev_id)
516{
517 struct ieee80211_channel *channel = ar->hw->conf.chandef.chan;
518 struct wmi_vdev_start_request_arg arg = {};
Kalle Valo5e3dd152013-06-12 20:52:10 +0300519 int ret = 0;
520
521 lockdep_assert_held(&ar->conf_mutex);
522
Michal Kazior0ed00ee2013-10-16 16:45:48 +0300523 if (!ar->monitor_present) {
524 ath10k_warn("mac montor stop -- monitor is not present\n");
525 return -EINVAL;
526 }
527
Kalle Valo5e3dd152013-06-12 20:52:10 +0300528 arg.vdev_id = vdev_id;
529 arg.channel.freq = channel->center_freq;
530 arg.channel.band_center_freq1 = ar->hw->conf.chandef.center_freq1;
531
532 /* TODO setup this dynamically, what in case we
533 don't have any vifs? */
534 arg.channel.mode = chan_to_phymode(&ar->hw->conf.chandef);
535
Michal Kazior89c5c842013-10-23 04:02:13 -0700536 arg.channel.min_power = 0;
Michal Kazior02256932013-10-23 04:02:14 -0700537 arg.channel.max_power = channel->max_power * 2;
538 arg.channel.max_reg_power = channel->max_reg_power * 2;
539 arg.channel.max_antenna_gain = channel->max_antenna_gain * 2;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300540
541 ret = ath10k_wmi_vdev_start(ar, &arg);
542 if (ret) {
543 ath10k_warn("Monitor vdev start failed: ret %d\n", ret);
544 return ret;
545 }
546
547 ret = ath10k_vdev_setup_sync(ar);
548 if (ret) {
549 ath10k_warn("Monitor vdev setup failed %d\n", ret);
550 return ret;
551 }
552
553 ret = ath10k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
554 if (ret) {
555 ath10k_warn("Monitor vdev up failed: %d\n", ret);
556 goto vdev_stop;
557 }
558
559 ar->monitor_vdev_id = vdev_id;
560 ar->monitor_enabled = true;
561
562 return 0;
563
564vdev_stop:
565 ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
566 if (ret)
567 ath10k_warn("Monitor vdev stop failed: %d\n", ret);
568
569 return ret;
570}
571
572static int ath10k_monitor_stop(struct ath10k *ar)
573{
574 int ret = 0;
575
576 lockdep_assert_held(&ar->conf_mutex);
577
Michal Kazior0ed00ee2013-10-16 16:45:48 +0300578 if (!ar->monitor_present) {
579 ath10k_warn("mac montor stop -- monitor is not present\n");
580 return -EINVAL;
581 }
582
583 if (!ar->monitor_enabled) {
584 ath10k_warn("mac montor stop -- monitor is not enabled\n");
585 return -EINVAL;
586 }
587
Marek Puzyniak52fa0192013-09-24 14:06:24 +0200588 ret = ath10k_wmi_vdev_down(ar, ar->monitor_vdev_id);
589 if (ret)
590 ath10k_warn("Monitor vdev down failed: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300591
592 ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
593 if (ret)
594 ath10k_warn("Monitor vdev stop failed: %d\n", ret);
595
596 ret = ath10k_vdev_setup_sync(ar);
597 if (ret)
598 ath10k_warn("Monitor_down sync failed: %d\n", ret);
599
600 ar->monitor_enabled = false;
601 return ret;
602}
603
604static int ath10k_monitor_create(struct ath10k *ar)
605{
606 int bit, ret = 0;
607
608 lockdep_assert_held(&ar->conf_mutex);
609
610 if (ar->monitor_present) {
611 ath10k_warn("Monitor mode already enabled\n");
612 return 0;
613 }
614
615 bit = ffs(ar->free_vdev_map);
616 if (bit == 0) {
617 ath10k_warn("No free VDEV slots\n");
618 return -ENOMEM;
619 }
620
621 ar->monitor_vdev_id = bit - 1;
622 ar->free_vdev_map &= ~(1 << ar->monitor_vdev_id);
623
624 ret = ath10k_wmi_vdev_create(ar, ar->monitor_vdev_id,
625 WMI_VDEV_TYPE_MONITOR,
626 0, ar->mac_addr);
627 if (ret) {
628 ath10k_warn("WMI vdev monitor create failed: ret %d\n", ret);
629 goto vdev_fail;
630 }
631
Kalle Valo60c3daa2013-09-08 17:56:07 +0300632 ath10k_dbg(ATH10K_DBG_MAC, "mac monitor vdev %d created\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300633 ar->monitor_vdev_id);
634
635 ar->monitor_present = true;
636 return 0;
637
638vdev_fail:
639 /*
640 * Restore the ID to the global map.
641 */
642 ar->free_vdev_map |= 1 << (ar->monitor_vdev_id);
643 return ret;
644}
645
646static int ath10k_monitor_destroy(struct ath10k *ar)
647{
648 int ret = 0;
649
650 lockdep_assert_held(&ar->conf_mutex);
651
652 if (!ar->monitor_present)
653 return 0;
654
655 ret = ath10k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
656 if (ret) {
657 ath10k_warn("WMI vdev monitor delete failed: %d\n", ret);
658 return ret;
659 }
660
661 ar->free_vdev_map |= 1 << (ar->monitor_vdev_id);
662 ar->monitor_present = false;
663
Kalle Valo60c3daa2013-09-08 17:56:07 +0300664 ath10k_dbg(ATH10K_DBG_MAC, "mac monitor vdev %d deleted\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300665 ar->monitor_vdev_id);
666 return ret;
667}
668
669static void ath10k_control_beaconing(struct ath10k_vif *arvif,
670 struct ieee80211_bss_conf *info)
671{
672 int ret = 0;
673
Michal Kazior548db542013-07-05 16:15:15 +0300674 lockdep_assert_held(&arvif->ar->conf_mutex);
675
Kalle Valo5e3dd152013-06-12 20:52:10 +0300676 if (!info->enable_beacon) {
677 ath10k_vdev_stop(arvif);
678 return;
679 }
680
681 arvif->tx_seq_no = 0x1000;
682
683 ret = ath10k_vdev_start(arvif);
684 if (ret)
685 return;
686
687 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, 0, info->bssid);
688 if (ret) {
689 ath10k_warn("Failed to bring up VDEV: %d\n",
690 arvif->vdev_id);
691 return;
692 }
Kalle Valo60c3daa2013-09-08 17:56:07 +0300693 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300694}
695
696static void ath10k_control_ibss(struct ath10k_vif *arvif,
697 struct ieee80211_bss_conf *info,
698 const u8 self_peer[ETH_ALEN])
699{
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200700 u32 vdev_param;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300701 int ret = 0;
702
Michal Kazior548db542013-07-05 16:15:15 +0300703 lockdep_assert_held(&arvif->ar->conf_mutex);
704
Kalle Valo5e3dd152013-06-12 20:52:10 +0300705 if (!info->ibss_joined) {
706 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, self_peer);
707 if (ret)
708 ath10k_warn("Failed to delete IBSS self peer:%pM for VDEV:%d ret:%d\n",
709 self_peer, arvif->vdev_id, ret);
710
711 if (is_zero_ether_addr(arvif->u.ibss.bssid))
712 return;
713
714 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id,
715 arvif->u.ibss.bssid);
716 if (ret) {
717 ath10k_warn("Failed to delete IBSS BSSID peer:%pM for VDEV:%d ret:%d\n",
718 arvif->u.ibss.bssid, arvif->vdev_id, ret);
719 return;
720 }
721
722 memset(arvif->u.ibss.bssid, 0, ETH_ALEN);
723
724 return;
725 }
726
727 ret = ath10k_peer_create(arvif->ar, arvif->vdev_id, self_peer);
728 if (ret) {
729 ath10k_warn("Failed to create IBSS self peer:%pM for VDEV:%d ret:%d\n",
730 self_peer, arvif->vdev_id, ret);
731 return;
732 }
733
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200734 vdev_param = arvif->ar->wmi.vdev_param->atim_window;
735 ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +0300736 ATH10K_DEFAULT_ATIM);
737 if (ret)
738 ath10k_warn("Failed to set IBSS ATIM for VDEV:%d ret:%d\n",
739 arvif->vdev_id, ret);
740}
741
742/*
743 * Review this when mac80211 gains per-interface powersave support.
744 */
Michal Kaziorad088bf2013-10-16 15:44:46 +0300745static int ath10k_mac_vif_setup_ps(struct ath10k_vif *arvif)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300746{
Michal Kaziorad088bf2013-10-16 15:44:46 +0300747 struct ath10k *ar = arvif->ar;
748 struct ieee80211_conf *conf = &ar->hw->conf;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300749 enum wmi_sta_powersave_param param;
750 enum wmi_sta_ps_mode psmode;
751 int ret;
752
Michal Kazior548db542013-07-05 16:15:15 +0300753 lockdep_assert_held(&arvif->ar->conf_mutex);
754
Michal Kaziorad088bf2013-10-16 15:44:46 +0300755 if (arvif->vif->type != NL80211_IFTYPE_STATION)
756 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300757
758 if (conf->flags & IEEE80211_CONF_PS) {
759 psmode = WMI_STA_PS_MODE_ENABLED;
760 param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
761
Michal Kaziorad088bf2013-10-16 15:44:46 +0300762 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
Kalle Valo5e3dd152013-06-12 20:52:10 +0300763 conf->dynamic_ps_timeout);
764 if (ret) {
765 ath10k_warn("Failed to set inactivity time for VDEV: %d\n",
766 arvif->vdev_id);
Michal Kaziorad088bf2013-10-16 15:44:46 +0300767 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300768 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300769 } else {
770 psmode = WMI_STA_PS_MODE_DISABLED;
771 }
772
Kalle Valo60c3daa2013-09-08 17:56:07 +0300773 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d psmode %s\n",
774 arvif->vdev_id, psmode ? "enable" : "disable");
775
Michal Kaziorad088bf2013-10-16 15:44:46 +0300776 ret = ath10k_wmi_set_psmode(ar, arvif->vdev_id, psmode);
777 if (ret) {
Kalle Valo5e3dd152013-06-12 20:52:10 +0300778 ath10k_warn("Failed to set PS Mode: %d for VDEV: %d\n",
779 psmode, arvif->vdev_id);
Michal Kaziorad088bf2013-10-16 15:44:46 +0300780 return ret;
781 }
782
783 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300784}
785
786/**********************/
787/* Station management */
788/**********************/
789
790static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
791 struct ath10k_vif *arvif,
792 struct ieee80211_sta *sta,
793 struct ieee80211_bss_conf *bss_conf,
794 struct wmi_peer_assoc_complete_arg *arg)
795{
Michal Kazior548db542013-07-05 16:15:15 +0300796 lockdep_assert_held(&ar->conf_mutex);
797
Kalle Valo5e3dd152013-06-12 20:52:10 +0300798 memcpy(arg->addr, sta->addr, ETH_ALEN);
799 arg->vdev_id = arvif->vdev_id;
800 arg->peer_aid = sta->aid;
801 arg->peer_flags |= WMI_PEER_AUTH;
802
803 if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
804 /*
805 * Seems FW have problems with Power Save in STA
806 * mode when we setup this parameter to high (eg. 5).
807 * Often we see that FW don't send NULL (with clean P flags)
808 * frame even there is info about buffered frames in beacons.
809 * Sometimes we have to wait more than 10 seconds before FW
810 * will wakeup. Often sending one ping from AP to our device
811 * just fail (more than 50%).
812 *
813 * Seems setting this FW parameter to 1 couse FW
814 * will check every beacon and will wakup immediately
815 * after detection buffered data.
816 */
817 arg->peer_listen_intval = 1;
818 else
819 arg->peer_listen_intval = ar->hw->conf.listen_interval;
820
821 arg->peer_num_spatial_streams = 1;
822
823 /*
824 * The assoc capabilities are available only in managed mode.
825 */
826 if (arvif->vdev_type == WMI_VDEV_TYPE_STA && bss_conf)
827 arg->peer_caps = bss_conf->assoc_capability;
828}
829
830static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
831 struct ath10k_vif *arvif,
832 struct wmi_peer_assoc_complete_arg *arg)
833{
834 struct ieee80211_vif *vif = arvif->vif;
835 struct ieee80211_bss_conf *info = &vif->bss_conf;
836 struct cfg80211_bss *bss;
837 const u8 *rsnie = NULL;
838 const u8 *wpaie = NULL;
839
Michal Kazior548db542013-07-05 16:15:15 +0300840 lockdep_assert_held(&ar->conf_mutex);
841
Kalle Valo5e3dd152013-06-12 20:52:10 +0300842 bss = cfg80211_get_bss(ar->hw->wiphy, ar->hw->conf.chandef.chan,
843 info->bssid, NULL, 0, 0, 0);
844 if (bss) {
845 const struct cfg80211_bss_ies *ies;
846
847 rcu_read_lock();
848 rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
849
850 ies = rcu_dereference(bss->ies);
851
852 wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
853 WLAN_OUI_TYPE_MICROSOFT_WPA,
854 ies->data,
855 ies->len);
856 rcu_read_unlock();
857 cfg80211_put_bss(ar->hw->wiphy, bss);
858 }
859
860 /* FIXME: base on RSN IE/WPA IE is a correct idea? */
861 if (rsnie || wpaie) {
862 ath10k_dbg(ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
863 arg->peer_flags |= WMI_PEER_NEED_PTK_4_WAY;
864 }
865
866 if (wpaie) {
867 ath10k_dbg(ATH10K_DBG_WMI, "%s: wpa ie found\n", __func__);
868 arg->peer_flags |= WMI_PEER_NEED_GTK_2_WAY;
869 }
870}
871
872static void ath10k_peer_assoc_h_rates(struct ath10k *ar,
873 struct ieee80211_sta *sta,
874 struct wmi_peer_assoc_complete_arg *arg)
875{
876 struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
877 const struct ieee80211_supported_band *sband;
878 const struct ieee80211_rate *rates;
879 u32 ratemask;
880 int i;
881
Michal Kazior548db542013-07-05 16:15:15 +0300882 lockdep_assert_held(&ar->conf_mutex);
883
Kalle Valo5e3dd152013-06-12 20:52:10 +0300884 sband = ar->hw->wiphy->bands[ar->hw->conf.chandef.chan->band];
885 ratemask = sta->supp_rates[ar->hw->conf.chandef.chan->band];
886 rates = sband->bitrates;
887
888 rateset->num_rates = 0;
889
890 for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
891 if (!(ratemask & 1))
892 continue;
893
894 rateset->rates[rateset->num_rates] = rates->hw_value;
895 rateset->num_rates++;
896 }
897}
898
899static void ath10k_peer_assoc_h_ht(struct ath10k *ar,
900 struct ieee80211_sta *sta,
901 struct wmi_peer_assoc_complete_arg *arg)
902{
903 const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
904 int smps;
905 int i, n;
906
Michal Kazior548db542013-07-05 16:15:15 +0300907 lockdep_assert_held(&ar->conf_mutex);
908
Kalle Valo5e3dd152013-06-12 20:52:10 +0300909 if (!ht_cap->ht_supported)
910 return;
911
912 arg->peer_flags |= WMI_PEER_HT;
913 arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
914 ht_cap->ampdu_factor)) - 1;
915
916 arg->peer_mpdu_density =
917 ath10k_parse_mpdudensity(ht_cap->ampdu_density);
918
919 arg->peer_ht_caps = ht_cap->cap;
920 arg->peer_rate_caps |= WMI_RC_HT_FLAG;
921
922 if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
923 arg->peer_flags |= WMI_PEER_LDPC;
924
925 if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
926 arg->peer_flags |= WMI_PEER_40MHZ;
927 arg->peer_rate_caps |= WMI_RC_CW40_FLAG;
928 }
929
930 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
931 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
932
933 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
934 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
935
936 if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
937 arg->peer_rate_caps |= WMI_RC_TX_STBC_FLAG;
938 arg->peer_flags |= WMI_PEER_STBC;
939 }
940
941 if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
942 u32 stbc;
943 stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
944 stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
945 stbc = stbc << WMI_RC_RX_STBC_FLAG_S;
946 arg->peer_rate_caps |= stbc;
947 arg->peer_flags |= WMI_PEER_STBC;
948 }
949
950 smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
951 smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
952
953 if (smps == WLAN_HT_CAP_SM_PS_STATIC) {
954 arg->peer_flags |= WMI_PEER_SPATIAL_MUX;
955 arg->peer_flags |= WMI_PEER_STATIC_MIMOPS;
956 } else if (smps == WLAN_HT_CAP_SM_PS_DYNAMIC) {
957 arg->peer_flags |= WMI_PEER_SPATIAL_MUX;
958 arg->peer_flags |= WMI_PEER_DYN_MIMOPS;
959 }
960
961 if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
962 arg->peer_rate_caps |= WMI_RC_TS_FLAG;
963 else if (ht_cap->mcs.rx_mask[1])
964 arg->peer_rate_caps |= WMI_RC_DS_FLAG;
965
966 for (i = 0, n = 0; i < IEEE80211_HT_MCS_MASK_LEN*8; i++)
967 if (ht_cap->mcs.rx_mask[i/8] & (1 << i%8))
968 arg->peer_ht_rates.rates[n++] = i;
969
970 arg->peer_ht_rates.num_rates = n;
971 arg->peer_num_spatial_streams = max((n+7) / 8, 1);
972
Kalle Valo60c3daa2013-09-08 17:56:07 +0300973 ath10k_dbg(ATH10K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
974 arg->addr,
Kalle Valo5e3dd152013-06-12 20:52:10 +0300975 arg->peer_ht_rates.num_rates,
976 arg->peer_num_spatial_streams);
977}
978
979static void ath10k_peer_assoc_h_qos_ap(struct ath10k *ar,
980 struct ath10k_vif *arvif,
981 struct ieee80211_sta *sta,
982 struct ieee80211_bss_conf *bss_conf,
983 struct wmi_peer_assoc_complete_arg *arg)
984{
985 u32 uapsd = 0;
986 u32 max_sp = 0;
987
Michal Kazior548db542013-07-05 16:15:15 +0300988 lockdep_assert_held(&ar->conf_mutex);
989
Kalle Valo5e3dd152013-06-12 20:52:10 +0300990 if (sta->wme)
991 arg->peer_flags |= WMI_PEER_QOS;
992
993 if (sta->wme && sta->uapsd_queues) {
Kalle Valo60c3daa2013-09-08 17:56:07 +0300994 ath10k_dbg(ATH10K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300995 sta->uapsd_queues, sta->max_sp);
996
997 arg->peer_flags |= WMI_PEER_APSD;
Janusz Dziedzicc69029b2013-08-07 12:10:49 +0200998 arg->peer_rate_caps |= WMI_RC_UAPSD_FLAG;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300999
1000 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1001 uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
1002 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
1003 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1004 uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
1005 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
1006 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1007 uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
1008 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
1009 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1010 uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
1011 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
1012
1013
1014 if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
1015 max_sp = sta->max_sp;
1016
1017 ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1018 sta->addr,
1019 WMI_AP_PS_PEER_PARAM_UAPSD,
1020 uapsd);
1021
1022 ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1023 sta->addr,
1024 WMI_AP_PS_PEER_PARAM_MAX_SP,
1025 max_sp);
1026
1027 /* TODO setup this based on STA listen interval and
1028 beacon interval. Currently we don't know
1029 sta->listen_interval - mac80211 patch required.
1030 Currently use 10 seconds */
1031 ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1032 sta->addr,
1033 WMI_AP_PS_PEER_PARAM_AGEOUT_TIME,
1034 10);
1035 }
1036}
1037
1038static void ath10k_peer_assoc_h_qos_sta(struct ath10k *ar,
1039 struct ath10k_vif *arvif,
1040 struct ieee80211_sta *sta,
1041 struct ieee80211_bss_conf *bss_conf,
1042 struct wmi_peer_assoc_complete_arg *arg)
1043{
1044 if (bss_conf->qos)
1045 arg->peer_flags |= WMI_PEER_QOS;
1046}
1047
1048static void ath10k_peer_assoc_h_vht(struct ath10k *ar,
1049 struct ieee80211_sta *sta,
1050 struct wmi_peer_assoc_complete_arg *arg)
1051{
1052 const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
Sujith Manoharana24b88b2013-10-07 19:51:57 -07001053 u8 ampdu_factor;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001054
1055 if (!vht_cap->vht_supported)
1056 return;
1057
1058 arg->peer_flags |= WMI_PEER_VHT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001059 arg->peer_vht_caps = vht_cap->cap;
1060
Sujith Manoharana24b88b2013-10-07 19:51:57 -07001061
1062 ampdu_factor = (vht_cap->cap &
1063 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
1064 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
1065
1066 /* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
1067 * zero in VHT IE. Using it would result in degraded throughput.
1068 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
1069 * it if VHT max_mpdu is smaller. */
1070 arg->peer_max_mpdu = max(arg->peer_max_mpdu,
1071 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1072 ampdu_factor)) - 1);
1073
Kalle Valo5e3dd152013-06-12 20:52:10 +03001074 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1075 arg->peer_flags |= WMI_PEER_80MHZ;
1076
1077 arg->peer_vht_rates.rx_max_rate =
1078 __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
1079 arg->peer_vht_rates.rx_mcs_set =
1080 __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
1081 arg->peer_vht_rates.tx_max_rate =
1082 __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
1083 arg->peer_vht_rates.tx_mcs_set =
1084 __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
1085
Kalle Valo60c3daa2013-09-08 17:56:07 +03001086 ath10k_dbg(ATH10K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
1087 sta->addr, arg->peer_max_mpdu, arg->peer_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001088}
1089
1090static void ath10k_peer_assoc_h_qos(struct ath10k *ar,
1091 struct ath10k_vif *arvif,
1092 struct ieee80211_sta *sta,
1093 struct ieee80211_bss_conf *bss_conf,
1094 struct wmi_peer_assoc_complete_arg *arg)
1095{
1096 switch (arvif->vdev_type) {
1097 case WMI_VDEV_TYPE_AP:
1098 ath10k_peer_assoc_h_qos_ap(ar, arvif, sta, bss_conf, arg);
1099 break;
1100 case WMI_VDEV_TYPE_STA:
1101 ath10k_peer_assoc_h_qos_sta(ar, arvif, sta, bss_conf, arg);
1102 break;
1103 default:
1104 break;
1105 }
1106}
1107
1108static void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
1109 struct ath10k_vif *arvif,
1110 struct ieee80211_sta *sta,
1111 struct wmi_peer_assoc_complete_arg *arg)
1112{
1113 enum wmi_phy_mode phymode = MODE_UNKNOWN;
1114
Kalle Valo5e3dd152013-06-12 20:52:10 +03001115 switch (ar->hw->conf.chandef.chan->band) {
1116 case IEEE80211_BAND_2GHZ:
1117 if (sta->ht_cap.ht_supported) {
1118 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1119 phymode = MODE_11NG_HT40;
1120 else
1121 phymode = MODE_11NG_HT20;
1122 } else {
1123 phymode = MODE_11G;
1124 }
1125
1126 break;
1127 case IEEE80211_BAND_5GHZ:
Sujith Manoharan7cc45e92013-09-08 18:19:55 +03001128 /*
1129 * Check VHT first.
1130 */
1131 if (sta->vht_cap.vht_supported) {
1132 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1133 phymode = MODE_11AC_VHT80;
1134 else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1135 phymode = MODE_11AC_VHT40;
1136 else if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
1137 phymode = MODE_11AC_VHT20;
1138 } else if (sta->ht_cap.ht_supported) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001139 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1140 phymode = MODE_11NA_HT40;
1141 else
1142 phymode = MODE_11NA_HT20;
1143 } else {
1144 phymode = MODE_11A;
1145 }
1146
1147 break;
1148 default:
1149 break;
1150 }
1151
Kalle Valo38a1d472013-09-08 17:56:14 +03001152 ath10k_dbg(ATH10K_DBG_MAC, "mac peer %pM phymode %s\n",
1153 sta->addr, ath10k_wmi_phymode_str(phymode));
Kalle Valo60c3daa2013-09-08 17:56:07 +03001154
Kalle Valo5e3dd152013-06-12 20:52:10 +03001155 arg->peer_phymode = phymode;
1156 WARN_ON(phymode == MODE_UNKNOWN);
1157}
1158
Kalle Valob9ada652013-10-16 15:44:46 +03001159static int ath10k_peer_assoc_prepare(struct ath10k *ar,
1160 struct ath10k_vif *arvif,
1161 struct ieee80211_sta *sta,
1162 struct ieee80211_bss_conf *bss_conf,
1163 struct wmi_peer_assoc_complete_arg *arg)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001164{
Michal Kazior548db542013-07-05 16:15:15 +03001165 lockdep_assert_held(&ar->conf_mutex);
1166
Kalle Valob9ada652013-10-16 15:44:46 +03001167 memset(arg, 0, sizeof(*arg));
Kalle Valo5e3dd152013-06-12 20:52:10 +03001168
Kalle Valob9ada652013-10-16 15:44:46 +03001169 ath10k_peer_assoc_h_basic(ar, arvif, sta, bss_conf, arg);
1170 ath10k_peer_assoc_h_crypto(ar, arvif, arg);
1171 ath10k_peer_assoc_h_rates(ar, sta, arg);
1172 ath10k_peer_assoc_h_ht(ar, sta, arg);
1173 ath10k_peer_assoc_h_vht(ar, sta, arg);
1174 ath10k_peer_assoc_h_qos(ar, arvif, sta, bss_conf, arg);
1175 ath10k_peer_assoc_h_phymode(ar, arvif, sta, arg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001176
Kalle Valob9ada652013-10-16 15:44:46 +03001177 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001178}
1179
1180/* can be called only in mac80211 callbacks due to `key_count` usage */
1181static void ath10k_bss_assoc(struct ieee80211_hw *hw,
1182 struct ieee80211_vif *vif,
1183 struct ieee80211_bss_conf *bss_conf)
1184{
1185 struct ath10k *ar = hw->priv;
1186 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Kalle Valob9ada652013-10-16 15:44:46 +03001187 struct wmi_peer_assoc_complete_arg peer_arg;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001188 struct ieee80211_sta *ap_sta;
1189 int ret;
1190
Michal Kazior548db542013-07-05 16:15:15 +03001191 lockdep_assert_held(&ar->conf_mutex);
1192
Kalle Valo5e3dd152013-06-12 20:52:10 +03001193 rcu_read_lock();
1194
1195 ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
1196 if (!ap_sta) {
1197 ath10k_warn("Failed to find station entry for %pM\n",
1198 bss_conf->bssid);
1199 rcu_read_unlock();
1200 return;
1201 }
1202
Kalle Valob9ada652013-10-16 15:44:46 +03001203 ret = ath10k_peer_assoc_prepare(ar, arvif, ap_sta,
1204 bss_conf, &peer_arg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001205 if (ret) {
Kalle Valob9ada652013-10-16 15:44:46 +03001206 ath10k_warn("Peer assoc prepare failed for %pM\n: %d",
1207 bss_conf->bssid, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001208 rcu_read_unlock();
1209 return;
1210 }
1211
1212 rcu_read_unlock();
1213
Kalle Valob9ada652013-10-16 15:44:46 +03001214 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
1215 if (ret) {
1216 ath10k_warn("Peer assoc failed for %pM\n: %d",
1217 bss_conf->bssid, ret);
1218 return;
1219 }
1220
Kalle Valo60c3daa2013-09-08 17:56:07 +03001221 ath10k_dbg(ATH10K_DBG_MAC,
1222 "mac vdev %d up (associated) bssid %pM aid %d\n",
1223 arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
1224
Kalle Valo5e3dd152013-06-12 20:52:10 +03001225 ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, bss_conf->aid,
1226 bss_conf->bssid);
1227 if (ret)
1228 ath10k_warn("VDEV: %d up failed: ret %d\n",
1229 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001230}
1231
1232/*
1233 * FIXME: flush TIDs
1234 */
1235static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
1236 struct ieee80211_vif *vif)
1237{
1238 struct ath10k *ar = hw->priv;
1239 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1240 int ret;
1241
Michal Kazior548db542013-07-05 16:15:15 +03001242 lockdep_assert_held(&ar->conf_mutex);
1243
Kalle Valo5e3dd152013-06-12 20:52:10 +03001244 /*
1245 * For some reason, calling VDEV-DOWN before VDEV-STOP
1246 * makes the FW to send frames via HTT after disassociation.
1247 * No idea why this happens, even though VDEV-DOWN is supposed
1248 * to be analogous to link down, so just stop the VDEV.
1249 */
Kalle Valo60c3daa2013-09-08 17:56:07 +03001250 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d stop (disassociated\n",
1251 arvif->vdev_id);
1252
1253 /* FIXME: check return value */
Kalle Valo5e3dd152013-06-12 20:52:10 +03001254 ret = ath10k_vdev_stop(arvif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001255
1256 /*
1257 * If we don't call VDEV-DOWN after VDEV-STOP FW will remain active and
1258 * report beacons from previously associated network through HTT.
1259 * This in turn would spam mac80211 WARN_ON if we bring down all
1260 * interfaces as it expects there is no rx when no interface is
1261 * running.
1262 */
Kalle Valo60c3daa2013-09-08 17:56:07 +03001263 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d down\n", arvif->vdev_id);
1264
1265 /* FIXME: why don't we print error if wmi call fails? */
Kalle Valo5e3dd152013-06-12 20:52:10 +03001266 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001267
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001268 arvif->def_wep_key_idx = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001269}
1270
1271static int ath10k_station_assoc(struct ath10k *ar, struct ath10k_vif *arvif,
1272 struct ieee80211_sta *sta)
1273{
Kalle Valob9ada652013-10-16 15:44:46 +03001274 struct wmi_peer_assoc_complete_arg peer_arg;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001275 int ret = 0;
1276
Michal Kazior548db542013-07-05 16:15:15 +03001277 lockdep_assert_held(&ar->conf_mutex);
1278
Kalle Valob9ada652013-10-16 15:44:46 +03001279 ret = ath10k_peer_assoc_prepare(ar, arvif, sta, NULL, &peer_arg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001280 if (ret) {
Kalle Valob9ada652013-10-16 15:44:46 +03001281 ath10k_warn("WMI peer assoc prepare failed for %pM\n",
1282 sta->addr);
1283 return ret;
1284 }
1285
1286 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
1287 if (ret) {
1288 ath10k_warn("Peer assoc failed for STA %pM\n: %d",
1289 sta->addr, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001290 return ret;
1291 }
1292
1293 ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
1294 if (ret) {
1295 ath10k_warn("could not install peer wep keys (%d)\n", ret);
1296 return ret;
1297 }
1298
1299 return ret;
1300}
1301
1302static int ath10k_station_disassoc(struct ath10k *ar, struct ath10k_vif *arvif,
1303 struct ieee80211_sta *sta)
1304{
1305 int ret = 0;
1306
Michal Kazior548db542013-07-05 16:15:15 +03001307 lockdep_assert_held(&ar->conf_mutex);
1308
Kalle Valo5e3dd152013-06-12 20:52:10 +03001309 ret = ath10k_clear_peer_keys(arvif, sta->addr);
1310 if (ret) {
1311 ath10k_warn("could not clear all peer wep keys (%d)\n", ret);
1312 return ret;
1313 }
1314
1315 return ret;
1316}
1317
1318/**************/
1319/* Regulatory */
1320/**************/
1321
1322static int ath10k_update_channel_list(struct ath10k *ar)
1323{
1324 struct ieee80211_hw *hw = ar->hw;
1325 struct ieee80211_supported_band **bands;
1326 enum ieee80211_band band;
1327 struct ieee80211_channel *channel;
1328 struct wmi_scan_chan_list_arg arg = {0};
1329 struct wmi_channel_arg *ch;
1330 bool passive;
1331 int len;
1332 int ret;
1333 int i;
1334
Michal Kazior548db542013-07-05 16:15:15 +03001335 lockdep_assert_held(&ar->conf_mutex);
1336
Kalle Valo5e3dd152013-06-12 20:52:10 +03001337 bands = hw->wiphy->bands;
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 if (bands[band]->channels[i].flags &
1344 IEEE80211_CHAN_DISABLED)
1345 continue;
1346
1347 arg.n_channels++;
1348 }
1349 }
1350
1351 len = sizeof(struct wmi_channel_arg) * arg.n_channels;
1352 arg.channels = kzalloc(len, GFP_KERNEL);
1353 if (!arg.channels)
1354 return -ENOMEM;
1355
1356 ch = arg.channels;
1357 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1358 if (!bands[band])
1359 continue;
1360
1361 for (i = 0; i < bands[band]->n_channels; i++) {
1362 channel = &bands[band]->channels[i];
1363
1364 if (channel->flags & IEEE80211_CHAN_DISABLED)
1365 continue;
1366
1367 ch->allow_ht = true;
1368
1369 /* FIXME: when should we really allow VHT? */
1370 ch->allow_vht = true;
1371
1372 ch->allow_ibss =
1373 !(channel->flags & IEEE80211_CHAN_NO_IBSS);
1374
1375 ch->ht40plus =
1376 !(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
1377
1378 passive = channel->flags & IEEE80211_CHAN_PASSIVE_SCAN;
1379 ch->passive = passive;
1380
1381 ch->freq = channel->center_freq;
Michal Kazior89c5c842013-10-23 04:02:13 -07001382 ch->min_power = 0;
Michal Kazior02256932013-10-23 04:02:14 -07001383 ch->max_power = channel->max_power * 2;
1384 ch->max_reg_power = channel->max_reg_power * 2;
1385 ch->max_antenna_gain = channel->max_antenna_gain * 2;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001386 ch->reg_class_id = 0; /* FIXME */
1387
1388 /* FIXME: why use only legacy modes, why not any
1389 * HT/VHT modes? Would that even make any
1390 * difference? */
1391 if (channel->band == IEEE80211_BAND_2GHZ)
1392 ch->mode = MODE_11G;
1393 else
1394 ch->mode = MODE_11A;
1395
1396 if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
1397 continue;
1398
1399 ath10k_dbg(ATH10K_DBG_WMI,
Kalle Valo60c3daa2013-09-08 17:56:07 +03001400 "mac channel [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
1401 ch - arg.channels, arg.n_channels,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001402 ch->freq, ch->max_power, ch->max_reg_power,
1403 ch->max_antenna_gain, ch->mode);
1404
1405 ch++;
1406 }
1407 }
1408
1409 ret = ath10k_wmi_scan_chan_list(ar, &arg);
1410 kfree(arg.channels);
1411
1412 return ret;
1413}
1414
Michal Kaziorf7843d72013-07-16 09:38:52 +02001415static void ath10k_regd_update(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001416{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001417 struct reg_dmn_pair_mapping *regpair;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001418 int ret;
1419
Michal Kaziorf7843d72013-07-16 09:38:52 +02001420 lockdep_assert_held(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001421
1422 ret = ath10k_update_channel_list(ar);
1423 if (ret)
1424 ath10k_warn("could not update channel list (%d)\n", ret);
1425
1426 regpair = ar->ath_common.regulatory.regpair;
Michal Kaziorf7843d72013-07-16 09:38:52 +02001427
Kalle Valo5e3dd152013-06-12 20:52:10 +03001428 /* Target allows setting up per-band regdomain but ath_common provides
1429 * a combined one only */
1430 ret = ath10k_wmi_pdev_set_regdomain(ar,
1431 regpair->regDmnEnum,
1432 regpair->regDmnEnum, /* 2ghz */
1433 regpair->regDmnEnum, /* 5ghz */
1434 regpair->reg_2ghz_ctl,
1435 regpair->reg_5ghz_ctl);
1436 if (ret)
1437 ath10k_warn("could not set pdev regdomain (%d)\n", ret);
Michal Kaziorf7843d72013-07-16 09:38:52 +02001438}
Michal Kazior548db542013-07-05 16:15:15 +03001439
Michal Kaziorf7843d72013-07-16 09:38:52 +02001440static void ath10k_reg_notifier(struct wiphy *wiphy,
1441 struct regulatory_request *request)
1442{
1443 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
1444 struct ath10k *ar = hw->priv;
1445
1446 ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
1447
1448 mutex_lock(&ar->conf_mutex);
1449 if (ar->state == ATH10K_STATE_ON)
1450 ath10k_regd_update(ar);
Michal Kazior548db542013-07-05 16:15:15 +03001451 mutex_unlock(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001452}
1453
1454/***************/
1455/* TX handlers */
1456/***************/
1457
Michal Kazior42c3aa62013-10-02 11:03:38 +02001458static u8 ath10k_tx_h_get_tid(struct ieee80211_hdr *hdr)
1459{
1460 if (ieee80211_is_mgmt(hdr->frame_control))
1461 return HTT_DATA_TX_EXT_TID_MGMT;
1462
1463 if (!ieee80211_is_data_qos(hdr->frame_control))
1464 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
1465
1466 if (!is_unicast_ether_addr(ieee80211_get_DA(hdr)))
1467 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
1468
1469 return ieee80211_get_qos_ctl(hdr)[0] & IEEE80211_QOS_CTL_TID_MASK;
1470}
1471
Michal Kaziorddb6ad72013-10-02 11:03:39 +02001472static u8 ath10k_tx_h_get_vdev_id(struct ath10k *ar,
1473 struct ieee80211_tx_info *info)
1474{
1475 if (info->control.vif)
1476 return ath10k_vif_to_arvif(info->control.vif)->vdev_id;
1477
1478 if (ar->monitor_enabled)
1479 return ar->monitor_vdev_id;
1480
1481 ath10k_warn("could not resolve vdev id\n");
1482 return 0;
1483}
1484
Kalle Valo5e3dd152013-06-12 20:52:10 +03001485/*
1486 * Frames sent to the FW have to be in "Native Wifi" format.
1487 * Strip the QoS field from the 802.11 header.
1488 */
1489static void ath10k_tx_h_qos_workaround(struct ieee80211_hw *hw,
1490 struct ieee80211_tx_control *control,
1491 struct sk_buff *skb)
1492{
1493 struct ieee80211_hdr *hdr = (void *)skb->data;
1494 u8 *qos_ctl;
1495
1496 if (!ieee80211_is_data_qos(hdr->frame_control))
1497 return;
1498
1499 qos_ctl = ieee80211_get_qos_ctl(hdr);
Michal Kaziorba0ccd72013-07-22 14:25:28 +02001500 memmove(skb->data + IEEE80211_QOS_CTL_LEN,
1501 skb->data, (void *)qos_ctl - (void *)skb->data);
1502 skb_pull(skb, IEEE80211_QOS_CTL_LEN);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001503}
1504
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001505static void ath10k_tx_wep_key_work(struct work_struct *work)
1506{
1507 struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
1508 wep_key_work);
1509 int ret, keyidx = arvif->def_wep_key_newidx;
1510
1511 if (arvif->def_wep_key_idx == keyidx)
1512 return;
1513
1514 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
1515 arvif->vdev_id, keyidx);
1516
1517 ret = ath10k_wmi_vdev_set_param(arvif->ar,
1518 arvif->vdev_id,
1519 arvif->ar->wmi.vdev_param->def_keyid,
1520 keyidx);
1521 if (ret) {
1522 ath10k_warn("could not update wep keyidx (%d)\n", ret);
1523 return;
1524 }
1525
1526 arvif->def_wep_key_idx = keyidx;
1527}
1528
Kalle Valo5e3dd152013-06-12 20:52:10 +03001529static void ath10k_tx_h_update_wep_key(struct sk_buff *skb)
1530{
1531 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1532 struct ieee80211_vif *vif = info->control.vif;
1533 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1534 struct ath10k *ar = arvif->ar;
1535 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1536 struct ieee80211_key_conf *key = info->control.hw_key;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001537
Kalle Valo5e3dd152013-06-12 20:52:10 +03001538 if (!ieee80211_has_protected(hdr->frame_control))
1539 return;
1540
1541 if (!key)
1542 return;
1543
1544 if (key->cipher != WLAN_CIPHER_SUITE_WEP40 &&
1545 key->cipher != WLAN_CIPHER_SUITE_WEP104)
1546 return;
1547
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001548 if (key->keyidx == arvif->def_wep_key_idx)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001549 return;
1550
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001551 /* FIXME: Most likely a few frames will be TXed with an old key. Simply
1552 * queueing frames until key index is updated is not an option because
1553 * sk_buff may need more processing to be done, e.g. offchannel */
1554 arvif->def_wep_key_newidx = key->keyidx;
1555 ieee80211_queue_work(ar->hw, &arvif->wep_key_work);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001556}
1557
1558static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar, struct sk_buff *skb)
1559{
1560 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1561 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1562 struct ieee80211_vif *vif = info->control.vif;
1563 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1564
1565 /* This is case only for P2P_GO */
1566 if (arvif->vdev_type != WMI_VDEV_TYPE_AP ||
1567 arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
1568 return;
1569
1570 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
1571 spin_lock_bh(&ar->data_lock);
1572 if (arvif->u.ap.noa_data)
1573 if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
1574 GFP_ATOMIC))
1575 memcpy(skb_put(skb, arvif->u.ap.noa_len),
1576 arvif->u.ap.noa_data,
1577 arvif->u.ap.noa_len);
1578 spin_unlock_bh(&ar->data_lock);
1579 }
1580}
1581
1582static void ath10k_tx_htt(struct ath10k *ar, struct sk_buff *skb)
1583{
1584 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Bartosz Markowski5e00d312013-09-26 17:47:12 +02001585 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001586
Michal Kazior961d4c32013-08-09 10:13:34 +02001587 if (ar->htt.target_version_major >= 3) {
1588 /* Since HTT 3.0 there is no separate mgmt tx command */
1589 ret = ath10k_htt_tx(&ar->htt, skb);
1590 goto exit;
1591 }
1592
Bartosz Markowski5e00d312013-09-26 17:47:12 +02001593 if (ieee80211_is_mgmt(hdr->frame_control)) {
1594 if (test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
1595 ar->fw_features)) {
1596 if (skb_queue_len(&ar->wmi_mgmt_tx_queue) >=
1597 ATH10K_MAX_NUM_MGMT_PENDING) {
1598 ath10k_warn("wmi mgmt_tx queue limit reached\n");
1599 ret = -EBUSY;
1600 goto exit;
1601 }
1602
1603 skb_queue_tail(&ar->wmi_mgmt_tx_queue, skb);
1604 ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
1605 } else {
1606 ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
1607 }
1608 } else if (!test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
1609 ar->fw_features) &&
1610 ieee80211_is_nullfunc(hdr->frame_control)) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001611 /* FW does not report tx status properly for NullFunc frames
1612 * unless they are sent through mgmt tx path. mac80211 sends
Bartosz Markowski5e00d312013-09-26 17:47:12 +02001613 * those frames when it detects link/beacon loss and depends
1614 * on the tx status to be correct. */
Michal Kazioredb82362013-07-05 16:15:14 +03001615 ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02001616 } else {
Michal Kazioredb82362013-07-05 16:15:14 +03001617 ret = ath10k_htt_tx(&ar->htt, skb);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02001618 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001619
Michal Kazior961d4c32013-08-09 10:13:34 +02001620exit:
Kalle Valo5e3dd152013-06-12 20:52:10 +03001621 if (ret) {
1622 ath10k_warn("tx failed (%d). dropping packet.\n", ret);
1623 ieee80211_free_txskb(ar->hw, skb);
1624 }
1625}
1626
1627void ath10k_offchan_tx_purge(struct ath10k *ar)
1628{
1629 struct sk_buff *skb;
1630
1631 for (;;) {
1632 skb = skb_dequeue(&ar->offchan_tx_queue);
1633 if (!skb)
1634 break;
1635
1636 ieee80211_free_txskb(ar->hw, skb);
1637 }
1638}
1639
1640void ath10k_offchan_tx_work(struct work_struct *work)
1641{
1642 struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
1643 struct ath10k_peer *peer;
1644 struct ieee80211_hdr *hdr;
1645 struct sk_buff *skb;
1646 const u8 *peer_addr;
1647 int vdev_id;
1648 int ret;
1649
1650 /* FW requirement: We must create a peer before FW will send out
1651 * an offchannel frame. Otherwise the frame will be stuck and
1652 * never transmitted. We delete the peer upon tx completion.
1653 * It is unlikely that a peer for offchannel tx will already be
1654 * present. However it may be in some rare cases so account for that.
1655 * Otherwise we might remove a legitimate peer and break stuff. */
1656
1657 for (;;) {
1658 skb = skb_dequeue(&ar->offchan_tx_queue);
1659 if (!skb)
1660 break;
1661
1662 mutex_lock(&ar->conf_mutex);
1663
Kalle Valo60c3daa2013-09-08 17:56:07 +03001664 ath10k_dbg(ATH10K_DBG_MAC, "mac offchannel skb %p\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001665 skb);
1666
1667 hdr = (struct ieee80211_hdr *)skb->data;
1668 peer_addr = ieee80211_get_DA(hdr);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02001669 vdev_id = ATH10K_SKB_CB(skb)->vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001670
1671 spin_lock_bh(&ar->data_lock);
1672 peer = ath10k_peer_find(ar, vdev_id, peer_addr);
1673 spin_unlock_bh(&ar->data_lock);
1674
1675 if (peer)
Kalle Valo60c3daa2013-09-08 17:56:07 +03001676 /* FIXME: should this use ath10k_warn()? */
Kalle Valo5e3dd152013-06-12 20:52:10 +03001677 ath10k_dbg(ATH10K_DBG_MAC, "peer %pM on vdev %d already present\n",
1678 peer_addr, vdev_id);
1679
1680 if (!peer) {
1681 ret = ath10k_peer_create(ar, vdev_id, peer_addr);
1682 if (ret)
1683 ath10k_warn("peer %pM on vdev %d not created (%d)\n",
1684 peer_addr, vdev_id, ret);
1685 }
1686
1687 spin_lock_bh(&ar->data_lock);
1688 INIT_COMPLETION(ar->offchan_tx_completed);
1689 ar->offchan_tx_skb = skb;
1690 spin_unlock_bh(&ar->data_lock);
1691
1692 ath10k_tx_htt(ar, skb);
1693
1694 ret = wait_for_completion_timeout(&ar->offchan_tx_completed,
1695 3 * HZ);
1696 if (ret <= 0)
1697 ath10k_warn("timed out waiting for offchannel skb %p\n",
1698 skb);
1699
1700 if (!peer) {
1701 ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
1702 if (ret)
1703 ath10k_warn("peer %pM on vdev %d not deleted (%d)\n",
1704 peer_addr, vdev_id, ret);
1705 }
1706
1707 mutex_unlock(&ar->conf_mutex);
1708 }
1709}
1710
Bartosz Markowski5e00d312013-09-26 17:47:12 +02001711void ath10k_mgmt_over_wmi_tx_purge(struct ath10k *ar)
1712{
1713 struct sk_buff *skb;
1714
1715 for (;;) {
1716 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
1717 if (!skb)
1718 break;
1719
1720 ieee80211_free_txskb(ar->hw, skb);
1721 }
1722}
1723
1724void ath10k_mgmt_over_wmi_tx_work(struct work_struct *work)
1725{
1726 struct ath10k *ar = container_of(work, struct ath10k, wmi_mgmt_tx_work);
1727 struct sk_buff *skb;
1728 int ret;
1729
1730 for (;;) {
1731 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
1732 if (!skb)
1733 break;
1734
1735 ret = ath10k_wmi_mgmt_tx(ar, skb);
Michal Kazior5fb5e412013-10-28 07:18:13 +01001736 if (ret) {
Bartosz Markowski5e00d312013-09-26 17:47:12 +02001737 ath10k_warn("wmi mgmt_tx failed (%d)\n", ret);
Michal Kazior5fb5e412013-10-28 07:18:13 +01001738 ieee80211_free_txskb(ar->hw, skb);
1739 }
Bartosz Markowski5e00d312013-09-26 17:47:12 +02001740 }
1741}
1742
Kalle Valo5e3dd152013-06-12 20:52:10 +03001743/************/
1744/* Scanning */
1745/************/
1746
1747/*
1748 * This gets called if we dont get a heart-beat during scan.
1749 * This may indicate the FW has hung and we need to abort the
1750 * scan manually to prevent cancel_hw_scan() from deadlocking
1751 */
1752void ath10k_reset_scan(unsigned long ptr)
1753{
1754 struct ath10k *ar = (struct ath10k *)ptr;
1755
1756 spin_lock_bh(&ar->data_lock);
1757 if (!ar->scan.in_progress) {
1758 spin_unlock_bh(&ar->data_lock);
1759 return;
1760 }
1761
1762 ath10k_warn("scan timeout. resetting. fw issue?\n");
1763
1764 if (ar->scan.is_roc)
1765 ieee80211_remain_on_channel_expired(ar->hw);
1766 else
1767 ieee80211_scan_completed(ar->hw, 1 /* aborted */);
1768
1769 ar->scan.in_progress = false;
1770 complete_all(&ar->scan.completed);
1771 spin_unlock_bh(&ar->data_lock);
1772}
1773
1774static int ath10k_abort_scan(struct ath10k *ar)
1775{
1776 struct wmi_stop_scan_arg arg = {
1777 .req_id = 1, /* FIXME */
1778 .req_type = WMI_SCAN_STOP_ONE,
1779 .u.scan_id = ATH10K_SCAN_ID,
1780 };
1781 int ret;
1782
1783 lockdep_assert_held(&ar->conf_mutex);
1784
1785 del_timer_sync(&ar->scan.timeout);
1786
1787 spin_lock_bh(&ar->data_lock);
1788 if (!ar->scan.in_progress) {
1789 spin_unlock_bh(&ar->data_lock);
1790 return 0;
1791 }
1792
1793 ar->scan.aborting = true;
1794 spin_unlock_bh(&ar->data_lock);
1795
1796 ret = ath10k_wmi_stop_scan(ar, &arg);
1797 if (ret) {
1798 ath10k_warn("could not submit wmi stop scan (%d)\n", ret);
Michal Kazioradb8c9b2013-07-05 16:15:16 +03001799 spin_lock_bh(&ar->data_lock);
1800 ar->scan.in_progress = false;
1801 ath10k_offchan_tx_purge(ar);
1802 spin_unlock_bh(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001803 return -EIO;
1804 }
1805
Kalle Valo5e3dd152013-06-12 20:52:10 +03001806 ret = wait_for_completion_timeout(&ar->scan.completed, 3*HZ);
1807 if (ret == 0)
1808 ath10k_warn("timed out while waiting for scan to stop\n");
1809
1810 /* scan completion may be done right after we timeout here, so let's
1811 * check the in_progress and tell mac80211 scan is completed. if we
1812 * don't do that and FW fails to send us scan completion indication
1813 * then userspace won't be able to scan anymore */
1814 ret = 0;
1815
1816 spin_lock_bh(&ar->data_lock);
1817 if (ar->scan.in_progress) {
1818 ath10k_warn("could not stop scan. its still in progress\n");
1819 ar->scan.in_progress = false;
1820 ath10k_offchan_tx_purge(ar);
1821 ret = -ETIMEDOUT;
1822 }
1823 spin_unlock_bh(&ar->data_lock);
1824
1825 return ret;
1826}
1827
1828static int ath10k_start_scan(struct ath10k *ar,
1829 const struct wmi_start_scan_arg *arg)
1830{
1831 int ret;
1832
1833 lockdep_assert_held(&ar->conf_mutex);
1834
1835 ret = ath10k_wmi_start_scan(ar, arg);
1836 if (ret)
1837 return ret;
1838
Kalle Valo5e3dd152013-06-12 20:52:10 +03001839 ret = wait_for_completion_timeout(&ar->scan.started, 1*HZ);
1840 if (ret == 0) {
1841 ath10k_abort_scan(ar);
1842 return ret;
1843 }
1844
1845 /* the scan can complete earlier, before we even
1846 * start the timer. in that case the timer handler
1847 * checks ar->scan.in_progress and bails out if its
1848 * false. Add a 200ms margin to account event/command
1849 * processing. */
1850 mod_timer(&ar->scan.timeout, jiffies +
1851 msecs_to_jiffies(arg->max_scan_time+200));
1852 return 0;
1853}
1854
1855/**********************/
1856/* mac80211 callbacks */
1857/**********************/
1858
1859static void ath10k_tx(struct ieee80211_hw *hw,
1860 struct ieee80211_tx_control *control,
1861 struct sk_buff *skb)
1862{
1863 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1864 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1865 struct ath10k *ar = hw->priv;
Michal Kaziorddb6ad72013-10-02 11:03:39 +02001866 u8 tid, vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001867
1868 /* We should disable CCK RATE due to P2P */
1869 if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
1870 ath10k_dbg(ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
1871
1872 /* we must calculate tid before we apply qos workaround
1873 * as we'd lose the qos control field */
Michal Kazior42c3aa62013-10-02 11:03:38 +02001874 tid = ath10k_tx_h_get_tid(hdr);
Michal Kaziorddb6ad72013-10-02 11:03:39 +02001875 vdev_id = ath10k_tx_h_get_vdev_id(ar, info);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001876
Michal Kaziorcf84bd42013-07-16 11:04:54 +02001877 /* it makes no sense to process injected frames like that */
1878 if (info->control.vif &&
1879 info->control.vif->type != NL80211_IFTYPE_MONITOR) {
1880 ath10k_tx_h_qos_workaround(hw, control, skb);
1881 ath10k_tx_h_update_wep_key(skb);
1882 ath10k_tx_h_add_p2p_noa_ie(ar, skb);
1883 ath10k_tx_h_seq_no(skb);
1884 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001885
Bartosz Markowski5e00d312013-09-26 17:47:12 +02001886 ATH10K_SKB_CB(skb)->vdev_id = vdev_id;
Michal Kazior27bb1782013-09-18 14:43:19 +02001887 ATH10K_SKB_CB(skb)->htt.is_offchan = false;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001888 ATH10K_SKB_CB(skb)->htt.tid = tid;
1889
1890 if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
1891 spin_lock_bh(&ar->data_lock);
1892 ATH10K_SKB_CB(skb)->htt.is_offchan = true;
Bartosz Markowski5e00d312013-09-26 17:47:12 +02001893 ATH10K_SKB_CB(skb)->vdev_id = ar->scan.vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001894 spin_unlock_bh(&ar->data_lock);
1895
1896 ath10k_dbg(ATH10K_DBG_MAC, "queued offchannel skb %p\n", skb);
1897
1898 skb_queue_tail(&ar->offchan_tx_queue, skb);
1899 ieee80211_queue_work(hw, &ar->offchan_tx_work);
1900 return;
1901 }
1902
1903 ath10k_tx_htt(ar, skb);
1904}
1905
1906/*
1907 * Initialize various parameters with default vaules.
1908 */
Michal Kazioraffd3212013-07-16 09:54:35 +02001909void ath10k_halt(struct ath10k *ar)
Michal Kazior818bdd12013-07-16 09:38:57 +02001910{
1911 lockdep_assert_held(&ar->conf_mutex);
1912
1913 del_timer_sync(&ar->scan.timeout);
1914 ath10k_offchan_tx_purge(ar);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02001915 ath10k_mgmt_over_wmi_tx_purge(ar);
Michal Kazior818bdd12013-07-16 09:38:57 +02001916 ath10k_peer_cleanup_all(ar);
1917 ath10k_core_stop(ar);
1918 ath10k_hif_power_down(ar);
1919
1920 spin_lock_bh(&ar->data_lock);
1921 if (ar->scan.in_progress) {
1922 del_timer(&ar->scan.timeout);
1923 ar->scan.in_progress = false;
1924 ieee80211_scan_completed(ar->hw, true);
1925 }
1926 spin_unlock_bh(&ar->data_lock);
1927}
1928
Kalle Valo5e3dd152013-06-12 20:52:10 +03001929static int ath10k_start(struct ieee80211_hw *hw)
1930{
1931 struct ath10k *ar = hw->priv;
Michal Kazior818bdd12013-07-16 09:38:57 +02001932 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001933
Michal Kazior548db542013-07-05 16:15:15 +03001934 mutex_lock(&ar->conf_mutex);
1935
Michal Kazioraffd3212013-07-16 09:54:35 +02001936 if (ar->state != ATH10K_STATE_OFF &&
1937 ar->state != ATH10K_STATE_RESTARTING) {
Michal Kazior818bdd12013-07-16 09:38:57 +02001938 ret = -EINVAL;
1939 goto exit;
1940 }
1941
1942 ret = ath10k_hif_power_up(ar);
1943 if (ret) {
1944 ath10k_err("could not init hif (%d)\n", ret);
1945 ar->state = ATH10K_STATE_OFF;
1946 goto exit;
1947 }
1948
1949 ret = ath10k_core_start(ar);
1950 if (ret) {
1951 ath10k_err("could not init core (%d)\n", ret);
1952 ath10k_hif_power_down(ar);
1953 ar->state = ATH10K_STATE_OFF;
1954 goto exit;
1955 }
1956
Michal Kazioraffd3212013-07-16 09:54:35 +02001957 if (ar->state == ATH10K_STATE_OFF)
1958 ar->state = ATH10K_STATE_ON;
1959 else if (ar->state == ATH10K_STATE_RESTARTING)
1960 ar->state = ATH10K_STATE_RESTARTED;
1961
Bartosz Markowski226a3392013-09-26 17:47:16 +02001962 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->pmf_qos, 1);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001963 if (ret)
1964 ath10k_warn("could not enable WMI_PDEV_PARAM_PMF_QOS (%d)\n",
1965 ret);
1966
Bartosz Markowski226a3392013-09-26 17:47:16 +02001967 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->dynamic_bw, 0);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001968 if (ret)
1969 ath10k_warn("could not init WMI_PDEV_PARAM_DYNAMIC_BW (%d)\n",
1970 ret);
1971
Michal Kaziorf7843d72013-07-16 09:38:52 +02001972 ath10k_regd_update(ar);
1973
Michal Kazior818bdd12013-07-16 09:38:57 +02001974exit:
Michal Kazior548db542013-07-05 16:15:15 +03001975 mutex_unlock(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001976 return 0;
1977}
1978
1979static void ath10k_stop(struct ieee80211_hw *hw)
1980{
1981 struct ath10k *ar = hw->priv;
1982
Michal Kazior548db542013-07-05 16:15:15 +03001983 mutex_lock(&ar->conf_mutex);
Michal Kazioraffd3212013-07-16 09:54:35 +02001984 if (ar->state == ATH10K_STATE_ON ||
1985 ar->state == ATH10K_STATE_RESTARTED ||
1986 ar->state == ATH10K_STATE_WEDGED)
Michal Kazior818bdd12013-07-16 09:38:57 +02001987 ath10k_halt(ar);
Michal Kaziora96d7742013-07-16 09:38:56 +02001988
Michal Kaziorf7843d72013-07-16 09:38:52 +02001989 ar->state = ATH10K_STATE_OFF;
Michal Kazior548db542013-07-05 16:15:15 +03001990 mutex_unlock(&ar->conf_mutex);
1991
Bartosz Markowski5e00d312013-09-26 17:47:12 +02001992 ath10k_mgmt_over_wmi_tx_purge(ar);
1993
Michal Kazior548db542013-07-05 16:15:15 +03001994 cancel_work_sync(&ar->offchan_tx_work);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02001995 cancel_work_sync(&ar->wmi_mgmt_tx_work);
Michal Kazioraffd3212013-07-16 09:54:35 +02001996 cancel_work_sync(&ar->restart_work);
1997}
1998
Michal Kaziorad088bf2013-10-16 15:44:46 +03001999static int ath10k_config_ps(struct ath10k *ar)
Michal Kazioraffd3212013-07-16 09:54:35 +02002000{
Michal Kaziorad088bf2013-10-16 15:44:46 +03002001 struct ath10k_vif *arvif;
2002 int ret = 0;
Michal Kazioraffd3212013-07-16 09:54:35 +02002003
2004 lockdep_assert_held(&ar->conf_mutex);
2005
Michal Kaziorad088bf2013-10-16 15:44:46 +03002006 list_for_each_entry(arvif, &ar->arvifs, list) {
2007 ret = ath10k_mac_vif_setup_ps(arvif);
2008 if (ret) {
2009 ath10k_warn("could not setup powersave (%d)\n", ret);
2010 break;
2011 }
2012 }
Michal Kazioraffd3212013-07-16 09:54:35 +02002013
Michal Kaziorad088bf2013-10-16 15:44:46 +03002014 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002015}
2016
2017static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
2018{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002019 struct ath10k *ar = hw->priv;
2020 struct ieee80211_conf *conf = &hw->conf;
2021 int ret = 0;
Michal Kazior5474efe2013-10-23 04:02:15 -07002022 u32 param;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002023
2024 mutex_lock(&ar->conf_mutex);
2025
2026 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
Kalle Valo60c3daa2013-09-08 17:56:07 +03002027 ath10k_dbg(ATH10K_DBG_MAC, "mac config channel %d mhz\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002028 conf->chandef.chan->center_freq);
2029 spin_lock_bh(&ar->data_lock);
2030 ar->rx_channel = conf->chandef.chan;
2031 spin_unlock_bh(&ar->data_lock);
2032 }
2033
Michal Kazior5474efe2013-10-23 04:02:15 -07002034 if (changed & IEEE80211_CONF_CHANGE_POWER) {
2035 ath10k_dbg(ATH10K_DBG_MAC, "mac config power %d\n",
2036 hw->conf.power_level);
2037
2038 param = ar->wmi.pdev_param->txpower_limit2g;
2039 ret = ath10k_wmi_pdev_set_param(ar, param,
2040 hw->conf.power_level * 2);
2041 if (ret)
2042 ath10k_warn("mac failed to set 2g txpower %d (%d)\n",
2043 hw->conf.power_level, ret);
2044
2045 param = ar->wmi.pdev_param->txpower_limit5g;
2046 ret = ath10k_wmi_pdev_set_param(ar, param,
2047 hw->conf.power_level * 2);
2048 if (ret)
2049 ath10k_warn("mac failed to set 5g txpower %d (%d)\n",
2050 hw->conf.power_level, ret);
2051 }
2052
Michal Kazioraffd3212013-07-16 09:54:35 +02002053 if (changed & IEEE80211_CONF_CHANGE_PS)
2054 ath10k_config_ps(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002055
2056 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
2057 if (conf->flags & IEEE80211_CONF_MONITOR)
2058 ret = ath10k_monitor_create(ar);
2059 else
2060 ret = ath10k_monitor_destroy(ar);
2061 }
2062
2063 mutex_unlock(&ar->conf_mutex);
2064 return ret;
2065}
2066
2067/*
2068 * TODO:
2069 * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
2070 * because we will send mgmt frames without CCK. This requirement
2071 * for P2P_FIND/GO_NEG should be handled by checking CCK flag
2072 * in the TX packet.
2073 */
2074static int ath10k_add_interface(struct ieee80211_hw *hw,
2075 struct ieee80211_vif *vif)
2076{
2077 struct ath10k *ar = hw->priv;
2078 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2079 enum wmi_sta_powersave_param param;
2080 int ret = 0;
Michal Kazior424121c2013-07-22 14:13:31 +02002081 u32 value;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002082 int bit;
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02002083 u32 vdev_param;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002084
2085 mutex_lock(&ar->conf_mutex);
2086
Michal Kazior0dbd09e2013-07-31 10:55:14 +02002087 memset(arvif, 0, sizeof(*arvif));
2088
Kalle Valo5e3dd152013-06-12 20:52:10 +03002089 arvif->ar = ar;
2090 arvif->vif = vif;
2091
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002092 INIT_WORK(&arvif->wep_key_work, ath10k_tx_wep_key_work);
Ben Greeare63b33f2013-10-22 14:54:14 -07002093 INIT_LIST_HEAD(&arvif->list);
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002094
Kalle Valo5e3dd152013-06-12 20:52:10 +03002095 if ((vif->type == NL80211_IFTYPE_MONITOR) && ar->monitor_present) {
2096 ath10k_warn("Only one monitor interface allowed\n");
2097 ret = -EBUSY;
Michal Kazior9dad14a2013-10-16 15:44:45 +03002098 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002099 }
2100
2101 bit = ffs(ar->free_vdev_map);
2102 if (bit == 0) {
2103 ret = -EBUSY;
Michal Kazior9dad14a2013-10-16 15:44:45 +03002104 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002105 }
2106
2107 arvif->vdev_id = bit - 1;
2108 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002109
2110 if (ar->p2p)
2111 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
2112
2113 switch (vif->type) {
2114 case NL80211_IFTYPE_UNSPECIFIED:
2115 case NL80211_IFTYPE_STATION:
2116 arvif->vdev_type = WMI_VDEV_TYPE_STA;
2117 if (vif->p2p)
2118 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
2119 break;
2120 case NL80211_IFTYPE_ADHOC:
2121 arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
2122 break;
2123 case NL80211_IFTYPE_AP:
2124 arvif->vdev_type = WMI_VDEV_TYPE_AP;
2125
2126 if (vif->p2p)
2127 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
2128 break;
2129 case NL80211_IFTYPE_MONITOR:
2130 arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
2131 break;
2132 default:
2133 WARN_ON(1);
2134 break;
2135 }
2136
Kalle Valo60c3daa2013-09-08 17:56:07 +03002137 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002138 arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype);
2139
2140 ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
2141 arvif->vdev_subtype, vif->addr);
2142 if (ret) {
2143 ath10k_warn("WMI vdev create failed: ret %d\n", ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002144 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002145 }
2146
Michal Kazior9dad14a2013-10-16 15:44:45 +03002147 ar->free_vdev_map &= ~BIT(arvif->vdev_id);
Michal Kazior05791192013-10-16 15:44:45 +03002148 list_add(&arvif->list, &ar->arvifs);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002149
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02002150 vdev_param = ar->wmi.vdev_param->def_keyid;
2151 ret = ath10k_wmi_vdev_set_param(ar, 0, vdev_param,
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002152 arvif->def_wep_key_idx);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002153 if (ret) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002154 ath10k_warn("Failed to set default keyid: %d\n", ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002155 goto err_vdev_delete;
2156 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002157
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02002158 vdev_param = ar->wmi.vdev_param->tx_encap_type;
2159 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002160 ATH10K_HW_TXRX_NATIVE_WIFI);
Bartosz Markowskiebc9abd2013-10-15 09:26:20 +02002161 /* 10.X firmware does not support this VDEV parameter. Do not warn */
Michal Kazior9dad14a2013-10-16 15:44:45 +03002162 if (ret && ret != -EOPNOTSUPP) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002163 ath10k_warn("Failed to set TX encap: %d\n", ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002164 goto err_vdev_delete;
2165 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002166
2167 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
2168 ret = ath10k_peer_create(ar, arvif->vdev_id, vif->addr);
2169 if (ret) {
2170 ath10k_warn("Failed to create peer for AP: %d\n", ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002171 goto err_vdev_delete;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002172 }
2173 }
2174
2175 if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
2176 param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
2177 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
2178 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
2179 param, value);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002180 if (ret) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002181 ath10k_warn("Failed to set RX wake policy: %d\n", ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002182 goto err_peer_delete;
2183 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002184
2185 param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
2186 value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
2187 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
2188 param, value);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002189 if (ret) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002190 ath10k_warn("Failed to set TX wake thresh: %d\n", ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002191 goto err_peer_delete;
2192 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002193
2194 param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
2195 value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
2196 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
2197 param, value);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002198 if (ret) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002199 ath10k_warn("Failed to set PSPOLL count: %d\n", ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002200 goto err_peer_delete;
2201 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002202 }
2203
Michal Kazior424121c2013-07-22 14:13:31 +02002204 ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002205 if (ret) {
Michal Kazior679c54a2013-07-05 16:15:04 +03002206 ath10k_warn("failed to set rts threshold for vdev %d (%d)\n",
2207 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002208 goto err_peer_delete;
2209 }
Michal Kazior679c54a2013-07-05 16:15:04 +03002210
Michal Kazior424121c2013-07-22 14:13:31 +02002211 ret = ath10k_mac_set_frag(arvif, ar->hw->wiphy->frag_threshold);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002212 if (ret) {
Michal Kazior679c54a2013-07-05 16:15:04 +03002213 ath10k_warn("failed to set frag threshold for vdev %d (%d)\n",
2214 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002215 goto err_peer_delete;
2216 }
Michal Kazior679c54a2013-07-05 16:15:04 +03002217
Kalle Valo5e3dd152013-06-12 20:52:10 +03002218 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
2219 ar->monitor_present = true;
2220
Kalle Valo5e3dd152013-06-12 20:52:10 +03002221 mutex_unlock(&ar->conf_mutex);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002222 return 0;
2223
2224err_peer_delete:
2225 if (arvif->vdev_type == WMI_VDEV_TYPE_AP)
2226 ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
2227
2228err_vdev_delete:
2229 ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
2230 ar->free_vdev_map &= ~BIT(arvif->vdev_id);
Michal Kazior05791192013-10-16 15:44:45 +03002231 list_del(&arvif->list);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002232
2233err:
2234 mutex_unlock(&ar->conf_mutex);
2235
Kalle Valo5e3dd152013-06-12 20:52:10 +03002236 return ret;
2237}
2238
2239static void ath10k_remove_interface(struct ieee80211_hw *hw,
2240 struct ieee80211_vif *vif)
2241{
2242 struct ath10k *ar = hw->priv;
2243 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2244 int ret;
2245
2246 mutex_lock(&ar->conf_mutex);
2247
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002248 cancel_work_sync(&arvif->wep_key_work);
2249
Michal Kaziored543882013-09-13 14:16:56 +02002250 spin_lock_bh(&ar->data_lock);
2251 if (arvif->beacon) {
2252 dev_kfree_skb_any(arvif->beacon);
2253 arvif->beacon = NULL;
2254 }
2255 spin_unlock_bh(&ar->data_lock);
2256
Kalle Valo5e3dd152013-06-12 20:52:10 +03002257 ar->free_vdev_map |= 1 << (arvif->vdev_id);
Michal Kazior05791192013-10-16 15:44:45 +03002258 list_del(&arvif->list);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002259
2260 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
2261 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, vif->addr);
2262 if (ret)
2263 ath10k_warn("Failed to remove peer for AP: %d\n", ret);
2264
2265 kfree(arvif->u.ap.noa_data);
2266 }
2267
Kalle Valo60c3daa2013-09-08 17:56:07 +03002268 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev delete %d (remove interface)\n",
2269 arvif->vdev_id);
2270
Kalle Valo5e3dd152013-06-12 20:52:10 +03002271 ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
2272 if (ret)
2273 ath10k_warn("WMI vdev delete failed: %d\n", ret);
2274
2275 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
2276 ar->monitor_present = false;
2277
2278 ath10k_peer_cleanup(ar, arvif->vdev_id);
2279
2280 mutex_unlock(&ar->conf_mutex);
2281}
2282
2283/*
2284 * FIXME: Has to be verified.
2285 */
2286#define SUPPORTED_FILTERS \
2287 (FIF_PROMISC_IN_BSS | \
2288 FIF_ALLMULTI | \
2289 FIF_CONTROL | \
2290 FIF_PSPOLL | \
2291 FIF_OTHER_BSS | \
2292 FIF_BCN_PRBRESP_PROMISC | \
2293 FIF_PROBE_REQ | \
2294 FIF_FCSFAIL)
2295
2296static void ath10k_configure_filter(struct ieee80211_hw *hw,
2297 unsigned int changed_flags,
2298 unsigned int *total_flags,
2299 u64 multicast)
2300{
2301 struct ath10k *ar = hw->priv;
2302 int ret;
2303
2304 mutex_lock(&ar->conf_mutex);
2305
2306 changed_flags &= SUPPORTED_FILTERS;
2307 *total_flags &= SUPPORTED_FILTERS;
2308 ar->filter_flags = *total_flags;
2309
Michal Kaziorafd09222013-10-16 16:45:41 +03002310 /* Monitor must not be started if it wasn't created first.
2311 * Promiscuous mode may be started on a non-monitor interface - in
2312 * such case the monitor vdev is not created so starting the
2313 * monitor makes no sense. Since ath10k uses no special RX filters
2314 * (only BSS filter in STA mode) there's no need for any special
2315 * action here. */
Kalle Valo5e3dd152013-06-12 20:52:10 +03002316 if ((ar->filter_flags & FIF_PROMISC_IN_BSS) &&
Michal Kaziorafd09222013-10-16 16:45:41 +03002317 !ar->monitor_enabled && ar->monitor_present) {
Kalle Valo60c3daa2013-09-08 17:56:07 +03002318 ath10k_dbg(ATH10K_DBG_MAC, "mac monitor %d start\n",
2319 ar->monitor_vdev_id);
2320
Kalle Valo5e3dd152013-06-12 20:52:10 +03002321 ret = ath10k_monitor_start(ar, ar->monitor_vdev_id);
2322 if (ret)
2323 ath10k_warn("Unable to start monitor mode\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002324 } else if (!(ar->filter_flags & FIF_PROMISC_IN_BSS) &&
Michal Kaziorafd09222013-10-16 16:45:41 +03002325 ar->monitor_enabled && ar->monitor_present) {
Kalle Valo60c3daa2013-09-08 17:56:07 +03002326 ath10k_dbg(ATH10K_DBG_MAC, "mac monitor %d stop\n",
2327 ar->monitor_vdev_id);
2328
Kalle Valo5e3dd152013-06-12 20:52:10 +03002329 ret = ath10k_monitor_stop(ar);
2330 if (ret)
2331 ath10k_warn("Unable to stop monitor mode\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002332 }
2333
2334 mutex_unlock(&ar->conf_mutex);
2335}
2336
2337static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
2338 struct ieee80211_vif *vif,
2339 struct ieee80211_bss_conf *info,
2340 u32 changed)
2341{
2342 struct ath10k *ar = hw->priv;
2343 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2344 int ret = 0;
Bartosz Markowski226a3392013-09-26 17:47:16 +02002345 u32 vdev_param, pdev_param;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002346
2347 mutex_lock(&ar->conf_mutex);
2348
2349 if (changed & BSS_CHANGED_IBSS)
2350 ath10k_control_ibss(arvif, info, vif->addr);
2351
2352 if (changed & BSS_CHANGED_BEACON_INT) {
2353 arvif->beacon_interval = info->beacon_int;
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02002354 vdev_param = ar->wmi.vdev_param->beacon_interval;
2355 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002356 arvif->beacon_interval);
Kalle Valo60c3daa2013-09-08 17:56:07 +03002357 ath10k_dbg(ATH10K_DBG_MAC,
2358 "mac vdev %d beacon_interval %d\n",
2359 arvif->vdev_id, arvif->beacon_interval);
2360
Kalle Valo5e3dd152013-06-12 20:52:10 +03002361 if (ret)
2362 ath10k_warn("Failed to set beacon interval for VDEV: %d\n",
2363 arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002364 }
2365
2366 if (changed & BSS_CHANGED_BEACON) {
Kalle Valo60c3daa2013-09-08 17:56:07 +03002367 ath10k_dbg(ATH10K_DBG_MAC,
2368 "vdev %d set beacon tx mode to staggered\n",
2369 arvif->vdev_id);
2370
Bartosz Markowski226a3392013-09-26 17:47:16 +02002371 pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
2372 ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002373 WMI_BEACON_STAGGERED_MODE);
2374 if (ret)
2375 ath10k_warn("Failed to set beacon mode for VDEV: %d\n",
2376 arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002377 }
2378
John W. Linvilleb70727e2013-06-13 13:34:29 -04002379 if (changed & BSS_CHANGED_BEACON_INFO) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002380 arvif->dtim_period = info->dtim_period;
2381
Kalle Valo60c3daa2013-09-08 17:56:07 +03002382 ath10k_dbg(ATH10K_DBG_MAC,
2383 "mac vdev %d dtim_period %d\n",
2384 arvif->vdev_id, arvif->dtim_period);
2385
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02002386 vdev_param = ar->wmi.vdev_param->dtim_period;
2387 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002388 arvif->dtim_period);
2389 if (ret)
2390 ath10k_warn("Failed to set dtim period for VDEV: %d\n",
2391 arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002392 }
2393
2394 if (changed & BSS_CHANGED_SSID &&
2395 vif->type == NL80211_IFTYPE_AP) {
2396 arvif->u.ap.ssid_len = info->ssid_len;
2397 if (info->ssid_len)
2398 memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
2399 arvif->u.ap.hidden_ssid = info->hidden_ssid;
2400 }
2401
2402 if (changed & BSS_CHANGED_BSSID) {
2403 if (!is_zero_ether_addr(info->bssid)) {
Kalle Valo60c3daa2013-09-08 17:56:07 +03002404 ath10k_dbg(ATH10K_DBG_MAC,
2405 "mac vdev %d create peer %pM\n",
2406 arvif->vdev_id, info->bssid);
2407
Kalle Valo5e3dd152013-06-12 20:52:10 +03002408 ret = ath10k_peer_create(ar, arvif->vdev_id,
2409 info->bssid);
2410 if (ret)
Ben Greear479398b2013-11-04 09:19:34 -08002411 ath10k_warn("Failed to add peer %pM for vdev %d when changin bssid: %i\n",
2412 info->bssid, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002413
2414 if (vif->type == NL80211_IFTYPE_STATION) {
2415 /*
2416 * this is never erased as we it for crypto key
2417 * clearing; this is FW requirement
2418 */
2419 memcpy(arvif->u.sta.bssid, info->bssid,
2420 ETH_ALEN);
2421
Kalle Valo60c3daa2013-09-08 17:56:07 +03002422 ath10k_dbg(ATH10K_DBG_MAC,
2423 "mac vdev %d start %pM\n",
2424 arvif->vdev_id, info->bssid);
2425
2426 /* FIXME: check return value */
Kalle Valo5e3dd152013-06-12 20:52:10 +03002427 ret = ath10k_vdev_start(arvif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002428 }
2429
2430 /*
2431 * Mac80211 does not keep IBSS bssid when leaving IBSS,
2432 * so driver need to store it. It is needed when leaving
2433 * IBSS in order to remove BSSID peer.
2434 */
2435 if (vif->type == NL80211_IFTYPE_ADHOC)
2436 memcpy(arvif->u.ibss.bssid, info->bssid,
2437 ETH_ALEN);
2438 }
2439 }
2440
2441 if (changed & BSS_CHANGED_BEACON_ENABLED)
2442 ath10k_control_beaconing(arvif, info);
2443
2444 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
2445 u32 cts_prot;
2446 if (info->use_cts_prot)
2447 cts_prot = 1;
2448 else
2449 cts_prot = 0;
2450
Kalle Valo60c3daa2013-09-08 17:56:07 +03002451 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d cts_prot %d\n",
2452 arvif->vdev_id, cts_prot);
2453
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02002454 vdev_param = ar->wmi.vdev_param->enable_rtscts;
2455 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002456 cts_prot);
2457 if (ret)
2458 ath10k_warn("Failed to set CTS prot for VDEV: %d\n",
2459 arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002460 }
2461
2462 if (changed & BSS_CHANGED_ERP_SLOT) {
2463 u32 slottime;
2464 if (info->use_short_slot)
2465 slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
2466
2467 else
2468 slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
2469
Kalle Valo60c3daa2013-09-08 17:56:07 +03002470 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
2471 arvif->vdev_id, slottime);
2472
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02002473 vdev_param = ar->wmi.vdev_param->slot_time;
2474 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002475 slottime);
2476 if (ret)
2477 ath10k_warn("Failed to set erp slot for VDEV: %d\n",
2478 arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002479 }
2480
2481 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
2482 u32 preamble;
2483 if (info->use_short_preamble)
2484 preamble = WMI_VDEV_PREAMBLE_SHORT;
2485 else
2486 preamble = WMI_VDEV_PREAMBLE_LONG;
2487
Kalle Valo60c3daa2013-09-08 17:56:07 +03002488 ath10k_dbg(ATH10K_DBG_MAC,
2489 "mac vdev %d preamble %dn",
2490 arvif->vdev_id, preamble);
2491
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02002492 vdev_param = ar->wmi.vdev_param->preamble;
2493 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002494 preamble);
2495 if (ret)
2496 ath10k_warn("Failed to set preamble for VDEV: %d\n",
2497 arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002498 }
2499
2500 if (changed & BSS_CHANGED_ASSOC) {
2501 if (info->assoc)
2502 ath10k_bss_assoc(hw, vif, info);
2503 }
2504
2505 mutex_unlock(&ar->conf_mutex);
2506}
2507
2508static int ath10k_hw_scan(struct ieee80211_hw *hw,
2509 struct ieee80211_vif *vif,
2510 struct cfg80211_scan_request *req)
2511{
2512 struct ath10k *ar = hw->priv;
2513 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2514 struct wmi_start_scan_arg arg;
2515 int ret = 0;
2516 int i;
2517
2518 mutex_lock(&ar->conf_mutex);
2519
2520 spin_lock_bh(&ar->data_lock);
2521 if (ar->scan.in_progress) {
2522 spin_unlock_bh(&ar->data_lock);
2523 ret = -EBUSY;
2524 goto exit;
2525 }
2526
2527 INIT_COMPLETION(ar->scan.started);
2528 INIT_COMPLETION(ar->scan.completed);
2529 ar->scan.in_progress = true;
2530 ar->scan.aborting = false;
2531 ar->scan.is_roc = false;
2532 ar->scan.vdev_id = arvif->vdev_id;
2533 spin_unlock_bh(&ar->data_lock);
2534
2535 memset(&arg, 0, sizeof(arg));
2536 ath10k_wmi_start_scan_init(ar, &arg);
2537 arg.vdev_id = arvif->vdev_id;
2538 arg.scan_id = ATH10K_SCAN_ID;
2539
2540 if (!req->no_cck)
2541 arg.scan_ctrl_flags |= WMI_SCAN_ADD_CCK_RATES;
2542
2543 if (req->ie_len) {
2544 arg.ie_len = req->ie_len;
2545 memcpy(arg.ie, req->ie, arg.ie_len);
2546 }
2547
2548 if (req->n_ssids) {
2549 arg.n_ssids = req->n_ssids;
2550 for (i = 0; i < arg.n_ssids; i++) {
2551 arg.ssids[i].len = req->ssids[i].ssid_len;
2552 arg.ssids[i].ssid = req->ssids[i].ssid;
2553 }
Michal Kaziordcd4a562013-07-31 10:55:12 +02002554 } else {
2555 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002556 }
2557
2558 if (req->n_channels) {
2559 arg.n_channels = req->n_channels;
2560 for (i = 0; i < arg.n_channels; i++)
2561 arg.channels[i] = req->channels[i]->center_freq;
2562 }
2563
2564 ret = ath10k_start_scan(ar, &arg);
2565 if (ret) {
2566 ath10k_warn("could not start hw scan (%d)\n", ret);
2567 spin_lock_bh(&ar->data_lock);
2568 ar->scan.in_progress = false;
2569 spin_unlock_bh(&ar->data_lock);
2570 }
2571
2572exit:
2573 mutex_unlock(&ar->conf_mutex);
2574 return ret;
2575}
2576
2577static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
2578 struct ieee80211_vif *vif)
2579{
2580 struct ath10k *ar = hw->priv;
2581 int ret;
2582
2583 mutex_lock(&ar->conf_mutex);
2584 ret = ath10k_abort_scan(ar);
2585 if (ret) {
2586 ath10k_warn("couldn't abort scan (%d). forcefully sending scan completion to mac80211\n",
2587 ret);
2588 ieee80211_scan_completed(hw, 1 /* aborted */);
2589 }
2590 mutex_unlock(&ar->conf_mutex);
2591}
2592
2593static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
2594 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
2595 struct ieee80211_key_conf *key)
2596{
2597 struct ath10k *ar = hw->priv;
2598 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2599 struct ath10k_peer *peer;
2600 const u8 *peer_addr;
2601 bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
2602 key->cipher == WLAN_CIPHER_SUITE_WEP104;
2603 int ret = 0;
2604
2605 if (key->keyidx > WMI_MAX_KEY_INDEX)
2606 return -ENOSPC;
2607
2608 mutex_lock(&ar->conf_mutex);
2609
2610 if (sta)
2611 peer_addr = sta->addr;
2612 else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
2613 peer_addr = vif->bss_conf.bssid;
2614 else
2615 peer_addr = vif->addr;
2616
2617 key->hw_key_idx = key->keyidx;
2618
2619 /* the peer should not disappear in mid-way (unless FW goes awry) since
2620 * we already hold conf_mutex. we just make sure its there now. */
2621 spin_lock_bh(&ar->data_lock);
2622 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
2623 spin_unlock_bh(&ar->data_lock);
2624
2625 if (!peer) {
2626 if (cmd == SET_KEY) {
2627 ath10k_warn("cannot install key for non-existent peer %pM\n",
2628 peer_addr);
2629 ret = -EOPNOTSUPP;
2630 goto exit;
2631 } else {
2632 /* if the peer doesn't exist there is no key to disable
2633 * anymore */
2634 goto exit;
2635 }
2636 }
2637
2638 if (is_wep) {
2639 if (cmd == SET_KEY)
2640 arvif->wep_keys[key->keyidx] = key;
2641 else
2642 arvif->wep_keys[key->keyidx] = NULL;
2643
2644 if (cmd == DISABLE_KEY)
2645 ath10k_clear_vdev_key(arvif, key);
2646 }
2647
2648 ret = ath10k_install_key(arvif, key, cmd, peer_addr);
2649 if (ret) {
2650 ath10k_warn("ath10k_install_key failed (%d)\n", ret);
2651 goto exit;
2652 }
2653
2654 spin_lock_bh(&ar->data_lock);
2655 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
2656 if (peer && cmd == SET_KEY)
2657 peer->keys[key->keyidx] = key;
2658 else if (peer && cmd == DISABLE_KEY)
2659 peer->keys[key->keyidx] = NULL;
2660 else if (peer == NULL)
2661 /* impossible unless FW goes crazy */
2662 ath10k_warn("peer %pM disappeared!\n", peer_addr);
2663 spin_unlock_bh(&ar->data_lock);
2664
2665exit:
2666 mutex_unlock(&ar->conf_mutex);
2667 return ret;
2668}
2669
2670static int ath10k_sta_state(struct ieee80211_hw *hw,
2671 struct ieee80211_vif *vif,
2672 struct ieee80211_sta *sta,
2673 enum ieee80211_sta_state old_state,
2674 enum ieee80211_sta_state new_state)
2675{
2676 struct ath10k *ar = hw->priv;
2677 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2678 int ret = 0;
2679
2680 mutex_lock(&ar->conf_mutex);
2681
2682 if (old_state == IEEE80211_STA_NOTEXIST &&
2683 new_state == IEEE80211_STA_NONE &&
2684 vif->type != NL80211_IFTYPE_STATION) {
2685 /*
2686 * New station addition.
2687 */
Kalle Valo60c3daa2013-09-08 17:56:07 +03002688 ath10k_dbg(ATH10K_DBG_MAC,
2689 "mac vdev %d peer create %pM (new sta)\n",
2690 arvif->vdev_id, sta->addr);
2691
Kalle Valo5e3dd152013-06-12 20:52:10 +03002692 ret = ath10k_peer_create(ar, arvif->vdev_id, sta->addr);
2693 if (ret)
Ben Greear479398b2013-11-04 09:19:34 -08002694 ath10k_warn("Failed to add peer %pM for vdev %d when adding a new sta: %i\n",
2695 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002696 } else if ((old_state == IEEE80211_STA_NONE &&
2697 new_state == IEEE80211_STA_NOTEXIST)) {
2698 /*
2699 * Existing station deletion.
2700 */
Kalle Valo60c3daa2013-09-08 17:56:07 +03002701 ath10k_dbg(ATH10K_DBG_MAC,
2702 "mac vdev %d peer delete %pM (sta gone)\n",
2703 arvif->vdev_id, sta->addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002704 ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
2705 if (ret)
2706 ath10k_warn("Failed to delete peer: %pM for VDEV: %d\n",
2707 sta->addr, arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002708
2709 if (vif->type == NL80211_IFTYPE_STATION)
2710 ath10k_bss_disassoc(hw, vif);
2711 } else if (old_state == IEEE80211_STA_AUTH &&
2712 new_state == IEEE80211_STA_ASSOC &&
2713 (vif->type == NL80211_IFTYPE_AP ||
2714 vif->type == NL80211_IFTYPE_ADHOC)) {
2715 /*
2716 * New association.
2717 */
Kalle Valo60c3daa2013-09-08 17:56:07 +03002718 ath10k_dbg(ATH10K_DBG_MAC, "mac sta %pM associated\n",
2719 sta->addr);
2720
Kalle Valo5e3dd152013-06-12 20:52:10 +03002721 ret = ath10k_station_assoc(ar, arvif, sta);
2722 if (ret)
2723 ath10k_warn("Failed to associate station: %pM\n",
2724 sta->addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002725 } else if (old_state == IEEE80211_STA_ASSOC &&
2726 new_state == IEEE80211_STA_AUTH &&
2727 (vif->type == NL80211_IFTYPE_AP ||
2728 vif->type == NL80211_IFTYPE_ADHOC)) {
2729 /*
2730 * Disassociation.
2731 */
Kalle Valo60c3daa2013-09-08 17:56:07 +03002732 ath10k_dbg(ATH10K_DBG_MAC, "mac sta %pM disassociated\n",
2733 sta->addr);
2734
Kalle Valo5e3dd152013-06-12 20:52:10 +03002735 ret = ath10k_station_disassoc(ar, arvif, sta);
2736 if (ret)
2737 ath10k_warn("Failed to disassociate station: %pM\n",
2738 sta->addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002739 }
2740
2741 mutex_unlock(&ar->conf_mutex);
2742 return ret;
2743}
2744
2745static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
2746 u16 ac, bool enable)
2747{
2748 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2749 u32 value = 0;
2750 int ret = 0;
2751
Michal Kazior548db542013-07-05 16:15:15 +03002752 lockdep_assert_held(&ar->conf_mutex);
2753
Kalle Valo5e3dd152013-06-12 20:52:10 +03002754 if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
2755 return 0;
2756
2757 switch (ac) {
2758 case IEEE80211_AC_VO:
2759 value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
2760 WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
2761 break;
2762 case IEEE80211_AC_VI:
2763 value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
2764 WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
2765 break;
2766 case IEEE80211_AC_BE:
2767 value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
2768 WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
2769 break;
2770 case IEEE80211_AC_BK:
2771 value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
2772 WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
2773 break;
2774 }
2775
2776 if (enable)
2777 arvif->u.sta.uapsd |= value;
2778 else
2779 arvif->u.sta.uapsd &= ~value;
2780
2781 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
2782 WMI_STA_PS_PARAM_UAPSD,
2783 arvif->u.sta.uapsd);
2784 if (ret) {
2785 ath10k_warn("could not set uapsd params %d\n", ret);
2786 goto exit;
2787 }
2788
2789 if (arvif->u.sta.uapsd)
2790 value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
2791 else
2792 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
2793
2794 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
2795 WMI_STA_PS_PARAM_RX_WAKE_POLICY,
2796 value);
2797 if (ret)
2798 ath10k_warn("could not set rx wake param %d\n", ret);
2799
2800exit:
2801 return ret;
2802}
2803
2804static int ath10k_conf_tx(struct ieee80211_hw *hw,
2805 struct ieee80211_vif *vif, u16 ac,
2806 const struct ieee80211_tx_queue_params *params)
2807{
2808 struct ath10k *ar = hw->priv;
2809 struct wmi_wmm_params_arg *p = NULL;
2810 int ret;
2811
2812 mutex_lock(&ar->conf_mutex);
2813
2814 switch (ac) {
2815 case IEEE80211_AC_VO:
2816 p = &ar->wmm_params.ac_vo;
2817 break;
2818 case IEEE80211_AC_VI:
2819 p = &ar->wmm_params.ac_vi;
2820 break;
2821 case IEEE80211_AC_BE:
2822 p = &ar->wmm_params.ac_be;
2823 break;
2824 case IEEE80211_AC_BK:
2825 p = &ar->wmm_params.ac_bk;
2826 break;
2827 }
2828
2829 if (WARN_ON(!p)) {
2830 ret = -EINVAL;
2831 goto exit;
2832 }
2833
2834 p->cwmin = params->cw_min;
2835 p->cwmax = params->cw_max;
2836 p->aifs = params->aifs;
2837
2838 /*
2839 * The channel time duration programmed in the HW is in absolute
2840 * microseconds, while mac80211 gives the txop in units of
2841 * 32 microseconds.
2842 */
2843 p->txop = params->txop * 32;
2844
2845 /* FIXME: FW accepts wmm params per hw, not per vif */
2846 ret = ath10k_wmi_pdev_set_wmm_params(ar, &ar->wmm_params);
2847 if (ret) {
2848 ath10k_warn("could not set wmm params %d\n", ret);
2849 goto exit;
2850 }
2851
2852 ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
2853 if (ret)
2854 ath10k_warn("could not set sta uapsd %d\n", ret);
2855
2856exit:
2857 mutex_unlock(&ar->conf_mutex);
2858 return ret;
2859}
2860
2861#define ATH10K_ROC_TIMEOUT_HZ (2*HZ)
2862
2863static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
2864 struct ieee80211_vif *vif,
2865 struct ieee80211_channel *chan,
2866 int duration,
2867 enum ieee80211_roc_type type)
2868{
2869 struct ath10k *ar = hw->priv;
2870 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2871 struct wmi_start_scan_arg arg;
2872 int ret;
2873
2874 mutex_lock(&ar->conf_mutex);
2875
2876 spin_lock_bh(&ar->data_lock);
2877 if (ar->scan.in_progress) {
2878 spin_unlock_bh(&ar->data_lock);
2879 ret = -EBUSY;
2880 goto exit;
2881 }
2882
2883 INIT_COMPLETION(ar->scan.started);
2884 INIT_COMPLETION(ar->scan.completed);
2885 INIT_COMPLETION(ar->scan.on_channel);
2886 ar->scan.in_progress = true;
2887 ar->scan.aborting = false;
2888 ar->scan.is_roc = true;
2889 ar->scan.vdev_id = arvif->vdev_id;
2890 ar->scan.roc_freq = chan->center_freq;
2891 spin_unlock_bh(&ar->data_lock);
2892
2893 memset(&arg, 0, sizeof(arg));
2894 ath10k_wmi_start_scan_init(ar, &arg);
2895 arg.vdev_id = arvif->vdev_id;
2896 arg.scan_id = ATH10K_SCAN_ID;
2897 arg.n_channels = 1;
2898 arg.channels[0] = chan->center_freq;
2899 arg.dwell_time_active = duration;
2900 arg.dwell_time_passive = duration;
2901 arg.max_scan_time = 2 * duration;
2902 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
2903 arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
2904
2905 ret = ath10k_start_scan(ar, &arg);
2906 if (ret) {
2907 ath10k_warn("could not start roc scan (%d)\n", ret);
2908 spin_lock_bh(&ar->data_lock);
2909 ar->scan.in_progress = false;
2910 spin_unlock_bh(&ar->data_lock);
2911 goto exit;
2912 }
2913
2914 ret = wait_for_completion_timeout(&ar->scan.on_channel, 3*HZ);
2915 if (ret == 0) {
2916 ath10k_warn("could not switch to channel for roc scan\n");
2917 ath10k_abort_scan(ar);
2918 ret = -ETIMEDOUT;
2919 goto exit;
2920 }
2921
2922 ret = 0;
2923exit:
2924 mutex_unlock(&ar->conf_mutex);
2925 return ret;
2926}
2927
2928static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw)
2929{
2930 struct ath10k *ar = hw->priv;
2931
2932 mutex_lock(&ar->conf_mutex);
2933 ath10k_abort_scan(ar);
2934 mutex_unlock(&ar->conf_mutex);
2935
2936 return 0;
2937}
2938
2939/*
2940 * Both RTS and Fragmentation threshold are interface-specific
2941 * in ath10k, but device-specific in mac80211.
2942 */
Kalle Valo5e3dd152013-06-12 20:52:10 +03002943
2944static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
2945{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002946 struct ath10k *ar = hw->priv;
Michal Kaziorad088bf2013-10-16 15:44:46 +03002947 struct ath10k_vif *arvif;
2948 int ret = 0;
Michal Kazior548db542013-07-05 16:15:15 +03002949
Michal Kaziorad088bf2013-10-16 15:44:46 +03002950 mutex_lock(&ar->conf_mutex);
2951 list_for_each_entry(arvif, &ar->arvifs, list) {
2952 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
2953 arvif->vdev_id, value);
Kalle Valo60c3daa2013-09-08 17:56:07 +03002954
Michal Kaziorad088bf2013-10-16 15:44:46 +03002955 ret = ath10k_mac_set_rts(arvif, value);
2956 if (ret) {
2957 ath10k_warn("could not set rts threshold for vdev %d (%d)\n",
2958 arvif->vdev_id, ret);
2959 break;
2960 }
2961 }
2962 mutex_unlock(&ar->conf_mutex);
2963
2964 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002965}
2966
2967static int ath10k_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
2968{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002969 struct ath10k *ar = hw->priv;
Michal Kaziorad088bf2013-10-16 15:44:46 +03002970 struct ath10k_vif *arvif;
2971 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002972
Kalle Valo5e3dd152013-06-12 20:52:10 +03002973 mutex_lock(&ar->conf_mutex);
Michal Kaziorad088bf2013-10-16 15:44:46 +03002974 list_for_each_entry(arvif, &ar->arvifs, list) {
2975 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d fragmentation threshold %d\n",
2976 arvif->vdev_id, value);
2977
2978 ret = ath10k_mac_set_rts(arvif, value);
2979 if (ret) {
2980 ath10k_warn("could not set fragmentation threshold for vdev %d (%d)\n",
2981 arvif->vdev_id, ret);
2982 break;
2983 }
2984 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002985 mutex_unlock(&ar->conf_mutex);
2986
Michal Kaziorad088bf2013-10-16 15:44:46 +03002987 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002988}
2989
2990static void ath10k_flush(struct ieee80211_hw *hw, u32 queues, bool drop)
2991{
2992 struct ath10k *ar = hw->priv;
Michal Kazioraffd3212013-07-16 09:54:35 +02002993 bool skip;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002994 int ret;
2995
2996 /* mac80211 doesn't care if we really xmit queued frames or not
2997 * we'll collect those frames either way if we stop/delete vdevs */
2998 if (drop)
2999 return;
3000
Michal Kazior548db542013-07-05 16:15:15 +03003001 mutex_lock(&ar->conf_mutex);
3002
Michal Kazioraffd3212013-07-16 09:54:35 +02003003 if (ar->state == ATH10K_STATE_WEDGED)
3004 goto skip;
3005
Michal Kazioredb82362013-07-05 16:15:14 +03003006 ret = wait_event_timeout(ar->htt.empty_tx_wq, ({
Kalle Valo5e3dd152013-06-12 20:52:10 +03003007 bool empty;
Michal Kazioraffd3212013-07-16 09:54:35 +02003008
Michal Kazioredb82362013-07-05 16:15:14 +03003009 spin_lock_bh(&ar->htt.tx_lock);
Michal Kazior0945baf2013-09-18 14:43:18 +02003010 empty = (ar->htt.num_pending_tx == 0);
Michal Kazioredb82362013-07-05 16:15:14 +03003011 spin_unlock_bh(&ar->htt.tx_lock);
Michal Kazioraffd3212013-07-16 09:54:35 +02003012
3013 skip = (ar->state == ATH10K_STATE_WEDGED);
3014
3015 (empty || skip);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003016 }), ATH10K_FLUSH_TIMEOUT_HZ);
Michal Kazioraffd3212013-07-16 09:54:35 +02003017
3018 if (ret <= 0 || skip)
Kalle Valo5e3dd152013-06-12 20:52:10 +03003019 ath10k_warn("tx not flushed\n");
Michal Kazior548db542013-07-05 16:15:15 +03003020
Michal Kazioraffd3212013-07-16 09:54:35 +02003021skip:
Michal Kazior548db542013-07-05 16:15:15 +03003022 mutex_unlock(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003023}
3024
3025/* TODO: Implement this function properly
3026 * For now it is needed to reply to Probe Requests in IBSS mode.
3027 * Propably we need this information from FW.
3028 */
3029static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
3030{
3031 return 1;
3032}
3033
Michal Kazior8cd13ca2013-07-16 09:38:54 +02003034#ifdef CONFIG_PM
3035static int ath10k_suspend(struct ieee80211_hw *hw,
3036 struct cfg80211_wowlan *wowlan)
3037{
3038 struct ath10k *ar = hw->priv;
3039 int ret;
3040
3041 ar->is_target_paused = false;
3042
3043 ret = ath10k_wmi_pdev_suspend_target(ar);
3044 if (ret) {
3045 ath10k_warn("could not suspend target (%d)\n", ret);
3046 return 1;
3047 }
3048
3049 ret = wait_event_interruptible_timeout(ar->event_queue,
3050 ar->is_target_paused == true,
3051 1 * HZ);
3052 if (ret < 0) {
3053 ath10k_warn("suspend interrupted (%d)\n", ret);
3054 goto resume;
3055 } else if (ret == 0) {
3056 ath10k_warn("suspend timed out - target pause event never came\n");
3057 goto resume;
3058 }
3059
3060 ret = ath10k_hif_suspend(ar);
3061 if (ret) {
3062 ath10k_warn("could not suspend hif (%d)\n", ret);
3063 goto resume;
3064 }
3065
3066 return 0;
3067resume:
3068 ret = ath10k_wmi_pdev_resume_target(ar);
3069 if (ret)
3070 ath10k_warn("could not resume target (%d)\n", ret);
3071 return 1;
3072}
3073
3074static int ath10k_resume(struct ieee80211_hw *hw)
3075{
3076 struct ath10k *ar = hw->priv;
3077 int ret;
3078
3079 ret = ath10k_hif_resume(ar);
3080 if (ret) {
3081 ath10k_warn("could not resume hif (%d)\n", ret);
3082 return 1;
3083 }
3084
3085 ret = ath10k_wmi_pdev_resume_target(ar);
3086 if (ret) {
3087 ath10k_warn("could not resume target (%d)\n", ret);
3088 return 1;
3089 }
3090
3091 return 0;
3092}
3093#endif
3094
Michal Kazioraffd3212013-07-16 09:54:35 +02003095static void ath10k_restart_complete(struct ieee80211_hw *hw)
3096{
3097 struct ath10k *ar = hw->priv;
3098
3099 mutex_lock(&ar->conf_mutex);
3100
3101 /* If device failed to restart it will be in a different state, e.g.
3102 * ATH10K_STATE_WEDGED */
3103 if (ar->state == ATH10K_STATE_RESTARTED) {
3104 ath10k_info("device successfully recovered\n");
3105 ar->state = ATH10K_STATE_ON;
3106 }
3107
3108 mutex_unlock(&ar->conf_mutex);
3109}
3110
Michal Kazior2e1dea42013-07-31 10:32:40 +02003111static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
3112 struct survey_info *survey)
3113{
3114 struct ath10k *ar = hw->priv;
3115 struct ieee80211_supported_band *sband;
3116 struct survey_info *ar_survey = &ar->survey[idx];
3117 int ret = 0;
3118
3119 mutex_lock(&ar->conf_mutex);
3120
3121 sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
3122 if (sband && idx >= sband->n_channels) {
3123 idx -= sband->n_channels;
3124 sband = NULL;
3125 }
3126
3127 if (!sband)
3128 sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
3129
3130 if (!sband || idx >= sband->n_channels) {
3131 ret = -ENOENT;
3132 goto exit;
3133 }
3134
3135 spin_lock_bh(&ar->data_lock);
3136 memcpy(survey, ar_survey, sizeof(*survey));
3137 spin_unlock_bh(&ar->data_lock);
3138
3139 survey->channel = &sband->channels[idx];
3140
3141exit:
3142 mutex_unlock(&ar->conf_mutex);
3143 return ret;
3144}
3145
Kalle Valo5e3dd152013-06-12 20:52:10 +03003146static const struct ieee80211_ops ath10k_ops = {
3147 .tx = ath10k_tx,
3148 .start = ath10k_start,
3149 .stop = ath10k_stop,
3150 .config = ath10k_config,
3151 .add_interface = ath10k_add_interface,
3152 .remove_interface = ath10k_remove_interface,
3153 .configure_filter = ath10k_configure_filter,
3154 .bss_info_changed = ath10k_bss_info_changed,
3155 .hw_scan = ath10k_hw_scan,
3156 .cancel_hw_scan = ath10k_cancel_hw_scan,
3157 .set_key = ath10k_set_key,
3158 .sta_state = ath10k_sta_state,
3159 .conf_tx = ath10k_conf_tx,
3160 .remain_on_channel = ath10k_remain_on_channel,
3161 .cancel_remain_on_channel = ath10k_cancel_remain_on_channel,
3162 .set_rts_threshold = ath10k_set_rts_threshold,
3163 .set_frag_threshold = ath10k_set_frag_threshold,
3164 .flush = ath10k_flush,
3165 .tx_last_beacon = ath10k_tx_last_beacon,
Michal Kazioraffd3212013-07-16 09:54:35 +02003166 .restart_complete = ath10k_restart_complete,
Michal Kazior2e1dea42013-07-31 10:32:40 +02003167 .get_survey = ath10k_get_survey,
Michal Kazior8cd13ca2013-07-16 09:38:54 +02003168#ifdef CONFIG_PM
3169 .suspend = ath10k_suspend,
3170 .resume = ath10k_resume,
3171#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03003172};
3173
3174#define RATETAB_ENT(_rate, _rateid, _flags) { \
3175 .bitrate = (_rate), \
3176 .flags = (_flags), \
3177 .hw_value = (_rateid), \
3178}
3179
3180#define CHAN2G(_channel, _freq, _flags) { \
3181 .band = IEEE80211_BAND_2GHZ, \
3182 .hw_value = (_channel), \
3183 .center_freq = (_freq), \
3184 .flags = (_flags), \
3185 .max_antenna_gain = 0, \
3186 .max_power = 30, \
3187}
3188
3189#define CHAN5G(_channel, _freq, _flags) { \
3190 .band = IEEE80211_BAND_5GHZ, \
3191 .hw_value = (_channel), \
3192 .center_freq = (_freq), \
3193 .flags = (_flags), \
3194 .max_antenna_gain = 0, \
3195 .max_power = 30, \
3196}
3197
3198static const struct ieee80211_channel ath10k_2ghz_channels[] = {
3199 CHAN2G(1, 2412, 0),
3200 CHAN2G(2, 2417, 0),
3201 CHAN2G(3, 2422, 0),
3202 CHAN2G(4, 2427, 0),
3203 CHAN2G(5, 2432, 0),
3204 CHAN2G(6, 2437, 0),
3205 CHAN2G(7, 2442, 0),
3206 CHAN2G(8, 2447, 0),
3207 CHAN2G(9, 2452, 0),
3208 CHAN2G(10, 2457, 0),
3209 CHAN2G(11, 2462, 0),
3210 CHAN2G(12, 2467, 0),
3211 CHAN2G(13, 2472, 0),
3212 CHAN2G(14, 2484, 0),
3213};
3214
3215static const struct ieee80211_channel ath10k_5ghz_channels[] = {
Michal Kazior429ff562013-06-26 08:54:54 +02003216 CHAN5G(36, 5180, 0),
3217 CHAN5G(40, 5200, 0),
3218 CHAN5G(44, 5220, 0),
3219 CHAN5G(48, 5240, 0),
3220 CHAN5G(52, 5260, 0),
3221 CHAN5G(56, 5280, 0),
3222 CHAN5G(60, 5300, 0),
3223 CHAN5G(64, 5320, 0),
3224 CHAN5G(100, 5500, 0),
3225 CHAN5G(104, 5520, 0),
3226 CHAN5G(108, 5540, 0),
3227 CHAN5G(112, 5560, 0),
3228 CHAN5G(116, 5580, 0),
3229 CHAN5G(120, 5600, 0),
3230 CHAN5G(124, 5620, 0),
3231 CHAN5G(128, 5640, 0),
3232 CHAN5G(132, 5660, 0),
3233 CHAN5G(136, 5680, 0),
3234 CHAN5G(140, 5700, 0),
3235 CHAN5G(149, 5745, 0),
3236 CHAN5G(153, 5765, 0),
3237 CHAN5G(157, 5785, 0),
3238 CHAN5G(161, 5805, 0),
3239 CHAN5G(165, 5825, 0),
Kalle Valo5e3dd152013-06-12 20:52:10 +03003240};
3241
3242static struct ieee80211_rate ath10k_rates[] = {
3243 /* CCK */
3244 RATETAB_ENT(10, 0x82, 0),
3245 RATETAB_ENT(20, 0x84, 0),
3246 RATETAB_ENT(55, 0x8b, 0),
3247 RATETAB_ENT(110, 0x96, 0),
3248 /* OFDM */
3249 RATETAB_ENT(60, 0x0c, 0),
3250 RATETAB_ENT(90, 0x12, 0),
3251 RATETAB_ENT(120, 0x18, 0),
3252 RATETAB_ENT(180, 0x24, 0),
3253 RATETAB_ENT(240, 0x30, 0),
3254 RATETAB_ENT(360, 0x48, 0),
3255 RATETAB_ENT(480, 0x60, 0),
3256 RATETAB_ENT(540, 0x6c, 0),
3257};
3258
3259#define ath10k_a_rates (ath10k_rates + 4)
3260#define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - 4)
3261#define ath10k_g_rates (ath10k_rates + 0)
3262#define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
3263
3264struct ath10k *ath10k_mac_create(void)
3265{
3266 struct ieee80211_hw *hw;
3267 struct ath10k *ar;
3268
3269 hw = ieee80211_alloc_hw(sizeof(struct ath10k), &ath10k_ops);
3270 if (!hw)
3271 return NULL;
3272
3273 ar = hw->priv;
3274 ar->hw = hw;
3275
3276 return ar;
3277}
3278
3279void ath10k_mac_destroy(struct ath10k *ar)
3280{
3281 ieee80211_free_hw(ar->hw);
3282}
3283
3284static const struct ieee80211_iface_limit ath10k_if_limits[] = {
3285 {
3286 .max = 8,
3287 .types = BIT(NL80211_IFTYPE_STATION)
3288 | BIT(NL80211_IFTYPE_P2P_CLIENT)
Michal Kaziord531cb82013-07-31 10:55:13 +02003289 },
3290 {
3291 .max = 3,
3292 .types = BIT(NL80211_IFTYPE_P2P_GO)
3293 },
3294 {
3295 .max = 7,
3296 .types = BIT(NL80211_IFTYPE_AP)
3297 },
Kalle Valo5e3dd152013-06-12 20:52:10 +03003298};
3299
3300static const struct ieee80211_iface_combination ath10k_if_comb = {
3301 .limits = ath10k_if_limits,
3302 .n_limits = ARRAY_SIZE(ath10k_if_limits),
3303 .max_interfaces = 8,
3304 .num_different_channels = 1,
3305 .beacon_int_infra_match = true,
3306};
3307
3308static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
3309{
3310 struct ieee80211_sta_vht_cap vht_cap = {0};
3311 u16 mcs_map;
Michal Kazior8865bee42013-07-24 12:36:46 +02003312 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003313
3314 vht_cap.vht_supported = 1;
3315 vht_cap.cap = ar->vht_cap_info;
3316
Michal Kazior8865bee42013-07-24 12:36:46 +02003317 mcs_map = 0;
3318 for (i = 0; i < 8; i++) {
3319 if (i < ar->num_rf_chains)
3320 mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i*2);
3321 else
3322 mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i*2);
3323 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003324
3325 vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
3326 vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
3327
3328 return vht_cap;
3329}
3330
3331static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
3332{
3333 int i;
3334 struct ieee80211_sta_ht_cap ht_cap = {0};
3335
3336 if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
3337 return ht_cap;
3338
3339 ht_cap.ht_supported = 1;
3340 ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
3341 ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
3342 ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
3343 ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
3344 ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
3345
3346 if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
3347 ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
3348
3349 if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
3350 ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
3351
3352 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
3353 u32 smps;
3354
3355 smps = WLAN_HT_CAP_SM_PS_DYNAMIC;
3356 smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
3357
3358 ht_cap.cap |= smps;
3359 }
3360
3361 if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC)
3362 ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
3363
3364 if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
3365 u32 stbc;
3366
3367 stbc = ar->ht_cap_info;
3368 stbc &= WMI_HT_CAP_RX_STBC;
3369 stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
3370 stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
3371 stbc &= IEEE80211_HT_CAP_RX_STBC;
3372
3373 ht_cap.cap |= stbc;
3374 }
3375
3376 if (ar->ht_cap_info & WMI_HT_CAP_LDPC)
3377 ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
3378
3379 if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
3380 ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
3381
3382 /* max AMSDU is implicitly taken from vht_cap_info */
3383 if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
3384 ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
3385
Michal Kazior8865bee42013-07-24 12:36:46 +02003386 for (i = 0; i < ar->num_rf_chains; i++)
Kalle Valo5e3dd152013-06-12 20:52:10 +03003387 ht_cap.mcs.rx_mask[i] = 0xFF;
3388
3389 ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
3390
3391 return ht_cap;
3392}
3393
3394
3395static void ath10k_get_arvif_iter(void *data, u8 *mac,
3396 struct ieee80211_vif *vif)
3397{
3398 struct ath10k_vif_iter *arvif_iter = data;
3399 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3400
3401 if (arvif->vdev_id == arvif_iter->vdev_id)
3402 arvif_iter->arvif = arvif;
3403}
3404
3405struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
3406{
3407 struct ath10k_vif_iter arvif_iter;
3408 u32 flags;
3409
3410 memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
3411 arvif_iter.vdev_id = vdev_id;
3412
3413 flags = IEEE80211_IFACE_ITER_RESUME_ALL;
3414 ieee80211_iterate_active_interfaces_atomic(ar->hw,
3415 flags,
3416 ath10k_get_arvif_iter,
3417 &arvif_iter);
3418 if (!arvif_iter.arvif) {
3419 ath10k_warn("No VIF found for VDEV: %d\n", vdev_id);
3420 return NULL;
3421 }
3422
3423 return arvif_iter.arvif;
3424}
3425
3426int ath10k_mac_register(struct ath10k *ar)
3427{
3428 struct ieee80211_supported_band *band;
3429 struct ieee80211_sta_vht_cap vht_cap;
3430 struct ieee80211_sta_ht_cap ht_cap;
3431 void *channels;
3432 int ret;
3433
3434 SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
3435
3436 SET_IEEE80211_DEV(ar->hw, ar->dev);
3437
3438 ht_cap = ath10k_get_ht_cap(ar);
3439 vht_cap = ath10k_create_vht_cap(ar);
3440
3441 if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
3442 channels = kmemdup(ath10k_2ghz_channels,
3443 sizeof(ath10k_2ghz_channels),
3444 GFP_KERNEL);
Michal Kaziord6015b22013-07-22 14:13:30 +02003445 if (!channels) {
3446 ret = -ENOMEM;
3447 goto err_free;
3448 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003449
3450 band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
3451 band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
3452 band->channels = channels;
3453 band->n_bitrates = ath10k_g_rates_size;
3454 band->bitrates = ath10k_g_rates;
3455 band->ht_cap = ht_cap;
3456
3457 /* vht is not supported in 2.4 GHz */
3458
3459 ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
3460 }
3461
3462 if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
3463 channels = kmemdup(ath10k_5ghz_channels,
3464 sizeof(ath10k_5ghz_channels),
3465 GFP_KERNEL);
3466 if (!channels) {
Michal Kaziord6015b22013-07-22 14:13:30 +02003467 ret = -ENOMEM;
3468 goto err_free;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003469 }
3470
3471 band = &ar->mac.sbands[IEEE80211_BAND_5GHZ];
3472 band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
3473 band->channels = channels;
3474 band->n_bitrates = ath10k_a_rates_size;
3475 band->bitrates = ath10k_a_rates;
3476 band->ht_cap = ht_cap;
3477 band->vht_cap = vht_cap;
3478 ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
3479 }
3480
3481 ar->hw->wiphy->interface_modes =
3482 BIT(NL80211_IFTYPE_STATION) |
3483 BIT(NL80211_IFTYPE_ADHOC) |
3484 BIT(NL80211_IFTYPE_AP) |
3485 BIT(NL80211_IFTYPE_P2P_CLIENT) |
3486 BIT(NL80211_IFTYPE_P2P_GO);
3487
3488 ar->hw->flags = IEEE80211_HW_SIGNAL_DBM |
3489 IEEE80211_HW_SUPPORTS_PS |
3490 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
3491 IEEE80211_HW_SUPPORTS_UAPSD |
3492 IEEE80211_HW_MFP_CAPABLE |
3493 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
3494 IEEE80211_HW_HAS_RATE_CONTROL |
3495 IEEE80211_HW_SUPPORTS_STATIC_SMPS |
3496 IEEE80211_HW_WANT_MONITOR_VIF |
3497 IEEE80211_HW_AP_LINK_PS;
3498
Michal Kazior1f8bb152013-09-18 14:43:22 +02003499 /* MSDU can have HTT TX fragment pushed in front. The additional 4
3500 * bytes is used for padding/alignment if necessary. */
3501 ar->hw->extra_tx_headroom += sizeof(struct htt_data_tx_desc_frag)*2 + 4;
3502
Kalle Valo5e3dd152013-06-12 20:52:10 +03003503 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
3504 ar->hw->flags |= IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS;
3505
3506 if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
3507 ar->hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
3508 ar->hw->flags |= IEEE80211_HW_TX_AMPDU_SETUP_IN_HW;
3509 }
3510
3511 ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
3512 ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
3513
3514 ar->hw->vif_data_size = sizeof(struct ath10k_vif);
3515
3516 ar->hw->channel_change_time = 5000;
3517 ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
3518
3519 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
3520 ar->hw->wiphy->max_remain_on_channel_duration = 5000;
3521
3522 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
3523 /*
3524 * on LL hardware queues are managed entirely by the FW
3525 * so we only advertise to mac we can do the queues thing
3526 */
3527 ar->hw->queues = 4;
3528
3529 ar->hw->wiphy->iface_combinations = &ath10k_if_comb;
3530 ar->hw->wiphy->n_iface_combinations = 1;
3531
Michal Kazior7c199992013-07-31 10:47:57 +02003532 ar->hw->netdev_features = NETIF_F_HW_CSUM;
3533
Kalle Valo5e3dd152013-06-12 20:52:10 +03003534 ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
3535 ath10k_reg_notifier);
3536 if (ret) {
3537 ath10k_err("Regulatory initialization failed\n");
Michal Kaziord6015b22013-07-22 14:13:30 +02003538 goto err_free;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003539 }
3540
3541 ret = ieee80211_register_hw(ar->hw);
3542 if (ret) {
3543 ath10k_err("ieee80211 registration failed: %d\n", ret);
Michal Kaziord6015b22013-07-22 14:13:30 +02003544 goto err_free;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003545 }
3546
3547 if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
3548 ret = regulatory_hint(ar->hw->wiphy,
3549 ar->ath_common.regulatory.alpha2);
3550 if (ret)
Michal Kaziord6015b22013-07-22 14:13:30 +02003551 goto err_unregister;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003552 }
3553
3554 return 0;
Michal Kaziord6015b22013-07-22 14:13:30 +02003555
3556err_unregister:
Kalle Valo5e3dd152013-06-12 20:52:10 +03003557 ieee80211_unregister_hw(ar->hw);
Michal Kaziord6015b22013-07-22 14:13:30 +02003558err_free:
3559 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
3560 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
3561
Kalle Valo5e3dd152013-06-12 20:52:10 +03003562 return ret;
3563}
3564
3565void ath10k_mac_unregister(struct ath10k *ar)
3566{
3567 ieee80211_unregister_hw(ar->hw);
3568
3569 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
3570 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
3571
3572 SET_IEEE80211_DEV(ar->hw, NULL);
3573}