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