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