Merge tag 'wireless-drivers-next-for-davem-2015-12-07' of git://git.kernel.org/pub...
[linux-2.6-block.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-tlv.h"
32 #include "wmi-ops.h"
33 #include "wow.h"
34
35 /*********/
36 /* Rates */
37 /*********/
38
39 static struct ieee80211_rate ath10k_rates[] = {
40         { .bitrate = 10,
41           .hw_value = ATH10K_HW_RATE_CCK_LP_1M },
42         { .bitrate = 20,
43           .hw_value = ATH10K_HW_RATE_CCK_LP_2M,
44           .hw_value_short = ATH10K_HW_RATE_CCK_SP_2M,
45           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
46         { .bitrate = 55,
47           .hw_value = ATH10K_HW_RATE_CCK_LP_5_5M,
48           .hw_value_short = ATH10K_HW_RATE_CCK_SP_5_5M,
49           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
50         { .bitrate = 110,
51           .hw_value = ATH10K_HW_RATE_CCK_LP_11M,
52           .hw_value_short = ATH10K_HW_RATE_CCK_SP_11M,
53           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
54
55         { .bitrate = 60, .hw_value = ATH10K_HW_RATE_OFDM_6M },
56         { .bitrate = 90, .hw_value = ATH10K_HW_RATE_OFDM_9M },
57         { .bitrate = 120, .hw_value = ATH10K_HW_RATE_OFDM_12M },
58         { .bitrate = 180, .hw_value = ATH10K_HW_RATE_OFDM_18M },
59         { .bitrate = 240, .hw_value = ATH10K_HW_RATE_OFDM_24M },
60         { .bitrate = 360, .hw_value = ATH10K_HW_RATE_OFDM_36M },
61         { .bitrate = 480, .hw_value = ATH10K_HW_RATE_OFDM_48M },
62         { .bitrate = 540, .hw_value = ATH10K_HW_RATE_OFDM_54M },
63 };
64
65 #define ATH10K_MAC_FIRST_OFDM_RATE_IDX 4
66
67 #define ath10k_a_rates (ath10k_rates + ATH10K_MAC_FIRST_OFDM_RATE_IDX)
68 #define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - \
69                              ATH10K_MAC_FIRST_OFDM_RATE_IDX)
70 #define ath10k_g_rates (ath10k_rates + 0)
71 #define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
72
73 static bool ath10k_mac_bitrate_is_cck(int bitrate)
74 {
75         switch (bitrate) {
76         case 10:
77         case 20:
78         case 55:
79         case 110:
80                 return true;
81         }
82
83         return false;
84 }
85
86 static u8 ath10k_mac_bitrate_to_rate(int bitrate)
87 {
88         return DIV_ROUND_UP(bitrate, 5) |
89                (ath10k_mac_bitrate_is_cck(bitrate) ? BIT(7) : 0);
90 }
91
92 u8 ath10k_mac_hw_rate_to_idx(const struct ieee80211_supported_band *sband,
93                              u8 hw_rate, bool cck)
94 {
95         const struct ieee80211_rate *rate;
96         int i;
97
98         for (i = 0; i < sband->n_bitrates; i++) {
99                 rate = &sband->bitrates[i];
100
101                 if (ath10k_mac_bitrate_is_cck(rate->bitrate) != cck)
102                         continue;
103
104                 if (rate->hw_value == hw_rate)
105                         return i;
106                 else if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE &&
107                          rate->hw_value_short == hw_rate)
108                         return i;
109         }
110
111         return 0;
112 }
113
114 u8 ath10k_mac_bitrate_to_idx(const struct ieee80211_supported_band *sband,
115                              u32 bitrate)
116 {
117         int i;
118
119         for (i = 0; i < sband->n_bitrates; i++)
120                 if (sband->bitrates[i].bitrate == bitrate)
121                         return i;
122
123         return 0;
124 }
125
126 static int ath10k_mac_get_max_vht_mcs_map(u16 mcs_map, int nss)
127 {
128         switch ((mcs_map >> (2 * nss)) & 0x3) {
129         case IEEE80211_VHT_MCS_SUPPORT_0_7: return BIT(8) - 1;
130         case IEEE80211_VHT_MCS_SUPPORT_0_8: return BIT(9) - 1;
131         case IEEE80211_VHT_MCS_SUPPORT_0_9: return BIT(10) - 1;
132         }
133         return 0;
134 }
135
136 static u32
137 ath10k_mac_max_ht_nss(const u8 ht_mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
138 {
139         int nss;
140
141         for (nss = IEEE80211_HT_MCS_MASK_LEN - 1; nss >= 0; nss--)
142                 if (ht_mcs_mask[nss])
143                         return nss + 1;
144
145         return 1;
146 }
147
148 static u32
149 ath10k_mac_max_vht_nss(const u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
150 {
151         int nss;
152
153         for (nss = NL80211_VHT_NSS_MAX - 1; nss >= 0; nss--)
154                 if (vht_mcs_mask[nss])
155                         return nss + 1;
156
157         return 1;
158 }
159
160 /**********/
161 /* Crypto */
162 /**********/
163
164 static int ath10k_send_key(struct ath10k_vif *arvif,
165                            struct ieee80211_key_conf *key,
166                            enum set_key_cmd cmd,
167                            const u8 *macaddr, u32 flags)
168 {
169         struct ath10k *ar = arvif->ar;
170         struct wmi_vdev_install_key_arg arg = {
171                 .vdev_id = arvif->vdev_id,
172                 .key_idx = key->keyidx,
173                 .key_len = key->keylen,
174                 .key_data = key->key,
175                 .key_flags = flags,
176                 .macaddr = macaddr,
177         };
178
179         lockdep_assert_held(&arvif->ar->conf_mutex);
180
181         switch (key->cipher) {
182         case WLAN_CIPHER_SUITE_CCMP:
183                 arg.key_cipher = WMI_CIPHER_AES_CCM;
184                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
185                 break;
186         case WLAN_CIPHER_SUITE_TKIP:
187                 arg.key_cipher = WMI_CIPHER_TKIP;
188                 arg.key_txmic_len = 8;
189                 arg.key_rxmic_len = 8;
190                 break;
191         case WLAN_CIPHER_SUITE_WEP40:
192         case WLAN_CIPHER_SUITE_WEP104:
193                 arg.key_cipher = WMI_CIPHER_WEP;
194                 break;
195         case WLAN_CIPHER_SUITE_AES_CMAC:
196                 WARN_ON(1);
197                 return -EINVAL;
198         default:
199                 ath10k_warn(ar, "cipher %d is not supported\n", key->cipher);
200                 return -EOPNOTSUPP;
201         }
202
203         if (test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
204                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
205
206         if (cmd == DISABLE_KEY) {
207                 arg.key_cipher = WMI_CIPHER_NONE;
208                 arg.key_data = NULL;
209         }
210
211         return ath10k_wmi_vdev_install_key(arvif->ar, &arg);
212 }
213
214 static int ath10k_install_key(struct ath10k_vif *arvif,
215                               struct ieee80211_key_conf *key,
216                               enum set_key_cmd cmd,
217                               const u8 *macaddr, u32 flags)
218 {
219         struct ath10k *ar = arvif->ar;
220         int ret;
221         unsigned long time_left;
222
223         lockdep_assert_held(&ar->conf_mutex);
224
225         reinit_completion(&ar->install_key_done);
226
227         if (arvif->nohwcrypt)
228                 return 1;
229
230         ret = ath10k_send_key(arvif, key, cmd, macaddr, flags);
231         if (ret)
232                 return ret;
233
234         time_left = wait_for_completion_timeout(&ar->install_key_done, 3 * HZ);
235         if (time_left == 0)
236                 return -ETIMEDOUT;
237
238         return 0;
239 }
240
241 static int ath10k_install_peer_wep_keys(struct ath10k_vif *arvif,
242                                         const u8 *addr)
243 {
244         struct ath10k *ar = arvif->ar;
245         struct ath10k_peer *peer;
246         int ret;
247         int i;
248         u32 flags;
249
250         lockdep_assert_held(&ar->conf_mutex);
251
252         if (WARN_ON(arvif->vif->type != NL80211_IFTYPE_AP &&
253                     arvif->vif->type != NL80211_IFTYPE_ADHOC))
254                 return -EINVAL;
255
256         spin_lock_bh(&ar->data_lock);
257         peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
258         spin_unlock_bh(&ar->data_lock);
259
260         if (!peer)
261                 return -ENOENT;
262
263         for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
264                 if (arvif->wep_keys[i] == NULL)
265                         continue;
266
267                 switch (arvif->vif->type) {
268                 case NL80211_IFTYPE_AP:
269                         flags = WMI_KEY_PAIRWISE;
270
271                         if (arvif->def_wep_key_idx == i)
272                                 flags |= WMI_KEY_TX_USAGE;
273
274                         ret = ath10k_install_key(arvif, arvif->wep_keys[i],
275                                                  SET_KEY, addr, flags);
276                         if (ret < 0)
277                                 return ret;
278                         break;
279                 case NL80211_IFTYPE_ADHOC:
280                         ret = ath10k_install_key(arvif, arvif->wep_keys[i],
281                                                  SET_KEY, addr,
282                                                  WMI_KEY_PAIRWISE);
283                         if (ret < 0)
284                                 return ret;
285
286                         ret = ath10k_install_key(arvif, arvif->wep_keys[i],
287                                                  SET_KEY, addr, WMI_KEY_GROUP);
288                         if (ret < 0)
289                                 return ret;
290                         break;
291                 default:
292                         WARN_ON(1);
293                         return -EINVAL;
294                 }
295
296                 spin_lock_bh(&ar->data_lock);
297                 peer->keys[i] = arvif->wep_keys[i];
298                 spin_unlock_bh(&ar->data_lock);
299         }
300
301         /* In some cases (notably with static WEP IBSS with multiple keys)
302          * multicast Tx becomes broken. Both pairwise and groupwise keys are
303          * installed already. Using WMI_KEY_TX_USAGE in different combinations
304          * didn't seem help. Using def_keyid vdev parameter seems to be
305          * effective so use that.
306          *
307          * FIXME: Revisit. Perhaps this can be done in a less hacky way.
308          */
309         if (arvif->vif->type != NL80211_IFTYPE_ADHOC)
310                 return 0;
311
312         if (arvif->def_wep_key_idx == -1)
313                 return 0;
314
315         ret = ath10k_wmi_vdev_set_param(arvif->ar,
316                                         arvif->vdev_id,
317                                         arvif->ar->wmi.vdev_param->def_keyid,
318                                         arvif->def_wep_key_idx);
319         if (ret) {
320                 ath10k_warn(ar, "failed to re-set def wpa key idxon vdev %i: %d\n",
321                             arvif->vdev_id, ret);
322                 return ret;
323         }
324
325         return 0;
326 }
327
328 static int ath10k_clear_peer_keys(struct ath10k_vif *arvif,
329                                   const u8 *addr)
330 {
331         struct ath10k *ar = arvif->ar;
332         struct ath10k_peer *peer;
333         int first_errno = 0;
334         int ret;
335         int i;
336         u32 flags = 0;
337
338         lockdep_assert_held(&ar->conf_mutex);
339
340         spin_lock_bh(&ar->data_lock);
341         peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
342         spin_unlock_bh(&ar->data_lock);
343
344         if (!peer)
345                 return -ENOENT;
346
347         for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
348                 if (peer->keys[i] == NULL)
349                         continue;
350
351                 /* key flags are not required to delete the key */
352                 ret = ath10k_install_key(arvif, peer->keys[i],
353                                          DISABLE_KEY, addr, flags);
354                 if (ret < 0 && first_errno == 0)
355                         first_errno = ret;
356
357                 if (ret < 0)
358                         ath10k_warn(ar, "failed to remove peer wep key %d: %d\n",
359                                     i, ret);
360
361                 spin_lock_bh(&ar->data_lock);
362                 peer->keys[i] = NULL;
363                 spin_unlock_bh(&ar->data_lock);
364         }
365
366         return first_errno;
367 }
368
369 bool ath10k_mac_is_peer_wep_key_set(struct ath10k *ar, const u8 *addr,
370                                     u8 keyidx)
371 {
372         struct ath10k_peer *peer;
373         int i;
374
375         lockdep_assert_held(&ar->data_lock);
376
377         /* We don't know which vdev this peer belongs to,
378          * since WMI doesn't give us that information.
379          *
380          * FIXME: multi-bss needs to be handled.
381          */
382         peer = ath10k_peer_find(ar, 0, addr);
383         if (!peer)
384                 return false;
385
386         for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
387                 if (peer->keys[i] && peer->keys[i]->keyidx == keyidx)
388                         return true;
389         }
390
391         return false;
392 }
393
394 static int ath10k_clear_vdev_key(struct ath10k_vif *arvif,
395                                  struct ieee80211_key_conf *key)
396 {
397         struct ath10k *ar = arvif->ar;
398         struct ath10k_peer *peer;
399         u8 addr[ETH_ALEN];
400         int first_errno = 0;
401         int ret;
402         int i;
403         u32 flags = 0;
404
405         lockdep_assert_held(&ar->conf_mutex);
406
407         for (;;) {
408                 /* since ath10k_install_key we can't hold data_lock all the
409                  * time, so we try to remove the keys incrementally */
410                 spin_lock_bh(&ar->data_lock);
411                 i = 0;
412                 list_for_each_entry(peer, &ar->peers, list) {
413                         for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
414                                 if (peer->keys[i] == key) {
415                                         ether_addr_copy(addr, peer->addr);
416                                         peer->keys[i] = NULL;
417                                         break;
418                                 }
419                         }
420
421                         if (i < ARRAY_SIZE(peer->keys))
422                                 break;
423                 }
424                 spin_unlock_bh(&ar->data_lock);
425
426                 if (i == ARRAY_SIZE(peer->keys))
427                         break;
428                 /* key flags are not required to delete the key */
429                 ret = ath10k_install_key(arvif, key, DISABLE_KEY, addr, flags);
430                 if (ret < 0 && first_errno == 0)
431                         first_errno = ret;
432
433                 if (ret)
434                         ath10k_warn(ar, "failed to remove key for %pM: %d\n",
435                                     addr, ret);
436         }
437
438         return first_errno;
439 }
440
441 static int ath10k_mac_vif_update_wep_key(struct ath10k_vif *arvif,
442                                          struct ieee80211_key_conf *key)
443 {
444         struct ath10k *ar = arvif->ar;
445         struct ath10k_peer *peer;
446         int ret;
447
448         lockdep_assert_held(&ar->conf_mutex);
449
450         list_for_each_entry(peer, &ar->peers, list) {
451                 if (!memcmp(peer->addr, arvif->vif->addr, ETH_ALEN))
452                         continue;
453
454                 if (!memcmp(peer->addr, arvif->bssid, ETH_ALEN))
455                         continue;
456
457                 if (peer->keys[key->keyidx] == key)
458                         continue;
459
460                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vif vdev %i update key %i needs update\n",
461                            arvif->vdev_id, key->keyidx);
462
463                 ret = ath10k_install_peer_wep_keys(arvif, peer->addr);
464                 if (ret) {
465                         ath10k_warn(ar, "failed to update wep keys on vdev %i for peer %pM: %d\n",
466                                     arvif->vdev_id, peer->addr, ret);
467                         return ret;
468                 }
469         }
470
471         return 0;
472 }
473
474 /*********************/
475 /* General utilities */
476 /*********************/
477
478 static inline enum wmi_phy_mode
479 chan_to_phymode(const struct cfg80211_chan_def *chandef)
480 {
481         enum wmi_phy_mode phymode = MODE_UNKNOWN;
482
483         switch (chandef->chan->band) {
484         case IEEE80211_BAND_2GHZ:
485                 switch (chandef->width) {
486                 case NL80211_CHAN_WIDTH_20_NOHT:
487                         if (chandef->chan->flags & IEEE80211_CHAN_NO_OFDM)
488                                 phymode = MODE_11B;
489                         else
490                                 phymode = MODE_11G;
491                         break;
492                 case NL80211_CHAN_WIDTH_20:
493                         phymode = MODE_11NG_HT20;
494                         break;
495                 case NL80211_CHAN_WIDTH_40:
496                         phymode = MODE_11NG_HT40;
497                         break;
498                 case NL80211_CHAN_WIDTH_5:
499                 case NL80211_CHAN_WIDTH_10:
500                 case NL80211_CHAN_WIDTH_80:
501                 case NL80211_CHAN_WIDTH_80P80:
502                 case NL80211_CHAN_WIDTH_160:
503                         phymode = MODE_UNKNOWN;
504                         break;
505                 }
506                 break;
507         case IEEE80211_BAND_5GHZ:
508                 switch (chandef->width) {
509                 case NL80211_CHAN_WIDTH_20_NOHT:
510                         phymode = MODE_11A;
511                         break;
512                 case NL80211_CHAN_WIDTH_20:
513                         phymode = MODE_11NA_HT20;
514                         break;
515                 case NL80211_CHAN_WIDTH_40:
516                         phymode = MODE_11NA_HT40;
517                         break;
518                 case NL80211_CHAN_WIDTH_80:
519                         phymode = MODE_11AC_VHT80;
520                         break;
521                 case NL80211_CHAN_WIDTH_5:
522                 case NL80211_CHAN_WIDTH_10:
523                 case NL80211_CHAN_WIDTH_80P80:
524                 case NL80211_CHAN_WIDTH_160:
525                         phymode = MODE_UNKNOWN;
526                         break;
527                 }
528                 break;
529         default:
530                 break;
531         }
532
533         WARN_ON(phymode == MODE_UNKNOWN);
534         return phymode;
535 }
536
537 static u8 ath10k_parse_mpdudensity(u8 mpdudensity)
538 {
539 /*
540  * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
541  *   0 for no restriction
542  *   1 for 1/4 us
543  *   2 for 1/2 us
544  *   3 for 1 us
545  *   4 for 2 us
546  *   5 for 4 us
547  *   6 for 8 us
548  *   7 for 16 us
549  */
550         switch (mpdudensity) {
551         case 0:
552                 return 0;
553         case 1:
554         case 2:
555         case 3:
556         /* Our lower layer calculations limit our precision to
557            1 microsecond */
558                 return 1;
559         case 4:
560                 return 2;
561         case 5:
562                 return 4;
563         case 6:
564                 return 8;
565         case 7:
566                 return 16;
567         default:
568                 return 0;
569         }
570 }
571
572 int ath10k_mac_vif_chan(struct ieee80211_vif *vif,
573                         struct cfg80211_chan_def *def)
574 {
575         struct ieee80211_chanctx_conf *conf;
576
577         rcu_read_lock();
578         conf = rcu_dereference(vif->chanctx_conf);
579         if (!conf) {
580                 rcu_read_unlock();
581                 return -ENOENT;
582         }
583
584         *def = conf->def;
585         rcu_read_unlock();
586
587         return 0;
588 }
589
590 static void ath10k_mac_num_chanctxs_iter(struct ieee80211_hw *hw,
591                                          struct ieee80211_chanctx_conf *conf,
592                                          void *data)
593 {
594         int *num = data;
595
596         (*num)++;
597 }
598
599 static int ath10k_mac_num_chanctxs(struct ath10k *ar)
600 {
601         int num = 0;
602
603         ieee80211_iter_chan_contexts_atomic(ar->hw,
604                                             ath10k_mac_num_chanctxs_iter,
605                                             &num);
606
607         return num;
608 }
609
610 static void
611 ath10k_mac_get_any_chandef_iter(struct ieee80211_hw *hw,
612                                 struct ieee80211_chanctx_conf *conf,
613                                 void *data)
614 {
615         struct cfg80211_chan_def **def = data;
616
617         *def = &conf->def;
618 }
619
620 static int ath10k_peer_create(struct ath10k *ar, u32 vdev_id, const u8 *addr,
621                               enum wmi_peer_type peer_type)
622 {
623         struct ath10k_vif *arvif;
624         int num_peers = 0;
625         int ret;
626
627         lockdep_assert_held(&ar->conf_mutex);
628
629         num_peers = ar->num_peers;
630
631         /* Each vdev consumes a peer entry as well */
632         list_for_each_entry(arvif, &ar->arvifs, list)
633                 num_peers++;
634
635         if (num_peers >= ar->max_num_peers)
636                 return -ENOBUFS;
637
638         ret = ath10k_wmi_peer_create(ar, vdev_id, addr, peer_type);
639         if (ret) {
640                 ath10k_warn(ar, "failed to create wmi peer %pM on vdev %i: %i\n",
641                             addr, vdev_id, ret);
642                 return ret;
643         }
644
645         ret = ath10k_wait_for_peer_created(ar, vdev_id, addr);
646         if (ret) {
647                 ath10k_warn(ar, "failed to wait for created wmi peer %pM on vdev %i: %i\n",
648                             addr, vdev_id, ret);
649                 return ret;
650         }
651
652         ar->num_peers++;
653
654         return 0;
655 }
656
657 static int ath10k_mac_set_kickout(struct ath10k_vif *arvif)
658 {
659         struct ath10k *ar = arvif->ar;
660         u32 param;
661         int ret;
662
663         param = ar->wmi.pdev_param->sta_kickout_th;
664         ret = ath10k_wmi_pdev_set_param(ar, param,
665                                         ATH10K_KICKOUT_THRESHOLD);
666         if (ret) {
667                 ath10k_warn(ar, "failed to set kickout threshold on vdev %i: %d\n",
668                             arvif->vdev_id, ret);
669                 return ret;
670         }
671
672         param = ar->wmi.vdev_param->ap_keepalive_min_idle_inactive_time_secs;
673         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
674                                         ATH10K_KEEPALIVE_MIN_IDLE);
675         if (ret) {
676                 ath10k_warn(ar, "failed to set keepalive minimum idle time on vdev %i: %d\n",
677                             arvif->vdev_id, ret);
678                 return ret;
679         }
680
681         param = ar->wmi.vdev_param->ap_keepalive_max_idle_inactive_time_secs;
682         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
683                                         ATH10K_KEEPALIVE_MAX_IDLE);
684         if (ret) {
685                 ath10k_warn(ar, "failed to set keepalive maximum idle time on vdev %i: %d\n",
686                             arvif->vdev_id, ret);
687                 return ret;
688         }
689
690         param = ar->wmi.vdev_param->ap_keepalive_max_unresponsive_time_secs;
691         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
692                                         ATH10K_KEEPALIVE_MAX_UNRESPONSIVE);
693         if (ret) {
694                 ath10k_warn(ar, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
695                             arvif->vdev_id, ret);
696                 return ret;
697         }
698
699         return 0;
700 }
701
702 static int ath10k_mac_set_rts(struct ath10k_vif *arvif, u32 value)
703 {
704         struct ath10k *ar = arvif->ar;
705         u32 vdev_param;
706
707         vdev_param = ar->wmi.vdev_param->rts_threshold;
708         return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
709 }
710
711 static int ath10k_peer_delete(struct ath10k *ar, u32 vdev_id, const u8 *addr)
712 {
713         int ret;
714
715         lockdep_assert_held(&ar->conf_mutex);
716
717         ret = ath10k_wmi_peer_delete(ar, vdev_id, addr);
718         if (ret)
719                 return ret;
720
721         ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
722         if (ret)
723                 return ret;
724
725         ar->num_peers--;
726
727         return 0;
728 }
729
730 static void ath10k_peer_cleanup(struct ath10k *ar, u32 vdev_id)
731 {
732         struct ath10k_peer *peer, *tmp;
733
734         lockdep_assert_held(&ar->conf_mutex);
735
736         spin_lock_bh(&ar->data_lock);
737         list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
738                 if (peer->vdev_id != vdev_id)
739                         continue;
740
741                 ath10k_warn(ar, "removing stale peer %pM from vdev_id %d\n",
742                             peer->addr, vdev_id);
743
744                 list_del(&peer->list);
745                 kfree(peer);
746                 ar->num_peers--;
747         }
748         spin_unlock_bh(&ar->data_lock);
749 }
750
751 static void ath10k_peer_cleanup_all(struct ath10k *ar)
752 {
753         struct ath10k_peer *peer, *tmp;
754
755         lockdep_assert_held(&ar->conf_mutex);
756
757         spin_lock_bh(&ar->data_lock);
758         list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
759                 list_del(&peer->list);
760                 kfree(peer);
761         }
762         spin_unlock_bh(&ar->data_lock);
763
764         ar->num_peers = 0;
765         ar->num_stations = 0;
766 }
767
768 static int ath10k_mac_tdls_peer_update(struct ath10k *ar, u32 vdev_id,
769                                        struct ieee80211_sta *sta,
770                                        enum wmi_tdls_peer_state state)
771 {
772         int ret;
773         struct wmi_tdls_peer_update_cmd_arg arg = {};
774         struct wmi_tdls_peer_capab_arg cap = {};
775         struct wmi_channel_arg chan_arg = {};
776
777         lockdep_assert_held(&ar->conf_mutex);
778
779         arg.vdev_id = vdev_id;
780         arg.peer_state = state;
781         ether_addr_copy(arg.addr, sta->addr);
782
783         cap.peer_max_sp = sta->max_sp;
784         cap.peer_uapsd_queues = sta->uapsd_queues;
785
786         if (state == WMI_TDLS_PEER_STATE_CONNECTED &&
787             !sta->tdls_initiator)
788                 cap.is_peer_responder = 1;
789
790         ret = ath10k_wmi_tdls_peer_update(ar, &arg, &cap, &chan_arg);
791         if (ret) {
792                 ath10k_warn(ar, "failed to update tdls peer %pM on vdev %i: %i\n",
793                             arg.addr, vdev_id, ret);
794                 return ret;
795         }
796
797         return 0;
798 }
799
800 /************************/
801 /* Interface management */
802 /************************/
803
804 void ath10k_mac_vif_beacon_free(struct ath10k_vif *arvif)
805 {
806         struct ath10k *ar = arvif->ar;
807
808         lockdep_assert_held(&ar->data_lock);
809
810         if (!arvif->beacon)
811                 return;
812
813         if (!arvif->beacon_buf)
814                 dma_unmap_single(ar->dev, ATH10K_SKB_CB(arvif->beacon)->paddr,
815                                  arvif->beacon->len, DMA_TO_DEVICE);
816
817         if (WARN_ON(arvif->beacon_state != ATH10K_BEACON_SCHEDULED &&
818                     arvif->beacon_state != ATH10K_BEACON_SENT))
819                 return;
820
821         dev_kfree_skb_any(arvif->beacon);
822
823         arvif->beacon = NULL;
824         arvif->beacon_state = ATH10K_BEACON_SCHEDULED;
825 }
826
827 static void ath10k_mac_vif_beacon_cleanup(struct ath10k_vif *arvif)
828 {
829         struct ath10k *ar = arvif->ar;
830
831         lockdep_assert_held(&ar->data_lock);
832
833         ath10k_mac_vif_beacon_free(arvif);
834
835         if (arvif->beacon_buf) {
836                 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
837                                   arvif->beacon_buf, arvif->beacon_paddr);
838                 arvif->beacon_buf = NULL;
839         }
840 }
841
842 static inline int ath10k_vdev_setup_sync(struct ath10k *ar)
843 {
844         unsigned long time_left;
845
846         lockdep_assert_held(&ar->conf_mutex);
847
848         if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags))
849                 return -ESHUTDOWN;
850
851         time_left = wait_for_completion_timeout(&ar->vdev_setup_done,
852                                                 ATH10K_VDEV_SETUP_TIMEOUT_HZ);
853         if (time_left == 0)
854                 return -ETIMEDOUT;
855
856         return 0;
857 }
858
859 static int ath10k_monitor_vdev_start(struct ath10k *ar, int vdev_id)
860 {
861         struct cfg80211_chan_def *chandef = NULL;
862         struct ieee80211_channel *channel = NULL;
863         struct wmi_vdev_start_request_arg arg = {};
864         int ret = 0;
865
866         lockdep_assert_held(&ar->conf_mutex);
867
868         ieee80211_iter_chan_contexts_atomic(ar->hw,
869                                             ath10k_mac_get_any_chandef_iter,
870                                             &chandef);
871         if (WARN_ON_ONCE(!chandef))
872                 return -ENOENT;
873
874         channel = chandef->chan;
875
876         arg.vdev_id = vdev_id;
877         arg.channel.freq = channel->center_freq;
878         arg.channel.band_center_freq1 = chandef->center_freq1;
879
880         /* TODO setup this dynamically, what in case we
881            don't have any vifs? */
882         arg.channel.mode = chan_to_phymode(chandef);
883         arg.channel.chan_radar =
884                         !!(channel->flags & IEEE80211_CHAN_RADAR);
885
886         arg.channel.min_power = 0;
887         arg.channel.max_power = channel->max_power * 2;
888         arg.channel.max_reg_power = channel->max_reg_power * 2;
889         arg.channel.max_antenna_gain = channel->max_antenna_gain * 2;
890
891         reinit_completion(&ar->vdev_setup_done);
892
893         ret = ath10k_wmi_vdev_start(ar, &arg);
894         if (ret) {
895                 ath10k_warn(ar, "failed to request monitor vdev %i start: %d\n",
896                             vdev_id, ret);
897                 return ret;
898         }
899
900         ret = ath10k_vdev_setup_sync(ar);
901         if (ret) {
902                 ath10k_warn(ar, "failed to synchronize setup for monitor vdev %i start: %d\n",
903                             vdev_id, ret);
904                 return ret;
905         }
906
907         ret = ath10k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
908         if (ret) {
909                 ath10k_warn(ar, "failed to put up monitor vdev %i: %d\n",
910                             vdev_id, ret);
911                 goto vdev_stop;
912         }
913
914         ar->monitor_vdev_id = vdev_id;
915
916         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i started\n",
917                    ar->monitor_vdev_id);
918         return 0;
919
920 vdev_stop:
921         ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
922         if (ret)
923                 ath10k_warn(ar, "failed to stop monitor vdev %i after start failure: %d\n",
924                             ar->monitor_vdev_id, ret);
925
926         return ret;
927 }
928
929 static int ath10k_monitor_vdev_stop(struct ath10k *ar)
930 {
931         int ret = 0;
932
933         lockdep_assert_held(&ar->conf_mutex);
934
935         ret = ath10k_wmi_vdev_down(ar, ar->monitor_vdev_id);
936         if (ret)
937                 ath10k_warn(ar, "failed to put down monitor vdev %i: %d\n",
938                             ar->monitor_vdev_id, ret);
939
940         reinit_completion(&ar->vdev_setup_done);
941
942         ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
943         if (ret)
944                 ath10k_warn(ar, "failed to to request monitor vdev %i stop: %d\n",
945                             ar->monitor_vdev_id, ret);
946
947         ret = ath10k_vdev_setup_sync(ar);
948         if (ret)
949                 ath10k_warn(ar, "failed to synchronize monitor vdev %i stop: %d\n",
950                             ar->monitor_vdev_id, ret);
951
952         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i stopped\n",
953                    ar->monitor_vdev_id);
954         return ret;
955 }
956
957 static int ath10k_monitor_vdev_create(struct ath10k *ar)
958 {
959         int bit, ret = 0;
960
961         lockdep_assert_held(&ar->conf_mutex);
962
963         if (ar->free_vdev_map == 0) {
964                 ath10k_warn(ar, "failed to find free vdev id for monitor vdev\n");
965                 return -ENOMEM;
966         }
967
968         bit = __ffs64(ar->free_vdev_map);
969
970         ar->monitor_vdev_id = bit;
971
972         ret = ath10k_wmi_vdev_create(ar, ar->monitor_vdev_id,
973                                      WMI_VDEV_TYPE_MONITOR,
974                                      0, ar->mac_addr);
975         if (ret) {
976                 ath10k_warn(ar, "failed to request monitor vdev %i creation: %d\n",
977                             ar->monitor_vdev_id, ret);
978                 return ret;
979         }
980
981         ar->free_vdev_map &= ~(1LL << ar->monitor_vdev_id);
982         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d created\n",
983                    ar->monitor_vdev_id);
984
985         return 0;
986 }
987
988 static int ath10k_monitor_vdev_delete(struct ath10k *ar)
989 {
990         int ret = 0;
991
992         lockdep_assert_held(&ar->conf_mutex);
993
994         ret = ath10k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
995         if (ret) {
996                 ath10k_warn(ar, "failed to request wmi monitor vdev %i removal: %d\n",
997                             ar->monitor_vdev_id, ret);
998                 return ret;
999         }
1000
1001         ar->free_vdev_map |= 1LL << ar->monitor_vdev_id;
1002
1003         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d deleted\n",
1004                    ar->monitor_vdev_id);
1005         return ret;
1006 }
1007
1008 static int ath10k_monitor_start(struct ath10k *ar)
1009 {
1010         int ret;
1011
1012         lockdep_assert_held(&ar->conf_mutex);
1013
1014         ret = ath10k_monitor_vdev_create(ar);
1015         if (ret) {
1016                 ath10k_warn(ar, "failed to create monitor vdev: %d\n", ret);
1017                 return ret;
1018         }
1019
1020         ret = ath10k_monitor_vdev_start(ar, ar->monitor_vdev_id);
1021         if (ret) {
1022                 ath10k_warn(ar, "failed to start monitor vdev: %d\n", ret);
1023                 ath10k_monitor_vdev_delete(ar);
1024                 return ret;
1025         }
1026
1027         ar->monitor_started = true;
1028         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor started\n");
1029
1030         return 0;
1031 }
1032
1033 static int ath10k_monitor_stop(struct ath10k *ar)
1034 {
1035         int ret;
1036
1037         lockdep_assert_held(&ar->conf_mutex);
1038
1039         ret = ath10k_monitor_vdev_stop(ar);
1040         if (ret) {
1041                 ath10k_warn(ar, "failed to stop monitor vdev: %d\n", ret);
1042                 return ret;
1043         }
1044
1045         ret = ath10k_monitor_vdev_delete(ar);
1046         if (ret) {
1047                 ath10k_warn(ar, "failed to delete monitor vdev: %d\n", ret);
1048                 return ret;
1049         }
1050
1051         ar->monitor_started = false;
1052         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopped\n");
1053
1054         return 0;
1055 }
1056
1057 static bool ath10k_mac_monitor_vdev_is_needed(struct ath10k *ar)
1058 {
1059         int num_ctx;
1060
1061         /* At least one chanctx is required to derive a channel to start
1062          * monitor vdev on.
1063          */
1064         num_ctx = ath10k_mac_num_chanctxs(ar);
1065         if (num_ctx == 0)
1066                 return false;
1067
1068         /* If there's already an existing special monitor interface then don't
1069          * bother creating another monitor vdev.
1070          */
1071         if (ar->monitor_arvif)
1072                 return false;
1073
1074         return ar->monitor ||
1075                ar->filter_flags & FIF_OTHER_BSS ||
1076                test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1077 }
1078
1079 static bool ath10k_mac_monitor_vdev_is_allowed(struct ath10k *ar)
1080 {
1081         int num_ctx;
1082
1083         num_ctx = ath10k_mac_num_chanctxs(ar);
1084
1085         /* FIXME: Current interface combinations and cfg80211/mac80211 code
1086          * shouldn't allow this but make sure to prevent handling the following
1087          * case anyway since multi-channel DFS hasn't been tested at all.
1088          */
1089         if (test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags) && num_ctx > 1)
1090                 return false;
1091
1092         return true;
1093 }
1094
1095 static int ath10k_monitor_recalc(struct ath10k *ar)
1096 {
1097         bool needed;
1098         bool allowed;
1099         int ret;
1100
1101         lockdep_assert_held(&ar->conf_mutex);
1102
1103         needed = ath10k_mac_monitor_vdev_is_needed(ar);
1104         allowed = ath10k_mac_monitor_vdev_is_allowed(ar);
1105
1106         ath10k_dbg(ar, ATH10K_DBG_MAC,
1107                    "mac monitor recalc started? %d needed? %d allowed? %d\n",
1108                    ar->monitor_started, needed, allowed);
1109
1110         if (WARN_ON(needed && !allowed)) {
1111                 if (ar->monitor_started) {
1112                         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopping disallowed monitor\n");
1113
1114                         ret = ath10k_monitor_stop(ar);
1115                         if (ret)
1116                                 ath10k_warn(ar, "failed to stop disallowed monitor: %d\n",
1117                                             ret);
1118                                 /* not serious */
1119                 }
1120
1121                 return -EPERM;
1122         }
1123
1124         if (needed == ar->monitor_started)
1125                 return 0;
1126
1127         if (needed)
1128                 return ath10k_monitor_start(ar);
1129         else
1130                 return ath10k_monitor_stop(ar);
1131 }
1132
1133 static int ath10k_recalc_rtscts_prot(struct ath10k_vif *arvif)
1134 {
1135         struct ath10k *ar = arvif->ar;
1136         u32 vdev_param, rts_cts = 0;
1137
1138         lockdep_assert_held(&ar->conf_mutex);
1139
1140         vdev_param = ar->wmi.vdev_param->enable_rtscts;
1141
1142         rts_cts |= SM(WMI_RTSCTS_ENABLED, WMI_RTSCTS_SET);
1143
1144         if (arvif->num_legacy_stations > 0)
1145                 rts_cts |= SM(WMI_RTSCTS_ACROSS_SW_RETRIES,
1146                               WMI_RTSCTS_PROFILE);
1147         else
1148                 rts_cts |= SM(WMI_RTSCTS_FOR_SECOND_RATESERIES,
1149                               WMI_RTSCTS_PROFILE);
1150
1151         return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
1152                                          rts_cts);
1153 }
1154
1155 static int ath10k_start_cac(struct ath10k *ar)
1156 {
1157         int ret;
1158
1159         lockdep_assert_held(&ar->conf_mutex);
1160
1161         set_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1162
1163         ret = ath10k_monitor_recalc(ar);
1164         if (ret) {
1165                 ath10k_warn(ar, "failed to start monitor (cac): %d\n", ret);
1166                 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1167                 return ret;
1168         }
1169
1170         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac start monitor vdev %d\n",
1171                    ar->monitor_vdev_id);
1172
1173         return 0;
1174 }
1175
1176 static int ath10k_stop_cac(struct ath10k *ar)
1177 {
1178         lockdep_assert_held(&ar->conf_mutex);
1179
1180         /* CAC is not running - do nothing */
1181         if (!test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags))
1182                 return 0;
1183
1184         clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1185         ath10k_monitor_stop(ar);
1186
1187         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac finished\n");
1188
1189         return 0;
1190 }
1191
1192 static void ath10k_mac_has_radar_iter(struct ieee80211_hw *hw,
1193                                       struct ieee80211_chanctx_conf *conf,
1194                                       void *data)
1195 {
1196         bool *ret = data;
1197
1198         if (!*ret && conf->radar_enabled)
1199                 *ret = true;
1200 }
1201
1202 static bool ath10k_mac_has_radar_enabled(struct ath10k *ar)
1203 {
1204         bool has_radar = false;
1205
1206         ieee80211_iter_chan_contexts_atomic(ar->hw,
1207                                             ath10k_mac_has_radar_iter,
1208                                             &has_radar);
1209
1210         return has_radar;
1211 }
1212
1213 static void ath10k_recalc_radar_detection(struct ath10k *ar)
1214 {
1215         int ret;
1216
1217         lockdep_assert_held(&ar->conf_mutex);
1218
1219         ath10k_stop_cac(ar);
1220
1221         if (!ath10k_mac_has_radar_enabled(ar))
1222                 return;
1223
1224         if (ar->num_started_vdevs > 0)
1225                 return;
1226
1227         ret = ath10k_start_cac(ar);
1228         if (ret) {
1229                 /*
1230                  * Not possible to start CAC on current channel so starting
1231                  * radiation is not allowed, make this channel DFS_UNAVAILABLE
1232                  * by indicating that radar was detected.
1233                  */
1234                 ath10k_warn(ar, "failed to start CAC: %d\n", ret);
1235                 ieee80211_radar_detected(ar->hw);
1236         }
1237 }
1238
1239 static int ath10k_vdev_stop(struct ath10k_vif *arvif)
1240 {
1241         struct ath10k *ar = arvif->ar;
1242         int ret;
1243
1244         lockdep_assert_held(&ar->conf_mutex);
1245
1246         reinit_completion(&ar->vdev_setup_done);
1247
1248         ret = ath10k_wmi_vdev_stop(ar, arvif->vdev_id);
1249         if (ret) {
1250                 ath10k_warn(ar, "failed to stop WMI vdev %i: %d\n",
1251                             arvif->vdev_id, ret);
1252                 return ret;
1253         }
1254
1255         ret = ath10k_vdev_setup_sync(ar);
1256         if (ret) {
1257                 ath10k_warn(ar, "failed to syncronise setup for vdev %i: %d\n",
1258                             arvif->vdev_id, ret);
1259                 return ret;
1260         }
1261
1262         WARN_ON(ar->num_started_vdevs == 0);
1263
1264         if (ar->num_started_vdevs != 0) {
1265                 ar->num_started_vdevs--;
1266                 ath10k_recalc_radar_detection(ar);
1267         }
1268
1269         return ret;
1270 }
1271
1272 static int ath10k_vdev_start_restart(struct ath10k_vif *arvif,
1273                                      const struct cfg80211_chan_def *chandef,
1274                                      bool restart)
1275 {
1276         struct ath10k *ar = arvif->ar;
1277         struct wmi_vdev_start_request_arg arg = {};
1278         int ret = 0;
1279
1280         lockdep_assert_held(&ar->conf_mutex);
1281
1282         reinit_completion(&ar->vdev_setup_done);
1283
1284         arg.vdev_id = arvif->vdev_id;
1285         arg.dtim_period = arvif->dtim_period;
1286         arg.bcn_intval = arvif->beacon_interval;
1287
1288         arg.channel.freq = chandef->chan->center_freq;
1289         arg.channel.band_center_freq1 = chandef->center_freq1;
1290         arg.channel.mode = chan_to_phymode(chandef);
1291
1292         arg.channel.min_power = 0;
1293         arg.channel.max_power = chandef->chan->max_power * 2;
1294         arg.channel.max_reg_power = chandef->chan->max_reg_power * 2;
1295         arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
1296
1297         if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
1298                 arg.ssid = arvif->u.ap.ssid;
1299                 arg.ssid_len = arvif->u.ap.ssid_len;
1300                 arg.hidden_ssid = arvif->u.ap.hidden_ssid;
1301
1302                 /* For now allow DFS for AP mode */
1303                 arg.channel.chan_radar =
1304                         !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
1305         } else if (arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
1306                 arg.ssid = arvif->vif->bss_conf.ssid;
1307                 arg.ssid_len = arvif->vif->bss_conf.ssid_len;
1308         }
1309
1310         ath10k_dbg(ar, ATH10K_DBG_MAC,
1311                    "mac vdev %d start center_freq %d phymode %s\n",
1312                    arg.vdev_id, arg.channel.freq,
1313                    ath10k_wmi_phymode_str(arg.channel.mode));
1314
1315         if (restart)
1316                 ret = ath10k_wmi_vdev_restart(ar, &arg);
1317         else
1318                 ret = ath10k_wmi_vdev_start(ar, &arg);
1319
1320         if (ret) {
1321                 ath10k_warn(ar, "failed to start WMI vdev %i: %d\n",
1322                             arg.vdev_id, ret);
1323                 return ret;
1324         }
1325
1326         ret = ath10k_vdev_setup_sync(ar);
1327         if (ret) {
1328                 ath10k_warn(ar,
1329                             "failed to synchronize setup for vdev %i restart %d: %d\n",
1330                             arg.vdev_id, restart, ret);
1331                 return ret;
1332         }
1333
1334         ar->num_started_vdevs++;
1335         ath10k_recalc_radar_detection(ar);
1336
1337         return ret;
1338 }
1339
1340 static int ath10k_vdev_start(struct ath10k_vif *arvif,
1341                              const struct cfg80211_chan_def *def)
1342 {
1343         return ath10k_vdev_start_restart(arvif, def, false);
1344 }
1345
1346 static int ath10k_vdev_restart(struct ath10k_vif *arvif,
1347                                const struct cfg80211_chan_def *def)
1348 {
1349         return ath10k_vdev_start_restart(arvif, def, true);
1350 }
1351
1352 static int ath10k_mac_setup_bcn_p2p_ie(struct ath10k_vif *arvif,
1353                                        struct sk_buff *bcn)
1354 {
1355         struct ath10k *ar = arvif->ar;
1356         struct ieee80211_mgmt *mgmt;
1357         const u8 *p2p_ie;
1358         int ret;
1359
1360         if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1361                 return 0;
1362
1363         if (arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
1364                 return 0;
1365
1366         mgmt = (void *)bcn->data;
1367         p2p_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1368                                          mgmt->u.beacon.variable,
1369                                          bcn->len - (mgmt->u.beacon.variable -
1370                                                      bcn->data));
1371         if (!p2p_ie)
1372                 return -ENOENT;
1373
1374         ret = ath10k_wmi_p2p_go_bcn_ie(ar, arvif->vdev_id, p2p_ie);
1375         if (ret) {
1376                 ath10k_warn(ar, "failed to submit p2p go bcn ie for vdev %i: %d\n",
1377                             arvif->vdev_id, ret);
1378                 return ret;
1379         }
1380
1381         return 0;
1382 }
1383
1384 static int ath10k_mac_remove_vendor_ie(struct sk_buff *skb, unsigned int oui,
1385                                        u8 oui_type, size_t ie_offset)
1386 {
1387         size_t len;
1388         const u8 *next;
1389         const u8 *end;
1390         u8 *ie;
1391
1392         if (WARN_ON(skb->len < ie_offset))
1393                 return -EINVAL;
1394
1395         ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
1396                                            skb->data + ie_offset,
1397                                            skb->len - ie_offset);
1398         if (!ie)
1399                 return -ENOENT;
1400
1401         len = ie[1] + 2;
1402         end = skb->data + skb->len;
1403         next = ie + len;
1404
1405         if (WARN_ON(next > end))
1406                 return -EINVAL;
1407
1408         memmove(ie, next, end - next);
1409         skb_trim(skb, skb->len - len);
1410
1411         return 0;
1412 }
1413
1414 static int ath10k_mac_setup_bcn_tmpl(struct ath10k_vif *arvif)
1415 {
1416         struct ath10k *ar = arvif->ar;
1417         struct ieee80211_hw *hw = ar->hw;
1418         struct ieee80211_vif *vif = arvif->vif;
1419         struct ieee80211_mutable_offsets offs = {};
1420         struct sk_buff *bcn;
1421         int ret;
1422
1423         if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1424                 return 0;
1425
1426         if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
1427             arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
1428                 return 0;
1429
1430         bcn = ieee80211_beacon_get_template(hw, vif, &offs);
1431         if (!bcn) {
1432                 ath10k_warn(ar, "failed to get beacon template from mac80211\n");
1433                 return -EPERM;
1434         }
1435
1436         ret = ath10k_mac_setup_bcn_p2p_ie(arvif, bcn);
1437         if (ret) {
1438                 ath10k_warn(ar, "failed to setup p2p go bcn ie: %d\n", ret);
1439                 kfree_skb(bcn);
1440                 return ret;
1441         }
1442
1443         /* P2P IE is inserted by firmware automatically (as configured above)
1444          * so remove it from the base beacon template to avoid duplicate P2P
1445          * IEs in beacon frames.
1446          */
1447         ath10k_mac_remove_vendor_ie(bcn, WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1448                                     offsetof(struct ieee80211_mgmt,
1449                                              u.beacon.variable));
1450
1451         ret = ath10k_wmi_bcn_tmpl(ar, arvif->vdev_id, offs.tim_offset, bcn, 0,
1452                                   0, NULL, 0);
1453         kfree_skb(bcn);
1454
1455         if (ret) {
1456                 ath10k_warn(ar, "failed to submit beacon template command: %d\n",
1457                             ret);
1458                 return ret;
1459         }
1460
1461         return 0;
1462 }
1463
1464 static int ath10k_mac_setup_prb_tmpl(struct ath10k_vif *arvif)
1465 {
1466         struct ath10k *ar = arvif->ar;
1467         struct ieee80211_hw *hw = ar->hw;
1468         struct ieee80211_vif *vif = arvif->vif;
1469         struct sk_buff *prb;
1470         int ret;
1471
1472         if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1473                 return 0;
1474
1475         if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1476                 return 0;
1477
1478         prb = ieee80211_proberesp_get(hw, vif);
1479         if (!prb) {
1480                 ath10k_warn(ar, "failed to get probe resp template from mac80211\n");
1481                 return -EPERM;
1482         }
1483
1484         ret = ath10k_wmi_prb_tmpl(ar, arvif->vdev_id, prb);
1485         kfree_skb(prb);
1486
1487         if (ret) {
1488                 ath10k_warn(ar, "failed to submit probe resp template command: %d\n",
1489                             ret);
1490                 return ret;
1491         }
1492
1493         return 0;
1494 }
1495
1496 static int ath10k_mac_vif_fix_hidden_ssid(struct ath10k_vif *arvif)
1497 {
1498         struct ath10k *ar = arvif->ar;
1499         struct cfg80211_chan_def def;
1500         int ret;
1501
1502         /* When originally vdev is started during assign_vif_chanctx() some
1503          * information is missing, notably SSID. Firmware revisions with beacon
1504          * offloading require the SSID to be provided during vdev (re)start to
1505          * handle hidden SSID properly.
1506          *
1507          * Vdev restart must be done after vdev has been both started and
1508          * upped. Otherwise some firmware revisions (at least 10.2) fail to
1509          * deliver vdev restart response event causing timeouts during vdev
1510          * syncing in ath10k.
1511          *
1512          * Note: The vdev down/up and template reinstallation could be skipped
1513          * since only wmi-tlv firmware are known to have beacon offload and
1514          * wmi-tlv doesn't seem to misbehave like 10.2 wrt vdev restart
1515          * response delivery. It's probably more robust to keep it as is.
1516          */
1517         if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1518                 return 0;
1519
1520         if (WARN_ON(!arvif->is_started))
1521                 return -EINVAL;
1522
1523         if (WARN_ON(!arvif->is_up))
1524                 return -EINVAL;
1525
1526         if (WARN_ON(ath10k_mac_vif_chan(arvif->vif, &def)))
1527                 return -EINVAL;
1528
1529         ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
1530         if (ret) {
1531                 ath10k_warn(ar, "failed to bring down ap vdev %i: %d\n",
1532                             arvif->vdev_id, ret);
1533                 return ret;
1534         }
1535
1536         /* Vdev down reset beacon & presp templates. Reinstall them. Otherwise
1537          * firmware will crash upon vdev up.
1538          */
1539
1540         ret = ath10k_mac_setup_bcn_tmpl(arvif);
1541         if (ret) {
1542                 ath10k_warn(ar, "failed to update beacon template: %d\n", ret);
1543                 return ret;
1544         }
1545
1546         ret = ath10k_mac_setup_prb_tmpl(arvif);
1547         if (ret) {
1548                 ath10k_warn(ar, "failed to update presp template: %d\n", ret);
1549                 return ret;
1550         }
1551
1552         ret = ath10k_vdev_restart(arvif, &def);
1553         if (ret) {
1554                 ath10k_warn(ar, "failed to restart ap vdev %i: %d\n",
1555                             arvif->vdev_id, ret);
1556                 return ret;
1557         }
1558
1559         ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
1560                                  arvif->bssid);
1561         if (ret) {
1562                 ath10k_warn(ar, "failed to bring up ap vdev %i: %d\n",
1563                             arvif->vdev_id, ret);
1564                 return ret;
1565         }
1566
1567         return 0;
1568 }
1569
1570 static void ath10k_control_beaconing(struct ath10k_vif *arvif,
1571                                      struct ieee80211_bss_conf *info)
1572 {
1573         struct ath10k *ar = arvif->ar;
1574         int ret = 0;
1575
1576         lockdep_assert_held(&arvif->ar->conf_mutex);
1577
1578         if (!info->enable_beacon) {
1579                 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
1580                 if (ret)
1581                         ath10k_warn(ar, "failed to down vdev_id %i: %d\n",
1582                                     arvif->vdev_id, ret);
1583
1584                 arvif->is_up = false;
1585
1586                 spin_lock_bh(&arvif->ar->data_lock);
1587                 ath10k_mac_vif_beacon_free(arvif);
1588                 spin_unlock_bh(&arvif->ar->data_lock);
1589
1590                 return;
1591         }
1592
1593         arvif->tx_seq_no = 0x1000;
1594
1595         arvif->aid = 0;
1596         ether_addr_copy(arvif->bssid, info->bssid);
1597
1598         ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
1599                                  arvif->bssid);
1600         if (ret) {
1601                 ath10k_warn(ar, "failed to bring up vdev %d: %i\n",
1602                             arvif->vdev_id, ret);
1603                 return;
1604         }
1605
1606         arvif->is_up = true;
1607
1608         ret = ath10k_mac_vif_fix_hidden_ssid(arvif);
1609         if (ret) {
1610                 ath10k_warn(ar, "failed to fix hidden ssid for vdev %i, expect trouble: %d\n",
1611                             arvif->vdev_id, ret);
1612                 return;
1613         }
1614
1615         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
1616 }
1617
1618 static void ath10k_control_ibss(struct ath10k_vif *arvif,
1619                                 struct ieee80211_bss_conf *info,
1620                                 const u8 self_peer[ETH_ALEN])
1621 {
1622         struct ath10k *ar = arvif->ar;
1623         u32 vdev_param;
1624         int ret = 0;
1625
1626         lockdep_assert_held(&arvif->ar->conf_mutex);
1627
1628         if (!info->ibss_joined) {
1629                 if (is_zero_ether_addr(arvif->bssid))
1630                         return;
1631
1632                 eth_zero_addr(arvif->bssid);
1633
1634                 return;
1635         }
1636
1637         vdev_param = arvif->ar->wmi.vdev_param->atim_window;
1638         ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id, vdev_param,
1639                                         ATH10K_DEFAULT_ATIM);
1640         if (ret)
1641                 ath10k_warn(ar, "failed to set IBSS ATIM for vdev %d: %d\n",
1642                             arvif->vdev_id, ret);
1643 }
1644
1645 static int ath10k_mac_vif_recalc_ps_wake_threshold(struct ath10k_vif *arvif)
1646 {
1647         struct ath10k *ar = arvif->ar;
1648         u32 param;
1649         u32 value;
1650         int ret;
1651
1652         lockdep_assert_held(&arvif->ar->conf_mutex);
1653
1654         if (arvif->u.sta.uapsd)
1655                 value = WMI_STA_PS_TX_WAKE_THRESHOLD_NEVER;
1656         else
1657                 value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
1658
1659         param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
1660         ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param, value);
1661         if (ret) {
1662                 ath10k_warn(ar, "failed to submit ps wake threshold %u on vdev %i: %d\n",
1663                             value, arvif->vdev_id, ret);
1664                 return ret;
1665         }
1666
1667         return 0;
1668 }
1669
1670 static int ath10k_mac_vif_recalc_ps_poll_count(struct ath10k_vif *arvif)
1671 {
1672         struct ath10k *ar = arvif->ar;
1673         u32 param;
1674         u32 value;
1675         int ret;
1676
1677         lockdep_assert_held(&arvif->ar->conf_mutex);
1678
1679         if (arvif->u.sta.uapsd)
1680                 value = WMI_STA_PS_PSPOLL_COUNT_UAPSD;
1681         else
1682                 value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
1683
1684         param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
1685         ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
1686                                           param, value);
1687         if (ret) {
1688                 ath10k_warn(ar, "failed to submit ps poll count %u on vdev %i: %d\n",
1689                             value, arvif->vdev_id, ret);
1690                 return ret;
1691         }
1692
1693         return 0;
1694 }
1695
1696 static int ath10k_mac_num_vifs_started(struct ath10k *ar)
1697 {
1698         struct ath10k_vif *arvif;
1699         int num = 0;
1700
1701         lockdep_assert_held(&ar->conf_mutex);
1702
1703         list_for_each_entry(arvif, &ar->arvifs, list)
1704                 if (arvif->is_started)
1705                         num++;
1706
1707         return num;
1708 }
1709
1710 static int ath10k_mac_vif_setup_ps(struct ath10k_vif *arvif)
1711 {
1712         struct ath10k *ar = arvif->ar;
1713         struct ieee80211_vif *vif = arvif->vif;
1714         struct ieee80211_conf *conf = &ar->hw->conf;
1715         enum wmi_sta_powersave_param param;
1716         enum wmi_sta_ps_mode psmode;
1717         int ret;
1718         int ps_timeout;
1719         bool enable_ps;
1720
1721         lockdep_assert_held(&arvif->ar->conf_mutex);
1722
1723         if (arvif->vif->type != NL80211_IFTYPE_STATION)
1724                 return 0;
1725
1726         enable_ps = arvif->ps;
1727
1728         if (enable_ps && ath10k_mac_num_vifs_started(ar) > 1 &&
1729             !test_bit(ATH10K_FW_FEATURE_MULTI_VIF_PS_SUPPORT,
1730                       ar->fw_features)) {
1731                 ath10k_warn(ar, "refusing to enable ps on vdev %i: not supported by fw\n",
1732                             arvif->vdev_id);
1733                 enable_ps = false;
1734         }
1735
1736         if (!arvif->is_started) {
1737                 /* mac80211 can update vif powersave state while disconnected.
1738                  * Firmware doesn't behave nicely and consumes more power than
1739                  * necessary if PS is disabled on a non-started vdev. Hence
1740                  * force-enable PS for non-running vdevs.
1741                  */
1742                 psmode = WMI_STA_PS_MODE_ENABLED;
1743         } else if (enable_ps) {
1744                 psmode = WMI_STA_PS_MODE_ENABLED;
1745                 param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
1746
1747                 ps_timeout = conf->dynamic_ps_timeout;
1748                 if (ps_timeout == 0) {
1749                         /* Firmware doesn't like 0 */
1750                         ps_timeout = ieee80211_tu_to_usec(
1751                                 vif->bss_conf.beacon_int) / 1000;
1752                 }
1753
1754                 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
1755                                                   ps_timeout);
1756                 if (ret) {
1757                         ath10k_warn(ar, "failed to set inactivity time for vdev %d: %i\n",
1758                                     arvif->vdev_id, ret);
1759                         return ret;
1760                 }
1761         } else {
1762                 psmode = WMI_STA_PS_MODE_DISABLED;
1763         }
1764
1765         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d psmode %s\n",
1766                    arvif->vdev_id, psmode ? "enable" : "disable");
1767
1768         ret = ath10k_wmi_set_psmode(ar, arvif->vdev_id, psmode);
1769         if (ret) {
1770                 ath10k_warn(ar, "failed to set PS Mode %d for vdev %d: %d\n",
1771                             psmode, arvif->vdev_id, ret);
1772                 return ret;
1773         }
1774
1775         return 0;
1776 }
1777
1778 static int ath10k_mac_vif_disable_keepalive(struct ath10k_vif *arvif)
1779 {
1780         struct ath10k *ar = arvif->ar;
1781         struct wmi_sta_keepalive_arg arg = {};
1782         int ret;
1783
1784         lockdep_assert_held(&arvif->ar->conf_mutex);
1785
1786         if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
1787                 return 0;
1788
1789         if (!test_bit(WMI_SERVICE_STA_KEEP_ALIVE, ar->wmi.svc_map))
1790                 return 0;
1791
1792         /* Some firmware revisions have a bug and ignore the `enabled` field.
1793          * Instead use the interval to disable the keepalive.
1794          */
1795         arg.vdev_id = arvif->vdev_id;
1796         arg.enabled = 1;
1797         arg.method = WMI_STA_KEEPALIVE_METHOD_NULL_FRAME;
1798         arg.interval = WMI_STA_KEEPALIVE_INTERVAL_DISABLE;
1799
1800         ret = ath10k_wmi_sta_keepalive(ar, &arg);
1801         if (ret) {
1802                 ath10k_warn(ar, "failed to submit keepalive on vdev %i: %d\n",
1803                             arvif->vdev_id, ret);
1804                 return ret;
1805         }
1806
1807         return 0;
1808 }
1809
1810 static void ath10k_mac_vif_ap_csa_count_down(struct ath10k_vif *arvif)
1811 {
1812         struct ath10k *ar = arvif->ar;
1813         struct ieee80211_vif *vif = arvif->vif;
1814         int ret;
1815
1816         lockdep_assert_held(&arvif->ar->conf_mutex);
1817
1818         if (WARN_ON(!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)))
1819                 return;
1820
1821         if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1822                 return;
1823
1824         if (!vif->csa_active)
1825                 return;
1826
1827         if (!arvif->is_up)
1828                 return;
1829
1830         if (!ieee80211_csa_is_complete(vif)) {
1831                 ieee80211_csa_update_counter(vif);
1832
1833                 ret = ath10k_mac_setup_bcn_tmpl(arvif);
1834                 if (ret)
1835                         ath10k_warn(ar, "failed to update bcn tmpl during csa: %d\n",
1836                                     ret);
1837
1838                 ret = ath10k_mac_setup_prb_tmpl(arvif);
1839                 if (ret)
1840                         ath10k_warn(ar, "failed to update prb tmpl during csa: %d\n",
1841                                     ret);
1842         } else {
1843                 ieee80211_csa_finish(vif);
1844         }
1845 }
1846
1847 static void ath10k_mac_vif_ap_csa_work(struct work_struct *work)
1848 {
1849         struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
1850                                                 ap_csa_work);
1851         struct ath10k *ar = arvif->ar;
1852
1853         mutex_lock(&ar->conf_mutex);
1854         ath10k_mac_vif_ap_csa_count_down(arvif);
1855         mutex_unlock(&ar->conf_mutex);
1856 }
1857
1858 static void ath10k_mac_handle_beacon_iter(void *data, u8 *mac,
1859                                           struct ieee80211_vif *vif)
1860 {
1861         struct sk_buff *skb = data;
1862         struct ieee80211_mgmt *mgmt = (void *)skb->data;
1863         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1864
1865         if (vif->type != NL80211_IFTYPE_STATION)
1866                 return;
1867
1868         if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
1869                 return;
1870
1871         cancel_delayed_work(&arvif->connection_loss_work);
1872 }
1873
1874 void ath10k_mac_handle_beacon(struct ath10k *ar, struct sk_buff *skb)
1875 {
1876         ieee80211_iterate_active_interfaces_atomic(ar->hw,
1877                                                    IEEE80211_IFACE_ITER_NORMAL,
1878                                                    ath10k_mac_handle_beacon_iter,
1879                                                    skb);
1880 }
1881
1882 static void ath10k_mac_handle_beacon_miss_iter(void *data, u8 *mac,
1883                                                struct ieee80211_vif *vif)
1884 {
1885         u32 *vdev_id = data;
1886         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1887         struct ath10k *ar = arvif->ar;
1888         struct ieee80211_hw *hw = ar->hw;
1889
1890         if (arvif->vdev_id != *vdev_id)
1891                 return;
1892
1893         if (!arvif->is_up)
1894                 return;
1895
1896         ieee80211_beacon_loss(vif);
1897
1898         /* Firmware doesn't report beacon loss events repeatedly. If AP probe
1899          * (done by mac80211) succeeds but beacons do not resume then it
1900          * doesn't make sense to continue operation. Queue connection loss work
1901          * which can be cancelled when beacon is received.
1902          */
1903         ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
1904                                      ATH10K_CONNECTION_LOSS_HZ);
1905 }
1906
1907 void ath10k_mac_handle_beacon_miss(struct ath10k *ar, u32 vdev_id)
1908 {
1909         ieee80211_iterate_active_interfaces_atomic(ar->hw,
1910                                                    IEEE80211_IFACE_ITER_NORMAL,
1911                                                    ath10k_mac_handle_beacon_miss_iter,
1912                                                    &vdev_id);
1913 }
1914
1915 static void ath10k_mac_vif_sta_connection_loss_work(struct work_struct *work)
1916 {
1917         struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
1918                                                 connection_loss_work.work);
1919         struct ieee80211_vif *vif = arvif->vif;
1920
1921         if (!arvif->is_up)
1922                 return;
1923
1924         ieee80211_connection_loss(vif);
1925 }
1926
1927 /**********************/
1928 /* Station management */
1929 /**********************/
1930
1931 static u32 ath10k_peer_assoc_h_listen_intval(struct ath10k *ar,
1932                                              struct ieee80211_vif *vif)
1933 {
1934         /* Some firmware revisions have unstable STA powersave when listen
1935          * interval is set too high (e.g. 5). The symptoms are firmware doesn't
1936          * generate NullFunc frames properly even if buffered frames have been
1937          * indicated in Beacon TIM. Firmware would seldom wake up to pull
1938          * buffered frames. Often pinging the device from AP would simply fail.
1939          *
1940          * As a workaround set it to 1.
1941          */
1942         if (vif->type == NL80211_IFTYPE_STATION)
1943                 return 1;
1944
1945         return ar->hw->conf.listen_interval;
1946 }
1947
1948 static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
1949                                       struct ieee80211_vif *vif,
1950                                       struct ieee80211_sta *sta,
1951                                       struct wmi_peer_assoc_complete_arg *arg)
1952 {
1953         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1954         u32 aid;
1955
1956         lockdep_assert_held(&ar->conf_mutex);
1957
1958         if (vif->type == NL80211_IFTYPE_STATION)
1959                 aid = vif->bss_conf.aid;
1960         else
1961                 aid = sta->aid;
1962
1963         ether_addr_copy(arg->addr, sta->addr);
1964         arg->vdev_id = arvif->vdev_id;
1965         arg->peer_aid = aid;
1966         arg->peer_flags |= arvif->ar->wmi.peer_flags->auth;
1967         arg->peer_listen_intval = ath10k_peer_assoc_h_listen_intval(ar, vif);
1968         arg->peer_num_spatial_streams = 1;
1969         arg->peer_caps = vif->bss_conf.assoc_capability;
1970 }
1971
1972 static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
1973                                        struct ieee80211_vif *vif,
1974                                        struct ieee80211_sta *sta,
1975                                        struct wmi_peer_assoc_complete_arg *arg)
1976 {
1977         struct ieee80211_bss_conf *info = &vif->bss_conf;
1978         struct cfg80211_chan_def def;
1979         struct cfg80211_bss *bss;
1980         const u8 *rsnie = NULL;
1981         const u8 *wpaie = NULL;
1982
1983         lockdep_assert_held(&ar->conf_mutex);
1984
1985         if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
1986                 return;
1987
1988         bss = cfg80211_get_bss(ar->hw->wiphy, def.chan, info->bssid, NULL, 0,
1989                                IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
1990         if (bss) {
1991                 const struct cfg80211_bss_ies *ies;
1992
1993                 rcu_read_lock();
1994                 rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
1995
1996                 ies = rcu_dereference(bss->ies);
1997
1998                 wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
1999                                                 WLAN_OUI_TYPE_MICROSOFT_WPA,
2000                                                 ies->data,
2001                                                 ies->len);
2002                 rcu_read_unlock();
2003                 cfg80211_put_bss(ar->hw->wiphy, bss);
2004         }
2005
2006         /* FIXME: base on RSN IE/WPA IE is a correct idea? */
2007         if (rsnie || wpaie) {
2008                 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
2009                 arg->peer_flags |= ar->wmi.peer_flags->need_ptk_4_way;
2010         }
2011
2012         if (wpaie) {
2013                 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: wpa ie found\n", __func__);
2014                 arg->peer_flags |= ar->wmi.peer_flags->need_gtk_2_way;
2015         }
2016
2017         if (sta->mfp &&
2018             test_bit(ATH10K_FW_FEATURE_MFP_SUPPORT, ar->fw_features)) {
2019                 arg->peer_flags |= ar->wmi.peer_flags->pmf;
2020         }
2021 }
2022
2023 static void ath10k_peer_assoc_h_rates(struct ath10k *ar,
2024                                       struct ieee80211_vif *vif,
2025                                       struct ieee80211_sta *sta,
2026                                       struct wmi_peer_assoc_complete_arg *arg)
2027 {
2028         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2029         struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
2030         struct cfg80211_chan_def def;
2031         const struct ieee80211_supported_band *sband;
2032         const struct ieee80211_rate *rates;
2033         enum ieee80211_band band;
2034         u32 ratemask;
2035         u8 rate;
2036         int i;
2037
2038         lockdep_assert_held(&ar->conf_mutex);
2039
2040         if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2041                 return;
2042
2043         band = def.chan->band;
2044         sband = ar->hw->wiphy->bands[band];
2045         ratemask = sta->supp_rates[band];
2046         ratemask &= arvif->bitrate_mask.control[band].legacy;
2047         rates = sband->bitrates;
2048
2049         rateset->num_rates = 0;
2050
2051         for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
2052                 if (!(ratemask & 1))
2053                         continue;
2054
2055                 rate = ath10k_mac_bitrate_to_rate(rates->bitrate);
2056                 rateset->rates[rateset->num_rates] = rate;
2057                 rateset->num_rates++;
2058         }
2059 }
2060
2061 static bool
2062 ath10k_peer_assoc_h_ht_masked(const u8 ht_mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
2063 {
2064         int nss;
2065
2066         for (nss = 0; nss < IEEE80211_HT_MCS_MASK_LEN; nss++)
2067                 if (ht_mcs_mask[nss])
2068                         return false;
2069
2070         return true;
2071 }
2072
2073 static bool
2074 ath10k_peer_assoc_h_vht_masked(const u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
2075 {
2076         int nss;
2077
2078         for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++)
2079                 if (vht_mcs_mask[nss])
2080                         return false;
2081
2082         return true;
2083 }
2084
2085 static void ath10k_peer_assoc_h_ht(struct ath10k *ar,
2086                                    struct ieee80211_vif *vif,
2087                                    struct ieee80211_sta *sta,
2088                                    struct wmi_peer_assoc_complete_arg *arg)
2089 {
2090         const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
2091         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2092         struct cfg80211_chan_def def;
2093         enum ieee80211_band band;
2094         const u8 *ht_mcs_mask;
2095         const u16 *vht_mcs_mask;
2096         int i, n;
2097         u8 max_nss;
2098         u32 stbc;
2099
2100         lockdep_assert_held(&ar->conf_mutex);
2101
2102         if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2103                 return;
2104
2105         if (!ht_cap->ht_supported)
2106                 return;
2107
2108         band = def.chan->band;
2109         ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2110         vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2111
2112         if (ath10k_peer_assoc_h_ht_masked(ht_mcs_mask) &&
2113             ath10k_peer_assoc_h_vht_masked(vht_mcs_mask))
2114                 return;
2115
2116         arg->peer_flags |= ar->wmi.peer_flags->ht;
2117         arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2118                                     ht_cap->ampdu_factor)) - 1;
2119
2120         arg->peer_mpdu_density =
2121                 ath10k_parse_mpdudensity(ht_cap->ampdu_density);
2122
2123         arg->peer_ht_caps = ht_cap->cap;
2124         arg->peer_rate_caps |= WMI_RC_HT_FLAG;
2125
2126         if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
2127                 arg->peer_flags |= ar->wmi.peer_flags->ldbc;
2128
2129         if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
2130                 arg->peer_flags |= ar->wmi.peer_flags->bw40;
2131                 arg->peer_rate_caps |= WMI_RC_CW40_FLAG;
2132         }
2133
2134         if (arvif->bitrate_mask.control[band].gi != NL80211_TXRATE_FORCE_LGI) {
2135                 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
2136                         arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
2137
2138                 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
2139                         arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
2140         }
2141
2142         if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
2143                 arg->peer_rate_caps |= WMI_RC_TX_STBC_FLAG;
2144                 arg->peer_flags |= ar->wmi.peer_flags->stbc;
2145         }
2146
2147         if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
2148                 stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
2149                 stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
2150                 stbc = stbc << WMI_RC_RX_STBC_FLAG_S;
2151                 arg->peer_rate_caps |= stbc;
2152                 arg->peer_flags |= ar->wmi.peer_flags->stbc;
2153         }
2154
2155         if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
2156                 arg->peer_rate_caps |= WMI_RC_TS_FLAG;
2157         else if (ht_cap->mcs.rx_mask[1])
2158                 arg->peer_rate_caps |= WMI_RC_DS_FLAG;
2159
2160         for (i = 0, n = 0, max_nss = 0; i < IEEE80211_HT_MCS_MASK_LEN * 8; i++)
2161                 if ((ht_cap->mcs.rx_mask[i / 8] & BIT(i % 8)) &&
2162                     (ht_mcs_mask[i / 8] & BIT(i % 8))) {
2163                         max_nss = (i / 8) + 1;
2164                         arg->peer_ht_rates.rates[n++] = i;
2165                 }
2166
2167         /*
2168          * This is a workaround for HT-enabled STAs which break the spec
2169          * and have no HT capabilities RX mask (no HT RX MCS map).
2170          *
2171          * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
2172          * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
2173          *
2174          * Firmware asserts if such situation occurs.
2175          */
2176         if (n == 0) {
2177                 arg->peer_ht_rates.num_rates = 8;
2178                 for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
2179                         arg->peer_ht_rates.rates[i] = i;
2180         } else {
2181                 arg->peer_ht_rates.num_rates = n;
2182                 arg->peer_num_spatial_streams = min(sta->rx_nss, max_nss);
2183         }
2184
2185         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
2186                    arg->addr,
2187                    arg->peer_ht_rates.num_rates,
2188                    arg->peer_num_spatial_streams);
2189 }
2190
2191 static int ath10k_peer_assoc_qos_ap(struct ath10k *ar,
2192                                     struct ath10k_vif *arvif,
2193                                     struct ieee80211_sta *sta)
2194 {
2195         u32 uapsd = 0;
2196         u32 max_sp = 0;
2197         int ret = 0;
2198
2199         lockdep_assert_held(&ar->conf_mutex);
2200
2201         if (sta->wme && sta->uapsd_queues) {
2202                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
2203                            sta->uapsd_queues, sta->max_sp);
2204
2205                 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
2206                         uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
2207                                  WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
2208                 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
2209                         uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
2210                                  WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
2211                 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
2212                         uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
2213                                  WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
2214                 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
2215                         uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
2216                                  WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
2217
2218                 if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
2219                         max_sp = sta->max_sp;
2220
2221                 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
2222                                                  sta->addr,
2223                                                  WMI_AP_PS_PEER_PARAM_UAPSD,
2224                                                  uapsd);
2225                 if (ret) {
2226                         ath10k_warn(ar, "failed to set ap ps peer param uapsd for vdev %i: %d\n",
2227                                     arvif->vdev_id, ret);
2228                         return ret;
2229                 }
2230
2231                 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
2232                                                  sta->addr,
2233                                                  WMI_AP_PS_PEER_PARAM_MAX_SP,
2234                                                  max_sp);
2235                 if (ret) {
2236                         ath10k_warn(ar, "failed to set ap ps peer param max sp for vdev %i: %d\n",
2237                                     arvif->vdev_id, ret);
2238                         return ret;
2239                 }
2240
2241                 /* TODO setup this based on STA listen interval and
2242                    beacon interval. Currently we don't know
2243                    sta->listen_interval - mac80211 patch required.
2244                    Currently use 10 seconds */
2245                 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id, sta->addr,
2246                                                  WMI_AP_PS_PEER_PARAM_AGEOUT_TIME,
2247                                                  10);
2248                 if (ret) {
2249                         ath10k_warn(ar, "failed to set ap ps peer param ageout time for vdev %i: %d\n",
2250                                     arvif->vdev_id, ret);
2251                         return ret;
2252                 }
2253         }
2254
2255         return 0;
2256 }
2257
2258 static u16
2259 ath10k_peer_assoc_h_vht_limit(u16 tx_mcs_set,
2260                               const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])
2261 {
2262         int idx_limit;
2263         int nss;
2264         u16 mcs_map;
2265         u16 mcs;
2266
2267         for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
2268                 mcs_map = ath10k_mac_get_max_vht_mcs_map(tx_mcs_set, nss) &
2269                           vht_mcs_limit[nss];
2270
2271                 if (mcs_map)
2272                         idx_limit = fls(mcs_map) - 1;
2273                 else
2274                         idx_limit = -1;
2275
2276                 switch (idx_limit) {
2277                 case 0: /* fall through */
2278                 case 1: /* fall through */
2279                 case 2: /* fall through */
2280                 case 3: /* fall through */
2281                 case 4: /* fall through */
2282                 case 5: /* fall through */
2283                 case 6: /* fall through */
2284                 default:
2285                         /* see ath10k_mac_can_set_bitrate_mask() */
2286                         WARN_ON(1);
2287                         /* fall through */
2288                 case -1:
2289                         mcs = IEEE80211_VHT_MCS_NOT_SUPPORTED;
2290                         break;
2291                 case 7:
2292                         mcs = IEEE80211_VHT_MCS_SUPPORT_0_7;
2293                         break;
2294                 case 8:
2295                         mcs = IEEE80211_VHT_MCS_SUPPORT_0_8;
2296                         break;
2297                 case 9:
2298                         mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
2299                         break;
2300                 }
2301
2302                 tx_mcs_set &= ~(0x3 << (nss * 2));
2303                 tx_mcs_set |= mcs << (nss * 2);
2304         }
2305
2306         return tx_mcs_set;
2307 }
2308
2309 static void ath10k_peer_assoc_h_vht(struct ath10k *ar,
2310                                     struct ieee80211_vif *vif,
2311                                     struct ieee80211_sta *sta,
2312                                     struct wmi_peer_assoc_complete_arg *arg)
2313 {
2314         const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
2315         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2316         struct cfg80211_chan_def def;
2317         enum ieee80211_band band;
2318         const u16 *vht_mcs_mask;
2319         u8 ampdu_factor;
2320
2321         if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2322                 return;
2323
2324         if (!vht_cap->vht_supported)
2325                 return;
2326
2327         band = def.chan->band;
2328         vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2329
2330         if (ath10k_peer_assoc_h_vht_masked(vht_mcs_mask))
2331                 return;
2332
2333         arg->peer_flags |= ar->wmi.peer_flags->vht;
2334
2335         if (def.chan->band == IEEE80211_BAND_2GHZ)
2336                 arg->peer_flags |= ar->wmi.peer_flags->vht_2g;
2337
2338         arg->peer_vht_caps = vht_cap->cap;
2339
2340         ampdu_factor = (vht_cap->cap &
2341                         IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
2342                        IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
2343
2344         /* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
2345          * zero in VHT IE. Using it would result in degraded throughput.
2346          * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
2347          * it if VHT max_mpdu is smaller. */
2348         arg->peer_max_mpdu = max(arg->peer_max_mpdu,
2349                                  (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2350                                         ampdu_factor)) - 1);
2351
2352         if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
2353                 arg->peer_flags |= ar->wmi.peer_flags->bw80;
2354
2355         arg->peer_vht_rates.rx_max_rate =
2356                 __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
2357         arg->peer_vht_rates.rx_mcs_set =
2358                 __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
2359         arg->peer_vht_rates.tx_max_rate =
2360                 __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
2361         arg->peer_vht_rates.tx_mcs_set = ath10k_peer_assoc_h_vht_limit(
2362                 __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map), vht_mcs_mask);
2363
2364         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
2365                    sta->addr, arg->peer_max_mpdu, arg->peer_flags);
2366 }
2367
2368 static void ath10k_peer_assoc_h_qos(struct ath10k *ar,
2369                                     struct ieee80211_vif *vif,
2370                                     struct ieee80211_sta *sta,
2371                                     struct wmi_peer_assoc_complete_arg *arg)
2372 {
2373         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2374
2375         switch (arvif->vdev_type) {
2376         case WMI_VDEV_TYPE_AP:
2377                 if (sta->wme)
2378                         arg->peer_flags |= arvif->ar->wmi.peer_flags->qos;
2379
2380                 if (sta->wme && sta->uapsd_queues) {
2381                         arg->peer_flags |= arvif->ar->wmi.peer_flags->apsd;
2382                         arg->peer_rate_caps |= WMI_RC_UAPSD_FLAG;
2383                 }
2384                 break;
2385         case WMI_VDEV_TYPE_STA:
2386                 if (vif->bss_conf.qos)
2387                         arg->peer_flags |= arvif->ar->wmi.peer_flags->qos;
2388                 break;
2389         case WMI_VDEV_TYPE_IBSS:
2390                 if (sta->wme)
2391                         arg->peer_flags |= arvif->ar->wmi.peer_flags->qos;
2392                 break;
2393         default:
2394                 break;
2395         }
2396
2397         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM qos %d\n",
2398                    sta->addr, !!(arg->peer_flags &
2399                    arvif->ar->wmi.peer_flags->qos));
2400 }
2401
2402 static bool ath10k_mac_sta_has_ofdm_only(struct ieee80211_sta *sta)
2403 {
2404         return sta->supp_rates[IEEE80211_BAND_2GHZ] >>
2405                ATH10K_MAC_FIRST_OFDM_RATE_IDX;
2406 }
2407
2408 static void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
2409                                         struct ieee80211_vif *vif,
2410                                         struct ieee80211_sta *sta,
2411                                         struct wmi_peer_assoc_complete_arg *arg)
2412 {
2413         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2414         struct cfg80211_chan_def def;
2415         enum ieee80211_band band;
2416         const u8 *ht_mcs_mask;
2417         const u16 *vht_mcs_mask;
2418         enum wmi_phy_mode phymode = MODE_UNKNOWN;
2419
2420         if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2421                 return;
2422
2423         band = def.chan->band;
2424         ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2425         vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2426
2427         switch (band) {
2428         case IEEE80211_BAND_2GHZ:
2429                 if (sta->vht_cap.vht_supported &&
2430                     !ath10k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
2431                         if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2432                                 phymode = MODE_11AC_VHT40;
2433                         else
2434                                 phymode = MODE_11AC_VHT20;
2435                 } else if (sta->ht_cap.ht_supported &&
2436                            !ath10k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
2437                         if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2438                                 phymode = MODE_11NG_HT40;
2439                         else
2440                                 phymode = MODE_11NG_HT20;
2441                 } else if (ath10k_mac_sta_has_ofdm_only(sta)) {
2442                         phymode = MODE_11G;
2443                 } else {
2444                         phymode = MODE_11B;
2445                 }
2446
2447                 break;
2448         case IEEE80211_BAND_5GHZ:
2449                 /*
2450                  * Check VHT first.
2451                  */
2452                 if (sta->vht_cap.vht_supported &&
2453                     !ath10k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
2454                         if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
2455                                 phymode = MODE_11AC_VHT80;
2456                         else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2457                                 phymode = MODE_11AC_VHT40;
2458                         else if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
2459                                 phymode = MODE_11AC_VHT20;
2460                 } else if (sta->ht_cap.ht_supported &&
2461                            !ath10k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
2462                         if (sta->bandwidth >= IEEE80211_STA_RX_BW_40)
2463                                 phymode = MODE_11NA_HT40;
2464                         else
2465                                 phymode = MODE_11NA_HT20;
2466                 } else {
2467                         phymode = MODE_11A;
2468                 }
2469
2470                 break;
2471         default:
2472                 break;
2473         }
2474
2475         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM phymode %s\n",
2476                    sta->addr, ath10k_wmi_phymode_str(phymode));
2477
2478         arg->peer_phymode = phymode;
2479         WARN_ON(phymode == MODE_UNKNOWN);
2480 }
2481
2482 static int ath10k_peer_assoc_prepare(struct ath10k *ar,
2483                                      struct ieee80211_vif *vif,
2484                                      struct ieee80211_sta *sta,
2485                                      struct wmi_peer_assoc_complete_arg *arg)
2486 {
2487         lockdep_assert_held(&ar->conf_mutex);
2488
2489         memset(arg, 0, sizeof(*arg));
2490
2491         ath10k_peer_assoc_h_basic(ar, vif, sta, arg);
2492         ath10k_peer_assoc_h_crypto(ar, vif, sta, arg);
2493         ath10k_peer_assoc_h_rates(ar, vif, sta, arg);
2494         ath10k_peer_assoc_h_ht(ar, vif, sta, arg);
2495         ath10k_peer_assoc_h_vht(ar, vif, sta, arg);
2496         ath10k_peer_assoc_h_qos(ar, vif, sta, arg);
2497         ath10k_peer_assoc_h_phymode(ar, vif, sta, arg);
2498
2499         return 0;
2500 }
2501
2502 static const u32 ath10k_smps_map[] = {
2503         [WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
2504         [WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
2505         [WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
2506         [WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
2507 };
2508
2509 static int ath10k_setup_peer_smps(struct ath10k *ar, struct ath10k_vif *arvif,
2510                                   const u8 *addr,
2511                                   const struct ieee80211_sta_ht_cap *ht_cap)
2512 {
2513         int smps;
2514
2515         if (!ht_cap->ht_supported)
2516                 return 0;
2517
2518         smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
2519         smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
2520
2521         if (smps >= ARRAY_SIZE(ath10k_smps_map))
2522                 return -EINVAL;
2523
2524         return ath10k_wmi_peer_set_param(ar, arvif->vdev_id, addr,
2525                                          WMI_PEER_SMPS_STATE,
2526                                          ath10k_smps_map[smps]);
2527 }
2528
2529 static int ath10k_mac_vif_recalc_txbf(struct ath10k *ar,
2530                                       struct ieee80211_vif *vif,
2531                                       struct ieee80211_sta_vht_cap vht_cap)
2532 {
2533         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2534         int ret;
2535         u32 param;
2536         u32 value;
2537
2538         if (ath10k_wmi_get_txbf_conf_scheme(ar) != WMI_TXBF_CONF_AFTER_ASSOC)
2539                 return 0;
2540
2541         if (!(ar->vht_cap_info &
2542               (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
2543                IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
2544                IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
2545                IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)))
2546                 return 0;
2547
2548         param = ar->wmi.vdev_param->txbf;
2549         value = 0;
2550
2551         if (WARN_ON(param == WMI_VDEV_PARAM_UNSUPPORTED))
2552                 return 0;
2553
2554         /* The following logic is correct. If a remote STA advertises support
2555          * for being a beamformer then we should enable us being a beamformee.
2556          */
2557
2558         if (ar->vht_cap_info &
2559             (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
2560              IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)) {
2561                 if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)
2562                         value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
2563
2564                 if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)
2565                         value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
2566         }
2567
2568         if (ar->vht_cap_info &
2569             (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
2570              IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)) {
2571                 if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)
2572                         value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
2573
2574                 if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)
2575                         value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
2576         }
2577
2578         if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFEE)
2579                 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
2580
2581         if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFER)
2582                 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
2583
2584         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param, value);
2585         if (ret) {
2586                 ath10k_warn(ar, "failed to submit vdev param txbf 0x%x: %d\n",
2587                             value, ret);
2588                 return ret;
2589         }
2590
2591         return 0;
2592 }
2593
2594 /* can be called only in mac80211 callbacks due to `key_count` usage */
2595 static void ath10k_bss_assoc(struct ieee80211_hw *hw,
2596                              struct ieee80211_vif *vif,
2597                              struct ieee80211_bss_conf *bss_conf)
2598 {
2599         struct ath10k *ar = hw->priv;
2600         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2601         struct ieee80211_sta_ht_cap ht_cap;
2602         struct ieee80211_sta_vht_cap vht_cap;
2603         struct wmi_peer_assoc_complete_arg peer_arg;
2604         struct ieee80211_sta *ap_sta;
2605         int ret;
2606
2607         lockdep_assert_held(&ar->conf_mutex);
2608
2609         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
2610                    arvif->vdev_id, arvif->bssid, arvif->aid);
2611
2612         rcu_read_lock();
2613
2614         ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
2615         if (!ap_sta) {
2616                 ath10k_warn(ar, "failed to find station entry for bss %pM vdev %i\n",
2617                             bss_conf->bssid, arvif->vdev_id);
2618                 rcu_read_unlock();
2619                 return;
2620         }
2621
2622         /* ap_sta must be accessed only within rcu section which must be left
2623          * before calling ath10k_setup_peer_smps() which might sleep. */
2624         ht_cap = ap_sta->ht_cap;
2625         vht_cap = ap_sta->vht_cap;
2626
2627         ret = ath10k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg);
2628         if (ret) {
2629                 ath10k_warn(ar, "failed to prepare peer assoc for %pM vdev %i: %d\n",
2630                             bss_conf->bssid, arvif->vdev_id, ret);
2631                 rcu_read_unlock();
2632                 return;
2633         }
2634
2635         rcu_read_unlock();
2636
2637         ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
2638         if (ret) {
2639                 ath10k_warn(ar, "failed to run peer assoc for %pM vdev %i: %d\n",
2640                             bss_conf->bssid, arvif->vdev_id, ret);
2641                 return;
2642         }
2643
2644         ret = ath10k_setup_peer_smps(ar, arvif, bss_conf->bssid, &ht_cap);
2645         if (ret) {
2646                 ath10k_warn(ar, "failed to setup peer SMPS for vdev %i: %d\n",
2647                             arvif->vdev_id, ret);
2648                 return;
2649         }
2650
2651         ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
2652         if (ret) {
2653                 ath10k_warn(ar, "failed to recalc txbf for vdev %i on bss %pM: %d\n",
2654                             arvif->vdev_id, bss_conf->bssid, ret);
2655                 return;
2656         }
2657
2658         ath10k_dbg(ar, ATH10K_DBG_MAC,
2659                    "mac vdev %d up (associated) bssid %pM aid %d\n",
2660                    arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
2661
2662         WARN_ON(arvif->is_up);
2663
2664         arvif->aid = bss_conf->aid;
2665         ether_addr_copy(arvif->bssid, bss_conf->bssid);
2666
2667         ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
2668         if (ret) {
2669                 ath10k_warn(ar, "failed to set vdev %d up: %d\n",
2670                             arvif->vdev_id, ret);
2671                 return;
2672         }
2673
2674         arvif->is_up = true;
2675
2676         /* Workaround: Some firmware revisions (tested with qca6174
2677          * WLAN.RM.2.0-00073) have buggy powersave state machine and must be
2678          * poked with peer param command.
2679          */
2680         ret = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, arvif->bssid,
2681                                         WMI_PEER_DUMMY_VAR, 1);
2682         if (ret) {
2683                 ath10k_warn(ar, "failed to poke peer %pM param for ps workaround on vdev %i: %d\n",
2684                             arvif->bssid, arvif->vdev_id, ret);
2685                 return;
2686         }
2687 }
2688
2689 static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
2690                                 struct ieee80211_vif *vif)
2691 {
2692         struct ath10k *ar = hw->priv;
2693         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2694         struct ieee80211_sta_vht_cap vht_cap = {};
2695         int ret;
2696
2697         lockdep_assert_held(&ar->conf_mutex);
2698
2699         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
2700                    arvif->vdev_id, arvif->bssid);
2701
2702         ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
2703         if (ret)
2704                 ath10k_warn(ar, "faield to down vdev %i: %d\n",
2705                             arvif->vdev_id, ret);
2706
2707         arvif->def_wep_key_idx = -1;
2708
2709         ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
2710         if (ret) {
2711                 ath10k_warn(ar, "failed to recalc txbf for vdev %i: %d\n",
2712                             arvif->vdev_id, ret);
2713                 return;
2714         }
2715
2716         arvif->is_up = false;
2717
2718         cancel_delayed_work_sync(&arvif->connection_loss_work);
2719 }
2720
2721 static int ath10k_station_assoc(struct ath10k *ar,
2722                                 struct ieee80211_vif *vif,
2723                                 struct ieee80211_sta *sta,
2724                                 bool reassoc)
2725 {
2726         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2727         struct wmi_peer_assoc_complete_arg peer_arg;
2728         int ret = 0;
2729
2730         lockdep_assert_held(&ar->conf_mutex);
2731
2732         ret = ath10k_peer_assoc_prepare(ar, vif, sta, &peer_arg);
2733         if (ret) {
2734                 ath10k_warn(ar, "failed to prepare WMI peer assoc for %pM vdev %i: %i\n",
2735                             sta->addr, arvif->vdev_id, ret);
2736                 return ret;
2737         }
2738
2739         ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
2740         if (ret) {
2741                 ath10k_warn(ar, "failed to run peer assoc for STA %pM vdev %i: %d\n",
2742                             sta->addr, arvif->vdev_id, ret);
2743                 return ret;
2744         }
2745
2746         /* Re-assoc is run only to update supported rates for given station. It
2747          * doesn't make much sense to reconfigure the peer completely.
2748          */
2749         if (!reassoc) {
2750                 ret = ath10k_setup_peer_smps(ar, arvif, sta->addr,
2751                                              &sta->ht_cap);
2752                 if (ret) {
2753                         ath10k_warn(ar, "failed to setup peer SMPS for vdev %d: %d\n",
2754                                     arvif->vdev_id, ret);
2755                         return ret;
2756                 }
2757
2758                 ret = ath10k_peer_assoc_qos_ap(ar, arvif, sta);
2759                 if (ret) {
2760                         ath10k_warn(ar, "failed to set qos params for STA %pM for vdev %i: %d\n",
2761                                     sta->addr, arvif->vdev_id, ret);
2762                         return ret;
2763                 }
2764
2765                 if (!sta->wme) {
2766                         arvif->num_legacy_stations++;
2767                         ret  = ath10k_recalc_rtscts_prot(arvif);
2768                         if (ret) {
2769                                 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
2770                                             arvif->vdev_id, ret);
2771                                 return ret;
2772                         }
2773                 }
2774
2775                 /* Plumb cached keys only for static WEP */
2776                 if (arvif->def_wep_key_idx != -1) {
2777                         ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
2778                         if (ret) {
2779                                 ath10k_warn(ar, "failed to install peer wep keys for vdev %i: %d\n",
2780                                             arvif->vdev_id, ret);
2781                                 return ret;
2782                         }
2783                 }
2784         }
2785
2786         return ret;
2787 }
2788
2789 static int ath10k_station_disassoc(struct ath10k *ar,
2790                                    struct ieee80211_vif *vif,
2791                                    struct ieee80211_sta *sta)
2792 {
2793         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2794         int ret = 0;
2795
2796         lockdep_assert_held(&ar->conf_mutex);
2797
2798         if (!sta->wme) {
2799                 arvif->num_legacy_stations--;
2800                 ret = ath10k_recalc_rtscts_prot(arvif);
2801                 if (ret) {
2802                         ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
2803                                     arvif->vdev_id, ret);
2804                         return ret;
2805                 }
2806         }
2807
2808         ret = ath10k_clear_peer_keys(arvif, sta->addr);
2809         if (ret) {
2810                 ath10k_warn(ar, "failed to clear all peer wep keys for vdev %i: %d\n",
2811                             arvif->vdev_id, ret);
2812                 return ret;
2813         }
2814
2815         return ret;
2816 }
2817
2818 /**************/
2819 /* Regulatory */
2820 /**************/
2821
2822 static int ath10k_update_channel_list(struct ath10k *ar)
2823 {
2824         struct ieee80211_hw *hw = ar->hw;
2825         struct ieee80211_supported_band **bands;
2826         enum ieee80211_band band;
2827         struct ieee80211_channel *channel;
2828         struct wmi_scan_chan_list_arg arg = {0};
2829         struct wmi_channel_arg *ch;
2830         bool passive;
2831         int len;
2832         int ret;
2833         int i;
2834
2835         lockdep_assert_held(&ar->conf_mutex);
2836
2837         bands = hw->wiphy->bands;
2838         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2839                 if (!bands[band])
2840                         continue;
2841
2842                 for (i = 0; i < bands[band]->n_channels; i++) {
2843                         if (bands[band]->channels[i].flags &
2844                             IEEE80211_CHAN_DISABLED)
2845                                 continue;
2846
2847                         arg.n_channels++;
2848                 }
2849         }
2850
2851         len = sizeof(struct wmi_channel_arg) * arg.n_channels;
2852         arg.channels = kzalloc(len, GFP_KERNEL);
2853         if (!arg.channels)
2854                 return -ENOMEM;
2855
2856         ch = arg.channels;
2857         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2858                 if (!bands[band])
2859                         continue;
2860
2861                 for (i = 0; i < bands[band]->n_channels; i++) {
2862                         channel = &bands[band]->channels[i];
2863
2864                         if (channel->flags & IEEE80211_CHAN_DISABLED)
2865                                 continue;
2866
2867                         ch->allow_ht   = true;
2868
2869                         /* FIXME: when should we really allow VHT? */
2870                         ch->allow_vht = true;
2871
2872                         ch->allow_ibss =
2873                                 !(channel->flags & IEEE80211_CHAN_NO_IR);
2874
2875                         ch->ht40plus =
2876                                 !(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
2877
2878                         ch->chan_radar =
2879                                 !!(channel->flags & IEEE80211_CHAN_RADAR);
2880
2881                         passive = channel->flags & IEEE80211_CHAN_NO_IR;
2882                         ch->passive = passive;
2883
2884                         ch->freq = channel->center_freq;
2885                         ch->band_center_freq1 = channel->center_freq;
2886                         ch->min_power = 0;
2887                         ch->max_power = channel->max_power * 2;
2888                         ch->max_reg_power = channel->max_reg_power * 2;
2889                         ch->max_antenna_gain = channel->max_antenna_gain * 2;
2890                         ch->reg_class_id = 0; /* FIXME */
2891
2892                         /* FIXME: why use only legacy modes, why not any
2893                          * HT/VHT modes? Would that even make any
2894                          * difference? */
2895                         if (channel->band == IEEE80211_BAND_2GHZ)
2896                                 ch->mode = MODE_11G;
2897                         else
2898                                 ch->mode = MODE_11A;
2899
2900                         if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
2901                                 continue;
2902
2903                         ath10k_dbg(ar, ATH10K_DBG_WMI,
2904                                    "mac channel [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
2905                                     ch - arg.channels, arg.n_channels,
2906                                    ch->freq, ch->max_power, ch->max_reg_power,
2907                                    ch->max_antenna_gain, ch->mode);
2908
2909                         ch++;
2910                 }
2911         }
2912
2913         ret = ath10k_wmi_scan_chan_list(ar, &arg);
2914         kfree(arg.channels);
2915
2916         return ret;
2917 }
2918
2919 static enum wmi_dfs_region
2920 ath10k_mac_get_dfs_region(enum nl80211_dfs_regions dfs_region)
2921 {
2922         switch (dfs_region) {
2923         case NL80211_DFS_UNSET:
2924                 return WMI_UNINIT_DFS_DOMAIN;
2925         case NL80211_DFS_FCC:
2926                 return WMI_FCC_DFS_DOMAIN;
2927         case NL80211_DFS_ETSI:
2928                 return WMI_ETSI_DFS_DOMAIN;
2929         case NL80211_DFS_JP:
2930                 return WMI_MKK4_DFS_DOMAIN;
2931         }
2932         return WMI_UNINIT_DFS_DOMAIN;
2933 }
2934
2935 static void ath10k_regd_update(struct ath10k *ar)
2936 {
2937         struct reg_dmn_pair_mapping *regpair;
2938         int ret;
2939         enum wmi_dfs_region wmi_dfs_reg;
2940         enum nl80211_dfs_regions nl_dfs_reg;
2941
2942         lockdep_assert_held(&ar->conf_mutex);
2943
2944         ret = ath10k_update_channel_list(ar);
2945         if (ret)
2946                 ath10k_warn(ar, "failed to update channel list: %d\n", ret);
2947
2948         regpair = ar->ath_common.regulatory.regpair;
2949
2950         if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
2951                 nl_dfs_reg = ar->dfs_detector->region;
2952                 wmi_dfs_reg = ath10k_mac_get_dfs_region(nl_dfs_reg);
2953         } else {
2954                 wmi_dfs_reg = WMI_UNINIT_DFS_DOMAIN;
2955         }
2956
2957         /* Target allows setting up per-band regdomain but ath_common provides
2958          * a combined one only */
2959         ret = ath10k_wmi_pdev_set_regdomain(ar,
2960                                             regpair->reg_domain,
2961                                             regpair->reg_domain, /* 2ghz */
2962                                             regpair->reg_domain, /* 5ghz */
2963                                             regpair->reg_2ghz_ctl,
2964                                             regpair->reg_5ghz_ctl,
2965                                             wmi_dfs_reg);
2966         if (ret)
2967                 ath10k_warn(ar, "failed to set pdev regdomain: %d\n", ret);
2968 }
2969
2970 static void ath10k_reg_notifier(struct wiphy *wiphy,
2971                                 struct regulatory_request *request)
2972 {
2973         struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
2974         struct ath10k *ar = hw->priv;
2975         bool result;
2976
2977         ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
2978
2979         if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
2980                 ath10k_dbg(ar, ATH10K_DBG_REGULATORY, "dfs region 0x%x\n",
2981                            request->dfs_region);
2982                 result = ar->dfs_detector->set_dfs_domain(ar->dfs_detector,
2983                                                           request->dfs_region);
2984                 if (!result)
2985                         ath10k_warn(ar, "DFS region 0x%X not supported, will trigger radar for every pulse\n",
2986                                     request->dfs_region);
2987         }
2988
2989         mutex_lock(&ar->conf_mutex);
2990         if (ar->state == ATH10K_STATE_ON)
2991                 ath10k_regd_update(ar);
2992         mutex_unlock(&ar->conf_mutex);
2993 }
2994
2995 /***************/
2996 /* TX handlers */
2997 /***************/
2998
2999 void ath10k_mac_tx_lock(struct ath10k *ar, int reason)
3000 {
3001         lockdep_assert_held(&ar->htt.tx_lock);
3002
3003         WARN_ON(reason >= ATH10K_TX_PAUSE_MAX);
3004         ar->tx_paused |= BIT(reason);
3005         ieee80211_stop_queues(ar->hw);
3006 }
3007
3008 static void ath10k_mac_tx_unlock_iter(void *data, u8 *mac,
3009                                       struct ieee80211_vif *vif)
3010 {
3011         struct ath10k *ar = data;
3012         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3013
3014         if (arvif->tx_paused)
3015                 return;
3016
3017         ieee80211_wake_queue(ar->hw, arvif->vdev_id);
3018 }
3019
3020 void ath10k_mac_tx_unlock(struct ath10k *ar, int reason)
3021 {
3022         lockdep_assert_held(&ar->htt.tx_lock);
3023
3024         WARN_ON(reason >= ATH10K_TX_PAUSE_MAX);
3025         ar->tx_paused &= ~BIT(reason);
3026
3027         if (ar->tx_paused)
3028                 return;
3029
3030         ieee80211_iterate_active_interfaces_atomic(ar->hw,
3031                                                    IEEE80211_IFACE_ITER_RESUME_ALL,
3032                                                    ath10k_mac_tx_unlock_iter,
3033                                                    ar);
3034
3035         ieee80211_wake_queue(ar->hw, ar->hw->offchannel_tx_hw_queue);
3036 }
3037
3038 void ath10k_mac_vif_tx_lock(struct ath10k_vif *arvif, int reason)
3039 {
3040         struct ath10k *ar = arvif->ar;
3041
3042         lockdep_assert_held(&ar->htt.tx_lock);
3043
3044         WARN_ON(reason >= BITS_PER_LONG);
3045         arvif->tx_paused |= BIT(reason);
3046         ieee80211_stop_queue(ar->hw, arvif->vdev_id);
3047 }
3048
3049 void ath10k_mac_vif_tx_unlock(struct ath10k_vif *arvif, int reason)
3050 {
3051         struct ath10k *ar = arvif->ar;
3052
3053         lockdep_assert_held(&ar->htt.tx_lock);
3054
3055         WARN_ON(reason >= BITS_PER_LONG);
3056         arvif->tx_paused &= ~BIT(reason);
3057
3058         if (ar->tx_paused)
3059                 return;
3060
3061         if (arvif->tx_paused)
3062                 return;
3063
3064         ieee80211_wake_queue(ar->hw, arvif->vdev_id);
3065 }
3066
3067 static void ath10k_mac_vif_handle_tx_pause(struct ath10k_vif *arvif,
3068                                            enum wmi_tlv_tx_pause_id pause_id,
3069                                            enum wmi_tlv_tx_pause_action action)
3070 {
3071         struct ath10k *ar = arvif->ar;
3072
3073         lockdep_assert_held(&ar->htt.tx_lock);
3074
3075         switch (action) {
3076         case WMI_TLV_TX_PAUSE_ACTION_STOP:
3077                 ath10k_mac_vif_tx_lock(arvif, pause_id);
3078                 break;
3079         case WMI_TLV_TX_PAUSE_ACTION_WAKE:
3080                 ath10k_mac_vif_tx_unlock(arvif, pause_id);
3081                 break;
3082         default:
3083                 ath10k_warn(ar, "received unknown tx pause action %d on vdev %i, ignoring\n",
3084                             action, arvif->vdev_id);
3085                 break;
3086         }
3087 }
3088
3089 struct ath10k_mac_tx_pause {
3090         u32 vdev_id;
3091         enum wmi_tlv_tx_pause_id pause_id;
3092         enum wmi_tlv_tx_pause_action action;
3093 };
3094
3095 static void ath10k_mac_handle_tx_pause_iter(void *data, u8 *mac,
3096                                             struct ieee80211_vif *vif)
3097 {
3098         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3099         struct ath10k_mac_tx_pause *arg = data;
3100
3101         if (arvif->vdev_id != arg->vdev_id)
3102                 return;
3103
3104         ath10k_mac_vif_handle_tx_pause(arvif, arg->pause_id, arg->action);
3105 }
3106
3107 void ath10k_mac_handle_tx_pause_vdev(struct ath10k *ar, u32 vdev_id,
3108                                      enum wmi_tlv_tx_pause_id pause_id,
3109                                      enum wmi_tlv_tx_pause_action action)
3110 {
3111         struct ath10k_mac_tx_pause arg = {
3112                 .vdev_id = vdev_id,
3113                 .pause_id = pause_id,
3114                 .action = action,
3115         };
3116
3117         spin_lock_bh(&ar->htt.tx_lock);
3118         ieee80211_iterate_active_interfaces_atomic(ar->hw,
3119                                                    IEEE80211_IFACE_ITER_RESUME_ALL,
3120                                                    ath10k_mac_handle_tx_pause_iter,
3121                                                    &arg);
3122         spin_unlock_bh(&ar->htt.tx_lock);
3123 }
3124
3125 static enum ath10k_hw_txrx_mode
3126 ath10k_mac_tx_h_get_txmode(struct ath10k *ar,
3127                            struct ieee80211_vif *vif,
3128                            struct ieee80211_sta *sta,
3129                            struct sk_buff *skb)
3130 {
3131         const struct ieee80211_hdr *hdr = (void *)skb->data;
3132         __le16 fc = hdr->frame_control;
3133
3134         if (!vif || vif->type == NL80211_IFTYPE_MONITOR)
3135                 return ATH10K_HW_TXRX_RAW;
3136
3137         if (ieee80211_is_mgmt(fc))
3138                 return ATH10K_HW_TXRX_MGMT;
3139
3140         /* Workaround:
3141          *
3142          * NullFunc frames are mostly used to ping if a client or AP are still
3143          * reachable and responsive. This implies tx status reports must be
3144          * accurate - otherwise either mac80211 or userspace (e.g. hostapd) can
3145          * come to a conclusion that the other end disappeared and tear down
3146          * BSS connection or it can never disconnect from BSS/client (which is
3147          * the case).
3148          *
3149          * Firmware with HTT older than 3.0 delivers incorrect tx status for
3150          * NullFunc frames to driver. However there's a HTT Mgmt Tx command
3151          * which seems to deliver correct tx reports for NullFunc frames. The
3152          * downside of using it is it ignores client powersave state so it can
3153          * end up disconnecting sleeping clients in AP mode. It should fix STA
3154          * mode though because AP don't sleep.
3155          */
3156         if (ar->htt.target_version_major < 3 &&
3157             (ieee80211_is_nullfunc(fc) || ieee80211_is_qos_nullfunc(fc)) &&
3158             !test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX, ar->fw_features))
3159                 return ATH10K_HW_TXRX_MGMT;
3160
3161         /* Workaround:
3162          *
3163          * Some wmi-tlv firmwares for qca6174 have broken Tx key selection for
3164          * NativeWifi txmode - it selects AP key instead of peer key. It seems
3165          * to work with Ethernet txmode so use it.
3166          *
3167          * FIXME: Check if raw mode works with TDLS.
3168          */
3169         if (ieee80211_is_data_present(fc) && sta && sta->tdls)
3170                 return ATH10K_HW_TXRX_ETHERNET;
3171
3172         if (test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
3173                 return ATH10K_HW_TXRX_RAW;
3174
3175         return ATH10K_HW_TXRX_NATIVE_WIFI;
3176 }
3177
3178 static bool ath10k_tx_h_use_hwcrypto(struct ieee80211_vif *vif,
3179                                      struct sk_buff *skb)
3180 {
3181         const struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3182         const struct ieee80211_hdr *hdr = (void *)skb->data;
3183         const u32 mask = IEEE80211_TX_INTFL_DONT_ENCRYPT |
3184                          IEEE80211_TX_CTL_INJECTED;
3185
3186         if (!ieee80211_has_protected(hdr->frame_control))
3187                 return false;
3188
3189         if ((info->flags & mask) == mask)
3190                 return false;
3191
3192         if (vif)
3193                 return !ath10k_vif_to_arvif(vif)->nohwcrypt;
3194
3195         return true;
3196 }
3197
3198 /* HTT Tx uses Native Wifi tx mode which expects 802.11 frames without QoS
3199  * Control in the header.
3200  */
3201 static void ath10k_tx_h_nwifi(struct ieee80211_hw *hw, struct sk_buff *skb)
3202 {
3203         struct ieee80211_hdr *hdr = (void *)skb->data;
3204         struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
3205         u8 *qos_ctl;
3206
3207         if (!ieee80211_is_data_qos(hdr->frame_control))
3208                 return;
3209
3210         qos_ctl = ieee80211_get_qos_ctl(hdr);
3211         memmove(skb->data + IEEE80211_QOS_CTL_LEN,
3212                 skb->data, (void *)qos_ctl - (void *)skb->data);
3213         skb_pull(skb, IEEE80211_QOS_CTL_LEN);
3214
3215         /* Some firmware revisions don't handle sending QoS NullFunc well.
3216          * These frames are mainly used for CQM purposes so it doesn't really
3217          * matter whether QoS NullFunc or NullFunc are sent.
3218          */
3219         hdr = (void *)skb->data;
3220         if (ieee80211_is_qos_nullfunc(hdr->frame_control))
3221                 cb->flags &= ~ATH10K_SKB_F_QOS;
3222
3223         hdr->frame_control &= ~__cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
3224 }
3225
3226 static void ath10k_tx_h_8023(struct sk_buff *skb)
3227 {
3228         struct ieee80211_hdr *hdr;
3229         struct rfc1042_hdr *rfc1042;
3230         struct ethhdr *eth;
3231         size_t hdrlen;
3232         u8 da[ETH_ALEN];
3233         u8 sa[ETH_ALEN];
3234         __be16 type;
3235
3236         hdr = (void *)skb->data;
3237         hdrlen = ieee80211_hdrlen(hdr->frame_control);
3238         rfc1042 = (void *)skb->data + hdrlen;
3239
3240         ether_addr_copy(da, ieee80211_get_DA(hdr));
3241         ether_addr_copy(sa, ieee80211_get_SA(hdr));
3242         type = rfc1042->snap_type;
3243
3244         skb_pull(skb, hdrlen + sizeof(*rfc1042));
3245         skb_push(skb, sizeof(*eth));
3246
3247         eth = (void *)skb->data;
3248         ether_addr_copy(eth->h_dest, da);
3249         ether_addr_copy(eth->h_source, sa);
3250         eth->h_proto = type;
3251 }
3252
3253 static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar,
3254                                        struct ieee80211_vif *vif,
3255                                        struct sk_buff *skb)
3256 {
3257         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
3258         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3259
3260         /* This is case only for P2P_GO */
3261         if (arvif->vdev_type != WMI_VDEV_TYPE_AP ||
3262             arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
3263                 return;
3264
3265         if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
3266                 spin_lock_bh(&ar->data_lock);
3267                 if (arvif->u.ap.noa_data)
3268                         if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
3269                                               GFP_ATOMIC))
3270                                 memcpy(skb_put(skb, arvif->u.ap.noa_len),
3271                                        arvif->u.ap.noa_data,
3272                                        arvif->u.ap.noa_len);
3273                 spin_unlock_bh(&ar->data_lock);
3274         }
3275 }
3276
3277 bool ath10k_mac_tx_frm_has_freq(struct ath10k *ar)
3278 {
3279         /* FIXME: Not really sure since when the behaviour changed. At some
3280          * point new firmware stopped requiring creation of peer entries for
3281          * offchannel tx (and actually creating them causes issues with wmi-htc
3282          * tx credit replenishment and reliability). Assuming it's at least 3.4
3283          * because that's when the `freq` was introduced to TX_FRM HTT command.
3284          */
3285         return (ar->htt.target_version_major >= 3 &&
3286                 ar->htt.target_version_minor >= 4 &&
3287                 ar->htt.op_version == ATH10K_FW_HTT_OP_VERSION_TLV);
3288 }
3289
3290 static int ath10k_mac_tx_wmi_mgmt(struct ath10k *ar, struct sk_buff *skb)
3291 {
3292         struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
3293         int ret = 0;
3294
3295         spin_lock_bh(&ar->data_lock);
3296
3297         if (skb_queue_len(q) == ATH10K_MAX_NUM_MGMT_PENDING) {
3298                 ath10k_warn(ar, "wmi mgmt tx queue is full\n");
3299                 ret = -ENOSPC;
3300                 goto unlock;
3301         }
3302
3303         __skb_queue_tail(q, skb);
3304         ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
3305
3306 unlock:
3307         spin_unlock_bh(&ar->data_lock);
3308
3309         return ret;
3310 }
3311
3312 static void ath10k_mac_tx(struct ath10k *ar, enum ath10k_hw_txrx_mode txmode,
3313                           struct sk_buff *skb)
3314 {
3315         struct ath10k_htt *htt = &ar->htt;
3316         int ret = 0;
3317
3318         switch (txmode) {
3319         case ATH10K_HW_TXRX_RAW:
3320         case ATH10K_HW_TXRX_NATIVE_WIFI:
3321         case ATH10K_HW_TXRX_ETHERNET:
3322                 ret = ath10k_htt_tx(htt, txmode, skb);
3323                 break;
3324         case ATH10K_HW_TXRX_MGMT:
3325                 if (test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
3326                              ar->fw_features))
3327                         ret = ath10k_mac_tx_wmi_mgmt(ar, skb);
3328                 else if (ar->htt.target_version_major >= 3)
3329                         ret = ath10k_htt_tx(htt, txmode, skb);
3330                 else
3331                         ret = ath10k_htt_mgmt_tx(htt, skb);
3332                 break;
3333         }
3334
3335         if (ret) {
3336                 ath10k_warn(ar, "failed to transmit packet, dropping: %d\n",
3337                             ret);
3338                 ieee80211_free_txskb(ar->hw, skb);
3339         }
3340 }
3341
3342 void ath10k_offchan_tx_purge(struct ath10k *ar)
3343 {
3344         struct sk_buff *skb;
3345
3346         for (;;) {
3347                 skb = skb_dequeue(&ar->offchan_tx_queue);
3348                 if (!skb)
3349                         break;
3350
3351                 ieee80211_free_txskb(ar->hw, skb);
3352         }
3353 }
3354
3355 void ath10k_offchan_tx_work(struct work_struct *work)
3356 {
3357         struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
3358         struct ath10k_peer *peer;
3359         struct ath10k_vif *arvif;
3360         struct ieee80211_hdr *hdr;
3361         struct ieee80211_vif *vif;
3362         struct ieee80211_sta *sta;
3363         struct sk_buff *skb;
3364         const u8 *peer_addr;
3365         enum ath10k_hw_txrx_mode txmode;
3366         int vdev_id;
3367         int ret;
3368         unsigned long time_left;
3369         bool tmp_peer_created = false;
3370
3371         /* FW requirement: We must create a peer before FW will send out
3372          * an offchannel frame. Otherwise the frame will be stuck and
3373          * never transmitted. We delete the peer upon tx completion.
3374          * It is unlikely that a peer for offchannel tx will already be
3375          * present. However it may be in some rare cases so account for that.
3376          * Otherwise we might remove a legitimate peer and break stuff. */
3377
3378         for (;;) {
3379                 skb = skb_dequeue(&ar->offchan_tx_queue);
3380                 if (!skb)
3381                         break;
3382
3383                 mutex_lock(&ar->conf_mutex);
3384
3385                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac offchannel skb %p\n",
3386                            skb);
3387
3388                 hdr = (struct ieee80211_hdr *)skb->data;
3389                 peer_addr = ieee80211_get_DA(hdr);
3390
3391                 spin_lock_bh(&ar->data_lock);
3392                 vdev_id = ar->scan.vdev_id;
3393                 peer = ath10k_peer_find(ar, vdev_id, peer_addr);
3394                 spin_unlock_bh(&ar->data_lock);
3395
3396                 if (peer)
3397                         /* FIXME: should this use ath10k_warn()? */
3398                         ath10k_dbg(ar, ATH10K_DBG_MAC, "peer %pM on vdev %d already present\n",
3399                                    peer_addr, vdev_id);
3400
3401                 if (!peer) {
3402                         ret = ath10k_peer_create(ar, vdev_id, peer_addr,
3403                                                  WMI_PEER_TYPE_DEFAULT);
3404                         if (ret)
3405                                 ath10k_warn(ar, "failed to create peer %pM on vdev %d: %d\n",
3406                                             peer_addr, vdev_id, ret);
3407                         tmp_peer_created = (ret == 0);
3408                 }
3409
3410                 spin_lock_bh(&ar->data_lock);
3411                 reinit_completion(&ar->offchan_tx_completed);
3412                 ar->offchan_tx_skb = skb;
3413                 spin_unlock_bh(&ar->data_lock);
3414
3415                 /* It's safe to access vif and sta - conf_mutex guarantees that
3416                  * sta_state() and remove_interface() are locked exclusively
3417                  * out wrt to this offchannel worker.
3418                  */
3419                 arvif = ath10k_get_arvif(ar, vdev_id);
3420                 if (arvif) {
3421                         vif = arvif->vif;
3422                         sta = ieee80211_find_sta(vif, peer_addr);
3423                 } else {
3424                         vif = NULL;
3425                         sta = NULL;
3426                 }
3427
3428                 txmode = ath10k_mac_tx_h_get_txmode(ar, vif, sta, skb);
3429
3430                 ath10k_mac_tx(ar, txmode, skb);
3431
3432                 time_left =
3433                 wait_for_completion_timeout(&ar->offchan_tx_completed, 3 * HZ);
3434                 if (time_left == 0)
3435                         ath10k_warn(ar, "timed out waiting for offchannel skb %p\n",
3436                                     skb);
3437
3438                 if (!peer && tmp_peer_created) {
3439                         ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
3440                         if (ret)
3441                                 ath10k_warn(ar, "failed to delete peer %pM on vdev %d: %d\n",
3442                                             peer_addr, vdev_id, ret);
3443                 }
3444
3445                 mutex_unlock(&ar->conf_mutex);
3446         }
3447 }
3448
3449 void ath10k_mgmt_over_wmi_tx_purge(struct ath10k *ar)
3450 {
3451         struct sk_buff *skb;
3452
3453         for (;;) {
3454                 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
3455                 if (!skb)
3456                         break;
3457
3458                 ieee80211_free_txskb(ar->hw, skb);
3459         }
3460 }
3461
3462 void ath10k_mgmt_over_wmi_tx_work(struct work_struct *work)
3463 {
3464         struct ath10k *ar = container_of(work, struct ath10k, wmi_mgmt_tx_work);
3465         struct sk_buff *skb;
3466         int ret;
3467
3468         for (;;) {
3469                 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
3470                 if (!skb)
3471                         break;
3472
3473                 ret = ath10k_wmi_mgmt_tx(ar, skb);
3474                 if (ret) {
3475                         ath10k_warn(ar, "failed to transmit management frame via WMI: %d\n",
3476                                     ret);
3477                         ieee80211_free_txskb(ar->hw, skb);
3478                 }
3479         }
3480 }
3481
3482 /************/
3483 /* Scanning */
3484 /************/
3485
3486 void __ath10k_scan_finish(struct ath10k *ar)
3487 {
3488         lockdep_assert_held(&ar->data_lock);
3489
3490         switch (ar->scan.state) {
3491         case ATH10K_SCAN_IDLE:
3492                 break;
3493         case ATH10K_SCAN_RUNNING:
3494         case ATH10K_SCAN_ABORTING:
3495                 if (!ar->scan.is_roc)
3496                         ieee80211_scan_completed(ar->hw,
3497                                                  (ar->scan.state ==
3498                                                   ATH10K_SCAN_ABORTING));
3499                 else if (ar->scan.roc_notify)
3500                         ieee80211_remain_on_channel_expired(ar->hw);
3501                 /* fall through */
3502         case ATH10K_SCAN_STARTING:
3503                 ar->scan.state = ATH10K_SCAN_IDLE;
3504                 ar->scan_channel = NULL;
3505                 ar->scan.roc_freq = 0;
3506                 ath10k_offchan_tx_purge(ar);
3507                 cancel_delayed_work(&ar->scan.timeout);
3508                 complete_all(&ar->scan.completed);
3509                 break;
3510         }
3511 }
3512
3513 void ath10k_scan_finish(struct ath10k *ar)
3514 {
3515         spin_lock_bh(&ar->data_lock);
3516         __ath10k_scan_finish(ar);
3517         spin_unlock_bh(&ar->data_lock);
3518 }
3519
3520 static int ath10k_scan_stop(struct ath10k *ar)
3521 {
3522         struct wmi_stop_scan_arg arg = {
3523                 .req_id = 1, /* FIXME */
3524                 .req_type = WMI_SCAN_STOP_ONE,
3525                 .u.scan_id = ATH10K_SCAN_ID,
3526         };
3527         int ret;
3528
3529         lockdep_assert_held(&ar->conf_mutex);
3530
3531         ret = ath10k_wmi_stop_scan(ar, &arg);
3532         if (ret) {
3533                 ath10k_warn(ar, "failed to stop wmi scan: %d\n", ret);
3534                 goto out;
3535         }
3536
3537         ret = wait_for_completion_timeout(&ar->scan.completed, 3*HZ);
3538         if (ret == 0) {
3539                 ath10k_warn(ar, "failed to receive scan abortion completion: timed out\n");
3540                 ret = -ETIMEDOUT;
3541         } else if (ret > 0) {
3542                 ret = 0;
3543         }
3544
3545 out:
3546         /* Scan state should be updated upon scan completion but in case
3547          * firmware fails to deliver the event (for whatever reason) it is
3548          * desired to clean up scan state anyway. Firmware may have just
3549          * dropped the scan completion event delivery due to transport pipe
3550          * being overflown with data and/or it can recover on its own before
3551          * next scan request is submitted.
3552          */
3553         spin_lock_bh(&ar->data_lock);
3554         if (ar->scan.state != ATH10K_SCAN_IDLE)
3555                 __ath10k_scan_finish(ar);
3556         spin_unlock_bh(&ar->data_lock);
3557
3558         return ret;
3559 }
3560
3561 static void ath10k_scan_abort(struct ath10k *ar)
3562 {
3563         int ret;
3564
3565         lockdep_assert_held(&ar->conf_mutex);
3566
3567         spin_lock_bh(&ar->data_lock);
3568
3569         switch (ar->scan.state) {
3570         case ATH10K_SCAN_IDLE:
3571                 /* This can happen if timeout worker kicked in and called
3572                  * abortion while scan completion was being processed.
3573                  */
3574                 break;
3575         case ATH10K_SCAN_STARTING:
3576         case ATH10K_SCAN_ABORTING:
3577                 ath10k_warn(ar, "refusing scan abortion due to invalid scan state: %s (%d)\n",
3578                             ath10k_scan_state_str(ar->scan.state),
3579                             ar->scan.state);
3580                 break;
3581         case ATH10K_SCAN_RUNNING:
3582                 ar->scan.state = ATH10K_SCAN_ABORTING;
3583                 spin_unlock_bh(&ar->data_lock);
3584
3585                 ret = ath10k_scan_stop(ar);
3586                 if (ret)
3587                         ath10k_warn(ar, "failed to abort scan: %d\n", ret);
3588
3589                 spin_lock_bh(&ar->data_lock);
3590                 break;
3591         }
3592
3593         spin_unlock_bh(&ar->data_lock);
3594 }
3595
3596 void ath10k_scan_timeout_work(struct work_struct *work)
3597 {
3598         struct ath10k *ar = container_of(work, struct ath10k,
3599                                          scan.timeout.work);
3600
3601         mutex_lock(&ar->conf_mutex);
3602         ath10k_scan_abort(ar);
3603         mutex_unlock(&ar->conf_mutex);
3604 }
3605
3606 static int ath10k_start_scan(struct ath10k *ar,
3607                              const struct wmi_start_scan_arg *arg)
3608 {
3609         int ret;
3610
3611         lockdep_assert_held(&ar->conf_mutex);
3612
3613         ret = ath10k_wmi_start_scan(ar, arg);
3614         if (ret)
3615                 return ret;
3616
3617         ret = wait_for_completion_timeout(&ar->scan.started, 1*HZ);
3618         if (ret == 0) {
3619                 ret = ath10k_scan_stop(ar);
3620                 if (ret)
3621                         ath10k_warn(ar, "failed to stop scan: %d\n", ret);
3622
3623                 return -ETIMEDOUT;
3624         }
3625
3626         /* If we failed to start the scan, return error code at
3627          * this point.  This is probably due to some issue in the
3628          * firmware, but no need to wedge the driver due to that...
3629          */
3630         spin_lock_bh(&ar->data_lock);
3631         if (ar->scan.state == ATH10K_SCAN_IDLE) {
3632                 spin_unlock_bh(&ar->data_lock);
3633                 return -EINVAL;
3634         }
3635         spin_unlock_bh(&ar->data_lock);
3636
3637         return 0;
3638 }
3639
3640 /**********************/
3641 /* mac80211 callbacks */
3642 /**********************/
3643
3644 static void ath10k_tx(struct ieee80211_hw *hw,
3645                       struct ieee80211_tx_control *control,
3646                       struct sk_buff *skb)
3647 {
3648         struct ath10k *ar = hw->priv;
3649         struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(skb);
3650         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3651         struct ieee80211_vif *vif = info->control.vif;
3652         struct ieee80211_sta *sta = control->sta;
3653         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
3654         enum ath10k_hw_txrx_mode txmode;
3655
3656         /* We should disable CCK RATE due to P2P */
3657         if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
3658                 ath10k_dbg(ar, ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
3659
3660         txmode = ath10k_mac_tx_h_get_txmode(ar, vif, sta, skb);
3661
3662         skb_cb->flags = 0;
3663         if (!ath10k_tx_h_use_hwcrypto(vif, skb))
3664                 skb_cb->flags |= ATH10K_SKB_F_NO_HWCRYPT;
3665
3666         if (ieee80211_is_mgmt(hdr->frame_control))
3667                 skb_cb->flags |= ATH10K_SKB_F_MGMT;
3668
3669         if (ieee80211_is_data_qos(hdr->frame_control))
3670                 skb_cb->flags |= ATH10K_SKB_F_QOS;
3671
3672         skb_cb->vif = vif;
3673
3674         switch (txmode) {
3675         case ATH10K_HW_TXRX_MGMT:
3676         case ATH10K_HW_TXRX_NATIVE_WIFI:
3677                 ath10k_tx_h_nwifi(hw, skb);
3678                 ath10k_tx_h_add_p2p_noa_ie(ar, vif, skb);
3679                 ath10k_tx_h_seq_no(vif, skb);
3680                 break;
3681         case ATH10K_HW_TXRX_ETHERNET:
3682                 ath10k_tx_h_8023(skb);
3683                 break;
3684         case ATH10K_HW_TXRX_RAW:
3685                 if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags)) {
3686                         WARN_ON_ONCE(1);
3687                         ieee80211_free_txskb(hw, skb);
3688                         return;
3689                 }
3690         }
3691
3692         if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
3693                 if (!ath10k_mac_tx_frm_has_freq(ar)) {
3694                         ath10k_dbg(ar, ATH10K_DBG_MAC, "queued offchannel skb %p\n",
3695                                    skb);
3696
3697                         skb_queue_tail(&ar->offchan_tx_queue, skb);
3698                         ieee80211_queue_work(hw, &ar->offchan_tx_work);
3699                         return;
3700                 }
3701         }
3702
3703         ath10k_mac_tx(ar, txmode, skb);
3704 }
3705
3706 /* Must not be called with conf_mutex held as workers can use that also. */
3707 void ath10k_drain_tx(struct ath10k *ar)
3708 {
3709         /* make sure rcu-protected mac80211 tx path itself is drained */
3710         synchronize_net();
3711
3712         ath10k_offchan_tx_purge(ar);
3713         ath10k_mgmt_over_wmi_tx_purge(ar);
3714
3715         cancel_work_sync(&ar->offchan_tx_work);
3716         cancel_work_sync(&ar->wmi_mgmt_tx_work);
3717 }
3718
3719 void ath10k_halt(struct ath10k *ar)
3720 {
3721         struct ath10k_vif *arvif;
3722
3723         lockdep_assert_held(&ar->conf_mutex);
3724
3725         clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
3726         ar->filter_flags = 0;
3727         ar->monitor = false;
3728         ar->monitor_arvif = NULL;
3729
3730         if (ar->monitor_started)
3731                 ath10k_monitor_stop(ar);
3732
3733         ar->monitor_started = false;
3734         ar->tx_paused = 0;
3735
3736         ath10k_scan_finish(ar);
3737         ath10k_peer_cleanup_all(ar);
3738         ath10k_core_stop(ar);
3739         ath10k_hif_power_down(ar);
3740
3741         spin_lock_bh(&ar->data_lock);
3742         list_for_each_entry(arvif, &ar->arvifs, list)
3743                 ath10k_mac_vif_beacon_cleanup(arvif);
3744         spin_unlock_bh(&ar->data_lock);
3745 }
3746
3747 static int ath10k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
3748 {
3749         struct ath10k *ar = hw->priv;
3750
3751         mutex_lock(&ar->conf_mutex);
3752
3753         *tx_ant = ar->cfg_tx_chainmask;
3754         *rx_ant = ar->cfg_rx_chainmask;
3755
3756         mutex_unlock(&ar->conf_mutex);
3757
3758         return 0;
3759 }
3760
3761 static void ath10k_check_chain_mask(struct ath10k *ar, u32 cm, const char *dbg)
3762 {
3763         /* It is not clear that allowing gaps in chainmask
3764          * is helpful.  Probably it will not do what user
3765          * is hoping for, so warn in that case.
3766          */
3767         if (cm == 15 || cm == 7 || cm == 3 || cm == 1 || cm == 0)
3768                 return;
3769
3770         ath10k_warn(ar, "mac %s antenna chainmask may be invalid: 0x%x.  Suggested values: 15, 7, 3, 1 or 0.\n",
3771                     dbg, cm);
3772 }
3773
3774 static int ath10k_mac_get_vht_cap_bf_sts(struct ath10k *ar)
3775 {
3776         int nsts = ar->vht_cap_info;
3777
3778         nsts &= IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
3779         nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
3780
3781         /* If firmware does not deliver to host number of space-time
3782          * streams supported, assume it support up to 4 BF STS and return
3783          * the value for VHT CAP: nsts-1)
3784          */
3785         if (nsts == 0)
3786                 return 3;
3787
3788         return nsts;
3789 }
3790
3791 static int ath10k_mac_get_vht_cap_bf_sound_dim(struct ath10k *ar)
3792 {
3793         int sound_dim = ar->vht_cap_info;
3794
3795         sound_dim &= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
3796         sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
3797
3798         /* If the sounding dimension is not advertised by the firmware,
3799          * let's use a default value of 1
3800          */
3801         if (sound_dim == 0)
3802                 return 1;
3803
3804         return sound_dim;
3805 }
3806
3807 static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
3808 {
3809         struct ieee80211_sta_vht_cap vht_cap = {0};
3810         u16 mcs_map;
3811         u32 val;
3812         int i;
3813
3814         vht_cap.vht_supported = 1;
3815         vht_cap.cap = ar->vht_cap_info;
3816
3817         if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
3818                                 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)) {
3819                 val = ath10k_mac_get_vht_cap_bf_sts(ar);
3820                 val <<= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
3821                 val &= IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
3822
3823                 vht_cap.cap |= val;
3824         }
3825
3826         if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
3827                                 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)) {
3828                 val = ath10k_mac_get_vht_cap_bf_sound_dim(ar);
3829                 val <<= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
3830                 val &= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
3831
3832                 vht_cap.cap |= val;
3833         }
3834
3835         mcs_map = 0;
3836         for (i = 0; i < 8; i++) {
3837                 if ((i < ar->num_rf_chains) && (ar->cfg_tx_chainmask & BIT(i)))
3838                         mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
3839                 else
3840                         mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
3841         }
3842
3843         vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
3844         vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
3845
3846         return vht_cap;
3847 }
3848
3849 static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
3850 {
3851         int i;
3852         struct ieee80211_sta_ht_cap ht_cap = {0};
3853
3854         if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
3855                 return ht_cap;
3856
3857         ht_cap.ht_supported = 1;
3858         ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
3859         ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
3860         ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
3861         ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
3862         ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
3863
3864         if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
3865                 ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
3866
3867         if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
3868                 ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
3869
3870         if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
3871                 u32 smps;
3872
3873                 smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
3874                 smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
3875
3876                 ht_cap.cap |= smps;
3877         }
3878
3879         if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC)
3880                 ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
3881
3882         if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
3883                 u32 stbc;
3884
3885                 stbc   = ar->ht_cap_info;
3886                 stbc  &= WMI_HT_CAP_RX_STBC;
3887                 stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
3888                 stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
3889                 stbc  &= IEEE80211_HT_CAP_RX_STBC;
3890
3891                 ht_cap.cap |= stbc;
3892         }
3893
3894         if (ar->ht_cap_info & WMI_HT_CAP_LDPC)
3895                 ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
3896
3897         if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
3898                 ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
3899
3900         /* max AMSDU is implicitly taken from vht_cap_info */
3901         if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
3902                 ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
3903
3904         for (i = 0; i < ar->num_rf_chains; i++) {
3905                 if (ar->cfg_rx_chainmask & BIT(i))
3906                         ht_cap.mcs.rx_mask[i] = 0xFF;
3907         }
3908
3909         ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
3910
3911         return ht_cap;
3912 }
3913
3914 static void ath10k_mac_setup_ht_vht_cap(struct ath10k *ar)
3915 {
3916         struct ieee80211_supported_band *band;
3917         struct ieee80211_sta_vht_cap vht_cap;
3918         struct ieee80211_sta_ht_cap ht_cap;
3919
3920         ht_cap = ath10k_get_ht_cap(ar);
3921         vht_cap = ath10k_create_vht_cap(ar);
3922
3923         if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
3924                 band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
3925                 band->ht_cap = ht_cap;
3926
3927                 /* Enable the VHT support at 2.4 GHz */
3928                 band->vht_cap = vht_cap;
3929         }
3930         if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
3931                 band = &ar->mac.sbands[IEEE80211_BAND_5GHZ];
3932                 band->ht_cap = ht_cap;
3933                 band->vht_cap = vht_cap;
3934         }
3935 }
3936
3937 static int __ath10k_set_antenna(struct ath10k *ar, u32 tx_ant, u32 rx_ant)
3938 {
3939         int ret;
3940
3941         lockdep_assert_held(&ar->conf_mutex);
3942
3943         ath10k_check_chain_mask(ar, tx_ant, "tx");
3944         ath10k_check_chain_mask(ar, rx_ant, "rx");
3945
3946         ar->cfg_tx_chainmask = tx_ant;
3947         ar->cfg_rx_chainmask = rx_ant;
3948
3949         if ((ar->state != ATH10K_STATE_ON) &&
3950             (ar->state != ATH10K_STATE_RESTARTED))
3951                 return 0;
3952
3953         ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->tx_chain_mask,
3954                                         tx_ant);
3955         if (ret) {
3956                 ath10k_warn(ar, "failed to set tx-chainmask: %d, req 0x%x\n",
3957                             ret, tx_ant);
3958                 return ret;
3959         }
3960
3961         ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->rx_chain_mask,
3962                                         rx_ant);
3963         if (ret) {
3964                 ath10k_warn(ar, "failed to set rx-chainmask: %d, req 0x%x\n",
3965                             ret, rx_ant);
3966                 return ret;
3967         }
3968
3969         /* Reload HT/VHT capability */
3970         ath10k_mac_setup_ht_vht_cap(ar);
3971
3972         return 0;
3973 }
3974
3975 static int ath10k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
3976 {
3977         struct ath10k *ar = hw->priv;
3978         int ret;
3979
3980         mutex_lock(&ar->conf_mutex);
3981         ret = __ath10k_set_antenna(ar, tx_ant, rx_ant);
3982         mutex_unlock(&ar->conf_mutex);
3983         return ret;
3984 }
3985
3986 static int ath10k_start(struct ieee80211_hw *hw)
3987 {
3988         struct ath10k *ar = hw->priv;
3989         u32 burst_enable;
3990         int ret = 0;
3991
3992         /*
3993          * This makes sense only when restarting hw. It is harmless to call
3994          * uncoditionally. This is necessary to make sure no HTT/WMI tx
3995          * commands will be submitted while restarting.
3996          */
3997         ath10k_drain_tx(ar);
3998
3999         mutex_lock(&ar->conf_mutex);
4000
4001         switch (ar->state) {
4002         case ATH10K_STATE_OFF:
4003                 ar->state = ATH10K_STATE_ON;
4004                 break;
4005         case ATH10K_STATE_RESTARTING:
4006                 ath10k_halt(ar);
4007                 ar->state = ATH10K_STATE_RESTARTED;
4008                 break;
4009         case ATH10K_STATE_ON:
4010         case ATH10K_STATE_RESTARTED:
4011         case ATH10K_STATE_WEDGED:
4012                 WARN_ON(1);
4013                 ret = -EINVAL;
4014                 goto err;
4015         case ATH10K_STATE_UTF:
4016                 ret = -EBUSY;
4017                 goto err;
4018         }
4019
4020         ret = ath10k_hif_power_up(ar);
4021         if (ret) {
4022                 ath10k_err(ar, "Could not init hif: %d\n", ret);
4023                 goto err_off;
4024         }
4025
4026         ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_NORMAL);
4027         if (ret) {
4028                 ath10k_err(ar, "Could not init core: %d\n", ret);
4029                 goto err_power_down;
4030         }
4031
4032         ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->pmf_qos, 1);
4033         if (ret) {
4034                 ath10k_warn(ar, "failed to enable PMF QOS: %d\n", ret);
4035                 goto err_core_stop;
4036         }
4037
4038         ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->dynamic_bw, 1);
4039         if (ret) {
4040                 ath10k_warn(ar, "failed to enable dynamic BW: %d\n", ret);
4041                 goto err_core_stop;
4042         }
4043
4044         if (test_bit(WMI_SERVICE_ADAPTIVE_OCS, ar->wmi.svc_map)) {
4045                 ret = ath10k_wmi_adaptive_qcs(ar, true);
4046                 if (ret) {
4047                         ath10k_warn(ar, "failed to enable adaptive qcs: %d\n",
4048                                     ret);
4049                         goto err_core_stop;
4050                 }
4051         }
4052
4053         if (test_bit(WMI_SERVICE_BURST, ar->wmi.svc_map)) {
4054                 burst_enable = ar->wmi.pdev_param->burst_enable;
4055                 ret = ath10k_wmi_pdev_set_param(ar, burst_enable, 0);
4056                 if (ret) {
4057                         ath10k_warn(ar, "failed to disable burst: %d\n", ret);
4058                         goto err_core_stop;
4059                 }
4060         }
4061
4062         __ath10k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
4063
4064         /*
4065          * By default FW set ARP frames ac to voice (6). In that case ARP
4066          * exchange is not working properly for UAPSD enabled AP. ARP requests
4067          * which arrives with access category 0 are processed by network stack
4068          * and send back with access category 0, but FW changes access category
4069          * to 6. Set ARP frames access category to best effort (0) solves
4070          * this problem.
4071          */
4072
4073         ret = ath10k_wmi_pdev_set_param(ar,
4074                                         ar->wmi.pdev_param->arp_ac_override, 0);
4075         if (ret) {
4076                 ath10k_warn(ar, "failed to set arp ac override parameter: %d\n",
4077                             ret);
4078                 goto err_core_stop;
4079         }
4080
4081         if (test_bit(ATH10K_FW_FEATURE_SUPPORTS_ADAPTIVE_CCA,
4082                      ar->fw_features)) {
4083                 ret = ath10k_wmi_pdev_enable_adaptive_cca(ar, 1,
4084                                                           WMI_CCA_DETECT_LEVEL_AUTO,
4085                                                           WMI_CCA_DETECT_MARGIN_AUTO);
4086                 if (ret) {
4087                         ath10k_warn(ar, "failed to enable adaptive cca: %d\n",
4088                                     ret);
4089                         goto err_core_stop;
4090                 }
4091         }
4092
4093         ret = ath10k_wmi_pdev_set_param(ar,
4094                                         ar->wmi.pdev_param->ani_enable, 1);
4095         if (ret) {
4096                 ath10k_warn(ar, "failed to enable ani by default: %d\n",
4097                             ret);
4098                 goto err_core_stop;
4099         }
4100
4101         ar->ani_enabled = true;
4102
4103         ar->num_started_vdevs = 0;
4104         ath10k_regd_update(ar);
4105
4106         ath10k_spectral_start(ar);
4107         ath10k_thermal_set_throttling(ar);
4108
4109         mutex_unlock(&ar->conf_mutex);
4110         return 0;
4111
4112 err_core_stop:
4113         ath10k_core_stop(ar);
4114
4115 err_power_down:
4116         ath10k_hif_power_down(ar);
4117
4118 err_off:
4119         ar->state = ATH10K_STATE_OFF;
4120
4121 err:
4122         mutex_unlock(&ar->conf_mutex);
4123         return ret;
4124 }
4125
4126 static void ath10k_stop(struct ieee80211_hw *hw)
4127 {
4128         struct ath10k *ar = hw->priv;
4129
4130         ath10k_drain_tx(ar);
4131
4132         mutex_lock(&ar->conf_mutex);
4133         if (ar->state != ATH10K_STATE_OFF) {
4134                 ath10k_halt(ar);
4135                 ar->state = ATH10K_STATE_OFF;
4136         }
4137         mutex_unlock(&ar->conf_mutex);
4138
4139         cancel_delayed_work_sync(&ar->scan.timeout);
4140         cancel_work_sync(&ar->restart_work);
4141 }
4142
4143 static int ath10k_config_ps(struct ath10k *ar)
4144 {
4145         struct ath10k_vif *arvif;
4146         int ret = 0;
4147
4148         lockdep_assert_held(&ar->conf_mutex);
4149
4150         list_for_each_entry(arvif, &ar->arvifs, list) {
4151                 ret = ath10k_mac_vif_setup_ps(arvif);
4152                 if (ret) {
4153                         ath10k_warn(ar, "failed to setup powersave: %d\n", ret);
4154                         break;
4155                 }
4156         }
4157
4158         return ret;
4159 }
4160
4161 static int ath10k_mac_txpower_setup(struct ath10k *ar, int txpower)
4162 {
4163         int ret;
4164         u32 param;
4165
4166         lockdep_assert_held(&ar->conf_mutex);
4167
4168         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac txpower %d\n", txpower);
4169
4170         param = ar->wmi.pdev_param->txpower_limit2g;
4171         ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
4172         if (ret) {
4173                 ath10k_warn(ar, "failed to set 2g txpower %d: %d\n",
4174                             txpower, ret);
4175                 return ret;
4176         }
4177
4178         param = ar->wmi.pdev_param->txpower_limit5g;
4179         ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
4180         if (ret) {
4181                 ath10k_warn(ar, "failed to set 5g txpower %d: %d\n",
4182                             txpower, ret);
4183                 return ret;
4184         }
4185
4186         return 0;
4187 }
4188
4189 static int ath10k_mac_txpower_recalc(struct ath10k *ar)
4190 {
4191         struct ath10k_vif *arvif;
4192         int ret, txpower = -1;
4193
4194         lockdep_assert_held(&ar->conf_mutex);
4195
4196         list_for_each_entry(arvif, &ar->arvifs, list) {
4197                 WARN_ON(arvif->txpower < 0);
4198
4199                 if (txpower == -1)
4200                         txpower = arvif->txpower;
4201                 else
4202                         txpower = min(txpower, arvif->txpower);
4203         }
4204
4205         if (WARN_ON(txpower == -1))
4206                 return -EINVAL;
4207
4208         ret = ath10k_mac_txpower_setup(ar, txpower);
4209         if (ret) {
4210                 ath10k_warn(ar, "failed to setup tx power %d: %d\n",
4211                             txpower, ret);
4212                 return ret;
4213         }
4214
4215         return 0;
4216 }
4217
4218 static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
4219 {
4220         struct ath10k *ar = hw->priv;
4221         struct ieee80211_conf *conf = &hw->conf;
4222         int ret = 0;
4223
4224         mutex_lock(&ar->conf_mutex);
4225
4226         if (changed & IEEE80211_CONF_CHANGE_PS)
4227                 ath10k_config_ps(ar);
4228
4229         if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
4230                 ar->monitor = conf->flags & IEEE80211_CONF_MONITOR;
4231                 ret = ath10k_monitor_recalc(ar);
4232                 if (ret)
4233                         ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
4234         }
4235
4236         mutex_unlock(&ar->conf_mutex);
4237         return ret;
4238 }
4239
4240 static u32 get_nss_from_chainmask(u16 chain_mask)
4241 {
4242         if ((chain_mask & 0xf) == 0xf)
4243                 return 4;
4244         else if ((chain_mask & 0x7) == 0x7)
4245                 return 3;
4246         else if ((chain_mask & 0x3) == 0x3)
4247                 return 2;
4248         return 1;
4249 }
4250
4251 static int ath10k_mac_set_txbf_conf(struct ath10k_vif *arvif)
4252 {
4253         u32 value = 0;
4254         struct ath10k *ar = arvif->ar;
4255         int nsts;
4256         int sound_dim;
4257
4258         if (ath10k_wmi_get_txbf_conf_scheme(ar) != WMI_TXBF_CONF_BEFORE_ASSOC)
4259                 return 0;
4260
4261         nsts = ath10k_mac_get_vht_cap_bf_sts(ar);
4262         if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
4263                                 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE))
4264                 value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
4265
4266         sound_dim = ath10k_mac_get_vht_cap_bf_sound_dim(ar);
4267         if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
4268                                 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE))
4269                 value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
4270
4271         if (!value)
4272                 return 0;
4273
4274         if (ar->vht_cap_info & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)
4275                 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
4276
4277         if (ar->vht_cap_info & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)
4278                 value |= (WMI_VDEV_PARAM_TXBF_MU_TX_BFER |
4279                           WMI_VDEV_PARAM_TXBF_SU_TX_BFER);
4280
4281         if (ar->vht_cap_info & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)
4282                 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
4283
4284         if (ar->vht_cap_info & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)
4285                 value |= (WMI_VDEV_PARAM_TXBF_MU_TX_BFEE |
4286                           WMI_VDEV_PARAM_TXBF_SU_TX_BFEE);
4287
4288         return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
4289                                          ar->wmi.vdev_param->txbf, value);
4290 }
4291
4292 /*
4293  * TODO:
4294  * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
4295  * because we will send mgmt frames without CCK. This requirement
4296  * for P2P_FIND/GO_NEG should be handled by checking CCK flag
4297  * in the TX packet.
4298  */
4299 static int ath10k_add_interface(struct ieee80211_hw *hw,
4300                                 struct ieee80211_vif *vif)
4301 {
4302         struct ath10k *ar = hw->priv;
4303         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4304         enum wmi_sta_powersave_param param;
4305         int ret = 0;
4306         u32 value;
4307         int bit;
4308         int i;
4309         u32 vdev_param;
4310
4311         vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
4312
4313         mutex_lock(&ar->conf_mutex);
4314
4315         memset(arvif, 0, sizeof(*arvif));
4316
4317         arvif->ar = ar;
4318         arvif->vif = vif;
4319
4320         INIT_LIST_HEAD(&arvif->list);
4321         INIT_WORK(&arvif->ap_csa_work, ath10k_mac_vif_ap_csa_work);
4322         INIT_DELAYED_WORK(&arvif->connection_loss_work,
4323                           ath10k_mac_vif_sta_connection_loss_work);
4324
4325         for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
4326                 arvif->bitrate_mask.control[i].legacy = 0xffffffff;
4327                 memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
4328                        sizeof(arvif->bitrate_mask.control[i].ht_mcs));
4329                 memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
4330                        sizeof(arvif->bitrate_mask.control[i].vht_mcs));
4331         }
4332
4333         if (ar->num_peers >= ar->max_num_peers) {
4334                 ath10k_warn(ar, "refusing vdev creation due to insufficient peer entry resources in firmware\n");
4335                 ret = -ENOBUFS;
4336                 goto err;
4337         }
4338
4339         if (ar->free_vdev_map == 0) {
4340                 ath10k_warn(ar, "Free vdev map is empty, no more interfaces allowed.\n");
4341                 ret = -EBUSY;
4342                 goto err;
4343         }
4344         bit = __ffs64(ar->free_vdev_map);
4345
4346         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac create vdev %i map %llx\n",
4347                    bit, ar->free_vdev_map);
4348
4349         arvif->vdev_id = bit;
4350         arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
4351
4352         switch (vif->type) {
4353         case NL80211_IFTYPE_P2P_DEVICE:
4354                 arvif->vdev_type = WMI_VDEV_TYPE_STA;
4355                 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
4356                 break;
4357         case NL80211_IFTYPE_UNSPECIFIED:
4358         case NL80211_IFTYPE_STATION:
4359                 arvif->vdev_type = WMI_VDEV_TYPE_STA;
4360                 if (vif->p2p)
4361                         arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
4362                 break;
4363         case NL80211_IFTYPE_ADHOC:
4364                 arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
4365                 break;
4366         case NL80211_IFTYPE_MESH_POINT:
4367                 if (test_bit(WMI_SERVICE_MESH, ar->wmi.svc_map)) {
4368                         arvif->vdev_subtype = WMI_VDEV_SUBTYPE_MESH;
4369                 } else if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags)) {
4370                         ret = -EINVAL;
4371                         ath10k_warn(ar, "must load driver with rawmode=1 to add mesh interfaces\n");
4372                         goto err;
4373                 }
4374                 arvif->vdev_type = WMI_VDEV_TYPE_AP;
4375                 break;
4376         case NL80211_IFTYPE_AP:
4377                 arvif->vdev_type = WMI_VDEV_TYPE_AP;
4378
4379                 if (vif->p2p)
4380                         arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
4381                 break;
4382         case NL80211_IFTYPE_MONITOR:
4383                 arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
4384                 break;
4385         default:
4386                 WARN_ON(1);
4387                 break;
4388         }
4389
4390         /* Using vdev_id as queue number will make it very easy to do per-vif
4391          * tx queue locking. This shouldn't wrap due to interface combinations
4392          * but do a modulo for correctness sake and prevent using offchannel tx
4393          * queues for regular vif tx.
4394          */
4395         vif->cab_queue = arvif->vdev_id % (IEEE80211_MAX_QUEUES - 1);
4396         for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
4397                 vif->hw_queue[i] = arvif->vdev_id % (IEEE80211_MAX_QUEUES - 1);
4398
4399         /* Some firmware revisions don't wait for beacon tx completion before
4400          * sending another SWBA event. This could lead to hardware using old
4401          * (freed) beacon data in some cases, e.g. tx credit starvation
4402          * combined with missed TBTT. This is very very rare.
4403          *
4404          * On non-IOMMU-enabled hosts this could be a possible security issue
4405          * because hw could beacon some random data on the air.  On
4406          * IOMMU-enabled hosts DMAR faults would occur in most cases and target
4407          * device would crash.
4408          *
4409          * Since there are no beacon tx completions (implicit nor explicit)
4410          * propagated to host the only workaround for this is to allocate a
4411          * DMA-coherent buffer for a lifetime of a vif and use it for all
4412          * beacon tx commands. Worst case for this approach is some beacons may
4413          * become corrupted, e.g. have garbled IEs or out-of-date TIM bitmap.
4414          */
4415         if (vif->type == NL80211_IFTYPE_ADHOC ||
4416             vif->type == NL80211_IFTYPE_MESH_POINT ||
4417             vif->type == NL80211_IFTYPE_AP) {
4418                 arvif->beacon_buf = dma_zalloc_coherent(ar->dev,
4419                                                         IEEE80211_MAX_FRAME_LEN,
4420                                                         &arvif->beacon_paddr,
4421                                                         GFP_ATOMIC);
4422                 if (!arvif->beacon_buf) {
4423                         ret = -ENOMEM;
4424                         ath10k_warn(ar, "failed to allocate beacon buffer: %d\n",
4425                                     ret);
4426                         goto err;
4427                 }
4428         }
4429         if (test_bit(ATH10K_FLAG_HW_CRYPTO_DISABLED, &ar->dev_flags))
4430                 arvif->nohwcrypt = true;
4431
4432         if (arvif->nohwcrypt &&
4433             !test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags)) {
4434                 ath10k_warn(ar, "cryptmode module param needed for sw crypto\n");
4435                 goto err;
4436         }
4437
4438         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d bcnmode %s\n",
4439                    arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
4440                    arvif->beacon_buf ? "single-buf" : "per-skb");
4441
4442         ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
4443                                      arvif->vdev_subtype, vif->addr);
4444         if (ret) {
4445                 ath10k_warn(ar, "failed to create WMI vdev %i: %d\n",
4446                             arvif->vdev_id, ret);
4447                 goto err;
4448         }
4449
4450         ar->free_vdev_map &= ~(1LL << arvif->vdev_id);
4451         list_add(&arvif->list, &ar->arvifs);
4452
4453         /* It makes no sense to have firmware do keepalives. mac80211 already
4454          * takes care of this with idle connection polling.
4455          */
4456         ret = ath10k_mac_vif_disable_keepalive(arvif);
4457         if (ret) {
4458                 ath10k_warn(ar, "failed to disable keepalive on vdev %i: %d\n",
4459                             arvif->vdev_id, ret);
4460                 goto err_vdev_delete;
4461         }
4462
4463         arvif->def_wep_key_idx = -1;
4464
4465         vdev_param = ar->wmi.vdev_param->tx_encap_type;
4466         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
4467                                         ATH10K_HW_TXRX_NATIVE_WIFI);
4468         /* 10.X firmware does not support this VDEV parameter. Do not warn */
4469         if (ret && ret != -EOPNOTSUPP) {
4470                 ath10k_warn(ar, "failed to set vdev %i TX encapsulation: %d\n",
4471                             arvif->vdev_id, ret);
4472                 goto err_vdev_delete;
4473         }
4474
4475         if (ar->cfg_tx_chainmask) {
4476                 u16 nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
4477
4478                 vdev_param = ar->wmi.vdev_param->nss;
4479                 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
4480                                                 nss);
4481                 if (ret) {
4482                         ath10k_warn(ar, "failed to set vdev %i chainmask 0x%x, nss %i: %d\n",
4483                                     arvif->vdev_id, ar->cfg_tx_chainmask, nss,
4484                                     ret);
4485                         goto err_vdev_delete;
4486                 }
4487         }
4488
4489         if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
4490             arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
4491                 ret = ath10k_peer_create(ar, arvif->vdev_id, vif->addr,
4492                                          WMI_PEER_TYPE_DEFAULT);
4493                 if (ret) {
4494                         ath10k_warn(ar, "failed to create vdev %i peer for AP/IBSS: %d\n",
4495                                     arvif->vdev_id, ret);
4496                         goto err_vdev_delete;
4497                 }
4498         }
4499
4500         if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
4501                 ret = ath10k_mac_set_kickout(arvif);
4502                 if (ret) {
4503                         ath10k_warn(ar, "failed to set vdev %i kickout parameters: %d\n",
4504                                     arvif->vdev_id, ret);
4505                         goto err_peer_delete;
4506                 }
4507         }
4508
4509         if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
4510                 param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
4511                 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
4512                 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4513                                                   param, value);
4514                 if (ret) {
4515                         ath10k_warn(ar, "failed to set vdev %i RX wake policy: %d\n",
4516                                     arvif->vdev_id, ret);
4517                         goto err_peer_delete;
4518                 }
4519
4520                 ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
4521                 if (ret) {
4522                         ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
4523                                     arvif->vdev_id, ret);
4524                         goto err_peer_delete;
4525                 }
4526
4527                 ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
4528                 if (ret) {
4529                         ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
4530                                     arvif->vdev_id, ret);
4531                         goto err_peer_delete;
4532                 }
4533         }
4534
4535         ret = ath10k_mac_set_txbf_conf(arvif);
4536         if (ret) {
4537                 ath10k_warn(ar, "failed to set txbf for vdev %d: %d\n",
4538                             arvif->vdev_id, ret);
4539                 goto err_peer_delete;
4540         }
4541
4542         ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
4543         if (ret) {
4544                 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
4545                             arvif->vdev_id, ret);
4546                 goto err_peer_delete;
4547         }
4548
4549         arvif->txpower = vif->bss_conf.txpower;
4550         ret = ath10k_mac_txpower_recalc(ar);
4551         if (ret) {
4552                 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
4553                 goto err_peer_delete;
4554         }
4555
4556         if (vif->type == NL80211_IFTYPE_MONITOR) {
4557                 ar->monitor_arvif = arvif;
4558                 ret = ath10k_monitor_recalc(ar);
4559                 if (ret) {
4560                         ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
4561                         goto err_peer_delete;
4562                 }
4563         }
4564
4565         spin_lock_bh(&ar->htt.tx_lock);
4566         if (!ar->tx_paused)
4567                 ieee80211_wake_queue(ar->hw, arvif->vdev_id);
4568         spin_unlock_bh(&ar->htt.tx_lock);
4569
4570         mutex_unlock(&ar->conf_mutex);
4571         return 0;
4572
4573 err_peer_delete:
4574         if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
4575             arvif->vdev_type == WMI_VDEV_TYPE_IBSS)
4576                 ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
4577
4578 err_vdev_delete:
4579         ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
4580         ar->free_vdev_map |= 1LL << arvif->vdev_id;
4581         list_del(&arvif->list);
4582
4583 err:
4584         if (arvif->beacon_buf) {
4585                 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
4586                                   arvif->beacon_buf, arvif->beacon_paddr);
4587                 arvif->beacon_buf = NULL;
4588         }
4589
4590         mutex_unlock(&ar->conf_mutex);
4591
4592         return ret;
4593 }
4594
4595 static void ath10k_mac_vif_tx_unlock_all(struct ath10k_vif *arvif)
4596 {
4597         int i;
4598
4599         for (i = 0; i < BITS_PER_LONG; i++)
4600                 ath10k_mac_vif_tx_unlock(arvif, i);
4601 }
4602
4603 static void ath10k_remove_interface(struct ieee80211_hw *hw,
4604                                     struct ieee80211_vif *vif)
4605 {
4606         struct ath10k *ar = hw->priv;
4607         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4608         int ret;
4609
4610         cancel_work_sync(&arvif->ap_csa_work);
4611         cancel_delayed_work_sync(&arvif->connection_loss_work);
4612
4613         mutex_lock(&ar->conf_mutex);
4614
4615         spin_lock_bh(&ar->data_lock);
4616         ath10k_mac_vif_beacon_cleanup(arvif);
4617         spin_unlock_bh(&ar->data_lock);
4618
4619         ret = ath10k_spectral_vif_stop(arvif);
4620         if (ret)
4621                 ath10k_warn(ar, "failed to stop spectral for vdev %i: %d\n",
4622                             arvif->vdev_id, ret);
4623
4624         ar->free_vdev_map |= 1LL << arvif->vdev_id;
4625         list_del(&arvif->list);
4626
4627         if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
4628             arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
4629                 ret = ath10k_wmi_peer_delete(arvif->ar, arvif->vdev_id,
4630                                              vif->addr);
4631                 if (ret)
4632                         ath10k_warn(ar, "failed to submit AP/IBSS self-peer removal on vdev %i: %d\n",
4633                                     arvif->vdev_id, ret);
4634
4635                 kfree(arvif->u.ap.noa_data);
4636         }
4637
4638         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i delete (remove interface)\n",
4639                    arvif->vdev_id);
4640
4641         ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
4642         if (ret)
4643                 ath10k_warn(ar, "failed to delete WMI vdev %i: %d\n",
4644                             arvif->vdev_id, ret);
4645
4646         /* Some firmware revisions don't notify host about self-peer removal
4647          * until after associated vdev is deleted.
4648          */
4649         if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
4650             arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
4651                 ret = ath10k_wait_for_peer_deleted(ar, arvif->vdev_id,
4652                                                    vif->addr);
4653                 if (ret)
4654                         ath10k_warn(ar, "failed to remove AP self-peer on vdev %i: %d\n",
4655                                     arvif->vdev_id, ret);
4656
4657                 spin_lock_bh(&ar->data_lock);
4658                 ar->num_peers--;
4659                 spin_unlock_bh(&ar->data_lock);
4660         }
4661
4662         ath10k_peer_cleanup(ar, arvif->vdev_id);
4663
4664         if (vif->type == NL80211_IFTYPE_MONITOR) {
4665                 ar->monitor_arvif = NULL;
4666                 ret = ath10k_monitor_recalc(ar);
4667                 if (ret)
4668                         ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
4669         }
4670
4671         spin_lock_bh(&ar->htt.tx_lock);
4672         ath10k_mac_vif_tx_unlock_all(arvif);
4673         spin_unlock_bh(&ar->htt.tx_lock);
4674
4675         mutex_unlock(&ar->conf_mutex);
4676 }
4677
4678 /*
4679  * FIXME: Has to be verified.
4680  */
4681 #define SUPPORTED_FILTERS                       \
4682         (FIF_ALLMULTI |                         \
4683         FIF_CONTROL |                           \
4684         FIF_PSPOLL |                            \
4685         FIF_OTHER_BSS |                         \
4686         FIF_BCN_PRBRESP_PROMISC |               \
4687         FIF_PROBE_REQ |                         \
4688         FIF_FCSFAIL)
4689
4690 static void ath10k_configure_filter(struct ieee80211_hw *hw,
4691                                     unsigned int changed_flags,
4692                                     unsigned int *total_flags,
4693                                     u64 multicast)
4694 {
4695         struct ath10k *ar = hw->priv;
4696         int ret;
4697
4698         mutex_lock(&ar->conf_mutex);
4699
4700         changed_flags &= SUPPORTED_FILTERS;
4701         *total_flags &= SUPPORTED_FILTERS;
4702         ar->filter_flags = *total_flags;
4703
4704         ret = ath10k_monitor_recalc(ar);
4705         if (ret)
4706                 ath10k_warn(ar, "failed to recalc montior: %d\n", ret);
4707
4708         mutex_unlock(&ar->conf_mutex);
4709 }
4710
4711 static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
4712                                     struct ieee80211_vif *vif,
4713                                     struct ieee80211_bss_conf *info,
4714                                     u32 changed)
4715 {
4716         struct ath10k *ar = hw->priv;
4717         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4718         int ret = 0;
4719         u32 vdev_param, pdev_param, slottime, preamble;
4720
4721         mutex_lock(&ar->conf_mutex);
4722
4723         if (changed & BSS_CHANGED_IBSS)
4724                 ath10k_control_ibss(arvif, info, vif->addr);
4725
4726         if (changed & BSS_CHANGED_BEACON_INT) {
4727                 arvif->beacon_interval = info->beacon_int;
4728                 vdev_param = ar->wmi.vdev_param->beacon_interval;
4729                 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
4730                                                 arvif->beacon_interval);
4731                 ath10k_dbg(ar, ATH10K_DBG_MAC,
4732                            "mac vdev %d beacon_interval %d\n",
4733                            arvif->vdev_id, arvif->beacon_interval);
4734
4735                 if (ret)
4736                         ath10k_warn(ar, "failed to set beacon interval for vdev %d: %i\n",
4737                                     arvif->vdev_id, ret);
4738         }
4739
4740         if (changed & BSS_CHANGED_BEACON) {
4741                 ath10k_dbg(ar, ATH10K_DBG_MAC,
4742                            "vdev %d set beacon tx mode to staggered\n",
4743                            arvif->vdev_id);
4744
4745                 pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
4746                 ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
4747                                                 WMI_BEACON_STAGGERED_MODE);
4748                 if (ret)
4749                         ath10k_warn(ar, "failed to set beacon mode for vdev %d: %i\n",
4750                                     arvif->vdev_id, ret);
4751
4752                 ret = ath10k_mac_setup_bcn_tmpl(arvif);
4753                 if (ret)
4754                         ath10k_warn(ar, "failed to update beacon template: %d\n",
4755                                     ret);
4756
4757                 if (ieee80211_vif_is_mesh(vif)) {
4758                         /* mesh doesn't use SSID but firmware needs it */
4759                         strncpy(arvif->u.ap.ssid, "mesh",
4760                                 sizeof(arvif->u.ap.ssid));
4761                         arvif->u.ap.ssid_len = 4;
4762                 }
4763         }
4764
4765         if (changed & BSS_CHANGED_AP_PROBE_RESP) {
4766                 ret = ath10k_mac_setup_prb_tmpl(arvif);
4767                 if (ret)
4768                         ath10k_warn(ar, "failed to setup probe resp template on vdev %i: %d\n",
4769                                     arvif->vdev_id, ret);
4770         }
4771
4772         if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
4773                 arvif->dtim_period = info->dtim_period;
4774
4775                 ath10k_dbg(ar, ATH10K_DBG_MAC,
4776                            "mac vdev %d dtim_period %d\n",
4777                            arvif->vdev_id, arvif->dtim_period);
4778
4779                 vdev_param = ar->wmi.vdev_param->dtim_period;
4780                 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
4781                                                 arvif->dtim_period);
4782                 if (ret)
4783                         ath10k_warn(ar, "failed to set dtim period for vdev %d: %i\n",
4784                                     arvif->vdev_id, ret);
4785         }
4786
4787         if (changed & BSS_CHANGED_SSID &&
4788             vif->type == NL80211_IFTYPE_AP) {
4789                 arvif->u.ap.ssid_len = info->ssid_len;
4790                 if (info->ssid_len)
4791                         memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
4792                 arvif->u.ap.hidden_ssid = info->hidden_ssid;
4793         }
4794
4795         if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
4796                 ether_addr_copy(arvif->bssid, info->bssid);
4797
4798         if (changed & BSS_CHANGED_BEACON_ENABLED)
4799                 ath10k_control_beaconing(arvif, info);
4800
4801         if (changed & BSS_CHANGED_ERP_CTS_PROT) {
4802                 arvif->use_cts_prot = info->use_cts_prot;
4803                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d cts_prot %d\n",
4804                            arvif->vdev_id, info->use_cts_prot);
4805
4806                 ret = ath10k_recalc_rtscts_prot(arvif);
4807                 if (ret)
4808                         ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
4809                                     arvif->vdev_id, ret);
4810
4811                 vdev_param = ar->wmi.vdev_param->protection_mode;
4812                 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
4813                                                 info->use_cts_prot ? 1 : 0);
4814                 if (ret)
4815                         ath10k_warn(ar, "failed to set protection mode %d on vdev %i: %d\n",
4816                                     info->use_cts_prot, arvif->vdev_id, ret);
4817         }
4818
4819         if (changed & BSS_CHANGED_ERP_SLOT) {
4820                 if (info->use_short_slot)
4821                         slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
4822
4823                 else
4824                         slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
4825
4826                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
4827                            arvif->vdev_id, slottime);
4828
4829                 vdev_param = ar->wmi.vdev_param->slot_time;
4830                 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
4831                                                 slottime);
4832                 if (ret)
4833                         ath10k_warn(ar, "failed to set erp slot for vdev %d: %i\n",
4834                                     arvif->vdev_id, ret);
4835         }
4836
4837         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
4838                 if (info->use_short_preamble)
4839                         preamble = WMI_VDEV_PREAMBLE_SHORT;
4840                 else
4841                         preamble = WMI_VDEV_PREAMBLE_LONG;
4842
4843                 ath10k_dbg(ar, ATH10K_DBG_MAC,
4844                            "mac vdev %d preamble %dn",
4845                            arvif->vdev_id, preamble);
4846
4847                 vdev_param = ar->wmi.vdev_param->preamble;
4848                 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
4849                                                 preamble);
4850                 if (ret)
4851                         ath10k_warn(ar, "failed to set preamble for vdev %d: %i\n",
4852                                     arvif->vdev_id, ret);
4853         }
4854
4855         if (changed & BSS_CHANGED_ASSOC) {
4856                 if (info->assoc) {
4857                         /* Workaround: Make sure monitor vdev is not running
4858                          * when associating to prevent some firmware revisions
4859                          * (e.g. 10.1 and 10.2) from crashing.
4860                          */
4861                         if (ar->monitor_started)
4862                                 ath10k_monitor_stop(ar);
4863                         ath10k_bss_assoc(hw, vif, info);
4864                         ath10k_monitor_recalc(ar);
4865                 } else {
4866                         ath10k_bss_disassoc(hw, vif);
4867                 }
4868         }
4869
4870         if (changed & BSS_CHANGED_TXPOWER) {
4871                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev_id %i txpower %d\n",
4872                            arvif->vdev_id, info->txpower);
4873
4874                 arvif->txpower = info->txpower;
4875                 ret = ath10k_mac_txpower_recalc(ar);
4876                 if (ret)
4877                         ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
4878         }
4879
4880         if (changed & BSS_CHANGED_PS) {
4881                 arvif->ps = vif->bss_conf.ps;
4882
4883                 ret = ath10k_config_ps(ar);
4884                 if (ret)
4885                         ath10k_warn(ar, "failed to setup ps on vdev %i: %d\n",
4886                                     arvif->vdev_id, ret);
4887         }
4888
4889         mutex_unlock(&ar->conf_mutex);
4890 }
4891
4892 static int ath10k_hw_scan(struct ieee80211_hw *hw,
4893                           struct ieee80211_vif *vif,
4894                           struct ieee80211_scan_request *hw_req)
4895 {
4896         struct ath10k *ar = hw->priv;
4897         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4898         struct cfg80211_scan_request *req = &hw_req->req;
4899         struct wmi_start_scan_arg arg;
4900         int ret = 0;
4901         int i;
4902
4903         mutex_lock(&ar->conf_mutex);
4904
4905         spin_lock_bh(&ar->data_lock);
4906         switch (ar->scan.state) {
4907         case ATH10K_SCAN_IDLE:
4908                 reinit_completion(&ar->scan.started);
4909                 reinit_completion(&ar->scan.completed);
4910                 ar->scan.state = ATH10K_SCAN_STARTING;
4911                 ar->scan.is_roc = false;
4912                 ar->scan.vdev_id = arvif->vdev_id;
4913                 ret = 0;
4914                 break;
4915         case ATH10K_SCAN_STARTING:
4916         case ATH10K_SCAN_RUNNING:
4917         case ATH10K_SCAN_ABORTING:
4918                 ret = -EBUSY;
4919                 break;
4920         }
4921         spin_unlock_bh(&ar->data_lock);
4922
4923         if (ret)
4924                 goto exit;
4925
4926         memset(&arg, 0, sizeof(arg));
4927         ath10k_wmi_start_scan_init(ar, &arg);
4928         arg.vdev_id = arvif->vdev_id;
4929         arg.scan_id = ATH10K_SCAN_ID;
4930
4931         if (req->ie_len) {
4932                 arg.ie_len = req->ie_len;
4933                 memcpy(arg.ie, req->ie, arg.ie_len);
4934         }
4935
4936         if (req->n_ssids) {
4937                 arg.n_ssids = req->n_ssids;
4938                 for (i = 0; i < arg.n_ssids; i++) {
4939                         arg.ssids[i].len  = req->ssids[i].ssid_len;
4940                         arg.ssids[i].ssid = req->ssids[i].ssid;
4941                 }
4942         } else {
4943                 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
4944         }
4945
4946         if (req->n_channels) {
4947                 arg.n_channels = req->n_channels;
4948                 for (i = 0; i < arg.n_channels; i++)
4949                         arg.channels[i] = req->channels[i]->center_freq;
4950         }
4951
4952         ret = ath10k_start_scan(ar, &arg);
4953         if (ret) {
4954                 ath10k_warn(ar, "failed to start hw scan: %d\n", ret);
4955                 spin_lock_bh(&ar->data_lock);
4956                 ar->scan.state = ATH10K_SCAN_IDLE;
4957                 spin_unlock_bh(&ar->data_lock);
4958         }
4959
4960         /* Add a 200ms margin to account for event/command processing */
4961         ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
4962                                      msecs_to_jiffies(arg.max_scan_time +
4963                                                       200));
4964
4965 exit:
4966         mutex_unlock(&ar->conf_mutex);
4967         return ret;
4968 }
4969
4970 static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
4971                                   struct ieee80211_vif *vif)
4972 {
4973         struct ath10k *ar = hw->priv;
4974
4975         mutex_lock(&ar->conf_mutex);
4976         ath10k_scan_abort(ar);
4977         mutex_unlock(&ar->conf_mutex);
4978
4979         cancel_delayed_work_sync(&ar->scan.timeout);
4980 }
4981
4982 static void ath10k_set_key_h_def_keyidx(struct ath10k *ar,
4983                                         struct ath10k_vif *arvif,
4984                                         enum set_key_cmd cmd,
4985                                         struct ieee80211_key_conf *key)
4986 {
4987         u32 vdev_param = arvif->ar->wmi.vdev_param->def_keyid;
4988         int ret;
4989
4990         /* 10.1 firmware branch requires default key index to be set to group
4991          * key index after installing it. Otherwise FW/HW Txes corrupted
4992          * frames with multi-vif APs. This is not required for main firmware
4993          * branch (e.g. 636).
4994          *
4995          * This is also needed for 636 fw for IBSS-RSN to work more reliably.
4996          *
4997          * FIXME: It remains unknown if this is required for multi-vif STA
4998          * interfaces on 10.1.
4999          */
5000
5001         if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
5002             arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
5003                 return;
5004
5005         if (key->cipher == WLAN_CIPHER_SUITE_WEP40)
5006                 return;
5007
5008         if (key->cipher == WLAN_CIPHER_SUITE_WEP104)
5009                 return;
5010
5011         if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
5012                 return;
5013
5014         if (cmd != SET_KEY)
5015                 return;
5016
5017         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5018                                         key->keyidx);
5019         if (ret)
5020                 ath10k_warn(ar, "failed to set vdev %i group key as default key: %d\n",
5021                             arvif->vdev_id, ret);
5022 }
5023
5024 static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
5025                           struct ieee80211_vif *vif, struct ieee80211_sta *sta,
5026                           struct ieee80211_key_conf *key)
5027 {
5028         struct ath10k *ar = hw->priv;
5029         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5030         struct ath10k_peer *peer;
5031         const u8 *peer_addr;
5032         bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
5033                       key->cipher == WLAN_CIPHER_SUITE_WEP104;
5034         int ret = 0;
5035         int ret2;
5036         u32 flags = 0;
5037         u32 flags2;
5038
5039         /* this one needs to be done in software */
5040         if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
5041                 return 1;
5042
5043         if (arvif->nohwcrypt)
5044                 return 1;
5045
5046         if (key->keyidx > WMI_MAX_KEY_INDEX)
5047                 return -ENOSPC;
5048
5049         mutex_lock(&ar->conf_mutex);
5050
5051         if (sta)
5052                 peer_addr = sta->addr;
5053         else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
5054                 peer_addr = vif->bss_conf.bssid;
5055         else
5056                 peer_addr = vif->addr;
5057
5058         key->hw_key_idx = key->keyidx;
5059
5060         if (is_wep) {
5061                 if (cmd == SET_KEY)
5062                         arvif->wep_keys[key->keyidx] = key;
5063                 else
5064                         arvif->wep_keys[key->keyidx] = NULL;
5065         }
5066
5067         /* the peer should not disappear in mid-way (unless FW goes awry) since
5068          * we already hold conf_mutex. we just make sure its there now. */
5069         spin_lock_bh(&ar->data_lock);
5070         peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
5071         spin_unlock_bh(&ar->data_lock);
5072
5073         if (!peer) {
5074                 if (cmd == SET_KEY) {
5075                         ath10k_warn(ar, "failed to install key for non-existent peer %pM\n",
5076                                     peer_addr);
5077                         ret = -EOPNOTSUPP;
5078                         goto exit;
5079                 } else {
5080                         /* if the peer doesn't exist there is no key to disable
5081                          * anymore */
5082                         goto exit;
5083                 }
5084         }
5085
5086         if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
5087                 flags |= WMI_KEY_PAIRWISE;
5088         else
5089                 flags |= WMI_KEY_GROUP;
5090
5091         if (is_wep) {
5092                 if (cmd == DISABLE_KEY)
5093                         ath10k_clear_vdev_key(arvif, key);
5094
5095                 /* When WEP keys are uploaded it's possible that there are
5096                  * stations associated already (e.g. when merging) without any
5097                  * keys. Static WEP needs an explicit per-peer key upload.
5098                  */
5099                 if (vif->type == NL80211_IFTYPE_ADHOC &&
5100                     cmd == SET_KEY)
5101                         ath10k_mac_vif_update_wep_key(arvif, key);
5102
5103                 /* 802.1x never sets the def_wep_key_idx so each set_key()
5104                  * call changes default tx key.
5105                  *
5106                  * Static WEP sets def_wep_key_idx via .set_default_unicast_key
5107                  * after first set_key().
5108                  */
5109                 if (cmd == SET_KEY && arvif->def_wep_key_idx == -1)
5110                         flags |= WMI_KEY_TX_USAGE;
5111         }
5112
5113         ret = ath10k_install_key(arvif, key, cmd, peer_addr, flags);
5114         if (ret) {
5115                 WARN_ON(ret > 0);
5116                 ath10k_warn(ar, "failed to install key for vdev %i peer %pM: %d\n",
5117                             arvif->vdev_id, peer_addr, ret);
5118                 goto exit;
5119         }
5120
5121         /* mac80211 sets static WEP keys as groupwise while firmware requires
5122          * them to be installed twice as both pairwise and groupwise.
5123          */
5124         if (is_wep && !sta && vif->type == NL80211_IFTYPE_STATION) {
5125                 flags2 = flags;
5126                 flags2 &= ~WMI_KEY_GROUP;
5127                 flags2 |= WMI_KEY_PAIRWISE;
5128
5129                 ret = ath10k_install_key(arvif, key, cmd, peer_addr, flags2);
5130                 if (ret) {
5131                         WARN_ON(ret > 0);
5132                         ath10k_warn(ar, "failed to install (ucast) key for vdev %i peer %pM: %d\n",
5133                                     arvif->vdev_id, peer_addr, ret);
5134                         ret2 = ath10k_install_key(arvif, key, DISABLE_KEY,
5135                                                   peer_addr, flags);
5136                         if (ret2) {
5137                                 WARN_ON(ret2 > 0);
5138                                 ath10k_warn(ar, "failed to disable (mcast) key for vdev %i peer %pM: %d\n",
5139                                             arvif->vdev_id, peer_addr, ret2);
5140                         }
5141                         goto exit;
5142                 }
5143         }
5144
5145         ath10k_set_key_h_def_keyidx(ar, arvif, cmd, key);
5146
5147         spin_lock_bh(&ar->data_lock);
5148         peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
5149         if (peer && cmd == SET_KEY)
5150                 peer->keys[key->keyidx] = key;
5151         else if (peer && cmd == DISABLE_KEY)
5152                 peer->keys[key->keyidx] = NULL;
5153         else if (peer == NULL)
5154                 /* impossible unless FW goes crazy */
5155                 ath10k_warn(ar, "Peer %pM disappeared!\n", peer_addr);
5156         spin_unlock_bh(&ar->data_lock);
5157
5158 exit:
5159         mutex_unlock(&ar->conf_mutex);
5160         return ret;
5161 }
5162
5163 static void ath10k_set_default_unicast_key(struct ieee80211_hw *hw,
5164                                            struct ieee80211_vif *vif,
5165                                            int keyidx)
5166 {
5167         struct ath10k *ar = hw->priv;
5168         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5169         int ret;
5170
5171         mutex_lock(&arvif->ar->conf_mutex);
5172
5173         if (arvif->ar->state != ATH10K_STATE_ON)
5174                 goto unlock;
5175
5176         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
5177                    arvif->vdev_id, keyidx);
5178
5179         ret = ath10k_wmi_vdev_set_param(arvif->ar,
5180                                         arvif->vdev_id,
5181                                         arvif->ar->wmi.vdev_param->def_keyid,
5182                                         keyidx);
5183
5184         if (ret) {
5185                 ath10k_warn(ar, "failed to update wep key index for vdev %d: %d\n",
5186                             arvif->vdev_id,
5187                             ret);
5188                 goto unlock;
5189         }
5190
5191         arvif->def_wep_key_idx = keyidx;
5192
5193 unlock:
5194         mutex_unlock(&arvif->ar->conf_mutex);
5195 }
5196
5197 static void ath10k_sta_rc_update_wk(struct work_struct *wk)
5198 {
5199         struct ath10k *ar;
5200         struct ath10k_vif *arvif;
5201         struct ath10k_sta *arsta;
5202         struct ieee80211_sta *sta;
5203         struct cfg80211_chan_def def;
5204         enum ieee80211_band band;
5205         const u8 *ht_mcs_mask;
5206         const u16 *vht_mcs_mask;
5207         u32 changed, bw, nss, smps;
5208         int err;
5209
5210         arsta = container_of(wk, struct ath10k_sta, update_wk);
5211         sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
5212         arvif = arsta->arvif;
5213         ar = arvif->ar;
5214
5215         if (WARN_ON(ath10k_mac_vif_chan(arvif->vif, &def)))
5216                 return;
5217
5218         band = def.chan->band;
5219         ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
5220         vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
5221
5222         spin_lock_bh(&ar->data_lock);
5223
5224         changed = arsta->changed;
5225         arsta->changed = 0;
5226
5227         bw = arsta->bw;
5228         nss = arsta->nss;
5229         smps = arsta->smps;
5230
5231         spin_unlock_bh(&ar->data_lock);
5232
5233         mutex_lock(&ar->conf_mutex);
5234
5235         nss = max_t(u32, 1, nss);
5236         nss = min(nss, max(ath10k_mac_max_ht_nss(ht_mcs_mask),
5237                            ath10k_mac_max_vht_nss(vht_mcs_mask)));
5238
5239         if (changed & IEEE80211_RC_BW_CHANGED) {
5240                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM peer bw %d\n",
5241                            sta->addr, bw);
5242
5243                 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
5244                                                 WMI_PEER_CHAN_WIDTH, bw);
5245                 if (err)
5246                         ath10k_warn(ar, "failed to update STA %pM peer bw %d: %d\n",
5247                                     sta->addr, bw, err);
5248         }
5249
5250         if (changed & IEEE80211_RC_NSS_CHANGED) {
5251                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM nss %d\n",
5252                            sta->addr, nss);
5253
5254                 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
5255                                                 WMI_PEER_NSS, nss);
5256                 if (err)
5257                         ath10k_warn(ar, "failed to update STA %pM nss %d: %d\n",
5258                                     sta->addr, nss, err);
5259         }
5260
5261         if (changed & IEEE80211_RC_SMPS_CHANGED) {
5262                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM smps %d\n",
5263                            sta->addr, smps);
5264
5265                 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
5266                                                 WMI_PEER_SMPS_STATE, smps);
5267                 if (err)
5268                         ath10k_warn(ar, "failed to update STA %pM smps %d: %d\n",
5269                                     sta->addr, smps, err);
5270         }
5271
5272         if (changed & IEEE80211_RC_SUPP_RATES_CHANGED ||
5273             changed & IEEE80211_RC_NSS_CHANGED) {
5274                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM supp rates/nss\n",
5275                            sta->addr);
5276
5277                 err = ath10k_station_assoc(ar, arvif->vif, sta, true);
5278                 if (err)
5279                         ath10k_warn(ar, "failed to reassociate station: %pM\n",
5280                                     sta->addr);
5281         }
5282
5283         mutex_unlock(&ar->conf_mutex);
5284 }
5285
5286 static int ath10k_mac_inc_num_stations(struct ath10k_vif *arvif,
5287                                        struct ieee80211_sta *sta)
5288 {
5289         struct ath10k *ar = arvif->ar;
5290
5291         lockdep_assert_held(&ar->conf_mutex);
5292
5293         if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
5294                 return 0;
5295
5296         if (ar->num_stations >= ar->max_num_stations)
5297                 return -ENOBUFS;
5298
5299         ar->num_stations++;
5300
5301         return 0;
5302 }
5303
5304 static void ath10k_mac_dec_num_stations(struct ath10k_vif *arvif,
5305                                         struct ieee80211_sta *sta)
5306 {
5307         struct ath10k *ar = arvif->ar;
5308
5309         lockdep_assert_held(&ar->conf_mutex);
5310
5311         if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
5312                 return;
5313
5314         ar->num_stations--;
5315 }
5316
5317 struct ath10k_mac_tdls_iter_data {
5318         u32 num_tdls_stations;
5319         struct ieee80211_vif *curr_vif;
5320 };
5321
5322 static void ath10k_mac_tdls_vif_stations_count_iter(void *data,
5323                                                     struct ieee80211_sta *sta)
5324 {
5325         struct ath10k_mac_tdls_iter_data *iter_data = data;
5326         struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
5327         struct ieee80211_vif *sta_vif = arsta->arvif->vif;
5328
5329         if (sta->tdls && sta_vif == iter_data->curr_vif)
5330                 iter_data->num_tdls_stations++;
5331 }
5332
5333 static int ath10k_mac_tdls_vif_stations_count(struct ieee80211_hw *hw,
5334                                               struct ieee80211_vif *vif)
5335 {
5336         struct ath10k_mac_tdls_iter_data data = {};
5337
5338         data.curr_vif = vif;
5339
5340         ieee80211_iterate_stations_atomic(hw,
5341                                           ath10k_mac_tdls_vif_stations_count_iter,
5342                                           &data);
5343         return data.num_tdls_stations;
5344 }
5345
5346 static void ath10k_mac_tdls_vifs_count_iter(void *data, u8 *mac,
5347                                             struct ieee80211_vif *vif)
5348 {
5349         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5350         int *num_tdls_vifs = data;
5351
5352         if (vif->type != NL80211_IFTYPE_STATION)
5353                 return;
5354
5355         if (ath10k_mac_tdls_vif_stations_count(arvif->ar->hw, vif) > 0)
5356                 (*num_tdls_vifs)++;
5357 }
5358
5359 static int ath10k_mac_tdls_vifs_count(struct ieee80211_hw *hw)
5360 {
5361         int num_tdls_vifs = 0;
5362
5363         ieee80211_iterate_active_interfaces_atomic(hw,
5364                                                    IEEE80211_IFACE_ITER_NORMAL,
5365                                                    ath10k_mac_tdls_vifs_count_iter,
5366                                                    &num_tdls_vifs);
5367         return num_tdls_vifs;
5368 }
5369
5370 static int ath10k_sta_state(struct ieee80211_hw *hw,
5371                             struct ieee80211_vif *vif,
5372                             struct ieee80211_sta *sta,
5373                             enum ieee80211_sta_state old_state,
5374                             enum ieee80211_sta_state new_state)
5375 {
5376         struct ath10k *ar = hw->priv;
5377         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5378         struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
5379         int ret = 0;
5380
5381         if (old_state == IEEE80211_STA_NOTEXIST &&
5382             new_state == IEEE80211_STA_NONE) {
5383                 memset(arsta, 0, sizeof(*arsta));
5384                 arsta->arvif = arvif;
5385                 INIT_WORK(&arsta->update_wk, ath10k_sta_rc_update_wk);
5386         }
5387
5388         /* cancel must be done outside the mutex to avoid deadlock */
5389         if ((old_state == IEEE80211_STA_NONE &&
5390              new_state == IEEE80211_STA_NOTEXIST))
5391                 cancel_work_sync(&arsta->update_wk);
5392
5393         mutex_lock(&ar->conf_mutex);
5394
5395         if (old_state == IEEE80211_STA_NOTEXIST &&
5396             new_state == IEEE80211_STA_NONE) {
5397                 /*
5398                  * New station addition.
5399                  */
5400                 enum wmi_peer_type peer_type = WMI_PEER_TYPE_DEFAULT;
5401                 u32 num_tdls_stations;
5402                 u32 num_tdls_vifs;
5403
5404                 ath10k_dbg(ar, ATH10K_DBG_MAC,
5405                            "mac vdev %d peer create %pM (new sta) sta %d / %d peer %d / %d\n",
5406                            arvif->vdev_id, sta->addr,
5407                            ar->num_stations + 1, ar->max_num_stations,
5408                            ar->num_peers + 1, ar->max_num_peers);
5409
5410                 ret = ath10k_mac_inc_num_stations(arvif, sta);
5411                 if (ret) {
5412                         ath10k_warn(ar, "refusing to associate station: too many connected already (%d)\n",
5413                                     ar->max_num_stations);
5414                         goto exit;
5415                 }
5416
5417                 if (sta->tdls)
5418                         peer_type = WMI_PEER_TYPE_TDLS;
5419
5420                 ret = ath10k_peer_create(ar, arvif->vdev_id, sta->addr,
5421                                          peer_type);
5422                 if (ret) {
5423                         ath10k_warn(ar, "failed to add peer %pM for vdev %d when adding a new sta: %i\n",
5424                                     sta->addr, arvif->vdev_id, ret);
5425                         ath10k_mac_dec_num_stations(arvif, sta);
5426                         goto exit;
5427                 }
5428
5429                 if (!sta->tdls)
5430                         goto exit;
5431
5432                 num_tdls_stations = ath10k_mac_tdls_vif_stations_count(hw, vif);
5433                 num_tdls_vifs = ath10k_mac_tdls_vifs_count(hw);
5434
5435                 if (num_tdls_vifs >= ar->max_num_tdls_vdevs &&
5436                     num_tdls_stations == 0) {
5437                         ath10k_warn(ar, "vdev %i exceeded maximum number of tdls vdevs %i\n",
5438                                     arvif->vdev_id, ar->max_num_tdls_vdevs);
5439                         ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
5440                         ath10k_mac_dec_num_stations(arvif, sta);
5441                         ret = -ENOBUFS;
5442                         goto exit;
5443                 }
5444
5445                 if (num_tdls_stations == 0) {
5446                         /* This is the first tdls peer in current vif */
5447                         enum wmi_tdls_state state = WMI_TDLS_ENABLE_ACTIVE;
5448
5449                         ret = ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
5450                                                               state);
5451                         if (ret) {
5452                                 ath10k_warn(ar, "failed to update fw tdls state on vdev %i: %i\n",
5453                                             arvif->vdev_id, ret);
5454                                 ath10k_peer_delete(ar, arvif->vdev_id,
5455                                                    sta->addr);
5456                                 ath10k_mac_dec_num_stations(arvif, sta);
5457                                 goto exit;
5458                         }
5459                 }
5460
5461                 ret = ath10k_mac_tdls_peer_update(ar, arvif->vdev_id, sta,
5462                                                   WMI_TDLS_PEER_STATE_PEERING);
5463                 if (ret) {
5464                         ath10k_warn(ar,
5465                                     "failed to update tdls peer %pM for vdev %d when adding a new sta: %i\n",
5466                                     sta->addr, arvif->vdev_id, ret);
5467                         ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
5468                         ath10k_mac_dec_num_stations(arvif, sta);
5469
5470                         if (num_tdls_stations != 0)
5471                                 goto exit;
5472                         ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
5473                                                         WMI_TDLS_DISABLE);
5474                 }
5475         } else if ((old_state == IEEE80211_STA_NONE &&
5476                     new_state == IEEE80211_STA_NOTEXIST)) {
5477                 /*
5478                  * Existing station deletion.
5479                  */
5480                 ath10k_dbg(ar, ATH10K_DBG_MAC,
5481                            "mac vdev %d peer delete %pM (sta gone)\n",
5482                            arvif->vdev_id, sta->addr);
5483
5484                 ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
5485                 if (ret)
5486                         ath10k_warn(ar, "failed to delete peer %pM for vdev %d: %i\n",
5487                                     sta->addr, arvif->vdev_id, ret);
5488
5489                 ath10k_mac_dec_num_stations(arvif, sta);
5490
5491                 if (!sta->tdls)
5492                         goto exit;
5493
5494                 if (ath10k_mac_tdls_vif_stations_count(hw, vif))
5495                         goto exit;
5496
5497                 /* This was the last tdls peer in current vif */
5498                 ret = ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
5499                                                       WMI_TDLS_DISABLE);
5500                 if (ret) {
5501                         ath10k_warn(ar, "failed to update fw tdls state on vdev %i: %i\n",
5502                                     arvif->vdev_id, ret);
5503                 }
5504         } else if (old_state == IEEE80211_STA_AUTH &&
5505                    new_state == IEEE80211_STA_ASSOC &&
5506                    (vif->type == NL80211_IFTYPE_AP ||
5507                     vif->type == NL80211_IFTYPE_MESH_POINT ||
5508                     vif->type == NL80211_IFTYPE_ADHOC)) {
5509                 /*
5510                  * New association.
5511                  */
5512                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM associated\n",
5513                            sta->addr);
5514
5515                 ret = ath10k_station_assoc(ar, vif, sta, false);
5516                 if (ret)
5517                         ath10k_warn(ar, "failed to associate station %pM for vdev %i: %i\n",
5518                                     sta->addr, arvif->vdev_id, ret);
5519         } else if (old_state == IEEE80211_STA_ASSOC &&
5520                    new_state == IEEE80211_STA_AUTHORIZED &&
5521                    sta->tdls) {
5522                 /*
5523                  * Tdls station authorized.
5524                  */
5525                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac tdls sta %pM authorized\n",
5526                            sta->addr);
5527
5528                 ret = ath10k_station_assoc(ar, vif, sta, false);
5529                 if (ret) {
5530                         ath10k_warn(ar, "failed to associate tdls station %pM for vdev %i: %i\n",
5531                                     sta->addr, arvif->vdev_id, ret);
5532                         goto exit;
5533                 }
5534
5535                 ret = ath10k_mac_tdls_peer_update(ar, arvif->vdev_id, sta,
5536                                                   WMI_TDLS_PEER_STATE_CONNECTED);
5537                 if (ret)
5538                         ath10k_warn(ar, "failed to update tdls peer %pM for vdev %i: %i\n",
5539                                     sta->addr, arvif->vdev_id, ret);
5540         } else if (old_state == IEEE80211_STA_ASSOC &&
5541                     new_state == IEEE80211_STA_AUTH &&
5542                     (vif->type == NL80211_IFTYPE_AP ||
5543                      vif->type == NL80211_IFTYPE_MESH_POINT ||
5544                      vif->type == NL80211_IFTYPE_ADHOC)) {
5545                 /*
5546                  * Disassociation.
5547                  */
5548                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM disassociated\n",
5549                            sta->addr);
5550
5551                 ret = ath10k_station_disassoc(ar, vif, sta);
5552                 if (ret)
5553                         ath10k_warn(ar, "failed to disassociate station: %pM vdev %i: %i\n",
5554                                     sta->addr, arvif->vdev_id, ret);
5555         }
5556 exit:
5557         mutex_unlock(&ar->conf_mutex);
5558         return ret;
5559 }
5560
5561 static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
5562                                 u16 ac, bool enable)
5563 {
5564         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5565         struct wmi_sta_uapsd_auto_trig_arg arg = {};
5566         u32 prio = 0, acc = 0;
5567         u32 value = 0;
5568         int ret = 0;
5569
5570         lockdep_assert_held(&ar->conf_mutex);
5571
5572         if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
5573                 return 0;
5574
5575         switch (ac) {
5576         case IEEE80211_AC_VO:
5577                 value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
5578                         WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
5579                 prio = 7;
5580                 acc = 3;
5581                 break;
5582         case IEEE80211_AC_VI:
5583                 value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
5584                         WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
5585                 prio = 5;
5586                 acc = 2;
5587                 break;
5588         case IEEE80211_AC_BE:
5589                 value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
5590                         WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
5591                 prio = 2;
5592                 acc = 1;
5593                 break;
5594         case IEEE80211_AC_BK:
5595                 value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
5596                         WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
5597                 prio = 0;
5598                 acc = 0;
5599                 break;
5600         }
5601
5602         if (enable)
5603                 arvif->u.sta.uapsd |= value;
5604         else
5605                 arvif->u.sta.uapsd &= ~value;
5606
5607         ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
5608                                           WMI_STA_PS_PARAM_UAPSD,
5609                                           arvif->u.sta.uapsd);
5610         if (ret) {
5611                 ath10k_warn(ar, "failed to set uapsd params: %d\n", ret);
5612                 goto exit;
5613         }
5614
5615         if (arvif->u.sta.uapsd)
5616                 value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
5617         else
5618                 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
5619
5620         ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
5621                                           WMI_STA_PS_PARAM_RX_WAKE_POLICY,
5622                                           value);
5623         if (ret)
5624                 ath10k_warn(ar, "failed to set rx wake param: %d\n", ret);
5625
5626         ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
5627         if (ret) {
5628                 ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
5629                             arvif->vdev_id, ret);
5630                 return ret;
5631         }
5632
5633         ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
5634         if (ret) {
5635                 ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
5636                             arvif->vdev_id, ret);
5637                 return ret;
5638         }
5639
5640         if (test_bit(WMI_SERVICE_STA_UAPSD_BASIC_AUTO_TRIG, ar->wmi.svc_map) ||
5641             test_bit(WMI_SERVICE_STA_UAPSD_VAR_AUTO_TRIG, ar->wmi.svc_map)) {
5642                 /* Only userspace can make an educated decision when to send
5643                  * trigger frame. The following effectively disables u-UAPSD
5644                  * autotrigger in firmware (which is enabled by default
5645                  * provided the autotrigger service is available).
5646                  */
5647
5648                 arg.wmm_ac = acc;
5649                 arg.user_priority = prio;
5650                 arg.service_interval = 0;
5651                 arg.suspend_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
5652                 arg.delay_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
5653
5654                 ret = ath10k_wmi_vdev_sta_uapsd(ar, arvif->vdev_id,
5655                                                 arvif->bssid, &arg, 1);
5656                 if (ret) {
5657                         ath10k_warn(ar, "failed to set uapsd auto trigger %d\n",
5658                                     ret);
5659                         return ret;
5660                 }
5661         }
5662
5663 exit:
5664         return ret;
5665 }
5666
5667 static int ath10k_conf_tx(struct ieee80211_hw *hw,
5668                           struct ieee80211_vif *vif, u16 ac,
5669                           const struct ieee80211_tx_queue_params *params)
5670 {
5671         struct ath10k *ar = hw->priv;
5672         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5673         struct wmi_wmm_params_arg *p = NULL;
5674         int ret;
5675
5676         mutex_lock(&ar->conf_mutex);
5677
5678         switch (ac) {
5679         case IEEE80211_AC_VO:
5680                 p = &arvif->wmm_params.ac_vo;
5681                 break;
5682         case IEEE80211_AC_VI:
5683                 p = &arvif->wmm_params.ac_vi;
5684                 break;
5685         case IEEE80211_AC_BE:
5686                 p = &arvif->wmm_params.ac_be;
5687                 break;
5688         case IEEE80211_AC_BK:
5689                 p = &arvif->wmm_params.ac_bk;
5690                 break;
5691         }
5692
5693         if (WARN_ON(!p)) {
5694                 ret = -EINVAL;
5695                 goto exit;
5696         }
5697
5698         p->cwmin = params->cw_min;
5699         p->cwmax = params->cw_max;
5700         p->aifs = params->aifs;
5701
5702         /*
5703          * The channel time duration programmed in the HW is in absolute
5704          * microseconds, while mac80211 gives the txop in units of
5705          * 32 microseconds.
5706          */
5707         p->txop = params->txop * 32;
5708
5709         if (ar->wmi.ops->gen_vdev_wmm_conf) {
5710                 ret = ath10k_wmi_vdev_wmm_conf(ar, arvif->vdev_id,
5711                                                &arvif->wmm_params);
5712                 if (ret) {
5713                         ath10k_warn(ar, "failed to set vdev wmm params on vdev %i: %d\n",
5714                                     arvif->vdev_id, ret);
5715                         goto exit;
5716                 }
5717         } else {
5718                 /* This won't work well with multi-interface cases but it's
5719                  * better than nothing.
5720                  */
5721                 ret = ath10k_wmi_pdev_set_wmm_params(ar, &arvif->wmm_params);
5722                 if (ret) {
5723                         ath10k_warn(ar, "failed to set wmm params: %d\n", ret);
5724                         goto exit;
5725                 }
5726         }
5727
5728         ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
5729         if (ret)
5730                 ath10k_warn(ar, "failed to set sta uapsd: %d\n", ret);
5731
5732 exit:
5733         mutex_unlock(&ar->conf_mutex);
5734         return ret;
5735 }
5736
5737 #define ATH10K_ROC_TIMEOUT_HZ (2*HZ)
5738
5739 static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
5740                                     struct ieee80211_vif *vif,
5741                                     struct ieee80211_channel *chan,
5742                                     int duration,
5743                                     enum ieee80211_roc_type type)
5744 {
5745         struct ath10k *ar = hw->priv;
5746         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5747         struct wmi_start_scan_arg arg;
5748         int ret = 0;
5749         u32 scan_time_msec;
5750
5751         mutex_lock(&ar->conf_mutex);
5752
5753         spin_lock_bh(&ar->data_lock);
5754         switch (ar->scan.state) {
5755         case ATH10K_SCAN_IDLE:
5756                 reinit_completion(&ar->scan.started);
5757                 reinit_completion(&ar->scan.completed);
5758                 reinit_completion(&ar->scan.on_channel);
5759                 ar->scan.state = ATH10K_SCAN_STARTING;
5760                 ar->scan.is_roc = true;
5761                 ar->scan.vdev_id = arvif->vdev_id;
5762                 ar->scan.roc_freq = chan->center_freq;
5763                 ar->scan.roc_notify = true;
5764                 ret = 0;
5765                 break;
5766         case ATH10K_SCAN_STARTING:
5767         case ATH10K_SCAN_RUNNING:
5768         case ATH10K_SCAN_ABORTING:
5769                 ret = -EBUSY;
5770                 break;
5771         }
5772         spin_unlock_bh(&ar->data_lock);
5773
5774         if (ret)
5775                 goto exit;
5776
5777         scan_time_msec = ar->hw->wiphy->max_remain_on_channel_duration * 2;
5778
5779         memset(&arg, 0, sizeof(arg));
5780         ath10k_wmi_start_scan_init(ar, &arg);
5781         arg.vdev_id = arvif->vdev_id;
5782         arg.scan_id = ATH10K_SCAN_ID;
5783         arg.n_channels = 1;
5784         arg.channels[0] = chan->center_freq;
5785         arg.dwell_time_active = scan_time_msec;
5786         arg.dwell_time_passive = scan_time_msec;
5787         arg.max_scan_time = scan_time_msec;
5788         arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
5789         arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
5790         arg.burst_duration_ms = duration;
5791
5792         ret = ath10k_start_scan(ar, &arg);
5793         if (ret) {
5794                 ath10k_warn(ar, "failed to start roc scan: %d\n", ret);
5795                 spin_lock_bh(&ar->data_lock);
5796                 ar->scan.state = ATH10K_SCAN_IDLE;
5797                 spin_unlock_bh(&ar->data_lock);
5798                 goto exit;
5799         }
5800
5801         ret = wait_for_completion_timeout(&ar->scan.on_channel, 3*HZ);
5802         if (ret == 0) {
5803                 ath10k_warn(ar, "failed to switch to channel for roc scan\n");
5804
5805                 ret = ath10k_scan_stop(ar);
5806                 if (ret)
5807                         ath10k_warn(ar, "failed to stop scan: %d\n", ret);
5808
5809                 ret = -ETIMEDOUT;
5810                 goto exit;
5811         }
5812
5813         ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
5814                                      msecs_to_jiffies(duration));
5815
5816         ret = 0;
5817 exit:
5818         mutex_unlock(&ar->conf_mutex);
5819         return ret;
5820 }
5821
5822 static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw)
5823 {
5824         struct ath10k *ar = hw->priv;
5825
5826         mutex_lock(&ar->conf_mutex);
5827
5828         spin_lock_bh(&ar->data_lock);
5829         ar->scan.roc_notify = false;
5830         spin_unlock_bh(&ar->data_lock);
5831
5832         ath10k_scan_abort(ar);
5833
5834         mutex_unlock(&ar->conf_mutex);
5835
5836         cancel_delayed_work_sync(&ar->scan.timeout);
5837
5838         return 0;
5839 }
5840
5841 /*
5842  * Both RTS and Fragmentation threshold are interface-specific
5843  * in ath10k, but device-specific in mac80211.
5844  */
5845
5846 static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
5847 {
5848         struct ath10k *ar = hw->priv;
5849         struct ath10k_vif *arvif;
5850         int ret = 0;
5851
5852         mutex_lock(&ar->conf_mutex);
5853         list_for_each_entry(arvif, &ar->arvifs, list) {
5854                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
5855                            arvif->vdev_id, value);
5856
5857                 ret = ath10k_mac_set_rts(arvif, value);
5858                 if (ret) {
5859                         ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
5860                                     arvif->vdev_id, ret);
5861                         break;
5862                 }
5863         }
5864         mutex_unlock(&ar->conf_mutex);
5865
5866         return ret;
5867 }
5868
5869 static int ath10k_mac_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
5870 {
5871         /* Even though there's a WMI enum for fragmentation threshold no known
5872          * firmware actually implements it. Moreover it is not possible to rely
5873          * frame fragmentation to mac80211 because firmware clears the "more
5874          * fragments" bit in frame control making it impossible for remote
5875          * devices to reassemble frames.
5876          *
5877          * Hence implement a dummy callback just to say fragmentation isn't
5878          * supported. This effectively prevents mac80211 from doing frame
5879          * fragmentation in software.
5880          */
5881         return -EOPNOTSUPP;
5882 }
5883
5884 static void ath10k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5885                          u32 queues, bool drop)
5886 {
5887         struct ath10k *ar = hw->priv;
5888         bool skip;
5889         long time_left;
5890
5891         /* mac80211 doesn't care if we really xmit queued frames or not
5892          * we'll collect those frames either way if we stop/delete vdevs */
5893         if (drop)
5894                 return;
5895
5896         mutex_lock(&ar->conf_mutex);
5897
5898         if (ar->state == ATH10K_STATE_WEDGED)
5899                 goto skip;
5900
5901         time_left = wait_event_timeout(ar->htt.empty_tx_wq, ({
5902                         bool empty;
5903
5904                         spin_lock_bh(&ar->htt.tx_lock);
5905                         empty = (ar->htt.num_pending_tx == 0);
5906                         spin_unlock_bh(&ar->htt.tx_lock);
5907
5908                         skip = (ar->state == ATH10K_STATE_WEDGED) ||
5909                                test_bit(ATH10K_FLAG_CRASH_FLUSH,
5910                                         &ar->dev_flags);
5911
5912                         (empty || skip);
5913                 }), ATH10K_FLUSH_TIMEOUT_HZ);
5914
5915         if (time_left == 0 || skip)
5916                 ath10k_warn(ar, "failed to flush transmit queue (skip %i ar-state %i): %ld\n",
5917                             skip, ar->state, time_left);
5918
5919 skip:
5920         mutex_unlock(&ar->conf_mutex);
5921 }
5922
5923 /* TODO: Implement this function properly
5924  * For now it is needed to reply to Probe Requests in IBSS mode.
5925  * Propably we need this information from FW.
5926  */
5927 static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
5928 {
5929         return 1;
5930 }
5931
5932 static void ath10k_reconfig_complete(struct ieee80211_hw *hw,
5933                                      enum ieee80211_reconfig_type reconfig_type)
5934 {
5935         struct ath10k *ar = hw->priv;
5936
5937         if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
5938                 return;
5939
5940         mutex_lock(&ar->conf_mutex);
5941
5942         /* If device failed to restart it will be in a different state, e.g.
5943          * ATH10K_STATE_WEDGED */
5944         if (ar->state == ATH10K_STATE_RESTARTED) {
5945                 ath10k_info(ar, "device successfully recovered\n");
5946                 ar->state = ATH10K_STATE_ON;
5947                 ieee80211_wake_queues(ar->hw);
5948         }
5949
5950         mutex_unlock(&ar->conf_mutex);
5951 }
5952
5953 static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
5954                              struct survey_info *survey)
5955 {
5956         struct ath10k *ar = hw->priv;
5957         struct ieee80211_supported_band *sband;
5958         struct survey_info *ar_survey = &ar->survey[idx];
5959         int ret = 0;
5960
5961         mutex_lock(&ar->conf_mutex);
5962
5963         sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
5964         if (sband && idx >= sband->n_channels) {
5965                 idx -= sband->n_channels;
5966                 sband = NULL;
5967         }
5968
5969         if (!sband)
5970                 sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
5971
5972         if (!sband || idx >= sband->n_channels) {
5973                 ret = -ENOENT;
5974                 goto exit;
5975         }
5976
5977         spin_lock_bh(&ar->data_lock);
5978         memcpy(survey, ar_survey, sizeof(*survey));
5979         spin_unlock_bh(&ar->data_lock);
5980
5981         survey->channel = &sband->channels[idx];
5982
5983         if (ar->rx_channel == survey->channel)
5984                 survey->filled |= SURVEY_INFO_IN_USE;
5985
5986 exit:
5987         mutex_unlock(&ar->conf_mutex);
5988         return ret;
5989 }
5990
5991 static bool
5992 ath10k_mac_bitrate_mask_has_single_rate(struct ath10k *ar,
5993                                         enum ieee80211_band band,
5994                                         const struct cfg80211_bitrate_mask *mask)
5995 {
5996         int num_rates = 0;
5997         int i;
5998
5999         num_rates += hweight32(mask->control[band].legacy);
6000
6001         for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
6002                 num_rates += hweight8(mask->control[band].ht_mcs[i]);
6003
6004         for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
6005                 num_rates += hweight16(mask->control[band].vht_mcs[i]);
6006
6007         return num_rates == 1;
6008 }
6009
6010 static bool
6011 ath10k_mac_bitrate_mask_get_single_nss(struct ath10k *ar,
6012                                        enum ieee80211_band band,
6013                                        const struct cfg80211_bitrate_mask *mask,
6014                                        int *nss)
6015 {
6016         struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
6017         u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
6018         u8 ht_nss_mask = 0;
6019         u8 vht_nss_mask = 0;
6020         int i;
6021
6022         if (mask->control[band].legacy)
6023                 return false;
6024
6025         for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
6026                 if (mask->control[band].ht_mcs[i] == 0)
6027                         continue;
6028                 else if (mask->control[band].ht_mcs[i] ==
6029                          sband->ht_cap.mcs.rx_mask[i])
6030                         ht_nss_mask |= BIT(i);
6031                 else
6032                         return false;
6033         }
6034
6035         for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
6036                 if (mask->control[band].vht_mcs[i] == 0)
6037                         continue;
6038                 else if (mask->control[band].vht_mcs[i] ==
6039                          ath10k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
6040                         vht_nss_mask |= BIT(i);
6041                 else
6042                         return false;
6043         }
6044
6045         if (ht_nss_mask != vht_nss_mask)
6046                 return false;
6047
6048         if (ht_nss_mask == 0)
6049                 return false;
6050
6051         if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
6052                 return false;
6053
6054         *nss = fls(ht_nss_mask);
6055
6056         return true;
6057 }
6058
6059 static int
6060 ath10k_mac_bitrate_mask_get_single_rate(struct ath10k *ar,
6061                                         enum ieee80211_band band,
6062                                         const struct cfg80211_bitrate_mask *mask,
6063                                         u8 *rate, u8 *nss)
6064 {
6065         struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
6066         int rate_idx;
6067         int i;
6068         u16 bitrate;
6069         u8 preamble;
6070         u8 hw_rate;
6071
6072         if (hweight32(mask->control[band].legacy) == 1) {
6073                 rate_idx = ffs(mask->control[band].legacy) - 1;
6074
6075                 hw_rate = sband->bitrates[rate_idx].hw_value;
6076                 bitrate = sband->bitrates[rate_idx].bitrate;
6077
6078                 if (ath10k_mac_bitrate_is_cck(bitrate))
6079                         preamble = WMI_RATE_PREAMBLE_CCK;
6080                 else
6081                         preamble = WMI_RATE_PREAMBLE_OFDM;
6082
6083                 *nss = 1;
6084                 *rate = preamble << 6 |
6085                         (*nss - 1) << 4 |
6086                         hw_rate << 0;
6087
6088                 return 0;
6089         }
6090
6091         for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
6092                 if (hweight8(mask->control[band].ht_mcs[i]) == 1) {
6093                         *nss = i + 1;
6094                         *rate = WMI_RATE_PREAMBLE_HT << 6 |
6095                                 (*nss - 1) << 4 |
6096                                 (ffs(mask->control[band].ht_mcs[i]) - 1);
6097
6098                         return 0;
6099                 }
6100         }
6101
6102         for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
6103                 if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
6104                         *nss = i + 1;
6105                         *rate = WMI_RATE_PREAMBLE_VHT << 6 |
6106                                 (*nss - 1) << 4 |
6107                                 (ffs(mask->control[band].vht_mcs[i]) - 1);
6108
6109                         return 0;
6110                 }
6111         }
6112
6113         return -EINVAL;
6114 }
6115
6116 static int ath10k_mac_set_fixed_rate_params(struct ath10k_vif *arvif,
6117                                             u8 rate, u8 nss, u8 sgi, u8 ldpc)
6118 {
6119         struct ath10k *ar = arvif->ar;
6120         u32 vdev_param;
6121         int ret;
6122
6123         lockdep_assert_held(&ar->conf_mutex);
6124
6125         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac set fixed rate params vdev %i rate 0x%02hhx nss %hhu sgi %hhu\n",
6126                    arvif->vdev_id, rate, nss, sgi);
6127
6128         vdev_param = ar->wmi.vdev_param->fixed_rate;
6129         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, rate);
6130         if (ret) {
6131                 ath10k_warn(ar, "failed to set fixed rate param 0x%02x: %d\n",
6132                             rate, ret);
6133                 return ret;
6134         }
6135
6136         vdev_param = ar->wmi.vdev_param->nss;
6137         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, nss);
6138         if (ret) {
6139                 ath10k_warn(ar, "failed to set nss param %d: %d\n", nss, ret);
6140                 return ret;
6141         }
6142
6143         vdev_param = ar->wmi.vdev_param->sgi;
6144         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, sgi);
6145         if (ret) {
6146                 ath10k_warn(ar, "failed to set sgi param %d: %d\n", sgi, ret);
6147                 return ret;
6148         }
6149
6150         vdev_param = ar->wmi.vdev_param->ldpc;
6151         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, ldpc);
6152         if (ret) {
6153                 ath10k_warn(ar, "failed to set ldpc param %d: %d\n", ldpc, ret);
6154                 return ret;
6155         }
6156
6157         return 0;
6158 }
6159
6160 static bool
6161 ath10k_mac_can_set_bitrate_mask(struct ath10k *ar,
6162                                 enum ieee80211_band band,
6163                                 const struct cfg80211_bitrate_mask *mask)
6164 {
6165         int i;
6166         u16 vht_mcs;
6167
6168         /* Due to firmware limitation in WMI_PEER_ASSOC_CMDID it is impossible
6169          * to express all VHT MCS rate masks. Effectively only the following
6170          * ranges can be used: none, 0-7, 0-8 and 0-9.
6171          */
6172         for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
6173                 vht_mcs = mask->control[band].vht_mcs[i];
6174
6175                 switch (vht_mcs) {
6176                 case 0:
6177                 case BIT(8) - 1:
6178                 case BIT(9) - 1:
6179                 case BIT(10) - 1:
6180                         break;
6181                 default:
6182                         ath10k_warn(ar, "refusing bitrate mask with missing 0-7 VHT MCS rates\n");
6183                         return false;
6184                 }
6185         }
6186
6187         return true;
6188 }
6189
6190 static void ath10k_mac_set_bitrate_mask_iter(void *data,
6191                                              struct ieee80211_sta *sta)
6192 {
6193         struct ath10k_vif *arvif = data;
6194         struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
6195         struct ath10k *ar = arvif->ar;
6196
6197         if (arsta->arvif != arvif)
6198                 return;
6199
6200         spin_lock_bh(&ar->data_lock);
6201         arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
6202         spin_unlock_bh(&ar->data_lock);
6203
6204         ieee80211_queue_work(ar->hw, &arsta->update_wk);
6205 }
6206
6207 static int ath10k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
6208                                           struct ieee80211_vif *vif,
6209                                           const struct cfg80211_bitrate_mask *mask)
6210 {
6211         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
6212         struct cfg80211_chan_def def;
6213         struct ath10k *ar = arvif->ar;
6214         enum ieee80211_band band;
6215         const u8 *ht_mcs_mask;
6216         const u16 *vht_mcs_mask;
6217         u8 rate;
6218         u8 nss;
6219         u8 sgi;
6220         u8 ldpc;
6221         int single_nss;
6222         int ret;
6223
6224         if (ath10k_mac_vif_chan(vif, &def))
6225                 return -EPERM;
6226
6227         band = def.chan->band;
6228         ht_mcs_mask = mask->control[band].ht_mcs;
6229         vht_mcs_mask = mask->control[band].vht_mcs;
6230         ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
6231
6232         sgi = mask->control[band].gi;
6233         if (sgi == NL80211_TXRATE_FORCE_LGI)
6234                 return -EINVAL;
6235
6236         if (ath10k_mac_bitrate_mask_has_single_rate(ar, band, mask)) {
6237                 ret = ath10k_mac_bitrate_mask_get_single_rate(ar, band, mask,
6238                                                               &rate, &nss);
6239                 if (ret) {
6240                         ath10k_warn(ar, "failed to get single rate for vdev %i: %d\n",
6241                                     arvif->vdev_id, ret);
6242                         return ret;
6243                 }
6244         } else if (ath10k_mac_bitrate_mask_get_single_nss(ar, band, mask,
6245                                                           &single_nss)) {
6246                 rate = WMI_FIXED_RATE_NONE;
6247                 nss = single_nss;
6248         } else {
6249                 rate = WMI_FIXED_RATE_NONE;
6250                 nss = min(ar->num_rf_chains,
6251                           max(ath10k_mac_max_ht_nss(ht_mcs_mask),
6252                               ath10k_mac_max_vht_nss(vht_mcs_mask)));
6253
6254                 if (!ath10k_mac_can_set_bitrate_mask(ar, band, mask))
6255                         return -EINVAL;
6256
6257                 mutex_lock(&ar->conf_mutex);
6258
6259                 arvif->bitrate_mask = *mask;
6260                 ieee80211_iterate_stations_atomic(ar->hw,
6261                                                   ath10k_mac_set_bitrate_mask_iter,
6262                                                   arvif);
6263
6264                 mutex_unlock(&ar->conf_mutex);
6265         }
6266
6267         mutex_lock(&ar->conf_mutex);
6268
6269         ret = ath10k_mac_set_fixed_rate_params(arvif, rate, nss, sgi, ldpc);
6270         if (ret) {
6271                 ath10k_warn(ar, "failed to set fixed rate params on vdev %i: %d\n",
6272                             arvif->vdev_id, ret);
6273                 goto exit;
6274         }
6275
6276 exit:
6277         mutex_unlock(&ar->conf_mutex);
6278
6279         return ret;
6280 }
6281
6282 static void ath10k_sta_rc_update(struct ieee80211_hw *hw,
6283                                  struct ieee80211_vif *vif,
6284                                  struct ieee80211_sta *sta,
6285                                  u32 changed)
6286 {
6287         struct ath10k *ar = hw->priv;
6288         struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
6289         u32 bw, smps;
6290
6291         spin_lock_bh(&ar->data_lock);
6292
6293         ath10k_dbg(ar, ATH10K_DBG_MAC,
6294                    "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
6295                    sta->addr, changed, sta->bandwidth, sta->rx_nss,
6296                    sta->smps_mode);
6297
6298         if (changed & IEEE80211_RC_BW_CHANGED) {
6299                 bw = WMI_PEER_CHWIDTH_20MHZ;
6300
6301                 switch (sta->bandwidth) {
6302                 case IEEE80211_STA_RX_BW_20:
6303                         bw = WMI_PEER_CHWIDTH_20MHZ;
6304                         break;
6305                 case IEEE80211_STA_RX_BW_40:
6306                         bw = WMI_PEER_CHWIDTH_40MHZ;
6307                         break;
6308                 case IEEE80211_STA_RX_BW_80:
6309                         bw = WMI_PEER_CHWIDTH_80MHZ;
6310                         break;
6311                 case IEEE80211_STA_RX_BW_160:
6312                         ath10k_warn(ar, "Invalid bandwidth %d in rc update for %pM\n",
6313                                     sta->bandwidth, sta->addr);
6314                         bw = WMI_PEER_CHWIDTH_20MHZ;
6315                         break;
6316                 }
6317
6318                 arsta->bw = bw;
6319         }
6320
6321         if (changed & IEEE80211_RC_NSS_CHANGED)
6322                 arsta->nss = sta->rx_nss;
6323
6324         if (changed & IEEE80211_RC_SMPS_CHANGED) {
6325                 smps = WMI_PEER_SMPS_PS_NONE;
6326
6327                 switch (sta->smps_mode) {
6328                 case IEEE80211_SMPS_AUTOMATIC:
6329                 case IEEE80211_SMPS_OFF:
6330                         smps = WMI_PEER_SMPS_PS_NONE;
6331                         break;
6332                 case IEEE80211_SMPS_STATIC:
6333                         smps = WMI_PEER_SMPS_STATIC;
6334                         break;
6335                 case IEEE80211_SMPS_DYNAMIC:
6336                         smps = WMI_PEER_SMPS_DYNAMIC;
6337                         break;
6338                 case IEEE80211_SMPS_NUM_MODES:
6339                         ath10k_warn(ar, "Invalid smps %d in sta rc update for %pM\n",
6340                                     sta->smps_mode, sta->addr);
6341                         smps = WMI_PEER_SMPS_PS_NONE;
6342                         break;
6343                 }
6344
6345                 arsta->smps = smps;
6346         }
6347
6348         arsta->changed |= changed;
6349
6350         spin_unlock_bh(&ar->data_lock);
6351
6352         ieee80211_queue_work(hw, &arsta->update_wk);
6353 }
6354
6355 static u64 ath10k_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
6356 {
6357         /*
6358          * FIXME: Return 0 for time being. Need to figure out whether FW
6359          * has the API to fetch 64-bit local TSF
6360          */
6361
6362         return 0;
6363 }
6364
6365 static int ath10k_ampdu_action(struct ieee80211_hw *hw,
6366                                struct ieee80211_vif *vif,
6367                                enum ieee80211_ampdu_mlme_action action,
6368                                struct ieee80211_sta *sta, u16 tid, u16 *ssn,
6369                                u8 buf_size, bool amsdu)
6370 {
6371         struct ath10k *ar = hw->priv;
6372         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
6373
6374         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ampdu vdev_id %i sta %pM tid %hu action %d\n",
6375                    arvif->vdev_id, sta->addr, tid, action);
6376
6377         switch (action) {
6378         case IEEE80211_AMPDU_RX_START:
6379         case IEEE80211_AMPDU_RX_STOP:
6380                 /* HTT AddBa/DelBa events trigger mac80211 Rx BA session
6381                  * creation/removal. Do we need to verify this?
6382                  */
6383                 return 0;
6384         case IEEE80211_AMPDU_TX_START:
6385         case IEEE80211_AMPDU_TX_STOP_CONT:
6386         case IEEE80211_AMPDU_TX_STOP_FLUSH:
6387         case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
6388         case IEEE80211_AMPDU_TX_OPERATIONAL:
6389                 /* Firmware offloads Tx aggregation entirely so deny mac80211
6390                  * Tx aggregation requests.
6391                  */
6392                 return -EOPNOTSUPP;
6393         }
6394
6395         return -EINVAL;
6396 }
6397
6398 static void
6399 ath10k_mac_update_rx_channel(struct ath10k *ar,
6400                              struct ieee80211_chanctx_conf *ctx,
6401                              struct ieee80211_vif_chanctx_switch *vifs,
6402                              int n_vifs)
6403 {
6404         struct cfg80211_chan_def *def = NULL;
6405
6406         /* Both locks are required because ar->rx_channel is modified. This
6407          * allows readers to hold either lock.
6408          */
6409         lockdep_assert_held(&ar->conf_mutex);
6410         lockdep_assert_held(&ar->data_lock);
6411
6412         WARN_ON(ctx && vifs);
6413         WARN_ON(vifs && n_vifs != 1);
6414
6415         /* FIXME: Sort of an optimization and a workaround. Peers and vifs are
6416          * on a linked list now. Doing a lookup peer -> vif -> chanctx for each
6417          * ppdu on Rx may reduce performance on low-end systems. It should be
6418          * possible to make tables/hashmaps to speed the lookup up (be vary of
6419          * cpu data cache lines though regarding sizes) but to keep the initial
6420          * implementation simple and less intrusive fallback to the slow lookup
6421          * only for multi-channel cases. Single-channel cases will remain to
6422          * use the old channel derival and thus performance should not be
6423          * affected much.
6424          */
6425         rcu_read_lock();
6426         if (!ctx && ath10k_mac_num_chanctxs(ar) == 1) {
6427                 ieee80211_iter_chan_contexts_atomic(ar->hw,
6428                                                     ath10k_mac_get_any_chandef_iter,
6429                                                     &def);
6430
6431                 if (vifs)
6432                         def = &vifs[0].new_ctx->def;
6433
6434                 ar->rx_channel = def->chan;
6435         } else if (ctx && ath10k_mac_num_chanctxs(ar) == 0) {
6436                 ar->rx_channel = ctx->def.chan;
6437         } else {
6438                 ar->rx_channel = NULL;
6439         }
6440         rcu_read_unlock();
6441 }
6442
6443 static void
6444 ath10k_mac_update_vif_chan(struct ath10k *ar,
6445                            struct ieee80211_vif_chanctx_switch *vifs,
6446                            int n_vifs)
6447 {
6448         struct ath10k_vif *arvif;
6449         int ret;
6450         int i;
6451
6452         lockdep_assert_held(&ar->conf_mutex);
6453
6454         /* First stop monitor interface. Some FW versions crash if there's a
6455          * lone monitor interface.
6456          */
6457         if (ar->monitor_started)
6458                 ath10k_monitor_stop(ar);
6459
6460         for (i = 0; i < n_vifs; i++) {
6461                 arvif = ath10k_vif_to_arvif(vifs[i].vif);
6462
6463                 ath10k_dbg(ar, ATH10K_DBG_MAC,
6464                            "mac chanctx switch vdev_id %i freq %hu->%hu width %d->%d\n",
6465                            arvif->vdev_id,
6466                            vifs[i].old_ctx->def.chan->center_freq,
6467                            vifs[i].new_ctx->def.chan->center_freq,
6468                            vifs[i].old_ctx->def.width,
6469                            vifs[i].new_ctx->def.width);
6470
6471                 if (WARN_ON(!arvif->is_started))
6472                         continue;
6473
6474                 if (WARN_ON(!arvif->is_up))
6475                         continue;
6476
6477                 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
6478                 if (ret) {
6479                         ath10k_warn(ar, "failed to down vdev %d: %d\n",
6480                                     arvif->vdev_id, ret);
6481                         continue;
6482                 }
6483         }
6484
6485         /* All relevant vdevs are downed and associated channel resources
6486          * should be available for the channel switch now.
6487          */
6488
6489         spin_lock_bh(&ar->data_lock);
6490         ath10k_mac_update_rx_channel(ar, NULL, vifs, n_vifs);
6491         spin_unlock_bh(&ar->data_lock);
6492
6493         for (i = 0; i < n_vifs; i++) {
6494                 arvif = ath10k_vif_to_arvif(vifs[i].vif);
6495
6496                 if (WARN_ON(!arvif->is_started))
6497                         continue;
6498
6499                 if (WARN_ON(!arvif->is_up))
6500                         continue;
6501
6502                 ret = ath10k_mac_setup_bcn_tmpl(arvif);
6503                 if (ret)
6504                         ath10k_warn(ar, "failed to update bcn tmpl during csa: %d\n",
6505                                     ret);
6506
6507                 ret = ath10k_mac_setup_prb_tmpl(arvif);
6508                 if (ret)
6509                         ath10k_warn(ar, "failed to update prb tmpl during csa: %d\n",
6510                                     ret);
6511
6512                 ret = ath10k_vdev_restart(arvif, &vifs[i].new_ctx->def);
6513                 if (ret) {
6514                         ath10k_warn(ar, "failed to restart vdev %d: %d\n",
6515                                     arvif->vdev_id, ret);
6516                         continue;
6517                 }
6518
6519                 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
6520                                          arvif->bssid);
6521                 if (ret) {
6522                         ath10k_warn(ar, "failed to bring vdev up %d: %d\n",
6523                                     arvif->vdev_id, ret);
6524                         continue;
6525                 }
6526         }
6527
6528         ath10k_monitor_recalc(ar);
6529 }
6530
6531 static int
6532 ath10k_mac_op_add_chanctx(struct ieee80211_hw *hw,
6533                           struct ieee80211_chanctx_conf *ctx)
6534 {
6535         struct ath10k *ar = hw->priv;
6536
6537         ath10k_dbg(ar, ATH10K_DBG_MAC,
6538                    "mac chanctx add freq %hu width %d ptr %p\n",
6539                    ctx->def.chan->center_freq, ctx->def.width, ctx);
6540
6541         mutex_lock(&ar->conf_mutex);
6542
6543         spin_lock_bh(&ar->data_lock);
6544         ath10k_mac_update_rx_channel(ar, ctx, NULL, 0);
6545         spin_unlock_bh(&ar->data_lock);
6546
6547         ath10k_recalc_radar_detection(ar);
6548         ath10k_monitor_recalc(ar);
6549
6550         mutex_unlock(&ar->conf_mutex);
6551
6552         return 0;
6553 }
6554
6555 static void
6556 ath10k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
6557                              struct ieee80211_chanctx_conf *ctx)
6558 {
6559         struct ath10k *ar = hw->priv;
6560
6561         ath10k_dbg(ar, ATH10K_DBG_MAC,
6562                    "mac chanctx remove freq %hu width %d ptr %p\n",
6563                    ctx->def.chan->center_freq, ctx->def.width, ctx);
6564
6565         mutex_lock(&ar->conf_mutex);
6566
6567         spin_lock_bh(&ar->data_lock);
6568         ath10k_mac_update_rx_channel(ar, NULL, NULL, 0);
6569         spin_unlock_bh(&ar->data_lock);
6570
6571         ath10k_recalc_radar_detection(ar);
6572         ath10k_monitor_recalc(ar);
6573
6574         mutex_unlock(&ar->conf_mutex);
6575 }
6576
6577 struct ath10k_mac_change_chanctx_arg {
6578         struct ieee80211_chanctx_conf *ctx;
6579         struct ieee80211_vif_chanctx_switch *vifs;
6580         int n_vifs;
6581         int next_vif;
6582 };
6583
6584 static void
6585 ath10k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
6586                                    struct ieee80211_vif *vif)
6587 {
6588         struct ath10k_mac_change_chanctx_arg *arg = data;
6589
6590         if (rcu_access_pointer(vif->chanctx_conf) != arg->ctx)
6591                 return;
6592
6593         arg->n_vifs++;
6594 }
6595
6596 static void
6597 ath10k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
6598                                     struct ieee80211_vif *vif)
6599 {
6600         struct ath10k_mac_change_chanctx_arg *arg = data;
6601         struct ieee80211_chanctx_conf *ctx;
6602
6603         ctx = rcu_access_pointer(vif->chanctx_conf);
6604         if (ctx != arg->ctx)
6605                 return;
6606
6607         if (WARN_ON(arg->next_vif == arg->n_vifs))
6608                 return;
6609
6610         arg->vifs[arg->next_vif].vif = vif;
6611         arg->vifs[arg->next_vif].old_ctx = ctx;
6612         arg->vifs[arg->next_vif].new_ctx = ctx;
6613         arg->next_vif++;
6614 }
6615
6616 static void
6617 ath10k_mac_op_change_chanctx(struct ieee80211_hw *hw,
6618                              struct ieee80211_chanctx_conf *ctx,
6619                              u32 changed)
6620 {
6621         struct ath10k *ar = hw->priv;
6622         struct ath10k_mac_change_chanctx_arg arg = { .ctx = ctx };
6623
6624         mutex_lock(&ar->conf_mutex);
6625
6626         ath10k_dbg(ar, ATH10K_DBG_MAC,
6627                    "mac chanctx change freq %hu width %d ptr %p changed %x\n",
6628                    ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
6629
6630         /* This shouldn't really happen because channel switching should use
6631          * switch_vif_chanctx().
6632          */
6633         if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
6634                 goto unlock;
6635
6636         if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH) {
6637                 ieee80211_iterate_active_interfaces_atomic(
6638                                         hw,
6639                                         IEEE80211_IFACE_ITER_NORMAL,
6640                                         ath10k_mac_change_chanctx_cnt_iter,
6641                                         &arg);
6642                 if (arg.n_vifs == 0)
6643                         goto radar;
6644
6645                 arg.vifs = kcalloc(arg.n_vifs, sizeof(arg.vifs[0]),
6646                                    GFP_KERNEL);
6647                 if (!arg.vifs)
6648                         goto radar;
6649
6650                 ieee80211_iterate_active_interfaces_atomic(
6651                                         hw,
6652                                         IEEE80211_IFACE_ITER_NORMAL,
6653                                         ath10k_mac_change_chanctx_fill_iter,
6654                                         &arg);
6655                 ath10k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
6656                 kfree(arg.vifs);
6657         }
6658
6659 radar:
6660         ath10k_recalc_radar_detection(ar);
6661
6662         /* FIXME: How to configure Rx chains properly? */
6663
6664         /* No other actions are actually necessary. Firmware maintains channel
6665          * definitions per vdev internally and there's no host-side channel
6666          * context abstraction to configure, e.g. channel width.
6667          */
6668
6669 unlock:
6670         mutex_unlock(&ar->conf_mutex);
6671 }
6672
6673 static int
6674 ath10k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
6675                                  struct ieee80211_vif *vif,
6676                                  struct ieee80211_chanctx_conf *ctx)
6677 {
6678         struct ath10k *ar = hw->priv;
6679         struct ath10k_vif *arvif = (void *)vif->drv_priv;
6680         int ret;
6681
6682         mutex_lock(&ar->conf_mutex);
6683
6684         ath10k_dbg(ar, ATH10K_DBG_MAC,
6685                    "mac chanctx assign ptr %p vdev_id %i\n",
6686                    ctx, arvif->vdev_id);
6687
6688         if (WARN_ON(arvif->is_started)) {
6689                 mutex_unlock(&ar->conf_mutex);
6690                 return -EBUSY;
6691         }
6692
6693         ret = ath10k_vdev_start(arvif, &ctx->def);
6694         if (ret) {
6695                 ath10k_warn(ar, "failed to start vdev %i addr %pM on freq %d: %d\n",
6696                             arvif->vdev_id, vif->addr,
6697                             ctx->def.chan->center_freq, ret);
6698                 goto err;
6699         }
6700
6701         arvif->is_started = true;
6702
6703         ret = ath10k_mac_vif_setup_ps(arvif);
6704         if (ret) {
6705                 ath10k_warn(ar, "failed to update vdev %i ps: %d\n",
6706                             arvif->vdev_id, ret);
6707                 goto err_stop;
6708         }
6709
6710         if (vif->type == NL80211_IFTYPE_MONITOR) {
6711                 ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, 0, vif->addr);
6712                 if (ret) {
6713                         ath10k_warn(ar, "failed to up monitor vdev %i: %d\n",
6714                                     arvif->vdev_id, ret);
6715                         goto err_stop;
6716                 }
6717
6718                 arvif->is_up = true;
6719         }
6720
6721         mutex_unlock(&ar->conf_mutex);
6722         return 0;
6723
6724 err_stop:
6725         ath10k_vdev_stop(arvif);
6726         arvif->is_started = false;
6727         ath10k_mac_vif_setup_ps(arvif);
6728
6729 err:
6730         mutex_unlock(&ar->conf_mutex);
6731         return ret;
6732 }
6733
6734 static void
6735 ath10k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
6736                                    struct ieee80211_vif *vif,
6737                                    struct ieee80211_chanctx_conf *ctx)
6738 {
6739         struct ath10k *ar = hw->priv;
6740         struct ath10k_vif *arvif = (void *)vif->drv_priv;
6741         int ret;
6742
6743         mutex_lock(&ar->conf_mutex);
6744
6745         ath10k_dbg(ar, ATH10K_DBG_MAC,
6746                    "mac chanctx unassign ptr %p vdev_id %i\n",
6747                    ctx, arvif->vdev_id);
6748
6749         WARN_ON(!arvif->is_started);
6750
6751         if (vif->type == NL80211_IFTYPE_MONITOR) {
6752                 WARN_ON(!arvif->is_up);
6753
6754                 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
6755                 if (ret)
6756                         ath10k_warn(ar, "failed to down monitor vdev %i: %d\n",
6757                                     arvif->vdev_id, ret);
6758
6759                 arvif->is_up = false;
6760         }
6761
6762         ret = ath10k_vdev_stop(arvif);
6763         if (ret)
6764                 ath10k_warn(ar, "failed to stop vdev %i: %d\n",
6765                             arvif->vdev_id, ret);
6766
6767         arvif->is_started = false;
6768
6769         mutex_unlock(&ar->conf_mutex);
6770 }
6771
6772 static int
6773 ath10k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
6774                                  struct ieee80211_vif_chanctx_switch *vifs,
6775                                  int n_vifs,
6776                                  enum ieee80211_chanctx_switch_mode mode)
6777 {
6778         struct ath10k *ar = hw->priv;
6779
6780         mutex_lock(&ar->conf_mutex);
6781
6782         ath10k_dbg(ar, ATH10K_DBG_MAC,
6783                    "mac chanctx switch n_vifs %d mode %d\n",
6784                    n_vifs, mode);
6785         ath10k_mac_update_vif_chan(ar, vifs, n_vifs);
6786
6787         mutex_unlock(&ar->conf_mutex);
6788         return 0;
6789 }
6790
6791 static const struct ieee80211_ops ath10k_ops = {
6792         .tx                             = ath10k_tx,
6793         .start                          = ath10k_start,
6794         .stop                           = ath10k_stop,
6795         .config                         = ath10k_config,
6796         .add_interface                  = ath10k_add_interface,
6797         .remove_interface               = ath10k_remove_interface,
6798         .configure_filter               = ath10k_configure_filter,
6799         .bss_info_changed               = ath10k_bss_info_changed,
6800         .hw_scan                        = ath10k_hw_scan,
6801         .cancel_hw_scan                 = ath10k_cancel_hw_scan,
6802         .set_key                        = ath10k_set_key,
6803         .set_default_unicast_key        = ath10k_set_default_unicast_key,
6804         .sta_state                      = ath10k_sta_state,
6805         .conf_tx                        = ath10k_conf_tx,
6806         .remain_on_channel              = ath10k_remain_on_channel,
6807         .cancel_remain_on_channel       = ath10k_cancel_remain_on_channel,
6808         .set_rts_threshold              = ath10k_set_rts_threshold,
6809         .set_frag_threshold             = ath10k_mac_op_set_frag_threshold,
6810         .flush                          = ath10k_flush,
6811         .tx_last_beacon                 = ath10k_tx_last_beacon,
6812         .set_antenna                    = ath10k_set_antenna,
6813         .get_antenna                    = ath10k_get_antenna,
6814         .reconfig_complete              = ath10k_reconfig_complete,
6815         .get_survey                     = ath10k_get_survey,
6816         .set_bitrate_mask               = ath10k_mac_op_set_bitrate_mask,
6817         .sta_rc_update                  = ath10k_sta_rc_update,
6818         .get_tsf                        = ath10k_get_tsf,
6819         .ampdu_action                   = ath10k_ampdu_action,
6820         .get_et_sset_count              = ath10k_debug_get_et_sset_count,
6821         .get_et_stats                   = ath10k_debug_get_et_stats,
6822         .get_et_strings                 = ath10k_debug_get_et_strings,
6823         .add_chanctx                    = ath10k_mac_op_add_chanctx,
6824         .remove_chanctx                 = ath10k_mac_op_remove_chanctx,
6825         .change_chanctx                 = ath10k_mac_op_change_chanctx,
6826         .assign_vif_chanctx             = ath10k_mac_op_assign_vif_chanctx,
6827         .unassign_vif_chanctx           = ath10k_mac_op_unassign_vif_chanctx,
6828         .switch_vif_chanctx             = ath10k_mac_op_switch_vif_chanctx,
6829
6830         CFG80211_TESTMODE_CMD(ath10k_tm_cmd)
6831
6832 #ifdef CONFIG_PM
6833         .suspend                        = ath10k_wow_op_suspend,
6834         .resume                         = ath10k_wow_op_resume,
6835 #endif
6836 #ifdef CONFIG_MAC80211_DEBUGFS
6837         .sta_add_debugfs                = ath10k_sta_add_debugfs,
6838 #endif
6839 };
6840
6841 #define CHAN2G(_channel, _freq, _flags) { \
6842         .band                   = IEEE80211_BAND_2GHZ, \
6843         .hw_value               = (_channel), \
6844         .center_freq            = (_freq), \
6845         .flags                  = (_flags), \
6846         .max_antenna_gain       = 0, \
6847         .max_power              = 30, \
6848 }
6849
6850 #define CHAN5G(_channel, _freq, _flags) { \
6851         .band                   = IEEE80211_BAND_5GHZ, \
6852         .hw_value               = (_channel), \
6853         .center_freq            = (_freq), \
6854         .flags                  = (_flags), \
6855         .max_antenna_gain       = 0, \
6856         .max_power              = 30, \
6857 }
6858
6859 static const struct ieee80211_channel ath10k_2ghz_channels[] = {
6860         CHAN2G(1, 2412, 0),
6861         CHAN2G(2, 2417, 0),
6862         CHAN2G(3, 2422, 0),
6863         CHAN2G(4, 2427, 0),
6864         CHAN2G(5, 2432, 0),
6865         CHAN2G(6, 2437, 0),
6866         CHAN2G(7, 2442, 0),
6867         CHAN2G(8, 2447, 0),
6868         CHAN2G(9, 2452, 0),
6869         CHAN2G(10, 2457, 0),
6870         CHAN2G(11, 2462, 0),
6871         CHAN2G(12, 2467, 0),
6872         CHAN2G(13, 2472, 0),
6873         CHAN2G(14, 2484, 0),
6874 };
6875
6876 static const struct ieee80211_channel ath10k_5ghz_channels[] = {
6877         CHAN5G(36, 5180, 0),
6878         CHAN5G(40, 5200, 0),
6879         CHAN5G(44, 5220, 0),
6880         CHAN5G(48, 5240, 0),
6881         CHAN5G(52, 5260, 0),
6882         CHAN5G(56, 5280, 0),
6883         CHAN5G(60, 5300, 0),
6884         CHAN5G(64, 5320, 0),
6885         CHAN5G(100, 5500, 0),
6886         CHAN5G(104, 5520, 0),
6887         CHAN5G(108, 5540, 0),
6888         CHAN5G(112, 5560, 0),
6889         CHAN5G(116, 5580, 0),
6890         CHAN5G(120, 5600, 0),
6891         CHAN5G(124, 5620, 0),
6892         CHAN5G(128, 5640, 0),
6893         CHAN5G(132, 5660, 0),
6894         CHAN5G(136, 5680, 0),
6895         CHAN5G(140, 5700, 0),
6896         CHAN5G(144, 5720, 0),
6897         CHAN5G(149, 5745, 0),
6898         CHAN5G(153, 5765, 0),
6899         CHAN5G(157, 5785, 0),
6900         CHAN5G(161, 5805, 0),
6901         CHAN5G(165, 5825, 0),
6902 };
6903
6904 struct ath10k *ath10k_mac_create(size_t priv_size)
6905 {
6906         struct ieee80211_hw *hw;
6907         struct ath10k *ar;
6908
6909         hw = ieee80211_alloc_hw(sizeof(struct ath10k) + priv_size, &ath10k_ops);
6910         if (!hw)
6911                 return NULL;
6912
6913         ar = hw->priv;
6914         ar->hw = hw;
6915
6916         return ar;
6917 }
6918
6919 void ath10k_mac_destroy(struct ath10k *ar)
6920 {
6921         ieee80211_free_hw(ar->hw);
6922 }
6923
6924 static const struct ieee80211_iface_limit ath10k_if_limits[] = {
6925         {
6926                 .max    = 8,
6927                 .types  = BIT(NL80211_IFTYPE_STATION)
6928                         | BIT(NL80211_IFTYPE_P2P_CLIENT)
6929         },
6930         {
6931                 .max    = 3,
6932                 .types  = BIT(NL80211_IFTYPE_P2P_GO)
6933         },
6934         {
6935                 .max    = 1,
6936                 .types  = BIT(NL80211_IFTYPE_P2P_DEVICE)
6937         },
6938         {
6939                 .max    = 7,
6940                 .types  = BIT(NL80211_IFTYPE_AP)
6941 #ifdef CONFIG_MAC80211_MESH
6942                         | BIT(NL80211_IFTYPE_MESH_POINT)
6943 #endif
6944         },
6945 };
6946
6947 static const struct ieee80211_iface_limit ath10k_10x_if_limits[] = {
6948         {
6949                 .max    = 8,
6950                 .types  = BIT(NL80211_IFTYPE_AP)
6951 #ifdef CONFIG_MAC80211_MESH
6952                         | BIT(NL80211_IFTYPE_MESH_POINT)
6953 #endif
6954         },
6955         {
6956                 .max    = 1,
6957                 .types  = BIT(NL80211_IFTYPE_STATION)
6958         },
6959 };
6960
6961 static const struct ieee80211_iface_combination ath10k_if_comb[] = {
6962         {
6963                 .limits = ath10k_if_limits,
6964                 .n_limits = ARRAY_SIZE(ath10k_if_limits),
6965                 .max_interfaces = 8,
6966                 .num_different_channels = 1,
6967                 .beacon_int_infra_match = true,
6968         },
6969 };
6970
6971 static const struct ieee80211_iface_combination ath10k_10x_if_comb[] = {
6972         {
6973                 .limits = ath10k_10x_if_limits,
6974                 .n_limits = ARRAY_SIZE(ath10k_10x_if_limits),
6975                 .max_interfaces = 8,
6976                 .num_different_channels = 1,
6977                 .beacon_int_infra_match = true,
6978 #ifdef CONFIG_ATH10K_DFS_CERTIFIED
6979                 .radar_detect_widths =  BIT(NL80211_CHAN_WIDTH_20_NOHT) |
6980                                         BIT(NL80211_CHAN_WIDTH_20) |
6981                                         BIT(NL80211_CHAN_WIDTH_40) |
6982                                         BIT(NL80211_CHAN_WIDTH_80),
6983 #endif
6984         },
6985 };
6986
6987 static const struct ieee80211_iface_limit ath10k_tlv_if_limit[] = {
6988         {
6989                 .max = 2,
6990                 .types = BIT(NL80211_IFTYPE_STATION),
6991         },
6992         {
6993                 .max = 2,
6994                 .types = BIT(NL80211_IFTYPE_AP) |
6995 #ifdef CONFIG_MAC80211_MESH
6996                          BIT(NL80211_IFTYPE_MESH_POINT) |
6997 #endif
6998                          BIT(NL80211_IFTYPE_P2P_CLIENT) |
6999                          BIT(NL80211_IFTYPE_P2P_GO),
7000         },
7001         {
7002                 .max = 1,
7003                 .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
7004         },
7005 };
7006
7007 static const struct ieee80211_iface_limit ath10k_tlv_qcs_if_limit[] = {
7008         {
7009                 .max = 2,
7010                 .types = BIT(NL80211_IFTYPE_STATION),
7011         },
7012         {
7013                 .max = 2,
7014                 .types = BIT(NL80211_IFTYPE_P2P_CLIENT),
7015         },
7016         {
7017                 .max = 1,
7018                 .types = BIT(NL80211_IFTYPE_AP) |
7019 #ifdef CONFIG_MAC80211_MESH
7020                          BIT(NL80211_IFTYPE_MESH_POINT) |
7021 #endif
7022                          BIT(NL80211_IFTYPE_P2P_GO),
7023         },
7024         {
7025                 .max = 1,
7026                 .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
7027         },
7028 };
7029
7030 static const struct ieee80211_iface_limit ath10k_tlv_if_limit_ibss[] = {
7031         {
7032                 .max = 1,
7033                 .types = BIT(NL80211_IFTYPE_STATION),
7034         },
7035         {
7036                 .max = 1,
7037                 .types = BIT(NL80211_IFTYPE_ADHOC),
7038         },
7039 };
7040
7041 /* FIXME: This is not thouroughly tested. These combinations may over- or
7042  * underestimate hw/fw capabilities.
7043  */
7044 static struct ieee80211_iface_combination ath10k_tlv_if_comb[] = {
7045         {
7046                 .limits = ath10k_tlv_if_limit,
7047                 .num_different_channels = 1,
7048                 .max_interfaces = 4,
7049                 .n_limits = ARRAY_SIZE(ath10k_tlv_if_limit),
7050         },
7051         {
7052                 .limits = ath10k_tlv_if_limit_ibss,
7053                 .num_different_channels = 1,
7054                 .max_interfaces = 2,
7055                 .n_limits = ARRAY_SIZE(ath10k_tlv_if_limit_ibss),
7056         },
7057 };
7058
7059 static struct ieee80211_iface_combination ath10k_tlv_qcs_if_comb[] = {
7060         {
7061                 .limits = ath10k_tlv_if_limit,
7062                 .num_different_channels = 1,
7063                 .max_interfaces = 4,
7064                 .n_limits = ARRAY_SIZE(ath10k_tlv_if_limit),
7065         },
7066         {
7067                 .limits = ath10k_tlv_qcs_if_limit,
7068                 .num_different_channels = 2,
7069                 .max_interfaces = 4,
7070                 .n_limits = ARRAY_SIZE(ath10k_tlv_qcs_if_limit),
7071         },
7072         {
7073                 .limits = ath10k_tlv_if_limit_ibss,
7074                 .num_different_channels = 1,
7075                 .max_interfaces = 2,
7076                 .n_limits = ARRAY_SIZE(ath10k_tlv_if_limit_ibss),
7077         },
7078 };
7079
7080 static const struct ieee80211_iface_limit ath10k_10_4_if_limits[] = {
7081         {
7082                 .max = 1,
7083                 .types = BIT(NL80211_IFTYPE_STATION),
7084         },
7085         {
7086                 .max    = 16,
7087                 .types  = BIT(NL80211_IFTYPE_AP)
7088 #ifdef CONFIG_MAC80211_MESH
7089                         | BIT(NL80211_IFTYPE_MESH_POINT)
7090 #endif
7091         },
7092 };
7093
7094 static const struct ieee80211_iface_combination ath10k_10_4_if_comb[] = {
7095         {
7096                 .limits = ath10k_10_4_if_limits,
7097                 .n_limits = ARRAY_SIZE(ath10k_10_4_if_limits),
7098                 .max_interfaces = 16,
7099                 .num_different_channels = 1,
7100                 .beacon_int_infra_match = true,
7101 #ifdef CONFIG_ATH10K_DFS_CERTIFIED
7102                 .radar_detect_widths =  BIT(NL80211_CHAN_WIDTH_20_NOHT) |
7103                                         BIT(NL80211_CHAN_WIDTH_20) |
7104                                         BIT(NL80211_CHAN_WIDTH_40) |
7105                                         BIT(NL80211_CHAN_WIDTH_80),
7106 #endif
7107         },
7108 };
7109
7110 static void ath10k_get_arvif_iter(void *data, u8 *mac,
7111                                   struct ieee80211_vif *vif)
7112 {
7113         struct ath10k_vif_iter *arvif_iter = data;
7114         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
7115
7116         if (arvif->vdev_id == arvif_iter->vdev_id)
7117                 arvif_iter->arvif = arvif;
7118 }
7119
7120 struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
7121 {
7122         struct ath10k_vif_iter arvif_iter;
7123         u32 flags;
7124
7125         memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
7126         arvif_iter.vdev_id = vdev_id;
7127
7128         flags = IEEE80211_IFACE_ITER_RESUME_ALL;
7129         ieee80211_iterate_active_interfaces_atomic(ar->hw,
7130                                                    flags,
7131                                                    ath10k_get_arvif_iter,
7132                                                    &arvif_iter);
7133         if (!arvif_iter.arvif) {
7134                 ath10k_warn(ar, "No VIF found for vdev %d\n", vdev_id);
7135                 return NULL;
7136         }
7137
7138         return arvif_iter.arvif;
7139 }
7140
7141 int ath10k_mac_register(struct ath10k *ar)
7142 {
7143         static const u32 cipher_suites[] = {
7144                 WLAN_CIPHER_SUITE_WEP40,
7145                 WLAN_CIPHER_SUITE_WEP104,
7146                 WLAN_CIPHER_SUITE_TKIP,
7147                 WLAN_CIPHER_SUITE_CCMP,
7148                 WLAN_CIPHER_SUITE_AES_CMAC,
7149         };
7150         struct ieee80211_supported_band *band;
7151         void *channels;
7152         int ret;
7153
7154         SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
7155
7156         SET_IEEE80211_DEV(ar->hw, ar->dev);
7157
7158         BUILD_BUG_ON((ARRAY_SIZE(ath10k_2ghz_channels) +
7159                       ARRAY_SIZE(ath10k_5ghz_channels)) !=
7160                      ATH10K_NUM_CHANS);
7161
7162         if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
7163                 channels = kmemdup(ath10k_2ghz_channels,
7164                                    sizeof(ath10k_2ghz_channels),
7165                                    GFP_KERNEL);
7166                 if (!channels) {
7167                         ret = -ENOMEM;
7168                         goto err_free;
7169                 }
7170
7171                 band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
7172                 band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
7173                 band->channels = channels;
7174                 band->n_bitrates = ath10k_g_rates_size;
7175                 band->bitrates = ath10k_g_rates;
7176
7177                 ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
7178         }
7179
7180         if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
7181                 channels = kmemdup(ath10k_5ghz_channels,
7182                                    sizeof(ath10k_5ghz_channels),
7183                                    GFP_KERNEL);
7184                 if (!channels) {
7185                         ret = -ENOMEM;
7186                         goto err_free;
7187                 }
7188
7189                 band = &ar->mac.sbands[IEEE80211_BAND_5GHZ];
7190                 band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
7191                 band->channels = channels;
7192                 band->n_bitrates = ath10k_a_rates_size;
7193                 band->bitrates = ath10k_a_rates;
7194                 ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
7195         }
7196
7197         ath10k_mac_setup_ht_vht_cap(ar);
7198
7199         ar->hw->wiphy->interface_modes =
7200                 BIT(NL80211_IFTYPE_STATION) |
7201                 BIT(NL80211_IFTYPE_AP) |
7202                 BIT(NL80211_IFTYPE_MESH_POINT);
7203
7204         ar->hw->wiphy->available_antennas_rx = ar->cfg_rx_chainmask;
7205         ar->hw->wiphy->available_antennas_tx = ar->cfg_tx_chainmask;
7206
7207         if (!test_bit(ATH10K_FW_FEATURE_NO_P2P, ar->fw_features))
7208                 ar->hw->wiphy->interface_modes |=
7209                         BIT(NL80211_IFTYPE_P2P_DEVICE) |
7210                         BIT(NL80211_IFTYPE_P2P_CLIENT) |
7211                         BIT(NL80211_IFTYPE_P2P_GO);
7212
7213         ieee80211_hw_set(ar->hw, SIGNAL_DBM);
7214         ieee80211_hw_set(ar->hw, SUPPORTS_PS);
7215         ieee80211_hw_set(ar->hw, SUPPORTS_DYNAMIC_PS);
7216         ieee80211_hw_set(ar->hw, MFP_CAPABLE);
7217         ieee80211_hw_set(ar->hw, REPORTS_TX_ACK_STATUS);
7218         ieee80211_hw_set(ar->hw, HAS_RATE_CONTROL);
7219         ieee80211_hw_set(ar->hw, AP_LINK_PS);
7220         ieee80211_hw_set(ar->hw, SPECTRUM_MGMT);
7221         ieee80211_hw_set(ar->hw, SUPPORT_FAST_XMIT);
7222         ieee80211_hw_set(ar->hw, CONNECTION_MONITOR);
7223         ieee80211_hw_set(ar->hw, SUPPORTS_PER_STA_GTK);
7224         ieee80211_hw_set(ar->hw, WANT_MONITOR_VIF);
7225         ieee80211_hw_set(ar->hw, CHANCTX_STA_CSA);
7226         ieee80211_hw_set(ar->hw, QUEUE_CONTROL);
7227
7228         if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
7229                 ieee80211_hw_set(ar->hw, SW_CRYPTO_CONTROL);
7230
7231         ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
7232         ar->hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
7233
7234         if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
7235                 ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
7236
7237         if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
7238                 ieee80211_hw_set(ar->hw, AMPDU_AGGREGATION);
7239                 ieee80211_hw_set(ar->hw, TX_AMPDU_SETUP_IN_HW);
7240         }
7241
7242         ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
7243         ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
7244
7245         ar->hw->vif_data_size = sizeof(struct ath10k_vif);
7246         ar->hw->sta_data_size = sizeof(struct ath10k_sta);
7247
7248         ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
7249
7250         if (test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)) {
7251                 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
7252
7253                 /* Firmware delivers WPS/P2P Probe Requests frames to driver so
7254                  * that userspace (e.g. wpa_supplicant/hostapd) can generate
7255                  * correct Probe Responses. This is more of a hack advert..
7256                  */
7257                 ar->hw->wiphy->probe_resp_offload |=
7258                         NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
7259                         NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
7260                         NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
7261         }
7262
7263         if (test_bit(WMI_SERVICE_TDLS, ar->wmi.svc_map))
7264                 ar->hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
7265
7266         ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
7267         ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
7268         ar->hw->wiphy->max_remain_on_channel_duration = 5000;
7269
7270         ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
7271         ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE;
7272
7273         ar->hw->wiphy->max_ap_assoc_sta = ar->max_num_stations;
7274
7275         ret = ath10k_wow_init(ar);
7276         if (ret) {
7277                 ath10k_warn(ar, "failed to init wow: %d\n", ret);
7278                 goto err_free;
7279         }
7280
7281         wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
7282
7283         /*
7284          * on LL hardware queues are managed entirely by the FW
7285          * so we only advertise to mac we can do the queues thing
7286          */
7287         ar->hw->queues = IEEE80211_MAX_QUEUES;
7288
7289         /* vdev_ids are used as hw queue numbers. Make sure offchan tx queue is
7290          * something that vdev_ids can't reach so that we don't stop the queue
7291          * accidentally.
7292          */
7293         ar->hw->offchannel_tx_hw_queue = IEEE80211_MAX_QUEUES - 1;
7294
7295         switch (ar->wmi.op_version) {
7296         case ATH10K_FW_WMI_OP_VERSION_MAIN:
7297                 ar->hw->wiphy->iface_combinations = ath10k_if_comb;
7298                 ar->hw->wiphy->n_iface_combinations =
7299                         ARRAY_SIZE(ath10k_if_comb);
7300                 ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
7301                 break;
7302         case ATH10K_FW_WMI_OP_VERSION_TLV:
7303                 if (test_bit(WMI_SERVICE_ADAPTIVE_OCS, ar->wmi.svc_map)) {
7304                         ar->hw->wiphy->iface_combinations =
7305                                 ath10k_tlv_qcs_if_comb;
7306                         ar->hw->wiphy->n_iface_combinations =
7307                                 ARRAY_SIZE(ath10k_tlv_qcs_if_comb);
7308                 } else {
7309                         ar->hw->wiphy->iface_combinations = ath10k_tlv_if_comb;
7310                         ar->hw->wiphy->n_iface_combinations =
7311                                 ARRAY_SIZE(ath10k_tlv_if_comb);
7312                 }
7313                 ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
7314                 break;
7315         case ATH10K_FW_WMI_OP_VERSION_10_1:
7316         case ATH10K_FW_WMI_OP_VERSION_10_2:
7317         case ATH10K_FW_WMI_OP_VERSION_10_2_4:
7318                 ar->hw->wiphy->iface_combinations = ath10k_10x_if_comb;
7319                 ar->hw->wiphy->n_iface_combinations =
7320                         ARRAY_SIZE(ath10k_10x_if_comb);
7321                 break;
7322         case ATH10K_FW_WMI_OP_VERSION_10_4:
7323                 ar->hw->wiphy->iface_combinations = ath10k_10_4_if_comb;
7324                 ar->hw->wiphy->n_iface_combinations =
7325                         ARRAY_SIZE(ath10k_10_4_if_comb);
7326                 break;
7327         case ATH10K_FW_WMI_OP_VERSION_UNSET:
7328         case ATH10K_FW_WMI_OP_VERSION_MAX:
7329                 WARN_ON(1);
7330                 ret = -EINVAL;
7331                 goto err_free;
7332         }
7333
7334         if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
7335                 ar->hw->netdev_features = NETIF_F_HW_CSUM;
7336
7337         if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED)) {
7338                 /* Init ath dfs pattern detector */
7339                 ar->ath_common.debug_mask = ATH_DBG_DFS;
7340                 ar->dfs_detector = dfs_pattern_detector_init(&ar->ath_common,
7341                                                              NL80211_DFS_UNSET);
7342
7343                 if (!ar->dfs_detector)
7344                         ath10k_warn(ar, "failed to initialise DFS pattern detector\n");
7345         }
7346
7347         ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
7348                             ath10k_reg_notifier);
7349         if (ret) {
7350                 ath10k_err(ar, "failed to initialise regulatory: %i\n", ret);
7351                 goto err_dfs_detector_exit;
7352         }
7353
7354         ar->hw->wiphy->cipher_suites = cipher_suites;
7355         ar->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
7356
7357         ret = ieee80211_register_hw(ar->hw);
7358         if (ret) {
7359                 ath10k_err(ar, "failed to register ieee80211: %d\n", ret);
7360                 goto err_dfs_detector_exit;
7361         }
7362
7363         if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
7364                 ret = regulatory_hint(ar->hw->wiphy,
7365                                       ar->ath_common.regulatory.alpha2);
7366                 if (ret)
7367                         goto err_unregister;
7368         }
7369
7370         return 0;
7371
7372 err_unregister:
7373         ieee80211_unregister_hw(ar->hw);
7374
7375 err_dfs_detector_exit:
7376         if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
7377                 ar->dfs_detector->exit(ar->dfs_detector);
7378
7379 err_free:
7380         kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
7381         kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
7382
7383         SET_IEEE80211_DEV(ar->hw, NULL);
7384         return ret;
7385 }
7386
7387 void ath10k_mac_unregister(struct ath10k *ar)
7388 {
7389         ieee80211_unregister_hw(ar->hw);
7390
7391         if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
7392                 ar->dfs_detector->exit(ar->dfs_detector);
7393
7394         kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
7395         kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
7396
7397         SET_IEEE80211_DEV(ar->hw, NULL);
7398 }