xref: /linux/drivers/net/wireless/ath/ath6kl/init.c (revision 958e1be848c92006ee4b95190d3725daf3a70034)
1bdcd8170SKalle Valo 
2bdcd8170SKalle Valo /*
3bdcd8170SKalle Valo  * Copyright (c) 2011 Atheros Communications Inc.
41b2df407SVasanthakumar Thiagarajan  * Copyright (c) 2011-2012 Qualcomm Atheros, Inc.
5bdcd8170SKalle Valo  *
6bdcd8170SKalle Valo  * Permission to use, copy, modify, and/or distribute this software for any
7bdcd8170SKalle Valo  * purpose with or without fee is hereby granted, provided that the above
8bdcd8170SKalle Valo  * copyright notice and this permission notice appear in all copies.
9bdcd8170SKalle Valo  *
10bdcd8170SKalle Valo  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11bdcd8170SKalle Valo  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12bdcd8170SKalle Valo  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13bdcd8170SKalle Valo  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14bdcd8170SKalle Valo  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15bdcd8170SKalle Valo  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16bdcd8170SKalle Valo  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17bdcd8170SKalle Valo  */
18bdcd8170SKalle Valo 
19516304b0SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20516304b0SJoe Perches 
21c6efe578SStephen Rothwell #include <linux/moduleparam.h>
22f7830202SSangwook Lee #include <linux/errno.h>
23d6a434d6SKalle Valo #include <linux/export.h>
2492ecbff4SSam Leffler #include <linux/of.h>
25bdcd8170SKalle Valo #include <linux/mmc/sdio_func.h>
268437754cSVivek Natarajan #include <linux/vmalloc.h>
27d6a434d6SKalle Valo 
28bdcd8170SKalle Valo #include "core.h"
29bdcd8170SKalle Valo #include "cfg80211.h"
30bdcd8170SKalle Valo #include "target.h"
31bdcd8170SKalle Valo #include "debug.h"
32bdcd8170SKalle Valo #include "hif-ops.h"
33e76ac2bfSKalle Valo #include "htc-ops.h"
34bdcd8170SKalle Valo 
35856f4b31SKalle Valo static const struct ath6kl_hw hw_list[] = {
36856f4b31SKalle Valo 	{
370d0192baSKalle Valo 		.id				= AR6003_HW_2_0_VERSION,
38293badf4SKalle Valo 		.name				= "ar6003 hw 2.0",
39856f4b31SKalle Valo 		.dataset_patch_addr		= 0x57e884,
40856f4b31SKalle Valo 		.app_load_addr			= 0x543180,
41856f4b31SKalle Valo 		.board_ext_data_addr		= 0x57e500,
42856f4b31SKalle Valo 		.reserved_ram_size		= 6912,
4339586bf2SRyan Hsu 		.refclk_hz			= 26000000,
4439586bf2SRyan Hsu 		.uarttx_pin			= 8,
45a2e1be33SMohammed Shafi Shajakhan 		.flags				= ATH6KL_HW_SDIO_CRC_ERROR_WAR,
46856f4b31SKalle Valo 
47856f4b31SKalle Valo 		/* hw2.0 needs override address hardcoded */
48856f4b31SKalle Valo 		.app_start_override_addr	= 0x944C00,
49d1a9421dSKalle Valo 
50c0038972SKalle Valo 		.fw = {
51c0038972SKalle Valo 			.dir		= AR6003_HW_2_0_FW_DIR,
52c0038972SKalle Valo 			.otp		= AR6003_HW_2_0_OTP_FILE,
53d1a9421dSKalle Valo 			.fw		= AR6003_HW_2_0_FIRMWARE_FILE,
54c0038972SKalle Valo 			.tcmd		= AR6003_HW_2_0_TCMD_FIRMWARE_FILE,
55c0038972SKalle Valo 			.patch		= AR6003_HW_2_0_PATCH_FILE,
56c0038972SKalle Valo 		},
57c0038972SKalle Valo 
58d1a9421dSKalle Valo 		.fw_board		= AR6003_HW_2_0_BOARD_DATA_FILE,
59d1a9421dSKalle Valo 		.fw_default_board	= AR6003_HW_2_0_DEFAULT_BOARD_DATA_FILE,
60856f4b31SKalle Valo 	},
61856f4b31SKalle Valo 	{
620d0192baSKalle Valo 		.id				= AR6003_HW_2_1_1_VERSION,
63293badf4SKalle Valo 		.name				= "ar6003 hw 2.1.1",
64856f4b31SKalle Valo 		.dataset_patch_addr		= 0x57ff74,
65856f4b31SKalle Valo 		.app_load_addr			= 0x1234,
66856f4b31SKalle Valo 		.board_ext_data_addr		= 0x542330,
67856f4b31SKalle Valo 		.reserved_ram_size		= 512,
6839586bf2SRyan Hsu 		.refclk_hz			= 26000000,
6939586bf2SRyan Hsu 		.uarttx_pin			= 8,
70cd23c1c9SAlex Yang 		.testscript_addr		= 0x57ef74,
71a2e1be33SMohammed Shafi Shajakhan 		.flags				= ATH6KL_HW_SDIO_CRC_ERROR_WAR,
72d1a9421dSKalle Valo 
73c0038972SKalle Valo 		.fw = {
74c0038972SKalle Valo 			.dir		= AR6003_HW_2_1_1_FW_DIR,
75c0038972SKalle Valo 			.otp		= AR6003_HW_2_1_1_OTP_FILE,
76d1a9421dSKalle Valo 			.fw		= AR6003_HW_2_1_1_FIRMWARE_FILE,
77c0038972SKalle Valo 			.tcmd		= AR6003_HW_2_1_1_TCMD_FIRMWARE_FILE,
78c0038972SKalle Valo 			.patch		= AR6003_HW_2_1_1_PATCH_FILE,
79cd23c1c9SAlex Yang 			.utf		= AR6003_HW_2_1_1_UTF_FIRMWARE_FILE,
80cd23c1c9SAlex Yang 			.testscript	= AR6003_HW_2_1_1_TESTSCRIPT_FILE,
81c0038972SKalle Valo 		},
82c0038972SKalle Valo 
83d1a9421dSKalle Valo 		.fw_board		= AR6003_HW_2_1_1_BOARD_DATA_FILE,
84d1a9421dSKalle Valo 		.fw_default_board = AR6003_HW_2_1_1_DEFAULT_BOARD_DATA_FILE,
85856f4b31SKalle Valo 	},
86856f4b31SKalle Valo 	{
870d0192baSKalle Valo 		.id				= AR6004_HW_1_0_VERSION,
88293badf4SKalle Valo 		.name				= "ar6004 hw 1.0",
89856f4b31SKalle Valo 		.dataset_patch_addr		= 0x57e884,
90856f4b31SKalle Valo 		.app_load_addr			= 0x1234,
91856f4b31SKalle Valo 		.board_ext_data_addr		= 0x437000,
92856f4b31SKalle Valo 		.reserved_ram_size		= 19456,
930d4d72bfSKalle Valo 		.board_addr			= 0x433900,
9439586bf2SRyan Hsu 		.refclk_hz			= 26000000,
9539586bf2SRyan Hsu 		.uarttx_pin			= 11,
96eba95bceSKalle Valo 		.flags				= 0,
97d1a9421dSKalle Valo 
98c0038972SKalle Valo 		.fw = {
99c0038972SKalle Valo 			.dir		= AR6004_HW_1_0_FW_DIR,
100d1a9421dSKalle Valo 			.fw		= AR6004_HW_1_0_FIRMWARE_FILE,
101c0038972SKalle Valo 		},
102c0038972SKalle Valo 
103d1a9421dSKalle Valo 		.fw_board		= AR6004_HW_1_0_BOARD_DATA_FILE,
104d1a9421dSKalle Valo 		.fw_default_board	= AR6004_HW_1_0_DEFAULT_BOARD_DATA_FILE,
105856f4b31SKalle Valo 	},
106856f4b31SKalle Valo 	{
1070d0192baSKalle Valo 		.id				= AR6004_HW_1_1_VERSION,
108293badf4SKalle Valo 		.name				= "ar6004 hw 1.1",
109856f4b31SKalle Valo 		.dataset_patch_addr		= 0x57e884,
110856f4b31SKalle Valo 		.app_load_addr			= 0x1234,
111856f4b31SKalle Valo 		.board_ext_data_addr		= 0x437000,
112856f4b31SKalle Valo 		.reserved_ram_size		= 11264,
1130d4d72bfSKalle Valo 		.board_addr			= 0x43d400,
11439586bf2SRyan Hsu 		.refclk_hz			= 40000000,
11539586bf2SRyan Hsu 		.uarttx_pin			= 11,
116eba95bceSKalle Valo 		.flags				= 0,
117c0038972SKalle Valo 		.fw = {
118c0038972SKalle Valo 			.dir		= AR6004_HW_1_1_FW_DIR,
119d1a9421dSKalle Valo 			.fw		= AR6004_HW_1_1_FIRMWARE_FILE,
120c0038972SKalle Valo 		},
121c0038972SKalle Valo 
122d1a9421dSKalle Valo 		.fw_board		= AR6004_HW_1_1_BOARD_DATA_FILE,
123d1a9421dSKalle Valo 		.fw_default_board	= AR6004_HW_1_1_DEFAULT_BOARD_DATA_FILE,
124856f4b31SKalle Valo 	},
1256146ca69SRay Chen 	{
1266146ca69SRay Chen 		.id				= AR6004_HW_1_2_VERSION,
1276146ca69SRay Chen 		.name				= "ar6004 hw 1.2",
1286146ca69SRay Chen 		.dataset_patch_addr		= 0x436ecc,
1296146ca69SRay Chen 		.app_load_addr			= 0x1234,
1306146ca69SRay Chen 		.board_ext_data_addr		= 0x437000,
1316146ca69SRay Chen 		.reserved_ram_size		= 9216,
1326146ca69SRay Chen 		.board_addr			= 0x435c00,
1336146ca69SRay Chen 		.refclk_hz			= 40000000,
1346146ca69SRay Chen 		.uarttx_pin			= 11,
135eba95bceSKalle Valo 		.flags				= 0,
1366146ca69SRay Chen 
1376146ca69SRay Chen 		.fw = {
1386146ca69SRay Chen 			.dir		= AR6004_HW_1_2_FW_DIR,
1396146ca69SRay Chen 			.fw		= AR6004_HW_1_2_FIRMWARE_FILE,
1406146ca69SRay Chen 		},
1416146ca69SRay Chen 		.fw_board		= AR6004_HW_1_2_BOARD_DATA_FILE,
1426146ca69SRay Chen 		.fw_default_board	= AR6004_HW_1_2_DEFAULT_BOARD_DATA_FILE,
1436146ca69SRay Chen 	},
144bf744f11SBala Shanmugam 	{
145bf744f11SBala Shanmugam 		.id				= AR6004_HW_1_3_VERSION,
146bf744f11SBala Shanmugam 		.name				= "ar6004 hw 1.3",
147bf744f11SBala Shanmugam 		.dataset_patch_addr		= 0x437860,
148bf744f11SBala Shanmugam 		.app_load_addr			= 0x1234,
149bf744f11SBala Shanmugam 		.board_ext_data_addr		= 0x437000,
150bf744f11SBala Shanmugam 		.reserved_ram_size		= 7168,
151bf744f11SBala Shanmugam 		.board_addr			= 0x436400,
152bf744f11SBala Shanmugam 		.refclk_hz                      = 40000000,
153bf744f11SBala Shanmugam 		.uarttx_pin                     = 11,
154eba95bceSKalle Valo 		.flags				= 0,
155bf744f11SBala Shanmugam 
156bf744f11SBala Shanmugam 		.fw = {
157bf744f11SBala Shanmugam 			.dir            = AR6004_HW_1_3_FW_DIR,
158bf744f11SBala Shanmugam 			.fw             = AR6004_HW_1_3_FIRMWARE_FILE,
159bf744f11SBala Shanmugam 		},
160bf744f11SBala Shanmugam 
161bf744f11SBala Shanmugam 		.fw_board               = AR6004_HW_1_3_BOARD_DATA_FILE,
162bf744f11SBala Shanmugam 		.fw_default_board       = AR6004_HW_1_3_DEFAULT_BOARD_DATA_FILE,
163bf744f11SBala Shanmugam 	},
164856f4b31SKalle Valo };
165856f4b31SKalle Valo 
166bdcd8170SKalle Valo /*
167bdcd8170SKalle Valo  * Include definitions here that can be used to tune the WLAN module
168bdcd8170SKalle Valo  * behavior. Different customers can tune the behavior as per their needs,
169bdcd8170SKalle Valo  * here.
170bdcd8170SKalle Valo  */
171bdcd8170SKalle Valo 
172bdcd8170SKalle Valo /*
173bdcd8170SKalle Valo  * This configuration item enable/disable keepalive support.
174bdcd8170SKalle Valo  * Keepalive support: In the absence of any data traffic to AP, null
175bdcd8170SKalle Valo  * frames will be sent to the AP at periodic interval, to keep the association
176bdcd8170SKalle Valo  * active. This configuration item defines the periodic interval.
177bdcd8170SKalle Valo  * Use value of zero to disable keepalive support
178bdcd8170SKalle Valo  * Default: 60 seconds
179bdcd8170SKalle Valo  */
180bdcd8170SKalle Valo #define WLAN_CONFIG_KEEP_ALIVE_INTERVAL 60
181bdcd8170SKalle Valo 
182bdcd8170SKalle Valo /*
183bdcd8170SKalle Valo  * This configuration item sets the value of disconnect timeout
184bdcd8170SKalle Valo  * Firmware delays sending the disconnec event to the host for this
185bdcd8170SKalle Valo  * timeout after is gets disconnected from the current AP.
186bdcd8170SKalle Valo  * If the firmware successly roams within the disconnect timeout
187bdcd8170SKalle Valo  * it sends a new connect event
188bdcd8170SKalle Valo  */
189bdcd8170SKalle Valo #define WLAN_CONFIG_DISCONNECT_TIMEOUT 10
190bdcd8170SKalle Valo 
191bdcd8170SKalle Valo 
192bdcd8170SKalle Valo #define ATH6KL_DATA_OFFSET    64
193bdcd8170SKalle Valo struct sk_buff *ath6kl_buf_alloc(int size)
194bdcd8170SKalle Valo {
195bdcd8170SKalle Valo 	struct sk_buff *skb;
196bdcd8170SKalle Valo 	u16 reserved;
197bdcd8170SKalle Valo 
198bdcd8170SKalle Valo 	/* Add chacheline space at front and back of buffer */
1996a3e4e06SMyoungje Kim 	reserved = roundup((2 * L1_CACHE_BYTES) + ATH6KL_DATA_OFFSET +
2006a3e4e06SMyoungje Kim 		   sizeof(struct htc_packet) + ATH6KL_HTC_ALIGN_BYTES, 4);
201bdcd8170SKalle Valo 	skb = dev_alloc_skb(size + reserved);
202bdcd8170SKalle Valo 
203bdcd8170SKalle Valo 	if (skb)
204bdcd8170SKalle Valo 		skb_reserve(skb, reserved - L1_CACHE_BYTES);
205bdcd8170SKalle Valo 	return skb;
206bdcd8170SKalle Valo }
207bdcd8170SKalle Valo 
208e29f25f5SVasanthakumar Thiagarajan void ath6kl_init_profile_info(struct ath6kl_vif *vif)
209bdcd8170SKalle Valo {
2103450334fSVasanthakumar Thiagarajan 	vif->ssid_len = 0;
2113450334fSVasanthakumar Thiagarajan 	memset(vif->ssid, 0, sizeof(vif->ssid));
2123450334fSVasanthakumar Thiagarajan 
2133450334fSVasanthakumar Thiagarajan 	vif->dot11_auth_mode = OPEN_AUTH;
2143450334fSVasanthakumar Thiagarajan 	vif->auth_mode = NONE_AUTH;
2153450334fSVasanthakumar Thiagarajan 	vif->prwise_crypto = NONE_CRYPT;
2163450334fSVasanthakumar Thiagarajan 	vif->prwise_crypto_len = 0;
2173450334fSVasanthakumar Thiagarajan 	vif->grp_crypto = NONE_CRYPT;
2183450334fSVasanthakumar Thiagarajan 	vif->grp_crypto_len = 0;
2196f2a73f9SVasanthakumar Thiagarajan 	memset(vif->wep_key_list, 0, sizeof(vif->wep_key_list));
2208c8b65e3SVasanthakumar Thiagarajan 	memset(vif->req_bssid, 0, sizeof(vif->req_bssid));
2218c8b65e3SVasanthakumar Thiagarajan 	memset(vif->bssid, 0, sizeof(vif->bssid));
222f74bac54SVasanthakumar Thiagarajan 	vif->bss_ch = 0;
223bdcd8170SKalle Valo }
224bdcd8170SKalle Valo 
225bdcd8170SKalle Valo static int ath6kl_set_host_app_area(struct ath6kl *ar)
226bdcd8170SKalle Valo {
227bdcd8170SKalle Valo 	u32 address, data;
228bdcd8170SKalle Valo 	struct host_app_area host_app_area;
229bdcd8170SKalle Valo 
230bdcd8170SKalle Valo 	/* Fetch the address of the host_app_area_s
231bdcd8170SKalle Valo 	 * instance in the host interest area */
232bdcd8170SKalle Valo 	address = ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_app_host_interest));
23331024d99SKevin Fang 	address = TARG_VTOP(ar->target_type, address);
234bdcd8170SKalle Valo 
235addb44beSKalle Valo 	if (ath6kl_diag_read32(ar, address, &data))
236bdcd8170SKalle Valo 		return -EIO;
237bdcd8170SKalle Valo 
23831024d99SKevin Fang 	address = TARG_VTOP(ar->target_type, data);
239cbf49a6fSKalle Valo 	host_app_area.wmi_protocol_ver = cpu_to_le32(WMI_PROTOCOL_VERSION);
240addb44beSKalle Valo 	if (ath6kl_diag_write(ar, address, (u8 *) &host_app_area,
241addb44beSKalle Valo 			      sizeof(struct host_app_area)))
242bdcd8170SKalle Valo 		return -EIO;
243bdcd8170SKalle Valo 
244bdcd8170SKalle Valo 	return 0;
245bdcd8170SKalle Valo }
246bdcd8170SKalle Valo 
247bdcd8170SKalle Valo static inline void set_ac2_ep_map(struct ath6kl *ar,
248bdcd8170SKalle Valo 				  u8 ac,
249bdcd8170SKalle Valo 				  enum htc_endpoint_id ep)
250bdcd8170SKalle Valo {
251bdcd8170SKalle Valo 	ar->ac2ep_map[ac] = ep;
252bdcd8170SKalle Valo 	ar->ep2ac_map[ep] = ac;
253bdcd8170SKalle Valo }
254bdcd8170SKalle Valo 
255bdcd8170SKalle Valo /* connect to a service */
256bdcd8170SKalle Valo static int ath6kl_connectservice(struct ath6kl *ar,
257bdcd8170SKalle Valo 				 struct htc_service_connect_req  *con_req,
258bdcd8170SKalle Valo 				 char *desc)
259bdcd8170SKalle Valo {
260bdcd8170SKalle Valo 	int status;
261bdcd8170SKalle Valo 	struct htc_service_connect_resp response;
262bdcd8170SKalle Valo 
263bdcd8170SKalle Valo 	memset(&response, 0, sizeof(response));
264bdcd8170SKalle Valo 
265ad226ec2SKalle Valo 	status = ath6kl_htc_conn_service(ar->htc_target, con_req, &response);
266bdcd8170SKalle Valo 	if (status) {
267bdcd8170SKalle Valo 		ath6kl_err("failed to connect to %s service status:%d\n",
268bdcd8170SKalle Valo 			   desc, status);
269bdcd8170SKalle Valo 		return status;
270bdcd8170SKalle Valo 	}
271bdcd8170SKalle Valo 
272bdcd8170SKalle Valo 	switch (con_req->svc_id) {
273bdcd8170SKalle Valo 	case WMI_CONTROL_SVC:
274bdcd8170SKalle Valo 		if (test_bit(WMI_ENABLED, &ar->flag))
275bdcd8170SKalle Valo 			ath6kl_wmi_set_control_ep(ar->wmi, response.endpoint);
276bdcd8170SKalle Valo 		ar->ctrl_ep = response.endpoint;
277bdcd8170SKalle Valo 		break;
278bdcd8170SKalle Valo 	case WMI_DATA_BE_SVC:
279bdcd8170SKalle Valo 		set_ac2_ep_map(ar, WMM_AC_BE, response.endpoint);
280bdcd8170SKalle Valo 		break;
281bdcd8170SKalle Valo 	case WMI_DATA_BK_SVC:
282bdcd8170SKalle Valo 		set_ac2_ep_map(ar, WMM_AC_BK, response.endpoint);
283bdcd8170SKalle Valo 		break;
284bdcd8170SKalle Valo 	case WMI_DATA_VI_SVC:
285bdcd8170SKalle Valo 		set_ac2_ep_map(ar, WMM_AC_VI, response.endpoint);
286bdcd8170SKalle Valo 		break;
287bdcd8170SKalle Valo 	case WMI_DATA_VO_SVC:
288bdcd8170SKalle Valo 		set_ac2_ep_map(ar, WMM_AC_VO, response.endpoint);
289bdcd8170SKalle Valo 		break;
290bdcd8170SKalle Valo 	default:
291bdcd8170SKalle Valo 		ath6kl_err("service id is not mapped %d\n", con_req->svc_id);
292bdcd8170SKalle Valo 		return -EINVAL;
293bdcd8170SKalle Valo 	}
294bdcd8170SKalle Valo 
295bdcd8170SKalle Valo 	return 0;
296bdcd8170SKalle Valo }
297bdcd8170SKalle Valo 
298bdcd8170SKalle Valo static int ath6kl_init_service_ep(struct ath6kl *ar)
299bdcd8170SKalle Valo {
300bdcd8170SKalle Valo 	struct htc_service_connect_req connect;
301bdcd8170SKalle Valo 
302bdcd8170SKalle Valo 	memset(&connect, 0, sizeof(connect));
303bdcd8170SKalle Valo 
304bdcd8170SKalle Valo 	/* these fields are the same for all service endpoints */
305900d6b3fSKalle Valo 	connect.ep_cb.tx_comp_multi = ath6kl_tx_complete;
306bdcd8170SKalle Valo 	connect.ep_cb.rx = ath6kl_rx;
307bdcd8170SKalle Valo 	connect.ep_cb.rx_refill = ath6kl_rx_refill;
308bdcd8170SKalle Valo 	connect.ep_cb.tx_full = ath6kl_tx_queue_full;
309bdcd8170SKalle Valo 
310bdcd8170SKalle Valo 	/*
311bdcd8170SKalle Valo 	 * Set the max queue depth so that our ath6kl_tx_queue_full handler
312bdcd8170SKalle Valo 	 * gets called.
313bdcd8170SKalle Valo 	*/
314bdcd8170SKalle Valo 	connect.max_txq_depth = MAX_DEFAULT_SEND_QUEUE_DEPTH;
315bdcd8170SKalle Valo 	connect.ep_cb.rx_refill_thresh = ATH6KL_MAX_RX_BUFFERS / 4;
316bdcd8170SKalle Valo 	if (!connect.ep_cb.rx_refill_thresh)
317bdcd8170SKalle Valo 		connect.ep_cb.rx_refill_thresh++;
318bdcd8170SKalle Valo 
319bdcd8170SKalle Valo 	/* connect to control service */
320bdcd8170SKalle Valo 	connect.svc_id = WMI_CONTROL_SVC;
321bdcd8170SKalle Valo 	if (ath6kl_connectservice(ar, &connect, "WMI CONTROL"))
322bdcd8170SKalle Valo 		return -EIO;
323bdcd8170SKalle Valo 
324bdcd8170SKalle Valo 	connect.flags |= HTC_FLGS_TX_BNDL_PAD_EN;
325bdcd8170SKalle Valo 
326bdcd8170SKalle Valo 	/*
327bdcd8170SKalle Valo 	 * Limit the HTC message size on the send path, although e can
328bdcd8170SKalle Valo 	 * receive A-MSDU frames of 4K, we will only send ethernet-sized
329bdcd8170SKalle Valo 	 * (802.3) frames on the send path.
330bdcd8170SKalle Valo 	 */
331bdcd8170SKalle Valo 	connect.max_rxmsg_sz = WMI_MAX_TX_DATA_FRAME_LENGTH;
332bdcd8170SKalle Valo 
333bdcd8170SKalle Valo 	/*
334bdcd8170SKalle Valo 	 * To reduce the amount of committed memory for larger A_MSDU
335bdcd8170SKalle Valo 	 * frames, use the recv-alloc threshold mechanism for larger
336bdcd8170SKalle Valo 	 * packets.
337bdcd8170SKalle Valo 	 */
338bdcd8170SKalle Valo 	connect.ep_cb.rx_alloc_thresh = ATH6KL_BUFFER_SIZE;
339bdcd8170SKalle Valo 	connect.ep_cb.rx_allocthresh = ath6kl_alloc_amsdu_rxbuf;
340bdcd8170SKalle Valo 
341bdcd8170SKalle Valo 	/*
342bdcd8170SKalle Valo 	 * For the remaining data services set the connection flag to
343bdcd8170SKalle Valo 	 * reduce dribbling, if configured to do so.
344bdcd8170SKalle Valo 	 */
345bdcd8170SKalle Valo 	connect.conn_flags |= HTC_CONN_FLGS_REDUCE_CRED_DRIB;
346bdcd8170SKalle Valo 	connect.conn_flags &= ~HTC_CONN_FLGS_THRESH_MASK;
347bdcd8170SKalle Valo 	connect.conn_flags |= HTC_CONN_FLGS_THRESH_LVL_HALF;
348bdcd8170SKalle Valo 
349bdcd8170SKalle Valo 	connect.svc_id = WMI_DATA_BE_SVC;
350bdcd8170SKalle Valo 
351bdcd8170SKalle Valo 	if (ath6kl_connectservice(ar, &connect, "WMI DATA BE"))
352bdcd8170SKalle Valo 		return -EIO;
353bdcd8170SKalle Valo 
354bdcd8170SKalle Valo 	/* connect to back-ground map this to WMI LOW_PRI */
355bdcd8170SKalle Valo 	connect.svc_id = WMI_DATA_BK_SVC;
356bdcd8170SKalle Valo 	if (ath6kl_connectservice(ar, &connect, "WMI DATA BK"))
357bdcd8170SKalle Valo 		return -EIO;
358bdcd8170SKalle Valo 
359171fe768SMohammed Shafi Shajakhan 	/* connect to Video service, map this to HI PRI */
360bdcd8170SKalle Valo 	connect.svc_id = WMI_DATA_VI_SVC;
361bdcd8170SKalle Valo 	if (ath6kl_connectservice(ar, &connect, "WMI DATA VI"))
362bdcd8170SKalle Valo 		return -EIO;
363bdcd8170SKalle Valo 
364bdcd8170SKalle Valo 	/*
365bdcd8170SKalle Valo 	 * Connect to VO service, this is currently not mapped to a WMI
366bdcd8170SKalle Valo 	 * priority stream due to historical reasons. WMI originally
367bdcd8170SKalle Valo 	 * defined 3 priorities over 3 mailboxes We can change this when
368bdcd8170SKalle Valo 	 * WMI is reworked so that priorities are not dependent on
369bdcd8170SKalle Valo 	 * mailboxes.
370bdcd8170SKalle Valo 	 */
371bdcd8170SKalle Valo 	connect.svc_id = WMI_DATA_VO_SVC;
372bdcd8170SKalle Valo 	if (ath6kl_connectservice(ar, &connect, "WMI DATA VO"))
373bdcd8170SKalle Valo 		return -EIO;
374bdcd8170SKalle Valo 
375bdcd8170SKalle Valo 	return 0;
376bdcd8170SKalle Valo }
377bdcd8170SKalle Valo 
378e29f25f5SVasanthakumar Thiagarajan void ath6kl_init_control_info(struct ath6kl_vif *vif)
379bdcd8170SKalle Valo {
380e29f25f5SVasanthakumar Thiagarajan 	ath6kl_init_profile_info(vif);
3813450334fSVasanthakumar Thiagarajan 	vif->def_txkey_index = 0;
3826f2a73f9SVasanthakumar Thiagarajan 	memset(vif->wep_key_list, 0, sizeof(vif->wep_key_list));
383f74bac54SVasanthakumar Thiagarajan 	vif->ch_hint = 0;
384bdcd8170SKalle Valo }
385bdcd8170SKalle Valo 
386bdcd8170SKalle Valo /*
387bdcd8170SKalle Valo  * Set HTC/Mbox operational parameters, this can only be called when the
388bdcd8170SKalle Valo  * target is in the BMI phase.
389bdcd8170SKalle Valo  */
390bdcd8170SKalle Valo static int ath6kl_set_htc_params(struct ath6kl *ar, u32 mbox_isr_yield_val,
391bdcd8170SKalle Valo 				 u8 htc_ctrl_buf)
392bdcd8170SKalle Valo {
393bdcd8170SKalle Valo 	int status;
394bdcd8170SKalle Valo 	u32 blk_size;
395bdcd8170SKalle Valo 
396bdcd8170SKalle Valo 	blk_size = ar->mbox_info.block_size;
397bdcd8170SKalle Valo 
398bdcd8170SKalle Valo 	if (htc_ctrl_buf)
399bdcd8170SKalle Valo 		blk_size |=  ((u32)htc_ctrl_buf) << 16;
400bdcd8170SKalle Valo 
401bdcd8170SKalle Valo 	/* set the host interest area for the block size */
40224fc32b3SKalle Valo 	status = ath6kl_bmi_write_hi32(ar, hi_mbox_io_block_sz, blk_size);
403bdcd8170SKalle Valo 	if (status) {
404bdcd8170SKalle Valo 		ath6kl_err("bmi_write_memory for IO block size failed\n");
405bdcd8170SKalle Valo 		goto out;
406bdcd8170SKalle Valo 	}
407bdcd8170SKalle Valo 
408bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_TRC, "block size set: %d (target addr:0x%X)\n",
409bdcd8170SKalle Valo 		   blk_size,
410bdcd8170SKalle Valo 		   ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_mbox_io_block_sz)));
411bdcd8170SKalle Valo 
412bdcd8170SKalle Valo 	if (mbox_isr_yield_val) {
413bdcd8170SKalle Valo 		/* set the host interest area for the mbox ISR yield limit */
41424fc32b3SKalle Valo 		status = ath6kl_bmi_write_hi32(ar, hi_mbox_isr_yield_limit,
41524fc32b3SKalle Valo 					       mbox_isr_yield_val);
416bdcd8170SKalle Valo 		if (status) {
417bdcd8170SKalle Valo 			ath6kl_err("bmi_write_memory for yield limit failed\n");
418bdcd8170SKalle Valo 			goto out;
419bdcd8170SKalle Valo 		}
420bdcd8170SKalle Valo 	}
421bdcd8170SKalle Valo 
422bdcd8170SKalle Valo out:
423bdcd8170SKalle Valo 	return status;
424bdcd8170SKalle Valo }
425bdcd8170SKalle Valo 
4260ce59445SVasanthakumar Thiagarajan static int ath6kl_target_config_wlan_params(struct ath6kl *ar, int idx)
427bdcd8170SKalle Valo {
4284dea08e0SJouni Malinen 	int ret;
429bdcd8170SKalle Valo 
430bdcd8170SKalle Valo 	/*
431bdcd8170SKalle Valo 	 * Configure the device for rx dot11 header rules. "0,0" are the
432bdcd8170SKalle Valo 	 * default values. Required if checksum offload is needed. Set
433bdcd8170SKalle Valo 	 * RxMetaVersion to 2.
434bdcd8170SKalle Valo 	 */
4351ca4d0b6SKalle Valo 	ret = ath6kl_wmi_set_rx_frame_format_cmd(ar->wmi, idx,
4361ca4d0b6SKalle Valo 						 ar->rx_meta_ver, 0, 0);
4371ca4d0b6SKalle Valo 	if (ret) {
4381ca4d0b6SKalle Valo 		ath6kl_err("unable to set the rx frame format: %d\n", ret);
4391ca4d0b6SKalle Valo 		return ret;
440bdcd8170SKalle Valo 	}
441bdcd8170SKalle Valo 
4421ca4d0b6SKalle Valo 	if (ar->conf_flags & ATH6KL_CONF_IGNORE_PS_FAIL_EVT_IN_SCAN) {
4431ca4d0b6SKalle Valo 		ret = ath6kl_wmi_pmparams_cmd(ar->wmi, idx, 0, 1, 0, 0, 1,
44405aab177SKalle Valo 					      IGNORE_PS_FAIL_DURING_SCAN);
4451ca4d0b6SKalle Valo 		if (ret) {
4461ca4d0b6SKalle Valo 			ath6kl_err("unable to set power save fail event policy: %d\n",
4471ca4d0b6SKalle Valo 				   ret);
4481ca4d0b6SKalle Valo 			return ret;
4491ca4d0b6SKalle Valo 		}
450bdcd8170SKalle Valo 	}
451bdcd8170SKalle Valo 
4521ca4d0b6SKalle Valo 	if (!(ar->conf_flags & ATH6KL_CONF_IGNORE_ERP_BARKER)) {
4531ca4d0b6SKalle Valo 		ret = ath6kl_wmi_set_lpreamble_cmd(ar->wmi, idx, 0,
45405aab177SKalle Valo 						   WMI_FOLLOW_BARKER_IN_ERP);
4551ca4d0b6SKalle Valo 		if (ret) {
4561ca4d0b6SKalle Valo 			ath6kl_err("unable to set barker preamble policy: %d\n",
4571ca4d0b6SKalle Valo 				   ret);
4581ca4d0b6SKalle Valo 			return ret;
4591ca4d0b6SKalle Valo 		}
460bdcd8170SKalle Valo 	}
461bdcd8170SKalle Valo 
4621ca4d0b6SKalle Valo 	ret = ath6kl_wmi_set_keepalive_cmd(ar->wmi, idx,
4631ca4d0b6SKalle Valo 					   WLAN_CONFIG_KEEP_ALIVE_INTERVAL);
4641ca4d0b6SKalle Valo 	if (ret) {
4651ca4d0b6SKalle Valo 		ath6kl_err("unable to set keep alive interval: %d\n", ret);
4661ca4d0b6SKalle Valo 		return ret;
467bdcd8170SKalle Valo 	}
468bdcd8170SKalle Valo 
4691ca4d0b6SKalle Valo 	ret = ath6kl_wmi_disctimeout_cmd(ar->wmi, idx,
4701ca4d0b6SKalle Valo 					 WLAN_CONFIG_DISCONNECT_TIMEOUT);
4711ca4d0b6SKalle Valo 	if (ret) {
4721ca4d0b6SKalle Valo 		ath6kl_err("unable to set disconnect timeout: %d\n", ret);
4731ca4d0b6SKalle Valo 		return ret;
474bdcd8170SKalle Valo 	}
475bdcd8170SKalle Valo 
4761ca4d0b6SKalle Valo 	if (!(ar->conf_flags & ATH6KL_CONF_ENABLE_TX_BURST)) {
4771ca4d0b6SKalle Valo 		ret = ath6kl_wmi_set_wmm_txop(ar->wmi, idx, WMI_TXOP_DISABLED);
4781ca4d0b6SKalle Valo 		if (ret) {
4791ca4d0b6SKalle Valo 			ath6kl_err("unable to set txop bursting: %d\n", ret);
4801ca4d0b6SKalle Valo 			return ret;
4811ca4d0b6SKalle Valo 		}
482bdcd8170SKalle Valo 	}
483bdcd8170SKalle Valo 
484b64de356SVasanthakumar Thiagarajan 	if (ar->p2p && (ar->vif_max == 1 || idx)) {
4850ce59445SVasanthakumar Thiagarajan 		ret = ath6kl_wmi_info_req_cmd(ar->wmi, idx,
4866bbc7c35SJouni Malinen 					      P2P_FLAG_CAPABILITIES_REQ |
4874dea08e0SJouni Malinen 					      P2P_FLAG_MACADDR_REQ |
4884dea08e0SJouni Malinen 					      P2P_FLAG_HMODEL_REQ);
4894dea08e0SJouni Malinen 		if (ret) {
490cdeb8602SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_TRC,
491cdeb8602SKalle Valo 				   "failed to request P2P capabilities (%d) - assuming P2P not supported\n",
492cdeb8602SKalle Valo 				   ret);
4933db1cd5cSRusty Russell 			ar->p2p = false;
4946bbc7c35SJouni Malinen 		}
4956bbc7c35SJouni Malinen 	}
4966bbc7c35SJouni Malinen 
497b64de356SVasanthakumar Thiagarajan 	if (ar->p2p && (ar->vif_max == 1 || idx)) {
4986bbc7c35SJouni Malinen 		/* Enable Probe Request reporting for P2P */
4990ce59445SVasanthakumar Thiagarajan 		ret = ath6kl_wmi_probe_report_req_cmd(ar->wmi, idx, true);
5006bbc7c35SJouni Malinen 		if (ret) {
501cdeb8602SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_TRC,
502cdeb8602SKalle Valo 				   "failed to enable Probe Request reporting (%d)\n",
503cdeb8602SKalle Valo 				   ret);
5046bbc7c35SJouni Malinen 		}
5054dea08e0SJouni Malinen 	}
5064dea08e0SJouni Malinen 
5071ca4d0b6SKalle Valo 	return ret;
508bdcd8170SKalle Valo }
509bdcd8170SKalle Valo 
510bdcd8170SKalle Valo int ath6kl_configure_target(struct ath6kl *ar)
511bdcd8170SKalle Valo {
512bdcd8170SKalle Valo 	u32 param, ram_reserved_size;
5133226f68aSVasanthakumar Thiagarajan 	u8 fw_iftype, fw_mode = 0, fw_submode = 0;
51439586bf2SRyan Hsu 	int i, status;
515bdcd8170SKalle Valo 
516f29af978SKalle Valo 	param = !!(ar->conf_flags & ATH6KL_CONF_UART_DEBUG);
51724fc32b3SKalle Valo 	if (ath6kl_bmi_write_hi32(ar, hi_serial_enable, param)) {
518a10e2f2fSVasanthakumar Thiagarajan 		ath6kl_err("bmi_write_memory for uart debug failed\n");
519a10e2f2fSVasanthakumar Thiagarajan 		return -EIO;
520a10e2f2fSVasanthakumar Thiagarajan 	}
521a10e2f2fSVasanthakumar Thiagarajan 
5227b85832dSVasanthakumar Thiagarajan 	/*
5237b85832dSVasanthakumar Thiagarajan 	 * Note: Even though the firmware interface type is
5247b85832dSVasanthakumar Thiagarajan 	 * chosen as BSS_STA for all three interfaces, can
5257b85832dSVasanthakumar Thiagarajan 	 * be configured to IBSS/AP as long as the fw submode
5267b85832dSVasanthakumar Thiagarajan 	 * remains normal mode (0 - AP, STA and IBSS). But
5277b85832dSVasanthakumar Thiagarajan 	 * due to an target assert in firmware only one interface is
5287b85832dSVasanthakumar Thiagarajan 	 * configured for now.
5297b85832dSVasanthakumar Thiagarajan 	 */
530dd3751f7SVasanthakumar Thiagarajan 	fw_iftype = HI_OPTION_FW_MODE_BSS_STA;
531bdcd8170SKalle Valo 
53271f96ee6SKalle Valo 	for (i = 0; i < ar->vif_max; i++)
5337b85832dSVasanthakumar Thiagarajan 		fw_mode |= fw_iftype << (i * HI_OPTION_FW_MODE_BITS);
5347b85832dSVasanthakumar Thiagarajan 
5357b85832dSVasanthakumar Thiagarajan 	/*
5361e8d13b0SVasanthakumar Thiagarajan 	 * Submodes when fw does not support dynamic interface
5371e8d13b0SVasanthakumar Thiagarajan 	 * switching:
5383226f68aSVasanthakumar Thiagarajan 	 *		vif[0] - AP/STA/IBSS
5397b85832dSVasanthakumar Thiagarajan 	 *		vif[1] - "P2P dev"/"P2P GO"/"P2P Client"
5407b85832dSVasanthakumar Thiagarajan 	 *		vif[2] - "P2P dev"/"P2P GO"/"P2P Client"
5411e8d13b0SVasanthakumar Thiagarajan 	 * Otherwise, All the interface are initialized to p2p dev.
5427b85832dSVasanthakumar Thiagarajan 	 */
5433226f68aSVasanthakumar Thiagarajan 
5441e8d13b0SVasanthakumar Thiagarajan 	if (test_bit(ATH6KL_FW_CAPABILITY_STA_P2PDEV_DUPLEX,
5451e8d13b0SVasanthakumar Thiagarajan 		     ar->fw_capabilities)) {
5461e8d13b0SVasanthakumar Thiagarajan 		for (i = 0; i < ar->vif_max; i++)
5471e8d13b0SVasanthakumar Thiagarajan 			fw_submode |= HI_OPTION_FW_SUBMODE_P2PDEV <<
5481e8d13b0SVasanthakumar Thiagarajan 				(i * HI_OPTION_FW_SUBMODE_BITS);
5491e8d13b0SVasanthakumar Thiagarajan 	} else {
5503226f68aSVasanthakumar Thiagarajan 		for (i = 0; i < ar->max_norm_iface; i++)
5513226f68aSVasanthakumar Thiagarajan 			fw_submode |= HI_OPTION_FW_SUBMODE_NONE <<
5523226f68aSVasanthakumar Thiagarajan 				(i * HI_OPTION_FW_SUBMODE_BITS);
5533226f68aSVasanthakumar Thiagarajan 
55471f96ee6SKalle Valo 		for (i = ar->max_norm_iface; i < ar->vif_max; i++)
5553226f68aSVasanthakumar Thiagarajan 			fw_submode |= HI_OPTION_FW_SUBMODE_P2PDEV <<
5563226f68aSVasanthakumar Thiagarajan 				(i * HI_OPTION_FW_SUBMODE_BITS);
5577b85832dSVasanthakumar Thiagarajan 
558b64de356SVasanthakumar Thiagarajan 		if (ar->p2p && ar->vif_max == 1)
5597b85832dSVasanthakumar Thiagarajan 			fw_submode = HI_OPTION_FW_SUBMODE_P2PDEV;
5601e8d13b0SVasanthakumar Thiagarajan 	}
5617b85832dSVasanthakumar Thiagarajan 
56224fc32b3SKalle Valo 	if (ath6kl_bmi_write_hi32(ar, hi_app_host_interest,
56324fc32b3SKalle Valo 				  HTC_PROTOCOL_VERSION) != 0) {
564bdcd8170SKalle Valo 		ath6kl_err("bmi_write_memory for htc version failed\n");
565bdcd8170SKalle Valo 		return -EIO;
566bdcd8170SKalle Valo 	}
567bdcd8170SKalle Valo 
568bdcd8170SKalle Valo 	/* set the firmware mode to STA/IBSS/AP */
569bdcd8170SKalle Valo 	param = 0;
570bdcd8170SKalle Valo 
57180fb2686SKalle Valo 	if (ath6kl_bmi_read_hi32(ar, hi_option_flag, &param) != 0) {
572bdcd8170SKalle Valo 		ath6kl_err("bmi_read_memory for setting fwmode failed\n");
573bdcd8170SKalle Valo 		return -EIO;
574bdcd8170SKalle Valo 	}
575bdcd8170SKalle Valo 
57671f96ee6SKalle Valo 	param |= (ar->vif_max << HI_OPTION_NUM_DEV_SHIFT);
5777b85832dSVasanthakumar Thiagarajan 	param |= fw_mode << HI_OPTION_FW_MODE_SHIFT;
5787b85832dSVasanthakumar Thiagarajan 	param |= fw_submode << HI_OPTION_FW_SUBMODE_SHIFT;
5797b85832dSVasanthakumar Thiagarajan 
580bdcd8170SKalle Valo 	param |= (0 << HI_OPTION_MAC_ADDR_METHOD_SHIFT);
581bdcd8170SKalle Valo 	param |= (0 << HI_OPTION_FW_BRIDGE_SHIFT);
582bdcd8170SKalle Valo 
58324fc32b3SKalle Valo 	if (ath6kl_bmi_write_hi32(ar, hi_option_flag, param) != 0) {
584bdcd8170SKalle Valo 		ath6kl_err("bmi_write_memory for setting fwmode failed\n");
585bdcd8170SKalle Valo 		return -EIO;
586bdcd8170SKalle Valo 	}
587bdcd8170SKalle Valo 
588bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_TRC, "firmware mode set\n");
589bdcd8170SKalle Valo 
590bdcd8170SKalle Valo 	/*
591bdcd8170SKalle Valo 	 * Hardcode the address use for the extended board data
592bdcd8170SKalle Valo 	 * Ideally this should be pre-allocate by the OS at boot time
593bdcd8170SKalle Valo 	 * But since it is a new feature and board data is loaded
594bdcd8170SKalle Valo 	 * at init time, we have to workaround this from host.
595bdcd8170SKalle Valo 	 * It is difficult to patch the firmware boot code,
596bdcd8170SKalle Valo 	 * but possible in theory.
597bdcd8170SKalle Valo 	 */
598bdcd8170SKalle Valo 
5996b42d308SKalle Valo 	if (ar->target_type == TARGET_TYPE_AR6003) {
600991b27eaSKalle Valo 		param = ar->hw.board_ext_data_addr;
601991b27eaSKalle Valo 		ram_reserved_size = ar->hw.reserved_ram_size;
602bdcd8170SKalle Valo 
60324fc32b3SKalle Valo 		if (ath6kl_bmi_write_hi32(ar, hi_board_ext_data, param) != 0) {
604bdcd8170SKalle Valo 			ath6kl_err("bmi_write_memory for hi_board_ext_data failed\n");
605bdcd8170SKalle Valo 			return -EIO;
606bdcd8170SKalle Valo 		}
607991b27eaSKalle Valo 
60824fc32b3SKalle Valo 		if (ath6kl_bmi_write_hi32(ar, hi_end_ram_reserve_sz,
60924fc32b3SKalle Valo 					  ram_reserved_size) != 0) {
610bdcd8170SKalle Valo 			ath6kl_err("bmi_write_memory for hi_end_ram_reserve_sz failed\n");
611bdcd8170SKalle Valo 			return -EIO;
612bdcd8170SKalle Valo 		}
6136b42d308SKalle Valo 	}
614bdcd8170SKalle Valo 
615bdcd8170SKalle Valo 	/* set the block size for the target */
616bdcd8170SKalle Valo 	if (ath6kl_set_htc_params(ar, MBOX_YIELD_LIMIT, 0))
617bdcd8170SKalle Valo 		/* use default number of control buffers */
618bdcd8170SKalle Valo 		return -EIO;
619bdcd8170SKalle Valo 
62039586bf2SRyan Hsu 	/* Configure GPIO AR600x UART */
62124fc32b3SKalle Valo 	status = ath6kl_bmi_write_hi32(ar, hi_dbg_uart_txpin,
62224fc32b3SKalle Valo 				       ar->hw.uarttx_pin);
62339586bf2SRyan Hsu 	if (status)
62439586bf2SRyan Hsu 		return status;
62539586bf2SRyan Hsu 
62639586bf2SRyan Hsu 	/* Configure target refclk_hz */
627*958e1be8SKalle Valo 	if (ar->hw.refclk_hz != 0) {
628*958e1be8SKalle Valo 		status = ath6kl_bmi_write_hi32(ar, hi_refclk_hz,
629*958e1be8SKalle Valo 					       ar->hw.refclk_hz);
63039586bf2SRyan Hsu 		if (status)
63139586bf2SRyan Hsu 			return status;
632*958e1be8SKalle Valo 	}
63339586bf2SRyan Hsu 
634bdcd8170SKalle Valo 	return 0;
635bdcd8170SKalle Valo }
636bdcd8170SKalle Valo 
637bdcd8170SKalle Valo /* firmware upload */
638bdcd8170SKalle Valo static int ath6kl_get_fw(struct ath6kl *ar, const char *filename,
639bdcd8170SKalle Valo 			 u8 **fw, size_t *fw_len)
640bdcd8170SKalle Valo {
641bdcd8170SKalle Valo 	const struct firmware *fw_entry;
642bdcd8170SKalle Valo 	int ret;
643bdcd8170SKalle Valo 
644bdcd8170SKalle Valo 	ret = request_firmware(&fw_entry, filename, ar->dev);
645bdcd8170SKalle Valo 	if (ret)
646bdcd8170SKalle Valo 		return ret;
647bdcd8170SKalle Valo 
648bdcd8170SKalle Valo 	*fw_len = fw_entry->size;
649bdcd8170SKalle Valo 	*fw = kmemdup(fw_entry->data, fw_entry->size, GFP_KERNEL);
650bdcd8170SKalle Valo 
651bdcd8170SKalle Valo 	if (*fw == NULL)
652bdcd8170SKalle Valo 		ret = -ENOMEM;
653bdcd8170SKalle Valo 
654bdcd8170SKalle Valo 	release_firmware(fw_entry);
655bdcd8170SKalle Valo 
656bdcd8170SKalle Valo 	return ret;
657bdcd8170SKalle Valo }
658bdcd8170SKalle Valo 
65992ecbff4SSam Leffler #ifdef CONFIG_OF
66092ecbff4SSam Leffler /*
66192ecbff4SSam Leffler  * Check the device tree for a board-id and use it to construct
66292ecbff4SSam Leffler  * the pathname to the firmware file.  Used (for now) to find a
66392ecbff4SSam Leffler  * fallback to the "bdata.bin" file--typically a symlink to the
66492ecbff4SSam Leffler  * appropriate board-specific file.
66592ecbff4SSam Leffler  */
66692ecbff4SSam Leffler static bool check_device_tree(struct ath6kl *ar)
66792ecbff4SSam Leffler {
66892ecbff4SSam Leffler 	static const char *board_id_prop = "atheros,board-id";
66992ecbff4SSam Leffler 	struct device_node *node;
67092ecbff4SSam Leffler 	char board_filename[64];
67192ecbff4SSam Leffler 	const char *board_id;
67292ecbff4SSam Leffler 	int ret;
67392ecbff4SSam Leffler 
67492ecbff4SSam Leffler 	for_each_compatible_node(node, NULL, "atheros,ath6kl") {
67592ecbff4SSam Leffler 		board_id = of_get_property(node, board_id_prop, NULL);
67692ecbff4SSam Leffler 		if (board_id == NULL) {
67792ecbff4SSam Leffler 			ath6kl_warn("No \"%s\" property on %s node.\n",
67892ecbff4SSam Leffler 				    board_id_prop, node->name);
67992ecbff4SSam Leffler 			continue;
68092ecbff4SSam Leffler 		}
68192ecbff4SSam Leffler 		snprintf(board_filename, sizeof(board_filename),
682c0038972SKalle Valo 			 "%s/bdata.%s.bin", ar->hw.fw.dir, board_id);
68392ecbff4SSam Leffler 
68492ecbff4SSam Leffler 		ret = ath6kl_get_fw(ar, board_filename, &ar->fw_board,
68592ecbff4SSam Leffler 				    &ar->fw_board_len);
68692ecbff4SSam Leffler 		if (ret) {
68792ecbff4SSam Leffler 			ath6kl_err("Failed to get DT board file %s: %d\n",
68892ecbff4SSam Leffler 				   board_filename, ret);
68992ecbff4SSam Leffler 			continue;
69092ecbff4SSam Leffler 		}
69192ecbff4SSam Leffler 		return true;
69292ecbff4SSam Leffler 	}
69392ecbff4SSam Leffler 	return false;
69492ecbff4SSam Leffler }
69592ecbff4SSam Leffler #else
69692ecbff4SSam Leffler static bool check_device_tree(struct ath6kl *ar)
69792ecbff4SSam Leffler {
69892ecbff4SSam Leffler 	return false;
69992ecbff4SSam Leffler }
70092ecbff4SSam Leffler #endif /* CONFIG_OF */
70192ecbff4SSam Leffler 
702bdcd8170SKalle Valo static int ath6kl_fetch_board_file(struct ath6kl *ar)
703bdcd8170SKalle Valo {
704bdcd8170SKalle Valo 	const char *filename;
705bdcd8170SKalle Valo 	int ret;
706bdcd8170SKalle Valo 
707772c31eeSKalle Valo 	if (ar->fw_board != NULL)
708772c31eeSKalle Valo 		return 0;
709772c31eeSKalle Valo 
710d1a9421dSKalle Valo 	if (WARN_ON(ar->hw.fw_board == NULL))
711d1a9421dSKalle Valo 		return -EINVAL;
712d1a9421dSKalle Valo 
713d1a9421dSKalle Valo 	filename = ar->hw.fw_board;
714bdcd8170SKalle Valo 
715bdcd8170SKalle Valo 	ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
716bdcd8170SKalle Valo 			    &ar->fw_board_len);
717bdcd8170SKalle Valo 	if (ret == 0) {
718bdcd8170SKalle Valo 		/* managed to get proper board file */
719bdcd8170SKalle Valo 		return 0;
720bdcd8170SKalle Valo 	}
721bdcd8170SKalle Valo 
72292ecbff4SSam Leffler 	if (check_device_tree(ar)) {
72392ecbff4SSam Leffler 		/* got board file from device tree */
72492ecbff4SSam Leffler 		return 0;
72592ecbff4SSam Leffler 	}
72692ecbff4SSam Leffler 
727bdcd8170SKalle Valo 	/* there was no proper board file, try to use default instead */
728bdcd8170SKalle Valo 	ath6kl_warn("Failed to get board file %s (%d), trying to find default board file.\n",
729bdcd8170SKalle Valo 		    filename, ret);
730bdcd8170SKalle Valo 
731d1a9421dSKalle Valo 	filename = ar->hw.fw_default_board;
732bdcd8170SKalle Valo 
733bdcd8170SKalle Valo 	ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
734bdcd8170SKalle Valo 			    &ar->fw_board_len);
735bdcd8170SKalle Valo 	if (ret) {
736bdcd8170SKalle Valo 		ath6kl_err("Failed to get default board file %s: %d\n",
737bdcd8170SKalle Valo 			   filename, ret);
738bdcd8170SKalle Valo 		return ret;
739bdcd8170SKalle Valo 	}
740bdcd8170SKalle Valo 
741bdcd8170SKalle Valo 	ath6kl_warn("WARNING! No proper board file was not found, instead using a default board file.\n");
742bdcd8170SKalle Valo 	ath6kl_warn("Most likely your hardware won't work as specified. Install correct board file!\n");
743bdcd8170SKalle Valo 
744bdcd8170SKalle Valo 	return 0;
745bdcd8170SKalle Valo }
746bdcd8170SKalle Valo 
747772c31eeSKalle Valo static int ath6kl_fetch_otp_file(struct ath6kl *ar)
748772c31eeSKalle Valo {
749c0038972SKalle Valo 	char filename[100];
750772c31eeSKalle Valo 	int ret;
751772c31eeSKalle Valo 
752772c31eeSKalle Valo 	if (ar->fw_otp != NULL)
753772c31eeSKalle Valo 		return 0;
754772c31eeSKalle Valo 
755c0038972SKalle Valo 	if (ar->hw.fw.otp == NULL) {
756d1a9421dSKalle Valo 		ath6kl_dbg(ATH6KL_DBG_BOOT,
757d1a9421dSKalle Valo 			   "no OTP file configured for this hw\n");
758772c31eeSKalle Valo 		return 0;
759772c31eeSKalle Valo 	}
760772c31eeSKalle Valo 
761c0038972SKalle Valo 	snprintf(filename, sizeof(filename), "%s/%s",
762c0038972SKalle Valo 		 ar->hw.fw.dir, ar->hw.fw.otp);
763d1a9421dSKalle Valo 
764772c31eeSKalle Valo 	ret = ath6kl_get_fw(ar, filename, &ar->fw_otp,
765772c31eeSKalle Valo 			    &ar->fw_otp_len);
766772c31eeSKalle Valo 	if (ret) {
767772c31eeSKalle Valo 		ath6kl_err("Failed to get OTP file %s: %d\n",
768772c31eeSKalle Valo 			   filename, ret);
769772c31eeSKalle Valo 		return ret;
770772c31eeSKalle Valo 	}
771772c31eeSKalle Valo 
772772c31eeSKalle Valo 	return 0;
773772c31eeSKalle Valo }
774772c31eeSKalle Valo 
7755f1127ffSKalle Valo static int ath6kl_fetch_testmode_file(struct ath6kl *ar)
776772c31eeSKalle Valo {
777c0038972SKalle Valo 	char filename[100];
778772c31eeSKalle Valo 	int ret;
779772c31eeSKalle Valo 
7805f1127ffSKalle Valo 	if (ar->testmode == 0)
781772c31eeSKalle Valo 		return 0;
782772c31eeSKalle Valo 
7835f1127ffSKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "testmode %d\n", ar->testmode);
7845f1127ffSKalle Valo 
7855f1127ffSKalle Valo 	if (ar->testmode == 2) {
786cd23c1c9SAlex Yang 		if (ar->hw.fw.utf == NULL) {
787cd23c1c9SAlex Yang 			ath6kl_warn("testmode 2 not supported\n");
788cd23c1c9SAlex Yang 			return -EOPNOTSUPP;
789cd23c1c9SAlex Yang 		}
790cd23c1c9SAlex Yang 
791cd23c1c9SAlex Yang 		snprintf(filename, sizeof(filename), "%s/%s",
792cd23c1c9SAlex Yang 			 ar->hw.fw.dir, ar->hw.fw.utf);
793cd23c1c9SAlex Yang 	} else {
794c0038972SKalle Valo 		if (ar->hw.fw.tcmd == NULL) {
795cd23c1c9SAlex Yang 			ath6kl_warn("testmode 1 not supported\n");
796772c31eeSKalle Valo 			return -EOPNOTSUPP;
797772c31eeSKalle Valo 		}
798772c31eeSKalle Valo 
799c0038972SKalle Valo 		snprintf(filename, sizeof(filename), "%s/%s",
800c0038972SKalle Valo 			 ar->hw.fw.dir, ar->hw.fw.tcmd);
801cd23c1c9SAlex Yang 	}
8025f1127ffSKalle Valo 
803772c31eeSKalle Valo 	set_bit(TESTMODE, &ar->flag);
804772c31eeSKalle Valo 
8055f1127ffSKalle Valo 	ret = ath6kl_get_fw(ar, filename, &ar->fw, &ar->fw_len);
8065f1127ffSKalle Valo 	if (ret) {
8075f1127ffSKalle Valo 		ath6kl_err("Failed to get testmode %d firmware file %s: %d\n",
8085f1127ffSKalle Valo 			   ar->testmode, filename, ret);
8095f1127ffSKalle Valo 		return ret;
810772c31eeSKalle Valo 	}
811772c31eeSKalle Valo 
8125f1127ffSKalle Valo 	return 0;
8135f1127ffSKalle Valo }
8145f1127ffSKalle Valo 
8155f1127ffSKalle Valo static int ath6kl_fetch_fw_file(struct ath6kl *ar)
8165f1127ffSKalle Valo {
8175f1127ffSKalle Valo 	char filename[100];
8185f1127ffSKalle Valo 	int ret;
8195f1127ffSKalle Valo 
8205f1127ffSKalle Valo 	if (ar->fw != NULL)
8215f1127ffSKalle Valo 		return 0;
8225f1127ffSKalle Valo 
823c0038972SKalle Valo 	/* FIXME: remove WARN_ON() as we won't support FW API 1 for long */
824c0038972SKalle Valo 	if (WARN_ON(ar->hw.fw.fw == NULL))
825d1a9421dSKalle Valo 		return -EINVAL;
826d1a9421dSKalle Valo 
827c0038972SKalle Valo 	snprintf(filename, sizeof(filename), "%s/%s",
828c0038972SKalle Valo 		 ar->hw.fw.dir, ar->hw.fw.fw);
829772c31eeSKalle Valo 
830772c31eeSKalle Valo 	ret = ath6kl_get_fw(ar, filename, &ar->fw, &ar->fw_len);
831772c31eeSKalle Valo 	if (ret) {
832772c31eeSKalle Valo 		ath6kl_err("Failed to get firmware file %s: %d\n",
833772c31eeSKalle Valo 			   filename, ret);
834772c31eeSKalle Valo 		return ret;
835772c31eeSKalle Valo 	}
836772c31eeSKalle Valo 
837772c31eeSKalle Valo 	return 0;
838772c31eeSKalle Valo }
839772c31eeSKalle Valo 
840772c31eeSKalle Valo static int ath6kl_fetch_patch_file(struct ath6kl *ar)
841772c31eeSKalle Valo {
842c0038972SKalle Valo 	char filename[100];
843772c31eeSKalle Valo 	int ret;
844772c31eeSKalle Valo 
845d1a9421dSKalle Valo 	if (ar->fw_patch != NULL)
846772c31eeSKalle Valo 		return 0;
847772c31eeSKalle Valo 
848c0038972SKalle Valo 	if (ar->hw.fw.patch == NULL)
849d1a9421dSKalle Valo 		return 0;
850d1a9421dSKalle Valo 
851c0038972SKalle Valo 	snprintf(filename, sizeof(filename), "%s/%s",
852c0038972SKalle Valo 		 ar->hw.fw.dir, ar->hw.fw.patch);
853d1a9421dSKalle Valo 
854772c31eeSKalle Valo 	ret = ath6kl_get_fw(ar, filename, &ar->fw_patch,
855772c31eeSKalle Valo 			    &ar->fw_patch_len);
856772c31eeSKalle Valo 	if (ret) {
857772c31eeSKalle Valo 		ath6kl_err("Failed to get patch file %s: %d\n",
858772c31eeSKalle Valo 			   filename, ret);
859772c31eeSKalle Valo 		return ret;
860772c31eeSKalle Valo 	}
861772c31eeSKalle Valo 
862772c31eeSKalle Valo 	return 0;
863772c31eeSKalle Valo }
864772c31eeSKalle Valo 
865cd23c1c9SAlex Yang static int ath6kl_fetch_testscript_file(struct ath6kl *ar)
866cd23c1c9SAlex Yang {
867cd23c1c9SAlex Yang 	char filename[100];
868cd23c1c9SAlex Yang 	int ret;
869cd23c1c9SAlex Yang 
8705f1127ffSKalle Valo 	if (ar->testmode != 2)
871cd23c1c9SAlex Yang 		return 0;
872cd23c1c9SAlex Yang 
873cd23c1c9SAlex Yang 	if (ar->fw_testscript != NULL)
874cd23c1c9SAlex Yang 		return 0;
875cd23c1c9SAlex Yang 
876cd23c1c9SAlex Yang 	if (ar->hw.fw.testscript == NULL)
877cd23c1c9SAlex Yang 		return 0;
878cd23c1c9SAlex Yang 
879cd23c1c9SAlex Yang 	snprintf(filename, sizeof(filename), "%s/%s",
880cd23c1c9SAlex Yang 		 ar->hw.fw.dir, ar->hw.fw.testscript);
881cd23c1c9SAlex Yang 
882cd23c1c9SAlex Yang 	ret = ath6kl_get_fw(ar, filename, &ar->fw_testscript,
883cd23c1c9SAlex Yang 				&ar->fw_testscript_len);
884cd23c1c9SAlex Yang 	if (ret) {
885cd23c1c9SAlex Yang 		ath6kl_err("Failed to get testscript file %s: %d\n",
886cd23c1c9SAlex Yang 			   filename, ret);
887cd23c1c9SAlex Yang 		return ret;
888cd23c1c9SAlex Yang 	}
889cd23c1c9SAlex Yang 
890cd23c1c9SAlex Yang 	return 0;
891cd23c1c9SAlex Yang }
892cd23c1c9SAlex Yang 
89350d41234SKalle Valo static int ath6kl_fetch_fw_api1(struct ath6kl *ar)
894772c31eeSKalle Valo {
895772c31eeSKalle Valo 	int ret;
896772c31eeSKalle Valo 
897772c31eeSKalle Valo 	ret = ath6kl_fetch_otp_file(ar);
898772c31eeSKalle Valo 	if (ret)
899772c31eeSKalle Valo 		return ret;
900772c31eeSKalle Valo 
901772c31eeSKalle Valo 	ret = ath6kl_fetch_fw_file(ar);
902772c31eeSKalle Valo 	if (ret)
903772c31eeSKalle Valo 		return ret;
904772c31eeSKalle Valo 
905772c31eeSKalle Valo 	ret = ath6kl_fetch_patch_file(ar);
906772c31eeSKalle Valo 	if (ret)
907772c31eeSKalle Valo 		return ret;
908772c31eeSKalle Valo 
909cd23c1c9SAlex Yang 	ret = ath6kl_fetch_testscript_file(ar);
910cd23c1c9SAlex Yang 	if (ret)
911cd23c1c9SAlex Yang 		return ret;
912cd23c1c9SAlex Yang 
913772c31eeSKalle Valo 	return 0;
914772c31eeSKalle Valo }
915bdcd8170SKalle Valo 
91665a8b4ccSKalle Valo static int ath6kl_fetch_fw_apin(struct ath6kl *ar, const char *name)
91750d41234SKalle Valo {
91850d41234SKalle Valo 	size_t magic_len, len, ie_len;
91950d41234SKalle Valo 	const struct firmware *fw;
92050d41234SKalle Valo 	struct ath6kl_fw_ie *hdr;
921c0038972SKalle Valo 	char filename[100];
92250d41234SKalle Valo 	const u8 *data;
92397e0496dSKalle Valo 	int ret, ie_id, i, index, bit;
9248a137480SKalle Valo 	__le32 *val;
92550d41234SKalle Valo 
92665a8b4ccSKalle Valo 	snprintf(filename, sizeof(filename), "%s/%s", ar->hw.fw.dir, name);
92750d41234SKalle Valo 
92850d41234SKalle Valo 	ret = request_firmware(&fw, filename, ar->dev);
92950d41234SKalle Valo 	if (ret)
93050d41234SKalle Valo 		return ret;
93150d41234SKalle Valo 
93250d41234SKalle Valo 	data = fw->data;
93350d41234SKalle Valo 	len = fw->size;
93450d41234SKalle Valo 
93550d41234SKalle Valo 	/* magic also includes the null byte, check that as well */
93650d41234SKalle Valo 	magic_len = strlen(ATH6KL_FIRMWARE_MAGIC) + 1;
93750d41234SKalle Valo 
93850d41234SKalle Valo 	if (len < magic_len) {
93950d41234SKalle Valo 		ret = -EINVAL;
94050d41234SKalle Valo 		goto out;
94150d41234SKalle Valo 	}
94250d41234SKalle Valo 
94350d41234SKalle Valo 	if (memcmp(data, ATH6KL_FIRMWARE_MAGIC, magic_len) != 0) {
94450d41234SKalle Valo 		ret = -EINVAL;
94550d41234SKalle Valo 		goto out;
94650d41234SKalle Valo 	}
94750d41234SKalle Valo 
94850d41234SKalle Valo 	len -= magic_len;
94950d41234SKalle Valo 	data += magic_len;
95050d41234SKalle Valo 
95150d41234SKalle Valo 	/* loop elements */
95250d41234SKalle Valo 	while (len > sizeof(struct ath6kl_fw_ie)) {
95350d41234SKalle Valo 		/* hdr is unaligned! */
95450d41234SKalle Valo 		hdr = (struct ath6kl_fw_ie *) data;
95550d41234SKalle Valo 
95650d41234SKalle Valo 		ie_id = le32_to_cpup(&hdr->id);
95750d41234SKalle Valo 		ie_len = le32_to_cpup(&hdr->len);
95850d41234SKalle Valo 
95950d41234SKalle Valo 		len -= sizeof(*hdr);
96050d41234SKalle Valo 		data += sizeof(*hdr);
96150d41234SKalle Valo 
96250d41234SKalle Valo 		if (len < ie_len) {
96350d41234SKalle Valo 			ret = -EINVAL;
96450d41234SKalle Valo 			goto out;
96550d41234SKalle Valo 		}
96650d41234SKalle Valo 
96750d41234SKalle Valo 		switch (ie_id) {
968b5b6f6a9SNaveen Singh 		case ATH6KL_FW_IE_FW_VERSION:
969b5b6f6a9SNaveen Singh 			strlcpy(ar->wiphy->fw_version, data,
970b5b6f6a9SNaveen Singh 				sizeof(ar->wiphy->fw_version));
971b5b6f6a9SNaveen Singh 
972b5b6f6a9SNaveen Singh 			ath6kl_dbg(ATH6KL_DBG_BOOT,
973b5b6f6a9SNaveen Singh 				   "found fw version %s\n",
974b5b6f6a9SNaveen Singh 				    ar->wiphy->fw_version);
975b5b6f6a9SNaveen Singh 			break;
97650d41234SKalle Valo 		case ATH6KL_FW_IE_OTP_IMAGE:
977ef548626SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT, "found otp image ie (%zd B)\n",
9786bc36431SKalle Valo 				   ie_len);
9796bc36431SKalle Valo 
98050d41234SKalle Valo 			ar->fw_otp = kmemdup(data, ie_len, GFP_KERNEL);
98150d41234SKalle Valo 
98250d41234SKalle Valo 			if (ar->fw_otp == NULL) {
98350d41234SKalle Valo 				ret = -ENOMEM;
98450d41234SKalle Valo 				goto out;
98550d41234SKalle Valo 			}
98650d41234SKalle Valo 
98750d41234SKalle Valo 			ar->fw_otp_len = ie_len;
98850d41234SKalle Valo 			break;
98950d41234SKalle Valo 		case ATH6KL_FW_IE_FW_IMAGE:
990ef548626SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT, "found fw image ie (%zd B)\n",
9916bc36431SKalle Valo 				   ie_len);
9926bc36431SKalle Valo 
9935f1127ffSKalle Valo 			/* in testmode we already might have a fw file */
9945f1127ffSKalle Valo 			if (ar->fw != NULL)
9955f1127ffSKalle Valo 				break;
9965f1127ffSKalle Valo 
9978437754cSVivek Natarajan 			ar->fw = vmalloc(ie_len);
99850d41234SKalle Valo 
99950d41234SKalle Valo 			if (ar->fw == NULL) {
100050d41234SKalle Valo 				ret = -ENOMEM;
100150d41234SKalle Valo 				goto out;
100250d41234SKalle Valo 			}
100350d41234SKalle Valo 
10048437754cSVivek Natarajan 			memcpy(ar->fw, data, ie_len);
100550d41234SKalle Valo 			ar->fw_len = ie_len;
100650d41234SKalle Valo 			break;
100750d41234SKalle Valo 		case ATH6KL_FW_IE_PATCH_IMAGE:
1008ef548626SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT, "found patch image ie (%zd B)\n",
10096bc36431SKalle Valo 				   ie_len);
10106bc36431SKalle Valo 
101150d41234SKalle Valo 			ar->fw_patch = kmemdup(data, ie_len, GFP_KERNEL);
101250d41234SKalle Valo 
101350d41234SKalle Valo 			if (ar->fw_patch == NULL) {
101450d41234SKalle Valo 				ret = -ENOMEM;
101550d41234SKalle Valo 				goto out;
101650d41234SKalle Valo 			}
101750d41234SKalle Valo 
101850d41234SKalle Valo 			ar->fw_patch_len = ie_len;
101950d41234SKalle Valo 			break;
10208a137480SKalle Valo 		case ATH6KL_FW_IE_RESERVED_RAM_SIZE:
10218a137480SKalle Valo 			val = (__le32 *) data;
10228a137480SKalle Valo 			ar->hw.reserved_ram_size = le32_to_cpup(val);
10236bc36431SKalle Valo 
10246bc36431SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT,
10256bc36431SKalle Valo 				   "found reserved ram size ie 0x%d\n",
10266bc36431SKalle Valo 				   ar->hw.reserved_ram_size);
10278a137480SKalle Valo 			break;
102897e0496dSKalle Valo 		case ATH6KL_FW_IE_CAPABILITIES:
10296bc36431SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT,
1030ef548626SKalle Valo 				   "found firmware capabilities ie (%zd B)\n",
10316bc36431SKalle Valo 				   ie_len);
10326bc36431SKalle Valo 
103397e0496dSKalle Valo 			for (i = 0; i < ATH6KL_FW_CAPABILITY_MAX; i++) {
1034277d90f4SKalle Valo 				index = i / 8;
103597e0496dSKalle Valo 				bit = i % 8;
103697e0496dSKalle Valo 
1037c85251f8SThomas Pedersen 				if (index == ie_len)
1038c85251f8SThomas Pedersen 					break;
1039c85251f8SThomas Pedersen 
104097e0496dSKalle Valo 				if (data[index] & (1 << bit))
104197e0496dSKalle Valo 					__set_bit(i, ar->fw_capabilities);
104297e0496dSKalle Valo 			}
10436bc36431SKalle Valo 
10446bc36431SKalle Valo 			ath6kl_dbg_dump(ATH6KL_DBG_BOOT, "capabilities", "",
10456bc36431SKalle Valo 					ar->fw_capabilities,
10466bc36431SKalle Valo 					sizeof(ar->fw_capabilities));
104797e0496dSKalle Valo 			break;
10481b4304daSKalle Valo 		case ATH6KL_FW_IE_PATCH_ADDR:
10491b4304daSKalle Valo 			if (ie_len != sizeof(*val))
10501b4304daSKalle Valo 				break;
10511b4304daSKalle Valo 
10521b4304daSKalle Valo 			val = (__le32 *) data;
10531b4304daSKalle Valo 			ar->hw.dataset_patch_addr = le32_to_cpup(val);
10546bc36431SKalle Valo 
10556bc36431SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT,
105603ef0250SKalle Valo 				   "found patch address ie 0x%x\n",
10576bc36431SKalle Valo 				   ar->hw.dataset_patch_addr);
10581b4304daSKalle Valo 			break;
105903ef0250SKalle Valo 		case ATH6KL_FW_IE_BOARD_ADDR:
106003ef0250SKalle Valo 			if (ie_len != sizeof(*val))
106103ef0250SKalle Valo 				break;
106203ef0250SKalle Valo 
106303ef0250SKalle Valo 			val = (__le32 *) data;
106403ef0250SKalle Valo 			ar->hw.board_addr = le32_to_cpup(val);
106503ef0250SKalle Valo 
106603ef0250SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT,
106703ef0250SKalle Valo 				   "found board address ie 0x%x\n",
106803ef0250SKalle Valo 				   ar->hw.board_addr);
106903ef0250SKalle Valo 			break;
1070368b1b0fSKalle Valo 		case ATH6KL_FW_IE_VIF_MAX:
1071368b1b0fSKalle Valo 			if (ie_len != sizeof(*val))
1072368b1b0fSKalle Valo 				break;
1073368b1b0fSKalle Valo 
1074368b1b0fSKalle Valo 			val = (__le32 *) data;
1075368b1b0fSKalle Valo 			ar->vif_max = min_t(unsigned int, le32_to_cpup(val),
1076368b1b0fSKalle Valo 					    ATH6KL_VIF_MAX);
1077368b1b0fSKalle Valo 
1078f143379dSVasanthakumar Thiagarajan 			if (ar->vif_max > 1 && !ar->p2p)
1079f143379dSVasanthakumar Thiagarajan 				ar->max_norm_iface = 2;
1080f143379dSVasanthakumar Thiagarajan 
1081368b1b0fSKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT,
1082368b1b0fSKalle Valo 				   "found vif max ie %d\n", ar->vif_max);
1083368b1b0fSKalle Valo 			break;
108450d41234SKalle Valo 		default:
10856bc36431SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT, "Unknown fw ie: %u\n",
108650d41234SKalle Valo 				   le32_to_cpup(&hdr->id));
108750d41234SKalle Valo 			break;
108850d41234SKalle Valo 		}
108950d41234SKalle Valo 
109050d41234SKalle Valo 		len -= ie_len;
109150d41234SKalle Valo 		data += ie_len;
109250d41234SKalle Valo 	};
109350d41234SKalle Valo 
109450d41234SKalle Valo 	ret = 0;
109550d41234SKalle Valo out:
109650d41234SKalle Valo 	release_firmware(fw);
109750d41234SKalle Valo 
109850d41234SKalle Valo 	return ret;
109950d41234SKalle Valo }
110050d41234SKalle Valo 
110145eaa78fSKalle Valo int ath6kl_init_fetch_firmwares(struct ath6kl *ar)
110250d41234SKalle Valo {
110350d41234SKalle Valo 	int ret;
110450d41234SKalle Valo 
110550d41234SKalle Valo 	ret = ath6kl_fetch_board_file(ar);
110650d41234SKalle Valo 	if (ret)
110750d41234SKalle Valo 		return ret;
110850d41234SKalle Valo 
11095f1127ffSKalle Valo 	ret = ath6kl_fetch_testmode_file(ar);
11105f1127ffSKalle Valo 	if (ret)
11115f1127ffSKalle Valo 		return ret;
11125f1127ffSKalle Valo 
1113b1f47e3aSThomas Pedersen 	ret = ath6kl_fetch_fw_apin(ar, ATH6KL_FW_API4_FILE);
1114b1f47e3aSThomas Pedersen 	if (ret == 0) {
1115b1f47e3aSThomas Pedersen 		ar->fw_api = 4;
1116b1f47e3aSThomas Pedersen 		goto out;
1117b1f47e3aSThomas Pedersen 	}
1118b1f47e3aSThomas Pedersen 
111965a8b4ccSKalle Valo 	ret = ath6kl_fetch_fw_apin(ar, ATH6KL_FW_API3_FILE);
11206bc36431SKalle Valo 	if (ret == 0) {
112165a8b4ccSKalle Valo 		ar->fw_api = 3;
112265a8b4ccSKalle Valo 		goto out;
112365a8b4ccSKalle Valo 	}
112465a8b4ccSKalle Valo 
112565a8b4ccSKalle Valo 	ret = ath6kl_fetch_fw_apin(ar, ATH6KL_FW_API2_FILE);
112665a8b4ccSKalle Valo 	if (ret == 0) {
112765a8b4ccSKalle Valo 		ar->fw_api = 2;
112865a8b4ccSKalle Valo 		goto out;
11296bc36431SKalle Valo 	}
113050d41234SKalle Valo 
113150d41234SKalle Valo 	ret = ath6kl_fetch_fw_api1(ar);
113250d41234SKalle Valo 	if (ret)
113350d41234SKalle Valo 		return ret;
113450d41234SKalle Valo 
113565a8b4ccSKalle Valo 	ar->fw_api = 1;
113665a8b4ccSKalle Valo 
113765a8b4ccSKalle Valo out:
113865a8b4ccSKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "using fw api %d\n", ar->fw_api);
11396bc36431SKalle Valo 
114050d41234SKalle Valo 	return 0;
114150d41234SKalle Valo }
114250d41234SKalle Valo 
1143bdcd8170SKalle Valo static int ath6kl_upload_board_file(struct ath6kl *ar)
1144bdcd8170SKalle Valo {
1145bdcd8170SKalle Valo 	u32 board_address, board_ext_address, param;
114631024d99SKevin Fang 	u32 board_data_size, board_ext_data_size;
1147bdcd8170SKalle Valo 	int ret;
1148bdcd8170SKalle Valo 
1149772c31eeSKalle Valo 	if (WARN_ON(ar->fw_board == NULL))
1150772c31eeSKalle Valo 		return -ENOENT;
1151bdcd8170SKalle Valo 
115231024d99SKevin Fang 	/*
115331024d99SKevin Fang 	 * Determine where in Target RAM to write Board Data.
115431024d99SKevin Fang 	 * For AR6004, host determine Target RAM address for
115531024d99SKevin Fang 	 * writing board data.
115631024d99SKevin Fang 	 */
11570d4d72bfSKalle Valo 	if (ar->hw.board_addr != 0) {
1158b0fc7c1aSKalle Valo 		board_address = ar->hw.board_addr;
115924fc32b3SKalle Valo 		ath6kl_bmi_write_hi32(ar, hi_board_data,
1160b0fc7c1aSKalle Valo 				      board_address);
116131024d99SKevin Fang 	} else {
11621c3d95edSFrederic Danis 		ret = ath6kl_bmi_read_hi32(ar, hi_board_data, &board_address);
11631c3d95edSFrederic Danis 		if (ret) {
11641c3d95edSFrederic Danis 			ath6kl_err("Failed to get board file target address.\n");
11651c3d95edSFrederic Danis 			return ret;
11661c3d95edSFrederic Danis 		}
116731024d99SKevin Fang 	}
116831024d99SKevin Fang 
1169bdcd8170SKalle Valo 	/* determine where in target ram to write extended board data */
11701c3d95edSFrederic Danis 	ret = ath6kl_bmi_read_hi32(ar, hi_board_ext_data, &board_ext_address);
11711c3d95edSFrederic Danis 	if (ret) {
11721c3d95edSFrederic Danis 		ath6kl_err("Failed to get extended board file target address.\n");
11731c3d95edSFrederic Danis 		return ret;
11741c3d95edSFrederic Danis 	}
1175bdcd8170SKalle Valo 
117650e2740bSKalle Valo 	if (ar->target_type == TARGET_TYPE_AR6003 &&
117750e2740bSKalle Valo 	    board_ext_address == 0) {
1178bdcd8170SKalle Valo 		ath6kl_err("Failed to get board file target address.\n");
1179bdcd8170SKalle Valo 		return -EINVAL;
1180bdcd8170SKalle Valo 	}
1181bdcd8170SKalle Valo 
118231024d99SKevin Fang 	switch (ar->target_type) {
118331024d99SKevin Fang 	case TARGET_TYPE_AR6003:
118431024d99SKevin Fang 		board_data_size = AR6003_BOARD_DATA_SZ;
118531024d99SKevin Fang 		board_ext_data_size = AR6003_BOARD_EXT_DATA_SZ;
1186fb1ac2efSPrasanna Kumar 		if (ar->fw_board_len > (board_data_size + board_ext_data_size))
1187fb1ac2efSPrasanna Kumar 			board_ext_data_size = AR6003_BOARD_EXT_DATA_SZ_V2;
118831024d99SKevin Fang 		break;
118931024d99SKevin Fang 	case TARGET_TYPE_AR6004:
119031024d99SKevin Fang 		board_data_size = AR6004_BOARD_DATA_SZ;
119131024d99SKevin Fang 		board_ext_data_size = AR6004_BOARD_EXT_DATA_SZ;
119231024d99SKevin Fang 		break;
119331024d99SKevin Fang 	default:
119431024d99SKevin Fang 		WARN_ON(1);
119531024d99SKevin Fang 		return -EINVAL;
119631024d99SKevin Fang 		break;
119731024d99SKevin Fang 	}
119831024d99SKevin Fang 
119950e2740bSKalle Valo 	if (board_ext_address &&
120050e2740bSKalle Valo 	    ar->fw_board_len == (board_data_size + board_ext_data_size)) {
1201bdcd8170SKalle Valo 		/* write extended board data */
12026bc36431SKalle Valo 		ath6kl_dbg(ATH6KL_DBG_BOOT,
12036bc36431SKalle Valo 			   "writing extended board data to 0x%x (%d B)\n",
12046bc36431SKalle Valo 			   board_ext_address, board_ext_data_size);
12056bc36431SKalle Valo 
1206bdcd8170SKalle Valo 		ret = ath6kl_bmi_write(ar, board_ext_address,
120731024d99SKevin Fang 				       ar->fw_board + board_data_size,
120831024d99SKevin Fang 				       board_ext_data_size);
1209bdcd8170SKalle Valo 		if (ret) {
1210bdcd8170SKalle Valo 			ath6kl_err("Failed to write extended board data: %d\n",
1211bdcd8170SKalle Valo 				   ret);
1212bdcd8170SKalle Valo 			return ret;
1213bdcd8170SKalle Valo 		}
1214bdcd8170SKalle Valo 
1215bdcd8170SKalle Valo 		/* record that extended board data is initialized */
121631024d99SKevin Fang 		param = (board_ext_data_size << 16) | 1;
121731024d99SKevin Fang 
121824fc32b3SKalle Valo 		ath6kl_bmi_write_hi32(ar, hi_board_ext_data_config, param);
1219bdcd8170SKalle Valo 	}
1220bdcd8170SKalle Valo 
122131024d99SKevin Fang 	if (ar->fw_board_len < board_data_size) {
1222bdcd8170SKalle Valo 		ath6kl_err("Too small board file: %zu\n", ar->fw_board_len);
1223bdcd8170SKalle Valo 		ret = -EINVAL;
1224bdcd8170SKalle Valo 		return ret;
1225bdcd8170SKalle Valo 	}
1226bdcd8170SKalle Valo 
12276bc36431SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "writing board file to 0x%x (%d B)\n",
12286bc36431SKalle Valo 		   board_address, board_data_size);
12296bc36431SKalle Valo 
1230bdcd8170SKalle Valo 	ret = ath6kl_bmi_write(ar, board_address, ar->fw_board,
123131024d99SKevin Fang 			       board_data_size);
1232bdcd8170SKalle Valo 
1233bdcd8170SKalle Valo 	if (ret) {
1234bdcd8170SKalle Valo 		ath6kl_err("Board file bmi write failed: %d\n", ret);
1235bdcd8170SKalle Valo 		return ret;
1236bdcd8170SKalle Valo 	}
1237bdcd8170SKalle Valo 
1238bdcd8170SKalle Valo 	/* record the fact that Board Data IS initialized */
123924fc32b3SKalle Valo 	ath6kl_bmi_write_hi32(ar, hi_board_data_initialized, 1);
1240bdcd8170SKalle Valo 
1241bdcd8170SKalle Valo 	return ret;
1242bdcd8170SKalle Valo }
1243bdcd8170SKalle Valo 
1244bdcd8170SKalle Valo static int ath6kl_upload_otp(struct ath6kl *ar)
1245bdcd8170SKalle Valo {
1246bdcd8170SKalle Valo 	u32 address, param;
1247bef26a7fSKalle Valo 	bool from_hw = false;
1248bdcd8170SKalle Valo 	int ret;
1249bdcd8170SKalle Valo 
125050e2740bSKalle Valo 	if (ar->fw_otp == NULL)
125150e2740bSKalle Valo 		return 0;
1252bdcd8170SKalle Valo 
1253a01ac414SKalle Valo 	address = ar->hw.app_load_addr;
1254bdcd8170SKalle Valo 
1255ef548626SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "writing otp to 0x%x (%zd B)\n", address,
12566bc36431SKalle Valo 		   ar->fw_otp_len);
12576bc36431SKalle Valo 
1258bdcd8170SKalle Valo 	ret = ath6kl_bmi_fast_download(ar, address, ar->fw_otp,
1259bdcd8170SKalle Valo 				       ar->fw_otp_len);
1260bdcd8170SKalle Valo 	if (ret) {
1261bdcd8170SKalle Valo 		ath6kl_err("Failed to upload OTP file: %d\n", ret);
1262bdcd8170SKalle Valo 		return ret;
1263bdcd8170SKalle Valo 	}
1264bdcd8170SKalle Valo 
1265639d0b89SKalle Valo 	/* read firmware start address */
126680fb2686SKalle Valo 	ret = ath6kl_bmi_read_hi32(ar, hi_app_start, &address);
1267639d0b89SKalle Valo 
1268639d0b89SKalle Valo 	if (ret) {
1269639d0b89SKalle Valo 		ath6kl_err("Failed to read hi_app_start: %d\n", ret);
1270639d0b89SKalle Valo 		return ret;
1271639d0b89SKalle Valo 	}
1272639d0b89SKalle Valo 
1273bef26a7fSKalle Valo 	if (ar->hw.app_start_override_addr == 0) {
1274639d0b89SKalle Valo 		ar->hw.app_start_override_addr = address;
1275bef26a7fSKalle Valo 		from_hw = true;
1276bef26a7fSKalle Valo 	}
1277639d0b89SKalle Valo 
1278bef26a7fSKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "app_start_override_addr%s 0x%x\n",
1279bef26a7fSKalle Valo 		   from_hw ? " (from hw)" : "",
12806bc36431SKalle Valo 		   ar->hw.app_start_override_addr);
12816bc36431SKalle Valo 
1282bdcd8170SKalle Valo 	/* execute the OTP code */
1283bef26a7fSKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "executing OTP at 0x%x\n",
1284bef26a7fSKalle Valo 		   ar->hw.app_start_override_addr);
1285bdcd8170SKalle Valo 	param = 0;
1286bef26a7fSKalle Valo 	ath6kl_bmi_execute(ar, ar->hw.app_start_override_addr, &param);
1287bdcd8170SKalle Valo 
1288bdcd8170SKalle Valo 	return ret;
1289bdcd8170SKalle Valo }
1290bdcd8170SKalle Valo 
1291bdcd8170SKalle Valo static int ath6kl_upload_firmware(struct ath6kl *ar)
1292bdcd8170SKalle Valo {
1293bdcd8170SKalle Valo 	u32 address;
1294bdcd8170SKalle Valo 	int ret;
1295bdcd8170SKalle Valo 
1296772c31eeSKalle Valo 	if (WARN_ON(ar->fw == NULL))
129750e2740bSKalle Valo 		return 0;
1298bdcd8170SKalle Valo 
1299a01ac414SKalle Valo 	address = ar->hw.app_load_addr;
1300bdcd8170SKalle Valo 
1301ef548626SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "writing firmware to 0x%x (%zd B)\n",
13026bc36431SKalle Valo 		   address, ar->fw_len);
13036bc36431SKalle Valo 
1304bdcd8170SKalle Valo 	ret = ath6kl_bmi_fast_download(ar, address, ar->fw, ar->fw_len);
1305bdcd8170SKalle Valo 
1306bdcd8170SKalle Valo 	if (ret) {
1307bdcd8170SKalle Valo 		ath6kl_err("Failed to write firmware: %d\n", ret);
1308bdcd8170SKalle Valo 		return ret;
1309bdcd8170SKalle Valo 	}
1310bdcd8170SKalle Valo 
131131024d99SKevin Fang 	/*
131231024d99SKevin Fang 	 * Set starting address for firmware
131331024d99SKevin Fang 	 * Don't need to setup app_start override addr on AR6004
131431024d99SKevin Fang 	 */
131531024d99SKevin Fang 	if (ar->target_type != TARGET_TYPE_AR6004) {
1316a01ac414SKalle Valo 		address = ar->hw.app_start_override_addr;
1317bdcd8170SKalle Valo 		ath6kl_bmi_set_app_start(ar, address);
131831024d99SKevin Fang 	}
1319bdcd8170SKalle Valo 	return ret;
1320bdcd8170SKalle Valo }
1321bdcd8170SKalle Valo 
1322bdcd8170SKalle Valo static int ath6kl_upload_patch(struct ath6kl *ar)
1323bdcd8170SKalle Valo {
132424fc32b3SKalle Valo 	u32 address;
1325bdcd8170SKalle Valo 	int ret;
1326bdcd8170SKalle Valo 
132750e2740bSKalle Valo 	if (ar->fw_patch == NULL)
132850e2740bSKalle Valo 		return 0;
1329bdcd8170SKalle Valo 
1330a01ac414SKalle Valo 	address = ar->hw.dataset_patch_addr;
1331bdcd8170SKalle Valo 
1332ef548626SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "writing patch to 0x%x (%zd B)\n",
13336bc36431SKalle Valo 		   address, ar->fw_patch_len);
13346bc36431SKalle Valo 
1335bdcd8170SKalle Valo 	ret = ath6kl_bmi_write(ar, address, ar->fw_patch, ar->fw_patch_len);
1336bdcd8170SKalle Valo 	if (ret) {
1337bdcd8170SKalle Valo 		ath6kl_err("Failed to write patch file: %d\n", ret);
1338bdcd8170SKalle Valo 		return ret;
1339bdcd8170SKalle Valo 	}
1340bdcd8170SKalle Valo 
134124fc32b3SKalle Valo 	ath6kl_bmi_write_hi32(ar, hi_dset_list_head, address);
1342bdcd8170SKalle Valo 
1343bdcd8170SKalle Valo 	return 0;
1344bdcd8170SKalle Valo }
1345bdcd8170SKalle Valo 
1346cd23c1c9SAlex Yang static int ath6kl_upload_testscript(struct ath6kl *ar)
1347cd23c1c9SAlex Yang {
134824fc32b3SKalle Valo 	u32 address;
1349cd23c1c9SAlex Yang 	int ret;
1350cd23c1c9SAlex Yang 
13515f1127ffSKalle Valo 	if (ar->testmode != 2)
1352cd23c1c9SAlex Yang 		return 0;
1353cd23c1c9SAlex Yang 
1354cd23c1c9SAlex Yang 	if (ar->fw_testscript == NULL)
1355cd23c1c9SAlex Yang 		return 0;
1356cd23c1c9SAlex Yang 
1357cd23c1c9SAlex Yang 	address = ar->hw.testscript_addr;
1358cd23c1c9SAlex Yang 
1359cd23c1c9SAlex Yang 	ath6kl_dbg(ATH6KL_DBG_BOOT, "writing testscript to 0x%x (%zd B)\n",
1360cd23c1c9SAlex Yang 		   address, ar->fw_testscript_len);
1361cd23c1c9SAlex Yang 
1362cd23c1c9SAlex Yang 	ret = ath6kl_bmi_write(ar, address, ar->fw_testscript,
1363cd23c1c9SAlex Yang 		ar->fw_testscript_len);
1364cd23c1c9SAlex Yang 	if (ret) {
1365cd23c1c9SAlex Yang 		ath6kl_err("Failed to write testscript file: %d\n", ret);
1366cd23c1c9SAlex Yang 		return ret;
1367cd23c1c9SAlex Yang 	}
1368cd23c1c9SAlex Yang 
136924fc32b3SKalle Valo 	ath6kl_bmi_write_hi32(ar, hi_ota_testscript, address);
137024fc32b3SKalle Valo 	ath6kl_bmi_write_hi32(ar, hi_end_ram_reserve_sz, 4096);
137124fc32b3SKalle Valo 	ath6kl_bmi_write_hi32(ar, hi_test_apps_related, 1);
1372cd23c1c9SAlex Yang 
1373cd23c1c9SAlex Yang 	return 0;
1374cd23c1c9SAlex Yang }
1375cd23c1c9SAlex Yang 
1376bdcd8170SKalle Valo static int ath6kl_init_upload(struct ath6kl *ar)
1377bdcd8170SKalle Valo {
1378bdcd8170SKalle Valo 	u32 param, options, sleep, address;
1379bdcd8170SKalle Valo 	int status = 0;
1380bdcd8170SKalle Valo 
138131024d99SKevin Fang 	if (ar->target_type != TARGET_TYPE_AR6003 &&
138231024d99SKevin Fang 	    ar->target_type != TARGET_TYPE_AR6004)
1383bdcd8170SKalle Valo 		return -EINVAL;
1384bdcd8170SKalle Valo 
1385bdcd8170SKalle Valo 	/* temporarily disable system sleep */
1386bdcd8170SKalle Valo 	address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
1387bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_read(ar, address, &param);
1388bdcd8170SKalle Valo 	if (status)
1389bdcd8170SKalle Valo 		return status;
1390bdcd8170SKalle Valo 
1391bdcd8170SKalle Valo 	options = param;
1392bdcd8170SKalle Valo 
1393bdcd8170SKalle Valo 	param |= ATH6KL_OPTION_SLEEP_DISABLE;
1394bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_write(ar, address, param);
1395bdcd8170SKalle Valo 	if (status)
1396bdcd8170SKalle Valo 		return status;
1397bdcd8170SKalle Valo 
1398bdcd8170SKalle Valo 	address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1399bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_read(ar, address, &param);
1400bdcd8170SKalle Valo 	if (status)
1401bdcd8170SKalle Valo 		return status;
1402bdcd8170SKalle Valo 
1403bdcd8170SKalle Valo 	sleep = param;
1404bdcd8170SKalle Valo 
1405bdcd8170SKalle Valo 	param |= SM(SYSTEM_SLEEP_DISABLE, 1);
1406bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_write(ar, address, param);
1407bdcd8170SKalle Valo 	if (status)
1408bdcd8170SKalle Valo 		return status;
1409bdcd8170SKalle Valo 
1410bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_TRC, "old options: %d, old sleep: %d\n",
1411bdcd8170SKalle Valo 		   options, sleep);
1412bdcd8170SKalle Valo 
1413bdcd8170SKalle Valo 	/* program analog PLL register */
141431024d99SKevin Fang 	/* no need to control 40/44MHz clock on AR6004 */
141531024d99SKevin Fang 	if (ar->target_type != TARGET_TYPE_AR6004) {
1416bdcd8170SKalle Valo 		status = ath6kl_bmi_reg_write(ar, ATH6KL_ANALOG_PLL_REGISTER,
1417bdcd8170SKalle Valo 					      0xF9104001);
141831024d99SKevin Fang 
1419bdcd8170SKalle Valo 		if (status)
1420bdcd8170SKalle Valo 			return status;
1421bdcd8170SKalle Valo 
1422bdcd8170SKalle Valo 		/* Run at 80/88MHz by default */
1423bdcd8170SKalle Valo 		param = SM(CPU_CLOCK_STANDARD, 1);
1424bdcd8170SKalle Valo 
1425bdcd8170SKalle Valo 		address = RTC_BASE_ADDRESS + CPU_CLOCK_ADDRESS;
1426bdcd8170SKalle Valo 		status = ath6kl_bmi_reg_write(ar, address, param);
1427bdcd8170SKalle Valo 		if (status)
1428bdcd8170SKalle Valo 			return status;
142931024d99SKevin Fang 	}
1430bdcd8170SKalle Valo 
1431bdcd8170SKalle Valo 	param = 0;
1432bdcd8170SKalle Valo 	address = RTC_BASE_ADDRESS + LPO_CAL_ADDRESS;
1433bdcd8170SKalle Valo 	param = SM(LPO_CAL_ENABLE, 1);
1434bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_write(ar, address, param);
1435bdcd8170SKalle Valo 	if (status)
1436bdcd8170SKalle Valo 		return status;
1437bdcd8170SKalle Valo 
1438bdcd8170SKalle Valo 	/* WAR to avoid SDIO CRC err */
1439a2e1be33SMohammed Shafi Shajakhan 	if (ar->hw.flags & ATH6KL_HW_SDIO_CRC_ERROR_WAR) {
1440bdcd8170SKalle Valo 		ath6kl_err("temporary war to avoid sdio crc error\n");
1441bdcd8170SKalle Valo 
1442fa338be0SVasanthakumar Thiagarajan 		param = 0x28;
1443fa338be0SVasanthakumar Thiagarajan 		address = GPIO_BASE_ADDRESS + GPIO_PIN9_ADDRESS;
1444fa338be0SVasanthakumar Thiagarajan 		status = ath6kl_bmi_reg_write(ar, address, param);
1445fa338be0SVasanthakumar Thiagarajan 		if (status)
1446fa338be0SVasanthakumar Thiagarajan 			return status;
1447fa338be0SVasanthakumar Thiagarajan 
1448bdcd8170SKalle Valo 		param = 0x20;
1449bdcd8170SKalle Valo 
1450bdcd8170SKalle Valo 		address = GPIO_BASE_ADDRESS + GPIO_PIN10_ADDRESS;
1451bdcd8170SKalle Valo 		status = ath6kl_bmi_reg_write(ar, address, param);
1452bdcd8170SKalle Valo 		if (status)
1453bdcd8170SKalle Valo 			return status;
1454bdcd8170SKalle Valo 
1455bdcd8170SKalle Valo 		address = GPIO_BASE_ADDRESS + GPIO_PIN11_ADDRESS;
1456bdcd8170SKalle Valo 		status = ath6kl_bmi_reg_write(ar, address, param);
1457bdcd8170SKalle Valo 		if (status)
1458bdcd8170SKalle Valo 			return status;
1459bdcd8170SKalle Valo 
1460bdcd8170SKalle Valo 		address = GPIO_BASE_ADDRESS + GPIO_PIN12_ADDRESS;
1461bdcd8170SKalle Valo 		status = ath6kl_bmi_reg_write(ar, address, param);
1462bdcd8170SKalle Valo 		if (status)
1463bdcd8170SKalle Valo 			return status;
1464bdcd8170SKalle Valo 
1465bdcd8170SKalle Valo 		address = GPIO_BASE_ADDRESS + GPIO_PIN13_ADDRESS;
1466bdcd8170SKalle Valo 		status = ath6kl_bmi_reg_write(ar, address, param);
1467bdcd8170SKalle Valo 		if (status)
1468bdcd8170SKalle Valo 			return status;
1469bdcd8170SKalle Valo 	}
1470bdcd8170SKalle Valo 
1471bdcd8170SKalle Valo 	/* write EEPROM data to Target RAM */
1472bdcd8170SKalle Valo 	status = ath6kl_upload_board_file(ar);
1473bdcd8170SKalle Valo 	if (status)
1474bdcd8170SKalle Valo 		return status;
1475bdcd8170SKalle Valo 
1476bdcd8170SKalle Valo 	/* transfer One time Programmable data */
1477bdcd8170SKalle Valo 	status = ath6kl_upload_otp(ar);
1478bdcd8170SKalle Valo 	if (status)
1479bdcd8170SKalle Valo 		return status;
1480bdcd8170SKalle Valo 
1481bdcd8170SKalle Valo 	/* Download Target firmware */
1482bdcd8170SKalle Valo 	status = ath6kl_upload_firmware(ar);
1483bdcd8170SKalle Valo 	if (status)
1484bdcd8170SKalle Valo 		return status;
1485bdcd8170SKalle Valo 
1486bdcd8170SKalle Valo 	status = ath6kl_upload_patch(ar);
1487bdcd8170SKalle Valo 	if (status)
1488bdcd8170SKalle Valo 		return status;
1489bdcd8170SKalle Valo 
1490cd23c1c9SAlex Yang 	/* Download the test script */
1491cd23c1c9SAlex Yang 	status = ath6kl_upload_testscript(ar);
1492cd23c1c9SAlex Yang 	if (status)
1493cd23c1c9SAlex Yang 		return status;
1494cd23c1c9SAlex Yang 
1495bdcd8170SKalle Valo 	/* Restore system sleep */
1496bdcd8170SKalle Valo 	address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1497bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_write(ar, address, sleep);
1498bdcd8170SKalle Valo 	if (status)
1499bdcd8170SKalle Valo 		return status;
1500bdcd8170SKalle Valo 
1501bdcd8170SKalle Valo 	address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
1502bdcd8170SKalle Valo 	param = options | 0x20;
1503bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_write(ar, address, param);
1504bdcd8170SKalle Valo 	if (status)
1505bdcd8170SKalle Valo 		return status;
1506bdcd8170SKalle Valo 
1507bdcd8170SKalle Valo 	return status;
1508bdcd8170SKalle Valo }
1509bdcd8170SKalle Valo 
151045eaa78fSKalle Valo int ath6kl_init_hw_params(struct ath6kl *ar)
1511a01ac414SKalle Valo {
15121b46dc04SKalle Valo 	const struct ath6kl_hw *uninitialized_var(hw);
1513856f4b31SKalle Valo 	int i;
1514bef26a7fSKalle Valo 
1515856f4b31SKalle Valo 	for (i = 0; i < ARRAY_SIZE(hw_list); i++) {
1516856f4b31SKalle Valo 		hw = &hw_list[i];
1517bef26a7fSKalle Valo 
1518856f4b31SKalle Valo 		if (hw->id == ar->version.target_ver)
1519a01ac414SKalle Valo 			break;
1520856f4b31SKalle Valo 	}
1521856f4b31SKalle Valo 
1522856f4b31SKalle Valo 	if (i == ARRAY_SIZE(hw_list)) {
1523a01ac414SKalle Valo 		ath6kl_err("Unsupported hardware version: 0x%x\n",
1524a01ac414SKalle Valo 			   ar->version.target_ver);
1525a01ac414SKalle Valo 		return -EINVAL;
1526a01ac414SKalle Valo 	}
1527a01ac414SKalle Valo 
1528856f4b31SKalle Valo 	ar->hw = *hw;
1529856f4b31SKalle Valo 
15306bc36431SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT,
15316bc36431SKalle Valo 		   "target_ver 0x%x target_type 0x%x dataset_patch 0x%x app_load_addr 0x%x\n",
15326bc36431SKalle Valo 		   ar->version.target_ver, ar->target_type,
15336bc36431SKalle Valo 		   ar->hw.dataset_patch_addr, ar->hw.app_load_addr);
15346bc36431SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT,
15356bc36431SKalle Valo 		   "app_start_override_addr 0x%x board_ext_data_addr 0x%x reserved_ram_size 0x%x",
15366bc36431SKalle Valo 		   ar->hw.app_start_override_addr, ar->hw.board_ext_data_addr,
15376bc36431SKalle Valo 		   ar->hw.reserved_ram_size);
153839586bf2SRyan Hsu 	ath6kl_dbg(ATH6KL_DBG_BOOT,
153939586bf2SRyan Hsu 		   "refclk_hz %d uarttx_pin %d",
154039586bf2SRyan Hsu 		   ar->hw.refclk_hz, ar->hw.uarttx_pin);
15416bc36431SKalle Valo 
1542a01ac414SKalle Valo 	return 0;
1543a01ac414SKalle Valo }
1544a01ac414SKalle Valo 
1545293badf4SKalle Valo static const char *ath6kl_init_get_hif_name(enum ath6kl_hif_type type)
1546293badf4SKalle Valo {
1547293badf4SKalle Valo 	switch (type) {
1548293badf4SKalle Valo 	case ATH6KL_HIF_TYPE_SDIO:
1549293badf4SKalle Valo 		return "sdio";
1550293badf4SKalle Valo 	case ATH6KL_HIF_TYPE_USB:
1551293badf4SKalle Valo 		return "usb";
1552293badf4SKalle Valo 	}
1553293badf4SKalle Valo 
1554293badf4SKalle Valo 	return NULL;
1555293badf4SKalle Valo }
1556293badf4SKalle Valo 
1557e72c2746SKalle Valo 
1558e72c2746SKalle Valo static const struct fw_capa_str_map {
1559e72c2746SKalle Valo 	int id;
1560e72c2746SKalle Valo 	const char *name;
1561e72c2746SKalle Valo } fw_capa_map[] = {
1562e72c2746SKalle Valo 	{ ATH6KL_FW_CAPABILITY_HOST_P2P, "host-p2p" },
1563e72c2746SKalle Valo 	{ ATH6KL_FW_CAPABILITY_SCHED_SCAN, "sched-scan" },
1564e72c2746SKalle Valo 	{ ATH6KL_FW_CAPABILITY_STA_P2PDEV_DUPLEX, "sta-p2pdev-duplex" },
1565e72c2746SKalle Valo 	{ ATH6KL_FW_CAPABILITY_INACTIVITY_TIMEOUT, "inactivity-timeout" },
1566e72c2746SKalle Valo 	{ ATH6KL_FW_CAPABILITY_RSN_CAP_OVERRIDE, "rsn-cap-override" },
1567e72c2746SKalle Valo 	{ ATH6KL_FW_CAPABILITY_WOW_MULTICAST_FILTER, "wow-mc-filter" },
1568e72c2746SKalle Valo 	{ ATH6KL_FW_CAPABILITY_BMISS_ENHANCE, "bmiss-enhance" },
1569e72c2746SKalle Valo 	{ ATH6KL_FW_CAPABILITY_SCHED_SCAN_MATCH_LIST, "sscan-match-list" },
1570e72c2746SKalle Valo 	{ ATH6KL_FW_CAPABILITY_RSSI_SCAN_THOLD, "rssi-scan-thold" },
1571e72c2746SKalle Valo 	{ ATH6KL_FW_CAPABILITY_CUSTOM_MAC_ADDR, "custom-mac-addr" },
1572e72c2746SKalle Valo 	{ ATH6KL_FW_CAPABILITY_TX_ERR_NOTIFY, "tx-err-notify" },
1573e72c2746SKalle Valo 	{ ATH6KL_FW_CAPABILITY_REGDOMAIN, "regdomain" },
1574e72c2746SKalle Valo 	{ ATH6KL_FW_CAPABILITY_SCHED_SCAN_V2, "sched-scan-v2" },
1575e72c2746SKalle Valo 	{ ATH6KL_FW_CAPABILITY_HEART_BEAT_POLL, "hb-poll" },
1576eba95bceSKalle Valo 	{ ATH6KL_FW_CAPABILITY_64BIT_RATES, "64bit-rates" },
1577eba95bceSKalle Valo 	{ ATH6KL_FW_CAPABILITY_AP_INACTIVITY_MINS, "ap-inactivity-mins" },
1578eba95bceSKalle Valo 	{ ATH6KL_FW_CAPABILITY_MAP_LP_ENDPOINT, "map-lp-endpoint" },
1579e72c2746SKalle Valo };
1580e72c2746SKalle Valo 
1581e72c2746SKalle Valo static const char *ath6kl_init_get_fw_capa_name(unsigned int id)
1582e72c2746SKalle Valo {
1583e72c2746SKalle Valo 	int i;
1584e72c2746SKalle Valo 
1585e72c2746SKalle Valo 	for (i = 0; i < ARRAY_SIZE(fw_capa_map); i++) {
1586e72c2746SKalle Valo 		if (fw_capa_map[i].id == id)
1587e72c2746SKalle Valo 			return fw_capa_map[i].name;
1588e72c2746SKalle Valo 	}
1589e72c2746SKalle Valo 
1590e72c2746SKalle Valo 	return "<unknown>";
1591e72c2746SKalle Valo }
1592e72c2746SKalle Valo 
1593e72c2746SKalle Valo static void ath6kl_init_get_fwcaps(struct ath6kl *ar, char *buf, size_t buf_len)
1594e72c2746SKalle Valo {
1595e72c2746SKalle Valo 	u8 *data = (u8 *) ar->fw_capabilities;
1596e72c2746SKalle Valo 	size_t trunc_len, len = 0;
1597e72c2746SKalle Valo 	int i, index, bit;
1598e72c2746SKalle Valo 	char *trunc = "...";
1599e72c2746SKalle Valo 
1600e72c2746SKalle Valo 	for (i = 0; i < ATH6KL_FW_CAPABILITY_MAX; i++) {
1601e72c2746SKalle Valo 		index = i / 8;
1602e72c2746SKalle Valo 		bit = i % 8;
1603e72c2746SKalle Valo 
1604e72c2746SKalle Valo 		if (index >= sizeof(ar->fw_capabilities) * 4)
1605e72c2746SKalle Valo 			break;
1606e72c2746SKalle Valo 
1607e72c2746SKalle Valo 		if (buf_len - len < 4) {
1608e72c2746SKalle Valo 			ath6kl_warn("firmware capability buffer too small!\n");
1609e72c2746SKalle Valo 
1610e72c2746SKalle Valo 			/* add "..." to the end of string */
1611e72c2746SKalle Valo 			trunc_len = strlen(trunc) + 1;
1612e72c2746SKalle Valo 			strncpy(buf + buf_len - trunc_len, trunc, trunc_len);
1613e72c2746SKalle Valo 
1614e72c2746SKalle Valo 			return;
1615e72c2746SKalle Valo 		}
1616e72c2746SKalle Valo 
1617e72c2746SKalle Valo 		if (data[index] & (1 << bit)) {
1618e72c2746SKalle Valo 			len += scnprintf(buf + len, buf_len - len, "%s,",
1619e72c2746SKalle Valo 					    ath6kl_init_get_fw_capa_name(i));
1620e72c2746SKalle Valo 		}
1621e72c2746SKalle Valo 	}
1622e72c2746SKalle Valo 
1623e72c2746SKalle Valo 	/* overwrite the last comma */
1624e72c2746SKalle Valo 	if (len > 0)
1625e72c2746SKalle Valo 		len--;
1626e72c2746SKalle Valo 
1627e72c2746SKalle Valo 	buf[len] = '\0';
1628e72c2746SKalle Valo }
1629e72c2746SKalle Valo 
1630ec1461dcSKalle Valo static int ath6kl_init_hw_reset(struct ath6kl *ar)
1631ec1461dcSKalle Valo {
1632ec1461dcSKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "cold resetting the device");
1633ec1461dcSKalle Valo 
1634ec1461dcSKalle Valo 	return ath6kl_diag_write32(ar, RESET_CONTROL_ADDRESS,
1635ec1461dcSKalle Valo 				   cpu_to_le32(RESET_CONTROL_COLD_RST));
1636ec1461dcSKalle Valo }
1637ec1461dcSKalle Valo 
1638ede615d2SVasanthakumar Thiagarajan static int __ath6kl_init_hw_start(struct ath6kl *ar)
163920459ee2SKalle Valo {
164020459ee2SKalle Valo 	long timeleft;
164120459ee2SKalle Valo 	int ret, i;
1642e72c2746SKalle Valo 	char buf[200];
164320459ee2SKalle Valo 
16445fe4dffbSKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "hw start\n");
16455fe4dffbSKalle Valo 
164620459ee2SKalle Valo 	ret = ath6kl_hif_power_on(ar);
164720459ee2SKalle Valo 	if (ret)
164820459ee2SKalle Valo 		return ret;
164920459ee2SKalle Valo 
165020459ee2SKalle Valo 	ret = ath6kl_configure_target(ar);
165120459ee2SKalle Valo 	if (ret)
165220459ee2SKalle Valo 		goto err_power_off;
165320459ee2SKalle Valo 
165420459ee2SKalle Valo 	ret = ath6kl_init_upload(ar);
165520459ee2SKalle Valo 	if (ret)
165620459ee2SKalle Valo 		goto err_power_off;
165720459ee2SKalle Valo 
165820459ee2SKalle Valo 	/* Do we need to finish the BMI phase */
1659bf978145SMohammed Shafi Shajakhan 	ret = ath6kl_bmi_done(ar);
1660bf978145SMohammed Shafi Shajakhan 	if (ret)
166120459ee2SKalle Valo 		goto err_power_off;
166220459ee2SKalle Valo 
166320459ee2SKalle Valo 	/*
166420459ee2SKalle Valo 	 * The reason we have to wait for the target here is that the
166520459ee2SKalle Valo 	 * driver layer has to init BMI in order to set the host block
166620459ee2SKalle Valo 	 * size.
166720459ee2SKalle Valo 	 */
16684e1609c9SKalle Valo 	ret = ath6kl_htc_wait_target(ar->htc_target);
166944af3442SKalle Valo 
167044af3442SKalle Valo 	if (ret == -ETIMEDOUT) {
167144af3442SKalle Valo 		/*
167244af3442SKalle Valo 		 * Most likely USB target is in odd state after reboot and
167344af3442SKalle Valo 		 * needs a reset. A cold reset makes the whole device
167444af3442SKalle Valo 		 * disappear from USB bus and initialisation starts from
167544af3442SKalle Valo 		 * beginning.
167644af3442SKalle Valo 		 */
167744af3442SKalle Valo 		ath6kl_warn("htc wait target timed out, resetting device\n");
167844af3442SKalle Valo 		ath6kl_init_hw_reset(ar);
167944af3442SKalle Valo 		goto err_power_off;
168044af3442SKalle Valo 	} else if (ret) {
16814e1609c9SKalle Valo 		ath6kl_err("htc wait target failed: %d\n", ret);
168220459ee2SKalle Valo 		goto err_power_off;
168320459ee2SKalle Valo 	}
168420459ee2SKalle Valo 
16854e1609c9SKalle Valo 	ret = ath6kl_init_service_ep(ar);
16864e1609c9SKalle Valo 	if (ret) {
16874e1609c9SKalle Valo 		ath6kl_err("Endpoint service initilisation failed: %d\n", ret);
168820459ee2SKalle Valo 		goto err_cleanup_scatter;
168920459ee2SKalle Valo 	}
169020459ee2SKalle Valo 
169120459ee2SKalle Valo 	/* setup credit distribution */
1692e76ac2bfSKalle Valo 	ath6kl_htc_credit_setup(ar->htc_target, &ar->credit_state_info);
169320459ee2SKalle Valo 
169420459ee2SKalle Valo 	/* start HTC */
169520459ee2SKalle Valo 	ret = ath6kl_htc_start(ar->htc_target);
169620459ee2SKalle Valo 	if (ret) {
169720459ee2SKalle Valo 		/* FIXME: call this */
169820459ee2SKalle Valo 		ath6kl_cookie_cleanup(ar);
169920459ee2SKalle Valo 		goto err_cleanup_scatter;
170020459ee2SKalle Valo 	}
170120459ee2SKalle Valo 
170220459ee2SKalle Valo 	/* Wait for Wmi event to be ready */
170320459ee2SKalle Valo 	timeleft = wait_event_interruptible_timeout(ar->event_wq,
170420459ee2SKalle Valo 						    test_bit(WMI_READY,
170520459ee2SKalle Valo 							     &ar->flag),
170620459ee2SKalle Valo 						    WMI_TIMEOUT);
1707ab1ef141SRaja Mani 	if (timeleft <= 0) {
1708ab1ef141SRaja Mani 		clear_bit(WMI_READY, &ar->flag);
1709ab1ef141SRaja Mani 		ath6kl_err("wmi is not ready or wait was interrupted: %ld\n",
1710ab1ef141SRaja Mani 			   timeleft);
1711ab1ef141SRaja Mani 		ret = -EIO;
1712ab1ef141SRaja Mani 		goto err_htc_stop;
1713ab1ef141SRaja Mani 	}
171420459ee2SKalle Valo 
171520459ee2SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "firmware booted\n");
171620459ee2SKalle Valo 
1717293badf4SKalle Valo 	if (test_and_clear_bit(FIRST_BOOT, &ar->flag)) {
171865a8b4ccSKalle Valo 		ath6kl_info("%s %s fw %s api %d%s\n",
1719293badf4SKalle Valo 			    ar->hw.name,
1720293badf4SKalle Valo 			    ath6kl_init_get_hif_name(ar->hif_type),
1721293badf4SKalle Valo 			    ar->wiphy->fw_version,
172265a8b4ccSKalle Valo 			    ar->fw_api,
1723293badf4SKalle Valo 			    test_bit(TESTMODE, &ar->flag) ? " testmode" : "");
1724e72c2746SKalle Valo 		ath6kl_init_get_fwcaps(ar, buf, sizeof(buf));
1725e72c2746SKalle Valo 		ath6kl_info("firmware supports: %s\n", buf);
1726293badf4SKalle Valo 	}
1727293badf4SKalle Valo 
172820459ee2SKalle Valo 	if (ar->version.abi_ver != ATH6KL_ABI_VERSION) {
172920459ee2SKalle Valo 		ath6kl_err("abi version mismatch: host(0x%x), target(0x%x)\n",
173020459ee2SKalle Valo 			   ATH6KL_ABI_VERSION, ar->version.abi_ver);
173120459ee2SKalle Valo 		ret = -EIO;
173220459ee2SKalle Valo 		goto err_htc_stop;
173320459ee2SKalle Valo 	}
173420459ee2SKalle Valo 
173520459ee2SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_TRC, "%s: wmi is ready\n", __func__);
173620459ee2SKalle Valo 
173720459ee2SKalle Valo 	/* communicate the wmi protocol verision to the target */
173820459ee2SKalle Valo 	/* FIXME: return error */
173920459ee2SKalle Valo 	if ((ath6kl_set_host_app_area(ar)) != 0)
174020459ee2SKalle Valo 		ath6kl_err("unable to set the host app area\n");
174120459ee2SKalle Valo 
174271f96ee6SKalle Valo 	for (i = 0; i < ar->vif_max; i++) {
174320459ee2SKalle Valo 		ret = ath6kl_target_config_wlan_params(ar, i);
174420459ee2SKalle Valo 		if (ret)
174520459ee2SKalle Valo 			goto err_htc_stop;
174620459ee2SKalle Valo 	}
174720459ee2SKalle Valo 
174820459ee2SKalle Valo 	return 0;
174920459ee2SKalle Valo 
175020459ee2SKalle Valo err_htc_stop:
175120459ee2SKalle Valo 	ath6kl_htc_stop(ar->htc_target);
175220459ee2SKalle Valo err_cleanup_scatter:
175320459ee2SKalle Valo 	ath6kl_hif_cleanup_scatter(ar);
175420459ee2SKalle Valo err_power_off:
175520459ee2SKalle Valo 	ath6kl_hif_power_off(ar);
175620459ee2SKalle Valo 
175720459ee2SKalle Valo 	return ret;
175820459ee2SKalle Valo }
175920459ee2SKalle Valo 
1760ede615d2SVasanthakumar Thiagarajan int ath6kl_init_hw_start(struct ath6kl *ar)
1761ede615d2SVasanthakumar Thiagarajan {
1762ede615d2SVasanthakumar Thiagarajan 	int err;
1763ede615d2SVasanthakumar Thiagarajan 
1764ede615d2SVasanthakumar Thiagarajan 	err = __ath6kl_init_hw_start(ar);
1765ede615d2SVasanthakumar Thiagarajan 	if (err)
1766ede615d2SVasanthakumar Thiagarajan 		return err;
1767ede615d2SVasanthakumar Thiagarajan 	ar->state = ATH6KL_STATE_ON;
1768ede615d2SVasanthakumar Thiagarajan 	return 0;
1769ede615d2SVasanthakumar Thiagarajan }
1770ede615d2SVasanthakumar Thiagarajan 
1771ede615d2SVasanthakumar Thiagarajan static int __ath6kl_init_hw_stop(struct ath6kl *ar)
17725fe4dffbSKalle Valo {
17735fe4dffbSKalle Valo 	int ret;
17745fe4dffbSKalle Valo 
17755fe4dffbSKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "hw stop\n");
17765fe4dffbSKalle Valo 
17775fe4dffbSKalle Valo 	ath6kl_htc_stop(ar->htc_target);
17785fe4dffbSKalle Valo 
17795fe4dffbSKalle Valo 	ath6kl_hif_stop(ar);
17805fe4dffbSKalle Valo 
17815fe4dffbSKalle Valo 	ath6kl_bmi_reset(ar);
17825fe4dffbSKalle Valo 
17835fe4dffbSKalle Valo 	ret = ath6kl_hif_power_off(ar);
17845fe4dffbSKalle Valo 	if (ret)
17855fe4dffbSKalle Valo 		ath6kl_warn("failed to power off hif: %d\n", ret);
17865fe4dffbSKalle Valo 
1787ede615d2SVasanthakumar Thiagarajan 	return 0;
1788ede615d2SVasanthakumar Thiagarajan }
178976a9fbe2SKalle Valo 
1790ede615d2SVasanthakumar Thiagarajan int ath6kl_init_hw_stop(struct ath6kl *ar)
1791ede615d2SVasanthakumar Thiagarajan {
1792ede615d2SVasanthakumar Thiagarajan 	int err;
1793ede615d2SVasanthakumar Thiagarajan 
1794ede615d2SVasanthakumar Thiagarajan 	err = __ath6kl_init_hw_stop(ar);
1795ede615d2SVasanthakumar Thiagarajan 	if (err)
1796ede615d2SVasanthakumar Thiagarajan 		return err;
1797ede615d2SVasanthakumar Thiagarajan 	ar->state = ATH6KL_STATE_OFF;
17985fe4dffbSKalle Valo 	return 0;
17995fe4dffbSKalle Valo }
18005fe4dffbSKalle Valo 
180184caf800SVasanthakumar Thiagarajan void ath6kl_init_hw_restart(struct ath6kl *ar)
180284caf800SVasanthakumar Thiagarajan {
180358109df6SVasanthakumar Thiagarajan 	clear_bit(WMI_READY, &ar->flag);
180458109df6SVasanthakumar Thiagarajan 
180584caf800SVasanthakumar Thiagarajan 	ath6kl_cfg80211_stop_all(ar);
180684caf800SVasanthakumar Thiagarajan 
180758109df6SVasanthakumar Thiagarajan 	if (__ath6kl_init_hw_stop(ar)) {
180858109df6SVasanthakumar Thiagarajan 		ath6kl_dbg(ATH6KL_DBG_RECOVERY, "Failed to stop during fw error recovery\n");
180984caf800SVasanthakumar Thiagarajan 		return;
181058109df6SVasanthakumar Thiagarajan 	}
181184caf800SVasanthakumar Thiagarajan 
181284caf800SVasanthakumar Thiagarajan 	if (__ath6kl_init_hw_start(ar)) {
181384caf800SVasanthakumar Thiagarajan 		ath6kl_dbg(ATH6KL_DBG_RECOVERY, "Failed to restart during fw error recovery\n");
181484caf800SVasanthakumar Thiagarajan 		return;
181584caf800SVasanthakumar Thiagarajan 	}
181684caf800SVasanthakumar Thiagarajan }
181784caf800SVasanthakumar Thiagarajan 
1818bdcd8170SKalle Valo void ath6kl_stop_txrx(struct ath6kl *ar)
1819bdcd8170SKalle Valo {
1820990bd915SVasanthakumar Thiagarajan 	struct ath6kl_vif *vif, *tmp_vif;
18211d2a4456SVasanthakumar Thiagarajan 	int i;
1822bdcd8170SKalle Valo 
1823bdcd8170SKalle Valo 	set_bit(DESTROY_IN_PROGRESS, &ar->flag);
1824bdcd8170SKalle Valo 
1825bdcd8170SKalle Valo 	if (down_interruptible(&ar->sem)) {
1826bdcd8170SKalle Valo 		ath6kl_err("down_interruptible failed\n");
1827bdcd8170SKalle Valo 		return;
1828bdcd8170SKalle Valo 	}
1829bdcd8170SKalle Valo 
18301d2a4456SVasanthakumar Thiagarajan 	for (i = 0; i < AP_MAX_NUM_STA; i++)
18311d2a4456SVasanthakumar Thiagarajan 		aggr_reset_state(ar->sta_list[i].aggr_conn);
18321d2a4456SVasanthakumar Thiagarajan 
183311f6e40dSVasanthakumar Thiagarajan 	spin_lock_bh(&ar->list_lock);
1834990bd915SVasanthakumar Thiagarajan 	list_for_each_entry_safe(vif, tmp_vif, &ar->vif_list, list) {
1835990bd915SVasanthakumar Thiagarajan 		list_del(&vif->list);
183611f6e40dSVasanthakumar Thiagarajan 		spin_unlock_bh(&ar->list_lock);
1837355b3a98SMohammed Shafi Shajakhan 		ath6kl_cfg80211_vif_stop(vif, test_bit(WMI_READY, &ar->flag));
183827929723SVasanthakumar Thiagarajan 		rtnl_lock();
1839c25889e8SKalle Valo 		ath6kl_cfg80211_vif_cleanup(vif);
184027929723SVasanthakumar Thiagarajan 		rtnl_unlock();
184111f6e40dSVasanthakumar Thiagarajan 		spin_lock_bh(&ar->list_lock);
1842990bd915SVasanthakumar Thiagarajan 	}
184311f6e40dSVasanthakumar Thiagarajan 	spin_unlock_bh(&ar->list_lock);
1844bdcd8170SKalle Valo 
18456db8fa53SVasanthakumar Thiagarajan 	clear_bit(WMI_READY, &ar->flag);
18466db8fa53SVasanthakumar Thiagarajan 
1847f32036e8SVasanthakumar Thiagarajan 	if (ar->fw_recovery.enable)
1848f32036e8SVasanthakumar Thiagarajan 		del_timer_sync(&ar->fw_recovery.hb_timer);
1849f32036e8SVasanthakumar Thiagarajan 
18506db8fa53SVasanthakumar Thiagarajan 	/*
18516db8fa53SVasanthakumar Thiagarajan 	 * After wmi_shudown all WMI events will be dropped. We
18526db8fa53SVasanthakumar Thiagarajan 	 * need to cleanup the buffers allocated in AP mode and
18536db8fa53SVasanthakumar Thiagarajan 	 * give disconnect notification to stack, which usually
18546db8fa53SVasanthakumar Thiagarajan 	 * happens in the disconnect_event. Simulate the disconnect
18556db8fa53SVasanthakumar Thiagarajan 	 * event by calling the function directly. Sometimes
18566db8fa53SVasanthakumar Thiagarajan 	 * disconnect_event will be received when the debug logs
18576db8fa53SVasanthakumar Thiagarajan 	 * are collected.
18586db8fa53SVasanthakumar Thiagarajan 	 */
18596db8fa53SVasanthakumar Thiagarajan 	ath6kl_wmi_shutdown(ar->wmi);
18606db8fa53SVasanthakumar Thiagarajan 
18616db8fa53SVasanthakumar Thiagarajan 	clear_bit(WMI_ENABLED, &ar->flag);
18626db8fa53SVasanthakumar Thiagarajan 	if (ar->htc_target) {
18636db8fa53SVasanthakumar Thiagarajan 		ath6kl_dbg(ATH6KL_DBG_TRC, "%s: shut down htc\n", __func__);
18646db8fa53SVasanthakumar Thiagarajan 		ath6kl_htc_stop(ar->htc_target);
1865bdcd8170SKalle Valo 	}
1866bdcd8170SKalle Valo 
1867bdcd8170SKalle Valo 	/*
18686db8fa53SVasanthakumar Thiagarajan 	 * Try to reset the device if we can. The driver may have been
18696db8fa53SVasanthakumar Thiagarajan 	 * configure NOT to reset the target during a debug session.
1870bdcd8170SKalle Valo 	 */
1871ec1461dcSKalle Valo 	ath6kl_init_hw_reset(ar);
1872bdcd8170SKalle Valo 
1873e8ad9a06SVasanthakumar Thiagarajan 	up(&ar->sem);
1874bdcd8170SKalle Valo }
1875d6a434d6SKalle Valo EXPORT_SYMBOL(ath6kl_stop_txrx);
1876