OSDN Git Service

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