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