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