OSDN Git Service

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