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