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