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