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