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