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