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