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