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