Merge tag 'wireless-drivers-next-for-davem-2016-07-13' of git://git.kernel.org/pub...
[cascardo/linux.git] / drivers / net / wireless / ath / ath6kl / cfg80211.c
1 /*
2  * Copyright (c) 2004-2011 Atheros Communications Inc.
3  * Copyright (c) 2011-2012 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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
20 #include <linux/moduleparam.h>
21 #include <linux/inetdevice.h>
22 #include <linux/export.h>
23
24 #include "core.h"
25 #include "cfg80211.h"
26 #include "debug.h"
27 #include "hif-ops.h"
28 #include "testmode.h"
29
30 #define RATETAB_ENT(_rate, _rateid, _flags) {   \
31         .bitrate    = (_rate),                  \
32         .flags      = (_flags),                 \
33         .hw_value   = (_rateid),                \
34 }
35
36 #define CHAN2G(_channel, _freq, _flags) {   \
37         .band           = NL80211_BAND_2GHZ,  \
38         .hw_value       = (_channel),           \
39         .center_freq    = (_freq),              \
40         .flags          = (_flags),             \
41         .max_antenna_gain   = 0,                \
42         .max_power      = 30,                   \
43 }
44
45 #define CHAN5G(_channel, _flags) {                  \
46         .band           = NL80211_BAND_5GHZ,      \
47         .hw_value       = (_channel),               \
48         .center_freq    = 5000 + (5 * (_channel)),  \
49         .flags          = (_flags),                 \
50         .max_antenna_gain   = 0,                    \
51         .max_power      = 30,                       \
52 }
53
54 #define DEFAULT_BG_SCAN_PERIOD 60
55
56 struct ath6kl_cfg80211_match_probe_ssid {
57         struct cfg80211_ssid ssid;
58         u8 flag;
59 };
60
61 static struct ieee80211_rate ath6kl_rates[] = {
62         RATETAB_ENT(10, 0x1, 0),
63         RATETAB_ENT(20, 0x2, 0),
64         RATETAB_ENT(55, 0x4, 0),
65         RATETAB_ENT(110, 0x8, 0),
66         RATETAB_ENT(60, 0x10, 0),
67         RATETAB_ENT(90, 0x20, 0),
68         RATETAB_ENT(120, 0x40, 0),
69         RATETAB_ENT(180, 0x80, 0),
70         RATETAB_ENT(240, 0x100, 0),
71         RATETAB_ENT(360, 0x200, 0),
72         RATETAB_ENT(480, 0x400, 0),
73         RATETAB_ENT(540, 0x800, 0),
74 };
75
76 #define ath6kl_a_rates     (ath6kl_rates + 4)
77 #define ath6kl_a_rates_size    8
78 #define ath6kl_g_rates     (ath6kl_rates + 0)
79 #define ath6kl_g_rates_size    12
80
81 #define ath6kl_g_htcap IEEE80211_HT_CAP_SGI_20
82 #define ath6kl_a_htcap (IEEE80211_HT_CAP_SUP_WIDTH_20_40 | \
83                         IEEE80211_HT_CAP_SGI_20          | \
84                         IEEE80211_HT_CAP_SGI_40)
85
86 static struct ieee80211_channel ath6kl_2ghz_channels[] = {
87         CHAN2G(1, 2412, 0),
88         CHAN2G(2, 2417, 0),
89         CHAN2G(3, 2422, 0),
90         CHAN2G(4, 2427, 0),
91         CHAN2G(5, 2432, 0),
92         CHAN2G(6, 2437, 0),
93         CHAN2G(7, 2442, 0),
94         CHAN2G(8, 2447, 0),
95         CHAN2G(9, 2452, 0),
96         CHAN2G(10, 2457, 0),
97         CHAN2G(11, 2462, 0),
98         CHAN2G(12, 2467, 0),
99         CHAN2G(13, 2472, 0),
100         CHAN2G(14, 2484, 0),
101 };
102
103 static struct ieee80211_channel ath6kl_5ghz_a_channels[] = {
104         CHAN5G(34, 0), CHAN5G(36, 0),
105         CHAN5G(38, 0), CHAN5G(40, 0),
106         CHAN5G(42, 0), CHAN5G(44, 0),
107         CHAN5G(46, 0), CHAN5G(48, 0),
108         CHAN5G(52, 0), CHAN5G(56, 0),
109         CHAN5G(60, 0), CHAN5G(64, 0),
110         CHAN5G(100, 0), CHAN5G(104, 0),
111         CHAN5G(108, 0), CHAN5G(112, 0),
112         CHAN5G(116, 0), CHAN5G(120, 0),
113         CHAN5G(124, 0), CHAN5G(128, 0),
114         CHAN5G(132, 0), CHAN5G(136, 0),
115         CHAN5G(140, 0), CHAN5G(149, 0),
116         CHAN5G(153, 0), CHAN5G(157, 0),
117         CHAN5G(161, 0), CHAN5G(165, 0),
118         CHAN5G(184, 0), CHAN5G(188, 0),
119         CHAN5G(192, 0), CHAN5G(196, 0),
120         CHAN5G(200, 0), CHAN5G(204, 0),
121         CHAN5G(208, 0), CHAN5G(212, 0),
122         CHAN5G(216, 0),
123 };
124
125 static struct ieee80211_supported_band ath6kl_band_2ghz = {
126         .n_channels = ARRAY_SIZE(ath6kl_2ghz_channels),
127         .channels = ath6kl_2ghz_channels,
128         .n_bitrates = ath6kl_g_rates_size,
129         .bitrates = ath6kl_g_rates,
130         .ht_cap.cap = ath6kl_g_htcap,
131         .ht_cap.ht_supported = true,
132 };
133
134 static struct ieee80211_supported_band ath6kl_band_5ghz = {
135         .n_channels = ARRAY_SIZE(ath6kl_5ghz_a_channels),
136         .channels = ath6kl_5ghz_a_channels,
137         .n_bitrates = ath6kl_a_rates_size,
138         .bitrates = ath6kl_a_rates,
139         .ht_cap.cap = ath6kl_a_htcap,
140         .ht_cap.ht_supported = true,
141 };
142
143 #define CCKM_KRK_CIPHER_SUITE 0x004096ff /* use for KRK */
144
145 /* returns true if scheduled scan was stopped */
146 static bool __ath6kl_cfg80211_sscan_stop(struct ath6kl_vif *vif)
147 {
148         struct ath6kl *ar = vif->ar;
149
150         if (!test_and_clear_bit(SCHED_SCANNING, &vif->flags))
151                 return false;
152
153         del_timer_sync(&vif->sched_scan_timer);
154
155         if (ar->state == ATH6KL_STATE_RECOVERY)
156                 return true;
157
158         ath6kl_wmi_enable_sched_scan_cmd(ar->wmi, vif->fw_vif_idx, false);
159
160         return true;
161 }
162
163 static void ath6kl_cfg80211_sscan_disable(struct ath6kl_vif *vif)
164 {
165         struct ath6kl *ar = vif->ar;
166         bool stopped;
167
168         stopped = __ath6kl_cfg80211_sscan_stop(vif);
169
170         if (!stopped)
171                 return;
172
173         cfg80211_sched_scan_stopped(ar->wiphy);
174 }
175
176 static int ath6kl_set_wpa_version(struct ath6kl_vif *vif,
177                                   enum nl80211_wpa_versions wpa_version)
178 {
179         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: %u\n", __func__, wpa_version);
180
181         if (!wpa_version) {
182                 vif->auth_mode = NONE_AUTH;
183         } else if (wpa_version & NL80211_WPA_VERSION_2) {
184                 vif->auth_mode = WPA2_AUTH;
185         } else if (wpa_version & NL80211_WPA_VERSION_1) {
186                 vif->auth_mode = WPA_AUTH;
187         } else {
188                 ath6kl_err("%s: %u not supported\n", __func__, wpa_version);
189                 return -ENOTSUPP;
190         }
191
192         return 0;
193 }
194
195 static int ath6kl_set_auth_type(struct ath6kl_vif *vif,
196                                 enum nl80211_auth_type auth_type)
197 {
198         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: 0x%x\n", __func__, auth_type);
199
200         switch (auth_type) {
201         case NL80211_AUTHTYPE_OPEN_SYSTEM:
202                 vif->dot11_auth_mode = OPEN_AUTH;
203                 break;
204         case NL80211_AUTHTYPE_SHARED_KEY:
205                 vif->dot11_auth_mode = SHARED_AUTH;
206                 break;
207         case NL80211_AUTHTYPE_NETWORK_EAP:
208                 vif->dot11_auth_mode = LEAP_AUTH;
209                 break;
210
211         case NL80211_AUTHTYPE_AUTOMATIC:
212                 vif->dot11_auth_mode = OPEN_AUTH | SHARED_AUTH;
213                 break;
214
215         default:
216                 ath6kl_err("%s: 0x%x not supported\n", __func__, auth_type);
217                 return -ENOTSUPP;
218         }
219
220         return 0;
221 }
222
223 static int ath6kl_set_cipher(struct ath6kl_vif *vif, u32 cipher, bool ucast)
224 {
225         u8 *ar_cipher = ucast ? &vif->prwise_crypto : &vif->grp_crypto;
226         u8 *ar_cipher_len = ucast ? &vif->prwise_crypto_len :
227                 &vif->grp_crypto_len;
228
229         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: cipher 0x%x, ucast %u\n",
230                    __func__, cipher, ucast);
231
232         switch (cipher) {
233         case 0:
234                 /* our own hack to use value 0 as no crypto used */
235                 *ar_cipher = NONE_CRYPT;
236                 *ar_cipher_len = 0;
237                 break;
238         case WLAN_CIPHER_SUITE_WEP40:
239                 *ar_cipher = WEP_CRYPT;
240                 *ar_cipher_len = 5;
241                 break;
242         case WLAN_CIPHER_SUITE_WEP104:
243                 *ar_cipher = WEP_CRYPT;
244                 *ar_cipher_len = 13;
245                 break;
246         case WLAN_CIPHER_SUITE_TKIP:
247                 *ar_cipher = TKIP_CRYPT;
248                 *ar_cipher_len = 0;
249                 break;
250         case WLAN_CIPHER_SUITE_CCMP:
251                 *ar_cipher = AES_CRYPT;
252                 *ar_cipher_len = 0;
253                 break;
254         case WLAN_CIPHER_SUITE_SMS4:
255                 *ar_cipher = WAPI_CRYPT;
256                 *ar_cipher_len = 0;
257                 break;
258         default:
259                 ath6kl_err("cipher 0x%x not supported\n", cipher);
260                 return -ENOTSUPP;
261         }
262
263         return 0;
264 }
265
266 static void ath6kl_set_key_mgmt(struct ath6kl_vif *vif, u32 key_mgmt)
267 {
268         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: 0x%x\n", __func__, key_mgmt);
269
270         if (key_mgmt == WLAN_AKM_SUITE_PSK) {
271                 if (vif->auth_mode == WPA_AUTH)
272                         vif->auth_mode = WPA_PSK_AUTH;
273                 else if (vif->auth_mode == WPA2_AUTH)
274                         vif->auth_mode = WPA2_PSK_AUTH;
275         } else if (key_mgmt == 0x00409600) {
276                 if (vif->auth_mode == WPA_AUTH)
277                         vif->auth_mode = WPA_AUTH_CCKM;
278                 else if (vif->auth_mode == WPA2_AUTH)
279                         vif->auth_mode = WPA2_AUTH_CCKM;
280         } else if (key_mgmt != WLAN_AKM_SUITE_8021X) {
281                 vif->auth_mode = NONE_AUTH;
282         }
283 }
284
285 static bool ath6kl_cfg80211_ready(struct ath6kl_vif *vif)
286 {
287         struct ath6kl *ar = vif->ar;
288
289         if (!test_bit(WMI_READY, &ar->flag)) {
290                 ath6kl_err("wmi is not ready\n");
291                 return false;
292         }
293
294         if (!test_bit(WLAN_ENABLED, &vif->flags)) {
295                 ath6kl_err("wlan disabled\n");
296                 return false;
297         }
298
299         return true;
300 }
301
302 static bool ath6kl_is_wpa_ie(const u8 *pos)
303 {
304         return pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
305                 pos[2] == 0x00 && pos[3] == 0x50 &&
306                 pos[4] == 0xf2 && pos[5] == 0x01;
307 }
308
309 static bool ath6kl_is_rsn_ie(const u8 *pos)
310 {
311         return pos[0] == WLAN_EID_RSN;
312 }
313
314 static bool ath6kl_is_wps_ie(const u8 *pos)
315 {
316         return (pos[0] == WLAN_EID_VENDOR_SPECIFIC &&
317                 pos[1] >= 4 &&
318                 pos[2] == 0x00 && pos[3] == 0x50 && pos[4] == 0xf2 &&
319                 pos[5] == 0x04);
320 }
321
322 static int ath6kl_set_assoc_req_ies(struct ath6kl_vif *vif, const u8 *ies,
323                                     size_t ies_len)
324 {
325         struct ath6kl *ar = vif->ar;
326         const u8 *pos;
327         u8 *buf = NULL;
328         size_t len = 0;
329         int ret;
330
331         /*
332          * Clear previously set flag
333          */
334
335         ar->connect_ctrl_flags &= ~CONNECT_WPS_FLAG;
336
337         /*
338          * Filter out RSN/WPA IE(s)
339          */
340
341         if (ies && ies_len) {
342                 buf = kmalloc(ies_len, GFP_KERNEL);
343                 if (buf == NULL)
344                         return -ENOMEM;
345                 pos = ies;
346
347                 while (pos + 1 < ies + ies_len) {
348                         if (pos + 2 + pos[1] > ies + ies_len)
349                                 break;
350                         if (!(ath6kl_is_wpa_ie(pos) || ath6kl_is_rsn_ie(pos))) {
351                                 memcpy(buf + len, pos, 2 + pos[1]);
352                                 len += 2 + pos[1];
353                         }
354
355                         if (ath6kl_is_wps_ie(pos))
356                                 ar->connect_ctrl_flags |= CONNECT_WPS_FLAG;
357
358                         pos += 2 + pos[1];
359                 }
360         }
361
362         ret = ath6kl_wmi_set_appie_cmd(ar->wmi, vif->fw_vif_idx,
363                                        WMI_FRAME_ASSOC_REQ, buf, len);
364         kfree(buf);
365         return ret;
366 }
367
368 static int ath6kl_nliftype_to_drv_iftype(enum nl80211_iftype type, u8 *nw_type)
369 {
370         switch (type) {
371         case NL80211_IFTYPE_STATION:
372         case NL80211_IFTYPE_P2P_CLIENT:
373                 *nw_type = INFRA_NETWORK;
374                 break;
375         case NL80211_IFTYPE_ADHOC:
376                 *nw_type = ADHOC_NETWORK;
377                 break;
378         case NL80211_IFTYPE_AP:
379         case NL80211_IFTYPE_P2P_GO:
380                 *nw_type = AP_NETWORK;
381                 break;
382         default:
383                 ath6kl_err("invalid interface type %u\n", type);
384                 return -ENOTSUPP;
385         }
386
387         return 0;
388 }
389
390 static bool ath6kl_is_valid_iftype(struct ath6kl *ar, enum nl80211_iftype type,
391                                    u8 *if_idx, u8 *nw_type)
392 {
393         int i;
394
395         if (ath6kl_nliftype_to_drv_iftype(type, nw_type))
396                 return false;
397
398         if (ar->ibss_if_active || ((type == NL80211_IFTYPE_ADHOC) &&
399                                    ar->num_vif))
400                 return false;
401
402         if (type == NL80211_IFTYPE_STATION ||
403             type == NL80211_IFTYPE_AP || type == NL80211_IFTYPE_ADHOC) {
404                 for (i = 0; i < ar->vif_max; i++) {
405                         if ((ar->avail_idx_map) & BIT(i)) {
406                                 *if_idx = i;
407                                 return true;
408                         }
409                 }
410         }
411
412         if (type == NL80211_IFTYPE_P2P_CLIENT ||
413             type == NL80211_IFTYPE_P2P_GO) {
414                 for (i = ar->max_norm_iface; i < ar->vif_max; i++) {
415                         if ((ar->avail_idx_map) & BIT(i)) {
416                                 *if_idx = i;
417                                 return true;
418                         }
419                 }
420         }
421
422         return false;
423 }
424
425 static bool ath6kl_is_tx_pending(struct ath6kl *ar)
426 {
427         return ar->tx_pending[ath6kl_wmi_get_control_ep(ar->wmi)] == 0;
428 }
429
430 static void ath6kl_cfg80211_sta_bmiss_enhance(struct ath6kl_vif *vif,
431                                               bool enable)
432 {
433         int err;
434
435         if (WARN_ON(!test_bit(WMI_READY, &vif->ar->flag)))
436                 return;
437
438         if (vif->nw_type != INFRA_NETWORK)
439                 return;
440
441         if (!test_bit(ATH6KL_FW_CAPABILITY_BMISS_ENHANCE,
442                       vif->ar->fw_capabilities))
443                 return;
444
445         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s fw bmiss enhance\n",
446                    enable ? "enable" : "disable");
447
448         err = ath6kl_wmi_sta_bmiss_enhance_cmd(vif->ar->wmi,
449                                                vif->fw_vif_idx, enable);
450         if (err)
451                 ath6kl_err("failed to %s enhanced bmiss detection: %d\n",
452                            enable ? "enable" : "disable", err);
453 }
454
455 static int ath6kl_cfg80211_connect(struct wiphy *wiphy, struct net_device *dev,
456                                    struct cfg80211_connect_params *sme)
457 {
458         struct ath6kl *ar = ath6kl_priv(dev);
459         struct ath6kl_vif *vif = netdev_priv(dev);
460         int status;
461         u8 nw_subtype = (ar->p2p) ? SUBTYPE_P2PDEV : SUBTYPE_NONE;
462         u16 interval;
463
464         ath6kl_cfg80211_sscan_disable(vif);
465
466         vif->sme_state = SME_CONNECTING;
467
468         if (!ath6kl_cfg80211_ready(vif))
469                 return -EIO;
470
471         if (test_bit(DESTROY_IN_PROGRESS, &ar->flag)) {
472                 ath6kl_err("destroy in progress\n");
473                 return -EBUSY;
474         }
475
476         if (test_bit(SKIP_SCAN, &ar->flag) &&
477             ((sme->channel && sme->channel->center_freq == 0) ||
478              (sme->bssid && is_zero_ether_addr(sme->bssid)))) {
479                 ath6kl_err("SkipScan: channel or bssid invalid\n");
480                 return -EINVAL;
481         }
482
483         if (down_interruptible(&ar->sem)) {
484                 ath6kl_err("busy, couldn't get access\n");
485                 return -ERESTARTSYS;
486         }
487
488         if (test_bit(DESTROY_IN_PROGRESS, &ar->flag)) {
489                 ath6kl_err("busy, destroy in progress\n");
490                 up(&ar->sem);
491                 return -EBUSY;
492         }
493
494         if (ar->tx_pending[ath6kl_wmi_get_control_ep(ar->wmi)]) {
495                 /*
496                  * sleep until the command queue drains
497                  */
498                 wait_event_interruptible_timeout(ar->event_wq,
499                                                  ath6kl_is_tx_pending(ar),
500                                                  WMI_TIMEOUT);
501                 if (signal_pending(current)) {
502                         ath6kl_err("cmd queue drain timeout\n");
503                         up(&ar->sem);
504                         return -EINTR;
505                 }
506         }
507
508         status = ath6kl_set_assoc_req_ies(vif, sme->ie, sme->ie_len);
509         if (status) {
510                 up(&ar->sem);
511                 return status;
512         }
513
514         if (sme->ie == NULL || sme->ie_len == 0)
515                 ar->connect_ctrl_flags &= ~CONNECT_WPS_FLAG;
516
517         if (test_bit(CONNECTED, &vif->flags) &&
518             vif->ssid_len == sme->ssid_len &&
519             !memcmp(vif->ssid, sme->ssid, vif->ssid_len)) {
520                 vif->reconnect_flag = true;
521                 status = ath6kl_wmi_reconnect_cmd(ar->wmi, vif->fw_vif_idx,
522                                                   vif->req_bssid,
523                                                   vif->ch_hint);
524
525                 up(&ar->sem);
526                 if (status) {
527                         ath6kl_err("wmi_reconnect_cmd failed\n");
528                         return -EIO;
529                 }
530                 return 0;
531         } else if (vif->ssid_len == sme->ssid_len &&
532                    !memcmp(vif->ssid, sme->ssid, vif->ssid_len)) {
533                 ath6kl_disconnect(vif);
534         }
535
536         memset(vif->ssid, 0, sizeof(vif->ssid));
537         vif->ssid_len = sme->ssid_len;
538         memcpy(vif->ssid, sme->ssid, sme->ssid_len);
539
540         if (sme->channel)
541                 vif->ch_hint = sme->channel->center_freq;
542
543         memset(vif->req_bssid, 0, sizeof(vif->req_bssid));
544         if (sme->bssid && !is_broadcast_ether_addr(sme->bssid))
545                 memcpy(vif->req_bssid, sme->bssid, sizeof(vif->req_bssid));
546
547         ath6kl_set_wpa_version(vif, sme->crypto.wpa_versions);
548
549         status = ath6kl_set_auth_type(vif, sme->auth_type);
550         if (status) {
551                 up(&ar->sem);
552                 return status;
553         }
554
555         if (sme->crypto.n_ciphers_pairwise)
556                 ath6kl_set_cipher(vif, sme->crypto.ciphers_pairwise[0], true);
557         else
558                 ath6kl_set_cipher(vif, 0, true);
559
560         ath6kl_set_cipher(vif, sme->crypto.cipher_group, false);
561
562         if (sme->crypto.n_akm_suites)
563                 ath6kl_set_key_mgmt(vif, sme->crypto.akm_suites[0]);
564
565         if ((sme->key_len) &&
566             (vif->auth_mode == NONE_AUTH) &&
567             (vif->prwise_crypto == WEP_CRYPT)) {
568                 struct ath6kl_key *key = NULL;
569
570                 if (sme->key_idx > WMI_MAX_KEY_INDEX) {
571                         ath6kl_err("key index %d out of bounds\n",
572                                    sme->key_idx);
573                         up(&ar->sem);
574                         return -ENOENT;
575                 }
576
577                 key = &vif->keys[sme->key_idx];
578                 key->key_len = sme->key_len;
579                 memcpy(key->key, sme->key, key->key_len);
580                 key->cipher = vif->prwise_crypto;
581                 vif->def_txkey_index = sme->key_idx;
582
583                 ath6kl_wmi_addkey_cmd(ar->wmi, vif->fw_vif_idx, sme->key_idx,
584                                       vif->prwise_crypto,
585                                       GROUP_USAGE | TX_USAGE,
586                                       key->key_len,
587                                       NULL, 0,
588                                       key->key, KEY_OP_INIT_VAL, NULL,
589                                       NO_SYNC_WMIFLAG);
590         }
591
592         if (!ar->usr_bss_filter) {
593                 clear_bit(CLEAR_BSSFILTER_ON_BEACON, &vif->flags);
594                 if (ath6kl_wmi_bssfilter_cmd(ar->wmi, vif->fw_vif_idx,
595                                              ALL_BSS_FILTER, 0) != 0) {
596                         ath6kl_err("couldn't set bss filtering\n");
597                         up(&ar->sem);
598                         return -EIO;
599                 }
600         }
601
602         vif->nw_type = vif->next_mode;
603
604         /* enable enhanced bmiss detection if applicable */
605         ath6kl_cfg80211_sta_bmiss_enhance(vif, true);
606
607         if (vif->wdev.iftype == NL80211_IFTYPE_P2P_CLIENT)
608                 nw_subtype = SUBTYPE_P2PCLIENT;
609
610         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
611                    "%s: connect called with authmode %d dot11 auth %d"
612                    " PW crypto %d PW crypto len %d GRP crypto %d"
613                    " GRP crypto len %d channel hint %u\n",
614                    __func__,
615                    vif->auth_mode, vif->dot11_auth_mode, vif->prwise_crypto,
616                    vif->prwise_crypto_len, vif->grp_crypto,
617                    vif->grp_crypto_len, vif->ch_hint);
618
619         vif->reconnect_flag = 0;
620
621         if (vif->nw_type == INFRA_NETWORK) {
622                 interval = max_t(u16, vif->listen_intvl_t,
623                                  ATH6KL_MAX_WOW_LISTEN_INTL);
624                 status = ath6kl_wmi_listeninterval_cmd(ar->wmi, vif->fw_vif_idx,
625                                                        interval,
626                                                        0);
627                 if (status) {
628                         ath6kl_err("couldn't set listen intervel\n");
629                         up(&ar->sem);
630                         return status;
631                 }
632         }
633
634         status = ath6kl_wmi_connect_cmd(ar->wmi, vif->fw_vif_idx, vif->nw_type,
635                                         vif->dot11_auth_mode, vif->auth_mode,
636                                         vif->prwise_crypto,
637                                         vif->prwise_crypto_len,
638                                         vif->grp_crypto, vif->grp_crypto_len,
639                                         vif->ssid_len, vif->ssid,
640                                         vif->req_bssid, vif->ch_hint,
641                                         ar->connect_ctrl_flags, nw_subtype);
642
643         if (sme->bg_scan_period == 0) {
644                 /* disable background scan if period is 0 */
645                 sme->bg_scan_period = 0xffff;
646         } else if (sme->bg_scan_period == -1) {
647                 /* configure default value if not specified */
648                 sme->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
649         }
650
651         ath6kl_wmi_scanparams_cmd(ar->wmi, vif->fw_vif_idx, 0, 0,
652                                   sme->bg_scan_period, 0, 0, 0, 3, 0, 0, 0);
653
654         up(&ar->sem);
655
656         if (status == -EINVAL) {
657                 memset(vif->ssid, 0, sizeof(vif->ssid));
658                 vif->ssid_len = 0;
659                 ath6kl_err("invalid request\n");
660                 return -ENOENT;
661         } else if (status) {
662                 ath6kl_err("ath6kl_wmi_connect_cmd failed\n");
663                 return -EIO;
664         }
665
666         if ((!(ar->connect_ctrl_flags & CONNECT_DO_WPA_OFFLOAD)) &&
667             ((vif->auth_mode == WPA_PSK_AUTH) ||
668              (vif->auth_mode == WPA2_PSK_AUTH))) {
669                 mod_timer(&vif->disconnect_timer,
670                           jiffies + msecs_to_jiffies(DISCON_TIMER_INTVAL));
671         }
672
673         ar->connect_ctrl_flags &= ~CONNECT_DO_WPA_OFFLOAD;
674         set_bit(CONNECT_PEND, &vif->flags);
675
676         return 0;
677 }
678
679 static struct cfg80211_bss *
680 ath6kl_add_bss_if_needed(struct ath6kl_vif *vif,
681                          enum network_type nw_type,
682                          const u8 *bssid,
683                          struct ieee80211_channel *chan,
684                          const u8 *beacon_ie,
685                          size_t beacon_ie_len)
686 {
687         struct ath6kl *ar = vif->ar;
688         struct cfg80211_bss *bss;
689         u16 cap_val;
690         enum ieee80211_bss_type bss_type;
691         u8 *ie;
692
693         if (nw_type & ADHOC_NETWORK) {
694                 cap_val = WLAN_CAPABILITY_IBSS;
695                 bss_type = IEEE80211_BSS_TYPE_IBSS;
696         } else {
697                 cap_val = WLAN_CAPABILITY_ESS;
698                 bss_type = IEEE80211_BSS_TYPE_ESS;
699         }
700
701         bss = cfg80211_get_bss(ar->wiphy, chan, bssid,
702                                vif->ssid, vif->ssid_len,
703                                bss_type, IEEE80211_PRIVACY_ANY);
704         if (bss == NULL) {
705                 /*
706                  * Since cfg80211 may not yet know about the BSS,
707                  * generate a partial entry until the first BSS info
708                  * event becomes available.
709                  *
710                  * Prepend SSID element since it is not included in the Beacon
711                  * IEs from the target.
712                  */
713                 ie = kmalloc(2 + vif->ssid_len + beacon_ie_len, GFP_KERNEL);
714                 if (ie == NULL)
715                         return NULL;
716                 ie[0] = WLAN_EID_SSID;
717                 ie[1] = vif->ssid_len;
718                 memcpy(ie + 2, vif->ssid, vif->ssid_len);
719                 memcpy(ie + 2 + vif->ssid_len, beacon_ie, beacon_ie_len);
720                 bss = cfg80211_inform_bss(ar->wiphy, chan,
721                                           CFG80211_BSS_FTYPE_UNKNOWN,
722                                           bssid, 0, cap_val, 100,
723                                           ie, 2 + vif->ssid_len + beacon_ie_len,
724                                           0, GFP_KERNEL);
725                 if (bss)
726                         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
727                                    "added bss %pM to cfg80211\n", bssid);
728                 kfree(ie);
729         } else {
730                 ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "cfg80211 already has a bss\n");
731         }
732
733         return bss;
734 }
735
736 void ath6kl_cfg80211_connect_event(struct ath6kl_vif *vif, u16 channel,
737                                    u8 *bssid, u16 listen_intvl,
738                                    u16 beacon_intvl,
739                                    enum network_type nw_type,
740                                    u8 beacon_ie_len, u8 assoc_req_len,
741                                    u8 assoc_resp_len, u8 *assoc_info)
742 {
743         struct ieee80211_channel *chan;
744         struct ath6kl *ar = vif->ar;
745         struct cfg80211_bss *bss;
746
747         /* capinfo + listen interval */
748         u8 assoc_req_ie_offset = sizeof(u16) + sizeof(u16);
749
750         /* capinfo + status code +  associd */
751         u8 assoc_resp_ie_offset = sizeof(u16) + sizeof(u16) + sizeof(u16);
752
753         u8 *assoc_req_ie = assoc_info + beacon_ie_len + assoc_req_ie_offset;
754         u8 *assoc_resp_ie = assoc_info + beacon_ie_len + assoc_req_len +
755             assoc_resp_ie_offset;
756
757         assoc_req_len -= assoc_req_ie_offset;
758         assoc_resp_len -= assoc_resp_ie_offset;
759
760         /*
761          * Store Beacon interval here; DTIM period will be available only once
762          * a Beacon frame from the AP is seen.
763          */
764         vif->assoc_bss_beacon_int = beacon_intvl;
765         clear_bit(DTIM_PERIOD_AVAIL, &vif->flags);
766
767         if (nw_type & ADHOC_NETWORK) {
768                 if (vif->wdev.iftype != NL80211_IFTYPE_ADHOC) {
769                         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
770                                    "%s: ath6k not in ibss mode\n", __func__);
771                         return;
772                 }
773         }
774
775         if (nw_type & INFRA_NETWORK) {
776                 if (vif->wdev.iftype != NL80211_IFTYPE_STATION &&
777                     vif->wdev.iftype != NL80211_IFTYPE_P2P_CLIENT) {
778                         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
779                                    "%s: ath6k not in station mode\n", __func__);
780                         return;
781                 }
782         }
783
784         chan = ieee80211_get_channel(ar->wiphy, (int) channel);
785
786         bss = ath6kl_add_bss_if_needed(vif, nw_type, bssid, chan,
787                                        assoc_info, beacon_ie_len);
788         if (!bss) {
789                 ath6kl_err("could not add cfg80211 bss entry\n");
790                 return;
791         }
792
793         if (nw_type & ADHOC_NETWORK) {
794                 ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "ad-hoc %s selected\n",
795                            nw_type & ADHOC_CREATOR ? "creator" : "joiner");
796                 cfg80211_ibss_joined(vif->ndev, bssid, chan, GFP_KERNEL);
797                 cfg80211_put_bss(ar->wiphy, bss);
798                 return;
799         }
800
801         if (vif->sme_state == SME_CONNECTING) {
802                 /* inform connect result to cfg80211 */
803                 vif->sme_state = SME_CONNECTED;
804                 cfg80211_connect_result(vif->ndev, bssid,
805                                         assoc_req_ie, assoc_req_len,
806                                         assoc_resp_ie, assoc_resp_len,
807                                         WLAN_STATUS_SUCCESS, GFP_KERNEL);
808                 cfg80211_put_bss(ar->wiphy, bss);
809         } else if (vif->sme_state == SME_CONNECTED) {
810                 /* inform roam event to cfg80211 */
811                 cfg80211_roamed_bss(vif->ndev, bss, assoc_req_ie, assoc_req_len,
812                                     assoc_resp_ie, assoc_resp_len, GFP_KERNEL);
813         }
814 }
815
816 static int ath6kl_cfg80211_disconnect(struct wiphy *wiphy,
817                                       struct net_device *dev, u16 reason_code)
818 {
819         struct ath6kl *ar = ath6kl_priv(dev);
820         struct ath6kl_vif *vif = netdev_priv(dev);
821
822         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: reason=%u\n", __func__,
823                    reason_code);
824
825         ath6kl_cfg80211_sscan_disable(vif);
826
827         if (!ath6kl_cfg80211_ready(vif))
828                 return -EIO;
829
830         if (test_bit(DESTROY_IN_PROGRESS, &ar->flag)) {
831                 ath6kl_err("busy, destroy in progress\n");
832                 return -EBUSY;
833         }
834
835         if (down_interruptible(&ar->sem)) {
836                 ath6kl_err("busy, couldn't get access\n");
837                 return -ERESTARTSYS;
838         }
839
840         vif->reconnect_flag = 0;
841         ath6kl_disconnect(vif);
842         memset(vif->ssid, 0, sizeof(vif->ssid));
843         vif->ssid_len = 0;
844
845         if (!test_bit(SKIP_SCAN, &ar->flag))
846                 memset(vif->req_bssid, 0, sizeof(vif->req_bssid));
847
848         up(&ar->sem);
849
850         vif->sme_state = SME_DISCONNECTED;
851
852         return 0;
853 }
854
855 void ath6kl_cfg80211_disconnect_event(struct ath6kl_vif *vif, u8 reason,
856                                       u8 *bssid, u8 assoc_resp_len,
857                                       u8 *assoc_info, u16 proto_reason)
858 {
859         struct ath6kl *ar = vif->ar;
860
861         if (vif->scan_req) {
862                 struct cfg80211_scan_info info = {
863                         .aborted = true,
864                 };
865
866                 cfg80211_scan_done(vif->scan_req, &info);
867                 vif->scan_req = NULL;
868         }
869
870         if (vif->nw_type & ADHOC_NETWORK) {
871                 if (vif->wdev.iftype != NL80211_IFTYPE_ADHOC)
872                         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
873                                    "%s: ath6k not in ibss mode\n", __func__);
874                 return;
875         }
876
877         if (vif->nw_type & INFRA_NETWORK) {
878                 if (vif->wdev.iftype != NL80211_IFTYPE_STATION &&
879                     vif->wdev.iftype != NL80211_IFTYPE_P2P_CLIENT) {
880                         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
881                                    "%s: ath6k not in station mode\n", __func__);
882                         return;
883                 }
884         }
885
886         clear_bit(CONNECT_PEND, &vif->flags);
887
888         if (vif->sme_state == SME_CONNECTING) {
889                 cfg80211_connect_result(vif->ndev,
890                                         bssid, NULL, 0,
891                                         NULL, 0,
892                                         WLAN_STATUS_UNSPECIFIED_FAILURE,
893                                         GFP_KERNEL);
894         } else if (vif->sme_state == SME_CONNECTED) {
895                 cfg80211_disconnected(vif->ndev, proto_reason,
896                                       NULL, 0, false, GFP_KERNEL);
897         }
898
899         vif->sme_state = SME_DISCONNECTED;
900
901         /*
902          * Send a disconnect command to target when a disconnect event is
903          * received with reason code other than 3 (DISCONNECT_CMD - disconnect
904          * request from host) to make the firmware stop trying to connect even
905          * after giving disconnect event. There will be one more disconnect
906          * event for this disconnect command with reason code DISCONNECT_CMD
907          * which won't be notified to cfg80211.
908          */
909         if (reason != DISCONNECT_CMD)
910                 ath6kl_wmi_disconnect_cmd(ar->wmi, vif->fw_vif_idx);
911 }
912
913 static int ath6kl_set_probed_ssids(struct ath6kl *ar,
914                                    struct ath6kl_vif *vif,
915                                    struct cfg80211_ssid *ssids, int n_ssids,
916                                    struct cfg80211_match_set *match_set,
917                                    int n_match_ssid)
918 {
919         u8 i, j, index_to_add, ssid_found = false;
920         struct ath6kl_cfg80211_match_probe_ssid ssid_list[MAX_PROBED_SSIDS];
921
922         memset(ssid_list, 0, sizeof(ssid_list));
923
924         if (n_ssids > MAX_PROBED_SSIDS ||
925             n_match_ssid > MAX_PROBED_SSIDS)
926                 return -EINVAL;
927
928         for (i = 0; i < n_ssids; i++) {
929                 memcpy(ssid_list[i].ssid.ssid,
930                        ssids[i].ssid,
931                        ssids[i].ssid_len);
932                 ssid_list[i].ssid.ssid_len = ssids[i].ssid_len;
933
934                 if (ssids[i].ssid_len)
935                         ssid_list[i].flag = SPECIFIC_SSID_FLAG;
936                 else
937                         ssid_list[i].flag = ANY_SSID_FLAG;
938
939                 if (n_match_ssid == 0)
940                         ssid_list[i].flag |= MATCH_SSID_FLAG;
941         }
942
943         index_to_add = i;
944
945         for (i = 0; i < n_match_ssid; i++) {
946                 ssid_found = false;
947
948                 for (j = 0; j < n_ssids; j++) {
949                         if ((match_set[i].ssid.ssid_len ==
950                              ssid_list[j].ssid.ssid_len) &&
951                             (!memcmp(ssid_list[j].ssid.ssid,
952                                      match_set[i].ssid.ssid,
953                                      match_set[i].ssid.ssid_len))) {
954                                 ssid_list[j].flag |= MATCH_SSID_FLAG;
955                                 ssid_found = true;
956                                 break;
957                         }
958                 }
959
960                 if (ssid_found)
961                         continue;
962
963                 if (index_to_add >= MAX_PROBED_SSIDS)
964                         continue;
965
966                 ssid_list[index_to_add].ssid.ssid_len =
967                         match_set[i].ssid.ssid_len;
968                 memcpy(ssid_list[index_to_add].ssid.ssid,
969                        match_set[i].ssid.ssid,
970                        match_set[i].ssid.ssid_len);
971                 ssid_list[index_to_add].flag |= MATCH_SSID_FLAG;
972                 index_to_add++;
973         }
974
975         for (i = 0; i < index_to_add; i++) {
976                 ath6kl_wmi_probedssid_cmd(ar->wmi, vif->fw_vif_idx, i,
977                                           ssid_list[i].flag,
978                                           ssid_list[i].ssid.ssid_len,
979                                           ssid_list[i].ssid.ssid);
980         }
981
982         /* Make sure no old entries are left behind */
983         for (i = index_to_add; i < MAX_PROBED_SSIDS; i++) {
984                 ath6kl_wmi_probedssid_cmd(ar->wmi, vif->fw_vif_idx, i,
985                                           DISABLE_SSID_FLAG, 0, NULL);
986         }
987
988         return 0;
989 }
990
991 static int ath6kl_cfg80211_scan(struct wiphy *wiphy,
992                                 struct cfg80211_scan_request *request)
993 {
994         struct ath6kl_vif *vif = ath6kl_vif_from_wdev(request->wdev);
995         struct ath6kl *ar = ath6kl_priv(vif->ndev);
996         s8 n_channels = 0;
997         u16 *channels = NULL;
998         int ret = 0;
999         u32 force_fg_scan = 0;
1000
1001         if (!ath6kl_cfg80211_ready(vif))
1002                 return -EIO;
1003
1004         ath6kl_cfg80211_sscan_disable(vif);
1005
1006         if (!ar->usr_bss_filter) {
1007                 clear_bit(CLEAR_BSSFILTER_ON_BEACON, &vif->flags);
1008                 ret = ath6kl_wmi_bssfilter_cmd(ar->wmi, vif->fw_vif_idx,
1009                                                ALL_BSS_FILTER, 0);
1010                 if (ret) {
1011                         ath6kl_err("couldn't set bss filtering\n");
1012                         return ret;
1013                 }
1014         }
1015
1016         ret = ath6kl_set_probed_ssids(ar, vif, request->ssids,
1017                                       request->n_ssids, NULL, 0);
1018         if (ret < 0)
1019                 return ret;
1020
1021         /* this also clears IE in fw if it's not set */
1022         ret = ath6kl_wmi_set_appie_cmd(ar->wmi, vif->fw_vif_idx,
1023                                        WMI_FRAME_PROBE_REQ,
1024                                        request->ie, request->ie_len);
1025         if (ret) {
1026                 ath6kl_err("failed to set Probe Request appie for scan\n");
1027                 return ret;
1028         }
1029
1030         /*
1031          * Scan only the requested channels if the request specifies a set of
1032          * channels. If the list is longer than the target supports, do not
1033          * configure the list and instead, scan all available channels.
1034          */
1035         if (request->n_channels > 0 &&
1036             request->n_channels <= WMI_MAX_CHANNELS) {
1037                 u8 i;
1038
1039                 n_channels = request->n_channels;
1040
1041                 channels = kzalloc(n_channels * sizeof(u16), GFP_KERNEL);
1042                 if (channels == NULL) {
1043                         ath6kl_warn("failed to set scan channels, scan all channels");
1044                         n_channels = 0;
1045                 }
1046
1047                 for (i = 0; i < n_channels; i++)
1048                         channels[i] = request->channels[i]->center_freq;
1049         }
1050
1051         if (test_bit(CONNECTED, &vif->flags))
1052                 force_fg_scan = 1;
1053
1054         vif->scan_req = request;
1055
1056         ret = ath6kl_wmi_beginscan_cmd(ar->wmi, vif->fw_vif_idx,
1057                                        WMI_LONG_SCAN, force_fg_scan,
1058                                        false, 0,
1059                                        ATH6KL_FG_SCAN_INTERVAL,
1060                                        n_channels, channels,
1061                                        request->no_cck,
1062                                        request->rates);
1063         if (ret) {
1064                 ath6kl_err("failed to start scan: %d\n", ret);
1065                 vif->scan_req = NULL;
1066         }
1067
1068         kfree(channels);
1069
1070         return ret;
1071 }
1072
1073 void ath6kl_cfg80211_scan_complete_event(struct ath6kl_vif *vif, bool aborted)
1074 {
1075         struct ath6kl *ar = vif->ar;
1076         struct cfg80211_scan_info info = {
1077                 .aborted = aborted,
1078         };
1079         int i;
1080
1081         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: status%s\n", __func__,
1082                    aborted ? " aborted" : "");
1083
1084         if (!vif->scan_req)
1085                 return;
1086
1087         if (aborted)
1088                 goto out;
1089
1090         if (vif->scan_req->n_ssids && vif->scan_req->ssids[0].ssid_len) {
1091                 for (i = 0; i < vif->scan_req->n_ssids; i++) {
1092                         ath6kl_wmi_probedssid_cmd(ar->wmi, vif->fw_vif_idx,
1093                                                   i + 1, DISABLE_SSID_FLAG,
1094                                                   0, NULL);
1095                 }
1096         }
1097
1098 out:
1099         cfg80211_scan_done(vif->scan_req, &info);
1100         vif->scan_req = NULL;
1101 }
1102
1103 void ath6kl_cfg80211_ch_switch_notify(struct ath6kl_vif *vif, int freq,
1104                                       enum wmi_phy_mode mode)
1105 {
1106         struct cfg80211_chan_def chandef;
1107
1108         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
1109                    "channel switch notify nw_type %d freq %d mode %d\n",
1110                    vif->nw_type, freq, mode);
1111
1112         cfg80211_chandef_create(&chandef,
1113                                 ieee80211_get_channel(vif->ar->wiphy, freq),
1114                                 (mode == WMI_11G_HT20) ?
1115                                         NL80211_CHAN_HT20 : NL80211_CHAN_NO_HT);
1116
1117         mutex_lock(&vif->wdev.mtx);
1118         cfg80211_ch_switch_notify(vif->ndev, &chandef);
1119         mutex_unlock(&vif->wdev.mtx);
1120 }
1121
1122 static int ath6kl_cfg80211_add_key(struct wiphy *wiphy, struct net_device *ndev,
1123                                    u8 key_index, bool pairwise,
1124                                    const u8 *mac_addr,
1125                                    struct key_params *params)
1126 {
1127         struct ath6kl *ar = ath6kl_priv(ndev);
1128         struct ath6kl_vif *vif = netdev_priv(ndev);
1129         struct ath6kl_key *key = NULL;
1130         int seq_len;
1131         u8 key_usage;
1132         u8 key_type;
1133
1134         if (!ath6kl_cfg80211_ready(vif))
1135                 return -EIO;
1136
1137         if (params->cipher == CCKM_KRK_CIPHER_SUITE) {
1138                 if (params->key_len != WMI_KRK_LEN)
1139                         return -EINVAL;
1140                 return ath6kl_wmi_add_krk_cmd(ar->wmi, vif->fw_vif_idx,
1141                                               params->key);
1142         }
1143
1144         if (key_index > WMI_MAX_KEY_INDEX) {
1145                 ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
1146                            "%s: key index %d out of bounds\n", __func__,
1147                            key_index);
1148                 return -ENOENT;
1149         }
1150
1151         key = &vif->keys[key_index];
1152         memset(key, 0, sizeof(struct ath6kl_key));
1153
1154         if (pairwise)
1155                 key_usage = PAIRWISE_USAGE;
1156         else
1157                 key_usage = GROUP_USAGE;
1158
1159         seq_len = params->seq_len;
1160         if (params->cipher == WLAN_CIPHER_SUITE_SMS4 &&
1161             seq_len > ATH6KL_KEY_SEQ_LEN) {
1162                 /* Only first half of the WPI PN is configured */
1163                 seq_len = ATH6KL_KEY_SEQ_LEN;
1164         }
1165         if (params->key_len > WLAN_MAX_KEY_LEN ||
1166             seq_len > sizeof(key->seq))
1167                 return -EINVAL;
1168
1169         key->key_len = params->key_len;
1170         memcpy(key->key, params->key, key->key_len);
1171         key->seq_len = seq_len;
1172         memcpy(key->seq, params->seq, key->seq_len);
1173         key->cipher = params->cipher;
1174
1175         switch (key->cipher) {
1176         case WLAN_CIPHER_SUITE_WEP40:
1177         case WLAN_CIPHER_SUITE_WEP104:
1178                 key_type = WEP_CRYPT;
1179                 break;
1180
1181         case WLAN_CIPHER_SUITE_TKIP:
1182                 key_type = TKIP_CRYPT;
1183                 break;
1184
1185         case WLAN_CIPHER_SUITE_CCMP:
1186                 key_type = AES_CRYPT;
1187                 break;
1188         case WLAN_CIPHER_SUITE_SMS4:
1189                 key_type = WAPI_CRYPT;
1190                 break;
1191
1192         default:
1193                 return -ENOTSUPP;
1194         }
1195
1196         if (((vif->auth_mode == WPA_PSK_AUTH) ||
1197              (vif->auth_mode == WPA2_PSK_AUTH)) &&
1198             (key_usage & GROUP_USAGE))
1199                 del_timer(&vif->disconnect_timer);
1200
1201         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
1202                    "%s: index %d, key_len %d, key_type 0x%x, key_usage 0x%x, seq_len %d\n",
1203                    __func__, key_index, key->key_len, key_type,
1204                    key_usage, key->seq_len);
1205
1206         if (vif->nw_type == AP_NETWORK && !pairwise &&
1207             (key_type == TKIP_CRYPT || key_type == AES_CRYPT ||
1208              key_type == WAPI_CRYPT)) {
1209                 ar->ap_mode_bkey.valid = true;
1210                 ar->ap_mode_bkey.key_index = key_index;
1211                 ar->ap_mode_bkey.key_type = key_type;
1212                 ar->ap_mode_bkey.key_len = key->key_len;
1213                 memcpy(ar->ap_mode_bkey.key, key->key, key->key_len);
1214                 if (!test_bit(CONNECTED, &vif->flags)) {
1215                         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
1216                                    "Delay initial group key configuration until AP mode has been started\n");
1217                         /*
1218                          * The key will be set in ath6kl_connect_ap_mode() once
1219                          * the connected event is received from the target.
1220                          */
1221                         return 0;
1222                 }
1223         }
1224
1225         if (vif->next_mode == AP_NETWORK && key_type == WEP_CRYPT &&
1226             !test_bit(CONNECTED, &vif->flags)) {
1227                 /*
1228                  * Store the key locally so that it can be re-configured after
1229                  * the AP mode has properly started
1230                  * (ath6kl_install_statioc_wep_keys).
1231                  */
1232                 ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
1233                            "Delay WEP key configuration until AP mode has been started\n");
1234                 vif->wep_key_list[key_index].key_len = key->key_len;
1235                 memcpy(vif->wep_key_list[key_index].key, key->key,
1236                        key->key_len);
1237                 return 0;
1238         }
1239
1240         return ath6kl_wmi_addkey_cmd(ar->wmi, vif->fw_vif_idx, key_index,
1241                                      key_type, key_usage, key->key_len,
1242                                      key->seq, key->seq_len, key->key,
1243                                      KEY_OP_INIT_VAL,
1244                                      (u8 *) mac_addr, SYNC_BOTH_WMIFLAG);
1245 }
1246
1247 static int ath6kl_cfg80211_del_key(struct wiphy *wiphy, struct net_device *ndev,
1248                                    u8 key_index, bool pairwise,
1249                                    const u8 *mac_addr)
1250 {
1251         struct ath6kl *ar = ath6kl_priv(ndev);
1252         struct ath6kl_vif *vif = netdev_priv(ndev);
1253
1254         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: index %d\n", __func__, key_index);
1255
1256         if (!ath6kl_cfg80211_ready(vif))
1257                 return -EIO;
1258
1259         if (key_index > WMI_MAX_KEY_INDEX) {
1260                 ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
1261                            "%s: key index %d out of bounds\n", __func__,
1262                            key_index);
1263                 return -ENOENT;
1264         }
1265
1266         if (!vif->keys[key_index].key_len) {
1267                 ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
1268                            "%s: index %d is empty\n", __func__, key_index);
1269                 return 0;
1270         }
1271
1272         vif->keys[key_index].key_len = 0;
1273
1274         return ath6kl_wmi_deletekey_cmd(ar->wmi, vif->fw_vif_idx, key_index);
1275 }
1276
1277 static int ath6kl_cfg80211_get_key(struct wiphy *wiphy, struct net_device *ndev,
1278                                    u8 key_index, bool pairwise,
1279                                    const u8 *mac_addr, void *cookie,
1280                                    void (*callback) (void *cookie,
1281                                                      struct key_params *))
1282 {
1283         struct ath6kl_vif *vif = netdev_priv(ndev);
1284         struct ath6kl_key *key = NULL;
1285         struct key_params params;
1286
1287         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: index %d\n", __func__, key_index);
1288
1289         if (!ath6kl_cfg80211_ready(vif))
1290                 return -EIO;
1291
1292         if (key_index > WMI_MAX_KEY_INDEX) {
1293                 ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
1294                            "%s: key index %d out of bounds\n", __func__,
1295                            key_index);
1296                 return -ENOENT;
1297         }
1298
1299         key = &vif->keys[key_index];
1300         memset(&params, 0, sizeof(params));
1301         params.cipher = key->cipher;
1302         params.key_len = key->key_len;
1303         params.seq_len = key->seq_len;
1304         params.seq = key->seq;
1305         params.key = key->key;
1306
1307         callback(cookie, &params);
1308
1309         return key->key_len ? 0 : -ENOENT;
1310 }
1311
1312 static int ath6kl_cfg80211_set_default_key(struct wiphy *wiphy,
1313                                            struct net_device *ndev,
1314                                            u8 key_index, bool unicast,
1315                                            bool multicast)
1316 {
1317         struct ath6kl *ar = ath6kl_priv(ndev);
1318         struct ath6kl_vif *vif = netdev_priv(ndev);
1319         struct ath6kl_key *key = NULL;
1320         u8 key_usage;
1321         enum crypto_type key_type = NONE_CRYPT;
1322
1323         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: index %d\n", __func__, key_index);
1324
1325         if (!ath6kl_cfg80211_ready(vif))
1326                 return -EIO;
1327
1328         if (key_index > WMI_MAX_KEY_INDEX) {
1329                 ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
1330                            "%s: key index %d out of bounds\n",
1331                            __func__, key_index);
1332                 return -ENOENT;
1333         }
1334
1335         if (!vif->keys[key_index].key_len) {
1336                 ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: invalid key index %d\n",
1337                            __func__, key_index);
1338                 return -EINVAL;
1339         }
1340
1341         vif->def_txkey_index = key_index;
1342         key = &vif->keys[vif->def_txkey_index];
1343         key_usage = GROUP_USAGE;
1344         if (vif->prwise_crypto == WEP_CRYPT)
1345                 key_usage |= TX_USAGE;
1346         if (unicast)
1347                 key_type = vif->prwise_crypto;
1348         if (multicast)
1349                 key_type = vif->grp_crypto;
1350
1351         if (vif->next_mode == AP_NETWORK && !test_bit(CONNECTED, &vif->flags))
1352                 return 0; /* Delay until AP mode has been started */
1353
1354         return ath6kl_wmi_addkey_cmd(ar->wmi, vif->fw_vif_idx,
1355                                      vif->def_txkey_index,
1356                                      key_type, key_usage,
1357                                      key->key_len, key->seq, key->seq_len,
1358                                      key->key,
1359                                      KEY_OP_INIT_VAL, NULL,
1360                                      SYNC_BOTH_WMIFLAG);
1361 }
1362
1363 void ath6kl_cfg80211_tkip_micerr_event(struct ath6kl_vif *vif, u8 keyid,
1364                                        bool ismcast)
1365 {
1366         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
1367                    "%s: keyid %d, ismcast %d\n", __func__, keyid, ismcast);
1368
1369         cfg80211_michael_mic_failure(vif->ndev, vif->bssid,
1370                                      (ismcast ? NL80211_KEYTYPE_GROUP :
1371                                       NL80211_KEYTYPE_PAIRWISE), keyid, NULL,
1372                                      GFP_KERNEL);
1373 }
1374
1375 static int ath6kl_cfg80211_set_wiphy_params(struct wiphy *wiphy, u32 changed)
1376 {
1377         struct ath6kl *ar = (struct ath6kl *)wiphy_priv(wiphy);
1378         struct ath6kl_vif *vif;
1379         int ret;
1380
1381         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: changed 0x%x\n", __func__,
1382                    changed);
1383
1384         vif = ath6kl_vif_first(ar);
1385         if (!vif)
1386                 return -EIO;
1387
1388         if (!ath6kl_cfg80211_ready(vif))
1389                 return -EIO;
1390
1391         if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
1392                 ret = ath6kl_wmi_set_rts_cmd(ar->wmi, wiphy->rts_threshold);
1393                 if (ret != 0) {
1394                         ath6kl_err("ath6kl_wmi_set_rts_cmd failed\n");
1395                         return -EIO;
1396                 }
1397         }
1398
1399         return 0;
1400 }
1401
1402 static int ath6kl_cfg80211_set_txpower(struct wiphy *wiphy,
1403                                        struct wireless_dev *wdev,
1404                                        enum nl80211_tx_power_setting type,
1405                                        int mbm)
1406 {
1407         struct ath6kl *ar = (struct ath6kl *)wiphy_priv(wiphy);
1408         struct ath6kl_vif *vif;
1409         int dbm = MBM_TO_DBM(mbm);
1410
1411         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: type 0x%x, dbm %d\n", __func__,
1412                    type, dbm);
1413
1414         vif = ath6kl_vif_first(ar);
1415         if (!vif)
1416                 return -EIO;
1417
1418         if (!ath6kl_cfg80211_ready(vif))
1419                 return -EIO;
1420
1421         switch (type) {
1422         case NL80211_TX_POWER_AUTOMATIC:
1423                 return 0;
1424         case NL80211_TX_POWER_LIMITED:
1425                 ar->tx_pwr = dbm;
1426                 break;
1427         default:
1428                 ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: type 0x%x not supported\n",
1429                            __func__, type);
1430                 return -EOPNOTSUPP;
1431         }
1432
1433         ath6kl_wmi_set_tx_pwr_cmd(ar->wmi, vif->fw_vif_idx, dbm);
1434
1435         return 0;
1436 }
1437
1438 static int ath6kl_cfg80211_get_txpower(struct wiphy *wiphy,
1439                                        struct wireless_dev *wdev,
1440                                        int *dbm)
1441 {
1442         struct ath6kl *ar = (struct ath6kl *)wiphy_priv(wiphy);
1443         struct ath6kl_vif *vif;
1444
1445         vif = ath6kl_vif_first(ar);
1446         if (!vif)
1447                 return -EIO;
1448
1449         if (!ath6kl_cfg80211_ready(vif))
1450                 return -EIO;
1451
1452         if (test_bit(CONNECTED, &vif->flags)) {
1453                 ar->tx_pwr = 0;
1454
1455                 if (ath6kl_wmi_get_tx_pwr_cmd(ar->wmi, vif->fw_vif_idx) != 0) {
1456                         ath6kl_err("ath6kl_wmi_get_tx_pwr_cmd failed\n");
1457                         return -EIO;
1458                 }
1459
1460                 wait_event_interruptible_timeout(ar->event_wq, ar->tx_pwr != 0,
1461                                                  5 * HZ);
1462
1463                 if (signal_pending(current)) {
1464                         ath6kl_err("target did not respond\n");
1465                         return -EINTR;
1466                 }
1467         }
1468
1469         *dbm = ar->tx_pwr;
1470         return 0;
1471 }
1472
1473 static int ath6kl_cfg80211_set_power_mgmt(struct wiphy *wiphy,
1474                                           struct net_device *dev,
1475                                           bool pmgmt, int timeout)
1476 {
1477         struct ath6kl *ar = ath6kl_priv(dev);
1478         struct wmi_power_mode_cmd mode;
1479         struct ath6kl_vif *vif = netdev_priv(dev);
1480
1481         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: pmgmt %d, timeout %d\n",
1482                    __func__, pmgmt, timeout);
1483
1484         if (!ath6kl_cfg80211_ready(vif))
1485                 return -EIO;
1486
1487         if (pmgmt) {
1488                 ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: rec power\n", __func__);
1489                 mode.pwr_mode = REC_POWER;
1490         } else {
1491                 ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: max perf\n", __func__);
1492                 mode.pwr_mode = MAX_PERF_POWER;
1493         }
1494
1495         if (ath6kl_wmi_powermode_cmd(ar->wmi, vif->fw_vif_idx,
1496                                      mode.pwr_mode) != 0) {
1497                 ath6kl_err("wmi_powermode_cmd failed\n");
1498                 return -EIO;
1499         }
1500
1501         return 0;
1502 }
1503
1504 static struct wireless_dev *ath6kl_cfg80211_add_iface(struct wiphy *wiphy,
1505                                                       const char *name,
1506                                                       unsigned char name_assign_type,
1507                                                       enum nl80211_iftype type,
1508                                                       u32 *flags,
1509                                                       struct vif_params *params)
1510 {
1511         struct ath6kl *ar = wiphy_priv(wiphy);
1512         struct wireless_dev *wdev;
1513         u8 if_idx, nw_type;
1514
1515         if (ar->num_vif == ar->vif_max) {
1516                 ath6kl_err("Reached maximum number of supported vif\n");
1517                 return ERR_PTR(-EINVAL);
1518         }
1519
1520         if (!ath6kl_is_valid_iftype(ar, type, &if_idx, &nw_type)) {
1521                 ath6kl_err("Not a supported interface type\n");
1522                 return ERR_PTR(-EINVAL);
1523         }
1524
1525         wdev = ath6kl_interface_add(ar, name, name_assign_type, type, if_idx, nw_type);
1526         if (!wdev)
1527                 return ERR_PTR(-ENOMEM);
1528
1529         ar->num_vif++;
1530
1531         return wdev;
1532 }
1533
1534 static int ath6kl_cfg80211_del_iface(struct wiphy *wiphy,
1535                                      struct wireless_dev *wdev)
1536 {
1537         struct ath6kl *ar = wiphy_priv(wiphy);
1538         struct ath6kl_vif *vif = netdev_priv(wdev->netdev);
1539
1540         spin_lock_bh(&ar->list_lock);
1541         list_del(&vif->list);
1542         spin_unlock_bh(&ar->list_lock);
1543
1544         ath6kl_cfg80211_vif_stop(vif, test_bit(WMI_READY, &ar->flag));
1545
1546         rtnl_lock();
1547         ath6kl_cfg80211_vif_cleanup(vif);
1548         rtnl_unlock();
1549
1550         return 0;
1551 }
1552
1553 static int ath6kl_cfg80211_change_iface(struct wiphy *wiphy,
1554                                         struct net_device *ndev,
1555                                         enum nl80211_iftype type, u32 *flags,
1556                                         struct vif_params *params)
1557 {
1558         struct ath6kl_vif *vif = netdev_priv(ndev);
1559         int i;
1560
1561         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: type %u\n", __func__, type);
1562
1563         /*
1564          * Don't bring up p2p on an interface which is not initialized
1565          * for p2p operation where fw does not have capability to switch
1566          * dynamically between non-p2p and p2p type interface.
1567          */
1568         if (!test_bit(ATH6KL_FW_CAPABILITY_STA_P2PDEV_DUPLEX,
1569                       vif->ar->fw_capabilities) &&
1570             (type == NL80211_IFTYPE_P2P_CLIENT ||
1571              type == NL80211_IFTYPE_P2P_GO)) {
1572                 if (vif->ar->vif_max == 1) {
1573                         if (vif->fw_vif_idx != 0)
1574                                 return -EINVAL;
1575                         else
1576                                 goto set_iface_type;
1577                 }
1578
1579                 for (i = vif->ar->max_norm_iface; i < vif->ar->vif_max; i++) {
1580                         if (i == vif->fw_vif_idx)
1581                                 break;
1582                 }
1583
1584                 if (i == vif->ar->vif_max) {
1585                         ath6kl_err("Invalid interface to bring up P2P\n");
1586                         return -EINVAL;
1587                 }
1588         }
1589
1590         /* need to clean up enhanced bmiss detection fw state */
1591         ath6kl_cfg80211_sta_bmiss_enhance(vif, false);
1592
1593 set_iface_type:
1594         switch (type) {
1595         case NL80211_IFTYPE_STATION:
1596         case NL80211_IFTYPE_P2P_CLIENT:
1597                 vif->next_mode = INFRA_NETWORK;
1598                 break;
1599         case NL80211_IFTYPE_ADHOC:
1600                 vif->next_mode = ADHOC_NETWORK;
1601                 break;
1602         case NL80211_IFTYPE_AP:
1603         case NL80211_IFTYPE_P2P_GO:
1604                 vif->next_mode = AP_NETWORK;
1605                 break;
1606         default:
1607                 ath6kl_err("invalid interface type %u\n", type);
1608                 return -EOPNOTSUPP;
1609         }
1610
1611         vif->wdev.iftype = type;
1612
1613         return 0;
1614 }
1615
1616 static int ath6kl_cfg80211_join_ibss(struct wiphy *wiphy,
1617                                      struct net_device *dev,
1618                                      struct cfg80211_ibss_params *ibss_param)
1619 {
1620         struct ath6kl *ar = ath6kl_priv(dev);
1621         struct ath6kl_vif *vif = netdev_priv(dev);
1622         int status;
1623
1624         if (!ath6kl_cfg80211_ready(vif))
1625                 return -EIO;
1626
1627         vif->ssid_len = ibss_param->ssid_len;
1628         memcpy(vif->ssid, ibss_param->ssid, vif->ssid_len);
1629
1630         if (ibss_param->chandef.chan)
1631                 vif->ch_hint = ibss_param->chandef.chan->center_freq;
1632
1633         if (ibss_param->channel_fixed) {
1634                 /*
1635                  * TODO: channel_fixed: The channel should be fixed, do not
1636                  * search for IBSSs to join on other channels. Target
1637                  * firmware does not support this feature, needs to be
1638                  * updated.
1639                  */
1640                 return -EOPNOTSUPP;
1641         }
1642
1643         memset(vif->req_bssid, 0, sizeof(vif->req_bssid));
1644         if (ibss_param->bssid && !is_broadcast_ether_addr(ibss_param->bssid))
1645                 memcpy(vif->req_bssid, ibss_param->bssid,
1646                        sizeof(vif->req_bssid));
1647
1648         ath6kl_set_wpa_version(vif, 0);
1649
1650         status = ath6kl_set_auth_type(vif, NL80211_AUTHTYPE_OPEN_SYSTEM);
1651         if (status)
1652                 return status;
1653
1654         if (ibss_param->privacy) {
1655                 ath6kl_set_cipher(vif, WLAN_CIPHER_SUITE_WEP40, true);
1656                 ath6kl_set_cipher(vif, WLAN_CIPHER_SUITE_WEP40, false);
1657         } else {
1658                 ath6kl_set_cipher(vif, 0, true);
1659                 ath6kl_set_cipher(vif, 0, false);
1660         }
1661
1662         vif->nw_type = vif->next_mode;
1663
1664         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
1665                    "%s: connect called with authmode %d dot11 auth %d"
1666                    " PW crypto %d PW crypto len %d GRP crypto %d"
1667                    " GRP crypto len %d channel hint %u\n",
1668                    __func__,
1669                    vif->auth_mode, vif->dot11_auth_mode, vif->prwise_crypto,
1670                    vif->prwise_crypto_len, vif->grp_crypto,
1671                    vif->grp_crypto_len, vif->ch_hint);
1672
1673         status = ath6kl_wmi_connect_cmd(ar->wmi, vif->fw_vif_idx, vif->nw_type,
1674                                         vif->dot11_auth_mode, vif->auth_mode,
1675                                         vif->prwise_crypto,
1676                                         vif->prwise_crypto_len,
1677                                         vif->grp_crypto, vif->grp_crypto_len,
1678                                         vif->ssid_len, vif->ssid,
1679                                         vif->req_bssid, vif->ch_hint,
1680                                         ar->connect_ctrl_flags, SUBTYPE_NONE);
1681         set_bit(CONNECT_PEND, &vif->flags);
1682
1683         return 0;
1684 }
1685
1686 static int ath6kl_cfg80211_leave_ibss(struct wiphy *wiphy,
1687                                       struct net_device *dev)
1688 {
1689         struct ath6kl_vif *vif = netdev_priv(dev);
1690
1691         if (!ath6kl_cfg80211_ready(vif))
1692                 return -EIO;
1693
1694         ath6kl_disconnect(vif);
1695         memset(vif->ssid, 0, sizeof(vif->ssid));
1696         vif->ssid_len = 0;
1697
1698         return 0;
1699 }
1700
1701 static const u32 cipher_suites[] = {
1702         WLAN_CIPHER_SUITE_WEP40,
1703         WLAN_CIPHER_SUITE_WEP104,
1704         WLAN_CIPHER_SUITE_TKIP,
1705         WLAN_CIPHER_SUITE_CCMP,
1706         CCKM_KRK_CIPHER_SUITE,
1707         WLAN_CIPHER_SUITE_SMS4,
1708 };
1709
1710 static bool is_rate_legacy(s32 rate)
1711 {
1712         static const s32 legacy[] = { 1000, 2000, 5500, 11000,
1713                 6000, 9000, 12000, 18000, 24000,
1714                 36000, 48000, 54000
1715         };
1716         u8 i;
1717
1718         for (i = 0; i < ARRAY_SIZE(legacy); i++)
1719                 if (rate == legacy[i])
1720                         return true;
1721
1722         return false;
1723 }
1724
1725 static bool is_rate_ht20(s32 rate, u8 *mcs, bool *sgi)
1726 {
1727         static const s32 ht20[] = { 6500, 13000, 19500, 26000, 39000,
1728                 52000, 58500, 65000, 72200
1729         };
1730         u8 i;
1731
1732         for (i = 0; i < ARRAY_SIZE(ht20); i++) {
1733                 if (rate == ht20[i]) {
1734                         if (i == ARRAY_SIZE(ht20) - 1)
1735                                 /* last rate uses sgi */
1736                                 *sgi = true;
1737                         else
1738                                 *sgi = false;
1739
1740                         *mcs = i;
1741                         return true;
1742                 }
1743         }
1744         return false;
1745 }
1746
1747 static bool is_rate_ht40(s32 rate, u8 *mcs, bool *sgi)
1748 {
1749         static const s32 ht40[] = { 13500, 27000, 40500, 54000,
1750                 81000, 108000, 121500, 135000,
1751                 150000
1752         };
1753         u8 i;
1754
1755         for (i = 0; i < ARRAY_SIZE(ht40); i++) {
1756                 if (rate == ht40[i]) {
1757                         if (i == ARRAY_SIZE(ht40) - 1)
1758                                 /* last rate uses sgi */
1759                                 *sgi = true;
1760                         else
1761                                 *sgi = false;
1762
1763                         *mcs = i;
1764                         return true;
1765                 }
1766         }
1767
1768         return false;
1769 }
1770
1771 static int ath6kl_get_station(struct wiphy *wiphy, struct net_device *dev,
1772                               const u8 *mac, struct station_info *sinfo)
1773 {
1774         struct ath6kl *ar = ath6kl_priv(dev);
1775         struct ath6kl_vif *vif = netdev_priv(dev);
1776         long left;
1777         bool sgi;
1778         s32 rate;
1779         int ret;
1780         u8 mcs;
1781
1782         if (memcmp(mac, vif->bssid, ETH_ALEN) != 0)
1783                 return -ENOENT;
1784
1785         if (down_interruptible(&ar->sem))
1786                 return -EBUSY;
1787
1788         set_bit(STATS_UPDATE_PEND, &vif->flags);
1789
1790         ret = ath6kl_wmi_get_stats_cmd(ar->wmi, vif->fw_vif_idx);
1791
1792         if (ret != 0) {
1793                 up(&ar->sem);
1794                 return -EIO;
1795         }
1796
1797         left = wait_event_interruptible_timeout(ar->event_wq,
1798                                                 !test_bit(STATS_UPDATE_PEND,
1799                                                           &vif->flags),
1800                                                 WMI_TIMEOUT);
1801
1802         up(&ar->sem);
1803
1804         if (left == 0)
1805                 return -ETIMEDOUT;
1806         else if (left < 0)
1807                 return left;
1808
1809         if (vif->target_stats.rx_byte) {
1810                 sinfo->rx_bytes = vif->target_stats.rx_byte;
1811                 sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64);
1812                 sinfo->rx_packets = vif->target_stats.rx_pkt;
1813                 sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS);
1814         }
1815
1816         if (vif->target_stats.tx_byte) {
1817                 sinfo->tx_bytes = vif->target_stats.tx_byte;
1818                 sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64);
1819                 sinfo->tx_packets = vif->target_stats.tx_pkt;
1820                 sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS);
1821         }
1822
1823         sinfo->signal = vif->target_stats.cs_rssi;
1824         sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
1825
1826         rate = vif->target_stats.tx_ucast_rate;
1827
1828         if (is_rate_legacy(rate)) {
1829                 sinfo->txrate.legacy = rate / 100;
1830         } else if (is_rate_ht20(rate, &mcs, &sgi)) {
1831                 if (sgi) {
1832                         sinfo->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
1833                         sinfo->txrate.mcs = mcs - 1;
1834                 } else {
1835                         sinfo->txrate.mcs = mcs;
1836                 }
1837
1838                 sinfo->txrate.flags |= RATE_INFO_FLAGS_MCS;
1839                 sinfo->txrate.bw = RATE_INFO_BW_20;
1840         } else if (is_rate_ht40(rate, &mcs, &sgi)) {
1841                 if (sgi) {
1842                         sinfo->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
1843                         sinfo->txrate.mcs = mcs - 1;
1844                 } else {
1845                         sinfo->txrate.mcs = mcs;
1846                 }
1847
1848                 sinfo->txrate.bw = RATE_INFO_BW_40;
1849                 sinfo->txrate.flags |= RATE_INFO_FLAGS_MCS;
1850         } else {
1851                 ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
1852                            "invalid rate from stats: %d\n", rate);
1853                 ath6kl_debug_war(ar, ATH6KL_WAR_INVALID_RATE);
1854                 return 0;
1855         }
1856
1857         sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE);
1858
1859         if (test_bit(CONNECTED, &vif->flags) &&
1860             test_bit(DTIM_PERIOD_AVAIL, &vif->flags) &&
1861             vif->nw_type == INFRA_NETWORK) {
1862                 sinfo->filled |= BIT(NL80211_STA_INFO_BSS_PARAM);
1863                 sinfo->bss_param.flags = 0;
1864                 sinfo->bss_param.dtim_period = vif->assoc_bss_dtim_period;
1865                 sinfo->bss_param.beacon_interval = vif->assoc_bss_beacon_int;
1866         }
1867
1868         return 0;
1869 }
1870
1871 static int ath6kl_set_pmksa(struct wiphy *wiphy, struct net_device *netdev,
1872                             struct cfg80211_pmksa *pmksa)
1873 {
1874         struct ath6kl *ar = ath6kl_priv(netdev);
1875         struct ath6kl_vif *vif = netdev_priv(netdev);
1876
1877         return ath6kl_wmi_setpmkid_cmd(ar->wmi, vif->fw_vif_idx, pmksa->bssid,
1878                                        pmksa->pmkid, true);
1879 }
1880
1881 static int ath6kl_del_pmksa(struct wiphy *wiphy, struct net_device *netdev,
1882                             struct cfg80211_pmksa *pmksa)
1883 {
1884         struct ath6kl *ar = ath6kl_priv(netdev);
1885         struct ath6kl_vif *vif = netdev_priv(netdev);
1886
1887         return ath6kl_wmi_setpmkid_cmd(ar->wmi, vif->fw_vif_idx, pmksa->bssid,
1888                                        pmksa->pmkid, false);
1889 }
1890
1891 static int ath6kl_flush_pmksa(struct wiphy *wiphy, struct net_device *netdev)
1892 {
1893         struct ath6kl *ar = ath6kl_priv(netdev);
1894         struct ath6kl_vif *vif = netdev_priv(netdev);
1895
1896         if (test_bit(CONNECTED, &vif->flags))
1897                 return ath6kl_wmi_setpmkid_cmd(ar->wmi, vif->fw_vif_idx,
1898                                                vif->bssid, NULL, false);
1899         return 0;
1900 }
1901
1902 static int ath6kl_wow_usr(struct ath6kl *ar, struct ath6kl_vif *vif,
1903                           struct cfg80211_wowlan *wow, u32 *filter)
1904 {
1905         int ret, pos;
1906         u8 mask[WOW_PATTERN_SIZE];
1907         u16 i;
1908
1909         /* Configure the patterns that we received from the user. */
1910         for (i = 0; i < wow->n_patterns; i++) {
1911                 /*
1912                  * Convert given nl80211 specific mask value to equivalent
1913                  * driver specific mask value and send it to the chip along
1914                  * with patterns. For example, If the mask value defined in
1915                  * struct cfg80211_wowlan is 0xA (equivalent binary is 1010),
1916                  * then equivalent driver specific mask value is
1917                  * "0xFF 0x00 0xFF 0x00".
1918                  */
1919                 memset(&mask, 0, sizeof(mask));
1920                 for (pos = 0; pos < wow->patterns[i].pattern_len; pos++) {
1921                         if (wow->patterns[i].mask[pos / 8] & (0x1 << (pos % 8)))
1922                                 mask[pos] = 0xFF;
1923                 }
1924                 /*
1925                  * Note: Pattern's offset is not passed as part of wowlan
1926                  * parameter from CFG layer. So it's always passed as ZERO
1927                  * to the firmware. It means, given WOW patterns are always
1928                  * matched from the first byte of received pkt in the firmware.
1929                  */
1930                 ret = ath6kl_wmi_add_wow_pattern_cmd(ar->wmi,
1931                                 vif->fw_vif_idx, WOW_LIST_ID,
1932                                 wow->patterns[i].pattern_len,
1933                                 0 /* pattern offset */,
1934                                 wow->patterns[i].pattern, mask);
1935                 if (ret)
1936                         return ret;
1937         }
1938
1939         if (wow->disconnect)
1940                 *filter |= WOW_FILTER_OPTION_NWK_DISASSOC;
1941
1942         if (wow->magic_pkt)
1943                 *filter |= WOW_FILTER_OPTION_MAGIC_PACKET;
1944
1945         if (wow->gtk_rekey_failure)
1946                 *filter |= WOW_FILTER_OPTION_GTK_ERROR;
1947
1948         if (wow->eap_identity_req)
1949                 *filter |= WOW_FILTER_OPTION_EAP_REQ;
1950
1951         if (wow->four_way_handshake)
1952                 *filter |= WOW_FILTER_OPTION_8021X_4WAYHS;
1953
1954         return 0;
1955 }
1956
1957 static int ath6kl_wow_ap(struct ath6kl *ar, struct ath6kl_vif *vif)
1958 {
1959         static const u8 unicst_pattern[] = { 0x00, 0x00, 0x00,
1960                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1961                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1962                 0x00, 0x08 };
1963         static const u8 unicst_mask[] = { 0x01, 0x00, 0x00,
1964                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1965                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1966                 0x00, 0x7f };
1967         u8 unicst_offset = 0;
1968         static const u8 arp_pattern[] = { 0x08, 0x06 };
1969         static const u8 arp_mask[] = { 0xff, 0xff };
1970         u8 arp_offset = 20;
1971         static const u8 discvr_pattern[] = { 0xe0, 0x00, 0x00, 0xf8 };
1972         static const u8 discvr_mask[] = { 0xf0, 0x00, 0x00, 0xf8 };
1973         u8 discvr_offset = 38;
1974         static const u8 dhcp_pattern[] = { 0xff, 0xff, 0xff, 0xff,
1975                 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1976                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00,
1977                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1978                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1979                 0x00, 0x00, 0x00, 0x00, 0x00, 0x43 /* port 67 */ };
1980         static const u8 dhcp_mask[] = { 0xff, 0xff, 0xff, 0xff,
1981                 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1982                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
1983                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1984                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1985                 0x00, 0x00, 0x00, 0x00, 0xff, 0xff /* port 67 */ };
1986         u8 dhcp_offset = 0;
1987         int ret;
1988
1989         /* Setup unicast IP, EAPOL-like and ARP pkt pattern */
1990         ret = ath6kl_wmi_add_wow_pattern_cmd(ar->wmi,
1991                         vif->fw_vif_idx, WOW_LIST_ID,
1992                         sizeof(unicst_pattern), unicst_offset,
1993                         unicst_pattern, unicst_mask);
1994         if (ret) {
1995                 ath6kl_err("failed to add WOW unicast IP pattern\n");
1996                 return ret;
1997         }
1998
1999         /* Setup all ARP pkt pattern */
2000         ret = ath6kl_wmi_add_wow_pattern_cmd(ar->wmi,
2001                         vif->fw_vif_idx, WOW_LIST_ID,
2002                         sizeof(arp_pattern), arp_offset,
2003                         arp_pattern, arp_mask);
2004         if (ret) {
2005                 ath6kl_err("failed to add WOW ARP pattern\n");
2006                 return ret;
2007         }
2008
2009         /*
2010          * Setup multicast pattern for mDNS 224.0.0.251,
2011          * SSDP 239.255.255.250 and LLMNR  224.0.0.252
2012          */
2013         ret = ath6kl_wmi_add_wow_pattern_cmd(ar->wmi,
2014                         vif->fw_vif_idx, WOW_LIST_ID,
2015                         sizeof(discvr_pattern), discvr_offset,
2016                         discvr_pattern, discvr_mask);
2017         if (ret) {
2018                 ath6kl_err("failed to add WOW mDNS/SSDP/LLMNR pattern\n");
2019                 return ret;
2020         }
2021
2022         /* Setup all DHCP broadcast pkt pattern */
2023         ret = ath6kl_wmi_add_wow_pattern_cmd(ar->wmi,
2024                         vif->fw_vif_idx, WOW_LIST_ID,
2025                         sizeof(dhcp_pattern), dhcp_offset,
2026                         dhcp_pattern, dhcp_mask);
2027         if (ret) {
2028                 ath6kl_err("failed to add WOW DHCP broadcast pattern\n");
2029                 return ret;
2030         }
2031
2032         return 0;
2033 }
2034
2035 static int ath6kl_wow_sta(struct ath6kl *ar, struct ath6kl_vif *vif)
2036 {
2037         struct net_device *ndev = vif->ndev;
2038         static const u8 discvr_pattern[] = { 0xe0, 0x00, 0x00, 0xf8 };
2039         static const u8 discvr_mask[] = { 0xf0, 0x00, 0x00, 0xf8 };
2040         u8 discvr_offset = 38;
2041         u8 mac_mask[ETH_ALEN];
2042         int ret;
2043
2044         /* Setup unicast pkt pattern */
2045         eth_broadcast_addr(mac_mask);
2046         ret = ath6kl_wmi_add_wow_pattern_cmd(ar->wmi,
2047                                 vif->fw_vif_idx, WOW_LIST_ID,
2048                                 ETH_ALEN, 0, ndev->dev_addr,
2049                                 mac_mask);
2050         if (ret) {
2051                 ath6kl_err("failed to add WOW unicast pattern\n");
2052                 return ret;
2053         }
2054
2055         /*
2056          * Setup multicast pattern for mDNS 224.0.0.251,
2057          * SSDP 239.255.255.250 and LLMNR 224.0.0.252
2058          */
2059         if ((ndev->flags & IFF_ALLMULTI) ||
2060             (ndev->flags & IFF_MULTICAST && netdev_mc_count(ndev) > 0)) {
2061                 ret = ath6kl_wmi_add_wow_pattern_cmd(ar->wmi,
2062                                 vif->fw_vif_idx, WOW_LIST_ID,
2063                                 sizeof(discvr_pattern), discvr_offset,
2064                                 discvr_pattern, discvr_mask);
2065                 if (ret) {
2066                         ath6kl_err("failed to add WOW mDNS/SSDP/LLMNR pattern\n");
2067                         return ret;
2068                 }
2069         }
2070
2071         return 0;
2072 }
2073
2074 static int is_hsleep_mode_procsed(struct ath6kl_vif *vif)
2075 {
2076         return test_bit(HOST_SLEEP_MODE_CMD_PROCESSED, &vif->flags);
2077 }
2078
2079 static bool is_ctrl_ep_empty(struct ath6kl *ar)
2080 {
2081         return !ar->tx_pending[ar->ctrl_ep];
2082 }
2083
2084 static int ath6kl_cfg80211_host_sleep(struct ath6kl *ar, struct ath6kl_vif *vif)
2085 {
2086         int ret, left;
2087
2088         clear_bit(HOST_SLEEP_MODE_CMD_PROCESSED, &vif->flags);
2089
2090         ret = ath6kl_wmi_set_host_sleep_mode_cmd(ar->wmi, vif->fw_vif_idx,
2091                                                  ATH6KL_HOST_MODE_ASLEEP);
2092         if (ret)
2093                 return ret;
2094
2095         left = wait_event_interruptible_timeout(ar->event_wq,
2096                                                 is_hsleep_mode_procsed(vif),
2097                                                 WMI_TIMEOUT);
2098         if (left == 0) {
2099                 ath6kl_warn("timeout, didn't get host sleep cmd processed event\n");
2100                 ret = -ETIMEDOUT;
2101         } else if (left < 0) {
2102                 ath6kl_warn("error while waiting for host sleep cmd processed event %d\n",
2103                             left);
2104                 ret = left;
2105         }
2106
2107         if (ar->tx_pending[ar->ctrl_ep]) {
2108                 left = wait_event_interruptible_timeout(ar->event_wq,
2109                                                         is_ctrl_ep_empty(ar),
2110                                                         WMI_TIMEOUT);
2111                 if (left == 0) {
2112                         ath6kl_warn("clear wmi ctrl data timeout\n");
2113                         ret = -ETIMEDOUT;
2114                 } else if (left < 0) {
2115                         ath6kl_warn("clear wmi ctrl data failed: %d\n", left);
2116                         ret = left;
2117                 }
2118         }
2119
2120         return ret;
2121 }
2122
2123 static int ath6kl_wow_suspend_vif(struct ath6kl_vif *vif,
2124                                   struct cfg80211_wowlan *wow, u32 *filter)
2125 {
2126         struct ath6kl *ar = vif->ar;
2127         struct in_device *in_dev;
2128         struct in_ifaddr *ifa;
2129         int ret;
2130         u16 i, bmiss_time;
2131         __be32 ips[MAX_IP_ADDRS];
2132         u8 index = 0;
2133
2134         if (!test_bit(NETDEV_MCAST_ALL_ON, &vif->flags) &&
2135             test_bit(ATH6KL_FW_CAPABILITY_WOW_MULTICAST_FILTER,
2136                      ar->fw_capabilities)) {
2137                 ret = ath6kl_wmi_mcast_filter_cmd(vif->ar->wmi,
2138                                                 vif->fw_vif_idx, false);
2139                 if (ret)
2140                         return ret;
2141         }
2142
2143         /* Clear existing WOW patterns */
2144         for (i = 0; i < WOW_MAX_FILTERS_PER_LIST; i++)
2145                 ath6kl_wmi_del_wow_pattern_cmd(ar->wmi, vif->fw_vif_idx,
2146                                                WOW_LIST_ID, i);
2147
2148         /*
2149          * Skip the default WOW pattern configuration
2150          * if the driver receives any WOW patterns from
2151          * the user.
2152          */
2153         if (wow)
2154                 ret = ath6kl_wow_usr(ar, vif, wow, filter);
2155         else if (vif->nw_type == AP_NETWORK)
2156                 ret = ath6kl_wow_ap(ar, vif);
2157         else
2158                 ret = ath6kl_wow_sta(ar, vif);
2159
2160         if (ret)
2161                 return ret;
2162
2163         netif_stop_queue(vif->ndev);
2164
2165         if (vif->nw_type != AP_NETWORK) {
2166                 ret = ath6kl_wmi_listeninterval_cmd(ar->wmi, vif->fw_vif_idx,
2167                                                     ATH6KL_MAX_WOW_LISTEN_INTL,
2168                                                     0);
2169                 if (ret)
2170                         return ret;
2171
2172                 /* Set listen interval x 15 times as bmiss time */
2173                 bmiss_time = ATH6KL_MAX_WOW_LISTEN_INTL * 15;
2174                 if (bmiss_time > ATH6KL_MAX_BMISS_TIME)
2175                         bmiss_time = ATH6KL_MAX_BMISS_TIME;
2176
2177                 ret = ath6kl_wmi_bmisstime_cmd(ar->wmi, vif->fw_vif_idx,
2178                                                bmiss_time, 0);
2179                 if (ret)
2180                         return ret;
2181
2182                 ret = ath6kl_wmi_scanparams_cmd(ar->wmi, vif->fw_vif_idx,
2183                                                 0xFFFF, 0, 0xFFFF, 0, 0, 0,
2184                                                 0, 0, 0, 0);
2185                 if (ret)
2186                         return ret;
2187         }
2188
2189         /* Setup own IP addr for ARP agent. */
2190         in_dev = __in_dev_get_rtnl(vif->ndev);
2191         if (!in_dev)
2192                 return 0;
2193
2194         ifa = in_dev->ifa_list;
2195         memset(&ips, 0, sizeof(ips));
2196
2197         /* Configure IP addr only if IP address count < MAX_IP_ADDRS */
2198         while (index < MAX_IP_ADDRS && ifa) {
2199                 ips[index] = ifa->ifa_local;
2200                 ifa = ifa->ifa_next;
2201                 index++;
2202         }
2203
2204         if (ifa) {
2205                 ath6kl_err("total IP addr count is exceeding fw limit\n");
2206                 return -EINVAL;
2207         }
2208
2209         ret = ath6kl_wmi_set_ip_cmd(ar->wmi, vif->fw_vif_idx, ips[0], ips[1]);
2210         if (ret) {
2211                 ath6kl_err("fail to setup ip for arp agent\n");
2212                 return ret;
2213         }
2214
2215         return ret;
2216 }
2217
2218 static int ath6kl_wow_suspend(struct ath6kl *ar, struct cfg80211_wowlan *wow)
2219 {
2220         struct ath6kl_vif *first_vif, *vif;
2221         int ret = 0;
2222         u32 filter = 0;
2223         bool connected = false;
2224
2225         /* enter / leave wow suspend on first vif always */
2226         first_vif = ath6kl_vif_first(ar);
2227         if (WARN_ON(!first_vif) ||
2228             !ath6kl_cfg80211_ready(first_vif))
2229                 return -EIO;
2230
2231         if (wow && (wow->n_patterns > WOW_MAX_FILTERS_PER_LIST))
2232                 return -EINVAL;
2233
2234         /* install filters for each connected vif */
2235         spin_lock_bh(&ar->list_lock);
2236         list_for_each_entry(vif, &ar->vif_list, list) {
2237                 if (!test_bit(CONNECTED, &vif->flags) ||
2238                     !ath6kl_cfg80211_ready(vif))
2239                         continue;
2240                 connected = true;
2241
2242                 ret = ath6kl_wow_suspend_vif(vif, wow, &filter);
2243                 if (ret)
2244                         break;
2245         }
2246         spin_unlock_bh(&ar->list_lock);
2247
2248         if (!connected)
2249                 return -ENOTCONN;
2250         else if (ret)
2251                 return ret;
2252
2253         ar->state = ATH6KL_STATE_SUSPENDING;
2254
2255         ret = ath6kl_wmi_set_wow_mode_cmd(ar->wmi, first_vif->fw_vif_idx,
2256                                           ATH6KL_WOW_MODE_ENABLE,
2257                                           filter,
2258                                           WOW_HOST_REQ_DELAY);
2259         if (ret)
2260                 return ret;
2261
2262         return ath6kl_cfg80211_host_sleep(ar, first_vif);
2263 }
2264
2265 static int ath6kl_wow_resume_vif(struct ath6kl_vif *vif)
2266 {
2267         struct ath6kl *ar = vif->ar;
2268         int ret;
2269
2270         if (vif->nw_type != AP_NETWORK) {
2271                 ret = ath6kl_wmi_scanparams_cmd(ar->wmi, vif->fw_vif_idx,
2272                                                 0, 0, 0, 0, 0, 0, 3, 0, 0, 0);
2273                 if (ret)
2274                         return ret;
2275
2276                 ret = ath6kl_wmi_listeninterval_cmd(ar->wmi, vif->fw_vif_idx,
2277                                                     vif->listen_intvl_t, 0);
2278                 if (ret)
2279                         return ret;
2280
2281                 ret = ath6kl_wmi_bmisstime_cmd(ar->wmi, vif->fw_vif_idx,
2282                                                vif->bmiss_time_t, 0);
2283                 if (ret)
2284                         return ret;
2285         }
2286
2287         if (!test_bit(NETDEV_MCAST_ALL_OFF, &vif->flags) &&
2288             test_bit(ATH6KL_FW_CAPABILITY_WOW_MULTICAST_FILTER,
2289                      ar->fw_capabilities)) {
2290                 ret = ath6kl_wmi_mcast_filter_cmd(vif->ar->wmi,
2291                                                   vif->fw_vif_idx, true);
2292                 if (ret)
2293                         return ret;
2294         }
2295
2296         netif_wake_queue(vif->ndev);
2297
2298         return 0;
2299 }
2300
2301 static int ath6kl_wow_resume(struct ath6kl *ar)
2302 {
2303         struct ath6kl_vif *vif;
2304         int ret;
2305
2306         vif = ath6kl_vif_first(ar);
2307         if (WARN_ON(!vif) ||
2308             !ath6kl_cfg80211_ready(vif))
2309                 return -EIO;
2310
2311         ar->state = ATH6KL_STATE_RESUMING;
2312
2313         ret = ath6kl_wmi_set_host_sleep_mode_cmd(ar->wmi, vif->fw_vif_idx,
2314                                                  ATH6KL_HOST_MODE_AWAKE);
2315         if (ret) {
2316                 ath6kl_warn("Failed to configure host sleep mode for wow resume: %d\n",
2317                             ret);
2318                 goto cleanup;
2319         }
2320
2321         spin_lock_bh(&ar->list_lock);
2322         list_for_each_entry(vif, &ar->vif_list, list) {
2323                 if (!test_bit(CONNECTED, &vif->flags) ||
2324                     !ath6kl_cfg80211_ready(vif))
2325                         continue;
2326                 ret = ath6kl_wow_resume_vif(vif);
2327                 if (ret)
2328                         break;
2329         }
2330         spin_unlock_bh(&ar->list_lock);
2331
2332         if (ret)
2333                 goto cleanup;
2334
2335         ar->state = ATH6KL_STATE_ON;
2336         return 0;
2337
2338 cleanup:
2339         ar->state = ATH6KL_STATE_WOW;
2340         return ret;
2341 }
2342
2343 static int ath6kl_cfg80211_deepsleep_suspend(struct ath6kl *ar)
2344 {
2345         struct ath6kl_vif *vif;
2346         int ret;
2347
2348         vif = ath6kl_vif_first(ar);
2349         if (!vif)
2350                 return -EIO;
2351
2352         if (!test_bit(WMI_READY, &ar->flag)) {
2353                 ath6kl_err("deepsleep failed as wmi is not ready\n");
2354                 return -EIO;
2355         }
2356
2357         ath6kl_cfg80211_stop_all(ar);
2358
2359         /* Save the current power mode before enabling power save */
2360         ar->wmi->saved_pwr_mode = ar->wmi->pwr_mode;
2361
2362         ret = ath6kl_wmi_powermode_cmd(ar->wmi, 0, REC_POWER);
2363         if (ret)
2364                 return ret;
2365
2366         /* Disable WOW mode */
2367         ret = ath6kl_wmi_set_wow_mode_cmd(ar->wmi, vif->fw_vif_idx,
2368                                           ATH6KL_WOW_MODE_DISABLE,
2369                                           0, 0);
2370         if (ret)
2371                 return ret;
2372
2373         /* Flush all non control pkts in TX path */
2374         ath6kl_tx_data_cleanup(ar);
2375
2376         ret = ath6kl_cfg80211_host_sleep(ar, vif);
2377         if (ret)
2378                 return ret;
2379
2380         return 0;
2381 }
2382
2383 static int ath6kl_cfg80211_deepsleep_resume(struct ath6kl *ar)
2384 {
2385         struct ath6kl_vif *vif;
2386         int ret;
2387
2388         vif = ath6kl_vif_first(ar);
2389
2390         if (!vif)
2391                 return -EIO;
2392
2393         if (ar->wmi->pwr_mode != ar->wmi->saved_pwr_mode) {
2394                 ret = ath6kl_wmi_powermode_cmd(ar->wmi, 0,
2395                                                ar->wmi->saved_pwr_mode);
2396                 if (ret)
2397                         return ret;
2398         }
2399
2400         ret = ath6kl_wmi_set_host_sleep_mode_cmd(ar->wmi, vif->fw_vif_idx,
2401                                                  ATH6KL_HOST_MODE_AWAKE);
2402         if (ret)
2403                 return ret;
2404
2405         ar->state = ATH6KL_STATE_ON;
2406
2407         /* Reset scan parameter to default values */
2408         ret = ath6kl_wmi_scanparams_cmd(ar->wmi, vif->fw_vif_idx,
2409                                         0, 0, 0, 0, 0, 0, 3, 0, 0, 0);
2410         if (ret)
2411                 return ret;
2412
2413         return 0;
2414 }
2415
2416 int ath6kl_cfg80211_suspend(struct ath6kl *ar,
2417                             enum ath6kl_cfg_suspend_mode mode,
2418                             struct cfg80211_wowlan *wow)
2419 {
2420         struct ath6kl_vif *vif;
2421         enum ath6kl_state prev_state;
2422         int ret;
2423
2424         switch (mode) {
2425         case ATH6KL_CFG_SUSPEND_WOW:
2426
2427                 ath6kl_dbg(ATH6KL_DBG_SUSPEND, "wow mode suspend\n");
2428
2429                 /* Flush all non control pkts in TX path */
2430                 ath6kl_tx_data_cleanup(ar);
2431
2432                 prev_state = ar->state;
2433
2434                 ret = ath6kl_wow_suspend(ar, wow);
2435                 if (ret) {
2436                         ar->state = prev_state;
2437                         return ret;
2438                 }
2439
2440                 ar->state = ATH6KL_STATE_WOW;
2441                 break;
2442
2443         case ATH6KL_CFG_SUSPEND_DEEPSLEEP:
2444
2445                 ath6kl_dbg(ATH6KL_DBG_SUSPEND, "deep sleep suspend\n");
2446
2447                 ret = ath6kl_cfg80211_deepsleep_suspend(ar);
2448                 if (ret) {
2449                         ath6kl_err("deepsleep suspend failed: %d\n", ret);
2450                         return ret;
2451                 }
2452
2453                 ar->state = ATH6KL_STATE_DEEPSLEEP;
2454
2455                 break;
2456
2457         case ATH6KL_CFG_SUSPEND_CUTPOWER:
2458
2459                 ath6kl_cfg80211_stop_all(ar);
2460
2461                 if (ar->state == ATH6KL_STATE_OFF) {
2462                         ath6kl_dbg(ATH6KL_DBG_SUSPEND,
2463                                    "suspend hw off, no action for cutpower\n");
2464                         break;
2465                 }
2466
2467                 ath6kl_dbg(ATH6KL_DBG_SUSPEND, "suspend cutting power\n");
2468
2469                 ret = ath6kl_init_hw_stop(ar);
2470                 if (ret) {
2471                         ath6kl_warn("failed to stop hw during suspend: %d\n",
2472                                     ret);
2473                 }
2474
2475                 ar->state = ATH6KL_STATE_CUTPOWER;
2476
2477                 break;
2478
2479         default:
2480                 break;
2481         }
2482
2483         list_for_each_entry(vif, &ar->vif_list, list)
2484                 ath6kl_cfg80211_scan_complete_event(vif, true);
2485
2486         return 0;
2487 }
2488 EXPORT_SYMBOL(ath6kl_cfg80211_suspend);
2489
2490 int ath6kl_cfg80211_resume(struct ath6kl *ar)
2491 {
2492         int ret;
2493
2494         switch (ar->state) {
2495         case  ATH6KL_STATE_WOW:
2496                 ath6kl_dbg(ATH6KL_DBG_SUSPEND, "wow mode resume\n");
2497
2498                 ret = ath6kl_wow_resume(ar);
2499                 if (ret) {
2500                         ath6kl_warn("wow mode resume failed: %d\n", ret);
2501                         return ret;
2502                 }
2503
2504                 break;
2505
2506         case ATH6KL_STATE_DEEPSLEEP:
2507                 ath6kl_dbg(ATH6KL_DBG_SUSPEND, "deep sleep resume\n");
2508
2509                 ret = ath6kl_cfg80211_deepsleep_resume(ar);
2510                 if (ret) {
2511                         ath6kl_warn("deep sleep resume failed: %d\n", ret);
2512                         return ret;
2513                 }
2514                 break;
2515
2516         case ATH6KL_STATE_CUTPOWER:
2517                 ath6kl_dbg(ATH6KL_DBG_SUSPEND, "resume restoring power\n");
2518
2519                 ret = ath6kl_init_hw_start(ar);
2520                 if (ret) {
2521                         ath6kl_warn("Failed to boot hw in resume: %d\n", ret);
2522                         return ret;
2523                 }
2524                 break;
2525
2526         default:
2527                 break;
2528         }
2529
2530         return 0;
2531 }
2532 EXPORT_SYMBOL(ath6kl_cfg80211_resume);
2533
2534 #ifdef CONFIG_PM
2535
2536 /* hif layer decides what suspend mode to use */
2537 static int __ath6kl_cfg80211_suspend(struct wiphy *wiphy,
2538                                  struct cfg80211_wowlan *wow)
2539 {
2540         struct ath6kl *ar = wiphy_priv(wiphy);
2541
2542         ath6kl_recovery_suspend(ar);
2543
2544         return ath6kl_hif_suspend(ar, wow);
2545 }
2546
2547 static int __ath6kl_cfg80211_resume(struct wiphy *wiphy)
2548 {
2549         struct ath6kl *ar = wiphy_priv(wiphy);
2550         int err;
2551
2552         err = ath6kl_hif_resume(ar);
2553         if (err)
2554                 return err;
2555
2556         ath6kl_recovery_resume(ar);
2557
2558         return 0;
2559 }
2560
2561 /*
2562  * FIXME: WOW suspend mode is selected if the host sdio controller supports
2563  * both sdio irq wake up and keep power. The target pulls sdio data line to
2564  * wake up the host when WOW pattern matches. This causes sdio irq handler
2565  * is being called in the host side which internally hits ath6kl's RX path.
2566  *
2567  * Since sdio interrupt is not disabled, RX path executes even before
2568  * the host executes the actual resume operation from PM module.
2569  *
2570  * In the current scenario, WOW resume should happen before start processing
2571  * any data from the target. So It's required to perform WOW resume in RX path.
2572  * Ideally we should perform WOW resume only in the actual platform
2573  * resume path. This area needs bit rework to avoid WOW resume in RX path.
2574  *
2575  * ath6kl_check_wow_status() is called from ath6kl_rx().
2576  */
2577 void ath6kl_check_wow_status(struct ath6kl *ar)
2578 {
2579         if (ar->state == ATH6KL_STATE_SUSPENDING)
2580                 return;
2581
2582         if (ar->state == ATH6KL_STATE_WOW)
2583                 ath6kl_cfg80211_resume(ar);
2584 }
2585
2586 #else
2587
2588 void ath6kl_check_wow_status(struct ath6kl *ar)
2589 {
2590 }
2591 #endif
2592
2593 static int ath6kl_set_htcap(struct ath6kl_vif *vif, enum nl80211_band band,
2594                             bool ht_enable)
2595 {
2596         struct ath6kl_htcap *htcap = &vif->htcap[band];
2597
2598         if (htcap->ht_enable == ht_enable)
2599                 return 0;
2600
2601         if (ht_enable) {
2602                 /* Set default ht capabilities */
2603                 htcap->ht_enable = true;
2604                 htcap->cap_info = (band == NL80211_BAND_2GHZ) ?
2605                                    ath6kl_g_htcap : ath6kl_a_htcap;
2606                 htcap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_16K;
2607         } else /* Disable ht */
2608                 memset(htcap, 0, sizeof(*htcap));
2609
2610         return ath6kl_wmi_set_htcap_cmd(vif->ar->wmi, vif->fw_vif_idx,
2611                                         band, htcap);
2612 }
2613
2614 static int ath6kl_restore_htcap(struct ath6kl_vif *vif)
2615 {
2616         struct wiphy *wiphy = vif->ar->wiphy;
2617         int band, ret = 0;
2618
2619         for (band = 0; band < NUM_NL80211_BANDS; band++) {
2620                 if (!wiphy->bands[band])
2621                         continue;
2622
2623                 ret = ath6kl_set_htcap(vif, band,
2624                                 wiphy->bands[band]->ht_cap.ht_supported);
2625                 if (ret)
2626                         return ret;
2627         }
2628
2629         return ret;
2630 }
2631
2632 static bool ath6kl_is_p2p_ie(const u8 *pos)
2633 {
2634         return pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
2635                 pos[2] == 0x50 && pos[3] == 0x6f &&
2636                 pos[4] == 0x9a && pos[5] == 0x09;
2637 }
2638
2639 static int ath6kl_set_ap_probe_resp_ies(struct ath6kl_vif *vif,
2640                                         const u8 *ies, size_t ies_len)
2641 {
2642         struct ath6kl *ar = vif->ar;
2643         const u8 *pos;
2644         u8 *buf = NULL;
2645         size_t len = 0;
2646         int ret;
2647
2648         /*
2649          * Filter out P2P IE(s) since they will be included depending on
2650          * the Probe Request frame in ath6kl_send_go_probe_resp().
2651          */
2652
2653         if (ies && ies_len) {
2654                 buf = kmalloc(ies_len, GFP_KERNEL);
2655                 if (buf == NULL)
2656                         return -ENOMEM;
2657                 pos = ies;
2658                 while (pos + 1 < ies + ies_len) {
2659                         if (pos + 2 + pos[1] > ies + ies_len)
2660                                 break;
2661                         if (!ath6kl_is_p2p_ie(pos)) {
2662                                 memcpy(buf + len, pos, 2 + pos[1]);
2663                                 len += 2 + pos[1];
2664                         }
2665                         pos += 2 + pos[1];
2666                 }
2667         }
2668
2669         ret = ath6kl_wmi_set_appie_cmd(ar->wmi, vif->fw_vif_idx,
2670                                        WMI_FRAME_PROBE_RESP, buf, len);
2671         kfree(buf);
2672         return ret;
2673 }
2674
2675 static int ath6kl_set_ies(struct ath6kl_vif *vif,
2676                           struct cfg80211_beacon_data *info)
2677 {
2678         struct ath6kl *ar = vif->ar;
2679         int res;
2680
2681         /* this also clears IE in fw if it's not set */
2682         res = ath6kl_wmi_set_appie_cmd(ar->wmi, vif->fw_vif_idx,
2683                                        WMI_FRAME_BEACON,
2684                                        info->beacon_ies,
2685                                        info->beacon_ies_len);
2686         if (res)
2687                 return res;
2688
2689         /* this also clears IE in fw if it's not set */
2690         res = ath6kl_set_ap_probe_resp_ies(vif, info->proberesp_ies,
2691                                            info->proberesp_ies_len);
2692         if (res)
2693                 return res;
2694
2695         /* this also clears IE in fw if it's not set */
2696         res = ath6kl_wmi_set_appie_cmd(ar->wmi, vif->fw_vif_idx,
2697                                        WMI_FRAME_ASSOC_RESP,
2698                                        info->assocresp_ies,
2699                                        info->assocresp_ies_len);
2700         if (res)
2701                 return res;
2702
2703         return 0;
2704 }
2705
2706 static int ath6kl_get_rsn_capab(struct cfg80211_beacon_data *beacon,
2707                                 u8 *rsn_capab)
2708 {
2709         const u8 *rsn_ie;
2710         size_t rsn_ie_len;
2711         u16 cnt;
2712
2713         if (!beacon->tail)
2714                 return -EINVAL;
2715
2716         rsn_ie = cfg80211_find_ie(WLAN_EID_RSN, beacon->tail, beacon->tail_len);
2717         if (!rsn_ie)
2718                 return -EINVAL;
2719
2720         rsn_ie_len = *(rsn_ie + 1);
2721         /* skip element id and length */
2722         rsn_ie += 2;
2723
2724         /* skip version */
2725         if (rsn_ie_len < 2)
2726                 return -EINVAL;
2727         rsn_ie +=  2;
2728         rsn_ie_len -= 2;
2729
2730         /* skip group cipher suite */
2731         if (rsn_ie_len < 4)
2732                 return 0;
2733         rsn_ie +=  4;
2734         rsn_ie_len -= 4;
2735
2736         /* skip pairwise cipher suite */
2737         if (rsn_ie_len < 2)
2738                 return 0;
2739         cnt = get_unaligned_le16(rsn_ie);
2740         rsn_ie += (2 + cnt * 4);
2741         rsn_ie_len -= (2 + cnt * 4);
2742
2743         /* skip akm suite */
2744         if (rsn_ie_len < 2)
2745                 return 0;
2746         cnt = get_unaligned_le16(rsn_ie);
2747         rsn_ie += (2 + cnt * 4);
2748         rsn_ie_len -= (2 + cnt * 4);
2749
2750         if (rsn_ie_len < 2)
2751                 return 0;
2752
2753         memcpy(rsn_capab, rsn_ie, 2);
2754
2755         return 0;
2756 }
2757
2758 static int ath6kl_start_ap(struct wiphy *wiphy, struct net_device *dev,
2759                            struct cfg80211_ap_settings *info)
2760 {
2761         struct ath6kl *ar = ath6kl_priv(dev);
2762         struct ath6kl_vif *vif = netdev_priv(dev);
2763         struct ieee80211_mgmt *mgmt;
2764         bool hidden = false;
2765         u8 *ies;
2766         int ies_len;
2767         struct wmi_connect_cmd p;
2768         int res;
2769         int i, ret;
2770         u16 rsn_capab = 0;
2771         int inactivity_timeout = 0;
2772
2773         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s:\n", __func__);
2774
2775         if (!ath6kl_cfg80211_ready(vif))
2776                 return -EIO;
2777
2778         if (vif->next_mode != AP_NETWORK)
2779                 return -EOPNOTSUPP;
2780
2781         res = ath6kl_set_ies(vif, &info->beacon);
2782
2783         ar->ap_mode_bkey.valid = false;
2784
2785         ret = ath6kl_wmi_ap_set_beacon_intvl_cmd(ar->wmi, vif->fw_vif_idx,
2786                                                  info->beacon_interval);
2787
2788         if (ret)
2789                 ath6kl_warn("Failed to set beacon interval: %d\n", ret);
2790
2791         ret = ath6kl_wmi_ap_set_dtim_cmd(ar->wmi, vif->fw_vif_idx,
2792                                          info->dtim_period);
2793
2794         /* ignore error, just print a warning and continue normally */
2795         if (ret)
2796                 ath6kl_warn("Failed to set dtim_period in beacon: %d\n", ret);
2797
2798         if (info->beacon.head == NULL)
2799                 return -EINVAL;
2800         mgmt = (struct ieee80211_mgmt *) info->beacon.head;
2801         ies = mgmt->u.beacon.variable;
2802         if (ies > info->beacon.head + info->beacon.head_len)
2803                 return -EINVAL;
2804         ies_len = info->beacon.head + info->beacon.head_len - ies;
2805
2806         if (info->ssid == NULL)
2807                 return -EINVAL;
2808         memcpy(vif->ssid, info->ssid, info->ssid_len);
2809         vif->ssid_len = info->ssid_len;
2810         if (info->hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE)
2811                 hidden = true;
2812
2813         res = ath6kl_wmi_ap_hidden_ssid(ar->wmi, vif->fw_vif_idx, hidden);
2814         if (res)
2815                 return res;
2816
2817         ret = ath6kl_set_auth_type(vif, info->auth_type);
2818         if (ret)
2819                 return ret;
2820
2821         memset(&p, 0, sizeof(p));
2822
2823         for (i = 0; i < info->crypto.n_akm_suites; i++) {
2824                 switch (info->crypto.akm_suites[i]) {
2825                 case WLAN_AKM_SUITE_8021X:
2826                         if (info->crypto.wpa_versions & NL80211_WPA_VERSION_1)
2827                                 p.auth_mode |= WPA_AUTH;
2828                         if (info->crypto.wpa_versions & NL80211_WPA_VERSION_2)
2829                                 p.auth_mode |= WPA2_AUTH;
2830                         break;
2831                 case WLAN_AKM_SUITE_PSK:
2832                         if (info->crypto.wpa_versions & NL80211_WPA_VERSION_1)
2833                                 p.auth_mode |= WPA_PSK_AUTH;
2834                         if (info->crypto.wpa_versions & NL80211_WPA_VERSION_2)
2835                                 p.auth_mode |= WPA2_PSK_AUTH;
2836                         break;
2837                 }
2838         }
2839         if (p.auth_mode == 0)
2840                 p.auth_mode = NONE_AUTH;
2841         vif->auth_mode = p.auth_mode;
2842
2843         for (i = 0; i < info->crypto.n_ciphers_pairwise; i++) {
2844                 switch (info->crypto.ciphers_pairwise[i]) {
2845                 case WLAN_CIPHER_SUITE_WEP40:
2846                 case WLAN_CIPHER_SUITE_WEP104:
2847                         p.prwise_crypto_type |= WEP_CRYPT;
2848                         break;
2849                 case WLAN_CIPHER_SUITE_TKIP:
2850                         p.prwise_crypto_type |= TKIP_CRYPT;
2851                         break;
2852                 case WLAN_CIPHER_SUITE_CCMP:
2853                         p.prwise_crypto_type |= AES_CRYPT;
2854                         break;
2855                 case WLAN_CIPHER_SUITE_SMS4:
2856                         p.prwise_crypto_type |= WAPI_CRYPT;
2857                         break;
2858                 }
2859         }
2860         if (p.prwise_crypto_type == 0) {
2861                 p.prwise_crypto_type = NONE_CRYPT;
2862                 ath6kl_set_cipher(vif, 0, true);
2863         } else if (info->crypto.n_ciphers_pairwise == 1) {
2864                 ath6kl_set_cipher(vif, info->crypto.ciphers_pairwise[0], true);
2865         }
2866
2867         switch (info->crypto.cipher_group) {
2868         case WLAN_CIPHER_SUITE_WEP40:
2869         case WLAN_CIPHER_SUITE_WEP104:
2870                 p.grp_crypto_type = WEP_CRYPT;
2871                 break;
2872         case WLAN_CIPHER_SUITE_TKIP:
2873                 p.grp_crypto_type = TKIP_CRYPT;
2874                 break;
2875         case WLAN_CIPHER_SUITE_CCMP:
2876                 p.grp_crypto_type = AES_CRYPT;
2877                 break;
2878         case WLAN_CIPHER_SUITE_SMS4:
2879                 p.grp_crypto_type = WAPI_CRYPT;
2880                 break;
2881         default:
2882                 p.grp_crypto_type = NONE_CRYPT;
2883                 break;
2884         }
2885         ath6kl_set_cipher(vif, info->crypto.cipher_group, false);
2886
2887         p.nw_type = AP_NETWORK;
2888         vif->nw_type = vif->next_mode;
2889
2890         p.ssid_len = vif->ssid_len;
2891         memcpy(p.ssid, vif->ssid, vif->ssid_len);
2892         p.dot11_auth_mode = vif->dot11_auth_mode;
2893         p.ch = cpu_to_le16(info->chandef.chan->center_freq);
2894
2895         /* Enable uAPSD support by default */
2896         res = ath6kl_wmi_ap_set_apsd(ar->wmi, vif->fw_vif_idx, true);
2897         if (res < 0)
2898                 return res;
2899
2900         if (vif->wdev.iftype == NL80211_IFTYPE_P2P_GO) {
2901                 p.nw_subtype = SUBTYPE_P2PGO;
2902         } else {
2903                 /*
2904                  * Due to firmware limitation, it is not possible to
2905                  * do P2P mgmt operations in AP mode
2906                  */
2907                 p.nw_subtype = SUBTYPE_NONE;
2908         }
2909
2910         if (info->inactivity_timeout) {
2911                 inactivity_timeout = info->inactivity_timeout;
2912
2913                 if (test_bit(ATH6KL_FW_CAPABILITY_AP_INACTIVITY_MINS,
2914                              ar->fw_capabilities))
2915                         inactivity_timeout = DIV_ROUND_UP(inactivity_timeout,
2916                                                           60);
2917
2918                 res = ath6kl_wmi_set_inact_period(ar->wmi, vif->fw_vif_idx,
2919                                                   inactivity_timeout);
2920                 if (res < 0)
2921                         return res;
2922         }
2923
2924         if (ath6kl_set_htcap(vif, info->chandef.chan->band,
2925                              cfg80211_get_chandef_type(&info->chandef)
2926                                         != NL80211_CHAN_NO_HT))
2927                 return -EIO;
2928
2929         /*
2930          * Get the PTKSA replay counter in the RSN IE. Supplicant
2931          * will use the RSN IE in M3 message and firmware has to
2932          * advertise the same in beacon/probe response. Send
2933          * the complete RSN IE capability field to firmware
2934          */
2935         if (!ath6kl_get_rsn_capab(&info->beacon, (u8 *) &rsn_capab) &&
2936             test_bit(ATH6KL_FW_CAPABILITY_RSN_CAP_OVERRIDE,
2937                      ar->fw_capabilities)) {
2938                 res = ath6kl_wmi_set_ie_cmd(ar->wmi, vif->fw_vif_idx,
2939                                             WLAN_EID_RSN, WMI_RSN_IE_CAPB,
2940                                             (const u8 *) &rsn_capab,
2941                                             sizeof(rsn_capab));
2942                 vif->rsn_capab = rsn_capab;
2943                 if (res < 0)
2944                         return res;
2945         }
2946
2947         memcpy(&vif->profile, &p, sizeof(p));
2948         res = ath6kl_wmi_ap_profile_commit(ar->wmi, vif->fw_vif_idx, &p);
2949         if (res < 0)
2950                 return res;
2951
2952         return 0;
2953 }
2954
2955 static int ath6kl_change_beacon(struct wiphy *wiphy, struct net_device *dev,
2956                                 struct cfg80211_beacon_data *beacon)
2957 {
2958         struct ath6kl_vif *vif = netdev_priv(dev);
2959
2960         if (!ath6kl_cfg80211_ready(vif))
2961                 return -EIO;
2962
2963         if (vif->next_mode != AP_NETWORK)
2964                 return -EOPNOTSUPP;
2965
2966         return ath6kl_set_ies(vif, beacon);
2967 }
2968
2969 static int ath6kl_stop_ap(struct wiphy *wiphy, struct net_device *dev)
2970 {
2971         struct ath6kl *ar = ath6kl_priv(dev);
2972         struct ath6kl_vif *vif = netdev_priv(dev);
2973
2974         if (vif->nw_type != AP_NETWORK)
2975                 return -EOPNOTSUPP;
2976         if (!test_bit(CONNECTED, &vif->flags))
2977                 return -ENOTCONN;
2978
2979         ath6kl_wmi_disconnect_cmd(ar->wmi, vif->fw_vif_idx);
2980         clear_bit(CONNECTED, &vif->flags);
2981
2982         /* Restore ht setting in firmware */
2983         return ath6kl_restore_htcap(vif);
2984 }
2985
2986 static const u8 bcast_addr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
2987
2988 static int ath6kl_del_station(struct wiphy *wiphy, struct net_device *dev,
2989                               struct station_del_parameters *params)
2990 {
2991         struct ath6kl *ar = ath6kl_priv(dev);
2992         struct ath6kl_vif *vif = netdev_priv(dev);
2993         const u8 *addr = params->mac ? params->mac : bcast_addr;
2994
2995         return ath6kl_wmi_ap_set_mlme(ar->wmi, vif->fw_vif_idx, WMI_AP_DEAUTH,
2996                                       addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
2997 }
2998
2999 static int ath6kl_change_station(struct wiphy *wiphy, struct net_device *dev,
3000                                  const u8 *mac,
3001                                  struct station_parameters *params)
3002 {
3003         struct ath6kl *ar = ath6kl_priv(dev);
3004         struct ath6kl_vif *vif = netdev_priv(dev);
3005         int err;
3006
3007         if (vif->nw_type != AP_NETWORK)
3008                 return -EOPNOTSUPP;
3009
3010         err = cfg80211_check_station_change(wiphy, params,
3011                                             CFG80211_STA_AP_MLME_CLIENT);
3012         if (err)
3013                 return err;
3014
3015         if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED))
3016                 return ath6kl_wmi_ap_set_mlme(ar->wmi, vif->fw_vif_idx,
3017                                               WMI_AP_MLME_AUTHORIZE, mac, 0);
3018         return ath6kl_wmi_ap_set_mlme(ar->wmi, vif->fw_vif_idx,
3019                                       WMI_AP_MLME_UNAUTHORIZE, mac, 0);
3020 }
3021
3022 static int ath6kl_remain_on_channel(struct wiphy *wiphy,
3023                                     struct wireless_dev *wdev,
3024                                     struct ieee80211_channel *chan,
3025                                     unsigned int duration,
3026                                     u64 *cookie)
3027 {
3028         struct ath6kl_vif *vif = ath6kl_vif_from_wdev(wdev);
3029         struct ath6kl *ar = ath6kl_priv(vif->ndev);
3030         u32 id;
3031
3032         /* TODO: if already pending or ongoing remain-on-channel,
3033          * return -EBUSY */
3034         id = ++vif->last_roc_id;
3035         if (id == 0) {
3036                 /* Do not use 0 as the cookie value */
3037                 id = ++vif->last_roc_id;
3038         }
3039         *cookie = id;
3040
3041         return ath6kl_wmi_remain_on_chnl_cmd(ar->wmi, vif->fw_vif_idx,
3042                                              chan->center_freq, duration);
3043 }
3044
3045 static int ath6kl_cancel_remain_on_channel(struct wiphy *wiphy,
3046                                            struct wireless_dev *wdev,
3047                                            u64 cookie)
3048 {
3049         struct ath6kl_vif *vif = ath6kl_vif_from_wdev(wdev);
3050         struct ath6kl *ar = ath6kl_priv(vif->ndev);
3051
3052         if (cookie != vif->last_roc_id)
3053                 return -ENOENT;
3054         vif->last_cancel_roc_id = cookie;
3055
3056         return ath6kl_wmi_cancel_remain_on_chnl_cmd(ar->wmi, vif->fw_vif_idx);
3057 }
3058
3059 static int ath6kl_send_go_probe_resp(struct ath6kl_vif *vif,
3060                                      const u8 *buf, size_t len,
3061                                      unsigned int freq)
3062 {
3063         struct ath6kl *ar = vif->ar;
3064         const u8 *pos;
3065         u8 *p2p;
3066         int p2p_len;
3067         int ret;
3068         const struct ieee80211_mgmt *mgmt;
3069
3070         mgmt = (const struct ieee80211_mgmt *) buf;
3071
3072         /* Include P2P IE(s) from the frame generated in user space. */
3073
3074         p2p = kmalloc(len, GFP_KERNEL);
3075         if (p2p == NULL)
3076                 return -ENOMEM;
3077         p2p_len = 0;
3078
3079         pos = mgmt->u.probe_resp.variable;
3080         while (pos + 1 < buf + len) {
3081                 if (pos + 2 + pos[1] > buf + len)
3082                         break;
3083                 if (ath6kl_is_p2p_ie(pos)) {
3084                         memcpy(p2p + p2p_len, pos, 2 + pos[1]);
3085                         p2p_len += 2 + pos[1];
3086                 }
3087                 pos += 2 + pos[1];
3088         }
3089
3090         ret = ath6kl_wmi_send_probe_response_cmd(ar->wmi, vif->fw_vif_idx, freq,
3091                                                  mgmt->da, p2p, p2p_len);
3092         kfree(p2p);
3093         return ret;
3094 }
3095
3096 static bool ath6kl_mgmt_powersave_ap(struct ath6kl_vif *vif,
3097                                      u32 id,
3098                                      u32 freq,
3099                                      u32 wait,
3100                                      const u8 *buf,
3101                                      size_t len,
3102                                      bool *more_data,
3103                                      bool no_cck)
3104 {
3105         struct ieee80211_mgmt *mgmt;
3106         struct ath6kl_sta *conn;
3107         bool is_psq_empty = false;
3108         struct ath6kl_mgmt_buff *mgmt_buf;
3109         size_t mgmt_buf_size;
3110         struct ath6kl *ar = vif->ar;
3111
3112         mgmt = (struct ieee80211_mgmt *) buf;
3113         if (is_multicast_ether_addr(mgmt->da))
3114                 return false;
3115
3116         conn = ath6kl_find_sta(vif, mgmt->da);
3117         if (!conn)
3118                 return false;
3119
3120         if (conn->sta_flags & STA_PS_SLEEP) {
3121                 if (!(conn->sta_flags & STA_PS_POLLED)) {
3122                         /* Queue the frames if the STA is sleeping */
3123                         mgmt_buf_size = len + sizeof(struct ath6kl_mgmt_buff);
3124                         mgmt_buf = kmalloc(mgmt_buf_size, GFP_KERNEL);
3125                         if (!mgmt_buf)
3126                                 return false;
3127
3128                         INIT_LIST_HEAD(&mgmt_buf->list);
3129                         mgmt_buf->id = id;
3130                         mgmt_buf->freq = freq;
3131                         mgmt_buf->wait = wait;
3132                         mgmt_buf->len = len;
3133                         mgmt_buf->no_cck = no_cck;
3134                         memcpy(mgmt_buf->buf, buf, len);
3135                         spin_lock_bh(&conn->psq_lock);
3136                         is_psq_empty = skb_queue_empty(&conn->psq) &&
3137                                         (conn->mgmt_psq_len == 0);
3138                         list_add_tail(&mgmt_buf->list, &conn->mgmt_psq);
3139                         conn->mgmt_psq_len++;
3140                         spin_unlock_bh(&conn->psq_lock);
3141
3142                         /*
3143                          * If this is the first pkt getting queued
3144                          * for this STA, update the PVB for this
3145                          * STA.
3146                          */
3147                         if (is_psq_empty)
3148                                 ath6kl_wmi_set_pvb_cmd(ar->wmi, vif->fw_vif_idx,
3149                                                        conn->aid, 1);
3150                         return true;
3151                 }
3152
3153                 /*
3154                  * This tx is because of a PsPoll.
3155                  * Determine if MoreData bit has to be set.
3156                  */
3157                 spin_lock_bh(&conn->psq_lock);
3158                 if (!skb_queue_empty(&conn->psq) || (conn->mgmt_psq_len != 0))
3159                         *more_data = true;
3160                 spin_unlock_bh(&conn->psq_lock);
3161         }
3162
3163         return false;
3164 }
3165
3166 /* Check if SSID length is greater than DIRECT- */
3167 static bool ath6kl_is_p2p_go_ssid(const u8 *buf, size_t len)
3168 {
3169         const struct ieee80211_mgmt *mgmt;
3170         mgmt = (const struct ieee80211_mgmt *) buf;
3171
3172         /* variable[1] contains the SSID tag length */
3173         if (buf + len >= &mgmt->u.probe_resp.variable[1] &&
3174             (mgmt->u.probe_resp.variable[1] > P2P_WILDCARD_SSID_LEN)) {
3175                 return true;
3176         }
3177
3178         return false;
3179 }
3180
3181 static int ath6kl_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
3182                           struct cfg80211_mgmt_tx_params *params, u64 *cookie)
3183 {
3184         struct ath6kl_vif *vif = ath6kl_vif_from_wdev(wdev);
3185         struct ath6kl *ar = ath6kl_priv(vif->ndev);
3186         struct ieee80211_channel *chan = params->chan;
3187         const u8 *buf = params->buf;
3188         size_t len = params->len;
3189         unsigned int wait = params->wait;
3190         bool no_cck = params->no_cck;
3191         u32 id, freq;
3192         const struct ieee80211_mgmt *mgmt;
3193         bool more_data, queued;
3194
3195         /* default to the current channel, but use the one specified as argument
3196          * if any
3197          */
3198         freq = vif->ch_hint;
3199         if (chan)
3200                 freq = chan->center_freq;
3201
3202         /* never send freq zero to the firmware */
3203         if (WARN_ON(freq == 0))
3204                 return -EINVAL;
3205
3206         mgmt = (const struct ieee80211_mgmt *) buf;
3207         if (vif->nw_type == AP_NETWORK && test_bit(CONNECTED, &vif->flags) &&
3208             ieee80211_is_probe_resp(mgmt->frame_control) &&
3209             ath6kl_is_p2p_go_ssid(buf, len)) {
3210                 /*
3211                  * Send Probe Response frame in GO mode using a separate WMI
3212                  * command to allow the target to fill in the generic IEs.
3213                  */
3214                 *cookie = 0; /* TX status not supported */
3215                 return ath6kl_send_go_probe_resp(vif, buf, len, freq);
3216         }
3217
3218         id = vif->send_action_id++;
3219         if (id == 0) {
3220                 /*
3221                  * 0 is a reserved value in the WMI command and shall not be
3222                  * used for the command.
3223                  */
3224                 id = vif->send_action_id++;
3225         }
3226
3227         *cookie = id;
3228
3229         /* AP mode Power saving processing */
3230         if (vif->nw_type == AP_NETWORK) {
3231                 queued = ath6kl_mgmt_powersave_ap(vif, id, freq, wait, buf, len,
3232                                                   &more_data, no_cck);
3233                 if (queued)
3234                         return 0;
3235         }
3236
3237         return ath6kl_wmi_send_mgmt_cmd(ar->wmi, vif->fw_vif_idx, id, freq,
3238                                         wait, buf, len, no_cck);
3239 }
3240
3241 static int ath6kl_get_antenna(struct wiphy *wiphy,
3242                               u32 *tx_ant, u32 *rx_ant)
3243 {
3244         struct ath6kl *ar = wiphy_priv(wiphy);
3245         *tx_ant = ar->hw.tx_ant;
3246         *rx_ant = ar->hw.rx_ant;
3247         return 0;
3248 }
3249
3250 static void ath6kl_mgmt_frame_register(struct wiphy *wiphy,
3251                                        struct wireless_dev *wdev,
3252                                        u16 frame_type, bool reg)
3253 {
3254         struct ath6kl_vif *vif = ath6kl_vif_from_wdev(wdev);
3255
3256         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: frame_type=0x%x reg=%d\n",
3257                    __func__, frame_type, reg);
3258         if (frame_type == IEEE80211_STYPE_PROBE_REQ) {
3259                 /*
3260                  * Note: This notification callback is not allowed to sleep, so
3261                  * we cannot send WMI_PROBE_REQ_REPORT_CMD here. Instead, we
3262                  * hardcode target to report Probe Request frames all the time.
3263                  */
3264                 vif->probe_req_report = reg;
3265         }
3266 }
3267
3268 static int ath6kl_cfg80211_sscan_start(struct wiphy *wiphy,
3269                         struct net_device *dev,
3270                         struct cfg80211_sched_scan_request *request)
3271 {
3272         struct ath6kl *ar = ath6kl_priv(dev);
3273         struct ath6kl_vif *vif = netdev_priv(dev);
3274         u16 interval;
3275         int ret, rssi_thold;
3276         int n_match_sets = request->n_match_sets;
3277
3278         /*
3279          * If there's a matchset w/o an SSID, then assume it's just for
3280          * the RSSI (nothing else is currently supported) and ignore it.
3281          * The device only supports a global RSSI filter that we set below.
3282          */
3283         if (n_match_sets == 1 && !request->match_sets[0].ssid.ssid_len)
3284                 n_match_sets = 0;
3285
3286         if (ar->state != ATH6KL_STATE_ON)
3287                 return -EIO;
3288
3289         if (vif->sme_state != SME_DISCONNECTED)
3290                 return -EBUSY;
3291
3292         ath6kl_cfg80211_scan_complete_event(vif, true);
3293
3294         ret = ath6kl_set_probed_ssids(ar, vif, request->ssids,
3295                                       request->n_ssids,
3296                                       request->match_sets,
3297                                       n_match_sets);
3298         if (ret < 0)
3299                 return ret;
3300
3301         if (!n_match_sets) {
3302                 ret = ath6kl_wmi_bssfilter_cmd(ar->wmi, vif->fw_vif_idx,
3303                                                ALL_BSS_FILTER, 0);
3304                 if (ret < 0)
3305                         return ret;
3306         } else {
3307                  ret = ath6kl_wmi_bssfilter_cmd(ar->wmi, vif->fw_vif_idx,
3308                                                 MATCHED_SSID_FILTER, 0);
3309                 if (ret < 0)
3310                         return ret;
3311         }
3312
3313         if (test_bit(ATH6KL_FW_CAPABILITY_RSSI_SCAN_THOLD,
3314                      ar->fw_capabilities)) {
3315                 if (request->min_rssi_thold <= NL80211_SCAN_RSSI_THOLD_OFF)
3316                         rssi_thold = 0;
3317                 else if (request->min_rssi_thold < -127)
3318                         rssi_thold = -127;
3319                 else
3320                         rssi_thold = request->min_rssi_thold;
3321
3322                 ret = ath6kl_wmi_set_rssi_filter_cmd(ar->wmi, vif->fw_vif_idx,
3323                                                      rssi_thold);
3324                 if (ret) {
3325                         ath6kl_err("failed to set RSSI threshold for scan\n");
3326                         return ret;
3327                 }
3328         }
3329
3330         /* fw uses seconds, also make sure that it's >0 */
3331         interval = max_t(u16, 1, request->scan_plans[0].interval);
3332
3333         ath6kl_wmi_scanparams_cmd(ar->wmi, vif->fw_vif_idx,
3334                                   interval, interval,
3335                                   vif->bg_scan_period, 0, 0, 0, 3, 0, 0, 0);
3336
3337         /* this also clears IE in fw if it's not set */
3338         ret = ath6kl_wmi_set_appie_cmd(ar->wmi, vif->fw_vif_idx,
3339                                        WMI_FRAME_PROBE_REQ,
3340                                        request->ie, request->ie_len);
3341         if (ret) {
3342                 ath6kl_warn("Failed to set probe request IE for scheduled scan: %d\n",
3343                             ret);
3344                 return ret;
3345         }
3346
3347         ret = ath6kl_wmi_enable_sched_scan_cmd(ar->wmi, vif->fw_vif_idx, true);
3348         if (ret)
3349                 return ret;
3350
3351         set_bit(SCHED_SCANNING, &vif->flags);
3352
3353         return 0;
3354 }
3355
3356 static int ath6kl_cfg80211_sscan_stop(struct wiphy *wiphy,
3357                                       struct net_device *dev)
3358 {
3359         struct ath6kl_vif *vif = netdev_priv(dev);
3360         bool stopped;
3361
3362         stopped = __ath6kl_cfg80211_sscan_stop(vif);
3363
3364         if (!stopped)
3365                 return -EIO;
3366
3367         return 0;
3368 }
3369
3370 static int ath6kl_cfg80211_set_bitrate(struct wiphy *wiphy,
3371                                        struct net_device *dev,
3372                                        const u8 *addr,
3373                                        const struct cfg80211_bitrate_mask *mask)
3374 {
3375         struct ath6kl *ar = ath6kl_priv(dev);
3376         struct ath6kl_vif *vif = netdev_priv(dev);
3377
3378         return ath6kl_wmi_set_bitrate_mask(ar->wmi, vif->fw_vif_idx,
3379                                            mask);
3380 }
3381
3382 static int ath6kl_cfg80211_set_txe_config(struct wiphy *wiphy,
3383                                           struct net_device *dev,
3384                                           u32 rate, u32 pkts, u32 intvl)
3385 {
3386         struct ath6kl *ar = ath6kl_priv(dev);
3387         struct ath6kl_vif *vif = netdev_priv(dev);
3388
3389         if (vif->nw_type != INFRA_NETWORK ||
3390             !test_bit(ATH6KL_FW_CAPABILITY_TX_ERR_NOTIFY, ar->fw_capabilities))
3391                 return -EOPNOTSUPP;
3392
3393         if (vif->sme_state != SME_CONNECTED)
3394                 return -ENOTCONN;
3395
3396         /* save this since the firmware won't report the interval */
3397         vif->txe_intvl = intvl;
3398
3399         return ath6kl_wmi_set_txe_notify(ar->wmi, vif->fw_vif_idx,
3400                                          rate, pkts, intvl);
3401 }
3402
3403 static const struct ieee80211_txrx_stypes
3404 ath6kl_mgmt_stypes[NUM_NL80211_IFTYPES] = {
3405         [NL80211_IFTYPE_STATION] = {
3406                 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
3407                 BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
3408                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
3409                 BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
3410         },
3411         [NL80211_IFTYPE_AP] = {
3412                 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
3413                 BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
3414                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
3415                 BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
3416         },
3417         [NL80211_IFTYPE_P2P_CLIENT] = {
3418                 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
3419                 BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
3420                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
3421                 BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
3422         },
3423         [NL80211_IFTYPE_P2P_GO] = {
3424                 .tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
3425                 BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
3426                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
3427                 BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
3428         },
3429 };
3430
3431 static struct cfg80211_ops ath6kl_cfg80211_ops = {
3432         .add_virtual_intf = ath6kl_cfg80211_add_iface,
3433         .del_virtual_intf = ath6kl_cfg80211_del_iface,
3434         .change_virtual_intf = ath6kl_cfg80211_change_iface,
3435         .scan = ath6kl_cfg80211_scan,
3436         .connect = ath6kl_cfg80211_connect,
3437         .disconnect = ath6kl_cfg80211_disconnect,
3438         .add_key = ath6kl_cfg80211_add_key,
3439         .get_key = ath6kl_cfg80211_get_key,
3440         .del_key = ath6kl_cfg80211_del_key,
3441         .set_default_key = ath6kl_cfg80211_set_default_key,
3442         .set_wiphy_params = ath6kl_cfg80211_set_wiphy_params,
3443         .set_tx_power = ath6kl_cfg80211_set_txpower,
3444         .get_tx_power = ath6kl_cfg80211_get_txpower,
3445         .set_power_mgmt = ath6kl_cfg80211_set_power_mgmt,
3446         .join_ibss = ath6kl_cfg80211_join_ibss,
3447         .leave_ibss = ath6kl_cfg80211_leave_ibss,
3448         .get_station = ath6kl_get_station,
3449         .set_pmksa = ath6kl_set_pmksa,
3450         .del_pmksa = ath6kl_del_pmksa,
3451         .flush_pmksa = ath6kl_flush_pmksa,
3452         CFG80211_TESTMODE_CMD(ath6kl_tm_cmd)
3453 #ifdef CONFIG_PM
3454         .suspend = __ath6kl_cfg80211_suspend,
3455         .resume = __ath6kl_cfg80211_resume,
3456 #endif
3457         .start_ap = ath6kl_start_ap,
3458         .change_beacon = ath6kl_change_beacon,
3459         .stop_ap = ath6kl_stop_ap,
3460         .del_station = ath6kl_del_station,
3461         .change_station = ath6kl_change_station,
3462         .remain_on_channel = ath6kl_remain_on_channel,
3463         .cancel_remain_on_channel = ath6kl_cancel_remain_on_channel,
3464         .mgmt_tx = ath6kl_mgmt_tx,
3465         .mgmt_frame_register = ath6kl_mgmt_frame_register,
3466         .get_antenna = ath6kl_get_antenna,
3467         .sched_scan_start = ath6kl_cfg80211_sscan_start,
3468         .sched_scan_stop = ath6kl_cfg80211_sscan_stop,
3469         .set_bitrate_mask = ath6kl_cfg80211_set_bitrate,
3470         .set_cqm_txe_config = ath6kl_cfg80211_set_txe_config,
3471 };
3472
3473 void ath6kl_cfg80211_stop(struct ath6kl_vif *vif)
3474 {
3475         ath6kl_cfg80211_sscan_disable(vif);
3476
3477         switch (vif->sme_state) {
3478         case SME_DISCONNECTED:
3479                 break;
3480         case SME_CONNECTING:
3481                 cfg80211_connect_result(vif->ndev, vif->bssid, NULL, 0,
3482                                         NULL, 0,
3483                                         WLAN_STATUS_UNSPECIFIED_FAILURE,
3484                                         GFP_KERNEL);
3485                 break;
3486         case SME_CONNECTED:
3487                 cfg80211_disconnected(vif->ndev, 0, NULL, 0, true, GFP_KERNEL);
3488                 break;
3489         }
3490
3491         if (vif->ar->state != ATH6KL_STATE_RECOVERY &&
3492             (test_bit(CONNECTED, &vif->flags) ||
3493             test_bit(CONNECT_PEND, &vif->flags)))
3494                 ath6kl_wmi_disconnect_cmd(vif->ar->wmi, vif->fw_vif_idx);
3495
3496         vif->sme_state = SME_DISCONNECTED;
3497         clear_bit(CONNECTED, &vif->flags);
3498         clear_bit(CONNECT_PEND, &vif->flags);
3499
3500         /* Stop netdev queues, needed during recovery */
3501         netif_stop_queue(vif->ndev);
3502         netif_carrier_off(vif->ndev);
3503
3504         /* disable scanning */
3505         if (vif->ar->state != ATH6KL_STATE_RECOVERY &&
3506             ath6kl_wmi_scanparams_cmd(vif->ar->wmi, vif->fw_vif_idx, 0xFFFF,
3507                                       0, 0, 0, 0, 0, 0, 0, 0, 0) != 0)
3508                 ath6kl_warn("failed to disable scan during stop\n");
3509
3510         ath6kl_cfg80211_scan_complete_event(vif, true);
3511 }
3512
3513 void ath6kl_cfg80211_stop_all(struct ath6kl *ar)
3514 {
3515         struct ath6kl_vif *vif;
3516
3517         vif = ath6kl_vif_first(ar);
3518         if (!vif && ar->state != ATH6KL_STATE_RECOVERY) {
3519                 /* save the current power mode before enabling power save */
3520                 ar->wmi->saved_pwr_mode = ar->wmi->pwr_mode;
3521
3522                 if (ath6kl_wmi_powermode_cmd(ar->wmi, 0, REC_POWER) != 0)
3523                         ath6kl_warn("ath6kl_deep_sleep_enable: wmi_powermode_cmd failed\n");
3524                 return;
3525         }
3526
3527         /*
3528          * FIXME: we should take ar->list_lock to protect changes in the
3529          * vif_list, but that's not trivial to do as ath6kl_cfg80211_stop()
3530          * sleeps.
3531          */
3532         list_for_each_entry(vif, &ar->vif_list, list)
3533                 ath6kl_cfg80211_stop(vif);
3534 }
3535
3536 static void ath6kl_cfg80211_reg_notify(struct wiphy *wiphy,
3537                                        struct regulatory_request *request)
3538 {
3539         struct ath6kl *ar = wiphy_priv(wiphy);
3540         u32 rates[NUM_NL80211_BANDS];
3541         int ret, i;
3542
3543         ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
3544                    "cfg reg_notify %c%c%s%s initiator %d hint_type %d\n",
3545                    request->alpha2[0], request->alpha2[1],
3546                    request->intersect ? " intersect" : "",
3547                    request->processed ? " processed" : "",
3548                    request->initiator, request->user_reg_hint_type);
3549
3550         if (request->user_reg_hint_type != NL80211_USER_REG_HINT_CELL_BASE)
3551                 return;
3552
3553         ret = ath6kl_wmi_set_regdomain_cmd(ar->wmi, request->alpha2);
3554         if (ret) {
3555                 ath6kl_err("failed to set regdomain: %d\n", ret);
3556                 return;
3557         }
3558
3559         /*
3560          * Firmware will apply the regdomain change only after a scan is
3561          * issued and it will send a WMI_REGDOMAIN_EVENTID when it has been
3562          * changed.
3563          */
3564
3565         for (i = 0; i < NUM_NL80211_BANDS; i++)
3566                 if (wiphy->bands[i])
3567                         rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
3568
3569
3570         ret = ath6kl_wmi_beginscan_cmd(ar->wmi, 0, WMI_LONG_SCAN, false,
3571                                        false, 0, ATH6KL_FG_SCAN_INTERVAL,
3572                                        0, NULL, false, rates);
3573         if (ret) {
3574                 ath6kl_err("failed to start scan for a regdomain change: %d\n",
3575                            ret);
3576                 return;
3577         }
3578 }
3579
3580 static int ath6kl_cfg80211_vif_init(struct ath6kl_vif *vif)
3581 {
3582         vif->aggr_cntxt = aggr_init(vif);
3583         if (!vif->aggr_cntxt) {
3584                 ath6kl_err("failed to initialize aggr\n");
3585                 return -ENOMEM;
3586         }
3587
3588         setup_timer(&vif->disconnect_timer, disconnect_timer_handler,
3589                     (unsigned long) vif->ndev);
3590         setup_timer(&vif->sched_scan_timer, ath6kl_wmi_sscan_timer,
3591                     (unsigned long) vif);
3592
3593         set_bit(WMM_ENABLED, &vif->flags);
3594         spin_lock_init(&vif->if_lock);
3595
3596         INIT_LIST_HEAD(&vif->mc_filter);
3597
3598         return 0;
3599 }
3600
3601 void ath6kl_cfg80211_vif_stop(struct ath6kl_vif *vif, bool wmi_ready)
3602 {
3603         static u8 bcast_mac[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
3604         bool discon_issued;
3605
3606         netif_stop_queue(vif->ndev);
3607
3608         clear_bit(WLAN_ENABLED, &vif->flags);
3609
3610         if (wmi_ready) {
3611                 discon_issued = test_bit(CONNECTED, &vif->flags) ||
3612                                 test_bit(CONNECT_PEND, &vif->flags);
3613                 ath6kl_disconnect(vif);
3614                 del_timer(&vif->disconnect_timer);
3615
3616                 if (discon_issued)
3617                         ath6kl_disconnect_event(vif, DISCONNECT_CMD,
3618                                                 (vif->nw_type & AP_NETWORK) ?
3619                                                 bcast_mac : vif->bssid,
3620                                                 0, NULL, 0);
3621         }
3622
3623         if (vif->scan_req) {
3624                 struct cfg80211_scan_info info = {
3625                         .aborted = true,
3626                 };
3627
3628                 cfg80211_scan_done(vif->scan_req, &info);
3629                 vif->scan_req = NULL;
3630         }
3631
3632         /* need to clean up enhanced bmiss detection fw state */
3633         ath6kl_cfg80211_sta_bmiss_enhance(vif, false);
3634 }
3635
3636 void ath6kl_cfg80211_vif_cleanup(struct ath6kl_vif *vif)
3637 {
3638         struct ath6kl *ar = vif->ar;
3639         struct ath6kl_mc_filter *mc_filter, *tmp;
3640
3641         aggr_module_destroy(vif->aggr_cntxt);
3642
3643         ar->avail_idx_map |= BIT(vif->fw_vif_idx);
3644
3645         if (vif->nw_type == ADHOC_NETWORK)
3646                 ar->ibss_if_active = false;
3647
3648         list_for_each_entry_safe(mc_filter, tmp, &vif->mc_filter, list) {
3649                 list_del(&mc_filter->list);
3650                 kfree(mc_filter);
3651         }
3652
3653         unregister_netdevice(vif->ndev);
3654
3655         ar->num_vif--;
3656 }
3657
3658 static const char ath6kl_gstrings_sta_stats[][ETH_GSTRING_LEN] = {
3659         /* Common stats names used by many drivers. */
3660         "tx_pkts_nic", "tx_bytes_nic", "rx_pkts_nic", "rx_bytes_nic",
3661
3662         /* TX stats. */
3663         "d_tx_ucast_pkts", "d_tx_bcast_pkts",
3664         "d_tx_ucast_bytes", "d_tx_bcast_bytes",
3665         "d_tx_rts_ok", "d_tx_error", "d_tx_fail",
3666         "d_tx_retry", "d_tx_multi_retry", "d_tx_rts_fail",
3667         "d_tx_tkip_counter_measures",
3668
3669         /* RX Stats. */
3670         "d_rx_ucast_pkts", "d_rx_ucast_rate", "d_rx_bcast_pkts",
3671         "d_rx_ucast_bytes", "d_rx_bcast_bytes", "d_rx_frag_pkt",
3672         "d_rx_error", "d_rx_crc_err", "d_rx_keycache_miss",
3673         "d_rx_decrypt_crc_err", "d_rx_duplicate_frames",
3674         "d_rx_mic_err", "d_rx_tkip_format_err", "d_rx_ccmp_format_err",
3675         "d_rx_ccmp_replay_err",
3676
3677         /* Misc stats. */
3678         "d_beacon_miss", "d_num_connects", "d_num_disconnects",
3679         "d_beacon_avg_rssi", "d_arp_received", "d_arp_matched",
3680         "d_arp_replied"
3681 };
3682
3683 #define ATH6KL_STATS_LEN        ARRAY_SIZE(ath6kl_gstrings_sta_stats)
3684
3685 static int ath6kl_get_sset_count(struct net_device *dev, int sset)
3686 {
3687         int rv = 0;
3688
3689         if (sset == ETH_SS_STATS)
3690                 rv += ATH6KL_STATS_LEN;
3691
3692         if (rv == 0)
3693                 return -EOPNOTSUPP;
3694         return rv;
3695 }
3696
3697 static void ath6kl_get_stats(struct net_device *dev,
3698                             struct ethtool_stats *stats,
3699                             u64 *data)
3700 {
3701         struct ath6kl_vif *vif = netdev_priv(dev);
3702         struct ath6kl *ar = vif->ar;
3703         int i = 0;
3704         struct target_stats *tgt_stats;
3705
3706         memset(data, 0, sizeof(u64) * ATH6KL_STATS_LEN);
3707
3708         ath6kl_read_tgt_stats(ar, vif);
3709
3710         tgt_stats = &vif->target_stats;
3711
3712         data[i++] = tgt_stats->tx_ucast_pkt + tgt_stats->tx_bcast_pkt;
3713         data[i++] = tgt_stats->tx_ucast_byte + tgt_stats->tx_bcast_byte;
3714         data[i++] = tgt_stats->rx_ucast_pkt + tgt_stats->rx_bcast_pkt;
3715         data[i++] = tgt_stats->rx_ucast_byte + tgt_stats->rx_bcast_byte;
3716
3717         data[i++] = tgt_stats->tx_ucast_pkt;
3718         data[i++] = tgt_stats->tx_bcast_pkt;
3719         data[i++] = tgt_stats->tx_ucast_byte;
3720         data[i++] = tgt_stats->tx_bcast_byte;
3721         data[i++] = tgt_stats->tx_rts_success_cnt;
3722         data[i++] = tgt_stats->tx_err;
3723         data[i++] = tgt_stats->tx_fail_cnt;
3724         data[i++] = tgt_stats->tx_retry_cnt;
3725         data[i++] = tgt_stats->tx_mult_retry_cnt;
3726         data[i++] = tgt_stats->tx_rts_fail_cnt;
3727         data[i++] = tgt_stats->tkip_cnter_measures_invoked;
3728
3729         data[i++] = tgt_stats->rx_ucast_pkt;
3730         data[i++] = tgt_stats->rx_ucast_rate;
3731         data[i++] = tgt_stats->rx_bcast_pkt;
3732         data[i++] = tgt_stats->rx_ucast_byte;
3733         data[i++] = tgt_stats->rx_bcast_byte;
3734         data[i++] = tgt_stats->rx_frgment_pkt;
3735         data[i++] = tgt_stats->rx_err;
3736         data[i++] = tgt_stats->rx_crc_err;
3737         data[i++] = tgt_stats->rx_key_cache_miss;
3738         data[i++] = tgt_stats->rx_decrypt_err;
3739         data[i++] = tgt_stats->rx_dupl_frame;
3740         data[i++] = tgt_stats->tkip_local_mic_fail;
3741         data[i++] = tgt_stats->tkip_fmt_err;
3742         data[i++] = tgt_stats->ccmp_fmt_err;
3743         data[i++] = tgt_stats->ccmp_replays;
3744
3745         data[i++] = tgt_stats->cs_bmiss_cnt;
3746         data[i++] = tgt_stats->cs_connect_cnt;
3747         data[i++] = tgt_stats->cs_discon_cnt;
3748         data[i++] = tgt_stats->cs_ave_beacon_rssi;
3749         data[i++] = tgt_stats->arp_received;
3750         data[i++] = tgt_stats->arp_matched;
3751         data[i++] = tgt_stats->arp_replied;
3752
3753         if (i !=  ATH6KL_STATS_LEN) {
3754                 WARN_ON_ONCE(1);
3755                 ath6kl_err("ethtool stats error, i: %d  STATS_LEN: %d\n",
3756                            i, (int)ATH6KL_STATS_LEN);
3757         }
3758 }
3759
3760 /* These stats are per NIC, not really per vdev, so we just ignore dev. */
3761 static void ath6kl_get_strings(struct net_device *dev, u32 sset, u8 *data)
3762 {
3763         int sz_sta_stats = 0;
3764
3765         if (sset == ETH_SS_STATS) {
3766                 sz_sta_stats = sizeof(ath6kl_gstrings_sta_stats);
3767                 memcpy(data, ath6kl_gstrings_sta_stats, sz_sta_stats);
3768         }
3769 }
3770
3771 static const struct ethtool_ops ath6kl_ethtool_ops = {
3772         .get_drvinfo = cfg80211_get_drvinfo,
3773         .get_link = ethtool_op_get_link,
3774         .get_strings = ath6kl_get_strings,
3775         .get_ethtool_stats = ath6kl_get_stats,
3776         .get_sset_count = ath6kl_get_sset_count,
3777 };
3778
3779 struct wireless_dev *ath6kl_interface_add(struct ath6kl *ar, const char *name,
3780                                           unsigned char name_assign_type,
3781                                           enum nl80211_iftype type,
3782                                           u8 fw_vif_idx, u8 nw_type)
3783 {
3784         struct net_device *ndev;
3785         struct ath6kl_vif *vif;
3786
3787         ndev = alloc_netdev(sizeof(*vif), name, name_assign_type, ether_setup);
3788         if (!ndev)
3789                 return NULL;
3790
3791         vif = netdev_priv(ndev);
3792         ndev->ieee80211_ptr = &vif->wdev;
3793         vif->wdev.wiphy = ar->wiphy;
3794         vif->ar = ar;
3795         vif->ndev = ndev;
3796         SET_NETDEV_DEV(ndev, wiphy_dev(vif->wdev.wiphy));
3797         vif->wdev.netdev = ndev;
3798         vif->wdev.iftype = type;
3799         vif->fw_vif_idx = fw_vif_idx;
3800         vif->nw_type = nw_type;
3801         vif->next_mode = nw_type;
3802         vif->listen_intvl_t = ATH6KL_DEFAULT_LISTEN_INTVAL;
3803         vif->bmiss_time_t = ATH6KL_DEFAULT_BMISS_TIME;
3804         vif->bg_scan_period = 0;
3805         vif->htcap[NL80211_BAND_2GHZ].ht_enable = true;
3806         vif->htcap[NL80211_BAND_5GHZ].ht_enable = true;
3807
3808         memcpy(ndev->dev_addr, ar->mac_addr, ETH_ALEN);
3809         if (fw_vif_idx != 0) {
3810                 ndev->dev_addr[0] = (ndev->dev_addr[0] ^ (1 << fw_vif_idx)) |
3811                                      0x2;
3812                 if (test_bit(ATH6KL_FW_CAPABILITY_CUSTOM_MAC_ADDR,
3813                              ar->fw_capabilities))
3814                         ndev->dev_addr[4] ^= 0x80;
3815         }
3816
3817         init_netdev(ndev);
3818
3819         ath6kl_init_control_info(vif);
3820
3821         if (ath6kl_cfg80211_vif_init(vif))
3822                 goto err;
3823
3824         netdev_set_default_ethtool_ops(ndev, &ath6kl_ethtool_ops);
3825
3826         if (register_netdevice(ndev))
3827                 goto err;
3828
3829         ar->avail_idx_map &= ~BIT(fw_vif_idx);
3830         vif->sme_state = SME_DISCONNECTED;
3831         set_bit(WLAN_ENABLED, &vif->flags);
3832         ar->wlan_pwr_state = WLAN_POWER_STATE_ON;
3833
3834         if (type == NL80211_IFTYPE_ADHOC)
3835                 ar->ibss_if_active = true;
3836
3837         spin_lock_bh(&ar->list_lock);
3838         list_add_tail(&vif->list, &ar->vif_list);
3839         spin_unlock_bh(&ar->list_lock);
3840
3841         return &vif->wdev;
3842
3843 err:
3844         aggr_module_destroy(vif->aggr_cntxt);
3845         free_netdev(ndev);
3846         return NULL;
3847 }
3848
3849 #ifdef CONFIG_PM
3850 static const struct wiphy_wowlan_support ath6kl_wowlan_support = {
3851         .flags = WIPHY_WOWLAN_MAGIC_PKT |
3852                  WIPHY_WOWLAN_DISCONNECT |
3853                  WIPHY_WOWLAN_GTK_REKEY_FAILURE  |
3854                  WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
3855                  WIPHY_WOWLAN_EAP_IDENTITY_REQ   |
3856                  WIPHY_WOWLAN_4WAY_HANDSHAKE,
3857         .n_patterns = WOW_MAX_FILTERS_PER_LIST,
3858         .pattern_min_len = 1,
3859         .pattern_max_len = WOW_PATTERN_SIZE,
3860 };
3861 #endif
3862
3863 int ath6kl_cfg80211_init(struct ath6kl *ar)
3864 {
3865         struct wiphy *wiphy = ar->wiphy;
3866         bool band_2gig = false, band_5gig = false, ht = false;
3867         int ret;
3868
3869         wiphy->mgmt_stypes = ath6kl_mgmt_stypes;
3870
3871         wiphy->max_remain_on_channel_duration = 5000;
3872
3873         /* set device pointer for wiphy */
3874         set_wiphy_dev(wiphy, ar->dev);
3875
3876         wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
3877                                  BIT(NL80211_IFTYPE_ADHOC) |
3878                                  BIT(NL80211_IFTYPE_AP);
3879         if (ar->p2p) {
3880                 wiphy->interface_modes |= BIT(NL80211_IFTYPE_P2P_GO) |
3881                                           BIT(NL80211_IFTYPE_P2P_CLIENT);
3882         }
3883
3884         if (config_enabled(CONFIG_ATH6KL_REGDOMAIN) &&
3885             test_bit(ATH6KL_FW_CAPABILITY_REGDOMAIN, ar->fw_capabilities)) {
3886                 wiphy->reg_notifier = ath6kl_cfg80211_reg_notify;
3887                 ar->wiphy->features |= NL80211_FEATURE_CELL_BASE_REG_HINTS;
3888         }
3889
3890         /* max num of ssids that can be probed during scanning */
3891         wiphy->max_scan_ssids = MAX_PROBED_SSIDS;
3892
3893         /* max num of ssids that can be matched after scan */
3894         if (test_bit(ATH6KL_FW_CAPABILITY_SCHED_SCAN_MATCH_LIST,
3895                      ar->fw_capabilities))
3896                 wiphy->max_match_sets = MAX_PROBED_SSIDS;
3897
3898         wiphy->max_scan_ie_len = 1000; /* FIX: what is correct limit? */
3899         switch (ar->hw.cap) {
3900         case WMI_11AN_CAP:
3901                 ht = true;
3902         case WMI_11A_CAP:
3903                 band_5gig = true;
3904                 break;
3905         case WMI_11GN_CAP:
3906                 ht = true;
3907         case WMI_11G_CAP:
3908                 band_2gig = true;
3909                 break;
3910         case WMI_11AGN_CAP:
3911                 ht = true;
3912         case WMI_11AG_CAP:
3913                 band_2gig = true;
3914                 band_5gig = true;
3915                 break;
3916         default:
3917                 ath6kl_err("invalid phy capability!\n");
3918                 return -EINVAL;
3919         }
3920
3921         /*
3922          * Even if the fw has HT support, advertise HT cap only when
3923          * the firmware has support to override RSN capability, otherwise
3924          * 4-way handshake would fail.
3925          */
3926         if (!(ht &&
3927               test_bit(ATH6KL_FW_CAPABILITY_RSN_CAP_OVERRIDE,
3928                        ar->fw_capabilities))) {
3929                 ath6kl_band_2ghz.ht_cap.cap = 0;
3930                 ath6kl_band_2ghz.ht_cap.ht_supported = false;
3931                 ath6kl_band_5ghz.ht_cap.cap = 0;
3932                 ath6kl_band_5ghz.ht_cap.ht_supported = false;
3933
3934                 if (ht)
3935                         ath6kl_err("Firmware lacks RSN-CAP-OVERRIDE, so HT (802.11n) is disabled.");
3936         }
3937
3938         if (test_bit(ATH6KL_FW_CAPABILITY_64BIT_RATES,
3939                      ar->fw_capabilities)) {
3940                 ath6kl_band_2ghz.ht_cap.mcs.rx_mask[0] = 0xff;
3941                 ath6kl_band_5ghz.ht_cap.mcs.rx_mask[0] = 0xff;
3942                 ath6kl_band_2ghz.ht_cap.mcs.rx_mask[1] = 0xff;
3943                 ath6kl_band_5ghz.ht_cap.mcs.rx_mask[1] = 0xff;
3944                 ar->hw.tx_ant = 0x3; /* mask, 2 antenna */
3945                 ar->hw.rx_ant = 0x3;
3946         } else {
3947                 ath6kl_band_2ghz.ht_cap.mcs.rx_mask[0] = 0xff;
3948                 ath6kl_band_5ghz.ht_cap.mcs.rx_mask[0] = 0xff;
3949                 ar->hw.tx_ant = 1;
3950                 ar->hw.rx_ant = 1;
3951         }
3952
3953         wiphy->available_antennas_tx = ar->hw.tx_ant;
3954         wiphy->available_antennas_rx = ar->hw.rx_ant;
3955
3956         if (band_2gig)
3957                 wiphy->bands[NL80211_BAND_2GHZ] = &ath6kl_band_2ghz;
3958         if (band_5gig)
3959                 wiphy->bands[NL80211_BAND_5GHZ] = &ath6kl_band_5ghz;
3960
3961         wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
3962
3963         wiphy->cipher_suites = cipher_suites;
3964         wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
3965
3966 #ifdef CONFIG_PM
3967         wiphy->wowlan = &ath6kl_wowlan_support;
3968 #endif
3969
3970         wiphy->max_sched_scan_ssids = MAX_PROBED_SSIDS;
3971
3972         ar->wiphy->flags |= WIPHY_FLAG_SUPPORTS_FW_ROAM |
3973                             WIPHY_FLAG_HAVE_AP_SME |
3974                             WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
3975                             WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
3976
3977         if (test_bit(ATH6KL_FW_CAPABILITY_SCHED_SCAN_V2, ar->fw_capabilities))
3978                 ar->wiphy->flags |= WIPHY_FLAG_SUPPORTS_SCHED_SCAN;
3979
3980         if (test_bit(ATH6KL_FW_CAPABILITY_INACTIVITY_TIMEOUT,
3981                      ar->fw_capabilities))
3982                 ar->wiphy->features |= NL80211_FEATURE_INACTIVITY_TIMER;
3983
3984         ar->wiphy->probe_resp_offload =
3985                 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
3986                 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
3987                 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
3988
3989         ret = wiphy_register(wiphy);
3990         if (ret < 0) {
3991                 ath6kl_err("couldn't register wiphy device\n");
3992                 return ret;
3993         }
3994
3995         ar->wiphy_registered = true;
3996
3997         return 0;
3998 }
3999
4000 void ath6kl_cfg80211_cleanup(struct ath6kl *ar)
4001 {
4002         wiphy_unregister(ar->wiphy);
4003
4004         ar->wiphy_registered = false;
4005 }
4006
4007 struct ath6kl *ath6kl_cfg80211_create(void)
4008 {
4009         struct ath6kl *ar;
4010         struct wiphy *wiphy;
4011
4012         /* create a new wiphy for use with cfg80211 */
4013         wiphy = wiphy_new(&ath6kl_cfg80211_ops, sizeof(struct ath6kl));
4014
4015         if (!wiphy) {
4016                 ath6kl_err("couldn't allocate wiphy device\n");
4017                 return NULL;
4018         }
4019
4020         ar = wiphy_priv(wiphy);
4021         ar->wiphy = wiphy;
4022
4023         return ar;
4024 }
4025
4026 /* Note: ar variable must not be accessed after calling this! */
4027 void ath6kl_cfg80211_destroy(struct ath6kl *ar)
4028 {
4029         int i;
4030
4031         for (i = 0; i < AP_MAX_NUM_STA; i++)
4032                 kfree(ar->sta_list[i].aggr_conn);
4033
4034         wiphy_free(ar->wiphy);
4035 }
4036