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