OSDN Git Service

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