ath10k: decouple suspend code
[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         INIT_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("could not 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("could not remove key for %pM\n", addr);
217         }
218
219         return first_errno;
220 }
221
222
223 /*********************/
224 /* General utilities */
225 /*********************/
226
227 static inline enum wmi_phy_mode
228 chan_to_phymode(const struct cfg80211_chan_def *chandef)
229 {
230         enum wmi_phy_mode phymode = MODE_UNKNOWN;
231
232         switch (chandef->chan->band) {
233         case IEEE80211_BAND_2GHZ:
234                 switch (chandef->width) {
235                 case NL80211_CHAN_WIDTH_20_NOHT:
236                         phymode = MODE_11G;
237                         break;
238                 case NL80211_CHAN_WIDTH_20:
239                         phymode = MODE_11NG_HT20;
240                         break;
241                 case NL80211_CHAN_WIDTH_40:
242                         phymode = MODE_11NG_HT40;
243                         break;
244                 case NL80211_CHAN_WIDTH_5:
245                 case NL80211_CHAN_WIDTH_10:
246                 case NL80211_CHAN_WIDTH_80:
247                 case NL80211_CHAN_WIDTH_80P80:
248                 case NL80211_CHAN_WIDTH_160:
249                         phymode = MODE_UNKNOWN;
250                         break;
251                 }
252                 break;
253         case IEEE80211_BAND_5GHZ:
254                 switch (chandef->width) {
255                 case NL80211_CHAN_WIDTH_20_NOHT:
256                         phymode = MODE_11A;
257                         break;
258                 case NL80211_CHAN_WIDTH_20:
259                         phymode = MODE_11NA_HT20;
260                         break;
261                 case NL80211_CHAN_WIDTH_40:
262                         phymode = MODE_11NA_HT40;
263                         break;
264                 case NL80211_CHAN_WIDTH_80:
265                         phymode = MODE_11AC_VHT80;
266                         break;
267                 case NL80211_CHAN_WIDTH_5:
268                 case NL80211_CHAN_WIDTH_10:
269                 case NL80211_CHAN_WIDTH_80P80:
270                 case NL80211_CHAN_WIDTH_160:
271                         phymode = MODE_UNKNOWN;
272                         break;
273                 }
274                 break;
275         default:
276                 break;
277         }
278
279         WARN_ON(phymode == MODE_UNKNOWN);
280         return phymode;
281 }
282
283 static u8 ath10k_parse_mpdudensity(u8 mpdudensity)
284 {
285 /*
286  * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
287  *   0 for no restriction
288  *   1 for 1/4 us
289  *   2 for 1/2 us
290  *   3 for 1 us
291  *   4 for 2 us
292  *   5 for 4 us
293  *   6 for 8 us
294  *   7 for 16 us
295  */
296         switch (mpdudensity) {
297         case 0:
298                 return 0;
299         case 1:
300         case 2:
301         case 3:
302         /* Our lower layer calculations limit our precision to
303            1 microsecond */
304                 return 1;
305         case 4:
306                 return 2;
307         case 5:
308                 return 4;
309         case 6:
310                 return 8;
311         case 7:
312                 return 16;
313         default:
314                 return 0;
315         }
316 }
317
318 static int ath10k_peer_create(struct ath10k *ar, u32 vdev_id, const u8 *addr)
319 {
320         int ret;
321
322         lockdep_assert_held(&ar->conf_mutex);
323
324         ret = ath10k_wmi_peer_create(ar, vdev_id, addr);
325         if (ret)
326                 return ret;
327
328         ret = ath10k_wait_for_peer_created(ar, vdev_id, addr);
329         if (ret)
330                 return ret;
331
332         return 0;
333 }
334
335 static int ath10k_peer_delete(struct ath10k *ar, u32 vdev_id, const u8 *addr)
336 {
337         int ret;
338
339         lockdep_assert_held(&ar->conf_mutex);
340
341         ret = ath10k_wmi_peer_delete(ar, vdev_id, addr);
342         if (ret)
343                 return ret;
344
345         ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
346         if (ret)
347                 return ret;
348
349         return 0;
350 }
351
352 static void ath10k_peer_cleanup(struct ath10k *ar, u32 vdev_id)
353 {
354         struct ath10k_peer *peer, *tmp;
355
356         lockdep_assert_held(&ar->conf_mutex);
357
358         spin_lock_bh(&ar->data_lock);
359         list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
360                 if (peer->vdev_id != vdev_id)
361                         continue;
362
363                 ath10k_warn("removing stale peer %pM from vdev_id %d\n",
364                             peer->addr, vdev_id);
365
366                 list_del(&peer->list);
367                 kfree(peer);
368         }
369         spin_unlock_bh(&ar->data_lock);
370 }
371
372 /************************/
373 /* Interface management */
374 /************************/
375
376 static inline int ath10k_vdev_setup_sync(struct ath10k *ar)
377 {
378         int ret;
379
380         lockdep_assert_held(&ar->conf_mutex);
381
382         ret = wait_for_completion_timeout(&ar->vdev_setup_done,
383                                           ATH10K_VDEV_SETUP_TIMEOUT_HZ);
384         if (ret == 0)
385                 return -ETIMEDOUT;
386
387         return 0;
388 }
389
390 static int ath10k_vdev_start(struct ath10k_vif *arvif)
391 {
392         struct ath10k *ar = arvif->ar;
393         struct ieee80211_conf *conf = &ar->hw->conf;
394         struct ieee80211_channel *channel = conf->chandef.chan;
395         struct wmi_vdev_start_request_arg arg = {};
396         int ret = 0;
397
398         lockdep_assert_held(&ar->conf_mutex);
399
400         INIT_COMPLETION(ar->vdev_setup_done);
401
402         arg.vdev_id = arvif->vdev_id;
403         arg.dtim_period = arvif->dtim_period;
404         arg.bcn_intval = arvif->beacon_interval;
405
406         arg.channel.freq = channel->center_freq;
407
408         arg.channel.band_center_freq1 = conf->chandef.center_freq1;
409
410         arg.channel.mode = chan_to_phymode(&conf->chandef);
411
412         arg.channel.min_power = channel->max_power * 3;
413         arg.channel.max_power = channel->max_power * 4;
414         arg.channel.max_reg_power = channel->max_reg_power * 4;
415         arg.channel.max_antenna_gain = channel->max_antenna_gain;
416
417         if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
418                 arg.ssid = arvif->u.ap.ssid;
419                 arg.ssid_len = arvif->u.ap.ssid_len;
420                 arg.hidden_ssid = arvif->u.ap.hidden_ssid;
421         } else if (arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
422                 arg.ssid = arvif->vif->bss_conf.ssid;
423                 arg.ssid_len = arvif->vif->bss_conf.ssid_len;
424         }
425
426         ret = ath10k_wmi_vdev_start(ar, &arg);
427         if (ret) {
428                 ath10k_warn("WMI vdev start failed: ret %d\n", ret);
429                 return ret;
430         }
431
432         ret = ath10k_vdev_setup_sync(ar);
433         if (ret) {
434                 ath10k_warn("vdev setup failed %d\n", ret);
435                 return ret;
436         }
437
438         return ret;
439 }
440
441 static int ath10k_vdev_stop(struct ath10k_vif *arvif)
442 {
443         struct ath10k *ar = arvif->ar;
444         int ret;
445
446         lockdep_assert_held(&ar->conf_mutex);
447
448         INIT_COMPLETION(ar->vdev_setup_done);
449
450         ret = ath10k_wmi_vdev_stop(ar, arvif->vdev_id);
451         if (ret) {
452                 ath10k_warn("WMI vdev stop failed: ret %d\n", ret);
453                 return ret;
454         }
455
456         ret = ath10k_vdev_setup_sync(ar);
457         if (ret) {
458                 ath10k_warn("vdev setup failed %d\n", ret);
459                 return ret;
460         }
461
462         return ret;
463 }
464
465 static int ath10k_monitor_start(struct ath10k *ar, int vdev_id)
466 {
467         struct ieee80211_channel *channel = ar->hw->conf.chandef.chan;
468         struct wmi_vdev_start_request_arg arg = {};
469         enum nl80211_channel_type type;
470         int ret = 0;
471
472         lockdep_assert_held(&ar->conf_mutex);
473
474         type = cfg80211_get_chandef_type(&ar->hw->conf.chandef);
475
476         arg.vdev_id = vdev_id;
477         arg.channel.freq = channel->center_freq;
478         arg.channel.band_center_freq1 = ar->hw->conf.chandef.center_freq1;
479
480         /* TODO setup this dynamically, what in case we
481            don't have any vifs? */
482         arg.channel.mode = chan_to_phymode(&ar->hw->conf.chandef);
483
484         arg.channel.min_power = channel->max_power * 3;
485         arg.channel.max_power = channel->max_power * 4;
486         arg.channel.max_reg_power = channel->max_reg_power * 4;
487         arg.channel.max_antenna_gain = channel->max_antenna_gain;
488
489         ret = ath10k_wmi_vdev_start(ar, &arg);
490         if (ret) {
491                 ath10k_warn("Monitor vdev start failed: ret %d\n", ret);
492                 return ret;
493         }
494
495         ret = ath10k_vdev_setup_sync(ar);
496         if (ret) {
497                 ath10k_warn("Monitor vdev setup failed %d\n", ret);
498                 return ret;
499         }
500
501         ret = ath10k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
502         if (ret) {
503                 ath10k_warn("Monitor vdev up failed: %d\n", ret);
504                 goto vdev_stop;
505         }
506
507         ar->monitor_vdev_id = vdev_id;
508         ar->monitor_enabled = true;
509
510         return 0;
511
512 vdev_stop:
513         ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
514         if (ret)
515                 ath10k_warn("Monitor vdev stop failed: %d\n", ret);
516
517         return ret;
518 }
519
520 static int ath10k_monitor_stop(struct ath10k *ar)
521 {
522         int ret = 0;
523
524         lockdep_assert_held(&ar->conf_mutex);
525
526         /* For some reasons, ath10k_wmi_vdev_down() here couse
527          * often ath10k_wmi_vdev_stop() to fail. Next we could
528          * not run monitor vdev and driver reload
529          * required. Don't see such problems we skip
530          * ath10k_wmi_vdev_down() here.
531          */
532
533         ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
534         if (ret)
535                 ath10k_warn("Monitor vdev stop failed: %d\n", ret);
536
537         ret = ath10k_vdev_setup_sync(ar);
538         if (ret)
539                 ath10k_warn("Monitor_down sync failed: %d\n", ret);
540
541         ar->monitor_enabled = false;
542         return ret;
543 }
544
545 static int ath10k_monitor_create(struct ath10k *ar)
546 {
547         int bit, ret = 0;
548
549         lockdep_assert_held(&ar->conf_mutex);
550
551         if (ar->monitor_present) {
552                 ath10k_warn("Monitor mode already enabled\n");
553                 return 0;
554         }
555
556         bit = ffs(ar->free_vdev_map);
557         if (bit == 0) {
558                 ath10k_warn("No free VDEV slots\n");
559                 return -ENOMEM;
560         }
561
562         ar->monitor_vdev_id = bit - 1;
563         ar->free_vdev_map &= ~(1 << ar->monitor_vdev_id);
564
565         ret = ath10k_wmi_vdev_create(ar, ar->monitor_vdev_id,
566                                      WMI_VDEV_TYPE_MONITOR,
567                                      0, ar->mac_addr);
568         if (ret) {
569                 ath10k_warn("WMI vdev monitor create failed: ret %d\n", ret);
570                 goto vdev_fail;
571         }
572
573         ath10k_dbg(ATH10K_DBG_MAC, "Monitor interface created, vdev id: %d\n",
574                    ar->monitor_vdev_id);
575
576         ar->monitor_present = true;
577         return 0;
578
579 vdev_fail:
580         /*
581          * Restore the ID to the global map.
582          */
583         ar->free_vdev_map |= 1 << (ar->monitor_vdev_id);
584         return ret;
585 }
586
587 static int ath10k_monitor_destroy(struct ath10k *ar)
588 {
589         int ret = 0;
590
591         lockdep_assert_held(&ar->conf_mutex);
592
593         if (!ar->monitor_present)
594                 return 0;
595
596         ret = ath10k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
597         if (ret) {
598                 ath10k_warn("WMI vdev monitor delete failed: %d\n", ret);
599                 return ret;
600         }
601
602         ar->free_vdev_map |= 1 << (ar->monitor_vdev_id);
603         ar->monitor_present = false;
604
605         ath10k_dbg(ATH10K_DBG_MAC, "Monitor interface destroyed, vdev id: %d\n",
606                    ar->monitor_vdev_id);
607         return ret;
608 }
609
610 static void ath10k_control_beaconing(struct ath10k_vif *arvif,
611                                 struct ieee80211_bss_conf *info)
612 {
613         int ret = 0;
614
615         lockdep_assert_held(&arvif->ar->conf_mutex);
616
617         if (!info->enable_beacon) {
618                 ath10k_vdev_stop(arvif);
619                 return;
620         }
621
622         arvif->tx_seq_no = 0x1000;
623
624         ret = ath10k_vdev_start(arvif);
625         if (ret)
626                 return;
627
628         ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, 0, info->bssid);
629         if (ret) {
630                 ath10k_warn("Failed to bring up VDEV: %d\n",
631                             arvif->vdev_id);
632                 return;
633         }
634         ath10k_dbg(ATH10K_DBG_MAC, "VDEV: %d up\n", arvif->vdev_id);
635 }
636
637 static void ath10k_control_ibss(struct ath10k_vif *arvif,
638                                 struct ieee80211_bss_conf *info,
639                                 const u8 self_peer[ETH_ALEN])
640 {
641         int ret = 0;
642
643         lockdep_assert_held(&arvif->ar->conf_mutex);
644
645         if (!info->ibss_joined) {
646                 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, self_peer);
647                 if (ret)
648                         ath10k_warn("Failed to delete IBSS self peer:%pM for VDEV:%d ret:%d\n",
649                                     self_peer, arvif->vdev_id, ret);
650
651                 if (is_zero_ether_addr(arvif->u.ibss.bssid))
652                         return;
653
654                 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id,
655                                          arvif->u.ibss.bssid);
656                 if (ret) {
657                         ath10k_warn("Failed to delete IBSS BSSID peer:%pM for VDEV:%d ret:%d\n",
658                                     arvif->u.ibss.bssid, arvif->vdev_id, ret);
659                         return;
660                 }
661
662                 memset(arvif->u.ibss.bssid, 0, ETH_ALEN);
663
664                 return;
665         }
666
667         ret = ath10k_peer_create(arvif->ar, arvif->vdev_id, self_peer);
668         if (ret) {
669                 ath10k_warn("Failed to create IBSS self peer:%pM for VDEV:%d ret:%d\n",
670                             self_peer, arvif->vdev_id, ret);
671                 return;
672         }
673
674         ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id,
675                                         WMI_VDEV_PARAM_ATIM_WINDOW,
676                                         ATH10K_DEFAULT_ATIM);
677         if (ret)
678                 ath10k_warn("Failed to set IBSS ATIM for VDEV:%d ret:%d\n",
679                             arvif->vdev_id, ret);
680 }
681
682 /*
683  * Review this when mac80211 gains per-interface powersave support.
684  */
685 static void ath10k_ps_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
686 {
687         struct ath10k_generic_iter *ar_iter = data;
688         struct ieee80211_conf *conf = &ar_iter->ar->hw->conf;
689         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
690         enum wmi_sta_powersave_param param;
691         enum wmi_sta_ps_mode psmode;
692         int ret;
693
694         lockdep_assert_held(&arvif->ar->conf_mutex);
695
696         if (vif->type != NL80211_IFTYPE_STATION)
697                 return;
698
699         if (conf->flags & IEEE80211_CONF_PS) {
700                 psmode = WMI_STA_PS_MODE_ENABLED;
701                 param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
702
703                 ret = ath10k_wmi_set_sta_ps_param(ar_iter->ar,
704                                                   arvif->vdev_id,
705                                                   param,
706                                                   conf->dynamic_ps_timeout);
707                 if (ret) {
708                         ath10k_warn("Failed to set inactivity time for VDEV: %d\n",
709                                     arvif->vdev_id);
710                         return;
711                 }
712
713                 ar_iter->ret = ret;
714         } else {
715                 psmode = WMI_STA_PS_MODE_DISABLED;
716         }
717
718         ar_iter->ret = ath10k_wmi_set_psmode(ar_iter->ar, arvif->vdev_id,
719                                              psmode);
720         if (ar_iter->ret)
721                 ath10k_warn("Failed to set PS Mode: %d for VDEV: %d\n",
722                             psmode, arvif->vdev_id);
723         else
724                 ath10k_dbg(ATH10K_DBG_MAC, "Set PS Mode: %d for VDEV: %d\n",
725                            psmode, arvif->vdev_id);
726 }
727
728 /**********************/
729 /* Station management */
730 /**********************/
731
732 static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
733                                       struct ath10k_vif *arvif,
734                                       struct ieee80211_sta *sta,
735                                       struct ieee80211_bss_conf *bss_conf,
736                                       struct wmi_peer_assoc_complete_arg *arg)
737 {
738         lockdep_assert_held(&ar->conf_mutex);
739
740         memcpy(arg->addr, sta->addr, ETH_ALEN);
741         arg->vdev_id = arvif->vdev_id;
742         arg->peer_aid = sta->aid;
743         arg->peer_flags |= WMI_PEER_AUTH;
744
745         if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
746                 /*
747                  * Seems FW have problems with Power Save in STA
748                  * mode when we setup this parameter to high (eg. 5).
749                  * Often we see that FW don't send NULL (with clean P flags)
750                  * frame even there is info about buffered frames in beacons.
751                  * Sometimes we have to wait more than 10 seconds before FW
752                  * will wakeup. Often sending one ping from AP to our device
753                  * just fail (more than 50%).
754                  *
755                  * Seems setting this FW parameter to 1 couse FW
756                  * will check every beacon and will wakup immediately
757                  * after detection buffered data.
758                  */
759                 arg->peer_listen_intval = 1;
760         else
761                 arg->peer_listen_intval = ar->hw->conf.listen_interval;
762
763         arg->peer_num_spatial_streams = 1;
764
765         /*
766          * The assoc capabilities are available only in managed mode.
767          */
768         if (arvif->vdev_type == WMI_VDEV_TYPE_STA && bss_conf)
769                 arg->peer_caps = bss_conf->assoc_capability;
770 }
771
772 static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
773                                        struct ath10k_vif *arvif,
774                                        struct wmi_peer_assoc_complete_arg *arg)
775 {
776         struct ieee80211_vif *vif = arvif->vif;
777         struct ieee80211_bss_conf *info = &vif->bss_conf;
778         struct cfg80211_bss *bss;
779         const u8 *rsnie = NULL;
780         const u8 *wpaie = NULL;
781
782         lockdep_assert_held(&ar->conf_mutex);
783
784         bss = cfg80211_get_bss(ar->hw->wiphy, ar->hw->conf.chandef.chan,
785                                info->bssid, NULL, 0, 0, 0);
786         if (bss) {
787                 const struct cfg80211_bss_ies *ies;
788
789                 rcu_read_lock();
790                 rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
791
792                 ies = rcu_dereference(bss->ies);
793
794                 wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
795                                 WLAN_OUI_TYPE_MICROSOFT_WPA,
796                                 ies->data,
797                                 ies->len);
798                 rcu_read_unlock();
799                 cfg80211_put_bss(ar->hw->wiphy, bss);
800         }
801
802         /* FIXME: base on RSN IE/WPA IE is a correct idea? */
803         if (rsnie || wpaie) {
804                 ath10k_dbg(ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
805                 arg->peer_flags |= WMI_PEER_NEED_PTK_4_WAY;
806         }
807
808         if (wpaie) {
809                 ath10k_dbg(ATH10K_DBG_WMI, "%s: wpa ie found\n", __func__);
810                 arg->peer_flags |= WMI_PEER_NEED_GTK_2_WAY;
811         }
812 }
813
814 static void ath10k_peer_assoc_h_rates(struct ath10k *ar,
815                                       struct ieee80211_sta *sta,
816                                       struct wmi_peer_assoc_complete_arg *arg)
817 {
818         struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
819         const struct ieee80211_supported_band *sband;
820         const struct ieee80211_rate *rates;
821         u32 ratemask;
822         int i;
823
824         lockdep_assert_held(&ar->conf_mutex);
825
826         sband = ar->hw->wiphy->bands[ar->hw->conf.chandef.chan->band];
827         ratemask = sta->supp_rates[ar->hw->conf.chandef.chan->band];
828         rates = sband->bitrates;
829
830         rateset->num_rates = 0;
831
832         for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
833                 if (!(ratemask & 1))
834                         continue;
835
836                 rateset->rates[rateset->num_rates] = rates->hw_value;
837                 rateset->num_rates++;
838         }
839 }
840
841 static void ath10k_peer_assoc_h_ht(struct ath10k *ar,
842                                    struct ieee80211_sta *sta,
843                                    struct wmi_peer_assoc_complete_arg *arg)
844 {
845         const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
846         int smps;
847         int i, n;
848
849         lockdep_assert_held(&ar->conf_mutex);
850
851         if (!ht_cap->ht_supported)
852                 return;
853
854         arg->peer_flags |= WMI_PEER_HT;
855         arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
856                                     ht_cap->ampdu_factor)) - 1;
857
858         arg->peer_mpdu_density =
859                 ath10k_parse_mpdudensity(ht_cap->ampdu_density);
860
861         arg->peer_ht_caps = ht_cap->cap;
862         arg->peer_rate_caps |= WMI_RC_HT_FLAG;
863
864         if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
865                 arg->peer_flags |= WMI_PEER_LDPC;
866
867         if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
868                 arg->peer_flags |= WMI_PEER_40MHZ;
869                 arg->peer_rate_caps |= WMI_RC_CW40_FLAG;
870         }
871
872         if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
873                 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
874
875         if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
876                 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
877
878         if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
879                 arg->peer_rate_caps |= WMI_RC_TX_STBC_FLAG;
880                 arg->peer_flags |= WMI_PEER_STBC;
881         }
882
883         if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
884                 u32 stbc;
885                 stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
886                 stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
887                 stbc = stbc << WMI_RC_RX_STBC_FLAG_S;
888                 arg->peer_rate_caps |= stbc;
889                 arg->peer_flags |= WMI_PEER_STBC;
890         }
891
892         smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
893         smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
894
895         if (smps == WLAN_HT_CAP_SM_PS_STATIC) {
896                 arg->peer_flags |= WMI_PEER_SPATIAL_MUX;
897                 arg->peer_flags |= WMI_PEER_STATIC_MIMOPS;
898         } else if (smps == WLAN_HT_CAP_SM_PS_DYNAMIC) {
899                 arg->peer_flags |= WMI_PEER_SPATIAL_MUX;
900                 arg->peer_flags |= WMI_PEER_DYN_MIMOPS;
901         }
902
903         if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
904                 arg->peer_rate_caps |= WMI_RC_TS_FLAG;
905         else if (ht_cap->mcs.rx_mask[1])
906                 arg->peer_rate_caps |= WMI_RC_DS_FLAG;
907
908         for (i = 0, n = 0; i < IEEE80211_HT_MCS_MASK_LEN*8; i++)
909                 if (ht_cap->mcs.rx_mask[i/8] & (1 << i%8))
910                         arg->peer_ht_rates.rates[n++] = i;
911
912         arg->peer_ht_rates.num_rates = n;
913         arg->peer_num_spatial_streams = max((n+7) / 8, 1);
914
915         ath10k_dbg(ATH10K_DBG_MAC, "mcs cnt %d nss %d\n",
916                    arg->peer_ht_rates.num_rates,
917                    arg->peer_num_spatial_streams);
918 }
919
920 static void ath10k_peer_assoc_h_qos_ap(struct ath10k *ar,
921                                        struct ath10k_vif *arvif,
922                                        struct ieee80211_sta *sta,
923                                        struct ieee80211_bss_conf *bss_conf,
924                                        struct wmi_peer_assoc_complete_arg *arg)
925 {
926         u32 uapsd = 0;
927         u32 max_sp = 0;
928
929         lockdep_assert_held(&ar->conf_mutex);
930
931         if (sta->wme)
932                 arg->peer_flags |= WMI_PEER_QOS;
933
934         if (sta->wme && sta->uapsd_queues) {
935                 ath10k_dbg(ATH10K_DBG_MAC, "uapsd_queues: 0x%X, max_sp: %d\n",
936                            sta->uapsd_queues, sta->max_sp);
937
938                 arg->peer_flags |= WMI_PEER_APSD;
939                 arg->peer_flags |= WMI_RC_UAPSD_FLAG;
940
941                 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
942                         uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
943                                  WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
944                 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
945                         uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
946                                  WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
947                 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
948                         uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
949                                  WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
950                 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
951                         uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
952                                  WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
953
954
955                 if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
956                         max_sp = sta->max_sp;
957
958                 ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
959                                            sta->addr,
960                                            WMI_AP_PS_PEER_PARAM_UAPSD,
961                                            uapsd);
962
963                 ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
964                                            sta->addr,
965                                            WMI_AP_PS_PEER_PARAM_MAX_SP,
966                                            max_sp);
967
968                 /* TODO setup this based on STA listen interval and
969                    beacon interval. Currently we don't know
970                    sta->listen_interval - mac80211 patch required.
971                    Currently use 10 seconds */
972                 ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
973                                            sta->addr,
974                                            WMI_AP_PS_PEER_PARAM_AGEOUT_TIME,
975                                            10);
976         }
977 }
978
979 static void ath10k_peer_assoc_h_qos_sta(struct ath10k *ar,
980                                         struct ath10k_vif *arvif,
981                                         struct ieee80211_sta *sta,
982                                         struct ieee80211_bss_conf *bss_conf,
983                                         struct wmi_peer_assoc_complete_arg *arg)
984 {
985         if (bss_conf->qos)
986                 arg->peer_flags |= WMI_PEER_QOS;
987 }
988
989 static void ath10k_peer_assoc_h_vht(struct ath10k *ar,
990                                     struct ieee80211_sta *sta,
991                                     struct wmi_peer_assoc_complete_arg *arg)
992 {
993         const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
994
995         if (!vht_cap->vht_supported)
996                 return;
997
998         arg->peer_flags |= WMI_PEER_VHT;
999
1000         arg->peer_vht_caps = vht_cap->cap;
1001
1002         if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1003                 arg->peer_flags |= WMI_PEER_80MHZ;
1004
1005         arg->peer_vht_rates.rx_max_rate =
1006                 __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
1007         arg->peer_vht_rates.rx_mcs_set =
1008                 __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
1009         arg->peer_vht_rates.tx_max_rate =
1010                 __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
1011         arg->peer_vht_rates.tx_mcs_set =
1012                 __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
1013
1014         ath10k_dbg(ATH10K_DBG_MAC, "mac vht peer\n");
1015 }
1016
1017 static void ath10k_peer_assoc_h_qos(struct ath10k *ar,
1018                                     struct ath10k_vif *arvif,
1019                                     struct ieee80211_sta *sta,
1020                                     struct ieee80211_bss_conf *bss_conf,
1021                                     struct wmi_peer_assoc_complete_arg *arg)
1022 {
1023         switch (arvif->vdev_type) {
1024         case WMI_VDEV_TYPE_AP:
1025                 ath10k_peer_assoc_h_qos_ap(ar, arvif, sta, bss_conf, arg);
1026                 break;
1027         case WMI_VDEV_TYPE_STA:
1028                 ath10k_peer_assoc_h_qos_sta(ar, arvif, sta, bss_conf, arg);
1029                 break;
1030         default:
1031                 break;
1032         }
1033 }
1034
1035 static void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
1036                                         struct ath10k_vif *arvif,
1037                                         struct ieee80211_sta *sta,
1038                                         struct wmi_peer_assoc_complete_arg *arg)
1039 {
1040         enum wmi_phy_mode phymode = MODE_UNKNOWN;
1041
1042         /* FIXME: add VHT */
1043
1044         switch (ar->hw->conf.chandef.chan->band) {
1045         case IEEE80211_BAND_2GHZ:
1046                 if (sta->ht_cap.ht_supported) {
1047                         if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1048                                 phymode = MODE_11NG_HT40;
1049                         else
1050                                 phymode = MODE_11NG_HT20;
1051                 } else {
1052                         phymode = MODE_11G;
1053                 }
1054
1055                 break;
1056         case IEEE80211_BAND_5GHZ:
1057                 if (sta->ht_cap.ht_supported) {
1058                         if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1059                                 phymode = MODE_11NA_HT40;
1060                         else
1061                                 phymode = MODE_11NA_HT20;
1062                 } else {
1063                         phymode = MODE_11A;
1064                 }
1065
1066                 break;
1067         default:
1068                 break;
1069         }
1070
1071         arg->peer_phymode = phymode;
1072         WARN_ON(phymode == MODE_UNKNOWN);
1073 }
1074
1075 static int ath10k_peer_assoc(struct ath10k *ar,
1076                              struct ath10k_vif *arvif,
1077                              struct ieee80211_sta *sta,
1078                              struct ieee80211_bss_conf *bss_conf)
1079 {
1080         struct wmi_peer_assoc_complete_arg arg;
1081
1082         lockdep_assert_held(&ar->conf_mutex);
1083
1084         memset(&arg, 0, sizeof(struct wmi_peer_assoc_complete_arg));
1085
1086         ath10k_peer_assoc_h_basic(ar, arvif, sta, bss_conf, &arg);
1087         ath10k_peer_assoc_h_crypto(ar, arvif, &arg);
1088         ath10k_peer_assoc_h_rates(ar, sta, &arg);
1089         ath10k_peer_assoc_h_ht(ar, sta, &arg);
1090         ath10k_peer_assoc_h_vht(ar, sta, &arg);
1091         ath10k_peer_assoc_h_qos(ar, arvif, sta, bss_conf, &arg);
1092         ath10k_peer_assoc_h_phymode(ar, arvif, sta, &arg);
1093
1094         return ath10k_wmi_peer_assoc(ar, &arg);
1095 }
1096
1097 /* can be called only in mac80211 callbacks due to `key_count` usage */
1098 static void ath10k_bss_assoc(struct ieee80211_hw *hw,
1099                              struct ieee80211_vif *vif,
1100                              struct ieee80211_bss_conf *bss_conf)
1101 {
1102         struct ath10k *ar = hw->priv;
1103         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1104         struct ieee80211_sta *ap_sta;
1105         int ret;
1106
1107         lockdep_assert_held(&ar->conf_mutex);
1108
1109         rcu_read_lock();
1110
1111         ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
1112         if (!ap_sta) {
1113                 ath10k_warn("Failed to find station entry for %pM\n",
1114                             bss_conf->bssid);
1115                 rcu_read_unlock();
1116                 return;
1117         }
1118
1119         ret = ath10k_peer_assoc(ar, arvif, ap_sta, bss_conf);
1120         if (ret) {
1121                 ath10k_warn("Peer assoc failed for %pM\n", bss_conf->bssid);
1122                 rcu_read_unlock();
1123                 return;
1124         }
1125
1126         rcu_read_unlock();
1127
1128         ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, bss_conf->aid,
1129                                  bss_conf->bssid);
1130         if (ret)
1131                 ath10k_warn("VDEV: %d up failed: ret %d\n",
1132                             arvif->vdev_id, ret);
1133         else
1134                 ath10k_dbg(ATH10K_DBG_MAC,
1135                            "VDEV: %d associated, BSSID: %pM, AID: %d\n",
1136                            arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
1137 }
1138
1139 /*
1140  * FIXME: flush TIDs
1141  */
1142 static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
1143                                 struct ieee80211_vif *vif)
1144 {
1145         struct ath10k *ar = hw->priv;
1146         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1147         int ret;
1148
1149         lockdep_assert_held(&ar->conf_mutex);
1150
1151         /*
1152          * For some reason, calling VDEV-DOWN before VDEV-STOP
1153          * makes the FW to send frames via HTT after disassociation.
1154          * No idea why this happens, even though VDEV-DOWN is supposed
1155          * to be analogous to link down, so just stop the VDEV.
1156          */
1157         ret = ath10k_vdev_stop(arvif);
1158         if (!ret)
1159                 ath10k_dbg(ATH10K_DBG_MAC, "VDEV: %d stopped\n",
1160                            arvif->vdev_id);
1161
1162         /*
1163          * If we don't call VDEV-DOWN after VDEV-STOP FW will remain active and
1164          * report beacons from previously associated network through HTT.
1165          * This in turn would spam mac80211 WARN_ON if we bring down all
1166          * interfaces as it expects there is no rx when no interface is
1167          * running.
1168          */
1169         ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
1170         if (ret)
1171                 ath10k_dbg(ATH10K_DBG_MAC, "VDEV: %d ath10k_wmi_vdev_down failed (%d)\n",
1172                            arvif->vdev_id, ret);
1173
1174         ath10k_wmi_flush_tx(ar);
1175
1176         arvif->def_wep_key_index = 0;
1177 }
1178
1179 static int ath10k_station_assoc(struct ath10k *ar, struct ath10k_vif *arvif,
1180                                 struct ieee80211_sta *sta)
1181 {
1182         int ret = 0;
1183
1184         lockdep_assert_held(&ar->conf_mutex);
1185
1186         ret = ath10k_peer_assoc(ar, arvif, sta, NULL);
1187         if (ret) {
1188                 ath10k_warn("WMI peer assoc failed for %pM\n", sta->addr);
1189                 return ret;
1190         }
1191
1192         ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
1193         if (ret) {
1194                 ath10k_warn("could not install peer wep keys (%d)\n", ret);
1195                 return ret;
1196         }
1197
1198         return ret;
1199 }
1200
1201 static int ath10k_station_disassoc(struct ath10k *ar, struct ath10k_vif *arvif,
1202                                    struct ieee80211_sta *sta)
1203 {
1204         int ret = 0;
1205
1206         lockdep_assert_held(&ar->conf_mutex);
1207
1208         ret = ath10k_clear_peer_keys(arvif, sta->addr);
1209         if (ret) {
1210                 ath10k_warn("could not clear all peer wep keys (%d)\n", ret);
1211                 return ret;
1212         }
1213
1214         return ret;
1215 }
1216
1217 /**************/
1218 /* Regulatory */
1219 /**************/
1220
1221 static int ath10k_update_channel_list(struct ath10k *ar)
1222 {
1223         struct ieee80211_hw *hw = ar->hw;
1224         struct ieee80211_supported_band **bands;
1225         enum ieee80211_band band;
1226         struct ieee80211_channel *channel;
1227         struct wmi_scan_chan_list_arg arg = {0};
1228         struct wmi_channel_arg *ch;
1229         bool passive;
1230         int len;
1231         int ret;
1232         int i;
1233
1234         lockdep_assert_held(&ar->conf_mutex);
1235
1236         bands = hw->wiphy->bands;
1237         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1238                 if (!bands[band])
1239                         continue;
1240
1241                 for (i = 0; i < bands[band]->n_channels; i++) {
1242                         if (bands[band]->channels[i].flags &
1243                             IEEE80211_CHAN_DISABLED)
1244                                 continue;
1245
1246                         arg.n_channels++;
1247                 }
1248         }
1249
1250         len = sizeof(struct wmi_channel_arg) * arg.n_channels;
1251         arg.channels = kzalloc(len, GFP_KERNEL);
1252         if (!arg.channels)
1253                 return -ENOMEM;
1254
1255         ch = arg.channels;
1256         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1257                 if (!bands[band])
1258                         continue;
1259
1260                 for (i = 0; i < bands[band]->n_channels; i++) {
1261                         channel = &bands[band]->channels[i];
1262
1263                         if (channel->flags & IEEE80211_CHAN_DISABLED)
1264                                 continue;
1265
1266                         ch->allow_ht   = true;
1267
1268                         /* FIXME: when should we really allow VHT? */
1269                         ch->allow_vht = true;
1270
1271                         ch->allow_ibss =
1272                                 !(channel->flags & IEEE80211_CHAN_NO_IBSS);
1273
1274                         ch->ht40plus =
1275                                 !(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
1276
1277                         passive = channel->flags & IEEE80211_CHAN_PASSIVE_SCAN;
1278                         ch->passive = passive;
1279
1280                         ch->freq = channel->center_freq;
1281                         ch->min_power = channel->max_power * 3;
1282                         ch->max_power = channel->max_power * 4;
1283                         ch->max_reg_power = channel->max_reg_power * 4;
1284                         ch->max_antenna_gain = channel->max_antenna_gain;
1285                         ch->reg_class_id = 0; /* FIXME */
1286
1287                         /* FIXME: why use only legacy modes, why not any
1288                          * HT/VHT modes? Would that even make any
1289                          * difference? */
1290                         if (channel->band == IEEE80211_BAND_2GHZ)
1291                                 ch->mode = MODE_11G;
1292                         else
1293                                 ch->mode = MODE_11A;
1294
1295                         if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
1296                                 continue;
1297
1298                         ath10k_dbg(ATH10K_DBG_WMI,
1299                                    "%s: [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
1300                                    __func__, ch - arg.channels, arg.n_channels,
1301                                    ch->freq, ch->max_power, ch->max_reg_power,
1302                                    ch->max_antenna_gain, ch->mode);
1303
1304                         ch++;
1305                 }
1306         }
1307
1308         ret = ath10k_wmi_scan_chan_list(ar, &arg);
1309         kfree(arg.channels);
1310
1311         return ret;
1312 }
1313
1314 static void ath10k_regd_update(struct ath10k *ar)
1315 {
1316         struct reg_dmn_pair_mapping *regpair;
1317         int ret;
1318
1319         lockdep_assert_held(&ar->conf_mutex);
1320
1321         ret = ath10k_update_channel_list(ar);
1322         if (ret)
1323                 ath10k_warn("could not update channel list (%d)\n", ret);
1324
1325         regpair = ar->ath_common.regulatory.regpair;
1326
1327         /* Target allows setting up per-band regdomain but ath_common provides
1328          * a combined one only */
1329         ret = ath10k_wmi_pdev_set_regdomain(ar,
1330                                             regpair->regDmnEnum,
1331                                             regpair->regDmnEnum, /* 2ghz */
1332                                             regpair->regDmnEnum, /* 5ghz */
1333                                             regpair->reg_2ghz_ctl,
1334                                             regpair->reg_5ghz_ctl);
1335         if (ret)
1336                 ath10k_warn("could not set pdev regdomain (%d)\n", ret);
1337 }
1338
1339 static void ath10k_reg_notifier(struct wiphy *wiphy,
1340                                 struct regulatory_request *request)
1341 {
1342         struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
1343         struct ath10k *ar = hw->priv;
1344
1345         ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
1346
1347         mutex_lock(&ar->conf_mutex);
1348         if (ar->state == ATH10K_STATE_ON)
1349                 ath10k_regd_update(ar);
1350         mutex_unlock(&ar->conf_mutex);
1351 }
1352
1353 /***************/
1354 /* TX handlers */
1355 /***************/
1356
1357 /*
1358  * Frames sent to the FW have to be in "Native Wifi" format.
1359  * Strip the QoS field from the 802.11 header.
1360  */
1361 static void ath10k_tx_h_qos_workaround(struct ieee80211_hw *hw,
1362                                        struct ieee80211_tx_control *control,
1363                                        struct sk_buff *skb)
1364 {
1365         struct ieee80211_hdr *hdr = (void *)skb->data;
1366         u8 *qos_ctl;
1367
1368         if (!ieee80211_is_data_qos(hdr->frame_control))
1369                 return;
1370
1371         qos_ctl = ieee80211_get_qos_ctl(hdr);
1372         memmove(qos_ctl, qos_ctl + IEEE80211_QOS_CTL_LEN,
1373                 skb->len - ieee80211_hdrlen(hdr->frame_control));
1374         skb_trim(skb, skb->len - IEEE80211_QOS_CTL_LEN);
1375 }
1376
1377 static void ath10k_tx_h_update_wep_key(struct sk_buff *skb)
1378 {
1379         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1380         struct ieee80211_vif *vif = info->control.vif;
1381         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1382         struct ath10k *ar = arvif->ar;
1383         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1384         struct ieee80211_key_conf *key = info->control.hw_key;
1385         int ret;
1386
1387         /* TODO AP mode should be implemented */
1388         if (vif->type != NL80211_IFTYPE_STATION)
1389                 return;
1390
1391         if (!ieee80211_has_protected(hdr->frame_control))
1392                 return;
1393
1394         if (!key)
1395                 return;
1396
1397         if (key->cipher != WLAN_CIPHER_SUITE_WEP40 &&
1398             key->cipher != WLAN_CIPHER_SUITE_WEP104)
1399                 return;
1400
1401         if (key->keyidx == arvif->def_wep_key_index)
1402                 return;
1403
1404         ath10k_dbg(ATH10K_DBG_MAC, "new wep keyidx will be %d\n", key->keyidx);
1405
1406         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
1407                                         WMI_VDEV_PARAM_DEF_KEYID,
1408                                         key->keyidx);
1409         if (ret) {
1410                 ath10k_warn("could not update wep keyidx (%d)\n", ret);
1411                 return;
1412         }
1413
1414         arvif->def_wep_key_index = key->keyidx;
1415 }
1416
1417 static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar, struct sk_buff *skb)
1418 {
1419         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1420         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1421         struct ieee80211_vif *vif = info->control.vif;
1422         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1423
1424         /* This is case only for P2P_GO */
1425         if (arvif->vdev_type != WMI_VDEV_TYPE_AP ||
1426             arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
1427                 return;
1428
1429         if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
1430                 spin_lock_bh(&ar->data_lock);
1431                 if (arvif->u.ap.noa_data)
1432                         if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
1433                                               GFP_ATOMIC))
1434                                 memcpy(skb_put(skb, arvif->u.ap.noa_len),
1435                                        arvif->u.ap.noa_data,
1436                                        arvif->u.ap.noa_len);
1437                 spin_unlock_bh(&ar->data_lock);
1438         }
1439 }
1440
1441 static void ath10k_tx_htt(struct ath10k *ar, struct sk_buff *skb)
1442 {
1443         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1444         int ret;
1445
1446         if (ieee80211_is_mgmt(hdr->frame_control))
1447                 ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
1448         else if (ieee80211_is_nullfunc(hdr->frame_control))
1449                 /* FW does not report tx status properly for NullFunc frames
1450                  * unless they are sent through mgmt tx path. mac80211 sends
1451                  * those frames when it detects link/beacon loss and depends on
1452                  * the tx status to be correct. */
1453                 ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
1454         else
1455                 ret = ath10k_htt_tx(&ar->htt, skb);
1456
1457         if (ret) {
1458                 ath10k_warn("tx failed (%d). dropping packet.\n", ret);
1459                 ieee80211_free_txskb(ar->hw, skb);
1460         }
1461 }
1462
1463 void ath10k_offchan_tx_purge(struct ath10k *ar)
1464 {
1465         struct sk_buff *skb;
1466
1467         for (;;) {
1468                 skb = skb_dequeue(&ar->offchan_tx_queue);
1469                 if (!skb)
1470                         break;
1471
1472                 ieee80211_free_txskb(ar->hw, skb);
1473         }
1474 }
1475
1476 void ath10k_offchan_tx_work(struct work_struct *work)
1477 {
1478         struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
1479         struct ath10k_peer *peer;
1480         struct ieee80211_hdr *hdr;
1481         struct sk_buff *skb;
1482         const u8 *peer_addr;
1483         int vdev_id;
1484         int ret;
1485
1486         /* FW requirement: We must create a peer before FW will send out
1487          * an offchannel frame. Otherwise the frame will be stuck and
1488          * never transmitted. We delete the peer upon tx completion.
1489          * It is unlikely that a peer for offchannel tx will already be
1490          * present. However it may be in some rare cases so account for that.
1491          * Otherwise we might remove a legitimate peer and break stuff. */
1492
1493         for (;;) {
1494                 skb = skb_dequeue(&ar->offchan_tx_queue);
1495                 if (!skb)
1496                         break;
1497
1498                 mutex_lock(&ar->conf_mutex);
1499
1500                 ath10k_dbg(ATH10K_DBG_MAC, "processing offchannel skb %p\n",
1501                            skb);
1502
1503                 hdr = (struct ieee80211_hdr *)skb->data;
1504                 peer_addr = ieee80211_get_DA(hdr);
1505                 vdev_id = ATH10K_SKB_CB(skb)->htt.vdev_id;
1506
1507                 spin_lock_bh(&ar->data_lock);
1508                 peer = ath10k_peer_find(ar, vdev_id, peer_addr);
1509                 spin_unlock_bh(&ar->data_lock);
1510
1511                 if (peer)
1512                         ath10k_dbg(ATH10K_DBG_MAC, "peer %pM on vdev %d already present\n",
1513                                    peer_addr, vdev_id);
1514
1515                 if (!peer) {
1516                         ret = ath10k_peer_create(ar, vdev_id, peer_addr);
1517                         if (ret)
1518                                 ath10k_warn("peer %pM on vdev %d not created (%d)\n",
1519                                             peer_addr, vdev_id, ret);
1520                 }
1521
1522                 spin_lock_bh(&ar->data_lock);
1523                 INIT_COMPLETION(ar->offchan_tx_completed);
1524                 ar->offchan_tx_skb = skb;
1525                 spin_unlock_bh(&ar->data_lock);
1526
1527                 ath10k_tx_htt(ar, skb);
1528
1529                 ret = wait_for_completion_timeout(&ar->offchan_tx_completed,
1530                                                   3 * HZ);
1531                 if (ret <= 0)
1532                         ath10k_warn("timed out waiting for offchannel skb %p\n",
1533                                     skb);
1534
1535                 if (!peer) {
1536                         ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
1537                         if (ret)
1538                                 ath10k_warn("peer %pM on vdev %d not deleted (%d)\n",
1539                                             peer_addr, vdev_id, ret);
1540                 }
1541
1542                 mutex_unlock(&ar->conf_mutex);
1543         }
1544 }
1545
1546 /************/
1547 /* Scanning */
1548 /************/
1549
1550 /*
1551  * This gets called if we dont get a heart-beat during scan.
1552  * This may indicate the FW has hung and we need to abort the
1553  * scan manually to prevent cancel_hw_scan() from deadlocking
1554  */
1555 void ath10k_reset_scan(unsigned long ptr)
1556 {
1557         struct ath10k *ar = (struct ath10k *)ptr;
1558
1559         spin_lock_bh(&ar->data_lock);
1560         if (!ar->scan.in_progress) {
1561                 spin_unlock_bh(&ar->data_lock);
1562                 return;
1563         }
1564
1565         ath10k_warn("scan timeout. resetting. fw issue?\n");
1566
1567         if (ar->scan.is_roc)
1568                 ieee80211_remain_on_channel_expired(ar->hw);
1569         else
1570                 ieee80211_scan_completed(ar->hw, 1 /* aborted */);
1571
1572         ar->scan.in_progress = false;
1573         complete_all(&ar->scan.completed);
1574         spin_unlock_bh(&ar->data_lock);
1575 }
1576
1577 static int ath10k_abort_scan(struct ath10k *ar)
1578 {
1579         struct wmi_stop_scan_arg arg = {
1580                 .req_id = 1, /* FIXME */
1581                 .req_type = WMI_SCAN_STOP_ONE,
1582                 .u.scan_id = ATH10K_SCAN_ID,
1583         };
1584         int ret;
1585
1586         lockdep_assert_held(&ar->conf_mutex);
1587
1588         del_timer_sync(&ar->scan.timeout);
1589
1590         spin_lock_bh(&ar->data_lock);
1591         if (!ar->scan.in_progress) {
1592                 spin_unlock_bh(&ar->data_lock);
1593                 return 0;
1594         }
1595
1596         ar->scan.aborting = true;
1597         spin_unlock_bh(&ar->data_lock);
1598
1599         ret = ath10k_wmi_stop_scan(ar, &arg);
1600         if (ret) {
1601                 ath10k_warn("could not submit wmi stop scan (%d)\n", ret);
1602                 spin_lock_bh(&ar->data_lock);
1603                 ar->scan.in_progress = false;
1604                 ath10k_offchan_tx_purge(ar);
1605                 spin_unlock_bh(&ar->data_lock);
1606                 return -EIO;
1607         }
1608
1609         ath10k_wmi_flush_tx(ar);
1610
1611         ret = wait_for_completion_timeout(&ar->scan.completed, 3*HZ);
1612         if (ret == 0)
1613                 ath10k_warn("timed out while waiting for scan to stop\n");
1614
1615         /* scan completion may be done right after we timeout here, so let's
1616          * check the in_progress and tell mac80211 scan is completed. if we
1617          * don't do that and FW fails to send us scan completion indication
1618          * then userspace won't be able to scan anymore */
1619         ret = 0;
1620
1621         spin_lock_bh(&ar->data_lock);
1622         if (ar->scan.in_progress) {
1623                 ath10k_warn("could not stop scan. its still in progress\n");
1624                 ar->scan.in_progress = false;
1625                 ath10k_offchan_tx_purge(ar);
1626                 ret = -ETIMEDOUT;
1627         }
1628         spin_unlock_bh(&ar->data_lock);
1629
1630         return ret;
1631 }
1632
1633 static int ath10k_start_scan(struct ath10k *ar,
1634                              const struct wmi_start_scan_arg *arg)
1635 {
1636         int ret;
1637
1638         lockdep_assert_held(&ar->conf_mutex);
1639
1640         ret = ath10k_wmi_start_scan(ar, arg);
1641         if (ret)
1642                 return ret;
1643
1644         /* make sure we submit the command so the completion
1645         * timeout makes sense */
1646         ath10k_wmi_flush_tx(ar);
1647
1648         ret = wait_for_completion_timeout(&ar->scan.started, 1*HZ);
1649         if (ret == 0) {
1650                 ath10k_abort_scan(ar);
1651                 return ret;
1652         }
1653
1654         /* the scan can complete earlier, before we even
1655          * start the timer. in that case the timer handler
1656          * checks ar->scan.in_progress and bails out if its
1657          * false. Add a 200ms margin to account event/command
1658          * processing. */
1659         mod_timer(&ar->scan.timeout, jiffies +
1660                   msecs_to_jiffies(arg->max_scan_time+200));
1661         return 0;
1662 }
1663
1664 /**********************/
1665 /* mac80211 callbacks */
1666 /**********************/
1667
1668 static void ath10k_tx(struct ieee80211_hw *hw,
1669                       struct ieee80211_tx_control *control,
1670                       struct sk_buff *skb)
1671 {
1672         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1673         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1674         struct ath10k *ar = hw->priv;
1675         struct ath10k_vif *arvif = NULL;
1676         u32 vdev_id = 0;
1677         u8 tid;
1678
1679         if (info->control.vif) {
1680                 arvif = ath10k_vif_to_arvif(info->control.vif);
1681                 vdev_id = arvif->vdev_id;
1682         } else if (ar->monitor_enabled) {
1683                 vdev_id = ar->monitor_vdev_id;
1684         }
1685
1686         /* We should disable CCK RATE due to P2P */
1687         if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
1688                 ath10k_dbg(ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
1689
1690         /* we must calculate tid before we apply qos workaround
1691          * as we'd lose the qos control field */
1692         tid = HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
1693         if (ieee80211_is_data_qos(hdr->frame_control) &&
1694             is_unicast_ether_addr(ieee80211_get_DA(hdr))) {
1695                 u8 *qc = ieee80211_get_qos_ctl(hdr);
1696                 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
1697         }
1698
1699         ath10k_tx_h_qos_workaround(hw, control, skb);
1700         ath10k_tx_h_update_wep_key(skb);
1701         ath10k_tx_h_add_p2p_noa_ie(ar, skb);
1702         ath10k_tx_h_seq_no(skb);
1703
1704         memset(ATH10K_SKB_CB(skb), 0, sizeof(*ATH10K_SKB_CB(skb)));
1705         ATH10K_SKB_CB(skb)->htt.vdev_id = vdev_id;
1706         ATH10K_SKB_CB(skb)->htt.tid = tid;
1707
1708         if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
1709                 spin_lock_bh(&ar->data_lock);
1710                 ATH10K_SKB_CB(skb)->htt.is_offchan = true;
1711                 ATH10K_SKB_CB(skb)->htt.vdev_id = ar->scan.vdev_id;
1712                 spin_unlock_bh(&ar->data_lock);
1713
1714                 ath10k_dbg(ATH10K_DBG_MAC, "queued offchannel skb %p\n", skb);
1715
1716                 skb_queue_tail(&ar->offchan_tx_queue, skb);
1717                 ieee80211_queue_work(hw, &ar->offchan_tx_work);
1718                 return;
1719         }
1720
1721         ath10k_tx_htt(ar, skb);
1722 }
1723
1724 /*
1725  * Initialize various parameters with default vaules.
1726  */
1727 static int ath10k_start(struct ieee80211_hw *hw)
1728 {
1729         struct ath10k *ar = hw->priv;
1730         int ret;
1731
1732         mutex_lock(&ar->conf_mutex);
1733
1734         ret = ath10k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_PMF_QOS, 1);
1735         if (ret)
1736                 ath10k_warn("could not enable WMI_PDEV_PARAM_PMF_QOS (%d)\n",
1737                             ret);
1738
1739         ret = ath10k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_DYNAMIC_BW, 0);
1740         if (ret)
1741                 ath10k_warn("could not init WMI_PDEV_PARAM_DYNAMIC_BW (%d)\n",
1742                             ret);
1743
1744         ar->state = ATH10K_STATE_ON;
1745         ath10k_regd_update(ar);
1746
1747         mutex_unlock(&ar->conf_mutex);
1748         return 0;
1749 }
1750
1751 static void ath10k_stop(struct ieee80211_hw *hw)
1752 {
1753         struct ath10k *ar = hw->priv;
1754
1755         mutex_lock(&ar->conf_mutex);
1756         ath10k_offchan_tx_purge(ar);
1757         ar->state = ATH10K_STATE_OFF;
1758         mutex_unlock(&ar->conf_mutex);
1759
1760         cancel_work_sync(&ar->offchan_tx_work);
1761 }
1762
1763 static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
1764 {
1765         struct ath10k_generic_iter ar_iter;
1766         struct ath10k *ar = hw->priv;
1767         struct ieee80211_conf *conf = &hw->conf;
1768         int ret = 0;
1769         u32 flags;
1770
1771         mutex_lock(&ar->conf_mutex);
1772
1773         if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
1774                 ath10k_dbg(ATH10K_DBG_MAC, "Config channel %d mhz\n",
1775                            conf->chandef.chan->center_freq);
1776                 spin_lock_bh(&ar->data_lock);
1777                 ar->rx_channel = conf->chandef.chan;
1778                 spin_unlock_bh(&ar->data_lock);
1779         }
1780
1781         if (changed & IEEE80211_CONF_CHANGE_PS) {
1782                 memset(&ar_iter, 0, sizeof(struct ath10k_generic_iter));
1783                 ar_iter.ar = ar;
1784                 flags = IEEE80211_IFACE_ITER_RESUME_ALL;
1785
1786                 ieee80211_iterate_active_interfaces_atomic(hw,
1787                                                            flags,
1788                                                            ath10k_ps_iter,
1789                                                            &ar_iter);
1790
1791                 ret = ar_iter.ret;
1792         }
1793
1794         if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
1795                 if (conf->flags & IEEE80211_CONF_MONITOR)
1796                         ret = ath10k_monitor_create(ar);
1797                 else
1798                         ret = ath10k_monitor_destroy(ar);
1799         }
1800
1801         mutex_unlock(&ar->conf_mutex);
1802         return ret;
1803 }
1804
1805 /*
1806  * TODO:
1807  * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
1808  * because we will send mgmt frames without CCK. This requirement
1809  * for P2P_FIND/GO_NEG should be handled by checking CCK flag
1810  * in the TX packet.
1811  */
1812 static int ath10k_add_interface(struct ieee80211_hw *hw,
1813                                 struct ieee80211_vif *vif)
1814 {
1815         struct ath10k *ar = hw->priv;
1816         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1817         enum wmi_sta_powersave_param param;
1818         int ret = 0;
1819         u32 value, rts, frag;
1820         int bit;
1821
1822         mutex_lock(&ar->conf_mutex);
1823
1824         arvif->ar = ar;
1825         arvif->vif = vif;
1826
1827         if ((vif->type == NL80211_IFTYPE_MONITOR) && ar->monitor_present) {
1828                 ath10k_warn("Only one monitor interface allowed\n");
1829                 ret = -EBUSY;
1830                 goto exit;
1831         }
1832
1833         bit = ffs(ar->free_vdev_map);
1834         if (bit == 0) {
1835                 ret = -EBUSY;
1836                 goto exit;
1837         }
1838
1839         arvif->vdev_id = bit - 1;
1840         arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
1841         ar->free_vdev_map &= ~(1 << arvif->vdev_id);
1842
1843         if (ar->p2p)
1844                 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
1845
1846         switch (vif->type) {
1847         case NL80211_IFTYPE_UNSPECIFIED:
1848         case NL80211_IFTYPE_STATION:
1849                 arvif->vdev_type = WMI_VDEV_TYPE_STA;
1850                 if (vif->p2p)
1851                         arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
1852                 break;
1853         case NL80211_IFTYPE_ADHOC:
1854                 arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
1855                 break;
1856         case NL80211_IFTYPE_AP:
1857                 arvif->vdev_type = WMI_VDEV_TYPE_AP;
1858
1859                 if (vif->p2p)
1860                         arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
1861                 break;
1862         case NL80211_IFTYPE_MONITOR:
1863                 arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
1864                 break;
1865         default:
1866                 WARN_ON(1);
1867                 break;
1868         }
1869
1870         ath10k_dbg(ATH10K_DBG_MAC, "Add interface: id %d type %d subtype %d\n",
1871                    arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype);
1872
1873         ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
1874                                      arvif->vdev_subtype, vif->addr);
1875         if (ret) {
1876                 ath10k_warn("WMI vdev create failed: ret %d\n", ret);
1877                 goto exit;
1878         }
1879
1880         ret = ath10k_wmi_vdev_set_param(ar, 0, WMI_VDEV_PARAM_DEF_KEYID,
1881                                         arvif->def_wep_key_index);
1882         if (ret)
1883                 ath10k_warn("Failed to set default keyid: %d\n", ret);
1884
1885         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
1886                                         WMI_VDEV_PARAM_TX_ENCAP_TYPE,
1887                                         ATH10K_HW_TXRX_NATIVE_WIFI);
1888         if (ret)
1889                 ath10k_warn("Failed to set TX encap: %d\n", ret);
1890
1891         if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
1892                 ret = ath10k_peer_create(ar, arvif->vdev_id, vif->addr);
1893                 if (ret) {
1894                         ath10k_warn("Failed to create peer for AP: %d\n", ret);
1895                         goto exit;
1896                 }
1897         }
1898
1899         if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
1900                 param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
1901                 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
1902                 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
1903                                                   param, value);
1904                 if (ret)
1905                         ath10k_warn("Failed to set RX wake policy: %d\n", ret);
1906
1907                 param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
1908                 value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
1909                 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
1910                                                   param, value);
1911                 if (ret)
1912                         ath10k_warn("Failed to set TX wake thresh: %d\n", ret);
1913
1914                 param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
1915                 value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
1916                 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
1917                                                   param, value);
1918                 if (ret)
1919                         ath10k_warn("Failed to set PSPOLL count: %d\n", ret);
1920         }
1921
1922         rts = min_t(u32, ar->hw->wiphy->rts_threshold, ATH10K_RTS_MAX);
1923         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
1924                                          WMI_VDEV_PARAM_RTS_THRESHOLD,
1925                                          rts);
1926         if (ret)
1927                 ath10k_warn("failed to set rts threshold for vdev %d (%d)\n",
1928                             arvif->vdev_id, ret);
1929
1930         frag = clamp_t(u32, ar->hw->wiphy->frag_threshold,
1931                        ATH10K_FRAGMT_THRESHOLD_MIN,
1932                        ATH10K_FRAGMT_THRESHOLD_MAX);
1933         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
1934                                         WMI_VDEV_PARAM_FRAGMENTATION_THRESHOLD,
1935                                         frag);
1936         if (ret)
1937                 ath10k_warn("failed to set frag threshold for vdev %d (%d)\n",
1938                             arvif->vdev_id, ret);
1939
1940         if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
1941                 ar->monitor_present = true;
1942
1943 exit:
1944         mutex_unlock(&ar->conf_mutex);
1945         return ret;
1946 }
1947
1948 static void ath10k_remove_interface(struct ieee80211_hw *hw,
1949                                     struct ieee80211_vif *vif)
1950 {
1951         struct ath10k *ar = hw->priv;
1952         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1953         int ret;
1954
1955         mutex_lock(&ar->conf_mutex);
1956
1957         ath10k_dbg(ATH10K_DBG_MAC, "Remove interface: id %d\n", arvif->vdev_id);
1958
1959         ar->free_vdev_map |= 1 << (arvif->vdev_id);
1960
1961         if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
1962                 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, vif->addr);
1963                 if (ret)
1964                         ath10k_warn("Failed to remove peer for AP: %d\n", ret);
1965
1966                 kfree(arvif->u.ap.noa_data);
1967         }
1968
1969         ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
1970         if (ret)
1971                 ath10k_warn("WMI vdev delete failed: %d\n", ret);
1972
1973         if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
1974                 ar->monitor_present = false;
1975
1976         ath10k_peer_cleanup(ar, arvif->vdev_id);
1977
1978         mutex_unlock(&ar->conf_mutex);
1979 }
1980
1981 /*
1982  * FIXME: Has to be verified.
1983  */
1984 #define SUPPORTED_FILTERS                       \
1985         (FIF_PROMISC_IN_BSS |                   \
1986         FIF_ALLMULTI |                          \
1987         FIF_CONTROL |                           \
1988         FIF_PSPOLL |                            \
1989         FIF_OTHER_BSS |                         \
1990         FIF_BCN_PRBRESP_PROMISC |               \
1991         FIF_PROBE_REQ |                         \
1992         FIF_FCSFAIL)
1993
1994 static void ath10k_configure_filter(struct ieee80211_hw *hw,
1995                                     unsigned int changed_flags,
1996                                     unsigned int *total_flags,
1997                                     u64 multicast)
1998 {
1999         struct ath10k *ar = hw->priv;
2000         int ret;
2001
2002         mutex_lock(&ar->conf_mutex);
2003
2004         changed_flags &= SUPPORTED_FILTERS;
2005         *total_flags &= SUPPORTED_FILTERS;
2006         ar->filter_flags = *total_flags;
2007
2008         if ((ar->filter_flags & FIF_PROMISC_IN_BSS) &&
2009             !ar->monitor_enabled) {
2010                 ret = ath10k_monitor_start(ar, ar->monitor_vdev_id);
2011                 if (ret)
2012                         ath10k_warn("Unable to start monitor mode\n");
2013                 else
2014                         ath10k_dbg(ATH10K_DBG_MAC, "Monitor mode started\n");
2015         } else if (!(ar->filter_flags & FIF_PROMISC_IN_BSS) &&
2016                    ar->monitor_enabled) {
2017                 ret = ath10k_monitor_stop(ar);
2018                 if (ret)
2019                         ath10k_warn("Unable to stop monitor mode\n");
2020                 else
2021                         ath10k_dbg(ATH10K_DBG_MAC, "Monitor mode stopped\n");
2022         }
2023
2024         mutex_unlock(&ar->conf_mutex);
2025 }
2026
2027 static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
2028                                     struct ieee80211_vif *vif,
2029                                     struct ieee80211_bss_conf *info,
2030                                     u32 changed)
2031 {
2032         struct ath10k *ar = hw->priv;
2033         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2034         int ret = 0;
2035
2036         mutex_lock(&ar->conf_mutex);
2037
2038         if (changed & BSS_CHANGED_IBSS)
2039                 ath10k_control_ibss(arvif, info, vif->addr);
2040
2041         if (changed & BSS_CHANGED_BEACON_INT) {
2042                 arvif->beacon_interval = info->beacon_int;
2043                 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
2044                                                 WMI_VDEV_PARAM_BEACON_INTERVAL,
2045                                                 arvif->beacon_interval);
2046                 if (ret)
2047                         ath10k_warn("Failed to set beacon interval for VDEV: %d\n",
2048                                     arvif->vdev_id);
2049                 else
2050                         ath10k_dbg(ATH10K_DBG_MAC,
2051                                    "Beacon interval: %d set for VDEV: %d\n",
2052                                    arvif->beacon_interval, arvif->vdev_id);
2053         }
2054
2055         if (changed & BSS_CHANGED_BEACON) {
2056                 ret = ath10k_wmi_pdev_set_param(ar,
2057                                                 WMI_PDEV_PARAM_BEACON_TX_MODE,
2058                                                 WMI_BEACON_STAGGERED_MODE);
2059                 if (ret)
2060                         ath10k_warn("Failed to set beacon mode for VDEV: %d\n",
2061                                     arvif->vdev_id);
2062                 else
2063                         ath10k_dbg(ATH10K_DBG_MAC,
2064                                    "Set staggered beacon mode for VDEV: %d\n",
2065                                    arvif->vdev_id);
2066         }
2067
2068         if (changed & BSS_CHANGED_BEACON_INFO) {
2069                 arvif->dtim_period = info->dtim_period;
2070
2071                 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
2072                                                 WMI_VDEV_PARAM_DTIM_PERIOD,
2073                                                 arvif->dtim_period);
2074                 if (ret)
2075                         ath10k_warn("Failed to set dtim period for VDEV: %d\n",
2076                                     arvif->vdev_id);
2077                 else
2078                         ath10k_dbg(ATH10K_DBG_MAC,
2079                                    "Set dtim period: %d for VDEV: %d\n",
2080                                    arvif->dtim_period, arvif->vdev_id);
2081         }
2082
2083         if (changed & BSS_CHANGED_SSID &&
2084             vif->type == NL80211_IFTYPE_AP) {
2085                 arvif->u.ap.ssid_len = info->ssid_len;
2086                 if (info->ssid_len)
2087                         memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
2088                 arvif->u.ap.hidden_ssid = info->hidden_ssid;
2089         }
2090
2091         if (changed & BSS_CHANGED_BSSID) {
2092                 if (!is_zero_ether_addr(info->bssid)) {
2093                         ret = ath10k_peer_create(ar, arvif->vdev_id,
2094                                                  info->bssid);
2095                         if (ret)
2096                                 ath10k_warn("Failed to add peer: %pM for VDEV: %d\n",
2097                                             info->bssid, arvif->vdev_id);
2098                         else
2099                                 ath10k_dbg(ATH10K_DBG_MAC,
2100                                            "Added peer: %pM for VDEV: %d\n",
2101                                            info->bssid, arvif->vdev_id);
2102
2103
2104                         if (vif->type == NL80211_IFTYPE_STATION) {
2105                                 /*
2106                                  * this is never erased as we it for crypto key
2107                                  * clearing; this is FW requirement
2108                                  */
2109                                 memcpy(arvif->u.sta.bssid, info->bssid,
2110                                        ETH_ALEN);
2111
2112                                 ret = ath10k_vdev_start(arvif);
2113                                 if (!ret)
2114                                         ath10k_dbg(ATH10K_DBG_MAC,
2115                                                    "VDEV: %d started with BSSID: %pM\n",
2116                                                    arvif->vdev_id, info->bssid);
2117                         }
2118
2119                         /*
2120                          * Mac80211 does not keep IBSS bssid when leaving IBSS,
2121                          * so driver need to store it. It is needed when leaving
2122                          * IBSS in order to remove BSSID peer.
2123                          */
2124                         if (vif->type == NL80211_IFTYPE_ADHOC)
2125                                 memcpy(arvif->u.ibss.bssid, info->bssid,
2126                                        ETH_ALEN);
2127                 }
2128         }
2129
2130         if (changed & BSS_CHANGED_BEACON_ENABLED)
2131                 ath10k_control_beaconing(arvif, info);
2132
2133         if (changed & BSS_CHANGED_ERP_CTS_PROT) {
2134                 u32 cts_prot;
2135                 if (info->use_cts_prot)
2136                         cts_prot = 1;
2137                 else
2138                         cts_prot = 0;
2139
2140                 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
2141                                                 WMI_VDEV_PARAM_ENABLE_RTSCTS,
2142                                                 cts_prot);
2143                 if (ret)
2144                         ath10k_warn("Failed to set CTS prot for VDEV: %d\n",
2145                                     arvif->vdev_id);
2146                 else
2147                         ath10k_dbg(ATH10K_DBG_MAC,
2148                                    "Set CTS prot: %d for VDEV: %d\n",
2149                                    cts_prot, arvif->vdev_id);
2150         }
2151
2152         if (changed & BSS_CHANGED_ERP_SLOT) {
2153                 u32 slottime;
2154                 if (info->use_short_slot)
2155                         slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
2156
2157                 else
2158                         slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
2159
2160                 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
2161                                                 WMI_VDEV_PARAM_SLOT_TIME,
2162                                                 slottime);
2163                 if (ret)
2164                         ath10k_warn("Failed to set erp slot for VDEV: %d\n",
2165                                     arvif->vdev_id);
2166                 else
2167                         ath10k_dbg(ATH10K_DBG_MAC,
2168                                    "Set slottime: %d for VDEV: %d\n",
2169                                    slottime, arvif->vdev_id);
2170         }
2171
2172         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
2173                 u32 preamble;
2174                 if (info->use_short_preamble)
2175                         preamble = WMI_VDEV_PREAMBLE_SHORT;
2176                 else
2177                         preamble = WMI_VDEV_PREAMBLE_LONG;
2178
2179                 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
2180                                                 WMI_VDEV_PARAM_PREAMBLE,
2181                                                 preamble);
2182                 if (ret)
2183                         ath10k_warn("Failed to set preamble for VDEV: %d\n",
2184                                     arvif->vdev_id);
2185                 else
2186                         ath10k_dbg(ATH10K_DBG_MAC,
2187                                    "Set preamble: %d for VDEV: %d\n",
2188                                    preamble, arvif->vdev_id);
2189         }
2190
2191         if (changed & BSS_CHANGED_ASSOC) {
2192                 if (info->assoc)
2193                         ath10k_bss_assoc(hw, vif, info);
2194         }
2195
2196         mutex_unlock(&ar->conf_mutex);
2197 }
2198
2199 static int ath10k_hw_scan(struct ieee80211_hw *hw,
2200                           struct ieee80211_vif *vif,
2201                           struct cfg80211_scan_request *req)
2202 {
2203         struct ath10k *ar = hw->priv;
2204         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2205         struct wmi_start_scan_arg arg;
2206         int ret = 0;
2207         int i;
2208
2209         mutex_lock(&ar->conf_mutex);
2210
2211         spin_lock_bh(&ar->data_lock);
2212         if (ar->scan.in_progress) {
2213                 spin_unlock_bh(&ar->data_lock);
2214                 ret = -EBUSY;
2215                 goto exit;
2216         }
2217
2218         INIT_COMPLETION(ar->scan.started);
2219         INIT_COMPLETION(ar->scan.completed);
2220         ar->scan.in_progress = true;
2221         ar->scan.aborting = false;
2222         ar->scan.is_roc = false;
2223         ar->scan.vdev_id = arvif->vdev_id;
2224         spin_unlock_bh(&ar->data_lock);
2225
2226         memset(&arg, 0, sizeof(arg));
2227         ath10k_wmi_start_scan_init(ar, &arg);
2228         arg.vdev_id = arvif->vdev_id;
2229         arg.scan_id = ATH10K_SCAN_ID;
2230
2231         if (!req->no_cck)
2232                 arg.scan_ctrl_flags |= WMI_SCAN_ADD_CCK_RATES;
2233
2234         if (req->ie_len) {
2235                 arg.ie_len = req->ie_len;
2236                 memcpy(arg.ie, req->ie, arg.ie_len);
2237         }
2238
2239         if (req->n_ssids) {
2240                 arg.n_ssids = req->n_ssids;
2241                 for (i = 0; i < arg.n_ssids; i++) {
2242                         arg.ssids[i].len  = req->ssids[i].ssid_len;
2243                         arg.ssids[i].ssid = req->ssids[i].ssid;
2244                 }
2245         }
2246
2247         if (req->n_channels) {
2248                 arg.n_channels = req->n_channels;
2249                 for (i = 0; i < arg.n_channels; i++)
2250                         arg.channels[i] = req->channels[i]->center_freq;
2251         }
2252
2253         ret = ath10k_start_scan(ar, &arg);
2254         if (ret) {
2255                 ath10k_warn("could not start hw scan (%d)\n", ret);
2256                 spin_lock_bh(&ar->data_lock);
2257                 ar->scan.in_progress = false;
2258                 spin_unlock_bh(&ar->data_lock);
2259         }
2260
2261 exit:
2262         mutex_unlock(&ar->conf_mutex);
2263         return ret;
2264 }
2265
2266 static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
2267                                   struct ieee80211_vif *vif)
2268 {
2269         struct ath10k *ar = hw->priv;
2270         int ret;
2271
2272         mutex_lock(&ar->conf_mutex);
2273         ret = ath10k_abort_scan(ar);
2274         if (ret) {
2275                 ath10k_warn("couldn't abort scan (%d). forcefully sending scan completion to mac80211\n",
2276                             ret);
2277                 ieee80211_scan_completed(hw, 1 /* aborted */);
2278         }
2279         mutex_unlock(&ar->conf_mutex);
2280 }
2281
2282 static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
2283                           struct ieee80211_vif *vif, struct ieee80211_sta *sta,
2284                           struct ieee80211_key_conf *key)
2285 {
2286         struct ath10k *ar = hw->priv;
2287         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2288         struct ath10k_peer *peer;
2289         const u8 *peer_addr;
2290         bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
2291                       key->cipher == WLAN_CIPHER_SUITE_WEP104;
2292         int ret = 0;
2293
2294         if (key->keyidx > WMI_MAX_KEY_INDEX)
2295                 return -ENOSPC;
2296
2297         mutex_lock(&ar->conf_mutex);
2298
2299         if (sta)
2300                 peer_addr = sta->addr;
2301         else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
2302                 peer_addr = vif->bss_conf.bssid;
2303         else
2304                 peer_addr = vif->addr;
2305
2306         key->hw_key_idx = key->keyidx;
2307
2308         /* the peer should not disappear in mid-way (unless FW goes awry) since
2309          * we already hold conf_mutex. we just make sure its there now. */
2310         spin_lock_bh(&ar->data_lock);
2311         peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
2312         spin_unlock_bh(&ar->data_lock);
2313
2314         if (!peer) {
2315                 if (cmd == SET_KEY) {
2316                         ath10k_warn("cannot install key for non-existent peer %pM\n",
2317                                     peer_addr);
2318                         ret = -EOPNOTSUPP;
2319                         goto exit;
2320                 } else {
2321                         /* if the peer doesn't exist there is no key to disable
2322                          * anymore */
2323                         goto exit;
2324                 }
2325         }
2326
2327         if (is_wep) {
2328                 if (cmd == SET_KEY)
2329                         arvif->wep_keys[key->keyidx] = key;
2330                 else
2331                         arvif->wep_keys[key->keyidx] = NULL;
2332
2333                 if (cmd == DISABLE_KEY)
2334                         ath10k_clear_vdev_key(arvif, key);
2335         }
2336
2337         ret = ath10k_install_key(arvif, key, cmd, peer_addr);
2338         if (ret) {
2339                 ath10k_warn("ath10k_install_key failed (%d)\n", ret);
2340                 goto exit;
2341         }
2342
2343         spin_lock_bh(&ar->data_lock);
2344         peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
2345         if (peer && cmd == SET_KEY)
2346                 peer->keys[key->keyidx] = key;
2347         else if (peer && cmd == DISABLE_KEY)
2348                 peer->keys[key->keyidx] = NULL;
2349         else if (peer == NULL)
2350                 /* impossible unless FW goes crazy */
2351                 ath10k_warn("peer %pM disappeared!\n", peer_addr);
2352         spin_unlock_bh(&ar->data_lock);
2353
2354 exit:
2355         mutex_unlock(&ar->conf_mutex);
2356         return ret;
2357 }
2358
2359 static int ath10k_sta_state(struct ieee80211_hw *hw,
2360                             struct ieee80211_vif *vif,
2361                             struct ieee80211_sta *sta,
2362                             enum ieee80211_sta_state old_state,
2363                             enum ieee80211_sta_state new_state)
2364 {
2365         struct ath10k *ar = hw->priv;
2366         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2367         int ret = 0;
2368
2369         mutex_lock(&ar->conf_mutex);
2370
2371         if (old_state == IEEE80211_STA_NOTEXIST &&
2372             new_state == IEEE80211_STA_NONE &&
2373             vif->type != NL80211_IFTYPE_STATION) {
2374                 /*
2375                  * New station addition.
2376                  */
2377                 ret = ath10k_peer_create(ar, arvif->vdev_id, sta->addr);
2378                 if (ret)
2379                         ath10k_warn("Failed to add peer: %pM for VDEV: %d\n",
2380                                     sta->addr, arvif->vdev_id);
2381                 else
2382                         ath10k_dbg(ATH10K_DBG_MAC,
2383                                    "Added peer: %pM for VDEV: %d\n",
2384                                    sta->addr, arvif->vdev_id);
2385         } else if ((old_state == IEEE80211_STA_NONE &&
2386                     new_state == IEEE80211_STA_NOTEXIST)) {
2387                 /*
2388                  * Existing station deletion.
2389                  */
2390                 ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
2391                 if (ret)
2392                         ath10k_warn("Failed to delete peer: %pM for VDEV: %d\n",
2393                                     sta->addr, arvif->vdev_id);
2394                 else
2395                         ath10k_dbg(ATH10K_DBG_MAC,
2396                                    "Removed peer: %pM for VDEV: %d\n",
2397                                    sta->addr, arvif->vdev_id);
2398
2399                 if (vif->type == NL80211_IFTYPE_STATION)
2400                         ath10k_bss_disassoc(hw, vif);
2401         } else if (old_state == IEEE80211_STA_AUTH &&
2402                    new_state == IEEE80211_STA_ASSOC &&
2403                    (vif->type == NL80211_IFTYPE_AP ||
2404                     vif->type == NL80211_IFTYPE_ADHOC)) {
2405                 /*
2406                  * New association.
2407                  */
2408                 ret = ath10k_station_assoc(ar, arvif, sta);
2409                 if (ret)
2410                         ath10k_warn("Failed to associate station: %pM\n",
2411                                     sta->addr);
2412                 else
2413                         ath10k_dbg(ATH10K_DBG_MAC,
2414                                    "Station %pM moved to assoc state\n",
2415                                    sta->addr);
2416         } else if (old_state == IEEE80211_STA_ASSOC &&
2417                    new_state == IEEE80211_STA_AUTH &&
2418                    (vif->type == NL80211_IFTYPE_AP ||
2419                     vif->type == NL80211_IFTYPE_ADHOC)) {
2420                 /*
2421                  * Disassociation.
2422                  */
2423                 ret = ath10k_station_disassoc(ar, arvif, sta);
2424                 if (ret)
2425                         ath10k_warn("Failed to disassociate station: %pM\n",
2426                                     sta->addr);
2427                 else
2428                         ath10k_dbg(ATH10K_DBG_MAC,
2429                                    "Station %pM moved to disassociated state\n",
2430                                    sta->addr);
2431         }
2432
2433         mutex_unlock(&ar->conf_mutex);
2434         return ret;
2435 }
2436
2437 static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
2438                                  u16 ac, bool enable)
2439 {
2440         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2441         u32 value = 0;
2442         int ret = 0;
2443
2444         lockdep_assert_held(&ar->conf_mutex);
2445
2446         if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
2447                 return 0;
2448
2449         switch (ac) {
2450         case IEEE80211_AC_VO:
2451                 value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
2452                         WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
2453                 break;
2454         case IEEE80211_AC_VI:
2455                 value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
2456                         WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
2457                 break;
2458         case IEEE80211_AC_BE:
2459                 value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
2460                         WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
2461                 break;
2462         case IEEE80211_AC_BK:
2463                 value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
2464                         WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
2465                 break;
2466         }
2467
2468         if (enable)
2469                 arvif->u.sta.uapsd |= value;
2470         else
2471                 arvif->u.sta.uapsd &= ~value;
2472
2473         ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
2474                                           WMI_STA_PS_PARAM_UAPSD,
2475                                           arvif->u.sta.uapsd);
2476         if (ret) {
2477                 ath10k_warn("could not set uapsd params %d\n", ret);
2478                 goto exit;
2479         }
2480
2481         if (arvif->u.sta.uapsd)
2482                 value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
2483         else
2484                 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
2485
2486         ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
2487                                           WMI_STA_PS_PARAM_RX_WAKE_POLICY,
2488                                           value);
2489         if (ret)
2490                 ath10k_warn("could not set rx wake param %d\n", ret);
2491
2492 exit:
2493         return ret;
2494 }
2495
2496 static int ath10k_conf_tx(struct ieee80211_hw *hw,
2497                           struct ieee80211_vif *vif, u16 ac,
2498                           const struct ieee80211_tx_queue_params *params)
2499 {
2500         struct ath10k *ar = hw->priv;
2501         struct wmi_wmm_params_arg *p = NULL;
2502         int ret;
2503
2504         mutex_lock(&ar->conf_mutex);
2505
2506         switch (ac) {
2507         case IEEE80211_AC_VO:
2508                 p = &ar->wmm_params.ac_vo;
2509                 break;
2510         case IEEE80211_AC_VI:
2511                 p = &ar->wmm_params.ac_vi;
2512                 break;
2513         case IEEE80211_AC_BE:
2514                 p = &ar->wmm_params.ac_be;
2515                 break;
2516         case IEEE80211_AC_BK:
2517                 p = &ar->wmm_params.ac_bk;
2518                 break;
2519         }
2520
2521         if (WARN_ON(!p)) {
2522                 ret = -EINVAL;
2523                 goto exit;
2524         }
2525
2526         p->cwmin = params->cw_min;
2527         p->cwmax = params->cw_max;
2528         p->aifs = params->aifs;
2529
2530         /*
2531          * The channel time duration programmed in the HW is in absolute
2532          * microseconds, while mac80211 gives the txop in units of
2533          * 32 microseconds.
2534          */
2535         p->txop = params->txop * 32;
2536
2537         /* FIXME: FW accepts wmm params per hw, not per vif */
2538         ret = ath10k_wmi_pdev_set_wmm_params(ar, &ar->wmm_params);
2539         if (ret) {
2540                 ath10k_warn("could not set wmm params %d\n", ret);
2541                 goto exit;
2542         }
2543
2544         ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
2545         if (ret)
2546                 ath10k_warn("could not set sta uapsd %d\n", ret);
2547
2548 exit:
2549         mutex_unlock(&ar->conf_mutex);
2550         return ret;
2551 }
2552
2553 #define ATH10K_ROC_TIMEOUT_HZ (2*HZ)
2554
2555 static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
2556                                     struct ieee80211_vif *vif,
2557                                     struct ieee80211_channel *chan,
2558                                     int duration,
2559                                     enum ieee80211_roc_type type)
2560 {
2561         struct ath10k *ar = hw->priv;
2562         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2563         struct wmi_start_scan_arg arg;
2564         int ret;
2565
2566         mutex_lock(&ar->conf_mutex);
2567
2568         spin_lock_bh(&ar->data_lock);
2569         if (ar->scan.in_progress) {
2570                 spin_unlock_bh(&ar->data_lock);
2571                 ret = -EBUSY;
2572                 goto exit;
2573         }
2574
2575         INIT_COMPLETION(ar->scan.started);
2576         INIT_COMPLETION(ar->scan.completed);
2577         INIT_COMPLETION(ar->scan.on_channel);
2578         ar->scan.in_progress = true;
2579         ar->scan.aborting = false;
2580         ar->scan.is_roc = true;
2581         ar->scan.vdev_id = arvif->vdev_id;
2582         ar->scan.roc_freq = chan->center_freq;
2583         spin_unlock_bh(&ar->data_lock);
2584
2585         memset(&arg, 0, sizeof(arg));
2586         ath10k_wmi_start_scan_init(ar, &arg);
2587         arg.vdev_id = arvif->vdev_id;
2588         arg.scan_id = ATH10K_SCAN_ID;
2589         arg.n_channels = 1;
2590         arg.channels[0] = chan->center_freq;
2591         arg.dwell_time_active = duration;
2592         arg.dwell_time_passive = duration;
2593         arg.max_scan_time = 2 * duration;
2594         arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
2595         arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
2596
2597         ret = ath10k_start_scan(ar, &arg);
2598         if (ret) {
2599                 ath10k_warn("could not start roc scan (%d)\n", ret);
2600                 spin_lock_bh(&ar->data_lock);
2601                 ar->scan.in_progress = false;
2602                 spin_unlock_bh(&ar->data_lock);
2603                 goto exit;
2604         }
2605
2606         ret = wait_for_completion_timeout(&ar->scan.on_channel, 3*HZ);
2607         if (ret == 0) {
2608                 ath10k_warn("could not switch to channel for roc scan\n");
2609                 ath10k_abort_scan(ar);
2610                 ret = -ETIMEDOUT;
2611                 goto exit;
2612         }
2613
2614         ret = 0;
2615 exit:
2616         mutex_unlock(&ar->conf_mutex);
2617         return ret;
2618 }
2619
2620 static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw)
2621 {
2622         struct ath10k *ar = hw->priv;
2623
2624         mutex_lock(&ar->conf_mutex);
2625         ath10k_abort_scan(ar);
2626         mutex_unlock(&ar->conf_mutex);
2627
2628         return 0;
2629 }
2630
2631 /*
2632  * Both RTS and Fragmentation threshold are interface-specific
2633  * in ath10k, but device-specific in mac80211.
2634  */
2635 static void ath10k_set_rts_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
2636 {
2637         struct ath10k_generic_iter *ar_iter = data;
2638         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2639         u32 rts = ar_iter->ar->hw->wiphy->rts_threshold;
2640
2641         lockdep_assert_held(&arvif->ar->conf_mutex);
2642
2643         rts = min_t(u32, rts, ATH10K_RTS_MAX);
2644
2645         ar_iter->ret = ath10k_wmi_vdev_set_param(ar_iter->ar, arvif->vdev_id,
2646                                                  WMI_VDEV_PARAM_RTS_THRESHOLD,
2647                                                  rts);
2648         if (ar_iter->ret)
2649                 ath10k_warn("Failed to set RTS threshold for VDEV: %d\n",
2650                             arvif->vdev_id);
2651         else
2652                 ath10k_dbg(ATH10K_DBG_MAC,
2653                            "Set RTS threshold: %d for VDEV: %d\n",
2654                            rts, arvif->vdev_id);
2655 }
2656
2657 static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
2658 {
2659         struct ath10k_generic_iter ar_iter;
2660         struct ath10k *ar = hw->priv;
2661
2662         memset(&ar_iter, 0, sizeof(struct ath10k_generic_iter));
2663         ar_iter.ar = ar;
2664
2665         mutex_lock(&ar->conf_mutex);
2666         ieee80211_iterate_active_interfaces_atomic(
2667                 hw, IEEE80211_IFACE_ITER_NORMAL,
2668                 ath10k_set_rts_iter, &ar_iter);
2669         mutex_unlock(&ar->conf_mutex);
2670
2671         return ar_iter.ret;
2672 }
2673
2674 static void ath10k_set_frag_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
2675 {
2676         struct ath10k_generic_iter *ar_iter = data;
2677         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2678         u32 frag = ar_iter->ar->hw->wiphy->frag_threshold;
2679         int ret;
2680
2681         lockdep_assert_held(&arvif->ar->conf_mutex);
2682
2683         frag = clamp_t(u32, frag,
2684                        ATH10K_FRAGMT_THRESHOLD_MIN,
2685                        ATH10K_FRAGMT_THRESHOLD_MAX);
2686
2687         ret = ath10k_wmi_vdev_set_param(ar_iter->ar, arvif->vdev_id,
2688                                         WMI_VDEV_PARAM_FRAGMENTATION_THRESHOLD,
2689                                         frag);
2690
2691         ar_iter->ret = ret;
2692         if (ar_iter->ret)
2693                 ath10k_warn("Failed to set frag threshold for VDEV: %d\n",
2694                             arvif->vdev_id);
2695         else
2696                 ath10k_dbg(ATH10K_DBG_MAC,
2697                            "Set frag threshold: %d for VDEV: %d\n",
2698                            frag, arvif->vdev_id);
2699 }
2700
2701 static int ath10k_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
2702 {
2703         struct ath10k_generic_iter ar_iter;
2704         struct ath10k *ar = hw->priv;
2705
2706         memset(&ar_iter, 0, sizeof(struct ath10k_generic_iter));
2707         ar_iter.ar = ar;
2708
2709         mutex_lock(&ar->conf_mutex);
2710         ieee80211_iterate_active_interfaces_atomic(
2711                 hw, IEEE80211_IFACE_ITER_NORMAL,
2712                 ath10k_set_frag_iter, &ar_iter);
2713         mutex_unlock(&ar->conf_mutex);
2714
2715         return ar_iter.ret;
2716 }
2717
2718 static void ath10k_flush(struct ieee80211_hw *hw, u32 queues, bool drop)
2719 {
2720         struct ath10k *ar = hw->priv;
2721         int ret;
2722
2723         /* mac80211 doesn't care if we really xmit queued frames or not
2724          * we'll collect those frames either way if we stop/delete vdevs */
2725         if (drop)
2726                 return;
2727
2728         mutex_lock(&ar->conf_mutex);
2729
2730         ret = wait_event_timeout(ar->htt.empty_tx_wq, ({
2731                         bool empty;
2732                         spin_lock_bh(&ar->htt.tx_lock);
2733                         empty = bitmap_empty(ar->htt.used_msdu_ids,
2734                                              ar->htt.max_num_pending_tx);
2735                         spin_unlock_bh(&ar->htt.tx_lock);
2736                         (empty);
2737                 }), ATH10K_FLUSH_TIMEOUT_HZ);
2738         if (ret <= 0)
2739                 ath10k_warn("tx not flushed\n");
2740
2741         mutex_unlock(&ar->conf_mutex);
2742 }
2743
2744 /* TODO: Implement this function properly
2745  * For now it is needed to reply to Probe Requests in IBSS mode.
2746  * Propably we need this information from FW.
2747  */
2748 static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
2749 {
2750         return 1;
2751 }
2752
2753 #ifdef CONFIG_PM
2754 static int ath10k_suspend(struct ieee80211_hw *hw,
2755                           struct cfg80211_wowlan *wowlan)
2756 {
2757         struct ath10k *ar = hw->priv;
2758         int ret;
2759
2760         ar->is_target_paused = false;
2761
2762         ret = ath10k_wmi_pdev_suspend_target(ar);
2763         if (ret) {
2764                 ath10k_warn("could not suspend target (%d)\n", ret);
2765                 return 1;
2766         }
2767
2768         ret = wait_event_interruptible_timeout(ar->event_queue,
2769                                                ar->is_target_paused == true,
2770                                                1 * HZ);
2771         if (ret < 0) {
2772                 ath10k_warn("suspend interrupted (%d)\n", ret);
2773                 goto resume;
2774         } else if (ret == 0) {
2775                 ath10k_warn("suspend timed out - target pause event never came\n");
2776                 goto resume;
2777         }
2778
2779         ret = ath10k_hif_suspend(ar);
2780         if (ret) {
2781                 ath10k_warn("could not suspend hif (%d)\n", ret);
2782                 goto resume;
2783         }
2784
2785         return 0;
2786 resume:
2787         ret = ath10k_wmi_pdev_resume_target(ar);
2788         if (ret)
2789                 ath10k_warn("could not resume target (%d)\n", ret);
2790         return 1;
2791 }
2792
2793 static int ath10k_resume(struct ieee80211_hw *hw)
2794 {
2795         struct ath10k *ar = hw->priv;
2796         int ret;
2797
2798         ret = ath10k_hif_resume(ar);
2799         if (ret) {
2800                 ath10k_warn("could not resume hif (%d)\n", ret);
2801                 return 1;
2802         }
2803
2804         ret = ath10k_wmi_pdev_resume_target(ar);
2805         if (ret) {
2806                 ath10k_warn("could not resume target (%d)\n", ret);
2807                 return 1;
2808         }
2809
2810         return 0;
2811 }
2812 #endif
2813
2814 static const struct ieee80211_ops ath10k_ops = {
2815         .tx                             = ath10k_tx,
2816         .start                          = ath10k_start,
2817         .stop                           = ath10k_stop,
2818         .config                         = ath10k_config,
2819         .add_interface                  = ath10k_add_interface,
2820         .remove_interface               = ath10k_remove_interface,
2821         .configure_filter               = ath10k_configure_filter,
2822         .bss_info_changed               = ath10k_bss_info_changed,
2823         .hw_scan                        = ath10k_hw_scan,
2824         .cancel_hw_scan                 = ath10k_cancel_hw_scan,
2825         .set_key                        = ath10k_set_key,
2826         .sta_state                      = ath10k_sta_state,
2827         .conf_tx                        = ath10k_conf_tx,
2828         .remain_on_channel              = ath10k_remain_on_channel,
2829         .cancel_remain_on_channel       = ath10k_cancel_remain_on_channel,
2830         .set_rts_threshold              = ath10k_set_rts_threshold,
2831         .set_frag_threshold             = ath10k_set_frag_threshold,
2832         .flush                          = ath10k_flush,
2833         .tx_last_beacon                 = ath10k_tx_last_beacon,
2834 #ifdef CONFIG_PM
2835         .suspend                        = ath10k_suspend,
2836         .resume                         = ath10k_resume,
2837 #endif
2838 };
2839
2840 #define RATETAB_ENT(_rate, _rateid, _flags) { \
2841         .bitrate                = (_rate), \
2842         .flags                  = (_flags), \
2843         .hw_value               = (_rateid), \
2844 }
2845
2846 #define CHAN2G(_channel, _freq, _flags) { \
2847         .band                   = IEEE80211_BAND_2GHZ, \
2848         .hw_value               = (_channel), \
2849         .center_freq            = (_freq), \
2850         .flags                  = (_flags), \
2851         .max_antenna_gain       = 0, \
2852         .max_power              = 30, \
2853 }
2854
2855 #define CHAN5G(_channel, _freq, _flags) { \
2856         .band                   = IEEE80211_BAND_5GHZ, \
2857         .hw_value               = (_channel), \
2858         .center_freq            = (_freq), \
2859         .flags                  = (_flags), \
2860         .max_antenna_gain       = 0, \
2861         .max_power              = 30, \
2862 }
2863
2864 static const struct ieee80211_channel ath10k_2ghz_channels[] = {
2865         CHAN2G(1, 2412, 0),
2866         CHAN2G(2, 2417, 0),
2867         CHAN2G(3, 2422, 0),
2868         CHAN2G(4, 2427, 0),
2869         CHAN2G(5, 2432, 0),
2870         CHAN2G(6, 2437, 0),
2871         CHAN2G(7, 2442, 0),
2872         CHAN2G(8, 2447, 0),
2873         CHAN2G(9, 2452, 0),
2874         CHAN2G(10, 2457, 0),
2875         CHAN2G(11, 2462, 0),
2876         CHAN2G(12, 2467, 0),
2877         CHAN2G(13, 2472, 0),
2878         CHAN2G(14, 2484, 0),
2879 };
2880
2881 static const struct ieee80211_channel ath10k_5ghz_channels[] = {
2882         CHAN5G(36, 5180, 0),
2883         CHAN5G(40, 5200, 0),
2884         CHAN5G(44, 5220, 0),
2885         CHAN5G(48, 5240, 0),
2886         CHAN5G(52, 5260, 0),
2887         CHAN5G(56, 5280, 0),
2888         CHAN5G(60, 5300, 0),
2889         CHAN5G(64, 5320, 0),
2890         CHAN5G(100, 5500, 0),
2891         CHAN5G(104, 5520, 0),
2892         CHAN5G(108, 5540, 0),
2893         CHAN5G(112, 5560, 0),
2894         CHAN5G(116, 5580, 0),
2895         CHAN5G(120, 5600, 0),
2896         CHAN5G(124, 5620, 0),
2897         CHAN5G(128, 5640, 0),
2898         CHAN5G(132, 5660, 0),
2899         CHAN5G(136, 5680, 0),
2900         CHAN5G(140, 5700, 0),
2901         CHAN5G(149, 5745, 0),
2902         CHAN5G(153, 5765, 0),
2903         CHAN5G(157, 5785, 0),
2904         CHAN5G(161, 5805, 0),
2905         CHAN5G(165, 5825, 0),
2906 };
2907
2908 static struct ieee80211_rate ath10k_rates[] = {
2909         /* CCK */
2910         RATETAB_ENT(10,  0x82, 0),
2911         RATETAB_ENT(20,  0x84, 0),
2912         RATETAB_ENT(55,  0x8b, 0),
2913         RATETAB_ENT(110, 0x96, 0),
2914         /* OFDM */
2915         RATETAB_ENT(60,  0x0c, 0),
2916         RATETAB_ENT(90,  0x12, 0),
2917         RATETAB_ENT(120, 0x18, 0),
2918         RATETAB_ENT(180, 0x24, 0),
2919         RATETAB_ENT(240, 0x30, 0),
2920         RATETAB_ENT(360, 0x48, 0),
2921         RATETAB_ENT(480, 0x60, 0),
2922         RATETAB_ENT(540, 0x6c, 0),
2923 };
2924
2925 #define ath10k_a_rates (ath10k_rates + 4)
2926 #define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - 4)
2927 #define ath10k_g_rates (ath10k_rates + 0)
2928 #define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
2929
2930 struct ath10k *ath10k_mac_create(void)
2931 {
2932         struct ieee80211_hw *hw;
2933         struct ath10k *ar;
2934
2935         hw = ieee80211_alloc_hw(sizeof(struct ath10k), &ath10k_ops);
2936         if (!hw)
2937                 return NULL;
2938
2939         ar = hw->priv;
2940         ar->hw = hw;
2941
2942         return ar;
2943 }
2944
2945 void ath10k_mac_destroy(struct ath10k *ar)
2946 {
2947         ieee80211_free_hw(ar->hw);
2948 }
2949
2950 static const struct ieee80211_iface_limit ath10k_if_limits[] = {
2951         {
2952         .max    = 8,
2953         .types  = BIT(NL80211_IFTYPE_STATION)
2954                 | BIT(NL80211_IFTYPE_P2P_CLIENT)
2955                 | BIT(NL80211_IFTYPE_P2P_GO)
2956                 | BIT(NL80211_IFTYPE_AP)
2957         }
2958 };
2959
2960 static const struct ieee80211_iface_combination ath10k_if_comb = {
2961         .limits = ath10k_if_limits,
2962         .n_limits = ARRAY_SIZE(ath10k_if_limits),
2963         .max_interfaces = 8,
2964         .num_different_channels = 1,
2965         .beacon_int_infra_match = true,
2966 };
2967
2968 static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
2969 {
2970         struct ieee80211_sta_vht_cap vht_cap = {0};
2971         u16 mcs_map;
2972
2973         vht_cap.vht_supported = 1;
2974         vht_cap.cap = ar->vht_cap_info;
2975
2976         /* FIXME: check dynamically how many streams board supports */
2977         mcs_map = IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
2978                 IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 |
2979                 IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 |
2980                 IEEE80211_VHT_MCS_NOT_SUPPORTED << 6 |
2981                 IEEE80211_VHT_MCS_NOT_SUPPORTED << 8 |
2982                 IEEE80211_VHT_MCS_NOT_SUPPORTED << 10 |
2983                 IEEE80211_VHT_MCS_NOT_SUPPORTED << 12 |
2984                 IEEE80211_VHT_MCS_NOT_SUPPORTED << 14;
2985
2986         vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
2987         vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
2988
2989         return vht_cap;
2990 }
2991
2992 static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
2993 {
2994         int i;
2995         struct ieee80211_sta_ht_cap ht_cap = {0};
2996
2997         if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
2998                 return ht_cap;
2999
3000         ht_cap.ht_supported = 1;
3001         ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
3002         ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
3003         ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
3004         ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
3005         ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
3006
3007         if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
3008                 ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
3009
3010         if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
3011                 ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
3012
3013         if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
3014                 u32 smps;
3015
3016                 smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
3017                 smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
3018
3019                 ht_cap.cap |= smps;
3020         }
3021
3022         if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC)
3023                 ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
3024
3025         if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
3026                 u32 stbc;
3027
3028                 stbc   = ar->ht_cap_info;
3029                 stbc  &= WMI_HT_CAP_RX_STBC;
3030                 stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
3031                 stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
3032                 stbc  &= IEEE80211_HT_CAP_RX_STBC;
3033
3034                 ht_cap.cap |= stbc;
3035         }
3036
3037         if (ar->ht_cap_info & WMI_HT_CAP_LDPC)
3038                 ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
3039
3040         if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
3041                 ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
3042
3043         /* max AMSDU is implicitly taken from vht_cap_info */
3044         if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
3045                 ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
3046
3047         for (i = 0; i < WMI_MAX_SPATIAL_STREAM; i++)
3048                 ht_cap.mcs.rx_mask[i] = 0xFF;
3049
3050         ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
3051
3052         return ht_cap;
3053 }
3054
3055
3056 static void ath10k_get_arvif_iter(void *data, u8 *mac,
3057                                   struct ieee80211_vif *vif)
3058 {
3059         struct ath10k_vif_iter *arvif_iter = data;
3060         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3061
3062         if (arvif->vdev_id == arvif_iter->vdev_id)
3063                 arvif_iter->arvif = arvif;
3064 }
3065
3066 struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
3067 {
3068         struct ath10k_vif_iter arvif_iter;
3069         u32 flags;
3070
3071         memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
3072         arvif_iter.vdev_id = vdev_id;
3073
3074         flags = IEEE80211_IFACE_ITER_RESUME_ALL;
3075         ieee80211_iterate_active_interfaces_atomic(ar->hw,
3076                                                    flags,
3077                                                    ath10k_get_arvif_iter,
3078                                                    &arvif_iter);
3079         if (!arvif_iter.arvif) {
3080                 ath10k_warn("No VIF found for VDEV: %d\n", vdev_id);
3081                 return NULL;
3082         }
3083
3084         return arvif_iter.arvif;
3085 }
3086
3087 int ath10k_mac_register(struct ath10k *ar)
3088 {
3089         struct ieee80211_supported_band *band;
3090         struct ieee80211_sta_vht_cap vht_cap;
3091         struct ieee80211_sta_ht_cap ht_cap;
3092         void *channels;
3093         int ret;
3094
3095         SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
3096
3097         SET_IEEE80211_DEV(ar->hw, ar->dev);
3098
3099         ht_cap = ath10k_get_ht_cap(ar);
3100         vht_cap = ath10k_create_vht_cap(ar);
3101
3102         if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
3103                 channels = kmemdup(ath10k_2ghz_channels,
3104                                    sizeof(ath10k_2ghz_channels),
3105                                    GFP_KERNEL);
3106                 if (!channels)
3107                         return -ENOMEM;
3108
3109                 band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
3110                 band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
3111                 band->channels = channels;
3112                 band->n_bitrates = ath10k_g_rates_size;
3113                 band->bitrates = ath10k_g_rates;
3114                 band->ht_cap = ht_cap;
3115
3116                 /* vht is not supported in 2.4 GHz */
3117
3118                 ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
3119         }
3120
3121         if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
3122                 channels = kmemdup(ath10k_5ghz_channels,
3123                                    sizeof(ath10k_5ghz_channels),
3124                                    GFP_KERNEL);
3125                 if (!channels) {
3126                         if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
3127                                 band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
3128                                 kfree(band->channels);
3129                         }
3130                         return -ENOMEM;
3131                 }
3132
3133                 band = &ar->mac.sbands[IEEE80211_BAND_5GHZ];
3134                 band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
3135                 band->channels = channels;
3136                 band->n_bitrates = ath10k_a_rates_size;
3137                 band->bitrates = ath10k_a_rates;
3138                 band->ht_cap = ht_cap;
3139                 band->vht_cap = vht_cap;
3140                 ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
3141         }
3142
3143         ar->hw->wiphy->interface_modes =
3144                 BIT(NL80211_IFTYPE_STATION) |
3145                 BIT(NL80211_IFTYPE_ADHOC) |
3146                 BIT(NL80211_IFTYPE_AP) |
3147                 BIT(NL80211_IFTYPE_P2P_CLIENT) |
3148                 BIT(NL80211_IFTYPE_P2P_GO);
3149
3150         ar->hw->flags = IEEE80211_HW_SIGNAL_DBM |
3151                         IEEE80211_HW_SUPPORTS_PS |
3152                         IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
3153                         IEEE80211_HW_SUPPORTS_UAPSD |
3154                         IEEE80211_HW_MFP_CAPABLE |
3155                         IEEE80211_HW_REPORTS_TX_ACK_STATUS |
3156                         IEEE80211_HW_HAS_RATE_CONTROL |
3157                         IEEE80211_HW_SUPPORTS_STATIC_SMPS |
3158                         IEEE80211_HW_WANT_MONITOR_VIF |
3159                         IEEE80211_HW_AP_LINK_PS;
3160
3161         if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
3162                 ar->hw->flags |= IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS;
3163
3164         if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
3165                 ar->hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
3166                 ar->hw->flags |= IEEE80211_HW_TX_AMPDU_SETUP_IN_HW;
3167         }
3168
3169         ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
3170         ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
3171
3172         ar->hw->vif_data_size = sizeof(struct ath10k_vif);
3173
3174         ar->hw->channel_change_time = 5000;
3175         ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
3176
3177         ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
3178         ar->hw->wiphy->max_remain_on_channel_duration = 5000;
3179
3180         ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
3181         /*
3182          * on LL hardware queues are managed entirely by the FW
3183          * so we only advertise to mac we can do the queues thing
3184          */
3185         ar->hw->queues = 4;
3186
3187         ar->hw->wiphy->iface_combinations = &ath10k_if_comb;
3188         ar->hw->wiphy->n_iface_combinations = 1;
3189
3190         ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
3191                             ath10k_reg_notifier);
3192         if (ret) {
3193                 ath10k_err("Regulatory initialization failed\n");
3194                 return ret;
3195         }
3196
3197         ret = ieee80211_register_hw(ar->hw);
3198         if (ret) {
3199                 ath10k_err("ieee80211 registration failed: %d\n", ret);
3200                 return ret;
3201         }
3202
3203         if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
3204                 ret = regulatory_hint(ar->hw->wiphy,
3205                                       ar->ath_common.regulatory.alpha2);
3206                 if (ret)
3207                         goto exit;
3208         }
3209
3210         return 0;
3211 exit:
3212         ieee80211_unregister_hw(ar->hw);
3213         return ret;
3214 }
3215
3216 void ath10k_mac_unregister(struct ath10k *ar)
3217 {
3218         ieee80211_unregister_hw(ar->hw);
3219
3220         kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
3221         kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
3222
3223         SET_IEEE80211_DEV(ar->hw, NULL);
3224 }