ath10k: unregister and remove frag_threshold callback
[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 void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
1420                                         struct ieee80211_vif *vif,
1421                                         struct ieee80211_sta *sta,
1422                                         struct wmi_peer_assoc_complete_arg *arg)
1423 {
1424         enum wmi_phy_mode phymode = MODE_UNKNOWN;
1425
1426         switch (ar->hw->conf.chandef.chan->band) {
1427         case IEEE80211_BAND_2GHZ:
1428                 if (sta->ht_cap.ht_supported) {
1429                         if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1430                                 phymode = MODE_11NG_HT40;
1431                         else
1432                                 phymode = MODE_11NG_HT20;
1433                 } else {
1434                         phymode = MODE_11G;
1435                 }
1436
1437                 break;
1438         case IEEE80211_BAND_5GHZ:
1439                 /*
1440                  * Check VHT first.
1441                  */
1442                 if (sta->vht_cap.vht_supported) {
1443                         if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1444                                 phymode = MODE_11AC_VHT80;
1445                         else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1446                                 phymode = MODE_11AC_VHT40;
1447                         else if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
1448                                 phymode = MODE_11AC_VHT20;
1449                 } else if (sta->ht_cap.ht_supported) {
1450                         if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1451                                 phymode = MODE_11NA_HT40;
1452                         else
1453                                 phymode = MODE_11NA_HT20;
1454                 } else {
1455                         phymode = MODE_11A;
1456                 }
1457
1458                 break;
1459         default:
1460                 break;
1461         }
1462
1463         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM phymode %s\n",
1464                    sta->addr, ath10k_wmi_phymode_str(phymode));
1465
1466         arg->peer_phymode = phymode;
1467         WARN_ON(phymode == MODE_UNKNOWN);
1468 }
1469
1470 static int ath10k_peer_assoc_prepare(struct ath10k *ar,
1471                                      struct ieee80211_vif *vif,
1472                                      struct ieee80211_sta *sta,
1473                                      struct wmi_peer_assoc_complete_arg *arg)
1474 {
1475         lockdep_assert_held(&ar->conf_mutex);
1476
1477         memset(arg, 0, sizeof(*arg));
1478
1479         ath10k_peer_assoc_h_basic(ar, vif, sta, arg);
1480         ath10k_peer_assoc_h_crypto(ar, vif, arg);
1481         ath10k_peer_assoc_h_rates(ar, sta, arg);
1482         ath10k_peer_assoc_h_ht(ar, sta, arg);
1483         ath10k_peer_assoc_h_vht(ar, sta, arg);
1484         ath10k_peer_assoc_h_qos(ar, vif, sta, arg);
1485         ath10k_peer_assoc_h_phymode(ar, vif, sta, arg);
1486
1487         return 0;
1488 }
1489
1490 static const u32 ath10k_smps_map[] = {
1491         [WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
1492         [WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
1493         [WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
1494         [WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
1495 };
1496
1497 static int ath10k_setup_peer_smps(struct ath10k *ar, struct ath10k_vif *arvif,
1498                                   const u8 *addr,
1499                                   const struct ieee80211_sta_ht_cap *ht_cap)
1500 {
1501         int smps;
1502
1503         if (!ht_cap->ht_supported)
1504                 return 0;
1505
1506         smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
1507         smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
1508
1509         if (smps >= ARRAY_SIZE(ath10k_smps_map))
1510                 return -EINVAL;
1511
1512         return ath10k_wmi_peer_set_param(ar, arvif->vdev_id, addr,
1513                                          WMI_PEER_SMPS_STATE,
1514                                          ath10k_smps_map[smps]);
1515 }
1516
1517 /* can be called only in mac80211 callbacks due to `key_count` usage */
1518 static void ath10k_bss_assoc(struct ieee80211_hw *hw,
1519                              struct ieee80211_vif *vif,
1520                              struct ieee80211_bss_conf *bss_conf)
1521 {
1522         struct ath10k *ar = hw->priv;
1523         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1524         struct ieee80211_sta_ht_cap ht_cap;
1525         struct wmi_peer_assoc_complete_arg peer_arg;
1526         struct ieee80211_sta *ap_sta;
1527         int ret;
1528
1529         lockdep_assert_held(&ar->conf_mutex);
1530
1531         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
1532                    arvif->vdev_id, arvif->bssid, arvif->aid);
1533
1534         rcu_read_lock();
1535
1536         ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
1537         if (!ap_sta) {
1538                 ath10k_warn(ar, "failed to find station entry for bss %pM vdev %i\n",
1539                             bss_conf->bssid, arvif->vdev_id);
1540                 rcu_read_unlock();
1541                 return;
1542         }
1543
1544         /* ap_sta must be accessed only within rcu section which must be left
1545          * before calling ath10k_setup_peer_smps() which might sleep. */
1546         ht_cap = ap_sta->ht_cap;
1547
1548         ret = ath10k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg);
1549         if (ret) {
1550                 ath10k_warn(ar, "failed to prepare peer assoc for %pM vdev %i: %d\n",
1551                             bss_conf->bssid, arvif->vdev_id, ret);
1552                 rcu_read_unlock();
1553                 return;
1554         }
1555
1556         rcu_read_unlock();
1557
1558         ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
1559         if (ret) {
1560                 ath10k_warn(ar, "failed to run peer assoc for %pM vdev %i: %d\n",
1561                             bss_conf->bssid, arvif->vdev_id, ret);
1562                 return;
1563         }
1564
1565         ret = ath10k_setup_peer_smps(ar, arvif, bss_conf->bssid, &ht_cap);
1566         if (ret) {
1567                 ath10k_warn(ar, "failed to setup peer SMPS for vdev %i: %d\n",
1568                             arvif->vdev_id, ret);
1569                 return;
1570         }
1571
1572         ath10k_dbg(ar, ATH10K_DBG_MAC,
1573                    "mac vdev %d up (associated) bssid %pM aid %d\n",
1574                    arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
1575
1576         WARN_ON(arvif->is_up);
1577
1578         arvif->aid = bss_conf->aid;
1579         ether_addr_copy(arvif->bssid, bss_conf->bssid);
1580
1581         ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
1582         if (ret) {
1583                 ath10k_warn(ar, "failed to set vdev %d up: %d\n",
1584                             arvif->vdev_id, ret);
1585                 return;
1586         }
1587
1588         arvif->is_up = true;
1589 }
1590
1591 static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
1592                                 struct ieee80211_vif *vif)
1593 {
1594         struct ath10k *ar = hw->priv;
1595         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1596         int ret;
1597
1598         lockdep_assert_held(&ar->conf_mutex);
1599
1600         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
1601                    arvif->vdev_id, arvif->bssid);
1602
1603         ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
1604         if (ret)
1605                 ath10k_warn(ar, "faield to down vdev %i: %d\n",
1606                             arvif->vdev_id, ret);
1607
1608         arvif->def_wep_key_idx = 0;
1609         arvif->is_up = false;
1610 }
1611
1612 static int ath10k_station_assoc(struct ath10k *ar,
1613                                 struct ieee80211_vif *vif,
1614                                 struct ieee80211_sta *sta,
1615                                 bool reassoc)
1616 {
1617         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1618         struct wmi_peer_assoc_complete_arg peer_arg;
1619         int ret = 0;
1620
1621         lockdep_assert_held(&ar->conf_mutex);
1622
1623         ret = ath10k_peer_assoc_prepare(ar, vif, sta, &peer_arg);
1624         if (ret) {
1625                 ath10k_warn(ar, "failed to prepare WMI peer assoc for %pM vdev %i: %i\n",
1626                             sta->addr, arvif->vdev_id, ret);
1627                 return ret;
1628         }
1629
1630         peer_arg.peer_reassoc = reassoc;
1631         ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
1632         if (ret) {
1633                 ath10k_warn(ar, "failed to run peer assoc for STA %pM vdev %i: %d\n",
1634                             sta->addr, arvif->vdev_id, ret);
1635                 return ret;
1636         }
1637
1638         /* Re-assoc is run only to update supported rates for given station. It
1639          * doesn't make much sense to reconfigure the peer completely.
1640          */
1641         if (!reassoc) {
1642                 ret = ath10k_setup_peer_smps(ar, arvif, sta->addr,
1643                                              &sta->ht_cap);
1644                 if (ret) {
1645                         ath10k_warn(ar, "failed to setup peer SMPS for vdev %d: %d\n",
1646                                     arvif->vdev_id, ret);
1647                         return ret;
1648                 }
1649
1650                 ret = ath10k_peer_assoc_qos_ap(ar, arvif, sta);
1651                 if (ret) {
1652                         ath10k_warn(ar, "failed to set qos params for STA %pM for vdev %i: %d\n",
1653                                     sta->addr, arvif->vdev_id, ret);
1654                         return ret;
1655                 }
1656
1657                 if (!sta->wme) {
1658                         arvif->num_legacy_stations++;
1659                         ret  = ath10k_recalc_rtscts_prot(arvif);
1660                         if (ret) {
1661                                 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
1662                                             arvif->vdev_id, ret);
1663                                 return ret;
1664                         }
1665                 }
1666
1667                 ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
1668                 if (ret) {
1669                         ath10k_warn(ar, "failed to install peer wep keys for vdev %i: %d\n",
1670                                     arvif->vdev_id, ret);
1671                         return ret;
1672                 }
1673         }
1674
1675         return ret;
1676 }
1677
1678 static int ath10k_station_disassoc(struct ath10k *ar,
1679                                    struct ieee80211_vif *vif,
1680                                    struct ieee80211_sta *sta)
1681 {
1682         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1683         int ret = 0;
1684
1685         lockdep_assert_held(&ar->conf_mutex);
1686
1687         if (!sta->wme) {
1688                 arvif->num_legacy_stations--;
1689                 ret = ath10k_recalc_rtscts_prot(arvif);
1690                 if (ret) {
1691                         ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
1692                                     arvif->vdev_id, ret);
1693                         return ret;
1694                 }
1695         }
1696
1697         ret = ath10k_clear_peer_keys(arvif, sta->addr);
1698         if (ret) {
1699                 ath10k_warn(ar, "failed to clear all peer wep keys for vdev %i: %d\n",
1700                             arvif->vdev_id, ret);
1701                 return ret;
1702         }
1703
1704         return ret;
1705 }
1706
1707 /**************/
1708 /* Regulatory */
1709 /**************/
1710
1711 static int ath10k_update_channel_list(struct ath10k *ar)
1712 {
1713         struct ieee80211_hw *hw = ar->hw;
1714         struct ieee80211_supported_band **bands;
1715         enum ieee80211_band band;
1716         struct ieee80211_channel *channel;
1717         struct wmi_scan_chan_list_arg arg = {0};
1718         struct wmi_channel_arg *ch;
1719         bool passive;
1720         int len;
1721         int ret;
1722         int i;
1723
1724         lockdep_assert_held(&ar->conf_mutex);
1725
1726         bands = hw->wiphy->bands;
1727         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1728                 if (!bands[band])
1729                         continue;
1730
1731                 for (i = 0; i < bands[band]->n_channels; i++) {
1732                         if (bands[band]->channels[i].flags &
1733                             IEEE80211_CHAN_DISABLED)
1734                                 continue;
1735
1736                         arg.n_channels++;
1737                 }
1738         }
1739
1740         len = sizeof(struct wmi_channel_arg) * arg.n_channels;
1741         arg.channels = kzalloc(len, GFP_KERNEL);
1742         if (!arg.channels)
1743                 return -ENOMEM;
1744
1745         ch = arg.channels;
1746         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1747                 if (!bands[band])
1748                         continue;
1749
1750                 for (i = 0; i < bands[band]->n_channels; i++) {
1751                         channel = &bands[band]->channels[i];
1752
1753                         if (channel->flags & IEEE80211_CHAN_DISABLED)
1754                                 continue;
1755
1756                         ch->allow_ht   = true;
1757
1758                         /* FIXME: when should we really allow VHT? */
1759                         ch->allow_vht = true;
1760
1761                         ch->allow_ibss =
1762                                 !(channel->flags & IEEE80211_CHAN_NO_IR);
1763
1764                         ch->ht40plus =
1765                                 !(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
1766
1767                         ch->chan_radar =
1768                                 !!(channel->flags & IEEE80211_CHAN_RADAR);
1769
1770                         passive = channel->flags & IEEE80211_CHAN_NO_IR;
1771                         ch->passive = passive;
1772
1773                         ch->freq = channel->center_freq;
1774                         ch->band_center_freq1 = channel->center_freq;
1775                         ch->min_power = 0;
1776                         ch->max_power = channel->max_power * 2;
1777                         ch->max_reg_power = channel->max_reg_power * 2;
1778                         ch->max_antenna_gain = channel->max_antenna_gain * 2;
1779                         ch->reg_class_id = 0; /* FIXME */
1780
1781                         /* FIXME: why use only legacy modes, why not any
1782                          * HT/VHT modes? Would that even make any
1783                          * difference? */
1784                         if (channel->band == IEEE80211_BAND_2GHZ)
1785                                 ch->mode = MODE_11G;
1786                         else
1787                                 ch->mode = MODE_11A;
1788
1789                         if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
1790                                 continue;
1791
1792                         ath10k_dbg(ar, ATH10K_DBG_WMI,
1793                                    "mac channel [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
1794                                     ch - arg.channels, arg.n_channels,
1795                                    ch->freq, ch->max_power, ch->max_reg_power,
1796                                    ch->max_antenna_gain, ch->mode);
1797
1798                         ch++;
1799                 }
1800         }
1801
1802         ret = ath10k_wmi_scan_chan_list(ar, &arg);
1803         kfree(arg.channels);
1804
1805         return ret;
1806 }
1807
1808 static enum wmi_dfs_region
1809 ath10k_mac_get_dfs_region(enum nl80211_dfs_regions dfs_region)
1810 {
1811         switch (dfs_region) {
1812         case NL80211_DFS_UNSET:
1813                 return WMI_UNINIT_DFS_DOMAIN;
1814         case NL80211_DFS_FCC:
1815                 return WMI_FCC_DFS_DOMAIN;
1816         case NL80211_DFS_ETSI:
1817                 return WMI_ETSI_DFS_DOMAIN;
1818         case NL80211_DFS_JP:
1819                 return WMI_MKK4_DFS_DOMAIN;
1820         }
1821         return WMI_UNINIT_DFS_DOMAIN;
1822 }
1823
1824 static void ath10k_regd_update(struct ath10k *ar)
1825 {
1826         struct reg_dmn_pair_mapping *regpair;
1827         int ret;
1828         enum wmi_dfs_region wmi_dfs_reg;
1829         enum nl80211_dfs_regions nl_dfs_reg;
1830
1831         lockdep_assert_held(&ar->conf_mutex);
1832
1833         ret = ath10k_update_channel_list(ar);
1834         if (ret)
1835                 ath10k_warn(ar, "failed to update channel list: %d\n", ret);
1836
1837         regpair = ar->ath_common.regulatory.regpair;
1838
1839         if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
1840                 nl_dfs_reg = ar->dfs_detector->region;
1841                 wmi_dfs_reg = ath10k_mac_get_dfs_region(nl_dfs_reg);
1842         } else {
1843                 wmi_dfs_reg = WMI_UNINIT_DFS_DOMAIN;
1844         }
1845
1846         /* Target allows setting up per-band regdomain but ath_common provides
1847          * a combined one only */
1848         ret = ath10k_wmi_pdev_set_regdomain(ar,
1849                                             regpair->reg_domain,
1850                                             regpair->reg_domain, /* 2ghz */
1851                                             regpair->reg_domain, /* 5ghz */
1852                                             regpair->reg_2ghz_ctl,
1853                                             regpair->reg_5ghz_ctl,
1854                                             wmi_dfs_reg);
1855         if (ret)
1856                 ath10k_warn(ar, "failed to set pdev regdomain: %d\n", ret);
1857 }
1858
1859 static void ath10k_reg_notifier(struct wiphy *wiphy,
1860                                 struct regulatory_request *request)
1861 {
1862         struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
1863         struct ath10k *ar = hw->priv;
1864         bool result;
1865
1866         ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
1867
1868         if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
1869                 ath10k_dbg(ar, ATH10K_DBG_REGULATORY, "dfs region 0x%x\n",
1870                            request->dfs_region);
1871                 result = ar->dfs_detector->set_dfs_domain(ar->dfs_detector,
1872                                                           request->dfs_region);
1873                 if (!result)
1874                         ath10k_warn(ar, "DFS region 0x%X not supported, will trigger radar for every pulse\n",
1875                                     request->dfs_region);
1876         }
1877
1878         mutex_lock(&ar->conf_mutex);
1879         if (ar->state == ATH10K_STATE_ON)
1880                 ath10k_regd_update(ar);
1881         mutex_unlock(&ar->conf_mutex);
1882 }
1883
1884 /***************/
1885 /* TX handlers */
1886 /***************/
1887
1888 static u8 ath10k_tx_h_get_tid(struct ieee80211_hdr *hdr)
1889 {
1890         if (ieee80211_is_mgmt(hdr->frame_control))
1891                 return HTT_DATA_TX_EXT_TID_MGMT;
1892
1893         if (!ieee80211_is_data_qos(hdr->frame_control))
1894                 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
1895
1896         if (!is_unicast_ether_addr(ieee80211_get_DA(hdr)))
1897                 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
1898
1899         return ieee80211_get_qos_ctl(hdr)[0] & IEEE80211_QOS_CTL_TID_MASK;
1900 }
1901
1902 static u8 ath10k_tx_h_get_vdev_id(struct ath10k *ar, struct ieee80211_vif *vif)
1903 {
1904         if (vif)
1905                 return ath10k_vif_to_arvif(vif)->vdev_id;
1906
1907         if (ar->monitor_started)
1908                 return ar->monitor_vdev_id;
1909
1910         ath10k_warn(ar, "failed to resolve vdev id\n");
1911         return 0;
1912 }
1913
1914 /* HTT Tx uses Native Wifi tx mode which expects 802.11 frames without QoS
1915  * Control in the header.
1916  */
1917 static void ath10k_tx_h_nwifi(struct ieee80211_hw *hw, struct sk_buff *skb)
1918 {
1919         struct ieee80211_hdr *hdr = (void *)skb->data;
1920         struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
1921         u8 *qos_ctl;
1922
1923         if (!ieee80211_is_data_qos(hdr->frame_control))
1924                 return;
1925
1926         qos_ctl = ieee80211_get_qos_ctl(hdr);
1927         memmove(skb->data + IEEE80211_QOS_CTL_LEN,
1928                 skb->data, (void *)qos_ctl - (void *)skb->data);
1929         skb_pull(skb, IEEE80211_QOS_CTL_LEN);
1930
1931         /* Fw/Hw generates a corrupted QoS Control Field for QoS NullFunc
1932          * frames. Powersave is handled by the fw/hw so QoS NyllFunc frames are
1933          * used only for CQM purposes (e.g. hostapd station keepalive ping) so
1934          * it is safe to downgrade to NullFunc.
1935          */
1936         if (ieee80211_is_qos_nullfunc(hdr->frame_control)) {
1937                 hdr->frame_control &= ~__cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
1938                 cb->htt.tid = HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
1939         }
1940 }
1941
1942 static void ath10k_tx_wep_key_work(struct work_struct *work)
1943 {
1944         struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
1945                                                 wep_key_work);
1946         struct ath10k *ar = arvif->ar;
1947         int ret, keyidx = arvif->def_wep_key_newidx;
1948
1949         mutex_lock(&arvif->ar->conf_mutex);
1950
1951         if (arvif->ar->state != ATH10K_STATE_ON)
1952                 goto unlock;
1953
1954         if (arvif->def_wep_key_idx == keyidx)
1955                 goto unlock;
1956
1957         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
1958                    arvif->vdev_id, keyidx);
1959
1960         ret = ath10k_wmi_vdev_set_param(arvif->ar,
1961                                         arvif->vdev_id,
1962                                         arvif->ar->wmi.vdev_param->def_keyid,
1963                                         keyidx);
1964         if (ret) {
1965                 ath10k_warn(ar, "failed to update wep key index for vdev %d: %d\n",
1966                             arvif->vdev_id,
1967                             ret);
1968                 goto unlock;
1969         }
1970
1971         arvif->def_wep_key_idx = keyidx;
1972
1973 unlock:
1974         mutex_unlock(&arvif->ar->conf_mutex);
1975 }
1976
1977 static void ath10k_tx_h_update_wep_key(struct ieee80211_vif *vif,
1978                                        struct ieee80211_key_conf *key,
1979                                        struct sk_buff *skb)
1980 {
1981         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1982         struct ath10k *ar = arvif->ar;
1983         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1984
1985         if (!ieee80211_has_protected(hdr->frame_control))
1986                 return;
1987
1988         if (!key)
1989                 return;
1990
1991         if (key->cipher != WLAN_CIPHER_SUITE_WEP40 &&
1992             key->cipher != WLAN_CIPHER_SUITE_WEP104)
1993                 return;
1994
1995         if (key->keyidx == arvif->def_wep_key_idx)
1996                 return;
1997
1998         /* FIXME: Most likely a few frames will be TXed with an old key. Simply
1999          * queueing frames until key index is updated is not an option because
2000          * sk_buff may need more processing to be done, e.g. offchannel */
2001         arvif->def_wep_key_newidx = key->keyidx;
2002         ieee80211_queue_work(ar->hw, &arvif->wep_key_work);
2003 }
2004
2005 static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar,
2006                                        struct ieee80211_vif *vif,
2007                                        struct sk_buff *skb)
2008 {
2009         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2010         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2011
2012         /* This is case only for P2P_GO */
2013         if (arvif->vdev_type != WMI_VDEV_TYPE_AP ||
2014             arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
2015                 return;
2016
2017         if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
2018                 spin_lock_bh(&ar->data_lock);
2019                 if (arvif->u.ap.noa_data)
2020                         if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
2021                                               GFP_ATOMIC))
2022                                 memcpy(skb_put(skb, arvif->u.ap.noa_len),
2023                                        arvif->u.ap.noa_data,
2024                                        arvif->u.ap.noa_len);
2025                 spin_unlock_bh(&ar->data_lock);
2026         }
2027 }
2028
2029 static bool ath10k_mac_need_offchan_tx_work(struct ath10k *ar)
2030 {
2031         /* FIXME: Not really sure since when the behaviour changed. At some
2032          * point new firmware stopped requiring creation of peer entries for
2033          * offchannel tx (and actually creating them causes issues with wmi-htc
2034          * tx credit replenishment and reliability). Assuming it's at least 3.4
2035          * because that's when the `freq` was introduced to TX_FRM HTT command.
2036          */
2037         return !(ar->htt.target_version_major >= 3 &&
2038                  ar->htt.target_version_minor >= 4);
2039 }
2040
2041 static void ath10k_tx_htt(struct ath10k *ar, struct sk_buff *skb)
2042 {
2043         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2044         int ret = 0;
2045
2046         if (ar->htt.target_version_major >= 3) {
2047                 /* Since HTT 3.0 there is no separate mgmt tx command */
2048                 ret = ath10k_htt_tx(&ar->htt, skb);
2049                 goto exit;
2050         }
2051
2052         if (ieee80211_is_mgmt(hdr->frame_control)) {
2053                 if (test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
2054                              ar->fw_features)) {
2055                         if (skb_queue_len(&ar->wmi_mgmt_tx_queue) >=
2056                             ATH10K_MAX_NUM_MGMT_PENDING) {
2057                                 ath10k_warn(ar, "reached WMI management transmit queue limit\n");
2058                                 ret = -EBUSY;
2059                                 goto exit;
2060                         }
2061
2062                         skb_queue_tail(&ar->wmi_mgmt_tx_queue, skb);
2063                         ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
2064                 } else {
2065                         ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
2066                 }
2067         } else if (!test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
2068                              ar->fw_features) &&
2069                    ieee80211_is_nullfunc(hdr->frame_control)) {
2070                 /* FW does not report tx status properly for NullFunc frames
2071                  * unless they are sent through mgmt tx path. mac80211 sends
2072                  * those frames when it detects link/beacon loss and depends
2073                  * on the tx status to be correct. */
2074                 ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
2075         } else {
2076                 ret = ath10k_htt_tx(&ar->htt, skb);
2077         }
2078
2079 exit:
2080         if (ret) {
2081                 ath10k_warn(ar, "failed to transmit packet, dropping: %d\n",
2082                             ret);
2083                 ieee80211_free_txskb(ar->hw, skb);
2084         }
2085 }
2086
2087 void ath10k_offchan_tx_purge(struct ath10k *ar)
2088 {
2089         struct sk_buff *skb;
2090
2091         for (;;) {
2092                 skb = skb_dequeue(&ar->offchan_tx_queue);
2093                 if (!skb)
2094                         break;
2095
2096                 ieee80211_free_txskb(ar->hw, skb);
2097         }
2098 }
2099
2100 void ath10k_offchan_tx_work(struct work_struct *work)
2101 {
2102         struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
2103         struct ath10k_peer *peer;
2104         struct ieee80211_hdr *hdr;
2105         struct sk_buff *skb;
2106         const u8 *peer_addr;
2107         int vdev_id;
2108         int ret;
2109
2110         /* FW requirement: We must create a peer before FW will send out
2111          * an offchannel frame. Otherwise the frame will be stuck and
2112          * never transmitted. We delete the peer upon tx completion.
2113          * It is unlikely that a peer for offchannel tx will already be
2114          * present. However it may be in some rare cases so account for that.
2115          * Otherwise we might remove a legitimate peer and break stuff. */
2116
2117         for (;;) {
2118                 skb = skb_dequeue(&ar->offchan_tx_queue);
2119                 if (!skb)
2120                         break;
2121
2122                 mutex_lock(&ar->conf_mutex);
2123
2124                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac offchannel skb %p\n",
2125                            skb);
2126
2127                 hdr = (struct ieee80211_hdr *)skb->data;
2128                 peer_addr = ieee80211_get_DA(hdr);
2129                 vdev_id = ATH10K_SKB_CB(skb)->vdev_id;
2130
2131                 spin_lock_bh(&ar->data_lock);
2132                 peer = ath10k_peer_find(ar, vdev_id, peer_addr);
2133                 spin_unlock_bh(&ar->data_lock);
2134
2135                 if (peer)
2136                         /* FIXME: should this use ath10k_warn()? */
2137                         ath10k_dbg(ar, ATH10K_DBG_MAC, "peer %pM on vdev %d already present\n",
2138                                    peer_addr, vdev_id);
2139
2140                 if (!peer) {
2141                         ret = ath10k_peer_create(ar, vdev_id, peer_addr);
2142                         if (ret)
2143                                 ath10k_warn(ar, "failed to create peer %pM on vdev %d: %d\n",
2144                                             peer_addr, vdev_id, ret);
2145                 }
2146
2147                 spin_lock_bh(&ar->data_lock);
2148                 reinit_completion(&ar->offchan_tx_completed);
2149                 ar->offchan_tx_skb = skb;
2150                 spin_unlock_bh(&ar->data_lock);
2151
2152                 ath10k_tx_htt(ar, skb);
2153
2154                 ret = wait_for_completion_timeout(&ar->offchan_tx_completed,
2155                                                   3 * HZ);
2156                 if (ret <= 0)
2157                         ath10k_warn(ar, "timed out waiting for offchannel skb %p\n",
2158                                     skb);
2159
2160                 if (!peer) {
2161                         ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
2162                         if (ret)
2163                                 ath10k_warn(ar, "failed to delete peer %pM on vdev %d: %d\n",
2164                                             peer_addr, vdev_id, ret);
2165                 }
2166
2167                 mutex_unlock(&ar->conf_mutex);
2168         }
2169 }
2170
2171 void ath10k_mgmt_over_wmi_tx_purge(struct ath10k *ar)
2172 {
2173         struct sk_buff *skb;
2174
2175         for (;;) {
2176                 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
2177                 if (!skb)
2178                         break;
2179
2180                 ieee80211_free_txskb(ar->hw, skb);
2181         }
2182 }
2183
2184 void ath10k_mgmt_over_wmi_tx_work(struct work_struct *work)
2185 {
2186         struct ath10k *ar = container_of(work, struct ath10k, wmi_mgmt_tx_work);
2187         struct sk_buff *skb;
2188         int ret;
2189
2190         for (;;) {
2191                 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
2192                 if (!skb)
2193                         break;
2194
2195                 ret = ath10k_wmi_mgmt_tx(ar, skb);
2196                 if (ret) {
2197                         ath10k_warn(ar, "failed to transmit management frame via WMI: %d\n",
2198                                     ret);
2199                         ieee80211_free_txskb(ar->hw, skb);
2200                 }
2201         }
2202 }
2203
2204 /************/
2205 /* Scanning */
2206 /************/
2207
2208 void __ath10k_scan_finish(struct ath10k *ar)
2209 {
2210         lockdep_assert_held(&ar->data_lock);
2211
2212         switch (ar->scan.state) {
2213         case ATH10K_SCAN_IDLE:
2214                 break;
2215         case ATH10K_SCAN_RUNNING:
2216                 if (ar->scan.is_roc)
2217                         ieee80211_remain_on_channel_expired(ar->hw);
2218         case ATH10K_SCAN_ABORTING:
2219                 if (!ar->scan.is_roc)
2220                         ieee80211_scan_completed(ar->hw,
2221                                                  (ar->scan.state ==
2222                                                   ATH10K_SCAN_ABORTING));
2223                 /* fall through */
2224         case ATH10K_SCAN_STARTING:
2225                 ar->scan.state = ATH10K_SCAN_IDLE;
2226                 ar->scan_channel = NULL;
2227                 ath10k_offchan_tx_purge(ar);
2228                 cancel_delayed_work(&ar->scan.timeout);
2229                 complete_all(&ar->scan.completed);
2230                 break;
2231         }
2232 }
2233
2234 void ath10k_scan_finish(struct ath10k *ar)
2235 {
2236         spin_lock_bh(&ar->data_lock);
2237         __ath10k_scan_finish(ar);
2238         spin_unlock_bh(&ar->data_lock);
2239 }
2240
2241 static int ath10k_scan_stop(struct ath10k *ar)
2242 {
2243         struct wmi_stop_scan_arg arg = {
2244                 .req_id = 1, /* FIXME */
2245                 .req_type = WMI_SCAN_STOP_ONE,
2246                 .u.scan_id = ATH10K_SCAN_ID,
2247         };
2248         int ret;
2249
2250         lockdep_assert_held(&ar->conf_mutex);
2251
2252         ret = ath10k_wmi_stop_scan(ar, &arg);
2253         if (ret) {
2254                 ath10k_warn(ar, "failed to stop wmi scan: %d\n", ret);
2255                 goto out;
2256         }
2257
2258         ret = wait_for_completion_timeout(&ar->scan.completed, 3*HZ);
2259         if (ret == 0) {
2260                 ath10k_warn(ar, "failed to receive scan abortion completion: timed out\n");
2261                 ret = -ETIMEDOUT;
2262         } else if (ret > 0) {
2263                 ret = 0;
2264         }
2265
2266 out:
2267         /* Scan state should be updated upon scan completion but in case
2268          * firmware fails to deliver the event (for whatever reason) it is
2269          * desired to clean up scan state anyway. Firmware may have just
2270          * dropped the scan completion event delivery due to transport pipe
2271          * being overflown with data and/or it can recover on its own before
2272          * next scan request is submitted.
2273          */
2274         spin_lock_bh(&ar->data_lock);
2275         if (ar->scan.state != ATH10K_SCAN_IDLE)
2276                 __ath10k_scan_finish(ar);
2277         spin_unlock_bh(&ar->data_lock);
2278
2279         return ret;
2280 }
2281
2282 static void ath10k_scan_abort(struct ath10k *ar)
2283 {
2284         int ret;
2285
2286         lockdep_assert_held(&ar->conf_mutex);
2287
2288         spin_lock_bh(&ar->data_lock);
2289
2290         switch (ar->scan.state) {
2291         case ATH10K_SCAN_IDLE:
2292                 /* This can happen if timeout worker kicked in and called
2293                  * abortion while scan completion was being processed.
2294                  */
2295                 break;
2296         case ATH10K_SCAN_STARTING:
2297         case ATH10K_SCAN_ABORTING:
2298                 ath10k_warn(ar, "refusing scan abortion due to invalid scan state: %s (%d)\n",
2299                             ath10k_scan_state_str(ar->scan.state),
2300                             ar->scan.state);
2301                 break;
2302         case ATH10K_SCAN_RUNNING:
2303                 ar->scan.state = ATH10K_SCAN_ABORTING;
2304                 spin_unlock_bh(&ar->data_lock);
2305
2306                 ret = ath10k_scan_stop(ar);
2307                 if (ret)
2308                         ath10k_warn(ar, "failed to abort scan: %d\n", ret);
2309
2310                 spin_lock_bh(&ar->data_lock);
2311                 break;
2312         }
2313
2314         spin_unlock_bh(&ar->data_lock);
2315 }
2316
2317 void ath10k_scan_timeout_work(struct work_struct *work)
2318 {
2319         struct ath10k *ar = container_of(work, struct ath10k,
2320                                          scan.timeout.work);
2321
2322         mutex_lock(&ar->conf_mutex);
2323         ath10k_scan_abort(ar);
2324         mutex_unlock(&ar->conf_mutex);
2325 }
2326
2327 static int ath10k_start_scan(struct ath10k *ar,
2328                              const struct wmi_start_scan_arg *arg)
2329 {
2330         int ret;
2331
2332         lockdep_assert_held(&ar->conf_mutex);
2333
2334         ret = ath10k_wmi_start_scan(ar, arg);
2335         if (ret)
2336                 return ret;
2337
2338         ret = wait_for_completion_timeout(&ar->scan.started, 1*HZ);
2339         if (ret == 0) {
2340                 ret = ath10k_scan_stop(ar);
2341                 if (ret)
2342                         ath10k_warn(ar, "failed to stop scan: %d\n", ret);
2343
2344                 return -ETIMEDOUT;
2345         }
2346
2347         /* Add a 200ms margin to account for event/command processing */
2348         ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
2349                                      msecs_to_jiffies(arg->max_scan_time+200));
2350         return 0;
2351 }
2352
2353 /**********************/
2354 /* mac80211 callbacks */
2355 /**********************/
2356
2357 static void ath10k_tx(struct ieee80211_hw *hw,
2358                       struct ieee80211_tx_control *control,
2359                       struct sk_buff *skb)
2360 {
2361         struct ath10k *ar = hw->priv;
2362         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2363         struct ieee80211_vif *vif = info->control.vif;
2364         struct ieee80211_key_conf *key = info->control.hw_key;
2365         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2366
2367         /* We should disable CCK RATE due to P2P */
2368         if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
2369                 ath10k_dbg(ar, ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
2370
2371         ATH10K_SKB_CB(skb)->htt.is_offchan = false;
2372         ATH10K_SKB_CB(skb)->htt.tid = ath10k_tx_h_get_tid(hdr);
2373         ATH10K_SKB_CB(skb)->vdev_id = ath10k_tx_h_get_vdev_id(ar, vif);
2374
2375         /* it makes no sense to process injected frames like that */
2376         if (vif && vif->type != NL80211_IFTYPE_MONITOR) {
2377                 ath10k_tx_h_nwifi(hw, skb);
2378                 ath10k_tx_h_update_wep_key(vif, key, skb);
2379                 ath10k_tx_h_add_p2p_noa_ie(ar, vif, skb);
2380                 ath10k_tx_h_seq_no(vif, skb);
2381         }
2382
2383         if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
2384                 spin_lock_bh(&ar->data_lock);
2385                 ATH10K_SKB_CB(skb)->htt.freq = ar->scan.roc_freq;
2386                 ATH10K_SKB_CB(skb)->vdev_id = ar->scan.vdev_id;
2387                 spin_unlock_bh(&ar->data_lock);
2388
2389                 if (ath10k_mac_need_offchan_tx_work(ar)) {
2390                         ATH10K_SKB_CB(skb)->htt.freq = 0;
2391                         ATH10K_SKB_CB(skb)->htt.is_offchan = true;
2392
2393                         ath10k_dbg(ar, ATH10K_DBG_MAC, "queued offchannel skb %p\n",
2394                                    skb);
2395
2396                         skb_queue_tail(&ar->offchan_tx_queue, skb);
2397                         ieee80211_queue_work(hw, &ar->offchan_tx_work);
2398                         return;
2399                 }
2400         }
2401
2402         ath10k_tx_htt(ar, skb);
2403 }
2404
2405 /* Must not be called with conf_mutex held as workers can use that also. */
2406 void ath10k_drain_tx(struct ath10k *ar)
2407 {
2408         /* make sure rcu-protected mac80211 tx path itself is drained */
2409         synchronize_net();
2410
2411         ath10k_offchan_tx_purge(ar);
2412         ath10k_mgmt_over_wmi_tx_purge(ar);
2413
2414         cancel_work_sync(&ar->offchan_tx_work);
2415         cancel_work_sync(&ar->wmi_mgmt_tx_work);
2416 }
2417
2418 void ath10k_halt(struct ath10k *ar)
2419 {
2420         struct ath10k_vif *arvif;
2421
2422         lockdep_assert_held(&ar->conf_mutex);
2423
2424         clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
2425         ar->filter_flags = 0;
2426         ar->monitor = false;
2427
2428         if (ar->monitor_started)
2429                 ath10k_monitor_stop(ar);
2430
2431         ar->monitor_started = false;
2432
2433         ath10k_scan_finish(ar);
2434         ath10k_peer_cleanup_all(ar);
2435         ath10k_core_stop(ar);
2436         ath10k_hif_power_down(ar);
2437
2438         spin_lock_bh(&ar->data_lock);
2439         list_for_each_entry(arvif, &ar->arvifs, list)
2440                 ath10k_mac_vif_beacon_cleanup(arvif);
2441         spin_unlock_bh(&ar->data_lock);
2442 }
2443
2444 static int ath10k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
2445 {
2446         struct ath10k *ar = hw->priv;
2447
2448         mutex_lock(&ar->conf_mutex);
2449
2450         if (ar->cfg_tx_chainmask) {
2451                 *tx_ant = ar->cfg_tx_chainmask;
2452                 *rx_ant = ar->cfg_rx_chainmask;
2453         } else {
2454                 *tx_ant = ar->supp_tx_chainmask;
2455                 *rx_ant = ar->supp_rx_chainmask;
2456         }
2457
2458         mutex_unlock(&ar->conf_mutex);
2459
2460         return 0;
2461 }
2462
2463 static void ath10k_check_chain_mask(struct ath10k *ar, u32 cm, const char *dbg)
2464 {
2465         /* It is not clear that allowing gaps in chainmask
2466          * is helpful.  Probably it will not do what user
2467          * is hoping for, so warn in that case.
2468          */
2469         if (cm == 15 || cm == 7 || cm == 3 || cm == 1 || cm == 0)
2470                 return;
2471
2472         ath10k_warn(ar, "mac %s antenna chainmask may be invalid: 0x%x.  Suggested values: 15, 7, 3, 1 or 0.\n",
2473                     dbg, cm);
2474 }
2475
2476 static int __ath10k_set_antenna(struct ath10k *ar, u32 tx_ant, u32 rx_ant)
2477 {
2478         int ret;
2479
2480         lockdep_assert_held(&ar->conf_mutex);
2481
2482         ath10k_check_chain_mask(ar, tx_ant, "tx");
2483         ath10k_check_chain_mask(ar, rx_ant, "rx");
2484
2485         ar->cfg_tx_chainmask = tx_ant;
2486         ar->cfg_rx_chainmask = rx_ant;
2487
2488         if ((ar->state != ATH10K_STATE_ON) &&
2489             (ar->state != ATH10K_STATE_RESTARTED))
2490                 return 0;
2491
2492         ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->tx_chain_mask,
2493                                         tx_ant);
2494         if (ret) {
2495                 ath10k_warn(ar, "failed to set tx-chainmask: %d, req 0x%x\n",
2496                             ret, tx_ant);
2497                 return ret;
2498         }
2499
2500         ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->rx_chain_mask,
2501                                         rx_ant);
2502         if (ret) {
2503                 ath10k_warn(ar, "failed to set rx-chainmask: %d, req 0x%x\n",
2504                             ret, rx_ant);
2505                 return ret;
2506         }
2507
2508         return 0;
2509 }
2510
2511 static int ath10k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
2512 {
2513         struct ath10k *ar = hw->priv;
2514         int ret;
2515
2516         mutex_lock(&ar->conf_mutex);
2517         ret = __ath10k_set_antenna(ar, tx_ant, rx_ant);
2518         mutex_unlock(&ar->conf_mutex);
2519         return ret;
2520 }
2521
2522 static int ath10k_start(struct ieee80211_hw *hw)
2523 {
2524         struct ath10k *ar = hw->priv;
2525         int ret = 0;
2526
2527         /*
2528          * This makes sense only when restarting hw. It is harmless to call
2529          * uncoditionally. This is necessary to make sure no HTT/WMI tx
2530          * commands will be submitted while restarting.
2531          */
2532         ath10k_drain_tx(ar);
2533
2534         mutex_lock(&ar->conf_mutex);
2535
2536         switch (ar->state) {
2537         case ATH10K_STATE_OFF:
2538                 ar->state = ATH10K_STATE_ON;
2539                 break;
2540         case ATH10K_STATE_RESTARTING:
2541                 ath10k_halt(ar);
2542                 ar->state = ATH10K_STATE_RESTARTED;
2543                 break;
2544         case ATH10K_STATE_ON:
2545         case ATH10K_STATE_RESTARTED:
2546         case ATH10K_STATE_WEDGED:
2547                 WARN_ON(1);
2548                 ret = -EINVAL;
2549                 goto err;
2550         case ATH10K_STATE_UTF:
2551                 ret = -EBUSY;
2552                 goto err;
2553         }
2554
2555         ret = ath10k_hif_power_up(ar);
2556         if (ret) {
2557                 ath10k_err(ar, "Could not init hif: %d\n", ret);
2558                 goto err_off;
2559         }
2560
2561         ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_NORMAL);
2562         if (ret) {
2563                 ath10k_err(ar, "Could not init core: %d\n", ret);
2564                 goto err_power_down;
2565         }
2566
2567         ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->pmf_qos, 1);
2568         if (ret) {
2569                 ath10k_warn(ar, "failed to enable PMF QOS: %d\n", ret);
2570                 goto err_core_stop;
2571         }
2572
2573         ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->dynamic_bw, 1);
2574         if (ret) {
2575                 ath10k_warn(ar, "failed to enable dynamic BW: %d\n", ret);
2576                 goto err_core_stop;
2577         }
2578
2579         if (ar->cfg_tx_chainmask)
2580                 __ath10k_set_antenna(ar, ar->cfg_tx_chainmask,
2581                                      ar->cfg_rx_chainmask);
2582
2583         /*
2584          * By default FW set ARP frames ac to voice (6). In that case ARP
2585          * exchange is not working properly for UAPSD enabled AP. ARP requests
2586          * which arrives with access category 0 are processed by network stack
2587          * and send back with access category 0, but FW changes access category
2588          * to 6. Set ARP frames access category to best effort (0) solves
2589          * this problem.
2590          */
2591
2592         ret = ath10k_wmi_pdev_set_param(ar,
2593                                         ar->wmi.pdev_param->arp_ac_override, 0);
2594         if (ret) {
2595                 ath10k_warn(ar, "failed to set arp ac override parameter: %d\n",
2596                             ret);
2597                 goto err_core_stop;
2598         }
2599
2600         ar->num_started_vdevs = 0;
2601         ath10k_regd_update(ar);
2602
2603         ath10k_spectral_start(ar);
2604
2605         mutex_unlock(&ar->conf_mutex);
2606         return 0;
2607
2608 err_core_stop:
2609         ath10k_core_stop(ar);
2610
2611 err_power_down:
2612         ath10k_hif_power_down(ar);
2613
2614 err_off:
2615         ar->state = ATH10K_STATE_OFF;
2616
2617 err:
2618         mutex_unlock(&ar->conf_mutex);
2619         return ret;
2620 }
2621
2622 static void ath10k_stop(struct ieee80211_hw *hw)
2623 {
2624         struct ath10k *ar = hw->priv;
2625
2626         ath10k_drain_tx(ar);
2627
2628         mutex_lock(&ar->conf_mutex);
2629         if (ar->state != ATH10K_STATE_OFF) {
2630                 ath10k_halt(ar);
2631                 ar->state = ATH10K_STATE_OFF;
2632         }
2633         mutex_unlock(&ar->conf_mutex);
2634
2635         cancel_delayed_work_sync(&ar->scan.timeout);
2636         cancel_work_sync(&ar->restart_work);
2637 }
2638
2639 static int ath10k_config_ps(struct ath10k *ar)
2640 {
2641         struct ath10k_vif *arvif;
2642         int ret = 0;
2643
2644         lockdep_assert_held(&ar->conf_mutex);
2645
2646         list_for_each_entry(arvif, &ar->arvifs, list) {
2647                 ret = ath10k_mac_vif_setup_ps(arvif);
2648                 if (ret) {
2649                         ath10k_warn(ar, "failed to setup powersave: %d\n", ret);
2650                         break;
2651                 }
2652         }
2653
2654         return ret;
2655 }
2656
2657 static const char *chandef_get_width(enum nl80211_chan_width width)
2658 {
2659         switch (width) {
2660         case NL80211_CHAN_WIDTH_20_NOHT:
2661                 return "20 (noht)";
2662         case NL80211_CHAN_WIDTH_20:
2663                 return "20";
2664         case NL80211_CHAN_WIDTH_40:
2665                 return "40";
2666         case NL80211_CHAN_WIDTH_80:
2667                 return "80";
2668         case NL80211_CHAN_WIDTH_80P80:
2669                 return "80+80";
2670         case NL80211_CHAN_WIDTH_160:
2671                 return "160";
2672         case NL80211_CHAN_WIDTH_5:
2673                 return "5";
2674         case NL80211_CHAN_WIDTH_10:
2675                 return "10";
2676         }
2677         return "?";
2678 }
2679
2680 static void ath10k_config_chan(struct ath10k *ar)
2681 {
2682         struct ath10k_vif *arvif;
2683         int ret;
2684
2685         lockdep_assert_held(&ar->conf_mutex);
2686
2687         ath10k_dbg(ar, ATH10K_DBG_MAC,
2688                    "mac config channel to %dMHz (cf1 %dMHz cf2 %dMHz width %s)\n",
2689                    ar->chandef.chan->center_freq,
2690                    ar->chandef.center_freq1,
2691                    ar->chandef.center_freq2,
2692                    chandef_get_width(ar->chandef.width));
2693
2694         /* First stop monitor interface. Some FW versions crash if there's a
2695          * lone monitor interface. */
2696         if (ar->monitor_started)
2697                 ath10k_monitor_stop(ar);
2698
2699         list_for_each_entry(arvif, &ar->arvifs, list) {
2700                 if (!arvif->is_started)
2701                         continue;
2702
2703                 if (!arvif->is_up)
2704                         continue;
2705
2706                 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
2707                         continue;
2708
2709                 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
2710                 if (ret) {
2711                         ath10k_warn(ar, "failed to down vdev %d: %d\n",
2712                                     arvif->vdev_id, ret);
2713                         continue;
2714                 }
2715         }
2716
2717         /* all vdevs are downed now - attempt to restart and re-up them */
2718
2719         list_for_each_entry(arvif, &ar->arvifs, list) {
2720                 if (!arvif->is_started)
2721                         continue;
2722
2723                 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
2724                         continue;
2725
2726                 ret = ath10k_vdev_restart(arvif);
2727                 if (ret) {
2728                         ath10k_warn(ar, "failed to restart vdev %d: %d\n",
2729                                     arvif->vdev_id, ret);
2730                         continue;
2731                 }
2732
2733                 if (!arvif->is_up)
2734                         continue;
2735
2736                 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
2737                                          arvif->bssid);
2738                 if (ret) {
2739                         ath10k_warn(ar, "failed to bring vdev up %d: %d\n",
2740                                     arvif->vdev_id, ret);
2741                         continue;
2742                 }
2743         }
2744
2745         ath10k_monitor_recalc(ar);
2746 }
2747
2748 static int ath10k_mac_txpower_setup(struct ath10k *ar, int txpower)
2749 {
2750         int ret;
2751         u32 param;
2752
2753         lockdep_assert_held(&ar->conf_mutex);
2754
2755         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac txpower %d\n", txpower);
2756
2757         param = ar->wmi.pdev_param->txpower_limit2g;
2758         ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
2759         if (ret) {
2760                 ath10k_warn(ar, "failed to set 2g txpower %d: %d\n",
2761                             txpower, ret);
2762                 return ret;
2763         }
2764
2765         param = ar->wmi.pdev_param->txpower_limit5g;
2766         ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
2767         if (ret) {
2768                 ath10k_warn(ar, "failed to set 5g txpower %d: %d\n",
2769                             txpower, ret);
2770                 return ret;
2771         }
2772
2773         return 0;
2774 }
2775
2776 static int ath10k_mac_txpower_recalc(struct ath10k *ar)
2777 {
2778         struct ath10k_vif *arvif;
2779         int ret, txpower = -1;
2780
2781         lockdep_assert_held(&ar->conf_mutex);
2782
2783         list_for_each_entry(arvif, &ar->arvifs, list) {
2784                 WARN_ON(arvif->txpower < 0);
2785
2786                 if (txpower == -1)
2787                         txpower = arvif->txpower;
2788                 else
2789                         txpower = min(txpower, arvif->txpower);
2790         }
2791
2792         if (WARN_ON(txpower == -1))
2793                 return -EINVAL;
2794
2795         ret = ath10k_mac_txpower_setup(ar, txpower);
2796         if (ret) {
2797                 ath10k_warn(ar, "failed to setup tx power %d: %d\n",
2798                             txpower, ret);
2799                 return ret;
2800         }
2801
2802         return 0;
2803 }
2804
2805 static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
2806 {
2807         struct ath10k *ar = hw->priv;
2808         struct ieee80211_conf *conf = &hw->conf;
2809         int ret = 0;
2810
2811         mutex_lock(&ar->conf_mutex);
2812
2813         if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
2814                 ath10k_dbg(ar, ATH10K_DBG_MAC,
2815                            "mac config channel %dMHz flags 0x%x radar %d\n",
2816                            conf->chandef.chan->center_freq,
2817                            conf->chandef.chan->flags,
2818                            conf->radar_enabled);
2819
2820                 spin_lock_bh(&ar->data_lock);
2821                 ar->rx_channel = conf->chandef.chan;
2822                 spin_unlock_bh(&ar->data_lock);
2823
2824                 ar->radar_enabled = conf->radar_enabled;
2825                 ath10k_recalc_radar_detection(ar);
2826
2827                 if (!cfg80211_chandef_identical(&ar->chandef, &conf->chandef)) {
2828                         ar->chandef = conf->chandef;
2829                         ath10k_config_chan(ar);
2830                 }
2831         }
2832
2833         if (changed & IEEE80211_CONF_CHANGE_PS)
2834                 ath10k_config_ps(ar);
2835
2836         if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
2837                 ar->monitor = conf->flags & IEEE80211_CONF_MONITOR;
2838                 ret = ath10k_monitor_recalc(ar);
2839                 if (ret)
2840                         ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
2841         }
2842
2843         mutex_unlock(&ar->conf_mutex);
2844         return ret;
2845 }
2846
2847 static u32 get_nss_from_chainmask(u16 chain_mask)
2848 {
2849         if ((chain_mask & 0x15) == 0x15)
2850                 return 4;
2851         else if ((chain_mask & 0x7) == 0x7)
2852                 return 3;
2853         else if ((chain_mask & 0x3) == 0x3)
2854                 return 2;
2855         return 1;
2856 }
2857
2858 /*
2859  * TODO:
2860  * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
2861  * because we will send mgmt frames without CCK. This requirement
2862  * for P2P_FIND/GO_NEG should be handled by checking CCK flag
2863  * in the TX packet.
2864  */
2865 static int ath10k_add_interface(struct ieee80211_hw *hw,
2866                                 struct ieee80211_vif *vif)
2867 {
2868         struct ath10k *ar = hw->priv;
2869         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2870         enum wmi_sta_powersave_param param;
2871         int ret = 0;
2872         u32 value;
2873         int bit;
2874         u32 vdev_param;
2875
2876         mutex_lock(&ar->conf_mutex);
2877
2878         memset(arvif, 0, sizeof(*arvif));
2879
2880         arvif->ar = ar;
2881         arvif->vif = vif;
2882
2883         INIT_WORK(&arvif->wep_key_work, ath10k_tx_wep_key_work);
2884         INIT_LIST_HEAD(&arvif->list);
2885
2886         if (ar->free_vdev_map == 0) {
2887                 ath10k_warn(ar, "Free vdev map is empty, no more interfaces allowed.\n");
2888                 ret = -EBUSY;
2889                 goto err;
2890         }
2891         bit = __ffs64(ar->free_vdev_map);
2892
2893         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac create vdev %i map %llx\n",
2894                    bit, ar->free_vdev_map);
2895
2896         arvif->vdev_id = bit;
2897         arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
2898
2899         if (ar->p2p)
2900                 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
2901
2902         switch (vif->type) {
2903         case NL80211_IFTYPE_UNSPECIFIED:
2904         case NL80211_IFTYPE_STATION:
2905                 arvif->vdev_type = WMI_VDEV_TYPE_STA;
2906                 if (vif->p2p)
2907                         arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
2908                 break;
2909         case NL80211_IFTYPE_ADHOC:
2910                 arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
2911                 break;
2912         case NL80211_IFTYPE_AP:
2913                 arvif->vdev_type = WMI_VDEV_TYPE_AP;
2914
2915                 if (vif->p2p)
2916                         arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
2917                 break;
2918         case NL80211_IFTYPE_MONITOR:
2919                 arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
2920                 break;
2921         default:
2922                 WARN_ON(1);
2923                 break;
2924         }
2925
2926         /* Some firmware revisions don't wait for beacon tx completion before
2927          * sending another SWBA event. This could lead to hardware using old
2928          * (freed) beacon data in some cases, e.g. tx credit starvation
2929          * combined with missed TBTT. This is very very rare.
2930          *
2931          * On non-IOMMU-enabled hosts this could be a possible security issue
2932          * because hw could beacon some random data on the air.  On
2933          * IOMMU-enabled hosts DMAR faults would occur in most cases and target
2934          * device would crash.
2935          *
2936          * Since there are no beacon tx completions (implicit nor explicit)
2937          * propagated to host the only workaround for this is to allocate a
2938          * DMA-coherent buffer for a lifetime of a vif and use it for all
2939          * beacon tx commands. Worst case for this approach is some beacons may
2940          * become corrupted, e.g. have garbled IEs or out-of-date TIM bitmap.
2941          */
2942         if (vif->type == NL80211_IFTYPE_ADHOC ||
2943             vif->type == NL80211_IFTYPE_AP) {
2944                 arvif->beacon_buf = dma_zalloc_coherent(ar->dev,
2945                                                         IEEE80211_MAX_FRAME_LEN,
2946                                                         &arvif->beacon_paddr,
2947                                                         GFP_ATOMIC);
2948                 if (!arvif->beacon_buf) {
2949                         ret = -ENOMEM;
2950                         ath10k_warn(ar, "failed to allocate beacon buffer: %d\n",
2951                                     ret);
2952                         goto err;
2953                 }
2954         }
2955
2956         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d bcnmode %s\n",
2957                    arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
2958                    arvif->beacon_buf ? "single-buf" : "per-skb");
2959
2960         ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
2961                                      arvif->vdev_subtype, vif->addr);
2962         if (ret) {
2963                 ath10k_warn(ar, "failed to create WMI vdev %i: %d\n",
2964                             arvif->vdev_id, ret);
2965                 goto err;
2966         }
2967
2968         ar->free_vdev_map &= ~(1LL << arvif->vdev_id);
2969         list_add(&arvif->list, &ar->arvifs);
2970
2971         vdev_param = ar->wmi.vdev_param->def_keyid;
2972         ret = ath10k_wmi_vdev_set_param(ar, 0, vdev_param,
2973                                         arvif->def_wep_key_idx);
2974         if (ret) {
2975                 ath10k_warn(ar, "failed to set vdev %i default key id: %d\n",
2976                             arvif->vdev_id, ret);
2977                 goto err_vdev_delete;
2978         }
2979
2980         vdev_param = ar->wmi.vdev_param->tx_encap_type;
2981         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
2982                                         ATH10K_HW_TXRX_NATIVE_WIFI);
2983         /* 10.X firmware does not support this VDEV parameter. Do not warn */
2984         if (ret && ret != -EOPNOTSUPP) {
2985                 ath10k_warn(ar, "failed to set vdev %i TX encapsulation: %d\n",
2986                             arvif->vdev_id, ret);
2987                 goto err_vdev_delete;
2988         }
2989
2990         if (ar->cfg_tx_chainmask) {
2991                 u16 nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
2992
2993                 vdev_param = ar->wmi.vdev_param->nss;
2994                 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
2995                                                 nss);
2996                 if (ret) {
2997                         ath10k_warn(ar, "failed to set vdev %i chainmask 0x%x, nss %i: %d\n",
2998                                     arvif->vdev_id, ar->cfg_tx_chainmask, nss,
2999                                     ret);
3000                         goto err_vdev_delete;
3001                 }
3002         }
3003
3004         if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3005                 ret = ath10k_peer_create(ar, arvif->vdev_id, vif->addr);
3006                 if (ret) {
3007                         ath10k_warn(ar, "failed to create vdev %i peer for AP: %d\n",
3008                                     arvif->vdev_id, ret);
3009                         goto err_vdev_delete;
3010                 }
3011
3012                 ret = ath10k_mac_set_kickout(arvif);
3013                 if (ret) {
3014                         ath10k_warn(ar, "failed to set vdev %i kickout parameters: %d\n",
3015                                     arvif->vdev_id, ret);
3016                         goto err_peer_delete;
3017                 }
3018         }
3019
3020         if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
3021                 param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
3022                 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
3023                 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3024                                                   param, value);
3025                 if (ret) {
3026                         ath10k_warn(ar, "failed to set vdev %i RX wake policy: %d\n",
3027                                     arvif->vdev_id, ret);
3028                         goto err_peer_delete;
3029                 }
3030
3031                 param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
3032                 value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
3033                 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3034                                                   param, value);
3035                 if (ret) {
3036                         ath10k_warn(ar, "failed to set vdev %i TX wake thresh: %d\n",
3037                                     arvif->vdev_id, ret);
3038                         goto err_peer_delete;
3039                 }
3040
3041                 param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
3042                 value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
3043                 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3044                                                   param, value);
3045                 if (ret) {
3046                         ath10k_warn(ar, "failed to set vdev %i PSPOLL count: %d\n",
3047                                     arvif->vdev_id, ret);
3048                         goto err_peer_delete;
3049                 }
3050         }
3051
3052         ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
3053         if (ret) {
3054                 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
3055                             arvif->vdev_id, ret);
3056                 goto err_peer_delete;
3057         }
3058
3059         ret = ath10k_mac_set_frag(arvif, ar->hw->wiphy->frag_threshold);
3060         if (ret) {
3061                 ath10k_warn(ar, "failed to set frag threshold for vdev %d: %d\n",
3062                             arvif->vdev_id, ret);
3063                 goto err_peer_delete;
3064         }
3065
3066         arvif->txpower = vif->bss_conf.txpower;
3067         ret = ath10k_mac_txpower_recalc(ar);
3068         if (ret) {
3069                 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
3070                 goto err_peer_delete;
3071         }
3072
3073         mutex_unlock(&ar->conf_mutex);
3074         return 0;
3075
3076 err_peer_delete:
3077         if (arvif->vdev_type == WMI_VDEV_TYPE_AP)
3078                 ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
3079
3080 err_vdev_delete:
3081         ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
3082         ar->free_vdev_map |= 1LL << arvif->vdev_id;
3083         list_del(&arvif->list);
3084
3085 err:
3086         if (arvif->beacon_buf) {
3087                 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
3088                                   arvif->beacon_buf, arvif->beacon_paddr);
3089                 arvif->beacon_buf = NULL;
3090         }
3091
3092         mutex_unlock(&ar->conf_mutex);
3093
3094         return ret;
3095 }
3096
3097 static void ath10k_remove_interface(struct ieee80211_hw *hw,
3098                                     struct ieee80211_vif *vif)
3099 {
3100         struct ath10k *ar = hw->priv;
3101         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3102         int ret;
3103
3104         cancel_work_sync(&arvif->wep_key_work);
3105
3106         mutex_lock(&ar->conf_mutex);
3107
3108         spin_lock_bh(&ar->data_lock);
3109         ath10k_mac_vif_beacon_cleanup(arvif);
3110         spin_unlock_bh(&ar->data_lock);
3111
3112         ret = ath10k_spectral_vif_stop(arvif);
3113         if (ret)
3114                 ath10k_warn(ar, "failed to stop spectral for vdev %i: %d\n",
3115                             arvif->vdev_id, ret);
3116
3117         ar->free_vdev_map |= 1LL << arvif->vdev_id;
3118         list_del(&arvif->list);
3119
3120         if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3121                 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, vif->addr);
3122                 if (ret)
3123                         ath10k_warn(ar, "failed to remove peer for AP vdev %i: %d\n",
3124                                     arvif->vdev_id, ret);
3125
3126                 kfree(arvif->u.ap.noa_data);
3127         }
3128
3129         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i delete (remove interface)\n",
3130                    arvif->vdev_id);
3131
3132         ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
3133         if (ret)
3134                 ath10k_warn(ar, "failed to delete WMI vdev %i: %d\n",
3135                             arvif->vdev_id, ret);
3136
3137         ath10k_peer_cleanup(ar, arvif->vdev_id);
3138
3139         mutex_unlock(&ar->conf_mutex);
3140 }
3141
3142 /*
3143  * FIXME: Has to be verified.
3144  */
3145 #define SUPPORTED_FILTERS                       \
3146         (FIF_PROMISC_IN_BSS |                   \
3147         FIF_ALLMULTI |                          \
3148         FIF_CONTROL |                           \
3149         FIF_PSPOLL |                            \
3150         FIF_OTHER_BSS |                         \
3151         FIF_BCN_PRBRESP_PROMISC |               \
3152         FIF_PROBE_REQ |                         \
3153         FIF_FCSFAIL)
3154
3155 static void ath10k_configure_filter(struct ieee80211_hw *hw,
3156                                     unsigned int changed_flags,
3157                                     unsigned int *total_flags,
3158                                     u64 multicast)
3159 {
3160         struct ath10k *ar = hw->priv;
3161         int ret;
3162
3163         mutex_lock(&ar->conf_mutex);
3164
3165         changed_flags &= SUPPORTED_FILTERS;
3166         *total_flags &= SUPPORTED_FILTERS;
3167         ar->filter_flags = *total_flags;
3168
3169         ret = ath10k_monitor_recalc(ar);
3170         if (ret)
3171                 ath10k_warn(ar, "failed to recalc montior: %d\n", ret);
3172
3173         mutex_unlock(&ar->conf_mutex);
3174 }
3175
3176 static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
3177                                     struct ieee80211_vif *vif,
3178                                     struct ieee80211_bss_conf *info,
3179                                     u32 changed)
3180 {
3181         struct ath10k *ar = hw->priv;
3182         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3183         int ret = 0;
3184         u32 vdev_param, pdev_param, slottime, preamble;
3185
3186         mutex_lock(&ar->conf_mutex);
3187
3188         if (changed & BSS_CHANGED_IBSS)
3189                 ath10k_control_ibss(arvif, info, vif->addr);
3190
3191         if (changed & BSS_CHANGED_BEACON_INT) {
3192                 arvif->beacon_interval = info->beacon_int;
3193                 vdev_param = ar->wmi.vdev_param->beacon_interval;
3194                 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3195                                                 arvif->beacon_interval);
3196                 ath10k_dbg(ar, ATH10K_DBG_MAC,
3197                            "mac vdev %d beacon_interval %d\n",
3198                            arvif->vdev_id, arvif->beacon_interval);
3199
3200                 if (ret)
3201                         ath10k_warn(ar, "failed to set beacon interval for vdev %d: %i\n",
3202                                     arvif->vdev_id, ret);
3203         }
3204
3205         if (changed & BSS_CHANGED_BEACON) {
3206                 ath10k_dbg(ar, ATH10K_DBG_MAC,
3207                            "vdev %d set beacon tx mode to staggered\n",
3208                            arvif->vdev_id);
3209
3210                 pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
3211                 ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
3212                                                 WMI_BEACON_STAGGERED_MODE);
3213                 if (ret)
3214                         ath10k_warn(ar, "failed to set beacon mode for vdev %d: %i\n",
3215                                     arvif->vdev_id, ret);
3216         }
3217
3218         if (changed & BSS_CHANGED_BEACON_INFO) {
3219                 arvif->dtim_period = info->dtim_period;
3220
3221                 ath10k_dbg(ar, ATH10K_DBG_MAC,
3222                            "mac vdev %d dtim_period %d\n",
3223                            arvif->vdev_id, arvif->dtim_period);
3224
3225                 vdev_param = ar->wmi.vdev_param->dtim_period;
3226                 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3227                                                 arvif->dtim_period);
3228                 if (ret)
3229                         ath10k_warn(ar, "failed to set dtim period for vdev %d: %i\n",
3230                                     arvif->vdev_id, ret);
3231         }
3232
3233         if (changed & BSS_CHANGED_SSID &&
3234             vif->type == NL80211_IFTYPE_AP) {
3235                 arvif->u.ap.ssid_len = info->ssid_len;
3236                 if (info->ssid_len)
3237                         memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
3238                 arvif->u.ap.hidden_ssid = info->hidden_ssid;
3239         }
3240
3241         if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
3242                 ether_addr_copy(arvif->bssid, info->bssid);
3243
3244         if (changed & BSS_CHANGED_BEACON_ENABLED)
3245                 ath10k_control_beaconing(arvif, info);
3246
3247         if (changed & BSS_CHANGED_ERP_CTS_PROT) {
3248                 arvif->use_cts_prot = info->use_cts_prot;
3249                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d cts_prot %d\n",
3250                            arvif->vdev_id, info->use_cts_prot);
3251
3252                 ret = ath10k_recalc_rtscts_prot(arvif);
3253                 if (ret)
3254                         ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
3255                                     arvif->vdev_id, ret);
3256         }
3257
3258         if (changed & BSS_CHANGED_ERP_SLOT) {
3259                 if (info->use_short_slot)
3260                         slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
3261
3262                 else
3263                         slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
3264
3265                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
3266                            arvif->vdev_id, slottime);
3267
3268                 vdev_param = ar->wmi.vdev_param->slot_time;
3269                 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3270                                                 slottime);
3271                 if (ret)
3272                         ath10k_warn(ar, "failed to set erp slot for vdev %d: %i\n",
3273                                     arvif->vdev_id, ret);
3274         }
3275
3276         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
3277                 if (info->use_short_preamble)
3278                         preamble = WMI_VDEV_PREAMBLE_SHORT;
3279                 else
3280                         preamble = WMI_VDEV_PREAMBLE_LONG;
3281
3282                 ath10k_dbg(ar, ATH10K_DBG_MAC,
3283                            "mac vdev %d preamble %dn",
3284                            arvif->vdev_id, preamble);
3285
3286                 vdev_param = ar->wmi.vdev_param->preamble;
3287                 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3288                                                 preamble);
3289                 if (ret)
3290                         ath10k_warn(ar, "failed to set preamble for vdev %d: %i\n",
3291                                     arvif->vdev_id, ret);
3292         }
3293
3294         if (changed & BSS_CHANGED_ASSOC) {
3295                 if (info->assoc) {
3296                         /* Workaround: Make sure monitor vdev is not running
3297                          * when associating to prevent some firmware revisions
3298                          * (e.g. 10.1 and 10.2) from crashing.
3299                          */
3300                         if (ar->monitor_started)
3301                                 ath10k_monitor_stop(ar);
3302                         ath10k_bss_assoc(hw, vif, info);
3303                         ath10k_monitor_recalc(ar);
3304                 } else {
3305                         ath10k_bss_disassoc(hw, vif);
3306                 }
3307         }
3308
3309         if (changed & BSS_CHANGED_TXPOWER) {
3310                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev_id %i txpower %d\n",
3311                            arvif->vdev_id, info->txpower);
3312
3313                 arvif->txpower = info->txpower;
3314                 ret = ath10k_mac_txpower_recalc(ar);
3315                 if (ret)
3316                         ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
3317         }
3318
3319         mutex_unlock(&ar->conf_mutex);
3320 }
3321
3322 static int ath10k_hw_scan(struct ieee80211_hw *hw,
3323                           struct ieee80211_vif *vif,
3324                           struct ieee80211_scan_request *hw_req)
3325 {
3326         struct ath10k *ar = hw->priv;
3327         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3328         struct cfg80211_scan_request *req = &hw_req->req;
3329         struct wmi_start_scan_arg arg;
3330         int ret = 0;
3331         int i;
3332
3333         mutex_lock(&ar->conf_mutex);
3334
3335         spin_lock_bh(&ar->data_lock);
3336         switch (ar->scan.state) {
3337         case ATH10K_SCAN_IDLE:
3338                 reinit_completion(&ar->scan.started);
3339                 reinit_completion(&ar->scan.completed);
3340                 ar->scan.state = ATH10K_SCAN_STARTING;
3341                 ar->scan.is_roc = false;
3342                 ar->scan.vdev_id = arvif->vdev_id;
3343                 ret = 0;
3344                 break;
3345         case ATH10K_SCAN_STARTING:
3346         case ATH10K_SCAN_RUNNING:
3347         case ATH10K_SCAN_ABORTING:
3348                 ret = -EBUSY;
3349                 break;
3350         }
3351         spin_unlock_bh(&ar->data_lock);
3352
3353         if (ret)
3354                 goto exit;
3355
3356         memset(&arg, 0, sizeof(arg));
3357         ath10k_wmi_start_scan_init(ar, &arg);
3358         arg.vdev_id = arvif->vdev_id;
3359         arg.scan_id = ATH10K_SCAN_ID;
3360
3361         if (!req->no_cck)
3362                 arg.scan_ctrl_flags |= WMI_SCAN_ADD_CCK_RATES;
3363
3364         if (req->ie_len) {
3365                 arg.ie_len = req->ie_len;
3366                 memcpy(arg.ie, req->ie, arg.ie_len);
3367         }
3368
3369         if (req->n_ssids) {
3370                 arg.n_ssids = req->n_ssids;
3371                 for (i = 0; i < arg.n_ssids; i++) {
3372                         arg.ssids[i].len  = req->ssids[i].ssid_len;
3373                         arg.ssids[i].ssid = req->ssids[i].ssid;
3374                 }
3375         } else {
3376                 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
3377         }
3378
3379         if (req->n_channels) {
3380                 arg.n_channels = req->n_channels;
3381                 for (i = 0; i < arg.n_channels; i++)
3382                         arg.channels[i] = req->channels[i]->center_freq;
3383         }
3384
3385         ret = ath10k_start_scan(ar, &arg);
3386         if (ret) {
3387                 ath10k_warn(ar, "failed to start hw scan: %d\n", ret);
3388                 spin_lock_bh(&ar->data_lock);
3389                 ar->scan.state = ATH10K_SCAN_IDLE;
3390                 spin_unlock_bh(&ar->data_lock);
3391         }
3392
3393 exit:
3394         mutex_unlock(&ar->conf_mutex);
3395         return ret;
3396 }
3397
3398 static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
3399                                   struct ieee80211_vif *vif)
3400 {
3401         struct ath10k *ar = hw->priv;
3402
3403         mutex_lock(&ar->conf_mutex);
3404         ath10k_scan_abort(ar);
3405         mutex_unlock(&ar->conf_mutex);
3406
3407         cancel_delayed_work_sync(&ar->scan.timeout);
3408 }
3409
3410 static void ath10k_set_key_h_def_keyidx(struct ath10k *ar,
3411                                         struct ath10k_vif *arvif,
3412                                         enum set_key_cmd cmd,
3413                                         struct ieee80211_key_conf *key)
3414 {
3415         u32 vdev_param = arvif->ar->wmi.vdev_param->def_keyid;
3416         int ret;
3417
3418         /* 10.1 firmware branch requires default key index to be set to group
3419          * key index after installing it. Otherwise FW/HW Txes corrupted
3420          * frames with multi-vif APs. This is not required for main firmware
3421          * branch (e.g. 636).
3422          *
3423          * FIXME: This has been tested only in AP. It remains unknown if this
3424          * is required for multi-vif STA interfaces on 10.1 */
3425
3426         if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
3427                 return;
3428
3429         if (key->cipher == WLAN_CIPHER_SUITE_WEP40)
3430                 return;
3431
3432         if (key->cipher == WLAN_CIPHER_SUITE_WEP104)
3433                 return;
3434
3435         if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3436                 return;
3437
3438         if (cmd != SET_KEY)
3439                 return;
3440
3441         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3442                                         key->keyidx);
3443         if (ret)
3444                 ath10k_warn(ar, "failed to set vdev %i group key as default key: %d\n",
3445                             arvif->vdev_id, ret);
3446 }
3447
3448 static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
3449                           struct ieee80211_vif *vif, struct ieee80211_sta *sta,
3450                           struct ieee80211_key_conf *key)
3451 {
3452         struct ath10k *ar = hw->priv;
3453         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3454         struct ath10k_peer *peer;
3455         const u8 *peer_addr;
3456         bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3457                       key->cipher == WLAN_CIPHER_SUITE_WEP104;
3458         int ret = 0;
3459
3460         if (key->keyidx > WMI_MAX_KEY_INDEX)
3461                 return -ENOSPC;
3462
3463         mutex_lock(&ar->conf_mutex);
3464
3465         if (sta)
3466                 peer_addr = sta->addr;
3467         else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
3468                 peer_addr = vif->bss_conf.bssid;
3469         else
3470                 peer_addr = vif->addr;
3471
3472         key->hw_key_idx = key->keyidx;
3473
3474         /* the peer should not disappear in mid-way (unless FW goes awry) since
3475          * we already hold conf_mutex. we just make sure its there now. */
3476         spin_lock_bh(&ar->data_lock);
3477         peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
3478         spin_unlock_bh(&ar->data_lock);
3479
3480         if (!peer) {
3481                 if (cmd == SET_KEY) {
3482                         ath10k_warn(ar, "failed to install key for non-existent peer %pM\n",
3483                                     peer_addr);
3484                         ret = -EOPNOTSUPP;
3485                         goto exit;
3486                 } else {
3487                         /* if the peer doesn't exist there is no key to disable
3488                          * anymore */
3489                         goto exit;
3490                 }
3491         }
3492
3493         if (is_wep) {
3494                 if (cmd == SET_KEY)
3495                         arvif->wep_keys[key->keyidx] = key;
3496                 else
3497                         arvif->wep_keys[key->keyidx] = NULL;
3498
3499                 if (cmd == DISABLE_KEY)
3500                         ath10k_clear_vdev_key(arvif, key);
3501         }
3502
3503         ret = ath10k_install_key(arvif, key, cmd, peer_addr);
3504         if (ret) {
3505                 ath10k_warn(ar, "failed to install key for vdev %i peer %pM: %d\n",
3506                             arvif->vdev_id, peer_addr, ret);
3507                 goto exit;
3508         }
3509
3510         ath10k_set_key_h_def_keyidx(ar, arvif, cmd, key);
3511
3512         spin_lock_bh(&ar->data_lock);
3513         peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
3514         if (peer && cmd == SET_KEY)
3515                 peer->keys[key->keyidx] = key;
3516         else if (peer && cmd == DISABLE_KEY)
3517                 peer->keys[key->keyidx] = NULL;
3518         else if (peer == NULL)
3519                 /* impossible unless FW goes crazy */
3520                 ath10k_warn(ar, "Peer %pM disappeared!\n", peer_addr);
3521         spin_unlock_bh(&ar->data_lock);
3522
3523 exit:
3524         mutex_unlock(&ar->conf_mutex);
3525         return ret;
3526 }
3527
3528 static void ath10k_sta_rc_update_wk(struct work_struct *wk)
3529 {
3530         struct ath10k *ar;
3531         struct ath10k_vif *arvif;
3532         struct ath10k_sta *arsta;
3533         struct ieee80211_sta *sta;
3534         u32 changed, bw, nss, smps;
3535         int err;
3536
3537         arsta = container_of(wk, struct ath10k_sta, update_wk);
3538         sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
3539         arvif = arsta->arvif;
3540         ar = arvif->ar;
3541
3542         spin_lock_bh(&ar->data_lock);
3543
3544         changed = arsta->changed;
3545         arsta->changed = 0;
3546
3547         bw = arsta->bw;
3548         nss = arsta->nss;
3549         smps = arsta->smps;
3550
3551         spin_unlock_bh(&ar->data_lock);
3552
3553         mutex_lock(&ar->conf_mutex);
3554
3555         if (changed & IEEE80211_RC_BW_CHANGED) {
3556                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM peer bw %d\n",
3557                            sta->addr, bw);
3558
3559                 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
3560                                                 WMI_PEER_CHAN_WIDTH, bw);
3561                 if (err)
3562                         ath10k_warn(ar, "failed to update STA %pM peer bw %d: %d\n",
3563                                     sta->addr, bw, err);
3564         }
3565
3566         if (changed & IEEE80211_RC_NSS_CHANGED) {
3567                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM nss %d\n",
3568                            sta->addr, nss);
3569
3570                 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
3571                                                 WMI_PEER_NSS, nss);
3572                 if (err)
3573                         ath10k_warn(ar, "failed to update STA %pM nss %d: %d\n",
3574                                     sta->addr, nss, err);
3575         }
3576
3577         if (changed & IEEE80211_RC_SMPS_CHANGED) {
3578                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM smps %d\n",
3579                            sta->addr, smps);
3580
3581                 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
3582                                                 WMI_PEER_SMPS_STATE, smps);
3583                 if (err)
3584                         ath10k_warn(ar, "failed to update STA %pM smps %d: %d\n",
3585                                     sta->addr, smps, err);
3586         }
3587
3588         if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
3589                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM supp rates\n",
3590                            sta->addr);
3591
3592                 err = ath10k_station_assoc(ar, arvif->vif, sta, true);
3593                 if (err)
3594                         ath10k_warn(ar, "failed to reassociate station: %pM\n",
3595                                     sta->addr);
3596         }
3597
3598         mutex_unlock(&ar->conf_mutex);
3599 }
3600
3601 static int ath10k_mac_inc_num_stations(struct ath10k_vif *arvif)
3602 {
3603         struct ath10k *ar = arvif->ar;
3604
3605         lockdep_assert_held(&ar->conf_mutex);
3606
3607         if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
3608             arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
3609                 return 0;
3610
3611         if (ar->num_stations >= ar->max_num_stations)
3612                 return -ENOBUFS;
3613
3614         ar->num_stations++;
3615
3616         return 0;
3617 }
3618
3619 static void ath10k_mac_dec_num_stations(struct ath10k_vif *arvif)
3620 {
3621         struct ath10k *ar = arvif->ar;
3622
3623         lockdep_assert_held(&ar->conf_mutex);
3624
3625         if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
3626             arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
3627                 return;
3628
3629         ar->num_stations--;
3630 }
3631
3632 static int ath10k_sta_state(struct ieee80211_hw *hw,
3633                             struct ieee80211_vif *vif,
3634                             struct ieee80211_sta *sta,
3635                             enum ieee80211_sta_state old_state,
3636                             enum ieee80211_sta_state new_state)
3637 {
3638         struct ath10k *ar = hw->priv;
3639         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3640         struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
3641         int ret = 0;
3642
3643         if (old_state == IEEE80211_STA_NOTEXIST &&
3644             new_state == IEEE80211_STA_NONE) {
3645                 memset(arsta, 0, sizeof(*arsta));
3646                 arsta->arvif = arvif;
3647                 INIT_WORK(&arsta->update_wk, ath10k_sta_rc_update_wk);
3648         }
3649
3650         /* cancel must be done outside the mutex to avoid deadlock */
3651         if ((old_state == IEEE80211_STA_NONE &&
3652              new_state == IEEE80211_STA_NOTEXIST))
3653                 cancel_work_sync(&arsta->update_wk);
3654
3655         mutex_lock(&ar->conf_mutex);
3656
3657         if (old_state == IEEE80211_STA_NOTEXIST &&
3658             new_state == IEEE80211_STA_NONE) {
3659                 /*
3660                  * New station addition.
3661                  */
3662                 ath10k_dbg(ar, ATH10K_DBG_MAC,
3663                            "mac vdev %d peer create %pM (new sta) sta %d / %d peer %d / %d\n",
3664                            arvif->vdev_id, sta->addr,
3665                            ar->num_stations + 1, ar->max_num_stations,
3666                            ar->num_peers + 1, ar->max_num_peers);
3667
3668                 ret = ath10k_mac_inc_num_stations(arvif);
3669                 if (ret) {
3670                         ath10k_warn(ar, "refusing to associate station: too many connected already (%d)\n",
3671                                     ar->max_num_stations);
3672                         goto exit;
3673                 }
3674
3675                 ret = ath10k_peer_create(ar, arvif->vdev_id, sta->addr);
3676                 if (ret) {
3677                         ath10k_warn(ar, "failed to add peer %pM for vdev %d when adding a new sta: %i\n",
3678                                     sta->addr, arvif->vdev_id, ret);
3679                         ath10k_mac_dec_num_stations(arvif);
3680                         goto exit;
3681                 }
3682
3683                 if (vif->type == NL80211_IFTYPE_STATION) {
3684                         WARN_ON(arvif->is_started);
3685
3686                         ret = ath10k_vdev_start(arvif);
3687                         if (ret) {
3688                                 ath10k_warn(ar, "failed to start vdev %i: %d\n",
3689                                             arvif->vdev_id, ret);
3690                                 WARN_ON(ath10k_peer_delete(ar, arvif->vdev_id,
3691                                                            sta->addr));
3692                                 ath10k_mac_dec_num_stations(arvif);
3693                                 goto exit;
3694                         }
3695
3696                         arvif->is_started = true;
3697                 }
3698         } else if ((old_state == IEEE80211_STA_NONE &&
3699                     new_state == IEEE80211_STA_NOTEXIST)) {
3700                 /*
3701                  * Existing station deletion.
3702                  */
3703                 ath10k_dbg(ar, ATH10K_DBG_MAC,
3704                            "mac vdev %d peer delete %pM (sta gone)\n",
3705                            arvif->vdev_id, sta->addr);
3706
3707                 if (vif->type == NL80211_IFTYPE_STATION) {
3708                         WARN_ON(!arvif->is_started);
3709
3710                         ret = ath10k_vdev_stop(arvif);
3711                         if (ret)
3712                                 ath10k_warn(ar, "failed to stop vdev %i: %d\n",
3713                                             arvif->vdev_id, ret);
3714
3715                         arvif->is_started = false;
3716                 }
3717
3718                 ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
3719                 if (ret)
3720                         ath10k_warn(ar, "failed to delete peer %pM for vdev %d: %i\n",
3721                                     sta->addr, arvif->vdev_id, ret);
3722
3723                 ath10k_mac_dec_num_stations(arvif);
3724         } else if (old_state == IEEE80211_STA_AUTH &&
3725                    new_state == IEEE80211_STA_ASSOC &&
3726                    (vif->type == NL80211_IFTYPE_AP ||
3727                     vif->type == NL80211_IFTYPE_ADHOC)) {
3728                 /*
3729                  * New association.
3730                  */
3731                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM associated\n",
3732                            sta->addr);
3733
3734                 ret = ath10k_station_assoc(ar, vif, sta, false);
3735                 if (ret)
3736                         ath10k_warn(ar, "failed to associate station %pM for vdev %i: %i\n",
3737                                     sta->addr, arvif->vdev_id, ret);
3738         } else if (old_state == IEEE80211_STA_ASSOC &&
3739                    new_state == IEEE80211_STA_AUTH &&
3740                    (vif->type == NL80211_IFTYPE_AP ||
3741                     vif->type == NL80211_IFTYPE_ADHOC)) {
3742                 /*
3743                  * Disassociation.
3744                  */
3745                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM disassociated\n",
3746                            sta->addr);
3747
3748                 ret = ath10k_station_disassoc(ar, vif, sta);
3749                 if (ret)
3750                         ath10k_warn(ar, "failed to disassociate station: %pM vdev %i: %i\n",
3751                                     sta->addr, arvif->vdev_id, ret);
3752         }
3753 exit:
3754         mutex_unlock(&ar->conf_mutex);
3755         return ret;
3756 }
3757
3758 static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
3759                                 u16 ac, bool enable)
3760 {
3761         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3762         u32 value = 0;
3763         int ret = 0;
3764
3765         lockdep_assert_held(&ar->conf_mutex);
3766
3767         if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
3768                 return 0;
3769
3770         switch (ac) {
3771         case IEEE80211_AC_VO:
3772                 value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
3773                         WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
3774                 break;
3775         case IEEE80211_AC_VI:
3776                 value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
3777                         WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
3778                 break;
3779         case IEEE80211_AC_BE:
3780                 value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
3781                         WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
3782                 break;
3783         case IEEE80211_AC_BK:
3784                 value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
3785                         WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
3786                 break;
3787         }
3788
3789         if (enable)
3790                 arvif->u.sta.uapsd |= value;
3791         else
3792                 arvif->u.sta.uapsd &= ~value;
3793
3794         ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3795                                           WMI_STA_PS_PARAM_UAPSD,
3796                                           arvif->u.sta.uapsd);
3797         if (ret) {
3798                 ath10k_warn(ar, "failed to set uapsd params: %d\n", ret);
3799                 goto exit;
3800         }
3801
3802         if (arvif->u.sta.uapsd)
3803                 value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
3804         else
3805                 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
3806
3807         ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3808                                           WMI_STA_PS_PARAM_RX_WAKE_POLICY,
3809                                           value);
3810         if (ret)
3811                 ath10k_warn(ar, "failed to set rx wake param: %d\n", ret);
3812
3813 exit:
3814         return ret;
3815 }
3816
3817 static int ath10k_conf_tx(struct ieee80211_hw *hw,
3818                           struct ieee80211_vif *vif, u16 ac,
3819                           const struct ieee80211_tx_queue_params *params)
3820 {
3821         struct ath10k *ar = hw->priv;
3822         struct wmi_wmm_params_arg *p = NULL;
3823         int ret;
3824
3825         mutex_lock(&ar->conf_mutex);
3826
3827         switch (ac) {
3828         case IEEE80211_AC_VO:
3829                 p = &ar->wmm_params.ac_vo;
3830                 break;
3831         case IEEE80211_AC_VI:
3832                 p = &ar->wmm_params.ac_vi;
3833                 break;
3834         case IEEE80211_AC_BE:
3835                 p = &ar->wmm_params.ac_be;
3836                 break;
3837         case IEEE80211_AC_BK:
3838                 p = &ar->wmm_params.ac_bk;
3839                 break;
3840         }
3841
3842         if (WARN_ON(!p)) {
3843                 ret = -EINVAL;
3844                 goto exit;
3845         }
3846
3847         p->cwmin = params->cw_min;
3848         p->cwmax = params->cw_max;
3849         p->aifs = params->aifs;
3850
3851         /*
3852          * The channel time duration programmed in the HW is in absolute
3853          * microseconds, while mac80211 gives the txop in units of
3854          * 32 microseconds.
3855          */
3856         p->txop = params->txop * 32;
3857
3858         /* FIXME: FW accepts wmm params per hw, not per vif */
3859         ret = ath10k_wmi_pdev_set_wmm_params(ar, &ar->wmm_params);
3860         if (ret) {
3861                 ath10k_warn(ar, "failed to set wmm params: %d\n", ret);
3862                 goto exit;
3863         }
3864
3865         ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
3866         if (ret)
3867                 ath10k_warn(ar, "failed to set sta uapsd: %d\n", ret);
3868
3869 exit:
3870         mutex_unlock(&ar->conf_mutex);
3871         return ret;
3872 }
3873
3874 #define ATH10K_ROC_TIMEOUT_HZ (2*HZ)
3875
3876 static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
3877                                     struct ieee80211_vif *vif,
3878                                     struct ieee80211_channel *chan,
3879                                     int duration,
3880                                     enum ieee80211_roc_type type)
3881 {
3882         struct ath10k *ar = hw->priv;
3883         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3884         struct wmi_start_scan_arg arg;
3885         int ret = 0;
3886
3887         mutex_lock(&ar->conf_mutex);
3888
3889         spin_lock_bh(&ar->data_lock);
3890         switch (ar->scan.state) {
3891         case ATH10K_SCAN_IDLE:
3892                 reinit_completion(&ar->scan.started);
3893                 reinit_completion(&ar->scan.completed);
3894                 reinit_completion(&ar->scan.on_channel);
3895                 ar->scan.state = ATH10K_SCAN_STARTING;
3896                 ar->scan.is_roc = true;
3897                 ar->scan.vdev_id = arvif->vdev_id;
3898                 ar->scan.roc_freq = chan->center_freq;
3899                 ret = 0;
3900                 break;
3901         case ATH10K_SCAN_STARTING:
3902         case ATH10K_SCAN_RUNNING:
3903         case ATH10K_SCAN_ABORTING:
3904                 ret = -EBUSY;
3905                 break;
3906         }
3907         spin_unlock_bh(&ar->data_lock);
3908
3909         if (ret)
3910                 goto exit;
3911
3912         duration = max(duration, WMI_SCAN_CHAN_MIN_TIME_MSEC);
3913
3914         memset(&arg, 0, sizeof(arg));
3915         ath10k_wmi_start_scan_init(ar, &arg);
3916         arg.vdev_id = arvif->vdev_id;
3917         arg.scan_id = ATH10K_SCAN_ID;
3918         arg.n_channels = 1;
3919         arg.channels[0] = chan->center_freq;
3920         arg.dwell_time_active = duration;
3921         arg.dwell_time_passive = duration;
3922         arg.max_scan_time = 2 * duration;
3923         arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
3924         arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
3925
3926         ret = ath10k_start_scan(ar, &arg);
3927         if (ret) {
3928                 ath10k_warn(ar, "failed to start roc scan: %d\n", ret);
3929                 spin_lock_bh(&ar->data_lock);
3930                 ar->scan.state = ATH10K_SCAN_IDLE;
3931                 spin_unlock_bh(&ar->data_lock);
3932                 goto exit;
3933         }
3934
3935         ret = wait_for_completion_timeout(&ar->scan.on_channel, 3*HZ);
3936         if (ret == 0) {
3937                 ath10k_warn(ar, "failed to switch to channel for roc scan\n");
3938
3939                 ret = ath10k_scan_stop(ar);
3940                 if (ret)
3941                         ath10k_warn(ar, "failed to stop scan: %d\n", ret);
3942
3943                 ret = -ETIMEDOUT;
3944                 goto exit;
3945         }
3946
3947         ret = 0;
3948 exit:
3949         mutex_unlock(&ar->conf_mutex);
3950         return ret;
3951 }
3952
3953 static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw)
3954 {
3955         struct ath10k *ar = hw->priv;
3956
3957         mutex_lock(&ar->conf_mutex);
3958         ath10k_scan_abort(ar);
3959         mutex_unlock(&ar->conf_mutex);
3960
3961         cancel_delayed_work_sync(&ar->scan.timeout);
3962
3963         return 0;
3964 }
3965
3966 /*
3967  * Both RTS and Fragmentation threshold are interface-specific
3968  * in ath10k, but device-specific in mac80211.
3969  */
3970
3971 static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3972 {
3973         struct ath10k *ar = hw->priv;
3974         struct ath10k_vif *arvif;
3975         int ret = 0;
3976
3977         mutex_lock(&ar->conf_mutex);
3978         list_for_each_entry(arvif, &ar->arvifs, list) {
3979                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
3980                            arvif->vdev_id, value);
3981
3982                 ret = ath10k_mac_set_rts(arvif, value);
3983                 if (ret) {
3984                         ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
3985                                     arvif->vdev_id, ret);
3986                         break;
3987                 }
3988         }
3989         mutex_unlock(&ar->conf_mutex);
3990
3991         return ret;
3992 }
3993
3994 static void ath10k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3995                          u32 queues, bool drop)
3996 {
3997         struct ath10k *ar = hw->priv;
3998         bool skip;
3999         int ret;
4000
4001         /* mac80211 doesn't care if we really xmit queued frames or not
4002          * we'll collect those frames either way if we stop/delete vdevs */
4003         if (drop)
4004                 return;
4005
4006         mutex_lock(&ar->conf_mutex);
4007
4008         if (ar->state == ATH10K_STATE_WEDGED)
4009                 goto skip;
4010
4011         ret = wait_event_timeout(ar->htt.empty_tx_wq, ({
4012                         bool empty;
4013
4014                         spin_lock_bh(&ar->htt.tx_lock);
4015                         empty = (ar->htt.num_pending_tx == 0);
4016                         spin_unlock_bh(&ar->htt.tx_lock);
4017
4018                         skip = (ar->state == ATH10K_STATE_WEDGED) ||
4019                                test_bit(ATH10K_FLAG_CRASH_FLUSH,
4020                                         &ar->dev_flags);
4021
4022                         (empty || skip);
4023                 }), ATH10K_FLUSH_TIMEOUT_HZ);
4024
4025         if (ret <= 0 || skip)
4026                 ath10k_warn(ar, "failed to flush transmit queue (skip %i ar-state %i): %i\n",
4027                             skip, ar->state, ret);
4028
4029 skip:
4030         mutex_unlock(&ar->conf_mutex);
4031 }
4032
4033 /* TODO: Implement this function properly
4034  * For now it is needed to reply to Probe Requests in IBSS mode.
4035  * Propably we need this information from FW.
4036  */
4037 static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
4038 {
4039         return 1;
4040 }
4041
4042 #ifdef CONFIG_PM
4043 static int ath10k_suspend(struct ieee80211_hw *hw,
4044                           struct cfg80211_wowlan *wowlan)
4045 {
4046         struct ath10k *ar = hw->priv;
4047         int ret;
4048
4049         mutex_lock(&ar->conf_mutex);
4050
4051         ret = ath10k_wait_for_suspend(ar, WMI_PDEV_SUSPEND);
4052         if (ret) {
4053                 if (ret == -ETIMEDOUT)
4054                         goto resume;
4055                 ret = 1;
4056                 goto exit;
4057         }
4058
4059         ret = ath10k_hif_suspend(ar);
4060         if (ret) {
4061                 ath10k_warn(ar, "failed to suspend hif: %d\n", ret);
4062                 goto resume;
4063         }
4064
4065         ret = 0;
4066         goto exit;
4067 resume:
4068         ret = ath10k_wmi_pdev_resume_target(ar);
4069         if (ret)
4070                 ath10k_warn(ar, "failed to resume target: %d\n", ret);
4071
4072         ret = 1;
4073 exit:
4074         mutex_unlock(&ar->conf_mutex);
4075         return ret;
4076 }
4077
4078 static int ath10k_resume(struct ieee80211_hw *hw)
4079 {
4080         struct ath10k *ar = hw->priv;
4081         int ret;
4082
4083         mutex_lock(&ar->conf_mutex);
4084
4085         ret = ath10k_hif_resume(ar);
4086         if (ret) {
4087                 ath10k_warn(ar, "failed to resume hif: %d\n", ret);
4088                 ret = 1;
4089                 goto exit;
4090         }
4091
4092         ret = ath10k_wmi_pdev_resume_target(ar);
4093         if (ret) {
4094                 ath10k_warn(ar, "failed to resume target: %d\n", ret);
4095                 ret = 1;
4096                 goto exit;
4097         }
4098
4099         ret = 0;
4100 exit:
4101         mutex_unlock(&ar->conf_mutex);
4102         return ret;
4103 }
4104 #endif
4105
4106 static void ath10k_reconfig_complete(struct ieee80211_hw *hw,
4107                                      enum ieee80211_reconfig_type reconfig_type)
4108 {
4109         struct ath10k *ar = hw->priv;
4110
4111         if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
4112                 return;
4113
4114         mutex_lock(&ar->conf_mutex);
4115
4116         /* If device failed to restart it will be in a different state, e.g.
4117          * ATH10K_STATE_WEDGED */
4118         if (ar->state == ATH10K_STATE_RESTARTED) {
4119                 ath10k_info(ar, "device successfully recovered\n");
4120                 ar->state = ATH10K_STATE_ON;
4121                 ieee80211_wake_queues(ar->hw);
4122         }
4123
4124         mutex_unlock(&ar->conf_mutex);
4125 }
4126
4127 static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
4128                              struct survey_info *survey)
4129 {
4130         struct ath10k *ar = hw->priv;
4131         struct ieee80211_supported_band *sband;
4132         struct survey_info *ar_survey = &ar->survey[idx];
4133         int ret = 0;
4134
4135         mutex_lock(&ar->conf_mutex);
4136
4137         sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
4138         if (sband && idx >= sband->n_channels) {
4139                 idx -= sband->n_channels;
4140                 sband = NULL;
4141         }
4142
4143         if (!sband)
4144                 sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
4145
4146         if (!sband || idx >= sband->n_channels) {
4147                 ret = -ENOENT;
4148                 goto exit;
4149         }
4150
4151         spin_lock_bh(&ar->data_lock);
4152         memcpy(survey, ar_survey, sizeof(*survey));
4153         spin_unlock_bh(&ar->data_lock);
4154
4155         survey->channel = &sband->channels[idx];
4156
4157         if (ar->rx_channel == survey->channel)
4158                 survey->filled |= SURVEY_INFO_IN_USE;
4159
4160 exit:
4161         mutex_unlock(&ar->conf_mutex);
4162         return ret;
4163 }
4164
4165 /* Helper table for legacy fixed_rate/bitrate_mask */
4166 static const u8 cck_ofdm_rate[] = {
4167         /* CCK */
4168         3, /* 1Mbps */
4169         2, /* 2Mbps */
4170         1, /* 5.5Mbps */
4171         0, /* 11Mbps */
4172         /* OFDM */
4173         3, /* 6Mbps */
4174         7, /* 9Mbps */
4175         2, /* 12Mbps */
4176         6, /* 18Mbps */
4177         1, /* 24Mbps */
4178         5, /* 36Mbps */
4179         0, /* 48Mbps */
4180         4, /* 54Mbps */
4181 };
4182
4183 /* Check if only one bit set */
4184 static int ath10k_check_single_mask(u32 mask)
4185 {
4186         int bit;
4187
4188         bit = ffs(mask);
4189         if (!bit)
4190                 return 0;
4191
4192         mask &= ~BIT(bit - 1);
4193         if (mask)
4194                 return 2;
4195
4196         return 1;
4197 }
4198
4199 static bool
4200 ath10k_default_bitrate_mask(struct ath10k *ar,
4201                             enum ieee80211_band band,
4202                             const struct cfg80211_bitrate_mask *mask)
4203 {
4204         u32 legacy = 0x00ff;
4205         u8 ht = 0xff, i;
4206         u16 vht = 0x3ff;
4207         u16 nrf = ar->num_rf_chains;
4208
4209         if (ar->cfg_tx_chainmask)
4210                 nrf = get_nss_from_chainmask(ar->cfg_tx_chainmask);
4211
4212         switch (band) {
4213         case IEEE80211_BAND_2GHZ:
4214                 legacy = 0x00fff;
4215                 vht = 0;
4216                 break;
4217         case IEEE80211_BAND_5GHZ:
4218                 break;
4219         default:
4220                 return false;
4221         }
4222
4223         if (mask->control[band].legacy != legacy)
4224                 return false;
4225
4226         for (i = 0; i < nrf; i++)
4227                 if (mask->control[band].ht_mcs[i] != ht)
4228                         return false;
4229
4230         for (i = 0; i < nrf; i++)
4231                 if (mask->control[band].vht_mcs[i] != vht)
4232                         return false;
4233
4234         return true;
4235 }
4236
4237 static bool
4238 ath10k_bitrate_mask_nss(const struct cfg80211_bitrate_mask *mask,
4239                         enum ieee80211_band band,
4240                         u8 *fixed_nss)
4241 {
4242         int ht_nss = 0, vht_nss = 0, i;
4243
4244         /* check legacy */
4245         if (ath10k_check_single_mask(mask->control[band].legacy))
4246                 return false;
4247
4248         /* check HT */
4249         for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
4250                 if (mask->control[band].ht_mcs[i] == 0xff)
4251                         continue;
4252                 else if (mask->control[band].ht_mcs[i] == 0x00)
4253                         break;
4254
4255                 return false;
4256         }
4257
4258         ht_nss = i;
4259
4260         /* check VHT */
4261         for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
4262                 if (mask->control[band].vht_mcs[i] == 0x03ff)
4263                         continue;
4264                 else if (mask->control[band].vht_mcs[i] == 0x0000)
4265                         break;
4266
4267                 return false;
4268         }
4269
4270         vht_nss = i;
4271
4272         if (ht_nss > 0 && vht_nss > 0)
4273                 return false;
4274
4275         if (ht_nss)
4276                 *fixed_nss = ht_nss;
4277         else if (vht_nss)
4278                 *fixed_nss = vht_nss;
4279         else
4280                 return false;
4281
4282         return true;
4283 }
4284
4285 static bool
4286 ath10k_bitrate_mask_correct(const struct cfg80211_bitrate_mask *mask,
4287                             enum ieee80211_band band,
4288                             enum wmi_rate_preamble *preamble)
4289 {
4290         int legacy = 0, ht = 0, vht = 0, i;
4291
4292         *preamble = WMI_RATE_PREAMBLE_OFDM;
4293
4294         /* check legacy */
4295         legacy = ath10k_check_single_mask(mask->control[band].legacy);
4296         if (legacy > 1)
4297                 return false;
4298
4299         /* check HT */
4300         for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
4301                 ht += ath10k_check_single_mask(mask->control[band].ht_mcs[i]);
4302         if (ht > 1)
4303                 return false;
4304
4305         /* check VHT */
4306         for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
4307                 vht += ath10k_check_single_mask(mask->control[band].vht_mcs[i]);
4308         if (vht > 1)
4309                 return false;
4310
4311         /* Currently we support only one fixed_rate */
4312         if ((legacy + ht + vht) != 1)
4313                 return false;
4314
4315         if (ht)
4316                 *preamble = WMI_RATE_PREAMBLE_HT;
4317         else if (vht)
4318                 *preamble = WMI_RATE_PREAMBLE_VHT;
4319
4320         return true;
4321 }
4322
4323 static bool
4324 ath10k_bitrate_mask_rate(struct ath10k *ar,
4325                          const struct cfg80211_bitrate_mask *mask,
4326                          enum ieee80211_band band,
4327                          u8 *fixed_rate,
4328                          u8 *fixed_nss)
4329 {
4330         u8 rate = 0, pream = 0, nss = 0, i;
4331         enum wmi_rate_preamble preamble;
4332
4333         /* Check if single rate correct */
4334         if (!ath10k_bitrate_mask_correct(mask, band, &preamble))
4335                 return false;
4336
4337         pream = preamble;
4338
4339         switch (preamble) {
4340         case WMI_RATE_PREAMBLE_CCK:
4341         case WMI_RATE_PREAMBLE_OFDM:
4342                 i = ffs(mask->control[band].legacy) - 1;
4343
4344                 if (band == IEEE80211_BAND_2GHZ && i < 4)
4345                         pream = WMI_RATE_PREAMBLE_CCK;
4346
4347                 if (band == IEEE80211_BAND_5GHZ)
4348                         i += 4;
4349
4350                 if (i >= ARRAY_SIZE(cck_ofdm_rate))
4351                         return false;
4352
4353                 rate = cck_ofdm_rate[i];
4354                 break;
4355         case WMI_RATE_PREAMBLE_HT:
4356                 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
4357                         if (mask->control[band].ht_mcs[i])
4358                                 break;
4359
4360                 if (i == IEEE80211_HT_MCS_MASK_LEN)
4361                         return false;
4362
4363                 rate = ffs(mask->control[band].ht_mcs[i]) - 1;
4364                 nss = i;
4365                 break;
4366         case WMI_RATE_PREAMBLE_VHT:
4367                 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
4368                         if (mask->control[band].vht_mcs[i])
4369                                 break;
4370
4371                 if (i == NL80211_VHT_NSS_MAX)
4372                         return false;
4373
4374                 rate = ffs(mask->control[band].vht_mcs[i]) - 1;
4375                 nss = i;
4376                 break;
4377         }
4378
4379         *fixed_nss = nss + 1;
4380         nss <<= 4;
4381         pream <<= 6;
4382
4383         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac fixed rate pream 0x%02x nss 0x%02x rate 0x%02x\n",
4384                    pream, nss, rate);
4385
4386         *fixed_rate = pream | nss | rate;
4387
4388         return true;
4389 }
4390
4391 static bool ath10k_get_fixed_rate_nss(struct ath10k *ar,
4392                                       const struct cfg80211_bitrate_mask *mask,
4393                                       enum ieee80211_band band,
4394                                       u8 *fixed_rate,
4395                                       u8 *fixed_nss)
4396 {
4397         /* First check full NSS mask, if we can simply limit NSS */
4398         if (ath10k_bitrate_mask_nss(mask, band, fixed_nss))
4399                 return true;
4400
4401         /* Next Check single rate is set */
4402         return ath10k_bitrate_mask_rate(ar, mask, band, fixed_rate, fixed_nss);
4403 }
4404
4405 static int ath10k_set_fixed_rate_param(struct ath10k_vif *arvif,
4406                                        u8 fixed_rate,
4407                                        u8 fixed_nss,
4408                                        u8 force_sgi)
4409 {
4410         struct ath10k *ar = arvif->ar;
4411         u32 vdev_param;
4412         int ret = 0;
4413
4414         mutex_lock(&ar->conf_mutex);
4415
4416         if (arvif->fixed_rate == fixed_rate &&
4417             arvif->fixed_nss == fixed_nss &&
4418             arvif->force_sgi == force_sgi)
4419                 goto exit;
4420
4421         if (fixed_rate == WMI_FIXED_RATE_NONE)
4422                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac disable fixed bitrate mask\n");
4423
4424         if (force_sgi)
4425                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac force sgi\n");
4426
4427         vdev_param = ar->wmi.vdev_param->fixed_rate;
4428         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
4429                                         vdev_param, fixed_rate);
4430         if (ret) {
4431                 ath10k_warn(ar, "failed to set fixed rate param 0x%02x: %d\n",
4432                             fixed_rate, ret);
4433                 ret = -EINVAL;
4434                 goto exit;
4435         }
4436
4437         arvif->fixed_rate = fixed_rate;
4438
4439         vdev_param = ar->wmi.vdev_param->nss;
4440         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
4441                                         vdev_param, fixed_nss);
4442
4443         if (ret) {
4444                 ath10k_warn(ar, "failed to set fixed nss param %d: %d\n",
4445                             fixed_nss, ret);
4446                 ret = -EINVAL;
4447                 goto exit;
4448         }
4449
4450         arvif->fixed_nss = fixed_nss;
4451
4452         vdev_param = ar->wmi.vdev_param->sgi;
4453         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
4454                                         force_sgi);
4455
4456         if (ret) {
4457                 ath10k_warn(ar, "failed to set sgi param %d: %d\n",
4458                             force_sgi, ret);
4459                 ret = -EINVAL;
4460                 goto exit;
4461         }
4462
4463         arvif->force_sgi = force_sgi;
4464
4465 exit:
4466         mutex_unlock(&ar->conf_mutex);
4467         return ret;
4468 }
4469
4470 static int ath10k_set_bitrate_mask(struct ieee80211_hw *hw,
4471                                    struct ieee80211_vif *vif,
4472                                    const struct cfg80211_bitrate_mask *mask)
4473 {
4474         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4475         struct ath10k *ar = arvif->ar;
4476         enum ieee80211_band band = ar->hw->conf.chandef.chan->band;
4477         u8 fixed_rate = WMI_FIXED_RATE_NONE;
4478         u8 fixed_nss = ar->num_rf_chains;
4479         u8 force_sgi;
4480
4481         if (ar->cfg_tx_chainmask)
4482                 fixed_nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
4483
4484         force_sgi = mask->control[band].gi;
4485         if (force_sgi == NL80211_TXRATE_FORCE_LGI)
4486                 return -EINVAL;
4487
4488         if (!ath10k_default_bitrate_mask(ar, band, mask)) {
4489                 if (!ath10k_get_fixed_rate_nss(ar, mask, band,
4490                                                &fixed_rate,
4491                                                &fixed_nss))
4492                         return -EINVAL;
4493         }
4494
4495         if (fixed_rate == WMI_FIXED_RATE_NONE && force_sgi) {
4496                 ath10k_warn(ar, "failed to force SGI usage for default rate settings\n");
4497                 return -EINVAL;
4498         }
4499
4500         return ath10k_set_fixed_rate_param(arvif, fixed_rate,
4501                                            fixed_nss, force_sgi);
4502 }
4503
4504 static void ath10k_sta_rc_update(struct ieee80211_hw *hw,
4505                                  struct ieee80211_vif *vif,
4506                                  struct ieee80211_sta *sta,
4507                                  u32 changed)
4508 {
4509         struct ath10k *ar = hw->priv;
4510         struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
4511         u32 bw, smps;
4512
4513         spin_lock_bh(&ar->data_lock);
4514
4515         ath10k_dbg(ar, ATH10K_DBG_MAC,
4516                    "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
4517                    sta->addr, changed, sta->bandwidth, sta->rx_nss,
4518                    sta->smps_mode);
4519
4520         if (changed & IEEE80211_RC_BW_CHANGED) {
4521                 bw = WMI_PEER_CHWIDTH_20MHZ;
4522
4523                 switch (sta->bandwidth) {
4524                 case IEEE80211_STA_RX_BW_20:
4525                         bw = WMI_PEER_CHWIDTH_20MHZ;
4526                         break;
4527                 case IEEE80211_STA_RX_BW_40:
4528                         bw = WMI_PEER_CHWIDTH_40MHZ;
4529                         break;
4530                 case IEEE80211_STA_RX_BW_80:
4531                         bw = WMI_PEER_CHWIDTH_80MHZ;
4532                         break;
4533                 case IEEE80211_STA_RX_BW_160:
4534                         ath10k_warn(ar, "Invalid bandwith %d in rc update for %pM\n",
4535                                     sta->bandwidth, sta->addr);
4536                         bw = WMI_PEER_CHWIDTH_20MHZ;
4537                         break;
4538                 }
4539
4540                 arsta->bw = bw;
4541         }
4542
4543         if (changed & IEEE80211_RC_NSS_CHANGED)
4544                 arsta->nss = sta->rx_nss;
4545
4546         if (changed & IEEE80211_RC_SMPS_CHANGED) {
4547                 smps = WMI_PEER_SMPS_PS_NONE;
4548
4549                 switch (sta->smps_mode) {
4550                 case IEEE80211_SMPS_AUTOMATIC:
4551                 case IEEE80211_SMPS_OFF:
4552                         smps = WMI_PEER_SMPS_PS_NONE;
4553                         break;
4554                 case IEEE80211_SMPS_STATIC:
4555                         smps = WMI_PEER_SMPS_STATIC;
4556                         break;
4557                 case IEEE80211_SMPS_DYNAMIC:
4558                         smps = WMI_PEER_SMPS_DYNAMIC;
4559                         break;
4560                 case IEEE80211_SMPS_NUM_MODES:
4561                         ath10k_warn(ar, "Invalid smps %d in sta rc update for %pM\n",
4562                                     sta->smps_mode, sta->addr);
4563                         smps = WMI_PEER_SMPS_PS_NONE;
4564                         break;
4565                 }
4566
4567                 arsta->smps = smps;
4568         }
4569
4570         arsta->changed |= changed;
4571
4572         spin_unlock_bh(&ar->data_lock);
4573
4574         ieee80211_queue_work(hw, &arsta->update_wk);
4575 }
4576
4577 static u64 ath10k_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
4578 {
4579         /*
4580          * FIXME: Return 0 for time being. Need to figure out whether FW
4581          * has the API to fetch 64-bit local TSF
4582          */
4583
4584         return 0;
4585 }
4586
4587 static int ath10k_ampdu_action(struct ieee80211_hw *hw,
4588                                struct ieee80211_vif *vif,
4589                                enum ieee80211_ampdu_mlme_action action,
4590                                struct ieee80211_sta *sta, u16 tid, u16 *ssn,
4591                                u8 buf_size)
4592 {
4593         struct ath10k *ar = hw->priv;
4594         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4595
4596         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ampdu vdev_id %i sta %pM tid %hu action %d\n",
4597                    arvif->vdev_id, sta->addr, tid, action);
4598
4599         switch (action) {
4600         case IEEE80211_AMPDU_RX_START:
4601         case IEEE80211_AMPDU_RX_STOP:
4602                 /* HTT AddBa/DelBa events trigger mac80211 Rx BA session
4603                  * creation/removal. Do we need to verify this?
4604                  */
4605                 return 0;
4606         case IEEE80211_AMPDU_TX_START:
4607         case IEEE80211_AMPDU_TX_STOP_CONT:
4608         case IEEE80211_AMPDU_TX_STOP_FLUSH:
4609         case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
4610         case IEEE80211_AMPDU_TX_OPERATIONAL:
4611                 /* Firmware offloads Tx aggregation entirely so deny mac80211
4612                  * Tx aggregation requests.
4613                  */
4614                 return -EOPNOTSUPP;
4615         }
4616
4617         return -EINVAL;
4618 }
4619
4620 static const struct ieee80211_ops ath10k_ops = {
4621         .tx                             = ath10k_tx,
4622         .start                          = ath10k_start,
4623         .stop                           = ath10k_stop,
4624         .config                         = ath10k_config,
4625         .add_interface                  = ath10k_add_interface,
4626         .remove_interface               = ath10k_remove_interface,
4627         .configure_filter               = ath10k_configure_filter,
4628         .bss_info_changed               = ath10k_bss_info_changed,
4629         .hw_scan                        = ath10k_hw_scan,
4630         .cancel_hw_scan                 = ath10k_cancel_hw_scan,
4631         .set_key                        = ath10k_set_key,
4632         .sta_state                      = ath10k_sta_state,
4633         .conf_tx                        = ath10k_conf_tx,
4634         .remain_on_channel              = ath10k_remain_on_channel,
4635         .cancel_remain_on_channel       = ath10k_cancel_remain_on_channel,
4636         .set_rts_threshold              = ath10k_set_rts_threshold,
4637         .flush                          = ath10k_flush,
4638         .tx_last_beacon                 = ath10k_tx_last_beacon,
4639         .set_antenna                    = ath10k_set_antenna,
4640         .get_antenna                    = ath10k_get_antenna,
4641         .reconfig_complete              = ath10k_reconfig_complete,
4642         .get_survey                     = ath10k_get_survey,
4643         .set_bitrate_mask               = ath10k_set_bitrate_mask,
4644         .sta_rc_update                  = ath10k_sta_rc_update,
4645         .get_tsf                        = ath10k_get_tsf,
4646         .ampdu_action                   = ath10k_ampdu_action,
4647         .get_et_sset_count              = ath10k_debug_get_et_sset_count,
4648         .get_et_stats                   = ath10k_debug_get_et_stats,
4649         .get_et_strings                 = ath10k_debug_get_et_strings,
4650
4651         CFG80211_TESTMODE_CMD(ath10k_tm_cmd)
4652
4653 #ifdef CONFIG_PM
4654         .suspend                        = ath10k_suspend,
4655         .resume                         = ath10k_resume,
4656 #endif
4657 };
4658
4659 #define RATETAB_ENT(_rate, _rateid, _flags) { \
4660         .bitrate                = (_rate), \
4661         .flags                  = (_flags), \
4662         .hw_value               = (_rateid), \
4663 }
4664
4665 #define CHAN2G(_channel, _freq, _flags) { \
4666         .band                   = IEEE80211_BAND_2GHZ, \
4667         .hw_value               = (_channel), \
4668         .center_freq            = (_freq), \
4669         .flags                  = (_flags), \
4670         .max_antenna_gain       = 0, \
4671         .max_power              = 30, \
4672 }
4673
4674 #define CHAN5G(_channel, _freq, _flags) { \
4675         .band                   = IEEE80211_BAND_5GHZ, \
4676         .hw_value               = (_channel), \
4677         .center_freq            = (_freq), \
4678         .flags                  = (_flags), \
4679         .max_antenna_gain       = 0, \
4680         .max_power              = 30, \
4681 }
4682
4683 static const struct ieee80211_channel ath10k_2ghz_channels[] = {
4684         CHAN2G(1, 2412, 0),
4685         CHAN2G(2, 2417, 0),
4686         CHAN2G(3, 2422, 0),
4687         CHAN2G(4, 2427, 0),
4688         CHAN2G(5, 2432, 0),
4689         CHAN2G(6, 2437, 0),
4690         CHAN2G(7, 2442, 0),
4691         CHAN2G(8, 2447, 0),
4692         CHAN2G(9, 2452, 0),
4693         CHAN2G(10, 2457, 0),
4694         CHAN2G(11, 2462, 0),
4695         CHAN2G(12, 2467, 0),
4696         CHAN2G(13, 2472, 0),
4697         CHAN2G(14, 2484, 0),
4698 };
4699
4700 static const struct ieee80211_channel ath10k_5ghz_channels[] = {
4701         CHAN5G(36, 5180, 0),
4702         CHAN5G(40, 5200, 0),
4703         CHAN5G(44, 5220, 0),
4704         CHAN5G(48, 5240, 0),
4705         CHAN5G(52, 5260, 0),
4706         CHAN5G(56, 5280, 0),
4707         CHAN5G(60, 5300, 0),
4708         CHAN5G(64, 5320, 0),
4709         CHAN5G(100, 5500, 0),
4710         CHAN5G(104, 5520, 0),
4711         CHAN5G(108, 5540, 0),
4712         CHAN5G(112, 5560, 0),
4713         CHAN5G(116, 5580, 0),
4714         CHAN5G(120, 5600, 0),
4715         CHAN5G(124, 5620, 0),
4716         CHAN5G(128, 5640, 0),
4717         CHAN5G(132, 5660, 0),
4718         CHAN5G(136, 5680, 0),
4719         CHAN5G(140, 5700, 0),
4720         CHAN5G(149, 5745, 0),
4721         CHAN5G(153, 5765, 0),
4722         CHAN5G(157, 5785, 0),
4723         CHAN5G(161, 5805, 0),
4724         CHAN5G(165, 5825, 0),
4725 };
4726
4727 static struct ieee80211_rate ath10k_rates[] = {
4728         /* CCK */
4729         RATETAB_ENT(10,  0x82, 0),
4730         RATETAB_ENT(20,  0x84, 0),
4731         RATETAB_ENT(55,  0x8b, 0),
4732         RATETAB_ENT(110, 0x96, 0),
4733         /* OFDM */
4734         RATETAB_ENT(60,  0x0c, 0),
4735         RATETAB_ENT(90,  0x12, 0),
4736         RATETAB_ENT(120, 0x18, 0),
4737         RATETAB_ENT(180, 0x24, 0),
4738         RATETAB_ENT(240, 0x30, 0),
4739         RATETAB_ENT(360, 0x48, 0),
4740         RATETAB_ENT(480, 0x60, 0),
4741         RATETAB_ENT(540, 0x6c, 0),
4742 };
4743
4744 #define ath10k_a_rates (ath10k_rates + 4)
4745 #define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - 4)
4746 #define ath10k_g_rates (ath10k_rates + 0)
4747 #define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
4748
4749 struct ath10k *ath10k_mac_create(size_t priv_size)
4750 {
4751         struct ieee80211_hw *hw;
4752         struct ath10k *ar;
4753
4754         hw = ieee80211_alloc_hw(sizeof(struct ath10k) + priv_size, &ath10k_ops);
4755         if (!hw)
4756                 return NULL;
4757
4758         ar = hw->priv;
4759         ar->hw = hw;
4760
4761         return ar;
4762 }
4763
4764 void ath10k_mac_destroy(struct ath10k *ar)
4765 {
4766         ieee80211_free_hw(ar->hw);
4767 }
4768
4769 static const struct ieee80211_iface_limit ath10k_if_limits[] = {
4770         {
4771         .max    = 8,
4772         .types  = BIT(NL80211_IFTYPE_STATION)
4773                 | BIT(NL80211_IFTYPE_P2P_CLIENT)
4774         },
4775         {
4776         .max    = 3,
4777         .types  = BIT(NL80211_IFTYPE_P2P_GO)
4778         },
4779         {
4780         .max    = 7,
4781         .types  = BIT(NL80211_IFTYPE_AP)
4782         },
4783 };
4784
4785 static const struct ieee80211_iface_limit ath10k_10x_if_limits[] = {
4786         {
4787         .max    = 8,
4788         .types  = BIT(NL80211_IFTYPE_AP)
4789         },
4790 };
4791
4792 static const struct ieee80211_iface_combination ath10k_if_comb[] = {
4793         {
4794                 .limits = ath10k_if_limits,
4795                 .n_limits = ARRAY_SIZE(ath10k_if_limits),
4796                 .max_interfaces = 8,
4797                 .num_different_channels = 1,
4798                 .beacon_int_infra_match = true,
4799         },
4800 };
4801
4802 static const struct ieee80211_iface_combination ath10k_10x_if_comb[] = {
4803         {
4804                 .limits = ath10k_10x_if_limits,
4805                 .n_limits = ARRAY_SIZE(ath10k_10x_if_limits),
4806                 .max_interfaces = 8,
4807                 .num_different_channels = 1,
4808                 .beacon_int_infra_match = true,
4809 #ifdef CONFIG_ATH10K_DFS_CERTIFIED
4810                 .radar_detect_widths =  BIT(NL80211_CHAN_WIDTH_20_NOHT) |
4811                                         BIT(NL80211_CHAN_WIDTH_20) |
4812                                         BIT(NL80211_CHAN_WIDTH_40) |
4813                                         BIT(NL80211_CHAN_WIDTH_80),
4814 #endif
4815         },
4816 };
4817
4818 static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
4819 {
4820         struct ieee80211_sta_vht_cap vht_cap = {0};
4821         u16 mcs_map;
4822         int i;
4823
4824         vht_cap.vht_supported = 1;
4825         vht_cap.cap = ar->vht_cap_info;
4826
4827         mcs_map = 0;
4828         for (i = 0; i < 8; i++) {
4829                 if (i < ar->num_rf_chains)
4830                         mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i*2);
4831                 else
4832                         mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i*2);
4833         }
4834
4835         vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
4836         vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
4837
4838         return vht_cap;
4839 }
4840
4841 static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
4842 {
4843         int i;
4844         struct ieee80211_sta_ht_cap ht_cap = {0};
4845
4846         if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
4847                 return ht_cap;
4848
4849         ht_cap.ht_supported = 1;
4850         ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
4851         ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
4852         ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
4853         ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
4854         ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
4855
4856         if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
4857                 ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
4858
4859         if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
4860                 ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
4861
4862         if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
4863                 u32 smps;
4864
4865                 smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
4866                 smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
4867
4868                 ht_cap.cap |= smps;
4869         }
4870
4871         if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC)
4872                 ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
4873
4874         if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
4875                 u32 stbc;
4876
4877                 stbc   = ar->ht_cap_info;
4878                 stbc  &= WMI_HT_CAP_RX_STBC;
4879                 stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
4880                 stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
4881                 stbc  &= IEEE80211_HT_CAP_RX_STBC;
4882
4883                 ht_cap.cap |= stbc;
4884         }
4885
4886         if (ar->ht_cap_info & WMI_HT_CAP_LDPC)
4887                 ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
4888
4889         if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
4890                 ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
4891
4892         /* max AMSDU is implicitly taken from vht_cap_info */
4893         if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
4894                 ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
4895
4896         for (i = 0; i < ar->num_rf_chains; i++)
4897                 ht_cap.mcs.rx_mask[i] = 0xFF;
4898
4899         ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
4900
4901         return ht_cap;
4902 }
4903
4904 static void ath10k_get_arvif_iter(void *data, u8 *mac,
4905                                   struct ieee80211_vif *vif)
4906 {
4907         struct ath10k_vif_iter *arvif_iter = data;
4908         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4909
4910         if (arvif->vdev_id == arvif_iter->vdev_id)
4911                 arvif_iter->arvif = arvif;
4912 }
4913
4914 struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
4915 {
4916         struct ath10k_vif_iter arvif_iter;
4917         u32 flags;
4918
4919         memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
4920         arvif_iter.vdev_id = vdev_id;
4921
4922         flags = IEEE80211_IFACE_ITER_RESUME_ALL;
4923         ieee80211_iterate_active_interfaces_atomic(ar->hw,
4924                                                    flags,
4925                                                    ath10k_get_arvif_iter,
4926                                                    &arvif_iter);
4927         if (!arvif_iter.arvif) {
4928                 ath10k_warn(ar, "No VIF found for vdev %d\n", vdev_id);
4929                 return NULL;
4930         }
4931
4932         return arvif_iter.arvif;
4933 }
4934
4935 int ath10k_mac_register(struct ath10k *ar)
4936 {
4937         struct ieee80211_supported_band *band;
4938         struct ieee80211_sta_vht_cap vht_cap;
4939         struct ieee80211_sta_ht_cap ht_cap;
4940         void *channels;
4941         int ret;
4942
4943         SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
4944
4945         SET_IEEE80211_DEV(ar->hw, ar->dev);
4946
4947         ht_cap = ath10k_get_ht_cap(ar);
4948         vht_cap = ath10k_create_vht_cap(ar);
4949
4950         if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
4951                 channels = kmemdup(ath10k_2ghz_channels,
4952                                    sizeof(ath10k_2ghz_channels),
4953                                    GFP_KERNEL);
4954                 if (!channels) {
4955                         ret = -ENOMEM;
4956                         goto err_free;
4957                 }
4958
4959                 band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
4960                 band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
4961                 band->channels = channels;
4962                 band->n_bitrates = ath10k_g_rates_size;
4963                 band->bitrates = ath10k_g_rates;
4964                 band->ht_cap = ht_cap;
4965
4966                 /* vht is not supported in 2.4 GHz */
4967
4968                 ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
4969         }
4970
4971         if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
4972                 channels = kmemdup(ath10k_5ghz_channels,
4973                                    sizeof(ath10k_5ghz_channels),
4974                                    GFP_KERNEL);
4975                 if (!channels) {
4976                         ret = -ENOMEM;
4977                         goto err_free;
4978                 }
4979
4980                 band = &ar->mac.sbands[IEEE80211_BAND_5GHZ];
4981                 band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
4982                 band->channels = channels;
4983                 band->n_bitrates = ath10k_a_rates_size;
4984                 band->bitrates = ath10k_a_rates;
4985                 band->ht_cap = ht_cap;
4986                 band->vht_cap = vht_cap;
4987                 ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
4988         }
4989
4990         ar->hw->wiphy->interface_modes =
4991                 BIT(NL80211_IFTYPE_STATION) |
4992                 BIT(NL80211_IFTYPE_AP);
4993
4994         ar->hw->wiphy->available_antennas_rx = ar->supp_rx_chainmask;
4995         ar->hw->wiphy->available_antennas_tx = ar->supp_tx_chainmask;
4996
4997         if (!test_bit(ATH10K_FW_FEATURE_NO_P2P, ar->fw_features))
4998                 ar->hw->wiphy->interface_modes |=
4999                         BIT(NL80211_IFTYPE_P2P_CLIENT) |
5000                         BIT(NL80211_IFTYPE_P2P_GO);
5001
5002         ar->hw->flags = IEEE80211_HW_SIGNAL_DBM |
5003                         IEEE80211_HW_SUPPORTS_PS |
5004                         IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
5005                         IEEE80211_HW_SUPPORTS_UAPSD |
5006                         IEEE80211_HW_MFP_CAPABLE |
5007                         IEEE80211_HW_REPORTS_TX_ACK_STATUS |
5008                         IEEE80211_HW_HAS_RATE_CONTROL |
5009                         IEEE80211_HW_AP_LINK_PS |
5010                         IEEE80211_HW_SPECTRUM_MGMT;
5011
5012         ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
5013
5014         if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
5015                 ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
5016
5017         if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
5018                 ar->hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
5019                 ar->hw->flags |= IEEE80211_HW_TX_AMPDU_SETUP_IN_HW;
5020         }
5021
5022         ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
5023         ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
5024
5025         ar->hw->vif_data_size = sizeof(struct ath10k_vif);
5026         ar->hw->sta_data_size = sizeof(struct ath10k_sta);
5027
5028         ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
5029
5030         ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
5031         ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
5032         ar->hw->wiphy->max_remain_on_channel_duration = 5000;
5033
5034         ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
5035         ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE;
5036
5037         /*
5038          * on LL hardware queues are managed entirely by the FW
5039          * so we only advertise to mac we can do the queues thing
5040          */
5041         ar->hw->queues = 4;
5042
5043         if (test_bit(ATH10K_FW_FEATURE_WMI_10X, ar->fw_features)) {
5044                 ar->hw->wiphy->iface_combinations = ath10k_10x_if_comb;
5045                 ar->hw->wiphy->n_iface_combinations =
5046                         ARRAY_SIZE(ath10k_10x_if_comb);
5047         } else {
5048                 ar->hw->wiphy->iface_combinations = ath10k_if_comb;
5049                 ar->hw->wiphy->n_iface_combinations =
5050                         ARRAY_SIZE(ath10k_if_comb);
5051
5052                 ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
5053         }
5054
5055         ar->hw->netdev_features = NETIF_F_HW_CSUM;
5056
5057         if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED)) {
5058                 /* Init ath dfs pattern detector */
5059                 ar->ath_common.debug_mask = ATH_DBG_DFS;
5060                 ar->dfs_detector = dfs_pattern_detector_init(&ar->ath_common,
5061                                                              NL80211_DFS_UNSET);
5062
5063                 if (!ar->dfs_detector)
5064                         ath10k_warn(ar, "failed to initialise DFS pattern detector\n");
5065         }
5066
5067         ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
5068                             ath10k_reg_notifier);
5069         if (ret) {
5070                 ath10k_err(ar, "failed to initialise regulatory: %i\n", ret);
5071                 goto err_free;
5072         }
5073
5074         ret = ieee80211_register_hw(ar->hw);
5075         if (ret) {
5076                 ath10k_err(ar, "failed to register ieee80211: %d\n", ret);
5077                 goto err_free;
5078         }
5079
5080         if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
5081                 ret = regulatory_hint(ar->hw->wiphy,
5082                                       ar->ath_common.regulatory.alpha2);
5083                 if (ret)
5084                         goto err_unregister;
5085         }
5086
5087         return 0;
5088
5089 err_unregister:
5090         ieee80211_unregister_hw(ar->hw);
5091 err_free:
5092         kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
5093         kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
5094
5095         return ret;
5096 }
5097
5098 void ath10k_mac_unregister(struct ath10k *ar)
5099 {
5100         ieee80211_unregister_hw(ar->hw);
5101
5102         if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
5103                 ar->dfs_detector->exit(ar->dfs_detector);
5104
5105         kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
5106         kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
5107
5108         SET_IEEE80211_DEV(ar->hw, NULL);
5109 }