blob: 4120fe5fc7bc7a58501b35526883db84f83cc86d [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,
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +020040 const u8 *macaddr, bool def_idx)
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,
48 .macaddr = macaddr,
49 };
50
Michal Kazior548db542013-07-05 16:15:15 +030051 lockdep_assert_held(&arvif->ar->conf_mutex);
52
Kalle Valo5e3dd152013-06-12 20:52:10 +030053 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
54 arg.key_flags = WMI_KEY_PAIRWISE;
55 else
56 arg.key_flags = WMI_KEY_GROUP;
57
58 switch (key->cipher) {
59 case WLAN_CIPHER_SUITE_CCMP:
60 arg.key_cipher = WMI_CIPHER_AES_CCM;
Marek Kwaczynskie4e82e92015-01-24 12:14:53 +020061 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
Kalle Valo5e3dd152013-06-12 20:52:10 +030062 break;
63 case WLAN_CIPHER_SUITE_TKIP:
Kalle Valo5e3dd152013-06-12 20:52:10 +030064 arg.key_cipher = WMI_CIPHER_TKIP;
65 arg.key_txmic_len = 8;
66 arg.key_rxmic_len = 8;
67 break;
68 case WLAN_CIPHER_SUITE_WEP40:
69 case WLAN_CIPHER_SUITE_WEP104:
70 arg.key_cipher = WMI_CIPHER_WEP;
71 /* AP/IBSS mode requires self-key to be groupwise
72 * Otherwise pairwise key must be set */
73 if (memcmp(macaddr, arvif->vif->addr, ETH_ALEN))
74 arg.key_flags = WMI_KEY_PAIRWISE;
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +020075
76 if (def_idx)
77 arg.key_flags |= WMI_KEY_TX_USAGE;
Kalle Valo5e3dd152013-06-12 20:52:10 +030078 break;
Johannes Berg3cb10942015-01-22 21:38:45 +010079 case WLAN_CIPHER_SUITE_AES_CMAC:
80 /* this one needs to be done in software */
81 return 1;
Kalle Valo5e3dd152013-06-12 20:52:10 +030082 default:
Michal Kazior7aa7a722014-08-25 12:09:38 +020083 ath10k_warn(ar, "cipher %d is not supported\n", key->cipher);
Kalle Valo5e3dd152013-06-12 20:52:10 +030084 return -EOPNOTSUPP;
85 }
86
87 if (cmd == DISABLE_KEY) {
88 arg.key_cipher = WMI_CIPHER_NONE;
89 arg.key_data = NULL;
90 }
91
92 return ath10k_wmi_vdev_install_key(arvif->ar, &arg);
93}
94
95static int ath10k_install_key(struct ath10k_vif *arvif,
96 struct ieee80211_key_conf *key,
97 enum set_key_cmd cmd,
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +020098 const u8 *macaddr, bool def_idx)
Kalle Valo5e3dd152013-06-12 20:52:10 +030099{
100 struct ath10k *ar = arvif->ar;
101 int ret;
102
Michal Kazior548db542013-07-05 16:15:15 +0300103 lockdep_assert_held(&ar->conf_mutex);
104
Wolfram Sang16735d02013-11-14 14:32:02 -0800105 reinit_completion(&ar->install_key_done);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300106
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +0200107 ret = ath10k_send_key(arvif, key, cmd, macaddr, def_idx);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300108 if (ret)
109 return ret;
110
111 ret = wait_for_completion_timeout(&ar->install_key_done, 3*HZ);
112 if (ret == 0)
113 return -ETIMEDOUT;
114
115 return 0;
116}
117
118static int ath10k_install_peer_wep_keys(struct ath10k_vif *arvif,
119 const u8 *addr)
120{
121 struct ath10k *ar = arvif->ar;
122 struct ath10k_peer *peer;
123 int ret;
124 int i;
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +0200125 bool def_idx;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300126
127 lockdep_assert_held(&ar->conf_mutex);
128
129 spin_lock_bh(&ar->data_lock);
130 peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
131 spin_unlock_bh(&ar->data_lock);
132
133 if (!peer)
134 return -ENOENT;
135
136 for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
137 if (arvif->wep_keys[i] == NULL)
138 continue;
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +0200139 /* set TX_USAGE flag for default key id */
140 if (arvif->def_wep_key_idx == i)
141 def_idx = true;
142 else
143 def_idx = false;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300144
145 ret = ath10k_install_key(arvif, arvif->wep_keys[i], SET_KEY,
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +0200146 addr, def_idx);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300147 if (ret)
148 return ret;
149
Sujith Manoharanae167132014-11-25 11:46:59 +0530150 spin_lock_bh(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300151 peer->keys[i] = arvif->wep_keys[i];
Sujith Manoharanae167132014-11-25 11:46:59 +0530152 spin_unlock_bh(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300153 }
154
155 return 0;
156}
157
158static int ath10k_clear_peer_keys(struct ath10k_vif *arvif,
159 const u8 *addr)
160{
161 struct ath10k *ar = arvif->ar;
162 struct ath10k_peer *peer;
163 int first_errno = 0;
164 int ret;
165 int i;
166
167 lockdep_assert_held(&ar->conf_mutex);
168
169 spin_lock_bh(&ar->data_lock);
170 peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
171 spin_unlock_bh(&ar->data_lock);
172
173 if (!peer)
174 return -ENOENT;
175
176 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
177 if (peer->keys[i] == NULL)
178 continue;
179
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +0200180 /* key flags are not required to delete the key */
Kalle Valo5e3dd152013-06-12 20:52:10 +0300181 ret = ath10k_install_key(arvif, peer->keys[i],
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +0200182 DISABLE_KEY, addr, false);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300183 if (ret && first_errno == 0)
184 first_errno = ret;
185
186 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200187 ath10k_warn(ar, "failed to remove peer wep key %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300188 i, ret);
189
Sujith Manoharanae167132014-11-25 11:46:59 +0530190 spin_lock_bh(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300191 peer->keys[i] = NULL;
Sujith Manoharanae167132014-11-25 11:46:59 +0530192 spin_unlock_bh(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300193 }
194
195 return first_errno;
196}
197
Sujith Manoharan504f6cd2014-11-25 11:46:58 +0530198bool ath10k_mac_is_peer_wep_key_set(struct ath10k *ar, const u8 *addr,
199 u8 keyidx)
200{
201 struct ath10k_peer *peer;
202 int i;
203
204 lockdep_assert_held(&ar->data_lock);
205
206 /* We don't know which vdev this peer belongs to,
207 * since WMI doesn't give us that information.
208 *
209 * FIXME: multi-bss needs to be handled.
210 */
211 peer = ath10k_peer_find(ar, 0, addr);
212 if (!peer)
213 return false;
214
215 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
216 if (peer->keys[i] && peer->keys[i]->keyidx == keyidx)
217 return true;
218 }
219
220 return false;
221}
222
Kalle Valo5e3dd152013-06-12 20:52:10 +0300223static int ath10k_clear_vdev_key(struct ath10k_vif *arvif,
224 struct ieee80211_key_conf *key)
225{
226 struct ath10k *ar = arvif->ar;
227 struct ath10k_peer *peer;
228 u8 addr[ETH_ALEN];
229 int first_errno = 0;
230 int ret;
231 int i;
232
233 lockdep_assert_held(&ar->conf_mutex);
234
235 for (;;) {
236 /* since ath10k_install_key we can't hold data_lock all the
237 * time, so we try to remove the keys incrementally */
238 spin_lock_bh(&ar->data_lock);
239 i = 0;
240 list_for_each_entry(peer, &ar->peers, list) {
241 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
242 if (peer->keys[i] == key) {
Kalle Valob25f32c2014-09-14 12:50:49 +0300243 ether_addr_copy(addr, peer->addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300244 peer->keys[i] = NULL;
245 break;
246 }
247 }
248
249 if (i < ARRAY_SIZE(peer->keys))
250 break;
251 }
252 spin_unlock_bh(&ar->data_lock);
253
254 if (i == ARRAY_SIZE(peer->keys))
255 break;
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +0200256 /* key flags are not required to delete the key */
257 ret = ath10k_install_key(arvif, key, DISABLE_KEY, addr, false);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300258 if (ret && first_errno == 0)
259 first_errno = ret;
260
261 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200262 ath10k_warn(ar, "failed to remove key for %pM: %d\n",
Kalle Valobe6546f2014-03-25 14:18:51 +0200263 addr, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300264 }
265
266 return first_errno;
267}
268
Kalle Valo5e3dd152013-06-12 20:52:10 +0300269/*********************/
270/* General utilities */
271/*********************/
272
273static inline enum wmi_phy_mode
274chan_to_phymode(const struct cfg80211_chan_def *chandef)
275{
276 enum wmi_phy_mode phymode = MODE_UNKNOWN;
277
278 switch (chandef->chan->band) {
279 case IEEE80211_BAND_2GHZ:
280 switch (chandef->width) {
281 case NL80211_CHAN_WIDTH_20_NOHT:
Peter Oh6faab122014-12-18 10:13:00 -0800282 if (chandef->chan->flags & IEEE80211_CHAN_NO_OFDM)
283 phymode = MODE_11B;
284 else
285 phymode = MODE_11G;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300286 break;
287 case NL80211_CHAN_WIDTH_20:
288 phymode = MODE_11NG_HT20;
289 break;
290 case NL80211_CHAN_WIDTH_40:
291 phymode = MODE_11NG_HT40;
292 break;
John W. Linville0f817ed2013-06-27 13:50:09 -0400293 case NL80211_CHAN_WIDTH_5:
294 case NL80211_CHAN_WIDTH_10:
Kalle Valo5e3dd152013-06-12 20:52:10 +0300295 case NL80211_CHAN_WIDTH_80:
296 case NL80211_CHAN_WIDTH_80P80:
297 case NL80211_CHAN_WIDTH_160:
298 phymode = MODE_UNKNOWN;
299 break;
300 }
301 break;
302 case IEEE80211_BAND_5GHZ:
303 switch (chandef->width) {
304 case NL80211_CHAN_WIDTH_20_NOHT:
305 phymode = MODE_11A;
306 break;
307 case NL80211_CHAN_WIDTH_20:
308 phymode = MODE_11NA_HT20;
309 break;
310 case NL80211_CHAN_WIDTH_40:
311 phymode = MODE_11NA_HT40;
312 break;
313 case NL80211_CHAN_WIDTH_80:
314 phymode = MODE_11AC_VHT80;
315 break;
John W. Linville0f817ed2013-06-27 13:50:09 -0400316 case NL80211_CHAN_WIDTH_5:
317 case NL80211_CHAN_WIDTH_10:
Kalle Valo5e3dd152013-06-12 20:52:10 +0300318 case NL80211_CHAN_WIDTH_80P80:
319 case NL80211_CHAN_WIDTH_160:
320 phymode = MODE_UNKNOWN;
321 break;
322 }
323 break;
324 default:
325 break;
326 }
327
328 WARN_ON(phymode == MODE_UNKNOWN);
329 return phymode;
330}
331
332static u8 ath10k_parse_mpdudensity(u8 mpdudensity)
333{
334/*
335 * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
336 * 0 for no restriction
337 * 1 for 1/4 us
338 * 2 for 1/2 us
339 * 3 for 1 us
340 * 4 for 2 us
341 * 5 for 4 us
342 * 6 for 8 us
343 * 7 for 16 us
344 */
345 switch (mpdudensity) {
346 case 0:
347 return 0;
348 case 1:
349 case 2:
350 case 3:
351 /* Our lower layer calculations limit our precision to
352 1 microsecond */
353 return 1;
354 case 4:
355 return 2;
356 case 5:
357 return 4;
358 case 6:
359 return 8;
360 case 7:
361 return 16;
362 default:
363 return 0;
364 }
365}
366
367static int ath10k_peer_create(struct ath10k *ar, u32 vdev_id, const u8 *addr)
368{
369 int ret;
370
371 lockdep_assert_held(&ar->conf_mutex);
372
Michal Kaziorcfd10612014-11-25 15:16:05 +0100373 if (ar->num_peers >= ar->max_num_peers)
374 return -ENOBUFS;
375
Kalle Valo5e3dd152013-06-12 20:52:10 +0300376 ret = ath10k_wmi_peer_create(ar, vdev_id, addr);
Ben Greear479398b2013-11-04 09:19:34 -0800377 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200378 ath10k_warn(ar, "failed to create wmi peer %pM on vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +0200379 addr, vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300380 return ret;
Ben Greear479398b2013-11-04 09:19:34 -0800381 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300382
383 ret = ath10k_wait_for_peer_created(ar, vdev_id, addr);
Ben Greear479398b2013-11-04 09:19:34 -0800384 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200385 ath10k_warn(ar, "failed to wait for created wmi peer %pM on vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +0200386 addr, vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300387 return ret;
Ben Greear479398b2013-11-04 09:19:34 -0800388 }
Michal Kazior292a7532014-11-25 15:16:04 +0100389
Bartosz Markowski0e759f32014-01-02 14:38:33 +0100390 ar->num_peers++;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300391
392 return 0;
393}
394
Kalle Valo5a13e762014-01-20 11:01:46 +0200395static int ath10k_mac_set_kickout(struct ath10k_vif *arvif)
396{
397 struct ath10k *ar = arvif->ar;
398 u32 param;
399 int ret;
400
401 param = ar->wmi.pdev_param->sta_kickout_th;
402 ret = ath10k_wmi_pdev_set_param(ar, param,
403 ATH10K_KICKOUT_THRESHOLD);
404 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200405 ath10k_warn(ar, "failed to set kickout threshold on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200406 arvif->vdev_id, ret);
Kalle Valo5a13e762014-01-20 11:01:46 +0200407 return ret;
408 }
409
410 param = ar->wmi.vdev_param->ap_keepalive_min_idle_inactive_time_secs;
411 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
412 ATH10K_KEEPALIVE_MIN_IDLE);
413 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200414 ath10k_warn(ar, "failed to set keepalive minimum idle time on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200415 arvif->vdev_id, ret);
Kalle Valo5a13e762014-01-20 11:01:46 +0200416 return ret;
417 }
418
419 param = ar->wmi.vdev_param->ap_keepalive_max_idle_inactive_time_secs;
420 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
421 ATH10K_KEEPALIVE_MAX_IDLE);
422 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200423 ath10k_warn(ar, "failed to set keepalive maximum idle time on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200424 arvif->vdev_id, ret);
Kalle Valo5a13e762014-01-20 11:01:46 +0200425 return ret;
426 }
427
428 param = ar->wmi.vdev_param->ap_keepalive_max_unresponsive_time_secs;
429 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
430 ATH10K_KEEPALIVE_MAX_UNRESPONSIVE);
431 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200432 ath10k_warn(ar, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200433 arvif->vdev_id, ret);
Kalle Valo5a13e762014-01-20 11:01:46 +0200434 return ret;
435 }
436
437 return 0;
438}
439
Vivek Natarajanacab6402014-11-26 09:06:12 +0200440static int ath10k_mac_set_rts(struct ath10k_vif *arvif, u32 value)
Michal Kazior424121c2013-07-22 14:13:31 +0200441{
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200442 struct ath10k *ar = arvif->ar;
443 u32 vdev_param;
444
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200445 vdev_param = ar->wmi.vdev_param->rts_threshold;
446 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
Michal Kazior424121c2013-07-22 14:13:31 +0200447}
448
449static int ath10k_mac_set_frag(struct ath10k_vif *arvif, u32 value)
450{
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200451 struct ath10k *ar = arvif->ar;
452 u32 vdev_param;
453
Michal Kazior424121c2013-07-22 14:13:31 +0200454 if (value != 0xFFFFFFFF)
455 value = clamp_t(u32, arvif->ar->hw->wiphy->frag_threshold,
456 ATH10K_FRAGMT_THRESHOLD_MIN,
457 ATH10K_FRAGMT_THRESHOLD_MAX);
458
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200459 vdev_param = ar->wmi.vdev_param->fragmentation_threshold;
460 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
Michal Kazior424121c2013-07-22 14:13:31 +0200461}
462
Kalle Valo5e3dd152013-06-12 20:52:10 +0300463static int ath10k_peer_delete(struct ath10k *ar, u32 vdev_id, const u8 *addr)
464{
465 int ret;
466
467 lockdep_assert_held(&ar->conf_mutex);
468
469 ret = ath10k_wmi_peer_delete(ar, vdev_id, addr);
470 if (ret)
471 return ret;
472
473 ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
474 if (ret)
475 return ret;
476
Bartosz Markowski0e759f32014-01-02 14:38:33 +0100477 ar->num_peers--;
Bartosz Markowski0e759f32014-01-02 14:38:33 +0100478
Kalle Valo5e3dd152013-06-12 20:52:10 +0300479 return 0;
480}
481
482static void ath10k_peer_cleanup(struct ath10k *ar, u32 vdev_id)
483{
484 struct ath10k_peer *peer, *tmp;
485
486 lockdep_assert_held(&ar->conf_mutex);
487
488 spin_lock_bh(&ar->data_lock);
489 list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
490 if (peer->vdev_id != vdev_id)
491 continue;
492
Michal Kazior7aa7a722014-08-25 12:09:38 +0200493 ath10k_warn(ar, "removing stale peer %pM from vdev_id %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300494 peer->addr, vdev_id);
495
496 list_del(&peer->list);
497 kfree(peer);
Bartosz Markowski0e759f32014-01-02 14:38:33 +0100498 ar->num_peers--;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300499 }
500 spin_unlock_bh(&ar->data_lock);
501}
502
Michal Kaziora96d7742013-07-16 09:38:56 +0200503static void ath10k_peer_cleanup_all(struct ath10k *ar)
504{
505 struct ath10k_peer *peer, *tmp;
506
507 lockdep_assert_held(&ar->conf_mutex);
508
509 spin_lock_bh(&ar->data_lock);
510 list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
511 list_del(&peer->list);
512 kfree(peer);
513 }
514 spin_unlock_bh(&ar->data_lock);
Michal Kazior292a7532014-11-25 15:16:04 +0100515
516 ar->num_peers = 0;
Michal Kaziorcfd10612014-11-25 15:16:05 +0100517 ar->num_stations = 0;
Michal Kaziora96d7742013-07-16 09:38:56 +0200518}
519
Kalle Valo5e3dd152013-06-12 20:52:10 +0300520/************************/
521/* Interface management */
522/************************/
523
Michal Kazior64badcb2014-09-18 11:18:02 +0300524void ath10k_mac_vif_beacon_free(struct ath10k_vif *arvif)
525{
526 struct ath10k *ar = arvif->ar;
527
528 lockdep_assert_held(&ar->data_lock);
529
530 if (!arvif->beacon)
531 return;
532
533 if (!arvif->beacon_buf)
534 dma_unmap_single(ar->dev, ATH10K_SKB_CB(arvif->beacon)->paddr,
535 arvif->beacon->len, DMA_TO_DEVICE);
536
Michal Kazioraf213192015-01-29 14:29:52 +0200537 if (WARN_ON(arvif->beacon_state != ATH10K_BEACON_SCHEDULED &&
538 arvif->beacon_state != ATH10K_BEACON_SENT))
539 return;
540
Michal Kazior64badcb2014-09-18 11:18:02 +0300541 dev_kfree_skb_any(arvif->beacon);
542
543 arvif->beacon = NULL;
Michal Kazioraf213192015-01-29 14:29:52 +0200544 arvif->beacon_state = ATH10K_BEACON_SCHEDULED;
Michal Kazior64badcb2014-09-18 11:18:02 +0300545}
546
547static void ath10k_mac_vif_beacon_cleanup(struct ath10k_vif *arvif)
548{
549 struct ath10k *ar = arvif->ar;
550
551 lockdep_assert_held(&ar->data_lock);
552
553 ath10k_mac_vif_beacon_free(arvif);
554
555 if (arvif->beacon_buf) {
556 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
557 arvif->beacon_buf, arvif->beacon_paddr);
558 arvif->beacon_buf = NULL;
559 }
560}
561
Kalle Valo5e3dd152013-06-12 20:52:10 +0300562static inline int ath10k_vdev_setup_sync(struct ath10k *ar)
563{
564 int ret;
565
Michal Kazior548db542013-07-05 16:15:15 +0300566 lockdep_assert_held(&ar->conf_mutex);
567
Michal Kazior7962b0d2014-10-28 10:34:38 +0100568 if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags))
569 return -ESHUTDOWN;
570
Kalle Valo5e3dd152013-06-12 20:52:10 +0300571 ret = wait_for_completion_timeout(&ar->vdev_setup_done,
572 ATH10K_VDEV_SETUP_TIMEOUT_HZ);
573 if (ret == 0)
574 return -ETIMEDOUT;
575
576 return 0;
577}
578
Michal Kazior1bbc0972014-04-08 09:45:47 +0300579static int ath10k_monitor_vdev_start(struct ath10k *ar, int vdev_id)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300580{
Michal Kaziorc930f742014-01-23 11:38:25 +0100581 struct cfg80211_chan_def *chandef = &ar->chandef;
582 struct ieee80211_channel *channel = chandef->chan;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300583 struct wmi_vdev_start_request_arg arg = {};
Kalle Valo5e3dd152013-06-12 20:52:10 +0300584 int ret = 0;
585
586 lockdep_assert_held(&ar->conf_mutex);
587
Kalle Valo5e3dd152013-06-12 20:52:10 +0300588 arg.vdev_id = vdev_id;
589 arg.channel.freq = channel->center_freq;
Michal Kaziorc930f742014-01-23 11:38:25 +0100590 arg.channel.band_center_freq1 = chandef->center_freq1;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300591
592 /* TODO setup this dynamically, what in case we
593 don't have any vifs? */
Michal Kaziorc930f742014-01-23 11:38:25 +0100594 arg.channel.mode = chan_to_phymode(chandef);
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200595 arg.channel.chan_radar =
596 !!(channel->flags & IEEE80211_CHAN_RADAR);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300597
Michal Kazior89c5c842013-10-23 04:02:13 -0700598 arg.channel.min_power = 0;
Michal Kazior02256932013-10-23 04:02:14 -0700599 arg.channel.max_power = channel->max_power * 2;
600 arg.channel.max_reg_power = channel->max_reg_power * 2;
601 arg.channel.max_antenna_gain = channel->max_antenna_gain * 2;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300602
Michal Kazior7962b0d2014-10-28 10:34:38 +0100603 reinit_completion(&ar->vdev_setup_done);
604
Kalle Valo5e3dd152013-06-12 20:52:10 +0300605 ret = ath10k_wmi_vdev_start(ar, &arg);
606 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200607 ath10k_warn(ar, "failed to request monitor vdev %i start: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200608 vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300609 return ret;
610 }
611
612 ret = ath10k_vdev_setup_sync(ar);
613 if (ret) {
Ben Greear60028a82015-02-15 16:50:39 +0200614 ath10k_warn(ar, "failed to synchronize setup for monitor vdev %i start: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200615 vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300616 return ret;
617 }
618
619 ret = ath10k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
620 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200621 ath10k_warn(ar, "failed to put up monitor vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200622 vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300623 goto vdev_stop;
624 }
625
626 ar->monitor_vdev_id = vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300627
Michal Kazior7aa7a722014-08-25 12:09:38 +0200628 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i started\n",
Michal Kazior1bbc0972014-04-08 09:45:47 +0300629 ar->monitor_vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300630 return 0;
631
632vdev_stop:
633 ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
634 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200635 ath10k_warn(ar, "failed to stop monitor vdev %i after start failure: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200636 ar->monitor_vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300637
638 return ret;
639}
640
Michal Kazior1bbc0972014-04-08 09:45:47 +0300641static int ath10k_monitor_vdev_stop(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300642{
643 int ret = 0;
644
645 lockdep_assert_held(&ar->conf_mutex);
646
Marek Puzyniak52fa0192013-09-24 14:06:24 +0200647 ret = ath10k_wmi_vdev_down(ar, ar->monitor_vdev_id);
648 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200649 ath10k_warn(ar, "failed to put down monitor vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200650 ar->monitor_vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300651
Michal Kazior7962b0d2014-10-28 10:34:38 +0100652 reinit_completion(&ar->vdev_setup_done);
653
Kalle Valo5e3dd152013-06-12 20:52:10 +0300654 ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
655 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200656 ath10k_warn(ar, "failed to to request monitor vdev %i stop: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200657 ar->monitor_vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300658
659 ret = ath10k_vdev_setup_sync(ar);
660 if (ret)
Ben Greear60028a82015-02-15 16:50:39 +0200661 ath10k_warn(ar, "failed to synchronize monitor vdev %i stop: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200662 ar->monitor_vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300663
Michal Kazior7aa7a722014-08-25 12:09:38 +0200664 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i stopped\n",
Michal Kazior1bbc0972014-04-08 09:45:47 +0300665 ar->monitor_vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300666 return ret;
667}
668
Michal Kazior1bbc0972014-04-08 09:45:47 +0300669static int ath10k_monitor_vdev_create(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300670{
671 int bit, ret = 0;
672
673 lockdep_assert_held(&ar->conf_mutex);
674
Ben Greeara9aefb32014-08-12 11:02:19 +0300675 if (ar->free_vdev_map == 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200676 ath10k_warn(ar, "failed to find free vdev id for monitor vdev\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +0300677 return -ENOMEM;
678 }
679
Ben Greear16c11172014-09-23 14:17:16 -0700680 bit = __ffs64(ar->free_vdev_map);
Ben Greeara9aefb32014-08-12 11:02:19 +0300681
Ben Greear16c11172014-09-23 14:17:16 -0700682 ar->monitor_vdev_id = bit;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300683
684 ret = ath10k_wmi_vdev_create(ar, ar->monitor_vdev_id,
685 WMI_VDEV_TYPE_MONITOR,
686 0, ar->mac_addr);
687 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200688 ath10k_warn(ar, "failed to request monitor vdev %i creation: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200689 ar->monitor_vdev_id, ret);
Ben Greeara9aefb32014-08-12 11:02:19 +0300690 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300691 }
692
Ben Greear16c11172014-09-23 14:17:16 -0700693 ar->free_vdev_map &= ~(1LL << ar->monitor_vdev_id);
Michal Kazior7aa7a722014-08-25 12:09:38 +0200694 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d created\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300695 ar->monitor_vdev_id);
696
Kalle Valo5e3dd152013-06-12 20:52:10 +0300697 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300698}
699
Michal Kazior1bbc0972014-04-08 09:45:47 +0300700static int ath10k_monitor_vdev_delete(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300701{
702 int ret = 0;
703
704 lockdep_assert_held(&ar->conf_mutex);
705
Kalle Valo5e3dd152013-06-12 20:52:10 +0300706 ret = ath10k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
707 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200708 ath10k_warn(ar, "failed to request wmi monitor vdev %i removal: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200709 ar->monitor_vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300710 return ret;
711 }
712
Ben Greear16c11172014-09-23 14:17:16 -0700713 ar->free_vdev_map |= 1LL << ar->monitor_vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300714
Michal Kazior7aa7a722014-08-25 12:09:38 +0200715 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d deleted\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300716 ar->monitor_vdev_id);
717 return ret;
718}
719
Michal Kazior1bbc0972014-04-08 09:45:47 +0300720static int ath10k_monitor_start(struct ath10k *ar)
721{
722 int ret;
723
724 lockdep_assert_held(&ar->conf_mutex);
725
Michal Kazior1bbc0972014-04-08 09:45:47 +0300726 ret = ath10k_monitor_vdev_create(ar);
727 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200728 ath10k_warn(ar, "failed to create monitor vdev: %d\n", ret);
Michal Kazior1bbc0972014-04-08 09:45:47 +0300729 return ret;
730 }
731
732 ret = ath10k_monitor_vdev_start(ar, ar->monitor_vdev_id);
733 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200734 ath10k_warn(ar, "failed to start monitor vdev: %d\n", ret);
Michal Kazior1bbc0972014-04-08 09:45:47 +0300735 ath10k_monitor_vdev_delete(ar);
736 return ret;
737 }
738
739 ar->monitor_started = true;
Michal Kazior7aa7a722014-08-25 12:09:38 +0200740 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor started\n");
Michal Kazior1bbc0972014-04-08 09:45:47 +0300741
742 return 0;
743}
744
Michal Kazior19337472014-08-28 12:58:16 +0200745static int ath10k_monitor_stop(struct ath10k *ar)
Michal Kazior1bbc0972014-04-08 09:45:47 +0300746{
747 int ret;
748
749 lockdep_assert_held(&ar->conf_mutex);
750
Michal Kazior1bbc0972014-04-08 09:45:47 +0300751 ret = ath10k_monitor_vdev_stop(ar);
Michal Kazior19337472014-08-28 12:58:16 +0200752 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200753 ath10k_warn(ar, "failed to stop monitor vdev: %d\n", ret);
Michal Kazior19337472014-08-28 12:58:16 +0200754 return ret;
755 }
Michal Kazior1bbc0972014-04-08 09:45:47 +0300756
757 ret = ath10k_monitor_vdev_delete(ar);
Michal Kazior19337472014-08-28 12:58:16 +0200758 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200759 ath10k_warn(ar, "failed to delete monitor vdev: %d\n", ret);
Michal Kazior19337472014-08-28 12:58:16 +0200760 return ret;
761 }
Michal Kazior1bbc0972014-04-08 09:45:47 +0300762
763 ar->monitor_started = false;
Michal Kazior7aa7a722014-08-25 12:09:38 +0200764 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopped\n");
Michal Kazior19337472014-08-28 12:58:16 +0200765
766 return 0;
767}
768
769static int ath10k_monitor_recalc(struct ath10k *ar)
770{
771 bool should_start;
772
773 lockdep_assert_held(&ar->conf_mutex);
774
775 should_start = ar->monitor ||
776 ar->filter_flags & FIF_PROMISC_IN_BSS ||
777 test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
778
779 ath10k_dbg(ar, ATH10K_DBG_MAC,
780 "mac monitor recalc started? %d should? %d\n",
781 ar->monitor_started, should_start);
782
783 if (should_start == ar->monitor_started)
784 return 0;
785
786 if (should_start)
787 return ath10k_monitor_start(ar);
Kalle Valod8bb26b2014-09-14 12:50:33 +0300788
789 return ath10k_monitor_stop(ar);
Michal Kazior1bbc0972014-04-08 09:45:47 +0300790}
791
Marek Kwaczynskie81bd102014-03-11 12:58:00 +0200792static int ath10k_recalc_rtscts_prot(struct ath10k_vif *arvif)
793{
794 struct ath10k *ar = arvif->ar;
795 u32 vdev_param, rts_cts = 0;
796
797 lockdep_assert_held(&ar->conf_mutex);
798
799 vdev_param = ar->wmi.vdev_param->enable_rtscts;
800
801 if (arvif->use_cts_prot || arvif->num_legacy_stations > 0)
802 rts_cts |= SM(WMI_RTSCTS_ENABLED, WMI_RTSCTS_SET);
803
804 if (arvif->num_legacy_stations > 0)
805 rts_cts |= SM(WMI_RTSCTS_ACROSS_SW_RETRIES,
806 WMI_RTSCTS_PROFILE);
807
808 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
809 rts_cts);
810}
811
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200812static int ath10k_start_cac(struct ath10k *ar)
813{
814 int ret;
815
816 lockdep_assert_held(&ar->conf_mutex);
817
818 set_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
819
Michal Kazior19337472014-08-28 12:58:16 +0200820 ret = ath10k_monitor_recalc(ar);
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200821 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200822 ath10k_warn(ar, "failed to start monitor (cac): %d\n", ret);
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200823 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
824 return ret;
825 }
826
Michal Kazior7aa7a722014-08-25 12:09:38 +0200827 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac start monitor vdev %d\n",
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200828 ar->monitor_vdev_id);
829
830 return 0;
831}
832
833static int ath10k_stop_cac(struct ath10k *ar)
834{
835 lockdep_assert_held(&ar->conf_mutex);
836
837 /* CAC is not running - do nothing */
838 if (!test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags))
839 return 0;
840
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200841 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
Michal Kazior1bbc0972014-04-08 09:45:47 +0300842 ath10k_monitor_stop(ar);
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200843
Michal Kazior7aa7a722014-08-25 12:09:38 +0200844 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac finished\n");
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200845
846 return 0;
847}
848
Michal Kaziord6500972014-04-08 09:56:09 +0300849static void ath10k_recalc_radar_detection(struct ath10k *ar)
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200850{
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200851 int ret;
852
853 lockdep_assert_held(&ar->conf_mutex);
854
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200855 ath10k_stop_cac(ar);
856
Michal Kaziord6500972014-04-08 09:56:09 +0300857 if (!ar->radar_enabled)
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200858 return;
859
Michal Kaziord6500972014-04-08 09:56:09 +0300860 if (ar->num_started_vdevs > 0)
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200861 return;
862
863 ret = ath10k_start_cac(ar);
864 if (ret) {
865 /*
866 * Not possible to start CAC on current channel so starting
867 * radiation is not allowed, make this channel DFS_UNAVAILABLE
868 * by indicating that radar was detected.
869 */
Michal Kazior7aa7a722014-08-25 12:09:38 +0200870 ath10k_warn(ar, "failed to start CAC: %d\n", ret);
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200871 ieee80211_radar_detected(ar->hw);
872 }
873}
874
Michal Kaziordc55e302014-07-29 12:53:36 +0300875static int ath10k_vdev_start_restart(struct ath10k_vif *arvif, bool restart)
Michal Kazior72654fa2014-04-08 09:56:09 +0300876{
877 struct ath10k *ar = arvif->ar;
878 struct cfg80211_chan_def *chandef = &ar->chandef;
879 struct wmi_vdev_start_request_arg arg = {};
880 int ret = 0;
881
882 lockdep_assert_held(&ar->conf_mutex);
883
884 reinit_completion(&ar->vdev_setup_done);
885
886 arg.vdev_id = arvif->vdev_id;
887 arg.dtim_period = arvif->dtim_period;
888 arg.bcn_intval = arvif->beacon_interval;
889
890 arg.channel.freq = chandef->chan->center_freq;
891 arg.channel.band_center_freq1 = chandef->center_freq1;
892 arg.channel.mode = chan_to_phymode(chandef);
893
894 arg.channel.min_power = 0;
895 arg.channel.max_power = chandef->chan->max_power * 2;
896 arg.channel.max_reg_power = chandef->chan->max_reg_power * 2;
897 arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
898
899 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
900 arg.ssid = arvif->u.ap.ssid;
901 arg.ssid_len = arvif->u.ap.ssid_len;
902 arg.hidden_ssid = arvif->u.ap.hidden_ssid;
903
904 /* For now allow DFS for AP mode */
905 arg.channel.chan_radar =
906 !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
907 } else if (arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
908 arg.ssid = arvif->vif->bss_conf.ssid;
909 arg.ssid_len = arvif->vif->bss_conf.ssid_len;
910 }
911
Michal Kazior7aa7a722014-08-25 12:09:38 +0200912 ath10k_dbg(ar, ATH10K_DBG_MAC,
Michal Kazior72654fa2014-04-08 09:56:09 +0300913 "mac vdev %d start center_freq %d phymode %s\n",
914 arg.vdev_id, arg.channel.freq,
915 ath10k_wmi_phymode_str(arg.channel.mode));
916
Michal Kaziordc55e302014-07-29 12:53:36 +0300917 if (restart)
918 ret = ath10k_wmi_vdev_restart(ar, &arg);
919 else
920 ret = ath10k_wmi_vdev_start(ar, &arg);
921
Michal Kazior72654fa2014-04-08 09:56:09 +0300922 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200923 ath10k_warn(ar, "failed to start WMI vdev %i: %d\n",
Michal Kazior72654fa2014-04-08 09:56:09 +0300924 arg.vdev_id, ret);
925 return ret;
926 }
927
928 ret = ath10k_vdev_setup_sync(ar);
929 if (ret) {
Ben Greear60028a82015-02-15 16:50:39 +0200930 ath10k_warn(ar,
931 "failed to synchronize setup for vdev %i restart %d: %d\n",
932 arg.vdev_id, restart, ret);
Michal Kazior72654fa2014-04-08 09:56:09 +0300933 return ret;
934 }
935
Michal Kaziord6500972014-04-08 09:56:09 +0300936 ar->num_started_vdevs++;
937 ath10k_recalc_radar_detection(ar);
938
Michal Kazior72654fa2014-04-08 09:56:09 +0300939 return ret;
940}
941
Michal Kaziordc55e302014-07-29 12:53:36 +0300942static int ath10k_vdev_start(struct ath10k_vif *arvif)
943{
944 return ath10k_vdev_start_restart(arvif, false);
945}
946
947static int ath10k_vdev_restart(struct ath10k_vif *arvif)
948{
949 return ath10k_vdev_start_restart(arvif, true);
950}
951
Michal Kazior72654fa2014-04-08 09:56:09 +0300952static int ath10k_vdev_stop(struct ath10k_vif *arvif)
953{
954 struct ath10k *ar = arvif->ar;
955 int ret;
956
957 lockdep_assert_held(&ar->conf_mutex);
958
959 reinit_completion(&ar->vdev_setup_done);
960
961 ret = ath10k_wmi_vdev_stop(ar, arvif->vdev_id);
962 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200963 ath10k_warn(ar, "failed to stop WMI vdev %i: %d\n",
Michal Kazior72654fa2014-04-08 09:56:09 +0300964 arvif->vdev_id, ret);
965 return ret;
966 }
967
968 ret = ath10k_vdev_setup_sync(ar);
969 if (ret) {
Ben Greear60028a82015-02-15 16:50:39 +0200970 ath10k_warn(ar, "failed to synchronize setup for vdev %i stop: %d\n",
Michal Kazior72654fa2014-04-08 09:56:09 +0300971 arvif->vdev_id, ret);
972 return ret;
973 }
974
Michal Kaziord6500972014-04-08 09:56:09 +0300975 WARN_ON(ar->num_started_vdevs == 0);
976
977 if (ar->num_started_vdevs != 0) {
978 ar->num_started_vdevs--;
979 ath10k_recalc_radar_detection(ar);
980 }
981
Michal Kazior72654fa2014-04-08 09:56:09 +0300982 return ret;
983}
984
Michal Kaziorfbb8f1b2015-01-13 16:30:12 +0200985static int ath10k_mac_setup_bcn_p2p_ie(struct ath10k_vif *arvif,
986 struct sk_buff *bcn)
987{
988 struct ath10k *ar = arvif->ar;
989 struct ieee80211_mgmt *mgmt;
990 const u8 *p2p_ie;
991 int ret;
992
993 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
994 return 0;
995
996 if (arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
997 return 0;
998
999 mgmt = (void *)bcn->data;
1000 p2p_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1001 mgmt->u.beacon.variable,
1002 bcn->len - (mgmt->u.beacon.variable -
1003 bcn->data));
1004 if (!p2p_ie)
1005 return -ENOENT;
1006
1007 ret = ath10k_wmi_p2p_go_bcn_ie(ar, arvif->vdev_id, p2p_ie);
1008 if (ret) {
1009 ath10k_warn(ar, "failed to submit p2p go bcn ie for vdev %i: %d\n",
1010 arvif->vdev_id, ret);
1011 return ret;
1012 }
1013
1014 return 0;
1015}
1016
1017static int ath10k_mac_remove_vendor_ie(struct sk_buff *skb, unsigned int oui,
1018 u8 oui_type, size_t ie_offset)
1019{
1020 size_t len;
1021 const u8 *next;
1022 const u8 *end;
1023 u8 *ie;
1024
1025 if (WARN_ON(skb->len < ie_offset))
1026 return -EINVAL;
1027
1028 ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
1029 skb->data + ie_offset,
1030 skb->len - ie_offset);
1031 if (!ie)
1032 return -ENOENT;
1033
1034 len = ie[1] + 2;
1035 end = skb->data + skb->len;
1036 next = ie + len;
1037
1038 if (WARN_ON(next > end))
1039 return -EINVAL;
1040
1041 memmove(ie, next, end - next);
1042 skb_trim(skb, skb->len - len);
1043
1044 return 0;
1045}
1046
1047static int ath10k_mac_setup_bcn_tmpl(struct ath10k_vif *arvif)
1048{
1049 struct ath10k *ar = arvif->ar;
1050 struct ieee80211_hw *hw = ar->hw;
1051 struct ieee80211_vif *vif = arvif->vif;
1052 struct ieee80211_mutable_offsets offs = {};
1053 struct sk_buff *bcn;
1054 int ret;
1055
1056 if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1057 return 0;
1058
1059 bcn = ieee80211_beacon_get_template(hw, vif, &offs);
1060 if (!bcn) {
1061 ath10k_warn(ar, "failed to get beacon template from mac80211\n");
1062 return -EPERM;
1063 }
1064
1065 ret = ath10k_mac_setup_bcn_p2p_ie(arvif, bcn);
1066 if (ret) {
1067 ath10k_warn(ar, "failed to setup p2p go bcn ie: %d\n", ret);
1068 kfree_skb(bcn);
1069 return ret;
1070 }
1071
1072 /* P2P IE is inserted by firmware automatically (as configured above)
1073 * so remove it from the base beacon template to avoid duplicate P2P
1074 * IEs in beacon frames.
1075 */
1076 ath10k_mac_remove_vendor_ie(bcn, WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1077 offsetof(struct ieee80211_mgmt,
1078 u.beacon.variable));
1079
1080 ret = ath10k_wmi_bcn_tmpl(ar, arvif->vdev_id, offs.tim_offset, bcn, 0,
1081 0, NULL, 0);
1082 kfree_skb(bcn);
1083
1084 if (ret) {
1085 ath10k_warn(ar, "failed to submit beacon template command: %d\n",
1086 ret);
1087 return ret;
1088 }
1089
1090 return 0;
1091}
1092
1093static int ath10k_mac_setup_prb_tmpl(struct ath10k_vif *arvif)
1094{
1095 struct ath10k *ar = arvif->ar;
1096 struct ieee80211_hw *hw = ar->hw;
1097 struct ieee80211_vif *vif = arvif->vif;
1098 struct sk_buff *prb;
1099 int ret;
1100
1101 if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1102 return 0;
1103
1104 prb = ieee80211_proberesp_get(hw, vif);
1105 if (!prb) {
1106 ath10k_warn(ar, "failed to get probe resp template from mac80211\n");
1107 return -EPERM;
1108 }
1109
1110 ret = ath10k_wmi_prb_tmpl(ar, arvif->vdev_id, prb);
1111 kfree_skb(prb);
1112
1113 if (ret) {
1114 ath10k_warn(ar, "failed to submit probe resp template command: %d\n",
1115 ret);
1116 return ret;
1117 }
1118
1119 return 0;
1120}
1121
Kalle Valo5e3dd152013-06-12 20:52:10 +03001122static void ath10k_control_beaconing(struct ath10k_vif *arvif,
Kalle Valo5b07e072014-09-14 12:50:06 +03001123 struct ieee80211_bss_conf *info)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001124{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001125 struct ath10k *ar = arvif->ar;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001126 int ret = 0;
1127
Michal Kazior548db542013-07-05 16:15:15 +03001128 lockdep_assert_held(&arvif->ar->conf_mutex);
1129
Kalle Valo5e3dd152013-06-12 20:52:10 +03001130 if (!info->enable_beacon) {
1131 ath10k_vdev_stop(arvif);
Michal Kaziorc930f742014-01-23 11:38:25 +01001132
1133 arvif->is_started = false;
1134 arvif->is_up = false;
1135
Michal Kazior748afc42014-01-23 12:48:21 +01001136 spin_lock_bh(&arvif->ar->data_lock);
Michal Kazior64badcb2014-09-18 11:18:02 +03001137 ath10k_mac_vif_beacon_free(arvif);
Michal Kazior748afc42014-01-23 12:48:21 +01001138 spin_unlock_bh(&arvif->ar->data_lock);
1139
Kalle Valo5e3dd152013-06-12 20:52:10 +03001140 return;
1141 }
1142
1143 arvif->tx_seq_no = 0x1000;
1144
1145 ret = ath10k_vdev_start(arvif);
1146 if (ret)
1147 return;
1148
Michal Kaziorc930f742014-01-23 11:38:25 +01001149 arvif->aid = 0;
Kalle Valob25f32c2014-09-14 12:50:49 +03001150 ether_addr_copy(arvif->bssid, info->bssid);
Michal Kaziorc930f742014-01-23 11:38:25 +01001151
1152 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
1153 arvif->bssid);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001154 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001155 ath10k_warn(ar, "failed to bring up vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02001156 arvif->vdev_id, ret);
Michal Kaziorc930f742014-01-23 11:38:25 +01001157 ath10k_vdev_stop(arvif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001158 return;
1159 }
Michal Kaziorc930f742014-01-23 11:38:25 +01001160
1161 arvif->is_started = true;
1162 arvif->is_up = true;
1163
Michal Kazior7aa7a722014-08-25 12:09:38 +02001164 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001165}
1166
1167static void ath10k_control_ibss(struct ath10k_vif *arvif,
1168 struct ieee80211_bss_conf *info,
1169 const u8 self_peer[ETH_ALEN])
1170{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001171 struct ath10k *ar = arvif->ar;
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02001172 u32 vdev_param;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001173 int ret = 0;
1174
Michal Kazior548db542013-07-05 16:15:15 +03001175 lockdep_assert_held(&arvif->ar->conf_mutex);
1176
Kalle Valo5e3dd152013-06-12 20:52:10 +03001177 if (!info->ibss_joined) {
1178 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, self_peer);
1179 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001180 ath10k_warn(ar, "failed to delete IBSS self peer %pM for vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001181 self_peer, arvif->vdev_id, ret);
1182
Michal Kaziorc930f742014-01-23 11:38:25 +01001183 if (is_zero_ether_addr(arvif->bssid))
Kalle Valo5e3dd152013-06-12 20:52:10 +03001184 return;
1185
Joe Perches93803b32015-03-02 19:54:49 -08001186 eth_zero_addr(arvif->bssid);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001187
1188 return;
1189 }
1190
1191 ret = ath10k_peer_create(arvif->ar, arvif->vdev_id, self_peer);
1192 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001193 ath10k_warn(ar, "failed to create IBSS self peer %pM for vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001194 self_peer, arvif->vdev_id, ret);
1195 return;
1196 }
1197
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02001198 vdev_param = arvif->ar->wmi.vdev_param->atim_window;
1199 ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001200 ATH10K_DEFAULT_ATIM);
1201 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001202 ath10k_warn(ar, "failed to set IBSS ATIM for vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001203 arvif->vdev_id, ret);
1204}
1205
Michal Kazior9f9b5742014-12-12 12:41:36 +01001206static int ath10k_mac_vif_recalc_ps_wake_threshold(struct ath10k_vif *arvif)
1207{
1208 struct ath10k *ar = arvif->ar;
1209 u32 param;
1210 u32 value;
1211 int ret;
1212
1213 lockdep_assert_held(&arvif->ar->conf_mutex);
1214
1215 if (arvif->u.sta.uapsd)
1216 value = WMI_STA_PS_TX_WAKE_THRESHOLD_NEVER;
1217 else
1218 value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
1219
1220 param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
1221 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param, value);
1222 if (ret) {
1223 ath10k_warn(ar, "failed to submit ps wake threshold %u on vdev %i: %d\n",
1224 value, arvif->vdev_id, ret);
1225 return ret;
1226 }
1227
1228 return 0;
1229}
1230
1231static int ath10k_mac_vif_recalc_ps_poll_count(struct ath10k_vif *arvif)
1232{
1233 struct ath10k *ar = arvif->ar;
1234 u32 param;
1235 u32 value;
1236 int ret;
1237
1238 lockdep_assert_held(&arvif->ar->conf_mutex);
1239
1240 if (arvif->u.sta.uapsd)
1241 value = WMI_STA_PS_PSPOLL_COUNT_UAPSD;
1242 else
1243 value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
1244
1245 param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
1246 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
1247 param, value);
1248 if (ret) {
1249 ath10k_warn(ar, "failed to submit ps poll count %u on vdev %i: %d\n",
1250 value, arvif->vdev_id, ret);
1251 return ret;
1252 }
1253
1254 return 0;
1255}
1256
Michal Kaziorcffb41f2015-02-13 13:30:16 +01001257static int ath10k_mac_ps_vif_count(struct ath10k *ar)
1258{
1259 struct ath10k_vif *arvif;
1260 int num = 0;
1261
1262 lockdep_assert_held(&ar->conf_mutex);
1263
1264 list_for_each_entry(arvif, &ar->arvifs, list)
1265 if (arvif->ps)
1266 num++;
1267
1268 return num;
1269}
1270
Michal Kaziorad088bf2013-10-16 15:44:46 +03001271static int ath10k_mac_vif_setup_ps(struct ath10k_vif *arvif)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001272{
Michal Kaziorad088bf2013-10-16 15:44:46 +03001273 struct ath10k *ar = arvif->ar;
Michal Kazior526549a2014-12-12 12:41:37 +01001274 struct ieee80211_vif *vif = arvif->vif;
Michal Kaziorad088bf2013-10-16 15:44:46 +03001275 struct ieee80211_conf *conf = &ar->hw->conf;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001276 enum wmi_sta_powersave_param param;
1277 enum wmi_sta_ps_mode psmode;
1278 int ret;
Michal Kazior526549a2014-12-12 12:41:37 +01001279 int ps_timeout;
Michal Kaziorcffb41f2015-02-13 13:30:16 +01001280 bool enable_ps;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001281
Michal Kazior548db542013-07-05 16:15:15 +03001282 lockdep_assert_held(&arvif->ar->conf_mutex);
1283
Michal Kaziorad088bf2013-10-16 15:44:46 +03001284 if (arvif->vif->type != NL80211_IFTYPE_STATION)
1285 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001286
Michal Kaziorcffb41f2015-02-13 13:30:16 +01001287 enable_ps = arvif->ps;
1288
1289 if (enable_ps && ath10k_mac_ps_vif_count(ar) > 1 &&
1290 !test_bit(ATH10K_FW_FEATURE_MULTI_VIF_PS_SUPPORT,
1291 ar->fw_features)) {
1292 ath10k_warn(ar, "refusing to enable ps on vdev %i: not supported by fw\n",
1293 arvif->vdev_id);
1294 enable_ps = false;
1295 }
1296
1297 if (enable_ps) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001298 psmode = WMI_STA_PS_MODE_ENABLED;
1299 param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
1300
Michal Kazior526549a2014-12-12 12:41:37 +01001301 ps_timeout = conf->dynamic_ps_timeout;
1302 if (ps_timeout == 0) {
1303 /* Firmware doesn't like 0 */
1304 ps_timeout = ieee80211_tu_to_usec(
1305 vif->bss_conf.beacon_int) / 1000;
1306 }
1307
Michal Kaziorad088bf2013-10-16 15:44:46 +03001308 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
Michal Kazior526549a2014-12-12 12:41:37 +01001309 ps_timeout);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001310 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001311 ath10k_warn(ar, "failed to set inactivity time for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02001312 arvif->vdev_id, ret);
Michal Kaziorad088bf2013-10-16 15:44:46 +03001313 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001314 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001315 } else {
1316 psmode = WMI_STA_PS_MODE_DISABLED;
1317 }
1318
Michal Kazior7aa7a722014-08-25 12:09:38 +02001319 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d psmode %s\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03001320 arvif->vdev_id, psmode ? "enable" : "disable");
1321
Michal Kaziorad088bf2013-10-16 15:44:46 +03001322 ret = ath10k_wmi_set_psmode(ar, arvif->vdev_id, psmode);
1323 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001324 ath10k_warn(ar, "failed to set PS Mode %d for vdev %d: %d\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02001325 psmode, arvif->vdev_id, ret);
Michal Kaziorad088bf2013-10-16 15:44:46 +03001326 return ret;
1327 }
1328
1329 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001330}
1331
Michal Kazior46725b152015-01-28 09:57:49 +02001332static int ath10k_mac_vif_disable_keepalive(struct ath10k_vif *arvif)
1333{
1334 struct ath10k *ar = arvif->ar;
1335 struct wmi_sta_keepalive_arg arg = {};
1336 int ret;
1337
1338 lockdep_assert_held(&arvif->ar->conf_mutex);
1339
1340 if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
1341 return 0;
1342
1343 if (!test_bit(WMI_SERVICE_STA_KEEP_ALIVE, ar->wmi.svc_map))
1344 return 0;
1345
1346 /* Some firmware revisions have a bug and ignore the `enabled` field.
1347 * Instead use the interval to disable the keepalive.
1348 */
1349 arg.vdev_id = arvif->vdev_id;
1350 arg.enabled = 1;
1351 arg.method = WMI_STA_KEEPALIVE_METHOD_NULL_FRAME;
1352 arg.interval = WMI_STA_KEEPALIVE_INTERVAL_DISABLE;
1353
1354 ret = ath10k_wmi_sta_keepalive(ar, &arg);
1355 if (ret) {
1356 ath10k_warn(ar, "failed to submit keepalive on vdev %i: %d\n",
1357 arvif->vdev_id, ret);
1358 return ret;
1359 }
1360
1361 return 0;
1362}
1363
Kalle Valo5e3dd152013-06-12 20:52:10 +03001364/**********************/
1365/* Station management */
1366/**********************/
1367
Michal Kazior590922a2014-10-21 10:10:29 +03001368static u32 ath10k_peer_assoc_h_listen_intval(struct ath10k *ar,
1369 struct ieee80211_vif *vif)
1370{
1371 /* Some firmware revisions have unstable STA powersave when listen
1372 * interval is set too high (e.g. 5). The symptoms are firmware doesn't
1373 * generate NullFunc frames properly even if buffered frames have been
1374 * indicated in Beacon TIM. Firmware would seldom wake up to pull
1375 * buffered frames. Often pinging the device from AP would simply fail.
1376 *
1377 * As a workaround set it to 1.
1378 */
1379 if (vif->type == NL80211_IFTYPE_STATION)
1380 return 1;
1381
1382 return ar->hw->conf.listen_interval;
1383}
1384
Kalle Valo5e3dd152013-06-12 20:52:10 +03001385static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001386 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001387 struct ieee80211_sta *sta,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001388 struct wmi_peer_assoc_complete_arg *arg)
1389{
Michal Kazior590922a2014-10-21 10:10:29 +03001390 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1391
Michal Kazior548db542013-07-05 16:15:15 +03001392 lockdep_assert_held(&ar->conf_mutex);
1393
Kalle Valob25f32c2014-09-14 12:50:49 +03001394 ether_addr_copy(arg->addr, sta->addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001395 arg->vdev_id = arvif->vdev_id;
1396 arg->peer_aid = sta->aid;
1397 arg->peer_flags |= WMI_PEER_AUTH;
Michal Kazior590922a2014-10-21 10:10:29 +03001398 arg->peer_listen_intval = ath10k_peer_assoc_h_listen_intval(ar, vif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001399 arg->peer_num_spatial_streams = 1;
Michal Kazior590922a2014-10-21 10:10:29 +03001400 arg->peer_caps = vif->bss_conf.assoc_capability;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001401}
1402
1403static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001404 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001405 struct wmi_peer_assoc_complete_arg *arg)
1406{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001407 struct ieee80211_bss_conf *info = &vif->bss_conf;
1408 struct cfg80211_bss *bss;
1409 const u8 *rsnie = NULL;
1410 const u8 *wpaie = NULL;
1411
Michal Kazior548db542013-07-05 16:15:15 +03001412 lockdep_assert_held(&ar->conf_mutex);
1413
Kalle Valo5e3dd152013-06-12 20:52:10 +03001414 bss = cfg80211_get_bss(ar->hw->wiphy, ar->hw->conf.chandef.chan,
Dedy Lansky6eb18132015-02-08 15:52:03 +02001415 info->bssid, NULL, 0, IEEE80211_BSS_TYPE_ANY,
1416 IEEE80211_PRIVACY_ANY);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001417 if (bss) {
1418 const struct cfg80211_bss_ies *ies;
1419
1420 rcu_read_lock();
1421 rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
1422
1423 ies = rcu_dereference(bss->ies);
1424
1425 wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
Kalle Valo5b07e072014-09-14 12:50:06 +03001426 WLAN_OUI_TYPE_MICROSOFT_WPA,
1427 ies->data,
1428 ies->len);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001429 rcu_read_unlock();
1430 cfg80211_put_bss(ar->hw->wiphy, bss);
1431 }
1432
1433 /* FIXME: base on RSN IE/WPA IE is a correct idea? */
1434 if (rsnie || wpaie) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001435 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001436 arg->peer_flags |= WMI_PEER_NEED_PTK_4_WAY;
1437 }
1438
1439 if (wpaie) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001440 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: wpa ie found\n", __func__);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001441 arg->peer_flags |= WMI_PEER_NEED_GTK_2_WAY;
1442 }
1443}
1444
1445static void ath10k_peer_assoc_h_rates(struct ath10k *ar,
1446 struct ieee80211_sta *sta,
1447 struct wmi_peer_assoc_complete_arg *arg)
1448{
1449 struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
1450 const struct ieee80211_supported_band *sband;
1451 const struct ieee80211_rate *rates;
1452 u32 ratemask;
1453 int i;
1454
Michal Kazior548db542013-07-05 16:15:15 +03001455 lockdep_assert_held(&ar->conf_mutex);
1456
Kalle Valo5e3dd152013-06-12 20:52:10 +03001457 sband = ar->hw->wiphy->bands[ar->hw->conf.chandef.chan->band];
1458 ratemask = sta->supp_rates[ar->hw->conf.chandef.chan->band];
1459 rates = sband->bitrates;
1460
1461 rateset->num_rates = 0;
1462
1463 for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
1464 if (!(ratemask & 1))
1465 continue;
1466
1467 rateset->rates[rateset->num_rates] = rates->hw_value;
1468 rateset->num_rates++;
1469 }
1470}
1471
1472static void ath10k_peer_assoc_h_ht(struct ath10k *ar,
1473 struct ieee80211_sta *sta,
1474 struct wmi_peer_assoc_complete_arg *arg)
1475{
1476 const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001477 int i, n;
Kalle Valoaf762c02014-09-14 12:50:17 +03001478 u32 stbc;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001479
Michal Kazior548db542013-07-05 16:15:15 +03001480 lockdep_assert_held(&ar->conf_mutex);
1481
Kalle Valo5e3dd152013-06-12 20:52:10 +03001482 if (!ht_cap->ht_supported)
1483 return;
1484
1485 arg->peer_flags |= WMI_PEER_HT;
1486 arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1487 ht_cap->ampdu_factor)) - 1;
1488
1489 arg->peer_mpdu_density =
1490 ath10k_parse_mpdudensity(ht_cap->ampdu_density);
1491
1492 arg->peer_ht_caps = ht_cap->cap;
1493 arg->peer_rate_caps |= WMI_RC_HT_FLAG;
1494
1495 if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
1496 arg->peer_flags |= WMI_PEER_LDPC;
1497
1498 if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
1499 arg->peer_flags |= WMI_PEER_40MHZ;
1500 arg->peer_rate_caps |= WMI_RC_CW40_FLAG;
1501 }
1502
1503 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
1504 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
1505
1506 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
1507 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
1508
1509 if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
1510 arg->peer_rate_caps |= WMI_RC_TX_STBC_FLAG;
1511 arg->peer_flags |= WMI_PEER_STBC;
1512 }
1513
1514 if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001515 stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
1516 stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
1517 stbc = stbc << WMI_RC_RX_STBC_FLAG_S;
1518 arg->peer_rate_caps |= stbc;
1519 arg->peer_flags |= WMI_PEER_STBC;
1520 }
1521
Kalle Valo5e3dd152013-06-12 20:52:10 +03001522 if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
1523 arg->peer_rate_caps |= WMI_RC_TS_FLAG;
1524 else if (ht_cap->mcs.rx_mask[1])
1525 arg->peer_rate_caps |= WMI_RC_DS_FLAG;
1526
1527 for (i = 0, n = 0; i < IEEE80211_HT_MCS_MASK_LEN*8; i++)
1528 if (ht_cap->mcs.rx_mask[i/8] & (1 << i%8))
1529 arg->peer_ht_rates.rates[n++] = i;
1530
Bartosz Markowskifd71f802014-02-10 13:12:55 +01001531 /*
1532 * This is a workaround for HT-enabled STAs which break the spec
1533 * and have no HT capabilities RX mask (no HT RX MCS map).
1534 *
1535 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
1536 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
1537 *
1538 * Firmware asserts if such situation occurs.
1539 */
1540 if (n == 0) {
1541 arg->peer_ht_rates.num_rates = 8;
1542 for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
1543 arg->peer_ht_rates.rates[i] = i;
1544 } else {
1545 arg->peer_ht_rates.num_rates = n;
1546 arg->peer_num_spatial_streams = sta->rx_nss;
1547 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001548
Michal Kazior7aa7a722014-08-25 12:09:38 +02001549 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03001550 arg->addr,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001551 arg->peer_ht_rates.num_rates,
1552 arg->peer_num_spatial_streams);
1553}
1554
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001555static int ath10k_peer_assoc_qos_ap(struct ath10k *ar,
1556 struct ath10k_vif *arvif,
1557 struct ieee80211_sta *sta)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001558{
1559 u32 uapsd = 0;
1560 u32 max_sp = 0;
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001561 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001562
Michal Kazior548db542013-07-05 16:15:15 +03001563 lockdep_assert_held(&ar->conf_mutex);
1564
Kalle Valo5e3dd152013-06-12 20:52:10 +03001565 if (sta->wme && sta->uapsd_queues) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001566 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001567 sta->uapsd_queues, sta->max_sp);
1568
Kalle Valo5e3dd152013-06-12 20:52:10 +03001569 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1570 uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
1571 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
1572 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1573 uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
1574 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
1575 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1576 uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
1577 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
1578 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1579 uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
1580 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
1581
Kalle Valo5e3dd152013-06-12 20:52:10 +03001582 if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
1583 max_sp = sta->max_sp;
1584
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001585 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1586 sta->addr,
1587 WMI_AP_PS_PEER_PARAM_UAPSD,
1588 uapsd);
1589 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001590 ath10k_warn(ar, "failed to set ap ps peer param uapsd for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001591 arvif->vdev_id, ret);
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001592 return ret;
1593 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001594
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001595 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1596 sta->addr,
1597 WMI_AP_PS_PEER_PARAM_MAX_SP,
1598 max_sp);
1599 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001600 ath10k_warn(ar, "failed to set ap ps peer param max sp for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001601 arvif->vdev_id, ret);
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001602 return ret;
1603 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001604
1605 /* TODO setup this based on STA listen interval and
1606 beacon interval. Currently we don't know
1607 sta->listen_interval - mac80211 patch required.
1608 Currently use 10 seconds */
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001609 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id, sta->addr,
Kalle Valo5b07e072014-09-14 12:50:06 +03001610 WMI_AP_PS_PEER_PARAM_AGEOUT_TIME,
1611 10);
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001612 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001613 ath10k_warn(ar, "failed to set ap ps peer param ageout time for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001614 arvif->vdev_id, ret);
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001615 return ret;
1616 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001617 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001618
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001619 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001620}
1621
1622static void ath10k_peer_assoc_h_vht(struct ath10k *ar,
1623 struct ieee80211_sta *sta,
1624 struct wmi_peer_assoc_complete_arg *arg)
1625{
1626 const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
Sujith Manoharana24b88b2013-10-07 19:51:57 -07001627 u8 ampdu_factor;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001628
1629 if (!vht_cap->vht_supported)
1630 return;
1631
1632 arg->peer_flags |= WMI_PEER_VHT;
Yanbo Lid68bb122015-01-23 08:18:20 +08001633
1634 if (ar->hw->conf.chandef.chan->band == IEEE80211_BAND_2GHZ)
1635 arg->peer_flags |= WMI_PEER_VHT_2G;
1636
Kalle Valo5e3dd152013-06-12 20:52:10 +03001637 arg->peer_vht_caps = vht_cap->cap;
1638
Sujith Manoharana24b88b2013-10-07 19:51:57 -07001639 ampdu_factor = (vht_cap->cap &
1640 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
1641 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
1642
1643 /* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
1644 * zero in VHT IE. Using it would result in degraded throughput.
1645 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
1646 * it if VHT max_mpdu is smaller. */
1647 arg->peer_max_mpdu = max(arg->peer_max_mpdu,
1648 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1649 ampdu_factor)) - 1);
1650
Kalle Valo5e3dd152013-06-12 20:52:10 +03001651 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1652 arg->peer_flags |= WMI_PEER_80MHZ;
1653
1654 arg->peer_vht_rates.rx_max_rate =
1655 __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
1656 arg->peer_vht_rates.rx_mcs_set =
1657 __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
1658 arg->peer_vht_rates.tx_max_rate =
1659 __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
1660 arg->peer_vht_rates.tx_mcs_set =
1661 __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
1662
Michal Kazior7aa7a722014-08-25 12:09:38 +02001663 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03001664 sta->addr, arg->peer_max_mpdu, arg->peer_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001665}
1666
1667static void ath10k_peer_assoc_h_qos(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001668 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001669 struct ieee80211_sta *sta,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001670 struct wmi_peer_assoc_complete_arg *arg)
1671{
Michal Kazior590922a2014-10-21 10:10:29 +03001672 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1673
Kalle Valo5e3dd152013-06-12 20:52:10 +03001674 switch (arvif->vdev_type) {
1675 case WMI_VDEV_TYPE_AP:
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001676 if (sta->wme)
1677 arg->peer_flags |= WMI_PEER_QOS;
1678
1679 if (sta->wme && sta->uapsd_queues) {
1680 arg->peer_flags |= WMI_PEER_APSD;
1681 arg->peer_rate_caps |= WMI_RC_UAPSD_FLAG;
1682 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001683 break;
1684 case WMI_VDEV_TYPE_STA:
Michal Kazior590922a2014-10-21 10:10:29 +03001685 if (vif->bss_conf.qos)
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001686 arg->peer_flags |= WMI_PEER_QOS;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001687 break;
Janusz Dziedzic627d9842014-12-17 12:29:54 +02001688 case WMI_VDEV_TYPE_IBSS:
1689 if (sta->wme)
1690 arg->peer_flags |= WMI_PEER_QOS;
1691 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001692 default:
1693 break;
1694 }
Janusz Dziedzic627d9842014-12-17 12:29:54 +02001695
1696 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM qos %d\n",
1697 sta->addr, !!(arg->peer_flags & WMI_PEER_QOS));
Kalle Valo5e3dd152013-06-12 20:52:10 +03001698}
1699
Michal Kazior91b12082014-12-12 12:41:35 +01001700static bool ath10k_mac_sta_has_11g_rates(struct ieee80211_sta *sta)
1701{
1702 /* First 4 rates in ath10k_rates are CCK (11b) rates. */
1703 return sta->supp_rates[IEEE80211_BAND_2GHZ] >> 4;
1704}
1705
Kalle Valo5e3dd152013-06-12 20:52:10 +03001706static void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001707 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001708 struct ieee80211_sta *sta,
1709 struct wmi_peer_assoc_complete_arg *arg)
1710{
1711 enum wmi_phy_mode phymode = MODE_UNKNOWN;
1712
Kalle Valo5e3dd152013-06-12 20:52:10 +03001713 switch (ar->hw->conf.chandef.chan->band) {
1714 case IEEE80211_BAND_2GHZ:
Yanbo Lid68bb122015-01-23 08:18:20 +08001715 if (sta->vht_cap.vht_supported) {
1716 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1717 phymode = MODE_11AC_VHT40;
1718 else
1719 phymode = MODE_11AC_VHT20;
1720 } else if (sta->ht_cap.ht_supported) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001721 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1722 phymode = MODE_11NG_HT40;
1723 else
1724 phymode = MODE_11NG_HT20;
Michal Kazior91b12082014-12-12 12:41:35 +01001725 } else if (ath10k_mac_sta_has_11g_rates(sta)) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001726 phymode = MODE_11G;
Michal Kazior91b12082014-12-12 12:41:35 +01001727 } else {
1728 phymode = MODE_11B;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001729 }
1730
1731 break;
1732 case IEEE80211_BAND_5GHZ:
Sujith Manoharan7cc45e92013-09-08 18:19:55 +03001733 /*
1734 * Check VHT first.
1735 */
1736 if (sta->vht_cap.vht_supported) {
1737 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1738 phymode = MODE_11AC_VHT80;
1739 else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1740 phymode = MODE_11AC_VHT40;
1741 else if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
1742 phymode = MODE_11AC_VHT20;
1743 } else if (sta->ht_cap.ht_supported) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001744 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1745 phymode = MODE_11NA_HT40;
1746 else
1747 phymode = MODE_11NA_HT20;
1748 } else {
1749 phymode = MODE_11A;
1750 }
1751
1752 break;
1753 default:
1754 break;
1755 }
1756
Michal Kazior7aa7a722014-08-25 12:09:38 +02001757 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM phymode %s\n",
Kalle Valo38a1d472013-09-08 17:56:14 +03001758 sta->addr, ath10k_wmi_phymode_str(phymode));
Kalle Valo60c3daa2013-09-08 17:56:07 +03001759
Kalle Valo5e3dd152013-06-12 20:52:10 +03001760 arg->peer_phymode = phymode;
1761 WARN_ON(phymode == MODE_UNKNOWN);
1762}
1763
Kalle Valob9ada652013-10-16 15:44:46 +03001764static int ath10k_peer_assoc_prepare(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001765 struct ieee80211_vif *vif,
Kalle Valob9ada652013-10-16 15:44:46 +03001766 struct ieee80211_sta *sta,
Kalle Valob9ada652013-10-16 15:44:46 +03001767 struct wmi_peer_assoc_complete_arg *arg)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001768{
Michal Kazior548db542013-07-05 16:15:15 +03001769 lockdep_assert_held(&ar->conf_mutex);
1770
Kalle Valob9ada652013-10-16 15:44:46 +03001771 memset(arg, 0, sizeof(*arg));
Kalle Valo5e3dd152013-06-12 20:52:10 +03001772
Michal Kazior590922a2014-10-21 10:10:29 +03001773 ath10k_peer_assoc_h_basic(ar, vif, sta, arg);
1774 ath10k_peer_assoc_h_crypto(ar, vif, arg);
Kalle Valob9ada652013-10-16 15:44:46 +03001775 ath10k_peer_assoc_h_rates(ar, sta, arg);
1776 ath10k_peer_assoc_h_ht(ar, sta, arg);
1777 ath10k_peer_assoc_h_vht(ar, sta, arg);
Michal Kazior590922a2014-10-21 10:10:29 +03001778 ath10k_peer_assoc_h_qos(ar, vif, sta, arg);
1779 ath10k_peer_assoc_h_phymode(ar, vif, sta, arg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001780
Kalle Valob9ada652013-10-16 15:44:46 +03001781 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001782}
1783
Michal Kazior90046f52014-02-14 14:45:51 +01001784static const u32 ath10k_smps_map[] = {
1785 [WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
1786 [WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
1787 [WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
1788 [WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
1789};
1790
1791static int ath10k_setup_peer_smps(struct ath10k *ar, struct ath10k_vif *arvif,
1792 const u8 *addr,
1793 const struct ieee80211_sta_ht_cap *ht_cap)
1794{
1795 int smps;
1796
1797 if (!ht_cap->ht_supported)
1798 return 0;
1799
1800 smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
1801 smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
1802
1803 if (smps >= ARRAY_SIZE(ath10k_smps_map))
1804 return -EINVAL;
1805
1806 return ath10k_wmi_peer_set_param(ar, arvif->vdev_id, addr,
1807 WMI_PEER_SMPS_STATE,
1808 ath10k_smps_map[smps]);
1809}
1810
Michal Kazior139e1702015-02-15 16:50:42 +02001811static int ath10k_mac_vif_recalc_txbf(struct ath10k *ar,
1812 struct ieee80211_vif *vif,
1813 struct ieee80211_sta_vht_cap vht_cap)
1814{
1815 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1816 int ret;
1817 u32 param;
1818 u32 value;
1819
1820 if (!(ar->vht_cap_info &
1821 (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
1822 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
1823 IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
1824 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)))
1825 return 0;
1826
1827 param = ar->wmi.vdev_param->txbf;
1828 value = 0;
1829
1830 if (WARN_ON(param == WMI_VDEV_PARAM_UNSUPPORTED))
1831 return 0;
1832
1833 /* The following logic is correct. If a remote STA advertises support
1834 * for being a beamformer then we should enable us being a beamformee.
1835 */
1836
1837 if (ar->vht_cap_info &
1838 (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
1839 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)) {
1840 if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)
1841 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
1842
1843 if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)
1844 value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
1845 }
1846
1847 if (ar->vht_cap_info &
1848 (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
1849 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)) {
1850 if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)
1851 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
1852
1853 if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)
1854 value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
1855 }
1856
1857 if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFEE)
1858 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
1859
1860 if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFER)
1861 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
1862
1863 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param, value);
1864 if (ret) {
1865 ath10k_warn(ar, "failed to submit vdev param txbf 0x%x: %d\n",
1866 value, ret);
1867 return ret;
1868 }
1869
1870 return 0;
1871}
1872
Kalle Valo5e3dd152013-06-12 20:52:10 +03001873/* can be called only in mac80211 callbacks due to `key_count` usage */
1874static void ath10k_bss_assoc(struct ieee80211_hw *hw,
1875 struct ieee80211_vif *vif,
1876 struct ieee80211_bss_conf *bss_conf)
1877{
1878 struct ath10k *ar = hw->priv;
1879 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Michal Kazior90046f52014-02-14 14:45:51 +01001880 struct ieee80211_sta_ht_cap ht_cap;
Michal Kazior139e1702015-02-15 16:50:42 +02001881 struct ieee80211_sta_vht_cap vht_cap;
Kalle Valob9ada652013-10-16 15:44:46 +03001882 struct wmi_peer_assoc_complete_arg peer_arg;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001883 struct ieee80211_sta *ap_sta;
1884 int ret;
1885
Michal Kazior548db542013-07-05 16:15:15 +03001886 lockdep_assert_held(&ar->conf_mutex);
1887
Michal Kazior077efc82014-10-21 10:10:29 +03001888 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
1889 arvif->vdev_id, arvif->bssid, arvif->aid);
1890
Kalle Valo5e3dd152013-06-12 20:52:10 +03001891 rcu_read_lock();
1892
1893 ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
1894 if (!ap_sta) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001895 ath10k_warn(ar, "failed to find station entry for bss %pM vdev %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02001896 bss_conf->bssid, arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001897 rcu_read_unlock();
1898 return;
1899 }
1900
Michal Kazior90046f52014-02-14 14:45:51 +01001901 /* ap_sta must be accessed only within rcu section which must be left
1902 * before calling ath10k_setup_peer_smps() which might sleep. */
1903 ht_cap = ap_sta->ht_cap;
Michal Kazior139e1702015-02-15 16:50:42 +02001904 vht_cap = ap_sta->vht_cap;
Michal Kazior90046f52014-02-14 14:45:51 +01001905
Michal Kazior590922a2014-10-21 10:10:29 +03001906 ret = ath10k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001907 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001908 ath10k_warn(ar, "failed to prepare peer assoc for %pM vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001909 bss_conf->bssid, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001910 rcu_read_unlock();
1911 return;
1912 }
1913
1914 rcu_read_unlock();
1915
Kalle Valob9ada652013-10-16 15:44:46 +03001916 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
1917 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001918 ath10k_warn(ar, "failed to run peer assoc for %pM vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001919 bss_conf->bssid, arvif->vdev_id, ret);
Kalle Valob9ada652013-10-16 15:44:46 +03001920 return;
1921 }
1922
Michal Kazior90046f52014-02-14 14:45:51 +01001923 ret = ath10k_setup_peer_smps(ar, arvif, bss_conf->bssid, &ht_cap);
1924 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001925 ath10k_warn(ar, "failed to setup peer SMPS for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001926 arvif->vdev_id, ret);
Michal Kazior90046f52014-02-14 14:45:51 +01001927 return;
1928 }
1929
Michal Kazior139e1702015-02-15 16:50:42 +02001930 ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
1931 if (ret) {
1932 ath10k_warn(ar, "failed to recalc txbf for vdev %i on bss %pM: %d\n",
1933 arvif->vdev_id, bss_conf->bssid, ret);
1934 return;
1935 }
1936
Michal Kazior7aa7a722014-08-25 12:09:38 +02001937 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03001938 "mac vdev %d up (associated) bssid %pM aid %d\n",
1939 arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
1940
Michal Kazior077efc82014-10-21 10:10:29 +03001941 WARN_ON(arvif->is_up);
1942
Michal Kaziorc930f742014-01-23 11:38:25 +01001943 arvif->aid = bss_conf->aid;
Kalle Valob25f32c2014-09-14 12:50:49 +03001944 ether_addr_copy(arvif->bssid, bss_conf->bssid);
Michal Kaziorc930f742014-01-23 11:38:25 +01001945
1946 ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
1947 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001948 ath10k_warn(ar, "failed to set vdev %d up: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001949 arvif->vdev_id, ret);
Michal Kaziorc930f742014-01-23 11:38:25 +01001950 return;
1951 }
1952
1953 arvif->is_up = true;
Michal Kazior0a987fb2015-02-13 13:30:15 +01001954
1955 /* Workaround: Some firmware revisions (tested with qca6174
1956 * WLAN.RM.2.0-00073) have buggy powersave state machine and must be
1957 * poked with peer param command.
1958 */
1959 ret = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, arvif->bssid,
1960 WMI_PEER_DUMMY_VAR, 1);
1961 if (ret) {
1962 ath10k_warn(ar, "failed to poke peer %pM param for ps workaround on vdev %i: %d\n",
1963 arvif->bssid, arvif->vdev_id, ret);
1964 return;
1965 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001966}
1967
Kalle Valo5e3dd152013-06-12 20:52:10 +03001968static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
1969 struct ieee80211_vif *vif)
1970{
1971 struct ath10k *ar = hw->priv;
1972 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Michal Kazior139e1702015-02-15 16:50:42 +02001973 struct ieee80211_sta_vht_cap vht_cap = {};
Kalle Valo5e3dd152013-06-12 20:52:10 +03001974 int ret;
1975
Michal Kazior548db542013-07-05 16:15:15 +03001976 lockdep_assert_held(&ar->conf_mutex);
1977
Michal Kazior077efc82014-10-21 10:10:29 +03001978 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
1979 arvif->vdev_id, arvif->bssid);
Kalle Valo60c3daa2013-09-08 17:56:07 +03001980
Kalle Valo5e3dd152013-06-12 20:52:10 +03001981 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
Michal Kazior077efc82014-10-21 10:10:29 +03001982 if (ret)
1983 ath10k_warn(ar, "faield to down vdev %i: %d\n",
1984 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001985
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +02001986 arvif->def_wep_key_idx = -1;
1987
Michal Kazior139e1702015-02-15 16:50:42 +02001988 ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
1989 if (ret) {
1990 ath10k_warn(ar, "failed to recalc txbf for vdev %i: %d\n",
1991 arvif->vdev_id, ret);
1992 return;
1993 }
1994
Michal Kaziorc930f742014-01-23 11:38:25 +01001995 arvif->is_up = false;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001996}
1997
Michal Kazior590922a2014-10-21 10:10:29 +03001998static int ath10k_station_assoc(struct ath10k *ar,
1999 struct ieee80211_vif *vif,
2000 struct ieee80211_sta *sta,
2001 bool reassoc)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002002{
Michal Kazior590922a2014-10-21 10:10:29 +03002003 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Kalle Valob9ada652013-10-16 15:44:46 +03002004 struct wmi_peer_assoc_complete_arg peer_arg;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002005 int ret = 0;
2006
Michal Kazior548db542013-07-05 16:15:15 +03002007 lockdep_assert_held(&ar->conf_mutex);
2008
Michal Kazior590922a2014-10-21 10:10:29 +03002009 ret = ath10k_peer_assoc_prepare(ar, vif, sta, &peer_arg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002010 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002011 ath10k_warn(ar, "failed to prepare WMI peer assoc for %pM vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02002012 sta->addr, arvif->vdev_id, ret);
Kalle Valob9ada652013-10-16 15:44:46 +03002013 return ret;
2014 }
2015
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02002016 peer_arg.peer_reassoc = reassoc;
Kalle Valob9ada652013-10-16 15:44:46 +03002017 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
2018 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002019 ath10k_warn(ar, "failed to run peer assoc for STA %pM vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02002020 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002021 return ret;
2022 }
2023
Michal Kaziorb1ecde32014-10-21 10:10:29 +03002024 /* Re-assoc is run only to update supported rates for given station. It
2025 * doesn't make much sense to reconfigure the peer completely.
2026 */
2027 if (!reassoc) {
2028 ret = ath10k_setup_peer_smps(ar, arvif, sta->addr,
2029 &sta->ht_cap);
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02002030 if (ret) {
Michal Kaziorb1ecde32014-10-21 10:10:29 +03002031 ath10k_warn(ar, "failed to setup peer SMPS for vdev %d: %d\n",
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02002032 arvif->vdev_id, ret);
2033 return ret;
2034 }
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02002035
Michal Kaziorb1ecde32014-10-21 10:10:29 +03002036 ret = ath10k_peer_assoc_qos_ap(ar, arvif, sta);
2037 if (ret) {
2038 ath10k_warn(ar, "failed to set qos params for STA %pM for vdev %i: %d\n",
2039 sta->addr, arvif->vdev_id, ret);
2040 return ret;
2041 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002042
Michal Kaziorb1ecde32014-10-21 10:10:29 +03002043 if (!sta->wme) {
2044 arvif->num_legacy_stations++;
2045 ret = ath10k_recalc_rtscts_prot(arvif);
2046 if (ret) {
2047 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
2048 arvif->vdev_id, ret);
2049 return ret;
2050 }
2051 }
2052
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +02002053 /* Plumb cached keys only for static WEP */
2054 if (arvif->def_wep_key_idx != -1) {
2055 ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
2056 if (ret) {
2057 ath10k_warn(ar, "failed to install peer wep keys for vdev %i: %d\n",
2058 arvif->vdev_id, ret);
2059 return ret;
2060 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002061 }
2062 }
2063
Kalle Valo5e3dd152013-06-12 20:52:10 +03002064 return ret;
2065}
2066
Michal Kazior590922a2014-10-21 10:10:29 +03002067static int ath10k_station_disassoc(struct ath10k *ar,
2068 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002069 struct ieee80211_sta *sta)
2070{
Michal Kazior590922a2014-10-21 10:10:29 +03002071 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002072 int ret = 0;
2073
2074 lockdep_assert_held(&ar->conf_mutex);
2075
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02002076 if (!sta->wme) {
2077 arvif->num_legacy_stations--;
2078 ret = ath10k_recalc_rtscts_prot(arvif);
2079 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002080 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02002081 arvif->vdev_id, ret);
2082 return ret;
2083 }
2084 }
2085
Kalle Valo5e3dd152013-06-12 20:52:10 +03002086 ret = ath10k_clear_peer_keys(arvif, sta->addr);
2087 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002088 ath10k_warn(ar, "failed to clear all peer wep keys for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02002089 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002090 return ret;
2091 }
2092
2093 return ret;
2094}
2095
2096/**************/
2097/* Regulatory */
2098/**************/
2099
2100static int ath10k_update_channel_list(struct ath10k *ar)
2101{
2102 struct ieee80211_hw *hw = ar->hw;
2103 struct ieee80211_supported_band **bands;
2104 enum ieee80211_band band;
2105 struct ieee80211_channel *channel;
2106 struct wmi_scan_chan_list_arg arg = {0};
2107 struct wmi_channel_arg *ch;
2108 bool passive;
2109 int len;
2110 int ret;
2111 int i;
2112
Michal Kazior548db542013-07-05 16:15:15 +03002113 lockdep_assert_held(&ar->conf_mutex);
2114
Kalle Valo5e3dd152013-06-12 20:52:10 +03002115 bands = hw->wiphy->bands;
2116 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2117 if (!bands[band])
2118 continue;
2119
2120 for (i = 0; i < bands[band]->n_channels; i++) {
2121 if (bands[band]->channels[i].flags &
2122 IEEE80211_CHAN_DISABLED)
2123 continue;
2124
2125 arg.n_channels++;
2126 }
2127 }
2128
2129 len = sizeof(struct wmi_channel_arg) * arg.n_channels;
2130 arg.channels = kzalloc(len, GFP_KERNEL);
2131 if (!arg.channels)
2132 return -ENOMEM;
2133
2134 ch = arg.channels;
2135 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2136 if (!bands[band])
2137 continue;
2138
2139 for (i = 0; i < bands[band]->n_channels; i++) {
2140 channel = &bands[band]->channels[i];
2141
2142 if (channel->flags & IEEE80211_CHAN_DISABLED)
2143 continue;
2144
2145 ch->allow_ht = true;
2146
2147 /* FIXME: when should we really allow VHT? */
2148 ch->allow_vht = true;
2149
2150 ch->allow_ibss =
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +02002151 !(channel->flags & IEEE80211_CHAN_NO_IR);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002152
2153 ch->ht40plus =
2154 !(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
2155
Marek Puzyniake8a50f82013-11-20 09:59:47 +02002156 ch->chan_radar =
2157 !!(channel->flags & IEEE80211_CHAN_RADAR);
2158
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +02002159 passive = channel->flags & IEEE80211_CHAN_NO_IR;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002160 ch->passive = passive;
2161
2162 ch->freq = channel->center_freq;
Michal Kazior2d667212014-09-18 15:21:21 +02002163 ch->band_center_freq1 = channel->center_freq;
Michal Kazior89c5c842013-10-23 04:02:13 -07002164 ch->min_power = 0;
Michal Kazior02256932013-10-23 04:02:14 -07002165 ch->max_power = channel->max_power * 2;
2166 ch->max_reg_power = channel->max_reg_power * 2;
2167 ch->max_antenna_gain = channel->max_antenna_gain * 2;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002168 ch->reg_class_id = 0; /* FIXME */
2169
2170 /* FIXME: why use only legacy modes, why not any
2171 * HT/VHT modes? Would that even make any
2172 * difference? */
2173 if (channel->band == IEEE80211_BAND_2GHZ)
2174 ch->mode = MODE_11G;
2175 else
2176 ch->mode = MODE_11A;
2177
2178 if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
2179 continue;
2180
Michal Kazior7aa7a722014-08-25 12:09:38 +02002181 ath10k_dbg(ar, ATH10K_DBG_WMI,
Kalle Valo60c3daa2013-09-08 17:56:07 +03002182 "mac channel [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
2183 ch - arg.channels, arg.n_channels,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002184 ch->freq, ch->max_power, ch->max_reg_power,
2185 ch->max_antenna_gain, ch->mode);
2186
2187 ch++;
2188 }
2189 }
2190
2191 ret = ath10k_wmi_scan_chan_list(ar, &arg);
2192 kfree(arg.channels);
2193
2194 return ret;
2195}
2196
Marek Puzyniak821af6a2014-03-21 17:46:57 +02002197static enum wmi_dfs_region
2198ath10k_mac_get_dfs_region(enum nl80211_dfs_regions dfs_region)
2199{
2200 switch (dfs_region) {
2201 case NL80211_DFS_UNSET:
2202 return WMI_UNINIT_DFS_DOMAIN;
2203 case NL80211_DFS_FCC:
2204 return WMI_FCC_DFS_DOMAIN;
2205 case NL80211_DFS_ETSI:
2206 return WMI_ETSI_DFS_DOMAIN;
2207 case NL80211_DFS_JP:
2208 return WMI_MKK4_DFS_DOMAIN;
2209 }
2210 return WMI_UNINIT_DFS_DOMAIN;
2211}
2212
Michal Kaziorf7843d72013-07-16 09:38:52 +02002213static void ath10k_regd_update(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002214{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002215 struct reg_dmn_pair_mapping *regpair;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002216 int ret;
Marek Puzyniak821af6a2014-03-21 17:46:57 +02002217 enum wmi_dfs_region wmi_dfs_reg;
2218 enum nl80211_dfs_regions nl_dfs_reg;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002219
Michal Kaziorf7843d72013-07-16 09:38:52 +02002220 lockdep_assert_held(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002221
2222 ret = ath10k_update_channel_list(ar);
2223 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002224 ath10k_warn(ar, "failed to update channel list: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002225
2226 regpair = ar->ath_common.regulatory.regpair;
Michal Kaziorf7843d72013-07-16 09:38:52 +02002227
Marek Puzyniak821af6a2014-03-21 17:46:57 +02002228 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
2229 nl_dfs_reg = ar->dfs_detector->region;
2230 wmi_dfs_reg = ath10k_mac_get_dfs_region(nl_dfs_reg);
2231 } else {
2232 wmi_dfs_reg = WMI_UNINIT_DFS_DOMAIN;
2233 }
2234
Kalle Valo5e3dd152013-06-12 20:52:10 +03002235 /* Target allows setting up per-band regdomain but ath_common provides
2236 * a combined one only */
2237 ret = ath10k_wmi_pdev_set_regdomain(ar,
Kalle Valoef8c0012014-02-13 18:13:12 +02002238 regpair->reg_domain,
2239 regpair->reg_domain, /* 2ghz */
2240 regpair->reg_domain, /* 5ghz */
Kalle Valo5e3dd152013-06-12 20:52:10 +03002241 regpair->reg_2ghz_ctl,
Marek Puzyniak821af6a2014-03-21 17:46:57 +02002242 regpair->reg_5ghz_ctl,
2243 wmi_dfs_reg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002244 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002245 ath10k_warn(ar, "failed to set pdev regdomain: %d\n", ret);
Michal Kaziorf7843d72013-07-16 09:38:52 +02002246}
Michal Kazior548db542013-07-05 16:15:15 +03002247
Michal Kaziorf7843d72013-07-16 09:38:52 +02002248static void ath10k_reg_notifier(struct wiphy *wiphy,
2249 struct regulatory_request *request)
2250{
2251 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
2252 struct ath10k *ar = hw->priv;
Janusz Dziedzic9702c682013-11-20 09:59:41 +02002253 bool result;
Michal Kaziorf7843d72013-07-16 09:38:52 +02002254
2255 ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
2256
Janusz Dziedzic9702c682013-11-20 09:59:41 +02002257 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002258 ath10k_dbg(ar, ATH10K_DBG_REGULATORY, "dfs region 0x%x\n",
Janusz Dziedzic9702c682013-11-20 09:59:41 +02002259 request->dfs_region);
2260 result = ar->dfs_detector->set_dfs_domain(ar->dfs_detector,
2261 request->dfs_region);
2262 if (!result)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002263 ath10k_warn(ar, "DFS region 0x%X not supported, will trigger radar for every pulse\n",
Janusz Dziedzic9702c682013-11-20 09:59:41 +02002264 request->dfs_region);
2265 }
2266
Michal Kaziorf7843d72013-07-16 09:38:52 +02002267 mutex_lock(&ar->conf_mutex);
2268 if (ar->state == ATH10K_STATE_ON)
2269 ath10k_regd_update(ar);
Michal Kazior548db542013-07-05 16:15:15 +03002270 mutex_unlock(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002271}
2272
2273/***************/
2274/* TX handlers */
2275/***************/
2276
Michal Kazior42c3aa62013-10-02 11:03:38 +02002277static u8 ath10k_tx_h_get_tid(struct ieee80211_hdr *hdr)
2278{
2279 if (ieee80211_is_mgmt(hdr->frame_control))
2280 return HTT_DATA_TX_EXT_TID_MGMT;
2281
2282 if (!ieee80211_is_data_qos(hdr->frame_control))
2283 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
2284
2285 if (!is_unicast_ether_addr(ieee80211_get_DA(hdr)))
2286 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
2287
2288 return ieee80211_get_qos_ctl(hdr)[0] & IEEE80211_QOS_CTL_TID_MASK;
2289}
2290
Michal Kazior2b37c292014-09-02 11:00:22 +03002291static u8 ath10k_tx_h_get_vdev_id(struct ath10k *ar, struct ieee80211_vif *vif)
Michal Kaziorddb6ad72013-10-02 11:03:39 +02002292{
Michal Kazior2b37c292014-09-02 11:00:22 +03002293 if (vif)
2294 return ath10k_vif_to_arvif(vif)->vdev_id;
Michal Kaziorddb6ad72013-10-02 11:03:39 +02002295
Michal Kazior1bbc0972014-04-08 09:45:47 +03002296 if (ar->monitor_started)
Michal Kaziorddb6ad72013-10-02 11:03:39 +02002297 return ar->monitor_vdev_id;
2298
Michal Kazior7aa7a722014-08-25 12:09:38 +02002299 ath10k_warn(ar, "failed to resolve vdev id\n");
Michal Kaziorddb6ad72013-10-02 11:03:39 +02002300 return 0;
2301}
2302
Michal Kazior4b604552014-07-21 21:03:09 +03002303/* HTT Tx uses Native Wifi tx mode which expects 802.11 frames without QoS
2304 * Control in the header.
Kalle Valo5e3dd152013-06-12 20:52:10 +03002305 */
Michal Kazior4b604552014-07-21 21:03:09 +03002306static void ath10k_tx_h_nwifi(struct ieee80211_hw *hw, struct sk_buff *skb)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002307{
2308 struct ieee80211_hdr *hdr = (void *)skb->data;
Michal Kaziorc21c64d2014-07-21 21:03:10 +03002309 struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002310 u8 *qos_ctl;
2311
2312 if (!ieee80211_is_data_qos(hdr->frame_control))
2313 return;
2314
2315 qos_ctl = ieee80211_get_qos_ctl(hdr);
Michal Kaziorba0ccd72013-07-22 14:25:28 +02002316 memmove(skb->data + IEEE80211_QOS_CTL_LEN,
2317 skb->data, (void *)qos_ctl - (void *)skb->data);
2318 skb_pull(skb, IEEE80211_QOS_CTL_LEN);
Michal Kaziorc21c64d2014-07-21 21:03:10 +03002319
2320 /* Fw/Hw generates a corrupted QoS Control Field for QoS NullFunc
2321 * frames. Powersave is handled by the fw/hw so QoS NyllFunc frames are
2322 * used only for CQM purposes (e.g. hostapd station keepalive ping) so
2323 * it is safe to downgrade to NullFunc.
2324 */
Michal Kaziorbf0a26d2015-01-24 12:14:51 +02002325 hdr = (void *)skb->data;
Michal Kaziorc21c64d2014-07-21 21:03:10 +03002326 if (ieee80211_is_qos_nullfunc(hdr->frame_control)) {
2327 hdr->frame_control &= ~__cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
2328 cb->htt.tid = HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
2329 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002330}
2331
Michal Kazior4b604552014-07-21 21:03:09 +03002332static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar,
2333 struct ieee80211_vif *vif,
2334 struct sk_buff *skb)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002335{
2336 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002337 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2338
2339 /* This is case only for P2P_GO */
2340 if (arvif->vdev_type != WMI_VDEV_TYPE_AP ||
2341 arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
2342 return;
2343
2344 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
2345 spin_lock_bh(&ar->data_lock);
2346 if (arvif->u.ap.noa_data)
2347 if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
2348 GFP_ATOMIC))
2349 memcpy(skb_put(skb, arvif->u.ap.noa_len),
2350 arvif->u.ap.noa_data,
2351 arvif->u.ap.noa_len);
2352 spin_unlock_bh(&ar->data_lock);
2353 }
2354}
2355
Michal Kazior8d6d3622014-11-24 14:58:31 +01002356static bool ath10k_mac_need_offchan_tx_work(struct ath10k *ar)
2357{
2358 /* FIXME: Not really sure since when the behaviour changed. At some
2359 * point new firmware stopped requiring creation of peer entries for
2360 * offchannel tx (and actually creating them causes issues with wmi-htc
2361 * tx credit replenishment and reliability). Assuming it's at least 3.4
2362 * because that's when the `freq` was introduced to TX_FRM HTT command.
2363 */
2364 return !(ar->htt.target_version_major >= 3 &&
2365 ar->htt.target_version_minor >= 4);
2366}
2367
Kalle Valo5e3dd152013-06-12 20:52:10 +03002368static void ath10k_tx_htt(struct ath10k *ar, struct sk_buff *skb)
2369{
2370 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002371 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002372
Michal Kazior961d4c32013-08-09 10:13:34 +02002373 if (ar->htt.target_version_major >= 3) {
2374 /* Since HTT 3.0 there is no separate mgmt tx command */
2375 ret = ath10k_htt_tx(&ar->htt, skb);
2376 goto exit;
2377 }
2378
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002379 if (ieee80211_is_mgmt(hdr->frame_control)) {
2380 if (test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
2381 ar->fw_features)) {
2382 if (skb_queue_len(&ar->wmi_mgmt_tx_queue) >=
2383 ATH10K_MAX_NUM_MGMT_PENDING) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002384 ath10k_warn(ar, "reached WMI management transmit queue limit\n");
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002385 ret = -EBUSY;
2386 goto exit;
2387 }
2388
2389 skb_queue_tail(&ar->wmi_mgmt_tx_queue, skb);
2390 ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
2391 } else {
2392 ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
2393 }
2394 } else if (!test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
2395 ar->fw_features) &&
2396 ieee80211_is_nullfunc(hdr->frame_control)) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002397 /* FW does not report tx status properly for NullFunc frames
2398 * unless they are sent through mgmt tx path. mac80211 sends
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002399 * those frames when it detects link/beacon loss and depends
2400 * on the tx status to be correct. */
Michal Kazioredb82362013-07-05 16:15:14 +03002401 ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002402 } else {
Michal Kazioredb82362013-07-05 16:15:14 +03002403 ret = ath10k_htt_tx(&ar->htt, skb);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002404 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002405
Michal Kazior961d4c32013-08-09 10:13:34 +02002406exit:
Kalle Valo5e3dd152013-06-12 20:52:10 +03002407 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002408 ath10k_warn(ar, "failed to transmit packet, dropping: %d\n",
2409 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002410 ieee80211_free_txskb(ar->hw, skb);
2411 }
2412}
2413
2414void ath10k_offchan_tx_purge(struct ath10k *ar)
2415{
2416 struct sk_buff *skb;
2417
2418 for (;;) {
2419 skb = skb_dequeue(&ar->offchan_tx_queue);
2420 if (!skb)
2421 break;
2422
2423 ieee80211_free_txskb(ar->hw, skb);
2424 }
2425}
2426
2427void ath10k_offchan_tx_work(struct work_struct *work)
2428{
2429 struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
2430 struct ath10k_peer *peer;
2431 struct ieee80211_hdr *hdr;
2432 struct sk_buff *skb;
2433 const u8 *peer_addr;
2434 int vdev_id;
2435 int ret;
2436
2437 /* FW requirement: We must create a peer before FW will send out
2438 * an offchannel frame. Otherwise the frame will be stuck and
2439 * never transmitted. We delete the peer upon tx completion.
2440 * It is unlikely that a peer for offchannel tx will already be
2441 * present. However it may be in some rare cases so account for that.
2442 * Otherwise we might remove a legitimate peer and break stuff. */
2443
2444 for (;;) {
2445 skb = skb_dequeue(&ar->offchan_tx_queue);
2446 if (!skb)
2447 break;
2448
2449 mutex_lock(&ar->conf_mutex);
2450
Michal Kazior7aa7a722014-08-25 12:09:38 +02002451 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac offchannel skb %p\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002452 skb);
2453
2454 hdr = (struct ieee80211_hdr *)skb->data;
2455 peer_addr = ieee80211_get_DA(hdr);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002456 vdev_id = ATH10K_SKB_CB(skb)->vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002457
2458 spin_lock_bh(&ar->data_lock);
2459 peer = ath10k_peer_find(ar, vdev_id, peer_addr);
2460 spin_unlock_bh(&ar->data_lock);
2461
2462 if (peer)
Kalle Valo60c3daa2013-09-08 17:56:07 +03002463 /* FIXME: should this use ath10k_warn()? */
Michal Kazior7aa7a722014-08-25 12:09:38 +02002464 ath10k_dbg(ar, ATH10K_DBG_MAC, "peer %pM on vdev %d already present\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002465 peer_addr, vdev_id);
2466
2467 if (!peer) {
2468 ret = ath10k_peer_create(ar, vdev_id, peer_addr);
2469 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002470 ath10k_warn(ar, "failed to create peer %pM on vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002471 peer_addr, vdev_id, ret);
2472 }
2473
2474 spin_lock_bh(&ar->data_lock);
Wolfram Sang16735d02013-11-14 14:32:02 -08002475 reinit_completion(&ar->offchan_tx_completed);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002476 ar->offchan_tx_skb = skb;
2477 spin_unlock_bh(&ar->data_lock);
2478
2479 ath10k_tx_htt(ar, skb);
2480
2481 ret = wait_for_completion_timeout(&ar->offchan_tx_completed,
2482 3 * HZ);
Nicholas Mc Guire38e2a642015-01-08 13:27:34 +01002483 if (ret == 0)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002484 ath10k_warn(ar, "timed out waiting for offchannel skb %p\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002485 skb);
2486
2487 if (!peer) {
2488 ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
2489 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002490 ath10k_warn(ar, "failed to delete peer %pM on vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002491 peer_addr, vdev_id, ret);
2492 }
2493
2494 mutex_unlock(&ar->conf_mutex);
2495 }
2496}
2497
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002498void ath10k_mgmt_over_wmi_tx_purge(struct ath10k *ar)
2499{
2500 struct sk_buff *skb;
2501
2502 for (;;) {
2503 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
2504 if (!skb)
2505 break;
2506
2507 ieee80211_free_txskb(ar->hw, skb);
2508 }
2509}
2510
2511void ath10k_mgmt_over_wmi_tx_work(struct work_struct *work)
2512{
2513 struct ath10k *ar = container_of(work, struct ath10k, wmi_mgmt_tx_work);
2514 struct sk_buff *skb;
2515 int ret;
2516
2517 for (;;) {
2518 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
2519 if (!skb)
2520 break;
2521
2522 ret = ath10k_wmi_mgmt_tx(ar, skb);
Michal Kazior5fb5e412013-10-28 07:18:13 +01002523 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002524 ath10k_warn(ar, "failed to transmit management frame via WMI: %d\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02002525 ret);
Michal Kazior5fb5e412013-10-28 07:18:13 +01002526 ieee80211_free_txskb(ar->hw, skb);
2527 }
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002528 }
2529}
2530
Kalle Valo5e3dd152013-06-12 20:52:10 +03002531/************/
2532/* Scanning */
2533/************/
2534
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002535void __ath10k_scan_finish(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002536{
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002537 lockdep_assert_held(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002538
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002539 switch (ar->scan.state) {
2540 case ATH10K_SCAN_IDLE:
2541 break;
2542 case ATH10K_SCAN_RUNNING:
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002543 if (ar->scan.is_roc)
2544 ieee80211_remain_on_channel_expired(ar->hw);
John W. Linvillef6eaf1e2015-01-12 16:07:02 -05002545 /* fall through */
Michal Kazior7305d3e2014-11-24 14:58:33 +01002546 case ATH10K_SCAN_ABORTING:
2547 if (!ar->scan.is_roc)
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002548 ieee80211_scan_completed(ar->hw,
2549 (ar->scan.state ==
2550 ATH10K_SCAN_ABORTING));
2551 /* fall through */
2552 case ATH10K_SCAN_STARTING:
2553 ar->scan.state = ATH10K_SCAN_IDLE;
2554 ar->scan_channel = NULL;
2555 ath10k_offchan_tx_purge(ar);
2556 cancel_delayed_work(&ar->scan.timeout);
2557 complete_all(&ar->scan.completed);
2558 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002559 }
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002560}
Kalle Valo5e3dd152013-06-12 20:52:10 +03002561
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002562void ath10k_scan_finish(struct ath10k *ar)
2563{
2564 spin_lock_bh(&ar->data_lock);
2565 __ath10k_scan_finish(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002566 spin_unlock_bh(&ar->data_lock);
2567}
2568
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002569static int ath10k_scan_stop(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002570{
2571 struct wmi_stop_scan_arg arg = {
2572 .req_id = 1, /* FIXME */
2573 .req_type = WMI_SCAN_STOP_ONE,
2574 .u.scan_id = ATH10K_SCAN_ID,
2575 };
2576 int ret;
2577
2578 lockdep_assert_held(&ar->conf_mutex);
2579
Kalle Valo5e3dd152013-06-12 20:52:10 +03002580 ret = ath10k_wmi_stop_scan(ar, &arg);
2581 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002582 ath10k_warn(ar, "failed to stop wmi scan: %d\n", ret);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002583 goto out;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002584 }
2585
Kalle Valo5e3dd152013-06-12 20:52:10 +03002586 ret = wait_for_completion_timeout(&ar->scan.completed, 3*HZ);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002587 if (ret == 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002588 ath10k_warn(ar, "failed to receive scan abortion completion: timed out\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002589 ret = -ETIMEDOUT;
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002590 } else if (ret > 0) {
2591 ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002592 }
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002593
2594out:
2595 /* Scan state should be updated upon scan completion but in case
2596 * firmware fails to deliver the event (for whatever reason) it is
2597 * desired to clean up scan state anyway. Firmware may have just
2598 * dropped the scan completion event delivery due to transport pipe
2599 * being overflown with data and/or it can recover on its own before
2600 * next scan request is submitted.
2601 */
2602 spin_lock_bh(&ar->data_lock);
2603 if (ar->scan.state != ATH10K_SCAN_IDLE)
2604 __ath10k_scan_finish(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002605 spin_unlock_bh(&ar->data_lock);
2606
2607 return ret;
2608}
2609
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002610static void ath10k_scan_abort(struct ath10k *ar)
2611{
2612 int ret;
2613
2614 lockdep_assert_held(&ar->conf_mutex);
2615
2616 spin_lock_bh(&ar->data_lock);
2617
2618 switch (ar->scan.state) {
2619 case ATH10K_SCAN_IDLE:
2620 /* This can happen if timeout worker kicked in and called
2621 * abortion while scan completion was being processed.
2622 */
2623 break;
2624 case ATH10K_SCAN_STARTING:
2625 case ATH10K_SCAN_ABORTING:
Michal Kazior7aa7a722014-08-25 12:09:38 +02002626 ath10k_warn(ar, "refusing scan abortion due to invalid scan state: %s (%d)\n",
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002627 ath10k_scan_state_str(ar->scan.state),
2628 ar->scan.state);
2629 break;
2630 case ATH10K_SCAN_RUNNING:
2631 ar->scan.state = ATH10K_SCAN_ABORTING;
2632 spin_unlock_bh(&ar->data_lock);
2633
2634 ret = ath10k_scan_stop(ar);
2635 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002636 ath10k_warn(ar, "failed to abort scan: %d\n", ret);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002637
2638 spin_lock_bh(&ar->data_lock);
2639 break;
2640 }
2641
2642 spin_unlock_bh(&ar->data_lock);
2643}
2644
2645void ath10k_scan_timeout_work(struct work_struct *work)
2646{
2647 struct ath10k *ar = container_of(work, struct ath10k,
2648 scan.timeout.work);
2649
2650 mutex_lock(&ar->conf_mutex);
2651 ath10k_scan_abort(ar);
2652 mutex_unlock(&ar->conf_mutex);
2653}
2654
Kalle Valo5e3dd152013-06-12 20:52:10 +03002655static int ath10k_start_scan(struct ath10k *ar,
2656 const struct wmi_start_scan_arg *arg)
2657{
2658 int ret;
2659
2660 lockdep_assert_held(&ar->conf_mutex);
2661
2662 ret = ath10k_wmi_start_scan(ar, arg);
2663 if (ret)
2664 return ret;
2665
Kalle Valo5e3dd152013-06-12 20:52:10 +03002666 ret = wait_for_completion_timeout(&ar->scan.started, 1*HZ);
2667 if (ret == 0) {
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002668 ret = ath10k_scan_stop(ar);
2669 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002670 ath10k_warn(ar, "failed to stop scan: %d\n", ret);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002671
2672 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002673 }
2674
Ben Greear2f9eec02015-02-15 16:50:38 +02002675 /* If we failed to start the scan, return error code at
2676 * this point. This is probably due to some issue in the
2677 * firmware, but no need to wedge the driver due to that...
2678 */
2679 spin_lock_bh(&ar->data_lock);
2680 if (ar->scan.state == ATH10K_SCAN_IDLE) {
2681 spin_unlock_bh(&ar->data_lock);
2682 return -EINVAL;
2683 }
2684 spin_unlock_bh(&ar->data_lock);
2685
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002686 /* Add a 200ms margin to account for event/command processing */
2687 ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
2688 msecs_to_jiffies(arg->max_scan_time+200));
Kalle Valo5e3dd152013-06-12 20:52:10 +03002689 return 0;
2690}
2691
2692/**********************/
2693/* mac80211 callbacks */
2694/**********************/
2695
2696static void ath10k_tx(struct ieee80211_hw *hw,
2697 struct ieee80211_tx_control *control,
2698 struct sk_buff *skb)
2699{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002700 struct ath10k *ar = hw->priv;
Michal Kazior4b604552014-07-21 21:03:09 +03002701 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2702 struct ieee80211_vif *vif = info->control.vif;
Michal Kazior4b604552014-07-21 21:03:09 +03002703 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002704
2705 /* We should disable CCK RATE due to P2P */
2706 if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002707 ath10k_dbg(ar, ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002708
Michal Kazior4b604552014-07-21 21:03:09 +03002709 ATH10K_SKB_CB(skb)->htt.is_offchan = false;
2710 ATH10K_SKB_CB(skb)->htt.tid = ath10k_tx_h_get_tid(hdr);
Michal Kazior2b37c292014-09-02 11:00:22 +03002711 ATH10K_SKB_CB(skb)->vdev_id = ath10k_tx_h_get_vdev_id(ar, vif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002712
Michal Kaziorcf84bd42013-07-16 11:04:54 +02002713 /* it makes no sense to process injected frames like that */
Michal Kazior4b604552014-07-21 21:03:09 +03002714 if (vif && vif->type != NL80211_IFTYPE_MONITOR) {
2715 ath10k_tx_h_nwifi(hw, skb);
Michal Kazior4b604552014-07-21 21:03:09 +03002716 ath10k_tx_h_add_p2p_noa_ie(ar, vif, skb);
2717 ath10k_tx_h_seq_no(vif, skb);
Michal Kaziorcf84bd42013-07-16 11:04:54 +02002718 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002719
Kalle Valo5e3dd152013-06-12 20:52:10 +03002720 if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
2721 spin_lock_bh(&ar->data_lock);
Michal Kazior8d6d3622014-11-24 14:58:31 +01002722 ATH10K_SKB_CB(skb)->htt.freq = ar->scan.roc_freq;
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002723 ATH10K_SKB_CB(skb)->vdev_id = ar->scan.vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002724 spin_unlock_bh(&ar->data_lock);
2725
Michal Kazior8d6d3622014-11-24 14:58:31 +01002726 if (ath10k_mac_need_offchan_tx_work(ar)) {
2727 ATH10K_SKB_CB(skb)->htt.freq = 0;
2728 ATH10K_SKB_CB(skb)->htt.is_offchan = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002729
Michal Kazior8d6d3622014-11-24 14:58:31 +01002730 ath10k_dbg(ar, ATH10K_DBG_MAC, "queued offchannel skb %p\n",
2731 skb);
2732
2733 skb_queue_tail(&ar->offchan_tx_queue, skb);
2734 ieee80211_queue_work(hw, &ar->offchan_tx_work);
2735 return;
2736 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002737 }
2738
2739 ath10k_tx_htt(ar, skb);
2740}
2741
Michal Kaziorbca7baf2014-05-26 12:46:03 +03002742/* Must not be called with conf_mutex held as workers can use that also. */
Michal Kazior7962b0d2014-10-28 10:34:38 +01002743void ath10k_drain_tx(struct ath10k *ar)
Michal Kaziorbca7baf2014-05-26 12:46:03 +03002744{
2745 /* make sure rcu-protected mac80211 tx path itself is drained */
2746 synchronize_net();
2747
2748 ath10k_offchan_tx_purge(ar);
2749 ath10k_mgmt_over_wmi_tx_purge(ar);
2750
2751 cancel_work_sync(&ar->offchan_tx_work);
2752 cancel_work_sync(&ar->wmi_mgmt_tx_work);
2753}
2754
Michal Kazioraffd3212013-07-16 09:54:35 +02002755void ath10k_halt(struct ath10k *ar)
Michal Kazior818bdd12013-07-16 09:38:57 +02002756{
Michal Kaziord9bc4b92014-04-23 19:30:06 +03002757 struct ath10k_vif *arvif;
2758
Michal Kazior818bdd12013-07-16 09:38:57 +02002759 lockdep_assert_held(&ar->conf_mutex);
2760
Michal Kazior19337472014-08-28 12:58:16 +02002761 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
2762 ar->filter_flags = 0;
2763 ar->monitor = false;
2764
2765 if (ar->monitor_started)
Michal Kazior1bbc0972014-04-08 09:45:47 +03002766 ath10k_monitor_stop(ar);
Michal Kazior19337472014-08-28 12:58:16 +02002767
2768 ar->monitor_started = false;
Michal Kazior1bbc0972014-04-08 09:45:47 +03002769
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002770 ath10k_scan_finish(ar);
Michal Kazior818bdd12013-07-16 09:38:57 +02002771 ath10k_peer_cleanup_all(ar);
2772 ath10k_core_stop(ar);
2773 ath10k_hif_power_down(ar);
2774
2775 spin_lock_bh(&ar->data_lock);
Michal Kazior64badcb2014-09-18 11:18:02 +03002776 list_for_each_entry(arvif, &ar->arvifs, list)
2777 ath10k_mac_vif_beacon_cleanup(arvif);
Michal Kazior818bdd12013-07-16 09:38:57 +02002778 spin_unlock_bh(&ar->data_lock);
2779}
2780
Ben Greear46acf7b2014-05-16 17:15:38 +03002781static int ath10k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
2782{
2783 struct ath10k *ar = hw->priv;
2784
2785 mutex_lock(&ar->conf_mutex);
2786
2787 if (ar->cfg_tx_chainmask) {
2788 *tx_ant = ar->cfg_tx_chainmask;
2789 *rx_ant = ar->cfg_rx_chainmask;
2790 } else {
2791 *tx_ant = ar->supp_tx_chainmask;
2792 *rx_ant = ar->supp_rx_chainmask;
2793 }
2794
2795 mutex_unlock(&ar->conf_mutex);
2796
2797 return 0;
2798}
2799
Ben Greear5572a952014-11-24 16:22:10 +02002800static void ath10k_check_chain_mask(struct ath10k *ar, u32 cm, const char *dbg)
2801{
2802 /* It is not clear that allowing gaps in chainmask
2803 * is helpful. Probably it will not do what user
2804 * is hoping for, so warn in that case.
2805 */
2806 if (cm == 15 || cm == 7 || cm == 3 || cm == 1 || cm == 0)
2807 return;
2808
2809 ath10k_warn(ar, "mac %s antenna chainmask may be invalid: 0x%x. Suggested values: 15, 7, 3, 1 or 0.\n",
2810 dbg, cm);
2811}
2812
Ben Greear46acf7b2014-05-16 17:15:38 +03002813static int __ath10k_set_antenna(struct ath10k *ar, u32 tx_ant, u32 rx_ant)
2814{
2815 int ret;
2816
2817 lockdep_assert_held(&ar->conf_mutex);
2818
Ben Greear5572a952014-11-24 16:22:10 +02002819 ath10k_check_chain_mask(ar, tx_ant, "tx");
2820 ath10k_check_chain_mask(ar, rx_ant, "rx");
2821
Ben Greear46acf7b2014-05-16 17:15:38 +03002822 ar->cfg_tx_chainmask = tx_ant;
2823 ar->cfg_rx_chainmask = rx_ant;
2824
2825 if ((ar->state != ATH10K_STATE_ON) &&
2826 (ar->state != ATH10K_STATE_RESTARTED))
2827 return 0;
2828
2829 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->tx_chain_mask,
2830 tx_ant);
2831 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002832 ath10k_warn(ar, "failed to set tx-chainmask: %d, req 0x%x\n",
Ben Greear46acf7b2014-05-16 17:15:38 +03002833 ret, tx_ant);
2834 return ret;
2835 }
2836
2837 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->rx_chain_mask,
2838 rx_ant);
2839 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002840 ath10k_warn(ar, "failed to set rx-chainmask: %d, req 0x%x\n",
Ben Greear46acf7b2014-05-16 17:15:38 +03002841 ret, rx_ant);
2842 return ret;
2843 }
2844
2845 return 0;
2846}
2847
2848static int ath10k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
2849{
2850 struct ath10k *ar = hw->priv;
2851 int ret;
2852
2853 mutex_lock(&ar->conf_mutex);
2854 ret = __ath10k_set_antenna(ar, tx_ant, rx_ant);
2855 mutex_unlock(&ar->conf_mutex);
2856 return ret;
2857}
2858
Kalle Valo5e3dd152013-06-12 20:52:10 +03002859static int ath10k_start(struct ieee80211_hw *hw)
2860{
2861 struct ath10k *ar = hw->priv;
Michal Kazior818bdd12013-07-16 09:38:57 +02002862 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002863
Michal Kaziorbca7baf2014-05-26 12:46:03 +03002864 /*
2865 * This makes sense only when restarting hw. It is harmless to call
2866 * uncoditionally. This is necessary to make sure no HTT/WMI tx
2867 * commands will be submitted while restarting.
2868 */
2869 ath10k_drain_tx(ar);
2870
Michal Kazior548db542013-07-05 16:15:15 +03002871 mutex_lock(&ar->conf_mutex);
2872
Michal Kaziorc5058f52014-05-26 12:46:03 +03002873 switch (ar->state) {
2874 case ATH10K_STATE_OFF:
2875 ar->state = ATH10K_STATE_ON;
2876 break;
2877 case ATH10K_STATE_RESTARTING:
2878 ath10k_halt(ar);
2879 ar->state = ATH10K_STATE_RESTARTED;
2880 break;
2881 case ATH10K_STATE_ON:
2882 case ATH10K_STATE_RESTARTED:
2883 case ATH10K_STATE_WEDGED:
2884 WARN_ON(1);
Michal Kazior818bdd12013-07-16 09:38:57 +02002885 ret = -EINVAL;
Michal Kaziorae254432014-05-26 12:46:02 +03002886 goto err;
Kalle Valo43d2a302014-09-10 18:23:30 +03002887 case ATH10K_STATE_UTF:
2888 ret = -EBUSY;
2889 goto err;
Michal Kazior818bdd12013-07-16 09:38:57 +02002890 }
2891
2892 ret = ath10k_hif_power_up(ar);
2893 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002894 ath10k_err(ar, "Could not init hif: %d\n", ret);
Michal Kaziorae254432014-05-26 12:46:02 +03002895 goto err_off;
Michal Kazior818bdd12013-07-16 09:38:57 +02002896 }
2897
Kalle Valo43d2a302014-09-10 18:23:30 +03002898 ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_NORMAL);
Michal Kazior818bdd12013-07-16 09:38:57 +02002899 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002900 ath10k_err(ar, "Could not init core: %d\n", ret);
Michal Kaziorae254432014-05-26 12:46:02 +03002901 goto err_power_down;
Michal Kazior818bdd12013-07-16 09:38:57 +02002902 }
2903
Bartosz Markowski226a3392013-09-26 17:47:16 +02002904 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->pmf_qos, 1);
Michal Kaziorae254432014-05-26 12:46:02 +03002905 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002906 ath10k_warn(ar, "failed to enable PMF QOS: %d\n", ret);
Michal Kaziorae254432014-05-26 12:46:02 +03002907 goto err_core_stop;
2908 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002909
Michal Kaziorc4dd0d02013-11-13 11:05:10 +01002910 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->dynamic_bw, 1);
Michal Kaziorae254432014-05-26 12:46:02 +03002911 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002912 ath10k_warn(ar, "failed to enable dynamic BW: %d\n", ret);
Michal Kaziorae254432014-05-26 12:46:02 +03002913 goto err_core_stop;
2914 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002915
Ben Greear46acf7b2014-05-16 17:15:38 +03002916 if (ar->cfg_tx_chainmask)
2917 __ath10k_set_antenna(ar, ar->cfg_tx_chainmask,
2918 ar->cfg_rx_chainmask);
2919
Marek Puzyniakab6258e2014-01-29 15:03:31 +02002920 /*
2921 * By default FW set ARP frames ac to voice (6). In that case ARP
2922 * exchange is not working properly for UAPSD enabled AP. ARP requests
2923 * which arrives with access category 0 are processed by network stack
2924 * and send back with access category 0, but FW changes access category
2925 * to 6. Set ARP frames access category to best effort (0) solves
2926 * this problem.
2927 */
2928
2929 ret = ath10k_wmi_pdev_set_param(ar,
2930 ar->wmi.pdev_param->arp_ac_override, 0);
2931 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002932 ath10k_warn(ar, "failed to set arp ac override parameter: %d\n",
Marek Puzyniakab6258e2014-01-29 15:03:31 +02002933 ret);
Michal Kaziorae254432014-05-26 12:46:02 +03002934 goto err_core_stop;
Marek Puzyniakab6258e2014-01-29 15:03:31 +02002935 }
2936
Michal Kaziord6500972014-04-08 09:56:09 +03002937 ar->num_started_vdevs = 0;
Michal Kaziorf7843d72013-07-16 09:38:52 +02002938 ath10k_regd_update(ar);
2939
Simon Wunderlich855aed12014-08-02 09:12:54 +03002940 ath10k_spectral_start(ar);
2941
Michal Kaziorae254432014-05-26 12:46:02 +03002942 mutex_unlock(&ar->conf_mutex);
2943 return 0;
2944
2945err_core_stop:
2946 ath10k_core_stop(ar);
2947
2948err_power_down:
2949 ath10k_hif_power_down(ar);
2950
2951err_off:
2952 ar->state = ATH10K_STATE_OFF;
2953
2954err:
Michal Kazior548db542013-07-05 16:15:15 +03002955 mutex_unlock(&ar->conf_mutex);
Michal Kaziorc60bdd82014-01-29 07:26:31 +01002956 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002957}
2958
2959static void ath10k_stop(struct ieee80211_hw *hw)
2960{
2961 struct ath10k *ar = hw->priv;
2962
Michal Kaziorbca7baf2014-05-26 12:46:03 +03002963 ath10k_drain_tx(ar);
2964
Michal Kazior548db542013-07-05 16:15:15 +03002965 mutex_lock(&ar->conf_mutex);
Michal Kaziorc5058f52014-05-26 12:46:03 +03002966 if (ar->state != ATH10K_STATE_OFF) {
Michal Kazior818bdd12013-07-16 09:38:57 +02002967 ath10k_halt(ar);
Michal Kaziorc5058f52014-05-26 12:46:03 +03002968 ar->state = ATH10K_STATE_OFF;
2969 }
Michal Kazior548db542013-07-05 16:15:15 +03002970 mutex_unlock(&ar->conf_mutex);
2971
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002972 cancel_delayed_work_sync(&ar->scan.timeout);
Michal Kazioraffd3212013-07-16 09:54:35 +02002973 cancel_work_sync(&ar->restart_work);
2974}
2975
Michal Kaziorad088bf2013-10-16 15:44:46 +03002976static int ath10k_config_ps(struct ath10k *ar)
Michal Kazioraffd3212013-07-16 09:54:35 +02002977{
Michal Kaziorad088bf2013-10-16 15:44:46 +03002978 struct ath10k_vif *arvif;
2979 int ret = 0;
Michal Kazioraffd3212013-07-16 09:54:35 +02002980
2981 lockdep_assert_held(&ar->conf_mutex);
2982
Michal Kaziorad088bf2013-10-16 15:44:46 +03002983 list_for_each_entry(arvif, &ar->arvifs, list) {
2984 ret = ath10k_mac_vif_setup_ps(arvif);
2985 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002986 ath10k_warn(ar, "failed to setup powersave: %d\n", ret);
Michal Kaziorad088bf2013-10-16 15:44:46 +03002987 break;
2988 }
2989 }
Michal Kazioraffd3212013-07-16 09:54:35 +02002990
Michal Kaziorad088bf2013-10-16 15:44:46 +03002991 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002992}
2993
Michal Kaziorc930f742014-01-23 11:38:25 +01002994static const char *chandef_get_width(enum nl80211_chan_width width)
2995{
2996 switch (width) {
2997 case NL80211_CHAN_WIDTH_20_NOHT:
2998 return "20 (noht)";
2999 case NL80211_CHAN_WIDTH_20:
3000 return "20";
3001 case NL80211_CHAN_WIDTH_40:
3002 return "40";
3003 case NL80211_CHAN_WIDTH_80:
3004 return "80";
3005 case NL80211_CHAN_WIDTH_80P80:
3006 return "80+80";
3007 case NL80211_CHAN_WIDTH_160:
3008 return "160";
3009 case NL80211_CHAN_WIDTH_5:
3010 return "5";
3011 case NL80211_CHAN_WIDTH_10:
3012 return "10";
3013 }
3014 return "?";
3015}
3016
3017static void ath10k_config_chan(struct ath10k *ar)
3018{
3019 struct ath10k_vif *arvif;
Michal Kaziorc930f742014-01-23 11:38:25 +01003020 int ret;
3021
3022 lockdep_assert_held(&ar->conf_mutex);
3023
Michal Kazior7aa7a722014-08-25 12:09:38 +02003024 ath10k_dbg(ar, ATH10K_DBG_MAC,
Michal Kaziorc930f742014-01-23 11:38:25 +01003025 "mac config channel to %dMHz (cf1 %dMHz cf2 %dMHz width %s)\n",
3026 ar->chandef.chan->center_freq,
3027 ar->chandef.center_freq1,
3028 ar->chandef.center_freq2,
3029 chandef_get_width(ar->chandef.width));
3030
3031 /* First stop monitor interface. Some FW versions crash if there's a
3032 * lone monitor interface. */
Michal Kazior1bbc0972014-04-08 09:45:47 +03003033 if (ar->monitor_started)
Michal Kazior19337472014-08-28 12:58:16 +02003034 ath10k_monitor_stop(ar);
Michal Kaziorc930f742014-01-23 11:38:25 +01003035
3036 list_for_each_entry(arvif, &ar->arvifs, list) {
3037 if (!arvif->is_started)
3038 continue;
3039
Michal Kaziordc55e302014-07-29 12:53:36 +03003040 if (!arvif->is_up)
3041 continue;
3042
Michal Kaziorc930f742014-01-23 11:38:25 +01003043 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
3044 continue;
3045
Michal Kaziordc55e302014-07-29 12:53:36 +03003046 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
Michal Kaziorc930f742014-01-23 11:38:25 +01003047 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003048 ath10k_warn(ar, "failed to down vdev %d: %d\n",
Michal Kaziorc930f742014-01-23 11:38:25 +01003049 arvif->vdev_id, ret);
3050 continue;
3051 }
3052 }
3053
Michal Kaziordc55e302014-07-29 12:53:36 +03003054 /* all vdevs are downed now - attempt to restart and re-up them */
Michal Kaziorc930f742014-01-23 11:38:25 +01003055
3056 list_for_each_entry(arvif, &ar->arvifs, list) {
3057 if (!arvif->is_started)
3058 continue;
3059
3060 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
3061 continue;
3062
Michal Kaziordc55e302014-07-29 12:53:36 +03003063 ret = ath10k_vdev_restart(arvif);
Michal Kaziorc930f742014-01-23 11:38:25 +01003064 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003065 ath10k_warn(ar, "failed to restart vdev %d: %d\n",
Michal Kaziorc930f742014-01-23 11:38:25 +01003066 arvif->vdev_id, ret);
3067 continue;
3068 }
3069
3070 if (!arvif->is_up)
3071 continue;
3072
3073 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
3074 arvif->bssid);
3075 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003076 ath10k_warn(ar, "failed to bring vdev up %d: %d\n",
Michal Kaziorc930f742014-01-23 11:38:25 +01003077 arvif->vdev_id, ret);
3078 continue;
3079 }
3080 }
3081
Michal Kazior19337472014-08-28 12:58:16 +02003082 ath10k_monitor_recalc(ar);
Michal Kaziorc930f742014-01-23 11:38:25 +01003083}
3084
Michal Kazior7d9d5582014-10-21 10:40:15 +03003085static int ath10k_mac_txpower_setup(struct ath10k *ar, int txpower)
3086{
3087 int ret;
3088 u32 param;
3089
3090 lockdep_assert_held(&ar->conf_mutex);
3091
3092 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac txpower %d\n", txpower);
3093
3094 param = ar->wmi.pdev_param->txpower_limit2g;
3095 ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
3096 if (ret) {
3097 ath10k_warn(ar, "failed to set 2g txpower %d: %d\n",
3098 txpower, ret);
3099 return ret;
3100 }
3101
3102 param = ar->wmi.pdev_param->txpower_limit5g;
3103 ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
3104 if (ret) {
3105 ath10k_warn(ar, "failed to set 5g txpower %d: %d\n",
3106 txpower, ret);
3107 return ret;
3108 }
3109
3110 return 0;
3111}
3112
3113static int ath10k_mac_txpower_recalc(struct ath10k *ar)
3114{
3115 struct ath10k_vif *arvif;
3116 int ret, txpower = -1;
3117
3118 lockdep_assert_held(&ar->conf_mutex);
3119
3120 list_for_each_entry(arvif, &ar->arvifs, list) {
3121 WARN_ON(arvif->txpower < 0);
3122
3123 if (txpower == -1)
3124 txpower = arvif->txpower;
3125 else
3126 txpower = min(txpower, arvif->txpower);
3127 }
3128
3129 if (WARN_ON(txpower == -1))
3130 return -EINVAL;
3131
3132 ret = ath10k_mac_txpower_setup(ar, txpower);
3133 if (ret) {
3134 ath10k_warn(ar, "failed to setup tx power %d: %d\n",
3135 txpower, ret);
3136 return ret;
3137 }
3138
3139 return 0;
3140}
3141
Kalle Valo5e3dd152013-06-12 20:52:10 +03003142static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
3143{
Kalle Valo5e3dd152013-06-12 20:52:10 +03003144 struct ath10k *ar = hw->priv;
3145 struct ieee80211_conf *conf = &hw->conf;
3146 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003147
3148 mutex_lock(&ar->conf_mutex);
3149
3150 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003151 ath10k_dbg(ar, ATH10K_DBG_MAC,
Michal Kaziord6500972014-04-08 09:56:09 +03003152 "mac config channel %dMHz flags 0x%x radar %d\n",
Marek Puzyniake8a50f82013-11-20 09:59:47 +02003153 conf->chandef.chan->center_freq,
Michal Kaziord6500972014-04-08 09:56:09 +03003154 conf->chandef.chan->flags,
3155 conf->radar_enabled);
Marek Puzyniake8a50f82013-11-20 09:59:47 +02003156
Kalle Valo5e3dd152013-06-12 20:52:10 +03003157 spin_lock_bh(&ar->data_lock);
3158 ar->rx_channel = conf->chandef.chan;
3159 spin_unlock_bh(&ar->data_lock);
Marek Puzyniake8a50f82013-11-20 09:59:47 +02003160
Michal Kaziord6500972014-04-08 09:56:09 +03003161 ar->radar_enabled = conf->radar_enabled;
3162 ath10k_recalc_radar_detection(ar);
Michal Kaziorc930f742014-01-23 11:38:25 +01003163
3164 if (!cfg80211_chandef_identical(&ar->chandef, &conf->chandef)) {
3165 ar->chandef = conf->chandef;
3166 ath10k_config_chan(ar);
3167 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003168 }
3169
Michal Kazioraffd3212013-07-16 09:54:35 +02003170 if (changed & IEEE80211_CONF_CHANGE_PS)
3171 ath10k_config_ps(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003172
3173 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
Michal Kazior19337472014-08-28 12:58:16 +02003174 ar->monitor = conf->flags & IEEE80211_CONF_MONITOR;
3175 ret = ath10k_monitor_recalc(ar);
3176 if (ret)
3177 ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003178 }
3179
3180 mutex_unlock(&ar->conf_mutex);
3181 return ret;
3182}
3183
Ben Greear5572a952014-11-24 16:22:10 +02003184static u32 get_nss_from_chainmask(u16 chain_mask)
3185{
3186 if ((chain_mask & 0x15) == 0x15)
3187 return 4;
3188 else if ((chain_mask & 0x7) == 0x7)
3189 return 3;
3190 else if ((chain_mask & 0x3) == 0x3)
3191 return 2;
3192 return 1;
3193}
3194
Kalle Valo5e3dd152013-06-12 20:52:10 +03003195/*
3196 * TODO:
3197 * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
3198 * because we will send mgmt frames without CCK. This requirement
3199 * for P2P_FIND/GO_NEG should be handled by checking CCK flag
3200 * in the TX packet.
3201 */
3202static int ath10k_add_interface(struct ieee80211_hw *hw,
3203 struct ieee80211_vif *vif)
3204{
3205 struct ath10k *ar = hw->priv;
3206 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3207 enum wmi_sta_powersave_param param;
3208 int ret = 0;
Kalle Valo5a13e762014-01-20 11:01:46 +02003209 u32 value;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003210 int bit;
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003211 u32 vdev_param;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003212
Johannes Berg848955c2014-11-11 12:48:42 +01003213 vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
3214
Kalle Valo5e3dd152013-06-12 20:52:10 +03003215 mutex_lock(&ar->conf_mutex);
3216
Michal Kazior0dbd09e2013-07-31 10:55:14 +02003217 memset(arvif, 0, sizeof(*arvif));
3218
Kalle Valo5e3dd152013-06-12 20:52:10 +03003219 arvif->ar = ar;
3220 arvif->vif = vif;
3221
Ben Greeare63b33f2013-10-22 14:54:14 -07003222 INIT_LIST_HEAD(&arvif->list);
Michal Kaziorcc4827b2013-10-16 15:44:45 +03003223
Ben Greeara9aefb32014-08-12 11:02:19 +03003224 if (ar->free_vdev_map == 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003225 ath10k_warn(ar, "Free vdev map is empty, no more interfaces allowed.\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03003226 ret = -EBUSY;
Michal Kazior9dad14a2013-10-16 15:44:45 +03003227 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003228 }
Ben Greear16c11172014-09-23 14:17:16 -07003229 bit = __ffs64(ar->free_vdev_map);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003230
Ben Greear16c11172014-09-23 14:17:16 -07003231 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac create vdev %i map %llx\n",
3232 bit, ar->free_vdev_map);
3233
3234 arvif->vdev_id = bit;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003235 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003236
Kalle Valo5e3dd152013-06-12 20:52:10 +03003237 switch (vif->type) {
Michal Kazior75d2bd42014-12-12 12:41:39 +01003238 case NL80211_IFTYPE_P2P_DEVICE:
3239 arvif->vdev_type = WMI_VDEV_TYPE_STA;
3240 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
3241 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003242 case NL80211_IFTYPE_UNSPECIFIED:
3243 case NL80211_IFTYPE_STATION:
3244 arvif->vdev_type = WMI_VDEV_TYPE_STA;
3245 if (vif->p2p)
3246 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
3247 break;
3248 case NL80211_IFTYPE_ADHOC:
3249 arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
3250 break;
3251 case NL80211_IFTYPE_AP:
3252 arvif->vdev_type = WMI_VDEV_TYPE_AP;
3253
3254 if (vif->p2p)
3255 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
3256 break;
3257 case NL80211_IFTYPE_MONITOR:
3258 arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
3259 break;
3260 default:
3261 WARN_ON(1);
3262 break;
3263 }
3264
Michal Kazior64badcb2014-09-18 11:18:02 +03003265 /* Some firmware revisions don't wait for beacon tx completion before
3266 * sending another SWBA event. This could lead to hardware using old
3267 * (freed) beacon data in some cases, e.g. tx credit starvation
3268 * combined with missed TBTT. This is very very rare.
3269 *
3270 * On non-IOMMU-enabled hosts this could be a possible security issue
3271 * because hw could beacon some random data on the air. On
3272 * IOMMU-enabled hosts DMAR faults would occur in most cases and target
3273 * device would crash.
3274 *
3275 * Since there are no beacon tx completions (implicit nor explicit)
3276 * propagated to host the only workaround for this is to allocate a
3277 * DMA-coherent buffer for a lifetime of a vif and use it for all
3278 * beacon tx commands. Worst case for this approach is some beacons may
3279 * become corrupted, e.g. have garbled IEs or out-of-date TIM bitmap.
3280 */
3281 if (vif->type == NL80211_IFTYPE_ADHOC ||
3282 vif->type == NL80211_IFTYPE_AP) {
3283 arvif->beacon_buf = dma_zalloc_coherent(ar->dev,
3284 IEEE80211_MAX_FRAME_LEN,
3285 &arvif->beacon_paddr,
Rajkumar Manoharan82d7aba2014-10-10 17:38:27 +05303286 GFP_ATOMIC);
Michal Kazior64badcb2014-09-18 11:18:02 +03003287 if (!arvif->beacon_buf) {
3288 ret = -ENOMEM;
3289 ath10k_warn(ar, "failed to allocate beacon buffer: %d\n",
3290 ret);
3291 goto err;
3292 }
3293 }
3294
3295 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d bcnmode %s\n",
3296 arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
3297 arvif->beacon_buf ? "single-buf" : "per-skb");
Kalle Valo5e3dd152013-06-12 20:52:10 +03003298
3299 ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
3300 arvif->vdev_subtype, vif->addr);
3301 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003302 ath10k_warn(ar, "failed to create WMI vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003303 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003304 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003305 }
3306
Ben Greear16c11172014-09-23 14:17:16 -07003307 ar->free_vdev_map &= ~(1LL << arvif->vdev_id);
Michal Kazior05791192013-10-16 15:44:45 +03003308 list_add(&arvif->list, &ar->arvifs);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003309
Michal Kazior46725b152015-01-28 09:57:49 +02003310 /* It makes no sense to have firmware do keepalives. mac80211 already
3311 * takes care of this with idle connection polling.
3312 */
3313 ret = ath10k_mac_vif_disable_keepalive(arvif);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003314 if (ret) {
Michal Kazior46725b152015-01-28 09:57:49 +02003315 ath10k_warn(ar, "failed to disable keepalive on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003316 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003317 goto err_vdev_delete;
3318 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003319
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +02003320 arvif->def_wep_key_idx = -1;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003321
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003322 vdev_param = ar->wmi.vdev_param->tx_encap_type;
3323 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003324 ATH10K_HW_TXRX_NATIVE_WIFI);
Bartosz Markowskiebc9abd2013-10-15 09:26:20 +02003325 /* 10.X firmware does not support this VDEV parameter. Do not warn */
Michal Kazior9dad14a2013-10-16 15:44:45 +03003326 if (ret && ret != -EOPNOTSUPP) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003327 ath10k_warn(ar, "failed to set vdev %i TX encapsulation: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003328 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003329 goto err_vdev_delete;
3330 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003331
Ben Greear5572a952014-11-24 16:22:10 +02003332 if (ar->cfg_tx_chainmask) {
3333 u16 nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
3334
3335 vdev_param = ar->wmi.vdev_param->nss;
3336 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3337 nss);
3338 if (ret) {
3339 ath10k_warn(ar, "failed to set vdev %i chainmask 0x%x, nss %i: %d\n",
3340 arvif->vdev_id, ar->cfg_tx_chainmask, nss,
3341 ret);
3342 goto err_vdev_delete;
3343 }
3344 }
3345
Kalle Valo5e3dd152013-06-12 20:52:10 +03003346 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3347 ret = ath10k_peer_create(ar, arvif->vdev_id, vif->addr);
3348 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003349 ath10k_warn(ar, "failed to create vdev %i peer for AP: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003350 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003351 goto err_vdev_delete;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003352 }
Marek Puzyniakcdf07402013-12-30 09:07:51 +01003353
Kalle Valo5a13e762014-01-20 11:01:46 +02003354 ret = ath10k_mac_set_kickout(arvif);
3355 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003356 ath10k_warn(ar, "failed to set vdev %i kickout parameters: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003357 arvif->vdev_id, ret);
Kalle Valo5a13e762014-01-20 11:01:46 +02003358 goto err_peer_delete;
3359 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003360 }
3361
3362 if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
3363 param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
3364 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
3365 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3366 param, value);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003367 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003368 ath10k_warn(ar, "failed to set vdev %i RX wake policy: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003369 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003370 goto err_peer_delete;
3371 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003372
Michal Kazior9f9b5742014-12-12 12:41:36 +01003373 ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003374 if (ret) {
Michal Kazior9f9b5742014-12-12 12:41:36 +01003375 ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003376 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003377 goto err_peer_delete;
3378 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003379
Michal Kazior9f9b5742014-12-12 12:41:36 +01003380 ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003381 if (ret) {
Michal Kazior9f9b5742014-12-12 12:41:36 +01003382 ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003383 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003384 goto err_peer_delete;
3385 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003386 }
3387
Michal Kazior424121c2013-07-22 14:13:31 +02003388 ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003389 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003390 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
Michal Kazior679c54a2013-07-05 16:15:04 +03003391 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003392 goto err_peer_delete;
3393 }
Michal Kazior679c54a2013-07-05 16:15:04 +03003394
Michal Kazior424121c2013-07-22 14:13:31 +02003395 ret = ath10k_mac_set_frag(arvif, ar->hw->wiphy->frag_threshold);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003396 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003397 ath10k_warn(ar, "failed to set frag threshold for vdev %d: %d\n",
Michal Kazior679c54a2013-07-05 16:15:04 +03003398 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003399 goto err_peer_delete;
3400 }
Michal Kazior679c54a2013-07-05 16:15:04 +03003401
Michal Kazior7d9d5582014-10-21 10:40:15 +03003402 arvif->txpower = vif->bss_conf.txpower;
3403 ret = ath10k_mac_txpower_recalc(ar);
3404 if (ret) {
3405 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
3406 goto err_peer_delete;
3407 }
3408
Kalle Valo5e3dd152013-06-12 20:52:10 +03003409 mutex_unlock(&ar->conf_mutex);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003410 return 0;
3411
3412err_peer_delete:
3413 if (arvif->vdev_type == WMI_VDEV_TYPE_AP)
3414 ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
3415
3416err_vdev_delete:
3417 ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
Ben Greear16c11172014-09-23 14:17:16 -07003418 ar->free_vdev_map |= 1LL << arvif->vdev_id;
Michal Kazior05791192013-10-16 15:44:45 +03003419 list_del(&arvif->list);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003420
3421err:
Michal Kazior64badcb2014-09-18 11:18:02 +03003422 if (arvif->beacon_buf) {
3423 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
3424 arvif->beacon_buf, arvif->beacon_paddr);
3425 arvif->beacon_buf = NULL;
3426 }
3427
Michal Kazior9dad14a2013-10-16 15:44:45 +03003428 mutex_unlock(&ar->conf_mutex);
3429
Kalle Valo5e3dd152013-06-12 20:52:10 +03003430 return ret;
3431}
3432
3433static void ath10k_remove_interface(struct ieee80211_hw *hw,
3434 struct ieee80211_vif *vif)
3435{
3436 struct ath10k *ar = hw->priv;
3437 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3438 int ret;
3439
Sujith Manoharan5d011f52014-11-25 11:47:00 +05303440 mutex_lock(&ar->conf_mutex);
3441
Michal Kaziored543882013-09-13 14:16:56 +02003442 spin_lock_bh(&ar->data_lock);
Michal Kazior64badcb2014-09-18 11:18:02 +03003443 ath10k_mac_vif_beacon_cleanup(arvif);
Michal Kaziored543882013-09-13 14:16:56 +02003444 spin_unlock_bh(&ar->data_lock);
3445
Simon Wunderlich855aed12014-08-02 09:12:54 +03003446 ret = ath10k_spectral_vif_stop(arvif);
3447 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003448 ath10k_warn(ar, "failed to stop spectral for vdev %i: %d\n",
Simon Wunderlich855aed12014-08-02 09:12:54 +03003449 arvif->vdev_id, ret);
3450
Ben Greear16c11172014-09-23 14:17:16 -07003451 ar->free_vdev_map |= 1LL << arvif->vdev_id;
Michal Kazior05791192013-10-16 15:44:45 +03003452 list_del(&arvif->list);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003453
3454 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
Michal Kazior2c512052015-02-15 16:50:40 +02003455 ret = ath10k_wmi_peer_delete(arvif->ar, arvif->vdev_id,
3456 vif->addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003457 if (ret)
Michal Kazior2c512052015-02-15 16:50:40 +02003458 ath10k_warn(ar, "failed to submit AP self-peer removal on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003459 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003460
3461 kfree(arvif->u.ap.noa_data);
3462 }
3463
Michal Kazior7aa7a722014-08-25 12:09:38 +02003464 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i delete (remove interface)\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03003465 arvif->vdev_id);
3466
Kalle Valo5e3dd152013-06-12 20:52:10 +03003467 ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
3468 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003469 ath10k_warn(ar, "failed to delete WMI vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003470 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003471
Michal Kazior2c512052015-02-15 16:50:40 +02003472 /* Some firmware revisions don't notify host about self-peer removal
3473 * until after associated vdev is deleted.
3474 */
3475 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3476 ret = ath10k_wait_for_peer_deleted(ar, arvif->vdev_id,
3477 vif->addr);
3478 if (ret)
3479 ath10k_warn(ar, "failed to remove AP self-peer on vdev %i: %d\n",
3480 arvif->vdev_id, ret);
3481
3482 spin_lock_bh(&ar->data_lock);
3483 ar->num_peers--;
3484 spin_unlock_bh(&ar->data_lock);
3485 }
3486
Kalle Valo5e3dd152013-06-12 20:52:10 +03003487 ath10k_peer_cleanup(ar, arvif->vdev_id);
3488
3489 mutex_unlock(&ar->conf_mutex);
3490}
3491
3492/*
3493 * FIXME: Has to be verified.
3494 */
3495#define SUPPORTED_FILTERS \
3496 (FIF_PROMISC_IN_BSS | \
3497 FIF_ALLMULTI | \
3498 FIF_CONTROL | \
3499 FIF_PSPOLL | \
3500 FIF_OTHER_BSS | \
3501 FIF_BCN_PRBRESP_PROMISC | \
3502 FIF_PROBE_REQ | \
3503 FIF_FCSFAIL)
3504
3505static void ath10k_configure_filter(struct ieee80211_hw *hw,
3506 unsigned int changed_flags,
3507 unsigned int *total_flags,
3508 u64 multicast)
3509{
3510 struct ath10k *ar = hw->priv;
3511 int ret;
3512
3513 mutex_lock(&ar->conf_mutex);
3514
3515 changed_flags &= SUPPORTED_FILTERS;
3516 *total_flags &= SUPPORTED_FILTERS;
3517 ar->filter_flags = *total_flags;
3518
Michal Kazior19337472014-08-28 12:58:16 +02003519 ret = ath10k_monitor_recalc(ar);
3520 if (ret)
3521 ath10k_warn(ar, "failed to recalc montior: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003522
3523 mutex_unlock(&ar->conf_mutex);
3524}
3525
3526static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
3527 struct ieee80211_vif *vif,
3528 struct ieee80211_bss_conf *info,
3529 u32 changed)
3530{
3531 struct ath10k *ar = hw->priv;
3532 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3533 int ret = 0;
Kalle Valoaf762c02014-09-14 12:50:17 +03003534 u32 vdev_param, pdev_param, slottime, preamble;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003535
3536 mutex_lock(&ar->conf_mutex);
3537
3538 if (changed & BSS_CHANGED_IBSS)
3539 ath10k_control_ibss(arvif, info, vif->addr);
3540
3541 if (changed & BSS_CHANGED_BEACON_INT) {
3542 arvif->beacon_interval = info->beacon_int;
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003543 vdev_param = ar->wmi.vdev_param->beacon_interval;
3544 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003545 arvif->beacon_interval);
Michal Kazior7aa7a722014-08-25 12:09:38 +02003546 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003547 "mac vdev %d beacon_interval %d\n",
3548 arvif->vdev_id, arvif->beacon_interval);
3549
Kalle Valo5e3dd152013-06-12 20:52:10 +03003550 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003551 ath10k_warn(ar, "failed to set beacon interval for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003552 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003553 }
3554
3555 if (changed & BSS_CHANGED_BEACON) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003556 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003557 "vdev %d set beacon tx mode to staggered\n",
3558 arvif->vdev_id);
3559
Bartosz Markowski226a3392013-09-26 17:47:16 +02003560 pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
3561 ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003562 WMI_BEACON_STAGGERED_MODE);
3563 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003564 ath10k_warn(ar, "failed to set beacon mode for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003565 arvif->vdev_id, ret);
Michal Kaziorfbb8f1b2015-01-13 16:30:12 +02003566
3567 ret = ath10k_mac_setup_bcn_tmpl(arvif);
3568 if (ret)
3569 ath10k_warn(ar, "failed to update beacon template: %d\n",
3570 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003571 }
3572
Michal Kaziorfbb8f1b2015-01-13 16:30:12 +02003573 if (changed & BSS_CHANGED_AP_PROBE_RESP) {
3574 ret = ath10k_mac_setup_prb_tmpl(arvif);
3575 if (ret)
3576 ath10k_warn(ar, "failed to setup probe resp template on vdev %i: %d\n",
3577 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003578 }
3579
Michal Kaziorba2479f2015-01-24 12:14:51 +02003580 if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03003581 arvif->dtim_period = info->dtim_period;
3582
Michal Kazior7aa7a722014-08-25 12:09:38 +02003583 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003584 "mac vdev %d dtim_period %d\n",
3585 arvif->vdev_id, arvif->dtim_period);
3586
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003587 vdev_param = ar->wmi.vdev_param->dtim_period;
3588 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003589 arvif->dtim_period);
3590 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003591 ath10k_warn(ar, "failed to set dtim period for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003592 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003593 }
3594
3595 if (changed & BSS_CHANGED_SSID &&
3596 vif->type == NL80211_IFTYPE_AP) {
3597 arvif->u.ap.ssid_len = info->ssid_len;
3598 if (info->ssid_len)
3599 memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
3600 arvif->u.ap.hidden_ssid = info->hidden_ssid;
3601 }
3602
Michal Kazior077efc82014-10-21 10:10:29 +03003603 if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
3604 ether_addr_copy(arvif->bssid, info->bssid);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003605
3606 if (changed & BSS_CHANGED_BEACON_ENABLED)
3607 ath10k_control_beaconing(arvif, info);
3608
3609 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02003610 arvif->use_cts_prot = info->use_cts_prot;
Michal Kazior7aa7a722014-08-25 12:09:38 +02003611 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d cts_prot %d\n",
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02003612 arvif->vdev_id, info->use_cts_prot);
Kalle Valo60c3daa2013-09-08 17:56:07 +03003613
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02003614 ret = ath10k_recalc_rtscts_prot(arvif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003615 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003616 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003617 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003618 }
3619
3620 if (changed & BSS_CHANGED_ERP_SLOT) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03003621 if (info->use_short_slot)
3622 slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
3623
3624 else
3625 slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
3626
Michal Kazior7aa7a722014-08-25 12:09:38 +02003627 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03003628 arvif->vdev_id, slottime);
3629
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003630 vdev_param = ar->wmi.vdev_param->slot_time;
3631 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003632 slottime);
3633 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003634 ath10k_warn(ar, "failed to set erp slot for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003635 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003636 }
3637
3638 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03003639 if (info->use_short_preamble)
3640 preamble = WMI_VDEV_PREAMBLE_SHORT;
3641 else
3642 preamble = WMI_VDEV_PREAMBLE_LONG;
3643
Michal Kazior7aa7a722014-08-25 12:09:38 +02003644 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003645 "mac vdev %d preamble %dn",
3646 arvif->vdev_id, preamble);
3647
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003648 vdev_param = ar->wmi.vdev_param->preamble;
3649 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003650 preamble);
3651 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003652 ath10k_warn(ar, "failed to set preamble for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003653 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003654 }
3655
3656 if (changed & BSS_CHANGED_ASSOC) {
Michal Kaziore556f112014-08-28 12:58:17 +02003657 if (info->assoc) {
3658 /* Workaround: Make sure monitor vdev is not running
3659 * when associating to prevent some firmware revisions
3660 * (e.g. 10.1 and 10.2) from crashing.
3661 */
3662 if (ar->monitor_started)
3663 ath10k_monitor_stop(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003664 ath10k_bss_assoc(hw, vif, info);
Michal Kaziore556f112014-08-28 12:58:17 +02003665 ath10k_monitor_recalc(ar);
Michal Kazior077efc82014-10-21 10:10:29 +03003666 } else {
3667 ath10k_bss_disassoc(hw, vif);
Michal Kaziore556f112014-08-28 12:58:17 +02003668 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003669 }
3670
Michal Kazior7d9d5582014-10-21 10:40:15 +03003671 if (changed & BSS_CHANGED_TXPOWER) {
3672 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev_id %i txpower %d\n",
3673 arvif->vdev_id, info->txpower);
3674
3675 arvif->txpower = info->txpower;
3676 ret = ath10k_mac_txpower_recalc(ar);
3677 if (ret)
3678 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
3679 }
3680
Michal Kaziorbf14e652014-12-12 12:41:38 +01003681 if (changed & BSS_CHANGED_PS) {
Michal Kaziorcffb41f2015-02-13 13:30:16 +01003682 arvif->ps = vif->bss_conf.ps;
3683
3684 ret = ath10k_config_ps(ar);
Michal Kaziorbf14e652014-12-12 12:41:38 +01003685 if (ret)
3686 ath10k_warn(ar, "failed to setup ps on vdev %i: %d\n",
3687 arvif->vdev_id, ret);
3688 }
3689
Kalle Valo5e3dd152013-06-12 20:52:10 +03003690 mutex_unlock(&ar->conf_mutex);
3691}
3692
3693static int ath10k_hw_scan(struct ieee80211_hw *hw,
3694 struct ieee80211_vif *vif,
David Spinadelc56ef672014-02-05 15:21:13 +02003695 struct ieee80211_scan_request *hw_req)
Kalle Valo5e3dd152013-06-12 20:52:10 +03003696{
3697 struct ath10k *ar = hw->priv;
3698 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
David Spinadelc56ef672014-02-05 15:21:13 +02003699 struct cfg80211_scan_request *req = &hw_req->req;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003700 struct wmi_start_scan_arg arg;
3701 int ret = 0;
3702 int i;
3703
3704 mutex_lock(&ar->conf_mutex);
3705
3706 spin_lock_bh(&ar->data_lock);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003707 switch (ar->scan.state) {
3708 case ATH10K_SCAN_IDLE:
3709 reinit_completion(&ar->scan.started);
3710 reinit_completion(&ar->scan.completed);
3711 ar->scan.state = ATH10K_SCAN_STARTING;
3712 ar->scan.is_roc = false;
3713 ar->scan.vdev_id = arvif->vdev_id;
3714 ret = 0;
3715 break;
3716 case ATH10K_SCAN_STARTING:
3717 case ATH10K_SCAN_RUNNING:
3718 case ATH10K_SCAN_ABORTING:
Kalle Valo5e3dd152013-06-12 20:52:10 +03003719 ret = -EBUSY;
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003720 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003721 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003722 spin_unlock_bh(&ar->data_lock);
3723
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003724 if (ret)
3725 goto exit;
3726
Kalle Valo5e3dd152013-06-12 20:52:10 +03003727 memset(&arg, 0, sizeof(arg));
3728 ath10k_wmi_start_scan_init(ar, &arg);
3729 arg.vdev_id = arvif->vdev_id;
3730 arg.scan_id = ATH10K_SCAN_ID;
3731
3732 if (!req->no_cck)
3733 arg.scan_ctrl_flags |= WMI_SCAN_ADD_CCK_RATES;
3734
3735 if (req->ie_len) {
3736 arg.ie_len = req->ie_len;
3737 memcpy(arg.ie, req->ie, arg.ie_len);
3738 }
3739
3740 if (req->n_ssids) {
3741 arg.n_ssids = req->n_ssids;
3742 for (i = 0; i < arg.n_ssids; i++) {
3743 arg.ssids[i].len = req->ssids[i].ssid_len;
3744 arg.ssids[i].ssid = req->ssids[i].ssid;
3745 }
Michal Kaziordcd4a562013-07-31 10:55:12 +02003746 } else {
3747 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003748 }
3749
3750 if (req->n_channels) {
3751 arg.n_channels = req->n_channels;
3752 for (i = 0; i < arg.n_channels; i++)
3753 arg.channels[i] = req->channels[i]->center_freq;
3754 }
3755
3756 ret = ath10k_start_scan(ar, &arg);
3757 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003758 ath10k_warn(ar, "failed to start hw scan: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003759 spin_lock_bh(&ar->data_lock);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003760 ar->scan.state = ATH10K_SCAN_IDLE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003761 spin_unlock_bh(&ar->data_lock);
3762 }
3763
3764exit:
3765 mutex_unlock(&ar->conf_mutex);
3766 return ret;
3767}
3768
3769static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
3770 struct ieee80211_vif *vif)
3771{
3772 struct ath10k *ar = hw->priv;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003773
3774 mutex_lock(&ar->conf_mutex);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003775 ath10k_scan_abort(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003776 mutex_unlock(&ar->conf_mutex);
Michal Kazior4eb2e162014-10-28 10:23:09 +01003777
3778 cancel_delayed_work_sync(&ar->scan.timeout);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003779}
3780
Michal Kaziorcfb27d22013-12-02 09:06:36 +01003781static void ath10k_set_key_h_def_keyidx(struct ath10k *ar,
3782 struct ath10k_vif *arvif,
3783 enum set_key_cmd cmd,
3784 struct ieee80211_key_conf *key)
3785{
3786 u32 vdev_param = arvif->ar->wmi.vdev_param->def_keyid;
3787 int ret;
3788
3789 /* 10.1 firmware branch requires default key index to be set to group
3790 * key index after installing it. Otherwise FW/HW Txes corrupted
3791 * frames with multi-vif APs. This is not required for main firmware
3792 * branch (e.g. 636).
3793 *
3794 * FIXME: This has been tested only in AP. It remains unknown if this
3795 * is required for multi-vif STA interfaces on 10.1 */
3796
3797 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
3798 return;
3799
3800 if (key->cipher == WLAN_CIPHER_SUITE_WEP40)
3801 return;
3802
3803 if (key->cipher == WLAN_CIPHER_SUITE_WEP104)
3804 return;
3805
3806 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3807 return;
3808
3809 if (cmd != SET_KEY)
3810 return;
3811
3812 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3813 key->keyidx);
3814 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003815 ath10k_warn(ar, "failed to set vdev %i group key as default key: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003816 arvif->vdev_id, ret);
Michal Kaziorcfb27d22013-12-02 09:06:36 +01003817}
3818
Kalle Valo5e3dd152013-06-12 20:52:10 +03003819static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
3820 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
3821 struct ieee80211_key_conf *key)
3822{
3823 struct ath10k *ar = hw->priv;
3824 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3825 struct ath10k_peer *peer;
3826 const u8 *peer_addr;
3827 bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3828 key->cipher == WLAN_CIPHER_SUITE_WEP104;
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +02003829 bool def_idx = false;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003830 int ret = 0;
3831
3832 if (key->keyidx > WMI_MAX_KEY_INDEX)
3833 return -ENOSPC;
3834
3835 mutex_lock(&ar->conf_mutex);
3836
3837 if (sta)
3838 peer_addr = sta->addr;
3839 else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
3840 peer_addr = vif->bss_conf.bssid;
3841 else
3842 peer_addr = vif->addr;
3843
3844 key->hw_key_idx = key->keyidx;
3845
3846 /* the peer should not disappear in mid-way (unless FW goes awry) since
3847 * we already hold conf_mutex. we just make sure its there now. */
3848 spin_lock_bh(&ar->data_lock);
3849 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
3850 spin_unlock_bh(&ar->data_lock);
3851
3852 if (!peer) {
3853 if (cmd == SET_KEY) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003854 ath10k_warn(ar, "failed to install key for non-existent peer %pM\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03003855 peer_addr);
3856 ret = -EOPNOTSUPP;
3857 goto exit;
3858 } else {
3859 /* if the peer doesn't exist there is no key to disable
3860 * anymore */
3861 goto exit;
3862 }
3863 }
3864
3865 if (is_wep) {
3866 if (cmd == SET_KEY)
3867 arvif->wep_keys[key->keyidx] = key;
3868 else
3869 arvif->wep_keys[key->keyidx] = NULL;
3870
3871 if (cmd == DISABLE_KEY)
3872 ath10k_clear_vdev_key(arvif, key);
3873 }
3874
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +02003875 /* set TX_USAGE flag for all the keys incase of dot1x-WEP. For
3876 * static WEP, do not set this flag for the keys whose key id
3877 * is greater than default key id.
3878 */
3879 if (arvif->def_wep_key_idx == -1)
3880 def_idx = true;
3881
3882 ret = ath10k_install_key(arvif, key, cmd, peer_addr, def_idx);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003883 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003884 ath10k_warn(ar, "failed to install key for vdev %i peer %pM: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003885 arvif->vdev_id, peer_addr, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003886 goto exit;
3887 }
3888
Michal Kaziorcfb27d22013-12-02 09:06:36 +01003889 ath10k_set_key_h_def_keyidx(ar, arvif, cmd, key);
3890
Kalle Valo5e3dd152013-06-12 20:52:10 +03003891 spin_lock_bh(&ar->data_lock);
3892 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
3893 if (peer && cmd == SET_KEY)
3894 peer->keys[key->keyidx] = key;
3895 else if (peer && cmd == DISABLE_KEY)
3896 peer->keys[key->keyidx] = NULL;
3897 else if (peer == NULL)
3898 /* impossible unless FW goes crazy */
Michal Kazior7aa7a722014-08-25 12:09:38 +02003899 ath10k_warn(ar, "Peer %pM disappeared!\n", peer_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003900 spin_unlock_bh(&ar->data_lock);
3901
3902exit:
3903 mutex_unlock(&ar->conf_mutex);
3904 return ret;
3905}
3906
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +02003907static void ath10k_set_default_unicast_key(struct ieee80211_hw *hw,
3908 struct ieee80211_vif *vif,
3909 int keyidx)
3910{
3911 struct ath10k *ar = hw->priv;
3912 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3913 int ret;
3914
3915 mutex_lock(&arvif->ar->conf_mutex);
3916
3917 if (arvif->ar->state != ATH10K_STATE_ON)
3918 goto unlock;
3919
3920 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
3921 arvif->vdev_id, keyidx);
3922
3923 ret = ath10k_wmi_vdev_set_param(arvif->ar,
3924 arvif->vdev_id,
3925 arvif->ar->wmi.vdev_param->def_keyid,
3926 keyidx);
3927
3928 if (ret) {
3929 ath10k_warn(ar, "failed to update wep key index for vdev %d: %d\n",
3930 arvif->vdev_id,
3931 ret);
3932 goto unlock;
3933 }
3934
3935 arvif->def_wep_key_idx = keyidx;
3936unlock:
3937 mutex_unlock(&arvif->ar->conf_mutex);
3938}
3939
Michal Kazior9797feb2014-02-14 14:49:48 +01003940static void ath10k_sta_rc_update_wk(struct work_struct *wk)
3941{
3942 struct ath10k *ar;
3943 struct ath10k_vif *arvif;
3944 struct ath10k_sta *arsta;
3945 struct ieee80211_sta *sta;
3946 u32 changed, bw, nss, smps;
3947 int err;
3948
3949 arsta = container_of(wk, struct ath10k_sta, update_wk);
3950 sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
3951 arvif = arsta->arvif;
3952 ar = arvif->ar;
3953
3954 spin_lock_bh(&ar->data_lock);
3955
3956 changed = arsta->changed;
3957 arsta->changed = 0;
3958
3959 bw = arsta->bw;
3960 nss = arsta->nss;
3961 smps = arsta->smps;
3962
3963 spin_unlock_bh(&ar->data_lock);
3964
3965 mutex_lock(&ar->conf_mutex);
3966
3967 if (changed & IEEE80211_RC_BW_CHANGED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003968 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM peer bw %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003969 sta->addr, bw);
3970
3971 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
3972 WMI_PEER_CHAN_WIDTH, bw);
3973 if (err)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003974 ath10k_warn(ar, "failed to update STA %pM peer bw %d: %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003975 sta->addr, bw, err);
3976 }
3977
3978 if (changed & IEEE80211_RC_NSS_CHANGED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003979 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM nss %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003980 sta->addr, nss);
3981
3982 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
3983 WMI_PEER_NSS, nss);
3984 if (err)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003985 ath10k_warn(ar, "failed to update STA %pM nss %d: %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003986 sta->addr, nss, err);
3987 }
3988
3989 if (changed & IEEE80211_RC_SMPS_CHANGED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003990 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM smps %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003991 sta->addr, smps);
3992
3993 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
3994 WMI_PEER_SMPS_STATE, smps);
3995 if (err)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003996 ath10k_warn(ar, "failed to update STA %pM smps %d: %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003997 sta->addr, smps, err);
3998 }
3999
Janusz Dziedzic55884c02014-12-17 12:30:02 +02004000 if (changed & IEEE80211_RC_SUPP_RATES_CHANGED ||
4001 changed & IEEE80211_RC_NSS_CHANGED) {
4002 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM supp rates/nss\n",
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02004003 sta->addr);
4004
Michal Kazior590922a2014-10-21 10:10:29 +03004005 err = ath10k_station_assoc(ar, arvif->vif, sta, true);
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02004006 if (err)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004007 ath10k_warn(ar, "failed to reassociate station: %pM\n",
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02004008 sta->addr);
4009 }
4010
Michal Kazior9797feb2014-02-14 14:49:48 +01004011 mutex_unlock(&ar->conf_mutex);
4012}
4013
Michal Kaziorcfd10612014-11-25 15:16:05 +01004014static int ath10k_mac_inc_num_stations(struct ath10k_vif *arvif)
4015{
4016 struct ath10k *ar = arvif->ar;
4017
4018 lockdep_assert_held(&ar->conf_mutex);
4019
4020 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
4021 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
4022 return 0;
4023
4024 if (ar->num_stations >= ar->max_num_stations)
4025 return -ENOBUFS;
4026
4027 ar->num_stations++;
4028
4029 return 0;
4030}
4031
4032static void ath10k_mac_dec_num_stations(struct ath10k_vif *arvif)
4033{
4034 struct ath10k *ar = arvif->ar;
4035
4036 lockdep_assert_held(&ar->conf_mutex);
4037
4038 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
4039 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
4040 return;
4041
4042 ar->num_stations--;
4043}
4044
Kalle Valo5e3dd152013-06-12 20:52:10 +03004045static int ath10k_sta_state(struct ieee80211_hw *hw,
4046 struct ieee80211_vif *vif,
4047 struct ieee80211_sta *sta,
4048 enum ieee80211_sta_state old_state,
4049 enum ieee80211_sta_state new_state)
4050{
4051 struct ath10k *ar = hw->priv;
4052 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Michal Kazior9797feb2014-02-14 14:49:48 +01004053 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004054 int ret = 0;
4055
Michal Kazior76f90022014-02-25 09:29:57 +02004056 if (old_state == IEEE80211_STA_NOTEXIST &&
4057 new_state == IEEE80211_STA_NONE) {
4058 memset(arsta, 0, sizeof(*arsta));
4059 arsta->arvif = arvif;
4060 INIT_WORK(&arsta->update_wk, ath10k_sta_rc_update_wk);
4061 }
4062
Michal Kazior9797feb2014-02-14 14:49:48 +01004063 /* cancel must be done outside the mutex to avoid deadlock */
4064 if ((old_state == IEEE80211_STA_NONE &&
4065 new_state == IEEE80211_STA_NOTEXIST))
4066 cancel_work_sync(&arsta->update_wk);
4067
Kalle Valo5e3dd152013-06-12 20:52:10 +03004068 mutex_lock(&ar->conf_mutex);
4069
4070 if (old_state == IEEE80211_STA_NOTEXIST &&
Michal Kazior077efc82014-10-21 10:10:29 +03004071 new_state == IEEE80211_STA_NONE) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03004072 /*
4073 * New station addition.
4074 */
Michal Kaziorcfd10612014-11-25 15:16:05 +01004075 ath10k_dbg(ar, ATH10K_DBG_MAC,
4076 "mac vdev %d peer create %pM (new sta) sta %d / %d peer %d / %d\n",
4077 arvif->vdev_id, sta->addr,
4078 ar->num_stations + 1, ar->max_num_stations,
4079 ar->num_peers + 1, ar->max_num_peers);
Bartosz Markowski0e759f32014-01-02 14:38:33 +01004080
Michal Kaziorcfd10612014-11-25 15:16:05 +01004081 ret = ath10k_mac_inc_num_stations(arvif);
4082 if (ret) {
4083 ath10k_warn(ar, "refusing to associate station: too many connected already (%d)\n",
4084 ar->max_num_stations);
Bartosz Markowski0e759f32014-01-02 14:38:33 +01004085 goto exit;
4086 }
4087
Kalle Valo5e3dd152013-06-12 20:52:10 +03004088 ret = ath10k_peer_create(ar, arvif->vdev_id, sta->addr);
Michal Kaziora52c0282014-11-25 15:16:03 +01004089 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004090 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 -08004091 sta->addr, arvif->vdev_id, ret);
Michal Kaziorcfd10612014-11-25 15:16:05 +01004092 ath10k_mac_dec_num_stations(arvif);
Michal Kaziora52c0282014-11-25 15:16:03 +01004093 goto exit;
4094 }
Michal Kazior077efc82014-10-21 10:10:29 +03004095
4096 if (vif->type == NL80211_IFTYPE_STATION) {
4097 WARN_ON(arvif->is_started);
4098
4099 ret = ath10k_vdev_start(arvif);
4100 if (ret) {
4101 ath10k_warn(ar, "failed to start vdev %i: %d\n",
4102 arvif->vdev_id, ret);
4103 WARN_ON(ath10k_peer_delete(ar, arvif->vdev_id,
4104 sta->addr));
Michal Kaziorcfd10612014-11-25 15:16:05 +01004105 ath10k_mac_dec_num_stations(arvif);
Michal Kazior077efc82014-10-21 10:10:29 +03004106 goto exit;
4107 }
4108
4109 arvif->is_started = true;
4110 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03004111 } else if ((old_state == IEEE80211_STA_NONE &&
4112 new_state == IEEE80211_STA_NOTEXIST)) {
4113 /*
4114 * Existing station deletion.
4115 */
Michal Kazior7aa7a722014-08-25 12:09:38 +02004116 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03004117 "mac vdev %d peer delete %pM (sta gone)\n",
4118 arvif->vdev_id, sta->addr);
Michal Kazior077efc82014-10-21 10:10:29 +03004119
4120 if (vif->type == NL80211_IFTYPE_STATION) {
4121 WARN_ON(!arvif->is_started);
4122
4123 ret = ath10k_vdev_stop(arvif);
4124 if (ret)
4125 ath10k_warn(ar, "failed to stop vdev %i: %d\n",
4126 arvif->vdev_id, ret);
4127
4128 arvif->is_started = false;
4129 }
4130
Kalle Valo5e3dd152013-06-12 20:52:10 +03004131 ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
4132 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004133 ath10k_warn(ar, "failed to delete peer %pM for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02004134 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004135
Michal Kaziorcfd10612014-11-25 15:16:05 +01004136 ath10k_mac_dec_num_stations(arvif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004137 } else if (old_state == IEEE80211_STA_AUTH &&
4138 new_state == IEEE80211_STA_ASSOC &&
4139 (vif->type == NL80211_IFTYPE_AP ||
4140 vif->type == NL80211_IFTYPE_ADHOC)) {
4141 /*
4142 * New association.
4143 */
Michal Kazior7aa7a722014-08-25 12:09:38 +02004144 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM associated\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03004145 sta->addr);
4146
Michal Kazior590922a2014-10-21 10:10:29 +03004147 ret = ath10k_station_assoc(ar, vif, sta, false);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004148 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004149 ath10k_warn(ar, "failed to associate station %pM for vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02004150 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004151 } else if (old_state == IEEE80211_STA_ASSOC &&
4152 new_state == IEEE80211_STA_AUTH &&
4153 (vif->type == NL80211_IFTYPE_AP ||
4154 vif->type == NL80211_IFTYPE_ADHOC)) {
4155 /*
4156 * Disassociation.
4157 */
Michal Kazior7aa7a722014-08-25 12:09:38 +02004158 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM disassociated\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03004159 sta->addr);
4160
Michal Kazior590922a2014-10-21 10:10:29 +03004161 ret = ath10k_station_disassoc(ar, vif, sta);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004162 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004163 ath10k_warn(ar, "failed to disassociate station: %pM vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02004164 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004165 }
Bartosz Markowski0e759f32014-01-02 14:38:33 +01004166exit:
Kalle Valo5e3dd152013-06-12 20:52:10 +03004167 mutex_unlock(&ar->conf_mutex);
4168 return ret;
4169}
4170
4171static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
Kalle Valo5b07e072014-09-14 12:50:06 +03004172 u16 ac, bool enable)
Kalle Valo5e3dd152013-06-12 20:52:10 +03004173{
4174 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Michal Kaziorb0e56152015-01-24 12:14:52 +02004175 struct wmi_sta_uapsd_auto_trig_arg arg = {};
4176 u32 prio = 0, acc = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004177 u32 value = 0;
4178 int ret = 0;
4179
Michal Kazior548db542013-07-05 16:15:15 +03004180 lockdep_assert_held(&ar->conf_mutex);
4181
Kalle Valo5e3dd152013-06-12 20:52:10 +03004182 if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
4183 return 0;
4184
4185 switch (ac) {
4186 case IEEE80211_AC_VO:
4187 value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
4188 WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
Michal Kaziorb0e56152015-01-24 12:14:52 +02004189 prio = 7;
4190 acc = 3;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004191 break;
4192 case IEEE80211_AC_VI:
4193 value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
4194 WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
Michal Kaziorb0e56152015-01-24 12:14:52 +02004195 prio = 5;
4196 acc = 2;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004197 break;
4198 case IEEE80211_AC_BE:
4199 value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
4200 WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
Michal Kaziorb0e56152015-01-24 12:14:52 +02004201 prio = 2;
4202 acc = 1;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004203 break;
4204 case IEEE80211_AC_BK:
4205 value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
4206 WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
Michal Kaziorb0e56152015-01-24 12:14:52 +02004207 prio = 0;
4208 acc = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004209 break;
4210 }
4211
4212 if (enable)
4213 arvif->u.sta.uapsd |= value;
4214 else
4215 arvif->u.sta.uapsd &= ~value;
4216
4217 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4218 WMI_STA_PS_PARAM_UAPSD,
4219 arvif->u.sta.uapsd);
4220 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004221 ath10k_warn(ar, "failed to set uapsd params: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004222 goto exit;
4223 }
4224
4225 if (arvif->u.sta.uapsd)
4226 value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
4227 else
4228 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
4229
4230 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4231 WMI_STA_PS_PARAM_RX_WAKE_POLICY,
4232 value);
4233 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004234 ath10k_warn(ar, "failed to set rx wake param: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004235
Michal Kazior9f9b5742014-12-12 12:41:36 +01004236 ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
4237 if (ret) {
4238 ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
4239 arvif->vdev_id, ret);
4240 return ret;
4241 }
4242
4243 ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
4244 if (ret) {
4245 ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
4246 arvif->vdev_id, ret);
4247 return ret;
4248 }
4249
Michal Kaziorb0e56152015-01-24 12:14:52 +02004250 if (test_bit(WMI_SERVICE_STA_UAPSD_BASIC_AUTO_TRIG, ar->wmi.svc_map) ||
4251 test_bit(WMI_SERVICE_STA_UAPSD_VAR_AUTO_TRIG, ar->wmi.svc_map)) {
4252 /* Only userspace can make an educated decision when to send
4253 * trigger frame. The following effectively disables u-UAPSD
4254 * autotrigger in firmware (which is enabled by default
4255 * provided the autotrigger service is available).
4256 */
4257
4258 arg.wmm_ac = acc;
4259 arg.user_priority = prio;
4260 arg.service_interval = 0;
4261 arg.suspend_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
4262 arg.delay_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
4263
4264 ret = ath10k_wmi_vdev_sta_uapsd(ar, arvif->vdev_id,
4265 arvif->bssid, &arg, 1);
4266 if (ret) {
4267 ath10k_warn(ar, "failed to set uapsd auto trigger %d\n",
4268 ret);
4269 return ret;
4270 }
4271 }
4272
Kalle Valo5e3dd152013-06-12 20:52:10 +03004273exit:
4274 return ret;
4275}
4276
4277static int ath10k_conf_tx(struct ieee80211_hw *hw,
4278 struct ieee80211_vif *vif, u16 ac,
4279 const struct ieee80211_tx_queue_params *params)
4280{
4281 struct ath10k *ar = hw->priv;
Michal Kazior5e752e42015-01-19 09:53:41 +01004282 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004283 struct wmi_wmm_params_arg *p = NULL;
4284 int ret;
4285
4286 mutex_lock(&ar->conf_mutex);
4287
4288 switch (ac) {
4289 case IEEE80211_AC_VO:
Michal Kazior5e752e42015-01-19 09:53:41 +01004290 p = &arvif->wmm_params.ac_vo;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004291 break;
4292 case IEEE80211_AC_VI:
Michal Kazior5e752e42015-01-19 09:53:41 +01004293 p = &arvif->wmm_params.ac_vi;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004294 break;
4295 case IEEE80211_AC_BE:
Michal Kazior5e752e42015-01-19 09:53:41 +01004296 p = &arvif->wmm_params.ac_be;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004297 break;
4298 case IEEE80211_AC_BK:
Michal Kazior5e752e42015-01-19 09:53:41 +01004299 p = &arvif->wmm_params.ac_bk;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004300 break;
4301 }
4302
4303 if (WARN_ON(!p)) {
4304 ret = -EINVAL;
4305 goto exit;
4306 }
4307
4308 p->cwmin = params->cw_min;
4309 p->cwmax = params->cw_max;
4310 p->aifs = params->aifs;
4311
4312 /*
4313 * The channel time duration programmed in the HW is in absolute
4314 * microseconds, while mac80211 gives the txop in units of
4315 * 32 microseconds.
4316 */
4317 p->txop = params->txop * 32;
4318
Michal Kazior7fc979a2015-01-28 09:57:28 +02004319 if (ar->wmi.ops->gen_vdev_wmm_conf) {
4320 ret = ath10k_wmi_vdev_wmm_conf(ar, arvif->vdev_id,
4321 &arvif->wmm_params);
4322 if (ret) {
4323 ath10k_warn(ar, "failed to set vdev wmm params on vdev %i: %d\n",
4324 arvif->vdev_id, ret);
4325 goto exit;
4326 }
4327 } else {
4328 /* This won't work well with multi-interface cases but it's
4329 * better than nothing.
4330 */
4331 ret = ath10k_wmi_pdev_set_wmm_params(ar, &arvif->wmm_params);
4332 if (ret) {
4333 ath10k_warn(ar, "failed to set wmm params: %d\n", ret);
4334 goto exit;
4335 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03004336 }
4337
4338 ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
4339 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004340 ath10k_warn(ar, "failed to set sta uapsd: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004341
4342exit:
4343 mutex_unlock(&ar->conf_mutex);
4344 return ret;
4345}
4346
4347#define ATH10K_ROC_TIMEOUT_HZ (2*HZ)
4348
4349static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
4350 struct ieee80211_vif *vif,
4351 struct ieee80211_channel *chan,
4352 int duration,
4353 enum ieee80211_roc_type type)
4354{
4355 struct ath10k *ar = hw->priv;
4356 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4357 struct wmi_start_scan_arg arg;
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004358 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004359
4360 mutex_lock(&ar->conf_mutex);
4361
4362 spin_lock_bh(&ar->data_lock);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004363 switch (ar->scan.state) {
4364 case ATH10K_SCAN_IDLE:
4365 reinit_completion(&ar->scan.started);
4366 reinit_completion(&ar->scan.completed);
4367 reinit_completion(&ar->scan.on_channel);
4368 ar->scan.state = ATH10K_SCAN_STARTING;
4369 ar->scan.is_roc = true;
4370 ar->scan.vdev_id = arvif->vdev_id;
4371 ar->scan.roc_freq = chan->center_freq;
4372 ret = 0;
4373 break;
4374 case ATH10K_SCAN_STARTING:
4375 case ATH10K_SCAN_RUNNING:
4376 case ATH10K_SCAN_ABORTING:
Kalle Valo5e3dd152013-06-12 20:52:10 +03004377 ret = -EBUSY;
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004378 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004379 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03004380 spin_unlock_bh(&ar->data_lock);
4381
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004382 if (ret)
4383 goto exit;
4384
Michal Kaziordcca0bd2014-11-24 14:58:32 +01004385 duration = max(duration, WMI_SCAN_CHAN_MIN_TIME_MSEC);
4386
Kalle Valo5e3dd152013-06-12 20:52:10 +03004387 memset(&arg, 0, sizeof(arg));
4388 ath10k_wmi_start_scan_init(ar, &arg);
4389 arg.vdev_id = arvif->vdev_id;
4390 arg.scan_id = ATH10K_SCAN_ID;
4391 arg.n_channels = 1;
4392 arg.channels[0] = chan->center_freq;
4393 arg.dwell_time_active = duration;
4394 arg.dwell_time_passive = duration;
4395 arg.max_scan_time = 2 * duration;
4396 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
4397 arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
4398
4399 ret = ath10k_start_scan(ar, &arg);
4400 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004401 ath10k_warn(ar, "failed to start roc scan: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004402 spin_lock_bh(&ar->data_lock);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004403 ar->scan.state = ATH10K_SCAN_IDLE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004404 spin_unlock_bh(&ar->data_lock);
4405 goto exit;
4406 }
4407
4408 ret = wait_for_completion_timeout(&ar->scan.on_channel, 3*HZ);
4409 if (ret == 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004410 ath10k_warn(ar, "failed to switch to channel for roc scan\n");
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004411
4412 ret = ath10k_scan_stop(ar);
4413 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004414 ath10k_warn(ar, "failed to stop scan: %d\n", ret);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004415
Kalle Valo5e3dd152013-06-12 20:52:10 +03004416 ret = -ETIMEDOUT;
4417 goto exit;
4418 }
4419
4420 ret = 0;
4421exit:
4422 mutex_unlock(&ar->conf_mutex);
4423 return ret;
4424}
4425
4426static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw)
4427{
4428 struct ath10k *ar = hw->priv;
4429
4430 mutex_lock(&ar->conf_mutex);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004431 ath10k_scan_abort(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004432 mutex_unlock(&ar->conf_mutex);
4433
Michal Kazior4eb2e162014-10-28 10:23:09 +01004434 cancel_delayed_work_sync(&ar->scan.timeout);
4435
Kalle Valo5e3dd152013-06-12 20:52:10 +03004436 return 0;
4437}
4438
4439/*
4440 * Both RTS and Fragmentation threshold are interface-specific
4441 * in ath10k, but device-specific in mac80211.
4442 */
Kalle Valo5e3dd152013-06-12 20:52:10 +03004443
4444static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
4445{
Kalle Valo5e3dd152013-06-12 20:52:10 +03004446 struct ath10k *ar = hw->priv;
Michal Kaziorad088bf2013-10-16 15:44:46 +03004447 struct ath10k_vif *arvif;
4448 int ret = 0;
Michal Kazior548db542013-07-05 16:15:15 +03004449
Michal Kaziorad088bf2013-10-16 15:44:46 +03004450 mutex_lock(&ar->conf_mutex);
4451 list_for_each_entry(arvif, &ar->arvifs, list) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004452 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
Michal Kaziorad088bf2013-10-16 15:44:46 +03004453 arvif->vdev_id, value);
Kalle Valo60c3daa2013-09-08 17:56:07 +03004454
Michal Kaziorad088bf2013-10-16 15:44:46 +03004455 ret = ath10k_mac_set_rts(arvif, value);
4456 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004457 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
Michal Kaziorad088bf2013-10-16 15:44:46 +03004458 arvif->vdev_id, ret);
4459 break;
4460 }
4461 }
4462 mutex_unlock(&ar->conf_mutex);
4463
4464 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004465}
4466
Emmanuel Grumbach77be2c52014-03-27 11:30:29 +02004467static void ath10k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4468 u32 queues, bool drop)
Kalle Valo5e3dd152013-06-12 20:52:10 +03004469{
4470 struct ath10k *ar = hw->priv;
Michal Kazioraffd3212013-07-16 09:54:35 +02004471 bool skip;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004472 int ret;
4473
4474 /* mac80211 doesn't care if we really xmit queued frames or not
4475 * we'll collect those frames either way if we stop/delete vdevs */
4476 if (drop)
4477 return;
4478
Michal Kazior548db542013-07-05 16:15:15 +03004479 mutex_lock(&ar->conf_mutex);
4480
Michal Kazioraffd3212013-07-16 09:54:35 +02004481 if (ar->state == ATH10K_STATE_WEDGED)
4482 goto skip;
4483
Michal Kazioredb82362013-07-05 16:15:14 +03004484 ret = wait_event_timeout(ar->htt.empty_tx_wq, ({
Kalle Valo5e3dd152013-06-12 20:52:10 +03004485 bool empty;
Michal Kazioraffd3212013-07-16 09:54:35 +02004486
Michal Kazioredb82362013-07-05 16:15:14 +03004487 spin_lock_bh(&ar->htt.tx_lock);
Michal Kazior0945baf2013-09-18 14:43:18 +02004488 empty = (ar->htt.num_pending_tx == 0);
Michal Kazioredb82362013-07-05 16:15:14 +03004489 spin_unlock_bh(&ar->htt.tx_lock);
Michal Kazioraffd3212013-07-16 09:54:35 +02004490
Michal Kazior7962b0d2014-10-28 10:34:38 +01004491 skip = (ar->state == ATH10K_STATE_WEDGED) ||
4492 test_bit(ATH10K_FLAG_CRASH_FLUSH,
4493 &ar->dev_flags);
Michal Kazioraffd3212013-07-16 09:54:35 +02004494
4495 (empty || skip);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004496 }), ATH10K_FLUSH_TIMEOUT_HZ);
Michal Kazioraffd3212013-07-16 09:54:35 +02004497
4498 if (ret <= 0 || skip)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004499 ath10k_warn(ar, "failed to flush transmit queue (skip %i ar-state %i): %i\n",
Ben Greear9ba4c782014-02-25 09:29:57 +02004500 skip, ar->state, ret);
Michal Kazior548db542013-07-05 16:15:15 +03004501
Michal Kazioraffd3212013-07-16 09:54:35 +02004502skip:
Michal Kazior548db542013-07-05 16:15:15 +03004503 mutex_unlock(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004504}
4505
4506/* TODO: Implement this function properly
4507 * For now it is needed to reply to Probe Requests in IBSS mode.
4508 * Propably we need this information from FW.
4509 */
4510static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
4511{
4512 return 1;
4513}
4514
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004515#ifdef CONFIG_PM
4516static int ath10k_suspend(struct ieee80211_hw *hw,
4517 struct cfg80211_wowlan *wowlan)
4518{
4519 struct ath10k *ar = hw->priv;
4520 int ret;
4521
Marek Puzyniak9042e172014-02-10 17:14:23 +01004522 mutex_lock(&ar->conf_mutex);
4523
Marek Puzyniak00f54822014-02-10 17:14:24 +01004524 ret = ath10k_wait_for_suspend(ar, WMI_PDEV_SUSPEND);
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004525 if (ret) {
Marek Puzyniak00f54822014-02-10 17:14:24 +01004526 if (ret == -ETIMEDOUT)
4527 goto resume;
Marek Puzyniak9042e172014-02-10 17:14:23 +01004528 ret = 1;
4529 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004530 }
4531
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004532 ret = ath10k_hif_suspend(ar);
4533 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004534 ath10k_warn(ar, "failed to suspend hif: %d\n", ret);
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004535 goto resume;
4536 }
4537
Marek Puzyniak9042e172014-02-10 17:14:23 +01004538 ret = 0;
4539 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004540resume:
4541 ret = ath10k_wmi_pdev_resume_target(ar);
4542 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004543 ath10k_warn(ar, "failed to resume target: %d\n", ret);
Marek Puzyniak9042e172014-02-10 17:14:23 +01004544
4545 ret = 1;
4546exit:
4547 mutex_unlock(&ar->conf_mutex);
4548 return ret;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004549}
4550
4551static int ath10k_resume(struct ieee80211_hw *hw)
4552{
4553 struct ath10k *ar = hw->priv;
4554 int ret;
4555
Marek Puzyniak9042e172014-02-10 17:14:23 +01004556 mutex_lock(&ar->conf_mutex);
4557
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004558 ret = ath10k_hif_resume(ar);
4559 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004560 ath10k_warn(ar, "failed to resume hif: %d\n", ret);
Marek Puzyniak9042e172014-02-10 17:14:23 +01004561 ret = 1;
4562 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004563 }
4564
4565 ret = ath10k_wmi_pdev_resume_target(ar);
4566 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004567 ath10k_warn(ar, "failed to resume target: %d\n", ret);
Marek Puzyniak9042e172014-02-10 17:14:23 +01004568 ret = 1;
4569 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004570 }
4571
Marek Puzyniak9042e172014-02-10 17:14:23 +01004572 ret = 0;
4573exit:
4574 mutex_unlock(&ar->conf_mutex);
4575 return ret;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004576}
4577#endif
4578
Eliad Pellercf2c92d2014-11-04 11:43:54 +02004579static void ath10k_reconfig_complete(struct ieee80211_hw *hw,
4580 enum ieee80211_reconfig_type reconfig_type)
Michal Kazioraffd3212013-07-16 09:54:35 +02004581{
4582 struct ath10k *ar = hw->priv;
4583
Eliad Pellercf2c92d2014-11-04 11:43:54 +02004584 if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
4585 return;
4586
Michal Kazioraffd3212013-07-16 09:54:35 +02004587 mutex_lock(&ar->conf_mutex);
4588
4589 /* If device failed to restart it will be in a different state, e.g.
4590 * ATH10K_STATE_WEDGED */
4591 if (ar->state == ATH10K_STATE_RESTARTED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004592 ath10k_info(ar, "device successfully recovered\n");
Michal Kazioraffd3212013-07-16 09:54:35 +02004593 ar->state = ATH10K_STATE_ON;
Michal Kazior7962b0d2014-10-28 10:34:38 +01004594 ieee80211_wake_queues(ar->hw);
Michal Kazioraffd3212013-07-16 09:54:35 +02004595 }
4596
4597 mutex_unlock(&ar->conf_mutex);
4598}
4599
Michal Kazior2e1dea42013-07-31 10:32:40 +02004600static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
4601 struct survey_info *survey)
4602{
4603 struct ath10k *ar = hw->priv;
4604 struct ieee80211_supported_band *sband;
4605 struct survey_info *ar_survey = &ar->survey[idx];
4606 int ret = 0;
4607
4608 mutex_lock(&ar->conf_mutex);
4609
4610 sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
4611 if (sband && idx >= sband->n_channels) {
4612 idx -= sband->n_channels;
4613 sband = NULL;
4614 }
4615
4616 if (!sband)
4617 sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
4618
4619 if (!sband || idx >= sband->n_channels) {
4620 ret = -ENOENT;
4621 goto exit;
4622 }
4623
4624 spin_lock_bh(&ar->data_lock);
4625 memcpy(survey, ar_survey, sizeof(*survey));
4626 spin_unlock_bh(&ar->data_lock);
4627
4628 survey->channel = &sband->channels[idx];
4629
Felix Fietkaufa1d4df2014-10-23 17:04:28 +03004630 if (ar->rx_channel == survey->channel)
4631 survey->filled |= SURVEY_INFO_IN_USE;
4632
Michal Kazior2e1dea42013-07-31 10:32:40 +02004633exit:
4634 mutex_unlock(&ar->conf_mutex);
4635 return ret;
4636}
4637
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004638/* Helper table for legacy fixed_rate/bitrate_mask */
4639static const u8 cck_ofdm_rate[] = {
4640 /* CCK */
4641 3, /* 1Mbps */
4642 2, /* 2Mbps */
4643 1, /* 5.5Mbps */
4644 0, /* 11Mbps */
4645 /* OFDM */
4646 3, /* 6Mbps */
4647 7, /* 9Mbps */
4648 2, /* 12Mbps */
4649 6, /* 18Mbps */
4650 1, /* 24Mbps */
4651 5, /* 36Mbps */
4652 0, /* 48Mbps */
4653 4, /* 54Mbps */
4654};
4655
4656/* Check if only one bit set */
4657static int ath10k_check_single_mask(u32 mask)
4658{
4659 int bit;
4660
4661 bit = ffs(mask);
4662 if (!bit)
4663 return 0;
4664
4665 mask &= ~BIT(bit - 1);
4666 if (mask)
4667 return 2;
4668
4669 return 1;
4670}
4671
4672static bool
4673ath10k_default_bitrate_mask(struct ath10k *ar,
4674 enum ieee80211_band band,
4675 const struct cfg80211_bitrate_mask *mask)
4676{
4677 u32 legacy = 0x00ff;
4678 u8 ht = 0xff, i;
4679 u16 vht = 0x3ff;
Ben Greearb116ea12014-11-24 16:22:10 +02004680 u16 nrf = ar->num_rf_chains;
4681
4682 if (ar->cfg_tx_chainmask)
4683 nrf = get_nss_from_chainmask(ar->cfg_tx_chainmask);
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004684
4685 switch (band) {
4686 case IEEE80211_BAND_2GHZ:
4687 legacy = 0x00fff;
4688 vht = 0;
4689 break;
4690 case IEEE80211_BAND_5GHZ:
4691 break;
4692 default:
4693 return false;
4694 }
4695
4696 if (mask->control[band].legacy != legacy)
4697 return false;
4698
Ben Greearb116ea12014-11-24 16:22:10 +02004699 for (i = 0; i < nrf; i++)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004700 if (mask->control[band].ht_mcs[i] != ht)
4701 return false;
4702
Ben Greearb116ea12014-11-24 16:22:10 +02004703 for (i = 0; i < nrf; i++)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004704 if (mask->control[band].vht_mcs[i] != vht)
4705 return false;
4706
4707 return true;
4708}
4709
4710static bool
4711ath10k_bitrate_mask_nss(const struct cfg80211_bitrate_mask *mask,
4712 enum ieee80211_band band,
4713 u8 *fixed_nss)
4714{
4715 int ht_nss = 0, vht_nss = 0, i;
4716
4717 /* check legacy */
4718 if (ath10k_check_single_mask(mask->control[band].legacy))
4719 return false;
4720
4721 /* check HT */
4722 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
4723 if (mask->control[band].ht_mcs[i] == 0xff)
4724 continue;
4725 else if (mask->control[band].ht_mcs[i] == 0x00)
4726 break;
Kalle Valod8bb26b2014-09-14 12:50:33 +03004727
4728 return false;
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004729 }
4730
4731 ht_nss = i;
4732
4733 /* check VHT */
4734 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
4735 if (mask->control[band].vht_mcs[i] == 0x03ff)
4736 continue;
4737 else if (mask->control[band].vht_mcs[i] == 0x0000)
4738 break;
Kalle Valod8bb26b2014-09-14 12:50:33 +03004739
4740 return false;
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004741 }
4742
4743 vht_nss = i;
4744
4745 if (ht_nss > 0 && vht_nss > 0)
4746 return false;
4747
4748 if (ht_nss)
4749 *fixed_nss = ht_nss;
4750 else if (vht_nss)
4751 *fixed_nss = vht_nss;
4752 else
4753 return false;
4754
4755 return true;
4756}
4757
4758static bool
4759ath10k_bitrate_mask_correct(const struct cfg80211_bitrate_mask *mask,
4760 enum ieee80211_band band,
4761 enum wmi_rate_preamble *preamble)
4762{
4763 int legacy = 0, ht = 0, vht = 0, i;
4764
4765 *preamble = WMI_RATE_PREAMBLE_OFDM;
4766
4767 /* check legacy */
4768 legacy = ath10k_check_single_mask(mask->control[band].legacy);
4769 if (legacy > 1)
4770 return false;
4771
4772 /* check HT */
4773 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
4774 ht += ath10k_check_single_mask(mask->control[band].ht_mcs[i]);
4775 if (ht > 1)
4776 return false;
4777
4778 /* check VHT */
4779 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
4780 vht += ath10k_check_single_mask(mask->control[band].vht_mcs[i]);
4781 if (vht > 1)
4782 return false;
4783
4784 /* Currently we support only one fixed_rate */
4785 if ((legacy + ht + vht) != 1)
4786 return false;
4787
4788 if (ht)
4789 *preamble = WMI_RATE_PREAMBLE_HT;
4790 else if (vht)
4791 *preamble = WMI_RATE_PREAMBLE_VHT;
4792
4793 return true;
4794}
4795
4796static bool
Michal Kazior7aa7a722014-08-25 12:09:38 +02004797ath10k_bitrate_mask_rate(struct ath10k *ar,
4798 const struct cfg80211_bitrate_mask *mask,
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004799 enum ieee80211_band band,
4800 u8 *fixed_rate,
4801 u8 *fixed_nss)
4802{
4803 u8 rate = 0, pream = 0, nss = 0, i;
4804 enum wmi_rate_preamble preamble;
4805
4806 /* Check if single rate correct */
4807 if (!ath10k_bitrate_mask_correct(mask, band, &preamble))
4808 return false;
4809
4810 pream = preamble;
4811
4812 switch (preamble) {
4813 case WMI_RATE_PREAMBLE_CCK:
4814 case WMI_RATE_PREAMBLE_OFDM:
4815 i = ffs(mask->control[band].legacy) - 1;
4816
4817 if (band == IEEE80211_BAND_2GHZ && i < 4)
4818 pream = WMI_RATE_PREAMBLE_CCK;
4819
4820 if (band == IEEE80211_BAND_5GHZ)
4821 i += 4;
4822
4823 if (i >= ARRAY_SIZE(cck_ofdm_rate))
4824 return false;
4825
4826 rate = cck_ofdm_rate[i];
4827 break;
4828 case WMI_RATE_PREAMBLE_HT:
4829 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
4830 if (mask->control[band].ht_mcs[i])
4831 break;
4832
4833 if (i == IEEE80211_HT_MCS_MASK_LEN)
4834 return false;
4835
4836 rate = ffs(mask->control[band].ht_mcs[i]) - 1;
4837 nss = i;
4838 break;
4839 case WMI_RATE_PREAMBLE_VHT:
4840 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
4841 if (mask->control[band].vht_mcs[i])
4842 break;
4843
4844 if (i == NL80211_VHT_NSS_MAX)
4845 return false;
4846
4847 rate = ffs(mask->control[band].vht_mcs[i]) - 1;
4848 nss = i;
4849 break;
4850 }
4851
4852 *fixed_nss = nss + 1;
4853 nss <<= 4;
4854 pream <<= 6;
4855
Michal Kazior7aa7a722014-08-25 12:09:38 +02004856 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 +01004857 pream, nss, rate);
4858
4859 *fixed_rate = pream | nss | rate;
4860
4861 return true;
4862}
4863
Michal Kazior7aa7a722014-08-25 12:09:38 +02004864static bool ath10k_get_fixed_rate_nss(struct ath10k *ar,
4865 const struct cfg80211_bitrate_mask *mask,
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004866 enum ieee80211_band band,
4867 u8 *fixed_rate,
4868 u8 *fixed_nss)
4869{
4870 /* First check full NSS mask, if we can simply limit NSS */
4871 if (ath10k_bitrate_mask_nss(mask, band, fixed_nss))
4872 return true;
4873
4874 /* Next Check single rate is set */
Michal Kazior7aa7a722014-08-25 12:09:38 +02004875 return ath10k_bitrate_mask_rate(ar, mask, band, fixed_rate, fixed_nss);
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004876}
4877
4878static int ath10k_set_fixed_rate_param(struct ath10k_vif *arvif,
4879 u8 fixed_rate,
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004880 u8 fixed_nss,
4881 u8 force_sgi)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004882{
4883 struct ath10k *ar = arvif->ar;
4884 u32 vdev_param;
4885 int ret = 0;
4886
4887 mutex_lock(&ar->conf_mutex);
4888
4889 if (arvif->fixed_rate == fixed_rate &&
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004890 arvif->fixed_nss == fixed_nss &&
4891 arvif->force_sgi == force_sgi)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004892 goto exit;
4893
4894 if (fixed_rate == WMI_FIXED_RATE_NONE)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004895 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac disable fixed bitrate mask\n");
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004896
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004897 if (force_sgi)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004898 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac force sgi\n");
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004899
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004900 vdev_param = ar->wmi.vdev_param->fixed_rate;
4901 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
4902 vdev_param, fixed_rate);
4903 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004904 ath10k_warn(ar, "failed to set fixed rate param 0x%02x: %d\n",
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004905 fixed_rate, ret);
4906 ret = -EINVAL;
4907 goto exit;
4908 }
4909
4910 arvif->fixed_rate = fixed_rate;
4911
4912 vdev_param = ar->wmi.vdev_param->nss;
4913 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
4914 vdev_param, fixed_nss);
4915
4916 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004917 ath10k_warn(ar, "failed to set fixed nss param %d: %d\n",
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004918 fixed_nss, ret);
4919 ret = -EINVAL;
4920 goto exit;
4921 }
4922
4923 arvif->fixed_nss = fixed_nss;
4924
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004925 vdev_param = ar->wmi.vdev_param->sgi;
4926 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
4927 force_sgi);
4928
4929 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004930 ath10k_warn(ar, "failed to set sgi param %d: %d\n",
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004931 force_sgi, ret);
4932 ret = -EINVAL;
4933 goto exit;
4934 }
4935
4936 arvif->force_sgi = force_sgi;
4937
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004938exit:
4939 mutex_unlock(&ar->conf_mutex);
4940 return ret;
4941}
4942
4943static int ath10k_set_bitrate_mask(struct ieee80211_hw *hw,
4944 struct ieee80211_vif *vif,
4945 const struct cfg80211_bitrate_mask *mask)
4946{
4947 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4948 struct ath10k *ar = arvif->ar;
4949 enum ieee80211_band band = ar->hw->conf.chandef.chan->band;
4950 u8 fixed_rate = WMI_FIXED_RATE_NONE;
4951 u8 fixed_nss = ar->num_rf_chains;
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004952 u8 force_sgi;
4953
Ben Greearb116ea12014-11-24 16:22:10 +02004954 if (ar->cfg_tx_chainmask)
4955 fixed_nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
4956
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004957 force_sgi = mask->control[band].gi;
4958 if (force_sgi == NL80211_TXRATE_FORCE_LGI)
4959 return -EINVAL;
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004960
4961 if (!ath10k_default_bitrate_mask(ar, band, mask)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004962 if (!ath10k_get_fixed_rate_nss(ar, mask, band,
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004963 &fixed_rate,
4964 &fixed_nss))
4965 return -EINVAL;
4966 }
4967
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004968 if (fixed_rate == WMI_FIXED_RATE_NONE && force_sgi) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004969 ath10k_warn(ar, "failed to force SGI usage for default rate settings\n");
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004970 return -EINVAL;
4971 }
4972
4973 return ath10k_set_fixed_rate_param(arvif, fixed_rate,
4974 fixed_nss, force_sgi);
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004975}
4976
Michal Kazior9797feb2014-02-14 14:49:48 +01004977static void ath10k_sta_rc_update(struct ieee80211_hw *hw,
4978 struct ieee80211_vif *vif,
4979 struct ieee80211_sta *sta,
4980 u32 changed)
4981{
4982 struct ath10k *ar = hw->priv;
4983 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
4984 u32 bw, smps;
4985
4986 spin_lock_bh(&ar->data_lock);
4987
Michal Kazior7aa7a722014-08-25 12:09:38 +02004988 ath10k_dbg(ar, ATH10K_DBG_MAC,
Michal Kazior9797feb2014-02-14 14:49:48 +01004989 "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
4990 sta->addr, changed, sta->bandwidth, sta->rx_nss,
4991 sta->smps_mode);
4992
4993 if (changed & IEEE80211_RC_BW_CHANGED) {
4994 bw = WMI_PEER_CHWIDTH_20MHZ;
4995
4996 switch (sta->bandwidth) {
4997 case IEEE80211_STA_RX_BW_20:
4998 bw = WMI_PEER_CHWIDTH_20MHZ;
4999 break;
5000 case IEEE80211_STA_RX_BW_40:
5001 bw = WMI_PEER_CHWIDTH_40MHZ;
5002 break;
5003 case IEEE80211_STA_RX_BW_80:
5004 bw = WMI_PEER_CHWIDTH_80MHZ;
5005 break;
5006 case IEEE80211_STA_RX_BW_160:
Masanari Iidad939be32015-02-27 23:52:31 +09005007 ath10k_warn(ar, "Invalid bandwidth %d in rc update for %pM\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02005008 sta->bandwidth, sta->addr);
Michal Kazior9797feb2014-02-14 14:49:48 +01005009 bw = WMI_PEER_CHWIDTH_20MHZ;
5010 break;
5011 }
5012
5013 arsta->bw = bw;
5014 }
5015
5016 if (changed & IEEE80211_RC_NSS_CHANGED)
5017 arsta->nss = sta->rx_nss;
5018
5019 if (changed & IEEE80211_RC_SMPS_CHANGED) {
5020 smps = WMI_PEER_SMPS_PS_NONE;
5021
5022 switch (sta->smps_mode) {
5023 case IEEE80211_SMPS_AUTOMATIC:
5024 case IEEE80211_SMPS_OFF:
5025 smps = WMI_PEER_SMPS_PS_NONE;
5026 break;
5027 case IEEE80211_SMPS_STATIC:
5028 smps = WMI_PEER_SMPS_STATIC;
5029 break;
5030 case IEEE80211_SMPS_DYNAMIC:
5031 smps = WMI_PEER_SMPS_DYNAMIC;
5032 break;
5033 case IEEE80211_SMPS_NUM_MODES:
Michal Kazior7aa7a722014-08-25 12:09:38 +02005034 ath10k_warn(ar, "Invalid smps %d in sta rc update for %pM\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02005035 sta->smps_mode, sta->addr);
Michal Kazior9797feb2014-02-14 14:49:48 +01005036 smps = WMI_PEER_SMPS_PS_NONE;
5037 break;
5038 }
5039
5040 arsta->smps = smps;
5041 }
5042
Michal Kazior9797feb2014-02-14 14:49:48 +01005043 arsta->changed |= changed;
5044
5045 spin_unlock_bh(&ar->data_lock);
5046
5047 ieee80211_queue_work(hw, &arsta->update_wk);
5048}
5049
Chun-Yeow Yeoh26ebbcc2014-02-25 09:29:54 +02005050static u64 ath10k_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
5051{
5052 /*
5053 * FIXME: Return 0 for time being. Need to figure out whether FW
5054 * has the API to fetch 64-bit local TSF
5055 */
5056
5057 return 0;
5058}
5059
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02005060static int ath10k_ampdu_action(struct ieee80211_hw *hw,
5061 struct ieee80211_vif *vif,
5062 enum ieee80211_ampdu_mlme_action action,
5063 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
5064 u8 buf_size)
5065{
Michal Kazior7aa7a722014-08-25 12:09:38 +02005066 struct ath10k *ar = hw->priv;
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02005067 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5068
Michal Kazior7aa7a722014-08-25 12:09:38 +02005069 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 +02005070 arvif->vdev_id, sta->addr, tid, action);
5071
5072 switch (action) {
5073 case IEEE80211_AMPDU_RX_START:
5074 case IEEE80211_AMPDU_RX_STOP:
5075 /* HTT AddBa/DelBa events trigger mac80211 Rx BA session
5076 * creation/removal. Do we need to verify this?
5077 */
5078 return 0;
5079 case IEEE80211_AMPDU_TX_START:
5080 case IEEE80211_AMPDU_TX_STOP_CONT:
5081 case IEEE80211_AMPDU_TX_STOP_FLUSH:
5082 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
5083 case IEEE80211_AMPDU_TX_OPERATIONAL:
5084 /* Firmware offloads Tx aggregation entirely so deny mac80211
5085 * Tx aggregation requests.
5086 */
5087 return -EOPNOTSUPP;
5088 }
5089
5090 return -EINVAL;
5091}
5092
Kalle Valo5e3dd152013-06-12 20:52:10 +03005093static const struct ieee80211_ops ath10k_ops = {
5094 .tx = ath10k_tx,
5095 .start = ath10k_start,
5096 .stop = ath10k_stop,
5097 .config = ath10k_config,
5098 .add_interface = ath10k_add_interface,
5099 .remove_interface = ath10k_remove_interface,
5100 .configure_filter = ath10k_configure_filter,
5101 .bss_info_changed = ath10k_bss_info_changed,
5102 .hw_scan = ath10k_hw_scan,
5103 .cancel_hw_scan = ath10k_cancel_hw_scan,
5104 .set_key = ath10k_set_key,
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +02005105 .set_default_unicast_key = ath10k_set_default_unicast_key,
Kalle Valo5e3dd152013-06-12 20:52:10 +03005106 .sta_state = ath10k_sta_state,
5107 .conf_tx = ath10k_conf_tx,
5108 .remain_on_channel = ath10k_remain_on_channel,
5109 .cancel_remain_on_channel = ath10k_cancel_remain_on_channel,
5110 .set_rts_threshold = ath10k_set_rts_threshold,
Kalle Valo5e3dd152013-06-12 20:52:10 +03005111 .flush = ath10k_flush,
5112 .tx_last_beacon = ath10k_tx_last_beacon,
Ben Greear46acf7b2014-05-16 17:15:38 +03005113 .set_antenna = ath10k_set_antenna,
5114 .get_antenna = ath10k_get_antenna,
Eliad Pellercf2c92d2014-11-04 11:43:54 +02005115 .reconfig_complete = ath10k_reconfig_complete,
Michal Kazior2e1dea42013-07-31 10:32:40 +02005116 .get_survey = ath10k_get_survey,
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005117 .set_bitrate_mask = ath10k_set_bitrate_mask,
Michal Kazior9797feb2014-02-14 14:49:48 +01005118 .sta_rc_update = ath10k_sta_rc_update,
Chun-Yeow Yeoh26ebbcc2014-02-25 09:29:54 +02005119 .get_tsf = ath10k_get_tsf,
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02005120 .ampdu_action = ath10k_ampdu_action,
Ben Greear6cddcc72014-09-29 14:41:46 +03005121 .get_et_sset_count = ath10k_debug_get_et_sset_count,
5122 .get_et_stats = ath10k_debug_get_et_stats,
5123 .get_et_strings = ath10k_debug_get_et_strings,
Kalle Valo43d2a302014-09-10 18:23:30 +03005124
5125 CFG80211_TESTMODE_CMD(ath10k_tm_cmd)
5126
Michal Kazior8cd13ca2013-07-16 09:38:54 +02005127#ifdef CONFIG_PM
5128 .suspend = ath10k_suspend,
5129 .resume = ath10k_resume,
5130#endif
Rajkumar Manoharanf5045982015-01-12 14:07:27 +02005131#ifdef CONFIG_MAC80211_DEBUGFS
5132 .sta_add_debugfs = ath10k_sta_add_debugfs,
5133#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03005134};
5135
5136#define RATETAB_ENT(_rate, _rateid, _flags) { \
5137 .bitrate = (_rate), \
5138 .flags = (_flags), \
5139 .hw_value = (_rateid), \
5140}
5141
5142#define CHAN2G(_channel, _freq, _flags) { \
5143 .band = IEEE80211_BAND_2GHZ, \
5144 .hw_value = (_channel), \
5145 .center_freq = (_freq), \
5146 .flags = (_flags), \
5147 .max_antenna_gain = 0, \
5148 .max_power = 30, \
5149}
5150
5151#define CHAN5G(_channel, _freq, _flags) { \
5152 .band = IEEE80211_BAND_5GHZ, \
5153 .hw_value = (_channel), \
5154 .center_freq = (_freq), \
5155 .flags = (_flags), \
5156 .max_antenna_gain = 0, \
5157 .max_power = 30, \
5158}
5159
5160static const struct ieee80211_channel ath10k_2ghz_channels[] = {
5161 CHAN2G(1, 2412, 0),
5162 CHAN2G(2, 2417, 0),
5163 CHAN2G(3, 2422, 0),
5164 CHAN2G(4, 2427, 0),
5165 CHAN2G(5, 2432, 0),
5166 CHAN2G(6, 2437, 0),
5167 CHAN2G(7, 2442, 0),
5168 CHAN2G(8, 2447, 0),
5169 CHAN2G(9, 2452, 0),
5170 CHAN2G(10, 2457, 0),
5171 CHAN2G(11, 2462, 0),
5172 CHAN2G(12, 2467, 0),
5173 CHAN2G(13, 2472, 0),
5174 CHAN2G(14, 2484, 0),
5175};
5176
5177static const struct ieee80211_channel ath10k_5ghz_channels[] = {
Michal Kazior429ff562013-06-26 08:54:54 +02005178 CHAN5G(36, 5180, 0),
5179 CHAN5G(40, 5200, 0),
5180 CHAN5G(44, 5220, 0),
5181 CHAN5G(48, 5240, 0),
5182 CHAN5G(52, 5260, 0),
5183 CHAN5G(56, 5280, 0),
5184 CHAN5G(60, 5300, 0),
5185 CHAN5G(64, 5320, 0),
5186 CHAN5G(100, 5500, 0),
5187 CHAN5G(104, 5520, 0),
5188 CHAN5G(108, 5540, 0),
5189 CHAN5G(112, 5560, 0),
5190 CHAN5G(116, 5580, 0),
5191 CHAN5G(120, 5600, 0),
5192 CHAN5G(124, 5620, 0),
5193 CHAN5G(128, 5640, 0),
5194 CHAN5G(132, 5660, 0),
5195 CHAN5G(136, 5680, 0),
5196 CHAN5G(140, 5700, 0),
5197 CHAN5G(149, 5745, 0),
5198 CHAN5G(153, 5765, 0),
5199 CHAN5G(157, 5785, 0),
5200 CHAN5G(161, 5805, 0),
5201 CHAN5G(165, 5825, 0),
Kalle Valo5e3dd152013-06-12 20:52:10 +03005202};
5203
Michal Kazior91b12082014-12-12 12:41:35 +01005204/* Note: Be careful if you re-order these. There is code which depends on this
5205 * ordering.
5206 */
Kalle Valo5e3dd152013-06-12 20:52:10 +03005207static struct ieee80211_rate ath10k_rates[] = {
5208 /* CCK */
5209 RATETAB_ENT(10, 0x82, 0),
5210 RATETAB_ENT(20, 0x84, 0),
5211 RATETAB_ENT(55, 0x8b, 0),
5212 RATETAB_ENT(110, 0x96, 0),
5213 /* OFDM */
5214 RATETAB_ENT(60, 0x0c, 0),
5215 RATETAB_ENT(90, 0x12, 0),
5216 RATETAB_ENT(120, 0x18, 0),
5217 RATETAB_ENT(180, 0x24, 0),
5218 RATETAB_ENT(240, 0x30, 0),
5219 RATETAB_ENT(360, 0x48, 0),
5220 RATETAB_ENT(480, 0x60, 0),
5221 RATETAB_ENT(540, 0x6c, 0),
5222};
5223
5224#define ath10k_a_rates (ath10k_rates + 4)
5225#define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - 4)
5226#define ath10k_g_rates (ath10k_rates + 0)
5227#define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
5228
Michal Kaziore7b54192014-08-07 11:03:27 +02005229struct ath10k *ath10k_mac_create(size_t priv_size)
Kalle Valo5e3dd152013-06-12 20:52:10 +03005230{
5231 struct ieee80211_hw *hw;
5232 struct ath10k *ar;
5233
Michal Kaziore7b54192014-08-07 11:03:27 +02005234 hw = ieee80211_alloc_hw(sizeof(struct ath10k) + priv_size, &ath10k_ops);
Kalle Valo5e3dd152013-06-12 20:52:10 +03005235 if (!hw)
5236 return NULL;
5237
5238 ar = hw->priv;
5239 ar->hw = hw;
5240
5241 return ar;
5242}
5243
5244void ath10k_mac_destroy(struct ath10k *ar)
5245{
5246 ieee80211_free_hw(ar->hw);
5247}
5248
5249static const struct ieee80211_iface_limit ath10k_if_limits[] = {
5250 {
5251 .max = 8,
5252 .types = BIT(NL80211_IFTYPE_STATION)
5253 | BIT(NL80211_IFTYPE_P2P_CLIENT)
Michal Kaziord531cb82013-07-31 10:55:13 +02005254 },
5255 {
5256 .max = 3,
5257 .types = BIT(NL80211_IFTYPE_P2P_GO)
5258 },
5259 {
Michal Kazior75d2bd42014-12-12 12:41:39 +01005260 .max = 1,
5261 .types = BIT(NL80211_IFTYPE_P2P_DEVICE)
5262 },
5263 {
Michal Kaziord531cb82013-07-31 10:55:13 +02005264 .max = 7,
5265 .types = BIT(NL80211_IFTYPE_AP)
5266 },
Kalle Valo5e3dd152013-06-12 20:52:10 +03005267};
5268
Bartosz Markowskif2595092013-12-10 16:20:39 +01005269static const struct ieee80211_iface_limit ath10k_10x_if_limits[] = {
Marek Puzyniake8a50f82013-11-20 09:59:47 +02005270 {
5271 .max = 8,
5272 .types = BIT(NL80211_IFTYPE_AP)
5273 },
5274};
Marek Puzyniake8a50f82013-11-20 09:59:47 +02005275
5276static const struct ieee80211_iface_combination ath10k_if_comb[] = {
5277 {
5278 .limits = ath10k_if_limits,
5279 .n_limits = ARRAY_SIZE(ath10k_if_limits),
5280 .max_interfaces = 8,
5281 .num_different_channels = 1,
5282 .beacon_int_infra_match = true,
5283 },
Bartosz Markowskif2595092013-12-10 16:20:39 +01005284};
5285
5286static const struct ieee80211_iface_combination ath10k_10x_if_comb[] = {
Marek Puzyniake8a50f82013-11-20 09:59:47 +02005287 {
Bartosz Markowskif2595092013-12-10 16:20:39 +01005288 .limits = ath10k_10x_if_limits,
5289 .n_limits = ARRAY_SIZE(ath10k_10x_if_limits),
Marek Puzyniake8a50f82013-11-20 09:59:47 +02005290 .max_interfaces = 8,
5291 .num_different_channels = 1,
5292 .beacon_int_infra_match = true,
Bartosz Markowskif2595092013-12-10 16:20:39 +01005293#ifdef CONFIG_ATH10K_DFS_CERTIFIED
Marek Puzyniake8a50f82013-11-20 09:59:47 +02005294 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
5295 BIT(NL80211_CHAN_WIDTH_20) |
5296 BIT(NL80211_CHAN_WIDTH_40) |
5297 BIT(NL80211_CHAN_WIDTH_80),
Marek Puzyniake8a50f82013-11-20 09:59:47 +02005298#endif
Bartosz Markowskif2595092013-12-10 16:20:39 +01005299 },
Kalle Valo5e3dd152013-06-12 20:52:10 +03005300};
5301
5302static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
5303{
5304 struct ieee80211_sta_vht_cap vht_cap = {0};
5305 u16 mcs_map;
Michal Kazior8865bee42013-07-24 12:36:46 +02005306 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005307
5308 vht_cap.vht_supported = 1;
5309 vht_cap.cap = ar->vht_cap_info;
5310
Michal Kazior8865bee42013-07-24 12:36:46 +02005311 mcs_map = 0;
5312 for (i = 0; i < 8; i++) {
5313 if (i < ar->num_rf_chains)
5314 mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i*2);
5315 else
5316 mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i*2);
5317 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03005318
5319 vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
5320 vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
5321
5322 return vht_cap;
5323}
5324
5325static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
5326{
5327 int i;
5328 struct ieee80211_sta_ht_cap ht_cap = {0};
5329
5330 if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
5331 return ht_cap;
5332
5333 ht_cap.ht_supported = 1;
5334 ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
5335 ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
5336 ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
5337 ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
5338 ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
5339
5340 if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
5341 ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
5342
5343 if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
5344 ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
5345
5346 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
5347 u32 smps;
5348
5349 smps = WLAN_HT_CAP_SM_PS_DYNAMIC;
5350 smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
5351
5352 ht_cap.cap |= smps;
5353 }
5354
5355 if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC)
5356 ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
5357
5358 if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
5359 u32 stbc;
5360
5361 stbc = ar->ht_cap_info;
5362 stbc &= WMI_HT_CAP_RX_STBC;
5363 stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
5364 stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
5365 stbc &= IEEE80211_HT_CAP_RX_STBC;
5366
5367 ht_cap.cap |= stbc;
5368 }
5369
5370 if (ar->ht_cap_info & WMI_HT_CAP_LDPC)
5371 ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
5372
5373 if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
5374 ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
5375
5376 /* max AMSDU is implicitly taken from vht_cap_info */
5377 if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
5378 ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
5379
Michal Kazior8865bee42013-07-24 12:36:46 +02005380 for (i = 0; i < ar->num_rf_chains; i++)
Kalle Valo5e3dd152013-06-12 20:52:10 +03005381 ht_cap.mcs.rx_mask[i] = 0xFF;
5382
5383 ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
5384
5385 return ht_cap;
5386}
5387
Kalle Valo5e3dd152013-06-12 20:52:10 +03005388static void ath10k_get_arvif_iter(void *data, u8 *mac,
5389 struct ieee80211_vif *vif)
5390{
5391 struct ath10k_vif_iter *arvif_iter = data;
5392 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5393
5394 if (arvif->vdev_id == arvif_iter->vdev_id)
5395 arvif_iter->arvif = arvif;
5396}
5397
5398struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
5399{
5400 struct ath10k_vif_iter arvif_iter;
5401 u32 flags;
5402
5403 memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
5404 arvif_iter.vdev_id = vdev_id;
5405
5406 flags = IEEE80211_IFACE_ITER_RESUME_ALL;
5407 ieee80211_iterate_active_interfaces_atomic(ar->hw,
5408 flags,
5409 ath10k_get_arvif_iter,
5410 &arvif_iter);
5411 if (!arvif_iter.arvif) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02005412 ath10k_warn(ar, "No VIF found for vdev %d\n", vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03005413 return NULL;
5414 }
5415
5416 return arvif_iter.arvif;
5417}
5418
5419int ath10k_mac_register(struct ath10k *ar)
5420{
Johannes Berg3cb10942015-01-22 21:38:45 +01005421 static const u32 cipher_suites[] = {
5422 WLAN_CIPHER_SUITE_WEP40,
5423 WLAN_CIPHER_SUITE_WEP104,
5424 WLAN_CIPHER_SUITE_TKIP,
5425 WLAN_CIPHER_SUITE_CCMP,
5426 WLAN_CIPHER_SUITE_AES_CMAC,
5427 };
Kalle Valo5e3dd152013-06-12 20:52:10 +03005428 struct ieee80211_supported_band *band;
5429 struct ieee80211_sta_vht_cap vht_cap;
5430 struct ieee80211_sta_ht_cap ht_cap;
5431 void *channels;
5432 int ret;
5433
5434 SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
5435
5436 SET_IEEE80211_DEV(ar->hw, ar->dev);
5437
5438 ht_cap = ath10k_get_ht_cap(ar);
5439 vht_cap = ath10k_create_vht_cap(ar);
5440
5441 if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
5442 channels = kmemdup(ath10k_2ghz_channels,
5443 sizeof(ath10k_2ghz_channels),
5444 GFP_KERNEL);
Michal Kaziord6015b22013-07-22 14:13:30 +02005445 if (!channels) {
5446 ret = -ENOMEM;
5447 goto err_free;
5448 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03005449
5450 band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
5451 band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
5452 band->channels = channels;
5453 band->n_bitrates = ath10k_g_rates_size;
5454 band->bitrates = ath10k_g_rates;
5455 band->ht_cap = ht_cap;
5456
Yanbo Lid68bb122015-01-23 08:18:20 +08005457 /* Enable the VHT support at 2.4 GHz */
5458 band->vht_cap = vht_cap;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005459
5460 ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
5461 }
5462
5463 if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
5464 channels = kmemdup(ath10k_5ghz_channels,
5465 sizeof(ath10k_5ghz_channels),
5466 GFP_KERNEL);
5467 if (!channels) {
Michal Kaziord6015b22013-07-22 14:13:30 +02005468 ret = -ENOMEM;
5469 goto err_free;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005470 }
5471
5472 band = &ar->mac.sbands[IEEE80211_BAND_5GHZ];
5473 band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
5474 band->channels = channels;
5475 band->n_bitrates = ath10k_a_rates_size;
5476 band->bitrates = ath10k_a_rates;
5477 band->ht_cap = ht_cap;
5478 band->vht_cap = vht_cap;
5479 ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
5480 }
5481
5482 ar->hw->wiphy->interface_modes =
5483 BIT(NL80211_IFTYPE_STATION) |
Bartosz Markowskid3541812013-12-10 16:20:40 +01005484 BIT(NL80211_IFTYPE_AP);
5485
Ben Greear46acf7b2014-05-16 17:15:38 +03005486 ar->hw->wiphy->available_antennas_rx = ar->supp_rx_chainmask;
5487 ar->hw->wiphy->available_antennas_tx = ar->supp_tx_chainmask;
5488
Bartosz Markowskid3541812013-12-10 16:20:40 +01005489 if (!test_bit(ATH10K_FW_FEATURE_NO_P2P, ar->fw_features))
5490 ar->hw->wiphy->interface_modes |=
Michal Kazior75d2bd42014-12-12 12:41:39 +01005491 BIT(NL80211_IFTYPE_P2P_DEVICE) |
Bartosz Markowskid3541812013-12-10 16:20:40 +01005492 BIT(NL80211_IFTYPE_P2P_CLIENT) |
5493 BIT(NL80211_IFTYPE_P2P_GO);
Kalle Valo5e3dd152013-06-12 20:52:10 +03005494
5495 ar->hw->flags = IEEE80211_HW_SIGNAL_DBM |
5496 IEEE80211_HW_SUPPORTS_PS |
5497 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
Kalle Valo5e3dd152013-06-12 20:52:10 +03005498 IEEE80211_HW_MFP_CAPABLE |
5499 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
5500 IEEE80211_HW_HAS_RATE_CONTROL |
Janusz Dziedzic2f0f1122014-02-26 18:42:09 +02005501 IEEE80211_HW_AP_LINK_PS |
Johannes Berg3cb10942015-01-22 21:38:45 +01005502 IEEE80211_HW_SPECTRUM_MGMT |
Johannes Berg41fbf6e2015-04-10 14:18:40 +02005503 IEEE80211_HW_SW_CRYPTO_CONTROL |
5504 IEEE80211_HW_SUPPORT_FAST_XMIT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005505
Eliad Peller0d8614b2014-09-10 14:07:36 +03005506 ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
5507
Kalle Valo5e3dd152013-06-12 20:52:10 +03005508 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
Eliad Peller0d8614b2014-09-10 14:07:36 +03005509 ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005510
5511 if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
5512 ar->hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
5513 ar->hw->flags |= IEEE80211_HW_TX_AMPDU_SETUP_IN_HW;
5514 }
5515
5516 ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
5517 ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
5518
5519 ar->hw->vif_data_size = sizeof(struct ath10k_vif);
Michal Kazior9797feb2014-02-14 14:49:48 +01005520 ar->hw->sta_data_size = sizeof(struct ath10k_sta);
Kalle Valo5e3dd152013-06-12 20:52:10 +03005521
Kalle Valo5e3dd152013-06-12 20:52:10 +03005522 ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
5523
Michal Kaziorfbb8f1b2015-01-13 16:30:12 +02005524 if (test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)) {
5525 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
5526
5527 /* Firmware delivers WPS/P2P Probe Requests frames to driver so
5528 * that userspace (e.g. wpa_supplicant/hostapd) can generate
5529 * correct Probe Responses. This is more of a hack advert..
5530 */
5531 ar->hw->wiphy->probe_resp_offload |=
5532 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
5533 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
5534 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
5535 }
5536
Kalle Valo5e3dd152013-06-12 20:52:10 +03005537 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
Michal Kaziorc2df44b2014-01-23 11:38:26 +01005538 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005539 ar->hw->wiphy->max_remain_on_channel_duration = 5000;
5540
5541 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
Rajkumar Manoharan78157a12014-11-17 16:44:15 +02005542 ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE;
5543
Kalle Valo5e3dd152013-06-12 20:52:10 +03005544 /*
5545 * on LL hardware queues are managed entirely by the FW
5546 * so we only advertise to mac we can do the queues thing
5547 */
5548 ar->hw->queues = 4;
5549
Kalle Valo5cc7caf2014-12-17 12:20:54 +02005550 switch (ar->wmi.op_version) {
5551 case ATH10K_FW_WMI_OP_VERSION_MAIN:
5552 case ATH10K_FW_WMI_OP_VERSION_TLV:
Bartosz Markowskif2595092013-12-10 16:20:39 +01005553 ar->hw->wiphy->iface_combinations = ath10k_if_comb;
5554 ar->hw->wiphy->n_iface_combinations =
5555 ARRAY_SIZE(ath10k_if_comb);
Michal Kaziorcf850d12014-07-24 20:07:00 +03005556 ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
Kalle Valo5cc7caf2014-12-17 12:20:54 +02005557 break;
5558 case ATH10K_FW_WMI_OP_VERSION_10_1:
5559 case ATH10K_FW_WMI_OP_VERSION_10_2:
Rajkumar Manoharan4a16fbe2014-12-17 12:21:12 +02005560 case ATH10K_FW_WMI_OP_VERSION_10_2_4:
Kalle Valo5cc7caf2014-12-17 12:20:54 +02005561 ar->hw->wiphy->iface_combinations = ath10k_10x_if_comb;
5562 ar->hw->wiphy->n_iface_combinations =
5563 ARRAY_SIZE(ath10k_10x_if_comb);
5564 break;
5565 case ATH10K_FW_WMI_OP_VERSION_UNSET:
5566 case ATH10K_FW_WMI_OP_VERSION_MAX:
5567 WARN_ON(1);
5568 ret = -EINVAL;
5569 goto err_free;
Bartosz Markowskif2595092013-12-10 16:20:39 +01005570 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03005571
Michal Kazior7c199992013-07-31 10:47:57 +02005572 ar->hw->netdev_features = NETIF_F_HW_CSUM;
5573
Janusz Dziedzic9702c682013-11-20 09:59:41 +02005574 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED)) {
5575 /* Init ath dfs pattern detector */
5576 ar->ath_common.debug_mask = ATH_DBG_DFS;
5577 ar->dfs_detector = dfs_pattern_detector_init(&ar->ath_common,
5578 NL80211_DFS_UNSET);
5579
5580 if (!ar->dfs_detector)
Michal Kazior7aa7a722014-08-25 12:09:38 +02005581 ath10k_warn(ar, "failed to initialise DFS pattern detector\n");
Janusz Dziedzic9702c682013-11-20 09:59:41 +02005582 }
5583
Kalle Valo5e3dd152013-06-12 20:52:10 +03005584 ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
5585 ath10k_reg_notifier);
5586 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02005587 ath10k_err(ar, "failed to initialise regulatory: %i\n", ret);
Michal Kaziord6015b22013-07-22 14:13:30 +02005588 goto err_free;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005589 }
5590
Johannes Berg3cb10942015-01-22 21:38:45 +01005591 ar->hw->wiphy->cipher_suites = cipher_suites;
5592 ar->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
5593
Kalle Valo5e3dd152013-06-12 20:52:10 +03005594 ret = ieee80211_register_hw(ar->hw);
5595 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02005596 ath10k_err(ar, "failed to register ieee80211: %d\n", ret);
Michal Kaziord6015b22013-07-22 14:13:30 +02005597 goto err_free;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005598 }
5599
5600 if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
5601 ret = regulatory_hint(ar->hw->wiphy,
5602 ar->ath_common.regulatory.alpha2);
5603 if (ret)
Michal Kaziord6015b22013-07-22 14:13:30 +02005604 goto err_unregister;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005605 }
5606
5607 return 0;
Michal Kaziord6015b22013-07-22 14:13:30 +02005608
5609err_unregister:
Kalle Valo5e3dd152013-06-12 20:52:10 +03005610 ieee80211_unregister_hw(ar->hw);
Michal Kaziord6015b22013-07-22 14:13:30 +02005611err_free:
5612 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
5613 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
5614
Kalle Valo5e3dd152013-06-12 20:52:10 +03005615 return ret;
5616}
5617
5618void ath10k_mac_unregister(struct ath10k *ar)
5619{
5620 ieee80211_unregister_hw(ar->hw);
5621
Janusz Dziedzic9702c682013-11-20 09:59:41 +02005622 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
5623 ar->dfs_detector->exit(ar->dfs_detector);
5624
Kalle Valo5e3dd152013-06-12 20:52:10 +03005625 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
5626 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
5627
5628 SET_IEEE80211_DEV(ar->hw, NULL);
5629}