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