blob: 2619db1e3e7468c05c8232749ca1d8a448a48afb [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"
Kalle Valo5e3dd152013-06-12 20:52:10 +030030
31/**********/
32/* Crypto */
33/**********/
34
35static int ath10k_send_key(struct ath10k_vif *arvif,
36 struct ieee80211_key_conf *key,
37 enum set_key_cmd cmd,
38 const u8 *macaddr)
39{
Michal Kazior7aa7a722014-08-25 12:09:38 +020040 struct ath10k *ar = arvif->ar;
Kalle Valo5e3dd152013-06-12 20:52:10 +030041 struct wmi_vdev_install_key_arg arg = {
42 .vdev_id = arvif->vdev_id,
43 .key_idx = key->keyidx,
44 .key_len = key->keylen,
45 .key_data = key->key,
46 .macaddr = macaddr,
47 };
48
Michal Kazior548db542013-07-05 16:15:15 +030049 lockdep_assert_held(&arvif->ar->conf_mutex);
50
Kalle Valo5e3dd152013-06-12 20:52:10 +030051 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
52 arg.key_flags = WMI_KEY_PAIRWISE;
53 else
54 arg.key_flags = WMI_KEY_GROUP;
55
56 switch (key->cipher) {
57 case WLAN_CIPHER_SUITE_CCMP:
58 arg.key_cipher = WMI_CIPHER_AES_CCM;
Marek Kwaczynskieeab2662014-05-14 16:56:17 +030059 if (arvif->vdev_type == WMI_VDEV_TYPE_AP)
60 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
61 else
62 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
Kalle Valo5e3dd152013-06-12 20:52:10 +030063 break;
64 case WLAN_CIPHER_SUITE_TKIP:
Kalle Valo5e3dd152013-06-12 20:52:10 +030065 arg.key_cipher = WMI_CIPHER_TKIP;
66 arg.key_txmic_len = 8;
67 arg.key_rxmic_len = 8;
68 break;
69 case WLAN_CIPHER_SUITE_WEP40:
70 case WLAN_CIPHER_SUITE_WEP104:
71 arg.key_cipher = WMI_CIPHER_WEP;
72 /* AP/IBSS mode requires self-key to be groupwise
73 * Otherwise pairwise key must be set */
74 if (memcmp(macaddr, arvif->vif->addr, ETH_ALEN))
75 arg.key_flags = WMI_KEY_PAIRWISE;
76 break;
77 default:
Michal Kazior7aa7a722014-08-25 12:09:38 +020078 ath10k_warn(ar, "cipher %d is not supported\n", key->cipher);
Kalle Valo5e3dd152013-06-12 20:52:10 +030079 return -EOPNOTSUPP;
80 }
81
82 if (cmd == DISABLE_KEY) {
83 arg.key_cipher = WMI_CIPHER_NONE;
84 arg.key_data = NULL;
85 }
86
87 return ath10k_wmi_vdev_install_key(arvif->ar, &arg);
88}
89
90static int ath10k_install_key(struct ath10k_vif *arvif,
91 struct ieee80211_key_conf *key,
92 enum set_key_cmd cmd,
93 const u8 *macaddr)
94{
95 struct ath10k *ar = arvif->ar;
96 int ret;
97
Michal Kazior548db542013-07-05 16:15:15 +030098 lockdep_assert_held(&ar->conf_mutex);
99
Wolfram Sang16735d02013-11-14 14:32:02 -0800100 reinit_completion(&ar->install_key_done);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300101
102 ret = ath10k_send_key(arvif, key, cmd, macaddr);
103 if (ret)
104 return ret;
105
106 ret = wait_for_completion_timeout(&ar->install_key_done, 3*HZ);
107 if (ret == 0)
108 return -ETIMEDOUT;
109
110 return 0;
111}
112
113static int ath10k_install_peer_wep_keys(struct ath10k_vif *arvif,
114 const u8 *addr)
115{
116 struct ath10k *ar = arvif->ar;
117 struct ath10k_peer *peer;
118 int ret;
119 int i;
120
121 lockdep_assert_held(&ar->conf_mutex);
122
123 spin_lock_bh(&ar->data_lock);
124 peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
125 spin_unlock_bh(&ar->data_lock);
126
127 if (!peer)
128 return -ENOENT;
129
130 for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
131 if (arvif->wep_keys[i] == NULL)
132 continue;
133
134 ret = ath10k_install_key(arvif, arvif->wep_keys[i], SET_KEY,
135 addr);
136 if (ret)
137 return ret;
138
Sujith Manoharanae167132014-11-25 11:46:59 +0530139 spin_lock_bh(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300140 peer->keys[i] = arvif->wep_keys[i];
Sujith Manoharanae167132014-11-25 11:46:59 +0530141 spin_unlock_bh(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300142 }
143
144 return 0;
145}
146
147static int ath10k_clear_peer_keys(struct ath10k_vif *arvif,
148 const u8 *addr)
149{
150 struct ath10k *ar = arvif->ar;
151 struct ath10k_peer *peer;
152 int first_errno = 0;
153 int ret;
154 int i;
155
156 lockdep_assert_held(&ar->conf_mutex);
157
158 spin_lock_bh(&ar->data_lock);
159 peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
160 spin_unlock_bh(&ar->data_lock);
161
162 if (!peer)
163 return -ENOENT;
164
165 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
166 if (peer->keys[i] == NULL)
167 continue;
168
169 ret = ath10k_install_key(arvif, peer->keys[i],
170 DISABLE_KEY, addr);
171 if (ret && first_errno == 0)
172 first_errno = ret;
173
174 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200175 ath10k_warn(ar, "failed to remove peer wep key %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300176 i, ret);
177
Sujith Manoharanae167132014-11-25 11:46:59 +0530178 spin_lock_bh(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300179 peer->keys[i] = NULL;
Sujith Manoharanae167132014-11-25 11:46:59 +0530180 spin_unlock_bh(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300181 }
182
183 return first_errno;
184}
185
Sujith Manoharan504f6cd2014-11-25 11:46:58 +0530186bool ath10k_mac_is_peer_wep_key_set(struct ath10k *ar, const u8 *addr,
187 u8 keyidx)
188{
189 struct ath10k_peer *peer;
190 int i;
191
192 lockdep_assert_held(&ar->data_lock);
193
194 /* We don't know which vdev this peer belongs to,
195 * since WMI doesn't give us that information.
196 *
197 * FIXME: multi-bss needs to be handled.
198 */
199 peer = ath10k_peer_find(ar, 0, addr);
200 if (!peer)
201 return false;
202
203 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
204 if (peer->keys[i] && peer->keys[i]->keyidx == keyidx)
205 return true;
206 }
207
208 return false;
209}
210
Kalle Valo5e3dd152013-06-12 20:52:10 +0300211static int ath10k_clear_vdev_key(struct ath10k_vif *arvif,
212 struct ieee80211_key_conf *key)
213{
214 struct ath10k *ar = arvif->ar;
215 struct ath10k_peer *peer;
216 u8 addr[ETH_ALEN];
217 int first_errno = 0;
218 int ret;
219 int i;
220
221 lockdep_assert_held(&ar->conf_mutex);
222
223 for (;;) {
224 /* since ath10k_install_key we can't hold data_lock all the
225 * time, so we try to remove the keys incrementally */
226 spin_lock_bh(&ar->data_lock);
227 i = 0;
228 list_for_each_entry(peer, &ar->peers, list) {
229 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
230 if (peer->keys[i] == key) {
Kalle Valob25f32c2014-09-14 12:50:49 +0300231 ether_addr_copy(addr, peer->addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300232 peer->keys[i] = NULL;
233 break;
234 }
235 }
236
237 if (i < ARRAY_SIZE(peer->keys))
238 break;
239 }
240 spin_unlock_bh(&ar->data_lock);
241
242 if (i == ARRAY_SIZE(peer->keys))
243 break;
244
245 ret = ath10k_install_key(arvif, key, DISABLE_KEY, addr);
246 if (ret && first_errno == 0)
247 first_errno = ret;
248
249 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200250 ath10k_warn(ar, "failed to remove key for %pM: %d\n",
Kalle Valobe6546f2014-03-25 14:18:51 +0200251 addr, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300252 }
253
254 return first_errno;
255}
256
Kalle Valo5e3dd152013-06-12 20:52:10 +0300257/*********************/
258/* General utilities */
259/*********************/
260
261static inline enum wmi_phy_mode
262chan_to_phymode(const struct cfg80211_chan_def *chandef)
263{
264 enum wmi_phy_mode phymode = MODE_UNKNOWN;
265
266 switch (chandef->chan->band) {
267 case IEEE80211_BAND_2GHZ:
268 switch (chandef->width) {
269 case NL80211_CHAN_WIDTH_20_NOHT:
270 phymode = MODE_11G;
271 break;
272 case NL80211_CHAN_WIDTH_20:
273 phymode = MODE_11NG_HT20;
274 break;
275 case NL80211_CHAN_WIDTH_40:
276 phymode = MODE_11NG_HT40;
277 break;
John W. Linville0f817ed2013-06-27 13:50:09 -0400278 case NL80211_CHAN_WIDTH_5:
279 case NL80211_CHAN_WIDTH_10:
Kalle Valo5e3dd152013-06-12 20:52:10 +0300280 case NL80211_CHAN_WIDTH_80:
281 case NL80211_CHAN_WIDTH_80P80:
282 case NL80211_CHAN_WIDTH_160:
283 phymode = MODE_UNKNOWN;
284 break;
285 }
286 break;
287 case IEEE80211_BAND_5GHZ:
288 switch (chandef->width) {
289 case NL80211_CHAN_WIDTH_20_NOHT:
290 phymode = MODE_11A;
291 break;
292 case NL80211_CHAN_WIDTH_20:
293 phymode = MODE_11NA_HT20;
294 break;
295 case NL80211_CHAN_WIDTH_40:
296 phymode = MODE_11NA_HT40;
297 break;
298 case NL80211_CHAN_WIDTH_80:
299 phymode = MODE_11AC_VHT80;
300 break;
John W. Linville0f817ed2013-06-27 13:50:09 -0400301 case NL80211_CHAN_WIDTH_5:
302 case NL80211_CHAN_WIDTH_10:
Kalle Valo5e3dd152013-06-12 20:52:10 +0300303 case NL80211_CHAN_WIDTH_80P80:
304 case NL80211_CHAN_WIDTH_160:
305 phymode = MODE_UNKNOWN;
306 break;
307 }
308 break;
309 default:
310 break;
311 }
312
313 WARN_ON(phymode == MODE_UNKNOWN);
314 return phymode;
315}
316
317static u8 ath10k_parse_mpdudensity(u8 mpdudensity)
318{
319/*
320 * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
321 * 0 for no restriction
322 * 1 for 1/4 us
323 * 2 for 1/2 us
324 * 3 for 1 us
325 * 4 for 2 us
326 * 5 for 4 us
327 * 6 for 8 us
328 * 7 for 16 us
329 */
330 switch (mpdudensity) {
331 case 0:
332 return 0;
333 case 1:
334 case 2:
335 case 3:
336 /* Our lower layer calculations limit our precision to
337 1 microsecond */
338 return 1;
339 case 4:
340 return 2;
341 case 5:
342 return 4;
343 case 6:
344 return 8;
345 case 7:
346 return 16;
347 default:
348 return 0;
349 }
350}
351
352static int ath10k_peer_create(struct ath10k *ar, u32 vdev_id, const u8 *addr)
353{
354 int ret;
355
356 lockdep_assert_held(&ar->conf_mutex);
357
Michal Kaziorcfd10612014-11-25 15:16:05 +0100358 if (ar->num_peers >= ar->max_num_peers)
359 return -ENOBUFS;
360
Kalle Valo5e3dd152013-06-12 20:52:10 +0300361 ret = ath10k_wmi_peer_create(ar, vdev_id, addr);
Ben Greear479398b2013-11-04 09:19:34 -0800362 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200363 ath10k_warn(ar, "failed to create wmi peer %pM on vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +0200364 addr, vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300365 return ret;
Ben Greear479398b2013-11-04 09:19:34 -0800366 }
Kalle Valo5e3dd152013-06-12 20:52:10 +0300367
368 ret = ath10k_wait_for_peer_created(ar, vdev_id, addr);
Ben Greear479398b2013-11-04 09:19:34 -0800369 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200370 ath10k_warn(ar, "failed to wait for created wmi peer %pM on vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +0200371 addr, vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300372 return ret;
Ben Greear479398b2013-11-04 09:19:34 -0800373 }
Michal Kazior292a7532014-11-25 15:16:04 +0100374
Bartosz Markowski0e759f32014-01-02 14:38:33 +0100375 ar->num_peers++;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300376
377 return 0;
378}
379
Kalle Valo5a13e762014-01-20 11:01:46 +0200380static int ath10k_mac_set_kickout(struct ath10k_vif *arvif)
381{
382 struct ath10k *ar = arvif->ar;
383 u32 param;
384 int ret;
385
386 param = ar->wmi.pdev_param->sta_kickout_th;
387 ret = ath10k_wmi_pdev_set_param(ar, param,
388 ATH10K_KICKOUT_THRESHOLD);
389 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200390 ath10k_warn(ar, "failed to set kickout threshold on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200391 arvif->vdev_id, ret);
Kalle Valo5a13e762014-01-20 11:01:46 +0200392 return ret;
393 }
394
395 param = ar->wmi.vdev_param->ap_keepalive_min_idle_inactive_time_secs;
396 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
397 ATH10K_KEEPALIVE_MIN_IDLE);
398 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200399 ath10k_warn(ar, "failed to set keepalive minimum idle time on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200400 arvif->vdev_id, ret);
Kalle Valo5a13e762014-01-20 11:01:46 +0200401 return ret;
402 }
403
404 param = ar->wmi.vdev_param->ap_keepalive_max_idle_inactive_time_secs;
405 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
406 ATH10K_KEEPALIVE_MAX_IDLE);
407 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200408 ath10k_warn(ar, "failed to set keepalive maximum idle time on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200409 arvif->vdev_id, ret);
Kalle Valo5a13e762014-01-20 11:01:46 +0200410 return ret;
411 }
412
413 param = ar->wmi.vdev_param->ap_keepalive_max_unresponsive_time_secs;
414 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
415 ATH10K_KEEPALIVE_MAX_UNRESPONSIVE);
416 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200417 ath10k_warn(ar, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200418 arvif->vdev_id, ret);
Kalle Valo5a13e762014-01-20 11:01:46 +0200419 return ret;
420 }
421
422 return 0;
423}
424
Vivek Natarajanacab6402014-11-26 09:06:12 +0200425static int ath10k_mac_set_rts(struct ath10k_vif *arvif, u32 value)
Michal Kazior424121c2013-07-22 14:13:31 +0200426{
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200427 struct ath10k *ar = arvif->ar;
428 u32 vdev_param;
429
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200430 vdev_param = ar->wmi.vdev_param->rts_threshold;
431 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
Michal Kazior424121c2013-07-22 14:13:31 +0200432}
433
434static int ath10k_mac_set_frag(struct ath10k_vif *arvif, u32 value)
435{
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200436 struct ath10k *ar = arvif->ar;
437 u32 vdev_param;
438
Michal Kazior424121c2013-07-22 14:13:31 +0200439 if (value != 0xFFFFFFFF)
440 value = clamp_t(u32, arvif->ar->hw->wiphy->frag_threshold,
441 ATH10K_FRAGMT_THRESHOLD_MIN,
442 ATH10K_FRAGMT_THRESHOLD_MAX);
443
Bartosz Markowski6d1506e2013-09-26 17:47:15 +0200444 vdev_param = ar->wmi.vdev_param->fragmentation_threshold;
445 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
Michal Kazior424121c2013-07-22 14:13:31 +0200446}
447
Kalle Valo5e3dd152013-06-12 20:52:10 +0300448static int ath10k_peer_delete(struct ath10k *ar, u32 vdev_id, const u8 *addr)
449{
450 int ret;
451
452 lockdep_assert_held(&ar->conf_mutex);
453
454 ret = ath10k_wmi_peer_delete(ar, vdev_id, addr);
455 if (ret)
456 return ret;
457
458 ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
459 if (ret)
460 return ret;
461
Bartosz Markowski0e759f32014-01-02 14:38:33 +0100462 ar->num_peers--;
Bartosz Markowski0e759f32014-01-02 14:38:33 +0100463
Kalle Valo5e3dd152013-06-12 20:52:10 +0300464 return 0;
465}
466
467static void ath10k_peer_cleanup(struct ath10k *ar, u32 vdev_id)
468{
469 struct ath10k_peer *peer, *tmp;
470
471 lockdep_assert_held(&ar->conf_mutex);
472
473 spin_lock_bh(&ar->data_lock);
474 list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
475 if (peer->vdev_id != vdev_id)
476 continue;
477
Michal Kazior7aa7a722014-08-25 12:09:38 +0200478 ath10k_warn(ar, "removing stale peer %pM from vdev_id %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300479 peer->addr, vdev_id);
480
481 list_del(&peer->list);
482 kfree(peer);
Bartosz Markowski0e759f32014-01-02 14:38:33 +0100483 ar->num_peers--;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300484 }
485 spin_unlock_bh(&ar->data_lock);
486}
487
Michal Kaziora96d7742013-07-16 09:38:56 +0200488static void ath10k_peer_cleanup_all(struct ath10k *ar)
489{
490 struct ath10k_peer *peer, *tmp;
491
492 lockdep_assert_held(&ar->conf_mutex);
493
494 spin_lock_bh(&ar->data_lock);
495 list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
496 list_del(&peer->list);
497 kfree(peer);
498 }
499 spin_unlock_bh(&ar->data_lock);
Michal Kazior292a7532014-11-25 15:16:04 +0100500
501 ar->num_peers = 0;
Michal Kaziorcfd10612014-11-25 15:16:05 +0100502 ar->num_stations = 0;
Michal Kaziora96d7742013-07-16 09:38:56 +0200503}
504
Kalle Valo5e3dd152013-06-12 20:52:10 +0300505/************************/
506/* Interface management */
507/************************/
508
Michal Kazior64badcb2014-09-18 11:18:02 +0300509void ath10k_mac_vif_beacon_free(struct ath10k_vif *arvif)
510{
511 struct ath10k *ar = arvif->ar;
512
513 lockdep_assert_held(&ar->data_lock);
514
515 if (!arvif->beacon)
516 return;
517
518 if (!arvif->beacon_buf)
519 dma_unmap_single(ar->dev, ATH10K_SKB_CB(arvif->beacon)->paddr,
520 arvif->beacon->len, DMA_TO_DEVICE);
521
522 dev_kfree_skb_any(arvif->beacon);
523
524 arvif->beacon = NULL;
525 arvif->beacon_sent = false;
526}
527
528static void ath10k_mac_vif_beacon_cleanup(struct ath10k_vif *arvif)
529{
530 struct ath10k *ar = arvif->ar;
531
532 lockdep_assert_held(&ar->data_lock);
533
534 ath10k_mac_vif_beacon_free(arvif);
535
536 if (arvif->beacon_buf) {
537 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
538 arvif->beacon_buf, arvif->beacon_paddr);
539 arvif->beacon_buf = NULL;
540 }
541}
542
Kalle Valo5e3dd152013-06-12 20:52:10 +0300543static inline int ath10k_vdev_setup_sync(struct ath10k *ar)
544{
545 int ret;
546
Michal Kazior548db542013-07-05 16:15:15 +0300547 lockdep_assert_held(&ar->conf_mutex);
548
Michal Kazior7962b0d2014-10-28 10:34:38 +0100549 if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags))
550 return -ESHUTDOWN;
551
Kalle Valo5e3dd152013-06-12 20:52:10 +0300552 ret = wait_for_completion_timeout(&ar->vdev_setup_done,
553 ATH10K_VDEV_SETUP_TIMEOUT_HZ);
554 if (ret == 0)
555 return -ETIMEDOUT;
556
557 return 0;
558}
559
Michal Kazior1bbc0972014-04-08 09:45:47 +0300560static int ath10k_monitor_vdev_start(struct ath10k *ar, int vdev_id)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300561{
Michal Kaziorc930f742014-01-23 11:38:25 +0100562 struct cfg80211_chan_def *chandef = &ar->chandef;
563 struct ieee80211_channel *channel = chandef->chan;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300564 struct wmi_vdev_start_request_arg arg = {};
Kalle Valo5e3dd152013-06-12 20:52:10 +0300565 int ret = 0;
566
567 lockdep_assert_held(&ar->conf_mutex);
568
Kalle Valo5e3dd152013-06-12 20:52:10 +0300569 arg.vdev_id = vdev_id;
570 arg.channel.freq = channel->center_freq;
Michal Kaziorc930f742014-01-23 11:38:25 +0100571 arg.channel.band_center_freq1 = chandef->center_freq1;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300572
573 /* TODO setup this dynamically, what in case we
574 don't have any vifs? */
Michal Kaziorc930f742014-01-23 11:38:25 +0100575 arg.channel.mode = chan_to_phymode(chandef);
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200576 arg.channel.chan_radar =
577 !!(channel->flags & IEEE80211_CHAN_RADAR);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300578
Michal Kazior89c5c842013-10-23 04:02:13 -0700579 arg.channel.min_power = 0;
Michal Kazior02256932013-10-23 04:02:14 -0700580 arg.channel.max_power = channel->max_power * 2;
581 arg.channel.max_reg_power = channel->max_reg_power * 2;
582 arg.channel.max_antenna_gain = channel->max_antenna_gain * 2;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300583
Michal Kazior7962b0d2014-10-28 10:34:38 +0100584 reinit_completion(&ar->vdev_setup_done);
585
Kalle Valo5e3dd152013-06-12 20:52:10 +0300586 ret = ath10k_wmi_vdev_start(ar, &arg);
587 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200588 ath10k_warn(ar, "failed to request monitor vdev %i start: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200589 vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300590 return ret;
591 }
592
593 ret = ath10k_vdev_setup_sync(ar);
594 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200595 ath10k_warn(ar, "failed to synchronize setup for monitor vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200596 vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300597 return ret;
598 }
599
600 ret = ath10k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
601 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200602 ath10k_warn(ar, "failed to put up monitor vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200603 vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300604 goto vdev_stop;
605 }
606
607 ar->monitor_vdev_id = vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300608
Michal Kazior7aa7a722014-08-25 12:09:38 +0200609 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i started\n",
Michal Kazior1bbc0972014-04-08 09:45:47 +0300610 ar->monitor_vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300611 return 0;
612
613vdev_stop:
614 ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
615 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200616 ath10k_warn(ar, "failed to stop monitor vdev %i after start failure: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200617 ar->monitor_vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300618
619 return ret;
620}
621
Michal Kazior1bbc0972014-04-08 09:45:47 +0300622static int ath10k_monitor_vdev_stop(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300623{
624 int ret = 0;
625
626 lockdep_assert_held(&ar->conf_mutex);
627
Marek Puzyniak52fa0192013-09-24 14:06:24 +0200628 ret = ath10k_wmi_vdev_down(ar, ar->monitor_vdev_id);
629 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200630 ath10k_warn(ar, "failed to put down monitor vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200631 ar->monitor_vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300632
Michal Kazior7962b0d2014-10-28 10:34:38 +0100633 reinit_completion(&ar->vdev_setup_done);
634
Kalle Valo5e3dd152013-06-12 20:52:10 +0300635 ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
636 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200637 ath10k_warn(ar, "failed to to request monitor vdev %i stop: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200638 ar->monitor_vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300639
640 ret = ath10k_vdev_setup_sync(ar);
641 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +0200642 ath10k_warn(ar, "failed to synchronise monitor vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200643 ar->monitor_vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300644
Michal Kazior7aa7a722014-08-25 12:09:38 +0200645 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i stopped\n",
Michal Kazior1bbc0972014-04-08 09:45:47 +0300646 ar->monitor_vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300647 return ret;
648}
649
Michal Kazior1bbc0972014-04-08 09:45:47 +0300650static int ath10k_monitor_vdev_create(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300651{
652 int bit, ret = 0;
653
654 lockdep_assert_held(&ar->conf_mutex);
655
Ben Greeara9aefb32014-08-12 11:02:19 +0300656 if (ar->free_vdev_map == 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200657 ath10k_warn(ar, "failed to find free vdev id for monitor vdev\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +0300658 return -ENOMEM;
659 }
660
Ben Greear16c11172014-09-23 14:17:16 -0700661 bit = __ffs64(ar->free_vdev_map);
Ben Greeara9aefb32014-08-12 11:02:19 +0300662
Ben Greear16c11172014-09-23 14:17:16 -0700663 ar->monitor_vdev_id = bit;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300664
665 ret = ath10k_wmi_vdev_create(ar, ar->monitor_vdev_id,
666 WMI_VDEV_TYPE_MONITOR,
667 0, ar->mac_addr);
668 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200669 ath10k_warn(ar, "failed to request monitor vdev %i creation: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200670 ar->monitor_vdev_id, ret);
Ben Greeara9aefb32014-08-12 11:02:19 +0300671 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300672 }
673
Ben Greear16c11172014-09-23 14:17:16 -0700674 ar->free_vdev_map &= ~(1LL << ar->monitor_vdev_id);
Michal Kazior7aa7a722014-08-25 12:09:38 +0200675 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d created\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300676 ar->monitor_vdev_id);
677
Kalle Valo5e3dd152013-06-12 20:52:10 +0300678 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300679}
680
Michal Kazior1bbc0972014-04-08 09:45:47 +0300681static int ath10k_monitor_vdev_delete(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300682{
683 int ret = 0;
684
685 lockdep_assert_held(&ar->conf_mutex);
686
Kalle Valo5e3dd152013-06-12 20:52:10 +0300687 ret = ath10k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
688 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200689 ath10k_warn(ar, "failed to request wmi monitor vdev %i removal: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +0200690 ar->monitor_vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300691 return ret;
692 }
693
Ben Greear16c11172014-09-23 14:17:16 -0700694 ar->free_vdev_map |= 1LL << ar->monitor_vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300695
Michal Kazior7aa7a722014-08-25 12:09:38 +0200696 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d deleted\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +0300697 ar->monitor_vdev_id);
698 return ret;
699}
700
Michal Kazior1bbc0972014-04-08 09:45:47 +0300701static int ath10k_monitor_start(struct ath10k *ar)
702{
703 int ret;
704
705 lockdep_assert_held(&ar->conf_mutex);
706
Michal Kazior1bbc0972014-04-08 09:45:47 +0300707 ret = ath10k_monitor_vdev_create(ar);
708 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200709 ath10k_warn(ar, "failed to create monitor vdev: %d\n", ret);
Michal Kazior1bbc0972014-04-08 09:45:47 +0300710 return ret;
711 }
712
713 ret = ath10k_monitor_vdev_start(ar, ar->monitor_vdev_id);
714 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200715 ath10k_warn(ar, "failed to start monitor vdev: %d\n", ret);
Michal Kazior1bbc0972014-04-08 09:45:47 +0300716 ath10k_monitor_vdev_delete(ar);
717 return ret;
718 }
719
720 ar->monitor_started = true;
Michal Kazior7aa7a722014-08-25 12:09:38 +0200721 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor started\n");
Michal Kazior1bbc0972014-04-08 09:45:47 +0300722
723 return 0;
724}
725
Michal Kazior19337472014-08-28 12:58:16 +0200726static int ath10k_monitor_stop(struct ath10k *ar)
Michal Kazior1bbc0972014-04-08 09:45:47 +0300727{
728 int ret;
729
730 lockdep_assert_held(&ar->conf_mutex);
731
Michal Kazior1bbc0972014-04-08 09:45:47 +0300732 ret = ath10k_monitor_vdev_stop(ar);
Michal Kazior19337472014-08-28 12:58:16 +0200733 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200734 ath10k_warn(ar, "failed to stop monitor vdev: %d\n", ret);
Michal Kazior19337472014-08-28 12:58:16 +0200735 return ret;
736 }
Michal Kazior1bbc0972014-04-08 09:45:47 +0300737
738 ret = ath10k_monitor_vdev_delete(ar);
Michal Kazior19337472014-08-28 12:58:16 +0200739 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200740 ath10k_warn(ar, "failed to delete monitor vdev: %d\n", ret);
Michal Kazior19337472014-08-28 12:58:16 +0200741 return ret;
742 }
Michal Kazior1bbc0972014-04-08 09:45:47 +0300743
744 ar->monitor_started = false;
Michal Kazior7aa7a722014-08-25 12:09:38 +0200745 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopped\n");
Michal Kazior19337472014-08-28 12:58:16 +0200746
747 return 0;
748}
749
750static int ath10k_monitor_recalc(struct ath10k *ar)
751{
752 bool should_start;
753
754 lockdep_assert_held(&ar->conf_mutex);
755
756 should_start = ar->monitor ||
757 ar->filter_flags & FIF_PROMISC_IN_BSS ||
758 test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
759
760 ath10k_dbg(ar, ATH10K_DBG_MAC,
761 "mac monitor recalc started? %d should? %d\n",
762 ar->monitor_started, should_start);
763
764 if (should_start == ar->monitor_started)
765 return 0;
766
767 if (should_start)
768 return ath10k_monitor_start(ar);
Kalle Valod8bb26b2014-09-14 12:50:33 +0300769
770 return ath10k_monitor_stop(ar);
Michal Kazior1bbc0972014-04-08 09:45:47 +0300771}
772
Marek Kwaczynskie81bd102014-03-11 12:58:00 +0200773static int ath10k_recalc_rtscts_prot(struct ath10k_vif *arvif)
774{
775 struct ath10k *ar = arvif->ar;
776 u32 vdev_param, rts_cts = 0;
777
778 lockdep_assert_held(&ar->conf_mutex);
779
780 vdev_param = ar->wmi.vdev_param->enable_rtscts;
781
782 if (arvif->use_cts_prot || arvif->num_legacy_stations > 0)
783 rts_cts |= SM(WMI_RTSCTS_ENABLED, WMI_RTSCTS_SET);
784
785 if (arvif->num_legacy_stations > 0)
786 rts_cts |= SM(WMI_RTSCTS_ACROSS_SW_RETRIES,
787 WMI_RTSCTS_PROFILE);
788
789 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
790 rts_cts);
791}
792
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200793static int ath10k_start_cac(struct ath10k *ar)
794{
795 int ret;
796
797 lockdep_assert_held(&ar->conf_mutex);
798
799 set_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
800
Michal Kazior19337472014-08-28 12:58:16 +0200801 ret = ath10k_monitor_recalc(ar);
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200802 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200803 ath10k_warn(ar, "failed to start monitor (cac): %d\n", ret);
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200804 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
805 return ret;
806 }
807
Michal Kazior7aa7a722014-08-25 12:09:38 +0200808 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac start monitor vdev %d\n",
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200809 ar->monitor_vdev_id);
810
811 return 0;
812}
813
814static int ath10k_stop_cac(struct ath10k *ar)
815{
816 lockdep_assert_held(&ar->conf_mutex);
817
818 /* CAC is not running - do nothing */
819 if (!test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags))
820 return 0;
821
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200822 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
Michal Kazior1bbc0972014-04-08 09:45:47 +0300823 ath10k_monitor_stop(ar);
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200824
Michal Kazior7aa7a722014-08-25 12:09:38 +0200825 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac finished\n");
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200826
827 return 0;
828}
829
Michal Kaziord6500972014-04-08 09:56:09 +0300830static void ath10k_recalc_radar_detection(struct ath10k *ar)
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200831{
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200832 int ret;
833
834 lockdep_assert_held(&ar->conf_mutex);
835
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200836 ath10k_stop_cac(ar);
837
Michal Kaziord6500972014-04-08 09:56:09 +0300838 if (!ar->radar_enabled)
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200839 return;
840
Michal Kaziord6500972014-04-08 09:56:09 +0300841 if (ar->num_started_vdevs > 0)
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200842 return;
843
844 ret = ath10k_start_cac(ar);
845 if (ret) {
846 /*
847 * Not possible to start CAC on current channel so starting
848 * radiation is not allowed, make this channel DFS_UNAVAILABLE
849 * by indicating that radar was detected.
850 */
Michal Kazior7aa7a722014-08-25 12:09:38 +0200851 ath10k_warn(ar, "failed to start CAC: %d\n", ret);
Marek Puzyniake8a50f82013-11-20 09:59:47 +0200852 ieee80211_radar_detected(ar->hw);
853 }
854}
855
Michal Kaziordc55e302014-07-29 12:53:36 +0300856static int ath10k_vdev_start_restart(struct ath10k_vif *arvif, bool restart)
Michal Kazior72654fa2014-04-08 09:56:09 +0300857{
858 struct ath10k *ar = arvif->ar;
859 struct cfg80211_chan_def *chandef = &ar->chandef;
860 struct wmi_vdev_start_request_arg arg = {};
861 int ret = 0;
862
863 lockdep_assert_held(&ar->conf_mutex);
864
865 reinit_completion(&ar->vdev_setup_done);
866
867 arg.vdev_id = arvif->vdev_id;
868 arg.dtim_period = arvif->dtim_period;
869 arg.bcn_intval = arvif->beacon_interval;
870
871 arg.channel.freq = chandef->chan->center_freq;
872 arg.channel.band_center_freq1 = chandef->center_freq1;
873 arg.channel.mode = chan_to_phymode(chandef);
874
875 arg.channel.min_power = 0;
876 arg.channel.max_power = chandef->chan->max_power * 2;
877 arg.channel.max_reg_power = chandef->chan->max_reg_power * 2;
878 arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
879
880 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
881 arg.ssid = arvif->u.ap.ssid;
882 arg.ssid_len = arvif->u.ap.ssid_len;
883 arg.hidden_ssid = arvif->u.ap.hidden_ssid;
884
885 /* For now allow DFS for AP mode */
886 arg.channel.chan_radar =
887 !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
888 } else if (arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
889 arg.ssid = arvif->vif->bss_conf.ssid;
890 arg.ssid_len = arvif->vif->bss_conf.ssid_len;
891 }
892
Michal Kazior7aa7a722014-08-25 12:09:38 +0200893 ath10k_dbg(ar, ATH10K_DBG_MAC,
Michal Kazior72654fa2014-04-08 09:56:09 +0300894 "mac vdev %d start center_freq %d phymode %s\n",
895 arg.vdev_id, arg.channel.freq,
896 ath10k_wmi_phymode_str(arg.channel.mode));
897
Michal Kaziordc55e302014-07-29 12:53:36 +0300898 if (restart)
899 ret = ath10k_wmi_vdev_restart(ar, &arg);
900 else
901 ret = ath10k_wmi_vdev_start(ar, &arg);
902
Michal Kazior72654fa2014-04-08 09:56:09 +0300903 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200904 ath10k_warn(ar, "failed to start WMI vdev %i: %d\n",
Michal Kazior72654fa2014-04-08 09:56:09 +0300905 arg.vdev_id, ret);
906 return ret;
907 }
908
909 ret = ath10k_vdev_setup_sync(ar);
910 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200911 ath10k_warn(ar, "failed to synchronise setup for vdev %i: %d\n",
Michal Kazior72654fa2014-04-08 09:56:09 +0300912 arg.vdev_id, ret);
913 return ret;
914 }
915
Michal Kaziord6500972014-04-08 09:56:09 +0300916 ar->num_started_vdevs++;
917 ath10k_recalc_radar_detection(ar);
918
Michal Kazior72654fa2014-04-08 09:56:09 +0300919 return ret;
920}
921
Michal Kaziordc55e302014-07-29 12:53:36 +0300922static int ath10k_vdev_start(struct ath10k_vif *arvif)
923{
924 return ath10k_vdev_start_restart(arvif, false);
925}
926
927static int ath10k_vdev_restart(struct ath10k_vif *arvif)
928{
929 return ath10k_vdev_start_restart(arvif, true);
930}
931
Michal Kazior72654fa2014-04-08 09:56:09 +0300932static int ath10k_vdev_stop(struct ath10k_vif *arvif)
933{
934 struct ath10k *ar = arvif->ar;
935 int ret;
936
937 lockdep_assert_held(&ar->conf_mutex);
938
939 reinit_completion(&ar->vdev_setup_done);
940
941 ret = ath10k_wmi_vdev_stop(ar, arvif->vdev_id);
942 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200943 ath10k_warn(ar, "failed to stop WMI vdev %i: %d\n",
Michal Kazior72654fa2014-04-08 09:56:09 +0300944 arvif->vdev_id, ret);
945 return ret;
946 }
947
948 ret = ath10k_vdev_setup_sync(ar);
949 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200950 ath10k_warn(ar, "failed to syncronise setup for vdev %i: %d\n",
Michal Kazior72654fa2014-04-08 09:56:09 +0300951 arvif->vdev_id, ret);
952 return ret;
953 }
954
Michal Kaziord6500972014-04-08 09:56:09 +0300955 WARN_ON(ar->num_started_vdevs == 0);
956
957 if (ar->num_started_vdevs != 0) {
958 ar->num_started_vdevs--;
959 ath10k_recalc_radar_detection(ar);
960 }
961
Michal Kazior72654fa2014-04-08 09:56:09 +0300962 return ret;
963}
964
Kalle Valo5e3dd152013-06-12 20:52:10 +0300965static void ath10k_control_beaconing(struct ath10k_vif *arvif,
Kalle Valo5b07e072014-09-14 12:50:06 +0300966 struct ieee80211_bss_conf *info)
Kalle Valo5e3dd152013-06-12 20:52:10 +0300967{
Michal Kazior7aa7a722014-08-25 12:09:38 +0200968 struct ath10k *ar = arvif->ar;
Kalle Valo5e3dd152013-06-12 20:52:10 +0300969 int ret = 0;
970
Michal Kazior548db542013-07-05 16:15:15 +0300971 lockdep_assert_held(&arvif->ar->conf_mutex);
972
Kalle Valo5e3dd152013-06-12 20:52:10 +0300973 if (!info->enable_beacon) {
974 ath10k_vdev_stop(arvif);
Michal Kaziorc930f742014-01-23 11:38:25 +0100975
976 arvif->is_started = false;
977 arvif->is_up = false;
978
Michal Kazior748afc42014-01-23 12:48:21 +0100979 spin_lock_bh(&arvif->ar->data_lock);
Michal Kazior64badcb2014-09-18 11:18:02 +0300980 ath10k_mac_vif_beacon_free(arvif);
Michal Kazior748afc42014-01-23 12:48:21 +0100981 spin_unlock_bh(&arvif->ar->data_lock);
982
Kalle Valo5e3dd152013-06-12 20:52:10 +0300983 return;
984 }
985
986 arvif->tx_seq_no = 0x1000;
987
988 ret = ath10k_vdev_start(arvif);
989 if (ret)
990 return;
991
Michal Kaziorc930f742014-01-23 11:38:25 +0100992 arvif->aid = 0;
Kalle Valob25f32c2014-09-14 12:50:49 +0300993 ether_addr_copy(arvif->bssid, info->bssid);
Michal Kaziorc930f742014-01-23 11:38:25 +0100994
995 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
996 arvif->bssid);
Kalle Valo5e3dd152013-06-12 20:52:10 +0300997 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +0200998 ath10k_warn(ar, "failed to bring up vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +0200999 arvif->vdev_id, ret);
Michal Kaziorc930f742014-01-23 11:38:25 +01001000 ath10k_vdev_stop(arvif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001001 return;
1002 }
Michal Kaziorc930f742014-01-23 11:38:25 +01001003
1004 arvif->is_started = true;
1005 arvif->is_up = true;
1006
Michal Kazior7aa7a722014-08-25 12:09:38 +02001007 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001008}
1009
1010static void ath10k_control_ibss(struct ath10k_vif *arvif,
1011 struct ieee80211_bss_conf *info,
1012 const u8 self_peer[ETH_ALEN])
1013{
Michal Kazior7aa7a722014-08-25 12:09:38 +02001014 struct ath10k *ar = arvif->ar;
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02001015 u32 vdev_param;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001016 int ret = 0;
1017
Michal Kazior548db542013-07-05 16:15:15 +03001018 lockdep_assert_held(&arvif->ar->conf_mutex);
1019
Kalle Valo5e3dd152013-06-12 20:52:10 +03001020 if (!info->ibss_joined) {
1021 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, self_peer);
1022 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001023 ath10k_warn(ar, "failed to delete IBSS self peer %pM for vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001024 self_peer, arvif->vdev_id, ret);
1025
Michal Kaziorc930f742014-01-23 11:38:25 +01001026 if (is_zero_ether_addr(arvif->bssid))
Kalle Valo5e3dd152013-06-12 20:52:10 +03001027 return;
1028
Michal Kaziorc930f742014-01-23 11:38:25 +01001029 memset(arvif->bssid, 0, ETH_ALEN);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001030
1031 return;
1032 }
1033
1034 ret = ath10k_peer_create(arvif->ar, arvif->vdev_id, self_peer);
1035 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001036 ath10k_warn(ar, "failed to create IBSS self peer %pM for vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001037 self_peer, arvif->vdev_id, ret);
1038 return;
1039 }
1040
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02001041 vdev_param = arvif->ar->wmi.vdev_param->atim_window;
1042 ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001043 ATH10K_DEFAULT_ATIM);
1044 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001045 ath10k_warn(ar, "failed to set IBSS ATIM for vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001046 arvif->vdev_id, ret);
1047}
1048
1049/*
1050 * Review this when mac80211 gains per-interface powersave support.
1051 */
Michal Kaziorad088bf2013-10-16 15:44:46 +03001052static int ath10k_mac_vif_setup_ps(struct ath10k_vif *arvif)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001053{
Michal Kaziorad088bf2013-10-16 15:44:46 +03001054 struct ath10k *ar = arvif->ar;
1055 struct ieee80211_conf *conf = &ar->hw->conf;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001056 enum wmi_sta_powersave_param param;
1057 enum wmi_sta_ps_mode psmode;
1058 int ret;
1059
Michal Kazior548db542013-07-05 16:15:15 +03001060 lockdep_assert_held(&arvif->ar->conf_mutex);
1061
Michal Kaziorad088bf2013-10-16 15:44:46 +03001062 if (arvif->vif->type != NL80211_IFTYPE_STATION)
1063 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001064
1065 if (conf->flags & IEEE80211_CONF_PS) {
1066 psmode = WMI_STA_PS_MODE_ENABLED;
1067 param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
1068
Michal Kaziorad088bf2013-10-16 15:44:46 +03001069 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001070 conf->dynamic_ps_timeout);
1071 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001072 ath10k_warn(ar, "failed to set inactivity time for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02001073 arvif->vdev_id, ret);
Michal Kaziorad088bf2013-10-16 15:44:46 +03001074 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001075 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001076 } else {
1077 psmode = WMI_STA_PS_MODE_DISABLED;
1078 }
1079
Michal Kazior7aa7a722014-08-25 12:09:38 +02001080 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d psmode %s\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03001081 arvif->vdev_id, psmode ? "enable" : "disable");
1082
Michal Kaziorad088bf2013-10-16 15:44:46 +03001083 ret = ath10k_wmi_set_psmode(ar, arvif->vdev_id, psmode);
1084 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001085 ath10k_warn(ar, "failed to set PS Mode %d for vdev %d: %d\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02001086 psmode, arvif->vdev_id, ret);
Michal Kaziorad088bf2013-10-16 15:44:46 +03001087 return ret;
1088 }
1089
1090 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001091}
1092
1093/**********************/
1094/* Station management */
1095/**********************/
1096
Michal Kazior590922a2014-10-21 10:10:29 +03001097static u32 ath10k_peer_assoc_h_listen_intval(struct ath10k *ar,
1098 struct ieee80211_vif *vif)
1099{
1100 /* Some firmware revisions have unstable STA powersave when listen
1101 * interval is set too high (e.g. 5). The symptoms are firmware doesn't
1102 * generate NullFunc frames properly even if buffered frames have been
1103 * indicated in Beacon TIM. Firmware would seldom wake up to pull
1104 * buffered frames. Often pinging the device from AP would simply fail.
1105 *
1106 * As a workaround set it to 1.
1107 */
1108 if (vif->type == NL80211_IFTYPE_STATION)
1109 return 1;
1110
1111 return ar->hw->conf.listen_interval;
1112}
1113
Kalle Valo5e3dd152013-06-12 20:52:10 +03001114static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001115 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001116 struct ieee80211_sta *sta,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001117 struct wmi_peer_assoc_complete_arg *arg)
1118{
Michal Kazior590922a2014-10-21 10:10:29 +03001119 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1120
Michal Kazior548db542013-07-05 16:15:15 +03001121 lockdep_assert_held(&ar->conf_mutex);
1122
Kalle Valob25f32c2014-09-14 12:50:49 +03001123 ether_addr_copy(arg->addr, sta->addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001124 arg->vdev_id = arvif->vdev_id;
1125 arg->peer_aid = sta->aid;
1126 arg->peer_flags |= WMI_PEER_AUTH;
Michal Kazior590922a2014-10-21 10:10:29 +03001127 arg->peer_listen_intval = ath10k_peer_assoc_h_listen_intval(ar, vif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001128 arg->peer_num_spatial_streams = 1;
Michal Kazior590922a2014-10-21 10:10:29 +03001129 arg->peer_caps = vif->bss_conf.assoc_capability;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001130}
1131
1132static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001133 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001134 struct wmi_peer_assoc_complete_arg *arg)
1135{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001136 struct ieee80211_bss_conf *info = &vif->bss_conf;
1137 struct cfg80211_bss *bss;
1138 const u8 *rsnie = NULL;
1139 const u8 *wpaie = NULL;
1140
Michal Kazior548db542013-07-05 16:15:15 +03001141 lockdep_assert_held(&ar->conf_mutex);
1142
Kalle Valo5e3dd152013-06-12 20:52:10 +03001143 bss = cfg80211_get_bss(ar->hw->wiphy, ar->hw->conf.chandef.chan,
1144 info->bssid, NULL, 0, 0, 0);
1145 if (bss) {
1146 const struct cfg80211_bss_ies *ies;
1147
1148 rcu_read_lock();
1149 rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
1150
1151 ies = rcu_dereference(bss->ies);
1152
1153 wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
Kalle Valo5b07e072014-09-14 12:50:06 +03001154 WLAN_OUI_TYPE_MICROSOFT_WPA,
1155 ies->data,
1156 ies->len);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001157 rcu_read_unlock();
1158 cfg80211_put_bss(ar->hw->wiphy, bss);
1159 }
1160
1161 /* FIXME: base on RSN IE/WPA IE is a correct idea? */
1162 if (rsnie || wpaie) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001163 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001164 arg->peer_flags |= WMI_PEER_NEED_PTK_4_WAY;
1165 }
1166
1167 if (wpaie) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001168 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: wpa ie found\n", __func__);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001169 arg->peer_flags |= WMI_PEER_NEED_GTK_2_WAY;
1170 }
1171}
1172
1173static void ath10k_peer_assoc_h_rates(struct ath10k *ar,
1174 struct ieee80211_sta *sta,
1175 struct wmi_peer_assoc_complete_arg *arg)
1176{
1177 struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
1178 const struct ieee80211_supported_band *sband;
1179 const struct ieee80211_rate *rates;
1180 u32 ratemask;
1181 int i;
1182
Michal Kazior548db542013-07-05 16:15:15 +03001183 lockdep_assert_held(&ar->conf_mutex);
1184
Kalle Valo5e3dd152013-06-12 20:52:10 +03001185 sband = ar->hw->wiphy->bands[ar->hw->conf.chandef.chan->band];
1186 ratemask = sta->supp_rates[ar->hw->conf.chandef.chan->band];
1187 rates = sband->bitrates;
1188
1189 rateset->num_rates = 0;
1190
1191 for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
1192 if (!(ratemask & 1))
1193 continue;
1194
1195 rateset->rates[rateset->num_rates] = rates->hw_value;
1196 rateset->num_rates++;
1197 }
1198}
1199
1200static void ath10k_peer_assoc_h_ht(struct ath10k *ar,
1201 struct ieee80211_sta *sta,
1202 struct wmi_peer_assoc_complete_arg *arg)
1203{
1204 const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001205 int i, n;
Kalle Valoaf762c02014-09-14 12:50:17 +03001206 u32 stbc;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001207
Michal Kazior548db542013-07-05 16:15:15 +03001208 lockdep_assert_held(&ar->conf_mutex);
1209
Kalle Valo5e3dd152013-06-12 20:52:10 +03001210 if (!ht_cap->ht_supported)
1211 return;
1212
1213 arg->peer_flags |= WMI_PEER_HT;
1214 arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1215 ht_cap->ampdu_factor)) - 1;
1216
1217 arg->peer_mpdu_density =
1218 ath10k_parse_mpdudensity(ht_cap->ampdu_density);
1219
1220 arg->peer_ht_caps = ht_cap->cap;
1221 arg->peer_rate_caps |= WMI_RC_HT_FLAG;
1222
1223 if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
1224 arg->peer_flags |= WMI_PEER_LDPC;
1225
1226 if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
1227 arg->peer_flags |= WMI_PEER_40MHZ;
1228 arg->peer_rate_caps |= WMI_RC_CW40_FLAG;
1229 }
1230
1231 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
1232 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
1233
1234 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
1235 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
1236
1237 if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
1238 arg->peer_rate_caps |= WMI_RC_TX_STBC_FLAG;
1239 arg->peer_flags |= WMI_PEER_STBC;
1240 }
1241
1242 if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001243 stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
1244 stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
1245 stbc = stbc << WMI_RC_RX_STBC_FLAG_S;
1246 arg->peer_rate_caps |= stbc;
1247 arg->peer_flags |= WMI_PEER_STBC;
1248 }
1249
Kalle Valo5e3dd152013-06-12 20:52:10 +03001250 if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
1251 arg->peer_rate_caps |= WMI_RC_TS_FLAG;
1252 else if (ht_cap->mcs.rx_mask[1])
1253 arg->peer_rate_caps |= WMI_RC_DS_FLAG;
1254
1255 for (i = 0, n = 0; i < IEEE80211_HT_MCS_MASK_LEN*8; i++)
1256 if (ht_cap->mcs.rx_mask[i/8] & (1 << i%8))
1257 arg->peer_ht_rates.rates[n++] = i;
1258
Bartosz Markowskifd71f802014-02-10 13:12:55 +01001259 /*
1260 * This is a workaround for HT-enabled STAs which break the spec
1261 * and have no HT capabilities RX mask (no HT RX MCS map).
1262 *
1263 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
1264 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
1265 *
1266 * Firmware asserts if such situation occurs.
1267 */
1268 if (n == 0) {
1269 arg->peer_ht_rates.num_rates = 8;
1270 for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
1271 arg->peer_ht_rates.rates[i] = i;
1272 } else {
1273 arg->peer_ht_rates.num_rates = n;
1274 arg->peer_num_spatial_streams = sta->rx_nss;
1275 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001276
Michal Kazior7aa7a722014-08-25 12:09:38 +02001277 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03001278 arg->addr,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001279 arg->peer_ht_rates.num_rates,
1280 arg->peer_num_spatial_streams);
1281}
1282
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001283static int ath10k_peer_assoc_qos_ap(struct ath10k *ar,
1284 struct ath10k_vif *arvif,
1285 struct ieee80211_sta *sta)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001286{
1287 u32 uapsd = 0;
1288 u32 max_sp = 0;
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001289 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001290
Michal Kazior548db542013-07-05 16:15:15 +03001291 lockdep_assert_held(&ar->conf_mutex);
1292
Kalle Valo5e3dd152013-06-12 20:52:10 +03001293 if (sta->wme && sta->uapsd_queues) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001294 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001295 sta->uapsd_queues, sta->max_sp);
1296
Kalle Valo5e3dd152013-06-12 20:52:10 +03001297 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1298 uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
1299 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
1300 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1301 uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
1302 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
1303 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1304 uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
1305 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
1306 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1307 uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
1308 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
1309
Kalle Valo5e3dd152013-06-12 20:52:10 +03001310 if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
1311 max_sp = sta->max_sp;
1312
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001313 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1314 sta->addr,
1315 WMI_AP_PS_PEER_PARAM_UAPSD,
1316 uapsd);
1317 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001318 ath10k_warn(ar, "failed to set ap ps peer param uapsd for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001319 arvif->vdev_id, ret);
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001320 return ret;
1321 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001322
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001323 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1324 sta->addr,
1325 WMI_AP_PS_PEER_PARAM_MAX_SP,
1326 max_sp);
1327 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001328 ath10k_warn(ar, "failed to set ap ps peer param max sp for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001329 arvif->vdev_id, ret);
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001330 return ret;
1331 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001332
1333 /* TODO setup this based on STA listen interval and
1334 beacon interval. Currently we don't know
1335 sta->listen_interval - mac80211 patch required.
1336 Currently use 10 seconds */
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001337 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id, sta->addr,
Kalle Valo5b07e072014-09-14 12:50:06 +03001338 WMI_AP_PS_PEER_PARAM_AGEOUT_TIME,
1339 10);
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001340 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001341 ath10k_warn(ar, "failed to set ap ps peer param ageout time for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001342 arvif->vdev_id, ret);
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001343 return ret;
1344 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001345 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001346
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001347 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001348}
1349
1350static void ath10k_peer_assoc_h_vht(struct ath10k *ar,
1351 struct ieee80211_sta *sta,
1352 struct wmi_peer_assoc_complete_arg *arg)
1353{
1354 const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
Sujith Manoharana24b88b2013-10-07 19:51:57 -07001355 u8 ampdu_factor;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001356
1357 if (!vht_cap->vht_supported)
1358 return;
1359
1360 arg->peer_flags |= WMI_PEER_VHT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001361 arg->peer_vht_caps = vht_cap->cap;
1362
Sujith Manoharana24b88b2013-10-07 19:51:57 -07001363 ampdu_factor = (vht_cap->cap &
1364 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
1365 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
1366
1367 /* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
1368 * zero in VHT IE. Using it would result in degraded throughput.
1369 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
1370 * it if VHT max_mpdu is smaller. */
1371 arg->peer_max_mpdu = max(arg->peer_max_mpdu,
1372 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1373 ampdu_factor)) - 1);
1374
Kalle Valo5e3dd152013-06-12 20:52:10 +03001375 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1376 arg->peer_flags |= WMI_PEER_80MHZ;
1377
1378 arg->peer_vht_rates.rx_max_rate =
1379 __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
1380 arg->peer_vht_rates.rx_mcs_set =
1381 __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
1382 arg->peer_vht_rates.tx_max_rate =
1383 __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
1384 arg->peer_vht_rates.tx_mcs_set =
1385 __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
1386
Michal Kazior7aa7a722014-08-25 12:09:38 +02001387 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03001388 sta->addr, arg->peer_max_mpdu, arg->peer_flags);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001389}
1390
1391static void ath10k_peer_assoc_h_qos(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001392 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001393 struct ieee80211_sta *sta,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001394 struct wmi_peer_assoc_complete_arg *arg)
1395{
Michal Kazior590922a2014-10-21 10:10:29 +03001396 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1397
Kalle Valo5e3dd152013-06-12 20:52:10 +03001398 switch (arvif->vdev_type) {
1399 case WMI_VDEV_TYPE_AP:
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001400 if (sta->wme)
1401 arg->peer_flags |= WMI_PEER_QOS;
1402
1403 if (sta->wme && sta->uapsd_queues) {
1404 arg->peer_flags |= WMI_PEER_APSD;
1405 arg->peer_rate_caps |= WMI_RC_UAPSD_FLAG;
1406 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001407 break;
1408 case WMI_VDEV_TYPE_STA:
Michal Kazior590922a2014-10-21 10:10:29 +03001409 if (vif->bss_conf.qos)
Janusz Dziedzicd3d3ff42014-01-21 07:06:53 +01001410 arg->peer_flags |= WMI_PEER_QOS;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001411 break;
1412 default:
1413 break;
1414 }
1415}
1416
1417static void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001418 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001419 struct ieee80211_sta *sta,
1420 struct wmi_peer_assoc_complete_arg *arg)
1421{
1422 enum wmi_phy_mode phymode = MODE_UNKNOWN;
1423
Kalle Valo5e3dd152013-06-12 20:52:10 +03001424 switch (ar->hw->conf.chandef.chan->band) {
1425 case IEEE80211_BAND_2GHZ:
1426 if (sta->ht_cap.ht_supported) {
1427 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1428 phymode = MODE_11NG_HT40;
1429 else
1430 phymode = MODE_11NG_HT20;
1431 } else {
1432 phymode = MODE_11G;
1433 }
1434
1435 break;
1436 case IEEE80211_BAND_5GHZ:
Sujith Manoharan7cc45e92013-09-08 18:19:55 +03001437 /*
1438 * Check VHT first.
1439 */
1440 if (sta->vht_cap.vht_supported) {
1441 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1442 phymode = MODE_11AC_VHT80;
1443 else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1444 phymode = MODE_11AC_VHT40;
1445 else if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
1446 phymode = MODE_11AC_VHT20;
1447 } else if (sta->ht_cap.ht_supported) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03001448 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1449 phymode = MODE_11NA_HT40;
1450 else
1451 phymode = MODE_11NA_HT20;
1452 } else {
1453 phymode = MODE_11A;
1454 }
1455
1456 break;
1457 default:
1458 break;
1459 }
1460
Michal Kazior7aa7a722014-08-25 12:09:38 +02001461 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM phymode %s\n",
Kalle Valo38a1d472013-09-08 17:56:14 +03001462 sta->addr, ath10k_wmi_phymode_str(phymode));
Kalle Valo60c3daa2013-09-08 17:56:07 +03001463
Kalle Valo5e3dd152013-06-12 20:52:10 +03001464 arg->peer_phymode = phymode;
1465 WARN_ON(phymode == MODE_UNKNOWN);
1466}
1467
Kalle Valob9ada652013-10-16 15:44:46 +03001468static int ath10k_peer_assoc_prepare(struct ath10k *ar,
Michal Kazior590922a2014-10-21 10:10:29 +03001469 struct ieee80211_vif *vif,
Kalle Valob9ada652013-10-16 15:44:46 +03001470 struct ieee80211_sta *sta,
Kalle Valob9ada652013-10-16 15:44:46 +03001471 struct wmi_peer_assoc_complete_arg *arg)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001472{
Michal Kazior548db542013-07-05 16:15:15 +03001473 lockdep_assert_held(&ar->conf_mutex);
1474
Kalle Valob9ada652013-10-16 15:44:46 +03001475 memset(arg, 0, sizeof(*arg));
Kalle Valo5e3dd152013-06-12 20:52:10 +03001476
Michal Kazior590922a2014-10-21 10:10:29 +03001477 ath10k_peer_assoc_h_basic(ar, vif, sta, arg);
1478 ath10k_peer_assoc_h_crypto(ar, vif, arg);
Kalle Valob9ada652013-10-16 15:44:46 +03001479 ath10k_peer_assoc_h_rates(ar, sta, arg);
1480 ath10k_peer_assoc_h_ht(ar, sta, arg);
1481 ath10k_peer_assoc_h_vht(ar, sta, arg);
Michal Kazior590922a2014-10-21 10:10:29 +03001482 ath10k_peer_assoc_h_qos(ar, vif, sta, arg);
1483 ath10k_peer_assoc_h_phymode(ar, vif, sta, arg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001484
Kalle Valob9ada652013-10-16 15:44:46 +03001485 return 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001486}
1487
Michal Kazior90046f52014-02-14 14:45:51 +01001488static const u32 ath10k_smps_map[] = {
1489 [WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
1490 [WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
1491 [WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
1492 [WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
1493};
1494
1495static int ath10k_setup_peer_smps(struct ath10k *ar, struct ath10k_vif *arvif,
1496 const u8 *addr,
1497 const struct ieee80211_sta_ht_cap *ht_cap)
1498{
1499 int smps;
1500
1501 if (!ht_cap->ht_supported)
1502 return 0;
1503
1504 smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
1505 smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
1506
1507 if (smps >= ARRAY_SIZE(ath10k_smps_map))
1508 return -EINVAL;
1509
1510 return ath10k_wmi_peer_set_param(ar, arvif->vdev_id, addr,
1511 WMI_PEER_SMPS_STATE,
1512 ath10k_smps_map[smps]);
1513}
1514
Kalle Valo5e3dd152013-06-12 20:52:10 +03001515/* can be called only in mac80211 callbacks due to `key_count` usage */
1516static void ath10k_bss_assoc(struct ieee80211_hw *hw,
1517 struct ieee80211_vif *vif,
1518 struct ieee80211_bss_conf *bss_conf)
1519{
1520 struct ath10k *ar = hw->priv;
1521 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Michal Kazior90046f52014-02-14 14:45:51 +01001522 struct ieee80211_sta_ht_cap ht_cap;
Kalle Valob9ada652013-10-16 15:44:46 +03001523 struct wmi_peer_assoc_complete_arg peer_arg;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001524 struct ieee80211_sta *ap_sta;
1525 int ret;
1526
Michal Kazior548db542013-07-05 16:15:15 +03001527 lockdep_assert_held(&ar->conf_mutex);
1528
Michal Kazior077efc82014-10-21 10:10:29 +03001529 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
1530 arvif->vdev_id, arvif->bssid, arvif->aid);
1531
Kalle Valo5e3dd152013-06-12 20:52:10 +03001532 rcu_read_lock();
1533
1534 ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
1535 if (!ap_sta) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001536 ath10k_warn(ar, "failed to find station entry for bss %pM vdev %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02001537 bss_conf->bssid, arvif->vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001538 rcu_read_unlock();
1539 return;
1540 }
1541
Michal Kazior90046f52014-02-14 14:45:51 +01001542 /* ap_sta must be accessed only within rcu section which must be left
1543 * before calling ath10k_setup_peer_smps() which might sleep. */
1544 ht_cap = ap_sta->ht_cap;
1545
Michal Kazior590922a2014-10-21 10:10:29 +03001546 ret = ath10k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001547 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001548 ath10k_warn(ar, "failed to prepare peer assoc for %pM vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001549 bss_conf->bssid, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001550 rcu_read_unlock();
1551 return;
1552 }
1553
1554 rcu_read_unlock();
1555
Kalle Valob9ada652013-10-16 15:44:46 +03001556 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
1557 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001558 ath10k_warn(ar, "failed to run peer assoc for %pM vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001559 bss_conf->bssid, arvif->vdev_id, ret);
Kalle Valob9ada652013-10-16 15:44:46 +03001560 return;
1561 }
1562
Michal Kazior90046f52014-02-14 14:45:51 +01001563 ret = ath10k_setup_peer_smps(ar, arvif, bss_conf->bssid, &ht_cap);
1564 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001565 ath10k_warn(ar, "failed to setup peer SMPS for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001566 arvif->vdev_id, ret);
Michal Kazior90046f52014-02-14 14:45:51 +01001567 return;
1568 }
1569
Michal Kazior7aa7a722014-08-25 12:09:38 +02001570 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03001571 "mac vdev %d up (associated) bssid %pM aid %d\n",
1572 arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
1573
Michal Kazior077efc82014-10-21 10:10:29 +03001574 WARN_ON(arvif->is_up);
1575
Michal Kaziorc930f742014-01-23 11:38:25 +01001576 arvif->aid = bss_conf->aid;
Kalle Valob25f32c2014-09-14 12:50:49 +03001577 ether_addr_copy(arvif->bssid, bss_conf->bssid);
Michal Kaziorc930f742014-01-23 11:38:25 +01001578
1579 ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
1580 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001581 ath10k_warn(ar, "failed to set vdev %d up: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001582 arvif->vdev_id, ret);
Michal Kaziorc930f742014-01-23 11:38:25 +01001583 return;
1584 }
1585
1586 arvif->is_up = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001587}
1588
Kalle Valo5e3dd152013-06-12 20:52:10 +03001589static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
1590 struct ieee80211_vif *vif)
1591{
1592 struct ath10k *ar = hw->priv;
1593 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1594 int ret;
1595
Michal Kazior548db542013-07-05 16:15:15 +03001596 lockdep_assert_held(&ar->conf_mutex);
1597
Michal Kazior077efc82014-10-21 10:10:29 +03001598 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
1599 arvif->vdev_id, arvif->bssid);
Kalle Valo60c3daa2013-09-08 17:56:07 +03001600
Kalle Valo5e3dd152013-06-12 20:52:10 +03001601 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
Michal Kazior077efc82014-10-21 10:10:29 +03001602 if (ret)
1603 ath10k_warn(ar, "faield to down vdev %i: %d\n",
1604 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001605
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001606 arvif->def_wep_key_idx = 0;
Michal Kaziorc930f742014-01-23 11:38:25 +01001607 arvif->is_up = false;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001608}
1609
Michal Kazior590922a2014-10-21 10:10:29 +03001610static int ath10k_station_assoc(struct ath10k *ar,
1611 struct ieee80211_vif *vif,
1612 struct ieee80211_sta *sta,
1613 bool reassoc)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001614{
Michal Kazior590922a2014-10-21 10:10:29 +03001615 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Kalle Valob9ada652013-10-16 15:44:46 +03001616 struct wmi_peer_assoc_complete_arg peer_arg;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001617 int ret = 0;
1618
Michal Kazior548db542013-07-05 16:15:15 +03001619 lockdep_assert_held(&ar->conf_mutex);
1620
Michal Kazior590922a2014-10-21 10:10:29 +03001621 ret = ath10k_peer_assoc_prepare(ar, vif, sta, &peer_arg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001622 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001623 ath10k_warn(ar, "failed to prepare WMI peer assoc for %pM vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02001624 sta->addr, arvif->vdev_id, ret);
Kalle Valob9ada652013-10-16 15:44:46 +03001625 return ret;
1626 }
1627
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02001628 peer_arg.peer_reassoc = reassoc;
Kalle Valob9ada652013-10-16 15:44:46 +03001629 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
1630 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001631 ath10k_warn(ar, "failed to run peer assoc for STA %pM vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001632 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001633 return ret;
1634 }
1635
Michal Kaziorb1ecde32014-10-21 10:10:29 +03001636 /* Re-assoc is run only to update supported rates for given station. It
1637 * doesn't make much sense to reconfigure the peer completely.
1638 */
1639 if (!reassoc) {
1640 ret = ath10k_setup_peer_smps(ar, arvif, sta->addr,
1641 &sta->ht_cap);
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02001642 if (ret) {
Michal Kaziorb1ecde32014-10-21 10:10:29 +03001643 ath10k_warn(ar, "failed to setup peer SMPS for vdev %d: %d\n",
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02001644 arvif->vdev_id, ret);
1645 return ret;
1646 }
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02001647
Michal Kaziorb1ecde32014-10-21 10:10:29 +03001648 ret = ath10k_peer_assoc_qos_ap(ar, arvif, sta);
1649 if (ret) {
1650 ath10k_warn(ar, "failed to set qos params for STA %pM for vdev %i: %d\n",
1651 sta->addr, arvif->vdev_id, ret);
1652 return ret;
1653 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001654
Michal Kaziorb1ecde32014-10-21 10:10:29 +03001655 if (!sta->wme) {
1656 arvif->num_legacy_stations++;
1657 ret = ath10k_recalc_rtscts_prot(arvif);
1658 if (ret) {
1659 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
1660 arvif->vdev_id, ret);
1661 return ret;
1662 }
1663 }
1664
1665 ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
1666 if (ret) {
1667 ath10k_warn(ar, "failed to install peer wep keys for vdev %i: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03001668 arvif->vdev_id, ret);
1669 return ret;
1670 }
1671 }
1672
Kalle Valo5e3dd152013-06-12 20:52:10 +03001673 return ret;
1674}
1675
Michal Kazior590922a2014-10-21 10:10:29 +03001676static int ath10k_station_disassoc(struct ath10k *ar,
1677 struct ieee80211_vif *vif,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001678 struct ieee80211_sta *sta)
1679{
Michal Kazior590922a2014-10-21 10:10:29 +03001680 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001681 int ret = 0;
1682
1683 lockdep_assert_held(&ar->conf_mutex);
1684
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02001685 if (!sta->wme) {
1686 arvif->num_legacy_stations--;
1687 ret = ath10k_recalc_rtscts_prot(arvif);
1688 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001689 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02001690 arvif->vdev_id, ret);
1691 return ret;
1692 }
1693 }
1694
Kalle Valo5e3dd152013-06-12 20:52:10 +03001695 ret = ath10k_clear_peer_keys(arvif, sta->addr);
1696 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001697 ath10k_warn(ar, "failed to clear all peer wep keys for vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02001698 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001699 return ret;
1700 }
1701
1702 return ret;
1703}
1704
1705/**************/
1706/* Regulatory */
1707/**************/
1708
1709static int ath10k_update_channel_list(struct ath10k *ar)
1710{
1711 struct ieee80211_hw *hw = ar->hw;
1712 struct ieee80211_supported_band **bands;
1713 enum ieee80211_band band;
1714 struct ieee80211_channel *channel;
1715 struct wmi_scan_chan_list_arg arg = {0};
1716 struct wmi_channel_arg *ch;
1717 bool passive;
1718 int len;
1719 int ret;
1720 int i;
1721
Michal Kazior548db542013-07-05 16:15:15 +03001722 lockdep_assert_held(&ar->conf_mutex);
1723
Kalle Valo5e3dd152013-06-12 20:52:10 +03001724 bands = hw->wiphy->bands;
1725 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1726 if (!bands[band])
1727 continue;
1728
1729 for (i = 0; i < bands[band]->n_channels; i++) {
1730 if (bands[band]->channels[i].flags &
1731 IEEE80211_CHAN_DISABLED)
1732 continue;
1733
1734 arg.n_channels++;
1735 }
1736 }
1737
1738 len = sizeof(struct wmi_channel_arg) * arg.n_channels;
1739 arg.channels = kzalloc(len, GFP_KERNEL);
1740 if (!arg.channels)
1741 return -ENOMEM;
1742
1743 ch = arg.channels;
1744 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1745 if (!bands[band])
1746 continue;
1747
1748 for (i = 0; i < bands[band]->n_channels; i++) {
1749 channel = &bands[band]->channels[i];
1750
1751 if (channel->flags & IEEE80211_CHAN_DISABLED)
1752 continue;
1753
1754 ch->allow_ht = true;
1755
1756 /* FIXME: when should we really allow VHT? */
1757 ch->allow_vht = true;
1758
1759 ch->allow_ibss =
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +02001760 !(channel->flags & IEEE80211_CHAN_NO_IR);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001761
1762 ch->ht40plus =
1763 !(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
1764
Marek Puzyniake8a50f82013-11-20 09:59:47 +02001765 ch->chan_radar =
1766 !!(channel->flags & IEEE80211_CHAN_RADAR);
1767
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +02001768 passive = channel->flags & IEEE80211_CHAN_NO_IR;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001769 ch->passive = passive;
1770
1771 ch->freq = channel->center_freq;
Michal Kazior2d667212014-09-18 15:21:21 +02001772 ch->band_center_freq1 = channel->center_freq;
Michal Kazior89c5c842013-10-23 04:02:13 -07001773 ch->min_power = 0;
Michal Kazior02256932013-10-23 04:02:14 -07001774 ch->max_power = channel->max_power * 2;
1775 ch->max_reg_power = channel->max_reg_power * 2;
1776 ch->max_antenna_gain = channel->max_antenna_gain * 2;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001777 ch->reg_class_id = 0; /* FIXME */
1778
1779 /* FIXME: why use only legacy modes, why not any
1780 * HT/VHT modes? Would that even make any
1781 * difference? */
1782 if (channel->band == IEEE80211_BAND_2GHZ)
1783 ch->mode = MODE_11G;
1784 else
1785 ch->mode = MODE_11A;
1786
1787 if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
1788 continue;
1789
Michal Kazior7aa7a722014-08-25 12:09:38 +02001790 ath10k_dbg(ar, ATH10K_DBG_WMI,
Kalle Valo60c3daa2013-09-08 17:56:07 +03001791 "mac channel [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
1792 ch - arg.channels, arg.n_channels,
Kalle Valo5e3dd152013-06-12 20:52:10 +03001793 ch->freq, ch->max_power, ch->max_reg_power,
1794 ch->max_antenna_gain, ch->mode);
1795
1796 ch++;
1797 }
1798 }
1799
1800 ret = ath10k_wmi_scan_chan_list(ar, &arg);
1801 kfree(arg.channels);
1802
1803 return ret;
1804}
1805
Marek Puzyniak821af6a2014-03-21 17:46:57 +02001806static enum wmi_dfs_region
1807ath10k_mac_get_dfs_region(enum nl80211_dfs_regions dfs_region)
1808{
1809 switch (dfs_region) {
1810 case NL80211_DFS_UNSET:
1811 return WMI_UNINIT_DFS_DOMAIN;
1812 case NL80211_DFS_FCC:
1813 return WMI_FCC_DFS_DOMAIN;
1814 case NL80211_DFS_ETSI:
1815 return WMI_ETSI_DFS_DOMAIN;
1816 case NL80211_DFS_JP:
1817 return WMI_MKK4_DFS_DOMAIN;
1818 }
1819 return WMI_UNINIT_DFS_DOMAIN;
1820}
1821
Michal Kaziorf7843d72013-07-16 09:38:52 +02001822static void ath10k_regd_update(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001823{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001824 struct reg_dmn_pair_mapping *regpair;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001825 int ret;
Marek Puzyniak821af6a2014-03-21 17:46:57 +02001826 enum wmi_dfs_region wmi_dfs_reg;
1827 enum nl80211_dfs_regions nl_dfs_reg;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001828
Michal Kaziorf7843d72013-07-16 09:38:52 +02001829 lockdep_assert_held(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001830
1831 ret = ath10k_update_channel_list(ar);
1832 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001833 ath10k_warn(ar, "failed to update channel list: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001834
1835 regpair = ar->ath_common.regulatory.regpair;
Michal Kaziorf7843d72013-07-16 09:38:52 +02001836
Marek Puzyniak821af6a2014-03-21 17:46:57 +02001837 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
1838 nl_dfs_reg = ar->dfs_detector->region;
1839 wmi_dfs_reg = ath10k_mac_get_dfs_region(nl_dfs_reg);
1840 } else {
1841 wmi_dfs_reg = WMI_UNINIT_DFS_DOMAIN;
1842 }
1843
Kalle Valo5e3dd152013-06-12 20:52:10 +03001844 /* Target allows setting up per-band regdomain but ath_common provides
1845 * a combined one only */
1846 ret = ath10k_wmi_pdev_set_regdomain(ar,
Kalle Valoef8c0012014-02-13 18:13:12 +02001847 regpair->reg_domain,
1848 regpair->reg_domain, /* 2ghz */
1849 regpair->reg_domain, /* 5ghz */
Kalle Valo5e3dd152013-06-12 20:52:10 +03001850 regpair->reg_2ghz_ctl,
Marek Puzyniak821af6a2014-03-21 17:46:57 +02001851 regpair->reg_5ghz_ctl,
1852 wmi_dfs_reg);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001853 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001854 ath10k_warn(ar, "failed to set pdev regdomain: %d\n", ret);
Michal Kaziorf7843d72013-07-16 09:38:52 +02001855}
Michal Kazior548db542013-07-05 16:15:15 +03001856
Michal Kaziorf7843d72013-07-16 09:38:52 +02001857static void ath10k_reg_notifier(struct wiphy *wiphy,
1858 struct regulatory_request *request)
1859{
1860 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
1861 struct ath10k *ar = hw->priv;
Janusz Dziedzic9702c682013-11-20 09:59:41 +02001862 bool result;
Michal Kaziorf7843d72013-07-16 09:38:52 +02001863
1864 ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
1865
Janusz Dziedzic9702c682013-11-20 09:59:41 +02001866 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001867 ath10k_dbg(ar, ATH10K_DBG_REGULATORY, "dfs region 0x%x\n",
Janusz Dziedzic9702c682013-11-20 09:59:41 +02001868 request->dfs_region);
1869 result = ar->dfs_detector->set_dfs_domain(ar->dfs_detector,
1870 request->dfs_region);
1871 if (!result)
Michal Kazior7aa7a722014-08-25 12:09:38 +02001872 ath10k_warn(ar, "DFS region 0x%X not supported, will trigger radar for every pulse\n",
Janusz Dziedzic9702c682013-11-20 09:59:41 +02001873 request->dfs_region);
1874 }
1875
Michal Kaziorf7843d72013-07-16 09:38:52 +02001876 mutex_lock(&ar->conf_mutex);
1877 if (ar->state == ATH10K_STATE_ON)
1878 ath10k_regd_update(ar);
Michal Kazior548db542013-07-05 16:15:15 +03001879 mutex_unlock(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001880}
1881
1882/***************/
1883/* TX handlers */
1884/***************/
1885
Michal Kazior42c3aa62013-10-02 11:03:38 +02001886static u8 ath10k_tx_h_get_tid(struct ieee80211_hdr *hdr)
1887{
1888 if (ieee80211_is_mgmt(hdr->frame_control))
1889 return HTT_DATA_TX_EXT_TID_MGMT;
1890
1891 if (!ieee80211_is_data_qos(hdr->frame_control))
1892 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
1893
1894 if (!is_unicast_ether_addr(ieee80211_get_DA(hdr)))
1895 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
1896
1897 return ieee80211_get_qos_ctl(hdr)[0] & IEEE80211_QOS_CTL_TID_MASK;
1898}
1899
Michal Kazior2b37c292014-09-02 11:00:22 +03001900static u8 ath10k_tx_h_get_vdev_id(struct ath10k *ar, struct ieee80211_vif *vif)
Michal Kaziorddb6ad72013-10-02 11:03:39 +02001901{
Michal Kazior2b37c292014-09-02 11:00:22 +03001902 if (vif)
1903 return ath10k_vif_to_arvif(vif)->vdev_id;
Michal Kaziorddb6ad72013-10-02 11:03:39 +02001904
Michal Kazior1bbc0972014-04-08 09:45:47 +03001905 if (ar->monitor_started)
Michal Kaziorddb6ad72013-10-02 11:03:39 +02001906 return ar->monitor_vdev_id;
1907
Michal Kazior7aa7a722014-08-25 12:09:38 +02001908 ath10k_warn(ar, "failed to resolve vdev id\n");
Michal Kaziorddb6ad72013-10-02 11:03:39 +02001909 return 0;
1910}
1911
Michal Kazior4b604552014-07-21 21:03:09 +03001912/* HTT Tx uses Native Wifi tx mode which expects 802.11 frames without QoS
1913 * Control in the header.
Kalle Valo5e3dd152013-06-12 20:52:10 +03001914 */
Michal Kazior4b604552014-07-21 21:03:09 +03001915static void ath10k_tx_h_nwifi(struct ieee80211_hw *hw, struct sk_buff *skb)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001916{
1917 struct ieee80211_hdr *hdr = (void *)skb->data;
Michal Kaziorc21c64d2014-07-21 21:03:10 +03001918 struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
Kalle Valo5e3dd152013-06-12 20:52:10 +03001919 u8 *qos_ctl;
1920
1921 if (!ieee80211_is_data_qos(hdr->frame_control))
1922 return;
1923
1924 qos_ctl = ieee80211_get_qos_ctl(hdr);
Michal Kaziorba0ccd72013-07-22 14:25:28 +02001925 memmove(skb->data + IEEE80211_QOS_CTL_LEN,
1926 skb->data, (void *)qos_ctl - (void *)skb->data);
1927 skb_pull(skb, IEEE80211_QOS_CTL_LEN);
Michal Kaziorc21c64d2014-07-21 21:03:10 +03001928
1929 /* Fw/Hw generates a corrupted QoS Control Field for QoS NullFunc
1930 * frames. Powersave is handled by the fw/hw so QoS NyllFunc frames are
1931 * used only for CQM purposes (e.g. hostapd station keepalive ping) so
1932 * it is safe to downgrade to NullFunc.
1933 */
1934 if (ieee80211_is_qos_nullfunc(hdr->frame_control)) {
1935 hdr->frame_control &= ~__cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
1936 cb->htt.tid = HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
1937 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03001938}
1939
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001940static void ath10k_tx_wep_key_work(struct work_struct *work)
1941{
1942 struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
1943 wep_key_work);
Michal Kazior7aa7a722014-08-25 12:09:38 +02001944 struct ath10k *ar = arvif->ar;
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001945 int ret, keyidx = arvif->def_wep_key_newidx;
1946
Michal Kazior911e6c02014-05-26 12:46:03 +03001947 mutex_lock(&arvif->ar->conf_mutex);
1948
1949 if (arvif->ar->state != ATH10K_STATE_ON)
1950 goto unlock;
1951
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001952 if (arvif->def_wep_key_idx == keyidx)
Michal Kazior911e6c02014-05-26 12:46:03 +03001953 goto unlock;
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001954
Michal Kazior7aa7a722014-08-25 12:09:38 +02001955 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001956 arvif->vdev_id, keyidx);
1957
1958 ret = ath10k_wmi_vdev_set_param(arvif->ar,
1959 arvif->vdev_id,
1960 arvif->ar->wmi.vdev_param->def_keyid,
1961 keyidx);
1962 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02001963 ath10k_warn(ar, "failed to update wep key index for vdev %d: %d\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02001964 arvif->vdev_id,
1965 ret);
Michal Kazior911e6c02014-05-26 12:46:03 +03001966 goto unlock;
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001967 }
1968
1969 arvif->def_wep_key_idx = keyidx;
Michal Kazior911e6c02014-05-26 12:46:03 +03001970
1971unlock:
1972 mutex_unlock(&arvif->ar->conf_mutex);
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001973}
1974
Michal Kazior4b604552014-07-21 21:03:09 +03001975static void ath10k_tx_h_update_wep_key(struct ieee80211_vif *vif,
1976 struct ieee80211_key_conf *key,
1977 struct sk_buff *skb)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001978{
Kalle Valo5e3dd152013-06-12 20:52:10 +03001979 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1980 struct ath10k *ar = arvif->ar;
1981 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Kalle Valo5e3dd152013-06-12 20:52:10 +03001982
Kalle Valo5e3dd152013-06-12 20:52:10 +03001983 if (!ieee80211_has_protected(hdr->frame_control))
1984 return;
1985
1986 if (!key)
1987 return;
1988
1989 if (key->cipher != WLAN_CIPHER_SUITE_WEP40 &&
1990 key->cipher != WLAN_CIPHER_SUITE_WEP104)
1991 return;
1992
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001993 if (key->keyidx == arvif->def_wep_key_idx)
Kalle Valo5e3dd152013-06-12 20:52:10 +03001994 return;
1995
Michal Kaziorcc4827b2013-10-16 15:44:45 +03001996 /* FIXME: Most likely a few frames will be TXed with an old key. Simply
1997 * queueing frames until key index is updated is not an option because
1998 * sk_buff may need more processing to be done, e.g. offchannel */
1999 arvif->def_wep_key_newidx = key->keyidx;
2000 ieee80211_queue_work(ar->hw, &arvif->wep_key_work);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002001}
2002
Michal Kazior4b604552014-07-21 21:03:09 +03002003static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar,
2004 struct ieee80211_vif *vif,
2005 struct sk_buff *skb)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002006{
2007 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002008 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2009
2010 /* This is case only for P2P_GO */
2011 if (arvif->vdev_type != WMI_VDEV_TYPE_AP ||
2012 arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
2013 return;
2014
2015 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
2016 spin_lock_bh(&ar->data_lock);
2017 if (arvif->u.ap.noa_data)
2018 if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
2019 GFP_ATOMIC))
2020 memcpy(skb_put(skb, arvif->u.ap.noa_len),
2021 arvif->u.ap.noa_data,
2022 arvif->u.ap.noa_len);
2023 spin_unlock_bh(&ar->data_lock);
2024 }
2025}
2026
Michal Kazior8d6d3622014-11-24 14:58:31 +01002027static bool ath10k_mac_need_offchan_tx_work(struct ath10k *ar)
2028{
2029 /* FIXME: Not really sure since when the behaviour changed. At some
2030 * point new firmware stopped requiring creation of peer entries for
2031 * offchannel tx (and actually creating them causes issues with wmi-htc
2032 * tx credit replenishment and reliability). Assuming it's at least 3.4
2033 * because that's when the `freq` was introduced to TX_FRM HTT command.
2034 */
2035 return !(ar->htt.target_version_major >= 3 &&
2036 ar->htt.target_version_minor >= 4);
2037}
2038
Kalle Valo5e3dd152013-06-12 20:52:10 +03002039static void ath10k_tx_htt(struct ath10k *ar, struct sk_buff *skb)
2040{
2041 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002042 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002043
Michal Kazior961d4c32013-08-09 10:13:34 +02002044 if (ar->htt.target_version_major >= 3) {
2045 /* Since HTT 3.0 there is no separate mgmt tx command */
2046 ret = ath10k_htt_tx(&ar->htt, skb);
2047 goto exit;
2048 }
2049
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002050 if (ieee80211_is_mgmt(hdr->frame_control)) {
2051 if (test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
2052 ar->fw_features)) {
2053 if (skb_queue_len(&ar->wmi_mgmt_tx_queue) >=
2054 ATH10K_MAX_NUM_MGMT_PENDING) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002055 ath10k_warn(ar, "reached WMI management transmit queue limit\n");
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002056 ret = -EBUSY;
2057 goto exit;
2058 }
2059
2060 skb_queue_tail(&ar->wmi_mgmt_tx_queue, skb);
2061 ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
2062 } else {
2063 ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
2064 }
2065 } else if (!test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
2066 ar->fw_features) &&
2067 ieee80211_is_nullfunc(hdr->frame_control)) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03002068 /* FW does not report tx status properly for NullFunc frames
2069 * unless they are sent through mgmt tx path. mac80211 sends
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002070 * those frames when it detects link/beacon loss and depends
2071 * on the tx status to be correct. */
Michal Kazioredb82362013-07-05 16:15:14 +03002072 ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002073 } else {
Michal Kazioredb82362013-07-05 16:15:14 +03002074 ret = ath10k_htt_tx(&ar->htt, skb);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002075 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002076
Michal Kazior961d4c32013-08-09 10:13:34 +02002077exit:
Kalle Valo5e3dd152013-06-12 20:52:10 +03002078 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002079 ath10k_warn(ar, "failed to transmit packet, dropping: %d\n",
2080 ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002081 ieee80211_free_txskb(ar->hw, skb);
2082 }
2083}
2084
2085void ath10k_offchan_tx_purge(struct ath10k *ar)
2086{
2087 struct sk_buff *skb;
2088
2089 for (;;) {
2090 skb = skb_dequeue(&ar->offchan_tx_queue);
2091 if (!skb)
2092 break;
2093
2094 ieee80211_free_txskb(ar->hw, skb);
2095 }
2096}
2097
2098void ath10k_offchan_tx_work(struct work_struct *work)
2099{
2100 struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
2101 struct ath10k_peer *peer;
2102 struct ieee80211_hdr *hdr;
2103 struct sk_buff *skb;
2104 const u8 *peer_addr;
2105 int vdev_id;
2106 int ret;
2107
2108 /* FW requirement: We must create a peer before FW will send out
2109 * an offchannel frame. Otherwise the frame will be stuck and
2110 * never transmitted. We delete the peer upon tx completion.
2111 * It is unlikely that a peer for offchannel tx will already be
2112 * present. However it may be in some rare cases so account for that.
2113 * Otherwise we might remove a legitimate peer and break stuff. */
2114
2115 for (;;) {
2116 skb = skb_dequeue(&ar->offchan_tx_queue);
2117 if (!skb)
2118 break;
2119
2120 mutex_lock(&ar->conf_mutex);
2121
Michal Kazior7aa7a722014-08-25 12:09:38 +02002122 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac offchannel skb %p\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002123 skb);
2124
2125 hdr = (struct ieee80211_hdr *)skb->data;
2126 peer_addr = ieee80211_get_DA(hdr);
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002127 vdev_id = ATH10K_SKB_CB(skb)->vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002128
2129 spin_lock_bh(&ar->data_lock);
2130 peer = ath10k_peer_find(ar, vdev_id, peer_addr);
2131 spin_unlock_bh(&ar->data_lock);
2132
2133 if (peer)
Kalle Valo60c3daa2013-09-08 17:56:07 +03002134 /* FIXME: should this use ath10k_warn()? */
Michal Kazior7aa7a722014-08-25 12:09:38 +02002135 ath10k_dbg(ar, ATH10K_DBG_MAC, "peer %pM on vdev %d already present\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002136 peer_addr, vdev_id);
2137
2138 if (!peer) {
2139 ret = ath10k_peer_create(ar, vdev_id, peer_addr);
2140 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002141 ath10k_warn(ar, "failed to create peer %pM on vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002142 peer_addr, vdev_id, ret);
2143 }
2144
2145 spin_lock_bh(&ar->data_lock);
Wolfram Sang16735d02013-11-14 14:32:02 -08002146 reinit_completion(&ar->offchan_tx_completed);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002147 ar->offchan_tx_skb = skb;
2148 spin_unlock_bh(&ar->data_lock);
2149
2150 ath10k_tx_htt(ar, skb);
2151
2152 ret = wait_for_completion_timeout(&ar->offchan_tx_completed,
2153 3 * HZ);
2154 if (ret <= 0)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002155 ath10k_warn(ar, "timed out waiting for offchannel skb %p\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002156 skb);
2157
2158 if (!peer) {
2159 ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
2160 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002161 ath10k_warn(ar, "failed to delete peer %pM on vdev %d: %d\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03002162 peer_addr, vdev_id, ret);
2163 }
2164
2165 mutex_unlock(&ar->conf_mutex);
2166 }
2167}
2168
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002169void ath10k_mgmt_over_wmi_tx_purge(struct ath10k *ar)
2170{
2171 struct sk_buff *skb;
2172
2173 for (;;) {
2174 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
2175 if (!skb)
2176 break;
2177
2178 ieee80211_free_txskb(ar->hw, skb);
2179 }
2180}
2181
2182void ath10k_mgmt_over_wmi_tx_work(struct work_struct *work)
2183{
2184 struct ath10k *ar = container_of(work, struct ath10k, wmi_mgmt_tx_work);
2185 struct sk_buff *skb;
2186 int ret;
2187
2188 for (;;) {
2189 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
2190 if (!skb)
2191 break;
2192
2193 ret = ath10k_wmi_mgmt_tx(ar, skb);
Michal Kazior5fb5e412013-10-28 07:18:13 +01002194 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002195 ath10k_warn(ar, "failed to transmit management frame via WMI: %d\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02002196 ret);
Michal Kazior5fb5e412013-10-28 07:18:13 +01002197 ieee80211_free_txskb(ar->hw, skb);
2198 }
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002199 }
2200}
2201
Kalle Valo5e3dd152013-06-12 20:52:10 +03002202/************/
2203/* Scanning */
2204/************/
2205
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002206void __ath10k_scan_finish(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002207{
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002208 lockdep_assert_held(&ar->data_lock);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002209
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002210 switch (ar->scan.state) {
2211 case ATH10K_SCAN_IDLE:
2212 break;
2213 case ATH10K_SCAN_RUNNING:
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002214 if (ar->scan.is_roc)
2215 ieee80211_remain_on_channel_expired(ar->hw);
Michal Kazior7305d3e2014-11-24 14:58:33 +01002216 case ATH10K_SCAN_ABORTING:
2217 if (!ar->scan.is_roc)
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002218 ieee80211_scan_completed(ar->hw,
2219 (ar->scan.state ==
2220 ATH10K_SCAN_ABORTING));
2221 /* fall through */
2222 case ATH10K_SCAN_STARTING:
2223 ar->scan.state = ATH10K_SCAN_IDLE;
2224 ar->scan_channel = NULL;
2225 ath10k_offchan_tx_purge(ar);
2226 cancel_delayed_work(&ar->scan.timeout);
2227 complete_all(&ar->scan.completed);
2228 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002229 }
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002230}
Kalle Valo5e3dd152013-06-12 20:52:10 +03002231
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002232void ath10k_scan_finish(struct ath10k *ar)
2233{
2234 spin_lock_bh(&ar->data_lock);
2235 __ath10k_scan_finish(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002236 spin_unlock_bh(&ar->data_lock);
2237}
2238
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002239static int ath10k_scan_stop(struct ath10k *ar)
Kalle Valo5e3dd152013-06-12 20:52:10 +03002240{
2241 struct wmi_stop_scan_arg arg = {
2242 .req_id = 1, /* FIXME */
2243 .req_type = WMI_SCAN_STOP_ONE,
2244 .u.scan_id = ATH10K_SCAN_ID,
2245 };
2246 int ret;
2247
2248 lockdep_assert_held(&ar->conf_mutex);
2249
Kalle Valo5e3dd152013-06-12 20:52:10 +03002250 ret = ath10k_wmi_stop_scan(ar, &arg);
2251 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002252 ath10k_warn(ar, "failed to stop wmi scan: %d\n", ret);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002253 goto out;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002254 }
2255
Kalle Valo5e3dd152013-06-12 20:52:10 +03002256 ret = wait_for_completion_timeout(&ar->scan.completed, 3*HZ);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002257 if (ret == 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002258 ath10k_warn(ar, "failed to receive scan abortion completion: timed out\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002259 ret = -ETIMEDOUT;
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002260 } else if (ret > 0) {
2261 ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002262 }
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002263
2264out:
2265 /* Scan state should be updated upon scan completion but in case
2266 * firmware fails to deliver the event (for whatever reason) it is
2267 * desired to clean up scan state anyway. Firmware may have just
2268 * dropped the scan completion event delivery due to transport pipe
2269 * being overflown with data and/or it can recover on its own before
2270 * next scan request is submitted.
2271 */
2272 spin_lock_bh(&ar->data_lock);
2273 if (ar->scan.state != ATH10K_SCAN_IDLE)
2274 __ath10k_scan_finish(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002275 spin_unlock_bh(&ar->data_lock);
2276
2277 return ret;
2278}
2279
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002280static void ath10k_scan_abort(struct ath10k *ar)
2281{
2282 int ret;
2283
2284 lockdep_assert_held(&ar->conf_mutex);
2285
2286 spin_lock_bh(&ar->data_lock);
2287
2288 switch (ar->scan.state) {
2289 case ATH10K_SCAN_IDLE:
2290 /* This can happen if timeout worker kicked in and called
2291 * abortion while scan completion was being processed.
2292 */
2293 break;
2294 case ATH10K_SCAN_STARTING:
2295 case ATH10K_SCAN_ABORTING:
Michal Kazior7aa7a722014-08-25 12:09:38 +02002296 ath10k_warn(ar, "refusing scan abortion due to invalid scan state: %s (%d)\n",
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002297 ath10k_scan_state_str(ar->scan.state),
2298 ar->scan.state);
2299 break;
2300 case ATH10K_SCAN_RUNNING:
2301 ar->scan.state = ATH10K_SCAN_ABORTING;
2302 spin_unlock_bh(&ar->data_lock);
2303
2304 ret = ath10k_scan_stop(ar);
2305 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002306 ath10k_warn(ar, "failed to abort scan: %d\n", ret);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002307
2308 spin_lock_bh(&ar->data_lock);
2309 break;
2310 }
2311
2312 spin_unlock_bh(&ar->data_lock);
2313}
2314
2315void ath10k_scan_timeout_work(struct work_struct *work)
2316{
2317 struct ath10k *ar = container_of(work, struct ath10k,
2318 scan.timeout.work);
2319
2320 mutex_lock(&ar->conf_mutex);
2321 ath10k_scan_abort(ar);
2322 mutex_unlock(&ar->conf_mutex);
2323}
2324
Kalle Valo5e3dd152013-06-12 20:52:10 +03002325static int ath10k_start_scan(struct ath10k *ar,
2326 const struct wmi_start_scan_arg *arg)
2327{
2328 int ret;
2329
2330 lockdep_assert_held(&ar->conf_mutex);
2331
2332 ret = ath10k_wmi_start_scan(ar, arg);
2333 if (ret)
2334 return ret;
2335
Kalle Valo5e3dd152013-06-12 20:52:10 +03002336 ret = wait_for_completion_timeout(&ar->scan.started, 1*HZ);
2337 if (ret == 0) {
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002338 ret = ath10k_scan_stop(ar);
2339 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002340 ath10k_warn(ar, "failed to stop scan: %d\n", ret);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002341
2342 return -ETIMEDOUT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002343 }
2344
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002345 /* Add a 200ms margin to account for event/command processing */
2346 ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
2347 msecs_to_jiffies(arg->max_scan_time+200));
Kalle Valo5e3dd152013-06-12 20:52:10 +03002348 return 0;
2349}
2350
2351/**********************/
2352/* mac80211 callbacks */
2353/**********************/
2354
2355static void ath10k_tx(struct ieee80211_hw *hw,
2356 struct ieee80211_tx_control *control,
2357 struct sk_buff *skb)
2358{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002359 struct ath10k *ar = hw->priv;
Michal Kazior4b604552014-07-21 21:03:09 +03002360 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2361 struct ieee80211_vif *vif = info->control.vif;
2362 struct ieee80211_key_conf *key = info->control.hw_key;
2363 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002364
2365 /* We should disable CCK RATE due to P2P */
2366 if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
Michal Kazior7aa7a722014-08-25 12:09:38 +02002367 ath10k_dbg(ar, ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002368
Michal Kazior4b604552014-07-21 21:03:09 +03002369 ATH10K_SKB_CB(skb)->htt.is_offchan = false;
2370 ATH10K_SKB_CB(skb)->htt.tid = ath10k_tx_h_get_tid(hdr);
Michal Kazior2b37c292014-09-02 11:00:22 +03002371 ATH10K_SKB_CB(skb)->vdev_id = ath10k_tx_h_get_vdev_id(ar, vif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002372
Michal Kaziorcf84bd42013-07-16 11:04:54 +02002373 /* it makes no sense to process injected frames like that */
Michal Kazior4b604552014-07-21 21:03:09 +03002374 if (vif && vif->type != NL80211_IFTYPE_MONITOR) {
2375 ath10k_tx_h_nwifi(hw, skb);
2376 ath10k_tx_h_update_wep_key(vif, key, skb);
2377 ath10k_tx_h_add_p2p_noa_ie(ar, vif, skb);
2378 ath10k_tx_h_seq_no(vif, skb);
Michal Kaziorcf84bd42013-07-16 11:04:54 +02002379 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002380
Kalle Valo5e3dd152013-06-12 20:52:10 +03002381 if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
2382 spin_lock_bh(&ar->data_lock);
Michal Kazior8d6d3622014-11-24 14:58:31 +01002383 ATH10K_SKB_CB(skb)->htt.freq = ar->scan.roc_freq;
Bartosz Markowski5e00d312013-09-26 17:47:12 +02002384 ATH10K_SKB_CB(skb)->vdev_id = ar->scan.vdev_id;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002385 spin_unlock_bh(&ar->data_lock);
2386
Michal Kazior8d6d3622014-11-24 14:58:31 +01002387 if (ath10k_mac_need_offchan_tx_work(ar)) {
2388 ATH10K_SKB_CB(skb)->htt.freq = 0;
2389 ATH10K_SKB_CB(skb)->htt.is_offchan = true;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002390
Michal Kazior8d6d3622014-11-24 14:58:31 +01002391 ath10k_dbg(ar, ATH10K_DBG_MAC, "queued offchannel skb %p\n",
2392 skb);
2393
2394 skb_queue_tail(&ar->offchan_tx_queue, skb);
2395 ieee80211_queue_work(hw, &ar->offchan_tx_work);
2396 return;
2397 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002398 }
2399
2400 ath10k_tx_htt(ar, skb);
2401}
2402
Michal Kaziorbca7baf2014-05-26 12:46:03 +03002403/* Must not be called with conf_mutex held as workers can use that also. */
Michal Kazior7962b0d2014-10-28 10:34:38 +01002404void ath10k_drain_tx(struct ath10k *ar)
Michal Kaziorbca7baf2014-05-26 12:46:03 +03002405{
2406 /* make sure rcu-protected mac80211 tx path itself is drained */
2407 synchronize_net();
2408
2409 ath10k_offchan_tx_purge(ar);
2410 ath10k_mgmt_over_wmi_tx_purge(ar);
2411
2412 cancel_work_sync(&ar->offchan_tx_work);
2413 cancel_work_sync(&ar->wmi_mgmt_tx_work);
2414}
2415
Michal Kazioraffd3212013-07-16 09:54:35 +02002416void ath10k_halt(struct ath10k *ar)
Michal Kazior818bdd12013-07-16 09:38:57 +02002417{
Michal Kaziord9bc4b92014-04-23 19:30:06 +03002418 struct ath10k_vif *arvif;
2419
Michal Kazior818bdd12013-07-16 09:38:57 +02002420 lockdep_assert_held(&ar->conf_mutex);
2421
Michal Kazior19337472014-08-28 12:58:16 +02002422 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
2423 ar->filter_flags = 0;
2424 ar->monitor = false;
2425
2426 if (ar->monitor_started)
Michal Kazior1bbc0972014-04-08 09:45:47 +03002427 ath10k_monitor_stop(ar);
Michal Kazior19337472014-08-28 12:58:16 +02002428
2429 ar->monitor_started = false;
Michal Kazior1bbc0972014-04-08 09:45:47 +03002430
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002431 ath10k_scan_finish(ar);
Michal Kazior818bdd12013-07-16 09:38:57 +02002432 ath10k_peer_cleanup_all(ar);
2433 ath10k_core_stop(ar);
2434 ath10k_hif_power_down(ar);
2435
2436 spin_lock_bh(&ar->data_lock);
Michal Kazior64badcb2014-09-18 11:18:02 +03002437 list_for_each_entry(arvif, &ar->arvifs, list)
2438 ath10k_mac_vif_beacon_cleanup(arvif);
Michal Kazior818bdd12013-07-16 09:38:57 +02002439 spin_unlock_bh(&ar->data_lock);
2440}
2441
Ben Greear46acf7b2014-05-16 17:15:38 +03002442static int ath10k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
2443{
2444 struct ath10k *ar = hw->priv;
2445
2446 mutex_lock(&ar->conf_mutex);
2447
2448 if (ar->cfg_tx_chainmask) {
2449 *tx_ant = ar->cfg_tx_chainmask;
2450 *rx_ant = ar->cfg_rx_chainmask;
2451 } else {
2452 *tx_ant = ar->supp_tx_chainmask;
2453 *rx_ant = ar->supp_rx_chainmask;
2454 }
2455
2456 mutex_unlock(&ar->conf_mutex);
2457
2458 return 0;
2459}
2460
Ben Greear5572a952014-11-24 16:22:10 +02002461static void ath10k_check_chain_mask(struct ath10k *ar, u32 cm, const char *dbg)
2462{
2463 /* It is not clear that allowing gaps in chainmask
2464 * is helpful. Probably it will not do what user
2465 * is hoping for, so warn in that case.
2466 */
2467 if (cm == 15 || cm == 7 || cm == 3 || cm == 1 || cm == 0)
2468 return;
2469
2470 ath10k_warn(ar, "mac %s antenna chainmask may be invalid: 0x%x. Suggested values: 15, 7, 3, 1 or 0.\n",
2471 dbg, cm);
2472}
2473
Ben Greear46acf7b2014-05-16 17:15:38 +03002474static int __ath10k_set_antenna(struct ath10k *ar, u32 tx_ant, u32 rx_ant)
2475{
2476 int ret;
2477
2478 lockdep_assert_held(&ar->conf_mutex);
2479
Ben Greear5572a952014-11-24 16:22:10 +02002480 ath10k_check_chain_mask(ar, tx_ant, "tx");
2481 ath10k_check_chain_mask(ar, rx_ant, "rx");
2482
Ben Greear46acf7b2014-05-16 17:15:38 +03002483 ar->cfg_tx_chainmask = tx_ant;
2484 ar->cfg_rx_chainmask = rx_ant;
2485
2486 if ((ar->state != ATH10K_STATE_ON) &&
2487 (ar->state != ATH10K_STATE_RESTARTED))
2488 return 0;
2489
2490 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->tx_chain_mask,
2491 tx_ant);
2492 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002493 ath10k_warn(ar, "failed to set tx-chainmask: %d, req 0x%x\n",
Ben Greear46acf7b2014-05-16 17:15:38 +03002494 ret, tx_ant);
2495 return ret;
2496 }
2497
2498 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->rx_chain_mask,
2499 rx_ant);
2500 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002501 ath10k_warn(ar, "failed to set rx-chainmask: %d, req 0x%x\n",
Ben Greear46acf7b2014-05-16 17:15:38 +03002502 ret, rx_ant);
2503 return ret;
2504 }
2505
2506 return 0;
2507}
2508
2509static int ath10k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
2510{
2511 struct ath10k *ar = hw->priv;
2512 int ret;
2513
2514 mutex_lock(&ar->conf_mutex);
2515 ret = __ath10k_set_antenna(ar, tx_ant, rx_ant);
2516 mutex_unlock(&ar->conf_mutex);
2517 return ret;
2518}
2519
Kalle Valo5e3dd152013-06-12 20:52:10 +03002520static int ath10k_start(struct ieee80211_hw *hw)
2521{
2522 struct ath10k *ar = hw->priv;
Michal Kazior818bdd12013-07-16 09:38:57 +02002523 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002524
Michal Kaziorbca7baf2014-05-26 12:46:03 +03002525 /*
2526 * This makes sense only when restarting hw. It is harmless to call
2527 * uncoditionally. This is necessary to make sure no HTT/WMI tx
2528 * commands will be submitted while restarting.
2529 */
2530 ath10k_drain_tx(ar);
2531
Michal Kazior548db542013-07-05 16:15:15 +03002532 mutex_lock(&ar->conf_mutex);
2533
Michal Kaziorc5058f52014-05-26 12:46:03 +03002534 switch (ar->state) {
2535 case ATH10K_STATE_OFF:
2536 ar->state = ATH10K_STATE_ON;
2537 break;
2538 case ATH10K_STATE_RESTARTING:
2539 ath10k_halt(ar);
2540 ar->state = ATH10K_STATE_RESTARTED;
2541 break;
2542 case ATH10K_STATE_ON:
2543 case ATH10K_STATE_RESTARTED:
2544 case ATH10K_STATE_WEDGED:
2545 WARN_ON(1);
Michal Kazior818bdd12013-07-16 09:38:57 +02002546 ret = -EINVAL;
Michal Kaziorae254432014-05-26 12:46:02 +03002547 goto err;
Kalle Valo43d2a302014-09-10 18:23:30 +03002548 case ATH10K_STATE_UTF:
2549 ret = -EBUSY;
2550 goto err;
Michal Kazior818bdd12013-07-16 09:38:57 +02002551 }
2552
2553 ret = ath10k_hif_power_up(ar);
2554 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002555 ath10k_err(ar, "Could not init hif: %d\n", ret);
Michal Kaziorae254432014-05-26 12:46:02 +03002556 goto err_off;
Michal Kazior818bdd12013-07-16 09:38:57 +02002557 }
2558
Kalle Valo43d2a302014-09-10 18:23:30 +03002559 ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_NORMAL);
Michal Kazior818bdd12013-07-16 09:38:57 +02002560 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002561 ath10k_err(ar, "Could not init core: %d\n", ret);
Michal Kaziorae254432014-05-26 12:46:02 +03002562 goto err_power_down;
Michal Kazior818bdd12013-07-16 09:38:57 +02002563 }
2564
Bartosz Markowski226a3392013-09-26 17:47:16 +02002565 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->pmf_qos, 1);
Michal Kaziorae254432014-05-26 12:46:02 +03002566 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002567 ath10k_warn(ar, "failed to enable PMF QOS: %d\n", ret);
Michal Kaziorae254432014-05-26 12:46:02 +03002568 goto err_core_stop;
2569 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002570
Michal Kaziorc4dd0d02013-11-13 11:05:10 +01002571 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->dynamic_bw, 1);
Michal Kaziorae254432014-05-26 12:46:02 +03002572 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002573 ath10k_warn(ar, "failed to enable dynamic BW: %d\n", ret);
Michal Kaziorae254432014-05-26 12:46:02 +03002574 goto err_core_stop;
2575 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002576
Ben Greear46acf7b2014-05-16 17:15:38 +03002577 if (ar->cfg_tx_chainmask)
2578 __ath10k_set_antenna(ar, ar->cfg_tx_chainmask,
2579 ar->cfg_rx_chainmask);
2580
Marek Puzyniakab6258e2014-01-29 15:03:31 +02002581 /*
2582 * By default FW set ARP frames ac to voice (6). In that case ARP
2583 * exchange is not working properly for UAPSD enabled AP. ARP requests
2584 * which arrives with access category 0 are processed by network stack
2585 * and send back with access category 0, but FW changes access category
2586 * to 6. Set ARP frames access category to best effort (0) solves
2587 * this problem.
2588 */
2589
2590 ret = ath10k_wmi_pdev_set_param(ar,
2591 ar->wmi.pdev_param->arp_ac_override, 0);
2592 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002593 ath10k_warn(ar, "failed to set arp ac override parameter: %d\n",
Marek Puzyniakab6258e2014-01-29 15:03:31 +02002594 ret);
Michal Kaziorae254432014-05-26 12:46:02 +03002595 goto err_core_stop;
Marek Puzyniakab6258e2014-01-29 15:03:31 +02002596 }
2597
Michal Kaziord6500972014-04-08 09:56:09 +03002598 ar->num_started_vdevs = 0;
Michal Kaziorf7843d72013-07-16 09:38:52 +02002599 ath10k_regd_update(ar);
2600
Simon Wunderlich855aed12014-08-02 09:12:54 +03002601 ath10k_spectral_start(ar);
2602
Michal Kaziorae254432014-05-26 12:46:02 +03002603 mutex_unlock(&ar->conf_mutex);
2604 return 0;
2605
2606err_core_stop:
2607 ath10k_core_stop(ar);
2608
2609err_power_down:
2610 ath10k_hif_power_down(ar);
2611
2612err_off:
2613 ar->state = ATH10K_STATE_OFF;
2614
2615err:
Michal Kazior548db542013-07-05 16:15:15 +03002616 mutex_unlock(&ar->conf_mutex);
Michal Kaziorc60bdd82014-01-29 07:26:31 +01002617 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002618}
2619
2620static void ath10k_stop(struct ieee80211_hw *hw)
2621{
2622 struct ath10k *ar = hw->priv;
2623
Michal Kaziorbca7baf2014-05-26 12:46:03 +03002624 ath10k_drain_tx(ar);
2625
Michal Kazior548db542013-07-05 16:15:15 +03002626 mutex_lock(&ar->conf_mutex);
Michal Kaziorc5058f52014-05-26 12:46:03 +03002627 if (ar->state != ATH10K_STATE_OFF) {
Michal Kazior818bdd12013-07-16 09:38:57 +02002628 ath10k_halt(ar);
Michal Kaziorc5058f52014-05-26 12:46:03 +03002629 ar->state = ATH10K_STATE_OFF;
2630 }
Michal Kazior548db542013-07-05 16:15:15 +03002631 mutex_unlock(&ar->conf_mutex);
2632
Michal Kazior5c81c7f2014-08-05 14:54:44 +02002633 cancel_delayed_work_sync(&ar->scan.timeout);
Michal Kazioraffd3212013-07-16 09:54:35 +02002634 cancel_work_sync(&ar->restart_work);
2635}
2636
Michal Kaziorad088bf2013-10-16 15:44:46 +03002637static int ath10k_config_ps(struct ath10k *ar)
Michal Kazioraffd3212013-07-16 09:54:35 +02002638{
Michal Kaziorad088bf2013-10-16 15:44:46 +03002639 struct ath10k_vif *arvif;
2640 int ret = 0;
Michal Kazioraffd3212013-07-16 09:54:35 +02002641
2642 lockdep_assert_held(&ar->conf_mutex);
2643
Michal Kaziorad088bf2013-10-16 15:44:46 +03002644 list_for_each_entry(arvif, &ar->arvifs, list) {
2645 ret = ath10k_mac_vif_setup_ps(arvif);
2646 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002647 ath10k_warn(ar, "failed to setup powersave: %d\n", ret);
Michal Kaziorad088bf2013-10-16 15:44:46 +03002648 break;
2649 }
2650 }
Michal Kazioraffd3212013-07-16 09:54:35 +02002651
Michal Kaziorad088bf2013-10-16 15:44:46 +03002652 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002653}
2654
Michal Kaziorc930f742014-01-23 11:38:25 +01002655static const char *chandef_get_width(enum nl80211_chan_width width)
2656{
2657 switch (width) {
2658 case NL80211_CHAN_WIDTH_20_NOHT:
2659 return "20 (noht)";
2660 case NL80211_CHAN_WIDTH_20:
2661 return "20";
2662 case NL80211_CHAN_WIDTH_40:
2663 return "40";
2664 case NL80211_CHAN_WIDTH_80:
2665 return "80";
2666 case NL80211_CHAN_WIDTH_80P80:
2667 return "80+80";
2668 case NL80211_CHAN_WIDTH_160:
2669 return "160";
2670 case NL80211_CHAN_WIDTH_5:
2671 return "5";
2672 case NL80211_CHAN_WIDTH_10:
2673 return "10";
2674 }
2675 return "?";
2676}
2677
2678static void ath10k_config_chan(struct ath10k *ar)
2679{
2680 struct ath10k_vif *arvif;
Michal Kaziorc930f742014-01-23 11:38:25 +01002681 int ret;
2682
2683 lockdep_assert_held(&ar->conf_mutex);
2684
Michal Kazior7aa7a722014-08-25 12:09:38 +02002685 ath10k_dbg(ar, ATH10K_DBG_MAC,
Michal Kaziorc930f742014-01-23 11:38:25 +01002686 "mac config channel to %dMHz (cf1 %dMHz cf2 %dMHz width %s)\n",
2687 ar->chandef.chan->center_freq,
2688 ar->chandef.center_freq1,
2689 ar->chandef.center_freq2,
2690 chandef_get_width(ar->chandef.width));
2691
2692 /* First stop monitor interface. Some FW versions crash if there's a
2693 * lone monitor interface. */
Michal Kazior1bbc0972014-04-08 09:45:47 +03002694 if (ar->monitor_started)
Michal Kazior19337472014-08-28 12:58:16 +02002695 ath10k_monitor_stop(ar);
Michal Kaziorc930f742014-01-23 11:38:25 +01002696
2697 list_for_each_entry(arvif, &ar->arvifs, list) {
2698 if (!arvif->is_started)
2699 continue;
2700
Michal Kaziordc55e302014-07-29 12:53:36 +03002701 if (!arvif->is_up)
2702 continue;
2703
Michal Kaziorc930f742014-01-23 11:38:25 +01002704 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
2705 continue;
2706
Michal Kaziordc55e302014-07-29 12:53:36 +03002707 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
Michal Kaziorc930f742014-01-23 11:38:25 +01002708 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002709 ath10k_warn(ar, "failed to down vdev %d: %d\n",
Michal Kaziorc930f742014-01-23 11:38:25 +01002710 arvif->vdev_id, ret);
2711 continue;
2712 }
2713 }
2714
Michal Kaziordc55e302014-07-29 12:53:36 +03002715 /* all vdevs are downed now - attempt to restart and re-up them */
Michal Kaziorc930f742014-01-23 11:38:25 +01002716
2717 list_for_each_entry(arvif, &ar->arvifs, list) {
2718 if (!arvif->is_started)
2719 continue;
2720
2721 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
2722 continue;
2723
Michal Kaziordc55e302014-07-29 12:53:36 +03002724 ret = ath10k_vdev_restart(arvif);
Michal Kaziorc930f742014-01-23 11:38:25 +01002725 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002726 ath10k_warn(ar, "failed to restart vdev %d: %d\n",
Michal Kaziorc930f742014-01-23 11:38:25 +01002727 arvif->vdev_id, ret);
2728 continue;
2729 }
2730
2731 if (!arvif->is_up)
2732 continue;
2733
2734 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
2735 arvif->bssid);
2736 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002737 ath10k_warn(ar, "failed to bring vdev up %d: %d\n",
Michal Kaziorc930f742014-01-23 11:38:25 +01002738 arvif->vdev_id, ret);
2739 continue;
2740 }
2741 }
2742
Michal Kazior19337472014-08-28 12:58:16 +02002743 ath10k_monitor_recalc(ar);
Michal Kaziorc930f742014-01-23 11:38:25 +01002744}
2745
Michal Kazior7d9d5582014-10-21 10:40:15 +03002746static int ath10k_mac_txpower_setup(struct ath10k *ar, int txpower)
2747{
2748 int ret;
2749 u32 param;
2750
2751 lockdep_assert_held(&ar->conf_mutex);
2752
2753 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac txpower %d\n", txpower);
2754
2755 param = ar->wmi.pdev_param->txpower_limit2g;
2756 ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
2757 if (ret) {
2758 ath10k_warn(ar, "failed to set 2g txpower %d: %d\n",
2759 txpower, ret);
2760 return ret;
2761 }
2762
2763 param = ar->wmi.pdev_param->txpower_limit5g;
2764 ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
2765 if (ret) {
2766 ath10k_warn(ar, "failed to set 5g txpower %d: %d\n",
2767 txpower, ret);
2768 return ret;
2769 }
2770
2771 return 0;
2772}
2773
2774static int ath10k_mac_txpower_recalc(struct ath10k *ar)
2775{
2776 struct ath10k_vif *arvif;
2777 int ret, txpower = -1;
2778
2779 lockdep_assert_held(&ar->conf_mutex);
2780
2781 list_for_each_entry(arvif, &ar->arvifs, list) {
2782 WARN_ON(arvif->txpower < 0);
2783
2784 if (txpower == -1)
2785 txpower = arvif->txpower;
2786 else
2787 txpower = min(txpower, arvif->txpower);
2788 }
2789
2790 if (WARN_ON(txpower == -1))
2791 return -EINVAL;
2792
2793 ret = ath10k_mac_txpower_setup(ar, txpower);
2794 if (ret) {
2795 ath10k_warn(ar, "failed to setup tx power %d: %d\n",
2796 txpower, ret);
2797 return ret;
2798 }
2799
2800 return 0;
2801}
2802
Kalle Valo5e3dd152013-06-12 20:52:10 +03002803static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
2804{
Kalle Valo5e3dd152013-06-12 20:52:10 +03002805 struct ath10k *ar = hw->priv;
2806 struct ieee80211_conf *conf = &hw->conf;
2807 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002808
2809 mutex_lock(&ar->conf_mutex);
2810
2811 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002812 ath10k_dbg(ar, ATH10K_DBG_MAC,
Michal Kaziord6500972014-04-08 09:56:09 +03002813 "mac config channel %dMHz flags 0x%x radar %d\n",
Marek Puzyniake8a50f82013-11-20 09:59:47 +02002814 conf->chandef.chan->center_freq,
Michal Kaziord6500972014-04-08 09:56:09 +03002815 conf->chandef.chan->flags,
2816 conf->radar_enabled);
Marek Puzyniake8a50f82013-11-20 09:59:47 +02002817
Kalle Valo5e3dd152013-06-12 20:52:10 +03002818 spin_lock_bh(&ar->data_lock);
2819 ar->rx_channel = conf->chandef.chan;
2820 spin_unlock_bh(&ar->data_lock);
Marek Puzyniake8a50f82013-11-20 09:59:47 +02002821
Michal Kaziord6500972014-04-08 09:56:09 +03002822 ar->radar_enabled = conf->radar_enabled;
2823 ath10k_recalc_radar_detection(ar);
Michal Kaziorc930f742014-01-23 11:38:25 +01002824
2825 if (!cfg80211_chandef_identical(&ar->chandef, &conf->chandef)) {
2826 ar->chandef = conf->chandef;
2827 ath10k_config_chan(ar);
2828 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002829 }
2830
Michal Kazioraffd3212013-07-16 09:54:35 +02002831 if (changed & IEEE80211_CONF_CHANGE_PS)
2832 ath10k_config_ps(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002833
2834 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
Michal Kazior19337472014-08-28 12:58:16 +02002835 ar->monitor = conf->flags & IEEE80211_CONF_MONITOR;
2836 ret = ath10k_monitor_recalc(ar);
2837 if (ret)
2838 ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002839 }
2840
2841 mutex_unlock(&ar->conf_mutex);
2842 return ret;
2843}
2844
Ben Greear5572a952014-11-24 16:22:10 +02002845static u32 get_nss_from_chainmask(u16 chain_mask)
2846{
2847 if ((chain_mask & 0x15) == 0x15)
2848 return 4;
2849 else if ((chain_mask & 0x7) == 0x7)
2850 return 3;
2851 else if ((chain_mask & 0x3) == 0x3)
2852 return 2;
2853 return 1;
2854}
2855
Kalle Valo5e3dd152013-06-12 20:52:10 +03002856/*
2857 * TODO:
2858 * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
2859 * because we will send mgmt frames without CCK. This requirement
2860 * for P2P_FIND/GO_NEG should be handled by checking CCK flag
2861 * in the TX packet.
2862 */
2863static int ath10k_add_interface(struct ieee80211_hw *hw,
2864 struct ieee80211_vif *vif)
2865{
2866 struct ath10k *ar = hw->priv;
2867 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2868 enum wmi_sta_powersave_param param;
2869 int ret = 0;
Kalle Valo5a13e762014-01-20 11:01:46 +02002870 u32 value;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002871 int bit;
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02002872 u32 vdev_param;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002873
Johannes Berg848955c2014-11-11 12:48:42 +01002874 vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
2875
Kalle Valo5e3dd152013-06-12 20:52:10 +03002876 mutex_lock(&ar->conf_mutex);
2877
Michal Kazior0dbd09e2013-07-31 10:55:14 +02002878 memset(arvif, 0, sizeof(*arvif));
2879
Kalle Valo5e3dd152013-06-12 20:52:10 +03002880 arvif->ar = ar;
2881 arvif->vif = vif;
2882
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002883 INIT_WORK(&arvif->wep_key_work, ath10k_tx_wep_key_work);
Ben Greeare63b33f2013-10-22 14:54:14 -07002884 INIT_LIST_HEAD(&arvif->list);
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002885
Ben Greeara9aefb32014-08-12 11:02:19 +03002886 if (ar->free_vdev_map == 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002887 ath10k_warn(ar, "Free vdev map is empty, no more interfaces allowed.\n");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002888 ret = -EBUSY;
Michal Kazior9dad14a2013-10-16 15:44:45 +03002889 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002890 }
Ben Greear16c11172014-09-23 14:17:16 -07002891 bit = __ffs64(ar->free_vdev_map);
Kalle Valo5e3dd152013-06-12 20:52:10 +03002892
Ben Greear16c11172014-09-23 14:17:16 -07002893 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac create vdev %i map %llx\n",
2894 bit, ar->free_vdev_map);
2895
2896 arvif->vdev_id = bit;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002897 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002898
2899 if (ar->p2p)
2900 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
2901
2902 switch (vif->type) {
2903 case NL80211_IFTYPE_UNSPECIFIED:
2904 case NL80211_IFTYPE_STATION:
2905 arvif->vdev_type = WMI_VDEV_TYPE_STA;
2906 if (vif->p2p)
2907 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
2908 break;
2909 case NL80211_IFTYPE_ADHOC:
2910 arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
2911 break;
2912 case NL80211_IFTYPE_AP:
2913 arvif->vdev_type = WMI_VDEV_TYPE_AP;
2914
2915 if (vif->p2p)
2916 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
2917 break;
2918 case NL80211_IFTYPE_MONITOR:
2919 arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
2920 break;
2921 default:
2922 WARN_ON(1);
2923 break;
2924 }
2925
Michal Kazior64badcb2014-09-18 11:18:02 +03002926 /* Some firmware revisions don't wait for beacon tx completion before
2927 * sending another SWBA event. This could lead to hardware using old
2928 * (freed) beacon data in some cases, e.g. tx credit starvation
2929 * combined with missed TBTT. This is very very rare.
2930 *
2931 * On non-IOMMU-enabled hosts this could be a possible security issue
2932 * because hw could beacon some random data on the air. On
2933 * IOMMU-enabled hosts DMAR faults would occur in most cases and target
2934 * device would crash.
2935 *
2936 * Since there are no beacon tx completions (implicit nor explicit)
2937 * propagated to host the only workaround for this is to allocate a
2938 * DMA-coherent buffer for a lifetime of a vif and use it for all
2939 * beacon tx commands. Worst case for this approach is some beacons may
2940 * become corrupted, e.g. have garbled IEs or out-of-date TIM bitmap.
2941 */
2942 if (vif->type == NL80211_IFTYPE_ADHOC ||
2943 vif->type == NL80211_IFTYPE_AP) {
2944 arvif->beacon_buf = dma_zalloc_coherent(ar->dev,
2945 IEEE80211_MAX_FRAME_LEN,
2946 &arvif->beacon_paddr,
Rajkumar Manoharan82d7aba2014-10-10 17:38:27 +05302947 GFP_ATOMIC);
Michal Kazior64badcb2014-09-18 11:18:02 +03002948 if (!arvif->beacon_buf) {
2949 ret = -ENOMEM;
2950 ath10k_warn(ar, "failed to allocate beacon buffer: %d\n",
2951 ret);
2952 goto err;
2953 }
2954 }
2955
2956 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d bcnmode %s\n",
2957 arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
2958 arvif->beacon_buf ? "single-buf" : "per-skb");
Kalle Valo5e3dd152013-06-12 20:52:10 +03002959
2960 ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
2961 arvif->vdev_subtype, vif->addr);
2962 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002963 ath10k_warn(ar, "failed to create WMI vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02002964 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002965 goto err;
Kalle Valo5e3dd152013-06-12 20:52:10 +03002966 }
2967
Ben Greear16c11172014-09-23 14:17:16 -07002968 ar->free_vdev_map &= ~(1LL << arvif->vdev_id);
Michal Kazior05791192013-10-16 15:44:45 +03002969 list_add(&arvif->list, &ar->arvifs);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002970
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02002971 vdev_param = ar->wmi.vdev_param->def_keyid;
2972 ret = ath10k_wmi_vdev_set_param(ar, 0, vdev_param,
Michal Kaziorcc4827b2013-10-16 15:44:45 +03002973 arvif->def_wep_key_idx);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002974 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002975 ath10k_warn(ar, "failed to set vdev %i default key id: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02002976 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002977 goto err_vdev_delete;
2978 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002979
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02002980 vdev_param = ar->wmi.vdev_param->tx_encap_type;
2981 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03002982 ATH10K_HW_TXRX_NATIVE_WIFI);
Bartosz Markowskiebc9abd2013-10-15 09:26:20 +02002983 /* 10.X firmware does not support this VDEV parameter. Do not warn */
Michal Kazior9dad14a2013-10-16 15:44:45 +03002984 if (ret && ret != -EOPNOTSUPP) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02002985 ath10k_warn(ar, "failed to set vdev %i TX encapsulation: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02002986 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03002987 goto err_vdev_delete;
2988 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03002989
Ben Greear5572a952014-11-24 16:22:10 +02002990 if (ar->cfg_tx_chainmask) {
2991 u16 nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
2992
2993 vdev_param = ar->wmi.vdev_param->nss;
2994 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
2995 nss);
2996 if (ret) {
2997 ath10k_warn(ar, "failed to set vdev %i chainmask 0x%x, nss %i: %d\n",
2998 arvif->vdev_id, ar->cfg_tx_chainmask, nss,
2999 ret);
3000 goto err_vdev_delete;
3001 }
3002 }
3003
Kalle Valo5e3dd152013-06-12 20:52:10 +03003004 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3005 ret = ath10k_peer_create(ar, arvif->vdev_id, vif->addr);
3006 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003007 ath10k_warn(ar, "failed to create vdev %i peer for AP: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003008 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003009 goto err_vdev_delete;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003010 }
Marek Puzyniakcdf07402013-12-30 09:07:51 +01003011
Kalle Valo5a13e762014-01-20 11:01:46 +02003012 ret = ath10k_mac_set_kickout(arvif);
3013 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003014 ath10k_warn(ar, "failed to set vdev %i kickout parameters: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003015 arvif->vdev_id, ret);
Kalle Valo5a13e762014-01-20 11:01:46 +02003016 goto err_peer_delete;
3017 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003018 }
3019
3020 if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
3021 param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
3022 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
3023 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3024 param, value);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003025 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003026 ath10k_warn(ar, "failed to set vdev %i RX wake policy: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003027 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003028 goto err_peer_delete;
3029 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003030
3031 param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
3032 value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
3033 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3034 param, value);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003035 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003036 ath10k_warn(ar, "failed to set vdev %i TX wake thresh: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003037 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003038 goto err_peer_delete;
3039 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003040
3041 param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
3042 value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
3043 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3044 param, value);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003045 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003046 ath10k_warn(ar, "failed to set vdev %i PSPOLL count: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003047 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003048 goto err_peer_delete;
3049 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003050 }
3051
Michal Kazior424121c2013-07-22 14:13:31 +02003052 ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003053 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003054 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
Michal Kazior679c54a2013-07-05 16:15:04 +03003055 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003056 goto err_peer_delete;
3057 }
Michal Kazior679c54a2013-07-05 16:15:04 +03003058
Michal Kazior424121c2013-07-22 14:13:31 +02003059 ret = ath10k_mac_set_frag(arvif, ar->hw->wiphy->frag_threshold);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003060 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003061 ath10k_warn(ar, "failed to set frag threshold for vdev %d: %d\n",
Michal Kazior679c54a2013-07-05 16:15:04 +03003062 arvif->vdev_id, ret);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003063 goto err_peer_delete;
3064 }
Michal Kazior679c54a2013-07-05 16:15:04 +03003065
Michal Kazior7d9d5582014-10-21 10:40:15 +03003066 arvif->txpower = vif->bss_conf.txpower;
3067 ret = ath10k_mac_txpower_recalc(ar);
3068 if (ret) {
3069 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
3070 goto err_peer_delete;
3071 }
3072
Kalle Valo5e3dd152013-06-12 20:52:10 +03003073 mutex_unlock(&ar->conf_mutex);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003074 return 0;
3075
3076err_peer_delete:
3077 if (arvif->vdev_type == WMI_VDEV_TYPE_AP)
3078 ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
3079
3080err_vdev_delete:
3081 ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
Ben Greear16c11172014-09-23 14:17:16 -07003082 ar->free_vdev_map |= 1LL << arvif->vdev_id;
Michal Kazior05791192013-10-16 15:44:45 +03003083 list_del(&arvif->list);
Michal Kazior9dad14a2013-10-16 15:44:45 +03003084
3085err:
Michal Kazior64badcb2014-09-18 11:18:02 +03003086 if (arvif->beacon_buf) {
3087 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
3088 arvif->beacon_buf, arvif->beacon_paddr);
3089 arvif->beacon_buf = NULL;
3090 }
3091
Michal Kazior9dad14a2013-10-16 15:44:45 +03003092 mutex_unlock(&ar->conf_mutex);
3093
Kalle Valo5e3dd152013-06-12 20:52:10 +03003094 return ret;
3095}
3096
3097static void ath10k_remove_interface(struct ieee80211_hw *hw,
3098 struct ieee80211_vif *vif)
3099{
3100 struct ath10k *ar = hw->priv;
3101 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3102 int ret;
3103
Michal Kaziorcc4827b2013-10-16 15:44:45 +03003104 cancel_work_sync(&arvif->wep_key_work);
3105
Sujith Manoharan5d011f52014-11-25 11:47:00 +05303106 mutex_lock(&ar->conf_mutex);
3107
Michal Kaziored543882013-09-13 14:16:56 +02003108 spin_lock_bh(&ar->data_lock);
Michal Kazior64badcb2014-09-18 11:18:02 +03003109 ath10k_mac_vif_beacon_cleanup(arvif);
Michal Kaziored543882013-09-13 14:16:56 +02003110 spin_unlock_bh(&ar->data_lock);
3111
Simon Wunderlich855aed12014-08-02 09:12:54 +03003112 ret = ath10k_spectral_vif_stop(arvif);
3113 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003114 ath10k_warn(ar, "failed to stop spectral for vdev %i: %d\n",
Simon Wunderlich855aed12014-08-02 09:12:54 +03003115 arvif->vdev_id, ret);
3116
Ben Greear16c11172014-09-23 14:17:16 -07003117 ar->free_vdev_map |= 1LL << arvif->vdev_id;
Michal Kazior05791192013-10-16 15:44:45 +03003118 list_del(&arvif->list);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003119
3120 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3121 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, vif->addr);
3122 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003123 ath10k_warn(ar, "failed to remove peer for AP vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003124 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003125
3126 kfree(arvif->u.ap.noa_data);
3127 }
3128
Michal Kazior7aa7a722014-08-25 12:09:38 +02003129 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i delete (remove interface)\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03003130 arvif->vdev_id);
3131
Kalle Valo5e3dd152013-06-12 20:52:10 +03003132 ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
3133 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003134 ath10k_warn(ar, "failed to delete WMI vdev %i: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003135 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003136
Kalle Valo5e3dd152013-06-12 20:52:10 +03003137 ath10k_peer_cleanup(ar, arvif->vdev_id);
3138
3139 mutex_unlock(&ar->conf_mutex);
3140}
3141
3142/*
3143 * FIXME: Has to be verified.
3144 */
3145#define SUPPORTED_FILTERS \
3146 (FIF_PROMISC_IN_BSS | \
3147 FIF_ALLMULTI | \
3148 FIF_CONTROL | \
3149 FIF_PSPOLL | \
3150 FIF_OTHER_BSS | \
3151 FIF_BCN_PRBRESP_PROMISC | \
3152 FIF_PROBE_REQ | \
3153 FIF_FCSFAIL)
3154
3155static void ath10k_configure_filter(struct ieee80211_hw *hw,
3156 unsigned int changed_flags,
3157 unsigned int *total_flags,
3158 u64 multicast)
3159{
3160 struct ath10k *ar = hw->priv;
3161 int ret;
3162
3163 mutex_lock(&ar->conf_mutex);
3164
3165 changed_flags &= SUPPORTED_FILTERS;
3166 *total_flags &= SUPPORTED_FILTERS;
3167 ar->filter_flags = *total_flags;
3168
Michal Kazior19337472014-08-28 12:58:16 +02003169 ret = ath10k_monitor_recalc(ar);
3170 if (ret)
3171 ath10k_warn(ar, "failed to recalc montior: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003172
3173 mutex_unlock(&ar->conf_mutex);
3174}
3175
3176static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
3177 struct ieee80211_vif *vif,
3178 struct ieee80211_bss_conf *info,
3179 u32 changed)
3180{
3181 struct ath10k *ar = hw->priv;
3182 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3183 int ret = 0;
Kalle Valoaf762c02014-09-14 12:50:17 +03003184 u32 vdev_param, pdev_param, slottime, preamble;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003185
3186 mutex_lock(&ar->conf_mutex);
3187
3188 if (changed & BSS_CHANGED_IBSS)
3189 ath10k_control_ibss(arvif, info, vif->addr);
3190
3191 if (changed & BSS_CHANGED_BEACON_INT) {
3192 arvif->beacon_interval = info->beacon_int;
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003193 vdev_param = ar->wmi.vdev_param->beacon_interval;
3194 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003195 arvif->beacon_interval);
Michal Kazior7aa7a722014-08-25 12:09:38 +02003196 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003197 "mac vdev %d beacon_interval %d\n",
3198 arvif->vdev_id, arvif->beacon_interval);
3199
Kalle Valo5e3dd152013-06-12 20:52:10 +03003200 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003201 ath10k_warn(ar, "failed to set beacon interval for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003202 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003203 }
3204
3205 if (changed & BSS_CHANGED_BEACON) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003206 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003207 "vdev %d set beacon tx mode to staggered\n",
3208 arvif->vdev_id);
3209
Bartosz Markowski226a3392013-09-26 17:47:16 +02003210 pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
3211 ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003212 WMI_BEACON_STAGGERED_MODE);
3213 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003214 ath10k_warn(ar, "failed to set beacon mode for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003215 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003216 }
3217
John W. Linvilleb70727e2013-06-13 13:34:29 -04003218 if (changed & BSS_CHANGED_BEACON_INFO) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03003219 arvif->dtim_period = info->dtim_period;
3220
Michal Kazior7aa7a722014-08-25 12:09:38 +02003221 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003222 "mac vdev %d dtim_period %d\n",
3223 arvif->vdev_id, arvif->dtim_period);
3224
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003225 vdev_param = ar->wmi.vdev_param->dtim_period;
3226 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003227 arvif->dtim_period);
3228 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003229 ath10k_warn(ar, "failed to set dtim period for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003230 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003231 }
3232
3233 if (changed & BSS_CHANGED_SSID &&
3234 vif->type == NL80211_IFTYPE_AP) {
3235 arvif->u.ap.ssid_len = info->ssid_len;
3236 if (info->ssid_len)
3237 memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
3238 arvif->u.ap.hidden_ssid = info->hidden_ssid;
3239 }
3240
Michal Kazior077efc82014-10-21 10:10:29 +03003241 if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
3242 ether_addr_copy(arvif->bssid, info->bssid);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003243
3244 if (changed & BSS_CHANGED_BEACON_ENABLED)
3245 ath10k_control_beaconing(arvif, info);
3246
3247 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02003248 arvif->use_cts_prot = info->use_cts_prot;
Michal Kazior7aa7a722014-08-25 12:09:38 +02003249 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d cts_prot %d\n",
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02003250 arvif->vdev_id, info->use_cts_prot);
Kalle Valo60c3daa2013-09-08 17:56:07 +03003251
Marek Kwaczynskie81bd102014-03-11 12:58:00 +02003252 ret = ath10k_recalc_rtscts_prot(arvif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003253 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003254 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003255 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003256 }
3257
3258 if (changed & BSS_CHANGED_ERP_SLOT) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03003259 if (info->use_short_slot)
3260 slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
3261
3262 else
3263 slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
3264
Michal Kazior7aa7a722014-08-25 12:09:38 +02003265 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03003266 arvif->vdev_id, slottime);
3267
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003268 vdev_param = ar->wmi.vdev_param->slot_time;
3269 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003270 slottime);
3271 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003272 ath10k_warn(ar, "failed to set erp slot for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003273 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003274 }
3275
3276 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03003277 if (info->use_short_preamble)
3278 preamble = WMI_VDEV_PREAMBLE_SHORT;
3279 else
3280 preamble = WMI_VDEV_PREAMBLE_LONG;
3281
Michal Kazior7aa7a722014-08-25 12:09:38 +02003282 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003283 "mac vdev %d preamble %dn",
3284 arvif->vdev_id, preamble);
3285
Bartosz Markowski6d1506e2013-09-26 17:47:15 +02003286 vdev_param = ar->wmi.vdev_param->preamble;
3287 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
Kalle Valo5e3dd152013-06-12 20:52:10 +03003288 preamble);
3289 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003290 ath10k_warn(ar, "failed to set preamble for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003291 arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003292 }
3293
3294 if (changed & BSS_CHANGED_ASSOC) {
Michal Kaziore556f112014-08-28 12:58:17 +02003295 if (info->assoc) {
3296 /* Workaround: Make sure monitor vdev is not running
3297 * when associating to prevent some firmware revisions
3298 * (e.g. 10.1 and 10.2) from crashing.
3299 */
3300 if (ar->monitor_started)
3301 ath10k_monitor_stop(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003302 ath10k_bss_assoc(hw, vif, info);
Michal Kaziore556f112014-08-28 12:58:17 +02003303 ath10k_monitor_recalc(ar);
Michal Kazior077efc82014-10-21 10:10:29 +03003304 } else {
3305 ath10k_bss_disassoc(hw, vif);
Michal Kaziore556f112014-08-28 12:58:17 +02003306 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003307 }
3308
Michal Kazior7d9d5582014-10-21 10:40:15 +03003309 if (changed & BSS_CHANGED_TXPOWER) {
3310 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev_id %i txpower %d\n",
3311 arvif->vdev_id, info->txpower);
3312
3313 arvif->txpower = info->txpower;
3314 ret = ath10k_mac_txpower_recalc(ar);
3315 if (ret)
3316 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
3317 }
3318
Kalle Valo5e3dd152013-06-12 20:52:10 +03003319 mutex_unlock(&ar->conf_mutex);
3320}
3321
3322static int ath10k_hw_scan(struct ieee80211_hw *hw,
3323 struct ieee80211_vif *vif,
David Spinadelc56ef672014-02-05 15:21:13 +02003324 struct ieee80211_scan_request *hw_req)
Kalle Valo5e3dd152013-06-12 20:52:10 +03003325{
3326 struct ath10k *ar = hw->priv;
3327 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
David Spinadelc56ef672014-02-05 15:21:13 +02003328 struct cfg80211_scan_request *req = &hw_req->req;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003329 struct wmi_start_scan_arg arg;
3330 int ret = 0;
3331 int i;
3332
3333 mutex_lock(&ar->conf_mutex);
3334
3335 spin_lock_bh(&ar->data_lock);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003336 switch (ar->scan.state) {
3337 case ATH10K_SCAN_IDLE:
3338 reinit_completion(&ar->scan.started);
3339 reinit_completion(&ar->scan.completed);
3340 ar->scan.state = ATH10K_SCAN_STARTING;
3341 ar->scan.is_roc = false;
3342 ar->scan.vdev_id = arvif->vdev_id;
3343 ret = 0;
3344 break;
3345 case ATH10K_SCAN_STARTING:
3346 case ATH10K_SCAN_RUNNING:
3347 case ATH10K_SCAN_ABORTING:
Kalle Valo5e3dd152013-06-12 20:52:10 +03003348 ret = -EBUSY;
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003349 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003350 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003351 spin_unlock_bh(&ar->data_lock);
3352
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003353 if (ret)
3354 goto exit;
3355
Kalle Valo5e3dd152013-06-12 20:52:10 +03003356 memset(&arg, 0, sizeof(arg));
3357 ath10k_wmi_start_scan_init(ar, &arg);
3358 arg.vdev_id = arvif->vdev_id;
3359 arg.scan_id = ATH10K_SCAN_ID;
3360
3361 if (!req->no_cck)
3362 arg.scan_ctrl_flags |= WMI_SCAN_ADD_CCK_RATES;
3363
3364 if (req->ie_len) {
3365 arg.ie_len = req->ie_len;
3366 memcpy(arg.ie, req->ie, arg.ie_len);
3367 }
3368
3369 if (req->n_ssids) {
3370 arg.n_ssids = req->n_ssids;
3371 for (i = 0; i < arg.n_ssids; i++) {
3372 arg.ssids[i].len = req->ssids[i].ssid_len;
3373 arg.ssids[i].ssid = req->ssids[i].ssid;
3374 }
Michal Kaziordcd4a562013-07-31 10:55:12 +02003375 } else {
3376 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003377 }
3378
3379 if (req->n_channels) {
3380 arg.n_channels = req->n_channels;
3381 for (i = 0; i < arg.n_channels; i++)
3382 arg.channels[i] = req->channels[i]->center_freq;
3383 }
3384
3385 ret = ath10k_start_scan(ar, &arg);
3386 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003387 ath10k_warn(ar, "failed to start hw scan: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003388 spin_lock_bh(&ar->data_lock);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003389 ar->scan.state = ATH10K_SCAN_IDLE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003390 spin_unlock_bh(&ar->data_lock);
3391 }
3392
3393exit:
3394 mutex_unlock(&ar->conf_mutex);
3395 return ret;
3396}
3397
3398static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
3399 struct ieee80211_vif *vif)
3400{
3401 struct ath10k *ar = hw->priv;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003402
3403 mutex_lock(&ar->conf_mutex);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003404 ath10k_scan_abort(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003405 mutex_unlock(&ar->conf_mutex);
Michal Kazior4eb2e162014-10-28 10:23:09 +01003406
3407 cancel_delayed_work_sync(&ar->scan.timeout);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003408}
3409
Michal Kaziorcfb27d22013-12-02 09:06:36 +01003410static void ath10k_set_key_h_def_keyidx(struct ath10k *ar,
3411 struct ath10k_vif *arvif,
3412 enum set_key_cmd cmd,
3413 struct ieee80211_key_conf *key)
3414{
3415 u32 vdev_param = arvif->ar->wmi.vdev_param->def_keyid;
3416 int ret;
3417
3418 /* 10.1 firmware branch requires default key index to be set to group
3419 * key index after installing it. Otherwise FW/HW Txes corrupted
3420 * frames with multi-vif APs. This is not required for main firmware
3421 * branch (e.g. 636).
3422 *
3423 * FIXME: This has been tested only in AP. It remains unknown if this
3424 * is required for multi-vif STA interfaces on 10.1 */
3425
3426 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
3427 return;
3428
3429 if (key->cipher == WLAN_CIPHER_SUITE_WEP40)
3430 return;
3431
3432 if (key->cipher == WLAN_CIPHER_SUITE_WEP104)
3433 return;
3434
3435 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3436 return;
3437
3438 if (cmd != SET_KEY)
3439 return;
3440
3441 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3442 key->keyidx);
3443 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003444 ath10k_warn(ar, "failed to set vdev %i group key as default key: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003445 arvif->vdev_id, ret);
Michal Kaziorcfb27d22013-12-02 09:06:36 +01003446}
3447
Kalle Valo5e3dd152013-06-12 20:52:10 +03003448static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
3449 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
3450 struct ieee80211_key_conf *key)
3451{
3452 struct ath10k *ar = hw->priv;
3453 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3454 struct ath10k_peer *peer;
3455 const u8 *peer_addr;
3456 bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3457 key->cipher == WLAN_CIPHER_SUITE_WEP104;
3458 int ret = 0;
3459
3460 if (key->keyidx > WMI_MAX_KEY_INDEX)
3461 return -ENOSPC;
3462
3463 mutex_lock(&ar->conf_mutex);
3464
3465 if (sta)
3466 peer_addr = sta->addr;
3467 else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
3468 peer_addr = vif->bss_conf.bssid;
3469 else
3470 peer_addr = vif->addr;
3471
3472 key->hw_key_idx = key->keyidx;
3473
3474 /* the peer should not disappear in mid-way (unless FW goes awry) since
3475 * we already hold conf_mutex. we just make sure its there now. */
3476 spin_lock_bh(&ar->data_lock);
3477 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
3478 spin_unlock_bh(&ar->data_lock);
3479
3480 if (!peer) {
3481 if (cmd == SET_KEY) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003482 ath10k_warn(ar, "failed to install key for non-existent peer %pM\n",
Kalle Valo5e3dd152013-06-12 20:52:10 +03003483 peer_addr);
3484 ret = -EOPNOTSUPP;
3485 goto exit;
3486 } else {
3487 /* if the peer doesn't exist there is no key to disable
3488 * anymore */
3489 goto exit;
3490 }
3491 }
3492
3493 if (is_wep) {
3494 if (cmd == SET_KEY)
3495 arvif->wep_keys[key->keyidx] = key;
3496 else
3497 arvif->wep_keys[key->keyidx] = NULL;
3498
3499 if (cmd == DISABLE_KEY)
3500 ath10k_clear_vdev_key(arvif, key);
3501 }
3502
3503 ret = ath10k_install_key(arvif, key, cmd, peer_addr);
3504 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003505 ath10k_warn(ar, "failed to install key for vdev %i peer %pM: %d\n",
Ben Greear69244e52014-02-27 18:50:00 +02003506 arvif->vdev_id, peer_addr, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003507 goto exit;
3508 }
3509
Michal Kaziorcfb27d22013-12-02 09:06:36 +01003510 ath10k_set_key_h_def_keyidx(ar, arvif, cmd, key);
3511
Kalle Valo5e3dd152013-06-12 20:52:10 +03003512 spin_lock_bh(&ar->data_lock);
3513 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
3514 if (peer && cmd == SET_KEY)
3515 peer->keys[key->keyidx] = key;
3516 else if (peer && cmd == DISABLE_KEY)
3517 peer->keys[key->keyidx] = NULL;
3518 else if (peer == NULL)
3519 /* impossible unless FW goes crazy */
Michal Kazior7aa7a722014-08-25 12:09:38 +02003520 ath10k_warn(ar, "Peer %pM disappeared!\n", peer_addr);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003521 spin_unlock_bh(&ar->data_lock);
3522
3523exit:
3524 mutex_unlock(&ar->conf_mutex);
3525 return ret;
3526}
3527
Michal Kazior9797feb2014-02-14 14:49:48 +01003528static void ath10k_sta_rc_update_wk(struct work_struct *wk)
3529{
3530 struct ath10k *ar;
3531 struct ath10k_vif *arvif;
3532 struct ath10k_sta *arsta;
3533 struct ieee80211_sta *sta;
3534 u32 changed, bw, nss, smps;
3535 int err;
3536
3537 arsta = container_of(wk, struct ath10k_sta, update_wk);
3538 sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
3539 arvif = arsta->arvif;
3540 ar = arvif->ar;
3541
3542 spin_lock_bh(&ar->data_lock);
3543
3544 changed = arsta->changed;
3545 arsta->changed = 0;
3546
3547 bw = arsta->bw;
3548 nss = arsta->nss;
3549 smps = arsta->smps;
3550
3551 spin_unlock_bh(&ar->data_lock);
3552
3553 mutex_lock(&ar->conf_mutex);
3554
3555 if (changed & IEEE80211_RC_BW_CHANGED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003556 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM peer bw %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003557 sta->addr, bw);
3558
3559 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
3560 WMI_PEER_CHAN_WIDTH, bw);
3561 if (err)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003562 ath10k_warn(ar, "failed to update STA %pM peer bw %d: %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003563 sta->addr, bw, err);
3564 }
3565
3566 if (changed & IEEE80211_RC_NSS_CHANGED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003567 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM nss %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003568 sta->addr, nss);
3569
3570 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
3571 WMI_PEER_NSS, nss);
3572 if (err)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003573 ath10k_warn(ar, "failed to update STA %pM nss %d: %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003574 sta->addr, nss, err);
3575 }
3576
3577 if (changed & IEEE80211_RC_SMPS_CHANGED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003578 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM smps %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003579 sta->addr, smps);
3580
3581 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
3582 WMI_PEER_SMPS_STATE, smps);
3583 if (err)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003584 ath10k_warn(ar, "failed to update STA %pM smps %d: %d\n",
Michal Kazior9797feb2014-02-14 14:49:48 +01003585 sta->addr, smps, err);
3586 }
3587
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02003588 if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003589 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM supp rates\n",
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02003590 sta->addr);
3591
Michal Kazior590922a2014-10-21 10:10:29 +03003592 err = ath10k_station_assoc(ar, arvif->vif, sta, true);
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02003593 if (err)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003594 ath10k_warn(ar, "failed to reassociate station: %pM\n",
Chun-Yeow Yeoh44d6fa92014-03-07 10:19:30 +02003595 sta->addr);
3596 }
3597
Michal Kazior9797feb2014-02-14 14:49:48 +01003598 mutex_unlock(&ar->conf_mutex);
3599}
3600
Michal Kaziorcfd10612014-11-25 15:16:05 +01003601static int ath10k_mac_inc_num_stations(struct ath10k_vif *arvif)
3602{
3603 struct ath10k *ar = arvif->ar;
3604
3605 lockdep_assert_held(&ar->conf_mutex);
3606
3607 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
3608 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
3609 return 0;
3610
3611 if (ar->num_stations >= ar->max_num_stations)
3612 return -ENOBUFS;
3613
3614 ar->num_stations++;
3615
3616 return 0;
3617}
3618
3619static void ath10k_mac_dec_num_stations(struct ath10k_vif *arvif)
3620{
3621 struct ath10k *ar = arvif->ar;
3622
3623 lockdep_assert_held(&ar->conf_mutex);
3624
3625 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
3626 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
3627 return;
3628
3629 ar->num_stations--;
3630}
3631
Kalle Valo5e3dd152013-06-12 20:52:10 +03003632static int ath10k_sta_state(struct ieee80211_hw *hw,
3633 struct ieee80211_vif *vif,
3634 struct ieee80211_sta *sta,
3635 enum ieee80211_sta_state old_state,
3636 enum ieee80211_sta_state new_state)
3637{
3638 struct ath10k *ar = hw->priv;
3639 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
Michal Kazior9797feb2014-02-14 14:49:48 +01003640 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003641 int ret = 0;
3642
Michal Kazior76f90022014-02-25 09:29:57 +02003643 if (old_state == IEEE80211_STA_NOTEXIST &&
3644 new_state == IEEE80211_STA_NONE) {
3645 memset(arsta, 0, sizeof(*arsta));
3646 arsta->arvif = arvif;
3647 INIT_WORK(&arsta->update_wk, ath10k_sta_rc_update_wk);
3648 }
3649
Michal Kazior9797feb2014-02-14 14:49:48 +01003650 /* cancel must be done outside the mutex to avoid deadlock */
3651 if ((old_state == IEEE80211_STA_NONE &&
3652 new_state == IEEE80211_STA_NOTEXIST))
3653 cancel_work_sync(&arsta->update_wk);
3654
Kalle Valo5e3dd152013-06-12 20:52:10 +03003655 mutex_lock(&ar->conf_mutex);
3656
3657 if (old_state == IEEE80211_STA_NOTEXIST &&
Michal Kazior077efc82014-10-21 10:10:29 +03003658 new_state == IEEE80211_STA_NONE) {
Kalle Valo5e3dd152013-06-12 20:52:10 +03003659 /*
3660 * New station addition.
3661 */
Michal Kaziorcfd10612014-11-25 15:16:05 +01003662 ath10k_dbg(ar, ATH10K_DBG_MAC,
3663 "mac vdev %d peer create %pM (new sta) sta %d / %d peer %d / %d\n",
3664 arvif->vdev_id, sta->addr,
3665 ar->num_stations + 1, ar->max_num_stations,
3666 ar->num_peers + 1, ar->max_num_peers);
Bartosz Markowski0e759f32014-01-02 14:38:33 +01003667
Michal Kaziorcfd10612014-11-25 15:16:05 +01003668 ret = ath10k_mac_inc_num_stations(arvif);
3669 if (ret) {
3670 ath10k_warn(ar, "refusing to associate station: too many connected already (%d)\n",
3671 ar->max_num_stations);
Bartosz Markowski0e759f32014-01-02 14:38:33 +01003672 goto exit;
3673 }
3674
Kalle Valo5e3dd152013-06-12 20:52:10 +03003675 ret = ath10k_peer_create(ar, arvif->vdev_id, sta->addr);
Michal Kaziora52c0282014-11-25 15:16:03 +01003676 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003677 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 -08003678 sta->addr, arvif->vdev_id, ret);
Michal Kaziorcfd10612014-11-25 15:16:05 +01003679 ath10k_mac_dec_num_stations(arvif);
Michal Kaziora52c0282014-11-25 15:16:03 +01003680 goto exit;
3681 }
Michal Kazior077efc82014-10-21 10:10:29 +03003682
3683 if (vif->type == NL80211_IFTYPE_STATION) {
3684 WARN_ON(arvif->is_started);
3685
3686 ret = ath10k_vdev_start(arvif);
3687 if (ret) {
3688 ath10k_warn(ar, "failed to start vdev %i: %d\n",
3689 arvif->vdev_id, ret);
3690 WARN_ON(ath10k_peer_delete(ar, arvif->vdev_id,
3691 sta->addr));
Michal Kaziorcfd10612014-11-25 15:16:05 +01003692 ath10k_mac_dec_num_stations(arvif);
Michal Kazior077efc82014-10-21 10:10:29 +03003693 goto exit;
3694 }
3695
3696 arvif->is_started = true;
3697 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003698 } else if ((old_state == IEEE80211_STA_NONE &&
3699 new_state == IEEE80211_STA_NOTEXIST)) {
3700 /*
3701 * Existing station deletion.
3702 */
Michal Kazior7aa7a722014-08-25 12:09:38 +02003703 ath10k_dbg(ar, ATH10K_DBG_MAC,
Kalle Valo60c3daa2013-09-08 17:56:07 +03003704 "mac vdev %d peer delete %pM (sta gone)\n",
3705 arvif->vdev_id, sta->addr);
Michal Kazior077efc82014-10-21 10:10:29 +03003706
3707 if (vif->type == NL80211_IFTYPE_STATION) {
3708 WARN_ON(!arvif->is_started);
3709
3710 ret = ath10k_vdev_stop(arvif);
3711 if (ret)
3712 ath10k_warn(ar, "failed to stop vdev %i: %d\n",
3713 arvif->vdev_id, ret);
3714
3715 arvif->is_started = false;
3716 }
3717
Kalle Valo5e3dd152013-06-12 20:52:10 +03003718 ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
3719 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003720 ath10k_warn(ar, "failed to delete peer %pM for vdev %d: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003721 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003722
Michal Kaziorcfd10612014-11-25 15:16:05 +01003723 ath10k_mac_dec_num_stations(arvif);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003724 } else if (old_state == IEEE80211_STA_AUTH &&
3725 new_state == IEEE80211_STA_ASSOC &&
3726 (vif->type == NL80211_IFTYPE_AP ||
3727 vif->type == NL80211_IFTYPE_ADHOC)) {
3728 /*
3729 * New association.
3730 */
Michal Kazior7aa7a722014-08-25 12:09:38 +02003731 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM associated\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03003732 sta->addr);
3733
Michal Kazior590922a2014-10-21 10:10:29 +03003734 ret = ath10k_station_assoc(ar, vif, sta, false);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003735 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003736 ath10k_warn(ar, "failed to associate station %pM for vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003737 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003738 } else if (old_state == IEEE80211_STA_ASSOC &&
3739 new_state == IEEE80211_STA_AUTH &&
3740 (vif->type == NL80211_IFTYPE_AP ||
3741 vif->type == NL80211_IFTYPE_ADHOC)) {
3742 /*
3743 * Disassociation.
3744 */
Michal Kazior7aa7a722014-08-25 12:09:38 +02003745 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM disassociated\n",
Kalle Valo60c3daa2013-09-08 17:56:07 +03003746 sta->addr);
3747
Michal Kazior590922a2014-10-21 10:10:29 +03003748 ret = ath10k_station_disassoc(ar, vif, sta);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003749 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003750 ath10k_warn(ar, "failed to disassociate station: %pM vdev %i: %i\n",
Ben Greear69244e52014-02-27 18:50:00 +02003751 sta->addr, arvif->vdev_id, ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003752 }
Bartosz Markowski0e759f32014-01-02 14:38:33 +01003753exit:
Kalle Valo5e3dd152013-06-12 20:52:10 +03003754 mutex_unlock(&ar->conf_mutex);
3755 return ret;
3756}
3757
3758static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
Kalle Valo5b07e072014-09-14 12:50:06 +03003759 u16 ac, bool enable)
Kalle Valo5e3dd152013-06-12 20:52:10 +03003760{
3761 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3762 u32 value = 0;
3763 int ret = 0;
3764
Michal Kazior548db542013-07-05 16:15:15 +03003765 lockdep_assert_held(&ar->conf_mutex);
3766
Kalle Valo5e3dd152013-06-12 20:52:10 +03003767 if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
3768 return 0;
3769
3770 switch (ac) {
3771 case IEEE80211_AC_VO:
3772 value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
3773 WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
3774 break;
3775 case IEEE80211_AC_VI:
3776 value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
3777 WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
3778 break;
3779 case IEEE80211_AC_BE:
3780 value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
3781 WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
3782 break;
3783 case IEEE80211_AC_BK:
3784 value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
3785 WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
3786 break;
3787 }
3788
3789 if (enable)
3790 arvif->u.sta.uapsd |= value;
3791 else
3792 arvif->u.sta.uapsd &= ~value;
3793
3794 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3795 WMI_STA_PS_PARAM_UAPSD,
3796 arvif->u.sta.uapsd);
3797 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003798 ath10k_warn(ar, "failed to set uapsd params: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003799 goto exit;
3800 }
3801
3802 if (arvif->u.sta.uapsd)
3803 value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
3804 else
3805 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
3806
3807 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3808 WMI_STA_PS_PARAM_RX_WAKE_POLICY,
3809 value);
3810 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003811 ath10k_warn(ar, "failed to set rx wake param: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003812
3813exit:
3814 return ret;
3815}
3816
3817static int ath10k_conf_tx(struct ieee80211_hw *hw,
3818 struct ieee80211_vif *vif, u16 ac,
3819 const struct ieee80211_tx_queue_params *params)
3820{
3821 struct ath10k *ar = hw->priv;
3822 struct wmi_wmm_params_arg *p = NULL;
3823 int ret;
3824
3825 mutex_lock(&ar->conf_mutex);
3826
3827 switch (ac) {
3828 case IEEE80211_AC_VO:
3829 p = &ar->wmm_params.ac_vo;
3830 break;
3831 case IEEE80211_AC_VI:
3832 p = &ar->wmm_params.ac_vi;
3833 break;
3834 case IEEE80211_AC_BE:
3835 p = &ar->wmm_params.ac_be;
3836 break;
3837 case IEEE80211_AC_BK:
3838 p = &ar->wmm_params.ac_bk;
3839 break;
3840 }
3841
3842 if (WARN_ON(!p)) {
3843 ret = -EINVAL;
3844 goto exit;
3845 }
3846
3847 p->cwmin = params->cw_min;
3848 p->cwmax = params->cw_max;
3849 p->aifs = params->aifs;
3850
3851 /*
3852 * The channel time duration programmed in the HW is in absolute
3853 * microseconds, while mac80211 gives the txop in units of
3854 * 32 microseconds.
3855 */
3856 p->txop = params->txop * 32;
3857
3858 /* FIXME: FW accepts wmm params per hw, not per vif */
3859 ret = ath10k_wmi_pdev_set_wmm_params(ar, &ar->wmm_params);
3860 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003861 ath10k_warn(ar, "failed to set wmm params: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003862 goto exit;
3863 }
3864
3865 ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
3866 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003867 ath10k_warn(ar, "failed to set sta uapsd: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003868
3869exit:
3870 mutex_unlock(&ar->conf_mutex);
3871 return ret;
3872}
3873
3874#define ATH10K_ROC_TIMEOUT_HZ (2*HZ)
3875
3876static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
3877 struct ieee80211_vif *vif,
3878 struct ieee80211_channel *chan,
3879 int duration,
3880 enum ieee80211_roc_type type)
3881{
3882 struct ath10k *ar = hw->priv;
3883 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3884 struct wmi_start_scan_arg arg;
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003885 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003886
3887 mutex_lock(&ar->conf_mutex);
3888
3889 spin_lock_bh(&ar->data_lock);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003890 switch (ar->scan.state) {
3891 case ATH10K_SCAN_IDLE:
3892 reinit_completion(&ar->scan.started);
3893 reinit_completion(&ar->scan.completed);
3894 reinit_completion(&ar->scan.on_channel);
3895 ar->scan.state = ATH10K_SCAN_STARTING;
3896 ar->scan.is_roc = true;
3897 ar->scan.vdev_id = arvif->vdev_id;
3898 ar->scan.roc_freq = chan->center_freq;
3899 ret = 0;
3900 break;
3901 case ATH10K_SCAN_STARTING:
3902 case ATH10K_SCAN_RUNNING:
3903 case ATH10K_SCAN_ABORTING:
Kalle Valo5e3dd152013-06-12 20:52:10 +03003904 ret = -EBUSY;
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003905 break;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003906 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03003907 spin_unlock_bh(&ar->data_lock);
3908
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003909 if (ret)
3910 goto exit;
3911
Michal Kaziordcca0bd2014-11-24 14:58:32 +01003912 duration = max(duration, WMI_SCAN_CHAN_MIN_TIME_MSEC);
3913
Kalle Valo5e3dd152013-06-12 20:52:10 +03003914 memset(&arg, 0, sizeof(arg));
3915 ath10k_wmi_start_scan_init(ar, &arg);
3916 arg.vdev_id = arvif->vdev_id;
3917 arg.scan_id = ATH10K_SCAN_ID;
3918 arg.n_channels = 1;
3919 arg.channels[0] = chan->center_freq;
3920 arg.dwell_time_active = duration;
3921 arg.dwell_time_passive = duration;
3922 arg.max_scan_time = 2 * duration;
3923 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
3924 arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
3925
3926 ret = ath10k_start_scan(ar, &arg);
3927 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003928 ath10k_warn(ar, "failed to start roc scan: %d\n", ret);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003929 spin_lock_bh(&ar->data_lock);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003930 ar->scan.state = ATH10K_SCAN_IDLE;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003931 spin_unlock_bh(&ar->data_lock);
3932 goto exit;
3933 }
3934
3935 ret = wait_for_completion_timeout(&ar->scan.on_channel, 3*HZ);
3936 if (ret == 0) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003937 ath10k_warn(ar, "failed to switch to channel for roc scan\n");
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003938
3939 ret = ath10k_scan_stop(ar);
3940 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02003941 ath10k_warn(ar, "failed to stop scan: %d\n", ret);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003942
Kalle Valo5e3dd152013-06-12 20:52:10 +03003943 ret = -ETIMEDOUT;
3944 goto exit;
3945 }
3946
3947 ret = 0;
3948exit:
3949 mutex_unlock(&ar->conf_mutex);
3950 return ret;
3951}
3952
3953static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw)
3954{
3955 struct ath10k *ar = hw->priv;
3956
3957 mutex_lock(&ar->conf_mutex);
Michal Kazior5c81c7f2014-08-05 14:54:44 +02003958 ath10k_scan_abort(ar);
Kalle Valo5e3dd152013-06-12 20:52:10 +03003959 mutex_unlock(&ar->conf_mutex);
3960
Michal Kazior4eb2e162014-10-28 10:23:09 +01003961 cancel_delayed_work_sync(&ar->scan.timeout);
3962
Kalle Valo5e3dd152013-06-12 20:52:10 +03003963 return 0;
3964}
3965
3966/*
3967 * Both RTS and Fragmentation threshold are interface-specific
3968 * in ath10k, but device-specific in mac80211.
3969 */
Kalle Valo5e3dd152013-06-12 20:52:10 +03003970
3971static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3972{
Kalle Valo5e3dd152013-06-12 20:52:10 +03003973 struct ath10k *ar = hw->priv;
Michal Kaziorad088bf2013-10-16 15:44:46 +03003974 struct ath10k_vif *arvif;
3975 int ret = 0;
Michal Kazior548db542013-07-05 16:15:15 +03003976
Michal Kaziorad088bf2013-10-16 15:44:46 +03003977 mutex_lock(&ar->conf_mutex);
3978 list_for_each_entry(arvif, &ar->arvifs, list) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003979 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
Michal Kaziorad088bf2013-10-16 15:44:46 +03003980 arvif->vdev_id, value);
Kalle Valo60c3daa2013-09-08 17:56:07 +03003981
Michal Kaziorad088bf2013-10-16 15:44:46 +03003982 ret = ath10k_mac_set_rts(arvif, value);
3983 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02003984 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
Michal Kaziorad088bf2013-10-16 15:44:46 +03003985 arvif->vdev_id, ret);
3986 break;
3987 }
3988 }
3989 mutex_unlock(&ar->conf_mutex);
3990
3991 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003992}
3993
3994static int ath10k_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
3995{
Kalle Valo5e3dd152013-06-12 20:52:10 +03003996 struct ath10k *ar = hw->priv;
Michal Kaziorad088bf2013-10-16 15:44:46 +03003997 struct ath10k_vif *arvif;
3998 int ret = 0;
Kalle Valo5e3dd152013-06-12 20:52:10 +03003999
Kalle Valo5e3dd152013-06-12 20:52:10 +03004000 mutex_lock(&ar->conf_mutex);
Michal Kaziorad088bf2013-10-16 15:44:46 +03004001 list_for_each_entry(arvif, &ar->arvifs, list) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004002 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d fragmentation threshold %d\n",
Michal Kaziorad088bf2013-10-16 15:44:46 +03004003 arvif->vdev_id, value);
4004
Michal Kazior56a0dee2014-10-23 17:04:29 +03004005 ret = ath10k_mac_set_frag(arvif, value);
Michal Kaziorad088bf2013-10-16 15:44:46 +03004006 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004007 ath10k_warn(ar, "failed to set fragmentation threshold for vdev %d: %d\n",
Michal Kaziorad088bf2013-10-16 15:44:46 +03004008 arvif->vdev_id, ret);
4009 break;
4010 }
4011 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03004012 mutex_unlock(&ar->conf_mutex);
4013
Michal Kaziorad088bf2013-10-16 15:44:46 +03004014 return ret;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004015}
4016
Emmanuel Grumbach77be2c52014-03-27 11:30:29 +02004017static void ath10k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4018 u32 queues, bool drop)
Kalle Valo5e3dd152013-06-12 20:52:10 +03004019{
4020 struct ath10k *ar = hw->priv;
Michal Kazioraffd3212013-07-16 09:54:35 +02004021 bool skip;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004022 int ret;
4023
4024 /* mac80211 doesn't care if we really xmit queued frames or not
4025 * we'll collect those frames either way if we stop/delete vdevs */
4026 if (drop)
4027 return;
4028
Michal Kazior548db542013-07-05 16:15:15 +03004029 mutex_lock(&ar->conf_mutex);
4030
Michal Kazioraffd3212013-07-16 09:54:35 +02004031 if (ar->state == ATH10K_STATE_WEDGED)
4032 goto skip;
4033
Michal Kazioredb82362013-07-05 16:15:14 +03004034 ret = wait_event_timeout(ar->htt.empty_tx_wq, ({
Kalle Valo5e3dd152013-06-12 20:52:10 +03004035 bool empty;
Michal Kazioraffd3212013-07-16 09:54:35 +02004036
Michal Kazioredb82362013-07-05 16:15:14 +03004037 spin_lock_bh(&ar->htt.tx_lock);
Michal Kazior0945baf2013-09-18 14:43:18 +02004038 empty = (ar->htt.num_pending_tx == 0);
Michal Kazioredb82362013-07-05 16:15:14 +03004039 spin_unlock_bh(&ar->htt.tx_lock);
Michal Kazioraffd3212013-07-16 09:54:35 +02004040
Michal Kazior7962b0d2014-10-28 10:34:38 +01004041 skip = (ar->state == ATH10K_STATE_WEDGED) ||
4042 test_bit(ATH10K_FLAG_CRASH_FLUSH,
4043 &ar->dev_flags);
Michal Kazioraffd3212013-07-16 09:54:35 +02004044
4045 (empty || skip);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004046 }), ATH10K_FLUSH_TIMEOUT_HZ);
Michal Kazioraffd3212013-07-16 09:54:35 +02004047
4048 if (ret <= 0 || skip)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004049 ath10k_warn(ar, "failed to flush transmit queue (skip %i ar-state %i): %i\n",
Ben Greear9ba4c782014-02-25 09:29:57 +02004050 skip, ar->state, ret);
Michal Kazior548db542013-07-05 16:15:15 +03004051
Michal Kazioraffd3212013-07-16 09:54:35 +02004052skip:
Michal Kazior548db542013-07-05 16:15:15 +03004053 mutex_unlock(&ar->conf_mutex);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004054}
4055
4056/* TODO: Implement this function properly
4057 * For now it is needed to reply to Probe Requests in IBSS mode.
4058 * Propably we need this information from FW.
4059 */
4060static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
4061{
4062 return 1;
4063}
4064
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004065#ifdef CONFIG_PM
4066static int ath10k_suspend(struct ieee80211_hw *hw,
4067 struct cfg80211_wowlan *wowlan)
4068{
4069 struct ath10k *ar = hw->priv;
4070 int ret;
4071
Marek Puzyniak9042e172014-02-10 17:14:23 +01004072 mutex_lock(&ar->conf_mutex);
4073
Marek Puzyniak00f54822014-02-10 17:14:24 +01004074 ret = ath10k_wait_for_suspend(ar, WMI_PDEV_SUSPEND);
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004075 if (ret) {
Marek Puzyniak00f54822014-02-10 17:14:24 +01004076 if (ret == -ETIMEDOUT)
4077 goto resume;
Marek Puzyniak9042e172014-02-10 17:14:23 +01004078 ret = 1;
4079 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004080 }
4081
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004082 ret = ath10k_hif_suspend(ar);
4083 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004084 ath10k_warn(ar, "failed to suspend hif: %d\n", ret);
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004085 goto resume;
4086 }
4087
Marek Puzyniak9042e172014-02-10 17:14:23 +01004088 ret = 0;
4089 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004090resume:
4091 ret = ath10k_wmi_pdev_resume_target(ar);
4092 if (ret)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004093 ath10k_warn(ar, "failed to resume target: %d\n", ret);
Marek Puzyniak9042e172014-02-10 17:14:23 +01004094
4095 ret = 1;
4096exit:
4097 mutex_unlock(&ar->conf_mutex);
4098 return ret;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004099}
4100
4101static int ath10k_resume(struct ieee80211_hw *hw)
4102{
4103 struct ath10k *ar = hw->priv;
4104 int ret;
4105
Marek Puzyniak9042e172014-02-10 17:14:23 +01004106 mutex_lock(&ar->conf_mutex);
4107
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004108 ret = ath10k_hif_resume(ar);
4109 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004110 ath10k_warn(ar, "failed to resume hif: %d\n", ret);
Marek Puzyniak9042e172014-02-10 17:14:23 +01004111 ret = 1;
4112 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004113 }
4114
4115 ret = ath10k_wmi_pdev_resume_target(ar);
4116 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004117 ath10k_warn(ar, "failed to resume target: %d\n", ret);
Marek Puzyniak9042e172014-02-10 17:14:23 +01004118 ret = 1;
4119 goto exit;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004120 }
4121
Marek Puzyniak9042e172014-02-10 17:14:23 +01004122 ret = 0;
4123exit:
4124 mutex_unlock(&ar->conf_mutex);
4125 return ret;
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004126}
4127#endif
4128
Eliad Pellercf2c92d2014-11-04 11:43:54 +02004129static void ath10k_reconfig_complete(struct ieee80211_hw *hw,
4130 enum ieee80211_reconfig_type reconfig_type)
Michal Kazioraffd3212013-07-16 09:54:35 +02004131{
4132 struct ath10k *ar = hw->priv;
4133
Eliad Pellercf2c92d2014-11-04 11:43:54 +02004134 if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
4135 return;
4136
Michal Kazioraffd3212013-07-16 09:54:35 +02004137 mutex_lock(&ar->conf_mutex);
4138
4139 /* If device failed to restart it will be in a different state, e.g.
4140 * ATH10K_STATE_WEDGED */
4141 if (ar->state == ATH10K_STATE_RESTARTED) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004142 ath10k_info(ar, "device successfully recovered\n");
Michal Kazioraffd3212013-07-16 09:54:35 +02004143 ar->state = ATH10K_STATE_ON;
Michal Kazior7962b0d2014-10-28 10:34:38 +01004144 ieee80211_wake_queues(ar->hw);
Michal Kazioraffd3212013-07-16 09:54:35 +02004145 }
4146
4147 mutex_unlock(&ar->conf_mutex);
4148}
4149
Michal Kazior2e1dea42013-07-31 10:32:40 +02004150static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
4151 struct survey_info *survey)
4152{
4153 struct ath10k *ar = hw->priv;
4154 struct ieee80211_supported_band *sband;
4155 struct survey_info *ar_survey = &ar->survey[idx];
4156 int ret = 0;
4157
4158 mutex_lock(&ar->conf_mutex);
4159
4160 sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
4161 if (sband && idx >= sband->n_channels) {
4162 idx -= sband->n_channels;
4163 sband = NULL;
4164 }
4165
4166 if (!sband)
4167 sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
4168
4169 if (!sband || idx >= sband->n_channels) {
4170 ret = -ENOENT;
4171 goto exit;
4172 }
4173
4174 spin_lock_bh(&ar->data_lock);
4175 memcpy(survey, ar_survey, sizeof(*survey));
4176 spin_unlock_bh(&ar->data_lock);
4177
4178 survey->channel = &sband->channels[idx];
4179
Felix Fietkaufa1d4df2014-10-23 17:04:28 +03004180 if (ar->rx_channel == survey->channel)
4181 survey->filled |= SURVEY_INFO_IN_USE;
4182
Michal Kazior2e1dea42013-07-31 10:32:40 +02004183exit:
4184 mutex_unlock(&ar->conf_mutex);
4185 return ret;
4186}
4187
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004188/* Helper table for legacy fixed_rate/bitrate_mask */
4189static const u8 cck_ofdm_rate[] = {
4190 /* CCK */
4191 3, /* 1Mbps */
4192 2, /* 2Mbps */
4193 1, /* 5.5Mbps */
4194 0, /* 11Mbps */
4195 /* OFDM */
4196 3, /* 6Mbps */
4197 7, /* 9Mbps */
4198 2, /* 12Mbps */
4199 6, /* 18Mbps */
4200 1, /* 24Mbps */
4201 5, /* 36Mbps */
4202 0, /* 48Mbps */
4203 4, /* 54Mbps */
4204};
4205
4206/* Check if only one bit set */
4207static int ath10k_check_single_mask(u32 mask)
4208{
4209 int bit;
4210
4211 bit = ffs(mask);
4212 if (!bit)
4213 return 0;
4214
4215 mask &= ~BIT(bit - 1);
4216 if (mask)
4217 return 2;
4218
4219 return 1;
4220}
4221
4222static bool
4223ath10k_default_bitrate_mask(struct ath10k *ar,
4224 enum ieee80211_band band,
4225 const struct cfg80211_bitrate_mask *mask)
4226{
4227 u32 legacy = 0x00ff;
4228 u8 ht = 0xff, i;
4229 u16 vht = 0x3ff;
Ben Greearb116ea12014-11-24 16:22:10 +02004230 u16 nrf = ar->num_rf_chains;
4231
4232 if (ar->cfg_tx_chainmask)
4233 nrf = get_nss_from_chainmask(ar->cfg_tx_chainmask);
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004234
4235 switch (band) {
4236 case IEEE80211_BAND_2GHZ:
4237 legacy = 0x00fff;
4238 vht = 0;
4239 break;
4240 case IEEE80211_BAND_5GHZ:
4241 break;
4242 default:
4243 return false;
4244 }
4245
4246 if (mask->control[band].legacy != legacy)
4247 return false;
4248
Ben Greearb116ea12014-11-24 16:22:10 +02004249 for (i = 0; i < nrf; i++)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004250 if (mask->control[band].ht_mcs[i] != ht)
4251 return false;
4252
Ben Greearb116ea12014-11-24 16:22:10 +02004253 for (i = 0; i < nrf; i++)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004254 if (mask->control[band].vht_mcs[i] != vht)
4255 return false;
4256
4257 return true;
4258}
4259
4260static bool
4261ath10k_bitrate_mask_nss(const struct cfg80211_bitrate_mask *mask,
4262 enum ieee80211_band band,
4263 u8 *fixed_nss)
4264{
4265 int ht_nss = 0, vht_nss = 0, i;
4266
4267 /* check legacy */
4268 if (ath10k_check_single_mask(mask->control[band].legacy))
4269 return false;
4270
4271 /* check HT */
4272 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
4273 if (mask->control[band].ht_mcs[i] == 0xff)
4274 continue;
4275 else if (mask->control[band].ht_mcs[i] == 0x00)
4276 break;
Kalle Valod8bb26b2014-09-14 12:50:33 +03004277
4278 return false;
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004279 }
4280
4281 ht_nss = i;
4282
4283 /* check VHT */
4284 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
4285 if (mask->control[band].vht_mcs[i] == 0x03ff)
4286 continue;
4287 else if (mask->control[band].vht_mcs[i] == 0x0000)
4288 break;
Kalle Valod8bb26b2014-09-14 12:50:33 +03004289
4290 return false;
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004291 }
4292
4293 vht_nss = i;
4294
4295 if (ht_nss > 0 && vht_nss > 0)
4296 return false;
4297
4298 if (ht_nss)
4299 *fixed_nss = ht_nss;
4300 else if (vht_nss)
4301 *fixed_nss = vht_nss;
4302 else
4303 return false;
4304
4305 return true;
4306}
4307
4308static bool
4309ath10k_bitrate_mask_correct(const struct cfg80211_bitrate_mask *mask,
4310 enum ieee80211_band band,
4311 enum wmi_rate_preamble *preamble)
4312{
4313 int legacy = 0, ht = 0, vht = 0, i;
4314
4315 *preamble = WMI_RATE_PREAMBLE_OFDM;
4316
4317 /* check legacy */
4318 legacy = ath10k_check_single_mask(mask->control[band].legacy);
4319 if (legacy > 1)
4320 return false;
4321
4322 /* check HT */
4323 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
4324 ht += ath10k_check_single_mask(mask->control[band].ht_mcs[i]);
4325 if (ht > 1)
4326 return false;
4327
4328 /* check VHT */
4329 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
4330 vht += ath10k_check_single_mask(mask->control[band].vht_mcs[i]);
4331 if (vht > 1)
4332 return false;
4333
4334 /* Currently we support only one fixed_rate */
4335 if ((legacy + ht + vht) != 1)
4336 return false;
4337
4338 if (ht)
4339 *preamble = WMI_RATE_PREAMBLE_HT;
4340 else if (vht)
4341 *preamble = WMI_RATE_PREAMBLE_VHT;
4342
4343 return true;
4344}
4345
4346static bool
Michal Kazior7aa7a722014-08-25 12:09:38 +02004347ath10k_bitrate_mask_rate(struct ath10k *ar,
4348 const struct cfg80211_bitrate_mask *mask,
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004349 enum ieee80211_band band,
4350 u8 *fixed_rate,
4351 u8 *fixed_nss)
4352{
4353 u8 rate = 0, pream = 0, nss = 0, i;
4354 enum wmi_rate_preamble preamble;
4355
4356 /* Check if single rate correct */
4357 if (!ath10k_bitrate_mask_correct(mask, band, &preamble))
4358 return false;
4359
4360 pream = preamble;
4361
4362 switch (preamble) {
4363 case WMI_RATE_PREAMBLE_CCK:
4364 case WMI_RATE_PREAMBLE_OFDM:
4365 i = ffs(mask->control[band].legacy) - 1;
4366
4367 if (band == IEEE80211_BAND_2GHZ && i < 4)
4368 pream = WMI_RATE_PREAMBLE_CCK;
4369
4370 if (band == IEEE80211_BAND_5GHZ)
4371 i += 4;
4372
4373 if (i >= ARRAY_SIZE(cck_ofdm_rate))
4374 return false;
4375
4376 rate = cck_ofdm_rate[i];
4377 break;
4378 case WMI_RATE_PREAMBLE_HT:
4379 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
4380 if (mask->control[band].ht_mcs[i])
4381 break;
4382
4383 if (i == IEEE80211_HT_MCS_MASK_LEN)
4384 return false;
4385
4386 rate = ffs(mask->control[band].ht_mcs[i]) - 1;
4387 nss = i;
4388 break;
4389 case WMI_RATE_PREAMBLE_VHT:
4390 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
4391 if (mask->control[band].vht_mcs[i])
4392 break;
4393
4394 if (i == NL80211_VHT_NSS_MAX)
4395 return false;
4396
4397 rate = ffs(mask->control[band].vht_mcs[i]) - 1;
4398 nss = i;
4399 break;
4400 }
4401
4402 *fixed_nss = nss + 1;
4403 nss <<= 4;
4404 pream <<= 6;
4405
Michal Kazior7aa7a722014-08-25 12:09:38 +02004406 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 +01004407 pream, nss, rate);
4408
4409 *fixed_rate = pream | nss | rate;
4410
4411 return true;
4412}
4413
Michal Kazior7aa7a722014-08-25 12:09:38 +02004414static bool ath10k_get_fixed_rate_nss(struct ath10k *ar,
4415 const struct cfg80211_bitrate_mask *mask,
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004416 enum ieee80211_band band,
4417 u8 *fixed_rate,
4418 u8 *fixed_nss)
4419{
4420 /* First check full NSS mask, if we can simply limit NSS */
4421 if (ath10k_bitrate_mask_nss(mask, band, fixed_nss))
4422 return true;
4423
4424 /* Next Check single rate is set */
Michal Kazior7aa7a722014-08-25 12:09:38 +02004425 return ath10k_bitrate_mask_rate(ar, mask, band, fixed_rate, fixed_nss);
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004426}
4427
4428static int ath10k_set_fixed_rate_param(struct ath10k_vif *arvif,
4429 u8 fixed_rate,
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004430 u8 fixed_nss,
4431 u8 force_sgi)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004432{
4433 struct ath10k *ar = arvif->ar;
4434 u32 vdev_param;
4435 int ret = 0;
4436
4437 mutex_lock(&ar->conf_mutex);
4438
4439 if (arvif->fixed_rate == fixed_rate &&
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004440 arvif->fixed_nss == fixed_nss &&
4441 arvif->force_sgi == force_sgi)
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004442 goto exit;
4443
4444 if (fixed_rate == WMI_FIXED_RATE_NONE)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004445 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac disable fixed bitrate mask\n");
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004446
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004447 if (force_sgi)
Michal Kazior7aa7a722014-08-25 12:09:38 +02004448 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac force sgi\n");
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004449
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004450 vdev_param = ar->wmi.vdev_param->fixed_rate;
4451 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
4452 vdev_param, fixed_rate);
4453 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004454 ath10k_warn(ar, "failed to set fixed rate param 0x%02x: %d\n",
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004455 fixed_rate, ret);
4456 ret = -EINVAL;
4457 goto exit;
4458 }
4459
4460 arvif->fixed_rate = fixed_rate;
4461
4462 vdev_param = ar->wmi.vdev_param->nss;
4463 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
4464 vdev_param, fixed_nss);
4465
4466 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004467 ath10k_warn(ar, "failed to set fixed nss param %d: %d\n",
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004468 fixed_nss, ret);
4469 ret = -EINVAL;
4470 goto exit;
4471 }
4472
4473 arvif->fixed_nss = fixed_nss;
4474
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004475 vdev_param = ar->wmi.vdev_param->sgi;
4476 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
4477 force_sgi);
4478
4479 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004480 ath10k_warn(ar, "failed to set sgi param %d: %d\n",
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004481 force_sgi, ret);
4482 ret = -EINVAL;
4483 goto exit;
4484 }
4485
4486 arvif->force_sgi = force_sgi;
4487
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004488exit:
4489 mutex_unlock(&ar->conf_mutex);
4490 return ret;
4491}
4492
4493static int ath10k_set_bitrate_mask(struct ieee80211_hw *hw,
4494 struct ieee80211_vif *vif,
4495 const struct cfg80211_bitrate_mask *mask)
4496{
4497 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4498 struct ath10k *ar = arvif->ar;
4499 enum ieee80211_band band = ar->hw->conf.chandef.chan->band;
4500 u8 fixed_rate = WMI_FIXED_RATE_NONE;
4501 u8 fixed_nss = ar->num_rf_chains;
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004502 u8 force_sgi;
4503
Ben Greearb116ea12014-11-24 16:22:10 +02004504 if (ar->cfg_tx_chainmask)
4505 fixed_nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
4506
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004507 force_sgi = mask->control[band].gi;
4508 if (force_sgi == NL80211_TXRATE_FORCE_LGI)
4509 return -EINVAL;
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004510
4511 if (!ath10k_default_bitrate_mask(ar, band, mask)) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004512 if (!ath10k_get_fixed_rate_nss(ar, mask, band,
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004513 &fixed_rate,
4514 &fixed_nss))
4515 return -EINVAL;
4516 }
4517
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004518 if (fixed_rate == WMI_FIXED_RATE_NONE && force_sgi) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004519 ath10k_warn(ar, "failed to force SGI usage for default rate settings\n");
Janusz Dziedzic9f81f722014-01-17 20:04:14 +01004520 return -EINVAL;
4521 }
4522
4523 return ath10k_set_fixed_rate_param(arvif, fixed_rate,
4524 fixed_nss, force_sgi);
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004525}
4526
Michal Kazior9797feb2014-02-14 14:49:48 +01004527static void ath10k_sta_rc_update(struct ieee80211_hw *hw,
4528 struct ieee80211_vif *vif,
4529 struct ieee80211_sta *sta,
4530 u32 changed)
4531{
4532 struct ath10k *ar = hw->priv;
4533 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
4534 u32 bw, smps;
4535
4536 spin_lock_bh(&ar->data_lock);
4537
Michal Kazior7aa7a722014-08-25 12:09:38 +02004538 ath10k_dbg(ar, ATH10K_DBG_MAC,
Michal Kazior9797feb2014-02-14 14:49:48 +01004539 "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
4540 sta->addr, changed, sta->bandwidth, sta->rx_nss,
4541 sta->smps_mode);
4542
4543 if (changed & IEEE80211_RC_BW_CHANGED) {
4544 bw = WMI_PEER_CHWIDTH_20MHZ;
4545
4546 switch (sta->bandwidth) {
4547 case IEEE80211_STA_RX_BW_20:
4548 bw = WMI_PEER_CHWIDTH_20MHZ;
4549 break;
4550 case IEEE80211_STA_RX_BW_40:
4551 bw = WMI_PEER_CHWIDTH_40MHZ;
4552 break;
4553 case IEEE80211_STA_RX_BW_80:
4554 bw = WMI_PEER_CHWIDTH_80MHZ;
4555 break;
4556 case IEEE80211_STA_RX_BW_160:
Michal Kazior7aa7a722014-08-25 12:09:38 +02004557 ath10k_warn(ar, "Invalid bandwith %d in rc update for %pM\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02004558 sta->bandwidth, sta->addr);
Michal Kazior9797feb2014-02-14 14:49:48 +01004559 bw = WMI_PEER_CHWIDTH_20MHZ;
4560 break;
4561 }
4562
4563 arsta->bw = bw;
4564 }
4565
4566 if (changed & IEEE80211_RC_NSS_CHANGED)
4567 arsta->nss = sta->rx_nss;
4568
4569 if (changed & IEEE80211_RC_SMPS_CHANGED) {
4570 smps = WMI_PEER_SMPS_PS_NONE;
4571
4572 switch (sta->smps_mode) {
4573 case IEEE80211_SMPS_AUTOMATIC:
4574 case IEEE80211_SMPS_OFF:
4575 smps = WMI_PEER_SMPS_PS_NONE;
4576 break;
4577 case IEEE80211_SMPS_STATIC:
4578 smps = WMI_PEER_SMPS_STATIC;
4579 break;
4580 case IEEE80211_SMPS_DYNAMIC:
4581 smps = WMI_PEER_SMPS_DYNAMIC;
4582 break;
4583 case IEEE80211_SMPS_NUM_MODES:
Michal Kazior7aa7a722014-08-25 12:09:38 +02004584 ath10k_warn(ar, "Invalid smps %d in sta rc update for %pM\n",
Kalle Valobe6546f2014-03-25 14:18:51 +02004585 sta->smps_mode, sta->addr);
Michal Kazior9797feb2014-02-14 14:49:48 +01004586 smps = WMI_PEER_SMPS_PS_NONE;
4587 break;
4588 }
4589
4590 arsta->smps = smps;
4591 }
4592
Michal Kazior9797feb2014-02-14 14:49:48 +01004593 arsta->changed |= changed;
4594
4595 spin_unlock_bh(&ar->data_lock);
4596
4597 ieee80211_queue_work(hw, &arsta->update_wk);
4598}
4599
Chun-Yeow Yeoh26ebbcc2014-02-25 09:29:54 +02004600static u64 ath10k_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
4601{
4602 /*
4603 * FIXME: Return 0 for time being. Need to figure out whether FW
4604 * has the API to fetch 64-bit local TSF
4605 */
4606
4607 return 0;
4608}
4609
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02004610static int ath10k_ampdu_action(struct ieee80211_hw *hw,
4611 struct ieee80211_vif *vif,
4612 enum ieee80211_ampdu_mlme_action action,
4613 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
4614 u8 buf_size)
4615{
Michal Kazior7aa7a722014-08-25 12:09:38 +02004616 struct ath10k *ar = hw->priv;
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02004617 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4618
Michal Kazior7aa7a722014-08-25 12:09:38 +02004619 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 +02004620 arvif->vdev_id, sta->addr, tid, action);
4621
4622 switch (action) {
4623 case IEEE80211_AMPDU_RX_START:
4624 case IEEE80211_AMPDU_RX_STOP:
4625 /* HTT AddBa/DelBa events trigger mac80211 Rx BA session
4626 * creation/removal. Do we need to verify this?
4627 */
4628 return 0;
4629 case IEEE80211_AMPDU_TX_START:
4630 case IEEE80211_AMPDU_TX_STOP_CONT:
4631 case IEEE80211_AMPDU_TX_STOP_FLUSH:
4632 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
4633 case IEEE80211_AMPDU_TX_OPERATIONAL:
4634 /* Firmware offloads Tx aggregation entirely so deny mac80211
4635 * Tx aggregation requests.
4636 */
4637 return -EOPNOTSUPP;
4638 }
4639
4640 return -EINVAL;
4641}
4642
Kalle Valo5e3dd152013-06-12 20:52:10 +03004643static const struct ieee80211_ops ath10k_ops = {
4644 .tx = ath10k_tx,
4645 .start = ath10k_start,
4646 .stop = ath10k_stop,
4647 .config = ath10k_config,
4648 .add_interface = ath10k_add_interface,
4649 .remove_interface = ath10k_remove_interface,
4650 .configure_filter = ath10k_configure_filter,
4651 .bss_info_changed = ath10k_bss_info_changed,
4652 .hw_scan = ath10k_hw_scan,
4653 .cancel_hw_scan = ath10k_cancel_hw_scan,
4654 .set_key = ath10k_set_key,
4655 .sta_state = ath10k_sta_state,
4656 .conf_tx = ath10k_conf_tx,
4657 .remain_on_channel = ath10k_remain_on_channel,
4658 .cancel_remain_on_channel = ath10k_cancel_remain_on_channel,
4659 .set_rts_threshold = ath10k_set_rts_threshold,
4660 .set_frag_threshold = ath10k_set_frag_threshold,
4661 .flush = ath10k_flush,
4662 .tx_last_beacon = ath10k_tx_last_beacon,
Ben Greear46acf7b2014-05-16 17:15:38 +03004663 .set_antenna = ath10k_set_antenna,
4664 .get_antenna = ath10k_get_antenna,
Eliad Pellercf2c92d2014-11-04 11:43:54 +02004665 .reconfig_complete = ath10k_reconfig_complete,
Michal Kazior2e1dea42013-07-31 10:32:40 +02004666 .get_survey = ath10k_get_survey,
Janusz Dziedzic51ab1a02014-01-08 09:08:33 +01004667 .set_bitrate_mask = ath10k_set_bitrate_mask,
Michal Kazior9797feb2014-02-14 14:49:48 +01004668 .sta_rc_update = ath10k_sta_rc_update,
Chun-Yeow Yeoh26ebbcc2014-02-25 09:29:54 +02004669 .get_tsf = ath10k_get_tsf,
Michal Kazioraa5b4fb2014-07-23 12:20:33 +02004670 .ampdu_action = ath10k_ampdu_action,
Ben Greear6cddcc72014-09-29 14:41:46 +03004671 .get_et_sset_count = ath10k_debug_get_et_sset_count,
4672 .get_et_stats = ath10k_debug_get_et_stats,
4673 .get_et_strings = ath10k_debug_get_et_strings,
Kalle Valo43d2a302014-09-10 18:23:30 +03004674
4675 CFG80211_TESTMODE_CMD(ath10k_tm_cmd)
4676
Michal Kazior8cd13ca2013-07-16 09:38:54 +02004677#ifdef CONFIG_PM
4678 .suspend = ath10k_suspend,
4679 .resume = ath10k_resume,
4680#endif
Kalle Valo5e3dd152013-06-12 20:52:10 +03004681};
4682
4683#define RATETAB_ENT(_rate, _rateid, _flags) { \
4684 .bitrate = (_rate), \
4685 .flags = (_flags), \
4686 .hw_value = (_rateid), \
4687}
4688
4689#define CHAN2G(_channel, _freq, _flags) { \
4690 .band = IEEE80211_BAND_2GHZ, \
4691 .hw_value = (_channel), \
4692 .center_freq = (_freq), \
4693 .flags = (_flags), \
4694 .max_antenna_gain = 0, \
4695 .max_power = 30, \
4696}
4697
4698#define CHAN5G(_channel, _freq, _flags) { \
4699 .band = IEEE80211_BAND_5GHZ, \
4700 .hw_value = (_channel), \
4701 .center_freq = (_freq), \
4702 .flags = (_flags), \
4703 .max_antenna_gain = 0, \
4704 .max_power = 30, \
4705}
4706
4707static const struct ieee80211_channel ath10k_2ghz_channels[] = {
4708 CHAN2G(1, 2412, 0),
4709 CHAN2G(2, 2417, 0),
4710 CHAN2G(3, 2422, 0),
4711 CHAN2G(4, 2427, 0),
4712 CHAN2G(5, 2432, 0),
4713 CHAN2G(6, 2437, 0),
4714 CHAN2G(7, 2442, 0),
4715 CHAN2G(8, 2447, 0),
4716 CHAN2G(9, 2452, 0),
4717 CHAN2G(10, 2457, 0),
4718 CHAN2G(11, 2462, 0),
4719 CHAN2G(12, 2467, 0),
4720 CHAN2G(13, 2472, 0),
4721 CHAN2G(14, 2484, 0),
4722};
4723
4724static const struct ieee80211_channel ath10k_5ghz_channels[] = {
Michal Kazior429ff562013-06-26 08:54:54 +02004725 CHAN5G(36, 5180, 0),
4726 CHAN5G(40, 5200, 0),
4727 CHAN5G(44, 5220, 0),
4728 CHAN5G(48, 5240, 0),
4729 CHAN5G(52, 5260, 0),
4730 CHAN5G(56, 5280, 0),
4731 CHAN5G(60, 5300, 0),
4732 CHAN5G(64, 5320, 0),
4733 CHAN5G(100, 5500, 0),
4734 CHAN5G(104, 5520, 0),
4735 CHAN5G(108, 5540, 0),
4736 CHAN5G(112, 5560, 0),
4737 CHAN5G(116, 5580, 0),
4738 CHAN5G(120, 5600, 0),
4739 CHAN5G(124, 5620, 0),
4740 CHAN5G(128, 5640, 0),
4741 CHAN5G(132, 5660, 0),
4742 CHAN5G(136, 5680, 0),
4743 CHAN5G(140, 5700, 0),
4744 CHAN5G(149, 5745, 0),
4745 CHAN5G(153, 5765, 0),
4746 CHAN5G(157, 5785, 0),
4747 CHAN5G(161, 5805, 0),
4748 CHAN5G(165, 5825, 0),
Kalle Valo5e3dd152013-06-12 20:52:10 +03004749};
4750
4751static struct ieee80211_rate ath10k_rates[] = {
4752 /* CCK */
4753 RATETAB_ENT(10, 0x82, 0),
4754 RATETAB_ENT(20, 0x84, 0),
4755 RATETAB_ENT(55, 0x8b, 0),
4756 RATETAB_ENT(110, 0x96, 0),
4757 /* OFDM */
4758 RATETAB_ENT(60, 0x0c, 0),
4759 RATETAB_ENT(90, 0x12, 0),
4760 RATETAB_ENT(120, 0x18, 0),
4761 RATETAB_ENT(180, 0x24, 0),
4762 RATETAB_ENT(240, 0x30, 0),
4763 RATETAB_ENT(360, 0x48, 0),
4764 RATETAB_ENT(480, 0x60, 0),
4765 RATETAB_ENT(540, 0x6c, 0),
4766};
4767
4768#define ath10k_a_rates (ath10k_rates + 4)
4769#define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - 4)
4770#define ath10k_g_rates (ath10k_rates + 0)
4771#define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
4772
Michal Kaziore7b54192014-08-07 11:03:27 +02004773struct ath10k *ath10k_mac_create(size_t priv_size)
Kalle Valo5e3dd152013-06-12 20:52:10 +03004774{
4775 struct ieee80211_hw *hw;
4776 struct ath10k *ar;
4777
Michal Kaziore7b54192014-08-07 11:03:27 +02004778 hw = ieee80211_alloc_hw(sizeof(struct ath10k) + priv_size, &ath10k_ops);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004779 if (!hw)
4780 return NULL;
4781
4782 ar = hw->priv;
4783 ar->hw = hw;
4784
4785 return ar;
4786}
4787
4788void ath10k_mac_destroy(struct ath10k *ar)
4789{
4790 ieee80211_free_hw(ar->hw);
4791}
4792
4793static const struct ieee80211_iface_limit ath10k_if_limits[] = {
4794 {
4795 .max = 8,
4796 .types = BIT(NL80211_IFTYPE_STATION)
4797 | BIT(NL80211_IFTYPE_P2P_CLIENT)
Michal Kaziord531cb82013-07-31 10:55:13 +02004798 },
4799 {
4800 .max = 3,
4801 .types = BIT(NL80211_IFTYPE_P2P_GO)
4802 },
4803 {
4804 .max = 7,
4805 .types = BIT(NL80211_IFTYPE_AP)
4806 },
Kalle Valo5e3dd152013-06-12 20:52:10 +03004807};
4808
Bartosz Markowskif2595092013-12-10 16:20:39 +01004809static const struct ieee80211_iface_limit ath10k_10x_if_limits[] = {
Marek Puzyniake8a50f82013-11-20 09:59:47 +02004810 {
4811 .max = 8,
4812 .types = BIT(NL80211_IFTYPE_AP)
4813 },
4814};
Marek Puzyniake8a50f82013-11-20 09:59:47 +02004815
4816static const struct ieee80211_iface_combination ath10k_if_comb[] = {
4817 {
4818 .limits = ath10k_if_limits,
4819 .n_limits = ARRAY_SIZE(ath10k_if_limits),
4820 .max_interfaces = 8,
4821 .num_different_channels = 1,
4822 .beacon_int_infra_match = true,
4823 },
Bartosz Markowskif2595092013-12-10 16:20:39 +01004824};
4825
4826static const struct ieee80211_iface_combination ath10k_10x_if_comb[] = {
Marek Puzyniake8a50f82013-11-20 09:59:47 +02004827 {
Bartosz Markowskif2595092013-12-10 16:20:39 +01004828 .limits = ath10k_10x_if_limits,
4829 .n_limits = ARRAY_SIZE(ath10k_10x_if_limits),
Marek Puzyniake8a50f82013-11-20 09:59:47 +02004830 .max_interfaces = 8,
4831 .num_different_channels = 1,
4832 .beacon_int_infra_match = true,
Bartosz Markowskif2595092013-12-10 16:20:39 +01004833#ifdef CONFIG_ATH10K_DFS_CERTIFIED
Marek Puzyniake8a50f82013-11-20 09:59:47 +02004834 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
4835 BIT(NL80211_CHAN_WIDTH_20) |
4836 BIT(NL80211_CHAN_WIDTH_40) |
4837 BIT(NL80211_CHAN_WIDTH_80),
Marek Puzyniake8a50f82013-11-20 09:59:47 +02004838#endif
Bartosz Markowskif2595092013-12-10 16:20:39 +01004839 },
Kalle Valo5e3dd152013-06-12 20:52:10 +03004840};
4841
4842static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
4843{
4844 struct ieee80211_sta_vht_cap vht_cap = {0};
4845 u16 mcs_map;
Michal Kazior8865bee42013-07-24 12:36:46 +02004846 int i;
Kalle Valo5e3dd152013-06-12 20:52:10 +03004847
4848 vht_cap.vht_supported = 1;
4849 vht_cap.cap = ar->vht_cap_info;
4850
Michal Kazior8865bee42013-07-24 12:36:46 +02004851 mcs_map = 0;
4852 for (i = 0; i < 8; i++) {
4853 if (i < ar->num_rf_chains)
4854 mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i*2);
4855 else
4856 mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i*2);
4857 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03004858
4859 vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
4860 vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
4861
4862 return vht_cap;
4863}
4864
4865static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
4866{
4867 int i;
4868 struct ieee80211_sta_ht_cap ht_cap = {0};
4869
4870 if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
4871 return ht_cap;
4872
4873 ht_cap.ht_supported = 1;
4874 ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
4875 ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
4876 ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
4877 ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
4878 ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
4879
4880 if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
4881 ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
4882
4883 if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
4884 ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
4885
4886 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
4887 u32 smps;
4888
4889 smps = WLAN_HT_CAP_SM_PS_DYNAMIC;
4890 smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
4891
4892 ht_cap.cap |= smps;
4893 }
4894
4895 if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC)
4896 ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
4897
4898 if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
4899 u32 stbc;
4900
4901 stbc = ar->ht_cap_info;
4902 stbc &= WMI_HT_CAP_RX_STBC;
4903 stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
4904 stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
4905 stbc &= IEEE80211_HT_CAP_RX_STBC;
4906
4907 ht_cap.cap |= stbc;
4908 }
4909
4910 if (ar->ht_cap_info & WMI_HT_CAP_LDPC)
4911 ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
4912
4913 if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
4914 ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
4915
4916 /* max AMSDU is implicitly taken from vht_cap_info */
4917 if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
4918 ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
4919
Michal Kazior8865bee42013-07-24 12:36:46 +02004920 for (i = 0; i < ar->num_rf_chains; i++)
Kalle Valo5e3dd152013-06-12 20:52:10 +03004921 ht_cap.mcs.rx_mask[i] = 0xFF;
4922
4923 ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
4924
4925 return ht_cap;
4926}
4927
Kalle Valo5e3dd152013-06-12 20:52:10 +03004928static void ath10k_get_arvif_iter(void *data, u8 *mac,
4929 struct ieee80211_vif *vif)
4930{
4931 struct ath10k_vif_iter *arvif_iter = data;
4932 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4933
4934 if (arvif->vdev_id == arvif_iter->vdev_id)
4935 arvif_iter->arvif = arvif;
4936}
4937
4938struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
4939{
4940 struct ath10k_vif_iter arvif_iter;
4941 u32 flags;
4942
4943 memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
4944 arvif_iter.vdev_id = vdev_id;
4945
4946 flags = IEEE80211_IFACE_ITER_RESUME_ALL;
4947 ieee80211_iterate_active_interfaces_atomic(ar->hw,
4948 flags,
4949 ath10k_get_arvif_iter,
4950 &arvif_iter);
4951 if (!arvif_iter.arvif) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02004952 ath10k_warn(ar, "No VIF found for vdev %d\n", vdev_id);
Kalle Valo5e3dd152013-06-12 20:52:10 +03004953 return NULL;
4954 }
4955
4956 return arvif_iter.arvif;
4957}
4958
4959int ath10k_mac_register(struct ath10k *ar)
4960{
4961 struct ieee80211_supported_band *band;
4962 struct ieee80211_sta_vht_cap vht_cap;
4963 struct ieee80211_sta_ht_cap ht_cap;
4964 void *channels;
4965 int ret;
4966
4967 SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
4968
4969 SET_IEEE80211_DEV(ar->hw, ar->dev);
4970
4971 ht_cap = ath10k_get_ht_cap(ar);
4972 vht_cap = ath10k_create_vht_cap(ar);
4973
4974 if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
4975 channels = kmemdup(ath10k_2ghz_channels,
4976 sizeof(ath10k_2ghz_channels),
4977 GFP_KERNEL);
Michal Kaziord6015b22013-07-22 14:13:30 +02004978 if (!channels) {
4979 ret = -ENOMEM;
4980 goto err_free;
4981 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03004982
4983 band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
4984 band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
4985 band->channels = channels;
4986 band->n_bitrates = ath10k_g_rates_size;
4987 band->bitrates = ath10k_g_rates;
4988 band->ht_cap = ht_cap;
4989
4990 /* vht is not supported in 2.4 GHz */
4991
4992 ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
4993 }
4994
4995 if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
4996 channels = kmemdup(ath10k_5ghz_channels,
4997 sizeof(ath10k_5ghz_channels),
4998 GFP_KERNEL);
4999 if (!channels) {
Michal Kaziord6015b22013-07-22 14:13:30 +02005000 ret = -ENOMEM;
5001 goto err_free;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005002 }
5003
5004 band = &ar->mac.sbands[IEEE80211_BAND_5GHZ];
5005 band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
5006 band->channels = channels;
5007 band->n_bitrates = ath10k_a_rates_size;
5008 band->bitrates = ath10k_a_rates;
5009 band->ht_cap = ht_cap;
5010 band->vht_cap = vht_cap;
5011 ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
5012 }
5013
5014 ar->hw->wiphy->interface_modes =
5015 BIT(NL80211_IFTYPE_STATION) |
Bartosz Markowskid3541812013-12-10 16:20:40 +01005016 BIT(NL80211_IFTYPE_AP);
5017
Ben Greear46acf7b2014-05-16 17:15:38 +03005018 ar->hw->wiphy->available_antennas_rx = ar->supp_rx_chainmask;
5019 ar->hw->wiphy->available_antennas_tx = ar->supp_tx_chainmask;
5020
Bartosz Markowskid3541812013-12-10 16:20:40 +01005021 if (!test_bit(ATH10K_FW_FEATURE_NO_P2P, ar->fw_features))
5022 ar->hw->wiphy->interface_modes |=
5023 BIT(NL80211_IFTYPE_P2P_CLIENT) |
5024 BIT(NL80211_IFTYPE_P2P_GO);
Kalle Valo5e3dd152013-06-12 20:52:10 +03005025
5026 ar->hw->flags = IEEE80211_HW_SIGNAL_DBM |
5027 IEEE80211_HW_SUPPORTS_PS |
5028 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
Kalle Valo5e3dd152013-06-12 20:52:10 +03005029 IEEE80211_HW_MFP_CAPABLE |
5030 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
5031 IEEE80211_HW_HAS_RATE_CONTROL |
Janusz Dziedzic2f0f1122014-02-26 18:42:09 +02005032 IEEE80211_HW_AP_LINK_PS |
5033 IEEE80211_HW_SPECTRUM_MGMT;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005034
Eliad Peller0d8614b2014-09-10 14:07:36 +03005035 ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
5036
Kalle Valo5e3dd152013-06-12 20:52:10 +03005037 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
Eliad Peller0d8614b2014-09-10 14:07:36 +03005038 ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005039
5040 if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
5041 ar->hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
5042 ar->hw->flags |= IEEE80211_HW_TX_AMPDU_SETUP_IN_HW;
5043 }
5044
5045 ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
5046 ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
5047
5048 ar->hw->vif_data_size = sizeof(struct ath10k_vif);
Michal Kazior9797feb2014-02-14 14:49:48 +01005049 ar->hw->sta_data_size = sizeof(struct ath10k_sta);
Kalle Valo5e3dd152013-06-12 20:52:10 +03005050
Kalle Valo5e3dd152013-06-12 20:52:10 +03005051 ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
5052
5053 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
Michal Kaziorc2df44b2014-01-23 11:38:26 +01005054 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005055 ar->hw->wiphy->max_remain_on_channel_duration = 5000;
5056
5057 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
Rajkumar Manoharan78157a12014-11-17 16:44:15 +02005058 ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE;
5059
Kalle Valo5e3dd152013-06-12 20:52:10 +03005060 /*
5061 * on LL hardware queues are managed entirely by the FW
5062 * so we only advertise to mac we can do the queues thing
5063 */
5064 ar->hw->queues = 4;
5065
Bartosz Markowskif2595092013-12-10 16:20:39 +01005066 if (test_bit(ATH10K_FW_FEATURE_WMI_10X, ar->fw_features)) {
5067 ar->hw->wiphy->iface_combinations = ath10k_10x_if_comb;
5068 ar->hw->wiphy->n_iface_combinations =
5069 ARRAY_SIZE(ath10k_10x_if_comb);
5070 } else {
5071 ar->hw->wiphy->iface_combinations = ath10k_if_comb;
5072 ar->hw->wiphy->n_iface_combinations =
5073 ARRAY_SIZE(ath10k_if_comb);
Michal Kaziorcf850d12014-07-24 20:07:00 +03005074
5075 ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
Bartosz Markowskif2595092013-12-10 16:20:39 +01005076 }
Kalle Valo5e3dd152013-06-12 20:52:10 +03005077
Michal Kazior7c199992013-07-31 10:47:57 +02005078 ar->hw->netdev_features = NETIF_F_HW_CSUM;
5079
Janusz Dziedzic9702c682013-11-20 09:59:41 +02005080 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED)) {
5081 /* Init ath dfs pattern detector */
5082 ar->ath_common.debug_mask = ATH_DBG_DFS;
5083 ar->dfs_detector = dfs_pattern_detector_init(&ar->ath_common,
5084 NL80211_DFS_UNSET);
5085
5086 if (!ar->dfs_detector)
Michal Kazior7aa7a722014-08-25 12:09:38 +02005087 ath10k_warn(ar, "failed to initialise DFS pattern detector\n");
Janusz Dziedzic9702c682013-11-20 09:59:41 +02005088 }
5089
Kalle Valo5e3dd152013-06-12 20:52:10 +03005090 ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
5091 ath10k_reg_notifier);
5092 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02005093 ath10k_err(ar, "failed to initialise regulatory: %i\n", ret);
Michal Kaziord6015b22013-07-22 14:13:30 +02005094 goto err_free;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005095 }
5096
5097 ret = ieee80211_register_hw(ar->hw);
5098 if (ret) {
Michal Kazior7aa7a722014-08-25 12:09:38 +02005099 ath10k_err(ar, "failed to register ieee80211: %d\n", ret);
Michal Kaziord6015b22013-07-22 14:13:30 +02005100 goto err_free;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005101 }
5102
5103 if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
5104 ret = regulatory_hint(ar->hw->wiphy,
5105 ar->ath_common.regulatory.alpha2);
5106 if (ret)
Michal Kaziord6015b22013-07-22 14:13:30 +02005107 goto err_unregister;
Kalle Valo5e3dd152013-06-12 20:52:10 +03005108 }
5109
5110 return 0;
Michal Kaziord6015b22013-07-22 14:13:30 +02005111
5112err_unregister:
Kalle Valo5e3dd152013-06-12 20:52:10 +03005113 ieee80211_unregister_hw(ar->hw);
Michal Kaziord6015b22013-07-22 14:13:30 +02005114err_free:
5115 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
5116 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
5117
Kalle Valo5e3dd152013-06-12 20:52:10 +03005118 return ret;
5119}
5120
5121void ath10k_mac_unregister(struct ath10k *ar)
5122{
5123 ieee80211_unregister_hw(ar->hw);
5124
Janusz Dziedzic9702c682013-11-20 09:59:41 +02005125 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
5126 ar->dfs_detector->exit(ar->dfs_detector);
5127
Kalle Valo5e3dd152013-06-12 20:52:10 +03005128 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
5129 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
5130
5131 SET_IEEE80211_DEV(ar->hw, NULL);
5132}