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