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