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