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