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