blob: 42f6a4ddeb5eb867293c39863c95911d082bb566 [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,
40 const u8 *macaddr)
41{
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 Kwaczynskieeab2662014-05-14 16:56:17 +030061 if (arvif->vdev_type == WMI_VDEV_TYPE_AP)
62 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
63 else
64 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
Kalle Valo5e3dd152013-06-12 20:52:10 +030065 break;
66 case WLAN_CIPHER_SUITE_TKIP:
Kalle Valo5e3dd152013-06-12 20:52:10 +030067 arg.key_cipher = WMI_CIPHER_TKIP;
68 arg.key_txmic_len = 8;
69 arg.key_rxmic_len = 8;
70 break;
71 case WLAN_CIPHER_SUITE_WEP40:
72 case WLAN_CIPHER_SUITE_WEP104:
73 arg.key_cipher = WMI_CIPHER_WEP;
74 /* AP/IBSS mode requires self-key to be groupwise
75 * Otherwise pairwise key must be set */
76 if (memcmp(macaddr, arvif->vif->addr, ETH_ALEN))
77 arg.key_flags = WMI_KEY_PAIRWISE;
78 break;
79 default:
Michal Kazior7aa7a722014-08-25 12:09:38 +020080 ath10k_warn(ar, "cipher %d is not supported\n", key->cipher);
Kalle Valo5e3dd152013-06-12 20:52:10 +030081 return -EOPNOTSUPP;
82 }
83
84 if (cmd == DISABLE_KEY) {
85 arg.key_cipher = WMI_CIPHER_NONE;
86 arg.key_data = NULL;
87 }
88
89 return ath10k_wmi_vdev_install_key(arvif->ar, &arg);
90}
91
92static int ath10k_install_key(struct ath10k_vif *arvif,
93 struct ieee80211_key_conf *key,
94 enum set_key_cmd cmd,
95 const u8 *macaddr)
96{
97 struct ath10k *ar = arvif->ar;
98 int ret;
99
Michal Kazior548db542013-07-05 16:15:15 +0300100 lockdep_assert_held(&ar->conf_mutex);
101
Wolfram Sang16735d02013-11-14 14:32:02 -0800102 reinit_completion(&ar->install_key_done);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300103
104 ret = ath10k_send_key(arvif, key, cmd, macaddr);
105 if (ret)
106 return ret;
107
108 ret = wait_for_completion_timeout(&ar->install_key_done, 3*HZ);
109 if (ret == 0)
110 return -ETIMEDOUT;
111
112 return 0;
113}
114
115static int ath10k_install_peer_wep_keys(struct ath10k_vif *arvif,
116 const u8 *addr)
117{
118 struct ath10k *ar = arvif->ar;
119 struct ath10k_peer *peer;
120 int ret;
121 int i;
122
123 lockdep_assert_held(&ar->conf_mutex);
124
125 spin_lock_bh(&ar->data_lock);
126 peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
127 spin_unlock_bh(&ar->data_lock);
128
129 if (!peer)
130 return -ENOENT;
131
132 for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
133 if (arvif->wep_keys[i] == NULL)
134 continue;
135
136 ret = ath10k_install_key(arvif, arvif->wep_keys[i], SET_KEY,
137 addr);
138 if (ret)
139 return ret;
140
Sujith Manoharanae167132014-11-25 11:46:59 +0530141 spin_lock_bh(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300142 peer->keys[i] = arvif->wep_keys[i];
Sujith Manoharanae167132014-11-25 11:46:59 +0530143 spin_unlock_bh(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300144 }
145
146 return 0;
147}
148
149static int ath10k_clear_peer_keys(struct ath10k_vif *arvif,
150 const u8 *addr)
151{
152 struct ath10k *ar = arvif->ar;
153 struct ath10k_peer *peer;
154 int first_errno = 0;
155 int ret;
156 int i;
157
158 lockdep_assert_held(&ar->conf_mutex);
159
160 spin_lock_bh(&ar->data_lock);
161 peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
162 spin_unlock_bh(&ar->data_lock);
163
164 if (!peer)
165 return -ENOENT;
166
167 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
168 if (peer->keys[i] == NULL)
169 continue;
170
171 ret = ath10k_install_key(arvif, peer->keys[i],
172 DISABLE_KEY, addr);
173 if (ret && first_errno == 0)
174 first_errno = ret;
175
176 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200177 ath10k_warn(ar, "failed to remove peer wep key %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300178 i, ret);
179
Sujith Manoharanae167132014-11-25 11:46:59 +0530180 spin_lock_bh(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300181 peer->keys[i] = NULL;
Sujith Manoharanae167132014-11-25 11:46:59 +0530182 spin_unlock_bh(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300183 }
184
185 return first_errno;
186}
187
Sujith Manoharan504f6cd2014-11-25 11:46:58 +0530188bool ath10k_mac_is_peer_wep_key_set(struct ath10k *ar, const u8 *addr,
189 u8 keyidx)
190{
191 struct ath10k_peer *peer;
192 int i;
193
194 lockdep_assert_held(&ar->data_lock);
195
196 /* We don't know which vdev this peer belongs to,
197 * since WMI doesn't give us that information.
198 *
199 * FIXME: multi-bss needs to be handled.
200 */
201 peer = ath10k_peer_find(ar, 0, addr);
202 if (!peer)
203 return false;
204
205 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
206 if (peer->keys[i] && peer->keys[i]->keyidx == keyidx)
207 return true;
208 }
209
210 return false;
211}
212
Kalle Valo5e3dd152013-06-12 20:52:10 +0300213static int ath10k_clear_vdev_key(struct ath10k_vif *arvif,
214 struct ieee80211_key_conf *key)
215{
216 struct ath10k *ar = arvif->ar;
217 struct ath10k_peer *peer;
218 u8 addr[ETH_ALEN];
219 int first_errno = 0;
220 int ret;
221 int i;
222
223 lockdep_assert_held(&ar->conf_mutex);
224
225 for (;;) {
226 /* since ath10k_install_key we can't hold data_lock all the
227 * time, so we try to remove the keys incrementally */
228 spin_lock_bh(&ar->data_lock);
229 i = 0;
230 list_for_each_entry(peer, &ar->peers, list) {
231 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
232 if (peer->keys[i] == key) {
Kalle Valob25f32c2014-09-14 12:50:49 +0300233 ether_addr_copy(addr, peer->addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300234 peer->keys[i] = NULL;
235 break;
236 }
237 }
238
239 if (i < ARRAY_SIZE(peer->keys))
240 break;
241 }
242 spin_unlock_bh(&ar->data_lock);
243
244 if (i == ARRAY_SIZE(peer->keys))
245 break;
246
247 ret = ath10k_install_key(arvif, key, DISABLE_KEY, addr);
248 if (ret && first_errno == 0)
249 first_errno = ret;
250
251 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200252 ath10k_warn(ar, "failed to remove key for %pM: %d\n",
Kalle Valobe6546f2014-03-25 14:18:51 +0200253 addr, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300254 }
255
256 return first_errno;
257}
258
Kalle Valo5e3dd152013-06-12 20:52:10 +0300259/*********************/
260/* General utilities */
261/*********************/
262
263static inline enum wmi_phy_mode
264chan_to_phymode(const struct cfg80211_chan_def *chandef)
265{
266 enum wmi_phy_mode phymode = MODE_UNKNOWN;
267
268 switch (chandef->chan->band) {
269 case IEEE80211_BAND_2GHZ:
270 switch (chandef->width) {
271 case NL80211_CHAN_WIDTH_20_NOHT:
272 phymode = MODE_11G;
273 break;
274 case NL80211_CHAN_WIDTH_20:
275 phymode = MODE_11NG_HT20;
276 break;
277 case NL80211_CHAN_WIDTH_40:
278 phymode = MODE_11NG_HT40;
279 break;
John W. Linville0f817ed2013-06-27 13:50:09 -0400280 case NL80211_CHAN_WIDTH_5:
281 case NL80211_CHAN_WIDTH_10:
Kalle Valo5e3dd152013-06-12 20:52:10 +0300282 case NL80211_CHAN_WIDTH_80:
283 case NL80211_CHAN_WIDTH_80P80:
284 case NL80211_CHAN_WIDTH_160:
285 phymode = MODE_UNKNOWN;
286 break;
287 }
288 break;
289 case IEEE80211_BAND_5GHZ:
290 switch (chandef->width) {
291 case NL80211_CHAN_WIDTH_20_NOHT:
292 phymode = MODE_11A;
293 break;
294 case NL80211_CHAN_WIDTH_20:
295 phymode = MODE_11NA_HT20;
296 break;
297 case NL80211_CHAN_WIDTH_40:
298 phymode = MODE_11NA_HT40;
299 break;
300 case NL80211_CHAN_WIDTH_80:
301 phymode = MODE_11AC_VHT80;
302 break;
John W. Linville0f817ed2013-06-27 13:50:09 -0400303 case NL80211_CHAN_WIDTH_5:
304 case NL80211_CHAN_WIDTH_10:
Kalle Valo5e3dd152013-06-12 20:52:10 +0300305 case NL80211_CHAN_WIDTH_80P80:
306 case NL80211_CHAN_WIDTH_160:
307 phymode = MODE_UNKNOWN;
308 break;
309 }
310 break;
311 default:
312 break;
313 }
314
315 WARN_ON(phymode == MODE_UNKNOWN);
316 return phymode;
317}
318
319static u8 ath10k_parse_mpdudensity(u8 mpdudensity)
320{
321/*
322 * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
323 * 0 for no restriction
324 * 1 for 1/4 us
325 * 2 for 1/2 us
326 * 3 for 1 us
327 * 4 for 2 us
328 * 5 for 4 us
329 * 6 for 8 us
330 * 7 for 16 us
331 */
332 switch (mpdudensity) {
333 case 0:
334 return 0;
335 case 1:
336 case 2:
337 case 3:
338 /* Our lower layer calculations limit our precision to
339 1 microsecond */
340 return 1;
341 case 4:
342 return 2;
343 case 5:
344 return 4;
345 case 6:
346 return 8;
347 case 7:
348 return 16;
349 default:
350 return 0;
351 }
352}
353
354static int ath10k_peer_create(struct ath10k *ar, u32 vdev_id, const u8 *addr)
355{
356 int ret;
357
358 lockdep_assert_held(&ar->conf_mutex);
359
Michal Kaziorcfd10612014-11-25 15:16:05 +0100360 if (ar->num_peers >= ar->max_num_peers)
361 return -ENOBUFS;
362
Kalle Valo5e3dd152013-06-12 20:52:10 +0300363 ret = ath10k_wmi_peer_create(ar, vdev_id, addr);
Ben Greear479398b2013-11-04 09:19:34 -0800364 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200365 ath10k_warn(ar, "failed to create wmi peer %pM on vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +0200366 addr, vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300367 return ret;
Ben Greear479398b2013-11-04 09:19:34 -0800368 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300369
370 ret = ath10k_wait_for_peer_created(ar, vdev_id, addr);
Ben Greear479398b2013-11-04 09:19:34 -0800371 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200372 ath10k_warn(ar, "failed to wait for created wmi peer %pM on vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +0200373 addr, vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300374 return ret;
Ben Greear479398b2013-11-04 09:19:34 -0800375 }
Michal Kazior292a7532014-11-25 15:16:04 +0100376
Bartosz Markowski0e759f32014-01-02 14:38:33 +0100377 ar->num_peers++;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300378
379 return 0;
380}
381
Kalle Valo5a13e762014-01-20 11:01:46 +0200382static int ath10k_mac_set_kickout(struct ath10k_vif *arvif)
383{
384 struct ath10k *ar = arvif->ar;
385 u32 param;
386 int ret;
387
388 param = ar->wmi.pdev_param->sta_kickout_th;
389 ret = ath10k_wmi_pdev_set_param(ar, param,
390 ATH10K_KICKOUT_THRESHOLD);
391 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200392 ath10k_warn(ar, "failed to set kickout threshold on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200393 arvif->vdev_id, ret);
Kalle Valo5a13e762014-01-20 11:01:46 +0200394 return ret;
395 }
396
397 param = ar->wmi.vdev_param->ap_keepalive_min_idle_inactive_time_secs;
398 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
399 ATH10K_KEEPALIVE_MIN_IDLE);
400 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200401 ath10k_warn(ar, "failed to set keepalive minimum idle time on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200402 arvif->vdev_id, ret);
Kalle Valo5a13e762014-01-20 11:01:46 +0200403 return ret;
404 }
405
406 param = ar->wmi.vdev_param->ap_keepalive_max_idle_inactive_time_secs;
407 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
408 ATH10K_KEEPALIVE_MAX_IDLE);
409 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200410 ath10k_warn(ar, "failed to set keepalive maximum idle time on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200411 arvif->vdev_id, ret);
Kalle Valo5a13e762014-01-20 11:01:46 +0200412 return ret;
413 }
414
415 param = ar->wmi.vdev_param->ap_keepalive_max_unresponsive_time_secs;
416 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
417 ATH10K_KEEPALIVE_MAX_UNRESPONSIVE);
418 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200419 ath10k_warn(ar, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200420 arvif->vdev_id, ret);
Kalle Valo5a13e762014-01-20 11:01:46 +0200421 return ret;
422 }
423
424 return 0;
425}
426
Vivek Natarajanacab6402014-11-26 09:06:12 +0200427static int ath10k_mac_set_rts(struct ath10k_vif *arvif, u32 value)
Michal Kazior424121c2013-07-22 14:13:31 +0200428{
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200429 struct ath10k *ar = arvif->ar;
430 u32 vdev_param;
431
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200432 vdev_param = ar->wmi.vdev_param->rts_threshold;
433 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
Michal Kazior424121c2013-07-22 14:13:31 +0200434}
435
436static int ath10k_mac_set_frag(struct ath10k_vif *arvif, u32 value)
437{
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200438 struct ath10k *ar = arvif->ar;
439 u32 vdev_param;
440
Michal Kazior424121c2013-07-22 14:13:31 +0200441 if (value != 0xFFFFFFFF)
442 value = clamp_t(u32, arvif->ar->hw->wiphy->frag_threshold,
443 ATH10K_FRAGMT_THRESHOLD_MIN,
444 ATH10K_FRAGMT_THRESHOLD_MAX);
445
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200446 vdev_param = ar->wmi.vdev_param->fragmentation_threshold;
447 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
Michal Kazior424121c2013-07-22 14:13:31 +0200448}
449
Kalle Valo5e3dd152013-06-12 20:52:10 +0300450static int ath10k_peer_delete(struct ath10k *ar, u32 vdev_id, const u8 *addr)
451{
452 int ret;
453
454 lockdep_assert_held(&ar->conf_mutex);
455
456 ret = ath10k_wmi_peer_delete(ar, vdev_id, addr);
457 if (ret)
458 return ret;
459
460 ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
461 if (ret)
462 return ret;
463
Bartosz Markowski0e759f32014-01-02 14:38:33 +0100464 ar->num_peers--;
Bartosz Markowski0e759f32014-01-02 14:38:33 +0100465
Kalle Valo5e3dd152013-06-12 20:52:10 +0300466 return 0;
467}
468
469static void ath10k_peer_cleanup(struct ath10k *ar, u32 vdev_id)
470{
471 struct ath10k_peer *peer, *tmp;
472
473 lockdep_assert_held(&ar->conf_mutex);
474
475 spin_lock_bh(&ar->data_lock);
476 list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
477 if (peer->vdev_id != vdev_id)
478 continue;
479
Michal Kazior7aa7a722014-08-25 12:09:38 +0200480 ath10k_warn(ar, "removing stale peer %pM from vdev_id %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300481 peer->addr, vdev_id);
482
483 list_del(&peer->list);
484 kfree(peer);
Bartosz Markowski0e759f32014-01-02 14:38:33 +0100485 ar->num_peers--;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300486 }
487 spin_unlock_bh(&ar->data_lock);
488}
489
Michal Kaziora96d7742013-07-16 09:38:56 +0200490static void ath10k_peer_cleanup_all(struct ath10k *ar)
491{
492 struct ath10k_peer *peer, *tmp;
493
494 lockdep_assert_held(&ar->conf_mutex);
495
496 spin_lock_bh(&ar->data_lock);
497 list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
498 list_del(&peer->list);
499 kfree(peer);
500 }
501 spin_unlock_bh(&ar->data_lock);
Michal Kazior292a7532014-11-25 15:16:04 +0100502
503 ar->num_peers = 0;
Michal Kaziorcfd10612014-11-25 15:16:05 +0100504 ar->num_stations = 0;
Michal Kaziora96d7742013-07-16 09:38:56 +0200505}
506
Kalle Valo5e3dd152013-06-12 20:52:10 +0300507/************************/
508/* Interface management */
509/************************/
510
Michal Kazior64badcb2014-09-18 11:18:02 +0300511void ath10k_mac_vif_beacon_free(struct ath10k_vif *arvif)
512{
513 struct ath10k *ar = arvif->ar;
514
515 lockdep_assert_held(&ar->data_lock);
516
517 if (!arvif->beacon)
518 return;
519
520 if (!arvif->beacon_buf)
521 dma_unmap_single(ar->dev, ATH10K_SKB_CB(arvif->beacon)->paddr,
522 arvif->beacon->len, DMA_TO_DEVICE);
523
524 dev_kfree_skb_any(arvif->beacon);
525
526 arvif->beacon = NULL;
527 arvif->beacon_sent = false;
528}
529
530static void ath10k_mac_vif_beacon_cleanup(struct ath10k_vif *arvif)
531{
532 struct ath10k *ar = arvif->ar;
533
534 lockdep_assert_held(&ar->data_lock);
535
536 ath10k_mac_vif_beacon_free(arvif);
537
538 if (arvif->beacon_buf) {
539 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
540 arvif->beacon_buf, arvif->beacon_paddr);
541 arvif->beacon_buf = NULL;
542 }
543}
544
Kalle Valo5e3dd152013-06-12 20:52:10 +0300545static inline int ath10k_vdev_setup_sync(struct ath10k *ar)
546{
547 int ret;
548
Michal Kazior548db542013-07-05 16:15:15 +0300549 lockdep_assert_held(&ar->conf_mutex);
550
Michal Kazior7962b0d2014-10-28 10:34:38 +0100551 if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags))
552 return -ESHUTDOWN;
553
Kalle Valo5e3dd152013-06-12 20:52:10 +0300554 ret = wait_for_completion_timeout(&ar->vdev_setup_done,
555 ATH10K_VDEV_SETUP_TIMEOUT_HZ);
556 if (ret == 0)
557 return -ETIMEDOUT;
558
559 return 0;
560}
561
Michal Kazior1bbc0972014-04-08 09:45:47 +0300562static int ath10k_monitor_vdev_start(struct ath10k *ar, int vdev_id)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300563{
Michal Kaziorc930f742014-01-23 11:38:25 +0100564 struct cfg80211_chan_def *chandef = &ar->chandef;
565 struct ieee80211_channel *channel = chandef->chan;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300566 struct wmi_vdev_start_request_arg arg = {};
Kalle Valo5e3dd152013-06-12 20:52:10 +0300567 int ret = 0;
568
569 lockdep_assert_held(&ar->conf_mutex);
570
Kalle Valo5e3dd152013-06-12 20:52:10 +0300571 arg.vdev_id = vdev_id;
572 arg.channel.freq = channel->center_freq;
Michal Kaziorc930f742014-01-23 11:38:25 +0100573 arg.channel.band_center_freq1 = chandef->center_freq1;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300574
575 /* TODO setup this dynamically, what in case we
576 don't have any vifs? */
Michal Kaziorc930f742014-01-23 11:38:25 +0100577 arg.channel.mode = chan_to_phymode(chandef);
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200578 arg.channel.chan_radar =
579 !!(channel->flags & IEEE80211_CHAN_RADAR);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300580
Michal Kazior89c5c842013-10-23 04:02:13 -0700581 arg.channel.min_power = 0;
Michal Kazior02256932013-10-23 04:02:14 -0700582 arg.channel.max_power = channel->max_power * 2;
583 arg.channel.max_reg_power = channel->max_reg_power * 2;
584 arg.channel.max_antenna_gain = channel->max_antenna_gain * 2;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300585
Michal Kazior7962b0d2014-10-28 10:34:38 +0100586 reinit_completion(&ar->vdev_setup_done);
587
Kalle Valo5e3dd152013-06-12 20:52:10 +0300588 ret = ath10k_wmi_vdev_start(ar, &arg);
589 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200590 ath10k_warn(ar, "failed to request monitor vdev %i start: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200591 vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300592 return ret;
593 }
594
595 ret = ath10k_vdev_setup_sync(ar);
596 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200597 ath10k_warn(ar, "failed to synchronize setup for monitor vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200598 vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300599 return ret;
600 }
601
602 ret = ath10k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
603 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200604 ath10k_warn(ar, "failed to put up monitor vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200605 vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300606 goto vdev_stop;
607 }
608
609 ar->monitor_vdev_id = vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300610
Michal Kazior7aa7a722014-08-25 12:09:38 +0200611 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i started\n",
Michal Kazior1bbc0972014-04-08 09:45:47 +0300612 ar->monitor_vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300613 return 0;
614
615vdev_stop:
616 ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
617 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200618 ath10k_warn(ar, "failed to stop monitor vdev %i after start failure: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200619 ar->monitor_vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300620
621 return ret;
622}
623
Michal Kazior1bbc0972014-04-08 09:45:47 +0300624static int ath10k_monitor_vdev_stop(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300625{
626 int ret = 0;
627
628 lockdep_assert_held(&ar->conf_mutex);
629
Marek Puzyniak52fa0192013-09-24 14:06:24 +0200630 ret = ath10k_wmi_vdev_down(ar, ar->monitor_vdev_id);
631 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200632 ath10k_warn(ar, "failed to put down monitor vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200633 ar->monitor_vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300634
Michal Kazior7962b0d2014-10-28 10:34:38 +0100635 reinit_completion(&ar->vdev_setup_done);
636
Kalle Valo5e3dd152013-06-12 20:52:10 +0300637 ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
638 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200639 ath10k_warn(ar, "failed to to request monitor vdev %i stop: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200640 ar->monitor_vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300641
642 ret = ath10k_vdev_setup_sync(ar);
643 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200644 ath10k_warn(ar, "failed to synchronise monitor vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200645 ar->monitor_vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300646
Michal Kazior7aa7a722014-08-25 12:09:38 +0200647 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i stopped\n",
Michal Kazior1bbc0972014-04-08 09:45:47 +0300648 ar->monitor_vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300649 return ret;
650}
651
Michal Kazior1bbc0972014-04-08 09:45:47 +0300652static int ath10k_monitor_vdev_create(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300653{
654 int bit, ret = 0;
655
656 lockdep_assert_held(&ar->conf_mutex);
657
Ben Greeara9aefb32014-08-12 11:02:19 +0300658 if (ar->free_vdev_map == 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200659 ath10k_warn(ar, "failed to find free vdev id for monitor vdev\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +0300660 return -ENOMEM;
661 }
662
Ben Greear16c11172014-09-23 14:17:16 -0700663 bit = __ffs64(ar->free_vdev_map);
Ben Greeara9aefb32014-08-12 11:02:19 +0300664
Ben Greear16c11172014-09-23 14:17:16 -0700665 ar->monitor_vdev_id = bit;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300666
667 ret = ath10k_wmi_vdev_create(ar, ar->monitor_vdev_id,
668 WMI_VDEV_TYPE_MONITOR,
669 0, ar->mac_addr);
670 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200671 ath10k_warn(ar, "failed to request monitor vdev %i creation: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200672 ar->monitor_vdev_id, ret);
Ben Greeara9aefb32014-08-12 11:02:19 +0300673 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300674 }
675
Ben Greear16c11172014-09-23 14:17:16 -0700676 ar->free_vdev_map &= ~(1LL << ar->monitor_vdev_id);
Michal Kazior7aa7a722014-08-25 12:09:38 +0200677 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d created\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300678 ar->monitor_vdev_id);
679
Kalle Valo5e3dd152013-06-12 20:52:10 +0300680 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300681}
682
Michal Kazior1bbc0972014-04-08 09:45:47 +0300683static int ath10k_monitor_vdev_delete(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300684{
685 int ret = 0;
686
687 lockdep_assert_held(&ar->conf_mutex);
688
Kalle Valo5e3dd152013-06-12 20:52:10 +0300689 ret = ath10k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
690 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200691 ath10k_warn(ar, "failed to request wmi monitor vdev %i removal: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200692 ar->monitor_vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300693 return ret;
694 }
695
Ben Greear16c11172014-09-23 14:17:16 -0700696 ar->free_vdev_map |= 1LL << ar->monitor_vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300697
Michal Kazior7aa7a722014-08-25 12:09:38 +0200698 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d deleted\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300699 ar->monitor_vdev_id);
700 return ret;
701}
702
Michal Kazior1bbc0972014-04-08 09:45:47 +0300703static int ath10k_monitor_start(struct ath10k *ar)
704{
705 int ret;
706
707 lockdep_assert_held(&ar->conf_mutex);
708
Michal Kazior1bbc0972014-04-08 09:45:47 +0300709 ret = ath10k_monitor_vdev_create(ar);
710 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200711 ath10k_warn(ar, "failed to create monitor vdev: %d\n", ret);
Michal Kazior1bbc0972014-04-08 09:45:47 +0300712 return ret;
713 }
714
715 ret = ath10k_monitor_vdev_start(ar, ar->monitor_vdev_id);
716 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200717 ath10k_warn(ar, "failed to start monitor vdev: %d\n", ret);
Michal Kazior1bbc0972014-04-08 09:45:47 +0300718 ath10k_monitor_vdev_delete(ar);
719 return ret;
720 }
721
722 ar->monitor_started = true;
Michal Kazior7aa7a722014-08-25 12:09:38 +0200723 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor started\n");
Michal Kazior1bbc0972014-04-08 09:45:47 +0300724
725 return 0;
726}
727
Michal Kazior19337472014-08-28 12:58:16 +0200728static int ath10k_monitor_stop(struct ath10k *ar)
Michal Kazior1bbc0972014-04-08 09:45:47 +0300729{
730 int ret;
731
732 lockdep_assert_held(&ar->conf_mutex);
733
Michal Kazior1bbc0972014-04-08 09:45:47 +0300734 ret = ath10k_monitor_vdev_stop(ar);
Michal Kazior19337472014-08-28 12:58:16 +0200735 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200736 ath10k_warn(ar, "failed to stop monitor vdev: %d\n", ret);
Michal Kazior19337472014-08-28 12:58:16 +0200737 return ret;
738 }
Michal Kazior1bbc0972014-04-08 09:45:47 +0300739
740 ret = ath10k_monitor_vdev_delete(ar);
Michal Kazior19337472014-08-28 12:58:16 +0200741 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200742 ath10k_warn(ar, "failed to delete monitor vdev: %d\n", ret);
Michal Kazior19337472014-08-28 12:58:16 +0200743 return ret;
744 }
Michal Kazior1bbc0972014-04-08 09:45:47 +0300745
746 ar->monitor_started = false;
Michal Kazior7aa7a722014-08-25 12:09:38 +0200747 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopped\n");
Michal Kazior19337472014-08-28 12:58:16 +0200748
749 return 0;
750}
751
752static int ath10k_monitor_recalc(struct ath10k *ar)
753{
754 bool should_start;
755
756 lockdep_assert_held(&ar->conf_mutex);
757
758 should_start = ar->monitor ||
759 ar->filter_flags & FIF_PROMISC_IN_BSS ||
760 test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
761
762 ath10k_dbg(ar, ATH10K_DBG_MAC,
763 "mac monitor recalc started? %d should? %d\n",
764 ar->monitor_started, should_start);
765
766 if (should_start == ar->monitor_started)
767 return 0;
768
769 if (should_start)
770 return ath10k_monitor_start(ar);
Kalle Valod8bb26b2014-09-14 12:50:33 +0300771
772 return ath10k_monitor_stop(ar);
Michal Kazior1bbc0972014-04-08 09:45:47 +0300773}
774
Marek Kwaczynskie81bd102014-03-11 12:58:00 +0200775static int ath10k_recalc_rtscts_prot(struct ath10k_vif *arvif)
776{
777 struct ath10k *ar = arvif->ar;
778 u32 vdev_param, rts_cts = 0;
779
780 lockdep_assert_held(&ar->conf_mutex);
781
782 vdev_param = ar->wmi.vdev_param->enable_rtscts;
783
784 if (arvif->use_cts_prot || arvif->num_legacy_stations > 0)
785 rts_cts |= SM(WMI_RTSCTS_ENABLED, WMI_RTSCTS_SET);
786
787 if (arvif->num_legacy_stations > 0)
788 rts_cts |= SM(WMI_RTSCTS_ACROSS_SW_RETRIES,
789 WMI_RTSCTS_PROFILE);
790
791 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
792 rts_cts);
793}
794
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200795static int ath10k_start_cac(struct ath10k *ar)
796{
797 int ret;
798
799 lockdep_assert_held(&ar->conf_mutex);
800
801 set_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
802
Michal Kazior19337472014-08-28 12:58:16 +0200803 ret = ath10k_monitor_recalc(ar);
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200804 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200805 ath10k_warn(ar, "failed to start monitor (cac): %d\n", ret);
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200806 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
807 return ret;
808 }
809
Michal Kazior7aa7a722014-08-25 12:09:38 +0200810 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac start monitor vdev %d\n",
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200811 ar->monitor_vdev_id);
812
813 return 0;
814}
815
816static int ath10k_stop_cac(struct ath10k *ar)
817{
818 lockdep_assert_held(&ar->conf_mutex);
819
820 /* CAC is not running - do nothing */
821 if (!test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags))
822 return 0;
823
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200824 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
Michal Kazior1bbc0972014-04-08 09:45:47 +0300825 ath10k_monitor_stop(ar);
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200826
Michal Kazior7aa7a722014-08-25 12:09:38 +0200827 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac finished\n");
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200828
829 return 0;
830}
831
Michal Kaziord6500972014-04-08 09:56:09 +0300832static void ath10k_recalc_radar_detection(struct ath10k *ar)
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200833{
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200834 int ret;
835
836 lockdep_assert_held(&ar->conf_mutex);
837
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200838 ath10k_stop_cac(ar);
839
Michal Kaziord6500972014-04-08 09:56:09 +0300840 if (!ar->radar_enabled)
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200841 return;
842
Michal Kaziord6500972014-04-08 09:56:09 +0300843 if (ar->num_started_vdevs > 0)
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200844 return;
845
846 ret = ath10k_start_cac(ar);
847 if (ret) {
848 /*
849 * Not possible to start CAC on current channel so starting
850 * radiation is not allowed, make this channel DFS_UNAVAILABLE
851 * by indicating that radar was detected.
852 */
Michal Kazior7aa7a722014-08-25 12:09:38 +0200853 ath10k_warn(ar, "failed to start CAC: %d\n", ret);
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200854 ieee80211_radar_detected(ar->hw);
855 }
856}
857
Michal Kaziordc55e302014-07-29 12:53:36 +0300858static int ath10k_vdev_start_restart(struct ath10k_vif *arvif, bool restart)
Michal Kazior72654fa2014-04-08 09:56:09 +0300859{
860 struct ath10k *ar = arvif->ar;
861 struct cfg80211_chan_def *chandef = &ar->chandef;
862 struct wmi_vdev_start_request_arg arg = {};
863 int ret = 0;
864
865 lockdep_assert_held(&ar->conf_mutex);
866
867 reinit_completion(&ar->vdev_setup_done);
868
869 arg.vdev_id = arvif->vdev_id;
870 arg.dtim_period = arvif->dtim_period;
871 arg.bcn_intval = arvif->beacon_interval;
872
873 arg.channel.freq = chandef->chan->center_freq;
874 arg.channel.band_center_freq1 = chandef->center_freq1;
875 arg.channel.mode = chan_to_phymode(chandef);
876
877 arg.channel.min_power = 0;
878 arg.channel.max_power = chandef->chan->max_power * 2;
879 arg.channel.max_reg_power = chandef->chan->max_reg_power * 2;
880 arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
881
882 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
883 arg.ssid = arvif->u.ap.ssid;
884 arg.ssid_len = arvif->u.ap.ssid_len;
885 arg.hidden_ssid = arvif->u.ap.hidden_ssid;
886
887 /* For now allow DFS for AP mode */
888 arg.channel.chan_radar =
889 !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
890 } else if (arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
891 arg.ssid = arvif->vif->bss_conf.ssid;
892 arg.ssid_len = arvif->vif->bss_conf.ssid_len;
893 }
894
Michal Kazior7aa7a722014-08-25 12:09:38 +0200895 ath10k_dbg(ar, ATH10K_DBG_MAC,
Michal Kazior72654fa2014-04-08 09:56:09 +0300896 "mac vdev %d start center_freq %d phymode %s\n",
897 arg.vdev_id, arg.channel.freq,
898 ath10k_wmi_phymode_str(arg.channel.mode));
899
Michal Kaziordc55e302014-07-29 12:53:36 +0300900 if (restart)
901 ret = ath10k_wmi_vdev_restart(ar, &arg);
902 else
903 ret = ath10k_wmi_vdev_start(ar, &arg);
904
Michal Kazior72654fa2014-04-08 09:56:09 +0300905 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200906 ath10k_warn(ar, "failed to start WMI vdev %i: %d\n",
Michal Kazior72654fa2014-04-08 09:56:09 +0300907 arg.vdev_id, ret);
908 return ret;
909 }
910
911 ret = ath10k_vdev_setup_sync(ar);
912 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200913 ath10k_warn(ar, "failed to synchronise setup for vdev %i: %d\n",
Michal Kazior72654fa2014-04-08 09:56:09 +0300914 arg.vdev_id, ret);
915 return ret;
916 }
917
Michal Kaziord6500972014-04-08 09:56:09 +0300918 ar->num_started_vdevs++;
919 ath10k_recalc_radar_detection(ar);
920
Michal Kazior72654fa2014-04-08 09:56:09 +0300921 return ret;
922}
923
Michal Kaziordc55e302014-07-29 12:53:36 +0300924static int ath10k_vdev_start(struct ath10k_vif *arvif)
925{
926 return ath10k_vdev_start_restart(arvif, false);
927}
928
929static int ath10k_vdev_restart(struct ath10k_vif *arvif)
930{
931 return ath10k_vdev_start_restart(arvif, true);
932}
933
Michal Kazior72654fa2014-04-08 09:56:09 +0300934static int ath10k_vdev_stop(struct ath10k_vif *arvif)
935{
936 struct ath10k *ar = arvif->ar;
937 int ret;
938
939 lockdep_assert_held(&ar->conf_mutex);
940
941 reinit_completion(&ar->vdev_setup_done);
942
943 ret = ath10k_wmi_vdev_stop(ar, arvif->vdev_id);
944 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200945 ath10k_warn(ar, "failed to stop WMI vdev %i: %d\n",
Michal Kazior72654fa2014-04-08 09:56:09 +0300946 arvif->vdev_id, ret);
947 return ret;
948 }
949
950 ret = ath10k_vdev_setup_sync(ar);
951 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200952 ath10k_warn(ar, "failed to syncronise setup for vdev %i: %d\n",
Michal Kazior72654fa2014-04-08 09:56:09 +0300953 arvif->vdev_id, ret);
954 return ret;
955 }
956
Michal Kaziord6500972014-04-08 09:56:09 +0300957 WARN_ON(ar->num_started_vdevs == 0);
958
959 if (ar->num_started_vdevs != 0) {
960 ar->num_started_vdevs--;
961 ath10k_recalc_radar_detection(ar);
962 }
963
Michal Kazior72654fa2014-04-08 09:56:09 +0300964 return ret;
965}
966
Kalle Valo5e3dd152013-06-12 20:52:10 +0300967static void ath10k_control_beaconing(struct ath10k_vif *arvif,
Kalle Valo5b07e072014-09-14 12:50:06 +0300968 struct ieee80211_bss_conf *info)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300969{
Michal Kazior7aa7a722014-08-25 12:09:38 +0200970 struct ath10k *ar = arvif->ar;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300971 int ret = 0;
972
Michal Kazior548db542013-07-05 16:15:15 +0300973 lockdep_assert_held(&arvif->ar->conf_mutex);
974
Kalle Valo5e3dd152013-06-12 20:52:10 +0300975 if (!info->enable_beacon) {
976 ath10k_vdev_stop(arvif);
Michal Kaziorc930f742014-01-23 11:38:25 +0100977
978 arvif->is_started = false;
979 arvif->is_up = false;
980
Michal Kazior748afc42014-01-23 12:48:21 +0100981 spin_lock_bh(&arvif->ar->data_lock);
Michal Kazior64badcb2014-09-18 11:18:02 +0300982 ath10k_mac_vif_beacon_free(arvif);
Michal Kazior748afc42014-01-23 12:48:21 +0100983 spin_unlock_bh(&arvif->ar->data_lock);
984
Kalle Valo5e3dd152013-06-12 20:52:10 +0300985 return;
986 }
987
988 arvif->tx_seq_no = 0x1000;
989
990 ret = ath10k_vdev_start(arvif);
991 if (ret)
992 return;
993
Michal Kaziorc930f742014-01-23 11:38:25 +0100994 arvif->aid = 0;
Kalle Valob25f32c2014-09-14 12:50:49 +0300995 ether_addr_copy(arvif->bssid, info->bssid);
Michal Kaziorc930f742014-01-23 11:38:25 +0100996
997 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
998 arvif->bssid);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300999 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001000 ath10k_warn(ar, "failed to bring up vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02001001 arvif->vdev_id, ret);
Michal Kaziorc930f742014-01-23 11:38:25 +01001002 ath10k_vdev_stop(arvif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001003 return;
1004 }
Michal Kaziorc930f742014-01-23 11:38:25 +01001005
1006 arvif->is_started = true;
1007 arvif->is_up = true;
1008
Michal Kazior7aa7a722014-08-25 12:09:38 +02001009 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001010}
1011
1012static void ath10k_control_ibss(struct ath10k_vif *arvif,
1013 struct ieee80211_bss_conf *info,
1014 const u8 self_peer[ETH_ALEN])
1015{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001016 struct ath10k *ar = arvif->ar;
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02001017 u32 vdev_param;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001018 int ret = 0;
1019
Michal Kazior548db542013-07-05 16:15:15 +03001020 lockdep_assert_held(&arvif->ar->conf_mutex);
1021
Kalle Valo5e3dd152013-06-12 20:52:10 +03001022 if (!info->ibss_joined) {
1023 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, self_peer);
1024 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001025 ath10k_warn(ar, "failed to delete IBSS self peer %pM for vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001026 self_peer, arvif->vdev_id, ret);
1027
Michal Kaziorc930f742014-01-23 11:38:25 +01001028 if (is_zero_ether_addr(arvif->bssid))
Kalle Valo5e3dd152013-06-12 20:52:10 +03001029 return;
1030
Michal Kaziorc930f742014-01-23 11:38:25 +01001031 memset(arvif->bssid, 0, ETH_ALEN);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001032
1033 return;
1034 }
1035
1036 ret = ath10k_peer_create(arvif->ar, arvif->vdev_id, self_peer);
1037 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001038 ath10k_warn(ar, "failed to create IBSS self peer %pM for vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001039 self_peer, arvif->vdev_id, ret);
1040 return;
1041 }
1042
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02001043 vdev_param = arvif->ar->wmi.vdev_param->atim_window;
1044 ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001045 ATH10K_DEFAULT_ATIM);
1046 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001047 ath10k_warn(ar, "failed to set IBSS ATIM for vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001048 arvif->vdev_id, ret);
1049}
1050
Michal Kazior9f9b5742014-12-12 12:41:36 +01001051static int ath10k_mac_vif_recalc_ps_wake_threshold(struct ath10k_vif *arvif)
1052{
1053 struct ath10k *ar = arvif->ar;
1054 u32 param;
1055 u32 value;
1056 int ret;
1057
1058 lockdep_assert_held(&arvif->ar->conf_mutex);
1059
1060 if (arvif->u.sta.uapsd)
1061 value = WMI_STA_PS_TX_WAKE_THRESHOLD_NEVER;
1062 else
1063 value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
1064
1065 param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
1066 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param, value);
1067 if (ret) {
1068 ath10k_warn(ar, "failed to submit ps wake threshold %u on vdev %i: %d\n",
1069 value, arvif->vdev_id, ret);
1070 return ret;
1071 }
1072
1073 return 0;
1074}
1075
1076static int ath10k_mac_vif_recalc_ps_poll_count(struct ath10k_vif *arvif)
1077{
1078 struct ath10k *ar = arvif->ar;
1079 u32 param;
1080 u32 value;
1081 int ret;
1082
1083 lockdep_assert_held(&arvif->ar->conf_mutex);
1084
1085 if (arvif->u.sta.uapsd)
1086 value = WMI_STA_PS_PSPOLL_COUNT_UAPSD;
1087 else
1088 value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
1089
1090 param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
1091 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
1092 param, value);
1093 if (ret) {
1094 ath10k_warn(ar, "failed to submit ps poll count %u on vdev %i: %d\n",
1095 value, arvif->vdev_id, ret);
1096 return ret;
1097 }
1098
1099 return 0;
1100}
1101
Michal Kaziorad088bf2013-10-16 15:44:46 +03001102static int ath10k_mac_vif_setup_ps(struct ath10k_vif *arvif)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001103{
Michal Kaziorad088bf2013-10-16 15:44:46 +03001104 struct ath10k *ar = arvif->ar;
Michal Kazior526549a2014-12-12 12:41:37 +01001105 struct ieee80211_vif *vif = arvif->vif;
Michal Kaziorad088bf2013-10-16 15:44:46 +03001106 struct ieee80211_conf *conf = &ar->hw->conf;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001107 enum wmi_sta_powersave_param param;
1108 enum wmi_sta_ps_mode psmode;
1109 int ret;
Michal Kazior526549a2014-12-12 12:41:37 +01001110 int ps_timeout;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001111
Michal Kazior548db542013-07-05 16:15:15 +03001112 lockdep_assert_held(&arvif->ar->conf_mutex);
1113
Michal Kaziorad088bf2013-10-16 15:44:46 +03001114 if (arvif->vif->type != NL80211_IFTYPE_STATION)
1115 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001116
Michal Kaziorbf14e652014-12-12 12:41:38 +01001117 if (vif->bss_conf.ps) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001118 psmode = WMI_STA_PS_MODE_ENABLED;
1119 param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
1120
Michal Kazior526549a2014-12-12 12:41:37 +01001121 ps_timeout = conf->dynamic_ps_timeout;
1122 if (ps_timeout == 0) {
1123 /* Firmware doesn't like 0 */
1124 ps_timeout = ieee80211_tu_to_usec(
1125 vif->bss_conf.beacon_int) / 1000;
1126 }
1127
Michal Kaziorad088bf2013-10-16 15:44:46 +03001128 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
Michal Kazior526549a2014-12-12 12:41:37 +01001129 ps_timeout);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001130 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001131 ath10k_warn(ar, "failed to set inactivity time for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02001132 arvif->vdev_id, ret);
Michal Kaziorad088bf2013-10-16 15:44:46 +03001133 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001134 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001135 } else {
1136 psmode = WMI_STA_PS_MODE_DISABLED;
1137 }
1138
Michal Kazior7aa7a722014-08-25 12:09:38 +02001139 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d psmode %s\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03001140 arvif->vdev_id, psmode ? "enable" : "disable");
1141
Michal Kaziorad088bf2013-10-16 15:44:46 +03001142 ret = ath10k_wmi_set_psmode(ar, arvif->vdev_id, psmode);
1143 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001144 ath10k_warn(ar, "failed to set PS Mode %d for vdev %d: %d\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02001145 psmode, arvif->vdev_id, ret);
Michal Kaziorad088bf2013-10-16 15:44:46 +03001146 return ret;
1147 }
1148
1149 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001150}
1151
1152/**********************/
1153/* Station management */
1154/**********************/
1155
Michal Kazior590922a2014-10-21 10:10:29 +03001156static u32 ath10k_peer_assoc_h_listen_intval(struct ath10k *ar,
1157 struct ieee80211_vif *vif)
1158{
1159 /* Some firmware revisions have unstable STA powersave when listen
1160 * interval is set too high (e.g. 5). The symptoms are firmware doesn't
1161 * generate NullFunc frames properly even if buffered frames have been
1162 * indicated in Beacon TIM. Firmware would seldom wake up to pull
1163 * buffered frames. Often pinging the device from AP would simply fail.
1164 *
1165 * As a workaround set it to 1.
1166 */
1167 if (vif->type == NL80211_IFTYPE_STATION)
1168 return 1;
1169
1170 return ar->hw->conf.listen_interval;
1171}
1172
Kalle Valo5e3dd152013-06-12 20:52:10 +03001173static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001174 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001175 struct ieee80211_sta *sta,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001176 struct wmi_peer_assoc_complete_arg *arg)
1177{
Michal Kazior590922a2014-10-21 10:10:29 +03001178 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1179
Michal Kazior548db542013-07-05 16:15:15 +03001180 lockdep_assert_held(&ar->conf_mutex);
1181
Kalle Valob25f32c2014-09-14 12:50:49 +03001182 ether_addr_copy(arg->addr, sta->addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001183 arg->vdev_id = arvif->vdev_id;
1184 arg->peer_aid = sta->aid;
1185 arg->peer_flags |= WMI_PEER_AUTH;
Michal Kazior590922a2014-10-21 10:10:29 +03001186 arg->peer_listen_intval = ath10k_peer_assoc_h_listen_intval(ar, vif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001187 arg->peer_num_spatial_streams = 1;
Michal Kazior590922a2014-10-21 10:10:29 +03001188 arg->peer_caps = vif->bss_conf.assoc_capability;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001189}
1190
1191static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001192 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001193 struct wmi_peer_assoc_complete_arg *arg)
1194{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001195 struct ieee80211_bss_conf *info = &vif->bss_conf;
1196 struct cfg80211_bss *bss;
1197 const u8 *rsnie = NULL;
1198 const u8 *wpaie = NULL;
1199
Michal Kazior548db542013-07-05 16:15:15 +03001200 lockdep_assert_held(&ar->conf_mutex);
1201
Kalle Valo5e3dd152013-06-12 20:52:10 +03001202 bss = cfg80211_get_bss(ar->hw->wiphy, ar->hw->conf.chandef.chan,
1203 info->bssid, NULL, 0, 0, 0);
1204 if (bss) {
1205 const struct cfg80211_bss_ies *ies;
1206
1207 rcu_read_lock();
1208 rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
1209
1210 ies = rcu_dereference(bss->ies);
1211
1212 wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
Kalle Valo5b07e072014-09-14 12:50:06 +03001213 WLAN_OUI_TYPE_MICROSOFT_WPA,
1214 ies->data,
1215 ies->len);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001216 rcu_read_unlock();
1217 cfg80211_put_bss(ar->hw->wiphy, bss);
1218 }
1219
1220 /* FIXME: base on RSN IE/WPA IE is a correct idea? */
1221 if (rsnie || wpaie) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001222 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001223 arg->peer_flags |= WMI_PEER_NEED_PTK_4_WAY;
1224 }
1225
1226 if (wpaie) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001227 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: wpa ie found\n", __func__);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001228 arg->peer_flags |= WMI_PEER_NEED_GTK_2_WAY;
1229 }
1230}
1231
1232static void ath10k_peer_assoc_h_rates(struct ath10k *ar,
1233 struct ieee80211_sta *sta,
1234 struct wmi_peer_assoc_complete_arg *arg)
1235{
1236 struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
1237 const struct ieee80211_supported_band *sband;
1238 const struct ieee80211_rate *rates;
1239 u32 ratemask;
1240 int i;
1241
Michal Kazior548db542013-07-05 16:15:15 +03001242 lockdep_assert_held(&ar->conf_mutex);
1243
Kalle Valo5e3dd152013-06-12 20:52:10 +03001244 sband = ar->hw->wiphy->bands[ar->hw->conf.chandef.chan->band];
1245 ratemask = sta->supp_rates[ar->hw->conf.chandef.chan->band];
1246 rates = sband->bitrates;
1247
1248 rateset->num_rates = 0;
1249
1250 for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
1251 if (!(ratemask & 1))
1252 continue;
1253
1254 rateset->rates[rateset->num_rates] = rates->hw_value;
1255 rateset->num_rates++;
1256 }
1257}
1258
1259static void ath10k_peer_assoc_h_ht(struct ath10k *ar,
1260 struct ieee80211_sta *sta,
1261 struct wmi_peer_assoc_complete_arg *arg)
1262{
1263 const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001264 int i, n;
Kalle Valoaf762c02014-09-14 12:50:17 +03001265 u32 stbc;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001266
Michal Kazior548db542013-07-05 16:15:15 +03001267 lockdep_assert_held(&ar->conf_mutex);
1268
Kalle Valo5e3dd152013-06-12 20:52:10 +03001269 if (!ht_cap->ht_supported)
1270 return;
1271
1272 arg->peer_flags |= WMI_PEER_HT;
1273 arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1274 ht_cap->ampdu_factor)) - 1;
1275
1276 arg->peer_mpdu_density =
1277 ath10k_parse_mpdudensity(ht_cap->ampdu_density);
1278
1279 arg->peer_ht_caps = ht_cap->cap;
1280 arg->peer_rate_caps |= WMI_RC_HT_FLAG;
1281
1282 if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
1283 arg->peer_flags |= WMI_PEER_LDPC;
1284
1285 if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
1286 arg->peer_flags |= WMI_PEER_40MHZ;
1287 arg->peer_rate_caps |= WMI_RC_CW40_FLAG;
1288 }
1289
1290 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
1291 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
1292
1293 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
1294 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
1295
1296 if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
1297 arg->peer_rate_caps |= WMI_RC_TX_STBC_FLAG;
1298 arg->peer_flags |= WMI_PEER_STBC;
1299 }
1300
1301 if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001302 stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
1303 stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
1304 stbc = stbc << WMI_RC_RX_STBC_FLAG_S;
1305 arg->peer_rate_caps |= stbc;
1306 arg->peer_flags |= WMI_PEER_STBC;
1307 }
1308
Kalle Valo5e3dd152013-06-12 20:52:10 +03001309 if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
1310 arg->peer_rate_caps |= WMI_RC_TS_FLAG;
1311 else if (ht_cap->mcs.rx_mask[1])
1312 arg->peer_rate_caps |= WMI_RC_DS_FLAG;
1313
1314 for (i = 0, n = 0; i < IEEE80211_HT_MCS_MASK_LEN*8; i++)
1315 if (ht_cap->mcs.rx_mask[i/8] & (1 << i%8))
1316 arg->peer_ht_rates.rates[n++] = i;
1317
Bartosz Markowskifd71f802014-02-10 13:12:55 +01001318 /*
1319 * This is a workaround for HT-enabled STAs which break the spec
1320 * and have no HT capabilities RX mask (no HT RX MCS map).
1321 *
1322 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
1323 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
1324 *
1325 * Firmware asserts if such situation occurs.
1326 */
1327 if (n == 0) {
1328 arg->peer_ht_rates.num_rates = 8;
1329 for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
1330 arg->peer_ht_rates.rates[i] = i;
1331 } else {
1332 arg->peer_ht_rates.num_rates = n;
1333 arg->peer_num_spatial_streams = sta->rx_nss;
1334 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001335
Michal Kazior7aa7a722014-08-25 12:09:38 +02001336 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03001337 arg->addr,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001338 arg->peer_ht_rates.num_rates,
1339 arg->peer_num_spatial_streams);
1340}
1341
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001342static int ath10k_peer_assoc_qos_ap(struct ath10k *ar,
1343 struct ath10k_vif *arvif,
1344 struct ieee80211_sta *sta)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001345{
1346 u32 uapsd = 0;
1347 u32 max_sp = 0;
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001348 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001349
Michal Kazior548db542013-07-05 16:15:15 +03001350 lockdep_assert_held(&ar->conf_mutex);
1351
Kalle Valo5e3dd152013-06-12 20:52:10 +03001352 if (sta->wme && sta->uapsd_queues) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001353 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001354 sta->uapsd_queues, sta->max_sp);
1355
Kalle Valo5e3dd152013-06-12 20:52:10 +03001356 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1357 uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
1358 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
1359 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1360 uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
1361 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
1362 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1363 uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
1364 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
1365 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1366 uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
1367 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
1368
Kalle Valo5e3dd152013-06-12 20:52:10 +03001369 if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
1370 max_sp = sta->max_sp;
1371
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001372 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1373 sta->addr,
1374 WMI_AP_PS_PEER_PARAM_UAPSD,
1375 uapsd);
1376 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001377 ath10k_warn(ar, "failed to set ap ps peer param uapsd for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001378 arvif->vdev_id, ret);
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001379 return ret;
1380 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001381
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001382 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1383 sta->addr,
1384 WMI_AP_PS_PEER_PARAM_MAX_SP,
1385 max_sp);
1386 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001387 ath10k_warn(ar, "failed to set ap ps peer param max sp for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001388 arvif->vdev_id, ret);
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001389 return ret;
1390 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001391
1392 /* TODO setup this based on STA listen interval and
1393 beacon interval. Currently we don't know
1394 sta->listen_interval - mac80211 patch required.
1395 Currently use 10 seconds */
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001396 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id, sta->addr,
Kalle Valo5b07e072014-09-14 12:50:06 +03001397 WMI_AP_PS_PEER_PARAM_AGEOUT_TIME,
1398 10);
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001399 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001400 ath10k_warn(ar, "failed to set ap ps peer param ageout time for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001401 arvif->vdev_id, ret);
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001402 return ret;
1403 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001404 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001405
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001406 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001407}
1408
1409static void ath10k_peer_assoc_h_vht(struct ath10k *ar,
1410 struct ieee80211_sta *sta,
1411 struct wmi_peer_assoc_complete_arg *arg)
1412{
1413 const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
Sujith Manoharana24b88b2013-10-07 19:51:57 -07001414 u8 ampdu_factor;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001415
1416 if (!vht_cap->vht_supported)
1417 return;
1418
1419 arg->peer_flags |= WMI_PEER_VHT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001420 arg->peer_vht_caps = vht_cap->cap;
1421
Sujith Manoharana24b88b2013-10-07 19:51:57 -07001422 ampdu_factor = (vht_cap->cap &
1423 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
1424 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
1425
1426 /* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
1427 * zero in VHT IE. Using it would result in degraded throughput.
1428 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
1429 * it if VHT max_mpdu is smaller. */
1430 arg->peer_max_mpdu = max(arg->peer_max_mpdu,
1431 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1432 ampdu_factor)) - 1);
1433
Kalle Valo5e3dd152013-06-12 20:52:10 +03001434 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1435 arg->peer_flags |= WMI_PEER_80MHZ;
1436
1437 arg->peer_vht_rates.rx_max_rate =
1438 __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
1439 arg->peer_vht_rates.rx_mcs_set =
1440 __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
1441 arg->peer_vht_rates.tx_max_rate =
1442 __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
1443 arg->peer_vht_rates.tx_mcs_set =
1444 __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
1445
Michal Kazior7aa7a722014-08-25 12:09:38 +02001446 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03001447 sta->addr, arg->peer_max_mpdu, arg->peer_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001448}
1449
1450static void ath10k_peer_assoc_h_qos(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001451 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001452 struct ieee80211_sta *sta,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001453 struct wmi_peer_assoc_complete_arg *arg)
1454{
Michal Kazior590922a2014-10-21 10:10:29 +03001455 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1456
Kalle Valo5e3dd152013-06-12 20:52:10 +03001457 switch (arvif->vdev_type) {
1458 case WMI_VDEV_TYPE_AP:
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001459 if (sta->wme)
1460 arg->peer_flags |= WMI_PEER_QOS;
1461
1462 if (sta->wme && sta->uapsd_queues) {
1463 arg->peer_flags |= WMI_PEER_APSD;
1464 arg->peer_rate_caps |= WMI_RC_UAPSD_FLAG;
1465 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001466 break;
1467 case WMI_VDEV_TYPE_STA:
Michal Kazior590922a2014-10-21 10:10:29 +03001468 if (vif->bss_conf.qos)
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001469 arg->peer_flags |= WMI_PEER_QOS;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001470 break;
1471 default:
1472 break;
1473 }
1474}
1475
Michal Kazior91b12082014-12-12 12:41:35 +01001476static bool ath10k_mac_sta_has_11g_rates(struct ieee80211_sta *sta)
1477{
1478 /* First 4 rates in ath10k_rates are CCK (11b) rates. */
1479 return sta->supp_rates[IEEE80211_BAND_2GHZ] >> 4;
1480}
1481
Kalle Valo5e3dd152013-06-12 20:52:10 +03001482static void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001483 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001484 struct ieee80211_sta *sta,
1485 struct wmi_peer_assoc_complete_arg *arg)
1486{
1487 enum wmi_phy_mode phymode = MODE_UNKNOWN;
1488
Kalle Valo5e3dd152013-06-12 20:52:10 +03001489 switch (ar->hw->conf.chandef.chan->band) {
1490 case IEEE80211_BAND_2GHZ:
1491 if (sta->ht_cap.ht_supported) {
1492 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1493 phymode = MODE_11NG_HT40;
1494 else
1495 phymode = MODE_11NG_HT20;
Michal Kazior91b12082014-12-12 12:41:35 +01001496 } else if (ath10k_mac_sta_has_11g_rates(sta)) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001497 phymode = MODE_11G;
Michal Kazior91b12082014-12-12 12:41:35 +01001498 } else {
1499 phymode = MODE_11B;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001500 }
1501
1502 break;
1503 case IEEE80211_BAND_5GHZ:
Sujith Manoharan7cc45e92013-09-08 18:19:55 +03001504 /*
1505 * Check VHT first.
1506 */
1507 if (sta->vht_cap.vht_supported) {
1508 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1509 phymode = MODE_11AC_VHT80;
1510 else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1511 phymode = MODE_11AC_VHT40;
1512 else if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
1513 phymode = MODE_11AC_VHT20;
1514 } else if (sta->ht_cap.ht_supported) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001515 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1516 phymode = MODE_11NA_HT40;
1517 else
1518 phymode = MODE_11NA_HT20;
1519 } else {
1520 phymode = MODE_11A;
1521 }
1522
1523 break;
1524 default:
1525 break;
1526 }
1527
Michal Kazior7aa7a722014-08-25 12:09:38 +02001528 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM phymode %s\n",
Kalle Valo38a1d472013-09-08 17:56:14 +03001529 sta->addr, ath10k_wmi_phymode_str(phymode));
Kalle Valo60c3daa2013-09-08 17:56:07 +03001530
Kalle Valo5e3dd152013-06-12 20:52:10 +03001531 arg->peer_phymode = phymode;
1532 WARN_ON(phymode == MODE_UNKNOWN);
1533}
1534
Kalle Valob9ada652013-10-16 15:44:46 +03001535static int ath10k_peer_assoc_prepare(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001536 struct ieee80211_vif *vif,
Kalle Valob9ada652013-10-16 15:44:46 +03001537 struct ieee80211_sta *sta,
Kalle Valob9ada652013-10-16 15:44:46 +03001538 struct wmi_peer_assoc_complete_arg *arg)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001539{
Michal Kazior548db542013-07-05 16:15:15 +03001540 lockdep_assert_held(&ar->conf_mutex);
1541
Kalle Valob9ada652013-10-16 15:44:46 +03001542 memset(arg, 0, sizeof(*arg));
Kalle Valo5e3dd152013-06-12 20:52:10 +03001543
Michal Kazior590922a2014-10-21 10:10:29 +03001544 ath10k_peer_assoc_h_basic(ar, vif, sta, arg);
1545 ath10k_peer_assoc_h_crypto(ar, vif, arg);
Kalle Valob9ada652013-10-16 15:44:46 +03001546 ath10k_peer_assoc_h_rates(ar, sta, arg);
1547 ath10k_peer_assoc_h_ht(ar, sta, arg);
1548 ath10k_peer_assoc_h_vht(ar, sta, arg);
Michal Kazior590922a2014-10-21 10:10:29 +03001549 ath10k_peer_assoc_h_qos(ar, vif, sta, arg);
1550 ath10k_peer_assoc_h_phymode(ar, vif, sta, arg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001551
Kalle Valob9ada652013-10-16 15:44:46 +03001552 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001553}
1554
Michal Kazior90046f52014-02-14 14:45:51 +01001555static const u32 ath10k_smps_map[] = {
1556 [WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
1557 [WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
1558 [WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
1559 [WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
1560};
1561
1562static int ath10k_setup_peer_smps(struct ath10k *ar, struct ath10k_vif *arvif,
1563 const u8 *addr,
1564 const struct ieee80211_sta_ht_cap *ht_cap)
1565{
1566 int smps;
1567
1568 if (!ht_cap->ht_supported)
1569 return 0;
1570
1571 smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
1572 smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
1573
1574 if (smps >= ARRAY_SIZE(ath10k_smps_map))
1575 return -EINVAL;
1576
1577 return ath10k_wmi_peer_set_param(ar, arvif->vdev_id, addr,
1578 WMI_PEER_SMPS_STATE,
1579 ath10k_smps_map[smps]);
1580}
1581
Kalle Valo5e3dd152013-06-12 20:52:10 +03001582/* can be called only in mac80211 callbacks due to `key_count` usage */
1583static void ath10k_bss_assoc(struct ieee80211_hw *hw,
1584 struct ieee80211_vif *vif,
1585 struct ieee80211_bss_conf *bss_conf)
1586{
1587 struct ath10k *ar = hw->priv;
1588 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Michal Kazior90046f52014-02-14 14:45:51 +01001589 struct ieee80211_sta_ht_cap ht_cap;
Kalle Valob9ada652013-10-16 15:44:46 +03001590 struct wmi_peer_assoc_complete_arg peer_arg;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001591 struct ieee80211_sta *ap_sta;
1592 int ret;
1593
Michal Kazior548db542013-07-05 16:15:15 +03001594 lockdep_assert_held(&ar->conf_mutex);
1595
Michal Kazior077efc82014-10-21 10:10:29 +03001596 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
1597 arvif->vdev_id, arvif->bssid, arvif->aid);
1598
Kalle Valo5e3dd152013-06-12 20:52:10 +03001599 rcu_read_lock();
1600
1601 ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
1602 if (!ap_sta) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001603 ath10k_warn(ar, "failed to find station entry for bss %pM vdev %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02001604 bss_conf->bssid, arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001605 rcu_read_unlock();
1606 return;
1607 }
1608
Michal Kazior90046f52014-02-14 14:45:51 +01001609 /* ap_sta must be accessed only within rcu section which must be left
1610 * before calling ath10k_setup_peer_smps() which might sleep. */
1611 ht_cap = ap_sta->ht_cap;
1612
Michal Kazior590922a2014-10-21 10:10:29 +03001613 ret = ath10k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001614 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001615 ath10k_warn(ar, "failed to prepare peer assoc for %pM vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001616 bss_conf->bssid, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001617 rcu_read_unlock();
1618 return;
1619 }
1620
1621 rcu_read_unlock();
1622
Kalle Valob9ada652013-10-16 15:44:46 +03001623 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
1624 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001625 ath10k_warn(ar, "failed to run peer assoc for %pM vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001626 bss_conf->bssid, arvif->vdev_id, ret);
Kalle Valob9ada652013-10-16 15:44:46 +03001627 return;
1628 }
1629
Michal Kazior90046f52014-02-14 14:45:51 +01001630 ret = ath10k_setup_peer_smps(ar, arvif, bss_conf->bssid, &ht_cap);
1631 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001632 ath10k_warn(ar, "failed to setup peer SMPS for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001633 arvif->vdev_id, ret);
Michal Kazior90046f52014-02-14 14:45:51 +01001634 return;
1635 }
1636
Michal Kazior7aa7a722014-08-25 12:09:38 +02001637 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03001638 "mac vdev %d up (associated) bssid %pM aid %d\n",
1639 arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
1640
Michal Kazior077efc82014-10-21 10:10:29 +03001641 WARN_ON(arvif->is_up);
1642
Michal Kaziorc930f742014-01-23 11:38:25 +01001643 arvif->aid = bss_conf->aid;
Kalle Valob25f32c2014-09-14 12:50:49 +03001644 ether_addr_copy(arvif->bssid, bss_conf->bssid);
Michal Kaziorc930f742014-01-23 11:38:25 +01001645
1646 ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
1647 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001648 ath10k_warn(ar, "failed to set vdev %d up: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001649 arvif->vdev_id, ret);
Michal Kaziorc930f742014-01-23 11:38:25 +01001650 return;
1651 }
1652
1653 arvif->is_up = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001654}
1655
Kalle Valo5e3dd152013-06-12 20:52:10 +03001656static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
1657 struct ieee80211_vif *vif)
1658{
1659 struct ath10k *ar = hw->priv;
1660 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1661 int ret;
1662
Michal Kazior548db542013-07-05 16:15:15 +03001663 lockdep_assert_held(&ar->conf_mutex);
1664
Michal Kazior077efc82014-10-21 10:10:29 +03001665 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
1666 arvif->vdev_id, arvif->bssid);
Kalle Valo60c3daa2013-09-08 17:56:07 +03001667
Kalle Valo5e3dd152013-06-12 20:52:10 +03001668 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
Michal Kazior077efc82014-10-21 10:10:29 +03001669 if (ret)
1670 ath10k_warn(ar, "faield to down vdev %i: %d\n",
1671 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001672
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001673 arvif->def_wep_key_idx = 0;
Michal Kaziorc930f742014-01-23 11:38:25 +01001674 arvif->is_up = false;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001675}
1676
Michal Kazior590922a2014-10-21 10:10:29 +03001677static int ath10k_station_assoc(struct ath10k *ar,
1678 struct ieee80211_vif *vif,
1679 struct ieee80211_sta *sta,
1680 bool reassoc)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001681{
Michal Kazior590922a2014-10-21 10:10:29 +03001682 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Kalle Valob9ada652013-10-16 15:44:46 +03001683 struct wmi_peer_assoc_complete_arg peer_arg;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001684 int ret = 0;
1685
Michal Kazior548db542013-07-05 16:15:15 +03001686 lockdep_assert_held(&ar->conf_mutex);
1687
Michal Kazior590922a2014-10-21 10:10:29 +03001688 ret = ath10k_peer_assoc_prepare(ar, vif, sta, &peer_arg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001689 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001690 ath10k_warn(ar, "failed to prepare WMI peer assoc for %pM vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02001691 sta->addr, arvif->vdev_id, ret);
Kalle Valob9ada652013-10-16 15:44:46 +03001692 return ret;
1693 }
1694
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02001695 peer_arg.peer_reassoc = reassoc;
Kalle Valob9ada652013-10-16 15:44:46 +03001696 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
1697 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001698 ath10k_warn(ar, "failed to run peer assoc for STA %pM vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001699 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001700 return ret;
1701 }
1702
Michal Kaziorb1ecde32014-10-21 10:10:29 +03001703 /* Re-assoc is run only to update supported rates for given station. It
1704 * doesn't make much sense to reconfigure the peer completely.
1705 */
1706 if (!reassoc) {
1707 ret = ath10k_setup_peer_smps(ar, arvif, sta->addr,
1708 &sta->ht_cap);
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02001709 if (ret) {
Michal Kaziorb1ecde32014-10-21 10:10:29 +03001710 ath10k_warn(ar, "failed to setup peer SMPS for vdev %d: %d\n",
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02001711 arvif->vdev_id, ret);
1712 return ret;
1713 }
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02001714
Michal Kaziorb1ecde32014-10-21 10:10:29 +03001715 ret = ath10k_peer_assoc_qos_ap(ar, arvif, sta);
1716 if (ret) {
1717 ath10k_warn(ar, "failed to set qos params for STA %pM for vdev %i: %d\n",
1718 sta->addr, arvif->vdev_id, ret);
1719 return ret;
1720 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001721
Michal Kaziorb1ecde32014-10-21 10:10:29 +03001722 if (!sta->wme) {
1723 arvif->num_legacy_stations++;
1724 ret = ath10k_recalc_rtscts_prot(arvif);
1725 if (ret) {
1726 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
1727 arvif->vdev_id, ret);
1728 return ret;
1729 }
1730 }
1731
1732 ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
1733 if (ret) {
1734 ath10k_warn(ar, "failed to install peer wep keys for vdev %i: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001735 arvif->vdev_id, ret);
1736 return ret;
1737 }
1738 }
1739
Kalle Valo5e3dd152013-06-12 20:52:10 +03001740 return ret;
1741}
1742
Michal Kazior590922a2014-10-21 10:10:29 +03001743static int ath10k_station_disassoc(struct ath10k *ar,
1744 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001745 struct ieee80211_sta *sta)
1746{
Michal Kazior590922a2014-10-21 10:10:29 +03001747 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001748 int ret = 0;
1749
1750 lockdep_assert_held(&ar->conf_mutex);
1751
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02001752 if (!sta->wme) {
1753 arvif->num_legacy_stations--;
1754 ret = ath10k_recalc_rtscts_prot(arvif);
1755 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001756 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02001757 arvif->vdev_id, ret);
1758 return ret;
1759 }
1760 }
1761
Kalle Valo5e3dd152013-06-12 20:52:10 +03001762 ret = ath10k_clear_peer_keys(arvif, sta->addr);
1763 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001764 ath10k_warn(ar, "failed to clear all peer wep keys for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001765 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001766 return ret;
1767 }
1768
1769 return ret;
1770}
1771
1772/**************/
1773/* Regulatory */
1774/**************/
1775
1776static int ath10k_update_channel_list(struct ath10k *ar)
1777{
1778 struct ieee80211_hw *hw = ar->hw;
1779 struct ieee80211_supported_band **bands;
1780 enum ieee80211_band band;
1781 struct ieee80211_channel *channel;
1782 struct wmi_scan_chan_list_arg arg = {0};
1783 struct wmi_channel_arg *ch;
1784 bool passive;
1785 int len;
1786 int ret;
1787 int i;
1788
Michal Kazior548db542013-07-05 16:15:15 +03001789 lockdep_assert_held(&ar->conf_mutex);
1790
Kalle Valo5e3dd152013-06-12 20:52:10 +03001791 bands = hw->wiphy->bands;
1792 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1793 if (!bands[band])
1794 continue;
1795
1796 for (i = 0; i < bands[band]->n_channels; i++) {
1797 if (bands[band]->channels[i].flags &
1798 IEEE80211_CHAN_DISABLED)
1799 continue;
1800
1801 arg.n_channels++;
1802 }
1803 }
1804
1805 len = sizeof(struct wmi_channel_arg) * arg.n_channels;
1806 arg.channels = kzalloc(len, GFP_KERNEL);
1807 if (!arg.channels)
1808 return -ENOMEM;
1809
1810 ch = arg.channels;
1811 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1812 if (!bands[band])
1813 continue;
1814
1815 for (i = 0; i < bands[band]->n_channels; i++) {
1816 channel = &bands[band]->channels[i];
1817
1818 if (channel->flags & IEEE80211_CHAN_DISABLED)
1819 continue;
1820
1821 ch->allow_ht = true;
1822
1823 /* FIXME: when should we really allow VHT? */
1824 ch->allow_vht = true;
1825
1826 ch->allow_ibss =
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +02001827 !(channel->flags & IEEE80211_CHAN_NO_IR);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001828
1829 ch->ht40plus =
1830 !(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
1831
Marek Puzyniake8a50f82013-11-20 09:59:47 +02001832 ch->chan_radar =
1833 !!(channel->flags & IEEE80211_CHAN_RADAR);
1834
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +02001835 passive = channel->flags & IEEE80211_CHAN_NO_IR;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001836 ch->passive = passive;
1837
1838 ch->freq = channel->center_freq;
Michal Kazior2d667212014-09-18 15:21:21 +02001839 ch->band_center_freq1 = channel->center_freq;
Michal Kazior89c5c842013-10-23 04:02:13 -07001840 ch->min_power = 0;
Michal Kazior02256932013-10-23 04:02:14 -07001841 ch->max_power = channel->max_power * 2;
1842 ch->max_reg_power = channel->max_reg_power * 2;
1843 ch->max_antenna_gain = channel->max_antenna_gain * 2;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001844 ch->reg_class_id = 0; /* FIXME */
1845
1846 /* FIXME: why use only legacy modes, why not any
1847 * HT/VHT modes? Would that even make any
1848 * difference? */
1849 if (channel->band == IEEE80211_BAND_2GHZ)
1850 ch->mode = MODE_11G;
1851 else
1852 ch->mode = MODE_11A;
1853
1854 if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
1855 continue;
1856
Michal Kazior7aa7a722014-08-25 12:09:38 +02001857 ath10k_dbg(ar, ATH10K_DBG_WMI,
Kalle Valo60c3daa2013-09-08 17:56:07 +03001858 "mac channel [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
1859 ch - arg.channels, arg.n_channels,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001860 ch->freq, ch->max_power, ch->max_reg_power,
1861 ch->max_antenna_gain, ch->mode);
1862
1863 ch++;
1864 }
1865 }
1866
1867 ret = ath10k_wmi_scan_chan_list(ar, &arg);
1868 kfree(arg.channels);
1869
1870 return ret;
1871}
1872
Marek Puzyniak821af6a2014-03-21 17:46:57 +02001873static enum wmi_dfs_region
1874ath10k_mac_get_dfs_region(enum nl80211_dfs_regions dfs_region)
1875{
1876 switch (dfs_region) {
1877 case NL80211_DFS_UNSET:
1878 return WMI_UNINIT_DFS_DOMAIN;
1879 case NL80211_DFS_FCC:
1880 return WMI_FCC_DFS_DOMAIN;
1881 case NL80211_DFS_ETSI:
1882 return WMI_ETSI_DFS_DOMAIN;
1883 case NL80211_DFS_JP:
1884 return WMI_MKK4_DFS_DOMAIN;
1885 }
1886 return WMI_UNINIT_DFS_DOMAIN;
1887}
1888
Michal Kaziorf7843d72013-07-16 09:38:52 +02001889static void ath10k_regd_update(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001890{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001891 struct reg_dmn_pair_mapping *regpair;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001892 int ret;
Marek Puzyniak821af6a2014-03-21 17:46:57 +02001893 enum wmi_dfs_region wmi_dfs_reg;
1894 enum nl80211_dfs_regions nl_dfs_reg;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001895
Michal Kaziorf7843d72013-07-16 09:38:52 +02001896 lockdep_assert_held(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001897
1898 ret = ath10k_update_channel_list(ar);
1899 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001900 ath10k_warn(ar, "failed to update channel list: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001901
1902 regpair = ar->ath_common.regulatory.regpair;
Michal Kaziorf7843d72013-07-16 09:38:52 +02001903
Marek Puzyniak821af6a2014-03-21 17:46:57 +02001904 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
1905 nl_dfs_reg = ar->dfs_detector->region;
1906 wmi_dfs_reg = ath10k_mac_get_dfs_region(nl_dfs_reg);
1907 } else {
1908 wmi_dfs_reg = WMI_UNINIT_DFS_DOMAIN;
1909 }
1910
Kalle Valo5e3dd152013-06-12 20:52:10 +03001911 /* Target allows setting up per-band regdomain but ath_common provides
1912 * a combined one only */
1913 ret = ath10k_wmi_pdev_set_regdomain(ar,
Kalle Valoef8c0012014-02-13 18:13:12 +02001914 regpair->reg_domain,
1915 regpair->reg_domain, /* 2ghz */
1916 regpair->reg_domain, /* 5ghz */
Kalle Valo5e3dd152013-06-12 20:52:10 +03001917 regpair->reg_2ghz_ctl,
Marek Puzyniak821af6a2014-03-21 17:46:57 +02001918 regpair->reg_5ghz_ctl,
1919 wmi_dfs_reg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001920 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001921 ath10k_warn(ar, "failed to set pdev regdomain: %d\n", ret);
Michal Kaziorf7843d72013-07-16 09:38:52 +02001922}
Michal Kazior548db542013-07-05 16:15:15 +03001923
Michal Kaziorf7843d72013-07-16 09:38:52 +02001924static void ath10k_reg_notifier(struct wiphy *wiphy,
1925 struct regulatory_request *request)
1926{
1927 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
1928 struct ath10k *ar = hw->priv;
Janusz Dziedzic9702c682013-11-20 09:59:41 +02001929 bool result;
Michal Kaziorf7843d72013-07-16 09:38:52 +02001930
1931 ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
1932
Janusz Dziedzic9702c682013-11-20 09:59:41 +02001933 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001934 ath10k_dbg(ar, ATH10K_DBG_REGULATORY, "dfs region 0x%x\n",
Janusz Dziedzic9702c682013-11-20 09:59:41 +02001935 request->dfs_region);
1936 result = ar->dfs_detector->set_dfs_domain(ar->dfs_detector,
1937 request->dfs_region);
1938 if (!result)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001939 ath10k_warn(ar, "DFS region 0x%X not supported, will trigger radar for every pulse\n",
Janusz Dziedzic9702c682013-11-20 09:59:41 +02001940 request->dfs_region);
1941 }
1942
Michal Kaziorf7843d72013-07-16 09:38:52 +02001943 mutex_lock(&ar->conf_mutex);
1944 if (ar->state == ATH10K_STATE_ON)
1945 ath10k_regd_update(ar);
Michal Kazior548db542013-07-05 16:15:15 +03001946 mutex_unlock(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001947}
1948
1949/***************/
1950/* TX handlers */
1951/***************/
1952
Michal Kazior42c3aa62013-10-02 11:03:38 +02001953static u8 ath10k_tx_h_get_tid(struct ieee80211_hdr *hdr)
1954{
1955 if (ieee80211_is_mgmt(hdr->frame_control))
1956 return HTT_DATA_TX_EXT_TID_MGMT;
1957
1958 if (!ieee80211_is_data_qos(hdr->frame_control))
1959 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
1960
1961 if (!is_unicast_ether_addr(ieee80211_get_DA(hdr)))
1962 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
1963
1964 return ieee80211_get_qos_ctl(hdr)[0] & IEEE80211_QOS_CTL_TID_MASK;
1965}
1966
Michal Kazior2b37c292014-09-02 11:00:22 +03001967static u8 ath10k_tx_h_get_vdev_id(struct ath10k *ar, struct ieee80211_vif *vif)
Michal Kaziorddb6ad72013-10-02 11:03:39 +02001968{
Michal Kazior2b37c292014-09-02 11:00:22 +03001969 if (vif)
1970 return ath10k_vif_to_arvif(vif)->vdev_id;
Michal Kaziorddb6ad72013-10-02 11:03:39 +02001971
Michal Kazior1bbc0972014-04-08 09:45:47 +03001972 if (ar->monitor_started)
Michal Kaziorddb6ad72013-10-02 11:03:39 +02001973 return ar->monitor_vdev_id;
1974
Michal Kazior7aa7a722014-08-25 12:09:38 +02001975 ath10k_warn(ar, "failed to resolve vdev id\n");
Michal Kaziorddb6ad72013-10-02 11:03:39 +02001976 return 0;
1977}
1978
Michal Kazior4b604552014-07-21 21:03:09 +03001979/* HTT Tx uses Native Wifi tx mode which expects 802.11 frames without QoS
1980 * Control in the header.
Kalle Valo5e3dd152013-06-12 20:52:10 +03001981 */
Michal Kazior4b604552014-07-21 21:03:09 +03001982static void ath10k_tx_h_nwifi(struct ieee80211_hw *hw, struct sk_buff *skb)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001983{
1984 struct ieee80211_hdr *hdr = (void *)skb->data;
Michal Kaziorc21c64d2014-07-21 21:03:10 +03001985 struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001986 u8 *qos_ctl;
1987
1988 if (!ieee80211_is_data_qos(hdr->frame_control))
1989 return;
1990
1991 qos_ctl = ieee80211_get_qos_ctl(hdr);
Michal Kaziorba0ccd72013-07-22 14:25:28 +02001992 memmove(skb->data + IEEE80211_QOS_CTL_LEN,
1993 skb->data, (void *)qos_ctl - (void *)skb->data);
1994 skb_pull(skb, IEEE80211_QOS_CTL_LEN);
Michal Kaziorc21c64d2014-07-21 21:03:10 +03001995
1996 /* Fw/Hw generates a corrupted QoS Control Field for QoS NullFunc
1997 * frames. Powersave is handled by the fw/hw so QoS NyllFunc frames are
1998 * used only for CQM purposes (e.g. hostapd station keepalive ping) so
1999 * it is safe to downgrade to NullFunc.
2000 */
2001 if (ieee80211_is_qos_nullfunc(hdr->frame_control)) {
2002 hdr->frame_control &= ~__cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
2003 cb->htt.tid = HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
2004 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002005}
2006
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002007static void ath10k_tx_wep_key_work(struct work_struct *work)
2008{
2009 struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
2010 wep_key_work);
Michal Kazior7aa7a722014-08-25 12:09:38 +02002011 struct ath10k *ar = arvif->ar;
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002012 int ret, keyidx = arvif->def_wep_key_newidx;
2013
Michal Kazior911e6c02014-05-26 12:46:03 +03002014 mutex_lock(&arvif->ar->conf_mutex);
2015
2016 if (arvif->ar->state != ATH10K_STATE_ON)
2017 goto unlock;
2018
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002019 if (arvif->def_wep_key_idx == keyidx)
Michal Kazior911e6c02014-05-26 12:46:03 +03002020 goto unlock;
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002021
Michal Kazior7aa7a722014-08-25 12:09:38 +02002022 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002023 arvif->vdev_id, keyidx);
2024
2025 ret = ath10k_wmi_vdev_set_param(arvif->ar,
2026 arvif->vdev_id,
2027 arvif->ar->wmi.vdev_param->def_keyid,
2028 keyidx);
2029 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002030 ath10k_warn(ar, "failed to update wep key index for vdev %d: %d\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02002031 arvif->vdev_id,
2032 ret);
Michal Kazior911e6c02014-05-26 12:46:03 +03002033 goto unlock;
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002034 }
2035
2036 arvif->def_wep_key_idx = keyidx;
Michal Kazior911e6c02014-05-26 12:46:03 +03002037
2038unlock:
2039 mutex_unlock(&arvif->ar->conf_mutex);
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002040}
2041
Michal Kazior4b604552014-07-21 21:03:09 +03002042static void ath10k_tx_h_update_wep_key(struct ieee80211_vif *vif,
2043 struct ieee80211_key_conf *key,
2044 struct sk_buff *skb)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002045{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002046 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2047 struct ath10k *ar = arvif->ar;
2048 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002049
Kalle Valo5e3dd152013-06-12 20:52:10 +03002050 if (!ieee80211_has_protected(hdr->frame_control))
2051 return;
2052
2053 if (!key)
2054 return;
2055
2056 if (key->cipher != WLAN_CIPHER_SUITE_WEP40 &&
2057 key->cipher != WLAN_CIPHER_SUITE_WEP104)
2058 return;
2059
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002060 if (key->keyidx == arvif->def_wep_key_idx)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002061 return;
2062
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002063 /* FIXME: Most likely a few frames will be TXed with an old key. Simply
2064 * queueing frames until key index is updated is not an option because
2065 * sk_buff may need more processing to be done, e.g. offchannel */
2066 arvif->def_wep_key_newidx = key->keyidx;
2067 ieee80211_queue_work(ar->hw, &arvif->wep_key_work);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002068}
2069
Michal Kazior4b604552014-07-21 21:03:09 +03002070static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar,
2071 struct ieee80211_vif *vif,
2072 struct sk_buff *skb)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002073{
2074 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002075 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2076
2077 /* This is case only for P2P_GO */
2078 if (arvif->vdev_type != WMI_VDEV_TYPE_AP ||
2079 arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
2080 return;
2081
2082 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
2083 spin_lock_bh(&ar->data_lock);
2084 if (arvif->u.ap.noa_data)
2085 if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
2086 GFP_ATOMIC))
2087 memcpy(skb_put(skb, arvif->u.ap.noa_len),
2088 arvif->u.ap.noa_data,
2089 arvif->u.ap.noa_len);
2090 spin_unlock_bh(&ar->data_lock);
2091 }
2092}
2093
Michal Kazior8d6d3622014-11-24 14:58:31 +01002094static bool ath10k_mac_need_offchan_tx_work(struct ath10k *ar)
2095{
2096 /* FIXME: Not really sure since when the behaviour changed. At some
2097 * point new firmware stopped requiring creation of peer entries for
2098 * offchannel tx (and actually creating them causes issues with wmi-htc
2099 * tx credit replenishment and reliability). Assuming it's at least 3.4
2100 * because that's when the `freq` was introduced to TX_FRM HTT command.
2101 */
2102 return !(ar->htt.target_version_major >= 3 &&
2103 ar->htt.target_version_minor >= 4);
2104}
2105
Kalle Valo5e3dd152013-06-12 20:52:10 +03002106static void ath10k_tx_htt(struct ath10k *ar, struct sk_buff *skb)
2107{
2108 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002109 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002110
Michal Kazior961d4c32013-08-09 10:13:34 +02002111 if (ar->htt.target_version_major >= 3) {
2112 /* Since HTT 3.0 there is no separate mgmt tx command */
2113 ret = ath10k_htt_tx(&ar->htt, skb);
2114 goto exit;
2115 }
2116
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002117 if (ieee80211_is_mgmt(hdr->frame_control)) {
2118 if (test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
2119 ar->fw_features)) {
2120 if (skb_queue_len(&ar->wmi_mgmt_tx_queue) >=
2121 ATH10K_MAX_NUM_MGMT_PENDING) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002122 ath10k_warn(ar, "reached WMI management transmit queue limit\n");
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002123 ret = -EBUSY;
2124 goto exit;
2125 }
2126
2127 skb_queue_tail(&ar->wmi_mgmt_tx_queue, skb);
2128 ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
2129 } else {
2130 ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
2131 }
2132 } else if (!test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
2133 ar->fw_features) &&
2134 ieee80211_is_nullfunc(hdr->frame_control)) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002135 /* FW does not report tx status properly for NullFunc frames
2136 * unless they are sent through mgmt tx path. mac80211 sends
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002137 * those frames when it detects link/beacon loss and depends
2138 * on the tx status to be correct. */
Michal Kazioredb82362013-07-05 16:15:14 +03002139 ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002140 } else {
Michal Kazioredb82362013-07-05 16:15:14 +03002141 ret = ath10k_htt_tx(&ar->htt, skb);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002142 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002143
Michal Kazior961d4c32013-08-09 10:13:34 +02002144exit:
Kalle Valo5e3dd152013-06-12 20:52:10 +03002145 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002146 ath10k_warn(ar, "failed to transmit packet, dropping: %d\n",
2147 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002148 ieee80211_free_txskb(ar->hw, skb);
2149 }
2150}
2151
2152void ath10k_offchan_tx_purge(struct ath10k *ar)
2153{
2154 struct sk_buff *skb;
2155
2156 for (;;) {
2157 skb = skb_dequeue(&ar->offchan_tx_queue);
2158 if (!skb)
2159 break;
2160
2161 ieee80211_free_txskb(ar->hw, skb);
2162 }
2163}
2164
2165void ath10k_offchan_tx_work(struct work_struct *work)
2166{
2167 struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
2168 struct ath10k_peer *peer;
2169 struct ieee80211_hdr *hdr;
2170 struct sk_buff *skb;
2171 const u8 *peer_addr;
2172 int vdev_id;
2173 int ret;
2174
2175 /* FW requirement: We must create a peer before FW will send out
2176 * an offchannel frame. Otherwise the frame will be stuck and
2177 * never transmitted. We delete the peer upon tx completion.
2178 * It is unlikely that a peer for offchannel tx will already be
2179 * present. However it may be in some rare cases so account for that.
2180 * Otherwise we might remove a legitimate peer and break stuff. */
2181
2182 for (;;) {
2183 skb = skb_dequeue(&ar->offchan_tx_queue);
2184 if (!skb)
2185 break;
2186
2187 mutex_lock(&ar->conf_mutex);
2188
Michal Kazior7aa7a722014-08-25 12:09:38 +02002189 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac offchannel skb %p\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002190 skb);
2191
2192 hdr = (struct ieee80211_hdr *)skb->data;
2193 peer_addr = ieee80211_get_DA(hdr);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002194 vdev_id = ATH10K_SKB_CB(skb)->vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002195
2196 spin_lock_bh(&ar->data_lock);
2197 peer = ath10k_peer_find(ar, vdev_id, peer_addr);
2198 spin_unlock_bh(&ar->data_lock);
2199
2200 if (peer)
Kalle Valo60c3daa2013-09-08 17:56:07 +03002201 /* FIXME: should this use ath10k_warn()? */
Michal Kazior7aa7a722014-08-25 12:09:38 +02002202 ath10k_dbg(ar, ATH10K_DBG_MAC, "peer %pM on vdev %d already present\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002203 peer_addr, vdev_id);
2204
2205 if (!peer) {
2206 ret = ath10k_peer_create(ar, vdev_id, peer_addr);
2207 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002208 ath10k_warn(ar, "failed to create peer %pM on vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002209 peer_addr, vdev_id, ret);
2210 }
2211
2212 spin_lock_bh(&ar->data_lock);
Wolfram Sang16735d02013-11-14 14:32:02 -08002213 reinit_completion(&ar->offchan_tx_completed);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002214 ar->offchan_tx_skb = skb;
2215 spin_unlock_bh(&ar->data_lock);
2216
2217 ath10k_tx_htt(ar, skb);
2218
2219 ret = wait_for_completion_timeout(&ar->offchan_tx_completed,
2220 3 * HZ);
2221 if (ret <= 0)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002222 ath10k_warn(ar, "timed out waiting for offchannel skb %p\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002223 skb);
2224
2225 if (!peer) {
2226 ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
2227 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002228 ath10k_warn(ar, "failed to delete peer %pM on vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002229 peer_addr, vdev_id, ret);
2230 }
2231
2232 mutex_unlock(&ar->conf_mutex);
2233 }
2234}
2235
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002236void ath10k_mgmt_over_wmi_tx_purge(struct ath10k *ar)
2237{
2238 struct sk_buff *skb;
2239
2240 for (;;) {
2241 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
2242 if (!skb)
2243 break;
2244
2245 ieee80211_free_txskb(ar->hw, skb);
2246 }
2247}
2248
2249void ath10k_mgmt_over_wmi_tx_work(struct work_struct *work)
2250{
2251 struct ath10k *ar = container_of(work, struct ath10k, wmi_mgmt_tx_work);
2252 struct sk_buff *skb;
2253 int ret;
2254
2255 for (;;) {
2256 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
2257 if (!skb)
2258 break;
2259
2260 ret = ath10k_wmi_mgmt_tx(ar, skb);
Michal Kazior5fb5e412013-10-28 07:18:13 +01002261 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002262 ath10k_warn(ar, "failed to transmit management frame via WMI: %d\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02002263 ret);
Michal Kazior5fb5e412013-10-28 07:18:13 +01002264 ieee80211_free_txskb(ar->hw, skb);
2265 }
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002266 }
2267}
2268
Kalle Valo5e3dd152013-06-12 20:52:10 +03002269/************/
2270/* Scanning */
2271/************/
2272
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002273void __ath10k_scan_finish(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002274{
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002275 lockdep_assert_held(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002276
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002277 switch (ar->scan.state) {
2278 case ATH10K_SCAN_IDLE:
2279 break;
2280 case ATH10K_SCAN_RUNNING:
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002281 if (ar->scan.is_roc)
2282 ieee80211_remain_on_channel_expired(ar->hw);
Michal Kazior7305d3e2014-11-24 14:58:33 +01002283 case ATH10K_SCAN_ABORTING:
2284 if (!ar->scan.is_roc)
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002285 ieee80211_scan_completed(ar->hw,
2286 (ar->scan.state ==
2287 ATH10K_SCAN_ABORTING));
2288 /* fall through */
2289 case ATH10K_SCAN_STARTING:
2290 ar->scan.state = ATH10K_SCAN_IDLE;
2291 ar->scan_channel = NULL;
2292 ath10k_offchan_tx_purge(ar);
2293 cancel_delayed_work(&ar->scan.timeout);
2294 complete_all(&ar->scan.completed);
2295 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002296 }
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002297}
Kalle Valo5e3dd152013-06-12 20:52:10 +03002298
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002299void ath10k_scan_finish(struct ath10k *ar)
2300{
2301 spin_lock_bh(&ar->data_lock);
2302 __ath10k_scan_finish(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002303 spin_unlock_bh(&ar->data_lock);
2304}
2305
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002306static int ath10k_scan_stop(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002307{
2308 struct wmi_stop_scan_arg arg = {
2309 .req_id = 1, /* FIXME */
2310 .req_type = WMI_SCAN_STOP_ONE,
2311 .u.scan_id = ATH10K_SCAN_ID,
2312 };
2313 int ret;
2314
2315 lockdep_assert_held(&ar->conf_mutex);
2316
Kalle Valo5e3dd152013-06-12 20:52:10 +03002317 ret = ath10k_wmi_stop_scan(ar, &arg);
2318 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002319 ath10k_warn(ar, "failed to stop wmi scan: %d\n", ret);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002320 goto out;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002321 }
2322
Kalle Valo5e3dd152013-06-12 20:52:10 +03002323 ret = wait_for_completion_timeout(&ar->scan.completed, 3*HZ);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002324 if (ret == 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002325 ath10k_warn(ar, "failed to receive scan abortion completion: timed out\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002326 ret = -ETIMEDOUT;
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002327 } else if (ret > 0) {
2328 ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002329 }
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002330
2331out:
2332 /* Scan state should be updated upon scan completion but in case
2333 * firmware fails to deliver the event (for whatever reason) it is
2334 * desired to clean up scan state anyway. Firmware may have just
2335 * dropped the scan completion event delivery due to transport pipe
2336 * being overflown with data and/or it can recover on its own before
2337 * next scan request is submitted.
2338 */
2339 spin_lock_bh(&ar->data_lock);
2340 if (ar->scan.state != ATH10K_SCAN_IDLE)
2341 __ath10k_scan_finish(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002342 spin_unlock_bh(&ar->data_lock);
2343
2344 return ret;
2345}
2346
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002347static void ath10k_scan_abort(struct ath10k *ar)
2348{
2349 int ret;
2350
2351 lockdep_assert_held(&ar->conf_mutex);
2352
2353 spin_lock_bh(&ar->data_lock);
2354
2355 switch (ar->scan.state) {
2356 case ATH10K_SCAN_IDLE:
2357 /* This can happen if timeout worker kicked in and called
2358 * abortion while scan completion was being processed.
2359 */
2360 break;
2361 case ATH10K_SCAN_STARTING:
2362 case ATH10K_SCAN_ABORTING:
Michal Kazior7aa7a722014-08-25 12:09:38 +02002363 ath10k_warn(ar, "refusing scan abortion due to invalid scan state: %s (%d)\n",
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002364 ath10k_scan_state_str(ar->scan.state),
2365 ar->scan.state);
2366 break;
2367 case ATH10K_SCAN_RUNNING:
2368 ar->scan.state = ATH10K_SCAN_ABORTING;
2369 spin_unlock_bh(&ar->data_lock);
2370
2371 ret = ath10k_scan_stop(ar);
2372 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002373 ath10k_warn(ar, "failed to abort scan: %d\n", ret);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002374
2375 spin_lock_bh(&ar->data_lock);
2376 break;
2377 }
2378
2379 spin_unlock_bh(&ar->data_lock);
2380}
2381
2382void ath10k_scan_timeout_work(struct work_struct *work)
2383{
2384 struct ath10k *ar = container_of(work, struct ath10k,
2385 scan.timeout.work);
2386
2387 mutex_lock(&ar->conf_mutex);
2388 ath10k_scan_abort(ar);
2389 mutex_unlock(&ar->conf_mutex);
2390}
2391
Kalle Valo5e3dd152013-06-12 20:52:10 +03002392static int ath10k_start_scan(struct ath10k *ar,
2393 const struct wmi_start_scan_arg *arg)
2394{
2395 int ret;
2396
2397 lockdep_assert_held(&ar->conf_mutex);
2398
2399 ret = ath10k_wmi_start_scan(ar, arg);
2400 if (ret)
2401 return ret;
2402
Kalle Valo5e3dd152013-06-12 20:52:10 +03002403 ret = wait_for_completion_timeout(&ar->scan.started, 1*HZ);
2404 if (ret == 0) {
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002405 ret = ath10k_scan_stop(ar);
2406 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002407 ath10k_warn(ar, "failed to stop scan: %d\n", ret);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002408
2409 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002410 }
2411
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002412 /* Add a 200ms margin to account for event/command processing */
2413 ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
2414 msecs_to_jiffies(arg->max_scan_time+200));
Kalle Valo5e3dd152013-06-12 20:52:10 +03002415 return 0;
2416}
2417
2418/**********************/
2419/* mac80211 callbacks */
2420/**********************/
2421
2422static void ath10k_tx(struct ieee80211_hw *hw,
2423 struct ieee80211_tx_control *control,
2424 struct sk_buff *skb)
2425{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002426 struct ath10k *ar = hw->priv;
Michal Kazior4b604552014-07-21 21:03:09 +03002427 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2428 struct ieee80211_vif *vif = info->control.vif;
2429 struct ieee80211_key_conf *key = info->control.hw_key;
2430 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002431
2432 /* We should disable CCK RATE due to P2P */
2433 if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002434 ath10k_dbg(ar, ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002435
Michal Kazior4b604552014-07-21 21:03:09 +03002436 ATH10K_SKB_CB(skb)->htt.is_offchan = false;
2437 ATH10K_SKB_CB(skb)->htt.tid = ath10k_tx_h_get_tid(hdr);
Michal Kazior2b37c292014-09-02 11:00:22 +03002438 ATH10K_SKB_CB(skb)->vdev_id = ath10k_tx_h_get_vdev_id(ar, vif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002439
Michal Kaziorcf84bd42013-07-16 11:04:54 +02002440 /* it makes no sense to process injected frames like that */
Michal Kazior4b604552014-07-21 21:03:09 +03002441 if (vif && vif->type != NL80211_IFTYPE_MONITOR) {
2442 ath10k_tx_h_nwifi(hw, skb);
2443 ath10k_tx_h_update_wep_key(vif, key, skb);
2444 ath10k_tx_h_add_p2p_noa_ie(ar, vif, skb);
2445 ath10k_tx_h_seq_no(vif, skb);
Michal Kaziorcf84bd42013-07-16 11:04:54 +02002446 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002447
Kalle Valo5e3dd152013-06-12 20:52:10 +03002448 if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
2449 spin_lock_bh(&ar->data_lock);
Michal Kazior8d6d3622014-11-24 14:58:31 +01002450 ATH10K_SKB_CB(skb)->htt.freq = ar->scan.roc_freq;
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002451 ATH10K_SKB_CB(skb)->vdev_id = ar->scan.vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002452 spin_unlock_bh(&ar->data_lock);
2453
Michal Kazior8d6d3622014-11-24 14:58:31 +01002454 if (ath10k_mac_need_offchan_tx_work(ar)) {
2455 ATH10K_SKB_CB(skb)->htt.freq = 0;
2456 ATH10K_SKB_CB(skb)->htt.is_offchan = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002457
Michal Kazior8d6d3622014-11-24 14:58:31 +01002458 ath10k_dbg(ar, ATH10K_DBG_MAC, "queued offchannel skb %p\n",
2459 skb);
2460
2461 skb_queue_tail(&ar->offchan_tx_queue, skb);
2462 ieee80211_queue_work(hw, &ar->offchan_tx_work);
2463 return;
2464 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002465 }
2466
2467 ath10k_tx_htt(ar, skb);
2468}
2469
Michal Kaziorbca7baf2014-05-26 12:46:03 +03002470/* Must not be called with conf_mutex held as workers can use that also. */
Michal Kazior7962b0d2014-10-28 10:34:38 +01002471void ath10k_drain_tx(struct ath10k *ar)
Michal Kaziorbca7baf2014-05-26 12:46:03 +03002472{
2473 /* make sure rcu-protected mac80211 tx path itself is drained */
2474 synchronize_net();
2475
2476 ath10k_offchan_tx_purge(ar);
2477 ath10k_mgmt_over_wmi_tx_purge(ar);
2478
2479 cancel_work_sync(&ar->offchan_tx_work);
2480 cancel_work_sync(&ar->wmi_mgmt_tx_work);
2481}
2482
Michal Kazioraffd3212013-07-16 09:54:35 +02002483void ath10k_halt(struct ath10k *ar)
Michal Kazior818bdd12013-07-16 09:38:57 +02002484{
Michal Kaziord9bc4b92014-04-23 19:30:06 +03002485 struct ath10k_vif *arvif;
2486
Michal Kazior818bdd12013-07-16 09:38:57 +02002487 lockdep_assert_held(&ar->conf_mutex);
2488
Michal Kazior19337472014-08-28 12:58:16 +02002489 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
2490 ar->filter_flags = 0;
2491 ar->monitor = false;
2492
2493 if (ar->monitor_started)
Michal Kazior1bbc0972014-04-08 09:45:47 +03002494 ath10k_monitor_stop(ar);
Michal Kazior19337472014-08-28 12:58:16 +02002495
2496 ar->monitor_started = false;
Michal Kazior1bbc0972014-04-08 09:45:47 +03002497
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002498 ath10k_scan_finish(ar);
Michal Kazior818bdd12013-07-16 09:38:57 +02002499 ath10k_peer_cleanup_all(ar);
2500 ath10k_core_stop(ar);
2501 ath10k_hif_power_down(ar);
2502
2503 spin_lock_bh(&ar->data_lock);
Michal Kazior64badcb2014-09-18 11:18:02 +03002504 list_for_each_entry(arvif, &ar->arvifs, list)
2505 ath10k_mac_vif_beacon_cleanup(arvif);
Michal Kazior818bdd12013-07-16 09:38:57 +02002506 spin_unlock_bh(&ar->data_lock);
2507}
2508
Ben Greear46acf7b2014-05-16 17:15:38 +03002509static int ath10k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
2510{
2511 struct ath10k *ar = hw->priv;
2512
2513 mutex_lock(&ar->conf_mutex);
2514
2515 if (ar->cfg_tx_chainmask) {
2516 *tx_ant = ar->cfg_tx_chainmask;
2517 *rx_ant = ar->cfg_rx_chainmask;
2518 } else {
2519 *tx_ant = ar->supp_tx_chainmask;
2520 *rx_ant = ar->supp_rx_chainmask;
2521 }
2522
2523 mutex_unlock(&ar->conf_mutex);
2524
2525 return 0;
2526}
2527
Ben Greear5572a952014-11-24 16:22:10 +02002528static void ath10k_check_chain_mask(struct ath10k *ar, u32 cm, const char *dbg)
2529{
2530 /* It is not clear that allowing gaps in chainmask
2531 * is helpful. Probably it will not do what user
2532 * is hoping for, so warn in that case.
2533 */
2534 if (cm == 15 || cm == 7 || cm == 3 || cm == 1 || cm == 0)
2535 return;
2536
2537 ath10k_warn(ar, "mac %s antenna chainmask may be invalid: 0x%x. Suggested values: 15, 7, 3, 1 or 0.\n",
2538 dbg, cm);
2539}
2540
Ben Greear46acf7b2014-05-16 17:15:38 +03002541static int __ath10k_set_antenna(struct ath10k *ar, u32 tx_ant, u32 rx_ant)
2542{
2543 int ret;
2544
2545 lockdep_assert_held(&ar->conf_mutex);
2546
Ben Greear5572a952014-11-24 16:22:10 +02002547 ath10k_check_chain_mask(ar, tx_ant, "tx");
2548 ath10k_check_chain_mask(ar, rx_ant, "rx");
2549
Ben Greear46acf7b2014-05-16 17:15:38 +03002550 ar->cfg_tx_chainmask = tx_ant;
2551 ar->cfg_rx_chainmask = rx_ant;
2552
2553 if ((ar->state != ATH10K_STATE_ON) &&
2554 (ar->state != ATH10K_STATE_RESTARTED))
2555 return 0;
2556
2557 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->tx_chain_mask,
2558 tx_ant);
2559 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002560 ath10k_warn(ar, "failed to set tx-chainmask: %d, req 0x%x\n",
Ben Greear46acf7b2014-05-16 17:15:38 +03002561 ret, tx_ant);
2562 return ret;
2563 }
2564
2565 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->rx_chain_mask,
2566 rx_ant);
2567 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002568 ath10k_warn(ar, "failed to set rx-chainmask: %d, req 0x%x\n",
Ben Greear46acf7b2014-05-16 17:15:38 +03002569 ret, rx_ant);
2570 return ret;
2571 }
2572
2573 return 0;
2574}
2575
2576static int ath10k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
2577{
2578 struct ath10k *ar = hw->priv;
2579 int ret;
2580
2581 mutex_lock(&ar->conf_mutex);
2582 ret = __ath10k_set_antenna(ar, tx_ant, rx_ant);
2583 mutex_unlock(&ar->conf_mutex);
2584 return ret;
2585}
2586
Kalle Valo5e3dd152013-06-12 20:52:10 +03002587static int ath10k_start(struct ieee80211_hw *hw)
2588{
2589 struct ath10k *ar = hw->priv;
Michal Kazior818bdd12013-07-16 09:38:57 +02002590 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002591
Michal Kaziorbca7baf2014-05-26 12:46:03 +03002592 /*
2593 * This makes sense only when restarting hw. It is harmless to call
2594 * uncoditionally. This is necessary to make sure no HTT/WMI tx
2595 * commands will be submitted while restarting.
2596 */
2597 ath10k_drain_tx(ar);
2598
Michal Kazior548db542013-07-05 16:15:15 +03002599 mutex_lock(&ar->conf_mutex);
2600
Michal Kaziorc5058f52014-05-26 12:46:03 +03002601 switch (ar->state) {
2602 case ATH10K_STATE_OFF:
2603 ar->state = ATH10K_STATE_ON;
2604 break;
2605 case ATH10K_STATE_RESTARTING:
2606 ath10k_halt(ar);
2607 ar->state = ATH10K_STATE_RESTARTED;
2608 break;
2609 case ATH10K_STATE_ON:
2610 case ATH10K_STATE_RESTARTED:
2611 case ATH10K_STATE_WEDGED:
2612 WARN_ON(1);
Michal Kazior818bdd12013-07-16 09:38:57 +02002613 ret = -EINVAL;
Michal Kaziorae254432014-05-26 12:46:02 +03002614 goto err;
Kalle Valo43d2a302014-09-10 18:23:30 +03002615 case ATH10K_STATE_UTF:
2616 ret = -EBUSY;
2617 goto err;
Michal Kazior818bdd12013-07-16 09:38:57 +02002618 }
2619
2620 ret = ath10k_hif_power_up(ar);
2621 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002622 ath10k_err(ar, "Could not init hif: %d\n", ret);
Michal Kaziorae254432014-05-26 12:46:02 +03002623 goto err_off;
Michal Kazior818bdd12013-07-16 09:38:57 +02002624 }
2625
Kalle Valo43d2a302014-09-10 18:23:30 +03002626 ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_NORMAL);
Michal Kazior818bdd12013-07-16 09:38:57 +02002627 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002628 ath10k_err(ar, "Could not init core: %d\n", ret);
Michal Kaziorae254432014-05-26 12:46:02 +03002629 goto err_power_down;
Michal Kazior818bdd12013-07-16 09:38:57 +02002630 }
2631
Bartosz Markowski226a3392013-09-26 17:47:16 +02002632 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->pmf_qos, 1);
Michal Kaziorae254432014-05-26 12:46:02 +03002633 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002634 ath10k_warn(ar, "failed to enable PMF QOS: %d\n", ret);
Michal Kaziorae254432014-05-26 12:46:02 +03002635 goto err_core_stop;
2636 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002637
Michal Kaziorc4dd0d02013-11-13 11:05:10 +01002638 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->dynamic_bw, 1);
Michal Kaziorae254432014-05-26 12:46:02 +03002639 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002640 ath10k_warn(ar, "failed to enable dynamic BW: %d\n", ret);
Michal Kaziorae254432014-05-26 12:46:02 +03002641 goto err_core_stop;
2642 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002643
Ben Greear46acf7b2014-05-16 17:15:38 +03002644 if (ar->cfg_tx_chainmask)
2645 __ath10k_set_antenna(ar, ar->cfg_tx_chainmask,
2646 ar->cfg_rx_chainmask);
2647
Marek Puzyniakab6258e2014-01-29 15:03:31 +02002648 /*
2649 * By default FW set ARP frames ac to voice (6). In that case ARP
2650 * exchange is not working properly for UAPSD enabled AP. ARP requests
2651 * which arrives with access category 0 are processed by network stack
2652 * and send back with access category 0, but FW changes access category
2653 * to 6. Set ARP frames access category to best effort (0) solves
2654 * this problem.
2655 */
2656
2657 ret = ath10k_wmi_pdev_set_param(ar,
2658 ar->wmi.pdev_param->arp_ac_override, 0);
2659 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002660 ath10k_warn(ar, "failed to set arp ac override parameter: %d\n",
Marek Puzyniakab6258e2014-01-29 15:03:31 +02002661 ret);
Michal Kaziorae254432014-05-26 12:46:02 +03002662 goto err_core_stop;
Marek Puzyniakab6258e2014-01-29 15:03:31 +02002663 }
2664
Michal Kaziord6500972014-04-08 09:56:09 +03002665 ar->num_started_vdevs = 0;
Michal Kaziorf7843d72013-07-16 09:38:52 +02002666 ath10k_regd_update(ar);
2667
Simon Wunderlich855aed12014-08-02 09:12:54 +03002668 ath10k_spectral_start(ar);
2669
Michal Kaziorae254432014-05-26 12:46:02 +03002670 mutex_unlock(&ar->conf_mutex);
2671 return 0;
2672
2673err_core_stop:
2674 ath10k_core_stop(ar);
2675
2676err_power_down:
2677 ath10k_hif_power_down(ar);
2678
2679err_off:
2680 ar->state = ATH10K_STATE_OFF;
2681
2682err:
Michal Kazior548db542013-07-05 16:15:15 +03002683 mutex_unlock(&ar->conf_mutex);
Michal Kaziorc60bdd82014-01-29 07:26:31 +01002684 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002685}
2686
2687static void ath10k_stop(struct ieee80211_hw *hw)
2688{
2689 struct ath10k *ar = hw->priv;
2690
Michal Kaziorbca7baf2014-05-26 12:46:03 +03002691 ath10k_drain_tx(ar);
2692
Michal Kazior548db542013-07-05 16:15:15 +03002693 mutex_lock(&ar->conf_mutex);
Michal Kaziorc5058f52014-05-26 12:46:03 +03002694 if (ar->state != ATH10K_STATE_OFF) {
Michal Kazior818bdd12013-07-16 09:38:57 +02002695 ath10k_halt(ar);
Michal Kaziorc5058f52014-05-26 12:46:03 +03002696 ar->state = ATH10K_STATE_OFF;
2697 }
Michal Kazior548db542013-07-05 16:15:15 +03002698 mutex_unlock(&ar->conf_mutex);
2699
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002700 cancel_delayed_work_sync(&ar->scan.timeout);
Michal Kazioraffd3212013-07-16 09:54:35 +02002701 cancel_work_sync(&ar->restart_work);
2702}
2703
Michal Kaziorad088bf2013-10-16 15:44:46 +03002704static int ath10k_config_ps(struct ath10k *ar)
Michal Kazioraffd3212013-07-16 09:54:35 +02002705{
Michal Kaziorad088bf2013-10-16 15:44:46 +03002706 struct ath10k_vif *arvif;
2707 int ret = 0;
Michal Kazioraffd3212013-07-16 09:54:35 +02002708
2709 lockdep_assert_held(&ar->conf_mutex);
2710
Michal Kaziorad088bf2013-10-16 15:44:46 +03002711 list_for_each_entry(arvif, &ar->arvifs, list) {
2712 ret = ath10k_mac_vif_setup_ps(arvif);
2713 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002714 ath10k_warn(ar, "failed to setup powersave: %d\n", ret);
Michal Kaziorad088bf2013-10-16 15:44:46 +03002715 break;
2716 }
2717 }
Michal Kazioraffd3212013-07-16 09:54:35 +02002718
Michal Kaziorad088bf2013-10-16 15:44:46 +03002719 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002720}
2721
Michal Kaziorc930f742014-01-23 11:38:25 +01002722static const char *chandef_get_width(enum nl80211_chan_width width)
2723{
2724 switch (width) {
2725 case NL80211_CHAN_WIDTH_20_NOHT:
2726 return "20 (noht)";
2727 case NL80211_CHAN_WIDTH_20:
2728 return "20";
2729 case NL80211_CHAN_WIDTH_40:
2730 return "40";
2731 case NL80211_CHAN_WIDTH_80:
2732 return "80";
2733 case NL80211_CHAN_WIDTH_80P80:
2734 return "80+80";
2735 case NL80211_CHAN_WIDTH_160:
2736 return "160";
2737 case NL80211_CHAN_WIDTH_5:
2738 return "5";
2739 case NL80211_CHAN_WIDTH_10:
2740 return "10";
2741 }
2742 return "?";
2743}
2744
2745static void ath10k_config_chan(struct ath10k *ar)
2746{
2747 struct ath10k_vif *arvif;
Michal Kaziorc930f742014-01-23 11:38:25 +01002748 int ret;
2749
2750 lockdep_assert_held(&ar->conf_mutex);
2751
Michal Kazior7aa7a722014-08-25 12:09:38 +02002752 ath10k_dbg(ar, ATH10K_DBG_MAC,
Michal Kaziorc930f742014-01-23 11:38:25 +01002753 "mac config channel to %dMHz (cf1 %dMHz cf2 %dMHz width %s)\n",
2754 ar->chandef.chan->center_freq,
2755 ar->chandef.center_freq1,
2756 ar->chandef.center_freq2,
2757 chandef_get_width(ar->chandef.width));
2758
2759 /* First stop monitor interface. Some FW versions crash if there's a
2760 * lone monitor interface. */
Michal Kazior1bbc0972014-04-08 09:45:47 +03002761 if (ar->monitor_started)
Michal Kazior19337472014-08-28 12:58:16 +02002762 ath10k_monitor_stop(ar);
Michal Kaziorc930f742014-01-23 11:38:25 +01002763
2764 list_for_each_entry(arvif, &ar->arvifs, list) {
2765 if (!arvif->is_started)
2766 continue;
2767
Michal Kaziordc55e302014-07-29 12:53:36 +03002768 if (!arvif->is_up)
2769 continue;
2770
Michal Kaziorc930f742014-01-23 11:38:25 +01002771 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
2772 continue;
2773
Michal Kaziordc55e302014-07-29 12:53:36 +03002774 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
Michal Kaziorc930f742014-01-23 11:38:25 +01002775 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002776 ath10k_warn(ar, "failed to down vdev %d: %d\n",
Michal Kaziorc930f742014-01-23 11:38:25 +01002777 arvif->vdev_id, ret);
2778 continue;
2779 }
2780 }
2781
Michal Kaziordc55e302014-07-29 12:53:36 +03002782 /* all vdevs are downed now - attempt to restart and re-up them */
Michal Kaziorc930f742014-01-23 11:38:25 +01002783
2784 list_for_each_entry(arvif, &ar->arvifs, list) {
2785 if (!arvif->is_started)
2786 continue;
2787
2788 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
2789 continue;
2790
Michal Kaziordc55e302014-07-29 12:53:36 +03002791 ret = ath10k_vdev_restart(arvif);
Michal Kaziorc930f742014-01-23 11:38:25 +01002792 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002793 ath10k_warn(ar, "failed to restart vdev %d: %d\n",
Michal Kaziorc930f742014-01-23 11:38:25 +01002794 arvif->vdev_id, ret);
2795 continue;
2796 }
2797
2798 if (!arvif->is_up)
2799 continue;
2800
2801 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
2802 arvif->bssid);
2803 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002804 ath10k_warn(ar, "failed to bring vdev up %d: %d\n",
Michal Kaziorc930f742014-01-23 11:38:25 +01002805 arvif->vdev_id, ret);
2806 continue;
2807 }
2808 }
2809
Michal Kazior19337472014-08-28 12:58:16 +02002810 ath10k_monitor_recalc(ar);
Michal Kaziorc930f742014-01-23 11:38:25 +01002811}
2812
Michal Kazior7d9d5582014-10-21 10:40:15 +03002813static int ath10k_mac_txpower_setup(struct ath10k *ar, int txpower)
2814{
2815 int ret;
2816 u32 param;
2817
2818 lockdep_assert_held(&ar->conf_mutex);
2819
2820 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac txpower %d\n", txpower);
2821
2822 param = ar->wmi.pdev_param->txpower_limit2g;
2823 ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
2824 if (ret) {
2825 ath10k_warn(ar, "failed to set 2g txpower %d: %d\n",
2826 txpower, ret);
2827 return ret;
2828 }
2829
2830 param = ar->wmi.pdev_param->txpower_limit5g;
2831 ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
2832 if (ret) {
2833 ath10k_warn(ar, "failed to set 5g txpower %d: %d\n",
2834 txpower, ret);
2835 return ret;
2836 }
2837
2838 return 0;
2839}
2840
2841static int ath10k_mac_txpower_recalc(struct ath10k *ar)
2842{
2843 struct ath10k_vif *arvif;
2844 int ret, txpower = -1;
2845
2846 lockdep_assert_held(&ar->conf_mutex);
2847
2848 list_for_each_entry(arvif, &ar->arvifs, list) {
2849 WARN_ON(arvif->txpower < 0);
2850
2851 if (txpower == -1)
2852 txpower = arvif->txpower;
2853 else
2854 txpower = min(txpower, arvif->txpower);
2855 }
2856
2857 if (WARN_ON(txpower == -1))
2858 return -EINVAL;
2859
2860 ret = ath10k_mac_txpower_setup(ar, txpower);
2861 if (ret) {
2862 ath10k_warn(ar, "failed to setup tx power %d: %d\n",
2863 txpower, ret);
2864 return ret;
2865 }
2866
2867 return 0;
2868}
2869
Kalle Valo5e3dd152013-06-12 20:52:10 +03002870static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
2871{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002872 struct ath10k *ar = hw->priv;
2873 struct ieee80211_conf *conf = &hw->conf;
2874 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002875
2876 mutex_lock(&ar->conf_mutex);
2877
2878 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002879 ath10k_dbg(ar, ATH10K_DBG_MAC,
Michal Kaziord6500972014-04-08 09:56:09 +03002880 "mac config channel %dMHz flags 0x%x radar %d\n",
Marek Puzyniake8a50f82013-11-20 09:59:47 +02002881 conf->chandef.chan->center_freq,
Michal Kaziord6500972014-04-08 09:56:09 +03002882 conf->chandef.chan->flags,
2883 conf->radar_enabled);
Marek Puzyniake8a50f82013-11-20 09:59:47 +02002884
Kalle Valo5e3dd152013-06-12 20:52:10 +03002885 spin_lock_bh(&ar->data_lock);
2886 ar->rx_channel = conf->chandef.chan;
2887 spin_unlock_bh(&ar->data_lock);
Marek Puzyniake8a50f82013-11-20 09:59:47 +02002888
Michal Kaziord6500972014-04-08 09:56:09 +03002889 ar->radar_enabled = conf->radar_enabled;
2890 ath10k_recalc_radar_detection(ar);
Michal Kaziorc930f742014-01-23 11:38:25 +01002891
2892 if (!cfg80211_chandef_identical(&ar->chandef, &conf->chandef)) {
2893 ar->chandef = conf->chandef;
2894 ath10k_config_chan(ar);
2895 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002896 }
2897
Michal Kazioraffd3212013-07-16 09:54:35 +02002898 if (changed & IEEE80211_CONF_CHANGE_PS)
2899 ath10k_config_ps(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002900
2901 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
Michal Kazior19337472014-08-28 12:58:16 +02002902 ar->monitor = conf->flags & IEEE80211_CONF_MONITOR;
2903 ret = ath10k_monitor_recalc(ar);
2904 if (ret)
2905 ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002906 }
2907
2908 mutex_unlock(&ar->conf_mutex);
2909 return ret;
2910}
2911
Ben Greear5572a952014-11-24 16:22:10 +02002912static u32 get_nss_from_chainmask(u16 chain_mask)
2913{
2914 if ((chain_mask & 0x15) == 0x15)
2915 return 4;
2916 else if ((chain_mask & 0x7) == 0x7)
2917 return 3;
2918 else if ((chain_mask & 0x3) == 0x3)
2919 return 2;
2920 return 1;
2921}
2922
Kalle Valo5e3dd152013-06-12 20:52:10 +03002923/*
2924 * TODO:
2925 * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
2926 * because we will send mgmt frames without CCK. This requirement
2927 * for P2P_FIND/GO_NEG should be handled by checking CCK flag
2928 * in the TX packet.
2929 */
2930static int ath10k_add_interface(struct ieee80211_hw *hw,
2931 struct ieee80211_vif *vif)
2932{
2933 struct ath10k *ar = hw->priv;
2934 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2935 enum wmi_sta_powersave_param param;
2936 int ret = 0;
Kalle Valo5a13e762014-01-20 11:01:46 +02002937 u32 value;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002938 int bit;
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02002939 u32 vdev_param;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002940
2941 mutex_lock(&ar->conf_mutex);
2942
Michal Kazior0dbd09e2013-07-31 10:55:14 +02002943 memset(arvif, 0, sizeof(*arvif));
2944
Kalle Valo5e3dd152013-06-12 20:52:10 +03002945 arvif->ar = ar;
2946 arvif->vif = vif;
2947
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002948 INIT_WORK(&arvif->wep_key_work, ath10k_tx_wep_key_work);
Ben Greeare63b33f2013-10-22 14:54:14 -07002949 INIT_LIST_HEAD(&arvif->list);
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002950
Ben Greeara9aefb32014-08-12 11:02:19 +03002951 if (ar->free_vdev_map == 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002952 ath10k_warn(ar, "Free vdev map is empty, no more interfaces allowed.\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002953 ret = -EBUSY;
Michal Kazior9dad14a2013-10-16 15:44:45 +03002954 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002955 }
Ben Greear16c11172014-09-23 14:17:16 -07002956 bit = __ffs64(ar->free_vdev_map);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002957
Ben Greear16c11172014-09-23 14:17:16 -07002958 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac create vdev %i map %llx\n",
2959 bit, ar->free_vdev_map);
2960
2961 arvif->vdev_id = bit;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002962 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002963
Kalle Valo5e3dd152013-06-12 20:52:10 +03002964 switch (vif->type) {
Michal Kazior75d2bd42014-12-12 12:41:39 +01002965 case NL80211_IFTYPE_P2P_DEVICE:
2966 arvif->vdev_type = WMI_VDEV_TYPE_STA;
2967 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
2968 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002969 case NL80211_IFTYPE_UNSPECIFIED:
2970 case NL80211_IFTYPE_STATION:
2971 arvif->vdev_type = WMI_VDEV_TYPE_STA;
2972 if (vif->p2p)
2973 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
2974 break;
2975 case NL80211_IFTYPE_ADHOC:
2976 arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
2977 break;
2978 case NL80211_IFTYPE_AP:
2979 arvif->vdev_type = WMI_VDEV_TYPE_AP;
2980
2981 if (vif->p2p)
2982 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
2983 break;
2984 case NL80211_IFTYPE_MONITOR:
2985 arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
2986 break;
2987 default:
2988 WARN_ON(1);
2989 break;
2990 }
2991
Michal Kazior64badcb2014-09-18 11:18:02 +03002992 /* Some firmware revisions don't wait for beacon tx completion before
2993 * sending another SWBA event. This could lead to hardware using old
2994 * (freed) beacon data in some cases, e.g. tx credit starvation
2995 * combined with missed TBTT. This is very very rare.
2996 *
2997 * On non-IOMMU-enabled hosts this could be a possible security issue
2998 * because hw could beacon some random data on the air. On
2999 * IOMMU-enabled hosts DMAR faults would occur in most cases and target
3000 * device would crash.
3001 *
3002 * Since there are no beacon tx completions (implicit nor explicit)
3003 * propagated to host the only workaround for this is to allocate a
3004 * DMA-coherent buffer for a lifetime of a vif and use it for all
3005 * beacon tx commands. Worst case for this approach is some beacons may
3006 * become corrupted, e.g. have garbled IEs or out-of-date TIM bitmap.
3007 */
3008 if (vif->type == NL80211_IFTYPE_ADHOC ||
3009 vif->type == NL80211_IFTYPE_AP) {
3010 arvif->beacon_buf = dma_zalloc_coherent(ar->dev,
3011 IEEE80211_MAX_FRAME_LEN,
3012 &arvif->beacon_paddr,
Rajkumar Manoharan82d7aba2014-10-10 17:38:27 +05303013 GFP_ATOMIC);
Michal Kazior64badcb2014-09-18 11:18:02 +03003014 if (!arvif->beacon_buf) {
3015 ret = -ENOMEM;
3016 ath10k_warn(ar, "failed to allocate beacon buffer: %d\n",
3017 ret);
3018 goto err;
3019 }
3020 }
3021
3022 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d bcnmode %s\n",
3023 arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
3024 arvif->beacon_buf ? "single-buf" : "per-skb");
Kalle Valo5e3dd152013-06-12 20:52:10 +03003025
3026 ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
3027 arvif->vdev_subtype, vif->addr);
3028 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003029 ath10k_warn(ar, "failed to create WMI vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003030 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003031 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003032 }
3033
Ben Greear16c11172014-09-23 14:17:16 -07003034 ar->free_vdev_map &= ~(1LL << arvif->vdev_id);
Michal Kazior05791192013-10-16 15:44:45 +03003035 list_add(&arvif->list, &ar->arvifs);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003036
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003037 vdev_param = ar->wmi.vdev_param->def_keyid;
3038 ret = ath10k_wmi_vdev_set_param(ar, 0, vdev_param,
Michal Kaziorcc4827b2013-10-16 15:44:45 +03003039 arvif->def_wep_key_idx);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003040 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003041 ath10k_warn(ar, "failed to set vdev %i default key id: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003042 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003043 goto err_vdev_delete;
3044 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003045
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003046 vdev_param = ar->wmi.vdev_param->tx_encap_type;
3047 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003048 ATH10K_HW_TXRX_NATIVE_WIFI);
Bartosz Markowskiebc9abd2013-10-15 09:26:20 +02003049 /* 10.X firmware does not support this VDEV parameter. Do not warn */
Michal Kazior9dad14a2013-10-16 15:44:45 +03003050 if (ret && ret != -EOPNOTSUPP) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003051 ath10k_warn(ar, "failed to set vdev %i TX encapsulation: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003052 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003053 goto err_vdev_delete;
3054 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003055
Ben Greear5572a952014-11-24 16:22:10 +02003056 if (ar->cfg_tx_chainmask) {
3057 u16 nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
3058
3059 vdev_param = ar->wmi.vdev_param->nss;
3060 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3061 nss);
3062 if (ret) {
3063 ath10k_warn(ar, "failed to set vdev %i chainmask 0x%x, nss %i: %d\n",
3064 arvif->vdev_id, ar->cfg_tx_chainmask, nss,
3065 ret);
3066 goto err_vdev_delete;
3067 }
3068 }
3069
Kalle Valo5e3dd152013-06-12 20:52:10 +03003070 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3071 ret = ath10k_peer_create(ar, arvif->vdev_id, vif->addr);
3072 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003073 ath10k_warn(ar, "failed to create vdev %i peer for AP: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003074 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003075 goto err_vdev_delete;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003076 }
Marek Puzyniakcdf07402013-12-30 09:07:51 +01003077
Kalle Valo5a13e762014-01-20 11:01:46 +02003078 ret = ath10k_mac_set_kickout(arvif);
3079 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003080 ath10k_warn(ar, "failed to set vdev %i kickout parameters: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003081 arvif->vdev_id, ret);
Kalle Valo5a13e762014-01-20 11:01:46 +02003082 goto err_peer_delete;
3083 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003084 }
3085
3086 if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
3087 param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
3088 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
3089 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3090 param, value);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003091 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003092 ath10k_warn(ar, "failed to set vdev %i RX wake policy: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003093 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003094 goto err_peer_delete;
3095 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003096
Michal Kazior9f9b5742014-12-12 12:41:36 +01003097 ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003098 if (ret) {
Michal Kazior9f9b5742014-12-12 12:41:36 +01003099 ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003100 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003101 goto err_peer_delete;
3102 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003103
Michal Kazior9f9b5742014-12-12 12:41:36 +01003104 ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003105 if (ret) {
Michal Kazior9f9b5742014-12-12 12:41:36 +01003106 ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003107 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003108 goto err_peer_delete;
3109 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003110 }
3111
Michal Kazior424121c2013-07-22 14:13:31 +02003112 ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003113 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003114 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
Michal Kazior679c54a2013-07-05 16:15:04 +03003115 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003116 goto err_peer_delete;
3117 }
Michal Kazior679c54a2013-07-05 16:15:04 +03003118
Michal Kazior424121c2013-07-22 14:13:31 +02003119 ret = ath10k_mac_set_frag(arvif, ar->hw->wiphy->frag_threshold);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003120 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003121 ath10k_warn(ar, "failed to set frag threshold for vdev %d: %d\n",
Michal Kazior679c54a2013-07-05 16:15:04 +03003122 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003123 goto err_peer_delete;
3124 }
Michal Kazior679c54a2013-07-05 16:15:04 +03003125
Michal Kazior7d9d5582014-10-21 10:40:15 +03003126 arvif->txpower = vif->bss_conf.txpower;
3127 ret = ath10k_mac_txpower_recalc(ar);
3128 if (ret) {
3129 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
3130 goto err_peer_delete;
3131 }
3132
Kalle Valo5e3dd152013-06-12 20:52:10 +03003133 mutex_unlock(&ar->conf_mutex);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003134 return 0;
3135
3136err_peer_delete:
3137 if (arvif->vdev_type == WMI_VDEV_TYPE_AP)
3138 ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
3139
3140err_vdev_delete:
3141 ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
Ben Greear16c11172014-09-23 14:17:16 -07003142 ar->free_vdev_map |= 1LL << arvif->vdev_id;
Michal Kazior05791192013-10-16 15:44:45 +03003143 list_del(&arvif->list);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003144
3145err:
Michal Kazior64badcb2014-09-18 11:18:02 +03003146 if (arvif->beacon_buf) {
3147 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
3148 arvif->beacon_buf, arvif->beacon_paddr);
3149 arvif->beacon_buf = NULL;
3150 }
3151
Michal Kazior9dad14a2013-10-16 15:44:45 +03003152 mutex_unlock(&ar->conf_mutex);
3153
Kalle Valo5e3dd152013-06-12 20:52:10 +03003154 return ret;
3155}
3156
3157static void ath10k_remove_interface(struct ieee80211_hw *hw,
3158 struct ieee80211_vif *vif)
3159{
3160 struct ath10k *ar = hw->priv;
3161 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3162 int ret;
3163
Michal Kaziorcc4827b2013-10-16 15:44:45 +03003164 cancel_work_sync(&arvif->wep_key_work);
3165
Sujith Manoharan5d011f52014-11-25 11:47:00 +05303166 mutex_lock(&ar->conf_mutex);
3167
Michal Kaziored543882013-09-13 14:16:56 +02003168 spin_lock_bh(&ar->data_lock);
Michal Kazior64badcb2014-09-18 11:18:02 +03003169 ath10k_mac_vif_beacon_cleanup(arvif);
Michal Kaziored543882013-09-13 14:16:56 +02003170 spin_unlock_bh(&ar->data_lock);
3171
Simon Wunderlich855aed12014-08-02 09:12:54 +03003172 ret = ath10k_spectral_vif_stop(arvif);
3173 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003174 ath10k_warn(ar, "failed to stop spectral for vdev %i: %d\n",
Simon Wunderlich855aed12014-08-02 09:12:54 +03003175 arvif->vdev_id, ret);
3176
Ben Greear16c11172014-09-23 14:17:16 -07003177 ar->free_vdev_map |= 1LL << arvif->vdev_id;
Michal Kazior05791192013-10-16 15:44:45 +03003178 list_del(&arvif->list);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003179
3180 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3181 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, vif->addr);
3182 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003183 ath10k_warn(ar, "failed to remove peer for AP vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003184 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003185
3186 kfree(arvif->u.ap.noa_data);
3187 }
3188
Michal Kazior7aa7a722014-08-25 12:09:38 +02003189 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i delete (remove interface)\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03003190 arvif->vdev_id);
3191
Kalle Valo5e3dd152013-06-12 20:52:10 +03003192 ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
3193 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003194 ath10k_warn(ar, "failed to delete WMI vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003195 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003196
Kalle Valo5e3dd152013-06-12 20:52:10 +03003197 ath10k_peer_cleanup(ar, arvif->vdev_id);
3198
3199 mutex_unlock(&ar->conf_mutex);
3200}
3201
3202/*
3203 * FIXME: Has to be verified.
3204 */
3205#define SUPPORTED_FILTERS \
3206 (FIF_PROMISC_IN_BSS | \
3207 FIF_ALLMULTI | \
3208 FIF_CONTROL | \
3209 FIF_PSPOLL | \
3210 FIF_OTHER_BSS | \
3211 FIF_BCN_PRBRESP_PROMISC | \
3212 FIF_PROBE_REQ | \
3213 FIF_FCSFAIL)
3214
3215static void ath10k_configure_filter(struct ieee80211_hw *hw,
3216 unsigned int changed_flags,
3217 unsigned int *total_flags,
3218 u64 multicast)
3219{
3220 struct ath10k *ar = hw->priv;
3221 int ret;
3222
3223 mutex_lock(&ar->conf_mutex);
3224
3225 changed_flags &= SUPPORTED_FILTERS;
3226 *total_flags &= SUPPORTED_FILTERS;
3227 ar->filter_flags = *total_flags;
3228
Michal Kazior19337472014-08-28 12:58:16 +02003229 ret = ath10k_monitor_recalc(ar);
3230 if (ret)
3231 ath10k_warn(ar, "failed to recalc montior: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003232
3233 mutex_unlock(&ar->conf_mutex);
3234}
3235
3236static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
3237 struct ieee80211_vif *vif,
3238 struct ieee80211_bss_conf *info,
3239 u32 changed)
3240{
3241 struct ath10k *ar = hw->priv;
3242 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3243 int ret = 0;
Kalle Valoaf762c02014-09-14 12:50:17 +03003244 u32 vdev_param, pdev_param, slottime, preamble;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003245
3246 mutex_lock(&ar->conf_mutex);
3247
3248 if (changed & BSS_CHANGED_IBSS)
3249 ath10k_control_ibss(arvif, info, vif->addr);
3250
3251 if (changed & BSS_CHANGED_BEACON_INT) {
3252 arvif->beacon_interval = info->beacon_int;
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003253 vdev_param = ar->wmi.vdev_param->beacon_interval;
3254 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003255 arvif->beacon_interval);
Michal Kazior7aa7a722014-08-25 12:09:38 +02003256 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003257 "mac vdev %d beacon_interval %d\n",
3258 arvif->vdev_id, arvif->beacon_interval);
3259
Kalle Valo5e3dd152013-06-12 20:52:10 +03003260 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003261 ath10k_warn(ar, "failed to set beacon interval for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003262 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003263 }
3264
3265 if (changed & BSS_CHANGED_BEACON) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003266 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003267 "vdev %d set beacon tx mode to staggered\n",
3268 arvif->vdev_id);
3269
Bartosz Markowski226a3392013-09-26 17:47:16 +02003270 pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
3271 ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003272 WMI_BEACON_STAGGERED_MODE);
3273 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003274 ath10k_warn(ar, "failed to set beacon mode for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003275 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003276 }
3277
John W. Linvilleb70727e2013-06-13 13:34:29 -04003278 if (changed & BSS_CHANGED_BEACON_INFO) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03003279 arvif->dtim_period = info->dtim_period;
3280
Michal Kazior7aa7a722014-08-25 12:09:38 +02003281 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003282 "mac vdev %d dtim_period %d\n",
3283 arvif->vdev_id, arvif->dtim_period);
3284
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003285 vdev_param = ar->wmi.vdev_param->dtim_period;
3286 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003287 arvif->dtim_period);
3288 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003289 ath10k_warn(ar, "failed to set dtim period for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003290 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003291 }
3292
3293 if (changed & BSS_CHANGED_SSID &&
3294 vif->type == NL80211_IFTYPE_AP) {
3295 arvif->u.ap.ssid_len = info->ssid_len;
3296 if (info->ssid_len)
3297 memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
3298 arvif->u.ap.hidden_ssid = info->hidden_ssid;
3299 }
3300
Michal Kazior077efc82014-10-21 10:10:29 +03003301 if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
3302 ether_addr_copy(arvif->bssid, info->bssid);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003303
3304 if (changed & BSS_CHANGED_BEACON_ENABLED)
3305 ath10k_control_beaconing(arvif, info);
3306
3307 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02003308 arvif->use_cts_prot = info->use_cts_prot;
Michal Kazior7aa7a722014-08-25 12:09:38 +02003309 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d cts_prot %d\n",
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02003310 arvif->vdev_id, info->use_cts_prot);
Kalle Valo60c3daa2013-09-08 17:56:07 +03003311
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02003312 ret = ath10k_recalc_rtscts_prot(arvif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003313 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003314 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003315 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003316 }
3317
3318 if (changed & BSS_CHANGED_ERP_SLOT) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03003319 if (info->use_short_slot)
3320 slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
3321
3322 else
3323 slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
3324
Michal Kazior7aa7a722014-08-25 12:09:38 +02003325 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03003326 arvif->vdev_id, slottime);
3327
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003328 vdev_param = ar->wmi.vdev_param->slot_time;
3329 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003330 slottime);
3331 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003332 ath10k_warn(ar, "failed to set erp slot for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003333 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003334 }
3335
3336 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03003337 if (info->use_short_preamble)
3338 preamble = WMI_VDEV_PREAMBLE_SHORT;
3339 else
3340 preamble = WMI_VDEV_PREAMBLE_LONG;
3341
Michal Kazior7aa7a722014-08-25 12:09:38 +02003342 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003343 "mac vdev %d preamble %dn",
3344 arvif->vdev_id, preamble);
3345
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003346 vdev_param = ar->wmi.vdev_param->preamble;
3347 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003348 preamble);
3349 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003350 ath10k_warn(ar, "failed to set preamble for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003351 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003352 }
3353
3354 if (changed & BSS_CHANGED_ASSOC) {
Michal Kaziore556f112014-08-28 12:58:17 +02003355 if (info->assoc) {
3356 /* Workaround: Make sure monitor vdev is not running
3357 * when associating to prevent some firmware revisions
3358 * (e.g. 10.1 and 10.2) from crashing.
3359 */
3360 if (ar->monitor_started)
3361 ath10k_monitor_stop(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003362 ath10k_bss_assoc(hw, vif, info);
Michal Kaziore556f112014-08-28 12:58:17 +02003363 ath10k_monitor_recalc(ar);
Michal Kazior077efc82014-10-21 10:10:29 +03003364 } else {
3365 ath10k_bss_disassoc(hw, vif);
Michal Kaziore556f112014-08-28 12:58:17 +02003366 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003367 }
3368
Michal Kazior7d9d5582014-10-21 10:40:15 +03003369 if (changed & BSS_CHANGED_TXPOWER) {
3370 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev_id %i txpower %d\n",
3371 arvif->vdev_id, info->txpower);
3372
3373 arvif->txpower = info->txpower;
3374 ret = ath10k_mac_txpower_recalc(ar);
3375 if (ret)
3376 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
3377 }
3378
Michal Kaziorbf14e652014-12-12 12:41:38 +01003379 if (changed & BSS_CHANGED_PS) {
3380 ret = ath10k_mac_vif_setup_ps(arvif);
3381 if (ret)
3382 ath10k_warn(ar, "failed to setup ps on vdev %i: %d\n",
3383 arvif->vdev_id, ret);
3384 }
3385
Kalle Valo5e3dd152013-06-12 20:52:10 +03003386 mutex_unlock(&ar->conf_mutex);
3387}
3388
3389static int ath10k_hw_scan(struct ieee80211_hw *hw,
3390 struct ieee80211_vif *vif,
David Spinadelc56ef672014-02-05 15:21:13 +02003391 struct ieee80211_scan_request *hw_req)
Kalle Valo5e3dd152013-06-12 20:52:10 +03003392{
3393 struct ath10k *ar = hw->priv;
3394 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
David Spinadelc56ef672014-02-05 15:21:13 +02003395 struct cfg80211_scan_request *req = &hw_req->req;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003396 struct wmi_start_scan_arg arg;
3397 int ret = 0;
3398 int i;
3399
3400 mutex_lock(&ar->conf_mutex);
3401
3402 spin_lock_bh(&ar->data_lock);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003403 switch (ar->scan.state) {
3404 case ATH10K_SCAN_IDLE:
3405 reinit_completion(&ar->scan.started);
3406 reinit_completion(&ar->scan.completed);
3407 ar->scan.state = ATH10K_SCAN_STARTING;
3408 ar->scan.is_roc = false;
3409 ar->scan.vdev_id = arvif->vdev_id;
3410 ret = 0;
3411 break;
3412 case ATH10K_SCAN_STARTING:
3413 case ATH10K_SCAN_RUNNING:
3414 case ATH10K_SCAN_ABORTING:
Kalle Valo5e3dd152013-06-12 20:52:10 +03003415 ret = -EBUSY;
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003416 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003417 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003418 spin_unlock_bh(&ar->data_lock);
3419
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003420 if (ret)
3421 goto exit;
3422
Kalle Valo5e3dd152013-06-12 20:52:10 +03003423 memset(&arg, 0, sizeof(arg));
3424 ath10k_wmi_start_scan_init(ar, &arg);
3425 arg.vdev_id = arvif->vdev_id;
3426 arg.scan_id = ATH10K_SCAN_ID;
3427
3428 if (!req->no_cck)
3429 arg.scan_ctrl_flags |= WMI_SCAN_ADD_CCK_RATES;
3430
3431 if (req->ie_len) {
3432 arg.ie_len = req->ie_len;
3433 memcpy(arg.ie, req->ie, arg.ie_len);
3434 }
3435
3436 if (req->n_ssids) {
3437 arg.n_ssids = req->n_ssids;
3438 for (i = 0; i < arg.n_ssids; i++) {
3439 arg.ssids[i].len = req->ssids[i].ssid_len;
3440 arg.ssids[i].ssid = req->ssids[i].ssid;
3441 }
Michal Kaziordcd4a562013-07-31 10:55:12 +02003442 } else {
3443 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003444 }
3445
3446 if (req->n_channels) {
3447 arg.n_channels = req->n_channels;
3448 for (i = 0; i < arg.n_channels; i++)
3449 arg.channels[i] = req->channels[i]->center_freq;
3450 }
3451
3452 ret = ath10k_start_scan(ar, &arg);
3453 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003454 ath10k_warn(ar, "failed to start hw scan: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003455 spin_lock_bh(&ar->data_lock);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003456 ar->scan.state = ATH10K_SCAN_IDLE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003457 spin_unlock_bh(&ar->data_lock);
3458 }
3459
3460exit:
3461 mutex_unlock(&ar->conf_mutex);
3462 return ret;
3463}
3464
3465static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
3466 struct ieee80211_vif *vif)
3467{
3468 struct ath10k *ar = hw->priv;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003469
3470 mutex_lock(&ar->conf_mutex);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003471 ath10k_scan_abort(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003472 mutex_unlock(&ar->conf_mutex);
Michal Kazior4eb2e162014-10-28 10:23:09 +01003473
3474 cancel_delayed_work_sync(&ar->scan.timeout);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003475}
3476
Michal Kaziorcfb27d22013-12-02 09:06:36 +01003477static void ath10k_set_key_h_def_keyidx(struct ath10k *ar,
3478 struct ath10k_vif *arvif,
3479 enum set_key_cmd cmd,
3480 struct ieee80211_key_conf *key)
3481{
3482 u32 vdev_param = arvif->ar->wmi.vdev_param->def_keyid;
3483 int ret;
3484
3485 /* 10.1 firmware branch requires default key index to be set to group
3486 * key index after installing it. Otherwise FW/HW Txes corrupted
3487 * frames with multi-vif APs. This is not required for main firmware
3488 * branch (e.g. 636).
3489 *
3490 * FIXME: This has been tested only in AP. It remains unknown if this
3491 * is required for multi-vif STA interfaces on 10.1 */
3492
3493 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
3494 return;
3495
3496 if (key->cipher == WLAN_CIPHER_SUITE_WEP40)
3497 return;
3498
3499 if (key->cipher == WLAN_CIPHER_SUITE_WEP104)
3500 return;
3501
3502 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3503 return;
3504
3505 if (cmd != SET_KEY)
3506 return;
3507
3508 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3509 key->keyidx);
3510 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003511 ath10k_warn(ar, "failed to set vdev %i group key as default key: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003512 arvif->vdev_id, ret);
Michal Kaziorcfb27d22013-12-02 09:06:36 +01003513}
3514
Kalle Valo5e3dd152013-06-12 20:52:10 +03003515static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
3516 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
3517 struct ieee80211_key_conf *key)
3518{
3519 struct ath10k *ar = hw->priv;
3520 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3521 struct ath10k_peer *peer;
3522 const u8 *peer_addr;
3523 bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3524 key->cipher == WLAN_CIPHER_SUITE_WEP104;
3525 int ret = 0;
3526
3527 if (key->keyidx > WMI_MAX_KEY_INDEX)
3528 return -ENOSPC;
3529
3530 mutex_lock(&ar->conf_mutex);
3531
3532 if (sta)
3533 peer_addr = sta->addr;
3534 else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
3535 peer_addr = vif->bss_conf.bssid;
3536 else
3537 peer_addr = vif->addr;
3538
3539 key->hw_key_idx = key->keyidx;
3540
3541 /* the peer should not disappear in mid-way (unless FW goes awry) since
3542 * we already hold conf_mutex. we just make sure its there now. */
3543 spin_lock_bh(&ar->data_lock);
3544 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
3545 spin_unlock_bh(&ar->data_lock);
3546
3547 if (!peer) {
3548 if (cmd == SET_KEY) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003549 ath10k_warn(ar, "failed to install key for non-existent peer %pM\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03003550 peer_addr);
3551 ret = -EOPNOTSUPP;
3552 goto exit;
3553 } else {
3554 /* if the peer doesn't exist there is no key to disable
3555 * anymore */
3556 goto exit;
3557 }
3558 }
3559
3560 if (is_wep) {
3561 if (cmd == SET_KEY)
3562 arvif->wep_keys[key->keyidx] = key;
3563 else
3564 arvif->wep_keys[key->keyidx] = NULL;
3565
3566 if (cmd == DISABLE_KEY)
3567 ath10k_clear_vdev_key(arvif, key);
3568 }
3569
3570 ret = ath10k_install_key(arvif, key, cmd, peer_addr);
3571 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003572 ath10k_warn(ar, "failed to install key for vdev %i peer %pM: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003573 arvif->vdev_id, peer_addr, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003574 goto exit;
3575 }
3576
Michal Kaziorcfb27d22013-12-02 09:06:36 +01003577 ath10k_set_key_h_def_keyidx(ar, arvif, cmd, key);
3578
Kalle Valo5e3dd152013-06-12 20:52:10 +03003579 spin_lock_bh(&ar->data_lock);
3580 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
3581 if (peer && cmd == SET_KEY)
3582 peer->keys[key->keyidx] = key;
3583 else if (peer && cmd == DISABLE_KEY)
3584 peer->keys[key->keyidx] = NULL;
3585 else if (peer == NULL)
3586 /* impossible unless FW goes crazy */
Michal Kazior7aa7a722014-08-25 12:09:38 +02003587 ath10k_warn(ar, "Peer %pM disappeared!\n", peer_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003588 spin_unlock_bh(&ar->data_lock);
3589
3590exit:
3591 mutex_unlock(&ar->conf_mutex);
3592 return ret;
3593}
3594
Michal Kazior9797feb2014-02-14 14:49:48 +01003595static void ath10k_sta_rc_update_wk(struct work_struct *wk)
3596{
3597 struct ath10k *ar;
3598 struct ath10k_vif *arvif;
3599 struct ath10k_sta *arsta;
3600 struct ieee80211_sta *sta;
3601 u32 changed, bw, nss, smps;
3602 int err;
3603
3604 arsta = container_of(wk, struct ath10k_sta, update_wk);
3605 sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
3606 arvif = arsta->arvif;
3607 ar = arvif->ar;
3608
3609 spin_lock_bh(&ar->data_lock);
3610
3611 changed = arsta->changed;
3612 arsta->changed = 0;
3613
3614 bw = arsta->bw;
3615 nss = arsta->nss;
3616 smps = arsta->smps;
3617
3618 spin_unlock_bh(&ar->data_lock);
3619
3620 mutex_lock(&ar->conf_mutex);
3621
3622 if (changed & IEEE80211_RC_BW_CHANGED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003623 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM peer bw %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003624 sta->addr, bw);
3625
3626 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
3627 WMI_PEER_CHAN_WIDTH, bw);
3628 if (err)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003629 ath10k_warn(ar, "failed to update STA %pM peer bw %d: %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003630 sta->addr, bw, err);
3631 }
3632
3633 if (changed & IEEE80211_RC_NSS_CHANGED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003634 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM nss %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003635 sta->addr, nss);
3636
3637 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
3638 WMI_PEER_NSS, nss);
3639 if (err)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003640 ath10k_warn(ar, "failed to update STA %pM nss %d: %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003641 sta->addr, nss, err);
3642 }
3643
3644 if (changed & IEEE80211_RC_SMPS_CHANGED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003645 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM smps %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003646 sta->addr, smps);
3647
3648 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
3649 WMI_PEER_SMPS_STATE, smps);
3650 if (err)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003651 ath10k_warn(ar, "failed to update STA %pM smps %d: %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003652 sta->addr, smps, err);
3653 }
3654
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02003655 if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003656 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM supp rates\n",
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02003657 sta->addr);
3658
Michal Kazior590922a2014-10-21 10:10:29 +03003659 err = ath10k_station_assoc(ar, arvif->vif, sta, true);
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02003660 if (err)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003661 ath10k_warn(ar, "failed to reassociate station: %pM\n",
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02003662 sta->addr);
3663 }
3664
Michal Kazior9797feb2014-02-14 14:49:48 +01003665 mutex_unlock(&ar->conf_mutex);
3666}
3667
Michal Kaziorcfd10612014-11-25 15:16:05 +01003668static int ath10k_mac_inc_num_stations(struct ath10k_vif *arvif)
3669{
3670 struct ath10k *ar = arvif->ar;
3671
3672 lockdep_assert_held(&ar->conf_mutex);
3673
3674 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
3675 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
3676 return 0;
3677
3678 if (ar->num_stations >= ar->max_num_stations)
3679 return -ENOBUFS;
3680
3681 ar->num_stations++;
3682
3683 return 0;
3684}
3685
3686static void ath10k_mac_dec_num_stations(struct ath10k_vif *arvif)
3687{
3688 struct ath10k *ar = arvif->ar;
3689
3690 lockdep_assert_held(&ar->conf_mutex);
3691
3692 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
3693 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
3694 return;
3695
3696 ar->num_stations--;
3697}
3698
Kalle Valo5e3dd152013-06-12 20:52:10 +03003699static int ath10k_sta_state(struct ieee80211_hw *hw,
3700 struct ieee80211_vif *vif,
3701 struct ieee80211_sta *sta,
3702 enum ieee80211_sta_state old_state,
3703 enum ieee80211_sta_state new_state)
3704{
3705 struct ath10k *ar = hw->priv;
3706 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Michal Kazior9797feb2014-02-14 14:49:48 +01003707 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003708 int ret = 0;
3709
Michal Kazior76f90022014-02-25 09:29:57 +02003710 if (old_state == IEEE80211_STA_NOTEXIST &&
3711 new_state == IEEE80211_STA_NONE) {
3712 memset(arsta, 0, sizeof(*arsta));
3713 arsta->arvif = arvif;
3714 INIT_WORK(&arsta->update_wk, ath10k_sta_rc_update_wk);
3715 }
3716
Michal Kazior9797feb2014-02-14 14:49:48 +01003717 /* cancel must be done outside the mutex to avoid deadlock */
3718 if ((old_state == IEEE80211_STA_NONE &&
3719 new_state == IEEE80211_STA_NOTEXIST))
3720 cancel_work_sync(&arsta->update_wk);
3721
Kalle Valo5e3dd152013-06-12 20:52:10 +03003722 mutex_lock(&ar->conf_mutex);
3723
3724 if (old_state == IEEE80211_STA_NOTEXIST &&
Michal Kazior077efc82014-10-21 10:10:29 +03003725 new_state == IEEE80211_STA_NONE) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03003726 /*
3727 * New station addition.
3728 */
Michal Kaziorcfd10612014-11-25 15:16:05 +01003729 ath10k_dbg(ar, ATH10K_DBG_MAC,
3730 "mac vdev %d peer create %pM (new sta) sta %d / %d peer %d / %d\n",
3731 arvif->vdev_id, sta->addr,
3732 ar->num_stations + 1, ar->max_num_stations,
3733 ar->num_peers + 1, ar->max_num_peers);
Bartosz Markowski0e759f32014-01-02 14:38:33 +01003734
Michal Kaziorcfd10612014-11-25 15:16:05 +01003735 ret = ath10k_mac_inc_num_stations(arvif);
3736 if (ret) {
3737 ath10k_warn(ar, "refusing to associate station: too many connected already (%d)\n",
3738 ar->max_num_stations);
Bartosz Markowski0e759f32014-01-02 14:38:33 +01003739 goto exit;
3740 }
3741
Kalle Valo5e3dd152013-06-12 20:52:10 +03003742 ret = ath10k_peer_create(ar, arvif->vdev_id, sta->addr);
Michal Kaziora52c0282014-11-25 15:16:03 +01003743 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003744 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 -08003745 sta->addr, arvif->vdev_id, ret);
Michal Kaziorcfd10612014-11-25 15:16:05 +01003746 ath10k_mac_dec_num_stations(arvif);
Michal Kaziora52c0282014-11-25 15:16:03 +01003747 goto exit;
3748 }
Michal Kazior077efc82014-10-21 10:10:29 +03003749
3750 if (vif->type == NL80211_IFTYPE_STATION) {
3751 WARN_ON(arvif->is_started);
3752
3753 ret = ath10k_vdev_start(arvif);
3754 if (ret) {
3755 ath10k_warn(ar, "failed to start vdev %i: %d\n",
3756 arvif->vdev_id, ret);
3757 WARN_ON(ath10k_peer_delete(ar, arvif->vdev_id,
3758 sta->addr));
Michal Kaziorcfd10612014-11-25 15:16:05 +01003759 ath10k_mac_dec_num_stations(arvif);
Michal Kazior077efc82014-10-21 10:10:29 +03003760 goto exit;
3761 }
3762
3763 arvif->is_started = true;
3764 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003765 } else if ((old_state == IEEE80211_STA_NONE &&
3766 new_state == IEEE80211_STA_NOTEXIST)) {
3767 /*
3768 * Existing station deletion.
3769 */
Michal Kazior7aa7a722014-08-25 12:09:38 +02003770 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003771 "mac vdev %d peer delete %pM (sta gone)\n",
3772 arvif->vdev_id, sta->addr);
Michal Kazior077efc82014-10-21 10:10:29 +03003773
3774 if (vif->type == NL80211_IFTYPE_STATION) {
3775 WARN_ON(!arvif->is_started);
3776
3777 ret = ath10k_vdev_stop(arvif);
3778 if (ret)
3779 ath10k_warn(ar, "failed to stop vdev %i: %d\n",
3780 arvif->vdev_id, ret);
3781
3782 arvif->is_started = false;
3783 }
3784
Kalle Valo5e3dd152013-06-12 20:52:10 +03003785 ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
3786 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003787 ath10k_warn(ar, "failed to delete peer %pM for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003788 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003789
Michal Kaziorcfd10612014-11-25 15:16:05 +01003790 ath10k_mac_dec_num_stations(arvif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003791 } else if (old_state == IEEE80211_STA_AUTH &&
3792 new_state == IEEE80211_STA_ASSOC &&
3793 (vif->type == NL80211_IFTYPE_AP ||
3794 vif->type == NL80211_IFTYPE_ADHOC)) {
3795 /*
3796 * New association.
3797 */
Michal Kazior7aa7a722014-08-25 12:09:38 +02003798 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM associated\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03003799 sta->addr);
3800
Michal Kazior590922a2014-10-21 10:10:29 +03003801 ret = ath10k_station_assoc(ar, vif, sta, false);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003802 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003803 ath10k_warn(ar, "failed to associate station %pM for vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003804 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003805 } else if (old_state == IEEE80211_STA_ASSOC &&
3806 new_state == IEEE80211_STA_AUTH &&
3807 (vif->type == NL80211_IFTYPE_AP ||
3808 vif->type == NL80211_IFTYPE_ADHOC)) {
3809 /*
3810 * Disassociation.
3811 */
Michal Kazior7aa7a722014-08-25 12:09:38 +02003812 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM disassociated\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03003813 sta->addr);
3814
Michal Kazior590922a2014-10-21 10:10:29 +03003815 ret = ath10k_station_disassoc(ar, vif, sta);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003816 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003817 ath10k_warn(ar, "failed to disassociate station: %pM vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003818 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003819 }
Bartosz Markowski0e759f32014-01-02 14:38:33 +01003820exit:
Kalle Valo5e3dd152013-06-12 20:52:10 +03003821 mutex_unlock(&ar->conf_mutex);
3822 return ret;
3823}
3824
3825static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
Kalle Valo5b07e072014-09-14 12:50:06 +03003826 u16 ac, bool enable)
Kalle Valo5e3dd152013-06-12 20:52:10 +03003827{
3828 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3829 u32 value = 0;
3830 int ret = 0;
3831
Michal Kazior548db542013-07-05 16:15:15 +03003832 lockdep_assert_held(&ar->conf_mutex);
3833
Kalle Valo5e3dd152013-06-12 20:52:10 +03003834 if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
3835 return 0;
3836
3837 switch (ac) {
3838 case IEEE80211_AC_VO:
3839 value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
3840 WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
3841 break;
3842 case IEEE80211_AC_VI:
3843 value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
3844 WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
3845 break;
3846 case IEEE80211_AC_BE:
3847 value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
3848 WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
3849 break;
3850 case IEEE80211_AC_BK:
3851 value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
3852 WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
3853 break;
3854 }
3855
3856 if (enable)
3857 arvif->u.sta.uapsd |= value;
3858 else
3859 arvif->u.sta.uapsd &= ~value;
3860
3861 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3862 WMI_STA_PS_PARAM_UAPSD,
3863 arvif->u.sta.uapsd);
3864 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003865 ath10k_warn(ar, "failed to set uapsd params: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003866 goto exit;
3867 }
3868
3869 if (arvif->u.sta.uapsd)
3870 value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
3871 else
3872 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
3873
3874 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3875 WMI_STA_PS_PARAM_RX_WAKE_POLICY,
3876 value);
3877 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003878 ath10k_warn(ar, "failed to set rx wake param: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003879
Michal Kazior9f9b5742014-12-12 12:41:36 +01003880 ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
3881 if (ret) {
3882 ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
3883 arvif->vdev_id, ret);
3884 return ret;
3885 }
3886
3887 ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
3888 if (ret) {
3889 ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
3890 arvif->vdev_id, ret);
3891 return ret;
3892 }
3893
Kalle Valo5e3dd152013-06-12 20:52:10 +03003894exit:
3895 return ret;
3896}
3897
3898static int ath10k_conf_tx(struct ieee80211_hw *hw,
3899 struct ieee80211_vif *vif, u16 ac,
3900 const struct ieee80211_tx_queue_params *params)
3901{
3902 struct ath10k *ar = hw->priv;
3903 struct wmi_wmm_params_arg *p = NULL;
3904 int ret;
3905
3906 mutex_lock(&ar->conf_mutex);
3907
3908 switch (ac) {
3909 case IEEE80211_AC_VO:
3910 p = &ar->wmm_params.ac_vo;
3911 break;
3912 case IEEE80211_AC_VI:
3913 p = &ar->wmm_params.ac_vi;
3914 break;
3915 case IEEE80211_AC_BE:
3916 p = &ar->wmm_params.ac_be;
3917 break;
3918 case IEEE80211_AC_BK:
3919 p = &ar->wmm_params.ac_bk;
3920 break;
3921 }
3922
3923 if (WARN_ON(!p)) {
3924 ret = -EINVAL;
3925 goto exit;
3926 }
3927
3928 p->cwmin = params->cw_min;
3929 p->cwmax = params->cw_max;
3930 p->aifs = params->aifs;
3931
3932 /*
3933 * The channel time duration programmed in the HW is in absolute
3934 * microseconds, while mac80211 gives the txop in units of
3935 * 32 microseconds.
3936 */
3937 p->txop = params->txop * 32;
3938
3939 /* FIXME: FW accepts wmm params per hw, not per vif */
3940 ret = ath10k_wmi_pdev_set_wmm_params(ar, &ar->wmm_params);
3941 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003942 ath10k_warn(ar, "failed to set wmm params: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003943 goto exit;
3944 }
3945
3946 ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
3947 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003948 ath10k_warn(ar, "failed to set sta uapsd: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003949
3950exit:
3951 mutex_unlock(&ar->conf_mutex);
3952 return ret;
3953}
3954
3955#define ATH10K_ROC_TIMEOUT_HZ (2*HZ)
3956
3957static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
3958 struct ieee80211_vif *vif,
3959 struct ieee80211_channel *chan,
3960 int duration,
3961 enum ieee80211_roc_type type)
3962{
3963 struct ath10k *ar = hw->priv;
3964 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3965 struct wmi_start_scan_arg arg;
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003966 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003967
3968 mutex_lock(&ar->conf_mutex);
3969
3970 spin_lock_bh(&ar->data_lock);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003971 switch (ar->scan.state) {
3972 case ATH10K_SCAN_IDLE:
3973 reinit_completion(&ar->scan.started);
3974 reinit_completion(&ar->scan.completed);
3975 reinit_completion(&ar->scan.on_channel);
3976 ar->scan.state = ATH10K_SCAN_STARTING;
3977 ar->scan.is_roc = true;
3978 ar->scan.vdev_id = arvif->vdev_id;
3979 ar->scan.roc_freq = chan->center_freq;
3980 ret = 0;
3981 break;
3982 case ATH10K_SCAN_STARTING:
3983 case ATH10K_SCAN_RUNNING:
3984 case ATH10K_SCAN_ABORTING:
Kalle Valo5e3dd152013-06-12 20:52:10 +03003985 ret = -EBUSY;
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003986 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003987 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003988 spin_unlock_bh(&ar->data_lock);
3989
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003990 if (ret)
3991 goto exit;
3992
Michal Kaziordcca0bd2014-11-24 14:58:32 +01003993 duration = max(duration, WMI_SCAN_CHAN_MIN_TIME_MSEC);
3994
Kalle Valo5e3dd152013-06-12 20:52:10 +03003995 memset(&arg, 0, sizeof(arg));
3996 ath10k_wmi_start_scan_init(ar, &arg);
3997 arg.vdev_id = arvif->vdev_id;
3998 arg.scan_id = ATH10K_SCAN_ID;
3999 arg.n_channels = 1;
4000 arg.channels[0] = chan->center_freq;
4001 arg.dwell_time_active = duration;
4002 arg.dwell_time_passive = duration;
4003 arg.max_scan_time = 2 * duration;
4004 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
4005 arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
4006
4007 ret = ath10k_start_scan(ar, &arg);
4008 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004009 ath10k_warn(ar, "failed to start roc scan: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004010 spin_lock_bh(&ar->data_lock);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004011 ar->scan.state = ATH10K_SCAN_IDLE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004012 spin_unlock_bh(&ar->data_lock);
4013 goto exit;
4014 }
4015
4016 ret = wait_for_completion_timeout(&ar->scan.on_channel, 3*HZ);
4017 if (ret == 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004018 ath10k_warn(ar, "failed to switch to channel for roc scan\n");
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004019
4020 ret = ath10k_scan_stop(ar);
4021 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004022 ath10k_warn(ar, "failed to stop scan: %d\n", ret);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004023
Kalle Valo5e3dd152013-06-12 20:52:10 +03004024 ret = -ETIMEDOUT;
4025 goto exit;
4026 }
4027
4028 ret = 0;
4029exit:
4030 mutex_unlock(&ar->conf_mutex);
4031 return ret;
4032}
4033
4034static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw)
4035{
4036 struct ath10k *ar = hw->priv;
4037
4038 mutex_lock(&ar->conf_mutex);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004039 ath10k_scan_abort(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004040 mutex_unlock(&ar->conf_mutex);
4041
Michal Kazior4eb2e162014-10-28 10:23:09 +01004042 cancel_delayed_work_sync(&ar->scan.timeout);
4043
Kalle Valo5e3dd152013-06-12 20:52:10 +03004044 return 0;
4045}
4046
4047/*
4048 * Both RTS and Fragmentation threshold are interface-specific
4049 * in ath10k, but device-specific in mac80211.
4050 */
Kalle Valo5e3dd152013-06-12 20:52:10 +03004051
4052static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
4053{
Kalle Valo5e3dd152013-06-12 20:52:10 +03004054 struct ath10k *ar = hw->priv;
Michal Kaziorad088bf2013-10-16 15:44:46 +03004055 struct ath10k_vif *arvif;
4056 int ret = 0;
Michal Kazior548db542013-07-05 16:15:15 +03004057
Michal Kaziorad088bf2013-10-16 15:44:46 +03004058 mutex_lock(&ar->conf_mutex);
4059 list_for_each_entry(arvif, &ar->arvifs, list) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004060 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
Michal Kaziorad088bf2013-10-16 15:44:46 +03004061 arvif->vdev_id, value);
Kalle Valo60c3daa2013-09-08 17:56:07 +03004062
Michal Kaziorad088bf2013-10-16 15:44:46 +03004063 ret = ath10k_mac_set_rts(arvif, value);
4064 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004065 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
Michal Kaziorad088bf2013-10-16 15:44:46 +03004066 arvif->vdev_id, ret);
4067 break;
4068 }
4069 }
4070 mutex_unlock(&ar->conf_mutex);
4071
4072 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004073}
4074
Emmanuel Grumbach77be2c52014-03-27 11:30:29 +02004075static void ath10k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4076 u32 queues, bool drop)
Kalle Valo5e3dd152013-06-12 20:52:10 +03004077{
4078 struct ath10k *ar = hw->priv;
Michal Kazioraffd3212013-07-16 09:54:35 +02004079 bool skip;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004080 int ret;
4081
4082 /* mac80211 doesn't care if we really xmit queued frames or not
4083 * we'll collect those frames either way if we stop/delete vdevs */
4084 if (drop)
4085 return;
4086
Michal Kazior548db542013-07-05 16:15:15 +03004087 mutex_lock(&ar->conf_mutex);
4088
Michal Kazioraffd3212013-07-16 09:54:35 +02004089 if (ar->state == ATH10K_STATE_WEDGED)
4090 goto skip;
4091
Michal Kazioredb82362013-07-05 16:15:14 +03004092 ret = wait_event_timeout(ar->htt.empty_tx_wq, ({
Kalle Valo5e3dd152013-06-12 20:52:10 +03004093 bool empty;
Michal Kazioraffd3212013-07-16 09:54:35 +02004094
Michal Kazioredb82362013-07-05 16:15:14 +03004095 spin_lock_bh(&ar->htt.tx_lock);
Michal Kazior0945baf2013-09-18 14:43:18 +02004096 empty = (ar->htt.num_pending_tx == 0);
Michal Kazioredb82362013-07-05 16:15:14 +03004097 spin_unlock_bh(&ar->htt.tx_lock);
Michal Kazioraffd3212013-07-16 09:54:35 +02004098
Michal Kazior7962b0d2014-10-28 10:34:38 +01004099 skip = (ar->state == ATH10K_STATE_WEDGED) ||
4100 test_bit(ATH10K_FLAG_CRASH_FLUSH,
4101 &ar->dev_flags);
Michal Kazioraffd3212013-07-16 09:54:35 +02004102
4103 (empty || skip);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004104 }), ATH10K_FLUSH_TIMEOUT_HZ);
Michal Kazioraffd3212013-07-16 09:54:35 +02004105
4106 if (ret <= 0 || skip)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004107 ath10k_warn(ar, "failed to flush transmit queue (skip %i ar-state %i): %i\n",
Ben Greear9ba4c782014-02-25 09:29:57 +02004108 skip, ar->state, ret);
Michal Kazior548db542013-07-05 16:15:15 +03004109
Michal Kazioraffd3212013-07-16 09:54:35 +02004110skip:
Michal Kazior548db542013-07-05 16:15:15 +03004111 mutex_unlock(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004112}
4113
4114/* TODO: Implement this function properly
4115 * For now it is needed to reply to Probe Requests in IBSS mode.
4116 * Propably we need this information from FW.
4117 */
4118static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
4119{
4120 return 1;
4121}
4122
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004123#ifdef CONFIG_PM
4124static int ath10k_suspend(struct ieee80211_hw *hw,
4125 struct cfg80211_wowlan *wowlan)
4126{
4127 struct ath10k *ar = hw->priv;
4128 int ret;
4129
Marek Puzyniak9042e172014-02-10 17:14:23 +01004130 mutex_lock(&ar->conf_mutex);
4131
Marek Puzyniak00f54822014-02-10 17:14:24 +01004132 ret = ath10k_wait_for_suspend(ar, WMI_PDEV_SUSPEND);
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004133 if (ret) {
Marek Puzyniak00f54822014-02-10 17:14:24 +01004134 if (ret == -ETIMEDOUT)
4135 goto resume;
Marek Puzyniak9042e172014-02-10 17:14:23 +01004136 ret = 1;
4137 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004138 }
4139
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004140 ret = ath10k_hif_suspend(ar);
4141 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004142 ath10k_warn(ar, "failed to suspend hif: %d\n", ret);
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004143 goto resume;
4144 }
4145
Marek Puzyniak9042e172014-02-10 17:14:23 +01004146 ret = 0;
4147 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004148resume:
4149 ret = ath10k_wmi_pdev_resume_target(ar);
4150 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004151 ath10k_warn(ar, "failed to resume target: %d\n", ret);
Marek Puzyniak9042e172014-02-10 17:14:23 +01004152
4153 ret = 1;
4154exit:
4155 mutex_unlock(&ar->conf_mutex);
4156 return ret;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004157}
4158
4159static int ath10k_resume(struct ieee80211_hw *hw)
4160{
4161 struct ath10k *ar = hw->priv;
4162 int ret;
4163
Marek Puzyniak9042e172014-02-10 17:14:23 +01004164 mutex_lock(&ar->conf_mutex);
4165
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004166 ret = ath10k_hif_resume(ar);
4167 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004168 ath10k_warn(ar, "failed to resume hif: %d\n", ret);
Marek Puzyniak9042e172014-02-10 17:14:23 +01004169 ret = 1;
4170 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004171 }
4172
4173 ret = ath10k_wmi_pdev_resume_target(ar);
4174 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004175 ath10k_warn(ar, "failed to resume target: %d\n", ret);
Marek Puzyniak9042e172014-02-10 17:14:23 +01004176 ret = 1;
4177 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004178 }
4179
Marek Puzyniak9042e172014-02-10 17:14:23 +01004180 ret = 0;
4181exit:
4182 mutex_unlock(&ar->conf_mutex);
4183 return ret;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004184}
4185#endif
4186
Eliad Pellercf2c92d2014-11-04 11:43:54 +02004187static void ath10k_reconfig_complete(struct ieee80211_hw *hw,
4188 enum ieee80211_reconfig_type reconfig_type)
Michal Kazioraffd3212013-07-16 09:54:35 +02004189{
4190 struct ath10k *ar = hw->priv;
4191
Eliad Pellercf2c92d2014-11-04 11:43:54 +02004192 if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
4193 return;
4194
Michal Kazioraffd3212013-07-16 09:54:35 +02004195 mutex_lock(&ar->conf_mutex);
4196
4197 /* If device failed to restart it will be in a different state, e.g.
4198 * ATH10K_STATE_WEDGED */
4199 if (ar->state == ATH10K_STATE_RESTARTED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004200 ath10k_info(ar, "device successfully recovered\n");
Michal Kazioraffd3212013-07-16 09:54:35 +02004201 ar->state = ATH10K_STATE_ON;
Michal Kazior7962b0d2014-10-28 10:34:38 +01004202 ieee80211_wake_queues(ar->hw);
Michal Kazioraffd3212013-07-16 09:54:35 +02004203 }
4204
4205 mutex_unlock(&ar->conf_mutex);
4206}
4207
Michal Kazior2e1dea42013-07-31 10:32:40 +02004208static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
4209 struct survey_info *survey)
4210{
4211 struct ath10k *ar = hw->priv;
4212 struct ieee80211_supported_band *sband;
4213 struct survey_info *ar_survey = &ar->survey[idx];
4214 int ret = 0;
4215
4216 mutex_lock(&ar->conf_mutex);
4217
4218 sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
4219 if (sband && idx >= sband->n_channels) {
4220 idx -= sband->n_channels;
4221 sband = NULL;
4222 }
4223
4224 if (!sband)
4225 sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
4226
4227 if (!sband || idx >= sband->n_channels) {
4228 ret = -ENOENT;
4229 goto exit;
4230 }
4231
4232 spin_lock_bh(&ar->data_lock);
4233 memcpy(survey, ar_survey, sizeof(*survey));
4234 spin_unlock_bh(&ar->data_lock);
4235
4236 survey->channel = &sband->channels[idx];
4237
Felix Fietkaufa1d4df2014-10-23 17:04:28 +03004238 if (ar->rx_channel == survey->channel)
4239 survey->filled |= SURVEY_INFO_IN_USE;
4240
Michal Kazior2e1dea42013-07-31 10:32:40 +02004241exit:
4242 mutex_unlock(&ar->conf_mutex);
4243 return ret;
4244}
4245
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004246/* Helper table for legacy fixed_rate/bitrate_mask */
4247static const u8 cck_ofdm_rate[] = {
4248 /* CCK */
4249 3, /* 1Mbps */
4250 2, /* 2Mbps */
4251 1, /* 5.5Mbps */
4252 0, /* 11Mbps */
4253 /* OFDM */
4254 3, /* 6Mbps */
4255 7, /* 9Mbps */
4256 2, /* 12Mbps */
4257 6, /* 18Mbps */
4258 1, /* 24Mbps */
4259 5, /* 36Mbps */
4260 0, /* 48Mbps */
4261 4, /* 54Mbps */
4262};
4263
4264/* Check if only one bit set */
4265static int ath10k_check_single_mask(u32 mask)
4266{
4267 int bit;
4268
4269 bit = ffs(mask);
4270 if (!bit)
4271 return 0;
4272
4273 mask &= ~BIT(bit - 1);
4274 if (mask)
4275 return 2;
4276
4277 return 1;
4278}
4279
4280static bool
4281ath10k_default_bitrate_mask(struct ath10k *ar,
4282 enum ieee80211_band band,
4283 const struct cfg80211_bitrate_mask *mask)
4284{
4285 u32 legacy = 0x00ff;
4286 u8 ht = 0xff, i;
4287 u16 vht = 0x3ff;
Ben Greearb116ea12014-11-24 16:22:10 +02004288 u16 nrf = ar->num_rf_chains;
4289
4290 if (ar->cfg_tx_chainmask)
4291 nrf = get_nss_from_chainmask(ar->cfg_tx_chainmask);
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004292
4293 switch (band) {
4294 case IEEE80211_BAND_2GHZ:
4295 legacy = 0x00fff;
4296 vht = 0;
4297 break;
4298 case IEEE80211_BAND_5GHZ:
4299 break;
4300 default:
4301 return false;
4302 }
4303
4304 if (mask->control[band].legacy != legacy)
4305 return false;
4306
Ben Greearb116ea12014-11-24 16:22:10 +02004307 for (i = 0; i < nrf; i++)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004308 if (mask->control[band].ht_mcs[i] != ht)
4309 return false;
4310
Ben Greearb116ea12014-11-24 16:22:10 +02004311 for (i = 0; i < nrf; i++)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004312 if (mask->control[band].vht_mcs[i] != vht)
4313 return false;
4314
4315 return true;
4316}
4317
4318static bool
4319ath10k_bitrate_mask_nss(const struct cfg80211_bitrate_mask *mask,
4320 enum ieee80211_band band,
4321 u8 *fixed_nss)
4322{
4323 int ht_nss = 0, vht_nss = 0, i;
4324
4325 /* check legacy */
4326 if (ath10k_check_single_mask(mask->control[band].legacy))
4327 return false;
4328
4329 /* check HT */
4330 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
4331 if (mask->control[band].ht_mcs[i] == 0xff)
4332 continue;
4333 else if (mask->control[band].ht_mcs[i] == 0x00)
4334 break;
Kalle Valod8bb26b2014-09-14 12:50:33 +03004335
4336 return false;
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004337 }
4338
4339 ht_nss = i;
4340
4341 /* check VHT */
4342 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
4343 if (mask->control[band].vht_mcs[i] == 0x03ff)
4344 continue;
4345 else if (mask->control[band].vht_mcs[i] == 0x0000)
4346 break;
Kalle Valod8bb26b2014-09-14 12:50:33 +03004347
4348 return false;
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004349 }
4350
4351 vht_nss = i;
4352
4353 if (ht_nss > 0 && vht_nss > 0)
4354 return false;
4355
4356 if (ht_nss)
4357 *fixed_nss = ht_nss;
4358 else if (vht_nss)
4359 *fixed_nss = vht_nss;
4360 else
4361 return false;
4362
4363 return true;
4364}
4365
4366static bool
4367ath10k_bitrate_mask_correct(const struct cfg80211_bitrate_mask *mask,
4368 enum ieee80211_band band,
4369 enum wmi_rate_preamble *preamble)
4370{
4371 int legacy = 0, ht = 0, vht = 0, i;
4372
4373 *preamble = WMI_RATE_PREAMBLE_OFDM;
4374
4375 /* check legacy */
4376 legacy = ath10k_check_single_mask(mask->control[band].legacy);
4377 if (legacy > 1)
4378 return false;
4379
4380 /* check HT */
4381 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
4382 ht += ath10k_check_single_mask(mask->control[band].ht_mcs[i]);
4383 if (ht > 1)
4384 return false;
4385
4386 /* check VHT */
4387 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
4388 vht += ath10k_check_single_mask(mask->control[band].vht_mcs[i]);
4389 if (vht > 1)
4390 return false;
4391
4392 /* Currently we support only one fixed_rate */
4393 if ((legacy + ht + vht) != 1)
4394 return false;
4395
4396 if (ht)
4397 *preamble = WMI_RATE_PREAMBLE_HT;
4398 else if (vht)
4399 *preamble = WMI_RATE_PREAMBLE_VHT;
4400
4401 return true;
4402}
4403
4404static bool
Michal Kazior7aa7a722014-08-25 12:09:38 +02004405ath10k_bitrate_mask_rate(struct ath10k *ar,
4406 const struct cfg80211_bitrate_mask *mask,
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004407 enum ieee80211_band band,
4408 u8 *fixed_rate,
4409 u8 *fixed_nss)
4410{
4411 u8 rate = 0, pream = 0, nss = 0, i;
4412 enum wmi_rate_preamble preamble;
4413
4414 /* Check if single rate correct */
4415 if (!ath10k_bitrate_mask_correct(mask, band, &preamble))
4416 return false;
4417
4418 pream = preamble;
4419
4420 switch (preamble) {
4421 case WMI_RATE_PREAMBLE_CCK:
4422 case WMI_RATE_PREAMBLE_OFDM:
4423 i = ffs(mask->control[band].legacy) - 1;
4424
4425 if (band == IEEE80211_BAND_2GHZ && i < 4)
4426 pream = WMI_RATE_PREAMBLE_CCK;
4427
4428 if (band == IEEE80211_BAND_5GHZ)
4429 i += 4;
4430
4431 if (i >= ARRAY_SIZE(cck_ofdm_rate))
4432 return false;
4433
4434 rate = cck_ofdm_rate[i];
4435 break;
4436 case WMI_RATE_PREAMBLE_HT:
4437 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
4438 if (mask->control[band].ht_mcs[i])
4439 break;
4440
4441 if (i == IEEE80211_HT_MCS_MASK_LEN)
4442 return false;
4443
4444 rate = ffs(mask->control[band].ht_mcs[i]) - 1;
4445 nss = i;
4446 break;
4447 case WMI_RATE_PREAMBLE_VHT:
4448 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
4449 if (mask->control[band].vht_mcs[i])
4450 break;
4451
4452 if (i == NL80211_VHT_NSS_MAX)
4453 return false;
4454
4455 rate = ffs(mask->control[band].vht_mcs[i]) - 1;
4456 nss = i;
4457 break;
4458 }
4459
4460 *fixed_nss = nss + 1;
4461 nss <<= 4;
4462 pream <<= 6;
4463
Michal Kazior7aa7a722014-08-25 12:09:38 +02004464 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 +01004465 pream, nss, rate);
4466
4467 *fixed_rate = pream | nss | rate;
4468
4469 return true;
4470}
4471
Michal Kazior7aa7a722014-08-25 12:09:38 +02004472static bool ath10k_get_fixed_rate_nss(struct ath10k *ar,
4473 const struct cfg80211_bitrate_mask *mask,
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004474 enum ieee80211_band band,
4475 u8 *fixed_rate,
4476 u8 *fixed_nss)
4477{
4478 /* First check full NSS mask, if we can simply limit NSS */
4479 if (ath10k_bitrate_mask_nss(mask, band, fixed_nss))
4480 return true;
4481
4482 /* Next Check single rate is set */
Michal Kazior7aa7a722014-08-25 12:09:38 +02004483 return ath10k_bitrate_mask_rate(ar, mask, band, fixed_rate, fixed_nss);
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004484}
4485
4486static int ath10k_set_fixed_rate_param(struct ath10k_vif *arvif,
4487 u8 fixed_rate,
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004488 u8 fixed_nss,
4489 u8 force_sgi)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004490{
4491 struct ath10k *ar = arvif->ar;
4492 u32 vdev_param;
4493 int ret = 0;
4494
4495 mutex_lock(&ar->conf_mutex);
4496
4497 if (arvif->fixed_rate == fixed_rate &&
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004498 arvif->fixed_nss == fixed_nss &&
4499 arvif->force_sgi == force_sgi)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004500 goto exit;
4501
4502 if (fixed_rate == WMI_FIXED_RATE_NONE)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004503 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac disable fixed bitrate mask\n");
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004504
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004505 if (force_sgi)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004506 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac force sgi\n");
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004507
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004508 vdev_param = ar->wmi.vdev_param->fixed_rate;
4509 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
4510 vdev_param, fixed_rate);
4511 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004512 ath10k_warn(ar, "failed to set fixed rate param 0x%02x: %d\n",
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004513 fixed_rate, ret);
4514 ret = -EINVAL;
4515 goto exit;
4516 }
4517
4518 arvif->fixed_rate = fixed_rate;
4519
4520 vdev_param = ar->wmi.vdev_param->nss;
4521 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
4522 vdev_param, fixed_nss);
4523
4524 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004525 ath10k_warn(ar, "failed to set fixed nss param %d: %d\n",
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004526 fixed_nss, ret);
4527 ret = -EINVAL;
4528 goto exit;
4529 }
4530
4531 arvif->fixed_nss = fixed_nss;
4532
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004533 vdev_param = ar->wmi.vdev_param->sgi;
4534 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
4535 force_sgi);
4536
4537 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004538 ath10k_warn(ar, "failed to set sgi param %d: %d\n",
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004539 force_sgi, ret);
4540 ret = -EINVAL;
4541 goto exit;
4542 }
4543
4544 arvif->force_sgi = force_sgi;
4545
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004546exit:
4547 mutex_unlock(&ar->conf_mutex);
4548 return ret;
4549}
4550
4551static int ath10k_set_bitrate_mask(struct ieee80211_hw *hw,
4552 struct ieee80211_vif *vif,
4553 const struct cfg80211_bitrate_mask *mask)
4554{
4555 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4556 struct ath10k *ar = arvif->ar;
4557 enum ieee80211_band band = ar->hw->conf.chandef.chan->band;
4558 u8 fixed_rate = WMI_FIXED_RATE_NONE;
4559 u8 fixed_nss = ar->num_rf_chains;
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004560 u8 force_sgi;
4561
Ben Greearb116ea12014-11-24 16:22:10 +02004562 if (ar->cfg_tx_chainmask)
4563 fixed_nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
4564
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004565 force_sgi = mask->control[band].gi;
4566 if (force_sgi == NL80211_TXRATE_FORCE_LGI)
4567 return -EINVAL;
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004568
4569 if (!ath10k_default_bitrate_mask(ar, band, mask)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004570 if (!ath10k_get_fixed_rate_nss(ar, mask, band,
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004571 &fixed_rate,
4572 &fixed_nss))
4573 return -EINVAL;
4574 }
4575
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004576 if (fixed_rate == WMI_FIXED_RATE_NONE && force_sgi) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004577 ath10k_warn(ar, "failed to force SGI usage for default rate settings\n");
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004578 return -EINVAL;
4579 }
4580
4581 return ath10k_set_fixed_rate_param(arvif, fixed_rate,
4582 fixed_nss, force_sgi);
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004583}
4584
Michal Kazior9797feb2014-02-14 14:49:48 +01004585static void ath10k_sta_rc_update(struct ieee80211_hw *hw,
4586 struct ieee80211_vif *vif,
4587 struct ieee80211_sta *sta,
4588 u32 changed)
4589{
4590 struct ath10k *ar = hw->priv;
4591 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
4592 u32 bw, smps;
4593
4594 spin_lock_bh(&ar->data_lock);
4595
Michal Kazior7aa7a722014-08-25 12:09:38 +02004596 ath10k_dbg(ar, ATH10K_DBG_MAC,
Michal Kazior9797feb2014-02-14 14:49:48 +01004597 "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
4598 sta->addr, changed, sta->bandwidth, sta->rx_nss,
4599 sta->smps_mode);
4600
4601 if (changed & IEEE80211_RC_BW_CHANGED) {
4602 bw = WMI_PEER_CHWIDTH_20MHZ;
4603
4604 switch (sta->bandwidth) {
4605 case IEEE80211_STA_RX_BW_20:
4606 bw = WMI_PEER_CHWIDTH_20MHZ;
4607 break;
4608 case IEEE80211_STA_RX_BW_40:
4609 bw = WMI_PEER_CHWIDTH_40MHZ;
4610 break;
4611 case IEEE80211_STA_RX_BW_80:
4612 bw = WMI_PEER_CHWIDTH_80MHZ;
4613 break;
4614 case IEEE80211_STA_RX_BW_160:
Michal Kazior7aa7a722014-08-25 12:09:38 +02004615 ath10k_warn(ar, "Invalid bandwith %d in rc update for %pM\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02004616 sta->bandwidth, sta->addr);
Michal Kazior9797feb2014-02-14 14:49:48 +01004617 bw = WMI_PEER_CHWIDTH_20MHZ;
4618 break;
4619 }
4620
4621 arsta->bw = bw;
4622 }
4623
4624 if (changed & IEEE80211_RC_NSS_CHANGED)
4625 arsta->nss = sta->rx_nss;
4626
4627 if (changed & IEEE80211_RC_SMPS_CHANGED) {
4628 smps = WMI_PEER_SMPS_PS_NONE;
4629
4630 switch (sta->smps_mode) {
4631 case IEEE80211_SMPS_AUTOMATIC:
4632 case IEEE80211_SMPS_OFF:
4633 smps = WMI_PEER_SMPS_PS_NONE;
4634 break;
4635 case IEEE80211_SMPS_STATIC:
4636 smps = WMI_PEER_SMPS_STATIC;
4637 break;
4638 case IEEE80211_SMPS_DYNAMIC:
4639 smps = WMI_PEER_SMPS_DYNAMIC;
4640 break;
4641 case IEEE80211_SMPS_NUM_MODES:
Michal Kazior7aa7a722014-08-25 12:09:38 +02004642 ath10k_warn(ar, "Invalid smps %d in sta rc update for %pM\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02004643 sta->smps_mode, sta->addr);
Michal Kazior9797feb2014-02-14 14:49:48 +01004644 smps = WMI_PEER_SMPS_PS_NONE;
4645 break;
4646 }
4647
4648 arsta->smps = smps;
4649 }
4650
Michal Kazior9797feb2014-02-14 14:49:48 +01004651 arsta->changed |= changed;
4652
4653 spin_unlock_bh(&ar->data_lock);
4654
4655 ieee80211_queue_work(hw, &arsta->update_wk);
4656}
4657
Chun-Yeow Yeoh26ebbcc2014-02-25 09:29:54 +02004658static u64 ath10k_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
4659{
4660 /*
4661 * FIXME: Return 0 for time being. Need to figure out whether FW
4662 * has the API to fetch 64-bit local TSF
4663 */
4664
4665 return 0;
4666}
4667
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02004668static int ath10k_ampdu_action(struct ieee80211_hw *hw,
4669 struct ieee80211_vif *vif,
4670 enum ieee80211_ampdu_mlme_action action,
4671 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
4672 u8 buf_size)
4673{
Michal Kazior7aa7a722014-08-25 12:09:38 +02004674 struct ath10k *ar = hw->priv;
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02004675 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4676
Michal Kazior7aa7a722014-08-25 12:09:38 +02004677 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 +02004678 arvif->vdev_id, sta->addr, tid, action);
4679
4680 switch (action) {
4681 case IEEE80211_AMPDU_RX_START:
4682 case IEEE80211_AMPDU_RX_STOP:
4683 /* HTT AddBa/DelBa events trigger mac80211 Rx BA session
4684 * creation/removal. Do we need to verify this?
4685 */
4686 return 0;
4687 case IEEE80211_AMPDU_TX_START:
4688 case IEEE80211_AMPDU_TX_STOP_CONT:
4689 case IEEE80211_AMPDU_TX_STOP_FLUSH:
4690 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
4691 case IEEE80211_AMPDU_TX_OPERATIONAL:
4692 /* Firmware offloads Tx aggregation entirely so deny mac80211
4693 * Tx aggregation requests.
4694 */
4695 return -EOPNOTSUPP;
4696 }
4697
4698 return -EINVAL;
4699}
4700
Kalle Valo5e3dd152013-06-12 20:52:10 +03004701static const struct ieee80211_ops ath10k_ops = {
4702 .tx = ath10k_tx,
4703 .start = ath10k_start,
4704 .stop = ath10k_stop,
4705 .config = ath10k_config,
4706 .add_interface = ath10k_add_interface,
4707 .remove_interface = ath10k_remove_interface,
4708 .configure_filter = ath10k_configure_filter,
4709 .bss_info_changed = ath10k_bss_info_changed,
4710 .hw_scan = ath10k_hw_scan,
4711 .cancel_hw_scan = ath10k_cancel_hw_scan,
4712 .set_key = ath10k_set_key,
4713 .sta_state = ath10k_sta_state,
4714 .conf_tx = ath10k_conf_tx,
4715 .remain_on_channel = ath10k_remain_on_channel,
4716 .cancel_remain_on_channel = ath10k_cancel_remain_on_channel,
4717 .set_rts_threshold = ath10k_set_rts_threshold,
Kalle Valo5e3dd152013-06-12 20:52:10 +03004718 .flush = ath10k_flush,
4719 .tx_last_beacon = ath10k_tx_last_beacon,
Ben Greear46acf7b2014-05-16 17:15:38 +03004720 .set_antenna = ath10k_set_antenna,
4721 .get_antenna = ath10k_get_antenna,
Eliad Pellercf2c92d2014-11-04 11:43:54 +02004722 .reconfig_complete = ath10k_reconfig_complete,
Michal Kazior2e1dea42013-07-31 10:32:40 +02004723 .get_survey = ath10k_get_survey,
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004724 .set_bitrate_mask = ath10k_set_bitrate_mask,
Michal Kazior9797feb2014-02-14 14:49:48 +01004725 .sta_rc_update = ath10k_sta_rc_update,
Chun-Yeow Yeoh26ebbcc2014-02-25 09:29:54 +02004726 .get_tsf = ath10k_get_tsf,
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02004727 .ampdu_action = ath10k_ampdu_action,
Ben Greear6cddcc72014-09-29 14:41:46 +03004728 .get_et_sset_count = ath10k_debug_get_et_sset_count,
4729 .get_et_stats = ath10k_debug_get_et_stats,
4730 .get_et_strings = ath10k_debug_get_et_strings,
Kalle Valo43d2a302014-09-10 18:23:30 +03004731
4732 CFG80211_TESTMODE_CMD(ath10k_tm_cmd)
4733
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004734#ifdef CONFIG_PM
4735 .suspend = ath10k_suspend,
4736 .resume = ath10k_resume,
4737#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03004738};
4739
4740#define RATETAB_ENT(_rate, _rateid, _flags) { \
4741 .bitrate = (_rate), \
4742 .flags = (_flags), \
4743 .hw_value = (_rateid), \
4744}
4745
4746#define CHAN2G(_channel, _freq, _flags) { \
4747 .band = IEEE80211_BAND_2GHZ, \
4748 .hw_value = (_channel), \
4749 .center_freq = (_freq), \
4750 .flags = (_flags), \
4751 .max_antenna_gain = 0, \
4752 .max_power = 30, \
4753}
4754
4755#define CHAN5G(_channel, _freq, _flags) { \
4756 .band = IEEE80211_BAND_5GHZ, \
4757 .hw_value = (_channel), \
4758 .center_freq = (_freq), \
4759 .flags = (_flags), \
4760 .max_antenna_gain = 0, \
4761 .max_power = 30, \
4762}
4763
4764static const struct ieee80211_channel ath10k_2ghz_channels[] = {
4765 CHAN2G(1, 2412, 0),
4766 CHAN2G(2, 2417, 0),
4767 CHAN2G(3, 2422, 0),
4768 CHAN2G(4, 2427, 0),
4769 CHAN2G(5, 2432, 0),
4770 CHAN2G(6, 2437, 0),
4771 CHAN2G(7, 2442, 0),
4772 CHAN2G(8, 2447, 0),
4773 CHAN2G(9, 2452, 0),
4774 CHAN2G(10, 2457, 0),
4775 CHAN2G(11, 2462, 0),
4776 CHAN2G(12, 2467, 0),
4777 CHAN2G(13, 2472, 0),
4778 CHAN2G(14, 2484, 0),
4779};
4780
4781static const struct ieee80211_channel ath10k_5ghz_channels[] = {
Michal Kazior429ff562013-06-26 08:54:54 +02004782 CHAN5G(36, 5180, 0),
4783 CHAN5G(40, 5200, 0),
4784 CHAN5G(44, 5220, 0),
4785 CHAN5G(48, 5240, 0),
4786 CHAN5G(52, 5260, 0),
4787 CHAN5G(56, 5280, 0),
4788 CHAN5G(60, 5300, 0),
4789 CHAN5G(64, 5320, 0),
4790 CHAN5G(100, 5500, 0),
4791 CHAN5G(104, 5520, 0),
4792 CHAN5G(108, 5540, 0),
4793 CHAN5G(112, 5560, 0),
4794 CHAN5G(116, 5580, 0),
4795 CHAN5G(120, 5600, 0),
4796 CHAN5G(124, 5620, 0),
4797 CHAN5G(128, 5640, 0),
4798 CHAN5G(132, 5660, 0),
4799 CHAN5G(136, 5680, 0),
4800 CHAN5G(140, 5700, 0),
4801 CHAN5G(149, 5745, 0),
4802 CHAN5G(153, 5765, 0),
4803 CHAN5G(157, 5785, 0),
4804 CHAN5G(161, 5805, 0),
4805 CHAN5G(165, 5825, 0),
Kalle Valo5e3dd152013-06-12 20:52:10 +03004806};
4807
Michal Kazior91b12082014-12-12 12:41:35 +01004808/* Note: Be careful if you re-order these. There is code which depends on this
4809 * ordering.
4810 */
Kalle Valo5e3dd152013-06-12 20:52:10 +03004811static struct ieee80211_rate ath10k_rates[] = {
4812 /* CCK */
4813 RATETAB_ENT(10, 0x82, 0),
4814 RATETAB_ENT(20, 0x84, 0),
4815 RATETAB_ENT(55, 0x8b, 0),
4816 RATETAB_ENT(110, 0x96, 0),
4817 /* OFDM */
4818 RATETAB_ENT(60, 0x0c, 0),
4819 RATETAB_ENT(90, 0x12, 0),
4820 RATETAB_ENT(120, 0x18, 0),
4821 RATETAB_ENT(180, 0x24, 0),
4822 RATETAB_ENT(240, 0x30, 0),
4823 RATETAB_ENT(360, 0x48, 0),
4824 RATETAB_ENT(480, 0x60, 0),
4825 RATETAB_ENT(540, 0x6c, 0),
4826};
4827
4828#define ath10k_a_rates (ath10k_rates + 4)
4829#define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - 4)
4830#define ath10k_g_rates (ath10k_rates + 0)
4831#define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
4832
Michal Kaziore7b54192014-08-07 11:03:27 +02004833struct ath10k *ath10k_mac_create(size_t priv_size)
Kalle Valo5e3dd152013-06-12 20:52:10 +03004834{
4835 struct ieee80211_hw *hw;
4836 struct ath10k *ar;
4837
Michal Kaziore7b54192014-08-07 11:03:27 +02004838 hw = ieee80211_alloc_hw(sizeof(struct ath10k) + priv_size, &ath10k_ops);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004839 if (!hw)
4840 return NULL;
4841
4842 ar = hw->priv;
4843 ar->hw = hw;
4844
4845 return ar;
4846}
4847
4848void ath10k_mac_destroy(struct ath10k *ar)
4849{
4850 ieee80211_free_hw(ar->hw);
4851}
4852
4853static const struct ieee80211_iface_limit ath10k_if_limits[] = {
4854 {
4855 .max = 8,
4856 .types = BIT(NL80211_IFTYPE_STATION)
4857 | BIT(NL80211_IFTYPE_P2P_CLIENT)
Michal Kaziord531cb82013-07-31 10:55:13 +02004858 },
4859 {
4860 .max = 3,
4861 .types = BIT(NL80211_IFTYPE_P2P_GO)
4862 },
4863 {
Michal Kazior75d2bd42014-12-12 12:41:39 +01004864 .max = 1,
4865 .types = BIT(NL80211_IFTYPE_P2P_DEVICE)
4866 },
4867 {
Michal Kaziord531cb82013-07-31 10:55:13 +02004868 .max = 7,
4869 .types = BIT(NL80211_IFTYPE_AP)
4870 },
Kalle Valo5e3dd152013-06-12 20:52:10 +03004871};
4872
Bartosz Markowskif2595092013-12-10 16:20:39 +01004873static const struct ieee80211_iface_limit ath10k_10x_if_limits[] = {
Marek Puzyniake8a50f82013-11-20 09:59:47 +02004874 {
4875 .max = 8,
4876 .types = BIT(NL80211_IFTYPE_AP)
4877 },
4878};
Marek Puzyniake8a50f82013-11-20 09:59:47 +02004879
4880static const struct ieee80211_iface_combination ath10k_if_comb[] = {
4881 {
4882 .limits = ath10k_if_limits,
4883 .n_limits = ARRAY_SIZE(ath10k_if_limits),
4884 .max_interfaces = 8,
4885 .num_different_channels = 1,
4886 .beacon_int_infra_match = true,
4887 },
Bartosz Markowskif2595092013-12-10 16:20:39 +01004888};
4889
4890static const struct ieee80211_iface_combination ath10k_10x_if_comb[] = {
Marek Puzyniake8a50f82013-11-20 09:59:47 +02004891 {
Bartosz Markowskif2595092013-12-10 16:20:39 +01004892 .limits = ath10k_10x_if_limits,
4893 .n_limits = ARRAY_SIZE(ath10k_10x_if_limits),
Marek Puzyniake8a50f82013-11-20 09:59:47 +02004894 .max_interfaces = 8,
4895 .num_different_channels = 1,
4896 .beacon_int_infra_match = true,
Bartosz Markowskif2595092013-12-10 16:20:39 +01004897#ifdef CONFIG_ATH10K_DFS_CERTIFIED
Marek Puzyniake8a50f82013-11-20 09:59:47 +02004898 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
4899 BIT(NL80211_CHAN_WIDTH_20) |
4900 BIT(NL80211_CHAN_WIDTH_40) |
4901 BIT(NL80211_CHAN_WIDTH_80),
Marek Puzyniake8a50f82013-11-20 09:59:47 +02004902#endif
Bartosz Markowskif2595092013-12-10 16:20:39 +01004903 },
Kalle Valo5e3dd152013-06-12 20:52:10 +03004904};
4905
4906static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
4907{
4908 struct ieee80211_sta_vht_cap vht_cap = {0};
4909 u16 mcs_map;
Michal Kazior8865bee42013-07-24 12:36:46 +02004910 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004911
4912 vht_cap.vht_supported = 1;
4913 vht_cap.cap = ar->vht_cap_info;
4914
Michal Kazior8865bee42013-07-24 12:36:46 +02004915 mcs_map = 0;
4916 for (i = 0; i < 8; i++) {
4917 if (i < ar->num_rf_chains)
4918 mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i*2);
4919 else
4920 mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i*2);
4921 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03004922
4923 vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
4924 vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
4925
4926 return vht_cap;
4927}
4928
4929static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
4930{
4931 int i;
4932 struct ieee80211_sta_ht_cap ht_cap = {0};
4933
4934 if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
4935 return ht_cap;
4936
4937 ht_cap.ht_supported = 1;
4938 ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
4939 ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
4940 ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
4941 ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
4942 ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
4943
4944 if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
4945 ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
4946
4947 if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
4948 ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
4949
4950 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
4951 u32 smps;
4952
4953 smps = WLAN_HT_CAP_SM_PS_DYNAMIC;
4954 smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
4955
4956 ht_cap.cap |= smps;
4957 }
4958
4959 if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC)
4960 ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
4961
4962 if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
4963 u32 stbc;
4964
4965 stbc = ar->ht_cap_info;
4966 stbc &= WMI_HT_CAP_RX_STBC;
4967 stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
4968 stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
4969 stbc &= IEEE80211_HT_CAP_RX_STBC;
4970
4971 ht_cap.cap |= stbc;
4972 }
4973
4974 if (ar->ht_cap_info & WMI_HT_CAP_LDPC)
4975 ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
4976
4977 if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
4978 ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
4979
4980 /* max AMSDU is implicitly taken from vht_cap_info */
4981 if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
4982 ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
4983
Michal Kazior8865bee42013-07-24 12:36:46 +02004984 for (i = 0; i < ar->num_rf_chains; i++)
Kalle Valo5e3dd152013-06-12 20:52:10 +03004985 ht_cap.mcs.rx_mask[i] = 0xFF;
4986
4987 ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
4988
4989 return ht_cap;
4990}
4991
Kalle Valo5e3dd152013-06-12 20:52:10 +03004992static void ath10k_get_arvif_iter(void *data, u8 *mac,
4993 struct ieee80211_vif *vif)
4994{
4995 struct ath10k_vif_iter *arvif_iter = data;
4996 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4997
4998 if (arvif->vdev_id == arvif_iter->vdev_id)
4999 arvif_iter->arvif = arvif;
5000}
5001
5002struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
5003{
5004 struct ath10k_vif_iter arvif_iter;
5005 u32 flags;
5006
5007 memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
5008 arvif_iter.vdev_id = vdev_id;
5009
5010 flags = IEEE80211_IFACE_ITER_RESUME_ALL;
5011 ieee80211_iterate_active_interfaces_atomic(ar->hw,
5012 flags,
5013 ath10k_get_arvif_iter,
5014 &arvif_iter);
5015 if (!arvif_iter.arvif) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02005016 ath10k_warn(ar, "No VIF found for vdev %d\n", vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03005017 return NULL;
5018 }
5019
5020 return arvif_iter.arvif;
5021}
5022
5023int ath10k_mac_register(struct ath10k *ar)
5024{
5025 struct ieee80211_supported_band *band;
5026 struct ieee80211_sta_vht_cap vht_cap;
5027 struct ieee80211_sta_ht_cap ht_cap;
5028 void *channels;
5029 int ret;
5030
5031 SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
5032
5033 SET_IEEE80211_DEV(ar->hw, ar->dev);
5034
5035 ht_cap = ath10k_get_ht_cap(ar);
5036 vht_cap = ath10k_create_vht_cap(ar);
5037
5038 if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
5039 channels = kmemdup(ath10k_2ghz_channels,
5040 sizeof(ath10k_2ghz_channels),
5041 GFP_KERNEL);
Michal Kaziord6015b22013-07-22 14:13:30 +02005042 if (!channels) {
5043 ret = -ENOMEM;
5044 goto err_free;
5045 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03005046
5047 band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
5048 band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
5049 band->channels = channels;
5050 band->n_bitrates = ath10k_g_rates_size;
5051 band->bitrates = ath10k_g_rates;
5052 band->ht_cap = ht_cap;
5053
5054 /* vht is not supported in 2.4 GHz */
5055
5056 ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
5057 }
5058
5059 if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
5060 channels = kmemdup(ath10k_5ghz_channels,
5061 sizeof(ath10k_5ghz_channels),
5062 GFP_KERNEL);
5063 if (!channels) {
Michal Kaziord6015b22013-07-22 14:13:30 +02005064 ret = -ENOMEM;
5065 goto err_free;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005066 }
5067
5068 band = &ar->mac.sbands[IEEE80211_BAND_5GHZ];
5069 band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
5070 band->channels = channels;
5071 band->n_bitrates = ath10k_a_rates_size;
5072 band->bitrates = ath10k_a_rates;
5073 band->ht_cap = ht_cap;
5074 band->vht_cap = vht_cap;
5075 ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
5076 }
5077
5078 ar->hw->wiphy->interface_modes =
5079 BIT(NL80211_IFTYPE_STATION) |
Bartosz Markowskid3541812013-12-10 16:20:40 +01005080 BIT(NL80211_IFTYPE_AP);
5081
Ben Greear46acf7b2014-05-16 17:15:38 +03005082 ar->hw->wiphy->available_antennas_rx = ar->supp_rx_chainmask;
5083 ar->hw->wiphy->available_antennas_tx = ar->supp_tx_chainmask;
5084
Bartosz Markowskid3541812013-12-10 16:20:40 +01005085 if (!test_bit(ATH10K_FW_FEATURE_NO_P2P, ar->fw_features))
5086 ar->hw->wiphy->interface_modes |=
Michal Kazior75d2bd42014-12-12 12:41:39 +01005087 BIT(NL80211_IFTYPE_P2P_DEVICE) |
Bartosz Markowskid3541812013-12-10 16:20:40 +01005088 BIT(NL80211_IFTYPE_P2P_CLIENT) |
5089 BIT(NL80211_IFTYPE_P2P_GO);
Kalle Valo5e3dd152013-06-12 20:52:10 +03005090
5091 ar->hw->flags = IEEE80211_HW_SIGNAL_DBM |
5092 IEEE80211_HW_SUPPORTS_PS |
5093 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
5094 IEEE80211_HW_SUPPORTS_UAPSD |
5095 IEEE80211_HW_MFP_CAPABLE |
5096 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
5097 IEEE80211_HW_HAS_RATE_CONTROL |
Janusz Dziedzic2f0f1122014-02-26 18:42:09 +02005098 IEEE80211_HW_AP_LINK_PS |
5099 IEEE80211_HW_SPECTRUM_MGMT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005100
Eliad Peller0d8614b2014-09-10 14:07:36 +03005101 ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
5102
Kalle Valo5e3dd152013-06-12 20:52:10 +03005103 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
Eliad Peller0d8614b2014-09-10 14:07:36 +03005104 ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005105
5106 if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
5107 ar->hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
5108 ar->hw->flags |= IEEE80211_HW_TX_AMPDU_SETUP_IN_HW;
5109 }
5110
5111 ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
5112 ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
5113
5114 ar->hw->vif_data_size = sizeof(struct ath10k_vif);
Michal Kazior9797feb2014-02-14 14:49:48 +01005115 ar->hw->sta_data_size = sizeof(struct ath10k_sta);
Kalle Valo5e3dd152013-06-12 20:52:10 +03005116
Kalle Valo5e3dd152013-06-12 20:52:10 +03005117 ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
5118
5119 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
Michal Kaziorc2df44b2014-01-23 11:38:26 +01005120 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005121 ar->hw->wiphy->max_remain_on_channel_duration = 5000;
5122
5123 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
Rajkumar Manoharan78157a12014-11-17 16:44:15 +02005124 ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE;
5125
Kalle Valo5e3dd152013-06-12 20:52:10 +03005126 /*
5127 * on LL hardware queues are managed entirely by the FW
5128 * so we only advertise to mac we can do the queues thing
5129 */
5130 ar->hw->queues = 4;
5131
Bartosz Markowskif2595092013-12-10 16:20:39 +01005132 if (test_bit(ATH10K_FW_FEATURE_WMI_10X, ar->fw_features)) {
5133 ar->hw->wiphy->iface_combinations = ath10k_10x_if_comb;
5134 ar->hw->wiphy->n_iface_combinations =
5135 ARRAY_SIZE(ath10k_10x_if_comb);
5136 } else {
5137 ar->hw->wiphy->iface_combinations = ath10k_if_comb;
5138 ar->hw->wiphy->n_iface_combinations =
5139 ARRAY_SIZE(ath10k_if_comb);
Michal Kaziorcf850d12014-07-24 20:07:00 +03005140
5141 ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
Bartosz Markowskif2595092013-12-10 16:20:39 +01005142 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03005143
Michal Kazior7c199992013-07-31 10:47:57 +02005144 ar->hw->netdev_features = NETIF_F_HW_CSUM;
5145
Janusz Dziedzic9702c682013-11-20 09:59:41 +02005146 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED)) {
5147 /* Init ath dfs pattern detector */
5148 ar->ath_common.debug_mask = ATH_DBG_DFS;
5149 ar->dfs_detector = dfs_pattern_detector_init(&ar->ath_common,
5150 NL80211_DFS_UNSET);
5151
5152 if (!ar->dfs_detector)
Michal Kazior7aa7a722014-08-25 12:09:38 +02005153 ath10k_warn(ar, "failed to initialise DFS pattern detector\n");
Janusz Dziedzic9702c682013-11-20 09:59:41 +02005154 }
5155
Kalle Valo5e3dd152013-06-12 20:52:10 +03005156 ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
5157 ath10k_reg_notifier);
5158 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02005159 ath10k_err(ar, "failed to initialise regulatory: %i\n", ret);
Michal Kaziord6015b22013-07-22 14:13:30 +02005160 goto err_free;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005161 }
5162
5163 ret = ieee80211_register_hw(ar->hw);
5164 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02005165 ath10k_err(ar, "failed to register ieee80211: %d\n", ret);
Michal Kaziord6015b22013-07-22 14:13:30 +02005166 goto err_free;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005167 }
5168
5169 if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
5170 ret = regulatory_hint(ar->hw->wiphy,
5171 ar->ath_common.regulatory.alpha2);
5172 if (ret)
Michal Kaziord6015b22013-07-22 14:13:30 +02005173 goto err_unregister;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005174 }
5175
5176 return 0;
Michal Kaziord6015b22013-07-22 14:13:30 +02005177
5178err_unregister:
Kalle Valo5e3dd152013-06-12 20:52:10 +03005179 ieee80211_unregister_hw(ar->hw);
Michal Kaziord6015b22013-07-22 14:13:30 +02005180err_free:
5181 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
5182 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
5183
Kalle Valo5e3dd152013-06-12 20:52:10 +03005184 return ret;
5185}
5186
5187void ath10k_mac_unregister(struct ath10k *ar)
5188{
5189 ieee80211_unregister_hw(ar->hw);
5190
Janusz Dziedzic9702c682013-11-20 09:59:41 +02005191 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
5192 ar->dfs_detector->exit(ar->dfs_detector);
5193
Kalle Valo5e3dd152013-06-12 20:52:10 +03005194 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
5195 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
5196
5197 SET_IEEE80211_DEV(ar->hw, NULL);
5198}