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