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