blob: 73a9d3843e91375bf377cd683b48a99d127314ad [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 Kazior46725b152015-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
Ashok Raj Nagarajan575f1c32015-03-19 16:37:59 +05303156 ret = ath10k_wmi_pdev_set_param(ar,
3157 ar->wmi.pdev_param->ani_enable, 1);
3158 if (ret) {
3159 ath10k_warn(ar, "failed to enable ani by default: %d\n",
3160 ret);
3161 goto err_core_stop;
3162 }
3163
Ashok Raj Nagarajanb3e71d72015-03-19 16:38:00 +05303164 ar->ani_enabled = true;
3165
Michal Kaziord6500972014-04-08 09:56:09 +03003166 ar->num_started_vdevs = 0;
Michal Kaziorf7843d72013-07-16 09:38:52 +02003167 ath10k_regd_update(ar);
3168
Simon Wunderlich855aed12014-08-02 09:12:54 +03003169 ath10k_spectral_start(ar);
Rajkumar Manoharan8515b5c2015-03-15 20:36:22 +05303170 ath10k_thermal_set_throttling(ar);
Simon Wunderlich855aed12014-08-02 09:12:54 +03003171
Michal Kaziorae254432014-05-26 12:46:02 +03003172 mutex_unlock(&ar->conf_mutex);
3173 return 0;
3174
3175err_core_stop:
3176 ath10k_core_stop(ar);
3177
3178err_power_down:
3179 ath10k_hif_power_down(ar);
3180
3181err_off:
3182 ar->state = ATH10K_STATE_OFF;
3183
3184err:
Michal Kazior548db542013-07-05 16:15:15 +03003185 mutex_unlock(&ar->conf_mutex);
Michal Kaziorc60bdd82014-01-29 07:26:31 +01003186 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003187}
3188
3189static void ath10k_stop(struct ieee80211_hw *hw)
3190{
3191 struct ath10k *ar = hw->priv;
3192
Michal Kaziorbca7baf2014-05-26 12:46:03 +03003193 ath10k_drain_tx(ar);
3194
Michal Kazior548db542013-07-05 16:15:15 +03003195 mutex_lock(&ar->conf_mutex);
Michal Kaziorc5058f52014-05-26 12:46:03 +03003196 if (ar->state != ATH10K_STATE_OFF) {
Michal Kazior818bdd12013-07-16 09:38:57 +02003197 ath10k_halt(ar);
Michal Kaziorc5058f52014-05-26 12:46:03 +03003198 ar->state = ATH10K_STATE_OFF;
3199 }
Michal Kazior548db542013-07-05 16:15:15 +03003200 mutex_unlock(&ar->conf_mutex);
3201
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003202 cancel_delayed_work_sync(&ar->scan.timeout);
Michal Kazioraffd3212013-07-16 09:54:35 +02003203 cancel_work_sync(&ar->restart_work);
3204}
3205
Michal Kaziorad088bf2013-10-16 15:44:46 +03003206static int ath10k_config_ps(struct ath10k *ar)
Michal Kazioraffd3212013-07-16 09:54:35 +02003207{
Michal Kaziorad088bf2013-10-16 15:44:46 +03003208 struct ath10k_vif *arvif;
3209 int ret = 0;
Michal Kazioraffd3212013-07-16 09:54:35 +02003210
3211 lockdep_assert_held(&ar->conf_mutex);
3212
Michal Kaziorad088bf2013-10-16 15:44:46 +03003213 list_for_each_entry(arvif, &ar->arvifs, list) {
3214 ret = ath10k_mac_vif_setup_ps(arvif);
3215 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003216 ath10k_warn(ar, "failed to setup powersave: %d\n", ret);
Michal Kaziorad088bf2013-10-16 15:44:46 +03003217 break;
3218 }
3219 }
Michal Kazioraffd3212013-07-16 09:54:35 +02003220
Michal Kaziorad088bf2013-10-16 15:44:46 +03003221 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003222}
3223
Michal Kaziorc930f742014-01-23 11:38:25 +01003224static const char *chandef_get_width(enum nl80211_chan_width width)
3225{
3226 switch (width) {
3227 case NL80211_CHAN_WIDTH_20_NOHT:
3228 return "20 (noht)";
3229 case NL80211_CHAN_WIDTH_20:
3230 return "20";
3231 case NL80211_CHAN_WIDTH_40:
3232 return "40";
3233 case NL80211_CHAN_WIDTH_80:
3234 return "80";
3235 case NL80211_CHAN_WIDTH_80P80:
3236 return "80+80";
3237 case NL80211_CHAN_WIDTH_160:
3238 return "160";
3239 case NL80211_CHAN_WIDTH_5:
3240 return "5";
3241 case NL80211_CHAN_WIDTH_10:
3242 return "10";
3243 }
3244 return "?";
3245}
3246
3247static void ath10k_config_chan(struct ath10k *ar)
3248{
3249 struct ath10k_vif *arvif;
Michal Kaziorc930f742014-01-23 11:38:25 +01003250 int ret;
3251
3252 lockdep_assert_held(&ar->conf_mutex);
3253
Michal Kazior7aa7a722014-08-25 12:09:38 +02003254 ath10k_dbg(ar, ATH10K_DBG_MAC,
Michal Kaziorc930f742014-01-23 11:38:25 +01003255 "mac config channel to %dMHz (cf1 %dMHz cf2 %dMHz width %s)\n",
3256 ar->chandef.chan->center_freq,
3257 ar->chandef.center_freq1,
3258 ar->chandef.center_freq2,
3259 chandef_get_width(ar->chandef.width));
3260
3261 /* First stop monitor interface. Some FW versions crash if there's a
3262 * lone monitor interface. */
Michal Kazior1bbc0972014-04-08 09:45:47 +03003263 if (ar->monitor_started)
Michal Kazior19337472014-08-28 12:58:16 +02003264 ath10k_monitor_stop(ar);
Michal Kaziorc930f742014-01-23 11:38:25 +01003265
3266 list_for_each_entry(arvif, &ar->arvifs, list) {
3267 if (!arvif->is_started)
3268 continue;
3269
Michal Kaziordc55e302014-07-29 12:53:36 +03003270 if (!arvif->is_up)
3271 continue;
3272
Michal Kaziorc930f742014-01-23 11:38:25 +01003273 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
3274 continue;
3275
Michal Kaziordc55e302014-07-29 12:53:36 +03003276 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
Michal Kaziorc930f742014-01-23 11:38:25 +01003277 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003278 ath10k_warn(ar, "failed to down vdev %d: %d\n",
Michal Kaziorc930f742014-01-23 11:38:25 +01003279 arvif->vdev_id, ret);
3280 continue;
3281 }
3282 }
3283
Michal Kaziordc55e302014-07-29 12:53:36 +03003284 /* all vdevs are downed now - attempt to restart and re-up them */
Michal Kaziorc930f742014-01-23 11:38:25 +01003285
3286 list_for_each_entry(arvif, &ar->arvifs, list) {
3287 if (!arvif->is_started)
3288 continue;
3289
3290 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
3291 continue;
3292
Michal Kazior81a9a172015-03-05 16:02:17 +02003293 ret = ath10k_mac_setup_bcn_tmpl(arvif);
3294 if (ret)
3295 ath10k_warn(ar, "failed to update bcn tmpl during csa: %d\n",
3296 ret);
3297
3298 ret = ath10k_mac_setup_prb_tmpl(arvif);
3299 if (ret)
3300 ath10k_warn(ar, "failed to update prb tmpl during csa: %d\n",
3301 ret);
3302
Michal Kaziordc55e302014-07-29 12:53:36 +03003303 ret = ath10k_vdev_restart(arvif);
Michal Kaziorc930f742014-01-23 11:38:25 +01003304 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003305 ath10k_warn(ar, "failed to restart vdev %d: %d\n",
Michal Kaziorc930f742014-01-23 11:38:25 +01003306 arvif->vdev_id, ret);
3307 continue;
3308 }
3309
3310 if (!arvif->is_up)
3311 continue;
3312
3313 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
3314 arvif->bssid);
3315 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003316 ath10k_warn(ar, "failed to bring vdev up %d: %d\n",
Michal Kaziorc930f742014-01-23 11:38:25 +01003317 arvif->vdev_id, ret);
3318 continue;
3319 }
3320 }
3321
Michal Kazior19337472014-08-28 12:58:16 +02003322 ath10k_monitor_recalc(ar);
Michal Kaziorc930f742014-01-23 11:38:25 +01003323}
3324
Michal Kazior7d9d5582014-10-21 10:40:15 +03003325static int ath10k_mac_txpower_setup(struct ath10k *ar, int txpower)
3326{
3327 int ret;
3328 u32 param;
3329
3330 lockdep_assert_held(&ar->conf_mutex);
3331
3332 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac txpower %d\n", txpower);
3333
3334 param = ar->wmi.pdev_param->txpower_limit2g;
3335 ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
3336 if (ret) {
3337 ath10k_warn(ar, "failed to set 2g txpower %d: %d\n",
3338 txpower, ret);
3339 return ret;
3340 }
3341
3342 param = ar->wmi.pdev_param->txpower_limit5g;
3343 ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
3344 if (ret) {
3345 ath10k_warn(ar, "failed to set 5g txpower %d: %d\n",
3346 txpower, ret);
3347 return ret;
3348 }
3349
3350 return 0;
3351}
3352
3353static int ath10k_mac_txpower_recalc(struct ath10k *ar)
3354{
3355 struct ath10k_vif *arvif;
3356 int ret, txpower = -1;
3357
3358 lockdep_assert_held(&ar->conf_mutex);
3359
3360 list_for_each_entry(arvif, &ar->arvifs, list) {
3361 WARN_ON(arvif->txpower < 0);
3362
3363 if (txpower == -1)
3364 txpower = arvif->txpower;
3365 else
3366 txpower = min(txpower, arvif->txpower);
3367 }
3368
3369 if (WARN_ON(txpower == -1))
3370 return -EINVAL;
3371
3372 ret = ath10k_mac_txpower_setup(ar, txpower);
3373 if (ret) {
3374 ath10k_warn(ar, "failed to setup tx power %d: %d\n",
3375 txpower, ret);
3376 return ret;
3377 }
3378
3379 return 0;
3380}
3381
Kalle Valo5e3dd152013-06-12 20:52:10 +03003382static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
3383{
Kalle Valo5e3dd152013-06-12 20:52:10 +03003384 struct ath10k *ar = hw->priv;
3385 struct ieee80211_conf *conf = &hw->conf;
3386 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003387
3388 mutex_lock(&ar->conf_mutex);
3389
3390 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003391 ath10k_dbg(ar, ATH10K_DBG_MAC,
Michal Kaziord6500972014-04-08 09:56:09 +03003392 "mac config channel %dMHz flags 0x%x radar %d\n",
Marek Puzyniake8a50f82013-11-20 09:59:47 +02003393 conf->chandef.chan->center_freq,
Michal Kaziord6500972014-04-08 09:56:09 +03003394 conf->chandef.chan->flags,
3395 conf->radar_enabled);
Marek Puzyniake8a50f82013-11-20 09:59:47 +02003396
Kalle Valo5e3dd152013-06-12 20:52:10 +03003397 spin_lock_bh(&ar->data_lock);
3398 ar->rx_channel = conf->chandef.chan;
3399 spin_unlock_bh(&ar->data_lock);
Marek Puzyniake8a50f82013-11-20 09:59:47 +02003400
Michal Kaziord6500972014-04-08 09:56:09 +03003401 ar->radar_enabled = conf->radar_enabled;
3402 ath10k_recalc_radar_detection(ar);
Michal Kaziorc930f742014-01-23 11:38:25 +01003403
3404 if (!cfg80211_chandef_identical(&ar->chandef, &conf->chandef)) {
3405 ar->chandef = conf->chandef;
3406 ath10k_config_chan(ar);
3407 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003408 }
3409
Michal Kazioraffd3212013-07-16 09:54:35 +02003410 if (changed & IEEE80211_CONF_CHANGE_PS)
3411 ath10k_config_ps(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003412
3413 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
Michal Kazior19337472014-08-28 12:58:16 +02003414 ar->monitor = conf->flags & IEEE80211_CONF_MONITOR;
3415 ret = ath10k_monitor_recalc(ar);
3416 if (ret)
3417 ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003418 }
3419
3420 mutex_unlock(&ar->conf_mutex);
3421 return ret;
3422}
3423
Ben Greear5572a952014-11-24 16:22:10 +02003424static u32 get_nss_from_chainmask(u16 chain_mask)
3425{
3426 if ((chain_mask & 0x15) == 0x15)
3427 return 4;
3428 else if ((chain_mask & 0x7) == 0x7)
3429 return 3;
3430 else if ((chain_mask & 0x3) == 0x3)
3431 return 2;
3432 return 1;
3433}
3434
Kalle Valo5e3dd152013-06-12 20:52:10 +03003435/*
3436 * TODO:
3437 * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
3438 * because we will send mgmt frames without CCK. This requirement
3439 * for P2P_FIND/GO_NEG should be handled by checking CCK flag
3440 * in the TX packet.
3441 */
3442static int ath10k_add_interface(struct ieee80211_hw *hw,
3443 struct ieee80211_vif *vif)
3444{
3445 struct ath10k *ar = hw->priv;
3446 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3447 enum wmi_sta_powersave_param param;
3448 int ret = 0;
Kalle Valo5a13e762014-01-20 11:01:46 +02003449 u32 value;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003450 int bit;
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003451 u32 vdev_param;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003452
Johannes Berg848955c2014-11-11 12:48:42 +01003453 vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
3454
Kalle Valo5e3dd152013-06-12 20:52:10 +03003455 mutex_lock(&ar->conf_mutex);
3456
Michal Kazior0dbd09e2013-07-31 10:55:14 +02003457 memset(arvif, 0, sizeof(*arvif));
3458
Kalle Valo5e3dd152013-06-12 20:52:10 +03003459 arvif->ar = ar;
3460 arvif->vif = vif;
3461
Ben Greeare63b33f2013-10-22 14:54:14 -07003462 INIT_LIST_HEAD(&arvif->list);
Michal Kazior81a9a172015-03-05 16:02:17 +02003463 INIT_WORK(&arvif->ap_csa_work, ath10k_mac_vif_ap_csa_work);
Michal Kaziorcc9904e2015-03-10 16:22:01 +02003464 INIT_DELAYED_WORK(&arvif->connection_loss_work,
3465 ath10k_mac_vif_sta_connection_loss_work);
Michal Kaziorcc4827b2013-10-16 15:44:45 +03003466
Ben Greeara9aefb32014-08-12 11:02:19 +03003467 if (ar->free_vdev_map == 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003468 ath10k_warn(ar, "Free vdev map is empty, no more interfaces allowed.\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03003469 ret = -EBUSY;
Michal Kazior9dad14a2013-10-16 15:44:45 +03003470 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003471 }
Ben Greear16c11172014-09-23 14:17:16 -07003472 bit = __ffs64(ar->free_vdev_map);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003473
Ben Greear16c11172014-09-23 14:17:16 -07003474 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac create vdev %i map %llx\n",
3475 bit, ar->free_vdev_map);
3476
3477 arvif->vdev_id = bit;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003478 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003479
Kalle Valo5e3dd152013-06-12 20:52:10 +03003480 switch (vif->type) {
Michal Kazior75d2bd42014-12-12 12:41:39 +01003481 case NL80211_IFTYPE_P2P_DEVICE:
3482 arvif->vdev_type = WMI_VDEV_TYPE_STA;
3483 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
3484 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003485 case NL80211_IFTYPE_UNSPECIFIED:
3486 case NL80211_IFTYPE_STATION:
3487 arvif->vdev_type = WMI_VDEV_TYPE_STA;
3488 if (vif->p2p)
3489 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
3490 break;
3491 case NL80211_IFTYPE_ADHOC:
3492 arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
3493 break;
3494 case NL80211_IFTYPE_AP:
3495 arvif->vdev_type = WMI_VDEV_TYPE_AP;
3496
3497 if (vif->p2p)
3498 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
3499 break;
3500 case NL80211_IFTYPE_MONITOR:
3501 arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
3502 break;
3503 default:
3504 WARN_ON(1);
3505 break;
3506 }
3507
Michal Kazior64badcb2014-09-18 11:18:02 +03003508 /* Some firmware revisions don't wait for beacon tx completion before
3509 * sending another SWBA event. This could lead to hardware using old
3510 * (freed) beacon data in some cases, e.g. tx credit starvation
3511 * combined with missed TBTT. This is very very rare.
3512 *
3513 * On non-IOMMU-enabled hosts this could be a possible security issue
3514 * because hw could beacon some random data on the air. On
3515 * IOMMU-enabled hosts DMAR faults would occur in most cases and target
3516 * device would crash.
3517 *
3518 * Since there are no beacon tx completions (implicit nor explicit)
3519 * propagated to host the only workaround for this is to allocate a
3520 * DMA-coherent buffer for a lifetime of a vif and use it for all
3521 * beacon tx commands. Worst case for this approach is some beacons may
3522 * become corrupted, e.g. have garbled IEs or out-of-date TIM bitmap.
3523 */
3524 if (vif->type == NL80211_IFTYPE_ADHOC ||
3525 vif->type == NL80211_IFTYPE_AP) {
3526 arvif->beacon_buf = dma_zalloc_coherent(ar->dev,
3527 IEEE80211_MAX_FRAME_LEN,
3528 &arvif->beacon_paddr,
Rajkumar Manoharan82d7aba2014-10-10 17:38:27 +05303529 GFP_ATOMIC);
Michal Kazior64badcb2014-09-18 11:18:02 +03003530 if (!arvif->beacon_buf) {
3531 ret = -ENOMEM;
3532 ath10k_warn(ar, "failed to allocate beacon buffer: %d\n",
3533 ret);
3534 goto err;
3535 }
3536 }
3537
3538 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d bcnmode %s\n",
3539 arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
3540 arvif->beacon_buf ? "single-buf" : "per-skb");
Kalle Valo5e3dd152013-06-12 20:52:10 +03003541
3542 ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
3543 arvif->vdev_subtype, vif->addr);
3544 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003545 ath10k_warn(ar, "failed to create WMI vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003546 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003547 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003548 }
3549
Ben Greear16c11172014-09-23 14:17:16 -07003550 ar->free_vdev_map &= ~(1LL << arvif->vdev_id);
Michal Kazior05791192013-10-16 15:44:45 +03003551 list_add(&arvif->list, &ar->arvifs);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003552
Michal Kazior46725b152015-01-28 09:57:49 +02003553 /* It makes no sense to have firmware do keepalives. mac80211 already
3554 * takes care of this with idle connection polling.
3555 */
3556 ret = ath10k_mac_vif_disable_keepalive(arvif);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003557 if (ret) {
Michal Kazior46725b152015-01-28 09:57:49 +02003558 ath10k_warn(ar, "failed to disable keepalive on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003559 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003560 goto err_vdev_delete;
3561 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003562
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +02003563 arvif->def_wep_key_idx = -1;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003564
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003565 vdev_param = ar->wmi.vdev_param->tx_encap_type;
3566 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003567 ATH10K_HW_TXRX_NATIVE_WIFI);
Bartosz Markowskiebc9abd2013-10-15 09:26:20 +02003568 /* 10.X firmware does not support this VDEV parameter. Do not warn */
Michal Kazior9dad14a2013-10-16 15:44:45 +03003569 if (ret && ret != -EOPNOTSUPP) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003570 ath10k_warn(ar, "failed to set vdev %i TX encapsulation: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003571 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003572 goto err_vdev_delete;
3573 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003574
Ben Greear5572a952014-11-24 16:22:10 +02003575 if (ar->cfg_tx_chainmask) {
3576 u16 nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
3577
3578 vdev_param = ar->wmi.vdev_param->nss;
3579 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3580 nss);
3581 if (ret) {
3582 ath10k_warn(ar, "failed to set vdev %i chainmask 0x%x, nss %i: %d\n",
3583 arvif->vdev_id, ar->cfg_tx_chainmask, nss,
3584 ret);
3585 goto err_vdev_delete;
3586 }
3587 }
3588
Kalle Valo5e3dd152013-06-12 20:52:10 +03003589 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3590 ret = ath10k_peer_create(ar, arvif->vdev_id, vif->addr);
3591 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003592 ath10k_warn(ar, "failed to create vdev %i peer for AP: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003593 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003594 goto err_vdev_delete;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003595 }
Marek Puzyniakcdf07402013-12-30 09:07:51 +01003596
Kalle Valo5a13e762014-01-20 11:01:46 +02003597 ret = ath10k_mac_set_kickout(arvif);
3598 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003599 ath10k_warn(ar, "failed to set vdev %i kickout parameters: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003600 arvif->vdev_id, ret);
Kalle Valo5a13e762014-01-20 11:01:46 +02003601 goto err_peer_delete;
3602 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003603 }
3604
3605 if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
3606 param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
3607 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
3608 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3609 param, value);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003610 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003611 ath10k_warn(ar, "failed to set vdev %i RX wake policy: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003612 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003613 goto err_peer_delete;
3614 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003615
Michal Kazior9f9b5742014-12-12 12:41:36 +01003616 ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003617 if (ret) {
Michal Kazior9f9b5742014-12-12 12:41:36 +01003618 ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003619 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003620 goto err_peer_delete;
3621 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003622
Michal Kazior9f9b5742014-12-12 12:41:36 +01003623 ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003624 if (ret) {
Michal Kazior9f9b5742014-12-12 12:41:36 +01003625 ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003626 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003627 goto err_peer_delete;
3628 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003629 }
3630
Michal Kazior424121c2013-07-22 14:13:31 +02003631 ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003632 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003633 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
Michal Kazior679c54a2013-07-05 16:15:04 +03003634 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003635 goto err_peer_delete;
3636 }
Michal Kazior679c54a2013-07-05 16:15:04 +03003637
Michal Kazior424121c2013-07-22 14:13:31 +02003638 ret = ath10k_mac_set_frag(arvif, ar->hw->wiphy->frag_threshold);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003639 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003640 ath10k_warn(ar, "failed to set frag threshold for vdev %d: %d\n",
Michal Kazior679c54a2013-07-05 16:15:04 +03003641 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003642 goto err_peer_delete;
3643 }
Michal Kazior679c54a2013-07-05 16:15:04 +03003644
Michal Kazior7d9d5582014-10-21 10:40:15 +03003645 arvif->txpower = vif->bss_conf.txpower;
3646 ret = ath10k_mac_txpower_recalc(ar);
3647 if (ret) {
3648 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
3649 goto err_peer_delete;
3650 }
3651
Kalle Valo5e3dd152013-06-12 20:52:10 +03003652 mutex_unlock(&ar->conf_mutex);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003653 return 0;
3654
3655err_peer_delete:
3656 if (arvif->vdev_type == WMI_VDEV_TYPE_AP)
3657 ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
3658
3659err_vdev_delete:
3660 ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
Ben Greear16c11172014-09-23 14:17:16 -07003661 ar->free_vdev_map |= 1LL << arvif->vdev_id;
Michal Kazior05791192013-10-16 15:44:45 +03003662 list_del(&arvif->list);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003663
3664err:
Michal Kazior64badcb2014-09-18 11:18:02 +03003665 if (arvif->beacon_buf) {
3666 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
3667 arvif->beacon_buf, arvif->beacon_paddr);
3668 arvif->beacon_buf = NULL;
3669 }
3670
Michal Kazior9dad14a2013-10-16 15:44:45 +03003671 mutex_unlock(&ar->conf_mutex);
3672
Kalle Valo5e3dd152013-06-12 20:52:10 +03003673 return ret;
3674}
3675
3676static void ath10k_remove_interface(struct ieee80211_hw *hw,
3677 struct ieee80211_vif *vif)
3678{
3679 struct ath10k *ar = hw->priv;
3680 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3681 int ret;
3682
Michal Kazior81a9a172015-03-05 16:02:17 +02003683 cancel_work_sync(&arvif->ap_csa_work);
Michal Kaziorcc9904e2015-03-10 16:22:01 +02003684 cancel_delayed_work_sync(&arvif->connection_loss_work);
Michal Kazior81a9a172015-03-05 16:02:17 +02003685
Sujith Manoharan5d011f52014-11-25 11:47:00 +05303686 mutex_lock(&ar->conf_mutex);
3687
Michal Kaziored543882013-09-13 14:16:56 +02003688 spin_lock_bh(&ar->data_lock);
Michal Kazior64badcb2014-09-18 11:18:02 +03003689 ath10k_mac_vif_beacon_cleanup(arvif);
Michal Kaziored543882013-09-13 14:16:56 +02003690 spin_unlock_bh(&ar->data_lock);
3691
Simon Wunderlich855aed12014-08-02 09:12:54 +03003692 ret = ath10k_spectral_vif_stop(arvif);
3693 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003694 ath10k_warn(ar, "failed to stop spectral for vdev %i: %d\n",
Simon Wunderlich855aed12014-08-02 09:12:54 +03003695 arvif->vdev_id, ret);
3696
Ben Greear16c11172014-09-23 14:17:16 -07003697 ar->free_vdev_map |= 1LL << arvif->vdev_id;
Michal Kazior05791192013-10-16 15:44:45 +03003698 list_del(&arvif->list);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003699
3700 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
Michal Kazior2c512052015-02-15 16:50:40 +02003701 ret = ath10k_wmi_peer_delete(arvif->ar, arvif->vdev_id,
3702 vif->addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003703 if (ret)
Michal Kazior2c512052015-02-15 16:50:40 +02003704 ath10k_warn(ar, "failed to submit AP self-peer removal on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003705 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003706
3707 kfree(arvif->u.ap.noa_data);
3708 }
3709
Michal Kazior7aa7a722014-08-25 12:09:38 +02003710 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i delete (remove interface)\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03003711 arvif->vdev_id);
3712
Kalle Valo5e3dd152013-06-12 20:52:10 +03003713 ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
3714 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003715 ath10k_warn(ar, "failed to delete WMI vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003716 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003717
Michal Kazior2c512052015-02-15 16:50:40 +02003718 /* Some firmware revisions don't notify host about self-peer removal
3719 * until after associated vdev is deleted.
3720 */
3721 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3722 ret = ath10k_wait_for_peer_deleted(ar, arvif->vdev_id,
3723 vif->addr);
3724 if (ret)
3725 ath10k_warn(ar, "failed to remove AP self-peer on vdev %i: %d\n",
3726 arvif->vdev_id, ret);
3727
3728 spin_lock_bh(&ar->data_lock);
3729 ar->num_peers--;
3730 spin_unlock_bh(&ar->data_lock);
3731 }
3732
Kalle Valo5e3dd152013-06-12 20:52:10 +03003733 ath10k_peer_cleanup(ar, arvif->vdev_id);
3734
3735 mutex_unlock(&ar->conf_mutex);
3736}
3737
3738/*
3739 * FIXME: Has to be verified.
3740 */
3741#define SUPPORTED_FILTERS \
3742 (FIF_PROMISC_IN_BSS | \
3743 FIF_ALLMULTI | \
3744 FIF_CONTROL | \
3745 FIF_PSPOLL | \
3746 FIF_OTHER_BSS | \
3747 FIF_BCN_PRBRESP_PROMISC | \
3748 FIF_PROBE_REQ | \
3749 FIF_FCSFAIL)
3750
3751static void ath10k_configure_filter(struct ieee80211_hw *hw,
3752 unsigned int changed_flags,
3753 unsigned int *total_flags,
3754 u64 multicast)
3755{
3756 struct ath10k *ar = hw->priv;
3757 int ret;
3758
3759 mutex_lock(&ar->conf_mutex);
3760
3761 changed_flags &= SUPPORTED_FILTERS;
3762 *total_flags &= SUPPORTED_FILTERS;
3763 ar->filter_flags = *total_flags;
3764
Michal Kazior19337472014-08-28 12:58:16 +02003765 ret = ath10k_monitor_recalc(ar);
3766 if (ret)
3767 ath10k_warn(ar, "failed to recalc montior: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003768
3769 mutex_unlock(&ar->conf_mutex);
3770}
3771
3772static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
3773 struct ieee80211_vif *vif,
3774 struct ieee80211_bss_conf *info,
3775 u32 changed)
3776{
3777 struct ath10k *ar = hw->priv;
3778 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3779 int ret = 0;
Kalle Valoaf762c02014-09-14 12:50:17 +03003780 u32 vdev_param, pdev_param, slottime, preamble;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003781
3782 mutex_lock(&ar->conf_mutex);
3783
3784 if (changed & BSS_CHANGED_IBSS)
3785 ath10k_control_ibss(arvif, info, vif->addr);
3786
3787 if (changed & BSS_CHANGED_BEACON_INT) {
3788 arvif->beacon_interval = info->beacon_int;
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003789 vdev_param = ar->wmi.vdev_param->beacon_interval;
3790 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003791 arvif->beacon_interval);
Michal Kazior7aa7a722014-08-25 12:09:38 +02003792 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003793 "mac vdev %d beacon_interval %d\n",
3794 arvif->vdev_id, arvif->beacon_interval);
3795
Kalle Valo5e3dd152013-06-12 20:52:10 +03003796 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003797 ath10k_warn(ar, "failed to set beacon interval for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003798 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003799 }
3800
3801 if (changed & BSS_CHANGED_BEACON) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003802 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003803 "vdev %d set beacon tx mode to staggered\n",
3804 arvif->vdev_id);
3805
Bartosz Markowski226a3392013-09-26 17:47:16 +02003806 pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
3807 ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003808 WMI_BEACON_STAGGERED_MODE);
3809 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003810 ath10k_warn(ar, "failed to set beacon mode for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003811 arvif->vdev_id, ret);
Michal Kaziorfbb8f1b2015-01-13 16:30:12 +02003812
3813 ret = ath10k_mac_setup_bcn_tmpl(arvif);
3814 if (ret)
3815 ath10k_warn(ar, "failed to update beacon template: %d\n",
3816 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003817 }
3818
Michal Kaziorfbb8f1b2015-01-13 16:30:12 +02003819 if (changed & BSS_CHANGED_AP_PROBE_RESP) {
3820 ret = ath10k_mac_setup_prb_tmpl(arvif);
3821 if (ret)
3822 ath10k_warn(ar, "failed to setup probe resp template on vdev %i: %d\n",
3823 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003824 }
3825
Michal Kaziorba2479f2015-01-24 12:14:51 +02003826 if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03003827 arvif->dtim_period = info->dtim_period;
3828
Michal Kazior7aa7a722014-08-25 12:09:38 +02003829 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003830 "mac vdev %d dtim_period %d\n",
3831 arvif->vdev_id, arvif->dtim_period);
3832
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003833 vdev_param = ar->wmi.vdev_param->dtim_period;
3834 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003835 arvif->dtim_period);
3836 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003837 ath10k_warn(ar, "failed to set dtim period for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003838 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003839 }
3840
3841 if (changed & BSS_CHANGED_SSID &&
3842 vif->type == NL80211_IFTYPE_AP) {
3843 arvif->u.ap.ssid_len = info->ssid_len;
3844 if (info->ssid_len)
3845 memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
3846 arvif->u.ap.hidden_ssid = info->hidden_ssid;
3847 }
3848
Michal Kazior077efc82014-10-21 10:10:29 +03003849 if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
3850 ether_addr_copy(arvif->bssid, info->bssid);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003851
3852 if (changed & BSS_CHANGED_BEACON_ENABLED)
3853 ath10k_control_beaconing(arvif, info);
3854
3855 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02003856 arvif->use_cts_prot = info->use_cts_prot;
Michal Kazior7aa7a722014-08-25 12:09:38 +02003857 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d cts_prot %d\n",
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02003858 arvif->vdev_id, info->use_cts_prot);
Kalle Valo60c3daa2013-09-08 17:56:07 +03003859
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02003860 ret = ath10k_recalc_rtscts_prot(arvif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003861 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003862 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003863 arvif->vdev_id, ret);
Michal Kaziora87fd4b2015-03-02 11:21:17 +01003864
3865 vdev_param = ar->wmi.vdev_param->protection_mode;
3866 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3867 info->use_cts_prot ? 1 : 0);
3868 if (ret)
3869 ath10k_warn(ar, "failed to set protection mode %d on vdev %i: %d\n",
3870 info->use_cts_prot, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003871 }
3872
3873 if (changed & BSS_CHANGED_ERP_SLOT) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03003874 if (info->use_short_slot)
3875 slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
3876
3877 else
3878 slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
3879
Michal Kazior7aa7a722014-08-25 12:09:38 +02003880 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03003881 arvif->vdev_id, slottime);
3882
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003883 vdev_param = ar->wmi.vdev_param->slot_time;
3884 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003885 slottime);
3886 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003887 ath10k_warn(ar, "failed to set erp slot for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003888 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003889 }
3890
3891 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03003892 if (info->use_short_preamble)
3893 preamble = WMI_VDEV_PREAMBLE_SHORT;
3894 else
3895 preamble = WMI_VDEV_PREAMBLE_LONG;
3896
Michal Kazior7aa7a722014-08-25 12:09:38 +02003897 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003898 "mac vdev %d preamble %dn",
3899 arvif->vdev_id, preamble);
3900
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003901 vdev_param = ar->wmi.vdev_param->preamble;
3902 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003903 preamble);
3904 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003905 ath10k_warn(ar, "failed to set preamble for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003906 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003907 }
3908
3909 if (changed & BSS_CHANGED_ASSOC) {
Michal Kaziore556f112014-08-28 12:58:17 +02003910 if (info->assoc) {
3911 /* Workaround: Make sure monitor vdev is not running
3912 * when associating to prevent some firmware revisions
3913 * (e.g. 10.1 and 10.2) from crashing.
3914 */
3915 if (ar->monitor_started)
3916 ath10k_monitor_stop(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003917 ath10k_bss_assoc(hw, vif, info);
Michal Kaziore556f112014-08-28 12:58:17 +02003918 ath10k_monitor_recalc(ar);
Michal Kazior077efc82014-10-21 10:10:29 +03003919 } else {
3920 ath10k_bss_disassoc(hw, vif);
Michal Kaziore556f112014-08-28 12:58:17 +02003921 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003922 }
3923
Michal Kazior7d9d5582014-10-21 10:40:15 +03003924 if (changed & BSS_CHANGED_TXPOWER) {
3925 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev_id %i txpower %d\n",
3926 arvif->vdev_id, info->txpower);
3927
3928 arvif->txpower = info->txpower;
3929 ret = ath10k_mac_txpower_recalc(ar);
3930 if (ret)
3931 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
3932 }
3933
Michal Kaziorbf14e652014-12-12 12:41:38 +01003934 if (changed & BSS_CHANGED_PS) {
Michal Kaziorcffb41f2015-02-13 13:30:16 +01003935 arvif->ps = vif->bss_conf.ps;
3936
3937 ret = ath10k_config_ps(ar);
Michal Kaziorbf14e652014-12-12 12:41:38 +01003938 if (ret)
3939 ath10k_warn(ar, "failed to setup ps on vdev %i: %d\n",
3940 arvif->vdev_id, ret);
3941 }
3942
Kalle Valo5e3dd152013-06-12 20:52:10 +03003943 mutex_unlock(&ar->conf_mutex);
3944}
3945
3946static int ath10k_hw_scan(struct ieee80211_hw *hw,
3947 struct ieee80211_vif *vif,
David Spinadelc56ef672014-02-05 15:21:13 +02003948 struct ieee80211_scan_request *hw_req)
Kalle Valo5e3dd152013-06-12 20:52:10 +03003949{
3950 struct ath10k *ar = hw->priv;
3951 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
David Spinadelc56ef672014-02-05 15:21:13 +02003952 struct cfg80211_scan_request *req = &hw_req->req;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003953 struct wmi_start_scan_arg arg;
3954 int ret = 0;
3955 int i;
3956
3957 mutex_lock(&ar->conf_mutex);
3958
3959 spin_lock_bh(&ar->data_lock);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003960 switch (ar->scan.state) {
3961 case ATH10K_SCAN_IDLE:
3962 reinit_completion(&ar->scan.started);
3963 reinit_completion(&ar->scan.completed);
3964 ar->scan.state = ATH10K_SCAN_STARTING;
3965 ar->scan.is_roc = false;
3966 ar->scan.vdev_id = arvif->vdev_id;
3967 ret = 0;
3968 break;
3969 case ATH10K_SCAN_STARTING:
3970 case ATH10K_SCAN_RUNNING:
3971 case ATH10K_SCAN_ABORTING:
Kalle Valo5e3dd152013-06-12 20:52:10 +03003972 ret = -EBUSY;
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003973 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003974 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003975 spin_unlock_bh(&ar->data_lock);
3976
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003977 if (ret)
3978 goto exit;
3979
Kalle Valo5e3dd152013-06-12 20:52:10 +03003980 memset(&arg, 0, sizeof(arg));
3981 ath10k_wmi_start_scan_init(ar, &arg);
3982 arg.vdev_id = arvif->vdev_id;
3983 arg.scan_id = ATH10K_SCAN_ID;
3984
3985 if (!req->no_cck)
3986 arg.scan_ctrl_flags |= WMI_SCAN_ADD_CCK_RATES;
3987
3988 if (req->ie_len) {
3989 arg.ie_len = req->ie_len;
3990 memcpy(arg.ie, req->ie, arg.ie_len);
3991 }
3992
3993 if (req->n_ssids) {
3994 arg.n_ssids = req->n_ssids;
3995 for (i = 0; i < arg.n_ssids; i++) {
3996 arg.ssids[i].len = req->ssids[i].ssid_len;
3997 arg.ssids[i].ssid = req->ssids[i].ssid;
3998 }
Michal Kaziordcd4a562013-07-31 10:55:12 +02003999 } else {
4000 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004001 }
4002
4003 if (req->n_channels) {
4004 arg.n_channels = req->n_channels;
4005 for (i = 0; i < arg.n_channels; i++)
4006 arg.channels[i] = req->channels[i]->center_freq;
4007 }
4008
4009 ret = ath10k_start_scan(ar, &arg);
4010 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004011 ath10k_warn(ar, "failed to start hw scan: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004012 spin_lock_bh(&ar->data_lock);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004013 ar->scan.state = ATH10K_SCAN_IDLE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004014 spin_unlock_bh(&ar->data_lock);
4015 }
4016
4017exit:
4018 mutex_unlock(&ar->conf_mutex);
4019 return ret;
4020}
4021
4022static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
4023 struct ieee80211_vif *vif)
4024{
4025 struct ath10k *ar = hw->priv;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004026
4027 mutex_lock(&ar->conf_mutex);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004028 ath10k_scan_abort(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004029 mutex_unlock(&ar->conf_mutex);
Michal Kazior4eb2e162014-10-28 10:23:09 +01004030
4031 cancel_delayed_work_sync(&ar->scan.timeout);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004032}
4033
Michal Kaziorcfb27d22013-12-02 09:06:36 +01004034static void ath10k_set_key_h_def_keyidx(struct ath10k *ar,
4035 struct ath10k_vif *arvif,
4036 enum set_key_cmd cmd,
4037 struct ieee80211_key_conf *key)
4038{
4039 u32 vdev_param = arvif->ar->wmi.vdev_param->def_keyid;
4040 int ret;
4041
4042 /* 10.1 firmware branch requires default key index to be set to group
4043 * key index after installing it. Otherwise FW/HW Txes corrupted
4044 * frames with multi-vif APs. This is not required for main firmware
4045 * branch (e.g. 636).
4046 *
4047 * FIXME: This has been tested only in AP. It remains unknown if this
4048 * is required for multi-vif STA interfaces on 10.1 */
4049
4050 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
4051 return;
4052
4053 if (key->cipher == WLAN_CIPHER_SUITE_WEP40)
4054 return;
4055
4056 if (key->cipher == WLAN_CIPHER_SUITE_WEP104)
4057 return;
4058
4059 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4060 return;
4061
4062 if (cmd != SET_KEY)
4063 return;
4064
4065 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
4066 key->keyidx);
4067 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004068 ath10k_warn(ar, "failed to set vdev %i group key as default key: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02004069 arvif->vdev_id, ret);
Michal Kaziorcfb27d22013-12-02 09:06:36 +01004070}
4071
Kalle Valo5e3dd152013-06-12 20:52:10 +03004072static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
4073 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
4074 struct ieee80211_key_conf *key)
4075{
4076 struct ath10k *ar = hw->priv;
4077 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4078 struct ath10k_peer *peer;
4079 const u8 *peer_addr;
4080 bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
4081 key->cipher == WLAN_CIPHER_SUITE_WEP104;
4082 int ret = 0;
Michal Kazior370e5672015-02-18 14:02:26 +01004083 u32 flags = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004084
Bartosz Markowskid7131c02015-03-10 14:32:19 +01004085 /* this one needs to be done in software */
4086 if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
4087 return 1;
4088
Kalle Valo5e3dd152013-06-12 20:52:10 +03004089 if (key->keyidx > WMI_MAX_KEY_INDEX)
4090 return -ENOSPC;
4091
4092 mutex_lock(&ar->conf_mutex);
4093
4094 if (sta)
4095 peer_addr = sta->addr;
4096 else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
4097 peer_addr = vif->bss_conf.bssid;
4098 else
4099 peer_addr = vif->addr;
4100
4101 key->hw_key_idx = key->keyidx;
4102
4103 /* the peer should not disappear in mid-way (unless FW goes awry) since
4104 * we already hold conf_mutex. we just make sure its there now. */
4105 spin_lock_bh(&ar->data_lock);
4106 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
4107 spin_unlock_bh(&ar->data_lock);
4108
4109 if (!peer) {
4110 if (cmd == SET_KEY) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004111 ath10k_warn(ar, "failed to install key for non-existent peer %pM\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03004112 peer_addr);
4113 ret = -EOPNOTSUPP;
4114 goto exit;
4115 } else {
4116 /* if the peer doesn't exist there is no key to disable
4117 * anymore */
4118 goto exit;
4119 }
4120 }
4121
Michal Kazior7cc45732015-03-09 14:24:17 +01004122 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4123 flags |= WMI_KEY_PAIRWISE;
4124 else
4125 flags |= WMI_KEY_GROUP;
4126
Kalle Valo5e3dd152013-06-12 20:52:10 +03004127 if (is_wep) {
4128 if (cmd == SET_KEY)
4129 arvif->wep_keys[key->keyidx] = key;
4130 else
4131 arvif->wep_keys[key->keyidx] = NULL;
4132
4133 if (cmd == DISABLE_KEY)
4134 ath10k_clear_vdev_key(arvif, key);
Michal Kazior370e5672015-02-18 14:02:26 +01004135
Michal Kaziorad325cb2015-02-18 14:02:27 +01004136 /* When WEP keys are uploaded it's possible that there are
4137 * stations associated already (e.g. when merging) without any
4138 * keys. Static WEP needs an explicit per-peer key upload.
4139 */
4140 if (vif->type == NL80211_IFTYPE_ADHOC &&
4141 cmd == SET_KEY)
4142 ath10k_mac_vif_update_wep_key(arvif, key);
4143
Michal Kazior370e5672015-02-18 14:02:26 +01004144 /* 802.1x never sets the def_wep_key_idx so each set_key()
4145 * call changes default tx key.
4146 *
4147 * Static WEP sets def_wep_key_idx via .set_default_unicast_key
4148 * after first set_key().
4149 */
4150 if (cmd == SET_KEY && arvif->def_wep_key_idx == -1)
4151 flags |= WMI_KEY_TX_USAGE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004152
Michal Kazior7cc45732015-03-09 14:24:17 +01004153 /* mac80211 uploads static WEP keys as groupwise while fw/hw
4154 * requires pairwise keys for non-self peers, i.e. BSSID in STA
4155 * mode and associated stations in AP/IBSS.
4156 *
4157 * Static WEP keys for peer_addr=vif->addr and 802.1X WEP keys
4158 * work fine when mapped directly from mac80211.
4159 *
4160 * Note: When installing first static WEP groupwise key (which
4161 * should be pairwise) def_wep_key_idx isn't known yet (it's
4162 * equal to -1). Since .set_default_unicast_key is called only
4163 * for static WEP it's used to re-upload the key as pairwise.
4164 */
4165 if (arvif->def_wep_key_idx >= 0 &&
4166 memcmp(peer_addr, arvif->vif->addr, ETH_ALEN)) {
4167 flags &= ~WMI_KEY_GROUP;
4168 flags |= WMI_KEY_PAIRWISE;
4169 }
Michal Kazior370e5672015-02-18 14:02:26 +01004170 }
4171
4172 ret = ath10k_install_key(arvif, key, cmd, peer_addr, flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004173 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004174 ath10k_warn(ar, "failed to install key for vdev %i peer %pM: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02004175 arvif->vdev_id, peer_addr, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004176 goto exit;
4177 }
4178
Michal Kaziorcfb27d22013-12-02 09:06:36 +01004179 ath10k_set_key_h_def_keyidx(ar, arvif, cmd, key);
4180
Kalle Valo5e3dd152013-06-12 20:52:10 +03004181 spin_lock_bh(&ar->data_lock);
4182 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
4183 if (peer && cmd == SET_KEY)
4184 peer->keys[key->keyidx] = key;
4185 else if (peer && cmd == DISABLE_KEY)
4186 peer->keys[key->keyidx] = NULL;
4187 else if (peer == NULL)
4188 /* impossible unless FW goes crazy */
Michal Kazior7aa7a722014-08-25 12:09:38 +02004189 ath10k_warn(ar, "Peer %pM disappeared!\n", peer_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004190 spin_unlock_bh(&ar->data_lock);
4191
4192exit:
4193 mutex_unlock(&ar->conf_mutex);
4194 return ret;
4195}
4196
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +02004197static void ath10k_set_default_unicast_key(struct ieee80211_hw *hw,
4198 struct ieee80211_vif *vif,
4199 int keyidx)
4200{
4201 struct ath10k *ar = hw->priv;
4202 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4203 int ret;
4204
4205 mutex_lock(&arvif->ar->conf_mutex);
4206
4207 if (arvif->ar->state != ATH10K_STATE_ON)
4208 goto unlock;
4209
4210 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
4211 arvif->vdev_id, keyidx);
4212
4213 ret = ath10k_wmi_vdev_set_param(arvif->ar,
4214 arvif->vdev_id,
4215 arvif->ar->wmi.vdev_param->def_keyid,
4216 keyidx);
4217
4218 if (ret) {
4219 ath10k_warn(ar, "failed to update wep key index for vdev %d: %d\n",
4220 arvif->vdev_id,
4221 ret);
4222 goto unlock;
4223 }
4224
4225 arvif->def_wep_key_idx = keyidx;
Michal Kazior370e5672015-02-18 14:02:26 +01004226
4227 ret = ath10k_mac_vif_sta_fix_wep_key(arvif);
4228 if (ret) {
4229 ath10k_warn(ar, "failed to fix sta wep key on vdev %i: %d\n",
4230 arvif->vdev_id, ret);
4231 goto unlock;
4232 }
4233
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +02004234unlock:
4235 mutex_unlock(&arvif->ar->conf_mutex);
4236}
4237
Michal Kazior9797feb2014-02-14 14:49:48 +01004238static void ath10k_sta_rc_update_wk(struct work_struct *wk)
4239{
4240 struct ath10k *ar;
4241 struct ath10k_vif *arvif;
4242 struct ath10k_sta *arsta;
4243 struct ieee80211_sta *sta;
4244 u32 changed, bw, nss, smps;
4245 int err;
4246
4247 arsta = container_of(wk, struct ath10k_sta, update_wk);
4248 sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
4249 arvif = arsta->arvif;
4250 ar = arvif->ar;
4251
4252 spin_lock_bh(&ar->data_lock);
4253
4254 changed = arsta->changed;
4255 arsta->changed = 0;
4256
4257 bw = arsta->bw;
4258 nss = arsta->nss;
4259 smps = arsta->smps;
4260
4261 spin_unlock_bh(&ar->data_lock);
4262
4263 mutex_lock(&ar->conf_mutex);
4264
4265 if (changed & IEEE80211_RC_BW_CHANGED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004266 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM peer bw %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01004267 sta->addr, bw);
4268
4269 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
4270 WMI_PEER_CHAN_WIDTH, bw);
4271 if (err)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004272 ath10k_warn(ar, "failed to update STA %pM peer bw %d: %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01004273 sta->addr, bw, err);
4274 }
4275
4276 if (changed & IEEE80211_RC_NSS_CHANGED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004277 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM nss %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01004278 sta->addr, nss);
4279
4280 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
4281 WMI_PEER_NSS, nss);
4282 if (err)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004283 ath10k_warn(ar, "failed to update STA %pM nss %d: %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01004284 sta->addr, nss, err);
4285 }
4286
4287 if (changed & IEEE80211_RC_SMPS_CHANGED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004288 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM smps %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01004289 sta->addr, smps);
4290
4291 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
4292 WMI_PEER_SMPS_STATE, smps);
4293 if (err)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004294 ath10k_warn(ar, "failed to update STA %pM smps %d: %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01004295 sta->addr, smps, err);
4296 }
4297
Janusz Dziedzic55884c02014-12-17 12:30:02 +02004298 if (changed & IEEE80211_RC_SUPP_RATES_CHANGED ||
4299 changed & IEEE80211_RC_NSS_CHANGED) {
4300 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM supp rates/nss\n",
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02004301 sta->addr);
4302
Michal Kazior590922a2014-10-21 10:10:29 +03004303 err = ath10k_station_assoc(ar, arvif->vif, sta, true);
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02004304 if (err)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004305 ath10k_warn(ar, "failed to reassociate station: %pM\n",
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02004306 sta->addr);
4307 }
4308
Michal Kazior9797feb2014-02-14 14:49:48 +01004309 mutex_unlock(&ar->conf_mutex);
4310}
4311
Michal Kaziorcfd10612014-11-25 15:16:05 +01004312static int ath10k_mac_inc_num_stations(struct ath10k_vif *arvif)
4313{
4314 struct ath10k *ar = arvif->ar;
4315
4316 lockdep_assert_held(&ar->conf_mutex);
4317
4318 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
4319 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
4320 return 0;
4321
4322 if (ar->num_stations >= ar->max_num_stations)
4323 return -ENOBUFS;
4324
4325 ar->num_stations++;
4326
4327 return 0;
4328}
4329
4330static void ath10k_mac_dec_num_stations(struct ath10k_vif *arvif)
4331{
4332 struct ath10k *ar = arvif->ar;
4333
4334 lockdep_assert_held(&ar->conf_mutex);
4335
4336 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
4337 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
4338 return;
4339
4340 ar->num_stations--;
4341}
4342
Kalle Valo5e3dd152013-06-12 20:52:10 +03004343static int ath10k_sta_state(struct ieee80211_hw *hw,
4344 struct ieee80211_vif *vif,
4345 struct ieee80211_sta *sta,
4346 enum ieee80211_sta_state old_state,
4347 enum ieee80211_sta_state new_state)
4348{
4349 struct ath10k *ar = hw->priv;
4350 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Michal Kazior9797feb2014-02-14 14:49:48 +01004351 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004352 int ret = 0;
4353
Michal Kazior76f90022014-02-25 09:29:57 +02004354 if (old_state == IEEE80211_STA_NOTEXIST &&
4355 new_state == IEEE80211_STA_NONE) {
4356 memset(arsta, 0, sizeof(*arsta));
4357 arsta->arvif = arvif;
4358 INIT_WORK(&arsta->update_wk, ath10k_sta_rc_update_wk);
4359 }
4360
Michal Kazior9797feb2014-02-14 14:49:48 +01004361 /* cancel must be done outside the mutex to avoid deadlock */
4362 if ((old_state == IEEE80211_STA_NONE &&
4363 new_state == IEEE80211_STA_NOTEXIST))
4364 cancel_work_sync(&arsta->update_wk);
4365
Kalle Valo5e3dd152013-06-12 20:52:10 +03004366 mutex_lock(&ar->conf_mutex);
4367
4368 if (old_state == IEEE80211_STA_NOTEXIST &&
Michal Kazior077efc82014-10-21 10:10:29 +03004369 new_state == IEEE80211_STA_NONE) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03004370 /*
4371 * New station addition.
4372 */
Michal Kaziorcfd10612014-11-25 15:16:05 +01004373 ath10k_dbg(ar, ATH10K_DBG_MAC,
4374 "mac vdev %d peer create %pM (new sta) sta %d / %d peer %d / %d\n",
4375 arvif->vdev_id, sta->addr,
4376 ar->num_stations + 1, ar->max_num_stations,
4377 ar->num_peers + 1, ar->max_num_peers);
Bartosz Markowski0e759f32014-01-02 14:38:33 +01004378
Michal Kaziorcfd10612014-11-25 15:16:05 +01004379 ret = ath10k_mac_inc_num_stations(arvif);
4380 if (ret) {
4381 ath10k_warn(ar, "refusing to associate station: too many connected already (%d)\n",
4382 ar->max_num_stations);
Bartosz Markowski0e759f32014-01-02 14:38:33 +01004383 goto exit;
4384 }
4385
Kalle Valo5e3dd152013-06-12 20:52:10 +03004386 ret = ath10k_peer_create(ar, arvif->vdev_id, sta->addr);
Michal Kaziora52c0282014-11-25 15:16:03 +01004387 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004388 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 -08004389 sta->addr, arvif->vdev_id, ret);
Michal Kaziorcfd10612014-11-25 15:16:05 +01004390 ath10k_mac_dec_num_stations(arvif);
Michal Kaziora52c0282014-11-25 15:16:03 +01004391 goto exit;
4392 }
Michal Kazior077efc82014-10-21 10:10:29 +03004393
4394 if (vif->type == NL80211_IFTYPE_STATION) {
4395 WARN_ON(arvif->is_started);
4396
4397 ret = ath10k_vdev_start(arvif);
4398 if (ret) {
4399 ath10k_warn(ar, "failed to start vdev %i: %d\n",
4400 arvif->vdev_id, ret);
4401 WARN_ON(ath10k_peer_delete(ar, arvif->vdev_id,
4402 sta->addr));
Michal Kaziorcfd10612014-11-25 15:16:05 +01004403 ath10k_mac_dec_num_stations(arvif);
Michal Kazior077efc82014-10-21 10:10:29 +03004404 goto exit;
4405 }
4406
4407 arvif->is_started = true;
4408 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03004409 } else if ((old_state == IEEE80211_STA_NONE &&
4410 new_state == IEEE80211_STA_NOTEXIST)) {
4411 /*
4412 * Existing station deletion.
4413 */
Michal Kazior7aa7a722014-08-25 12:09:38 +02004414 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03004415 "mac vdev %d peer delete %pM (sta gone)\n",
4416 arvif->vdev_id, sta->addr);
Michal Kazior077efc82014-10-21 10:10:29 +03004417
4418 if (vif->type == NL80211_IFTYPE_STATION) {
4419 WARN_ON(!arvif->is_started);
4420
4421 ret = ath10k_vdev_stop(arvif);
4422 if (ret)
4423 ath10k_warn(ar, "failed to stop vdev %i: %d\n",
4424 arvif->vdev_id, ret);
4425
4426 arvif->is_started = false;
4427 }
4428
Kalle Valo5e3dd152013-06-12 20:52:10 +03004429 ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
4430 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004431 ath10k_warn(ar, "failed to delete peer %pM for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02004432 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004433
Michal Kaziorcfd10612014-11-25 15:16:05 +01004434 ath10k_mac_dec_num_stations(arvif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004435 } else if (old_state == IEEE80211_STA_AUTH &&
4436 new_state == IEEE80211_STA_ASSOC &&
4437 (vif->type == NL80211_IFTYPE_AP ||
4438 vif->type == NL80211_IFTYPE_ADHOC)) {
4439 /*
4440 * New association.
4441 */
Michal Kazior7aa7a722014-08-25 12:09:38 +02004442 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM associated\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03004443 sta->addr);
4444
Michal Kazior590922a2014-10-21 10:10:29 +03004445 ret = ath10k_station_assoc(ar, vif, sta, false);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004446 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004447 ath10k_warn(ar, "failed to associate station %pM for vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02004448 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004449 } else if (old_state == IEEE80211_STA_ASSOC &&
4450 new_state == IEEE80211_STA_AUTH &&
4451 (vif->type == NL80211_IFTYPE_AP ||
4452 vif->type == NL80211_IFTYPE_ADHOC)) {
4453 /*
4454 * Disassociation.
4455 */
Michal Kazior7aa7a722014-08-25 12:09:38 +02004456 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM disassociated\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03004457 sta->addr);
4458
Michal Kazior590922a2014-10-21 10:10:29 +03004459 ret = ath10k_station_disassoc(ar, vif, sta);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004460 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004461 ath10k_warn(ar, "failed to disassociate station: %pM vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02004462 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004463 }
Bartosz Markowski0e759f32014-01-02 14:38:33 +01004464exit:
Kalle Valo5e3dd152013-06-12 20:52:10 +03004465 mutex_unlock(&ar->conf_mutex);
4466 return ret;
4467}
4468
4469static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
Kalle Valo5b07e072014-09-14 12:50:06 +03004470 u16 ac, bool enable)
Kalle Valo5e3dd152013-06-12 20:52:10 +03004471{
4472 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Michal Kaziorb0e56152015-01-24 12:14:52 +02004473 struct wmi_sta_uapsd_auto_trig_arg arg = {};
4474 u32 prio = 0, acc = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004475 u32 value = 0;
4476 int ret = 0;
4477
Michal Kazior548db542013-07-05 16:15:15 +03004478 lockdep_assert_held(&ar->conf_mutex);
4479
Kalle Valo5e3dd152013-06-12 20:52:10 +03004480 if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
4481 return 0;
4482
4483 switch (ac) {
4484 case IEEE80211_AC_VO:
4485 value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
4486 WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
Michal Kaziorb0e56152015-01-24 12:14:52 +02004487 prio = 7;
4488 acc = 3;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004489 break;
4490 case IEEE80211_AC_VI:
4491 value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
4492 WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
Michal Kaziorb0e56152015-01-24 12:14:52 +02004493 prio = 5;
4494 acc = 2;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004495 break;
4496 case IEEE80211_AC_BE:
4497 value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
4498 WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
Michal Kaziorb0e56152015-01-24 12:14:52 +02004499 prio = 2;
4500 acc = 1;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004501 break;
4502 case IEEE80211_AC_BK:
4503 value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
4504 WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
Michal Kaziorb0e56152015-01-24 12:14:52 +02004505 prio = 0;
4506 acc = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004507 break;
4508 }
4509
4510 if (enable)
4511 arvif->u.sta.uapsd |= value;
4512 else
4513 arvif->u.sta.uapsd &= ~value;
4514
4515 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4516 WMI_STA_PS_PARAM_UAPSD,
4517 arvif->u.sta.uapsd);
4518 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004519 ath10k_warn(ar, "failed to set uapsd params: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004520 goto exit;
4521 }
4522
4523 if (arvif->u.sta.uapsd)
4524 value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
4525 else
4526 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
4527
4528 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4529 WMI_STA_PS_PARAM_RX_WAKE_POLICY,
4530 value);
4531 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004532 ath10k_warn(ar, "failed to set rx wake param: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004533
Michal Kazior9f9b5742014-12-12 12:41:36 +01004534 ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
4535 if (ret) {
4536 ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
4537 arvif->vdev_id, ret);
4538 return ret;
4539 }
4540
4541 ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
4542 if (ret) {
4543 ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
4544 arvif->vdev_id, ret);
4545 return ret;
4546 }
4547
Michal Kaziorb0e56152015-01-24 12:14:52 +02004548 if (test_bit(WMI_SERVICE_STA_UAPSD_BASIC_AUTO_TRIG, ar->wmi.svc_map) ||
4549 test_bit(WMI_SERVICE_STA_UAPSD_VAR_AUTO_TRIG, ar->wmi.svc_map)) {
4550 /* Only userspace can make an educated decision when to send
4551 * trigger frame. The following effectively disables u-UAPSD
4552 * autotrigger in firmware (which is enabled by default
4553 * provided the autotrigger service is available).
4554 */
4555
4556 arg.wmm_ac = acc;
4557 arg.user_priority = prio;
4558 arg.service_interval = 0;
4559 arg.suspend_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
4560 arg.delay_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
4561
4562 ret = ath10k_wmi_vdev_sta_uapsd(ar, arvif->vdev_id,
4563 arvif->bssid, &arg, 1);
4564 if (ret) {
4565 ath10k_warn(ar, "failed to set uapsd auto trigger %d\n",
4566 ret);
4567 return ret;
4568 }
4569 }
4570
Kalle Valo5e3dd152013-06-12 20:52:10 +03004571exit:
4572 return ret;
4573}
4574
4575static int ath10k_conf_tx(struct ieee80211_hw *hw,
4576 struct ieee80211_vif *vif, u16 ac,
4577 const struct ieee80211_tx_queue_params *params)
4578{
4579 struct ath10k *ar = hw->priv;
Michal Kazior5e752e42015-01-19 09:53:41 +01004580 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004581 struct wmi_wmm_params_arg *p = NULL;
4582 int ret;
4583
4584 mutex_lock(&ar->conf_mutex);
4585
4586 switch (ac) {
4587 case IEEE80211_AC_VO:
Michal Kazior5e752e42015-01-19 09:53:41 +01004588 p = &arvif->wmm_params.ac_vo;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004589 break;
4590 case IEEE80211_AC_VI:
Michal Kazior5e752e42015-01-19 09:53:41 +01004591 p = &arvif->wmm_params.ac_vi;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004592 break;
4593 case IEEE80211_AC_BE:
Michal Kazior5e752e42015-01-19 09:53:41 +01004594 p = &arvif->wmm_params.ac_be;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004595 break;
4596 case IEEE80211_AC_BK:
Michal Kazior5e752e42015-01-19 09:53:41 +01004597 p = &arvif->wmm_params.ac_bk;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004598 break;
4599 }
4600
4601 if (WARN_ON(!p)) {
4602 ret = -EINVAL;
4603 goto exit;
4604 }
4605
4606 p->cwmin = params->cw_min;
4607 p->cwmax = params->cw_max;
4608 p->aifs = params->aifs;
4609
4610 /*
4611 * The channel time duration programmed in the HW is in absolute
4612 * microseconds, while mac80211 gives the txop in units of
4613 * 32 microseconds.
4614 */
4615 p->txop = params->txop * 32;
4616
Michal Kazior7fc979a2015-01-28 09:57:28 +02004617 if (ar->wmi.ops->gen_vdev_wmm_conf) {
4618 ret = ath10k_wmi_vdev_wmm_conf(ar, arvif->vdev_id,
4619 &arvif->wmm_params);
4620 if (ret) {
4621 ath10k_warn(ar, "failed to set vdev wmm params on vdev %i: %d\n",
4622 arvif->vdev_id, ret);
4623 goto exit;
4624 }
4625 } else {
4626 /* This won't work well with multi-interface cases but it's
4627 * better than nothing.
4628 */
4629 ret = ath10k_wmi_pdev_set_wmm_params(ar, &arvif->wmm_params);
4630 if (ret) {
4631 ath10k_warn(ar, "failed to set wmm params: %d\n", ret);
4632 goto exit;
4633 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03004634 }
4635
4636 ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
4637 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004638 ath10k_warn(ar, "failed to set sta uapsd: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004639
4640exit:
4641 mutex_unlock(&ar->conf_mutex);
4642 return ret;
4643}
4644
4645#define ATH10K_ROC_TIMEOUT_HZ (2*HZ)
4646
4647static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
4648 struct ieee80211_vif *vif,
4649 struct ieee80211_channel *chan,
4650 int duration,
4651 enum ieee80211_roc_type type)
4652{
4653 struct ath10k *ar = hw->priv;
4654 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4655 struct wmi_start_scan_arg arg;
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004656 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004657
4658 mutex_lock(&ar->conf_mutex);
4659
4660 spin_lock_bh(&ar->data_lock);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004661 switch (ar->scan.state) {
4662 case ATH10K_SCAN_IDLE:
4663 reinit_completion(&ar->scan.started);
4664 reinit_completion(&ar->scan.completed);
4665 reinit_completion(&ar->scan.on_channel);
4666 ar->scan.state = ATH10K_SCAN_STARTING;
4667 ar->scan.is_roc = true;
4668 ar->scan.vdev_id = arvif->vdev_id;
4669 ar->scan.roc_freq = chan->center_freq;
4670 ret = 0;
4671 break;
4672 case ATH10K_SCAN_STARTING:
4673 case ATH10K_SCAN_RUNNING:
4674 case ATH10K_SCAN_ABORTING:
Kalle Valo5e3dd152013-06-12 20:52:10 +03004675 ret = -EBUSY;
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004676 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004677 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03004678 spin_unlock_bh(&ar->data_lock);
4679
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004680 if (ret)
4681 goto exit;
4682
Michal Kaziordcca0bd2014-11-24 14:58:32 +01004683 duration = max(duration, WMI_SCAN_CHAN_MIN_TIME_MSEC);
4684
Kalle Valo5e3dd152013-06-12 20:52:10 +03004685 memset(&arg, 0, sizeof(arg));
4686 ath10k_wmi_start_scan_init(ar, &arg);
4687 arg.vdev_id = arvif->vdev_id;
4688 arg.scan_id = ATH10K_SCAN_ID;
4689 arg.n_channels = 1;
4690 arg.channels[0] = chan->center_freq;
4691 arg.dwell_time_active = duration;
4692 arg.dwell_time_passive = duration;
4693 arg.max_scan_time = 2 * duration;
4694 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
4695 arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
4696
4697 ret = ath10k_start_scan(ar, &arg);
4698 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004699 ath10k_warn(ar, "failed to start roc scan: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004700 spin_lock_bh(&ar->data_lock);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004701 ar->scan.state = ATH10K_SCAN_IDLE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004702 spin_unlock_bh(&ar->data_lock);
4703 goto exit;
4704 }
4705
4706 ret = wait_for_completion_timeout(&ar->scan.on_channel, 3*HZ);
4707 if (ret == 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004708 ath10k_warn(ar, "failed to switch to channel for roc scan\n");
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004709
4710 ret = ath10k_scan_stop(ar);
4711 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004712 ath10k_warn(ar, "failed to stop scan: %d\n", ret);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004713
Kalle Valo5e3dd152013-06-12 20:52:10 +03004714 ret = -ETIMEDOUT;
4715 goto exit;
4716 }
4717
4718 ret = 0;
4719exit:
4720 mutex_unlock(&ar->conf_mutex);
4721 return ret;
4722}
4723
4724static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw)
4725{
4726 struct ath10k *ar = hw->priv;
4727
4728 mutex_lock(&ar->conf_mutex);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004729 ath10k_scan_abort(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004730 mutex_unlock(&ar->conf_mutex);
4731
Michal Kazior4eb2e162014-10-28 10:23:09 +01004732 cancel_delayed_work_sync(&ar->scan.timeout);
4733
Kalle Valo5e3dd152013-06-12 20:52:10 +03004734 return 0;
4735}
4736
4737/*
4738 * Both RTS and Fragmentation threshold are interface-specific
4739 * in ath10k, but device-specific in mac80211.
4740 */
Kalle Valo5e3dd152013-06-12 20:52:10 +03004741
4742static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
4743{
Kalle Valo5e3dd152013-06-12 20:52:10 +03004744 struct ath10k *ar = hw->priv;
Michal Kaziorad088bf2013-10-16 15:44:46 +03004745 struct ath10k_vif *arvif;
4746 int ret = 0;
Michal Kazior548db542013-07-05 16:15:15 +03004747
Michal Kaziorad088bf2013-10-16 15:44:46 +03004748 mutex_lock(&ar->conf_mutex);
4749 list_for_each_entry(arvif, &ar->arvifs, list) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004750 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
Michal Kaziorad088bf2013-10-16 15:44:46 +03004751 arvif->vdev_id, value);
Kalle Valo60c3daa2013-09-08 17:56:07 +03004752
Michal Kaziorad088bf2013-10-16 15:44:46 +03004753 ret = ath10k_mac_set_rts(arvif, value);
4754 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004755 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
Michal Kaziorad088bf2013-10-16 15:44:46 +03004756 arvif->vdev_id, ret);
4757 break;
4758 }
4759 }
4760 mutex_unlock(&ar->conf_mutex);
4761
4762 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004763}
4764
Emmanuel Grumbach77be2c52014-03-27 11:30:29 +02004765static void ath10k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4766 u32 queues, bool drop)
Kalle Valo5e3dd152013-06-12 20:52:10 +03004767{
4768 struct ath10k *ar = hw->priv;
Michal Kazioraffd3212013-07-16 09:54:35 +02004769 bool skip;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004770 int ret;
4771
4772 /* mac80211 doesn't care if we really xmit queued frames or not
4773 * we'll collect those frames either way if we stop/delete vdevs */
4774 if (drop)
4775 return;
4776
Michal Kazior548db542013-07-05 16:15:15 +03004777 mutex_lock(&ar->conf_mutex);
4778
Michal Kazioraffd3212013-07-16 09:54:35 +02004779 if (ar->state == ATH10K_STATE_WEDGED)
4780 goto skip;
4781
Michal Kazioredb82362013-07-05 16:15:14 +03004782 ret = wait_event_timeout(ar->htt.empty_tx_wq, ({
Kalle Valo5e3dd152013-06-12 20:52:10 +03004783 bool empty;
Michal Kazioraffd3212013-07-16 09:54:35 +02004784
Michal Kazioredb82362013-07-05 16:15:14 +03004785 spin_lock_bh(&ar->htt.tx_lock);
Michal Kazior0945baf2013-09-18 14:43:18 +02004786 empty = (ar->htt.num_pending_tx == 0);
Michal Kazioredb82362013-07-05 16:15:14 +03004787 spin_unlock_bh(&ar->htt.tx_lock);
Michal Kazioraffd3212013-07-16 09:54:35 +02004788
Michal Kazior7962b0d2014-10-28 10:34:38 +01004789 skip = (ar->state == ATH10K_STATE_WEDGED) ||
4790 test_bit(ATH10K_FLAG_CRASH_FLUSH,
4791 &ar->dev_flags);
Michal Kazioraffd3212013-07-16 09:54:35 +02004792
4793 (empty || skip);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004794 }), ATH10K_FLUSH_TIMEOUT_HZ);
Michal Kazioraffd3212013-07-16 09:54:35 +02004795
4796 if (ret <= 0 || skip)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004797 ath10k_warn(ar, "failed to flush transmit queue (skip %i ar-state %i): %i\n",
Ben Greear9ba4c782014-02-25 09:29:57 +02004798 skip, ar->state, ret);
Michal Kazior548db542013-07-05 16:15:15 +03004799
Michal Kazioraffd3212013-07-16 09:54:35 +02004800skip:
Michal Kazior548db542013-07-05 16:15:15 +03004801 mutex_unlock(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004802}
4803
4804/* TODO: Implement this function properly
4805 * For now it is needed to reply to Probe Requests in IBSS mode.
4806 * Propably we need this information from FW.
4807 */
4808static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
4809{
4810 return 1;
4811}
4812
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004813#ifdef CONFIG_PM
4814static int ath10k_suspend(struct ieee80211_hw *hw,
4815 struct cfg80211_wowlan *wowlan)
4816{
4817 struct ath10k *ar = hw->priv;
4818 int ret;
4819
Marek Puzyniak9042e172014-02-10 17:14:23 +01004820 mutex_lock(&ar->conf_mutex);
4821
Marek Puzyniak00f54822014-02-10 17:14:24 +01004822 ret = ath10k_wait_for_suspend(ar, WMI_PDEV_SUSPEND);
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004823 if (ret) {
Marek Puzyniak00f54822014-02-10 17:14:24 +01004824 if (ret == -ETIMEDOUT)
4825 goto resume;
Marek Puzyniak9042e172014-02-10 17:14:23 +01004826 ret = 1;
4827 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004828 }
4829
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004830 ret = ath10k_hif_suspend(ar);
4831 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004832 ath10k_warn(ar, "failed to suspend hif: %d\n", ret);
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004833 goto resume;
4834 }
4835
Marek Puzyniak9042e172014-02-10 17:14:23 +01004836 ret = 0;
4837 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004838resume:
4839 ret = ath10k_wmi_pdev_resume_target(ar);
4840 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004841 ath10k_warn(ar, "failed to resume target: %d\n", ret);
Marek Puzyniak9042e172014-02-10 17:14:23 +01004842
4843 ret = 1;
4844exit:
4845 mutex_unlock(&ar->conf_mutex);
4846 return ret;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004847}
4848
4849static int ath10k_resume(struct ieee80211_hw *hw)
4850{
4851 struct ath10k *ar = hw->priv;
4852 int ret;
4853
Marek Puzyniak9042e172014-02-10 17:14:23 +01004854 mutex_lock(&ar->conf_mutex);
4855
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004856 ret = ath10k_hif_resume(ar);
4857 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004858 ath10k_warn(ar, "failed to resume hif: %d\n", ret);
Marek Puzyniak9042e172014-02-10 17:14:23 +01004859 ret = 1;
4860 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004861 }
4862
4863 ret = ath10k_wmi_pdev_resume_target(ar);
4864 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004865 ath10k_warn(ar, "failed to resume target: %d\n", ret);
Marek Puzyniak9042e172014-02-10 17:14:23 +01004866 ret = 1;
4867 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004868 }
4869
Marek Puzyniak9042e172014-02-10 17:14:23 +01004870 ret = 0;
4871exit:
4872 mutex_unlock(&ar->conf_mutex);
4873 return ret;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004874}
4875#endif
4876
Eliad Pellercf2c92d2014-11-04 11:43:54 +02004877static void ath10k_reconfig_complete(struct ieee80211_hw *hw,
4878 enum ieee80211_reconfig_type reconfig_type)
Michal Kazioraffd3212013-07-16 09:54:35 +02004879{
4880 struct ath10k *ar = hw->priv;
4881
Eliad Pellercf2c92d2014-11-04 11:43:54 +02004882 if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
4883 return;
4884
Michal Kazioraffd3212013-07-16 09:54:35 +02004885 mutex_lock(&ar->conf_mutex);
4886
4887 /* If device failed to restart it will be in a different state, e.g.
4888 * ATH10K_STATE_WEDGED */
4889 if (ar->state == ATH10K_STATE_RESTARTED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004890 ath10k_info(ar, "device successfully recovered\n");
Michal Kazioraffd3212013-07-16 09:54:35 +02004891 ar->state = ATH10K_STATE_ON;
Michal Kazior7962b0d2014-10-28 10:34:38 +01004892 ieee80211_wake_queues(ar->hw);
Michal Kazioraffd3212013-07-16 09:54:35 +02004893 }
4894
4895 mutex_unlock(&ar->conf_mutex);
4896}
4897
Michal Kazior2e1dea42013-07-31 10:32:40 +02004898static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
4899 struct survey_info *survey)
4900{
4901 struct ath10k *ar = hw->priv;
4902 struct ieee80211_supported_band *sband;
4903 struct survey_info *ar_survey = &ar->survey[idx];
4904 int ret = 0;
4905
4906 mutex_lock(&ar->conf_mutex);
4907
4908 sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
4909 if (sband && idx >= sband->n_channels) {
4910 idx -= sband->n_channels;
4911 sband = NULL;
4912 }
4913
4914 if (!sband)
4915 sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
4916
4917 if (!sband || idx >= sband->n_channels) {
4918 ret = -ENOENT;
4919 goto exit;
4920 }
4921
4922 spin_lock_bh(&ar->data_lock);
4923 memcpy(survey, ar_survey, sizeof(*survey));
4924 spin_unlock_bh(&ar->data_lock);
4925
4926 survey->channel = &sband->channels[idx];
4927
Felix Fietkaufa1d4df2014-10-23 17:04:28 +03004928 if (ar->rx_channel == survey->channel)
4929 survey->filled |= SURVEY_INFO_IN_USE;
4930
Michal Kazior2e1dea42013-07-31 10:32:40 +02004931exit:
4932 mutex_unlock(&ar->conf_mutex);
4933 return ret;
4934}
4935
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004936/* Helper table for legacy fixed_rate/bitrate_mask */
4937static const u8 cck_ofdm_rate[] = {
4938 /* CCK */
4939 3, /* 1Mbps */
4940 2, /* 2Mbps */
4941 1, /* 5.5Mbps */
4942 0, /* 11Mbps */
4943 /* OFDM */
4944 3, /* 6Mbps */
4945 7, /* 9Mbps */
4946 2, /* 12Mbps */
4947 6, /* 18Mbps */
4948 1, /* 24Mbps */
4949 5, /* 36Mbps */
4950 0, /* 48Mbps */
4951 4, /* 54Mbps */
4952};
4953
4954/* Check if only one bit set */
4955static int ath10k_check_single_mask(u32 mask)
4956{
4957 int bit;
4958
4959 bit = ffs(mask);
4960 if (!bit)
4961 return 0;
4962
4963 mask &= ~BIT(bit - 1);
4964 if (mask)
4965 return 2;
4966
4967 return 1;
4968}
4969
4970static bool
4971ath10k_default_bitrate_mask(struct ath10k *ar,
4972 enum ieee80211_band band,
4973 const struct cfg80211_bitrate_mask *mask)
4974{
4975 u32 legacy = 0x00ff;
4976 u8 ht = 0xff, i;
4977 u16 vht = 0x3ff;
Ben Greearb116ea12014-11-24 16:22:10 +02004978 u16 nrf = ar->num_rf_chains;
4979
4980 if (ar->cfg_tx_chainmask)
4981 nrf = get_nss_from_chainmask(ar->cfg_tx_chainmask);
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004982
4983 switch (band) {
4984 case IEEE80211_BAND_2GHZ:
4985 legacy = 0x00fff;
4986 vht = 0;
4987 break;
4988 case IEEE80211_BAND_5GHZ:
4989 break;
4990 default:
4991 return false;
4992 }
4993
4994 if (mask->control[band].legacy != legacy)
4995 return false;
4996
Ben Greearb116ea12014-11-24 16:22:10 +02004997 for (i = 0; i < nrf; i++)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004998 if (mask->control[band].ht_mcs[i] != ht)
4999 return false;
5000
Ben Greearb116ea12014-11-24 16:22:10 +02005001 for (i = 0; i < nrf; i++)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005002 if (mask->control[band].vht_mcs[i] != vht)
5003 return false;
5004
5005 return true;
5006}
5007
5008static bool
5009ath10k_bitrate_mask_nss(const struct cfg80211_bitrate_mask *mask,
5010 enum ieee80211_band band,
5011 u8 *fixed_nss)
5012{
5013 int ht_nss = 0, vht_nss = 0, i;
5014
5015 /* check legacy */
5016 if (ath10k_check_single_mask(mask->control[band].legacy))
5017 return false;
5018
5019 /* check HT */
5020 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
5021 if (mask->control[band].ht_mcs[i] == 0xff)
5022 continue;
5023 else if (mask->control[band].ht_mcs[i] == 0x00)
5024 break;
Kalle Valod8bb26b2014-09-14 12:50:33 +03005025
5026 return false;
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005027 }
5028
5029 ht_nss = i;
5030
5031 /* check VHT */
5032 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
5033 if (mask->control[band].vht_mcs[i] == 0x03ff)
5034 continue;
5035 else if (mask->control[band].vht_mcs[i] == 0x0000)
5036 break;
Kalle Valod8bb26b2014-09-14 12:50:33 +03005037
5038 return false;
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005039 }
5040
5041 vht_nss = i;
5042
5043 if (ht_nss > 0 && vht_nss > 0)
5044 return false;
5045
5046 if (ht_nss)
5047 *fixed_nss = ht_nss;
5048 else if (vht_nss)
5049 *fixed_nss = vht_nss;
5050 else
5051 return false;
5052
5053 return true;
5054}
5055
5056static bool
5057ath10k_bitrate_mask_correct(const struct cfg80211_bitrate_mask *mask,
5058 enum ieee80211_band band,
5059 enum wmi_rate_preamble *preamble)
5060{
5061 int legacy = 0, ht = 0, vht = 0, i;
5062
5063 *preamble = WMI_RATE_PREAMBLE_OFDM;
5064
5065 /* check legacy */
5066 legacy = ath10k_check_single_mask(mask->control[band].legacy);
5067 if (legacy > 1)
5068 return false;
5069
5070 /* check HT */
5071 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5072 ht += ath10k_check_single_mask(mask->control[band].ht_mcs[i]);
5073 if (ht > 1)
5074 return false;
5075
5076 /* check VHT */
5077 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
5078 vht += ath10k_check_single_mask(mask->control[band].vht_mcs[i]);
5079 if (vht > 1)
5080 return false;
5081
5082 /* Currently we support only one fixed_rate */
5083 if ((legacy + ht + vht) != 1)
5084 return false;
5085
5086 if (ht)
5087 *preamble = WMI_RATE_PREAMBLE_HT;
5088 else if (vht)
5089 *preamble = WMI_RATE_PREAMBLE_VHT;
5090
5091 return true;
5092}
5093
5094static bool
Michal Kazior7aa7a722014-08-25 12:09:38 +02005095ath10k_bitrate_mask_rate(struct ath10k *ar,
5096 const struct cfg80211_bitrate_mask *mask,
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005097 enum ieee80211_band band,
5098 u8 *fixed_rate,
5099 u8 *fixed_nss)
5100{
5101 u8 rate = 0, pream = 0, nss = 0, i;
5102 enum wmi_rate_preamble preamble;
5103
5104 /* Check if single rate correct */
5105 if (!ath10k_bitrate_mask_correct(mask, band, &preamble))
5106 return false;
5107
5108 pream = preamble;
5109
5110 switch (preamble) {
5111 case WMI_RATE_PREAMBLE_CCK:
5112 case WMI_RATE_PREAMBLE_OFDM:
5113 i = ffs(mask->control[band].legacy) - 1;
5114
5115 if (band == IEEE80211_BAND_2GHZ && i < 4)
5116 pream = WMI_RATE_PREAMBLE_CCK;
5117
5118 if (band == IEEE80211_BAND_5GHZ)
5119 i += 4;
5120
5121 if (i >= ARRAY_SIZE(cck_ofdm_rate))
5122 return false;
5123
5124 rate = cck_ofdm_rate[i];
5125 break;
5126 case WMI_RATE_PREAMBLE_HT:
5127 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5128 if (mask->control[band].ht_mcs[i])
5129 break;
5130
5131 if (i == IEEE80211_HT_MCS_MASK_LEN)
5132 return false;
5133
5134 rate = ffs(mask->control[band].ht_mcs[i]) - 1;
5135 nss = i;
5136 break;
5137 case WMI_RATE_PREAMBLE_VHT:
5138 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
5139 if (mask->control[band].vht_mcs[i])
5140 break;
5141
5142 if (i == NL80211_VHT_NSS_MAX)
5143 return false;
5144
5145 rate = ffs(mask->control[band].vht_mcs[i]) - 1;
5146 nss = i;
5147 break;
5148 }
5149
5150 *fixed_nss = nss + 1;
5151 nss <<= 4;
5152 pream <<= 6;
5153
Michal Kazior7aa7a722014-08-25 12:09:38 +02005154 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 +01005155 pream, nss, rate);
5156
5157 *fixed_rate = pream | nss | rate;
5158
5159 return true;
5160}
5161
Michal Kazior7aa7a722014-08-25 12:09:38 +02005162static bool ath10k_get_fixed_rate_nss(struct ath10k *ar,
5163 const struct cfg80211_bitrate_mask *mask,
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005164 enum ieee80211_band band,
5165 u8 *fixed_rate,
5166 u8 *fixed_nss)
5167{
5168 /* First check full NSS mask, if we can simply limit NSS */
5169 if (ath10k_bitrate_mask_nss(mask, band, fixed_nss))
5170 return true;
5171
5172 /* Next Check single rate is set */
Michal Kazior7aa7a722014-08-25 12:09:38 +02005173 return ath10k_bitrate_mask_rate(ar, mask, band, fixed_rate, fixed_nss);
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005174}
5175
5176static int ath10k_set_fixed_rate_param(struct ath10k_vif *arvif,
5177 u8 fixed_rate,
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01005178 u8 fixed_nss,
5179 u8 force_sgi)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005180{
5181 struct ath10k *ar = arvif->ar;
5182 u32 vdev_param;
5183 int ret = 0;
5184
5185 mutex_lock(&ar->conf_mutex);
5186
5187 if (arvif->fixed_rate == fixed_rate &&
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01005188 arvif->fixed_nss == fixed_nss &&
5189 arvif->force_sgi == force_sgi)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005190 goto exit;
5191
5192 if (fixed_rate == WMI_FIXED_RATE_NONE)
Michal Kazior7aa7a722014-08-25 12:09:38 +02005193 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac disable fixed bitrate mask\n");
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005194
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01005195 if (force_sgi)
Michal Kazior7aa7a722014-08-25 12:09:38 +02005196 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac force sgi\n");
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01005197
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005198 vdev_param = ar->wmi.vdev_param->fixed_rate;
5199 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
5200 vdev_param, fixed_rate);
5201 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02005202 ath10k_warn(ar, "failed to set fixed rate param 0x%02x: %d\n",
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005203 fixed_rate, ret);
5204 ret = -EINVAL;
5205 goto exit;
5206 }
5207
5208 arvif->fixed_rate = fixed_rate;
5209
5210 vdev_param = ar->wmi.vdev_param->nss;
5211 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
5212 vdev_param, fixed_nss);
5213
5214 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02005215 ath10k_warn(ar, "failed to set fixed nss param %d: %d\n",
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005216 fixed_nss, ret);
5217 ret = -EINVAL;
5218 goto exit;
5219 }
5220
5221 arvif->fixed_nss = fixed_nss;
5222
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01005223 vdev_param = ar->wmi.vdev_param->sgi;
5224 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5225 force_sgi);
5226
5227 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02005228 ath10k_warn(ar, "failed to set sgi param %d: %d\n",
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01005229 force_sgi, ret);
5230 ret = -EINVAL;
5231 goto exit;
5232 }
5233
5234 arvif->force_sgi = force_sgi;
5235
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005236exit:
5237 mutex_unlock(&ar->conf_mutex);
5238 return ret;
5239}
5240
5241static int ath10k_set_bitrate_mask(struct ieee80211_hw *hw,
5242 struct ieee80211_vif *vif,
5243 const struct cfg80211_bitrate_mask *mask)
5244{
5245 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5246 struct ath10k *ar = arvif->ar;
5247 enum ieee80211_band band = ar->hw->conf.chandef.chan->band;
5248 u8 fixed_rate = WMI_FIXED_RATE_NONE;
5249 u8 fixed_nss = ar->num_rf_chains;
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01005250 u8 force_sgi;
5251
Ben Greearb116ea12014-11-24 16:22:10 +02005252 if (ar->cfg_tx_chainmask)
5253 fixed_nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
5254
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01005255 force_sgi = mask->control[band].gi;
5256 if (force_sgi == NL80211_TXRATE_FORCE_LGI)
5257 return -EINVAL;
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005258
5259 if (!ath10k_default_bitrate_mask(ar, band, mask)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02005260 if (!ath10k_get_fixed_rate_nss(ar, mask, band,
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005261 &fixed_rate,
5262 &fixed_nss))
5263 return -EINVAL;
5264 }
5265
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01005266 if (fixed_rate == WMI_FIXED_RATE_NONE && force_sgi) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02005267 ath10k_warn(ar, "failed to force SGI usage for default rate settings\n");
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01005268 return -EINVAL;
5269 }
5270
5271 return ath10k_set_fixed_rate_param(arvif, fixed_rate,
5272 fixed_nss, force_sgi);
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005273}
5274
Michal Kazior9797feb2014-02-14 14:49:48 +01005275static void ath10k_sta_rc_update(struct ieee80211_hw *hw,
5276 struct ieee80211_vif *vif,
5277 struct ieee80211_sta *sta,
5278 u32 changed)
5279{
5280 struct ath10k *ar = hw->priv;
5281 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
5282 u32 bw, smps;
5283
5284 spin_lock_bh(&ar->data_lock);
5285
Michal Kazior7aa7a722014-08-25 12:09:38 +02005286 ath10k_dbg(ar, ATH10K_DBG_MAC,
Michal Kazior9797feb2014-02-14 14:49:48 +01005287 "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
5288 sta->addr, changed, sta->bandwidth, sta->rx_nss,
5289 sta->smps_mode);
5290
5291 if (changed & IEEE80211_RC_BW_CHANGED) {
5292 bw = WMI_PEER_CHWIDTH_20MHZ;
5293
5294 switch (sta->bandwidth) {
5295 case IEEE80211_STA_RX_BW_20:
5296 bw = WMI_PEER_CHWIDTH_20MHZ;
5297 break;
5298 case IEEE80211_STA_RX_BW_40:
5299 bw = WMI_PEER_CHWIDTH_40MHZ;
5300 break;
5301 case IEEE80211_STA_RX_BW_80:
5302 bw = WMI_PEER_CHWIDTH_80MHZ;
5303 break;
5304 case IEEE80211_STA_RX_BW_160:
Michal Kazior7aa7a722014-08-25 12:09:38 +02005305 ath10k_warn(ar, "Invalid bandwith %d in rc update for %pM\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02005306 sta->bandwidth, sta->addr);
Michal Kazior9797feb2014-02-14 14:49:48 +01005307 bw = WMI_PEER_CHWIDTH_20MHZ;
5308 break;
5309 }
5310
5311 arsta->bw = bw;
5312 }
5313
5314 if (changed & IEEE80211_RC_NSS_CHANGED)
5315 arsta->nss = sta->rx_nss;
5316
5317 if (changed & IEEE80211_RC_SMPS_CHANGED) {
5318 smps = WMI_PEER_SMPS_PS_NONE;
5319
5320 switch (sta->smps_mode) {
5321 case IEEE80211_SMPS_AUTOMATIC:
5322 case IEEE80211_SMPS_OFF:
5323 smps = WMI_PEER_SMPS_PS_NONE;
5324 break;
5325 case IEEE80211_SMPS_STATIC:
5326 smps = WMI_PEER_SMPS_STATIC;
5327 break;
5328 case IEEE80211_SMPS_DYNAMIC:
5329 smps = WMI_PEER_SMPS_DYNAMIC;
5330 break;
5331 case IEEE80211_SMPS_NUM_MODES:
Michal Kazior7aa7a722014-08-25 12:09:38 +02005332 ath10k_warn(ar, "Invalid smps %d in sta rc update for %pM\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02005333 sta->smps_mode, sta->addr);
Michal Kazior9797feb2014-02-14 14:49:48 +01005334 smps = WMI_PEER_SMPS_PS_NONE;
5335 break;
5336 }
5337
5338 arsta->smps = smps;
5339 }
5340
Michal Kazior9797feb2014-02-14 14:49:48 +01005341 arsta->changed |= changed;
5342
5343 spin_unlock_bh(&ar->data_lock);
5344
5345 ieee80211_queue_work(hw, &arsta->update_wk);
5346}
5347
Chun-Yeow Yeoh26ebbcc2014-02-25 09:29:54 +02005348static u64 ath10k_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
5349{
5350 /*
5351 * FIXME: Return 0 for time being. Need to figure out whether FW
5352 * has the API to fetch 64-bit local TSF
5353 */
5354
5355 return 0;
5356}
5357
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02005358static int ath10k_ampdu_action(struct ieee80211_hw *hw,
5359 struct ieee80211_vif *vif,
5360 enum ieee80211_ampdu_mlme_action action,
5361 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
5362 u8 buf_size)
5363{
Michal Kazior7aa7a722014-08-25 12:09:38 +02005364 struct ath10k *ar = hw->priv;
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02005365 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5366
Michal Kazior7aa7a722014-08-25 12:09:38 +02005367 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 +02005368 arvif->vdev_id, sta->addr, tid, action);
5369
5370 switch (action) {
5371 case IEEE80211_AMPDU_RX_START:
5372 case IEEE80211_AMPDU_RX_STOP:
5373 /* HTT AddBa/DelBa events trigger mac80211 Rx BA session
5374 * creation/removal. Do we need to verify this?
5375 */
5376 return 0;
5377 case IEEE80211_AMPDU_TX_START:
5378 case IEEE80211_AMPDU_TX_STOP_CONT:
5379 case IEEE80211_AMPDU_TX_STOP_FLUSH:
5380 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
5381 case IEEE80211_AMPDU_TX_OPERATIONAL:
5382 /* Firmware offloads Tx aggregation entirely so deny mac80211
5383 * Tx aggregation requests.
5384 */
5385 return -EOPNOTSUPP;
5386 }
5387
5388 return -EINVAL;
5389}
5390
Kalle Valo5e3dd152013-06-12 20:52:10 +03005391static const struct ieee80211_ops ath10k_ops = {
5392 .tx = ath10k_tx,
5393 .start = ath10k_start,
5394 .stop = ath10k_stop,
5395 .config = ath10k_config,
5396 .add_interface = ath10k_add_interface,
5397 .remove_interface = ath10k_remove_interface,
5398 .configure_filter = ath10k_configure_filter,
5399 .bss_info_changed = ath10k_bss_info_changed,
5400 .hw_scan = ath10k_hw_scan,
5401 .cancel_hw_scan = ath10k_cancel_hw_scan,
5402 .set_key = ath10k_set_key,
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +02005403 .set_default_unicast_key = ath10k_set_default_unicast_key,
Kalle Valo5e3dd152013-06-12 20:52:10 +03005404 .sta_state = ath10k_sta_state,
5405 .conf_tx = ath10k_conf_tx,
5406 .remain_on_channel = ath10k_remain_on_channel,
5407 .cancel_remain_on_channel = ath10k_cancel_remain_on_channel,
5408 .set_rts_threshold = ath10k_set_rts_threshold,
Kalle Valo5e3dd152013-06-12 20:52:10 +03005409 .flush = ath10k_flush,
5410 .tx_last_beacon = ath10k_tx_last_beacon,
Ben Greear46acf7b2014-05-16 17:15:38 +03005411 .set_antenna = ath10k_set_antenna,
5412 .get_antenna = ath10k_get_antenna,
Eliad Pellercf2c92d2014-11-04 11:43:54 +02005413 .reconfig_complete = ath10k_reconfig_complete,
Michal Kazior2e1dea42013-07-31 10:32:40 +02005414 .get_survey = ath10k_get_survey,
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005415 .set_bitrate_mask = ath10k_set_bitrate_mask,
Michal Kazior9797feb2014-02-14 14:49:48 +01005416 .sta_rc_update = ath10k_sta_rc_update,
Chun-Yeow Yeoh26ebbcc2014-02-25 09:29:54 +02005417 .get_tsf = ath10k_get_tsf,
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02005418 .ampdu_action = ath10k_ampdu_action,
Ben Greear6cddcc72014-09-29 14:41:46 +03005419 .get_et_sset_count = ath10k_debug_get_et_sset_count,
5420 .get_et_stats = ath10k_debug_get_et_stats,
5421 .get_et_strings = ath10k_debug_get_et_strings,
Kalle Valo43d2a302014-09-10 18:23:30 +03005422
5423 CFG80211_TESTMODE_CMD(ath10k_tm_cmd)
5424
Michal Kazior8cd13ca2013-07-16 09:38:54 +02005425#ifdef CONFIG_PM
5426 .suspend = ath10k_suspend,
5427 .resume = ath10k_resume,
5428#endif
Rajkumar Manoharanf5045982015-01-12 14:07:27 +02005429#ifdef CONFIG_MAC80211_DEBUGFS
5430 .sta_add_debugfs = ath10k_sta_add_debugfs,
5431#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03005432};
5433
5434#define RATETAB_ENT(_rate, _rateid, _flags) { \
5435 .bitrate = (_rate), \
5436 .flags = (_flags), \
5437 .hw_value = (_rateid), \
5438}
5439
5440#define CHAN2G(_channel, _freq, _flags) { \
5441 .band = IEEE80211_BAND_2GHZ, \
5442 .hw_value = (_channel), \
5443 .center_freq = (_freq), \
5444 .flags = (_flags), \
5445 .max_antenna_gain = 0, \
5446 .max_power = 30, \
5447}
5448
5449#define CHAN5G(_channel, _freq, _flags) { \
5450 .band = IEEE80211_BAND_5GHZ, \
5451 .hw_value = (_channel), \
5452 .center_freq = (_freq), \
5453 .flags = (_flags), \
5454 .max_antenna_gain = 0, \
5455 .max_power = 30, \
5456}
5457
5458static const struct ieee80211_channel ath10k_2ghz_channels[] = {
5459 CHAN2G(1, 2412, 0),
5460 CHAN2G(2, 2417, 0),
5461 CHAN2G(3, 2422, 0),
5462 CHAN2G(4, 2427, 0),
5463 CHAN2G(5, 2432, 0),
5464 CHAN2G(6, 2437, 0),
5465 CHAN2G(7, 2442, 0),
5466 CHAN2G(8, 2447, 0),
5467 CHAN2G(9, 2452, 0),
5468 CHAN2G(10, 2457, 0),
5469 CHAN2G(11, 2462, 0),
5470 CHAN2G(12, 2467, 0),
5471 CHAN2G(13, 2472, 0),
5472 CHAN2G(14, 2484, 0),
5473};
5474
5475static const struct ieee80211_channel ath10k_5ghz_channels[] = {
Michal Kazior429ff562013-06-26 08:54:54 +02005476 CHAN5G(36, 5180, 0),
5477 CHAN5G(40, 5200, 0),
5478 CHAN5G(44, 5220, 0),
5479 CHAN5G(48, 5240, 0),
5480 CHAN5G(52, 5260, 0),
5481 CHAN5G(56, 5280, 0),
5482 CHAN5G(60, 5300, 0),
5483 CHAN5G(64, 5320, 0),
5484 CHAN5G(100, 5500, 0),
5485 CHAN5G(104, 5520, 0),
5486 CHAN5G(108, 5540, 0),
5487 CHAN5G(112, 5560, 0),
5488 CHAN5G(116, 5580, 0),
5489 CHAN5G(120, 5600, 0),
5490 CHAN5G(124, 5620, 0),
5491 CHAN5G(128, 5640, 0),
5492 CHAN5G(132, 5660, 0),
5493 CHAN5G(136, 5680, 0),
5494 CHAN5G(140, 5700, 0),
Peter Oh4a7898f2015-03-18 11:39:18 -07005495 CHAN5G(144, 5720, 0),
Michal Kazior429ff562013-06-26 08:54:54 +02005496 CHAN5G(149, 5745, 0),
5497 CHAN5G(153, 5765, 0),
5498 CHAN5G(157, 5785, 0),
5499 CHAN5G(161, 5805, 0),
5500 CHAN5G(165, 5825, 0),
Kalle Valo5e3dd152013-06-12 20:52:10 +03005501};
5502
Michal Kazior91b12082014-12-12 12:41:35 +01005503/* Note: Be careful if you re-order these. There is code which depends on this
5504 * ordering.
5505 */
Kalle Valo5e3dd152013-06-12 20:52:10 +03005506static struct ieee80211_rate ath10k_rates[] = {
5507 /* CCK */
5508 RATETAB_ENT(10, 0x82, 0),
5509 RATETAB_ENT(20, 0x84, 0),
5510 RATETAB_ENT(55, 0x8b, 0),
5511 RATETAB_ENT(110, 0x96, 0),
5512 /* OFDM */
5513 RATETAB_ENT(60, 0x0c, 0),
5514 RATETAB_ENT(90, 0x12, 0),
5515 RATETAB_ENT(120, 0x18, 0),
5516 RATETAB_ENT(180, 0x24, 0),
5517 RATETAB_ENT(240, 0x30, 0),
5518 RATETAB_ENT(360, 0x48, 0),
5519 RATETAB_ENT(480, 0x60, 0),
5520 RATETAB_ENT(540, 0x6c, 0),
5521};
5522
5523#define ath10k_a_rates (ath10k_rates + 4)
5524#define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - 4)
5525#define ath10k_g_rates (ath10k_rates + 0)
5526#define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
5527
Michal Kaziore7b54192014-08-07 11:03:27 +02005528struct ath10k *ath10k_mac_create(size_t priv_size)
Kalle Valo5e3dd152013-06-12 20:52:10 +03005529{
5530 struct ieee80211_hw *hw;
5531 struct ath10k *ar;
5532
Michal Kaziore7b54192014-08-07 11:03:27 +02005533 hw = ieee80211_alloc_hw(sizeof(struct ath10k) + priv_size, &ath10k_ops);
Kalle Valo5e3dd152013-06-12 20:52:10 +03005534 if (!hw)
5535 return NULL;
5536
5537 ar = hw->priv;
5538 ar->hw = hw;
5539
5540 return ar;
5541}
5542
5543void ath10k_mac_destroy(struct ath10k *ar)
5544{
5545 ieee80211_free_hw(ar->hw);
5546}
5547
5548static const struct ieee80211_iface_limit ath10k_if_limits[] = {
5549 {
5550 .max = 8,
5551 .types = BIT(NL80211_IFTYPE_STATION)
5552 | BIT(NL80211_IFTYPE_P2P_CLIENT)
Michal Kaziord531cb82013-07-31 10:55:13 +02005553 },
5554 {
5555 .max = 3,
5556 .types = BIT(NL80211_IFTYPE_P2P_GO)
5557 },
5558 {
Michal Kazior75d2bd42014-12-12 12:41:39 +01005559 .max = 1,
5560 .types = BIT(NL80211_IFTYPE_P2P_DEVICE)
5561 },
5562 {
Michal Kaziord531cb82013-07-31 10:55:13 +02005563 .max = 7,
5564 .types = BIT(NL80211_IFTYPE_AP)
5565 },
Kalle Valo5e3dd152013-06-12 20:52:10 +03005566};
5567
Bartosz Markowskif2595092013-12-10 16:20:39 +01005568static const struct ieee80211_iface_limit ath10k_10x_if_limits[] = {
Marek Puzyniake8a50f82013-11-20 09:59:47 +02005569 {
5570 .max = 8,
5571 .types = BIT(NL80211_IFTYPE_AP)
5572 },
5573};
Marek Puzyniake8a50f82013-11-20 09:59:47 +02005574
5575static const struct ieee80211_iface_combination ath10k_if_comb[] = {
5576 {
5577 .limits = ath10k_if_limits,
5578 .n_limits = ARRAY_SIZE(ath10k_if_limits),
5579 .max_interfaces = 8,
5580 .num_different_channels = 1,
5581 .beacon_int_infra_match = true,
5582 },
Bartosz Markowskif2595092013-12-10 16:20:39 +01005583};
5584
5585static const struct ieee80211_iface_combination ath10k_10x_if_comb[] = {
Marek Puzyniake8a50f82013-11-20 09:59:47 +02005586 {
Bartosz Markowskif2595092013-12-10 16:20:39 +01005587 .limits = ath10k_10x_if_limits,
5588 .n_limits = ARRAY_SIZE(ath10k_10x_if_limits),
Marek Puzyniake8a50f82013-11-20 09:59:47 +02005589 .max_interfaces = 8,
5590 .num_different_channels = 1,
5591 .beacon_int_infra_match = true,
Bartosz Markowskif2595092013-12-10 16:20:39 +01005592#ifdef CONFIG_ATH10K_DFS_CERTIFIED
Marek Puzyniake8a50f82013-11-20 09:59:47 +02005593 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
5594 BIT(NL80211_CHAN_WIDTH_20) |
5595 BIT(NL80211_CHAN_WIDTH_40) |
5596 BIT(NL80211_CHAN_WIDTH_80),
Marek Puzyniake8a50f82013-11-20 09:59:47 +02005597#endif
Bartosz Markowskif2595092013-12-10 16:20:39 +01005598 },
Kalle Valo5e3dd152013-06-12 20:52:10 +03005599};
5600
5601static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
5602{
5603 struct ieee80211_sta_vht_cap vht_cap = {0};
5604 u16 mcs_map;
Michal Kaziorbc657a362015-02-26 11:11:22 +01005605 u32 val;
Michal Kazior8865bee42013-07-24 12:36:46 +02005606 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005607
5608 vht_cap.vht_supported = 1;
5609 vht_cap.cap = ar->vht_cap_info;
5610
Michal Kaziorbc657a362015-02-26 11:11:22 +01005611 if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
5612 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)) {
5613 val = ar->num_rf_chains - 1;
5614 val <<= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
5615 val &= IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
5616
5617 vht_cap.cap |= val;
5618 }
5619
5620 if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
5621 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)) {
5622 val = ar->num_rf_chains - 1;
5623 val <<= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
5624 val &= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
5625
5626 vht_cap.cap |= val;
5627 }
5628
Michal Kazior8865bee42013-07-24 12:36:46 +02005629 mcs_map = 0;
5630 for (i = 0; i < 8; i++) {
5631 if (i < ar->num_rf_chains)
5632 mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i*2);
5633 else
5634 mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i*2);
5635 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03005636
5637 vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
5638 vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
5639
5640 return vht_cap;
5641}
5642
5643static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
5644{
5645 int i;
5646 struct ieee80211_sta_ht_cap ht_cap = {0};
5647
5648 if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
5649 return ht_cap;
5650
5651 ht_cap.ht_supported = 1;
5652 ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
5653 ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
5654 ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
5655 ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
5656 ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
5657
5658 if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
5659 ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
5660
5661 if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
5662 ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
5663
5664 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
5665 u32 smps;
5666
5667 smps = WLAN_HT_CAP_SM_PS_DYNAMIC;
5668 smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
5669
5670 ht_cap.cap |= smps;
5671 }
5672
5673 if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC)
5674 ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
5675
5676 if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
5677 u32 stbc;
5678
5679 stbc = ar->ht_cap_info;
5680 stbc &= WMI_HT_CAP_RX_STBC;
5681 stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
5682 stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
5683 stbc &= IEEE80211_HT_CAP_RX_STBC;
5684
5685 ht_cap.cap |= stbc;
5686 }
5687
5688 if (ar->ht_cap_info & WMI_HT_CAP_LDPC)
5689 ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
5690
5691 if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
5692 ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
5693
5694 /* max AMSDU is implicitly taken from vht_cap_info */
5695 if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
5696 ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
5697
Michal Kazior8865bee42013-07-24 12:36:46 +02005698 for (i = 0; i < ar->num_rf_chains; i++)
Kalle Valo5e3dd152013-06-12 20:52:10 +03005699 ht_cap.mcs.rx_mask[i] = 0xFF;
5700
5701 ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
5702
5703 return ht_cap;
5704}
5705
Kalle Valo5e3dd152013-06-12 20:52:10 +03005706static void ath10k_get_arvif_iter(void *data, u8 *mac,
5707 struct ieee80211_vif *vif)
5708{
5709 struct ath10k_vif_iter *arvif_iter = data;
5710 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5711
5712 if (arvif->vdev_id == arvif_iter->vdev_id)
5713 arvif_iter->arvif = arvif;
5714}
5715
5716struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
5717{
5718 struct ath10k_vif_iter arvif_iter;
5719 u32 flags;
5720
5721 memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
5722 arvif_iter.vdev_id = vdev_id;
5723
5724 flags = IEEE80211_IFACE_ITER_RESUME_ALL;
5725 ieee80211_iterate_active_interfaces_atomic(ar->hw,
5726 flags,
5727 ath10k_get_arvif_iter,
5728 &arvif_iter);
5729 if (!arvif_iter.arvif) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02005730 ath10k_warn(ar, "No VIF found for vdev %d\n", vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03005731 return NULL;
5732 }
5733
5734 return arvif_iter.arvif;
5735}
5736
5737int ath10k_mac_register(struct ath10k *ar)
5738{
Johannes Berg3cb10942015-01-22 21:38:45 +01005739 static const u32 cipher_suites[] = {
5740 WLAN_CIPHER_SUITE_WEP40,
5741 WLAN_CIPHER_SUITE_WEP104,
5742 WLAN_CIPHER_SUITE_TKIP,
5743 WLAN_CIPHER_SUITE_CCMP,
5744 WLAN_CIPHER_SUITE_AES_CMAC,
5745 };
Kalle Valo5e3dd152013-06-12 20:52:10 +03005746 struct ieee80211_supported_band *band;
5747 struct ieee80211_sta_vht_cap vht_cap;
5748 struct ieee80211_sta_ht_cap ht_cap;
5749 void *channels;
5750 int ret;
5751
5752 SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
5753
5754 SET_IEEE80211_DEV(ar->hw, ar->dev);
5755
5756 ht_cap = ath10k_get_ht_cap(ar);
5757 vht_cap = ath10k_create_vht_cap(ar);
5758
5759 if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
5760 channels = kmemdup(ath10k_2ghz_channels,
5761 sizeof(ath10k_2ghz_channels),
5762 GFP_KERNEL);
Michal Kaziord6015b22013-07-22 14:13:30 +02005763 if (!channels) {
5764 ret = -ENOMEM;
5765 goto err_free;
5766 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03005767
5768 band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
5769 band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
5770 band->channels = channels;
5771 band->n_bitrates = ath10k_g_rates_size;
5772 band->bitrates = ath10k_g_rates;
5773 band->ht_cap = ht_cap;
5774
Yanbo Lid68bb122015-01-23 08:18:20 +08005775 /* Enable the VHT support at 2.4 GHz */
5776 band->vht_cap = vht_cap;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005777
5778 ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
5779 }
5780
5781 if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
5782 channels = kmemdup(ath10k_5ghz_channels,
5783 sizeof(ath10k_5ghz_channels),
5784 GFP_KERNEL);
5785 if (!channels) {
Michal Kaziord6015b22013-07-22 14:13:30 +02005786 ret = -ENOMEM;
5787 goto err_free;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005788 }
5789
5790 band = &ar->mac.sbands[IEEE80211_BAND_5GHZ];
5791 band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
5792 band->channels = channels;
5793 band->n_bitrates = ath10k_a_rates_size;
5794 band->bitrates = ath10k_a_rates;
5795 band->ht_cap = ht_cap;
5796 band->vht_cap = vht_cap;
5797 ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
5798 }
5799
5800 ar->hw->wiphy->interface_modes =
5801 BIT(NL80211_IFTYPE_STATION) |
Bartosz Markowskid3541812013-12-10 16:20:40 +01005802 BIT(NL80211_IFTYPE_AP);
5803
Ben Greear46acf7b2014-05-16 17:15:38 +03005804 ar->hw->wiphy->available_antennas_rx = ar->supp_rx_chainmask;
5805 ar->hw->wiphy->available_antennas_tx = ar->supp_tx_chainmask;
5806
Bartosz Markowskid3541812013-12-10 16:20:40 +01005807 if (!test_bit(ATH10K_FW_FEATURE_NO_P2P, ar->fw_features))
5808 ar->hw->wiphy->interface_modes |=
Michal Kazior75d2bd42014-12-12 12:41:39 +01005809 BIT(NL80211_IFTYPE_P2P_DEVICE) |
Bartosz Markowskid3541812013-12-10 16:20:40 +01005810 BIT(NL80211_IFTYPE_P2P_CLIENT) |
5811 BIT(NL80211_IFTYPE_P2P_GO);
Kalle Valo5e3dd152013-06-12 20:52:10 +03005812
5813 ar->hw->flags = IEEE80211_HW_SIGNAL_DBM |
5814 IEEE80211_HW_SUPPORTS_PS |
5815 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
Kalle Valo5e3dd152013-06-12 20:52:10 +03005816 IEEE80211_HW_MFP_CAPABLE |
5817 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
5818 IEEE80211_HW_HAS_RATE_CONTROL |
Janusz Dziedzic2f0f1122014-02-26 18:42:09 +02005819 IEEE80211_HW_AP_LINK_PS |
Johannes Berg3cb10942015-01-22 21:38:45 +01005820 IEEE80211_HW_SPECTRUM_MGMT |
Michal Kaziorcc9904e2015-03-10 16:22:01 +02005821 IEEE80211_HW_SW_CRYPTO_CONTROL |
5822 IEEE80211_HW_CONNECTION_MONITOR;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005823
Eliad Peller0d8614b2014-09-10 14:07:36 +03005824 ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
5825
Kalle Valo5e3dd152013-06-12 20:52:10 +03005826 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
Eliad Peller0d8614b2014-09-10 14:07:36 +03005827 ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005828
5829 if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
5830 ar->hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
5831 ar->hw->flags |= IEEE80211_HW_TX_AMPDU_SETUP_IN_HW;
5832 }
5833
5834 ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
5835 ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
5836
5837 ar->hw->vif_data_size = sizeof(struct ath10k_vif);
Michal Kazior9797feb2014-02-14 14:49:48 +01005838 ar->hw->sta_data_size = sizeof(struct ath10k_sta);
Kalle Valo5e3dd152013-06-12 20:52:10 +03005839
Kalle Valo5e3dd152013-06-12 20:52:10 +03005840 ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
5841
Michal Kaziorfbb8f1b2015-01-13 16:30:12 +02005842 if (test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)) {
5843 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
5844
5845 /* Firmware delivers WPS/P2P Probe Requests frames to driver so
5846 * that userspace (e.g. wpa_supplicant/hostapd) can generate
5847 * correct Probe Responses. This is more of a hack advert..
5848 */
5849 ar->hw->wiphy->probe_resp_offload |=
5850 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
5851 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
5852 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
5853 }
5854
Kalle Valo5e3dd152013-06-12 20:52:10 +03005855 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
Michal Kaziorc2df44b2014-01-23 11:38:26 +01005856 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005857 ar->hw->wiphy->max_remain_on_channel_duration = 5000;
5858
5859 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
Rajkumar Manoharan78157a12014-11-17 16:44:15 +02005860 ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE;
5861
Janusz.Dziedzic@tieto.com37a0b392015-03-12 13:11:41 +01005862 ar->hw->wiphy->max_ap_assoc_sta = ar->max_num_stations;
5863
Kalle Valo5e3dd152013-06-12 20:52:10 +03005864 /*
5865 * on LL hardware queues are managed entirely by the FW
5866 * so we only advertise to mac we can do the queues thing
5867 */
5868 ar->hw->queues = 4;
5869
Kalle Valo5cc7caf2014-12-17 12:20:54 +02005870 switch (ar->wmi.op_version) {
5871 case ATH10K_FW_WMI_OP_VERSION_MAIN:
5872 case ATH10K_FW_WMI_OP_VERSION_TLV:
Bartosz Markowskif2595092013-12-10 16:20:39 +01005873 ar->hw->wiphy->iface_combinations = ath10k_if_comb;
5874 ar->hw->wiphy->n_iface_combinations =
5875 ARRAY_SIZE(ath10k_if_comb);
Michal Kaziorcf850d12014-07-24 20:07:00 +03005876 ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
Kalle Valo5cc7caf2014-12-17 12:20:54 +02005877 break;
5878 case ATH10K_FW_WMI_OP_VERSION_10_1:
5879 case ATH10K_FW_WMI_OP_VERSION_10_2:
Rajkumar Manoharan4a16fbe2014-12-17 12:21:12 +02005880 case ATH10K_FW_WMI_OP_VERSION_10_2_4:
Kalle Valo5cc7caf2014-12-17 12:20:54 +02005881 ar->hw->wiphy->iface_combinations = ath10k_10x_if_comb;
5882 ar->hw->wiphy->n_iface_combinations =
5883 ARRAY_SIZE(ath10k_10x_if_comb);
5884 break;
5885 case ATH10K_FW_WMI_OP_VERSION_UNSET:
5886 case ATH10K_FW_WMI_OP_VERSION_MAX:
5887 WARN_ON(1);
5888 ret = -EINVAL;
5889 goto err_free;
Bartosz Markowskif2595092013-12-10 16:20:39 +01005890 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03005891
Michal Kazior7c199992013-07-31 10:47:57 +02005892 ar->hw->netdev_features = NETIF_F_HW_CSUM;
5893
Janusz Dziedzic9702c682013-11-20 09:59:41 +02005894 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED)) {
5895 /* Init ath dfs pattern detector */
5896 ar->ath_common.debug_mask = ATH_DBG_DFS;
5897 ar->dfs_detector = dfs_pattern_detector_init(&ar->ath_common,
5898 NL80211_DFS_UNSET);
5899
5900 if (!ar->dfs_detector)
Michal Kazior7aa7a722014-08-25 12:09:38 +02005901 ath10k_warn(ar, "failed to initialise DFS pattern detector\n");
Janusz Dziedzic9702c682013-11-20 09:59:41 +02005902 }
5903
Kalle Valo5e3dd152013-06-12 20:52:10 +03005904 ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
5905 ath10k_reg_notifier);
5906 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02005907 ath10k_err(ar, "failed to initialise regulatory: %i\n", ret);
Michal Kaziord6015b22013-07-22 14:13:30 +02005908 goto err_free;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005909 }
5910
Johannes Berg3cb10942015-01-22 21:38:45 +01005911 ar->hw->wiphy->cipher_suites = cipher_suites;
5912 ar->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
5913
Kalle Valo5e3dd152013-06-12 20:52:10 +03005914 ret = ieee80211_register_hw(ar->hw);
5915 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02005916 ath10k_err(ar, "failed to register ieee80211: %d\n", ret);
Michal Kaziord6015b22013-07-22 14:13:30 +02005917 goto err_free;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005918 }
5919
5920 if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
5921 ret = regulatory_hint(ar->hw->wiphy,
5922 ar->ath_common.regulatory.alpha2);
5923 if (ret)
Michal Kaziord6015b22013-07-22 14:13:30 +02005924 goto err_unregister;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005925 }
5926
5927 return 0;
Michal Kaziord6015b22013-07-22 14:13:30 +02005928
5929err_unregister:
Kalle Valo5e3dd152013-06-12 20:52:10 +03005930 ieee80211_unregister_hw(ar->hw);
Michal Kaziord6015b22013-07-22 14:13:30 +02005931err_free:
5932 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
5933 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
5934
Kalle Valo5e3dd152013-06-12 20:52:10 +03005935 return ret;
5936}
5937
5938void ath10k_mac_unregister(struct ath10k *ar)
5939{
5940 ieee80211_unregister_hw(ar->hw);
5941
Janusz Dziedzic9702c682013-11-20 09:59:41 +02005942 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
5943 ar->dfs_detector->exit(ar->dfs_detector);
5944
Kalle Valo5e3dd152013-06-12 20:52:10 +03005945 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
5946 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
5947
5948 SET_IEEE80211_DEV(ar->hw, NULL);
5949}