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