87fa3c2d93824cf5be5f6a071ca71171fa860ee8
[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, rts, frag;
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         rts = min_t(u32, ar->hw->wiphy->rts_threshold, ATH10K_RTS_MAX);
1863         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
1864                                          WMI_VDEV_PARAM_RTS_THRESHOLD,
1865                                          rts);
1866         if (ret)
1867                 ath10k_warn("failed to set rts threshold for vdev %d (%d)\n",
1868                             arvif->vdev_id, ret);
1869
1870         frag = clamp_t(u32, ar->hw->wiphy->frag_threshold,
1871                        ATH10K_FRAGMT_THRESHOLD_MIN,
1872                        ATH10K_FRAGMT_THRESHOLD_MAX);
1873         ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
1874                                         WMI_VDEV_PARAM_FRAGMENTATION_THRESHOLD,
1875                                         frag);
1876         if (ret)
1877                 ath10k_warn("failed to set frag threshold for vdev %d (%d)\n",
1878                             arvif->vdev_id, ret);
1879
1880         if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
1881                 ar->monitor_present = true;
1882
1883 exit:
1884         mutex_unlock(&ar->conf_mutex);
1885         return ret;
1886 }
1887
1888 static void ath10k_remove_interface(struct ieee80211_hw *hw,
1889                                     struct ieee80211_vif *vif)
1890 {
1891         struct ath10k *ar = hw->priv;
1892         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1893         int ret;
1894
1895         mutex_lock(&ar->conf_mutex);
1896
1897         ath10k_dbg(ATH10K_DBG_MAC, "Remove interface: id %d\n", arvif->vdev_id);
1898
1899         ar->free_vdev_map |= 1 << (arvif->vdev_id);
1900
1901         if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
1902                 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, vif->addr);
1903                 if (ret)
1904                         ath10k_warn("Failed to remove peer for AP: %d\n", ret);
1905
1906                 kfree(arvif->u.ap.noa_data);
1907         }
1908
1909         ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
1910         if (ret)
1911                 ath10k_warn("WMI vdev delete failed: %d\n", ret);
1912
1913         if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
1914                 ar->monitor_present = false;
1915
1916         ath10k_peer_cleanup(ar, arvif->vdev_id);
1917
1918         mutex_unlock(&ar->conf_mutex);
1919 }
1920
1921 /*
1922  * FIXME: Has to be verified.
1923  */
1924 #define SUPPORTED_FILTERS                       \
1925         (FIF_PROMISC_IN_BSS |                   \
1926         FIF_ALLMULTI |                          \
1927         FIF_CONTROL |                           \
1928         FIF_PSPOLL |                            \
1929         FIF_OTHER_BSS |                         \
1930         FIF_BCN_PRBRESP_PROMISC |               \
1931         FIF_PROBE_REQ |                         \
1932         FIF_FCSFAIL)
1933
1934 static void ath10k_configure_filter(struct ieee80211_hw *hw,
1935                                     unsigned int changed_flags,
1936                                     unsigned int *total_flags,
1937                                     u64 multicast)
1938 {
1939         struct ath10k *ar = hw->priv;
1940         int ret;
1941
1942         mutex_lock(&ar->conf_mutex);
1943
1944         changed_flags &= SUPPORTED_FILTERS;
1945         *total_flags &= SUPPORTED_FILTERS;
1946         ar->filter_flags = *total_flags;
1947
1948         if ((ar->filter_flags & FIF_PROMISC_IN_BSS) &&
1949             !ar->monitor_enabled) {
1950                 ret = ath10k_monitor_start(ar, ar->monitor_vdev_id);
1951                 if (ret)
1952                         ath10k_warn("Unable to start monitor mode\n");
1953                 else
1954                         ath10k_dbg(ATH10K_DBG_MAC, "Monitor mode started\n");
1955         } else if (!(ar->filter_flags & FIF_PROMISC_IN_BSS) &&
1956                    ar->monitor_enabled) {
1957                 ret = ath10k_monitor_stop(ar);
1958                 if (ret)
1959                         ath10k_warn("Unable to stop monitor mode\n");
1960                 else
1961                         ath10k_dbg(ATH10K_DBG_MAC, "Monitor mode stopped\n");
1962         }
1963
1964         mutex_unlock(&ar->conf_mutex);
1965 }
1966
1967 static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
1968                                     struct ieee80211_vif *vif,
1969                                     struct ieee80211_bss_conf *info,
1970                                     u32 changed)
1971 {
1972         struct ath10k *ar = hw->priv;
1973         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1974         int ret = 0;
1975
1976         mutex_lock(&ar->conf_mutex);
1977
1978         if (changed & BSS_CHANGED_IBSS)
1979                 ath10k_control_ibss(arvif, info, vif->addr);
1980
1981         if (changed & BSS_CHANGED_BEACON_INT) {
1982                 arvif->beacon_interval = info->beacon_int;
1983                 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
1984                                                 WMI_VDEV_PARAM_BEACON_INTERVAL,
1985                                                 arvif->beacon_interval);
1986                 if (ret)
1987                         ath10k_warn("Failed to set beacon interval for VDEV: %d\n",
1988                                     arvif->vdev_id);
1989                 else
1990                         ath10k_dbg(ATH10K_DBG_MAC,
1991                                    "Beacon interval: %d set for VDEV: %d\n",
1992                                    arvif->beacon_interval, arvif->vdev_id);
1993         }
1994
1995         if (changed & BSS_CHANGED_BEACON) {
1996                 ret = ath10k_wmi_pdev_set_param(ar,
1997                                                 WMI_PDEV_PARAM_BEACON_TX_MODE,
1998                                                 WMI_BEACON_STAGGERED_MODE);
1999                 if (ret)
2000                         ath10k_warn("Failed to set beacon mode for VDEV: %d\n",
2001                                     arvif->vdev_id);
2002                 else
2003                         ath10k_dbg(ATH10K_DBG_MAC,
2004                                    "Set staggered beacon mode for VDEV: %d\n",
2005                                    arvif->vdev_id);
2006         }
2007
2008         if (changed & BSS_CHANGED_BEACON_INFO) {
2009                 arvif->dtim_period = info->dtim_period;
2010
2011                 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
2012                                                 WMI_VDEV_PARAM_DTIM_PERIOD,
2013                                                 arvif->dtim_period);
2014                 if (ret)
2015                         ath10k_warn("Failed to set dtim period for VDEV: %d\n",
2016                                     arvif->vdev_id);
2017                 else
2018                         ath10k_dbg(ATH10K_DBG_MAC,
2019                                    "Set dtim period: %d for VDEV: %d\n",
2020                                    arvif->dtim_period, arvif->vdev_id);
2021         }
2022
2023         if (changed & BSS_CHANGED_SSID &&
2024             vif->type == NL80211_IFTYPE_AP) {
2025                 arvif->u.ap.ssid_len = info->ssid_len;
2026                 if (info->ssid_len)
2027                         memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
2028                 arvif->u.ap.hidden_ssid = info->hidden_ssid;
2029         }
2030
2031         if (changed & BSS_CHANGED_BSSID) {
2032                 if (!is_zero_ether_addr(info->bssid)) {
2033                         ret = ath10k_peer_create(ar, arvif->vdev_id,
2034                                                  info->bssid);
2035                         if (ret)
2036                                 ath10k_warn("Failed to add peer: %pM for VDEV: %d\n",
2037                                             info->bssid, arvif->vdev_id);
2038                         else
2039                                 ath10k_dbg(ATH10K_DBG_MAC,
2040                                            "Added peer: %pM for VDEV: %d\n",
2041                                            info->bssid, arvif->vdev_id);
2042
2043
2044                         if (vif->type == NL80211_IFTYPE_STATION) {
2045                                 /*
2046                                  * this is never erased as we it for crypto key
2047                                  * clearing; this is FW requirement
2048                                  */
2049                                 memcpy(arvif->u.sta.bssid, info->bssid,
2050                                        ETH_ALEN);
2051
2052                                 ret = ath10k_vdev_start(arvif);
2053                                 if (!ret)
2054                                         ath10k_dbg(ATH10K_DBG_MAC,
2055                                                    "VDEV: %d started with BSSID: %pM\n",
2056                                                    arvif->vdev_id, info->bssid);
2057                         }
2058
2059                         /*
2060                          * Mac80211 does not keep IBSS bssid when leaving IBSS,
2061                          * so driver need to store it. It is needed when leaving
2062                          * IBSS in order to remove BSSID peer.
2063                          */
2064                         if (vif->type == NL80211_IFTYPE_ADHOC)
2065                                 memcpy(arvif->u.ibss.bssid, info->bssid,
2066                                        ETH_ALEN);
2067                 }
2068         }
2069
2070         if (changed & BSS_CHANGED_BEACON_ENABLED)
2071                 ath10k_control_beaconing(arvif, info);
2072
2073         if (changed & BSS_CHANGED_ERP_CTS_PROT) {
2074                 u32 cts_prot;
2075                 if (info->use_cts_prot)
2076                         cts_prot = 1;
2077                 else
2078                         cts_prot = 0;
2079
2080                 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
2081                                                 WMI_VDEV_PARAM_ENABLE_RTSCTS,
2082                                                 cts_prot);
2083                 if (ret)
2084                         ath10k_warn("Failed to set CTS prot for VDEV: %d\n",
2085                                     arvif->vdev_id);
2086                 else
2087                         ath10k_dbg(ATH10K_DBG_MAC,
2088                                    "Set CTS prot: %d for VDEV: %d\n",
2089                                    cts_prot, arvif->vdev_id);
2090         }
2091
2092         if (changed & BSS_CHANGED_ERP_SLOT) {
2093                 u32 slottime;
2094                 if (info->use_short_slot)
2095                         slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
2096
2097                 else
2098                         slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
2099
2100                 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
2101                                                 WMI_VDEV_PARAM_SLOT_TIME,
2102                                                 slottime);
2103                 if (ret)
2104                         ath10k_warn("Failed to set erp slot for VDEV: %d\n",
2105                                     arvif->vdev_id);
2106                 else
2107                         ath10k_dbg(ATH10K_DBG_MAC,
2108                                    "Set slottime: %d for VDEV: %d\n",
2109                                    slottime, arvif->vdev_id);
2110         }
2111
2112         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
2113                 u32 preamble;
2114                 if (info->use_short_preamble)
2115                         preamble = WMI_VDEV_PREAMBLE_SHORT;
2116                 else
2117                         preamble = WMI_VDEV_PREAMBLE_LONG;
2118
2119                 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
2120                                                 WMI_VDEV_PARAM_PREAMBLE,
2121                                                 preamble);
2122                 if (ret)
2123                         ath10k_warn("Failed to set preamble for VDEV: %d\n",
2124                                     arvif->vdev_id);
2125                 else
2126                         ath10k_dbg(ATH10K_DBG_MAC,
2127                                    "Set preamble: %d for VDEV: %d\n",
2128                                    preamble, arvif->vdev_id);
2129         }
2130
2131         if (changed & BSS_CHANGED_ASSOC) {
2132                 if (info->assoc)
2133                         ath10k_bss_assoc(hw, vif, info);
2134         }
2135
2136         mutex_unlock(&ar->conf_mutex);
2137 }
2138
2139 static int ath10k_hw_scan(struct ieee80211_hw *hw,
2140                           struct ieee80211_vif *vif,
2141                           struct cfg80211_scan_request *req)
2142 {
2143         struct ath10k *ar = hw->priv;
2144         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2145         struct wmi_start_scan_arg arg;
2146         int ret = 0;
2147         int i;
2148
2149         mutex_lock(&ar->conf_mutex);
2150
2151         spin_lock_bh(&ar->data_lock);
2152         if (ar->scan.in_progress) {
2153                 spin_unlock_bh(&ar->data_lock);
2154                 ret = -EBUSY;
2155                 goto exit;
2156         }
2157
2158         INIT_COMPLETION(ar->scan.started);
2159         INIT_COMPLETION(ar->scan.completed);
2160         ar->scan.in_progress = true;
2161         ar->scan.aborting = false;
2162         ar->scan.is_roc = false;
2163         ar->scan.vdev_id = arvif->vdev_id;
2164         spin_unlock_bh(&ar->data_lock);
2165
2166         memset(&arg, 0, sizeof(arg));
2167         ath10k_wmi_start_scan_init(ar, &arg);
2168         arg.vdev_id = arvif->vdev_id;
2169         arg.scan_id = ATH10K_SCAN_ID;
2170
2171         if (!req->no_cck)
2172                 arg.scan_ctrl_flags |= WMI_SCAN_ADD_CCK_RATES;
2173
2174         if (req->ie_len) {
2175                 arg.ie_len = req->ie_len;
2176                 memcpy(arg.ie, req->ie, arg.ie_len);
2177         }
2178
2179         if (req->n_ssids) {
2180                 arg.n_ssids = req->n_ssids;
2181                 for (i = 0; i < arg.n_ssids; i++) {
2182                         arg.ssids[i].len  = req->ssids[i].ssid_len;
2183                         arg.ssids[i].ssid = req->ssids[i].ssid;
2184                 }
2185         }
2186
2187         if (req->n_channels) {
2188                 arg.n_channels = req->n_channels;
2189                 for (i = 0; i < arg.n_channels; i++)
2190                         arg.channels[i] = req->channels[i]->center_freq;
2191         }
2192
2193         ret = ath10k_start_scan(ar, &arg);
2194         if (ret) {
2195                 ath10k_warn("could not start hw scan (%d)\n", ret);
2196                 spin_lock_bh(&ar->data_lock);
2197                 ar->scan.in_progress = false;
2198                 spin_unlock_bh(&ar->data_lock);
2199         }
2200
2201 exit:
2202         mutex_unlock(&ar->conf_mutex);
2203         return ret;
2204 }
2205
2206 static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
2207                                   struct ieee80211_vif *vif)
2208 {
2209         struct ath10k *ar = hw->priv;
2210         int ret;
2211
2212         mutex_lock(&ar->conf_mutex);
2213         ret = ath10k_abort_scan(ar);
2214         if (ret) {
2215                 ath10k_warn("couldn't abort scan (%d). forcefully sending scan completion to mac80211\n",
2216                             ret);
2217                 ieee80211_scan_completed(hw, 1 /* aborted */);
2218         }
2219         mutex_unlock(&ar->conf_mutex);
2220 }
2221
2222 static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
2223                           struct ieee80211_vif *vif, struct ieee80211_sta *sta,
2224                           struct ieee80211_key_conf *key)
2225 {
2226         struct ath10k *ar = hw->priv;
2227         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2228         struct ath10k_peer *peer;
2229         const u8 *peer_addr;
2230         bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
2231                       key->cipher == WLAN_CIPHER_SUITE_WEP104;
2232         int ret = 0;
2233
2234         if (key->keyidx > WMI_MAX_KEY_INDEX)
2235                 return -ENOSPC;
2236
2237         mutex_lock(&ar->conf_mutex);
2238
2239         if (sta)
2240                 peer_addr = sta->addr;
2241         else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
2242                 peer_addr = vif->bss_conf.bssid;
2243         else
2244                 peer_addr = vif->addr;
2245
2246         key->hw_key_idx = key->keyidx;
2247
2248         /* the peer should not disappear in mid-way (unless FW goes awry) since
2249          * we already hold conf_mutex. we just make sure its there now. */
2250         spin_lock_bh(&ar->data_lock);
2251         peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
2252         spin_unlock_bh(&ar->data_lock);
2253
2254         if (!peer) {
2255                 if (cmd == SET_KEY) {
2256                         ath10k_warn("cannot install key for non-existent peer %pM\n",
2257                                     peer_addr);
2258                         ret = -EOPNOTSUPP;
2259                         goto exit;
2260                 } else {
2261                         /* if the peer doesn't exist there is no key to disable
2262                          * anymore */
2263                         goto exit;
2264                 }
2265         }
2266
2267         if (is_wep) {
2268                 if (cmd == SET_KEY)
2269                         arvif->wep_keys[key->keyidx] = key;
2270                 else
2271                         arvif->wep_keys[key->keyidx] = NULL;
2272
2273                 if (cmd == DISABLE_KEY)
2274                         ath10k_clear_vdev_key(arvif, key);
2275         }
2276
2277         ret = ath10k_install_key(arvif, key, cmd, peer_addr);
2278         if (ret) {
2279                 ath10k_warn("ath10k_install_key failed (%d)\n", ret);
2280                 goto exit;
2281         }
2282
2283         spin_lock_bh(&ar->data_lock);
2284         peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
2285         if (peer && cmd == SET_KEY)
2286                 peer->keys[key->keyidx] = key;
2287         else if (peer && cmd == DISABLE_KEY)
2288                 peer->keys[key->keyidx] = NULL;
2289         else if (peer == NULL)
2290                 /* impossible unless FW goes crazy */
2291                 ath10k_warn("peer %pM disappeared!\n", peer_addr);
2292         spin_unlock_bh(&ar->data_lock);
2293
2294 exit:
2295         mutex_unlock(&ar->conf_mutex);
2296         return ret;
2297 }
2298
2299 static int ath10k_sta_state(struct ieee80211_hw *hw,
2300                             struct ieee80211_vif *vif,
2301                             struct ieee80211_sta *sta,
2302                             enum ieee80211_sta_state old_state,
2303                             enum ieee80211_sta_state new_state)
2304 {
2305         struct ath10k *ar = hw->priv;
2306         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2307         int ret = 0;
2308
2309         mutex_lock(&ar->conf_mutex);
2310
2311         if (old_state == IEEE80211_STA_NOTEXIST &&
2312             new_state == IEEE80211_STA_NONE &&
2313             vif->type != NL80211_IFTYPE_STATION) {
2314                 /*
2315                  * New station addition.
2316                  */
2317                 ret = ath10k_peer_create(ar, arvif->vdev_id, sta->addr);
2318                 if (ret)
2319                         ath10k_warn("Failed to add peer: %pM for VDEV: %d\n",
2320                                     sta->addr, arvif->vdev_id);
2321                 else
2322                         ath10k_dbg(ATH10K_DBG_MAC,
2323                                    "Added peer: %pM for VDEV: %d\n",
2324                                    sta->addr, arvif->vdev_id);
2325         } else if ((old_state == IEEE80211_STA_NONE &&
2326                     new_state == IEEE80211_STA_NOTEXIST)) {
2327                 /*
2328                  * Existing station deletion.
2329                  */
2330                 ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
2331                 if (ret)
2332                         ath10k_warn("Failed to delete peer: %pM for VDEV: %d\n",
2333                                     sta->addr, arvif->vdev_id);
2334                 else
2335                         ath10k_dbg(ATH10K_DBG_MAC,
2336                                    "Removed peer: %pM for VDEV: %d\n",
2337                                    sta->addr, arvif->vdev_id);
2338
2339                 if (vif->type == NL80211_IFTYPE_STATION)
2340                         ath10k_bss_disassoc(hw, vif);
2341         } else if (old_state == IEEE80211_STA_AUTH &&
2342                    new_state == IEEE80211_STA_ASSOC &&
2343                    (vif->type == NL80211_IFTYPE_AP ||
2344                     vif->type == NL80211_IFTYPE_ADHOC)) {
2345                 /*
2346                  * New association.
2347                  */
2348                 ret = ath10k_station_assoc(ar, arvif, sta);
2349                 if (ret)
2350                         ath10k_warn("Failed to associate station: %pM\n",
2351                                     sta->addr);
2352                 else
2353                         ath10k_dbg(ATH10K_DBG_MAC,
2354                                    "Station %pM moved to assoc state\n",
2355                                    sta->addr);
2356         } else if (old_state == IEEE80211_STA_ASSOC &&
2357                    new_state == IEEE80211_STA_AUTH &&
2358                    (vif->type == NL80211_IFTYPE_AP ||
2359                     vif->type == NL80211_IFTYPE_ADHOC)) {
2360                 /*
2361                  * Disassociation.
2362                  */
2363                 ret = ath10k_station_disassoc(ar, arvif, sta);
2364                 if (ret)
2365                         ath10k_warn("Failed to disassociate station: %pM\n",
2366                                     sta->addr);
2367                 else
2368                         ath10k_dbg(ATH10K_DBG_MAC,
2369                                    "Station %pM moved to disassociated state\n",
2370                                    sta->addr);
2371         }
2372
2373         mutex_unlock(&ar->conf_mutex);
2374         return ret;
2375 }
2376
2377 static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
2378                                  u16 ac, bool enable)
2379 {
2380         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2381         u32 value = 0;
2382         int ret = 0;
2383
2384         if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
2385                 return 0;
2386
2387         switch (ac) {
2388         case IEEE80211_AC_VO:
2389                 value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
2390                         WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
2391                 break;
2392         case IEEE80211_AC_VI:
2393                 value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
2394                         WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
2395                 break;
2396         case IEEE80211_AC_BE:
2397                 value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
2398                         WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
2399                 break;
2400         case IEEE80211_AC_BK:
2401                 value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
2402                         WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
2403                 break;
2404         }
2405
2406         if (enable)
2407                 arvif->u.sta.uapsd |= value;
2408         else
2409                 arvif->u.sta.uapsd &= ~value;
2410
2411         ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
2412                                           WMI_STA_PS_PARAM_UAPSD,
2413                                           arvif->u.sta.uapsd);
2414         if (ret) {
2415                 ath10k_warn("could not set uapsd params %d\n", ret);
2416                 goto exit;
2417         }
2418
2419         if (arvif->u.sta.uapsd)
2420                 value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
2421         else
2422                 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
2423
2424         ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
2425                                           WMI_STA_PS_PARAM_RX_WAKE_POLICY,
2426                                           value);
2427         if (ret)
2428                 ath10k_warn("could not set rx wake param %d\n", ret);
2429
2430 exit:
2431         return ret;
2432 }
2433
2434 static int ath10k_conf_tx(struct ieee80211_hw *hw,
2435                           struct ieee80211_vif *vif, u16 ac,
2436                           const struct ieee80211_tx_queue_params *params)
2437 {
2438         struct ath10k *ar = hw->priv;
2439         struct wmi_wmm_params_arg *p = NULL;
2440         int ret;
2441
2442         mutex_lock(&ar->conf_mutex);
2443
2444         switch (ac) {
2445         case IEEE80211_AC_VO:
2446                 p = &ar->wmm_params.ac_vo;
2447                 break;
2448         case IEEE80211_AC_VI:
2449                 p = &ar->wmm_params.ac_vi;
2450                 break;
2451         case IEEE80211_AC_BE:
2452                 p = &ar->wmm_params.ac_be;
2453                 break;
2454         case IEEE80211_AC_BK:
2455                 p = &ar->wmm_params.ac_bk;
2456                 break;
2457         }
2458
2459         if (WARN_ON(!p)) {
2460                 ret = -EINVAL;
2461                 goto exit;
2462         }
2463
2464         p->cwmin = params->cw_min;
2465         p->cwmax = params->cw_max;
2466         p->aifs = params->aifs;
2467
2468         /*
2469          * The channel time duration programmed in the HW is in absolute
2470          * microseconds, while mac80211 gives the txop in units of
2471          * 32 microseconds.
2472          */
2473         p->txop = params->txop * 32;
2474
2475         /* FIXME: FW accepts wmm params per hw, not per vif */
2476         ret = ath10k_wmi_pdev_set_wmm_params(ar, &ar->wmm_params);
2477         if (ret) {
2478                 ath10k_warn("could not set wmm params %d\n", ret);
2479                 goto exit;
2480         }
2481
2482         ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
2483         if (ret)
2484                 ath10k_warn("could not set sta uapsd %d\n", ret);
2485
2486 exit:
2487         mutex_unlock(&ar->conf_mutex);
2488         return ret;
2489 }
2490
2491 #define ATH10K_ROC_TIMEOUT_HZ (2*HZ)
2492
2493 static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
2494                                     struct ieee80211_vif *vif,
2495                                     struct ieee80211_channel *chan,
2496                                     int duration,
2497                                     enum ieee80211_roc_type type)
2498 {
2499         struct ath10k *ar = hw->priv;
2500         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2501         struct wmi_start_scan_arg arg;
2502         int ret;
2503
2504         mutex_lock(&ar->conf_mutex);
2505
2506         spin_lock_bh(&ar->data_lock);
2507         if (ar->scan.in_progress) {
2508                 spin_unlock_bh(&ar->data_lock);
2509                 ret = -EBUSY;
2510                 goto exit;
2511         }
2512
2513         INIT_COMPLETION(ar->scan.started);
2514         INIT_COMPLETION(ar->scan.completed);
2515         INIT_COMPLETION(ar->scan.on_channel);
2516         ar->scan.in_progress = true;
2517         ar->scan.aborting = false;
2518         ar->scan.is_roc = true;
2519         ar->scan.vdev_id = arvif->vdev_id;
2520         ar->scan.roc_freq = chan->center_freq;
2521         spin_unlock_bh(&ar->data_lock);
2522
2523         memset(&arg, 0, sizeof(arg));
2524         ath10k_wmi_start_scan_init(ar, &arg);
2525         arg.vdev_id = arvif->vdev_id;
2526         arg.scan_id = ATH10K_SCAN_ID;
2527         arg.n_channels = 1;
2528         arg.channels[0] = chan->center_freq;
2529         arg.dwell_time_active = duration;
2530         arg.dwell_time_passive = duration;
2531         arg.max_scan_time = 2 * duration;
2532         arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
2533         arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
2534
2535         ret = ath10k_start_scan(ar, &arg);
2536         if (ret) {
2537                 ath10k_warn("could not start roc scan (%d)\n", ret);
2538                 spin_lock_bh(&ar->data_lock);
2539                 ar->scan.in_progress = false;
2540                 spin_unlock_bh(&ar->data_lock);
2541                 goto exit;
2542         }
2543
2544         ret = wait_for_completion_timeout(&ar->scan.on_channel, 3*HZ);
2545         if (ret == 0) {
2546                 ath10k_warn("could not switch to channel for roc scan\n");
2547                 ath10k_abort_scan(ar);
2548                 ret = -ETIMEDOUT;
2549                 goto exit;
2550         }
2551
2552         ret = 0;
2553 exit:
2554         mutex_unlock(&ar->conf_mutex);
2555         return ret;
2556 }
2557
2558 static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw)
2559 {
2560         struct ath10k *ar = hw->priv;
2561
2562         mutex_lock(&ar->conf_mutex);
2563         ath10k_abort_scan(ar);
2564         mutex_unlock(&ar->conf_mutex);
2565
2566         return 0;
2567 }
2568
2569 /*
2570  * Both RTS and Fragmentation threshold are interface-specific
2571  * in ath10k, but device-specific in mac80211.
2572  */
2573 static void ath10k_set_rts_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
2574 {
2575         struct ath10k_generic_iter *ar_iter = data;
2576         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2577         u32 rts = ar_iter->ar->hw->wiphy->rts_threshold;
2578
2579         rts = min_t(u32, rts, ATH10K_RTS_MAX);
2580
2581         ar_iter->ret = ath10k_wmi_vdev_set_param(ar_iter->ar, arvif->vdev_id,
2582                                                  WMI_VDEV_PARAM_RTS_THRESHOLD,
2583                                                  rts);
2584         if (ar_iter->ret)
2585                 ath10k_warn("Failed to set RTS threshold for VDEV: %d\n",
2586                             arvif->vdev_id);
2587         else
2588                 ath10k_dbg(ATH10K_DBG_MAC,
2589                            "Set RTS threshold: %d for VDEV: %d\n",
2590                            rts, arvif->vdev_id);
2591 }
2592
2593 static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
2594 {
2595         struct ath10k_generic_iter ar_iter;
2596         struct ath10k *ar = hw->priv;
2597
2598         memset(&ar_iter, 0, sizeof(struct ath10k_generic_iter));
2599         ar_iter.ar = ar;
2600
2601         mutex_lock(&ar->conf_mutex);
2602         ieee80211_iterate_active_interfaces_atomic(
2603                 hw, IEEE80211_IFACE_ITER_NORMAL,
2604                 ath10k_set_rts_iter, &ar_iter);
2605         mutex_unlock(&ar->conf_mutex);
2606
2607         return ar_iter.ret;
2608 }
2609
2610 static void ath10k_set_frag_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
2611 {
2612         struct ath10k_generic_iter *ar_iter = data;
2613         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2614         u32 frag = ar_iter->ar->hw->wiphy->frag_threshold;
2615         int ret;
2616
2617         frag = clamp_t(u32, frag,
2618                        ATH10K_FRAGMT_THRESHOLD_MIN,
2619                        ATH10K_FRAGMT_THRESHOLD_MAX);
2620
2621         ret = ath10k_wmi_vdev_set_param(ar_iter->ar, arvif->vdev_id,
2622                                         WMI_VDEV_PARAM_FRAGMENTATION_THRESHOLD,
2623                                         frag);
2624
2625         ar_iter->ret = ret;
2626         if (ar_iter->ret)
2627                 ath10k_warn("Failed to set frag threshold for VDEV: %d\n",
2628                             arvif->vdev_id);
2629         else
2630                 ath10k_dbg(ATH10K_DBG_MAC,
2631                            "Set frag threshold: %d for VDEV: %d\n",
2632                            frag, arvif->vdev_id);
2633 }
2634
2635 static int ath10k_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
2636 {
2637         struct ath10k_generic_iter ar_iter;
2638         struct ath10k *ar = hw->priv;
2639
2640         memset(&ar_iter, 0, sizeof(struct ath10k_generic_iter));
2641         ar_iter.ar = ar;
2642
2643         mutex_lock(&ar->conf_mutex);
2644         ieee80211_iterate_active_interfaces_atomic(
2645                 hw, IEEE80211_IFACE_ITER_NORMAL,
2646                 ath10k_set_frag_iter, &ar_iter);
2647         mutex_unlock(&ar->conf_mutex);
2648
2649         return ar_iter.ret;
2650 }
2651
2652 static void ath10k_flush(struct ieee80211_hw *hw, u32 queues, bool drop)
2653 {
2654         struct ath10k *ar = hw->priv;
2655         int ret;
2656
2657         /* mac80211 doesn't care if we really xmit queued frames or not
2658          * we'll collect those frames either way if we stop/delete vdevs */
2659         if (drop)
2660                 return;
2661
2662         ret = wait_event_timeout(ar->htt->empty_tx_wq, ({
2663                         bool empty;
2664                         spin_lock_bh(&ar->htt->tx_lock);
2665                         empty = bitmap_empty(ar->htt->used_msdu_ids,
2666                                              ar->htt->max_num_pending_tx);
2667                         spin_unlock_bh(&ar->htt->tx_lock);
2668                         (empty);
2669                 }), ATH10K_FLUSH_TIMEOUT_HZ);
2670         if (ret <= 0)
2671                 ath10k_warn("tx not flushed\n");
2672 }
2673
2674 /* TODO: Implement this function properly
2675  * For now it is needed to reply to Probe Requests in IBSS mode.
2676  * Propably we need this information from FW.
2677  */
2678 static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
2679 {
2680         return 1;
2681 }
2682
2683 static const struct ieee80211_ops ath10k_ops = {
2684         .tx                             = ath10k_tx,
2685         .start                          = ath10k_start,
2686         .stop                           = ath10k_stop,
2687         .config                         = ath10k_config,
2688         .add_interface                  = ath10k_add_interface,
2689         .remove_interface               = ath10k_remove_interface,
2690         .configure_filter               = ath10k_configure_filter,
2691         .bss_info_changed               = ath10k_bss_info_changed,
2692         .hw_scan                        = ath10k_hw_scan,
2693         .cancel_hw_scan                 = ath10k_cancel_hw_scan,
2694         .set_key                        = ath10k_set_key,
2695         .sta_state                      = ath10k_sta_state,
2696         .conf_tx                        = ath10k_conf_tx,
2697         .remain_on_channel              = ath10k_remain_on_channel,
2698         .cancel_remain_on_channel       = ath10k_cancel_remain_on_channel,
2699         .set_rts_threshold              = ath10k_set_rts_threshold,
2700         .set_frag_threshold             = ath10k_set_frag_threshold,
2701         .flush                          = ath10k_flush,
2702         .tx_last_beacon                 = ath10k_tx_last_beacon,
2703 };
2704
2705 #define RATETAB_ENT(_rate, _rateid, _flags) { \
2706         .bitrate                = (_rate), \
2707         .flags                  = (_flags), \
2708         .hw_value               = (_rateid), \
2709 }
2710
2711 #define CHAN2G(_channel, _freq, _flags) { \
2712         .band                   = IEEE80211_BAND_2GHZ, \
2713         .hw_value               = (_channel), \
2714         .center_freq            = (_freq), \
2715         .flags                  = (_flags), \
2716         .max_antenna_gain       = 0, \
2717         .max_power              = 30, \
2718 }
2719
2720 #define CHAN5G(_channel, _freq, _flags) { \
2721         .band                   = IEEE80211_BAND_5GHZ, \
2722         .hw_value               = (_channel), \
2723         .center_freq            = (_freq), \
2724         .flags                  = (_flags), \
2725         .max_antenna_gain       = 0, \
2726         .max_power              = 30, \
2727 }
2728
2729 static const struct ieee80211_channel ath10k_2ghz_channels[] = {
2730         CHAN2G(1, 2412, 0),
2731         CHAN2G(2, 2417, 0),
2732         CHAN2G(3, 2422, 0),
2733         CHAN2G(4, 2427, 0),
2734         CHAN2G(5, 2432, 0),
2735         CHAN2G(6, 2437, 0),
2736         CHAN2G(7, 2442, 0),
2737         CHAN2G(8, 2447, 0),
2738         CHAN2G(9, 2452, 0),
2739         CHAN2G(10, 2457, 0),
2740         CHAN2G(11, 2462, 0),
2741         CHAN2G(12, 2467, 0),
2742         CHAN2G(13, 2472, 0),
2743         CHAN2G(14, 2484, 0),
2744 };
2745
2746 static const struct ieee80211_channel ath10k_5ghz_channels[] = {
2747         CHAN5G(36, 5180, 0),
2748         CHAN5G(40, 5200, 0),
2749         CHAN5G(44, 5220, 0),
2750         CHAN5G(48, 5240, 0),
2751         CHAN5G(52, 5260, 0),
2752         CHAN5G(56, 5280, 0),
2753         CHAN5G(60, 5300, 0),
2754         CHAN5G(64, 5320, 0),
2755         CHAN5G(100, 5500, 0),
2756         CHAN5G(104, 5520, 0),
2757         CHAN5G(108, 5540, 0),
2758         CHAN5G(112, 5560, 0),
2759         CHAN5G(116, 5580, 0),
2760         CHAN5G(120, 5600, 0),
2761         CHAN5G(124, 5620, 0),
2762         CHAN5G(128, 5640, 0),
2763         CHAN5G(132, 5660, 0),
2764         CHAN5G(136, 5680, 0),
2765         CHAN5G(140, 5700, 0),
2766         CHAN5G(149, 5745, 0),
2767         CHAN5G(153, 5765, 0),
2768         CHAN5G(157, 5785, 0),
2769         CHAN5G(161, 5805, 0),
2770         CHAN5G(165, 5825, 0),
2771 };
2772
2773 static struct ieee80211_rate ath10k_rates[] = {
2774         /* CCK */
2775         RATETAB_ENT(10,  0x82, 0),
2776         RATETAB_ENT(20,  0x84, 0),
2777         RATETAB_ENT(55,  0x8b, 0),
2778         RATETAB_ENT(110, 0x96, 0),
2779         /* OFDM */
2780         RATETAB_ENT(60,  0x0c, 0),
2781         RATETAB_ENT(90,  0x12, 0),
2782         RATETAB_ENT(120, 0x18, 0),
2783         RATETAB_ENT(180, 0x24, 0),
2784         RATETAB_ENT(240, 0x30, 0),
2785         RATETAB_ENT(360, 0x48, 0),
2786         RATETAB_ENT(480, 0x60, 0),
2787         RATETAB_ENT(540, 0x6c, 0),
2788 };
2789
2790 #define ath10k_a_rates (ath10k_rates + 4)
2791 #define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - 4)
2792 #define ath10k_g_rates (ath10k_rates + 0)
2793 #define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
2794
2795 struct ath10k *ath10k_mac_create(void)
2796 {
2797         struct ieee80211_hw *hw;
2798         struct ath10k *ar;
2799
2800         hw = ieee80211_alloc_hw(sizeof(struct ath10k), &ath10k_ops);
2801         if (!hw)
2802                 return NULL;
2803
2804         ar = hw->priv;
2805         ar->hw = hw;
2806
2807         return ar;
2808 }
2809
2810 void ath10k_mac_destroy(struct ath10k *ar)
2811 {
2812         ieee80211_free_hw(ar->hw);
2813 }
2814
2815 static const struct ieee80211_iface_limit ath10k_if_limits[] = {
2816         {
2817         .max    = 8,
2818         .types  = BIT(NL80211_IFTYPE_STATION)
2819                 | BIT(NL80211_IFTYPE_P2P_CLIENT)
2820                 | BIT(NL80211_IFTYPE_P2P_GO)
2821                 | BIT(NL80211_IFTYPE_AP)
2822         }
2823 };
2824
2825 static const struct ieee80211_iface_combination ath10k_if_comb = {
2826         .limits = ath10k_if_limits,
2827         .n_limits = ARRAY_SIZE(ath10k_if_limits),
2828         .max_interfaces = 8,
2829         .num_different_channels = 1,
2830         .beacon_int_infra_match = true,
2831 };
2832
2833 static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
2834 {
2835         struct ieee80211_sta_vht_cap vht_cap = {0};
2836         u16 mcs_map;
2837
2838         vht_cap.vht_supported = 1;
2839         vht_cap.cap = ar->vht_cap_info;
2840
2841         /* FIXME: check dynamically how many streams board supports */
2842         mcs_map = IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
2843                 IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 |
2844                 IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 |
2845                 IEEE80211_VHT_MCS_NOT_SUPPORTED << 6 |
2846                 IEEE80211_VHT_MCS_NOT_SUPPORTED << 8 |
2847                 IEEE80211_VHT_MCS_NOT_SUPPORTED << 10 |
2848                 IEEE80211_VHT_MCS_NOT_SUPPORTED << 12 |
2849                 IEEE80211_VHT_MCS_NOT_SUPPORTED << 14;
2850
2851         vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
2852         vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
2853
2854         return vht_cap;
2855 }
2856
2857 static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
2858 {
2859         int i;
2860         struct ieee80211_sta_ht_cap ht_cap = {0};
2861
2862         if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
2863                 return ht_cap;
2864
2865         ht_cap.ht_supported = 1;
2866         ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
2867         ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
2868         ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
2869         ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
2870         ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
2871
2872         if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
2873                 ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
2874
2875         if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
2876                 ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
2877
2878         if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
2879                 u32 smps;
2880
2881                 smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
2882                 smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
2883
2884                 ht_cap.cap |= smps;
2885         }
2886
2887         if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC)
2888                 ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
2889
2890         if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
2891                 u32 stbc;
2892
2893                 stbc   = ar->ht_cap_info;
2894                 stbc  &= WMI_HT_CAP_RX_STBC;
2895                 stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
2896                 stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
2897                 stbc  &= IEEE80211_HT_CAP_RX_STBC;
2898
2899                 ht_cap.cap |= stbc;
2900         }
2901
2902         if (ar->ht_cap_info & WMI_HT_CAP_LDPC)
2903                 ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
2904
2905         if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
2906                 ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
2907
2908         /* max AMSDU is implicitly taken from vht_cap_info */
2909         if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
2910                 ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
2911
2912         for (i = 0; i < WMI_MAX_SPATIAL_STREAM; i++)
2913                 ht_cap.mcs.rx_mask[i] = 0xFF;
2914
2915         ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
2916
2917         return ht_cap;
2918 }
2919
2920
2921 static void ath10k_get_arvif_iter(void *data, u8 *mac,
2922                                   struct ieee80211_vif *vif)
2923 {
2924         struct ath10k_vif_iter *arvif_iter = data;
2925         struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2926
2927         if (arvif->vdev_id == arvif_iter->vdev_id)
2928                 arvif_iter->arvif = arvif;
2929 }
2930
2931 struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
2932 {
2933         struct ath10k_vif_iter arvif_iter;
2934         u32 flags;
2935
2936         memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
2937         arvif_iter.vdev_id = vdev_id;
2938
2939         flags = IEEE80211_IFACE_ITER_RESUME_ALL;
2940         ieee80211_iterate_active_interfaces_atomic(ar->hw,
2941                                                    flags,
2942                                                    ath10k_get_arvif_iter,
2943                                                    &arvif_iter);
2944         if (!arvif_iter.arvif) {
2945                 ath10k_warn("No VIF found for VDEV: %d\n", vdev_id);
2946                 return NULL;
2947         }
2948
2949         return arvif_iter.arvif;
2950 }
2951
2952 int ath10k_mac_register(struct ath10k *ar)
2953 {
2954         struct ieee80211_supported_band *band;
2955         struct ieee80211_sta_vht_cap vht_cap;
2956         struct ieee80211_sta_ht_cap ht_cap;
2957         void *channels;
2958         int ret;
2959
2960         SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
2961
2962         SET_IEEE80211_DEV(ar->hw, ar->dev);
2963
2964         ht_cap = ath10k_get_ht_cap(ar);
2965         vht_cap = ath10k_create_vht_cap(ar);
2966
2967         if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
2968                 channels = kmemdup(ath10k_2ghz_channels,
2969                                    sizeof(ath10k_2ghz_channels),
2970                                    GFP_KERNEL);
2971                 if (!channels)
2972                         return -ENOMEM;
2973
2974                 band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
2975                 band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
2976                 band->channels = channels;
2977                 band->n_bitrates = ath10k_g_rates_size;
2978                 band->bitrates = ath10k_g_rates;
2979                 band->ht_cap = ht_cap;
2980
2981                 /* vht is not supported in 2.4 GHz */
2982
2983                 ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
2984         }
2985
2986         if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
2987                 channels = kmemdup(ath10k_5ghz_channels,
2988                                    sizeof(ath10k_5ghz_channels),
2989                                    GFP_KERNEL);
2990                 if (!channels) {
2991                         if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
2992                                 band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
2993                                 kfree(band->channels);
2994                         }
2995                         return -ENOMEM;
2996                 }
2997
2998                 band = &ar->mac.sbands[IEEE80211_BAND_5GHZ];
2999                 band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
3000                 band->channels = channels;
3001                 band->n_bitrates = ath10k_a_rates_size;
3002                 band->bitrates = ath10k_a_rates;
3003                 band->ht_cap = ht_cap;
3004                 band->vht_cap = vht_cap;
3005                 ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
3006         }
3007
3008         ar->hw->wiphy->interface_modes =
3009                 BIT(NL80211_IFTYPE_STATION) |
3010                 BIT(NL80211_IFTYPE_ADHOC) |
3011                 BIT(NL80211_IFTYPE_AP) |
3012                 BIT(NL80211_IFTYPE_P2P_CLIENT) |
3013                 BIT(NL80211_IFTYPE_P2P_GO);
3014
3015         ar->hw->flags = IEEE80211_HW_SIGNAL_DBM |
3016                         IEEE80211_HW_SUPPORTS_PS |
3017                         IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
3018                         IEEE80211_HW_SUPPORTS_UAPSD |
3019                         IEEE80211_HW_MFP_CAPABLE |
3020                         IEEE80211_HW_REPORTS_TX_ACK_STATUS |
3021                         IEEE80211_HW_HAS_RATE_CONTROL |
3022                         IEEE80211_HW_SUPPORTS_STATIC_SMPS |
3023                         IEEE80211_HW_WANT_MONITOR_VIF |
3024                         IEEE80211_HW_AP_LINK_PS;
3025
3026         if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
3027                 ar->hw->flags |= IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS;
3028
3029         if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
3030                 ar->hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
3031                 ar->hw->flags |= IEEE80211_HW_TX_AMPDU_SETUP_IN_HW;
3032         }
3033
3034         ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
3035         ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
3036
3037         ar->hw->vif_data_size = sizeof(struct ath10k_vif);
3038
3039         ar->hw->channel_change_time = 5000;
3040         ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
3041
3042         ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
3043         ar->hw->wiphy->max_remain_on_channel_duration = 5000;
3044
3045         ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
3046         /*
3047          * on LL hardware queues are managed entirely by the FW
3048          * so we only advertise to mac we can do the queues thing
3049          */
3050         ar->hw->queues = 4;
3051
3052         ar->hw->wiphy->iface_combinations = &ath10k_if_comb;
3053         ar->hw->wiphy->n_iface_combinations = 1;
3054
3055         ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
3056                             ath10k_reg_notifier);
3057         if (ret) {
3058                 ath10k_err("Regulatory initialization failed\n");
3059                 return ret;
3060         }
3061
3062         ret = ieee80211_register_hw(ar->hw);
3063         if (ret) {
3064                 ath10k_err("ieee80211 registration failed: %d\n", ret);
3065                 return ret;
3066         }
3067
3068         if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
3069                 ret = regulatory_hint(ar->hw->wiphy,
3070                                       ar->ath_common.regulatory.alpha2);
3071                 if (ret)
3072                         goto exit;
3073         }
3074
3075         return 0;
3076 exit:
3077         ieee80211_unregister_hw(ar->hw);
3078         return ret;
3079 }
3080
3081 void ath10k_mac_unregister(struct ath10k *ar)
3082 {
3083         ieee80211_unregister_hw(ar->hw);
3084
3085         kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
3086         kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
3087
3088         SET_IEEE80211_DEV(ar->hw, NULL);
3089 }