ath10k: update station counting
[cascardo/linux.git] / drivers / net / wireless / ath / ath10k / mac.c
1 /*
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
23 #include "hif.h"
24 #include "core.h"
25 #include "debug.h"
26 #include "wmi.h"
27 #include "htt.h"
28 #include "txrx.h"
29 #include "testmode.h"
30 #include "wmi.h"
31 #include "wmi-ops.h"
32 #include "wow.h"
33
34 /**********/
35 /* Crypto */
36 /**********/
37
38 static int ath10k_send_key(struct ath10k_vif *arvif,
39                            struct ieee80211_key_conf *key,
40                            enum set_key_cmd cmd,
41                            const u8 *macaddr, u32 flags)
42 {
43         struct ath10k *ar = arvif->ar;
44         struct wmi_vdev_install_key_arg arg = {
45                 .vdev_id = arvif->vdev_id,
46                 .key_idx = key->keyidx,
47                 .key_len = key->keylen,
48                 .key_data = key->key,
49                 .key_flags = flags,
50                 .macaddr = macaddr,
51         };
52
53         lockdep_assert_held(&arvif->ar->conf_mutex);
54
55         switch (key->cipher) {
56         case WLAN_CIPHER_SUITE_CCMP:
57                 arg.key_cipher = WMI_CIPHER_AES_CCM;
58                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
59                 break;
60         case WLAN_CIPHER_SUITE_TKIP:
61                 arg.key_cipher = WMI_CIPHER_TKIP;
62                 arg.key_txmic_len = 8;
63                 arg.key_rxmic_len = 8;
64                 break;
65         case WLAN_CIPHER_SUITE_WEP40:
66         case WLAN_CIPHER_SUITE_WEP104:
67                 arg.key_cipher = WMI_CIPHER_WEP;
68                 break;
69         case WLAN_CIPHER_SUITE_AES_CMAC:
70                 WARN_ON(1);
71                 return -EINVAL;
72         default:
73                 ath10k_warn(ar, "cipher %d is not supported\n", key->cipher);
74                 return -EOPNOTSUPP;
75         }
76
77         if (cmd == DISABLE_KEY) {
78                 arg.key_cipher = WMI_CIPHER_NONE;
79                 arg.key_data = NULL;
80         }
81
82         return ath10k_wmi_vdev_install_key(arvif->ar, &arg);
83 }
84
85 static int ath10k_install_key(struct ath10k_vif *arvif,
86                               struct ieee80211_key_conf *key,
87                               enum set_key_cmd cmd,
88                               const u8 *macaddr, u32 flags)
89 {
90         struct ath10k *ar = arvif->ar;
91         int ret;
92
93         lockdep_assert_held(&ar->conf_mutex);
94
95         reinit_completion(&ar->install_key_done);
96
97         ret = ath10k_send_key(arvif, key, cmd, macaddr, flags);
98         if (ret)
99                 return ret;
100
101         ret = wait_for_completion_timeout(&ar->install_key_done, 3*HZ);
102         if (ret == 0)
103                 return -ETIMEDOUT;
104
105         return 0;
106 }
107
108 static int ath10k_install_peer_wep_keys(struct ath10k_vif *arvif,
109                                         const u8 *addr)
110 {
111         struct ath10k *ar = arvif->ar;
112         struct ath10k_peer *peer;
113         int ret;
114         int i;
115         u32 flags;
116
117         lockdep_assert_held(&ar->conf_mutex);
118
119         spin_lock_bh(&ar->data_lock);
120         peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
121         spin_unlock_bh(&ar->data_lock);
122
123         if (!peer)
124                 return -ENOENT;
125
126         for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
127                 if (arvif->wep_keys[i] == NULL)
128                         continue;
129
130                 flags = 0;
131                 flags |= WMI_KEY_PAIRWISE;
132
133                 /* set TX_USAGE flag for default key id */
134                 if (arvif->def_wep_key_idx == i)
135                         flags |= WMI_KEY_TX_USAGE;
136
137                 ret = ath10k_install_key(arvif, arvif->wep_keys[i], SET_KEY,
138                                          addr, flags);
139                 if (ret)
140                         return ret;
141
142                 spin_lock_bh(&ar->data_lock);
143                 peer->keys[i] = arvif->wep_keys[i];
144                 spin_unlock_bh(&ar->data_lock);
145         }
146
147         return 0;
148 }
149
150 static int ath10k_clear_peer_keys(struct ath10k_vif *arvif,
151                                   const u8 *addr)
152 {
153         struct ath10k *ar = arvif->ar;
154         struct ath10k_peer *peer;
155         int first_errno = 0;
156         int ret;
157         int i;
158         u32 flags = 0;
159
160         lockdep_assert_held(&ar->conf_mutex);
161
162         spin_lock_bh(&ar->data_lock);
163         peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
164         spin_unlock_bh(&ar->data_lock);
165
166         if (!peer)
167                 return -ENOENT;
168
169         for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
170                 if (peer->keys[i] == NULL)
171                         continue;
172
173                 /* key flags are not required to delete the key */
174                 ret = ath10k_install_key(arvif, peer->keys[i],
175                                          DISABLE_KEY, addr, flags);
176                 if (ret && first_errno == 0)
177                         first_errno = ret;
178
179                 if (ret)
180                         ath10k_warn(ar, "failed to remove peer wep key %d: %d\n",
181                                     i, ret);
182
183                 spin_lock_bh(&ar->data_lock);
184                 peer->keys[i] = NULL;
185                 spin_unlock_bh(&ar->data_lock);
186         }
187
188         return first_errno;
189 }
190
191 bool ath10k_mac_is_peer_wep_key_set(struct ath10k *ar, const u8 *addr,
192                                     u8 keyidx)
193 {
194         struct ath10k_peer *peer;
195         int i;
196
197         lockdep_assert_held(&ar->data_lock);
198
199         /* We don't know which vdev this peer belongs to,
200          * since WMI doesn't give us that information.
201          *
202          * FIXME: multi-bss needs to be handled.
203          */
204         peer = ath10k_peer_find(ar, 0, addr);
205         if (!peer)
206                 return false;
207
208         for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
209                 if (peer->keys[i] && peer->keys[i]->keyidx == keyidx)
210                         return true;
211         }
212
213         return false;
214 }
215
216 static int ath10k_clear_vdev_key(struct ath10k_vif *arvif,
217                                  struct ieee80211_key_conf *key)
218 {
219         struct ath10k *ar = arvif->ar;
220         struct ath10k_peer *peer;
221         u8 addr[ETH_ALEN];
222         int first_errno = 0;
223         int ret;
224         int i;
225         u32 flags = 0;
226
227         lockdep_assert_held(&ar->conf_mutex);
228
229         for (;;) {
230                 /* since ath10k_install_key we can't hold data_lock all the
231                  * time, so we try to remove the keys incrementally */
232                 spin_lock_bh(&ar->data_lock);
233                 i = 0;
234                 list_for_each_entry(peer, &ar->peers, list) {
235                         for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
236                                 if (peer->keys[i] == key) {
237                                         ether_addr_copy(addr, peer->addr);
238                                         peer->keys[i] = NULL;
239                                         break;
240                                 }
241                         }
242
243                         if (i < ARRAY_SIZE(peer->keys))
244                                 break;
245                 }
246                 spin_unlock_bh(&ar->data_lock);
247
248                 if (i == ARRAY_SIZE(peer->keys))
249                         break;
250                 /* key flags are not required to delete the key */
251                 ret = ath10k_install_key(arvif, key, DISABLE_KEY, addr, flags);
252                 if (ret && first_errno == 0)
253                         first_errno = ret;
254
255                 if (ret)
256                         ath10k_warn(ar, "failed to remove key for %pM: %d\n",
257                                     addr, ret);
258         }
259
260         return first_errno;
261 }
262
263 static int ath10k_mac_vif_sta_fix_wep_key(struct ath10k_vif *arvif)
264 {
265         struct ath10k *ar = arvif->ar;
266         enum nl80211_iftype iftype = arvif->vif->type;
267         struct ieee80211_key_conf *key;
268         u32 flags = 0;
269         int num = 0;
270         int i;
271         int ret;
272
273         lockdep_assert_held(&ar->conf_mutex);
274
275         if (iftype != NL80211_IFTYPE_STATION)
276                 return 0;
277
278         for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
279                 if (arvif->wep_keys[i]) {
280                         key = arvif->wep_keys[i];
281                         ++num;
282                 }
283         }
284
285         if (num != 1)
286                 return 0;
287
288         flags |= WMI_KEY_PAIRWISE;
289         flags |= WMI_KEY_TX_USAGE;
290
291         ret = ath10k_install_key(arvif, key, SET_KEY, arvif->bssid, flags);
292         if (ret) {
293                 ath10k_warn(ar, "failed to install key %i on vdev %i: %d\n",
294                             key->keyidx, arvif->vdev_id, ret);
295                 return ret;
296         }
297
298         return 0;
299 }
300
301 static int ath10k_mac_vif_update_wep_key(struct ath10k_vif *arvif,
302                                          struct ieee80211_key_conf *key)
303 {
304         struct ath10k *ar = arvif->ar;
305         struct ath10k_peer *peer;
306         int ret;
307
308         lockdep_assert_held(&ar->conf_mutex);
309
310         list_for_each_entry(peer, &ar->peers, list) {
311                 if (!memcmp(peer->addr, arvif->vif->addr, ETH_ALEN))
312                         continue;
313
314                 if (!memcmp(peer->addr, arvif->bssid, ETH_ALEN))
315                         continue;
316
317                 if (peer->keys[key->keyidx] == key)
318                         continue;
319
320                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vif vdev %i update key %i needs update\n",
321                            arvif->vdev_id, key->keyidx);
322
323                 ret = ath10k_install_peer_wep_keys(arvif, peer->addr);
324                 if (ret) {
325                         ath10k_warn(ar, "failed to update wep keys on vdev %i for peer %pM: %d\n",
326                                     arvif->vdev_id, peer->addr, ret);
327                         return ret;
328                 }
329         }
330
331         return 0;
332 }
333
334 /*********************/
335 /* General utilities */
336 /*********************/
337
338 static inline enum wmi_phy_mode
339 chan_to_phymode(const struct cfg80211_chan_def *chandef)
340 {
341         enum wmi_phy_mode phymode = MODE_UNKNOWN;
342
343         switch (chandef->chan->band) {
344         case IEEE80211_BAND_2GHZ:
345                 switch (chandef->width) {
346                 case NL80211_CHAN_WIDTH_20_NOHT:
347                         if (chandef->chan->flags & IEEE80211_CHAN_NO_OFDM)
348                                 phymode = MODE_11B;
349                         else
350                                 phymode = MODE_11G;
351                         break;
352                 case NL80211_CHAN_WIDTH_20:
353                         phymode = MODE_11NG_HT20;
354                         break;
355                 case NL80211_CHAN_WIDTH_40:
356                         phymode = MODE_11NG_HT40;
357                         break;
358                 case NL80211_CHAN_WIDTH_5:
359                 case NL80211_CHAN_WIDTH_10:
360                 case NL80211_CHAN_WIDTH_80:
361                 case NL80211_CHAN_WIDTH_80P80:
362                 case NL80211_CHAN_WIDTH_160:
363                         phymode = MODE_UNKNOWN;
364                         break;
365                 }
366                 break;
367         case IEEE80211_BAND_5GHZ:
368                 switch (chandef->width) {
369                 case NL80211_CHAN_WIDTH_20_NOHT:
370                         phymode = MODE_11A;
371                         break;
372                 case NL80211_CHAN_WIDTH_20:
373                         phymode = MODE_11NA_HT20;
374                         break;
375                 case NL80211_CHAN_WIDTH_40:
376                         phymode = MODE_11NA_HT40;
377                         break;
378                 case NL80211_CHAN_WIDTH_80:
379                         phymode = MODE_11AC_VHT80;
380                         break;
381                 case NL80211_CHAN_WIDTH_5:
382                 case NL80211_CHAN_WIDTH_10:
383                 case NL80211_CHAN_WIDTH_80P80:
384                 case NL80211_CHAN_WIDTH_160:
385                         phymode = MODE_UNKNOWN;
386                         break;
387                 }
388                 break;
389         default:
390                 break;
391         }
392
393         WARN_ON(phymode == MODE_UNKNOWN);
394         return phymode;
395 }
396
397 static u8 ath10k_parse_mpdudensity(u8 mpdudensity)
398 {
399 /*
400  * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
401  *   0 for no restriction
402  *   1 for 1/4 us
403  *   2 for 1/2 us
404  *   3 for 1 us
405  *   4 for 2 us
406  *   5 for 4 us
407  *   6 for 8 us
408  *   7 for 16 us
409  */
410         switch (mpdudensity) {
411         case 0:
412                 return 0;
413         case 1:
414         case 2:
415         case 3:
416         /* Our lower layer calculations limit our precision to
417            1 microsecond */
418                 return 1;
419         case 4:
420                 return 2;
421         case 5:
422                 return 4;
423         case 6:
424                 return 8;
425         case 7:
426                 return 16;
427         default:
428                 return 0;
429         }
430 }
431
432 static int ath10k_peer_create(struct ath10k *ar, u32 vdev_id, const u8 *addr,
433                               enum wmi_peer_type peer_type)
434 {
435         int ret;
436
437         lockdep_assert_held(&ar->conf_mutex);
438
439         if (ar->num_peers >= ar->max_num_peers)
440                 return -ENOBUFS;
441
442         ret = ath10k_wmi_peer_create(ar, vdev_id, addr, peer_type);
443         if (ret) {
444                 ath10k_warn(ar, "failed to create wmi peer %pM on vdev %i: %i\n",
445                             addr, vdev_id, ret);
446                 return ret;
447         }
448
449         ret = ath10k_wait_for_peer_created(ar, vdev_id, addr);
450         if (ret) {
451                 ath10k_warn(ar, "failed to wait for created wmi peer %pM on vdev %i: %i\n",
452                             addr, vdev_id, ret);
453                 return ret;
454         }
455
456         ar->num_peers++;
457
458         return 0;
459 }
460
461 static int ath10k_mac_set_kickout(struct ath10k_vif *arvif)
462 {
463         struct ath10k *ar = arvif->ar;
464         u32 param;
465         int ret;
466
467         param = ar->wmi.pdev_param->sta_kickout_th;
468         ret = ath10k_wmi_pdev_set_param(ar, param,
469                                         ATH10K_KICKOUT_THRESHOLD);
470         if (ret) {
471                 ath10k_warn(ar, "failed to set kickout threshold on vdev %i: %d\n",
472                             arvif->vdev_id, ret);
473                 return ret;
474         }
475
476         param = ar->wmi.vdev_param->ap_keepalive_min_idle_inactive_time_secs;
477         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
478                                         ATH10K_KEEPALIVE_MIN_IDLE);
479         if (ret) {
480                 ath10k_warn(ar, "failed to set keepalive minimum idle time on vdev %i: %d\n",
481                             arvif->vdev_id, ret);
482                 return ret;
483         }
484
485         param = ar->wmi.vdev_param->ap_keepalive_max_idle_inactive_time_secs;
486         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
487                                         ATH10K_KEEPALIVE_MAX_IDLE);
488         if (ret) {
489                 ath10k_warn(ar, "failed to set keepalive maximum idle time on vdev %i: %d\n",
490                             arvif->vdev_id, ret);
491                 return ret;
492         }
493
494         param = ar->wmi.vdev_param->ap_keepalive_max_unresponsive_time_secs;
495         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
496                                         ATH10K_KEEPALIVE_MAX_UNRESPONSIVE);
497         if (ret) {
498                 ath10k_warn(ar, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
499                             arvif->vdev_id, ret);
500                 return ret;
501         }
502
503         return 0;
504 }
505
506 static int ath10k_mac_set_rts(struct ath10k_vif *arvif, u32 value)
507 {
508         struct ath10k *ar = arvif->ar;
509         u32 vdev_param;
510
511         vdev_param = ar->wmi.vdev_param->rts_threshold;
512         return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
513 }
514
515 static int ath10k_mac_set_frag(struct ath10k_vif *arvif, u32 value)
516 {
517         struct ath10k *ar = arvif->ar;
518         u32 vdev_param;
519
520         if (value != 0xFFFFFFFF)
521                 value = clamp_t(u32, arvif->ar->hw->wiphy->frag_threshold,
522                                 ATH10K_FRAGMT_THRESHOLD_MIN,
523                                 ATH10K_FRAGMT_THRESHOLD_MAX);
524
525         vdev_param = ar->wmi.vdev_param->fragmentation_threshold;
526         return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
527 }
528
529 static int ath10k_peer_delete(struct ath10k *ar, u32 vdev_id, const u8 *addr)
530 {
531         int ret;
532
533         lockdep_assert_held(&ar->conf_mutex);
534
535         ret = ath10k_wmi_peer_delete(ar, vdev_id, addr);
536         if (ret)
537                 return ret;
538
539         ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
540         if (ret)
541                 return ret;
542
543         ar->num_peers--;
544
545         return 0;
546 }
547
548 static void ath10k_peer_cleanup(struct ath10k *ar, u32 vdev_id)
549 {
550         struct ath10k_peer *peer, *tmp;
551
552         lockdep_assert_held(&ar->conf_mutex);
553
554         spin_lock_bh(&ar->data_lock);
555         list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
556                 if (peer->vdev_id != vdev_id)
557                         continue;
558
559                 ath10k_warn(ar, "removing stale peer %pM from vdev_id %d\n",
560                             peer->addr, vdev_id);
561
562                 list_del(&peer->list);
563                 kfree(peer);
564                 ar->num_peers--;
565         }
566         spin_unlock_bh(&ar->data_lock);
567 }
568
569 static void ath10k_peer_cleanup_all(struct ath10k *ar)
570 {
571         struct ath10k_peer *peer, *tmp;
572
573         lockdep_assert_held(&ar->conf_mutex);
574
575         spin_lock_bh(&ar->data_lock);
576         list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
577                 list_del(&peer->list);
578                 kfree(peer);
579         }
580         spin_unlock_bh(&ar->data_lock);
581
582         ar->num_peers = 0;
583         ar->num_stations = 0;
584 }
585
586 /************************/
587 /* Interface management */
588 /************************/
589
590 void ath10k_mac_vif_beacon_free(struct ath10k_vif *arvif)
591 {
592         struct ath10k *ar = arvif->ar;
593
594         lockdep_assert_held(&ar->data_lock);
595
596         if (!arvif->beacon)
597                 return;
598
599         if (!arvif->beacon_buf)
600                 dma_unmap_single(ar->dev, ATH10K_SKB_CB(arvif->beacon)->paddr,
601                                  arvif->beacon->len, DMA_TO_DEVICE);
602
603         if (WARN_ON(arvif->beacon_state != ATH10K_BEACON_SCHEDULED &&
604                     arvif->beacon_state != ATH10K_BEACON_SENT))
605                 return;
606
607         dev_kfree_skb_any(arvif->beacon);
608
609         arvif->beacon = NULL;
610         arvif->beacon_state = ATH10K_BEACON_SCHEDULED;
611 }
612
613 static void ath10k_mac_vif_beacon_cleanup(struct ath10k_vif *arvif)
614 {
615         struct ath10k *ar = arvif->ar;
616
617         lockdep_assert_held(&ar->data_lock);
618
619         ath10k_mac_vif_beacon_free(arvif);
620
621         if (arvif->beacon_buf) {
622                 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
623                                   arvif->beacon_buf, arvif->beacon_paddr);
624                 arvif->beacon_buf = NULL;
625         }
626 }
627
628 static inline int ath10k_vdev_setup_sync(struct ath10k *ar)
629 {
630         int ret;
631
632         lockdep_assert_held(&ar->conf_mutex);
633
634         if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags))
635                 return -ESHUTDOWN;
636
637         ret = wait_for_completion_timeout(&ar->vdev_setup_done,
638                                           ATH10K_VDEV_SETUP_TIMEOUT_HZ);
639         if (ret == 0)
640                 return -ETIMEDOUT;
641
642         return 0;
643 }
644
645 static int ath10k_monitor_vdev_start(struct ath10k *ar, int vdev_id)
646 {
647         struct cfg80211_chan_def *chandef = &ar->chandef;
648         struct ieee80211_channel *channel = chandef->chan;
649         struct wmi_vdev_start_request_arg arg = {};
650         int ret = 0;
651
652         lockdep_assert_held(&ar->conf_mutex);
653
654         arg.vdev_id = vdev_id;
655         arg.channel.freq = channel->center_freq;
656         arg.channel.band_center_freq1 = chandef->center_freq1;
657
658         /* TODO setup this dynamically, what in case we
659            don't have any vifs? */
660         arg.channel.mode = chan_to_phymode(chandef);
661         arg.channel.chan_radar =
662                         !!(channel->flags & IEEE80211_CHAN_RADAR);
663
664         arg.channel.min_power = 0;
665         arg.channel.max_power = channel->max_power * 2;
666         arg.channel.max_reg_power = channel->max_reg_power * 2;
667         arg.channel.max_antenna_gain = channel->max_antenna_gain * 2;
668
669         reinit_completion(&ar->vdev_setup_done);
670
671         ret = ath10k_wmi_vdev_start(ar, &arg);
672         if (ret) {
673                 ath10k_warn(ar, "failed to request monitor vdev %i start: %d\n",
674                             vdev_id, ret);
675                 return ret;
676         }
677
678         ret = ath10k_vdev_setup_sync(ar);
679         if (ret) {
680                 ath10k_warn(ar, "failed to synchronize setup for monitor vdev %i start: %d\n",
681                             vdev_id, ret);
682                 return ret;
683         }
684
685         ret = ath10k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
686         if (ret) {
687                 ath10k_warn(ar, "failed to put up monitor vdev %i: %d\n",
688                             vdev_id, ret);
689                 goto vdev_stop;
690         }
691
692         ar->monitor_vdev_id = vdev_id;
693
694         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i started\n",
695                    ar->monitor_vdev_id);
696         return 0;
697
698 vdev_stop:
699         ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
700         if (ret)
701                 ath10k_warn(ar, "failed to stop monitor vdev %i after start failure: %d\n",
702                             ar->monitor_vdev_id, ret);
703
704         return ret;
705 }
706
707 static int ath10k_monitor_vdev_stop(struct ath10k *ar)
708 {
709         int ret = 0;
710
711         lockdep_assert_held(&ar->conf_mutex);
712
713         ret = ath10k_wmi_vdev_down(ar, ar->monitor_vdev_id);
714         if (ret)
715                 ath10k_warn(ar, "failed to put down monitor vdev %i: %d\n",
716                             ar->monitor_vdev_id, ret);
717
718         reinit_completion(&ar->vdev_setup_done);
719
720         ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
721         if (ret)
722                 ath10k_warn(ar, "failed to to request monitor vdev %i stop: %d\n",
723                             ar->monitor_vdev_id, ret);
724
725         ret = ath10k_vdev_setup_sync(ar);
726         if (ret)
727                 ath10k_warn(ar, "failed to synchronize monitor vdev %i stop: %d\n",
728                             ar->monitor_vdev_id, ret);
729
730         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i stopped\n",
731                    ar->monitor_vdev_id);
732         return ret;
733 }
734
735 static int ath10k_monitor_vdev_create(struct ath10k *ar)
736 {
737         int bit, ret = 0;
738
739         lockdep_assert_held(&ar->conf_mutex);
740
741         if (ar->free_vdev_map == 0) {
742                 ath10k_warn(ar, "failed to find free vdev id for monitor vdev\n");
743                 return -ENOMEM;
744         }
745
746         bit = __ffs64(ar->free_vdev_map);
747
748         ar->monitor_vdev_id = bit;
749
750         ret = ath10k_wmi_vdev_create(ar, ar->monitor_vdev_id,
751                                      WMI_VDEV_TYPE_MONITOR,
752                                      0, ar->mac_addr);
753         if (ret) {
754                 ath10k_warn(ar, "failed to request monitor vdev %i creation: %d\n",
755                             ar->monitor_vdev_id, ret);
756                 return ret;
757         }
758
759         ar->free_vdev_map &= ~(1LL << ar->monitor_vdev_id);
760         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d created\n",
761                    ar->monitor_vdev_id);
762
763         return 0;
764 }
765
766 static int ath10k_monitor_vdev_delete(struct ath10k *ar)
767 {
768         int ret = 0;
769
770         lockdep_assert_held(&ar->conf_mutex);
771
772         ret = ath10k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
773         if (ret) {
774                 ath10k_warn(ar, "failed to request wmi monitor vdev %i removal: %d\n",
775                             ar->monitor_vdev_id, ret);
776                 return ret;
777         }
778
779         ar->free_vdev_map |= 1LL << ar->monitor_vdev_id;
780
781         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d deleted\n",
782                    ar->monitor_vdev_id);
783         return ret;
784 }
785
786 static int ath10k_monitor_start(struct ath10k *ar)
787 {
788         int ret;
789
790         lockdep_assert_held(&ar->conf_mutex);
791
792         ret = ath10k_monitor_vdev_create(ar);
793         if (ret) {
794                 ath10k_warn(ar, "failed to create monitor vdev: %d\n", ret);
795                 return ret;
796         }
797
798         ret = ath10k_monitor_vdev_start(ar, ar->monitor_vdev_id);
799         if (ret) {
800                 ath10k_warn(ar, "failed to start monitor vdev: %d\n", ret);
801                 ath10k_monitor_vdev_delete(ar);
802                 return ret;
803         }
804
805         ar->monitor_started = true;
806         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor started\n");
807
808         return 0;
809 }
810
811 static int ath10k_monitor_stop(struct ath10k *ar)
812 {
813         int ret;
814
815         lockdep_assert_held(&ar->conf_mutex);
816
817         ret = ath10k_monitor_vdev_stop(ar);
818         if (ret) {
819                 ath10k_warn(ar, "failed to stop monitor vdev: %d\n", ret);
820                 return ret;
821         }
822
823         ret = ath10k_monitor_vdev_delete(ar);
824         if (ret) {
825                 ath10k_warn(ar, "failed to delete monitor vdev: %d\n", ret);
826                 return ret;
827         }
828
829         ar->monitor_started = false;
830         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopped\n");
831
832         return 0;
833 }
834
835 static bool ath10k_mac_should_disable_promisc(struct ath10k *ar)
836 {
837         struct ath10k_vif *arvif;
838
839         if (!(ar->filter_flags & FIF_PROMISC_IN_BSS))
840                 return true;
841
842         if (!ar->num_started_vdevs)
843                 return false;
844
845         list_for_each_entry(arvif, &ar->arvifs, list)
846                 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
847                         return false;
848
849         ath10k_dbg(ar, ATH10K_DBG_MAC,
850                    "mac disabling promiscuous mode because vdev is started\n");
851         return true;
852 }
853
854 static int ath10k_monitor_recalc(struct ath10k *ar)
855 {
856         bool should_start;
857
858         lockdep_assert_held(&ar->conf_mutex);
859
860         should_start = ar->monitor ||
861                        !ath10k_mac_should_disable_promisc(ar) ||
862                        test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
863
864         ath10k_dbg(ar, ATH10K_DBG_MAC,
865                    "mac monitor recalc started? %d should? %d\n",
866                    ar->monitor_started, should_start);
867
868         if (should_start == ar->monitor_started)
869                 return 0;
870
871         if (should_start)
872                 return ath10k_monitor_start(ar);
873
874         return ath10k_monitor_stop(ar);
875 }
876
877 static int ath10k_recalc_rtscts_prot(struct ath10k_vif *arvif)
878 {
879         struct ath10k *ar = arvif->ar;
880         u32 vdev_param, rts_cts = 0;
881
882         lockdep_assert_held(&ar->conf_mutex);
883
884         vdev_param = ar->wmi.vdev_param->enable_rtscts;
885
886         rts_cts |= SM(WMI_RTSCTS_ENABLED, WMI_RTSCTS_SET);
887
888         if (arvif->num_legacy_stations > 0)
889                 rts_cts |= SM(WMI_RTSCTS_ACROSS_SW_RETRIES,
890                               WMI_RTSCTS_PROFILE);
891         else
892                 rts_cts |= SM(WMI_RTSCTS_FOR_SECOND_RATESERIES,
893                               WMI_RTSCTS_PROFILE);
894
895         return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
896                                          rts_cts);
897 }
898
899 static int ath10k_start_cac(struct ath10k *ar)
900 {
901         int ret;
902
903         lockdep_assert_held(&ar->conf_mutex);
904
905         set_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
906
907         ret = ath10k_monitor_recalc(ar);
908         if (ret) {
909                 ath10k_warn(ar, "failed to start monitor (cac): %d\n", ret);
910                 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
911                 return ret;
912         }
913
914         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac start monitor vdev %d\n",
915                    ar->monitor_vdev_id);
916
917         return 0;
918 }
919
920 static int ath10k_stop_cac(struct ath10k *ar)
921 {
922         lockdep_assert_held(&ar->conf_mutex);
923
924         /* CAC is not running - do nothing */
925         if (!test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags))
926                 return 0;
927
928         clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
929         ath10k_monitor_stop(ar);
930
931         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac finished\n");
932
933         return 0;
934 }
935
936 static void ath10k_recalc_radar_detection(struct ath10k *ar)
937 {
938         int ret;
939
940         lockdep_assert_held(&ar->conf_mutex);
941
942         ath10k_stop_cac(ar);
943
944         if (!ar->radar_enabled)
945                 return;
946
947         if (ar->num_started_vdevs > 0)
948                 return;
949
950         ret = ath10k_start_cac(ar);
951         if (ret) {
952                 /*
953                  * Not possible to start CAC on current channel so starting
954                  * radiation is not allowed, make this channel DFS_UNAVAILABLE
955                  * by indicating that radar was detected.
956                  */
957                 ath10k_warn(ar, "failed to start CAC: %d\n", ret);
958                 ieee80211_radar_detected(ar->hw);
959         }
960 }
961
962 static int ath10k_vdev_stop(struct ath10k_vif *arvif)
963 {
964         struct ath10k *ar = arvif->ar;
965         int ret;
966
967         lockdep_assert_held(&ar->conf_mutex);
968
969         reinit_completion(&ar->vdev_setup_done);
970
971         ret = ath10k_wmi_vdev_stop(ar, arvif->vdev_id);
972         if (ret) {
973                 ath10k_warn(ar, "failed to stop WMI vdev %i: %d\n",
974                             arvif->vdev_id, ret);
975                 return ret;
976         }
977
978         ret = ath10k_vdev_setup_sync(ar);
979         if (ret) {
980                 ath10k_warn(ar, "failed to syncronise setup for vdev %i: %d\n",
981                             arvif->vdev_id, ret);
982                 return ret;
983         }
984
985         WARN_ON(ar->num_started_vdevs == 0);
986
987         if (ar->num_started_vdevs != 0) {
988                 ar->num_started_vdevs--;
989                 ath10k_recalc_radar_detection(ar);
990         }
991
992         return ret;
993 }
994
995 static int ath10k_vdev_start_restart(struct ath10k_vif *arvif, bool restart)
996 {
997         struct ath10k *ar = arvif->ar;
998         struct cfg80211_chan_def *chandef = &ar->chandef;
999         struct wmi_vdev_start_request_arg arg = {};
1000         int ret = 0, ret2;
1001
1002         lockdep_assert_held(&ar->conf_mutex);
1003
1004         reinit_completion(&ar->vdev_setup_done);
1005
1006         arg.vdev_id = arvif->vdev_id;
1007         arg.dtim_period = arvif->dtim_period;
1008         arg.bcn_intval = arvif->beacon_interval;
1009
1010         arg.channel.freq = chandef->chan->center_freq;
1011         arg.channel.band_center_freq1 = chandef->center_freq1;
1012         arg.channel.mode = chan_to_phymode(chandef);
1013
1014         arg.channel.min_power = 0;
1015         arg.channel.max_power = chandef->chan->max_power * 2;
1016         arg.channel.max_reg_power = chandef->chan->max_reg_power * 2;
1017         arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
1018
1019         if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
1020                 arg.ssid = arvif->u.ap.ssid;
1021                 arg.ssid_len = arvif->u.ap.ssid_len;
1022                 arg.hidden_ssid = arvif->u.ap.hidden_ssid;
1023
1024                 /* For now allow DFS for AP mode */
1025                 arg.channel.chan_radar =
1026                         !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
1027         } else if (arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
1028                 arg.ssid = arvif->vif->bss_conf.ssid;
1029                 arg.ssid_len = arvif->vif->bss_conf.ssid_len;
1030         }
1031
1032         ath10k_dbg(ar, ATH10K_DBG_MAC,
1033                    "mac vdev %d start center_freq %d phymode %s\n",
1034                    arg.vdev_id, arg.channel.freq,
1035                    ath10k_wmi_phymode_str(arg.channel.mode));
1036
1037         if (restart)
1038                 ret = ath10k_wmi_vdev_restart(ar, &arg);
1039         else
1040                 ret = ath10k_wmi_vdev_start(ar, &arg);
1041
1042         if (ret) {
1043                 ath10k_warn(ar, "failed to start WMI vdev %i: %d\n",
1044                             arg.vdev_id, ret);
1045                 return ret;
1046         }
1047
1048         ret = ath10k_vdev_setup_sync(ar);
1049         if (ret) {
1050                 ath10k_warn(ar,
1051                             "failed to synchronize setup for vdev %i restart %d: %d\n",
1052                             arg.vdev_id, restart, ret);
1053                 return ret;
1054         }
1055
1056         ar->num_started_vdevs++;
1057         ath10k_recalc_radar_detection(ar);
1058
1059         ret = ath10k_monitor_recalc(ar);
1060         if (ret) {
1061                 ath10k_warn(ar, "mac failed to recalc monitor for vdev %i restart %d: %d\n",
1062                             arg.vdev_id, restart, ret);
1063                 ret2 = ath10k_vdev_stop(arvif);
1064                 if (ret2)
1065                         ath10k_warn(ar, "mac failed to stop vdev %i restart %d: %d\n",
1066                                     arg.vdev_id, restart, ret2);
1067         }
1068
1069         return ret;
1070 }
1071
1072 static int ath10k_vdev_start(struct ath10k_vif *arvif)
1073 {
1074         return ath10k_vdev_start_restart(arvif, false);
1075 }
1076
1077 static int ath10k_vdev_restart(struct ath10k_vif *arvif)
1078 {
1079         return ath10k_vdev_start_restart(arvif, true);
1080 }
1081
1082 static int ath10k_mac_setup_bcn_p2p_ie(struct ath10k_vif *arvif,
1083                                        struct sk_buff *bcn)
1084 {
1085         struct ath10k *ar = arvif->ar;
1086         struct ieee80211_mgmt *mgmt;
1087         const u8 *p2p_ie;
1088         int ret;
1089
1090         if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1091                 return 0;
1092
1093         if (arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
1094                 return 0;
1095
1096         mgmt = (void *)bcn->data;
1097         p2p_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1098                                          mgmt->u.beacon.variable,
1099                                          bcn->len - (mgmt->u.beacon.variable -
1100                                                      bcn->data));
1101         if (!p2p_ie)
1102                 return -ENOENT;
1103
1104         ret = ath10k_wmi_p2p_go_bcn_ie(ar, arvif->vdev_id, p2p_ie);
1105         if (ret) {
1106                 ath10k_warn(ar, "failed to submit p2p go bcn ie for vdev %i: %d\n",
1107                             arvif->vdev_id, ret);
1108                 return ret;
1109         }
1110
1111         return 0;
1112 }
1113
1114 static int ath10k_mac_remove_vendor_ie(struct sk_buff *skb, unsigned int oui,
1115                                        u8 oui_type, size_t ie_offset)
1116 {
1117         size_t len;
1118         const u8 *next;
1119         const u8 *end;
1120         u8 *ie;
1121
1122         if (WARN_ON(skb->len < ie_offset))
1123                 return -EINVAL;
1124
1125         ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
1126                                            skb->data + ie_offset,
1127                                            skb->len - ie_offset);
1128         if (!ie)
1129                 return -ENOENT;
1130
1131         len = ie[1] + 2;
1132         end = skb->data + skb->len;
1133         next = ie + len;
1134
1135         if (WARN_ON(next > end))
1136                 return -EINVAL;
1137
1138         memmove(ie, next, end - next);
1139         skb_trim(skb, skb->len - len);
1140
1141         return 0;
1142 }
1143
1144 static int ath10k_mac_setup_bcn_tmpl(struct ath10k_vif *arvif)
1145 {
1146         struct ath10k *ar = arvif->ar;
1147         struct ieee80211_hw *hw = ar->hw;
1148         struct ieee80211_vif *vif = arvif->vif;
1149         struct ieee80211_mutable_offsets offs = {};
1150         struct sk_buff *bcn;
1151         int ret;
1152
1153         if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1154                 return 0;
1155
1156         if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
1157             arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
1158                 return 0;
1159
1160         bcn = ieee80211_beacon_get_template(hw, vif, &offs);
1161         if (!bcn) {
1162                 ath10k_warn(ar, "failed to get beacon template from mac80211\n");
1163                 return -EPERM;
1164         }
1165
1166         ret = ath10k_mac_setup_bcn_p2p_ie(arvif, bcn);
1167         if (ret) {
1168                 ath10k_warn(ar, "failed to setup p2p go bcn ie: %d\n", ret);
1169                 kfree_skb(bcn);
1170                 return ret;
1171         }
1172
1173         /* P2P IE is inserted by firmware automatically (as configured above)
1174          * so remove it from the base beacon template to avoid duplicate P2P
1175          * IEs in beacon frames.
1176          */
1177         ath10k_mac_remove_vendor_ie(bcn, WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1178                                     offsetof(struct ieee80211_mgmt,
1179                                              u.beacon.variable));
1180
1181         ret = ath10k_wmi_bcn_tmpl(ar, arvif->vdev_id, offs.tim_offset, bcn, 0,
1182                                   0, NULL, 0);
1183         kfree_skb(bcn);
1184
1185         if (ret) {
1186                 ath10k_warn(ar, "failed to submit beacon template command: %d\n",
1187                             ret);
1188                 return ret;
1189         }
1190
1191         return 0;
1192 }
1193
1194 static int ath10k_mac_setup_prb_tmpl(struct ath10k_vif *arvif)
1195 {
1196         struct ath10k *ar = arvif->ar;
1197         struct ieee80211_hw *hw = ar->hw;
1198         struct ieee80211_vif *vif = arvif->vif;
1199         struct sk_buff *prb;
1200         int ret;
1201
1202         if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1203                 return 0;
1204
1205         if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1206                 return 0;
1207
1208         prb = ieee80211_proberesp_get(hw, vif);
1209         if (!prb) {
1210                 ath10k_warn(ar, "failed to get probe resp template from mac80211\n");
1211                 return -EPERM;
1212         }
1213
1214         ret = ath10k_wmi_prb_tmpl(ar, arvif->vdev_id, prb);
1215         kfree_skb(prb);
1216
1217         if (ret) {
1218                 ath10k_warn(ar, "failed to submit probe resp template command: %d\n",
1219                             ret);
1220                 return ret;
1221         }
1222
1223         return 0;
1224 }
1225
1226 static void ath10k_control_beaconing(struct ath10k_vif *arvif,
1227                                      struct ieee80211_bss_conf *info)
1228 {
1229         struct ath10k *ar = arvif->ar;
1230         int ret = 0;
1231
1232         lockdep_assert_held(&arvif->ar->conf_mutex);
1233
1234         if (!info->enable_beacon) {
1235                 ath10k_vdev_stop(arvif);
1236
1237                 arvif->is_started = false;
1238                 arvif->is_up = false;
1239
1240                 spin_lock_bh(&arvif->ar->data_lock);
1241                 ath10k_mac_vif_beacon_free(arvif);
1242                 spin_unlock_bh(&arvif->ar->data_lock);
1243
1244                 return;
1245         }
1246
1247         arvif->tx_seq_no = 0x1000;
1248
1249         ret = ath10k_vdev_start(arvif);
1250         if (ret)
1251                 return;
1252
1253         arvif->aid = 0;
1254         ether_addr_copy(arvif->bssid, info->bssid);
1255
1256         ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
1257                                  arvif->bssid);
1258         if (ret) {
1259                 ath10k_warn(ar, "failed to bring up vdev %d: %i\n",
1260                             arvif->vdev_id, ret);
1261                 ath10k_vdev_stop(arvif);
1262                 return;
1263         }
1264
1265         arvif->is_started = true;
1266         arvif->is_up = true;
1267
1268         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
1269 }
1270
1271 static void ath10k_control_ibss(struct ath10k_vif *arvif,
1272                                 struct ieee80211_bss_conf *info,
1273                                 const u8 self_peer[ETH_ALEN])
1274 {
1275         struct ath10k *ar = arvif->ar;
1276         u32 vdev_param;
1277         int ret = 0;
1278
1279         lockdep_assert_held(&arvif->ar->conf_mutex);
1280
1281         if (!info->ibss_joined) {
1282                 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, self_peer);
1283                 if (ret)
1284                         ath10k_warn(ar, "failed to delete IBSS self peer %pM for vdev %d: %d\n",
1285                                     self_peer, arvif->vdev_id, ret);
1286
1287                 if (is_zero_ether_addr(arvif->bssid))
1288                         return;
1289
1290                 memset(arvif->bssid, 0, ETH_ALEN);
1291
1292                 return;
1293         }
1294
1295         ret = ath10k_peer_create(arvif->ar, arvif->vdev_id, self_peer,
1296                                  WMI_PEER_TYPE_DEFAULT);
1297         if (ret) {
1298                 ath10k_warn(ar, "failed to create IBSS self peer %pM for vdev %d: %d\n",
1299                             self_peer, arvif->vdev_id, ret);
1300                 return;
1301         }
1302
1303         vdev_param = arvif->ar->wmi.vdev_param->atim_window;
1304         ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id, vdev_param,
1305                                         ATH10K_DEFAULT_ATIM);
1306         if (ret)
1307                 ath10k_warn(ar, "failed to set IBSS ATIM for vdev %d: %d\n",
1308                             arvif->vdev_id, ret);
1309 }
1310
1311 static int ath10k_mac_vif_recalc_ps_wake_threshold(struct ath10k_vif *arvif)
1312 {
1313         struct ath10k *ar = arvif->ar;
1314         u32 param;
1315         u32 value;
1316         int ret;
1317
1318         lockdep_assert_held(&arvif->ar->conf_mutex);
1319
1320         if (arvif->u.sta.uapsd)
1321                 value = WMI_STA_PS_TX_WAKE_THRESHOLD_NEVER;
1322         else
1323                 value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
1324
1325         param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
1326         ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param, value);
1327         if (ret) {
1328                 ath10k_warn(ar, "failed to submit ps wake threshold %u on vdev %i: %d\n",
1329                             value, arvif->vdev_id, ret);
1330                 return ret;
1331         }
1332
1333         return 0;
1334 }
1335
1336 static int ath10k_mac_vif_recalc_ps_poll_count(struct ath10k_vif *arvif)
1337 {
1338         struct ath10k *ar = arvif->ar;
1339         u32 param;
1340         u32 value;
1341         int ret;
1342
1343         lockdep_assert_held(&arvif->ar->conf_mutex);
1344
1345         if (arvif->u.sta.uapsd)
1346                 value = WMI_STA_PS_PSPOLL_COUNT_UAPSD;
1347         else
1348                 value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
1349
1350         param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
1351         ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
1352                                           param, value);
1353         if (ret) {
1354                 ath10k_warn(ar, "failed to submit ps poll count %u on vdev %i: %d\n",
1355                             value, arvif->vdev_id, ret);
1356                 return ret;
1357         }
1358
1359         return 0;
1360 }
1361
1362 static int ath10k_mac_ps_vif_count(struct ath10k *ar)
1363 {
1364         struct ath10k_vif *arvif;
1365         int num = 0;
1366
1367         lockdep_assert_held(&ar->conf_mutex);
1368
1369         list_for_each_entry(arvif, &ar->arvifs, list)
1370                 if (arvif->ps)
1371                         num++;
1372
1373         return num;
1374 }
1375
1376 static int ath10k_mac_vif_setup_ps(struct ath10k_vif *arvif)
1377 {
1378         struct ath10k *ar = arvif->ar;
1379         struct ieee80211_vif *vif = arvif->vif;
1380         struct ieee80211_conf *conf = &ar->hw->conf;
1381         enum wmi_sta_powersave_param param;
1382         enum wmi_sta_ps_mode psmode;
1383         int ret;
1384         int ps_timeout;
1385         bool enable_ps;
1386
1387         lockdep_assert_held(&arvif->ar->conf_mutex);
1388
1389         if (arvif->vif->type != NL80211_IFTYPE_STATION)
1390                 return 0;
1391
1392         enable_ps = arvif->ps;
1393
1394         if (enable_ps && ath10k_mac_ps_vif_count(ar) > 1 &&
1395             !test_bit(ATH10K_FW_FEATURE_MULTI_VIF_PS_SUPPORT,
1396                       ar->fw_features)) {
1397                 ath10k_warn(ar, "refusing to enable ps on vdev %i: not supported by fw\n",
1398                             arvif->vdev_id);
1399                 enable_ps = false;
1400         }
1401
1402         if (enable_ps) {
1403                 psmode = WMI_STA_PS_MODE_ENABLED;
1404                 param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
1405
1406                 ps_timeout = conf->dynamic_ps_timeout;
1407                 if (ps_timeout == 0) {
1408                         /* Firmware doesn't like 0 */
1409                         ps_timeout = ieee80211_tu_to_usec(
1410                                 vif->bss_conf.beacon_int) / 1000;
1411                 }
1412
1413                 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
1414                                                   ps_timeout);
1415                 if (ret) {
1416                         ath10k_warn(ar, "failed to set inactivity time for vdev %d: %i\n",
1417                                     arvif->vdev_id, ret);
1418                         return ret;
1419                 }
1420         } else {
1421                 psmode = WMI_STA_PS_MODE_DISABLED;
1422         }
1423
1424         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d psmode %s\n",
1425                    arvif->vdev_id, psmode ? "enable" : "disable");
1426
1427         ret = ath10k_wmi_set_psmode(ar, arvif->vdev_id, psmode);
1428         if (ret) {
1429                 ath10k_warn(ar, "failed to set PS Mode %d for vdev %d: %d\n",
1430                             psmode, arvif->vdev_id, ret);
1431                 return ret;
1432         }
1433
1434         return 0;
1435 }
1436
1437 static int ath10k_mac_vif_disable_keepalive(struct ath10k_vif *arvif)
1438 {
1439         struct ath10k *ar = arvif->ar;
1440         struct wmi_sta_keepalive_arg arg = {};
1441         int ret;
1442
1443         lockdep_assert_held(&arvif->ar->conf_mutex);
1444
1445         if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
1446                 return 0;
1447
1448         if (!test_bit(WMI_SERVICE_STA_KEEP_ALIVE, ar->wmi.svc_map))
1449                 return 0;
1450
1451         /* Some firmware revisions have a bug and ignore the `enabled` field.
1452          * Instead use the interval to disable the keepalive.
1453          */
1454         arg.vdev_id = arvif->vdev_id;
1455         arg.enabled = 1;
1456         arg.method = WMI_STA_KEEPALIVE_METHOD_NULL_FRAME;
1457         arg.interval = WMI_STA_KEEPALIVE_INTERVAL_DISABLE;
1458
1459         ret = ath10k_wmi_sta_keepalive(ar, &arg);
1460         if (ret) {
1461                 ath10k_warn(ar, "failed to submit keepalive on vdev %i: %d\n",
1462                             arvif->vdev_id, ret);
1463                 return ret;
1464         }
1465
1466         return 0;
1467 }
1468
1469 static void ath10k_mac_vif_ap_csa_count_down(struct ath10k_vif *arvif)
1470 {
1471         struct ath10k *ar = arvif->ar;
1472         struct ieee80211_vif *vif = arvif->vif;
1473         int ret;
1474
1475         lockdep_assert_held(&arvif->ar->conf_mutex);
1476
1477         if (WARN_ON(!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)))
1478                 return;
1479
1480         if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1481                 return;
1482
1483         if (!vif->csa_active)
1484                 return;
1485
1486         if (!arvif->is_up)
1487                 return;
1488
1489         if (!ieee80211_csa_is_complete(vif)) {
1490                 ieee80211_csa_update_counter(vif);
1491
1492                 ret = ath10k_mac_setup_bcn_tmpl(arvif);
1493                 if (ret)
1494                         ath10k_warn(ar, "failed to update bcn tmpl during csa: %d\n",
1495                                     ret);
1496
1497                 ret = ath10k_mac_setup_prb_tmpl(arvif);
1498                 if (ret)
1499                         ath10k_warn(ar, "failed to update prb tmpl during csa: %d\n",
1500                                     ret);
1501         } else {
1502                 ieee80211_csa_finish(vif);
1503         }
1504 }
1505
1506 static void ath10k_mac_vif_ap_csa_work(struct work_struct *work)
1507 {
1508         struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
1509                                                 ap_csa_work);
1510         struct ath10k *ar = arvif->ar;
1511
1512         mutex_lock(&ar->conf_mutex);
1513         ath10k_mac_vif_ap_csa_count_down(arvif);
1514         mutex_unlock(&ar->conf_mutex);
1515 }
1516
1517 static void ath10k_mac_handle_beacon_iter(void *data, u8 *mac,
1518                                           struct ieee80211_vif *vif)
1519 {
1520         struct sk_buff *skb = data;
1521         struct ieee80211_mgmt *mgmt = (void *)skb->data;
1522         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1523
1524         if (vif->type != NL80211_IFTYPE_STATION)
1525                 return;
1526
1527         if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
1528                 return;
1529
1530         cancel_delayed_work(&arvif->connection_loss_work);
1531 }
1532
1533 void ath10k_mac_handle_beacon(struct ath10k *ar, struct sk_buff *skb)
1534 {
1535         ieee80211_iterate_active_interfaces_atomic(ar->hw,
1536                                                    IEEE80211_IFACE_ITER_NORMAL,
1537                                                    ath10k_mac_handle_beacon_iter,
1538                                                    skb);
1539 }
1540
1541 static void ath10k_mac_handle_beacon_miss_iter(void *data, u8 *mac,
1542                                                struct ieee80211_vif *vif)
1543 {
1544         u32 *vdev_id = data;
1545         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1546         struct ath10k *ar = arvif->ar;
1547         struct ieee80211_hw *hw = ar->hw;
1548
1549         if (arvif->vdev_id != *vdev_id)
1550                 return;
1551
1552         if (!arvif->is_up)
1553                 return;
1554
1555         ieee80211_beacon_loss(vif);
1556
1557         /* Firmware doesn't report beacon loss events repeatedly. If AP probe
1558          * (done by mac80211) succeeds but beacons do not resume then it
1559          * doesn't make sense to continue operation. Queue connection loss work
1560          * which can be cancelled when beacon is received.
1561          */
1562         ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
1563                                      ATH10K_CONNECTION_LOSS_HZ);
1564 }
1565
1566 void ath10k_mac_handle_beacon_miss(struct ath10k *ar, u32 vdev_id)
1567 {
1568         ieee80211_iterate_active_interfaces_atomic(ar->hw,
1569                                                    IEEE80211_IFACE_ITER_NORMAL,
1570                                                    ath10k_mac_handle_beacon_miss_iter,
1571                                                    &vdev_id);
1572 }
1573
1574 static void ath10k_mac_vif_sta_connection_loss_work(struct work_struct *work)
1575 {
1576         struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
1577                                                 connection_loss_work.work);
1578         struct ieee80211_vif *vif = arvif->vif;
1579
1580         if (!arvif->is_up)
1581                 return;
1582
1583         ieee80211_connection_loss(vif);
1584 }
1585
1586 /**********************/
1587 /* Station management */
1588 /**********************/
1589
1590 static u32 ath10k_peer_assoc_h_listen_intval(struct ath10k *ar,
1591                                              struct ieee80211_vif *vif)
1592 {
1593         /* Some firmware revisions have unstable STA powersave when listen
1594          * interval is set too high (e.g. 5). The symptoms are firmware doesn't
1595          * generate NullFunc frames properly even if buffered frames have been
1596          * indicated in Beacon TIM. Firmware would seldom wake up to pull
1597          * buffered frames. Often pinging the device from AP would simply fail.
1598          *
1599          * As a workaround set it to 1.
1600          */
1601         if (vif->type == NL80211_IFTYPE_STATION)
1602                 return 1;
1603
1604         return ar->hw->conf.listen_interval;
1605 }
1606
1607 static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
1608                                       struct ieee80211_vif *vif,
1609                                       struct ieee80211_sta *sta,
1610                                       struct wmi_peer_assoc_complete_arg *arg)
1611 {
1612         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1613
1614         lockdep_assert_held(&ar->conf_mutex);
1615
1616         ether_addr_copy(arg->addr, sta->addr);
1617         arg->vdev_id = arvif->vdev_id;
1618         arg->peer_aid = sta->aid;
1619         arg->peer_flags |= WMI_PEER_AUTH;
1620         arg->peer_listen_intval = ath10k_peer_assoc_h_listen_intval(ar, vif);
1621         arg->peer_num_spatial_streams = 1;
1622         arg->peer_caps = vif->bss_conf.assoc_capability;
1623 }
1624
1625 static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
1626                                        struct ieee80211_vif *vif,
1627                                        struct wmi_peer_assoc_complete_arg *arg)
1628 {
1629         struct ieee80211_bss_conf *info = &vif->bss_conf;
1630         struct cfg80211_bss *bss;
1631         const u8 *rsnie = NULL;
1632         const u8 *wpaie = NULL;
1633
1634         lockdep_assert_held(&ar->conf_mutex);
1635
1636         bss = cfg80211_get_bss(ar->hw->wiphy, ar->hw->conf.chandef.chan,
1637                                info->bssid, NULL, 0, IEEE80211_BSS_TYPE_ANY,
1638                                IEEE80211_PRIVACY_ANY);
1639         if (bss) {
1640                 const struct cfg80211_bss_ies *ies;
1641
1642                 rcu_read_lock();
1643                 rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
1644
1645                 ies = rcu_dereference(bss->ies);
1646
1647                 wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
1648                                                 WLAN_OUI_TYPE_MICROSOFT_WPA,
1649                                                 ies->data,
1650                                                 ies->len);
1651                 rcu_read_unlock();
1652                 cfg80211_put_bss(ar->hw->wiphy, bss);
1653         }
1654
1655         /* FIXME: base on RSN IE/WPA IE is a correct idea? */
1656         if (rsnie || wpaie) {
1657                 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
1658                 arg->peer_flags |= WMI_PEER_NEED_PTK_4_WAY;
1659         }
1660
1661         if (wpaie) {
1662                 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: wpa ie found\n", __func__);
1663                 arg->peer_flags |= WMI_PEER_NEED_GTK_2_WAY;
1664         }
1665 }
1666
1667 static void ath10k_peer_assoc_h_rates(struct ath10k *ar,
1668                                       struct ieee80211_sta *sta,
1669                                       struct wmi_peer_assoc_complete_arg *arg)
1670 {
1671         struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
1672         const struct ieee80211_supported_band *sband;
1673         const struct ieee80211_rate *rates;
1674         u32 ratemask;
1675         int i;
1676
1677         lockdep_assert_held(&ar->conf_mutex);
1678
1679         sband = ar->hw->wiphy->bands[ar->hw->conf.chandef.chan->band];
1680         ratemask = sta->supp_rates[ar->hw->conf.chandef.chan->band];
1681         rates = sband->bitrates;
1682
1683         rateset->num_rates = 0;
1684
1685         for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
1686                 if (!(ratemask & 1))
1687                         continue;
1688
1689                 rateset->rates[rateset->num_rates] = rates->hw_value;
1690                 rateset->num_rates++;
1691         }
1692 }
1693
1694 static void ath10k_peer_assoc_h_ht(struct ath10k *ar,
1695                                    struct ieee80211_sta *sta,
1696                                    struct wmi_peer_assoc_complete_arg *arg)
1697 {
1698         const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1699         int i, n;
1700         u32 stbc;
1701
1702         lockdep_assert_held(&ar->conf_mutex);
1703
1704         if (!ht_cap->ht_supported)
1705                 return;
1706
1707         arg->peer_flags |= WMI_PEER_HT;
1708         arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1709                                     ht_cap->ampdu_factor)) - 1;
1710
1711         arg->peer_mpdu_density =
1712                 ath10k_parse_mpdudensity(ht_cap->ampdu_density);
1713
1714         arg->peer_ht_caps = ht_cap->cap;
1715         arg->peer_rate_caps |= WMI_RC_HT_FLAG;
1716
1717         if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
1718                 arg->peer_flags |= WMI_PEER_LDPC;
1719
1720         if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
1721                 arg->peer_flags |= WMI_PEER_40MHZ;
1722                 arg->peer_rate_caps |= WMI_RC_CW40_FLAG;
1723         }
1724
1725         if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
1726                 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
1727
1728         if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
1729                 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
1730
1731         if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
1732                 arg->peer_rate_caps |= WMI_RC_TX_STBC_FLAG;
1733                 arg->peer_flags |= WMI_PEER_STBC;
1734         }
1735
1736         if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
1737                 stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
1738                 stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
1739                 stbc = stbc << WMI_RC_RX_STBC_FLAG_S;
1740                 arg->peer_rate_caps |= stbc;
1741                 arg->peer_flags |= WMI_PEER_STBC;
1742         }
1743
1744         if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
1745                 arg->peer_rate_caps |= WMI_RC_TS_FLAG;
1746         else if (ht_cap->mcs.rx_mask[1])
1747                 arg->peer_rate_caps |= WMI_RC_DS_FLAG;
1748
1749         for (i = 0, n = 0; i < IEEE80211_HT_MCS_MASK_LEN*8; i++)
1750                 if (ht_cap->mcs.rx_mask[i/8] & (1 << i%8))
1751                         arg->peer_ht_rates.rates[n++] = i;
1752
1753         /*
1754          * This is a workaround for HT-enabled STAs which break the spec
1755          * and have no HT capabilities RX mask (no HT RX MCS map).
1756          *
1757          * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
1758          * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
1759          *
1760          * Firmware asserts if such situation occurs.
1761          */
1762         if (n == 0) {
1763                 arg->peer_ht_rates.num_rates = 8;
1764                 for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
1765                         arg->peer_ht_rates.rates[i] = i;
1766         } else {
1767                 arg->peer_ht_rates.num_rates = n;
1768                 arg->peer_num_spatial_streams = sta->rx_nss;
1769         }
1770
1771         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
1772                    arg->addr,
1773                    arg->peer_ht_rates.num_rates,
1774                    arg->peer_num_spatial_streams);
1775 }
1776
1777 static int ath10k_peer_assoc_qos_ap(struct ath10k *ar,
1778                                     struct ath10k_vif *arvif,
1779                                     struct ieee80211_sta *sta)
1780 {
1781         u32 uapsd = 0;
1782         u32 max_sp = 0;
1783         int ret = 0;
1784
1785         lockdep_assert_held(&ar->conf_mutex);
1786
1787         if (sta->wme && sta->uapsd_queues) {
1788                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
1789                            sta->uapsd_queues, sta->max_sp);
1790
1791                 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1792                         uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
1793                                  WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
1794                 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1795                         uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
1796                                  WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
1797                 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1798                         uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
1799                                  WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
1800                 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1801                         uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
1802                                  WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
1803
1804                 if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
1805                         max_sp = sta->max_sp;
1806
1807                 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1808                                                  sta->addr,
1809                                                  WMI_AP_PS_PEER_PARAM_UAPSD,
1810                                                  uapsd);
1811                 if (ret) {
1812                         ath10k_warn(ar, "failed to set ap ps peer param uapsd for vdev %i: %d\n",
1813                                     arvif->vdev_id, ret);
1814                         return ret;
1815                 }
1816
1817                 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1818                                                  sta->addr,
1819                                                  WMI_AP_PS_PEER_PARAM_MAX_SP,
1820                                                  max_sp);
1821                 if (ret) {
1822                         ath10k_warn(ar, "failed to set ap ps peer param max sp for vdev %i: %d\n",
1823                                     arvif->vdev_id, ret);
1824                         return ret;
1825                 }
1826
1827                 /* TODO setup this based on STA listen interval and
1828                    beacon interval. Currently we don't know
1829                    sta->listen_interval - mac80211 patch required.
1830                    Currently use 10 seconds */
1831                 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id, sta->addr,
1832                                                  WMI_AP_PS_PEER_PARAM_AGEOUT_TIME,
1833                                                  10);
1834                 if (ret) {
1835                         ath10k_warn(ar, "failed to set ap ps peer param ageout time for vdev %i: %d\n",
1836                                     arvif->vdev_id, ret);
1837                         return ret;
1838                 }
1839         }
1840
1841         return 0;
1842 }
1843
1844 static void ath10k_peer_assoc_h_vht(struct ath10k *ar,
1845                                     struct ieee80211_sta *sta,
1846                                     struct wmi_peer_assoc_complete_arg *arg)
1847 {
1848         const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
1849         u8 ampdu_factor;
1850
1851         if (!vht_cap->vht_supported)
1852                 return;
1853
1854         arg->peer_flags |= WMI_PEER_VHT;
1855
1856         if (ar->hw->conf.chandef.chan->band == IEEE80211_BAND_2GHZ)
1857                 arg->peer_flags |= WMI_PEER_VHT_2G;
1858
1859         arg->peer_vht_caps = vht_cap->cap;
1860
1861         ampdu_factor = (vht_cap->cap &
1862                         IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
1863                        IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
1864
1865         /* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
1866          * zero in VHT IE. Using it would result in degraded throughput.
1867          * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
1868          * it if VHT max_mpdu is smaller. */
1869         arg->peer_max_mpdu = max(arg->peer_max_mpdu,
1870                                  (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1871                                         ampdu_factor)) - 1);
1872
1873         if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1874                 arg->peer_flags |= WMI_PEER_80MHZ;
1875
1876         arg->peer_vht_rates.rx_max_rate =
1877                 __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
1878         arg->peer_vht_rates.rx_mcs_set =
1879                 __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
1880         arg->peer_vht_rates.tx_max_rate =
1881                 __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
1882         arg->peer_vht_rates.tx_mcs_set =
1883                 __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
1884
1885         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
1886                    sta->addr, arg->peer_max_mpdu, arg->peer_flags);
1887 }
1888
1889 static void ath10k_peer_assoc_h_qos(struct ath10k *ar,
1890                                     struct ieee80211_vif *vif,
1891                                     struct ieee80211_sta *sta,
1892                                     struct wmi_peer_assoc_complete_arg *arg)
1893 {
1894         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1895
1896         switch (arvif->vdev_type) {
1897         case WMI_VDEV_TYPE_AP:
1898                 if (sta->wme)
1899                         arg->peer_flags |= WMI_PEER_QOS;
1900
1901                 if (sta->wme && sta->uapsd_queues) {
1902                         arg->peer_flags |= WMI_PEER_APSD;
1903                         arg->peer_rate_caps |= WMI_RC_UAPSD_FLAG;
1904                 }
1905                 break;
1906         case WMI_VDEV_TYPE_STA:
1907                 if (vif->bss_conf.qos)
1908                         arg->peer_flags |= WMI_PEER_QOS;
1909                 break;
1910         case WMI_VDEV_TYPE_IBSS:
1911                 if (sta->wme)
1912                         arg->peer_flags |= WMI_PEER_QOS;
1913                 break;
1914         default:
1915                 break;
1916         }
1917
1918         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM qos %d\n",
1919                    sta->addr, !!(arg->peer_flags & WMI_PEER_QOS));
1920 }
1921
1922 static bool ath10k_mac_sta_has_11g_rates(struct ieee80211_sta *sta)
1923 {
1924         /* First 4 rates in ath10k_rates are CCK (11b) rates. */
1925         return sta->supp_rates[IEEE80211_BAND_2GHZ] >> 4;
1926 }
1927
1928 static void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
1929                                         struct ieee80211_vif *vif,
1930                                         struct ieee80211_sta *sta,
1931                                         struct wmi_peer_assoc_complete_arg *arg)
1932 {
1933         enum wmi_phy_mode phymode = MODE_UNKNOWN;
1934
1935         switch (ar->hw->conf.chandef.chan->band) {
1936         case IEEE80211_BAND_2GHZ:
1937                 if (sta->vht_cap.vht_supported) {
1938                         if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1939                                 phymode = MODE_11AC_VHT40;
1940                         else
1941                                 phymode = MODE_11AC_VHT20;
1942                 } else if (sta->ht_cap.ht_supported) {
1943                         if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1944                                 phymode = MODE_11NG_HT40;
1945                         else
1946                                 phymode = MODE_11NG_HT20;
1947                 } else if (ath10k_mac_sta_has_11g_rates(sta)) {
1948                         phymode = MODE_11G;
1949                 } else {
1950                         phymode = MODE_11B;
1951                 }
1952
1953                 break;
1954         case IEEE80211_BAND_5GHZ:
1955                 /*
1956                  * Check VHT first.
1957                  */
1958                 if (sta->vht_cap.vht_supported) {
1959                         if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1960                                 phymode = MODE_11AC_VHT80;
1961                         else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1962                                 phymode = MODE_11AC_VHT40;
1963                         else if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
1964                                 phymode = MODE_11AC_VHT20;
1965                 } else if (sta->ht_cap.ht_supported) {
1966                         if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1967                                 phymode = MODE_11NA_HT40;
1968                         else
1969                                 phymode = MODE_11NA_HT20;
1970                 } else {
1971                         phymode = MODE_11A;
1972                 }
1973
1974                 break;
1975         default:
1976                 break;
1977         }
1978
1979         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM phymode %s\n",
1980                    sta->addr, ath10k_wmi_phymode_str(phymode));
1981
1982         arg->peer_phymode = phymode;
1983         WARN_ON(phymode == MODE_UNKNOWN);
1984 }
1985
1986 static int ath10k_peer_assoc_prepare(struct ath10k *ar,
1987                                      struct ieee80211_vif *vif,
1988                                      struct ieee80211_sta *sta,
1989                                      struct wmi_peer_assoc_complete_arg *arg)
1990 {
1991         lockdep_assert_held(&ar->conf_mutex);
1992
1993         memset(arg, 0, sizeof(*arg));
1994
1995         ath10k_peer_assoc_h_basic(ar, vif, sta, arg);
1996         ath10k_peer_assoc_h_crypto(ar, vif, arg);
1997         ath10k_peer_assoc_h_rates(ar, sta, arg);
1998         ath10k_peer_assoc_h_ht(ar, sta, arg);
1999         ath10k_peer_assoc_h_vht(ar, sta, arg);
2000         ath10k_peer_assoc_h_qos(ar, vif, sta, arg);
2001         ath10k_peer_assoc_h_phymode(ar, vif, sta, arg);
2002
2003         return 0;
2004 }
2005
2006 static const u32 ath10k_smps_map[] = {
2007         [WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
2008         [WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
2009         [WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
2010         [WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
2011 };
2012
2013 static int ath10k_setup_peer_smps(struct ath10k *ar, struct ath10k_vif *arvif,
2014                                   const u8 *addr,
2015                                   const struct ieee80211_sta_ht_cap *ht_cap)
2016 {
2017         int smps;
2018
2019         if (!ht_cap->ht_supported)
2020                 return 0;
2021
2022         smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
2023         smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
2024
2025         if (smps >= ARRAY_SIZE(ath10k_smps_map))
2026                 return -EINVAL;
2027
2028         return ath10k_wmi_peer_set_param(ar, arvif->vdev_id, addr,
2029                                          WMI_PEER_SMPS_STATE,
2030                                          ath10k_smps_map[smps]);
2031 }
2032
2033 static int ath10k_mac_vif_recalc_txbf(struct ath10k *ar,
2034                                       struct ieee80211_vif *vif,
2035                                       struct ieee80211_sta_vht_cap vht_cap)
2036 {
2037         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2038         int ret;
2039         u32 param;
2040         u32 value;
2041
2042         if (!(ar->vht_cap_info &
2043               (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
2044                IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
2045                IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
2046                IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)))
2047                 return 0;
2048
2049         param = ar->wmi.vdev_param->txbf;
2050         value = 0;
2051
2052         if (WARN_ON(param == WMI_VDEV_PARAM_UNSUPPORTED))
2053                 return 0;
2054
2055         /* The following logic is correct. If a remote STA advertises support
2056          * for being a beamformer then we should enable us being a beamformee.
2057          */
2058
2059         if (ar->vht_cap_info &
2060             (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
2061              IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)) {
2062                 if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)
2063                         value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
2064
2065                 if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)
2066                         value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
2067         }
2068
2069         if (ar->vht_cap_info &
2070             (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
2071              IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)) {
2072                 if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)
2073                         value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
2074
2075                 if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)
2076                         value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
2077         }
2078
2079         if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFEE)
2080                 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
2081
2082         if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFER)
2083                 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
2084
2085         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param, value);
2086         if (ret) {
2087                 ath10k_warn(ar, "failed to submit vdev param txbf 0x%x: %d\n",
2088                             value, ret);
2089                 return ret;
2090         }
2091
2092         return 0;
2093 }
2094
2095 /* can be called only in mac80211 callbacks due to `key_count` usage */
2096 static void ath10k_bss_assoc(struct ieee80211_hw *hw,
2097                              struct ieee80211_vif *vif,
2098                              struct ieee80211_bss_conf *bss_conf)
2099 {
2100         struct ath10k *ar = hw->priv;
2101         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2102         struct ieee80211_sta_ht_cap ht_cap;
2103         struct ieee80211_sta_vht_cap vht_cap;
2104         struct wmi_peer_assoc_complete_arg peer_arg;
2105         struct ieee80211_sta *ap_sta;
2106         int ret;
2107
2108         lockdep_assert_held(&ar->conf_mutex);
2109
2110         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
2111                    arvif->vdev_id, arvif->bssid, arvif->aid);
2112
2113         rcu_read_lock();
2114
2115         ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
2116         if (!ap_sta) {
2117                 ath10k_warn(ar, "failed to find station entry for bss %pM vdev %i\n",
2118                             bss_conf->bssid, arvif->vdev_id);
2119                 rcu_read_unlock();
2120                 return;
2121         }
2122
2123         /* ap_sta must be accessed only within rcu section which must be left
2124          * before calling ath10k_setup_peer_smps() which might sleep. */
2125         ht_cap = ap_sta->ht_cap;
2126         vht_cap = ap_sta->vht_cap;
2127
2128         ret = ath10k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg);
2129         if (ret) {
2130                 ath10k_warn(ar, "failed to prepare peer assoc for %pM vdev %i: %d\n",
2131                             bss_conf->bssid, arvif->vdev_id, ret);
2132                 rcu_read_unlock();
2133                 return;
2134         }
2135
2136         rcu_read_unlock();
2137
2138         ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
2139         if (ret) {
2140                 ath10k_warn(ar, "failed to run peer assoc for %pM vdev %i: %d\n",
2141                             bss_conf->bssid, arvif->vdev_id, ret);
2142                 return;
2143         }
2144
2145         ret = ath10k_setup_peer_smps(ar, arvif, bss_conf->bssid, &ht_cap);
2146         if (ret) {
2147                 ath10k_warn(ar, "failed to setup peer SMPS for vdev %i: %d\n",
2148                             arvif->vdev_id, ret);
2149                 return;
2150         }
2151
2152         ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
2153         if (ret) {
2154                 ath10k_warn(ar, "failed to recalc txbf for vdev %i on bss %pM: %d\n",
2155                             arvif->vdev_id, bss_conf->bssid, ret);
2156                 return;
2157         }
2158
2159         ath10k_dbg(ar, ATH10K_DBG_MAC,
2160                    "mac vdev %d up (associated) bssid %pM aid %d\n",
2161                    arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
2162
2163         WARN_ON(arvif->is_up);
2164
2165         arvif->aid = bss_conf->aid;
2166         ether_addr_copy(arvif->bssid, bss_conf->bssid);
2167
2168         ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
2169         if (ret) {
2170                 ath10k_warn(ar, "failed to set vdev %d up: %d\n",
2171                             arvif->vdev_id, ret);
2172                 return;
2173         }
2174
2175         arvif->is_up = true;
2176
2177         /* Workaround: Some firmware revisions (tested with qca6174
2178          * WLAN.RM.2.0-00073) have buggy powersave state machine and must be
2179          * poked with peer param command.
2180          */
2181         ret = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, arvif->bssid,
2182                                         WMI_PEER_DUMMY_VAR, 1);
2183         if (ret) {
2184                 ath10k_warn(ar, "failed to poke peer %pM param for ps workaround on vdev %i: %d\n",
2185                             arvif->bssid, arvif->vdev_id, ret);
2186                 return;
2187         }
2188 }
2189
2190 static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
2191                                 struct ieee80211_vif *vif)
2192 {
2193         struct ath10k *ar = hw->priv;
2194         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2195         struct ieee80211_sta_vht_cap vht_cap = {};
2196         int ret;
2197
2198         lockdep_assert_held(&ar->conf_mutex);
2199
2200         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
2201                    arvif->vdev_id, arvif->bssid);
2202
2203         ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
2204         if (ret)
2205                 ath10k_warn(ar, "faield to down vdev %i: %d\n",
2206                             arvif->vdev_id, ret);
2207
2208         arvif->def_wep_key_idx = -1;
2209
2210         ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
2211         if (ret) {
2212                 ath10k_warn(ar, "failed to recalc txbf for vdev %i: %d\n",
2213                             arvif->vdev_id, ret);
2214                 return;
2215         }
2216
2217         arvif->is_up = false;
2218
2219         cancel_delayed_work_sync(&arvif->connection_loss_work);
2220 }
2221
2222 static int ath10k_station_assoc(struct ath10k *ar,
2223                                 struct ieee80211_vif *vif,
2224                                 struct ieee80211_sta *sta,
2225                                 bool reassoc)
2226 {
2227         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2228         struct wmi_peer_assoc_complete_arg peer_arg;
2229         int ret = 0;
2230
2231         lockdep_assert_held(&ar->conf_mutex);
2232
2233         ret = ath10k_peer_assoc_prepare(ar, vif, sta, &peer_arg);
2234         if (ret) {
2235                 ath10k_warn(ar, "failed to prepare WMI peer assoc for %pM vdev %i: %i\n",
2236                             sta->addr, arvif->vdev_id, ret);
2237                 return ret;
2238         }
2239
2240         peer_arg.peer_reassoc = reassoc;
2241         ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
2242         if (ret) {
2243                 ath10k_warn(ar, "failed to run peer assoc for STA %pM vdev %i: %d\n",
2244                             sta->addr, arvif->vdev_id, ret);
2245                 return ret;
2246         }
2247
2248         /* Re-assoc is run only to update supported rates for given station. It
2249          * doesn't make much sense to reconfigure the peer completely.
2250          */
2251         if (!reassoc) {
2252                 ret = ath10k_setup_peer_smps(ar, arvif, sta->addr,
2253                                              &sta->ht_cap);
2254                 if (ret) {
2255                         ath10k_warn(ar, "failed to setup peer SMPS for vdev %d: %d\n",
2256                                     arvif->vdev_id, ret);
2257                         return ret;
2258                 }
2259
2260                 ret = ath10k_peer_assoc_qos_ap(ar, arvif, sta);
2261                 if (ret) {
2262                         ath10k_warn(ar, "failed to set qos params for STA %pM for vdev %i: %d\n",
2263                                     sta->addr, arvif->vdev_id, ret);
2264                         return ret;
2265                 }
2266
2267                 if (!sta->wme) {
2268                         arvif->num_legacy_stations++;
2269                         ret  = ath10k_recalc_rtscts_prot(arvif);
2270                         if (ret) {
2271                                 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
2272                                             arvif->vdev_id, ret);
2273                                 return ret;
2274                         }
2275                 }
2276
2277                 /* Plumb cached keys only for static WEP */
2278                 if (arvif->def_wep_key_idx != -1) {
2279                         ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
2280                         if (ret) {
2281                                 ath10k_warn(ar, "failed to install peer wep keys for vdev %i: %d\n",
2282                                             arvif->vdev_id, ret);
2283                                 return ret;
2284                         }
2285                 }
2286         }
2287
2288         return ret;
2289 }
2290
2291 static int ath10k_station_disassoc(struct ath10k *ar,
2292                                    struct ieee80211_vif *vif,
2293                                    struct ieee80211_sta *sta)
2294 {
2295         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2296         int ret = 0;
2297
2298         lockdep_assert_held(&ar->conf_mutex);
2299
2300         if (!sta->wme) {
2301                 arvif->num_legacy_stations--;
2302                 ret = ath10k_recalc_rtscts_prot(arvif);
2303                 if (ret) {
2304                         ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
2305                                     arvif->vdev_id, ret);
2306                         return ret;
2307                 }
2308         }
2309
2310         ret = ath10k_clear_peer_keys(arvif, sta->addr);
2311         if (ret) {
2312                 ath10k_warn(ar, "failed to clear all peer wep keys for vdev %i: %d\n",
2313                             arvif->vdev_id, ret);
2314                 return ret;
2315         }
2316
2317         return ret;
2318 }
2319
2320 /**************/
2321 /* Regulatory */
2322 /**************/
2323
2324 static int ath10k_update_channel_list(struct ath10k *ar)
2325 {
2326         struct ieee80211_hw *hw = ar->hw;
2327         struct ieee80211_supported_band **bands;
2328         enum ieee80211_band band;
2329         struct ieee80211_channel *channel;
2330         struct wmi_scan_chan_list_arg arg = {0};
2331         struct wmi_channel_arg *ch;
2332         bool passive;
2333         int len;
2334         int ret;
2335         int i;
2336
2337         lockdep_assert_held(&ar->conf_mutex);
2338
2339         bands = hw->wiphy->bands;
2340         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2341                 if (!bands[band])
2342                         continue;
2343
2344                 for (i = 0; i < bands[band]->n_channels; i++) {
2345                         if (bands[band]->channels[i].flags &
2346                             IEEE80211_CHAN_DISABLED)
2347                                 continue;
2348
2349                         arg.n_channels++;
2350                 }
2351         }
2352
2353         len = sizeof(struct wmi_channel_arg) * arg.n_channels;
2354         arg.channels = kzalloc(len, GFP_KERNEL);
2355         if (!arg.channels)
2356                 return -ENOMEM;
2357
2358         ch = arg.channels;
2359         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2360                 if (!bands[band])
2361                         continue;
2362
2363                 for (i = 0; i < bands[band]->n_channels; i++) {
2364                         channel = &bands[band]->channels[i];
2365
2366                         if (channel->flags & IEEE80211_CHAN_DISABLED)
2367                                 continue;
2368
2369                         ch->allow_ht   = true;
2370
2371                         /* FIXME: when should we really allow VHT? */
2372                         ch->allow_vht = true;
2373
2374                         ch->allow_ibss =
2375                                 !(channel->flags & IEEE80211_CHAN_NO_IR);
2376
2377                         ch->ht40plus =
2378                                 !(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
2379
2380                         ch->chan_radar =
2381                                 !!(channel->flags & IEEE80211_CHAN_RADAR);
2382
2383                         passive = channel->flags & IEEE80211_CHAN_NO_IR;
2384                         ch->passive = passive;
2385
2386                         ch->freq = channel->center_freq;
2387                         ch->band_center_freq1 = channel->center_freq;
2388                         ch->min_power = 0;
2389                         ch->max_power = channel->max_power * 2;
2390                         ch->max_reg_power = channel->max_reg_power * 2;
2391                         ch->max_antenna_gain = channel->max_antenna_gain * 2;
2392                         ch->reg_class_id = 0; /* FIXME */
2393
2394                         /* FIXME: why use only legacy modes, why not any
2395                          * HT/VHT modes? Would that even make any
2396                          * difference? */
2397                         if (channel->band == IEEE80211_BAND_2GHZ)
2398                                 ch->mode = MODE_11G;
2399                         else
2400                                 ch->mode = MODE_11A;
2401
2402                         if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
2403                                 continue;
2404
2405                         ath10k_dbg(ar, ATH10K_DBG_WMI,
2406                                    "mac channel [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
2407                                     ch - arg.channels, arg.n_channels,
2408                                    ch->freq, ch->max_power, ch->max_reg_power,
2409                                    ch->max_antenna_gain, ch->mode);
2410
2411                         ch++;
2412                 }
2413         }
2414
2415         ret = ath10k_wmi_scan_chan_list(ar, &arg);
2416         kfree(arg.channels);
2417
2418         return ret;
2419 }
2420
2421 static enum wmi_dfs_region
2422 ath10k_mac_get_dfs_region(enum nl80211_dfs_regions dfs_region)
2423 {
2424         switch (dfs_region) {
2425         case NL80211_DFS_UNSET:
2426                 return WMI_UNINIT_DFS_DOMAIN;
2427         case NL80211_DFS_FCC:
2428                 return WMI_FCC_DFS_DOMAIN;
2429         case NL80211_DFS_ETSI:
2430                 return WMI_ETSI_DFS_DOMAIN;
2431         case NL80211_DFS_JP:
2432                 return WMI_MKK4_DFS_DOMAIN;
2433         }
2434         return WMI_UNINIT_DFS_DOMAIN;
2435 }
2436
2437 static void ath10k_regd_update(struct ath10k *ar)
2438 {
2439         struct reg_dmn_pair_mapping *regpair;
2440         int ret;
2441         enum wmi_dfs_region wmi_dfs_reg;
2442         enum nl80211_dfs_regions nl_dfs_reg;
2443
2444         lockdep_assert_held(&ar->conf_mutex);
2445
2446         ret = ath10k_update_channel_list(ar);
2447         if (ret)
2448                 ath10k_warn(ar, "failed to update channel list: %d\n", ret);
2449
2450         regpair = ar->ath_common.regulatory.regpair;
2451
2452         if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
2453                 nl_dfs_reg = ar->dfs_detector->region;
2454                 wmi_dfs_reg = ath10k_mac_get_dfs_region(nl_dfs_reg);
2455         } else {
2456                 wmi_dfs_reg = WMI_UNINIT_DFS_DOMAIN;
2457         }
2458
2459         /* Target allows setting up per-band regdomain but ath_common provides
2460          * a combined one only */
2461         ret = ath10k_wmi_pdev_set_regdomain(ar,
2462                                             regpair->reg_domain,
2463                                             regpair->reg_domain, /* 2ghz */
2464                                             regpair->reg_domain, /* 5ghz */
2465                                             regpair->reg_2ghz_ctl,
2466                                             regpair->reg_5ghz_ctl,
2467                                             wmi_dfs_reg);
2468         if (ret)
2469                 ath10k_warn(ar, "failed to set pdev regdomain: %d\n", ret);
2470 }
2471
2472 static void ath10k_reg_notifier(struct wiphy *wiphy,
2473                                 struct regulatory_request *request)
2474 {
2475         struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
2476         struct ath10k *ar = hw->priv;
2477         bool result;
2478
2479         ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
2480
2481         if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
2482                 ath10k_dbg(ar, ATH10K_DBG_REGULATORY, "dfs region 0x%x\n",
2483                            request->dfs_region);
2484                 result = ar->dfs_detector->set_dfs_domain(ar->dfs_detector,
2485                                                           request->dfs_region);
2486                 if (!result)
2487                         ath10k_warn(ar, "DFS region 0x%X not supported, will trigger radar for every pulse\n",
2488                                     request->dfs_region);
2489         }
2490
2491         mutex_lock(&ar->conf_mutex);
2492         if (ar->state == ATH10K_STATE_ON)
2493                 ath10k_regd_update(ar);
2494         mutex_unlock(&ar->conf_mutex);
2495 }
2496
2497 /***************/
2498 /* TX handlers */
2499 /***************/
2500
2501 static u8 ath10k_tx_h_get_tid(struct ieee80211_hdr *hdr)
2502 {
2503         if (ieee80211_is_mgmt(hdr->frame_control))
2504                 return HTT_DATA_TX_EXT_TID_MGMT;
2505
2506         if (!ieee80211_is_data_qos(hdr->frame_control))
2507                 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
2508
2509         if (!is_unicast_ether_addr(ieee80211_get_DA(hdr)))
2510                 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
2511
2512         return ieee80211_get_qos_ctl(hdr)[0] & IEEE80211_QOS_CTL_TID_MASK;
2513 }
2514
2515 static u8 ath10k_tx_h_get_vdev_id(struct ath10k *ar, struct ieee80211_vif *vif)
2516 {
2517         if (vif)
2518                 return ath10k_vif_to_arvif(vif)->vdev_id;
2519
2520         if (ar->monitor_started)
2521                 return ar->monitor_vdev_id;
2522
2523         ath10k_warn(ar, "failed to resolve vdev id\n");
2524         return 0;
2525 }
2526
2527 static enum ath10k_hw_txrx_mode
2528 ath10k_tx_h_get_txmode(struct ath10k *ar, struct ieee80211_vif *vif,
2529                        struct sk_buff *skb)
2530 {
2531         const struct ieee80211_hdr *hdr = (void *)skb->data;
2532         __le16 fc = hdr->frame_control;
2533
2534         if (!vif || vif->type == NL80211_IFTYPE_MONITOR)
2535                 return ATH10K_HW_TXRX_RAW;
2536
2537         if (ieee80211_is_mgmt(fc))
2538                 return ATH10K_HW_TXRX_MGMT;
2539
2540         /* Workaround:
2541          *
2542          * NullFunc frames are mostly used to ping if a client or AP are still
2543          * reachable and responsive. This implies tx status reports must be
2544          * accurate - otherwise either mac80211 or userspace (e.g. hostapd) can
2545          * come to a conclusion that the other end disappeared and tear down
2546          * BSS connection or it can never disconnect from BSS/client (which is
2547          * the case).
2548          *
2549          * Firmware with HTT older than 3.0 delivers incorrect tx status for
2550          * NullFunc frames to driver. However there's a HTT Mgmt Tx command
2551          * which seems to deliver correct tx reports for NullFunc frames. The
2552          * downside of using it is it ignores client powersave state so it can
2553          * end up disconnecting sleeping clients in AP mode. It should fix STA
2554          * mode though because AP don't sleep.
2555          */
2556         if (ar->htt.target_version_major < 3 &&
2557             (ieee80211_is_nullfunc(fc) || ieee80211_is_qos_nullfunc(fc)) &&
2558             !test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX, ar->fw_features))
2559                 return ATH10K_HW_TXRX_MGMT;
2560
2561         return ATH10K_HW_TXRX_NATIVE_WIFI;
2562 }
2563
2564 /* HTT Tx uses Native Wifi tx mode which expects 802.11 frames without QoS
2565  * Control in the header.
2566  */
2567 static void ath10k_tx_h_nwifi(struct ieee80211_hw *hw, struct sk_buff *skb)
2568 {
2569         struct ieee80211_hdr *hdr = (void *)skb->data;
2570         struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
2571         u8 *qos_ctl;
2572
2573         if (!ieee80211_is_data_qos(hdr->frame_control))
2574                 return;
2575
2576         qos_ctl = ieee80211_get_qos_ctl(hdr);
2577         memmove(skb->data + IEEE80211_QOS_CTL_LEN,
2578                 skb->data, (void *)qos_ctl - (void *)skb->data);
2579         skb_pull(skb, IEEE80211_QOS_CTL_LEN);
2580
2581         /* Some firmware revisions don't handle sending QoS NullFunc well.
2582          * These frames are mainly used for CQM purposes so it doesn't really
2583          * matter whether QoS NullFunc or NullFunc are sent.
2584          */
2585         hdr = (void *)skb->data;
2586         if (ieee80211_is_qos_nullfunc(hdr->frame_control))
2587                 cb->htt.tid = HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
2588
2589         hdr->frame_control &= ~__cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
2590 }
2591
2592 static void ath10k_tx_h_8023(struct sk_buff *skb)
2593 {
2594         struct ieee80211_hdr *hdr;
2595         struct rfc1042_hdr *rfc1042;
2596         struct ethhdr *eth;
2597         size_t hdrlen;
2598         u8 da[ETH_ALEN];
2599         u8 sa[ETH_ALEN];
2600         __be16 type;
2601
2602         hdr = (void *)skb->data;
2603         hdrlen = ieee80211_hdrlen(hdr->frame_control);
2604         rfc1042 = (void *)skb->data + hdrlen;
2605
2606         ether_addr_copy(da, ieee80211_get_DA(hdr));
2607         ether_addr_copy(sa, ieee80211_get_SA(hdr));
2608         type = rfc1042->snap_type;
2609
2610         skb_pull(skb, hdrlen + sizeof(*rfc1042));
2611         skb_push(skb, sizeof(*eth));
2612
2613         eth = (void *)skb->data;
2614         ether_addr_copy(eth->h_dest, da);
2615         ether_addr_copy(eth->h_source, sa);
2616         eth->h_proto = type;
2617 }
2618
2619 static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar,
2620                                        struct ieee80211_vif *vif,
2621                                        struct sk_buff *skb)
2622 {
2623         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2624         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2625
2626         /* This is case only for P2P_GO */
2627         if (arvif->vdev_type != WMI_VDEV_TYPE_AP ||
2628             arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
2629                 return;
2630
2631         if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
2632                 spin_lock_bh(&ar->data_lock);
2633                 if (arvif->u.ap.noa_data)
2634                         if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
2635                                               GFP_ATOMIC))
2636                                 memcpy(skb_put(skb, arvif->u.ap.noa_len),
2637                                        arvif->u.ap.noa_data,
2638                                        arvif->u.ap.noa_len);
2639                 spin_unlock_bh(&ar->data_lock);
2640         }
2641 }
2642
2643 static bool ath10k_mac_need_offchan_tx_work(struct ath10k *ar)
2644 {
2645         /* FIXME: Not really sure since when the behaviour changed. At some
2646          * point new firmware stopped requiring creation of peer entries for
2647          * offchannel tx (and actually creating them causes issues with wmi-htc
2648          * tx credit replenishment and reliability). Assuming it's at least 3.4
2649          * because that's when the `freq` was introduced to TX_FRM HTT command.
2650          */
2651         return !(ar->htt.target_version_major >= 3 &&
2652                  ar->htt.target_version_minor >= 4);
2653 }
2654
2655 static int ath10k_mac_tx_wmi_mgmt(struct ath10k *ar, struct sk_buff *skb)
2656 {
2657         struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
2658         int ret = 0;
2659
2660         spin_lock_bh(&ar->data_lock);
2661
2662         if (skb_queue_len(q) == ATH10K_MAX_NUM_MGMT_PENDING) {
2663                 ath10k_warn(ar, "wmi mgmt tx queue is full\n");
2664                 ret = -ENOSPC;
2665                 goto unlock;
2666         }
2667
2668         __skb_queue_tail(q, skb);
2669         ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
2670
2671 unlock:
2672         spin_unlock_bh(&ar->data_lock);
2673
2674         return ret;
2675 }
2676
2677 static void ath10k_mac_tx(struct ath10k *ar, struct sk_buff *skb)
2678 {
2679         struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
2680         struct ath10k_htt *htt = &ar->htt;
2681         int ret = 0;
2682
2683         switch (cb->txmode) {
2684         case ATH10K_HW_TXRX_RAW:
2685         case ATH10K_HW_TXRX_NATIVE_WIFI:
2686         case ATH10K_HW_TXRX_ETHERNET:
2687                 ret = ath10k_htt_tx(htt, skb);
2688                 break;
2689         case ATH10K_HW_TXRX_MGMT:
2690                 if (test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
2691                              ar->fw_features))
2692                         ret = ath10k_mac_tx_wmi_mgmt(ar, skb);
2693                 else if (ar->htt.target_version_major >= 3)
2694                         ret = ath10k_htt_tx(htt, skb);
2695                 else
2696                         ret = ath10k_htt_mgmt_tx(htt, skb);
2697                 break;
2698         }
2699
2700         if (ret) {
2701                 ath10k_warn(ar, "failed to transmit packet, dropping: %d\n",
2702                             ret);
2703                 ieee80211_free_txskb(ar->hw, skb);
2704         }
2705 }
2706
2707 void ath10k_offchan_tx_purge(struct ath10k *ar)
2708 {
2709         struct sk_buff *skb;
2710
2711         for (;;) {
2712                 skb = skb_dequeue(&ar->offchan_tx_queue);
2713                 if (!skb)
2714                         break;
2715
2716                 ieee80211_free_txskb(ar->hw, skb);
2717         }
2718 }
2719
2720 void ath10k_offchan_tx_work(struct work_struct *work)
2721 {
2722         struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
2723         struct ath10k_peer *peer;
2724         struct ieee80211_hdr *hdr;
2725         struct sk_buff *skb;
2726         const u8 *peer_addr;
2727         int vdev_id;
2728         int ret;
2729
2730         /* FW requirement: We must create a peer before FW will send out
2731          * an offchannel frame. Otherwise the frame will be stuck and
2732          * never transmitted. We delete the peer upon tx completion.
2733          * It is unlikely that a peer for offchannel tx will already be
2734          * present. However it may be in some rare cases so account for that.
2735          * Otherwise we might remove a legitimate peer and break stuff. */
2736
2737         for (;;) {
2738                 skb = skb_dequeue(&ar->offchan_tx_queue);
2739                 if (!skb)
2740                         break;
2741
2742                 mutex_lock(&ar->conf_mutex);
2743
2744                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac offchannel skb %p\n",
2745                            skb);
2746
2747                 hdr = (struct ieee80211_hdr *)skb->data;
2748                 peer_addr = ieee80211_get_DA(hdr);
2749                 vdev_id = ATH10K_SKB_CB(skb)->vdev_id;
2750
2751                 spin_lock_bh(&ar->data_lock);
2752                 peer = ath10k_peer_find(ar, vdev_id, peer_addr);
2753                 spin_unlock_bh(&ar->data_lock);
2754
2755                 if (peer)
2756                         /* FIXME: should this use ath10k_warn()? */
2757                         ath10k_dbg(ar, ATH10K_DBG_MAC, "peer %pM on vdev %d already present\n",
2758                                    peer_addr, vdev_id);
2759
2760                 if (!peer) {
2761                         ret = ath10k_peer_create(ar, vdev_id, peer_addr,
2762                                                  WMI_PEER_TYPE_DEFAULT);
2763                         if (ret)
2764                                 ath10k_warn(ar, "failed to create peer %pM on vdev %d: %d\n",
2765                                             peer_addr, vdev_id, ret);
2766                 }
2767
2768                 spin_lock_bh(&ar->data_lock);
2769                 reinit_completion(&ar->offchan_tx_completed);
2770                 ar->offchan_tx_skb = skb;
2771                 spin_unlock_bh(&ar->data_lock);
2772
2773                 ath10k_mac_tx(ar, skb);
2774
2775                 ret = wait_for_completion_timeout(&ar->offchan_tx_completed,
2776                                                   3 * HZ);
2777                 if (ret == 0)
2778                         ath10k_warn(ar, "timed out waiting for offchannel skb %p\n",
2779                                     skb);
2780
2781                 if (!peer) {
2782                         ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
2783                         if (ret)
2784                                 ath10k_warn(ar, "failed to delete peer %pM on vdev %d: %d\n",
2785                                             peer_addr, vdev_id, ret);
2786                 }
2787
2788                 mutex_unlock(&ar->conf_mutex);
2789         }
2790 }
2791
2792 void ath10k_mgmt_over_wmi_tx_purge(struct ath10k *ar)
2793 {
2794         struct sk_buff *skb;
2795
2796         for (;;) {
2797                 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
2798                 if (!skb)
2799                         break;
2800
2801                 ieee80211_free_txskb(ar->hw, skb);
2802         }
2803 }
2804
2805 void ath10k_mgmt_over_wmi_tx_work(struct work_struct *work)
2806 {
2807         struct ath10k *ar = container_of(work, struct ath10k, wmi_mgmt_tx_work);
2808         struct sk_buff *skb;
2809         int ret;
2810
2811         for (;;) {
2812                 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
2813                 if (!skb)
2814                         break;
2815
2816                 ret = ath10k_wmi_mgmt_tx(ar, skb);
2817                 if (ret) {
2818                         ath10k_warn(ar, "failed to transmit management frame via WMI: %d\n",
2819                                     ret);
2820                         ieee80211_free_txskb(ar->hw, skb);
2821                 }
2822         }
2823 }
2824
2825 /************/
2826 /* Scanning */
2827 /************/
2828
2829 void __ath10k_scan_finish(struct ath10k *ar)
2830 {
2831         lockdep_assert_held(&ar->data_lock);
2832
2833         switch (ar->scan.state) {
2834         case ATH10K_SCAN_IDLE:
2835                 break;
2836         case ATH10K_SCAN_RUNNING:
2837                 if (ar->scan.is_roc)
2838                         ieee80211_remain_on_channel_expired(ar->hw);
2839                 /* fall through */
2840         case ATH10K_SCAN_ABORTING:
2841                 if (!ar->scan.is_roc)
2842                         ieee80211_scan_completed(ar->hw,
2843                                                  (ar->scan.state ==
2844                                                   ATH10K_SCAN_ABORTING));
2845                 /* fall through */
2846         case ATH10K_SCAN_STARTING:
2847                 ar->scan.state = ATH10K_SCAN_IDLE;
2848                 ar->scan_channel = NULL;
2849                 ath10k_offchan_tx_purge(ar);
2850                 cancel_delayed_work(&ar->scan.timeout);
2851                 complete_all(&ar->scan.completed);
2852                 break;
2853         }
2854 }
2855
2856 void ath10k_scan_finish(struct ath10k *ar)
2857 {
2858         spin_lock_bh(&ar->data_lock);
2859         __ath10k_scan_finish(ar);
2860         spin_unlock_bh(&ar->data_lock);
2861 }
2862
2863 static int ath10k_scan_stop(struct ath10k *ar)
2864 {
2865         struct wmi_stop_scan_arg arg = {
2866                 .req_id = 1, /* FIXME */
2867                 .req_type = WMI_SCAN_STOP_ONE,
2868                 .u.scan_id = ATH10K_SCAN_ID,
2869         };
2870         int ret;
2871
2872         lockdep_assert_held(&ar->conf_mutex);
2873
2874         ret = ath10k_wmi_stop_scan(ar, &arg);
2875         if (ret) {
2876                 ath10k_warn(ar, "failed to stop wmi scan: %d\n", ret);
2877                 goto out;
2878         }
2879
2880         ret = wait_for_completion_timeout(&ar->scan.completed, 3*HZ);
2881         if (ret == 0) {
2882                 ath10k_warn(ar, "failed to receive scan abortion completion: timed out\n");
2883                 ret = -ETIMEDOUT;
2884         } else if (ret > 0) {
2885                 ret = 0;
2886         }
2887
2888 out:
2889         /* Scan state should be updated upon scan completion but in case
2890          * firmware fails to deliver the event (for whatever reason) it is
2891          * desired to clean up scan state anyway. Firmware may have just
2892          * dropped the scan completion event delivery due to transport pipe
2893          * being overflown with data and/or it can recover on its own before
2894          * next scan request is submitted.
2895          */
2896         spin_lock_bh(&ar->data_lock);
2897         if (ar->scan.state != ATH10K_SCAN_IDLE)
2898                 __ath10k_scan_finish(ar);
2899         spin_unlock_bh(&ar->data_lock);
2900
2901         return ret;
2902 }
2903
2904 static void ath10k_scan_abort(struct ath10k *ar)
2905 {
2906         int ret;
2907
2908         lockdep_assert_held(&ar->conf_mutex);
2909
2910         spin_lock_bh(&ar->data_lock);
2911
2912         switch (ar->scan.state) {
2913         case ATH10K_SCAN_IDLE:
2914                 /* This can happen if timeout worker kicked in and called
2915                  * abortion while scan completion was being processed.
2916                  */
2917                 break;
2918         case ATH10K_SCAN_STARTING:
2919         case ATH10K_SCAN_ABORTING:
2920                 ath10k_warn(ar, "refusing scan abortion due to invalid scan state: %s (%d)\n",
2921                             ath10k_scan_state_str(ar->scan.state),
2922                             ar->scan.state);
2923                 break;
2924         case ATH10K_SCAN_RUNNING:
2925                 ar->scan.state = ATH10K_SCAN_ABORTING;
2926                 spin_unlock_bh(&ar->data_lock);
2927
2928                 ret = ath10k_scan_stop(ar);
2929                 if (ret)
2930                         ath10k_warn(ar, "failed to abort scan: %d\n", ret);
2931
2932                 spin_lock_bh(&ar->data_lock);
2933                 break;
2934         }
2935
2936         spin_unlock_bh(&ar->data_lock);
2937 }
2938
2939 void ath10k_scan_timeout_work(struct work_struct *work)
2940 {
2941         struct ath10k *ar = container_of(work, struct ath10k,
2942                                          scan.timeout.work);
2943
2944         mutex_lock(&ar->conf_mutex);
2945         ath10k_scan_abort(ar);
2946         mutex_unlock(&ar->conf_mutex);
2947 }
2948
2949 static int ath10k_start_scan(struct ath10k *ar,
2950                              const struct wmi_start_scan_arg *arg)
2951 {
2952         int ret;
2953
2954         lockdep_assert_held(&ar->conf_mutex);
2955
2956         ret = ath10k_wmi_start_scan(ar, arg);
2957         if (ret)
2958                 return ret;
2959
2960         ret = wait_for_completion_timeout(&ar->scan.started, 1*HZ);
2961         if (ret == 0) {
2962                 ret = ath10k_scan_stop(ar);
2963                 if (ret)
2964                         ath10k_warn(ar, "failed to stop scan: %d\n", ret);
2965
2966                 return -ETIMEDOUT;
2967         }
2968
2969         /* If we failed to start the scan, return error code at
2970          * this point.  This is probably due to some issue in the
2971          * firmware, but no need to wedge the driver due to that...
2972          */
2973         spin_lock_bh(&ar->data_lock);
2974         if (ar->scan.state == ATH10K_SCAN_IDLE) {
2975                 spin_unlock_bh(&ar->data_lock);
2976                 return -EINVAL;
2977         }
2978         spin_unlock_bh(&ar->data_lock);
2979
2980         /* Add a 200ms margin to account for event/command processing */
2981         ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
2982                                      msecs_to_jiffies(arg->max_scan_time+200));
2983         return 0;
2984 }
2985
2986 /**********************/
2987 /* mac80211 callbacks */
2988 /**********************/
2989
2990 static void ath10k_tx(struct ieee80211_hw *hw,
2991                       struct ieee80211_tx_control *control,
2992                       struct sk_buff *skb)
2993 {
2994         struct ath10k *ar = hw->priv;
2995         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2996         struct ieee80211_vif *vif = info->control.vif;
2997         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2998         __le16 fc = hdr->frame_control;
2999
3000         /* We should disable CCK RATE due to P2P */
3001         if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
3002                 ath10k_dbg(ar, ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
3003
3004         ATH10K_SKB_CB(skb)->htt.is_offchan = false;
3005         ATH10K_SKB_CB(skb)->htt.freq = 0;
3006         ATH10K_SKB_CB(skb)->htt.tid = ath10k_tx_h_get_tid(hdr);
3007         ATH10K_SKB_CB(skb)->vdev_id = ath10k_tx_h_get_vdev_id(ar, vif);
3008         ATH10K_SKB_CB(skb)->txmode = ath10k_tx_h_get_txmode(ar, vif, skb);
3009         ATH10K_SKB_CB(skb)->is_protected = ieee80211_has_protected(fc);
3010
3011         switch (ATH10K_SKB_CB(skb)->txmode) {
3012         case ATH10K_HW_TXRX_MGMT:
3013         case ATH10K_HW_TXRX_NATIVE_WIFI:
3014                 ath10k_tx_h_nwifi(hw, skb);
3015                 ath10k_tx_h_add_p2p_noa_ie(ar, vif, skb);
3016                 ath10k_tx_h_seq_no(vif, skb);
3017                 break;
3018         case ATH10K_HW_TXRX_ETHERNET:
3019                 ath10k_tx_h_8023(skb);
3020                 break;
3021         case ATH10K_HW_TXRX_RAW:
3022                 /* FIXME: Packet injection isn't implemented. It should be
3023                  * doable with firmware 10.2 on qca988x.
3024                  */
3025                 WARN_ON_ONCE(1);
3026                 ieee80211_free_txskb(hw, skb);
3027                 return;
3028         }
3029
3030         if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
3031                 spin_lock_bh(&ar->data_lock);
3032                 ATH10K_SKB_CB(skb)->htt.freq = ar->scan.roc_freq;
3033                 ATH10K_SKB_CB(skb)->vdev_id = ar->scan.vdev_id;
3034                 spin_unlock_bh(&ar->data_lock);
3035
3036                 if (ath10k_mac_need_offchan_tx_work(ar)) {
3037                         ATH10K_SKB_CB(skb)->htt.freq = 0;
3038                         ATH10K_SKB_CB(skb)->htt.is_offchan = true;
3039
3040                         ath10k_dbg(ar, ATH10K_DBG_MAC, "queued offchannel skb %p\n",
3041                                    skb);
3042
3043                         skb_queue_tail(&ar->offchan_tx_queue, skb);
3044                         ieee80211_queue_work(hw, &ar->offchan_tx_work);
3045                         return;
3046                 }
3047         }
3048
3049         ath10k_mac_tx(ar, skb);
3050 }
3051
3052 /* Must not be called with conf_mutex held as workers can use that also. */
3053 void ath10k_drain_tx(struct ath10k *ar)
3054 {
3055         /* make sure rcu-protected mac80211 tx path itself is drained */
3056         synchronize_net();
3057
3058         ath10k_offchan_tx_purge(ar);
3059         ath10k_mgmt_over_wmi_tx_purge(ar);
3060
3061         cancel_work_sync(&ar->offchan_tx_work);
3062         cancel_work_sync(&ar->wmi_mgmt_tx_work);
3063 }
3064
3065 void ath10k_halt(struct ath10k *ar)
3066 {
3067         struct ath10k_vif *arvif;
3068
3069         lockdep_assert_held(&ar->conf_mutex);
3070
3071         clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
3072         ar->filter_flags = 0;
3073         ar->monitor = false;
3074
3075         if (ar->monitor_started)
3076                 ath10k_monitor_stop(ar);
3077
3078         ar->monitor_started = false;
3079
3080         ath10k_scan_finish(ar);
3081         ath10k_peer_cleanup_all(ar);
3082         ath10k_core_stop(ar);
3083         ath10k_hif_power_down(ar);
3084
3085         spin_lock_bh(&ar->data_lock);
3086         list_for_each_entry(arvif, &ar->arvifs, list)
3087                 ath10k_mac_vif_beacon_cleanup(arvif);
3088         spin_unlock_bh(&ar->data_lock);
3089 }
3090
3091 static int ath10k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
3092 {
3093         struct ath10k *ar = hw->priv;
3094
3095         mutex_lock(&ar->conf_mutex);
3096
3097         if (ar->cfg_tx_chainmask) {
3098                 *tx_ant = ar->cfg_tx_chainmask;
3099                 *rx_ant = ar->cfg_rx_chainmask;
3100         } else {
3101                 *tx_ant = ar->supp_tx_chainmask;
3102                 *rx_ant = ar->supp_rx_chainmask;
3103         }
3104
3105         mutex_unlock(&ar->conf_mutex);
3106
3107         return 0;
3108 }
3109
3110 static void ath10k_check_chain_mask(struct ath10k *ar, u32 cm, const char *dbg)
3111 {
3112         /* It is not clear that allowing gaps in chainmask
3113          * is helpful.  Probably it will not do what user
3114          * is hoping for, so warn in that case.
3115          */
3116         if (cm == 15 || cm == 7 || cm == 3 || cm == 1 || cm == 0)
3117                 return;
3118
3119         ath10k_warn(ar, "mac %s antenna chainmask may be invalid: 0x%x.  Suggested values: 15, 7, 3, 1 or 0.\n",
3120                     dbg, cm);
3121 }
3122
3123 static int __ath10k_set_antenna(struct ath10k *ar, u32 tx_ant, u32 rx_ant)
3124 {
3125         int ret;
3126
3127         lockdep_assert_held(&ar->conf_mutex);
3128
3129         ath10k_check_chain_mask(ar, tx_ant, "tx");
3130         ath10k_check_chain_mask(ar, rx_ant, "rx");
3131
3132         ar->cfg_tx_chainmask = tx_ant;
3133         ar->cfg_rx_chainmask = rx_ant;
3134
3135         if ((ar->state != ATH10K_STATE_ON) &&
3136             (ar->state != ATH10K_STATE_RESTARTED))
3137                 return 0;
3138
3139         ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->tx_chain_mask,
3140                                         tx_ant);
3141         if (ret) {
3142                 ath10k_warn(ar, "failed to set tx-chainmask: %d, req 0x%x\n",
3143                             ret, tx_ant);
3144                 return ret;
3145         }
3146
3147         ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->rx_chain_mask,
3148                                         rx_ant);
3149         if (ret) {
3150                 ath10k_warn(ar, "failed to set rx-chainmask: %d, req 0x%x\n",
3151                             ret, rx_ant);
3152                 return ret;
3153         }
3154
3155         return 0;
3156 }
3157
3158 static int ath10k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
3159 {
3160         struct ath10k *ar = hw->priv;
3161         int ret;
3162
3163         mutex_lock(&ar->conf_mutex);
3164         ret = __ath10k_set_antenna(ar, tx_ant, rx_ant);
3165         mutex_unlock(&ar->conf_mutex);
3166         return ret;
3167 }
3168
3169 static int ath10k_start(struct ieee80211_hw *hw)
3170 {
3171         struct ath10k *ar = hw->priv;
3172         int ret = 0;
3173
3174         /*
3175          * This makes sense only when restarting hw. It is harmless to call
3176          * uncoditionally. This is necessary to make sure no HTT/WMI tx
3177          * commands will be submitted while restarting.
3178          */
3179         ath10k_drain_tx(ar);
3180
3181         mutex_lock(&ar->conf_mutex);
3182
3183         switch (ar->state) {
3184         case ATH10K_STATE_OFF:
3185                 ar->state = ATH10K_STATE_ON;
3186                 break;
3187         case ATH10K_STATE_RESTARTING:
3188                 ath10k_halt(ar);
3189                 ar->state = ATH10K_STATE_RESTARTED;
3190                 break;
3191         case ATH10K_STATE_ON:
3192         case ATH10K_STATE_RESTARTED:
3193         case ATH10K_STATE_WEDGED:
3194                 WARN_ON(1);
3195                 ret = -EINVAL;
3196                 goto err;
3197         case ATH10K_STATE_UTF:
3198                 ret = -EBUSY;
3199                 goto err;
3200         }
3201
3202         ret = ath10k_hif_power_up(ar);
3203         if (ret) {
3204                 ath10k_err(ar, "Could not init hif: %d\n", ret);
3205                 goto err_off;
3206         }
3207
3208         ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_NORMAL);
3209         if (ret) {
3210                 ath10k_err(ar, "Could not init core: %d\n", ret);
3211                 goto err_power_down;
3212         }
3213
3214         ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->pmf_qos, 1);
3215         if (ret) {
3216                 ath10k_warn(ar, "failed to enable PMF QOS: %d\n", ret);
3217                 goto err_core_stop;
3218         }
3219
3220         ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->dynamic_bw, 1);
3221         if (ret) {
3222                 ath10k_warn(ar, "failed to enable dynamic BW: %d\n", ret);
3223                 goto err_core_stop;
3224         }
3225
3226         if (ar->cfg_tx_chainmask)
3227                 __ath10k_set_antenna(ar, ar->cfg_tx_chainmask,
3228                                      ar->cfg_rx_chainmask);
3229
3230         /*
3231          * By default FW set ARP frames ac to voice (6). In that case ARP
3232          * exchange is not working properly for UAPSD enabled AP. ARP requests
3233          * which arrives with access category 0 are processed by network stack
3234          * and send back with access category 0, but FW changes access category
3235          * to 6. Set ARP frames access category to best effort (0) solves
3236          * this problem.
3237          */
3238
3239         ret = ath10k_wmi_pdev_set_param(ar,
3240                                         ar->wmi.pdev_param->arp_ac_override, 0);
3241         if (ret) {
3242                 ath10k_warn(ar, "failed to set arp ac override parameter: %d\n",
3243                             ret);
3244                 goto err_core_stop;
3245         }
3246
3247         ret = ath10k_wmi_pdev_set_param(ar,
3248                                         ar->wmi.pdev_param->ani_enable, 1);
3249         if (ret) {
3250                 ath10k_warn(ar, "failed to enable ani by default: %d\n",
3251                             ret);
3252                 goto err_core_stop;
3253         }
3254
3255         ar->ani_enabled = true;
3256
3257         ar->num_started_vdevs = 0;
3258         ath10k_regd_update(ar);
3259
3260         ath10k_spectral_start(ar);
3261         ath10k_thermal_set_throttling(ar);
3262
3263         mutex_unlock(&ar->conf_mutex);
3264         return 0;
3265
3266 err_core_stop:
3267         ath10k_core_stop(ar);
3268
3269 err_power_down:
3270         ath10k_hif_power_down(ar);
3271
3272 err_off:
3273         ar->state = ATH10K_STATE_OFF;
3274
3275 err:
3276         mutex_unlock(&ar->conf_mutex);
3277         return ret;
3278 }
3279
3280 static void ath10k_stop(struct ieee80211_hw *hw)
3281 {
3282         struct ath10k *ar = hw->priv;
3283
3284         ath10k_drain_tx(ar);
3285
3286         mutex_lock(&ar->conf_mutex);
3287         if (ar->state != ATH10K_STATE_OFF) {
3288                 ath10k_halt(ar);
3289                 ar->state = ATH10K_STATE_OFF;
3290         }
3291         mutex_unlock(&ar->conf_mutex);
3292
3293         cancel_delayed_work_sync(&ar->scan.timeout);
3294         cancel_work_sync(&ar->restart_work);
3295 }
3296
3297 static int ath10k_config_ps(struct ath10k *ar)
3298 {
3299         struct ath10k_vif *arvif;
3300         int ret = 0;
3301
3302         lockdep_assert_held(&ar->conf_mutex);
3303
3304         list_for_each_entry(arvif, &ar->arvifs, list) {
3305                 ret = ath10k_mac_vif_setup_ps(arvif);
3306                 if (ret) {
3307                         ath10k_warn(ar, "failed to setup powersave: %d\n", ret);
3308                         break;
3309                 }
3310         }
3311
3312         return ret;
3313 }
3314
3315 static const char *chandef_get_width(enum nl80211_chan_width width)
3316 {
3317         switch (width) {
3318         case NL80211_CHAN_WIDTH_20_NOHT:
3319                 return "20 (noht)";
3320         case NL80211_CHAN_WIDTH_20:
3321                 return "20";
3322         case NL80211_CHAN_WIDTH_40:
3323                 return "40";
3324         case NL80211_CHAN_WIDTH_80:
3325                 return "80";
3326         case NL80211_CHAN_WIDTH_80P80:
3327                 return "80+80";
3328         case NL80211_CHAN_WIDTH_160:
3329                 return "160";
3330         case NL80211_CHAN_WIDTH_5:
3331                 return "5";
3332         case NL80211_CHAN_WIDTH_10:
3333                 return "10";
3334         }
3335         return "?";
3336 }
3337
3338 static void ath10k_config_chan(struct ath10k *ar)
3339 {
3340         struct ath10k_vif *arvif;
3341         int ret;
3342
3343         lockdep_assert_held(&ar->conf_mutex);
3344
3345         ath10k_dbg(ar, ATH10K_DBG_MAC,
3346                    "mac config channel to %dMHz (cf1 %dMHz cf2 %dMHz width %s)\n",
3347                    ar->chandef.chan->center_freq,
3348                    ar->chandef.center_freq1,
3349                    ar->chandef.center_freq2,
3350                    chandef_get_width(ar->chandef.width));
3351
3352         /* First stop monitor interface. Some FW versions crash if there's a
3353          * lone monitor interface. */
3354         if (ar->monitor_started)
3355                 ath10k_monitor_stop(ar);
3356
3357         list_for_each_entry(arvif, &ar->arvifs, list) {
3358                 if (!arvif->is_started)
3359                         continue;
3360
3361                 if (!arvif->is_up)
3362                         continue;
3363
3364                 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
3365                         continue;
3366
3367                 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
3368                 if (ret) {
3369                         ath10k_warn(ar, "failed to down vdev %d: %d\n",
3370                                     arvif->vdev_id, ret);
3371                         continue;
3372                 }
3373         }
3374
3375         /* all vdevs are downed now - attempt to restart and re-up them */
3376
3377         list_for_each_entry(arvif, &ar->arvifs, list) {
3378                 if (!arvif->is_started)
3379                         continue;
3380
3381                 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
3382                         continue;
3383
3384                 ret = ath10k_mac_setup_bcn_tmpl(arvif);
3385                 if (ret)
3386                         ath10k_warn(ar, "failed to update bcn tmpl during csa: %d\n",
3387                                     ret);
3388
3389                 ret = ath10k_mac_setup_prb_tmpl(arvif);
3390                 if (ret)
3391                         ath10k_warn(ar, "failed to update prb tmpl during csa: %d\n",
3392                                     ret);
3393
3394                 ret = ath10k_vdev_restart(arvif);
3395                 if (ret) {
3396                         ath10k_warn(ar, "failed to restart vdev %d: %d\n",
3397                                     arvif->vdev_id, ret);
3398                         continue;
3399                 }
3400
3401                 if (!arvif->is_up)
3402                         continue;
3403
3404                 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
3405                                          arvif->bssid);
3406                 if (ret) {
3407                         ath10k_warn(ar, "failed to bring vdev up %d: %d\n",
3408                                     arvif->vdev_id, ret);
3409                         continue;
3410                 }
3411         }
3412
3413         ath10k_monitor_recalc(ar);
3414 }
3415
3416 static int ath10k_mac_txpower_setup(struct ath10k *ar, int txpower)
3417 {
3418         int ret;
3419         u32 param;
3420
3421         lockdep_assert_held(&ar->conf_mutex);
3422
3423         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac txpower %d\n", txpower);
3424
3425         param = ar->wmi.pdev_param->txpower_limit2g;
3426         ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
3427         if (ret) {
3428                 ath10k_warn(ar, "failed to set 2g txpower %d: %d\n",
3429                             txpower, ret);
3430                 return ret;
3431         }
3432
3433         param = ar->wmi.pdev_param->txpower_limit5g;
3434         ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
3435         if (ret) {
3436                 ath10k_warn(ar, "failed to set 5g txpower %d: %d\n",
3437                             txpower, ret);
3438                 return ret;
3439         }
3440
3441         return 0;
3442 }
3443
3444 static int ath10k_mac_txpower_recalc(struct ath10k *ar)
3445 {
3446         struct ath10k_vif *arvif;
3447         int ret, txpower = -1;
3448
3449         lockdep_assert_held(&ar->conf_mutex);
3450
3451         list_for_each_entry(arvif, &ar->arvifs, list) {
3452                 WARN_ON(arvif->txpower < 0);
3453
3454                 if (txpower == -1)
3455                         txpower = arvif->txpower;
3456                 else
3457                         txpower = min(txpower, arvif->txpower);
3458         }
3459
3460         if (WARN_ON(txpower == -1))
3461                 return -EINVAL;
3462
3463         ret = ath10k_mac_txpower_setup(ar, txpower);
3464         if (ret) {
3465                 ath10k_warn(ar, "failed to setup tx power %d: %d\n",
3466                             txpower, ret);
3467                 return ret;
3468         }
3469
3470         return 0;
3471 }
3472
3473 static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
3474 {
3475         struct ath10k *ar = hw->priv;
3476         struct ieee80211_conf *conf = &hw->conf;
3477         int ret = 0;
3478
3479         mutex_lock(&ar->conf_mutex);
3480
3481         if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
3482                 ath10k_dbg(ar, ATH10K_DBG_MAC,
3483                            "mac config channel %dMHz flags 0x%x radar %d\n",
3484                            conf->chandef.chan->center_freq,
3485                            conf->chandef.chan->flags,
3486                            conf->radar_enabled);
3487
3488                 spin_lock_bh(&ar->data_lock);
3489                 ar->rx_channel = conf->chandef.chan;
3490                 spin_unlock_bh(&ar->data_lock);
3491
3492                 ar->radar_enabled = conf->radar_enabled;
3493                 ath10k_recalc_radar_detection(ar);
3494
3495                 if (!cfg80211_chandef_identical(&ar->chandef, &conf->chandef)) {
3496                         ar->chandef = conf->chandef;
3497                         ath10k_config_chan(ar);
3498                 }
3499         }
3500
3501         if (changed & IEEE80211_CONF_CHANGE_PS)
3502                 ath10k_config_ps(ar);
3503
3504         if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
3505                 ar->monitor = conf->flags & IEEE80211_CONF_MONITOR;
3506                 ret = ath10k_monitor_recalc(ar);
3507                 if (ret)
3508                         ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
3509         }
3510
3511         mutex_unlock(&ar->conf_mutex);
3512         return ret;
3513 }
3514
3515 static u32 get_nss_from_chainmask(u16 chain_mask)
3516 {
3517         if ((chain_mask & 0x15) == 0x15)
3518                 return 4;
3519         else if ((chain_mask & 0x7) == 0x7)
3520                 return 3;
3521         else if ((chain_mask & 0x3) == 0x3)
3522                 return 2;
3523         return 1;
3524 }
3525
3526 /*
3527  * TODO:
3528  * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
3529  * because we will send mgmt frames without CCK. This requirement
3530  * for P2P_FIND/GO_NEG should be handled by checking CCK flag
3531  * in the TX packet.
3532  */
3533 static int ath10k_add_interface(struct ieee80211_hw *hw,
3534                                 struct ieee80211_vif *vif)
3535 {
3536         struct ath10k *ar = hw->priv;
3537         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3538         enum wmi_sta_powersave_param param;
3539         int ret = 0;
3540         u32 value;
3541         int bit;
3542         u32 vdev_param;
3543
3544         vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
3545
3546         mutex_lock(&ar->conf_mutex);
3547
3548         memset(arvif, 0, sizeof(*arvif));
3549
3550         arvif->ar = ar;
3551         arvif->vif = vif;
3552
3553         INIT_LIST_HEAD(&arvif->list);
3554         INIT_WORK(&arvif->ap_csa_work, ath10k_mac_vif_ap_csa_work);
3555         INIT_DELAYED_WORK(&arvif->connection_loss_work,
3556                           ath10k_mac_vif_sta_connection_loss_work);
3557
3558         if (ar->free_vdev_map == 0) {
3559                 ath10k_warn(ar, "Free vdev map is empty, no more interfaces allowed.\n");
3560                 ret = -EBUSY;
3561                 goto err;
3562         }
3563         bit = __ffs64(ar->free_vdev_map);
3564
3565         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac create vdev %i map %llx\n",
3566                    bit, ar->free_vdev_map);
3567
3568         arvif->vdev_id = bit;
3569         arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
3570
3571         switch (vif->type) {
3572         case NL80211_IFTYPE_P2P_DEVICE:
3573                 arvif->vdev_type = WMI_VDEV_TYPE_STA;
3574                 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
3575                 break;
3576         case NL80211_IFTYPE_UNSPECIFIED:
3577         case NL80211_IFTYPE_STATION:
3578                 arvif->vdev_type = WMI_VDEV_TYPE_STA;
3579                 if (vif->p2p)
3580                         arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
3581                 break;
3582         case NL80211_IFTYPE_ADHOC:
3583                 arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
3584                 break;
3585         case NL80211_IFTYPE_AP:
3586                 arvif->vdev_type = WMI_VDEV_TYPE_AP;
3587
3588                 if (vif->p2p)
3589                         arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
3590                 break;
3591         case NL80211_IFTYPE_MONITOR:
3592                 arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
3593                 break;
3594         default:
3595                 WARN_ON(1);
3596                 break;
3597         }
3598
3599         /* Some firmware revisions don't wait for beacon tx completion before
3600          * sending another SWBA event. This could lead to hardware using old
3601          * (freed) beacon data in some cases, e.g. tx credit starvation
3602          * combined with missed TBTT. This is very very rare.
3603          *
3604          * On non-IOMMU-enabled hosts this could be a possible security issue
3605          * because hw could beacon some random data on the air.  On
3606          * IOMMU-enabled hosts DMAR faults would occur in most cases and target
3607          * device would crash.
3608          *
3609          * Since there are no beacon tx completions (implicit nor explicit)
3610          * propagated to host the only workaround for this is to allocate a
3611          * DMA-coherent buffer for a lifetime of a vif and use it for all
3612          * beacon tx commands. Worst case for this approach is some beacons may
3613          * become corrupted, e.g. have garbled IEs or out-of-date TIM bitmap.
3614          */
3615         if (vif->type == NL80211_IFTYPE_ADHOC ||
3616             vif->type == NL80211_IFTYPE_AP) {
3617                 arvif->beacon_buf = dma_zalloc_coherent(ar->dev,
3618                                                         IEEE80211_MAX_FRAME_LEN,
3619                                                         &arvif->beacon_paddr,
3620                                                         GFP_ATOMIC);
3621                 if (!arvif->beacon_buf) {
3622                         ret = -ENOMEM;
3623                         ath10k_warn(ar, "failed to allocate beacon buffer: %d\n",
3624                                     ret);
3625                         goto err;
3626                 }
3627         }
3628
3629         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d bcnmode %s\n",
3630                    arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
3631                    arvif->beacon_buf ? "single-buf" : "per-skb");
3632
3633         ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
3634                                      arvif->vdev_subtype, vif->addr);
3635         if (ret) {
3636                 ath10k_warn(ar, "failed to create WMI vdev %i: %d\n",
3637                             arvif->vdev_id, ret);
3638                 goto err;
3639         }
3640
3641         ar->free_vdev_map &= ~(1LL << arvif->vdev_id);
3642         list_add(&arvif->list, &ar->arvifs);
3643
3644         /* It makes no sense to have firmware do keepalives. mac80211 already
3645          * takes care of this with idle connection polling.
3646          */
3647         ret = ath10k_mac_vif_disable_keepalive(arvif);
3648         if (ret) {
3649                 ath10k_warn(ar, "failed to disable keepalive on vdev %i: %d\n",
3650                             arvif->vdev_id, ret);
3651                 goto err_vdev_delete;
3652         }
3653
3654         arvif->def_wep_key_idx = -1;
3655
3656         vdev_param = ar->wmi.vdev_param->tx_encap_type;
3657         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3658                                         ATH10K_HW_TXRX_NATIVE_WIFI);
3659         /* 10.X firmware does not support this VDEV parameter. Do not warn */
3660         if (ret && ret != -EOPNOTSUPP) {
3661                 ath10k_warn(ar, "failed to set vdev %i TX encapsulation: %d\n",
3662                             arvif->vdev_id, ret);
3663                 goto err_vdev_delete;
3664         }
3665
3666         if (ar->cfg_tx_chainmask) {
3667                 u16 nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
3668
3669                 vdev_param = ar->wmi.vdev_param->nss;
3670                 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3671                                                 nss);
3672                 if (ret) {
3673                         ath10k_warn(ar, "failed to set vdev %i chainmask 0x%x, nss %i: %d\n",
3674                                     arvif->vdev_id, ar->cfg_tx_chainmask, nss,
3675                                     ret);
3676                         goto err_vdev_delete;
3677                 }
3678         }
3679
3680         if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3681                 ret = ath10k_peer_create(ar, arvif->vdev_id, vif->addr,
3682                                          WMI_PEER_TYPE_DEFAULT);
3683                 if (ret) {
3684                         ath10k_warn(ar, "failed to create vdev %i peer for AP: %d\n",
3685                                     arvif->vdev_id, ret);
3686                         goto err_vdev_delete;
3687                 }
3688
3689                 ret = ath10k_mac_set_kickout(arvif);
3690                 if (ret) {
3691                         ath10k_warn(ar, "failed to set vdev %i kickout parameters: %d\n",
3692                                     arvif->vdev_id, ret);
3693                         goto err_peer_delete;
3694                 }
3695         }
3696
3697         if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
3698                 param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
3699                 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
3700                 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3701                                                   param, value);
3702                 if (ret) {
3703                         ath10k_warn(ar, "failed to set vdev %i RX wake policy: %d\n",
3704                                     arvif->vdev_id, ret);
3705                         goto err_peer_delete;
3706                 }
3707
3708                 ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
3709                 if (ret) {
3710                         ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
3711                                     arvif->vdev_id, ret);
3712                         goto err_peer_delete;
3713                 }
3714
3715                 ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
3716                 if (ret) {
3717                         ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
3718                                     arvif->vdev_id, ret);
3719                         goto err_peer_delete;
3720                 }
3721         }
3722
3723         ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
3724         if (ret) {
3725                 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
3726                             arvif->vdev_id, ret);
3727                 goto err_peer_delete;
3728         }
3729
3730         ret = ath10k_mac_set_frag(arvif, ar->hw->wiphy->frag_threshold);
3731         if (ret) {
3732                 ath10k_warn(ar, "failed to set frag threshold for vdev %d: %d\n",
3733                             arvif->vdev_id, ret);
3734                 goto err_peer_delete;
3735         }
3736
3737         arvif->txpower = vif->bss_conf.txpower;
3738         ret = ath10k_mac_txpower_recalc(ar);
3739         if (ret) {
3740                 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
3741                 goto err_peer_delete;
3742         }
3743
3744         mutex_unlock(&ar->conf_mutex);
3745         return 0;
3746
3747 err_peer_delete:
3748         if (arvif->vdev_type == WMI_VDEV_TYPE_AP)
3749                 ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
3750
3751 err_vdev_delete:
3752         ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
3753         ar->free_vdev_map |= 1LL << arvif->vdev_id;
3754         list_del(&arvif->list);
3755
3756 err:
3757         if (arvif->beacon_buf) {
3758                 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
3759                                   arvif->beacon_buf, arvif->beacon_paddr);
3760                 arvif->beacon_buf = NULL;
3761         }
3762
3763         mutex_unlock(&ar->conf_mutex);
3764
3765         return ret;
3766 }
3767
3768 static void ath10k_remove_interface(struct ieee80211_hw *hw,
3769                                     struct ieee80211_vif *vif)
3770 {
3771         struct ath10k *ar = hw->priv;
3772         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3773         int ret;
3774
3775         cancel_work_sync(&arvif->ap_csa_work);
3776         cancel_delayed_work_sync(&arvif->connection_loss_work);
3777
3778         mutex_lock(&ar->conf_mutex);
3779
3780         spin_lock_bh(&ar->data_lock);
3781         ath10k_mac_vif_beacon_cleanup(arvif);
3782         spin_unlock_bh(&ar->data_lock);
3783
3784         ret = ath10k_spectral_vif_stop(arvif);
3785         if (ret)
3786                 ath10k_warn(ar, "failed to stop spectral for vdev %i: %d\n",
3787                             arvif->vdev_id, ret);
3788
3789         ar->free_vdev_map |= 1LL << arvif->vdev_id;
3790         list_del(&arvif->list);
3791
3792         if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3793                 ret = ath10k_wmi_peer_delete(arvif->ar, arvif->vdev_id,
3794                                              vif->addr);
3795                 if (ret)
3796                         ath10k_warn(ar, "failed to submit AP self-peer removal on vdev %i: %d\n",
3797                                     arvif->vdev_id, ret);
3798
3799                 kfree(arvif->u.ap.noa_data);
3800         }
3801
3802         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i delete (remove interface)\n",
3803                    arvif->vdev_id);
3804
3805         ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
3806         if (ret)
3807                 ath10k_warn(ar, "failed to delete WMI vdev %i: %d\n",
3808                             arvif->vdev_id, ret);
3809
3810         /* Some firmware revisions don't notify host about self-peer removal
3811          * until after associated vdev is deleted.
3812          */
3813         if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3814                 ret = ath10k_wait_for_peer_deleted(ar, arvif->vdev_id,
3815                                                    vif->addr);
3816                 if (ret)
3817                         ath10k_warn(ar, "failed to remove AP self-peer on vdev %i: %d\n",
3818                                     arvif->vdev_id, ret);
3819
3820                 spin_lock_bh(&ar->data_lock);
3821                 ar->num_peers--;
3822                 spin_unlock_bh(&ar->data_lock);
3823         }
3824
3825         ath10k_peer_cleanup(ar, arvif->vdev_id);
3826
3827         mutex_unlock(&ar->conf_mutex);
3828 }
3829
3830 /*
3831  * FIXME: Has to be verified.
3832  */
3833 #define SUPPORTED_FILTERS                       \
3834         (FIF_PROMISC_IN_BSS |                   \
3835         FIF_ALLMULTI |                          \
3836         FIF_CONTROL |                           \
3837         FIF_PSPOLL |                            \
3838         FIF_OTHER_BSS |                         \
3839         FIF_BCN_PRBRESP_PROMISC |               \
3840         FIF_PROBE_REQ |                         \
3841         FIF_FCSFAIL)
3842
3843 static void ath10k_configure_filter(struct ieee80211_hw *hw,
3844                                     unsigned int changed_flags,
3845                                     unsigned int *total_flags,
3846                                     u64 multicast)
3847 {
3848         struct ath10k *ar = hw->priv;
3849         int ret;
3850
3851         mutex_lock(&ar->conf_mutex);
3852
3853         changed_flags &= SUPPORTED_FILTERS;
3854         *total_flags &= SUPPORTED_FILTERS;
3855         ar->filter_flags = *total_flags;
3856
3857         ret = ath10k_monitor_recalc(ar);
3858         if (ret)
3859                 ath10k_warn(ar, "failed to recalc montior: %d\n", ret);
3860
3861         mutex_unlock(&ar->conf_mutex);
3862 }
3863
3864 static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
3865                                     struct ieee80211_vif *vif,
3866                                     struct ieee80211_bss_conf *info,
3867                                     u32 changed)
3868 {
3869         struct ath10k *ar = hw->priv;
3870         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3871         int ret = 0;
3872         u32 vdev_param, pdev_param, slottime, preamble;
3873
3874         mutex_lock(&ar->conf_mutex);
3875
3876         if (changed & BSS_CHANGED_IBSS)
3877                 ath10k_control_ibss(arvif, info, vif->addr);
3878
3879         if (changed & BSS_CHANGED_BEACON_INT) {
3880                 arvif->beacon_interval = info->beacon_int;
3881                 vdev_param = ar->wmi.vdev_param->beacon_interval;
3882                 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3883                                                 arvif->beacon_interval);
3884                 ath10k_dbg(ar, ATH10K_DBG_MAC,
3885                            "mac vdev %d beacon_interval %d\n",
3886                            arvif->vdev_id, arvif->beacon_interval);
3887
3888                 if (ret)
3889                         ath10k_warn(ar, "failed to set beacon interval for vdev %d: %i\n",
3890                                     arvif->vdev_id, ret);
3891         }
3892
3893         if (changed & BSS_CHANGED_BEACON) {
3894                 ath10k_dbg(ar, ATH10K_DBG_MAC,
3895                            "vdev %d set beacon tx mode to staggered\n",
3896                            arvif->vdev_id);
3897
3898                 pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
3899                 ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
3900                                                 WMI_BEACON_STAGGERED_MODE);
3901                 if (ret)
3902                         ath10k_warn(ar, "failed to set beacon mode for vdev %d: %i\n",
3903                                     arvif->vdev_id, ret);
3904
3905                 ret = ath10k_mac_setup_bcn_tmpl(arvif);
3906                 if (ret)
3907                         ath10k_warn(ar, "failed to update beacon template: %d\n",
3908                                     ret);
3909         }
3910
3911         if (changed & BSS_CHANGED_AP_PROBE_RESP) {
3912                 ret = ath10k_mac_setup_prb_tmpl(arvif);
3913                 if (ret)
3914                         ath10k_warn(ar, "failed to setup probe resp template on vdev %i: %d\n",
3915                                     arvif->vdev_id, ret);
3916         }
3917
3918         if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
3919                 arvif->dtim_period = info->dtim_period;
3920
3921                 ath10k_dbg(ar, ATH10K_DBG_MAC,
3922                            "mac vdev %d dtim_period %d\n",
3923                            arvif->vdev_id, arvif->dtim_period);
3924
3925                 vdev_param = ar->wmi.vdev_param->dtim_period;
3926                 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3927                                                 arvif->dtim_period);
3928                 if (ret)
3929                         ath10k_warn(ar, "failed to set dtim period for vdev %d: %i\n",
3930                                     arvif->vdev_id, ret);
3931         }
3932
3933         if (changed & BSS_CHANGED_SSID &&
3934             vif->type == NL80211_IFTYPE_AP) {
3935                 arvif->u.ap.ssid_len = info->ssid_len;
3936                 if (info->ssid_len)
3937                         memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
3938                 arvif->u.ap.hidden_ssid = info->hidden_ssid;
3939         }
3940
3941         if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
3942                 ether_addr_copy(arvif->bssid, info->bssid);
3943
3944         if (changed & BSS_CHANGED_BEACON_ENABLED)
3945                 ath10k_control_beaconing(arvif, info);
3946
3947         if (changed & BSS_CHANGED_ERP_CTS_PROT) {
3948                 arvif->use_cts_prot = info->use_cts_prot;
3949                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d cts_prot %d\n",
3950                            arvif->vdev_id, info->use_cts_prot);
3951
3952                 ret = ath10k_recalc_rtscts_prot(arvif);
3953                 if (ret)
3954                         ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
3955                                     arvif->vdev_id, ret);
3956
3957                 vdev_param = ar->wmi.vdev_param->protection_mode;
3958                 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3959                                                 info->use_cts_prot ? 1 : 0);
3960                 if (ret)
3961                         ath10k_warn(ar, "failed to set protection mode %d on vdev %i: %d\n",
3962                                         info->use_cts_prot, arvif->vdev_id, ret);
3963         }
3964
3965         if (changed & BSS_CHANGED_ERP_SLOT) {
3966                 if (info->use_short_slot)
3967                         slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
3968
3969                 else
3970                         slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
3971
3972                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
3973                            arvif->vdev_id, slottime);
3974
3975                 vdev_param = ar->wmi.vdev_param->slot_time;
3976                 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3977                                                 slottime);
3978                 if (ret)
3979                         ath10k_warn(ar, "failed to set erp slot for vdev %d: %i\n",
3980                                     arvif->vdev_id, ret);
3981         }
3982
3983         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
3984                 if (info->use_short_preamble)
3985                         preamble = WMI_VDEV_PREAMBLE_SHORT;
3986                 else
3987                         preamble = WMI_VDEV_PREAMBLE_LONG;
3988
3989                 ath10k_dbg(ar, ATH10K_DBG_MAC,
3990                            "mac vdev %d preamble %dn",
3991                            arvif->vdev_id, preamble);
3992
3993                 vdev_param = ar->wmi.vdev_param->preamble;
3994                 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3995                                                 preamble);
3996                 if (ret)
3997                         ath10k_warn(ar, "failed to set preamble for vdev %d: %i\n",
3998                                     arvif->vdev_id, ret);
3999         }
4000
4001         if (changed & BSS_CHANGED_ASSOC) {
4002                 if (info->assoc) {
4003                         /* Workaround: Make sure monitor vdev is not running
4004                          * when associating to prevent some firmware revisions
4005                          * (e.g. 10.1 and 10.2) from crashing.
4006                          */
4007                         if (ar->monitor_started)
4008                                 ath10k_monitor_stop(ar);
4009                         ath10k_bss_assoc(hw, vif, info);
4010                         ath10k_monitor_recalc(ar);
4011                 } else {
4012                         ath10k_bss_disassoc(hw, vif);
4013                 }
4014         }
4015
4016         if (changed & BSS_CHANGED_TXPOWER) {
4017                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev_id %i txpower %d\n",
4018                            arvif->vdev_id, info->txpower);
4019
4020                 arvif->txpower = info->txpower;
4021                 ret = ath10k_mac_txpower_recalc(ar);
4022                 if (ret)
4023                         ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
4024         }
4025
4026         if (changed & BSS_CHANGED_PS) {
4027                 arvif->ps = vif->bss_conf.ps;
4028
4029                 ret = ath10k_config_ps(ar);
4030                 if (ret)
4031                         ath10k_warn(ar, "failed to setup ps on vdev %i: %d\n",
4032                                     arvif->vdev_id, ret);
4033         }
4034
4035         mutex_unlock(&ar->conf_mutex);
4036 }
4037
4038 static int ath10k_hw_scan(struct ieee80211_hw *hw,
4039                           struct ieee80211_vif *vif,
4040                           struct ieee80211_scan_request *hw_req)
4041 {
4042         struct ath10k *ar = hw->priv;
4043         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4044         struct cfg80211_scan_request *req = &hw_req->req;
4045         struct wmi_start_scan_arg arg;
4046         int ret = 0;
4047         int i;
4048
4049         mutex_lock(&ar->conf_mutex);
4050
4051         spin_lock_bh(&ar->data_lock);
4052         switch (ar->scan.state) {
4053         case ATH10K_SCAN_IDLE:
4054                 reinit_completion(&ar->scan.started);
4055                 reinit_completion(&ar->scan.completed);
4056                 ar->scan.state = ATH10K_SCAN_STARTING;
4057                 ar->scan.is_roc = false;
4058                 ar->scan.vdev_id = arvif->vdev_id;
4059                 ret = 0;
4060                 break;
4061         case ATH10K_SCAN_STARTING:
4062         case ATH10K_SCAN_RUNNING:
4063         case ATH10K_SCAN_ABORTING:
4064                 ret = -EBUSY;
4065                 break;
4066         }
4067         spin_unlock_bh(&ar->data_lock);
4068
4069         if (ret)
4070                 goto exit;
4071
4072         memset(&arg, 0, sizeof(arg));
4073         ath10k_wmi_start_scan_init(ar, &arg);
4074         arg.vdev_id = arvif->vdev_id;
4075         arg.scan_id = ATH10K_SCAN_ID;
4076
4077         if (!req->no_cck)
4078                 arg.scan_ctrl_flags |= WMI_SCAN_ADD_CCK_RATES;
4079
4080         if (req->ie_len) {
4081                 arg.ie_len = req->ie_len;
4082                 memcpy(arg.ie, req->ie, arg.ie_len);
4083         }
4084
4085         if (req->n_ssids) {
4086                 arg.n_ssids = req->n_ssids;
4087                 for (i = 0; i < arg.n_ssids; i++) {
4088                         arg.ssids[i].len  = req->ssids[i].ssid_len;
4089                         arg.ssids[i].ssid = req->ssids[i].ssid;
4090                 }
4091         } else {
4092                 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
4093         }
4094
4095         if (req->n_channels) {
4096                 arg.n_channels = req->n_channels;
4097                 for (i = 0; i < arg.n_channels; i++)
4098                         arg.channels[i] = req->channels[i]->center_freq;
4099         }
4100
4101         ret = ath10k_start_scan(ar, &arg);
4102         if (ret) {
4103                 ath10k_warn(ar, "failed to start hw scan: %d\n", ret);
4104                 spin_lock_bh(&ar->data_lock);
4105                 ar->scan.state = ATH10K_SCAN_IDLE;
4106                 spin_unlock_bh(&ar->data_lock);
4107         }
4108
4109 exit:
4110         mutex_unlock(&ar->conf_mutex);
4111         return ret;
4112 }
4113
4114 static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
4115                                   struct ieee80211_vif *vif)
4116 {
4117         struct ath10k *ar = hw->priv;
4118
4119         mutex_lock(&ar->conf_mutex);
4120         ath10k_scan_abort(ar);
4121         mutex_unlock(&ar->conf_mutex);
4122
4123         cancel_delayed_work_sync(&ar->scan.timeout);
4124 }
4125
4126 static void ath10k_set_key_h_def_keyidx(struct ath10k *ar,
4127                                         struct ath10k_vif *arvif,
4128                                         enum set_key_cmd cmd,
4129                                         struct ieee80211_key_conf *key)
4130 {
4131         u32 vdev_param = arvif->ar->wmi.vdev_param->def_keyid;
4132         int ret;
4133
4134         /* 10.1 firmware branch requires default key index to be set to group
4135          * key index after installing it. Otherwise FW/HW Txes corrupted
4136          * frames with multi-vif APs. This is not required for main firmware
4137          * branch (e.g. 636).
4138          *
4139          * FIXME: This has been tested only in AP. It remains unknown if this
4140          * is required for multi-vif STA interfaces on 10.1 */
4141
4142         if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
4143                 return;
4144
4145         if (key->cipher == WLAN_CIPHER_SUITE_WEP40)
4146                 return;
4147
4148         if (key->cipher == WLAN_CIPHER_SUITE_WEP104)
4149                 return;
4150
4151         if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4152                 return;
4153
4154         if (cmd != SET_KEY)
4155                 return;
4156
4157         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
4158                                         key->keyidx);
4159         if (ret)
4160                 ath10k_warn(ar, "failed to set vdev %i group key as default key: %d\n",
4161                             arvif->vdev_id, ret);
4162 }
4163
4164 static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
4165                           struct ieee80211_vif *vif, struct ieee80211_sta *sta,
4166                           struct ieee80211_key_conf *key)
4167 {
4168         struct ath10k *ar = hw->priv;
4169         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4170         struct ath10k_peer *peer;
4171         const u8 *peer_addr;
4172         bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
4173                       key->cipher == WLAN_CIPHER_SUITE_WEP104;
4174         int ret = 0;
4175         u32 flags = 0;
4176
4177         /* this one needs to be done in software */
4178         if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
4179                 return 1;
4180
4181         if (key->keyidx > WMI_MAX_KEY_INDEX)
4182                 return -ENOSPC;
4183
4184         mutex_lock(&ar->conf_mutex);
4185
4186         if (sta)
4187                 peer_addr = sta->addr;
4188         else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
4189                 peer_addr = vif->bss_conf.bssid;
4190         else
4191                 peer_addr = vif->addr;
4192
4193         key->hw_key_idx = key->keyidx;
4194
4195         /* the peer should not disappear in mid-way (unless FW goes awry) since
4196          * we already hold conf_mutex. we just make sure its there now. */
4197         spin_lock_bh(&ar->data_lock);
4198         peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
4199         spin_unlock_bh(&ar->data_lock);
4200
4201         if (!peer) {
4202                 if (cmd == SET_KEY) {
4203                         ath10k_warn(ar, "failed to install key for non-existent peer %pM\n",
4204                                     peer_addr);
4205                         ret = -EOPNOTSUPP;
4206                         goto exit;
4207                 } else {
4208                         /* if the peer doesn't exist there is no key to disable
4209                          * anymore */
4210                         goto exit;
4211                 }
4212         }
4213
4214         if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4215                 flags |= WMI_KEY_PAIRWISE;
4216         else
4217                 flags |= WMI_KEY_GROUP;
4218
4219         if (is_wep) {
4220                 if (cmd == SET_KEY)
4221                         arvif->wep_keys[key->keyidx] = key;
4222                 else
4223                         arvif->wep_keys[key->keyidx] = NULL;
4224
4225                 if (cmd == DISABLE_KEY)
4226                         ath10k_clear_vdev_key(arvif, key);
4227
4228                 /* When WEP keys are uploaded it's possible that there are
4229                  * stations associated already (e.g. when merging) without any
4230                  * keys. Static WEP needs an explicit per-peer key upload.
4231                  */
4232                 if (vif->type == NL80211_IFTYPE_ADHOC &&
4233                     cmd == SET_KEY)
4234                         ath10k_mac_vif_update_wep_key(arvif, key);
4235
4236                 /* 802.1x never sets the def_wep_key_idx so each set_key()
4237                  * call changes default tx key.
4238                  *
4239                  * Static WEP sets def_wep_key_idx via .set_default_unicast_key
4240                  * after first set_key().
4241                  */
4242                 if (cmd == SET_KEY && arvif->def_wep_key_idx == -1)
4243                         flags |= WMI_KEY_TX_USAGE;
4244
4245                 /* mac80211 uploads static WEP keys as groupwise while fw/hw
4246                  * requires pairwise keys for non-self peers, i.e. BSSID in STA
4247                  * mode and associated stations in AP/IBSS.
4248                  *
4249                  * Static WEP keys for peer_addr=vif->addr and 802.1X WEP keys
4250                  * work fine when mapped directly from mac80211.
4251                  *
4252                  * Note: When installing first static WEP groupwise key (which
4253                  * should be pairwise) def_wep_key_idx isn't known yet (it's
4254                  * equal to -1).  Since .set_default_unicast_key is called only
4255                  * for static WEP it's used to re-upload the key as pairwise.
4256                  */
4257                 if (arvif->def_wep_key_idx >= 0 &&
4258                     memcmp(peer_addr, arvif->vif->addr, ETH_ALEN)) {
4259                         flags &= ~WMI_KEY_GROUP;
4260                         flags |= WMI_KEY_PAIRWISE;
4261                 }
4262         }
4263
4264         ret = ath10k_install_key(arvif, key, cmd, peer_addr, flags);
4265         if (ret) {
4266                 ath10k_warn(ar, "failed to install key for vdev %i peer %pM: %d\n",
4267                             arvif->vdev_id, peer_addr, ret);
4268                 goto exit;
4269         }
4270
4271         ath10k_set_key_h_def_keyidx(ar, arvif, cmd, key);
4272
4273         spin_lock_bh(&ar->data_lock);
4274         peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
4275         if (peer && cmd == SET_KEY)
4276                 peer->keys[key->keyidx] = key;
4277         else if (peer && cmd == DISABLE_KEY)
4278                 peer->keys[key->keyidx] = NULL;
4279         else if (peer == NULL)
4280                 /* impossible unless FW goes crazy */
4281                 ath10k_warn(ar, "Peer %pM disappeared!\n", peer_addr);
4282         spin_unlock_bh(&ar->data_lock);
4283
4284 exit:
4285         mutex_unlock(&ar->conf_mutex);
4286         return ret;
4287 }
4288
4289 static void ath10k_set_default_unicast_key(struct ieee80211_hw *hw,
4290                                            struct ieee80211_vif *vif,
4291                                            int keyidx)
4292 {
4293         struct ath10k *ar = hw->priv;
4294         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4295         int ret;
4296
4297         mutex_lock(&arvif->ar->conf_mutex);
4298
4299         if (arvif->ar->state != ATH10K_STATE_ON)
4300                 goto unlock;
4301
4302         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
4303                    arvif->vdev_id, keyidx);
4304
4305         ret = ath10k_wmi_vdev_set_param(arvif->ar,
4306                                         arvif->vdev_id,
4307                                         arvif->ar->wmi.vdev_param->def_keyid,
4308                                         keyidx);
4309
4310         if (ret) {
4311                 ath10k_warn(ar, "failed to update wep key index for vdev %d: %d\n",
4312                             arvif->vdev_id,
4313                             ret);
4314                 goto unlock;
4315         }
4316
4317         arvif->def_wep_key_idx = keyidx;
4318
4319         ret = ath10k_mac_vif_sta_fix_wep_key(arvif);
4320         if (ret) {
4321                 ath10k_warn(ar, "failed to fix sta wep key on vdev %i: %d\n",
4322                             arvif->vdev_id, ret);
4323                 goto unlock;
4324         }
4325
4326 unlock:
4327         mutex_unlock(&arvif->ar->conf_mutex);
4328 }
4329
4330 static void ath10k_sta_rc_update_wk(struct work_struct *wk)
4331 {
4332         struct ath10k *ar;
4333         struct ath10k_vif *arvif;
4334         struct ath10k_sta *arsta;
4335         struct ieee80211_sta *sta;
4336         u32 changed, bw, nss, smps;
4337         int err;
4338
4339         arsta = container_of(wk, struct ath10k_sta, update_wk);
4340         sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
4341         arvif = arsta->arvif;
4342         ar = arvif->ar;
4343
4344         spin_lock_bh(&ar->data_lock);
4345
4346         changed = arsta->changed;
4347         arsta->changed = 0;
4348
4349         bw = arsta->bw;
4350         nss = arsta->nss;
4351         smps = arsta->smps;
4352
4353         spin_unlock_bh(&ar->data_lock);
4354
4355         mutex_lock(&ar->conf_mutex);
4356
4357         if (changed & IEEE80211_RC_BW_CHANGED) {
4358                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM peer bw %d\n",
4359                            sta->addr, bw);
4360
4361                 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
4362                                                 WMI_PEER_CHAN_WIDTH, bw);
4363                 if (err)
4364                         ath10k_warn(ar, "failed to update STA %pM peer bw %d: %d\n",
4365                                     sta->addr, bw, err);
4366         }
4367
4368         if (changed & IEEE80211_RC_NSS_CHANGED) {
4369                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM nss %d\n",
4370                            sta->addr, nss);
4371
4372                 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
4373                                                 WMI_PEER_NSS, nss);
4374                 if (err)
4375                         ath10k_warn(ar, "failed to update STA %pM nss %d: %d\n",
4376                                     sta->addr, nss, err);
4377         }
4378
4379         if (changed & IEEE80211_RC_SMPS_CHANGED) {
4380                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM smps %d\n",
4381                            sta->addr, smps);
4382
4383                 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
4384                                                 WMI_PEER_SMPS_STATE, smps);
4385                 if (err)
4386                         ath10k_warn(ar, "failed to update STA %pM smps %d: %d\n",
4387                                     sta->addr, smps, err);
4388         }
4389
4390         if (changed & IEEE80211_RC_SUPP_RATES_CHANGED ||
4391             changed & IEEE80211_RC_NSS_CHANGED) {
4392                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM supp rates/nss\n",
4393                            sta->addr);
4394
4395                 err = ath10k_station_assoc(ar, arvif->vif, sta, true);
4396                 if (err)
4397                         ath10k_warn(ar, "failed to reassociate station: %pM\n",
4398                                     sta->addr);
4399         }
4400
4401         mutex_unlock(&ar->conf_mutex);
4402 }
4403
4404 static int ath10k_mac_inc_num_stations(struct ath10k_vif *arvif,
4405                                        struct ieee80211_sta *sta)
4406 {
4407         struct ath10k *ar = arvif->ar;
4408
4409         lockdep_assert_held(&ar->conf_mutex);
4410
4411         if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
4412                 return 0;
4413
4414         if (ar->num_stations >= ar->max_num_stations)
4415                 return -ENOBUFS;
4416
4417         ar->num_stations++;
4418
4419         return 0;
4420 }
4421
4422 static void ath10k_mac_dec_num_stations(struct ath10k_vif *arvif,
4423                                         struct ieee80211_sta *sta)
4424 {
4425         struct ath10k *ar = arvif->ar;
4426
4427         lockdep_assert_held(&ar->conf_mutex);
4428
4429         if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
4430                 return;
4431
4432         ar->num_stations--;
4433 }
4434
4435 static int ath10k_sta_state(struct ieee80211_hw *hw,
4436                             struct ieee80211_vif *vif,
4437                             struct ieee80211_sta *sta,
4438                             enum ieee80211_sta_state old_state,
4439                             enum ieee80211_sta_state new_state)
4440 {
4441         struct ath10k *ar = hw->priv;
4442         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4443         struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
4444         int ret = 0;
4445
4446         if (old_state == IEEE80211_STA_NOTEXIST &&
4447             new_state == IEEE80211_STA_NONE) {
4448                 memset(arsta, 0, sizeof(*arsta));
4449                 arsta->arvif = arvif;
4450                 INIT_WORK(&arsta->update_wk, ath10k_sta_rc_update_wk);
4451         }
4452
4453         /* cancel must be done outside the mutex to avoid deadlock */
4454         if ((old_state == IEEE80211_STA_NONE &&
4455              new_state == IEEE80211_STA_NOTEXIST))
4456                 cancel_work_sync(&arsta->update_wk);
4457
4458         mutex_lock(&ar->conf_mutex);
4459
4460         if (old_state == IEEE80211_STA_NOTEXIST &&
4461             new_state == IEEE80211_STA_NONE) {
4462                 /*
4463                  * New station addition.
4464                  */
4465                 ath10k_dbg(ar, ATH10K_DBG_MAC,
4466                            "mac vdev %d peer create %pM (new sta) sta %d / %d peer %d / %d\n",
4467                            arvif->vdev_id, sta->addr,
4468                            ar->num_stations + 1, ar->max_num_stations,
4469                            ar->num_peers + 1, ar->max_num_peers);
4470
4471                 ret = ath10k_mac_inc_num_stations(arvif, sta);
4472                 if (ret) {
4473                         ath10k_warn(ar, "refusing to associate station: too many connected already (%d)\n",
4474                                     ar->max_num_stations);
4475                         goto exit;
4476                 }
4477
4478                 ret = ath10k_peer_create(ar, arvif->vdev_id, sta->addr,
4479                                          WMI_PEER_TYPE_DEFAULT);
4480                 if (ret) {
4481                         ath10k_warn(ar, "failed to add peer %pM for vdev %d when adding a new sta: %i\n",
4482                                     sta->addr, arvif->vdev_id, ret);
4483                         ath10k_mac_dec_num_stations(arvif, sta);
4484                         goto exit;
4485                 }
4486
4487                 if (vif->type == NL80211_IFTYPE_STATION) {
4488                         WARN_ON(arvif->is_started);
4489
4490                         ret = ath10k_vdev_start(arvif);
4491                         if (ret) {
4492                                 ath10k_warn(ar, "failed to start vdev %i: %d\n",
4493                                             arvif->vdev_id, ret);
4494                                 WARN_ON(ath10k_peer_delete(ar, arvif->vdev_id,
4495                                                            sta->addr));
4496                                 ath10k_mac_dec_num_stations(arvif, sta);
4497                                 goto exit;
4498                         }
4499
4500                         arvif->is_started = true;
4501                 }
4502         } else if ((old_state == IEEE80211_STA_NONE &&
4503                     new_state == IEEE80211_STA_NOTEXIST)) {
4504                 /*
4505                  * Existing station deletion.
4506                  */
4507                 ath10k_dbg(ar, ATH10K_DBG_MAC,
4508                            "mac vdev %d peer delete %pM (sta gone)\n",
4509                            arvif->vdev_id, sta->addr);
4510
4511                 if (vif->type == NL80211_IFTYPE_STATION) {
4512                         WARN_ON(!arvif->is_started);
4513
4514                         ret = ath10k_vdev_stop(arvif);
4515                         if (ret)
4516                                 ath10k_warn(ar, "failed to stop vdev %i: %d\n",
4517                                             arvif->vdev_id, ret);
4518
4519                         arvif->is_started = false;
4520                 }
4521
4522                 ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
4523                 if (ret)
4524                         ath10k_warn(ar, "failed to delete peer %pM for vdev %d: %i\n",
4525                                     sta->addr, arvif->vdev_id, ret);
4526
4527                 ath10k_mac_dec_num_stations(arvif, sta);
4528         } else if (old_state == IEEE80211_STA_AUTH &&
4529                    new_state == IEEE80211_STA_ASSOC &&
4530                    (vif->type == NL80211_IFTYPE_AP ||
4531                     vif->type == NL80211_IFTYPE_ADHOC)) {
4532                 /*
4533                  * New association.
4534                  */
4535                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM associated\n",
4536                            sta->addr);
4537
4538                 ret = ath10k_station_assoc(ar, vif, sta, false);
4539                 if (ret)
4540                         ath10k_warn(ar, "failed to associate station %pM for vdev %i: %i\n",
4541                                     sta->addr, arvif->vdev_id, ret);
4542         } else if (old_state == IEEE80211_STA_ASSOC &&
4543                    new_state == IEEE80211_STA_AUTH &&
4544                    (vif->type == NL80211_IFTYPE_AP ||
4545                     vif->type == NL80211_IFTYPE_ADHOC)) {
4546                 /*
4547                  * Disassociation.
4548                  */
4549                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM disassociated\n",
4550                            sta->addr);
4551
4552                 ret = ath10k_station_disassoc(ar, vif, sta);
4553                 if (ret)
4554                         ath10k_warn(ar, "failed to disassociate station: %pM vdev %i: %i\n",
4555                                     sta->addr, arvif->vdev_id, ret);
4556         }
4557 exit:
4558         mutex_unlock(&ar->conf_mutex);
4559         return ret;
4560 }
4561
4562 static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
4563                                 u16 ac, bool enable)
4564 {
4565         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4566         struct wmi_sta_uapsd_auto_trig_arg arg = {};
4567         u32 prio = 0, acc = 0;
4568         u32 value = 0;
4569         int ret = 0;
4570
4571         lockdep_assert_held(&ar->conf_mutex);
4572
4573         if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
4574                 return 0;
4575
4576         switch (ac) {
4577         case IEEE80211_AC_VO:
4578                 value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
4579                         WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
4580                 prio = 7;
4581                 acc = 3;
4582                 break;
4583         case IEEE80211_AC_VI:
4584                 value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
4585                         WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
4586                 prio = 5;
4587                 acc = 2;
4588                 break;
4589         case IEEE80211_AC_BE:
4590                 value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
4591                         WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
4592                 prio = 2;
4593                 acc = 1;
4594                 break;
4595         case IEEE80211_AC_BK:
4596                 value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
4597                         WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
4598                 prio = 0;
4599                 acc = 0;
4600                 break;
4601         }
4602
4603         if (enable)
4604                 arvif->u.sta.uapsd |= value;
4605         else
4606                 arvif->u.sta.uapsd &= ~value;
4607
4608         ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4609                                           WMI_STA_PS_PARAM_UAPSD,
4610                                           arvif->u.sta.uapsd);
4611         if (ret) {
4612                 ath10k_warn(ar, "failed to set uapsd params: %d\n", ret);
4613                 goto exit;
4614         }
4615
4616         if (arvif->u.sta.uapsd)
4617                 value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
4618         else
4619                 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
4620
4621         ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4622                                           WMI_STA_PS_PARAM_RX_WAKE_POLICY,
4623                                           value);
4624         if (ret)
4625                 ath10k_warn(ar, "failed to set rx wake param: %d\n", ret);
4626
4627         ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
4628         if (ret) {
4629                 ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
4630                             arvif->vdev_id, ret);
4631                 return ret;
4632         }
4633
4634         ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
4635         if (ret) {
4636                 ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
4637                             arvif->vdev_id, ret);
4638                 return ret;
4639         }
4640
4641         if (test_bit(WMI_SERVICE_STA_UAPSD_BASIC_AUTO_TRIG, ar->wmi.svc_map) ||
4642             test_bit(WMI_SERVICE_STA_UAPSD_VAR_AUTO_TRIG, ar->wmi.svc_map)) {
4643                 /* Only userspace can make an educated decision when to send
4644                  * trigger frame. The following effectively disables u-UAPSD
4645                  * autotrigger in firmware (which is enabled by default
4646                  * provided the autotrigger service is available).
4647                  */
4648
4649                 arg.wmm_ac = acc;
4650                 arg.user_priority = prio;
4651                 arg.service_interval = 0;
4652                 arg.suspend_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
4653                 arg.delay_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
4654
4655                 ret = ath10k_wmi_vdev_sta_uapsd(ar, arvif->vdev_id,
4656                                                 arvif->bssid, &arg, 1);
4657                 if (ret) {
4658                         ath10k_warn(ar, "failed to set uapsd auto trigger %d\n",
4659                                     ret);
4660                         return ret;
4661                 }
4662         }
4663
4664 exit:
4665         return ret;
4666 }
4667
4668 static int ath10k_conf_tx(struct ieee80211_hw *hw,
4669                           struct ieee80211_vif *vif, u16 ac,
4670                           const struct ieee80211_tx_queue_params *params)
4671 {
4672         struct ath10k *ar = hw->priv;
4673         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4674         struct wmi_wmm_params_arg *p = NULL;
4675         int ret;
4676
4677         mutex_lock(&ar->conf_mutex);
4678
4679         switch (ac) {
4680         case IEEE80211_AC_VO:
4681                 p = &arvif->wmm_params.ac_vo;
4682                 break;
4683         case IEEE80211_AC_VI:
4684                 p = &arvif->wmm_params.ac_vi;
4685                 break;
4686         case IEEE80211_AC_BE:
4687                 p = &arvif->wmm_params.ac_be;
4688                 break;
4689         case IEEE80211_AC_BK:
4690                 p = &arvif->wmm_params.ac_bk;
4691                 break;
4692         }
4693
4694         if (WARN_ON(!p)) {
4695                 ret = -EINVAL;
4696                 goto exit;
4697         }
4698
4699         p->cwmin = params->cw_min;
4700         p->cwmax = params->cw_max;
4701         p->aifs = params->aifs;
4702
4703         /*
4704          * The channel time duration programmed in the HW is in absolute
4705          * microseconds, while mac80211 gives the txop in units of
4706          * 32 microseconds.
4707          */
4708         p->txop = params->txop * 32;
4709
4710         if (ar->wmi.ops->gen_vdev_wmm_conf) {
4711                 ret = ath10k_wmi_vdev_wmm_conf(ar, arvif->vdev_id,
4712                                                &arvif->wmm_params);
4713                 if (ret) {
4714                         ath10k_warn(ar, "failed to set vdev wmm params on vdev %i: %d\n",
4715                                     arvif->vdev_id, ret);
4716                         goto exit;
4717                 }
4718         } else {
4719                 /* This won't work well with multi-interface cases but it's
4720                  * better than nothing.
4721                  */
4722                 ret = ath10k_wmi_pdev_set_wmm_params(ar, &arvif->wmm_params);
4723                 if (ret) {
4724                         ath10k_warn(ar, "failed to set wmm params: %d\n", ret);
4725                         goto exit;
4726                 }
4727         }
4728
4729         ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
4730         if (ret)
4731                 ath10k_warn(ar, "failed to set sta uapsd: %d\n", ret);
4732
4733 exit:
4734         mutex_unlock(&ar->conf_mutex);
4735         return ret;
4736 }
4737
4738 #define ATH10K_ROC_TIMEOUT_HZ (2*HZ)
4739
4740 static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
4741                                     struct ieee80211_vif *vif,
4742                                     struct ieee80211_channel *chan,
4743                                     int duration,
4744                                     enum ieee80211_roc_type type)
4745 {
4746         struct ath10k *ar = hw->priv;
4747         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4748         struct wmi_start_scan_arg arg;
4749         int ret = 0;
4750
4751         mutex_lock(&ar->conf_mutex);
4752
4753         spin_lock_bh(&ar->data_lock);
4754         switch (ar->scan.state) {
4755         case ATH10K_SCAN_IDLE:
4756                 reinit_completion(&ar->scan.started);
4757                 reinit_completion(&ar->scan.completed);
4758                 reinit_completion(&ar->scan.on_channel);
4759                 ar->scan.state = ATH10K_SCAN_STARTING;
4760                 ar->scan.is_roc = true;
4761                 ar->scan.vdev_id = arvif->vdev_id;
4762                 ar->scan.roc_freq = chan->center_freq;
4763                 ret = 0;
4764                 break;
4765         case ATH10K_SCAN_STARTING:
4766         case ATH10K_SCAN_RUNNING:
4767         case ATH10K_SCAN_ABORTING:
4768                 ret = -EBUSY;
4769                 break;
4770         }
4771         spin_unlock_bh(&ar->data_lock);
4772
4773         if (ret)
4774                 goto exit;
4775
4776         duration = max(duration, WMI_SCAN_CHAN_MIN_TIME_MSEC);
4777
4778         memset(&arg, 0, sizeof(arg));
4779         ath10k_wmi_start_scan_init(ar, &arg);
4780         arg.vdev_id = arvif->vdev_id;
4781         arg.scan_id = ATH10K_SCAN_ID;
4782         arg.n_channels = 1;
4783         arg.channels[0] = chan->center_freq;
4784         arg.dwell_time_active = duration;
4785         arg.dwell_time_passive = duration;
4786         arg.max_scan_time = 2 * duration;
4787         arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
4788         arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
4789
4790         ret = ath10k_start_scan(ar, &arg);
4791         if (ret) {
4792                 ath10k_warn(ar, "failed to start roc scan: %d\n", ret);
4793                 spin_lock_bh(&ar->data_lock);
4794                 ar->scan.state = ATH10K_SCAN_IDLE;
4795                 spin_unlock_bh(&ar->data_lock);
4796                 goto exit;
4797         }
4798
4799         ret = wait_for_completion_timeout(&ar->scan.on_channel, 3*HZ);
4800         if (ret == 0) {
4801                 ath10k_warn(ar, "failed to switch to channel for roc scan\n");
4802
4803                 ret = ath10k_scan_stop(ar);
4804                 if (ret)
4805                         ath10k_warn(ar, "failed to stop scan: %d\n", ret);
4806
4807                 ret = -ETIMEDOUT;
4808                 goto exit;
4809         }
4810
4811         ret = 0;
4812 exit:
4813         mutex_unlock(&ar->conf_mutex);
4814         return ret;
4815 }
4816
4817 static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw)
4818 {
4819         struct ath10k *ar = hw->priv;
4820
4821         mutex_lock(&ar->conf_mutex);
4822         ath10k_scan_abort(ar);
4823         mutex_unlock(&ar->conf_mutex);
4824
4825         cancel_delayed_work_sync(&ar->scan.timeout);
4826
4827         return 0;
4828 }
4829
4830 /*
4831  * Both RTS and Fragmentation threshold are interface-specific
4832  * in ath10k, but device-specific in mac80211.
4833  */
4834
4835 static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
4836 {
4837         struct ath10k *ar = hw->priv;
4838         struct ath10k_vif *arvif;
4839         int ret = 0;
4840
4841         mutex_lock(&ar->conf_mutex);
4842         list_for_each_entry(arvif, &ar->arvifs, list) {
4843                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
4844                            arvif->vdev_id, value);
4845
4846                 ret = ath10k_mac_set_rts(arvif, value);
4847                 if (ret) {
4848                         ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
4849                                     arvif->vdev_id, ret);
4850                         break;
4851                 }
4852         }
4853         mutex_unlock(&ar->conf_mutex);
4854
4855         return ret;
4856 }
4857
4858 static void ath10k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4859                          u32 queues, bool drop)
4860 {
4861         struct ath10k *ar = hw->priv;
4862         bool skip;
4863         int ret;
4864
4865         /* mac80211 doesn't care if we really xmit queued frames or not
4866          * we'll collect those frames either way if we stop/delete vdevs */
4867         if (drop)
4868                 return;
4869
4870         mutex_lock(&ar->conf_mutex);
4871
4872         if (ar->state == ATH10K_STATE_WEDGED)
4873                 goto skip;
4874
4875         ret = wait_event_timeout(ar->htt.empty_tx_wq, ({
4876                         bool empty;
4877
4878                         spin_lock_bh(&ar->htt.tx_lock);
4879                         empty = (ar->htt.num_pending_tx == 0);
4880                         spin_unlock_bh(&ar->htt.tx_lock);
4881
4882                         skip = (ar->state == ATH10K_STATE_WEDGED) ||
4883                                test_bit(ATH10K_FLAG_CRASH_FLUSH,
4884                                         &ar->dev_flags);
4885
4886                         (empty || skip);
4887                 }), ATH10K_FLUSH_TIMEOUT_HZ);
4888
4889         if (ret <= 0 || skip)
4890                 ath10k_warn(ar, "failed to flush transmit queue (skip %i ar-state %i): %i\n",
4891                             skip, ar->state, ret);
4892
4893 skip:
4894         mutex_unlock(&ar->conf_mutex);
4895 }
4896
4897 /* TODO: Implement this function properly
4898  * For now it is needed to reply to Probe Requests in IBSS mode.
4899  * Propably we need this information from FW.
4900  */
4901 static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
4902 {
4903         return 1;
4904 }
4905
4906 static void ath10k_reconfig_complete(struct ieee80211_hw *hw,
4907                                      enum ieee80211_reconfig_type reconfig_type)
4908 {
4909         struct ath10k *ar = hw->priv;
4910
4911         if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
4912                 return;
4913
4914         mutex_lock(&ar->conf_mutex);
4915
4916         /* If device failed to restart it will be in a different state, e.g.
4917          * ATH10K_STATE_WEDGED */
4918         if (ar->state == ATH10K_STATE_RESTARTED) {
4919                 ath10k_info(ar, "device successfully recovered\n");
4920                 ar->state = ATH10K_STATE_ON;
4921                 ieee80211_wake_queues(ar->hw);
4922         }
4923
4924         mutex_unlock(&ar->conf_mutex);
4925 }
4926
4927 static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
4928                              struct survey_info *survey)
4929 {
4930         struct ath10k *ar = hw->priv;
4931         struct ieee80211_supported_band *sband;
4932         struct survey_info *ar_survey = &ar->survey[idx];
4933         int ret = 0;
4934
4935         mutex_lock(&ar->conf_mutex);
4936
4937         sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
4938         if (sband && idx >= sband->n_channels) {
4939                 idx -= sband->n_channels;
4940                 sband = NULL;
4941         }
4942
4943         if (!sband)
4944                 sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
4945
4946         if (!sband || idx >= sband->n_channels) {
4947                 ret = -ENOENT;
4948                 goto exit;
4949         }
4950
4951         spin_lock_bh(&ar->data_lock);
4952         memcpy(survey, ar_survey, sizeof(*survey));
4953         spin_unlock_bh(&ar->data_lock);
4954
4955         survey->channel = &sband->channels[idx];
4956
4957         if (ar->rx_channel == survey->channel)
4958                 survey->filled |= SURVEY_INFO_IN_USE;
4959
4960 exit:
4961         mutex_unlock(&ar->conf_mutex);
4962         return ret;
4963 }
4964
4965 /* Helper table for legacy fixed_rate/bitrate_mask */
4966 static const u8 cck_ofdm_rate[] = {
4967         /* CCK */
4968         3, /* 1Mbps */
4969         2, /* 2Mbps */
4970         1, /* 5.5Mbps */
4971         0, /* 11Mbps */
4972         /* OFDM */
4973         3, /* 6Mbps */
4974         7, /* 9Mbps */
4975         2, /* 12Mbps */
4976         6, /* 18Mbps */
4977         1, /* 24Mbps */
4978         5, /* 36Mbps */
4979         0, /* 48Mbps */
4980         4, /* 54Mbps */
4981 };
4982
4983 /* Check if only one bit set */
4984 static int ath10k_check_single_mask(u32 mask)
4985 {
4986         int bit;
4987
4988         bit = ffs(mask);
4989         if (!bit)
4990                 return 0;
4991
4992         mask &= ~BIT(bit - 1);
4993         if (mask)
4994                 return 2;
4995
4996         return 1;
4997 }
4998
4999 static bool
5000 ath10k_default_bitrate_mask(struct ath10k *ar,
5001                             enum ieee80211_band band,
5002                             const struct cfg80211_bitrate_mask *mask)
5003 {
5004         u32 legacy = 0x00ff;
5005         u8 ht = 0xff, i;
5006         u16 vht = 0x3ff;
5007         u16 nrf = ar->num_rf_chains;
5008
5009         if (ar->cfg_tx_chainmask)
5010                 nrf = get_nss_from_chainmask(ar->cfg_tx_chainmask);
5011
5012         switch (band) {
5013         case IEEE80211_BAND_2GHZ:
5014                 legacy = 0x00fff;
5015                 vht = 0;
5016                 break;
5017         case IEEE80211_BAND_5GHZ:
5018                 break;
5019         default:
5020                 return false;
5021         }
5022
5023         if (mask->control[band].legacy != legacy)
5024                 return false;
5025
5026         for (i = 0; i < nrf; i++)
5027                 if (mask->control[band].ht_mcs[i] != ht)
5028                         return false;
5029
5030         for (i = 0; i < nrf; i++)
5031                 if (mask->control[band].vht_mcs[i] != vht)
5032                         return false;
5033
5034         return true;
5035 }
5036
5037 static bool
5038 ath10k_bitrate_mask_nss(const struct cfg80211_bitrate_mask *mask,
5039                         enum ieee80211_band band,
5040                         u8 *fixed_nss)
5041 {
5042         int ht_nss = 0, vht_nss = 0, i;
5043
5044         /* check legacy */
5045         if (ath10k_check_single_mask(mask->control[band].legacy))
5046                 return false;
5047
5048         /* check HT */
5049         for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
5050                 if (mask->control[band].ht_mcs[i] == 0xff)
5051                         continue;
5052                 else if (mask->control[band].ht_mcs[i] == 0x00)
5053                         break;
5054
5055                 return false;
5056         }
5057
5058         ht_nss = i;
5059
5060         /* check VHT */
5061         for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
5062                 if (mask->control[band].vht_mcs[i] == 0x03ff)
5063                         continue;
5064                 else if (mask->control[band].vht_mcs[i] == 0x0000)
5065                         break;
5066
5067                 return false;
5068         }
5069
5070         vht_nss = i;
5071
5072         if (ht_nss > 0 && vht_nss > 0)
5073                 return false;
5074
5075         if (ht_nss)
5076                 *fixed_nss = ht_nss;
5077         else if (vht_nss)
5078                 *fixed_nss = vht_nss;
5079         else
5080                 return false;
5081
5082         return true;
5083 }
5084
5085 static bool
5086 ath10k_bitrate_mask_correct(const struct cfg80211_bitrate_mask *mask,
5087                             enum ieee80211_band band,
5088                             enum wmi_rate_preamble *preamble)
5089 {
5090         int legacy = 0, ht = 0, vht = 0, i;
5091
5092         *preamble = WMI_RATE_PREAMBLE_OFDM;
5093
5094         /* check legacy */
5095         legacy = ath10k_check_single_mask(mask->control[band].legacy);
5096         if (legacy > 1)
5097                 return false;
5098
5099         /* check HT */
5100         for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5101                 ht += ath10k_check_single_mask(mask->control[band].ht_mcs[i]);
5102         if (ht > 1)
5103                 return false;
5104
5105         /* check VHT */
5106         for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
5107                 vht += ath10k_check_single_mask(mask->control[band].vht_mcs[i]);
5108         if (vht > 1)
5109                 return false;
5110
5111         /* Currently we support only one fixed_rate */
5112         if ((legacy + ht + vht) != 1)
5113                 return false;
5114
5115         if (ht)
5116                 *preamble = WMI_RATE_PREAMBLE_HT;
5117         else if (vht)
5118                 *preamble = WMI_RATE_PREAMBLE_VHT;
5119
5120         return true;
5121 }
5122
5123 static bool
5124 ath10k_bitrate_mask_rate(struct ath10k *ar,
5125                          const struct cfg80211_bitrate_mask *mask,
5126                          enum ieee80211_band band,
5127                          u8 *fixed_rate,
5128                          u8 *fixed_nss)
5129 {
5130         u8 rate = 0, pream = 0, nss = 0, i;
5131         enum wmi_rate_preamble preamble;
5132
5133         /* Check if single rate correct */
5134         if (!ath10k_bitrate_mask_correct(mask, band, &preamble))
5135                 return false;
5136
5137         pream = preamble;
5138
5139         switch (preamble) {
5140         case WMI_RATE_PREAMBLE_CCK:
5141         case WMI_RATE_PREAMBLE_OFDM:
5142                 i = ffs(mask->control[band].legacy) - 1;
5143
5144                 if (band == IEEE80211_BAND_2GHZ && i < 4)
5145                         pream = WMI_RATE_PREAMBLE_CCK;
5146
5147                 if (band == IEEE80211_BAND_5GHZ)
5148                         i += 4;
5149
5150                 if (i >= ARRAY_SIZE(cck_ofdm_rate))
5151                         return false;
5152
5153                 rate = cck_ofdm_rate[i];
5154                 break;
5155         case WMI_RATE_PREAMBLE_HT:
5156                 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5157                         if (mask->control[band].ht_mcs[i])
5158                                 break;
5159
5160                 if (i == IEEE80211_HT_MCS_MASK_LEN)
5161                         return false;
5162
5163                 rate = ffs(mask->control[band].ht_mcs[i]) - 1;
5164                 nss = i;
5165                 break;
5166         case WMI_RATE_PREAMBLE_VHT:
5167                 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
5168                         if (mask->control[band].vht_mcs[i])
5169                                 break;
5170
5171                 if (i == NL80211_VHT_NSS_MAX)
5172                         return false;
5173
5174                 rate = ffs(mask->control[band].vht_mcs[i]) - 1;
5175                 nss = i;
5176                 break;
5177         }
5178
5179         *fixed_nss = nss + 1;
5180         nss <<= 4;
5181         pream <<= 6;
5182
5183         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac fixed rate pream 0x%02x nss 0x%02x rate 0x%02x\n",
5184                    pream, nss, rate);
5185
5186         *fixed_rate = pream | nss | rate;
5187
5188         return true;
5189 }
5190
5191 static bool ath10k_get_fixed_rate_nss(struct ath10k *ar,
5192                                       const struct cfg80211_bitrate_mask *mask,
5193                                       enum ieee80211_band band,
5194                                       u8 *fixed_rate,
5195                                       u8 *fixed_nss)
5196 {
5197         /* First check full NSS mask, if we can simply limit NSS */
5198         if (ath10k_bitrate_mask_nss(mask, band, fixed_nss))
5199                 return true;
5200
5201         /* Next Check single rate is set */
5202         return ath10k_bitrate_mask_rate(ar, mask, band, fixed_rate, fixed_nss);
5203 }
5204
5205 static int ath10k_set_fixed_rate_param(struct ath10k_vif *arvif,
5206                                        u8 fixed_rate,
5207                                        u8 fixed_nss,
5208                                        u8 force_sgi)
5209 {
5210         struct ath10k *ar = arvif->ar;
5211         u32 vdev_param;
5212         int ret = 0;
5213
5214         mutex_lock(&ar->conf_mutex);
5215
5216         if (arvif->fixed_rate == fixed_rate &&
5217             arvif->fixed_nss == fixed_nss &&
5218             arvif->force_sgi == force_sgi)
5219                 goto exit;
5220
5221         if (fixed_rate == WMI_FIXED_RATE_NONE)
5222                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac disable fixed bitrate mask\n");
5223
5224         if (force_sgi)
5225                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac force sgi\n");
5226
5227         vdev_param = ar->wmi.vdev_param->fixed_rate;
5228         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
5229                                         vdev_param, fixed_rate);
5230         if (ret) {
5231                 ath10k_warn(ar, "failed to set fixed rate param 0x%02x: %d\n",
5232                             fixed_rate, ret);
5233                 ret = -EINVAL;
5234                 goto exit;
5235         }
5236
5237         arvif->fixed_rate = fixed_rate;
5238
5239         vdev_param = ar->wmi.vdev_param->nss;
5240         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
5241                                         vdev_param, fixed_nss);
5242
5243         if (ret) {
5244                 ath10k_warn(ar, "failed to set fixed nss param %d: %d\n",
5245                             fixed_nss, ret);
5246                 ret = -EINVAL;
5247                 goto exit;
5248         }
5249
5250         arvif->fixed_nss = fixed_nss;
5251
5252         vdev_param = ar->wmi.vdev_param->sgi;
5253         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5254                                         force_sgi);
5255
5256         if (ret) {
5257                 ath10k_warn(ar, "failed to set sgi param %d: %d\n",
5258                             force_sgi, ret);
5259                 ret = -EINVAL;
5260                 goto exit;
5261         }
5262
5263         arvif->force_sgi = force_sgi;
5264
5265 exit:
5266         mutex_unlock(&ar->conf_mutex);
5267         return ret;
5268 }
5269
5270 static int ath10k_set_bitrate_mask(struct ieee80211_hw *hw,
5271                                    struct ieee80211_vif *vif,
5272                                    const struct cfg80211_bitrate_mask *mask)
5273 {
5274         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5275         struct ath10k *ar = arvif->ar;
5276         enum ieee80211_band band = ar->hw->conf.chandef.chan->band;
5277         u8 fixed_rate = WMI_FIXED_RATE_NONE;
5278         u8 fixed_nss = ar->num_rf_chains;
5279         u8 force_sgi;
5280
5281         if (ar->cfg_tx_chainmask)
5282                 fixed_nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
5283
5284         force_sgi = mask->control[band].gi;
5285         if (force_sgi == NL80211_TXRATE_FORCE_LGI)
5286                 return -EINVAL;
5287
5288         if (!ath10k_default_bitrate_mask(ar, band, mask)) {
5289                 if (!ath10k_get_fixed_rate_nss(ar, mask, band,
5290                                                &fixed_rate,
5291                                                &fixed_nss))
5292                         return -EINVAL;
5293         }
5294
5295         if (fixed_rate == WMI_FIXED_RATE_NONE && force_sgi) {
5296                 ath10k_warn(ar, "failed to force SGI usage for default rate settings\n");
5297                 return -EINVAL;
5298         }
5299
5300         return ath10k_set_fixed_rate_param(arvif, fixed_rate,
5301                                            fixed_nss, force_sgi);
5302 }
5303
5304 static void ath10k_sta_rc_update(struct ieee80211_hw *hw,
5305                                  struct ieee80211_vif *vif,
5306                                  struct ieee80211_sta *sta,
5307                                  u32 changed)
5308 {
5309         struct ath10k *ar = hw->priv;
5310         struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
5311         u32 bw, smps;
5312
5313         spin_lock_bh(&ar->data_lock);
5314
5315         ath10k_dbg(ar, ATH10K_DBG_MAC,
5316                    "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
5317                    sta->addr, changed, sta->bandwidth, sta->rx_nss,
5318                    sta->smps_mode);
5319
5320         if (changed & IEEE80211_RC_BW_CHANGED) {
5321                 bw = WMI_PEER_CHWIDTH_20MHZ;
5322
5323                 switch (sta->bandwidth) {
5324                 case IEEE80211_STA_RX_BW_20:
5325                         bw = WMI_PEER_CHWIDTH_20MHZ;
5326                         break;
5327                 case IEEE80211_STA_RX_BW_40:
5328                         bw = WMI_PEER_CHWIDTH_40MHZ;
5329                         break;
5330                 case IEEE80211_STA_RX_BW_80:
5331                         bw = WMI_PEER_CHWIDTH_80MHZ;
5332                         break;
5333                 case IEEE80211_STA_RX_BW_160:
5334                         ath10k_warn(ar, "Invalid bandwith %d in rc update for %pM\n",
5335                                     sta->bandwidth, sta->addr);
5336                         bw = WMI_PEER_CHWIDTH_20MHZ;
5337                         break;
5338                 }
5339
5340                 arsta->bw = bw;
5341         }
5342
5343         if (changed & IEEE80211_RC_NSS_CHANGED)
5344                 arsta->nss = sta->rx_nss;
5345
5346         if (changed & IEEE80211_RC_SMPS_CHANGED) {
5347                 smps = WMI_PEER_SMPS_PS_NONE;
5348
5349                 switch (sta->smps_mode) {
5350                 case IEEE80211_SMPS_AUTOMATIC:
5351                 case IEEE80211_SMPS_OFF:
5352                         smps = WMI_PEER_SMPS_PS_NONE;
5353                         break;
5354                 case IEEE80211_SMPS_STATIC:
5355                         smps = WMI_PEER_SMPS_STATIC;
5356                         break;
5357                 case IEEE80211_SMPS_DYNAMIC:
5358                         smps = WMI_PEER_SMPS_DYNAMIC;
5359                         break;
5360                 case IEEE80211_SMPS_NUM_MODES:
5361                         ath10k_warn(ar, "Invalid smps %d in sta rc update for %pM\n",
5362                                     sta->smps_mode, sta->addr);
5363                         smps = WMI_PEER_SMPS_PS_NONE;
5364                         break;
5365                 }
5366
5367                 arsta->smps = smps;
5368         }
5369
5370         arsta->changed |= changed;
5371
5372         spin_unlock_bh(&ar->data_lock);
5373
5374         ieee80211_queue_work(hw, &arsta->update_wk);
5375 }
5376
5377 static u64 ath10k_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
5378 {
5379         /*
5380          * FIXME: Return 0 for time being. Need to figure out whether FW
5381          * has the API to fetch 64-bit local TSF
5382          */
5383
5384         return 0;
5385 }
5386
5387 static int ath10k_ampdu_action(struct ieee80211_hw *hw,
5388                                struct ieee80211_vif *vif,
5389                                enum ieee80211_ampdu_mlme_action action,
5390                                struct ieee80211_sta *sta, u16 tid, u16 *ssn,
5391                                u8 buf_size)
5392 {
5393         struct ath10k *ar = hw->priv;
5394         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5395
5396         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ampdu vdev_id %i sta %pM tid %hu action %d\n",
5397                    arvif->vdev_id, sta->addr, tid, action);
5398
5399         switch (action) {
5400         case IEEE80211_AMPDU_RX_START:
5401         case IEEE80211_AMPDU_RX_STOP:
5402                 /* HTT AddBa/DelBa events trigger mac80211 Rx BA session
5403                  * creation/removal. Do we need to verify this?
5404                  */
5405                 return 0;
5406         case IEEE80211_AMPDU_TX_START:
5407         case IEEE80211_AMPDU_TX_STOP_CONT:
5408         case IEEE80211_AMPDU_TX_STOP_FLUSH:
5409         case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
5410         case IEEE80211_AMPDU_TX_OPERATIONAL:
5411                 /* Firmware offloads Tx aggregation entirely so deny mac80211
5412                  * Tx aggregation requests.
5413                  */
5414                 return -EOPNOTSUPP;
5415         }
5416
5417         return -EINVAL;
5418 }
5419
5420 static const struct ieee80211_ops ath10k_ops = {
5421         .tx                             = ath10k_tx,
5422         .start                          = ath10k_start,
5423         .stop                           = ath10k_stop,
5424         .config                         = ath10k_config,
5425         .add_interface                  = ath10k_add_interface,
5426         .remove_interface               = ath10k_remove_interface,
5427         .configure_filter               = ath10k_configure_filter,
5428         .bss_info_changed               = ath10k_bss_info_changed,
5429         .hw_scan                        = ath10k_hw_scan,
5430         .cancel_hw_scan                 = ath10k_cancel_hw_scan,
5431         .set_key                        = ath10k_set_key,
5432         .set_default_unicast_key        = ath10k_set_default_unicast_key,
5433         .sta_state                      = ath10k_sta_state,
5434         .conf_tx                        = ath10k_conf_tx,
5435         .remain_on_channel              = ath10k_remain_on_channel,
5436         .cancel_remain_on_channel       = ath10k_cancel_remain_on_channel,
5437         .set_rts_threshold              = ath10k_set_rts_threshold,
5438         .flush                          = ath10k_flush,
5439         .tx_last_beacon                 = ath10k_tx_last_beacon,
5440         .set_antenna                    = ath10k_set_antenna,
5441         .get_antenna                    = ath10k_get_antenna,
5442         .reconfig_complete              = ath10k_reconfig_complete,
5443         .get_survey                     = ath10k_get_survey,
5444         .set_bitrate_mask               = ath10k_set_bitrate_mask,
5445         .sta_rc_update                  = ath10k_sta_rc_update,
5446         .get_tsf                        = ath10k_get_tsf,
5447         .ampdu_action                   = ath10k_ampdu_action,
5448         .get_et_sset_count              = ath10k_debug_get_et_sset_count,
5449         .get_et_stats                   = ath10k_debug_get_et_stats,
5450         .get_et_strings                 = ath10k_debug_get_et_strings,
5451
5452         CFG80211_TESTMODE_CMD(ath10k_tm_cmd)
5453
5454 #ifdef CONFIG_PM
5455         .suspend                        = ath10k_wow_op_suspend,
5456         .resume                         = ath10k_wow_op_resume,
5457 #endif
5458 #ifdef CONFIG_MAC80211_DEBUGFS
5459         .sta_add_debugfs                = ath10k_sta_add_debugfs,
5460 #endif
5461 };
5462
5463 #define RATETAB_ENT(_rate, _rateid, _flags) { \
5464         .bitrate                = (_rate), \
5465         .flags                  = (_flags), \
5466         .hw_value               = (_rateid), \
5467 }
5468
5469 #define CHAN2G(_channel, _freq, _flags) { \
5470         .band                   = IEEE80211_BAND_2GHZ, \
5471         .hw_value               = (_channel), \
5472         .center_freq            = (_freq), \
5473         .flags                  = (_flags), \
5474         .max_antenna_gain       = 0, \
5475         .max_power              = 30, \
5476 }
5477
5478 #define CHAN5G(_channel, _freq, _flags) { \
5479         .band                   = IEEE80211_BAND_5GHZ, \
5480         .hw_value               = (_channel), \
5481         .center_freq            = (_freq), \
5482         .flags                  = (_flags), \
5483         .max_antenna_gain       = 0, \
5484         .max_power              = 30, \
5485 }
5486
5487 static const struct ieee80211_channel ath10k_2ghz_channels[] = {
5488         CHAN2G(1, 2412, 0),
5489         CHAN2G(2, 2417, 0),
5490         CHAN2G(3, 2422, 0),
5491         CHAN2G(4, 2427, 0),
5492         CHAN2G(5, 2432, 0),
5493         CHAN2G(6, 2437, 0),
5494         CHAN2G(7, 2442, 0),
5495         CHAN2G(8, 2447, 0),
5496         CHAN2G(9, 2452, 0),
5497         CHAN2G(10, 2457, 0),
5498         CHAN2G(11, 2462, 0),
5499         CHAN2G(12, 2467, 0),
5500         CHAN2G(13, 2472, 0),
5501         CHAN2G(14, 2484, 0),
5502 };
5503
5504 static const struct ieee80211_channel ath10k_5ghz_channels[] = {
5505         CHAN5G(36, 5180, 0),
5506         CHAN5G(40, 5200, 0),
5507         CHAN5G(44, 5220, 0),
5508         CHAN5G(48, 5240, 0),
5509         CHAN5G(52, 5260, 0),
5510         CHAN5G(56, 5280, 0),
5511         CHAN5G(60, 5300, 0),
5512         CHAN5G(64, 5320, 0),
5513         CHAN5G(100, 5500, 0),
5514         CHAN5G(104, 5520, 0),
5515         CHAN5G(108, 5540, 0),
5516         CHAN5G(112, 5560, 0),
5517         CHAN5G(116, 5580, 0),
5518         CHAN5G(120, 5600, 0),
5519         CHAN5G(124, 5620, 0),
5520         CHAN5G(128, 5640, 0),
5521         CHAN5G(132, 5660, 0),
5522         CHAN5G(136, 5680, 0),
5523         CHAN5G(140, 5700, 0),
5524         CHAN5G(144, 5720, 0),
5525         CHAN5G(149, 5745, 0),
5526         CHAN5G(153, 5765, 0),
5527         CHAN5G(157, 5785, 0),
5528         CHAN5G(161, 5805, 0),
5529         CHAN5G(165, 5825, 0),
5530 };
5531
5532 /* Note: Be careful if you re-order these. There is code which depends on this
5533  * ordering.
5534  */
5535 static struct ieee80211_rate ath10k_rates[] = {
5536         /* CCK */
5537         RATETAB_ENT(10,  0x82, 0),
5538         RATETAB_ENT(20,  0x84, 0),
5539         RATETAB_ENT(55,  0x8b, 0),
5540         RATETAB_ENT(110, 0x96, 0),
5541         /* OFDM */
5542         RATETAB_ENT(60,  0x0c, 0),
5543         RATETAB_ENT(90,  0x12, 0),
5544         RATETAB_ENT(120, 0x18, 0),
5545         RATETAB_ENT(180, 0x24, 0),
5546         RATETAB_ENT(240, 0x30, 0),
5547         RATETAB_ENT(360, 0x48, 0),
5548         RATETAB_ENT(480, 0x60, 0),
5549         RATETAB_ENT(540, 0x6c, 0),
5550 };
5551
5552 #define ath10k_a_rates (ath10k_rates + 4)
5553 #define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - 4)
5554 #define ath10k_g_rates (ath10k_rates + 0)
5555 #define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
5556
5557 struct ath10k *ath10k_mac_create(size_t priv_size)
5558 {
5559         struct ieee80211_hw *hw;
5560         struct ath10k *ar;
5561
5562         hw = ieee80211_alloc_hw(sizeof(struct ath10k) + priv_size, &ath10k_ops);
5563         if (!hw)
5564                 return NULL;
5565
5566         ar = hw->priv;
5567         ar->hw = hw;
5568
5569         return ar;
5570 }
5571
5572 void ath10k_mac_destroy(struct ath10k *ar)
5573 {
5574         ieee80211_free_hw(ar->hw);
5575 }
5576
5577 static const struct ieee80211_iface_limit ath10k_if_limits[] = {
5578         {
5579         .max    = 8,
5580         .types  = BIT(NL80211_IFTYPE_STATION)
5581                 | BIT(NL80211_IFTYPE_P2P_CLIENT)
5582         },
5583         {
5584         .max    = 3,
5585         .types  = BIT(NL80211_IFTYPE_P2P_GO)
5586         },
5587         {
5588         .max    = 1,
5589         .types  = BIT(NL80211_IFTYPE_P2P_DEVICE)
5590         },
5591         {
5592         .max    = 7,
5593         .types  = BIT(NL80211_IFTYPE_AP)
5594         },
5595 };
5596
5597 static const struct ieee80211_iface_limit ath10k_10x_if_limits[] = {
5598         {
5599         .max    = 8,
5600         .types  = BIT(NL80211_IFTYPE_AP)
5601         },
5602 };
5603
5604 static const struct ieee80211_iface_combination ath10k_if_comb[] = {
5605         {
5606                 .limits = ath10k_if_limits,
5607                 .n_limits = ARRAY_SIZE(ath10k_if_limits),
5608                 .max_interfaces = 8,
5609                 .num_different_channels = 1,
5610                 .beacon_int_infra_match = true,
5611         },
5612 };
5613
5614 static const struct ieee80211_iface_combination ath10k_10x_if_comb[] = {
5615         {
5616                 .limits = ath10k_10x_if_limits,
5617                 .n_limits = ARRAY_SIZE(ath10k_10x_if_limits),
5618                 .max_interfaces = 8,
5619                 .num_different_channels = 1,
5620                 .beacon_int_infra_match = true,
5621 #ifdef CONFIG_ATH10K_DFS_CERTIFIED
5622                 .radar_detect_widths =  BIT(NL80211_CHAN_WIDTH_20_NOHT) |
5623                                         BIT(NL80211_CHAN_WIDTH_20) |
5624                                         BIT(NL80211_CHAN_WIDTH_40) |
5625                                         BIT(NL80211_CHAN_WIDTH_80),
5626 #endif
5627         },
5628 };
5629
5630 static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
5631 {
5632         struct ieee80211_sta_vht_cap vht_cap = {0};
5633         u16 mcs_map;
5634         u32 val;
5635         int i;
5636
5637         vht_cap.vht_supported = 1;
5638         vht_cap.cap = ar->vht_cap_info;
5639
5640         if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
5641                                 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)) {
5642                 val = ar->num_rf_chains - 1;
5643                 val <<= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
5644                 val &= IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
5645
5646                 vht_cap.cap |= val;
5647         }
5648
5649         if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
5650                                 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)) {
5651                 val = ar->num_rf_chains - 1;
5652                 val <<= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
5653                 val &= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
5654
5655                 vht_cap.cap |= val;
5656         }
5657
5658         mcs_map = 0;
5659         for (i = 0; i < 8; i++) {
5660                 if (i < ar->num_rf_chains)
5661                         mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i*2);
5662                 else
5663                         mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i*2);
5664         }
5665
5666         vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
5667         vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
5668
5669         return vht_cap;
5670 }
5671
5672 static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
5673 {
5674         int i;
5675         struct ieee80211_sta_ht_cap ht_cap = {0};
5676
5677         if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
5678                 return ht_cap;
5679
5680         ht_cap.ht_supported = 1;
5681         ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
5682         ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
5683         ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
5684         ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
5685         ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
5686
5687         if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
5688                 ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
5689
5690         if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
5691                 ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
5692
5693         if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
5694                 u32 smps;
5695
5696                 smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
5697                 smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
5698
5699                 ht_cap.cap |= smps;
5700         }
5701
5702         if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC)
5703                 ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
5704
5705         if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
5706                 u32 stbc;
5707
5708                 stbc   = ar->ht_cap_info;
5709                 stbc  &= WMI_HT_CAP_RX_STBC;
5710                 stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
5711                 stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
5712                 stbc  &= IEEE80211_HT_CAP_RX_STBC;
5713
5714                 ht_cap.cap |= stbc;
5715         }
5716
5717         if (ar->ht_cap_info & WMI_HT_CAP_LDPC)
5718                 ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
5719
5720         if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
5721                 ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
5722
5723         /* max AMSDU is implicitly taken from vht_cap_info */
5724         if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
5725                 ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
5726
5727         for (i = 0; i < ar->num_rf_chains; i++)
5728                 ht_cap.mcs.rx_mask[i] = 0xFF;
5729
5730         ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
5731
5732         return ht_cap;
5733 }
5734
5735 static void ath10k_get_arvif_iter(void *data, u8 *mac,
5736                                   struct ieee80211_vif *vif)
5737 {
5738         struct ath10k_vif_iter *arvif_iter = data;
5739         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5740
5741         if (arvif->vdev_id == arvif_iter->vdev_id)
5742                 arvif_iter->arvif = arvif;
5743 }
5744
5745 struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
5746 {
5747         struct ath10k_vif_iter arvif_iter;
5748         u32 flags;
5749
5750         memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
5751         arvif_iter.vdev_id = vdev_id;
5752
5753         flags = IEEE80211_IFACE_ITER_RESUME_ALL;
5754         ieee80211_iterate_active_interfaces_atomic(ar->hw,
5755                                                    flags,
5756                                                    ath10k_get_arvif_iter,
5757                                                    &arvif_iter);
5758         if (!arvif_iter.arvif) {
5759                 ath10k_warn(ar, "No VIF found for vdev %d\n", vdev_id);
5760                 return NULL;
5761         }
5762
5763         return arvif_iter.arvif;
5764 }
5765
5766 int ath10k_mac_register(struct ath10k *ar)
5767 {
5768         static const u32 cipher_suites[] = {
5769                 WLAN_CIPHER_SUITE_WEP40,
5770                 WLAN_CIPHER_SUITE_WEP104,
5771                 WLAN_CIPHER_SUITE_TKIP,
5772                 WLAN_CIPHER_SUITE_CCMP,
5773                 WLAN_CIPHER_SUITE_AES_CMAC,
5774         };
5775         struct ieee80211_supported_band *band;
5776         struct ieee80211_sta_vht_cap vht_cap;
5777         struct ieee80211_sta_ht_cap ht_cap;
5778         void *channels;
5779         int ret;
5780
5781         SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
5782
5783         SET_IEEE80211_DEV(ar->hw, ar->dev);
5784
5785         ht_cap = ath10k_get_ht_cap(ar);
5786         vht_cap = ath10k_create_vht_cap(ar);
5787
5788         if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
5789                 channels = kmemdup(ath10k_2ghz_channels,
5790                                    sizeof(ath10k_2ghz_channels),
5791                                    GFP_KERNEL);
5792                 if (!channels) {
5793                         ret = -ENOMEM;
5794                         goto err_free;
5795                 }
5796
5797                 band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
5798                 band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
5799                 band->channels = channels;
5800                 band->n_bitrates = ath10k_g_rates_size;
5801                 band->bitrates = ath10k_g_rates;
5802                 band->ht_cap = ht_cap;
5803
5804                 /* Enable the VHT support at 2.4 GHz */
5805                 band->vht_cap = vht_cap;
5806
5807                 ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
5808         }
5809
5810         if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
5811                 channels = kmemdup(ath10k_5ghz_channels,
5812                                    sizeof(ath10k_5ghz_channels),
5813                                    GFP_KERNEL);
5814                 if (!channels) {
5815                         ret = -ENOMEM;
5816                         goto err_free;
5817                 }
5818
5819                 band = &ar->mac.sbands[IEEE80211_BAND_5GHZ];
5820                 band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
5821                 band->channels = channels;
5822                 band->n_bitrates = ath10k_a_rates_size;
5823                 band->bitrates = ath10k_a_rates;
5824                 band->ht_cap = ht_cap;
5825                 band->vht_cap = vht_cap;
5826                 ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
5827         }
5828
5829         ar->hw->wiphy->interface_modes =
5830                 BIT(NL80211_IFTYPE_STATION) |
5831                 BIT(NL80211_IFTYPE_AP);
5832
5833         ar->hw->wiphy->available_antennas_rx = ar->supp_rx_chainmask;
5834         ar->hw->wiphy->available_antennas_tx = ar->supp_tx_chainmask;
5835
5836         if (!test_bit(ATH10K_FW_FEATURE_NO_P2P, ar->fw_features))
5837                 ar->hw->wiphy->interface_modes |=
5838                         BIT(NL80211_IFTYPE_P2P_DEVICE) |
5839                         BIT(NL80211_IFTYPE_P2P_CLIENT) |
5840                         BIT(NL80211_IFTYPE_P2P_GO);
5841
5842         ar->hw->flags = IEEE80211_HW_SIGNAL_DBM |
5843                         IEEE80211_HW_SUPPORTS_PS |
5844                         IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
5845                         IEEE80211_HW_MFP_CAPABLE |
5846                         IEEE80211_HW_REPORTS_TX_ACK_STATUS |
5847                         IEEE80211_HW_HAS_RATE_CONTROL |
5848                         IEEE80211_HW_AP_LINK_PS |
5849                         IEEE80211_HW_SPECTRUM_MGMT |
5850                         IEEE80211_HW_SW_CRYPTO_CONTROL |
5851                         IEEE80211_HW_CONNECTION_MONITOR;
5852
5853         ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
5854
5855         if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
5856                 ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
5857
5858         if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
5859                 ar->hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
5860                 ar->hw->flags |= IEEE80211_HW_TX_AMPDU_SETUP_IN_HW;
5861         }
5862
5863         ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
5864         ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
5865
5866         ar->hw->vif_data_size = sizeof(struct ath10k_vif);
5867         ar->hw->sta_data_size = sizeof(struct ath10k_sta);
5868
5869         ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
5870
5871         if (test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)) {
5872                 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
5873
5874                 /* Firmware delivers WPS/P2P Probe Requests frames to driver so
5875                  * that userspace (e.g. wpa_supplicant/hostapd) can generate
5876                  * correct Probe Responses. This is more of a hack advert..
5877                  */
5878                 ar->hw->wiphy->probe_resp_offload |=
5879                         NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
5880                         NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
5881                         NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
5882         }
5883
5884         ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
5885         ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
5886         ar->hw->wiphy->max_remain_on_channel_duration = 5000;
5887
5888         ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
5889         ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE;
5890
5891         ar->hw->wiphy->max_ap_assoc_sta = ar->max_num_stations;
5892
5893         ret = ath10k_wow_init(ar);
5894         if (ret) {
5895                 ath10k_warn(ar, "failed to init wow: %d\n", ret);
5896                 goto err_free;
5897         }
5898
5899         /*
5900          * on LL hardware queues are managed entirely by the FW
5901          * so we only advertise to mac we can do the queues thing
5902          */
5903         ar->hw->queues = 4;
5904
5905         switch (ar->wmi.op_version) {
5906         case ATH10K_FW_WMI_OP_VERSION_MAIN:
5907         case ATH10K_FW_WMI_OP_VERSION_TLV:
5908                 ar->hw->wiphy->iface_combinations = ath10k_if_comb;
5909                 ar->hw->wiphy->n_iface_combinations =
5910                         ARRAY_SIZE(ath10k_if_comb);
5911                 ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
5912                 break;
5913         case ATH10K_FW_WMI_OP_VERSION_10_1:
5914         case ATH10K_FW_WMI_OP_VERSION_10_2:
5915         case ATH10K_FW_WMI_OP_VERSION_10_2_4:
5916                 ar->hw->wiphy->iface_combinations = ath10k_10x_if_comb;
5917                 ar->hw->wiphy->n_iface_combinations =
5918                         ARRAY_SIZE(ath10k_10x_if_comb);
5919                 break;
5920         case ATH10K_FW_WMI_OP_VERSION_UNSET:
5921         case ATH10K_FW_WMI_OP_VERSION_MAX:
5922                 WARN_ON(1);
5923                 ret = -EINVAL;
5924                 goto err_free;
5925         }
5926
5927         ar->hw->netdev_features = NETIF_F_HW_CSUM;
5928
5929         if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED)) {
5930                 /* Init ath dfs pattern detector */
5931                 ar->ath_common.debug_mask = ATH_DBG_DFS;
5932                 ar->dfs_detector = dfs_pattern_detector_init(&ar->ath_common,
5933                                                              NL80211_DFS_UNSET);
5934
5935                 if (!ar->dfs_detector)
5936                         ath10k_warn(ar, "failed to initialise DFS pattern detector\n");
5937         }
5938
5939         ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
5940                             ath10k_reg_notifier);
5941         if (ret) {
5942                 ath10k_err(ar, "failed to initialise regulatory: %i\n", ret);
5943                 goto err_free;
5944         }
5945
5946         ar->hw->wiphy->cipher_suites = cipher_suites;
5947         ar->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
5948
5949         ret = ieee80211_register_hw(ar->hw);
5950         if (ret) {
5951                 ath10k_err(ar, "failed to register ieee80211: %d\n", ret);
5952                 goto err_free;
5953         }
5954
5955         if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
5956                 ret = regulatory_hint(ar->hw->wiphy,
5957                                       ar->ath_common.regulatory.alpha2);
5958                 if (ret)
5959                         goto err_unregister;
5960         }
5961
5962         return 0;
5963
5964 err_unregister:
5965         ieee80211_unregister_hw(ar->hw);
5966 err_free:
5967         kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
5968         kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
5969
5970         return ret;
5971 }
5972
5973 void ath10k_mac_unregister(struct ath10k *ar)
5974 {
5975         ieee80211_unregister_hw(ar->hw);
5976
5977         if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
5978                 ar->dfs_detector->exit(ar->dfs_detector);
5979
5980         kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
5981         kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
5982
5983         SET_IEEE80211_DEV(ar->hw, NULL);
5984 }