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