Merge branch 'for-linville' of git://github.com/kvalo/ath
[sfrench/cifs-2.6.git] / drivers / net / wireless / ath / ath10k / mac.c
1 /*
2  * Copyright (c) 2005-2011 Atheros Communications Inc.
3  * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 #include "mac.h"
19
20 #include <net/mac80211.h>
21 #include <linux/etherdevice.h>
22
23 #include "hif.h"
24 #include "core.h"
25 #include "debug.h"
26 #include "wmi.h"
27 #include "htt.h"
28 #include "txrx.h"
29
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 ath10k *ar = arvif->ar;
40         struct wmi_vdev_install_key_arg arg = {
41                 .vdev_id = arvif->vdev_id,
42                 .key_idx = key->keyidx,
43                 .key_len = key->keylen,
44                 .key_data = key->key,
45                 .macaddr = macaddr,
46         };
47
48         lockdep_assert_held(&arvif->ar->conf_mutex);
49
50         if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
51                 arg.key_flags = WMI_KEY_PAIRWISE;
52         else
53                 arg.key_flags = WMI_KEY_GROUP;
54
55         switch (key->cipher) {
56         case WLAN_CIPHER_SUITE_CCMP:
57                 arg.key_cipher = WMI_CIPHER_AES_CCM;
58                 if (arvif->vdev_type == WMI_VDEV_TYPE_AP)
59                         key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
60                 else
61                         key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
62                 break;
63         case WLAN_CIPHER_SUITE_TKIP:
64                 arg.key_cipher = WMI_CIPHER_TKIP;
65                 arg.key_txmic_len = 8;
66                 arg.key_rxmic_len = 8;
67                 break;
68         case WLAN_CIPHER_SUITE_WEP40:
69         case WLAN_CIPHER_SUITE_WEP104:
70                 arg.key_cipher = WMI_CIPHER_WEP;
71                 /* AP/IBSS mode requires self-key to be groupwise
72                  * Otherwise pairwise key must be set */
73                 if (memcmp(macaddr, arvif->vif->addr, ETH_ALEN))
74                         arg.key_flags = WMI_KEY_PAIRWISE;
75                 break;
76         default:
77                 ath10k_warn(ar, "cipher %d is not supported\n", key->cipher);
78                 return -EOPNOTSUPP;
79         }
80
81         if (cmd == DISABLE_KEY) {
82                 arg.key_cipher = WMI_CIPHER_NONE;
83                 arg.key_data = NULL;
84         }
85
86         return ath10k_wmi_vdev_install_key(arvif->ar, &arg);
87 }
88
89 static int ath10k_install_key(struct ath10k_vif *arvif,
90                               struct ieee80211_key_conf *key,
91                               enum set_key_cmd cmd,
92                               const u8 *macaddr)
93 {
94         struct ath10k *ar = arvif->ar;
95         int ret;
96
97         lockdep_assert_held(&ar->conf_mutex);
98
99         reinit_completion(&ar->install_key_done);
100
101         ret = ath10k_send_key(arvif, key, cmd, macaddr);
102         if (ret)
103                 return ret;
104
105         ret = wait_for_completion_timeout(&ar->install_key_done, 3*HZ);
106         if (ret == 0)
107                 return -ETIMEDOUT;
108
109         return 0;
110 }
111
112 static int ath10k_install_peer_wep_keys(struct ath10k_vif *arvif,
113                                         const u8 *addr)
114 {
115         struct ath10k *ar = arvif->ar;
116         struct ath10k_peer *peer;
117         int ret;
118         int i;
119
120         lockdep_assert_held(&ar->conf_mutex);
121
122         spin_lock_bh(&ar->data_lock);
123         peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
124         spin_unlock_bh(&ar->data_lock);
125
126         if (!peer)
127                 return -ENOENT;
128
129         for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
130                 if (arvif->wep_keys[i] == NULL)
131                         continue;
132
133                 ret = ath10k_install_key(arvif, arvif->wep_keys[i], SET_KEY,
134                                          addr);
135                 if (ret)
136                         return ret;
137
138                 peer->keys[i] = arvif->wep_keys[i];
139         }
140
141         return 0;
142 }
143
144 static int ath10k_clear_peer_keys(struct ath10k_vif *arvif,
145                                   const u8 *addr)
146 {
147         struct ath10k *ar = arvif->ar;
148         struct ath10k_peer *peer;
149         int first_errno = 0;
150         int ret;
151         int i;
152
153         lockdep_assert_held(&ar->conf_mutex);
154
155         spin_lock_bh(&ar->data_lock);
156         peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
157         spin_unlock_bh(&ar->data_lock);
158
159         if (!peer)
160                 return -ENOENT;
161
162         for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
163                 if (peer->keys[i] == NULL)
164                         continue;
165
166                 ret = ath10k_install_key(arvif, peer->keys[i],
167                                          DISABLE_KEY, addr);
168                 if (ret && first_errno == 0)
169                         first_errno = ret;
170
171                 if (ret)
172                         ath10k_warn(ar, "failed to remove peer wep key %d: %d\n",
173                                     i, ret);
174
175                 peer->keys[i] = NULL;
176         }
177
178         return first_errno;
179 }
180
181 static int ath10k_clear_vdev_key(struct ath10k_vif *arvif,
182                                  struct ieee80211_key_conf *key)
183 {
184         struct ath10k *ar = arvif->ar;
185         struct ath10k_peer *peer;
186         u8 addr[ETH_ALEN];
187         int first_errno = 0;
188         int ret;
189         int i;
190
191         lockdep_assert_held(&ar->conf_mutex);
192
193         for (;;) {
194                 /* since ath10k_install_key we can't hold data_lock all the
195                  * time, so we try to remove the keys incrementally */
196                 spin_lock_bh(&ar->data_lock);
197                 i = 0;
198                 list_for_each_entry(peer, &ar->peers, list) {
199                         for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
200                                 if (peer->keys[i] == key) {
201                                         memcpy(addr, peer->addr, ETH_ALEN);
202                                         peer->keys[i] = NULL;
203                                         break;
204                                 }
205                         }
206
207                         if (i < ARRAY_SIZE(peer->keys))
208                                 break;
209                 }
210                 spin_unlock_bh(&ar->data_lock);
211
212                 if (i == ARRAY_SIZE(peer->keys))
213                         break;
214
215                 ret = ath10k_install_key(arvif, key, DISABLE_KEY, addr);
216                 if (ret && first_errno == 0)
217                         first_errno = ret;
218
219                 if (ret)
220                         ath10k_warn(ar, "failed to remove key for %pM: %d\n",
221                                     addr, ret);
222         }
223
224         return first_errno;
225 }
226
227
228 /*********************/
229 /* General utilities */
230 /*********************/
231
232 static inline enum wmi_phy_mode
233 chan_to_phymode(const struct cfg80211_chan_def *chandef)
234 {
235         enum wmi_phy_mode phymode = MODE_UNKNOWN;
236
237         switch (chandef->chan->band) {
238         case IEEE80211_BAND_2GHZ:
239                 switch (chandef->width) {
240                 case NL80211_CHAN_WIDTH_20_NOHT:
241                         phymode = MODE_11G;
242                         break;
243                 case NL80211_CHAN_WIDTH_20:
244                         phymode = MODE_11NG_HT20;
245                         break;
246                 case NL80211_CHAN_WIDTH_40:
247                         phymode = MODE_11NG_HT40;
248                         break;
249                 case NL80211_CHAN_WIDTH_5:
250                 case NL80211_CHAN_WIDTH_10:
251                 case NL80211_CHAN_WIDTH_80:
252                 case NL80211_CHAN_WIDTH_80P80:
253                 case NL80211_CHAN_WIDTH_160:
254                         phymode = MODE_UNKNOWN;
255                         break;
256                 }
257                 break;
258         case IEEE80211_BAND_5GHZ:
259                 switch (chandef->width) {
260                 case NL80211_CHAN_WIDTH_20_NOHT:
261                         phymode = MODE_11A;
262                         break;
263                 case NL80211_CHAN_WIDTH_20:
264                         phymode = MODE_11NA_HT20;
265                         break;
266                 case NL80211_CHAN_WIDTH_40:
267                         phymode = MODE_11NA_HT40;
268                         break;
269                 case NL80211_CHAN_WIDTH_80:
270                         phymode = MODE_11AC_VHT80;
271                         break;
272                 case NL80211_CHAN_WIDTH_5:
273                 case NL80211_CHAN_WIDTH_10:
274                 case NL80211_CHAN_WIDTH_80P80:
275                 case NL80211_CHAN_WIDTH_160:
276                         phymode = MODE_UNKNOWN;
277                         break;
278                 }
279                 break;
280         default:
281                 break;
282         }
283
284         WARN_ON(phymode == MODE_UNKNOWN);
285         return phymode;
286 }
287
288 static u8 ath10k_parse_mpdudensity(u8 mpdudensity)
289 {
290 /*
291  * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
292  *   0 for no restriction
293  *   1 for 1/4 us
294  *   2 for 1/2 us
295  *   3 for 1 us
296  *   4 for 2 us
297  *   5 for 4 us
298  *   6 for 8 us
299  *   7 for 16 us
300  */
301         switch (mpdudensity) {
302         case 0:
303                 return 0;
304         case 1:
305         case 2:
306         case 3:
307         /* Our lower layer calculations limit our precision to
308            1 microsecond */
309                 return 1;
310         case 4:
311                 return 2;
312         case 5:
313                 return 4;
314         case 6:
315                 return 8;
316         case 7:
317                 return 16;
318         default:
319                 return 0;
320         }
321 }
322
323 static int ath10k_peer_create(struct ath10k *ar, u32 vdev_id, const u8 *addr)
324 {
325         int ret;
326
327         lockdep_assert_held(&ar->conf_mutex);
328
329         ret = ath10k_wmi_peer_create(ar, vdev_id, addr);
330         if (ret) {
331                 ath10k_warn(ar, "failed to create wmi peer %pM on vdev %i: %i\n",
332                             addr, vdev_id, ret);
333                 return ret;
334         }
335
336         ret = ath10k_wait_for_peer_created(ar, vdev_id, addr);
337         if (ret) {
338                 ath10k_warn(ar, "failed to wait for created wmi peer %pM on vdev %i: %i\n",
339                             addr, vdev_id, ret);
340                 return ret;
341         }
342         spin_lock_bh(&ar->data_lock);
343         ar->num_peers++;
344         spin_unlock_bh(&ar->data_lock);
345
346         return 0;
347 }
348
349 static int ath10k_mac_set_kickout(struct ath10k_vif *arvif)
350 {
351         struct ath10k *ar = arvif->ar;
352         u32 param;
353         int ret;
354
355         param = ar->wmi.pdev_param->sta_kickout_th;
356         ret = ath10k_wmi_pdev_set_param(ar, param,
357                                         ATH10K_KICKOUT_THRESHOLD);
358         if (ret) {
359                 ath10k_warn(ar, "failed to set kickout threshold on vdev %i: %d\n",
360                             arvif->vdev_id, ret);
361                 return ret;
362         }
363
364         param = ar->wmi.vdev_param->ap_keepalive_min_idle_inactive_time_secs;
365         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
366                                         ATH10K_KEEPALIVE_MIN_IDLE);
367         if (ret) {
368                 ath10k_warn(ar, "failed to set keepalive minimum idle time on vdev %i: %d\n",
369                             arvif->vdev_id, ret);
370                 return ret;
371         }
372
373         param = ar->wmi.vdev_param->ap_keepalive_max_idle_inactive_time_secs;
374         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
375                                         ATH10K_KEEPALIVE_MAX_IDLE);
376         if (ret) {
377                 ath10k_warn(ar, "failed to set keepalive maximum idle time on vdev %i: %d\n",
378                             arvif->vdev_id, ret);
379                 return ret;
380         }
381
382         param = ar->wmi.vdev_param->ap_keepalive_max_unresponsive_time_secs;
383         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
384                                         ATH10K_KEEPALIVE_MAX_UNRESPONSIVE);
385         if (ret) {
386                 ath10k_warn(ar, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
387                             arvif->vdev_id, ret);
388                 return ret;
389         }
390
391         return 0;
392 }
393
394 static int  ath10k_mac_set_rts(struct ath10k_vif *arvif, u32 value)
395 {
396         struct ath10k *ar = arvif->ar;
397         u32 vdev_param;
398
399         if (value != 0xFFFFFFFF)
400                 value = min_t(u32, arvif->ar->hw->wiphy->rts_threshold,
401                               ATH10K_RTS_MAX);
402
403         vdev_param = ar->wmi.vdev_param->rts_threshold;
404         return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
405 }
406
407 static int ath10k_mac_set_frag(struct ath10k_vif *arvif, u32 value)
408 {
409         struct ath10k *ar = arvif->ar;
410         u32 vdev_param;
411
412         if (value != 0xFFFFFFFF)
413                 value = clamp_t(u32, arvif->ar->hw->wiphy->frag_threshold,
414                                 ATH10K_FRAGMT_THRESHOLD_MIN,
415                                 ATH10K_FRAGMT_THRESHOLD_MAX);
416
417         vdev_param = ar->wmi.vdev_param->fragmentation_threshold;
418         return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
419 }
420
421 static int ath10k_peer_delete(struct ath10k *ar, u32 vdev_id, const u8 *addr)
422 {
423         int ret;
424
425         lockdep_assert_held(&ar->conf_mutex);
426
427         ret = ath10k_wmi_peer_delete(ar, vdev_id, addr);
428         if (ret)
429                 return ret;
430
431         ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
432         if (ret)
433                 return ret;
434
435         spin_lock_bh(&ar->data_lock);
436         ar->num_peers--;
437         spin_unlock_bh(&ar->data_lock);
438
439         return 0;
440 }
441
442 static void ath10k_peer_cleanup(struct ath10k *ar, u32 vdev_id)
443 {
444         struct ath10k_peer *peer, *tmp;
445
446         lockdep_assert_held(&ar->conf_mutex);
447
448         spin_lock_bh(&ar->data_lock);
449         list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
450                 if (peer->vdev_id != vdev_id)
451                         continue;
452
453                 ath10k_warn(ar, "removing stale peer %pM from vdev_id %d\n",
454                             peer->addr, vdev_id);
455
456                 list_del(&peer->list);
457                 kfree(peer);
458                 ar->num_peers--;
459         }
460         spin_unlock_bh(&ar->data_lock);
461 }
462
463 static void ath10k_peer_cleanup_all(struct ath10k *ar)
464 {
465         struct ath10k_peer *peer, *tmp;
466
467         lockdep_assert_held(&ar->conf_mutex);
468
469         spin_lock_bh(&ar->data_lock);
470         list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
471                 list_del(&peer->list);
472                 kfree(peer);
473         }
474         ar->num_peers = 0;
475         spin_unlock_bh(&ar->data_lock);
476 }
477
478 /************************/
479 /* Interface management */
480 /************************/
481
482 static inline int ath10k_vdev_setup_sync(struct ath10k *ar)
483 {
484         int ret;
485
486         lockdep_assert_held(&ar->conf_mutex);
487
488         ret = wait_for_completion_timeout(&ar->vdev_setup_done,
489                                           ATH10K_VDEV_SETUP_TIMEOUT_HZ);
490         if (ret == 0)
491                 return -ETIMEDOUT;
492
493         return 0;
494 }
495
496 static bool ath10k_monitor_is_enabled(struct ath10k *ar)
497 {
498         lockdep_assert_held(&ar->conf_mutex);
499
500         ath10k_dbg(ar, ATH10K_DBG_MAC,
501                    "mac monitor refs: promisc %d monitor %d cac %d\n",
502                    ar->promisc, ar->monitor,
503                    test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags));
504
505         return ar->promisc || ar->monitor ||
506                test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
507 }
508
509 static int ath10k_monitor_vdev_start(struct ath10k *ar, int vdev_id)
510 {
511         struct cfg80211_chan_def *chandef = &ar->chandef;
512         struct ieee80211_channel *channel = chandef->chan;
513         struct wmi_vdev_start_request_arg arg = {};
514         int ret = 0;
515
516         lockdep_assert_held(&ar->conf_mutex);
517
518         arg.vdev_id = vdev_id;
519         arg.channel.freq = channel->center_freq;
520         arg.channel.band_center_freq1 = chandef->center_freq1;
521
522         /* TODO setup this dynamically, what in case we
523            don't have any vifs? */
524         arg.channel.mode = chan_to_phymode(chandef);
525         arg.channel.chan_radar =
526                         !!(channel->flags & IEEE80211_CHAN_RADAR);
527
528         arg.channel.min_power = 0;
529         arg.channel.max_power = channel->max_power * 2;
530         arg.channel.max_reg_power = channel->max_reg_power * 2;
531         arg.channel.max_antenna_gain = channel->max_antenna_gain * 2;
532
533         ret = ath10k_wmi_vdev_start(ar, &arg);
534         if (ret) {
535                 ath10k_warn(ar, "failed to request monitor vdev %i start: %d\n",
536                             vdev_id, ret);
537                 return ret;
538         }
539
540         ret = ath10k_vdev_setup_sync(ar);
541         if (ret) {
542                 ath10k_warn(ar, "failed to synchronize setup for monitor vdev %i: %d\n",
543                             vdev_id, ret);
544                 return ret;
545         }
546
547         ret = ath10k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
548         if (ret) {
549                 ath10k_warn(ar, "failed to put up monitor vdev %i: %d\n",
550                             vdev_id, ret);
551                 goto vdev_stop;
552         }
553
554         ar->monitor_vdev_id = vdev_id;
555
556         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i started\n",
557                    ar->monitor_vdev_id);
558         return 0;
559
560 vdev_stop:
561         ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
562         if (ret)
563                 ath10k_warn(ar, "failed to stop monitor vdev %i after start failure: %d\n",
564                             ar->monitor_vdev_id, ret);
565
566         return ret;
567 }
568
569 static int ath10k_monitor_vdev_stop(struct ath10k *ar)
570 {
571         int ret = 0;
572
573         lockdep_assert_held(&ar->conf_mutex);
574
575         ret = ath10k_wmi_vdev_down(ar, ar->monitor_vdev_id);
576         if (ret)
577                 ath10k_warn(ar, "failed to put down monitor vdev %i: %d\n",
578                             ar->monitor_vdev_id, ret);
579
580         ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
581         if (ret)
582                 ath10k_warn(ar, "failed to to request monitor vdev %i stop: %d\n",
583                             ar->monitor_vdev_id, ret);
584
585         ret = ath10k_vdev_setup_sync(ar);
586         if (ret)
587                 ath10k_warn(ar, "failed to synchronise monitor vdev %i: %d\n",
588                             ar->monitor_vdev_id, ret);
589
590         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i stopped\n",
591                    ar->monitor_vdev_id);
592         return ret;
593 }
594
595 static int ath10k_monitor_vdev_create(struct ath10k *ar)
596 {
597         int bit, ret = 0;
598
599         lockdep_assert_held(&ar->conf_mutex);
600
601         if (ar->free_vdev_map == 0) {
602                 ath10k_warn(ar, "failed to find free vdev id for monitor vdev\n");
603                 return -ENOMEM;
604         }
605
606         bit = ffs(ar->free_vdev_map);
607
608         ar->monitor_vdev_id = bit - 1;
609
610         ret = ath10k_wmi_vdev_create(ar, ar->monitor_vdev_id,
611                                      WMI_VDEV_TYPE_MONITOR,
612                                      0, ar->mac_addr);
613         if (ret) {
614                 ath10k_warn(ar, "failed to request monitor vdev %i creation: %d\n",
615                             ar->monitor_vdev_id, ret);
616                 return ret;
617         }
618
619         ar->free_vdev_map &= ~(1 << ar->monitor_vdev_id);
620         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d created\n",
621                    ar->monitor_vdev_id);
622
623         return 0;
624 }
625
626 static int ath10k_monitor_vdev_delete(struct ath10k *ar)
627 {
628         int ret = 0;
629
630         lockdep_assert_held(&ar->conf_mutex);
631
632         ret = ath10k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
633         if (ret) {
634                 ath10k_warn(ar, "failed to request wmi monitor vdev %i removal: %d\n",
635                             ar->monitor_vdev_id, ret);
636                 return ret;
637         }
638
639         ar->free_vdev_map |= 1 << ar->monitor_vdev_id;
640
641         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d deleted\n",
642                    ar->monitor_vdev_id);
643         return ret;
644 }
645
646 static int ath10k_monitor_start(struct ath10k *ar)
647 {
648         int ret;
649
650         lockdep_assert_held(&ar->conf_mutex);
651
652         if (!ath10k_monitor_is_enabled(ar)) {
653                 ath10k_warn(ar, "trying to start monitor with no references\n");
654                 return 0;
655         }
656
657         if (ar->monitor_started) {
658                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor already started\n");
659                 return 0;
660         }
661
662         ret = ath10k_monitor_vdev_create(ar);
663         if (ret) {
664                 ath10k_warn(ar, "failed to create monitor vdev: %d\n", ret);
665                 return ret;
666         }
667
668         ret = ath10k_monitor_vdev_start(ar, ar->monitor_vdev_id);
669         if (ret) {
670                 ath10k_warn(ar, "failed to start monitor vdev: %d\n", ret);
671                 ath10k_monitor_vdev_delete(ar);
672                 return ret;
673         }
674
675         ar->monitor_started = true;
676         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor started\n");
677
678         return 0;
679 }
680
681 static void ath10k_monitor_stop(struct ath10k *ar)
682 {
683         int ret;
684
685         lockdep_assert_held(&ar->conf_mutex);
686
687         if (ath10k_monitor_is_enabled(ar)) {
688                 ath10k_dbg(ar, ATH10K_DBG_MAC,
689                            "mac monitor will be stopped later\n");
690                 return;
691         }
692
693         if (!ar->monitor_started) {
694                 ath10k_dbg(ar, ATH10K_DBG_MAC,
695                            "mac monitor probably failed to start earlier\n");
696                 return;
697         }
698
699         ret = ath10k_monitor_vdev_stop(ar);
700         if (ret)
701                 ath10k_warn(ar, "failed to stop monitor vdev: %d\n", ret);
702
703         ret = ath10k_monitor_vdev_delete(ar);
704         if (ret)
705                 ath10k_warn(ar, "failed to delete monitor vdev: %d\n", ret);
706
707         ar->monitor_started = false;
708         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopped\n");
709 }
710
711 static int ath10k_recalc_rtscts_prot(struct ath10k_vif *arvif)
712 {
713         struct ath10k *ar = arvif->ar;
714         u32 vdev_param, rts_cts = 0;
715
716         lockdep_assert_held(&ar->conf_mutex);
717
718         vdev_param = ar->wmi.vdev_param->enable_rtscts;
719
720         if (arvif->use_cts_prot || arvif->num_legacy_stations > 0)
721                 rts_cts |= SM(WMI_RTSCTS_ENABLED, WMI_RTSCTS_SET);
722
723         if (arvif->num_legacy_stations > 0)
724                 rts_cts |= SM(WMI_RTSCTS_ACROSS_SW_RETRIES,
725                               WMI_RTSCTS_PROFILE);
726
727         return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
728                                          rts_cts);
729 }
730
731 static int ath10k_start_cac(struct ath10k *ar)
732 {
733         int ret;
734
735         lockdep_assert_held(&ar->conf_mutex);
736
737         set_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
738
739         ret = ath10k_monitor_start(ar);
740         if (ret) {
741                 ath10k_warn(ar, "failed to start monitor (cac): %d\n", ret);
742                 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
743                 return ret;
744         }
745
746         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac start monitor vdev %d\n",
747                    ar->monitor_vdev_id);
748
749         return 0;
750 }
751
752 static int ath10k_stop_cac(struct ath10k *ar)
753 {
754         lockdep_assert_held(&ar->conf_mutex);
755
756         /* CAC is not running - do nothing */
757         if (!test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags))
758                 return 0;
759
760         clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
761         ath10k_monitor_stop(ar);
762
763         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac finished\n");
764
765         return 0;
766 }
767
768 static void ath10k_recalc_radar_detection(struct ath10k *ar)
769 {
770         int ret;
771
772         lockdep_assert_held(&ar->conf_mutex);
773
774         ath10k_stop_cac(ar);
775
776         if (!ar->radar_enabled)
777                 return;
778
779         if (ar->num_started_vdevs > 0)
780                 return;
781
782         ret = ath10k_start_cac(ar);
783         if (ret) {
784                 /*
785                  * Not possible to start CAC on current channel so starting
786                  * radiation is not allowed, make this channel DFS_UNAVAILABLE
787                  * by indicating that radar was detected.
788                  */
789                 ath10k_warn(ar, "failed to start CAC: %d\n", ret);
790                 ieee80211_radar_detected(ar->hw);
791         }
792 }
793
794 static int ath10k_vdev_start_restart(struct ath10k_vif *arvif, bool restart)
795 {
796         struct ath10k *ar = arvif->ar;
797         struct cfg80211_chan_def *chandef = &ar->chandef;
798         struct wmi_vdev_start_request_arg arg = {};
799         int ret = 0;
800
801         lockdep_assert_held(&ar->conf_mutex);
802
803         reinit_completion(&ar->vdev_setup_done);
804
805         arg.vdev_id = arvif->vdev_id;
806         arg.dtim_period = arvif->dtim_period;
807         arg.bcn_intval = arvif->beacon_interval;
808
809         arg.channel.freq = chandef->chan->center_freq;
810         arg.channel.band_center_freq1 = chandef->center_freq1;
811         arg.channel.mode = chan_to_phymode(chandef);
812
813         arg.channel.min_power = 0;
814         arg.channel.max_power = chandef->chan->max_power * 2;
815         arg.channel.max_reg_power = chandef->chan->max_reg_power * 2;
816         arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
817
818         if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
819                 arg.ssid = arvif->u.ap.ssid;
820                 arg.ssid_len = arvif->u.ap.ssid_len;
821                 arg.hidden_ssid = arvif->u.ap.hidden_ssid;
822
823                 /* For now allow DFS for AP mode */
824                 arg.channel.chan_radar =
825                         !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
826         } else if (arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
827                 arg.ssid = arvif->vif->bss_conf.ssid;
828                 arg.ssid_len = arvif->vif->bss_conf.ssid_len;
829         }
830
831         ath10k_dbg(ar, ATH10K_DBG_MAC,
832                    "mac vdev %d start center_freq %d phymode %s\n",
833                    arg.vdev_id, arg.channel.freq,
834                    ath10k_wmi_phymode_str(arg.channel.mode));
835
836         if (restart)
837                 ret = ath10k_wmi_vdev_restart(ar, &arg);
838         else
839                 ret = ath10k_wmi_vdev_start(ar, &arg);
840
841         if (ret) {
842                 ath10k_warn(ar, "failed to start WMI vdev %i: %d\n",
843                             arg.vdev_id, ret);
844                 return ret;
845         }
846
847         ret = ath10k_vdev_setup_sync(ar);
848         if (ret) {
849                 ath10k_warn(ar, "failed to synchronise setup for vdev %i: %d\n",
850                             arg.vdev_id, ret);
851                 return ret;
852         }
853
854         ar->num_started_vdevs++;
855         ath10k_recalc_radar_detection(ar);
856
857         return ret;
858 }
859
860 static int ath10k_vdev_start(struct ath10k_vif *arvif)
861 {
862         return ath10k_vdev_start_restart(arvif, false);
863 }
864
865 static int ath10k_vdev_restart(struct ath10k_vif *arvif)
866 {
867         return ath10k_vdev_start_restart(arvif, true);
868 }
869
870 static int ath10k_vdev_stop(struct ath10k_vif *arvif)
871 {
872         struct ath10k *ar = arvif->ar;
873         int ret;
874
875         lockdep_assert_held(&ar->conf_mutex);
876
877         reinit_completion(&ar->vdev_setup_done);
878
879         ret = ath10k_wmi_vdev_stop(ar, arvif->vdev_id);
880         if (ret) {
881                 ath10k_warn(ar, "failed to stop WMI vdev %i: %d\n",
882                             arvif->vdev_id, ret);
883                 return ret;
884         }
885
886         ret = ath10k_vdev_setup_sync(ar);
887         if (ret) {
888                 ath10k_warn(ar, "failed to syncronise setup for vdev %i: %d\n",
889                             arvif->vdev_id, ret);
890                 return ret;
891         }
892
893         WARN_ON(ar->num_started_vdevs == 0);
894
895         if (ar->num_started_vdevs != 0) {
896                 ar->num_started_vdevs--;
897                 ath10k_recalc_radar_detection(ar);
898         }
899
900         return ret;
901 }
902
903 static void ath10k_control_beaconing(struct ath10k_vif *arvif,
904                                 struct ieee80211_bss_conf *info)
905 {
906         struct ath10k *ar = arvif->ar;
907         int ret = 0;
908
909         lockdep_assert_held(&arvif->ar->conf_mutex);
910
911         if (!info->enable_beacon) {
912                 ath10k_vdev_stop(arvif);
913
914                 arvif->is_started = false;
915                 arvif->is_up = false;
916
917                 spin_lock_bh(&arvif->ar->data_lock);
918                 if (arvif->beacon) {
919                         dma_unmap_single(arvif->ar->dev,
920                                          ATH10K_SKB_CB(arvif->beacon)->paddr,
921                                          arvif->beacon->len, DMA_TO_DEVICE);
922                         dev_kfree_skb_any(arvif->beacon);
923
924                         arvif->beacon = NULL;
925                         arvif->beacon_sent = false;
926                 }
927                 spin_unlock_bh(&arvif->ar->data_lock);
928
929                 return;
930         }
931
932         arvif->tx_seq_no = 0x1000;
933
934         ret = ath10k_vdev_start(arvif);
935         if (ret)
936                 return;
937
938         arvif->aid = 0;
939         memcpy(arvif->bssid, info->bssid, ETH_ALEN);
940
941         ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
942                                  arvif->bssid);
943         if (ret) {
944                 ath10k_warn(ar, "failed to bring up vdev %d: %i\n",
945                             arvif->vdev_id, ret);
946                 ath10k_vdev_stop(arvif);
947                 return;
948         }
949
950         arvif->is_started = true;
951         arvif->is_up = true;
952
953         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
954 }
955
956 static void ath10k_control_ibss(struct ath10k_vif *arvif,
957                                 struct ieee80211_bss_conf *info,
958                                 const u8 self_peer[ETH_ALEN])
959 {
960         struct ath10k *ar = arvif->ar;
961         u32 vdev_param;
962         int ret = 0;
963
964         lockdep_assert_held(&arvif->ar->conf_mutex);
965
966         if (!info->ibss_joined) {
967                 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, self_peer);
968                 if (ret)
969                         ath10k_warn(ar, "failed to delete IBSS self peer %pM for vdev %d: %d\n",
970                                     self_peer, arvif->vdev_id, ret);
971
972                 if (is_zero_ether_addr(arvif->bssid))
973                         return;
974
975                 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id,
976                                          arvif->bssid);
977                 if (ret) {
978                         ath10k_warn(ar, "failed to delete IBSS BSSID peer %pM for vdev %d: %d\n",
979                                     arvif->bssid, arvif->vdev_id, ret);
980                         return;
981                 }
982
983                 memset(arvif->bssid, 0, ETH_ALEN);
984
985                 return;
986         }
987
988         ret = ath10k_peer_create(arvif->ar, arvif->vdev_id, self_peer);
989         if (ret) {
990                 ath10k_warn(ar, "failed to create IBSS self peer %pM for vdev %d: %d\n",
991                             self_peer, arvif->vdev_id, ret);
992                 return;
993         }
994
995         vdev_param = arvif->ar->wmi.vdev_param->atim_window;
996         ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id, vdev_param,
997                                         ATH10K_DEFAULT_ATIM);
998         if (ret)
999                 ath10k_warn(ar, "failed to set IBSS ATIM for vdev %d: %d\n",
1000                             arvif->vdev_id, ret);
1001 }
1002
1003 /*
1004  * Review this when mac80211 gains per-interface powersave support.
1005  */
1006 static int ath10k_mac_vif_setup_ps(struct ath10k_vif *arvif)
1007 {
1008         struct ath10k *ar = arvif->ar;
1009         struct ieee80211_conf *conf = &ar->hw->conf;
1010         enum wmi_sta_powersave_param param;
1011         enum wmi_sta_ps_mode psmode;
1012         int ret;
1013
1014         lockdep_assert_held(&arvif->ar->conf_mutex);
1015
1016         if (arvif->vif->type != NL80211_IFTYPE_STATION)
1017                 return 0;
1018
1019         if (conf->flags & IEEE80211_CONF_PS) {
1020                 psmode = WMI_STA_PS_MODE_ENABLED;
1021                 param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
1022
1023                 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
1024                                                   conf->dynamic_ps_timeout);
1025                 if (ret) {
1026                         ath10k_warn(ar, "failed to set inactivity time for vdev %d: %i\n",
1027                                     arvif->vdev_id, ret);
1028                         return ret;
1029                 }
1030         } else {
1031                 psmode = WMI_STA_PS_MODE_DISABLED;
1032         }
1033
1034         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d psmode %s\n",
1035                    arvif->vdev_id, psmode ? "enable" : "disable");
1036
1037         ret = ath10k_wmi_set_psmode(ar, arvif->vdev_id, psmode);
1038         if (ret) {
1039                 ath10k_warn(ar, "failed to set PS Mode %d for vdev %d: %d\n",
1040                             psmode, arvif->vdev_id, ret);
1041                 return ret;
1042         }
1043
1044         return 0;
1045 }
1046
1047 /**********************/
1048 /* Station management */
1049 /**********************/
1050
1051 static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
1052                                       struct ath10k_vif *arvif,
1053                                       struct ieee80211_sta *sta,
1054                                       struct ieee80211_bss_conf *bss_conf,
1055                                       struct wmi_peer_assoc_complete_arg *arg)
1056 {
1057         lockdep_assert_held(&ar->conf_mutex);
1058
1059         memcpy(arg->addr, sta->addr, ETH_ALEN);
1060         arg->vdev_id = arvif->vdev_id;
1061         arg->peer_aid = sta->aid;
1062         arg->peer_flags |= WMI_PEER_AUTH;
1063
1064         if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
1065                 /*
1066                  * Seems FW have problems with Power Save in STA
1067                  * mode when we setup this parameter to high (eg. 5).
1068                  * Often we see that FW don't send NULL (with clean P flags)
1069                  * frame even there is info about buffered frames in beacons.
1070                  * Sometimes we have to wait more than 10 seconds before FW
1071                  * will wakeup. Often sending one ping from AP to our device
1072                  * just fail (more than 50%).
1073                  *
1074                  * Seems setting this FW parameter to 1 couse FW
1075                  * will check every beacon and will wakup immediately
1076                  * after detection buffered data.
1077                  */
1078                 arg->peer_listen_intval = 1;
1079         else
1080                 arg->peer_listen_intval = ar->hw->conf.listen_interval;
1081
1082         arg->peer_num_spatial_streams = 1;
1083
1084         /*
1085          * The assoc capabilities are available only in managed mode.
1086          */
1087         if (arvif->vdev_type == WMI_VDEV_TYPE_STA && bss_conf)
1088                 arg->peer_caps = bss_conf->assoc_capability;
1089 }
1090
1091 static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
1092                                        struct ath10k_vif *arvif,
1093                                        struct wmi_peer_assoc_complete_arg *arg)
1094 {
1095         struct ieee80211_vif *vif = arvif->vif;
1096         struct ieee80211_bss_conf *info = &vif->bss_conf;
1097         struct cfg80211_bss *bss;
1098         const u8 *rsnie = NULL;
1099         const u8 *wpaie = NULL;
1100
1101         lockdep_assert_held(&ar->conf_mutex);
1102
1103         bss = cfg80211_get_bss(ar->hw->wiphy, ar->hw->conf.chandef.chan,
1104                                info->bssid, NULL, 0, 0, 0);
1105         if (bss) {
1106                 const struct cfg80211_bss_ies *ies;
1107
1108                 rcu_read_lock();
1109                 rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
1110
1111                 ies = rcu_dereference(bss->ies);
1112
1113                 wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
1114                                 WLAN_OUI_TYPE_MICROSOFT_WPA,
1115                                 ies->data,
1116                                 ies->len);
1117                 rcu_read_unlock();
1118                 cfg80211_put_bss(ar->hw->wiphy, bss);
1119         }
1120
1121         /* FIXME: base on RSN IE/WPA IE is a correct idea? */
1122         if (rsnie || wpaie) {
1123                 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
1124                 arg->peer_flags |= WMI_PEER_NEED_PTK_4_WAY;
1125         }
1126
1127         if (wpaie) {
1128                 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: wpa ie found\n", __func__);
1129                 arg->peer_flags |= WMI_PEER_NEED_GTK_2_WAY;
1130         }
1131 }
1132
1133 static void ath10k_peer_assoc_h_rates(struct ath10k *ar,
1134                                       struct ieee80211_sta *sta,
1135                                       struct wmi_peer_assoc_complete_arg *arg)
1136 {
1137         struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
1138         const struct ieee80211_supported_band *sband;
1139         const struct ieee80211_rate *rates;
1140         u32 ratemask;
1141         int i;
1142
1143         lockdep_assert_held(&ar->conf_mutex);
1144
1145         sband = ar->hw->wiphy->bands[ar->hw->conf.chandef.chan->band];
1146         ratemask = sta->supp_rates[ar->hw->conf.chandef.chan->band];
1147         rates = sband->bitrates;
1148
1149         rateset->num_rates = 0;
1150
1151         for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
1152                 if (!(ratemask & 1))
1153                         continue;
1154
1155                 rateset->rates[rateset->num_rates] = rates->hw_value;
1156                 rateset->num_rates++;
1157         }
1158 }
1159
1160 static void ath10k_peer_assoc_h_ht(struct ath10k *ar,
1161                                    struct ieee80211_sta *sta,
1162                                    struct wmi_peer_assoc_complete_arg *arg)
1163 {
1164         const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1165         int i, n;
1166
1167         lockdep_assert_held(&ar->conf_mutex);
1168
1169         if (!ht_cap->ht_supported)
1170                 return;
1171
1172         arg->peer_flags |= WMI_PEER_HT;
1173         arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1174                                     ht_cap->ampdu_factor)) - 1;
1175
1176         arg->peer_mpdu_density =
1177                 ath10k_parse_mpdudensity(ht_cap->ampdu_density);
1178
1179         arg->peer_ht_caps = ht_cap->cap;
1180         arg->peer_rate_caps |= WMI_RC_HT_FLAG;
1181
1182         if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
1183                 arg->peer_flags |= WMI_PEER_LDPC;
1184
1185         if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
1186                 arg->peer_flags |= WMI_PEER_40MHZ;
1187                 arg->peer_rate_caps |= WMI_RC_CW40_FLAG;
1188         }
1189
1190         if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
1191                 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
1192
1193         if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
1194                 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
1195
1196         if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
1197                 arg->peer_rate_caps |= WMI_RC_TX_STBC_FLAG;
1198                 arg->peer_flags |= WMI_PEER_STBC;
1199         }
1200
1201         if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
1202                 u32 stbc;
1203                 stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
1204                 stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
1205                 stbc = stbc << WMI_RC_RX_STBC_FLAG_S;
1206                 arg->peer_rate_caps |= stbc;
1207                 arg->peer_flags |= WMI_PEER_STBC;
1208         }
1209
1210         if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
1211                 arg->peer_rate_caps |= WMI_RC_TS_FLAG;
1212         else if (ht_cap->mcs.rx_mask[1])
1213                 arg->peer_rate_caps |= WMI_RC_DS_FLAG;
1214
1215         for (i = 0, n = 0; i < IEEE80211_HT_MCS_MASK_LEN*8; i++)
1216                 if (ht_cap->mcs.rx_mask[i/8] & (1 << i%8))
1217                         arg->peer_ht_rates.rates[n++] = i;
1218
1219         /*
1220          * This is a workaround for HT-enabled STAs which break the spec
1221          * and have no HT capabilities RX mask (no HT RX MCS map).
1222          *
1223          * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
1224          * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
1225          *
1226          * Firmware asserts if such situation occurs.
1227          */
1228         if (n == 0) {
1229                 arg->peer_ht_rates.num_rates = 8;
1230                 for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
1231                         arg->peer_ht_rates.rates[i] = i;
1232         } else {
1233                 arg->peer_ht_rates.num_rates = n;
1234                 arg->peer_num_spatial_streams = sta->rx_nss;
1235         }
1236
1237         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
1238                    arg->addr,
1239                    arg->peer_ht_rates.num_rates,
1240                    arg->peer_num_spatial_streams);
1241 }
1242
1243 static int ath10k_peer_assoc_qos_ap(struct ath10k *ar,
1244                                     struct ath10k_vif *arvif,
1245                                     struct ieee80211_sta *sta)
1246 {
1247         u32 uapsd = 0;
1248         u32 max_sp = 0;
1249         int ret = 0;
1250
1251         lockdep_assert_held(&ar->conf_mutex);
1252
1253         if (sta->wme && sta->uapsd_queues) {
1254                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
1255                            sta->uapsd_queues, sta->max_sp);
1256
1257                 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1258                         uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
1259                                  WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
1260                 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1261                         uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
1262                                  WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
1263                 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1264                         uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
1265                                  WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
1266                 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1267                         uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
1268                                  WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
1269
1270
1271                 if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
1272                         max_sp = sta->max_sp;
1273
1274                 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1275                                                  sta->addr,
1276                                                  WMI_AP_PS_PEER_PARAM_UAPSD,
1277                                                  uapsd);
1278                 if (ret) {
1279                         ath10k_warn(ar, "failed to set ap ps peer param uapsd for vdev %i: %d\n",
1280                                     arvif->vdev_id, ret);
1281                         return ret;
1282                 }
1283
1284                 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1285                                                  sta->addr,
1286                                                  WMI_AP_PS_PEER_PARAM_MAX_SP,
1287                                                  max_sp);
1288                 if (ret) {
1289                         ath10k_warn(ar, "failed to set ap ps peer param max sp for vdev %i: %d\n",
1290                                     arvif->vdev_id, ret);
1291                         return ret;
1292                 }
1293
1294                 /* TODO setup this based on STA listen interval and
1295                    beacon interval. Currently we don't know
1296                    sta->listen_interval - mac80211 patch required.
1297                    Currently use 10 seconds */
1298                 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id, sta->addr,
1299                                         WMI_AP_PS_PEER_PARAM_AGEOUT_TIME, 10);
1300                 if (ret) {
1301                         ath10k_warn(ar, "failed to set ap ps peer param ageout time for vdev %i: %d\n",
1302                                     arvif->vdev_id, ret);
1303                         return ret;
1304                 }
1305         }
1306
1307         return 0;
1308 }
1309
1310 static void ath10k_peer_assoc_h_vht(struct ath10k *ar,
1311                                     struct ieee80211_sta *sta,
1312                                     struct wmi_peer_assoc_complete_arg *arg)
1313 {
1314         const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
1315         u8 ampdu_factor;
1316
1317         if (!vht_cap->vht_supported)
1318                 return;
1319
1320         arg->peer_flags |= WMI_PEER_VHT;
1321         arg->peer_vht_caps = vht_cap->cap;
1322
1323
1324         ampdu_factor = (vht_cap->cap &
1325                         IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
1326                        IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
1327
1328         /* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
1329          * zero in VHT IE. Using it would result in degraded throughput.
1330          * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
1331          * it if VHT max_mpdu is smaller. */
1332         arg->peer_max_mpdu = max(arg->peer_max_mpdu,
1333                                  (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1334                                         ampdu_factor)) - 1);
1335
1336         if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1337                 arg->peer_flags |= WMI_PEER_80MHZ;
1338
1339         arg->peer_vht_rates.rx_max_rate =
1340                 __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
1341         arg->peer_vht_rates.rx_mcs_set =
1342                 __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
1343         arg->peer_vht_rates.tx_max_rate =
1344                 __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
1345         arg->peer_vht_rates.tx_mcs_set =
1346                 __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
1347
1348         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
1349                    sta->addr, arg->peer_max_mpdu, arg->peer_flags);
1350 }
1351
1352 static void ath10k_peer_assoc_h_qos(struct ath10k *ar,
1353                                     struct ath10k_vif *arvif,
1354                                     struct ieee80211_sta *sta,
1355                                     struct ieee80211_bss_conf *bss_conf,
1356                                     struct wmi_peer_assoc_complete_arg *arg)
1357 {
1358         switch (arvif->vdev_type) {
1359         case WMI_VDEV_TYPE_AP:
1360                 if (sta->wme)
1361                         arg->peer_flags |= WMI_PEER_QOS;
1362
1363                 if (sta->wme && sta->uapsd_queues) {
1364                         arg->peer_flags |= WMI_PEER_APSD;
1365                         arg->peer_rate_caps |= WMI_RC_UAPSD_FLAG;
1366                 }
1367                 break;
1368         case WMI_VDEV_TYPE_STA:
1369                 if (bss_conf->qos)
1370                         arg->peer_flags |= WMI_PEER_QOS;
1371                 break;
1372         default:
1373                 break;
1374         }
1375 }
1376
1377 static void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
1378                                         struct ath10k_vif *arvif,
1379                                         struct ieee80211_sta *sta,
1380                                         struct wmi_peer_assoc_complete_arg *arg)
1381 {
1382         enum wmi_phy_mode phymode = MODE_UNKNOWN;
1383
1384         switch (ar->hw->conf.chandef.chan->band) {
1385         case IEEE80211_BAND_2GHZ:
1386                 if (sta->ht_cap.ht_supported) {
1387                         if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1388                                 phymode = MODE_11NG_HT40;
1389                         else
1390                                 phymode = MODE_11NG_HT20;
1391                 } else {
1392                         phymode = MODE_11G;
1393                 }
1394
1395                 break;
1396         case IEEE80211_BAND_5GHZ:
1397                 /*
1398                  * Check VHT first.
1399                  */
1400                 if (sta->vht_cap.vht_supported) {
1401                         if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1402                                 phymode = MODE_11AC_VHT80;
1403                         else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1404                                 phymode = MODE_11AC_VHT40;
1405                         else if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
1406                                 phymode = MODE_11AC_VHT20;
1407                 } else if (sta->ht_cap.ht_supported) {
1408                         if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1409                                 phymode = MODE_11NA_HT40;
1410                         else
1411                                 phymode = MODE_11NA_HT20;
1412                 } else {
1413                         phymode = MODE_11A;
1414                 }
1415
1416                 break;
1417         default:
1418                 break;
1419         }
1420
1421         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM phymode %s\n",
1422                    sta->addr, ath10k_wmi_phymode_str(phymode));
1423
1424         arg->peer_phymode = phymode;
1425         WARN_ON(phymode == MODE_UNKNOWN);
1426 }
1427
1428 static int ath10k_peer_assoc_prepare(struct ath10k *ar,
1429                                      struct ath10k_vif *arvif,
1430                                      struct ieee80211_sta *sta,
1431                                      struct ieee80211_bss_conf *bss_conf,
1432                                      struct wmi_peer_assoc_complete_arg *arg)
1433 {
1434         lockdep_assert_held(&ar->conf_mutex);
1435
1436         memset(arg, 0, sizeof(*arg));
1437
1438         ath10k_peer_assoc_h_basic(ar, arvif, sta, bss_conf, arg);
1439         ath10k_peer_assoc_h_crypto(ar, arvif, arg);
1440         ath10k_peer_assoc_h_rates(ar, sta, arg);
1441         ath10k_peer_assoc_h_ht(ar, sta, arg);
1442         ath10k_peer_assoc_h_vht(ar, sta, arg);
1443         ath10k_peer_assoc_h_qos(ar, arvif, sta, bss_conf, arg);
1444         ath10k_peer_assoc_h_phymode(ar, arvif, sta, arg);
1445
1446         return 0;
1447 }
1448
1449 static const u32 ath10k_smps_map[] = {
1450         [WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
1451         [WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
1452         [WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
1453         [WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
1454 };
1455
1456 static int ath10k_setup_peer_smps(struct ath10k *ar, struct ath10k_vif *arvif,
1457                                   const u8 *addr,
1458                                   const struct ieee80211_sta_ht_cap *ht_cap)
1459 {
1460         int smps;
1461
1462         if (!ht_cap->ht_supported)
1463                 return 0;
1464
1465         smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
1466         smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
1467
1468         if (smps >= ARRAY_SIZE(ath10k_smps_map))
1469                 return -EINVAL;
1470
1471         return ath10k_wmi_peer_set_param(ar, arvif->vdev_id, addr,
1472                                          WMI_PEER_SMPS_STATE,
1473                                          ath10k_smps_map[smps]);
1474 }
1475
1476 /* can be called only in mac80211 callbacks due to `key_count` usage */
1477 static void ath10k_bss_assoc(struct ieee80211_hw *hw,
1478                              struct ieee80211_vif *vif,
1479                              struct ieee80211_bss_conf *bss_conf)
1480 {
1481         struct ath10k *ar = hw->priv;
1482         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1483         struct ieee80211_sta_ht_cap ht_cap;
1484         struct wmi_peer_assoc_complete_arg peer_arg;
1485         struct ieee80211_sta *ap_sta;
1486         int ret;
1487
1488         lockdep_assert_held(&ar->conf_mutex);
1489
1490         rcu_read_lock();
1491
1492         ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
1493         if (!ap_sta) {
1494                 ath10k_warn(ar, "failed to find station entry for bss %pM vdev %i\n",
1495                             bss_conf->bssid, arvif->vdev_id);
1496                 rcu_read_unlock();
1497                 return;
1498         }
1499
1500         /* ap_sta must be accessed only within rcu section which must be left
1501          * before calling ath10k_setup_peer_smps() which might sleep. */
1502         ht_cap = ap_sta->ht_cap;
1503
1504         ret = ath10k_peer_assoc_prepare(ar, arvif, ap_sta,
1505                                         bss_conf, &peer_arg);
1506         if (ret) {
1507                 ath10k_warn(ar, "failed to prepare peer assoc for %pM vdev %i: %d\n",
1508                             bss_conf->bssid, arvif->vdev_id, ret);
1509                 rcu_read_unlock();
1510                 return;
1511         }
1512
1513         rcu_read_unlock();
1514
1515         ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
1516         if (ret) {
1517                 ath10k_warn(ar, "failed to run peer assoc for %pM vdev %i: %d\n",
1518                             bss_conf->bssid, arvif->vdev_id, ret);
1519                 return;
1520         }
1521
1522         ret = ath10k_setup_peer_smps(ar, arvif, bss_conf->bssid, &ht_cap);
1523         if (ret) {
1524                 ath10k_warn(ar, "failed to setup peer SMPS for vdev %i: %d\n",
1525                             arvif->vdev_id, ret);
1526                 return;
1527         }
1528
1529         ath10k_dbg(ar, ATH10K_DBG_MAC,
1530                    "mac vdev %d up (associated) bssid %pM aid %d\n",
1531                    arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
1532
1533         arvif->aid = bss_conf->aid;
1534         memcpy(arvif->bssid, bss_conf->bssid, ETH_ALEN);
1535
1536         ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
1537         if (ret) {
1538                 ath10k_warn(ar, "failed to set vdev %d up: %d\n",
1539                             arvif->vdev_id, ret);
1540                 return;
1541         }
1542
1543         arvif->is_up = true;
1544 }
1545
1546 /*
1547  * FIXME: flush TIDs
1548  */
1549 static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
1550                                 struct ieee80211_vif *vif)
1551 {
1552         struct ath10k *ar = hw->priv;
1553         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1554         int ret;
1555
1556         lockdep_assert_held(&ar->conf_mutex);
1557
1558         /*
1559          * For some reason, calling VDEV-DOWN before VDEV-STOP
1560          * makes the FW to send frames via HTT after disassociation.
1561          * No idea why this happens, even though VDEV-DOWN is supposed
1562          * to be analogous to link down, so just stop the VDEV.
1563          */
1564         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d stop (disassociated\n",
1565                    arvif->vdev_id);
1566
1567         /* FIXME: check return value */
1568         ret = ath10k_vdev_stop(arvif);
1569
1570         /*
1571          * If we don't call VDEV-DOWN after VDEV-STOP FW will remain active and
1572          * report beacons from previously associated network through HTT.
1573          * This in turn would spam mac80211 WARN_ON if we bring down all
1574          * interfaces as it expects there is no rx when no interface is
1575          * running.
1576          */
1577         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d down\n", arvif->vdev_id);
1578
1579         /* FIXME: why don't we print error if wmi call fails? */
1580         ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
1581
1582         arvif->def_wep_key_idx = 0;
1583
1584         arvif->is_started = false;
1585         arvif->is_up = false;
1586 }
1587
1588 static int ath10k_station_assoc(struct ath10k *ar, struct ath10k_vif *arvif,
1589                                 struct ieee80211_sta *sta, bool reassoc)
1590 {
1591         struct wmi_peer_assoc_complete_arg peer_arg;
1592         int ret = 0;
1593
1594         lockdep_assert_held(&ar->conf_mutex);
1595
1596         ret = ath10k_peer_assoc_prepare(ar, arvif, sta, NULL, &peer_arg);
1597         if (ret) {
1598                 ath10k_warn(ar, "failed to prepare WMI peer assoc for %pM vdev %i: %i\n",
1599                             sta->addr, arvif->vdev_id, ret);
1600                 return ret;
1601         }
1602
1603         peer_arg.peer_reassoc = reassoc;
1604         ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
1605         if (ret) {
1606                 ath10k_warn(ar, "failed to run peer assoc for STA %pM vdev %i: %d\n",
1607                             sta->addr, arvif->vdev_id, ret);
1608                 return ret;
1609         }
1610
1611         ret = ath10k_setup_peer_smps(ar, arvif, sta->addr, &sta->ht_cap);
1612         if (ret) {
1613                 ath10k_warn(ar, "failed to setup peer SMPS for vdev %d: %d\n",
1614                             arvif->vdev_id, ret);
1615                 return ret;
1616         }
1617
1618         if (!sta->wme) {
1619                 arvif->num_legacy_stations++;
1620                 ret  = ath10k_recalc_rtscts_prot(arvif);
1621                 if (ret) {
1622                         ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
1623                                     arvif->vdev_id, ret);
1624                         return ret;
1625                 }
1626         }
1627
1628         ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
1629         if (ret) {
1630                 ath10k_warn(ar, "failed to install peer wep keys for vdev %i: %d\n",
1631                             arvif->vdev_id, ret);
1632                 return ret;
1633         }
1634
1635         ret = ath10k_peer_assoc_qos_ap(ar, arvif, sta);
1636         if (ret) {
1637                 ath10k_warn(ar, "failed to set qos params for STA %pM for vdev %i: %d\n",
1638                             sta->addr, arvif->vdev_id, ret);
1639                 return ret;
1640         }
1641
1642         return ret;
1643 }
1644
1645 static int ath10k_station_disassoc(struct ath10k *ar, struct ath10k_vif *arvif,
1646                                    struct ieee80211_sta *sta)
1647 {
1648         int ret = 0;
1649
1650         lockdep_assert_held(&ar->conf_mutex);
1651
1652         if (!sta->wme) {
1653                 arvif->num_legacy_stations--;
1654                 ret = ath10k_recalc_rtscts_prot(arvif);
1655                 if (ret) {
1656                         ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
1657                                     arvif->vdev_id, ret);
1658                         return ret;
1659                 }
1660         }
1661
1662         ret = ath10k_clear_peer_keys(arvif, sta->addr);
1663         if (ret) {
1664                 ath10k_warn(ar, "failed to clear all peer wep keys for vdev %i: %d\n",
1665                             arvif->vdev_id, ret);
1666                 return ret;
1667         }
1668
1669         return ret;
1670 }
1671
1672 /**************/
1673 /* Regulatory */
1674 /**************/
1675
1676 static int ath10k_update_channel_list(struct ath10k *ar)
1677 {
1678         struct ieee80211_hw *hw = ar->hw;
1679         struct ieee80211_supported_band **bands;
1680         enum ieee80211_band band;
1681         struct ieee80211_channel *channel;
1682         struct wmi_scan_chan_list_arg arg = {0};
1683         struct wmi_channel_arg *ch;
1684         bool passive;
1685         int len;
1686         int ret;
1687         int i;
1688
1689         lockdep_assert_held(&ar->conf_mutex);
1690
1691         bands = hw->wiphy->bands;
1692         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1693                 if (!bands[band])
1694                         continue;
1695
1696                 for (i = 0; i < bands[band]->n_channels; i++) {
1697                         if (bands[band]->channels[i].flags &
1698                             IEEE80211_CHAN_DISABLED)
1699                                 continue;
1700
1701                         arg.n_channels++;
1702                 }
1703         }
1704
1705         len = sizeof(struct wmi_channel_arg) * arg.n_channels;
1706         arg.channels = kzalloc(len, GFP_KERNEL);
1707         if (!arg.channels)
1708                 return -ENOMEM;
1709
1710         ch = arg.channels;
1711         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1712                 if (!bands[band])
1713                         continue;
1714
1715                 for (i = 0; i < bands[band]->n_channels; i++) {
1716                         channel = &bands[band]->channels[i];
1717
1718                         if (channel->flags & IEEE80211_CHAN_DISABLED)
1719                                 continue;
1720
1721                         ch->allow_ht   = true;
1722
1723                         /* FIXME: when should we really allow VHT? */
1724                         ch->allow_vht = true;
1725
1726                         ch->allow_ibss =
1727                                 !(channel->flags & IEEE80211_CHAN_NO_IR);
1728
1729                         ch->ht40plus =
1730                                 !(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
1731
1732                         ch->chan_radar =
1733                                 !!(channel->flags & IEEE80211_CHAN_RADAR);
1734
1735                         passive = channel->flags & IEEE80211_CHAN_NO_IR;
1736                         ch->passive = passive;
1737
1738                         ch->freq = channel->center_freq;
1739                         ch->min_power = 0;
1740                         ch->max_power = channel->max_power * 2;
1741                         ch->max_reg_power = channel->max_reg_power * 2;
1742                         ch->max_antenna_gain = channel->max_antenna_gain * 2;
1743                         ch->reg_class_id = 0; /* FIXME */
1744
1745                         /* FIXME: why use only legacy modes, why not any
1746                          * HT/VHT modes? Would that even make any
1747                          * difference? */
1748                         if (channel->band == IEEE80211_BAND_2GHZ)
1749                                 ch->mode = MODE_11G;
1750                         else
1751                                 ch->mode = MODE_11A;
1752
1753                         if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
1754                                 continue;
1755
1756                         ath10k_dbg(ar, ATH10K_DBG_WMI,
1757                                    "mac channel [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
1758                                     ch - arg.channels, arg.n_channels,
1759                                    ch->freq, ch->max_power, ch->max_reg_power,
1760                                    ch->max_antenna_gain, ch->mode);
1761
1762                         ch++;
1763                 }
1764         }
1765
1766         ret = ath10k_wmi_scan_chan_list(ar, &arg);
1767         kfree(arg.channels);
1768
1769         return ret;
1770 }
1771
1772 static enum wmi_dfs_region
1773 ath10k_mac_get_dfs_region(enum nl80211_dfs_regions dfs_region)
1774 {
1775         switch (dfs_region) {
1776         case NL80211_DFS_UNSET:
1777                 return WMI_UNINIT_DFS_DOMAIN;
1778         case NL80211_DFS_FCC:
1779                 return WMI_FCC_DFS_DOMAIN;
1780         case NL80211_DFS_ETSI:
1781                 return WMI_ETSI_DFS_DOMAIN;
1782         case NL80211_DFS_JP:
1783                 return WMI_MKK4_DFS_DOMAIN;
1784         }
1785         return WMI_UNINIT_DFS_DOMAIN;
1786 }
1787
1788 static void ath10k_regd_update(struct ath10k *ar)
1789 {
1790         struct reg_dmn_pair_mapping *regpair;
1791         int ret;
1792         enum wmi_dfs_region wmi_dfs_reg;
1793         enum nl80211_dfs_regions nl_dfs_reg;
1794
1795         lockdep_assert_held(&ar->conf_mutex);
1796
1797         ret = ath10k_update_channel_list(ar);
1798         if (ret)
1799                 ath10k_warn(ar, "failed to update channel list: %d\n", ret);
1800
1801         regpair = ar->ath_common.regulatory.regpair;
1802
1803         if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
1804                 nl_dfs_reg = ar->dfs_detector->region;
1805                 wmi_dfs_reg = ath10k_mac_get_dfs_region(nl_dfs_reg);
1806         } else {
1807                 wmi_dfs_reg = WMI_UNINIT_DFS_DOMAIN;
1808         }
1809
1810         /* Target allows setting up per-band regdomain but ath_common provides
1811          * a combined one only */
1812         ret = ath10k_wmi_pdev_set_regdomain(ar,
1813                                             regpair->reg_domain,
1814                                             regpair->reg_domain, /* 2ghz */
1815                                             regpair->reg_domain, /* 5ghz */
1816                                             regpair->reg_2ghz_ctl,
1817                                             regpair->reg_5ghz_ctl,
1818                                             wmi_dfs_reg);
1819         if (ret)
1820                 ath10k_warn(ar, "failed to set pdev regdomain: %d\n", ret);
1821 }
1822
1823 static void ath10k_reg_notifier(struct wiphy *wiphy,
1824                                 struct regulatory_request *request)
1825 {
1826         struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
1827         struct ath10k *ar = hw->priv;
1828         bool result;
1829
1830         ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
1831
1832         if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
1833                 ath10k_dbg(ar, ATH10K_DBG_REGULATORY, "dfs region 0x%x\n",
1834                            request->dfs_region);
1835                 result = ar->dfs_detector->set_dfs_domain(ar->dfs_detector,
1836                                                           request->dfs_region);
1837                 if (!result)
1838                         ath10k_warn(ar, "DFS region 0x%X not supported, will trigger radar for every pulse\n",
1839                                     request->dfs_region);
1840         }
1841
1842         mutex_lock(&ar->conf_mutex);
1843         if (ar->state == ATH10K_STATE_ON)
1844                 ath10k_regd_update(ar);
1845         mutex_unlock(&ar->conf_mutex);
1846 }
1847
1848 /***************/
1849 /* TX handlers */
1850 /***************/
1851
1852 static u8 ath10k_tx_h_get_tid(struct ieee80211_hdr *hdr)
1853 {
1854         if (ieee80211_is_mgmt(hdr->frame_control))
1855                 return HTT_DATA_TX_EXT_TID_MGMT;
1856
1857         if (!ieee80211_is_data_qos(hdr->frame_control))
1858                 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
1859
1860         if (!is_unicast_ether_addr(ieee80211_get_DA(hdr)))
1861                 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
1862
1863         return ieee80211_get_qos_ctl(hdr)[0] & IEEE80211_QOS_CTL_TID_MASK;
1864 }
1865
1866 static u8 ath10k_tx_h_get_vdev_id(struct ath10k *ar,
1867                                   struct ieee80211_tx_info *info)
1868 {
1869         if (info->control.vif)
1870                 return ath10k_vif_to_arvif(info->control.vif)->vdev_id;
1871
1872         if (ar->monitor_started)
1873                 return ar->monitor_vdev_id;
1874
1875         ath10k_warn(ar, "failed to resolve vdev id\n");
1876         return 0;
1877 }
1878
1879 /* HTT Tx uses Native Wifi tx mode which expects 802.11 frames without QoS
1880  * Control in the header.
1881  */
1882 static void ath10k_tx_h_nwifi(struct ieee80211_hw *hw, struct sk_buff *skb)
1883 {
1884         struct ieee80211_hdr *hdr = (void *)skb->data;
1885         struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
1886         u8 *qos_ctl;
1887
1888         if (!ieee80211_is_data_qos(hdr->frame_control))
1889                 return;
1890
1891         qos_ctl = ieee80211_get_qos_ctl(hdr);
1892         memmove(skb->data + IEEE80211_QOS_CTL_LEN,
1893                 skb->data, (void *)qos_ctl - (void *)skb->data);
1894         skb_pull(skb, IEEE80211_QOS_CTL_LEN);
1895
1896         /* Fw/Hw generates a corrupted QoS Control Field for QoS NullFunc
1897          * frames. Powersave is handled by the fw/hw so QoS NyllFunc frames are
1898          * used only for CQM purposes (e.g. hostapd station keepalive ping) so
1899          * it is safe to downgrade to NullFunc.
1900          */
1901         if (ieee80211_is_qos_nullfunc(hdr->frame_control)) {
1902                 hdr->frame_control &= ~__cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
1903                 cb->htt.tid = HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
1904         }
1905 }
1906
1907 static void ath10k_tx_wep_key_work(struct work_struct *work)
1908 {
1909         struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
1910                                                 wep_key_work);
1911         struct ath10k *ar = arvif->ar;
1912         int ret, keyidx = arvif->def_wep_key_newidx;
1913
1914         mutex_lock(&arvif->ar->conf_mutex);
1915
1916         if (arvif->ar->state != ATH10K_STATE_ON)
1917                 goto unlock;
1918
1919         if (arvif->def_wep_key_idx == keyidx)
1920                 goto unlock;
1921
1922         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
1923                    arvif->vdev_id, keyidx);
1924
1925         ret = ath10k_wmi_vdev_set_param(arvif->ar,
1926                                         arvif->vdev_id,
1927                                         arvif->ar->wmi.vdev_param->def_keyid,
1928                                         keyidx);
1929         if (ret) {
1930                 ath10k_warn(ar, "failed to update wep key index for vdev %d: %d\n",
1931                             arvif->vdev_id,
1932                             ret);
1933                 goto unlock;
1934         }
1935
1936         arvif->def_wep_key_idx = keyidx;
1937
1938 unlock:
1939         mutex_unlock(&arvif->ar->conf_mutex);
1940 }
1941
1942 static void ath10k_tx_h_update_wep_key(struct ieee80211_vif *vif,
1943                                        struct ieee80211_key_conf *key,
1944                                        struct sk_buff *skb)
1945 {
1946         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1947         struct ath10k *ar = arvif->ar;
1948         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1949
1950         if (!ieee80211_has_protected(hdr->frame_control))
1951                 return;
1952
1953         if (!key)
1954                 return;
1955
1956         if (key->cipher != WLAN_CIPHER_SUITE_WEP40 &&
1957             key->cipher != WLAN_CIPHER_SUITE_WEP104)
1958                 return;
1959
1960         if (key->keyidx == arvif->def_wep_key_idx)
1961                 return;
1962
1963         /* FIXME: Most likely a few frames will be TXed with an old key. Simply
1964          * queueing frames until key index is updated is not an option because
1965          * sk_buff may need more processing to be done, e.g. offchannel */
1966         arvif->def_wep_key_newidx = key->keyidx;
1967         ieee80211_queue_work(ar->hw, &arvif->wep_key_work);
1968 }
1969
1970 static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar,
1971                                        struct ieee80211_vif *vif,
1972                                        struct sk_buff *skb)
1973 {
1974         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1975         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1976
1977         /* This is case only for P2P_GO */
1978         if (arvif->vdev_type != WMI_VDEV_TYPE_AP ||
1979             arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
1980                 return;
1981
1982         if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
1983                 spin_lock_bh(&ar->data_lock);
1984                 if (arvif->u.ap.noa_data)
1985                         if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
1986                                               GFP_ATOMIC))
1987                                 memcpy(skb_put(skb, arvif->u.ap.noa_len),
1988                                        arvif->u.ap.noa_data,
1989                                        arvif->u.ap.noa_len);
1990                 spin_unlock_bh(&ar->data_lock);
1991         }
1992 }
1993
1994 static void ath10k_tx_htt(struct ath10k *ar, struct sk_buff *skb)
1995 {
1996         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1997         int ret = 0;
1998
1999         if (ar->htt.target_version_major >= 3) {
2000                 /* Since HTT 3.0 there is no separate mgmt tx command */
2001                 ret = ath10k_htt_tx(&ar->htt, skb);
2002                 goto exit;
2003         }
2004
2005         if (ieee80211_is_mgmt(hdr->frame_control)) {
2006                 if (test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
2007                              ar->fw_features)) {
2008                         if (skb_queue_len(&ar->wmi_mgmt_tx_queue) >=
2009                             ATH10K_MAX_NUM_MGMT_PENDING) {
2010                                 ath10k_warn(ar, "reached WMI management transmit queue limit\n");
2011                                 ret = -EBUSY;
2012                                 goto exit;
2013                         }
2014
2015                         skb_queue_tail(&ar->wmi_mgmt_tx_queue, skb);
2016                         ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
2017                 } else {
2018                         ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
2019                 }
2020         } else if (!test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
2021                              ar->fw_features) &&
2022                    ieee80211_is_nullfunc(hdr->frame_control)) {
2023                 /* FW does not report tx status properly for NullFunc frames
2024                  * unless they are sent through mgmt tx path. mac80211 sends
2025                  * those frames when it detects link/beacon loss and depends
2026                  * on the tx status to be correct. */
2027                 ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
2028         } else {
2029                 ret = ath10k_htt_tx(&ar->htt, skb);
2030         }
2031
2032 exit:
2033         if (ret) {
2034                 ath10k_warn(ar, "failed to transmit packet, dropping: %d\n",
2035                             ret);
2036                 ieee80211_free_txskb(ar->hw, skb);
2037         }
2038 }
2039
2040 void ath10k_offchan_tx_purge(struct ath10k *ar)
2041 {
2042         struct sk_buff *skb;
2043
2044         for (;;) {
2045                 skb = skb_dequeue(&ar->offchan_tx_queue);
2046                 if (!skb)
2047                         break;
2048
2049                 ieee80211_free_txskb(ar->hw, skb);
2050         }
2051 }
2052
2053 void ath10k_offchan_tx_work(struct work_struct *work)
2054 {
2055         struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
2056         struct ath10k_peer *peer;
2057         struct ieee80211_hdr *hdr;
2058         struct sk_buff *skb;
2059         const u8 *peer_addr;
2060         int vdev_id;
2061         int ret;
2062
2063         /* FW requirement: We must create a peer before FW will send out
2064          * an offchannel frame. Otherwise the frame will be stuck and
2065          * never transmitted. We delete the peer upon tx completion.
2066          * It is unlikely that a peer for offchannel tx will already be
2067          * present. However it may be in some rare cases so account for that.
2068          * Otherwise we might remove a legitimate peer and break stuff. */
2069
2070         for (;;) {
2071                 skb = skb_dequeue(&ar->offchan_tx_queue);
2072                 if (!skb)
2073                         break;
2074
2075                 mutex_lock(&ar->conf_mutex);
2076
2077                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac offchannel skb %p\n",
2078                            skb);
2079
2080                 hdr = (struct ieee80211_hdr *)skb->data;
2081                 peer_addr = ieee80211_get_DA(hdr);
2082                 vdev_id = ATH10K_SKB_CB(skb)->vdev_id;
2083
2084                 spin_lock_bh(&ar->data_lock);
2085                 peer = ath10k_peer_find(ar, vdev_id, peer_addr);
2086                 spin_unlock_bh(&ar->data_lock);
2087
2088                 if (peer)
2089                         /* FIXME: should this use ath10k_warn()? */
2090                         ath10k_dbg(ar, ATH10K_DBG_MAC, "peer %pM on vdev %d already present\n",
2091                                    peer_addr, vdev_id);
2092
2093                 if (!peer) {
2094                         ret = ath10k_peer_create(ar, vdev_id, peer_addr);
2095                         if (ret)
2096                                 ath10k_warn(ar, "failed to create peer %pM on vdev %d: %d\n",
2097                                             peer_addr, vdev_id, ret);
2098                 }
2099
2100                 spin_lock_bh(&ar->data_lock);
2101                 reinit_completion(&ar->offchan_tx_completed);
2102                 ar->offchan_tx_skb = skb;
2103                 spin_unlock_bh(&ar->data_lock);
2104
2105                 ath10k_tx_htt(ar, skb);
2106
2107                 ret = wait_for_completion_timeout(&ar->offchan_tx_completed,
2108                                                   3 * HZ);
2109                 if (ret <= 0)
2110                         ath10k_warn(ar, "timed out waiting for offchannel skb %p\n",
2111                                     skb);
2112
2113                 if (!peer) {
2114                         ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
2115                         if (ret)
2116                                 ath10k_warn(ar, "failed to delete peer %pM on vdev %d: %d\n",
2117                                             peer_addr, vdev_id, ret);
2118                 }
2119
2120                 mutex_unlock(&ar->conf_mutex);
2121         }
2122 }
2123
2124 void ath10k_mgmt_over_wmi_tx_purge(struct ath10k *ar)
2125 {
2126         struct sk_buff *skb;
2127
2128         for (;;) {
2129                 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
2130                 if (!skb)
2131                         break;
2132
2133                 ieee80211_free_txskb(ar->hw, skb);
2134         }
2135 }
2136
2137 void ath10k_mgmt_over_wmi_tx_work(struct work_struct *work)
2138 {
2139         struct ath10k *ar = container_of(work, struct ath10k, wmi_mgmt_tx_work);
2140         struct sk_buff *skb;
2141         int ret;
2142
2143         for (;;) {
2144                 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
2145                 if (!skb)
2146                         break;
2147
2148                 ret = ath10k_wmi_mgmt_tx(ar, skb);
2149                 if (ret) {
2150                         ath10k_warn(ar, "failed to transmit management frame via WMI: %d\n",
2151                                     ret);
2152                         ieee80211_free_txskb(ar->hw, skb);
2153                 }
2154         }
2155 }
2156
2157 /************/
2158 /* Scanning */
2159 /************/
2160
2161 void __ath10k_scan_finish(struct ath10k *ar)
2162 {
2163         lockdep_assert_held(&ar->data_lock);
2164
2165         switch (ar->scan.state) {
2166         case ATH10K_SCAN_IDLE:
2167                 break;
2168         case ATH10K_SCAN_RUNNING:
2169         case ATH10K_SCAN_ABORTING:
2170                 if (ar->scan.is_roc)
2171                         ieee80211_remain_on_channel_expired(ar->hw);
2172                 else
2173                         ieee80211_scan_completed(ar->hw,
2174                                                  (ar->scan.state ==
2175                                                   ATH10K_SCAN_ABORTING));
2176                 /* fall through */
2177         case ATH10K_SCAN_STARTING:
2178                 ar->scan.state = ATH10K_SCAN_IDLE;
2179                 ar->scan_channel = NULL;
2180                 ath10k_offchan_tx_purge(ar);
2181                 cancel_delayed_work(&ar->scan.timeout);
2182                 complete_all(&ar->scan.completed);
2183                 break;
2184         }
2185 }
2186
2187 void ath10k_scan_finish(struct ath10k *ar)
2188 {
2189         spin_lock_bh(&ar->data_lock);
2190         __ath10k_scan_finish(ar);
2191         spin_unlock_bh(&ar->data_lock);
2192 }
2193
2194 static int ath10k_scan_stop(struct ath10k *ar)
2195 {
2196         struct wmi_stop_scan_arg arg = {
2197                 .req_id = 1, /* FIXME */
2198                 .req_type = WMI_SCAN_STOP_ONE,
2199                 .u.scan_id = ATH10K_SCAN_ID,
2200         };
2201         int ret;
2202
2203         lockdep_assert_held(&ar->conf_mutex);
2204
2205         ret = ath10k_wmi_stop_scan(ar, &arg);
2206         if (ret) {
2207                 ath10k_warn(ar, "failed to stop wmi scan: %d\n", ret);
2208                 goto out;
2209         }
2210
2211         ret = wait_for_completion_timeout(&ar->scan.completed, 3*HZ);
2212         if (ret == 0) {
2213                 ath10k_warn(ar, "failed to receive scan abortion completion: timed out\n");
2214                 ret = -ETIMEDOUT;
2215         } else if (ret > 0) {
2216                 ret = 0;
2217         }
2218
2219 out:
2220         /* Scan state should be updated upon scan completion but in case
2221          * firmware fails to deliver the event (for whatever reason) it is
2222          * desired to clean up scan state anyway. Firmware may have just
2223          * dropped the scan completion event delivery due to transport pipe
2224          * being overflown with data and/or it can recover on its own before
2225          * next scan request is submitted.
2226          */
2227         spin_lock_bh(&ar->data_lock);
2228         if (ar->scan.state != ATH10K_SCAN_IDLE)
2229                 __ath10k_scan_finish(ar);
2230         spin_unlock_bh(&ar->data_lock);
2231
2232         return ret;
2233 }
2234
2235 static void ath10k_scan_abort(struct ath10k *ar)
2236 {
2237         int ret;
2238
2239         lockdep_assert_held(&ar->conf_mutex);
2240
2241         spin_lock_bh(&ar->data_lock);
2242
2243         switch (ar->scan.state) {
2244         case ATH10K_SCAN_IDLE:
2245                 /* This can happen if timeout worker kicked in and called
2246                  * abortion while scan completion was being processed.
2247                  */
2248                 break;
2249         case ATH10K_SCAN_STARTING:
2250         case ATH10K_SCAN_ABORTING:
2251                 ath10k_warn(ar, "refusing scan abortion due to invalid scan state: %s (%d)\n",
2252                             ath10k_scan_state_str(ar->scan.state),
2253                             ar->scan.state);
2254                 break;
2255         case ATH10K_SCAN_RUNNING:
2256                 ar->scan.state = ATH10K_SCAN_ABORTING;
2257                 spin_unlock_bh(&ar->data_lock);
2258
2259                 ret = ath10k_scan_stop(ar);
2260                 if (ret)
2261                         ath10k_warn(ar, "failed to abort scan: %d\n", ret);
2262
2263                 spin_lock_bh(&ar->data_lock);
2264                 break;
2265         }
2266
2267         spin_unlock_bh(&ar->data_lock);
2268 }
2269
2270 void ath10k_scan_timeout_work(struct work_struct *work)
2271 {
2272         struct ath10k *ar = container_of(work, struct ath10k,
2273                                          scan.timeout.work);
2274
2275         mutex_lock(&ar->conf_mutex);
2276         ath10k_scan_abort(ar);
2277         mutex_unlock(&ar->conf_mutex);
2278 }
2279
2280 static int ath10k_start_scan(struct ath10k *ar,
2281                              const struct wmi_start_scan_arg *arg)
2282 {
2283         int ret;
2284
2285         lockdep_assert_held(&ar->conf_mutex);
2286
2287         ret = ath10k_wmi_start_scan(ar, arg);
2288         if (ret)
2289                 return ret;
2290
2291         ret = wait_for_completion_timeout(&ar->scan.started, 1*HZ);
2292         if (ret == 0) {
2293                 ret = ath10k_scan_stop(ar);
2294                 if (ret)
2295                         ath10k_warn(ar, "failed to stop scan: %d\n", ret);
2296
2297                 return -ETIMEDOUT;
2298         }
2299
2300         /* Add a 200ms margin to account for event/command processing */
2301         ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
2302                                      msecs_to_jiffies(arg->max_scan_time+200));
2303         return 0;
2304 }
2305
2306 /**********************/
2307 /* mac80211 callbacks */
2308 /**********************/
2309
2310 static void ath10k_tx(struct ieee80211_hw *hw,
2311                       struct ieee80211_tx_control *control,
2312                       struct sk_buff *skb)
2313 {
2314         struct ath10k *ar = hw->priv;
2315         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2316         struct ieee80211_vif *vif = info->control.vif;
2317         struct ieee80211_key_conf *key = info->control.hw_key;
2318         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2319
2320         /* We should disable CCK RATE due to P2P */
2321         if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
2322                 ath10k_dbg(ar, ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
2323
2324         ATH10K_SKB_CB(skb)->htt.is_offchan = false;
2325         ATH10K_SKB_CB(skb)->htt.tid = ath10k_tx_h_get_tid(hdr);
2326         ATH10K_SKB_CB(skb)->vdev_id = ath10k_tx_h_get_vdev_id(ar, info);
2327
2328         /* it makes no sense to process injected frames like that */
2329         if (vif && vif->type != NL80211_IFTYPE_MONITOR) {
2330                 ath10k_tx_h_nwifi(hw, skb);
2331                 ath10k_tx_h_update_wep_key(vif, key, skb);
2332                 ath10k_tx_h_add_p2p_noa_ie(ar, vif, skb);
2333                 ath10k_tx_h_seq_no(vif, skb);
2334         }
2335
2336         if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
2337                 spin_lock_bh(&ar->data_lock);
2338                 ATH10K_SKB_CB(skb)->htt.is_offchan = true;
2339                 ATH10K_SKB_CB(skb)->vdev_id = ar->scan.vdev_id;
2340                 spin_unlock_bh(&ar->data_lock);
2341
2342                 ath10k_dbg(ar, ATH10K_DBG_MAC, "queued offchannel skb %p\n",
2343                            skb);
2344
2345                 skb_queue_tail(&ar->offchan_tx_queue, skb);
2346                 ieee80211_queue_work(hw, &ar->offchan_tx_work);
2347                 return;
2348         }
2349
2350         ath10k_tx_htt(ar, skb);
2351 }
2352
2353 /* Must not be called with conf_mutex held as workers can use that also. */
2354 static void ath10k_drain_tx(struct ath10k *ar)
2355 {
2356         /* make sure rcu-protected mac80211 tx path itself is drained */
2357         synchronize_net();
2358
2359         ath10k_offchan_tx_purge(ar);
2360         ath10k_mgmt_over_wmi_tx_purge(ar);
2361
2362         cancel_work_sync(&ar->offchan_tx_work);
2363         cancel_work_sync(&ar->wmi_mgmt_tx_work);
2364 }
2365
2366 void ath10k_halt(struct ath10k *ar)
2367 {
2368         struct ath10k_vif *arvif;
2369
2370         lockdep_assert_held(&ar->conf_mutex);
2371
2372         if (ath10k_monitor_is_enabled(ar)) {
2373                 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
2374                 ar->promisc = false;
2375                 ar->monitor = false;
2376                 ath10k_monitor_stop(ar);
2377         }
2378
2379         ath10k_scan_finish(ar);
2380         ath10k_peer_cleanup_all(ar);
2381         ath10k_core_stop(ar);
2382         ath10k_hif_power_down(ar);
2383
2384         spin_lock_bh(&ar->data_lock);
2385         list_for_each_entry(arvif, &ar->arvifs, list) {
2386                 if (!arvif->beacon)
2387                         continue;
2388
2389                 dma_unmap_single(arvif->ar->dev,
2390                                  ATH10K_SKB_CB(arvif->beacon)->paddr,
2391                                  arvif->beacon->len, DMA_TO_DEVICE);
2392                 dev_kfree_skb_any(arvif->beacon);
2393                 arvif->beacon = NULL;
2394         }
2395         spin_unlock_bh(&ar->data_lock);
2396 }
2397
2398 static int ath10k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
2399 {
2400         struct ath10k *ar = hw->priv;
2401
2402         mutex_lock(&ar->conf_mutex);
2403
2404         if (ar->cfg_tx_chainmask) {
2405                 *tx_ant = ar->cfg_tx_chainmask;
2406                 *rx_ant = ar->cfg_rx_chainmask;
2407         } else {
2408                 *tx_ant = ar->supp_tx_chainmask;
2409                 *rx_ant = ar->supp_rx_chainmask;
2410         }
2411
2412         mutex_unlock(&ar->conf_mutex);
2413
2414         return 0;
2415 }
2416
2417 static int __ath10k_set_antenna(struct ath10k *ar, u32 tx_ant, u32 rx_ant)
2418 {
2419         int ret;
2420
2421         lockdep_assert_held(&ar->conf_mutex);
2422
2423         ar->cfg_tx_chainmask = tx_ant;
2424         ar->cfg_rx_chainmask = rx_ant;
2425
2426         if ((ar->state != ATH10K_STATE_ON) &&
2427             (ar->state != ATH10K_STATE_RESTARTED))
2428                 return 0;
2429
2430         ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->tx_chain_mask,
2431                                         tx_ant);
2432         if (ret) {
2433                 ath10k_warn(ar, "failed to set tx-chainmask: %d, req 0x%x\n",
2434                             ret, tx_ant);
2435                 return ret;
2436         }
2437
2438         ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->rx_chain_mask,
2439                                         rx_ant);
2440         if (ret) {
2441                 ath10k_warn(ar, "failed to set rx-chainmask: %d, req 0x%x\n",
2442                             ret, rx_ant);
2443                 return ret;
2444         }
2445
2446         return 0;
2447 }
2448
2449 static int ath10k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
2450 {
2451         struct ath10k *ar = hw->priv;
2452         int ret;
2453
2454         mutex_lock(&ar->conf_mutex);
2455         ret = __ath10k_set_antenna(ar, tx_ant, rx_ant);
2456         mutex_unlock(&ar->conf_mutex);
2457         return ret;
2458 }
2459
2460 static int ath10k_start(struct ieee80211_hw *hw)
2461 {
2462         struct ath10k *ar = hw->priv;
2463         int ret = 0;
2464
2465         /*
2466          * This makes sense only when restarting hw. It is harmless to call
2467          * uncoditionally. This is necessary to make sure no HTT/WMI tx
2468          * commands will be submitted while restarting.
2469          */
2470         ath10k_drain_tx(ar);
2471
2472         mutex_lock(&ar->conf_mutex);
2473
2474         switch (ar->state) {
2475         case ATH10K_STATE_OFF:
2476                 ar->state = ATH10K_STATE_ON;
2477                 break;
2478         case ATH10K_STATE_RESTARTING:
2479                 ath10k_halt(ar);
2480                 ar->state = ATH10K_STATE_RESTARTED;
2481                 break;
2482         case ATH10K_STATE_ON:
2483         case ATH10K_STATE_RESTARTED:
2484         case ATH10K_STATE_WEDGED:
2485                 WARN_ON(1);
2486                 ret = -EINVAL;
2487                 goto err;
2488         }
2489
2490         ret = ath10k_hif_power_up(ar);
2491         if (ret) {
2492                 ath10k_err(ar, "Could not init hif: %d\n", ret);
2493                 goto err_off;
2494         }
2495
2496         ret = ath10k_core_start(ar);
2497         if (ret) {
2498                 ath10k_err(ar, "Could not init core: %d\n", ret);
2499                 goto err_power_down;
2500         }
2501
2502         ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->pmf_qos, 1);
2503         if (ret) {
2504                 ath10k_warn(ar, "failed to enable PMF QOS: %d\n", ret);
2505                 goto err_core_stop;
2506         }
2507
2508         ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->dynamic_bw, 1);
2509         if (ret) {
2510                 ath10k_warn(ar, "failed to enable dynamic BW: %d\n", ret);
2511                 goto err_core_stop;
2512         }
2513
2514         if (ar->cfg_tx_chainmask)
2515                 __ath10k_set_antenna(ar, ar->cfg_tx_chainmask,
2516                                      ar->cfg_rx_chainmask);
2517
2518         /*
2519          * By default FW set ARP frames ac to voice (6). In that case ARP
2520          * exchange is not working properly for UAPSD enabled AP. ARP requests
2521          * which arrives with access category 0 are processed by network stack
2522          * and send back with access category 0, but FW changes access category
2523          * to 6. Set ARP frames access category to best effort (0) solves
2524          * this problem.
2525          */
2526
2527         ret = ath10k_wmi_pdev_set_param(ar,
2528                                         ar->wmi.pdev_param->arp_ac_override, 0);
2529         if (ret) {
2530                 ath10k_warn(ar, "failed to set arp ac override parameter: %d\n",
2531                             ret);
2532                 goto err_core_stop;
2533         }
2534
2535         ar->num_started_vdevs = 0;
2536         ath10k_regd_update(ar);
2537
2538         ath10k_spectral_start(ar);
2539
2540         mutex_unlock(&ar->conf_mutex);
2541         return 0;
2542
2543 err_core_stop:
2544         ath10k_core_stop(ar);
2545
2546 err_power_down:
2547         ath10k_hif_power_down(ar);
2548
2549 err_off:
2550         ar->state = ATH10K_STATE_OFF;
2551
2552 err:
2553         mutex_unlock(&ar->conf_mutex);
2554         return ret;
2555 }
2556
2557 static void ath10k_stop(struct ieee80211_hw *hw)
2558 {
2559         struct ath10k *ar = hw->priv;
2560
2561         ath10k_drain_tx(ar);
2562
2563         mutex_lock(&ar->conf_mutex);
2564         if (ar->state != ATH10K_STATE_OFF) {
2565                 ath10k_halt(ar);
2566                 ar->state = ATH10K_STATE_OFF;
2567         }
2568         mutex_unlock(&ar->conf_mutex);
2569
2570         cancel_delayed_work_sync(&ar->scan.timeout);
2571         cancel_work_sync(&ar->restart_work);
2572 }
2573
2574 static int ath10k_config_ps(struct ath10k *ar)
2575 {
2576         struct ath10k_vif *arvif;
2577         int ret = 0;
2578
2579         lockdep_assert_held(&ar->conf_mutex);
2580
2581         list_for_each_entry(arvif, &ar->arvifs, list) {
2582                 ret = ath10k_mac_vif_setup_ps(arvif);
2583                 if (ret) {
2584                         ath10k_warn(ar, "failed to setup powersave: %d\n", ret);
2585                         break;
2586                 }
2587         }
2588
2589         return ret;
2590 }
2591
2592 static const char *chandef_get_width(enum nl80211_chan_width width)
2593 {
2594         switch (width) {
2595         case NL80211_CHAN_WIDTH_20_NOHT:
2596                 return "20 (noht)";
2597         case NL80211_CHAN_WIDTH_20:
2598                 return "20";
2599         case NL80211_CHAN_WIDTH_40:
2600                 return "40";
2601         case NL80211_CHAN_WIDTH_80:
2602                 return "80";
2603         case NL80211_CHAN_WIDTH_80P80:
2604                 return "80+80";
2605         case NL80211_CHAN_WIDTH_160:
2606                 return "160";
2607         case NL80211_CHAN_WIDTH_5:
2608                 return "5";
2609         case NL80211_CHAN_WIDTH_10:
2610                 return "10";
2611         }
2612         return "?";
2613 }
2614
2615 static void ath10k_config_chan(struct ath10k *ar)
2616 {
2617         struct ath10k_vif *arvif;
2618         int ret;
2619
2620         lockdep_assert_held(&ar->conf_mutex);
2621
2622         ath10k_dbg(ar, ATH10K_DBG_MAC,
2623                    "mac config channel to %dMHz (cf1 %dMHz cf2 %dMHz width %s)\n",
2624                    ar->chandef.chan->center_freq,
2625                    ar->chandef.center_freq1,
2626                    ar->chandef.center_freq2,
2627                    chandef_get_width(ar->chandef.width));
2628
2629         /* First stop monitor interface. Some FW versions crash if there's a
2630          * lone monitor interface. */
2631         if (ar->monitor_started)
2632                 ath10k_monitor_vdev_stop(ar);
2633
2634         list_for_each_entry(arvif, &ar->arvifs, list) {
2635                 if (!arvif->is_started)
2636                         continue;
2637
2638                 if (!arvif->is_up)
2639                         continue;
2640
2641                 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
2642                         continue;
2643
2644                 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
2645                 if (ret) {
2646                         ath10k_warn(ar, "failed to down vdev %d: %d\n",
2647                                     arvif->vdev_id, ret);
2648                         continue;
2649                 }
2650         }
2651
2652         /* all vdevs are downed now - attempt to restart and re-up them */
2653
2654         list_for_each_entry(arvif, &ar->arvifs, list) {
2655                 if (!arvif->is_started)
2656                         continue;
2657
2658                 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
2659                         continue;
2660
2661                 ret = ath10k_vdev_restart(arvif);
2662                 if (ret) {
2663                         ath10k_warn(ar, "failed to restart vdev %d: %d\n",
2664                                     arvif->vdev_id, ret);
2665                         continue;
2666                 }
2667
2668                 if (!arvif->is_up)
2669                         continue;
2670
2671                 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
2672                                          arvif->bssid);
2673                 if (ret) {
2674                         ath10k_warn(ar, "failed to bring vdev up %d: %d\n",
2675                                     arvif->vdev_id, ret);
2676                         continue;
2677                 }
2678         }
2679
2680         if (ath10k_monitor_is_enabled(ar))
2681                 ath10k_monitor_vdev_start(ar, ar->monitor_vdev_id);
2682 }
2683
2684 static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
2685 {
2686         struct ath10k *ar = hw->priv;
2687         struct ieee80211_conf *conf = &hw->conf;
2688         int ret = 0;
2689         u32 param;
2690
2691         mutex_lock(&ar->conf_mutex);
2692
2693         if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
2694                 ath10k_dbg(ar, ATH10K_DBG_MAC,
2695                            "mac config channel %dMHz flags 0x%x radar %d\n",
2696                            conf->chandef.chan->center_freq,
2697                            conf->chandef.chan->flags,
2698                            conf->radar_enabled);
2699
2700                 spin_lock_bh(&ar->data_lock);
2701                 ar->rx_channel = conf->chandef.chan;
2702                 spin_unlock_bh(&ar->data_lock);
2703
2704                 ar->radar_enabled = conf->radar_enabled;
2705                 ath10k_recalc_radar_detection(ar);
2706
2707                 if (!cfg80211_chandef_identical(&ar->chandef, &conf->chandef)) {
2708                         ar->chandef = conf->chandef;
2709                         ath10k_config_chan(ar);
2710                 }
2711         }
2712
2713         if (changed & IEEE80211_CONF_CHANGE_POWER) {
2714                 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac config power %d\n",
2715                            hw->conf.power_level);
2716
2717                 param = ar->wmi.pdev_param->txpower_limit2g;
2718                 ret = ath10k_wmi_pdev_set_param(ar, param,
2719                                                 hw->conf.power_level * 2);
2720                 if (ret)
2721                         ath10k_warn(ar, "failed to set 2g txpower %d: %d\n",
2722                                     hw->conf.power_level, ret);
2723
2724                 param = ar->wmi.pdev_param->txpower_limit5g;
2725                 ret = ath10k_wmi_pdev_set_param(ar, param,
2726                                                 hw->conf.power_level * 2);
2727                 if (ret)
2728                         ath10k_warn(ar, "failed to set 5g txpower %d: %d\n",
2729                                     hw->conf.power_level, ret);
2730         }
2731
2732         if (changed & IEEE80211_CONF_CHANGE_PS)
2733                 ath10k_config_ps(ar);
2734
2735         if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
2736                 if (conf->flags & IEEE80211_CONF_MONITOR && !ar->monitor) {
2737                         ar->monitor = true;
2738                         ret = ath10k_monitor_start(ar);
2739                         if (ret) {
2740                                 ath10k_warn(ar, "failed to start monitor (config): %d\n",
2741                                             ret);
2742                                 ar->monitor = false;
2743                         }
2744                 } else if (!(conf->flags & IEEE80211_CONF_MONITOR) &&
2745                            ar->monitor) {
2746                         ar->monitor = false;
2747                         ath10k_monitor_stop(ar);
2748                 }
2749         }
2750
2751         mutex_unlock(&ar->conf_mutex);
2752         return ret;
2753 }
2754
2755 /*
2756  * TODO:
2757  * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
2758  * because we will send mgmt frames without CCK. This requirement
2759  * for P2P_FIND/GO_NEG should be handled by checking CCK flag
2760  * in the TX packet.
2761  */
2762 static int ath10k_add_interface(struct ieee80211_hw *hw,
2763                                 struct ieee80211_vif *vif)
2764 {
2765         struct ath10k *ar = hw->priv;
2766         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2767         enum wmi_sta_powersave_param param;
2768         int ret = 0;
2769         u32 value;
2770         int bit;
2771         u32 vdev_param;
2772
2773         mutex_lock(&ar->conf_mutex);
2774
2775         memset(arvif, 0, sizeof(*arvif));
2776
2777         arvif->ar = ar;
2778         arvif->vif = vif;
2779
2780         INIT_WORK(&arvif->wep_key_work, ath10k_tx_wep_key_work);
2781         INIT_LIST_HEAD(&arvif->list);
2782
2783         if (ar->free_vdev_map == 0) {
2784                 ath10k_warn(ar, "Free vdev map is empty, no more interfaces allowed.\n");
2785                 ret = -EBUSY;
2786                 goto err;
2787         }
2788         bit = ffs(ar->free_vdev_map);
2789
2790         arvif->vdev_id = bit - 1;
2791         arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
2792
2793         if (ar->p2p)
2794                 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
2795
2796         switch (vif->type) {
2797         case NL80211_IFTYPE_UNSPECIFIED:
2798         case NL80211_IFTYPE_STATION:
2799                 arvif->vdev_type = WMI_VDEV_TYPE_STA;
2800                 if (vif->p2p)
2801                         arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
2802                 break;
2803         case NL80211_IFTYPE_ADHOC:
2804                 arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
2805                 break;
2806         case NL80211_IFTYPE_AP:
2807                 arvif->vdev_type = WMI_VDEV_TYPE_AP;
2808
2809                 if (vif->p2p)
2810                         arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
2811                 break;
2812         case NL80211_IFTYPE_MONITOR:
2813                 arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
2814                 break;
2815         default:
2816                 WARN_ON(1);
2817                 break;
2818         }
2819
2820         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d\n",
2821                    arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype);
2822
2823         ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
2824                                      arvif->vdev_subtype, vif->addr);
2825         if (ret) {
2826                 ath10k_warn(ar, "failed to create WMI vdev %i: %d\n",
2827                             arvif->vdev_id, ret);
2828                 goto err;
2829         }
2830
2831         ar->free_vdev_map &= ~(1 << arvif->vdev_id);
2832         list_add(&arvif->list, &ar->arvifs);
2833
2834         vdev_param = ar->wmi.vdev_param->def_keyid;
2835         ret = ath10k_wmi_vdev_set_param(ar, 0, vdev_param,
2836                                         arvif->def_wep_key_idx);
2837         if (ret) {
2838                 ath10k_warn(ar, "failed to set vdev %i default key id: %d\n",
2839                             arvif->vdev_id, ret);
2840                 goto err_vdev_delete;
2841         }
2842
2843         vdev_param = ar->wmi.vdev_param->tx_encap_type;
2844         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
2845                                         ATH10K_HW_TXRX_NATIVE_WIFI);
2846         /* 10.X firmware does not support this VDEV parameter. Do not warn */
2847         if (ret && ret != -EOPNOTSUPP) {
2848                 ath10k_warn(ar, "failed to set vdev %i TX encapsulation: %d\n",
2849                             arvif->vdev_id, ret);
2850                 goto err_vdev_delete;
2851         }
2852
2853         if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
2854                 ret = ath10k_peer_create(ar, arvif->vdev_id, vif->addr);
2855                 if (ret) {
2856                         ath10k_warn(ar, "failed to create vdev %i peer for AP: %d\n",
2857                                     arvif->vdev_id, ret);
2858                         goto err_vdev_delete;
2859                 }
2860
2861                 ret = ath10k_mac_set_kickout(arvif);
2862                 if (ret) {
2863                         ath10k_warn(ar, "failed to set vdev %i kickout parameters: %d\n",
2864                                     arvif->vdev_id, ret);
2865                         goto err_peer_delete;
2866                 }
2867         }
2868
2869         if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
2870                 param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
2871                 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
2872                 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
2873                                                   param, value);
2874                 if (ret) {
2875                         ath10k_warn(ar, "failed to set vdev %i RX wake policy: %d\n",
2876                                     arvif->vdev_id, ret);
2877                         goto err_peer_delete;
2878                 }
2879
2880                 param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
2881                 value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
2882                 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
2883                                                   param, value);
2884                 if (ret) {
2885                         ath10k_warn(ar, "failed to set vdev %i TX wake thresh: %d\n",
2886                                     arvif->vdev_id, ret);
2887                         goto err_peer_delete;
2888                 }
2889
2890                 param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
2891                 value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
2892                 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
2893                                                   param, value);
2894                 if (ret) {
2895                         ath10k_warn(ar, "failed to set vdev %i PSPOLL count: %d\n",
2896                                     arvif->vdev_id, ret);
2897                         goto err_peer_delete;
2898                 }
2899         }
2900
2901         ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
2902         if (ret) {
2903                 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
2904                             arvif->vdev_id, ret);
2905                 goto err_peer_delete;
2906         }
2907
2908         ret = ath10k_mac_set_frag(arvif, ar->hw->wiphy->frag_threshold);
2909         if (ret) {
2910                 ath10k_warn(ar, "failed to set frag threshold for vdev %d: %d\n",
2911                             arvif->vdev_id, ret);
2912                 goto err_peer_delete;
2913         }
2914
2915         mutex_unlock(&ar->conf_mutex);
2916         return 0;
2917
2918 err_peer_delete:
2919         if (arvif->vdev_type == WMI_VDEV_TYPE_AP)
2920                 ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
2921
2922 err_vdev_delete:
2923         ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
2924         ar->free_vdev_map |= 1 << arvif->vdev_id;
2925         list_del(&arvif->list);
2926
2927 err:
2928         mutex_unlock(&ar->conf_mutex);
2929
2930         return ret;
2931 }
2932
2933 static void ath10k_remove_interface(struct ieee80211_hw *hw,
2934                                     struct ieee80211_vif *vif)
2935 {
2936         struct ath10k *ar = hw->priv;
2937         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2938         int ret;
2939
2940         mutex_lock(&ar->conf_mutex);
2941
2942         cancel_work_sync(&arvif->wep_key_work);
2943
2944         spin_lock_bh(&ar->data_lock);
2945         if (arvif->beacon) {
2946                 dma_unmap_single(arvif->ar->dev,
2947                                  ATH10K_SKB_CB(arvif->beacon)->paddr,
2948                                  arvif->beacon->len, DMA_TO_DEVICE);
2949                 dev_kfree_skb_any(arvif->beacon);
2950                 arvif->beacon = NULL;
2951         }
2952
2953         spin_unlock_bh(&ar->data_lock);
2954
2955         ret = ath10k_spectral_vif_stop(arvif);
2956         if (ret)
2957                 ath10k_warn(ar, "failed to stop spectral for vdev %i: %d\n",
2958                             arvif->vdev_id, ret);
2959
2960         ar->free_vdev_map |= 1 << arvif->vdev_id;
2961         list_del(&arvif->list);
2962
2963         if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
2964                 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, vif->addr);
2965                 if (ret)
2966                         ath10k_warn(ar, "failed to remove peer for AP vdev %i: %d\n",
2967                                     arvif->vdev_id, ret);
2968
2969                 kfree(arvif->u.ap.noa_data);
2970         }
2971
2972         ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i delete (remove interface)\n",
2973                    arvif->vdev_id);
2974
2975         ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
2976         if (ret)
2977                 ath10k_warn(ar, "failed to delete WMI vdev %i: %d\n",
2978                             arvif->vdev_id, ret);
2979
2980         ath10k_peer_cleanup(ar, arvif->vdev_id);
2981
2982         mutex_unlock(&ar->conf_mutex);
2983 }
2984
2985 /*
2986  * FIXME: Has to be verified.
2987  */
2988 #define SUPPORTED_FILTERS                       \
2989         (FIF_PROMISC_IN_BSS |                   \
2990         FIF_ALLMULTI |                          \
2991         FIF_CONTROL |                           \
2992         FIF_PSPOLL |                            \
2993         FIF_OTHER_BSS |                         \
2994         FIF_BCN_PRBRESP_PROMISC |               \
2995         FIF_PROBE_REQ |                         \
2996         FIF_FCSFAIL)
2997
2998 static void ath10k_configure_filter(struct ieee80211_hw *hw,
2999                                     unsigned int changed_flags,
3000                                     unsigned int *total_flags,
3001                                     u64 multicast)
3002 {
3003         struct ath10k *ar = hw->priv;
3004         int ret;
3005
3006         mutex_lock(&ar->conf_mutex);
3007
3008         changed_flags &= SUPPORTED_FILTERS;
3009         *total_flags &= SUPPORTED_FILTERS;
3010         ar->filter_flags = *total_flags;
3011
3012         if (ar->filter_flags & FIF_PROMISC_IN_BSS && !ar->promisc) {
3013                 ar->promisc = true;
3014                 ret = ath10k_monitor_start(ar);
3015                 if (ret) {
3016                         ath10k_warn(ar, "failed to start monitor (promisc): %d\n",
3017                                     ret);
3018                         ar->promisc = false;
3019                 }
3020         } else if (!(ar->filter_flags & FIF_PROMISC_IN_BSS) && ar->promisc) {
3021                 ar->promisc = false;
3022                 ath10k_monitor_stop(ar);
3023         }
3024
3025         mutex_unlock(&ar->conf_mutex);
3026 }
3027
3028 static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
3029                                     struct ieee80211_vif *vif,
3030                                     struct ieee80211_bss_conf *info,
3031                                     u32 changed)
3032 {
3033         struct ath10k *ar = hw->priv;
3034         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3035         int ret = 0;
3036         u32 vdev_param, pdev_param;
3037
3038         mutex_lock(&ar->conf_mutex);
3039
3040         if (changed & BSS_CHANGED_IBSS)
3041                 ath10k_control_ibss(arvif, info, vif->addr);
3042
3043         if (changed & BSS_CHANGED_BEACON_INT) {
3044                 arvif->beacon_interval = info->beacon_int;
3045                 vdev_param = ar->wmi.vdev_param->beacon_interval;
3046                 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3047                                                 arvif->beacon_interval);
3048                 ath10k_dbg(ar, ATH10K_DBG_MAC,
3049                            "mac vdev %d beacon_interval %d\n",
3050                            arvif->vdev_id, arvif->beacon_interval);
3051
3052                 if (ret)
3053                         ath10k_warn(ar, "failed to set beacon interval for vdev %d: %i\n",
3054                                     arvif->vdev_id, ret);
3055         }
3056
3057         if (changed & BSS_CHANGED_BEACON) {
3058                 ath10k_dbg(ar, ATH10K_DBG_MAC,
3059     &n