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