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