blob: 2804952448c534ee87975e05a2bbfebe287c3284 [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
Kalle Valo5e3dd152013-06-12 20:52:10 +03001102/*
1103 * Review this when mac80211 gains per-interface powersave support.
1104 */
Michal Kaziorad088bf2013-10-16 15:44:46 +03001105static int ath10k_mac_vif_setup_ps(struct ath10k_vif *arvif)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001106{
Michal Kaziorad088bf2013-10-16 15:44:46 +03001107 struct ath10k *ar = arvif->ar;
Michal Kazior526549a2014-12-12 12:41:37 +01001108 struct ieee80211_vif *vif = arvif->vif;
Michal Kaziorad088bf2013-10-16 15:44:46 +03001109 struct ieee80211_conf *conf = &ar->hw->conf;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001110 enum wmi_sta_powersave_param param;
1111 enum wmi_sta_ps_mode psmode;
1112 int ret;
Michal Kazior526549a2014-12-12 12:41:37 +01001113 int ps_timeout;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001114
Michal Kazior548db542013-07-05 16:15:15 +03001115 lockdep_assert_held(&arvif->ar->conf_mutex);
1116
Michal Kaziorad088bf2013-10-16 15:44:46 +03001117 if (arvif->vif->type != NL80211_IFTYPE_STATION)
1118 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001119
1120 if (conf->flags & IEEE80211_CONF_PS) {
1121 psmode = WMI_STA_PS_MODE_ENABLED;
1122 param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
1123
Michal Kazior526549a2014-12-12 12:41:37 +01001124 ps_timeout = conf->dynamic_ps_timeout;
1125 if (ps_timeout == 0) {
1126 /* Firmware doesn't like 0 */
1127 ps_timeout = ieee80211_tu_to_usec(
1128 vif->bss_conf.beacon_int) / 1000;
1129 }
1130
Michal Kaziorad088bf2013-10-16 15:44:46 +03001131 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
Michal Kazior526549a2014-12-12 12:41:37 +01001132 ps_timeout);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001133 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001134 ath10k_warn(ar, "failed to set inactivity time for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02001135 arvif->vdev_id, ret);
Michal Kaziorad088bf2013-10-16 15:44:46 +03001136 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001137 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001138 } else {
1139 psmode = WMI_STA_PS_MODE_DISABLED;
1140 }
1141
Michal Kazior7aa7a722014-08-25 12:09:38 +02001142 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d psmode %s\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03001143 arvif->vdev_id, psmode ? "enable" : "disable");
1144
Michal Kaziorad088bf2013-10-16 15:44:46 +03001145 ret = ath10k_wmi_set_psmode(ar, arvif->vdev_id, psmode);
1146 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001147 ath10k_warn(ar, "failed to set PS Mode %d for vdev %d: %d\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02001148 psmode, arvif->vdev_id, ret);
Michal Kaziorad088bf2013-10-16 15:44:46 +03001149 return ret;
1150 }
1151
1152 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001153}
1154
1155/**********************/
1156/* Station management */
1157/**********************/
1158
Michal Kazior590922a2014-10-21 10:10:29 +03001159static u32 ath10k_peer_assoc_h_listen_intval(struct ath10k *ar,
1160 struct ieee80211_vif *vif)
1161{
1162 /* Some firmware revisions have unstable STA powersave when listen
1163 * interval is set too high (e.g. 5). The symptoms are firmware doesn't
1164 * generate NullFunc frames properly even if buffered frames have been
1165 * indicated in Beacon TIM. Firmware would seldom wake up to pull
1166 * buffered frames. Often pinging the device from AP would simply fail.
1167 *
1168 * As a workaround set it to 1.
1169 */
1170 if (vif->type == NL80211_IFTYPE_STATION)
1171 return 1;
1172
1173 return ar->hw->conf.listen_interval;
1174}
1175
Kalle Valo5e3dd152013-06-12 20:52:10 +03001176static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001177 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001178 struct ieee80211_sta *sta,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001179 struct wmi_peer_assoc_complete_arg *arg)
1180{
Michal Kazior590922a2014-10-21 10:10:29 +03001181 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1182
Michal Kazior548db542013-07-05 16:15:15 +03001183 lockdep_assert_held(&ar->conf_mutex);
1184
Kalle Valob25f32c2014-09-14 12:50:49 +03001185 ether_addr_copy(arg->addr, sta->addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001186 arg->vdev_id = arvif->vdev_id;
1187 arg->peer_aid = sta->aid;
1188 arg->peer_flags |= WMI_PEER_AUTH;
Michal Kazior590922a2014-10-21 10:10:29 +03001189 arg->peer_listen_intval = ath10k_peer_assoc_h_listen_intval(ar, vif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001190 arg->peer_num_spatial_streams = 1;
Michal Kazior590922a2014-10-21 10:10:29 +03001191 arg->peer_caps = vif->bss_conf.assoc_capability;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001192}
1193
1194static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001195 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001196 struct wmi_peer_assoc_complete_arg *arg)
1197{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001198 struct ieee80211_bss_conf *info = &vif->bss_conf;
1199 struct cfg80211_bss *bss;
1200 const u8 *rsnie = NULL;
1201 const u8 *wpaie = NULL;
1202
Michal Kazior548db542013-07-05 16:15:15 +03001203 lockdep_assert_held(&ar->conf_mutex);
1204
Kalle Valo5e3dd152013-06-12 20:52:10 +03001205 bss = cfg80211_get_bss(ar->hw->wiphy, ar->hw->conf.chandef.chan,
1206 info->bssid, NULL, 0, 0, 0);
1207 if (bss) {
1208 const struct cfg80211_bss_ies *ies;
1209
1210 rcu_read_lock();
1211 rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
1212
1213 ies = rcu_dereference(bss->ies);
1214
1215 wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
Kalle Valo5b07e072014-09-14 12:50:06 +03001216 WLAN_OUI_TYPE_MICROSOFT_WPA,
1217 ies->data,
1218 ies->len);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001219 rcu_read_unlock();
1220 cfg80211_put_bss(ar->hw->wiphy, bss);
1221 }
1222
1223 /* FIXME: base on RSN IE/WPA IE is a correct idea? */
1224 if (rsnie || wpaie) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001225 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001226 arg->peer_flags |= WMI_PEER_NEED_PTK_4_WAY;
1227 }
1228
1229 if (wpaie) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001230 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: wpa ie found\n", __func__);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001231 arg->peer_flags |= WMI_PEER_NEED_GTK_2_WAY;
1232 }
1233}
1234
1235static void ath10k_peer_assoc_h_rates(struct ath10k *ar,
1236 struct ieee80211_sta *sta,
1237 struct wmi_peer_assoc_complete_arg *arg)
1238{
1239 struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
1240 const struct ieee80211_supported_band *sband;
1241 const struct ieee80211_rate *rates;
1242 u32 ratemask;
1243 int i;
1244
Michal Kazior548db542013-07-05 16:15:15 +03001245 lockdep_assert_held(&ar->conf_mutex);
1246
Kalle Valo5e3dd152013-06-12 20:52:10 +03001247 sband = ar->hw->wiphy->bands[ar->hw->conf.chandef.chan->band];
1248 ratemask = sta->supp_rates[ar->hw->conf.chandef.chan->band];
1249 rates = sband->bitrates;
1250
1251 rateset->num_rates = 0;
1252
1253 for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
1254 if (!(ratemask & 1))
1255 continue;
1256
1257 rateset->rates[rateset->num_rates] = rates->hw_value;
1258 rateset->num_rates++;
1259 }
1260}
1261
1262static void ath10k_peer_assoc_h_ht(struct ath10k *ar,
1263 struct ieee80211_sta *sta,
1264 struct wmi_peer_assoc_complete_arg *arg)
1265{
1266 const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001267 int i, n;
Kalle Valoaf762c02014-09-14 12:50:17 +03001268 u32 stbc;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001269
Michal Kazior548db542013-07-05 16:15:15 +03001270 lockdep_assert_held(&ar->conf_mutex);
1271
Kalle Valo5e3dd152013-06-12 20:52:10 +03001272 if (!ht_cap->ht_supported)
1273 return;
1274
1275 arg->peer_flags |= WMI_PEER_HT;
1276 arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1277 ht_cap->ampdu_factor)) - 1;
1278
1279 arg->peer_mpdu_density =
1280 ath10k_parse_mpdudensity(ht_cap->ampdu_density);
1281
1282 arg->peer_ht_caps = ht_cap->cap;
1283 arg->peer_rate_caps |= WMI_RC_HT_FLAG;
1284
1285 if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
1286 arg->peer_flags |= WMI_PEER_LDPC;
1287
1288 if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
1289 arg->peer_flags |= WMI_PEER_40MHZ;
1290 arg->peer_rate_caps |= WMI_RC_CW40_FLAG;
1291 }
1292
1293 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
1294 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
1295
1296 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
1297 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
1298
1299 if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
1300 arg->peer_rate_caps |= WMI_RC_TX_STBC_FLAG;
1301 arg->peer_flags |= WMI_PEER_STBC;
1302 }
1303
1304 if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001305 stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
1306 stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
1307 stbc = stbc << WMI_RC_RX_STBC_FLAG_S;
1308 arg->peer_rate_caps |= stbc;
1309 arg->peer_flags |= WMI_PEER_STBC;
1310 }
1311
Kalle Valo5e3dd152013-06-12 20:52:10 +03001312 if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
1313 arg->peer_rate_caps |= WMI_RC_TS_FLAG;
1314 else if (ht_cap->mcs.rx_mask[1])
1315 arg->peer_rate_caps |= WMI_RC_DS_FLAG;
1316
1317 for (i = 0, n = 0; i < IEEE80211_HT_MCS_MASK_LEN*8; i++)
1318 if (ht_cap->mcs.rx_mask[i/8] & (1 << i%8))
1319 arg->peer_ht_rates.rates[n++] = i;
1320
Bartosz Markowskifd71f802014-02-10 13:12:55 +01001321 /*
1322 * This is a workaround for HT-enabled STAs which break the spec
1323 * and have no HT capabilities RX mask (no HT RX MCS map).
1324 *
1325 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
1326 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
1327 *
1328 * Firmware asserts if such situation occurs.
1329 */
1330 if (n == 0) {
1331 arg->peer_ht_rates.num_rates = 8;
1332 for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
1333 arg->peer_ht_rates.rates[i] = i;
1334 } else {
1335 arg->peer_ht_rates.num_rates = n;
1336 arg->peer_num_spatial_streams = sta->rx_nss;
1337 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001338
Michal Kazior7aa7a722014-08-25 12:09:38 +02001339 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03001340 arg->addr,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001341 arg->peer_ht_rates.num_rates,
1342 arg->peer_num_spatial_streams);
1343}
1344
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001345static int ath10k_peer_assoc_qos_ap(struct ath10k *ar,
1346 struct ath10k_vif *arvif,
1347 struct ieee80211_sta *sta)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001348{
1349 u32 uapsd = 0;
1350 u32 max_sp = 0;
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001351 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001352
Michal Kazior548db542013-07-05 16:15:15 +03001353 lockdep_assert_held(&ar->conf_mutex);
1354
Kalle Valo5e3dd152013-06-12 20:52:10 +03001355 if (sta->wme && sta->uapsd_queues) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001356 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001357 sta->uapsd_queues, sta->max_sp);
1358
Kalle Valo5e3dd152013-06-12 20:52:10 +03001359 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1360 uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
1361 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
1362 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1363 uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
1364 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
1365 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1366 uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
1367 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
1368 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1369 uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
1370 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
1371
Kalle Valo5e3dd152013-06-12 20:52:10 +03001372 if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
1373 max_sp = sta->max_sp;
1374
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001375 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1376 sta->addr,
1377 WMI_AP_PS_PEER_PARAM_UAPSD,
1378 uapsd);
1379 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001380 ath10k_warn(ar, "failed to set ap ps peer param uapsd for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001381 arvif->vdev_id, ret);
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001382 return ret;
1383 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001384
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001385 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1386 sta->addr,
1387 WMI_AP_PS_PEER_PARAM_MAX_SP,
1388 max_sp);
1389 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001390 ath10k_warn(ar, "failed to set ap ps peer param max sp for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001391 arvif->vdev_id, ret);
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001392 return ret;
1393 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001394
1395 /* TODO setup this based on STA listen interval and
1396 beacon interval. Currently we don't know
1397 sta->listen_interval - mac80211 patch required.
1398 Currently use 10 seconds */
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001399 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id, sta->addr,
Kalle Valo5b07e072014-09-14 12:50:06 +03001400 WMI_AP_PS_PEER_PARAM_AGEOUT_TIME,
1401 10);
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001402 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001403 ath10k_warn(ar, "failed to set ap ps peer param ageout time for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001404 arvif->vdev_id, ret);
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001405 return ret;
1406 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001407 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001408
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001409 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001410}
1411
1412static void ath10k_peer_assoc_h_vht(struct ath10k *ar,
1413 struct ieee80211_sta *sta,
1414 struct wmi_peer_assoc_complete_arg *arg)
1415{
1416 const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
Sujith Manoharana24b88b2013-10-07 19:51:57 -07001417 u8 ampdu_factor;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001418
1419 if (!vht_cap->vht_supported)
1420 return;
1421
1422 arg->peer_flags |= WMI_PEER_VHT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001423 arg->peer_vht_caps = vht_cap->cap;
1424
Sujith Manoharana24b88b2013-10-07 19:51:57 -07001425 ampdu_factor = (vht_cap->cap &
1426 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
1427 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
1428
1429 /* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
1430 * zero in VHT IE. Using it would result in degraded throughput.
1431 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
1432 * it if VHT max_mpdu is smaller. */
1433 arg->peer_max_mpdu = max(arg->peer_max_mpdu,
1434 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1435 ampdu_factor)) - 1);
1436
Kalle Valo5e3dd152013-06-12 20:52:10 +03001437 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1438 arg->peer_flags |= WMI_PEER_80MHZ;
1439
1440 arg->peer_vht_rates.rx_max_rate =
1441 __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
1442 arg->peer_vht_rates.rx_mcs_set =
1443 __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
1444 arg->peer_vht_rates.tx_max_rate =
1445 __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
1446 arg->peer_vht_rates.tx_mcs_set =
1447 __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
1448
Michal Kazior7aa7a722014-08-25 12:09:38 +02001449 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03001450 sta->addr, arg->peer_max_mpdu, arg->peer_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001451}
1452
1453static void ath10k_peer_assoc_h_qos(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001454 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001455 struct ieee80211_sta *sta,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001456 struct wmi_peer_assoc_complete_arg *arg)
1457{
Michal Kazior590922a2014-10-21 10:10:29 +03001458 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1459
Kalle Valo5e3dd152013-06-12 20:52:10 +03001460 switch (arvif->vdev_type) {
1461 case WMI_VDEV_TYPE_AP:
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001462 if (sta->wme)
1463 arg->peer_flags |= WMI_PEER_QOS;
1464
1465 if (sta->wme && sta->uapsd_queues) {
1466 arg->peer_flags |= WMI_PEER_APSD;
1467 arg->peer_rate_caps |= WMI_RC_UAPSD_FLAG;
1468 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001469 break;
1470 case WMI_VDEV_TYPE_STA:
Michal Kazior590922a2014-10-21 10:10:29 +03001471 if (vif->bss_conf.qos)
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001472 arg->peer_flags |= WMI_PEER_QOS;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001473 break;
1474 default:
1475 break;
1476 }
1477}
1478
Michal Kazior91b12082014-12-12 12:41:35 +01001479static bool ath10k_mac_sta_has_11g_rates(struct ieee80211_sta *sta)
1480{
1481 /* First 4 rates in ath10k_rates are CCK (11b) rates. */
1482 return sta->supp_rates[IEEE80211_BAND_2GHZ] >> 4;
1483}
1484
Kalle Valo5e3dd152013-06-12 20:52:10 +03001485static void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001486 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001487 struct ieee80211_sta *sta,
1488 struct wmi_peer_assoc_complete_arg *arg)
1489{
1490 enum wmi_phy_mode phymode = MODE_UNKNOWN;
1491
Kalle Valo5e3dd152013-06-12 20:52:10 +03001492 switch (ar->hw->conf.chandef.chan->band) {
1493 case IEEE80211_BAND_2GHZ:
1494 if (sta->ht_cap.ht_supported) {
1495 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1496 phymode = MODE_11NG_HT40;
1497 else
1498 phymode = MODE_11NG_HT20;
Michal Kazior91b12082014-12-12 12:41:35 +01001499 } else if (ath10k_mac_sta_has_11g_rates(sta)) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001500 phymode = MODE_11G;
Michal Kazior91b12082014-12-12 12:41:35 +01001501 } else {
1502 phymode = MODE_11B;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001503 }
1504
1505 break;
1506 case IEEE80211_BAND_5GHZ:
Sujith Manoharan7cc45e92013-09-08 18:19:55 +03001507 /*
1508 * Check VHT first.
1509 */
1510 if (sta->vht_cap.vht_supported) {
1511 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1512 phymode = MODE_11AC_VHT80;
1513 else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1514 phymode = MODE_11AC_VHT40;
1515 else if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
1516 phymode = MODE_11AC_VHT20;
1517 } else if (sta->ht_cap.ht_supported) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001518 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1519 phymode = MODE_11NA_HT40;
1520 else
1521 phymode = MODE_11NA_HT20;
1522 } else {
1523 phymode = MODE_11A;
1524 }
1525
1526 break;
1527 default:
1528 break;
1529 }
1530
Michal Kazior7aa7a722014-08-25 12:09:38 +02001531 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM phymode %s\n",
Kalle Valo38a1d472013-09-08 17:56:14 +03001532 sta->addr, ath10k_wmi_phymode_str(phymode));
Kalle Valo60c3daa2013-09-08 17:56:07 +03001533
Kalle Valo5e3dd152013-06-12 20:52:10 +03001534 arg->peer_phymode = phymode;
1535 WARN_ON(phymode == MODE_UNKNOWN);
1536}
1537
Kalle Valob9ada652013-10-16 15:44:46 +03001538static int ath10k_peer_assoc_prepare(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001539 struct ieee80211_vif *vif,
Kalle Valob9ada652013-10-16 15:44:46 +03001540 struct ieee80211_sta *sta,
Kalle Valob9ada652013-10-16 15:44:46 +03001541 struct wmi_peer_assoc_complete_arg *arg)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001542{
Michal Kazior548db542013-07-05 16:15:15 +03001543 lockdep_assert_held(&ar->conf_mutex);
1544
Kalle Valob9ada652013-10-16 15:44:46 +03001545 memset(arg, 0, sizeof(*arg));
Kalle Valo5e3dd152013-06-12 20:52:10 +03001546
Michal Kazior590922a2014-10-21 10:10:29 +03001547 ath10k_peer_assoc_h_basic(ar, vif, sta, arg);
1548 ath10k_peer_assoc_h_crypto(ar, vif, arg);
Kalle Valob9ada652013-10-16 15:44:46 +03001549 ath10k_peer_assoc_h_rates(ar, sta, arg);
1550 ath10k_peer_assoc_h_ht(ar, sta, arg);
1551 ath10k_peer_assoc_h_vht(ar, sta, arg);
Michal Kazior590922a2014-10-21 10:10:29 +03001552 ath10k_peer_assoc_h_qos(ar, vif, sta, arg);
1553 ath10k_peer_assoc_h_phymode(ar, vif, sta, arg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001554
Kalle Valob9ada652013-10-16 15:44:46 +03001555 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001556}
1557
Michal Kazior90046f52014-02-14 14:45:51 +01001558static const u32 ath10k_smps_map[] = {
1559 [WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
1560 [WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
1561 [WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
1562 [WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
1563};
1564
1565static int ath10k_setup_peer_smps(struct ath10k *ar, struct ath10k_vif *arvif,
1566 const u8 *addr,
1567 const struct ieee80211_sta_ht_cap *ht_cap)
1568{
1569 int smps;
1570
1571 if (!ht_cap->ht_supported)
1572 return 0;
1573
1574 smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
1575 smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
1576
1577 if (smps >= ARRAY_SIZE(ath10k_smps_map))
1578 return -EINVAL;
1579
1580 return ath10k_wmi_peer_set_param(ar, arvif->vdev_id, addr,
1581 WMI_PEER_SMPS_STATE,
1582 ath10k_smps_map[smps]);
1583}
1584
Kalle Valo5e3dd152013-06-12 20:52:10 +03001585/* can be called only in mac80211 callbacks due to `key_count` usage */
1586static void ath10k_bss_assoc(struct ieee80211_hw *hw,
1587 struct ieee80211_vif *vif,
1588 struct ieee80211_bss_conf *bss_conf)
1589{
1590 struct ath10k *ar = hw->priv;
1591 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Michal Kazior90046f52014-02-14 14:45:51 +01001592 struct ieee80211_sta_ht_cap ht_cap;
Kalle Valob9ada652013-10-16 15:44:46 +03001593 struct wmi_peer_assoc_complete_arg peer_arg;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001594 struct ieee80211_sta *ap_sta;
1595 int ret;
1596
Michal Kazior548db542013-07-05 16:15:15 +03001597 lockdep_assert_held(&ar->conf_mutex);
1598
Michal Kazior077efc82014-10-21 10:10:29 +03001599 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
1600 arvif->vdev_id, arvif->bssid, arvif->aid);
1601
Kalle Valo5e3dd152013-06-12 20:52:10 +03001602 rcu_read_lock();
1603
1604 ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
1605 if (!ap_sta) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001606 ath10k_warn(ar, "failed to find station entry for bss %pM vdev %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02001607 bss_conf->bssid, arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001608 rcu_read_unlock();
1609 return;
1610 }
1611
Michal Kazior90046f52014-02-14 14:45:51 +01001612 /* ap_sta must be accessed only within rcu section which must be left
1613 * before calling ath10k_setup_peer_smps() which might sleep. */
1614 ht_cap = ap_sta->ht_cap;
1615
Michal Kazior590922a2014-10-21 10:10:29 +03001616 ret = ath10k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001617 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001618 ath10k_warn(ar, "failed to prepare peer assoc for %pM vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001619 bss_conf->bssid, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001620 rcu_read_unlock();
1621 return;
1622 }
1623
1624 rcu_read_unlock();
1625
Kalle Valob9ada652013-10-16 15:44:46 +03001626 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
1627 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001628 ath10k_warn(ar, "failed to run peer assoc for %pM vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001629 bss_conf->bssid, arvif->vdev_id, ret);
Kalle Valob9ada652013-10-16 15:44:46 +03001630 return;
1631 }
1632
Michal Kazior90046f52014-02-14 14:45:51 +01001633 ret = ath10k_setup_peer_smps(ar, arvif, bss_conf->bssid, &ht_cap);
1634 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001635 ath10k_warn(ar, "failed to setup peer SMPS for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001636 arvif->vdev_id, ret);
Michal Kazior90046f52014-02-14 14:45:51 +01001637 return;
1638 }
1639
Michal Kazior7aa7a722014-08-25 12:09:38 +02001640 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03001641 "mac vdev %d up (associated) bssid %pM aid %d\n",
1642 arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
1643
Michal Kazior077efc82014-10-21 10:10:29 +03001644 WARN_ON(arvif->is_up);
1645
Michal Kaziorc930f742014-01-23 11:38:25 +01001646 arvif->aid = bss_conf->aid;
Kalle Valob25f32c2014-09-14 12:50:49 +03001647 ether_addr_copy(arvif->bssid, bss_conf->bssid);
Michal Kaziorc930f742014-01-23 11:38:25 +01001648
1649 ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
1650 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001651 ath10k_warn(ar, "failed to set vdev %d up: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001652 arvif->vdev_id, ret);
Michal Kaziorc930f742014-01-23 11:38:25 +01001653 return;
1654 }
1655
1656 arvif->is_up = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001657}
1658
Kalle Valo5e3dd152013-06-12 20:52:10 +03001659static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
1660 struct ieee80211_vif *vif)
1661{
1662 struct ath10k *ar = hw->priv;
1663 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1664 int ret;
1665
Michal Kazior548db542013-07-05 16:15:15 +03001666 lockdep_assert_held(&ar->conf_mutex);
1667
Michal Kazior077efc82014-10-21 10:10:29 +03001668 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
1669 arvif->vdev_id, arvif->bssid);
Kalle Valo60c3daa2013-09-08 17:56:07 +03001670
Kalle Valo5e3dd152013-06-12 20:52:10 +03001671 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
Michal Kazior077efc82014-10-21 10:10:29 +03001672 if (ret)
1673 ath10k_warn(ar, "faield to down vdev %i: %d\n",
1674 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001675
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001676 arvif->def_wep_key_idx = 0;
Michal Kaziorc930f742014-01-23 11:38:25 +01001677 arvif->is_up = false;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001678}
1679
Michal Kazior590922a2014-10-21 10:10:29 +03001680static int ath10k_station_assoc(struct ath10k *ar,
1681 struct ieee80211_vif *vif,
1682 struct ieee80211_sta *sta,
1683 bool reassoc)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001684{
Michal Kazior590922a2014-10-21 10:10:29 +03001685 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Kalle Valob9ada652013-10-16 15:44:46 +03001686 struct wmi_peer_assoc_complete_arg peer_arg;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001687 int ret = 0;
1688
Michal Kazior548db542013-07-05 16:15:15 +03001689 lockdep_assert_held(&ar->conf_mutex);
1690
Michal Kazior590922a2014-10-21 10:10:29 +03001691 ret = ath10k_peer_assoc_prepare(ar, vif, sta, &peer_arg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001692 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001693 ath10k_warn(ar, "failed to prepare WMI peer assoc for %pM vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02001694 sta->addr, arvif->vdev_id, ret);
Kalle Valob9ada652013-10-16 15:44:46 +03001695 return ret;
1696 }
1697
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02001698 peer_arg.peer_reassoc = reassoc;
Kalle Valob9ada652013-10-16 15:44:46 +03001699 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
1700 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001701 ath10k_warn(ar, "failed to run peer assoc for STA %pM vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001702 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001703 return ret;
1704 }
1705
Michal Kaziorb1ecde32014-10-21 10:10:29 +03001706 /* Re-assoc is run only to update supported rates for given station. It
1707 * doesn't make much sense to reconfigure the peer completely.
1708 */
1709 if (!reassoc) {
1710 ret = ath10k_setup_peer_smps(ar, arvif, sta->addr,
1711 &sta->ht_cap);
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02001712 if (ret) {
Michal Kaziorb1ecde32014-10-21 10:10:29 +03001713 ath10k_warn(ar, "failed to setup peer SMPS for vdev %d: %d\n",
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02001714 arvif->vdev_id, ret);
1715 return ret;
1716 }
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02001717
Michal Kaziorb1ecde32014-10-21 10:10:29 +03001718 ret = ath10k_peer_assoc_qos_ap(ar, arvif, sta);
1719 if (ret) {
1720 ath10k_warn(ar, "failed to set qos params for STA %pM for vdev %i: %d\n",
1721 sta->addr, arvif->vdev_id, ret);
1722 return ret;
1723 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001724
Michal Kaziorb1ecde32014-10-21 10:10:29 +03001725 if (!sta->wme) {
1726 arvif->num_legacy_stations++;
1727 ret = ath10k_recalc_rtscts_prot(arvif);
1728 if (ret) {
1729 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
1730 arvif->vdev_id, ret);
1731 return ret;
1732 }
1733 }
1734
1735 ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
1736 if (ret) {
1737 ath10k_warn(ar, "failed to install peer wep keys for vdev %i: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001738 arvif->vdev_id, ret);
1739 return ret;
1740 }
1741 }
1742
Kalle Valo5e3dd152013-06-12 20:52:10 +03001743 return ret;
1744}
1745
Michal Kazior590922a2014-10-21 10:10:29 +03001746static int ath10k_station_disassoc(struct ath10k *ar,
1747 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001748 struct ieee80211_sta *sta)
1749{
Michal Kazior590922a2014-10-21 10:10:29 +03001750 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001751 int ret = 0;
1752
1753 lockdep_assert_held(&ar->conf_mutex);
1754
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02001755 if (!sta->wme) {
1756 arvif->num_legacy_stations--;
1757 ret = ath10k_recalc_rtscts_prot(arvif);
1758 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001759 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02001760 arvif->vdev_id, ret);
1761 return ret;
1762 }
1763 }
1764
Kalle Valo5e3dd152013-06-12 20:52:10 +03001765 ret = ath10k_clear_peer_keys(arvif, sta->addr);
1766 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001767 ath10k_warn(ar, "failed to clear all peer wep keys for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001768 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001769 return ret;
1770 }
1771
1772 return ret;
1773}
1774
1775/**************/
1776/* Regulatory */
1777/**************/
1778
1779static int ath10k_update_channel_list(struct ath10k *ar)
1780{
1781 struct ieee80211_hw *hw = ar->hw;
1782 struct ieee80211_supported_band **bands;
1783 enum ieee80211_band band;
1784 struct ieee80211_channel *channel;
1785 struct wmi_scan_chan_list_arg arg = {0};
1786 struct wmi_channel_arg *ch;
1787 bool passive;
1788 int len;
1789 int ret;
1790 int i;
1791
Michal Kazior548db542013-07-05 16:15:15 +03001792 lockdep_assert_held(&ar->conf_mutex);
1793
Kalle Valo5e3dd152013-06-12 20:52:10 +03001794 bands = hw->wiphy->bands;
1795 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1796 if (!bands[band])
1797 continue;
1798
1799 for (i = 0; i < bands[band]->n_channels; i++) {
1800 if (bands[band]->channels[i].flags &
1801 IEEE80211_CHAN_DISABLED)
1802 continue;
1803
1804 arg.n_channels++;
1805 }
1806 }
1807
1808 len = sizeof(struct wmi_channel_arg) * arg.n_channels;
1809 arg.channels = kzalloc(len, GFP_KERNEL);
1810 if (!arg.channels)
1811 return -ENOMEM;
1812
1813 ch = arg.channels;
1814 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1815 if (!bands[band])
1816 continue;
1817
1818 for (i = 0; i < bands[band]->n_channels; i++) {
1819 channel = &bands[band]->channels[i];
1820
1821 if (channel->flags & IEEE80211_CHAN_DISABLED)
1822 continue;
1823
1824 ch->allow_ht = true;
1825
1826 /* FIXME: when should we really allow VHT? */
1827 ch->allow_vht = true;
1828
1829 ch->allow_ibss =
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +02001830 !(channel->flags & IEEE80211_CHAN_NO_IR);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001831
1832 ch->ht40plus =
1833 !(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
1834
Marek Puzyniake8a50f82013-11-20 09:59:47 +02001835 ch->chan_radar =
1836 !!(channel->flags & IEEE80211_CHAN_RADAR);
1837
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +02001838 passive = channel->flags & IEEE80211_CHAN_NO_IR;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001839 ch->passive = passive;
1840
1841 ch->freq = channel->center_freq;
Michal Kazior2d667212014-09-18 15:21:21 +02001842 ch->band_center_freq1 = channel->center_freq;
Michal Kazior89c5c842013-10-23 04:02:13 -07001843 ch->min_power = 0;
Michal Kazior02256932013-10-23 04:02:14 -07001844 ch->max_power = channel->max_power * 2;
1845 ch->max_reg_power = channel->max_reg_power * 2;
1846 ch->max_antenna_gain = channel->max_antenna_gain * 2;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001847 ch->reg_class_id = 0; /* FIXME */
1848
1849 /* FIXME: why use only legacy modes, why not any
1850 * HT/VHT modes? Would that even make any
1851 * difference? */
1852 if (channel->band == IEEE80211_BAND_2GHZ)
1853 ch->mode = MODE_11G;
1854 else
1855 ch->mode = MODE_11A;
1856
1857 if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
1858 continue;
1859
Michal Kazior7aa7a722014-08-25 12:09:38 +02001860 ath10k_dbg(ar, ATH10K_DBG_WMI,
Kalle Valo60c3daa2013-09-08 17:56:07 +03001861 "mac channel [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
1862 ch - arg.channels, arg.n_channels,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001863 ch->freq, ch->max_power, ch->max_reg_power,
1864 ch->max_antenna_gain, ch->mode);
1865
1866 ch++;
1867 }
1868 }
1869
1870 ret = ath10k_wmi_scan_chan_list(ar, &arg);
1871 kfree(arg.channels);
1872
1873 return ret;
1874}
1875
Marek Puzyniak821af6a2014-03-21 17:46:57 +02001876static enum wmi_dfs_region
1877ath10k_mac_get_dfs_region(enum nl80211_dfs_regions dfs_region)
1878{
1879 switch (dfs_region) {
1880 case NL80211_DFS_UNSET:
1881 return WMI_UNINIT_DFS_DOMAIN;
1882 case NL80211_DFS_FCC:
1883 return WMI_FCC_DFS_DOMAIN;
1884 case NL80211_DFS_ETSI:
1885 return WMI_ETSI_DFS_DOMAIN;
1886 case NL80211_DFS_JP:
1887 return WMI_MKK4_DFS_DOMAIN;
1888 }
1889 return WMI_UNINIT_DFS_DOMAIN;
1890}
1891
Michal Kaziorf7843d72013-07-16 09:38:52 +02001892static void ath10k_regd_update(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001893{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001894 struct reg_dmn_pair_mapping *regpair;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001895 int ret;
Marek Puzyniak821af6a2014-03-21 17:46:57 +02001896 enum wmi_dfs_region wmi_dfs_reg;
1897 enum nl80211_dfs_regions nl_dfs_reg;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001898
Michal Kaziorf7843d72013-07-16 09:38:52 +02001899 lockdep_assert_held(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001900
1901 ret = ath10k_update_channel_list(ar);
1902 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001903 ath10k_warn(ar, "failed to update channel list: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001904
1905 regpair = ar->ath_common.regulatory.regpair;
Michal Kaziorf7843d72013-07-16 09:38:52 +02001906
Marek Puzyniak821af6a2014-03-21 17:46:57 +02001907 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
1908 nl_dfs_reg = ar->dfs_detector->region;
1909 wmi_dfs_reg = ath10k_mac_get_dfs_region(nl_dfs_reg);
1910 } else {
1911 wmi_dfs_reg = WMI_UNINIT_DFS_DOMAIN;
1912 }
1913
Kalle Valo5e3dd152013-06-12 20:52:10 +03001914 /* Target allows setting up per-band regdomain but ath_common provides
1915 * a combined one only */
1916 ret = ath10k_wmi_pdev_set_regdomain(ar,
Kalle Valoef8c0012014-02-13 18:13:12 +02001917 regpair->reg_domain,
1918 regpair->reg_domain, /* 2ghz */
1919 regpair->reg_domain, /* 5ghz */
Kalle Valo5e3dd152013-06-12 20:52:10 +03001920 regpair->reg_2ghz_ctl,
Marek Puzyniak821af6a2014-03-21 17:46:57 +02001921 regpair->reg_5ghz_ctl,
1922 wmi_dfs_reg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001923 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001924 ath10k_warn(ar, "failed to set pdev regdomain: %d\n", ret);
Michal Kaziorf7843d72013-07-16 09:38:52 +02001925}
Michal Kazior548db542013-07-05 16:15:15 +03001926
Michal Kaziorf7843d72013-07-16 09:38:52 +02001927static void ath10k_reg_notifier(struct wiphy *wiphy,
1928 struct regulatory_request *request)
1929{
1930 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
1931 struct ath10k *ar = hw->priv;
Janusz Dziedzic9702c682013-11-20 09:59:41 +02001932 bool result;
Michal Kaziorf7843d72013-07-16 09:38:52 +02001933
1934 ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
1935
Janusz Dziedzic9702c682013-11-20 09:59:41 +02001936 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001937 ath10k_dbg(ar, ATH10K_DBG_REGULATORY, "dfs region 0x%x\n",
Janusz Dziedzic9702c682013-11-20 09:59:41 +02001938 request->dfs_region);
1939 result = ar->dfs_detector->set_dfs_domain(ar->dfs_detector,
1940 request->dfs_region);
1941 if (!result)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001942 ath10k_warn(ar, "DFS region 0x%X not supported, will trigger radar for every pulse\n",
Janusz Dziedzic9702c682013-11-20 09:59:41 +02001943 request->dfs_region);
1944 }
1945
Michal Kaziorf7843d72013-07-16 09:38:52 +02001946 mutex_lock(&ar->conf_mutex);
1947 if (ar->state == ATH10K_STATE_ON)
1948 ath10k_regd_update(ar);
Michal Kazior548db542013-07-05 16:15:15 +03001949 mutex_unlock(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001950}
1951
1952/***************/
1953/* TX handlers */
1954/***************/
1955
Michal Kazior42c3aa62013-10-02 11:03:38 +02001956static u8 ath10k_tx_h_get_tid(struct ieee80211_hdr *hdr)
1957{
1958 if (ieee80211_is_mgmt(hdr->frame_control))
1959 return HTT_DATA_TX_EXT_TID_MGMT;
1960
1961 if (!ieee80211_is_data_qos(hdr->frame_control))
1962 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
1963
1964 if (!is_unicast_ether_addr(ieee80211_get_DA(hdr)))
1965 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
1966
1967 return ieee80211_get_qos_ctl(hdr)[0] & IEEE80211_QOS_CTL_TID_MASK;
1968}
1969
Michal Kazior2b37c292014-09-02 11:00:22 +03001970static u8 ath10k_tx_h_get_vdev_id(struct ath10k *ar, struct ieee80211_vif *vif)
Michal Kaziorddb6ad72013-10-02 11:03:39 +02001971{
Michal Kazior2b37c292014-09-02 11:00:22 +03001972 if (vif)
1973 return ath10k_vif_to_arvif(vif)->vdev_id;
Michal Kaziorddb6ad72013-10-02 11:03:39 +02001974
Michal Kazior1bbc0972014-04-08 09:45:47 +03001975 if (ar->monitor_started)
Michal Kaziorddb6ad72013-10-02 11:03:39 +02001976 return ar->monitor_vdev_id;
1977
Michal Kazior7aa7a722014-08-25 12:09:38 +02001978 ath10k_warn(ar, "failed to resolve vdev id\n");
Michal Kaziorddb6ad72013-10-02 11:03:39 +02001979 return 0;
1980}
1981
Michal Kazior4b604552014-07-21 21:03:09 +03001982/* HTT Tx uses Native Wifi tx mode which expects 802.11 frames without QoS
1983 * Control in the header.
Kalle Valo5e3dd152013-06-12 20:52:10 +03001984 */
Michal Kazior4b604552014-07-21 21:03:09 +03001985static void ath10k_tx_h_nwifi(struct ieee80211_hw *hw, struct sk_buff *skb)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001986{
1987 struct ieee80211_hdr *hdr = (void *)skb->data;
Michal Kaziorc21c64d2014-07-21 21:03:10 +03001988 struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001989 u8 *qos_ctl;
1990
1991 if (!ieee80211_is_data_qos(hdr->frame_control))
1992 return;
1993
1994 qos_ctl = ieee80211_get_qos_ctl(hdr);
Michal Kaziorba0ccd72013-07-22 14:25:28 +02001995 memmove(skb->data + IEEE80211_QOS_CTL_LEN,
1996 skb->data, (void *)qos_ctl - (void *)skb->data);
1997 skb_pull(skb, IEEE80211_QOS_CTL_LEN);
Michal Kaziorc21c64d2014-07-21 21:03:10 +03001998
1999 /* Fw/Hw generates a corrupted QoS Control Field for QoS NullFunc
2000 * frames. Powersave is handled by the fw/hw so QoS NyllFunc frames are
2001 * used only for CQM purposes (e.g. hostapd station keepalive ping) so
2002 * it is safe to downgrade to NullFunc.
2003 */
2004 if (ieee80211_is_qos_nullfunc(hdr->frame_control)) {
2005 hdr->frame_control &= ~__cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
2006 cb->htt.tid = HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
2007 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002008}
2009
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002010static void ath10k_tx_wep_key_work(struct work_struct *work)
2011{
2012 struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
2013 wep_key_work);
Michal Kazior7aa7a722014-08-25 12:09:38 +02002014 struct ath10k *ar = arvif->ar;
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002015 int ret, keyidx = arvif->def_wep_key_newidx;
2016
Michal Kazior911e6c02014-05-26 12:46:03 +03002017 mutex_lock(&arvif->ar->conf_mutex);
2018
2019 if (arvif->ar->state != ATH10K_STATE_ON)
2020 goto unlock;
2021
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002022 if (arvif->def_wep_key_idx == keyidx)
Michal Kazior911e6c02014-05-26 12:46:03 +03002023 goto unlock;
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002024
Michal Kazior7aa7a722014-08-25 12:09:38 +02002025 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002026 arvif->vdev_id, keyidx);
2027
2028 ret = ath10k_wmi_vdev_set_param(arvif->ar,
2029 arvif->vdev_id,
2030 arvif->ar->wmi.vdev_param->def_keyid,
2031 keyidx);
2032 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002033 ath10k_warn(ar, "failed to update wep key index for vdev %d: %d\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02002034 arvif->vdev_id,
2035 ret);
Michal Kazior911e6c02014-05-26 12:46:03 +03002036 goto unlock;
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002037 }
2038
2039 arvif->def_wep_key_idx = keyidx;
Michal Kazior911e6c02014-05-26 12:46:03 +03002040
2041unlock:
2042 mutex_unlock(&arvif->ar->conf_mutex);
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002043}
2044
Michal Kazior4b604552014-07-21 21:03:09 +03002045static void ath10k_tx_h_update_wep_key(struct ieee80211_vif *vif,
2046 struct ieee80211_key_conf *key,
2047 struct sk_buff *skb)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002048{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002049 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2050 struct ath10k *ar = arvif->ar;
2051 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002052
Kalle Valo5e3dd152013-06-12 20:52:10 +03002053 if (!ieee80211_has_protected(hdr->frame_control))
2054 return;
2055
2056 if (!key)
2057 return;
2058
2059 if (key->cipher != WLAN_CIPHER_SUITE_WEP40 &&
2060 key->cipher != WLAN_CIPHER_SUITE_WEP104)
2061 return;
2062
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002063 if (key->keyidx == arvif->def_wep_key_idx)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002064 return;
2065
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002066 /* FIXME: Most likely a few frames will be TXed with an old key. Simply
2067 * queueing frames until key index is updated is not an option because
2068 * sk_buff may need more processing to be done, e.g. offchannel */
2069 arvif->def_wep_key_newidx = key->keyidx;
2070 ieee80211_queue_work(ar->hw, &arvif->wep_key_work);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002071}
2072
Michal Kazior4b604552014-07-21 21:03:09 +03002073static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar,
2074 struct ieee80211_vif *vif,
2075 struct sk_buff *skb)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002076{
2077 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002078 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2079
2080 /* This is case only for P2P_GO */
2081 if (arvif->vdev_type != WMI_VDEV_TYPE_AP ||
2082 arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
2083 return;
2084
2085 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
2086 spin_lock_bh(&ar->data_lock);
2087 if (arvif->u.ap.noa_data)
2088 if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
2089 GFP_ATOMIC))
2090 memcpy(skb_put(skb, arvif->u.ap.noa_len),
2091 arvif->u.ap.noa_data,
2092 arvif->u.ap.noa_len);
2093 spin_unlock_bh(&ar->data_lock);
2094 }
2095}
2096
Michal Kazior8d6d3622014-11-24 14:58:31 +01002097static bool ath10k_mac_need_offchan_tx_work(struct ath10k *ar)
2098{
2099 /* FIXME: Not really sure since when the behaviour changed. At some
2100 * point new firmware stopped requiring creation of peer entries for
2101 * offchannel tx (and actually creating them causes issues with wmi-htc
2102 * tx credit replenishment and reliability). Assuming it's at least 3.4
2103 * because that's when the `freq` was introduced to TX_FRM HTT command.
2104 */
2105 return !(ar->htt.target_version_major >= 3 &&
2106 ar->htt.target_version_minor >= 4);
2107}
2108
Kalle Valo5e3dd152013-06-12 20:52:10 +03002109static void ath10k_tx_htt(struct ath10k *ar, struct sk_buff *skb)
2110{
2111 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002112 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002113
Michal Kazior961d4c32013-08-09 10:13:34 +02002114 if (ar->htt.target_version_major >= 3) {
2115 /* Since HTT 3.0 there is no separate mgmt tx command */
2116 ret = ath10k_htt_tx(&ar->htt, skb);
2117 goto exit;
2118 }
2119
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002120 if (ieee80211_is_mgmt(hdr->frame_control)) {
2121 if (test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
2122 ar->fw_features)) {
2123 if (skb_queue_len(&ar->wmi_mgmt_tx_queue) >=
2124 ATH10K_MAX_NUM_MGMT_PENDING) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002125 ath10k_warn(ar, "reached WMI management transmit queue limit\n");
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002126 ret = -EBUSY;
2127 goto exit;
2128 }
2129
2130 skb_queue_tail(&ar->wmi_mgmt_tx_queue, skb);
2131 ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
2132 } else {
2133 ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
2134 }
2135 } else if (!test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
2136 ar->fw_features) &&
2137 ieee80211_is_nullfunc(hdr->frame_control)) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002138 /* FW does not report tx status properly for NullFunc frames
2139 * unless they are sent through mgmt tx path. mac80211 sends
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002140 * those frames when it detects link/beacon loss and depends
2141 * on the tx status to be correct. */
Michal Kazioredb82362013-07-05 16:15:14 +03002142 ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002143 } else {
Michal Kazioredb82362013-07-05 16:15:14 +03002144 ret = ath10k_htt_tx(&ar->htt, skb);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002145 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002146
Michal Kazior961d4c32013-08-09 10:13:34 +02002147exit:
Kalle Valo5e3dd152013-06-12 20:52:10 +03002148 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002149 ath10k_warn(ar, "failed to transmit packet, dropping: %d\n",
2150 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002151 ieee80211_free_txskb(ar->hw, skb);
2152 }
2153}
2154
2155void ath10k_offchan_tx_purge(struct ath10k *ar)
2156{
2157 struct sk_buff *skb;
2158
2159 for (;;) {
2160 skb = skb_dequeue(&ar->offchan_tx_queue);
2161 if (!skb)
2162 break;
2163
2164 ieee80211_free_txskb(ar->hw, skb);
2165 }
2166}
2167
2168void ath10k_offchan_tx_work(struct work_struct *work)
2169{
2170 struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
2171 struct ath10k_peer *peer;
2172 struct ieee80211_hdr *hdr;
2173 struct sk_buff *skb;
2174 const u8 *peer_addr;
2175 int vdev_id;
2176 int ret;
2177
2178 /* FW requirement: We must create a peer before FW will send out
2179 * an offchannel frame. Otherwise the frame will be stuck and
2180 * never transmitted. We delete the peer upon tx completion.
2181 * It is unlikely that a peer for offchannel tx will already be
2182 * present. However it may be in some rare cases so account for that.
2183 * Otherwise we might remove a legitimate peer and break stuff. */
2184
2185 for (;;) {
2186 skb = skb_dequeue(&ar->offchan_tx_queue);
2187 if (!skb)
2188 break;
2189
2190 mutex_lock(&ar->conf_mutex);
2191
Michal Kazior7aa7a722014-08-25 12:09:38 +02002192 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac offchannel skb %p\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002193 skb);
2194
2195 hdr = (struct ieee80211_hdr *)skb->data;
2196 peer_addr = ieee80211_get_DA(hdr);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002197 vdev_id = ATH10K_SKB_CB(skb)->vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002198
2199 spin_lock_bh(&ar->data_lock);
2200 peer = ath10k_peer_find(ar, vdev_id, peer_addr);
2201 spin_unlock_bh(&ar->data_lock);
2202
2203 if (peer)
Kalle Valo60c3daa2013-09-08 17:56:07 +03002204 /* FIXME: should this use ath10k_warn()? */
Michal Kazior7aa7a722014-08-25 12:09:38 +02002205 ath10k_dbg(ar, ATH10K_DBG_MAC, "peer %pM on vdev %d already present\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002206 peer_addr, vdev_id);
2207
2208 if (!peer) {
2209 ret = ath10k_peer_create(ar, vdev_id, peer_addr);
2210 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002211 ath10k_warn(ar, "failed to create peer %pM on vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002212 peer_addr, vdev_id, ret);
2213 }
2214
2215 spin_lock_bh(&ar->data_lock);
Wolfram Sang16735d02013-11-14 14:32:02 -08002216 reinit_completion(&ar->offchan_tx_completed);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002217 ar->offchan_tx_skb = skb;
2218 spin_unlock_bh(&ar->data_lock);
2219
2220 ath10k_tx_htt(ar, skb);
2221
2222 ret = wait_for_completion_timeout(&ar->offchan_tx_completed,
2223 3 * HZ);
2224 if (ret <= 0)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002225 ath10k_warn(ar, "timed out waiting for offchannel skb %p\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002226 skb);
2227
2228 if (!peer) {
2229 ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
2230 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002231 ath10k_warn(ar, "failed to delete peer %pM on vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002232 peer_addr, vdev_id, ret);
2233 }
2234
2235 mutex_unlock(&ar->conf_mutex);
2236 }
2237}
2238
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002239void ath10k_mgmt_over_wmi_tx_purge(struct ath10k *ar)
2240{
2241 struct sk_buff *skb;
2242
2243 for (;;) {
2244 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
2245 if (!skb)
2246 break;
2247
2248 ieee80211_free_txskb(ar->hw, skb);
2249 }
2250}
2251
2252void ath10k_mgmt_over_wmi_tx_work(struct work_struct *work)
2253{
2254 struct ath10k *ar = container_of(work, struct ath10k, wmi_mgmt_tx_work);
2255 struct sk_buff *skb;
2256 int ret;
2257
2258 for (;;) {
2259 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
2260 if (!skb)
2261 break;
2262
2263 ret = ath10k_wmi_mgmt_tx(ar, skb);
Michal Kazior5fb5e412013-10-28 07:18:13 +01002264 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002265 ath10k_warn(ar, "failed to transmit management frame via WMI: %d\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02002266 ret);
Michal Kazior5fb5e412013-10-28 07:18:13 +01002267 ieee80211_free_txskb(ar->hw, skb);
2268 }
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002269 }
2270}
2271
Kalle Valo5e3dd152013-06-12 20:52:10 +03002272/************/
2273/* Scanning */
2274/************/
2275
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002276void __ath10k_scan_finish(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002277{
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002278 lockdep_assert_held(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002279
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002280 switch (ar->scan.state) {
2281 case ATH10K_SCAN_IDLE:
2282 break;
2283 case ATH10K_SCAN_RUNNING:
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002284 if (ar->scan.is_roc)
2285 ieee80211_remain_on_channel_expired(ar->hw);
Michal Kazior7305d3e2014-11-24 14:58:33 +01002286 case ATH10K_SCAN_ABORTING:
2287 if (!ar->scan.is_roc)
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002288 ieee80211_scan_completed(ar->hw,
2289 (ar->scan.state ==
2290 ATH10K_SCAN_ABORTING));
2291 /* fall through */
2292 case ATH10K_SCAN_STARTING:
2293 ar->scan.state = ATH10K_SCAN_IDLE;
2294 ar->scan_channel = NULL;
2295 ath10k_offchan_tx_purge(ar);
2296 cancel_delayed_work(&ar->scan.timeout);
2297 complete_all(&ar->scan.completed);
2298 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002299 }
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002300}
Kalle Valo5e3dd152013-06-12 20:52:10 +03002301
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002302void ath10k_scan_finish(struct ath10k *ar)
2303{
2304 spin_lock_bh(&ar->data_lock);
2305 __ath10k_scan_finish(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002306 spin_unlock_bh(&ar->data_lock);
2307}
2308
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002309static int ath10k_scan_stop(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002310{
2311 struct wmi_stop_scan_arg arg = {
2312 .req_id = 1, /* FIXME */
2313 .req_type = WMI_SCAN_STOP_ONE,
2314 .u.scan_id = ATH10K_SCAN_ID,
2315 };
2316 int ret;
2317
2318 lockdep_assert_held(&ar->conf_mutex);
2319
Kalle Valo5e3dd152013-06-12 20:52:10 +03002320 ret = ath10k_wmi_stop_scan(ar, &arg);
2321 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002322 ath10k_warn(ar, "failed to stop wmi scan: %d\n", ret);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002323 goto out;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002324 }
2325
Kalle Valo5e3dd152013-06-12 20:52:10 +03002326 ret = wait_for_completion_timeout(&ar->scan.completed, 3*HZ);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002327 if (ret == 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002328 ath10k_warn(ar, "failed to receive scan abortion completion: timed out\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002329 ret = -ETIMEDOUT;
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002330 } else if (ret > 0) {
2331 ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002332 }
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002333
2334out:
2335 /* Scan state should be updated upon scan completion but in case
2336 * firmware fails to deliver the event (for whatever reason) it is
2337 * desired to clean up scan state anyway. Firmware may have just
2338 * dropped the scan completion event delivery due to transport pipe
2339 * being overflown with data and/or it can recover on its own before
2340 * next scan request is submitted.
2341 */
2342 spin_lock_bh(&ar->data_lock);
2343 if (ar->scan.state != ATH10K_SCAN_IDLE)
2344 __ath10k_scan_finish(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002345 spin_unlock_bh(&ar->data_lock);
2346
2347 return ret;
2348}
2349
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002350static void ath10k_scan_abort(struct ath10k *ar)
2351{
2352 int ret;
2353
2354 lockdep_assert_held(&ar->conf_mutex);
2355
2356 spin_lock_bh(&ar->data_lock);
2357
2358 switch (ar->scan.state) {
2359 case ATH10K_SCAN_IDLE:
2360 /* This can happen if timeout worker kicked in and called
2361 * abortion while scan completion was being processed.
2362 */
2363 break;
2364 case ATH10K_SCAN_STARTING:
2365 case ATH10K_SCAN_ABORTING:
Michal Kazior7aa7a722014-08-25 12:09:38 +02002366 ath10k_warn(ar, "refusing scan abortion due to invalid scan state: %s (%d)\n",
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002367 ath10k_scan_state_str(ar->scan.state),
2368 ar->scan.state);
2369 break;
2370 case ATH10K_SCAN_RUNNING:
2371 ar->scan.state = ATH10K_SCAN_ABORTING;
2372 spin_unlock_bh(&ar->data_lock);
2373
2374 ret = ath10k_scan_stop(ar);
2375 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002376 ath10k_warn(ar, "failed to abort scan: %d\n", ret);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002377
2378 spin_lock_bh(&ar->data_lock);
2379 break;
2380 }
2381
2382 spin_unlock_bh(&ar->data_lock);
2383}
2384
2385void ath10k_scan_timeout_work(struct work_struct *work)
2386{
2387 struct ath10k *ar = container_of(work, struct ath10k,
2388 scan.timeout.work);
2389
2390 mutex_lock(&ar->conf_mutex);
2391 ath10k_scan_abort(ar);
2392 mutex_unlock(&ar->conf_mutex);
2393}
2394
Kalle Valo5e3dd152013-06-12 20:52:10 +03002395static int ath10k_start_scan(struct ath10k *ar,
2396 const struct wmi_start_scan_arg *arg)
2397{
2398 int ret;
2399
2400 lockdep_assert_held(&ar->conf_mutex);
2401
2402 ret = ath10k_wmi_start_scan(ar, arg);
2403 if (ret)
2404 return ret;
2405
Kalle Valo5e3dd152013-06-12 20:52:10 +03002406 ret = wait_for_completion_timeout(&ar->scan.started, 1*HZ);
2407 if (ret == 0) {
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002408 ret = ath10k_scan_stop(ar);
2409 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002410 ath10k_warn(ar, "failed to stop scan: %d\n", ret);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002411
2412 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002413 }
2414
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002415 /* Add a 200ms margin to account for event/command processing */
2416 ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
2417 msecs_to_jiffies(arg->max_scan_time+200));
Kalle Valo5e3dd152013-06-12 20:52:10 +03002418 return 0;
2419}
2420
2421/**********************/
2422/* mac80211 callbacks */
2423/**********************/
2424
2425static void ath10k_tx(struct ieee80211_hw *hw,
2426 struct ieee80211_tx_control *control,
2427 struct sk_buff *skb)
2428{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002429 struct ath10k *ar = hw->priv;
Michal Kazior4b604552014-07-21 21:03:09 +03002430 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2431 struct ieee80211_vif *vif = info->control.vif;
2432 struct ieee80211_key_conf *key = info->control.hw_key;
2433 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002434
2435 /* We should disable CCK RATE due to P2P */
2436 if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002437 ath10k_dbg(ar, ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002438
Michal Kazior4b604552014-07-21 21:03:09 +03002439 ATH10K_SKB_CB(skb)->htt.is_offchan = false;
2440 ATH10K_SKB_CB(skb)->htt.tid = ath10k_tx_h_get_tid(hdr);
Michal Kazior2b37c292014-09-02 11:00:22 +03002441 ATH10K_SKB_CB(skb)->vdev_id = ath10k_tx_h_get_vdev_id(ar, vif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002442
Michal Kaziorcf84bd42013-07-16 11:04:54 +02002443 /* it makes no sense to process injected frames like that */
Michal Kazior4b604552014-07-21 21:03:09 +03002444 if (vif && vif->type != NL80211_IFTYPE_MONITOR) {
2445 ath10k_tx_h_nwifi(hw, skb);
2446 ath10k_tx_h_update_wep_key(vif, key, skb);
2447 ath10k_tx_h_add_p2p_noa_ie(ar, vif, skb);
2448 ath10k_tx_h_seq_no(vif, skb);
Michal Kaziorcf84bd42013-07-16 11:04:54 +02002449 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002450
Kalle Valo5e3dd152013-06-12 20:52:10 +03002451 if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
2452 spin_lock_bh(&ar->data_lock);
Michal Kazior8d6d3622014-11-24 14:58:31 +01002453 ATH10K_SKB_CB(skb)->htt.freq = ar->scan.roc_freq;
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002454 ATH10K_SKB_CB(skb)->vdev_id = ar->scan.vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002455 spin_unlock_bh(&ar->data_lock);
2456
Michal Kazior8d6d3622014-11-24 14:58:31 +01002457 if (ath10k_mac_need_offchan_tx_work(ar)) {
2458 ATH10K_SKB_CB(skb)->htt.freq = 0;
2459 ATH10K_SKB_CB(skb)->htt.is_offchan = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002460
Michal Kazior8d6d3622014-11-24 14:58:31 +01002461 ath10k_dbg(ar, ATH10K_DBG_MAC, "queued offchannel skb %p\n",
2462 skb);
2463
2464 skb_queue_tail(&ar->offchan_tx_queue, skb);
2465 ieee80211_queue_work(hw, &ar->offchan_tx_work);
2466 return;
2467 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002468 }
2469
2470 ath10k_tx_htt(ar, skb);
2471}
2472
Michal Kaziorbca7baf2014-05-26 12:46:03 +03002473/* Must not be called with conf_mutex held as workers can use that also. */
Michal Kazior7962b0d2014-10-28 10:34:38 +01002474void ath10k_drain_tx(struct ath10k *ar)
Michal Kaziorbca7baf2014-05-26 12:46:03 +03002475{
2476 /* make sure rcu-protected mac80211 tx path itself is drained */
2477 synchronize_net();
2478
2479 ath10k_offchan_tx_purge(ar);
2480 ath10k_mgmt_over_wmi_tx_purge(ar);
2481
2482 cancel_work_sync(&ar->offchan_tx_work);
2483 cancel_work_sync(&ar->wmi_mgmt_tx_work);
2484}
2485
Michal Kazioraffd3212013-07-16 09:54:35 +02002486void ath10k_halt(struct ath10k *ar)
Michal Kazior818bdd12013-07-16 09:38:57 +02002487{
Michal Kaziord9bc4b92014-04-23 19:30:06 +03002488 struct ath10k_vif *arvif;
2489
Michal Kazior818bdd12013-07-16 09:38:57 +02002490 lockdep_assert_held(&ar->conf_mutex);
2491
Michal Kazior19337472014-08-28 12:58:16 +02002492 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
2493 ar->filter_flags = 0;
2494 ar->monitor = false;
2495
2496 if (ar->monitor_started)
Michal Kazior1bbc0972014-04-08 09:45:47 +03002497 ath10k_monitor_stop(ar);
Michal Kazior19337472014-08-28 12:58:16 +02002498
2499 ar->monitor_started = false;
Michal Kazior1bbc0972014-04-08 09:45:47 +03002500
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002501 ath10k_scan_finish(ar);
Michal Kazior818bdd12013-07-16 09:38:57 +02002502 ath10k_peer_cleanup_all(ar);
2503 ath10k_core_stop(ar);
2504 ath10k_hif_power_down(ar);
2505
2506 spin_lock_bh(&ar->data_lock);
Michal Kazior64badcb2014-09-18 11:18:02 +03002507 list_for_each_entry(arvif, &ar->arvifs, list)
2508 ath10k_mac_vif_beacon_cleanup(arvif);
Michal Kazior818bdd12013-07-16 09:38:57 +02002509 spin_unlock_bh(&ar->data_lock);
2510}
2511
Ben Greear46acf7bb2014-05-16 17:15:38 +03002512static int ath10k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
2513{
2514 struct ath10k *ar = hw->priv;
2515
2516 mutex_lock(&ar->conf_mutex);
2517
2518 if (ar->cfg_tx_chainmask) {
2519 *tx_ant = ar->cfg_tx_chainmask;
2520 *rx_ant = ar->cfg_rx_chainmask;
2521 } else {
2522 *tx_ant = ar->supp_tx_chainmask;
2523 *rx_ant = ar->supp_rx_chainmask;
2524 }
2525
2526 mutex_unlock(&ar->conf_mutex);
2527
2528 return 0;
2529}
2530
Ben Greear5572a952014-11-24 16:22:10 +02002531static void ath10k_check_chain_mask(struct ath10k *ar, u32 cm, const char *dbg)
2532{
2533 /* It is not clear that allowing gaps in chainmask
2534 * is helpful. Probably it will not do what user
2535 * is hoping for, so warn in that case.
2536 */
2537 if (cm == 15 || cm == 7 || cm == 3 || cm == 1 || cm == 0)
2538 return;
2539
2540 ath10k_warn(ar, "mac %s antenna chainmask may be invalid: 0x%x. Suggested values: 15, 7, 3, 1 or 0.\n",
2541 dbg, cm);
2542}
2543
Ben Greear46acf7bb2014-05-16 17:15:38 +03002544static int __ath10k_set_antenna(struct ath10k *ar, u32 tx_ant, u32 rx_ant)
2545{
2546 int ret;
2547
2548 lockdep_assert_held(&ar->conf_mutex);
2549
Ben Greear5572a952014-11-24 16:22:10 +02002550 ath10k_check_chain_mask(ar, tx_ant, "tx");
2551 ath10k_check_chain_mask(ar, rx_ant, "rx");
2552
Ben Greear46acf7bb2014-05-16 17:15:38 +03002553 ar->cfg_tx_chainmask = tx_ant;
2554 ar->cfg_rx_chainmask = rx_ant;
2555
2556 if ((ar->state != ATH10K_STATE_ON) &&
2557 (ar->state != ATH10K_STATE_RESTARTED))
2558 return 0;
2559
2560 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->tx_chain_mask,
2561 tx_ant);
2562 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002563 ath10k_warn(ar, "failed to set tx-chainmask: %d, req 0x%x\n",
Ben Greear46acf7bb2014-05-16 17:15:38 +03002564 ret, tx_ant);
2565 return ret;
2566 }
2567
2568 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->rx_chain_mask,
2569 rx_ant);
2570 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002571 ath10k_warn(ar, "failed to set rx-chainmask: %d, req 0x%x\n",
Ben Greear46acf7bb2014-05-16 17:15:38 +03002572 ret, rx_ant);
2573 return ret;
2574 }
2575
2576 return 0;
2577}
2578
2579static int ath10k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
2580{
2581 struct ath10k *ar = hw->priv;
2582 int ret;
2583
2584 mutex_lock(&ar->conf_mutex);
2585 ret = __ath10k_set_antenna(ar, tx_ant, rx_ant);
2586 mutex_unlock(&ar->conf_mutex);
2587 return ret;
2588}
2589
Kalle Valo5e3dd152013-06-12 20:52:10 +03002590static int ath10k_start(struct ieee80211_hw *hw)
2591{
2592 struct ath10k *ar = hw->priv;
Michal Kazior818bdd12013-07-16 09:38:57 +02002593 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002594
Michal Kaziorbca7baf2014-05-26 12:46:03 +03002595 /*
2596 * This makes sense only when restarting hw. It is harmless to call
2597 * uncoditionally. This is necessary to make sure no HTT/WMI tx
2598 * commands will be submitted while restarting.
2599 */
2600 ath10k_drain_tx(ar);
2601
Michal Kazior548db542013-07-05 16:15:15 +03002602 mutex_lock(&ar->conf_mutex);
2603
Michal Kaziorc5058f52014-05-26 12:46:03 +03002604 switch (ar->state) {
2605 case ATH10K_STATE_OFF:
2606 ar->state = ATH10K_STATE_ON;
2607 break;
2608 case ATH10K_STATE_RESTARTING:
2609 ath10k_halt(ar);
2610 ar->state = ATH10K_STATE_RESTARTED;
2611 break;
2612 case ATH10K_STATE_ON:
2613 case ATH10K_STATE_RESTARTED:
2614 case ATH10K_STATE_WEDGED:
2615 WARN_ON(1);
Michal Kazior818bdd12013-07-16 09:38:57 +02002616 ret = -EINVAL;
Michal Kaziorae254432014-05-26 12:46:02 +03002617 goto err;
Kalle Valo43d2a302014-09-10 18:23:30 +03002618 case ATH10K_STATE_UTF:
2619 ret = -EBUSY;
2620 goto err;
Michal Kazior818bdd12013-07-16 09:38:57 +02002621 }
2622
2623 ret = ath10k_hif_power_up(ar);
2624 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002625 ath10k_err(ar, "Could not init hif: %d\n", ret);
Michal Kaziorae254432014-05-26 12:46:02 +03002626 goto err_off;
Michal Kazior818bdd12013-07-16 09:38:57 +02002627 }
2628
Kalle Valo43d2a302014-09-10 18:23:30 +03002629 ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_NORMAL);
Michal Kazior818bdd12013-07-16 09:38:57 +02002630 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002631 ath10k_err(ar, "Could not init core: %d\n", ret);
Michal Kaziorae254432014-05-26 12:46:02 +03002632 goto err_power_down;
Michal Kazior818bdd12013-07-16 09:38:57 +02002633 }
2634
Bartosz Markowski226a3392013-09-26 17:47:16 +02002635 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->pmf_qos, 1);
Michal Kaziorae254432014-05-26 12:46:02 +03002636 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002637 ath10k_warn(ar, "failed to enable PMF QOS: %d\n", ret);
Michal Kaziorae254432014-05-26 12:46:02 +03002638 goto err_core_stop;
2639 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002640
Michal Kaziorc4dd0d02013-11-13 11:05:10 +01002641 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->dynamic_bw, 1);
Michal Kaziorae254432014-05-26 12:46:02 +03002642 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002643 ath10k_warn(ar, "failed to enable dynamic BW: %d\n", ret);
Michal Kaziorae254432014-05-26 12:46:02 +03002644 goto err_core_stop;
2645 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002646
Ben Greear46acf7bb2014-05-16 17:15:38 +03002647 if (ar->cfg_tx_chainmask)
2648 __ath10k_set_antenna(ar, ar->cfg_tx_chainmask,
2649 ar->cfg_rx_chainmask);
2650
Marek Puzyniakab6258e2014-01-29 15:03:31 +02002651 /*
2652 * By default FW set ARP frames ac to voice (6). In that case ARP
2653 * exchange is not working properly for UAPSD enabled AP. ARP requests
2654 * which arrives with access category 0 are processed by network stack
2655 * and send back with access category 0, but FW changes access category
2656 * to 6. Set ARP frames access category to best effort (0) solves
2657 * this problem.
2658 */
2659
2660 ret = ath10k_wmi_pdev_set_param(ar,
2661 ar->wmi.pdev_param->arp_ac_override, 0);
2662 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002663 ath10k_warn(ar, "failed to set arp ac override parameter: %d\n",
Marek Puzyniakab6258e2014-01-29 15:03:31 +02002664 ret);
Michal Kaziorae254432014-05-26 12:46:02 +03002665 goto err_core_stop;
Marek Puzyniakab6258e2014-01-29 15:03:31 +02002666 }
2667
Michal Kaziord6500972014-04-08 09:56:09 +03002668 ar->num_started_vdevs = 0;
Michal Kaziorf7843d72013-07-16 09:38:52 +02002669 ath10k_regd_update(ar);
2670
Simon Wunderlich855aed12014-08-02 09:12:54 +03002671 ath10k_spectral_start(ar);
2672
Michal Kaziorae254432014-05-26 12:46:02 +03002673 mutex_unlock(&ar->conf_mutex);
2674 return 0;
2675
2676err_core_stop:
2677 ath10k_core_stop(ar);
2678
2679err_power_down:
2680 ath10k_hif_power_down(ar);
2681
2682err_off:
2683 ar->state = ATH10K_STATE_OFF;
2684
2685err:
Michal Kazior548db542013-07-05 16:15:15 +03002686 mutex_unlock(&ar->conf_mutex);
Michal Kaziorc60bdd82014-01-29 07:26:31 +01002687 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002688}
2689
2690static void ath10k_stop(struct ieee80211_hw *hw)
2691{
2692 struct ath10k *ar = hw->priv;
2693
Michal Kaziorbca7baf2014-05-26 12:46:03 +03002694 ath10k_drain_tx(ar);
2695
Michal Kazior548db542013-07-05 16:15:15 +03002696 mutex_lock(&ar->conf_mutex);
Michal Kaziorc5058f52014-05-26 12:46:03 +03002697 if (ar->state != ATH10K_STATE_OFF) {
Michal Kazior818bdd12013-07-16 09:38:57 +02002698 ath10k_halt(ar);
Michal Kaziorc5058f52014-05-26 12:46:03 +03002699 ar->state = ATH10K_STATE_OFF;
2700 }
Michal Kazior548db542013-07-05 16:15:15 +03002701 mutex_unlock(&ar->conf_mutex);
2702
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002703 cancel_delayed_work_sync(&ar->scan.timeout);
Michal Kazioraffd3212013-07-16 09:54:35 +02002704 cancel_work_sync(&ar->restart_work);
2705}
2706
Michal Kaziorad088bf2013-10-16 15:44:46 +03002707static int ath10k_config_ps(struct ath10k *ar)
Michal Kazioraffd3212013-07-16 09:54:35 +02002708{
Michal Kaziorad088bf2013-10-16 15:44:46 +03002709 struct ath10k_vif *arvif;
2710 int ret = 0;
Michal Kazioraffd3212013-07-16 09:54:35 +02002711
2712 lockdep_assert_held(&ar->conf_mutex);
2713
Michal Kaziorad088bf2013-10-16 15:44:46 +03002714 list_for_each_entry(arvif, &ar->arvifs, list) {
2715 ret = ath10k_mac_vif_setup_ps(arvif);
2716 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002717 ath10k_warn(ar, "failed to setup powersave: %d\n", ret);
Michal Kaziorad088bf2013-10-16 15:44:46 +03002718 break;
2719 }
2720 }
Michal Kazioraffd3212013-07-16 09:54:35 +02002721
Michal Kaziorad088bf2013-10-16 15:44:46 +03002722 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002723}
2724
Michal Kaziorc930f742014-01-23 11:38:25 +01002725static const char *chandef_get_width(enum nl80211_chan_width width)
2726{
2727 switch (width) {
2728 case NL80211_CHAN_WIDTH_20_NOHT:
2729 return "20 (noht)";
2730 case NL80211_CHAN_WIDTH_20:
2731 return "20";
2732 case NL80211_CHAN_WIDTH_40:
2733 return "40";
2734 case NL80211_CHAN_WIDTH_80:
2735 return "80";
2736 case NL80211_CHAN_WIDTH_80P80:
2737 return "80+80";
2738 case NL80211_CHAN_WIDTH_160:
2739 return "160";
2740 case NL80211_CHAN_WIDTH_5:
2741 return "5";
2742 case NL80211_CHAN_WIDTH_10:
2743 return "10";
2744 }
2745 return "?";
2746}
2747
2748static void ath10k_config_chan(struct ath10k *ar)
2749{
2750 struct ath10k_vif *arvif;
Michal Kaziorc930f742014-01-23 11:38:25 +01002751 int ret;
2752
2753 lockdep_assert_held(&ar->conf_mutex);
2754
Michal Kazior7aa7a722014-08-25 12:09:38 +02002755 ath10k_dbg(ar, ATH10K_DBG_MAC,
Michal Kaziorc930f742014-01-23 11:38:25 +01002756 "mac config channel to %dMHz (cf1 %dMHz cf2 %dMHz width %s)\n",
2757 ar->chandef.chan->center_freq,
2758 ar->chandef.center_freq1,
2759 ar->chandef.center_freq2,
2760 chandef_get_width(ar->chandef.width));
2761
2762 /* First stop monitor interface. Some FW versions crash if there's a
2763 * lone monitor interface. */
Michal Kazior1bbc0972014-04-08 09:45:47 +03002764 if (ar->monitor_started)
Michal Kazior19337472014-08-28 12:58:16 +02002765 ath10k_monitor_stop(ar);
Michal Kaziorc930f742014-01-23 11:38:25 +01002766
2767 list_for_each_entry(arvif, &ar->arvifs, list) {
2768 if (!arvif->is_started)
2769 continue;
2770
Michal Kaziordc55e302014-07-29 12:53:36 +03002771 if (!arvif->is_up)
2772 continue;
2773
Michal Kaziorc930f742014-01-23 11:38:25 +01002774 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
2775 continue;
2776
Michal Kaziordc55e302014-07-29 12:53:36 +03002777 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
Michal Kaziorc930f742014-01-23 11:38:25 +01002778 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002779 ath10k_warn(ar, "failed to down vdev %d: %d\n",
Michal Kaziorc930f742014-01-23 11:38:25 +01002780 arvif->vdev_id, ret);
2781 continue;
2782 }
2783 }
2784
Michal Kaziordc55e302014-07-29 12:53:36 +03002785 /* all vdevs are downed now - attempt to restart and re-up them */
Michal Kaziorc930f742014-01-23 11:38:25 +01002786
2787 list_for_each_entry(arvif, &ar->arvifs, list) {
2788 if (!arvif->is_started)
2789 continue;
2790
2791 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
2792 continue;
2793
Michal Kaziordc55e302014-07-29 12:53:36 +03002794 ret = ath10k_vdev_restart(arvif);
Michal Kaziorc930f742014-01-23 11:38:25 +01002795 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002796 ath10k_warn(ar, "failed to restart vdev %d: %d\n",
Michal Kaziorc930f742014-01-23 11:38:25 +01002797 arvif->vdev_id, ret);
2798 continue;
2799 }
2800
2801 if (!arvif->is_up)
2802 continue;
2803
2804 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
2805 arvif->bssid);
2806 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002807 ath10k_warn(ar, "failed to bring vdev up %d: %d\n",
Michal Kaziorc930f742014-01-23 11:38:25 +01002808 arvif->vdev_id, ret);
2809 continue;
2810 }
2811 }
2812
Michal Kazior19337472014-08-28 12:58:16 +02002813 ath10k_monitor_recalc(ar);
Michal Kaziorc930f742014-01-23 11:38:25 +01002814}
2815
Michal Kazior7d9d5582014-10-21 10:40:15 +03002816static int ath10k_mac_txpower_setup(struct ath10k *ar, int txpower)
2817{
2818 int ret;
2819 u32 param;
2820
2821 lockdep_assert_held(&ar->conf_mutex);
2822
2823 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac txpower %d\n", txpower);
2824
2825 param = ar->wmi.pdev_param->txpower_limit2g;
2826 ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
2827 if (ret) {
2828 ath10k_warn(ar, "failed to set 2g txpower %d: %d\n",
2829 txpower, ret);
2830 return ret;
2831 }
2832
2833 param = ar->wmi.pdev_param->txpower_limit5g;
2834 ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
2835 if (ret) {
2836 ath10k_warn(ar, "failed to set 5g txpower %d: %d\n",
2837 txpower, ret);
2838 return ret;
2839 }
2840
2841 return 0;
2842}
2843
2844static int ath10k_mac_txpower_recalc(struct ath10k *ar)
2845{
2846 struct ath10k_vif *arvif;
2847 int ret, txpower = -1;
2848
2849 lockdep_assert_held(&ar->conf_mutex);
2850
2851 list_for_each_entry(arvif, &ar->arvifs, list) {
2852 WARN_ON(arvif->txpower < 0);
2853
2854 if (txpower == -1)
2855 txpower = arvif->txpower;
2856 else
2857 txpower = min(txpower, arvif->txpower);
2858 }
2859
2860 if (WARN_ON(txpower == -1))
2861 return -EINVAL;
2862
2863 ret = ath10k_mac_txpower_setup(ar, txpower);
2864 if (ret) {
2865 ath10k_warn(ar, "failed to setup tx power %d: %d\n",
2866 txpower, ret);
2867 return ret;
2868 }
2869
2870 return 0;
2871}
2872
Kalle Valo5e3dd152013-06-12 20:52:10 +03002873static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
2874{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002875 struct ath10k *ar = hw->priv;
2876 struct ieee80211_conf *conf = &hw->conf;
2877 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002878
2879 mutex_lock(&ar->conf_mutex);
2880
2881 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002882 ath10k_dbg(ar, ATH10K_DBG_MAC,
Michal Kaziord6500972014-04-08 09:56:09 +03002883 "mac config channel %dMHz flags 0x%x radar %d\n",
Marek Puzyniake8a50f82013-11-20 09:59:47 +02002884 conf->chandef.chan->center_freq,
Michal Kaziord6500972014-04-08 09:56:09 +03002885 conf->chandef.chan->flags,
2886 conf->radar_enabled);
Marek Puzyniake8a50f82013-11-20 09:59:47 +02002887
Kalle Valo5e3dd152013-06-12 20:52:10 +03002888 spin_lock_bh(&ar->data_lock);
2889 ar->rx_channel = conf->chandef.chan;
2890 spin_unlock_bh(&ar->data_lock);
Marek Puzyniake8a50f82013-11-20 09:59:47 +02002891
Michal Kaziord6500972014-04-08 09:56:09 +03002892 ar->radar_enabled = conf->radar_enabled;
2893 ath10k_recalc_radar_detection(ar);
Michal Kaziorc930f742014-01-23 11:38:25 +01002894
2895 if (!cfg80211_chandef_identical(&ar->chandef, &conf->chandef)) {
2896 ar->chandef = conf->chandef;
2897 ath10k_config_chan(ar);
2898 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002899 }
2900
Michal Kazioraffd3212013-07-16 09:54:35 +02002901 if (changed & IEEE80211_CONF_CHANGE_PS)
2902 ath10k_config_ps(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002903
2904 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
Michal Kazior19337472014-08-28 12:58:16 +02002905 ar->monitor = conf->flags & IEEE80211_CONF_MONITOR;
2906 ret = ath10k_monitor_recalc(ar);
2907 if (ret)
2908 ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002909 }
2910
2911 mutex_unlock(&ar->conf_mutex);
2912 return ret;
2913}
2914
Ben Greear5572a952014-11-24 16:22:10 +02002915static u32 get_nss_from_chainmask(u16 chain_mask)
2916{
2917 if ((chain_mask & 0x15) == 0x15)
2918 return 4;
2919 else if ((chain_mask & 0x7) == 0x7)
2920 return 3;
2921 else if ((chain_mask & 0x3) == 0x3)
2922 return 2;
2923 return 1;
2924}
2925
Kalle Valo5e3dd152013-06-12 20:52:10 +03002926/*
2927 * TODO:
2928 * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
2929 * because we will send mgmt frames without CCK. This requirement
2930 * for P2P_FIND/GO_NEG should be handled by checking CCK flag
2931 * in the TX packet.
2932 */
2933static int ath10k_add_interface(struct ieee80211_hw *hw,
2934 struct ieee80211_vif *vif)
2935{
2936 struct ath10k *ar = hw->priv;
2937 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2938 enum wmi_sta_powersave_param param;
2939 int ret = 0;
Kalle Valo5a13e762014-01-20 11:01:46 +02002940 u32 value;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002941 int bit;
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02002942 u32 vdev_param;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002943
2944 mutex_lock(&ar->conf_mutex);
2945
Michal Kazior0dbd09e2013-07-31 10:55:14 +02002946 memset(arvif, 0, sizeof(*arvif));
2947
Kalle Valo5e3dd152013-06-12 20:52:10 +03002948 arvif->ar = ar;
2949 arvif->vif = vif;
2950
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002951 INIT_WORK(&arvif->wep_key_work, ath10k_tx_wep_key_work);
Ben Greeare63b33f2013-10-22 14:54:14 -07002952 INIT_LIST_HEAD(&arvif->list);
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002953
Ben Greeara9aefb32014-08-12 11:02:19 +03002954 if (ar->free_vdev_map == 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002955 ath10k_warn(ar, "Free vdev map is empty, no more interfaces allowed.\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002956 ret = -EBUSY;
Michal Kazior9dad14ae2013-10-16 15:44:45 +03002957 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002958 }
Ben Greear16c11172014-09-23 14:17:16 -07002959 bit = __ffs64(ar->free_vdev_map);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002960
Ben Greear16c11172014-09-23 14:17:16 -07002961 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac create vdev %i map %llx\n",
2962 bit, ar->free_vdev_map);
2963
2964 arvif->vdev_id = bit;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002965 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002966
2967 if (ar->p2p)
2968 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
2969
2970 switch (vif->type) {
2971 case NL80211_IFTYPE_UNSPECIFIED:
2972 case NL80211_IFTYPE_STATION:
2973 arvif->vdev_type = WMI_VDEV_TYPE_STA;
2974 if (vif->p2p)
2975 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
2976 break;
2977 case NL80211_IFTYPE_ADHOC:
2978 arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
2979 break;
2980 case NL80211_IFTYPE_AP:
2981 arvif->vdev_type = WMI_VDEV_TYPE_AP;
2982
2983 if (vif->p2p)
2984 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
2985 break;
2986 case NL80211_IFTYPE_MONITOR:
2987 arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
2988 break;
2989 default:
2990 WARN_ON(1);
2991 break;
2992 }
2993
Michal Kazior64badcb2014-09-18 11:18:02 +03002994 /* Some firmware revisions don't wait for beacon tx completion before
2995 * sending another SWBA event. This could lead to hardware using old
2996 * (freed) beacon data in some cases, e.g. tx credit starvation
2997 * combined with missed TBTT. This is very very rare.
2998 *
2999 * On non-IOMMU-enabled hosts this could be a possible security issue
3000 * because hw could beacon some random data on the air. On
3001 * IOMMU-enabled hosts DMAR faults would occur in most cases and target
3002 * device would crash.
3003 *
3004 * Since there are no beacon tx completions (implicit nor explicit)
3005 * propagated to host the only workaround for this is to allocate a
3006 * DMA-coherent buffer for a lifetime of a vif and use it for all
3007 * beacon tx commands. Worst case for this approach is some beacons may
3008 * become corrupted, e.g. have garbled IEs or out-of-date TIM bitmap.
3009 */
3010 if (vif->type == NL80211_IFTYPE_ADHOC ||
3011 vif->type == NL80211_IFTYPE_AP) {
3012 arvif->beacon_buf = dma_zalloc_coherent(ar->dev,
3013 IEEE80211_MAX_FRAME_LEN,
3014 &arvif->beacon_paddr,
Rajkumar Manoharan82d7aba2014-10-10 17:38:27 +05303015 GFP_ATOMIC);
Michal Kazior64badcb2014-09-18 11:18:02 +03003016 if (!arvif->beacon_buf) {
3017 ret = -ENOMEM;
3018 ath10k_warn(ar, "failed to allocate beacon buffer: %d\n",
3019 ret);
3020 goto err;
3021 }
3022 }
3023
3024 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d bcnmode %s\n",
3025 arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
3026 arvif->beacon_buf ? "single-buf" : "per-skb");
Kalle Valo5e3dd152013-06-12 20:52:10 +03003027
3028 ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
3029 arvif->vdev_subtype, vif->addr);
3030 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003031 ath10k_warn(ar, "failed to create WMI vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003032 arvif->vdev_id, ret);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03003033 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003034 }
3035
Ben Greear16c11172014-09-23 14:17:16 -07003036 ar->free_vdev_map &= ~(1LL << arvif->vdev_id);
Michal Kazior05791192013-10-16 15:44:45 +03003037 list_add(&arvif->list, &ar->arvifs);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03003038
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003039 vdev_param = ar->wmi.vdev_param->def_keyid;
3040 ret = ath10k_wmi_vdev_set_param(ar, 0, vdev_param,
Michal Kaziorcc4827b2013-10-16 15:44:45 +03003041 arvif->def_wep_key_idx);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03003042 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003043 ath10k_warn(ar, "failed to set vdev %i default key id: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003044 arvif->vdev_id, ret);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03003045 goto err_vdev_delete;
3046 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003047
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003048 vdev_param = ar->wmi.vdev_param->tx_encap_type;
3049 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003050 ATH10K_HW_TXRX_NATIVE_WIFI);
Bartosz Markowskiebc9abd2013-10-15 09:26:20 +02003051 /* 10.X firmware does not support this VDEV parameter. Do not warn */
Michal Kazior9dad14ae2013-10-16 15:44:45 +03003052 if (ret && ret != -EOPNOTSUPP) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003053 ath10k_warn(ar, "failed to set vdev %i TX encapsulation: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003054 arvif->vdev_id, ret);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03003055 goto err_vdev_delete;
3056 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003057
Ben Greear5572a952014-11-24 16:22:10 +02003058 if (ar->cfg_tx_chainmask) {
3059 u16 nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
3060
3061 vdev_param = ar->wmi.vdev_param->nss;
3062 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3063 nss);
3064 if (ret) {
3065 ath10k_warn(ar, "failed to set vdev %i chainmask 0x%x, nss %i: %d\n",
3066 arvif->vdev_id, ar->cfg_tx_chainmask, nss,
3067 ret);
3068 goto err_vdev_delete;
3069 }
3070 }
3071
Kalle Valo5e3dd152013-06-12 20:52:10 +03003072 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3073 ret = ath10k_peer_create(ar, arvif->vdev_id, vif->addr);
3074 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003075 ath10k_warn(ar, "failed to create vdev %i peer for AP: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003076 arvif->vdev_id, ret);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03003077 goto err_vdev_delete;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003078 }
Marek Puzyniakcdf07402013-12-30 09:07:51 +01003079
Kalle Valo5a13e762014-01-20 11:01:46 +02003080 ret = ath10k_mac_set_kickout(arvif);
3081 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003082 ath10k_warn(ar, "failed to set vdev %i kickout parameters: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003083 arvif->vdev_id, ret);
Kalle Valo5a13e762014-01-20 11:01:46 +02003084 goto err_peer_delete;
3085 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003086 }
3087
3088 if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
3089 param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
3090 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
3091 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3092 param, value);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03003093 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003094 ath10k_warn(ar, "failed to set vdev %i RX wake policy: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003095 arvif->vdev_id, ret);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03003096 goto err_peer_delete;
3097 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003098
Michal Kazior9f9b5742014-12-12 12:41:36 +01003099 ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03003100 if (ret) {
Michal Kazior9f9b5742014-12-12 12:41:36 +01003101 ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003102 arvif->vdev_id, ret);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03003103 goto err_peer_delete;
3104 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003105
Michal Kazior9f9b5742014-12-12 12:41:36 +01003106 ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03003107 if (ret) {
Michal Kazior9f9b5742014-12-12 12:41:36 +01003108 ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003109 arvif->vdev_id, ret);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03003110 goto err_peer_delete;
3111 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003112 }
3113
Michal Kazior424121c2013-07-22 14:13:31 +02003114 ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03003115 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003116 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
Michal Kazior679c54a2013-07-05 16:15:04 +03003117 arvif->vdev_id, ret);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03003118 goto err_peer_delete;
3119 }
Michal Kazior679c54a2013-07-05 16:15:04 +03003120
Michal Kazior424121c2013-07-22 14:13:31 +02003121 ret = ath10k_mac_set_frag(arvif, ar->hw->wiphy->frag_threshold);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03003122 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003123 ath10k_warn(ar, "failed to set frag threshold for vdev %d: %d\n",
Michal Kazior679c54a2013-07-05 16:15:04 +03003124 arvif->vdev_id, ret);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03003125 goto err_peer_delete;
3126 }
Michal Kazior679c54a2013-07-05 16:15:04 +03003127
Michal Kazior7d9d5582014-10-21 10:40:15 +03003128 arvif->txpower = vif->bss_conf.txpower;
3129 ret = ath10k_mac_txpower_recalc(ar);
3130 if (ret) {
3131 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
3132 goto err_peer_delete;
3133 }
3134
Kalle Valo5e3dd152013-06-12 20:52:10 +03003135 mutex_unlock(&ar->conf_mutex);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03003136 return 0;
3137
3138err_peer_delete:
3139 if (arvif->vdev_type == WMI_VDEV_TYPE_AP)
3140 ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
3141
3142err_vdev_delete:
3143 ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
Ben Greear16c11172014-09-23 14:17:16 -07003144 ar->free_vdev_map |= 1LL << arvif->vdev_id;
Michal Kazior05791192013-10-16 15:44:45 +03003145 list_del(&arvif->list);
Michal Kazior9dad14ae2013-10-16 15:44:45 +03003146
3147err:
Michal Kazior64badcb2014-09-18 11:18:02 +03003148 if (arvif->beacon_buf) {
3149 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
3150 arvif->beacon_buf, arvif->beacon_paddr);
3151 arvif->beacon_buf = NULL;
3152 }
3153
Michal Kazior9dad14ae2013-10-16 15:44:45 +03003154 mutex_unlock(&ar->conf_mutex);
3155
Kalle Valo5e3dd152013-06-12 20:52:10 +03003156 return ret;
3157}
3158
3159static void ath10k_remove_interface(struct ieee80211_hw *hw,
3160 struct ieee80211_vif *vif)
3161{
3162 struct ath10k *ar = hw->priv;
3163 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3164 int ret;
3165
Michal Kaziorcc4827b2013-10-16 15:44:45 +03003166 cancel_work_sync(&arvif->wep_key_work);
3167
Sujith Manoharan5d011f52014-11-25 11:47:00 +05303168 mutex_lock(&ar->conf_mutex);
3169
Michal Kaziored543882013-09-13 14:16:56 +02003170 spin_lock_bh(&ar->data_lock);
Michal Kazior64badcb2014-09-18 11:18:02 +03003171 ath10k_mac_vif_beacon_cleanup(arvif);
Michal Kaziored543882013-09-13 14:16:56 +02003172 spin_unlock_bh(&ar->data_lock);
3173
Simon Wunderlich855aed12014-08-02 09:12:54 +03003174 ret = ath10k_spectral_vif_stop(arvif);
3175 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003176 ath10k_warn(ar, "failed to stop spectral for vdev %i: %d\n",
Simon Wunderlich855aed12014-08-02 09:12:54 +03003177 arvif->vdev_id, ret);
3178
Ben Greear16c11172014-09-23 14:17:16 -07003179 ar->free_vdev_map |= 1LL << arvif->vdev_id;
Michal Kazior05791192013-10-16 15:44:45 +03003180 list_del(&arvif->list);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003181
3182 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3183 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, vif->addr);
3184 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003185 ath10k_warn(ar, "failed to remove peer for AP vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003186 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003187
3188 kfree(arvif->u.ap.noa_data);
3189 }
3190
Michal Kazior7aa7a722014-08-25 12:09:38 +02003191 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i delete (remove interface)\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03003192 arvif->vdev_id);
3193
Kalle Valo5e3dd152013-06-12 20:52:10 +03003194 ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
3195 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003196 ath10k_warn(ar, "failed to delete WMI vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003197 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003198
Kalle Valo5e3dd152013-06-12 20:52:10 +03003199 ath10k_peer_cleanup(ar, arvif->vdev_id);
3200
3201 mutex_unlock(&ar->conf_mutex);
3202}
3203
3204/*
3205 * FIXME: Has to be verified.
3206 */
3207#define SUPPORTED_FILTERS \
3208 (FIF_PROMISC_IN_BSS | \
3209 FIF_ALLMULTI | \
3210 FIF_CONTROL | \
3211 FIF_PSPOLL | \
3212 FIF_OTHER_BSS | \
3213 FIF_BCN_PRBRESP_PROMISC | \
3214 FIF_PROBE_REQ | \
3215 FIF_FCSFAIL)
3216
3217static void ath10k_configure_filter(struct ieee80211_hw *hw,
3218 unsigned int changed_flags,
3219 unsigned int *total_flags,
3220 u64 multicast)
3221{
3222 struct ath10k *ar = hw->priv;
3223 int ret;
3224
3225 mutex_lock(&ar->conf_mutex);
3226
3227 changed_flags &= SUPPORTED_FILTERS;
3228 *total_flags &= SUPPORTED_FILTERS;
3229 ar->filter_flags = *total_flags;
3230
Michal Kazior19337472014-08-28 12:58:16 +02003231 ret = ath10k_monitor_recalc(ar);
3232 if (ret)
3233 ath10k_warn(ar, "failed to recalc montior: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003234
3235 mutex_unlock(&ar->conf_mutex);
3236}
3237
3238static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
3239 struct ieee80211_vif *vif,
3240 struct ieee80211_bss_conf *info,
3241 u32 changed)
3242{
3243 struct ath10k *ar = hw->priv;
3244 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3245 int ret = 0;
Kalle Valoaf762c02014-09-14 12:50:17 +03003246 u32 vdev_param, pdev_param, slottime, preamble;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003247
3248 mutex_lock(&ar->conf_mutex);
3249
3250 if (changed & BSS_CHANGED_IBSS)
3251 ath10k_control_ibss(arvif, info, vif->addr);
3252
3253 if (changed & BSS_CHANGED_BEACON_INT) {
3254 arvif->beacon_interval = info->beacon_int;
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003255 vdev_param = ar->wmi.vdev_param->beacon_interval;
3256 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003257 arvif->beacon_interval);
Michal Kazior7aa7a722014-08-25 12:09:38 +02003258 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003259 "mac vdev %d beacon_interval %d\n",
3260 arvif->vdev_id, arvif->beacon_interval);
3261
Kalle Valo5e3dd152013-06-12 20:52:10 +03003262 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003263 ath10k_warn(ar, "failed to set beacon interval for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003264 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003265 }
3266
3267 if (changed & BSS_CHANGED_BEACON) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003268 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003269 "vdev %d set beacon tx mode to staggered\n",
3270 arvif->vdev_id);
3271
Bartosz Markowski226a3392013-09-26 17:47:16 +02003272 pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
3273 ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003274 WMI_BEACON_STAGGERED_MODE);
3275 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003276 ath10k_warn(ar, "failed to set beacon mode for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003277 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003278 }
3279
John W. Linvilleb70727e82013-06-13 13:34:29 -04003280 if (changed & BSS_CHANGED_BEACON_INFO) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03003281 arvif->dtim_period = info->dtim_period;
3282
Michal Kazior7aa7a722014-08-25 12:09:38 +02003283 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003284 "mac vdev %d dtim_period %d\n",
3285 arvif->vdev_id, arvif->dtim_period);
3286
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003287 vdev_param = ar->wmi.vdev_param->dtim_period;
3288 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003289 arvif->dtim_period);
3290 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003291 ath10k_warn(ar, "failed to set dtim period for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003292 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003293 }
3294
3295 if (changed & BSS_CHANGED_SSID &&
3296 vif->type == NL80211_IFTYPE_AP) {
3297 arvif->u.ap.ssid_len = info->ssid_len;
3298 if (info->ssid_len)
3299 memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
3300 arvif->u.ap.hidden_ssid = info->hidden_ssid;
3301 }
3302
Michal Kazior077efc82014-10-21 10:10:29 +03003303 if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
3304 ether_addr_copy(arvif->bssid, info->bssid);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003305
3306 if (changed & BSS_CHANGED_BEACON_ENABLED)
3307 ath10k_control_beaconing(arvif, info);
3308
3309 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02003310 arvif->use_cts_prot = info->use_cts_prot;
Michal Kazior7aa7a722014-08-25 12:09:38 +02003311 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d cts_prot %d\n",
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02003312 arvif->vdev_id, info->use_cts_prot);
Kalle Valo60c3daa2013-09-08 17:56:07 +03003313
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02003314 ret = ath10k_recalc_rtscts_prot(arvif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003315 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003316 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003317 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003318 }
3319
3320 if (changed & BSS_CHANGED_ERP_SLOT) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03003321 if (info->use_short_slot)
3322 slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
3323
3324 else
3325 slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
3326
Michal Kazior7aa7a722014-08-25 12:09:38 +02003327 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03003328 arvif->vdev_id, slottime);
3329
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003330 vdev_param = ar->wmi.vdev_param->slot_time;
3331 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003332 slottime);
3333 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003334 ath10k_warn(ar, "failed to set erp slot for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003335 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003336 }
3337
3338 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03003339 if (info->use_short_preamble)
3340 preamble = WMI_VDEV_PREAMBLE_SHORT;
3341 else
3342 preamble = WMI_VDEV_PREAMBLE_LONG;
3343
Michal Kazior7aa7a722014-08-25 12:09:38 +02003344 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003345 "mac vdev %d preamble %dn",
3346 arvif->vdev_id, preamble);
3347
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003348 vdev_param = ar->wmi.vdev_param->preamble;
3349 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003350 preamble);
3351 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003352 ath10k_warn(ar, "failed to set preamble for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003353 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003354 }
3355
3356 if (changed & BSS_CHANGED_ASSOC) {
Michal Kaziore556f112014-08-28 12:58:17 +02003357 if (info->assoc) {
3358 /* Workaround: Make sure monitor vdev is not running
3359 * when associating to prevent some firmware revisions
3360 * (e.g. 10.1 and 10.2) from crashing.
3361 */
3362 if (ar->monitor_started)
3363 ath10k_monitor_stop(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003364 ath10k_bss_assoc(hw, vif, info);
Michal Kaziore556f112014-08-28 12:58:17 +02003365 ath10k_monitor_recalc(ar);
Michal Kazior077efc82014-10-21 10:10:29 +03003366 } else {
3367 ath10k_bss_disassoc(hw, vif);
Michal Kaziore556f112014-08-28 12:58:17 +02003368 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003369 }
3370
Michal Kazior7d9d5582014-10-21 10:40:15 +03003371 if (changed & BSS_CHANGED_TXPOWER) {
3372 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev_id %i txpower %d\n",
3373 arvif->vdev_id, info->txpower);
3374
3375 arvif->txpower = info->txpower;
3376 ret = ath10k_mac_txpower_recalc(ar);
3377 if (ret)
3378 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
3379 }
3380
Kalle Valo5e3dd152013-06-12 20:52:10 +03003381 mutex_unlock(&ar->conf_mutex);
3382}
3383
3384static int ath10k_hw_scan(struct ieee80211_hw *hw,
3385 struct ieee80211_vif *vif,
David Spinadelc56ef672014-02-05 15:21:13 +02003386 struct ieee80211_scan_request *hw_req)
Kalle Valo5e3dd152013-06-12 20:52:10 +03003387{
3388 struct ath10k *ar = hw->priv;
3389 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
David Spinadelc56ef672014-02-05 15:21:13 +02003390 struct cfg80211_scan_request *req = &hw_req->req;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003391 struct wmi_start_scan_arg arg;
3392 int ret = 0;
3393 int i;
3394
3395 mutex_lock(&ar->conf_mutex);
3396
3397 spin_lock_bh(&ar->data_lock);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003398 switch (ar->scan.state) {
3399 case ATH10K_SCAN_IDLE:
3400 reinit_completion(&ar->scan.started);
3401 reinit_completion(&ar->scan.completed);
3402 ar->scan.state = ATH10K_SCAN_STARTING;
3403 ar->scan.is_roc = false;
3404 ar->scan.vdev_id = arvif->vdev_id;
3405 ret = 0;
3406 break;
3407 case ATH10K_SCAN_STARTING:
3408 case ATH10K_SCAN_RUNNING:
3409 case ATH10K_SCAN_ABORTING:
Kalle Valo5e3dd152013-06-12 20:52:10 +03003410 ret = -EBUSY;
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003411 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003412 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003413 spin_unlock_bh(&ar->data_lock);
3414
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003415 if (ret)
3416 goto exit;
3417
Kalle Valo5e3dd152013-06-12 20:52:10 +03003418 memset(&arg, 0, sizeof(arg));
3419 ath10k_wmi_start_scan_init(ar, &arg);
3420 arg.vdev_id = arvif->vdev_id;
3421 arg.scan_id = ATH10K_SCAN_ID;
3422
3423 if (!req->no_cck)
3424 arg.scan_ctrl_flags |= WMI_SCAN_ADD_CCK_RATES;
3425
3426 if (req->ie_len) {
3427 arg.ie_len = req->ie_len;
3428 memcpy(arg.ie, req->ie, arg.ie_len);
3429 }
3430
3431 if (req->n_ssids) {
3432 arg.n_ssids = req->n_ssids;
3433 for (i = 0; i < arg.n_ssids; i++) {
3434 arg.ssids[i].len = req->ssids[i].ssid_len;
3435 arg.ssids[i].ssid = req->ssids[i].ssid;
3436 }
Michal Kaziordcd4a562013-07-31 10:55:12 +02003437 } else {
3438 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003439 }
3440
3441 if (req->n_channels) {
3442 arg.n_channels = req->n_channels;
3443 for (i = 0; i < arg.n_channels; i++)
3444 arg.channels[i] = req->channels[i]->center_freq;
3445 }
3446
3447 ret = ath10k_start_scan(ar, &arg);
3448 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003449 ath10k_warn(ar, "failed to start hw scan: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003450 spin_lock_bh(&ar->data_lock);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003451 ar->scan.state = ATH10K_SCAN_IDLE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003452 spin_unlock_bh(&ar->data_lock);
3453 }
3454
3455exit:
3456 mutex_unlock(&ar->conf_mutex);
3457 return ret;
3458}
3459
3460static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
3461 struct ieee80211_vif *vif)
3462{
3463 struct ath10k *ar = hw->priv;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003464
3465 mutex_lock(&ar->conf_mutex);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003466 ath10k_scan_abort(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003467 mutex_unlock(&ar->conf_mutex);
Michal Kazior4eb2e162014-10-28 10:23:09 +01003468
3469 cancel_delayed_work_sync(&ar->scan.timeout);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003470}
3471
Michal Kaziorcfb27d22013-12-02 09:06:36 +01003472static void ath10k_set_key_h_def_keyidx(struct ath10k *ar,
3473 struct ath10k_vif *arvif,
3474 enum set_key_cmd cmd,
3475 struct ieee80211_key_conf *key)
3476{
3477 u32 vdev_param = arvif->ar->wmi.vdev_param->def_keyid;
3478 int ret;
3479
3480 /* 10.1 firmware branch requires default key index to be set to group
3481 * key index after installing it. Otherwise FW/HW Txes corrupted
3482 * frames with multi-vif APs. This is not required for main firmware
3483 * branch (e.g. 636).
3484 *
3485 * FIXME: This has been tested only in AP. It remains unknown if this
3486 * is required for multi-vif STA interfaces on 10.1 */
3487
3488 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
3489 return;
3490
3491 if (key->cipher == WLAN_CIPHER_SUITE_WEP40)
3492 return;
3493
3494 if (key->cipher == WLAN_CIPHER_SUITE_WEP104)
3495 return;
3496
3497 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3498 return;
3499
3500 if (cmd != SET_KEY)
3501 return;
3502
3503 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3504 key->keyidx);
3505 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003506 ath10k_warn(ar, "failed to set vdev %i group key as default key: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003507 arvif->vdev_id, ret);
Michal Kaziorcfb27d22013-12-02 09:06:36 +01003508}
3509
Kalle Valo5e3dd152013-06-12 20:52:10 +03003510static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
3511 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
3512 struct ieee80211_key_conf *key)
3513{
3514 struct ath10k *ar = hw->priv;
3515 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3516 struct ath10k_peer *peer;
3517 const u8 *peer_addr;
3518 bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3519 key->cipher == WLAN_CIPHER_SUITE_WEP104;
3520 int ret = 0;
3521
3522 if (key->keyidx > WMI_MAX_KEY_INDEX)
3523 return -ENOSPC;
3524
3525 mutex_lock(&ar->conf_mutex);
3526
3527 if (sta)
3528 peer_addr = sta->addr;
3529 else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
3530 peer_addr = vif->bss_conf.bssid;
3531 else
3532 peer_addr = vif->addr;
3533
3534 key->hw_key_idx = key->keyidx;
3535
3536 /* the peer should not disappear in mid-way (unless FW goes awry) since
3537 * we already hold conf_mutex. we just make sure its there now. */
3538 spin_lock_bh(&ar->data_lock);
3539 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
3540 spin_unlock_bh(&ar->data_lock);
3541
3542 if (!peer) {
3543 if (cmd == SET_KEY) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003544 ath10k_warn(ar, "failed to install key for non-existent peer %pM\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03003545 peer_addr);
3546 ret = -EOPNOTSUPP;
3547 goto exit;
3548 } else {
3549 /* if the peer doesn't exist there is no key to disable
3550 * anymore */
3551 goto exit;
3552 }
3553 }
3554
3555 if (is_wep) {
3556 if (cmd == SET_KEY)
3557 arvif->wep_keys[key->keyidx] = key;
3558 else
3559 arvif->wep_keys[key->keyidx] = NULL;
3560
3561 if (cmd == DISABLE_KEY)
3562 ath10k_clear_vdev_key(arvif, key);
3563 }
3564
3565 ret = ath10k_install_key(arvif, key, cmd, peer_addr);
3566 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003567 ath10k_warn(ar, "failed to install key for vdev %i peer %pM: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003568 arvif->vdev_id, peer_addr, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003569 goto exit;
3570 }
3571
Michal Kaziorcfb27d22013-12-02 09:06:36 +01003572 ath10k_set_key_h_def_keyidx(ar, arvif, cmd, key);
3573
Kalle Valo5e3dd152013-06-12 20:52:10 +03003574 spin_lock_bh(&ar->data_lock);
3575 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
3576 if (peer && cmd == SET_KEY)
3577 peer->keys[key->keyidx] = key;
3578 else if (peer && cmd == DISABLE_KEY)
3579 peer->keys[key->keyidx] = NULL;
3580 else if (peer == NULL)
3581 /* impossible unless FW goes crazy */
Michal Kazior7aa7a722014-08-25 12:09:38 +02003582 ath10k_warn(ar, "Peer %pM disappeared!\n", peer_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003583 spin_unlock_bh(&ar->data_lock);
3584
3585exit:
3586 mutex_unlock(&ar->conf_mutex);
3587 return ret;
3588}
3589
Michal Kazior9797feb2014-02-14 14:49:48 +01003590static void ath10k_sta_rc_update_wk(struct work_struct *wk)
3591{
3592 struct ath10k *ar;
3593 struct ath10k_vif *arvif;
3594 struct ath10k_sta *arsta;
3595 struct ieee80211_sta *sta;
3596 u32 changed, bw, nss, smps;
3597 int err;
3598
3599 arsta = container_of(wk, struct ath10k_sta, update_wk);
3600 sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
3601 arvif = arsta->arvif;
3602 ar = arvif->ar;
3603
3604 spin_lock_bh(&ar->data_lock);
3605
3606 changed = arsta->changed;
3607 arsta->changed = 0;
3608
3609 bw = arsta->bw;
3610 nss = arsta->nss;
3611 smps = arsta->smps;
3612
3613 spin_unlock_bh(&ar->data_lock);
3614
3615 mutex_lock(&ar->conf_mutex);
3616
3617 if (changed & IEEE80211_RC_BW_CHANGED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003618 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM peer bw %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003619 sta->addr, bw);
3620
3621 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
3622 WMI_PEER_CHAN_WIDTH, bw);
3623 if (err)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003624 ath10k_warn(ar, "failed to update STA %pM peer bw %d: %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003625 sta->addr, bw, err);
3626 }
3627
3628 if (changed & IEEE80211_RC_NSS_CHANGED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003629 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM nss %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003630 sta->addr, nss);
3631
3632 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
3633 WMI_PEER_NSS, nss);
3634 if (err)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003635 ath10k_warn(ar, "failed to update STA %pM nss %d: %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003636 sta->addr, nss, err);
3637 }
3638
3639 if (changed & IEEE80211_RC_SMPS_CHANGED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003640 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM smps %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003641 sta->addr, smps);
3642
3643 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
3644 WMI_PEER_SMPS_STATE, smps);
3645 if (err)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003646 ath10k_warn(ar, "failed to update STA %pM smps %d: %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003647 sta->addr, smps, err);
3648 }
3649
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02003650 if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003651 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM supp rates\n",
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02003652 sta->addr);
3653
Michal Kazior590922a2014-10-21 10:10:29 +03003654 err = ath10k_station_assoc(ar, arvif->vif, sta, true);
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02003655 if (err)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003656 ath10k_warn(ar, "failed to reassociate station: %pM\n",
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02003657 sta->addr);
3658 }
3659
Michal Kazior9797feb2014-02-14 14:49:48 +01003660 mutex_unlock(&ar->conf_mutex);
3661}
3662
Michal Kaziorcfd10612014-11-25 15:16:05 +01003663static int ath10k_mac_inc_num_stations(struct ath10k_vif *arvif)
3664{
3665 struct ath10k *ar = arvif->ar;
3666
3667 lockdep_assert_held(&ar->conf_mutex);
3668
3669 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
3670 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
3671 return 0;
3672
3673 if (ar->num_stations >= ar->max_num_stations)
3674 return -ENOBUFS;
3675
3676 ar->num_stations++;
3677
3678 return 0;
3679}
3680
3681static void ath10k_mac_dec_num_stations(struct ath10k_vif *arvif)
3682{
3683 struct ath10k *ar = arvif->ar;
3684
3685 lockdep_assert_held(&ar->conf_mutex);
3686
3687 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
3688 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
3689 return;
3690
3691 ar->num_stations--;
3692}
3693
Kalle Valo5e3dd152013-06-12 20:52:10 +03003694static int ath10k_sta_state(struct ieee80211_hw *hw,
3695 struct ieee80211_vif *vif,
3696 struct ieee80211_sta *sta,
3697 enum ieee80211_sta_state old_state,
3698 enum ieee80211_sta_state new_state)
3699{
3700 struct ath10k *ar = hw->priv;
3701 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Michal Kazior9797feb2014-02-14 14:49:48 +01003702 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003703 int ret = 0;
3704
Michal Kazior76f90022014-02-25 09:29:57 +02003705 if (old_state == IEEE80211_STA_NOTEXIST &&
3706 new_state == IEEE80211_STA_NONE) {
3707 memset(arsta, 0, sizeof(*arsta));
3708 arsta->arvif = arvif;
3709 INIT_WORK(&arsta->update_wk, ath10k_sta_rc_update_wk);
3710 }
3711
Michal Kazior9797feb2014-02-14 14:49:48 +01003712 /* cancel must be done outside the mutex to avoid deadlock */
3713 if ((old_state == IEEE80211_STA_NONE &&
3714 new_state == IEEE80211_STA_NOTEXIST))
3715 cancel_work_sync(&arsta->update_wk);
3716
Kalle Valo5e3dd152013-06-12 20:52:10 +03003717 mutex_lock(&ar->conf_mutex);
3718
3719 if (old_state == IEEE80211_STA_NOTEXIST &&
Michal Kazior077efc82014-10-21 10:10:29 +03003720 new_state == IEEE80211_STA_NONE) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03003721 /*
3722 * New station addition.
3723 */
Michal Kaziorcfd10612014-11-25 15:16:05 +01003724 ath10k_dbg(ar, ATH10K_DBG_MAC,
3725 "mac vdev %d peer create %pM (new sta) sta %d / %d peer %d / %d\n",
3726 arvif->vdev_id, sta->addr,
3727 ar->num_stations + 1, ar->max_num_stations,
3728 ar->num_peers + 1, ar->max_num_peers);
Bartosz Markowski0e759f32014-01-02 14:38:33 +01003729
Michal Kaziorcfd10612014-11-25 15:16:05 +01003730 ret = ath10k_mac_inc_num_stations(arvif);
3731 if (ret) {
3732 ath10k_warn(ar, "refusing to associate station: too many connected already (%d)\n",
3733 ar->max_num_stations);
Bartosz Markowski0e759f32014-01-02 14:38:33 +01003734 goto exit;
3735 }
3736
Kalle Valo5e3dd152013-06-12 20:52:10 +03003737 ret = ath10k_peer_create(ar, arvif->vdev_id, sta->addr);
Michal Kaziora52c0282014-11-25 15:16:03 +01003738 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003739 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 -08003740 sta->addr, arvif->vdev_id, ret);
Michal Kaziorcfd10612014-11-25 15:16:05 +01003741 ath10k_mac_dec_num_stations(arvif);
Michal Kaziora52c0282014-11-25 15:16:03 +01003742 goto exit;
3743 }
Michal Kazior077efc82014-10-21 10:10:29 +03003744
3745 if (vif->type == NL80211_IFTYPE_STATION) {
3746 WARN_ON(arvif->is_started);
3747
3748 ret = ath10k_vdev_start(arvif);
3749 if (ret) {
3750 ath10k_warn(ar, "failed to start vdev %i: %d\n",
3751 arvif->vdev_id, ret);
3752 WARN_ON(ath10k_peer_delete(ar, arvif->vdev_id,
3753 sta->addr));
Michal Kaziorcfd10612014-11-25 15:16:05 +01003754 ath10k_mac_dec_num_stations(arvif);
Michal Kazior077efc82014-10-21 10:10:29 +03003755 goto exit;
3756 }
3757
3758 arvif->is_started = true;
3759 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003760 } else if ((old_state == IEEE80211_STA_NONE &&
3761 new_state == IEEE80211_STA_NOTEXIST)) {
3762 /*
3763 * Existing station deletion.
3764 */
Michal Kazior7aa7a722014-08-25 12:09:38 +02003765 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003766 "mac vdev %d peer delete %pM (sta gone)\n",
3767 arvif->vdev_id, sta->addr);
Michal Kazior077efc82014-10-21 10:10:29 +03003768
3769 if (vif->type == NL80211_IFTYPE_STATION) {
3770 WARN_ON(!arvif->is_started);
3771
3772 ret = ath10k_vdev_stop(arvif);
3773 if (ret)
3774 ath10k_warn(ar, "failed to stop vdev %i: %d\n",
3775 arvif->vdev_id, ret);
3776
3777 arvif->is_started = false;
3778 }
3779
Kalle Valo5e3dd152013-06-12 20:52:10 +03003780 ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
3781 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003782 ath10k_warn(ar, "failed to delete peer %pM for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003783 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003784
Michal Kaziorcfd10612014-11-25 15:16:05 +01003785 ath10k_mac_dec_num_stations(arvif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003786 } else if (old_state == IEEE80211_STA_AUTH &&
3787 new_state == IEEE80211_STA_ASSOC &&
3788 (vif->type == NL80211_IFTYPE_AP ||
3789 vif->type == NL80211_IFTYPE_ADHOC)) {
3790 /*
3791 * New association.
3792 */
Michal Kazior7aa7a722014-08-25 12:09:38 +02003793 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM associated\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03003794 sta->addr);
3795
Michal Kazior590922a2014-10-21 10:10:29 +03003796 ret = ath10k_station_assoc(ar, vif, sta, false);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003797 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003798 ath10k_warn(ar, "failed to associate station %pM for vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003799 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003800 } else if (old_state == IEEE80211_STA_ASSOC &&
3801 new_state == IEEE80211_STA_AUTH &&
3802 (vif->type == NL80211_IFTYPE_AP ||
3803 vif->type == NL80211_IFTYPE_ADHOC)) {
3804 /*
3805 * Disassociation.
3806 */
Michal Kazior7aa7a722014-08-25 12:09:38 +02003807 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM disassociated\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03003808 sta->addr);
3809
Michal Kazior590922a2014-10-21 10:10:29 +03003810 ret = ath10k_station_disassoc(ar, vif, sta);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003811 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003812 ath10k_warn(ar, "failed to disassociate station: %pM vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003813 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003814 }
Bartosz Markowski0e759f32014-01-02 14:38:33 +01003815exit:
Kalle Valo5e3dd152013-06-12 20:52:10 +03003816 mutex_unlock(&ar->conf_mutex);
3817 return ret;
3818}
3819
3820static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
Kalle Valo5b07e072014-09-14 12:50:06 +03003821 u16 ac, bool enable)
Kalle Valo5e3dd152013-06-12 20:52:10 +03003822{
3823 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3824 u32 value = 0;
3825 int ret = 0;
3826
Michal Kazior548db542013-07-05 16:15:15 +03003827 lockdep_assert_held(&ar->conf_mutex);
3828
Kalle Valo5e3dd152013-06-12 20:52:10 +03003829 if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
3830 return 0;
3831
3832 switch (ac) {
3833 case IEEE80211_AC_VO:
3834 value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
3835 WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
3836 break;
3837 case IEEE80211_AC_VI:
3838 value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
3839 WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
3840 break;
3841 case IEEE80211_AC_BE:
3842 value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
3843 WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
3844 break;
3845 case IEEE80211_AC_BK:
3846 value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
3847 WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
3848 break;
3849 }
3850
3851 if (enable)
3852 arvif->u.sta.uapsd |= value;
3853 else
3854 arvif->u.sta.uapsd &= ~value;
3855
3856 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3857 WMI_STA_PS_PARAM_UAPSD,
3858 arvif->u.sta.uapsd);
3859 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003860 ath10k_warn(ar, "failed to set uapsd params: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003861 goto exit;
3862 }
3863
3864 if (arvif->u.sta.uapsd)
3865 value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
3866 else
3867 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
3868
3869 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3870 WMI_STA_PS_PARAM_RX_WAKE_POLICY,
3871 value);
3872 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003873 ath10k_warn(ar, "failed to set rx wake param: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003874
Michal Kazior9f9b5742014-12-12 12:41:36 +01003875 ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
3876 if (ret) {
3877 ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
3878 arvif->vdev_id, ret);
3879 return ret;
3880 }
3881
3882 ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
3883 if (ret) {
3884 ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
3885 arvif->vdev_id, ret);
3886 return ret;
3887 }
3888
Kalle Valo5e3dd152013-06-12 20:52:10 +03003889exit:
3890 return ret;
3891}
3892
3893static int ath10k_conf_tx(struct ieee80211_hw *hw,
3894 struct ieee80211_vif *vif, u16 ac,
3895 const struct ieee80211_tx_queue_params *params)
3896{
3897 struct ath10k *ar = hw->priv;
3898 struct wmi_wmm_params_arg *p = NULL;
3899 int ret;
3900
3901 mutex_lock(&ar->conf_mutex);
3902
3903 switch (ac) {
3904 case IEEE80211_AC_VO:
3905 p = &ar->wmm_params.ac_vo;
3906 break;
3907 case IEEE80211_AC_VI:
3908 p = &ar->wmm_params.ac_vi;
3909 break;
3910 case IEEE80211_AC_BE:
3911 p = &ar->wmm_params.ac_be;
3912 break;
3913 case IEEE80211_AC_BK:
3914 p = &ar->wmm_params.ac_bk;
3915 break;
3916 }
3917
3918 if (WARN_ON(!p)) {
3919 ret = -EINVAL;
3920 goto exit;
3921 }
3922
3923 p->cwmin = params->cw_min;
3924 p->cwmax = params->cw_max;
3925 p->aifs = params->aifs;
3926
3927 /*
3928 * The channel time duration programmed in the HW is in absolute
3929 * microseconds, while mac80211 gives the txop in units of
3930 * 32 microseconds.
3931 */
3932 p->txop = params->txop * 32;
3933
3934 /* FIXME: FW accepts wmm params per hw, not per vif */
3935 ret = ath10k_wmi_pdev_set_wmm_params(ar, &ar->wmm_params);
3936 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003937 ath10k_warn(ar, "failed to set wmm params: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003938 goto exit;
3939 }
3940
3941 ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
3942 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003943 ath10k_warn(ar, "failed to set sta uapsd: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003944
3945exit:
3946 mutex_unlock(&ar->conf_mutex);
3947 return ret;
3948}
3949
3950#define ATH10K_ROC_TIMEOUT_HZ (2*HZ)
3951
3952static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
3953 struct ieee80211_vif *vif,
3954 struct ieee80211_channel *chan,
3955 int duration,
3956 enum ieee80211_roc_type type)
3957{
3958 struct ath10k *ar = hw->priv;
3959 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3960 struct wmi_start_scan_arg arg;
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003961 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003962
3963 mutex_lock(&ar->conf_mutex);
3964
3965 spin_lock_bh(&ar->data_lock);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003966 switch (ar->scan.state) {
3967 case ATH10K_SCAN_IDLE:
3968 reinit_completion(&ar->scan.started);
3969 reinit_completion(&ar->scan.completed);
3970 reinit_completion(&ar->scan.on_channel);
3971 ar->scan.state = ATH10K_SCAN_STARTING;
3972 ar->scan.is_roc = true;
3973 ar->scan.vdev_id = arvif->vdev_id;
3974 ar->scan.roc_freq = chan->center_freq;
3975 ret = 0;
3976 break;
3977 case ATH10K_SCAN_STARTING:
3978 case ATH10K_SCAN_RUNNING:
3979 case ATH10K_SCAN_ABORTING:
Kalle Valo5e3dd152013-06-12 20:52:10 +03003980 ret = -EBUSY;
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003981 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003982 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003983 spin_unlock_bh(&ar->data_lock);
3984
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003985 if (ret)
3986 goto exit;
3987
Michal Kaziordcca0bd2014-11-24 14:58:32 +01003988 duration = max(duration, WMI_SCAN_CHAN_MIN_TIME_MSEC);
3989
Kalle Valo5e3dd152013-06-12 20:52:10 +03003990 memset(&arg, 0, sizeof(arg));
3991 ath10k_wmi_start_scan_init(ar, &arg);
3992 arg.vdev_id = arvif->vdev_id;
3993 arg.scan_id = ATH10K_SCAN_ID;
3994 arg.n_channels = 1;
3995 arg.channels[0] = chan->center_freq;
3996 arg.dwell_time_active = duration;
3997 arg.dwell_time_passive = duration;
3998 arg.max_scan_time = 2 * duration;
3999 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
4000 arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
4001
4002 ret = ath10k_start_scan(ar, &arg);
4003 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004004 ath10k_warn(ar, "failed to start roc scan: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004005 spin_lock_bh(&ar->data_lock);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004006 ar->scan.state = ATH10K_SCAN_IDLE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004007 spin_unlock_bh(&ar->data_lock);
4008 goto exit;
4009 }
4010
4011 ret = wait_for_completion_timeout(&ar->scan.on_channel, 3*HZ);
4012 if (ret == 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004013 ath10k_warn(ar, "failed to switch to channel for roc scan\n");
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004014
4015 ret = ath10k_scan_stop(ar);
4016 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004017 ath10k_warn(ar, "failed to stop scan: %d\n", ret);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004018
Kalle Valo5e3dd152013-06-12 20:52:10 +03004019 ret = -ETIMEDOUT;
4020 goto exit;
4021 }
4022
4023 ret = 0;
4024exit:
4025 mutex_unlock(&ar->conf_mutex);
4026 return ret;
4027}
4028
4029static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw)
4030{
4031 struct ath10k *ar = hw->priv;
4032
4033 mutex_lock(&ar->conf_mutex);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02004034 ath10k_scan_abort(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004035 mutex_unlock(&ar->conf_mutex);
4036
Michal Kazior4eb2e162014-10-28 10:23:09 +01004037 cancel_delayed_work_sync(&ar->scan.timeout);
4038
Kalle Valo5e3dd152013-06-12 20:52:10 +03004039 return 0;
4040}
4041
4042/*
4043 * Both RTS and Fragmentation threshold are interface-specific
4044 * in ath10k, but device-specific in mac80211.
4045 */
Kalle Valo5e3dd152013-06-12 20:52:10 +03004046
4047static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
4048{
Kalle Valo5e3dd152013-06-12 20:52:10 +03004049 struct ath10k *ar = hw->priv;
Michal Kaziorad088bf2013-10-16 15:44:46 +03004050 struct ath10k_vif *arvif;
4051 int ret = 0;
Michal Kazior548db542013-07-05 16:15:15 +03004052
Michal Kaziorad088bf2013-10-16 15:44:46 +03004053 mutex_lock(&ar->conf_mutex);
4054 list_for_each_entry(arvif, &ar->arvifs, list) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004055 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
Michal Kaziorad088bf2013-10-16 15:44:46 +03004056 arvif->vdev_id, value);
Kalle Valo60c3daa2013-09-08 17:56:07 +03004057
Michal Kaziorad088bf2013-10-16 15:44:46 +03004058 ret = ath10k_mac_set_rts(arvif, value);
4059 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004060 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
Michal Kaziorad088bf2013-10-16 15:44:46 +03004061 arvif->vdev_id, ret);
4062 break;
4063 }
4064 }
4065 mutex_unlock(&ar->conf_mutex);
4066
4067 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004068}
4069
Emmanuel Grumbach77be2c52014-03-27 11:30:29 +02004070static void ath10k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4071 u32 queues, bool drop)
Kalle Valo5e3dd152013-06-12 20:52:10 +03004072{
4073 struct ath10k *ar = hw->priv;
Michal Kazioraffd3212013-07-16 09:54:35 +02004074 bool skip;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004075 int ret;
4076
4077 /* mac80211 doesn't care if we really xmit queued frames or not
4078 * we'll collect those frames either way if we stop/delete vdevs */
4079 if (drop)
4080 return;
4081
Michal Kazior548db542013-07-05 16:15:15 +03004082 mutex_lock(&ar->conf_mutex);
4083
Michal Kazioraffd3212013-07-16 09:54:35 +02004084 if (ar->state == ATH10K_STATE_WEDGED)
4085 goto skip;
4086
Michal Kazioredb82362013-07-05 16:15:14 +03004087 ret = wait_event_timeout(ar->htt.empty_tx_wq, ({
Kalle Valo5e3dd152013-06-12 20:52:10 +03004088 bool empty;
Michal Kazioraffd3212013-07-16 09:54:35 +02004089
Michal Kazioredb82362013-07-05 16:15:14 +03004090 spin_lock_bh(&ar->htt.tx_lock);
Michal Kazior0945baf2013-09-18 14:43:18 +02004091 empty = (ar->htt.num_pending_tx == 0);
Michal Kazioredb82362013-07-05 16:15:14 +03004092 spin_unlock_bh(&ar->htt.tx_lock);
Michal Kazioraffd3212013-07-16 09:54:35 +02004093
Michal Kazior7962b0d2014-10-28 10:34:38 +01004094 skip = (ar->state == ATH10K_STATE_WEDGED) ||
4095 test_bit(ATH10K_FLAG_CRASH_FLUSH,
4096 &ar->dev_flags);
Michal Kazioraffd3212013-07-16 09:54:35 +02004097
4098 (empty || skip);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004099 }), ATH10K_FLUSH_TIMEOUT_HZ);
Michal Kazioraffd3212013-07-16 09:54:35 +02004100
4101 if (ret <= 0 || skip)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004102 ath10k_warn(ar, "failed to flush transmit queue (skip %i ar-state %i): %i\n",
Ben Greear9ba4c782014-02-25 09:29:57 +02004103 skip, ar->state, ret);
Michal Kazior548db542013-07-05 16:15:15 +03004104
Michal Kazioraffd3212013-07-16 09:54:35 +02004105skip:
Michal Kazior548db542013-07-05 16:15:15 +03004106 mutex_unlock(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004107}
4108
4109/* TODO: Implement this function properly
4110 * For now it is needed to reply to Probe Requests in IBSS mode.
4111 * Propably we need this information from FW.
4112 */
4113static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
4114{
4115 return 1;
4116}
4117
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004118#ifdef CONFIG_PM
4119static int ath10k_suspend(struct ieee80211_hw *hw,
4120 struct cfg80211_wowlan *wowlan)
4121{
4122 struct ath10k *ar = hw->priv;
4123 int ret;
4124
Marek Puzyniak9042e172014-02-10 17:14:23 +01004125 mutex_lock(&ar->conf_mutex);
4126
Marek Puzyniak00f54822014-02-10 17:14:24 +01004127 ret = ath10k_wait_for_suspend(ar, WMI_PDEV_SUSPEND);
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004128 if (ret) {
Marek Puzyniak00f54822014-02-10 17:14:24 +01004129 if (ret == -ETIMEDOUT)
4130 goto resume;
Marek Puzyniak9042e172014-02-10 17:14:23 +01004131 ret = 1;
4132 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004133 }
4134
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004135 ret = ath10k_hif_suspend(ar);
4136 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004137 ath10k_warn(ar, "failed to suspend hif: %d\n", ret);
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004138 goto resume;
4139 }
4140
Marek Puzyniak9042e172014-02-10 17:14:23 +01004141 ret = 0;
4142 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004143resume:
4144 ret = ath10k_wmi_pdev_resume_target(ar);
4145 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004146 ath10k_warn(ar, "failed to resume target: %d\n", ret);
Marek Puzyniak9042e172014-02-10 17:14:23 +01004147
4148 ret = 1;
4149exit:
4150 mutex_unlock(&ar->conf_mutex);
4151 return ret;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004152}
4153
4154static int ath10k_resume(struct ieee80211_hw *hw)
4155{
4156 struct ath10k *ar = hw->priv;
4157 int ret;
4158
Marek Puzyniak9042e172014-02-10 17:14:23 +01004159 mutex_lock(&ar->conf_mutex);
4160
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004161 ret = ath10k_hif_resume(ar);
4162 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004163 ath10k_warn(ar, "failed to resume hif: %d\n", ret);
Marek Puzyniak9042e172014-02-10 17:14:23 +01004164 ret = 1;
4165 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004166 }
4167
4168 ret = ath10k_wmi_pdev_resume_target(ar);
4169 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004170 ath10k_warn(ar, "failed to resume target: %d\n", ret);
Marek Puzyniak9042e172014-02-10 17:14:23 +01004171 ret = 1;
4172 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004173 }
4174
Marek Puzyniak9042e172014-02-10 17:14:23 +01004175 ret = 0;
4176exit:
4177 mutex_unlock(&ar->conf_mutex);
4178 return ret;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004179}
4180#endif
4181
Eliad Pellercf2c92d2014-11-04 11:43:54 +02004182static void ath10k_reconfig_complete(struct ieee80211_hw *hw,
4183 enum ieee80211_reconfig_type reconfig_type)
Michal Kazioraffd3212013-07-16 09:54:35 +02004184{
4185 struct ath10k *ar = hw->priv;
4186
Eliad Pellercf2c92d2014-11-04 11:43:54 +02004187 if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
4188 return;
4189
Michal Kazioraffd3212013-07-16 09:54:35 +02004190 mutex_lock(&ar->conf_mutex);
4191
4192 /* If device failed to restart it will be in a different state, e.g.
4193 * ATH10K_STATE_WEDGED */
4194 if (ar->state == ATH10K_STATE_RESTARTED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004195 ath10k_info(ar, "device successfully recovered\n");
Michal Kazioraffd3212013-07-16 09:54:35 +02004196 ar->state = ATH10K_STATE_ON;
Michal Kazior7962b0d2014-10-28 10:34:38 +01004197 ieee80211_wake_queues(ar->hw);
Michal Kazioraffd3212013-07-16 09:54:35 +02004198 }
4199
4200 mutex_unlock(&ar->conf_mutex);
4201}
4202
Michal Kazior2e1dea42013-07-31 10:32:40 +02004203static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
4204 struct survey_info *survey)
4205{
4206 struct ath10k *ar = hw->priv;
4207 struct ieee80211_supported_band *sband;
4208 struct survey_info *ar_survey = &ar->survey[idx];
4209 int ret = 0;
4210
4211 mutex_lock(&ar->conf_mutex);
4212
4213 sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
4214 if (sband && idx >= sband->n_channels) {
4215 idx -= sband->n_channels;
4216 sband = NULL;
4217 }
4218
4219 if (!sband)
4220 sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
4221
4222 if (!sband || idx >= sband->n_channels) {
4223 ret = -ENOENT;
4224 goto exit;
4225 }
4226
4227 spin_lock_bh(&ar->data_lock);
4228 memcpy(survey, ar_survey, sizeof(*survey));
4229 spin_unlock_bh(&ar->data_lock);
4230
4231 survey->channel = &sband->channels[idx];
4232
Felix Fietkaufa1d4df2014-10-23 17:04:28 +03004233 if (ar->rx_channel == survey->channel)
4234 survey->filled |= SURVEY_INFO_IN_USE;
4235
Michal Kazior2e1dea42013-07-31 10:32:40 +02004236exit:
4237 mutex_unlock(&ar->conf_mutex);
4238 return ret;
4239}
4240
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004241/* Helper table for legacy fixed_rate/bitrate_mask */
4242static const u8 cck_ofdm_rate[] = {
4243 /* CCK */
4244 3, /* 1Mbps */
4245 2, /* 2Mbps */
4246 1, /* 5.5Mbps */
4247 0, /* 11Mbps */
4248 /* OFDM */
4249 3, /* 6Mbps */
4250 7, /* 9Mbps */
4251 2, /* 12Mbps */
4252 6, /* 18Mbps */
4253 1, /* 24Mbps */
4254 5, /* 36Mbps */
4255 0, /* 48Mbps */
4256 4, /* 54Mbps */
4257};
4258
4259/* Check if only one bit set */
4260static int ath10k_check_single_mask(u32 mask)
4261{
4262 int bit;
4263
4264 bit = ffs(mask);
4265 if (!bit)
4266 return 0;
4267
4268 mask &= ~BIT(bit - 1);
4269 if (mask)
4270 return 2;
4271
4272 return 1;
4273}
4274
4275static bool
4276ath10k_default_bitrate_mask(struct ath10k *ar,
4277 enum ieee80211_band band,
4278 const struct cfg80211_bitrate_mask *mask)
4279{
4280 u32 legacy = 0x00ff;
4281 u8 ht = 0xff, i;
4282 u16 vht = 0x3ff;
Ben Greearb116ea12014-11-24 16:22:10 +02004283 u16 nrf = ar->num_rf_chains;
4284
4285 if (ar->cfg_tx_chainmask)
4286 nrf = get_nss_from_chainmask(ar->cfg_tx_chainmask);
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004287
4288 switch (band) {
4289 case IEEE80211_BAND_2GHZ:
4290 legacy = 0x00fff;
4291 vht = 0;
4292 break;
4293 case IEEE80211_BAND_5GHZ:
4294 break;
4295 default:
4296 return false;
4297 }
4298
4299 if (mask->control[band].legacy != legacy)
4300 return false;
4301
Ben Greearb116ea12014-11-24 16:22:10 +02004302 for (i = 0; i < nrf; i++)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004303 if (mask->control[band].ht_mcs[i] != ht)
4304 return false;
4305
Ben Greearb116ea12014-11-24 16:22:10 +02004306 for (i = 0; i < nrf; i++)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004307 if (mask->control[band].vht_mcs[i] != vht)
4308 return false;
4309
4310 return true;
4311}
4312
4313static bool
4314ath10k_bitrate_mask_nss(const struct cfg80211_bitrate_mask *mask,
4315 enum ieee80211_band band,
4316 u8 *fixed_nss)
4317{
4318 int ht_nss = 0, vht_nss = 0, i;
4319
4320 /* check legacy */
4321 if (ath10k_check_single_mask(mask->control[band].legacy))
4322 return false;
4323
4324 /* check HT */
4325 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
4326 if (mask->control[band].ht_mcs[i] == 0xff)
4327 continue;
4328 else if (mask->control[band].ht_mcs[i] == 0x00)
4329 break;
Kalle Valod8bb26b2014-09-14 12:50:33 +03004330
4331 return false;
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004332 }
4333
4334 ht_nss = i;
4335
4336 /* check VHT */
4337 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
4338 if (mask->control[band].vht_mcs[i] == 0x03ff)
4339 continue;
4340 else if (mask->control[band].vht_mcs[i] == 0x0000)
4341 break;
Kalle Valod8bb26b2014-09-14 12:50:33 +03004342
4343 return false;
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004344 }
4345
4346 vht_nss = i;
4347
4348 if (ht_nss > 0 && vht_nss > 0)
4349 return false;
4350
4351 if (ht_nss)
4352 *fixed_nss = ht_nss;
4353 else if (vht_nss)
4354 *fixed_nss = vht_nss;
4355 else
4356 return false;
4357
4358 return true;
4359}
4360
4361static bool
4362ath10k_bitrate_mask_correct(const struct cfg80211_bitrate_mask *mask,
4363 enum ieee80211_band band,
4364 enum wmi_rate_preamble *preamble)
4365{
4366 int legacy = 0, ht = 0, vht = 0, i;
4367
4368 *preamble = WMI_RATE_PREAMBLE_OFDM;
4369
4370 /* check legacy */
4371 legacy = ath10k_check_single_mask(mask->control[band].legacy);
4372 if (legacy > 1)
4373 return false;
4374
4375 /* check HT */
4376 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
4377 ht += ath10k_check_single_mask(mask->control[band].ht_mcs[i]);
4378 if (ht > 1)
4379 return false;
4380
4381 /* check VHT */
4382 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
4383 vht += ath10k_check_single_mask(mask->control[band].vht_mcs[i]);
4384 if (vht > 1)
4385 return false;
4386
4387 /* Currently we support only one fixed_rate */
4388 if ((legacy + ht + vht) != 1)
4389 return false;
4390
4391 if (ht)
4392 *preamble = WMI_RATE_PREAMBLE_HT;
4393 else if (vht)
4394 *preamble = WMI_RATE_PREAMBLE_VHT;
4395
4396 return true;
4397}
4398
4399static bool
Michal Kazior7aa7a722014-08-25 12:09:38 +02004400ath10k_bitrate_mask_rate(struct ath10k *ar,
4401 const struct cfg80211_bitrate_mask *mask,
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004402 enum ieee80211_band band,
4403 u8 *fixed_rate,
4404 u8 *fixed_nss)
4405{
4406 u8 rate = 0, pream = 0, nss = 0, i;
4407 enum wmi_rate_preamble preamble;
4408
4409 /* Check if single rate correct */
4410 if (!ath10k_bitrate_mask_correct(mask, band, &preamble))
4411 return false;
4412
4413 pream = preamble;
4414
4415 switch (preamble) {
4416 case WMI_RATE_PREAMBLE_CCK:
4417 case WMI_RATE_PREAMBLE_OFDM:
4418 i = ffs(mask->control[band].legacy) - 1;
4419
4420 if (band == IEEE80211_BAND_2GHZ && i < 4)
4421 pream = WMI_RATE_PREAMBLE_CCK;
4422
4423 if (band == IEEE80211_BAND_5GHZ)
4424 i += 4;
4425
4426 if (i >= ARRAY_SIZE(cck_ofdm_rate))
4427 return false;
4428
4429 rate = cck_ofdm_rate[i];
4430 break;
4431 case WMI_RATE_PREAMBLE_HT:
4432 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
4433 if (mask->control[band].ht_mcs[i])
4434 break;
4435
4436 if (i == IEEE80211_HT_MCS_MASK_LEN)
4437 return false;
4438
4439 rate = ffs(mask->control[band].ht_mcs[i]) - 1;
4440 nss = i;
4441 break;
4442 case WMI_RATE_PREAMBLE_VHT:
4443 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
4444 if (mask->control[band].vht_mcs[i])
4445 break;
4446
4447 if (i == NL80211_VHT_NSS_MAX)
4448 return false;
4449
4450 rate = ffs(mask->control[band].vht_mcs[i]) - 1;
4451 nss = i;
4452 break;
4453 }
4454
4455 *fixed_nss = nss + 1;
4456 nss <<= 4;
4457 pream <<= 6;
4458
Michal Kazior7aa7a722014-08-25 12:09:38 +02004459 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 +01004460 pream, nss, rate);
4461
4462 *fixed_rate = pream | nss | rate;
4463
4464 return true;
4465}
4466
Michal Kazior7aa7a722014-08-25 12:09:38 +02004467static bool ath10k_get_fixed_rate_nss(struct ath10k *ar,
4468 const struct cfg80211_bitrate_mask *mask,
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004469 enum ieee80211_band band,
4470 u8 *fixed_rate,
4471 u8 *fixed_nss)
4472{
4473 /* First check full NSS mask, if we can simply limit NSS */
4474 if (ath10k_bitrate_mask_nss(mask, band, fixed_nss))
4475 return true;
4476
4477 /* Next Check single rate is set */
Michal Kazior7aa7a722014-08-25 12:09:38 +02004478 return ath10k_bitrate_mask_rate(ar, mask, band, fixed_rate, fixed_nss);
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004479}
4480
4481static int ath10k_set_fixed_rate_param(struct ath10k_vif *arvif,
4482 u8 fixed_rate,
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004483 u8 fixed_nss,
4484 u8 force_sgi)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004485{
4486 struct ath10k *ar = arvif->ar;
4487 u32 vdev_param;
4488 int ret = 0;
4489
4490 mutex_lock(&ar->conf_mutex);
4491
4492 if (arvif->fixed_rate == fixed_rate &&
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004493 arvif->fixed_nss == fixed_nss &&
4494 arvif->force_sgi == force_sgi)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004495 goto exit;
4496
4497 if (fixed_rate == WMI_FIXED_RATE_NONE)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004498 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac disable fixed bitrate mask\n");
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004499
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004500 if (force_sgi)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004501 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac force sgi\n");
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004502
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004503 vdev_param = ar->wmi.vdev_param->fixed_rate;
4504 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
4505 vdev_param, fixed_rate);
4506 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004507 ath10k_warn(ar, "failed to set fixed rate param 0x%02x: %d\n",
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004508 fixed_rate, ret);
4509 ret = -EINVAL;
4510 goto exit;
4511 }
4512
4513 arvif->fixed_rate = fixed_rate;
4514
4515 vdev_param = ar->wmi.vdev_param->nss;
4516 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
4517 vdev_param, fixed_nss);
4518
4519 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004520 ath10k_warn(ar, "failed to set fixed nss param %d: %d\n",
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004521 fixed_nss, ret);
4522 ret = -EINVAL;
4523 goto exit;
4524 }
4525
4526 arvif->fixed_nss = fixed_nss;
4527
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004528 vdev_param = ar->wmi.vdev_param->sgi;
4529 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
4530 force_sgi);
4531
4532 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004533 ath10k_warn(ar, "failed to set sgi param %d: %d\n",
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004534 force_sgi, ret);
4535 ret = -EINVAL;
4536 goto exit;
4537 }
4538
4539 arvif->force_sgi = force_sgi;
4540
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004541exit:
4542 mutex_unlock(&ar->conf_mutex);
4543 return ret;
4544}
4545
4546static int ath10k_set_bitrate_mask(struct ieee80211_hw *hw,
4547 struct ieee80211_vif *vif,
4548 const struct cfg80211_bitrate_mask *mask)
4549{
4550 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4551 struct ath10k *ar = arvif->ar;
4552 enum ieee80211_band band = ar->hw->conf.chandef.chan->band;
4553 u8 fixed_rate = WMI_FIXED_RATE_NONE;
4554 u8 fixed_nss = ar->num_rf_chains;
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004555 u8 force_sgi;
4556
Ben Greearb116ea12014-11-24 16:22:10 +02004557 if (ar->cfg_tx_chainmask)
4558 fixed_nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
4559
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004560 force_sgi = mask->control[band].gi;
4561 if (force_sgi == NL80211_TXRATE_FORCE_LGI)
4562 return -EINVAL;
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004563
4564 if (!ath10k_default_bitrate_mask(ar, band, mask)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004565 if (!ath10k_get_fixed_rate_nss(ar, mask, band,
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004566 &fixed_rate,
4567 &fixed_nss))
4568 return -EINVAL;
4569 }
4570
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004571 if (fixed_rate == WMI_FIXED_RATE_NONE && force_sgi) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004572 ath10k_warn(ar, "failed to force SGI usage for default rate settings\n");
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004573 return -EINVAL;
4574 }
4575
4576 return ath10k_set_fixed_rate_param(arvif, fixed_rate,
4577 fixed_nss, force_sgi);
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004578}
4579
Michal Kazior9797feb2014-02-14 14:49:48 +01004580static void ath10k_sta_rc_update(struct ieee80211_hw *hw,
4581 struct ieee80211_vif *vif,
4582 struct ieee80211_sta *sta,
4583 u32 changed)
4584{
4585 struct ath10k *ar = hw->priv;
4586 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
4587 u32 bw, smps;
4588
4589 spin_lock_bh(&ar->data_lock);
4590
Michal Kazior7aa7a722014-08-25 12:09:38 +02004591 ath10k_dbg(ar, ATH10K_DBG_MAC,
Michal Kazior9797feb2014-02-14 14:49:48 +01004592 "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
4593 sta->addr, changed, sta->bandwidth, sta->rx_nss,
4594 sta->smps_mode);
4595
4596 if (changed & IEEE80211_RC_BW_CHANGED) {
4597 bw = WMI_PEER_CHWIDTH_20MHZ;
4598
4599 switch (sta->bandwidth) {
4600 case IEEE80211_STA_RX_BW_20:
4601 bw = WMI_PEER_CHWIDTH_20MHZ;
4602 break;
4603 case IEEE80211_STA_RX_BW_40:
4604 bw = WMI_PEER_CHWIDTH_40MHZ;
4605 break;
4606 case IEEE80211_STA_RX_BW_80:
4607 bw = WMI_PEER_CHWIDTH_80MHZ;
4608 break;
4609 case IEEE80211_STA_RX_BW_160:
Michal Kazior7aa7a722014-08-25 12:09:38 +02004610 ath10k_warn(ar, "Invalid bandwith %d in rc update for %pM\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02004611 sta->bandwidth, sta->addr);
Michal Kazior9797feb2014-02-14 14:49:48 +01004612 bw = WMI_PEER_CHWIDTH_20MHZ;
4613 break;
4614 }
4615
4616 arsta->bw = bw;
4617 }
4618
4619 if (changed & IEEE80211_RC_NSS_CHANGED)
4620 arsta->nss = sta->rx_nss;
4621
4622 if (changed & IEEE80211_RC_SMPS_CHANGED) {
4623 smps = WMI_PEER_SMPS_PS_NONE;
4624
4625 switch (sta->smps_mode) {
4626 case IEEE80211_SMPS_AUTOMATIC:
4627 case IEEE80211_SMPS_OFF:
4628 smps = WMI_PEER_SMPS_PS_NONE;
4629 break;
4630 case IEEE80211_SMPS_STATIC:
4631 smps = WMI_PEER_SMPS_STATIC;
4632 break;
4633 case IEEE80211_SMPS_DYNAMIC:
4634 smps = WMI_PEER_SMPS_DYNAMIC;
4635 break;
4636 case IEEE80211_SMPS_NUM_MODES:
Michal Kazior7aa7a722014-08-25 12:09:38 +02004637 ath10k_warn(ar, "Invalid smps %d in sta rc update for %pM\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02004638 sta->smps_mode, sta->addr);
Michal Kazior9797feb2014-02-14 14:49:48 +01004639 smps = WMI_PEER_SMPS_PS_NONE;
4640 break;
4641 }
4642
4643 arsta->smps = smps;
4644 }
4645
Michal Kazior9797feb2014-02-14 14:49:48 +01004646 arsta->changed |= changed;
4647
4648 spin_unlock_bh(&ar->data_lock);
4649
4650 ieee80211_queue_work(hw, &arsta->update_wk);
4651}
4652
Chun-Yeow Yeoh26ebbcc2014-02-25 09:29:54 +02004653static u64 ath10k_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
4654{
4655 /*
4656 * FIXME: Return 0 for time being. Need to figure out whether FW
4657 * has the API to fetch 64-bit local TSF
4658 */
4659
4660 return 0;
4661}
4662
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02004663static int ath10k_ampdu_action(struct ieee80211_hw *hw,
4664 struct ieee80211_vif *vif,
4665 enum ieee80211_ampdu_mlme_action action,
4666 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
4667 u8 buf_size)
4668{
Michal Kazior7aa7a722014-08-25 12:09:38 +02004669 struct ath10k *ar = hw->priv;
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02004670 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4671
Michal Kazior7aa7a722014-08-25 12:09:38 +02004672 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 +02004673 arvif->vdev_id, sta->addr, tid, action);
4674
4675 switch (action) {
4676 case IEEE80211_AMPDU_RX_START:
4677 case IEEE80211_AMPDU_RX_STOP:
4678 /* HTT AddBa/DelBa events trigger mac80211 Rx BA session
4679 * creation/removal. Do we need to verify this?
4680 */
4681 return 0;
4682 case IEEE80211_AMPDU_TX_START:
4683 case IEEE80211_AMPDU_TX_STOP_CONT:
4684 case IEEE80211_AMPDU_TX_STOP_FLUSH:
4685 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
4686 case IEEE80211_AMPDU_TX_OPERATIONAL:
4687 /* Firmware offloads Tx aggregation entirely so deny mac80211
4688 * Tx aggregation requests.
4689 */
4690 return -EOPNOTSUPP;
4691 }
4692
4693 return -EINVAL;
4694}
4695
Kalle Valo5e3dd152013-06-12 20:52:10 +03004696static const struct ieee80211_ops ath10k_ops = {
4697 .tx = ath10k_tx,
4698 .start = ath10k_start,
4699 .stop = ath10k_stop,
4700 .config = ath10k_config,
4701 .add_interface = ath10k_add_interface,
4702 .remove_interface = ath10k_remove_interface,
4703 .configure_filter = ath10k_configure_filter,
4704 .bss_info_changed = ath10k_bss_info_changed,
4705 .hw_scan = ath10k_hw_scan,
4706 .cancel_hw_scan = ath10k_cancel_hw_scan,
4707 .set_key = ath10k_set_key,
4708 .sta_state = ath10k_sta_state,
4709 .conf_tx = ath10k_conf_tx,
4710 .remain_on_channel = ath10k_remain_on_channel,
4711 .cancel_remain_on_channel = ath10k_cancel_remain_on_channel,
4712 .set_rts_threshold = ath10k_set_rts_threshold,
Kalle Valo5e3dd152013-06-12 20:52:10 +03004713 .flush = ath10k_flush,
4714 .tx_last_beacon = ath10k_tx_last_beacon,
Ben Greear46acf7bb2014-05-16 17:15:38 +03004715 .set_antenna = ath10k_set_antenna,
4716 .get_antenna = ath10k_get_antenna,
Eliad Pellercf2c92d2014-11-04 11:43:54 +02004717 .reconfig_complete = ath10k_reconfig_complete,
Michal Kazior2e1dea42013-07-31 10:32:40 +02004718 .get_survey = ath10k_get_survey,
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004719 .set_bitrate_mask = ath10k_set_bitrate_mask,
Michal Kazior9797feb2014-02-14 14:49:48 +01004720 .sta_rc_update = ath10k_sta_rc_update,
Chun-Yeow Yeoh26ebbcc2014-02-25 09:29:54 +02004721 .get_tsf = ath10k_get_tsf,
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02004722 .ampdu_action = ath10k_ampdu_action,
Ben Greear6cddcc72014-09-29 14:41:46 +03004723 .get_et_sset_count = ath10k_debug_get_et_sset_count,
4724 .get_et_stats = ath10k_debug_get_et_stats,
4725 .get_et_strings = ath10k_debug_get_et_strings,
Kalle Valo43d2a302014-09-10 18:23:30 +03004726
4727 CFG80211_TESTMODE_CMD(ath10k_tm_cmd)
4728
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004729#ifdef CONFIG_PM
4730 .suspend = ath10k_suspend,
4731 .resume = ath10k_resume,
4732#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03004733};
4734
4735#define RATETAB_ENT(_rate, _rateid, _flags) { \
4736 .bitrate = (_rate), \
4737 .flags = (_flags), \
4738 .hw_value = (_rateid), \
4739}
4740
4741#define CHAN2G(_channel, _freq, _flags) { \
4742 .band = IEEE80211_BAND_2GHZ, \
4743 .hw_value = (_channel), \
4744 .center_freq = (_freq), \
4745 .flags = (_flags), \
4746 .max_antenna_gain = 0, \
4747 .max_power = 30, \
4748}
4749
4750#define CHAN5G(_channel, _freq, _flags) { \
4751 .band = IEEE80211_BAND_5GHZ, \
4752 .hw_value = (_channel), \
4753 .center_freq = (_freq), \
4754 .flags = (_flags), \
4755 .max_antenna_gain = 0, \
4756 .max_power = 30, \
4757}
4758
4759static const struct ieee80211_channel ath10k_2ghz_channels[] = {
4760 CHAN2G(1, 2412, 0),
4761 CHAN2G(2, 2417, 0),
4762 CHAN2G(3, 2422, 0),
4763 CHAN2G(4, 2427, 0),
4764 CHAN2G(5, 2432, 0),
4765 CHAN2G(6, 2437, 0),
4766 CHAN2G(7, 2442, 0),
4767 CHAN2G(8, 2447, 0),
4768 CHAN2G(9, 2452, 0),
4769 CHAN2G(10, 2457, 0),
4770 CHAN2G(11, 2462, 0),
4771 CHAN2G(12, 2467, 0),
4772 CHAN2G(13, 2472, 0),
4773 CHAN2G(14, 2484, 0),
4774};
4775
4776static const struct ieee80211_channel ath10k_5ghz_channels[] = {
Michal Kazior429ff562013-06-26 08:54:54 +02004777 CHAN5G(36, 5180, 0),
4778 CHAN5G(40, 5200, 0),
4779 CHAN5G(44, 5220, 0),
4780 CHAN5G(48, 5240, 0),
4781 CHAN5G(52, 5260, 0),
4782 CHAN5G(56, 5280, 0),
4783 CHAN5G(60, 5300, 0),
4784 CHAN5G(64, 5320, 0),
4785 CHAN5G(100, 5500, 0),
4786 CHAN5G(104, 5520, 0),
4787 CHAN5G(108, 5540, 0),
4788 CHAN5G(112, 5560, 0),
4789 CHAN5G(116, 5580, 0),
4790 CHAN5G(120, 5600, 0),
4791 CHAN5G(124, 5620, 0),
4792 CHAN5G(128, 5640, 0),
4793 CHAN5G(132, 5660, 0),
4794 CHAN5G(136, 5680, 0),
4795 CHAN5G(140, 5700, 0),
4796 CHAN5G(149, 5745, 0),
4797 CHAN5G(153, 5765, 0),
4798 CHAN5G(157, 5785, 0),
4799 CHAN5G(161, 5805, 0),
4800 CHAN5G(165, 5825, 0),
Kalle Valo5e3dd152013-06-12 20:52:10 +03004801};
4802
Michal Kazior91b12082014-12-12 12:41:35 +01004803/* Note: Be careful if you re-order these. There is code which depends on this
4804 * ordering.
4805 */
Kalle Valo5e3dd152013-06-12 20:52:10 +03004806static struct ieee80211_rate ath10k_rates[] = {
4807 /* CCK */
4808 RATETAB_ENT(10, 0x82, 0),
4809 RATETAB_ENT(20, 0x84, 0),
4810 RATETAB_ENT(55, 0x8b, 0),
4811 RATETAB_ENT(110, 0x96, 0),
4812 /* OFDM */
4813 RATETAB_ENT(60, 0x0c, 0),
4814 RATETAB_ENT(90, 0x12, 0),
4815 RATETAB_ENT(120, 0x18, 0),
4816 RATETAB_ENT(180, 0x24, 0),
4817 RATETAB_ENT(240, 0x30, 0),
4818 RATETAB_ENT(360, 0x48, 0),
4819 RATETAB_ENT(480, 0x60, 0),
4820 RATETAB_ENT(540, 0x6c, 0),
4821};
4822
4823#define ath10k_a_rates (ath10k_rates + 4)
4824#define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - 4)
4825#define ath10k_g_rates (ath10k_rates + 0)
4826#define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
4827
Michal Kaziore7b54192014-08-07 11:03:27 +02004828struct ath10k *ath10k_mac_create(size_t priv_size)
Kalle Valo5e3dd152013-06-12 20:52:10 +03004829{
4830 struct ieee80211_hw *hw;
4831 struct ath10k *ar;
4832
Michal Kaziore7b54192014-08-07 11:03:27 +02004833 hw = ieee80211_alloc_hw(sizeof(struct ath10k) + priv_size, &ath10k_ops);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004834 if (!hw)
4835 return NULL;
4836
4837 ar = hw->priv;
4838 ar->hw = hw;
4839
4840 return ar;
4841}
4842
4843void ath10k_mac_destroy(struct ath10k *ar)
4844{
4845 ieee80211_free_hw(ar->hw);
4846}
4847
4848static const struct ieee80211_iface_limit ath10k_if_limits[] = {
4849 {
4850 .max = 8,
4851 .types = BIT(NL80211_IFTYPE_STATION)
4852 | BIT(NL80211_IFTYPE_P2P_CLIENT)
Michal Kaziord531cb82013-07-31 10:55:13 +02004853 },
4854 {
4855 .max = 3,
4856 .types = BIT(NL80211_IFTYPE_P2P_GO)
4857 },
4858 {
4859 .max = 7,
4860 .types = BIT(NL80211_IFTYPE_AP)
4861 },
Kalle Valo5e3dd152013-06-12 20:52:10 +03004862};
4863
Bartosz Markowskif2595092013-12-10 16:20:39 +01004864static const struct ieee80211_iface_limit ath10k_10x_if_limits[] = {
Marek Puzyniake8a50f82013-11-20 09:59:47 +02004865 {
4866 .max = 8,
4867 .types = BIT(NL80211_IFTYPE_AP)
4868 },
4869};
Marek Puzyniake8a50f82013-11-20 09:59:47 +02004870
4871static const struct ieee80211_iface_combination ath10k_if_comb[] = {
4872 {
4873 .limits = ath10k_if_limits,
4874 .n_limits = ARRAY_SIZE(ath10k_if_limits),
4875 .max_interfaces = 8,
4876 .num_different_channels = 1,
4877 .beacon_int_infra_match = true,
4878 },
Bartosz Markowskif2595092013-12-10 16:20:39 +01004879};
4880
4881static const struct ieee80211_iface_combination ath10k_10x_if_comb[] = {
Marek Puzyniake8a50f82013-11-20 09:59:47 +02004882 {
Bartosz Markowskif2595092013-12-10 16:20:39 +01004883 .limits = ath10k_10x_if_limits,
4884 .n_limits = ARRAY_SIZE(ath10k_10x_if_limits),
Marek Puzyniake8a50f82013-11-20 09:59:47 +02004885 .max_interfaces = 8,
4886 .num_different_channels = 1,
4887 .beacon_int_infra_match = true,
Bartosz Markowskif2595092013-12-10 16:20:39 +01004888#ifdef CONFIG_ATH10K_DFS_CERTIFIED
Marek Puzyniake8a50f82013-11-20 09:59:47 +02004889 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
4890 BIT(NL80211_CHAN_WIDTH_20) |
4891 BIT(NL80211_CHAN_WIDTH_40) |
4892 BIT(NL80211_CHAN_WIDTH_80),
Marek Puzyniake8a50f82013-11-20 09:59:47 +02004893#endif
Bartosz Markowskif2595092013-12-10 16:20:39 +01004894 },
Kalle Valo5e3dd152013-06-12 20:52:10 +03004895};
4896
4897static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
4898{
4899 struct ieee80211_sta_vht_cap vht_cap = {0};
4900 u16 mcs_map;
Michal Kazior8865bee42013-07-24 12:36:46 +02004901 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004902
4903 vht_cap.vht_supported = 1;
4904 vht_cap.cap = ar->vht_cap_info;
4905
Michal Kazior8865bee42013-07-24 12:36:46 +02004906 mcs_map = 0;
4907 for (i = 0; i < 8; i++) {
4908 if (i < ar->num_rf_chains)
4909 mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i*2);
4910 else
4911 mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i*2);
4912 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03004913
4914 vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
4915 vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
4916
4917 return vht_cap;
4918}
4919
4920static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
4921{
4922 int i;
4923 struct ieee80211_sta_ht_cap ht_cap = {0};
4924
4925 if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
4926 return ht_cap;
4927
4928 ht_cap.ht_supported = 1;
4929 ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
4930 ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
4931 ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
4932 ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
4933 ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
4934
4935 if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
4936 ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
4937
4938 if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
4939 ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
4940
4941 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
4942 u32 smps;
4943
4944 smps = WLAN_HT_CAP_SM_PS_DYNAMIC;
4945 smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
4946
4947 ht_cap.cap |= smps;
4948 }
4949
4950 if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC)
4951 ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
4952
4953 if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
4954 u32 stbc;
4955
4956 stbc = ar->ht_cap_info;
4957 stbc &= WMI_HT_CAP_RX_STBC;
4958 stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
4959 stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
4960 stbc &= IEEE80211_HT_CAP_RX_STBC;
4961
4962 ht_cap.cap |= stbc;
4963 }
4964
4965 if (ar->ht_cap_info & WMI_HT_CAP_LDPC)
4966 ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
4967
4968 if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
4969 ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
4970
4971 /* max AMSDU is implicitly taken from vht_cap_info */
4972 if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
4973 ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
4974
Michal Kazior8865bee42013-07-24 12:36:46 +02004975 for (i = 0; i < ar->num_rf_chains; i++)
Kalle Valo5e3dd152013-06-12 20:52:10 +03004976 ht_cap.mcs.rx_mask[i] = 0xFF;
4977
4978 ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
4979
4980 return ht_cap;
4981}
4982
Kalle Valo5e3dd152013-06-12 20:52:10 +03004983static void ath10k_get_arvif_iter(void *data, u8 *mac,
4984 struct ieee80211_vif *vif)
4985{
4986 struct ath10k_vif_iter *arvif_iter = data;
4987 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4988
4989 if (arvif->vdev_id == arvif_iter->vdev_id)
4990 arvif_iter->arvif = arvif;
4991}
4992
4993struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
4994{
4995 struct ath10k_vif_iter arvif_iter;
4996 u32 flags;
4997
4998 memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
4999 arvif_iter.vdev_id = vdev_id;
5000
5001 flags = IEEE80211_IFACE_ITER_RESUME_ALL;
5002 ieee80211_iterate_active_interfaces_atomic(ar->hw,
5003 flags,
5004 ath10k_get_arvif_iter,
5005 &arvif_iter);
5006 if (!arvif_iter.arvif) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02005007 ath10k_warn(ar, "No VIF found for vdev %d\n", vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03005008 return NULL;
5009 }
5010
5011 return arvif_iter.arvif;
5012}
5013
5014int ath10k_mac_register(struct ath10k *ar)
5015{
5016 struct ieee80211_supported_band *band;
5017 struct ieee80211_sta_vht_cap vht_cap;
5018 struct ieee80211_sta_ht_cap ht_cap;
5019 void *channels;
5020 int ret;
5021
5022 SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
5023
5024 SET_IEEE80211_DEV(ar->hw, ar->dev);
5025
5026 ht_cap = ath10k_get_ht_cap(ar);
5027 vht_cap = ath10k_create_vht_cap(ar);
5028
5029 if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
5030 channels = kmemdup(ath10k_2ghz_channels,
5031 sizeof(ath10k_2ghz_channels),
5032 GFP_KERNEL);
Michal Kaziord6015b22013-07-22 14:13:30 +02005033 if (!channels) {
5034 ret = -ENOMEM;
5035 goto err_free;
5036 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03005037
5038 band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
5039 band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
5040 band->channels = channels;
5041 band->n_bitrates = ath10k_g_rates_size;
5042 band->bitrates = ath10k_g_rates;
5043 band->ht_cap = ht_cap;
5044
5045 /* vht is not supported in 2.4 GHz */
5046
5047 ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
5048 }
5049
5050 if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
5051 channels = kmemdup(ath10k_5ghz_channels,
5052 sizeof(ath10k_5ghz_channels),
5053 GFP_KERNEL);
5054 if (!channels) {
Michal Kaziord6015b22013-07-22 14:13:30 +02005055 ret = -ENOMEM;
5056 goto err_free;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005057 }
5058
5059 band = &ar->mac.sbands[IEEE80211_BAND_5GHZ];
5060 band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
5061 band->channels = channels;
5062 band->n_bitrates = ath10k_a_rates_size;
5063 band->bitrates = ath10k_a_rates;
5064 band->ht_cap = ht_cap;
5065 band->vht_cap = vht_cap;
5066 ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
5067 }
5068
5069 ar->hw->wiphy->interface_modes =
5070 BIT(NL80211_IFTYPE_STATION) |
Bartosz Markowskid3541812013-12-10 16:20:40 +01005071 BIT(NL80211_IFTYPE_AP);
5072
Ben Greear46acf7bb2014-05-16 17:15:38 +03005073 ar->hw->wiphy->available_antennas_rx = ar->supp_rx_chainmask;
5074 ar->hw->wiphy->available_antennas_tx = ar->supp_tx_chainmask;
5075
Bartosz Markowskid3541812013-12-10 16:20:40 +01005076 if (!test_bit(ATH10K_FW_FEATURE_NO_P2P, ar->fw_features))
5077 ar->hw->wiphy->interface_modes |=
5078 BIT(NL80211_IFTYPE_P2P_CLIENT) |
5079 BIT(NL80211_IFTYPE_P2P_GO);
Kalle Valo5e3dd152013-06-12 20:52:10 +03005080
5081 ar->hw->flags = IEEE80211_HW_SIGNAL_DBM |
5082 IEEE80211_HW_SUPPORTS_PS |
5083 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
5084 IEEE80211_HW_SUPPORTS_UAPSD |
5085 IEEE80211_HW_MFP_CAPABLE |
5086 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
5087 IEEE80211_HW_HAS_RATE_CONTROL |
Janusz Dziedzic2f0f1122014-02-26 18:42:09 +02005088 IEEE80211_HW_AP_LINK_PS |
5089 IEEE80211_HW_SPECTRUM_MGMT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005090
Eliad Peller0d8614b2014-09-10 14:07:36 +03005091 ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
5092
Kalle Valo5e3dd152013-06-12 20:52:10 +03005093 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
Eliad Peller0d8614b2014-09-10 14:07:36 +03005094 ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005095
5096 if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
5097 ar->hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
5098 ar->hw->flags |= IEEE80211_HW_TX_AMPDU_SETUP_IN_HW;
5099 }
5100
5101 ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
5102 ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
5103
5104 ar->hw->vif_data_size = sizeof(struct ath10k_vif);
Michal Kazior9797feb2014-02-14 14:49:48 +01005105 ar->hw->sta_data_size = sizeof(struct ath10k_sta);
Kalle Valo5e3dd152013-06-12 20:52:10 +03005106
Kalle Valo5e3dd152013-06-12 20:52:10 +03005107 ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
5108
5109 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
Michal Kaziorc2df44b2014-01-23 11:38:26 +01005110 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005111 ar->hw->wiphy->max_remain_on_channel_duration = 5000;
5112
5113 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
Rajkumar Manoharan78157a12014-11-17 16:44:15 +02005114 ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE;
5115
Kalle Valo5e3dd152013-06-12 20:52:10 +03005116 /*
5117 * on LL hardware queues are managed entirely by the FW
5118 * so we only advertise to mac we can do the queues thing
5119 */
5120 ar->hw->queues = 4;
5121
Bartosz Markowskif2595092013-12-10 16:20:39 +01005122 if (test_bit(ATH10K_FW_FEATURE_WMI_10X, ar->fw_features)) {
5123 ar->hw->wiphy->iface_combinations = ath10k_10x_if_comb;
5124 ar->hw->wiphy->n_iface_combinations =
5125 ARRAY_SIZE(ath10k_10x_if_comb);
5126 } else {
5127 ar->hw->wiphy->iface_combinations = ath10k_if_comb;
5128 ar->hw->wiphy->n_iface_combinations =
5129 ARRAY_SIZE(ath10k_if_comb);
Michal Kaziorcf850d12014-07-24 20:07:00 +03005130
5131 ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
Bartosz Markowskif2595092013-12-10 16:20:39 +01005132 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03005133
Michal Kazior7c199992013-07-31 10:47:57 +02005134 ar->hw->netdev_features = NETIF_F_HW_CSUM;
5135
Janusz Dziedzic9702c682013-11-20 09:59:41 +02005136 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED)) {
5137 /* Init ath dfs pattern detector */
5138 ar->ath_common.debug_mask = ATH_DBG_DFS;
5139 ar->dfs_detector = dfs_pattern_detector_init(&ar->ath_common,
5140 NL80211_DFS_UNSET);
5141
5142 if (!ar->dfs_detector)
Michal Kazior7aa7a722014-08-25 12:09:38 +02005143 ath10k_warn(ar, "failed to initialise DFS pattern detector\n");
Janusz Dziedzic9702c682013-11-20 09:59:41 +02005144 }
5145
Kalle Valo5e3dd152013-06-12 20:52:10 +03005146 ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
5147 ath10k_reg_notifier);
5148 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02005149 ath10k_err(ar, "failed to initialise regulatory: %i\n", ret);
Michal Kaziord6015b22013-07-22 14:13:30 +02005150 goto err_free;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005151 }
5152
5153 ret = ieee80211_register_hw(ar->hw);
5154 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02005155 ath10k_err(ar, "failed to register ieee80211: %d\n", ret);
Michal Kaziord6015b22013-07-22 14:13:30 +02005156 goto err_free;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005157 }
5158
5159 if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
5160 ret = regulatory_hint(ar->hw->wiphy,
5161 ar->ath_common.regulatory.alpha2);
5162 if (ret)
Michal Kaziord6015b22013-07-22 14:13:30 +02005163 goto err_unregister;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005164 }
5165
5166 return 0;
Michal Kaziord6015b22013-07-22 14:13:30 +02005167
5168err_unregister:
Kalle Valo5e3dd152013-06-12 20:52:10 +03005169 ieee80211_unregister_hw(ar->hw);
Michal Kaziord6015b22013-07-22 14:13:30 +02005170err_free:
5171 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
5172 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
5173
Kalle Valo5e3dd152013-06-12 20:52:10 +03005174 return ret;
5175}
5176
5177void ath10k_mac_unregister(struct ath10k *ar)
5178{
5179 ieee80211_unregister_hw(ar->hw);
5180
Janusz Dziedzic9702c682013-11-20 09:59:41 +02005181 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
5182 ar->dfs_detector->exit(ar->dfs_detector);
5183
Kalle Valo5e3dd152013-06-12 20:52:10 +03005184 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
5185 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
5186
5187 SET_IEEE80211_DEV(ar->hw, NULL);
5188}