blob: e475cc4095b62a2e1a72e880029837f4c6340490 [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"
Kalle Valo43d2a302014-09-10 18:23:30 +030029#include "testmode.h"
Kalle Valo5e3dd152013-06-12 20:52:10 +030030
31/**********/
32/* Crypto */
33/**********/
34
35static int ath10k_send_key(struct ath10k_vif *arvif,
36 struct ieee80211_key_conf *key,
37 enum set_key_cmd cmd,
38 const u8 *macaddr)
39{
Michal Kazior7aa7a722014-08-25 12:09:38 +020040 struct ath10k *ar = arvif->ar;
Kalle Valo5e3dd152013-06-12 20:52:10 +030041 struct wmi_vdev_install_key_arg arg = {
42 .vdev_id = arvif->vdev_id,
43 .key_idx = key->keyidx,
44 .key_len = key->keylen,
45 .key_data = key->key,
46 .macaddr = macaddr,
47 };
48
Michal Kazior548db542013-07-05 16:15:15 +030049 lockdep_assert_held(&arvif->ar->conf_mutex);
50
Kalle Valo5e3dd152013-06-12 20:52:10 +030051 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
52 arg.key_flags = WMI_KEY_PAIRWISE;
53 else
54 arg.key_flags = WMI_KEY_GROUP;
55
56 switch (key->cipher) {
57 case WLAN_CIPHER_SUITE_CCMP:
58 arg.key_cipher = WMI_CIPHER_AES_CCM;
Marek Kwaczynskieeab2662014-05-14 16:56:17 +030059 if (arvif->vdev_type == WMI_VDEV_TYPE_AP)
60 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
61 else
62 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
Kalle Valo5e3dd152013-06-12 20:52:10 +030063 break;
64 case WLAN_CIPHER_SUITE_TKIP:
Kalle Valo5e3dd152013-06-12 20:52:10 +030065 arg.key_cipher = WMI_CIPHER_TKIP;
66 arg.key_txmic_len = 8;
67 arg.key_rxmic_len = 8;
68 break;
69 case WLAN_CIPHER_SUITE_WEP40:
70 case WLAN_CIPHER_SUITE_WEP104:
71 arg.key_cipher = WMI_CIPHER_WEP;
72 /* AP/IBSS mode requires self-key to be groupwise
73 * Otherwise pairwise key must be set */
74 if (memcmp(macaddr, arvif->vif->addr, ETH_ALEN))
75 arg.key_flags = WMI_KEY_PAIRWISE;
76 break;
77 default:
Michal Kazior7aa7a722014-08-25 12:09:38 +020078 ath10k_warn(ar, "cipher %d is not supported\n", key->cipher);
Kalle Valo5e3dd152013-06-12 20:52:10 +030079 return -EOPNOTSUPP;
80 }
81
82 if (cmd == DISABLE_KEY) {
83 arg.key_cipher = WMI_CIPHER_NONE;
84 arg.key_data = NULL;
85 }
86
87 return ath10k_wmi_vdev_install_key(arvif->ar, &arg);
88}
89
90static int ath10k_install_key(struct ath10k_vif *arvif,
91 struct ieee80211_key_conf *key,
92 enum set_key_cmd cmd,
93 const u8 *macaddr)
94{
95 struct ath10k *ar = arvif->ar;
96 int ret;
97
Michal Kazior548db542013-07-05 16:15:15 +030098 lockdep_assert_held(&ar->conf_mutex);
99
Wolfram Sang16735d02013-11-14 14:32:02 -0800100 reinit_completion(&ar->install_key_done);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300101
102 ret = ath10k_send_key(arvif, key, cmd, macaddr);
103 if (ret)
104 return ret;
105
106 ret = wait_for_completion_timeout(&ar->install_key_done, 3*HZ);
107 if (ret == 0)
108 return -ETIMEDOUT;
109
110 return 0;
111}
112
113static int ath10k_install_peer_wep_keys(struct ath10k_vif *arvif,
114 const u8 *addr)
115{
116 struct ath10k *ar = arvif->ar;
117 struct ath10k_peer *peer;
118 int ret;
119 int i;
120
121 lockdep_assert_held(&ar->conf_mutex);
122
123 spin_lock_bh(&ar->data_lock);
124 peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
125 spin_unlock_bh(&ar->data_lock);
126
127 if (!peer)
128 return -ENOENT;
129
130 for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
131 if (arvif->wep_keys[i] == NULL)
132 continue;
133
134 ret = ath10k_install_key(arvif, arvif->wep_keys[i], SET_KEY,
135 addr);
136 if (ret)
137 return ret;
138
Sujith Manoharanae167132014-11-25 11:46:59 +0530139 spin_lock_bh(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300140 peer->keys[i] = arvif->wep_keys[i];
Sujith Manoharanae167132014-11-25 11:46:59 +0530141 spin_unlock_bh(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300142 }
143
144 return 0;
145}
146
147static int ath10k_clear_peer_keys(struct ath10k_vif *arvif,
148 const u8 *addr)
149{
150 struct ath10k *ar = arvif->ar;
151 struct ath10k_peer *peer;
152 int first_errno = 0;
153 int ret;
154 int i;
155
156 lockdep_assert_held(&ar->conf_mutex);
157
158 spin_lock_bh(&ar->data_lock);
159 peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
160 spin_unlock_bh(&ar->data_lock);
161
162 if (!peer)
163 return -ENOENT;
164
165 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
166 if (peer->keys[i] == NULL)
167 continue;
168
169 ret = ath10k_install_key(arvif, peer->keys[i],
170 DISABLE_KEY, addr);
171 if (ret && first_errno == 0)
172 first_errno = ret;
173
174 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200175 ath10k_warn(ar, "failed to remove peer wep key %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300176 i, ret);
177
Sujith Manoharanae167132014-11-25 11:46:59 +0530178 spin_lock_bh(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300179 peer->keys[i] = NULL;
Sujith Manoharanae167132014-11-25 11:46:59 +0530180 spin_unlock_bh(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300181 }
182
183 return first_errno;
184}
185
Sujith Manoharan504f6cd2014-11-25 11:46:58 +0530186bool ath10k_mac_is_peer_wep_key_set(struct ath10k *ar, const u8 *addr,
187 u8 keyidx)
188{
189 struct ath10k_peer *peer;
190 int i;
191
192 lockdep_assert_held(&ar->data_lock);
193
194 /* We don't know which vdev this peer belongs to,
195 * since WMI doesn't give us that information.
196 *
197 * FIXME: multi-bss needs to be handled.
198 */
199 peer = ath10k_peer_find(ar, 0, addr);
200 if (!peer)
201 return false;
202
203 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
204 if (peer->keys[i] && peer->keys[i]->keyidx == keyidx)
205 return true;
206 }
207
208 return false;
209}
210
Kalle Valo5e3dd152013-06-12 20:52:10 +0300211static int ath10k_clear_vdev_key(struct ath10k_vif *arvif,
212 struct ieee80211_key_conf *key)
213{
214 struct ath10k *ar = arvif->ar;
215 struct ath10k_peer *peer;
216 u8 addr[ETH_ALEN];
217 int first_errno = 0;
218 int ret;
219 int i;
220
221 lockdep_assert_held(&ar->conf_mutex);
222
223 for (;;) {
224 /* since ath10k_install_key we can't hold data_lock all the
225 * time, so we try to remove the keys incrementally */
226 spin_lock_bh(&ar->data_lock);
227 i = 0;
228 list_for_each_entry(peer, &ar->peers, list) {
229 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
230 if (peer->keys[i] == key) {
Kalle Valob25f32c2014-09-14 12:50:49 +0300231 ether_addr_copy(addr, peer->addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300232 peer->keys[i] = NULL;
233 break;
234 }
235 }
236
237 if (i < ARRAY_SIZE(peer->keys))
238 break;
239 }
240 spin_unlock_bh(&ar->data_lock);
241
242 if (i == ARRAY_SIZE(peer->keys))
243 break;
244
245 ret = ath10k_install_key(arvif, key, DISABLE_KEY, addr);
246 if (ret && first_errno == 0)
247 first_errno = ret;
248
249 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200250 ath10k_warn(ar, "failed to remove key for %pM: %d\n",
Kalle Valobe6546f2014-03-25 14:18:51 +0200251 addr, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300252 }
253
254 return first_errno;
255}
256
Kalle Valo5e3dd152013-06-12 20:52:10 +0300257/*********************/
258/* General utilities */
259/*********************/
260
261static inline enum wmi_phy_mode
262chan_to_phymode(const struct cfg80211_chan_def *chandef)
263{
264 enum wmi_phy_mode phymode = MODE_UNKNOWN;
265
266 switch (chandef->chan->band) {
267 case IEEE80211_BAND_2GHZ:
268 switch (chandef->width) {
269 case NL80211_CHAN_WIDTH_20_NOHT:
270 phymode = MODE_11G;
271 break;
272 case NL80211_CHAN_WIDTH_20:
273 phymode = MODE_11NG_HT20;
274 break;
275 case NL80211_CHAN_WIDTH_40:
276 phymode = MODE_11NG_HT40;
277 break;
John W. Linville0f817ed2013-06-27 13:50:09 -0400278 case NL80211_CHAN_WIDTH_5:
279 case NL80211_CHAN_WIDTH_10:
Kalle Valo5e3dd152013-06-12 20:52:10 +0300280 case NL80211_CHAN_WIDTH_80:
281 case NL80211_CHAN_WIDTH_80P80:
282 case NL80211_CHAN_WIDTH_160:
283 phymode = MODE_UNKNOWN;
284 break;
285 }
286 break;
287 case IEEE80211_BAND_5GHZ:
288 switch (chandef->width) {
289 case NL80211_CHAN_WIDTH_20_NOHT:
290 phymode = MODE_11A;
291 break;
292 case NL80211_CHAN_WIDTH_20:
293 phymode = MODE_11NA_HT20;
294 break;
295 case NL80211_CHAN_WIDTH_40:
296 phymode = MODE_11NA_HT40;
297 break;
298 case NL80211_CHAN_WIDTH_80:
299 phymode = MODE_11AC_VHT80;
300 break;
John W. Linville0f817ed2013-06-27 13:50:09 -0400301 case NL80211_CHAN_WIDTH_5:
302 case NL80211_CHAN_WIDTH_10:
Kalle Valo5e3dd152013-06-12 20:52:10 +0300303 case NL80211_CHAN_WIDTH_80P80:
304 case NL80211_CHAN_WIDTH_160:
305 phymode = MODE_UNKNOWN;
306 break;
307 }
308 break;
309 default:
310 break;
311 }
312
313 WARN_ON(phymode == MODE_UNKNOWN);
314 return phymode;
315}
316
317static u8 ath10k_parse_mpdudensity(u8 mpdudensity)
318{
319/*
320 * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
321 * 0 for no restriction
322 * 1 for 1/4 us
323 * 2 for 1/2 us
324 * 3 for 1 us
325 * 4 for 2 us
326 * 5 for 4 us
327 * 6 for 8 us
328 * 7 for 16 us
329 */
330 switch (mpdudensity) {
331 case 0:
332 return 0;
333 case 1:
334 case 2:
335 case 3:
336 /* Our lower layer calculations limit our precision to
337 1 microsecond */
338 return 1;
339 case 4:
340 return 2;
341 case 5:
342 return 4;
343 case 6:
344 return 8;
345 case 7:
346 return 16;
347 default:
348 return 0;
349 }
350}
351
352static int ath10k_peer_create(struct ath10k *ar, u32 vdev_id, const u8 *addr)
353{
354 int ret;
355
356 lockdep_assert_held(&ar->conf_mutex);
357
Michal Kaziorcfd10612014-11-25 15:16:05 +0100358 if (ar->num_peers >= ar->max_num_peers)
359 return -ENOBUFS;
360
Kalle Valo5e3dd152013-06-12 20:52:10 +0300361 ret = ath10k_wmi_peer_create(ar, vdev_id, addr);
Ben Greear479398b2013-11-04 09:19:34 -0800362 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200363 ath10k_warn(ar, "failed to create wmi peer %pM on vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +0200364 addr, vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300365 return ret;
Ben Greear479398b2013-11-04 09:19:34 -0800366 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300367
368 ret = ath10k_wait_for_peer_created(ar, vdev_id, addr);
Ben Greear479398b2013-11-04 09:19:34 -0800369 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200370 ath10k_warn(ar, "failed to wait for created wmi peer %pM on vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +0200371 addr, vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300372 return ret;
Ben Greear479398b2013-11-04 09:19:34 -0800373 }
Michal Kazior292a7532014-11-25 15:16:04 +0100374
Bartosz Markowski0e759f32014-01-02 14:38:33 +0100375 ar->num_peers++;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300376
377 return 0;
378}
379
Kalle Valo5a13e762014-01-20 11:01:46 +0200380static int ath10k_mac_set_kickout(struct ath10k_vif *arvif)
381{
382 struct ath10k *ar = arvif->ar;
383 u32 param;
384 int ret;
385
386 param = ar->wmi.pdev_param->sta_kickout_th;
387 ret = ath10k_wmi_pdev_set_param(ar, param,
388 ATH10K_KICKOUT_THRESHOLD);
389 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200390 ath10k_warn(ar, "failed to set kickout threshold on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200391 arvif->vdev_id, ret);
Kalle Valo5a13e762014-01-20 11:01:46 +0200392 return ret;
393 }
394
395 param = ar->wmi.vdev_param->ap_keepalive_min_idle_inactive_time_secs;
396 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
397 ATH10K_KEEPALIVE_MIN_IDLE);
398 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200399 ath10k_warn(ar, "failed to set keepalive minimum idle time on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200400 arvif->vdev_id, ret);
Kalle Valo5a13e762014-01-20 11:01:46 +0200401 return ret;
402 }
403
404 param = ar->wmi.vdev_param->ap_keepalive_max_idle_inactive_time_secs;
405 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
406 ATH10K_KEEPALIVE_MAX_IDLE);
407 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200408 ath10k_warn(ar, "failed to set keepalive maximum idle time on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200409 arvif->vdev_id, ret);
Kalle Valo5a13e762014-01-20 11:01:46 +0200410 return ret;
411 }
412
413 param = ar->wmi.vdev_param->ap_keepalive_max_unresponsive_time_secs;
414 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
415 ATH10K_KEEPALIVE_MAX_UNRESPONSIVE);
416 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200417 ath10k_warn(ar, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200418 arvif->vdev_id, ret);
Kalle Valo5a13e762014-01-20 11:01:46 +0200419 return ret;
420 }
421
422 return 0;
423}
424
Michal Kazior424121c2013-07-22 14:13:31 +0200425static int ath10k_mac_set_rts(struct ath10k_vif *arvif, u32 value)
426{
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200427 struct ath10k *ar = arvif->ar;
428 u32 vdev_param;
429
Michal Kazior424121c2013-07-22 14:13:31 +0200430 if (value != 0xFFFFFFFF)
431 value = min_t(u32, arvif->ar->hw->wiphy->rts_threshold,
432 ATH10K_RTS_MAX);
433
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200434 vdev_param = ar->wmi.vdev_param->rts_threshold;
435 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
Michal Kazior424121c2013-07-22 14:13:31 +0200436}
437
438static int ath10k_mac_set_frag(struct ath10k_vif *arvif, u32 value)
439{
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200440 struct ath10k *ar = arvif->ar;
441 u32 vdev_param;
442
Michal Kazior424121c2013-07-22 14:13:31 +0200443 if (value != 0xFFFFFFFF)
444 value = clamp_t(u32, arvif->ar->hw->wiphy->frag_threshold,
445 ATH10K_FRAGMT_THRESHOLD_MIN,
446 ATH10K_FRAGMT_THRESHOLD_MAX);
447
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200448 vdev_param = ar->wmi.vdev_param->fragmentation_threshold;
449 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
Michal Kazior424121c2013-07-22 14:13:31 +0200450}
451
Kalle Valo5e3dd152013-06-12 20:52:10 +0300452static int ath10k_peer_delete(struct ath10k *ar, u32 vdev_id, const u8 *addr)
453{
454 int ret;
455
456 lockdep_assert_held(&ar->conf_mutex);
457
458 ret = ath10k_wmi_peer_delete(ar, vdev_id, addr);
459 if (ret)
460 return ret;
461
462 ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
463 if (ret)
464 return ret;
465
Bartosz Markowski0e759f32014-01-02 14:38:33 +0100466 ar->num_peers--;
Bartosz Markowski0e759f32014-01-02 14:38:33 +0100467
Kalle Valo5e3dd152013-06-12 20:52:10 +0300468 return 0;
469}
470
471static void ath10k_peer_cleanup(struct ath10k *ar, u32 vdev_id)
472{
473 struct ath10k_peer *peer, *tmp;
474
475 lockdep_assert_held(&ar->conf_mutex);
476
477 spin_lock_bh(&ar->data_lock);
478 list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
479 if (peer->vdev_id != vdev_id)
480 continue;
481
Michal Kazior7aa7a722014-08-25 12:09:38 +0200482 ath10k_warn(ar, "removing stale peer %pM from vdev_id %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300483 peer->addr, vdev_id);
484
485 list_del(&peer->list);
486 kfree(peer);
Bartosz Markowski0e759f32014-01-02 14:38:33 +0100487 ar->num_peers--;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300488 }
489 spin_unlock_bh(&ar->data_lock);
490}
491
Michal Kaziora96d7742013-07-16 09:38:56 +0200492static void ath10k_peer_cleanup_all(struct ath10k *ar)
493{
494 struct ath10k_peer *peer, *tmp;
495
496 lockdep_assert_held(&ar->conf_mutex);
497
498 spin_lock_bh(&ar->data_lock);
499 list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
500 list_del(&peer->list);
501 kfree(peer);
502 }
503 spin_unlock_bh(&ar->data_lock);
Michal Kazior292a7532014-11-25 15:16:04 +0100504
505 ar->num_peers = 0;
Michal Kaziorcfd10612014-11-25 15:16:05 +0100506 ar->num_stations = 0;
Michal Kaziora96d7742013-07-16 09:38:56 +0200507}
508
Kalle Valo5e3dd152013-06-12 20:52:10 +0300509/************************/
510/* Interface management */
511/************************/
512
Michal Kazior64badcb2014-09-18 11:18:02 +0300513void ath10k_mac_vif_beacon_free(struct ath10k_vif *arvif)
514{
515 struct ath10k *ar = arvif->ar;
516
517 lockdep_assert_held(&ar->data_lock);
518
519 if (!arvif->beacon)
520 return;
521
522 if (!arvif->beacon_buf)
523 dma_unmap_single(ar->dev, ATH10K_SKB_CB(arvif->beacon)->paddr,
524 arvif->beacon->len, DMA_TO_DEVICE);
525
526 dev_kfree_skb_any(arvif->beacon);
527
528 arvif->beacon = NULL;
529 arvif->beacon_sent = false;
530}
531
532static void ath10k_mac_vif_beacon_cleanup(struct ath10k_vif *arvif)
533{
534 struct ath10k *ar = arvif->ar;
535
536 lockdep_assert_held(&ar->data_lock);
537
538 ath10k_mac_vif_beacon_free(arvif);
539
540 if (arvif->beacon_buf) {
541 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
542 arvif->beacon_buf, arvif->beacon_paddr);
543 arvif->beacon_buf = NULL;
544 }
545}
546
Kalle Valo5e3dd152013-06-12 20:52:10 +0300547static inline int ath10k_vdev_setup_sync(struct ath10k *ar)
548{
549 int ret;
550
Michal Kazior548db542013-07-05 16:15:15 +0300551 lockdep_assert_held(&ar->conf_mutex);
552
Michal Kazior7962b0d2014-10-28 10:34:38 +0100553 if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags))
554 return -ESHUTDOWN;
555
Kalle Valo5e3dd152013-06-12 20:52:10 +0300556 ret = wait_for_completion_timeout(&ar->vdev_setup_done,
557 ATH10K_VDEV_SETUP_TIMEOUT_HZ);
558 if (ret == 0)
559 return -ETIMEDOUT;
560
561 return 0;
562}
563
Michal Kazior1bbc0972014-04-08 09:45:47 +0300564static int ath10k_monitor_vdev_start(struct ath10k *ar, int vdev_id)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300565{
Michal Kaziorc930f742014-01-23 11:38:25 +0100566 struct cfg80211_chan_def *chandef = &ar->chandef;
567 struct ieee80211_channel *channel = chandef->chan;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300568 struct wmi_vdev_start_request_arg arg = {};
Kalle Valo5e3dd152013-06-12 20:52:10 +0300569 int ret = 0;
570
571 lockdep_assert_held(&ar->conf_mutex);
572
Kalle Valo5e3dd152013-06-12 20:52:10 +0300573 arg.vdev_id = vdev_id;
574 arg.channel.freq = channel->center_freq;
Michal Kaziorc930f742014-01-23 11:38:25 +0100575 arg.channel.band_center_freq1 = chandef->center_freq1;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300576
577 /* TODO setup this dynamically, what in case we
578 don't have any vifs? */
Michal Kaziorc930f742014-01-23 11:38:25 +0100579 arg.channel.mode = chan_to_phymode(chandef);
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200580 arg.channel.chan_radar =
581 !!(channel->flags & IEEE80211_CHAN_RADAR);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300582
Michal Kazior89c5c842013-10-23 04:02:13 -0700583 arg.channel.min_power = 0;
Michal Kazior02256932013-10-23 04:02:14 -0700584 arg.channel.max_power = channel->max_power * 2;
585 arg.channel.max_reg_power = channel->max_reg_power * 2;
586 arg.channel.max_antenna_gain = channel->max_antenna_gain * 2;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300587
Michal Kazior7962b0d2014-10-28 10:34:38 +0100588 reinit_completion(&ar->vdev_setup_done);
589
Kalle Valo5e3dd152013-06-12 20:52:10 +0300590 ret = ath10k_wmi_vdev_start(ar, &arg);
591 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200592 ath10k_warn(ar, "failed to request monitor vdev %i start: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200593 vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300594 return ret;
595 }
596
597 ret = ath10k_vdev_setup_sync(ar);
598 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200599 ath10k_warn(ar, "failed to synchronize setup for monitor vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200600 vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300601 return ret;
602 }
603
604 ret = ath10k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
605 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200606 ath10k_warn(ar, "failed to put up monitor vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200607 vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300608 goto vdev_stop;
609 }
610
611 ar->monitor_vdev_id = vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300612
Michal Kazior7aa7a722014-08-25 12:09:38 +0200613 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i started\n",
Michal Kazior1bbc0972014-04-08 09:45:47 +0300614 ar->monitor_vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300615 return 0;
616
617vdev_stop:
618 ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
619 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200620 ath10k_warn(ar, "failed to stop monitor vdev %i after start failure: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200621 ar->monitor_vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300622
623 return ret;
624}
625
Michal Kazior1bbc0972014-04-08 09:45:47 +0300626static int ath10k_monitor_vdev_stop(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300627{
628 int ret = 0;
629
630 lockdep_assert_held(&ar->conf_mutex);
631
Marek Puzyniak52fa0192013-09-24 14:06:24 +0200632 ret = ath10k_wmi_vdev_down(ar, ar->monitor_vdev_id);
633 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200634 ath10k_warn(ar, "failed to put down monitor vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200635 ar->monitor_vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300636
Michal Kazior7962b0d2014-10-28 10:34:38 +0100637 reinit_completion(&ar->vdev_setup_done);
638
Kalle Valo5e3dd152013-06-12 20:52:10 +0300639 ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
640 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200641 ath10k_warn(ar, "failed to to request monitor vdev %i stop: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200642 ar->monitor_vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300643
644 ret = ath10k_vdev_setup_sync(ar);
645 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200646 ath10k_warn(ar, "failed to synchronise monitor vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200647 ar->monitor_vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300648
Michal Kazior7aa7a722014-08-25 12:09:38 +0200649 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i stopped\n",
Michal Kazior1bbc0972014-04-08 09:45:47 +0300650 ar->monitor_vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300651 return ret;
652}
653
Michal Kazior1bbc0972014-04-08 09:45:47 +0300654static int ath10k_monitor_vdev_create(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300655{
656 int bit, ret = 0;
657
658 lockdep_assert_held(&ar->conf_mutex);
659
Ben Greeara9aefb32014-08-12 11:02:19 +0300660 if (ar->free_vdev_map == 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200661 ath10k_warn(ar, "failed to find free vdev id for monitor vdev\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +0300662 return -ENOMEM;
663 }
664
Ben Greear16c11172014-09-23 14:17:16 -0700665 bit = __ffs64(ar->free_vdev_map);
Ben Greeara9aefb32014-08-12 11:02:19 +0300666
Ben Greear16c11172014-09-23 14:17:16 -0700667 ar->monitor_vdev_id = bit;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300668
669 ret = ath10k_wmi_vdev_create(ar, ar->monitor_vdev_id,
670 WMI_VDEV_TYPE_MONITOR,
671 0, ar->mac_addr);
672 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200673 ath10k_warn(ar, "failed to request monitor vdev %i creation: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200674 ar->monitor_vdev_id, ret);
Ben Greeara9aefb32014-08-12 11:02:19 +0300675 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300676 }
677
Ben Greear16c11172014-09-23 14:17:16 -0700678 ar->free_vdev_map &= ~(1LL << ar->monitor_vdev_id);
Michal Kazior7aa7a722014-08-25 12:09:38 +0200679 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d created\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300680 ar->monitor_vdev_id);
681
Kalle Valo5e3dd152013-06-12 20:52:10 +0300682 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300683}
684
Michal Kazior1bbc0972014-04-08 09:45:47 +0300685static int ath10k_monitor_vdev_delete(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300686{
687 int ret = 0;
688
689 lockdep_assert_held(&ar->conf_mutex);
690
Kalle Valo5e3dd152013-06-12 20:52:10 +0300691 ret = ath10k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
692 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200693 ath10k_warn(ar, "failed to request wmi monitor vdev %i removal: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200694 ar->monitor_vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300695 return ret;
696 }
697
Ben Greear16c11172014-09-23 14:17:16 -0700698 ar->free_vdev_map |= 1LL << ar->monitor_vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300699
Michal Kazior7aa7a722014-08-25 12:09:38 +0200700 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d deleted\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300701 ar->monitor_vdev_id);
702 return ret;
703}
704
Michal Kazior1bbc0972014-04-08 09:45:47 +0300705static int ath10k_monitor_start(struct ath10k *ar)
706{
707 int ret;
708
709 lockdep_assert_held(&ar->conf_mutex);
710
Michal Kazior1bbc0972014-04-08 09:45:47 +0300711 ret = ath10k_monitor_vdev_create(ar);
712 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200713 ath10k_warn(ar, "failed to create monitor vdev: %d\n", ret);
Michal Kazior1bbc0972014-04-08 09:45:47 +0300714 return ret;
715 }
716
717 ret = ath10k_monitor_vdev_start(ar, ar->monitor_vdev_id);
718 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200719 ath10k_warn(ar, "failed to start monitor vdev: %d\n", ret);
Michal Kazior1bbc0972014-04-08 09:45:47 +0300720 ath10k_monitor_vdev_delete(ar);
721 return ret;
722 }
723
724 ar->monitor_started = true;
Michal Kazior7aa7a722014-08-25 12:09:38 +0200725 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor started\n");
Michal Kazior1bbc0972014-04-08 09:45:47 +0300726
727 return 0;
728}
729
Michal Kazior19337472014-08-28 12:58:16 +0200730static int ath10k_monitor_stop(struct ath10k *ar)
Michal Kazior1bbc0972014-04-08 09:45:47 +0300731{
732 int ret;
733
734 lockdep_assert_held(&ar->conf_mutex);
735
Michal Kazior1bbc0972014-04-08 09:45:47 +0300736 ret = ath10k_monitor_vdev_stop(ar);
Michal Kazior19337472014-08-28 12:58:16 +0200737 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200738 ath10k_warn(ar, "failed to stop monitor vdev: %d\n", ret);
Michal Kazior19337472014-08-28 12:58:16 +0200739 return ret;
740 }
Michal Kazior1bbc0972014-04-08 09:45:47 +0300741
742 ret = ath10k_monitor_vdev_delete(ar);
Michal Kazior19337472014-08-28 12:58:16 +0200743 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200744 ath10k_warn(ar, "failed to delete monitor vdev: %d\n", ret);
Michal Kazior19337472014-08-28 12:58:16 +0200745 return ret;
746 }
Michal Kazior1bbc0972014-04-08 09:45:47 +0300747
748 ar->monitor_started = false;
Michal Kazior7aa7a722014-08-25 12:09:38 +0200749 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopped\n");
Michal Kazior19337472014-08-28 12:58:16 +0200750
751 return 0;
752}
753
754static int ath10k_monitor_recalc(struct ath10k *ar)
755{
756 bool should_start;
757
758 lockdep_assert_held(&ar->conf_mutex);
759
760 should_start = ar->monitor ||
761 ar->filter_flags & FIF_PROMISC_IN_BSS ||
762 test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
763
764 ath10k_dbg(ar, ATH10K_DBG_MAC,
765 "mac monitor recalc started? %d should? %d\n",
766 ar->monitor_started, should_start);
767
768 if (should_start == ar->monitor_started)
769 return 0;
770
771 if (should_start)
772 return ath10k_monitor_start(ar);
Kalle Valod8bb26b2014-09-14 12:50:33 +0300773
774 return ath10k_monitor_stop(ar);
Michal Kazior1bbc0972014-04-08 09:45:47 +0300775}
776
Marek Kwaczynskie81bd102014-03-11 12:58:00 +0200777static int ath10k_recalc_rtscts_prot(struct ath10k_vif *arvif)
778{
779 struct ath10k *ar = arvif->ar;
780 u32 vdev_param, rts_cts = 0;
781
782 lockdep_assert_held(&ar->conf_mutex);
783
784 vdev_param = ar->wmi.vdev_param->enable_rtscts;
785
786 if (arvif->use_cts_prot || arvif->num_legacy_stations > 0)
787 rts_cts |= SM(WMI_RTSCTS_ENABLED, WMI_RTSCTS_SET);
788
789 if (arvif->num_legacy_stations > 0)
790 rts_cts |= SM(WMI_RTSCTS_ACROSS_SW_RETRIES,
791 WMI_RTSCTS_PROFILE);
792
793 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
794 rts_cts);
795}
796
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200797static int ath10k_start_cac(struct ath10k *ar)
798{
799 int ret;
800
801 lockdep_assert_held(&ar->conf_mutex);
802
803 set_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
804
Michal Kazior19337472014-08-28 12:58:16 +0200805 ret = ath10k_monitor_recalc(ar);
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200806 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200807 ath10k_warn(ar, "failed to start monitor (cac): %d\n", ret);
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200808 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
809 return ret;
810 }
811
Michal Kazior7aa7a722014-08-25 12:09:38 +0200812 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac start monitor vdev %d\n",
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200813 ar->monitor_vdev_id);
814
815 return 0;
816}
817
818static int ath10k_stop_cac(struct ath10k *ar)
819{
820 lockdep_assert_held(&ar->conf_mutex);
821
822 /* CAC is not running - do nothing */
823 if (!test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags))
824 return 0;
825
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200826 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
Michal Kazior1bbc0972014-04-08 09:45:47 +0300827 ath10k_monitor_stop(ar);
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200828
Michal Kazior7aa7a722014-08-25 12:09:38 +0200829 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac finished\n");
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200830
831 return 0;
832}
833
Michal Kaziord6500972014-04-08 09:56:09 +0300834static void ath10k_recalc_radar_detection(struct ath10k *ar)
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200835{
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200836 int ret;
837
838 lockdep_assert_held(&ar->conf_mutex);
839
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200840 ath10k_stop_cac(ar);
841
Michal Kaziord6500972014-04-08 09:56:09 +0300842 if (!ar->radar_enabled)
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200843 return;
844
Michal Kaziord6500972014-04-08 09:56:09 +0300845 if (ar->num_started_vdevs > 0)
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200846 return;
847
848 ret = ath10k_start_cac(ar);
849 if (ret) {
850 /*
851 * Not possible to start CAC on current channel so starting
852 * radiation is not allowed, make this channel DFS_UNAVAILABLE
853 * by indicating that radar was detected.
854 */
Michal Kazior7aa7a722014-08-25 12:09:38 +0200855 ath10k_warn(ar, "failed to start CAC: %d\n", ret);
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200856 ieee80211_radar_detected(ar->hw);
857 }
858}
859
Michal Kaziordc55e302014-07-29 12:53:36 +0300860static int ath10k_vdev_start_restart(struct ath10k_vif *arvif, bool restart)
Michal Kazior72654fa2014-04-08 09:56:09 +0300861{
862 struct ath10k *ar = arvif->ar;
863 struct cfg80211_chan_def *chandef = &ar->chandef;
864 struct wmi_vdev_start_request_arg arg = {};
865 int ret = 0;
866
867 lockdep_assert_held(&ar->conf_mutex);
868
869 reinit_completion(&ar->vdev_setup_done);
870
871 arg.vdev_id = arvif->vdev_id;
872 arg.dtim_period = arvif->dtim_period;
873 arg.bcn_intval = arvif->beacon_interval;
874
875 arg.channel.freq = chandef->chan->center_freq;
876 arg.channel.band_center_freq1 = chandef->center_freq1;
877 arg.channel.mode = chan_to_phymode(chandef);
878
879 arg.channel.min_power = 0;
880 arg.channel.max_power = chandef->chan->max_power * 2;
881 arg.channel.max_reg_power = chandef->chan->max_reg_power * 2;
882 arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
883
884 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
885 arg.ssid = arvif->u.ap.ssid;
886 arg.ssid_len = arvif->u.ap.ssid_len;
887 arg.hidden_ssid = arvif->u.ap.hidden_ssid;
888
889 /* For now allow DFS for AP mode */
890 arg.channel.chan_radar =
891 !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
892 } else if (arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
893 arg.ssid = arvif->vif->bss_conf.ssid;
894 arg.ssid_len = arvif->vif->bss_conf.ssid_len;
895 }
896
Michal Kazior7aa7a722014-08-25 12:09:38 +0200897 ath10k_dbg(ar, ATH10K_DBG_MAC,
Michal Kazior72654fa2014-04-08 09:56:09 +0300898 "mac vdev %d start center_freq %d phymode %s\n",
899 arg.vdev_id, arg.channel.freq,
900 ath10k_wmi_phymode_str(arg.channel.mode));
901
Michal Kaziordc55e302014-07-29 12:53:36 +0300902 if (restart)
903 ret = ath10k_wmi_vdev_restart(ar, &arg);
904 else
905 ret = ath10k_wmi_vdev_start(ar, &arg);
906
Michal Kazior72654fa2014-04-08 09:56:09 +0300907 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200908 ath10k_warn(ar, "failed to start WMI vdev %i: %d\n",
Michal Kazior72654fa2014-04-08 09:56:09 +0300909 arg.vdev_id, ret);
910 return ret;
911 }
912
913 ret = ath10k_vdev_setup_sync(ar);
914 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200915 ath10k_warn(ar, "failed to synchronise setup for vdev %i: %d\n",
Michal Kazior72654fa2014-04-08 09:56:09 +0300916 arg.vdev_id, ret);
917 return ret;
918 }
919
Michal Kaziord6500972014-04-08 09:56:09 +0300920 ar->num_started_vdevs++;
921 ath10k_recalc_radar_detection(ar);
922
Michal Kazior72654fa2014-04-08 09:56:09 +0300923 return ret;
924}
925
Michal Kaziordc55e302014-07-29 12:53:36 +0300926static int ath10k_vdev_start(struct ath10k_vif *arvif)
927{
928 return ath10k_vdev_start_restart(arvif, false);
929}
930
931static int ath10k_vdev_restart(struct ath10k_vif *arvif)
932{
933 return ath10k_vdev_start_restart(arvif, true);
934}
935
Michal Kazior72654fa2014-04-08 09:56:09 +0300936static int ath10k_vdev_stop(struct ath10k_vif *arvif)
937{
938 struct ath10k *ar = arvif->ar;
939 int ret;
940
941 lockdep_assert_held(&ar->conf_mutex);
942
943 reinit_completion(&ar->vdev_setup_done);
944
945 ret = ath10k_wmi_vdev_stop(ar, arvif->vdev_id);
946 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200947 ath10k_warn(ar, "failed to stop WMI vdev %i: %d\n",
Michal Kazior72654fa2014-04-08 09:56:09 +0300948 arvif->vdev_id, ret);
949 return ret;
950 }
951
952 ret = ath10k_vdev_setup_sync(ar);
953 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200954 ath10k_warn(ar, "failed to syncronise setup for vdev %i: %d\n",
Michal Kazior72654fa2014-04-08 09:56:09 +0300955 arvif->vdev_id, ret);
956 return ret;
957 }
958
Michal Kaziord6500972014-04-08 09:56:09 +0300959 WARN_ON(ar->num_started_vdevs == 0);
960
961 if (ar->num_started_vdevs != 0) {
962 ar->num_started_vdevs--;
963 ath10k_recalc_radar_detection(ar);
964 }
965
Michal Kazior72654fa2014-04-08 09:56:09 +0300966 return ret;
967}
968
Kalle Valo5e3dd152013-06-12 20:52:10 +0300969static void ath10k_control_beaconing(struct ath10k_vif *arvif,
Kalle Valo5b07e072014-09-14 12:50:06 +0300970 struct ieee80211_bss_conf *info)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300971{
Michal Kazior7aa7a722014-08-25 12:09:38 +0200972 struct ath10k *ar = arvif->ar;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300973 int ret = 0;
974
Michal Kazior548db542013-07-05 16:15:15 +0300975 lockdep_assert_held(&arvif->ar->conf_mutex);
976
Kalle Valo5e3dd152013-06-12 20:52:10 +0300977 if (!info->enable_beacon) {
978 ath10k_vdev_stop(arvif);
Michal Kaziorc930f742014-01-23 11:38:25 +0100979
980 arvif->is_started = false;
981 arvif->is_up = false;
982
Michal Kazior748afc42014-01-23 12:48:21 +0100983 spin_lock_bh(&arvif->ar->data_lock);
Michal Kazior64badcb2014-09-18 11:18:02 +0300984 ath10k_mac_vif_beacon_free(arvif);
Michal Kazior748afc42014-01-23 12:48:21 +0100985 spin_unlock_bh(&arvif->ar->data_lock);
986
Kalle Valo5e3dd152013-06-12 20:52:10 +0300987 return;
988 }
989
990 arvif->tx_seq_no = 0x1000;
991
992 ret = ath10k_vdev_start(arvif);
993 if (ret)
994 return;
995
Michal Kaziorc930f742014-01-23 11:38:25 +0100996 arvif->aid = 0;
Kalle Valob25f32c2014-09-14 12:50:49 +0300997 ether_addr_copy(arvif->bssid, info->bssid);
Michal Kaziorc930f742014-01-23 11:38:25 +0100998
999 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
1000 arvif->bssid);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001001 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001002 ath10k_warn(ar, "failed to bring up vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02001003 arvif->vdev_id, ret);
Michal Kaziorc930f742014-01-23 11:38:25 +01001004 ath10k_vdev_stop(arvif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001005 return;
1006 }
Michal Kaziorc930f742014-01-23 11:38:25 +01001007
1008 arvif->is_started = true;
1009 arvif->is_up = true;
1010
Michal Kazior7aa7a722014-08-25 12:09:38 +02001011 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001012}
1013
1014static void ath10k_control_ibss(struct ath10k_vif *arvif,
1015 struct ieee80211_bss_conf *info,
1016 const u8 self_peer[ETH_ALEN])
1017{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001018 struct ath10k *ar = arvif->ar;
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02001019 u32 vdev_param;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001020 int ret = 0;
1021
Michal Kazior548db542013-07-05 16:15:15 +03001022 lockdep_assert_held(&arvif->ar->conf_mutex);
1023
Kalle Valo5e3dd152013-06-12 20:52:10 +03001024 if (!info->ibss_joined) {
1025 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, self_peer);
1026 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001027 ath10k_warn(ar, "failed to delete IBSS self peer %pM for vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001028 self_peer, arvif->vdev_id, ret);
1029
Michal Kaziorc930f742014-01-23 11:38:25 +01001030 if (is_zero_ether_addr(arvif->bssid))
Kalle Valo5e3dd152013-06-12 20:52:10 +03001031 return;
1032
Michal Kaziorc930f742014-01-23 11:38:25 +01001033 memset(arvif->bssid, 0, ETH_ALEN);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001034
1035 return;
1036 }
1037
1038 ret = ath10k_peer_create(arvif->ar, arvif->vdev_id, self_peer);
1039 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001040 ath10k_warn(ar, "failed to create IBSS self peer %pM for vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001041 self_peer, arvif->vdev_id, ret);
1042 return;
1043 }
1044
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02001045 vdev_param = arvif->ar->wmi.vdev_param->atim_window;
1046 ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001047 ATH10K_DEFAULT_ATIM);
1048 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001049 ath10k_warn(ar, "failed to set IBSS ATIM for vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001050 arvif->vdev_id, ret);
1051}
1052
1053/*
1054 * Review this when mac80211 gains per-interface powersave support.
1055 */
Michal Kaziorad088bf2013-10-16 15:44:46 +03001056static int ath10k_mac_vif_setup_ps(struct ath10k_vif *arvif)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001057{
Michal Kaziorad088bf2013-10-16 15:44:46 +03001058 struct ath10k *ar = arvif->ar;
1059 struct ieee80211_conf *conf = &ar->hw->conf;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001060 enum wmi_sta_powersave_param param;
1061 enum wmi_sta_ps_mode psmode;
1062 int ret;
1063
Michal Kazior548db542013-07-05 16:15:15 +03001064 lockdep_assert_held(&arvif->ar->conf_mutex);
1065
Michal Kaziorad088bf2013-10-16 15:44:46 +03001066 if (arvif->vif->type != NL80211_IFTYPE_STATION)
1067 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001068
1069 if (conf->flags & IEEE80211_CONF_PS) {
1070 psmode = WMI_STA_PS_MODE_ENABLED;
1071 param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
1072
Michal Kaziorad088bf2013-10-16 15:44:46 +03001073 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001074 conf->dynamic_ps_timeout);
1075 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001076 ath10k_warn(ar, "failed to set inactivity time for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02001077 arvif->vdev_id, ret);
Michal Kaziorad088bf2013-10-16 15:44:46 +03001078 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001079 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001080 } else {
1081 psmode = WMI_STA_PS_MODE_DISABLED;
1082 }
1083
Michal Kazior7aa7a722014-08-25 12:09:38 +02001084 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d psmode %s\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03001085 arvif->vdev_id, psmode ? "enable" : "disable");
1086
Michal Kaziorad088bf2013-10-16 15:44:46 +03001087 ret = ath10k_wmi_set_psmode(ar, arvif->vdev_id, psmode);
1088 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001089 ath10k_warn(ar, "failed to set PS Mode %d for vdev %d: %d\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02001090 psmode, arvif->vdev_id, ret);
Michal Kaziorad088bf2013-10-16 15:44:46 +03001091 return ret;
1092 }
1093
1094 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001095}
1096
1097/**********************/
1098/* Station management */
1099/**********************/
1100
Michal Kazior590922a2014-10-21 10:10:29 +03001101static u32 ath10k_peer_assoc_h_listen_intval(struct ath10k *ar,
1102 struct ieee80211_vif *vif)
1103{
1104 /* Some firmware revisions have unstable STA powersave when listen
1105 * interval is set too high (e.g. 5). The symptoms are firmware doesn't
1106 * generate NullFunc frames properly even if buffered frames have been
1107 * indicated in Beacon TIM. Firmware would seldom wake up to pull
1108 * buffered frames. Often pinging the device from AP would simply fail.
1109 *
1110 * As a workaround set it to 1.
1111 */
1112 if (vif->type == NL80211_IFTYPE_STATION)
1113 return 1;
1114
1115 return ar->hw->conf.listen_interval;
1116}
1117
Kalle Valo5e3dd152013-06-12 20:52:10 +03001118static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001119 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001120 struct ieee80211_sta *sta,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001121 struct wmi_peer_assoc_complete_arg *arg)
1122{
Michal Kazior590922a2014-10-21 10:10:29 +03001123 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1124
Michal Kazior548db542013-07-05 16:15:15 +03001125 lockdep_assert_held(&ar->conf_mutex);
1126
Kalle Valob25f32c2014-09-14 12:50:49 +03001127 ether_addr_copy(arg->addr, sta->addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001128 arg->vdev_id = arvif->vdev_id;
1129 arg->peer_aid = sta->aid;
1130 arg->peer_flags |= WMI_PEER_AUTH;
Michal Kazior590922a2014-10-21 10:10:29 +03001131 arg->peer_listen_intval = ath10k_peer_assoc_h_listen_intval(ar, vif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001132 arg->peer_num_spatial_streams = 1;
Michal Kazior590922a2014-10-21 10:10:29 +03001133 arg->peer_caps = vif->bss_conf.assoc_capability;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001134}
1135
1136static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001137 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001138 struct wmi_peer_assoc_complete_arg *arg)
1139{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001140 struct ieee80211_bss_conf *info = &vif->bss_conf;
1141 struct cfg80211_bss *bss;
1142 const u8 *rsnie = NULL;
1143 const u8 *wpaie = NULL;
1144
Michal Kazior548db542013-07-05 16:15:15 +03001145 lockdep_assert_held(&ar->conf_mutex);
1146
Kalle Valo5e3dd152013-06-12 20:52:10 +03001147 bss = cfg80211_get_bss(ar->hw->wiphy, ar->hw->conf.chandef.chan,
1148 info->bssid, NULL, 0, 0, 0);
1149 if (bss) {
1150 const struct cfg80211_bss_ies *ies;
1151
1152 rcu_read_lock();
1153 rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
1154
1155 ies = rcu_dereference(bss->ies);
1156
1157 wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
Kalle Valo5b07e072014-09-14 12:50:06 +03001158 WLAN_OUI_TYPE_MICROSOFT_WPA,
1159 ies->data,
1160 ies->len);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001161 rcu_read_unlock();
1162 cfg80211_put_bss(ar->hw->wiphy, bss);
1163 }
1164
1165 /* FIXME: base on RSN IE/WPA IE is a correct idea? */
1166 if (rsnie || wpaie) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001167 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001168 arg->peer_flags |= WMI_PEER_NEED_PTK_4_WAY;
1169 }
1170
1171 if (wpaie) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001172 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: wpa ie found\n", __func__);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001173 arg->peer_flags |= WMI_PEER_NEED_GTK_2_WAY;
1174 }
1175}
1176
1177static void ath10k_peer_assoc_h_rates(struct ath10k *ar,
1178 struct ieee80211_sta *sta,
1179 struct wmi_peer_assoc_complete_arg *arg)
1180{
1181 struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
1182 const struct ieee80211_supported_band *sband;
1183 const struct ieee80211_rate *rates;
1184 u32 ratemask;
1185 int i;
1186
Michal Kazior548db542013-07-05 16:15:15 +03001187 lockdep_assert_held(&ar->conf_mutex);
1188
Kalle Valo5e3dd152013-06-12 20:52:10 +03001189 sband = ar->hw->wiphy->bands[ar->hw->conf.chandef.chan->band];
1190 ratemask = sta->supp_rates[ar->hw->conf.chandef.chan->band];
1191 rates = sband->bitrates;
1192
1193 rateset->num_rates = 0;
1194
1195 for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
1196 if (!(ratemask & 1))
1197 continue;
1198
1199 rateset->rates[rateset->num_rates] = rates->hw_value;
1200 rateset->num_rates++;
1201 }
1202}
1203
1204static void ath10k_peer_assoc_h_ht(struct ath10k *ar,
1205 struct ieee80211_sta *sta,
1206 struct wmi_peer_assoc_complete_arg *arg)
1207{
1208 const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001209 int i, n;
Kalle Valoaf762c02014-09-14 12:50:17 +03001210 u32 stbc;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001211
Michal Kazior548db542013-07-05 16:15:15 +03001212 lockdep_assert_held(&ar->conf_mutex);
1213
Kalle Valo5e3dd152013-06-12 20:52:10 +03001214 if (!ht_cap->ht_supported)
1215 return;
1216
1217 arg->peer_flags |= WMI_PEER_HT;
1218 arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1219 ht_cap->ampdu_factor)) - 1;
1220
1221 arg->peer_mpdu_density =
1222 ath10k_parse_mpdudensity(ht_cap->ampdu_density);
1223
1224 arg->peer_ht_caps = ht_cap->cap;
1225 arg->peer_rate_caps |= WMI_RC_HT_FLAG;
1226
1227 if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
1228 arg->peer_flags |= WMI_PEER_LDPC;
1229
1230 if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
1231 arg->peer_flags |= WMI_PEER_40MHZ;
1232 arg->peer_rate_caps |= WMI_RC_CW40_FLAG;
1233 }
1234
1235 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
1236 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
1237
1238 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
1239 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
1240
1241 if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
1242 arg->peer_rate_caps |= WMI_RC_TX_STBC_FLAG;
1243 arg->peer_flags |= WMI_PEER_STBC;
1244 }
1245
1246 if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001247 stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
1248 stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
1249 stbc = stbc << WMI_RC_RX_STBC_FLAG_S;
1250 arg->peer_rate_caps |= stbc;
1251 arg->peer_flags |= WMI_PEER_STBC;
1252 }
1253
Kalle Valo5e3dd152013-06-12 20:52:10 +03001254 if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
1255 arg->peer_rate_caps |= WMI_RC_TS_FLAG;
1256 else if (ht_cap->mcs.rx_mask[1])
1257 arg->peer_rate_caps |= WMI_RC_DS_FLAG;
1258
1259 for (i = 0, n = 0; i < IEEE80211_HT_MCS_MASK_LEN*8; i++)
1260 if (ht_cap->mcs.rx_mask[i/8] & (1 << i%8))
1261 arg->peer_ht_rates.rates[n++] = i;
1262
Bartosz Markowskifd71f802014-02-10 13:12:55 +01001263 /*
1264 * This is a workaround for HT-enabled STAs which break the spec
1265 * and have no HT capabilities RX mask (no HT RX MCS map).
1266 *
1267 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
1268 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
1269 *
1270 * Firmware asserts if such situation occurs.
1271 */
1272 if (n == 0) {
1273 arg->peer_ht_rates.num_rates = 8;
1274 for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
1275 arg->peer_ht_rates.rates[i] = i;
1276 } else {
1277 arg->peer_ht_rates.num_rates = n;
1278 arg->peer_num_spatial_streams = sta->rx_nss;
1279 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001280
Michal Kazior7aa7a722014-08-25 12:09:38 +02001281 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03001282 arg->addr,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001283 arg->peer_ht_rates.num_rates,
1284 arg->peer_num_spatial_streams);
1285}
1286
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001287static int ath10k_peer_assoc_qos_ap(struct ath10k *ar,
1288 struct ath10k_vif *arvif,
1289 struct ieee80211_sta *sta)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001290{
1291 u32 uapsd = 0;
1292 u32 max_sp = 0;
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001293 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001294
Michal Kazior548db542013-07-05 16:15:15 +03001295 lockdep_assert_held(&ar->conf_mutex);
1296
Kalle Valo5e3dd152013-06-12 20:52:10 +03001297 if (sta->wme && sta->uapsd_queues) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001298 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001299 sta->uapsd_queues, sta->max_sp);
1300
Kalle Valo5e3dd152013-06-12 20:52:10 +03001301 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1302 uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
1303 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
1304 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1305 uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
1306 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
1307 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1308 uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
1309 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
1310 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1311 uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
1312 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
1313
Kalle Valo5e3dd152013-06-12 20:52:10 +03001314 if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
1315 max_sp = sta->max_sp;
1316
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001317 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1318 sta->addr,
1319 WMI_AP_PS_PEER_PARAM_UAPSD,
1320 uapsd);
1321 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001322 ath10k_warn(ar, "failed to set ap ps peer param uapsd for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001323 arvif->vdev_id, ret);
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001324 return ret;
1325 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001326
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001327 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1328 sta->addr,
1329 WMI_AP_PS_PEER_PARAM_MAX_SP,
1330 max_sp);
1331 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001332 ath10k_warn(ar, "failed to set ap ps peer param max sp for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001333 arvif->vdev_id, ret);
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001334 return ret;
1335 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001336
1337 /* TODO setup this based on STA listen interval and
1338 beacon interval. Currently we don't know
1339 sta->listen_interval - mac80211 patch required.
1340 Currently use 10 seconds */
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001341 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id, sta->addr,
Kalle Valo5b07e072014-09-14 12:50:06 +03001342 WMI_AP_PS_PEER_PARAM_AGEOUT_TIME,
1343 10);
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001344 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001345 ath10k_warn(ar, "failed to set ap ps peer param ageout time for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001346 arvif->vdev_id, ret);
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001347 return ret;
1348 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001349 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001350
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001351 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001352}
1353
1354static void ath10k_peer_assoc_h_vht(struct ath10k *ar,
1355 struct ieee80211_sta *sta,
1356 struct wmi_peer_assoc_complete_arg *arg)
1357{
1358 const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
Sujith Manoharana24b88b2013-10-07 19:51:57 -07001359 u8 ampdu_factor;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001360
1361 if (!vht_cap->vht_supported)
1362 return;
1363
1364 arg->peer_flags |= WMI_PEER_VHT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001365 arg->peer_vht_caps = vht_cap->cap;
1366
Sujith Manoharana24b88b2013-10-07 19:51:57 -07001367 ampdu_factor = (vht_cap->cap &
1368 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
1369 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
1370
1371 /* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
1372 * zero in VHT IE. Using it would result in degraded throughput.
1373 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
1374 * it if VHT max_mpdu is smaller. */
1375 arg->peer_max_mpdu = max(arg->peer_max_mpdu,
1376 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1377 ampdu_factor)) - 1);
1378
Kalle Valo5e3dd152013-06-12 20:52:10 +03001379 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1380 arg->peer_flags |= WMI_PEER_80MHZ;
1381
1382 arg->peer_vht_rates.rx_max_rate =
1383 __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
1384 arg->peer_vht_rates.rx_mcs_set =
1385 __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
1386 arg->peer_vht_rates.tx_max_rate =
1387 __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
1388 arg->peer_vht_rates.tx_mcs_set =
1389 __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
1390
Michal Kazior7aa7a722014-08-25 12:09:38 +02001391 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03001392 sta->addr, arg->peer_max_mpdu, arg->peer_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001393}
1394
1395static void ath10k_peer_assoc_h_qos(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001396 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001397 struct ieee80211_sta *sta,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001398 struct wmi_peer_assoc_complete_arg *arg)
1399{
Michal Kazior590922a2014-10-21 10:10:29 +03001400 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1401
Kalle Valo5e3dd152013-06-12 20:52:10 +03001402 switch (arvif->vdev_type) {
1403 case WMI_VDEV_TYPE_AP:
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001404 if (sta->wme)
1405 arg->peer_flags |= WMI_PEER_QOS;
1406
1407 if (sta->wme && sta->uapsd_queues) {
1408 arg->peer_flags |= WMI_PEER_APSD;
1409 arg->peer_rate_caps |= WMI_RC_UAPSD_FLAG;
1410 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001411 break;
1412 case WMI_VDEV_TYPE_STA:
Michal Kazior590922a2014-10-21 10:10:29 +03001413 if (vif->bss_conf.qos)
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001414 arg->peer_flags |= WMI_PEER_QOS;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001415 break;
1416 default:
1417 break;
1418 }
1419}
1420
1421static void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001422 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001423 struct ieee80211_sta *sta,
1424 struct wmi_peer_assoc_complete_arg *arg)
1425{
1426 enum wmi_phy_mode phymode = MODE_UNKNOWN;
1427
Kalle Valo5e3dd152013-06-12 20:52:10 +03001428 switch (ar->hw->conf.chandef.chan->band) {
1429 case IEEE80211_BAND_2GHZ:
1430 if (sta->ht_cap.ht_supported) {
1431 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1432 phymode = MODE_11NG_HT40;
1433 else
1434 phymode = MODE_11NG_HT20;
1435 } else {
1436 phymode = MODE_11G;
1437 }
1438
1439 break;
1440 case IEEE80211_BAND_5GHZ:
Sujith Manoharan7cc45e92013-09-08 18:19:55 +03001441 /*
1442 * Check VHT first.
1443 */
1444 if (sta->vht_cap.vht_supported) {
1445 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1446 phymode = MODE_11AC_VHT80;
1447 else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1448 phymode = MODE_11AC_VHT40;
1449 else if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
1450 phymode = MODE_11AC_VHT20;
1451 } else if (sta->ht_cap.ht_supported) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001452 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1453 phymode = MODE_11NA_HT40;
1454 else
1455 phymode = MODE_11NA_HT20;
1456 } else {
1457 phymode = MODE_11A;
1458 }
1459
1460 break;
1461 default:
1462 break;
1463 }
1464
Michal Kazior7aa7a722014-08-25 12:09:38 +02001465 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM phymode %s\n",
Kalle Valo38a1d472013-09-08 17:56:14 +03001466 sta->addr, ath10k_wmi_phymode_str(phymode));
Kalle Valo60c3daa2013-09-08 17:56:07 +03001467
Kalle Valo5e3dd152013-06-12 20:52:10 +03001468 arg->peer_phymode = phymode;
1469 WARN_ON(phymode == MODE_UNKNOWN);
1470}
1471
Kalle Valob9ada652013-10-16 15:44:46 +03001472static int ath10k_peer_assoc_prepare(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001473 struct ieee80211_vif *vif,
Kalle Valob9ada652013-10-16 15:44:46 +03001474 struct ieee80211_sta *sta,
Kalle Valob9ada652013-10-16 15:44:46 +03001475 struct wmi_peer_assoc_complete_arg *arg)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001476{
Michal Kazior548db542013-07-05 16:15:15 +03001477 lockdep_assert_held(&ar->conf_mutex);
1478
Kalle Valob9ada652013-10-16 15:44:46 +03001479 memset(arg, 0, sizeof(*arg));
Kalle Valo5e3dd152013-06-12 20:52:10 +03001480
Michal Kazior590922a2014-10-21 10:10:29 +03001481 ath10k_peer_assoc_h_basic(ar, vif, sta, arg);
1482 ath10k_peer_assoc_h_crypto(ar, vif, arg);
Kalle Valob9ada652013-10-16 15:44:46 +03001483 ath10k_peer_assoc_h_rates(ar, sta, arg);
1484 ath10k_peer_assoc_h_ht(ar, sta, arg);
1485 ath10k_peer_assoc_h_vht(ar, sta, arg);
Michal Kazior590922a2014-10-21 10:10:29 +03001486 ath10k_peer_assoc_h_qos(ar, vif, sta, arg);
1487 ath10k_peer_assoc_h_phymode(ar, vif, sta, arg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001488
Kalle Valob9ada652013-10-16 15:44:46 +03001489 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001490}
1491
Michal Kazior90046f52014-02-14 14:45:51 +01001492static const u32 ath10k_smps_map[] = {
1493 [WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
1494 [WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
1495 [WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
1496 [WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
1497};
1498
1499static int ath10k_setup_peer_smps(struct ath10k *ar, struct ath10k_vif *arvif,
1500 const u8 *addr,
1501 const struct ieee80211_sta_ht_cap *ht_cap)
1502{
1503 int smps;
1504
1505 if (!ht_cap->ht_supported)
1506 return 0;
1507
1508 smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
1509 smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
1510
1511 if (smps >= ARRAY_SIZE(ath10k_smps_map))
1512 return -EINVAL;
1513
1514 return ath10k_wmi_peer_set_param(ar, arvif->vdev_id, addr,
1515 WMI_PEER_SMPS_STATE,
1516 ath10k_smps_map[smps]);
1517}
1518
Kalle Valo5e3dd152013-06-12 20:52:10 +03001519/* can be called only in mac80211 callbacks due to `key_count` usage */
1520static void ath10k_bss_assoc(struct ieee80211_hw *hw,
1521 struct ieee80211_vif *vif,
1522 struct ieee80211_bss_conf *bss_conf)
1523{
1524 struct ath10k *ar = hw->priv;
1525 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Michal Kazior90046f52014-02-14 14:45:51 +01001526 struct ieee80211_sta_ht_cap ht_cap;
Kalle Valob9ada652013-10-16 15:44:46 +03001527 struct wmi_peer_assoc_complete_arg peer_arg;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001528 struct ieee80211_sta *ap_sta;
1529 int ret;
1530
Michal Kazior548db542013-07-05 16:15:15 +03001531 lockdep_assert_held(&ar->conf_mutex);
1532
Michal Kazior077efc82014-10-21 10:10:29 +03001533 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
1534 arvif->vdev_id, arvif->bssid, arvif->aid);
1535
Kalle Valo5e3dd152013-06-12 20:52:10 +03001536 rcu_read_lock();
1537
1538 ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
1539 if (!ap_sta) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001540 ath10k_warn(ar, "failed to find station entry for bss %pM vdev %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02001541 bss_conf->bssid, arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001542 rcu_read_unlock();
1543 return;
1544 }
1545
Michal Kazior90046f52014-02-14 14:45:51 +01001546 /* ap_sta must be accessed only within rcu section which must be left
1547 * before calling ath10k_setup_peer_smps() which might sleep. */
1548 ht_cap = ap_sta->ht_cap;
1549
Michal Kazior590922a2014-10-21 10:10:29 +03001550 ret = ath10k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001551 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001552 ath10k_warn(ar, "failed to prepare peer assoc for %pM vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001553 bss_conf->bssid, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001554 rcu_read_unlock();
1555 return;
1556 }
1557
1558 rcu_read_unlock();
1559
Kalle Valob9ada652013-10-16 15:44:46 +03001560 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
1561 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001562 ath10k_warn(ar, "failed to run peer assoc for %pM vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001563 bss_conf->bssid, arvif->vdev_id, ret);
Kalle Valob9ada652013-10-16 15:44:46 +03001564 return;
1565 }
1566
Michal Kazior90046f52014-02-14 14:45:51 +01001567 ret = ath10k_setup_peer_smps(ar, arvif, bss_conf->bssid, &ht_cap);
1568 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001569 ath10k_warn(ar, "failed to setup peer SMPS for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001570 arvif->vdev_id, ret);
Michal Kazior90046f52014-02-14 14:45:51 +01001571 return;
1572 }
1573
Michal Kazior7aa7a722014-08-25 12:09:38 +02001574 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03001575 "mac vdev %d up (associated) bssid %pM aid %d\n",
1576 arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
1577
Michal Kazior077efc82014-10-21 10:10:29 +03001578 WARN_ON(arvif->is_up);
1579
Michal Kaziorc930f742014-01-23 11:38:25 +01001580 arvif->aid = bss_conf->aid;
Kalle Valob25f32c2014-09-14 12:50:49 +03001581 ether_addr_copy(arvif->bssid, bss_conf->bssid);
Michal Kaziorc930f742014-01-23 11:38:25 +01001582
1583 ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
1584 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001585 ath10k_warn(ar, "failed to set vdev %d up: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001586 arvif->vdev_id, ret);
Michal Kaziorc930f742014-01-23 11:38:25 +01001587 return;
1588 }
1589
1590 arvif->is_up = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001591}
1592
Kalle Valo5e3dd152013-06-12 20:52:10 +03001593static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
1594 struct ieee80211_vif *vif)
1595{
1596 struct ath10k *ar = hw->priv;
1597 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1598 int ret;
1599
Michal Kazior548db542013-07-05 16:15:15 +03001600 lockdep_assert_held(&ar->conf_mutex);
1601
Michal Kazior077efc82014-10-21 10:10:29 +03001602 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
1603 arvif->vdev_id, arvif->bssid);
Kalle Valo60c3daa2013-09-08 17:56:07 +03001604
Kalle Valo5e3dd152013-06-12 20:52:10 +03001605 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
Michal Kazior077efc82014-10-21 10:10:29 +03001606 if (ret)
1607 ath10k_warn(ar, "faield to down vdev %i: %d\n",
1608 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001609
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001610 arvif->def_wep_key_idx = 0;
Michal Kaziorc930f742014-01-23 11:38:25 +01001611 arvif->is_up = false;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001612}
1613
Michal Kazior590922a2014-10-21 10:10:29 +03001614static int ath10k_station_assoc(struct ath10k *ar,
1615 struct ieee80211_vif *vif,
1616 struct ieee80211_sta *sta,
1617 bool reassoc)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001618{
Michal Kazior590922a2014-10-21 10:10:29 +03001619 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Kalle Valob9ada652013-10-16 15:44:46 +03001620 struct wmi_peer_assoc_complete_arg peer_arg;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001621 int ret = 0;
1622
Michal Kazior548db542013-07-05 16:15:15 +03001623 lockdep_assert_held(&ar->conf_mutex);
1624
Michal Kazior590922a2014-10-21 10:10:29 +03001625 ret = ath10k_peer_assoc_prepare(ar, vif, sta, &peer_arg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001626 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001627 ath10k_warn(ar, "failed to prepare WMI peer assoc for %pM vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02001628 sta->addr, arvif->vdev_id, ret);
Kalle Valob9ada652013-10-16 15:44:46 +03001629 return ret;
1630 }
1631
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02001632 peer_arg.peer_reassoc = reassoc;
Kalle Valob9ada652013-10-16 15:44:46 +03001633 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
1634 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001635 ath10k_warn(ar, "failed to run peer assoc for STA %pM vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001636 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001637 return ret;
1638 }
1639
Michal Kaziorb1ecde32014-10-21 10:10:29 +03001640 /* Re-assoc is run only to update supported rates for given station. It
1641 * doesn't make much sense to reconfigure the peer completely.
1642 */
1643 if (!reassoc) {
1644 ret = ath10k_setup_peer_smps(ar, arvif, sta->addr,
1645 &sta->ht_cap);
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02001646 if (ret) {
Michal Kaziorb1ecde32014-10-21 10:10:29 +03001647 ath10k_warn(ar, "failed to setup peer SMPS for vdev %d: %d\n",
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02001648 arvif->vdev_id, ret);
1649 return ret;
1650 }
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02001651
Michal Kaziorb1ecde32014-10-21 10:10:29 +03001652 ret = ath10k_peer_assoc_qos_ap(ar, arvif, sta);
1653 if (ret) {
1654 ath10k_warn(ar, "failed to set qos params for STA %pM for vdev %i: %d\n",
1655 sta->addr, arvif->vdev_id, ret);
1656 return ret;
1657 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001658
Michal Kaziorb1ecde32014-10-21 10:10:29 +03001659 if (!sta->wme) {
1660 arvif->num_legacy_stations++;
1661 ret = ath10k_recalc_rtscts_prot(arvif);
1662 if (ret) {
1663 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
1664 arvif->vdev_id, ret);
1665 return ret;
1666 }
1667 }
1668
1669 ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
1670 if (ret) {
1671 ath10k_warn(ar, "failed to install peer wep keys for vdev %i: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001672 arvif->vdev_id, ret);
1673 return ret;
1674 }
1675 }
1676
Kalle Valo5e3dd152013-06-12 20:52:10 +03001677 return ret;
1678}
1679
Michal Kazior590922a2014-10-21 10:10:29 +03001680static int ath10k_station_disassoc(struct ath10k *ar,
1681 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001682 struct ieee80211_sta *sta)
1683{
Michal Kazior590922a2014-10-21 10:10:29 +03001684 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001685 int ret = 0;
1686
1687 lockdep_assert_held(&ar->conf_mutex);
1688
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02001689 if (!sta->wme) {
1690 arvif->num_legacy_stations--;
1691 ret = ath10k_recalc_rtscts_prot(arvif);
1692 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001693 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02001694 arvif->vdev_id, ret);
1695 return ret;
1696 }
1697 }
1698
Kalle Valo5e3dd152013-06-12 20:52:10 +03001699 ret = ath10k_clear_peer_keys(arvif, sta->addr);
1700 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001701 ath10k_warn(ar, "failed to clear all peer wep keys for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001702 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001703 return ret;
1704 }
1705
1706 return ret;
1707}
1708
1709/**************/
1710/* Regulatory */
1711/**************/
1712
1713static int ath10k_update_channel_list(struct ath10k *ar)
1714{
1715 struct ieee80211_hw *hw = ar->hw;
1716 struct ieee80211_supported_band **bands;
1717 enum ieee80211_band band;
1718 struct ieee80211_channel *channel;
1719 struct wmi_scan_chan_list_arg arg = {0};
1720 struct wmi_channel_arg *ch;
1721 bool passive;
1722 int len;
1723 int ret;
1724 int i;
1725
Michal Kazior548db542013-07-05 16:15:15 +03001726 lockdep_assert_held(&ar->conf_mutex);
1727
Kalle Valo5e3dd152013-06-12 20:52:10 +03001728 bands = hw->wiphy->bands;
1729 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1730 if (!bands[band])
1731 continue;
1732
1733 for (i = 0; i < bands[band]->n_channels; i++) {
1734 if (bands[band]->channels[i].flags &
1735 IEEE80211_CHAN_DISABLED)
1736 continue;
1737
1738 arg.n_channels++;
1739 }
1740 }
1741
1742 len = sizeof(struct wmi_channel_arg) * arg.n_channels;
1743 arg.channels = kzalloc(len, GFP_KERNEL);
1744 if (!arg.channels)
1745 return -ENOMEM;
1746
1747 ch = arg.channels;
1748 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1749 if (!bands[band])
1750 continue;
1751
1752 for (i = 0; i < bands[band]->n_channels; i++) {
1753 channel = &bands[band]->channels[i];
1754
1755 if (channel->flags & IEEE80211_CHAN_DISABLED)
1756 continue;
1757
1758 ch->allow_ht = true;
1759
1760 /* FIXME: when should we really allow VHT? */
1761 ch->allow_vht = true;
1762
1763 ch->allow_ibss =
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +02001764 !(channel->flags & IEEE80211_CHAN_NO_IR);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001765
1766 ch->ht40plus =
1767 !(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
1768
Marek Puzyniake8a50f82013-11-20 09:59:47 +02001769 ch->chan_radar =
1770 !!(channel->flags & IEEE80211_CHAN_RADAR);
1771
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +02001772 passive = channel->flags & IEEE80211_CHAN_NO_IR;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001773 ch->passive = passive;
1774
1775 ch->freq = channel->center_freq;
Michal Kazior2d667212014-09-18 15:21:21 +02001776 ch->band_center_freq1 = channel->center_freq;
Michal Kazior89c5c842013-10-23 04:02:13 -07001777 ch->min_power = 0;
Michal Kazior02256932013-10-23 04:02:14 -07001778 ch->max_power = channel->max_power * 2;
1779 ch->max_reg_power = channel->max_reg_power * 2;
1780 ch->max_antenna_gain = channel->max_antenna_gain * 2;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001781 ch->reg_class_id = 0; /* FIXME */
1782
1783 /* FIXME: why use only legacy modes, why not any
1784 * HT/VHT modes? Would that even make any
1785 * difference? */
1786 if (channel->band == IEEE80211_BAND_2GHZ)
1787 ch->mode = MODE_11G;
1788 else
1789 ch->mode = MODE_11A;
1790
1791 if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
1792 continue;
1793
Michal Kazior7aa7a722014-08-25 12:09:38 +02001794 ath10k_dbg(ar, ATH10K_DBG_WMI,
Kalle Valo60c3daa2013-09-08 17:56:07 +03001795 "mac channel [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
1796 ch - arg.channels, arg.n_channels,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001797 ch->freq, ch->max_power, ch->max_reg_power,
1798 ch->max_antenna_gain, ch->mode);
1799
1800 ch++;
1801 }
1802 }
1803
1804 ret = ath10k_wmi_scan_chan_list(ar, &arg);
1805 kfree(arg.channels);
1806
1807 return ret;
1808}
1809
Marek Puzyniak821af6a2014-03-21 17:46:57 +02001810static enum wmi_dfs_region
1811ath10k_mac_get_dfs_region(enum nl80211_dfs_regions dfs_region)
1812{
1813 switch (dfs_region) {
1814 case NL80211_DFS_UNSET:
1815 return WMI_UNINIT_DFS_DOMAIN;
1816 case NL80211_DFS_FCC:
1817 return WMI_FCC_DFS_DOMAIN;
1818 case NL80211_DFS_ETSI:
1819 return WMI_ETSI_DFS_DOMAIN;
1820 case NL80211_DFS_JP:
1821 return WMI_MKK4_DFS_DOMAIN;
1822 }
1823 return WMI_UNINIT_DFS_DOMAIN;
1824}
1825
Michal Kaziorf7843d72013-07-16 09:38:52 +02001826static void ath10k_regd_update(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001827{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001828 struct reg_dmn_pair_mapping *regpair;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001829 int ret;
Marek Puzyniak821af6a2014-03-21 17:46:57 +02001830 enum wmi_dfs_region wmi_dfs_reg;
1831 enum nl80211_dfs_regions nl_dfs_reg;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001832
Michal Kaziorf7843d72013-07-16 09:38:52 +02001833 lockdep_assert_held(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001834
1835 ret = ath10k_update_channel_list(ar);
1836 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001837 ath10k_warn(ar, "failed to update channel list: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001838
1839 regpair = ar->ath_common.regulatory.regpair;
Michal Kaziorf7843d72013-07-16 09:38:52 +02001840
Marek Puzyniak821af6a2014-03-21 17:46:57 +02001841 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
1842 nl_dfs_reg = ar->dfs_detector->region;
1843 wmi_dfs_reg = ath10k_mac_get_dfs_region(nl_dfs_reg);
1844 } else {
1845 wmi_dfs_reg = WMI_UNINIT_DFS_DOMAIN;
1846 }
1847
Kalle Valo5e3dd152013-06-12 20:52:10 +03001848 /* Target allows setting up per-band regdomain but ath_common provides
1849 * a combined one only */
1850 ret = ath10k_wmi_pdev_set_regdomain(ar,
Kalle Valoef8c0012014-02-13 18:13:12 +02001851 regpair->reg_domain,
1852 regpair->reg_domain, /* 2ghz */
1853 regpair->reg_domain, /* 5ghz */
Kalle Valo5e3dd152013-06-12 20:52:10 +03001854 regpair->reg_2ghz_ctl,
Marek Puzyniak821af6a2014-03-21 17:46:57 +02001855 regpair->reg_5ghz_ctl,
1856 wmi_dfs_reg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001857 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001858 ath10k_warn(ar, "failed to set pdev regdomain: %d\n", ret);
Michal Kaziorf7843d72013-07-16 09:38:52 +02001859}
Michal Kazior548db542013-07-05 16:15:15 +03001860
Michal Kaziorf7843d72013-07-16 09:38:52 +02001861static void ath10k_reg_notifier(struct wiphy *wiphy,
1862 struct regulatory_request *request)
1863{
1864 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
1865 struct ath10k *ar = hw->priv;
Janusz Dziedzic9702c682013-11-20 09:59:41 +02001866 bool result;
Michal Kaziorf7843d72013-07-16 09:38:52 +02001867
1868 ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
1869
Janusz Dziedzic9702c682013-11-20 09:59:41 +02001870 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001871 ath10k_dbg(ar, ATH10K_DBG_REGULATORY, "dfs region 0x%x\n",
Janusz Dziedzic9702c682013-11-20 09:59:41 +02001872 request->dfs_region);
1873 result = ar->dfs_detector->set_dfs_domain(ar->dfs_detector,
1874 request->dfs_region);
1875 if (!result)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001876 ath10k_warn(ar, "DFS region 0x%X not supported, will trigger radar for every pulse\n",
Janusz Dziedzic9702c682013-11-20 09:59:41 +02001877 request->dfs_region);
1878 }
1879
Michal Kaziorf7843d72013-07-16 09:38:52 +02001880 mutex_lock(&ar->conf_mutex);
1881 if (ar->state == ATH10K_STATE_ON)
1882 ath10k_regd_update(ar);
Michal Kazior548db542013-07-05 16:15:15 +03001883 mutex_unlock(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001884}
1885
1886/***************/
1887/* TX handlers */
1888/***************/
1889
Michal Kazior42c3aa62013-10-02 11:03:38 +02001890static u8 ath10k_tx_h_get_tid(struct ieee80211_hdr *hdr)
1891{
1892 if (ieee80211_is_mgmt(hdr->frame_control))
1893 return HTT_DATA_TX_EXT_TID_MGMT;
1894
1895 if (!ieee80211_is_data_qos(hdr->frame_control))
1896 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
1897
1898 if (!is_unicast_ether_addr(ieee80211_get_DA(hdr)))
1899 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
1900
1901 return ieee80211_get_qos_ctl(hdr)[0] & IEEE80211_QOS_CTL_TID_MASK;
1902}
1903
Michal Kazior2b37c292014-09-02 11:00:22 +03001904static u8 ath10k_tx_h_get_vdev_id(struct ath10k *ar, struct ieee80211_vif *vif)
Michal Kaziorddb6ad72013-10-02 11:03:39 +02001905{
Michal Kazior2b37c292014-09-02 11:00:22 +03001906 if (vif)
1907 return ath10k_vif_to_arvif(vif)->vdev_id;
Michal Kaziorddb6ad72013-10-02 11:03:39 +02001908
Michal Kazior1bbc0972014-04-08 09:45:47 +03001909 if (ar->monitor_started)
Michal Kaziorddb6ad72013-10-02 11:03:39 +02001910 return ar->monitor_vdev_id;
1911
Michal Kazior7aa7a722014-08-25 12:09:38 +02001912 ath10k_warn(ar, "failed to resolve vdev id\n");
Michal Kaziorddb6ad72013-10-02 11:03:39 +02001913 return 0;
1914}
1915
Michal Kazior4b604552014-07-21 21:03:09 +03001916/* HTT Tx uses Native Wifi tx mode which expects 802.11 frames without QoS
1917 * Control in the header.
Kalle Valo5e3dd152013-06-12 20:52:10 +03001918 */
Michal Kazior4b604552014-07-21 21:03:09 +03001919static void ath10k_tx_h_nwifi(struct ieee80211_hw *hw, struct sk_buff *skb)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001920{
1921 struct ieee80211_hdr *hdr = (void *)skb->data;
Michal Kaziorc21c64d2014-07-21 21:03:10 +03001922 struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001923 u8 *qos_ctl;
1924
1925 if (!ieee80211_is_data_qos(hdr->frame_control))
1926 return;
1927
1928 qos_ctl = ieee80211_get_qos_ctl(hdr);
Michal Kaziorba0ccd72013-07-22 14:25:28 +02001929 memmove(skb->data + IEEE80211_QOS_CTL_LEN,
1930 skb->data, (void *)qos_ctl - (void *)skb->data);
1931 skb_pull(skb, IEEE80211_QOS_CTL_LEN);
Michal Kaziorc21c64d2014-07-21 21:03:10 +03001932
1933 /* Fw/Hw generates a corrupted QoS Control Field for QoS NullFunc
1934 * frames. Powersave is handled by the fw/hw so QoS NyllFunc frames are
1935 * used only for CQM purposes (e.g. hostapd station keepalive ping) so
1936 * it is safe to downgrade to NullFunc.
1937 */
1938 if (ieee80211_is_qos_nullfunc(hdr->frame_control)) {
1939 hdr->frame_control &= ~__cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
1940 cb->htt.tid = HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
1941 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001942}
1943
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001944static void ath10k_tx_wep_key_work(struct work_struct *work)
1945{
1946 struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
1947 wep_key_work);
Michal Kazior7aa7a722014-08-25 12:09:38 +02001948 struct ath10k *ar = arvif->ar;
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001949 int ret, keyidx = arvif->def_wep_key_newidx;
1950
Michal Kazior911e6c02014-05-26 12:46:03 +03001951 mutex_lock(&arvif->ar->conf_mutex);
1952
1953 if (arvif->ar->state != ATH10K_STATE_ON)
1954 goto unlock;
1955
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001956 if (arvif->def_wep_key_idx == keyidx)
Michal Kazior911e6c02014-05-26 12:46:03 +03001957 goto unlock;
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001958
Michal Kazior7aa7a722014-08-25 12:09:38 +02001959 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001960 arvif->vdev_id, keyidx);
1961
1962 ret = ath10k_wmi_vdev_set_param(arvif->ar,
1963 arvif->vdev_id,
1964 arvif->ar->wmi.vdev_param->def_keyid,
1965 keyidx);
1966 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001967 ath10k_warn(ar, "failed to update wep key index for vdev %d: %d\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02001968 arvif->vdev_id,
1969 ret);
Michal Kazior911e6c02014-05-26 12:46:03 +03001970 goto unlock;
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001971 }
1972
1973 arvif->def_wep_key_idx = keyidx;
Michal Kazior911e6c02014-05-26 12:46:03 +03001974
1975unlock:
1976 mutex_unlock(&arvif->ar->conf_mutex);
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001977}
1978
Michal Kazior4b604552014-07-21 21:03:09 +03001979static void ath10k_tx_h_update_wep_key(struct ieee80211_vif *vif,
1980 struct ieee80211_key_conf *key,
1981 struct sk_buff *skb)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001982{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001983 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1984 struct ath10k *ar = arvif->ar;
1985 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001986
Kalle Valo5e3dd152013-06-12 20:52:10 +03001987 if (!ieee80211_has_protected(hdr->frame_control))
1988 return;
1989
1990 if (!key)
1991 return;
1992
1993 if (key->cipher != WLAN_CIPHER_SUITE_WEP40 &&
1994 key->cipher != WLAN_CIPHER_SUITE_WEP104)
1995 return;
1996
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001997 if (key->keyidx == arvif->def_wep_key_idx)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001998 return;
1999
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002000 /* FIXME: Most likely a few frames will be TXed with an old key. Simply
2001 * queueing frames until key index is updated is not an option because
2002 * sk_buff may need more processing to be done, e.g. offchannel */
2003 arvif->def_wep_key_newidx = key->keyidx;
2004 ieee80211_queue_work(ar->hw, &arvif->wep_key_work);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002005}
2006
Michal Kazior4b604552014-07-21 21:03:09 +03002007static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar,
2008 struct ieee80211_vif *vif,
2009 struct sk_buff *skb)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002010{
2011 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002012 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2013
2014 /* This is case only for P2P_GO */
2015 if (arvif->vdev_type != WMI_VDEV_TYPE_AP ||
2016 arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
2017 return;
2018
2019 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
2020 spin_lock_bh(&ar->data_lock);
2021 if (arvif->u.ap.noa_data)
2022 if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
2023 GFP_ATOMIC))
2024 memcpy(skb_put(skb, arvif->u.ap.noa_len),
2025 arvif->u.ap.noa_data,
2026 arvif->u.ap.noa_len);
2027 spin_unlock_bh(&ar->data_lock);
2028 }
2029}
2030
Michal Kazior8d6d3622014-11-24 14:58:31 +01002031static bool ath10k_mac_need_offchan_tx_work(struct ath10k *ar)
2032{
2033 /* FIXME: Not really sure since when the behaviour changed. At some
2034 * point new firmware stopped requiring creation of peer entries for
2035 * offchannel tx (and actually creating them causes issues with wmi-htc
2036 * tx credit replenishment and reliability). Assuming it's at least 3.4
2037 * because that's when the `freq` was introduced to TX_FRM HTT command.
2038 */
2039 return !(ar->htt.target_version_major >= 3 &&
2040 ar->htt.target_version_minor >= 4);
2041}
2042
Kalle Valo5e3dd152013-06-12 20:52:10 +03002043static void ath10k_tx_htt(struct ath10k *ar, struct sk_buff *skb)
2044{
2045 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002046 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002047
Michal Kazior961d4c32013-08-09 10:13:34 +02002048 if (ar->htt.target_version_major >= 3) {
2049 /* Since HTT 3.0 there is no separate mgmt tx command */
2050 ret = ath10k_htt_tx(&ar->htt, skb);
2051 goto exit;
2052 }
2053
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002054 if (ieee80211_is_mgmt(hdr->frame_control)) {
2055 if (test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
2056 ar->fw_features)) {
2057 if (skb_queue_len(&ar->wmi_mgmt_tx_queue) >=
2058 ATH10K_MAX_NUM_MGMT_PENDING) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002059 ath10k_warn(ar, "reached WMI management transmit queue limit\n");
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002060 ret = -EBUSY;
2061 goto exit;
2062 }
2063
2064 skb_queue_tail(&ar->wmi_mgmt_tx_queue, skb);
2065 ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
2066 } else {
2067 ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
2068 }
2069 } else if (!test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
2070 ar->fw_features) &&
2071 ieee80211_is_nullfunc(hdr->frame_control)) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002072 /* FW does not report tx status properly for NullFunc frames
2073 * unless they are sent through mgmt tx path. mac80211 sends
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002074 * those frames when it detects link/beacon loss and depends
2075 * on the tx status to be correct. */
Michal Kazioredb82362013-07-05 16:15:14 +03002076 ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002077 } else {
Michal Kazioredb82362013-07-05 16:15:14 +03002078 ret = ath10k_htt_tx(&ar->htt, skb);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002079 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002080
Michal Kazior961d4c32013-08-09 10:13:34 +02002081exit:
Kalle Valo5e3dd152013-06-12 20:52:10 +03002082 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002083 ath10k_warn(ar, "failed to transmit packet, dropping: %d\n",
2084 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002085 ieee80211_free_txskb(ar->hw, skb);
2086 }
2087}
2088
2089void ath10k_offchan_tx_purge(struct ath10k *ar)
2090{
2091 struct sk_buff *skb;
2092
2093 for (;;) {
2094 skb = skb_dequeue(&ar->offchan_tx_queue);
2095 if (!skb)
2096 break;
2097
2098 ieee80211_free_txskb(ar->hw, skb);
2099 }
2100}
2101
2102void ath10k_offchan_tx_work(struct work_struct *work)
2103{
2104 struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
2105 struct ath10k_peer *peer;
2106 struct ieee80211_hdr *hdr;
2107 struct sk_buff *skb;
2108 const u8 *peer_addr;
2109 int vdev_id;
2110 int ret;
2111
2112 /* FW requirement: We must create a peer before FW will send out
2113 * an offchannel frame. Otherwise the frame will be stuck and
2114 * never transmitted. We delete the peer upon tx completion.
2115 * It is unlikely that a peer for offchannel tx will already be
2116 * present. However it may be in some rare cases so account for that.
2117 * Otherwise we might remove a legitimate peer and break stuff. */
2118
2119 for (;;) {
2120 skb = skb_dequeue(&ar->offchan_tx_queue);
2121 if (!skb)
2122 break;
2123
2124 mutex_lock(&ar->conf_mutex);
2125
Michal Kazior7aa7a722014-08-25 12:09:38 +02002126 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac offchannel skb %p\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002127 skb);
2128
2129 hdr = (struct ieee80211_hdr *)skb->data;
2130 peer_addr = ieee80211_get_DA(hdr);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002131 vdev_id = ATH10K_SKB_CB(skb)->vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002132
2133 spin_lock_bh(&ar->data_lock);
2134 peer = ath10k_peer_find(ar, vdev_id, peer_addr);
2135 spin_unlock_bh(&ar->data_lock);
2136
2137 if (peer)
Kalle Valo60c3daa2013-09-08 17:56:07 +03002138 /* FIXME: should this use ath10k_warn()? */
Michal Kazior7aa7a722014-08-25 12:09:38 +02002139 ath10k_dbg(ar, ATH10K_DBG_MAC, "peer %pM on vdev %d already present\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002140 peer_addr, vdev_id);
2141
2142 if (!peer) {
2143 ret = ath10k_peer_create(ar, vdev_id, peer_addr);
2144 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002145 ath10k_warn(ar, "failed to create peer %pM on vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002146 peer_addr, vdev_id, ret);
2147 }
2148
2149 spin_lock_bh(&ar->data_lock);
Wolfram Sang16735d02013-11-14 14:32:02 -08002150 reinit_completion(&ar->offchan_tx_completed);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002151 ar->offchan_tx_skb = skb;
2152 spin_unlock_bh(&ar->data_lock);
2153
2154 ath10k_tx_htt(ar, skb);
2155
2156 ret = wait_for_completion_timeout(&ar->offchan_tx_completed,
2157 3 * HZ);
2158 if (ret <= 0)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002159 ath10k_warn(ar, "timed out waiting for offchannel skb %p\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002160 skb);
2161
2162 if (!peer) {
2163 ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
2164 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002165 ath10k_warn(ar, "failed to delete peer %pM on vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002166 peer_addr, vdev_id, ret);
2167 }
2168
2169 mutex_unlock(&ar->conf_mutex);
2170 }
2171}
2172
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002173void ath10k_mgmt_over_wmi_tx_purge(struct ath10k *ar)
2174{
2175 struct sk_buff *skb;
2176
2177 for (;;) {
2178 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
2179 if (!skb)
2180 break;
2181
2182 ieee80211_free_txskb(ar->hw, skb);
2183 }
2184}
2185
2186void ath10k_mgmt_over_wmi_tx_work(struct work_struct *work)
2187{
2188 struct ath10k *ar = container_of(work, struct ath10k, wmi_mgmt_tx_work);
2189 struct sk_buff *skb;
2190 int ret;
2191
2192 for (;;) {
2193 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
2194 if (!skb)
2195 break;
2196
2197 ret = ath10k_wmi_mgmt_tx(ar, skb);
Michal Kazior5fb5e412013-10-28 07:18:13 +01002198 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002199 ath10k_warn(ar, "failed to transmit management frame via WMI: %d\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02002200 ret);
Michal Kazior5fb5e412013-10-28 07:18:13 +01002201 ieee80211_free_txskb(ar->hw, skb);
2202 }
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002203 }
2204}
2205
Kalle Valo5e3dd152013-06-12 20:52:10 +03002206/************/
2207/* Scanning */
2208/************/
2209
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002210void __ath10k_scan_finish(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002211{
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002212 lockdep_assert_held(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002213
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002214 switch (ar->scan.state) {
2215 case ATH10K_SCAN_IDLE:
2216 break;
2217 case ATH10K_SCAN_RUNNING:
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002218 if (ar->scan.is_roc)
2219 ieee80211_remain_on_channel_expired(ar->hw);
Michal Kazior7305d3e2014-11-24 14:58:33 +01002220 case ATH10K_SCAN_ABORTING:
2221 if (!ar->scan.is_roc)
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002222 ieee80211_scan_completed(ar->hw,
2223 (ar->scan.state ==
2224 ATH10K_SCAN_ABORTING));
2225 /* fall through */
2226 case ATH10K_SCAN_STARTING:
2227 ar->scan.state = ATH10K_SCAN_IDLE;
2228 ar->scan_channel = NULL;
2229 ath10k_offchan_tx_purge(ar);
2230 cancel_delayed_work(&ar->scan.timeout);
2231 complete_all(&ar->scan.completed);
2232 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002233 }
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002234}
Kalle Valo5e3dd152013-06-12 20:52:10 +03002235
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002236void ath10k_scan_finish(struct ath10k *ar)
2237{
2238 spin_lock_bh(&ar->data_lock);
2239 __ath10k_scan_finish(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002240 spin_unlock_bh(&ar->data_lock);
2241}
2242
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002243static int ath10k_scan_stop(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002244{
2245 struct wmi_stop_scan_arg arg = {
2246 .req_id = 1, /* FIXME */
2247 .req_type = WMI_SCAN_STOP_ONE,
2248 .u.scan_id = ATH10K_SCAN_ID,
2249 };
2250 int ret;
2251
2252 lockdep_assert_held(&ar->conf_mutex);
2253
Kalle Valo5e3dd152013-06-12 20:52:10 +03002254 ret = ath10k_wmi_stop_scan(ar, &arg);
2255 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002256 ath10k_warn(ar, "failed to stop wmi scan: %d\n", ret);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002257 goto out;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002258 }
2259
Kalle Valo5e3dd152013-06-12 20:52:10 +03002260 ret = wait_for_completion_timeout(&ar->scan.completed, 3*HZ);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002261 if (ret == 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002262 ath10k_warn(ar, "failed to receive scan abortion completion: timed out\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002263 ret = -ETIMEDOUT;
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002264 } else if (ret > 0) {
2265 ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002266 }
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002267
2268out:
2269 /* Scan state should be updated upon scan completion but in case
2270 * firmware fails to deliver the event (for whatever reason) it is
2271 * desired to clean up scan state anyway. Firmware may have just
2272 * dropped the scan completion event delivery due to transport pipe
2273 * being overflown with data and/or it can recover on its own before
2274 * next scan request is submitted.
2275 */
2276 spin_lock_bh(&ar->data_lock);
2277 if (ar->scan.state != ATH10K_SCAN_IDLE)
2278 __ath10k_scan_finish(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002279 spin_unlock_bh(&ar->data_lock);
2280
2281 return ret;
2282}
2283
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002284static void ath10k_scan_abort(struct ath10k *ar)
2285{
2286 int ret;
2287
2288 lockdep_assert_held(&ar->conf_mutex);
2289
2290 spin_lock_bh(&ar->data_lock);
2291
2292 switch (ar->scan.state) {
2293 case ATH10K_SCAN_IDLE:
2294 /* This can happen if timeout worker kicked in and called
2295 * abortion while scan completion was being processed.
2296 */
2297 break;
2298 case ATH10K_SCAN_STARTING:
2299 case ATH10K_SCAN_ABORTING:
Michal Kazior7aa7a722014-08-25 12:09:38 +02002300 ath10k_warn(ar, "refusing scan abortion due to invalid scan state: %s (%d)\n",
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002301 ath10k_scan_state_str(ar->scan.state),
2302 ar->scan.state);
2303 break;
2304 case ATH10K_SCAN_RUNNING:
2305 ar->scan.state = ATH10K_SCAN_ABORTING;
2306 spin_unlock_bh(&ar->data_lock);
2307
2308 ret = ath10k_scan_stop(ar);
2309 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002310 ath10k_warn(ar, "failed to abort scan: %d\n", ret);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002311
2312 spin_lock_bh(&ar->data_lock);
2313 break;
2314 }
2315
2316 spin_unlock_bh(&ar->data_lock);
2317}
2318
2319void ath10k_scan_timeout_work(struct work_struct *work)
2320{
2321 struct ath10k *ar = container_of(work, struct ath10k,
2322 scan.timeout.work);
2323
2324 mutex_lock(&ar->conf_mutex);
2325 ath10k_scan_abort(ar);
2326 mutex_unlock(&ar->conf_mutex);
2327}
2328
Kalle Valo5e3dd152013-06-12 20:52:10 +03002329static int ath10k_start_scan(struct ath10k *ar,
2330 const struct wmi_start_scan_arg *arg)
2331{
2332 int ret;
2333
2334 lockdep_assert_held(&ar->conf_mutex);
2335
2336 ret = ath10k_wmi_start_scan(ar, arg);
2337 if (ret)
2338 return ret;
2339
Kalle Valo5e3dd152013-06-12 20:52:10 +03002340 ret = wait_for_completion_timeout(&ar->scan.started, 1*HZ);
2341 if (ret == 0) {
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002342 ret = ath10k_scan_stop(ar);
2343 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002344 ath10k_warn(ar, "failed to stop scan: %d\n", ret);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002345
2346 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002347 }
2348
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002349 /* Add a 200ms margin to account for event/command processing */
2350 ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
2351 msecs_to_jiffies(arg->max_scan_time+200));
Kalle Valo5e3dd152013-06-12 20:52:10 +03002352 return 0;
2353}
2354
2355/**********************/
2356/* mac80211 callbacks */
2357/**********************/
2358
2359static void ath10k_tx(struct ieee80211_hw *hw,
2360 struct ieee80211_tx_control *control,
2361 struct sk_buff *skb)
2362{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002363 struct ath10k *ar = hw->priv;
Michal Kazior4b604552014-07-21 21:03:09 +03002364 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2365 struct ieee80211_vif *vif = info->control.vif;
2366 struct ieee80211_key_conf *key = info->control.hw_key;
2367 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002368
2369 /* We should disable CCK RATE due to P2P */
2370 if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002371 ath10k_dbg(ar, ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002372
Michal Kazior4b604552014-07-21 21:03:09 +03002373 ATH10K_SKB_CB(skb)->htt.is_offchan = false;
2374 ATH10K_SKB_CB(skb)->htt.tid = ath10k_tx_h_get_tid(hdr);
Michal Kazior2b37c292014-09-02 11:00:22 +03002375 ATH10K_SKB_CB(skb)->vdev_id = ath10k_tx_h_get_vdev_id(ar, vif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002376
Michal Kaziorcf84bd42013-07-16 11:04:54 +02002377 /* it makes no sense to process injected frames like that */
Michal Kazior4b604552014-07-21 21:03:09 +03002378 if (vif && vif->type != NL80211_IFTYPE_MONITOR) {
2379 ath10k_tx_h_nwifi(hw, skb);
2380 ath10k_tx_h_update_wep_key(vif, key, skb);
2381 ath10k_tx_h_add_p2p_noa_ie(ar, vif, skb);
2382 ath10k_tx_h_seq_no(vif, skb);
Michal Kaziorcf84bd42013-07-16 11:04:54 +02002383 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002384
Kalle Valo5e3dd152013-06-12 20:52:10 +03002385 if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
2386 spin_lock_bh(&ar->data_lock);
Michal Kazior8d6d3622014-11-24 14:58:31 +01002387 ATH10K_SKB_CB(skb)->htt.freq = ar->scan.roc_freq;
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002388 ATH10K_SKB_CB(skb)->vdev_id = ar->scan.vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002389 spin_unlock_bh(&ar->data_lock);
2390
Michal Kazior8d6d3622014-11-24 14:58:31 +01002391 if (ath10k_mac_need_offchan_tx_work(ar)) {
2392 ATH10K_SKB_CB(skb)->htt.freq = 0;
2393 ATH10K_SKB_CB(skb)->htt.is_offchan = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002394
Michal Kazior8d6d3622014-11-24 14:58:31 +01002395 ath10k_dbg(ar, ATH10K_DBG_MAC, "queued offchannel skb %p\n",
2396 skb);
2397
2398 skb_queue_tail(&ar->offchan_tx_queue, skb);
2399 ieee80211_queue_work(hw, &ar->offchan_tx_work);
2400 return;
2401 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002402 }
2403
2404 ath10k_tx_htt(ar, skb);
2405}
2406
Michal Kaziorbca7baf2014-05-26 12:46:03 +03002407/* Must not be called with conf_mutex held as workers can use that also. */
Michal Kazior7962b0d2014-10-28 10:34:38 +01002408void ath10k_drain_tx(struct ath10k *ar)
Michal Kaziorbca7baf2014-05-26 12:46:03 +03002409{
2410 /* make sure rcu-protected mac80211 tx path itself is drained */
2411 synchronize_net();
2412
2413 ath10k_offchan_tx_purge(ar);
2414 ath10k_mgmt_over_wmi_tx_purge(ar);
2415
2416 cancel_work_sync(&ar->offchan_tx_work);
2417 cancel_work_sync(&ar->wmi_mgmt_tx_work);
2418}
2419
Michal Kazioraffd3212013-07-16 09:54:35 +02002420void ath10k_halt(struct ath10k *ar)
Michal Kazior818bdd12013-07-16 09:38:57 +02002421{
Michal Kaziord9bc4b92014-04-23 19:30:06 +03002422 struct ath10k_vif *arvif;
2423
Michal Kazior818bdd12013-07-16 09:38:57 +02002424 lockdep_assert_held(&ar->conf_mutex);
2425
Michal Kazior19337472014-08-28 12:58:16 +02002426 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
2427 ar->filter_flags = 0;
2428 ar->monitor = false;
2429
2430 if (ar->monitor_started)
Michal Kazior1bbc0972014-04-08 09:45:47 +03002431 ath10k_monitor_stop(ar);
Michal Kazior19337472014-08-28 12:58:16 +02002432
2433 ar->monitor_started = false;
Michal Kazior1bbc0972014-04-08 09:45:47 +03002434
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002435 ath10k_scan_finish(ar);
Michal Kazior818bdd12013-07-16 09:38:57 +02002436 ath10k_peer_cleanup_all(ar);
2437 ath10k_core_stop(ar);
2438 ath10k_hif_power_down(ar);
2439
2440 spin_lock_bh(&ar->data_lock);
Michal Kazior64badcb2014-09-18 11:18:02 +03002441 list_for_each_entry(arvif, &ar->arvifs, list)
2442 ath10k_mac_vif_beacon_cleanup(arvif);
Michal Kazior818bdd12013-07-16 09:38:57 +02002443 spin_unlock_bh(&ar->data_lock);
2444}
2445
Ben Greear46acf7b2014-05-16 17:15:38 +03002446static int ath10k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
2447{
2448 struct ath10k *ar = hw->priv;
2449
2450 mutex_lock(&ar->conf_mutex);
2451
2452 if (ar->cfg_tx_chainmask) {
2453 *tx_ant = ar->cfg_tx_chainmask;
2454 *rx_ant = ar->cfg_rx_chainmask;
2455 } else {
2456 *tx_ant = ar->supp_tx_chainmask;
2457 *rx_ant = ar->supp_rx_chainmask;
2458 }
2459
2460 mutex_unlock(&ar->conf_mutex);
2461
2462 return 0;
2463}
2464
Ben Greear5572a952014-11-24 16:22:10 +02002465static void ath10k_check_chain_mask(struct ath10k *ar, u32 cm, const char *dbg)
2466{
2467 /* It is not clear that allowing gaps in chainmask
2468 * is helpful. Probably it will not do what user
2469 * is hoping for, so warn in that case.
2470 */
2471 if (cm == 15 || cm == 7 || cm == 3 || cm == 1 || cm == 0)
2472 return;
2473
2474 ath10k_warn(ar, "mac %s antenna chainmask may be invalid: 0x%x. Suggested values: 15, 7, 3, 1 or 0.\n",
2475 dbg, cm);
2476}
2477
Ben Greear46acf7b2014-05-16 17:15:38 +03002478static int __ath10k_set_antenna(struct ath10k *ar, u32 tx_ant, u32 rx_ant)
2479{
2480 int ret;
2481
2482 lockdep_assert_held(&ar->conf_mutex);
2483
Ben Greear5572a952014-11-24 16:22:10 +02002484 ath10k_check_chain_mask(ar, tx_ant, "tx");
2485 ath10k_check_chain_mask(ar, rx_ant, "rx");
2486
Ben Greear46acf7b2014-05-16 17:15:38 +03002487 ar->cfg_tx_chainmask = tx_ant;
2488 ar->cfg_rx_chainmask = rx_ant;
2489
2490 if ((ar->state != ATH10K_STATE_ON) &&
2491 (ar->state != ATH10K_STATE_RESTARTED))
2492 return 0;
2493
2494 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->tx_chain_mask,
2495 tx_ant);
2496 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002497 ath10k_warn(ar, "failed to set tx-chainmask: %d, req 0x%x\n",
Ben Greear46acf7b2014-05-16 17:15:38 +03002498 ret, tx_ant);
2499 return ret;
2500 }
2501
2502 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->rx_chain_mask,
2503 rx_ant);
2504 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002505 ath10k_warn(ar, "failed to set rx-chainmask: %d, req 0x%x\n",
Ben Greear46acf7b2014-05-16 17:15:38 +03002506 ret, rx_ant);
2507 return ret;
2508 }
2509
2510 return 0;
2511}
2512
2513static int ath10k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
2514{
2515 struct ath10k *ar = hw->priv;
2516 int ret;
2517
2518 mutex_lock(&ar->conf_mutex);
2519 ret = __ath10k_set_antenna(ar, tx_ant, rx_ant);
2520 mutex_unlock(&ar->conf_mutex);
2521 return ret;
2522}
2523
Kalle Valo5e3dd152013-06-12 20:52:10 +03002524static int ath10k_start(struct ieee80211_hw *hw)
2525{
2526 struct ath10k *ar = hw->priv;
Michal Kazior818bdd12013-07-16 09:38:57 +02002527 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002528
Michal Kaziorbca7baf2014-05-26 12:46:03 +03002529 /*
2530 * This makes sense only when restarting hw. It is harmless to call
2531 * uncoditionally. This is necessary to make sure no HTT/WMI tx
2532 * commands will be submitted while restarting.
2533 */
2534 ath10k_drain_tx(ar);
2535
Michal Kazior548db542013-07-05 16:15:15 +03002536 mutex_lock(&ar->conf_mutex);
2537
Michal Kaziorc5058f52014-05-26 12:46:03 +03002538 switch (ar->state) {
2539 case ATH10K_STATE_OFF:
2540 ar->state = ATH10K_STATE_ON;
2541 break;
2542 case ATH10K_STATE_RESTARTING:
2543 ath10k_halt(ar);
2544 ar->state = ATH10K_STATE_RESTARTED;
2545 break;
2546 case ATH10K_STATE_ON:
2547 case ATH10K_STATE_RESTARTED:
2548 case ATH10K_STATE_WEDGED:
2549 WARN_ON(1);
Michal Kazior818bdd12013-07-16 09:38:57 +02002550 ret = -EINVAL;
Michal Kaziorae254432014-05-26 12:46:02 +03002551 goto err;
Kalle Valo43d2a302014-09-10 18:23:30 +03002552 case ATH10K_STATE_UTF:
2553 ret = -EBUSY;
2554 goto err;
Michal Kazior818bdd12013-07-16 09:38:57 +02002555 }
2556
2557 ret = ath10k_hif_power_up(ar);
2558 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002559 ath10k_err(ar, "Could not init hif: %d\n", ret);
Michal Kaziorae254432014-05-26 12:46:02 +03002560 goto err_off;
Michal Kazior818bdd12013-07-16 09:38:57 +02002561 }
2562
Kalle Valo43d2a302014-09-10 18:23:30 +03002563 ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_NORMAL);
Michal Kazior818bdd12013-07-16 09:38:57 +02002564 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002565 ath10k_err(ar, "Could not init core: %d\n", ret);
Michal Kaziorae254432014-05-26 12:46:02 +03002566 goto err_power_down;
Michal Kazior818bdd12013-07-16 09:38:57 +02002567 }
2568
Bartosz Markowski226a3392013-09-26 17:47:16 +02002569 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->pmf_qos, 1);
Michal Kaziorae254432014-05-26 12:46:02 +03002570 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002571 ath10k_warn(ar, "failed to enable PMF QOS: %d\n", ret);
Michal Kaziorae254432014-05-26 12:46:02 +03002572 goto err_core_stop;
2573 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002574
Michal Kaziorc4dd0d02013-11-13 11:05:10 +01002575 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->dynamic_bw, 1);
Michal Kaziorae254432014-05-26 12:46:02 +03002576 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002577 ath10k_warn(ar, "failed to enable dynamic BW: %d\n", ret);
Michal Kaziorae254432014-05-26 12:46:02 +03002578 goto err_core_stop;
2579 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002580
Ben Greear46acf7b2014-05-16 17:15:38 +03002581 if (ar->cfg_tx_chainmask)
2582 __ath10k_set_antenna(ar, ar->cfg_tx_chainmask,
2583 ar->cfg_rx_chainmask);
2584
Marek Puzyniakab6258e2014-01-29 15:03:31 +02002585 /*
2586 * By default FW set ARP frames ac to voice (6). In that case ARP
2587 * exchange is not working properly for UAPSD enabled AP. ARP requests
2588 * which arrives with access category 0 are processed by network stack
2589 * and send back with access category 0, but FW changes access category
2590 * to 6. Set ARP frames access category to best effort (0) solves
2591 * this problem.
2592 */
2593
2594 ret = ath10k_wmi_pdev_set_param(ar,
2595 ar->wmi.pdev_param->arp_ac_override, 0);
2596 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002597 ath10k_warn(ar, "failed to set arp ac override parameter: %d\n",
Marek Puzyniakab6258e2014-01-29 15:03:31 +02002598 ret);
Michal Kaziorae254432014-05-26 12:46:02 +03002599 goto err_core_stop;
Marek Puzyniakab6258e2014-01-29 15:03:31 +02002600 }
2601
Michal Kaziord6500972014-04-08 09:56:09 +03002602 ar->num_started_vdevs = 0;
Michal Kaziorf7843d72013-07-16 09:38:52 +02002603 ath10k_regd_update(ar);
2604
Simon Wunderlich855aed12014-08-02 09:12:54 +03002605 ath10k_spectral_start(ar);
2606
Michal Kaziorae254432014-05-26 12:46:02 +03002607 mutex_unlock(&ar->conf_mutex);
2608 return 0;
2609
2610err_core_stop:
2611 ath10k_core_stop(ar);
2612
2613err_power_down:
2614 ath10k_hif_power_down(ar);
2615
2616err_off:
2617 ar->state = ATH10K_STATE_OFF;
2618
2619err:
Michal Kazior548db542013-07-05 16:15:15 +03002620 mutex_unlock(&ar->conf_mutex);
Michal Kaziorc60bdd82014-01-29 07:26:31 +01002621 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002622}
2623
2624static void ath10k_stop(struct ieee80211_hw *hw)
2625{
2626 struct ath10k *ar = hw->priv;
2627
Michal Kaziorbca7baf2014-05-26 12:46:03 +03002628 ath10k_drain_tx(ar);
2629
Michal Kazior548db542013-07-05 16:15:15 +03002630 mutex_lock(&ar->conf_mutex);
Michal Kaziorc5058f52014-05-26 12:46:03 +03002631 if (ar->state != ATH10K_STATE_OFF) {
Michal Kazior818bdd12013-07-16 09:38:57 +02002632 ath10k_halt(ar);
Michal Kaziorc5058f52014-05-26 12:46:03 +03002633 ar->state = ATH10K_STATE_OFF;
2634 }
Michal Kazior548db542013-07-05 16:15:15 +03002635 mutex_unlock(&ar->conf_mutex);
2636
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002637 cancel_delayed_work_sync(&ar->scan.timeout);
Michal Kazioraffd3212013-07-16 09:54:35 +02002638 cancel_work_sync(&ar->restart_work);
2639}
2640
Michal Kaziorad088bf2013-10-16 15:44:46 +03002641static int ath10k_config_ps(struct ath10k *ar)
Michal Kazioraffd3212013-07-16 09:54:35 +02002642{
Michal Kaziorad088bf2013-10-16 15:44:46 +03002643 struct ath10k_vif *arvif;
2644 int ret = 0;
Michal Kazioraffd3212013-07-16 09:54:35 +02002645
2646 lockdep_assert_held(&ar->conf_mutex);
2647
Michal Kaziorad088bf2013-10-16 15:44:46 +03002648 list_for_each_entry(arvif, &ar->arvifs, list) {
2649 ret = ath10k_mac_vif_setup_ps(arvif);
2650 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002651 ath10k_warn(ar, "failed to setup powersave: %d\n", ret);
Michal Kaziorad088bf2013-10-16 15:44:46 +03002652 break;
2653 }
2654 }
Michal Kazioraffd3212013-07-16 09:54:35 +02002655
Michal Kaziorad088bf2013-10-16 15:44:46 +03002656 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002657}
2658
Michal Kaziorc930f742014-01-23 11:38:25 +01002659static const char *chandef_get_width(enum nl80211_chan_width width)
2660{
2661 switch (width) {
2662 case NL80211_CHAN_WIDTH_20_NOHT:
2663 return "20 (noht)";
2664 case NL80211_CHAN_WIDTH_20:
2665 return "20";
2666 case NL80211_CHAN_WIDTH_40:
2667 return "40";
2668 case NL80211_CHAN_WIDTH_80:
2669 return "80";
2670 case NL80211_CHAN_WIDTH_80P80:
2671 return "80+80";
2672 case NL80211_CHAN_WIDTH_160:
2673 return "160";
2674 case NL80211_CHAN_WIDTH_5:
2675 return "5";
2676 case NL80211_CHAN_WIDTH_10:
2677 return "10";
2678 }
2679 return "?";
2680}
2681
2682static void ath10k_config_chan(struct ath10k *ar)
2683{
2684 struct ath10k_vif *arvif;
Michal Kaziorc930f742014-01-23 11:38:25 +01002685 int ret;
2686
2687 lockdep_assert_held(&ar->conf_mutex);
2688
Michal Kazior7aa7a722014-08-25 12:09:38 +02002689 ath10k_dbg(ar, ATH10K_DBG_MAC,
Michal Kaziorc930f742014-01-23 11:38:25 +01002690 "mac config channel to %dMHz (cf1 %dMHz cf2 %dMHz width %s)\n",
2691 ar->chandef.chan->center_freq,
2692 ar->chandef.center_freq1,
2693 ar->chandef.center_freq2,
2694 chandef_get_width(ar->chandef.width));
2695
2696 /* First stop monitor interface. Some FW versions crash if there's a
2697 * lone monitor interface. */
Michal Kazior1bbc0972014-04-08 09:45:47 +03002698 if (ar->monitor_started)
Michal Kazior19337472014-08-28 12:58:16 +02002699 ath10k_monitor_stop(ar);
Michal Kaziorc930f742014-01-23 11:38:25 +01002700
2701 list_for_each_entry(arvif, &ar->arvifs, list) {
2702 if (!arvif->is_started)
2703 continue;
2704
Michal Kaziordc55e302014-07-29 12:53:36 +03002705 if (!arvif->is_up)
2706 continue;
2707
Michal Kaziorc930f742014-01-23 11:38:25 +01002708 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
2709 continue;
2710
Michal Kaziordc55e302014-07-29 12:53:36 +03002711 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
Michal Kaziorc930f742014-01-23 11:38:25 +01002712 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002713 ath10k_warn(ar, "failed to down vdev %d: %d\n",
Michal Kaziorc930f742014-01-23 11:38:25 +01002714 arvif->vdev_id, ret);
2715 continue;
2716 }
2717 }
2718
Michal Kaziordc55e302014-07-29 12:53:36 +03002719 /* all vdevs are downed now - attempt to restart and re-up them */
Michal Kaziorc930f742014-01-23 11:38:25 +01002720
2721 list_for_each_entry(arvif, &ar->arvifs, list) {
2722 if (!arvif->is_started)
2723 continue;
2724
2725 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
2726 continue;
2727
Michal Kaziordc55e302014-07-29 12:53:36 +03002728 ret = ath10k_vdev_restart(arvif);
Michal Kaziorc930f742014-01-23 11:38:25 +01002729 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002730 ath10k_warn(ar, "failed to restart vdev %d: %d\n",
Michal Kaziorc930f742014-01-23 11:38:25 +01002731 arvif->vdev_id, ret);
2732 continue;
2733 }
2734
2735 if (!arvif->is_up)
2736 continue;
2737
2738 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
2739 arvif->bssid);
2740 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002741 ath10k_warn(ar, "failed to bring vdev up %d: %d\n",
Michal Kaziorc930f742014-01-23 11:38:25 +01002742 arvif->vdev_id, ret);
2743 continue;
2744 }
2745 }
2746
Michal Kazior19337472014-08-28 12:58:16 +02002747 ath10k_monitor_recalc(ar);
Michal Kaziorc930f742014-01-23 11:38:25 +01002748}
2749
Michal Kazior7d9d5582014-10-21 10:40:15 +03002750static int ath10k_mac_txpower_setup(struct ath10k *ar, int txpower)
2751{
2752 int ret;
2753 u32 param;
2754
2755 lockdep_assert_held(&ar->conf_mutex);
2756
2757 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac txpower %d\n", txpower);
2758
2759 param = ar->wmi.pdev_param->txpower_limit2g;
2760 ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
2761 if (ret) {
2762 ath10k_warn(ar, "failed to set 2g txpower %d: %d\n",
2763 txpower, ret);
2764 return ret;
2765 }
2766
2767 param = ar->wmi.pdev_param->txpower_limit5g;
2768 ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
2769 if (ret) {
2770 ath10k_warn(ar, "failed to set 5g txpower %d: %d\n",
2771 txpower, ret);
2772 return ret;
2773 }
2774
2775 return 0;
2776}
2777
2778static int ath10k_mac_txpower_recalc(struct ath10k *ar)
2779{
2780 struct ath10k_vif *arvif;
2781 int ret, txpower = -1;
2782
2783 lockdep_assert_held(&ar->conf_mutex);
2784
2785 list_for_each_entry(arvif, &ar->arvifs, list) {
2786 WARN_ON(arvif->txpower < 0);
2787
2788 if (txpower == -1)
2789 txpower = arvif->txpower;
2790 else
2791 txpower = min(txpower, arvif->txpower);
2792 }
2793
2794 if (WARN_ON(txpower == -1))
2795 return -EINVAL;
2796
2797 ret = ath10k_mac_txpower_setup(ar, txpower);
2798 if (ret) {
2799 ath10k_warn(ar, "failed to setup tx power %d: %d\n",
2800 txpower, ret);
2801 return ret;
2802 }
2803
2804 return 0;
2805}
2806
Kalle Valo5e3dd152013-06-12 20:52:10 +03002807static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
2808{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002809 struct ath10k *ar = hw->priv;
2810 struct ieee80211_conf *conf = &hw->conf;
2811 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002812
2813 mutex_lock(&ar->conf_mutex);
2814
2815 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002816 ath10k_dbg(ar, ATH10K_DBG_MAC,
Michal Kaziord6500972014-04-08 09:56:09 +03002817 "mac config channel %dMHz flags 0x%x radar %d\n",
Marek Puzyniake8a50f82013-11-20 09:59:47 +02002818 conf->chandef.chan->center_freq,
Michal Kaziord6500972014-04-08 09:56:09 +03002819 conf->chandef.chan->flags,
2820 conf->radar_enabled);
Marek Puzyniake8a50f82013-11-20 09:59:47 +02002821
Kalle Valo5e3dd152013-06-12 20:52:10 +03002822 spin_lock_bh(&ar->data_lock);
2823 ar->rx_channel = conf->chandef.chan;
2824 spin_unlock_bh(&ar->data_lock);
Marek Puzyniake8a50f82013-11-20 09:59:47 +02002825
Michal Kaziord6500972014-04-08 09:56:09 +03002826 ar->radar_enabled = conf->radar_enabled;
2827 ath10k_recalc_radar_detection(ar);
Michal Kaziorc930f742014-01-23 11:38:25 +01002828
2829 if (!cfg80211_chandef_identical(&ar->chandef, &conf->chandef)) {
2830 ar->chandef = conf->chandef;
2831 ath10k_config_chan(ar);
2832 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002833 }
2834
Michal Kazioraffd3212013-07-16 09:54:35 +02002835 if (changed & IEEE80211_CONF_CHANGE_PS)
2836 ath10k_config_ps(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002837
2838 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
Michal Kazior19337472014-08-28 12:58:16 +02002839 ar->monitor = conf->flags & IEEE80211_CONF_MONITOR;
2840 ret = ath10k_monitor_recalc(ar);
2841 if (ret)
2842 ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002843 }
2844
2845 mutex_unlock(&ar->conf_mutex);
2846 return ret;
2847}
2848
Ben Greear5572a952014-11-24 16:22:10 +02002849static u32 get_nss_from_chainmask(u16 chain_mask)
2850{
2851 if ((chain_mask & 0x15) == 0x15)
2852 return 4;
2853 else if ((chain_mask & 0x7) == 0x7)
2854 return 3;
2855 else if ((chain_mask & 0x3) == 0x3)
2856 return 2;
2857 return 1;
2858}
2859
Kalle Valo5e3dd152013-06-12 20:52:10 +03002860/*
2861 * TODO:
2862 * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
2863 * because we will send mgmt frames without CCK. This requirement
2864 * for P2P_FIND/GO_NEG should be handled by checking CCK flag
2865 * in the TX packet.
2866 */
2867static int ath10k_add_interface(struct ieee80211_hw *hw,
2868 struct ieee80211_vif *vif)
2869{
2870 struct ath10k *ar = hw->priv;
2871 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2872 enum wmi_sta_powersave_param param;
2873 int ret = 0;
Kalle Valo5a13e762014-01-20 11:01:46 +02002874 u32 value;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002875 int bit;
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02002876 u32 vdev_param;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002877
2878 mutex_lock(&ar->conf_mutex);
2879
Michal Kazior0dbd09e2013-07-31 10:55:14 +02002880 memset(arvif, 0, sizeof(*arvif));
2881
Kalle Valo5e3dd152013-06-12 20:52:10 +03002882 arvif->ar = ar;
2883 arvif->vif = vif;
2884
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002885 INIT_WORK(&arvif->wep_key_work, ath10k_tx_wep_key_work);
Ben Greeare63b33f2013-10-22 14:54:14 -07002886 INIT_LIST_HEAD(&arvif->list);
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002887
Ben Greeara9aefb32014-08-12 11:02:19 +03002888 if (ar->free_vdev_map == 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002889 ath10k_warn(ar, "Free vdev map is empty, no more interfaces allowed.\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002890 ret = -EBUSY;
Michal Kazior9dad14a2013-10-16 15:44:45 +03002891 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002892 }
Ben Greear16c11172014-09-23 14:17:16 -07002893 bit = __ffs64(ar->free_vdev_map);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002894
Ben Greear16c11172014-09-23 14:17:16 -07002895 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac create vdev %i map %llx\n",
2896 bit, ar->free_vdev_map);
2897
2898 arvif->vdev_id = bit;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002899 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002900
2901 if (ar->p2p)
2902 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
2903
2904 switch (vif->type) {
2905 case NL80211_IFTYPE_UNSPECIFIED:
2906 case NL80211_IFTYPE_STATION:
2907 arvif->vdev_type = WMI_VDEV_TYPE_STA;
2908 if (vif->p2p)
2909 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
2910 break;
2911 case NL80211_IFTYPE_ADHOC:
2912 arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
2913 break;
2914 case NL80211_IFTYPE_AP:
2915 arvif->vdev_type = WMI_VDEV_TYPE_AP;
2916
2917 if (vif->p2p)
2918 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
2919 break;
2920 case NL80211_IFTYPE_MONITOR:
2921 arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
2922 break;
2923 default:
2924 WARN_ON(1);
2925 break;
2926 }
2927
Michal Kazior64badcb2014-09-18 11:18:02 +03002928 /* Some firmware revisions don't wait for beacon tx completion before
2929 * sending another SWBA event. This could lead to hardware using old
2930 * (freed) beacon data in some cases, e.g. tx credit starvation
2931 * combined with missed TBTT. This is very very rare.
2932 *
2933 * On non-IOMMU-enabled hosts this could be a possible security issue
2934 * because hw could beacon some random data on the air. On
2935 * IOMMU-enabled hosts DMAR faults would occur in most cases and target
2936 * device would crash.
2937 *
2938 * Since there are no beacon tx completions (implicit nor explicit)
2939 * propagated to host the only workaround for this is to allocate a
2940 * DMA-coherent buffer for a lifetime of a vif and use it for all
2941 * beacon tx commands. Worst case for this approach is some beacons may
2942 * become corrupted, e.g. have garbled IEs or out-of-date TIM bitmap.
2943 */
2944 if (vif->type == NL80211_IFTYPE_ADHOC ||
2945 vif->type == NL80211_IFTYPE_AP) {
2946 arvif->beacon_buf = dma_zalloc_coherent(ar->dev,
2947 IEEE80211_MAX_FRAME_LEN,
2948 &arvif->beacon_paddr,
Rajkumar Manoharan82d7aba2014-10-10 17:38:27 +05302949 GFP_ATOMIC);
Michal Kazior64badcb2014-09-18 11:18:02 +03002950 if (!arvif->beacon_buf) {
2951 ret = -ENOMEM;
2952 ath10k_warn(ar, "failed to allocate beacon buffer: %d\n",
2953 ret);
2954 goto err;
2955 }
2956 }
2957
2958 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d bcnmode %s\n",
2959 arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
2960 arvif->beacon_buf ? "single-buf" : "per-skb");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002961
2962 ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
2963 arvif->vdev_subtype, vif->addr);
2964 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002965 ath10k_warn(ar, "failed to create WMI vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02002966 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002967 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002968 }
2969
Ben Greear16c11172014-09-23 14:17:16 -07002970 ar->free_vdev_map &= ~(1LL << arvif->vdev_id);
Michal Kazior05791192013-10-16 15:44:45 +03002971 list_add(&arvif->list, &ar->arvifs);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002972
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02002973 vdev_param = ar->wmi.vdev_param->def_keyid;
2974 ret = ath10k_wmi_vdev_set_param(ar, 0, vdev_param,
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002975 arvif->def_wep_key_idx);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002976 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002977 ath10k_warn(ar, "failed to set vdev %i default key id: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02002978 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002979 goto err_vdev_delete;
2980 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002981
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02002982 vdev_param = ar->wmi.vdev_param->tx_encap_type;
2983 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002984 ATH10K_HW_TXRX_NATIVE_WIFI);
Bartosz Markowskiebc9abd2013-10-15 09:26:20 +02002985 /* 10.X firmware does not support this VDEV parameter. Do not warn */
Michal Kazior9dad14a2013-10-16 15:44:45 +03002986 if (ret && ret != -EOPNOTSUPP) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002987 ath10k_warn(ar, "failed to set vdev %i TX encapsulation: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02002988 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002989 goto err_vdev_delete;
2990 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002991
Ben Greear5572a952014-11-24 16:22:10 +02002992 if (ar->cfg_tx_chainmask) {
2993 u16 nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
2994
2995 vdev_param = ar->wmi.vdev_param->nss;
2996 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
2997 nss);
2998 if (ret) {
2999 ath10k_warn(ar, "failed to set vdev %i chainmask 0x%x, nss %i: %d\n",
3000 arvif->vdev_id, ar->cfg_tx_chainmask, nss,
3001 ret);
3002 goto err_vdev_delete;
3003 }
3004 }
3005
Kalle Valo5e3dd152013-06-12 20:52:10 +03003006 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3007 ret = ath10k_peer_create(ar, arvif->vdev_id, vif->addr);
3008 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003009 ath10k_warn(ar, "failed to create vdev %i peer for AP: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003010 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003011 goto err_vdev_delete;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003012 }
Marek Puzyniakcdf07402013-12-30 09:07:51 +01003013
Kalle Valo5a13e762014-01-20 11:01:46 +02003014 ret = ath10k_mac_set_kickout(arvif);
3015 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003016 ath10k_warn(ar, "failed to set vdev %i kickout parameters: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003017 arvif->vdev_id, ret);
Kalle Valo5a13e762014-01-20 11:01:46 +02003018 goto err_peer_delete;
3019 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003020 }
3021
3022 if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
3023 param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
3024 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
3025 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3026 param, value);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003027 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003028 ath10k_warn(ar, "failed to set vdev %i RX wake policy: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003029 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003030 goto err_peer_delete;
3031 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003032
3033 param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
3034 value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
3035 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3036 param, value);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003037 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003038 ath10k_warn(ar, "failed to set vdev %i TX wake thresh: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003039 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003040 goto err_peer_delete;
3041 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003042
3043 param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
3044 value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
3045 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3046 param, value);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003047 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003048 ath10k_warn(ar, "failed to set vdev %i PSPOLL count: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003049 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003050 goto err_peer_delete;
3051 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003052 }
3053
Michal Kazior424121c2013-07-22 14:13:31 +02003054 ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003055 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003056 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
Michal Kazior679c54a2013-07-05 16:15:04 +03003057 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003058 goto err_peer_delete;
3059 }
Michal Kazior679c54a2013-07-05 16:15:04 +03003060
Michal Kazior424121c2013-07-22 14:13:31 +02003061 ret = ath10k_mac_set_frag(arvif, ar->hw->wiphy->frag_threshold);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003062 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003063 ath10k_warn(ar, "failed to set frag threshold for vdev %d: %d\n",
Michal Kazior679c54a2013-07-05 16:15:04 +03003064 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003065 goto err_peer_delete;
3066 }
Michal Kazior679c54a2013-07-05 16:15:04 +03003067
Michal Kazior7d9d5582014-10-21 10:40:15 +03003068 arvif->txpower = vif->bss_conf.txpower;
3069 ret = ath10k_mac_txpower_recalc(ar);
3070 if (ret) {
3071 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
3072 goto err_peer_delete;
3073 }
3074
Kalle Valo5e3dd152013-06-12 20:52:10 +03003075 mutex_unlock(&ar->conf_mutex);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003076 return 0;
3077
3078err_peer_delete:
3079 if (arvif->vdev_type == WMI_VDEV_TYPE_AP)
3080 ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
3081
3082err_vdev_delete:
3083 ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
Ben Greear16c11172014-09-23 14:17:16 -07003084 ar->free_vdev_map |= 1LL << arvif->vdev_id;
Michal Kazior05791192013-10-16 15:44:45 +03003085 list_del(&arvif->list);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003086
3087err:
Michal Kazior64badcb2014-09-18 11:18:02 +03003088 if (arvif->beacon_buf) {
3089 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
3090 arvif->beacon_buf, arvif->beacon_paddr);
3091 arvif->beacon_buf = NULL;
3092 }
3093
Michal Kazior9dad14a2013-10-16 15:44:45 +03003094 mutex_unlock(&ar->conf_mutex);
3095
Kalle Valo5e3dd152013-06-12 20:52:10 +03003096 return ret;
3097}
3098
3099static void ath10k_remove_interface(struct ieee80211_hw *hw,
3100 struct ieee80211_vif *vif)
3101{
3102 struct ath10k *ar = hw->priv;
3103 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3104 int ret;
3105
Michal Kaziorcc4827b2013-10-16 15:44:45 +03003106 cancel_work_sync(&arvif->wep_key_work);
3107
Sujith Manoharan5d011f52014-11-25 11:47:00 +05303108 mutex_lock(&ar->conf_mutex);
3109
Michal Kaziored543882013-09-13 14:16:56 +02003110 spin_lock_bh(&ar->data_lock);
Michal Kazior64badcb2014-09-18 11:18:02 +03003111 ath10k_mac_vif_beacon_cleanup(arvif);
Michal Kaziored543882013-09-13 14:16:56 +02003112 spin_unlock_bh(&ar->data_lock);
3113
Simon Wunderlich855aed12014-08-02 09:12:54 +03003114 ret = ath10k_spectral_vif_stop(arvif);
3115 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003116 ath10k_warn(ar, "failed to stop spectral for vdev %i: %d\n",
Simon Wunderlich855aed12014-08-02 09:12:54 +03003117 arvif->vdev_id, ret);
3118
Ben Greear16c11172014-09-23 14:17:16 -07003119 ar->free_vdev_map |= 1LL << arvif->vdev_id;
Michal Kazior05791192013-10-16 15:44:45 +03003120 list_del(&arvif->list);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003121
3122 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3123 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, vif->addr);
3124 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003125 ath10k_warn(ar, "failed to remove peer for AP vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003126 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003127
3128 kfree(arvif->u.ap.noa_data);
3129 }
3130
Michal Kazior7aa7a722014-08-25 12:09:38 +02003131 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i delete (remove interface)\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03003132 arvif->vdev_id);
3133
Kalle Valo5e3dd152013-06-12 20:52:10 +03003134 ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
3135 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003136 ath10k_warn(ar, "failed to delete WMI vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003137 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003138
Kalle Valo5e3dd152013-06-12 20:52:10 +03003139 ath10k_peer_cleanup(ar, arvif->vdev_id);
3140
3141 mutex_unlock(&ar->conf_mutex);
3142}
3143
3144/*
3145 * FIXME: Has to be verified.
3146 */
3147#define SUPPORTED_FILTERS \
3148 (FIF_PROMISC_IN_BSS | \
3149 FIF_ALLMULTI | \
3150 FIF_CONTROL | \
3151 FIF_PSPOLL | \
3152 FIF_OTHER_BSS | \
3153 FIF_BCN_PRBRESP_PROMISC | \
3154 FIF_PROBE_REQ | \
3155 FIF_FCSFAIL)
3156
3157static void ath10k_configure_filter(struct ieee80211_hw *hw,
3158 unsigned int changed_flags,
3159 unsigned int *total_flags,
3160 u64 multicast)
3161{
3162 struct ath10k *ar = hw->priv;
3163 int ret;
3164
3165 mutex_lock(&ar->conf_mutex);
3166
3167 changed_flags &= SUPPORTED_FILTERS;
3168 *total_flags &= SUPPORTED_FILTERS;
3169 ar->filter_flags = *total_flags;
3170
Michal Kazior19337472014-08-28 12:58:16 +02003171 ret = ath10k_monitor_recalc(ar);
3172 if (ret)
3173 ath10k_warn(ar, "failed to recalc montior: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003174
3175 mutex_unlock(&ar->conf_mutex);
3176}
3177
3178static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
3179 struct ieee80211_vif *vif,
3180 struct ieee80211_bss_conf *info,
3181 u32 changed)
3182{
3183 struct ath10k *ar = hw->priv;
3184 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3185 int ret = 0;
Kalle Valoaf762c02014-09-14 12:50:17 +03003186 u32 vdev_param, pdev_param, slottime, preamble;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003187
3188 mutex_lock(&ar->conf_mutex);
3189
3190 if (changed & BSS_CHANGED_IBSS)
3191 ath10k_control_ibss(arvif, info, vif->addr);
3192
3193 if (changed & BSS_CHANGED_BEACON_INT) {
3194 arvif->beacon_interval = info->beacon_int;
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003195 vdev_param = ar->wmi.vdev_param->beacon_interval;
3196 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003197 arvif->beacon_interval);
Michal Kazior7aa7a722014-08-25 12:09:38 +02003198 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003199 "mac vdev %d beacon_interval %d\n",
3200 arvif->vdev_id, arvif->beacon_interval);
3201
Kalle Valo5e3dd152013-06-12 20:52:10 +03003202 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003203 ath10k_warn(ar, "failed to set beacon interval for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003204 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003205 }
3206
3207 if (changed & BSS_CHANGED_BEACON) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003208 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003209 "vdev %d set beacon tx mode to staggered\n",
3210 arvif->vdev_id);
3211
Bartosz Markowski226a3392013-09-26 17:47:16 +02003212 pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
3213 ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003214 WMI_BEACON_STAGGERED_MODE);
3215 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003216 ath10k_warn(ar, "failed to set beacon mode for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003217 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003218 }
3219
John W. Linvilleb70727e2013-06-13 13:34:29 -04003220 if (changed & BSS_CHANGED_BEACON_INFO) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03003221 arvif->dtim_period = info->dtim_period;
3222
Michal Kazior7aa7a722014-08-25 12:09:38 +02003223 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003224 "mac vdev %d dtim_period %d\n",
3225 arvif->vdev_id, arvif->dtim_period);
3226
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003227 vdev_param = ar->wmi.vdev_param->dtim_period;
3228 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003229 arvif->dtim_period);
3230 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003231 ath10k_warn(ar, "failed to set dtim period for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003232 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003233 }
3234
3235 if (changed & BSS_CHANGED_SSID &&
3236 vif->type == NL80211_IFTYPE_AP) {
3237 arvif->u.ap.ssid_len = info->ssid_len;
3238 if (info->ssid_len)
3239 memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
3240 arvif->u.ap.hidden_ssid = info->hidden_ssid;
3241 }
3242
Michal Kazior077efc82014-10-21 10:10:29 +03003243 if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
3244 ether_addr_copy(arvif->bssid, info->bssid);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003245
3246 if (changed & BSS_CHANGED_BEACON_ENABLED)
3247 ath10k_control_beaconing(arvif, info);
3248
3249 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02003250 arvif->use_cts_prot = info->use_cts_prot;
Michal Kazior7aa7a722014-08-25 12:09:38 +02003251 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d cts_prot %d\n",
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02003252 arvif->vdev_id, info->use_cts_prot);
Kalle Valo60c3daa2013-09-08 17:56:07 +03003253
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02003254 ret = ath10k_recalc_rtscts_prot(arvif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003255 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003256 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003257 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003258 }
3259
3260 if (changed & BSS_CHANGED_ERP_SLOT) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03003261 if (info->use_short_slot)
3262 slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
3263
3264 else
3265 slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
3266
Michal Kazior7aa7a722014-08-25 12:09:38 +02003267 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03003268 arvif->vdev_id, slottime);
3269
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003270 vdev_param = ar->wmi.vdev_param->slot_time;
3271 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003272 slottime);
3273 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003274 ath10k_warn(ar, "failed to set erp slot for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003275 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003276 }
3277
3278 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03003279 if (info->use_short_preamble)
3280 preamble = WMI_VDEV_PREAMBLE_SHORT;
3281 else
3282 preamble = WMI_VDEV_PREAMBLE_LONG;
3283
Michal Kazior7aa7a722014-08-25 12:09:38 +02003284 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003285 "mac vdev %d preamble %dn",
3286 arvif->vdev_id, preamble);
3287
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003288 vdev_param = ar->wmi.vdev_param->preamble;
3289 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003290 preamble);
3291 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003292 ath10k_warn(ar, "failed to set preamble for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003293 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003294 }
3295
3296 if (changed & BSS_CHANGED_ASSOC) {
Michal Kaziore556f112014-08-28 12:58:17 +02003297 if (info->assoc) {
3298 /* Workaround: Make sure monitor vdev is not running
3299 * when associating to prevent some firmware revisions
3300 * (e.g. 10.1 and 10.2) from crashing.
3301 */
3302 if (ar->monitor_started)
3303 ath10k_monitor_stop(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003304 ath10k_bss_assoc(hw, vif, info);
Michal Kaziore556f112014-08-28 12:58:17 +02003305 ath10k_monitor_recalc(ar);
Michal Kazior077efc82014-10-21 10:10:29 +03003306 } else {
3307 ath10k_bss_disassoc(hw, vif);
Michal Kaziore556f112014-08-28 12:58:17 +02003308 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003309 }
3310
Michal Kazior7d9d5582014-10-21 10:40:15 +03003311 if (changed & BSS_CHANGED_TXPOWER) {
3312 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev_id %i txpower %d\n",
3313 arvif->vdev_id, info->txpower);
3314
3315 arvif->txpower = info->txpower;
3316 ret = ath10k_mac_txpower_recalc(ar);
3317 if (ret)
3318 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
3319 }
3320
Kalle Valo5e3dd152013-06-12 20:52:10 +03003321 mutex_unlock(&ar->conf_mutex);
3322}
3323
3324static int ath10k_hw_scan(struct ieee80211_hw *hw,
3325 struct ieee80211_vif *vif,
David Spinadelc56ef672014-02-05 15:21:13 +02003326 struct ieee80211_scan_request *hw_req)
Kalle Valo5e3dd152013-06-12 20:52:10 +03003327{
3328 struct ath10k *ar = hw->priv;
3329 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
David Spinadelc56ef672014-02-05 15:21:13 +02003330 struct cfg80211_scan_request *req = &hw_req->req;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003331 struct wmi_start_scan_arg arg;
3332 int ret = 0;
3333 int i;
3334
3335 mutex_lock(&ar->conf_mutex);
3336
3337 spin_lock_bh(&ar->data_lock);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003338 switch (ar->scan.state) {
3339 case ATH10K_SCAN_IDLE:
3340 reinit_completion(&ar->scan.started);
3341 reinit_completion(&ar->scan.completed);
3342 ar->scan.state = ATH10K_SCAN_STARTING;
3343 ar->scan.is_roc = false;
3344 ar->scan.vdev_id = arvif->vdev_id;
3345 ret = 0;
3346 break;
3347 case ATH10K_SCAN_STARTING:
3348 case ATH10K_SCAN_RUNNING:
3349 case ATH10K_SCAN_ABORTING:
Kalle Valo5e3dd152013-06-12 20:52:10 +03003350 ret = -EBUSY;
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003351 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003352 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003353 spin_unlock_bh(&ar->data_lock);
3354
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003355 if (ret)
3356 goto exit;
3357
Kalle Valo5e3dd152013-06-12 20:52:10 +03003358 memset(&arg, 0, sizeof(arg));
3359 ath10k_wmi_start_scan_init(ar, &arg);
3360 arg.vdev_id = arvif->vdev_id;
3361 arg.scan_id = ATH10K_SCAN_ID;
3362
3363 if (!req->no_cck)
3364 arg.scan_ctrl_flags |= WMI_SCAN_ADD_CCK_RATES;
3365
3366 if (req->ie_len) {
3367 arg.ie_len = req->ie_len;
3368 memcpy(arg.ie, req->ie, arg.ie_len);
3369 }
3370
3371 if (req->n_ssids) {
3372 arg.n_ssids = req->n_ssids;
3373 for (i = 0; i < arg.n_ssids; i++) {
3374 arg.ssids[i].len = req->ssids[i].ssid_len;
3375 arg.ssids[i].ssid = req->ssids[i].ssid;
3376 }
Michal Kaziordcd4a562013-07-31 10:55:12 +02003377 } else {
3378 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003379 }
3380
3381 if (req->n_channels) {
3382 arg.n_channels = req->n_channels;
3383 for (i = 0; i < arg.n_channels; i++)
3384 arg.channels[i] = req->channels[i]->center_freq;
3385 }
3386
3387 ret = ath10k_start_scan(ar, &arg);
3388 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003389 ath10k_warn(ar, "failed to start hw scan: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003390 spin_lock_bh(&ar->data_lock);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003391 ar->scan.state = ATH10K_SCAN_IDLE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003392 spin_unlock_bh(&ar->data_lock);
3393 }
3394
3395exit:
3396 mutex_unlock(&ar->conf_mutex);
3397 return ret;
3398}
3399
3400static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
3401 struct ieee80211_vif *vif)
3402{
3403 struct ath10k *ar = hw->priv;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003404
3405 mutex_lock(&ar->conf_mutex);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003406 ath10k_scan_abort(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003407 mutex_unlock(&ar->conf_mutex);
Michal Kazior4eb2e162014-10-28 10:23:09 +01003408
3409 cancel_delayed_work_sync(&ar->scan.timeout);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003410}
3411
Michal Kaziorcfb27d22013-12-02 09:06:36 +01003412static void ath10k_set_key_h_def_keyidx(struct ath10k *ar,
3413 struct ath10k_vif *arvif,
3414 enum set_key_cmd cmd,
3415 struct ieee80211_key_conf *key)
3416{
3417 u32 vdev_param = arvif->ar->wmi.vdev_param->def_keyid;
3418 int ret;
3419
3420 /* 10.1 firmware branch requires default key index to be set to group
3421 * key index after installing it. Otherwise FW/HW Txes corrupted
3422 * frames with multi-vif APs. This is not required for main firmware
3423 * branch (e.g. 636).
3424 *
3425 * FIXME: This has been tested only in AP. It remains unknown if this
3426 * is required for multi-vif STA interfaces on 10.1 */
3427
3428 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
3429 return;
3430
3431 if (key->cipher == WLAN_CIPHER_SUITE_WEP40)
3432 return;
3433
3434 if (key->cipher == WLAN_CIPHER_SUITE_WEP104)
3435 return;
3436
3437 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3438 return;
3439
3440 if (cmd != SET_KEY)
3441 return;
3442
3443 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3444 key->keyidx);
3445 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003446 ath10k_warn(ar, "failed to set vdev %i group key as default key: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003447 arvif->vdev_id, ret);
Michal Kaziorcfb27d22013-12-02 09:06:36 +01003448}
3449
Kalle Valo5e3dd152013-06-12 20:52:10 +03003450static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
3451 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
3452 struct ieee80211_key_conf *key)
3453{
3454 struct ath10k *ar = hw->priv;
3455 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3456 struct ath10k_peer *peer;
3457 const u8 *peer_addr;
3458 bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3459 key->cipher == WLAN_CIPHER_SUITE_WEP104;
3460 int ret = 0;
3461
3462 if (key->keyidx > WMI_MAX_KEY_INDEX)
3463 return -ENOSPC;
3464
3465 mutex_lock(&ar->conf_mutex);
3466
3467 if (sta)
3468 peer_addr = sta->addr;
3469 else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
3470 peer_addr = vif->bss_conf.bssid;
3471 else
3472 peer_addr = vif->addr;
3473
3474 key->hw_key_idx = key->keyidx;
3475
3476 /* the peer should not disappear in mid-way (unless FW goes awry) since
3477 * we already hold conf_mutex. we just make sure its there now. */
3478 spin_lock_bh(&ar->data_lock);
3479 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
3480 spin_unlock_bh(&ar->data_lock);
3481
3482 if (!peer) {
3483 if (cmd == SET_KEY) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003484 ath10k_warn(ar, "failed to install key for non-existent peer %pM\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03003485 peer_addr);
3486 ret = -EOPNOTSUPP;
3487 goto exit;
3488 } else {
3489 /* if the peer doesn't exist there is no key to disable
3490 * anymore */
3491 goto exit;
3492 }
3493 }
3494
3495 if (is_wep) {
3496 if (cmd == SET_KEY)
3497 arvif->wep_keys[key->keyidx] = key;
3498 else
3499 arvif->wep_keys[key->keyidx] = NULL;
3500
3501 if (cmd == DISABLE_KEY)
3502 ath10k_clear_vdev_key(arvif, key);
3503 }
3504
3505 ret = ath10k_install_key(arvif, key, cmd, peer_addr);
3506 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003507 ath10k_warn(ar, "failed to install key for vdev %i peer %pM: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003508 arvif->vdev_id, peer_addr, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003509 goto exit;
3510 }
3511
Michal Kaziorcfb27d22013-12-02 09:06:36 +01003512 ath10k_set_key_h_def_keyidx(ar, arvif, cmd, key);
3513
Kalle Valo5e3dd152013-06-12 20:52:10 +03003514 spin_lock_bh(&ar->data_lock);
3515 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
3516 if (peer && cmd == SET_KEY)
3517 peer->keys[key->keyidx] = key;
3518 else if (peer && cmd == DISABLE_KEY)
3519 peer->keys[key->keyidx] = NULL;
3520 else if (peer == NULL)
3521 /* impossible unless FW goes crazy */
Michal Kazior7aa7a722014-08-25 12:09:38 +02003522 ath10k_warn(ar, "Peer %pM disappeared!\n", peer_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003523 spin_unlock_bh(&ar->data_lock);
3524
3525exit:
3526 mutex_unlock(&ar->conf_mutex);
3527 return ret;
3528}
3529
Michal Kazior9797feb2014-02-14 14:49:48 +01003530static void ath10k_sta_rc_update_wk(struct work_struct *wk)
3531{
3532 struct ath10k *ar;
3533 struct ath10k_vif *arvif;
3534 struct ath10k_sta *arsta;
3535 struct ieee80211_sta *sta;
3536 u32 changed, bw, nss, smps;
3537 int err;
3538
3539 arsta = container_of(wk, struct ath10k_sta, update_wk);
3540 sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
3541 arvif = arsta->arvif;
3542 ar = arvif->ar;
3543
3544 spin_lock_bh(&ar->data_lock);
3545
3546 changed = arsta->changed;
3547 arsta->changed = 0;
3548
3549 bw = arsta->bw;
3550 nss = arsta->nss;
3551 smps = arsta->smps;
3552
3553 spin_unlock_bh(&ar->data_lock);
3554
3555 mutex_lock(&ar->conf_mutex);
3556
3557 if (changed & IEEE80211_RC_BW_CHANGED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003558 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM peer bw %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003559 sta->addr, bw);
3560
3561 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
3562 WMI_PEER_CHAN_WIDTH, bw);
3563 if (err)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003564 ath10k_warn(ar, "failed to update STA %pM peer bw %d: %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003565 sta->addr, bw, err);
3566 }
3567
3568 if (changed & IEEE80211_RC_NSS_CHANGED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003569 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM nss %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003570 sta->addr, nss);
3571
3572 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
3573 WMI_PEER_NSS, nss);
3574 if (err)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003575 ath10k_warn(ar, "failed to update STA %pM nss %d: %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003576 sta->addr, nss, err);
3577 }
3578
3579 if (changed & IEEE80211_RC_SMPS_CHANGED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003580 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM smps %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003581 sta->addr, smps);
3582
3583 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
3584 WMI_PEER_SMPS_STATE, smps);
3585 if (err)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003586 ath10k_warn(ar, "failed to update STA %pM smps %d: %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003587 sta->addr, smps, err);
3588 }
3589
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02003590 if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003591 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM supp rates\n",
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02003592 sta->addr);
3593
Michal Kazior590922a2014-10-21 10:10:29 +03003594 err = ath10k_station_assoc(ar, arvif->vif, sta, true);
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02003595 if (err)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003596 ath10k_warn(ar, "failed to reassociate station: %pM\n",
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02003597 sta->addr);
3598 }
3599
Michal Kazior9797feb2014-02-14 14:49:48 +01003600 mutex_unlock(&ar->conf_mutex);
3601}
3602
Michal Kaziorcfd10612014-11-25 15:16:05 +01003603static int ath10k_mac_inc_num_stations(struct ath10k_vif *arvif)
3604{
3605 struct ath10k *ar = arvif->ar;
3606
3607 lockdep_assert_held(&ar->conf_mutex);
3608
3609 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
3610 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
3611 return 0;
3612
3613 if (ar->num_stations >= ar->max_num_stations)
3614 return -ENOBUFS;
3615
3616 ar->num_stations++;
3617
3618 return 0;
3619}
3620
3621static void ath10k_mac_dec_num_stations(struct ath10k_vif *arvif)
3622{
3623 struct ath10k *ar = arvif->ar;
3624
3625 lockdep_assert_held(&ar->conf_mutex);
3626
3627 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
3628 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
3629 return;
3630
3631 ar->num_stations--;
3632}
3633
Kalle Valo5e3dd152013-06-12 20:52:10 +03003634static int ath10k_sta_state(struct ieee80211_hw *hw,
3635 struct ieee80211_vif *vif,
3636 struct ieee80211_sta *sta,
3637 enum ieee80211_sta_state old_state,
3638 enum ieee80211_sta_state new_state)
3639{
3640 struct ath10k *ar = hw->priv;
3641 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Michal Kazior9797feb2014-02-14 14:49:48 +01003642 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003643 int ret = 0;
3644
Michal Kazior76f90022014-02-25 09:29:57 +02003645 if (old_state == IEEE80211_STA_NOTEXIST &&
3646 new_state == IEEE80211_STA_NONE) {
3647 memset(arsta, 0, sizeof(*arsta));
3648 arsta->arvif = arvif;
3649 INIT_WORK(&arsta->update_wk, ath10k_sta_rc_update_wk);
3650 }
3651
Michal Kazior9797feb2014-02-14 14:49:48 +01003652 /* cancel must be done outside the mutex to avoid deadlock */
3653 if ((old_state == IEEE80211_STA_NONE &&
3654 new_state == IEEE80211_STA_NOTEXIST))
3655 cancel_work_sync(&arsta->update_wk);
3656
Kalle Valo5e3dd152013-06-12 20:52:10 +03003657 mutex_lock(&ar->conf_mutex);
3658
3659 if (old_state == IEEE80211_STA_NOTEXIST &&
Michal Kazior077efc82014-10-21 10:10:29 +03003660 new_state == IEEE80211_STA_NONE) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03003661 /*
3662 * New station addition.
3663 */
Michal Kaziorcfd10612014-11-25 15:16:05 +01003664 ath10k_dbg(ar, ATH10K_DBG_MAC,
3665 "mac vdev %d peer create %pM (new sta) sta %d / %d peer %d / %d\n",
3666 arvif->vdev_id, sta->addr,
3667 ar->num_stations + 1, ar->max_num_stations,
3668 ar->num_peers + 1, ar->max_num_peers);
Bartosz Markowski0e759f32014-01-02 14:38:33 +01003669
Michal Kaziorcfd10612014-11-25 15:16:05 +01003670 ret = ath10k_mac_inc_num_stations(arvif);
3671 if (ret) {
3672 ath10k_warn(ar, "refusing to associate station: too many connected already (%d)\n",
3673 ar->max_num_stations);
Bartosz Markowski0e759f32014-01-02 14:38:33 +01003674 goto exit;
3675 }
3676
Kalle Valo5e3dd152013-06-12 20:52:10 +03003677 ret = ath10k_peer_create(ar, arvif->vdev_id, sta->addr);
Michal Kaziora52c0282014-11-25 15:16:03 +01003678 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003679 ath10k_warn(ar, "failed to add peer %pM for vdev %d when adding a new sta: %i\n",
Ben Greear479398b2013-11-04 09:19:34 -08003680 sta->addr, arvif->vdev_id, ret);
Michal Kaziorcfd10612014-11-25 15:16:05 +01003681 ath10k_mac_dec_num_stations(arvif);
Michal Kaziora52c0282014-11-25 15:16:03 +01003682 goto exit;
3683 }
Michal Kazior077efc82014-10-21 10:10:29 +03003684
3685 if (vif->type == NL80211_IFTYPE_STATION) {
3686 WARN_ON(arvif->is_started);
3687
3688 ret = ath10k_vdev_start(arvif);
3689 if (ret) {
3690 ath10k_warn(ar, "failed to start vdev %i: %d\n",
3691 arvif->vdev_id, ret);
3692 WARN_ON(ath10k_peer_delete(ar, arvif->vdev_id,
3693 sta->addr));
Michal Kaziorcfd10612014-11-25 15:16:05 +01003694 ath10k_mac_dec_num_stations(arvif);
Michal Kazior077efc82014-10-21 10:10:29 +03003695 goto exit;
3696 }
3697
3698 arvif->is_started = true;
3699 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003700 } else if ((old_state == IEEE80211_STA_NONE &&
3701 new_state == IEEE80211_STA_NOTEXIST)) {
3702 /*
3703 * Existing station deletion.
3704 */
Michal Kazior7aa7a722014-08-25 12:09:38 +02003705 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003706 "mac vdev %d peer delete %pM (sta gone)\n",
3707 arvif->vdev_id, sta->addr);
Michal Kazior077efc82014-10-21 10:10:29 +03003708
3709 if (vif->type == NL80211_IFTYPE_STATION) {
3710 WARN_ON(!arvif->is_started);
3711
3712 ret = ath10k_vdev_stop(arvif);
3713 if (ret)
3714 ath10k_warn(ar, "failed to stop vdev %i: %d\n",
3715 arvif->vdev_id, ret);
3716
3717 arvif->is_started = false;
3718 }
3719
Kalle Valo5e3dd152013-06-12 20:52:10 +03003720 ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
3721 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003722 ath10k_warn(ar, "failed to delete peer %pM for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003723 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003724
Michal Kaziorcfd10612014-11-25 15:16:05 +01003725 ath10k_mac_dec_num_stations(arvif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003726 } else if (old_state == IEEE80211_STA_AUTH &&
3727 new_state == IEEE80211_STA_ASSOC &&
3728 (vif->type == NL80211_IFTYPE_AP ||
3729 vif->type == NL80211_IFTYPE_ADHOC)) {
3730 /*
3731 * New association.
3732 */
Michal Kazior7aa7a722014-08-25 12:09:38 +02003733 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM associated\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03003734 sta->addr);
3735
Michal Kazior590922a2014-10-21 10:10:29 +03003736 ret = ath10k_station_assoc(ar, vif, sta, false);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003737 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003738 ath10k_warn(ar, "failed to associate station %pM for vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003739 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003740 } else if (old_state == IEEE80211_STA_ASSOC &&
3741 new_state == IEEE80211_STA_AUTH &&
3742 (vif->type == NL80211_IFTYPE_AP ||
3743 vif->type == NL80211_IFTYPE_ADHOC)) {
3744 /*
3745 * Disassociation.
3746 */
Michal Kazior7aa7a722014-08-25 12:09:38 +02003747 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM disassociated\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03003748 sta->addr);
3749
Michal Kazior590922a2014-10-21 10:10:29 +03003750 ret = ath10k_station_disassoc(ar, vif, sta);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003751 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003752 ath10k_warn(ar, "failed to disassociate station: %pM vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003753 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003754 }
Bartosz Markowski0e759f32014-01-02 14:38:33 +01003755exit:
Kalle Valo5e3dd152013-06-12 20:52:10 +03003756 mutex_unlock(&ar->conf_mutex);
3757 return ret;
3758}
3759
3760static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
Kalle Valo5b07e072014-09-14 12:50:06 +03003761 u16 ac, bool enable)
Kalle Valo5e3dd152013-06-12 20:52:10 +03003762{
3763 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3764 u32 value = 0;
3765 int ret = 0;
3766
Michal Kazior548db542013-07-05 16:15:15 +03003767 lockdep_assert_held(&ar->conf_mutex);
3768
Kalle Valo5e3dd152013-06-12 20:52:10 +03003769 if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
3770 return 0;
3771
3772 switch (ac) {
3773 case IEEE80211_AC_VO:
3774 value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
3775 WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
3776 break;
3777 case IEEE80211_AC_VI:
3778 value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
3779 WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
3780 break;
3781 case IEEE80211_AC_BE:
3782 value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
3783 WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
3784 break;
3785 case IEEE80211_AC_BK:
3786 value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
3787 WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
3788 break;
3789 }
3790
3791 if (enable)
3792 arvif->u.sta.uapsd |= value;
3793 else
3794 arvif->u.sta.uapsd &= ~value;
3795
3796 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3797 WMI_STA_PS_PARAM_UAPSD,
3798 arvif->u.sta.uapsd);
3799 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003800 ath10k_warn(ar, "failed to set uapsd params: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003801 goto exit;
3802 }
3803
3804 if (arvif->u.sta.uapsd)
3805 value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
3806 else
3807 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
3808
3809 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3810 WMI_STA_PS_PARAM_RX_WAKE_POLICY,
3811 value);
3812 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003813 ath10k_warn(ar, "failed to set rx wake param: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003814
3815exit:
3816 return ret;
3817}
3818
3819static int ath10k_conf_tx(struct ieee80211_hw *hw,
3820 struct ieee80211_vif *vif, u16 ac,
3821 const struct ieee80211_tx_queue_params *params)
3822{
3823 struct ath10k *ar = hw->priv;
3824 struct wmi_wmm_params_arg *p = NULL;
3825 int ret;
3826
3827 mutex_lock(&ar->conf_mutex);
3828
3829 switch (ac) {
3830 case IEEE80211_AC_VO:
3831 p = &ar->wmm_params.ac_vo;
3832 break;
3833 case IEEE80211_AC_VI:
3834 p = &ar->wmm_params.ac_vi;
3835 break;
3836 case IEEE80211_AC_BE:
3837 p = &ar->wmm_params.ac_be;
3838 break;
3839 case IEEE80211_AC_BK:
3840 p = &ar->wmm_params.ac_bk;
3841 break;
3842 }
3843
3844 if (WARN_ON(!p)) {
3845 ret = -EINVAL;
3846 goto exit;
3847 }
3848
3849 p->cwmin = params->cw_min;
3850 p->cwmax = params->cw_max;
3851 p->aifs = params->aifs;
3852
3853 /*
3854 * The channel time duration programmed in the HW is in absolute
3855 * microseconds, while mac80211 gives the txop in units of
3856 * 32 microseconds.
3857 */
3858 p->txop = params->txop * 32;
3859
3860 /* FIXME: FW accepts wmm params per hw, not per vif */
3861 ret = ath10k_wmi_pdev_set_wmm_params(ar, &ar->wmm_params);
3862 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003863 ath10k_warn(ar, "failed to set wmm params: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003864 goto exit;
3865 }
3866
3867 ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
3868 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003869 ath10k_warn(ar, "failed to set sta uapsd: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003870
3871exit:
3872 mutex_unlock(&ar->conf_mutex);
3873 return ret;
3874}
3875
3876#define ATH10K_ROC_TIMEOUT_HZ (2*HZ)
3877
3878static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
3879 struct ieee80211_vif *vif,
3880 struct ieee80211_channel *chan,
3881 int duration,
3882 enum ieee80211_roc_type type)
3883{
3884 struct ath10k *ar = hw->priv;
3885 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3886 struct wmi_start_scan_arg arg;
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003887 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003888
3889 mutex_lock(&ar->conf_mutex);
3890
3891 spin_lock_bh(&ar->data_lock);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003892 switch (ar->scan.state) {
3893 case ATH10K_SCAN_IDLE:
3894 reinit_completion(&ar->scan.started);
3895 reinit_completion(&ar->scan.completed);
3896 reinit_completion(&ar->scan.on_channel);
3897 ar->scan.state = ATH10K_SCAN_STARTING;
3898 ar->scan.is_roc = true;
3899 ar->scan.vdev_id = arvif->vdev_id;
3900 ar->scan.roc_freq = chan->center_freq;
3901 ret = 0;
3902 break;
3903 case ATH10K_SCAN_STARTING:
3904 case ATH10K_SCAN_RUNNING:
3905 case ATH10K_SCAN_ABORTING:
Kalle Valo5e3dd152013-06-12 20:52:10 +03003906 ret = -EBUSY;
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003907 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003908 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003909 spin_unlock_bh(&ar->data_lock);
3910
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003911 if (ret)
3912 goto exit;
3913
Michal Kaziordcca0bd2014-11-24 14:58:32 +01003914 duration = max(duration, WMI_SCAN_CHAN_MIN_TIME_MSEC);
3915
Kalle Valo5e3dd152013-06-12 20:52:10 +03003916 memset(&arg, 0, sizeof(arg));
3917 ath10k_wmi_start_scan_init(ar, &arg);
3918 arg.vdev_id = arvif->vdev_id;
3919 arg.scan_id = ATH10K_SCAN_ID;
3920 arg.n_channels = 1;
3921 arg.channels[0] = chan->center_freq;
3922 arg.dwell_time_active = duration;
3923 arg.dwell_time_passive = duration;
3924 arg.max_scan_time = 2 * duration;
3925 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
3926 arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
3927
3928 ret = ath10k_start_scan(ar, &arg);
3929 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003930 ath10k_warn(ar, "failed to start roc scan: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003931 spin_lock_bh(&ar->data_lock);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003932 ar->scan.state = ATH10K_SCAN_IDLE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003933 spin_unlock_bh(&ar->data_lock);
3934 goto exit;
3935 }
3936
3937 ret = wait_for_completion_timeout(&ar->scan.on_channel, 3*HZ);
3938 if (ret == 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003939 ath10k_warn(ar, "failed to switch to channel for roc scan\n");
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003940
3941 ret = ath10k_scan_stop(ar);
3942 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003943 ath10k_warn(ar, "failed to stop scan: %d\n", ret);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003944
Kalle Valo5e3dd152013-06-12 20:52:10 +03003945 ret = -ETIMEDOUT;
3946 goto exit;
3947 }
3948
3949 ret = 0;
3950exit:
3951 mutex_unlock(&ar->conf_mutex);
3952 return ret;
3953}
3954
3955static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw)
3956{
3957 struct ath10k *ar = hw->priv;
3958
3959 mutex_lock(&ar->conf_mutex);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003960 ath10k_scan_abort(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003961 mutex_unlock(&ar->conf_mutex);
3962
Michal Kazior4eb2e162014-10-28 10:23:09 +01003963 cancel_delayed_work_sync(&ar->scan.timeout);
3964
Kalle Valo5e3dd152013-06-12 20:52:10 +03003965 return 0;
3966}
3967
3968/*
3969 * Both RTS and Fragmentation threshold are interface-specific
3970 * in ath10k, but device-specific in mac80211.
3971 */
Kalle Valo5e3dd152013-06-12 20:52:10 +03003972
3973static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3974{
Kalle Valo5e3dd152013-06-12 20:52:10 +03003975 struct ath10k *ar = hw->priv;
Michal Kaziorad088bf2013-10-16 15:44:46 +03003976 struct ath10k_vif *arvif;
3977 int ret = 0;
Michal Kazior548db542013-07-05 16:15:15 +03003978
Michal Kaziorad088bf2013-10-16 15:44:46 +03003979 mutex_lock(&ar->conf_mutex);
3980 list_for_each_entry(arvif, &ar->arvifs, list) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003981 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
Michal Kaziorad088bf2013-10-16 15:44:46 +03003982 arvif->vdev_id, value);
Kalle Valo60c3daa2013-09-08 17:56:07 +03003983
Michal Kaziorad088bf2013-10-16 15:44:46 +03003984 ret = ath10k_mac_set_rts(arvif, value);
3985 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003986 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
Michal Kaziorad088bf2013-10-16 15:44:46 +03003987 arvif->vdev_id, ret);
3988 break;
3989 }
3990 }
3991 mutex_unlock(&ar->conf_mutex);
3992
3993 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003994}
3995
3996static int ath10k_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
3997{
Kalle Valo5e3dd152013-06-12 20:52:10 +03003998 struct ath10k *ar = hw->priv;
Michal Kaziorad088bf2013-10-16 15:44:46 +03003999 struct ath10k_vif *arvif;
4000 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004001
Kalle Valo5e3dd152013-06-12 20:52:10 +03004002 mutex_lock(&ar->conf_mutex);
Michal Kaziorad088bf2013-10-16 15:44:46 +03004003 list_for_each_entry(arvif, &ar->arvifs, list) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004004 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d fragmentation threshold %d\n",
Michal Kaziorad088bf2013-10-16 15:44:46 +03004005 arvif->vdev_id, value);
4006
Michal Kazior56a0dee2014-10-23 17:04:29 +03004007 ret = ath10k_mac_set_frag(arvif, value);
Michal Kaziorad088bf2013-10-16 15:44:46 +03004008 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004009 ath10k_warn(ar, "failed to set fragmentation threshold for vdev %d: %d\n",
Michal Kaziorad088bf2013-10-16 15:44:46 +03004010 arvif->vdev_id, ret);
4011 break;
4012 }
4013 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03004014 mutex_unlock(&ar->conf_mutex);
4015
Michal Kaziorad088bf2013-10-16 15:44:46 +03004016 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004017}
4018
Emmanuel Grumbach77be2c52014-03-27 11:30:29 +02004019static void ath10k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4020 u32 queues, bool drop)
Kalle Valo5e3dd152013-06-12 20:52:10 +03004021{
4022 struct ath10k *ar = hw->priv;
Michal Kazioraffd3212013-07-16 09:54:35 +02004023 bool skip;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004024 int ret;
4025
4026 /* mac80211 doesn't care if we really xmit queued frames or not
4027 * we'll collect those frames either way if we stop/delete vdevs */
4028 if (drop)
4029 return;
4030
Michal Kazior548db542013-07-05 16:15:15 +03004031 mutex_lock(&ar->conf_mutex);
4032
Michal Kazioraffd3212013-07-16 09:54:35 +02004033 if (ar->state == ATH10K_STATE_WEDGED)
4034 goto skip;
4035
Michal Kazioredb82362013-07-05 16:15:14 +03004036 ret = wait_event_timeout(ar->htt.empty_tx_wq, ({
Kalle Valo5e3dd152013-06-12 20:52:10 +03004037 bool empty;
Michal Kazioraffd3212013-07-16 09:54:35 +02004038
Michal Kazioredb82362013-07-05 16:15:14 +03004039 spin_lock_bh(&ar->htt.tx_lock);
Michal Kazior0945baf2013-09-18 14:43:18 +02004040 empty = (ar->htt.num_pending_tx == 0);
Michal Kazioredb82362013-07-05 16:15:14 +03004041 spin_unlock_bh(&ar->htt.tx_lock);
Michal Kazioraffd3212013-07-16 09:54:35 +02004042
Michal Kazior7962b0d2014-10-28 10:34:38 +01004043 skip = (ar->state == ATH10K_STATE_WEDGED) ||
4044 test_bit(ATH10K_FLAG_CRASH_FLUSH,
4045 &ar->dev_flags);
Michal Kazioraffd3212013-07-16 09:54:35 +02004046
4047 (empty || skip);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004048 }), ATH10K_FLUSH_TIMEOUT_HZ);
Michal Kazioraffd3212013-07-16 09:54:35 +02004049
4050 if (ret <= 0 || skip)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004051 ath10k_warn(ar, "failed to flush transmit queue (skip %i ar-state %i): %i\n",
Ben Greear9ba4c782014-02-25 09:29:57 +02004052 skip, ar->state, ret);
Michal Kazior548db542013-07-05 16:15:15 +03004053
Michal Kazioraffd3212013-07-16 09:54:35 +02004054skip:
Michal Kazior548db542013-07-05 16:15:15 +03004055 mutex_unlock(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004056}
4057
4058/* TODO: Implement this function properly
4059 * For now it is needed to reply to Probe Requests in IBSS mode.
4060 * Propably we need this information from FW.
4061 */
4062static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
4063{
4064 return 1;
4065}
4066
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004067#ifdef CONFIG_PM
4068static int ath10k_suspend(struct ieee80211_hw *hw,
4069 struct cfg80211_wowlan *wowlan)
4070{
4071 struct ath10k *ar = hw->priv;
4072 int ret;
4073
Marek Puzyniak9042e172014-02-10 17:14:23 +01004074 mutex_lock(&ar->conf_mutex);
4075
Marek Puzyniak00f54822014-02-10 17:14:24 +01004076 ret = ath10k_wait_for_suspend(ar, WMI_PDEV_SUSPEND);
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004077 if (ret) {
Marek Puzyniak00f54822014-02-10 17:14:24 +01004078 if (ret == -ETIMEDOUT)
4079 goto resume;
Marek Puzyniak9042e172014-02-10 17:14:23 +01004080 ret = 1;
4081 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004082 }
4083
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004084 ret = ath10k_hif_suspend(ar);
4085 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004086 ath10k_warn(ar, "failed to suspend hif: %d\n", ret);
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004087 goto resume;
4088 }
4089
Marek Puzyniak9042e172014-02-10 17:14:23 +01004090 ret = 0;
4091 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004092resume:
4093 ret = ath10k_wmi_pdev_resume_target(ar);
4094 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004095 ath10k_warn(ar, "failed to resume target: %d\n", ret);
Marek Puzyniak9042e172014-02-10 17:14:23 +01004096
4097 ret = 1;
4098exit:
4099 mutex_unlock(&ar->conf_mutex);
4100 return ret;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004101}
4102
4103static int ath10k_resume(struct ieee80211_hw *hw)
4104{
4105 struct ath10k *ar = hw->priv;
4106 int ret;
4107
Marek Puzyniak9042e172014-02-10 17:14:23 +01004108 mutex_lock(&ar->conf_mutex);
4109
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004110 ret = ath10k_hif_resume(ar);
4111 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004112 ath10k_warn(ar, "failed to resume hif: %d\n", ret);
Marek Puzyniak9042e172014-02-10 17:14:23 +01004113 ret = 1;
4114 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004115 }
4116
4117 ret = ath10k_wmi_pdev_resume_target(ar);
4118 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004119 ath10k_warn(ar, "failed to resume target: %d\n", ret);
Marek Puzyniak9042e172014-02-10 17:14:23 +01004120 ret = 1;
4121 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004122 }
4123
Marek Puzyniak9042e172014-02-10 17:14:23 +01004124 ret = 0;
4125exit:
4126 mutex_unlock(&ar->conf_mutex);
4127 return ret;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004128}
4129#endif
4130
Eliad Pellercf2c92d2014-11-04 11:43:54 +02004131static void ath10k_reconfig_complete(struct ieee80211_hw *hw,
4132 enum ieee80211_reconfig_type reconfig_type)
Michal Kazioraffd3212013-07-16 09:54:35 +02004133{
4134 struct ath10k *ar = hw->priv;
4135
Eliad Pellercf2c92d2014-11-04 11:43:54 +02004136 if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
4137 return;
4138
Michal Kazioraffd3212013-07-16 09:54:35 +02004139 mutex_lock(&ar->conf_mutex);
4140
4141 /* If device failed to restart it will be in a different state, e.g.
4142 * ATH10K_STATE_WEDGED */
4143 if (ar->state == ATH10K_STATE_RESTARTED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004144 ath10k_info(ar, "device successfully recovered\n");
Michal Kazioraffd3212013-07-16 09:54:35 +02004145 ar->state = ATH10K_STATE_ON;
Michal Kazior7962b0d2014-10-28 10:34:38 +01004146 ieee80211_wake_queues(ar->hw);
Michal Kazioraffd3212013-07-16 09:54:35 +02004147 }
4148
4149 mutex_unlock(&ar->conf_mutex);
4150}
4151
Michal Kazior2e1dea42013-07-31 10:32:40 +02004152static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
4153 struct survey_info *survey)
4154{
4155 struct ath10k *ar = hw->priv;
4156 struct ieee80211_supported_band *sband;
4157 struct survey_info *ar_survey = &ar->survey[idx];
4158 int ret = 0;
4159
4160 mutex_lock(&ar->conf_mutex);
4161
4162 sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
4163 if (sband && idx >= sband->n_channels) {
4164 idx -= sband->n_channels;
4165 sband = NULL;
4166 }
4167
4168 if (!sband)
4169 sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
4170
4171 if (!sband || idx >= sband->n_channels) {
4172 ret = -ENOENT;
4173 goto exit;
4174 }
4175
4176 spin_lock_bh(&ar->data_lock);
4177 memcpy(survey, ar_survey, sizeof(*survey));
4178 spin_unlock_bh(&ar->data_lock);
4179
4180 survey->channel = &sband->channels[idx];
4181
Felix Fietkaufa1d4df2014-10-23 17:04:28 +03004182 if (ar->rx_channel == survey->channel)
4183 survey->filled |= SURVEY_INFO_IN_USE;
4184
Michal Kazior2e1dea42013-07-31 10:32:40 +02004185exit:
4186 mutex_unlock(&ar->conf_mutex);
4187 return ret;
4188}
4189
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004190/* Helper table for legacy fixed_rate/bitrate_mask */
4191static const u8 cck_ofdm_rate[] = {
4192 /* CCK */
4193 3, /* 1Mbps */
4194 2, /* 2Mbps */
4195 1, /* 5.5Mbps */
4196 0, /* 11Mbps */
4197 /* OFDM */
4198 3, /* 6Mbps */
4199 7, /* 9Mbps */
4200 2, /* 12Mbps */
4201 6, /* 18Mbps */
4202 1, /* 24Mbps */
4203 5, /* 36Mbps */
4204 0, /* 48Mbps */
4205 4, /* 54Mbps */
4206};
4207
4208/* Check if only one bit set */
4209static int ath10k_check_single_mask(u32 mask)
4210{
4211 int bit;
4212
4213 bit = ffs(mask);
4214 if (!bit)
4215 return 0;
4216
4217 mask &= ~BIT(bit - 1);
4218 if (mask)
4219 return 2;
4220
4221 return 1;
4222}
4223
4224static bool
4225ath10k_default_bitrate_mask(struct ath10k *ar,
4226 enum ieee80211_band band,
4227 const struct cfg80211_bitrate_mask *mask)
4228{
4229 u32 legacy = 0x00ff;
4230 u8 ht = 0xff, i;
4231 u16 vht = 0x3ff;
Ben Greearb116ea12014-11-24 16:22:10 +02004232 u16 nrf = ar->num_rf_chains;
4233
4234 if (ar->cfg_tx_chainmask)
4235 nrf = get_nss_from_chainmask(ar->cfg_tx_chainmask);
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004236
4237 switch (band) {
4238 case IEEE80211_BAND_2GHZ:
4239 legacy = 0x00fff;
4240 vht = 0;
4241 break;
4242 case IEEE80211_BAND_5GHZ:
4243 break;
4244 default:
4245 return false;
4246 }
4247
4248 if (mask->control[band].legacy != legacy)
4249 return false;
4250
Ben Greearb116ea12014-11-24 16:22:10 +02004251 for (i = 0; i < nrf; i++)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004252 if (mask->control[band].ht_mcs[i] != ht)
4253 return false;
4254
Ben Greearb116ea12014-11-24 16:22:10 +02004255 for (i = 0; i < nrf; i++)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004256 if (mask->control[band].vht_mcs[i] != vht)
4257 return false;
4258
4259 return true;
4260}
4261
4262static bool
4263ath10k_bitrate_mask_nss(const struct cfg80211_bitrate_mask *mask,
4264 enum ieee80211_band band,
4265 u8 *fixed_nss)
4266{
4267 int ht_nss = 0, vht_nss = 0, i;
4268
4269 /* check legacy */
4270 if (ath10k_check_single_mask(mask->control[band].legacy))
4271 return false;
4272
4273 /* check HT */
4274 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
4275 if (mask->control[band].ht_mcs[i] == 0xff)
4276 continue;
4277 else if (mask->control[band].ht_mcs[i] == 0x00)
4278 break;
Kalle Valod8bb26b2014-09-14 12:50:33 +03004279
4280 return false;
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004281 }
4282
4283 ht_nss = i;
4284
4285 /* check VHT */
4286 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
4287 if (mask->control[band].vht_mcs[i] == 0x03ff)
4288 continue;
4289 else if (mask->control[band].vht_mcs[i] == 0x0000)
4290 break;
Kalle Valod8bb26b2014-09-14 12:50:33 +03004291
4292 return false;
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004293 }
4294
4295 vht_nss = i;
4296
4297 if (ht_nss > 0 && vht_nss > 0)
4298 return false;
4299
4300 if (ht_nss)
4301 *fixed_nss = ht_nss;
4302 else if (vht_nss)
4303 *fixed_nss = vht_nss;
4304 else
4305 return false;
4306
4307 return true;
4308}
4309
4310static bool
4311ath10k_bitrate_mask_correct(const struct cfg80211_bitrate_mask *mask,
4312 enum ieee80211_band band,
4313 enum wmi_rate_preamble *preamble)
4314{
4315 int legacy = 0, ht = 0, vht = 0, i;
4316
4317 *preamble = WMI_RATE_PREAMBLE_OFDM;
4318
4319 /* check legacy */
4320 legacy = ath10k_check_single_mask(mask->control[band].legacy);
4321 if (legacy > 1)
4322 return false;
4323
4324 /* check HT */
4325 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
4326 ht += ath10k_check_single_mask(mask->control[band].ht_mcs[i]);
4327 if (ht > 1)
4328 return false;
4329
4330 /* check VHT */
4331 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
4332 vht += ath10k_check_single_mask(mask->control[band].vht_mcs[i]);
4333 if (vht > 1)
4334 return false;
4335
4336 /* Currently we support only one fixed_rate */
4337 if ((legacy + ht + vht) != 1)
4338 return false;
4339
4340 if (ht)
4341 *preamble = WMI_RATE_PREAMBLE_HT;
4342 else if (vht)
4343 *preamble = WMI_RATE_PREAMBLE_VHT;
4344
4345 return true;
4346}
4347
4348static bool
Michal Kazior7aa7a722014-08-25 12:09:38 +02004349ath10k_bitrate_mask_rate(struct ath10k *ar,
4350 const struct cfg80211_bitrate_mask *mask,
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004351 enum ieee80211_band band,
4352 u8 *fixed_rate,
4353 u8 *fixed_nss)
4354{
4355 u8 rate = 0, pream = 0, nss = 0, i;
4356 enum wmi_rate_preamble preamble;
4357
4358 /* Check if single rate correct */
4359 if (!ath10k_bitrate_mask_correct(mask, band, &preamble))
4360 return false;
4361
4362 pream = preamble;
4363
4364 switch (preamble) {
4365 case WMI_RATE_PREAMBLE_CCK:
4366 case WMI_RATE_PREAMBLE_OFDM:
4367 i = ffs(mask->control[band].legacy) - 1;
4368
4369 if (band == IEEE80211_BAND_2GHZ && i < 4)
4370 pream = WMI_RATE_PREAMBLE_CCK;
4371
4372 if (band == IEEE80211_BAND_5GHZ)
4373 i += 4;
4374
4375 if (i >= ARRAY_SIZE(cck_ofdm_rate))
4376 return false;
4377
4378 rate = cck_ofdm_rate[i];
4379 break;
4380 case WMI_RATE_PREAMBLE_HT:
4381 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
4382 if (mask->control[band].ht_mcs[i])
4383 break;
4384
4385 if (i == IEEE80211_HT_MCS_MASK_LEN)
4386 return false;
4387
4388 rate = ffs(mask->control[band].ht_mcs[i]) - 1;
4389 nss = i;
4390 break;
4391 case WMI_RATE_PREAMBLE_VHT:
4392 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
4393 if (mask->control[band].vht_mcs[i])
4394 break;
4395
4396 if (i == NL80211_VHT_NSS_MAX)
4397 return false;
4398
4399 rate = ffs(mask->control[band].vht_mcs[i]) - 1;
4400 nss = i;
4401 break;
4402 }
4403
4404 *fixed_nss = nss + 1;
4405 nss <<= 4;
4406 pream <<= 6;
4407
Michal Kazior7aa7a722014-08-25 12:09:38 +02004408 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac fixed rate pream 0x%02x nss 0x%02x rate 0x%02x\n",
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004409 pream, nss, rate);
4410
4411 *fixed_rate = pream | nss | rate;
4412
4413 return true;
4414}
4415
Michal Kazior7aa7a722014-08-25 12:09:38 +02004416static bool ath10k_get_fixed_rate_nss(struct ath10k *ar,
4417 const struct cfg80211_bitrate_mask *mask,
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004418 enum ieee80211_band band,
4419 u8 *fixed_rate,
4420 u8 *fixed_nss)
4421{
4422 /* First check full NSS mask, if we can simply limit NSS */
4423 if (ath10k_bitrate_mask_nss(mask, band, fixed_nss))
4424 return true;
4425
4426 /* Next Check single rate is set */
Michal Kazior7aa7a722014-08-25 12:09:38 +02004427 return ath10k_bitrate_mask_rate(ar, mask, band, fixed_rate, fixed_nss);
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004428}
4429
4430static int ath10k_set_fixed_rate_param(struct ath10k_vif *arvif,
4431 u8 fixed_rate,
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004432 u8 fixed_nss,
4433 u8 force_sgi)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004434{
4435 struct ath10k *ar = arvif->ar;
4436 u32 vdev_param;
4437 int ret = 0;
4438
4439 mutex_lock(&ar->conf_mutex);
4440
4441 if (arvif->fixed_rate == fixed_rate &&
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004442 arvif->fixed_nss == fixed_nss &&
4443 arvif->force_sgi == force_sgi)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004444 goto exit;
4445
4446 if (fixed_rate == WMI_FIXED_RATE_NONE)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004447 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac disable fixed bitrate mask\n");
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004448
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004449 if (force_sgi)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004450 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac force sgi\n");
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004451
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004452 vdev_param = ar->wmi.vdev_param->fixed_rate;
4453 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
4454 vdev_param, fixed_rate);
4455 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004456 ath10k_warn(ar, "failed to set fixed rate param 0x%02x: %d\n",
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004457 fixed_rate, ret);
4458 ret = -EINVAL;
4459 goto exit;
4460 }
4461
4462 arvif->fixed_rate = fixed_rate;
4463
4464 vdev_param = ar->wmi.vdev_param->nss;
4465 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
4466 vdev_param, fixed_nss);
4467
4468 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004469 ath10k_warn(ar, "failed to set fixed nss param %d: %d\n",
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004470 fixed_nss, ret);
4471 ret = -EINVAL;
4472 goto exit;
4473 }
4474
4475 arvif->fixed_nss = fixed_nss;
4476
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004477 vdev_param = ar->wmi.vdev_param->sgi;
4478 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
4479 force_sgi);
4480
4481 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004482 ath10k_warn(ar, "failed to set sgi param %d: %d\n",
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004483 force_sgi, ret);
4484 ret = -EINVAL;
4485 goto exit;
4486 }
4487
4488 arvif->force_sgi = force_sgi;
4489
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004490exit:
4491 mutex_unlock(&ar->conf_mutex);
4492 return ret;
4493}
4494
4495static int ath10k_set_bitrate_mask(struct ieee80211_hw *hw,
4496 struct ieee80211_vif *vif,
4497 const struct cfg80211_bitrate_mask *mask)
4498{
4499 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4500 struct ath10k *ar = arvif->ar;
4501 enum ieee80211_band band = ar->hw->conf.chandef.chan->band;
4502 u8 fixed_rate = WMI_FIXED_RATE_NONE;
4503 u8 fixed_nss = ar->num_rf_chains;
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004504 u8 force_sgi;
4505
Ben Greearb116ea12014-11-24 16:22:10 +02004506 if (ar->cfg_tx_chainmask)
4507 fixed_nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
4508
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004509 force_sgi = mask->control[band].gi;
4510 if (force_sgi == NL80211_TXRATE_FORCE_LGI)
4511 return -EINVAL;
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004512
4513 if (!ath10k_default_bitrate_mask(ar, band, mask)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004514 if (!ath10k_get_fixed_rate_nss(ar, mask, band,
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004515 &fixed_rate,
4516 &fixed_nss))
4517 return -EINVAL;
4518 }
4519
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004520 if (fixed_rate == WMI_FIXED_RATE_NONE && force_sgi) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004521 ath10k_warn(ar, "failed to force SGI usage for default rate settings\n");
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004522 return -EINVAL;
4523 }
4524
4525 return ath10k_set_fixed_rate_param(arvif, fixed_rate,
4526 fixed_nss, force_sgi);
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004527}
4528
Michal Kazior9797feb2014-02-14 14:49:48 +01004529static void ath10k_sta_rc_update(struct ieee80211_hw *hw,
4530 struct ieee80211_vif *vif,
4531 struct ieee80211_sta *sta,
4532 u32 changed)
4533{
4534 struct ath10k *ar = hw->priv;
4535 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
4536 u32 bw, smps;
4537
4538 spin_lock_bh(&ar->data_lock);
4539
Michal Kazior7aa7a722014-08-25 12:09:38 +02004540 ath10k_dbg(ar, ATH10K_DBG_MAC,
Michal Kazior9797feb2014-02-14 14:49:48 +01004541 "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
4542 sta->addr, changed, sta->bandwidth, sta->rx_nss,
4543 sta->smps_mode);
4544
4545 if (changed & IEEE80211_RC_BW_CHANGED) {
4546 bw = WMI_PEER_CHWIDTH_20MHZ;
4547
4548 switch (sta->bandwidth) {
4549 case IEEE80211_STA_RX_BW_20:
4550 bw = WMI_PEER_CHWIDTH_20MHZ;
4551 break;
4552 case IEEE80211_STA_RX_BW_40:
4553 bw = WMI_PEER_CHWIDTH_40MHZ;
4554 break;
4555 case IEEE80211_STA_RX_BW_80:
4556 bw = WMI_PEER_CHWIDTH_80MHZ;
4557 break;
4558 case IEEE80211_STA_RX_BW_160:
Michal Kazior7aa7a722014-08-25 12:09:38 +02004559 ath10k_warn(ar, "Invalid bandwith %d in rc update for %pM\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02004560 sta->bandwidth, sta->addr);
Michal Kazior9797feb2014-02-14 14:49:48 +01004561 bw = WMI_PEER_CHWIDTH_20MHZ;
4562 break;
4563 }
4564
4565 arsta->bw = bw;
4566 }
4567
4568 if (changed & IEEE80211_RC_NSS_CHANGED)
4569 arsta->nss = sta->rx_nss;
4570
4571 if (changed & IEEE80211_RC_SMPS_CHANGED) {
4572 smps = WMI_PEER_SMPS_PS_NONE;
4573
4574 switch (sta->smps_mode) {
4575 case IEEE80211_SMPS_AUTOMATIC:
4576 case IEEE80211_SMPS_OFF:
4577 smps = WMI_PEER_SMPS_PS_NONE;
4578 break;
4579 case IEEE80211_SMPS_STATIC:
4580 smps = WMI_PEER_SMPS_STATIC;
4581 break;
4582 case IEEE80211_SMPS_DYNAMIC:
4583 smps = WMI_PEER_SMPS_DYNAMIC;
4584 break;
4585 case IEEE80211_SMPS_NUM_MODES:
Michal Kazior7aa7a722014-08-25 12:09:38 +02004586 ath10k_warn(ar, "Invalid smps %d in sta rc update for %pM\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02004587 sta->smps_mode, sta->addr);
Michal Kazior9797feb2014-02-14 14:49:48 +01004588 smps = WMI_PEER_SMPS_PS_NONE;
4589 break;
4590 }
4591
4592 arsta->smps = smps;
4593 }
4594
Michal Kazior9797feb2014-02-14 14:49:48 +01004595 arsta->changed |= changed;
4596
4597 spin_unlock_bh(&ar->data_lock);
4598
4599 ieee80211_queue_work(hw, &arsta->update_wk);
4600}
4601
Chun-Yeow Yeoh26ebbcc2014-02-25 09:29:54 +02004602static u64 ath10k_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
4603{
4604 /*
4605 * FIXME: Return 0 for time being. Need to figure out whether FW
4606 * has the API to fetch 64-bit local TSF
4607 */
4608
4609 return 0;
4610}
4611
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02004612static int ath10k_ampdu_action(struct ieee80211_hw *hw,
4613 struct ieee80211_vif *vif,
4614 enum ieee80211_ampdu_mlme_action action,
4615 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
4616 u8 buf_size)
4617{
Michal Kazior7aa7a722014-08-25 12:09:38 +02004618 struct ath10k *ar = hw->priv;
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02004619 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4620
Michal Kazior7aa7a722014-08-25 12:09:38 +02004621 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ampdu vdev_id %i sta %pM tid %hu action %d\n",
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02004622 arvif->vdev_id, sta->addr, tid, action);
4623
4624 switch (action) {
4625 case IEEE80211_AMPDU_RX_START:
4626 case IEEE80211_AMPDU_RX_STOP:
4627 /* HTT AddBa/DelBa events trigger mac80211 Rx BA session
4628 * creation/removal. Do we need to verify this?
4629 */
4630 return 0;
4631 case IEEE80211_AMPDU_TX_START:
4632 case IEEE80211_AMPDU_TX_STOP_CONT:
4633 case IEEE80211_AMPDU_TX_STOP_FLUSH:
4634 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
4635 case IEEE80211_AMPDU_TX_OPERATIONAL:
4636 /* Firmware offloads Tx aggregation entirely so deny mac80211
4637 * Tx aggregation requests.
4638 */
4639 return -EOPNOTSUPP;
4640 }
4641
4642 return -EINVAL;
4643}
4644
Kalle Valo5e3dd152013-06-12 20:52:10 +03004645static const struct ieee80211_ops ath10k_ops = {
4646 .tx = ath10k_tx,
4647 .start = ath10k_start,
4648 .stop = ath10k_stop,
4649 .config = ath10k_config,
4650 .add_interface = ath10k_add_interface,
4651 .remove_interface = ath10k_remove_interface,
4652 .configure_filter = ath10k_configure_filter,
4653 .bss_info_changed = ath10k_bss_info_changed,
4654 .hw_scan = ath10k_hw_scan,
4655 .cancel_hw_scan = ath10k_cancel_hw_scan,
4656 .set_key = ath10k_set_key,
4657 .sta_state = ath10k_sta_state,
4658 .conf_tx = ath10k_conf_tx,
4659 .remain_on_channel = ath10k_remain_on_channel,
4660 .cancel_remain_on_channel = ath10k_cancel_remain_on_channel,
4661 .set_rts_threshold = ath10k_set_rts_threshold,
4662 .set_frag_threshold = ath10k_set_frag_threshold,
4663 .flush = ath10k_flush,
4664 .tx_last_beacon = ath10k_tx_last_beacon,
Ben Greear46acf7b2014-05-16 17:15:38 +03004665 .set_antenna = ath10k_set_antenna,
4666 .get_antenna = ath10k_get_antenna,
Eliad Pellercf2c92d2014-11-04 11:43:54 +02004667 .reconfig_complete = ath10k_reconfig_complete,
Michal Kazior2e1dea42013-07-31 10:32:40 +02004668 .get_survey = ath10k_get_survey,
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004669 .set_bitrate_mask = ath10k_set_bitrate_mask,
Michal Kazior9797feb2014-02-14 14:49:48 +01004670 .sta_rc_update = ath10k_sta_rc_update,
Chun-Yeow Yeoh26ebbcc2014-02-25 09:29:54 +02004671 .get_tsf = ath10k_get_tsf,
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02004672 .ampdu_action = ath10k_ampdu_action,
Ben Greear6cddcc72014-09-29 14:41:46 +03004673 .get_et_sset_count = ath10k_debug_get_et_sset_count,
4674 .get_et_stats = ath10k_debug_get_et_stats,
4675 .get_et_strings = ath10k_debug_get_et_strings,
Kalle Valo43d2a302014-09-10 18:23:30 +03004676
4677 CFG80211_TESTMODE_CMD(ath10k_tm_cmd)
4678
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004679#ifdef CONFIG_PM
4680 .suspend = ath10k_suspend,
4681 .resume = ath10k_resume,
4682#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03004683};
4684
4685#define RATETAB_ENT(_rate, _rateid, _flags) { \
4686 .bitrate = (_rate), \
4687 .flags = (_flags), \
4688 .hw_value = (_rateid), \
4689}
4690
4691#define CHAN2G(_channel, _freq, _flags) { \
4692 .band = IEEE80211_BAND_2GHZ, \
4693 .hw_value = (_channel), \
4694 .center_freq = (_freq), \
4695 .flags = (_flags), \
4696 .max_antenna_gain = 0, \
4697 .max_power = 30, \
4698}
4699
4700#define CHAN5G(_channel, _freq, _flags) { \
4701 .band = IEEE80211_BAND_5GHZ, \
4702 .hw_value = (_channel), \
4703 .center_freq = (_freq), \
4704 .flags = (_flags), \
4705 .max_antenna_gain = 0, \
4706 .max_power = 30, \
4707}
4708
4709static const struct ieee80211_channel ath10k_2ghz_channels[] = {
4710 CHAN2G(1, 2412, 0),
4711 CHAN2G(2, 2417, 0),
4712 CHAN2G(3, 2422, 0),
4713 CHAN2G(4, 2427, 0),
4714 CHAN2G(5, 2432, 0),
4715 CHAN2G(6, 2437, 0),
4716 CHAN2G(7, 2442, 0),
4717 CHAN2G(8, 2447, 0),
4718 CHAN2G(9, 2452, 0),
4719 CHAN2G(10, 2457, 0),
4720 CHAN2G(11, 2462, 0),
4721 CHAN2G(12, 2467, 0),
4722 CHAN2G(13, 2472, 0),
4723 CHAN2G(14, 2484, 0),
4724};
4725
4726static const struct ieee80211_channel ath10k_5ghz_channels[] = {
Michal Kazior429ff562013-06-26 08:54:54 +02004727 CHAN5G(36, 5180, 0),
4728 CHAN5G(40, 5200, 0),
4729 CHAN5G(44, 5220, 0),
4730 CHAN5G(48, 5240, 0),
4731 CHAN5G(52, 5260, 0),
4732 CHAN5G(56, 5280, 0),
4733 CHAN5G(60, 5300, 0),
4734 CHAN5G(64, 5320, 0),
4735 CHAN5G(100, 5500, 0),
4736 CHAN5G(104, 5520, 0),
4737 CHAN5G(108, 5540, 0),
4738 CHAN5G(112, 5560, 0),
4739 CHAN5G(116, 5580, 0),
4740 CHAN5G(120, 5600, 0),
4741 CHAN5G(124, 5620, 0),
4742 CHAN5G(128, 5640, 0),
4743 CHAN5G(132, 5660, 0),
4744 CHAN5G(136, 5680, 0),
4745 CHAN5G(140, 5700, 0),
4746 CHAN5G(149, 5745, 0),
4747 CHAN5G(153, 5765, 0),
4748 CHAN5G(157, 5785, 0),
4749 CHAN5G(161, 5805, 0),
4750 CHAN5G(165, 5825, 0),
Kalle Valo5e3dd152013-06-12 20:52:10 +03004751};
4752
4753static struct ieee80211_rate ath10k_rates[] = {
4754 /* CCK */
4755 RATETAB_ENT(10, 0x82, 0),
4756 RATETAB_ENT(20, 0x84, 0),
4757 RATETAB_ENT(55, 0x8b, 0),
4758 RATETAB_ENT(110, 0x96, 0),
4759 /* OFDM */
4760 RATETAB_ENT(60, 0x0c, 0),
4761 RATETAB_ENT(90, 0x12, 0),
4762 RATETAB_ENT(120, 0x18, 0),
4763 RATETAB_ENT(180, 0x24, 0),
4764 RATETAB_ENT(240, 0x30, 0),
4765 RATETAB_ENT(360, 0x48, 0),
4766 RATETAB_ENT(480, 0x60, 0),
4767 RATETAB_ENT(540, 0x6c, 0),
4768};
4769
4770#define ath10k_a_rates (ath10k_rates + 4)
4771#define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - 4)
4772#define ath10k_g_rates (ath10k_rates + 0)
4773#define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
4774
Michal Kaziore7b54192014-08-07 11:03:27 +02004775struct ath10k *ath10k_mac_create(size_t priv_size)
Kalle Valo5e3dd152013-06-12 20:52:10 +03004776{
4777 struct ieee80211_hw *hw;
4778 struct ath10k *ar;
4779
Michal Kaziore7b54192014-08-07 11:03:27 +02004780 hw = ieee80211_alloc_hw(sizeof(struct ath10k) + priv_size, &ath10k_ops);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004781 if (!hw)
4782 return NULL;
4783
4784 ar = hw->priv;
4785 ar->hw = hw;
4786
4787 return ar;
4788}
4789
4790void ath10k_mac_destroy(struct ath10k *ar)
4791{
4792 ieee80211_free_hw(ar->hw);
4793}
4794
4795static const struct ieee80211_iface_limit ath10k_if_limits[] = {
4796 {
4797 .max = 8,
4798 .types = BIT(NL80211_IFTYPE_STATION)
4799 | BIT(NL80211_IFTYPE_P2P_CLIENT)
Michal Kaziord531cb82013-07-31 10:55:13 +02004800 },
4801 {
4802 .max = 3,
4803 .types = BIT(NL80211_IFTYPE_P2P_GO)
4804 },
4805 {
4806 .max = 7,
4807 .types = BIT(NL80211_IFTYPE_AP)
4808 },
Kalle Valo5e3dd152013-06-12 20:52:10 +03004809};
4810
Bartosz Markowskif2595092013-12-10 16:20:39 +01004811static const struct ieee80211_iface_limit ath10k_10x_if_limits[] = {
Marek Puzyniake8a50f82013-11-20 09:59:47 +02004812 {
4813 .max = 8,
4814 .types = BIT(NL80211_IFTYPE_AP)
4815 },
4816};
Marek Puzyniake8a50f82013-11-20 09:59:47 +02004817
4818static const struct ieee80211_iface_combination ath10k_if_comb[] = {
4819 {
4820 .limits = ath10k_if_limits,
4821 .n_limits = ARRAY_SIZE(ath10k_if_limits),
4822 .max_interfaces = 8,
4823 .num_different_channels = 1,
4824 .beacon_int_infra_match = true,
4825 },
Bartosz Markowskif2595092013-12-10 16:20:39 +01004826};
4827
4828static const struct ieee80211_iface_combination ath10k_10x_if_comb[] = {
Marek Puzyniake8a50f82013-11-20 09:59:47 +02004829 {
Bartosz Markowskif2595092013-12-10 16:20:39 +01004830 .limits = ath10k_10x_if_limits,
4831 .n_limits = ARRAY_SIZE(ath10k_10x_if_limits),
Marek Puzyniake8a50f82013-11-20 09:59:47 +02004832 .max_interfaces = 8,
4833 .num_different_channels = 1,
4834 .beacon_int_infra_match = true,
Bartosz Markowskif2595092013-12-10 16:20:39 +01004835#ifdef CONFIG_ATH10K_DFS_CERTIFIED
Marek Puzyniake8a50f82013-11-20 09:59:47 +02004836 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
4837 BIT(NL80211_CHAN_WIDTH_20) |
4838 BIT(NL80211_CHAN_WIDTH_40) |
4839 BIT(NL80211_CHAN_WIDTH_80),
Marek Puzyniake8a50f82013-11-20 09:59:47 +02004840#endif
Bartosz Markowskif2595092013-12-10 16:20:39 +01004841 },
Kalle Valo5e3dd152013-06-12 20:52:10 +03004842};
4843
4844static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
4845{
4846 struct ieee80211_sta_vht_cap vht_cap = {0};
4847 u16 mcs_map;
Michal Kazior8865bee42013-07-24 12:36:46 +02004848 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004849
4850 vht_cap.vht_supported = 1;
4851 vht_cap.cap = ar->vht_cap_info;
4852
Michal Kazior8865bee42013-07-24 12:36:46 +02004853 mcs_map = 0;
4854 for (i = 0; i < 8; i++) {
4855 if (i < ar->num_rf_chains)
4856 mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i*2);
4857 else
4858 mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i*2);
4859 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03004860
4861 vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
4862 vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
4863
4864 return vht_cap;
4865}
4866
4867static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
4868{
4869 int i;
4870 struct ieee80211_sta_ht_cap ht_cap = {0};
4871
4872 if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
4873 return ht_cap;
4874
4875 ht_cap.ht_supported = 1;
4876 ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
4877 ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
4878 ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
4879 ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
4880 ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
4881
4882 if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
4883 ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
4884
4885 if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
4886 ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
4887
4888 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
4889 u32 smps;
4890
4891 smps = WLAN_HT_CAP_SM_PS_DYNAMIC;
4892 smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
4893
4894 ht_cap.cap |= smps;
4895 }
4896
4897 if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC)
4898 ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
4899
4900 if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
4901 u32 stbc;
4902
4903 stbc = ar->ht_cap_info;
4904 stbc &= WMI_HT_CAP_RX_STBC;
4905 stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
4906 stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
4907 stbc &= IEEE80211_HT_CAP_RX_STBC;
4908
4909 ht_cap.cap |= stbc;
4910 }
4911
4912 if (ar->ht_cap_info & WMI_HT_CAP_LDPC)
4913 ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
4914
4915 if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
4916 ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
4917
4918 /* max AMSDU is implicitly taken from vht_cap_info */
4919 if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
4920 ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
4921
Michal Kazior8865bee42013-07-24 12:36:46 +02004922 for (i = 0; i < ar->num_rf_chains; i++)
Kalle Valo5e3dd152013-06-12 20:52:10 +03004923 ht_cap.mcs.rx_mask[i] = 0xFF;
4924
4925 ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
4926
4927 return ht_cap;
4928}
4929
Kalle Valo5e3dd152013-06-12 20:52:10 +03004930static void ath10k_get_arvif_iter(void *data, u8 *mac,
4931 struct ieee80211_vif *vif)
4932{
4933 struct ath10k_vif_iter *arvif_iter = data;
4934 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4935
4936 if (arvif->vdev_id == arvif_iter->vdev_id)
4937 arvif_iter->arvif = arvif;
4938}
4939
4940struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
4941{
4942 struct ath10k_vif_iter arvif_iter;
4943 u32 flags;
4944
4945 memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
4946 arvif_iter.vdev_id = vdev_id;
4947
4948 flags = IEEE80211_IFACE_ITER_RESUME_ALL;
4949 ieee80211_iterate_active_interfaces_atomic(ar->hw,
4950 flags,
4951 ath10k_get_arvif_iter,
4952 &arvif_iter);
4953 if (!arvif_iter.arvif) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004954 ath10k_warn(ar, "No VIF found for vdev %d\n", vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004955 return NULL;
4956 }
4957
4958 return arvif_iter.arvif;
4959}
4960
4961int ath10k_mac_register(struct ath10k *ar)
4962{
4963 struct ieee80211_supported_band *band;
4964 struct ieee80211_sta_vht_cap vht_cap;
4965 struct ieee80211_sta_ht_cap ht_cap;
4966 void *channels;
4967 int ret;
4968
4969 SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
4970
4971 SET_IEEE80211_DEV(ar->hw, ar->dev);
4972
4973 ht_cap = ath10k_get_ht_cap(ar);
4974 vht_cap = ath10k_create_vht_cap(ar);
4975
4976 if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
4977 channels = kmemdup(ath10k_2ghz_channels,
4978 sizeof(ath10k_2ghz_channels),
4979 GFP_KERNEL);
Michal Kaziord6015b22013-07-22 14:13:30 +02004980 if (!channels) {
4981 ret = -ENOMEM;
4982 goto err_free;
4983 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03004984
4985 band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
4986 band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
4987 band->channels = channels;
4988 band->n_bitrates = ath10k_g_rates_size;
4989 band->bitrates = ath10k_g_rates;
4990 band->ht_cap = ht_cap;
4991
4992 /* vht is not supported in 2.4 GHz */
4993
4994 ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
4995 }
4996
4997 if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
4998 channels = kmemdup(ath10k_5ghz_channels,
4999 sizeof(ath10k_5ghz_channels),
5000 GFP_KERNEL);
5001 if (!channels) {
Michal Kaziord6015b22013-07-22 14:13:30 +02005002 ret = -ENOMEM;
5003 goto err_free;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005004 }
5005
5006 band = &ar->mac.sbands[IEEE80211_BAND_5GHZ];
5007 band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
5008 band->channels = channels;
5009 band->n_bitrates = ath10k_a_rates_size;
5010 band->bitrates = ath10k_a_rates;
5011 band->ht_cap = ht_cap;
5012 band->vht_cap = vht_cap;
5013 ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
5014 }
5015
5016 ar->hw->wiphy->interface_modes =
5017 BIT(NL80211_IFTYPE_STATION) |
Bartosz Markowskid3541812013-12-10 16:20:40 +01005018 BIT(NL80211_IFTYPE_AP);
5019
Ben Greear46acf7b2014-05-16 17:15:38 +03005020 ar->hw->wiphy->available_antennas_rx = ar->supp_rx_chainmask;
5021 ar->hw->wiphy->available_antennas_tx = ar->supp_tx_chainmask;
5022
Bartosz Markowskid3541812013-12-10 16:20:40 +01005023 if (!test_bit(ATH10K_FW_FEATURE_NO_P2P, ar->fw_features))
5024 ar->hw->wiphy->interface_modes |=
5025 BIT(NL80211_IFTYPE_P2P_CLIENT) |
5026 BIT(NL80211_IFTYPE_P2P_GO);
Kalle Valo5e3dd152013-06-12 20:52:10 +03005027
5028 ar->hw->flags = IEEE80211_HW_SIGNAL_DBM |
5029 IEEE80211_HW_SUPPORTS_PS |
5030 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
5031 IEEE80211_HW_SUPPORTS_UAPSD |
5032 IEEE80211_HW_MFP_CAPABLE |
5033 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
5034 IEEE80211_HW_HAS_RATE_CONTROL |
Janusz Dziedzic2f0f1122014-02-26 18:42:09 +02005035 IEEE80211_HW_AP_LINK_PS |
5036 IEEE80211_HW_SPECTRUM_MGMT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005037
Eliad Peller0d8614b2014-09-10 14:07:36 +03005038 ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
5039
Kalle Valo5e3dd152013-06-12 20:52:10 +03005040 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
Eliad Peller0d8614b2014-09-10 14:07:36 +03005041 ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005042
5043 if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
5044 ar->hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
5045 ar->hw->flags |= IEEE80211_HW_TX_AMPDU_SETUP_IN_HW;
5046 }
5047
5048 ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
5049 ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
5050
5051 ar->hw->vif_data_size = sizeof(struct ath10k_vif);
Michal Kazior9797feb2014-02-14 14:49:48 +01005052 ar->hw->sta_data_size = sizeof(struct ath10k_sta);
Kalle Valo5e3dd152013-06-12 20:52:10 +03005053
Kalle Valo5e3dd152013-06-12 20:52:10 +03005054 ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
5055
5056 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
Michal Kaziorc2df44b2014-01-23 11:38:26 +01005057 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005058 ar->hw->wiphy->max_remain_on_channel_duration = 5000;
5059
5060 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
Rajkumar Manoharan78157a12014-11-17 16:44:15 +02005061 ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE;
5062
Kalle Valo5e3dd152013-06-12 20:52:10 +03005063 /*
5064 * on LL hardware queues are managed entirely by the FW
5065 * so we only advertise to mac we can do the queues thing
5066 */
5067 ar->hw->queues = 4;
5068
Bartosz Markowskif2595092013-12-10 16:20:39 +01005069 if (test_bit(ATH10K_FW_FEATURE_WMI_10X, ar->fw_features)) {
5070 ar->hw->wiphy->iface_combinations = ath10k_10x_if_comb;
5071 ar->hw->wiphy->n_iface_combinations =
5072 ARRAY_SIZE(ath10k_10x_if_comb);
5073 } else {
5074 ar->hw->wiphy->iface_combinations = ath10k_if_comb;
5075 ar->hw->wiphy->n_iface_combinations =
5076 ARRAY_SIZE(ath10k_if_comb);
Michal Kaziorcf850d12014-07-24 20:07:00 +03005077
5078 ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
Bartosz Markowskif2595092013-12-10 16:20:39 +01005079 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03005080
Michal Kazior7c199992013-07-31 10:47:57 +02005081 ar->hw->netdev_features = NETIF_F_HW_CSUM;
5082
Janusz Dziedzic9702c682013-11-20 09:59:41 +02005083 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED)) {
5084 /* Init ath dfs pattern detector */
5085 ar->ath_common.debug_mask = ATH_DBG_DFS;
5086 ar->dfs_detector = dfs_pattern_detector_init(&ar->ath_common,
5087 NL80211_DFS_UNSET);
5088
5089 if (!ar->dfs_detector)
Michal Kazior7aa7a722014-08-25 12:09:38 +02005090 ath10k_warn(ar, "failed to initialise DFS pattern detector\n");
Janusz Dziedzic9702c682013-11-20 09:59:41 +02005091 }
5092
Kalle Valo5e3dd152013-06-12 20:52:10 +03005093 ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
5094 ath10k_reg_notifier);
5095 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02005096 ath10k_err(ar, "failed to initialise regulatory: %i\n", ret);
Michal Kaziord6015b22013-07-22 14:13:30 +02005097 goto err_free;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005098 }
5099
5100 ret = ieee80211_register_hw(ar->hw);
5101 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02005102 ath10k_err(ar, "failed to register ieee80211: %d\n", ret);
Michal Kaziord6015b22013-07-22 14:13:30 +02005103 goto err_free;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005104 }
5105
5106 if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
5107 ret = regulatory_hint(ar->hw->wiphy,
5108 ar->ath_common.regulatory.alpha2);
5109 if (ret)
Michal Kaziord6015b22013-07-22 14:13:30 +02005110 goto err_unregister;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005111 }
5112
5113 return 0;
Michal Kaziord6015b22013-07-22 14:13:30 +02005114
5115err_unregister:
Kalle Valo5e3dd152013-06-12 20:52:10 +03005116 ieee80211_unregister_hw(ar->hw);
Michal Kaziord6015b22013-07-22 14:13:30 +02005117err_free:
5118 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
5119 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
5120
Kalle Valo5e3dd152013-06-12 20:52:10 +03005121 return ret;
5122}
5123
5124void ath10k_mac_unregister(struct ath10k *ar)
5125{
5126 ieee80211_unregister_hw(ar->hw);
5127
Janusz Dziedzic9702c682013-11-20 09:59:41 +02005128 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
5129 ar->dfs_detector->exit(ar->dfs_detector);
5130
Kalle Valo5e3dd152013-06-12 20:52:10 +03005131 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
5132 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
5133
5134 SET_IEEE80211_DEV(ar->hw, NULL);
5135}