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