1bdcd8170SKalle Valo /*
2bdcd8170SKalle Valo  * Copyright (c) 2004-2011 Atheros Communications Inc.
31b2df407SVasanthakumar Thiagarajan  * Copyright (c) 2011-2012 Qualcomm Atheros, Inc.
4bdcd8170SKalle Valo  *
5bdcd8170SKalle Valo  * Permission to use, copy, modify, and/or distribute this software for any
6bdcd8170SKalle Valo  * purpose with or without fee is hereby granted, provided that the above
7bdcd8170SKalle Valo  * copyright notice and this permission notice appear in all copies.
8bdcd8170SKalle Valo  *
9bdcd8170SKalle Valo  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10bdcd8170SKalle Valo  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11bdcd8170SKalle Valo  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12bdcd8170SKalle Valo  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13bdcd8170SKalle Valo  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14bdcd8170SKalle Valo  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15bdcd8170SKalle Valo  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16bdcd8170SKalle Valo  */
17bdcd8170SKalle Valo 
186eb07cafSPaul Gortmaker #include <linux/moduleparam.h>
19c08631c6SRaja Mani #include <linux/inetdevice.h>
20d6a434d6SKalle Valo #include <linux/export.h>
216eb07cafSPaul Gortmaker 
22bdcd8170SKalle Valo #include "core.h"
23bdcd8170SKalle Valo #include "cfg80211.h"
24bdcd8170SKalle Valo #include "debug.h"
25abcb344bSKalle Valo #include "hif-ops.h"
26003353b0SKalle Valo #include "testmode.h"
27bdcd8170SKalle Valo 
28bdcd8170SKalle Valo #define RATETAB_ENT(_rate, _rateid, _flags) {   \
29bdcd8170SKalle Valo 	.bitrate    = (_rate),                  \
30bdcd8170SKalle Valo 	.flags      = (_flags),                 \
31bdcd8170SKalle Valo 	.hw_value   = (_rateid),                \
32bdcd8170SKalle Valo }
33bdcd8170SKalle Valo 
34bdcd8170SKalle Valo #define CHAN2G(_channel, _freq, _flags) {   \
35bdcd8170SKalle Valo 	.band           = IEEE80211_BAND_2GHZ,  \
36bdcd8170SKalle Valo 	.hw_value       = (_channel),           \
37bdcd8170SKalle Valo 	.center_freq    = (_freq),              \
38bdcd8170SKalle Valo 	.flags          = (_flags),             \
39bdcd8170SKalle Valo 	.max_antenna_gain   = 0,                \
40bdcd8170SKalle Valo 	.max_power      = 30,                   \
41bdcd8170SKalle Valo }
42bdcd8170SKalle Valo 
43bdcd8170SKalle Valo #define CHAN5G(_channel, _flags) {		    \
44bdcd8170SKalle Valo 	.band           = IEEE80211_BAND_5GHZ,      \
45bdcd8170SKalle Valo 	.hw_value       = (_channel),               \
46bdcd8170SKalle Valo 	.center_freq    = 5000 + (5 * (_channel)),  \
47bdcd8170SKalle Valo 	.flags          = (_flags),                 \
48bdcd8170SKalle Valo 	.max_antenna_gain   = 0,                    \
49bdcd8170SKalle Valo 	.max_power      = 30,                       \
50bdcd8170SKalle Valo }
51bdcd8170SKalle Valo 
52f599359cSBala Shanmugam #define DEFAULT_BG_SCAN_PERIOD 60
53f599359cSBala Shanmugam 
54bdcd8170SKalle Valo static struct ieee80211_rate ath6kl_rates[] = {
55bdcd8170SKalle Valo 	RATETAB_ENT(10, 0x1, 0),
56bdcd8170SKalle Valo 	RATETAB_ENT(20, 0x2, 0),
57bdcd8170SKalle Valo 	RATETAB_ENT(55, 0x4, 0),
58bdcd8170SKalle Valo 	RATETAB_ENT(110, 0x8, 0),
59bdcd8170SKalle Valo 	RATETAB_ENT(60, 0x10, 0),
60bdcd8170SKalle Valo 	RATETAB_ENT(90, 0x20, 0),
61bdcd8170SKalle Valo 	RATETAB_ENT(120, 0x40, 0),
62bdcd8170SKalle Valo 	RATETAB_ENT(180, 0x80, 0),
63bdcd8170SKalle Valo 	RATETAB_ENT(240, 0x100, 0),
64bdcd8170SKalle Valo 	RATETAB_ENT(360, 0x200, 0),
65bdcd8170SKalle Valo 	RATETAB_ENT(480, 0x400, 0),
66bdcd8170SKalle Valo 	RATETAB_ENT(540, 0x800, 0),
67bdcd8170SKalle Valo };
68bdcd8170SKalle Valo 
69bdcd8170SKalle Valo #define ath6kl_a_rates     (ath6kl_rates + 4)
70bdcd8170SKalle Valo #define ath6kl_a_rates_size    8
71bdcd8170SKalle Valo #define ath6kl_g_rates     (ath6kl_rates + 0)
72bdcd8170SKalle Valo #define ath6kl_g_rates_size    12
73bdcd8170SKalle Valo 
74faaf1929SVasanthakumar Thiagarajan #define ath6kl_g_htcap (IEEE80211_HT_CAP_SUP_WIDTH_20_40 | \
75faaf1929SVasanthakumar Thiagarajan 			IEEE80211_HT_CAP_SGI_20		 | \
76faaf1929SVasanthakumar Thiagarajan 			IEEE80211_HT_CAP_SGI_40)
77faaf1929SVasanthakumar Thiagarajan 
78bdcd8170SKalle Valo static struct ieee80211_channel ath6kl_2ghz_channels[] = {
79bdcd8170SKalle Valo 	CHAN2G(1, 2412, 0),
80bdcd8170SKalle Valo 	CHAN2G(2, 2417, 0),
81bdcd8170SKalle Valo 	CHAN2G(3, 2422, 0),
82bdcd8170SKalle Valo 	CHAN2G(4, 2427, 0),
83bdcd8170SKalle Valo 	CHAN2G(5, 2432, 0),
84bdcd8170SKalle Valo 	CHAN2G(6, 2437, 0),
85bdcd8170SKalle Valo 	CHAN2G(7, 2442, 0),
86bdcd8170SKalle Valo 	CHAN2G(8, 2447, 0),
87bdcd8170SKalle Valo 	CHAN2G(9, 2452, 0),
88bdcd8170SKalle Valo 	CHAN2G(10, 2457, 0),
89bdcd8170SKalle Valo 	CHAN2G(11, 2462, 0),
90bdcd8170SKalle Valo 	CHAN2G(12, 2467, 0),
91bdcd8170SKalle Valo 	CHAN2G(13, 2472, 0),
92bdcd8170SKalle Valo 	CHAN2G(14, 2484, 0),
93bdcd8170SKalle Valo };
94bdcd8170SKalle Valo 
95bdcd8170SKalle Valo static struct ieee80211_channel ath6kl_5ghz_a_channels[] = {
96bdcd8170SKalle Valo 	CHAN5G(34, 0), CHAN5G(36, 0),
97bdcd8170SKalle Valo 	CHAN5G(38, 0), CHAN5G(40, 0),
98bdcd8170SKalle Valo 	CHAN5G(42, 0), CHAN5G(44, 0),
99bdcd8170SKalle Valo 	CHAN5G(46, 0), CHAN5G(48, 0),
100bdcd8170SKalle Valo 	CHAN5G(52, 0), CHAN5G(56, 0),
101bdcd8170SKalle Valo 	CHAN5G(60, 0), CHAN5G(64, 0),
102bdcd8170SKalle Valo 	CHAN5G(100, 0), CHAN5G(104, 0),
103bdcd8170SKalle Valo 	CHAN5G(108, 0), CHAN5G(112, 0),
104bdcd8170SKalle Valo 	CHAN5G(116, 0), CHAN5G(120, 0),
105bdcd8170SKalle Valo 	CHAN5G(124, 0), CHAN5G(128, 0),
106bdcd8170SKalle Valo 	CHAN5G(132, 0), CHAN5G(136, 0),
107bdcd8170SKalle Valo 	CHAN5G(140, 0), CHAN5G(149, 0),
108bdcd8170SKalle Valo 	CHAN5G(153, 0), CHAN5G(157, 0),
109bdcd8170SKalle Valo 	CHAN5G(161, 0), CHAN5G(165, 0),
110bdcd8170SKalle Valo 	CHAN5G(184, 0), CHAN5G(188, 0),
111bdcd8170SKalle Valo 	CHAN5G(192, 0), CHAN5G(196, 0),
112bdcd8170SKalle Valo 	CHAN5G(200, 0), CHAN5G(204, 0),
113bdcd8170SKalle Valo 	CHAN5G(208, 0), CHAN5G(212, 0),
114bdcd8170SKalle Valo 	CHAN5G(216, 0),
115bdcd8170SKalle Valo };
116bdcd8170SKalle Valo 
117bdcd8170SKalle Valo static struct ieee80211_supported_band ath6kl_band_2ghz = {
118bdcd8170SKalle Valo 	.n_channels = ARRAY_SIZE(ath6kl_2ghz_channels),
119bdcd8170SKalle Valo 	.channels = ath6kl_2ghz_channels,
120bdcd8170SKalle Valo 	.n_bitrates = ath6kl_g_rates_size,
121bdcd8170SKalle Valo 	.bitrates = ath6kl_g_rates,
122faaf1929SVasanthakumar Thiagarajan 	.ht_cap.cap = ath6kl_g_htcap,
123faaf1929SVasanthakumar Thiagarajan 	.ht_cap.ht_supported = true,
124bdcd8170SKalle Valo };
125bdcd8170SKalle Valo 
126bdcd8170SKalle Valo static struct ieee80211_supported_band ath6kl_band_5ghz = {
127bdcd8170SKalle Valo 	.n_channels = ARRAY_SIZE(ath6kl_5ghz_a_channels),
128bdcd8170SKalle Valo 	.channels = ath6kl_5ghz_a_channels,
129bdcd8170SKalle Valo 	.n_bitrates = ath6kl_a_rates_size,
130bdcd8170SKalle Valo 	.bitrates = ath6kl_a_rates,
131faaf1929SVasanthakumar Thiagarajan 	.ht_cap.cap = ath6kl_g_htcap,
132faaf1929SVasanthakumar Thiagarajan 	.ht_cap.ht_supported = true,
133bdcd8170SKalle Valo };
134bdcd8170SKalle Valo 
135837cb97eSJouni Malinen #define CCKM_KRK_CIPHER_SUITE 0x004096ff /* use for KRK */
136837cb97eSJouni Malinen 
13710509f90SKalle Valo /* returns true if scheduled scan was stopped */
13810509f90SKalle Valo static bool __ath6kl_cfg80211_sscan_stop(struct ath6kl_vif *vif)
13910509f90SKalle Valo {
14010509f90SKalle Valo 	struct ath6kl *ar = vif->ar;
14110509f90SKalle Valo 
14210509f90SKalle Valo 	if (ar->state != ATH6KL_STATE_SCHED_SCAN)
14310509f90SKalle Valo 		return false;
14410509f90SKalle Valo 
14510509f90SKalle Valo 	del_timer_sync(&vif->sched_scan_timer);
14610509f90SKalle Valo 
14710509f90SKalle Valo 	ath6kl_wmi_set_host_sleep_mode_cmd(ar->wmi, vif->fw_vif_idx,
14810509f90SKalle Valo 					   ATH6KL_HOST_MODE_AWAKE);
14910509f90SKalle Valo 
15010509f90SKalle Valo 	ar->state = ATH6KL_STATE_ON;
15110509f90SKalle Valo 
15210509f90SKalle Valo 	return true;
15310509f90SKalle Valo }
15410509f90SKalle Valo 
15510509f90SKalle Valo static void ath6kl_cfg80211_sscan_disable(struct ath6kl_vif *vif)
15610509f90SKalle Valo {
15710509f90SKalle Valo 	struct ath6kl *ar = vif->ar;
15810509f90SKalle Valo 	bool stopped;
15910509f90SKalle Valo 
16010509f90SKalle Valo 	stopped = __ath6kl_cfg80211_sscan_stop(vif);
16110509f90SKalle Valo 
16210509f90SKalle Valo 	if (!stopped)
16310509f90SKalle Valo 		return;
16410509f90SKalle Valo 
16510509f90SKalle Valo 	cfg80211_sched_scan_stopped(ar->wiphy);
16610509f90SKalle Valo }
16710509f90SKalle Valo 
168240d2799SVasanthakumar Thiagarajan static int ath6kl_set_wpa_version(struct ath6kl_vif *vif,
169bdcd8170SKalle Valo 				  enum nl80211_wpa_versions wpa_version)
170bdcd8170SKalle Valo {
171bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: %u\n", __func__, wpa_version);
172bdcd8170SKalle Valo 
173bdcd8170SKalle Valo 	if (!wpa_version) {
1743450334fSVasanthakumar Thiagarajan 		vif->auth_mode = NONE_AUTH;
175bdcd8170SKalle Valo 	} else if (wpa_version & NL80211_WPA_VERSION_2) {
1763450334fSVasanthakumar Thiagarajan 		vif->auth_mode = WPA2_AUTH;
177bdcd8170SKalle Valo 	} else if (wpa_version & NL80211_WPA_VERSION_1) {
1783450334fSVasanthakumar Thiagarajan 		vif->auth_mode = WPA_AUTH;
179bdcd8170SKalle Valo 	} else {
180bdcd8170SKalle Valo 		ath6kl_err("%s: %u not supported\n", __func__, wpa_version);
181bdcd8170SKalle Valo 		return -ENOTSUPP;
182bdcd8170SKalle Valo 	}
183bdcd8170SKalle Valo 
184bdcd8170SKalle Valo 	return 0;
185bdcd8170SKalle Valo }
186bdcd8170SKalle Valo 
187240d2799SVasanthakumar Thiagarajan static int ath6kl_set_auth_type(struct ath6kl_vif *vif,
188bdcd8170SKalle Valo 				enum nl80211_auth_type auth_type)
189bdcd8170SKalle Valo {
190bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: 0x%x\n", __func__, auth_type);
191bdcd8170SKalle Valo 
192bdcd8170SKalle Valo 	switch (auth_type) {
193bdcd8170SKalle Valo 	case NL80211_AUTHTYPE_OPEN_SYSTEM:
1943450334fSVasanthakumar Thiagarajan 		vif->dot11_auth_mode = OPEN_AUTH;
195bdcd8170SKalle Valo 		break;
196bdcd8170SKalle Valo 	case NL80211_AUTHTYPE_SHARED_KEY:
1973450334fSVasanthakumar Thiagarajan 		vif->dot11_auth_mode = SHARED_AUTH;
198bdcd8170SKalle Valo 		break;
199bdcd8170SKalle Valo 	case NL80211_AUTHTYPE_NETWORK_EAP:
2003450334fSVasanthakumar Thiagarajan 		vif->dot11_auth_mode = LEAP_AUTH;
201bdcd8170SKalle Valo 		break;
202bdcd8170SKalle Valo 
203bdcd8170SKalle Valo 	case NL80211_AUTHTYPE_AUTOMATIC:
2043450334fSVasanthakumar Thiagarajan 		vif->dot11_auth_mode = OPEN_AUTH | SHARED_AUTH;
205bdcd8170SKalle Valo 		break;
206bdcd8170SKalle Valo 
207bdcd8170SKalle Valo 	default:
2083c325fbdSMasanari Iida 		ath6kl_err("%s: 0x%x not supported\n", __func__, auth_type);
209bdcd8170SKalle Valo 		return -ENOTSUPP;
210bdcd8170SKalle Valo 	}
211bdcd8170SKalle Valo 
212bdcd8170SKalle Valo 	return 0;
213bdcd8170SKalle Valo }
214bdcd8170SKalle Valo 
215240d2799SVasanthakumar Thiagarajan static int ath6kl_set_cipher(struct ath6kl_vif *vif, u32 cipher, bool ucast)
216bdcd8170SKalle Valo {
2173450334fSVasanthakumar Thiagarajan 	u8 *ar_cipher = ucast ? &vif->prwise_crypto : &vif->grp_crypto;
2183450334fSVasanthakumar Thiagarajan 	u8 *ar_cipher_len = ucast ? &vif->prwise_crypto_len :
2193450334fSVasanthakumar Thiagarajan 		&vif->grp_crypto_len;
220bdcd8170SKalle Valo 
221bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: cipher 0x%x, ucast %u\n",
222bdcd8170SKalle Valo 		   __func__, cipher, ucast);
223bdcd8170SKalle Valo 
224bdcd8170SKalle Valo 	switch (cipher) {
225bdcd8170SKalle Valo 	case 0:
226bdcd8170SKalle Valo 		/* our own hack to use value 0 as no crypto used */
227bdcd8170SKalle Valo 		*ar_cipher = NONE_CRYPT;
228bdcd8170SKalle Valo 		*ar_cipher_len = 0;
229bdcd8170SKalle Valo 		break;
230bdcd8170SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
231bdcd8170SKalle Valo 		*ar_cipher = WEP_CRYPT;
232bdcd8170SKalle Valo 		*ar_cipher_len = 5;
233bdcd8170SKalle Valo 		break;
234bdcd8170SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
235bdcd8170SKalle Valo 		*ar_cipher = WEP_CRYPT;
236bdcd8170SKalle Valo 		*ar_cipher_len = 13;
237bdcd8170SKalle Valo 		break;
238bdcd8170SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
239bdcd8170SKalle Valo 		*ar_cipher = TKIP_CRYPT;
240bdcd8170SKalle Valo 		*ar_cipher_len = 0;
241bdcd8170SKalle Valo 		break;
242bdcd8170SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
243bdcd8170SKalle Valo 		*ar_cipher = AES_CRYPT;
244bdcd8170SKalle Valo 		*ar_cipher_len = 0;
245bdcd8170SKalle Valo 		break;
2465e07021eSDai Shuibing 	case WLAN_CIPHER_SUITE_SMS4:
2475e07021eSDai Shuibing 		*ar_cipher = WAPI_CRYPT;
2485e07021eSDai Shuibing 		*ar_cipher_len = 0;
2495e07021eSDai Shuibing 		break;
250bdcd8170SKalle Valo 	default:
251bdcd8170SKalle Valo 		ath6kl_err("cipher 0x%x not supported\n", cipher);
252bdcd8170SKalle Valo 		return -ENOTSUPP;
253bdcd8170SKalle Valo 	}
254bdcd8170SKalle Valo 
255bdcd8170SKalle Valo 	return 0;
256bdcd8170SKalle Valo }
257bdcd8170SKalle Valo 
258240d2799SVasanthakumar Thiagarajan static void ath6kl_set_key_mgmt(struct ath6kl_vif *vif, u32 key_mgmt)
259bdcd8170SKalle Valo {
260bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: 0x%x\n", __func__, key_mgmt);
261bdcd8170SKalle Valo 
262bdcd8170SKalle Valo 	if (key_mgmt == WLAN_AKM_SUITE_PSK) {
2633450334fSVasanthakumar Thiagarajan 		if (vif->auth_mode == WPA_AUTH)
2643450334fSVasanthakumar Thiagarajan 			vif->auth_mode = WPA_PSK_AUTH;
2653450334fSVasanthakumar Thiagarajan 		else if (vif->auth_mode == WPA2_AUTH)
2663450334fSVasanthakumar Thiagarajan 			vif->auth_mode = WPA2_PSK_AUTH;
267837cb97eSJouni Malinen 	} else if (key_mgmt == 0x00409600) {
2683450334fSVasanthakumar Thiagarajan 		if (vif->auth_mode == WPA_AUTH)
2693450334fSVasanthakumar Thiagarajan 			vif->auth_mode = WPA_AUTH_CCKM;
2703450334fSVasanthakumar Thiagarajan 		else if (vif->auth_mode == WPA2_AUTH)
2713450334fSVasanthakumar Thiagarajan 			vif->auth_mode = WPA2_AUTH_CCKM;
272bdcd8170SKalle Valo 	} else if (key_mgmt != WLAN_AKM_SUITE_8021X) {
2733450334fSVasanthakumar Thiagarajan 		vif->auth_mode = NONE_AUTH;
274bdcd8170SKalle Valo 	}
275bdcd8170SKalle Valo }
276bdcd8170SKalle Valo 
277990bd915SVasanthakumar Thiagarajan static bool ath6kl_cfg80211_ready(struct ath6kl_vif *vif)
278bdcd8170SKalle Valo {
279990bd915SVasanthakumar Thiagarajan 	struct ath6kl *ar = vif->ar;
28059c98449SVasanthakumar Thiagarajan 
281bdcd8170SKalle Valo 	if (!test_bit(WMI_READY, &ar->flag)) {
282bdcd8170SKalle Valo 		ath6kl_err("wmi is not ready\n");
283bdcd8170SKalle Valo 		return false;
284bdcd8170SKalle Valo 	}
285bdcd8170SKalle Valo 
28659c98449SVasanthakumar Thiagarajan 	if (!test_bit(WLAN_ENABLED, &vif->flags)) {
287bdcd8170SKalle Valo 		ath6kl_err("wlan disabled\n");
288bdcd8170SKalle Valo 		return false;
289bdcd8170SKalle Valo 	}
290bdcd8170SKalle Valo 
291bdcd8170SKalle Valo 	return true;
292bdcd8170SKalle Valo }
293bdcd8170SKalle Valo 
2946981ffdcSKevin Fang static bool ath6kl_is_wpa_ie(const u8 *pos)
2956981ffdcSKevin Fang {
2966981ffdcSKevin Fang 	return pos[0] == WLAN_EID_WPA && pos[1] >= 4 &&
2976981ffdcSKevin Fang 		pos[2] == 0x00 && pos[3] == 0x50 &&
2986981ffdcSKevin Fang 		pos[4] == 0xf2 && pos[5] == 0x01;
2996981ffdcSKevin Fang }
3006981ffdcSKevin Fang 
3016981ffdcSKevin Fang static bool ath6kl_is_rsn_ie(const u8 *pos)
3026981ffdcSKevin Fang {
3036981ffdcSKevin Fang 	return pos[0] == WLAN_EID_RSN;
3046981ffdcSKevin Fang }
3056981ffdcSKevin Fang 
30663541212SAarthi Thiruvengadam static bool ath6kl_is_wps_ie(const u8 *pos)
30763541212SAarthi Thiruvengadam {
30863541212SAarthi Thiruvengadam 	return (pos[0] == WLAN_EID_VENDOR_SPECIFIC &&
30963541212SAarthi Thiruvengadam 		pos[1] >= 4 &&
31063541212SAarthi Thiruvengadam 		pos[2] == 0x00 && pos[3] == 0x50 && pos[4] == 0xf2 &&
31163541212SAarthi Thiruvengadam 		pos[5] == 0x04);
31263541212SAarthi Thiruvengadam }
31363541212SAarthi Thiruvengadam 
314334234b5SVasanthakumar Thiagarajan static int ath6kl_set_assoc_req_ies(struct ath6kl_vif *vif, const u8 *ies,
3156981ffdcSKevin Fang 				    size_t ies_len)
3166981ffdcSKevin Fang {
317334234b5SVasanthakumar Thiagarajan 	struct ath6kl *ar = vif->ar;
3186981ffdcSKevin Fang 	const u8 *pos;
3196981ffdcSKevin Fang 	u8 *buf = NULL;
3206981ffdcSKevin Fang 	size_t len = 0;
3216981ffdcSKevin Fang 	int ret;
3226981ffdcSKevin Fang 
3236981ffdcSKevin Fang 	/*
32463541212SAarthi Thiruvengadam 	 * Clear previously set flag
32563541212SAarthi Thiruvengadam 	 */
32663541212SAarthi Thiruvengadam 
32763541212SAarthi Thiruvengadam 	ar->connect_ctrl_flags &= ~CONNECT_WPS_FLAG;
32863541212SAarthi Thiruvengadam 
32963541212SAarthi Thiruvengadam 	/*
3306981ffdcSKevin Fang 	 * Filter out RSN/WPA IE(s)
3316981ffdcSKevin Fang 	 */
3326981ffdcSKevin Fang 
3336981ffdcSKevin Fang 	if (ies && ies_len) {
3346981ffdcSKevin Fang 		buf = kmalloc(ies_len, GFP_KERNEL);
3356981ffdcSKevin Fang 		if (buf == NULL)
3366981ffdcSKevin Fang 			return -ENOMEM;
3376981ffdcSKevin Fang 		pos = ies;
3386981ffdcSKevin Fang 
3396981ffdcSKevin Fang 		while (pos + 1 < ies + ies_len) {
3406981ffdcSKevin Fang 			if (pos + 2 + pos[1] > ies + ies_len)
3416981ffdcSKevin Fang 				break;
3426981ffdcSKevin Fang 			if (!(ath6kl_is_wpa_ie(pos) || ath6kl_is_rsn_ie(pos))) {
3436981ffdcSKevin Fang 				memcpy(buf + len, pos, 2 + pos[1]);
3446981ffdcSKevin Fang 				len += 2 + pos[1];
3456981ffdcSKevin Fang 			}
34663541212SAarthi Thiruvengadam 
34763541212SAarthi Thiruvengadam 			if (ath6kl_is_wps_ie(pos))
34863541212SAarthi Thiruvengadam 				ar->connect_ctrl_flags |= CONNECT_WPS_FLAG;
34963541212SAarthi Thiruvengadam 
3506981ffdcSKevin Fang 			pos += 2 + pos[1];
3516981ffdcSKevin Fang 		}
3526981ffdcSKevin Fang 	}
3536981ffdcSKevin Fang 
354334234b5SVasanthakumar Thiagarajan 	ret = ath6kl_wmi_set_appie_cmd(ar->wmi, vif->fw_vif_idx,
355334234b5SVasanthakumar Thiagarajan 				       WMI_FRAME_ASSOC_REQ, buf, len);
3566981ffdcSKevin Fang 	kfree(buf);
3576981ffdcSKevin Fang 	return ret;
3586981ffdcSKevin Fang }
3596981ffdcSKevin Fang 
36055055976SVasanthakumar Thiagarajan static int ath6kl_nliftype_to_drv_iftype(enum nl80211_iftype type, u8 *nw_type)
36155055976SVasanthakumar Thiagarajan {
36255055976SVasanthakumar Thiagarajan 	switch (type) {
36355055976SVasanthakumar Thiagarajan 	case NL80211_IFTYPE_STATION:
36455055976SVasanthakumar Thiagarajan 		*nw_type = INFRA_NETWORK;
36555055976SVasanthakumar Thiagarajan 		break;
36655055976SVasanthakumar Thiagarajan 	case NL80211_IFTYPE_ADHOC:
36755055976SVasanthakumar Thiagarajan 		*nw_type = ADHOC_NETWORK;
36855055976SVasanthakumar Thiagarajan 		break;
36955055976SVasanthakumar Thiagarajan 	case NL80211_IFTYPE_AP:
37055055976SVasanthakumar Thiagarajan 		*nw_type = AP_NETWORK;
37155055976SVasanthakumar Thiagarajan 		break;
37255055976SVasanthakumar Thiagarajan 	case NL80211_IFTYPE_P2P_CLIENT:
37355055976SVasanthakumar Thiagarajan 		*nw_type = INFRA_NETWORK;
37455055976SVasanthakumar Thiagarajan 		break;
37555055976SVasanthakumar Thiagarajan 	case NL80211_IFTYPE_P2P_GO:
37655055976SVasanthakumar Thiagarajan 		*nw_type = AP_NETWORK;
37755055976SVasanthakumar Thiagarajan 		break;
37855055976SVasanthakumar Thiagarajan 	default:
37955055976SVasanthakumar Thiagarajan 		ath6kl_err("invalid interface type %u\n", type);
38055055976SVasanthakumar Thiagarajan 		return -ENOTSUPP;
38155055976SVasanthakumar Thiagarajan 	}
38255055976SVasanthakumar Thiagarajan 
38355055976SVasanthakumar Thiagarajan 	return 0;
38455055976SVasanthakumar Thiagarajan }
38555055976SVasanthakumar Thiagarajan 
38655055976SVasanthakumar Thiagarajan static bool ath6kl_is_valid_iftype(struct ath6kl *ar, enum nl80211_iftype type,
38755055976SVasanthakumar Thiagarajan 				   u8 *if_idx, u8 *nw_type)
38855055976SVasanthakumar Thiagarajan {
38955055976SVasanthakumar Thiagarajan 	int i;
39055055976SVasanthakumar Thiagarajan 
39155055976SVasanthakumar Thiagarajan 	if (ath6kl_nliftype_to_drv_iftype(type, nw_type))
39255055976SVasanthakumar Thiagarajan 		return false;
39355055976SVasanthakumar Thiagarajan 
39455055976SVasanthakumar Thiagarajan 	if (ar->ibss_if_active || ((type == NL80211_IFTYPE_ADHOC) &&
39555055976SVasanthakumar Thiagarajan 				   ar->num_vif))
39655055976SVasanthakumar Thiagarajan 		return false;
39755055976SVasanthakumar Thiagarajan 
39855055976SVasanthakumar Thiagarajan 	if (type == NL80211_IFTYPE_STATION ||
39955055976SVasanthakumar Thiagarajan 	    type == NL80211_IFTYPE_AP || type == NL80211_IFTYPE_ADHOC) {
40071f96ee6SKalle Valo 		for (i = 0; i < ar->vif_max; i++) {
40155055976SVasanthakumar Thiagarajan 			if ((ar->avail_idx_map >> i) & BIT(0)) {
40255055976SVasanthakumar Thiagarajan 				*if_idx = i;
40355055976SVasanthakumar Thiagarajan 				return true;
40455055976SVasanthakumar Thiagarajan 			}
40555055976SVasanthakumar Thiagarajan 		}
40655055976SVasanthakumar Thiagarajan 	}
40755055976SVasanthakumar Thiagarajan 
4083226f68aSVasanthakumar Thiagarajan 	if (type == NL80211_IFTYPE_P2P_CLIENT ||
4093226f68aSVasanthakumar Thiagarajan 	    type == NL80211_IFTYPE_P2P_GO) {
41071f96ee6SKalle Valo 		for (i = ar->max_norm_iface; i < ar->vif_max; i++) {
4113226f68aSVasanthakumar Thiagarajan 			if ((ar->avail_idx_map >> i) & BIT(0)) {
4123226f68aSVasanthakumar Thiagarajan 				*if_idx = i;
4133226f68aSVasanthakumar Thiagarajan 				return true;
4143226f68aSVasanthakumar Thiagarajan 			}
4153226f68aSVasanthakumar Thiagarajan 		}
4163226f68aSVasanthakumar Thiagarajan 	}
4173226f68aSVasanthakumar Thiagarajan 
41855055976SVasanthakumar Thiagarajan 	return false;
41955055976SVasanthakumar Thiagarajan }
42055055976SVasanthakumar Thiagarajan 
4218c9bb054SKalle Valo static bool ath6kl_is_tx_pending(struct ath6kl *ar)
4228c9bb054SKalle Valo {
4238c9bb054SKalle Valo 	return ar->tx_pending[ath6kl_wmi_get_control_ep(ar->wmi)] == 0;
4248c9bb054SKalle Valo }
4258c9bb054SKalle Valo 
4268c9bb054SKalle Valo 
427bdcd8170SKalle Valo static int ath6kl_cfg80211_connect(struct wiphy *wiphy, struct net_device *dev,
428bdcd8170SKalle Valo 				   struct cfg80211_connect_params *sme)
429bdcd8170SKalle Valo {
430bdcd8170SKalle Valo 	struct ath6kl *ar = ath6kl_priv(dev);
43159c98449SVasanthakumar Thiagarajan 	struct ath6kl_vif *vif = netdev_priv(dev);
432bdcd8170SKalle Valo 	int status;
4333ca9d1fcSAarthi Thiruvengadam 	u8 nw_subtype = (ar->p2p) ? SUBTYPE_P2PDEV : SUBTYPE_NONE;
434ce0dc0cfSRaja Mani 	u16 interval;
435bdcd8170SKalle Valo 
43610509f90SKalle Valo 	ath6kl_cfg80211_sscan_disable(vif);
43710509f90SKalle Valo 
43814ee6f6bSVasanthakumar Thiagarajan 	vif->sme_state = SME_CONNECTING;
439bdcd8170SKalle Valo 
440990bd915SVasanthakumar Thiagarajan 	if (!ath6kl_cfg80211_ready(vif))
441bdcd8170SKalle Valo 		return -EIO;
442bdcd8170SKalle Valo 
443bdcd8170SKalle Valo 	if (test_bit(DESTROY_IN_PROGRESS, &ar->flag)) {
444bdcd8170SKalle Valo 		ath6kl_err("destroy in progress\n");
445bdcd8170SKalle Valo 		return -EBUSY;
446bdcd8170SKalle Valo 	}
447bdcd8170SKalle Valo 
448bdcd8170SKalle Valo 	if (test_bit(SKIP_SCAN, &ar->flag) &&
449bdcd8170SKalle Valo 	    ((sme->channel && sme->channel->center_freq == 0) ||
450bdcd8170SKalle Valo 	     (sme->bssid && is_zero_ether_addr(sme->bssid)))) {
451bdcd8170SKalle Valo 		ath6kl_err("SkipScan: channel or bssid invalid\n");
452bdcd8170SKalle Valo 		return -EINVAL;
453bdcd8170SKalle Valo 	}
454bdcd8170SKalle Valo 
455bdcd8170SKalle Valo 	if (down_interruptible(&ar->sem)) {
456bdcd8170SKalle Valo 		ath6kl_err("busy, couldn't get access\n");
457bdcd8170SKalle Valo 		return -ERESTARTSYS;
458bdcd8170SKalle Valo 	}
459bdcd8170SKalle Valo 
460bdcd8170SKalle Valo 	if (test_bit(DESTROY_IN_PROGRESS, &ar->flag)) {
461bdcd8170SKalle Valo 		ath6kl_err("busy, destroy in progress\n");
462bdcd8170SKalle Valo 		up(&ar->sem);
463bdcd8170SKalle Valo 		return -EBUSY;
464bdcd8170SKalle Valo 	}
465bdcd8170SKalle Valo 
466bdcd8170SKalle Valo 	if (ar->tx_pending[ath6kl_wmi_get_control_ep(ar->wmi)]) {
467bdcd8170SKalle Valo 		/*
468bdcd8170SKalle Valo 		 * sleep until the command queue drains
469bdcd8170SKalle Valo 		 */
470bdcd8170SKalle Valo 		wait_event_interruptible_timeout(ar->event_wq,
4718c9bb054SKalle Valo 						 ath6kl_is_tx_pending(ar),
472bdcd8170SKalle Valo 						 WMI_TIMEOUT);
473bdcd8170SKalle Valo 		if (signal_pending(current)) {
474bdcd8170SKalle Valo 			ath6kl_err("cmd queue drain timeout\n");
475bdcd8170SKalle Valo 			up(&ar->sem);
476bdcd8170SKalle Valo 			return -EINTR;
477bdcd8170SKalle Valo 		}
478bdcd8170SKalle Valo 	}
479bdcd8170SKalle Valo 
480334234b5SVasanthakumar Thiagarajan 	status = ath6kl_set_assoc_req_ies(vif, sme->ie, sme->ie_len);
481743b4518SDan Carpenter 	if (status) {
482743b4518SDan Carpenter 		up(&ar->sem);
4836981ffdcSKevin Fang 		return status;
484743b4518SDan Carpenter 	}
4856e786cb1SJouni Malinen 
4866e786cb1SJouni Malinen 	if (sme->ie == NULL || sme->ie_len == 0)
487542c519aSRaja Mani 		ar->connect_ctrl_flags &= ~CONNECT_WPS_FLAG;
4886981ffdcSKevin Fang 
48959c98449SVasanthakumar Thiagarajan 	if (test_bit(CONNECTED, &vif->flags) &&
4903450334fSVasanthakumar Thiagarajan 	    vif->ssid_len == sme->ssid_len &&
4913450334fSVasanthakumar Thiagarajan 	    !memcmp(vif->ssid, sme->ssid, vif->ssid_len)) {
492cf5333d7SVasanthakumar Thiagarajan 		vif->reconnect_flag = true;
493334234b5SVasanthakumar Thiagarajan 		status = ath6kl_wmi_reconnect_cmd(ar->wmi, vif->fw_vif_idx,
494334234b5SVasanthakumar Thiagarajan 						  vif->req_bssid,
495f74bac54SVasanthakumar Thiagarajan 						  vif->ch_hint);
496bdcd8170SKalle Valo 
497bdcd8170SKalle Valo 		up(&ar->sem);
498bdcd8170SKalle Valo 		if (status) {
499bdcd8170SKalle Valo 			ath6kl_err("wmi_reconnect_cmd failed\n");
500bdcd8170SKalle Valo 			return -EIO;
501bdcd8170SKalle Valo 		}
502bdcd8170SKalle Valo 		return 0;
5033450334fSVasanthakumar Thiagarajan 	} else if (vif->ssid_len == sme->ssid_len &&
5043450334fSVasanthakumar Thiagarajan 		   !memcmp(vif->ssid, sme->ssid, vif->ssid_len)) {
505240d2799SVasanthakumar Thiagarajan 		ath6kl_disconnect(vif);
506bdcd8170SKalle Valo 	}
507bdcd8170SKalle Valo 
5083450334fSVasanthakumar Thiagarajan 	memset(vif->ssid, 0, sizeof(vif->ssid));
5093450334fSVasanthakumar Thiagarajan 	vif->ssid_len = sme->ssid_len;
5103450334fSVasanthakumar Thiagarajan 	memcpy(vif->ssid, sme->ssid, sme->ssid_len);
511bdcd8170SKalle Valo 
512bdcd8170SKalle Valo 	if (sme->channel)
513f74bac54SVasanthakumar Thiagarajan 		vif->ch_hint = sme->channel->center_freq;
514bdcd8170SKalle Valo 
5158c8b65e3SVasanthakumar Thiagarajan 	memset(vif->req_bssid, 0, sizeof(vif->req_bssid));
516bdcd8170SKalle Valo 	if (sme->bssid && !is_broadcast_ether_addr(sme->bssid))
5178c8b65e3SVasanthakumar Thiagarajan 		memcpy(vif->req_bssid, sme->bssid, sizeof(vif->req_bssid));
518bdcd8170SKalle Valo 
519240d2799SVasanthakumar Thiagarajan 	ath6kl_set_wpa_version(vif, sme->crypto.wpa_versions);
520bdcd8170SKalle Valo 
521240d2799SVasanthakumar Thiagarajan 	status = ath6kl_set_auth_type(vif, sme->auth_type);
522bdcd8170SKalle Valo 	if (status) {
523bdcd8170SKalle Valo 		up(&ar->sem);
524bdcd8170SKalle Valo 		return status;
525bdcd8170SKalle Valo 	}
526bdcd8170SKalle Valo 
527bdcd8170SKalle Valo 	if (sme->crypto.n_ciphers_pairwise)
528240d2799SVasanthakumar Thiagarajan 		ath6kl_set_cipher(vif, sme->crypto.ciphers_pairwise[0], true);
529bdcd8170SKalle Valo 	else
530240d2799SVasanthakumar Thiagarajan 		ath6kl_set_cipher(vif, 0, true);
531bdcd8170SKalle Valo 
532240d2799SVasanthakumar Thiagarajan 	ath6kl_set_cipher(vif, sme->crypto.cipher_group, false);
533bdcd8170SKalle Valo 
534bdcd8170SKalle Valo 	if (sme->crypto.n_akm_suites)
535240d2799SVasanthakumar Thiagarajan 		ath6kl_set_key_mgmt(vif, sme->crypto.akm_suites[0]);
536bdcd8170SKalle Valo 
537bdcd8170SKalle Valo 	if ((sme->key_len) &&
5383450334fSVasanthakumar Thiagarajan 	    (vif->auth_mode == NONE_AUTH) &&
5393450334fSVasanthakumar Thiagarajan 	    (vif->prwise_crypto == WEP_CRYPT)) {
540bdcd8170SKalle Valo 		struct ath6kl_key *key = NULL;
541bdcd8170SKalle Valo 
542792ecb33SVivek Natarajan 		if (sme->key_idx > WMI_MAX_KEY_INDEX) {
543bdcd8170SKalle Valo 			ath6kl_err("key index %d out of bounds\n",
544bdcd8170SKalle Valo 				   sme->key_idx);
545bdcd8170SKalle Valo 			up(&ar->sem);
546bdcd8170SKalle Valo 			return -ENOENT;
547bdcd8170SKalle Valo 		}
548bdcd8170SKalle Valo 
5496f2a73f9SVasanthakumar Thiagarajan 		key = &vif->keys[sme->key_idx];
550bdcd8170SKalle Valo 		key->key_len = sme->key_len;
551bdcd8170SKalle Valo 		memcpy(key->key, sme->key, key->key_len);
5523450334fSVasanthakumar Thiagarajan 		key->cipher = vif->prwise_crypto;
5533450334fSVasanthakumar Thiagarajan 		vif->def_txkey_index = sme->key_idx;
554bdcd8170SKalle Valo 
555334234b5SVasanthakumar Thiagarajan 		ath6kl_wmi_addkey_cmd(ar->wmi, vif->fw_vif_idx, sme->key_idx,
5563450334fSVasanthakumar Thiagarajan 				      vif->prwise_crypto,
557bdcd8170SKalle Valo 				      GROUP_USAGE | TX_USAGE,
558bdcd8170SKalle Valo 				      key->key_len,
559f4bb9a6fSJouni Malinen 				      NULL, 0,
560bdcd8170SKalle Valo 				      key->key, KEY_OP_INIT_VAL, NULL,
561bdcd8170SKalle Valo 				      NO_SYNC_WMIFLAG);
562bdcd8170SKalle Valo 	}
563bdcd8170SKalle Valo 
564bdcd8170SKalle Valo 	if (!ar->usr_bss_filter) {
56559c98449SVasanthakumar Thiagarajan 		clear_bit(CLEAR_BSSFILTER_ON_BEACON, &vif->flags);
566240d2799SVasanthakumar Thiagarajan 		if (ath6kl_wmi_bssfilter_cmd(ar->wmi, vif->fw_vif_idx,
567240d2799SVasanthakumar Thiagarajan 					     ALL_BSS_FILTER, 0) != 0) {
568bdcd8170SKalle Valo 			ath6kl_err("couldn't set bss filtering\n");
569bdcd8170SKalle Valo 			up(&ar->sem);
570bdcd8170SKalle Valo 			return -EIO;
571bdcd8170SKalle Valo 		}
572bdcd8170SKalle Valo 	}
573bdcd8170SKalle Valo 
574f5938f24SVasanthakumar Thiagarajan 	vif->nw_type = vif->next_mode;
575bdcd8170SKalle Valo 
5763ca9d1fcSAarthi Thiruvengadam 	if (vif->wdev.iftype == NL80211_IFTYPE_P2P_CLIENT)
5773ca9d1fcSAarthi Thiruvengadam 		nw_subtype = SUBTYPE_P2PCLIENT;
5783ca9d1fcSAarthi Thiruvengadam 
579bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
580bdcd8170SKalle Valo 		   "%s: connect called with authmode %d dot11 auth %d"
581bdcd8170SKalle Valo 		   " PW crypto %d PW crypto len %d GRP crypto %d"
582bdcd8170SKalle Valo 		   " GRP crypto len %d channel hint %u\n",
583bdcd8170SKalle Valo 		   __func__,
5843450334fSVasanthakumar Thiagarajan 		   vif->auth_mode, vif->dot11_auth_mode, vif->prwise_crypto,
5853450334fSVasanthakumar Thiagarajan 		   vif->prwise_crypto_len, vif->grp_crypto,
586f74bac54SVasanthakumar Thiagarajan 		   vif->grp_crypto_len, vif->ch_hint);
587bdcd8170SKalle Valo 
588cf5333d7SVasanthakumar Thiagarajan 	vif->reconnect_flag = 0;
589ce0dc0cfSRaja Mani 
590ce0dc0cfSRaja Mani 	if (vif->nw_type == INFRA_NETWORK) {
591b5283871SKalle Valo 		interval = max_t(u16, vif->listen_intvl_t,
592b5283871SKalle Valo 				 ATH6KL_MAX_WOW_LISTEN_INTL);
593ce0dc0cfSRaja Mani 		status = ath6kl_wmi_listeninterval_cmd(ar->wmi, vif->fw_vif_idx,
594ce0dc0cfSRaja Mani 						       interval,
595ce0dc0cfSRaja Mani 						       0);
596ce0dc0cfSRaja Mani 		if (status) {
597ce0dc0cfSRaja Mani 			ath6kl_err("couldn't set listen intervel\n");
598ce0dc0cfSRaja Mani 			up(&ar->sem);
599ce0dc0cfSRaja Mani 			return status;
600ce0dc0cfSRaja Mani 		}
601ce0dc0cfSRaja Mani 	}
602ce0dc0cfSRaja Mani 
603334234b5SVasanthakumar Thiagarajan 	status = ath6kl_wmi_connect_cmd(ar->wmi, vif->fw_vif_idx, vif->nw_type,
6043450334fSVasanthakumar Thiagarajan 					vif->dot11_auth_mode, vif->auth_mode,
6053450334fSVasanthakumar Thiagarajan 					vif->prwise_crypto,
6063450334fSVasanthakumar Thiagarajan 					vif->prwise_crypto_len,
6073450334fSVasanthakumar Thiagarajan 					vif->grp_crypto, vif->grp_crypto_len,
6083450334fSVasanthakumar Thiagarajan 					vif->ssid_len, vif->ssid,
609f74bac54SVasanthakumar Thiagarajan 					vif->req_bssid, vif->ch_hint,
6103ca9d1fcSAarthi Thiruvengadam 					ar->connect_ctrl_flags, nw_subtype);
611bdcd8170SKalle Valo 
612f599359cSBala Shanmugam 	/* disable background scan if period is 0 */
613f599359cSBala Shanmugam 	if (sme->bg_scan_period == 0)
614f599359cSBala Shanmugam 		sme->bg_scan_period = 0xffff;
615f599359cSBala Shanmugam 
616f599359cSBala Shanmugam 	/* configure default value if not specified */
617f599359cSBala Shanmugam 	if (sme->bg_scan_period == -1)
618f599359cSBala Shanmugam 		sme->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
619f599359cSBala Shanmugam 
620f599359cSBala Shanmugam 	ath6kl_wmi_scanparams_cmd(ar->wmi, vif->fw_vif_idx, 0, 0,
621f599359cSBala Shanmugam 				  sme->bg_scan_period, 0, 0, 0, 3, 0, 0, 0);
622f599359cSBala Shanmugam 
623bdcd8170SKalle Valo 	up(&ar->sem);
624bdcd8170SKalle Valo 
625bdcd8170SKalle Valo 	if (status == -EINVAL) {
6263450334fSVasanthakumar Thiagarajan 		memset(vif->ssid, 0, sizeof(vif->ssid));
6273450334fSVasanthakumar Thiagarajan 		vif->ssid_len = 0;
628bdcd8170SKalle Valo 		ath6kl_err("invalid request\n");
629bdcd8170SKalle Valo 		return -ENOENT;
630bdcd8170SKalle Valo 	} else if (status) {
631bdcd8170SKalle Valo 		ath6kl_err("ath6kl_wmi_connect_cmd failed\n");
632bdcd8170SKalle Valo 		return -EIO;
633bdcd8170SKalle Valo 	}
634bdcd8170SKalle Valo 
635bdcd8170SKalle Valo 	if ((!(ar->connect_ctrl_flags & CONNECT_DO_WPA_OFFLOAD)) &&
636ddc3d77cSKalle Valo 	    ((vif->auth_mode == WPA_PSK_AUTH) ||
637ddc3d77cSKalle Valo 	     (vif->auth_mode == WPA2_PSK_AUTH))) {
638de3ad713SVasanthakumar Thiagarajan 		mod_timer(&vif->disconnect_timer,
639bdcd8170SKalle Valo 			  jiffies + msecs_to_jiffies(DISCON_TIMER_INTVAL));
640bdcd8170SKalle Valo 	}
641bdcd8170SKalle Valo 
642bdcd8170SKalle Valo 	ar->connect_ctrl_flags &= ~CONNECT_DO_WPA_OFFLOAD;
64359c98449SVasanthakumar Thiagarajan 	set_bit(CONNECT_PEND, &vif->flags);
644bdcd8170SKalle Valo 
645bdcd8170SKalle Valo 	return 0;
646bdcd8170SKalle Valo }
647bdcd8170SKalle Valo 
6485e13fd35SVasanthakumar Thiagarajan static struct cfg80211_bss *
6495e13fd35SVasanthakumar Thiagarajan ath6kl_add_bss_if_needed(struct ath6kl_vif *vif,
6504eab6f4fSRaja Mani 			 enum network_type nw_type,
6514eab6f4fSRaja Mani 			 const u8 *bssid,
65201cac476SJouni Malinen 			 struct ieee80211_channel *chan,
6535e13fd35SVasanthakumar Thiagarajan 			 const u8 *beacon_ie,
6545e13fd35SVasanthakumar Thiagarajan 			 size_t beacon_ie_len)
65501cac476SJouni Malinen {
656240d2799SVasanthakumar Thiagarajan 	struct ath6kl *ar = vif->ar;
65701cac476SJouni Malinen 	struct cfg80211_bss *bss;
6584eab6f4fSRaja Mani 	u16 cap_mask, cap_val;
65901cac476SJouni Malinen 	u8 *ie;
66001cac476SJouni Malinen 
6614eab6f4fSRaja Mani 	if (nw_type & ADHOC_NETWORK) {
6624eab6f4fSRaja Mani 		cap_mask = WLAN_CAPABILITY_IBSS;
6634eab6f4fSRaja Mani 		cap_val = WLAN_CAPABILITY_IBSS;
6644eab6f4fSRaja Mani 	} else {
6654eab6f4fSRaja Mani 		cap_mask = WLAN_CAPABILITY_ESS;
6664eab6f4fSRaja Mani 		cap_val = WLAN_CAPABILITY_ESS;
6674eab6f4fSRaja Mani 	}
6684eab6f4fSRaja Mani 
669be98e3a4SVasanthakumar Thiagarajan 	bss = cfg80211_get_bss(ar->wiphy, chan, bssid,
6704eab6f4fSRaja Mani 			       vif->ssid, vif->ssid_len,
6714eab6f4fSRaja Mani 			       cap_mask, cap_val);
67201cac476SJouni Malinen 	if (bss == NULL) {
67301cac476SJouni Malinen 		/*
67401cac476SJouni Malinen 		 * Since cfg80211 may not yet know about the BSS,
67501cac476SJouni Malinen 		 * generate a partial entry until the first BSS info
67601cac476SJouni Malinen 		 * event becomes available.
67701cac476SJouni Malinen 		 *
67801cac476SJouni Malinen 		 * Prepend SSID element since it is not included in the Beacon
67901cac476SJouni Malinen 		 * IEs from the target.
68001cac476SJouni Malinen 		 */
6813450334fSVasanthakumar Thiagarajan 		ie = kmalloc(2 + vif->ssid_len + beacon_ie_len, GFP_KERNEL);
68201cac476SJouni Malinen 		if (ie == NULL)
6835e13fd35SVasanthakumar Thiagarajan 			return NULL;
68401cac476SJouni Malinen 		ie[0] = WLAN_EID_SSID;
6853450334fSVasanthakumar Thiagarajan 		ie[1] = vif->ssid_len;
6863450334fSVasanthakumar Thiagarajan 		memcpy(ie + 2, vif->ssid, vif->ssid_len);
6873450334fSVasanthakumar Thiagarajan 		memcpy(ie + 2 + vif->ssid_len, beacon_ie, beacon_ie_len);
688be98e3a4SVasanthakumar Thiagarajan 		bss = cfg80211_inform_bss(ar->wiphy, chan,
6894eab6f4fSRaja Mani 					  bssid, 0, cap_val, 100,
6903450334fSVasanthakumar Thiagarajan 					  ie, 2 + vif->ssid_len + beacon_ie_len,
69101cac476SJouni Malinen 					  0, GFP_KERNEL);
69201cac476SJouni Malinen 		if (bss)
6934eab6f4fSRaja Mani 			ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "added bss %pM to "
6944eab6f4fSRaja Mani 				   "cfg80211\n", bssid);
69501cac476SJouni Malinen 		kfree(ie);
69601cac476SJouni Malinen 	} else
6975e13fd35SVasanthakumar Thiagarajan 		ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "cfg80211 already has a bss\n");
69801cac476SJouni Malinen 
6995e13fd35SVasanthakumar Thiagarajan 	return bss;
70001cac476SJouni Malinen }
70101cac476SJouni Malinen 
702240d2799SVasanthakumar Thiagarajan void ath6kl_cfg80211_connect_event(struct ath6kl_vif *vif, u16 channel,
703bdcd8170SKalle Valo 				   u8 *bssid, u16 listen_intvl,
704bdcd8170SKalle Valo 				   u16 beacon_intvl,
705bdcd8170SKalle Valo 				   enum network_type nw_type,
706bdcd8170SKalle Valo 				   u8 beacon_ie_len, u8 assoc_req_len,
707bdcd8170SKalle Valo 				   u8 assoc_resp_len, u8 *assoc_info)
708bdcd8170SKalle Valo {
70901cac476SJouni Malinen 	struct ieee80211_channel *chan;
710240d2799SVasanthakumar Thiagarajan 	struct ath6kl *ar = vif->ar;
7115e13fd35SVasanthakumar Thiagarajan 	struct cfg80211_bss *bss;
712bdcd8170SKalle Valo 
713bdcd8170SKalle Valo 	/* capinfo + listen interval */
714bdcd8170SKalle Valo 	u8 assoc_req_ie_offset = sizeof(u16) + sizeof(u16);
715bdcd8170SKalle Valo 
716bdcd8170SKalle Valo 	/* capinfo + status code +  associd */
717bdcd8170SKalle Valo 	u8 assoc_resp_ie_offset = sizeof(u16) + sizeof(u16) + sizeof(u16);
718bdcd8170SKalle Valo 
719bdcd8170SKalle Valo 	u8 *assoc_req_ie = assoc_info + beacon_ie_len + assoc_req_ie_offset;
720bdcd8170SKalle Valo 	u8 *assoc_resp_ie = assoc_info + beacon_ie_len + assoc_req_len +
721bdcd8170SKalle Valo 	    assoc_resp_ie_offset;
722bdcd8170SKalle Valo 
723bdcd8170SKalle Valo 	assoc_req_len -= assoc_req_ie_offset;
724bdcd8170SKalle Valo 	assoc_resp_len -= assoc_resp_ie_offset;
725bdcd8170SKalle Valo 
72632c10874SJouni Malinen 	/*
72732c10874SJouni Malinen 	 * Store Beacon interval here; DTIM period will be available only once
72832c10874SJouni Malinen 	 * a Beacon frame from the AP is seen.
72932c10874SJouni Malinen 	 */
730cf5333d7SVasanthakumar Thiagarajan 	vif->assoc_bss_beacon_int = beacon_intvl;
73159c98449SVasanthakumar Thiagarajan 	clear_bit(DTIM_PERIOD_AVAIL, &vif->flags);
73232c10874SJouni Malinen 
733bdcd8170SKalle Valo 	if (nw_type & ADHOC_NETWORK) {
734551959d8SVasanthakumar Thiagarajan 		if (vif->wdev.iftype != NL80211_IFTYPE_ADHOC) {
735bdcd8170SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
736bdcd8170SKalle Valo 				   "%s: ath6k not in ibss mode\n", __func__);
737bdcd8170SKalle Valo 			return;
738bdcd8170SKalle Valo 		}
739bdcd8170SKalle Valo 	}
740bdcd8170SKalle Valo 
741bdcd8170SKalle Valo 	if (nw_type & INFRA_NETWORK) {
742551959d8SVasanthakumar Thiagarajan 		if (vif->wdev.iftype != NL80211_IFTYPE_STATION &&
743551959d8SVasanthakumar Thiagarajan 		    vif->wdev.iftype != NL80211_IFTYPE_P2P_CLIENT) {
744bdcd8170SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
745bdcd8170SKalle Valo 				   "%s: ath6k not in station mode\n", __func__);
746bdcd8170SKalle Valo 			return;
747bdcd8170SKalle Valo 		}
748bdcd8170SKalle Valo 	}
749bdcd8170SKalle Valo 
750be98e3a4SVasanthakumar Thiagarajan 	chan = ieee80211_get_channel(ar->wiphy, (int) channel);
751bdcd8170SKalle Valo 
7525e13fd35SVasanthakumar Thiagarajan 	bss = ath6kl_add_bss_if_needed(vif, nw_type, bssid, chan,
7535e13fd35SVasanthakumar Thiagarajan 				       assoc_info, beacon_ie_len);
7545e13fd35SVasanthakumar Thiagarajan 	if (!bss) {
7554eab6f4fSRaja Mani 		ath6kl_err("could not add cfg80211 bss entry\n");
756bdcd8170SKalle Valo 		return;
757bdcd8170SKalle Valo 	}
758bdcd8170SKalle Valo 
7594eab6f4fSRaja Mani 	if (nw_type & ADHOC_NETWORK) {
7604eab6f4fSRaja Mani 		ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "ad-hoc %s selected\n",
7614eab6f4fSRaja Mani 			   nw_type & ADHOC_CREATOR ? "creator" : "joiner");
7624eab6f4fSRaja Mani 		cfg80211_ibss_joined(vif->ndev, bssid, GFP_KERNEL);
7635e13fd35SVasanthakumar Thiagarajan 		cfg80211_put_bss(bss);
76401cac476SJouni Malinen 		return;
76501cac476SJouni Malinen 	}
76601cac476SJouni Malinen 
76714ee6f6bSVasanthakumar Thiagarajan 	if (vif->sme_state == SME_CONNECTING) {
768bdcd8170SKalle Valo 		/* inform connect result to cfg80211 */
76914ee6f6bSVasanthakumar Thiagarajan 		vif->sme_state = SME_CONNECTED;
770240d2799SVasanthakumar Thiagarajan 		cfg80211_connect_result(vif->ndev, bssid,
771bdcd8170SKalle Valo 					assoc_req_ie, assoc_req_len,
772bdcd8170SKalle Valo 					assoc_resp_ie, assoc_resp_len,
773bdcd8170SKalle Valo 					WLAN_STATUS_SUCCESS, GFP_KERNEL);
7745e13fd35SVasanthakumar Thiagarajan 		cfg80211_put_bss(bss);
77514ee6f6bSVasanthakumar Thiagarajan 	} else if (vif->sme_state == SME_CONNECTED) {
776bdcd8170SKalle Valo 		/* inform roam event to cfg80211 */
7775e13fd35SVasanthakumar Thiagarajan 		cfg80211_roamed_bss(vif->ndev, bss, assoc_req_ie, assoc_req_len,
778bdcd8170SKalle Valo 				    assoc_resp_ie, assoc_resp_len, GFP_KERNEL);
779bdcd8170SKalle Valo 	}
780bdcd8170SKalle Valo }
781bdcd8170SKalle Valo 
782bdcd8170SKalle Valo static int ath6kl_cfg80211_disconnect(struct wiphy *wiphy,
783bdcd8170SKalle Valo 				      struct net_device *dev, u16 reason_code)
784bdcd8170SKalle Valo {
785d6d5c06cSKalle Valo 	struct ath6kl *ar = ath6kl_priv(dev);
7863450334fSVasanthakumar Thiagarajan 	struct ath6kl_vif *vif = netdev_priv(dev);
787bdcd8170SKalle Valo 
788bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: reason=%u\n", __func__,
789bdcd8170SKalle Valo 		   reason_code);
790bdcd8170SKalle Valo 
79110509f90SKalle Valo 	ath6kl_cfg80211_sscan_disable(vif);
79210509f90SKalle Valo 
793990bd915SVasanthakumar Thiagarajan 	if (!ath6kl_cfg80211_ready(vif))
794bdcd8170SKalle Valo 		return -EIO;
795bdcd8170SKalle Valo 
796bdcd8170SKalle Valo 	if (test_bit(DESTROY_IN_PROGRESS, &ar->flag)) {
797bdcd8170SKalle Valo 		ath6kl_err("busy, destroy in progress\n");
798bdcd8170SKalle Valo 		return -EBUSY;
799bdcd8170SKalle Valo 	}
800bdcd8170SKalle Valo 
801bdcd8170SKalle Valo 	if (down_interruptible(&ar->sem)) {
802bdcd8170SKalle Valo 		ath6kl_err("busy, couldn't get access\n");
803bdcd8170SKalle Valo 		return -ERESTARTSYS;
804bdcd8170SKalle Valo 	}
805bdcd8170SKalle Valo 
806cf5333d7SVasanthakumar Thiagarajan 	vif->reconnect_flag = 0;
807240d2799SVasanthakumar Thiagarajan 	ath6kl_disconnect(vif);
8083450334fSVasanthakumar Thiagarajan 	memset(vif->ssid, 0, sizeof(vif->ssid));
8093450334fSVasanthakumar Thiagarajan 	vif->ssid_len = 0;
810bdcd8170SKalle Valo 
811bdcd8170SKalle Valo 	if (!test_bit(SKIP_SCAN, &ar->flag))
8128c8b65e3SVasanthakumar Thiagarajan 		memset(vif->req_bssid, 0, sizeof(vif->req_bssid));
813bdcd8170SKalle Valo 
814bdcd8170SKalle Valo 	up(&ar->sem);
815bdcd8170SKalle Valo 
81614ee6f6bSVasanthakumar Thiagarajan 	vif->sme_state = SME_DISCONNECTED;
817170826ddSVasanthakumar Thiagarajan 
818bdcd8170SKalle Valo 	return 0;
819bdcd8170SKalle Valo }
820bdcd8170SKalle Valo 
821240d2799SVasanthakumar Thiagarajan void ath6kl_cfg80211_disconnect_event(struct ath6kl_vif *vif, u8 reason,
822bdcd8170SKalle Valo 				      u8 *bssid, u8 assoc_resp_len,
823bdcd8170SKalle Valo 				      u8 *assoc_info, u16 proto_reason)
824bdcd8170SKalle Valo {
825240d2799SVasanthakumar Thiagarajan 	struct ath6kl *ar = vif->ar;
82659c98449SVasanthakumar Thiagarajan 
82714ee6f6bSVasanthakumar Thiagarajan 	if (vif->scan_req) {
82814ee6f6bSVasanthakumar Thiagarajan 		cfg80211_scan_done(vif->scan_req, true);
82914ee6f6bSVasanthakumar Thiagarajan 		vif->scan_req = NULL;
830bdcd8170SKalle Valo 	}
831bdcd8170SKalle Valo 
832f5938f24SVasanthakumar Thiagarajan 	if (vif->nw_type & ADHOC_NETWORK) {
833551959d8SVasanthakumar Thiagarajan 		if (vif->wdev.iftype != NL80211_IFTYPE_ADHOC) {
834bdcd8170SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
835bdcd8170SKalle Valo 				   "%s: ath6k not in ibss mode\n", __func__);
836bdcd8170SKalle Valo 			return;
837bdcd8170SKalle Valo 		}
838bdcd8170SKalle Valo 		memset(bssid, 0, ETH_ALEN);
839240d2799SVasanthakumar Thiagarajan 		cfg80211_ibss_joined(vif->ndev, bssid, GFP_KERNEL);
840bdcd8170SKalle Valo 		return;
841bdcd8170SKalle Valo 	}
842bdcd8170SKalle Valo 
843f5938f24SVasanthakumar Thiagarajan 	if (vif->nw_type & INFRA_NETWORK) {
844551959d8SVasanthakumar Thiagarajan 		if (vif->wdev.iftype != NL80211_IFTYPE_STATION &&
845551959d8SVasanthakumar Thiagarajan 		    vif->wdev.iftype != NL80211_IFTYPE_P2P_CLIENT) {
846bdcd8170SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
847bdcd8170SKalle Valo 				   "%s: ath6k not in station mode\n", __func__);
848bdcd8170SKalle Valo 			return;
849bdcd8170SKalle Valo 		}
850bdcd8170SKalle Valo 	}
851bdcd8170SKalle Valo 
8521de547d6SVasanthakumar Thiagarajan 	/*
8531de547d6SVasanthakumar Thiagarajan 	 * Send a disconnect command to target when a disconnect event is
8541de547d6SVasanthakumar Thiagarajan 	 * received with reason code other than 3 (DISCONNECT_CMD - disconnect
8551de547d6SVasanthakumar Thiagarajan 	 * request from host) to make the firmware stop trying to connect even
8561de547d6SVasanthakumar Thiagarajan 	 * after giving disconnect event. There will be one more disconnect
8571de547d6SVasanthakumar Thiagarajan 	 * event for this disconnect command with reason code DISCONNECT_CMD
8581de547d6SVasanthakumar Thiagarajan 	 * which will be notified to cfg80211.
8591de547d6SVasanthakumar Thiagarajan 	 */
860bdcd8170SKalle Valo 
8611de547d6SVasanthakumar Thiagarajan 	if (reason != DISCONNECT_CMD) {
862334234b5SVasanthakumar Thiagarajan 		ath6kl_wmi_disconnect_cmd(ar->wmi, vif->fw_vif_idx);
863bdcd8170SKalle Valo 		return;
864bdcd8170SKalle Valo 	}
865bdcd8170SKalle Valo 
86659c98449SVasanthakumar Thiagarajan 	clear_bit(CONNECT_PEND, &vif->flags);
867bdcd8170SKalle Valo 
86814ee6f6bSVasanthakumar Thiagarajan 	if (vif->sme_state == SME_CONNECTING) {
869240d2799SVasanthakumar Thiagarajan 		cfg80211_connect_result(vif->ndev,
870bdcd8170SKalle Valo 					bssid, NULL, 0,
871bdcd8170SKalle Valo 					NULL, 0,
872bdcd8170SKalle Valo 					WLAN_STATUS_UNSPECIFIED_FAILURE,
873bdcd8170SKalle Valo 					GFP_KERNEL);
87414ee6f6bSVasanthakumar Thiagarajan 	} else if (vif->sme_state == SME_CONNECTED) {
875240d2799SVasanthakumar Thiagarajan 		cfg80211_disconnected(vif->ndev, reason,
876bdcd8170SKalle Valo 				      NULL, 0, GFP_KERNEL);
877bdcd8170SKalle Valo 	}
878bdcd8170SKalle Valo 
87914ee6f6bSVasanthakumar Thiagarajan 	vif->sme_state = SME_DISCONNECTED;
880bdcd8170SKalle Valo }
881bdcd8170SKalle Valo 
882bdcd8170SKalle Valo static int ath6kl_cfg80211_scan(struct wiphy *wiphy, struct net_device *ndev,
883bdcd8170SKalle Valo 				struct cfg80211_scan_request *request)
884bdcd8170SKalle Valo {
885d6d5c06cSKalle Valo 	struct ath6kl *ar = ath6kl_priv(ndev);
88659c98449SVasanthakumar Thiagarajan 	struct ath6kl_vif *vif = netdev_priv(ndev);
8871276c9efSEdward Lu 	s8 n_channels = 0;
8881276c9efSEdward Lu 	u16 *channels = NULL;
889bdcd8170SKalle Valo 	int ret = 0;
890f1f92179SVasanthakumar Thiagarajan 	u32 force_fg_scan = 0;
891bdcd8170SKalle Valo 
892990bd915SVasanthakumar Thiagarajan 	if (!ath6kl_cfg80211_ready(vif))
893bdcd8170SKalle Valo 		return -EIO;
894bdcd8170SKalle Valo 
89510509f90SKalle Valo 	ath6kl_cfg80211_sscan_disable(vif);
89610509f90SKalle Valo 
897bdcd8170SKalle Valo 	if (!ar->usr_bss_filter) {
89859c98449SVasanthakumar Thiagarajan 		clear_bit(CLEAR_BSSFILTER_ON_BEACON, &vif->flags);
8991b1e6ee3SJouni Malinen 		ret = ath6kl_wmi_bssfilter_cmd(
900240d2799SVasanthakumar Thiagarajan 			ar->wmi, vif->fw_vif_idx,
90159c98449SVasanthakumar Thiagarajan 			(test_bit(CONNECTED, &vif->flags) ?
9021b1e6ee3SJouni Malinen 			 ALL_BUT_BSS_FILTER : ALL_BSS_FILTER), 0);
9031b1e6ee3SJouni Malinen 		if (ret) {
904bdcd8170SKalle Valo 			ath6kl_err("couldn't set bss filtering\n");
9051b1e6ee3SJouni Malinen 			return ret;
906bdcd8170SKalle Valo 		}
907bdcd8170SKalle Valo 	}
908bdcd8170SKalle Valo 
909bdcd8170SKalle Valo 	if (request->n_ssids && request->ssids[0].ssid_len) {
910bdcd8170SKalle Valo 		u8 i;
911bdcd8170SKalle Valo 
912bdcd8170SKalle Valo 		if (request->n_ssids > (MAX_PROBED_SSID_INDEX - 1))
913bdcd8170SKalle Valo 			request->n_ssids = MAX_PROBED_SSID_INDEX - 1;
914bdcd8170SKalle Valo 
915bdcd8170SKalle Valo 		for (i = 0; i < request->n_ssids; i++)
916334234b5SVasanthakumar Thiagarajan 			ath6kl_wmi_probedssid_cmd(ar->wmi, vif->fw_vif_idx,
917334234b5SVasanthakumar Thiagarajan 						  i + 1, SPECIFIC_SSID_FLAG,
918bdcd8170SKalle Valo 						  request->ssids[i].ssid_len,
919bdcd8170SKalle Valo 						  request->ssids[i].ssid);
920bdcd8170SKalle Valo 	}
921bdcd8170SKalle Valo 
922080eec4fSAarthi Thiruvengadam 	/* this also clears IE in fw if it's not set */
923334234b5SVasanthakumar Thiagarajan 	ret = ath6kl_wmi_set_appie_cmd(ar->wmi, vif->fw_vif_idx,
924334234b5SVasanthakumar Thiagarajan 				       WMI_FRAME_PROBE_REQ,
925b84da8c7SJouni Malinen 				       request->ie, request->ie_len);
926b84da8c7SJouni Malinen 	if (ret) {
927b84da8c7SJouni Malinen 		ath6kl_err("failed to set Probe Request appie for "
928b84da8c7SJouni Malinen 			   "scan");
929b84da8c7SJouni Malinen 		return ret;
930b84da8c7SJouni Malinen 	}
931b84da8c7SJouni Malinen 
93211869befSJouni Malinen 	/*
93311869befSJouni Malinen 	 * Scan only the requested channels if the request specifies a set of
93411869befSJouni Malinen 	 * channels. If the list is longer than the target supports, do not
93511869befSJouni Malinen 	 * configure the list and instead, scan all available channels.
93611869befSJouni Malinen 	 */
93711869befSJouni Malinen 	if (request->n_channels > 0 &&
93811869befSJouni Malinen 	    request->n_channels <= WMI_MAX_CHANNELS) {
9391276c9efSEdward Lu 		u8 i;
9401276c9efSEdward Lu 
94111869befSJouni Malinen 		n_channels = request->n_channels;
9421276c9efSEdward Lu 
9431276c9efSEdward Lu 		channels = kzalloc(n_channels * sizeof(u16), GFP_KERNEL);
9441276c9efSEdward Lu 		if (channels == NULL) {
9451276c9efSEdward Lu 			ath6kl_warn("failed to set scan channels, "
9461276c9efSEdward Lu 				    "scan all channels");
9471276c9efSEdward Lu 			n_channels = 0;
9481276c9efSEdward Lu 		}
9491276c9efSEdward Lu 
9501276c9efSEdward Lu 		for (i = 0; i < n_channels; i++)
9511276c9efSEdward Lu 			channels[i] = request->channels[i]->center_freq;
9521276c9efSEdward Lu 	}
9531276c9efSEdward Lu 
95459c98449SVasanthakumar Thiagarajan 	if (test_bit(CONNECTED, &vif->flags))
955f1f92179SVasanthakumar Thiagarajan 		force_fg_scan = 1;
956f1f92179SVasanthakumar Thiagarajan 
9575b35dff0SRaja Mani 	vif->scan_req = request;
9585b35dff0SRaja Mani 
9593ca9d1fcSAarthi Thiruvengadam 	if (test_bit(ATH6KL_FW_CAPABILITY_STA_P2PDEV_DUPLEX,
9603ca9d1fcSAarthi Thiruvengadam 		     ar->fw_capabilities)) {
9613ca9d1fcSAarthi Thiruvengadam 		/*
9623ca9d1fcSAarthi Thiruvengadam 		 * If capable of doing P2P mgmt operations using
9633ca9d1fcSAarthi Thiruvengadam 		 * station interface, send additional information like
9643ca9d1fcSAarthi Thiruvengadam 		 * supported rates to advertise and xmit rates for
9653ca9d1fcSAarthi Thiruvengadam 		 * probe requests
9663ca9d1fcSAarthi Thiruvengadam 		 */
9673ca9d1fcSAarthi Thiruvengadam 		ret = ath6kl_wmi_beginscan_cmd(ar->wmi, vif->fw_vif_idx,
9683ca9d1fcSAarthi Thiruvengadam 						WMI_LONG_SCAN, force_fg_scan,
96913423c31SVasanthakumar Thiagarajan 						false, 0,
97013423c31SVasanthakumar Thiagarajan 						ATH6KL_FG_SCAN_INTERVAL,
97113423c31SVasanthakumar Thiagarajan 						n_channels, channels,
97213423c31SVasanthakumar Thiagarajan 						request->no_cck,
9733ca9d1fcSAarthi Thiruvengadam 						request->rates);
9743ca9d1fcSAarthi Thiruvengadam 	} else {
9753ca9d1fcSAarthi Thiruvengadam 		ret = ath6kl_wmi_startscan_cmd(ar->wmi, vif->fw_vif_idx,
9763ca9d1fcSAarthi Thiruvengadam 						WMI_LONG_SCAN, force_fg_scan,
97713423c31SVasanthakumar Thiagarajan 						false, 0,
97813423c31SVasanthakumar Thiagarajan 						ATH6KL_FG_SCAN_INTERVAL,
97913423c31SVasanthakumar Thiagarajan 						n_channels, channels);
9803ca9d1fcSAarthi Thiruvengadam 	}
9815b35dff0SRaja Mani 	if (ret) {
982bdcd8170SKalle Valo 		ath6kl_err("wmi_startscan_cmd failed\n");
9835b35dff0SRaja Mani 		vif->scan_req = NULL;
9845b35dff0SRaja Mani 	}
985bdcd8170SKalle Valo 
9861276c9efSEdward Lu 	kfree(channels);
9871276c9efSEdward Lu 
988bdcd8170SKalle Valo 	return ret;
989bdcd8170SKalle Valo }
990bdcd8170SKalle Valo 
9911c17d313SKalle Valo void ath6kl_cfg80211_scan_complete_event(struct ath6kl_vif *vif, bool aborted)
992bdcd8170SKalle Valo {
993240d2799SVasanthakumar Thiagarajan 	struct ath6kl *ar = vif->ar;
9946fd1eaceSKalle Valo 	int i;
995bdcd8170SKalle Valo 
9961c17d313SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: status%s\n", __func__,
9971c17d313SKalle Valo 		   aborted ? " aborted" : "");
998bdcd8170SKalle Valo 
99914ee6f6bSVasanthakumar Thiagarajan 	if (!vif->scan_req)
10006fd1eaceSKalle Valo 		return;
10016fd1eaceSKalle Valo 
10021c17d313SKalle Valo 	if (aborted)
10036fd1eaceSKalle Valo 		goto out;
1004bdcd8170SKalle Valo 
100514ee6f6bSVasanthakumar Thiagarajan 	if (vif->scan_req->n_ssids && vif->scan_req->ssids[0].ssid_len) {
100614ee6f6bSVasanthakumar Thiagarajan 		for (i = 0; i < vif->scan_req->n_ssids; i++) {
1007334234b5SVasanthakumar Thiagarajan 			ath6kl_wmi_probedssid_cmd(ar->wmi, vif->fw_vif_idx,
1008334234b5SVasanthakumar Thiagarajan 						  i + 1, DISABLE_SSID_FLAG,
1009bdcd8170SKalle Valo 						  0, NULL);
1010bdcd8170SKalle Valo 		}
1011bdcd8170SKalle Valo 	}
10126fd1eaceSKalle Valo 
10136fd1eaceSKalle Valo out:
1014cb93821aSKalle Valo 	cfg80211_scan_done(vif->scan_req, aborted);
101514ee6f6bSVasanthakumar Thiagarajan 	vif->scan_req = NULL;
1016bdcd8170SKalle Valo }
1017bdcd8170SKalle Valo 
1018bdcd8170SKalle Valo static int ath6kl_cfg80211_add_key(struct wiphy *wiphy, struct net_device *ndev,
1019bdcd8170SKalle Valo 				   u8 key_index, bool pairwise,
1020bdcd8170SKalle Valo 				   const u8 *mac_addr,
1021bdcd8170SKalle Valo 				   struct key_params *params)
1022bdcd8170SKalle Valo {
1023d6d5c06cSKalle Valo 	struct ath6kl *ar = ath6kl_priv(ndev);
102459c98449SVasanthakumar Thiagarajan 	struct ath6kl_vif *vif = netdev_priv(ndev);
1025bdcd8170SKalle Valo 	struct ath6kl_key *key = NULL;
10264a8ce2fdSSujith Manoharan 	int seq_len;
1027bdcd8170SKalle Valo 	u8 key_usage;
1028bdcd8170SKalle Valo 	u8 key_type;
1029bdcd8170SKalle Valo 
1030990bd915SVasanthakumar Thiagarajan 	if (!ath6kl_cfg80211_ready(vif))
1031bdcd8170SKalle Valo 		return -EIO;
1032bdcd8170SKalle Valo 
1033837cb97eSJouni Malinen 	if (params->cipher == CCKM_KRK_CIPHER_SUITE) {
1034837cb97eSJouni Malinen 		if (params->key_len != WMI_KRK_LEN)
1035837cb97eSJouni Malinen 			return -EINVAL;
1036240d2799SVasanthakumar Thiagarajan 		return ath6kl_wmi_add_krk_cmd(ar->wmi, vif->fw_vif_idx,
1037240d2799SVasanthakumar Thiagarajan 					      params->key);
1038837cb97eSJouni Malinen 	}
1039837cb97eSJouni Malinen 
1040792ecb33SVivek Natarajan 	if (key_index > WMI_MAX_KEY_INDEX) {
1041bdcd8170SKalle Valo 		ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
1042bdcd8170SKalle Valo 			   "%s: key index %d out of bounds\n", __func__,
1043bdcd8170SKalle Valo 			   key_index);
1044bdcd8170SKalle Valo 		return -ENOENT;
1045bdcd8170SKalle Valo 	}
1046bdcd8170SKalle Valo 
10476f2a73f9SVasanthakumar Thiagarajan 	key = &vif->keys[key_index];
1048bdcd8170SKalle Valo 	memset(key, 0, sizeof(struct ath6kl_key));
1049bdcd8170SKalle Valo 
1050bdcd8170SKalle Valo 	if (pairwise)
1051bdcd8170SKalle Valo 		key_usage = PAIRWISE_USAGE;
1052bdcd8170SKalle Valo 	else
1053bdcd8170SKalle Valo 		key_usage = GROUP_USAGE;
1054bdcd8170SKalle Valo 
10554a8ce2fdSSujith Manoharan 	seq_len = params->seq_len;
10565e07021eSDai Shuibing 	if (params->cipher == WLAN_CIPHER_SUITE_SMS4 &&
10575e07021eSDai Shuibing 	    seq_len > ATH6KL_KEY_SEQ_LEN) {
10585e07021eSDai Shuibing 		/* Only first half of the WPI PN is configured */
10595e07021eSDai Shuibing 		seq_len = ATH6KL_KEY_SEQ_LEN;
10605e07021eSDai Shuibing 	}
1061bdcd8170SKalle Valo 	if (params->key_len > WLAN_MAX_KEY_LEN ||
10625e07021eSDai Shuibing 	    seq_len > sizeof(key->seq))
1063bdcd8170SKalle Valo 		return -EINVAL;
1064bdcd8170SKalle Valo 
1065bdcd8170SKalle Valo 	key->key_len = params->key_len;
1066bdcd8170SKalle Valo 	memcpy(key->key, params->key, key->key_len);
10675e07021eSDai Shuibing 	key->seq_len = seq_len;
1068bdcd8170SKalle Valo 	memcpy(key->seq, params->seq, key->seq_len);
1069bdcd8170SKalle Valo 	key->cipher = params->cipher;
1070bdcd8170SKalle Valo 
1071bdcd8170SKalle Valo 	switch (key->cipher) {
1072bdcd8170SKalle Valo 	case WLAN_CIPHER_SUITE_WEP40:
1073bdcd8170SKalle Valo 	case WLAN_CIPHER_SUITE_WEP104:
1074bdcd8170SKalle Valo 		key_type = WEP_CRYPT;
1075bdcd8170SKalle Valo 		break;
1076bdcd8170SKalle Valo 
1077bdcd8170SKalle Valo 	case WLAN_CIPHER_SUITE_TKIP:
1078bdcd8170SKalle Valo 		key_type = TKIP_CRYPT;
1079bdcd8170SKalle Valo 		break;
1080bdcd8170SKalle Valo 
1081bdcd8170SKalle Valo 	case WLAN_CIPHER_SUITE_CCMP:
1082bdcd8170SKalle Valo 		key_type = AES_CRYPT;
1083bdcd8170SKalle Valo 		break;
10845e07021eSDai Shuibing 	case WLAN_CIPHER_SUITE_SMS4:
10855e07021eSDai Shuibing 		key_type = WAPI_CRYPT;
10865e07021eSDai Shuibing 		break;
1087bdcd8170SKalle Valo 
1088bdcd8170SKalle Valo 	default:
1089bdcd8170SKalle Valo 		return -ENOTSUPP;
1090bdcd8170SKalle Valo 	}
1091bdcd8170SKalle Valo 
1092ddc3d77cSKalle Valo 	if (((vif->auth_mode == WPA_PSK_AUTH) ||
1093ddc3d77cSKalle Valo 	     (vif->auth_mode == WPA2_PSK_AUTH)) &&
1094ddc3d77cSKalle Valo 	    (key_usage & GROUP_USAGE))
1095de3ad713SVasanthakumar Thiagarajan 		del_timer(&vif->disconnect_timer);
1096bdcd8170SKalle Valo 
1097bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
1098bdcd8170SKalle Valo 		   "%s: index %d, key_len %d, key_type 0x%x, key_usage 0x%x, seq_len %d\n",
1099bdcd8170SKalle Valo 		   __func__, key_index, key->key_len, key_type,
1100bdcd8170SKalle Valo 		   key_usage, key->seq_len);
1101bdcd8170SKalle Valo 
1102f5938f24SVasanthakumar Thiagarajan 	if (vif->nw_type == AP_NETWORK && !pairwise &&
11034703290aSJouni Malinen 	    (key_type == TKIP_CRYPT || key_type == AES_CRYPT ||
1104cc4d623dSVasanthakumar Thiagarajan 	     key_type == WAPI_CRYPT)) {
11059a5b1318SJouni Malinen 		ar->ap_mode_bkey.valid = true;
11069a5b1318SJouni Malinen 		ar->ap_mode_bkey.key_index = key_index;
11079a5b1318SJouni Malinen 		ar->ap_mode_bkey.key_type = key_type;
11089a5b1318SJouni Malinen 		ar->ap_mode_bkey.key_len = key->key_len;
11099a5b1318SJouni Malinen 		memcpy(ar->ap_mode_bkey.key, key->key, key->key_len);
111059c98449SVasanthakumar Thiagarajan 		if (!test_bit(CONNECTED, &vif->flags)) {
11119a5b1318SJouni Malinen 			ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "Delay initial group "
11129a5b1318SJouni Malinen 				   "key configuration until AP mode has been "
11139a5b1318SJouni Malinen 				   "started\n");
11149a5b1318SJouni Malinen 			/*
11159a5b1318SJouni Malinen 			 * The key will be set in ath6kl_connect_ap_mode() once
11169a5b1318SJouni Malinen 			 * the connected event is received from the target.
11179a5b1318SJouni Malinen 			 */
11189a5b1318SJouni Malinen 			return 0;
11199a5b1318SJouni Malinen 		}
11209a5b1318SJouni Malinen 	}
11219a5b1318SJouni Malinen 
1122f5938f24SVasanthakumar Thiagarajan 	if (vif->next_mode == AP_NETWORK && key_type == WEP_CRYPT &&
112359c98449SVasanthakumar Thiagarajan 	    !test_bit(CONNECTED, &vif->flags)) {
1124151411e8SJouni Malinen 		/*
1125151411e8SJouni Malinen 		 * Store the key locally so that it can be re-configured after
1126151411e8SJouni Malinen 		 * the AP mode has properly started
1127151411e8SJouni Malinen 		 * (ath6kl_install_statioc_wep_keys).
1128151411e8SJouni Malinen 		 */
1129151411e8SJouni Malinen 		ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "Delay WEP key configuration "
1130151411e8SJouni Malinen 			   "until AP mode has been started\n");
11316f2a73f9SVasanthakumar Thiagarajan 		vif->wep_key_list[key_index].key_len = key->key_len;
11326f2a73f9SVasanthakumar Thiagarajan 		memcpy(vif->wep_key_list[key_index].key, key->key,
11336f2a73f9SVasanthakumar Thiagarajan 		       key->key_len);
1134151411e8SJouni Malinen 		return 0;
1135151411e8SJouni Malinen 	}
1136151411e8SJouni Malinen 
11377cefa44fSVasanthakumar Thiagarajan 	return ath6kl_wmi_addkey_cmd(ar->wmi, vif->fw_vif_idx, key_index,
1138bdcd8170SKalle Valo 				     key_type, key_usage, key->key_len,
1139f4bb9a6fSJouni Malinen 				     key->seq, key->seq_len, key->key,
1140f4bb9a6fSJouni Malinen 				     KEY_OP_INIT_VAL,
1141bdcd8170SKalle Valo 				     (u8 *) mac_addr, SYNC_BOTH_WMIFLAG);
1142bdcd8170SKalle Valo }
1143bdcd8170SKalle Valo 
1144bdcd8170SKalle Valo static int ath6kl_cfg80211_del_key(struct wiphy *wiphy, struct net_device *ndev,
1145bdcd8170SKalle Valo 				   u8 key_index, bool pairwise,
1146bdcd8170SKalle Valo 				   const u8 *mac_addr)
1147bdcd8170SKalle Valo {
1148d6d5c06cSKalle Valo 	struct ath6kl *ar = ath6kl_priv(ndev);
11496f2a73f9SVasanthakumar Thiagarajan 	struct ath6kl_vif *vif = netdev_priv(ndev);
1150bdcd8170SKalle Valo 
1151bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: index %d\n", __func__, key_index);
1152bdcd8170SKalle Valo 
1153990bd915SVasanthakumar Thiagarajan 	if (!ath6kl_cfg80211_ready(vif))
1154bdcd8170SKalle Valo 		return -EIO;
1155bdcd8170SKalle Valo 
1156792ecb33SVivek Natarajan 	if (key_index > WMI_MAX_KEY_INDEX) {
1157bdcd8170SKalle Valo 		ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
1158bdcd8170SKalle Valo 			   "%s: key index %d out of bounds\n", __func__,
1159bdcd8170SKalle Valo 			   key_index);
1160bdcd8170SKalle Valo 		return -ENOENT;
1161bdcd8170SKalle Valo 	}
1162bdcd8170SKalle Valo 
11636f2a73f9SVasanthakumar Thiagarajan 	if (!vif->keys[key_index].key_len) {
1164bdcd8170SKalle Valo 		ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
1165bdcd8170SKalle Valo 			   "%s: index %d is empty\n", __func__, key_index);
1166bdcd8170SKalle Valo 		return 0;
1167bdcd8170SKalle Valo 	}
1168bdcd8170SKalle Valo 
11696f2a73f9SVasanthakumar Thiagarajan 	vif->keys[key_index].key_len = 0;
1170bdcd8170SKalle Valo 
1171334234b5SVasanthakumar Thiagarajan 	return ath6kl_wmi_deletekey_cmd(ar->wmi, vif->fw_vif_idx, key_index);
1172bdcd8170SKalle Valo }
1173bdcd8170SKalle Valo 
1174bdcd8170SKalle Valo static int ath6kl_cfg80211_get_key(struct wiphy *wiphy, struct net_device *ndev,
1175bdcd8170SKalle Valo 				   u8 key_index, bool pairwise,
1176bdcd8170SKalle Valo 				   const u8 *mac_addr, void *cookie,
1177bdcd8170SKalle Valo 				   void (*callback) (void *cookie,
1178bdcd8170SKalle Valo 						     struct key_params *))
1179bdcd8170SKalle Valo {
11806f2a73f9SVasanthakumar Thiagarajan 	struct ath6kl_vif *vif = netdev_priv(ndev);
1181bdcd8170SKalle Valo 	struct ath6kl_key *key = NULL;
1182bdcd8170SKalle Valo 	struct key_params params;
1183bdcd8170SKalle Valo 
1184bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: index %d\n", __func__, key_index);
1185bdcd8170SKalle Valo 
1186990bd915SVasanthakumar Thiagarajan 	if (!ath6kl_cfg80211_ready(vif))
1187bdcd8170SKalle Valo 		return -EIO;
1188bdcd8170SKalle Valo 
1189792ecb33SVivek Natarajan 	if (key_index > WMI_MAX_KEY_INDEX) {
1190bdcd8170SKalle Valo 		ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
1191bdcd8170SKalle Valo 			   "%s: key index %d out of bounds\n", __func__,
1192bdcd8170SKalle Valo 			   key_index);
1193bdcd8170SKalle Valo 		return -ENOENT;
1194bdcd8170SKalle Valo 	}
1195bdcd8170SKalle Valo 
11966f2a73f9SVasanthakumar Thiagarajan 	key = &vif->keys[key_index];
1197bdcd8170SKalle Valo 	memset(&params, 0, sizeof(params));
1198bdcd8170SKalle Valo 	params.cipher = key->cipher;
1199bdcd8170SKalle Valo 	params.key_len = key->key_len;
1200bdcd8170SKalle Valo 	params.seq_len = key->seq_len;
1201bdcd8170SKalle Valo 	params.seq = key->seq;
1202bdcd8170SKalle Valo 	params.key = key->key;
1203bdcd8170SKalle Valo 
1204bdcd8170SKalle Valo 	callback(cookie, &params);
1205bdcd8170SKalle Valo 
1206bdcd8170SKalle Valo 	return key->key_len ? 0 : -ENOENT;
1207bdcd8170SKalle Valo }
1208bdcd8170SKalle Valo 
1209bdcd8170SKalle Valo static int ath6kl_cfg80211_set_default_key(struct wiphy *wiphy,
1210bdcd8170SKalle Valo 					   struct net_device *ndev,
1211bdcd8170SKalle Valo 					   u8 key_index, bool unicast,
1212bdcd8170SKalle Valo 					   bool multicast)
1213bdcd8170SKalle Valo {
1214d6d5c06cSKalle Valo 	struct ath6kl *ar = ath6kl_priv(ndev);
121559c98449SVasanthakumar Thiagarajan 	struct ath6kl_vif *vif = netdev_priv(ndev);
1216bdcd8170SKalle Valo 	struct ath6kl_key *key = NULL;
1217bdcd8170SKalle Valo 	u8 key_usage;
1218229ed6b5SEdward Lu 	enum crypto_type key_type = NONE_CRYPT;
1219bdcd8170SKalle Valo 
1220bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: index %d\n", __func__, key_index);
1221bdcd8170SKalle Valo 
1222990bd915SVasanthakumar Thiagarajan 	if (!ath6kl_cfg80211_ready(vif))
1223bdcd8170SKalle Valo 		return -EIO;
1224bdcd8170SKalle Valo 
1225792ecb33SVivek Natarajan 	if (key_index > WMI_MAX_KEY_INDEX) {
1226bdcd8170SKalle Valo 		ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
1227bdcd8170SKalle Valo 			   "%s: key index %d out of bounds\n",
1228bdcd8170SKalle Valo 			   __func__, key_index);
1229bdcd8170SKalle Valo 		return -ENOENT;
1230bdcd8170SKalle Valo 	}
1231bdcd8170SKalle Valo 
12326f2a73f9SVasanthakumar Thiagarajan 	if (!vif->keys[key_index].key_len) {
1233bdcd8170SKalle Valo 		ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: invalid key index %d\n",
1234bdcd8170SKalle Valo 			   __func__, key_index);
1235bdcd8170SKalle Valo 		return -EINVAL;
1236bdcd8170SKalle Valo 	}
1237bdcd8170SKalle Valo 
12383450334fSVasanthakumar Thiagarajan 	vif->def_txkey_index = key_index;
12396f2a73f9SVasanthakumar Thiagarajan 	key = &vif->keys[vif->def_txkey_index];
1240bdcd8170SKalle Valo 	key_usage = GROUP_USAGE;
12413450334fSVasanthakumar Thiagarajan 	if (vif->prwise_crypto == WEP_CRYPT)
1242bdcd8170SKalle Valo 		key_usage |= TX_USAGE;
1243229ed6b5SEdward Lu 	if (unicast)
12443450334fSVasanthakumar Thiagarajan 		key_type = vif->prwise_crypto;
1245229ed6b5SEdward Lu 	if (multicast)
12463450334fSVasanthakumar Thiagarajan 		key_type = vif->grp_crypto;
1247bdcd8170SKalle Valo 
1248f5938f24SVasanthakumar Thiagarajan 	if (vif->next_mode == AP_NETWORK && !test_bit(CONNECTED, &vif->flags))
12499a5b1318SJouni Malinen 		return 0; /* Delay until AP mode has been started */
12509a5b1318SJouni Malinen 
1251f3e61eceSJouni Malinen 	return ath6kl_wmi_addkey_cmd(ar->wmi, vif->fw_vif_idx,
1252334234b5SVasanthakumar Thiagarajan 				     vif->def_txkey_index,
1253229ed6b5SEdward Lu 				     key_type, key_usage,
1254f4bb9a6fSJouni Malinen 				     key->key_len, key->seq, key->seq_len,
1255f4bb9a6fSJouni Malinen 				     key->key,
1256bdcd8170SKalle Valo 				     KEY_OP_INIT_VAL, NULL,
1257bdcd8170SKalle Valo 				     SYNC_BOTH_WMIFLAG);
1258bdcd8170SKalle Valo }
1259bdcd8170SKalle Valo 
1260240d2799SVasanthakumar Thiagarajan void ath6kl_cfg80211_tkip_micerr_event(struct ath6kl_vif *vif, u8 keyid,
1261bdcd8170SKalle Valo 				       bool ismcast)
1262bdcd8170SKalle Valo {
1263bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
1264bdcd8170SKalle Valo 		   "%s: keyid %d, ismcast %d\n", __func__, keyid, ismcast);
1265bdcd8170SKalle Valo 
1266240d2799SVasanthakumar Thiagarajan 	cfg80211_michael_mic_failure(vif->ndev, vif->bssid,
1267bdcd8170SKalle Valo 				     (ismcast ? NL80211_KEYTYPE_GROUP :
1268bdcd8170SKalle Valo 				      NL80211_KEYTYPE_PAIRWISE), keyid, NULL,
1269bdcd8170SKalle Valo 				     GFP_KERNEL);
1270bdcd8170SKalle Valo }
1271bdcd8170SKalle Valo 
1272bdcd8170SKalle Valo static int ath6kl_cfg80211_set_wiphy_params(struct wiphy *wiphy, u32 changed)
1273bdcd8170SKalle Valo {
1274bdcd8170SKalle Valo 	struct ath6kl *ar = (struct ath6kl *)wiphy_priv(wiphy);
1275990bd915SVasanthakumar Thiagarajan 	struct ath6kl_vif *vif;
1276bdcd8170SKalle Valo 	int ret;
1277bdcd8170SKalle Valo 
1278bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: changed 0x%x\n", __func__,
1279bdcd8170SKalle Valo 		   changed);
1280bdcd8170SKalle Valo 
1281990bd915SVasanthakumar Thiagarajan 	vif = ath6kl_vif_first(ar);
1282990bd915SVasanthakumar Thiagarajan 	if (!vif)
1283990bd915SVasanthakumar Thiagarajan 		return -EIO;
1284990bd915SVasanthakumar Thiagarajan 
1285990bd915SVasanthakumar Thiagarajan 	if (!ath6kl_cfg80211_ready(vif))
1286bdcd8170SKalle Valo 		return -EIO;
1287bdcd8170SKalle Valo 
1288bdcd8170SKalle Valo 	if (changed & WIPHY_PARAM_RTS_THRESHOLD) {
1289bdcd8170SKalle Valo 		ret = ath6kl_wmi_set_rts_cmd(ar->wmi, wiphy->rts_threshold);
1290bdcd8170SKalle Valo 		if (ret != 0) {
1291bdcd8170SKalle Valo 			ath6kl_err("ath6kl_wmi_set_rts_cmd failed\n");
1292bdcd8170SKalle Valo 			return -EIO;
1293bdcd8170SKalle Valo 		}
1294bdcd8170SKalle Valo 	}
1295bdcd8170SKalle Valo 
1296bdcd8170SKalle Valo 	return 0;
1297bdcd8170SKalle Valo }
1298bdcd8170SKalle Valo 
1299bdcd8170SKalle Valo /*
1300bdcd8170SKalle Valo  * The type nl80211_tx_power_setting replaces the following
1301bdcd8170SKalle Valo  * data type from 2.6.36 onwards
1302bdcd8170SKalle Valo */
1303bdcd8170SKalle Valo static int ath6kl_cfg80211_set_txpower(struct wiphy *wiphy,
1304bdcd8170SKalle Valo 				       enum nl80211_tx_power_setting type,
1305b992a285SLuis R. Rodriguez 				       int mbm)
1306bdcd8170SKalle Valo {
1307bdcd8170SKalle Valo 	struct ath6kl *ar = (struct ath6kl *)wiphy_priv(wiphy);
1308990bd915SVasanthakumar Thiagarajan 	struct ath6kl_vif *vif;
1309b992a285SLuis R. Rodriguez 	int dbm = MBM_TO_DBM(mbm);
1310bdcd8170SKalle Valo 
1311bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: type 0x%x, dbm %d\n", __func__,
1312bdcd8170SKalle Valo 		   type, dbm);
1313bdcd8170SKalle Valo 
1314990bd915SVasanthakumar Thiagarajan 	vif = ath6kl_vif_first(ar);
1315990bd915SVasanthakumar Thiagarajan 	if (!vif)
1316990bd915SVasanthakumar Thiagarajan 		return -EIO;
1317990bd915SVasanthakumar Thiagarajan 
1318990bd915SVasanthakumar Thiagarajan 	if (!ath6kl_cfg80211_ready(vif))
1319bdcd8170SKalle Valo 		return -EIO;
1320bdcd8170SKalle Valo 
1321bdcd8170SKalle Valo 	switch (type) {
1322bdcd8170SKalle Valo 	case NL80211_TX_POWER_AUTOMATIC:
1323bdcd8170SKalle Valo 		return 0;
1324bdcd8170SKalle Valo 	case NL80211_TX_POWER_LIMITED:
1325d0d670abSKalle Valo 		ar->tx_pwr = dbm;
1326bdcd8170SKalle Valo 		break;
1327bdcd8170SKalle Valo 	default:
1328bdcd8170SKalle Valo 		ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: type 0x%x not supported\n",
1329bdcd8170SKalle Valo 			   __func__, type);
1330bdcd8170SKalle Valo 		return -EOPNOTSUPP;
1331bdcd8170SKalle Valo 	}
1332bdcd8170SKalle Valo 
1333d0d670abSKalle Valo 	ath6kl_wmi_set_tx_pwr_cmd(ar->wmi, vif->fw_vif_idx, dbm);
1334bdcd8170SKalle Valo 
1335bdcd8170SKalle Valo 	return 0;
1336bdcd8170SKalle Valo }
1337bdcd8170SKalle Valo 
1338bdcd8170SKalle Valo static int ath6kl_cfg80211_get_txpower(struct wiphy *wiphy, int *dbm)
1339bdcd8170SKalle Valo {
1340bdcd8170SKalle Valo 	struct ath6kl *ar = (struct ath6kl *)wiphy_priv(wiphy);
1341990bd915SVasanthakumar Thiagarajan 	struct ath6kl_vif *vif;
1342bdcd8170SKalle Valo 
1343990bd915SVasanthakumar Thiagarajan 	vif = ath6kl_vif_first(ar);
1344990bd915SVasanthakumar Thiagarajan 	if (!vif)
1345990bd915SVasanthakumar Thiagarajan 		return -EIO;
1346990bd915SVasanthakumar Thiagarajan 
1347990bd915SVasanthakumar Thiagarajan 	if (!ath6kl_cfg80211_ready(vif))
1348bdcd8170SKalle Valo 		return -EIO;
1349bdcd8170SKalle Valo 
135059c98449SVasanthakumar Thiagarajan 	if (test_bit(CONNECTED, &vif->flags)) {
1351bdcd8170SKalle Valo 		ar->tx_pwr = 0;
1352bdcd8170SKalle Valo 
1353990bd915SVasanthakumar Thiagarajan 		if (ath6kl_wmi_get_tx_pwr_cmd(ar->wmi, vif->fw_vif_idx) != 0) {
1354bdcd8170SKalle Valo 			ath6kl_err("ath6kl_wmi_get_tx_pwr_cmd failed\n");
1355bdcd8170SKalle Valo 			return -EIO;
1356bdcd8170SKalle Valo 		}
1357bdcd8170SKalle Valo 
1358bdcd8170SKalle Valo 		wait_event_interruptible_timeout(ar->event_wq, ar->tx_pwr != 0,
1359bdcd8170SKalle Valo 						 5 * HZ);
1360bdcd8170SKalle Valo 
1361bdcd8170SKalle Valo 		if (signal_pending(current)) {
1362bdcd8170SKalle Valo 			ath6kl_err("target did not respond\n");
1363bdcd8170SKalle Valo 			return -EINTR;
1364bdcd8170SKalle Valo 		}
1365bdcd8170SKalle Valo 	}
1366bdcd8170SKalle Valo 
1367bdcd8170SKalle Valo 	*dbm = ar->tx_pwr;
1368bdcd8170SKalle Valo 	return 0;
1369bdcd8170SKalle Valo }
1370bdcd8170SKalle Valo 
1371bdcd8170SKalle Valo static int ath6kl_cfg80211_set_power_mgmt(struct wiphy *wiphy,
1372bdcd8170SKalle Valo 					  struct net_device *dev,
1373bdcd8170SKalle Valo 					  bool pmgmt, int timeout)
1374bdcd8170SKalle Valo {
1375bdcd8170SKalle Valo 	struct ath6kl *ar = ath6kl_priv(dev);
1376bdcd8170SKalle Valo 	struct wmi_power_mode_cmd mode;
1377334234b5SVasanthakumar Thiagarajan 	struct ath6kl_vif *vif = netdev_priv(dev);
1378bdcd8170SKalle Valo 
1379bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: pmgmt %d, timeout %d\n",
1380bdcd8170SKalle Valo 		   __func__, pmgmt, timeout);
1381bdcd8170SKalle Valo 
1382990bd915SVasanthakumar Thiagarajan 	if (!ath6kl_cfg80211_ready(vif))
1383bdcd8170SKalle Valo 		return -EIO;
1384bdcd8170SKalle Valo 
1385bdcd8170SKalle Valo 	if (pmgmt) {
1386bdcd8170SKalle Valo 		ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: max perf\n", __func__);
1387bdcd8170SKalle Valo 		mode.pwr_mode = REC_POWER;
1388bdcd8170SKalle Valo 	} else {
1389bdcd8170SKalle Valo 		ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: rec power\n", __func__);
1390bdcd8170SKalle Valo 		mode.pwr_mode = MAX_PERF_POWER;
1391bdcd8170SKalle Valo 	}
1392bdcd8170SKalle Valo 
1393334234b5SVasanthakumar Thiagarajan 	if (ath6kl_wmi_powermode_cmd(ar->wmi, vif->fw_vif_idx,
1394334234b5SVasanthakumar Thiagarajan 				     mode.pwr_mode) != 0) {
1395bdcd8170SKalle Valo 		ath6kl_err("wmi_powermode_cmd failed\n");
1396bdcd8170SKalle Valo 		return -EIO;
1397bdcd8170SKalle Valo 	}
1398bdcd8170SKalle Valo 
1399bdcd8170SKalle Valo 	return 0;
1400bdcd8170SKalle Valo }
1401bdcd8170SKalle Valo 
140255055976SVasanthakumar Thiagarajan static struct net_device *ath6kl_cfg80211_add_iface(struct wiphy *wiphy,
140355055976SVasanthakumar Thiagarajan 						    char *name,
140455055976SVasanthakumar Thiagarajan 						    enum nl80211_iftype type,
140555055976SVasanthakumar Thiagarajan 						    u32 *flags,
140655055976SVasanthakumar Thiagarajan 						    struct vif_params *params)
140755055976SVasanthakumar Thiagarajan {
140855055976SVasanthakumar Thiagarajan 	struct ath6kl *ar = wiphy_priv(wiphy);
140955055976SVasanthakumar Thiagarajan 	struct net_device *ndev;
141055055976SVasanthakumar Thiagarajan 	u8 if_idx, nw_type;
141155055976SVasanthakumar Thiagarajan 
141271f96ee6SKalle Valo 	if (ar->num_vif == ar->vif_max) {
141355055976SVasanthakumar Thiagarajan 		ath6kl_err("Reached maximum number of supported vif\n");
141455055976SVasanthakumar Thiagarajan 		return ERR_PTR(-EINVAL);
141555055976SVasanthakumar Thiagarajan 	}
141655055976SVasanthakumar Thiagarajan 
141755055976SVasanthakumar Thiagarajan 	if (!ath6kl_is_valid_iftype(ar, type, &if_idx, &nw_type)) {
141855055976SVasanthakumar Thiagarajan 		ath6kl_err("Not a supported interface type\n");
141955055976SVasanthakumar Thiagarajan 		return ERR_PTR(-EINVAL);
142055055976SVasanthakumar Thiagarajan 	}
142155055976SVasanthakumar Thiagarajan 
142255055976SVasanthakumar Thiagarajan 	ndev = ath6kl_interface_add(ar, name, type, if_idx, nw_type);
142355055976SVasanthakumar Thiagarajan 	if (!ndev)
142455055976SVasanthakumar Thiagarajan 		return ERR_PTR(-ENOMEM);
142555055976SVasanthakumar Thiagarajan 
142655055976SVasanthakumar Thiagarajan 	ar->num_vif++;
142755055976SVasanthakumar Thiagarajan 
142855055976SVasanthakumar Thiagarajan 	return ndev;
142955055976SVasanthakumar Thiagarajan }
143055055976SVasanthakumar Thiagarajan 
143155055976SVasanthakumar Thiagarajan static int ath6kl_cfg80211_del_iface(struct wiphy *wiphy,
143255055976SVasanthakumar Thiagarajan 				     struct net_device *ndev)
143355055976SVasanthakumar Thiagarajan {
143455055976SVasanthakumar Thiagarajan 	struct ath6kl *ar = wiphy_priv(wiphy);
143555055976SVasanthakumar Thiagarajan 	struct ath6kl_vif *vif = netdev_priv(ndev);
143655055976SVasanthakumar Thiagarajan 
143711f6e40dSVasanthakumar Thiagarajan 	spin_lock_bh(&ar->list_lock);
143855055976SVasanthakumar Thiagarajan 	list_del(&vif->list);
143911f6e40dSVasanthakumar Thiagarajan 	spin_unlock_bh(&ar->list_lock);
144055055976SVasanthakumar Thiagarajan 
144155055976SVasanthakumar Thiagarajan 	ath6kl_cleanup_vif(vif, test_bit(WMI_READY, &ar->flag));
144255055976SVasanthakumar Thiagarajan 
1443c25889e8SKalle Valo 	ath6kl_cfg80211_vif_cleanup(vif);
144455055976SVasanthakumar Thiagarajan 
144555055976SVasanthakumar Thiagarajan 	return 0;
144655055976SVasanthakumar Thiagarajan }
144755055976SVasanthakumar Thiagarajan 
1448bdcd8170SKalle Valo static int ath6kl_cfg80211_change_iface(struct wiphy *wiphy,
1449bdcd8170SKalle Valo 					struct net_device *ndev,
1450bdcd8170SKalle Valo 					enum nl80211_iftype type, u32 *flags,
1451bdcd8170SKalle Valo 					struct vif_params *params)
1452bdcd8170SKalle Valo {
1453f5938f24SVasanthakumar Thiagarajan 	struct ath6kl_vif *vif = netdev_priv(ndev);
14541e8d13b0SVasanthakumar Thiagarajan 	int i;
1455bdcd8170SKalle Valo 
1456bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: type %u\n", __func__, type);
1457bdcd8170SKalle Valo 
14581e8d13b0SVasanthakumar Thiagarajan 	/*
14591e8d13b0SVasanthakumar Thiagarajan 	 * Don't bring up p2p on an interface which is not initialized
14601e8d13b0SVasanthakumar Thiagarajan 	 * for p2p operation where fw does not have capability to switch
14611e8d13b0SVasanthakumar Thiagarajan 	 * dynamically between non-p2p and p2p type interface.
14621e8d13b0SVasanthakumar Thiagarajan 	 */
14631e8d13b0SVasanthakumar Thiagarajan 	if (!test_bit(ATH6KL_FW_CAPABILITY_STA_P2PDEV_DUPLEX,
14641e8d13b0SVasanthakumar Thiagarajan 		      vif->ar->fw_capabilities) &&
14651e8d13b0SVasanthakumar Thiagarajan 	    (type == NL80211_IFTYPE_P2P_CLIENT ||
14661e8d13b0SVasanthakumar Thiagarajan 	     type == NL80211_IFTYPE_P2P_GO)) {
14671e8d13b0SVasanthakumar Thiagarajan 		if (vif->ar->vif_max == 1) {
14681e8d13b0SVasanthakumar Thiagarajan 			if (vif->fw_vif_idx != 0)
14691e8d13b0SVasanthakumar Thiagarajan 				return -EINVAL;
14701e8d13b0SVasanthakumar Thiagarajan 			else
14711e8d13b0SVasanthakumar Thiagarajan 				goto set_iface_type;
14721e8d13b0SVasanthakumar Thiagarajan 		}
14731e8d13b0SVasanthakumar Thiagarajan 
14741e8d13b0SVasanthakumar Thiagarajan 		for (i = vif->ar->max_norm_iface; i < vif->ar->vif_max; i++) {
14751e8d13b0SVasanthakumar Thiagarajan 			if (i == vif->fw_vif_idx)
14761e8d13b0SVasanthakumar Thiagarajan 				break;
14771e8d13b0SVasanthakumar Thiagarajan 		}
14781e8d13b0SVasanthakumar Thiagarajan 
14791e8d13b0SVasanthakumar Thiagarajan 		if (i == vif->ar->vif_max) {
14801e8d13b0SVasanthakumar Thiagarajan 			ath6kl_err("Invalid interface to bring up P2P\n");
14811e8d13b0SVasanthakumar Thiagarajan 			return -EINVAL;
14821e8d13b0SVasanthakumar Thiagarajan 		}
14831e8d13b0SVasanthakumar Thiagarajan 	}
14841e8d13b0SVasanthakumar Thiagarajan 
14851e8d13b0SVasanthakumar Thiagarajan set_iface_type:
1486bdcd8170SKalle Valo 	switch (type) {
1487bdcd8170SKalle Valo 	case NL80211_IFTYPE_STATION:
1488f5938f24SVasanthakumar Thiagarajan 		vif->next_mode = INFRA_NETWORK;
1489bdcd8170SKalle Valo 		break;
1490bdcd8170SKalle Valo 	case NL80211_IFTYPE_ADHOC:
1491f5938f24SVasanthakumar Thiagarajan 		vif->next_mode = ADHOC_NETWORK;
1492bdcd8170SKalle Valo 		break;
14936e4604c8SJouni Malinen 	case NL80211_IFTYPE_AP:
1494f5938f24SVasanthakumar Thiagarajan 		vif->next_mode = AP_NETWORK;
14956e4604c8SJouni Malinen 		break;
14966b5e5d25SJouni Malinen 	case NL80211_IFTYPE_P2P_CLIENT:
1497f5938f24SVasanthakumar Thiagarajan 		vif->next_mode = INFRA_NETWORK;
14986b5e5d25SJouni Malinen 		break;
14996b5e5d25SJouni Malinen 	case NL80211_IFTYPE_P2P_GO:
1500f5938f24SVasanthakumar Thiagarajan 		vif->next_mode = AP_NETWORK;
15016b5e5d25SJouni Malinen 		break;
1502bdcd8170SKalle Valo 	default:
1503bdcd8170SKalle Valo 		ath6kl_err("invalid interface type %u\n", type);
1504bdcd8170SKalle Valo 		return -EOPNOTSUPP;
1505bdcd8170SKalle Valo 	}
1506bdcd8170SKalle Valo 
1507551959d8SVasanthakumar Thiagarajan 	vif->wdev.iftype = type;
1508bdcd8170SKalle Valo 
1509bdcd8170SKalle Valo 	return 0;
1510bdcd8170SKalle Valo }
1511bdcd8170SKalle Valo 
1512bdcd8170SKalle Valo static int ath6kl_cfg80211_join_ibss(struct wiphy *wiphy,
1513bdcd8170SKalle Valo 				     struct net_device *dev,
1514bdcd8170SKalle Valo 				     struct cfg80211_ibss_params *ibss_param)
1515bdcd8170SKalle Valo {
1516bdcd8170SKalle Valo 	struct ath6kl *ar = ath6kl_priv(dev);
151759c98449SVasanthakumar Thiagarajan 	struct ath6kl_vif *vif = netdev_priv(dev);
1518bdcd8170SKalle Valo 	int status;
1519bdcd8170SKalle Valo 
1520990bd915SVasanthakumar Thiagarajan 	if (!ath6kl_cfg80211_ready(vif))
1521bdcd8170SKalle Valo 		return -EIO;
1522bdcd8170SKalle Valo 
15233450334fSVasanthakumar Thiagarajan 	vif->ssid_len = ibss_param->ssid_len;
15243450334fSVasanthakumar Thiagarajan 	memcpy(vif->ssid, ibss_param->ssid, vif->ssid_len);
1525bdcd8170SKalle Valo 
1526bdcd8170SKalle Valo 	if (ibss_param->channel)
1527f74bac54SVasanthakumar Thiagarajan 		vif->ch_hint = ibss_param->channel->center_freq;
1528bdcd8170SKalle Valo 
1529bdcd8170SKalle Valo 	if (ibss_param->channel_fixed) {
1530bdcd8170SKalle Valo 		/*
1531bdcd8170SKalle Valo 		 * TODO: channel_fixed: The channel should be fixed, do not
1532bdcd8170SKalle Valo 		 * search for IBSSs to join on other channels. Target
1533bdcd8170SKalle Valo 		 * firmware does not support this feature, needs to be
1534bdcd8170SKalle Valo 		 * updated.
1535bdcd8170SKalle Valo 		 */
1536bdcd8170SKalle Valo 		return -EOPNOTSUPP;
1537bdcd8170SKalle Valo 	}
1538bdcd8170SKalle Valo 
15398c8b65e3SVasanthakumar Thiagarajan 	memset(vif->req_bssid, 0, sizeof(vif->req_bssid));
1540bdcd8170SKalle Valo 	if (ibss_param->bssid && !is_broadcast_ether_addr(ibss_param->bssid))
15418c8b65e3SVasanthakumar Thiagarajan 		memcpy(vif->req_bssid, ibss_param->bssid,
15428c8b65e3SVasanthakumar Thiagarajan 		       sizeof(vif->req_bssid));
1543bdcd8170SKalle Valo 
1544240d2799SVasanthakumar Thiagarajan 	ath6kl_set_wpa_version(vif, 0);
1545bdcd8170SKalle Valo 
1546240d2799SVasanthakumar Thiagarajan 	status = ath6kl_set_auth_type(vif, NL80211_AUTHTYPE_OPEN_SYSTEM);
1547bdcd8170SKalle Valo 	if (status)
1548bdcd8170SKalle Valo 		return status;
1549bdcd8170SKalle Valo 
1550bdcd8170SKalle Valo 	if (ibss_param->privacy) {
1551240d2799SVasanthakumar Thiagarajan 		ath6kl_set_cipher(vif, WLAN_CIPHER_SUITE_WEP40, true);
1552240d2799SVasanthakumar Thiagarajan 		ath6kl_set_cipher(vif, WLAN_CIPHER_SUITE_WEP40, false);
1553bdcd8170SKalle Valo 	} else {
1554240d2799SVasanthakumar Thiagarajan 		ath6kl_set_cipher(vif, 0, true);
1555240d2799SVasanthakumar Thiagarajan 		ath6kl_set_cipher(vif, 0, false);
1556bdcd8170SKalle Valo 	}
1557bdcd8170SKalle Valo 
1558f5938f24SVasanthakumar Thiagarajan 	vif->nw_type = vif->next_mode;
1559bdcd8170SKalle Valo 
1560bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
1561bdcd8170SKalle Valo 		   "%s: connect called with authmode %d dot11 auth %d"
1562bdcd8170SKalle Valo 		   " PW crypto %d PW crypto len %d GRP crypto %d"
1563bdcd8170SKalle Valo 		   " GRP crypto len %d channel hint %u\n",
1564bdcd8170SKalle Valo 		   __func__,
15653450334fSVasanthakumar Thiagarajan 		   vif->auth_mode, vif->dot11_auth_mode, vif->prwise_crypto,
15663450334fSVasanthakumar Thiagarajan 		   vif->prwise_crypto_len, vif->grp_crypto,
1567f74bac54SVasanthakumar Thiagarajan 		   vif->grp_crypto_len, vif->ch_hint);
1568bdcd8170SKalle Valo 
1569334234b5SVasanthakumar Thiagarajan 	status = ath6kl_wmi_connect_cmd(ar->wmi, vif->fw_vif_idx, vif->nw_type,
15703450334fSVasanthakumar Thiagarajan 					vif->dot11_auth_mode, vif->auth_mode,
15713450334fSVasanthakumar Thiagarajan 					vif->prwise_crypto,
15723450334fSVasanthakumar Thiagarajan 					vif->prwise_crypto_len,
15733450334fSVasanthakumar Thiagarajan 					vif->grp_crypto, vif->grp_crypto_len,
15743450334fSVasanthakumar Thiagarajan 					vif->ssid_len, vif->ssid,
1575f74bac54SVasanthakumar Thiagarajan 					vif->req_bssid, vif->ch_hint,
15763ca9d1fcSAarthi Thiruvengadam 					ar->connect_ctrl_flags, SUBTYPE_NONE);
157759c98449SVasanthakumar Thiagarajan 	set_bit(CONNECT_PEND, &vif->flags);
1578bdcd8170SKalle Valo 
1579bdcd8170SKalle Valo 	return 0;
1580bdcd8170SKalle Valo }
1581bdcd8170SKalle Valo 
1582bdcd8170SKalle Valo static int ath6kl_cfg80211_leave_ibss(struct wiphy *wiphy,
1583bdcd8170SKalle Valo 				      struct net_device *dev)
1584bdcd8170SKalle Valo {
15853450334fSVasanthakumar Thiagarajan 	struct ath6kl_vif *vif = netdev_priv(dev);
1586bdcd8170SKalle Valo 
1587990bd915SVasanthakumar Thiagarajan 	if (!ath6kl_cfg80211_ready(vif))
1588bdcd8170SKalle Valo 		return -EIO;
1589bdcd8170SKalle Valo 
1590240d2799SVasanthakumar Thiagarajan 	ath6kl_disconnect(vif);
15913450334fSVasanthakumar Thiagarajan 	memset(vif->ssid, 0, sizeof(vif->ssid));
15923450334fSVasanthakumar Thiagarajan 	vif->ssid_len = 0;
1593bdcd8170SKalle Valo 
1594bdcd8170SKalle Valo 	return 0;
1595bdcd8170SKalle Valo }
1596bdcd8170SKalle Valo 
1597bdcd8170SKalle Valo static const u32 cipher_suites[] = {
1598bdcd8170SKalle Valo 	WLAN_CIPHER_SUITE_WEP40,
1599bdcd8170SKalle Valo 	WLAN_CIPHER_SUITE_WEP104,
1600bdcd8170SKalle Valo 	WLAN_CIPHER_SUITE_TKIP,
1601bdcd8170SKalle Valo 	WLAN_CIPHER_SUITE_CCMP,
1602837cb97eSJouni Malinen 	CCKM_KRK_CIPHER_SUITE,
16035e07021eSDai Shuibing 	WLAN_CIPHER_SUITE_SMS4,
1604bdcd8170SKalle Valo };
1605bdcd8170SKalle Valo 
1606bdcd8170SKalle Valo static bool is_rate_legacy(s32 rate)
1607bdcd8170SKalle Valo {
1608bdcd8170SKalle Valo 	static const s32 legacy[] = { 1000, 2000, 5500, 11000,
1609bdcd8170SKalle Valo 		6000, 9000, 12000, 18000, 24000,
1610bdcd8170SKalle Valo 		36000, 48000, 54000
1611bdcd8170SKalle Valo 	};
1612bdcd8170SKalle Valo 	u8 i;
1613bdcd8170SKalle Valo 
1614bdcd8170SKalle Valo 	for (i = 0; i < ARRAY_SIZE(legacy); i++)
1615bdcd8170SKalle Valo 		if (rate == legacy[i])
1616bdcd8170SKalle Valo 			return true;
1617bdcd8170SKalle Valo 
1618bdcd8170SKalle Valo 	return false;
1619bdcd8170SKalle Valo }
1620bdcd8170SKalle Valo 
1621bdcd8170SKalle Valo static bool is_rate_ht20(s32 rate, u8 *mcs, bool *sgi)
1622bdcd8170SKalle Valo {
1623bdcd8170SKalle Valo 	static const s32 ht20[] = { 6500, 13000, 19500, 26000, 39000,
1624bdcd8170SKalle Valo 		52000, 58500, 65000, 72200
1625bdcd8170SKalle Valo 	};
1626bdcd8170SKalle Valo 	u8 i;
1627bdcd8170SKalle Valo 
1628bdcd8170SKalle Valo 	for (i = 0; i < ARRAY_SIZE(ht20); i++) {
1629bdcd8170SKalle Valo 		if (rate == ht20[i]) {
1630bdcd8170SKalle Valo 			if (i == ARRAY_SIZE(ht20) - 1)
1631bdcd8170SKalle Valo 				/* last rate uses sgi */
1632bdcd8170SKalle Valo 				*sgi = true;
1633bdcd8170SKalle Valo 			else
1634bdcd8170SKalle Valo 				*sgi = false;
1635bdcd8170SKalle Valo 
1636bdcd8170SKalle Valo 			*mcs = i;
1637bdcd8170SKalle Valo 			return true;
1638bdcd8170SKalle Valo 		}
1639bdcd8170SKalle Valo 	}
1640bdcd8170SKalle Valo 	return false;
1641bdcd8170SKalle Valo }
1642bdcd8170SKalle Valo 
1643bdcd8170SKalle Valo static bool is_rate_ht40(s32 rate, u8 *mcs, bool *sgi)
1644bdcd8170SKalle Valo {
1645bdcd8170SKalle Valo 	static const s32 ht40[] = { 13500, 27000, 40500, 54000,
1646bdcd8170SKalle Valo 		81000, 108000, 121500, 135000,
1647bdcd8170SKalle Valo 		150000
1648bdcd8170SKalle Valo 	};
1649bdcd8170SKalle Valo 	u8 i;
1650bdcd8170SKalle Valo 
1651bdcd8170SKalle Valo 	for (i = 0; i < ARRAY_SIZE(ht40); i++) {
1652bdcd8170SKalle Valo 		if (rate == ht40[i]) {
1653bdcd8170SKalle Valo 			if (i == ARRAY_SIZE(ht40) - 1)
1654bdcd8170SKalle Valo 				/* last rate uses sgi */
1655bdcd8170SKalle Valo 				*sgi = true;
1656bdcd8170SKalle Valo 			else
1657bdcd8170SKalle Valo 				*sgi = false;
1658bdcd8170SKalle Valo 
1659bdcd8170SKalle Valo 			*mcs = i;
1660bdcd8170SKalle Valo 			return true;
1661bdcd8170SKalle Valo 		}
1662bdcd8170SKalle Valo 	}
1663bdcd8170SKalle Valo 
1664bdcd8170SKalle Valo 	return false;
1665bdcd8170SKalle Valo }
1666bdcd8170SKalle Valo 
1667bdcd8170SKalle Valo static int ath6kl_get_station(struct wiphy *wiphy, struct net_device *dev,
1668bdcd8170SKalle Valo 			      u8 *mac, struct station_info *sinfo)
1669bdcd8170SKalle Valo {
1670bdcd8170SKalle Valo 	struct ath6kl *ar = ath6kl_priv(dev);
167159c98449SVasanthakumar Thiagarajan 	struct ath6kl_vif *vif = netdev_priv(dev);
1672bdcd8170SKalle Valo 	long left;
1673bdcd8170SKalle Valo 	bool sgi;
1674bdcd8170SKalle Valo 	s32 rate;
1675bdcd8170SKalle Valo 	int ret;
1676bdcd8170SKalle Valo 	u8 mcs;
1677bdcd8170SKalle Valo 
16788c8b65e3SVasanthakumar Thiagarajan 	if (memcmp(mac, vif->bssid, ETH_ALEN) != 0)
1679bdcd8170SKalle Valo 		return -ENOENT;
1680bdcd8170SKalle Valo 
1681bdcd8170SKalle Valo 	if (down_interruptible(&ar->sem))
1682bdcd8170SKalle Valo 		return -EBUSY;
1683bdcd8170SKalle Valo 
1684b95907a7SVasanthakumar Thiagarajan 	set_bit(STATS_UPDATE_PEND, &vif->flags);
1685bdcd8170SKalle Valo 
1686334234b5SVasanthakumar Thiagarajan 	ret = ath6kl_wmi_get_stats_cmd(ar->wmi, vif->fw_vif_idx);
1687bdcd8170SKalle Valo 
1688bdcd8170SKalle Valo 	if (ret != 0) {
1689bdcd8170SKalle Valo 		up(&ar->sem);
1690bdcd8170SKalle Valo 		return -EIO;
1691bdcd8170SKalle Valo 	}
1692bdcd8170SKalle Valo 
1693bdcd8170SKalle Valo 	left = wait_event_interruptible_timeout(ar->event_wq,
1694bdcd8170SKalle Valo 						!test_bit(STATS_UPDATE_PEND,
1695b95907a7SVasanthakumar Thiagarajan 							  &vif->flags),
1696bdcd8170SKalle Valo 						WMI_TIMEOUT);
1697bdcd8170SKalle Valo 
1698bdcd8170SKalle Valo 	up(&ar->sem);
1699bdcd8170SKalle Valo 
1700bdcd8170SKalle Valo 	if (left == 0)
1701bdcd8170SKalle Valo 		return -ETIMEDOUT;
1702bdcd8170SKalle Valo 	else if (left < 0)
1703bdcd8170SKalle Valo 		return left;
1704bdcd8170SKalle Valo 
1705b95907a7SVasanthakumar Thiagarajan 	if (vif->target_stats.rx_byte) {
1706b95907a7SVasanthakumar Thiagarajan 		sinfo->rx_bytes = vif->target_stats.rx_byte;
1707bdcd8170SKalle Valo 		sinfo->filled |= STATION_INFO_RX_BYTES;
1708b95907a7SVasanthakumar Thiagarajan 		sinfo->rx_packets = vif->target_stats.rx_pkt;
1709bdcd8170SKalle Valo 		sinfo->filled |= STATION_INFO_RX_PACKETS;
1710bdcd8170SKalle Valo 	}
1711bdcd8170SKalle Valo 
1712b95907a7SVasanthakumar Thiagarajan 	if (vif->target_stats.tx_byte) {
1713b95907a7SVasanthakumar Thiagarajan 		sinfo->tx_bytes = vif->target_stats.tx_byte;
1714bdcd8170SKalle Valo 		sinfo->filled |= STATION_INFO_TX_BYTES;
1715b95907a7SVasanthakumar Thiagarajan 		sinfo->tx_packets = vif->target_stats.tx_pkt;
1716bdcd8170SKalle Valo 		sinfo->filled |= STATION_INFO_TX_PACKETS;
1717bdcd8170SKalle Valo 	}
1718bdcd8170SKalle Valo 
1719b95907a7SVasanthakumar Thiagarajan 	sinfo->signal = vif->target_stats.cs_rssi;
1720bdcd8170SKalle Valo 	sinfo->filled |= STATION_INFO_SIGNAL;
1721bdcd8170SKalle Valo 
1722b95907a7SVasanthakumar Thiagarajan 	rate = vif->target_stats.tx_ucast_rate;
1723bdcd8170SKalle Valo 
1724bdcd8170SKalle Valo 	if (is_rate_legacy(rate)) {
1725bdcd8170SKalle Valo 		sinfo->txrate.legacy = rate / 100;
1726bdcd8170SKalle Valo 	} else if (is_rate_ht20(rate, &mcs, &sgi)) {
1727bdcd8170SKalle Valo 		if (sgi) {
1728bdcd8170SKalle Valo 			sinfo->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
1729bdcd8170SKalle Valo 			sinfo->txrate.mcs = mcs - 1;
1730bdcd8170SKalle Valo 		} else {
1731bdcd8170SKalle Valo 			sinfo->txrate.mcs = mcs;
1732bdcd8170SKalle Valo 		}
1733bdcd8170SKalle Valo 
1734bdcd8170SKalle Valo 		sinfo->txrate.flags |= RATE_INFO_FLAGS_MCS;
1735bdcd8170SKalle Valo 	} else if (is_rate_ht40(rate, &mcs, &sgi)) {
1736bdcd8170SKalle Valo 		if (sgi) {
1737bdcd8170SKalle Valo 			sinfo->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
1738bdcd8170SKalle Valo 			sinfo->txrate.mcs = mcs - 1;
1739bdcd8170SKalle Valo 		} else {
1740bdcd8170SKalle Valo 			sinfo->txrate.mcs = mcs;
1741bdcd8170SKalle Valo 		}
1742bdcd8170SKalle Valo 
1743bdcd8170SKalle Valo 		sinfo->txrate.flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
1744bdcd8170SKalle Valo 		sinfo->txrate.flags |= RATE_INFO_FLAGS_MCS;
1745bdcd8170SKalle Valo 	} else {
17469a730834SKalle Valo 		ath6kl_dbg(ATH6KL_DBG_WLAN_CFG,
17479a730834SKalle Valo 			   "invalid rate from stats: %d\n", rate);
17489a730834SKalle Valo 		ath6kl_debug_war(ar, ATH6KL_WAR_INVALID_RATE);
1749bdcd8170SKalle Valo 		return 0;
1750bdcd8170SKalle Valo 	}
1751bdcd8170SKalle Valo 
1752bdcd8170SKalle Valo 	sinfo->filled |= STATION_INFO_TX_BITRATE;
1753bdcd8170SKalle Valo 
175459c98449SVasanthakumar Thiagarajan 	if (test_bit(CONNECTED, &vif->flags) &&
175559c98449SVasanthakumar Thiagarajan 	    test_bit(DTIM_PERIOD_AVAIL, &vif->flags) &&
1756f5938f24SVasanthakumar Thiagarajan 	    vif->nw_type == INFRA_NETWORK) {
175732c10874SJouni Malinen 		sinfo->filled |= STATION_INFO_BSS_PARAM;
175832c10874SJouni Malinen 		sinfo->bss_param.flags = 0;
1759cf5333d7SVasanthakumar Thiagarajan 		sinfo->bss_param.dtim_period = vif->assoc_bss_dtim_period;
1760cf5333d7SVasanthakumar Thiagarajan 		sinfo->bss_param.beacon_interval = vif->assoc_bss_beacon_int;
176132c10874SJouni Malinen 	}
176232c10874SJouni Malinen 
1763bdcd8170SKalle Valo 	return 0;
1764bdcd8170SKalle Valo }
1765bdcd8170SKalle Valo 
1766bdcd8170SKalle Valo static int ath6kl_set_pmksa(struct wiphy *wiphy, struct net_device *netdev,
1767bdcd8170SKalle Valo 			    struct cfg80211_pmksa *pmksa)
1768bdcd8170SKalle Valo {
1769bdcd8170SKalle Valo 	struct ath6kl *ar = ath6kl_priv(netdev);
1770334234b5SVasanthakumar Thiagarajan 	struct ath6kl_vif *vif = netdev_priv(netdev);
1771334234b5SVasanthakumar Thiagarajan 
1772334234b5SVasanthakumar Thiagarajan 	return ath6kl_wmi_setpmkid_cmd(ar->wmi, vif->fw_vif_idx, pmksa->bssid,
1773bdcd8170SKalle Valo 				       pmksa->pmkid, true);
1774bdcd8170SKalle Valo }
1775bdcd8170SKalle Valo 
1776bdcd8170SKalle Valo static int ath6kl_del_pmksa(struct wiphy *wiphy, struct net_device *netdev,
1777bdcd8170SKalle Valo 			    struct cfg80211_pmksa *pmksa)
1778bdcd8170SKalle Valo {
1779bdcd8170SKalle Valo 	struct ath6kl *ar = ath6kl_priv(netdev);
1780334234b5SVasanthakumar Thiagarajan 	struct ath6kl_vif *vif = netdev_priv(netdev);
1781334234b5SVasanthakumar Thiagarajan 
1782334234b5SVasanthakumar Thiagarajan 	return ath6kl_wmi_setpmkid_cmd(ar->wmi, vif->fw_vif_idx, pmksa->bssid,
1783bdcd8170SKalle Valo 				       pmksa->pmkid, false);
1784bdcd8170SKalle Valo }
1785bdcd8170SKalle Valo 
1786bdcd8170SKalle Valo static int ath6kl_flush_pmksa(struct wiphy *wiphy, struct net_device *netdev)
1787bdcd8170SKalle Valo {
1788bdcd8170SKalle Valo 	struct ath6kl *ar = ath6kl_priv(netdev);
178959c98449SVasanthakumar Thiagarajan 	struct ath6kl_vif *vif = netdev_priv(netdev);
179059c98449SVasanthakumar Thiagarajan 
179159c98449SVasanthakumar Thiagarajan 	if (test_bit(CONNECTED, &vif->flags))
1792334234b5SVasanthakumar Thiagarajan 		return ath6kl_wmi_setpmkid_cmd(ar->wmi, vif->fw_vif_idx,
1793334234b5SVasanthakumar Thiagarajan 					       vif->bssid, NULL, false);
1794bdcd8170SKalle Valo 	return 0;
1795bdcd8170SKalle Valo }
1796bdcd8170SKalle Valo 
1797d91e8eeeSRaja Mani static int ath6kl_wow_usr(struct ath6kl *ar, struct ath6kl_vif *vif,
1798d91e8eeeSRaja Mani 			  struct cfg80211_wowlan *wow, u32 *filter)
17996cb3c714SRaja Mani {
1800d91e8eeeSRaja Mani 	int ret, pos;
1801d91e8eeeSRaja Mani 	u8 mask[WOW_MASK_SIZE];
18026cb3c714SRaja Mani 	u16 i;
18036cb3c714SRaja Mani 
1804d91e8eeeSRaja Mani 	/* Configure the patterns that we received from the user. */
18056cb3c714SRaja Mani 	for (i = 0; i < wow->n_patterns; i++) {
18066cb3c714SRaja Mani 
18076cb3c714SRaja Mani 		/*
18086cb3c714SRaja Mani 		 * Convert given nl80211 specific mask value to equivalent
18096cb3c714SRaja Mani 		 * driver specific mask value and send it to the chip along
18106cb3c714SRaja Mani 		 * with patterns. For example, If the mask value defined in
18116cb3c714SRaja Mani 		 * struct cfg80211_wowlan is 0xA (equivalent binary is 1010),
18126cb3c714SRaja Mani 		 * then equivalent driver specific mask value is
18136cb3c714SRaja Mani 		 * "0xFF 0x00 0xFF 0x00".
18146cb3c714SRaja Mani 		 */
18156cb3c714SRaja Mani 		memset(&mask, 0, sizeof(mask));
18166cb3c714SRaja Mani 		for (pos = 0; pos < wow->patterns[i].pattern_len; pos++) {
18176cb3c714SRaja Mani 			if (wow->patterns[i].mask[pos / 8] & (0x1 << (pos % 8)))
18186cb3c714SRaja Mani 				mask[pos] = 0xFF;
18196cb3c714SRaja Mani 		}
18206cb3c714SRaja Mani 		/*
18216cb3c714SRaja Mani 		 * Note: Pattern's offset is not passed as part of wowlan
18226cb3c714SRaja Mani 		 * parameter from CFG layer. So it's always passed as ZERO
18236cb3c714SRaja Mani 		 * to the firmware. It means, given WOW patterns are always
18246cb3c714SRaja Mani 		 * matched from the first byte of received pkt in the firmware.
18256cb3c714SRaja Mani 		 */
18266cb3c714SRaja Mani 		ret = ath6kl_wmi_add_wow_pattern_cmd(ar->wmi,
18276cb3c714SRaja Mani 				vif->fw_vif_idx, WOW_LIST_ID,
18286cb3c714SRaja Mani 				wow->patterns[i].pattern_len,
18296cb3c714SRaja Mani 				0 /* pattern offset */,
18306cb3c714SRaja Mani 				wow->patterns[i].pattern, mask);
18316cb3c714SRaja Mani 		if (ret)
18326cb3c714SRaja Mani 			return ret;
18336cb3c714SRaja Mani 	}
18346cb3c714SRaja Mani 
1835d91e8eeeSRaja Mani 	if (wow->disconnect)
1836d91e8eeeSRaja Mani 		*filter |= WOW_FILTER_OPTION_NWK_DISASSOC;
1837d91e8eeeSRaja Mani 
1838d91e8eeeSRaja Mani 	if (wow->magic_pkt)
1839d91e8eeeSRaja Mani 		*filter |= WOW_FILTER_OPTION_MAGIC_PACKET;
1840d91e8eeeSRaja Mani 
1841d91e8eeeSRaja Mani 	if (wow->gtk_rekey_failure)
1842d91e8eeeSRaja Mani 		*filter |= WOW_FILTER_OPTION_GTK_ERROR;
1843d91e8eeeSRaja Mani 
1844d91e8eeeSRaja Mani 	if (wow->eap_identity_req)
1845d91e8eeeSRaja Mani 		*filter |= WOW_FILTER_OPTION_EAP_REQ;
1846d91e8eeeSRaja Mani 
1847d91e8eeeSRaja Mani 	if (wow->four_way_handshake)
1848d91e8eeeSRaja Mani 		*filter |= WOW_FILTER_OPTION_8021X_4WAYHS;
1849d91e8eeeSRaja Mani 
1850d91e8eeeSRaja Mani 	return 0;
1851d91e8eeeSRaja Mani }
1852d91e8eeeSRaja Mani 
1853d91e8eeeSRaja Mani static int ath6kl_wow_ap(struct ath6kl *ar, struct ath6kl_vif *vif)
1854d91e8eeeSRaja Mani {
1855d91e8eeeSRaja Mani 	static const u8 unicst_pattern[] = { 0x00, 0x00, 0x00,
1856d91e8eeeSRaja Mani 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1857d91e8eeeSRaja Mani 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1858d91e8eeeSRaja Mani 		0x00, 0x08 };
1859d91e8eeeSRaja Mani 	static const u8 unicst_mask[] = { 0x01, 0x00, 0x00,
1860d91e8eeeSRaja Mani 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1861d91e8eeeSRaja Mani 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1862d91e8eeeSRaja Mani 		0x00, 0x7f };
1863d91e8eeeSRaja Mani 	u8 unicst_offset = 0;
1864d91e8eeeSRaja Mani 	static const u8 arp_pattern[] = { 0x08, 0x06 };
1865d91e8eeeSRaja Mani 	static const u8 arp_mask[] = { 0xff, 0xff };
1866d91e8eeeSRaja Mani 	u8 arp_offset = 20;
1867d91e8eeeSRaja Mani 	static const u8 discvr_pattern[] = { 0xe0, 0x00, 0x00, 0xf8 };
1868d91e8eeeSRaja Mani 	static const u8 discvr_mask[] = { 0xf0, 0x00, 0x00, 0xf8 };
1869d91e8eeeSRaja Mani 	u8 discvr_offset = 38;
1870d91e8eeeSRaja Mani 	static const u8 dhcp_pattern[] = { 0xff, 0xff, 0xff, 0xff,
1871d91e8eeeSRaja Mani 		0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1872d91e8eeeSRaja Mani 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00,
1873d91e8eeeSRaja Mani 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1874d91e8eeeSRaja Mani 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1875d91e8eeeSRaja Mani 		0x00, 0x00, 0x00, 0x00, 0x00, 0x43 /* port 67 */ };
1876d91e8eeeSRaja Mani 	static const u8 dhcp_mask[] = { 0xff, 0xff, 0xff, 0xff,
1877d91e8eeeSRaja Mani 		0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1878d91e8eeeSRaja Mani 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
1879d91e8eeeSRaja Mani 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1880d91e8eeeSRaja Mani 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1881d91e8eeeSRaja Mani 		0x00, 0x00, 0x00, 0x00, 0xff, 0xff /* port 67 */ };
1882d91e8eeeSRaja Mani 	u8 dhcp_offset = 0;
1883d91e8eeeSRaja Mani 	int ret;
1884d91e8eeeSRaja Mani 
1885d91e8eeeSRaja Mani 	/* Setup unicast IP, EAPOL-like and ARP pkt pattern */
1886d91e8eeeSRaja Mani 	ret = ath6kl_wmi_add_wow_pattern_cmd(ar->wmi,
1887d91e8eeeSRaja Mani 			vif->fw_vif_idx, WOW_LIST_ID,
1888d91e8eeeSRaja Mani 			sizeof(unicst_pattern), unicst_offset,
1889d91e8eeeSRaja Mani 			unicst_pattern, unicst_mask);
1890d91e8eeeSRaja Mani 	if (ret) {
1891d91e8eeeSRaja Mani 		ath6kl_err("failed to add WOW unicast IP pattern\n");
1892d91e8eeeSRaja Mani 		return ret;
1893d91e8eeeSRaja Mani 	}
1894d91e8eeeSRaja Mani 
1895d91e8eeeSRaja Mani 	/* Setup all ARP pkt pattern */
1896d91e8eeeSRaja Mani 	ret = ath6kl_wmi_add_wow_pattern_cmd(ar->wmi,
1897d91e8eeeSRaja Mani 			vif->fw_vif_idx, WOW_LIST_ID,
1898d91e8eeeSRaja Mani 			sizeof(arp_pattern), arp_offset,
1899d91e8eeeSRaja Mani 			arp_pattern, arp_mask);
1900d91e8eeeSRaja Mani 	if (ret) {
1901d91e8eeeSRaja Mani 		ath6kl_err("failed to add WOW ARP pattern\n");
1902d91e8eeeSRaja Mani 		return ret;
1903d91e8eeeSRaja Mani 	}
1904d91e8eeeSRaja Mani 
1905d91e8eeeSRaja Mani 	/*
1906d91e8eeeSRaja Mani 	 * Setup multicast pattern for mDNS 224.0.0.251,
1907d91e8eeeSRaja Mani 	 * SSDP 239.255.255.250 and LLMNR  224.0.0.252
1908d91e8eeeSRaja Mani 	 */
1909d91e8eeeSRaja Mani 	ret = ath6kl_wmi_add_wow_pattern_cmd(ar->wmi,
1910d91e8eeeSRaja Mani 			vif->fw_vif_idx, WOW_LIST_ID,
1911d91e8eeeSRaja Mani 			sizeof(discvr_pattern), discvr_offset,
1912d91e8eeeSRaja Mani 			discvr_pattern, discvr_mask);
1913d91e8eeeSRaja Mani 	if (ret) {
1914d91e8eeeSRaja Mani 		ath6kl_err("failed to add WOW mDNS/SSDP/LLMNR pattern\n");
1915d91e8eeeSRaja Mani 		return ret;
1916d91e8eeeSRaja Mani 	}
1917d91e8eeeSRaja Mani 
1918d91e8eeeSRaja Mani 	/* Setup all DHCP broadcast pkt pattern */
1919d91e8eeeSRaja Mani 	ret = ath6kl_wmi_add_wow_pattern_cmd(ar->wmi,
1920d91e8eeeSRaja Mani 			vif->fw_vif_idx, WOW_LIST_ID,
1921d91e8eeeSRaja Mani 			sizeof(dhcp_pattern), dhcp_offset,
1922d91e8eeeSRaja Mani 			dhcp_pattern, dhcp_mask);
1923d91e8eeeSRaja Mani 	if (ret) {
1924d91e8eeeSRaja Mani 		ath6kl_err("failed to add WOW DHCP broadcast pattern\n");
1925d91e8eeeSRaja Mani 		return ret;
1926d91e8eeeSRaja Mani 	}
1927d91e8eeeSRaja Mani 
1928d91e8eeeSRaja Mani 	return 0;
1929d91e8eeeSRaja Mani }
1930d91e8eeeSRaja Mani 
1931d91e8eeeSRaja Mani static int ath6kl_wow_sta(struct ath6kl *ar, struct ath6kl_vif *vif)
1932d91e8eeeSRaja Mani {
1933d91e8eeeSRaja Mani 	struct net_device *ndev = vif->ndev;
1934d91e8eeeSRaja Mani 	static const u8 discvr_pattern[] = { 0xe0, 0x00, 0x00, 0xf8 };
1935d91e8eeeSRaja Mani 	static const u8 discvr_mask[] = { 0xf0, 0x00, 0x00, 0xf8 };
1936d91e8eeeSRaja Mani 	u8 discvr_offset = 38;
1937d91e8eeeSRaja Mani 	u8 mac_mask[ETH_ALEN];
1938d91e8eeeSRaja Mani 	int ret;
1939d91e8eeeSRaja Mani 
1940d91e8eeeSRaja Mani 	/* Setup unicast pkt pattern */
1941d91e8eeeSRaja Mani 	memset(mac_mask, 0xff, ETH_ALEN);
1942d91e8eeeSRaja Mani 	ret = ath6kl_wmi_add_wow_pattern_cmd(ar->wmi,
1943d91e8eeeSRaja Mani 				vif->fw_vif_idx, WOW_LIST_ID,
1944d91e8eeeSRaja Mani 				ETH_ALEN, 0, ndev->dev_addr,
1945d91e8eeeSRaja Mani 				mac_mask);
1946d91e8eeeSRaja Mani 	if (ret) {
1947d91e8eeeSRaja Mani 		ath6kl_err("failed to add WOW unicast pattern\n");
1948d91e8eeeSRaja Mani 		return ret;
1949d91e8eeeSRaja Mani 	}
1950d91e8eeeSRaja Mani 
1951d91e8eeeSRaja Mani 	/*
1952d91e8eeeSRaja Mani 	 * Setup multicast pattern for mDNS 224.0.0.251,
1953d91e8eeeSRaja Mani 	 * SSDP 239.255.255.250 and LLMNR 224.0.0.252
1954d91e8eeeSRaja Mani 	 */
1955d91e8eeeSRaja Mani 	if ((ndev->flags & IFF_ALLMULTI) ||
1956d91e8eeeSRaja Mani 	    (ndev->flags & IFF_MULTICAST && netdev_mc_count(ndev) > 0)) {
1957d91e8eeeSRaja Mani 		ret = ath6kl_wmi_add_wow_pattern_cmd(ar->wmi,
1958d91e8eeeSRaja Mani 				vif->fw_vif_idx, WOW_LIST_ID,
1959d91e8eeeSRaja Mani 				sizeof(discvr_pattern), discvr_offset,
1960d91e8eeeSRaja Mani 				discvr_pattern, discvr_mask);
1961d91e8eeeSRaja Mani 		if (ret) {
1962d91e8eeeSRaja Mani 			ath6kl_err("failed to add WOW mDNS/SSDP/LLMNR "
1963d91e8eeeSRaja Mani 				   "pattern\n");
1964d91e8eeeSRaja Mani 			return ret;
1965d91e8eeeSRaja Mani 		}
1966d91e8eeeSRaja Mani 	}
1967d91e8eeeSRaja Mani 
1968d91e8eeeSRaja Mani 	return 0;
1969d91e8eeeSRaja Mani }
1970d91e8eeeSRaja Mani 
1971055bde49SRaja Mani static int is_hsleep_mode_procsed(struct ath6kl_vif *vif)
1972055bde49SRaja Mani {
1973055bde49SRaja Mani 	return test_bit(HOST_SLEEP_MODE_CMD_PROCESSED, &vif->flags);
1974055bde49SRaja Mani }
1975055bde49SRaja Mani 
1976055bde49SRaja Mani static bool is_ctrl_ep_empty(struct ath6kl *ar)
1977055bde49SRaja Mani {
1978055bde49SRaja Mani 	return !ar->tx_pending[ar->ctrl_ep];
1979055bde49SRaja Mani }
1980055bde49SRaja Mani 
1981055bde49SRaja Mani static int ath6kl_cfg80211_host_sleep(struct ath6kl *ar, struct ath6kl_vif *vif)
1982055bde49SRaja Mani {
1983055bde49SRaja Mani 	int ret, left;
1984055bde49SRaja Mani 
1985055bde49SRaja Mani 	clear_bit(HOST_SLEEP_MODE_CMD_PROCESSED, &vif->flags);
1986055bde49SRaja Mani 
1987055bde49SRaja Mani 	ret = ath6kl_wmi_set_host_sleep_mode_cmd(ar->wmi, vif->fw_vif_idx,
1988055bde49SRaja Mani 						 ATH6KL_HOST_MODE_ASLEEP);
1989055bde49SRaja Mani 	if (ret)
1990055bde49SRaja Mani 		return ret;
1991055bde49SRaja Mani 
1992055bde49SRaja Mani 	left = wait_event_interruptible_timeout(ar->event_wq,
1993055bde49SRaja Mani 						is_hsleep_mode_procsed(vif),
1994055bde49SRaja Mani 						WMI_TIMEOUT);
1995055bde49SRaja Mani 	if (left == 0) {
1996055bde49SRaja Mani 		ath6kl_warn("timeout, didn't get host sleep cmd processed event\n");
1997055bde49SRaja Mani 		ret = -ETIMEDOUT;
1998055bde49SRaja Mani 	} else if (left < 0) {
1999055bde49SRaja Mani 		ath6kl_warn("error while waiting for host sleep cmd processed event %d\n",
2000055bde49SRaja Mani 			    left);
2001055bde49SRaja Mani 		ret = left;
2002055bde49SRaja Mani 	}
2003055bde49SRaja Mani 
2004055bde49SRaja Mani 	if (ar->tx_pending[ar->ctrl_ep]) {
2005055bde49SRaja Mani 		left = wait_event_interruptible_timeout(ar->event_wq,
2006055bde49SRaja Mani 							is_ctrl_ep_empty(ar),
2007055bde49SRaja Mani 							WMI_TIMEOUT);
2008055bde49SRaja Mani 		if (left == 0) {
2009055bde49SRaja Mani 			ath6kl_warn("clear wmi ctrl data timeout\n");
2010055bde49SRaja Mani 			ret = -ETIMEDOUT;
2011055bde49SRaja Mani 		} else if (left < 0) {
2012055bde49SRaja Mani 			ath6kl_warn("clear wmi ctrl data failed: %d\n", left);
2013055bde49SRaja Mani 			ret = left;
2014055bde49SRaja Mani 		}
2015055bde49SRaja Mani 	}
2016055bde49SRaja Mani 
2017055bde49SRaja Mani 	return ret;
2018055bde49SRaja Mani }
2019055bde49SRaja Mani 
2020d91e8eeeSRaja Mani static int ath6kl_wow_suspend(struct ath6kl *ar, struct cfg80211_wowlan *wow)
2021d91e8eeeSRaja Mani {
2022d91e8eeeSRaja Mani 	struct in_device *in_dev;
2023d91e8eeeSRaja Mani 	struct in_ifaddr *ifa;
2024d91e8eeeSRaja Mani 	struct ath6kl_vif *vif;
2025055bde49SRaja Mani 	int ret;
2026d91e8eeeSRaja Mani 	u32 filter = 0;
2027ce0dc0cfSRaja Mani 	u16 i, bmiss_time;
2028d91e8eeeSRaja Mani 	u8 index = 0;
2029d91e8eeeSRaja Mani 	__be32 ips[MAX_IP_ADDRS];
2030d91e8eeeSRaja Mani 
2031d91e8eeeSRaja Mani 	vif = ath6kl_vif_first(ar);
2032d91e8eeeSRaja Mani 	if (!vif)
2033d91e8eeeSRaja Mani 		return -EIO;
2034d91e8eeeSRaja Mani 
2035d91e8eeeSRaja Mani 	if (!ath6kl_cfg80211_ready(vif))
2036d91e8eeeSRaja Mani 		return -EIO;
2037d91e8eeeSRaja Mani 
2038d91e8eeeSRaja Mani 	if (!test_bit(CONNECTED, &vif->flags))
20393c411a43SRaja Mani 		return -ENOTCONN;
2040d91e8eeeSRaja Mani 
2041d91e8eeeSRaja Mani 	if (wow && (wow->n_patterns > WOW_MAX_FILTERS_PER_LIST))
2042d91e8eeeSRaja Mani 		return -EINVAL;
2043d91e8eeeSRaja Mani 
2044d91e8eeeSRaja Mani 	/* Clear existing WOW patterns */
2045d91e8eeeSRaja Mani 	for (i = 0; i < WOW_MAX_FILTERS_PER_LIST; i++)
2046d91e8eeeSRaja Mani 		ath6kl_wmi_del_wow_pattern_cmd(ar->wmi, vif->fw_vif_idx,
2047d91e8eeeSRaja Mani 					       WOW_LIST_ID, i);
2048d91e8eeeSRaja Mani 
2049d91e8eeeSRaja Mani 	/*
2050d91e8eeeSRaja Mani 	 * Skip the default WOW pattern configuration
2051d91e8eeeSRaja Mani 	 * if the driver receives any WOW patterns from
2052d91e8eeeSRaja Mani 	 * the user.
2053d91e8eeeSRaja Mani 	 */
2054d91e8eeeSRaja Mani 	if (wow)
2055d91e8eeeSRaja Mani 		ret = ath6kl_wow_usr(ar, vif, wow, &filter);
2056d91e8eeeSRaja Mani 	else if (vif->nw_type == AP_NETWORK)
2057d91e8eeeSRaja Mani 		ret = ath6kl_wow_ap(ar, vif);
2058d91e8eeeSRaja Mani 	else
2059d91e8eeeSRaja Mani 		ret = ath6kl_wow_sta(ar, vif);
2060d91e8eeeSRaja Mani 
2061d91e8eeeSRaja Mani 	if (ret)
2062d91e8eeeSRaja Mani 		return ret;
2063d91e8eeeSRaja Mani 
2064390a8c8fSRaja Mani 	netif_stop_queue(vif->ndev);
2065390a8c8fSRaja Mani 
2066ce0dc0cfSRaja Mani 	if (vif->nw_type != AP_NETWORK) {
2067ce0dc0cfSRaja Mani 		ret = ath6kl_wmi_listeninterval_cmd(ar->wmi, vif->fw_vif_idx,
2068ce0dc0cfSRaja Mani 						    ATH6KL_MAX_WOW_LISTEN_INTL,
2069ce0dc0cfSRaja Mani 						    0);
2070ce0dc0cfSRaja Mani 		if (ret)
2071ce0dc0cfSRaja Mani 			return ret;
2072ce0dc0cfSRaja Mani 
2073ce0dc0cfSRaja Mani 		/* Set listen interval x 15 times as bmiss time */
2074ce0dc0cfSRaja Mani 		bmiss_time = ATH6KL_MAX_WOW_LISTEN_INTL * 15;
2075ce0dc0cfSRaja Mani 		if (bmiss_time > ATH6KL_MAX_BMISS_TIME)
2076ce0dc0cfSRaja Mani 			bmiss_time = ATH6KL_MAX_BMISS_TIME;
2077ce0dc0cfSRaja Mani 
2078ce0dc0cfSRaja Mani 		ret = ath6kl_wmi_bmisstime_cmd(ar->wmi, vif->fw_vif_idx,
2079ce0dc0cfSRaja Mani 					       bmiss_time, 0);
2080ce0dc0cfSRaja Mani 		if (ret)
2081ce0dc0cfSRaja Mani 			return ret;
2082ce0dc0cfSRaja Mani 
2083ce0dc0cfSRaja Mani 		ret = ath6kl_wmi_scanparams_cmd(ar->wmi, vif->fw_vif_idx,
2084ce0dc0cfSRaja Mani 						0xFFFF, 0, 0xFFFF, 0, 0, 0,
2085ce0dc0cfSRaja Mani 						0, 0, 0, 0);
2086ce0dc0cfSRaja Mani 		if (ret)
2087ce0dc0cfSRaja Mani 			return ret;
2088ce0dc0cfSRaja Mani 	}
2089ce0dc0cfSRaja Mani 
2090390a8c8fSRaja Mani 	ar->state = ATH6KL_STATE_SUSPENDING;
2091390a8c8fSRaja Mani 
2092c08631c6SRaja Mani 	/* Setup own IP addr for ARP agent. */
2093c08631c6SRaja Mani 	in_dev = __in_dev_get_rtnl(vif->ndev);
2094c08631c6SRaja Mani 	if (!in_dev)
2095c08631c6SRaja Mani 		goto skip_arp;
2096c08631c6SRaja Mani 
2097c08631c6SRaja Mani 	ifa = in_dev->ifa_list;
2098c08631c6SRaja Mani 	memset(&ips, 0, sizeof(ips));
2099c08631c6SRaja Mani 
2100c08631c6SRaja Mani 	/* Configure IP addr only if IP address count < MAX_IP_ADDRS */
2101c08631c6SRaja Mani 	while (index < MAX_IP_ADDRS && ifa) {
2102c08631c6SRaja Mani 		ips[index] = ifa->ifa_local;
2103c08631c6SRaja Mani 		ifa = ifa->ifa_next;
2104c08631c6SRaja Mani 		index++;
2105c08631c6SRaja Mani 	}
2106c08631c6SRaja Mani 
2107c08631c6SRaja Mani 	if (ifa) {
2108c08631c6SRaja Mani 		ath6kl_err("total IP addr count is exceeding fw limit\n");
2109c08631c6SRaja Mani 		return -EINVAL;
2110c08631c6SRaja Mani 	}
2111c08631c6SRaja Mani 
2112c08631c6SRaja Mani 	ret = ath6kl_wmi_set_ip_cmd(ar->wmi, vif->fw_vif_idx, ips[0], ips[1]);
2113c08631c6SRaja Mani 	if (ret) {
2114c08631c6SRaja Mani 		ath6kl_err("fail to setup ip for arp agent\n");
2115c08631c6SRaja Mani 		return ret;
2116c08631c6SRaja Mani 	}
2117c08631c6SRaja Mani 
2118c08631c6SRaja Mani skip_arp:
21196cb3c714SRaja Mani 	ret = ath6kl_wmi_set_wow_mode_cmd(ar->wmi, vif->fw_vif_idx,
21206cb3c714SRaja Mani 					  ATH6KL_WOW_MODE_ENABLE,
21216cb3c714SRaja Mani 					  filter,
21226cb3c714SRaja Mani 					  WOW_HOST_REQ_DELAY);
21236cb3c714SRaja Mani 	if (ret)
21246cb3c714SRaja Mani 		return ret;
21256cb3c714SRaja Mani 
2126055bde49SRaja Mani 	ret = ath6kl_cfg80211_host_sleep(ar, vif);
21276cb3c714SRaja Mani 	if (ret)
21286cb3c714SRaja Mani 		return ret;
21296cb3c714SRaja Mani 
2130055bde49SRaja Mani 	return 0;
21316cb3c714SRaja Mani }
21326cb3c714SRaja Mani 
21336cb3c714SRaja Mani static int ath6kl_wow_resume(struct ath6kl *ar)
21346cb3c714SRaja Mani {
21356cb3c714SRaja Mani 	struct ath6kl_vif *vif;
21366cb3c714SRaja Mani 	int ret;
21376cb3c714SRaja Mani 
21386cb3c714SRaja Mani 	vif = ath6kl_vif_first(ar);
21396cb3c714SRaja Mani 	if (!vif)
21406cb3c714SRaja Mani 		return -EIO;
21416cb3c714SRaja Mani 
2142390a8c8fSRaja Mani 	ar->state = ATH6KL_STATE_RESUMING;
2143390a8c8fSRaja Mani 
21446cb3c714SRaja Mani 	ret = ath6kl_wmi_set_host_sleep_mode_cmd(ar->wmi, vif->fw_vif_idx,
21456cb3c714SRaja Mani 						 ATH6KL_HOST_MODE_AWAKE);
2146390a8c8fSRaja Mani 	if (ret) {
2147390a8c8fSRaja Mani 		ath6kl_warn("Failed to configure host sleep mode for "
2148390a8c8fSRaja Mani 			    "wow resume: %d\n", ret);
2149390a8c8fSRaja Mani 		ar->state = ATH6KL_STATE_WOW;
21506cb3c714SRaja Mani 		return ret;
21516cb3c714SRaja Mani 	}
21526cb3c714SRaja Mani 
2153ce0dc0cfSRaja Mani 	if (vif->nw_type != AP_NETWORK) {
2154ce0dc0cfSRaja Mani 		ret = ath6kl_wmi_scanparams_cmd(ar->wmi, vif->fw_vif_idx,
2155ce0dc0cfSRaja Mani 						0, 0, 0, 0, 0, 0, 3, 0, 0, 0);
2156ce0dc0cfSRaja Mani 		if (ret)
2157ce0dc0cfSRaja Mani 			return ret;
2158ce0dc0cfSRaja Mani 
2159ce0dc0cfSRaja Mani 		ret = ath6kl_wmi_listeninterval_cmd(ar->wmi, vif->fw_vif_idx,
2160ce0dc0cfSRaja Mani 						    vif->listen_intvl_t, 0);
2161ce0dc0cfSRaja Mani 		if (ret)
2162ce0dc0cfSRaja Mani 			return ret;
2163ce0dc0cfSRaja Mani 
2164ce0dc0cfSRaja Mani 		ret = ath6kl_wmi_bmisstime_cmd(ar->wmi, vif->fw_vif_idx,
2165ce0dc0cfSRaja Mani 					       vif->bmiss_time_t, 0);
2166ce0dc0cfSRaja Mani 		if (ret)
2167ce0dc0cfSRaja Mani 			return ret;
2168ce0dc0cfSRaja Mani 	}
2169ce0dc0cfSRaja Mani 
2170390a8c8fSRaja Mani 	ar->state = ATH6KL_STATE_ON;
2171390a8c8fSRaja Mani 
2172390a8c8fSRaja Mani 	netif_wake_queue(vif->ndev);
2173390a8c8fSRaja Mani 
2174390a8c8fSRaja Mani 	return 0;
2175390a8c8fSRaja Mani }
2176390a8c8fSRaja Mani 
217740abc2deSRaja Mani static int ath6kl_cfg80211_deepsleep_suspend(struct ath6kl *ar)
217840abc2deSRaja Mani {
217940abc2deSRaja Mani 	struct ath6kl_vif *vif;
218040abc2deSRaja Mani 	int ret;
218140abc2deSRaja Mani 
218240abc2deSRaja Mani 	vif = ath6kl_vif_first(ar);
218340abc2deSRaja Mani 	if (!vif)
218440abc2deSRaja Mani 		return -EIO;
218540abc2deSRaja Mani 
218640abc2deSRaja Mani 	if (!ath6kl_cfg80211_ready(vif))
218740abc2deSRaja Mani 		return -EIO;
218840abc2deSRaja Mani 
218940abc2deSRaja Mani 	ath6kl_cfg80211_stop_all(ar);
219040abc2deSRaja Mani 
219140abc2deSRaja Mani 	/* Save the current power mode before enabling power save */
219240abc2deSRaja Mani 	ar->wmi->saved_pwr_mode = ar->wmi->pwr_mode;
219340abc2deSRaja Mani 
219440abc2deSRaja Mani 	ret = ath6kl_wmi_powermode_cmd(ar->wmi, 0, REC_POWER);
219540abc2deSRaja Mani 	if (ret)
219640abc2deSRaja Mani 		return ret;
219740abc2deSRaja Mani 
219840abc2deSRaja Mani 	/* Disable WOW mode */
219940abc2deSRaja Mani 	ret = ath6kl_wmi_set_wow_mode_cmd(ar->wmi, vif->fw_vif_idx,
220040abc2deSRaja Mani 					  ATH6KL_WOW_MODE_DISABLE,
220140abc2deSRaja Mani 					  0, 0);
220240abc2deSRaja Mani 	if (ret)
220340abc2deSRaja Mani 		return ret;
220440abc2deSRaja Mani 
220540abc2deSRaja Mani 	/* Flush all non control pkts in TX path */
220640abc2deSRaja Mani 	ath6kl_tx_data_cleanup(ar);
220740abc2deSRaja Mani 
220840abc2deSRaja Mani 	ret = ath6kl_cfg80211_host_sleep(ar, vif);
220940abc2deSRaja Mani 	if (ret)
221040abc2deSRaja Mani 		return ret;
221140abc2deSRaja Mani 
221240abc2deSRaja Mani 	return 0;
221340abc2deSRaja Mani }
221440abc2deSRaja Mani 
221540abc2deSRaja Mani static int ath6kl_cfg80211_deepsleep_resume(struct ath6kl *ar)
221640abc2deSRaja Mani {
221740abc2deSRaja Mani 	struct ath6kl_vif *vif;
221840abc2deSRaja Mani 	int ret;
221940abc2deSRaja Mani 
222040abc2deSRaja Mani 	vif = ath6kl_vif_first(ar);
222140abc2deSRaja Mani 
222240abc2deSRaja Mani 	if (!vif)
222340abc2deSRaja Mani 		return -EIO;
222440abc2deSRaja Mani 
222540abc2deSRaja Mani 	if (ar->wmi->pwr_mode != ar->wmi->saved_pwr_mode) {
222640abc2deSRaja Mani 		ret = ath6kl_wmi_powermode_cmd(ar->wmi, 0,
222740abc2deSRaja Mani 					       ar->wmi->saved_pwr_mode);
222840abc2deSRaja Mani 		if (ret)
222940abc2deSRaja Mani 			return ret;
223040abc2deSRaja Mani 	}
223140abc2deSRaja Mani 
223240abc2deSRaja Mani 	ret = ath6kl_wmi_set_host_sleep_mode_cmd(ar->wmi, vif->fw_vif_idx,
223340abc2deSRaja Mani 						 ATH6KL_HOST_MODE_AWAKE);
223440abc2deSRaja Mani 	if (ret)
223540abc2deSRaja Mani 		return ret;
223640abc2deSRaja Mani 
223740abc2deSRaja Mani 	ar->state = ATH6KL_STATE_ON;
223840abc2deSRaja Mani 
223940abc2deSRaja Mani 	/* Reset scan parameter to default values */
224040abc2deSRaja Mani 	ret = ath6kl_wmi_scanparams_cmd(ar->wmi, vif->fw_vif_idx,
224140abc2deSRaja Mani 					0, 0, 0, 0, 0, 0, 3, 0, 0, 0);
224240abc2deSRaja Mani 	if (ret)
224340abc2deSRaja Mani 		return ret;
224440abc2deSRaja Mani 
224540abc2deSRaja Mani 	return 0;
224640abc2deSRaja Mani }
224740abc2deSRaja Mani 
224852d81a68SKalle Valo int ath6kl_cfg80211_suspend(struct ath6kl *ar,
22490f60e9f4SRaja Mani 			    enum ath6kl_cfg_suspend_mode mode,
22500f60e9f4SRaja Mani 			    struct cfg80211_wowlan *wow)
225152d81a68SKalle Valo {
22523d79499cSVivek Natarajan 	struct ath6kl_vif *vif;
2253390a8c8fSRaja Mani 	enum ath6kl_state prev_state;
225452d81a68SKalle Valo 	int ret;
225552d81a68SKalle Valo 
225652d81a68SKalle Valo 	switch (mode) {
2257d7c44e0bSRaja Mani 	case ATH6KL_CFG_SUSPEND_WOW:
2258d7c44e0bSRaja Mani 
2259d7c44e0bSRaja Mani 		ath6kl_dbg(ATH6KL_DBG_SUSPEND, "wow mode suspend\n");
2260d7c44e0bSRaja Mani 
2261d7c44e0bSRaja Mani 		/* Flush all non control pkts in TX path */
2262d7c44e0bSRaja Mani 		ath6kl_tx_data_cleanup(ar);
2263d7c44e0bSRaja Mani 
2264390a8c8fSRaja Mani 		prev_state = ar->state;
2265390a8c8fSRaja Mani 
2266d7c44e0bSRaja Mani 		ret = ath6kl_wow_suspend(ar, wow);
2267390a8c8fSRaja Mani 		if (ret) {
2268390a8c8fSRaja Mani 			ar->state = prev_state;
2269d7c44e0bSRaja Mani 			return ret;
2270390a8c8fSRaja Mani 		}
22711e9a905dSRaja Mani 
2272d7c44e0bSRaja Mani 		ar->state = ATH6KL_STATE_WOW;
2273d7c44e0bSRaja Mani 		break;
2274d7c44e0bSRaja Mani 
227552d81a68SKalle Valo 	case ATH6KL_CFG_SUSPEND_DEEPSLEEP:
2276524441e3SRaja Mani 
227740abc2deSRaja Mani 		ath6kl_dbg(ATH6KL_DBG_SUSPEND, "deep sleep suspend\n");
2278524441e3SRaja Mani 
227940abc2deSRaja Mani 		ret = ath6kl_cfg80211_deepsleep_suspend(ar);
228052d81a68SKalle Valo 		if (ret) {
228140abc2deSRaja Mani 			ath6kl_err("deepsleep suspend failed: %d\n", ret);
228240abc2deSRaja Mani 			return ret;
228352d81a68SKalle Valo 		}
228452d81a68SKalle Valo 
228576a9fbe2SKalle Valo 		ar->state = ATH6KL_STATE_DEEPSLEEP;
228676a9fbe2SKalle Valo 
228752d81a68SKalle Valo 		break;
2288b4b2a0b1SKalle Valo 
2289b4b2a0b1SKalle Valo 	case ATH6KL_CFG_SUSPEND_CUTPOWER:
2290524441e3SRaja Mani 
22917125f01dSKalle Valo 		ath6kl_cfg80211_stop_all(ar);
2292524441e3SRaja Mani 
2293b4b2a0b1SKalle Valo 		if (ar->state == ATH6KL_STATE_OFF) {
2294b4b2a0b1SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_SUSPEND,
2295b4b2a0b1SKalle Valo 				   "suspend hw off, no action for cutpower\n");
2296b4b2a0b1SKalle Valo 			break;
2297b4b2a0b1SKalle Valo 		}
2298b4b2a0b1SKalle Valo 
2299b4b2a0b1SKalle Valo 		ath6kl_dbg(ATH6KL_DBG_SUSPEND, "suspend cutting power\n");
2300b4b2a0b1SKalle Valo 
2301b4b2a0b1SKalle Valo 		ret = ath6kl_init_hw_stop(ar);
2302b4b2a0b1SKalle Valo 		if (ret) {
2303b4b2a0b1SKalle Valo 			ath6kl_warn("failed to stop hw during suspend: %d\n",
2304b4b2a0b1SKalle Valo 				    ret);
2305b4b2a0b1SKalle Valo 		}
2306b4b2a0b1SKalle Valo 
2307b4b2a0b1SKalle Valo 		ar->state = ATH6KL_STATE_CUTPOWER;
2308b4b2a0b1SKalle Valo 
2309b4b2a0b1SKalle Valo 		break;
2310b4b2a0b1SKalle Valo 
231110509f90SKalle Valo 	case ATH6KL_CFG_SUSPEND_SCHED_SCAN:
231210509f90SKalle Valo 		/*
231310509f90SKalle Valo 		 * Nothing needed for schedule scan, firmware is already in
231410509f90SKalle Valo 		 * wow mode and sleeping most of the time.
231510509f90SKalle Valo 		 */
231610509f90SKalle Valo 		break;
231710509f90SKalle Valo 
2318b4b2a0b1SKalle Valo 	default:
2319b4b2a0b1SKalle Valo 		break;
232052d81a68SKalle Valo 	}
232152d81a68SKalle Valo 
23223d79499cSVivek Natarajan 	list_for_each_entry(vif, &ar->vif_list, list)
23233d79499cSVivek Natarajan 		ath6kl_cfg80211_scan_complete_event(vif, true);
23243d79499cSVivek Natarajan 
232552d81a68SKalle Valo 	return 0;
232652d81a68SKalle Valo }
2327d6a434d6SKalle Valo EXPORT_SYMBOL(ath6kl_cfg80211_suspend);
232852d81a68SKalle Valo 
232952d81a68SKalle Valo int ath6kl_cfg80211_resume(struct ath6kl *ar)
233052d81a68SKalle Valo {
233176a9fbe2SKalle Valo 	int ret;
233276a9fbe2SKalle Valo 
233376a9fbe2SKalle Valo 	switch (ar->state) {
2334d7c44e0bSRaja Mani 	case  ATH6KL_STATE_WOW:
2335d7c44e0bSRaja Mani 		ath6kl_dbg(ATH6KL_DBG_SUSPEND, "wow mode resume\n");
2336d7c44e0bSRaja Mani 
2337d7c44e0bSRaja Mani 		ret = ath6kl_wow_resume(ar);
2338d7c44e0bSRaja Mani 		if (ret) {
2339d7c44e0bSRaja Mani 			ath6kl_warn("wow mode resume failed: %d\n", ret);
2340d7c44e0bSRaja Mani 			return ret;
2341d7c44e0bSRaja Mani 		}
2342d7c44e0bSRaja Mani 
2343d7c44e0bSRaja Mani 		break;
2344d7c44e0bSRaja Mani 
234576a9fbe2SKalle Valo 	case ATH6KL_STATE_DEEPSLEEP:
234640abc2deSRaja Mani 		ath6kl_dbg(ATH6KL_DBG_SUSPEND, "deep sleep resume\n");
234740abc2deSRaja Mani 
234840abc2deSRaja Mani 		ret = ath6kl_cfg80211_deepsleep_resume(ar);
234976a9fbe2SKalle Valo 		if (ret) {
235040abc2deSRaja Mani 			ath6kl_warn("deep sleep resume failed: %d\n", ret);
235140abc2deSRaja Mani 			return ret;
235276a9fbe2SKalle Valo 		}
235376a9fbe2SKalle Valo 		break;
235476a9fbe2SKalle Valo 
2355b4b2a0b1SKalle Valo 	case ATH6KL_STATE_CUTPOWER:
2356b4b2a0b1SKalle Valo 		ath6kl_dbg(ATH6KL_DBG_SUSPEND, "resume restoring power\n");
2357b4b2a0b1SKalle Valo 
2358b4b2a0b1SKalle Valo 		ret = ath6kl_init_hw_start(ar);
2359b4b2a0b1SKalle Valo 		if (ret) {
2360b4b2a0b1SKalle Valo 			ath6kl_warn("Failed to boot hw in resume: %d\n", ret);
2361b4b2a0b1SKalle Valo 			return ret;
2362b4b2a0b1SKalle Valo 		}
2363d7c44e0bSRaja Mani 		break;
2364b4b2a0b1SKalle Valo 
236510509f90SKalle Valo 	case ATH6KL_STATE_SCHED_SCAN:
236610509f90SKalle Valo 		break;
236710509f90SKalle Valo 
236876a9fbe2SKalle Valo 	default:
236976a9fbe2SKalle Valo 		break;
237052d81a68SKalle Valo 	}
237152d81a68SKalle Valo 
237252d81a68SKalle Valo 	return 0;
237352d81a68SKalle Valo }
2374d6a434d6SKalle Valo EXPORT_SYMBOL(ath6kl_cfg80211_resume);
237552d81a68SKalle Valo 
2376abcb344bSKalle Valo #ifdef CONFIG_PM
237752d81a68SKalle Valo 
237852d81a68SKalle Valo /* hif layer decides what suspend mode to use */
237952d81a68SKalle Valo static int __ath6kl_cfg80211_suspend(struct wiphy *wiphy,
2380abcb344bSKalle Valo 				 struct cfg80211_wowlan *wow)
2381abcb344bSKalle Valo {
2382abcb344bSKalle Valo 	struct ath6kl *ar = wiphy_priv(wiphy);
2383abcb344bSKalle Valo 
23840f60e9f4SRaja Mani 	return ath6kl_hif_suspend(ar, wow);
2385abcb344bSKalle Valo }
2386aa6cffc1SChilam Ng 
238752d81a68SKalle Valo static int __ath6kl_cfg80211_resume(struct wiphy *wiphy)
2388aa6cffc1SChilam Ng {
2389aa6cffc1SChilam Ng 	struct ath6kl *ar = wiphy_priv(wiphy);
2390aa6cffc1SChilam Ng 
2391aa6cffc1SChilam Ng 	return ath6kl_hif_resume(ar);
2392aa6cffc1SChilam Ng }
2393a918fb3cSRaja Mani 
2394a918fb3cSRaja Mani /*
2395a918fb3cSRaja Mani  * FIXME: WOW suspend mode is selected if the host sdio controller supports
2396a918fb3cSRaja Mani  * both sdio irq wake up and keep power. The target pulls sdio data line to
2397a918fb3cSRaja Mani  * wake up the host when WOW pattern matches. This causes sdio irq handler
2398a918fb3cSRaja Mani  * is being called in the host side which internally hits ath6kl's RX path.
2399a918fb3cSRaja Mani  *
2400a918fb3cSRaja Mani  * Since sdio interrupt is not disabled, RX path executes even before
2401a918fb3cSRaja Mani  * the host executes the actual resume operation from PM module.
2402a918fb3cSRaja Mani  *
2403a918fb3cSRaja Mani  * In the current scenario, WOW resume should happen before start processing
2404a918fb3cSRaja Mani  * any data from the target. So It's required to perform WOW resume in RX path.
2405a918fb3cSRaja Mani  * Ideally we should perform WOW resume only in the actual platform
2406a918fb3cSRaja Mani  * resume path. This area needs bit rework to avoid WOW resume in RX path.
2407a918fb3cSRaja Mani  *
2408a918fb3cSRaja Mani  * ath6kl_check_wow_status() is called from ath6kl_rx().
2409a918fb3cSRaja Mani  */
2410a918fb3cSRaja Mani void ath6kl_check_wow_status(struct ath6kl *ar)
2411a918fb3cSRaja Mani {
2412390a8c8fSRaja Mani 	if (ar->state == ATH6KL_STATE_SUSPENDING)
2413390a8c8fSRaja Mani 		return;
2414390a8c8fSRaja Mani 
2415a918fb3cSRaja Mani 	if (ar->state == ATH6KL_STATE_WOW)
2416a918fb3cSRaja Mani 		ath6kl_cfg80211_resume(ar);
2417a918fb3cSRaja Mani }
2418a918fb3cSRaja Mani 
2419a918fb3cSRaja Mani #else
2420a918fb3cSRaja Mani 
2421a918fb3cSRaja Mani void ath6kl_check_wow_status(struct ath6kl *ar)
2422a918fb3cSRaja Mani {
2423a918fb3cSRaja Mani }
2424abcb344bSKalle Valo #endif
2425abcb344bSKalle Valo 
24266a7c9badSJouni Malinen static int ath6kl_set_channel(struct wiphy *wiphy, struct net_device *dev,
24276a7c9badSJouni Malinen 			      struct ieee80211_channel *chan,
24286a7c9badSJouni Malinen 			      enum nl80211_channel_type channel_type)
24296a7c9badSJouni Malinen {
2430e68f6750SSujith Manoharan 	struct ath6kl_vif *vif;
2431e68f6750SSujith Manoharan 
2432e68f6750SSujith Manoharan 	/*
2433e68f6750SSujith Manoharan 	 * 'dev' could be NULL if a channel change is required for the hardware
2434e68f6750SSujith Manoharan 	 * device itself, instead of a particular VIF.
2435e68f6750SSujith Manoharan 	 *
2436e68f6750SSujith Manoharan 	 * FIXME: To be handled properly when monitor mode is supported.
2437e68f6750SSujith Manoharan 	 */
2438e68f6750SSujith Manoharan 	if (!dev)
2439e68f6750SSujith Manoharan 		return -EBUSY;
2440e68f6750SSujith Manoharan 
2441e68f6750SSujith Manoharan 	vif = netdev_priv(dev);
24426a7c9badSJouni Malinen 
2443990bd915SVasanthakumar Thiagarajan 	if (!ath6kl_cfg80211_ready(vif))
24446a7c9badSJouni Malinen 		return -EIO;
24456a7c9badSJouni Malinen 
24466a7c9badSJouni Malinen 	ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: center_freq=%u hw_value=%u\n",
24476a7c9badSJouni Malinen 		   __func__, chan->center_freq, chan->hw_value);
2448cf5333d7SVasanthakumar Thiagarajan 	vif->next_chan = chan->center_freq;
24496a7c9badSJouni Malinen 
24506a7c9badSJouni Malinen 	return 0;
24516a7c9badSJouni Malinen }
24526a7c9badSJouni Malinen 
24538bdfbf40SJouni Malinen static bool ath6kl_is_p2p_ie(const u8 *pos)
24548bdfbf40SJouni Malinen {
24558bdfbf40SJouni Malinen 	return pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
24568bdfbf40SJouni Malinen 		pos[2] == 0x50 && pos[3] == 0x6f &&
24578bdfbf40SJouni Malinen 		pos[4] == 0x9a && pos[5] == 0x09;
24588bdfbf40SJouni Malinen }
24598bdfbf40SJouni Malinen 
2460334234b5SVasanthakumar Thiagarajan static int ath6kl_set_ap_probe_resp_ies(struct ath6kl_vif *vif,
2461334234b5SVasanthakumar Thiagarajan 					const u8 *ies, size_t ies_len)
24628bdfbf40SJouni Malinen {
2463334234b5SVasanthakumar Thiagarajan 	struct ath6kl *ar = vif->ar;
24648bdfbf40SJouni Malinen 	const u8 *pos;
24658bdfbf40SJouni Malinen 	u8 *buf = NULL;
24668bdfbf40SJouni Malinen 	size_t len = 0;
24678bdfbf40SJouni Malinen 	int ret;
24688bdfbf40SJouni Malinen 
24698bdfbf40SJouni Malinen 	/*
24708bdfbf40SJouni Malinen 	 * Filter out P2P IE(s) since they will be included depending on
24718bdfbf40SJouni Malinen 	 * the Probe Request frame in ath6kl_send_go_probe_resp().
24728bdfbf40SJouni Malinen 	 */
24738bdfbf40SJouni Malinen 
24748bdfbf40SJouni Malinen 	if (ies && ies_len) {
24758bdfbf40SJouni Malinen 		buf = kmalloc(ies_len, GFP_KERNEL);
24768bdfbf40SJouni Malinen 		if (buf == NULL)
24778bdfbf40SJouni Malinen 			return -ENOMEM;
24788bdfbf40SJouni Malinen 		pos = ies;
24798bdfbf40SJouni Malinen 		while (pos + 1 < ies + ies_len) {
24808bdfbf40SJouni Malinen 			if (pos + 2 + pos[1] > ies + ies_len)
24818bdfbf40SJouni Malinen 				break;
24828bdfbf40SJouni Malinen 			if (!ath6kl_is_p2p_ie(pos)) {
24838bdfbf40SJouni Malinen 				memcpy(buf + len, pos, 2 + pos[1]);
24848bdfbf40SJouni Malinen 				len += 2 + pos[1];
24858bdfbf40SJouni Malinen 			}
24868bdfbf40SJouni Malinen 			pos += 2 + pos[1];
24878bdfbf40SJouni Malinen 		}
24888bdfbf40SJouni Malinen 	}
24898bdfbf40SJouni Malinen 
2490334234b5SVasanthakumar Thiagarajan 	ret = ath6kl_wmi_set_appie_cmd(ar->wmi, vif->fw_vif_idx,
2491334234b5SVasanthakumar Thiagarajan 				       WMI_FRAME_PROBE_RESP, buf, len);
24928bdfbf40SJouni Malinen 	kfree(buf);
24938bdfbf40SJouni Malinen 	return ret;
24948bdfbf40SJouni Malinen }
24958bdfbf40SJouni Malinen 
24968860020eSJohannes Berg static int ath6kl_set_ies(struct ath6kl_vif *vif,
24978860020eSJohannes Berg 			  struct cfg80211_beacon_data *info)
24988860020eSJohannes Berg {
24998860020eSJohannes Berg 	struct ath6kl *ar = vif->ar;
25008860020eSJohannes Berg 	int res;
25018860020eSJohannes Berg 
250217a7b16dSAarthi Thiruvengadam 	/* this also clears IE in fw if it's not set */
25038860020eSJohannes Berg 	res = ath6kl_wmi_set_appie_cmd(ar->wmi, vif->fw_vif_idx,
25048860020eSJohannes Berg 				       WMI_FRAME_BEACON,
25058860020eSJohannes Berg 				       info->beacon_ies,
25068860020eSJohannes Berg 				       info->beacon_ies_len);
25078860020eSJohannes Berg 	if (res)
25088860020eSJohannes Berg 		return res;
25098860020eSJohannes Berg 
251017a7b16dSAarthi Thiruvengadam 	/* this also clears IE in fw if it's not set */
25118860020eSJohannes Berg 	res = ath6kl_set_ap_probe_resp_ies(vif, info->proberesp_ies,
25128860020eSJohannes Berg 					   info->proberesp_ies_len);
25138860020eSJohannes Berg 	if (res)
25148860020eSJohannes Berg 		return res;
25158860020eSJohannes Berg 
251617a7b16dSAarthi Thiruvengadam 	/* this also clears IE in fw if it's not set */
25178860020eSJohannes Berg 	res = ath6kl_wmi_set_appie_cmd(ar->wmi, vif->fw_vif_idx,
25188860020eSJohannes Berg 				       WMI_FRAME_ASSOC_RESP,
25198860020eSJohannes Berg 				       info->assocresp_ies,
25208860020eSJohannes Berg 				       info->assocresp_ies_len);
25218860020eSJohannes Berg 	if (res)
25228860020eSJohannes Berg 		return res;
25238860020eSJohannes Berg 
25248860020eSJohannes Berg 	return 0;
25258860020eSJohannes Berg }
25268860020eSJohannes Berg 
25278860020eSJohannes Berg static int ath6kl_start_ap(struct wiphy *wiphy, struct net_device *dev,
25288860020eSJohannes Berg 			   struct cfg80211_ap_settings *info)
25296a7c9badSJouni Malinen {
25306a7c9badSJouni Malinen 	struct ath6kl *ar = ath6kl_priv(dev);
25313450334fSVasanthakumar Thiagarajan 	struct ath6kl_vif *vif = netdev_priv(dev);
25326a7c9badSJouni Malinen 	struct ieee80211_mgmt *mgmt;
253367cd22e4SThomas Pedersen 	bool hidden = false;
25346a7c9badSJouni Malinen 	u8 *ies;
25356a7c9badSJouni Malinen 	int ies_len;
25366a7c9badSJouni Malinen 	struct wmi_connect_cmd p;
25376a7c9badSJouni Malinen 	int res;
2538be5abaafSVasanthakumar Thiagarajan 	int i, ret;
25396a7c9badSJouni Malinen 
25408860020eSJohannes Berg 	ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s:\n", __func__);
25416a7c9badSJouni Malinen 
2542990bd915SVasanthakumar Thiagarajan 	if (!ath6kl_cfg80211_ready(vif))
25436a7c9badSJouni Malinen 		return -EIO;
25446a7c9badSJouni Malinen 
2545f5938f24SVasanthakumar Thiagarajan 	if (vif->next_mode != AP_NETWORK)
25466a7c9badSJouni Malinen 		return -EOPNOTSUPP;
25476a7c9badSJouni Malinen 
25488860020eSJohannes Berg 	res = ath6kl_set_ies(vif, &info->beacon);
25496a7c9badSJouni Malinen 
25509a5b1318SJouni Malinen 	ar->ap_mode_bkey.valid = false;
25519a5b1318SJouni Malinen 
25526a7c9badSJouni Malinen 	/* TODO:
25536a7c9badSJouni Malinen 	 * info->interval
25546a7c9badSJouni Malinen 	 * info->dtim_period
25556a7c9badSJouni Malinen 	 */
25566a7c9badSJouni Malinen 
25578860020eSJohannes Berg 	if (info->beacon.head == NULL)
25586a7c9badSJouni Malinen 		return -EINVAL;
25598860020eSJohannes Berg 	mgmt = (struct ieee80211_mgmt *) info->beacon.head;
25606a7c9badSJouni Malinen 	ies = mgmt->u.beacon.variable;
25618860020eSJohannes Berg 	if (ies > info->beacon.head + info->beacon.head_len)
25626a7c9badSJouni Malinen 		return -EINVAL;
25638860020eSJohannes Berg 	ies_len = info->beacon.head + info->beacon.head_len - ies;
25646a7c9badSJouni Malinen 
25656a7c9badSJouni Malinen 	if (info->ssid == NULL)
25666a7c9badSJouni Malinen 		return -EINVAL;
25673450334fSVasanthakumar Thiagarajan 	memcpy(vif->ssid, info->ssid, info->ssid_len);
25683450334fSVasanthakumar Thiagarajan 	vif->ssid_len = info->ssid_len;
25696a7c9badSJouni Malinen 	if (info->hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE)
257067cd22e4SThomas Pedersen 		hidden = true;
257167cd22e4SThomas Pedersen 
257267cd22e4SThomas Pedersen 	res = ath6kl_wmi_ap_hidden_ssid(ar->wmi, vif->fw_vif_idx, hidden);
257367cd22e4SThomas Pedersen 	if (res)
257467cd22e4SThomas Pedersen 		return res;
25756a7c9badSJouni Malinen 
2576be5abaafSVasanthakumar Thiagarajan 	ret = ath6kl_set_auth_type(vif, info->auth_type);
2577be5abaafSVasanthakumar Thiagarajan 	if (ret)
2578be5abaafSVasanthakumar Thiagarajan 		return ret;
25796a7c9badSJouni Malinen 
25806a7c9badSJouni Malinen 	memset(&p, 0, sizeof(p));
25816a7c9badSJouni Malinen 
25826a7c9badSJouni Malinen 	for (i = 0; i < info->crypto.n_akm_suites; i++) {
25836a7c9badSJouni Malinen 		switch (info->crypto.akm_suites[i]) {
25846a7c9badSJouni Malinen 		case WLAN_AKM_SUITE_8021X:
25856a7c9badSJouni Malinen 			if (info->crypto.wpa_versions & NL80211_WPA_VERSION_1)
25866a7c9badSJouni Malinen 				p.auth_mode |= WPA_AUTH;
25876a7c9badSJouni Malinen 			if (info->crypto.wpa_versions & NL80211_WPA_VERSION_2)
25886a7c9badSJouni Malinen 				p.auth_mode |= WPA2_AUTH;
25896a7c9badSJouni Malinen 			break;
25906a7c9badSJouni Malinen 		case WLAN_AKM_SUITE_PSK:
25916a7c9badSJouni Malinen 			if (info->crypto.wpa_versions & NL80211_WPA_VERSION_1)
25926a7c9badSJouni Malinen 				p.auth_mode |= WPA_PSK_AUTH;
25936a7c9badSJouni Malinen 			if (info->crypto.wpa_versions & NL80211_WPA_VERSION_2)
25946a7c9badSJouni Malinen 				p.auth_mode |= WPA2_PSK_AUTH;
25956a7c9badSJouni Malinen 			break;
25966a7c9badSJouni Malinen 		}
25976a7c9badSJouni Malinen 	}
25986a7c9badSJouni Malinen 	if (p.auth_mode == 0)
25996a7c9badSJouni Malinen 		p.auth_mode = NONE_AUTH;
26003450334fSVasanthakumar Thiagarajan 	vif->auth_mode = p.auth_mode;
26016a7c9badSJouni Malinen 
26026a7c9badSJouni Malinen 	for (i = 0; i < info->crypto.n_ciphers_pairwise; i++) {
26036a7c9badSJouni Malinen 		switch (info->crypto.ciphers_pairwise[i]) {
26046a7c9badSJouni Malinen 		case WLAN_CIPHER_SUITE_WEP40:
26056a7c9badSJouni Malinen 		case WLAN_CIPHER_SUITE_WEP104:
26066a7c9badSJouni Malinen 			p.prwise_crypto_type |= WEP_CRYPT;
26076a7c9badSJouni Malinen 			break;
26086a7c9badSJouni Malinen 		case WLAN_CIPHER_SUITE_TKIP:
26096a7c9badSJouni Malinen 			p.prwise_crypto_type |= TKIP_CRYPT;
26106a7c9badSJouni Malinen 			break;
26116a7c9badSJouni Malinen 		case WLAN_CIPHER_SUITE_CCMP:
26126a7c9badSJouni Malinen 			p.prwise_crypto_type |= AES_CRYPT;
26136a7c9badSJouni Malinen 			break;
2614b8214df1SDai Shuibing 		case WLAN_CIPHER_SUITE_SMS4:
2615b8214df1SDai Shuibing 			p.prwise_crypto_type |= WAPI_CRYPT;
2616b8214df1SDai Shuibing 			break;
26176a7c9badSJouni Malinen 		}
26186a7c9badSJouni Malinen 	}
2619229ed6b5SEdward Lu 	if (p.prwise_crypto_type == 0) {
26206a7c9badSJouni Malinen 		p.prwise_crypto_type = NONE_CRYPT;
2621240d2799SVasanthakumar Thiagarajan 		ath6kl_set_cipher(vif, 0, true);
2622229ed6b5SEdward Lu 	} else if (info->crypto.n_ciphers_pairwise == 1)
2623240d2799SVasanthakumar Thiagarajan 		ath6kl_set_cipher(vif, info->crypto.ciphers_pairwise[0], true);
26246a7c9badSJouni Malinen 
26256a7c9badSJouni Malinen 	switch (info->crypto.cipher_group) {
26266a7c9badSJouni Malinen 	case WLAN_CIPHER_SUITE_WEP40:
26276a7c9badSJouni Malinen 	case WLAN_CIPHER_SUITE_WEP104:
26286a7c9badSJouni Malinen 		p.grp_crypto_type = WEP_CRYPT;
26296a7c9badSJouni Malinen 		break;
26306a7c9badSJouni Malinen 	case WLAN_CIPHER_SUITE_TKIP:
26316a7c9badSJouni Malinen 		p.grp_crypto_type = TKIP_CRYPT;
26326a7c9badSJouni Malinen 		break;
26336a7c9badSJouni Malinen 	case WLAN_CIPHER_SUITE_CCMP:
26346a7c9badSJouni Malinen 		p.grp_crypto_type = AES_CRYPT;
26356a7c9badSJouni Malinen 		break;
2636b8214df1SDai Shuibing 	case WLAN_CIPHER_SUITE_SMS4:
2637b8214df1SDai Shuibing 		p.grp_crypto_type = WAPI_CRYPT;
2638b8214df1SDai Shuibing 		break;
26396a7c9badSJouni Malinen 	default:
26406a7c9badSJouni Malinen 		p.grp_crypto_type = NONE_CRYPT;
26416a7c9badSJouni Malinen 		break;
26426a7c9badSJouni Malinen 	}
2643240d2799SVasanthakumar Thiagarajan 	ath6kl_set_cipher(vif, info->crypto.cipher_group, false);
26446a7c9badSJouni Malinen 
26456a7c9badSJouni Malinen 	p.nw_type = AP_NETWORK;
2646f5938f24SVasanthakumar Thiagarajan 	vif->nw_type = vif->next_mode;
26476a7c9badSJouni Malinen 
26483450334fSVasanthakumar Thiagarajan 	p.ssid_len = vif->ssid_len;
26493450334fSVasanthakumar Thiagarajan 	memcpy(p.ssid, vif->ssid, vif->ssid_len);
26503450334fSVasanthakumar Thiagarajan 	p.dot11_auth_mode = vif->dot11_auth_mode;
2651cf5333d7SVasanthakumar Thiagarajan 	p.ch = cpu_to_le16(vif->next_chan);
26526a7c9badSJouni Malinen 
2653c1762a3fSThirumalai Pachamuthu 	/* Enable uAPSD support by default */
2654c1762a3fSThirumalai Pachamuthu 	res = ath6kl_wmi_ap_set_apsd(ar->wmi, vif->fw_vif_idx, true);
2655c1762a3fSThirumalai Pachamuthu 	if (res < 0)
2656c1762a3fSThirumalai Pachamuthu 		return res;
2657c1762a3fSThirumalai Pachamuthu 
26583ca9d1fcSAarthi Thiruvengadam 	if (vif->wdev.iftype == NL80211_IFTYPE_P2P_GO) {
26593ca9d1fcSAarthi Thiruvengadam 		p.nw_subtype = SUBTYPE_P2PGO;
26603ca9d1fcSAarthi Thiruvengadam 	} else {
26613ca9d1fcSAarthi Thiruvengadam 		/*
26623ca9d1fcSAarthi Thiruvengadam 		 * Due to firmware limitation, it is not possible to
26633ca9d1fcSAarthi Thiruvengadam 		 * do P2P mgmt operations in AP mode
26643ca9d1fcSAarthi Thiruvengadam 		 */
26653ca9d1fcSAarthi Thiruvengadam 		p.nw_subtype = SUBTYPE_NONE;
26663ca9d1fcSAarthi Thiruvengadam 	}
26673ca9d1fcSAarthi Thiruvengadam 
266803bdeb0dSVasanthakumar Thiagarajan 	if (info->inactivity_timeout) {
266903bdeb0dSVasanthakumar Thiagarajan 		res = ath6kl_wmi_set_inact_period(ar->wmi, vif->fw_vif_idx,
267003bdeb0dSVasanthakumar Thiagarajan 						  info->inactivity_timeout);
267103bdeb0dSVasanthakumar Thiagarajan 		if (res < 0)
267203bdeb0dSVasanthakumar Thiagarajan 			return res;
267303bdeb0dSVasanthakumar Thiagarajan 	}
267403bdeb0dSVasanthakumar Thiagarajan 
2675334234b5SVasanthakumar Thiagarajan 	res = ath6kl_wmi_ap_profile_commit(ar->wmi, vif->fw_vif_idx, &p);
26769a5b1318SJouni Malinen 	if (res < 0)
26779a5b1318SJouni Malinen 		return res;
26789a5b1318SJouni Malinen 
26799a5b1318SJouni Malinen 	return 0;
26806a7c9badSJouni Malinen }
26816a7c9badSJouni Malinen 
26828860020eSJohannes Berg static int ath6kl_change_beacon(struct wiphy *wiphy, struct net_device *dev,
26838860020eSJohannes Berg 				struct cfg80211_beacon_data *beacon)
26846a7c9badSJouni Malinen {
26858860020eSJohannes Berg 	struct ath6kl_vif *vif = netdev_priv(dev);
26868860020eSJohannes Berg 
26878860020eSJohannes Berg 	if (!ath6kl_cfg80211_ready(vif))
26888860020eSJohannes Berg 		return -EIO;
26898860020eSJohannes Berg 
26908860020eSJohannes Berg 	if (vif->next_mode != AP_NETWORK)
26918860020eSJohannes Berg 		return -EOPNOTSUPP;
26928860020eSJohannes Berg 
26938860020eSJohannes Berg 	return ath6kl_set_ies(vif, beacon);
26946a7c9badSJouni Malinen }
26956a7c9badSJouni Malinen 
26968860020eSJohannes Berg static int ath6kl_stop_ap(struct wiphy *wiphy, struct net_device *dev)
26976a7c9badSJouni Malinen {
26986a7c9badSJouni Malinen 	struct ath6kl *ar = ath6kl_priv(dev);
269959c98449SVasanthakumar Thiagarajan 	struct ath6kl_vif *vif = netdev_priv(dev);
27006a7c9badSJouni Malinen 
2701f5938f24SVasanthakumar Thiagarajan 	if (vif->nw_type != AP_NETWORK)
27026a7c9badSJouni Malinen 		return -EOPNOTSUPP;
270359c98449SVasanthakumar Thiagarajan 	if (!test_bit(CONNECTED, &vif->flags))
27046a7c9badSJouni Malinen 		return -ENOTCONN;
27056a7c9badSJouni Malinen 
2706334234b5SVasanthakumar Thiagarajan 	ath6kl_wmi_disconnect_cmd(ar->wmi, vif->fw_vif_idx);
270759c98449SVasanthakumar Thiagarajan 	clear_bit(CONNECTED, &vif->flags);
27086a7c9badSJouni Malinen 
27096a7c9badSJouni Malinen 	return 0;
27106a7c9badSJouni Malinen }
27116a7c9badSJouni Malinen 
271233e5308dSJouni Malinen static const u8 bcast_addr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
271333e5308dSJouni Malinen 
271433e5308dSJouni Malinen static int ath6kl_del_station(struct wiphy *wiphy, struct net_device *dev,
271533e5308dSJouni Malinen 			      u8 *mac)
271633e5308dSJouni Malinen {
271733e5308dSJouni Malinen 	struct ath6kl *ar = ath6kl_priv(dev);
271833e5308dSJouni Malinen 	struct ath6kl_vif *vif = netdev_priv(dev);
271933e5308dSJouni Malinen 	const u8 *addr = mac ? mac : bcast_addr;
272033e5308dSJouni Malinen 
272133e5308dSJouni Malinen 	return ath6kl_wmi_ap_set_mlme(ar->wmi, vif->fw_vif_idx, WMI_AP_DEAUTH,
272233e5308dSJouni Malinen 				      addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
272333e5308dSJouni Malinen }
272433e5308dSJouni Malinen 
272523875136SJouni Malinen static int ath6kl_change_station(struct wiphy *wiphy, struct net_device *dev,
272623875136SJouni Malinen 				 u8 *mac, struct station_parameters *params)
272723875136SJouni Malinen {
272823875136SJouni Malinen 	struct ath6kl *ar = ath6kl_priv(dev);
2729f5938f24SVasanthakumar Thiagarajan 	struct ath6kl_vif *vif = netdev_priv(dev);
273023875136SJouni Malinen 
2731f5938f24SVasanthakumar Thiagarajan 	if (vif->nw_type != AP_NETWORK)
273223875136SJouni Malinen 		return -EOPNOTSUPP;
273323875136SJouni Malinen 
273423875136SJouni Malinen 	/* Use this only for authorizing/unauthorizing a station */
273523875136SJouni Malinen 	if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
273623875136SJouni Malinen 		return -EOPNOTSUPP;
273723875136SJouni Malinen 
273823875136SJouni Malinen 	if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED))
2739334234b5SVasanthakumar Thiagarajan 		return ath6kl_wmi_ap_set_mlme(ar->wmi, vif->fw_vif_idx,
2740334234b5SVasanthakumar Thiagarajan 					      WMI_AP_MLME_AUTHORIZE, mac, 0);
2741334234b5SVasanthakumar Thiagarajan 	return ath6kl_wmi_ap_set_mlme(ar->wmi, vif->fw_vif_idx,
2742334234b5SVasanthakumar Thiagarajan 				      WMI_AP_MLME_UNAUTHORIZE, mac, 0);
274323875136SJouni Malinen }
274423875136SJouni Malinen 
274563fa1e0cSJouni Malinen static int ath6kl_remain_on_channel(struct wiphy *wiphy,
274663fa1e0cSJouni Malinen 				    struct net_device *dev,
274763fa1e0cSJouni Malinen 				    struct ieee80211_channel *chan,
274863fa1e0cSJouni Malinen 				    enum nl80211_channel_type channel_type,
274963fa1e0cSJouni Malinen 				    unsigned int duration,
275063fa1e0cSJouni Malinen 				    u64 *cookie)
275163fa1e0cSJouni Malinen {
275263fa1e0cSJouni Malinen 	struct ath6kl *ar = ath6kl_priv(dev);
2753334234b5SVasanthakumar Thiagarajan 	struct ath6kl_vif *vif = netdev_priv(dev);
27541052261eSJouni Malinen 	u32 id;
275563fa1e0cSJouni Malinen 
275663fa1e0cSJouni Malinen 	/* TODO: if already pending or ongoing remain-on-channel,
275763fa1e0cSJouni Malinen 	 * return -EBUSY */
27581052261eSJouni Malinen 	id = ++vif->last_roc_id;
27591052261eSJouni Malinen 	if (id == 0) {
27601052261eSJouni Malinen 		/* Do not use 0 as the cookie value */
27611052261eSJouni Malinen 		id = ++vif->last_roc_id;
27621052261eSJouni Malinen 	}
27631052261eSJouni Malinen 	*cookie = id;
276463fa1e0cSJouni Malinen 
2765334234b5SVasanthakumar Thiagarajan 	return ath6kl_wmi_remain_on_chnl_cmd(ar->wmi, vif->fw_vif_idx,
2766334234b5SVasanthakumar Thiagarajan 					     chan->center_freq, duration);
276763fa1e0cSJouni Malinen }
276863fa1e0cSJouni Malinen 
276963fa1e0cSJouni Malinen static int ath6kl_cancel_remain_on_channel(struct wiphy *wiphy,
277063fa1e0cSJouni Malinen 					   struct net_device *dev,
277163fa1e0cSJouni Malinen 					   u64 cookie)
277263fa1e0cSJouni Malinen {
277363fa1e0cSJouni Malinen 	struct ath6kl *ar = ath6kl_priv(dev);
2774334234b5SVasanthakumar Thiagarajan 	struct ath6kl_vif *vif = netdev_priv(dev);
277563fa1e0cSJouni Malinen 
27761052261eSJouni Malinen 	if (cookie != vif->last_roc_id)
277763fa1e0cSJouni Malinen 		return -ENOENT;
27781052261eSJouni Malinen 	vif->last_cancel_roc_id = cookie;
277963fa1e0cSJouni Malinen 
2780334234b5SVasanthakumar Thiagarajan 	return ath6kl_wmi_cancel_remain_on_chnl_cmd(ar->wmi, vif->fw_vif_idx);
278163fa1e0cSJouni Malinen }
278263fa1e0cSJouni Malinen 
2783334234b5SVasanthakumar Thiagarajan static int ath6kl_send_go_probe_resp(struct ath6kl_vif *vif,
2784334234b5SVasanthakumar Thiagarajan 				     const u8 *buf, size_t len,
2785334234b5SVasanthakumar Thiagarajan 				     unsigned int freq)
27868bdfbf40SJouni Malinen {
2787334234b5SVasanthakumar Thiagarajan 	struct ath6kl *ar = vif->ar;
27888bdfbf40SJouni Malinen 	const u8 *pos;
27898bdfbf40SJouni Malinen 	u8 *p2p;
27908bdfbf40SJouni Malinen 	int p2p_len;
27918bdfbf40SJouni Malinen 	int ret;
27928bdfbf40SJouni Malinen 	const struct ieee80211_mgmt *mgmt;
27938bdfbf40SJouni Malinen 
27948bdfbf40SJouni Malinen 	mgmt = (const struct ieee80211_mgmt *) buf;
27958bdfbf40SJouni Malinen 
27968bdfbf40SJouni Malinen 	/* Include P2P IE(s) from the frame generated in user space. */
27978bdfbf40SJouni Malinen 
27988bdfbf40SJouni Malinen 	p2p = kmalloc(len, GFP_KERNEL);
27998bdfbf40SJouni Malinen 	if (p2p == NULL)
28008bdfbf40SJouni Malinen 		return -ENOMEM;
28018bdfbf40SJouni Malinen 	p2p_len = 0;
28028bdfbf40SJouni Malinen 
28038bdfbf40SJouni Malinen 	pos = mgmt->u.probe_resp.variable;
28048bdfbf40SJouni Malinen 	while (pos + 1 < buf + len) {
28058bdfbf40SJouni Malinen 		if (pos + 2 + pos[1] > buf + len)
28068bdfbf40SJouni Malinen 			break;
28078bdfbf40SJouni Malinen 		if (ath6kl_is_p2p_ie(pos)) {
28088bdfbf40SJouni Malinen 			memcpy(p2p + p2p_len, pos, 2 + pos[1]);
28098bdfbf40SJouni Malinen 			p2p_len += 2 + pos[1];
28108bdfbf40SJouni Malinen 		}
28118bdfbf40SJouni Malinen 		pos += 2 + pos[1];
28128bdfbf40SJouni Malinen 	}
28138bdfbf40SJouni Malinen 
2814334234b5SVasanthakumar Thiagarajan 	ret = ath6kl_wmi_send_probe_response_cmd(ar->wmi, vif->fw_vif_idx, freq,
2815334234b5SVasanthakumar Thiagarajan 						 mgmt->da, p2p, p2p_len);
28168bdfbf40SJouni Malinen 	kfree(p2p);
28178bdfbf40SJouni Malinen 	return ret;
28188bdfbf40SJouni Malinen }
28198bdfbf40SJouni Malinen 
2820d0ff7383SNaveen Gangadharan static bool ath6kl_mgmt_powersave_ap(struct ath6kl_vif *vif,
2821d0ff7383SNaveen Gangadharan 				     u32 id,
2822d0ff7383SNaveen Gangadharan 				     u32 freq,
2823d0ff7383SNaveen Gangadharan 				     u32 wait,
2824d0ff7383SNaveen Gangadharan 				     const u8 *buf,
2825d0ff7383SNaveen Gangadharan 				     size_t len,
2826d0ff7383SNaveen Gangadharan 				     bool *more_data,
2827d0ff7383SNaveen Gangadharan 				     bool no_cck)
2828d0ff7383SNaveen Gangadharan {
2829d0ff7383SNaveen Gangadharan 	struct ieee80211_mgmt *mgmt;
2830d0ff7383SNaveen Gangadharan 	struct ath6kl_sta *conn;
2831d0ff7383SNaveen Gangadharan 	bool is_psq_empty = false;
2832d0ff7383SNaveen Gangadharan 	struct ath6kl_mgmt_buff *mgmt_buf;
2833d0ff7383SNaveen Gangadharan 	size_t mgmt_buf_size;
2834d0ff7383SNaveen Gangadharan 	struct ath6kl *ar = vif->ar;
2835d0ff7383SNaveen Gangadharan 
2836d0ff7383SNaveen Gangadharan 	mgmt = (struct ieee80211_mgmt *) buf;
2837d0ff7383SNaveen Gangadharan 	if (is_multicast_ether_addr(mgmt->da))
2838d0ff7383SNaveen Gangadharan 		return false;
2839d0ff7383SNaveen Gangadharan 
2840d0ff7383SNaveen Gangadharan 	conn = ath6kl_find_sta(vif, mgmt->da);
2841d0ff7383SNaveen Gangadharan 	if (!conn)
2842d0ff7383SNaveen Gangadharan 		return false;
2843d0ff7383SNaveen Gangadharan 
2844d0ff7383SNaveen Gangadharan 	if (conn->sta_flags & STA_PS_SLEEP) {
2845d0ff7383SNaveen Gangadharan 		if (!(conn->sta_flags & STA_PS_POLLED)) {
2846d0ff7383SNaveen Gangadharan 			/* Queue the frames if the STA is sleeping */
2847d0ff7383SNaveen Gangadharan 			mgmt_buf_size = len + sizeof(struct ath6kl_mgmt_buff);
2848d0ff7383SNaveen Gangadharan 			mgmt_buf = kmalloc(mgmt_buf_size, GFP_KERNEL);
2849d0ff7383SNaveen Gangadharan 			if (!mgmt_buf)
2850d0ff7383SNaveen Gangadharan 				return false;
2851d0ff7383SNaveen Gangadharan 
2852d0ff7383SNaveen Gangadharan 			INIT_LIST_HEAD(&mgmt_buf->list);
2853d0ff7383SNaveen Gangadharan 			mgmt_buf->id = id;
2854d0ff7383SNaveen Gangadharan 			mgmt_buf->freq = freq;
2855d0ff7383SNaveen Gangadharan 			mgmt_buf->wait = wait;
2856d0ff7383SNaveen Gangadharan 			mgmt_buf->len = len;
2857d0ff7383SNaveen Gangadharan 			mgmt_buf->no_cck = no_cck;
2858d0ff7383SNaveen Gangadharan 			memcpy(mgmt_buf->buf, buf, len);
2859d0ff7383SNaveen Gangadharan 			spin_lock_bh(&conn->psq_lock);
2860d0ff7383SNaveen Gangadharan 			is_psq_empty = skb_queue_empty(&conn->psq) &&
2861d0ff7383SNaveen Gangadharan 					(conn->mgmt_psq_len == 0);
2862d0ff7383SNaveen Gangadharan 			list_add_tail(&mgmt_buf->list, &conn->mgmt_psq);
2863d0ff7383SNaveen Gangadharan 			conn->mgmt_psq_len++;
2864d0ff7383SNaveen Gangadharan 			spin_unlock_bh(&conn->psq_lock);
2865d0ff7383SNaveen Gangadharan 
2866d0ff7383SNaveen Gangadharan 			/*
2867d0ff7383SNaveen Gangadharan 			 * If this is the first pkt getting queued
2868d0ff7383SNaveen Gangadharan 			 * for this STA, update the PVB for this
2869d0ff7383SNaveen Gangadharan 			 * STA.
2870d0ff7383SNaveen Gangadharan 			 */
2871d0ff7383SNaveen Gangadharan 			if (is_psq_empty)
2872d0ff7383SNaveen Gangadharan 				ath6kl_wmi_set_pvb_cmd(ar->wmi, vif->fw_vif_idx,
2873d0ff7383SNaveen Gangadharan 						       conn->aid, 1);
2874d0ff7383SNaveen Gangadharan 			return true;
2875d0ff7383SNaveen Gangadharan 		}
2876d0ff7383SNaveen Gangadharan 
2877d0ff7383SNaveen Gangadharan 		/*
2878d0ff7383SNaveen Gangadharan 		 * This tx is because of a PsPoll.
2879d0ff7383SNaveen Gangadharan 		 * Determine if MoreData bit has to be set.
2880d0ff7383SNaveen Gangadharan 		 */
2881d0ff7383SNaveen Gangadharan 		spin_lock_bh(&conn->psq_lock);
2882d0ff7383SNaveen Gangadharan 		if (!skb_queue_empty(&conn->psq) || (conn->mgmt_psq_len != 0))
2883d0ff7383SNaveen Gangadharan 			*more_data = true;
2884d0ff7383SNaveen Gangadharan 		spin_unlock_bh(&conn->psq_lock);
2885d0ff7383SNaveen Gangadharan 	}
2886d0ff7383SNaveen Gangadharan 
2887d0ff7383SNaveen Gangadharan 	return false;
2888d0ff7383SNaveen Gangadharan }
2889d0ff7383SNaveen Gangadharan 
2890c86e4f44SAarthi Thiruvengadam /* Check if SSID length is greater than DIRECT- */
2891c86e4f44SAarthi Thiruvengadam static bool ath6kl_is_p2p_go_ssid(const u8 *buf, size_t len)
2892c86e4f44SAarthi Thiruvengadam {
2893c86e4f44SAarthi Thiruvengadam 	const struct ieee80211_mgmt *mgmt;
2894c86e4f44SAarthi Thiruvengadam 	mgmt = (const struct ieee80211_mgmt *) buf;
2895c86e4f44SAarthi Thiruvengadam 
2896c86e4f44SAarthi Thiruvengadam 	/* variable[1] contains the SSID tag length */
2897c86e4f44SAarthi Thiruvengadam 	if (buf + len >= &mgmt->u.probe_resp.variable[1] &&
2898c86e4f44SAarthi Thiruvengadam 	    (mgmt->u.probe_resp.variable[1] > P2P_WILDCARD_SSID_LEN)) {
2899c86e4f44SAarthi Thiruvengadam 		return true;
2900c86e4f44SAarthi Thiruvengadam 	}
2901c86e4f44SAarthi Thiruvengadam 
2902c86e4f44SAarthi Thiruvengadam 	return false;
2903c86e4f44SAarthi Thiruvengadam }
2904c86e4f44SAarthi Thiruvengadam 
29058a6c8060SJouni Malinen static int ath6kl_mgmt_tx(struct wiphy *wiphy, struct net_device *dev,
29068a6c8060SJouni Malinen 			  struct ieee80211_channel *chan, bool offchan,
29078a6c8060SJouni Malinen 			  enum nl80211_channel_type channel_type,
29088a6c8060SJouni Malinen 			  bool channel_type_valid, unsigned int wait,
2909e247bd90SJohannes Berg 			  const u8 *buf, size_t len, bool no_cck,
2910e247bd90SJohannes Berg 			  bool dont_wait_for_ack, u64 *cookie)
29118a6c8060SJouni Malinen {
29128a6c8060SJouni Malinen 	struct ath6kl *ar = ath6kl_priv(dev);
291359c98449SVasanthakumar Thiagarajan 	struct ath6kl_vif *vif = netdev_priv(dev);
29148a6c8060SJouni Malinen 	u32 id;
29158bdfbf40SJouni Malinen 	const struct ieee80211_mgmt *mgmt;
2916d0ff7383SNaveen Gangadharan 	bool more_data, queued;
29178bdfbf40SJouni Malinen 
29188bdfbf40SJouni Malinen 	mgmt = (const struct ieee80211_mgmt *) buf;
2919c86e4f44SAarthi Thiruvengadam 	if (vif->nw_type == AP_NETWORK && test_bit(CONNECTED, &vif->flags) &&
2920c86e4f44SAarthi Thiruvengadam 	    ieee80211_is_probe_resp(mgmt->frame_control) &&
2921c86e4f44SAarthi Thiruvengadam 	    ath6kl_is_p2p_go_ssid(buf, len)) {
29228bdfbf40SJouni Malinen 		/*
2923c86e4f44SAarthi Thiruvengadam 		 * Send Probe Response frame in GO mode using a separate WMI
29248bdfbf40SJouni Malinen 		 * command to allow the target to fill in the generic IEs.
29258bdfbf40SJouni Malinen 		 */
29268bdfbf40SJouni Malinen 		*cookie = 0; /* TX status not supported */
2927334234b5SVasanthakumar Thiagarajan 		return ath6kl_send_go_probe_resp(vif, buf, len,
29288bdfbf40SJouni Malinen 						 chan->center_freq);
29298bdfbf40SJouni Malinen 	}
29308a6c8060SJouni Malinen 
2931cf5333d7SVasanthakumar Thiagarajan 	id = vif->send_action_id++;
29328a6c8060SJouni Malinen 	if (id == 0) {
29338a6c8060SJouni Malinen 		/*
29348a6c8060SJouni Malinen 		 * 0 is a reserved value in the WMI command and shall not be
29358a6c8060SJouni Malinen 		 * used for the command.
29368a6c8060SJouni Malinen 		 */
2937cf5333d7SVasanthakumar Thiagarajan 		id = vif->send_action_id++;
29388a6c8060SJouni Malinen 	}
29398a6c8060SJouni Malinen 
29408a6c8060SJouni Malinen 	*cookie = id;
29413ca9d1fcSAarthi Thiruvengadam 
2942d0ff7383SNaveen Gangadharan 	/* AP mode Power saving processing */
2943d0ff7383SNaveen Gangadharan 	if (vif->nw_type == AP_NETWORK) {
2944d0ff7383SNaveen Gangadharan 		queued = ath6kl_mgmt_powersave_ap(vif,
2945d0ff7383SNaveen Gangadharan 					id, chan->center_freq,
2946d0ff7383SNaveen Gangadharan 					wait, buf,
2947d0ff7383SNaveen Gangadharan 					len, &more_data, no_cck);
2948d0ff7383SNaveen Gangadharan 		if (queued)
2949d0ff7383SNaveen Gangadharan 			return 0;
2950d0ff7383SNaveen Gangadharan 	}
2951d0ff7383SNaveen Gangadharan 
29523ca9d1fcSAarthi Thiruvengadam 	return ath6kl_wmi_send_mgmt_cmd(ar->wmi, vif->fw_vif_idx, id,
29533ca9d1fcSAarthi Thiruvengadam 					chan->center_freq, wait,
29543ca9d1fcSAarthi Thiruvengadam 					buf, len, no_cck);
29553ca9d1fcSAarthi Thiruvengadam }
29568a6c8060SJouni Malinen 
2957ae32c30aSJouni Malinen static void ath6kl_mgmt_frame_register(struct wiphy *wiphy,
2958ae32c30aSJouni Malinen 				       struct net_device *dev,
2959ae32c30aSJouni Malinen 				       u16 frame_type, bool reg)
2960ae32c30aSJouni Malinen {
2961cf5333d7SVasanthakumar Thiagarajan 	struct ath6kl_vif *vif = netdev_priv(dev);
2962ae32c30aSJouni Malinen 
2963ae32c30aSJouni Malinen 	ath6kl_dbg(ATH6KL_DBG_WLAN_CFG, "%s: frame_type=0x%x reg=%d\n",
2964ae32c30aSJouni Malinen 		   __func__, frame_type, reg);
2965ae32c30aSJouni Malinen 	if (frame_type == IEEE80211_STYPE_PROBE_REQ) {
2966ae32c30aSJouni Malinen 		/*
2967ae32c30aSJouni Malinen 		 * Note: This notification callback is not allowed to sleep, so
2968ae32c30aSJouni Malinen 		 * we cannot send WMI_PROBE_REQ_REPORT_CMD here. Instead, we
2969ae32c30aSJouni Malinen 		 * hardcode target to report Probe Request frames all the time.
2970ae32c30aSJouni Malinen 		 */
2971cf5333d7SVasanthakumar Thiagarajan 		vif->probe_req_report = reg;
2972ae32c30aSJouni Malinen 	}
2973ae32c30aSJouni Malinen }
2974ae32c30aSJouni Malinen 
297510509f90SKalle Valo static int ath6kl_cfg80211_sscan_start(struct wiphy *wiphy,
297610509f90SKalle Valo 			struct net_device *dev,
297710509f90SKalle Valo 			struct cfg80211_sched_scan_request *request)
297810509f90SKalle Valo {
297910509f90SKalle Valo 	struct ath6kl *ar = ath6kl_priv(dev);
298010509f90SKalle Valo 	struct ath6kl_vif *vif = netdev_priv(dev);
298110509f90SKalle Valo 	u16 interval;
298210509f90SKalle Valo 	int ret;
298310509f90SKalle Valo 	u8 i;
298410509f90SKalle Valo 
298510509f90SKalle Valo 	if (ar->state != ATH6KL_STATE_ON)
298610509f90SKalle Valo 		return -EIO;
298710509f90SKalle Valo 
298810509f90SKalle Valo 	if (vif->sme_state != SME_DISCONNECTED)
298910509f90SKalle Valo 		return -EBUSY;
299010509f90SKalle Valo 
2991b4d13d3bSKalle Valo 	ath6kl_cfg80211_scan_complete_event(vif, true);
2992b4d13d3bSKalle Valo 
299310509f90SKalle Valo 	for (i = 0; i < ar->wiphy->max_sched_scan_ssids; i++) {
299410509f90SKalle Valo 		ath6kl_wmi_probedssid_cmd(ar->wmi, vif->fw_vif_idx,
299510509f90SKalle Valo 					  i, DISABLE_SSID_FLAG,
299610509f90SKalle Valo 					  0, NULL);
299710509f90SKalle Valo 	}
299810509f90SKalle Valo 
299910509f90SKalle Valo 	/* fw uses seconds, also make sure that it's >0 */
300010509f90SKalle Valo 	interval = max_t(u16, 1, request->interval / 1000);
300110509f90SKalle Valo 
300210509f90SKalle Valo 	ath6kl_wmi_scanparams_cmd(ar->wmi, vif->fw_vif_idx,
300310509f90SKalle Valo 				  interval, interval,
300410509f90SKalle Valo 				  10, 0, 0, 0, 3, 0, 0, 0);
300510509f90SKalle Valo 
300610509f90SKalle Valo 	if (request->n_ssids && request->ssids[0].ssid_len) {
300710509f90SKalle Valo 		for (i = 0; i < request->n_ssids; i++) {
300810509f90SKalle Valo 			ath6kl_wmi_probedssid_cmd(ar->wmi, vif->fw_vif_idx,
300910509f90SKalle Valo 						  i, SPECIFIC_SSID_FLAG,
301010509f90SKalle Valo 						  request->ssids[i].ssid_len,
301110509f90SKalle Valo 						  request->ssids[i].ssid);
301210509f90SKalle Valo 		}
301310509f90SKalle Valo 	}
301410509f90SKalle Valo 
301510509f90SKalle Valo 	ret = ath6kl_wmi_set_wow_mode_cmd(ar->wmi, vif->fw_vif_idx,
301610509f90SKalle Valo 					  ATH6KL_WOW_MODE_ENABLE,
301710509f90SKalle Valo 					  WOW_FILTER_SSID,
301810509f90SKalle Valo 					  WOW_HOST_REQ_DELAY);
301910509f90SKalle Valo 	if (ret) {
302010509f90SKalle Valo 		ath6kl_warn("Failed to enable wow with ssid filter: %d\n", ret);
302110509f90SKalle Valo 		return ret;
302210509f90SKalle Valo 	}
302310509f90SKalle Valo 
302410509f90SKalle Valo 	/* this also clears IE in fw if it's not set */
302510509f90SKalle Valo 	ret = ath6kl_wmi_set_appie_cmd(ar->wmi, vif->fw_vif_idx,
302610509f90SKalle Valo 				       WMI_FRAME_PROBE_REQ,
302710509f90SKalle Valo 				       request->ie, request->ie_len);
302810509f90SKalle Valo 	if (ret) {
302910509f90SKalle Valo 		ath6kl_warn("Failed to set probe request IE for scheduled scan: %d",
303010509f90SKalle Valo 			    ret);
303110509f90SKalle Valo 		return ret;
303210509f90SKalle Valo 	}
303310509f90SKalle Valo 
303410509f90SKalle Valo 	ret = ath6kl_wmi_set_host_sleep_mode_cmd(ar->wmi, vif->fw_vif_idx,
303510509f90SKalle Valo 						 ATH6KL_HOST_MODE_ASLEEP);
303610509f90SKalle Valo 	if (ret) {
303710509f90SKalle Valo 		ath6kl_warn("Failed to enable host sleep mode for sched scan: %d\n",
303810509f90SKalle Valo 			    ret);
303910509f90SKalle Valo 		return ret;
304010509f90SKalle Valo 	}
304110509f90SKalle Valo 
304210509f90SKalle Valo 	ar->state = ATH6KL_STATE_SCHED_SCAN;
304310509f90SKalle Valo 
304410509f90SKalle Valo 	return ret;
304510509f90SKalle Valo }
304610509f90SKalle Valo 
304710509f90SKalle Valo static int ath6kl_cfg80211_sscan_stop(struct wiphy *wiphy,
304810509f90SKalle Valo 				      struct net_device *dev)
304910509f90SKalle Valo {
305010509f90SKalle Valo 	struct ath6kl_vif *vif = netdev_priv(dev);
305110509f90SKalle Valo 	bool stopped;
305210509f90SKalle Valo 
305310509f90SKalle Valo 	stopped = __ath6kl_cfg80211_sscan_stop(vif);
305410509f90SKalle Valo 
305510509f90SKalle Valo 	if (!stopped)
305610509f90SKalle Valo 		return -EIO;
305710509f90SKalle Valo 
305810509f90SKalle Valo 	return 0;
305910509f90SKalle Valo }
306010509f90SKalle Valo 
3061f80574aeSJouni Malinen static const struct ieee80211_txrx_stypes
3062f80574aeSJouni Malinen ath6kl_mgmt_stypes[NUM_NL80211_IFTYPES] = {
3063f80574aeSJouni Malinen 	[NL80211_IFTYPE_STATION] = {
3064f80574aeSJouni Malinen 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
3065f80574aeSJouni Malinen 		BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
3066f80574aeSJouni Malinen 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
3067f80574aeSJouni Malinen 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
3068f80574aeSJouni Malinen 	},
3069ba1f6fe3SJouni Malinen 	[NL80211_IFTYPE_AP] = {
3070ba1f6fe3SJouni Malinen 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
3071ba1f6fe3SJouni Malinen 		BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
3072ba1f6fe3SJouni Malinen 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
3073ba1f6fe3SJouni Malinen 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
3074ba1f6fe3SJouni Malinen 	},
3075f80574aeSJouni Malinen 	[NL80211_IFTYPE_P2P_CLIENT] = {
3076f80574aeSJouni Malinen 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
3077f80574aeSJouni Malinen 		BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
3078f80574aeSJouni Malinen 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
3079f80574aeSJouni Malinen 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
3080f80574aeSJouni Malinen 	},
3081f80574aeSJouni Malinen 	[NL80211_IFTYPE_P2P_GO] = {
3082f80574aeSJouni Malinen 		.tx = BIT(IEEE80211_STYPE_ACTION >> 4) |
3083f80574aeSJouni Malinen 		BIT(IEEE80211_STYPE_PROBE_RESP >> 4),
3084f80574aeSJouni Malinen 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
3085f80574aeSJouni Malinen 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
3086f80574aeSJouni Malinen 	},
3087f80574aeSJouni Malinen };
3088f80574aeSJouni Malinen 
3089bdcd8170SKalle Valo static struct cfg80211_ops ath6kl_cfg80211_ops = {
309055055976SVasanthakumar Thiagarajan 	.add_virtual_intf = ath6kl_cfg80211_add_iface,
309155055976SVasanthakumar Thiagarajan 	.del_virtual_intf = ath6kl_cfg80211_del_iface,
3092bdcd8170SKalle Valo 	.change_virtual_intf = ath6kl_cfg80211_change_iface,
3093bdcd8170SKalle Valo 	.scan = ath6kl_cfg80211_scan,
3094bdcd8170SKalle Valo 	.connect = ath6kl_cfg80211_connect,
3095bdcd8170SKalle Valo 	.disconnect = ath6kl_cfg80211_disconnect,
3096bdcd8170SKalle Valo 	.add_key = ath6kl_cfg80211_add_key,
3097bdcd8170SKalle Valo 	.get_key = ath6kl_cfg80211_get_key,
3098bdcd8170SKalle Valo 	.del_key = ath6kl_cfg80211_del_key,
3099bdcd8170SKalle Valo 	.set_default_key = ath6kl_cfg80211_set_default_key,
3100bdcd8170SKalle Valo 	.set_wiphy_params = ath6kl_cfg80211_set_wiphy_params,
3101bdcd8170SKalle Valo 	.set_tx_power = ath6kl_cfg80211_set_txpower,
3102bdcd8170SKalle Valo 	.get_tx_power = ath6kl_cfg80211_get_txpower,
3103bdcd8170SKalle Valo 	.set_power_mgmt = ath6kl_cfg80211_set_power_mgmt,
3104bdcd8170SKalle Valo 	.join_ibss = ath6kl_cfg80211_join_ibss,
3105bdcd8170SKalle Valo 	.leave_ibss = ath6kl_cfg80211_leave_ibss,
3106bdcd8170SKalle Valo 	.get_station = ath6kl_get_station,
3107bdcd8170SKalle Valo 	.set_pmksa = ath6kl_set_pmksa,
3108bdcd8170SKalle Valo 	.del_pmksa = ath6kl_del_pmksa,
3109bdcd8170SKalle Valo 	.flush_pmksa = ath6kl_flush_pmksa,
3110003353b0SKalle Valo 	CFG80211_TESTMODE_CMD(ath6kl_tm_cmd)
3111abcb344bSKalle Valo #ifdef CONFIG_PM
311252d81a68SKalle Valo 	.suspend = __ath6kl_cfg80211_suspend,
311352d81a68SKalle Valo 	.resume = __ath6kl_cfg80211_resume,
3114abcb344bSKalle Valo #endif
31156a7c9badSJouni Malinen 	.set_channel = ath6kl_set_channel,
31168860020eSJohannes Berg 	.start_ap = ath6kl_start_ap,
31178860020eSJohannes Berg 	.change_beacon = ath6kl_change_beacon,
31188860020eSJohannes Berg 	.stop_ap = ath6kl_stop_ap,
311933e5308dSJouni Malinen 	.del_station = ath6kl_del_station,
312023875136SJouni Malinen 	.change_station = ath6kl_change_station,
312163fa1e0cSJouni Malinen 	.remain_on_channel = ath6kl_remain_on_channel,
312263fa1e0cSJouni Malinen 	.cancel_remain_on_channel = ath6kl_cancel_remain_on_channel,
31238a6c8060SJouni Malinen 	.mgmt_tx = ath6kl_mgmt_tx,
3124ae32c30aSJouni Malinen 	.mgmt_frame_register = ath6kl_mgmt_frame_register,
312510509f90SKalle Valo 	.sched_scan_start = ath6kl_cfg80211_sscan_start,
312610509f90SKalle Valo 	.sched_scan_stop = ath6kl_cfg80211_sscan_stop,
3127bdcd8170SKalle Valo };
3128bdcd8170SKalle Valo 
31297125f01dSKalle Valo void ath6kl_cfg80211_stop(struct ath6kl_vif *vif)
3130ec4b7f60SKalle Valo {
313110509f90SKalle Valo 	ath6kl_cfg80211_sscan_disable(vif);
313210509f90SKalle Valo 
3133ec4b7f60SKalle Valo 	switch (vif->sme_state) {
3134c97a31b0SKalle Valo 	case SME_DISCONNECTED:
3135c97a31b0SKalle Valo 		break;
3136ec4b7f60SKalle Valo 	case SME_CONNECTING:
3137ec4b7f60SKalle Valo 		cfg80211_connect_result(vif->ndev, vif->bssid, NULL, 0,
3138ec4b7f60SKalle Valo 					NULL, 0,
3139ec4b7f60SKalle Valo 					WLAN_STATUS_UNSPECIFIED_FAILURE,
3140ec4b7f60SKalle Valo 					GFP_KERNEL);
3141ec4b7f60SKalle Valo 		break;
3142ec4b7f60SKalle Valo 	case SME_CONNECTED:
3143ec4b7f60SKalle Valo 		cfg80211_disconnected(vif->ndev, 0, NULL, 0, GFP_KERNEL);
3144ec4b7f60SKalle Valo 		break;
3145ec4b7f60SKalle Valo 	}
3146ec4b7f60SKalle Valo 
3147ec4b7f60SKalle Valo 	if (test_bit(CONNECTED, &vif->flags) ||
3148ec4b7f60SKalle Valo 	    test_bit(CONNECT_PEND, &vif->flags))
31497125f01dSKalle Valo 		ath6kl_wmi_disconnect_cmd(vif->ar->wmi, vif->fw_vif_idx);
3150ec4b7f60SKalle Valo 
3151ec4b7f60SKalle Valo 	vif->sme_state = SME_DISCONNECTED;
31521f405255SKalle Valo 	clear_bit(CONNECTED, &vif->flags);
31531f405255SKalle Valo 	clear_bit(CONNECT_PEND, &vif->flags);
3154ec4b7f60SKalle Valo 
3155ec4b7f60SKalle Valo 	/* disable scanning */
31567125f01dSKalle Valo 	if (ath6kl_wmi_scanparams_cmd(vif->ar->wmi, vif->fw_vif_idx, 0xFFFF,
31577125f01dSKalle Valo 				      0, 0, 0, 0, 0, 0, 0, 0, 0) != 0)
31587125f01dSKalle Valo 		ath6kl_warn("failed to disable scan during stop\n");
3159ec4b7f60SKalle Valo 
3160ec4b7f60SKalle Valo 	ath6kl_cfg80211_scan_complete_event(vif, true);
3161ec4b7f60SKalle Valo }
3162ec4b7f60SKalle Valo 
31637125f01dSKalle Valo void ath6kl_cfg80211_stop_all(struct ath6kl *ar)
31647125f01dSKalle Valo {
31657125f01dSKalle Valo 	struct ath6kl_vif *vif;
31667125f01dSKalle Valo 
31677125f01dSKalle Valo 	vif = ath6kl_vif_first(ar);
31687125f01dSKalle Valo 	if (!vif) {
31697125f01dSKalle Valo 		/* save the current power mode before enabling power save */
31707125f01dSKalle Valo 		ar->wmi->saved_pwr_mode = ar->wmi->pwr_mode;
31717125f01dSKalle Valo 
31727125f01dSKalle Valo 		if (ath6kl_wmi_powermode_cmd(ar->wmi, 0, REC_POWER) != 0)
31737125f01dSKalle Valo 			ath6kl_warn("ath6kl_deep_sleep_enable: "
31747125f01dSKalle Valo 				    "wmi_powermode_cmd failed\n");
31757125f01dSKalle Valo 		return;
31767125f01dSKalle Valo 	}
31777125f01dSKalle Valo 
31787125f01dSKalle Valo 	/*
31797125f01dSKalle Valo 	 * FIXME: we should take ar->list_lock to protect changes in the
31807125f01dSKalle Valo 	 * vif_list, but that's not trivial to do as ath6kl_cfg80211_stop()
31817125f01dSKalle Valo 	 * sleeps.
31827125f01dSKalle Valo 	 */
31837125f01dSKalle Valo 	list_for_each_entry(vif, &ar->vif_list, list)
31847125f01dSKalle Valo 		ath6kl_cfg80211_stop(vif);
31857125f01dSKalle Valo }
31867125f01dSKalle Valo 
3187c25889e8SKalle Valo static int ath6kl_cfg80211_vif_init(struct ath6kl_vif *vif)
31888dafb70eSVasanthakumar Thiagarajan {
31897baef812SVasanthakumar Thiagarajan 	vif->aggr_cntxt = aggr_init(vif);
31902132c69cSVasanthakumar Thiagarajan 	if (!vif->aggr_cntxt) {
31918dafb70eSVasanthakumar Thiagarajan 		ath6kl_err("failed to initialize aggr\n");
31928dafb70eSVasanthakumar Thiagarajan 		return -ENOMEM;
31938dafb70eSVasanthakumar Thiagarajan 	}
31948dafb70eSVasanthakumar Thiagarajan 
3195de3ad713SVasanthakumar Thiagarajan 	setup_timer(&vif->disconnect_timer, disconnect_timer_handler,
3196108438bcSVasanthakumar Thiagarajan 		    (unsigned long) vif->ndev);
319710509f90SKalle Valo 	setup_timer(&vif->sched_scan_timer, ath6kl_wmi_sscan_timer,
319810509f90SKalle Valo 		    (unsigned long) vif);
319910509f90SKalle Valo 
3200de3ad713SVasanthakumar Thiagarajan 	set_bit(WMM_ENABLED, &vif->flags);
3201478ac027SVasanthakumar Thiagarajan 	spin_lock_init(&vif->if_lock);
32028dafb70eSVasanthakumar Thiagarajan 
320380abaf9bSVasanthakumar Thiagarajan 	INIT_LIST_HEAD(&vif->mc_filter);
320480abaf9bSVasanthakumar Thiagarajan 
32058dafb70eSVasanthakumar Thiagarajan 	return 0;
32068dafb70eSVasanthakumar Thiagarajan }
32078dafb70eSVasanthakumar Thiagarajan 
3208c25889e8SKalle Valo void ath6kl_cfg80211_vif_cleanup(struct ath6kl_vif *vif)
32098dafb70eSVasanthakumar Thiagarajan {
321055055976SVasanthakumar Thiagarajan 	struct ath6kl *ar = vif->ar;
321180abaf9bSVasanthakumar Thiagarajan 	struct ath6kl_mc_filter *mc_filter, *tmp;
321255055976SVasanthakumar Thiagarajan 
32132132c69cSVasanthakumar Thiagarajan 	aggr_module_destroy(vif->aggr_cntxt);
3214108438bcSVasanthakumar Thiagarajan 
321555055976SVasanthakumar Thiagarajan 	ar->avail_idx_map |= BIT(vif->fw_vif_idx);
321655055976SVasanthakumar Thiagarajan 
321755055976SVasanthakumar Thiagarajan 	if (vif->nw_type == ADHOC_NETWORK)
321855055976SVasanthakumar Thiagarajan 		ar->ibss_if_active = false;
321955055976SVasanthakumar Thiagarajan 
322080abaf9bSVasanthakumar Thiagarajan 	list_for_each_entry_safe(mc_filter, tmp, &vif->mc_filter, list) {
322180abaf9bSVasanthakumar Thiagarajan 		list_del(&mc_filter->list);
322280abaf9bSVasanthakumar Thiagarajan 		kfree(mc_filter);
322380abaf9bSVasanthakumar Thiagarajan 	}
322480abaf9bSVasanthakumar Thiagarajan 
322527929723SVasanthakumar Thiagarajan 	unregister_netdevice(vif->ndev);
322655055976SVasanthakumar Thiagarajan 
322755055976SVasanthakumar Thiagarajan 	ar->num_vif--;
32288dafb70eSVasanthakumar Thiagarajan }
32298dafb70eSVasanthakumar Thiagarajan 
32308dafb70eSVasanthakumar Thiagarajan struct net_device *ath6kl_interface_add(struct ath6kl *ar, char *name,
323155055976SVasanthakumar Thiagarajan 					enum nl80211_iftype type, u8 fw_vif_idx,
323255055976SVasanthakumar Thiagarajan 					u8 nw_type)
32338dafb70eSVasanthakumar Thiagarajan {
32348dafb70eSVasanthakumar Thiagarajan 	struct net_device *ndev;
3235108438bcSVasanthakumar Thiagarajan 	struct ath6kl_vif *vif;
32368dafb70eSVasanthakumar Thiagarajan 
323755055976SVasanthakumar Thiagarajan 	ndev = alloc_netdev(sizeof(*vif), name, ether_setup);
32388dafb70eSVasanthakumar Thiagarajan 	if (!ndev)
32398dafb70eSVasanthakumar Thiagarajan 		return NULL;
32408dafb70eSVasanthakumar Thiagarajan 
3241108438bcSVasanthakumar Thiagarajan 	vif = netdev_priv(ndev);
3242108438bcSVasanthakumar Thiagarajan 	ndev->ieee80211_ptr = &vif->wdev;
3243108438bcSVasanthakumar Thiagarajan 	vif->wdev.wiphy = ar->wiphy;
3244108438bcSVasanthakumar Thiagarajan 	vif->ar = ar;
3245108438bcSVasanthakumar Thiagarajan 	vif->ndev = ndev;
3246108438bcSVasanthakumar Thiagarajan 	SET_NETDEV_DEV(ndev, wiphy_dev(vif->wdev.wiphy));
3247108438bcSVasanthakumar Thiagarajan 	vif->wdev.netdev = ndev;
3248108438bcSVasanthakumar Thiagarajan 	vif->wdev.iftype = type;
3249334234b5SVasanthakumar Thiagarajan 	vif->fw_vif_idx = fw_vif_idx;
3250d0d670abSKalle Valo 	vif->nw_type = nw_type;
3251d0d670abSKalle Valo 	vif->next_mode = nw_type;
32528f46fccdSRaja Mani 	vif->listen_intvl_t = ATH6KL_DEFAULT_LISTEN_INTVAL;
3253ce0dc0cfSRaja Mani 	vif->bmiss_time_t = ATH6KL_DEFAULT_BMISS_TIME;
32548dafb70eSVasanthakumar Thiagarajan 
325555055976SVasanthakumar Thiagarajan 	memcpy(ndev->dev_addr, ar->mac_addr, ETH_ALEN);
325655055976SVasanthakumar Thiagarajan 	if (fw_vif_idx != 0)
325755055976SVasanthakumar Thiagarajan 		ndev->dev_addr[0] = (ndev->dev_addr[0] ^ (1 << fw_vif_idx)) |
325855055976SVasanthakumar Thiagarajan 				     0x2;
325955055976SVasanthakumar Thiagarajan 
32608dafb70eSVasanthakumar Thiagarajan 	init_netdev(ndev);
32618dafb70eSVasanthakumar Thiagarajan 
3262e29f25f5SVasanthakumar Thiagarajan 	ath6kl_init_control_info(vif);
32638dafb70eSVasanthakumar Thiagarajan 
3264c25889e8SKalle Valo 	if (ath6kl_cfg80211_vif_init(vif))
32658dafb70eSVasanthakumar Thiagarajan 		goto err;
32668dafb70eSVasanthakumar Thiagarajan 
326727929723SVasanthakumar Thiagarajan 	if (register_netdevice(ndev))
32688dafb70eSVasanthakumar Thiagarajan 		goto err;
32698dafb70eSVasanthakumar Thiagarajan 
327055055976SVasanthakumar Thiagarajan 	ar->avail_idx_map &= ~BIT(fw_vif_idx);
327114ee6f6bSVasanthakumar Thiagarajan 	vif->sme_state = SME_DISCONNECTED;
327259c98449SVasanthakumar Thiagarajan 	set_bit(WLAN_ENABLED, &vif->flags);
32738dafb70eSVasanthakumar Thiagarajan 	ar->wlan_pwr_state = WLAN_POWER_STATE_ON;
327459c98449SVasanthakumar Thiagarajan 	set_bit(NETDEV_REGISTERED, &vif->flags);
32758dafb70eSVasanthakumar Thiagarajan 
327655055976SVasanthakumar Thiagarajan 	if (type == NL80211_IFTYPE_ADHOC)
327755055976SVasanthakumar Thiagarajan 		ar->ibss_if_active = true;
327855055976SVasanthakumar Thiagarajan 
327911f6e40dSVasanthakumar Thiagarajan 	spin_lock_bh(&ar->list_lock);
3280990bd915SVasanthakumar Thiagarajan 	list_add_tail(&vif->list, &ar->vif_list);
328111f6e40dSVasanthakumar Thiagarajan 	spin_unlock_bh(&ar->list_lock);
3282990bd915SVasanthakumar Thiagarajan 
32838dafb70eSVasanthakumar Thiagarajan 	return ndev;
32848dafb70eSVasanthakumar Thiagarajan 
32858dafb70eSVasanthakumar Thiagarajan err:
328627929723SVasanthakumar Thiagarajan 	aggr_module_destroy(vif->aggr_cntxt);
328727929723SVasanthakumar Thiagarajan 	free_netdev(ndev);
3288bdcd8170SKalle Valo 	return NULL;
3289bdcd8170SKalle Valo }
3290bdcd8170SKalle Valo 
329146d33a21SKalle Valo int ath6kl_cfg80211_init(struct ath6kl *ar)
329246d33a21SKalle Valo {
329346d33a21SKalle Valo 	struct wiphy *wiphy = ar->wiphy;
329446d33a21SKalle Valo 	int ret;
329546d33a21SKalle Valo 
329646d33a21SKalle Valo 	wiphy->mgmt_stypes = ath6kl_mgmt_stypes;
329746d33a21SKalle Valo 
329846d33a21SKalle Valo 	wiphy->max_remain_on_channel_duration = 5000;
329946d33a21SKalle Valo 
330046d33a21SKalle Valo 	/* set device pointer for wiphy */
330146d33a21SKalle Valo 	set_wiphy_dev(wiphy, ar->dev);
330246d33a21SKalle Valo 
330346d33a21SKalle Valo 	wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
330446d33a21SKalle Valo 				 BIT(NL80211_IFTYPE_ADHOC) |
330546d33a21SKalle Valo 				 BIT(NL80211_IFTYPE_AP);
330646d33a21SKalle Valo 	if (ar->p2p) {
330746d33a21SKalle Valo 		wiphy->interface_modes |= BIT(NL80211_IFTYPE_P2P_GO) |
330846d33a21SKalle Valo 					  BIT(NL80211_IFTYPE_P2P_CLIENT);
330946d33a21SKalle Valo 	}
331046d33a21SKalle Valo 
331146d33a21SKalle Valo 	/* max num of ssids that can be probed during scanning */
331246d33a21SKalle Valo 	wiphy->max_scan_ssids = MAX_PROBED_SSID_INDEX;
331346d33a21SKalle Valo 	wiphy->max_scan_ie_len = 1000; /* FIX: what is correct limit? */
331446d33a21SKalle Valo 	wiphy->bands[IEEE80211_BAND_2GHZ] = &ath6kl_band_2ghz;
331546d33a21SKalle Valo 	wiphy->bands[IEEE80211_BAND_5GHZ] = &ath6kl_band_5ghz;
331646d33a21SKalle Valo 	wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
331746d33a21SKalle Valo 
331846d33a21SKalle Valo 	wiphy->cipher_suites = cipher_suites;
331946d33a21SKalle Valo 	wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
332046d33a21SKalle Valo 
332146d33a21SKalle Valo 	wiphy->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT |
332246d33a21SKalle Valo 			      WIPHY_WOWLAN_DISCONNECT |
332346d33a21SKalle Valo 			      WIPHY_WOWLAN_GTK_REKEY_FAILURE  |
332446d33a21SKalle Valo 			      WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
332546d33a21SKalle Valo 			      WIPHY_WOWLAN_EAP_IDENTITY_REQ   |
332646d33a21SKalle Valo 			      WIPHY_WOWLAN_4WAY_HANDSHAKE;
332746d33a21SKalle Valo 	wiphy->wowlan.n_patterns = WOW_MAX_FILTERS_PER_LIST;
332846d33a21SKalle Valo 	wiphy->wowlan.pattern_min_len = 1;
332946d33a21SKalle Valo 	wiphy->wowlan.pattern_max_len = WOW_PATTERN_SIZE;
333046d33a21SKalle Valo 
333146d33a21SKalle Valo 	wiphy->max_sched_scan_ssids = 10;
333246d33a21SKalle Valo 
3333f2afdac7SVasanthakumar Thiagarajan 	ar->wiphy->flags |= WIPHY_FLAG_SUPPORTS_FW_ROAM |
3334f2afdac7SVasanthakumar Thiagarajan 			    WIPHY_FLAG_HAVE_AP_SME |
3335f2afdac7SVasanthakumar Thiagarajan 			    WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
3336f2afdac7SVasanthakumar Thiagarajan 			    WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
3337f2afdac7SVasanthakumar Thiagarajan 
3338f2afdac7SVasanthakumar Thiagarajan 	if (test_bit(ATH6KL_FW_CAPABILITY_SCHED_SCAN, ar->fw_capabilities))
3339f2afdac7SVasanthakumar Thiagarajan 		ar->wiphy->flags |= WIPHY_FLAG_SUPPORTS_SCHED_SCAN;
3340f2afdac7SVasanthakumar Thiagarajan 
334103bdeb0dSVasanthakumar Thiagarajan 	if (test_bit(ATH6KL_FW_CAPABILITY_INACTIVITY_TIMEOUT,
334203bdeb0dSVasanthakumar Thiagarajan 		     ar->fw_capabilities))
334303bdeb0dSVasanthakumar Thiagarajan 		ar->wiphy->features = NL80211_FEATURE_INACTIVITY_TIMER;
334403bdeb0dSVasanthakumar Thiagarajan 
3345f2afdac7SVasanthakumar Thiagarajan 	ar->wiphy->probe_resp_offload =
3346f2afdac7SVasanthakumar Thiagarajan 		NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
3347f2afdac7SVasanthakumar Thiagarajan 		NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
3348f2afdac7SVasanthakumar Thiagarajan 		NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P |
3349f2afdac7SVasanthakumar Thiagarajan 		NL80211_PROBE_RESP_OFFLOAD_SUPPORT_80211U;
3350f2afdac7SVasanthakumar Thiagarajan 
335146d33a21SKalle Valo 	ret = wiphy_register(wiphy);
335246d33a21SKalle Valo 	if (ret < 0) {
335346d33a21SKalle Valo 		ath6kl_err("couldn't register wiphy device\n");
335446d33a21SKalle Valo 		return ret;
335546d33a21SKalle Valo 	}
335646d33a21SKalle Valo 
3357e5348a1eSVasanthakumar Thiagarajan 	ar->wiphy_registered = true;
3358e5348a1eSVasanthakumar Thiagarajan 
335946d33a21SKalle Valo 	return 0;
336046d33a21SKalle Valo }
336146d33a21SKalle Valo 
336246d33a21SKalle Valo void ath6kl_cfg80211_cleanup(struct ath6kl *ar)
3363bdcd8170SKalle Valo {
3364be98e3a4SVasanthakumar Thiagarajan 	wiphy_unregister(ar->wiphy);
3365e5348a1eSVasanthakumar Thiagarajan 
3366e5348a1eSVasanthakumar Thiagarajan 	ar->wiphy_registered = false;
336745eaa78fSKalle Valo }
336846d33a21SKalle Valo 
336945eaa78fSKalle Valo struct ath6kl *ath6kl_cfg80211_create(void)
337045eaa78fSKalle Valo {
337145eaa78fSKalle Valo 	struct ath6kl *ar;
337245eaa78fSKalle Valo 	struct wiphy *wiphy;
337345eaa78fSKalle Valo 
337445eaa78fSKalle Valo 	/* create a new wiphy for use with cfg80211 */
337545eaa78fSKalle Valo 	wiphy = wiphy_new(&ath6kl_cfg80211_ops, sizeof(struct ath6kl));
337645eaa78fSKalle Valo 
337745eaa78fSKalle Valo 	if (!wiphy) {
337845eaa78fSKalle Valo 		ath6kl_err("couldn't allocate wiphy device\n");
337945eaa78fSKalle Valo 		return NULL;
338045eaa78fSKalle Valo 	}
338145eaa78fSKalle Valo 
338245eaa78fSKalle Valo 	ar = wiphy_priv(wiphy);
338345eaa78fSKalle Valo 	ar->wiphy = wiphy;
338445eaa78fSKalle Valo 
338545eaa78fSKalle Valo 	return ar;
338645eaa78fSKalle Valo }
338745eaa78fSKalle Valo 
338845eaa78fSKalle Valo /* Note: ar variable must not be accessed after calling this! */
338945eaa78fSKalle Valo void ath6kl_cfg80211_destroy(struct ath6kl *ar)
339045eaa78fSKalle Valo {
33911d2a4456SVasanthakumar Thiagarajan 	int i;
33921d2a4456SVasanthakumar Thiagarajan 
33931d2a4456SVasanthakumar Thiagarajan 	for (i = 0; i < AP_MAX_NUM_STA; i++)
33941d2a4456SVasanthakumar Thiagarajan 		kfree(ar->sta_list[i].aggr_conn);
33951d2a4456SVasanthakumar Thiagarajan 
3396be98e3a4SVasanthakumar Thiagarajan 	wiphy_free(ar->wiphy);
3397bdcd8170SKalle Valo }
339845eaa78fSKalle Valo 
3399