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