blob: fcd08b2f8d26ac4bcb634668a64d8cc500c829ec [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 ||
Michal Kazior19337472014-08-28 12:58:16 +0200776 test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
777
778 ath10k_dbg(ar, ATH10K_DBG_MAC,
779 "mac monitor recalc started? %d should? %d\n",
780 ar->monitor_started, should_start);
781
782 if (should_start == ar->monitor_started)
783 return 0;
784
785 if (should_start)
786 return ath10k_monitor_start(ar);
Kalle Valod8bb26b2014-09-14 12:50:33 +0300787
788 return ath10k_monitor_stop(ar);
Michal Kazior1bbc0972014-04-08 09:45:47 +0300789}
790
Marek Kwaczynskie81bd102014-03-11 12:58:00 +0200791static int ath10k_recalc_rtscts_prot(struct ath10k_vif *arvif)
792{
793 struct ath10k *ar = arvif->ar;
794 u32 vdev_param, rts_cts = 0;
795
796 lockdep_assert_held(&ar->conf_mutex);
797
798 vdev_param = ar->wmi.vdev_param->enable_rtscts;
799
800 if (arvif->use_cts_prot || arvif->num_legacy_stations > 0)
801 rts_cts |= SM(WMI_RTSCTS_ENABLED, WMI_RTSCTS_SET);
802
803 if (arvif->num_legacy_stations > 0)
804 rts_cts |= SM(WMI_RTSCTS_ACROSS_SW_RETRIES,
805 WMI_RTSCTS_PROFILE);
806
807 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
808 rts_cts);
809}
810
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200811static int ath10k_start_cac(struct ath10k *ar)
812{
813 int ret;
814
815 lockdep_assert_held(&ar->conf_mutex);
816
817 set_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
818
Michal Kazior19337472014-08-28 12:58:16 +0200819 ret = ath10k_monitor_recalc(ar);
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200820 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200821 ath10k_warn(ar, "failed to start monitor (cac): %d\n", ret);
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200822 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
823 return ret;
824 }
825
Michal Kazior7aa7a722014-08-25 12:09:38 +0200826 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac start monitor vdev %d\n",
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200827 ar->monitor_vdev_id);
828
829 return 0;
830}
831
832static int ath10k_stop_cac(struct ath10k *ar)
833{
834 lockdep_assert_held(&ar->conf_mutex);
835
836 /* CAC is not running - do nothing */
837 if (!test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags))
838 return 0;
839
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200840 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
Michal Kazior1bbc0972014-04-08 09:45:47 +0300841 ath10k_monitor_stop(ar);
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200842
Michal Kazior7aa7a722014-08-25 12:09:38 +0200843 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac finished\n");
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200844
845 return 0;
846}
847
Michal Kaziord6500972014-04-08 09:56:09 +0300848static void ath10k_recalc_radar_detection(struct ath10k *ar)
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200849{
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200850 int ret;
851
852 lockdep_assert_held(&ar->conf_mutex);
853
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200854 ath10k_stop_cac(ar);
855
Michal Kaziord6500972014-04-08 09:56:09 +0300856 if (!ar->radar_enabled)
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200857 return;
858
Michal Kaziord6500972014-04-08 09:56:09 +0300859 if (ar->num_started_vdevs > 0)
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200860 return;
861
862 ret = ath10k_start_cac(ar);
863 if (ret) {
864 /*
865 * Not possible to start CAC on current channel so starting
866 * radiation is not allowed, make this channel DFS_UNAVAILABLE
867 * by indicating that radar was detected.
868 */
Michal Kazior7aa7a722014-08-25 12:09:38 +0200869 ath10k_warn(ar, "failed to start CAC: %d\n", ret);
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200870 ieee80211_radar_detected(ar->hw);
871 }
872}
873
Michal Kaziordc55e302014-07-29 12:53:36 +0300874static int ath10k_vdev_start_restart(struct ath10k_vif *arvif, bool restart)
Michal Kazior72654fa2014-04-08 09:56:09 +0300875{
876 struct ath10k *ar = arvif->ar;
877 struct cfg80211_chan_def *chandef = &ar->chandef;
878 struct wmi_vdev_start_request_arg arg = {};
879 int ret = 0;
880
881 lockdep_assert_held(&ar->conf_mutex);
882
883 reinit_completion(&ar->vdev_setup_done);
884
885 arg.vdev_id = arvif->vdev_id;
886 arg.dtim_period = arvif->dtim_period;
887 arg.bcn_intval = arvif->beacon_interval;
888
889 arg.channel.freq = chandef->chan->center_freq;
890 arg.channel.band_center_freq1 = chandef->center_freq1;
891 arg.channel.mode = chan_to_phymode(chandef);
892
893 arg.channel.min_power = 0;
894 arg.channel.max_power = chandef->chan->max_power * 2;
895 arg.channel.max_reg_power = chandef->chan->max_reg_power * 2;
896 arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
897
898 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
899 arg.ssid = arvif->u.ap.ssid;
900 arg.ssid_len = arvif->u.ap.ssid_len;
901 arg.hidden_ssid = arvif->u.ap.hidden_ssid;
902
903 /* For now allow DFS for AP mode */
904 arg.channel.chan_radar =
905 !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
906 } else if (arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
907 arg.ssid = arvif->vif->bss_conf.ssid;
908 arg.ssid_len = arvif->vif->bss_conf.ssid_len;
909 }
910
Michal Kazior7aa7a722014-08-25 12:09:38 +0200911 ath10k_dbg(ar, ATH10K_DBG_MAC,
Michal Kazior72654fa2014-04-08 09:56:09 +0300912 "mac vdev %d start center_freq %d phymode %s\n",
913 arg.vdev_id, arg.channel.freq,
914 ath10k_wmi_phymode_str(arg.channel.mode));
915
Michal Kaziordc55e302014-07-29 12:53:36 +0300916 if (restart)
917 ret = ath10k_wmi_vdev_restart(ar, &arg);
918 else
919 ret = ath10k_wmi_vdev_start(ar, &arg);
920
Michal Kazior72654fa2014-04-08 09:56:09 +0300921 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200922 ath10k_warn(ar, "failed to start WMI vdev %i: %d\n",
Michal Kazior72654fa2014-04-08 09:56:09 +0300923 arg.vdev_id, ret);
924 return ret;
925 }
926
927 ret = ath10k_vdev_setup_sync(ar);
928 if (ret) {
Ben Greear60028a82015-02-15 16:50:39 +0200929 ath10k_warn(ar,
930 "failed to synchronize setup for vdev %i restart %d: %d\n",
931 arg.vdev_id, restart, ret);
Michal Kazior72654fa2014-04-08 09:56:09 +0300932 return ret;
933 }
934
Michal Kaziord6500972014-04-08 09:56:09 +0300935 ar->num_started_vdevs++;
936 ath10k_recalc_radar_detection(ar);
937
Michal Kazior72654fa2014-04-08 09:56:09 +0300938 return ret;
939}
940
Michal Kaziordc55e302014-07-29 12:53:36 +0300941static int ath10k_vdev_start(struct ath10k_vif *arvif)
942{
943 return ath10k_vdev_start_restart(arvif, false);
944}
945
946static int ath10k_vdev_restart(struct ath10k_vif *arvif)
947{
948 return ath10k_vdev_start_restart(arvif, true);
949}
950
Michal Kazior72654fa2014-04-08 09:56:09 +0300951static int ath10k_vdev_stop(struct ath10k_vif *arvif)
952{
953 struct ath10k *ar = arvif->ar;
954 int ret;
955
956 lockdep_assert_held(&ar->conf_mutex);
957
958 reinit_completion(&ar->vdev_setup_done);
959
960 ret = ath10k_wmi_vdev_stop(ar, arvif->vdev_id);
961 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200962 ath10k_warn(ar, "failed to stop WMI vdev %i: %d\n",
Michal Kazior72654fa2014-04-08 09:56:09 +0300963 arvif->vdev_id, ret);
964 return ret;
965 }
966
967 ret = ath10k_vdev_setup_sync(ar);
968 if (ret) {
Ben Greear60028a82015-02-15 16:50:39 +0200969 ath10k_warn(ar, "failed to synchronize setup for vdev %i stop: %d\n",
Michal Kazior72654fa2014-04-08 09:56:09 +0300970 arvif->vdev_id, ret);
971 return ret;
972 }
973
Michal Kaziord6500972014-04-08 09:56:09 +0300974 WARN_ON(ar->num_started_vdevs == 0);
975
976 if (ar->num_started_vdevs != 0) {
977 ar->num_started_vdevs--;
978 ath10k_recalc_radar_detection(ar);
979 }
980
Michal Kazior72654fa2014-04-08 09:56:09 +0300981 return ret;
982}
983
Michal Kaziorfbb8f1b2015-01-13 16:30:12 +0200984static int ath10k_mac_setup_bcn_p2p_ie(struct ath10k_vif *arvif,
985 struct sk_buff *bcn)
986{
987 struct ath10k *ar = arvif->ar;
988 struct ieee80211_mgmt *mgmt;
989 const u8 *p2p_ie;
990 int ret;
991
992 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
993 return 0;
994
995 if (arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
996 return 0;
997
998 mgmt = (void *)bcn->data;
999 p2p_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1000 mgmt->u.beacon.variable,
1001 bcn->len - (mgmt->u.beacon.variable -
1002 bcn->data));
1003 if (!p2p_ie)
1004 return -ENOENT;
1005
1006 ret = ath10k_wmi_p2p_go_bcn_ie(ar, arvif->vdev_id, p2p_ie);
1007 if (ret) {
1008 ath10k_warn(ar, "failed to submit p2p go bcn ie for vdev %i: %d\n",
1009 arvif->vdev_id, ret);
1010 return ret;
1011 }
1012
1013 return 0;
1014}
1015
1016static int ath10k_mac_remove_vendor_ie(struct sk_buff *skb, unsigned int oui,
1017 u8 oui_type, size_t ie_offset)
1018{
1019 size_t len;
1020 const u8 *next;
1021 const u8 *end;
1022 u8 *ie;
1023
1024 if (WARN_ON(skb->len < ie_offset))
1025 return -EINVAL;
1026
1027 ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
1028 skb->data + ie_offset,
1029 skb->len - ie_offset);
1030 if (!ie)
1031 return -ENOENT;
1032
1033 len = ie[1] + 2;
1034 end = skb->data + skb->len;
1035 next = ie + len;
1036
1037 if (WARN_ON(next > end))
1038 return -EINVAL;
1039
1040 memmove(ie, next, end - next);
1041 skb_trim(skb, skb->len - len);
1042
1043 return 0;
1044}
1045
1046static int ath10k_mac_setup_bcn_tmpl(struct ath10k_vif *arvif)
1047{
1048 struct ath10k *ar = arvif->ar;
1049 struct ieee80211_hw *hw = ar->hw;
1050 struct ieee80211_vif *vif = arvif->vif;
1051 struct ieee80211_mutable_offsets offs = {};
1052 struct sk_buff *bcn;
1053 int ret;
1054
1055 if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1056 return 0;
1057
1058 bcn = ieee80211_beacon_get_template(hw, vif, &offs);
1059 if (!bcn) {
1060 ath10k_warn(ar, "failed to get beacon template from mac80211\n");
1061 return -EPERM;
1062 }
1063
1064 ret = ath10k_mac_setup_bcn_p2p_ie(arvif, bcn);
1065 if (ret) {
1066 ath10k_warn(ar, "failed to setup p2p go bcn ie: %d\n", ret);
1067 kfree_skb(bcn);
1068 return ret;
1069 }
1070
1071 /* P2P IE is inserted by firmware automatically (as configured above)
1072 * so remove it from the base beacon template to avoid duplicate P2P
1073 * IEs in beacon frames.
1074 */
1075 ath10k_mac_remove_vendor_ie(bcn, WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1076 offsetof(struct ieee80211_mgmt,
1077 u.beacon.variable));
1078
1079 ret = ath10k_wmi_bcn_tmpl(ar, arvif->vdev_id, offs.tim_offset, bcn, 0,
1080 0, NULL, 0);
1081 kfree_skb(bcn);
1082
1083 if (ret) {
1084 ath10k_warn(ar, "failed to submit beacon template command: %d\n",
1085 ret);
1086 return ret;
1087 }
1088
1089 return 0;
1090}
1091
1092static int ath10k_mac_setup_prb_tmpl(struct ath10k_vif *arvif)
1093{
1094 struct ath10k *ar = arvif->ar;
1095 struct ieee80211_hw *hw = ar->hw;
1096 struct ieee80211_vif *vif = arvif->vif;
1097 struct sk_buff *prb;
1098 int ret;
1099
1100 if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1101 return 0;
1102
1103 prb = ieee80211_proberesp_get(hw, vif);
1104 if (!prb) {
1105 ath10k_warn(ar, "failed to get probe resp template from mac80211\n");
1106 return -EPERM;
1107 }
1108
1109 ret = ath10k_wmi_prb_tmpl(ar, arvif->vdev_id, prb);
1110 kfree_skb(prb);
1111
1112 if (ret) {
1113 ath10k_warn(ar, "failed to submit probe resp template command: %d\n",
1114 ret);
1115 return ret;
1116 }
1117
1118 return 0;
1119}
1120
Kalle Valo5e3dd152013-06-12 20:52:10 +03001121static void ath10k_control_beaconing(struct ath10k_vif *arvif,
Kalle Valo5b07e072014-09-14 12:50:06 +03001122 struct ieee80211_bss_conf *info)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001123{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001124 struct ath10k *ar = arvif->ar;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001125 int ret = 0;
1126
Michal Kazior548db542013-07-05 16:15:15 +03001127 lockdep_assert_held(&arvif->ar->conf_mutex);
1128
Kalle Valo5e3dd152013-06-12 20:52:10 +03001129 if (!info->enable_beacon) {
1130 ath10k_vdev_stop(arvif);
Michal Kaziorc930f742014-01-23 11:38:25 +01001131
1132 arvif->is_started = false;
1133 arvif->is_up = false;
1134
Michal Kazior748afc42014-01-23 12:48:21 +01001135 spin_lock_bh(&arvif->ar->data_lock);
Michal Kazior64badcb2014-09-18 11:18:02 +03001136 ath10k_mac_vif_beacon_free(arvif);
Michal Kazior748afc42014-01-23 12:48:21 +01001137 spin_unlock_bh(&arvif->ar->data_lock);
1138
Kalle Valo5e3dd152013-06-12 20:52:10 +03001139 return;
1140 }
1141
1142 arvif->tx_seq_no = 0x1000;
1143
1144 ret = ath10k_vdev_start(arvif);
1145 if (ret)
1146 return;
1147
Michal Kaziorc930f742014-01-23 11:38:25 +01001148 arvif->aid = 0;
Kalle Valob25f32c2014-09-14 12:50:49 +03001149 ether_addr_copy(arvif->bssid, info->bssid);
Michal Kaziorc930f742014-01-23 11:38:25 +01001150
1151 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
1152 arvif->bssid);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001153 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001154 ath10k_warn(ar, "failed to bring up vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02001155 arvif->vdev_id, ret);
Michal Kaziorc930f742014-01-23 11:38:25 +01001156 ath10k_vdev_stop(arvif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001157 return;
1158 }
Michal Kaziorc930f742014-01-23 11:38:25 +01001159
1160 arvif->is_started = true;
1161 arvif->is_up = true;
1162
Michal Kazior7aa7a722014-08-25 12:09:38 +02001163 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001164}
1165
1166static void ath10k_control_ibss(struct ath10k_vif *arvif,
1167 struct ieee80211_bss_conf *info,
1168 const u8 self_peer[ETH_ALEN])
1169{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001170 struct ath10k *ar = arvif->ar;
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02001171 u32 vdev_param;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001172 int ret = 0;
1173
Michal Kazior548db542013-07-05 16:15:15 +03001174 lockdep_assert_held(&arvif->ar->conf_mutex);
1175
Kalle Valo5e3dd152013-06-12 20:52:10 +03001176 if (!info->ibss_joined) {
1177 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, self_peer);
1178 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001179 ath10k_warn(ar, "failed to delete IBSS self peer %pM for vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001180 self_peer, arvif->vdev_id, ret);
1181
Michal Kaziorc930f742014-01-23 11:38:25 +01001182 if (is_zero_ether_addr(arvif->bssid))
Kalle Valo5e3dd152013-06-12 20:52:10 +03001183 return;
1184
Joe Perches93803b32015-03-02 19:54:49 -08001185 eth_zero_addr(arvif->bssid);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001186
1187 return;
1188 }
1189
1190 ret = ath10k_peer_create(arvif->ar, arvif->vdev_id, self_peer);
1191 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001192 ath10k_warn(ar, "failed to create IBSS self peer %pM for vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001193 self_peer, arvif->vdev_id, ret);
1194 return;
1195 }
1196
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02001197 vdev_param = arvif->ar->wmi.vdev_param->atim_window;
1198 ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001199 ATH10K_DEFAULT_ATIM);
1200 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001201 ath10k_warn(ar, "failed to set IBSS ATIM for vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001202 arvif->vdev_id, ret);
1203}
1204
Michal Kazior9f9b5742014-12-12 12:41:36 +01001205static int ath10k_mac_vif_recalc_ps_wake_threshold(struct ath10k_vif *arvif)
1206{
1207 struct ath10k *ar = arvif->ar;
1208 u32 param;
1209 u32 value;
1210 int ret;
1211
1212 lockdep_assert_held(&arvif->ar->conf_mutex);
1213
1214 if (arvif->u.sta.uapsd)
1215 value = WMI_STA_PS_TX_WAKE_THRESHOLD_NEVER;
1216 else
1217 value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
1218
1219 param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
1220 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param, value);
1221 if (ret) {
1222 ath10k_warn(ar, "failed to submit ps wake threshold %u on vdev %i: %d\n",
1223 value, arvif->vdev_id, ret);
1224 return ret;
1225 }
1226
1227 return 0;
1228}
1229
1230static int ath10k_mac_vif_recalc_ps_poll_count(struct ath10k_vif *arvif)
1231{
1232 struct ath10k *ar = arvif->ar;
1233 u32 param;
1234 u32 value;
1235 int ret;
1236
1237 lockdep_assert_held(&arvif->ar->conf_mutex);
1238
1239 if (arvif->u.sta.uapsd)
1240 value = WMI_STA_PS_PSPOLL_COUNT_UAPSD;
1241 else
1242 value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
1243
1244 param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
1245 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
1246 param, value);
1247 if (ret) {
1248 ath10k_warn(ar, "failed to submit ps poll count %u on vdev %i: %d\n",
1249 value, arvif->vdev_id, ret);
1250 return ret;
1251 }
1252
1253 return 0;
1254}
1255
Michal Kaziorcffb41f2015-02-13 13:30:16 +01001256static int ath10k_mac_ps_vif_count(struct ath10k *ar)
1257{
1258 struct ath10k_vif *arvif;
1259 int num = 0;
1260
1261 lockdep_assert_held(&ar->conf_mutex);
1262
1263 list_for_each_entry(arvif, &ar->arvifs, list)
1264 if (arvif->ps)
1265 num++;
1266
1267 return num;
1268}
1269
Michal Kaziorad088bf2013-10-16 15:44:46 +03001270static int ath10k_mac_vif_setup_ps(struct ath10k_vif *arvif)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001271{
Michal Kaziorad088bf2013-10-16 15:44:46 +03001272 struct ath10k *ar = arvif->ar;
Michal Kazior526549a2014-12-12 12:41:37 +01001273 struct ieee80211_vif *vif = arvif->vif;
Michal Kaziorad088bf2013-10-16 15:44:46 +03001274 struct ieee80211_conf *conf = &ar->hw->conf;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001275 enum wmi_sta_powersave_param param;
1276 enum wmi_sta_ps_mode psmode;
1277 int ret;
Michal Kazior526549a2014-12-12 12:41:37 +01001278 int ps_timeout;
Michal Kaziorcffb41f2015-02-13 13:30:16 +01001279 bool enable_ps;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001280
Michal Kazior548db542013-07-05 16:15:15 +03001281 lockdep_assert_held(&arvif->ar->conf_mutex);
1282
Michal Kaziorad088bf2013-10-16 15:44:46 +03001283 if (arvif->vif->type != NL80211_IFTYPE_STATION)
1284 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001285
Michal Kaziorcffb41f2015-02-13 13:30:16 +01001286 enable_ps = arvif->ps;
1287
1288 if (enable_ps && ath10k_mac_ps_vif_count(ar) > 1 &&
1289 !test_bit(ATH10K_FW_FEATURE_MULTI_VIF_PS_SUPPORT,
1290 ar->fw_features)) {
1291 ath10k_warn(ar, "refusing to enable ps on vdev %i: not supported by fw\n",
1292 arvif->vdev_id);
1293 enable_ps = false;
1294 }
1295
1296 if (enable_ps) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001297 psmode = WMI_STA_PS_MODE_ENABLED;
1298 param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
1299
Michal Kazior526549a2014-12-12 12:41:37 +01001300 ps_timeout = conf->dynamic_ps_timeout;
1301 if (ps_timeout == 0) {
1302 /* Firmware doesn't like 0 */
1303 ps_timeout = ieee80211_tu_to_usec(
1304 vif->bss_conf.beacon_int) / 1000;
1305 }
1306
Michal Kaziorad088bf2013-10-16 15:44:46 +03001307 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
Michal Kazior526549a2014-12-12 12:41:37 +01001308 ps_timeout);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001309 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001310 ath10k_warn(ar, "failed to set inactivity time for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02001311 arvif->vdev_id, ret);
Michal Kaziorad088bf2013-10-16 15:44:46 +03001312 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001313 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001314 } else {
1315 psmode = WMI_STA_PS_MODE_DISABLED;
1316 }
1317
Michal Kazior7aa7a722014-08-25 12:09:38 +02001318 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d psmode %s\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03001319 arvif->vdev_id, psmode ? "enable" : "disable");
1320
Michal Kaziorad088bf2013-10-16 15:44:46 +03001321 ret = ath10k_wmi_set_psmode(ar, arvif->vdev_id, psmode);
1322 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001323 ath10k_warn(ar, "failed to set PS Mode %d for vdev %d: %d\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02001324 psmode, arvif->vdev_id, ret);
Michal Kaziorad088bf2013-10-16 15:44:46 +03001325 return ret;
1326 }
1327
1328 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001329}
1330
Michal Kazior46725b152015-01-28 09:57:49 +02001331static int ath10k_mac_vif_disable_keepalive(struct ath10k_vif *arvif)
1332{
1333 struct ath10k *ar = arvif->ar;
1334 struct wmi_sta_keepalive_arg arg = {};
1335 int ret;
1336
1337 lockdep_assert_held(&arvif->ar->conf_mutex);
1338
1339 if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
1340 return 0;
1341
1342 if (!test_bit(WMI_SERVICE_STA_KEEP_ALIVE, ar->wmi.svc_map))
1343 return 0;
1344
1345 /* Some firmware revisions have a bug and ignore the `enabled` field.
1346 * Instead use the interval to disable the keepalive.
1347 */
1348 arg.vdev_id = arvif->vdev_id;
1349 arg.enabled = 1;
1350 arg.method = WMI_STA_KEEPALIVE_METHOD_NULL_FRAME;
1351 arg.interval = WMI_STA_KEEPALIVE_INTERVAL_DISABLE;
1352
1353 ret = ath10k_wmi_sta_keepalive(ar, &arg);
1354 if (ret) {
1355 ath10k_warn(ar, "failed to submit keepalive on vdev %i: %d\n",
1356 arvif->vdev_id, ret);
1357 return ret;
1358 }
1359
1360 return 0;
1361}
1362
Kalle Valo5e3dd152013-06-12 20:52:10 +03001363/**********************/
1364/* Station management */
1365/**********************/
1366
Michal Kazior590922a2014-10-21 10:10:29 +03001367static u32 ath10k_peer_assoc_h_listen_intval(struct ath10k *ar,
1368 struct ieee80211_vif *vif)
1369{
1370 /* Some firmware revisions have unstable STA powersave when listen
1371 * interval is set too high (e.g. 5). The symptoms are firmware doesn't
1372 * generate NullFunc frames properly even if buffered frames have been
1373 * indicated in Beacon TIM. Firmware would seldom wake up to pull
1374 * buffered frames. Often pinging the device from AP would simply fail.
1375 *
1376 * As a workaround set it to 1.
1377 */
1378 if (vif->type == NL80211_IFTYPE_STATION)
1379 return 1;
1380
1381 return ar->hw->conf.listen_interval;
1382}
1383
Kalle Valo5e3dd152013-06-12 20:52:10 +03001384static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001385 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001386 struct ieee80211_sta *sta,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001387 struct wmi_peer_assoc_complete_arg *arg)
1388{
Michal Kazior590922a2014-10-21 10:10:29 +03001389 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1390
Michal Kazior548db542013-07-05 16:15:15 +03001391 lockdep_assert_held(&ar->conf_mutex);
1392
Kalle Valob25f32c2014-09-14 12:50:49 +03001393 ether_addr_copy(arg->addr, sta->addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001394 arg->vdev_id = arvif->vdev_id;
1395 arg->peer_aid = sta->aid;
1396 arg->peer_flags |= WMI_PEER_AUTH;
Michal Kazior590922a2014-10-21 10:10:29 +03001397 arg->peer_listen_intval = ath10k_peer_assoc_h_listen_intval(ar, vif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001398 arg->peer_num_spatial_streams = 1;
Michal Kazior590922a2014-10-21 10:10:29 +03001399 arg->peer_caps = vif->bss_conf.assoc_capability;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001400}
1401
1402static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001403 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001404 struct wmi_peer_assoc_complete_arg *arg)
1405{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001406 struct ieee80211_bss_conf *info = &vif->bss_conf;
1407 struct cfg80211_bss *bss;
1408 const u8 *rsnie = NULL;
1409 const u8 *wpaie = NULL;
1410
Michal Kazior548db542013-07-05 16:15:15 +03001411 lockdep_assert_held(&ar->conf_mutex);
1412
Kalle Valo5e3dd152013-06-12 20:52:10 +03001413 bss = cfg80211_get_bss(ar->hw->wiphy, ar->hw->conf.chandef.chan,
Dedy Lansky6eb18132015-02-08 15:52:03 +02001414 info->bssid, NULL, 0, IEEE80211_BSS_TYPE_ANY,
1415 IEEE80211_PRIVACY_ANY);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001416 if (bss) {
1417 const struct cfg80211_bss_ies *ies;
1418
1419 rcu_read_lock();
1420 rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
1421
1422 ies = rcu_dereference(bss->ies);
1423
1424 wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
Kalle Valo5b07e072014-09-14 12:50:06 +03001425 WLAN_OUI_TYPE_MICROSOFT_WPA,
1426 ies->data,
1427 ies->len);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001428 rcu_read_unlock();
1429 cfg80211_put_bss(ar->hw->wiphy, bss);
1430 }
1431
1432 /* FIXME: base on RSN IE/WPA IE is a correct idea? */
1433 if (rsnie || wpaie) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001434 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001435 arg->peer_flags |= WMI_PEER_NEED_PTK_4_WAY;
1436 }
1437
1438 if (wpaie) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001439 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: wpa ie found\n", __func__);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001440 arg->peer_flags |= WMI_PEER_NEED_GTK_2_WAY;
1441 }
1442}
1443
1444static void ath10k_peer_assoc_h_rates(struct ath10k *ar,
1445 struct ieee80211_sta *sta,
1446 struct wmi_peer_assoc_complete_arg *arg)
1447{
1448 struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
1449 const struct ieee80211_supported_band *sband;
1450 const struct ieee80211_rate *rates;
1451 u32 ratemask;
1452 int i;
1453
Michal Kazior548db542013-07-05 16:15:15 +03001454 lockdep_assert_held(&ar->conf_mutex);
1455
Kalle Valo5e3dd152013-06-12 20:52:10 +03001456 sband = ar->hw->wiphy->bands[ar->hw->conf.chandef.chan->band];
1457 ratemask = sta->supp_rates[ar->hw->conf.chandef.chan->band];
1458 rates = sband->bitrates;
1459
1460 rateset->num_rates = 0;
1461
1462 for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
1463 if (!(ratemask & 1))
1464 continue;
1465
1466 rateset->rates[rateset->num_rates] = rates->hw_value;
1467 rateset->num_rates++;
1468 }
1469}
1470
1471static void ath10k_peer_assoc_h_ht(struct ath10k *ar,
1472 struct ieee80211_sta *sta,
1473 struct wmi_peer_assoc_complete_arg *arg)
1474{
1475 const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001476 int i, n;
Kalle Valoaf762c02014-09-14 12:50:17 +03001477 u32 stbc;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001478
Michal Kazior548db542013-07-05 16:15:15 +03001479 lockdep_assert_held(&ar->conf_mutex);
1480
Kalle Valo5e3dd152013-06-12 20:52:10 +03001481 if (!ht_cap->ht_supported)
1482 return;
1483
1484 arg->peer_flags |= WMI_PEER_HT;
1485 arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1486 ht_cap->ampdu_factor)) - 1;
1487
1488 arg->peer_mpdu_density =
1489 ath10k_parse_mpdudensity(ht_cap->ampdu_density);
1490
1491 arg->peer_ht_caps = ht_cap->cap;
1492 arg->peer_rate_caps |= WMI_RC_HT_FLAG;
1493
1494 if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
1495 arg->peer_flags |= WMI_PEER_LDPC;
1496
1497 if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
1498 arg->peer_flags |= WMI_PEER_40MHZ;
1499 arg->peer_rate_caps |= WMI_RC_CW40_FLAG;
1500 }
1501
1502 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
1503 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
1504
1505 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
1506 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
1507
1508 if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
1509 arg->peer_rate_caps |= WMI_RC_TX_STBC_FLAG;
1510 arg->peer_flags |= WMI_PEER_STBC;
1511 }
1512
1513 if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001514 stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
1515 stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
1516 stbc = stbc << WMI_RC_RX_STBC_FLAG_S;
1517 arg->peer_rate_caps |= stbc;
1518 arg->peer_flags |= WMI_PEER_STBC;
1519 }
1520
Kalle Valo5e3dd152013-06-12 20:52:10 +03001521 if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
1522 arg->peer_rate_caps |= WMI_RC_TS_FLAG;
1523 else if (ht_cap->mcs.rx_mask[1])
1524 arg->peer_rate_caps |= WMI_RC_DS_FLAG;
1525
1526 for (i = 0, n = 0; i < IEEE80211_HT_MCS_MASK_LEN*8; i++)
1527 if (ht_cap->mcs.rx_mask[i/8] & (1 << i%8))
1528 arg->peer_ht_rates.rates[n++] = i;
1529
Bartosz Markowskifd71f802014-02-10 13:12:55 +01001530 /*
1531 * This is a workaround for HT-enabled STAs which break the spec
1532 * and have no HT capabilities RX mask (no HT RX MCS map).
1533 *
1534 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
1535 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
1536 *
1537 * Firmware asserts if such situation occurs.
1538 */
1539 if (n == 0) {
1540 arg->peer_ht_rates.num_rates = 8;
1541 for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
1542 arg->peer_ht_rates.rates[i] = i;
1543 } else {
1544 arg->peer_ht_rates.num_rates = n;
1545 arg->peer_num_spatial_streams = sta->rx_nss;
1546 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001547
Michal Kazior7aa7a722014-08-25 12:09:38 +02001548 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03001549 arg->addr,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001550 arg->peer_ht_rates.num_rates,
1551 arg->peer_num_spatial_streams);
1552}
1553
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001554static int ath10k_peer_assoc_qos_ap(struct ath10k *ar,
1555 struct ath10k_vif *arvif,
1556 struct ieee80211_sta *sta)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001557{
1558 u32 uapsd = 0;
1559 u32 max_sp = 0;
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001560 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001561
Michal Kazior548db542013-07-05 16:15:15 +03001562 lockdep_assert_held(&ar->conf_mutex);
1563
Kalle Valo5e3dd152013-06-12 20:52:10 +03001564 if (sta->wme && sta->uapsd_queues) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001565 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001566 sta->uapsd_queues, sta->max_sp);
1567
Kalle Valo5e3dd152013-06-12 20:52:10 +03001568 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1569 uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
1570 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
1571 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1572 uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
1573 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
1574 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1575 uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
1576 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
1577 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1578 uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
1579 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
1580
Kalle Valo5e3dd152013-06-12 20:52:10 +03001581 if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
1582 max_sp = sta->max_sp;
1583
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001584 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1585 sta->addr,
1586 WMI_AP_PS_PEER_PARAM_UAPSD,
1587 uapsd);
1588 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001589 ath10k_warn(ar, "failed to set ap ps peer param uapsd for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001590 arvif->vdev_id, ret);
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001591 return ret;
1592 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001593
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001594 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1595 sta->addr,
1596 WMI_AP_PS_PEER_PARAM_MAX_SP,
1597 max_sp);
1598 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001599 ath10k_warn(ar, "failed to set ap ps peer param max sp for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001600 arvif->vdev_id, ret);
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001601 return ret;
1602 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001603
1604 /* TODO setup this based on STA listen interval and
1605 beacon interval. Currently we don't know
1606 sta->listen_interval - mac80211 patch required.
1607 Currently use 10 seconds */
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001608 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id, sta->addr,
Kalle Valo5b07e072014-09-14 12:50:06 +03001609 WMI_AP_PS_PEER_PARAM_AGEOUT_TIME,
1610 10);
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001611 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001612 ath10k_warn(ar, "failed to set ap ps peer param ageout time for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001613 arvif->vdev_id, ret);
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001614 return ret;
1615 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001616 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001617
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001618 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001619}
1620
1621static void ath10k_peer_assoc_h_vht(struct ath10k *ar,
1622 struct ieee80211_sta *sta,
1623 struct wmi_peer_assoc_complete_arg *arg)
1624{
1625 const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
Sujith Manoharana24b88b2013-10-07 19:51:57 -07001626 u8 ampdu_factor;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001627
1628 if (!vht_cap->vht_supported)
1629 return;
1630
1631 arg->peer_flags |= WMI_PEER_VHT;
Yanbo Lid68bb122015-01-23 08:18:20 +08001632
1633 if (ar->hw->conf.chandef.chan->band == IEEE80211_BAND_2GHZ)
1634 arg->peer_flags |= WMI_PEER_VHT_2G;
1635
Kalle Valo5e3dd152013-06-12 20:52:10 +03001636 arg->peer_vht_caps = vht_cap->cap;
1637
Sujith Manoharana24b88b2013-10-07 19:51:57 -07001638 ampdu_factor = (vht_cap->cap &
1639 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
1640 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
1641
1642 /* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
1643 * zero in VHT IE. Using it would result in degraded throughput.
1644 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
1645 * it if VHT max_mpdu is smaller. */
1646 arg->peer_max_mpdu = max(arg->peer_max_mpdu,
1647 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1648 ampdu_factor)) - 1);
1649
Kalle Valo5e3dd152013-06-12 20:52:10 +03001650 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1651 arg->peer_flags |= WMI_PEER_80MHZ;
1652
1653 arg->peer_vht_rates.rx_max_rate =
1654 __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
1655 arg->peer_vht_rates.rx_mcs_set =
1656 __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
1657 arg->peer_vht_rates.tx_max_rate =
1658 __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
1659 arg->peer_vht_rates.tx_mcs_set =
1660 __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
1661
Michal Kazior7aa7a722014-08-25 12:09:38 +02001662 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03001663 sta->addr, arg->peer_max_mpdu, arg->peer_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001664}
1665
1666static void ath10k_peer_assoc_h_qos(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001667 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001668 struct ieee80211_sta *sta,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001669 struct wmi_peer_assoc_complete_arg *arg)
1670{
Michal Kazior590922a2014-10-21 10:10:29 +03001671 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1672
Kalle Valo5e3dd152013-06-12 20:52:10 +03001673 switch (arvif->vdev_type) {
1674 case WMI_VDEV_TYPE_AP:
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001675 if (sta->wme)
1676 arg->peer_flags |= WMI_PEER_QOS;
1677
1678 if (sta->wme && sta->uapsd_queues) {
1679 arg->peer_flags |= WMI_PEER_APSD;
1680 arg->peer_rate_caps |= WMI_RC_UAPSD_FLAG;
1681 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001682 break;
1683 case WMI_VDEV_TYPE_STA:
Michal Kazior590922a2014-10-21 10:10:29 +03001684 if (vif->bss_conf.qos)
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001685 arg->peer_flags |= WMI_PEER_QOS;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001686 break;
Janusz Dziedzic627d9842014-12-17 12:29:54 +02001687 case WMI_VDEV_TYPE_IBSS:
1688 if (sta->wme)
1689 arg->peer_flags |= WMI_PEER_QOS;
1690 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001691 default:
1692 break;
1693 }
Janusz Dziedzic627d9842014-12-17 12:29:54 +02001694
1695 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM qos %d\n",
1696 sta->addr, !!(arg->peer_flags & WMI_PEER_QOS));
Kalle Valo5e3dd152013-06-12 20:52:10 +03001697}
1698
Michal Kazior91b12082014-12-12 12:41:35 +01001699static bool ath10k_mac_sta_has_11g_rates(struct ieee80211_sta *sta)
1700{
1701 /* First 4 rates in ath10k_rates are CCK (11b) rates. */
1702 return sta->supp_rates[IEEE80211_BAND_2GHZ] >> 4;
1703}
1704
Kalle Valo5e3dd152013-06-12 20:52:10 +03001705static void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001706 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001707 struct ieee80211_sta *sta,
1708 struct wmi_peer_assoc_complete_arg *arg)
1709{
1710 enum wmi_phy_mode phymode = MODE_UNKNOWN;
1711
Kalle Valo5e3dd152013-06-12 20:52:10 +03001712 switch (ar->hw->conf.chandef.chan->band) {
1713 case IEEE80211_BAND_2GHZ:
Yanbo Lid68bb122015-01-23 08:18:20 +08001714 if (sta->vht_cap.vht_supported) {
1715 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1716 phymode = MODE_11AC_VHT40;
1717 else
1718 phymode = MODE_11AC_VHT20;
1719 } else if (sta->ht_cap.ht_supported) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001720 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1721 phymode = MODE_11NG_HT40;
1722 else
1723 phymode = MODE_11NG_HT20;
Michal Kazior91b12082014-12-12 12:41:35 +01001724 } else if (ath10k_mac_sta_has_11g_rates(sta)) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001725 phymode = MODE_11G;
Michal Kazior91b12082014-12-12 12:41:35 +01001726 } else {
1727 phymode = MODE_11B;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001728 }
1729
1730 break;
1731 case IEEE80211_BAND_5GHZ:
Sujith Manoharan7cc45e92013-09-08 18:19:55 +03001732 /*
1733 * Check VHT first.
1734 */
1735 if (sta->vht_cap.vht_supported) {
1736 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1737 phymode = MODE_11AC_VHT80;
1738 else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1739 phymode = MODE_11AC_VHT40;
1740 else if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
1741 phymode = MODE_11AC_VHT20;
1742 } else if (sta->ht_cap.ht_supported) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001743 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1744 phymode = MODE_11NA_HT40;
1745 else
1746 phymode = MODE_11NA_HT20;
1747 } else {
1748 phymode = MODE_11A;
1749 }
1750
1751 break;
1752 default:
1753 break;
1754 }
1755
Michal Kazior7aa7a722014-08-25 12:09:38 +02001756 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM phymode %s\n",
Kalle Valo38a1d472013-09-08 17:56:14 +03001757 sta->addr, ath10k_wmi_phymode_str(phymode));
Kalle Valo60c3daa2013-09-08 17:56:07 +03001758
Kalle Valo5e3dd152013-06-12 20:52:10 +03001759 arg->peer_phymode = phymode;
1760 WARN_ON(phymode == MODE_UNKNOWN);
1761}
1762
Kalle Valob9ada652013-10-16 15:44:46 +03001763static int ath10k_peer_assoc_prepare(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001764 struct ieee80211_vif *vif,
Kalle Valob9ada652013-10-16 15:44:46 +03001765 struct ieee80211_sta *sta,
Kalle Valob9ada652013-10-16 15:44:46 +03001766 struct wmi_peer_assoc_complete_arg *arg)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001767{
Michal Kazior548db542013-07-05 16:15:15 +03001768 lockdep_assert_held(&ar->conf_mutex);
1769
Kalle Valob9ada652013-10-16 15:44:46 +03001770 memset(arg, 0, sizeof(*arg));
Kalle Valo5e3dd152013-06-12 20:52:10 +03001771
Michal Kazior590922a2014-10-21 10:10:29 +03001772 ath10k_peer_assoc_h_basic(ar, vif, sta, arg);
1773 ath10k_peer_assoc_h_crypto(ar, vif, arg);
Kalle Valob9ada652013-10-16 15:44:46 +03001774 ath10k_peer_assoc_h_rates(ar, sta, arg);
1775 ath10k_peer_assoc_h_ht(ar, sta, arg);
1776 ath10k_peer_assoc_h_vht(ar, sta, arg);
Michal Kazior590922a2014-10-21 10:10:29 +03001777 ath10k_peer_assoc_h_qos(ar, vif, sta, arg);
1778 ath10k_peer_assoc_h_phymode(ar, vif, sta, arg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001779
Kalle Valob9ada652013-10-16 15:44:46 +03001780 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001781}
1782
Michal Kazior90046f52014-02-14 14:45:51 +01001783static const u32 ath10k_smps_map[] = {
1784 [WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
1785 [WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
1786 [WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
1787 [WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
1788};
1789
1790static int ath10k_setup_peer_smps(struct ath10k *ar, struct ath10k_vif *arvif,
1791 const u8 *addr,
1792 const struct ieee80211_sta_ht_cap *ht_cap)
1793{
1794 int smps;
1795
1796 if (!ht_cap->ht_supported)
1797 return 0;
1798
1799 smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
1800 smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
1801
1802 if (smps >= ARRAY_SIZE(ath10k_smps_map))
1803 return -EINVAL;
1804
1805 return ath10k_wmi_peer_set_param(ar, arvif->vdev_id, addr,
1806 WMI_PEER_SMPS_STATE,
1807 ath10k_smps_map[smps]);
1808}
1809
Michal Kazior139e1702015-02-15 16:50:42 +02001810static int ath10k_mac_vif_recalc_txbf(struct ath10k *ar,
1811 struct ieee80211_vif *vif,
1812 struct ieee80211_sta_vht_cap vht_cap)
1813{
1814 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1815 int ret;
1816 u32 param;
1817 u32 value;
1818
1819 if (!(ar->vht_cap_info &
1820 (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
1821 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
1822 IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
1823 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)))
1824 return 0;
1825
1826 param = ar->wmi.vdev_param->txbf;
1827 value = 0;
1828
1829 if (WARN_ON(param == WMI_VDEV_PARAM_UNSUPPORTED))
1830 return 0;
1831
1832 /* The following logic is correct. If a remote STA advertises support
1833 * for being a beamformer then we should enable us being a beamformee.
1834 */
1835
1836 if (ar->vht_cap_info &
1837 (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
1838 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)) {
1839 if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)
1840 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
1841
1842 if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)
1843 value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
1844 }
1845
1846 if (ar->vht_cap_info &
1847 (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
1848 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)) {
1849 if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)
1850 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
1851
1852 if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)
1853 value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
1854 }
1855
1856 if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFEE)
1857 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
1858
1859 if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFER)
1860 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
1861
1862 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param, value);
1863 if (ret) {
1864 ath10k_warn(ar, "failed to submit vdev param txbf 0x%x: %d\n",
1865 value, ret);
1866 return ret;
1867 }
1868
1869 return 0;
1870}
1871
Kalle Valo5e3dd152013-06-12 20:52:10 +03001872/* can be called only in mac80211 callbacks due to `key_count` usage */
1873static void ath10k_bss_assoc(struct ieee80211_hw *hw,
1874 struct ieee80211_vif *vif,
1875 struct ieee80211_bss_conf *bss_conf)
1876{
1877 struct ath10k *ar = hw->priv;
1878 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Michal Kazior90046f52014-02-14 14:45:51 +01001879 struct ieee80211_sta_ht_cap ht_cap;
Michal Kazior139e1702015-02-15 16:50:42 +02001880 struct ieee80211_sta_vht_cap vht_cap;
Kalle Valob9ada652013-10-16 15:44:46 +03001881 struct wmi_peer_assoc_complete_arg peer_arg;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001882 struct ieee80211_sta *ap_sta;
1883 int ret;
1884
Michal Kazior548db542013-07-05 16:15:15 +03001885 lockdep_assert_held(&ar->conf_mutex);
1886
Michal Kazior077efc82014-10-21 10:10:29 +03001887 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
1888 arvif->vdev_id, arvif->bssid, arvif->aid);
1889
Kalle Valo5e3dd152013-06-12 20:52:10 +03001890 rcu_read_lock();
1891
1892 ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
1893 if (!ap_sta) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001894 ath10k_warn(ar, "failed to find station entry for bss %pM vdev %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02001895 bss_conf->bssid, arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001896 rcu_read_unlock();
1897 return;
1898 }
1899
Michal Kazior90046f52014-02-14 14:45:51 +01001900 /* ap_sta must be accessed only within rcu section which must be left
1901 * before calling ath10k_setup_peer_smps() which might sleep. */
1902 ht_cap = ap_sta->ht_cap;
Michal Kazior139e1702015-02-15 16:50:42 +02001903 vht_cap = ap_sta->vht_cap;
Michal Kazior90046f52014-02-14 14:45:51 +01001904
Michal Kazior590922a2014-10-21 10:10:29 +03001905 ret = ath10k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001906 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001907 ath10k_warn(ar, "failed to prepare peer assoc for %pM vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001908 bss_conf->bssid, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001909 rcu_read_unlock();
1910 return;
1911 }
1912
1913 rcu_read_unlock();
1914
Kalle Valob9ada652013-10-16 15:44:46 +03001915 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
1916 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001917 ath10k_warn(ar, "failed to run peer assoc for %pM vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001918 bss_conf->bssid, arvif->vdev_id, ret);
Kalle Valob9ada652013-10-16 15:44:46 +03001919 return;
1920 }
1921
Michal Kazior90046f52014-02-14 14:45:51 +01001922 ret = ath10k_setup_peer_smps(ar, arvif, bss_conf->bssid, &ht_cap);
1923 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001924 ath10k_warn(ar, "failed to setup peer SMPS for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001925 arvif->vdev_id, ret);
Michal Kazior90046f52014-02-14 14:45:51 +01001926 return;
1927 }
1928
Michal Kazior139e1702015-02-15 16:50:42 +02001929 ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
1930 if (ret) {
1931 ath10k_warn(ar, "failed to recalc txbf for vdev %i on bss %pM: %d\n",
1932 arvif->vdev_id, bss_conf->bssid, ret);
1933 return;
1934 }
1935
Michal Kazior7aa7a722014-08-25 12:09:38 +02001936 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03001937 "mac vdev %d up (associated) bssid %pM aid %d\n",
1938 arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
1939
Michal Kazior077efc82014-10-21 10:10:29 +03001940 WARN_ON(arvif->is_up);
1941
Michal Kaziorc930f742014-01-23 11:38:25 +01001942 arvif->aid = bss_conf->aid;
Kalle Valob25f32c2014-09-14 12:50:49 +03001943 ether_addr_copy(arvif->bssid, bss_conf->bssid);
Michal Kaziorc930f742014-01-23 11:38:25 +01001944
1945 ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
1946 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001947 ath10k_warn(ar, "failed to set vdev %d up: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001948 arvif->vdev_id, ret);
Michal Kaziorc930f742014-01-23 11:38:25 +01001949 return;
1950 }
1951
1952 arvif->is_up = true;
Michal Kazior0a987fb2015-02-13 13:30:15 +01001953
1954 /* Workaround: Some firmware revisions (tested with qca6174
1955 * WLAN.RM.2.0-00073) have buggy powersave state machine and must be
1956 * poked with peer param command.
1957 */
1958 ret = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, arvif->bssid,
1959 WMI_PEER_DUMMY_VAR, 1);
1960 if (ret) {
1961 ath10k_warn(ar, "failed to poke peer %pM param for ps workaround on vdev %i: %d\n",
1962 arvif->bssid, arvif->vdev_id, ret);
1963 return;
1964 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001965}
1966
Kalle Valo5e3dd152013-06-12 20:52:10 +03001967static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
1968 struct ieee80211_vif *vif)
1969{
1970 struct ath10k *ar = hw->priv;
1971 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Michal Kazior139e1702015-02-15 16:50:42 +02001972 struct ieee80211_sta_vht_cap vht_cap = {};
Kalle Valo5e3dd152013-06-12 20:52:10 +03001973 int ret;
1974
Michal Kazior548db542013-07-05 16:15:15 +03001975 lockdep_assert_held(&ar->conf_mutex);
1976
Michal Kazior077efc82014-10-21 10:10:29 +03001977 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
1978 arvif->vdev_id, arvif->bssid);
Kalle Valo60c3daa2013-09-08 17:56:07 +03001979
Kalle Valo5e3dd152013-06-12 20:52:10 +03001980 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
Michal Kazior077efc82014-10-21 10:10:29 +03001981 if (ret)
1982 ath10k_warn(ar, "faield to down vdev %i: %d\n",
1983 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001984
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +02001985 arvif->def_wep_key_idx = -1;
1986
Michal Kazior139e1702015-02-15 16:50:42 +02001987 ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
1988 if (ret) {
1989 ath10k_warn(ar, "failed to recalc txbf for vdev %i: %d\n",
1990 arvif->vdev_id, ret);
1991 return;
1992 }
1993
Michal Kaziorc930f742014-01-23 11:38:25 +01001994 arvif->is_up = false;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001995}
1996
Michal Kazior590922a2014-10-21 10:10:29 +03001997static int ath10k_station_assoc(struct ath10k *ar,
1998 struct ieee80211_vif *vif,
1999 struct ieee80211_sta *sta,
2000 bool reassoc)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002001{
Michal Kazior590922a2014-10-21 10:10:29 +03002002 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Kalle Valob9ada652013-10-16 15:44:46 +03002003 struct wmi_peer_assoc_complete_arg peer_arg;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002004 int ret = 0;
2005
Michal Kazior548db542013-07-05 16:15:15 +03002006 lockdep_assert_held(&ar->conf_mutex);
2007
Michal Kazior590922a2014-10-21 10:10:29 +03002008 ret = ath10k_peer_assoc_prepare(ar, vif, sta, &peer_arg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002009 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002010 ath10k_warn(ar, "failed to prepare WMI peer assoc for %pM vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02002011 sta->addr, arvif->vdev_id, ret);
Kalle Valob9ada652013-10-16 15:44:46 +03002012 return ret;
2013 }
2014
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02002015 peer_arg.peer_reassoc = reassoc;
Kalle Valob9ada652013-10-16 15:44:46 +03002016 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
2017 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002018 ath10k_warn(ar, "failed to run peer assoc for STA %pM vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02002019 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002020 return ret;
2021 }
2022
Michal Kaziorb1ecde32014-10-21 10:10:29 +03002023 /* Re-assoc is run only to update supported rates for given station. It
2024 * doesn't make much sense to reconfigure the peer completely.
2025 */
2026 if (!reassoc) {
2027 ret = ath10k_setup_peer_smps(ar, arvif, sta->addr,
2028 &sta->ht_cap);
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02002029 if (ret) {
Michal Kaziorb1ecde32014-10-21 10:10:29 +03002030 ath10k_warn(ar, "failed to setup peer SMPS for vdev %d: %d\n",
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02002031 arvif->vdev_id, ret);
2032 return ret;
2033 }
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02002034
Michal Kaziorb1ecde32014-10-21 10:10:29 +03002035 ret = ath10k_peer_assoc_qos_ap(ar, arvif, sta);
2036 if (ret) {
2037 ath10k_warn(ar, "failed to set qos params for STA %pM for vdev %i: %d\n",
2038 sta->addr, arvif->vdev_id, ret);
2039 return ret;
2040 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002041
Michal Kaziorb1ecde32014-10-21 10:10:29 +03002042 if (!sta->wme) {
2043 arvif->num_legacy_stations++;
2044 ret = ath10k_recalc_rtscts_prot(arvif);
2045 if (ret) {
2046 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
2047 arvif->vdev_id, ret);
2048 return ret;
2049 }
2050 }
2051
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +02002052 /* Plumb cached keys only for static WEP */
2053 if (arvif->def_wep_key_idx != -1) {
2054 ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
2055 if (ret) {
2056 ath10k_warn(ar, "failed to install peer wep keys for vdev %i: %d\n",
2057 arvif->vdev_id, ret);
2058 return ret;
2059 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002060 }
2061 }
2062
Kalle Valo5e3dd152013-06-12 20:52:10 +03002063 return ret;
2064}
2065
Michal Kazior590922a2014-10-21 10:10:29 +03002066static int ath10k_station_disassoc(struct ath10k *ar,
2067 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002068 struct ieee80211_sta *sta)
2069{
Michal Kazior590922a2014-10-21 10:10:29 +03002070 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002071 int ret = 0;
2072
2073 lockdep_assert_held(&ar->conf_mutex);
2074
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02002075 if (!sta->wme) {
2076 arvif->num_legacy_stations--;
2077 ret = ath10k_recalc_rtscts_prot(arvif);
2078 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002079 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02002080 arvif->vdev_id, ret);
2081 return ret;
2082 }
2083 }
2084
Kalle Valo5e3dd152013-06-12 20:52:10 +03002085 ret = ath10k_clear_peer_keys(arvif, sta->addr);
2086 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002087 ath10k_warn(ar, "failed to clear all peer wep keys for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02002088 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002089 return ret;
2090 }
2091
2092 return ret;
2093}
2094
2095/**************/
2096/* Regulatory */
2097/**************/
2098
2099static int ath10k_update_channel_list(struct ath10k *ar)
2100{
2101 struct ieee80211_hw *hw = ar->hw;
2102 struct ieee80211_supported_band **bands;
2103 enum ieee80211_band band;
2104 struct ieee80211_channel *channel;
2105 struct wmi_scan_chan_list_arg arg = {0};
2106 struct wmi_channel_arg *ch;
2107 bool passive;
2108 int len;
2109 int ret;
2110 int i;
2111
Michal Kazior548db542013-07-05 16:15:15 +03002112 lockdep_assert_held(&ar->conf_mutex);
2113
Kalle Valo5e3dd152013-06-12 20:52:10 +03002114 bands = hw->wiphy->bands;
2115 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2116 if (!bands[band])
2117 continue;
2118
2119 for (i = 0; i < bands[band]->n_channels; i++) {
2120 if (bands[band]->channels[i].flags &
2121 IEEE80211_CHAN_DISABLED)
2122 continue;
2123
2124 arg.n_channels++;
2125 }
2126 }
2127
2128 len = sizeof(struct wmi_channel_arg) * arg.n_channels;
2129 arg.channels = kzalloc(len, GFP_KERNEL);
2130 if (!arg.channels)
2131 return -ENOMEM;
2132
2133 ch = arg.channels;
2134 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2135 if (!bands[band])
2136 continue;
2137
2138 for (i = 0; i < bands[band]->n_channels; i++) {
2139 channel = &bands[band]->channels[i];
2140
2141 if (channel->flags & IEEE80211_CHAN_DISABLED)
2142 continue;
2143
2144 ch->allow_ht = true;
2145
2146 /* FIXME: when should we really allow VHT? */
2147 ch->allow_vht = true;
2148
2149 ch->allow_ibss =
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +02002150 !(channel->flags & IEEE80211_CHAN_NO_IR);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002151
2152 ch->ht40plus =
2153 !(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
2154
Marek Puzyniake8a50f82013-11-20 09:59:47 +02002155 ch->chan_radar =
2156 !!(channel->flags & IEEE80211_CHAN_RADAR);
2157
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +02002158 passive = channel->flags & IEEE80211_CHAN_NO_IR;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002159 ch->passive = passive;
2160
2161 ch->freq = channel->center_freq;
Michal Kazior2d667212014-09-18 15:21:21 +02002162 ch->band_center_freq1 = channel->center_freq;
Michal Kazior89c5c842013-10-23 04:02:13 -07002163 ch->min_power = 0;
Michal Kazior02256932013-10-23 04:02:14 -07002164 ch->max_power = channel->max_power * 2;
2165 ch->max_reg_power = channel->max_reg_power * 2;
2166 ch->max_antenna_gain = channel->max_antenna_gain * 2;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002167 ch->reg_class_id = 0; /* FIXME */
2168
2169 /* FIXME: why use only legacy modes, why not any
2170 * HT/VHT modes? Would that even make any
2171 * difference? */
2172 if (channel->band == IEEE80211_BAND_2GHZ)
2173 ch->mode = MODE_11G;
2174 else
2175 ch->mode = MODE_11A;
2176
2177 if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
2178 continue;
2179
Michal Kazior7aa7a722014-08-25 12:09:38 +02002180 ath10k_dbg(ar, ATH10K_DBG_WMI,
Kalle Valo60c3daa2013-09-08 17:56:07 +03002181 "mac channel [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
2182 ch - arg.channels, arg.n_channels,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002183 ch->freq, ch->max_power, ch->max_reg_power,
2184 ch->max_antenna_gain, ch->mode);
2185
2186 ch++;
2187 }
2188 }
2189
2190 ret = ath10k_wmi_scan_chan_list(ar, &arg);
2191 kfree(arg.channels);
2192
2193 return ret;
2194}
2195
Marek Puzyniak821af6a2014-03-21 17:46:57 +02002196static enum wmi_dfs_region
2197ath10k_mac_get_dfs_region(enum nl80211_dfs_regions dfs_region)
2198{
2199 switch (dfs_region) {
2200 case NL80211_DFS_UNSET:
2201 return WMI_UNINIT_DFS_DOMAIN;
2202 case NL80211_DFS_FCC:
2203 return WMI_FCC_DFS_DOMAIN;
2204 case NL80211_DFS_ETSI:
2205 return WMI_ETSI_DFS_DOMAIN;
2206 case NL80211_DFS_JP:
2207 return WMI_MKK4_DFS_DOMAIN;
2208 }
2209 return WMI_UNINIT_DFS_DOMAIN;
2210}
2211
Michal Kaziorf7843d72013-07-16 09:38:52 +02002212static void ath10k_regd_update(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002213{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002214 struct reg_dmn_pair_mapping *regpair;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002215 int ret;
Marek Puzyniak821af6a2014-03-21 17:46:57 +02002216 enum wmi_dfs_region wmi_dfs_reg;
2217 enum nl80211_dfs_regions nl_dfs_reg;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002218
Michal Kaziorf7843d72013-07-16 09:38:52 +02002219 lockdep_assert_held(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002220
2221 ret = ath10k_update_channel_list(ar);
2222 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002223 ath10k_warn(ar, "failed to update channel list: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002224
2225 regpair = ar->ath_common.regulatory.regpair;
Michal Kaziorf7843d72013-07-16 09:38:52 +02002226
Marek Puzyniak821af6a2014-03-21 17:46:57 +02002227 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
2228 nl_dfs_reg = ar->dfs_detector->region;
2229 wmi_dfs_reg = ath10k_mac_get_dfs_region(nl_dfs_reg);
2230 } else {
2231 wmi_dfs_reg = WMI_UNINIT_DFS_DOMAIN;
2232 }
2233
Kalle Valo5e3dd152013-06-12 20:52:10 +03002234 /* Target allows setting up per-band regdomain but ath_common provides
2235 * a combined one only */
2236 ret = ath10k_wmi_pdev_set_regdomain(ar,
Kalle Valoef8c0012014-02-13 18:13:12 +02002237 regpair->reg_domain,
2238 regpair->reg_domain, /* 2ghz */
2239 regpair->reg_domain, /* 5ghz */
Kalle Valo5e3dd152013-06-12 20:52:10 +03002240 regpair->reg_2ghz_ctl,
Marek Puzyniak821af6a2014-03-21 17:46:57 +02002241 regpair->reg_5ghz_ctl,
2242 wmi_dfs_reg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002243 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002244 ath10k_warn(ar, "failed to set pdev regdomain: %d\n", ret);
Michal Kaziorf7843d72013-07-16 09:38:52 +02002245}
Michal Kazior548db542013-07-05 16:15:15 +03002246
Michal Kaziorf7843d72013-07-16 09:38:52 +02002247static void ath10k_reg_notifier(struct wiphy *wiphy,
2248 struct regulatory_request *request)
2249{
2250 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
2251 struct ath10k *ar = hw->priv;
Janusz Dziedzic9702c682013-11-20 09:59:41 +02002252 bool result;
Michal Kaziorf7843d72013-07-16 09:38:52 +02002253
2254 ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
2255
Janusz Dziedzic9702c682013-11-20 09:59:41 +02002256 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002257 ath10k_dbg(ar, ATH10K_DBG_REGULATORY, "dfs region 0x%x\n",
Janusz Dziedzic9702c682013-11-20 09:59:41 +02002258 request->dfs_region);
2259 result = ar->dfs_detector->set_dfs_domain(ar->dfs_detector,
2260 request->dfs_region);
2261 if (!result)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002262 ath10k_warn(ar, "DFS region 0x%X not supported, will trigger radar for every pulse\n",
Janusz Dziedzic9702c682013-11-20 09:59:41 +02002263 request->dfs_region);
2264 }
2265
Michal Kaziorf7843d72013-07-16 09:38:52 +02002266 mutex_lock(&ar->conf_mutex);
2267 if (ar->state == ATH10K_STATE_ON)
2268 ath10k_regd_update(ar);
Michal Kazior548db542013-07-05 16:15:15 +03002269 mutex_unlock(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002270}
2271
2272/***************/
2273/* TX handlers */
2274/***************/
2275
Michal Kazior42c3aa62013-10-02 11:03:38 +02002276static u8 ath10k_tx_h_get_tid(struct ieee80211_hdr *hdr)
2277{
2278 if (ieee80211_is_mgmt(hdr->frame_control))
2279 return HTT_DATA_TX_EXT_TID_MGMT;
2280
2281 if (!ieee80211_is_data_qos(hdr->frame_control))
2282 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
2283
2284 if (!is_unicast_ether_addr(ieee80211_get_DA(hdr)))
2285 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
2286
2287 return ieee80211_get_qos_ctl(hdr)[0] & IEEE80211_QOS_CTL_TID_MASK;
2288}
2289
Michal Kazior2b37c292014-09-02 11:00:22 +03002290static u8 ath10k_tx_h_get_vdev_id(struct ath10k *ar, struct ieee80211_vif *vif)
Michal Kaziorddb6ad72013-10-02 11:03:39 +02002291{
Michal Kazior2b37c292014-09-02 11:00:22 +03002292 if (vif)
2293 return ath10k_vif_to_arvif(vif)->vdev_id;
Michal Kaziorddb6ad72013-10-02 11:03:39 +02002294
Michal Kazior1bbc0972014-04-08 09:45:47 +03002295 if (ar->monitor_started)
Michal Kaziorddb6ad72013-10-02 11:03:39 +02002296 return ar->monitor_vdev_id;
2297
Michal Kazior7aa7a722014-08-25 12:09:38 +02002298 ath10k_warn(ar, "failed to resolve vdev id\n");
Michal Kaziorddb6ad72013-10-02 11:03:39 +02002299 return 0;
2300}
2301
Michal Kazior4b604552014-07-21 21:03:09 +03002302/* HTT Tx uses Native Wifi tx mode which expects 802.11 frames without QoS
2303 * Control in the header.
Kalle Valo5e3dd152013-06-12 20:52:10 +03002304 */
Michal Kazior4b604552014-07-21 21:03:09 +03002305static void ath10k_tx_h_nwifi(struct ieee80211_hw *hw, struct sk_buff *skb)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002306{
2307 struct ieee80211_hdr *hdr = (void *)skb->data;
Michal Kaziorc21c64d2014-07-21 21:03:10 +03002308 struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002309 u8 *qos_ctl;
2310
2311 if (!ieee80211_is_data_qos(hdr->frame_control))
2312 return;
2313
2314 qos_ctl = ieee80211_get_qos_ctl(hdr);
Michal Kaziorba0ccd72013-07-22 14:25:28 +02002315 memmove(skb->data + IEEE80211_QOS_CTL_LEN,
2316 skb->data, (void *)qos_ctl - (void *)skb->data);
2317 skb_pull(skb, IEEE80211_QOS_CTL_LEN);
Michal Kaziorc21c64d2014-07-21 21:03:10 +03002318
2319 /* Fw/Hw generates a corrupted QoS Control Field for QoS NullFunc
2320 * frames. Powersave is handled by the fw/hw so QoS NyllFunc frames are
2321 * used only for CQM purposes (e.g. hostapd station keepalive ping) so
2322 * it is safe to downgrade to NullFunc.
2323 */
Michal Kaziorbf0a26d2015-01-24 12:14:51 +02002324 hdr = (void *)skb->data;
Michal Kaziorc21c64d2014-07-21 21:03:10 +03002325 if (ieee80211_is_qos_nullfunc(hdr->frame_control)) {
2326 hdr->frame_control &= ~__cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
2327 cb->htt.tid = HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
2328 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002329}
2330
Michal Kazior4b604552014-07-21 21:03:09 +03002331static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar,
2332 struct ieee80211_vif *vif,
2333 struct sk_buff *skb)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002334{
2335 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002336 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2337
2338 /* This is case only for P2P_GO */
2339 if (arvif->vdev_type != WMI_VDEV_TYPE_AP ||
2340 arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
2341 return;
2342
2343 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
2344 spin_lock_bh(&ar->data_lock);
2345 if (arvif->u.ap.noa_data)
2346 if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
2347 GFP_ATOMIC))
2348 memcpy(skb_put(skb, arvif->u.ap.noa_len),
2349 arvif->u.ap.noa_data,
2350 arvif->u.ap.noa_len);
2351 spin_unlock_bh(&ar->data_lock);
2352 }
2353}
2354
Michal Kazior8d6d3622014-11-24 14:58:31 +01002355static bool ath10k_mac_need_offchan_tx_work(struct ath10k *ar)
2356{
2357 /* FIXME: Not really sure since when the behaviour changed. At some
2358 * point new firmware stopped requiring creation of peer entries for
2359 * offchannel tx (and actually creating them causes issues with wmi-htc
2360 * tx credit replenishment and reliability). Assuming it's at least 3.4
2361 * because that's when the `freq` was introduced to TX_FRM HTT command.
2362 */
2363 return !(ar->htt.target_version_major >= 3 &&
2364 ar->htt.target_version_minor >= 4);
2365}
2366
Kalle Valo5e3dd152013-06-12 20:52:10 +03002367static void ath10k_tx_htt(struct ath10k *ar, struct sk_buff *skb)
2368{
2369 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002370 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002371
Michal Kazior961d4c32013-08-09 10:13:34 +02002372 if (ar->htt.target_version_major >= 3) {
2373 /* Since HTT 3.0 there is no separate mgmt tx command */
2374 ret = ath10k_htt_tx(&ar->htt, skb);
2375 goto exit;
2376 }
2377
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002378 if (ieee80211_is_mgmt(hdr->frame_control)) {
2379 if (test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
2380 ar->fw_features)) {
2381 if (skb_queue_len(&ar->wmi_mgmt_tx_queue) >=
2382 ATH10K_MAX_NUM_MGMT_PENDING) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002383 ath10k_warn(ar, "reached WMI management transmit queue limit\n");
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002384 ret = -EBUSY;
2385 goto exit;
2386 }
2387
2388 skb_queue_tail(&ar->wmi_mgmt_tx_queue, skb);
2389 ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
2390 } else {
2391 ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
2392 }
2393 } else if (!test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
2394 ar->fw_features) &&
2395 ieee80211_is_nullfunc(hdr->frame_control)) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002396 /* FW does not report tx status properly for NullFunc frames
2397 * unless they are sent through mgmt tx path. mac80211 sends
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002398 * those frames when it detects link/beacon loss and depends
2399 * on the tx status to be correct. */
Michal Kazioredb82362013-07-05 16:15:14 +03002400 ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002401 } else {
Michal Kazioredb82362013-07-05 16:15:14 +03002402 ret = ath10k_htt_tx(&ar->htt, skb);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002403 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002404
Michal Kazior961d4c32013-08-09 10:13:34 +02002405exit:
Kalle Valo5e3dd152013-06-12 20:52:10 +03002406 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002407 ath10k_warn(ar, "failed to transmit packet, dropping: %d\n",
2408 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002409 ieee80211_free_txskb(ar->hw, skb);
2410 }
2411}
2412
2413void ath10k_offchan_tx_purge(struct ath10k *ar)
2414{
2415 struct sk_buff *skb;
2416
2417 for (;;) {
2418 skb = skb_dequeue(&ar->offchan_tx_queue);
2419 if (!skb)
2420 break;
2421
2422 ieee80211_free_txskb(ar->hw, skb);
2423 }
2424}
2425
2426void ath10k_offchan_tx_work(struct work_struct *work)
2427{
2428 struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
2429 struct ath10k_peer *peer;
2430 struct ieee80211_hdr *hdr;
2431 struct sk_buff *skb;
2432 const u8 *peer_addr;
2433 int vdev_id;
2434 int ret;
2435
2436 /* FW requirement: We must create a peer before FW will send out
2437 * an offchannel frame. Otherwise the frame will be stuck and
2438 * never transmitted. We delete the peer upon tx completion.
2439 * It is unlikely that a peer for offchannel tx will already be
2440 * present. However it may be in some rare cases so account for that.
2441 * Otherwise we might remove a legitimate peer and break stuff. */
2442
2443 for (;;) {
2444 skb = skb_dequeue(&ar->offchan_tx_queue);
2445 if (!skb)
2446 break;
2447
2448 mutex_lock(&ar->conf_mutex);
2449
Michal Kazior7aa7a722014-08-25 12:09:38 +02002450 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac offchannel skb %p\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002451 skb);
2452
2453 hdr = (struct ieee80211_hdr *)skb->data;
2454 peer_addr = ieee80211_get_DA(hdr);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002455 vdev_id = ATH10K_SKB_CB(skb)->vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002456
2457 spin_lock_bh(&ar->data_lock);
2458 peer = ath10k_peer_find(ar, vdev_id, peer_addr);
2459 spin_unlock_bh(&ar->data_lock);
2460
2461 if (peer)
Kalle Valo60c3daa2013-09-08 17:56:07 +03002462 /* FIXME: should this use ath10k_warn()? */
Michal Kazior7aa7a722014-08-25 12:09:38 +02002463 ath10k_dbg(ar, ATH10K_DBG_MAC, "peer %pM on vdev %d already present\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002464 peer_addr, vdev_id);
2465
2466 if (!peer) {
2467 ret = ath10k_peer_create(ar, vdev_id, peer_addr);
2468 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002469 ath10k_warn(ar, "failed to create peer %pM on vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002470 peer_addr, vdev_id, ret);
2471 }
2472
2473 spin_lock_bh(&ar->data_lock);
Wolfram Sang16735d02013-11-14 14:32:02 -08002474 reinit_completion(&ar->offchan_tx_completed);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002475 ar->offchan_tx_skb = skb;
2476 spin_unlock_bh(&ar->data_lock);
2477
2478 ath10k_tx_htt(ar, skb);
2479
2480 ret = wait_for_completion_timeout(&ar->offchan_tx_completed,
2481 3 * HZ);
Nicholas Mc Guire38e2a642015-01-08 13:27:34 +01002482 if (ret == 0)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002483 ath10k_warn(ar, "timed out waiting for offchannel skb %p\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002484 skb);
2485
2486 if (!peer) {
2487 ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
2488 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002489 ath10k_warn(ar, "failed to delete peer %pM on vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002490 peer_addr, vdev_id, ret);
2491 }
2492
2493 mutex_unlock(&ar->conf_mutex);
2494 }
2495}
2496
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002497void ath10k_mgmt_over_wmi_tx_purge(struct ath10k *ar)
2498{
2499 struct sk_buff *skb;
2500
2501 for (;;) {
2502 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
2503 if (!skb)
2504 break;
2505
2506 ieee80211_free_txskb(ar->hw, skb);
2507 }
2508}
2509
2510void ath10k_mgmt_over_wmi_tx_work(struct work_struct *work)
2511{
2512 struct ath10k *ar = container_of(work, struct ath10k, wmi_mgmt_tx_work);
2513 struct sk_buff *skb;
2514 int ret;
2515
2516 for (;;) {
2517 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
2518 if (!skb)
2519 break;
2520
2521 ret = ath10k_wmi_mgmt_tx(ar, skb);
Michal Kazior5fb5e412013-10-28 07:18:13 +01002522 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002523 ath10k_warn(ar, "failed to transmit management frame via WMI: %d\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02002524 ret);
Michal Kazior5fb5e412013-10-28 07:18:13 +01002525 ieee80211_free_txskb(ar->hw, skb);
2526 }
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002527 }
2528}
2529
Kalle Valo5e3dd152013-06-12 20:52:10 +03002530/************/
2531/* Scanning */
2532/************/
2533
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002534void __ath10k_scan_finish(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002535{
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002536 lockdep_assert_held(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002537
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002538 switch (ar->scan.state) {
2539 case ATH10K_SCAN_IDLE:
2540 break;
2541 case ATH10K_SCAN_RUNNING:
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002542 if (ar->scan.is_roc)
2543 ieee80211_remain_on_channel_expired(ar->hw);
John W. Linvillef6eaf1e2015-01-12 16:07:02 -05002544 /* fall through */
Michal Kazior7305d3e2014-11-24 14:58:33 +01002545 case ATH10K_SCAN_ABORTING:
2546 if (!ar->scan.is_roc)
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002547 ieee80211_scan_completed(ar->hw,
2548 (ar->scan.state ==
2549 ATH10K_SCAN_ABORTING));
2550 /* fall through */
2551 case ATH10K_SCAN_STARTING:
2552 ar->scan.state = ATH10K_SCAN_IDLE;
2553 ar->scan_channel = NULL;
2554 ath10k_offchan_tx_purge(ar);
2555 cancel_delayed_work(&ar->scan.timeout);
2556 complete_all(&ar->scan.completed);
2557 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002558 }
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002559}
Kalle Valo5e3dd152013-06-12 20:52:10 +03002560
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002561void ath10k_scan_finish(struct ath10k *ar)
2562{
2563 spin_lock_bh(&ar->data_lock);
2564 __ath10k_scan_finish(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002565 spin_unlock_bh(&ar->data_lock);
2566}
2567
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002568static int ath10k_scan_stop(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002569{
2570 struct wmi_stop_scan_arg arg = {
2571 .req_id = 1, /* FIXME */
2572 .req_type = WMI_SCAN_STOP_ONE,
2573 .u.scan_id = ATH10K_SCAN_ID,
2574 };
2575 int ret;
2576
2577 lockdep_assert_held(&ar->conf_mutex);
2578
Kalle Valo5e3dd152013-06-12 20:52:10 +03002579 ret = ath10k_wmi_stop_scan(ar, &arg);
2580 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002581 ath10k_warn(ar, "failed to stop wmi scan: %d\n", ret);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002582 goto out;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002583 }
2584
Kalle Valo5e3dd152013-06-12 20:52:10 +03002585 ret = wait_for_completion_timeout(&ar->scan.completed, 3*HZ);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002586 if (ret == 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002587 ath10k_warn(ar, "failed to receive scan abortion completion: timed out\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002588 ret = -ETIMEDOUT;
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002589 } else if (ret > 0) {
2590 ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002591 }
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002592
2593out:
2594 /* Scan state should be updated upon scan completion but in case
2595 * firmware fails to deliver the event (for whatever reason) it is
2596 * desired to clean up scan state anyway. Firmware may have just
2597 * dropped the scan completion event delivery due to transport pipe
2598 * being overflown with data and/or it can recover on its own before
2599 * next scan request is submitted.
2600 */
2601 spin_lock_bh(&ar->data_lock);
2602 if (ar->scan.state != ATH10K_SCAN_IDLE)
2603 __ath10k_scan_finish(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002604 spin_unlock_bh(&ar->data_lock);
2605
2606 return ret;
2607}
2608
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002609static void ath10k_scan_abort(struct ath10k *ar)
2610{
2611 int ret;
2612
2613 lockdep_assert_held(&ar->conf_mutex);
2614
2615 spin_lock_bh(&ar->data_lock);
2616
2617 switch (ar->scan.state) {
2618 case ATH10K_SCAN_IDLE:
2619 /* This can happen if timeout worker kicked in and called
2620 * abortion while scan completion was being processed.
2621 */
2622 break;
2623 case ATH10K_SCAN_STARTING:
2624 case ATH10K_SCAN_ABORTING:
Michal Kazior7aa7a722014-08-25 12:09:38 +02002625 ath10k_warn(ar, "refusing scan abortion due to invalid scan state: %s (%d)\n",
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002626 ath10k_scan_state_str(ar->scan.state),
2627 ar->scan.state);
2628 break;
2629 case ATH10K_SCAN_RUNNING:
2630 ar->scan.state = ATH10K_SCAN_ABORTING;
2631 spin_unlock_bh(&ar->data_lock);
2632
2633 ret = ath10k_scan_stop(ar);
2634 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002635 ath10k_warn(ar, "failed to abort scan: %d\n", ret);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002636
2637 spin_lock_bh(&ar->data_lock);
2638 break;
2639 }
2640
2641 spin_unlock_bh(&ar->data_lock);
2642}
2643
2644void ath10k_scan_timeout_work(struct work_struct *work)
2645{
2646 struct ath10k *ar = container_of(work, struct ath10k,
2647 scan.timeout.work);
2648
2649 mutex_lock(&ar->conf_mutex);
2650 ath10k_scan_abort(ar);
2651 mutex_unlock(&ar->conf_mutex);
2652}
2653
Kalle Valo5e3dd152013-06-12 20:52:10 +03002654static int ath10k_start_scan(struct ath10k *ar,
2655 const struct wmi_start_scan_arg *arg)
2656{
2657 int ret;
2658
2659 lockdep_assert_held(&ar->conf_mutex);
2660
2661 ret = ath10k_wmi_start_scan(ar, arg);
2662 if (ret)
2663 return ret;
2664
Kalle Valo5e3dd152013-06-12 20:52:10 +03002665 ret = wait_for_completion_timeout(&ar->scan.started, 1*HZ);
2666 if (ret == 0) {
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002667 ret = ath10k_scan_stop(ar);
2668 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002669 ath10k_warn(ar, "failed to stop scan: %d\n", ret);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002670
2671 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002672 }
2673
Ben Greear2f9eec02015-02-15 16:50:38 +02002674 /* If we failed to start the scan, return error code at
2675 * this point. This is probably due to some issue in the
2676 * firmware, but no need to wedge the driver due to that...
2677 */
2678 spin_lock_bh(&ar->data_lock);
2679 if (ar->scan.state == ATH10K_SCAN_IDLE) {
2680 spin_unlock_bh(&ar->data_lock);
2681 return -EINVAL;
2682 }
2683 spin_unlock_bh(&ar->data_lock);
2684
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002685 /* Add a 200ms margin to account for event/command processing */
2686 ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
2687 msecs_to_jiffies(arg->max_scan_time+200));
Kalle Valo5e3dd152013-06-12 20:52:10 +03002688 return 0;
2689}
2690
2691/**********************/
2692/* mac80211 callbacks */
2693/**********************/
2694
2695static void ath10k_tx(struct ieee80211_hw *hw,
2696 struct ieee80211_tx_control *control,
2697 struct sk_buff *skb)
2698{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002699 struct ath10k *ar = hw->priv;
Michal Kazior4b604552014-07-21 21:03:09 +03002700 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2701 struct ieee80211_vif *vif = info->control.vif;
Michal Kazior4b604552014-07-21 21:03:09 +03002702 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002703
2704 /* We should disable CCK RATE due to P2P */
2705 if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002706 ath10k_dbg(ar, ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002707
Michal Kazior4b604552014-07-21 21:03:09 +03002708 ATH10K_SKB_CB(skb)->htt.is_offchan = false;
2709 ATH10K_SKB_CB(skb)->htt.tid = ath10k_tx_h_get_tid(hdr);
Michal Kazior2b37c292014-09-02 11:00:22 +03002710 ATH10K_SKB_CB(skb)->vdev_id = ath10k_tx_h_get_vdev_id(ar, vif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002711
Michal Kaziorcf84bd42013-07-16 11:04:54 +02002712 /* it makes no sense to process injected frames like that */
Michal Kazior4b604552014-07-21 21:03:09 +03002713 if (vif && vif->type != NL80211_IFTYPE_MONITOR) {
2714 ath10k_tx_h_nwifi(hw, skb);
Michal Kazior4b604552014-07-21 21:03:09 +03002715 ath10k_tx_h_add_p2p_noa_ie(ar, vif, skb);
2716 ath10k_tx_h_seq_no(vif, skb);
Michal Kaziorcf84bd42013-07-16 11:04:54 +02002717 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002718
Kalle Valo5e3dd152013-06-12 20:52:10 +03002719 if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
2720 spin_lock_bh(&ar->data_lock);
Michal Kazior8d6d3622014-11-24 14:58:31 +01002721 ATH10K_SKB_CB(skb)->htt.freq = ar->scan.roc_freq;
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002722 ATH10K_SKB_CB(skb)->vdev_id = ar->scan.vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002723 spin_unlock_bh(&ar->data_lock);
2724
Michal Kazior8d6d3622014-11-24 14:58:31 +01002725 if (ath10k_mac_need_offchan_tx_work(ar)) {
2726 ATH10K_SKB_CB(skb)->htt.freq = 0;
2727 ATH10K_SKB_CB(skb)->htt.is_offchan = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002728
Michal Kazior8d6d3622014-11-24 14:58:31 +01002729 ath10k_dbg(ar, ATH10K_DBG_MAC, "queued offchannel skb %p\n",
2730 skb);
2731
2732 skb_queue_tail(&ar->offchan_tx_queue, skb);
2733 ieee80211_queue_work(hw, &ar->offchan_tx_work);
2734 return;
2735 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002736 }
2737
2738 ath10k_tx_htt(ar, skb);
2739}
2740
Michal Kaziorbca7baf2014-05-26 12:46:03 +03002741/* Must not be called with conf_mutex held as workers can use that also. */
Michal Kazior7962b0d2014-10-28 10:34:38 +01002742void ath10k_drain_tx(struct ath10k *ar)
Michal Kaziorbca7baf2014-05-26 12:46:03 +03002743{
2744 /* make sure rcu-protected mac80211 tx path itself is drained */
2745 synchronize_net();
2746
2747 ath10k_offchan_tx_purge(ar);
2748 ath10k_mgmt_over_wmi_tx_purge(ar);
2749
2750 cancel_work_sync(&ar->offchan_tx_work);
2751 cancel_work_sync(&ar->wmi_mgmt_tx_work);
2752}
2753
Michal Kazioraffd3212013-07-16 09:54:35 +02002754void ath10k_halt(struct ath10k *ar)
Michal Kazior818bdd12013-07-16 09:38:57 +02002755{
Michal Kaziord9bc4b92014-04-23 19:30:06 +03002756 struct ath10k_vif *arvif;
2757
Michal Kazior818bdd12013-07-16 09:38:57 +02002758 lockdep_assert_held(&ar->conf_mutex);
2759
Michal Kazior19337472014-08-28 12:58:16 +02002760 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
2761 ar->filter_flags = 0;
2762 ar->monitor = false;
2763
2764 if (ar->monitor_started)
Michal Kazior1bbc0972014-04-08 09:45:47 +03002765 ath10k_monitor_stop(ar);
Michal Kazior19337472014-08-28 12:58:16 +02002766
2767 ar->monitor_started = false;
Michal Kazior1bbc0972014-04-08 09:45:47 +03002768
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002769 ath10k_scan_finish(ar);
Michal Kazior818bdd12013-07-16 09:38:57 +02002770 ath10k_peer_cleanup_all(ar);
2771 ath10k_core_stop(ar);
2772 ath10k_hif_power_down(ar);
2773
2774 spin_lock_bh(&ar->data_lock);
Michal Kazior64badcb2014-09-18 11:18:02 +03002775 list_for_each_entry(arvif, &ar->arvifs, list)
2776 ath10k_mac_vif_beacon_cleanup(arvif);
Michal Kazior818bdd12013-07-16 09:38:57 +02002777 spin_unlock_bh(&ar->data_lock);
2778}
2779
Ben Greear46acf7b2014-05-16 17:15:38 +03002780static int ath10k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
2781{
2782 struct ath10k *ar = hw->priv;
2783
2784 mutex_lock(&ar->conf_mutex);
2785
2786 if (ar->cfg_tx_chainmask) {
2787 *tx_ant = ar->cfg_tx_chainmask;
2788 *rx_ant = ar->cfg_rx_chainmask;
2789 } else {
2790 *tx_ant = ar->supp_tx_chainmask;
2791 *rx_ant = ar->supp_rx_chainmask;
2792 }
2793
2794 mutex_unlock(&ar->conf_mutex);
2795
2796 return 0;
2797}
2798
Ben Greear5572a952014-11-24 16:22:10 +02002799static void ath10k_check_chain_mask(struct ath10k *ar, u32 cm, const char *dbg)
2800{
2801 /* It is not clear that allowing gaps in chainmask
2802 * is helpful. Probably it will not do what user
2803 * is hoping for, so warn in that case.
2804 */
2805 if (cm == 15 || cm == 7 || cm == 3 || cm == 1 || cm == 0)
2806 return;
2807
2808 ath10k_warn(ar, "mac %s antenna chainmask may be invalid: 0x%x. Suggested values: 15, 7, 3, 1 or 0.\n",
2809 dbg, cm);
2810}
2811
Ben Greear46acf7b2014-05-16 17:15:38 +03002812static int __ath10k_set_antenna(struct ath10k *ar, u32 tx_ant, u32 rx_ant)
2813{
2814 int ret;
2815
2816 lockdep_assert_held(&ar->conf_mutex);
2817
Ben Greear5572a952014-11-24 16:22:10 +02002818 ath10k_check_chain_mask(ar, tx_ant, "tx");
2819 ath10k_check_chain_mask(ar, rx_ant, "rx");
2820
Ben Greear46acf7b2014-05-16 17:15:38 +03002821 ar->cfg_tx_chainmask = tx_ant;
2822 ar->cfg_rx_chainmask = rx_ant;
2823
2824 if ((ar->state != ATH10K_STATE_ON) &&
2825 (ar->state != ATH10K_STATE_RESTARTED))
2826 return 0;
2827
2828 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->tx_chain_mask,
2829 tx_ant);
2830 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002831 ath10k_warn(ar, "failed to set tx-chainmask: %d, req 0x%x\n",
Ben Greear46acf7b2014-05-16 17:15:38 +03002832 ret, tx_ant);
2833 return ret;
2834 }
2835
2836 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->rx_chain_mask,
2837 rx_ant);
2838 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002839 ath10k_warn(ar, "failed to set rx-chainmask: %d, req 0x%x\n",
Ben Greear46acf7b2014-05-16 17:15:38 +03002840 ret, rx_ant);
2841 return ret;
2842 }
2843
2844 return 0;
2845}
2846
2847static int ath10k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
2848{
2849 struct ath10k *ar = hw->priv;
2850 int ret;
2851
2852 mutex_lock(&ar->conf_mutex);
2853 ret = __ath10k_set_antenna(ar, tx_ant, rx_ant);
2854 mutex_unlock(&ar->conf_mutex);
2855 return ret;
2856}
2857
Kalle Valo5e3dd152013-06-12 20:52:10 +03002858static int ath10k_start(struct ieee80211_hw *hw)
2859{
2860 struct ath10k *ar = hw->priv;
Michal Kazior818bdd12013-07-16 09:38:57 +02002861 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002862
Michal Kaziorbca7baf2014-05-26 12:46:03 +03002863 /*
2864 * This makes sense only when restarting hw. It is harmless to call
2865 * uncoditionally. This is necessary to make sure no HTT/WMI tx
2866 * commands will be submitted while restarting.
2867 */
2868 ath10k_drain_tx(ar);
2869
Michal Kazior548db542013-07-05 16:15:15 +03002870 mutex_lock(&ar->conf_mutex);
2871
Michal Kaziorc5058f52014-05-26 12:46:03 +03002872 switch (ar->state) {
2873 case ATH10K_STATE_OFF:
2874 ar->state = ATH10K_STATE_ON;
2875 break;
2876 case ATH10K_STATE_RESTARTING:
2877 ath10k_halt(ar);
2878 ar->state = ATH10K_STATE_RESTARTED;
2879 break;
2880 case ATH10K_STATE_ON:
2881 case ATH10K_STATE_RESTARTED:
2882 case ATH10K_STATE_WEDGED:
2883 WARN_ON(1);
Michal Kazior818bdd12013-07-16 09:38:57 +02002884 ret = -EINVAL;
Michal Kaziorae254432014-05-26 12:46:02 +03002885 goto err;
Kalle Valo43d2a302014-09-10 18:23:30 +03002886 case ATH10K_STATE_UTF:
2887 ret = -EBUSY;
2888 goto err;
Michal Kazior818bdd12013-07-16 09:38:57 +02002889 }
2890
2891 ret = ath10k_hif_power_up(ar);
2892 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002893 ath10k_err(ar, "Could not init hif: %d\n", ret);
Michal Kaziorae254432014-05-26 12:46:02 +03002894 goto err_off;
Michal Kazior818bdd12013-07-16 09:38:57 +02002895 }
2896
Kalle Valo43d2a302014-09-10 18:23:30 +03002897 ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_NORMAL);
Michal Kazior818bdd12013-07-16 09:38:57 +02002898 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002899 ath10k_err(ar, "Could not init core: %d\n", ret);
Michal Kaziorae254432014-05-26 12:46:02 +03002900 goto err_power_down;
Michal Kazior818bdd12013-07-16 09:38:57 +02002901 }
2902
Bartosz Markowski226a3392013-09-26 17:47:16 +02002903 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->pmf_qos, 1);
Michal Kaziorae254432014-05-26 12:46:02 +03002904 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002905 ath10k_warn(ar, "failed to enable PMF QOS: %d\n", ret);
Michal Kaziorae254432014-05-26 12:46:02 +03002906 goto err_core_stop;
2907 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002908
Michal Kaziorc4dd0d02013-11-13 11:05:10 +01002909 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->dynamic_bw, 1);
Michal Kaziorae254432014-05-26 12:46:02 +03002910 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002911 ath10k_warn(ar, "failed to enable dynamic BW: %d\n", ret);
Michal Kaziorae254432014-05-26 12:46:02 +03002912 goto err_core_stop;
2913 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002914
Ben Greear46acf7b2014-05-16 17:15:38 +03002915 if (ar->cfg_tx_chainmask)
2916 __ath10k_set_antenna(ar, ar->cfg_tx_chainmask,
2917 ar->cfg_rx_chainmask);
2918
Marek Puzyniakab6258e2014-01-29 15:03:31 +02002919 /*
2920 * By default FW set ARP frames ac to voice (6). In that case ARP
2921 * exchange is not working properly for UAPSD enabled AP. ARP requests
2922 * which arrives with access category 0 are processed by network stack
2923 * and send back with access category 0, but FW changes access category
2924 * to 6. Set ARP frames access category to best effort (0) solves
2925 * this problem.
2926 */
2927
2928 ret = ath10k_wmi_pdev_set_param(ar,
2929 ar->wmi.pdev_param->arp_ac_override, 0);
2930 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002931 ath10k_warn(ar, "failed to set arp ac override parameter: %d\n",
Marek Puzyniakab6258e2014-01-29 15:03:31 +02002932 ret);
Michal Kaziorae254432014-05-26 12:46:02 +03002933 goto err_core_stop;
Marek Puzyniakab6258e2014-01-29 15:03:31 +02002934 }
2935
Michal Kaziord6500972014-04-08 09:56:09 +03002936 ar->num_started_vdevs = 0;
Michal Kaziorf7843d72013-07-16 09:38:52 +02002937 ath10k_regd_update(ar);
2938
Simon Wunderlich855aed12014-08-02 09:12:54 +03002939 ath10k_spectral_start(ar);
2940
Michal Kaziorae254432014-05-26 12:46:02 +03002941 mutex_unlock(&ar->conf_mutex);
2942 return 0;
2943
2944err_core_stop:
2945 ath10k_core_stop(ar);
2946
2947err_power_down:
2948 ath10k_hif_power_down(ar);
2949
2950err_off:
2951 ar->state = ATH10K_STATE_OFF;
2952
2953err:
Michal Kazior548db542013-07-05 16:15:15 +03002954 mutex_unlock(&ar->conf_mutex);
Michal Kaziorc60bdd82014-01-29 07:26:31 +01002955 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002956}
2957
2958static void ath10k_stop(struct ieee80211_hw *hw)
2959{
2960 struct ath10k *ar = hw->priv;
2961
Michal Kaziorbca7baf2014-05-26 12:46:03 +03002962 ath10k_drain_tx(ar);
2963
Michal Kazior548db542013-07-05 16:15:15 +03002964 mutex_lock(&ar->conf_mutex);
Michal Kaziorc5058f52014-05-26 12:46:03 +03002965 if (ar->state != ATH10K_STATE_OFF) {
Michal Kazior818bdd12013-07-16 09:38:57 +02002966 ath10k_halt(ar);
Michal Kaziorc5058f52014-05-26 12:46:03 +03002967 ar->state = ATH10K_STATE_OFF;
2968 }
Michal Kazior548db542013-07-05 16:15:15 +03002969 mutex_unlock(&ar->conf_mutex);
2970
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002971 cancel_delayed_work_sync(&ar->scan.timeout);
Michal Kazioraffd3212013-07-16 09:54:35 +02002972 cancel_work_sync(&ar->restart_work);
2973}
2974
Michal Kaziorad088bf2013-10-16 15:44:46 +03002975static int ath10k_config_ps(struct ath10k *ar)
Michal Kazioraffd3212013-07-16 09:54:35 +02002976{
Michal Kaziorad088bf2013-10-16 15:44:46 +03002977 struct ath10k_vif *arvif;
2978 int ret = 0;
Michal Kazioraffd3212013-07-16 09:54:35 +02002979
2980 lockdep_assert_held(&ar->conf_mutex);
2981
Michal Kaziorad088bf2013-10-16 15:44:46 +03002982 list_for_each_entry(arvif, &ar->arvifs, list) {
2983 ret = ath10k_mac_vif_setup_ps(arvif);
2984 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002985 ath10k_warn(ar, "failed to setup powersave: %d\n", ret);
Michal Kaziorad088bf2013-10-16 15:44:46 +03002986 break;
2987 }
2988 }
Michal Kazioraffd3212013-07-16 09:54:35 +02002989
Michal Kaziorad088bf2013-10-16 15:44:46 +03002990 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002991}
2992
Michal Kaziorc930f742014-01-23 11:38:25 +01002993static const char *chandef_get_width(enum nl80211_chan_width width)
2994{
2995 switch (width) {
2996 case NL80211_CHAN_WIDTH_20_NOHT:
2997 return "20 (noht)";
2998 case NL80211_CHAN_WIDTH_20:
2999 return "20";
3000 case NL80211_CHAN_WIDTH_40:
3001 return "40";
3002 case NL80211_CHAN_WIDTH_80:
3003 return "80";
3004 case NL80211_CHAN_WIDTH_80P80:
3005 return "80+80";
3006 case NL80211_CHAN_WIDTH_160:
3007 return "160";
3008 case NL80211_CHAN_WIDTH_5:
3009 return "5";
3010 case NL80211_CHAN_WIDTH_10:
3011 return "10";
3012 }
3013 return "?";
3014}
3015
3016static void ath10k_config_chan(struct ath10k *ar)
3017{
3018 struct ath10k_vif *arvif;
Michal Kaziorc930f742014-01-23 11:38:25 +01003019 int ret;
3020
3021 lockdep_assert_held(&ar->conf_mutex);
3022
Michal Kazior7aa7a722014-08-25 12:09:38 +02003023 ath10k_dbg(ar, ATH10K_DBG_MAC,
Michal Kaziorc930f742014-01-23 11:38:25 +01003024 "mac config channel to %dMHz (cf1 %dMHz cf2 %dMHz width %s)\n",
3025 ar->chandef.chan->center_freq,
3026 ar->chandef.center_freq1,
3027 ar->chandef.center_freq2,
3028 chandef_get_width(ar->chandef.width));
3029
3030 /* First stop monitor interface. Some FW versions crash if there's a
3031 * lone monitor interface. */
Michal Kazior1bbc0972014-04-08 09:45:47 +03003032 if (ar->monitor_started)
Michal Kazior19337472014-08-28 12:58:16 +02003033 ath10k_monitor_stop(ar);
Michal Kaziorc930f742014-01-23 11:38:25 +01003034
3035 list_for_each_entry(arvif, &ar->arvifs, list) {
3036 if (!arvif->is_started)
3037 continue;
3038
Michal Kaziordc55e302014-07-29 12:53:36 +03003039 if (!arvif->is_up)
3040 continue;
3041
Michal Kaziorc930f742014-01-23 11:38:25 +01003042 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
3043 continue;
3044
Michal Kaziordc55e302014-07-29 12:53:36 +03003045 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
Michal Kaziorc930f742014-01-23 11:38:25 +01003046 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003047 ath10k_warn(ar, "failed to down vdev %d: %d\n",
Michal Kaziorc930f742014-01-23 11:38:25 +01003048 arvif->vdev_id, ret);
3049 continue;
3050 }
3051 }
3052
Michal Kaziordc55e302014-07-29 12:53:36 +03003053 /* all vdevs are downed now - attempt to restart and re-up them */
Michal Kaziorc930f742014-01-23 11:38:25 +01003054
3055 list_for_each_entry(arvif, &ar->arvifs, list) {
3056 if (!arvif->is_started)
3057 continue;
3058
3059 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
3060 continue;
3061
Michal Kaziordc55e302014-07-29 12:53:36 +03003062 ret = ath10k_vdev_restart(arvif);
Michal Kaziorc930f742014-01-23 11:38:25 +01003063 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003064 ath10k_warn(ar, "failed to restart vdev %d: %d\n",
Michal Kaziorc930f742014-01-23 11:38:25 +01003065 arvif->vdev_id, ret);
3066 continue;
3067 }
3068
3069 if (!arvif->is_up)
3070 continue;
3071
3072 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
3073 arvif->bssid);
3074 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003075 ath10k_warn(ar, "failed to bring vdev up %d: %d\n",
Michal Kaziorc930f742014-01-23 11:38:25 +01003076 arvif->vdev_id, ret);
3077 continue;
3078 }
3079 }
3080
Michal Kazior19337472014-08-28 12:58:16 +02003081 ath10k_monitor_recalc(ar);
Michal Kaziorc930f742014-01-23 11:38:25 +01003082}
3083
Michal Kazior7d9d5582014-10-21 10:40:15 +03003084static int ath10k_mac_txpower_setup(struct ath10k *ar, int txpower)
3085{
3086 int ret;
3087 u32 param;
3088
3089 lockdep_assert_held(&ar->conf_mutex);
3090
3091 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac txpower %d\n", txpower);
3092
3093 param = ar->wmi.pdev_param->txpower_limit2g;
3094 ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
3095 if (ret) {
3096 ath10k_warn(ar, "failed to set 2g txpower %d: %d\n",
3097 txpower, ret);
3098 return ret;
3099 }
3100
3101 param = ar->wmi.pdev_param->txpower_limit5g;
3102 ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
3103 if (ret) {
3104 ath10k_warn(ar, "failed to set 5g txpower %d: %d\n",
3105 txpower, ret);
3106 return ret;
3107 }
3108
3109 return 0;
3110}
3111
3112static int ath10k_mac_txpower_recalc(struct ath10k *ar)
3113{
3114 struct ath10k_vif *arvif;
3115 int ret, txpower = -1;
3116
3117 lockdep_assert_held(&ar->conf_mutex);
3118
3119 list_for_each_entry(arvif, &ar->arvifs, list) {
3120 WARN_ON(arvif->txpower < 0);
3121
3122 if (txpower == -1)
3123 txpower = arvif->txpower;
3124 else
3125 txpower = min(txpower, arvif->txpower);
3126 }
3127
3128 if (WARN_ON(txpower == -1))
3129 return -EINVAL;
3130
3131 ret = ath10k_mac_txpower_setup(ar, txpower);
3132 if (ret) {
3133 ath10k_warn(ar, "failed to setup tx power %d: %d\n",
3134 txpower, ret);
3135 return ret;
3136 }
3137
3138 return 0;
3139}
3140
Kalle Valo5e3dd152013-06-12 20:52:10 +03003141static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
3142{
Kalle Valo5e3dd152013-06-12 20:52:10 +03003143 struct ath10k *ar = hw->priv;
3144 struct ieee80211_conf *conf = &hw->conf;
3145 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003146
3147 mutex_lock(&ar->conf_mutex);
3148
3149 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003150 ath10k_dbg(ar, ATH10K_DBG_MAC,
Michal Kaziord6500972014-04-08 09:56:09 +03003151 "mac config channel %dMHz flags 0x%x radar %d\n",
Marek Puzyniake8a50f82013-11-20 09:59:47 +02003152 conf->chandef.chan->center_freq,
Michal Kaziord6500972014-04-08 09:56:09 +03003153 conf->chandef.chan->flags,
3154 conf->radar_enabled);
Marek Puzyniake8a50f82013-11-20 09:59:47 +02003155
Kalle Valo5e3dd152013-06-12 20:52:10 +03003156 spin_lock_bh(&ar->data_lock);
3157 ar->rx_channel = conf->chandef.chan;
3158 spin_unlock_bh(&ar->data_lock);
Marek Puzyniake8a50f82013-11-20 09:59:47 +02003159
Michal Kaziord6500972014-04-08 09:56:09 +03003160 ar->radar_enabled = conf->radar_enabled;
3161 ath10k_recalc_radar_detection(ar);
Michal Kaziorc930f742014-01-23 11:38:25 +01003162
3163 if (!cfg80211_chandef_identical(&ar->chandef, &conf->chandef)) {
3164 ar->chandef = conf->chandef;
3165 ath10k_config_chan(ar);
3166 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003167 }
3168
Michal Kazioraffd3212013-07-16 09:54:35 +02003169 if (changed & IEEE80211_CONF_CHANGE_PS)
3170 ath10k_config_ps(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003171
3172 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
Michal Kazior19337472014-08-28 12:58:16 +02003173 ar->monitor = conf->flags & IEEE80211_CONF_MONITOR;
3174 ret = ath10k_monitor_recalc(ar);
3175 if (ret)
3176 ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003177 }
3178
3179 mutex_unlock(&ar->conf_mutex);
3180 return ret;
3181}
3182
Ben Greear5572a952014-11-24 16:22:10 +02003183static u32 get_nss_from_chainmask(u16 chain_mask)
3184{
3185 if ((chain_mask & 0x15) == 0x15)
3186 return 4;
3187 else if ((chain_mask & 0x7) == 0x7)
3188 return 3;
3189 else if ((chain_mask & 0x3) == 0x3)
3190 return 2;
3191 return 1;
3192}
3193
Kalle Valo5e3dd152013-06-12 20:52:10 +03003194/*
3195 * TODO:
3196 * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
3197 * because we will send mgmt frames without CCK. This requirement
3198 * for P2P_FIND/GO_NEG should be handled by checking CCK flag
3199 * in the TX packet.
3200 */
3201static int ath10k_add_interface(struct ieee80211_hw *hw,
3202 struct ieee80211_vif *vif)
3203{
3204 struct ath10k *ar = hw->priv;
3205 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3206 enum wmi_sta_powersave_param param;
3207 int ret = 0;
Kalle Valo5a13e762014-01-20 11:01:46 +02003208 u32 value;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003209 int bit;
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003210 u32 vdev_param;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003211
Johannes Berg848955c2014-11-11 12:48:42 +01003212 vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
3213
Kalle Valo5e3dd152013-06-12 20:52:10 +03003214 mutex_lock(&ar->conf_mutex);
3215
Michal Kazior0dbd09e2013-07-31 10:55:14 +02003216 memset(arvif, 0, sizeof(*arvif));
3217
Kalle Valo5e3dd152013-06-12 20:52:10 +03003218 arvif->ar = ar;
3219 arvif->vif = vif;
3220
Ben Greeare63b33f2013-10-22 14:54:14 -07003221 INIT_LIST_HEAD(&arvif->list);
Michal Kaziorcc4827b2013-10-16 15:44:45 +03003222
Ben Greeara9aefb32014-08-12 11:02:19 +03003223 if (ar->free_vdev_map == 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003224 ath10k_warn(ar, "Free vdev map is empty, no more interfaces allowed.\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03003225 ret = -EBUSY;
Michal Kazior9dad14a2013-10-16 15:44:45 +03003226 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003227 }
Ben Greear16c11172014-09-23 14:17:16 -07003228 bit = __ffs64(ar->free_vdev_map);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003229
Ben Greear16c11172014-09-23 14:17:16 -07003230 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac create vdev %i map %llx\n",
3231 bit, ar->free_vdev_map);
3232
3233 arvif->vdev_id = bit;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003234 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003235
Kalle Valo5e3dd152013-06-12 20:52:10 +03003236 switch (vif->type) {
Michal Kazior75d2bd42014-12-12 12:41:39 +01003237 case NL80211_IFTYPE_P2P_DEVICE:
3238 arvif->vdev_type = WMI_VDEV_TYPE_STA;
3239 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
3240 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003241 case NL80211_IFTYPE_UNSPECIFIED:
3242 case NL80211_IFTYPE_STATION:
3243 arvif->vdev_type = WMI_VDEV_TYPE_STA;
3244 if (vif->p2p)
3245 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
3246 break;
3247 case NL80211_IFTYPE_ADHOC:
3248 arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
3249 break;
3250 case NL80211_IFTYPE_AP:
3251 arvif->vdev_type = WMI_VDEV_TYPE_AP;
3252
3253 if (vif->p2p)
3254 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
3255 break;
3256 case NL80211_IFTYPE_MONITOR:
3257 arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
3258 break;
3259 default:
3260 WARN_ON(1);
3261 break;
3262 }
3263
Michal Kazior64badcb2014-09-18 11:18:02 +03003264 /* Some firmware revisions don't wait for beacon tx completion before
3265 * sending another SWBA event. This could lead to hardware using old
3266 * (freed) beacon data in some cases, e.g. tx credit starvation
3267 * combined with missed TBTT. This is very very rare.
3268 *
3269 * On non-IOMMU-enabled hosts this could be a possible security issue
3270 * because hw could beacon some random data on the air. On
3271 * IOMMU-enabled hosts DMAR faults would occur in most cases and target
3272 * device would crash.
3273 *
3274 * Since there are no beacon tx completions (implicit nor explicit)
3275 * propagated to host the only workaround for this is to allocate a
3276 * DMA-coherent buffer for a lifetime of a vif and use it for all
3277 * beacon tx commands. Worst case for this approach is some beacons may
3278 * become corrupted, e.g. have garbled IEs or out-of-date TIM bitmap.
3279 */
3280 if (vif->type == NL80211_IFTYPE_ADHOC ||
3281 vif->type == NL80211_IFTYPE_AP) {
3282 arvif->beacon_buf = dma_zalloc_coherent(ar->dev,
3283 IEEE80211_MAX_FRAME_LEN,
3284 &arvif->beacon_paddr,
Rajkumar Manoharan82d7aba2014-10-10 17:38:27 +05303285 GFP_ATOMIC);
Michal Kazior64badcb2014-09-18 11:18:02 +03003286 if (!arvif->beacon_buf) {
3287 ret = -ENOMEM;
3288 ath10k_warn(ar, "failed to allocate beacon buffer: %d\n",
3289 ret);
3290 goto err;
3291 }
3292 }
3293
3294 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d bcnmode %s\n",
3295 arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
3296 arvif->beacon_buf ? "single-buf" : "per-skb");
Kalle Valo5e3dd152013-06-12 20:52:10 +03003297
3298 ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
3299 arvif->vdev_subtype, vif->addr);
3300 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003301 ath10k_warn(ar, "failed to create WMI vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003302 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003303 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003304 }
3305
Ben Greear16c11172014-09-23 14:17:16 -07003306 ar->free_vdev_map &= ~(1LL << arvif->vdev_id);
Michal Kazior05791192013-10-16 15:44:45 +03003307 list_add(&arvif->list, &ar->arvifs);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003308
Michal Kazior46725b152015-01-28 09:57:49 +02003309 /* It makes no sense to have firmware do keepalives. mac80211 already
3310 * takes care of this with idle connection polling.
3311 */
3312 ret = ath10k_mac_vif_disable_keepalive(arvif);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003313 if (ret) {
Michal Kazior46725b152015-01-28 09:57:49 +02003314 ath10k_warn(ar, "failed to disable keepalive on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003315 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003316 goto err_vdev_delete;
3317 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003318
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +02003319 arvif->def_wep_key_idx = -1;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003320
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003321 vdev_param = ar->wmi.vdev_param->tx_encap_type;
3322 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003323 ATH10K_HW_TXRX_NATIVE_WIFI);
Bartosz Markowskiebc9abd2013-10-15 09:26:20 +02003324 /* 10.X firmware does not support this VDEV parameter. Do not warn */
Michal Kazior9dad14a2013-10-16 15:44:45 +03003325 if (ret && ret != -EOPNOTSUPP) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003326 ath10k_warn(ar, "failed to set vdev %i TX encapsulation: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003327 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003328 goto err_vdev_delete;
3329 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003330
Ben Greear5572a952014-11-24 16:22:10 +02003331 if (ar->cfg_tx_chainmask) {
3332 u16 nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
3333
3334 vdev_param = ar->wmi.vdev_param->nss;
3335 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3336 nss);
3337 if (ret) {
3338 ath10k_warn(ar, "failed to set vdev %i chainmask 0x%x, nss %i: %d\n",
3339 arvif->vdev_id, ar->cfg_tx_chainmask, nss,
3340 ret);
3341 goto err_vdev_delete;
3342 }
3343 }
3344
Kalle Valo5e3dd152013-06-12 20:52:10 +03003345 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3346 ret = ath10k_peer_create(ar, arvif->vdev_id, vif->addr);
3347 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003348 ath10k_warn(ar, "failed to create vdev %i peer for AP: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003349 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003350 goto err_vdev_delete;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003351 }
Marek Puzyniakcdf07402013-12-30 09:07:51 +01003352
Kalle Valo5a13e762014-01-20 11:01:46 +02003353 ret = ath10k_mac_set_kickout(arvif);
3354 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003355 ath10k_warn(ar, "failed to set vdev %i kickout parameters: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003356 arvif->vdev_id, ret);
Kalle Valo5a13e762014-01-20 11:01:46 +02003357 goto err_peer_delete;
3358 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003359 }
3360
3361 if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
3362 param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
3363 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
3364 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3365 param, value);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003366 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003367 ath10k_warn(ar, "failed to set vdev %i RX wake policy: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003368 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003369 goto err_peer_delete;
3370 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003371
Michal Kazior9f9b5742014-12-12 12:41:36 +01003372 ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003373 if (ret) {
Michal Kazior9f9b5742014-12-12 12:41:36 +01003374 ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003375 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003376 goto err_peer_delete;
3377 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003378
Michal Kazior9f9b5742014-12-12 12:41:36 +01003379 ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003380 if (ret) {
Michal Kazior9f9b5742014-12-12 12:41:36 +01003381 ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003382 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003383 goto err_peer_delete;
3384 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003385 }
3386
Michal Kazior424121c2013-07-22 14:13:31 +02003387 ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003388 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003389 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
Michal Kazior679c54a2013-07-05 16:15:04 +03003390 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003391 goto err_peer_delete;
3392 }
Michal Kazior679c54a2013-07-05 16:15:04 +03003393
Michal Kazior424121c2013-07-22 14:13:31 +02003394 ret = ath10k_mac_set_frag(arvif, ar->hw->wiphy->frag_threshold);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003395 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003396 ath10k_warn(ar, "failed to set frag threshold for vdev %d: %d\n",
Michal Kazior679c54a2013-07-05 16:15:04 +03003397 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003398 goto err_peer_delete;
3399 }
Michal Kazior679c54a2013-07-05 16:15:04 +03003400
Michal Kazior7d9d5582014-10-21 10:40:15 +03003401 arvif->txpower = vif->bss_conf.txpower;
3402 ret = ath10k_mac_txpower_recalc(ar);
3403 if (ret) {
3404 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
3405 goto err_peer_delete;
3406 }
3407
Kalle Valo5e3dd152013-06-12 20:52:10 +03003408 mutex_unlock(&ar->conf_mutex);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003409 return 0;
3410
3411err_peer_delete:
3412 if (arvif->vdev_type == WMI_VDEV_TYPE_AP)
3413 ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
3414
3415err_vdev_delete:
3416 ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
Ben Greear16c11172014-09-23 14:17:16 -07003417 ar->free_vdev_map |= 1LL << arvif->vdev_id;
Michal Kazior05791192013-10-16 15:44:45 +03003418 list_del(&arvif->list);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003419
3420err:
Michal Kazior64badcb2014-09-18 11:18:02 +03003421 if (arvif->beacon_buf) {
3422 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
3423 arvif->beacon_buf, arvif->beacon_paddr);
3424 arvif->beacon_buf = NULL;
3425 }
3426
Michal Kazior9dad14a2013-10-16 15:44:45 +03003427 mutex_unlock(&ar->conf_mutex);
3428
Kalle Valo5e3dd152013-06-12 20:52:10 +03003429 return ret;
3430}
3431
3432static void ath10k_remove_interface(struct ieee80211_hw *hw,
3433 struct ieee80211_vif *vif)
3434{
3435 struct ath10k *ar = hw->priv;
3436 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3437 int ret;
3438
Sujith Manoharan5d011f52014-11-25 11:47:00 +05303439 mutex_lock(&ar->conf_mutex);
3440
Michal Kaziored543882013-09-13 14:16:56 +02003441 spin_lock_bh(&ar->data_lock);
Michal Kazior64badcb2014-09-18 11:18:02 +03003442 ath10k_mac_vif_beacon_cleanup(arvif);
Michal Kaziored543882013-09-13 14:16:56 +02003443 spin_unlock_bh(&ar->data_lock);
3444
Simon Wunderlich855aed12014-08-02 09:12:54 +03003445 ret = ath10k_spectral_vif_stop(arvif);
3446 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003447 ath10k_warn(ar, "failed to stop spectral for vdev %i: %d\n",
Simon Wunderlich855aed12014-08-02 09:12:54 +03003448 arvif->vdev_id, ret);
3449
Ben Greear16c11172014-09-23 14:17:16 -07003450 ar->free_vdev_map |= 1LL << arvif->vdev_id;
Michal Kazior05791192013-10-16 15:44:45 +03003451 list_del(&arvif->list);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003452
3453 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
Michal Kazior2c512052015-02-15 16:50:40 +02003454 ret = ath10k_wmi_peer_delete(arvif->ar, arvif->vdev_id,
3455 vif->addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003456 if (ret)
Michal Kazior2c512052015-02-15 16:50:40 +02003457 ath10k_warn(ar, "failed to submit AP self-peer removal on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003458 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003459
3460 kfree(arvif->u.ap.noa_data);
3461 }
3462
Michal Kazior7aa7a722014-08-25 12:09:38 +02003463 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i delete (remove interface)\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03003464 arvif->vdev_id);
3465
Kalle Valo5e3dd152013-06-12 20:52:10 +03003466 ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
3467 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003468 ath10k_warn(ar, "failed to delete WMI vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003469 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003470
Michal Kazior2c512052015-02-15 16:50:40 +02003471 /* Some firmware revisions don't notify host about self-peer removal
3472 * until after associated vdev is deleted.
3473 */
3474 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3475 ret = ath10k_wait_for_peer_deleted(ar, arvif->vdev_id,
3476 vif->addr);
3477 if (ret)
3478 ath10k_warn(ar, "failed to remove AP self-peer on vdev %i: %d\n",
3479 arvif->vdev_id, ret);
3480
3481 spin_lock_bh(&ar->data_lock);
3482 ar->num_peers--;
3483 spin_unlock_bh(&ar->data_lock);
3484 }
3485
Kalle Valo5e3dd152013-06-12 20:52:10 +03003486 ath10k_peer_cleanup(ar, arvif->vdev_id);
3487
3488 mutex_unlock(&ar->conf_mutex);
3489}
3490
3491/*
3492 * FIXME: Has to be verified.
3493 */
3494#define SUPPORTED_FILTERS \
Johannes Bergdf140462015-04-22 14:40:58 +02003495 (FIF_ALLMULTI | \
Kalle Valo5e3dd152013-06-12 20:52:10 +03003496 FIF_CONTROL | \
3497 FIF_PSPOLL | \
3498 FIF_OTHER_BSS | \
3499 FIF_BCN_PRBRESP_PROMISC | \
3500 FIF_PROBE_REQ | \
3501 FIF_FCSFAIL)
3502
3503static void ath10k_configure_filter(struct ieee80211_hw *hw,
3504 unsigned int changed_flags,
3505 unsigned int *total_flags,
3506 u64 multicast)
3507{
3508 struct ath10k *ar = hw->priv;
3509 int ret;
3510
3511 mutex_lock(&ar->conf_mutex);
3512
3513 changed_flags &= SUPPORTED_FILTERS;
3514 *total_flags &= SUPPORTED_FILTERS;
3515 ar->filter_flags = *total_flags;
3516
Michal Kazior19337472014-08-28 12:58:16 +02003517 ret = ath10k_monitor_recalc(ar);
3518 if (ret)
3519 ath10k_warn(ar, "failed to recalc montior: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003520
3521 mutex_unlock(&ar->conf_mutex);
3522}
3523
3524static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
3525 struct ieee80211_vif *vif,
3526 struct ieee80211_bss_conf *info,
3527 u32 changed)
3528{
3529 struct ath10k *ar = hw->priv;
3530 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3531 int ret = 0;
Kalle Valoaf762c02014-09-14 12:50:17 +03003532 u32 vdev_param, pdev_param, slottime, preamble;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003533
3534 mutex_lock(&ar->conf_mutex);
3535
3536 if (changed & BSS_CHANGED_IBSS)
3537 ath10k_control_ibss(arvif, info, vif->addr);
3538
3539 if (changed & BSS_CHANGED_BEACON_INT) {
3540 arvif->beacon_interval = info->beacon_int;
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003541 vdev_param = ar->wmi.vdev_param->beacon_interval;
3542 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003543 arvif->beacon_interval);
Michal Kazior7aa7a722014-08-25 12:09:38 +02003544 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003545 "mac vdev %d beacon_interval %d\n",
3546 arvif->vdev_id, arvif->beacon_interval);
3547
Kalle Valo5e3dd152013-06-12 20:52:10 +03003548 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003549 ath10k_warn(ar, "failed to set beacon interval for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003550 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003551 }
3552
3553 if (changed & BSS_CHANGED_BEACON) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003554 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003555 "vdev %d set beacon tx mode to staggered\n",
3556 arvif->vdev_id);
3557
Bartosz Markowski226a3392013-09-26 17:47:16 +02003558 pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
3559 ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003560 WMI_BEACON_STAGGERED_MODE);
3561 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003562 ath10k_warn(ar, "failed to set beacon mode for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003563 arvif->vdev_id, ret);
Michal Kaziorfbb8f1b2015-01-13 16:30:12 +02003564
3565 ret = ath10k_mac_setup_bcn_tmpl(arvif);
3566 if (ret)
3567 ath10k_warn(ar, "failed to update beacon template: %d\n",
3568 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003569 }
3570
Michal Kaziorfbb8f1b2015-01-13 16:30:12 +02003571 if (changed & BSS_CHANGED_AP_PROBE_RESP) {
3572 ret = ath10k_mac_setup_prb_tmpl(arvif);
3573 if (ret)
3574 ath10k_warn(ar, "failed to setup probe resp template on vdev %i: %d\n",
3575 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003576 }
3577
Michal Kaziorba2479f2015-01-24 12:14:51 +02003578 if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03003579 arvif->dtim_period = info->dtim_period;
3580
Michal Kazior7aa7a722014-08-25 12:09:38 +02003581 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003582 "mac vdev %d dtim_period %d\n",
3583 arvif->vdev_id, arvif->dtim_period);
3584
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003585 vdev_param = ar->wmi.vdev_param->dtim_period;
3586 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003587 arvif->dtim_period);
3588 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003589 ath10k_warn(ar, "failed to set dtim period for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003590 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003591 }
3592
3593 if (changed & BSS_CHANGED_SSID &&
3594 vif->type == NL80211_IFTYPE_AP) {
3595 arvif->u.ap.ssid_len = info->ssid_len;
3596 if (info->ssid_len)
3597 memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
3598 arvif->u.ap.hidden_ssid = info->hidden_ssid;
3599 }
3600
Michal Kazior077efc82014-10-21 10:10:29 +03003601 if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
3602 ether_addr_copy(arvif->bssid, info->bssid);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003603
3604 if (changed & BSS_CHANGED_BEACON_ENABLED)
3605 ath10k_control_beaconing(arvif, info);
3606
3607 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02003608 arvif->use_cts_prot = info->use_cts_prot;
Michal Kazior7aa7a722014-08-25 12:09:38 +02003609 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d cts_prot %d\n",
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02003610 arvif->vdev_id, info->use_cts_prot);
Kalle Valo60c3daa2013-09-08 17:56:07 +03003611
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02003612 ret = ath10k_recalc_rtscts_prot(arvif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003613 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003614 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003615 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003616 }
3617
3618 if (changed & BSS_CHANGED_ERP_SLOT) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03003619 if (info->use_short_slot)
3620 slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
3621
3622 else
3623 slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
3624
Michal Kazior7aa7a722014-08-25 12:09:38 +02003625 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03003626 arvif->vdev_id, slottime);
3627
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003628 vdev_param = ar->wmi.vdev_param->slot_time;
3629 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003630 slottime);
3631 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003632 ath10k_warn(ar, "failed to set erp slot for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003633 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003634 }
3635
3636 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03003637 if (info->use_short_preamble)
3638 preamble = WMI_VDEV_PREAMBLE_SHORT;
3639 else
3640 preamble = WMI_VDEV_PREAMBLE_LONG;
3641
Michal Kazior7aa7a722014-08-25 12:09:38 +02003642 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003643 "mac vdev %d preamble %dn",
3644 arvif->vdev_id, preamble);
3645
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003646 vdev_param = ar->wmi.vdev_param->preamble;
3647 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003648 preamble);
3649 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003650 ath10k_warn(ar, "failed to set preamble for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003651 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003652 }
3653
3654 if (changed & BSS_CHANGED_ASSOC) {
Michal Kaziore556f112014-08-28 12:58:17 +02003655 if (info->assoc) {
3656 /* Workaround: Make sure monitor vdev is not running
3657 * when associating to prevent some firmware revisions
3658 * (e.g. 10.1 and 10.2) from crashing.
3659 */
3660 if (ar->monitor_started)
3661 ath10k_monitor_stop(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003662 ath10k_bss_assoc(hw, vif, info);
Michal Kaziore556f112014-08-28 12:58:17 +02003663 ath10k_monitor_recalc(ar);
Michal Kazior077efc82014-10-21 10:10:29 +03003664 } else {
3665 ath10k_bss_disassoc(hw, vif);
Michal Kaziore556f112014-08-28 12:58:17 +02003666 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003667 }
3668
Michal Kazior7d9d5582014-10-21 10:40:15 +03003669 if (changed & BSS_CHANGED_TXPOWER) {
3670 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev_id %i txpower %d\n",
3671 arvif->vdev_id, info->txpower);
3672
3673 arvif->txpower = info->txpower;
3674 ret = ath10k_mac_txpower_recalc(ar);
3675 if (ret)
3676 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
3677 }
3678
Michal Kaziorbf14e652014-12-12 12:41:38 +01003679 if (changed & BSS_CHANGED_PS) {
Michal Kaziorcffb41f2015-02-13 13:30:16 +01003680 arvif->ps = vif->bss_conf.ps;
3681
3682 ret = ath10k_config_ps(ar);
Michal Kaziorbf14e652014-12-12 12:41:38 +01003683 if (ret)
3684 ath10k_warn(ar, "failed to setup ps on vdev %i: %d\n",
3685 arvif->vdev_id, ret);
3686 }
3687
Kalle Valo5e3dd152013-06-12 20:52:10 +03003688 mutex_unlock(&ar->conf_mutex);
3689}
3690
3691static int ath10k_hw_scan(struct ieee80211_hw *hw,
3692 struct ieee80211_vif *vif,
David Spinadelc56ef672014-02-05 15:21:13 +02003693 struct ieee80211_scan_request *hw_req)
Kalle Valo5e3dd152013-06-12 20:52:10 +03003694{
3695 struct ath10k *ar = hw->priv;
3696 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
David Spinadelc56ef672014-02-05 15:21:13 +02003697 struct cfg80211_scan_request *req = &hw_req->req;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003698 struct wmi_start_scan_arg arg;
3699 int ret = 0;
3700 int i;
3701
3702 mutex_lock(&ar->conf_mutex);
3703
3704 spin_lock_bh(&ar->data_lock);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003705 switch (ar->scan.state) {
3706 case ATH10K_SCAN_IDLE:
3707 reinit_completion(&ar->scan.started);
3708 reinit_completion(&ar->scan.completed);
3709 ar->scan.state = ATH10K_SCAN_STARTING;
3710 ar->scan.is_roc = false;
3711 ar->scan.vdev_id = arvif->vdev_id;
3712 ret = 0;
3713 break;
3714 case ATH10K_SCAN_STARTING:
3715 case ATH10K_SCAN_RUNNING:
3716 case ATH10K_SCAN_ABORTING:
Kalle Valo5e3dd152013-06-12 20:52:10 +03003717 ret = -EBUSY;
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003718 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003719 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003720 spin_unlock_bh(&ar->data_lock);
3721
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003722 if (ret)
3723 goto exit;
3724
Kalle Valo5e3dd152013-06-12 20:52:10 +03003725 memset(&arg, 0, sizeof(arg));
3726 ath10k_wmi_start_scan_init(ar, &arg);
3727 arg.vdev_id = arvif->vdev_id;
3728 arg.scan_id = ATH10K_SCAN_ID;
3729
3730 if (!req->no_cck)
3731 arg.scan_ctrl_flags |= WMI_SCAN_ADD_CCK_RATES;
3732
3733 if (req->ie_len) {
3734 arg.ie_len = req->ie_len;
3735 memcpy(arg.ie, req->ie, arg.ie_len);
3736 }
3737
3738 if (req->n_ssids) {
3739 arg.n_ssids = req->n_ssids;
3740 for (i = 0; i < arg.n_ssids; i++) {
3741 arg.ssids[i].len = req->ssids[i].ssid_len;
3742 arg.ssids[i].ssid = req->ssids[i].ssid;
3743 }
Michal Kaziordcd4a562013-07-31 10:55:12 +02003744 } else {
3745 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003746 }
3747
3748 if (req->n_channels) {
3749 arg.n_channels = req->n_channels;
3750 for (i = 0; i < arg.n_channels; i++)
3751 arg.channels[i] = req->channels[i]->center_freq;
3752 }
3753
3754 ret = ath10k_start_scan(ar, &arg);
3755 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003756 ath10k_warn(ar, "failed to start hw scan: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003757 spin_lock_bh(&ar->data_lock);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003758 ar->scan.state = ATH10K_SCAN_IDLE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003759 spin_unlock_bh(&ar->data_lock);
3760 }
3761
3762exit:
3763 mutex_unlock(&ar->conf_mutex);
3764 return ret;
3765}
3766
3767static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
3768 struct ieee80211_vif *vif)
3769{
3770 struct ath10k *ar = hw->priv;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003771
3772 mutex_lock(&ar->conf_mutex);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003773 ath10k_scan_abort(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003774 mutex_unlock(&ar->conf_mutex);
Michal Kazior4eb2e162014-10-28 10:23:09 +01003775
3776 cancel_delayed_work_sync(&ar->scan.timeout);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003777}
3778
Michal Kaziorcfb27d22013-12-02 09:06:36 +01003779static void ath10k_set_key_h_def_keyidx(struct ath10k *ar,
3780 struct ath10k_vif *arvif,
3781 enum set_key_cmd cmd,
3782 struct ieee80211_key_conf *key)
3783{
3784 u32 vdev_param = arvif->ar->wmi.vdev_param->def_keyid;
3785 int ret;
3786
3787 /* 10.1 firmware branch requires default key index to be set to group
3788 * key index after installing it. Otherwise FW/HW Txes corrupted
3789 * frames with multi-vif APs. This is not required for main firmware
3790 * branch (e.g. 636).
3791 *
3792 * FIXME: This has been tested only in AP. It remains unknown if this
3793 * is required for multi-vif STA interfaces on 10.1 */
3794
3795 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
3796 return;
3797
3798 if (key->cipher == WLAN_CIPHER_SUITE_WEP40)
3799 return;
3800
3801 if (key->cipher == WLAN_CIPHER_SUITE_WEP104)
3802 return;
3803
3804 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3805 return;
3806
3807 if (cmd != SET_KEY)
3808 return;
3809
3810 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3811 key->keyidx);
3812 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003813 ath10k_warn(ar, "failed to set vdev %i group key as default key: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003814 arvif->vdev_id, ret);
Michal Kaziorcfb27d22013-12-02 09:06:36 +01003815}
3816
Kalle Valo5e3dd152013-06-12 20:52:10 +03003817static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
3818 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
3819 struct ieee80211_key_conf *key)
3820{
3821 struct ath10k *ar = hw->priv;
3822 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3823 struct ath10k_peer *peer;
3824 const u8 *peer_addr;
3825 bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3826 key->cipher == WLAN_CIPHER_SUITE_WEP104;
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +02003827 bool def_idx = false;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003828 int ret = 0;
3829
3830 if (key->keyidx > WMI_MAX_KEY_INDEX)
3831 return -ENOSPC;
3832
3833 mutex_lock(&ar->conf_mutex);
3834
3835 if (sta)
3836 peer_addr = sta->addr;
3837 else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
3838 peer_addr = vif->bss_conf.bssid;
3839 else
3840 peer_addr = vif->addr;
3841
3842 key->hw_key_idx = key->keyidx;
3843
3844 /* the peer should not disappear in mid-way (unless FW goes awry) since
3845 * we already hold conf_mutex. we just make sure its there now. */
3846 spin_lock_bh(&ar->data_lock);
3847 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
3848 spin_unlock_bh(&ar->data_lock);
3849
3850 if (!peer) {
3851 if (cmd == SET_KEY) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003852 ath10k_warn(ar, "failed to install key for non-existent peer %pM\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03003853 peer_addr);
3854 ret = -EOPNOTSUPP;
3855 goto exit;
3856 } else {
3857 /* if the peer doesn't exist there is no key to disable
3858 * anymore */
3859 goto exit;
3860 }
3861 }
3862
3863 if (is_wep) {
3864 if (cmd == SET_KEY)
3865 arvif->wep_keys[key->keyidx] = key;
3866 else
3867 arvif->wep_keys[key->keyidx] = NULL;
3868
3869 if (cmd == DISABLE_KEY)
3870 ath10k_clear_vdev_key(arvif, key);
3871 }
3872
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +02003873 /* set TX_USAGE flag for all the keys incase of dot1x-WEP. For
3874 * static WEP, do not set this flag for the keys whose key id
3875 * is greater than default key id.
3876 */
3877 if (arvif->def_wep_key_idx == -1)
3878 def_idx = true;
3879
3880 ret = ath10k_install_key(arvif, key, cmd, peer_addr, def_idx);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003881 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003882 ath10k_warn(ar, "failed to install key for vdev %i peer %pM: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003883 arvif->vdev_id, peer_addr, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003884 goto exit;
3885 }
3886
Michal Kaziorcfb27d22013-12-02 09:06:36 +01003887 ath10k_set_key_h_def_keyidx(ar, arvif, cmd, key);
3888
Kalle Valo5e3dd152013-06-12 20:52:10 +03003889 spin_lock_bh(&ar->data_lock);
3890 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
3891 if (peer && cmd == SET_KEY)
3892 peer->keys[key->keyidx] = key;
3893 else if (peer && cmd == DISABLE_KEY)
3894 peer->keys[key->keyidx] = NULL;
3895 else if (peer == NULL)
3896 /* impossible unless FW goes crazy */
Michal Kazior7aa7a722014-08-25 12:09:38 +02003897 ath10k_warn(ar, "Peer %pM disappeared!\n", peer_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003898 spin_unlock_bh(&ar->data_lock);
3899
3900exit:
3901 mutex_unlock(&ar->conf_mutex);
3902 return ret;
3903}
3904
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +02003905static void ath10k_set_default_unicast_key(struct ieee80211_hw *hw,
3906 struct ieee80211_vif *vif,
3907 int keyidx)
3908{
3909 struct ath10k *ar = hw->priv;
3910 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3911 int ret;
3912
3913 mutex_lock(&arvif->ar->conf_mutex);
3914
3915 if (arvif->ar->state != ATH10K_STATE_ON)
3916 goto unlock;
3917
3918 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
3919 arvif->vdev_id, keyidx);
3920
3921 ret = ath10k_wmi_vdev_set_param(arvif->ar,
3922 arvif->vdev_id,
3923 arvif->ar->wmi.vdev_param->def_keyid,
3924 keyidx);
3925
3926 if (ret) {
3927 ath10k_warn(ar, "failed to update wep key index for vdev %d: %d\n",
3928 arvif->vdev_id,
3929 ret);
3930 goto unlock;
3931 }
3932
3933 arvif->def_wep_key_idx = keyidx;
3934unlock:
3935 mutex_unlock(&arvif->ar->conf_mutex);
3936}
3937
Michal Kazior9797feb2014-02-14 14:49:48 +01003938static void ath10k_sta_rc_update_wk(struct work_struct *wk)
3939{
3940 struct ath10k *ar;
3941 struct ath10k_vif *arvif;
3942 struct ath10k_sta *arsta;
3943 struct ieee80211_sta *sta;
3944 u32 changed, bw, nss, smps;
3945 int err;
3946
3947 arsta = container_of(wk, struct ath10k_sta, update_wk);
3948 sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
3949 arvif = arsta->arvif;
3950 ar = arvif->ar;
3951
3952 spin_lock_bh(&ar->data_lock);
3953
3954 changed = arsta->changed;
3955 arsta->changed = 0;
3956
3957 bw = arsta->bw;
3958 nss = arsta->nss;
3959 smps = arsta->smps;
3960
3961 spin_unlock_bh(&ar->data_lock);
3962
3963 mutex_lock(&ar->conf_mutex);
3964
3965 if (changed & IEEE80211_RC_BW_CHANGED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003966 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM peer bw %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003967 sta->addr, bw);
3968
3969 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
3970 WMI_PEER_CHAN_WIDTH, bw);
3971 if (err)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003972 ath10k_warn(ar, "failed to update STA %pM peer bw %d: %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003973 sta->addr, bw, err);
3974 }
3975
3976 if (changed & IEEE80211_RC_NSS_CHANGED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003977 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM nss %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003978 sta->addr, nss);
3979
3980 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
3981 WMI_PEER_NSS, nss);
3982 if (err)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003983 ath10k_warn(ar, "failed to update STA %pM nss %d: %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003984 sta->addr, nss, err);
3985 }
3986
3987 if (changed & IEEE80211_RC_SMPS_CHANGED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003988 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM smps %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003989 sta->addr, smps);
3990
3991 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
3992 WMI_PEER_SMPS_STATE, smps);
3993 if (err)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003994 ath10k_warn(ar, "failed to update STA %pM smps %d: %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003995 sta->addr, smps, err);
3996 }
3997
Janusz Dziedzic55884c02014-12-17 12:30:02 +02003998 if (changed & IEEE80211_RC_SUPP_RATES_CHANGED ||
3999 changed & IEEE80211_RC_NSS_CHANGED) {
4000 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM supp rates/nss\n",
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02004001 sta->addr);
4002
Michal Kazior590922a2014-10-21 10:10:29 +03004003 err = ath10k_station_assoc(ar, arvif->vif, sta, true);
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02004004 if (err)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004005 ath10k_warn(ar, "failed to reassociate station: %pM\n",
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02004006 sta->addr);
4007 }
4008
Michal Kazior9797feb2014-02-14 14:49:48 +01004009 mutex_unlock(&ar->conf_mutex);
4010}
4011
Michal Kaziorcfd10612014-11-25 15:16:05 +01004012static int ath10k_mac_inc_num_stations(struct ath10k_vif *arvif)
4013{
4014 struct ath10k *ar = arvif->ar;
4015
4016 lockdep_assert_held(&ar->conf_mutex);
4017
4018 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
4019 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
4020 return 0;
4021
4022 if (ar->num_stations >= ar->max_num_stations)
4023 return -ENOBUFS;
4024
4025 ar->num_stations++;
4026
4027 return 0;
4028}
4029
4030static void ath10k_mac_dec_num_stations(struct ath10k_vif *arvif)
4031{
4032 struct ath10k *ar = arvif->ar;
4033
4034 lockdep_assert_held(&ar->conf_mutex);
4035
4036 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
4037 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
4038 return;
4039
4040 ar->num_stations--;
4041}
4042
Kalle Valo5e3dd152013-06-12 20:52:10 +03004043static int ath10k_sta_state(struct ieee80211_hw *hw,
4044 struct ieee80211_vif *vif,
4045 struct ieee80211_sta *sta,
4046 enum ieee80211_sta_state old_state,
4047 enum ieee80211_sta_state new_state)
4048{
4049 struct ath10k *ar = hw->priv;
4050 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Michal Kazior9797feb2014-02-14 14:49:48 +01004051 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004052 int ret = 0;
4053
Michal Kazior76f90022014-02-25 09:29:57 +02004054 if (old_state == IEEE80211_STA_NOTEXIST &&
4055 new_state == IEEE80211_STA_NONE) {
4056 memset(arsta, 0, sizeof(*arsta));
4057 arsta->arvif = arvif;
4058 INIT_WORK(&arsta->update_wk, ath10k_sta_rc_update_wk);
4059 }
4060
Michal Kazior9797feb2014-02-14 14:49:48 +01004061 /* cancel must be done outside the mutex to avoid deadlock */
4062 if ((old_state == IEEE80211_STA_NONE &&
4063 new_state == IEEE80211_STA_NOTEXIST))
4064 cancel_work_sync(&arsta->update_wk);
4065
Kalle Valo5e3dd152013-06-12 20:52:10 +03004066 mutex_lock(&ar->conf_mutex);
4067
4068 if (old_state == IEEE80211_STA_NOTEXIST &&
Michal Kazior077efc82014-10-21 10:10:29 +03004069 new_state == IEEE80211_STA_NONE) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03004070 /*
4071 * New station addition.
4072 */
Michal Kaziorcfd10612014-11-25 15:16:05 +01004073 ath10k_dbg(ar, ATH10K_DBG_MAC,
4074 "mac vdev %d peer create %pM (new sta) sta %d / %d peer %d / %d\n",
4075 arvif->vdev_id, sta->addr,
4076 ar->num_stations + 1, ar->max_num_stations,
4077 ar->num_peers + 1, ar->max_num_peers);
Bartosz Markowski0e759f32014-01-02 14:38:33 +01004078
Michal Kaziorcfd10612014-11-25 15:16:05 +01004079 ret = ath10k_mac_inc_num_stations(arvif);
4080 if (ret) {
4081 ath10k_warn(ar, "refusing to associate station: too many connected already (%d)\n",
4082 ar->max_num_stations);
Bartosz Markowski0e759f32014-01-02 14:38:33 +01004083 goto exit;
4084 }
4085
Kalle Valo5e3dd152013-06-12 20:52:10 +03004086 ret = ath10k_peer_create(ar, arvif->vdev_id, sta->addr);
Michal Kaziora52c0282014-11-25 15:16:03 +01004087 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004088 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 -08004089 sta->addr, arvif->vdev_id, ret);
Michal Kaziorcfd10612014-11-25 15:16:05 +01004090 ath10k_mac_dec_num_stations(arvif);
Michal Kaziora52c0282014-11-25 15:16:03 +01004091 goto exit;
4092 }
Michal Kazior077efc82014-10-21 10:10:29 +03004093
4094 if (vif->type == NL80211_IFTYPE_STATION) {
4095 WARN_ON(arvif->is_started);
4096
4097 ret = ath10k_vdev_start(arvif);
4098 if (ret) {
4099 ath10k_warn(ar, "failed to start vdev %i: %d\n",
4100 arvif->vdev_id, ret);
4101 WARN_ON(ath10k_peer_delete(ar, arvif->vdev_id,
4102 sta->addr));
Michal Kaziorcfd10612014-11-25 15:16:05 +01004103 ath10k_mac_dec_num_stations(arvif);
Michal Kazior077efc82014-10-21 10:10:29 +03004104 goto exit;
4105 }
4106
4107 arvif->is_started = true;
4108 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03004109 } else if ((old_state == IEEE80211_STA_NONE &&
4110 new_state == IEEE80211_STA_NOTEXIST)) {
4111 /*
4112 * Existing station deletion.
4113 */
Michal Kazior7aa7a722014-08-25 12:09:38 +02004114 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03004115 "mac vdev %d peer delete %pM (sta gone)\n",
4116 arvif->vdev_id, sta->addr);
Michal Kazior077efc82014-10-21 10:10:29 +03004117
4118 if (vif->type == NL80211_IFTYPE_STATION) {
4119 WARN_ON(!arvif->is_started);
4120
4121 ret = ath10k_vdev_stop(arvif);
4122 if (ret)
4123 ath10k_warn(ar, "failed to stop vdev %i: %d\n",
4124 arvif->vdev_id, ret);
4125
4126 arvif->is_started = false;
4127 }
4128
Kalle Valo5e3dd152013-06-12 20:52:10 +03004129 ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
4130 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004131 ath10k_warn(ar, "failed to delete peer %pM for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02004132 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004133
Michal Kaziorcfd10612014-11-25 15:16:05 +01004134 ath10k_mac_dec_num_stations(arvif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004135 } else if (old_state == IEEE80211_STA_AUTH &&
4136 new_state == IEEE80211_STA_ASSOC &&
4137 (vif->type == NL80211_IFTYPE_AP ||
4138 vif->type == NL80211_IFTYPE_ADHOC)) {
4139 /*
4140 * New association.
4141 */
Michal Kazior7aa7a722014-08-25 12:09:38 +02004142 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM associated\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03004143 sta->addr);
4144
Michal Kazior590922a2014-10-21 10:10:29 +03004145 ret = ath10k_station_assoc(ar, vif, sta, false);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004146 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004147 ath10k_warn(ar, "failed to associate station %pM for vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02004148 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004149 } else if (old_state == IEEE80211_STA_ASSOC &&
4150 new_state == IEEE80211_STA_AUTH &&
4151 (vif->type == NL80211_IFTYPE_AP ||
4152 vif->type == NL80211_IFTYPE_ADHOC)) {
4153 /*
4154 * Disassociation.
4155 */
Michal Kazior7aa7a722014-08-25 12:09:38 +02004156 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM disassociated\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03004157 sta->addr);
4158
Michal Kazior590922a2014-10-21 10:10:29 +03004159 ret = ath10k_station_disassoc(ar, vif, sta);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004160 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004161 ath10k_warn(ar, "failed to disassociate station: %pM vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02004162 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004163 }
Bartosz Markowski0e759f32014-01-02 14:38:33 +01004164exit:
Kalle Valo5e3dd152013-06-12 20:52:10 +03004165 mutex_unlock(&ar->conf_mutex);
4166 return ret;
4167}
4168
4169static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
Kalle Valo5b07e072014-09-14 12:50:06 +03004170 u16 ac, bool enable)
Kalle Valo5e3dd152013-06-12 20:52:10 +03004171{
4172 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Michal Kaziorb0e56152015-01-24 12:14:52 +02004173 struct wmi_sta_uapsd_auto_trig_arg arg = {};
4174 u32 prio = 0, acc = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004175 u32 value = 0;
4176 int ret = 0;
4177
Michal Kazior548db542013-07-05 16:15:15 +03004178 lockdep_assert_held(&ar->conf_mutex);
4179
Kalle Valo5e3dd152013-06-12 20:52:10 +03004180 if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
4181 return 0;
4182
4183 switch (ac) {
4184 case IEEE80211_AC_VO:
4185 value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
4186 WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
Michal Kaziorb0e56152015-01-24 12:14:52 +02004187 prio = 7;
4188 acc = 3;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004189 break;
4190 case IEEE80211_AC_VI:
4191 value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
4192 WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
Michal Kaziorb0e56152015-01-24 12:14:52 +02004193 prio = 5;
4194 acc = 2;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004195 break;
4196 case IEEE80211_AC_BE:
4197 value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
4198 WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
Michal Kaziorb0e56152015-01-24 12:14:52 +02004199 prio = 2;
4200 acc = 1;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004201 break;
4202 case IEEE80211_AC_BK:
4203 value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
4204 WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
Michal Kaziorb0e56152015-01-24 12:14:52 +02004205 prio = 0;
4206 acc = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004207 break;
4208 }
4209
4210 if (enable)
4211 arvif->u.sta.uapsd |= value;
4212 else
4213 arvif->u.sta.uapsd &= ~value;
4214
4215 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4216 WMI_STA_PS_PARAM_UAPSD,
4217 arvif->u.sta.uapsd);
4218 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004219 ath10k_warn(ar, "failed to set uapsd params: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004220 goto exit;
4221 }
4222
4223 if (arvif->u.sta.uapsd)
4224 value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
4225 else
4226 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
4227
4228 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4229 WMI_STA_PS_PARAM_RX_WAKE_POLICY,
4230 value);
4231 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004232 ath10k_warn(ar, "failed to set rx wake param: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004233
Michal Kazior9f9b5742014-12-12 12:41:36 +01004234 ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
4235 if (ret) {
4236 ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
4237 arvif->vdev_id, ret);
4238 return ret;
4239 }
4240
4241 ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
4242 if (ret) {
4243 ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
4244 arvif->vdev_id, ret);
4245 return ret;
4246 }
4247
Michal Kaziorb0e56152015-01-24 12:14:52 +02004248 if (test_bit(WMI_SERVICE_STA_UAPSD_BASIC_AUTO_TRIG, ar->wmi.svc_map) ||
4249 test_bit(WMI_SERVICE_STA_UAPSD_VAR_AUTO_TRIG, ar->wmi.svc_map)) {
4250 /* Only userspace can make an educated decision when to send
4251 * trigger frame. The following effectively disables u-UAPSD
4252 * autotrigger in firmware (which is enabled by default
4253 * provided the autotrigger service is available).
4254 */
4255
4256 arg.wmm_ac = acc;
4257 arg.user_priority = prio;
4258 arg.service_interval = 0;
4259 arg.suspend_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
4260 arg.delay_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
4261
4262 ret = ath10k_wmi_vdev_sta_uapsd(ar, arvif->vdev_id,
4263 arvif->bssid, &arg, 1);
4264 if (ret) {
4265 ath10k_warn(ar, "failed to set uapsd auto trigger %d\n",
4266 ret);
4267 return ret;
4268 }
4269 }
4270
Kalle Valo5e3dd152013-06-12 20:52:10 +03004271exit:
4272 return ret;
4273}
4274
4275static int ath10k_conf_tx(struct ieee80211_hw *hw,
4276 struct ieee80211_vif *vif, u16 ac,
4277 const struct ieee80211_tx_queue_params *params)
4278{
4279 struct ath10k *ar = hw->priv;
Michal Kazior5e752e42015-01-19 09:53:41 +01004280 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004281 struct wmi_wmm_params_arg *p = NULL;
4282 int ret;
4283
4284 mutex_lock(&ar->conf_mutex);
4285
4286 switch (ac) {
4287 case IEEE80211_AC_VO:
Michal Kazior5e752e42015-01-19 09:53:41 +01004288 p = &arvif->wmm_params.ac_vo;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004289 break;
4290 case IEEE80211_AC_VI:
Michal Kazior5e752e42015-01-19 09:53:41 +01004291 p = &arvif->wmm_params.ac_vi;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004292 break;
4293 case IEEE80211_AC_BE:
Michal Kazior5e752e42015-01-19 09:53:41 +01004294 p = &arvif->wmm_params.ac_be;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004295 break;
4296 case IEEE80211_AC_BK:
Michal Kazior5e752e42015-01-19 09:53:41 +01004297 p = &arvif->wmm_params.ac_bk;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004298 break;
4299 }
4300
4301 if (WARN_ON(!p)) {
4302 ret = -EINVAL;
4303 goto exit;
4304 }
4305
4306 p->cwmin = params->cw_min;
4307 p->cwmax = params->cw_max;
4308 p->aifs = params->aifs;
4309
4310 /*
4311 * The channel time duration programmed in the HW is in absolute
4312 * microseconds, while mac80211 gives the txop in units of
4313 * 32 microseconds.
4314 */
4315 p->txop = params->txop * 32;
4316
Michal Kazior7fc979a2015-01-28 09:57:28 +02004317 if (ar->wmi.ops->gen_vdev_wmm_conf) {
4318 ret = ath10k_wmi_vdev_wmm_conf(ar, arvif->vdev_id,
4319 &arvif->wmm_params);
4320 if (ret) {
4321 ath10k_warn(ar, "failed to set vdev wmm params on vdev %i: %d\n",
4322 arvif->vdev_id, ret);
4323 goto exit;
4324 }
4325 } else {
4326 /* This won't work well with multi-interface cases but it's
4327 * better than nothing.
4328 */
4329 ret = ath10k_wmi_pdev_set_wmm_params(ar, &arvif->wmm_params);
4330 if (ret) {
4331 ath10k_warn(ar, "failed to set wmm params: %d\n", ret);
4332 goto exit;
4333 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03004334 }
4335
4336 ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
4337 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004338 ath10k_warn(ar, "failed to set sta uapsd: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004339
4340exit:
4341 mutex_unlock(&ar->conf_mutex);
4342 return ret;
4343}
4344
4345#define ATH10K_ROC_TIMEOUT_HZ (2*HZ)
4346
4347static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
4348 struct ieee80211_vif *vif,
4349 struct ieee80211_channel *chan,
4350 int duration,
4351 enum ieee80211_roc_type type)
4352{
4353 struct ath10k *ar = hw->priv;
4354 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4355 struct wmi_start_scan_arg arg;
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004356 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004357
4358 mutex_lock(&ar->conf_mutex);
4359
4360 spin_lock_bh(&ar->data_lock);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004361 switch (ar->scan.state) {
4362 case ATH10K_SCAN_IDLE:
4363 reinit_completion(&ar->scan.started);
4364 reinit_completion(&ar->scan.completed);
4365 reinit_completion(&ar->scan.on_channel);
4366 ar->scan.state = ATH10K_SCAN_STARTING;
4367 ar->scan.is_roc = true;
4368 ar->scan.vdev_id = arvif->vdev_id;
4369 ar->scan.roc_freq = chan->center_freq;
4370 ret = 0;
4371 break;
4372 case ATH10K_SCAN_STARTING:
4373 case ATH10K_SCAN_RUNNING:
4374 case ATH10K_SCAN_ABORTING:
Kalle Valo5e3dd152013-06-12 20:52:10 +03004375 ret = -EBUSY;
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004376 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004377 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03004378 spin_unlock_bh(&ar->data_lock);
4379
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004380 if (ret)
4381 goto exit;
4382
Michal Kaziordcca0bd2014-11-24 14:58:32 +01004383 duration = max(duration, WMI_SCAN_CHAN_MIN_TIME_MSEC);
4384
Kalle Valo5e3dd152013-06-12 20:52:10 +03004385 memset(&arg, 0, sizeof(arg));
4386 ath10k_wmi_start_scan_init(ar, &arg);
4387 arg.vdev_id = arvif->vdev_id;
4388 arg.scan_id = ATH10K_SCAN_ID;
4389 arg.n_channels = 1;
4390 arg.channels[0] = chan->center_freq;
4391 arg.dwell_time_active = duration;
4392 arg.dwell_time_passive = duration;
4393 arg.max_scan_time = 2 * duration;
4394 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
4395 arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
4396
4397 ret = ath10k_start_scan(ar, &arg);
4398 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004399 ath10k_warn(ar, "failed to start roc scan: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004400 spin_lock_bh(&ar->data_lock);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004401 ar->scan.state = ATH10K_SCAN_IDLE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004402 spin_unlock_bh(&ar->data_lock);
4403 goto exit;
4404 }
4405
4406 ret = wait_for_completion_timeout(&ar->scan.on_channel, 3*HZ);
4407 if (ret == 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004408 ath10k_warn(ar, "failed to switch to channel for roc scan\n");
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004409
4410 ret = ath10k_scan_stop(ar);
4411 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004412 ath10k_warn(ar, "failed to stop scan: %d\n", ret);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004413
Kalle Valo5e3dd152013-06-12 20:52:10 +03004414 ret = -ETIMEDOUT;
4415 goto exit;
4416 }
4417
4418 ret = 0;
4419exit:
4420 mutex_unlock(&ar->conf_mutex);
4421 return ret;
4422}
4423
4424static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw)
4425{
4426 struct ath10k *ar = hw->priv;
4427
4428 mutex_lock(&ar->conf_mutex);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004429 ath10k_scan_abort(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004430 mutex_unlock(&ar->conf_mutex);
4431
Michal Kazior4eb2e162014-10-28 10:23:09 +01004432 cancel_delayed_work_sync(&ar->scan.timeout);
4433
Kalle Valo5e3dd152013-06-12 20:52:10 +03004434 return 0;
4435}
4436
4437/*
4438 * Both RTS and Fragmentation threshold are interface-specific
4439 * in ath10k, but device-specific in mac80211.
4440 */
Kalle Valo5e3dd152013-06-12 20:52:10 +03004441
4442static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
4443{
Kalle Valo5e3dd152013-06-12 20:52:10 +03004444 struct ath10k *ar = hw->priv;
Michal Kaziorad088bf2013-10-16 15:44:46 +03004445 struct ath10k_vif *arvif;
4446 int ret = 0;
Michal Kazior548db542013-07-05 16:15:15 +03004447
Michal Kaziorad088bf2013-10-16 15:44:46 +03004448 mutex_lock(&ar->conf_mutex);
4449 list_for_each_entry(arvif, &ar->arvifs, list) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004450 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
Michal Kaziorad088bf2013-10-16 15:44:46 +03004451 arvif->vdev_id, value);
Kalle Valo60c3daa2013-09-08 17:56:07 +03004452
Michal Kaziorad088bf2013-10-16 15:44:46 +03004453 ret = ath10k_mac_set_rts(arvif, value);
4454 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004455 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
Michal Kaziorad088bf2013-10-16 15:44:46 +03004456 arvif->vdev_id, ret);
4457 break;
4458 }
4459 }
4460 mutex_unlock(&ar->conf_mutex);
4461
4462 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004463}
4464
Emmanuel Grumbach77be2c52014-03-27 11:30:29 +02004465static void ath10k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4466 u32 queues, bool drop)
Kalle Valo5e3dd152013-06-12 20:52:10 +03004467{
4468 struct ath10k *ar = hw->priv;
Michal Kazioraffd3212013-07-16 09:54:35 +02004469 bool skip;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004470 int ret;
4471
4472 /* mac80211 doesn't care if we really xmit queued frames or not
4473 * we'll collect those frames either way if we stop/delete vdevs */
4474 if (drop)
4475 return;
4476
Michal Kazior548db542013-07-05 16:15:15 +03004477 mutex_lock(&ar->conf_mutex);
4478
Michal Kazioraffd3212013-07-16 09:54:35 +02004479 if (ar->state == ATH10K_STATE_WEDGED)
4480 goto skip;
4481
Michal Kazioredb82362013-07-05 16:15:14 +03004482 ret = wait_event_timeout(ar->htt.empty_tx_wq, ({
Kalle Valo5e3dd152013-06-12 20:52:10 +03004483 bool empty;
Michal Kazioraffd3212013-07-16 09:54:35 +02004484
Michal Kazioredb82362013-07-05 16:15:14 +03004485 spin_lock_bh(&ar->htt.tx_lock);
Michal Kazior0945baf2013-09-18 14:43:18 +02004486 empty = (ar->htt.num_pending_tx == 0);
Michal Kazioredb82362013-07-05 16:15:14 +03004487 spin_unlock_bh(&ar->htt.tx_lock);
Michal Kazioraffd3212013-07-16 09:54:35 +02004488
Michal Kazior7962b0d2014-10-28 10:34:38 +01004489 skip = (ar->state == ATH10K_STATE_WEDGED) ||
4490 test_bit(ATH10K_FLAG_CRASH_FLUSH,
4491 &ar->dev_flags);
Michal Kazioraffd3212013-07-16 09:54:35 +02004492
4493 (empty || skip);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004494 }), ATH10K_FLUSH_TIMEOUT_HZ);
Michal Kazioraffd3212013-07-16 09:54:35 +02004495
4496 if (ret <= 0 || skip)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004497 ath10k_warn(ar, "failed to flush transmit queue (skip %i ar-state %i): %i\n",
Ben Greear9ba4c782014-02-25 09:29:57 +02004498 skip, ar->state, ret);
Michal Kazior548db542013-07-05 16:15:15 +03004499
Michal Kazioraffd3212013-07-16 09:54:35 +02004500skip:
Michal Kazior548db542013-07-05 16:15:15 +03004501 mutex_unlock(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004502}
4503
4504/* TODO: Implement this function properly
4505 * For now it is needed to reply to Probe Requests in IBSS mode.
4506 * Propably we need this information from FW.
4507 */
4508static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
4509{
4510 return 1;
4511}
4512
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004513#ifdef CONFIG_PM
4514static int ath10k_suspend(struct ieee80211_hw *hw,
4515 struct cfg80211_wowlan *wowlan)
4516{
4517 struct ath10k *ar = hw->priv;
4518 int ret;
4519
Marek Puzyniak9042e172014-02-10 17:14:23 +01004520 mutex_lock(&ar->conf_mutex);
4521
Marek Puzyniak00f54822014-02-10 17:14:24 +01004522 ret = ath10k_wait_for_suspend(ar, WMI_PDEV_SUSPEND);
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004523 if (ret) {
Marek Puzyniak00f54822014-02-10 17:14:24 +01004524 if (ret == -ETIMEDOUT)
4525 goto resume;
Marek Puzyniak9042e172014-02-10 17:14:23 +01004526 ret = 1;
4527 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004528 }
4529
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004530 ret = ath10k_hif_suspend(ar);
4531 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004532 ath10k_warn(ar, "failed to suspend hif: %d\n", ret);
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004533 goto resume;
4534 }
4535
Marek Puzyniak9042e172014-02-10 17:14:23 +01004536 ret = 0;
4537 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004538resume:
4539 ret = ath10k_wmi_pdev_resume_target(ar);
4540 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004541 ath10k_warn(ar, "failed to resume target: %d\n", ret);
Marek Puzyniak9042e172014-02-10 17:14:23 +01004542
4543 ret = 1;
4544exit:
4545 mutex_unlock(&ar->conf_mutex);
4546 return ret;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004547}
4548
4549static int ath10k_resume(struct ieee80211_hw *hw)
4550{
4551 struct ath10k *ar = hw->priv;
4552 int ret;
4553
Marek Puzyniak9042e172014-02-10 17:14:23 +01004554 mutex_lock(&ar->conf_mutex);
4555
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004556 ret = ath10k_hif_resume(ar);
4557 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004558 ath10k_warn(ar, "failed to resume hif: %d\n", ret);
Marek Puzyniak9042e172014-02-10 17:14:23 +01004559 ret = 1;
4560 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004561 }
4562
4563 ret = ath10k_wmi_pdev_resume_target(ar);
4564 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004565 ath10k_warn(ar, "failed to resume target: %d\n", ret);
Marek Puzyniak9042e172014-02-10 17:14:23 +01004566 ret = 1;
4567 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004568 }
4569
Marek Puzyniak9042e172014-02-10 17:14:23 +01004570 ret = 0;
4571exit:
4572 mutex_unlock(&ar->conf_mutex);
4573 return ret;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004574}
4575#endif
4576
Eliad Pellercf2c92d2014-11-04 11:43:54 +02004577static void ath10k_reconfig_complete(struct ieee80211_hw *hw,
4578 enum ieee80211_reconfig_type reconfig_type)
Michal Kazioraffd3212013-07-16 09:54:35 +02004579{
4580 struct ath10k *ar = hw->priv;
4581
Eliad Pellercf2c92d2014-11-04 11:43:54 +02004582 if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
4583 return;
4584
Michal Kazioraffd3212013-07-16 09:54:35 +02004585 mutex_lock(&ar->conf_mutex);
4586
4587 /* If device failed to restart it will be in a different state, e.g.
4588 * ATH10K_STATE_WEDGED */
4589 if (ar->state == ATH10K_STATE_RESTARTED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004590 ath10k_info(ar, "device successfully recovered\n");
Michal Kazioraffd3212013-07-16 09:54:35 +02004591 ar->state = ATH10K_STATE_ON;
Michal Kazior7962b0d2014-10-28 10:34:38 +01004592 ieee80211_wake_queues(ar->hw);
Michal Kazioraffd3212013-07-16 09:54:35 +02004593 }
4594
4595 mutex_unlock(&ar->conf_mutex);
4596}
4597
Michal Kazior2e1dea42013-07-31 10:32:40 +02004598static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
4599 struct survey_info *survey)
4600{
4601 struct ath10k *ar = hw->priv;
4602 struct ieee80211_supported_band *sband;
4603 struct survey_info *ar_survey = &ar->survey[idx];
4604 int ret = 0;
4605
4606 mutex_lock(&ar->conf_mutex);
4607
4608 sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
4609 if (sband && idx >= sband->n_channels) {
4610 idx -= sband->n_channels;
4611 sband = NULL;
4612 }
4613
4614 if (!sband)
4615 sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
4616
4617 if (!sband || idx >= sband->n_channels) {
4618 ret = -ENOENT;
4619 goto exit;
4620 }
4621
4622 spin_lock_bh(&ar->data_lock);
4623 memcpy(survey, ar_survey, sizeof(*survey));
4624 spin_unlock_bh(&ar->data_lock);
4625
4626 survey->channel = &sband->channels[idx];
4627
Felix Fietkaufa1d4df2014-10-23 17:04:28 +03004628 if (ar->rx_channel == survey->channel)
4629 survey->filled |= SURVEY_INFO_IN_USE;
4630
Michal Kazior2e1dea42013-07-31 10:32:40 +02004631exit:
4632 mutex_unlock(&ar->conf_mutex);
4633 return ret;
4634}
4635
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004636/* Helper table for legacy fixed_rate/bitrate_mask */
4637static const u8 cck_ofdm_rate[] = {
4638 /* CCK */
4639 3, /* 1Mbps */
4640 2, /* 2Mbps */
4641 1, /* 5.5Mbps */
4642 0, /* 11Mbps */
4643 /* OFDM */
4644 3, /* 6Mbps */
4645 7, /* 9Mbps */
4646 2, /* 12Mbps */
4647 6, /* 18Mbps */
4648 1, /* 24Mbps */
4649 5, /* 36Mbps */
4650 0, /* 48Mbps */
4651 4, /* 54Mbps */
4652};
4653
4654/* Check if only one bit set */
4655static int ath10k_check_single_mask(u32 mask)
4656{
4657 int bit;
4658
4659 bit = ffs(mask);
4660 if (!bit)
4661 return 0;
4662
4663 mask &= ~BIT(bit - 1);
4664 if (mask)
4665 return 2;
4666
4667 return 1;
4668}
4669
4670static bool
4671ath10k_default_bitrate_mask(struct ath10k *ar,
4672 enum ieee80211_band band,
4673 const struct cfg80211_bitrate_mask *mask)
4674{
4675 u32 legacy = 0x00ff;
4676 u8 ht = 0xff, i;
4677 u16 vht = 0x3ff;
Ben Greearb116ea12014-11-24 16:22:10 +02004678 u16 nrf = ar->num_rf_chains;
4679
4680 if (ar->cfg_tx_chainmask)
4681 nrf = get_nss_from_chainmask(ar->cfg_tx_chainmask);
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004682
4683 switch (band) {
4684 case IEEE80211_BAND_2GHZ:
4685 legacy = 0x00fff;
4686 vht = 0;
4687 break;
4688 case IEEE80211_BAND_5GHZ:
4689 break;
4690 default:
4691 return false;
4692 }
4693
4694 if (mask->control[band].legacy != legacy)
4695 return false;
4696
Ben Greearb116ea12014-11-24 16:22:10 +02004697 for (i = 0; i < nrf; i++)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004698 if (mask->control[band].ht_mcs[i] != ht)
4699 return false;
4700
Ben Greearb116ea12014-11-24 16:22:10 +02004701 for (i = 0; i < nrf; i++)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004702 if (mask->control[band].vht_mcs[i] != vht)
4703 return false;
4704
4705 return true;
4706}
4707
4708static bool
4709ath10k_bitrate_mask_nss(const struct cfg80211_bitrate_mask *mask,
4710 enum ieee80211_band band,
4711 u8 *fixed_nss)
4712{
4713 int ht_nss = 0, vht_nss = 0, i;
4714
4715 /* check legacy */
4716 if (ath10k_check_single_mask(mask->control[band].legacy))
4717 return false;
4718
4719 /* check HT */
4720 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
4721 if (mask->control[band].ht_mcs[i] == 0xff)
4722 continue;
4723 else if (mask->control[band].ht_mcs[i] == 0x00)
4724 break;
Kalle Valod8bb26b2014-09-14 12:50:33 +03004725
4726 return false;
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004727 }
4728
4729 ht_nss = i;
4730
4731 /* check VHT */
4732 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
4733 if (mask->control[band].vht_mcs[i] == 0x03ff)
4734 continue;
4735 else if (mask->control[band].vht_mcs[i] == 0x0000)
4736 break;
Kalle Valod8bb26b2014-09-14 12:50:33 +03004737
4738 return false;
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004739 }
4740
4741 vht_nss = i;
4742
4743 if (ht_nss > 0 && vht_nss > 0)
4744 return false;
4745
4746 if (ht_nss)
4747 *fixed_nss = ht_nss;
4748 else if (vht_nss)
4749 *fixed_nss = vht_nss;
4750 else
4751 return false;
4752
4753 return true;
4754}
4755
4756static bool
4757ath10k_bitrate_mask_correct(const struct cfg80211_bitrate_mask *mask,
4758 enum ieee80211_band band,
4759 enum wmi_rate_preamble *preamble)
4760{
4761 int legacy = 0, ht = 0, vht = 0, i;
4762
4763 *preamble = WMI_RATE_PREAMBLE_OFDM;
4764
4765 /* check legacy */
4766 legacy = ath10k_check_single_mask(mask->control[band].legacy);
4767 if (legacy > 1)
4768 return false;
4769
4770 /* check HT */
4771 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
4772 ht += ath10k_check_single_mask(mask->control[band].ht_mcs[i]);
4773 if (ht > 1)
4774 return false;
4775
4776 /* check VHT */
4777 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
4778 vht += ath10k_check_single_mask(mask->control[band].vht_mcs[i]);
4779 if (vht > 1)
4780 return false;
4781
4782 /* Currently we support only one fixed_rate */
4783 if ((legacy + ht + vht) != 1)
4784 return false;
4785
4786 if (ht)
4787 *preamble = WMI_RATE_PREAMBLE_HT;
4788 else if (vht)
4789 *preamble = WMI_RATE_PREAMBLE_VHT;
4790
4791 return true;
4792}
4793
4794static bool
Michal Kazior7aa7a722014-08-25 12:09:38 +02004795ath10k_bitrate_mask_rate(struct ath10k *ar,
4796 const struct cfg80211_bitrate_mask *mask,
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004797 enum ieee80211_band band,
4798 u8 *fixed_rate,
4799 u8 *fixed_nss)
4800{
4801 u8 rate = 0, pream = 0, nss = 0, i;
4802 enum wmi_rate_preamble preamble;
4803
4804 /* Check if single rate correct */
4805 if (!ath10k_bitrate_mask_correct(mask, band, &preamble))
4806 return false;
4807
4808 pream = preamble;
4809
4810 switch (preamble) {
4811 case WMI_RATE_PREAMBLE_CCK:
4812 case WMI_RATE_PREAMBLE_OFDM:
4813 i = ffs(mask->control[band].legacy) - 1;
4814
4815 if (band == IEEE80211_BAND_2GHZ && i < 4)
4816 pream = WMI_RATE_PREAMBLE_CCK;
4817
4818 if (band == IEEE80211_BAND_5GHZ)
4819 i += 4;
4820
4821 if (i >= ARRAY_SIZE(cck_ofdm_rate))
4822 return false;
4823
4824 rate = cck_ofdm_rate[i];
4825 break;
4826 case WMI_RATE_PREAMBLE_HT:
4827 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
4828 if (mask->control[band].ht_mcs[i])
4829 break;
4830
4831 if (i == IEEE80211_HT_MCS_MASK_LEN)
4832 return false;
4833
4834 rate = ffs(mask->control[band].ht_mcs[i]) - 1;
4835 nss = i;
4836 break;
4837 case WMI_RATE_PREAMBLE_VHT:
4838 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
4839 if (mask->control[band].vht_mcs[i])
4840 break;
4841
4842 if (i == NL80211_VHT_NSS_MAX)
4843 return false;
4844
4845 rate = ffs(mask->control[band].vht_mcs[i]) - 1;
4846 nss = i;
4847 break;
4848 }
4849
4850 *fixed_nss = nss + 1;
4851 nss <<= 4;
4852 pream <<= 6;
4853
Michal Kazior7aa7a722014-08-25 12:09:38 +02004854 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 +01004855 pream, nss, rate);
4856
4857 *fixed_rate = pream | nss | rate;
4858
4859 return true;
4860}
4861
Michal Kazior7aa7a722014-08-25 12:09:38 +02004862static bool ath10k_get_fixed_rate_nss(struct ath10k *ar,
4863 const struct cfg80211_bitrate_mask *mask,
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004864 enum ieee80211_band band,
4865 u8 *fixed_rate,
4866 u8 *fixed_nss)
4867{
4868 /* First check full NSS mask, if we can simply limit NSS */
4869 if (ath10k_bitrate_mask_nss(mask, band, fixed_nss))
4870 return true;
4871
4872 /* Next Check single rate is set */
Michal Kazior7aa7a722014-08-25 12:09:38 +02004873 return ath10k_bitrate_mask_rate(ar, mask, band, fixed_rate, fixed_nss);
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004874}
4875
4876static int ath10k_set_fixed_rate_param(struct ath10k_vif *arvif,
4877 u8 fixed_rate,
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004878 u8 fixed_nss,
4879 u8 force_sgi)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004880{
4881 struct ath10k *ar = arvif->ar;
4882 u32 vdev_param;
4883 int ret = 0;
4884
4885 mutex_lock(&ar->conf_mutex);
4886
4887 if (arvif->fixed_rate == fixed_rate &&
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004888 arvif->fixed_nss == fixed_nss &&
4889 arvif->force_sgi == force_sgi)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004890 goto exit;
4891
4892 if (fixed_rate == WMI_FIXED_RATE_NONE)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004893 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac disable fixed bitrate mask\n");
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004894
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004895 if (force_sgi)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004896 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac force sgi\n");
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004897
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004898 vdev_param = ar->wmi.vdev_param->fixed_rate;
4899 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
4900 vdev_param, fixed_rate);
4901 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004902 ath10k_warn(ar, "failed to set fixed rate param 0x%02x: %d\n",
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004903 fixed_rate, ret);
4904 ret = -EINVAL;
4905 goto exit;
4906 }
4907
4908 arvif->fixed_rate = fixed_rate;
4909
4910 vdev_param = ar->wmi.vdev_param->nss;
4911 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
4912 vdev_param, fixed_nss);
4913
4914 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004915 ath10k_warn(ar, "failed to set fixed nss param %d: %d\n",
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004916 fixed_nss, ret);
4917 ret = -EINVAL;
4918 goto exit;
4919 }
4920
4921 arvif->fixed_nss = fixed_nss;
4922
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004923 vdev_param = ar->wmi.vdev_param->sgi;
4924 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
4925 force_sgi);
4926
4927 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004928 ath10k_warn(ar, "failed to set sgi param %d: %d\n",
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004929 force_sgi, ret);
4930 ret = -EINVAL;
4931 goto exit;
4932 }
4933
4934 arvif->force_sgi = force_sgi;
4935
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004936exit:
4937 mutex_unlock(&ar->conf_mutex);
4938 return ret;
4939}
4940
4941static int ath10k_set_bitrate_mask(struct ieee80211_hw *hw,
4942 struct ieee80211_vif *vif,
4943 const struct cfg80211_bitrate_mask *mask)
4944{
4945 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4946 struct ath10k *ar = arvif->ar;
4947 enum ieee80211_band band = ar->hw->conf.chandef.chan->band;
4948 u8 fixed_rate = WMI_FIXED_RATE_NONE;
4949 u8 fixed_nss = ar->num_rf_chains;
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004950 u8 force_sgi;
4951
Ben Greearb116ea12014-11-24 16:22:10 +02004952 if (ar->cfg_tx_chainmask)
4953 fixed_nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
4954
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004955 force_sgi = mask->control[band].gi;
4956 if (force_sgi == NL80211_TXRATE_FORCE_LGI)
4957 return -EINVAL;
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004958
4959 if (!ath10k_default_bitrate_mask(ar, band, mask)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004960 if (!ath10k_get_fixed_rate_nss(ar, mask, band,
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004961 &fixed_rate,
4962 &fixed_nss))
4963 return -EINVAL;
4964 }
4965
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004966 if (fixed_rate == WMI_FIXED_RATE_NONE && force_sgi) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004967 ath10k_warn(ar, "failed to force SGI usage for default rate settings\n");
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004968 return -EINVAL;
4969 }
4970
4971 return ath10k_set_fixed_rate_param(arvif, fixed_rate,
4972 fixed_nss, force_sgi);
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004973}
4974
Michal Kazior9797feb2014-02-14 14:49:48 +01004975static void ath10k_sta_rc_update(struct ieee80211_hw *hw,
4976 struct ieee80211_vif *vif,
4977 struct ieee80211_sta *sta,
4978 u32 changed)
4979{
4980 struct ath10k *ar = hw->priv;
4981 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
4982 u32 bw, smps;
4983
4984 spin_lock_bh(&ar->data_lock);
4985
Michal Kazior7aa7a722014-08-25 12:09:38 +02004986 ath10k_dbg(ar, ATH10K_DBG_MAC,
Michal Kazior9797feb2014-02-14 14:49:48 +01004987 "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
4988 sta->addr, changed, sta->bandwidth, sta->rx_nss,
4989 sta->smps_mode);
4990
4991 if (changed & IEEE80211_RC_BW_CHANGED) {
4992 bw = WMI_PEER_CHWIDTH_20MHZ;
4993
4994 switch (sta->bandwidth) {
4995 case IEEE80211_STA_RX_BW_20:
4996 bw = WMI_PEER_CHWIDTH_20MHZ;
4997 break;
4998 case IEEE80211_STA_RX_BW_40:
4999 bw = WMI_PEER_CHWIDTH_40MHZ;
5000 break;
5001 case IEEE80211_STA_RX_BW_80:
5002 bw = WMI_PEER_CHWIDTH_80MHZ;
5003 break;
5004 case IEEE80211_STA_RX_BW_160:
Masanari Iidad939be32015-02-27 23:52:31 +09005005 ath10k_warn(ar, "Invalid bandwidth %d in rc update for %pM\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02005006 sta->bandwidth, sta->addr);
Michal Kazior9797feb2014-02-14 14:49:48 +01005007 bw = WMI_PEER_CHWIDTH_20MHZ;
5008 break;
5009 }
5010
5011 arsta->bw = bw;
5012 }
5013
5014 if (changed & IEEE80211_RC_NSS_CHANGED)
5015 arsta->nss = sta->rx_nss;
5016
5017 if (changed & IEEE80211_RC_SMPS_CHANGED) {
5018 smps = WMI_PEER_SMPS_PS_NONE;
5019
5020 switch (sta->smps_mode) {
5021 case IEEE80211_SMPS_AUTOMATIC:
5022 case IEEE80211_SMPS_OFF:
5023 smps = WMI_PEER_SMPS_PS_NONE;
5024 break;
5025 case IEEE80211_SMPS_STATIC:
5026 smps = WMI_PEER_SMPS_STATIC;
5027 break;
5028 case IEEE80211_SMPS_DYNAMIC:
5029 smps = WMI_PEER_SMPS_DYNAMIC;
5030 break;
5031 case IEEE80211_SMPS_NUM_MODES:
Michal Kazior7aa7a722014-08-25 12:09:38 +02005032 ath10k_warn(ar, "Invalid smps %d in sta rc update for %pM\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02005033 sta->smps_mode, sta->addr);
Michal Kazior9797feb2014-02-14 14:49:48 +01005034 smps = WMI_PEER_SMPS_PS_NONE;
5035 break;
5036 }
5037
5038 arsta->smps = smps;
5039 }
5040
Michal Kazior9797feb2014-02-14 14:49:48 +01005041 arsta->changed |= changed;
5042
5043 spin_unlock_bh(&ar->data_lock);
5044
5045 ieee80211_queue_work(hw, &arsta->update_wk);
5046}
5047
Chun-Yeow Yeoh26ebbcc2014-02-25 09:29:54 +02005048static u64 ath10k_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
5049{
5050 /*
5051 * FIXME: Return 0 for time being. Need to figure out whether FW
5052 * has the API to fetch 64-bit local TSF
5053 */
5054
5055 return 0;
5056}
5057
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02005058static int ath10k_ampdu_action(struct ieee80211_hw *hw,
5059 struct ieee80211_vif *vif,
5060 enum ieee80211_ampdu_mlme_action action,
5061 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
5062 u8 buf_size)
5063{
Michal Kazior7aa7a722014-08-25 12:09:38 +02005064 struct ath10k *ar = hw->priv;
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02005065 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5066
Michal Kazior7aa7a722014-08-25 12:09:38 +02005067 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 +02005068 arvif->vdev_id, sta->addr, tid, action);
5069
5070 switch (action) {
5071 case IEEE80211_AMPDU_RX_START:
5072 case IEEE80211_AMPDU_RX_STOP:
5073 /* HTT AddBa/DelBa events trigger mac80211 Rx BA session
5074 * creation/removal. Do we need to verify this?
5075 */
5076 return 0;
5077 case IEEE80211_AMPDU_TX_START:
5078 case IEEE80211_AMPDU_TX_STOP_CONT:
5079 case IEEE80211_AMPDU_TX_STOP_FLUSH:
5080 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
5081 case IEEE80211_AMPDU_TX_OPERATIONAL:
5082 /* Firmware offloads Tx aggregation entirely so deny mac80211
5083 * Tx aggregation requests.
5084 */
5085 return -EOPNOTSUPP;
5086 }
5087
5088 return -EINVAL;
5089}
5090
Kalle Valo5e3dd152013-06-12 20:52:10 +03005091static const struct ieee80211_ops ath10k_ops = {
5092 .tx = ath10k_tx,
5093 .start = ath10k_start,
5094 .stop = ath10k_stop,
5095 .config = ath10k_config,
5096 .add_interface = ath10k_add_interface,
5097 .remove_interface = ath10k_remove_interface,
5098 .configure_filter = ath10k_configure_filter,
5099 .bss_info_changed = ath10k_bss_info_changed,
5100 .hw_scan = ath10k_hw_scan,
5101 .cancel_hw_scan = ath10k_cancel_hw_scan,
5102 .set_key = ath10k_set_key,
SenthilKumar Jegadeesan627613f2015-01-29 13:50:38 +02005103 .set_default_unicast_key = ath10k_set_default_unicast_key,
Kalle Valo5e3dd152013-06-12 20:52:10 +03005104 .sta_state = ath10k_sta_state,
5105 .conf_tx = ath10k_conf_tx,
5106 .remain_on_channel = ath10k_remain_on_channel,
5107 .cancel_remain_on_channel = ath10k_cancel_remain_on_channel,
5108 .set_rts_threshold = ath10k_set_rts_threshold,
Kalle Valo5e3dd152013-06-12 20:52:10 +03005109 .flush = ath10k_flush,
5110 .tx_last_beacon = ath10k_tx_last_beacon,
Ben Greear46acf7b2014-05-16 17:15:38 +03005111 .set_antenna = ath10k_set_antenna,
5112 .get_antenna = ath10k_get_antenna,
Eliad Pellercf2c92d2014-11-04 11:43:54 +02005113 .reconfig_complete = ath10k_reconfig_complete,
Michal Kazior2e1dea42013-07-31 10:32:40 +02005114 .get_survey = ath10k_get_survey,
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01005115 .set_bitrate_mask = ath10k_set_bitrate_mask,
Michal Kazior9797feb2014-02-14 14:49:48 +01005116 .sta_rc_update = ath10k_sta_rc_update,
Chun-Yeow Yeoh26ebbcc2014-02-25 09:29:54 +02005117 .get_tsf = ath10k_get_tsf,
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02005118 .ampdu_action = ath10k_ampdu_action,
Ben Greear6cddcc72014-09-29 14:41:46 +03005119 .get_et_sset_count = ath10k_debug_get_et_sset_count,
5120 .get_et_stats = ath10k_debug_get_et_stats,
5121 .get_et_strings = ath10k_debug_get_et_strings,
Kalle Valo43d2a302014-09-10 18:23:30 +03005122
5123 CFG80211_TESTMODE_CMD(ath10k_tm_cmd)
5124
Michal Kazior8cd13ca2013-07-16 09:38:54 +02005125#ifdef CONFIG_PM
5126 .suspend = ath10k_suspend,
5127 .resume = ath10k_resume,
5128#endif
Rajkumar Manoharanf5045982015-01-12 14:07:27 +02005129#ifdef CONFIG_MAC80211_DEBUGFS
5130 .sta_add_debugfs = ath10k_sta_add_debugfs,
5131#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03005132};
5133
5134#define RATETAB_ENT(_rate, _rateid, _flags) { \
5135 .bitrate = (_rate), \
5136 .flags = (_flags), \
5137 .hw_value = (_rateid), \
5138}
5139
5140#define CHAN2G(_channel, _freq, _flags) { \
5141 .band = IEEE80211_BAND_2GHZ, \
5142 .hw_value = (_channel), \
5143 .center_freq = (_freq), \
5144 .flags = (_flags), \
5145 .max_antenna_gain = 0, \
5146 .max_power = 30, \
5147}
5148
5149#define CHAN5G(_channel, _freq, _flags) { \
5150 .band = IEEE80211_BAND_5GHZ, \
5151 .hw_value = (_channel), \
5152 .center_freq = (_freq), \
5153 .flags = (_flags), \
5154 .max_antenna_gain = 0, \
5155 .max_power = 30, \
5156}
5157
5158static const struct ieee80211_channel ath10k_2ghz_channels[] = {
5159 CHAN2G(1, 2412, 0),
5160 CHAN2G(2, 2417, 0),
5161 CHAN2G(3, 2422, 0),
5162 CHAN2G(4, 2427, 0),
5163 CHAN2G(5, 2432, 0),
5164 CHAN2G(6, 2437, 0),
5165 CHAN2G(7, 2442, 0),
5166 CHAN2G(8, 2447, 0),
5167 CHAN2G(9, 2452, 0),
5168 CHAN2G(10, 2457, 0),
5169 CHAN2G(11, 2462, 0),
5170 CHAN2G(12, 2467, 0),
5171 CHAN2G(13, 2472, 0),
5172 CHAN2G(14, 2484, 0),
5173};
5174
5175static const struct ieee80211_channel ath10k_5ghz_channels[] = {
Michal Kazior429ff562013-06-26 08:54:54 +02005176 CHAN5G(36, 5180, 0),
5177 CHAN5G(40, 5200, 0),
5178 CHAN5G(44, 5220, 0),
5179 CHAN5G(48, 5240, 0),
5180 CHAN5G(52, 5260, 0),
5181 CHAN5G(56, 5280, 0),
5182 CHAN5G(60, 5300, 0),
5183 CHAN5G(64, 5320, 0),
5184 CHAN5G(100, 5500, 0),
5185 CHAN5G(104, 5520, 0),
5186 CHAN5G(108, 5540, 0),
5187 CHAN5G(112, 5560, 0),
5188 CHAN5G(116, 5580, 0),
5189 CHAN5G(120, 5600, 0),
5190 CHAN5G(124, 5620, 0),
5191 CHAN5G(128, 5640, 0),
5192 CHAN5G(132, 5660, 0),
5193 CHAN5G(136, 5680, 0),
5194 CHAN5G(140, 5700, 0),
5195 CHAN5G(149, 5745, 0),
5196 CHAN5G(153, 5765, 0),
5197 CHAN5G(157, 5785, 0),
5198 CHAN5G(161, 5805, 0),
5199 CHAN5G(165, 5825, 0),
Kalle Valo5e3dd152013-06-12 20:52:10 +03005200};
5201
Michal Kazior91b12082014-12-12 12:41:35 +01005202/* Note: Be careful if you re-order these. There is code which depends on this
5203 * ordering.
5204 */
Kalle Valo5e3dd152013-06-12 20:52:10 +03005205static struct ieee80211_rate ath10k_rates[] = {
5206 /* CCK */
5207 RATETAB_ENT(10, 0x82, 0),
5208 RATETAB_ENT(20, 0x84, 0),
5209 RATETAB_ENT(55, 0x8b, 0),
5210 RATETAB_ENT(110, 0x96, 0),
5211 /* OFDM */
5212 RATETAB_ENT(60, 0x0c, 0),
5213 RATETAB_ENT(90, 0x12, 0),
5214 RATETAB_ENT(120, 0x18, 0),
5215 RATETAB_ENT(180, 0x24, 0),
5216 RATETAB_ENT(240, 0x30, 0),
5217 RATETAB_ENT(360, 0x48, 0),
5218 RATETAB_ENT(480, 0x60, 0),
5219 RATETAB_ENT(540, 0x6c, 0),
5220};
5221
5222#define ath10k_a_rates (ath10k_rates + 4)
5223#define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - 4)
5224#define ath10k_g_rates (ath10k_rates + 0)
5225#define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
5226
Michal Kaziore7b54192014-08-07 11:03:27 +02005227struct ath10k *ath10k_mac_create(size_t priv_size)
Kalle Valo5e3dd152013-06-12 20:52:10 +03005228{
5229 struct ieee80211_hw *hw;
5230 struct ath10k *ar;
5231
Michal Kaziore7b54192014-08-07 11:03:27 +02005232 hw = ieee80211_alloc_hw(sizeof(struct ath10k) + priv_size, &ath10k_ops);
Kalle Valo5e3dd152013-06-12 20:52:10 +03005233 if (!hw)
5234 return NULL;
5235
5236 ar = hw->priv;
5237 ar->hw = hw;
5238
5239 return ar;
5240}
5241
5242void ath10k_mac_destroy(struct ath10k *ar)
5243{
5244 ieee80211_free_hw(ar->hw);
5245}
5246
5247static const struct ieee80211_iface_limit ath10k_if_limits[] = {
5248 {
5249 .max = 8,
5250 .types = BIT(NL80211_IFTYPE_STATION)
5251 | BIT(NL80211_IFTYPE_P2P_CLIENT)
Michal Kaziord531cb82013-07-31 10:55:13 +02005252 },
5253 {
5254 .max = 3,
5255 .types = BIT(NL80211_IFTYPE_P2P_GO)
5256 },
5257 {
Michal Kazior75d2bd42014-12-12 12:41:39 +01005258 .max = 1,
5259 .types = BIT(NL80211_IFTYPE_P2P_DEVICE)
5260 },
5261 {
Michal Kaziord531cb82013-07-31 10:55:13 +02005262 .max = 7,
5263 .types = BIT(NL80211_IFTYPE_AP)
5264 },
Kalle Valo5e3dd152013-06-12 20:52:10 +03005265};
5266
Bartosz Markowskif2595092013-12-10 16:20:39 +01005267static const struct ieee80211_iface_limit ath10k_10x_if_limits[] = {
Marek Puzyniake8a50f82013-11-20 09:59:47 +02005268 {
5269 .max = 8,
5270 .types = BIT(NL80211_IFTYPE_AP)
5271 },
5272};
Marek Puzyniake8a50f82013-11-20 09:59:47 +02005273
5274static const struct ieee80211_iface_combination ath10k_if_comb[] = {
5275 {
5276 .limits = ath10k_if_limits,
5277 .n_limits = ARRAY_SIZE(ath10k_if_limits),
5278 .max_interfaces = 8,
5279 .num_different_channels = 1,
5280 .beacon_int_infra_match = true,
5281 },
Bartosz Markowskif2595092013-12-10 16:20:39 +01005282};
5283
5284static const struct ieee80211_iface_combination ath10k_10x_if_comb[] = {
Marek Puzyniake8a50f82013-11-20 09:59:47 +02005285 {
Bartosz Markowskif2595092013-12-10 16:20:39 +01005286 .limits = ath10k_10x_if_limits,
5287 .n_limits = ARRAY_SIZE(ath10k_10x_if_limits),
Marek Puzyniake8a50f82013-11-20 09:59:47 +02005288 .max_interfaces = 8,
5289 .num_different_channels = 1,
5290 .beacon_int_infra_match = true,
Bartosz Markowskif2595092013-12-10 16:20:39 +01005291#ifdef CONFIG_ATH10K_DFS_CERTIFIED
Marek Puzyniake8a50f82013-11-20 09:59:47 +02005292 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
5293 BIT(NL80211_CHAN_WIDTH_20) |
5294 BIT(NL80211_CHAN_WIDTH_40) |
5295 BIT(NL80211_CHAN_WIDTH_80),
Marek Puzyniake8a50f82013-11-20 09:59:47 +02005296#endif
Bartosz Markowskif2595092013-12-10 16:20:39 +01005297 },
Kalle Valo5e3dd152013-06-12 20:52:10 +03005298};
5299
5300static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
5301{
5302 struct ieee80211_sta_vht_cap vht_cap = {0};
5303 u16 mcs_map;
Michal Kazior8865bee42013-07-24 12:36:46 +02005304 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005305
5306 vht_cap.vht_supported = 1;
5307 vht_cap.cap = ar->vht_cap_info;
5308
Michal Kazior8865bee42013-07-24 12:36:46 +02005309 mcs_map = 0;
5310 for (i = 0; i < 8; i++) {
5311 if (i < ar->num_rf_chains)
5312 mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i*2);
5313 else
5314 mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i*2);
5315 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03005316
5317 vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
5318 vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
5319
5320 return vht_cap;
5321}
5322
5323static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
5324{
5325 int i;
5326 struct ieee80211_sta_ht_cap ht_cap = {0};
5327
5328 if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
5329 return ht_cap;
5330
5331 ht_cap.ht_supported = 1;
5332 ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
5333 ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
5334 ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
5335 ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
5336 ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
5337
5338 if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
5339 ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
5340
5341 if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
5342 ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
5343
5344 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
5345 u32 smps;
5346
5347 smps = WLAN_HT_CAP_SM_PS_DYNAMIC;
5348 smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
5349
5350 ht_cap.cap |= smps;
5351 }
5352
5353 if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC)
5354 ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
5355
5356 if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
5357 u32 stbc;
5358
5359 stbc = ar->ht_cap_info;
5360 stbc &= WMI_HT_CAP_RX_STBC;
5361 stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
5362 stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
5363 stbc &= IEEE80211_HT_CAP_RX_STBC;
5364
5365 ht_cap.cap |= stbc;
5366 }
5367
5368 if (ar->ht_cap_info & WMI_HT_CAP_LDPC)
5369 ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
5370
5371 if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
5372 ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
5373
5374 /* max AMSDU is implicitly taken from vht_cap_info */
5375 if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
5376 ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
5377
Michal Kazior8865bee42013-07-24 12:36:46 +02005378 for (i = 0; i < ar->num_rf_chains; i++)
Kalle Valo5e3dd152013-06-12 20:52:10 +03005379 ht_cap.mcs.rx_mask[i] = 0xFF;
5380
5381 ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
5382
5383 return ht_cap;
5384}
5385
Kalle Valo5e3dd152013-06-12 20:52:10 +03005386static void ath10k_get_arvif_iter(void *data, u8 *mac,
5387 struct ieee80211_vif *vif)
5388{
5389 struct ath10k_vif_iter *arvif_iter = data;
5390 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5391
5392 if (arvif->vdev_id == arvif_iter->vdev_id)
5393 arvif_iter->arvif = arvif;
5394}
5395
5396struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
5397{
5398 struct ath10k_vif_iter arvif_iter;
5399 u32 flags;
5400
5401 memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
5402 arvif_iter.vdev_id = vdev_id;
5403
5404 flags = IEEE80211_IFACE_ITER_RESUME_ALL;
5405 ieee80211_iterate_active_interfaces_atomic(ar->hw,
5406 flags,
5407 ath10k_get_arvif_iter,
5408 &arvif_iter);
5409 if (!arvif_iter.arvif) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02005410 ath10k_warn(ar, "No VIF found for vdev %d\n", vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03005411 return NULL;
5412 }
5413
5414 return arvif_iter.arvif;
5415}
5416
5417int ath10k_mac_register(struct ath10k *ar)
5418{
Johannes Berg3cb10942015-01-22 21:38:45 +01005419 static const u32 cipher_suites[] = {
5420 WLAN_CIPHER_SUITE_WEP40,
5421 WLAN_CIPHER_SUITE_WEP104,
5422 WLAN_CIPHER_SUITE_TKIP,
5423 WLAN_CIPHER_SUITE_CCMP,
5424 WLAN_CIPHER_SUITE_AES_CMAC,
5425 };
Kalle Valo5e3dd152013-06-12 20:52:10 +03005426 struct ieee80211_supported_band *band;
5427 struct ieee80211_sta_vht_cap vht_cap;
5428 struct ieee80211_sta_ht_cap ht_cap;
5429 void *channels;
5430 int ret;
5431
5432 SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
5433
5434 SET_IEEE80211_DEV(ar->hw, ar->dev);
5435
5436 ht_cap = ath10k_get_ht_cap(ar);
5437 vht_cap = ath10k_create_vht_cap(ar);
5438
5439 if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
5440 channels = kmemdup(ath10k_2ghz_channels,
5441 sizeof(ath10k_2ghz_channels),
5442 GFP_KERNEL);
Michal Kaziord6015b22013-07-22 14:13:30 +02005443 if (!channels) {
5444 ret = -ENOMEM;
5445 goto err_free;
5446 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03005447
5448 band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
5449 band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
5450 band->channels = channels;
5451 band->n_bitrates = ath10k_g_rates_size;
5452 band->bitrates = ath10k_g_rates;
5453 band->ht_cap = ht_cap;
5454
Yanbo Lid68bb122015-01-23 08:18:20 +08005455 /* Enable the VHT support at 2.4 GHz */
5456 band->vht_cap = vht_cap;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005457
5458 ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
5459 }
5460
5461 if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
5462 channels = kmemdup(ath10k_5ghz_channels,
5463 sizeof(ath10k_5ghz_channels),
5464 GFP_KERNEL);
5465 if (!channels) {
Michal Kaziord6015b22013-07-22 14:13:30 +02005466 ret = -ENOMEM;
5467 goto err_free;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005468 }
5469
5470 band = &ar->mac.sbands[IEEE80211_BAND_5GHZ];
5471 band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
5472 band->channels = channels;
5473 band->n_bitrates = ath10k_a_rates_size;
5474 band->bitrates = ath10k_a_rates;
5475 band->ht_cap = ht_cap;
5476 band->vht_cap = vht_cap;
5477 ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
5478 }
5479
5480 ar->hw->wiphy->interface_modes =
5481 BIT(NL80211_IFTYPE_STATION) |
Bartosz Markowskid3541812013-12-10 16:20:40 +01005482 BIT(NL80211_IFTYPE_AP);
5483
Ben Greear46acf7b2014-05-16 17:15:38 +03005484 ar->hw->wiphy->available_antennas_rx = ar->supp_rx_chainmask;
5485 ar->hw->wiphy->available_antennas_tx = ar->supp_tx_chainmask;
5486
Bartosz Markowskid3541812013-12-10 16:20:40 +01005487 if (!test_bit(ATH10K_FW_FEATURE_NO_P2P, ar->fw_features))
5488 ar->hw->wiphy->interface_modes |=
Michal Kazior75d2bd42014-12-12 12:41:39 +01005489 BIT(NL80211_IFTYPE_P2P_DEVICE) |
Bartosz Markowskid3541812013-12-10 16:20:40 +01005490 BIT(NL80211_IFTYPE_P2P_CLIENT) |
5491 BIT(NL80211_IFTYPE_P2P_GO);
Kalle Valo5e3dd152013-06-12 20:52:10 +03005492
5493 ar->hw->flags = IEEE80211_HW_SIGNAL_DBM |
5494 IEEE80211_HW_SUPPORTS_PS |
5495 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
Kalle Valo5e3dd152013-06-12 20:52:10 +03005496 IEEE80211_HW_MFP_CAPABLE |
5497 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
5498 IEEE80211_HW_HAS_RATE_CONTROL |
Janusz Dziedzic2f0f1122014-02-26 18:42:09 +02005499 IEEE80211_HW_AP_LINK_PS |
Johannes Berg3cb10942015-01-22 21:38:45 +01005500 IEEE80211_HW_SPECTRUM_MGMT |
Johannes Berg41fbf6e2015-04-10 14:18:40 +02005501 IEEE80211_HW_SW_CRYPTO_CONTROL |
5502 IEEE80211_HW_SUPPORT_FAST_XMIT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005503
Eliad Peller0d8614b2014-09-10 14:07:36 +03005504 ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
5505
Kalle Valo5e3dd152013-06-12 20:52:10 +03005506 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
Eliad Peller0d8614b2014-09-10 14:07:36 +03005507 ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005508
5509 if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
5510 ar->hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
5511 ar->hw->flags |= IEEE80211_HW_TX_AMPDU_SETUP_IN_HW;
5512 }
5513
5514 ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
5515 ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
5516
5517 ar->hw->vif_data_size = sizeof(struct ath10k_vif);
Michal Kazior9797feb2014-02-14 14:49:48 +01005518 ar->hw->sta_data_size = sizeof(struct ath10k_sta);
Kalle Valo5e3dd152013-06-12 20:52:10 +03005519
Kalle Valo5e3dd152013-06-12 20:52:10 +03005520 ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
5521
Michal Kaziorfbb8f1b2015-01-13 16:30:12 +02005522 if (test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)) {
5523 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
5524
5525 /* Firmware delivers WPS/P2P Probe Requests frames to driver so
5526 * that userspace (e.g. wpa_supplicant/hostapd) can generate
5527 * correct Probe Responses. This is more of a hack advert..
5528 */
5529 ar->hw->wiphy->probe_resp_offload |=
5530 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
5531 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
5532 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
5533 }
5534
Kalle Valo5e3dd152013-06-12 20:52:10 +03005535 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
Michal Kaziorc2df44b2014-01-23 11:38:26 +01005536 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005537 ar->hw->wiphy->max_remain_on_channel_duration = 5000;
5538
5539 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
Rajkumar Manoharan78157a12014-11-17 16:44:15 +02005540 ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE;
5541
Kalle Valo5e3dd152013-06-12 20:52:10 +03005542 /*
5543 * on LL hardware queues are managed entirely by the FW
5544 * so we only advertise to mac we can do the queues thing
5545 */
5546 ar->hw->queues = 4;
5547
Kalle Valo5cc7caf2014-12-17 12:20:54 +02005548 switch (ar->wmi.op_version) {
5549 case ATH10K_FW_WMI_OP_VERSION_MAIN:
5550 case ATH10K_FW_WMI_OP_VERSION_TLV:
Bartosz Markowskif2595092013-12-10 16:20:39 +01005551 ar->hw->wiphy->iface_combinations = ath10k_if_comb;
5552 ar->hw->wiphy->n_iface_combinations =
5553 ARRAY_SIZE(ath10k_if_comb);
Michal Kaziorcf850d12014-07-24 20:07:00 +03005554 ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
Kalle Valo5cc7caf2014-12-17 12:20:54 +02005555 break;
5556 case ATH10K_FW_WMI_OP_VERSION_10_1:
5557 case ATH10K_FW_WMI_OP_VERSION_10_2:
Rajkumar Manoharan4a16fbe2014-12-17 12:21:12 +02005558 case ATH10K_FW_WMI_OP_VERSION_10_2_4:
Kalle Valo5cc7caf2014-12-17 12:20:54 +02005559 ar->hw->wiphy->iface_combinations = ath10k_10x_if_comb;
5560 ar->hw->wiphy->n_iface_combinations =
5561 ARRAY_SIZE(ath10k_10x_if_comb);
5562 break;
5563 case ATH10K_FW_WMI_OP_VERSION_UNSET:
5564 case ATH10K_FW_WMI_OP_VERSION_MAX:
5565 WARN_ON(1);
5566 ret = -EINVAL;
5567 goto err_free;
Bartosz Markowskif2595092013-12-10 16:20:39 +01005568 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03005569
Michal Kazior7c199992013-07-31 10:47:57 +02005570 ar->hw->netdev_features = NETIF_F_HW_CSUM;
5571
Janusz Dziedzic9702c682013-11-20 09:59:41 +02005572 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED)) {
5573 /* Init ath dfs pattern detector */
5574 ar->ath_common.debug_mask = ATH_DBG_DFS;
5575 ar->dfs_detector = dfs_pattern_detector_init(&ar->ath_common,
5576 NL80211_DFS_UNSET);
5577
5578 if (!ar->dfs_detector)
Michal Kazior7aa7a722014-08-25 12:09:38 +02005579 ath10k_warn(ar, "failed to initialise DFS pattern detector\n");
Janusz Dziedzic9702c682013-11-20 09:59:41 +02005580 }
5581
Kalle Valo5e3dd152013-06-12 20:52:10 +03005582 ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
5583 ath10k_reg_notifier);
5584 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02005585 ath10k_err(ar, "failed to initialise regulatory: %i\n", ret);
Michal Kaziord6015b22013-07-22 14:13:30 +02005586 goto err_free;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005587 }
5588
Johannes Berg3cb10942015-01-22 21:38:45 +01005589 ar->hw->wiphy->cipher_suites = cipher_suites;
5590 ar->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
5591
Kalle Valo5e3dd152013-06-12 20:52:10 +03005592 ret = ieee80211_register_hw(ar->hw);
5593 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02005594 ath10k_err(ar, "failed to register ieee80211: %d\n", ret);
Michal Kaziord6015b22013-07-22 14:13:30 +02005595 goto err_free;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005596 }
5597
5598 if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
5599 ret = regulatory_hint(ar->hw->wiphy,
5600 ar->ath_common.regulatory.alpha2);
5601 if (ret)
Michal Kaziord6015b22013-07-22 14:13:30 +02005602 goto err_unregister;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005603 }
5604
5605 return 0;
Michal Kaziord6015b22013-07-22 14:13:30 +02005606
5607err_unregister:
Kalle Valo5e3dd152013-06-12 20:52:10 +03005608 ieee80211_unregister_hw(ar->hw);
Michal Kaziord6015b22013-07-22 14:13:30 +02005609err_free:
5610 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
5611 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
5612
Kalle Valo5e3dd152013-06-12 20:52:10 +03005613 return ret;
5614}
5615
5616void ath10k_mac_unregister(struct ath10k *ar)
5617{
5618 ieee80211_unregister_hw(ar->hw);
5619
Janusz Dziedzic9702c682013-11-20 09:59:41 +02005620 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
5621 ar->dfs_detector->exit(ar->dfs_detector);
5622
Kalle Valo5e3dd152013-06-12 20:52:10 +03005623 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
5624 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
5625
5626 SET_IEEE80211_DEV(ar->hw, NULL);
5627}