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