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