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