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