blob: 58ec5a7dfb3415bca97d40cc5df1f15c5f1619fc [file] [log] [blame]
Kalle Valo5e3dd152013-06-12 20:52:10 +03001/*
2 * Copyright (c) 2005-2011 Atheros Communications Inc.
3 * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include "mac.h"
19
20#include <net/mac80211.h>
21#include <linux/etherdevice.h>
22
Michal Kazior8cd13ca2013-07-16 09:38:54 +020023#include "hif.h"
Kalle Valo5e3dd152013-06-12 20:52:10 +030024#include "core.h"
25#include "debug.h"
26#include "wmi.h"
27#include "htt.h"
28#include "txrx.h"
29
30/**********/
31/* Crypto */
32/**********/
33
34static int ath10k_send_key(struct ath10k_vif *arvif,
35 struct ieee80211_key_conf *key,
36 enum set_key_cmd cmd,
37 const u8 *macaddr)
38{
39 struct wmi_vdev_install_key_arg arg = {
40 .vdev_id = arvif->vdev_id,
41 .key_idx = key->keyidx,
42 .key_len = key->keylen,
43 .key_data = key->key,
44 .macaddr = macaddr,
45 };
46
Michal Kazior548db542013-07-05 16:15:15 +030047 lockdep_assert_held(&arvif->ar->conf_mutex);
48
Kalle Valo5e3dd152013-06-12 20:52:10 +030049 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
50 arg.key_flags = WMI_KEY_PAIRWISE;
51 else
52 arg.key_flags = WMI_KEY_GROUP;
53
54 switch (key->cipher) {
55 case WLAN_CIPHER_SUITE_CCMP:
56 arg.key_cipher = WMI_CIPHER_AES_CCM;
57 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
58 break;
59 case WLAN_CIPHER_SUITE_TKIP:
Kalle Valo5e3dd152013-06-12 20:52:10 +030060 arg.key_cipher = WMI_CIPHER_TKIP;
61 arg.key_txmic_len = 8;
62 arg.key_rxmic_len = 8;
63 break;
64 case WLAN_CIPHER_SUITE_WEP40:
65 case WLAN_CIPHER_SUITE_WEP104:
66 arg.key_cipher = WMI_CIPHER_WEP;
67 /* AP/IBSS mode requires self-key to be groupwise
68 * Otherwise pairwise key must be set */
69 if (memcmp(macaddr, arvif->vif->addr, ETH_ALEN))
70 arg.key_flags = WMI_KEY_PAIRWISE;
71 break;
72 default:
73 ath10k_warn("cipher %d is not supported\n", key->cipher);
74 return -EOPNOTSUPP;
75 }
76
77 if (cmd == DISABLE_KEY) {
78 arg.key_cipher = WMI_CIPHER_NONE;
79 arg.key_data = NULL;
80 }
81
82 return ath10k_wmi_vdev_install_key(arvif->ar, &arg);
83}
84
85static int ath10k_install_key(struct ath10k_vif *arvif,
86 struct ieee80211_key_conf *key,
87 enum set_key_cmd cmd,
88 const u8 *macaddr)
89{
90 struct ath10k *ar = arvif->ar;
91 int ret;
92
Michal Kazior548db542013-07-05 16:15:15 +030093 lockdep_assert_held(&ar->conf_mutex);
94
Wolfram Sang16735d02013-11-14 14:32:02 -080095 reinit_completion(&ar->install_key_done);
Kalle Valo5e3dd152013-06-12 20:52:10 +030096
97 ret = ath10k_send_key(arvif, key, cmd, macaddr);
98 if (ret)
99 return ret;
100
101 ret = wait_for_completion_timeout(&ar->install_key_done, 3*HZ);
102 if (ret == 0)
103 return -ETIMEDOUT;
104
105 return 0;
106}
107
108static int ath10k_install_peer_wep_keys(struct ath10k_vif *arvif,
109 const u8 *addr)
110{
111 struct ath10k *ar = arvif->ar;
112 struct ath10k_peer *peer;
113 int ret;
114 int i;
115
116 lockdep_assert_held(&ar->conf_mutex);
117
118 spin_lock_bh(&ar->data_lock);
119 peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
120 spin_unlock_bh(&ar->data_lock);
121
122 if (!peer)
123 return -ENOENT;
124
125 for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
126 if (arvif->wep_keys[i] == NULL)
127 continue;
128
129 ret = ath10k_install_key(arvif, arvif->wep_keys[i], SET_KEY,
130 addr);
131 if (ret)
132 return ret;
133
134 peer->keys[i] = arvif->wep_keys[i];
135 }
136
137 return 0;
138}
139
140static int ath10k_clear_peer_keys(struct ath10k_vif *arvif,
141 const u8 *addr)
142{
143 struct ath10k *ar = arvif->ar;
144 struct ath10k_peer *peer;
145 int first_errno = 0;
146 int ret;
147 int i;
148
149 lockdep_assert_held(&ar->conf_mutex);
150
151 spin_lock_bh(&ar->data_lock);
152 peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
153 spin_unlock_bh(&ar->data_lock);
154
155 if (!peer)
156 return -ENOENT;
157
158 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
159 if (peer->keys[i] == NULL)
160 continue;
161
162 ret = ath10k_install_key(arvif, peer->keys[i],
163 DISABLE_KEY, addr);
164 if (ret && first_errno == 0)
165 first_errno = ret;
166
167 if (ret)
Kalle Valobe6546f2014-03-25 14:18:51 +0200168 ath10k_warn("failed to remove peer wep key %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300169 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)
Kalle Valobe6546f2014-03-25 14:18:51 +0200216 ath10k_warn("failed to remove key for %pM: %d\n",
217 addr, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300218 }
219
220 return first_errno;
221}
222
223
224/*********************/
225/* General utilities */
226/*********************/
227
228static inline enum wmi_phy_mode
229chan_to_phymode(const struct cfg80211_chan_def *chandef)
230{
231 enum wmi_phy_mode phymode = MODE_UNKNOWN;
232
233 switch (chandef->chan->band) {
234 case IEEE80211_BAND_2GHZ:
235 switch (chandef->width) {
236 case NL80211_CHAN_WIDTH_20_NOHT:
237 phymode = MODE_11G;
238 break;
239 case NL80211_CHAN_WIDTH_20:
240 phymode = MODE_11NG_HT20;
241 break;
242 case NL80211_CHAN_WIDTH_40:
243 phymode = MODE_11NG_HT40;
244 break;
John W. Linville0f817ed2013-06-27 13:50:09 -0400245 case NL80211_CHAN_WIDTH_5:
246 case NL80211_CHAN_WIDTH_10:
Kalle Valo5e3dd152013-06-12 20:52:10 +0300247 case NL80211_CHAN_WIDTH_80:
248 case NL80211_CHAN_WIDTH_80P80:
249 case NL80211_CHAN_WIDTH_160:
250 phymode = MODE_UNKNOWN;
251 break;
252 }
253 break;
254 case IEEE80211_BAND_5GHZ:
255 switch (chandef->width) {
256 case NL80211_CHAN_WIDTH_20_NOHT:
257 phymode = MODE_11A;
258 break;
259 case NL80211_CHAN_WIDTH_20:
260 phymode = MODE_11NA_HT20;
261 break;
262 case NL80211_CHAN_WIDTH_40:
263 phymode = MODE_11NA_HT40;
264 break;
265 case NL80211_CHAN_WIDTH_80:
266 phymode = MODE_11AC_VHT80;
267 break;
John W. Linville0f817ed2013-06-27 13:50:09 -0400268 case NL80211_CHAN_WIDTH_5:
269 case NL80211_CHAN_WIDTH_10:
Kalle Valo5e3dd152013-06-12 20:52:10 +0300270 case NL80211_CHAN_WIDTH_80P80:
271 case NL80211_CHAN_WIDTH_160:
272 phymode = MODE_UNKNOWN;
273 break;
274 }
275 break;
276 default:
277 break;
278 }
279
280 WARN_ON(phymode == MODE_UNKNOWN);
281 return phymode;
282}
283
284static u8 ath10k_parse_mpdudensity(u8 mpdudensity)
285{
286/*
287 * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
288 * 0 for no restriction
289 * 1 for 1/4 us
290 * 2 for 1/2 us
291 * 3 for 1 us
292 * 4 for 2 us
293 * 5 for 4 us
294 * 6 for 8 us
295 * 7 for 16 us
296 */
297 switch (mpdudensity) {
298 case 0:
299 return 0;
300 case 1:
301 case 2:
302 case 3:
303 /* Our lower layer calculations limit our precision to
304 1 microsecond */
305 return 1;
306 case 4:
307 return 2;
308 case 5:
309 return 4;
310 case 6:
311 return 8;
312 case 7:
313 return 16;
314 default:
315 return 0;
316 }
317}
318
319static int ath10k_peer_create(struct ath10k *ar, u32 vdev_id, const u8 *addr)
320{
321 int ret;
322
323 lockdep_assert_held(&ar->conf_mutex);
324
325 ret = ath10k_wmi_peer_create(ar, vdev_id, addr);
Ben Greear479398b2013-11-04 09:19:34 -0800326 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +0200327 ath10k_warn("failed to create wmi peer %pM on vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +0200328 addr, vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300329 return ret;
Ben Greear479398b2013-11-04 09:19:34 -0800330 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300331
332 ret = ath10k_wait_for_peer_created(ar, vdev_id, addr);
Ben Greear479398b2013-11-04 09:19:34 -0800333 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +0200334 ath10k_warn("failed to wait for created wmi peer %pM on vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +0200335 addr, vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300336 return ret;
Ben Greear479398b2013-11-04 09:19:34 -0800337 }
Bartosz Markowski0e759f32014-01-02 14:38:33 +0100338 spin_lock_bh(&ar->data_lock);
339 ar->num_peers++;
340 spin_unlock_bh(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300341
342 return 0;
343}
344
Kalle Valo5a13e762014-01-20 11:01:46 +0200345static int ath10k_mac_set_kickout(struct ath10k_vif *arvif)
346{
347 struct ath10k *ar = arvif->ar;
348 u32 param;
349 int ret;
350
351 param = ar->wmi.pdev_param->sta_kickout_th;
352 ret = ath10k_wmi_pdev_set_param(ar, param,
353 ATH10K_KICKOUT_THRESHOLD);
354 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +0200355 ath10k_warn("failed to set kickout threshold on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200356 arvif->vdev_id, ret);
Kalle Valo5a13e762014-01-20 11:01:46 +0200357 return ret;
358 }
359
360 param = ar->wmi.vdev_param->ap_keepalive_min_idle_inactive_time_secs;
361 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
362 ATH10K_KEEPALIVE_MIN_IDLE);
363 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +0200364 ath10k_warn("failed to set keepalive minimum idle time on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200365 arvif->vdev_id, ret);
Kalle Valo5a13e762014-01-20 11:01:46 +0200366 return ret;
367 }
368
369 param = ar->wmi.vdev_param->ap_keepalive_max_idle_inactive_time_secs;
370 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
371 ATH10K_KEEPALIVE_MAX_IDLE);
372 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +0200373 ath10k_warn("failed to set keepalive maximum idle time on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200374 arvif->vdev_id, ret);
Kalle Valo5a13e762014-01-20 11:01:46 +0200375 return ret;
376 }
377
378 param = ar->wmi.vdev_param->ap_keepalive_max_unresponsive_time_secs;
379 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
380 ATH10K_KEEPALIVE_MAX_UNRESPONSIVE);
381 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +0200382 ath10k_warn("failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200383 arvif->vdev_id, ret);
Kalle Valo5a13e762014-01-20 11:01:46 +0200384 return ret;
385 }
386
387 return 0;
388}
389
Michal Kazior424121c2013-07-22 14:13:31 +0200390static int ath10k_mac_set_rts(struct ath10k_vif *arvif, u32 value)
391{
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200392 struct ath10k *ar = arvif->ar;
393 u32 vdev_param;
394
Michal Kazior424121c2013-07-22 14:13:31 +0200395 if (value != 0xFFFFFFFF)
396 value = min_t(u32, arvif->ar->hw->wiphy->rts_threshold,
397 ATH10K_RTS_MAX);
398
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200399 vdev_param = ar->wmi.vdev_param->rts_threshold;
400 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
Michal Kazior424121c2013-07-22 14:13:31 +0200401}
402
403static int ath10k_mac_set_frag(struct ath10k_vif *arvif, u32 value)
404{
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200405 struct ath10k *ar = arvif->ar;
406 u32 vdev_param;
407
Michal Kazior424121c2013-07-22 14:13:31 +0200408 if (value != 0xFFFFFFFF)
409 value = clamp_t(u32, arvif->ar->hw->wiphy->frag_threshold,
410 ATH10K_FRAGMT_THRESHOLD_MIN,
411 ATH10K_FRAGMT_THRESHOLD_MAX);
412
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200413 vdev_param = ar->wmi.vdev_param->fragmentation_threshold;
414 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
Michal Kazior424121c2013-07-22 14:13:31 +0200415}
416
Kalle Valo5e3dd152013-06-12 20:52:10 +0300417static int ath10k_peer_delete(struct ath10k *ar, u32 vdev_id, const u8 *addr)
418{
419 int ret;
420
421 lockdep_assert_held(&ar->conf_mutex);
422
423 ret = ath10k_wmi_peer_delete(ar, vdev_id, addr);
424 if (ret)
425 return ret;
426
427 ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
428 if (ret)
429 return ret;
430
Bartosz Markowski0e759f32014-01-02 14:38:33 +0100431 spin_lock_bh(&ar->data_lock);
432 ar->num_peers--;
433 spin_unlock_bh(&ar->data_lock);
434
Kalle Valo5e3dd152013-06-12 20:52:10 +0300435 return 0;
436}
437
438static void ath10k_peer_cleanup(struct ath10k *ar, u32 vdev_id)
439{
440 struct ath10k_peer *peer, *tmp;
441
442 lockdep_assert_held(&ar->conf_mutex);
443
444 spin_lock_bh(&ar->data_lock);
445 list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
446 if (peer->vdev_id != vdev_id)
447 continue;
448
449 ath10k_warn("removing stale peer %pM from vdev_id %d\n",
450 peer->addr, vdev_id);
451
452 list_del(&peer->list);
453 kfree(peer);
Bartosz Markowski0e759f32014-01-02 14:38:33 +0100454 ar->num_peers--;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300455 }
456 spin_unlock_bh(&ar->data_lock);
457}
458
Michal Kaziora96d7742013-07-16 09:38:56 +0200459static void ath10k_peer_cleanup_all(struct ath10k *ar)
460{
461 struct ath10k_peer *peer, *tmp;
462
463 lockdep_assert_held(&ar->conf_mutex);
464
465 spin_lock_bh(&ar->data_lock);
466 list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
467 list_del(&peer->list);
468 kfree(peer);
469 }
Bartosz Markowski0e759f32014-01-02 14:38:33 +0100470 ar->num_peers = 0;
Michal Kaziora96d7742013-07-16 09:38:56 +0200471 spin_unlock_bh(&ar->data_lock);
472}
473
Kalle Valo5e3dd152013-06-12 20:52:10 +0300474/************************/
475/* Interface management */
476/************************/
477
478static inline int ath10k_vdev_setup_sync(struct ath10k *ar)
479{
480 int ret;
481
Michal Kazior548db542013-07-05 16:15:15 +0300482 lockdep_assert_held(&ar->conf_mutex);
483
Kalle Valo5e3dd152013-06-12 20:52:10 +0300484 ret = wait_for_completion_timeout(&ar->vdev_setup_done,
485 ATH10K_VDEV_SETUP_TIMEOUT_HZ);
486 if (ret == 0)
487 return -ETIMEDOUT;
488
489 return 0;
490}
491
492static int ath10k_vdev_start(struct ath10k_vif *arvif)
493{
494 struct ath10k *ar = arvif->ar;
Michal Kaziorc930f742014-01-23 11:38:25 +0100495 struct cfg80211_chan_def *chandef = &ar->chandef;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300496 struct wmi_vdev_start_request_arg arg = {};
497 int ret = 0;
498
499 lockdep_assert_held(&ar->conf_mutex);
500
Wolfram Sang16735d02013-11-14 14:32:02 -0800501 reinit_completion(&ar->vdev_setup_done);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300502
503 arg.vdev_id = arvif->vdev_id;
504 arg.dtim_period = arvif->dtim_period;
505 arg.bcn_intval = arvif->beacon_interval;
506
Michal Kaziorc930f742014-01-23 11:38:25 +0100507 arg.channel.freq = chandef->chan->center_freq;
508 arg.channel.band_center_freq1 = chandef->center_freq1;
509 arg.channel.mode = chan_to_phymode(chandef);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300510
Michal Kazior89c5c842013-10-23 04:02:13 -0700511 arg.channel.min_power = 0;
Michal Kaziorc930f742014-01-23 11:38:25 +0100512 arg.channel.max_power = chandef->chan->max_power * 2;
513 arg.channel.max_reg_power = chandef->chan->max_reg_power * 2;
514 arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300515
516 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
517 arg.ssid = arvif->u.ap.ssid;
518 arg.ssid_len = arvif->u.ap.ssid_len;
519 arg.hidden_ssid = arvif->u.ap.hidden_ssid;
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200520
521 /* For now allow DFS for AP mode */
522 arg.channel.chan_radar =
Michal Kaziorc930f742014-01-23 11:38:25 +0100523 !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300524 } else if (arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
525 arg.ssid = arvif->vif->bss_conf.ssid;
526 arg.ssid_len = arvif->vif->bss_conf.ssid_len;
527 }
528
Kalle Valo38a1d472013-09-08 17:56:14 +0300529 ath10k_dbg(ATH10K_DBG_MAC,
530 "mac vdev %d start center_freq %d phymode %s\n",
531 arg.vdev_id, arg.channel.freq,
532 ath10k_wmi_phymode_str(arg.channel.mode));
533
Kalle Valo5e3dd152013-06-12 20:52:10 +0300534 ret = ath10k_wmi_vdev_start(ar, &arg);
535 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +0200536 ath10k_warn("failed to start WMI vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200537 arg.vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300538 return ret;
539 }
540
541 ret = ath10k_vdev_setup_sync(ar);
542 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +0200543 ath10k_warn("failed to synchronise setup for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200544 arg.vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300545 return ret;
546 }
547
548 return ret;
549}
550
551static int ath10k_vdev_stop(struct ath10k_vif *arvif)
552{
553 struct ath10k *ar = arvif->ar;
554 int ret;
555
556 lockdep_assert_held(&ar->conf_mutex);
557
Wolfram Sang16735d02013-11-14 14:32:02 -0800558 reinit_completion(&ar->vdev_setup_done);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300559
560 ret = ath10k_wmi_vdev_stop(ar, arvif->vdev_id);
561 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +0200562 ath10k_warn("failed to stop WMI vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200563 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300564 return ret;
565 }
566
567 ret = ath10k_vdev_setup_sync(ar);
568 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +0200569 ath10k_warn("failed to syncronise setup for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200570 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300571 return ret;
572 }
573
574 return ret;
575}
576
577static int ath10k_monitor_start(struct ath10k *ar, int vdev_id)
578{
Michal Kaziorc930f742014-01-23 11:38:25 +0100579 struct cfg80211_chan_def *chandef = &ar->chandef;
580 struct ieee80211_channel *channel = chandef->chan;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300581 struct wmi_vdev_start_request_arg arg = {};
Kalle Valo5e3dd152013-06-12 20:52:10 +0300582 int ret = 0;
583
584 lockdep_assert_held(&ar->conf_mutex);
585
Michal Kazior0ed00ee2013-10-16 16:45:48 +0300586 if (!ar->monitor_present) {
Kalle Valobe6546f2014-03-25 14:18:51 +0200587 ath10k_warn("mac monitor stop -- monitor is not present\n");
Michal Kazior0ed00ee2013-10-16 16:45:48 +0300588 return -EINVAL;
589 }
590
Kalle Valo5e3dd152013-06-12 20:52:10 +0300591 arg.vdev_id = vdev_id;
592 arg.channel.freq = channel->center_freq;
Michal Kaziorc930f742014-01-23 11:38:25 +0100593 arg.channel.band_center_freq1 = chandef->center_freq1;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300594
595 /* TODO setup this dynamically, what in case we
596 don't have any vifs? */
Michal Kaziorc930f742014-01-23 11:38:25 +0100597 arg.channel.mode = chan_to_phymode(chandef);
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200598 arg.channel.chan_radar =
599 !!(channel->flags & IEEE80211_CHAN_RADAR);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300600
Michal Kazior89c5c842013-10-23 04:02:13 -0700601 arg.channel.min_power = 0;
Michal Kazior02256932013-10-23 04:02:14 -0700602 arg.channel.max_power = channel->max_power * 2;
603 arg.channel.max_reg_power = channel->max_reg_power * 2;
604 arg.channel.max_antenna_gain = channel->max_antenna_gain * 2;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300605
606 ret = ath10k_wmi_vdev_start(ar, &arg);
607 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +0200608 ath10k_warn("failed to start Monitor vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200609 vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300610 return ret;
611 }
612
613 ret = ath10k_vdev_setup_sync(ar);
614 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +0200615 ath10k_warn("failed to syncronise setup for monitor vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200616 vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300617 return ret;
618 }
619
620 ret = ath10k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
621 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +0200622 ath10k_warn("failed to put up monitor vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200623 vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300624 goto vdev_stop;
625 }
626
627 ar->monitor_vdev_id = vdev_id;
628 ar->monitor_enabled = true;
629
630 return 0;
631
632vdev_stop:
633 ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
634 if (ret)
Kalle Valobe6546f2014-03-25 14:18:51 +0200635 ath10k_warn("failed to stop monitor vdev %i after start failure: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200636 ar->monitor_vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300637
638 return ret;
639}
640
641static int ath10k_monitor_stop(struct ath10k *ar)
642{
643 int ret = 0;
644
645 lockdep_assert_held(&ar->conf_mutex);
646
Michal Kazior0ed00ee2013-10-16 16:45:48 +0300647 if (!ar->monitor_present) {
Kalle Valobe6546f2014-03-25 14:18:51 +0200648 ath10k_warn("mac monitor stop -- monitor is not present\n");
Michal Kazior0ed00ee2013-10-16 16:45:48 +0300649 return -EINVAL;
650 }
651
652 if (!ar->monitor_enabled) {
Kalle Valobe6546f2014-03-25 14:18:51 +0200653 ath10k_warn("mac monitor stop -- monitor is not enabled\n");
Michal Kazior0ed00ee2013-10-16 16:45:48 +0300654 return -EINVAL;
655 }
656
Marek Puzyniak52fa0192013-09-24 14:06:24 +0200657 ret = ath10k_wmi_vdev_down(ar, ar->monitor_vdev_id);
658 if (ret)
Kalle Valobe6546f2014-03-25 14:18:51 +0200659 ath10k_warn("failed to put down monitor vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200660 ar->monitor_vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300661
662 ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
663 if (ret)
Kalle Valobe6546f2014-03-25 14:18:51 +0200664 ath10k_warn("failed to stop monitor vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200665 ar->monitor_vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300666
667 ret = ath10k_vdev_setup_sync(ar);
668 if (ret)
Kalle Valobe6546f2014-03-25 14:18:51 +0200669 ath10k_warn("failed to synchronise monitor vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200670 ar->monitor_vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300671
672 ar->monitor_enabled = false;
673 return ret;
674}
675
676static int ath10k_monitor_create(struct ath10k *ar)
677{
678 int bit, ret = 0;
679
680 lockdep_assert_held(&ar->conf_mutex);
681
682 if (ar->monitor_present) {
Kalle Valobe6546f2014-03-25 14:18:51 +0200683 ath10k_warn("monitor mode already enabled\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +0300684 return 0;
685 }
686
687 bit = ffs(ar->free_vdev_map);
688 if (bit == 0) {
Kalle Valobe6546f2014-03-25 14:18:51 +0200689 ath10k_warn("no free vdev slots\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +0300690 return -ENOMEM;
691 }
692
693 ar->monitor_vdev_id = bit - 1;
694 ar->free_vdev_map &= ~(1 << ar->monitor_vdev_id);
695
696 ret = ath10k_wmi_vdev_create(ar, ar->monitor_vdev_id,
697 WMI_VDEV_TYPE_MONITOR,
698 0, ar->mac_addr);
699 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +0200700 ath10k_warn("failed to create WMI monitor vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200701 ar->monitor_vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300702 goto vdev_fail;
703 }
704
Kalle Valo60c3daa2013-09-08 17:56:07 +0300705 ath10k_dbg(ATH10K_DBG_MAC, "mac monitor vdev %d created\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300706 ar->monitor_vdev_id);
707
708 ar->monitor_present = true;
709 return 0;
710
711vdev_fail:
712 /*
713 * Restore the ID to the global map.
714 */
715 ar->free_vdev_map |= 1 << (ar->monitor_vdev_id);
716 return ret;
717}
718
719static int ath10k_monitor_destroy(struct ath10k *ar)
720{
721 int ret = 0;
722
723 lockdep_assert_held(&ar->conf_mutex);
724
725 if (!ar->monitor_present)
726 return 0;
727
728 ret = ath10k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
729 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +0200730 ath10k_warn("failed to delete WMI vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200731 ar->monitor_vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300732 return ret;
733 }
734
735 ar->free_vdev_map |= 1 << (ar->monitor_vdev_id);
736 ar->monitor_present = false;
737
Kalle Valo60c3daa2013-09-08 17:56:07 +0300738 ath10k_dbg(ATH10K_DBG_MAC, "mac monitor vdev %d deleted\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300739 ar->monitor_vdev_id);
740 return ret;
741}
742
Marek Kwaczynskie81bd102014-03-11 12:58:00 +0200743static int ath10k_recalc_rtscts_prot(struct ath10k_vif *arvif)
744{
745 struct ath10k *ar = arvif->ar;
746 u32 vdev_param, rts_cts = 0;
747
748 lockdep_assert_held(&ar->conf_mutex);
749
750 vdev_param = ar->wmi.vdev_param->enable_rtscts;
751
752 if (arvif->use_cts_prot || arvif->num_legacy_stations > 0)
753 rts_cts |= SM(WMI_RTSCTS_ENABLED, WMI_RTSCTS_SET);
754
755 if (arvif->num_legacy_stations > 0)
756 rts_cts |= SM(WMI_RTSCTS_ACROSS_SW_RETRIES,
757 WMI_RTSCTS_PROFILE);
758
759 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
760 rts_cts);
761}
762
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200763static int ath10k_start_cac(struct ath10k *ar)
764{
765 int ret;
766
767 lockdep_assert_held(&ar->conf_mutex);
768
769 set_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
770
771 ret = ath10k_monitor_create(ar);
772 if (ret) {
773 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
774 return ret;
775 }
776
777 ret = ath10k_monitor_start(ar, ar->monitor_vdev_id);
778 if (ret) {
779 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
780 ath10k_monitor_destroy(ar);
781 return ret;
782 }
783
784 ath10k_dbg(ATH10K_DBG_MAC, "mac cac start monitor vdev %d\n",
785 ar->monitor_vdev_id);
786
787 return 0;
788}
789
790static int ath10k_stop_cac(struct ath10k *ar)
791{
792 lockdep_assert_held(&ar->conf_mutex);
793
794 /* CAC is not running - do nothing */
795 if (!test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags))
796 return 0;
797
798 ath10k_monitor_stop(ar);
799 ath10k_monitor_destroy(ar);
800 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
801
802 ath10k_dbg(ATH10K_DBG_MAC, "mac cac finished\n");
803
804 return 0;
805}
806
807static const char *ath10k_dfs_state(enum nl80211_dfs_state dfs_state)
808{
809 switch (dfs_state) {
810 case NL80211_DFS_USABLE:
811 return "USABLE";
812 case NL80211_DFS_UNAVAILABLE:
813 return "UNAVAILABLE";
814 case NL80211_DFS_AVAILABLE:
815 return "AVAILABLE";
816 default:
817 WARN_ON(1);
818 return "bug";
819 }
820}
821
822static void ath10k_config_radar_detection(struct ath10k *ar)
823{
824 struct ieee80211_channel *chan = ar->hw->conf.chandef.chan;
825 bool radar = ar->hw->conf.radar_enabled;
826 bool chan_radar = !!(chan->flags & IEEE80211_CHAN_RADAR);
827 enum nl80211_dfs_state dfs_state = chan->dfs_state;
828 int ret;
829
830 lockdep_assert_held(&ar->conf_mutex);
831
832 ath10k_dbg(ATH10K_DBG_MAC,
833 "mac radar config update: chan %dMHz radar %d chan radar %d chan state %s\n",
834 chan->center_freq, radar, chan_radar,
835 ath10k_dfs_state(dfs_state));
836
837 /*
838 * It's safe to call it even if CAC is not started.
839 * This call here guarantees changing channel, etc. will stop CAC.
840 */
841 ath10k_stop_cac(ar);
842
843 if (!radar)
844 return;
845
846 if (!chan_radar)
847 return;
848
849 if (dfs_state != NL80211_DFS_USABLE)
850 return;
851
852 ret = ath10k_start_cac(ar);
853 if (ret) {
854 /*
855 * Not possible to start CAC on current channel so starting
856 * radiation is not allowed, make this channel DFS_UNAVAILABLE
857 * by indicating that radar was detected.
858 */
Kalle Valobe6546f2014-03-25 14:18:51 +0200859 ath10k_warn("failed to start CAC: %d\n", ret);
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200860 ieee80211_radar_detected(ar->hw);
861 }
862}
863
Kalle Valo5e3dd152013-06-12 20:52:10 +0300864static void ath10k_control_beaconing(struct ath10k_vif *arvif,
865 struct ieee80211_bss_conf *info)
866{
867 int ret = 0;
868
Michal Kazior548db542013-07-05 16:15:15 +0300869 lockdep_assert_held(&arvif->ar->conf_mutex);
870
Kalle Valo5e3dd152013-06-12 20:52:10 +0300871 if (!info->enable_beacon) {
872 ath10k_vdev_stop(arvif);
Michal Kaziorc930f742014-01-23 11:38:25 +0100873
874 arvif->is_started = false;
875 arvif->is_up = false;
876
Michal Kazior748afc42014-01-23 12:48:21 +0100877 spin_lock_bh(&arvif->ar->data_lock);
878 if (arvif->beacon) {
Michal Kazior767d34f2014-02-27 18:50:03 +0200879 dma_unmap_single(arvif->ar->dev,
880 ATH10K_SKB_CB(arvif->beacon)->paddr,
881 arvif->beacon->len, DMA_TO_DEVICE);
Michal Kazior748afc42014-01-23 12:48:21 +0100882 dev_kfree_skb_any(arvif->beacon);
883
884 arvif->beacon = NULL;
885 arvif->beacon_sent = false;
886 }
887 spin_unlock_bh(&arvif->ar->data_lock);
888
Kalle Valo5e3dd152013-06-12 20:52:10 +0300889 return;
890 }
891
892 arvif->tx_seq_no = 0x1000;
893
894 ret = ath10k_vdev_start(arvif);
895 if (ret)
896 return;
897
Michal Kaziorc930f742014-01-23 11:38:25 +0100898 arvif->aid = 0;
899 memcpy(arvif->bssid, info->bssid, ETH_ALEN);
900
901 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
902 arvif->bssid);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300903 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +0200904 ath10k_warn("failed to bring up vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +0200905 arvif->vdev_id, ret);
Michal Kaziorc930f742014-01-23 11:38:25 +0100906 ath10k_vdev_stop(arvif);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300907 return;
908 }
Michal Kaziorc930f742014-01-23 11:38:25 +0100909
910 arvif->is_started = true;
911 arvif->is_up = true;
912
Kalle Valo60c3daa2013-09-08 17:56:07 +0300913 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300914}
915
916static void ath10k_control_ibss(struct ath10k_vif *arvif,
917 struct ieee80211_bss_conf *info,
918 const u8 self_peer[ETH_ALEN])
919{
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200920 u32 vdev_param;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300921 int ret = 0;
922
Michal Kazior548db542013-07-05 16:15:15 +0300923 lockdep_assert_held(&arvif->ar->conf_mutex);
924
Kalle Valo5e3dd152013-06-12 20:52:10 +0300925 if (!info->ibss_joined) {
926 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, self_peer);
927 if (ret)
Kalle Valobe6546f2014-03-25 14:18:51 +0200928 ath10k_warn("failed to delete IBSS self peer %pM for vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300929 self_peer, arvif->vdev_id, ret);
930
Michal Kaziorc930f742014-01-23 11:38:25 +0100931 if (is_zero_ether_addr(arvif->bssid))
Kalle Valo5e3dd152013-06-12 20:52:10 +0300932 return;
933
934 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id,
Michal Kaziorc930f742014-01-23 11:38:25 +0100935 arvif->bssid);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300936 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +0200937 ath10k_warn("failed to delete IBSS BSSID peer %pM for vdev %d: %d\n",
Michal Kaziorc930f742014-01-23 11:38:25 +0100938 arvif->bssid, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300939 return;
940 }
941
Michal Kaziorc930f742014-01-23 11:38:25 +0100942 memset(arvif->bssid, 0, ETH_ALEN);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300943
944 return;
945 }
946
947 ret = ath10k_peer_create(arvif->ar, arvif->vdev_id, self_peer);
948 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +0200949 ath10k_warn("failed to create IBSS self peer %pM for vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300950 self_peer, arvif->vdev_id, ret);
951 return;
952 }
953
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200954 vdev_param = arvif->ar->wmi.vdev_param->atim_window;
955 ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +0300956 ATH10K_DEFAULT_ATIM);
957 if (ret)
Kalle Valobe6546f2014-03-25 14:18:51 +0200958 ath10k_warn("failed to set IBSS ATIM for vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300959 arvif->vdev_id, ret);
960}
961
962/*
963 * Review this when mac80211 gains per-interface powersave support.
964 */
Michal Kaziorad088bf2013-10-16 15:44:46 +0300965static int ath10k_mac_vif_setup_ps(struct ath10k_vif *arvif)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300966{
Michal Kaziorad088bf2013-10-16 15:44:46 +0300967 struct ath10k *ar = arvif->ar;
968 struct ieee80211_conf *conf = &ar->hw->conf;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300969 enum wmi_sta_powersave_param param;
970 enum wmi_sta_ps_mode psmode;
971 int ret;
972
Michal Kazior548db542013-07-05 16:15:15 +0300973 lockdep_assert_held(&arvif->ar->conf_mutex);
974
Michal Kaziorad088bf2013-10-16 15:44:46 +0300975 if (arvif->vif->type != NL80211_IFTYPE_STATION)
976 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300977
978 if (conf->flags & IEEE80211_CONF_PS) {
979 psmode = WMI_STA_PS_MODE_ENABLED;
980 param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
981
Michal Kaziorad088bf2013-10-16 15:44:46 +0300982 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
Kalle Valo5e3dd152013-06-12 20:52:10 +0300983 conf->dynamic_ps_timeout);
984 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +0200985 ath10k_warn("failed to set inactivity time for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +0200986 arvif->vdev_id, ret);
Michal Kaziorad088bf2013-10-16 15:44:46 +0300987 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300988 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300989 } else {
990 psmode = WMI_STA_PS_MODE_DISABLED;
991 }
992
Kalle Valo60c3daa2013-09-08 17:56:07 +0300993 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d psmode %s\n",
994 arvif->vdev_id, psmode ? "enable" : "disable");
995
Michal Kaziorad088bf2013-10-16 15:44:46 +0300996 ret = ath10k_wmi_set_psmode(ar, arvif->vdev_id, psmode);
997 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +0200998 ath10k_warn("failed to set PS Mode %d for vdev %d: %d\n",
999 psmode, arvif->vdev_id, ret);
Michal Kaziorad088bf2013-10-16 15:44:46 +03001000 return ret;
1001 }
1002
1003 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001004}
1005
1006/**********************/
1007/* Station management */
1008/**********************/
1009
1010static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
1011 struct ath10k_vif *arvif,
1012 struct ieee80211_sta *sta,
1013 struct ieee80211_bss_conf *bss_conf,
1014 struct wmi_peer_assoc_complete_arg *arg)
1015{
Michal Kazior548db542013-07-05 16:15:15 +03001016 lockdep_assert_held(&ar->conf_mutex);
1017
Kalle Valo5e3dd152013-06-12 20:52:10 +03001018 memcpy(arg->addr, sta->addr, ETH_ALEN);
1019 arg->vdev_id = arvif->vdev_id;
1020 arg->peer_aid = sta->aid;
1021 arg->peer_flags |= WMI_PEER_AUTH;
1022
1023 if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
1024 /*
1025 * Seems FW have problems with Power Save in STA
1026 * mode when we setup this parameter to high (eg. 5).
1027 * Often we see that FW don't send NULL (with clean P flags)
1028 * frame even there is info about buffered frames in beacons.
1029 * Sometimes we have to wait more than 10 seconds before FW
1030 * will wakeup. Often sending one ping from AP to our device
1031 * just fail (more than 50%).
1032 *
1033 * Seems setting this FW parameter to 1 couse FW
1034 * will check every beacon and will wakup immediately
1035 * after detection buffered data.
1036 */
1037 arg->peer_listen_intval = 1;
1038 else
1039 arg->peer_listen_intval = ar->hw->conf.listen_interval;
1040
1041 arg->peer_num_spatial_streams = 1;
1042
1043 /*
1044 * The assoc capabilities are available only in managed mode.
1045 */
1046 if (arvif->vdev_type == WMI_VDEV_TYPE_STA && bss_conf)
1047 arg->peer_caps = bss_conf->assoc_capability;
1048}
1049
1050static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
1051 struct ath10k_vif *arvif,
1052 struct wmi_peer_assoc_complete_arg *arg)
1053{
1054 struct ieee80211_vif *vif = arvif->vif;
1055 struct ieee80211_bss_conf *info = &vif->bss_conf;
1056 struct cfg80211_bss *bss;
1057 const u8 *rsnie = NULL;
1058 const u8 *wpaie = NULL;
1059
Michal Kazior548db542013-07-05 16:15:15 +03001060 lockdep_assert_held(&ar->conf_mutex);
1061
Kalle Valo5e3dd152013-06-12 20:52:10 +03001062 bss = cfg80211_get_bss(ar->hw->wiphy, ar->hw->conf.chandef.chan,
1063 info->bssid, NULL, 0, 0, 0);
1064 if (bss) {
1065 const struct cfg80211_bss_ies *ies;
1066
1067 rcu_read_lock();
1068 rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
1069
1070 ies = rcu_dereference(bss->ies);
1071
1072 wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
1073 WLAN_OUI_TYPE_MICROSOFT_WPA,
1074 ies->data,
1075 ies->len);
1076 rcu_read_unlock();
1077 cfg80211_put_bss(ar->hw->wiphy, bss);
1078 }
1079
1080 /* FIXME: base on RSN IE/WPA IE is a correct idea? */
1081 if (rsnie || wpaie) {
1082 ath10k_dbg(ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
1083 arg->peer_flags |= WMI_PEER_NEED_PTK_4_WAY;
1084 }
1085
1086 if (wpaie) {
1087 ath10k_dbg(ATH10K_DBG_WMI, "%s: wpa ie found\n", __func__);
1088 arg->peer_flags |= WMI_PEER_NEED_GTK_2_WAY;
1089 }
1090}
1091
1092static void ath10k_peer_assoc_h_rates(struct ath10k *ar,
1093 struct ieee80211_sta *sta,
1094 struct wmi_peer_assoc_complete_arg *arg)
1095{
1096 struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
1097 const struct ieee80211_supported_band *sband;
1098 const struct ieee80211_rate *rates;
1099 u32 ratemask;
1100 int i;
1101
Michal Kazior548db542013-07-05 16:15:15 +03001102 lockdep_assert_held(&ar->conf_mutex);
1103
Kalle Valo5e3dd152013-06-12 20:52:10 +03001104 sband = ar->hw->wiphy->bands[ar->hw->conf.chandef.chan->band];
1105 ratemask = sta->supp_rates[ar->hw->conf.chandef.chan->band];
1106 rates = sband->bitrates;
1107
1108 rateset->num_rates = 0;
1109
1110 for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
1111 if (!(ratemask & 1))
1112 continue;
1113
1114 rateset->rates[rateset->num_rates] = rates->hw_value;
1115 rateset->num_rates++;
1116 }
1117}
1118
1119static void ath10k_peer_assoc_h_ht(struct ath10k *ar,
1120 struct ieee80211_sta *sta,
1121 struct wmi_peer_assoc_complete_arg *arg)
1122{
1123 const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001124 int i, n;
1125
Michal Kazior548db542013-07-05 16:15:15 +03001126 lockdep_assert_held(&ar->conf_mutex);
1127
Kalle Valo5e3dd152013-06-12 20:52:10 +03001128 if (!ht_cap->ht_supported)
1129 return;
1130
1131 arg->peer_flags |= WMI_PEER_HT;
1132 arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1133 ht_cap->ampdu_factor)) - 1;
1134
1135 arg->peer_mpdu_density =
1136 ath10k_parse_mpdudensity(ht_cap->ampdu_density);
1137
1138 arg->peer_ht_caps = ht_cap->cap;
1139 arg->peer_rate_caps |= WMI_RC_HT_FLAG;
1140
1141 if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
1142 arg->peer_flags |= WMI_PEER_LDPC;
1143
1144 if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
1145 arg->peer_flags |= WMI_PEER_40MHZ;
1146 arg->peer_rate_caps |= WMI_RC_CW40_FLAG;
1147 }
1148
1149 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
1150 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
1151
1152 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
1153 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
1154
1155 if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
1156 arg->peer_rate_caps |= WMI_RC_TX_STBC_FLAG;
1157 arg->peer_flags |= WMI_PEER_STBC;
1158 }
1159
1160 if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
1161 u32 stbc;
1162 stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
1163 stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
1164 stbc = stbc << WMI_RC_RX_STBC_FLAG_S;
1165 arg->peer_rate_caps |= stbc;
1166 arg->peer_flags |= WMI_PEER_STBC;
1167 }
1168
Kalle Valo5e3dd152013-06-12 20:52:10 +03001169 if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
1170 arg->peer_rate_caps |= WMI_RC_TS_FLAG;
1171 else if (ht_cap->mcs.rx_mask[1])
1172 arg->peer_rate_caps |= WMI_RC_DS_FLAG;
1173
1174 for (i = 0, n = 0; i < IEEE80211_HT_MCS_MASK_LEN*8; i++)
1175 if (ht_cap->mcs.rx_mask[i/8] & (1 << i%8))
1176 arg->peer_ht_rates.rates[n++] = i;
1177
Bartosz Markowskifd71f802014-02-10 13:12:55 +01001178 /*
1179 * This is a workaround for HT-enabled STAs which break the spec
1180 * and have no HT capabilities RX mask (no HT RX MCS map).
1181 *
1182 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
1183 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
1184 *
1185 * Firmware asserts if such situation occurs.
1186 */
1187 if (n == 0) {
1188 arg->peer_ht_rates.num_rates = 8;
1189 for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
1190 arg->peer_ht_rates.rates[i] = i;
1191 } else {
1192 arg->peer_ht_rates.num_rates = n;
1193 arg->peer_num_spatial_streams = sta->rx_nss;
1194 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001195
Kalle Valo60c3daa2013-09-08 17:56:07 +03001196 ath10k_dbg(ATH10K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
1197 arg->addr,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001198 arg->peer_ht_rates.num_rates,
1199 arg->peer_num_spatial_streams);
1200}
1201
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001202static int ath10k_peer_assoc_qos_ap(struct ath10k *ar,
1203 struct ath10k_vif *arvif,
1204 struct ieee80211_sta *sta)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001205{
1206 u32 uapsd = 0;
1207 u32 max_sp = 0;
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001208 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001209
Michal Kazior548db542013-07-05 16:15:15 +03001210 lockdep_assert_held(&ar->conf_mutex);
1211
Kalle Valo5e3dd152013-06-12 20:52:10 +03001212 if (sta->wme && sta->uapsd_queues) {
Kalle Valo60c3daa2013-09-08 17:56:07 +03001213 ath10k_dbg(ATH10K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001214 sta->uapsd_queues, sta->max_sp);
1215
Kalle Valo5e3dd152013-06-12 20:52:10 +03001216 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1217 uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
1218 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
1219 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1220 uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
1221 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
1222 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1223 uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
1224 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
1225 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1226 uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
1227 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
1228
1229
1230 if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
1231 max_sp = sta->max_sp;
1232
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001233 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1234 sta->addr,
1235 WMI_AP_PS_PEER_PARAM_UAPSD,
1236 uapsd);
1237 if (ret) {
Ben Greear69244e52014-02-27 18:50:00 +02001238 ath10k_warn("failed to set ap ps peer param uapsd for vdev %i: %d\n",
1239 arvif->vdev_id, ret);
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001240 return ret;
1241 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001242
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001243 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1244 sta->addr,
1245 WMI_AP_PS_PEER_PARAM_MAX_SP,
1246 max_sp);
1247 if (ret) {
Ben Greear69244e52014-02-27 18:50:00 +02001248 ath10k_warn("failed to set ap ps peer param max sp for vdev %i: %d\n",
1249 arvif->vdev_id, ret);
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001250 return ret;
1251 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001252
1253 /* TODO setup this based on STA listen interval and
1254 beacon interval. Currently we don't know
1255 sta->listen_interval - mac80211 patch required.
1256 Currently use 10 seconds */
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001257 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id, sta->addr,
1258 WMI_AP_PS_PEER_PARAM_AGEOUT_TIME, 10);
1259 if (ret) {
Ben Greear69244e52014-02-27 18:50:00 +02001260 ath10k_warn("failed to set ap ps peer param ageout time for vdev %i: %d\n",
1261 arvif->vdev_id, ret);
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001262 return ret;
1263 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001264 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001265
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001266 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001267}
1268
1269static void ath10k_peer_assoc_h_vht(struct ath10k *ar,
1270 struct ieee80211_sta *sta,
1271 struct wmi_peer_assoc_complete_arg *arg)
1272{
1273 const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
Sujith Manoharana24b88b2013-10-07 19:51:57 -07001274 u8 ampdu_factor;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001275
1276 if (!vht_cap->vht_supported)
1277 return;
1278
1279 arg->peer_flags |= WMI_PEER_VHT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001280 arg->peer_vht_caps = vht_cap->cap;
1281
Sujith Manoharana24b88b2013-10-07 19:51:57 -07001282
1283 ampdu_factor = (vht_cap->cap &
1284 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
1285 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
1286
1287 /* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
1288 * zero in VHT IE. Using it would result in degraded throughput.
1289 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
1290 * it if VHT max_mpdu is smaller. */
1291 arg->peer_max_mpdu = max(arg->peer_max_mpdu,
1292 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1293 ampdu_factor)) - 1);
1294
Kalle Valo5e3dd152013-06-12 20:52:10 +03001295 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1296 arg->peer_flags |= WMI_PEER_80MHZ;
1297
1298 arg->peer_vht_rates.rx_max_rate =
1299 __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
1300 arg->peer_vht_rates.rx_mcs_set =
1301 __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
1302 arg->peer_vht_rates.tx_max_rate =
1303 __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
1304 arg->peer_vht_rates.tx_mcs_set =
1305 __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
1306
Kalle Valo60c3daa2013-09-08 17:56:07 +03001307 ath10k_dbg(ATH10K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
1308 sta->addr, arg->peer_max_mpdu, arg->peer_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001309}
1310
1311static void ath10k_peer_assoc_h_qos(struct ath10k *ar,
1312 struct ath10k_vif *arvif,
1313 struct ieee80211_sta *sta,
1314 struct ieee80211_bss_conf *bss_conf,
1315 struct wmi_peer_assoc_complete_arg *arg)
1316{
1317 switch (arvif->vdev_type) {
1318 case WMI_VDEV_TYPE_AP:
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001319 if (sta->wme)
1320 arg->peer_flags |= WMI_PEER_QOS;
1321
1322 if (sta->wme && sta->uapsd_queues) {
1323 arg->peer_flags |= WMI_PEER_APSD;
1324 arg->peer_rate_caps |= WMI_RC_UAPSD_FLAG;
1325 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001326 break;
1327 case WMI_VDEV_TYPE_STA:
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001328 if (bss_conf->qos)
1329 arg->peer_flags |= WMI_PEER_QOS;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001330 break;
1331 default:
1332 break;
1333 }
1334}
1335
1336static void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
1337 struct ath10k_vif *arvif,
1338 struct ieee80211_sta *sta,
1339 struct wmi_peer_assoc_complete_arg *arg)
1340{
1341 enum wmi_phy_mode phymode = MODE_UNKNOWN;
1342
Kalle Valo5e3dd152013-06-12 20:52:10 +03001343 switch (ar->hw->conf.chandef.chan->band) {
1344 case IEEE80211_BAND_2GHZ:
1345 if (sta->ht_cap.ht_supported) {
1346 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1347 phymode = MODE_11NG_HT40;
1348 else
1349 phymode = MODE_11NG_HT20;
1350 } else {
1351 phymode = MODE_11G;
1352 }
1353
1354 break;
1355 case IEEE80211_BAND_5GHZ:
Sujith Manoharan7cc45e92013-09-08 18:19:55 +03001356 /*
1357 * Check VHT first.
1358 */
1359 if (sta->vht_cap.vht_supported) {
1360 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1361 phymode = MODE_11AC_VHT80;
1362 else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1363 phymode = MODE_11AC_VHT40;
1364 else if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
1365 phymode = MODE_11AC_VHT20;
1366 } else if (sta->ht_cap.ht_supported) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001367 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1368 phymode = MODE_11NA_HT40;
1369 else
1370 phymode = MODE_11NA_HT20;
1371 } else {
1372 phymode = MODE_11A;
1373 }
1374
1375 break;
1376 default:
1377 break;
1378 }
1379
Kalle Valo38a1d472013-09-08 17:56:14 +03001380 ath10k_dbg(ATH10K_DBG_MAC, "mac peer %pM phymode %s\n",
1381 sta->addr, ath10k_wmi_phymode_str(phymode));
Kalle Valo60c3daa2013-09-08 17:56:07 +03001382
Kalle Valo5e3dd152013-06-12 20:52:10 +03001383 arg->peer_phymode = phymode;
1384 WARN_ON(phymode == MODE_UNKNOWN);
1385}
1386
Kalle Valob9ada652013-10-16 15:44:46 +03001387static int ath10k_peer_assoc_prepare(struct ath10k *ar,
1388 struct ath10k_vif *arvif,
1389 struct ieee80211_sta *sta,
1390 struct ieee80211_bss_conf *bss_conf,
1391 struct wmi_peer_assoc_complete_arg *arg)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001392{
Michal Kazior548db542013-07-05 16:15:15 +03001393 lockdep_assert_held(&ar->conf_mutex);
1394
Kalle Valob9ada652013-10-16 15:44:46 +03001395 memset(arg, 0, sizeof(*arg));
Kalle Valo5e3dd152013-06-12 20:52:10 +03001396
Kalle Valob9ada652013-10-16 15:44:46 +03001397 ath10k_peer_assoc_h_basic(ar, arvif, sta, bss_conf, arg);
1398 ath10k_peer_assoc_h_crypto(ar, arvif, arg);
1399 ath10k_peer_assoc_h_rates(ar, sta, arg);
1400 ath10k_peer_assoc_h_ht(ar, sta, arg);
1401 ath10k_peer_assoc_h_vht(ar, sta, arg);
1402 ath10k_peer_assoc_h_qos(ar, arvif, sta, bss_conf, arg);
1403 ath10k_peer_assoc_h_phymode(ar, arvif, sta, arg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001404
Kalle Valob9ada652013-10-16 15:44:46 +03001405 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001406}
1407
Michal Kazior90046f52014-02-14 14:45:51 +01001408static const u32 ath10k_smps_map[] = {
1409 [WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
1410 [WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
1411 [WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
1412 [WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
1413};
1414
1415static int ath10k_setup_peer_smps(struct ath10k *ar, struct ath10k_vif *arvif,
1416 const u8 *addr,
1417 const struct ieee80211_sta_ht_cap *ht_cap)
1418{
1419 int smps;
1420
1421 if (!ht_cap->ht_supported)
1422 return 0;
1423
1424 smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
1425 smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
1426
1427 if (smps >= ARRAY_SIZE(ath10k_smps_map))
1428 return -EINVAL;
1429
1430 return ath10k_wmi_peer_set_param(ar, arvif->vdev_id, addr,
1431 WMI_PEER_SMPS_STATE,
1432 ath10k_smps_map[smps]);
1433}
1434
Kalle Valo5e3dd152013-06-12 20:52:10 +03001435/* can be called only in mac80211 callbacks due to `key_count` usage */
1436static void ath10k_bss_assoc(struct ieee80211_hw *hw,
1437 struct ieee80211_vif *vif,
1438 struct ieee80211_bss_conf *bss_conf)
1439{
1440 struct ath10k *ar = hw->priv;
1441 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Michal Kazior90046f52014-02-14 14:45:51 +01001442 struct ieee80211_sta_ht_cap ht_cap;
Kalle Valob9ada652013-10-16 15:44:46 +03001443 struct wmi_peer_assoc_complete_arg peer_arg;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001444 struct ieee80211_sta *ap_sta;
1445 int ret;
1446
Michal Kazior548db542013-07-05 16:15:15 +03001447 lockdep_assert_held(&ar->conf_mutex);
1448
Kalle Valo5e3dd152013-06-12 20:52:10 +03001449 rcu_read_lock();
1450
1451 ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
1452 if (!ap_sta) {
Kalle Valobe6546f2014-03-25 14:18:51 +02001453 ath10k_warn("failed to find station entry for bss %pM vdev %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02001454 bss_conf->bssid, arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001455 rcu_read_unlock();
1456 return;
1457 }
1458
Michal Kazior90046f52014-02-14 14:45:51 +01001459 /* ap_sta must be accessed only within rcu section which must be left
1460 * before calling ath10k_setup_peer_smps() which might sleep. */
1461 ht_cap = ap_sta->ht_cap;
1462
Kalle Valob9ada652013-10-16 15:44:46 +03001463 ret = ath10k_peer_assoc_prepare(ar, arvif, ap_sta,
1464 bss_conf, &peer_arg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001465 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02001466 ath10k_warn("failed to prepare peer assoc for %pM vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001467 bss_conf->bssid, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001468 rcu_read_unlock();
1469 return;
1470 }
1471
1472 rcu_read_unlock();
1473
Kalle Valob9ada652013-10-16 15:44:46 +03001474 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
1475 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02001476 ath10k_warn("failed to run peer assoc for %pM vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001477 bss_conf->bssid, arvif->vdev_id, ret);
Kalle Valob9ada652013-10-16 15:44:46 +03001478 return;
1479 }
1480
Michal Kazior90046f52014-02-14 14:45:51 +01001481 ret = ath10k_setup_peer_smps(ar, arvif, bss_conf->bssid, &ht_cap);
1482 if (ret) {
Ben Greear69244e52014-02-27 18:50:00 +02001483 ath10k_warn("failed to setup peer SMPS for vdev %i: %d\n",
1484 arvif->vdev_id, ret);
Michal Kazior90046f52014-02-14 14:45:51 +01001485 return;
1486 }
1487
Kalle Valo60c3daa2013-09-08 17:56:07 +03001488 ath10k_dbg(ATH10K_DBG_MAC,
1489 "mac vdev %d up (associated) bssid %pM aid %d\n",
1490 arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
1491
Michal Kaziorc930f742014-01-23 11:38:25 +01001492 arvif->aid = bss_conf->aid;
1493 memcpy(arvif->bssid, bss_conf->bssid, ETH_ALEN);
1494
1495 ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
1496 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02001497 ath10k_warn("failed to set vdev %d up: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001498 arvif->vdev_id, ret);
Michal Kaziorc930f742014-01-23 11:38:25 +01001499 return;
1500 }
1501
1502 arvif->is_up = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001503}
1504
1505/*
1506 * FIXME: flush TIDs
1507 */
1508static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
1509 struct ieee80211_vif *vif)
1510{
1511 struct ath10k *ar = hw->priv;
1512 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1513 int ret;
1514
Michal Kazior548db542013-07-05 16:15:15 +03001515 lockdep_assert_held(&ar->conf_mutex);
1516
Kalle Valo5e3dd152013-06-12 20:52:10 +03001517 /*
1518 * For some reason, calling VDEV-DOWN before VDEV-STOP
1519 * makes the FW to send frames via HTT after disassociation.
1520 * No idea why this happens, even though VDEV-DOWN is supposed
1521 * to be analogous to link down, so just stop the VDEV.
1522 */
Kalle Valo60c3daa2013-09-08 17:56:07 +03001523 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d stop (disassociated\n",
1524 arvif->vdev_id);
1525
1526 /* FIXME: check return value */
Kalle Valo5e3dd152013-06-12 20:52:10 +03001527 ret = ath10k_vdev_stop(arvif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001528
1529 /*
1530 * If we don't call VDEV-DOWN after VDEV-STOP FW will remain active and
1531 * report beacons from previously associated network through HTT.
1532 * This in turn would spam mac80211 WARN_ON if we bring down all
1533 * interfaces as it expects there is no rx when no interface is
1534 * running.
1535 */
Kalle Valo60c3daa2013-09-08 17:56:07 +03001536 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d down\n", arvif->vdev_id);
1537
1538 /* FIXME: why don't we print error if wmi call fails? */
Kalle Valo5e3dd152013-06-12 20:52:10 +03001539 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001540
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001541 arvif->def_wep_key_idx = 0;
Michal Kaziorc930f742014-01-23 11:38:25 +01001542
1543 arvif->is_started = false;
1544 arvif->is_up = false;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001545}
1546
1547static int ath10k_station_assoc(struct ath10k *ar, struct ath10k_vif *arvif,
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02001548 struct ieee80211_sta *sta, bool reassoc)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001549{
Kalle Valob9ada652013-10-16 15:44:46 +03001550 struct wmi_peer_assoc_complete_arg peer_arg;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001551 int ret = 0;
1552
Michal Kazior548db542013-07-05 16:15:15 +03001553 lockdep_assert_held(&ar->conf_mutex);
1554
Kalle Valob9ada652013-10-16 15:44:46 +03001555 ret = ath10k_peer_assoc_prepare(ar, arvif, sta, NULL, &peer_arg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001556 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02001557 ath10k_warn("failed to prepare WMI peer assoc for %pM vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02001558 sta->addr, arvif->vdev_id, ret);
Kalle Valob9ada652013-10-16 15:44:46 +03001559 return ret;
1560 }
1561
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02001562 peer_arg.peer_reassoc = reassoc;
Kalle Valob9ada652013-10-16 15:44:46 +03001563 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
1564 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02001565 ath10k_warn("failed to run peer assoc for STA %pM vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001566 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001567 return ret;
1568 }
1569
Michal Kazior90046f52014-02-14 14:45:51 +01001570 ret = ath10k_setup_peer_smps(ar, arvif, sta->addr, &sta->ht_cap);
1571 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02001572 ath10k_warn("failed to setup peer SMPS for vdev %d: %d\n",
1573 arvif->vdev_id, ret);
Michal Kazior90046f52014-02-14 14:45:51 +01001574 return ret;
1575 }
1576
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02001577 if (!sta->wme) {
1578 arvif->num_legacy_stations++;
1579 ret = ath10k_recalc_rtscts_prot(arvif);
1580 if (ret) {
1581 ath10k_warn("failed to recalculate rts/cts prot for vdev %d: %d\n",
1582 arvif->vdev_id, ret);
1583 return ret;
1584 }
1585 }
1586
Kalle Valo5e3dd152013-06-12 20:52:10 +03001587 ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
1588 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02001589 ath10k_warn("failed to install peer wep keys for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001590 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001591 return ret;
1592 }
1593
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001594 ret = ath10k_peer_assoc_qos_ap(ar, arvif, sta);
1595 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02001596 ath10k_warn("failed to set qos params for STA %pM for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001597 sta->addr, arvif->vdev_id, ret);
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001598 return ret;
1599 }
1600
Kalle Valo5e3dd152013-06-12 20:52:10 +03001601 return ret;
1602}
1603
1604static int ath10k_station_disassoc(struct ath10k *ar, struct ath10k_vif *arvif,
1605 struct ieee80211_sta *sta)
1606{
1607 int ret = 0;
1608
Michal Kazior548db542013-07-05 16:15:15 +03001609 lockdep_assert_held(&ar->conf_mutex);
1610
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02001611 if (!sta->wme) {
1612 arvif->num_legacy_stations--;
1613 ret = ath10k_recalc_rtscts_prot(arvif);
1614 if (ret) {
1615 ath10k_warn("failed to recalculate rts/cts prot for vdev %d: %d\n",
1616 arvif->vdev_id, ret);
1617 return ret;
1618 }
1619 }
1620
Kalle Valo5e3dd152013-06-12 20:52:10 +03001621 ret = ath10k_clear_peer_keys(arvif, sta->addr);
1622 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02001623 ath10k_warn("failed to clear all peer wep keys for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001624 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001625 return ret;
1626 }
1627
1628 return ret;
1629}
1630
1631/**************/
1632/* Regulatory */
1633/**************/
1634
1635static int ath10k_update_channel_list(struct ath10k *ar)
1636{
1637 struct ieee80211_hw *hw = ar->hw;
1638 struct ieee80211_supported_band **bands;
1639 enum ieee80211_band band;
1640 struct ieee80211_channel *channel;
1641 struct wmi_scan_chan_list_arg arg = {0};
1642 struct wmi_channel_arg *ch;
1643 bool passive;
1644 int len;
1645 int ret;
1646 int i;
1647
Michal Kazior548db542013-07-05 16:15:15 +03001648 lockdep_assert_held(&ar->conf_mutex);
1649
Kalle Valo5e3dd152013-06-12 20:52:10 +03001650 bands = hw->wiphy->bands;
1651 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1652 if (!bands[band])
1653 continue;
1654
1655 for (i = 0; i < bands[band]->n_channels; i++) {
1656 if (bands[band]->channels[i].flags &
1657 IEEE80211_CHAN_DISABLED)
1658 continue;
1659
1660 arg.n_channels++;
1661 }
1662 }
1663
1664 len = sizeof(struct wmi_channel_arg) * arg.n_channels;
1665 arg.channels = kzalloc(len, GFP_KERNEL);
1666 if (!arg.channels)
1667 return -ENOMEM;
1668
1669 ch = arg.channels;
1670 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1671 if (!bands[band])
1672 continue;
1673
1674 for (i = 0; i < bands[band]->n_channels; i++) {
1675 channel = &bands[band]->channels[i];
1676
1677 if (channel->flags & IEEE80211_CHAN_DISABLED)
1678 continue;
1679
1680 ch->allow_ht = true;
1681
1682 /* FIXME: when should we really allow VHT? */
1683 ch->allow_vht = true;
1684
1685 ch->allow_ibss =
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +02001686 !(channel->flags & IEEE80211_CHAN_NO_IR);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001687
1688 ch->ht40plus =
1689 !(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
1690
Marek Puzyniake8a50f82013-11-20 09:59:47 +02001691 ch->chan_radar =
1692 !!(channel->flags & IEEE80211_CHAN_RADAR);
1693
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +02001694 passive = channel->flags & IEEE80211_CHAN_NO_IR;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001695 ch->passive = passive;
1696
1697 ch->freq = channel->center_freq;
Michal Kazior89c5c842013-10-23 04:02:13 -07001698 ch->min_power = 0;
Michal Kazior02256932013-10-23 04:02:14 -07001699 ch->max_power = channel->max_power * 2;
1700 ch->max_reg_power = channel->max_reg_power * 2;
1701 ch->max_antenna_gain = channel->max_antenna_gain * 2;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001702 ch->reg_class_id = 0; /* FIXME */
1703
1704 /* FIXME: why use only legacy modes, why not any
1705 * HT/VHT modes? Would that even make any
1706 * difference? */
1707 if (channel->band == IEEE80211_BAND_2GHZ)
1708 ch->mode = MODE_11G;
1709 else
1710 ch->mode = MODE_11A;
1711
1712 if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
1713 continue;
1714
1715 ath10k_dbg(ATH10K_DBG_WMI,
Kalle Valo60c3daa2013-09-08 17:56:07 +03001716 "mac channel [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
1717 ch - arg.channels, arg.n_channels,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001718 ch->freq, ch->max_power, ch->max_reg_power,
1719 ch->max_antenna_gain, ch->mode);
1720
1721 ch++;
1722 }
1723 }
1724
1725 ret = ath10k_wmi_scan_chan_list(ar, &arg);
1726 kfree(arg.channels);
1727
1728 return ret;
1729}
1730
Marek Puzyniak821af6a2014-03-21 17:46:57 +02001731static enum wmi_dfs_region
1732ath10k_mac_get_dfs_region(enum nl80211_dfs_regions dfs_region)
1733{
1734 switch (dfs_region) {
1735 case NL80211_DFS_UNSET:
1736 return WMI_UNINIT_DFS_DOMAIN;
1737 case NL80211_DFS_FCC:
1738 return WMI_FCC_DFS_DOMAIN;
1739 case NL80211_DFS_ETSI:
1740 return WMI_ETSI_DFS_DOMAIN;
1741 case NL80211_DFS_JP:
1742 return WMI_MKK4_DFS_DOMAIN;
1743 }
1744 return WMI_UNINIT_DFS_DOMAIN;
1745}
1746
Michal Kaziorf7843d72013-07-16 09:38:52 +02001747static void ath10k_regd_update(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001748{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001749 struct reg_dmn_pair_mapping *regpair;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001750 int ret;
Marek Puzyniak821af6a2014-03-21 17:46:57 +02001751 enum wmi_dfs_region wmi_dfs_reg;
1752 enum nl80211_dfs_regions nl_dfs_reg;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001753
Michal Kaziorf7843d72013-07-16 09:38:52 +02001754 lockdep_assert_held(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001755
1756 ret = ath10k_update_channel_list(ar);
1757 if (ret)
Kalle Valobe6546f2014-03-25 14:18:51 +02001758 ath10k_warn("failed to update channel list: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001759
1760 regpair = ar->ath_common.regulatory.regpair;
Michal Kaziorf7843d72013-07-16 09:38:52 +02001761
Marek Puzyniak821af6a2014-03-21 17:46:57 +02001762 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
1763 nl_dfs_reg = ar->dfs_detector->region;
1764 wmi_dfs_reg = ath10k_mac_get_dfs_region(nl_dfs_reg);
1765 } else {
1766 wmi_dfs_reg = WMI_UNINIT_DFS_DOMAIN;
1767 }
1768
Kalle Valo5e3dd152013-06-12 20:52:10 +03001769 /* Target allows setting up per-band regdomain but ath_common provides
1770 * a combined one only */
1771 ret = ath10k_wmi_pdev_set_regdomain(ar,
Kalle Valoef8c0012014-02-13 18:13:12 +02001772 regpair->reg_domain,
1773 regpair->reg_domain, /* 2ghz */
1774 regpair->reg_domain, /* 5ghz */
Kalle Valo5e3dd152013-06-12 20:52:10 +03001775 regpair->reg_2ghz_ctl,
Marek Puzyniak821af6a2014-03-21 17:46:57 +02001776 regpair->reg_5ghz_ctl,
1777 wmi_dfs_reg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001778 if (ret)
Kalle Valobe6546f2014-03-25 14:18:51 +02001779 ath10k_warn("failed to set pdev regdomain: %d\n", ret);
Michal Kaziorf7843d72013-07-16 09:38:52 +02001780}
Michal Kazior548db542013-07-05 16:15:15 +03001781
Michal Kaziorf7843d72013-07-16 09:38:52 +02001782static void ath10k_reg_notifier(struct wiphy *wiphy,
1783 struct regulatory_request *request)
1784{
1785 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
1786 struct ath10k *ar = hw->priv;
Janusz Dziedzic9702c682013-11-20 09:59:41 +02001787 bool result;
Michal Kaziorf7843d72013-07-16 09:38:52 +02001788
1789 ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
1790
Janusz Dziedzic9702c682013-11-20 09:59:41 +02001791 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
1792 ath10k_dbg(ATH10K_DBG_REGULATORY, "dfs region 0x%x\n",
1793 request->dfs_region);
1794 result = ar->dfs_detector->set_dfs_domain(ar->dfs_detector,
1795 request->dfs_region);
1796 if (!result)
Kalle Valobe6546f2014-03-25 14:18:51 +02001797 ath10k_warn("DFS region 0x%X not supported, will trigger radar for every pulse\n",
Janusz Dziedzic9702c682013-11-20 09:59:41 +02001798 request->dfs_region);
1799 }
1800
Michal Kaziorf7843d72013-07-16 09:38:52 +02001801 mutex_lock(&ar->conf_mutex);
1802 if (ar->state == ATH10K_STATE_ON)
1803 ath10k_regd_update(ar);
Michal Kazior548db542013-07-05 16:15:15 +03001804 mutex_unlock(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001805}
1806
1807/***************/
1808/* TX handlers */
1809/***************/
1810
Michal Kazior42c3aa62013-10-02 11:03:38 +02001811static u8 ath10k_tx_h_get_tid(struct ieee80211_hdr *hdr)
1812{
1813 if (ieee80211_is_mgmt(hdr->frame_control))
1814 return HTT_DATA_TX_EXT_TID_MGMT;
1815
1816 if (!ieee80211_is_data_qos(hdr->frame_control))
1817 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
1818
1819 if (!is_unicast_ether_addr(ieee80211_get_DA(hdr)))
1820 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
1821
1822 return ieee80211_get_qos_ctl(hdr)[0] & IEEE80211_QOS_CTL_TID_MASK;
1823}
1824
Michal Kaziorddb6ad72013-10-02 11:03:39 +02001825static u8 ath10k_tx_h_get_vdev_id(struct ath10k *ar,
1826 struct ieee80211_tx_info *info)
1827{
1828 if (info->control.vif)
1829 return ath10k_vif_to_arvif(info->control.vif)->vdev_id;
1830
1831 if (ar->monitor_enabled)
1832 return ar->monitor_vdev_id;
1833
Kalle Valobe6546f2014-03-25 14:18:51 +02001834 ath10k_warn("failed to resolve vdev id\n");
Michal Kaziorddb6ad72013-10-02 11:03:39 +02001835 return 0;
1836}
1837
Kalle Valo5e3dd152013-06-12 20:52:10 +03001838/*
1839 * Frames sent to the FW have to be in "Native Wifi" format.
1840 * Strip the QoS field from the 802.11 header.
1841 */
1842static void ath10k_tx_h_qos_workaround(struct ieee80211_hw *hw,
1843 struct ieee80211_tx_control *control,
1844 struct sk_buff *skb)
1845{
1846 struct ieee80211_hdr *hdr = (void *)skb->data;
1847 u8 *qos_ctl;
1848
1849 if (!ieee80211_is_data_qos(hdr->frame_control))
1850 return;
1851
1852 qos_ctl = ieee80211_get_qos_ctl(hdr);
Michal Kaziorba0ccd72013-07-22 14:25:28 +02001853 memmove(skb->data + IEEE80211_QOS_CTL_LEN,
1854 skb->data, (void *)qos_ctl - (void *)skb->data);
1855 skb_pull(skb, IEEE80211_QOS_CTL_LEN);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001856}
1857
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001858static void ath10k_tx_wep_key_work(struct work_struct *work)
1859{
1860 struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
1861 wep_key_work);
1862 int ret, keyidx = arvif->def_wep_key_newidx;
1863
1864 if (arvif->def_wep_key_idx == keyidx)
1865 return;
1866
1867 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
1868 arvif->vdev_id, keyidx);
1869
1870 ret = ath10k_wmi_vdev_set_param(arvif->ar,
1871 arvif->vdev_id,
1872 arvif->ar->wmi.vdev_param->def_keyid,
1873 keyidx);
1874 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02001875 ath10k_warn("failed to update wep key index for vdev %d: %d\n",
1876 arvif->vdev_id,
1877 ret);
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001878 return;
1879 }
1880
1881 arvif->def_wep_key_idx = keyidx;
1882}
1883
Kalle Valo5e3dd152013-06-12 20:52:10 +03001884static void ath10k_tx_h_update_wep_key(struct sk_buff *skb)
1885{
1886 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1887 struct ieee80211_vif *vif = info->control.vif;
1888 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1889 struct ath10k *ar = arvif->ar;
1890 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1891 struct ieee80211_key_conf *key = info->control.hw_key;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001892
Kalle Valo5e3dd152013-06-12 20:52:10 +03001893 if (!ieee80211_has_protected(hdr->frame_control))
1894 return;
1895
1896 if (!key)
1897 return;
1898
1899 if (key->cipher != WLAN_CIPHER_SUITE_WEP40 &&
1900 key->cipher != WLAN_CIPHER_SUITE_WEP104)
1901 return;
1902
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001903 if (key->keyidx == arvif->def_wep_key_idx)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001904 return;
1905
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001906 /* FIXME: Most likely a few frames will be TXed with an old key. Simply
1907 * queueing frames until key index is updated is not an option because
1908 * sk_buff may need more processing to be done, e.g. offchannel */
1909 arvif->def_wep_key_newidx = key->keyidx;
1910 ieee80211_queue_work(ar->hw, &arvif->wep_key_work);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001911}
1912
1913static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar, struct sk_buff *skb)
1914{
1915 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1916 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1917 struct ieee80211_vif *vif = info->control.vif;
1918 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1919
1920 /* This is case only for P2P_GO */
1921 if (arvif->vdev_type != WMI_VDEV_TYPE_AP ||
1922 arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
1923 return;
1924
1925 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
1926 spin_lock_bh(&ar->data_lock);
1927 if (arvif->u.ap.noa_data)
1928 if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
1929 GFP_ATOMIC))
1930 memcpy(skb_put(skb, arvif->u.ap.noa_len),
1931 arvif->u.ap.noa_data,
1932 arvif->u.ap.noa_len);
1933 spin_unlock_bh(&ar->data_lock);
1934 }
1935}
1936
1937static void ath10k_tx_htt(struct ath10k *ar, struct sk_buff *skb)
1938{
1939 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Bartosz Markowski5e00d312013-09-26 17:47:12 +02001940 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001941
Michal Kazior961d4c32013-08-09 10:13:34 +02001942 if (ar->htt.target_version_major >= 3) {
1943 /* Since HTT 3.0 there is no separate mgmt tx command */
1944 ret = ath10k_htt_tx(&ar->htt, skb);
1945 goto exit;
1946 }
1947
Bartosz Markowski5e00d312013-09-26 17:47:12 +02001948 if (ieee80211_is_mgmt(hdr->frame_control)) {
1949 if (test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
1950 ar->fw_features)) {
1951 if (skb_queue_len(&ar->wmi_mgmt_tx_queue) >=
1952 ATH10K_MAX_NUM_MGMT_PENDING) {
Kalle Valobe6546f2014-03-25 14:18:51 +02001953 ath10k_warn("reached WMI management tranmist queue limit\n");
Bartosz Markowski5e00d312013-09-26 17:47:12 +02001954 ret = -EBUSY;
1955 goto exit;
1956 }
1957
1958 skb_queue_tail(&ar->wmi_mgmt_tx_queue, skb);
1959 ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
1960 } else {
1961 ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
1962 }
1963 } else if (!test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
1964 ar->fw_features) &&
1965 ieee80211_is_nullfunc(hdr->frame_control)) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001966 /* FW does not report tx status properly for NullFunc frames
1967 * unless they are sent through mgmt tx path. mac80211 sends
Bartosz Markowski5e00d312013-09-26 17:47:12 +02001968 * those frames when it detects link/beacon loss and depends
1969 * on the tx status to be correct. */
Michal Kazioredb82362013-07-05 16:15:14 +03001970 ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02001971 } else {
Michal Kazioredb82362013-07-05 16:15:14 +03001972 ret = ath10k_htt_tx(&ar->htt, skb);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02001973 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001974
Michal Kazior961d4c32013-08-09 10:13:34 +02001975exit:
Kalle Valo5e3dd152013-06-12 20:52:10 +03001976 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02001977 ath10k_warn("failed to transmit packet, dropping: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001978 ieee80211_free_txskb(ar->hw, skb);
1979 }
1980}
1981
1982void ath10k_offchan_tx_purge(struct ath10k *ar)
1983{
1984 struct sk_buff *skb;
1985
1986 for (;;) {
1987 skb = skb_dequeue(&ar->offchan_tx_queue);
1988 if (!skb)
1989 break;
1990
1991 ieee80211_free_txskb(ar->hw, skb);
1992 }
1993}
1994
1995void ath10k_offchan_tx_work(struct work_struct *work)
1996{
1997 struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
1998 struct ath10k_peer *peer;
1999 struct ieee80211_hdr *hdr;
2000 struct sk_buff *skb;
2001 const u8 *peer_addr;
2002 int vdev_id;
2003 int ret;
2004
2005 /* FW requirement: We must create a peer before FW will send out
2006 * an offchannel frame. Otherwise the frame will be stuck and
2007 * never transmitted. We delete the peer upon tx completion.
2008 * It is unlikely that a peer for offchannel tx will already be
2009 * present. However it may be in some rare cases so account for that.
2010 * Otherwise we might remove a legitimate peer and break stuff. */
2011
2012 for (;;) {
2013 skb = skb_dequeue(&ar->offchan_tx_queue);
2014 if (!skb)
2015 break;
2016
2017 mutex_lock(&ar->conf_mutex);
2018
Kalle Valo60c3daa2013-09-08 17:56:07 +03002019 ath10k_dbg(ATH10K_DBG_MAC, "mac offchannel skb %p\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002020 skb);
2021
2022 hdr = (struct ieee80211_hdr *)skb->data;
2023 peer_addr = ieee80211_get_DA(hdr);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002024 vdev_id = ATH10K_SKB_CB(skb)->vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002025
2026 spin_lock_bh(&ar->data_lock);
2027 peer = ath10k_peer_find(ar, vdev_id, peer_addr);
2028 spin_unlock_bh(&ar->data_lock);
2029
2030 if (peer)
Kalle Valo60c3daa2013-09-08 17:56:07 +03002031 /* FIXME: should this use ath10k_warn()? */
Kalle Valo5e3dd152013-06-12 20:52:10 +03002032 ath10k_dbg(ATH10K_DBG_MAC, "peer %pM on vdev %d already present\n",
2033 peer_addr, vdev_id);
2034
2035 if (!peer) {
2036 ret = ath10k_peer_create(ar, vdev_id, peer_addr);
2037 if (ret)
Kalle Valobe6546f2014-03-25 14:18:51 +02002038 ath10k_warn("failed to create peer %pM on vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002039 peer_addr, vdev_id, ret);
2040 }
2041
2042 spin_lock_bh(&ar->data_lock);
Wolfram Sang16735d02013-11-14 14:32:02 -08002043 reinit_completion(&ar->offchan_tx_completed);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002044 ar->offchan_tx_skb = skb;
2045 spin_unlock_bh(&ar->data_lock);
2046
2047 ath10k_tx_htt(ar, skb);
2048
2049 ret = wait_for_completion_timeout(&ar->offchan_tx_completed,
2050 3 * HZ);
2051 if (ret <= 0)
2052 ath10k_warn("timed out waiting for offchannel skb %p\n",
2053 skb);
2054
2055 if (!peer) {
2056 ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
2057 if (ret)
Kalle Valobe6546f2014-03-25 14:18:51 +02002058 ath10k_warn("failed to delete peer %pM on vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002059 peer_addr, vdev_id, ret);
2060 }
2061
2062 mutex_unlock(&ar->conf_mutex);
2063 }
2064}
2065
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002066void ath10k_mgmt_over_wmi_tx_purge(struct ath10k *ar)
2067{
2068 struct sk_buff *skb;
2069
2070 for (;;) {
2071 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
2072 if (!skb)
2073 break;
2074
2075 ieee80211_free_txskb(ar->hw, skb);
2076 }
2077}
2078
2079void ath10k_mgmt_over_wmi_tx_work(struct work_struct *work)
2080{
2081 struct ath10k *ar = container_of(work, struct ath10k, wmi_mgmt_tx_work);
2082 struct sk_buff *skb;
2083 int ret;
2084
2085 for (;;) {
2086 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
2087 if (!skb)
2088 break;
2089
2090 ret = ath10k_wmi_mgmt_tx(ar, skb);
Michal Kazior5fb5e412013-10-28 07:18:13 +01002091 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02002092 ath10k_warn("failed to transmit management frame via WMI: %d\n",
2093 ret);
Michal Kazior5fb5e412013-10-28 07:18:13 +01002094 ieee80211_free_txskb(ar->hw, skb);
2095 }
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002096 }
2097}
2098
Kalle Valo5e3dd152013-06-12 20:52:10 +03002099/************/
2100/* Scanning */
2101/************/
2102
2103/*
2104 * This gets called if we dont get a heart-beat during scan.
2105 * This may indicate the FW has hung and we need to abort the
2106 * scan manually to prevent cancel_hw_scan() from deadlocking
2107 */
2108void ath10k_reset_scan(unsigned long ptr)
2109{
2110 struct ath10k *ar = (struct ath10k *)ptr;
2111
2112 spin_lock_bh(&ar->data_lock);
2113 if (!ar->scan.in_progress) {
2114 spin_unlock_bh(&ar->data_lock);
2115 return;
2116 }
2117
Kalle Valobe6546f2014-03-25 14:18:51 +02002118 ath10k_warn("scan timed out, firmware problem?\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002119
2120 if (ar->scan.is_roc)
2121 ieee80211_remain_on_channel_expired(ar->hw);
2122 else
2123 ieee80211_scan_completed(ar->hw, 1 /* aborted */);
2124
2125 ar->scan.in_progress = false;
2126 complete_all(&ar->scan.completed);
2127 spin_unlock_bh(&ar->data_lock);
2128}
2129
2130static int ath10k_abort_scan(struct ath10k *ar)
2131{
2132 struct wmi_stop_scan_arg arg = {
2133 .req_id = 1, /* FIXME */
2134 .req_type = WMI_SCAN_STOP_ONE,
2135 .u.scan_id = ATH10K_SCAN_ID,
2136 };
2137 int ret;
2138
2139 lockdep_assert_held(&ar->conf_mutex);
2140
2141 del_timer_sync(&ar->scan.timeout);
2142
2143 spin_lock_bh(&ar->data_lock);
2144 if (!ar->scan.in_progress) {
2145 spin_unlock_bh(&ar->data_lock);
2146 return 0;
2147 }
2148
2149 ar->scan.aborting = true;
2150 spin_unlock_bh(&ar->data_lock);
2151
2152 ret = ath10k_wmi_stop_scan(ar, &arg);
2153 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02002154 ath10k_warn("failed to stop wmi scan: %d\n", ret);
Michal Kazioradb8c9b2013-07-05 16:15:16 +03002155 spin_lock_bh(&ar->data_lock);
2156 ar->scan.in_progress = false;
2157 ath10k_offchan_tx_purge(ar);
2158 spin_unlock_bh(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002159 return -EIO;
2160 }
2161
Kalle Valo5e3dd152013-06-12 20:52:10 +03002162 ret = wait_for_completion_timeout(&ar->scan.completed, 3*HZ);
2163 if (ret == 0)
2164 ath10k_warn("timed out while waiting for scan to stop\n");
2165
2166 /* scan completion may be done right after we timeout here, so let's
2167 * check the in_progress and tell mac80211 scan is completed. if we
2168 * don't do that and FW fails to send us scan completion indication
2169 * then userspace won't be able to scan anymore */
2170 ret = 0;
2171
2172 spin_lock_bh(&ar->data_lock);
2173 if (ar->scan.in_progress) {
Kalle Valobe6546f2014-03-25 14:18:51 +02002174 ath10k_warn("failed to stop scan, it's still in progress\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002175 ar->scan.in_progress = false;
2176 ath10k_offchan_tx_purge(ar);
2177 ret = -ETIMEDOUT;
2178 }
2179 spin_unlock_bh(&ar->data_lock);
2180
2181 return ret;
2182}
2183
2184static int ath10k_start_scan(struct ath10k *ar,
2185 const struct wmi_start_scan_arg *arg)
2186{
2187 int ret;
2188
2189 lockdep_assert_held(&ar->conf_mutex);
2190
2191 ret = ath10k_wmi_start_scan(ar, arg);
2192 if (ret)
2193 return ret;
2194
Kalle Valo5e3dd152013-06-12 20:52:10 +03002195 ret = wait_for_completion_timeout(&ar->scan.started, 1*HZ);
2196 if (ret == 0) {
2197 ath10k_abort_scan(ar);
2198 return ret;
2199 }
2200
2201 /* the scan can complete earlier, before we even
2202 * start the timer. in that case the timer handler
2203 * checks ar->scan.in_progress and bails out if its
2204 * false. Add a 200ms margin to account event/command
2205 * processing. */
2206 mod_timer(&ar->scan.timeout, jiffies +
2207 msecs_to_jiffies(arg->max_scan_time+200));
2208 return 0;
2209}
2210
2211/**********************/
2212/* mac80211 callbacks */
2213/**********************/
2214
2215static void ath10k_tx(struct ieee80211_hw *hw,
2216 struct ieee80211_tx_control *control,
2217 struct sk_buff *skb)
2218{
2219 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2220 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2221 struct ath10k *ar = hw->priv;
Michal Kaziorddb6ad72013-10-02 11:03:39 +02002222 u8 tid, vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002223
2224 /* We should disable CCK RATE due to P2P */
2225 if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
2226 ath10k_dbg(ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
2227
2228 /* we must calculate tid before we apply qos workaround
2229 * as we'd lose the qos control field */
Michal Kazior42c3aa62013-10-02 11:03:38 +02002230 tid = ath10k_tx_h_get_tid(hdr);
Michal Kaziorddb6ad72013-10-02 11:03:39 +02002231 vdev_id = ath10k_tx_h_get_vdev_id(ar, info);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002232
Michal Kaziorcf84bd42013-07-16 11:04:54 +02002233 /* it makes no sense to process injected frames like that */
2234 if (info->control.vif &&
2235 info->control.vif->type != NL80211_IFTYPE_MONITOR) {
2236 ath10k_tx_h_qos_workaround(hw, control, skb);
2237 ath10k_tx_h_update_wep_key(skb);
2238 ath10k_tx_h_add_p2p_noa_ie(ar, skb);
2239 ath10k_tx_h_seq_no(skb);
2240 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002241
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002242 ATH10K_SKB_CB(skb)->vdev_id = vdev_id;
Michal Kazior27bb1782013-09-18 14:43:19 +02002243 ATH10K_SKB_CB(skb)->htt.is_offchan = false;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002244 ATH10K_SKB_CB(skb)->htt.tid = tid;
2245
2246 if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
2247 spin_lock_bh(&ar->data_lock);
2248 ATH10K_SKB_CB(skb)->htt.is_offchan = true;
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002249 ATH10K_SKB_CB(skb)->vdev_id = ar->scan.vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002250 spin_unlock_bh(&ar->data_lock);
2251
2252 ath10k_dbg(ATH10K_DBG_MAC, "queued offchannel skb %p\n", skb);
2253
2254 skb_queue_tail(&ar->offchan_tx_queue, skb);
2255 ieee80211_queue_work(hw, &ar->offchan_tx_work);
2256 return;
2257 }
2258
2259 ath10k_tx_htt(ar, skb);
2260}
2261
2262/*
2263 * Initialize various parameters with default vaules.
2264 */
Michal Kazioraffd3212013-07-16 09:54:35 +02002265void ath10k_halt(struct ath10k *ar)
Michal Kazior818bdd12013-07-16 09:38:57 +02002266{
2267 lockdep_assert_held(&ar->conf_mutex);
2268
Marek Puzyniake8a50f82013-11-20 09:59:47 +02002269 ath10k_stop_cac(ar);
Michal Kazior818bdd12013-07-16 09:38:57 +02002270 del_timer_sync(&ar->scan.timeout);
2271 ath10k_offchan_tx_purge(ar);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002272 ath10k_mgmt_over_wmi_tx_purge(ar);
Michal Kazior818bdd12013-07-16 09:38:57 +02002273 ath10k_peer_cleanup_all(ar);
2274 ath10k_core_stop(ar);
2275 ath10k_hif_power_down(ar);
2276
2277 spin_lock_bh(&ar->data_lock);
2278 if (ar->scan.in_progress) {
2279 del_timer(&ar->scan.timeout);
2280 ar->scan.in_progress = false;
2281 ieee80211_scan_completed(ar->hw, true);
2282 }
2283 spin_unlock_bh(&ar->data_lock);
2284}
2285
Kalle Valo5e3dd152013-06-12 20:52:10 +03002286static int ath10k_start(struct ieee80211_hw *hw)
2287{
2288 struct ath10k *ar = hw->priv;
Michal Kazior818bdd12013-07-16 09:38:57 +02002289 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002290
Michal Kazior548db542013-07-05 16:15:15 +03002291 mutex_lock(&ar->conf_mutex);
2292
Michal Kazioraffd3212013-07-16 09:54:35 +02002293 if (ar->state != ATH10K_STATE_OFF &&
2294 ar->state != ATH10K_STATE_RESTARTING) {
Michal Kazior818bdd12013-07-16 09:38:57 +02002295 ret = -EINVAL;
2296 goto exit;
2297 }
2298
2299 ret = ath10k_hif_power_up(ar);
2300 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02002301 ath10k_err("Could not init hif: %d\n", ret);
Michal Kazior818bdd12013-07-16 09:38:57 +02002302 ar->state = ATH10K_STATE_OFF;
2303 goto exit;
2304 }
2305
2306 ret = ath10k_core_start(ar);
2307 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02002308 ath10k_err("Could not init core: %d\n", ret);
Michal Kazior818bdd12013-07-16 09:38:57 +02002309 ath10k_hif_power_down(ar);
2310 ar->state = ATH10K_STATE_OFF;
2311 goto exit;
2312 }
2313
Michal Kazioraffd3212013-07-16 09:54:35 +02002314 if (ar->state == ATH10K_STATE_OFF)
2315 ar->state = ATH10K_STATE_ON;
2316 else if (ar->state == ATH10K_STATE_RESTARTING)
2317 ar->state = ATH10K_STATE_RESTARTED;
2318
Bartosz Markowski226a3392013-09-26 17:47:16 +02002319 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->pmf_qos, 1);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002320 if (ret)
Kalle Valobe6546f2014-03-25 14:18:51 +02002321 ath10k_warn("failed to enable PMF QOS: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002322
Michal Kaziorc4dd0d02013-11-13 11:05:10 +01002323 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->dynamic_bw, 1);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002324 if (ret)
Kalle Valobe6546f2014-03-25 14:18:51 +02002325 ath10k_warn("failed to enable dynamic BW: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002326
Marek Puzyniakab6258e2014-01-29 15:03:31 +02002327 /*
2328 * By default FW set ARP frames ac to voice (6). In that case ARP
2329 * exchange is not working properly for UAPSD enabled AP. ARP requests
2330 * which arrives with access category 0 are processed by network stack
2331 * and send back with access category 0, but FW changes access category
2332 * to 6. Set ARP frames access category to best effort (0) solves
2333 * this problem.
2334 */
2335
2336 ret = ath10k_wmi_pdev_set_param(ar,
2337 ar->wmi.pdev_param->arp_ac_override, 0);
2338 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02002339 ath10k_warn("failed to set arp ac override parameter: %d\n",
Marek Puzyniakab6258e2014-01-29 15:03:31 +02002340 ret);
2341 goto exit;
2342 }
2343
Michal Kaziorf7843d72013-07-16 09:38:52 +02002344 ath10k_regd_update(ar);
Michal Kaziorc60bdd82014-01-29 07:26:31 +01002345 ret = 0;
Michal Kaziorf7843d72013-07-16 09:38:52 +02002346
Michal Kazior818bdd12013-07-16 09:38:57 +02002347exit:
Michal Kazior548db542013-07-05 16:15:15 +03002348 mutex_unlock(&ar->conf_mutex);
Michal Kaziorc60bdd82014-01-29 07:26:31 +01002349 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002350}
2351
2352static void ath10k_stop(struct ieee80211_hw *hw)
2353{
2354 struct ath10k *ar = hw->priv;
2355
Michal Kazior548db542013-07-05 16:15:15 +03002356 mutex_lock(&ar->conf_mutex);
Michal Kazioraffd3212013-07-16 09:54:35 +02002357 if (ar->state == ATH10K_STATE_ON ||
2358 ar->state == ATH10K_STATE_RESTARTED ||
2359 ar->state == ATH10K_STATE_WEDGED)
Michal Kazior818bdd12013-07-16 09:38:57 +02002360 ath10k_halt(ar);
Michal Kaziora96d7742013-07-16 09:38:56 +02002361
Michal Kaziorf7843d72013-07-16 09:38:52 +02002362 ar->state = ATH10K_STATE_OFF;
Michal Kazior548db542013-07-05 16:15:15 +03002363 mutex_unlock(&ar->conf_mutex);
2364
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002365 ath10k_mgmt_over_wmi_tx_purge(ar);
2366
Michal Kazior548db542013-07-05 16:15:15 +03002367 cancel_work_sync(&ar->offchan_tx_work);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002368 cancel_work_sync(&ar->wmi_mgmt_tx_work);
Michal Kazioraffd3212013-07-16 09:54:35 +02002369 cancel_work_sync(&ar->restart_work);
2370}
2371
Michal Kaziorad088bf2013-10-16 15:44:46 +03002372static int ath10k_config_ps(struct ath10k *ar)
Michal Kazioraffd3212013-07-16 09:54:35 +02002373{
Michal Kaziorad088bf2013-10-16 15:44:46 +03002374 struct ath10k_vif *arvif;
2375 int ret = 0;
Michal Kazioraffd3212013-07-16 09:54:35 +02002376
2377 lockdep_assert_held(&ar->conf_mutex);
2378
Michal Kaziorad088bf2013-10-16 15:44:46 +03002379 list_for_each_entry(arvif, &ar->arvifs, list) {
2380 ret = ath10k_mac_vif_setup_ps(arvif);
2381 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02002382 ath10k_warn("failed to setup powersave: %d\n", ret);
Michal Kaziorad088bf2013-10-16 15:44:46 +03002383 break;
2384 }
2385 }
Michal Kazioraffd3212013-07-16 09:54:35 +02002386
Michal Kaziorad088bf2013-10-16 15:44:46 +03002387 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002388}
2389
Michal Kaziorc930f742014-01-23 11:38:25 +01002390static const char *chandef_get_width(enum nl80211_chan_width width)
2391{
2392 switch (width) {
2393 case NL80211_CHAN_WIDTH_20_NOHT:
2394 return "20 (noht)";
2395 case NL80211_CHAN_WIDTH_20:
2396 return "20";
2397 case NL80211_CHAN_WIDTH_40:
2398 return "40";
2399 case NL80211_CHAN_WIDTH_80:
2400 return "80";
2401 case NL80211_CHAN_WIDTH_80P80:
2402 return "80+80";
2403 case NL80211_CHAN_WIDTH_160:
2404 return "160";
2405 case NL80211_CHAN_WIDTH_5:
2406 return "5";
2407 case NL80211_CHAN_WIDTH_10:
2408 return "10";
2409 }
2410 return "?";
2411}
2412
2413static void ath10k_config_chan(struct ath10k *ar)
2414{
2415 struct ath10k_vif *arvif;
2416 bool monitor_was_enabled;
2417 int ret;
2418
2419 lockdep_assert_held(&ar->conf_mutex);
2420
2421 ath10k_dbg(ATH10K_DBG_MAC,
2422 "mac config channel to %dMHz (cf1 %dMHz cf2 %dMHz width %s)\n",
2423 ar->chandef.chan->center_freq,
2424 ar->chandef.center_freq1,
2425 ar->chandef.center_freq2,
2426 chandef_get_width(ar->chandef.width));
2427
2428 /* First stop monitor interface. Some FW versions crash if there's a
2429 * lone monitor interface. */
2430 monitor_was_enabled = ar->monitor_enabled;
2431
2432 if (ar->monitor_enabled)
2433 ath10k_monitor_stop(ar);
2434
2435 list_for_each_entry(arvif, &ar->arvifs, list) {
2436 if (!arvif->is_started)
2437 continue;
2438
2439 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
2440 continue;
2441
2442 ret = ath10k_vdev_stop(arvif);
2443 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02002444 ath10k_warn("failed to stop vdev %d: %d\n",
Michal Kaziorc930f742014-01-23 11:38:25 +01002445 arvif->vdev_id, ret);
2446 continue;
2447 }
2448 }
2449
2450 /* all vdevs are now stopped - now attempt to restart them */
2451
2452 list_for_each_entry(arvif, &ar->arvifs, list) {
2453 if (!arvif->is_started)
2454 continue;
2455
2456 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
2457 continue;
2458
2459 ret = ath10k_vdev_start(arvif);
2460 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02002461 ath10k_warn("failed to start vdev %d: %d\n",
Michal Kaziorc930f742014-01-23 11:38:25 +01002462 arvif->vdev_id, ret);
2463 continue;
2464 }
2465
2466 if (!arvif->is_up)
2467 continue;
2468
2469 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
2470 arvif->bssid);
2471 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02002472 ath10k_warn("failed to bring vdev up %d: %d\n",
Michal Kaziorc930f742014-01-23 11:38:25 +01002473 arvif->vdev_id, ret);
2474 continue;
2475 }
2476 }
2477
2478 if (monitor_was_enabled)
2479 ath10k_monitor_start(ar, ar->monitor_vdev_id);
2480}
2481
Kalle Valo5e3dd152013-06-12 20:52:10 +03002482static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
2483{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002484 struct ath10k *ar = hw->priv;
2485 struct ieee80211_conf *conf = &hw->conf;
2486 int ret = 0;
Michal Kazior5474efe2013-10-23 04:02:15 -07002487 u32 param;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002488
2489 mutex_lock(&ar->conf_mutex);
2490
2491 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
Marek Puzyniake8a50f82013-11-20 09:59:47 +02002492 ath10k_dbg(ATH10K_DBG_MAC,
2493 "mac config channel %d mhz flags 0x%x\n",
2494 conf->chandef.chan->center_freq,
2495 conf->chandef.chan->flags);
2496
Kalle Valo5e3dd152013-06-12 20:52:10 +03002497 spin_lock_bh(&ar->data_lock);
2498 ar->rx_channel = conf->chandef.chan;
2499 spin_unlock_bh(&ar->data_lock);
Marek Puzyniake8a50f82013-11-20 09:59:47 +02002500
2501 ath10k_config_radar_detection(ar);
Michal Kaziorc930f742014-01-23 11:38:25 +01002502
2503 if (!cfg80211_chandef_identical(&ar->chandef, &conf->chandef)) {
2504 ar->chandef = conf->chandef;
2505 ath10k_config_chan(ar);
2506 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002507 }
2508
Michal Kazior5474efe2013-10-23 04:02:15 -07002509 if (changed & IEEE80211_CONF_CHANGE_POWER) {
2510 ath10k_dbg(ATH10K_DBG_MAC, "mac config power %d\n",
2511 hw->conf.power_level);
2512
2513 param = ar->wmi.pdev_param->txpower_limit2g;
2514 ret = ath10k_wmi_pdev_set_param(ar, param,
2515 hw->conf.power_level * 2);
2516 if (ret)
Kalle Valobe6546f2014-03-25 14:18:51 +02002517 ath10k_warn("failed to set 2g txpower %d: %d\n",
Michal Kazior5474efe2013-10-23 04:02:15 -07002518 hw->conf.power_level, ret);
2519
2520 param = ar->wmi.pdev_param->txpower_limit5g;
2521 ret = ath10k_wmi_pdev_set_param(ar, param,
2522 hw->conf.power_level * 2);
2523 if (ret)
Kalle Valobe6546f2014-03-25 14:18:51 +02002524 ath10k_warn("failed to set 5g txpower %d: %d\n",
Michal Kazior5474efe2013-10-23 04:02:15 -07002525 hw->conf.power_level, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002526 }
2527
Michal Kazioraffd3212013-07-16 09:54:35 +02002528 if (changed & IEEE80211_CONF_CHANGE_PS)
2529 ath10k_config_ps(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002530
2531 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
2532 if (conf->flags & IEEE80211_CONF_MONITOR)
2533 ret = ath10k_monitor_create(ar);
2534 else
2535 ret = ath10k_monitor_destroy(ar);
2536 }
2537
2538 mutex_unlock(&ar->conf_mutex);
2539 return ret;
2540}
2541
2542/*
2543 * TODO:
2544 * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
2545 * because we will send mgmt frames without CCK. This requirement
2546 * for P2P_FIND/GO_NEG should be handled by checking CCK flag
2547 * in the TX packet.
2548 */
2549static int ath10k_add_interface(struct ieee80211_hw *hw,
2550 struct ieee80211_vif *vif)
2551{
2552 struct ath10k *ar = hw->priv;
2553 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2554 enum wmi_sta_powersave_param param;
2555 int ret = 0;
Kalle Valo5a13e762014-01-20 11:01:46 +02002556 u32 value;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002557 int bit;
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02002558 u32 vdev_param;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002559
2560 mutex_lock(&ar->conf_mutex);
2561
Michal Kazior0dbd09e2013-07-31 10:55:14 +02002562 memset(arvif, 0, sizeof(*arvif));
2563
Kalle Valo5e3dd152013-06-12 20:52:10 +03002564 arvif->ar = ar;
2565 arvif->vif = vif;
2566
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002567 INIT_WORK(&arvif->wep_key_work, ath10k_tx_wep_key_work);
Ben Greeare63b33f2013-10-22 14:54:14 -07002568 INIT_LIST_HEAD(&arvif->list);
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002569
Kalle Valo5e3dd152013-06-12 20:52:10 +03002570 if ((vif->type == NL80211_IFTYPE_MONITOR) && ar->monitor_present) {
Kalle Valobe6546f2014-03-25 14:18:51 +02002571 ath10k_warn("only one monitor interface allowed\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002572 ret = -EBUSY;
Michal Kazior9dad14ae2013-10-16 15:44:45 +03002573 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002574 }
2575
2576 bit = ffs(ar->free_vdev_map);
2577 if (bit == 0) {
2578 ret = -EBUSY;
Michal Kazior9dad14ae2013-10-16 15:44:45 +03002579 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002580 }
2581
2582 arvif->vdev_id = bit - 1;
2583 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002584
2585 if (ar->p2p)
2586 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
2587
2588 switch (vif->type) {
2589 case NL80211_IFTYPE_UNSPECIFIED:
2590 case NL80211_IFTYPE_STATION:
2591 arvif->vdev_type = WMI_VDEV_TYPE_STA;
2592 if (vif->p2p)
2593 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
2594 break;
2595 case NL80211_IFTYPE_ADHOC:
2596 arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
2597 break;
2598 case NL80211_IFTYPE_AP:
2599 arvif->vdev_type = WMI_VDEV_TYPE_AP;
2600
2601 if (vif->p2p)
2602 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
2603 break;
2604 case NL80211_IFTYPE_MONITOR:
2605 arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
2606 break;
2607 default:
2608 WARN_ON(1);
2609 break;
2610 }
2611
Kalle Valo60c3daa2013-09-08 17:56:07 +03002612 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002613 arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype);
2614
2615 ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
2616 arvif->vdev_subtype, vif->addr);
2617 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02002618 ath10k_warn("failed to create WMI vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02002619 arvif->vdev_id, ret);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03002620 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002621 }
2622
Michal Kazior9dad14ae2013-10-16 15:44:45 +03002623 ar->free_vdev_map &= ~BIT(arvif->vdev_id);
Michal Kazior05791192013-10-16 15:44:45 +03002624 list_add(&arvif->list, &ar->arvifs);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03002625
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02002626 vdev_param = ar->wmi.vdev_param->def_keyid;
2627 ret = ath10k_wmi_vdev_set_param(ar, 0, vdev_param,
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002628 arvif->def_wep_key_idx);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03002629 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02002630 ath10k_warn("failed to set vdev %i default key id: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02002631 arvif->vdev_id, ret);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03002632 goto err_vdev_delete;
2633 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002634
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02002635 vdev_param = ar->wmi.vdev_param->tx_encap_type;
2636 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002637 ATH10K_HW_TXRX_NATIVE_WIFI);
Bartosz Markowskiebc9abd2013-10-15 09:26:20 +02002638 /* 10.X firmware does not support this VDEV parameter. Do not warn */
Michal Kazior9dad14ae2013-10-16 15:44:45 +03002639 if (ret && ret != -EOPNOTSUPP) {
Kalle Valobe6546f2014-03-25 14:18:51 +02002640 ath10k_warn("failed to set vdev %i TX encapsulation: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02002641 arvif->vdev_id, ret);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03002642 goto err_vdev_delete;
2643 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002644
2645 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
2646 ret = ath10k_peer_create(ar, arvif->vdev_id, vif->addr);
2647 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02002648 ath10k_warn("failed to create vdev %i peer for AP: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02002649 arvif->vdev_id, ret);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03002650 goto err_vdev_delete;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002651 }
Marek Puzyniakcdf07402013-12-30 09:07:51 +01002652
Kalle Valo5a13e762014-01-20 11:01:46 +02002653 ret = ath10k_mac_set_kickout(arvif);
2654 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02002655 ath10k_warn("failed to set vdev %i kickout parameters: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02002656 arvif->vdev_id, ret);
Kalle Valo5a13e762014-01-20 11:01:46 +02002657 goto err_peer_delete;
2658 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002659 }
2660
2661 if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
2662 param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
2663 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
2664 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
2665 param, value);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03002666 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02002667 ath10k_warn("failed to set vdev %i RX wake policy: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02002668 arvif->vdev_id, ret);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03002669 goto err_peer_delete;
2670 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002671
2672 param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
2673 value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
2674 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
2675 param, value);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03002676 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02002677 ath10k_warn("failed to set vdev %i TX wake thresh: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02002678 arvif->vdev_id, ret);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03002679 goto err_peer_delete;
2680 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002681
2682 param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
2683 value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
2684 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
2685 param, value);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03002686 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02002687 ath10k_warn("failed to set vdev %i PSPOLL count: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02002688 arvif->vdev_id, ret);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03002689 goto err_peer_delete;
2690 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002691 }
2692
Michal Kazior424121c2013-07-22 14:13:31 +02002693 ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03002694 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02002695 ath10k_warn("failed to set rts threshold for vdev %d: %d\n",
Michal Kazior679c54a2013-07-05 16:15:04 +03002696 arvif->vdev_id, ret);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03002697 goto err_peer_delete;
2698 }
Michal Kazior679c54a2013-07-05 16:15:04 +03002699
Michal Kazior424121c2013-07-22 14:13:31 +02002700 ret = ath10k_mac_set_frag(arvif, ar->hw->wiphy->frag_threshold);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03002701 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02002702 ath10k_warn("failed to set frag threshold for vdev %d: %d\n",
Michal Kazior679c54a2013-07-05 16:15:04 +03002703 arvif->vdev_id, ret);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03002704 goto err_peer_delete;
2705 }
Michal Kazior679c54a2013-07-05 16:15:04 +03002706
Kalle Valo5e3dd152013-06-12 20:52:10 +03002707 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
2708 ar->monitor_present = true;
2709
Kalle Valo5e3dd152013-06-12 20:52:10 +03002710 mutex_unlock(&ar->conf_mutex);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03002711 return 0;
2712
2713err_peer_delete:
2714 if (arvif->vdev_type == WMI_VDEV_TYPE_AP)
2715 ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
2716
2717err_vdev_delete:
2718 ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
2719 ar->free_vdev_map &= ~BIT(arvif->vdev_id);
Michal Kazior05791192013-10-16 15:44:45 +03002720 list_del(&arvif->list);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03002721
2722err:
2723 mutex_unlock(&ar->conf_mutex);
2724
Kalle Valo5e3dd152013-06-12 20:52:10 +03002725 return ret;
2726}
2727
2728static void ath10k_remove_interface(struct ieee80211_hw *hw,
2729 struct ieee80211_vif *vif)
2730{
2731 struct ath10k *ar = hw->priv;
2732 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2733 int ret;
2734
2735 mutex_lock(&ar->conf_mutex);
2736
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002737 cancel_work_sync(&arvif->wep_key_work);
2738
Michal Kaziored543882013-09-13 14:16:56 +02002739 spin_lock_bh(&ar->data_lock);
2740 if (arvif->beacon) {
2741 dev_kfree_skb_any(arvif->beacon);
2742 arvif->beacon = NULL;
2743 }
2744 spin_unlock_bh(&ar->data_lock);
2745
Kalle Valo5e3dd152013-06-12 20:52:10 +03002746 ar->free_vdev_map |= 1 << (arvif->vdev_id);
Michal Kazior05791192013-10-16 15:44:45 +03002747 list_del(&arvif->list);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002748
2749 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
2750 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, vif->addr);
2751 if (ret)
Kalle Valobe6546f2014-03-25 14:18:51 +02002752 ath10k_warn("failed to remove peer for AP vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02002753 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002754
2755 kfree(arvif->u.ap.noa_data);
2756 }
2757
Ben Greear69244e52014-02-27 18:50:00 +02002758 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %i delete (remove interface)\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03002759 arvif->vdev_id);
2760
Kalle Valo5e3dd152013-06-12 20:52:10 +03002761 ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
2762 if (ret)
Kalle Valobe6546f2014-03-25 14:18:51 +02002763 ath10k_warn("failed to delete WMI vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02002764 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002765
2766 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
2767 ar->monitor_present = false;
2768
2769 ath10k_peer_cleanup(ar, arvif->vdev_id);
2770
2771 mutex_unlock(&ar->conf_mutex);
2772}
2773
2774/*
2775 * FIXME: Has to be verified.
2776 */
2777#define SUPPORTED_FILTERS \
2778 (FIF_PROMISC_IN_BSS | \
2779 FIF_ALLMULTI | \
2780 FIF_CONTROL | \
2781 FIF_PSPOLL | \
2782 FIF_OTHER_BSS | \
2783 FIF_BCN_PRBRESP_PROMISC | \
2784 FIF_PROBE_REQ | \
2785 FIF_FCSFAIL)
2786
2787static void ath10k_configure_filter(struct ieee80211_hw *hw,
2788 unsigned int changed_flags,
2789 unsigned int *total_flags,
2790 u64 multicast)
2791{
2792 struct ath10k *ar = hw->priv;
2793 int ret;
2794
2795 mutex_lock(&ar->conf_mutex);
2796
2797 changed_flags &= SUPPORTED_FILTERS;
2798 *total_flags &= SUPPORTED_FILTERS;
2799 ar->filter_flags = *total_flags;
2800
Michal Kaziorafd09222013-10-16 16:45:41 +03002801 /* Monitor must not be started if it wasn't created first.
2802 * Promiscuous mode may be started on a non-monitor interface - in
2803 * such case the monitor vdev is not created so starting the
2804 * monitor makes no sense. Since ath10k uses no special RX filters
2805 * (only BSS filter in STA mode) there's no need for any special
2806 * action here. */
Kalle Valo5e3dd152013-06-12 20:52:10 +03002807 if ((ar->filter_flags & FIF_PROMISC_IN_BSS) &&
Michal Kaziorafd09222013-10-16 16:45:41 +03002808 !ar->monitor_enabled && ar->monitor_present) {
Kalle Valo60c3daa2013-09-08 17:56:07 +03002809 ath10k_dbg(ATH10K_DBG_MAC, "mac monitor %d start\n",
2810 ar->monitor_vdev_id);
2811
Kalle Valo5e3dd152013-06-12 20:52:10 +03002812 ret = ath10k_monitor_start(ar, ar->monitor_vdev_id);
2813 if (ret)
Kalle Valobe6546f2014-03-25 14:18:51 +02002814 ath10k_warn("failed to start monitor mode: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002815 } else if (!(ar->filter_flags & FIF_PROMISC_IN_BSS) &&
Michal Kaziorafd09222013-10-16 16:45:41 +03002816 ar->monitor_enabled && ar->monitor_present) {
Kalle Valo60c3daa2013-09-08 17:56:07 +03002817 ath10k_dbg(ATH10K_DBG_MAC, "mac monitor %d stop\n",
2818 ar->monitor_vdev_id);
2819
Kalle Valo5e3dd152013-06-12 20:52:10 +03002820 ret = ath10k_monitor_stop(ar);
2821 if (ret)
Kalle Valobe6546f2014-03-25 14:18:51 +02002822 ath10k_warn("failed to stop monitor mode: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002823 }
2824
2825 mutex_unlock(&ar->conf_mutex);
2826}
2827
2828static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
2829 struct ieee80211_vif *vif,
2830 struct ieee80211_bss_conf *info,
2831 u32 changed)
2832{
2833 struct ath10k *ar = hw->priv;
2834 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2835 int ret = 0;
Bartosz Markowski226a3392013-09-26 17:47:16 +02002836 u32 vdev_param, pdev_param;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002837
2838 mutex_lock(&ar->conf_mutex);
2839
2840 if (changed & BSS_CHANGED_IBSS)
2841 ath10k_control_ibss(arvif, info, vif->addr);
2842
2843 if (changed & BSS_CHANGED_BEACON_INT) {
2844 arvif->beacon_interval = info->beacon_int;
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02002845 vdev_param = ar->wmi.vdev_param->beacon_interval;
2846 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002847 arvif->beacon_interval);
Kalle Valo60c3daa2013-09-08 17:56:07 +03002848 ath10k_dbg(ATH10K_DBG_MAC,
2849 "mac vdev %d beacon_interval %d\n",
2850 arvif->vdev_id, arvif->beacon_interval);
2851
Kalle Valo5e3dd152013-06-12 20:52:10 +03002852 if (ret)
Kalle Valobe6546f2014-03-25 14:18:51 +02002853 ath10k_warn("failed to set beacon interval for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02002854 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002855 }
2856
2857 if (changed & BSS_CHANGED_BEACON) {
Kalle Valo60c3daa2013-09-08 17:56:07 +03002858 ath10k_dbg(ATH10K_DBG_MAC,
2859 "vdev %d set beacon tx mode to staggered\n",
2860 arvif->vdev_id);
2861
Bartosz Markowski226a3392013-09-26 17:47:16 +02002862 pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
2863 ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002864 WMI_BEACON_STAGGERED_MODE);
2865 if (ret)
Kalle Valobe6546f2014-03-25 14:18:51 +02002866 ath10k_warn("failed to set beacon mode for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02002867 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002868 }
2869
John W. Linvilleb70727e82013-06-13 13:34:29 -04002870 if (changed & BSS_CHANGED_BEACON_INFO) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002871 arvif->dtim_period = info->dtim_period;
2872
Kalle Valo60c3daa2013-09-08 17:56:07 +03002873 ath10k_dbg(ATH10K_DBG_MAC,
2874 "mac vdev %d dtim_period %d\n",
2875 arvif->vdev_id, arvif->dtim_period);
2876
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02002877 vdev_param = ar->wmi.vdev_param->dtim_period;
2878 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002879 arvif->dtim_period);
2880 if (ret)
Kalle Valobe6546f2014-03-25 14:18:51 +02002881 ath10k_warn("failed to set dtim period for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02002882 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002883 }
2884
2885 if (changed & BSS_CHANGED_SSID &&
2886 vif->type == NL80211_IFTYPE_AP) {
2887 arvif->u.ap.ssid_len = info->ssid_len;
2888 if (info->ssid_len)
2889 memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
2890 arvif->u.ap.hidden_ssid = info->hidden_ssid;
2891 }
2892
2893 if (changed & BSS_CHANGED_BSSID) {
2894 if (!is_zero_ether_addr(info->bssid)) {
Kalle Valo60c3daa2013-09-08 17:56:07 +03002895 ath10k_dbg(ATH10K_DBG_MAC,
2896 "mac vdev %d create peer %pM\n",
2897 arvif->vdev_id, info->bssid);
2898
Kalle Valo5e3dd152013-06-12 20:52:10 +03002899 ret = ath10k_peer_create(ar, arvif->vdev_id,
2900 info->bssid);
2901 if (ret)
Kalle Valobe6546f2014-03-25 14:18:51 +02002902 ath10k_warn("failed to add peer %pM for vdev %d when changing bssid: %i\n",
Ben Greear479398b2013-11-04 09:19:34 -08002903 info->bssid, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002904
2905 if (vif->type == NL80211_IFTYPE_STATION) {
2906 /*
2907 * this is never erased as we it for crypto key
2908 * clearing; this is FW requirement
2909 */
Michal Kaziorc930f742014-01-23 11:38:25 +01002910 memcpy(arvif->bssid, info->bssid, ETH_ALEN);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002911
Kalle Valo60c3daa2013-09-08 17:56:07 +03002912 ath10k_dbg(ATH10K_DBG_MAC,
2913 "mac vdev %d start %pM\n",
2914 arvif->vdev_id, info->bssid);
2915
Kalle Valo5e3dd152013-06-12 20:52:10 +03002916 ret = ath10k_vdev_start(arvif);
Michal Kaziorc930f742014-01-23 11:38:25 +01002917 if (ret) {
Ben Greear69244e52014-02-27 18:50:00 +02002918 ath10k_warn("failed to start vdev %i: %d\n",
2919 arvif->vdev_id, ret);
Kalle Valo75459e32014-02-13 18:13:12 +02002920 goto exit;
Michal Kaziorc930f742014-01-23 11:38:25 +01002921 }
2922
2923 arvif->is_started = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002924 }
2925
2926 /*
2927 * Mac80211 does not keep IBSS bssid when leaving IBSS,
2928 * so driver need to store it. It is needed when leaving
2929 * IBSS in order to remove BSSID peer.
2930 */
2931 if (vif->type == NL80211_IFTYPE_ADHOC)
Michal Kaziorc930f742014-01-23 11:38:25 +01002932 memcpy(arvif->bssid, info->bssid,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002933 ETH_ALEN);
2934 }
2935 }
2936
2937 if (changed & BSS_CHANGED_BEACON_ENABLED)
2938 ath10k_control_beaconing(arvif, info);
2939
2940 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02002941 arvif->use_cts_prot = info->use_cts_prot;
Kalle Valo60c3daa2013-09-08 17:56:07 +03002942 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d cts_prot %d\n",
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02002943 arvif->vdev_id, info->use_cts_prot);
Kalle Valo60c3daa2013-09-08 17:56:07 +03002944
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02002945 ret = ath10k_recalc_rtscts_prot(arvif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002946 if (ret)
Kalle Valobe6546f2014-03-25 14:18:51 +02002947 ath10k_warn("failed to recalculate rts/cts prot for vdev %d: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02002948 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002949 }
2950
2951 if (changed & BSS_CHANGED_ERP_SLOT) {
2952 u32 slottime;
2953 if (info->use_short_slot)
2954 slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
2955
2956 else
2957 slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
2958
Kalle Valo60c3daa2013-09-08 17:56:07 +03002959 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
2960 arvif->vdev_id, slottime);
2961
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02002962 vdev_param = ar->wmi.vdev_param->slot_time;
2963 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002964 slottime);
2965 if (ret)
Kalle Valobe6546f2014-03-25 14:18:51 +02002966 ath10k_warn("failed to set erp slot for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02002967 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002968 }
2969
2970 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
2971 u32 preamble;
2972 if (info->use_short_preamble)
2973 preamble = WMI_VDEV_PREAMBLE_SHORT;
2974 else
2975 preamble = WMI_VDEV_PREAMBLE_LONG;
2976
Kalle Valo60c3daa2013-09-08 17:56:07 +03002977 ath10k_dbg(ATH10K_DBG_MAC,
2978 "mac vdev %d preamble %dn",
2979 arvif->vdev_id, preamble);
2980
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02002981 vdev_param = ar->wmi.vdev_param->preamble;
2982 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002983 preamble);
2984 if (ret)
Kalle Valobe6546f2014-03-25 14:18:51 +02002985 ath10k_warn("failed to set preamble for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02002986 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002987 }
2988
2989 if (changed & BSS_CHANGED_ASSOC) {
2990 if (info->assoc)
2991 ath10k_bss_assoc(hw, vif, info);
2992 }
2993
Kalle Valo75459e32014-02-13 18:13:12 +02002994exit:
Kalle Valo5e3dd152013-06-12 20:52:10 +03002995 mutex_unlock(&ar->conf_mutex);
2996}
2997
2998static int ath10k_hw_scan(struct ieee80211_hw *hw,
2999 struct ieee80211_vif *vif,
3000 struct cfg80211_scan_request *req)
3001{
3002 struct ath10k *ar = hw->priv;
3003 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3004 struct wmi_start_scan_arg arg;
3005 int ret = 0;
3006 int i;
3007
3008 mutex_lock(&ar->conf_mutex);
3009
3010 spin_lock_bh(&ar->data_lock);
3011 if (ar->scan.in_progress) {
3012 spin_unlock_bh(&ar->data_lock);
3013 ret = -EBUSY;
3014 goto exit;
3015 }
3016
Wolfram Sang16735d02013-11-14 14:32:02 -08003017 reinit_completion(&ar->scan.started);
3018 reinit_completion(&ar->scan.completed);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003019 ar->scan.in_progress = true;
3020 ar->scan.aborting = false;
3021 ar->scan.is_roc = false;
3022 ar->scan.vdev_id = arvif->vdev_id;
3023 spin_unlock_bh(&ar->data_lock);
3024
3025 memset(&arg, 0, sizeof(arg));
3026 ath10k_wmi_start_scan_init(ar, &arg);
3027 arg.vdev_id = arvif->vdev_id;
3028 arg.scan_id = ATH10K_SCAN_ID;
3029
3030 if (!req->no_cck)
3031 arg.scan_ctrl_flags |= WMI_SCAN_ADD_CCK_RATES;
3032
3033 if (req->ie_len) {
3034 arg.ie_len = req->ie_len;
3035 memcpy(arg.ie, req->ie, arg.ie_len);
3036 }
3037
3038 if (req->n_ssids) {
3039 arg.n_ssids = req->n_ssids;
3040 for (i = 0; i < arg.n_ssids; i++) {
3041 arg.ssids[i].len = req->ssids[i].ssid_len;
3042 arg.ssids[i].ssid = req->ssids[i].ssid;
3043 }
Michal Kaziordcd4a562013-07-31 10:55:12 +02003044 } else {
3045 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003046 }
3047
3048 if (req->n_channels) {
3049 arg.n_channels = req->n_channels;
3050 for (i = 0; i < arg.n_channels; i++)
3051 arg.channels[i] = req->channels[i]->center_freq;
3052 }
3053
3054 ret = ath10k_start_scan(ar, &arg);
3055 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02003056 ath10k_warn("failed to start hw scan: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003057 spin_lock_bh(&ar->data_lock);
3058 ar->scan.in_progress = false;
3059 spin_unlock_bh(&ar->data_lock);
3060 }
3061
3062exit:
3063 mutex_unlock(&ar->conf_mutex);
3064 return ret;
3065}
3066
3067static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
3068 struct ieee80211_vif *vif)
3069{
3070 struct ath10k *ar = hw->priv;
3071 int ret;
3072
3073 mutex_lock(&ar->conf_mutex);
3074 ret = ath10k_abort_scan(ar);
3075 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02003076 ath10k_warn("failed to abort scan: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003077 ieee80211_scan_completed(hw, 1 /* aborted */);
3078 }
3079 mutex_unlock(&ar->conf_mutex);
3080}
3081
Michal Kaziorcfb27d22013-12-02 09:06:36 +01003082static void ath10k_set_key_h_def_keyidx(struct ath10k *ar,
3083 struct ath10k_vif *arvif,
3084 enum set_key_cmd cmd,
3085 struct ieee80211_key_conf *key)
3086{
3087 u32 vdev_param = arvif->ar->wmi.vdev_param->def_keyid;
3088 int ret;
3089
3090 /* 10.1 firmware branch requires default key index to be set to group
3091 * key index after installing it. Otherwise FW/HW Txes corrupted
3092 * frames with multi-vif APs. This is not required for main firmware
3093 * branch (e.g. 636).
3094 *
3095 * FIXME: This has been tested only in AP. It remains unknown if this
3096 * is required for multi-vif STA interfaces on 10.1 */
3097
3098 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
3099 return;
3100
3101 if (key->cipher == WLAN_CIPHER_SUITE_WEP40)
3102 return;
3103
3104 if (key->cipher == WLAN_CIPHER_SUITE_WEP104)
3105 return;
3106
3107 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3108 return;
3109
3110 if (cmd != SET_KEY)
3111 return;
3112
3113 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3114 key->keyidx);
3115 if (ret)
Ben Greear69244e52014-02-27 18:50:00 +02003116 ath10k_warn("failed to set vdev %i group key as default key: %d\n",
3117 arvif->vdev_id, ret);
Michal Kaziorcfb27d22013-12-02 09:06:36 +01003118}
3119
Kalle Valo5e3dd152013-06-12 20:52:10 +03003120static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
3121 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
3122 struct ieee80211_key_conf *key)
3123{
3124 struct ath10k *ar = hw->priv;
3125 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3126 struct ath10k_peer *peer;
3127 const u8 *peer_addr;
3128 bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3129 key->cipher == WLAN_CIPHER_SUITE_WEP104;
3130 int ret = 0;
3131
3132 if (key->keyidx > WMI_MAX_KEY_INDEX)
3133 return -ENOSPC;
3134
3135 mutex_lock(&ar->conf_mutex);
3136
3137 if (sta)
3138 peer_addr = sta->addr;
3139 else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
3140 peer_addr = vif->bss_conf.bssid;
3141 else
3142 peer_addr = vif->addr;
3143
3144 key->hw_key_idx = key->keyidx;
3145
3146 /* the peer should not disappear in mid-way (unless FW goes awry) since
3147 * we already hold conf_mutex. we just make sure its there now. */
3148 spin_lock_bh(&ar->data_lock);
3149 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
3150 spin_unlock_bh(&ar->data_lock);
3151
3152 if (!peer) {
3153 if (cmd == SET_KEY) {
Kalle Valobe6546f2014-03-25 14:18:51 +02003154 ath10k_warn("failed to install key for non-existent peer %pM\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03003155 peer_addr);
3156 ret = -EOPNOTSUPP;
3157 goto exit;
3158 } else {
3159 /* if the peer doesn't exist there is no key to disable
3160 * anymore */
3161 goto exit;
3162 }
3163 }
3164
3165 if (is_wep) {
3166 if (cmd == SET_KEY)
3167 arvif->wep_keys[key->keyidx] = key;
3168 else
3169 arvif->wep_keys[key->keyidx] = NULL;
3170
3171 if (cmd == DISABLE_KEY)
3172 ath10k_clear_vdev_key(arvif, key);
3173 }
3174
3175 ret = ath10k_install_key(arvif, key, cmd, peer_addr);
3176 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02003177 ath10k_warn("failed to install key for vdev %i peer %pM: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003178 arvif->vdev_id, peer_addr, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003179 goto exit;
3180 }
3181
Michal Kaziorcfb27d22013-12-02 09:06:36 +01003182 ath10k_set_key_h_def_keyidx(ar, arvif, cmd, key);
3183
Kalle Valo5e3dd152013-06-12 20:52:10 +03003184 spin_lock_bh(&ar->data_lock);
3185 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
3186 if (peer && cmd == SET_KEY)
3187 peer->keys[key->keyidx] = key;
3188 else if (peer && cmd == DISABLE_KEY)
3189 peer->keys[key->keyidx] = NULL;
3190 else if (peer == NULL)
3191 /* impossible unless FW goes crazy */
Kalle Valobe6546f2014-03-25 14:18:51 +02003192 ath10k_warn("Peer %pM disappeared!\n", peer_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003193 spin_unlock_bh(&ar->data_lock);
3194
3195exit:
3196 mutex_unlock(&ar->conf_mutex);
3197 return ret;
3198}
3199
Michal Kazior9797feb2014-02-14 14:49:48 +01003200static void ath10k_sta_rc_update_wk(struct work_struct *wk)
3201{
3202 struct ath10k *ar;
3203 struct ath10k_vif *arvif;
3204 struct ath10k_sta *arsta;
3205 struct ieee80211_sta *sta;
3206 u32 changed, bw, nss, smps;
3207 int err;
3208
3209 arsta = container_of(wk, struct ath10k_sta, update_wk);
3210 sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
3211 arvif = arsta->arvif;
3212 ar = arvif->ar;
3213
3214 spin_lock_bh(&ar->data_lock);
3215
3216 changed = arsta->changed;
3217 arsta->changed = 0;
3218
3219 bw = arsta->bw;
3220 nss = arsta->nss;
3221 smps = arsta->smps;
3222
3223 spin_unlock_bh(&ar->data_lock);
3224
3225 mutex_lock(&ar->conf_mutex);
3226
3227 if (changed & IEEE80211_RC_BW_CHANGED) {
3228 ath10k_dbg(ATH10K_DBG_MAC, "mac update sta %pM peer bw %d\n",
3229 sta->addr, bw);
3230
3231 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
3232 WMI_PEER_CHAN_WIDTH, bw);
3233 if (err)
3234 ath10k_warn("failed to update STA %pM peer bw %d: %d\n",
3235 sta->addr, bw, err);
3236 }
3237
3238 if (changed & IEEE80211_RC_NSS_CHANGED) {
3239 ath10k_dbg(ATH10K_DBG_MAC, "mac update sta %pM nss %d\n",
3240 sta->addr, nss);
3241
3242 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
3243 WMI_PEER_NSS, nss);
3244 if (err)
3245 ath10k_warn("failed to update STA %pM nss %d: %d\n",
3246 sta->addr, nss, err);
3247 }
3248
3249 if (changed & IEEE80211_RC_SMPS_CHANGED) {
3250 ath10k_dbg(ATH10K_DBG_MAC, "mac update sta %pM smps %d\n",
3251 sta->addr, smps);
3252
3253 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
3254 WMI_PEER_SMPS_STATE, smps);
3255 if (err)
3256 ath10k_warn("failed to update STA %pM smps %d: %d\n",
3257 sta->addr, smps, err);
3258 }
3259
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02003260 if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
3261 ath10k_dbg(ATH10K_DBG_MAC, "mac update sta %pM supp rates\n",
3262 sta->addr);
3263
3264 err = ath10k_station_assoc(ar, arvif, sta, true);
3265 if (err)
Kalle Valobe6546f2014-03-25 14:18:51 +02003266 ath10k_warn("failed to reassociate station: %pM\n",
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02003267 sta->addr);
3268 }
3269
Michal Kazior9797feb2014-02-14 14:49:48 +01003270 mutex_unlock(&ar->conf_mutex);
3271}
3272
Kalle Valo5e3dd152013-06-12 20:52:10 +03003273static int ath10k_sta_state(struct ieee80211_hw *hw,
3274 struct ieee80211_vif *vif,
3275 struct ieee80211_sta *sta,
3276 enum ieee80211_sta_state old_state,
3277 enum ieee80211_sta_state new_state)
3278{
3279 struct ath10k *ar = hw->priv;
3280 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Michal Kazior9797feb2014-02-14 14:49:48 +01003281 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
Bartosz Markowski0e759f32014-01-02 14:38:33 +01003282 int max_num_peers;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003283 int ret = 0;
3284
Michal Kazior76f90022014-02-25 09:29:57 +02003285 if (old_state == IEEE80211_STA_NOTEXIST &&
3286 new_state == IEEE80211_STA_NONE) {
3287 memset(arsta, 0, sizeof(*arsta));
3288 arsta->arvif = arvif;
3289 INIT_WORK(&arsta->update_wk, ath10k_sta_rc_update_wk);
3290 }
3291
Michal Kazior9797feb2014-02-14 14:49:48 +01003292 /* cancel must be done outside the mutex to avoid deadlock */
3293 if ((old_state == IEEE80211_STA_NONE &&
3294 new_state == IEEE80211_STA_NOTEXIST))
3295 cancel_work_sync(&arsta->update_wk);
3296
Kalle Valo5e3dd152013-06-12 20:52:10 +03003297 mutex_lock(&ar->conf_mutex);
3298
3299 if (old_state == IEEE80211_STA_NOTEXIST &&
3300 new_state == IEEE80211_STA_NONE &&
3301 vif->type != NL80211_IFTYPE_STATION) {
3302 /*
3303 * New station addition.
3304 */
Bartosz Markowski0e759f32014-01-02 14:38:33 +01003305 if (test_bit(ATH10K_FW_FEATURE_WMI_10X, ar->fw_features))
3306 max_num_peers = TARGET_10X_NUM_PEERS_MAX - 1;
3307 else
3308 max_num_peers = TARGET_NUM_PEERS;
3309
3310 if (ar->num_peers >= max_num_peers) {
Kalle Valobe6546f2014-03-25 14:18:51 +02003311 ath10k_warn("number of peers exceeded: peers number %d (max peers %d)\n",
Bartosz Markowski0e759f32014-01-02 14:38:33 +01003312 ar->num_peers, max_num_peers);
3313 ret = -ENOBUFS;
3314 goto exit;
3315 }
3316
Kalle Valo60c3daa2013-09-08 17:56:07 +03003317 ath10k_dbg(ATH10K_DBG_MAC,
Bartosz Markowski0e759f32014-01-02 14:38:33 +01003318 "mac vdev %d peer create %pM (new sta) num_peers %d\n",
3319 arvif->vdev_id, sta->addr, ar->num_peers);
Kalle Valo60c3daa2013-09-08 17:56:07 +03003320
Kalle Valo5e3dd152013-06-12 20:52:10 +03003321 ret = ath10k_peer_create(ar, arvif->vdev_id, sta->addr);
3322 if (ret)
Kalle Valobe6546f2014-03-25 14:18:51 +02003323 ath10k_warn("failed to add peer %pM for vdev %d when adding a new sta: %i\n",
Ben Greear479398b2013-11-04 09:19:34 -08003324 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003325 } else if ((old_state == IEEE80211_STA_NONE &&
3326 new_state == IEEE80211_STA_NOTEXIST)) {
3327 /*
3328 * Existing station deletion.
3329 */
Kalle Valo60c3daa2013-09-08 17:56:07 +03003330 ath10k_dbg(ATH10K_DBG_MAC,
3331 "mac vdev %d peer delete %pM (sta gone)\n",
3332 arvif->vdev_id, sta->addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003333 ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
3334 if (ret)
Kalle Valobe6546f2014-03-25 14:18:51 +02003335 ath10k_warn("failed to delete peer %pM for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003336 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003337
3338 if (vif->type == NL80211_IFTYPE_STATION)
3339 ath10k_bss_disassoc(hw, vif);
3340 } else if (old_state == IEEE80211_STA_AUTH &&
3341 new_state == IEEE80211_STA_ASSOC &&
3342 (vif->type == NL80211_IFTYPE_AP ||
3343 vif->type == NL80211_IFTYPE_ADHOC)) {
3344 /*
3345 * New association.
3346 */
Kalle Valo60c3daa2013-09-08 17:56:07 +03003347 ath10k_dbg(ATH10K_DBG_MAC, "mac sta %pM associated\n",
3348 sta->addr);
3349
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02003350 ret = ath10k_station_assoc(ar, arvif, sta, false);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003351 if (ret)
Kalle Valobe6546f2014-03-25 14:18:51 +02003352 ath10k_warn("failed to associate station %pM for vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003353 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003354 } else if (old_state == IEEE80211_STA_ASSOC &&
3355 new_state == IEEE80211_STA_AUTH &&
3356 (vif->type == NL80211_IFTYPE_AP ||
3357 vif->type == NL80211_IFTYPE_ADHOC)) {
3358 /*
3359 * Disassociation.
3360 */
Kalle Valo60c3daa2013-09-08 17:56:07 +03003361 ath10k_dbg(ATH10K_DBG_MAC, "mac sta %pM disassociated\n",
3362 sta->addr);
3363
Kalle Valo5e3dd152013-06-12 20:52:10 +03003364 ret = ath10k_station_disassoc(ar, arvif, sta);
3365 if (ret)
Kalle Valobe6546f2014-03-25 14:18:51 +02003366 ath10k_warn("failed to disassociate station: %pM vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003367 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003368 }
Bartosz Markowski0e759f32014-01-02 14:38:33 +01003369exit:
Kalle Valo5e3dd152013-06-12 20:52:10 +03003370 mutex_unlock(&ar->conf_mutex);
3371 return ret;
3372}
3373
3374static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
3375 u16 ac, bool enable)
3376{
3377 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3378 u32 value = 0;
3379 int ret = 0;
3380
Michal Kazior548db542013-07-05 16:15:15 +03003381 lockdep_assert_held(&ar->conf_mutex);
3382
Kalle Valo5e3dd152013-06-12 20:52:10 +03003383 if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
3384 return 0;
3385
3386 switch (ac) {
3387 case IEEE80211_AC_VO:
3388 value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
3389 WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
3390 break;
3391 case IEEE80211_AC_VI:
3392 value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
3393 WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
3394 break;
3395 case IEEE80211_AC_BE:
3396 value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
3397 WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
3398 break;
3399 case IEEE80211_AC_BK:
3400 value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
3401 WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
3402 break;
3403 }
3404
3405 if (enable)
3406 arvif->u.sta.uapsd |= value;
3407 else
3408 arvif->u.sta.uapsd &= ~value;
3409
3410 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3411 WMI_STA_PS_PARAM_UAPSD,
3412 arvif->u.sta.uapsd);
3413 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02003414 ath10k_warn("failed to set uapsd params: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003415 goto exit;
3416 }
3417
3418 if (arvif->u.sta.uapsd)
3419 value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
3420 else
3421 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
3422
3423 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3424 WMI_STA_PS_PARAM_RX_WAKE_POLICY,
3425 value);
3426 if (ret)
Kalle Valobe6546f2014-03-25 14:18:51 +02003427 ath10k_warn("failed to set rx wake param: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003428
3429exit:
3430 return ret;
3431}
3432
3433static int ath10k_conf_tx(struct ieee80211_hw *hw,
3434 struct ieee80211_vif *vif, u16 ac,
3435 const struct ieee80211_tx_queue_params *params)
3436{
3437 struct ath10k *ar = hw->priv;
3438 struct wmi_wmm_params_arg *p = NULL;
3439 int ret;
3440
3441 mutex_lock(&ar->conf_mutex);
3442
3443 switch (ac) {
3444 case IEEE80211_AC_VO:
3445 p = &ar->wmm_params.ac_vo;
3446 break;
3447 case IEEE80211_AC_VI:
3448 p = &ar->wmm_params.ac_vi;
3449 break;
3450 case IEEE80211_AC_BE:
3451 p = &ar->wmm_params.ac_be;
3452 break;
3453 case IEEE80211_AC_BK:
3454 p = &ar->wmm_params.ac_bk;
3455 break;
3456 }
3457
3458 if (WARN_ON(!p)) {
3459 ret = -EINVAL;
3460 goto exit;
3461 }
3462
3463 p->cwmin = params->cw_min;
3464 p->cwmax = params->cw_max;
3465 p->aifs = params->aifs;
3466
3467 /*
3468 * The channel time duration programmed in the HW is in absolute
3469 * microseconds, while mac80211 gives the txop in units of
3470 * 32 microseconds.
3471 */
3472 p->txop = params->txop * 32;
3473
3474 /* FIXME: FW accepts wmm params per hw, not per vif */
3475 ret = ath10k_wmi_pdev_set_wmm_params(ar, &ar->wmm_params);
3476 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02003477 ath10k_warn("failed to set wmm params: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003478 goto exit;
3479 }
3480
3481 ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
3482 if (ret)
Kalle Valobe6546f2014-03-25 14:18:51 +02003483 ath10k_warn("failed to set sta uapsd: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003484
3485exit:
3486 mutex_unlock(&ar->conf_mutex);
3487 return ret;
3488}
3489
3490#define ATH10K_ROC_TIMEOUT_HZ (2*HZ)
3491
3492static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
3493 struct ieee80211_vif *vif,
3494 struct ieee80211_channel *chan,
3495 int duration,
3496 enum ieee80211_roc_type type)
3497{
3498 struct ath10k *ar = hw->priv;
3499 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3500 struct wmi_start_scan_arg arg;
3501 int ret;
3502
3503 mutex_lock(&ar->conf_mutex);
3504
3505 spin_lock_bh(&ar->data_lock);
3506 if (ar->scan.in_progress) {
3507 spin_unlock_bh(&ar->data_lock);
3508 ret = -EBUSY;
3509 goto exit;
3510 }
3511
Wolfram Sang16735d02013-11-14 14:32:02 -08003512 reinit_completion(&ar->scan.started);
3513 reinit_completion(&ar->scan.completed);
3514 reinit_completion(&ar->scan.on_channel);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003515 ar->scan.in_progress = true;
3516 ar->scan.aborting = false;
3517 ar->scan.is_roc = true;
3518 ar->scan.vdev_id = arvif->vdev_id;
3519 ar->scan.roc_freq = chan->center_freq;
3520 spin_unlock_bh(&ar->data_lock);
3521
3522 memset(&arg, 0, sizeof(arg));
3523 ath10k_wmi_start_scan_init(ar, &arg);
3524 arg.vdev_id = arvif->vdev_id;
3525 arg.scan_id = ATH10K_SCAN_ID;
3526 arg.n_channels = 1;
3527 arg.channels[0] = chan->center_freq;
3528 arg.dwell_time_active = duration;
3529 arg.dwell_time_passive = duration;
3530 arg.max_scan_time = 2 * duration;
3531 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
3532 arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
3533
3534 ret = ath10k_start_scan(ar, &arg);
3535 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02003536 ath10k_warn("failed to start roc scan: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003537 spin_lock_bh(&ar->data_lock);
3538 ar->scan.in_progress = false;
3539 spin_unlock_bh(&ar->data_lock);
3540 goto exit;
3541 }
3542
3543 ret = wait_for_completion_timeout(&ar->scan.on_channel, 3*HZ);
3544 if (ret == 0) {
Kalle Valobe6546f2014-03-25 14:18:51 +02003545 ath10k_warn("failed to switch to channel for roc scan\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03003546 ath10k_abort_scan(ar);
3547 ret = -ETIMEDOUT;
3548 goto exit;
3549 }
3550
3551 ret = 0;
3552exit:
3553 mutex_unlock(&ar->conf_mutex);
3554 return ret;
3555}
3556
3557static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw)
3558{
3559 struct ath10k *ar = hw->priv;
3560
3561 mutex_lock(&ar->conf_mutex);
3562 ath10k_abort_scan(ar);
3563 mutex_unlock(&ar->conf_mutex);
3564
3565 return 0;
3566}
3567
3568/*
3569 * Both RTS and Fragmentation threshold are interface-specific
3570 * in ath10k, but device-specific in mac80211.
3571 */
Kalle Valo5e3dd152013-06-12 20:52:10 +03003572
3573static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3574{
Kalle Valo5e3dd152013-06-12 20:52:10 +03003575 struct ath10k *ar = hw->priv;
Michal Kaziorad088bf2013-10-16 15:44:46 +03003576 struct ath10k_vif *arvif;
3577 int ret = 0;
Michal Kazior548db542013-07-05 16:15:15 +03003578
Michal Kaziorad088bf2013-10-16 15:44:46 +03003579 mutex_lock(&ar->conf_mutex);
3580 list_for_each_entry(arvif, &ar->arvifs, list) {
3581 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
3582 arvif->vdev_id, value);
Kalle Valo60c3daa2013-09-08 17:56:07 +03003583
Michal Kaziorad088bf2013-10-16 15:44:46 +03003584 ret = ath10k_mac_set_rts(arvif, value);
3585 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02003586 ath10k_warn("failed to set rts threshold for vdev %d: %d\n",
Michal Kaziorad088bf2013-10-16 15:44:46 +03003587 arvif->vdev_id, ret);
3588 break;
3589 }
3590 }
3591 mutex_unlock(&ar->conf_mutex);
3592
3593 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003594}
3595
3596static int ath10k_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
3597{
Kalle Valo5e3dd152013-06-12 20:52:10 +03003598 struct ath10k *ar = hw->priv;
Michal Kaziorad088bf2013-10-16 15:44:46 +03003599 struct ath10k_vif *arvif;
3600 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003601
Kalle Valo5e3dd152013-06-12 20:52:10 +03003602 mutex_lock(&ar->conf_mutex);
Michal Kaziorad088bf2013-10-16 15:44:46 +03003603 list_for_each_entry(arvif, &ar->arvifs, list) {
3604 ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d fragmentation threshold %d\n",
3605 arvif->vdev_id, value);
3606
3607 ret = ath10k_mac_set_rts(arvif, value);
3608 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02003609 ath10k_warn("failed to set fragmentation threshold for vdev %d: %d\n",
Michal Kaziorad088bf2013-10-16 15:44:46 +03003610 arvif->vdev_id, ret);
3611 break;
3612 }
3613 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003614 mutex_unlock(&ar->conf_mutex);
3615
Michal Kaziorad088bf2013-10-16 15:44:46 +03003616 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003617}
3618
3619static void ath10k_flush(struct ieee80211_hw *hw, u32 queues, bool drop)
3620{
3621 struct ath10k *ar = hw->priv;
Michal Kazioraffd3212013-07-16 09:54:35 +02003622 bool skip;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003623 int ret;
3624
3625 /* mac80211 doesn't care if we really xmit queued frames or not
3626 * we'll collect those frames either way if we stop/delete vdevs */
3627 if (drop)
3628 return;
3629
Michal Kazior548db542013-07-05 16:15:15 +03003630 mutex_lock(&ar->conf_mutex);
3631
Michal Kazioraffd3212013-07-16 09:54:35 +02003632 if (ar->state == ATH10K_STATE_WEDGED)
3633 goto skip;
3634
Michal Kazioredb82362013-07-05 16:15:14 +03003635 ret = wait_event_timeout(ar->htt.empty_tx_wq, ({
Kalle Valo5e3dd152013-06-12 20:52:10 +03003636 bool empty;
Michal Kazioraffd3212013-07-16 09:54:35 +02003637
Michal Kazioredb82362013-07-05 16:15:14 +03003638 spin_lock_bh(&ar->htt.tx_lock);
Michal Kazior0945baf2013-09-18 14:43:18 +02003639 empty = (ar->htt.num_pending_tx == 0);
Michal Kazioredb82362013-07-05 16:15:14 +03003640 spin_unlock_bh(&ar->htt.tx_lock);
Michal Kazioraffd3212013-07-16 09:54:35 +02003641
3642 skip = (ar->state == ATH10K_STATE_WEDGED);
3643
3644 (empty || skip);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003645 }), ATH10K_FLUSH_TIMEOUT_HZ);
Michal Kazioraffd3212013-07-16 09:54:35 +02003646
3647 if (ret <= 0 || skip)
Kalle Valobe6546f2014-03-25 14:18:51 +02003648 ath10k_warn("failed to flush transmit queue (skip %i ar-state %i): %i\n",
Ben Greear9ba4c782014-02-25 09:29:57 +02003649 skip, ar->state, ret);
Michal Kazior548db542013-07-05 16:15:15 +03003650
Michal Kazioraffd3212013-07-16 09:54:35 +02003651skip:
Michal Kazior548db542013-07-05 16:15:15 +03003652 mutex_unlock(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003653}
3654
3655/* TODO: Implement this function properly
3656 * For now it is needed to reply to Probe Requests in IBSS mode.
3657 * Propably we need this information from FW.
3658 */
3659static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
3660{
3661 return 1;
3662}
3663
Michal Kazior8cd13ca2013-07-16 09:38:54 +02003664#ifdef CONFIG_PM
3665static int ath10k_suspend(struct ieee80211_hw *hw,
3666 struct cfg80211_wowlan *wowlan)
3667{
3668 struct ath10k *ar = hw->priv;
3669 int ret;
3670
Marek Puzyniak9042e172014-02-10 17:14:23 +01003671 mutex_lock(&ar->conf_mutex);
3672
Marek Puzyniak00f54822014-02-10 17:14:24 +01003673 ret = ath10k_wait_for_suspend(ar, WMI_PDEV_SUSPEND);
Michal Kazior8cd13ca2013-07-16 09:38:54 +02003674 if (ret) {
Marek Puzyniak00f54822014-02-10 17:14:24 +01003675 if (ret == -ETIMEDOUT)
3676 goto resume;
Marek Puzyniak9042e172014-02-10 17:14:23 +01003677 ret = 1;
3678 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02003679 }
3680
Michal Kazior8cd13ca2013-07-16 09:38:54 +02003681 ret = ath10k_hif_suspend(ar);
3682 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02003683 ath10k_warn("failed to suspend hif: %d\n", ret);
Michal Kazior8cd13ca2013-07-16 09:38:54 +02003684 goto resume;
3685 }
3686
Marek Puzyniak9042e172014-02-10 17:14:23 +01003687 ret = 0;
3688 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02003689resume:
3690 ret = ath10k_wmi_pdev_resume_target(ar);
3691 if (ret)
Kalle Valobe6546f2014-03-25 14:18:51 +02003692 ath10k_warn("failed to resume target: %d\n", ret);
Marek Puzyniak9042e172014-02-10 17:14:23 +01003693
3694 ret = 1;
3695exit:
3696 mutex_unlock(&ar->conf_mutex);
3697 return ret;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02003698}
3699
3700static int ath10k_resume(struct ieee80211_hw *hw)
3701{
3702 struct ath10k *ar = hw->priv;
3703 int ret;
3704
Marek Puzyniak9042e172014-02-10 17:14:23 +01003705 mutex_lock(&ar->conf_mutex);
3706
Michal Kazior8cd13ca2013-07-16 09:38:54 +02003707 ret = ath10k_hif_resume(ar);
3708 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02003709 ath10k_warn("failed to resume hif: %d\n", ret);
Marek Puzyniak9042e172014-02-10 17:14:23 +01003710 ret = 1;
3711 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02003712 }
3713
3714 ret = ath10k_wmi_pdev_resume_target(ar);
3715 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02003716 ath10k_warn("failed to resume target: %d\n", ret);
Marek Puzyniak9042e172014-02-10 17:14:23 +01003717 ret = 1;
3718 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02003719 }
3720
Marek Puzyniak9042e172014-02-10 17:14:23 +01003721 ret = 0;
3722exit:
3723 mutex_unlock(&ar->conf_mutex);
3724 return ret;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02003725}
3726#endif
3727
Michal Kazioraffd3212013-07-16 09:54:35 +02003728static void ath10k_restart_complete(struct ieee80211_hw *hw)
3729{
3730 struct ath10k *ar = hw->priv;
3731
3732 mutex_lock(&ar->conf_mutex);
3733
3734 /* If device failed to restart it will be in a different state, e.g.
3735 * ATH10K_STATE_WEDGED */
3736 if (ar->state == ATH10K_STATE_RESTARTED) {
3737 ath10k_info("device successfully recovered\n");
3738 ar->state = ATH10K_STATE_ON;
3739 }
3740
3741 mutex_unlock(&ar->conf_mutex);
3742}
3743
Michal Kazior2e1dea42013-07-31 10:32:40 +02003744static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
3745 struct survey_info *survey)
3746{
3747 struct ath10k *ar = hw->priv;
3748 struct ieee80211_supported_band *sband;
3749 struct survey_info *ar_survey = &ar->survey[idx];
3750 int ret = 0;
3751
3752 mutex_lock(&ar->conf_mutex);
3753
3754 sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
3755 if (sband && idx >= sband->n_channels) {
3756 idx -= sband->n_channels;
3757 sband = NULL;
3758 }
3759
3760 if (!sband)
3761 sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
3762
3763 if (!sband || idx >= sband->n_channels) {
3764 ret = -ENOENT;
3765 goto exit;
3766 }
3767
3768 spin_lock_bh(&ar->data_lock);
3769 memcpy(survey, ar_survey, sizeof(*survey));
3770 spin_unlock_bh(&ar->data_lock);
3771
3772 survey->channel = &sband->channels[idx];
3773
3774exit:
3775 mutex_unlock(&ar->conf_mutex);
3776 return ret;
3777}
3778
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01003779/* Helper table for legacy fixed_rate/bitrate_mask */
3780static const u8 cck_ofdm_rate[] = {
3781 /* CCK */
3782 3, /* 1Mbps */
3783 2, /* 2Mbps */
3784 1, /* 5.5Mbps */
3785 0, /* 11Mbps */
3786 /* OFDM */
3787 3, /* 6Mbps */
3788 7, /* 9Mbps */
3789 2, /* 12Mbps */
3790 6, /* 18Mbps */
3791 1, /* 24Mbps */
3792 5, /* 36Mbps */
3793 0, /* 48Mbps */
3794 4, /* 54Mbps */
3795};
3796
3797/* Check if only one bit set */
3798static int ath10k_check_single_mask(u32 mask)
3799{
3800 int bit;
3801
3802 bit = ffs(mask);
3803 if (!bit)
3804 return 0;
3805
3806 mask &= ~BIT(bit - 1);
3807 if (mask)
3808 return 2;
3809
3810 return 1;
3811}
3812
3813static bool
3814ath10k_default_bitrate_mask(struct ath10k *ar,
3815 enum ieee80211_band band,
3816 const struct cfg80211_bitrate_mask *mask)
3817{
3818 u32 legacy = 0x00ff;
3819 u8 ht = 0xff, i;
3820 u16 vht = 0x3ff;
3821
3822 switch (band) {
3823 case IEEE80211_BAND_2GHZ:
3824 legacy = 0x00fff;
3825 vht = 0;
3826 break;
3827 case IEEE80211_BAND_5GHZ:
3828 break;
3829 default:
3830 return false;
3831 }
3832
3833 if (mask->control[band].legacy != legacy)
3834 return false;
3835
3836 for (i = 0; i < ar->num_rf_chains; i++)
3837 if (mask->control[band].ht_mcs[i] != ht)
3838 return false;
3839
3840 for (i = 0; i < ar->num_rf_chains; i++)
3841 if (mask->control[band].vht_mcs[i] != vht)
3842 return false;
3843
3844 return true;
3845}
3846
3847static bool
3848ath10k_bitrate_mask_nss(const struct cfg80211_bitrate_mask *mask,
3849 enum ieee80211_band band,
3850 u8 *fixed_nss)
3851{
3852 int ht_nss = 0, vht_nss = 0, i;
3853
3854 /* check legacy */
3855 if (ath10k_check_single_mask(mask->control[band].legacy))
3856 return false;
3857
3858 /* check HT */
3859 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
3860 if (mask->control[band].ht_mcs[i] == 0xff)
3861 continue;
3862 else if (mask->control[band].ht_mcs[i] == 0x00)
3863 break;
3864 else
3865 return false;
3866 }
3867
3868 ht_nss = i;
3869
3870 /* check VHT */
3871 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
3872 if (mask->control[band].vht_mcs[i] == 0x03ff)
3873 continue;
3874 else if (mask->control[band].vht_mcs[i] == 0x0000)
3875 break;
3876 else
3877 return false;
3878 }
3879
3880 vht_nss = i;
3881
3882 if (ht_nss > 0 && vht_nss > 0)
3883 return false;
3884
3885 if (ht_nss)
3886 *fixed_nss = ht_nss;
3887 else if (vht_nss)
3888 *fixed_nss = vht_nss;
3889 else
3890 return false;
3891
3892 return true;
3893}
3894
3895static bool
3896ath10k_bitrate_mask_correct(const struct cfg80211_bitrate_mask *mask,
3897 enum ieee80211_band band,
3898 enum wmi_rate_preamble *preamble)
3899{
3900 int legacy = 0, ht = 0, vht = 0, i;
3901
3902 *preamble = WMI_RATE_PREAMBLE_OFDM;
3903
3904 /* check legacy */
3905 legacy = ath10k_check_single_mask(mask->control[band].legacy);
3906 if (legacy > 1)
3907 return false;
3908
3909 /* check HT */
3910 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
3911 ht += ath10k_check_single_mask(mask->control[band].ht_mcs[i]);
3912 if (ht > 1)
3913 return false;
3914
3915 /* check VHT */
3916 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
3917 vht += ath10k_check_single_mask(mask->control[band].vht_mcs[i]);
3918 if (vht > 1)
3919 return false;
3920
3921 /* Currently we support only one fixed_rate */
3922 if ((legacy + ht + vht) != 1)
3923 return false;
3924
3925 if (ht)
3926 *preamble = WMI_RATE_PREAMBLE_HT;
3927 else if (vht)
3928 *preamble = WMI_RATE_PREAMBLE_VHT;
3929
3930 return true;
3931}
3932
3933static bool
3934ath10k_bitrate_mask_rate(const struct cfg80211_bitrate_mask *mask,
3935 enum ieee80211_band band,
3936 u8 *fixed_rate,
3937 u8 *fixed_nss)
3938{
3939 u8 rate = 0, pream = 0, nss = 0, i;
3940 enum wmi_rate_preamble preamble;
3941
3942 /* Check if single rate correct */
3943 if (!ath10k_bitrate_mask_correct(mask, band, &preamble))
3944 return false;
3945
3946 pream = preamble;
3947
3948 switch (preamble) {
3949 case WMI_RATE_PREAMBLE_CCK:
3950 case WMI_RATE_PREAMBLE_OFDM:
3951 i = ffs(mask->control[band].legacy) - 1;
3952
3953 if (band == IEEE80211_BAND_2GHZ && i < 4)
3954 pream = WMI_RATE_PREAMBLE_CCK;
3955
3956 if (band == IEEE80211_BAND_5GHZ)
3957 i += 4;
3958
3959 if (i >= ARRAY_SIZE(cck_ofdm_rate))
3960 return false;
3961
3962 rate = cck_ofdm_rate[i];
3963 break;
3964 case WMI_RATE_PREAMBLE_HT:
3965 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
3966 if (mask->control[band].ht_mcs[i])
3967 break;
3968
3969 if (i == IEEE80211_HT_MCS_MASK_LEN)
3970 return false;
3971
3972 rate = ffs(mask->control[band].ht_mcs[i]) - 1;
3973 nss = i;
3974 break;
3975 case WMI_RATE_PREAMBLE_VHT:
3976 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
3977 if (mask->control[band].vht_mcs[i])
3978 break;
3979
3980 if (i == NL80211_VHT_NSS_MAX)
3981 return false;
3982
3983 rate = ffs(mask->control[band].vht_mcs[i]) - 1;
3984 nss = i;
3985 break;
3986 }
3987
3988 *fixed_nss = nss + 1;
3989 nss <<= 4;
3990 pream <<= 6;
3991
3992 ath10k_dbg(ATH10K_DBG_MAC, "mac fixed rate pream 0x%02x nss 0x%02x rate 0x%02x\n",
3993 pream, nss, rate);
3994
3995 *fixed_rate = pream | nss | rate;
3996
3997 return true;
3998}
3999
4000static bool ath10k_get_fixed_rate_nss(const struct cfg80211_bitrate_mask *mask,
4001 enum ieee80211_band band,
4002 u8 *fixed_rate,
4003 u8 *fixed_nss)
4004{
4005 /* First check full NSS mask, if we can simply limit NSS */
4006 if (ath10k_bitrate_mask_nss(mask, band, fixed_nss))
4007 return true;
4008
4009 /* Next Check single rate is set */
4010 return ath10k_bitrate_mask_rate(mask, band, fixed_rate, fixed_nss);
4011}
4012
4013static int ath10k_set_fixed_rate_param(struct ath10k_vif *arvif,
4014 u8 fixed_rate,
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004015 u8 fixed_nss,
4016 u8 force_sgi)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004017{
4018 struct ath10k *ar = arvif->ar;
4019 u32 vdev_param;
4020 int ret = 0;
4021
4022 mutex_lock(&ar->conf_mutex);
4023
4024 if (arvif->fixed_rate == fixed_rate &&
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004025 arvif->fixed_nss == fixed_nss &&
4026 arvif->force_sgi == force_sgi)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004027 goto exit;
4028
4029 if (fixed_rate == WMI_FIXED_RATE_NONE)
4030 ath10k_dbg(ATH10K_DBG_MAC, "mac disable fixed bitrate mask\n");
4031
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004032 if (force_sgi)
4033 ath10k_dbg(ATH10K_DBG_MAC, "mac force sgi\n");
4034
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004035 vdev_param = ar->wmi.vdev_param->fixed_rate;
4036 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
4037 vdev_param, fixed_rate);
4038 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02004039 ath10k_warn("failed to set fixed rate param 0x%02x: %d\n",
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004040 fixed_rate, ret);
4041 ret = -EINVAL;
4042 goto exit;
4043 }
4044
4045 arvif->fixed_rate = fixed_rate;
4046
4047 vdev_param = ar->wmi.vdev_param->nss;
4048 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
4049 vdev_param, fixed_nss);
4050
4051 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02004052 ath10k_warn("failed to set fixed nss param %d: %d\n",
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004053 fixed_nss, ret);
4054 ret = -EINVAL;
4055 goto exit;
4056 }
4057
4058 arvif->fixed_nss = fixed_nss;
4059
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004060 vdev_param = ar->wmi.vdev_param->sgi;
4061 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
4062 force_sgi);
4063
4064 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02004065 ath10k_warn("failed to set sgi param %d: %d\n",
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004066 force_sgi, ret);
4067 ret = -EINVAL;
4068 goto exit;
4069 }
4070
4071 arvif->force_sgi = force_sgi;
4072
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004073exit:
4074 mutex_unlock(&ar->conf_mutex);
4075 return ret;
4076}
4077
4078static int ath10k_set_bitrate_mask(struct ieee80211_hw *hw,
4079 struct ieee80211_vif *vif,
4080 const struct cfg80211_bitrate_mask *mask)
4081{
4082 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4083 struct ath10k *ar = arvif->ar;
4084 enum ieee80211_band band = ar->hw->conf.chandef.chan->band;
4085 u8 fixed_rate = WMI_FIXED_RATE_NONE;
4086 u8 fixed_nss = ar->num_rf_chains;
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004087 u8 force_sgi;
4088
4089 force_sgi = mask->control[band].gi;
4090 if (force_sgi == NL80211_TXRATE_FORCE_LGI)
4091 return -EINVAL;
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004092
4093 if (!ath10k_default_bitrate_mask(ar, band, mask)) {
4094 if (!ath10k_get_fixed_rate_nss(mask, band,
4095 &fixed_rate,
4096 &fixed_nss))
4097 return -EINVAL;
4098 }
4099
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004100 if (fixed_rate == WMI_FIXED_RATE_NONE && force_sgi) {
Kalle Valobe6546f2014-03-25 14:18:51 +02004101 ath10k_warn("failed to force SGI usage for default rate settings\n");
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004102 return -EINVAL;
4103 }
4104
4105 return ath10k_set_fixed_rate_param(arvif, fixed_rate,
4106 fixed_nss, force_sgi);
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004107}
4108
Michal Kaziorc2df44b2014-01-23 11:38:26 +01004109static void ath10k_channel_switch_beacon(struct ieee80211_hw *hw,
4110 struct ieee80211_vif *vif,
4111 struct cfg80211_chan_def *chandef)
4112{
4113 /* there's no need to do anything here. vif->csa_active is enough */
4114 return;
4115}
4116
Michal Kazior9797feb2014-02-14 14:49:48 +01004117static void ath10k_sta_rc_update(struct ieee80211_hw *hw,
4118 struct ieee80211_vif *vif,
4119 struct ieee80211_sta *sta,
4120 u32 changed)
4121{
4122 struct ath10k *ar = hw->priv;
4123 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
4124 u32 bw, smps;
4125
4126 spin_lock_bh(&ar->data_lock);
4127
4128 ath10k_dbg(ATH10K_DBG_MAC,
4129 "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
4130 sta->addr, changed, sta->bandwidth, sta->rx_nss,
4131 sta->smps_mode);
4132
4133 if (changed & IEEE80211_RC_BW_CHANGED) {
4134 bw = WMI_PEER_CHWIDTH_20MHZ;
4135
4136 switch (sta->bandwidth) {
4137 case IEEE80211_STA_RX_BW_20:
4138 bw = WMI_PEER_CHWIDTH_20MHZ;
4139 break;
4140 case IEEE80211_STA_RX_BW_40:
4141 bw = WMI_PEER_CHWIDTH_40MHZ;
4142 break;
4143 case IEEE80211_STA_RX_BW_80:
4144 bw = WMI_PEER_CHWIDTH_80MHZ;
4145 break;
4146 case IEEE80211_STA_RX_BW_160:
Kalle Valobe6546f2014-03-25 14:18:51 +02004147 ath10k_warn("Invalid bandwith %d in rc update for %pM\n",
4148 sta->bandwidth, sta->addr);
Michal Kazior9797feb2014-02-14 14:49:48 +01004149 bw = WMI_PEER_CHWIDTH_20MHZ;
4150 break;
4151 }
4152
4153 arsta->bw = bw;
4154 }
4155
4156 if (changed & IEEE80211_RC_NSS_CHANGED)
4157 arsta->nss = sta->rx_nss;
4158
4159 if (changed & IEEE80211_RC_SMPS_CHANGED) {
4160 smps = WMI_PEER_SMPS_PS_NONE;
4161
4162 switch (sta->smps_mode) {
4163 case IEEE80211_SMPS_AUTOMATIC:
4164 case IEEE80211_SMPS_OFF:
4165 smps = WMI_PEER_SMPS_PS_NONE;
4166 break;
4167 case IEEE80211_SMPS_STATIC:
4168 smps = WMI_PEER_SMPS_STATIC;
4169 break;
4170 case IEEE80211_SMPS_DYNAMIC:
4171 smps = WMI_PEER_SMPS_DYNAMIC;
4172 break;
4173 case IEEE80211_SMPS_NUM_MODES:
Kalle Valobe6546f2014-03-25 14:18:51 +02004174 ath10k_warn("Invalid smps %d in sta rc update for %pM\n",
4175 sta->smps_mode, sta->addr);
Michal Kazior9797feb2014-02-14 14:49:48 +01004176 smps = WMI_PEER_SMPS_PS_NONE;
4177 break;
4178 }
4179
4180 arsta->smps = smps;
4181 }
4182
Michal Kazior9797feb2014-02-14 14:49:48 +01004183 arsta->changed |= changed;
4184
4185 spin_unlock_bh(&ar->data_lock);
4186
4187 ieee80211_queue_work(hw, &arsta->update_wk);
4188}
4189
Chun-Yeow Yeoh26ebbcc2014-02-25 09:29:54 +02004190static u64 ath10k_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
4191{
4192 /*
4193 * FIXME: Return 0 for time being. Need to figure out whether FW
4194 * has the API to fetch 64-bit local TSF
4195 */
4196
4197 return 0;
4198}
4199
Kalle Valo5e3dd152013-06-12 20:52:10 +03004200static const struct ieee80211_ops ath10k_ops = {
4201 .tx = ath10k_tx,
4202 .start = ath10k_start,
4203 .stop = ath10k_stop,
4204 .config = ath10k_config,
4205 .add_interface = ath10k_add_interface,
4206 .remove_interface = ath10k_remove_interface,
4207 .configure_filter = ath10k_configure_filter,
4208 .bss_info_changed = ath10k_bss_info_changed,
4209 .hw_scan = ath10k_hw_scan,
4210 .cancel_hw_scan = ath10k_cancel_hw_scan,
4211 .set_key = ath10k_set_key,
4212 .sta_state = ath10k_sta_state,
4213 .conf_tx = ath10k_conf_tx,
4214 .remain_on_channel = ath10k_remain_on_channel,
4215 .cancel_remain_on_channel = ath10k_cancel_remain_on_channel,
4216 .set_rts_threshold = ath10k_set_rts_threshold,
4217 .set_frag_threshold = ath10k_set_frag_threshold,
4218 .flush = ath10k_flush,
4219 .tx_last_beacon = ath10k_tx_last_beacon,
Michal Kazioraffd3212013-07-16 09:54:35 +02004220 .restart_complete = ath10k_restart_complete,
Michal Kazior2e1dea42013-07-31 10:32:40 +02004221 .get_survey = ath10k_get_survey,
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004222 .set_bitrate_mask = ath10k_set_bitrate_mask,
Michal Kaziorc2df44b2014-01-23 11:38:26 +01004223 .channel_switch_beacon = ath10k_channel_switch_beacon,
Michal Kazior9797feb2014-02-14 14:49:48 +01004224 .sta_rc_update = ath10k_sta_rc_update,
Chun-Yeow Yeoh26ebbcc2014-02-25 09:29:54 +02004225 .get_tsf = ath10k_get_tsf,
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004226#ifdef CONFIG_PM
4227 .suspend = ath10k_suspend,
4228 .resume = ath10k_resume,
4229#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03004230};
4231
4232#define RATETAB_ENT(_rate, _rateid, _flags) { \
4233 .bitrate = (_rate), \
4234 .flags = (_flags), \
4235 .hw_value = (_rateid), \
4236}
4237
4238#define CHAN2G(_channel, _freq, _flags) { \
4239 .band = IEEE80211_BAND_2GHZ, \
4240 .hw_value = (_channel), \
4241 .center_freq = (_freq), \
4242 .flags = (_flags), \
4243 .max_antenna_gain = 0, \
4244 .max_power = 30, \
4245}
4246
4247#define CHAN5G(_channel, _freq, _flags) { \
4248 .band = IEEE80211_BAND_5GHZ, \
4249 .hw_value = (_channel), \
4250 .center_freq = (_freq), \
4251 .flags = (_flags), \
4252 .max_antenna_gain = 0, \
4253 .max_power = 30, \
4254}
4255
4256static const struct ieee80211_channel ath10k_2ghz_channels[] = {
4257 CHAN2G(1, 2412, 0),
4258 CHAN2G(2, 2417, 0),
4259 CHAN2G(3, 2422, 0),
4260 CHAN2G(4, 2427, 0),
4261 CHAN2G(5, 2432, 0),
4262 CHAN2G(6, 2437, 0),
4263 CHAN2G(7, 2442, 0),
4264 CHAN2G(8, 2447, 0),
4265 CHAN2G(9, 2452, 0),
4266 CHAN2G(10, 2457, 0),
4267 CHAN2G(11, 2462, 0),
4268 CHAN2G(12, 2467, 0),
4269 CHAN2G(13, 2472, 0),
4270 CHAN2G(14, 2484, 0),
4271};
4272
4273static const struct ieee80211_channel ath10k_5ghz_channels[] = {
Michal Kazior429ff562013-06-26 08:54:54 +02004274 CHAN5G(36, 5180, 0),
4275 CHAN5G(40, 5200, 0),
4276 CHAN5G(44, 5220, 0),
4277 CHAN5G(48, 5240, 0),
4278 CHAN5G(52, 5260, 0),
4279 CHAN5G(56, 5280, 0),
4280 CHAN5G(60, 5300, 0),
4281 CHAN5G(64, 5320, 0),
4282 CHAN5G(100, 5500, 0),
4283 CHAN5G(104, 5520, 0),
4284 CHAN5G(108, 5540, 0),
4285 CHAN5G(112, 5560, 0),
4286 CHAN5G(116, 5580, 0),
4287 CHAN5G(120, 5600, 0),
4288 CHAN5G(124, 5620, 0),
4289 CHAN5G(128, 5640, 0),
4290 CHAN5G(132, 5660, 0),
4291 CHAN5G(136, 5680, 0),
4292 CHAN5G(140, 5700, 0),
4293 CHAN5G(149, 5745, 0),
4294 CHAN5G(153, 5765, 0),
4295 CHAN5G(157, 5785, 0),
4296 CHAN5G(161, 5805, 0),
4297 CHAN5G(165, 5825, 0),
Kalle Valo5e3dd152013-06-12 20:52:10 +03004298};
4299
4300static struct ieee80211_rate ath10k_rates[] = {
4301 /* CCK */
4302 RATETAB_ENT(10, 0x82, 0),
4303 RATETAB_ENT(20, 0x84, 0),
4304 RATETAB_ENT(55, 0x8b, 0),
4305 RATETAB_ENT(110, 0x96, 0),
4306 /* OFDM */
4307 RATETAB_ENT(60, 0x0c, 0),
4308 RATETAB_ENT(90, 0x12, 0),
4309 RATETAB_ENT(120, 0x18, 0),
4310 RATETAB_ENT(180, 0x24, 0),
4311 RATETAB_ENT(240, 0x30, 0),
4312 RATETAB_ENT(360, 0x48, 0),
4313 RATETAB_ENT(480, 0x60, 0),
4314 RATETAB_ENT(540, 0x6c, 0),
4315};
4316
4317#define ath10k_a_rates (ath10k_rates + 4)
4318#define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - 4)
4319#define ath10k_g_rates (ath10k_rates + 0)
4320#define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
4321
4322struct ath10k *ath10k_mac_create(void)
4323{
4324 struct ieee80211_hw *hw;
4325 struct ath10k *ar;
4326
4327 hw = ieee80211_alloc_hw(sizeof(struct ath10k), &ath10k_ops);
4328 if (!hw)
4329 return NULL;
4330
4331 ar = hw->priv;
4332 ar->hw = hw;
4333
4334 return ar;
4335}
4336
4337void ath10k_mac_destroy(struct ath10k *ar)
4338{
4339 ieee80211_free_hw(ar->hw);
4340}
4341
4342static const struct ieee80211_iface_limit ath10k_if_limits[] = {
4343 {
4344 .max = 8,
4345 .types = BIT(NL80211_IFTYPE_STATION)
4346 | BIT(NL80211_IFTYPE_P2P_CLIENT)
Michal Kaziord531cb82013-07-31 10:55:13 +02004347 },
4348 {
4349 .max = 3,
4350 .types = BIT(NL80211_IFTYPE_P2P_GO)
4351 },
4352 {
4353 .max = 7,
4354 .types = BIT(NL80211_IFTYPE_AP)
4355 },
Kalle Valo5e3dd152013-06-12 20:52:10 +03004356};
4357
Bartosz Markowskif2595092013-12-10 16:20:39 +01004358static const struct ieee80211_iface_limit ath10k_10x_if_limits[] = {
Marek Puzyniake8a50f82013-11-20 09:59:47 +02004359 {
4360 .max = 8,
4361 .types = BIT(NL80211_IFTYPE_AP)
4362 },
4363};
Marek Puzyniake8a50f82013-11-20 09:59:47 +02004364
4365static const struct ieee80211_iface_combination ath10k_if_comb[] = {
4366 {
4367 .limits = ath10k_if_limits,
4368 .n_limits = ARRAY_SIZE(ath10k_if_limits),
4369 .max_interfaces = 8,
4370 .num_different_channels = 1,
4371 .beacon_int_infra_match = true,
4372 },
Bartosz Markowskif2595092013-12-10 16:20:39 +01004373};
4374
4375static const struct ieee80211_iface_combination ath10k_10x_if_comb[] = {
Marek Puzyniake8a50f82013-11-20 09:59:47 +02004376 {
Bartosz Markowskif2595092013-12-10 16:20:39 +01004377 .limits = ath10k_10x_if_limits,
4378 .n_limits = ARRAY_SIZE(ath10k_10x_if_limits),
Marek Puzyniake8a50f82013-11-20 09:59:47 +02004379 .max_interfaces = 8,
4380 .num_different_channels = 1,
4381 .beacon_int_infra_match = true,
Bartosz Markowskif2595092013-12-10 16:20:39 +01004382#ifdef CONFIG_ATH10K_DFS_CERTIFIED
Marek Puzyniake8a50f82013-11-20 09:59:47 +02004383 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
4384 BIT(NL80211_CHAN_WIDTH_20) |
4385 BIT(NL80211_CHAN_WIDTH_40) |
4386 BIT(NL80211_CHAN_WIDTH_80),
Marek Puzyniake8a50f82013-11-20 09:59:47 +02004387#endif
Bartosz Markowskif2595092013-12-10 16:20:39 +01004388 },
Kalle Valo5e3dd152013-06-12 20:52:10 +03004389};
4390
4391static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
4392{
4393 struct ieee80211_sta_vht_cap vht_cap = {0};
4394 u16 mcs_map;
Michal Kazior8865bee42013-07-24 12:36:46 +02004395 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004396
4397 vht_cap.vht_supported = 1;
4398 vht_cap.cap = ar->vht_cap_info;
4399
Michal Kazior8865bee42013-07-24 12:36:46 +02004400 mcs_map = 0;
4401 for (i = 0; i < 8; i++) {
4402 if (i < ar->num_rf_chains)
4403 mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i*2);
4404 else
4405 mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i*2);
4406 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03004407
4408 vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
4409 vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
4410
4411 return vht_cap;
4412}
4413
4414static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
4415{
4416 int i;
4417 struct ieee80211_sta_ht_cap ht_cap = {0};
4418
4419 if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
4420 return ht_cap;
4421
4422 ht_cap.ht_supported = 1;
4423 ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
4424 ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
4425 ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
4426 ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
4427 ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
4428
4429 if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
4430 ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
4431
4432 if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
4433 ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
4434
4435 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
4436 u32 smps;
4437
4438 smps = WLAN_HT_CAP_SM_PS_DYNAMIC;
4439 smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
4440
4441 ht_cap.cap |= smps;
4442 }
4443
4444 if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC)
4445 ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
4446
4447 if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
4448 u32 stbc;
4449
4450 stbc = ar->ht_cap_info;
4451 stbc &= WMI_HT_CAP_RX_STBC;
4452 stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
4453 stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
4454 stbc &= IEEE80211_HT_CAP_RX_STBC;
4455
4456 ht_cap.cap |= stbc;
4457 }
4458
4459 if (ar->ht_cap_info & WMI_HT_CAP_LDPC)
4460 ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
4461
4462 if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
4463 ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
4464
4465 /* max AMSDU is implicitly taken from vht_cap_info */
4466 if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
4467 ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
4468
Michal Kazior8865bee42013-07-24 12:36:46 +02004469 for (i = 0; i < ar->num_rf_chains; i++)
Kalle Valo5e3dd152013-06-12 20:52:10 +03004470 ht_cap.mcs.rx_mask[i] = 0xFF;
4471
4472 ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
4473
4474 return ht_cap;
4475}
4476
4477
4478static void ath10k_get_arvif_iter(void *data, u8 *mac,
4479 struct ieee80211_vif *vif)
4480{
4481 struct ath10k_vif_iter *arvif_iter = data;
4482 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4483
4484 if (arvif->vdev_id == arvif_iter->vdev_id)
4485 arvif_iter->arvif = arvif;
4486}
4487
4488struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
4489{
4490 struct ath10k_vif_iter arvif_iter;
4491 u32 flags;
4492
4493 memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
4494 arvif_iter.vdev_id = vdev_id;
4495
4496 flags = IEEE80211_IFACE_ITER_RESUME_ALL;
4497 ieee80211_iterate_active_interfaces_atomic(ar->hw,
4498 flags,
4499 ath10k_get_arvif_iter,
4500 &arvif_iter);
4501 if (!arvif_iter.arvif) {
Ben Greear69244e52014-02-27 18:50:00 +02004502 ath10k_warn("No VIF found for vdev %d\n", vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004503 return NULL;
4504 }
4505
4506 return arvif_iter.arvif;
4507}
4508
4509int ath10k_mac_register(struct ath10k *ar)
4510{
4511 struct ieee80211_supported_band *band;
4512 struct ieee80211_sta_vht_cap vht_cap;
4513 struct ieee80211_sta_ht_cap ht_cap;
4514 void *channels;
4515 int ret;
4516
4517 SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
4518
4519 SET_IEEE80211_DEV(ar->hw, ar->dev);
4520
4521 ht_cap = ath10k_get_ht_cap(ar);
4522 vht_cap = ath10k_create_vht_cap(ar);
4523
4524 if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
4525 channels = kmemdup(ath10k_2ghz_channels,
4526 sizeof(ath10k_2ghz_channels),
4527 GFP_KERNEL);
Michal Kaziord6015b22013-07-22 14:13:30 +02004528 if (!channels) {
4529 ret = -ENOMEM;
4530 goto err_free;
4531 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03004532
4533 band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
4534 band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
4535 band->channels = channels;
4536 band->n_bitrates = ath10k_g_rates_size;
4537 band->bitrates = ath10k_g_rates;
4538 band->ht_cap = ht_cap;
4539
4540 /* vht is not supported in 2.4 GHz */
4541
4542 ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
4543 }
4544
4545 if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
4546 channels = kmemdup(ath10k_5ghz_channels,
4547 sizeof(ath10k_5ghz_channels),
4548 GFP_KERNEL);
4549 if (!channels) {
Michal Kaziord6015b22013-07-22 14:13:30 +02004550 ret = -ENOMEM;
4551 goto err_free;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004552 }
4553
4554 band = &ar->mac.sbands[IEEE80211_BAND_5GHZ];
4555 band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
4556 band->channels = channels;
4557 band->n_bitrates = ath10k_a_rates_size;
4558 band->bitrates = ath10k_a_rates;
4559 band->ht_cap = ht_cap;
4560 band->vht_cap = vht_cap;
4561 ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
4562 }
4563
4564 ar->hw->wiphy->interface_modes =
4565 BIT(NL80211_IFTYPE_STATION) |
4566 BIT(NL80211_IFTYPE_ADHOC) |
Bartosz Markowskid3541812013-12-10 16:20:40 +01004567 BIT(NL80211_IFTYPE_AP);
4568
4569 if (!test_bit(ATH10K_FW_FEATURE_NO_P2P, ar->fw_features))
4570 ar->hw->wiphy->interface_modes |=
4571 BIT(NL80211_IFTYPE_P2P_CLIENT) |
4572 BIT(NL80211_IFTYPE_P2P_GO);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004573
4574 ar->hw->flags = IEEE80211_HW_SIGNAL_DBM |
4575 IEEE80211_HW_SUPPORTS_PS |
4576 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
4577 IEEE80211_HW_SUPPORTS_UAPSD |
4578 IEEE80211_HW_MFP_CAPABLE |
4579 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
4580 IEEE80211_HW_HAS_RATE_CONTROL |
4581 IEEE80211_HW_SUPPORTS_STATIC_SMPS |
4582 IEEE80211_HW_WANT_MONITOR_VIF |
Janusz Dziedzic2f0f1122014-02-26 18:42:09 +02004583 IEEE80211_HW_AP_LINK_PS |
4584 IEEE80211_HW_SPECTRUM_MGMT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004585
Michal Kazior1f8bb152013-09-18 14:43:22 +02004586 /* MSDU can have HTT TX fragment pushed in front. The additional 4
4587 * bytes is used for padding/alignment if necessary. */
4588 ar->hw->extra_tx_headroom += sizeof(struct htt_data_tx_desc_frag)*2 + 4;
4589
Kalle Valo5e3dd152013-06-12 20:52:10 +03004590 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
4591 ar->hw->flags |= IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS;
4592
4593 if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
4594 ar->hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
4595 ar->hw->flags |= IEEE80211_HW_TX_AMPDU_SETUP_IN_HW;
4596 }
4597
4598 ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
4599 ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
4600
4601 ar->hw->vif_data_size = sizeof(struct ath10k_vif);
Michal Kazior9797feb2014-02-14 14:49:48 +01004602 ar->hw->sta_data_size = sizeof(struct ath10k_sta);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004603
Kalle Valo5e3dd152013-06-12 20:52:10 +03004604 ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
4605
4606 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
Michal Kaziorc2df44b2014-01-23 11:38:26 +01004607 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004608 ar->hw->wiphy->max_remain_on_channel_duration = 5000;
4609
4610 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
4611 /*
4612 * on LL hardware queues are managed entirely by the FW
4613 * so we only advertise to mac we can do the queues thing
4614 */
4615 ar->hw->queues = 4;
4616
Bartosz Markowskif2595092013-12-10 16:20:39 +01004617 if (test_bit(ATH10K_FW_FEATURE_WMI_10X, ar->fw_features)) {
4618 ar->hw->wiphy->iface_combinations = ath10k_10x_if_comb;
4619 ar->hw->wiphy->n_iface_combinations =
4620 ARRAY_SIZE(ath10k_10x_if_comb);
4621 } else {
4622 ar->hw->wiphy->iface_combinations = ath10k_if_comb;
4623 ar->hw->wiphy->n_iface_combinations =
4624 ARRAY_SIZE(ath10k_if_comb);
4625 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03004626
Michal Kazior7c199992013-07-31 10:47:57 +02004627 ar->hw->netdev_features = NETIF_F_HW_CSUM;
4628
Janusz Dziedzic9702c682013-11-20 09:59:41 +02004629 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED)) {
4630 /* Init ath dfs pattern detector */
4631 ar->ath_common.debug_mask = ATH_DBG_DFS;
4632 ar->dfs_detector = dfs_pattern_detector_init(&ar->ath_common,
4633 NL80211_DFS_UNSET);
4634
4635 if (!ar->dfs_detector)
Kalle Valobe6546f2014-03-25 14:18:51 +02004636 ath10k_warn("failed to initialise DFS pattern detector\n");
Janusz Dziedzic9702c682013-11-20 09:59:41 +02004637 }
4638
Kalle Valo5e3dd152013-06-12 20:52:10 +03004639 ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
4640 ath10k_reg_notifier);
4641 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02004642 ath10k_err("failed to initialise regulatory: %i\n", ret);
Michal Kaziord6015b22013-07-22 14:13:30 +02004643 goto err_free;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004644 }
4645
4646 ret = ieee80211_register_hw(ar->hw);
4647 if (ret) {
Kalle Valobe6546f2014-03-25 14:18:51 +02004648 ath10k_err("failed to register ieee80211: %d\n", ret);
Michal Kaziord6015b22013-07-22 14:13:30 +02004649 goto err_free;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004650 }
4651
4652 if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
4653 ret = regulatory_hint(ar->hw->wiphy,
4654 ar->ath_common.regulatory.alpha2);
4655 if (ret)
Michal Kaziord6015b22013-07-22 14:13:30 +02004656 goto err_unregister;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004657 }
4658
4659 return 0;
Michal Kaziord6015b22013-07-22 14:13:30 +02004660
4661err_unregister:
Kalle Valo5e3dd152013-06-12 20:52:10 +03004662 ieee80211_unregister_hw(ar->hw);
Michal Kaziord6015b22013-07-22 14:13:30 +02004663err_free:
4664 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
4665 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
4666
Kalle Valo5e3dd152013-06-12 20:52:10 +03004667 return ret;
4668}
4669
4670void ath10k_mac_unregister(struct ath10k *ar)
4671{
4672 ieee80211_unregister_hw(ar->hw);
4673
Janusz Dziedzic9702c682013-11-20 09:59:41 +02004674 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
4675 ar->dfs_detector->exit(ar->dfs_detector);
4676
Kalle Valo5e3dd152013-06-12 20:52:10 +03004677 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
4678 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
4679
4680 SET_IEEE80211_DEV(ar->hw, NULL);
4681}