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