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