blob: 2409147d0b0faf40454930bbaae299dde27ea49f [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"
Michal Kaziord7579d12014-12-03 10:10:54 +020030#include "wmi.h"
31#include "wmi-ops.h"
Kalle Valo5e3dd152013-06-12 20:52:10 +030032
33/**********/
34/* Crypto */
35/**********/
36
37static int ath10k_send_key(struct ath10k_vif *arvif,
38 struct ieee80211_key_conf *key,
39 enum set_key_cmd cmd,
Michal Kazior370e5672015-02-18 14:02:26 +010040 const u8 *macaddr, u32 flags)
Kalle Valo5e3dd152013-06-12 20:52:10 +030041{
Michal Kazior7aa7a722014-08-25 12:09:38 +020042 struct ath10k *ar = arvif->ar;
Kalle Valo5e3dd152013-06-12 20:52:10 +030043 struct wmi_vdev_install_key_arg arg = {
44 .vdev_id = arvif->vdev_id,
45 .key_idx = key->keyidx,
46 .key_len = key->keylen,
47 .key_data = key->key,
Michal Kazior370e5672015-02-18 14:02:26 +010048 .key_flags = flags,
Kalle Valo5e3dd152013-06-12 20:52:10 +030049 .macaddr = macaddr,
50 };
51
Michal Kazior548db542013-07-05 16:15:15 +030052 lockdep_assert_held(&arvif->ar->conf_mutex);
53
Kalle Valo5e3dd152013-06-12 20:52:10 +030054 switch (key->cipher) {
55 case WLAN_CIPHER_SUITE_CCMP:
56 arg.key_cipher = WMI_CIPHER_AES_CCM;
Marek Kwaczynskie4e82e92015-01-24 12:14:53 +020057 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
Kalle Valo5e3dd152013-06-12 20:52:10 +030058 break;
59 case WLAN_CIPHER_SUITE_TKIP:
Kalle Valo5e3dd152013-06-12 20:52:10 +030060 arg.key_cipher = WMI_CIPHER_TKIP;
61 arg.key_txmic_len = 8;
62 arg.key_rxmic_len = 8;
63 break;
64 case WLAN_CIPHER_SUITE_WEP40:
65 case WLAN_CIPHER_SUITE_WEP104:
66 arg.key_cipher = WMI_CIPHER_WEP;
Kalle Valo5e3dd152013-06-12 20:52:10 +030067 break;
Johannes Berg3cb10942015-01-22 21:38:45 +010068 case WLAN_CIPHER_SUITE_AES_CMAC:
Bartosz Markowskid7131c02015-03-10 14:32:19 +010069 WARN_ON(1);
70 return -EINVAL;
Kalle Valo5e3dd152013-06-12 20:52:10 +030071 default:
Michal Kazior7aa7a722014-08-25 12:09:38 +020072 ath10k_warn(ar, "cipher %d is not supported\n", key->cipher);
Kalle Valo5e3dd152013-06-12 20:52:10 +030073 return -EOPNOTSUPP;
74 }
75
76 if (cmd == DISABLE_KEY) {
77 arg.key_cipher = WMI_CIPHER_NONE;
78 arg.key_data = NULL;
79 }
80
81 return ath10k_wmi_vdev_install_key(arvif->ar, &arg);
82}
83
84static int ath10k_install_key(struct ath10k_vif *arvif,
85 struct ieee80211_key_conf *key,
86 enum set_key_cmd cmd,
Michal Kazior370e5672015-02-18 14:02:26 +010087 const u8 *macaddr, u32 flags)
Kalle Valo5e3dd152013-06-12 20:52:10 +030088{
89 struct ath10k *ar = arvif->ar;
90 int ret;
91
Michal Kazior548db542013-07-05 16:15:15 +030092 lockdep_assert_held(&ar->conf_mutex);
93
Wolfram Sang16735d02013-11-14 14:32:02 -080094 reinit_completion(&ar->install_key_done);
Kalle Valo5e3dd152013-06-12 20:52:10 +030095
Michal Kazior370e5672015-02-18 14:02:26 +010096 ret = ath10k_send_key(arvif, key, cmd, macaddr, flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +030097 if (ret)
98 return ret;
99
100 ret = wait_for_completion_timeout(&ar->install_key_done, 3*HZ);
101 if (ret == 0)
102 return -ETIMEDOUT;
103
104 return 0;
105}
106
107static int ath10k_install_peer_wep_keys(struct ath10k_vif *arvif,
108 const u8 *addr)
109{
110 struct ath10k *ar = arvif->ar;
111 struct ath10k_peer *peer;
112 int ret;
113 int i;
Michal Kazior370e5672015-02-18 14:02:26 +0100114 u32 flags;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300115
116 lockdep_assert_held(&ar->conf_mutex);
117
118 spin_lock_bh(&ar->data_lock);
119 peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
120 spin_unlock_bh(&ar->data_lock);
121
122 if (!peer)
123 return -ENOENT;
124
125 for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
126 if (arvif->wep_keys[i] == NULL)
127 continue;
Michal Kazior370e5672015-02-18 14:02:26 +0100128
129 flags = 0;
130 flags |= WMI_KEY_PAIRWISE;
131
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +0200132 /* set TX_USAGE flag for default key id */
133 if (arvif->def_wep_key_idx == i)
Michal Kazior370e5672015-02-18 14:02:26 +0100134 flags |= WMI_KEY_TX_USAGE;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300135
136 ret = ath10k_install_key(arvif, arvif->wep_keys[i], SET_KEY,
Michal Kazior370e5672015-02-18 14:02:26 +0100137 addr, flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300138 if (ret)
139 return ret;
140
Sujith Manoharanae167132014-11-25 11:46:59 +0530141 spin_lock_bh(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300142 peer->keys[i] = arvif->wep_keys[i];
Sujith Manoharanae167132014-11-25 11:46:59 +0530143 spin_unlock_bh(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300144 }
145
146 return 0;
147}
148
149static int ath10k_clear_peer_keys(struct ath10k_vif *arvif,
150 const u8 *addr)
151{
152 struct ath10k *ar = arvif->ar;
153 struct ath10k_peer *peer;
154 int first_errno = 0;
155 int ret;
156 int i;
Michal Kazior370e5672015-02-18 14:02:26 +0100157 u32 flags = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300158
159 lockdep_assert_held(&ar->conf_mutex);
160
161 spin_lock_bh(&ar->data_lock);
162 peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
163 spin_unlock_bh(&ar->data_lock);
164
165 if (!peer)
166 return -ENOENT;
167
168 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
169 if (peer->keys[i] == NULL)
170 continue;
171
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +0200172 /* key flags are not required to delete the key */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300173 ret = ath10k_install_key(arvif, peer->keys[i],
Michal Kazior370e5672015-02-18 14:02:26 +0100174 DISABLE_KEY, addr, flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300175 if (ret && first_errno == 0)
176 first_errno = ret;
177
178 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200179 ath10k_warn(ar, "failed to remove peer wep key %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300180 i, ret);
181
Sujith Manoharanae167132014-11-25 11:46:59 +0530182 spin_lock_bh(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300183 peer->keys[i] = NULL;
Sujith Manoharanae167132014-11-25 11:46:59 +0530184 spin_unlock_bh(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300185 }
186
187 return first_errno;
188}
189
Sujith Manoharan504f6cd2014-11-25 11:46:58 +0530190bool ath10k_mac_is_peer_wep_key_set(struct ath10k *ar, const u8 *addr,
191 u8 keyidx)
192{
193 struct ath10k_peer *peer;
194 int i;
195
196 lockdep_assert_held(&ar->data_lock);
197
198 /* We don't know which vdev this peer belongs to,
199 * since WMI doesn't give us that information.
200 *
201 * FIXME: multi-bss needs to be handled.
202 */
203 peer = ath10k_peer_find(ar, 0, addr);
204 if (!peer)
205 return false;
206
207 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
208 if (peer->keys[i] && peer->keys[i]->keyidx == keyidx)
209 return true;
210 }
211
212 return false;
213}
214
Kalle Valo5e3dd152013-06-12 20:52:10 +0300215static int ath10k_clear_vdev_key(struct ath10k_vif *arvif,
216 struct ieee80211_key_conf *key)
217{
218 struct ath10k *ar = arvif->ar;
219 struct ath10k_peer *peer;
220 u8 addr[ETH_ALEN];
221 int first_errno = 0;
222 int ret;
223 int i;
Michal Kazior370e5672015-02-18 14:02:26 +0100224 u32 flags = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300225
226 lockdep_assert_held(&ar->conf_mutex);
227
228 for (;;) {
229 /* since ath10k_install_key we can't hold data_lock all the
230 * time, so we try to remove the keys incrementally */
231 spin_lock_bh(&ar->data_lock);
232 i = 0;
233 list_for_each_entry(peer, &ar->peers, list) {
234 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
235 if (peer->keys[i] == key) {
Kalle Valob25f32c2014-09-14 12:50:49 +0300236 ether_addr_copy(addr, peer->addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300237 peer->keys[i] = NULL;
238 break;
239 }
240 }
241
242 if (i < ARRAY_SIZE(peer->keys))
243 break;
244 }
245 spin_unlock_bh(&ar->data_lock);
246
247 if (i == ARRAY_SIZE(peer->keys))
248 break;
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +0200249 /* key flags are not required to delete the key */
Michal Kazior370e5672015-02-18 14:02:26 +0100250 ret = ath10k_install_key(arvif, key, DISABLE_KEY, addr, flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300251 if (ret && first_errno == 0)
252 first_errno = ret;
253
254 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200255 ath10k_warn(ar, "failed to remove key for %pM: %d\n",
Kalle Valobe6546f2014-03-25 14:18:51 +0200256 addr, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300257 }
258
259 return first_errno;
260}
261
Michal Kazior370e5672015-02-18 14:02:26 +0100262static int ath10k_mac_vif_sta_fix_wep_key(struct ath10k_vif *arvif)
263{
264 struct ath10k *ar = arvif->ar;
265 enum nl80211_iftype iftype = arvif->vif->type;
266 struct ieee80211_key_conf *key;
267 u32 flags = 0;
268 int num = 0;
269 int i;
270 int ret;
271
272 lockdep_assert_held(&ar->conf_mutex);
273
274 if (iftype != NL80211_IFTYPE_STATION)
275 return 0;
276
277 for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
278 if (arvif->wep_keys[i]) {
279 key = arvif->wep_keys[i];
280 ++num;
281 }
282 }
283
284 if (num != 1)
285 return 0;
286
287 flags |= WMI_KEY_PAIRWISE;
288 flags |= WMI_KEY_TX_USAGE;
289
290 ret = ath10k_install_key(arvif, key, SET_KEY, arvif->bssid, flags);
291 if (ret) {
292 ath10k_warn(ar, "failed to install key %i on vdev %i: %d\n",
293 key->keyidx, arvif->vdev_id, ret);
294 return ret;
295 }
296
297 return 0;
298}
299
Michal Kaziorad325cb2015-02-18 14:02:27 +0100300static int ath10k_mac_vif_update_wep_key(struct ath10k_vif *arvif,
301 struct ieee80211_key_conf *key)
302{
303 struct ath10k *ar = arvif->ar;
304 struct ath10k_peer *peer;
305 int ret;
306
307 lockdep_assert_held(&ar->conf_mutex);
308
309 list_for_each_entry(peer, &ar->peers, list) {
310 if (!memcmp(peer->addr, arvif->vif->addr, ETH_ALEN))
311 continue;
312
313 if (!memcmp(peer->addr, arvif->bssid, ETH_ALEN))
314 continue;
315
316 if (peer->keys[key->keyidx] == key)
317 continue;
318
319 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vif vdev %i update key %i needs update\n",
320 arvif->vdev_id, key->keyidx);
321
322 ret = ath10k_install_peer_wep_keys(arvif, peer->addr);
323 if (ret) {
324 ath10k_warn(ar, "failed to update wep keys on vdev %i for peer %pM: %d\n",
325 arvif->vdev_id, peer->addr, ret);
326 return ret;
327 }
328 }
329
330 return 0;
331}
332
Kalle Valo5e3dd152013-06-12 20:52:10 +0300333/*********************/
334/* General utilities */
335/*********************/
336
337static inline enum wmi_phy_mode
338chan_to_phymode(const struct cfg80211_chan_def *chandef)
339{
340 enum wmi_phy_mode phymode = MODE_UNKNOWN;
341
342 switch (chandef->chan->band) {
343 case IEEE80211_BAND_2GHZ:
344 switch (chandef->width) {
345 case NL80211_CHAN_WIDTH_20_NOHT:
Peter Oh6faab122014-12-18 10:13:00 -0800346 if (chandef->chan->flags & IEEE80211_CHAN_NO_OFDM)
347 phymode = MODE_11B;
348 else
349 phymode = MODE_11G;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300350 break;
351 case NL80211_CHAN_WIDTH_20:
352 phymode = MODE_11NG_HT20;
353 break;
354 case NL80211_CHAN_WIDTH_40:
355 phymode = MODE_11NG_HT40;
356 break;
John W. Linville0f817ed2013-06-27 13:50:09 -0400357 case NL80211_CHAN_WIDTH_5:
358 case NL80211_CHAN_WIDTH_10:
Kalle Valo5e3dd152013-06-12 20:52:10 +0300359 case NL80211_CHAN_WIDTH_80:
360 case NL80211_CHAN_WIDTH_80P80:
361 case NL80211_CHAN_WIDTH_160:
362 phymode = MODE_UNKNOWN;
363 break;
364 }
365 break;
366 case IEEE80211_BAND_5GHZ:
367 switch (chandef->width) {
368 case NL80211_CHAN_WIDTH_20_NOHT:
369 phymode = MODE_11A;
370 break;
371 case NL80211_CHAN_WIDTH_20:
372 phymode = MODE_11NA_HT20;
373 break;
374 case NL80211_CHAN_WIDTH_40:
375 phymode = MODE_11NA_HT40;
376 break;
377 case NL80211_CHAN_WIDTH_80:
378 phymode = MODE_11AC_VHT80;
379 break;
John W. Linville0f817ed2013-06-27 13:50:09 -0400380 case NL80211_CHAN_WIDTH_5:
381 case NL80211_CHAN_WIDTH_10:
Kalle Valo5e3dd152013-06-12 20:52:10 +0300382 case NL80211_CHAN_WIDTH_80P80:
383 case NL80211_CHAN_WIDTH_160:
384 phymode = MODE_UNKNOWN;
385 break;
386 }
387 break;
388 default:
389 break;
390 }
391
392 WARN_ON(phymode == MODE_UNKNOWN);
393 return phymode;
394}
395
396static u8 ath10k_parse_mpdudensity(u8 mpdudensity)
397{
398/*
399 * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
400 * 0 for no restriction
401 * 1 for 1/4 us
402 * 2 for 1/2 us
403 * 3 for 1 us
404 * 4 for 2 us
405 * 5 for 4 us
406 * 6 for 8 us
407 * 7 for 16 us
408 */
409 switch (mpdudensity) {
410 case 0:
411 return 0;
412 case 1:
413 case 2:
414 case 3:
415 /* Our lower layer calculations limit our precision to
416 1 microsecond */
417 return 1;
418 case 4:
419 return 2;
420 case 5:
421 return 4;
422 case 6:
423 return 8;
424 case 7:
425 return 16;
426 default:
427 return 0;
428 }
429}
430
431static int ath10k_peer_create(struct ath10k *ar, u32 vdev_id, const u8 *addr)
432{
433 int ret;
434
435 lockdep_assert_held(&ar->conf_mutex);
436
Michal Kaziorcfd10612014-11-25 15:16:05 +0100437 if (ar->num_peers >= ar->max_num_peers)
438 return -ENOBUFS;
439
Kalle Valo5e3dd152013-06-12 20:52:10 +0300440 ret = ath10k_wmi_peer_create(ar, vdev_id, addr);
Ben Greear479398b2013-11-04 09:19:34 -0800441 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200442 ath10k_warn(ar, "failed to create wmi peer %pM on vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +0200443 addr, vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300444 return ret;
Ben Greear479398b2013-11-04 09:19:34 -0800445 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300446
447 ret = ath10k_wait_for_peer_created(ar, vdev_id, addr);
Ben Greear479398b2013-11-04 09:19:34 -0800448 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200449 ath10k_warn(ar, "failed to wait for created wmi peer %pM on vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +0200450 addr, vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300451 return ret;
Ben Greear479398b2013-11-04 09:19:34 -0800452 }
Michal Kazior292a7532014-11-25 15:16:04 +0100453
Bartosz Markowski0e759f32014-01-02 14:38:33 +0100454 ar->num_peers++;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300455
456 return 0;
457}
458
Kalle Valo5a13e762014-01-20 11:01:46 +0200459static int ath10k_mac_set_kickout(struct ath10k_vif *arvif)
460{
461 struct ath10k *ar = arvif->ar;
462 u32 param;
463 int ret;
464
465 param = ar->wmi.pdev_param->sta_kickout_th;
466 ret = ath10k_wmi_pdev_set_param(ar, param,
467 ATH10K_KICKOUT_THRESHOLD);
468 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200469 ath10k_warn(ar, "failed to set kickout threshold on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200470 arvif->vdev_id, ret);
Kalle Valo5a13e762014-01-20 11:01:46 +0200471 return ret;
472 }
473
474 param = ar->wmi.vdev_param->ap_keepalive_min_idle_inactive_time_secs;
475 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
476 ATH10K_KEEPALIVE_MIN_IDLE);
477 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200478 ath10k_warn(ar, "failed to set keepalive minimum idle time on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200479 arvif->vdev_id, ret);
Kalle Valo5a13e762014-01-20 11:01:46 +0200480 return ret;
481 }
482
483 param = ar->wmi.vdev_param->ap_keepalive_max_idle_inactive_time_secs;
484 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
485 ATH10K_KEEPALIVE_MAX_IDLE);
486 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200487 ath10k_warn(ar, "failed to set keepalive maximum idle time on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200488 arvif->vdev_id, ret);
Kalle Valo5a13e762014-01-20 11:01:46 +0200489 return ret;
490 }
491
492 param = ar->wmi.vdev_param->ap_keepalive_max_unresponsive_time_secs;
493 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
494 ATH10K_KEEPALIVE_MAX_UNRESPONSIVE);
495 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200496 ath10k_warn(ar, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200497 arvif->vdev_id, ret);
Kalle Valo5a13e762014-01-20 11:01:46 +0200498 return ret;
499 }
500
501 return 0;
502}
503
Vivek Natarajanacab6402014-11-26 09:06:12 +0200504static int ath10k_mac_set_rts(struct ath10k_vif *arvif, u32 value)
Michal Kazior424121c2013-07-22 14:13:31 +0200505{
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200506 struct ath10k *ar = arvif->ar;
507 u32 vdev_param;
508
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200509 vdev_param = ar->wmi.vdev_param->rts_threshold;
510 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
Michal Kazior424121c2013-07-22 14:13:31 +0200511}
512
513static int ath10k_mac_set_frag(struct ath10k_vif *arvif, u32 value)
514{
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200515 struct ath10k *ar = arvif->ar;
516 u32 vdev_param;
517
Michal Kazior424121c2013-07-22 14:13:31 +0200518 if (value != 0xFFFFFFFF)
519 value = clamp_t(u32, arvif->ar->hw->wiphy->frag_threshold,
520 ATH10K_FRAGMT_THRESHOLD_MIN,
521 ATH10K_FRAGMT_THRESHOLD_MAX);
522
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200523 vdev_param = ar->wmi.vdev_param->fragmentation_threshold;
524 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
Michal Kazior424121c2013-07-22 14:13:31 +0200525}
526
Kalle Valo5e3dd152013-06-12 20:52:10 +0300527static int ath10k_peer_delete(struct ath10k *ar, u32 vdev_id, const u8 *addr)
528{
529 int ret;
530
531 lockdep_assert_held(&ar->conf_mutex);
532
533 ret = ath10k_wmi_peer_delete(ar, vdev_id, addr);
534 if (ret)
535 return ret;
536
537 ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
538 if (ret)
539 return ret;
540
Bartosz Markowski0e759f32014-01-02 14:38:33 +0100541 ar->num_peers--;
Bartosz Markowski0e759f32014-01-02 14:38:33 +0100542
Kalle Valo5e3dd152013-06-12 20:52:10 +0300543 return 0;
544}
545
546static void ath10k_peer_cleanup(struct ath10k *ar, u32 vdev_id)
547{
548 struct ath10k_peer *peer, *tmp;
549
550 lockdep_assert_held(&ar->conf_mutex);
551
552 spin_lock_bh(&ar->data_lock);
553 list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
554 if (peer->vdev_id != vdev_id)
555 continue;
556
Michal Kazior7aa7a722014-08-25 12:09:38 +0200557 ath10k_warn(ar, "removing stale peer %pM from vdev_id %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300558 peer->addr, vdev_id);
559
560 list_del(&peer->list);
561 kfree(peer);
Bartosz Markowski0e759f32014-01-02 14:38:33 +0100562 ar->num_peers--;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300563 }
564 spin_unlock_bh(&ar->data_lock);
565}
566
Michal Kaziora96d7742013-07-16 09:38:56 +0200567static void ath10k_peer_cleanup_all(struct ath10k *ar)
568{
569 struct ath10k_peer *peer, *tmp;
570
571 lockdep_assert_held(&ar->conf_mutex);
572
573 spin_lock_bh(&ar->data_lock);
574 list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
575 list_del(&peer->list);
576 kfree(peer);
577 }
578 spin_unlock_bh(&ar->data_lock);
Michal Kazior292a7532014-11-25 15:16:04 +0100579
580 ar->num_peers = 0;
Michal Kaziorcfd10612014-11-25 15:16:05 +0100581 ar->num_stations = 0;
Michal Kaziora96d7742013-07-16 09:38:56 +0200582}
583
Kalle Valo5e3dd152013-06-12 20:52:10 +0300584/************************/
585/* Interface management */
586/************************/
587
Michal Kazior64badcb2014-09-18 11:18:02 +0300588void ath10k_mac_vif_beacon_free(struct ath10k_vif *arvif)
589{
590 struct ath10k *ar = arvif->ar;
591
592 lockdep_assert_held(&ar->data_lock);
593
594 if (!arvif->beacon)
595 return;
596
597 if (!arvif->beacon_buf)
598 dma_unmap_single(ar->dev, ATH10K_SKB_CB(arvif->beacon)->paddr,
599 arvif->beacon->len, DMA_TO_DEVICE);
600
Michal Kazioraf213192015-01-29 14:29:52 +0200601 if (WARN_ON(arvif->beacon_state != ATH10K_BEACON_SCHEDULED &&
602 arvif->beacon_state != ATH10K_BEACON_SENT))
603 return;
604
Michal Kazior64badcb2014-09-18 11:18:02 +0300605 dev_kfree_skb_any(arvif->beacon);
606
607 arvif->beacon = NULL;
Michal Kazioraf213192015-01-29 14:29:52 +0200608 arvif->beacon_state = ATH10K_BEACON_SCHEDULED;
Michal Kazior64badcb2014-09-18 11:18:02 +0300609}
610
611static void ath10k_mac_vif_beacon_cleanup(struct ath10k_vif *arvif)
612{
613 struct ath10k *ar = arvif->ar;
614
615 lockdep_assert_held(&ar->data_lock);
616
617 ath10k_mac_vif_beacon_free(arvif);
618
619 if (arvif->beacon_buf) {
620 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
621 arvif->beacon_buf, arvif->beacon_paddr);
622 arvif->beacon_buf = NULL;
623 }
624}
625
Kalle Valo5e3dd152013-06-12 20:52:10 +0300626static inline int ath10k_vdev_setup_sync(struct ath10k *ar)
627{
628 int ret;
629
Michal Kazior548db542013-07-05 16:15:15 +0300630 lockdep_assert_held(&ar->conf_mutex);
631
Michal Kazior7962b0d2014-10-28 10:34:38 +0100632 if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags))
633 return -ESHUTDOWN;
634
Kalle Valo5e3dd152013-06-12 20:52:10 +0300635 ret = wait_for_completion_timeout(&ar->vdev_setup_done,
636 ATH10K_VDEV_SETUP_TIMEOUT_HZ);
637 if (ret == 0)
638 return -ETIMEDOUT;
639
640 return 0;
641}
642
Michal Kazior1bbc0972014-04-08 09:45:47 +0300643static int ath10k_monitor_vdev_start(struct ath10k *ar, int vdev_id)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300644{
Michal Kaziorc930f742014-01-23 11:38:25 +0100645 struct cfg80211_chan_def *chandef = &ar->chandef;
646 struct ieee80211_channel *channel = chandef->chan;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300647 struct wmi_vdev_start_request_arg arg = {};
Kalle Valo5e3dd152013-06-12 20:52:10 +0300648 int ret = 0;
649
650 lockdep_assert_held(&ar->conf_mutex);
651
Kalle Valo5e3dd152013-06-12 20:52:10 +0300652 arg.vdev_id = vdev_id;
653 arg.channel.freq = channel->center_freq;
Michal Kaziorc930f742014-01-23 11:38:25 +0100654 arg.channel.band_center_freq1 = chandef->center_freq1;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300655
656 /* TODO setup this dynamically, what in case we
657 don't have any vifs? */
Michal Kaziorc930f742014-01-23 11:38:25 +0100658 arg.channel.mode = chan_to_phymode(chandef);
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200659 arg.channel.chan_radar =
660 !!(channel->flags & IEEE80211_CHAN_RADAR);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300661
Michal Kazior89c5c842013-10-23 04:02:13 -0700662 arg.channel.min_power = 0;
Michal Kazior02256932013-10-23 04:02:14 -0700663 arg.channel.max_power = channel->max_power * 2;
664 arg.channel.max_reg_power = channel->max_reg_power * 2;
665 arg.channel.max_antenna_gain = channel->max_antenna_gain * 2;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300666
Michal Kazior7962b0d2014-10-28 10:34:38 +0100667 reinit_completion(&ar->vdev_setup_done);
668
Kalle Valo5e3dd152013-06-12 20:52:10 +0300669 ret = ath10k_wmi_vdev_start(ar, &arg);
670 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200671 ath10k_warn(ar, "failed to request monitor vdev %i start: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200672 vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300673 return ret;
674 }
675
676 ret = ath10k_vdev_setup_sync(ar);
677 if (ret) {
Ben Greear60028a82015-02-15 16:50:39 +0200678 ath10k_warn(ar, "failed to synchronize setup for monitor vdev %i start: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200679 vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300680 return ret;
681 }
682
683 ret = ath10k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
684 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200685 ath10k_warn(ar, "failed to put up monitor vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200686 vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300687 goto vdev_stop;
688 }
689
690 ar->monitor_vdev_id = vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300691
Michal Kazior7aa7a722014-08-25 12:09:38 +0200692 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i started\n",
Michal Kazior1bbc0972014-04-08 09:45:47 +0300693 ar->monitor_vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300694 return 0;
695
696vdev_stop:
697 ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
698 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200699 ath10k_warn(ar, "failed to stop monitor vdev %i after start failure: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200700 ar->monitor_vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300701
702 return ret;
703}
704
Michal Kazior1bbc0972014-04-08 09:45:47 +0300705static int ath10k_monitor_vdev_stop(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300706{
707 int ret = 0;
708
709 lockdep_assert_held(&ar->conf_mutex);
710
Marek Puzyniak52fa0192013-09-24 14:06:24 +0200711 ret = ath10k_wmi_vdev_down(ar, ar->monitor_vdev_id);
712 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200713 ath10k_warn(ar, "failed to put down monitor vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200714 ar->monitor_vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300715
Michal Kazior7962b0d2014-10-28 10:34:38 +0100716 reinit_completion(&ar->vdev_setup_done);
717
Kalle Valo5e3dd152013-06-12 20:52:10 +0300718 ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
719 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200720 ath10k_warn(ar, "failed to to request monitor vdev %i stop: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200721 ar->monitor_vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300722
723 ret = ath10k_vdev_setup_sync(ar);
724 if (ret)
Ben Greear60028a82015-02-15 16:50:39 +0200725 ath10k_warn(ar, "failed to synchronize monitor vdev %i stop: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200726 ar->monitor_vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300727
Michal Kazior7aa7a722014-08-25 12:09:38 +0200728 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i stopped\n",
Michal Kazior1bbc0972014-04-08 09:45:47 +0300729 ar->monitor_vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300730 return ret;
731}
732
Michal Kazior1bbc0972014-04-08 09:45:47 +0300733static int ath10k_monitor_vdev_create(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300734{
735 int bit, ret = 0;
736
737 lockdep_assert_held(&ar->conf_mutex);
738
Ben Greeara9aefb32014-08-12 11:02:19 +0300739 if (ar->free_vdev_map == 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200740 ath10k_warn(ar, "failed to find free vdev id for monitor vdev\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +0300741 return -ENOMEM;
742 }
743
Ben Greear16c11172014-09-23 14:17:16 -0700744 bit = __ffs64(ar->free_vdev_map);
Ben Greeara9aefb32014-08-12 11:02:19 +0300745
Ben Greear16c11172014-09-23 14:17:16 -0700746 ar->monitor_vdev_id = bit;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300747
748 ret = ath10k_wmi_vdev_create(ar, ar->monitor_vdev_id,
749 WMI_VDEV_TYPE_MONITOR,
750 0, ar->mac_addr);
751 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200752 ath10k_warn(ar, "failed to request monitor vdev %i creation: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200753 ar->monitor_vdev_id, ret);
Ben Greeara9aefb32014-08-12 11:02:19 +0300754 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300755 }
756
Ben Greear16c11172014-09-23 14:17:16 -0700757 ar->free_vdev_map &= ~(1LL << ar->monitor_vdev_id);
Michal Kazior7aa7a722014-08-25 12:09:38 +0200758 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d created\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300759 ar->monitor_vdev_id);
760
Kalle Valo5e3dd152013-06-12 20:52:10 +0300761 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300762}
763
Michal Kazior1bbc0972014-04-08 09:45:47 +0300764static int ath10k_monitor_vdev_delete(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300765{
766 int ret = 0;
767
768 lockdep_assert_held(&ar->conf_mutex);
769
Kalle Valo5e3dd152013-06-12 20:52:10 +0300770 ret = ath10k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
771 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200772 ath10k_warn(ar, "failed to request wmi monitor vdev %i removal: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200773 ar->monitor_vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300774 return ret;
775 }
776
Ben Greear16c11172014-09-23 14:17:16 -0700777 ar->free_vdev_map |= 1LL << ar->monitor_vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300778
Michal Kazior7aa7a722014-08-25 12:09:38 +0200779 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d deleted\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300780 ar->monitor_vdev_id);
781 return ret;
782}
783
Michal Kazior1bbc0972014-04-08 09:45:47 +0300784static int ath10k_monitor_start(struct ath10k *ar)
785{
786 int ret;
787
788 lockdep_assert_held(&ar->conf_mutex);
789
Michal Kazior1bbc0972014-04-08 09:45:47 +0300790 ret = ath10k_monitor_vdev_create(ar);
791 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200792 ath10k_warn(ar, "failed to create monitor vdev: %d\n", ret);
Michal Kazior1bbc0972014-04-08 09:45:47 +0300793 return ret;
794 }
795
796 ret = ath10k_monitor_vdev_start(ar, ar->monitor_vdev_id);
797 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200798 ath10k_warn(ar, "failed to start monitor vdev: %d\n", ret);
Michal Kazior1bbc0972014-04-08 09:45:47 +0300799 ath10k_monitor_vdev_delete(ar);
800 return ret;
801 }
802
803 ar->monitor_started = true;
Michal Kazior7aa7a722014-08-25 12:09:38 +0200804 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor started\n");
Michal Kazior1bbc0972014-04-08 09:45:47 +0300805
806 return 0;
807}
808
Michal Kazior19337472014-08-28 12:58:16 +0200809static int ath10k_monitor_stop(struct ath10k *ar)
Michal Kazior1bbc0972014-04-08 09:45:47 +0300810{
811 int ret;
812
813 lockdep_assert_held(&ar->conf_mutex);
814
Michal Kazior1bbc0972014-04-08 09:45:47 +0300815 ret = ath10k_monitor_vdev_stop(ar);
Michal Kazior19337472014-08-28 12:58:16 +0200816 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200817 ath10k_warn(ar, "failed to stop monitor vdev: %d\n", ret);
Michal Kazior19337472014-08-28 12:58:16 +0200818 return ret;
819 }
Michal Kazior1bbc0972014-04-08 09:45:47 +0300820
821 ret = ath10k_monitor_vdev_delete(ar);
Michal Kazior19337472014-08-28 12:58:16 +0200822 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200823 ath10k_warn(ar, "failed to delete monitor vdev: %d\n", ret);
Michal Kazior19337472014-08-28 12:58:16 +0200824 return ret;
825 }
Michal Kazior1bbc0972014-04-08 09:45:47 +0300826
827 ar->monitor_started = false;
Michal Kazior7aa7a722014-08-25 12:09:38 +0200828 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopped\n");
Michal Kazior19337472014-08-28 12:58:16 +0200829
830 return 0;
831}
832
Vasanthakumar Thiagarajan54846212015-03-02 17:45:28 +0530833static bool ath10k_mac_should_disable_promisc(struct ath10k *ar)
834{
835 struct ath10k_vif *arvif;
836
837 if (!(ar->filter_flags & FIF_PROMISC_IN_BSS))
838 return true;
839
840 if (!ar->num_started_vdevs)
841 return false;
842
843 list_for_each_entry(arvif, &ar->arvifs, list)
844 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
845 return false;
846
847 ath10k_dbg(ar, ATH10K_DBG_MAC,
848 "mac disabling promiscuous mode because vdev is started\n");
849 return true;
850}
851
Michal Kazior19337472014-08-28 12:58:16 +0200852static int ath10k_monitor_recalc(struct ath10k *ar)
853{
854 bool should_start;
855
856 lockdep_assert_held(&ar->conf_mutex);
857
858 should_start = ar->monitor ||
Michal Kaziorbff414c2015-03-09 14:20:55 +0100859 !ath10k_mac_should_disable_promisc(ar) ||
Michal Kazior19337472014-08-28 12:58:16 +0200860 test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
861
862 ath10k_dbg(ar, ATH10K_DBG_MAC,
863 "mac monitor recalc started? %d should? %d\n",
864 ar->monitor_started, should_start);
865
866 if (should_start == ar->monitor_started)
867 return 0;
868
869 if (should_start)
870 return ath10k_monitor_start(ar);
Kalle Valod8bb26b2014-09-14 12:50:33 +0300871
872 return ath10k_monitor_stop(ar);
Michal Kazior1bbc0972014-04-08 09:45:47 +0300873}
874
Marek Kwaczynskie81bd102014-03-11 12:58:00 +0200875static int ath10k_recalc_rtscts_prot(struct ath10k_vif *arvif)
876{
877 struct ath10k *ar = arvif->ar;
878 u32 vdev_param, rts_cts = 0;
879
880 lockdep_assert_held(&ar->conf_mutex);
881
882 vdev_param = ar->wmi.vdev_param->enable_rtscts;
883
Rajkumar Manoharan9a5ab0f2015-03-19 16:03:29 +0200884 rts_cts |= SM(WMI_RTSCTS_ENABLED, WMI_RTSCTS_SET);
Marek Kwaczynskie81bd102014-03-11 12:58:00 +0200885
886 if (arvif->num_legacy_stations > 0)
887 rts_cts |= SM(WMI_RTSCTS_ACROSS_SW_RETRIES,
888 WMI_RTSCTS_PROFILE);
Rajkumar Manoharan9a5ab0f2015-03-19 16:03:29 +0200889 else
890 rts_cts |= SM(WMI_RTSCTS_FOR_SECOND_RATESERIES,
891 WMI_RTSCTS_PROFILE);
Marek Kwaczynskie81bd102014-03-11 12:58:00 +0200892
893 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
894 rts_cts);
895}
896
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200897static int ath10k_start_cac(struct ath10k *ar)
898{
899 int ret;
900
901 lockdep_assert_held(&ar->conf_mutex);
902
903 set_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
904
Michal Kazior19337472014-08-28 12:58:16 +0200905 ret = ath10k_monitor_recalc(ar);
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200906 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200907 ath10k_warn(ar, "failed to start monitor (cac): %d\n", ret);
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200908 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
909 return ret;
910 }
911
Michal Kazior7aa7a722014-08-25 12:09:38 +0200912 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac start monitor vdev %d\n",
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200913 ar->monitor_vdev_id);
914
915 return 0;
916}
917
918static int ath10k_stop_cac(struct ath10k *ar)
919{
920 lockdep_assert_held(&ar->conf_mutex);
921
922 /* CAC is not running - do nothing */
923 if (!test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags))
924 return 0;
925
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200926 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
Michal Kazior1bbc0972014-04-08 09:45:47 +0300927 ath10k_monitor_stop(ar);
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200928
Michal Kazior7aa7a722014-08-25 12:09:38 +0200929 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac finished\n");
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200930
931 return 0;
932}
933
Michal Kaziord6500972014-04-08 09:56:09 +0300934static void ath10k_recalc_radar_detection(struct ath10k *ar)
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200935{
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200936 int ret;
937
938 lockdep_assert_held(&ar->conf_mutex);
939
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200940 ath10k_stop_cac(ar);
941
Michal Kaziord6500972014-04-08 09:56:09 +0300942 if (!ar->radar_enabled)
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200943 return;
944
Michal Kaziord6500972014-04-08 09:56:09 +0300945 if (ar->num_started_vdevs > 0)
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200946 return;
947
948 ret = ath10k_start_cac(ar);
949 if (ret) {
950 /*
951 * Not possible to start CAC on current channel so starting
952 * radiation is not allowed, make this channel DFS_UNAVAILABLE
953 * by indicating that radar was detected.
954 */
Michal Kazior7aa7a722014-08-25 12:09:38 +0200955 ath10k_warn(ar, "failed to start CAC: %d\n", ret);
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200956 ieee80211_radar_detected(ar->hw);
957 }
958}
959
Vasanthakumar Thiagarajan822b7e02015-03-02 17:45:27 +0530960static int ath10k_vdev_stop(struct ath10k_vif *arvif)
961{
962 struct ath10k *ar = arvif->ar;
963 int ret;
964
965 lockdep_assert_held(&ar->conf_mutex);
966
967 reinit_completion(&ar->vdev_setup_done);
968
969 ret = ath10k_wmi_vdev_stop(ar, arvif->vdev_id);
970 if (ret) {
971 ath10k_warn(ar, "failed to stop WMI vdev %i: %d\n",
972 arvif->vdev_id, ret);
973 return ret;
974 }
975
976 ret = ath10k_vdev_setup_sync(ar);
977 if (ret) {
978 ath10k_warn(ar, "failed to syncronise setup for vdev %i: %d\n",
979 arvif->vdev_id, ret);
980 return ret;
981 }
982
983 WARN_ON(ar->num_started_vdevs == 0);
984
985 if (ar->num_started_vdevs != 0) {
986 ar->num_started_vdevs--;
987 ath10k_recalc_radar_detection(ar);
988 }
989
990 return ret;
991}
992
Michal Kaziordc55e302014-07-29 12:53:36 +0300993static int ath10k_vdev_start_restart(struct ath10k_vif *arvif, bool restart)
Michal Kazior72654fa2014-04-08 09:56:09 +0300994{
995 struct ath10k *ar = arvif->ar;
996 struct cfg80211_chan_def *chandef = &ar->chandef;
997 struct wmi_vdev_start_request_arg arg = {};
Vasanthakumar Thiagarajan54846212015-03-02 17:45:28 +0530998 int ret = 0, ret2;
Michal Kazior72654fa2014-04-08 09:56:09 +0300999
1000 lockdep_assert_held(&ar->conf_mutex);
1001
1002 reinit_completion(&ar->vdev_setup_done);
1003
1004 arg.vdev_id = arvif->vdev_id;
1005 arg.dtim_period = arvif->dtim_period;
1006 arg.bcn_intval = arvif->beacon_interval;
1007
1008 arg.channel.freq = chandef->chan->center_freq;
1009 arg.channel.band_center_freq1 = chandef->center_freq1;
1010 arg.channel.mode = chan_to_phymode(chandef);
1011
1012 arg.channel.min_power = 0;
1013 arg.channel.max_power = chandef->chan->max_power * 2;
1014 arg.channel.max_reg_power = chandef->chan->max_reg_power * 2;
1015 arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
1016
1017 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
1018 arg.ssid = arvif->u.ap.ssid;
1019 arg.ssid_len = arvif->u.ap.ssid_len;
1020 arg.hidden_ssid = arvif->u.ap.hidden_ssid;
1021
1022 /* For now allow DFS for AP mode */
1023 arg.channel.chan_radar =
1024 !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
1025 } else if (arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
1026 arg.ssid = arvif->vif->bss_conf.ssid;
1027 arg.ssid_len = arvif->vif->bss_conf.ssid_len;
1028 }
1029
Michal Kazior7aa7a722014-08-25 12:09:38 +02001030 ath10k_dbg(ar, ATH10K_DBG_MAC,
Michal Kazior72654fa2014-04-08 09:56:09 +03001031 "mac vdev %d start center_freq %d phymode %s\n",
1032 arg.vdev_id, arg.channel.freq,
1033 ath10k_wmi_phymode_str(arg.channel.mode));
1034
Michal Kaziordc55e302014-07-29 12:53:36 +03001035 if (restart)
1036 ret = ath10k_wmi_vdev_restart(ar, &arg);
1037 else
1038 ret = ath10k_wmi_vdev_start(ar, &arg);
1039
Michal Kazior72654fa2014-04-08 09:56:09 +03001040 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001041 ath10k_warn(ar, "failed to start WMI vdev %i: %d\n",
Michal Kazior72654fa2014-04-08 09:56:09 +03001042 arg.vdev_id, ret);
1043 return ret;
1044 }
1045
1046 ret = ath10k_vdev_setup_sync(ar);
1047 if (ret) {
Ben Greear60028a82015-02-15 16:50:39 +02001048 ath10k_warn(ar,
1049 "failed to synchronize setup for vdev %i restart %d: %d\n",
1050 arg.vdev_id, restart, ret);
Michal Kazior72654fa2014-04-08 09:56:09 +03001051 return ret;
1052 }
1053
Michal Kaziord6500972014-04-08 09:56:09 +03001054 ar->num_started_vdevs++;
1055 ath10k_recalc_radar_detection(ar);
1056
Vasanthakumar Thiagarajan54846212015-03-02 17:45:28 +05301057 ret = ath10k_monitor_recalc(ar);
1058 if (ret) {
1059 ath10k_warn(ar, "mac failed to recalc monitor for vdev %i restart %d: %d\n",
1060 arg.vdev_id, restart, ret);
1061 ret2 = ath10k_vdev_stop(arvif);
1062 if (ret2)
1063 ath10k_warn(ar, "mac failed to stop vdev %i restart %d: %d\n",
1064 arg.vdev_id, restart, ret2);
1065 }
1066
Michal Kazior72654fa2014-04-08 09:56:09 +03001067 return ret;
1068}
1069
Michal Kaziordc55e302014-07-29 12:53:36 +03001070static int ath10k_vdev_start(struct ath10k_vif *arvif)
1071{
1072 return ath10k_vdev_start_restart(arvif, false);
1073}
1074
1075static int ath10k_vdev_restart(struct ath10k_vif *arvif)
1076{
1077 return ath10k_vdev_start_restart(arvif, true);
1078}
1079
Michal Kaziorfbb8f1b2015-01-13 16:30:12 +02001080static int ath10k_mac_setup_bcn_p2p_ie(struct ath10k_vif *arvif,
1081 struct sk_buff *bcn)
1082{
1083 struct ath10k *ar = arvif->ar;
1084 struct ieee80211_mgmt *mgmt;
1085 const u8 *p2p_ie;
1086 int ret;
1087
1088 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1089 return 0;
1090
1091 if (arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
1092 return 0;
1093
1094 mgmt = (void *)bcn->data;
1095 p2p_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1096 mgmt->u.beacon.variable,
1097 bcn->len - (mgmt->u.beacon.variable -
1098 bcn->data));
1099 if (!p2p_ie)
1100 return -ENOENT;
1101
1102 ret = ath10k_wmi_p2p_go_bcn_ie(ar, arvif->vdev_id, p2p_ie);
1103 if (ret) {
1104 ath10k_warn(ar, "failed to submit p2p go bcn ie for vdev %i: %d\n",
1105 arvif->vdev_id, ret);
1106 return ret;
1107 }
1108
1109 return 0;
1110}
1111
1112static int ath10k_mac_remove_vendor_ie(struct sk_buff *skb, unsigned int oui,
1113 u8 oui_type, size_t ie_offset)
1114{
1115 size_t len;
1116 const u8 *next;
1117 const u8 *end;
1118 u8 *ie;
1119
1120 if (WARN_ON(skb->len < ie_offset))
1121 return -EINVAL;
1122
1123 ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
1124 skb->data + ie_offset,
1125 skb->len - ie_offset);
1126 if (!ie)
1127 return -ENOENT;
1128
1129 len = ie[1] + 2;
1130 end = skb->data + skb->len;
1131 next = ie + len;
1132
1133 if (WARN_ON(next > end))
1134 return -EINVAL;
1135
1136 memmove(ie, next, end - next);
1137 skb_trim(skb, skb->len - len);
1138
1139 return 0;
1140}
1141
1142static int ath10k_mac_setup_bcn_tmpl(struct ath10k_vif *arvif)
1143{
1144 struct ath10k *ar = arvif->ar;
1145 struct ieee80211_hw *hw = ar->hw;
1146 struct ieee80211_vif *vif = arvif->vif;
1147 struct ieee80211_mutable_offsets offs = {};
1148 struct sk_buff *bcn;
1149 int ret;
1150
1151 if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1152 return 0;
1153
Michal Kazior81a9a172015-03-05 16:02:17 +02001154 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
1155 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
1156 return 0;
1157
Michal Kaziorfbb8f1b2015-01-13 16:30:12 +02001158 bcn = ieee80211_beacon_get_template(hw, vif, &offs);
1159 if (!bcn) {
1160 ath10k_warn(ar, "failed to get beacon template from mac80211\n");
1161 return -EPERM;
1162 }
1163
1164 ret = ath10k_mac_setup_bcn_p2p_ie(arvif, bcn);
1165 if (ret) {
1166 ath10k_warn(ar, "failed to setup p2p go bcn ie: %d\n", ret);
1167 kfree_skb(bcn);
1168 return ret;
1169 }
1170
1171 /* P2P IE is inserted by firmware automatically (as configured above)
1172 * so remove it from the base beacon template to avoid duplicate P2P
1173 * IEs in beacon frames.
1174 */
1175 ath10k_mac_remove_vendor_ie(bcn, WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1176 offsetof(struct ieee80211_mgmt,
1177 u.beacon.variable));
1178
1179 ret = ath10k_wmi_bcn_tmpl(ar, arvif->vdev_id, offs.tim_offset, bcn, 0,
1180 0, NULL, 0);
1181 kfree_skb(bcn);
1182
1183 if (ret) {
1184 ath10k_warn(ar, "failed to submit beacon template command: %d\n",
1185 ret);
1186 return ret;
1187 }
1188
1189 return 0;
1190}
1191
1192static int ath10k_mac_setup_prb_tmpl(struct ath10k_vif *arvif)
1193{
1194 struct ath10k *ar = arvif->ar;
1195 struct ieee80211_hw *hw = ar->hw;
1196 struct ieee80211_vif *vif = arvif->vif;
1197 struct sk_buff *prb;
1198 int ret;
1199
1200 if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1201 return 0;
1202
Michal Kazior81a9a172015-03-05 16:02:17 +02001203 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1204 return 0;
1205
Michal Kaziorfbb8f1b2015-01-13 16:30:12 +02001206 prb = ieee80211_proberesp_get(hw, vif);
1207 if (!prb) {
1208 ath10k_warn(ar, "failed to get probe resp template from mac80211\n");
1209 return -EPERM;
1210 }
1211
1212 ret = ath10k_wmi_prb_tmpl(ar, arvif->vdev_id, prb);
1213 kfree_skb(prb);
1214
1215 if (ret) {
1216 ath10k_warn(ar, "failed to submit probe resp template command: %d\n",
1217 ret);
1218 return ret;
1219 }
1220
1221 return 0;
1222}
1223
Kalle Valo5e3dd152013-06-12 20:52:10 +03001224static void ath10k_control_beaconing(struct ath10k_vif *arvif,
Kalle Valo5b07e072014-09-14 12:50:06 +03001225 struct ieee80211_bss_conf *info)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001226{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001227 struct ath10k *ar = arvif->ar;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001228 int ret = 0;
1229
Michal Kazior548db542013-07-05 16:15:15 +03001230 lockdep_assert_held(&arvif->ar->conf_mutex);
1231
Kalle Valo5e3dd152013-06-12 20:52:10 +03001232 if (!info->enable_beacon) {
1233 ath10k_vdev_stop(arvif);
Michal Kaziorc930f742014-01-23 11:38:25 +01001234
1235 arvif->is_started = false;
1236 arvif->is_up = false;
Michal Kazior8513d952015-03-09 14:19:24 +01001237
1238 spin_lock_bh(&arvif->ar->data_lock);
Michal Kazior64badcb2014-09-18 11:18:02 +03001239 ath10k_mac_vif_beacon_free(arvif);
Michal Kazior748afc42014-01-23 12:48:21 +01001240 spin_unlock_bh(&arvif->ar->data_lock);
1241
Kalle Valo5e3dd152013-06-12 20:52:10 +03001242 return;
1243 }
1244
1245 arvif->tx_seq_no = 0x1000;
1246
1247 ret = ath10k_vdev_start(arvif);
1248 if (ret)
1249 return;
1250
Michal Kaziorc930f742014-01-23 11:38:25 +01001251 arvif->aid = 0;
Kalle Valob25f32c2014-09-14 12:50:49 +03001252 ether_addr_copy(arvif->bssid, info->bssid);
Michal Kaziorc930f742014-01-23 11:38:25 +01001253
1254 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
1255 arvif->bssid);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001256 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001257 ath10k_warn(ar, "failed to bring up vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02001258 arvif->vdev_id, ret);
Michal Kaziorc930f742014-01-23 11:38:25 +01001259 ath10k_vdev_stop(arvif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001260 return;
1261 }
Michal Kaziorc930f742014-01-23 11:38:25 +01001262
1263 arvif->is_started = true;
1264 arvif->is_up = true;
1265
Michal Kazior7aa7a722014-08-25 12:09:38 +02001266 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001267}
1268
1269static void ath10k_control_ibss(struct ath10k_vif *arvif,
1270 struct ieee80211_bss_conf *info,
1271 const u8 self_peer[ETH_ALEN])
1272{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001273 struct ath10k *ar = arvif->ar;
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02001274 u32 vdev_param;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001275 int ret = 0;
1276
Michal Kazior548db542013-07-05 16:15:15 +03001277 lockdep_assert_held(&arvif->ar->conf_mutex);
1278
Kalle Valo5e3dd152013-06-12 20:52:10 +03001279 if (!info->ibss_joined) {
1280 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, self_peer);
1281 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001282 ath10k_warn(ar, "failed to delete IBSS self peer %pM for vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001283 self_peer, arvif->vdev_id, ret);
1284
Michal Kaziorc930f742014-01-23 11:38:25 +01001285 if (is_zero_ether_addr(arvif->bssid))
Kalle Valo5e3dd152013-06-12 20:52:10 +03001286 return;
1287
Michal Kaziorc930f742014-01-23 11:38:25 +01001288 memset(arvif->bssid, 0, ETH_ALEN);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001289
1290 return;
1291 }
1292
1293 ret = ath10k_peer_create(arvif->ar, arvif->vdev_id, self_peer);
1294 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001295 ath10k_warn(ar, "failed to create IBSS self peer %pM for vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001296 self_peer, arvif->vdev_id, ret);
1297 return;
1298 }
1299
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02001300 vdev_param = arvif->ar->wmi.vdev_param->atim_window;
1301 ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001302 ATH10K_DEFAULT_ATIM);
1303 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001304 ath10k_warn(ar, "failed to set IBSS ATIM for vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001305 arvif->vdev_id, ret);
1306}
1307
Michal Kazior9f9b5742014-12-12 12:41:36 +01001308static int ath10k_mac_vif_recalc_ps_wake_threshold(struct ath10k_vif *arvif)
1309{
1310 struct ath10k *ar = arvif->ar;
1311 u32 param;
1312 u32 value;
1313 int ret;
1314
1315 lockdep_assert_held(&arvif->ar->conf_mutex);
1316
1317 if (arvif->u.sta.uapsd)
1318 value = WMI_STA_PS_TX_WAKE_THRESHOLD_NEVER;
1319 else
1320 value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
1321
1322 param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
1323 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param, value);
1324 if (ret) {
1325 ath10k_warn(ar, "failed to submit ps wake threshold %u on vdev %i: %d\n",
1326 value, arvif->vdev_id, ret);
1327 return ret;
1328 }
1329
1330 return 0;
1331}
1332
1333static int ath10k_mac_vif_recalc_ps_poll_count(struct ath10k_vif *arvif)
1334{
1335 struct ath10k *ar = arvif->ar;
1336 u32 param;
1337 u32 value;
1338 int ret;
1339
1340 lockdep_assert_held(&arvif->ar->conf_mutex);
1341
1342 if (arvif->u.sta.uapsd)
1343 value = WMI_STA_PS_PSPOLL_COUNT_UAPSD;
1344 else
1345 value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
1346
1347 param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
1348 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
1349 param, value);
1350 if (ret) {
1351 ath10k_warn(ar, "failed to submit ps poll count %u on vdev %i: %d\n",
1352 value, arvif->vdev_id, ret);
1353 return ret;
1354 }
1355
1356 return 0;
1357}
1358
Michal Kaziorcffb41f2015-02-13 13:30:16 +01001359static int ath10k_mac_ps_vif_count(struct ath10k *ar)
1360{
1361 struct ath10k_vif *arvif;
1362 int num = 0;
1363
1364 lockdep_assert_held(&ar->conf_mutex);
1365
1366 list_for_each_entry(arvif, &ar->arvifs, list)
1367 if (arvif->ps)
1368 num++;
1369
1370 return num;
1371}
1372
Michal Kaziorad088bf2013-10-16 15:44:46 +03001373static int ath10k_mac_vif_setup_ps(struct ath10k_vif *arvif)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001374{
Michal Kaziorad088bf2013-10-16 15:44:46 +03001375 struct ath10k *ar = arvif->ar;
Michal Kazior526549a2014-12-12 12:41:37 +01001376 struct ieee80211_vif *vif = arvif->vif;
Michal Kaziorad088bf2013-10-16 15:44:46 +03001377 struct ieee80211_conf *conf = &ar->hw->conf;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001378 enum wmi_sta_powersave_param param;
1379 enum wmi_sta_ps_mode psmode;
1380 int ret;
Michal Kazior526549a2014-12-12 12:41:37 +01001381 int ps_timeout;
Michal Kaziorcffb41f2015-02-13 13:30:16 +01001382 bool enable_ps;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001383
Michal Kazior548db542013-07-05 16:15:15 +03001384 lockdep_assert_held(&arvif->ar->conf_mutex);
1385
Michal Kaziorad088bf2013-10-16 15:44:46 +03001386 if (arvif->vif->type != NL80211_IFTYPE_STATION)
1387 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001388
Michal Kaziorcffb41f2015-02-13 13:30:16 +01001389 enable_ps = arvif->ps;
1390
1391 if (enable_ps && ath10k_mac_ps_vif_count(ar) > 1 &&
1392 !test_bit(ATH10K_FW_FEATURE_MULTI_VIF_PS_SUPPORT,
1393 ar->fw_features)) {
1394 ath10k_warn(ar, "refusing to enable ps on vdev %i: not supported by fw\n",
1395 arvif->vdev_id);
1396 enable_ps = false;
1397 }
1398
1399 if (enable_ps) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001400 psmode = WMI_STA_PS_MODE_ENABLED;
1401 param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
1402
Michal Kazior526549a2014-12-12 12:41:37 +01001403 ps_timeout = conf->dynamic_ps_timeout;
1404 if (ps_timeout == 0) {
1405 /* Firmware doesn't like 0 */
1406 ps_timeout = ieee80211_tu_to_usec(
1407 vif->bss_conf.beacon_int) / 1000;
1408 }
1409
Michal Kaziorad088bf2013-10-16 15:44:46 +03001410 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
Michal Kazior526549a2014-12-12 12:41:37 +01001411 ps_timeout);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001412 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001413 ath10k_warn(ar, "failed to set inactivity time for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02001414 arvif->vdev_id, ret);
Michal Kaziorad088bf2013-10-16 15:44:46 +03001415 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001416 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001417 } else {
1418 psmode = WMI_STA_PS_MODE_DISABLED;
1419 }
1420
Michal Kazior7aa7a722014-08-25 12:09:38 +02001421 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d psmode %s\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03001422 arvif->vdev_id, psmode ? "enable" : "disable");
1423
Michal Kaziorad088bf2013-10-16 15:44:46 +03001424 ret = ath10k_wmi_set_psmode(ar, arvif->vdev_id, psmode);
1425 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001426 ath10k_warn(ar, "failed to set PS Mode %d for vdev %d: %d\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02001427 psmode, arvif->vdev_id, ret);
Michal Kaziorad088bf2013-10-16 15:44:46 +03001428 return ret;
1429 }
1430
1431 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001432}
1433
Michal Kazior46725b12015-01-28 09:57:49 +02001434static int ath10k_mac_vif_disable_keepalive(struct ath10k_vif *arvif)
1435{
1436 struct ath10k *ar = arvif->ar;
1437 struct wmi_sta_keepalive_arg arg = {};
1438 int ret;
1439
1440 lockdep_assert_held(&arvif->ar->conf_mutex);
1441
1442 if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
1443 return 0;
1444
1445 if (!test_bit(WMI_SERVICE_STA_KEEP_ALIVE, ar->wmi.svc_map))
1446 return 0;
1447
1448 /* Some firmware revisions have a bug and ignore the `enabled` field.
1449 * Instead use the interval to disable the keepalive.
1450 */
1451 arg.vdev_id = arvif->vdev_id;
1452 arg.enabled = 1;
1453 arg.method = WMI_STA_KEEPALIVE_METHOD_NULL_FRAME;
1454 arg.interval = WMI_STA_KEEPALIVE_INTERVAL_DISABLE;
1455
1456 ret = ath10k_wmi_sta_keepalive(ar, &arg);
1457 if (ret) {
1458 ath10k_warn(ar, "failed to submit keepalive on vdev %i: %d\n",
1459 arvif->vdev_id, ret);
1460 return ret;
1461 }
1462
1463 return 0;
1464}
1465
Michal Kazior81a9a172015-03-05 16:02:17 +02001466static void ath10k_mac_vif_ap_csa_count_down(struct ath10k_vif *arvif)
1467{
1468 struct ath10k *ar = arvif->ar;
1469 struct ieee80211_vif *vif = arvif->vif;
1470 int ret;
1471
Michal Kazior8513d952015-03-09 14:19:24 +01001472 lockdep_assert_held(&arvif->ar->conf_mutex);
1473
1474 if (WARN_ON(!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)))
1475 return;
1476
Michal Kazior81a9a172015-03-05 16:02:17 +02001477 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1478 return;
1479
1480 if (!vif->csa_active)
1481 return;
1482
1483 if (!arvif->is_up)
1484 return;
1485
1486 if (!ieee80211_csa_is_complete(vif)) {
1487 ieee80211_csa_update_counter(vif);
1488
1489 ret = ath10k_mac_setup_bcn_tmpl(arvif);
1490 if (ret)
1491 ath10k_warn(ar, "failed to update bcn tmpl during csa: %d\n",
1492 ret);
1493
1494 ret = ath10k_mac_setup_prb_tmpl(arvif);
1495 if (ret)
1496 ath10k_warn(ar, "failed to update prb tmpl during csa: %d\n",
1497 ret);
1498 } else {
1499 ieee80211_csa_finish(vif);
1500 }
1501}
1502
1503static void ath10k_mac_vif_ap_csa_work(struct work_struct *work)
1504{
1505 struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
1506 ap_csa_work);
1507 struct ath10k *ar = arvif->ar;
1508
1509 mutex_lock(&ar->conf_mutex);
1510 ath10k_mac_vif_ap_csa_count_down(arvif);
1511 mutex_unlock(&ar->conf_mutex);
1512}
1513
Michal Kaziorcc9904e2015-03-10 16:22:01 +02001514static void ath10k_mac_handle_beacon_iter(void *data, u8 *mac,
1515 struct ieee80211_vif *vif)
1516{
1517 struct sk_buff *skb = data;
1518 struct ieee80211_mgmt *mgmt = (void *)skb->data;
1519 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1520
1521 if (vif->type != NL80211_IFTYPE_STATION)
1522 return;
1523
1524 if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
1525 return;
1526
1527 cancel_delayed_work(&arvif->connection_loss_work);
1528}
1529
1530void ath10k_mac_handle_beacon(struct ath10k *ar, struct sk_buff *skb)
1531{
1532 ieee80211_iterate_active_interfaces_atomic(ar->hw,
1533 IEEE80211_IFACE_ITER_NORMAL,
1534 ath10k_mac_handle_beacon_iter,
1535 skb);
1536}
1537
1538static void ath10k_mac_handle_beacon_miss_iter(void *data, u8 *mac,
1539 struct ieee80211_vif *vif)
1540{
1541 u32 *vdev_id = data;
1542 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1543 struct ath10k *ar = arvif->ar;
1544 struct ieee80211_hw *hw = ar->hw;
1545
1546 if (arvif->vdev_id != *vdev_id)
1547 return;
1548
1549 if (!arvif->is_up)
1550 return;
1551
1552 ieee80211_beacon_loss(vif);
1553
1554 /* Firmware doesn't report beacon loss events repeatedly. If AP probe
1555 * (done by mac80211) succeeds but beacons do not resume then it
1556 * doesn't make sense to continue operation. Queue connection loss work
1557 * which can be cancelled when beacon is received.
1558 */
1559 ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
1560 ATH10K_CONNECTION_LOSS_HZ);
1561}
1562
1563void ath10k_mac_handle_beacon_miss(struct ath10k *ar, u32 vdev_id)
1564{
1565 ieee80211_iterate_active_interfaces_atomic(ar->hw,
1566 IEEE80211_IFACE_ITER_NORMAL,
1567 ath10k_mac_handle_beacon_miss_iter,
1568 &vdev_id);
1569}
1570
1571static void ath10k_mac_vif_sta_connection_loss_work(struct work_struct *work)
1572{
1573 struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
1574 connection_loss_work.work);
1575 struct ieee80211_vif *vif = arvif->vif;
1576
1577 if (!arvif->is_up)
1578 return;
1579
1580 ieee80211_connection_loss(vif);
1581}
1582
Kalle Valo5e3dd152013-06-12 20:52:10 +03001583/**********************/
1584/* Station management */
1585/**********************/
1586
Michal Kazior590922a2014-10-21 10:10:29 +03001587static u32 ath10k_peer_assoc_h_listen_intval(struct ath10k *ar,
1588 struct ieee80211_vif *vif)
1589{
1590 /* Some firmware revisions have unstable STA powersave when listen
1591 * interval is set too high (e.g. 5). The symptoms are firmware doesn't
1592 * generate NullFunc frames properly even if buffered frames have been
1593 * indicated in Beacon TIM. Firmware would seldom wake up to pull
1594 * buffered frames. Often pinging the device from AP would simply fail.
1595 *
1596 * As a workaround set it to 1.
1597 */
1598 if (vif->type == NL80211_IFTYPE_STATION)
1599 return 1;
1600
1601 return ar->hw->conf.listen_interval;
1602}
1603
Kalle Valo5e3dd152013-06-12 20:52:10 +03001604static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001605 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001606 struct ieee80211_sta *sta,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001607 struct wmi_peer_assoc_complete_arg *arg)
1608{
Michal Kazior590922a2014-10-21 10:10:29 +03001609 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1610
Michal Kazior548db542013-07-05 16:15:15 +03001611 lockdep_assert_held(&ar->conf_mutex);
1612
Kalle Valob25f32c2014-09-14 12:50:49 +03001613 ether_addr_copy(arg->addr, sta->addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001614 arg->vdev_id = arvif->vdev_id;
1615 arg->peer_aid = sta->aid;
1616 arg->peer_flags |= WMI_PEER_AUTH;
Michal Kazior590922a2014-10-21 10:10:29 +03001617 arg->peer_listen_intval = ath10k_peer_assoc_h_listen_intval(ar, vif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001618 arg->peer_num_spatial_streams = 1;
Michal Kazior590922a2014-10-21 10:10:29 +03001619 arg->peer_caps = vif->bss_conf.assoc_capability;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001620}
1621
1622static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001623 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001624 struct wmi_peer_assoc_complete_arg *arg)
1625{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001626 struct ieee80211_bss_conf *info = &vif->bss_conf;
1627 struct cfg80211_bss *bss;
1628 const u8 *rsnie = NULL;
1629 const u8 *wpaie = NULL;
1630
Michal Kazior548db542013-07-05 16:15:15 +03001631 lockdep_assert_held(&ar->conf_mutex);
1632
Kalle Valo5e3dd152013-06-12 20:52:10 +03001633 bss = cfg80211_get_bss(ar->hw->wiphy, ar->hw->conf.chandef.chan,
1634 info->bssid, NULL, 0, 0, 0);
1635 if (bss) {
1636 const struct cfg80211_bss_ies *ies;
1637
1638 rcu_read_lock();
1639 rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
1640
1641 ies = rcu_dereference(bss->ies);
1642
1643 wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
Kalle Valo5b07e072014-09-14 12:50:06 +03001644 WLAN_OUI_TYPE_MICROSOFT_WPA,
1645 ies->data,
1646 ies->len);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001647 rcu_read_unlock();
1648 cfg80211_put_bss(ar->hw->wiphy, bss);
1649 }
1650
1651 /* FIXME: base on RSN IE/WPA IE is a correct idea? */
1652 if (rsnie || wpaie) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001653 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001654 arg->peer_flags |= WMI_PEER_NEED_PTK_4_WAY;
1655 }
1656
1657 if (wpaie) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001658 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: wpa ie found\n", __func__);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001659 arg->peer_flags |= WMI_PEER_NEED_GTK_2_WAY;
1660 }
1661}
1662
1663static void ath10k_peer_assoc_h_rates(struct ath10k *ar,
1664 struct ieee80211_sta *sta,
1665 struct wmi_peer_assoc_complete_arg *arg)
1666{
1667 struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
1668 const struct ieee80211_supported_band *sband;
1669 const struct ieee80211_rate *rates;
1670 u32 ratemask;
1671 int i;
1672
Michal Kazior548db542013-07-05 16:15:15 +03001673 lockdep_assert_held(&ar->conf_mutex);
1674
Kalle Valo5e3dd152013-06-12 20:52:10 +03001675 sband = ar->hw->wiphy->bands[ar->hw->conf.chandef.chan->band];
1676 ratemask = sta->supp_rates[ar->hw->conf.chandef.chan->band];
1677 rates = sband->bitrates;
1678
1679 rateset->num_rates = 0;
1680
1681 for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
1682 if (!(ratemask & 1))
1683 continue;
1684
1685 rateset->rates[rateset->num_rates] = rates->hw_value;
1686 rateset->num_rates++;
1687 }
1688}
1689
1690static void ath10k_peer_assoc_h_ht(struct ath10k *ar,
1691 struct ieee80211_sta *sta,
1692 struct wmi_peer_assoc_complete_arg *arg)
1693{
1694 const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001695 int i, n;
Kalle Valoaf762c02014-09-14 12:50:17 +03001696 u32 stbc;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001697
Michal Kazior548db542013-07-05 16:15:15 +03001698 lockdep_assert_held(&ar->conf_mutex);
1699
Kalle Valo5e3dd152013-06-12 20:52:10 +03001700 if (!ht_cap->ht_supported)
1701 return;
1702
1703 arg->peer_flags |= WMI_PEER_HT;
1704 arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1705 ht_cap->ampdu_factor)) - 1;
1706
1707 arg->peer_mpdu_density =
1708 ath10k_parse_mpdudensity(ht_cap->ampdu_density);
1709
1710 arg->peer_ht_caps = ht_cap->cap;
1711 arg->peer_rate_caps |= WMI_RC_HT_FLAG;
1712
1713 if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
1714 arg->peer_flags |= WMI_PEER_LDPC;
1715
1716 if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
1717 arg->peer_flags |= WMI_PEER_40MHZ;
1718 arg->peer_rate_caps |= WMI_RC_CW40_FLAG;
1719 }
1720
1721 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
1722 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
1723
1724 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
1725 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
1726
1727 if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
1728 arg->peer_rate_caps |= WMI_RC_TX_STBC_FLAG;
1729 arg->peer_flags |= WMI_PEER_STBC;
1730 }
1731
1732 if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001733 stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
1734 stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
1735 stbc = stbc << WMI_RC_RX_STBC_FLAG_S;
1736 arg->peer_rate_caps |= stbc;
1737 arg->peer_flags |= WMI_PEER_STBC;
1738 }
1739
Kalle Valo5e3dd152013-06-12 20:52:10 +03001740 if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
1741 arg->peer_rate_caps |= WMI_RC_TS_FLAG;
1742 else if (ht_cap->mcs.rx_mask[1])
1743 arg->peer_rate_caps |= WMI_RC_DS_FLAG;
1744
1745 for (i = 0, n = 0; i < IEEE80211_HT_MCS_MASK_LEN*8; i++)
1746 if (ht_cap->mcs.rx_mask[i/8] & (1 << i%8))
1747 arg->peer_ht_rates.rates[n++] = i;
1748
Bartosz Markowskifd71f802014-02-10 13:12:55 +01001749 /*
1750 * This is a workaround for HT-enabled STAs which break the spec
1751 * and have no HT capabilities RX mask (no HT RX MCS map).
1752 *
1753 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
1754 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
1755 *
1756 * Firmware asserts if such situation occurs.
1757 */
1758 if (n == 0) {
1759 arg->peer_ht_rates.num_rates = 8;
1760 for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
1761 arg->peer_ht_rates.rates[i] = i;
1762 } else {
1763 arg->peer_ht_rates.num_rates = n;
1764 arg->peer_num_spatial_streams = sta->rx_nss;
1765 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001766
Michal Kazior7aa7a722014-08-25 12:09:38 +02001767 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03001768 arg->addr,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001769 arg->peer_ht_rates.num_rates,
1770 arg->peer_num_spatial_streams);
1771}
1772
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001773static int ath10k_peer_assoc_qos_ap(struct ath10k *ar,
1774 struct ath10k_vif *arvif,
1775 struct ieee80211_sta *sta)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001776{
1777 u32 uapsd = 0;
1778 u32 max_sp = 0;
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001779 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001780
Michal Kazior548db542013-07-05 16:15:15 +03001781 lockdep_assert_held(&ar->conf_mutex);
1782
Kalle Valo5e3dd152013-06-12 20:52:10 +03001783 if (sta->wme && sta->uapsd_queues) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001784 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001785 sta->uapsd_queues, sta->max_sp);
1786
Kalle Valo5e3dd152013-06-12 20:52:10 +03001787 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1788 uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
1789 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
1790 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1791 uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
1792 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
1793 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1794 uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
1795 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
1796 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1797 uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
1798 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
1799
Kalle Valo5e3dd152013-06-12 20:52:10 +03001800 if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
1801 max_sp = sta->max_sp;
1802
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001803 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1804 sta->addr,
1805 WMI_AP_PS_PEER_PARAM_UAPSD,
1806 uapsd);
1807 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001808 ath10k_warn(ar, "failed to set ap ps peer param uapsd for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001809 arvif->vdev_id, ret);
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001810 return ret;
1811 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001812
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001813 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1814 sta->addr,
1815 WMI_AP_PS_PEER_PARAM_MAX_SP,
1816 max_sp);
1817 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001818 ath10k_warn(ar, "failed to set ap ps peer param max sp for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001819 arvif->vdev_id, ret);
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001820 return ret;
1821 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001822
1823 /* TODO setup this based on STA listen interval and
1824 beacon interval. Currently we don't know
1825 sta->listen_interval - mac80211 patch required.
1826 Currently use 10 seconds */
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001827 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id, sta->addr,
Kalle Valo5b07e072014-09-14 12:50:06 +03001828 WMI_AP_PS_PEER_PARAM_AGEOUT_TIME,
1829 10);
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001830 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001831 ath10k_warn(ar, "failed to set ap ps peer param ageout time for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001832 arvif->vdev_id, ret);
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001833 return ret;
1834 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001835 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001836
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001837 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001838}
1839
1840static void ath10k_peer_assoc_h_vht(struct ath10k *ar,
1841 struct ieee80211_sta *sta,
1842 struct wmi_peer_assoc_complete_arg *arg)
1843{
1844 const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
Sujith Manoharana24b88b2013-10-07 19:51:57 -07001845 u8 ampdu_factor;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001846
1847 if (!vht_cap->vht_supported)
1848 return;
1849
1850 arg->peer_flags |= WMI_PEER_VHT;
Yanbo Lid68bb122015-01-23 08:18:20 +08001851
1852 if (ar->hw->conf.chandef.chan->band == IEEE80211_BAND_2GHZ)
1853 arg->peer_flags |= WMI_PEER_VHT_2G;
1854
Kalle Valo5e3dd152013-06-12 20:52:10 +03001855 arg->peer_vht_caps = vht_cap->cap;
1856
Sujith Manoharana24b88b2013-10-07 19:51:57 -07001857 ampdu_factor = (vht_cap->cap &
1858 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
1859 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
1860
1861 /* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
1862 * zero in VHT IE. Using it would result in degraded throughput.
1863 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
1864 * it if VHT max_mpdu is smaller. */
1865 arg->peer_max_mpdu = max(arg->peer_max_mpdu,
1866 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1867 ampdu_factor)) - 1);
1868
Kalle Valo5e3dd152013-06-12 20:52:10 +03001869 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1870 arg->peer_flags |= WMI_PEER_80MHZ;
1871
1872 arg->peer_vht_rates.rx_max_rate =
1873 __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
1874 arg->peer_vht_rates.rx_mcs_set =
1875 __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
1876 arg->peer_vht_rates.tx_max_rate =
1877 __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
1878 arg->peer_vht_rates.tx_mcs_set =
1879 __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
1880
Michal Kazior7aa7a722014-08-25 12:09:38 +02001881 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03001882 sta->addr, arg->peer_max_mpdu, arg->peer_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001883}
1884
1885static void ath10k_peer_assoc_h_qos(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001886 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001887 struct ieee80211_sta *sta,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001888 struct wmi_peer_assoc_complete_arg *arg)
1889{
Michal Kazior590922a2014-10-21 10:10:29 +03001890 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1891
Kalle Valo5e3dd152013-06-12 20:52:10 +03001892 switch (arvif->vdev_type) {
1893 case WMI_VDEV_TYPE_AP:
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001894 if (sta->wme)
1895 arg->peer_flags |= WMI_PEER_QOS;
1896
1897 if (sta->wme && sta->uapsd_queues) {
1898 arg->peer_flags |= WMI_PEER_APSD;
1899 arg->peer_rate_caps |= WMI_RC_UAPSD_FLAG;
1900 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001901 break;
1902 case WMI_VDEV_TYPE_STA:
Michal Kazior590922a2014-10-21 10:10:29 +03001903 if (vif->bss_conf.qos)
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001904 arg->peer_flags |= WMI_PEER_QOS;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001905 break;
Janusz Dziedzic627d9842014-12-17 12:29:54 +02001906 case WMI_VDEV_TYPE_IBSS:
1907 if (sta->wme)
1908 arg->peer_flags |= WMI_PEER_QOS;
1909 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001910 default:
1911 break;
1912 }
Janusz Dziedzic627d9842014-12-17 12:29:54 +02001913
1914 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM qos %d\n",
1915 sta->addr, !!(arg->peer_flags & WMI_PEER_QOS));
Kalle Valo5e3dd152013-06-12 20:52:10 +03001916}
1917
Michal Kazior91b12082014-12-12 12:41:35 +01001918static bool ath10k_mac_sta_has_11g_rates(struct ieee80211_sta *sta)
1919{
1920 /* First 4 rates in ath10k_rates are CCK (11b) rates. */
1921 return sta->supp_rates[IEEE80211_BAND_2GHZ] >> 4;
1922}
1923
Kalle Valo5e3dd152013-06-12 20:52:10 +03001924static void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001925 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001926 struct ieee80211_sta *sta,
1927 struct wmi_peer_assoc_complete_arg *arg)
1928{
1929 enum wmi_phy_mode phymode = MODE_UNKNOWN;
1930
Kalle Valo5e3dd152013-06-12 20:52:10 +03001931 switch (ar->hw->conf.chandef.chan->band) {
1932 case IEEE80211_BAND_2GHZ:
Yanbo Lid68bb122015-01-23 08:18:20 +08001933 if (sta->vht_cap.vht_supported) {
1934 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1935 phymode = MODE_11AC_VHT40;
1936 else
1937 phymode = MODE_11AC_VHT20;
1938 } else if (sta->ht_cap.ht_supported) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001939 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1940 phymode = MODE_11NG_HT40;
1941 else
1942 phymode = MODE_11NG_HT20;
Michal Kazior91b12082014-12-12 12:41:35 +01001943 } else if (ath10k_mac_sta_has_11g_rates(sta)) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001944 phymode = MODE_11G;
Michal Kazior91b12082014-12-12 12:41:35 +01001945 } else {
1946 phymode = MODE_11B;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001947 }
1948
1949 break;
1950 case IEEE80211_BAND_5GHZ:
Sujith Manoharan7cc45e92013-09-08 18:19:55 +03001951 /*
1952 * Check VHT first.
1953 */
1954 if (sta->vht_cap.vht_supported) {
1955 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1956 phymode = MODE_11AC_VHT80;
1957 else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1958 phymode = MODE_11AC_VHT40;
1959 else if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
1960 phymode = MODE_11AC_VHT20;
1961 } else if (sta->ht_cap.ht_supported) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001962 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1963 phymode = MODE_11NA_HT40;
1964 else
1965 phymode = MODE_11NA_HT20;
1966 } else {
1967 phymode = MODE_11A;
1968 }
1969
1970 break;
1971 default:
1972 break;
1973 }
1974
Michal Kazior7aa7a722014-08-25 12:09:38 +02001975 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM phymode %s\n",
Kalle Valo38a1d472013-09-08 17:56:14 +03001976 sta->addr, ath10k_wmi_phymode_str(phymode));
Kalle Valo60c3daa2013-09-08 17:56:07 +03001977
Kalle Valo5e3dd152013-06-12 20:52:10 +03001978 arg->peer_phymode = phymode;
1979 WARN_ON(phymode == MODE_UNKNOWN);
1980}
1981
Kalle Valob9ada652013-10-16 15:44:46 +03001982static int ath10k_peer_assoc_prepare(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001983 struct ieee80211_vif *vif,
Kalle Valob9ada652013-10-16 15:44:46 +03001984 struct ieee80211_sta *sta,
Kalle Valob9ada652013-10-16 15:44:46 +03001985 struct wmi_peer_assoc_complete_arg *arg)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001986{
Michal Kazior548db542013-07-05 16:15:15 +03001987 lockdep_assert_held(&ar->conf_mutex);
1988
Kalle Valob9ada652013-10-16 15:44:46 +03001989 memset(arg, 0, sizeof(*arg));
Kalle Valo5e3dd152013-06-12 20:52:10 +03001990
Michal Kazior590922a2014-10-21 10:10:29 +03001991 ath10k_peer_assoc_h_basic(ar, vif, sta, arg);
1992 ath10k_peer_assoc_h_crypto(ar, vif, arg);
Kalle Valob9ada652013-10-16 15:44:46 +03001993 ath10k_peer_assoc_h_rates(ar, sta, arg);
1994 ath10k_peer_assoc_h_ht(ar, sta, arg);
1995 ath10k_peer_assoc_h_vht(ar, sta, arg);
Michal Kazior590922a2014-10-21 10:10:29 +03001996 ath10k_peer_assoc_h_qos(ar, vif, sta, arg);
1997 ath10k_peer_assoc_h_phymode(ar, vif, sta, arg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001998
Kalle Valob9ada652013-10-16 15:44:46 +03001999 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002000}
2001
Michal Kazior90046f52014-02-14 14:45:51 +01002002static const u32 ath10k_smps_map[] = {
2003 [WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
2004 [WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
2005 [WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
2006 [WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
2007};
2008
2009static int ath10k_setup_peer_smps(struct ath10k *ar, struct ath10k_vif *arvif,
2010 const u8 *addr,
2011 const struct ieee80211_sta_ht_cap *ht_cap)
2012{
2013 int smps;
2014
2015 if (!ht_cap->ht_supported)
2016 return 0;
2017
2018 smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
2019 smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
2020
2021 if (smps >= ARRAY_SIZE(ath10k_smps_map))
2022 return -EINVAL;
2023
2024 return ath10k_wmi_peer_set_param(ar, arvif->vdev_id, addr,
2025 WMI_PEER_SMPS_STATE,
2026 ath10k_smps_map[smps]);
2027}
2028
Michal Kazior139e1702015-02-15 16:50:42 +02002029static int ath10k_mac_vif_recalc_txbf(struct ath10k *ar,
2030 struct ieee80211_vif *vif,
2031 struct ieee80211_sta_vht_cap vht_cap)
2032{
2033 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2034 int ret;
2035 u32 param;
2036 u32 value;
2037
2038 if (!(ar->vht_cap_info &
2039 (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
2040 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
2041 IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
2042 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)))
2043 return 0;
2044
2045 param = ar->wmi.vdev_param->txbf;
2046 value = 0;
2047
2048 if (WARN_ON(param == WMI_VDEV_PARAM_UNSUPPORTED))
2049 return 0;
2050
2051 /* The following logic is correct. If a remote STA advertises support
2052 * for being a beamformer then we should enable us being a beamformee.
2053 */
2054
2055 if (ar->vht_cap_info &
2056 (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
2057 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)) {
2058 if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)
2059 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
2060
2061 if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)
2062 value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
2063 }
2064
2065 if (ar->vht_cap_info &
2066 (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
2067 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)) {
2068 if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)
2069 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
2070
2071 if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)
2072 value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
2073 }
2074
2075 if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFEE)
2076 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
2077
2078 if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFER)
2079 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
2080
2081 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param, value);
2082 if (ret) {
2083 ath10k_warn(ar, "failed to submit vdev param txbf 0x%x: %d\n",
2084 value, ret);
2085 return ret;
2086 }
2087
2088 return 0;
2089}
2090
Kalle Valo5e3dd152013-06-12 20:52:10 +03002091/* can be called only in mac80211 callbacks due to `key_count` usage */
2092static void ath10k_bss_assoc(struct ieee80211_hw *hw,
2093 struct ieee80211_vif *vif,
2094 struct ieee80211_bss_conf *bss_conf)
2095{
2096 struct ath10k *ar = hw->priv;
2097 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Michal Kazior90046f52014-02-14 14:45:51 +01002098 struct ieee80211_sta_ht_cap ht_cap;
Michal Kazior139e1702015-02-15 16:50:42 +02002099 struct ieee80211_sta_vht_cap vht_cap;
Kalle Valob9ada652013-10-16 15:44:46 +03002100 struct wmi_peer_assoc_complete_arg peer_arg;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002101 struct ieee80211_sta *ap_sta;
2102 int ret;
2103
Michal Kazior548db542013-07-05 16:15:15 +03002104 lockdep_assert_held(&ar->conf_mutex);
2105
Michal Kazior077efc82014-10-21 10:10:29 +03002106 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
2107 arvif->vdev_id, arvif->bssid, arvif->aid);
2108
Kalle Valo5e3dd152013-06-12 20:52:10 +03002109 rcu_read_lock();
2110
2111 ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
2112 if (!ap_sta) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002113 ath10k_warn(ar, "failed to find station entry for bss %pM vdev %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02002114 bss_conf->bssid, arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002115 rcu_read_unlock();
2116 return;
2117 }
2118
Michal Kazior90046f52014-02-14 14:45:51 +01002119 /* ap_sta must be accessed only within rcu section which must be left
2120 * before calling ath10k_setup_peer_smps() which might sleep. */
2121 ht_cap = ap_sta->ht_cap;
Michal Kazior139e1702015-02-15 16:50:42 +02002122 vht_cap = ap_sta->vht_cap;
Michal Kazior90046f52014-02-14 14:45:51 +01002123
Michal Kazior590922a2014-10-21 10:10:29 +03002124 ret = ath10k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002125 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002126 ath10k_warn(ar, "failed to prepare peer assoc for %pM vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02002127 bss_conf->bssid, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002128 rcu_read_unlock();
2129 return;
2130 }
2131
2132 rcu_read_unlock();
2133
Kalle Valob9ada652013-10-16 15:44:46 +03002134 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
2135 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002136 ath10k_warn(ar, "failed to run peer assoc for %pM vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02002137 bss_conf->bssid, arvif->vdev_id, ret);
Kalle Valob9ada652013-10-16 15:44:46 +03002138 return;
2139 }
2140
Michal Kazior90046f52014-02-14 14:45:51 +01002141 ret = ath10k_setup_peer_smps(ar, arvif, bss_conf->bssid, &ht_cap);
2142 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002143 ath10k_warn(ar, "failed to setup peer SMPS for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02002144 arvif->vdev_id, ret);
Michal Kazior90046f52014-02-14 14:45:51 +01002145 return;
2146 }
2147
Michal Kazior139e1702015-02-15 16:50:42 +02002148 ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
2149 if (ret) {
2150 ath10k_warn(ar, "failed to recalc txbf for vdev %i on bss %pM: %d\n",
2151 arvif->vdev_id, bss_conf->bssid, ret);
2152 return;
2153 }
2154
Michal Kazior7aa7a722014-08-25 12:09:38 +02002155 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03002156 "mac vdev %d up (associated) bssid %pM aid %d\n",
2157 arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
2158
Michal Kazior077efc82014-10-21 10:10:29 +03002159 WARN_ON(arvif->is_up);
2160
Michal Kaziorc930f742014-01-23 11:38:25 +01002161 arvif->aid = bss_conf->aid;
Kalle Valob25f32c2014-09-14 12:50:49 +03002162 ether_addr_copy(arvif->bssid, bss_conf->bssid);
Michal Kaziorc930f742014-01-23 11:38:25 +01002163
2164 ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
2165 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002166 ath10k_warn(ar, "failed to set vdev %d up: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002167 arvif->vdev_id, ret);
Michal Kaziorc930f742014-01-23 11:38:25 +01002168 return;
2169 }
2170
2171 arvif->is_up = true;
Michal Kazior0a987fb2015-02-13 13:30:15 +01002172
2173 /* Workaround: Some firmware revisions (tested with qca6174
2174 * WLAN.RM.2.0-00073) have buggy powersave state machine and must be
2175 * poked with peer param command.
2176 */
2177 ret = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, arvif->bssid,
2178 WMI_PEER_DUMMY_VAR, 1);
2179 if (ret) {
2180 ath10k_warn(ar, "failed to poke peer %pM param for ps workaround on vdev %i: %d\n",
2181 arvif->bssid, arvif->vdev_id, ret);
2182 return;
2183 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002184}
2185
Kalle Valo5e3dd152013-06-12 20:52:10 +03002186static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
2187 struct ieee80211_vif *vif)
2188{
2189 struct ath10k *ar = hw->priv;
2190 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Michal Kazior139e1702015-02-15 16:50:42 +02002191 struct ieee80211_sta_vht_cap vht_cap = {};
Kalle Valo5e3dd152013-06-12 20:52:10 +03002192 int ret;
2193
Michal Kazior548db542013-07-05 16:15:15 +03002194 lockdep_assert_held(&ar->conf_mutex);
2195
Michal Kazior077efc82014-10-21 10:10:29 +03002196 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
2197 arvif->vdev_id, arvif->bssid);
Kalle Valo60c3daa2013-09-08 17:56:07 +03002198
Kalle Valo5e3dd152013-06-12 20:52:10 +03002199 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
Michal Kazior077efc82014-10-21 10:10:29 +03002200 if (ret)
2201 ath10k_warn(ar, "faield to down vdev %i: %d\n",
2202 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002203
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +02002204 arvif->def_wep_key_idx = -1;
2205
Michal Kazior139e1702015-02-15 16:50:42 +02002206 ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
2207 if (ret) {
2208 ath10k_warn(ar, "failed to recalc txbf for vdev %i: %d\n",
2209 arvif->vdev_id, ret);
2210 return;
2211 }
2212
Michal Kaziorc930f742014-01-23 11:38:25 +01002213 arvif->is_up = false;
Michal Kaziorcc9904e2015-03-10 16:22:01 +02002214
2215 cancel_delayed_work_sync(&arvif->connection_loss_work);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002216}
2217
Michal Kazior590922a2014-10-21 10:10:29 +03002218static int ath10k_station_assoc(struct ath10k *ar,
2219 struct ieee80211_vif *vif,
2220 struct ieee80211_sta *sta,
2221 bool reassoc)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002222{
Michal Kazior590922a2014-10-21 10:10:29 +03002223 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Kalle Valob9ada652013-10-16 15:44:46 +03002224 struct wmi_peer_assoc_complete_arg peer_arg;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002225 int ret = 0;
2226
Michal Kazior548db542013-07-05 16:15:15 +03002227 lockdep_assert_held(&ar->conf_mutex);
2228
Michal Kazior590922a2014-10-21 10:10:29 +03002229 ret = ath10k_peer_assoc_prepare(ar, vif, sta, &peer_arg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002230 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002231 ath10k_warn(ar, "failed to prepare WMI peer assoc for %pM vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02002232 sta->addr, arvif->vdev_id, ret);
Kalle Valob9ada652013-10-16 15:44:46 +03002233 return ret;
2234 }
2235
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02002236 peer_arg.peer_reassoc = reassoc;
Kalle Valob9ada652013-10-16 15:44:46 +03002237 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
2238 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002239 ath10k_warn(ar, "failed to run peer assoc for STA %pM vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02002240 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002241 return ret;
2242 }
2243
Michal Kaziorb1ecde32014-10-21 10:10:29 +03002244 /* Re-assoc is run only to update supported rates for given station. It
2245 * doesn't make much sense to reconfigure the peer completely.
2246 */
2247 if (!reassoc) {
2248 ret = ath10k_setup_peer_smps(ar, arvif, sta->addr,
2249 &sta->ht_cap);
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02002250 if (ret) {
Michal Kaziorb1ecde32014-10-21 10:10:29 +03002251 ath10k_warn(ar, "failed to setup peer SMPS for vdev %d: %d\n",
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02002252 arvif->vdev_id, ret);
2253 return ret;
2254 }
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02002255
Michal Kaziorb1ecde32014-10-21 10:10:29 +03002256 ret = ath10k_peer_assoc_qos_ap(ar, arvif, sta);
2257 if (ret) {
2258 ath10k_warn(ar, "failed to set qos params for STA %pM for vdev %i: %d\n",
2259 sta->addr, arvif->vdev_id, ret);
2260 return ret;
2261 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002262
Michal Kaziorb1ecde32014-10-21 10:10:29 +03002263 if (!sta->wme) {
2264 arvif->num_legacy_stations++;
2265 ret = ath10k_recalc_rtscts_prot(arvif);
2266 if (ret) {
2267 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
2268 arvif->vdev_id, ret);
2269 return ret;
2270 }
2271 }
2272
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +02002273 /* Plumb cached keys only for static WEP */
2274 if (arvif->def_wep_key_idx != -1) {
2275 ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
2276 if (ret) {
2277 ath10k_warn(ar, "failed to install peer wep keys for vdev %i: %d\n",
2278 arvif->vdev_id, ret);
2279 return ret;
2280 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002281 }
2282 }
2283
Kalle Valo5e3dd152013-06-12 20:52:10 +03002284 return ret;
2285}
2286
Michal Kazior590922a2014-10-21 10:10:29 +03002287static int ath10k_station_disassoc(struct ath10k *ar,
2288 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002289 struct ieee80211_sta *sta)
2290{
Michal Kazior590922a2014-10-21 10:10:29 +03002291 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002292 int ret = 0;
2293
2294 lockdep_assert_held(&ar->conf_mutex);
2295
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02002296 if (!sta->wme) {
2297 arvif->num_legacy_stations--;
2298 ret = ath10k_recalc_rtscts_prot(arvif);
2299 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002300 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02002301 arvif->vdev_id, ret);
2302 return ret;
2303 }
2304 }
2305
Kalle Valo5e3dd152013-06-12 20:52:10 +03002306 ret = ath10k_clear_peer_keys(arvif, sta->addr);
2307 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002308 ath10k_warn(ar, "failed to clear all peer wep keys for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02002309 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002310 return ret;
2311 }
2312
2313 return ret;
2314}
2315
2316/**************/
2317/* Regulatory */
2318/**************/
2319
2320static int ath10k_update_channel_list(struct ath10k *ar)
2321{
2322 struct ieee80211_hw *hw = ar->hw;
2323 struct ieee80211_supported_band **bands;
2324 enum ieee80211_band band;
2325 struct ieee80211_channel *channel;
2326 struct wmi_scan_chan_list_arg arg = {0};
2327 struct wmi_channel_arg *ch;
2328 bool passive;
2329 int len;
2330 int ret;
2331 int i;
2332
Michal Kazior548db542013-07-05 16:15:15 +03002333 lockdep_assert_held(&ar->conf_mutex);
2334
Kalle Valo5e3dd152013-06-12 20:52:10 +03002335 bands = hw->wiphy->bands;
2336 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2337 if (!bands[band])
2338 continue;
2339
2340 for (i = 0; i < bands[band]->n_channels; i++) {
2341 if (bands[band]->channels[i].flags &
2342 IEEE80211_CHAN_DISABLED)
2343 continue;
2344
2345 arg.n_channels++;
2346 }
2347 }
2348
2349 len = sizeof(struct wmi_channel_arg) * arg.n_channels;
2350 arg.channels = kzalloc(len, GFP_KERNEL);
2351 if (!arg.channels)
2352 return -ENOMEM;
2353
2354 ch = arg.channels;
2355 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2356 if (!bands[band])
2357 continue;
2358
2359 for (i = 0; i < bands[band]->n_channels; i++) {
2360 channel = &bands[band]->channels[i];
2361
2362 if (channel->flags & IEEE80211_CHAN_DISABLED)
2363 continue;
2364
2365 ch->allow_ht = true;
2366
2367 /* FIXME: when should we really allow VHT? */
2368 ch->allow_vht = true;
2369
2370 ch->allow_ibss =
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +02002371 !(channel->flags & IEEE80211_CHAN_NO_IR);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002372
2373 ch->ht40plus =
2374 !(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
2375
Marek Puzyniake8a50f82013-11-20 09:59:47 +02002376 ch->chan_radar =
2377 !!(channel->flags & IEEE80211_CHAN_RADAR);
2378
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +02002379 passive = channel->flags & IEEE80211_CHAN_NO_IR;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002380 ch->passive = passive;
2381
2382 ch->freq = channel->center_freq;
Michal Kazior2d667212014-09-18 15:21:21 +02002383 ch->band_center_freq1 = channel->center_freq;
Michal Kazior89c5c842013-10-23 04:02:13 -07002384 ch->min_power = 0;
Michal Kazior02256932013-10-23 04:02:14 -07002385 ch->max_power = channel->max_power * 2;
2386 ch->max_reg_power = channel->max_reg_power * 2;
2387 ch->max_antenna_gain = channel->max_antenna_gain * 2;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002388 ch->reg_class_id = 0; /* FIXME */
2389
2390 /* FIXME: why use only legacy modes, why not any
2391 * HT/VHT modes? Would that even make any
2392 * difference? */
2393 if (channel->band == IEEE80211_BAND_2GHZ)
2394 ch->mode = MODE_11G;
2395 else
2396 ch->mode = MODE_11A;
2397
2398 if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
2399 continue;
2400
Michal Kazior7aa7a722014-08-25 12:09:38 +02002401 ath10k_dbg(ar, ATH10K_DBG_WMI,
Kalle Valo60c3daa2013-09-08 17:56:07 +03002402 "mac channel [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
2403 ch - arg.channels, arg.n_channels,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002404 ch->freq, ch->max_power, ch->max_reg_power,
2405 ch->max_antenna_gain, ch->mode);
2406
2407 ch++;
2408 }
2409 }
2410
2411 ret = ath10k_wmi_scan_chan_list(ar, &arg);
2412 kfree(arg.channels);
2413
2414 return ret;
2415}
2416
Marek Puzyniak821af6a2014-03-21 17:46:57 +02002417static enum wmi_dfs_region
2418ath10k_mac_get_dfs_region(enum nl80211_dfs_regions dfs_region)
2419{
2420 switch (dfs_region) {
2421 case NL80211_DFS_UNSET:
2422 return WMI_UNINIT_DFS_DOMAIN;
2423 case NL80211_DFS_FCC:
2424 return WMI_FCC_DFS_DOMAIN;
2425 case NL80211_DFS_ETSI:
2426 return WMI_ETSI_DFS_DOMAIN;
2427 case NL80211_DFS_JP:
2428 return WMI_MKK4_DFS_DOMAIN;
2429 }
2430 return WMI_UNINIT_DFS_DOMAIN;
2431}
2432
Michal Kaziorf7843d72013-07-16 09:38:52 +02002433static void ath10k_regd_update(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002434{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002435 struct reg_dmn_pair_mapping *regpair;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002436 int ret;
Marek Puzyniak821af6a2014-03-21 17:46:57 +02002437 enum wmi_dfs_region wmi_dfs_reg;
2438 enum nl80211_dfs_regions nl_dfs_reg;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002439
Michal Kaziorf7843d72013-07-16 09:38:52 +02002440 lockdep_assert_held(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002441
2442 ret = ath10k_update_channel_list(ar);
2443 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002444 ath10k_warn(ar, "failed to update channel list: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002445
2446 regpair = ar->ath_common.regulatory.regpair;
Michal Kaziorf7843d72013-07-16 09:38:52 +02002447
Marek Puzyniak821af6a2014-03-21 17:46:57 +02002448 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
2449 nl_dfs_reg = ar->dfs_detector->region;
2450 wmi_dfs_reg = ath10k_mac_get_dfs_region(nl_dfs_reg);
2451 } else {
2452 wmi_dfs_reg = WMI_UNINIT_DFS_DOMAIN;
2453 }
2454
Kalle Valo5e3dd152013-06-12 20:52:10 +03002455 /* Target allows setting up per-band regdomain but ath_common provides
2456 * a combined one only */
2457 ret = ath10k_wmi_pdev_set_regdomain(ar,
Kalle Valoef8c0012014-02-13 18:13:12 +02002458 regpair->reg_domain,
2459 regpair->reg_domain, /* 2ghz */
2460 regpair->reg_domain, /* 5ghz */
Kalle Valo5e3dd152013-06-12 20:52:10 +03002461 regpair->reg_2ghz_ctl,
Marek Puzyniak821af6a2014-03-21 17:46:57 +02002462 regpair->reg_5ghz_ctl,
2463 wmi_dfs_reg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002464 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002465 ath10k_warn(ar, "failed to set pdev regdomain: %d\n", ret);
Michal Kaziorf7843d72013-07-16 09:38:52 +02002466}
Michal Kazior548db542013-07-05 16:15:15 +03002467
Michal Kaziorf7843d72013-07-16 09:38:52 +02002468static void ath10k_reg_notifier(struct wiphy *wiphy,
2469 struct regulatory_request *request)
2470{
2471 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
2472 struct ath10k *ar = hw->priv;
Janusz Dziedzic9702c682013-11-20 09:59:41 +02002473 bool result;
Michal Kaziorf7843d72013-07-16 09:38:52 +02002474
2475 ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
2476
Janusz Dziedzic9702c682013-11-20 09:59:41 +02002477 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002478 ath10k_dbg(ar, ATH10K_DBG_REGULATORY, "dfs region 0x%x\n",
Janusz Dziedzic9702c682013-11-20 09:59:41 +02002479 request->dfs_region);
2480 result = ar->dfs_detector->set_dfs_domain(ar->dfs_detector,
2481 request->dfs_region);
2482 if (!result)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002483 ath10k_warn(ar, "DFS region 0x%X not supported, will trigger radar for every pulse\n",
Janusz Dziedzic9702c682013-11-20 09:59:41 +02002484 request->dfs_region);
2485 }
2486
Michal Kaziorf7843d72013-07-16 09:38:52 +02002487 mutex_lock(&ar->conf_mutex);
2488 if (ar->state == ATH10K_STATE_ON)
2489 ath10k_regd_update(ar);
Michal Kazior548db542013-07-05 16:15:15 +03002490 mutex_unlock(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002491}
2492
2493/***************/
2494/* TX handlers */
2495/***************/
2496
Michal Kazior42c3aa62013-10-02 11:03:38 +02002497static u8 ath10k_tx_h_get_tid(struct ieee80211_hdr *hdr)
2498{
2499 if (ieee80211_is_mgmt(hdr->frame_control))
2500 return HTT_DATA_TX_EXT_TID_MGMT;
2501
2502 if (!ieee80211_is_data_qos(hdr->frame_control))
2503 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
2504
2505 if (!is_unicast_ether_addr(ieee80211_get_DA(hdr)))
2506 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
2507
2508 return ieee80211_get_qos_ctl(hdr)[0] & IEEE80211_QOS_CTL_TID_MASK;
2509}
2510
Michal Kazior2b37c292014-09-02 11:00:22 +03002511static u8 ath10k_tx_h_get_vdev_id(struct ath10k *ar, struct ieee80211_vif *vif)
Michal Kaziorddb6ad72013-10-02 11:03:39 +02002512{
Michal Kazior2b37c292014-09-02 11:00:22 +03002513 if (vif)
2514 return ath10k_vif_to_arvif(vif)->vdev_id;
Michal Kaziorddb6ad72013-10-02 11:03:39 +02002515
Michal Kazior1bbc0972014-04-08 09:45:47 +03002516 if (ar->monitor_started)
Michal Kaziorddb6ad72013-10-02 11:03:39 +02002517 return ar->monitor_vdev_id;
2518
Michal Kazior7aa7a722014-08-25 12:09:38 +02002519 ath10k_warn(ar, "failed to resolve vdev id\n");
Michal Kaziorddb6ad72013-10-02 11:03:39 +02002520 return 0;
2521}
2522
Michal Kazior4b604552014-07-21 21:03:09 +03002523/* HTT Tx uses Native Wifi tx mode which expects 802.11 frames without QoS
2524 * Control in the header.
Kalle Valo5e3dd152013-06-12 20:52:10 +03002525 */
Michal Kazior4b604552014-07-21 21:03:09 +03002526static void ath10k_tx_h_nwifi(struct ieee80211_hw *hw, struct sk_buff *skb)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002527{
2528 struct ieee80211_hdr *hdr = (void *)skb->data;
Michal Kaziorc21c64d2014-07-21 21:03:10 +03002529 struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002530 u8 *qos_ctl;
2531
2532 if (!ieee80211_is_data_qos(hdr->frame_control))
2533 return;
2534
2535 qos_ctl = ieee80211_get_qos_ctl(hdr);
Michal Kaziorba0ccd72013-07-22 14:25:28 +02002536 memmove(skb->data + IEEE80211_QOS_CTL_LEN,
2537 skb->data, (void *)qos_ctl - (void *)skb->data);
2538 skb_pull(skb, IEEE80211_QOS_CTL_LEN);
Michal Kaziorc21c64d2014-07-21 21:03:10 +03002539
Michal Kazior8bad8dc2015-03-11 14:25:26 +01002540 /* Some firmware revisions don't handle sending QoS NullFunc well.
2541 * These frames are mainly used for CQM purposes so it doesn't really
2542 * matter whether QoS NullFunc or NullFunc are sent.
Michal Kaziorc21c64d2014-07-21 21:03:10 +03002543 */
Michal Kaziorbf0a26d2015-01-24 12:14:51 +02002544 hdr = (void *)skb->data;
Michal Kazior8bad8dc2015-03-11 14:25:26 +01002545 if (ieee80211_is_qos_nullfunc(hdr->frame_control))
Michal Kaziorc21c64d2014-07-21 21:03:10 +03002546 cb->htt.tid = HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
Michal Kazior8bad8dc2015-03-11 14:25:26 +01002547
2548 hdr->frame_control &= ~__cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002549}
2550
Michal Kazior4b604552014-07-21 21:03:09 +03002551static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar,
2552 struct ieee80211_vif *vif,
2553 struct sk_buff *skb)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002554{
2555 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002556 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2557
2558 /* This is case only for P2P_GO */
2559 if (arvif->vdev_type != WMI_VDEV_TYPE_AP ||
2560 arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
2561 return;
2562
2563 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
2564 spin_lock_bh(&ar->data_lock);
2565 if (arvif->u.ap.noa_data)
2566 if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
2567 GFP_ATOMIC))
2568 memcpy(skb_put(skb, arvif->u.ap.noa_len),
2569 arvif->u.ap.noa_data,
2570 arvif->u.ap.noa_len);
2571 spin_unlock_bh(&ar->data_lock);
2572 }
2573}
2574
Michal Kazior8d6d3622014-11-24 14:58:31 +01002575static bool ath10k_mac_need_offchan_tx_work(struct ath10k *ar)
2576{
2577 /* FIXME: Not really sure since when the behaviour changed. At some
2578 * point new firmware stopped requiring creation of peer entries for
2579 * offchannel tx (and actually creating them causes issues with wmi-htc
2580 * tx credit replenishment and reliability). Assuming it's at least 3.4
2581 * because that's when the `freq` was introduced to TX_FRM HTT command.
2582 */
2583 return !(ar->htt.target_version_major >= 3 &&
2584 ar->htt.target_version_minor >= 4);
2585}
2586
Kalle Valo5e3dd152013-06-12 20:52:10 +03002587static void ath10k_tx_htt(struct ath10k *ar, struct sk_buff *skb)
2588{
2589 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002590 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002591
Michal Kazior961d4c32013-08-09 10:13:34 +02002592 if (ar->htt.target_version_major >= 3) {
2593 /* Since HTT 3.0 there is no separate mgmt tx command */
2594 ret = ath10k_htt_tx(&ar->htt, skb);
2595 goto exit;
2596 }
2597
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002598 if (ieee80211_is_mgmt(hdr->frame_control)) {
2599 if (test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
2600 ar->fw_features)) {
2601 if (skb_queue_len(&ar->wmi_mgmt_tx_queue) >=
2602 ATH10K_MAX_NUM_MGMT_PENDING) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002603 ath10k_warn(ar, "reached WMI management transmit queue limit\n");
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002604 ret = -EBUSY;
2605 goto exit;
2606 }
2607
2608 skb_queue_tail(&ar->wmi_mgmt_tx_queue, skb);
2609 ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
2610 } else {
2611 ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
2612 }
2613 } else if (!test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
2614 ar->fw_features) &&
2615 ieee80211_is_nullfunc(hdr->frame_control)) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002616 /* FW does not report tx status properly for NullFunc frames
2617 * unless they are sent through mgmt tx path. mac80211 sends
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002618 * those frames when it detects link/beacon loss and depends
2619 * on the tx status to be correct. */
Michal Kazioredb82362013-07-05 16:15:14 +03002620 ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002621 } else {
Michal Kazioredb82362013-07-05 16:15:14 +03002622 ret = ath10k_htt_tx(&ar->htt, skb);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002623 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002624
Michal Kazior961d4c32013-08-09 10:13:34 +02002625exit:
Kalle Valo5e3dd152013-06-12 20:52:10 +03002626 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002627 ath10k_warn(ar, "failed to transmit packet, dropping: %d\n",
2628 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002629 ieee80211_free_txskb(ar->hw, skb);
2630 }
2631}
2632
2633void ath10k_offchan_tx_purge(struct ath10k *ar)
2634{
2635 struct sk_buff *skb;
2636
2637 for (;;) {
2638 skb = skb_dequeue(&ar->offchan_tx_queue);
2639 if (!skb)
2640 break;
2641
2642 ieee80211_free_txskb(ar->hw, skb);
2643 }
2644}
2645
2646void ath10k_offchan_tx_work(struct work_struct *work)
2647{
2648 struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
2649 struct ath10k_peer *peer;
2650 struct ieee80211_hdr *hdr;
2651 struct sk_buff *skb;
2652 const u8 *peer_addr;
2653 int vdev_id;
2654 int ret;
2655
2656 /* FW requirement: We must create a peer before FW will send out
2657 * an offchannel frame. Otherwise the frame will be stuck and
2658 * never transmitted. We delete the peer upon tx completion.
2659 * It is unlikely that a peer for offchannel tx will already be
2660 * present. However it may be in some rare cases so account for that.
2661 * Otherwise we might remove a legitimate peer and break stuff. */
2662
2663 for (;;) {
2664 skb = skb_dequeue(&ar->offchan_tx_queue);
2665 if (!skb)
2666 break;
2667
2668 mutex_lock(&ar->conf_mutex);
2669
Michal Kazior7aa7a722014-08-25 12:09:38 +02002670 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac offchannel skb %p\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002671 skb);
2672
2673 hdr = (struct ieee80211_hdr *)skb->data;
2674 peer_addr = ieee80211_get_DA(hdr);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002675 vdev_id = ATH10K_SKB_CB(skb)->vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002676
2677 spin_lock_bh(&ar->data_lock);
2678 peer = ath10k_peer_find(ar, vdev_id, peer_addr);
2679 spin_unlock_bh(&ar->data_lock);
2680
2681 if (peer)
Kalle Valo60c3daa2013-09-08 17:56:07 +03002682 /* FIXME: should this use ath10k_warn()? */
Michal Kazior7aa7a722014-08-25 12:09:38 +02002683 ath10k_dbg(ar, ATH10K_DBG_MAC, "peer %pM on vdev %d already present\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002684 peer_addr, vdev_id);
2685
2686 if (!peer) {
2687 ret = ath10k_peer_create(ar, vdev_id, peer_addr);
2688 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002689 ath10k_warn(ar, "failed to create peer %pM on vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002690 peer_addr, vdev_id, ret);
2691 }
2692
2693 spin_lock_bh(&ar->data_lock);
Wolfram Sang16735d02013-11-14 14:32:02 -08002694 reinit_completion(&ar->offchan_tx_completed);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002695 ar->offchan_tx_skb = skb;
2696 spin_unlock_bh(&ar->data_lock);
2697
2698 ath10k_tx_htt(ar, skb);
2699
2700 ret = wait_for_completion_timeout(&ar->offchan_tx_completed,
2701 3 * HZ);
Nicholas Mc Guire38e2a642015-01-08 13:27:34 +01002702 if (ret == 0)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002703 ath10k_warn(ar, "timed out waiting for offchannel skb %p\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002704 skb);
2705
2706 if (!peer) {
2707 ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
2708 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002709 ath10k_warn(ar, "failed to delete peer %pM on vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002710 peer_addr, vdev_id, ret);
2711 }
2712
2713 mutex_unlock(&ar->conf_mutex);
2714 }
2715}
2716
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002717void ath10k_mgmt_over_wmi_tx_purge(struct ath10k *ar)
2718{
2719 struct sk_buff *skb;
2720
2721 for (;;) {
2722 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
2723 if (!skb)
2724 break;
2725
2726 ieee80211_free_txskb(ar->hw, skb);
2727 }
2728}
2729
2730void ath10k_mgmt_over_wmi_tx_work(struct work_struct *work)
2731{
2732 struct ath10k *ar = container_of(work, struct ath10k, wmi_mgmt_tx_work);
2733 struct sk_buff *skb;
2734 int ret;
2735
2736 for (;;) {
2737 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
2738 if (!skb)
2739 break;
2740
2741 ret = ath10k_wmi_mgmt_tx(ar, skb);
Michal Kazior5fb5e412013-10-28 07:18:13 +01002742 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002743 ath10k_warn(ar, "failed to transmit management frame via WMI: %d\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02002744 ret);
Michal Kazior5fb5e412013-10-28 07:18:13 +01002745 ieee80211_free_txskb(ar->hw, skb);
2746 }
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002747 }
2748}
2749
Kalle Valo5e3dd152013-06-12 20:52:10 +03002750/************/
2751/* Scanning */
2752/************/
2753
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002754void __ath10k_scan_finish(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002755{
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002756 lockdep_assert_held(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002757
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002758 switch (ar->scan.state) {
2759 case ATH10K_SCAN_IDLE:
2760 break;
2761 case ATH10K_SCAN_RUNNING:
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002762 if (ar->scan.is_roc)
2763 ieee80211_remain_on_channel_expired(ar->hw);
John W. Linvillef6eaf1e2015-01-12 16:07:02 -05002764 /* fall through */
Michal Kazior7305d3e2014-11-24 14:58:33 +01002765 case ATH10K_SCAN_ABORTING:
2766 if (!ar->scan.is_roc)
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002767 ieee80211_scan_completed(ar->hw,
2768 (ar->scan.state ==
2769 ATH10K_SCAN_ABORTING));
2770 /* fall through */
2771 case ATH10K_SCAN_STARTING:
2772 ar->scan.state = ATH10K_SCAN_IDLE;
2773 ar->scan_channel = NULL;
2774 ath10k_offchan_tx_purge(ar);
2775 cancel_delayed_work(&ar->scan.timeout);
2776 complete_all(&ar->scan.completed);
2777 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002778 }
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002779}
Kalle Valo5e3dd152013-06-12 20:52:10 +03002780
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002781void ath10k_scan_finish(struct ath10k *ar)
2782{
2783 spin_lock_bh(&ar->data_lock);
2784 __ath10k_scan_finish(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002785 spin_unlock_bh(&ar->data_lock);
2786}
2787
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002788static int ath10k_scan_stop(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002789{
2790 struct wmi_stop_scan_arg arg = {
2791 .req_id = 1, /* FIXME */
2792 .req_type = WMI_SCAN_STOP_ONE,
2793 .u.scan_id = ATH10K_SCAN_ID,
2794 };
2795 int ret;
2796
2797 lockdep_assert_held(&ar->conf_mutex);
2798
Kalle Valo5e3dd152013-06-12 20:52:10 +03002799 ret = ath10k_wmi_stop_scan(ar, &arg);
2800 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002801 ath10k_warn(ar, "failed to stop wmi scan: %d\n", ret);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002802 goto out;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002803 }
2804
Kalle Valo5e3dd152013-06-12 20:52:10 +03002805 ret = wait_for_completion_timeout(&ar->scan.completed, 3*HZ);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002806 if (ret == 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002807 ath10k_warn(ar, "failed to receive scan abortion completion: timed out\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002808 ret = -ETIMEDOUT;
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002809 } else if (ret > 0) {
2810 ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002811 }
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002812
2813out:
2814 /* Scan state should be updated upon scan completion but in case
2815 * firmware fails to deliver the event (for whatever reason) it is
2816 * desired to clean up scan state anyway. Firmware may have just
2817 * dropped the scan completion event delivery due to transport pipe
2818 * being overflown with data and/or it can recover on its own before
2819 * next scan request is submitted.
2820 */
2821 spin_lock_bh(&ar->data_lock);
2822 if (ar->scan.state != ATH10K_SCAN_IDLE)
2823 __ath10k_scan_finish(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002824 spin_unlock_bh(&ar->data_lock);
2825
2826 return ret;
2827}
2828
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002829static void ath10k_scan_abort(struct ath10k *ar)
2830{
2831 int ret;
2832
2833 lockdep_assert_held(&ar->conf_mutex);
2834
2835 spin_lock_bh(&ar->data_lock);
2836
2837 switch (ar->scan.state) {
2838 case ATH10K_SCAN_IDLE:
2839 /* This can happen if timeout worker kicked in and called
2840 * abortion while scan completion was being processed.
2841 */
2842 break;
2843 case ATH10K_SCAN_STARTING:
2844 case ATH10K_SCAN_ABORTING:
Michal Kazior7aa7a722014-08-25 12:09:38 +02002845 ath10k_warn(ar, "refusing scan abortion due to invalid scan state: %s (%d)\n",
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002846 ath10k_scan_state_str(ar->scan.state),
2847 ar->scan.state);
2848 break;
2849 case ATH10K_SCAN_RUNNING:
2850 ar->scan.state = ATH10K_SCAN_ABORTING;
2851 spin_unlock_bh(&ar->data_lock);
2852
2853 ret = ath10k_scan_stop(ar);
2854 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002855 ath10k_warn(ar, "failed to abort scan: %d\n", ret);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002856
2857 spin_lock_bh(&ar->data_lock);
2858 break;
2859 }
2860
2861 spin_unlock_bh(&ar->data_lock);
2862}
2863
2864void ath10k_scan_timeout_work(struct work_struct *work)
2865{
2866 struct ath10k *ar = container_of(work, struct ath10k,
2867 scan.timeout.work);
2868
2869 mutex_lock(&ar->conf_mutex);
2870 ath10k_scan_abort(ar);
2871 mutex_unlock(&ar->conf_mutex);
2872}
2873
Kalle Valo5e3dd152013-06-12 20:52:10 +03002874static int ath10k_start_scan(struct ath10k *ar,
2875 const struct wmi_start_scan_arg *arg)
2876{
2877 int ret;
2878
2879 lockdep_assert_held(&ar->conf_mutex);
2880
2881 ret = ath10k_wmi_start_scan(ar, arg);
2882 if (ret)
2883 return ret;
2884
Kalle Valo5e3dd152013-06-12 20:52:10 +03002885 ret = wait_for_completion_timeout(&ar->scan.started, 1*HZ);
2886 if (ret == 0) {
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002887 ret = ath10k_scan_stop(ar);
2888 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002889 ath10k_warn(ar, "failed to stop scan: %d\n", ret);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002890
2891 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002892 }
2893
Ben Greear2f9eec02015-02-15 16:50:38 +02002894 /* If we failed to start the scan, return error code at
2895 * this point. This is probably due to some issue in the
2896 * firmware, but no need to wedge the driver due to that...
2897 */
2898 spin_lock_bh(&ar->data_lock);
2899 if (ar->scan.state == ATH10K_SCAN_IDLE) {
2900 spin_unlock_bh(&ar->data_lock);
2901 return -EINVAL;
2902 }
2903 spin_unlock_bh(&ar->data_lock);
2904
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002905 /* Add a 200ms margin to account for event/command processing */
2906 ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
2907 msecs_to_jiffies(arg->max_scan_time+200));
Kalle Valo5e3dd152013-06-12 20:52:10 +03002908 return 0;
2909}
2910
2911/**********************/
2912/* mac80211 callbacks */
2913/**********************/
2914
2915static void ath10k_tx(struct ieee80211_hw *hw,
2916 struct ieee80211_tx_control *control,
2917 struct sk_buff *skb)
2918{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002919 struct ath10k *ar = hw->priv;
Michal Kazior4b604552014-07-21 21:03:09 +03002920 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2921 struct ieee80211_vif *vif = info->control.vif;
Michal Kazior4b604552014-07-21 21:03:09 +03002922 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002923
2924 /* We should disable CCK RATE due to P2P */
2925 if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002926 ath10k_dbg(ar, ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002927
Michal Kazior4b604552014-07-21 21:03:09 +03002928 ATH10K_SKB_CB(skb)->htt.is_offchan = false;
2929 ATH10K_SKB_CB(skb)->htt.tid = ath10k_tx_h_get_tid(hdr);
Michal Kazior2b37c292014-09-02 11:00:22 +03002930 ATH10K_SKB_CB(skb)->vdev_id = ath10k_tx_h_get_vdev_id(ar, vif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002931
Michal Kaziorcf84bd42013-07-16 11:04:54 +02002932 /* it makes no sense to process injected frames like that */
Michal Kazior4b604552014-07-21 21:03:09 +03002933 if (vif && vif->type != NL80211_IFTYPE_MONITOR) {
2934 ath10k_tx_h_nwifi(hw, skb);
Michal Kazior4b604552014-07-21 21:03:09 +03002935 ath10k_tx_h_add_p2p_noa_ie(ar, vif, skb);
2936 ath10k_tx_h_seq_no(vif, skb);
Michal Kaziorcf84bd42013-07-16 11:04:54 +02002937 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002938
Kalle Valo5e3dd152013-06-12 20:52:10 +03002939 if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
2940 spin_lock_bh(&ar->data_lock);
Michal Kazior8d6d3622014-11-24 14:58:31 +01002941 ATH10K_SKB_CB(skb)->htt.freq = ar->scan.roc_freq;
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002942 ATH10K_SKB_CB(skb)->vdev_id = ar->scan.vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002943 spin_unlock_bh(&ar->data_lock);
2944
Michal Kazior8d6d3622014-11-24 14:58:31 +01002945 if (ath10k_mac_need_offchan_tx_work(ar)) {
2946 ATH10K_SKB_CB(skb)->htt.freq = 0;
2947 ATH10K_SKB_CB(skb)->htt.is_offchan = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002948
Michal Kazior8d6d3622014-11-24 14:58:31 +01002949 ath10k_dbg(ar, ATH10K_DBG_MAC, "queued offchannel skb %p\n",
2950 skb);
2951
2952 skb_queue_tail(&ar->offchan_tx_queue, skb);
2953 ieee80211_queue_work(hw, &ar->offchan_tx_work);
2954 return;
2955 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002956 }
2957
2958 ath10k_tx_htt(ar, skb);
2959}
2960
Michal Kaziorbca7baf2014-05-26 12:46:03 +03002961/* Must not be called with conf_mutex held as workers can use that also. */
Michal Kazior7962b0d2014-10-28 10:34:38 +01002962void ath10k_drain_tx(struct ath10k *ar)
Michal Kaziorbca7baf2014-05-26 12:46:03 +03002963{
2964 /* make sure rcu-protected mac80211 tx path itself is drained */
2965 synchronize_net();
2966
2967 ath10k_offchan_tx_purge(ar);
2968 ath10k_mgmt_over_wmi_tx_purge(ar);
2969
2970 cancel_work_sync(&ar->offchan_tx_work);
2971 cancel_work_sync(&ar->wmi_mgmt_tx_work);
2972}
2973
Michal Kazioraffd3212013-07-16 09:54:35 +02002974void ath10k_halt(struct ath10k *ar)
Michal Kazior818bdd12013-07-16 09:38:57 +02002975{
Michal Kaziord9bc4b92014-04-23 19:30:06 +03002976 struct ath10k_vif *arvif;
2977
Michal Kazior818bdd12013-07-16 09:38:57 +02002978 lockdep_assert_held(&ar->conf_mutex);
2979
Michal Kazior19337472014-08-28 12:58:16 +02002980 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
2981 ar->filter_flags = 0;
2982 ar->monitor = false;
2983
2984 if (ar->monitor_started)
Michal Kazior1bbc0972014-04-08 09:45:47 +03002985 ath10k_monitor_stop(ar);
Michal Kazior19337472014-08-28 12:58:16 +02002986
2987 ar->monitor_started = false;
Michal Kazior1bbc0972014-04-08 09:45:47 +03002988
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002989 ath10k_scan_finish(ar);
Michal Kazior818bdd12013-07-16 09:38:57 +02002990 ath10k_peer_cleanup_all(ar);
2991 ath10k_core_stop(ar);
2992 ath10k_hif_power_down(ar);
2993
2994 spin_lock_bh(&ar->data_lock);
Michal Kazior64badcb2014-09-18 11:18:02 +03002995 list_for_each_entry(arvif, &ar->arvifs, list)
2996 ath10k_mac_vif_beacon_cleanup(arvif);
Michal Kazior818bdd12013-07-16 09:38:57 +02002997 spin_unlock_bh(&ar->data_lock);
2998}
2999
Ben Greear46acf7b2014-05-16 17:15:38 +03003000static int ath10k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
3001{
3002 struct ath10k *ar = hw->priv;
3003
3004 mutex_lock(&ar->conf_mutex);
3005
3006 if (ar->cfg_tx_chainmask) {
3007 *tx_ant = ar->cfg_tx_chainmask;
3008 *rx_ant = ar->cfg_rx_chainmask;
3009 } else {
3010 *tx_ant = ar->supp_tx_chainmask;
3011 *rx_ant = ar->supp_rx_chainmask;
3012 }
3013
3014 mutex_unlock(&ar->conf_mutex);
3015
3016 return 0;
3017}
3018
Ben Greear5572a952014-11-24 16:22:10 +02003019static void ath10k_check_chain_mask(struct ath10k *ar, u32 cm, const char *dbg)
3020{
3021 /* It is not clear that allowing gaps in chainmask
3022 * is helpful. Probably it will not do what user
3023 * is hoping for, so warn in that case.
3024 */
3025 if (cm == 15 || cm == 7 || cm == 3 || cm == 1 || cm == 0)
3026 return;
3027
3028 ath10k_warn(ar, "mac %s antenna chainmask may be invalid: 0x%x. Suggested values: 15, 7, 3, 1 or 0.\n",
3029 dbg, cm);
3030}
3031
Ben Greear46acf7b2014-05-16 17:15:38 +03003032static int __ath10k_set_antenna(struct ath10k *ar, u32 tx_ant, u32 rx_ant)
3033{
3034 int ret;
3035
3036 lockdep_assert_held(&ar->conf_mutex);
3037
Ben Greear5572a952014-11-24 16:22:10 +02003038 ath10k_check_chain_mask(ar, tx_ant, "tx");
3039 ath10k_check_chain_mask(ar, rx_ant, "rx");
3040
Ben Greear46acf7b2014-05-16 17:15:38 +03003041 ar->cfg_tx_chainmask = tx_ant;
3042 ar->cfg_rx_chainmask = rx_ant;
3043
3044 if ((ar->state != ATH10K_STATE_ON) &&
3045 (ar->state != ATH10K_STATE_RESTARTED))
3046 return 0;
3047
3048 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->tx_chain_mask,
3049 tx_ant);
3050 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003051 ath10k_warn(ar, "failed to set tx-chainmask: %d, req 0x%x\n",
Ben Greear46acf7b2014-05-16 17:15:38 +03003052 ret, tx_ant);
3053 return ret;
3054 }
3055
3056 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->rx_chain_mask,
3057 rx_ant);
3058 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003059 ath10k_warn(ar, "failed to set rx-chainmask: %d, req 0x%x\n",
Ben Greear46acf7b2014-05-16 17:15:38 +03003060 ret, rx_ant);
3061 return ret;
3062 }
3063
3064 return 0;
3065}
3066
3067static int ath10k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
3068{
3069 struct ath10k *ar = hw->priv;
3070 int ret;
3071
3072 mutex_lock(&ar->conf_mutex);
3073 ret = __ath10k_set_antenna(ar, tx_ant, rx_ant);
3074 mutex_unlock(&ar->conf_mutex);
3075 return ret;
3076}
3077
Kalle Valo5e3dd152013-06-12 20:52:10 +03003078static int ath10k_start(struct ieee80211_hw *hw)
3079{
3080 struct ath10k *ar = hw->priv;
Michal Kazior818bdd12013-07-16 09:38:57 +02003081 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003082
Michal Kaziorbca7baf2014-05-26 12:46:03 +03003083 /*
3084 * This makes sense only when restarting hw. It is harmless to call
3085 * uncoditionally. This is necessary to make sure no HTT/WMI tx
3086 * commands will be submitted while restarting.
3087 */
3088 ath10k_drain_tx(ar);
3089
Michal Kazior548db542013-07-05 16:15:15 +03003090 mutex_lock(&ar->conf_mutex);
3091
Michal Kaziorc5058f52014-05-26 12:46:03 +03003092 switch (ar->state) {
3093 case ATH10K_STATE_OFF:
3094 ar->state = ATH10K_STATE_ON;
3095 break;
3096 case ATH10K_STATE_RESTARTING:
3097 ath10k_halt(ar);
3098 ar->state = ATH10K_STATE_RESTARTED;
3099 break;
3100 case ATH10K_STATE_ON:
3101 case ATH10K_STATE_RESTARTED:
3102 case ATH10K_STATE_WEDGED:
3103 WARN_ON(1);
Michal Kazior818bdd12013-07-16 09:38:57 +02003104 ret = -EINVAL;
Michal Kaziorae254432014-05-26 12:46:02 +03003105 goto err;
Kalle Valo43d2a302014-09-10 18:23:30 +03003106 case ATH10K_STATE_UTF:
3107 ret = -EBUSY;
3108 goto err;
Michal Kazior818bdd12013-07-16 09:38:57 +02003109 }
3110
3111 ret = ath10k_hif_power_up(ar);
3112 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003113 ath10k_err(ar, "Could not init hif: %d\n", ret);
Michal Kaziorae254432014-05-26 12:46:02 +03003114 goto err_off;
Michal Kazior818bdd12013-07-16 09:38:57 +02003115 }
3116
Kalle Valo43d2a302014-09-10 18:23:30 +03003117 ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_NORMAL);
Michal Kazior818bdd12013-07-16 09:38:57 +02003118 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003119 ath10k_err(ar, "Could not init core: %d\n", ret);
Michal Kaziorae254432014-05-26 12:46:02 +03003120 goto err_power_down;
Michal Kazior818bdd12013-07-16 09:38:57 +02003121 }
3122
Bartosz Markowski226a3392013-09-26 17:47:16 +02003123 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->pmf_qos, 1);
Michal Kaziorae254432014-05-26 12:46:02 +03003124 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003125 ath10k_warn(ar, "failed to enable PMF QOS: %d\n", ret);
Michal Kaziorae254432014-05-26 12:46:02 +03003126 goto err_core_stop;
3127 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003128
Michal Kaziorc4dd0d02013-11-13 11:05:10 +01003129 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->dynamic_bw, 1);
Michal Kaziorae254432014-05-26 12:46:02 +03003130 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003131 ath10k_warn(ar, "failed to enable dynamic BW: %d\n", ret);
Michal Kaziorae254432014-05-26 12:46:02 +03003132 goto err_core_stop;
3133 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003134
Ben Greear46acf7b2014-05-16 17:15:38 +03003135 if (ar->cfg_tx_chainmask)
3136 __ath10k_set_antenna(ar, ar->cfg_tx_chainmask,
3137 ar->cfg_rx_chainmask);
3138
Marek Puzyniakab6258e2014-01-29 15:03:31 +02003139 /*
3140 * By default FW set ARP frames ac to voice (6). In that case ARP
3141 * exchange is not working properly for UAPSD enabled AP. ARP requests
3142 * which arrives with access category 0 are processed by network stack
3143 * and send back with access category 0, but FW changes access category
3144 * to 6. Set ARP frames access category to best effort (0) solves
3145 * this problem.
3146 */
3147
3148 ret = ath10k_wmi_pdev_set_param(ar,
3149 ar->wmi.pdev_param->arp_ac_override, 0);
3150 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003151 ath10k_warn(ar, "failed to set arp ac override parameter: %d\n",
Marek Puzyniakab6258e2014-01-29 15:03:31 +02003152 ret);
Michal Kaziorae254432014-05-26 12:46:02 +03003153 goto err_core_stop;
Marek Puzyniakab6258e2014-01-29 15:03:31 +02003154 }
3155
Michal Kaziord6500972014-04-08 09:56:09 +03003156 ar->num_started_vdevs = 0;
Michal Kaziorf7843d72013-07-16 09:38:52 +02003157 ath10k_regd_update(ar);
3158
Simon Wunderlich855aed12014-08-02 09:12:54 +03003159 ath10k_spectral_start(ar);
3160
Michal Kaziorae254432014-05-26 12:46:02 +03003161 mutex_unlock(&ar->conf_mutex);
3162 return 0;
3163
3164err_core_stop:
3165 ath10k_core_stop(ar);
3166
3167err_power_down:
3168 ath10k_hif_power_down(ar);
3169
3170err_off:
3171 ar->state = ATH10K_STATE_OFF;
3172
3173err:
Michal Kazior548db542013-07-05 16:15:15 +03003174 mutex_unlock(&ar->conf_mutex);
Michal Kaziorc60bdd82014-01-29 07:26:31 +01003175 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003176}
3177
3178static void ath10k_stop(struct ieee80211_hw *hw)
3179{
3180 struct ath10k *ar = hw->priv;
3181
Michal Kaziorbca7baf2014-05-26 12:46:03 +03003182 ath10k_drain_tx(ar);
3183
Michal Kazior548db542013-07-05 16:15:15 +03003184 mutex_lock(&ar->conf_mutex);
Michal Kaziorc5058f52014-05-26 12:46:03 +03003185 if (ar->state != ATH10K_STATE_OFF) {
Michal Kazior818bdd12013-07-16 09:38:57 +02003186 ath10k_halt(ar);
Michal Kaziorc5058f52014-05-26 12:46:03 +03003187 ar->state = ATH10K_STATE_OFF;
3188 }
Michal Kazior548db542013-07-05 16:15:15 +03003189 mutex_unlock(&ar->conf_mutex);
3190
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003191 cancel_delayed_work_sync(&ar->scan.timeout);
Michal Kazioraffd3212013-07-16 09:54:35 +02003192 cancel_work_sync(&ar->restart_work);
3193}
3194
Michal Kaziorad088bf2013-10-16 15:44:46 +03003195static int ath10k_config_ps(struct ath10k *ar)
Michal Kazioraffd3212013-07-16 09:54:35 +02003196{
Michal Kaziorad088bf2013-10-16 15:44:46 +03003197 struct ath10k_vif *arvif;
3198 int ret = 0;
Michal Kazioraffd3212013-07-16 09:54:35 +02003199
3200 lockdep_assert_held(&ar->conf_mutex);
3201
Michal Kaziorad088bf2013-10-16 15:44:46 +03003202 list_for_each_entry(arvif, &ar->arvifs, list) {
3203 ret = ath10k_mac_vif_setup_ps(arvif);
3204 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003205 ath10k_warn(ar, "failed to setup powersave: %d\n", ret);
Michal Kaziorad088bf2013-10-16 15:44:46 +03003206 break;
3207 }
3208 }
Michal Kazioraffd3212013-07-16 09:54:35 +02003209
Michal Kaziorad088bf2013-10-16 15:44:46 +03003210 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003211}
3212
Michal Kaziorc930f742014-01-23 11:38:25 +01003213static const char *chandef_get_width(enum nl80211_chan_width width)
3214{
3215 switch (width) {
3216 case NL80211_CHAN_WIDTH_20_NOHT:
3217 return "20 (noht)";
3218 case NL80211_CHAN_WIDTH_20:
3219 return "20";
3220 case NL80211_CHAN_WIDTH_40:
3221 return "40";
3222 case NL80211_CHAN_WIDTH_80:
3223 return "80";
3224 case NL80211_CHAN_WIDTH_80P80:
3225 return "80+80";
3226 case NL80211_CHAN_WIDTH_160:
3227 return "160";
3228 case NL80211_CHAN_WIDTH_5:
3229 return "5";
3230 case NL80211_CHAN_WIDTH_10:
3231 return "10";
3232 }
3233 return "?";
3234}
3235
3236static void ath10k_config_chan(struct ath10k *ar)
3237{
3238 struct ath10k_vif *arvif;
Michal Kaziorc930f742014-01-23 11:38:25 +01003239 int ret;
3240
3241 lockdep_assert_held(&ar->conf_mutex);
3242
Michal Kazior7aa7a722014-08-25 12:09:38 +02003243 ath10k_dbg(ar, ATH10K_DBG_MAC,
Michal Kaziorc930f742014-01-23 11:38:25 +01003244 "mac config channel to %dMHz (cf1 %dMHz cf2 %dMHz width %s)\n",
3245 ar->chandef.chan->center_freq,
3246 ar->chandef.center_freq1,
3247 ar->chandef.center_freq2,
3248 chandef_get_width(ar->chandef.width));
3249
3250 /* First stop monitor interface. Some FW versions crash if there's a
3251 * lone monitor interface. */
Michal Kazior1bbc0972014-04-08 09:45:47 +03003252 if (ar->monitor_started)
Michal Kazior19337472014-08-28 12:58:16 +02003253 ath10k_monitor_stop(ar);
Michal Kaziorc930f742014-01-23 11:38:25 +01003254
3255 list_for_each_entry(arvif, &ar->arvifs, list) {
3256 if (!arvif->is_started)
3257 continue;
3258
Michal Kaziordc55e302014-07-29 12:53:36 +03003259 if (!arvif->is_up)
3260 continue;
3261
Michal Kaziorc930f742014-01-23 11:38:25 +01003262 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
3263 continue;
3264
Michal Kaziordc55e302014-07-29 12:53:36 +03003265 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
Michal Kaziorc930f742014-01-23 11:38:25 +01003266 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003267 ath10k_warn(ar, "failed to down vdev %d: %d\n",
Michal Kaziorc930f742014-01-23 11:38:25 +01003268 arvif->vdev_id, ret);
3269 continue;
3270 }
3271 }
3272
Michal Kaziordc55e302014-07-29 12:53:36 +03003273 /* all vdevs are downed now - attempt to restart and re-up them */
Michal Kaziorc930f742014-01-23 11:38:25 +01003274
3275 list_for_each_entry(arvif, &ar->arvifs, list) {
3276 if (!arvif->is_started)
3277 continue;
3278
3279 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
3280 continue;
3281
Michal Kazior81a9a172015-03-05 16:02:17 +02003282 ret = ath10k_mac_setup_bcn_tmpl(arvif);
3283 if (ret)
3284 ath10k_warn(ar, "failed to update bcn tmpl during csa: %d\n",
3285 ret);
3286
3287 ret = ath10k_mac_setup_prb_tmpl(arvif);
3288 if (ret)
3289 ath10k_warn(ar, "failed to update prb tmpl during csa: %d\n",
3290 ret);
3291
Michal Kaziordc55e302014-07-29 12:53:36 +03003292 ret = ath10k_vdev_restart(arvif);
Michal Kaziorc930f742014-01-23 11:38:25 +01003293 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003294 ath10k_warn(ar, "failed to restart vdev %d: %d\n",
Michal Kaziorc930f742014-01-23 11:38:25 +01003295 arvif->vdev_id, ret);
3296 continue;
3297 }
3298
3299 if (!arvif->is_up)
3300 continue;
3301
3302 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
3303 arvif->bssid);
3304 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003305 ath10k_warn(ar, "failed to bring vdev up %d: %d\n",
Michal Kaziorc930f742014-01-23 11:38:25 +01003306 arvif->vdev_id, ret);
3307 continue;
3308 }
3309 }
3310
Michal Kazior19337472014-08-28 12:58:16 +02003311 ath10k_monitor_recalc(ar);
Michal Kaziorc930f742014-01-23 11:38:25 +01003312}
3313
Michal Kazior7d9d5582014-10-21 10:40:15 +03003314static int ath10k_mac_txpower_setup(struct ath10k *ar, int txpower)
3315{
3316 int ret;
3317 u32 param;
3318
3319 lockdep_assert_held(&ar->conf_mutex);
3320
3321 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac txpower %d\n", txpower);
3322
3323 param = ar->wmi.pdev_param->txpower_limit2g;
3324 ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
3325 if (ret) {
3326 ath10k_warn(ar, "failed to set 2g txpower %d: %d\n",
3327 txpower, ret);
3328 return ret;
3329 }
3330
3331 param = ar->wmi.pdev_param->txpower_limit5g;
3332 ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
3333 if (ret) {
3334 ath10k_warn(ar, "failed to set 5g txpower %d: %d\n",
3335 txpower, ret);
3336 return ret;
3337 }
3338
3339 return 0;
3340}
3341
3342static int ath10k_mac_txpower_recalc(struct ath10k *ar)
3343{
3344 struct ath10k_vif *arvif;
3345 int ret, txpower = -1;
3346
3347 lockdep_assert_held(&ar->conf_mutex);
3348
3349 list_for_each_entry(arvif, &ar->arvifs, list) {
3350 WARN_ON(arvif->txpower < 0);
3351
3352 if (txpower == -1)
3353 txpower = arvif->txpower;
3354 else
3355 txpower = min(txpower, arvif->txpower);
3356 }
3357
3358 if (WARN_ON(txpower == -1))
3359 return -EINVAL;
3360
3361 ret = ath10k_mac_txpower_setup(ar, txpower);
3362 if (ret) {
3363 ath10k_warn(ar, "failed to setup tx power %d: %d\n",
3364 txpower, ret);
3365 return ret;
3366 }
3367
3368 return 0;
3369}
3370
Kalle Valo5e3dd152013-06-12 20:52:10 +03003371static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
3372{
Kalle Valo5e3dd152013-06-12 20:52:10 +03003373 struct ath10k *ar = hw->priv;
3374 struct ieee80211_conf *conf = &hw->conf;
3375 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003376
3377 mutex_lock(&ar->conf_mutex);
3378
3379 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003380 ath10k_dbg(ar, ATH10K_DBG_MAC,
Michal Kaziord6500972014-04-08 09:56:09 +03003381 "mac config channel %dMHz flags 0x%x radar %d\n",
Marek Puzyniake8a50f82013-11-20 09:59:47 +02003382 conf->chandef.chan->center_freq,
Michal Kaziord6500972014-04-08 09:56:09 +03003383 conf->chandef.chan->flags,
3384 conf->radar_enabled);
Marek Puzyniake8a50f82013-11-20 09:59:47 +02003385
Kalle Valo5e3dd152013-06-12 20:52:10 +03003386 spin_lock_bh(&ar->data_lock);
3387 ar->rx_channel = conf->chandef.chan;
3388 spin_unlock_bh(&ar->data_lock);
Marek Puzyniake8a50f82013-11-20 09:59:47 +02003389
Michal Kaziord6500972014-04-08 09:56:09 +03003390 ar->radar_enabled = conf->radar_enabled;
3391 ath10k_recalc_radar_detection(ar);
Michal Kaziorc930f742014-01-23 11:38:25 +01003392
3393 if (!cfg80211_chandef_identical(&ar->chandef, &conf->chandef)) {
3394 ar->chandef = conf->chandef;
3395 ath10k_config_chan(ar);
3396 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003397 }
3398
Michal Kazioraffd3212013-07-16 09:54:35 +02003399 if (changed & IEEE80211_CONF_CHANGE_PS)
3400 ath10k_config_ps(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003401
3402 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
Michal Kazior19337472014-08-28 12:58:16 +02003403 ar->monitor = conf->flags & IEEE80211_CONF_MONITOR;
3404 ret = ath10k_monitor_recalc(ar);
3405 if (ret)
3406 ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003407 }
3408
3409 mutex_unlock(&ar->conf_mutex);
3410 return ret;
3411}
3412
Ben Greear5572a952014-11-24 16:22:10 +02003413static u32 get_nss_from_chainmask(u16 chain_mask)
3414{
3415 if ((chain_mask & 0x15) == 0x15)
3416 return 4;
3417 else if ((chain_mask & 0x7) == 0x7)
3418 return 3;
3419 else if ((chain_mask & 0x3) == 0x3)
3420 return 2;
3421 return 1;
3422}
3423
Kalle Valo5e3dd152013-06-12 20:52:10 +03003424/*
3425 * TODO:
3426 * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
3427 * because we will send mgmt frames without CCK. This requirement
3428 * for P2P_FIND/GO_NEG should be handled by checking CCK flag
3429 * in the TX packet.
3430 */
3431static int ath10k_add_interface(struct ieee80211_hw *hw,
3432 struct ieee80211_vif *vif)
3433{
3434 struct ath10k *ar = hw->priv;
3435 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3436 enum wmi_sta_powersave_param param;
3437 int ret = 0;
Kalle Valo5a13e762014-01-20 11:01:46 +02003438 u32 value;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003439 int bit;
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003440 u32 vdev_param;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003441
Johannes Berg848955c2014-11-11 12:48:42 +01003442 vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
3443
Kalle Valo5e3dd152013-06-12 20:52:10 +03003444 mutex_lock(&ar->conf_mutex);
3445
Michal Kazior0dbd09e2013-07-31 10:55:14 +02003446 memset(arvif, 0, sizeof(*arvif));
3447
Kalle Valo5e3dd152013-06-12 20:52:10 +03003448 arvif->ar = ar;
3449 arvif->vif = vif;
3450
Ben Greeare63b33f2013-10-22 14:54:14 -07003451 INIT_LIST_HEAD(&arvif->list);
Michal Kazior81a9a172015-03-05 16:02:17 +02003452 INIT_WORK(&arvif->ap_csa_work, ath10k_mac_vif_ap_csa_work);
Michal Kaziorcc9904e2015-03-10 16:22:01 +02003453 INIT_DELAYED_WORK(&arvif->connection_loss_work,
3454 ath10k_mac_vif_sta_connection_loss_work);
Michal Kaziorcc4827b2013-10-16 15:44:45 +03003455
Ben Greeara9aefb32014-08-12 11:02:19 +03003456 if (ar->free_vdev_map == 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003457 ath10k_warn(ar, "Free vdev map is empty, no more interfaces allowed.\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03003458 ret = -EBUSY;
Michal Kazior9dad14a2013-10-16 15:44:45 +03003459 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003460 }
Ben Greear16c11172014-09-23 14:17:16 -07003461 bit = __ffs64(ar->free_vdev_map);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003462
Ben Greear16c11172014-09-23 14:17:16 -07003463 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac create vdev %i map %llx\n",
3464 bit, ar->free_vdev_map);
3465
3466 arvif->vdev_id = bit;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003467 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003468
Kalle Valo5e3dd152013-06-12 20:52:10 +03003469 switch (vif->type) {
Michal Kazior75d2bd42014-12-12 12:41:39 +01003470 case NL80211_IFTYPE_P2P_DEVICE:
3471 arvif->vdev_type = WMI_VDEV_TYPE_STA;
3472 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
3473 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003474 case NL80211_IFTYPE_UNSPECIFIED:
3475 case NL80211_IFTYPE_STATION:
3476 arvif->vdev_type = WMI_VDEV_TYPE_STA;
3477 if (vif->p2p)
3478 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
3479 break;
3480 case NL80211_IFTYPE_ADHOC:
3481 arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
3482 break;
3483 case NL80211_IFTYPE_AP:
3484 arvif->vdev_type = WMI_VDEV_TYPE_AP;
3485
3486 if (vif->p2p)
3487 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
3488 break;
3489 case NL80211_IFTYPE_MONITOR:
3490 arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
3491 break;
3492 default:
3493 WARN_ON(1);
3494 break;
3495 }
3496
Michal Kazior64badcb2014-09-18 11:18:02 +03003497 /* Some firmware revisions don't wait for beacon tx completion before
3498 * sending another SWBA event. This could lead to hardware using old
3499 * (freed) beacon data in some cases, e.g. tx credit starvation
3500 * combined with missed TBTT. This is very very rare.
3501 *
3502 * On non-IOMMU-enabled hosts this could be a possible security issue
3503 * because hw could beacon some random data on the air. On
3504 * IOMMU-enabled hosts DMAR faults would occur in most cases and target
3505 * device would crash.
3506 *
3507 * Since there are no beacon tx completions (implicit nor explicit)
3508 * propagated to host the only workaround for this is to allocate a
3509 * DMA-coherent buffer for a lifetime of a vif and use it for all
3510 * beacon tx commands. Worst case for this approach is some beacons may
3511 * become corrupted, e.g. have garbled IEs or out-of-date TIM bitmap.
3512 */
3513 if (vif->type == NL80211_IFTYPE_ADHOC ||
3514 vif->type == NL80211_IFTYPE_AP) {
3515 arvif->beacon_buf = dma_zalloc_coherent(ar->dev,
3516 IEEE80211_MAX_FRAME_LEN,
3517 &arvif->beacon_paddr,
Rajkumar Manoharan82d7aba2014-10-10 17:38:27 +05303518 GFP_ATOMIC);
Michal Kazior64badcb2014-09-18 11:18:02 +03003519 if (!arvif->beacon_buf) {
3520 ret = -ENOMEM;
3521 ath10k_warn(ar, "failed to allocate beacon buffer: %d\n",
3522 ret);
3523 goto err;
3524 }
3525 }
3526
3527 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d bcnmode %s\n",
3528 arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
3529 arvif->beacon_buf ? "single-buf" : "per-skb");
Kalle Valo5e3dd152013-06-12 20:52:10 +03003530
3531 ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
3532 arvif->vdev_subtype, vif->addr);
3533 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003534 ath10k_warn(ar, "failed to create WMI vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003535 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003536 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003537 }
3538
Ben Greear16c11172014-09-23 14:17:16 -07003539 ar->free_vdev_map &= ~(1LL << arvif->vdev_id);
Michal Kazior05791192013-10-16 15:44:45 +03003540 list_add(&arvif->list, &ar->arvifs);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003541
Michal Kazior46725b12015-01-28 09:57:49 +02003542 /* It makes no sense to have firmware do keepalives. mac80211 already
3543 * takes care of this with idle connection polling.
3544 */
3545 ret = ath10k_mac_vif_disable_keepalive(arvif);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003546 if (ret) {
Michal Kazior46725b12015-01-28 09:57:49 +02003547 ath10k_warn(ar, "failed to disable keepalive on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003548 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003549 goto err_vdev_delete;
3550 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003551
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +02003552 arvif->def_wep_key_idx = -1;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003553
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003554 vdev_param = ar->wmi.vdev_param->tx_encap_type;
3555 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003556 ATH10K_HW_TXRX_NATIVE_WIFI);
Bartosz Markowskiebc9abd2013-10-15 09:26:20 +02003557 /* 10.X firmware does not support this VDEV parameter. Do not warn */
Michal Kazior9dad14a2013-10-16 15:44:45 +03003558 if (ret && ret != -EOPNOTSUPP) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003559 ath10k_warn(ar, "failed to set vdev %i TX encapsulation: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003560 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003561 goto err_vdev_delete;
3562 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003563
Ben Greear5572a952014-11-24 16:22:10 +02003564 if (ar->cfg_tx_chainmask) {
3565 u16 nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
3566
3567 vdev_param = ar->wmi.vdev_param->nss;
3568 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3569 nss);
3570 if (ret) {
3571 ath10k_warn(ar, "failed to set vdev %i chainmask 0x%x, nss %i: %d\n",
3572 arvif->vdev_id, ar->cfg_tx_chainmask, nss,
3573 ret);
3574 goto err_vdev_delete;
3575 }
3576 }
3577
Kalle Valo5e3dd152013-06-12 20:52:10 +03003578 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3579 ret = ath10k_peer_create(ar, arvif->vdev_id, vif->addr);
3580 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003581 ath10k_warn(ar, "failed to create vdev %i peer for AP: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003582 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003583 goto err_vdev_delete;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003584 }
Marek Puzyniakcdf07402013-12-30 09:07:51 +01003585
Kalle Valo5a13e762014-01-20 11:01:46 +02003586 ret = ath10k_mac_set_kickout(arvif);
3587 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003588 ath10k_warn(ar, "failed to set vdev %i kickout parameters: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003589 arvif->vdev_id, ret);
Kalle Valo5a13e762014-01-20 11:01:46 +02003590 goto err_peer_delete;
3591 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003592 }
3593
3594 if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
3595 param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
3596 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
3597 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3598 param, value);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003599 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003600 ath10k_warn(ar, "failed to set vdev %i RX wake policy: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003601 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003602 goto err_peer_delete;
3603 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003604
Michal Kazior9f9b5742014-12-12 12:41:36 +01003605 ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003606 if (ret) {
Michal Kazior9f9b5742014-12-12 12:41:36 +01003607 ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003608 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003609 goto err_peer_delete;
3610 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003611
Michal Kazior9f9b5742014-12-12 12:41:36 +01003612 ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003613 if (ret) {
Michal Kazior9f9b5742014-12-12 12:41:36 +01003614 ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003615 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003616 goto err_peer_delete;
3617 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003618 }
3619
Michal Kazior424121c2013-07-22 14:13:31 +02003620 ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003621 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003622 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
Michal Kazior679c54a2013-07-05 16:15:04 +03003623 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003624 goto err_peer_delete;
3625 }
Michal Kazior679c54a2013-07-05 16:15:04 +03003626
Michal Kazior424121c2013-07-22 14:13:31 +02003627 ret = ath10k_mac_set_frag(arvif, ar->hw->wiphy->frag_threshold);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003628 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003629 ath10k_warn(ar, "failed to set frag threshold for vdev %d: %d\n",
Michal Kazior679c54a2013-07-05 16:15:04 +03003630 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003631 goto err_peer_delete;
3632 }
Michal Kazior679c54a2013-07-05 16:15:04 +03003633
Michal Kazior7d9d5582014-10-21 10:40:15 +03003634 arvif->txpower = vif->bss_conf.txpower;
3635 ret = ath10k_mac_txpower_recalc(ar);
3636 if (ret) {
3637 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
3638 goto err_peer_delete;
3639 }
3640
Kalle Valo5e3dd152013-06-12 20:52:10 +03003641 mutex_unlock(&ar->conf_mutex);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003642 return 0;
3643
3644err_peer_delete:
3645 if (arvif->vdev_type == WMI_VDEV_TYPE_AP)
3646 ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
3647
3648err_vdev_delete:
3649 ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
Ben Greear16c11172014-09-23 14:17:16 -07003650 ar->free_vdev_map |= 1LL << arvif->vdev_id;
Michal Kazior05791192013-10-16 15:44:45 +03003651 list_del(&arvif->list);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003652
3653err:
Michal Kazior64badcb2014-09-18 11:18:02 +03003654 if (arvif->beacon_buf) {
3655 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
3656 arvif->beacon_buf, arvif->beacon_paddr);
3657 arvif->beacon_buf = NULL;
3658 }
3659
Michal Kazior9dad14a2013-10-16 15:44:45 +03003660 mutex_unlock(&ar->conf_mutex);
3661
Kalle Valo5e3dd152013-06-12 20:52:10 +03003662 return ret;
3663}
3664
3665static void ath10k_remove_interface(struct ieee80211_hw *hw,
3666 struct ieee80211_vif *vif)
3667{
3668 struct ath10k *ar = hw->priv;
3669 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3670 int ret;
3671
Michal Kazior81a9a172015-03-05 16:02:17 +02003672 cancel_work_sync(&arvif->ap_csa_work);
Michal Kaziorcc9904e2015-03-10 16:22:01 +02003673 cancel_delayed_work_sync(&arvif->connection_loss_work);
Michal Kazior81a9a172015-03-05 16:02:17 +02003674
Sujith Manoharan5d011f52014-11-25 11:47:00 +05303675 mutex_lock(&ar->conf_mutex);
3676
Michal Kaziored543882013-09-13 14:16:56 +02003677 spin_lock_bh(&ar->data_lock);
Michal Kazior64badcb2014-09-18 11:18:02 +03003678 ath10k_mac_vif_beacon_cleanup(arvif);
Michal Kaziored543882013-09-13 14:16:56 +02003679 spin_unlock_bh(&ar->data_lock);
3680
Simon Wunderlich855aed12014-08-02 09:12:54 +03003681 ret = ath10k_spectral_vif_stop(arvif);
3682 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003683 ath10k_warn(ar, "failed to stop spectral for vdev %i: %d\n",
Simon Wunderlich855aed12014-08-02 09:12:54 +03003684 arvif->vdev_id, ret);
3685
Ben Greear16c11172014-09-23 14:17:16 -07003686 ar->free_vdev_map |= 1LL << arvif->vdev_id;
Michal Kazior05791192013-10-16 15:44:45 +03003687 list_del(&arvif->list);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003688
3689 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
Michal Kazior2c512052015-02-15 16:50:40 +02003690 ret = ath10k_wmi_peer_delete(arvif->ar, arvif->vdev_id,
3691 vif->addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003692 if (ret)
Michal Kazior2c512052015-02-15 16:50:40 +02003693 ath10k_warn(ar, "failed to submit AP self-peer removal on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003694 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003695
3696 kfree(arvif->u.ap.noa_data);
3697 }
3698
Michal Kazior7aa7a722014-08-25 12:09:38 +02003699 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i delete (remove interface)\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03003700 arvif->vdev_id);
3701
Kalle Valo5e3dd152013-06-12 20:52:10 +03003702 ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
3703 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003704 ath10k_warn(ar, "failed to delete WMI vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003705 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003706
Michal Kazior2c512052015-02-15 16:50:40 +02003707 /* Some firmware revisions don't notify host about self-peer removal
3708 * until after associated vdev is deleted.
3709 */
3710 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3711 ret = ath10k_wait_for_peer_deleted(ar, arvif->vdev_id,
3712 vif->addr);
3713 if (ret)
3714 ath10k_warn(ar, "failed to remove AP self-peer on vdev %i: %d\n",
3715 arvif->vdev_id, ret);
3716
3717 spin_lock_bh(&ar->data_lock);
3718 ar->num_peers--;
3719 spin_unlock_bh(&ar->data_lock);
3720 }
3721
Kalle Valo5e3dd152013-06-12 20:52:10 +03003722 ath10k_peer_cleanup(ar, arvif->vdev_id);
3723
3724 mutex_unlock(&ar->conf_mutex);
3725}
3726
3727/*
3728 * FIXME: Has to be verified.
3729 */
3730#define SUPPORTED_FILTERS \
3731 (FIF_PROMISC_IN_BSS | \
3732 FIF_ALLMULTI | \
3733 FIF_CONTROL | \
3734 FIF_PSPOLL | \
3735 FIF_OTHER_BSS | \
3736 FIF_BCN_PRBRESP_PROMISC | \
3737 FIF_PROBE_REQ | \
3738 FIF_FCSFAIL)
3739
3740static void ath10k_configure_filter(struct ieee80211_hw *hw,
3741 unsigned int changed_flags,
3742 unsigned int *total_flags,
3743 u64 multicast)
3744{
3745 struct ath10k *ar = hw->priv;
3746 int ret;
3747
3748 mutex_lock(&ar->conf_mutex);
3749
3750 changed_flags &= SUPPORTED_FILTERS;
3751 *total_flags &= SUPPORTED_FILTERS;
3752 ar->filter_flags = *total_flags;
3753
Michal Kazior19337472014-08-28 12:58:16 +02003754 ret = ath10k_monitor_recalc(ar);
3755 if (ret)
3756 ath10k_warn(ar, "failed to recalc montior: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003757
3758 mutex_unlock(&ar->conf_mutex);
3759}
3760
3761static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
3762 struct ieee80211_vif *vif,
3763 struct ieee80211_bss_conf *info,
3764 u32 changed)
3765{
3766 struct ath10k *ar = hw->priv;
3767 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3768 int ret = 0;
Kalle Valoaf762c02014-09-14 12:50:17 +03003769 u32 vdev_param, pdev_param, slottime, preamble;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003770
3771 mutex_lock(&ar->conf_mutex);
3772
3773 if (changed & BSS_CHANGED_IBSS)
3774 ath10k_control_ibss(arvif, info, vif->addr);
3775
3776 if (changed & BSS_CHANGED_BEACON_INT) {
3777 arvif->beacon_interval = info->beacon_int;
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003778 vdev_param = ar->wmi.vdev_param->beacon_interval;
3779 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003780 arvif->beacon_interval);
Michal Kazior7aa7a722014-08-25 12:09:38 +02003781 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003782 "mac vdev %d beacon_interval %d\n",
3783 arvif->vdev_id, arvif->beacon_interval);
3784
Kalle Valo5e3dd152013-06-12 20:52:10 +03003785 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003786 ath10k_warn(ar, "failed to set beacon interval for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003787 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003788 }
3789
3790 if (changed & BSS_CHANGED_BEACON) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003791 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003792 "vdev %d set beacon tx mode to staggered\n",
3793 arvif->vdev_id);
3794
Bartosz Markowski226a3392013-09-26 17:47:16 +02003795 pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
3796 ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003797 WMI_BEACON_STAGGERED_MODE);
3798 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003799 ath10k_warn(ar, "failed to set beacon mode for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003800 arvif->vdev_id, ret);
Michal Kaziorfbb8f1b2015-01-13 16:30:12 +02003801
3802 ret = ath10k_mac_setup_bcn_tmpl(arvif);
3803 if (ret)
3804 ath10k_warn(ar, "failed to update beacon template: %d\n",
3805 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003806 }
3807
Michal Kaziorfbb8f1b2015-01-13 16:30:12 +02003808 if (changed & BSS_CHANGED_AP_PROBE_RESP) {
3809 ret = ath10k_mac_setup_prb_tmpl(arvif);
3810 if (ret)
3811 ath10k_warn(ar, "failed to setup probe resp template on vdev %i: %d\n",
3812 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003813 }
3814
Michal Kaziorba2479f2015-01-24 12:14:51 +02003815 if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03003816 arvif->dtim_period = info->dtim_period;
3817
Michal Kazior7aa7a722014-08-25 12:09:38 +02003818 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003819 "mac vdev %d dtim_period %d\n",
3820 arvif->vdev_id, arvif->dtim_period);
3821
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003822 vdev_param = ar->wmi.vdev_param->dtim_period;
3823 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003824 arvif->dtim_period);
3825 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003826 ath10k_warn(ar, "failed to set dtim period for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003827 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003828 }
3829
3830 if (changed & BSS_CHANGED_SSID &&
3831 vif->type == NL80211_IFTYPE_AP) {
3832 arvif->u.ap.ssid_len = info->ssid_len;
3833 if (info->ssid_len)
3834 memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
3835 arvif->u.ap.hidden_ssid = info->hidden_ssid;
3836 }
3837
Michal Kazior077efc82014-10-21 10:10:29 +03003838 if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
3839 ether_addr_copy(arvif->bssid, info->bssid);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003840
3841 if (changed & BSS_CHANGED_BEACON_ENABLED)
3842 ath10k_control_beaconing(arvif, info);
3843
3844 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02003845 arvif->use_cts_prot = info->use_cts_prot;
Michal Kazior7aa7a722014-08-25 12:09:38 +02003846 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d cts_prot %d\n",
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02003847 arvif->vdev_id, info->use_cts_prot);
Kalle Valo60c3daa2013-09-08 17:56:07 +03003848
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02003849 ret = ath10k_recalc_rtscts_prot(arvif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003850 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003851 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003852 arvif->vdev_id, ret);
Michal Kaziora87fd4b2015-03-02 11:21:17 +01003853
3854 vdev_param = ar->wmi.vdev_param->protection_mode;
3855 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3856 info->use_cts_prot ? 1 : 0);
3857 if (ret)
3858 ath10k_warn(ar, "failed to set protection mode %d on vdev %i: %d\n",
3859 info->use_cts_prot, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003860 }
3861
3862 if (changed & BSS_CHANGED_ERP_SLOT) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03003863 if (info->use_short_slot)
3864 slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
3865
3866 else
3867 slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
3868
Michal Kazior7aa7a722014-08-25 12:09:38 +02003869 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03003870 arvif->vdev_id, slottime);
3871
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003872 vdev_param = ar->wmi.vdev_param->slot_time;
3873 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003874 slottime);
3875 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003876 ath10k_warn(ar, "failed to set erp slot for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003877 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003878 }
3879
3880 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03003881 if (info->use_short_preamble)
3882 preamble = WMI_VDEV_PREAMBLE_SHORT;
3883 else
3884 preamble = WMI_VDEV_PREAMBLE_LONG;
3885
Michal Kazior7aa7a722014-08-25 12:09:38 +02003886 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003887 "mac vdev %d preamble %dn",
3888 arvif->vdev_id, preamble);
3889
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003890 vdev_param = ar->wmi.vdev_param->preamble;
3891 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003892 preamble);
3893 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003894 ath10k_warn(ar, "failed to set preamble for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003895 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003896 }
3897
3898 if (changed & BSS_CHANGED_ASSOC) {
Michal Kaziore556f112014-08-28 12:58:17 +02003899 if (info->assoc) {
3900 /* Workaround: Make sure monitor vdev is not running
3901 * when associating to prevent some firmware revisions
3902 * (e.g. 10.1 and 10.2) from crashing.
3903 */
3904 if (ar->monitor_started)
3905 ath10k_monitor_stop(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003906 ath10k_bss_assoc(hw, vif, info);
Michal Kaziore556f112014-08-28 12:58:17 +02003907 ath10k_monitor_recalc(ar);
Michal Kazior077efc82014-10-21 10:10:29 +03003908 } else {
3909 ath10k_bss_disassoc(hw, vif);
Michal Kaziore556f112014-08-28 12:58:17 +02003910 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003911 }
3912
Michal Kazior7d9d5582014-10-21 10:40:15 +03003913 if (changed & BSS_CHANGED_TXPOWER) {
3914 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev_id %i txpower %d\n",
3915 arvif->vdev_id, info->txpower);
3916
3917 arvif->txpower = info->txpower;
3918 ret = ath10k_mac_txpower_recalc(ar);
3919 if (ret)
3920 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
3921 }
3922
Michal Kaziorbf14e652014-12-12 12:41:38 +01003923 if (changed & BSS_CHANGED_PS) {
Michal Kaziorcffb41f2015-02-13 13:30:16 +01003924 arvif->ps = vif->bss_conf.ps;
3925
3926 ret = ath10k_config_ps(ar);
Michal Kaziorbf14e652014-12-12 12:41:38 +01003927 if (ret)
3928 ath10k_warn(ar, "failed to setup ps on vdev %i: %d\n",
3929 arvif->vdev_id, ret);
3930 }
3931
Kalle Valo5e3dd152013-06-12 20:52:10 +03003932 mutex_unlock(&ar->conf_mutex);
3933}
3934
3935static int ath10k_hw_scan(struct ieee80211_hw *hw,
3936 struct ieee80211_vif *vif,
David Spinadelc56ef672014-02-05 15:21:13 +02003937 struct ieee80211_scan_request *hw_req)
Kalle Valo5e3dd152013-06-12 20:52:10 +03003938{
3939 struct ath10k *ar = hw->priv;
3940 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
David Spinadelc56ef672014-02-05 15:21:13 +02003941 struct cfg80211_scan_request *req = &hw_req->req;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003942 struct wmi_start_scan_arg arg;
3943 int ret = 0;
3944 int i;
3945
3946 mutex_lock(&ar->conf_mutex);
3947
3948 spin_lock_bh(&ar->data_lock);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003949 switch (ar->scan.state) {
3950 case ATH10K_SCAN_IDLE:
3951 reinit_completion(&ar->scan.started);
3952 reinit_completion(&ar->scan.completed);
3953 ar->scan.state = ATH10K_SCAN_STARTING;
3954 ar->scan.is_roc = false;
3955 ar->scan.vdev_id = arvif->vdev_id;
3956 ret = 0;
3957 break;
3958 case ATH10K_SCAN_STARTING:
3959 case ATH10K_SCAN_RUNNING:
3960 case ATH10K_SCAN_ABORTING:
Kalle Valo5e3dd152013-06-12 20:52:10 +03003961 ret = -EBUSY;
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003962 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003963 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003964 spin_unlock_bh(&ar->data_lock);
3965
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003966 if (ret)
3967 goto exit;
3968
Kalle Valo5e3dd152013-06-12 20:52:10 +03003969 memset(&arg, 0, sizeof(arg));
3970 ath10k_wmi_start_scan_init(ar, &arg);
3971 arg.vdev_id = arvif->vdev_id;
3972 arg.scan_id = ATH10K_SCAN_ID;
3973
3974 if (!req->no_cck)
3975 arg.scan_ctrl_flags |= WMI_SCAN_ADD_CCK_RATES;
3976
3977 if (req->ie_len) {
3978 arg.ie_len = req->ie_len;
3979 memcpy(arg.ie, req->ie, arg.ie_len);
3980 }
3981
3982 if (req->n_ssids) {
3983 arg.n_ssids = req->n_ssids;
3984 for (i = 0; i < arg.n_ssids; i++) {
3985 arg.ssids[i].len = req->ssids[i].ssid_len;
3986 arg.ssids[i].ssid = req->ssids[i].ssid;
3987 }
Michal Kaziordcd4a562013-07-31 10:55:12 +02003988 } else {
3989 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003990 }
3991
3992 if (req->n_channels) {
3993 arg.n_channels = req->n_channels;
3994 for (i = 0; i < arg.n_channels; i++)
3995 arg.channels[i] = req->channels[i]->center_freq;
3996 }
3997
3998 ret = ath10k_start_scan(ar, &arg);
3999 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004000 ath10k_warn(ar, "failed to start hw scan: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004001 spin_lock_bh(&ar->data_lock);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004002 ar->scan.state = ATH10K_SCAN_IDLE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004003 spin_unlock_bh(&ar->data_lock);
4004 }
4005
4006exit:
4007 mutex_unlock(&ar->conf_mutex);
4008 return ret;
4009}
4010
4011static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
4012 struct ieee80211_vif *vif)
4013{
4014 struct ath10k *ar = hw->priv;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004015
4016 mutex_lock(&ar->conf_mutex);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004017 ath10k_scan_abort(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004018 mutex_unlock(&ar->conf_mutex);
Michal Kazior4eb2e162014-10-28 10:23:09 +01004019
4020 cancel_delayed_work_sync(&ar->scan.timeout);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004021}
4022
Michal Kaziorcfb27d22013-12-02 09:06:36 +01004023static void ath10k_set_key_h_def_keyidx(struct ath10k *ar,
4024 struct ath10k_vif *arvif,
4025 enum set_key_cmd cmd,
4026 struct ieee80211_key_conf *key)
4027{
4028 u32 vdev_param = arvif->ar->wmi.vdev_param->def_keyid;
4029 int ret;
4030
4031 /* 10.1 firmware branch requires default key index to be set to group
4032 * key index after installing it. Otherwise FW/HW Txes corrupted
4033 * frames with multi-vif APs. This is not required for main firmware
4034 * branch (e.g. 636).
4035 *
4036 * FIXME: This has been tested only in AP. It remains unknown if this
4037 * is required for multi-vif STA interfaces on 10.1 */
4038
4039 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
4040 return;
4041
4042 if (key->cipher == WLAN_CIPHER_SUITE_WEP40)
4043 return;
4044
4045 if (key->cipher == WLAN_CIPHER_SUITE_WEP104)
4046 return;
4047
4048 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4049 return;
4050
4051 if (cmd != SET_KEY)
4052 return;
4053
4054 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
4055 key->keyidx);
4056 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004057 ath10k_warn(ar, "failed to set vdev %i group key as default key: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02004058 arvif->vdev_id, ret);
Michal Kaziorcfb27d22013-12-02 09:06:36 +01004059}
4060
Kalle Valo5e3dd152013-06-12 20:52:10 +03004061static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
4062 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
4063 struct ieee80211_key_conf *key)
4064{
4065 struct ath10k *ar = hw->priv;
4066 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4067 struct ath10k_peer *peer;
4068 const u8 *peer_addr;
4069 bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
4070 key->cipher == WLAN_CIPHER_SUITE_WEP104;
4071 int ret = 0;
Michal Kazior370e5672015-02-18 14:02:26 +01004072 u32 flags = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004073
Bartosz Markowskid7131c02015-03-10 14:32:19 +01004074 /* this one needs to be done in software */
4075 if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
4076 return 1;
4077
Kalle Valo5e3dd152013-06-12 20:52:10 +03004078 if (key->keyidx > WMI_MAX_KEY_INDEX)
4079 return -ENOSPC;
4080
4081 mutex_lock(&ar->conf_mutex);
4082
4083 if (sta)
4084 peer_addr = sta->addr;
4085 else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
4086 peer_addr = vif->bss_conf.bssid;
4087 else
4088 peer_addr = vif->addr;
4089
4090 key->hw_key_idx = key->keyidx;
4091
4092 /* the peer should not disappear in mid-way (unless FW goes awry) since
4093 * we already hold conf_mutex. we just make sure its there now. */
4094 spin_lock_bh(&ar->data_lock);
4095 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
4096 spin_unlock_bh(&ar->data_lock);
4097
4098 if (!peer) {
4099 if (cmd == SET_KEY) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004100 ath10k_warn(ar, "failed to install key for non-existent peer %pM\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03004101 peer_addr);
4102 ret = -EOPNOTSUPP;
4103 goto exit;
4104 } else {
4105 /* if the peer doesn't exist there is no key to disable
4106 * anymore */
4107 goto exit;
4108 }
4109 }
4110
Michal Kazior7cc45732015-03-09 14:24:17 +01004111 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4112 flags |= WMI_KEY_PAIRWISE;
4113 else
4114 flags |= WMI_KEY_GROUP;
4115
Kalle Valo5e3dd152013-06-12 20:52:10 +03004116 if (is_wep) {
4117 if (cmd == SET_KEY)
4118 arvif->wep_keys[key->keyidx] = key;
4119 else
4120 arvif->wep_keys[key->keyidx] = NULL;
4121
4122 if (cmd == DISABLE_KEY)
4123 ath10k_clear_vdev_key(arvif, key);
Michal Kazior370e5672015-02-18 14:02:26 +01004124
Michal Kaziorad325cb2015-02-18 14:02:27 +01004125 /* When WEP keys are uploaded it's possible that there are
4126 * stations associated already (e.g. when merging) without any
4127 * keys. Static WEP needs an explicit per-peer key upload.
4128 */
4129 if (vif->type == NL80211_IFTYPE_ADHOC &&
4130 cmd == SET_KEY)
4131 ath10k_mac_vif_update_wep_key(arvif, key);
4132
Michal Kazior370e5672015-02-18 14:02:26 +01004133 /* 802.1x never sets the def_wep_key_idx so each set_key()
4134 * call changes default tx key.
4135 *
4136 * Static WEP sets def_wep_key_idx via .set_default_unicast_key
4137 * after first set_key().
4138 */
4139 if (cmd == SET_KEY && arvif->def_wep_key_idx == -1)
4140 flags |= WMI_KEY_TX_USAGE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004141
Michal Kazior7cc45732015-03-09 14:24:17 +01004142 /* mac80211 uploads static WEP keys as groupwise while fw/hw
4143 * requires pairwise keys for non-self peers, i.e. BSSID in STA
4144 * mode and associated stations in AP/IBSS.
4145 *
4146 * Static WEP keys for peer_addr=vif->addr and 802.1X WEP keys
4147 * work fine when mapped directly from mac80211.
4148 *
4149 * Note: When installing first static WEP groupwise key (which
4150 * should be pairwise) def_wep_key_idx isn't known yet (it's
4151 * equal to -1). Since .set_default_unicast_key is called only
4152 * for static WEP it's used to re-upload the key as pairwise.
4153 */
4154 if (arvif->def_wep_key_idx >= 0 &&
4155 memcmp(peer_addr, arvif->vif->addr, ETH_ALEN)) {
4156 flags &= ~WMI_KEY_GROUP;
4157 flags |= WMI_KEY_PAIRWISE;
4158 }
Michal Kazior370e5672015-02-18 14:02:26 +01004159 }
4160
4161 ret = ath10k_install_key(arvif, key, cmd, peer_addr, flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004162 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004163 ath10k_warn(ar, "failed to install key for vdev %i peer %pM: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02004164 arvif->vdev_id, peer_addr, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004165 goto exit;
4166 }
4167
Michal Kaziorcfb27d22013-12-02 09:06:36 +01004168 ath10k_set_key_h_def_keyidx(ar, arvif, cmd, key);
4169
Kalle Valo5e3dd152013-06-12 20:52:10 +03004170 spin_lock_bh(&ar->data_lock);
4171 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
4172 if (peer && cmd == SET_KEY)
4173 peer->keys[key->keyidx] = key;
4174 else if (peer && cmd == DISABLE_KEY)
4175 peer->keys[key->keyidx] = NULL;
4176 else if (peer == NULL)
4177 /* impossible unless FW goes crazy */
Michal Kazior7aa7a722014-08-25 12:09:38 +02004178 ath10k_warn(ar, "Peer %pM disappeared!\n", peer_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004179 spin_unlock_bh(&ar->data_lock);
4180
4181exit:
4182 mutex_unlock(&ar->conf_mutex);
4183 return ret;
4184}
4185
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +02004186static void ath10k_set_default_unicast_key(struct ieee80211_hw *hw,
4187 struct ieee80211_vif *vif,
4188 int keyidx)
4189{
4190 struct ath10k *ar = hw->priv;
4191 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4192 int ret;
4193
4194 mutex_lock(&arvif->ar->conf_mutex);
4195
4196 if (arvif->ar->state != ATH10K_STATE_ON)
4197 goto unlock;
4198
4199 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
4200 arvif->vdev_id, keyidx);
4201
4202 ret = ath10k_wmi_vdev_set_param(arvif->ar,
4203 arvif->vdev_id,
4204 arvif->ar->wmi.vdev_param->def_keyid,
4205 keyidx);
4206
4207 if (ret) {
4208 ath10k_warn(ar, "failed to update wep key index for vdev %d: %d\n",
4209 arvif->vdev_id,
4210 ret);
4211 goto unlock;
4212 }
4213
4214 arvif->def_wep_key_idx = keyidx;
Michal Kazior370e5672015-02-18 14:02:26 +01004215
4216 ret = ath10k_mac_vif_sta_fix_wep_key(arvif);
4217 if (ret) {
4218 ath10k_warn(ar, "failed to fix sta wep key on vdev %i: %d\n",
4219 arvif->vdev_id, ret);
4220 goto unlock;
4221 }
4222
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +02004223unlock:
4224 mutex_unlock(&arvif->ar->conf_mutex);
4225}
4226
Michal Kazior9797feb2014-02-14 14:49:48 +01004227static void ath10k_sta_rc_update_wk(struct work_struct *wk)
4228{
4229 struct ath10k *ar;
4230 struct ath10k_vif *arvif;
4231 struct ath10k_sta *arsta;
4232 struct ieee80211_sta *sta;
4233 u32 changed, bw, nss, smps;
4234 int err;
4235
4236 arsta = container_of(wk, struct ath10k_sta, update_wk);
4237 sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
4238 arvif = arsta->arvif;
4239 ar = arvif->ar;
4240
4241 spin_lock_bh(&ar->data_lock);
4242
4243 changed = arsta->changed;
4244 arsta->changed = 0;
4245
4246 bw = arsta->bw;
4247 nss = arsta->nss;
4248 smps = arsta->smps;
4249
4250 spin_unlock_bh(&ar->data_lock);
4251
4252 mutex_lock(&ar->conf_mutex);
4253
4254 if (changed & IEEE80211_RC_BW_CHANGED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004255 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM peer bw %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01004256 sta->addr, bw);
4257
4258 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
4259 WMI_PEER_CHAN_WIDTH, bw);
4260 if (err)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004261 ath10k_warn(ar, "failed to update STA %pM peer bw %d: %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01004262 sta->addr, bw, err);
4263 }
4264
4265 if (changed & IEEE80211_RC_NSS_CHANGED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004266 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM nss %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01004267 sta->addr, nss);
4268
4269 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
4270 WMI_PEER_NSS, nss);
4271 if (err)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004272 ath10k_warn(ar, "failed to update STA %pM nss %d: %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01004273 sta->addr, nss, err);
4274 }
4275
4276 if (changed & IEEE80211_RC_SMPS_CHANGED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004277 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM smps %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01004278 sta->addr, smps);
4279
4280 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
4281 WMI_PEER_SMPS_STATE, smps);
4282 if (err)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004283 ath10k_warn(ar, "failed to update STA %pM smps %d: %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01004284 sta->addr, smps, err);
4285 }
4286
Janusz Dziedzic55884c02014-12-17 12:30:02 +02004287 if (changed & IEEE80211_RC_SUPP_RATES_CHANGED ||
4288 changed & IEEE80211_RC_NSS_CHANGED) {
4289 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM supp rates/nss\n",
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02004290 sta->addr);
4291
Michal Kazior590922a2014-10-21 10:10:29 +03004292 err = ath10k_station_assoc(ar, arvif->vif, sta, true);
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02004293 if (err)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004294 ath10k_warn(ar, "failed to reassociate station: %pM\n",
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02004295 sta->addr);
4296 }
4297
Michal Kazior9797feb2014-02-14 14:49:48 +01004298 mutex_unlock(&ar->conf_mutex);
4299}
4300
Michal Kaziorcfd10612014-11-25 15:16:05 +01004301static int ath10k_mac_inc_num_stations(struct ath10k_vif *arvif)
4302{
4303 struct ath10k *ar = arvif->ar;
4304
4305 lockdep_assert_held(&ar->conf_mutex);
4306
4307 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
4308 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
4309 return 0;
4310
4311 if (ar->num_stations >= ar->max_num_stations)
4312 return -ENOBUFS;
4313
4314 ar->num_stations++;
4315
4316 return 0;
4317}
4318
4319static void ath10k_mac_dec_num_stations(struct ath10k_vif *arvif)
4320{
4321 struct ath10k *ar = arvif->ar;
4322
4323 lockdep_assert_held(&ar->conf_mutex);
4324
4325 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
4326 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
4327 return;
4328
4329 ar->num_stations--;
4330}
4331
Kalle Valo5e3dd152013-06-12 20:52:10 +03004332static int ath10k_sta_state(struct ieee80211_hw *hw,
4333 struct ieee80211_vif *vif,
4334 struct ieee80211_sta *sta,
4335 enum ieee80211_sta_state old_state,
4336 enum ieee80211_sta_state new_state)
4337{
4338 struct ath10k *ar = hw->priv;
4339 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Michal Kazior9797feb2014-02-14 14:49:48 +01004340 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004341 int ret = 0;
4342
Michal Kazior76f90022014-02-25 09:29:57 +02004343 if (old_state == IEEE80211_STA_NOTEXIST &&
4344 new_state == IEEE80211_STA_NONE) {
4345 memset(arsta, 0, sizeof(*arsta));
4346 arsta->arvif = arvif;
4347 INIT_WORK(&arsta->update_wk, ath10k_sta_rc_update_wk);
4348 }
4349
Michal Kazior9797feb2014-02-14 14:49:48 +01004350 /* cancel must be done outside the mutex to avoid deadlock */
4351 if ((old_state == IEEE80211_STA_NONE &&
4352 new_state == IEEE80211_STA_NOTEXIST))
4353 cancel_work_sync(&arsta->update_wk);
4354
Kalle Valo5e3dd152013-06-12 20:52:10 +03004355 mutex_lock(&ar->conf_mutex);
4356
4357 if (old_state == IEEE80211_STA_NOTEXIST &&
Michal Kazior077efc82014-10-21 10:10:29 +03004358 new_state == IEEE80211_STA_NONE) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03004359 /*
4360 * New station addition.
4361 */
Michal Kaziorcfd10612014-11-25 15:16:05 +01004362 ath10k_dbg(ar, ATH10K_DBG_MAC,
4363 "mac vdev %d peer create %pM (new sta) sta %d / %d peer %d / %d\n",
4364 arvif->vdev_id, sta->addr,
4365 ar->num_stations + 1, ar->max_num_stations,
4366 ar->num_peers + 1, ar->max_num_peers);
Bartosz Markowski0e759f32014-01-02 14:38:33 +01004367
Michal Kaziorcfd10612014-11-25 15:16:05 +01004368 ret = ath10k_mac_inc_num_stations(arvif);
4369 if (ret) {
4370 ath10k_warn(ar, "refusing to associate station: too many connected already (%d)\n",
4371 ar->max_num_stations);
Bartosz Markowski0e759f32014-01-02 14:38:33 +01004372 goto exit;
4373 }
4374
Kalle Valo5e3dd152013-06-12 20:52:10 +03004375 ret = ath10k_peer_create(ar, arvif->vdev_id, sta->addr);
Michal Kaziora52c0282014-11-25 15:16:03 +01004376 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004377 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 -08004378 sta->addr, arvif->vdev_id, ret);
Michal Kaziorcfd10612014-11-25 15:16:05 +01004379 ath10k_mac_dec_num_stations(arvif);
Michal Kaziora52c0282014-11-25 15:16:03 +01004380 goto exit;
4381 }
Michal Kazior077efc82014-10-21 10:10:29 +03004382
4383 if (vif->type == NL80211_IFTYPE_STATION) {
4384 WARN_ON(arvif->is_started);
4385
4386 ret = ath10k_vdev_start(arvif);
4387 if (ret) {
4388 ath10k_warn(ar, "failed to start vdev %i: %d\n",
4389 arvif->vdev_id, ret);
4390 WARN_ON(ath10k_peer_delete(ar, arvif->vdev_id,
4391 sta->addr));
Michal Kaziorcfd10612014-11-25 15:16:05 +01004392 ath10k_mac_dec_num_stations(arvif);
Michal Kazior077efc82014-10-21 10:10:29 +03004393 goto exit;
4394 }
4395
4396 arvif->is_started = true;
4397 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03004398 } else if ((old_state == IEEE80211_STA_NONE &&
4399 new_state == IEEE80211_STA_NOTEXIST)) {
4400 /*
4401 * Existing station deletion.
4402 */
Michal Kazior7aa7a722014-08-25 12:09:38 +02004403 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03004404 "mac vdev %d peer delete %pM (sta gone)\n",
4405 arvif->vdev_id, sta->addr);
Michal Kazior077efc82014-10-21 10:10:29 +03004406
4407 if (vif->type == NL80211_IFTYPE_STATION) {
4408 WARN_ON(!arvif->is_started);
4409
4410 ret = ath10k_vdev_stop(arvif);
4411 if (ret)
4412 ath10k_warn(ar, "failed to stop vdev %i: %d\n",
4413 arvif->vdev_id, ret);
4414
4415 arvif->is_started = false;
4416 }
4417
Kalle Valo5e3dd152013-06-12 20:52:10 +03004418 ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
4419 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004420 ath10k_warn(ar, "failed to delete peer %pM for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02004421 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004422
Michal Kaziorcfd10612014-11-25 15:16:05 +01004423 ath10k_mac_dec_num_stations(arvif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004424 } else if (old_state == IEEE80211_STA_AUTH &&
4425 new_state == IEEE80211_STA_ASSOC &&
4426 (vif->type == NL80211_IFTYPE_AP ||
4427 vif->type == NL80211_IFTYPE_ADHOC)) {
4428 /*
4429 * New association.
4430 */
Michal Kazior7aa7a722014-08-25 12:09:38 +02004431 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM associated\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03004432 sta->addr);
4433
Michal Kazior590922a2014-10-21 10:10:29 +03004434 ret = ath10k_station_assoc(ar, vif, sta, false);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004435 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004436 ath10k_warn(ar, "failed to associate station %pM for vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02004437 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004438 } else if (old_state == IEEE80211_STA_ASSOC &&
4439 new_state == IEEE80211_STA_AUTH &&
4440 (vif->type == NL80211_IFTYPE_AP ||
4441 vif->type == NL80211_IFTYPE_ADHOC)) {
4442 /*
4443 * Disassociation.
4444 */
Michal Kazior7aa7a722014-08-25 12:09:38 +02004445 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM disassociated\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03004446 sta->addr);
4447
Michal Kazior590922a2014-10-21 10:10:29 +03004448 ret = ath10k_station_disassoc(ar, vif, sta);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004449 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004450 ath10k_warn(ar, "failed to disassociate station: %pM vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02004451 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004452 }
Bartosz Markowski0e759f32014-01-02 14:38:33 +01004453exit:
Kalle Valo5e3dd152013-06-12 20:52:10 +03004454 mutex_unlock(&ar->conf_mutex);
4455 return ret;
4456}
4457
4458static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
Kalle Valo5b07e072014-09-14 12:50:06 +03004459 u16 ac, bool enable)
Kalle Valo5e3dd152013-06-12 20:52:10 +03004460{
4461 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Michal Kaziorb0e56152015-01-24 12:14:52 +02004462 struct wmi_sta_uapsd_auto_trig_arg arg = {};
4463 u32 prio = 0, acc = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004464 u32 value = 0;
4465 int ret = 0;
4466
Michal Kazior548db542013-07-05 16:15:15 +03004467 lockdep_assert_held(&ar->conf_mutex);
4468
Kalle Valo5e3dd152013-06-12 20:52:10 +03004469 if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
4470 return 0;
4471
4472 switch (ac) {
4473 case IEEE80211_AC_VO:
4474 value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
4475 WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
Michal Kaziorb0e56152015-01-24 12:14:52 +02004476 prio = 7;
4477 acc = 3;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004478 break;
4479 case IEEE80211_AC_VI:
4480 value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
4481 WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
Michal Kaziorb0e56152015-01-24 12:14:52 +02004482 prio = 5;
4483 acc = 2;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004484 break;
4485 case IEEE80211_AC_BE:
4486 value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
4487 WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
Michal Kaziorb0e56152015-01-24 12:14:52 +02004488 prio = 2;
4489 acc = 1;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004490 break;
4491 case IEEE80211_AC_BK:
4492 value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
4493 WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
Michal Kaziorb0e56152015-01-24 12:14:52 +02004494 prio = 0;
4495 acc = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004496 break;
4497 }
4498
4499 if (enable)
4500 arvif->u.sta.uapsd |= value;
4501 else
4502 arvif->u.sta.uapsd &= ~value;
4503
4504 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4505 WMI_STA_PS_PARAM_UAPSD,
4506 arvif->u.sta.uapsd);
4507 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004508 ath10k_warn(ar, "failed to set uapsd params: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004509 goto exit;
4510 }
4511
4512 if (arvif->u.sta.uapsd)
4513 value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
4514 else
4515 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
4516
4517 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4518 WMI_STA_PS_PARAM_RX_WAKE_POLICY,
4519 value);
4520 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004521 ath10k_warn(ar, "failed to set rx wake param: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004522
Michal Kazior9f9b5742014-12-12 12:41:36 +01004523 ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
4524 if (ret) {
4525 ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
4526 arvif->vdev_id, ret);
4527 return ret;
4528 }
4529
4530 ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
4531 if (ret) {
4532 ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
4533 arvif->vdev_id, ret);
4534 return ret;
4535 }
4536
Michal Kaziorb0e56152015-01-24 12:14:52 +02004537 if (test_bit(WMI_SERVICE_STA_UAPSD_BASIC_AUTO_TRIG, ar->wmi.svc_map) ||
4538 test_bit(WMI_SERVICE_STA_UAPSD_VAR_AUTO_TRIG, ar->wmi.svc_map)) {
4539 /* Only userspace can make an educated decision when to send
4540 * trigger frame. The following effectively disables u-UAPSD
4541 * autotrigger in firmware (which is enabled by default
4542 * provided the autotrigger service is available).
4543 */
4544
4545 arg.wmm_ac = acc;
4546 arg.user_priority = prio;
4547 arg.service_interval = 0;
4548 arg.suspend_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
4549 arg.delay_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
4550
4551 ret = ath10k_wmi_vdev_sta_uapsd(ar, arvif->vdev_id,
4552 arvif->bssid, &arg, 1);
4553 if (ret) {
4554 ath10k_warn(ar, "failed to set uapsd auto trigger %d\n",
4555 ret);
4556 return ret;
4557 }
4558 }
4559
Kalle Valo5e3dd152013-06-12 20:52:10 +03004560exit:
4561 return ret;
4562}
4563
4564static int ath10k_conf_tx(struct ieee80211_hw *hw,
4565 struct ieee80211_vif *vif, u16 ac,
4566 const struct ieee80211_tx_queue_params *params)
4567{
4568 struct ath10k *ar = hw->priv;
Michal Kazior5e752e42015-01-19 09:53:41 +01004569 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004570 struct wmi_wmm_params_arg *p = NULL;
4571 int ret;
4572
4573 mutex_lock(&ar->conf_mutex);
4574
4575 switch (ac) {
4576 case IEEE80211_AC_VO:
Michal Kazior5e752e42015-01-19 09:53:41 +01004577 p = &arvif->wmm_params.ac_vo;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004578 break;
4579 case IEEE80211_AC_VI:
Michal Kazior5e752e42015-01-19 09:53:41 +01004580 p = &arvif->wmm_params.ac_vi;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004581 break;
4582 case IEEE80211_AC_BE:
Michal Kazior5e752e42015-01-19 09:53:41 +01004583 p = &arvif->wmm_params.ac_be;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004584 break;
4585 case IEEE80211_AC_BK:
Michal Kazior5e752e42015-01-19 09:53:41 +01004586 p = &arvif->wmm_params.ac_bk;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004587 break;
4588 }
4589
4590 if (WARN_ON(!p)) {
4591 ret = -EINVAL;
4592 goto exit;
4593 }
4594
4595 p->cwmin = params->cw_min;
4596 p->cwmax = params->cw_max;
4597 p->aifs = params->aifs;
4598
4599 /*
4600 * The channel time duration programmed in the HW is in absolute
4601 * microseconds, while mac80211 gives the txop in units of
4602 * 32 microseconds.
4603 */
4604 p->txop = params->txop * 32;
4605
Michal Kazior7fc979a2015-01-28 09:57:28 +02004606 if (ar->wmi.ops->gen_vdev_wmm_conf) {
4607 ret = ath10k_wmi_vdev_wmm_conf(ar, arvif->vdev_id,
4608 &arvif->wmm_params);
4609 if (ret) {
4610 ath10k_warn(ar, "failed to set vdev wmm params on vdev %i: %d\n",
4611 arvif->vdev_id, ret);
4612 goto exit;
4613 }
4614 } else {
4615 /* This won't work well with multi-interface cases but it's
4616 * better than nothing.
4617 */
4618 ret = ath10k_wmi_pdev_set_wmm_params(ar, &arvif->wmm_params);
4619 if (ret) {
4620 ath10k_warn(ar, "failed to set wmm params: %d\n", ret);
4621 goto exit;
4622 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03004623 }
4624
4625 ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
4626 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004627 ath10k_warn(ar, "failed to set sta uapsd: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004628
4629exit:
4630 mutex_unlock(&ar->conf_mutex);
4631 return ret;
4632}
4633
4634#define ATH10K_ROC_TIMEOUT_HZ (2*HZ)
4635
4636static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
4637 struct ieee80211_vif *vif,
4638 struct ieee80211_channel *chan,
4639 int duration,
4640 enum ieee80211_roc_type type)
4641{
4642 struct ath10k *ar = hw->priv;
4643 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4644 struct wmi_start_scan_arg arg;
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004645 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004646
4647 mutex_lock(&ar->conf_mutex);
4648
4649 spin_lock_bh(&ar->data_lock);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004650 switch (ar->scan.state) {
4651 case ATH10K_SCAN_IDLE:
4652 reinit_completion(&ar->scan.started);
4653 reinit_completion(&ar->scan.completed);
4654 reinit_completion(&ar->scan.on_channel);
4655 ar->scan.state = ATH10K_SCAN_STARTING;
4656 ar->scan.is_roc = true;
4657 ar->scan.vdev_id = arvif->vdev_id;
4658 ar->scan.roc_freq = chan->center_freq;
4659 ret = 0;
4660 break;
4661 case ATH10K_SCAN_STARTING:
4662 case ATH10K_SCAN_RUNNING:
4663 case ATH10K_SCAN_ABORTING:
Kalle Valo5e3dd152013-06-12 20:52:10 +03004664 ret = -EBUSY;
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004665 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004666 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03004667 spin_unlock_bh(&ar->data_lock);
4668
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004669 if (ret)
4670 goto exit;
4671
Michal Kaziordcca0bd2014-11-24 14:58:32 +01004672 duration = max(duration, WMI_SCAN_CHAN_MIN_TIME_MSEC);
4673
Kalle Valo5e3dd152013-06-12 20:52:10 +03004674 memset(&arg, 0, sizeof(arg));
4675 ath10k_wmi_start_scan_init(ar, &arg);
4676 arg.vdev_id = arvif->vdev_id;
4677 arg.scan_id = ATH10K_SCAN_ID;
4678 arg.n_channels = 1;
4679 arg.channels[0] = chan->center_freq;
4680 arg.dwell_time_active = duration;
4681 arg.dwell_time_passive = duration;
4682 arg.max_scan_time = 2 * duration;
4683 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
4684 arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
4685
4686 ret = ath10k_start_scan(ar, &arg);
4687 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004688 ath10k_warn(ar, "failed to start roc scan: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004689 spin_lock_bh(&ar->data_lock);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004690 ar->scan.state = ATH10K_SCAN_IDLE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004691 spin_unlock_bh(&ar->data_lock);
4692 goto exit;
4693 }
4694
4695 ret = wait_for_completion_timeout(&ar->scan.on_channel, 3*HZ);
4696 if (ret == 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004697 ath10k_warn(ar, "failed to switch to channel for roc scan\n");
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004698
4699 ret = ath10k_scan_stop(ar);
4700 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004701 ath10k_warn(ar, "failed to stop scan: %d\n", ret);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004702
Kalle Valo5e3dd152013-06-12 20:52:10 +03004703 ret = -ETIMEDOUT;
4704 goto exit;
4705 }
4706
4707 ret = 0;
4708exit:
4709 mutex_unlock(&ar->conf_mutex);
4710 return ret;
4711}
4712
4713static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw)
4714{
4715 struct ath10k *ar = hw->priv;
4716
4717 mutex_lock(&ar->conf_mutex);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004718 ath10k_scan_abort(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004719 mutex_unlock(&ar->conf_mutex);
4720
Michal Kazior4eb2e162014-10-28 10:23:09 +01004721 cancel_delayed_work_sync(&ar->scan.timeout);
4722
Kalle Valo5e3dd152013-06-12 20:52:10 +03004723 return 0;
4724}
4725
4726/*
4727 * Both RTS and Fragmentation threshold are interface-specific
4728 * in ath10k, but device-specific in mac80211.
4729 */
Kalle Valo5e3dd152013-06-12 20:52:10 +03004730
4731static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
4732{
Kalle Valo5e3dd152013-06-12 20:52:10 +03004733 struct ath10k *ar = hw->priv;
Michal Kaziorad088bf2013-10-16 15:44:46 +03004734 struct ath10k_vif *arvif;
4735 int ret = 0;
Michal Kazior548db542013-07-05 16:15:15 +03004736
Michal Kaziorad088bf2013-10-16 15:44:46 +03004737 mutex_lock(&ar->conf_mutex);
4738 list_for_each_entry(arvif, &ar->arvifs, list) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004739 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
Michal Kaziorad088bf2013-10-16 15:44:46 +03004740 arvif->vdev_id, value);
Kalle Valo60c3daa2013-09-08 17:56:07 +03004741
Michal Kaziorad088bf2013-10-16 15:44:46 +03004742 ret = ath10k_mac_set_rts(arvif, value);
4743 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004744 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
Michal Kaziorad088bf2013-10-16 15:44:46 +03004745 arvif->vdev_id, ret);
4746 break;
4747 }
4748 }
4749 mutex_unlock(&ar->conf_mutex);
4750
4751 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004752}
4753
Emmanuel Grumbach77be2c52014-03-27 11:30:29 +02004754static void ath10k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4755 u32 queues, bool drop)
Kalle Valo5e3dd152013-06-12 20:52:10 +03004756{
4757 struct ath10k *ar = hw->priv;
Michal Kazioraffd3212013-07-16 09:54:35 +02004758 bool skip;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004759 int ret;
4760
4761 /* mac80211 doesn't care if we really xmit queued frames or not
4762 * we'll collect those frames either way if we stop/delete vdevs */
4763 if (drop)
4764 return;
4765
Michal Kazior548db542013-07-05 16:15:15 +03004766 mutex_lock(&ar->conf_mutex);
4767
Michal Kazioraffd3212013-07-16 09:54:35 +02004768 if (ar->state == ATH10K_STATE_WEDGED)
4769 goto skip;
4770
Michal Kazioredb82362013-07-05 16:15:14 +03004771 ret = wait_event_timeout(ar->htt.empty_tx_wq, ({
Kalle Valo5e3dd152013-06-12 20:52:10 +03004772 bool empty;
Michal Kazioraffd3212013-07-16 09:54:35 +02004773
Michal Kazioredb82362013-07-05 16:15:14 +03004774 spin_lock_bh(&ar->htt.tx_lock);
Michal Kazior0945baf2013-09-18 14:43:18 +02004775 empty = (ar->htt.num_pending_tx == 0);
Michal Kazioredb82362013-07-05 16:15:14 +03004776 spin_unlock_bh(&ar->htt.tx_lock);
Michal Kazioraffd3212013-07-16 09:54:35 +02004777
Michal Kazior7962b0d2014-10-28 10:34:38 +01004778 skip = (ar->state == ATH10K_STATE_WEDGED) ||
4779 test_bit(ATH10K_FLAG_CRASH_FLUSH,
4780 &ar->dev_flags);
Michal Kazioraffd3212013-07-16 09:54:35 +02004781
4782 (empty || skip);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004783 }), ATH10K_FLUSH_TIMEOUT_HZ);
Michal Kazioraffd3212013-07-16 09:54:35 +02004784
4785 if (ret <= 0 || skip)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004786 ath10k_warn(ar, "failed to flush transmit queue (skip %i ar-state %i): %i\n",
Ben Greear9ba4c782014-02-25 09:29:57 +02004787 skip, ar->state, ret);
Michal Kazior548db542013-07-05 16:15:15 +03004788
Michal Kazioraffd3212013-07-16 09:54:35 +02004789skip:
Michal Kazior548db542013-07-05 16:15:15 +03004790 mutex_unlock(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004791}
4792
4793/* TODO: Implement this function properly
4794 * For now it is needed to reply to Probe Requests in IBSS mode.
4795 * Propably we need this information from FW.
4796 */
4797static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
4798{
4799 return 1;
4800}
4801
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004802#ifdef CONFIG_PM
4803static int ath10k_suspend(struct ieee80211_hw *hw,
4804 struct cfg80211_wowlan *wowlan)
4805{
4806 struct ath10k *ar = hw->priv;
4807 int ret;
4808
Marek Puzyniak9042e172014-02-10 17:14:23 +01004809 mutex_lock(&ar->conf_mutex);
4810
Marek Puzyniak00f54822014-02-10 17:14:24 +01004811 ret = ath10k_wait_for_suspend(ar, WMI_PDEV_SUSPEND);
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004812 if (ret) {
Marek Puzyniak00f54822014-02-10 17:14:24 +01004813 if (ret == -ETIMEDOUT)
4814 goto resume;
Marek Puzyniak9042e172014-02-10 17:14:23 +01004815 ret = 1;
4816 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004817 }
4818
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004819 ret = ath10k_hif_suspend(ar);
4820 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004821 ath10k_warn(ar, "failed to suspend hif: %d\n", ret);
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004822 goto resume;
4823 }
4824
Marek Puzyniak9042e172014-02-10 17:14:23 +01004825 ret = 0;
4826 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004827resume:
4828 ret = ath10k_wmi_pdev_resume_target(ar);
4829 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004830 ath10k_warn(ar, "failed to resume target: %d\n", ret);
Marek Puzyniak9042e172014-02-10 17:14:23 +01004831
4832 ret = 1;
4833exit:
4834 mutex_unlock(&ar->conf_mutex);
4835 return ret;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004836}
4837
4838static int ath10k_resume(struct ieee80211_hw *hw)
4839{
4840 struct ath10k *ar = hw->priv;
4841 int ret;
4842
Marek Puzyniak9042e172014-02-10 17:14:23 +01004843 mutex_lock(&ar->conf_mutex);
4844
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004845 ret = ath10k_hif_resume(ar);
4846 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004847 ath10k_warn(ar, "failed to resume hif: %d\n", ret);
Marek Puzyniak9042e172014-02-10 17:14:23 +01004848 ret = 1;
4849 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004850 }
4851
4852 ret = ath10k_wmi_pdev_resume_target(ar);
4853 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004854 ath10k_warn(ar, "failed to resume target: %d\n", ret);
Marek Puzyniak9042e172014-02-10 17:14:23 +01004855 ret = 1;
4856 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004857 }
4858
Marek Puzyniak9042e172014-02-10 17:14:23 +01004859 ret = 0;
4860exit:
4861 mutex_unlock(&ar->conf_mutex);
4862 return ret;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004863}
4864#endif
4865
Eliad Pellercf2c92d2014-11-04 11:43:54 +02004866static void ath10k_reconfig_complete(struct ieee80211_hw *hw,
4867 enum ieee80211_reconfig_type reconfig_type)
Michal Kazioraffd3212013-07-16 09:54:35 +02004868{
4869 struct ath10k *ar = hw->priv;
4870
Eliad Pellercf2c92d2014-11-04 11:43:54 +02004871 if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
4872 return;
4873
Michal Kazioraffd3212013-07-16 09:54:35 +02004874 mutex_lock(&ar->conf_mutex);
4875
4876 /* If device failed to restart it will be in a different state, e.g.
4877 * ATH10K_STATE_WEDGED */
4878 if (ar->state == ATH10K_STATE_RESTARTED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004879 ath10k_info(ar, "device successfully recovered\n");
Michal Kazioraffd3212013-07-16 09:54:35 +02004880 ar->state = ATH10K_STATE_ON;
Michal Kazior7962b0d2014-10-28 10:34:38 +01004881 ieee80211_wake_queues(ar->hw);
Michal Kazioraffd3212013-07-16 09:54:35 +02004882 }
4883
4884 mutex_unlock(&ar->conf_mutex);
4885}
4886
Michal Kazior2e1dea42013-07-31 10:32:40 +02004887static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
4888 struct survey_info *survey)
4889{
4890 struct ath10k *ar = hw->priv;
4891 struct ieee80211_supported_band *sband;
4892 struct survey_info *ar_survey = &ar->survey[idx];
4893 int ret = 0;
4894
4895 mutex_lock(&ar->conf_mutex);
4896
4897 sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
4898 if (sband && idx >= sband->n_channels) {
4899 idx -= sband->n_channels;
4900 sband = NULL;
4901 }
4902
4903 if (!sband)
4904 sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
4905
4906 if (!sband || idx >= sband->n_channels) {
4907 ret = -ENOENT;
4908 goto exit;
4909 }
4910
4911 spin_lock_bh(&ar->data_lock);
4912 memcpy(survey, ar_survey, sizeof(*survey));
4913 spin_unlock_bh(&ar->data_lock);
4914
4915 survey->channel = &sband->channels[idx];
4916
Felix Fietkaufa1d4df2014-10-23 17:04:28 +03004917 if (ar->rx_channel == survey->channel)
4918 survey->filled |= SURVEY_INFO_IN_USE;
4919
Michal Kazior2e1dea42013-07-31 10:32:40 +02004920exit:
4921 mutex_unlock(&ar->conf_mutex);
4922 return ret;
4923}
4924
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004925/* Helper table for legacy fixed_rate/bitrate_mask */
4926static const u8 cck_ofdm_rate[] = {
4927 /* CCK */
4928 3, /* 1Mbps */
4929 2, /* 2Mbps */
4930 1, /* 5.5Mbps */
4931 0, /* 11Mbps */
4932 /* OFDM */
4933 3, /* 6Mbps */
4934 7, /* 9Mbps */
4935 2, /* 12Mbps */
4936 6, /* 18Mbps */
4937 1, /* 24Mbps */
4938 5, /* 36Mbps */
4939 0, /* 48Mbps */
4940 4, /* 54Mbps */
4941};
4942
4943/* Check if only one bit set */
4944static int ath10k_check_single_mask(u32 mask)
4945{
4946 int bit;
4947
4948 bit = ffs(mask);
4949 if (!bit)
4950 return 0;
4951
4952 mask &= ~BIT(bit - 1);
4953 if (mask)
4954 return 2;
4955
4956 return 1;
4957}
4958
4959static bool
4960ath10k_default_bitrate_mask(struct ath10k *ar,
4961 enum ieee80211_band band,
4962 const struct cfg80211_bitrate_mask *mask)
4963{
4964 u32 legacy = 0x00ff;
4965 u8 ht = 0xff, i;
4966 u16 vht = 0x3ff;
Ben Greearb116ea12014-11-24 16:22:10 +02004967 u16 nrf = ar->num_rf_chains;
4968
4969 if (ar->cfg_tx_chainmask)
4970 nrf = get_nss_from_chainmask(ar->cfg_tx_chainmask);
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004971
4972 switch (band) {
4973 case IEEE80211_BAND_2GHZ:
4974 legacy = 0x00fff;
4975 vht = 0;
4976 break;
4977 case IEEE80211_BAND_5GHZ:
4978 break;
4979 default:
4980 return false;
4981 }
4982
4983 if (mask->control[band].legacy != legacy)
4984 return false;
4985
Ben Greearb116ea12014-11-24 16:22:10 +02004986 for (i = 0; i < nrf; i++)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004987 if (mask->control[band].ht_mcs[i] != ht)
4988 return false;
4989
Ben Greearb116ea12014-11-24 16:22:10 +02004990 for (i = 0; i < nrf; i++)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004991 if (mask->control[band].vht_mcs[i] != vht)
4992 return false;
4993
4994 return true;
4995}
4996
4997static bool
4998ath10k_bitrate_mask_nss(const struct cfg80211_bitrate_mask *mask,
4999 enum ieee80211_band band,
5000 u8 *fixed_nss)
5001{
5002 int ht_nss = 0, vht_nss = 0, i;
5003
5004 /* check legacy */
5005 if (ath10k_check_single_mask(mask->control[band].legacy))
5006 return false;
5007
5008 /* check HT */
5009 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
5010 if (mask->control[band].ht_mcs[i] == 0xff)
5011 continue;
5012 else if (mask->control[band].ht_mcs[i] == 0x00)
5013 break;
Kalle Valod8bb26b2014-09-14 12:50:33 +03005014
5015 return false;
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005016 }
5017
5018 ht_nss = i;
5019
5020 /* check VHT */
5021 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
5022 if (mask->control[band].vht_mcs[i] == 0x03ff)
5023 continue;
5024 else if (mask->control[band].vht_mcs[i] == 0x0000)
5025 break;
Kalle Valod8bb26b2014-09-14 12:50:33 +03005026
5027 return false;
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005028 }
5029
5030 vht_nss = i;
5031
5032 if (ht_nss > 0 && vht_nss > 0)
5033 return false;
5034
5035 if (ht_nss)
5036 *fixed_nss = ht_nss;
5037 else if (vht_nss)
5038 *fixed_nss = vht_nss;
5039 else
5040 return false;
5041
5042 return true;
5043}
5044
5045static bool
5046ath10k_bitrate_mask_correct(const struct cfg80211_bitrate_mask *mask,
5047 enum ieee80211_band band,
5048 enum wmi_rate_preamble *preamble)
5049{
5050 int legacy = 0, ht = 0, vht = 0, i;
5051
5052 *preamble = WMI_RATE_PREAMBLE_OFDM;
5053
5054 /* check legacy */
5055 legacy = ath10k_check_single_mask(mask->control[band].legacy);
5056 if (legacy > 1)
5057 return false;
5058
5059 /* check HT */
5060 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5061 ht += ath10k_check_single_mask(mask->control[band].ht_mcs[i]);
5062 if (ht > 1)
5063 return false;
5064
5065 /* check VHT */
5066 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
5067 vht += ath10k_check_single_mask(mask->control[band].vht_mcs[i]);
5068 if (vht > 1)
5069 return false;
5070
5071 /* Currently we support only one fixed_rate */
5072 if ((legacy + ht + vht) != 1)
5073 return false;
5074
5075 if (ht)
5076 *preamble = WMI_RATE_PREAMBLE_HT;
5077 else if (vht)
5078 *preamble = WMI_RATE_PREAMBLE_VHT;
5079
5080 return true;
5081}
5082
5083static bool
Michal Kazior7aa7a722014-08-25 12:09:38 +02005084ath10k_bitrate_mask_rate(struct ath10k *ar,
5085 const struct cfg80211_bitrate_mask *mask,
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005086 enum ieee80211_band band,
5087 u8 *fixed_rate,
5088 u8 *fixed_nss)
5089{
5090 u8 rate = 0, pream = 0, nss = 0, i;
5091 enum wmi_rate_preamble preamble;
5092
5093 /* Check if single rate correct */
5094 if (!ath10k_bitrate_mask_correct(mask, band, &preamble))
5095 return false;
5096
5097 pream = preamble;
5098
5099 switch (preamble) {
5100 case WMI_RATE_PREAMBLE_CCK:
5101 case WMI_RATE_PREAMBLE_OFDM:
5102 i = ffs(mask->control[band].legacy) - 1;
5103
5104 if (band == IEEE80211_BAND_2GHZ && i < 4)
5105 pream = WMI_RATE_PREAMBLE_CCK;
5106
5107 if (band == IEEE80211_BAND_5GHZ)
5108 i += 4;
5109
5110 if (i >= ARRAY_SIZE(cck_ofdm_rate))
5111 return false;
5112
5113 rate = cck_ofdm_rate[i];
5114 break;
5115 case WMI_RATE_PREAMBLE_HT:
5116 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5117 if (mask->control[band].ht_mcs[i])
5118 break;
5119
5120 if (i == IEEE80211_HT_MCS_MASK_LEN)
5121 return false;
5122
5123 rate = ffs(mask->control[band].ht_mcs[i]) - 1;
5124 nss = i;
5125 break;
5126 case WMI_RATE_PREAMBLE_VHT:
5127 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
5128 if (mask->control[band].vht_mcs[i])
5129 break;
5130
5131 if (i == NL80211_VHT_NSS_MAX)
5132 return false;
5133
5134 rate = ffs(mask->control[band].vht_mcs[i]) - 1;
5135 nss = i;
5136 break;
5137 }
5138
5139 *fixed_nss = nss + 1;
5140 nss <<= 4;
5141 pream <<= 6;
5142
Michal Kazior7aa7a722014-08-25 12:09:38 +02005143 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 +01005144 pream, nss, rate);
5145
5146 *fixed_rate = pream | nss | rate;
5147
5148 return true;
5149}
5150
Michal Kazior7aa7a722014-08-25 12:09:38 +02005151static bool ath10k_get_fixed_rate_nss(struct ath10k *ar,
5152 const struct cfg80211_bitrate_mask *mask,
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005153 enum ieee80211_band band,
5154 u8 *fixed_rate,
5155 u8 *fixed_nss)
5156{
5157 /* First check full NSS mask, if we can simply limit NSS */
5158 if (ath10k_bitrate_mask_nss(mask, band, fixed_nss))
5159 return true;
5160
5161 /* Next Check single rate is set */
Michal Kazior7aa7a722014-08-25 12:09:38 +02005162 return ath10k_bitrate_mask_rate(ar, mask, band, fixed_rate, fixed_nss);
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005163}
5164
5165static int ath10k_set_fixed_rate_param(struct ath10k_vif *arvif,
5166 u8 fixed_rate,
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01005167 u8 fixed_nss,
5168 u8 force_sgi)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005169{
5170 struct ath10k *ar = arvif->ar;
5171 u32 vdev_param;
5172 int ret = 0;
5173
5174 mutex_lock(&ar->conf_mutex);
5175
5176 if (arvif->fixed_rate == fixed_rate &&
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01005177 arvif->fixed_nss == fixed_nss &&
5178 arvif->force_sgi == force_sgi)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005179 goto exit;
5180
5181 if (fixed_rate == WMI_FIXED_RATE_NONE)
Michal Kazior7aa7a722014-08-25 12:09:38 +02005182 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac disable fixed bitrate mask\n");
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005183
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01005184 if (force_sgi)
Michal Kazior7aa7a722014-08-25 12:09:38 +02005185 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac force sgi\n");
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01005186
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005187 vdev_param = ar->wmi.vdev_param->fixed_rate;
5188 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
5189 vdev_param, fixed_rate);
5190 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02005191 ath10k_warn(ar, "failed to set fixed rate param 0x%02x: %d\n",
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005192 fixed_rate, ret);
5193 ret = -EINVAL;
5194 goto exit;
5195 }
5196
5197 arvif->fixed_rate = fixed_rate;
5198
5199 vdev_param = ar->wmi.vdev_param->nss;
5200 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
5201 vdev_param, fixed_nss);
5202
5203 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02005204 ath10k_warn(ar, "failed to set fixed nss param %d: %d\n",
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005205 fixed_nss, ret);
5206 ret = -EINVAL;
5207 goto exit;
5208 }
5209
5210 arvif->fixed_nss = fixed_nss;
5211
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01005212 vdev_param = ar->wmi.vdev_param->sgi;
5213 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5214 force_sgi);
5215
5216 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02005217 ath10k_warn(ar, "failed to set sgi param %d: %d\n",
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01005218 force_sgi, ret);
5219 ret = -EINVAL;
5220 goto exit;
5221 }
5222
5223 arvif->force_sgi = force_sgi;
5224
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005225exit:
5226 mutex_unlock(&ar->conf_mutex);
5227 return ret;
5228}
5229
5230static int ath10k_set_bitrate_mask(struct ieee80211_hw *hw,
5231 struct ieee80211_vif *vif,
5232 const struct cfg80211_bitrate_mask *mask)
5233{
5234 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5235 struct ath10k *ar = arvif->ar;
5236 enum ieee80211_band band = ar->hw->conf.chandef.chan->band;
5237 u8 fixed_rate = WMI_FIXED_RATE_NONE;
5238 u8 fixed_nss = ar->num_rf_chains;
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01005239 u8 force_sgi;
5240
Ben Greearb116ea12014-11-24 16:22:10 +02005241 if (ar->cfg_tx_chainmask)
5242 fixed_nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
5243
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01005244 force_sgi = mask->control[band].gi;
5245 if (force_sgi == NL80211_TXRATE_FORCE_LGI)
5246 return -EINVAL;
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005247
5248 if (!ath10k_default_bitrate_mask(ar, band, mask)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02005249 if (!ath10k_get_fixed_rate_nss(ar, mask, band,
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005250 &fixed_rate,
5251 &fixed_nss))
5252 return -EINVAL;
5253 }
5254
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01005255 if (fixed_rate == WMI_FIXED_RATE_NONE && force_sgi) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02005256 ath10k_warn(ar, "failed to force SGI usage for default rate settings\n");
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01005257 return -EINVAL;
5258 }
5259
5260 return ath10k_set_fixed_rate_param(arvif, fixed_rate,
5261 fixed_nss, force_sgi);
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005262}
5263
Michal Kazior9797feb2014-02-14 14:49:48 +01005264static void ath10k_sta_rc_update(struct ieee80211_hw *hw,
5265 struct ieee80211_vif *vif,
5266 struct ieee80211_sta *sta,
5267 u32 changed)
5268{
5269 struct ath10k *ar = hw->priv;
5270 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
5271 u32 bw, smps;
5272
5273 spin_lock_bh(&ar->data_lock);
5274
Michal Kazior7aa7a722014-08-25 12:09:38 +02005275 ath10k_dbg(ar, ATH10K_DBG_MAC,
Michal Kazior9797feb2014-02-14 14:49:48 +01005276 "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
5277 sta->addr, changed, sta->bandwidth, sta->rx_nss,
5278 sta->smps_mode);
5279
5280 if (changed & IEEE80211_RC_BW_CHANGED) {
5281 bw = WMI_PEER_CHWIDTH_20MHZ;
5282
5283 switch (sta->bandwidth) {
5284 case IEEE80211_STA_RX_BW_20:
5285 bw = WMI_PEER_CHWIDTH_20MHZ;
5286 break;
5287 case IEEE80211_STA_RX_BW_40:
5288 bw = WMI_PEER_CHWIDTH_40MHZ;
5289 break;
5290 case IEEE80211_STA_RX_BW_80:
5291 bw = WMI_PEER_CHWIDTH_80MHZ;
5292 break;
5293 case IEEE80211_STA_RX_BW_160:
Michal Kazior7aa7a722014-08-25 12:09:38 +02005294 ath10k_warn(ar, "Invalid bandwith %d in rc update for %pM\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02005295 sta->bandwidth, sta->addr);
Michal Kazior9797feb2014-02-14 14:49:48 +01005296 bw = WMI_PEER_CHWIDTH_20MHZ;
5297 break;
5298 }
5299
5300 arsta->bw = bw;
5301 }
5302
5303 if (changed & IEEE80211_RC_NSS_CHANGED)
5304 arsta->nss = sta->rx_nss;
5305
5306 if (changed & IEEE80211_RC_SMPS_CHANGED) {
5307 smps = WMI_PEER_SMPS_PS_NONE;
5308
5309 switch (sta->smps_mode) {
5310 case IEEE80211_SMPS_AUTOMATIC:
5311 case IEEE80211_SMPS_OFF:
5312 smps = WMI_PEER_SMPS_PS_NONE;
5313 break;
5314 case IEEE80211_SMPS_STATIC:
5315 smps = WMI_PEER_SMPS_STATIC;
5316 break;
5317 case IEEE80211_SMPS_DYNAMIC:
5318 smps = WMI_PEER_SMPS_DYNAMIC;
5319 break;
5320 case IEEE80211_SMPS_NUM_MODES:
Michal Kazior7aa7a722014-08-25 12:09:38 +02005321 ath10k_warn(ar, "Invalid smps %d in sta rc update for %pM\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02005322 sta->smps_mode, sta->addr);
Michal Kazior9797feb2014-02-14 14:49:48 +01005323 smps = WMI_PEER_SMPS_PS_NONE;
5324 break;
5325 }
5326
5327 arsta->smps = smps;
5328 }
5329
Michal Kazior9797feb2014-02-14 14:49:48 +01005330 arsta->changed |= changed;
5331
5332 spin_unlock_bh(&ar->data_lock);
5333
5334 ieee80211_queue_work(hw, &arsta->update_wk);
5335}
5336
Chun-Yeow Yeoh26ebbcc2014-02-25 09:29:54 +02005337static u64 ath10k_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
5338{
5339 /*
5340 * FIXME: Return 0 for time being. Need to figure out whether FW
5341 * has the API to fetch 64-bit local TSF
5342 */
5343
5344 return 0;
5345}
5346
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02005347static int ath10k_ampdu_action(struct ieee80211_hw *hw,
5348 struct ieee80211_vif *vif,
5349 enum ieee80211_ampdu_mlme_action action,
5350 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
5351 u8 buf_size)
5352{
Michal Kazior7aa7a722014-08-25 12:09:38 +02005353 struct ath10k *ar = hw->priv;
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02005354 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5355
Michal Kazior7aa7a722014-08-25 12:09:38 +02005356 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 +02005357 arvif->vdev_id, sta->addr, tid, action);
5358
5359 switch (action) {
5360 case IEEE80211_AMPDU_RX_START:
5361 case IEEE80211_AMPDU_RX_STOP:
5362 /* HTT AddBa/DelBa events trigger mac80211 Rx BA session
5363 * creation/removal. Do we need to verify this?
5364 */
5365 return 0;
5366 case IEEE80211_AMPDU_TX_START:
5367 case IEEE80211_AMPDU_TX_STOP_CONT:
5368 case IEEE80211_AMPDU_TX_STOP_FLUSH:
5369 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
5370 case IEEE80211_AMPDU_TX_OPERATIONAL:
5371 /* Firmware offloads Tx aggregation entirely so deny mac80211
5372 * Tx aggregation requests.
5373 */
5374 return -EOPNOTSUPP;
5375 }
5376
5377 return -EINVAL;
5378}
5379
Kalle Valo5e3dd152013-06-12 20:52:10 +03005380static const struct ieee80211_ops ath10k_ops = {
5381 .tx = ath10k_tx,
5382 .start = ath10k_start,
5383 .stop = ath10k_stop,
5384 .config = ath10k_config,
5385 .add_interface = ath10k_add_interface,
5386 .remove_interface = ath10k_remove_interface,
5387 .configure_filter = ath10k_configure_filter,
5388 .bss_info_changed = ath10k_bss_info_changed,
5389 .hw_scan = ath10k_hw_scan,
5390 .cancel_hw_scan = ath10k_cancel_hw_scan,
5391 .set_key = ath10k_set_key,
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +02005392 .set_default_unicast_key = ath10k_set_default_unicast_key,
Kalle Valo5e3dd152013-06-12 20:52:10 +03005393 .sta_state = ath10k_sta_state,
5394 .conf_tx = ath10k_conf_tx,
5395 .remain_on_channel = ath10k_remain_on_channel,
5396 .cancel_remain_on_channel = ath10k_cancel_remain_on_channel,
5397 .set_rts_threshold = ath10k_set_rts_threshold,
Kalle Valo5e3dd152013-06-12 20:52:10 +03005398 .flush = ath10k_flush,
5399 .tx_last_beacon = ath10k_tx_last_beacon,
Ben Greear46acf7b2014-05-16 17:15:38 +03005400 .set_antenna = ath10k_set_antenna,
5401 .get_antenna = ath10k_get_antenna,
Eliad Pellercf2c92d2014-11-04 11:43:54 +02005402 .reconfig_complete = ath10k_reconfig_complete,
Michal Kazior2e1dea42013-07-31 10:32:40 +02005403 .get_survey = ath10k_get_survey,
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005404 .set_bitrate_mask = ath10k_set_bitrate_mask,
Michal Kazior9797feb2014-02-14 14:49:48 +01005405 .sta_rc_update = ath10k_sta_rc_update,
Chun-Yeow Yeoh26ebbcc2014-02-25 09:29:54 +02005406 .get_tsf = ath10k_get_tsf,
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02005407 .ampdu_action = ath10k_ampdu_action,
Ben Greear6cddcc72014-09-29 14:41:46 +03005408 .get_et_sset_count = ath10k_debug_get_et_sset_count,
5409 .get_et_stats = ath10k_debug_get_et_stats,
5410 .get_et_strings = ath10k_debug_get_et_strings,
Kalle Valo43d2a302014-09-10 18:23:30 +03005411
5412 CFG80211_TESTMODE_CMD(ath10k_tm_cmd)
5413
Michal Kazior8cd13ca2013-07-16 09:38:54 +02005414#ifdef CONFIG_PM
5415 .suspend = ath10k_suspend,
5416 .resume = ath10k_resume,
5417#endif
Rajkumar Manoharanf5045982015-01-12 14:07:27 +02005418#ifdef CONFIG_MAC80211_DEBUGFS
5419 .sta_add_debugfs = ath10k_sta_add_debugfs,
5420#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03005421};
5422
5423#define RATETAB_ENT(_rate, _rateid, _flags) { \
5424 .bitrate = (_rate), \
5425 .flags = (_flags), \
5426 .hw_value = (_rateid), \
5427}
5428
5429#define CHAN2G(_channel, _freq, _flags) { \
5430 .band = IEEE80211_BAND_2GHZ, \
5431 .hw_value = (_channel), \
5432 .center_freq = (_freq), \
5433 .flags = (_flags), \
5434 .max_antenna_gain = 0, \
5435 .max_power = 30, \
5436}
5437
5438#define CHAN5G(_channel, _freq, _flags) { \
5439 .band = IEEE80211_BAND_5GHZ, \
5440 .hw_value = (_channel), \
5441 .center_freq = (_freq), \
5442 .flags = (_flags), \
5443 .max_antenna_gain = 0, \
5444 .max_power = 30, \
5445}
5446
5447static const struct ieee80211_channel ath10k_2ghz_channels[] = {
5448 CHAN2G(1, 2412, 0),
5449 CHAN2G(2, 2417, 0),
5450 CHAN2G(3, 2422, 0),
5451 CHAN2G(4, 2427, 0),
5452 CHAN2G(5, 2432, 0),
5453 CHAN2G(6, 2437, 0),
5454 CHAN2G(7, 2442, 0),
5455 CHAN2G(8, 2447, 0),
5456 CHAN2G(9, 2452, 0),
5457 CHAN2G(10, 2457, 0),
5458 CHAN2G(11, 2462, 0),
5459 CHAN2G(12, 2467, 0),
5460 CHAN2G(13, 2472, 0),
5461 CHAN2G(14, 2484, 0),
5462};
5463
5464static const struct ieee80211_channel ath10k_5ghz_channels[] = {
Michal Kazior429ff562013-06-26 08:54:54 +02005465 CHAN5G(36, 5180, 0),
5466 CHAN5G(40, 5200, 0),
5467 CHAN5G(44, 5220, 0),
5468 CHAN5G(48, 5240, 0),
5469 CHAN5G(52, 5260, 0),
5470 CHAN5G(56, 5280, 0),
5471 CHAN5G(60, 5300, 0),
5472 CHAN5G(64, 5320, 0),
5473 CHAN5G(100, 5500, 0),
5474 CHAN5G(104, 5520, 0),
5475 CHAN5G(108, 5540, 0),
5476 CHAN5G(112, 5560, 0),
5477 CHAN5G(116, 5580, 0),
5478 CHAN5G(120, 5600, 0),
5479 CHAN5G(124, 5620, 0),
5480 CHAN5G(128, 5640, 0),
5481 CHAN5G(132, 5660, 0),
5482 CHAN5G(136, 5680, 0),
5483 CHAN5G(140, 5700, 0),
5484 CHAN5G(149, 5745, 0),
5485 CHAN5G(153, 5765, 0),
5486 CHAN5G(157, 5785, 0),
5487 CHAN5G(161, 5805, 0),
5488 CHAN5G(165, 5825, 0),
Kalle Valo5e3dd152013-06-12 20:52:10 +03005489};
5490
Michal Kazior91b12082014-12-12 12:41:35 +01005491/* Note: Be careful if you re-order these. There is code which depends on this
5492 * ordering.
5493 */
Kalle Valo5e3dd152013-06-12 20:52:10 +03005494static struct ieee80211_rate ath10k_rates[] = {
5495 /* CCK */
5496 RATETAB_ENT(10, 0x82, 0),
5497 RATETAB_ENT(20, 0x84, 0),
5498 RATETAB_ENT(55, 0x8b, 0),
5499 RATETAB_ENT(110, 0x96, 0),
5500 /* OFDM */
5501 RATETAB_ENT(60, 0x0c, 0),
5502 RATETAB_ENT(90, 0x12, 0),
5503 RATETAB_ENT(120, 0x18, 0),
5504 RATETAB_ENT(180, 0x24, 0),
5505 RATETAB_ENT(240, 0x30, 0),
5506 RATETAB_ENT(360, 0x48, 0),
5507 RATETAB_ENT(480, 0x60, 0),
5508 RATETAB_ENT(540, 0x6c, 0),
5509};
5510
5511#define ath10k_a_rates (ath10k_rates + 4)
5512#define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - 4)
5513#define ath10k_g_rates (ath10k_rates + 0)
5514#define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
5515
Michal Kaziore7b54192014-08-07 11:03:27 +02005516struct ath10k *ath10k_mac_create(size_t priv_size)
Kalle Valo5e3dd152013-06-12 20:52:10 +03005517{
5518 struct ieee80211_hw *hw;
5519 struct ath10k *ar;
5520
Michal Kaziore7b54192014-08-07 11:03:27 +02005521 hw = ieee80211_alloc_hw(sizeof(struct ath10k) + priv_size, &ath10k_ops);
Kalle Valo5e3dd152013-06-12 20:52:10 +03005522 if (!hw)
5523 return NULL;
5524
5525 ar = hw->priv;
5526 ar->hw = hw;
5527
5528 return ar;
5529}
5530
5531void ath10k_mac_destroy(struct ath10k *ar)
5532{
5533 ieee80211_free_hw(ar->hw);
5534}
5535
5536static const struct ieee80211_iface_limit ath10k_if_limits[] = {
5537 {
5538 .max = 8,
5539 .types = BIT(NL80211_IFTYPE_STATION)
5540 | BIT(NL80211_IFTYPE_P2P_CLIENT)
Michal Kaziord531cb82013-07-31 10:55:13 +02005541 },
5542 {
5543 .max = 3,
5544 .types = BIT(NL80211_IFTYPE_P2P_GO)
5545 },
5546 {
Michal Kazior75d2bd42014-12-12 12:41:39 +01005547 .max = 1,
5548 .types = BIT(NL80211_IFTYPE_P2P_DEVICE)
5549 },
5550 {
Michal Kaziord531cb82013-07-31 10:55:13 +02005551 .max = 7,
5552 .types = BIT(NL80211_IFTYPE_AP)
5553 },
Kalle Valo5e3dd152013-06-12 20:52:10 +03005554};
5555
Bartosz Markowskif2595092013-12-10 16:20:39 +01005556static const struct ieee80211_iface_limit ath10k_10x_if_limits[] = {
Marek Puzyniake8a50f82013-11-20 09:59:47 +02005557 {
5558 .max = 8,
5559 .types = BIT(NL80211_IFTYPE_AP)
5560 },
5561};
Marek Puzyniake8a50f82013-11-20 09:59:47 +02005562
5563static const struct ieee80211_iface_combination ath10k_if_comb[] = {
5564 {
5565 .limits = ath10k_if_limits,
5566 .n_limits = ARRAY_SIZE(ath10k_if_limits),
5567 .max_interfaces = 8,
5568 .num_different_channels = 1,
5569 .beacon_int_infra_match = true,
5570 },
Bartosz Markowskif2595092013-12-10 16:20:39 +01005571};
5572
5573static const struct ieee80211_iface_combination ath10k_10x_if_comb[] = {
Marek Puzyniake8a50f82013-11-20 09:59:47 +02005574 {
Bartosz Markowskif2595092013-12-10 16:20:39 +01005575 .limits = ath10k_10x_if_limits,
5576 .n_limits = ARRAY_SIZE(ath10k_10x_if_limits),
Marek Puzyniake8a50f82013-11-20 09:59:47 +02005577 .max_interfaces = 8,
5578 .num_different_channels = 1,
5579 .beacon_int_infra_match = true,
Bartosz Markowskif2595092013-12-10 16:20:39 +01005580#ifdef CONFIG_ATH10K_DFS_CERTIFIED
Marek Puzyniake8a50f82013-11-20 09:59:47 +02005581 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
5582 BIT(NL80211_CHAN_WIDTH_20) |
5583 BIT(NL80211_CHAN_WIDTH_40) |
5584 BIT(NL80211_CHAN_WIDTH_80),
Marek Puzyniake8a50f82013-11-20 09:59:47 +02005585#endif
Bartosz Markowskif2595092013-12-10 16:20:39 +01005586 },
Kalle Valo5e3dd152013-06-12 20:52:10 +03005587};
5588
5589static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
5590{
5591 struct ieee80211_sta_vht_cap vht_cap = {0};
5592 u16 mcs_map;
Michal Kaziorbc657a362015-02-26 11:11:22 +01005593 u32 val;
Michal Kazior8865bee42013-07-24 12:36:46 +02005594 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005595
5596 vht_cap.vht_supported = 1;
5597 vht_cap.cap = ar->vht_cap_info;
5598
Michal Kaziorbc657a362015-02-26 11:11:22 +01005599 if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
5600 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)) {
5601 val = ar->num_rf_chains - 1;
5602 val <<= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
5603 val &= IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
5604
5605 vht_cap.cap |= val;
5606 }
5607
5608 if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
5609 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)) {
5610 val = ar->num_rf_chains - 1;
5611 val <<= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
5612 val &= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
5613
5614 vht_cap.cap |= val;
5615 }
5616
Michal Kazior8865bee42013-07-24 12:36:46 +02005617 mcs_map = 0;
5618 for (i = 0; i < 8; i++) {
5619 if (i < ar->num_rf_chains)
5620 mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i*2);
5621 else
5622 mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i*2);
5623 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03005624
5625 vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
5626 vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
5627
5628 return vht_cap;
5629}
5630
5631static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
5632{
5633 int i;
5634 struct ieee80211_sta_ht_cap ht_cap = {0};
5635
5636 if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
5637 return ht_cap;
5638
5639 ht_cap.ht_supported = 1;
5640 ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
5641 ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
5642 ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
5643 ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
5644 ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
5645
5646 if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
5647 ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
5648
5649 if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
5650 ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
5651
5652 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
5653 u32 smps;
5654
5655 smps = WLAN_HT_CAP_SM_PS_DYNAMIC;
5656 smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
5657
5658 ht_cap.cap |= smps;
5659 }
5660
5661 if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC)
5662 ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
5663
5664 if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
5665 u32 stbc;
5666
5667 stbc = ar->ht_cap_info;
5668 stbc &= WMI_HT_CAP_RX_STBC;
5669 stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
5670 stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
5671 stbc &= IEEE80211_HT_CAP_RX_STBC;
5672
5673 ht_cap.cap |= stbc;
5674 }
5675
5676 if (ar->ht_cap_info & WMI_HT_CAP_LDPC)
5677 ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
5678
5679 if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
5680 ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
5681
5682 /* max AMSDU is implicitly taken from vht_cap_info */
5683 if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
5684 ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
5685
Michal Kazior8865bee42013-07-24 12:36:46 +02005686 for (i = 0; i < ar->num_rf_chains; i++)
Kalle Valo5e3dd152013-06-12 20:52:10 +03005687 ht_cap.mcs.rx_mask[i] = 0xFF;
5688
5689 ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
5690
5691 return ht_cap;
5692}
5693
Kalle Valo5e3dd152013-06-12 20:52:10 +03005694static void ath10k_get_arvif_iter(void *data, u8 *mac,
5695 struct ieee80211_vif *vif)
5696{
5697 struct ath10k_vif_iter *arvif_iter = data;
5698 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5699
5700 if (arvif->vdev_id == arvif_iter->vdev_id)
5701 arvif_iter->arvif = arvif;
5702}
5703
5704struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
5705{
5706 struct ath10k_vif_iter arvif_iter;
5707 u32 flags;
5708
5709 memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
5710 arvif_iter.vdev_id = vdev_id;
5711
5712 flags = IEEE80211_IFACE_ITER_RESUME_ALL;
5713 ieee80211_iterate_active_interfaces_atomic(ar->hw,
5714 flags,
5715 ath10k_get_arvif_iter,
5716 &arvif_iter);
5717 if (!arvif_iter.arvif) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02005718 ath10k_warn(ar, "No VIF found for vdev %d\n", vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03005719 return NULL;
5720 }
5721
5722 return arvif_iter.arvif;
5723}
5724
5725int ath10k_mac_register(struct ath10k *ar)
5726{
Johannes Berg3cb10942015-01-22 21:38:45 +01005727 static const u32 cipher_suites[] = {
5728 WLAN_CIPHER_SUITE_WEP40,
5729 WLAN_CIPHER_SUITE_WEP104,
5730 WLAN_CIPHER_SUITE_TKIP,
5731 WLAN_CIPHER_SUITE_CCMP,
5732 WLAN_CIPHER_SUITE_AES_CMAC,
5733 };
Kalle Valo5e3dd152013-06-12 20:52:10 +03005734 struct ieee80211_supported_band *band;
5735 struct ieee80211_sta_vht_cap vht_cap;
5736 struct ieee80211_sta_ht_cap ht_cap;
5737 void *channels;
5738 int ret;
5739
5740 SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
5741
5742 SET_IEEE80211_DEV(ar->hw, ar->dev);
5743
5744 ht_cap = ath10k_get_ht_cap(ar);
5745 vht_cap = ath10k_create_vht_cap(ar);
5746
5747 if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
5748 channels = kmemdup(ath10k_2ghz_channels,
5749 sizeof(ath10k_2ghz_channels),
5750 GFP_KERNEL);
Michal Kaziord6015b22013-07-22 14:13:30 +02005751 if (!channels) {
5752 ret = -ENOMEM;
5753 goto err_free;
5754 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03005755
5756 band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
5757 band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
5758 band->channels = channels;
5759 band->n_bitrates = ath10k_g_rates_size;
5760 band->bitrates = ath10k_g_rates;
5761 band->ht_cap = ht_cap;
5762
Yanbo Lid68bb122015-01-23 08:18:20 +08005763 /* Enable the VHT support at 2.4 GHz */
5764 band->vht_cap = vht_cap;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005765
5766 ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
5767 }
5768
5769 if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
5770 channels = kmemdup(ath10k_5ghz_channels,
5771 sizeof(ath10k_5ghz_channels),
5772 GFP_KERNEL);
5773 if (!channels) {
Michal Kaziord6015b22013-07-22 14:13:30 +02005774 ret = -ENOMEM;
5775 goto err_free;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005776 }
5777
5778 band = &ar->mac.sbands[IEEE80211_BAND_5GHZ];
5779 band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
5780 band->channels = channels;
5781 band->n_bitrates = ath10k_a_rates_size;
5782 band->bitrates = ath10k_a_rates;
5783 band->ht_cap = ht_cap;
5784 band->vht_cap = vht_cap;
5785 ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
5786 }
5787
5788 ar->hw->wiphy->interface_modes =
5789 BIT(NL80211_IFTYPE_STATION) |
Bartosz Markowskid3541812013-12-10 16:20:40 +01005790 BIT(NL80211_IFTYPE_AP);
5791
Ben Greear46acf7b2014-05-16 17:15:38 +03005792 ar->hw->wiphy->available_antennas_rx = ar->supp_rx_chainmask;
5793 ar->hw->wiphy->available_antennas_tx = ar->supp_tx_chainmask;
5794
Bartosz Markowskid3541812013-12-10 16:20:40 +01005795 if (!test_bit(ATH10K_FW_FEATURE_NO_P2P, ar->fw_features))
5796 ar->hw->wiphy->interface_modes |=
Michal Kazior75d2bd42014-12-12 12:41:39 +01005797 BIT(NL80211_IFTYPE_P2P_DEVICE) |
Bartosz Markowskid3541812013-12-10 16:20:40 +01005798 BIT(NL80211_IFTYPE_P2P_CLIENT) |
5799 BIT(NL80211_IFTYPE_P2P_GO);
Kalle Valo5e3dd152013-06-12 20:52:10 +03005800
5801 ar->hw->flags = IEEE80211_HW_SIGNAL_DBM |
5802 IEEE80211_HW_SUPPORTS_PS |
5803 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
Kalle Valo5e3dd152013-06-12 20:52:10 +03005804 IEEE80211_HW_MFP_CAPABLE |
5805 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
5806 IEEE80211_HW_HAS_RATE_CONTROL |
Janusz Dziedzic2f0f1122014-02-26 18:42:09 +02005807 IEEE80211_HW_AP_LINK_PS |
Johannes Berg3cb10942015-01-22 21:38:45 +01005808 IEEE80211_HW_SPECTRUM_MGMT |
Michal Kaziorcc9904e2015-03-10 16:22:01 +02005809 IEEE80211_HW_SW_CRYPTO_CONTROL |
5810 IEEE80211_HW_CONNECTION_MONITOR;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005811
Eliad Peller0d8614b2014-09-10 14:07:36 +03005812 ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
5813
Kalle Valo5e3dd152013-06-12 20:52:10 +03005814 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
Eliad Peller0d8614b2014-09-10 14:07:36 +03005815 ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005816
5817 if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
5818 ar->hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
5819 ar->hw->flags |= IEEE80211_HW_TX_AMPDU_SETUP_IN_HW;
5820 }
5821
5822 ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
5823 ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
5824
5825 ar->hw->vif_data_size = sizeof(struct ath10k_vif);
Michal Kazior9797feb2014-02-14 14:49:48 +01005826 ar->hw->sta_data_size = sizeof(struct ath10k_sta);
Kalle Valo5e3dd152013-06-12 20:52:10 +03005827
Kalle Valo5e3dd152013-06-12 20:52:10 +03005828 ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
5829
Michal Kaziorfbb8f1b2015-01-13 16:30:12 +02005830 if (test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)) {
5831 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
5832
5833 /* Firmware delivers WPS/P2P Probe Requests frames to driver so
5834 * that userspace (e.g. wpa_supplicant/hostapd) can generate
5835 * correct Probe Responses. This is more of a hack advert..
5836 */
5837 ar->hw->wiphy->probe_resp_offload |=
5838 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
5839 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
5840 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
5841 }
5842
Kalle Valo5e3dd152013-06-12 20:52:10 +03005843 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
Michal Kaziorc2df44b2014-01-23 11:38:26 +01005844 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005845 ar->hw->wiphy->max_remain_on_channel_duration = 5000;
5846
5847 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
Rajkumar Manoharan78157a12014-11-17 16:44:15 +02005848 ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE;
5849
Janusz.Dziedzic@tieto.com37a0b392015-03-12 13:11:41 +01005850 ar->hw->wiphy->max_ap_assoc_sta = ar->max_num_stations;
5851
Kalle Valo5e3dd152013-06-12 20:52:10 +03005852 /*
5853 * on LL hardware queues are managed entirely by the FW
5854 * so we only advertise to mac we can do the queues thing
5855 */
5856 ar->hw->queues = 4;
5857
Kalle Valo5cc7caf2014-12-17 12:20:54 +02005858 switch (ar->wmi.op_version) {
5859 case ATH10K_FW_WMI_OP_VERSION_MAIN:
5860 case ATH10K_FW_WMI_OP_VERSION_TLV:
Bartosz Markowskif2595092013-12-10 16:20:39 +01005861 ar->hw->wiphy->iface_combinations = ath10k_if_comb;
5862 ar->hw->wiphy->n_iface_combinations =
5863 ARRAY_SIZE(ath10k_if_comb);
Michal Kaziorcf850d12014-07-24 20:07:00 +03005864 ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
Kalle Valo5cc7caf2014-12-17 12:20:54 +02005865 break;
5866 case ATH10K_FW_WMI_OP_VERSION_10_1:
5867 case ATH10K_FW_WMI_OP_VERSION_10_2:
Rajkumar Manoharan4a16fbe2014-12-17 12:21:12 +02005868 case ATH10K_FW_WMI_OP_VERSION_10_2_4:
Kalle Valo5cc7caf2014-12-17 12:20:54 +02005869 ar->hw->wiphy->iface_combinations = ath10k_10x_if_comb;
5870 ar->hw->wiphy->n_iface_combinations =
5871 ARRAY_SIZE(ath10k_10x_if_comb);
5872 break;
5873 case ATH10K_FW_WMI_OP_VERSION_UNSET:
5874 case ATH10K_FW_WMI_OP_VERSION_MAX:
5875 WARN_ON(1);
5876 ret = -EINVAL;
5877 goto err_free;
Bartosz Markowskif2595092013-12-10 16:20:39 +01005878 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03005879
Michal Kazior7c199992013-07-31 10:47:57 +02005880 ar->hw->netdev_features = NETIF_F_HW_CSUM;
5881
Janusz Dziedzic9702c682013-11-20 09:59:41 +02005882 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED)) {
5883 /* Init ath dfs pattern detector */
5884 ar->ath_common.debug_mask = ATH_DBG_DFS;
5885 ar->dfs_detector = dfs_pattern_detector_init(&ar->ath_common,
5886 NL80211_DFS_UNSET);
5887
5888 if (!ar->dfs_detector)
Michal Kazior7aa7a722014-08-25 12:09:38 +02005889 ath10k_warn(ar, "failed to initialise DFS pattern detector\n");
Janusz Dziedzic9702c682013-11-20 09:59:41 +02005890 }
5891
Kalle Valo5e3dd152013-06-12 20:52:10 +03005892 ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
5893 ath10k_reg_notifier);
5894 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02005895 ath10k_err(ar, "failed to initialise regulatory: %i\n", ret);
Michal Kaziord6015b22013-07-22 14:13:30 +02005896 goto err_free;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005897 }
5898
Johannes Berg3cb10942015-01-22 21:38:45 +01005899 ar->hw->wiphy->cipher_suites = cipher_suites;
5900 ar->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
5901
Kalle Valo5e3dd152013-06-12 20:52:10 +03005902 ret = ieee80211_register_hw(ar->hw);
5903 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02005904 ath10k_err(ar, "failed to register ieee80211: %d\n", ret);
Michal Kaziord6015b22013-07-22 14:13:30 +02005905 goto err_free;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005906 }
5907
5908 if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
5909 ret = regulatory_hint(ar->hw->wiphy,
5910 ar->ath_common.regulatory.alpha2);
5911 if (ret)
Michal Kaziord6015b22013-07-22 14:13:30 +02005912 goto err_unregister;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005913 }
5914
5915 return 0;
Michal Kaziord6015b22013-07-22 14:13:30 +02005916
5917err_unregister:
Kalle Valo5e3dd152013-06-12 20:52:10 +03005918 ieee80211_unregister_hw(ar->hw);
Michal Kaziord6015b22013-07-22 14:13:30 +02005919err_free:
5920 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
5921 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
5922
Kalle Valo5e3dd152013-06-12 20:52:10 +03005923 return ret;
5924}
5925
5926void ath10k_mac_unregister(struct ath10k *ar)
5927{
5928 ieee80211_unregister_hw(ar->hw);
5929
Janusz Dziedzic9702c682013-11-20 09:59:41 +02005930 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
5931 ar->dfs_detector->exit(ar->dfs_detector);
5932
Kalle Valo5e3dd152013-06-12 20:52:10 +03005933 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
5934 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
5935
5936 SET_IEEE80211_DEV(ar->hw, NULL);
5937}