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