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