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