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