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