xref: /linux/drivers/net/wireless/ath/ath6kl/init.c (revision 53cc3291bcfbefc0b3f1e3b7b30ed028b5f1a75d)
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,
15278803770SJessica Wu 		.refclk_hz                      = 0,
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,
15978803770SJessica Wu 			.tcmd	        = AR6004_HW_1_3_TCMD_FIRMWARE_FILE,
16078803770SJessica Wu 			.utf		= AR6004_HW_1_3_UTF_FIRMWARE_FILE,
16178803770SJessica Wu 			.testscript	= AR6004_HW_1_3_TESTSCRIPT_FILE,
162bf744f11SBala Shanmugam 		},
163bf744f11SBala Shanmugam 
164bf744f11SBala Shanmugam 		.fw_board               = AR6004_HW_1_3_BOARD_DATA_FILE,
165bf744f11SBala Shanmugam 		.fw_default_board       = AR6004_HW_1_3_DEFAULT_BOARD_DATA_FILE,
166bf744f11SBala Shanmugam 	},
16778803770SJessica Wu 	{
16878803770SJessica Wu 		.id				= AR6004_HW_3_0_VERSION,
16978803770SJessica Wu 		.name				= "ar6004 hw 3.0",
17078803770SJessica Wu 		.dataset_patch_addr		= 0,
17178803770SJessica Wu 		.app_load_addr			= 0x1234,
17278803770SJessica Wu 		.board_ext_data_addr		= 0,
17378803770SJessica Wu 		.reserved_ram_size		= 7168,
17478803770SJessica Wu 		.board_addr			= 0x436400,
17578803770SJessica Wu 		.testscript_addr		= 0,
17678803770SJessica Wu 		.flags				= 0,
17778803770SJessica Wu 
17878803770SJessica Wu 		.fw = {
17978803770SJessica Wu 			.dir		= AR6004_HW_3_0_FW_DIR,
18078803770SJessica Wu 			.fw		= AR6004_HW_3_0_FIRMWARE_FILE,
18178803770SJessica Wu 			.tcmd	        = AR6004_HW_3_0_TCMD_FIRMWARE_FILE,
18278803770SJessica Wu 			.utf		= AR6004_HW_3_0_UTF_FIRMWARE_FILE,
18378803770SJessica Wu 			.testscript	= AR6004_HW_3_0_TESTSCRIPT_FILE,
18478803770SJessica Wu 		},
18578803770SJessica Wu 
18678803770SJessica Wu 		.fw_board		= AR6004_HW_3_0_BOARD_DATA_FILE,
18778803770SJessica Wu 		.fw_default_board	= AR6004_HW_3_0_DEFAULT_BOARD_DATA_FILE,
18878803770SJessica Wu 	},
189856f4b31SKalle Valo };
190856f4b31SKalle Valo 
191bdcd8170SKalle Valo /*
192bdcd8170SKalle Valo  * Include definitions here that can be used to tune the WLAN module
193bdcd8170SKalle Valo  * behavior. Different customers can tune the behavior as per their needs,
194bdcd8170SKalle Valo  * here.
195bdcd8170SKalle Valo  */
196bdcd8170SKalle Valo 
197bdcd8170SKalle Valo /*
198bdcd8170SKalle Valo  * This configuration item enable/disable keepalive support.
199bdcd8170SKalle Valo  * Keepalive support: In the absence of any data traffic to AP, null
200bdcd8170SKalle Valo  * frames will be sent to the AP at periodic interval, to keep the association
201bdcd8170SKalle Valo  * active. This configuration item defines the periodic interval.
202bdcd8170SKalle Valo  * Use value of zero to disable keepalive support
203bdcd8170SKalle Valo  * Default: 60 seconds
204bdcd8170SKalle Valo  */
205bdcd8170SKalle Valo #define WLAN_CONFIG_KEEP_ALIVE_INTERVAL 60
206bdcd8170SKalle Valo 
207bdcd8170SKalle Valo /*
208bdcd8170SKalle Valo  * This configuration item sets the value of disconnect timeout
209bdcd8170SKalle Valo  * Firmware delays sending the disconnec event to the host for this
210bdcd8170SKalle Valo  * timeout after is gets disconnected from the current AP.
211bdcd8170SKalle Valo  * If the firmware successly roams within the disconnect timeout
212bdcd8170SKalle Valo  * it sends a new connect event
213bdcd8170SKalle Valo  */
214bdcd8170SKalle Valo #define WLAN_CONFIG_DISCONNECT_TIMEOUT 10
215bdcd8170SKalle Valo 
216bdcd8170SKalle Valo 
217bdcd8170SKalle Valo #define ATH6KL_DATA_OFFSET    64
218bdcd8170SKalle Valo struct sk_buff *ath6kl_buf_alloc(int size)
219bdcd8170SKalle Valo {
220bdcd8170SKalle Valo 	struct sk_buff *skb;
221bdcd8170SKalle Valo 	u16 reserved;
222bdcd8170SKalle Valo 
223bdcd8170SKalle Valo 	/* Add chacheline space at front and back of buffer */
2246a3e4e06SMyoungje Kim 	reserved = roundup((2 * L1_CACHE_BYTES) + ATH6KL_DATA_OFFSET +
2256a3e4e06SMyoungje Kim 		   sizeof(struct htc_packet) + ATH6KL_HTC_ALIGN_BYTES, 4);
226bdcd8170SKalle Valo 	skb = dev_alloc_skb(size + reserved);
227bdcd8170SKalle Valo 
228bdcd8170SKalle Valo 	if (skb)
229bdcd8170SKalle Valo 		skb_reserve(skb, reserved - L1_CACHE_BYTES);
230bdcd8170SKalle Valo 	return skb;
231bdcd8170SKalle Valo }
232bdcd8170SKalle Valo 
233e29f25f5SVasanthakumar Thiagarajan void ath6kl_init_profile_info(struct ath6kl_vif *vif)
234bdcd8170SKalle Valo {
2353450334fSVasanthakumar Thiagarajan 	vif->ssid_len = 0;
2363450334fSVasanthakumar Thiagarajan 	memset(vif->ssid, 0, sizeof(vif->ssid));
2373450334fSVasanthakumar Thiagarajan 
2383450334fSVasanthakumar Thiagarajan 	vif->dot11_auth_mode = OPEN_AUTH;
2393450334fSVasanthakumar Thiagarajan 	vif->auth_mode = NONE_AUTH;
2403450334fSVasanthakumar Thiagarajan 	vif->prwise_crypto = NONE_CRYPT;
2413450334fSVasanthakumar Thiagarajan 	vif->prwise_crypto_len = 0;
2423450334fSVasanthakumar Thiagarajan 	vif->grp_crypto = NONE_CRYPT;
2433450334fSVasanthakumar Thiagarajan 	vif->grp_crypto_len = 0;
2446f2a73f9SVasanthakumar Thiagarajan 	memset(vif->wep_key_list, 0, sizeof(vif->wep_key_list));
2458c8b65e3SVasanthakumar Thiagarajan 	memset(vif->req_bssid, 0, sizeof(vif->req_bssid));
2468c8b65e3SVasanthakumar Thiagarajan 	memset(vif->bssid, 0, sizeof(vif->bssid));
247f74bac54SVasanthakumar Thiagarajan 	vif->bss_ch = 0;
248bdcd8170SKalle Valo }
249bdcd8170SKalle Valo 
250bdcd8170SKalle Valo static int ath6kl_set_host_app_area(struct ath6kl *ar)
251bdcd8170SKalle Valo {
252bdcd8170SKalle Valo 	u32 address, data;
253bdcd8170SKalle Valo 	struct host_app_area host_app_area;
254bdcd8170SKalle Valo 
255bdcd8170SKalle Valo 	/* Fetch the address of the host_app_area_s
256bdcd8170SKalle Valo 	 * instance in the host interest area */
257bdcd8170SKalle Valo 	address = ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_app_host_interest));
25831024d99SKevin Fang 	address = TARG_VTOP(ar->target_type, address);
259bdcd8170SKalle Valo 
260addb44beSKalle Valo 	if (ath6kl_diag_read32(ar, address, &data))
261bdcd8170SKalle Valo 		return -EIO;
262bdcd8170SKalle Valo 
26331024d99SKevin Fang 	address = TARG_VTOP(ar->target_type, data);
264cbf49a6fSKalle Valo 	host_app_area.wmi_protocol_ver = cpu_to_le32(WMI_PROTOCOL_VERSION);
265addb44beSKalle Valo 	if (ath6kl_diag_write(ar, address, (u8 *) &host_app_area,
266addb44beSKalle Valo 			      sizeof(struct host_app_area)))
267bdcd8170SKalle Valo 		return -EIO;
268bdcd8170SKalle Valo 
269bdcd8170SKalle Valo 	return 0;
270bdcd8170SKalle Valo }
271bdcd8170SKalle Valo 
272bdcd8170SKalle Valo static inline void set_ac2_ep_map(struct ath6kl *ar,
273bdcd8170SKalle Valo 				  u8 ac,
274bdcd8170SKalle Valo 				  enum htc_endpoint_id ep)
275bdcd8170SKalle Valo {
276bdcd8170SKalle Valo 	ar->ac2ep_map[ac] = ep;
277bdcd8170SKalle Valo 	ar->ep2ac_map[ep] = ac;
278bdcd8170SKalle Valo }
279bdcd8170SKalle Valo 
280bdcd8170SKalle Valo /* connect to a service */
281bdcd8170SKalle Valo static int ath6kl_connectservice(struct ath6kl *ar,
282bdcd8170SKalle Valo 				 struct htc_service_connect_req  *con_req,
283bdcd8170SKalle Valo 				 char *desc)
284bdcd8170SKalle Valo {
285bdcd8170SKalle Valo 	int status;
286bdcd8170SKalle Valo 	struct htc_service_connect_resp response;
287bdcd8170SKalle Valo 
288bdcd8170SKalle Valo 	memset(&response, 0, sizeof(response));
289bdcd8170SKalle Valo 
290ad226ec2SKalle Valo 	status = ath6kl_htc_conn_service(ar->htc_target, con_req, &response);
291bdcd8170SKalle Valo 	if (status) {
292bdcd8170SKalle Valo 		ath6kl_err("failed to connect to %s service status:%d\n",
293bdcd8170SKalle Valo 			   desc, status);
294bdcd8170SKalle Valo 		return status;
295bdcd8170SKalle Valo 	}
296bdcd8170SKalle Valo 
297bdcd8170SKalle Valo 	switch (con_req->svc_id) {
298bdcd8170SKalle Valo 	case WMI_CONTROL_SVC:
299bdcd8170SKalle Valo 		if (test_bit(WMI_ENABLED, &ar->flag))
300bdcd8170SKalle Valo 			ath6kl_wmi_set_control_ep(ar->wmi, response.endpoint);
301bdcd8170SKalle Valo 		ar->ctrl_ep = response.endpoint;
302bdcd8170SKalle Valo 		break;
303bdcd8170SKalle Valo 	case WMI_DATA_BE_SVC:
304bdcd8170SKalle Valo 		set_ac2_ep_map(ar, WMM_AC_BE, response.endpoint);
305bdcd8170SKalle Valo 		break;
306bdcd8170SKalle Valo 	case WMI_DATA_BK_SVC:
307bdcd8170SKalle Valo 		set_ac2_ep_map(ar, WMM_AC_BK, response.endpoint);
308bdcd8170SKalle Valo 		break;
309bdcd8170SKalle Valo 	case WMI_DATA_VI_SVC:
310bdcd8170SKalle Valo 		set_ac2_ep_map(ar, WMM_AC_VI, response.endpoint);
311bdcd8170SKalle Valo 		break;
312bdcd8170SKalle Valo 	case WMI_DATA_VO_SVC:
313bdcd8170SKalle Valo 		set_ac2_ep_map(ar, WMM_AC_VO, response.endpoint);
314bdcd8170SKalle Valo 		break;
315bdcd8170SKalle Valo 	default:
316bdcd8170SKalle Valo 		ath6kl_err("service id is not mapped %d\n", con_req->svc_id);
317bdcd8170SKalle Valo 		return -EINVAL;
318bdcd8170SKalle Valo 	}
319bdcd8170SKalle Valo 
320bdcd8170SKalle Valo 	return 0;
321bdcd8170SKalle Valo }
322bdcd8170SKalle Valo 
323bdcd8170SKalle Valo static int ath6kl_init_service_ep(struct ath6kl *ar)
324bdcd8170SKalle Valo {
325bdcd8170SKalle Valo 	struct htc_service_connect_req connect;
326bdcd8170SKalle Valo 
327bdcd8170SKalle Valo 	memset(&connect, 0, sizeof(connect));
328bdcd8170SKalle Valo 
329bdcd8170SKalle Valo 	/* these fields are the same for all service endpoints */
330900d6b3fSKalle Valo 	connect.ep_cb.tx_comp_multi = ath6kl_tx_complete;
331bdcd8170SKalle Valo 	connect.ep_cb.rx = ath6kl_rx;
332bdcd8170SKalle Valo 	connect.ep_cb.rx_refill = ath6kl_rx_refill;
333bdcd8170SKalle Valo 	connect.ep_cb.tx_full = ath6kl_tx_queue_full;
334bdcd8170SKalle Valo 
335bdcd8170SKalle Valo 	/*
336bdcd8170SKalle Valo 	 * Set the max queue depth so that our ath6kl_tx_queue_full handler
337bdcd8170SKalle Valo 	 * gets called.
338bdcd8170SKalle Valo 	*/
339bdcd8170SKalle Valo 	connect.max_txq_depth = MAX_DEFAULT_SEND_QUEUE_DEPTH;
340bdcd8170SKalle Valo 	connect.ep_cb.rx_refill_thresh = ATH6KL_MAX_RX_BUFFERS / 4;
341bdcd8170SKalle Valo 	if (!connect.ep_cb.rx_refill_thresh)
342bdcd8170SKalle Valo 		connect.ep_cb.rx_refill_thresh++;
343bdcd8170SKalle Valo 
344bdcd8170SKalle Valo 	/* connect to control service */
345bdcd8170SKalle Valo 	connect.svc_id = WMI_CONTROL_SVC;
346bdcd8170SKalle Valo 	if (ath6kl_connectservice(ar, &connect, "WMI CONTROL"))
347bdcd8170SKalle Valo 		return -EIO;
348bdcd8170SKalle Valo 
349bdcd8170SKalle Valo 	connect.flags |= HTC_FLGS_TX_BNDL_PAD_EN;
350bdcd8170SKalle Valo 
351bdcd8170SKalle Valo 	/*
352bdcd8170SKalle Valo 	 * Limit the HTC message size on the send path, although e can
353bdcd8170SKalle Valo 	 * receive A-MSDU frames of 4K, we will only send ethernet-sized
354bdcd8170SKalle Valo 	 * (802.3) frames on the send path.
355bdcd8170SKalle Valo 	 */
356bdcd8170SKalle Valo 	connect.max_rxmsg_sz = WMI_MAX_TX_DATA_FRAME_LENGTH;
357bdcd8170SKalle Valo 
358bdcd8170SKalle Valo 	/*
359bdcd8170SKalle Valo 	 * To reduce the amount of committed memory for larger A_MSDU
360bdcd8170SKalle Valo 	 * frames, use the recv-alloc threshold mechanism for larger
361bdcd8170SKalle Valo 	 * packets.
362bdcd8170SKalle Valo 	 */
363bdcd8170SKalle Valo 	connect.ep_cb.rx_alloc_thresh = ATH6KL_BUFFER_SIZE;
364bdcd8170SKalle Valo 	connect.ep_cb.rx_allocthresh = ath6kl_alloc_amsdu_rxbuf;
365bdcd8170SKalle Valo 
366bdcd8170SKalle Valo 	/*
367bdcd8170SKalle Valo 	 * For the remaining data services set the connection flag to
368bdcd8170SKalle Valo 	 * reduce dribbling, if configured to do so.
369bdcd8170SKalle Valo 	 */
370bdcd8170SKalle Valo 	connect.conn_flags |= HTC_CONN_FLGS_REDUCE_CRED_DRIB;
371bdcd8170SKalle Valo 	connect.conn_flags &= ~HTC_CONN_FLGS_THRESH_MASK;
372bdcd8170SKalle Valo 	connect.conn_flags |= HTC_CONN_FLGS_THRESH_LVL_HALF;
373bdcd8170SKalle Valo 
374bdcd8170SKalle Valo 	connect.svc_id = WMI_DATA_BE_SVC;
375bdcd8170SKalle Valo 
376bdcd8170SKalle Valo 	if (ath6kl_connectservice(ar, &connect, "WMI DATA BE"))
377bdcd8170SKalle Valo 		return -EIO;
378bdcd8170SKalle Valo 
379bdcd8170SKalle Valo 	/* connect to back-ground map this to WMI LOW_PRI */
380bdcd8170SKalle Valo 	connect.svc_id = WMI_DATA_BK_SVC;
381bdcd8170SKalle Valo 	if (ath6kl_connectservice(ar, &connect, "WMI DATA BK"))
382bdcd8170SKalle Valo 		return -EIO;
383bdcd8170SKalle Valo 
384171fe768SMohammed Shafi Shajakhan 	/* connect to Video service, map this to HI PRI */
385bdcd8170SKalle Valo 	connect.svc_id = WMI_DATA_VI_SVC;
386bdcd8170SKalle Valo 	if (ath6kl_connectservice(ar, &connect, "WMI DATA VI"))
387bdcd8170SKalle Valo 		return -EIO;
388bdcd8170SKalle Valo 
389bdcd8170SKalle Valo 	/*
390bdcd8170SKalle Valo 	 * Connect to VO service, this is currently not mapped to a WMI
391bdcd8170SKalle Valo 	 * priority stream due to historical reasons. WMI originally
392bdcd8170SKalle Valo 	 * defined 3 priorities over 3 mailboxes We can change this when
393bdcd8170SKalle Valo 	 * WMI is reworked so that priorities are not dependent on
394bdcd8170SKalle Valo 	 * mailboxes.
395bdcd8170SKalle Valo 	 */
396bdcd8170SKalle Valo 	connect.svc_id = WMI_DATA_VO_SVC;
397bdcd8170SKalle Valo 	if (ath6kl_connectservice(ar, &connect, "WMI DATA VO"))
398bdcd8170SKalle Valo 		return -EIO;
399bdcd8170SKalle Valo 
400bdcd8170SKalle Valo 	return 0;
401bdcd8170SKalle Valo }
402bdcd8170SKalle Valo 
403e29f25f5SVasanthakumar Thiagarajan void ath6kl_init_control_info(struct ath6kl_vif *vif)
404bdcd8170SKalle Valo {
405e29f25f5SVasanthakumar Thiagarajan 	ath6kl_init_profile_info(vif);
4063450334fSVasanthakumar Thiagarajan 	vif->def_txkey_index = 0;
4076f2a73f9SVasanthakumar Thiagarajan 	memset(vif->wep_key_list, 0, sizeof(vif->wep_key_list));
408f74bac54SVasanthakumar Thiagarajan 	vif->ch_hint = 0;
409bdcd8170SKalle Valo }
410bdcd8170SKalle Valo 
411bdcd8170SKalle Valo /*
412bdcd8170SKalle Valo  * Set HTC/Mbox operational parameters, this can only be called when the
413bdcd8170SKalle Valo  * target is in the BMI phase.
414bdcd8170SKalle Valo  */
415bdcd8170SKalle Valo static int ath6kl_set_htc_params(struct ath6kl *ar, u32 mbox_isr_yield_val,
416bdcd8170SKalle Valo 				 u8 htc_ctrl_buf)
417bdcd8170SKalle Valo {
418bdcd8170SKalle Valo 	int status;
419bdcd8170SKalle Valo 	u32 blk_size;
420bdcd8170SKalle Valo 
421bdcd8170SKalle Valo 	blk_size = ar->mbox_info.block_size;
422bdcd8170SKalle Valo 
423bdcd8170SKalle Valo 	if (htc_ctrl_buf)
424bdcd8170SKalle Valo 		blk_size |=  ((u32)htc_ctrl_buf) << 16;
425bdcd8170SKalle Valo 
426bdcd8170SKalle Valo 	/* set the host interest area for the block size */
42724fc32b3SKalle Valo 	status = ath6kl_bmi_write_hi32(ar, hi_mbox_io_block_sz, blk_size);
428bdcd8170SKalle Valo 	if (status) {
429bdcd8170SKalle Valo 		ath6kl_err("bmi_write_memory for IO block size failed\n");
430bdcd8170SKalle Valo 		goto out;
431bdcd8170SKalle Valo 	}
432bdcd8170SKalle Valo 
433bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_TRC, "block size set: %d (target addr:0x%X)\n",
434bdcd8170SKalle Valo 		   blk_size,
435bdcd8170SKalle Valo 		   ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_mbox_io_block_sz)));
436bdcd8170SKalle Valo 
437bdcd8170SKalle Valo 	if (mbox_isr_yield_val) {
438bdcd8170SKalle Valo 		/* set the host interest area for the mbox ISR yield limit */
43924fc32b3SKalle Valo 		status = ath6kl_bmi_write_hi32(ar, hi_mbox_isr_yield_limit,
44024fc32b3SKalle Valo 					       mbox_isr_yield_val);
441bdcd8170SKalle Valo 		if (status) {
442bdcd8170SKalle Valo 			ath6kl_err("bmi_write_memory for yield limit failed\n");
443bdcd8170SKalle Valo 			goto out;
444bdcd8170SKalle Valo 		}
445bdcd8170SKalle Valo 	}
446bdcd8170SKalle Valo 
447bdcd8170SKalle Valo out:
448bdcd8170SKalle Valo 	return status;
449bdcd8170SKalle Valo }
450bdcd8170SKalle Valo 
4510ce59445SVasanthakumar Thiagarajan static int ath6kl_target_config_wlan_params(struct ath6kl *ar, int idx)
452bdcd8170SKalle Valo {
4534dea08e0SJouni Malinen 	int ret;
454bdcd8170SKalle Valo 
455bdcd8170SKalle Valo 	/*
456bdcd8170SKalle Valo 	 * Configure the device for rx dot11 header rules. "0,0" are the
457bdcd8170SKalle Valo 	 * default values. Required if checksum offload is needed. Set
458bdcd8170SKalle Valo 	 * RxMetaVersion to 2.
459bdcd8170SKalle Valo 	 */
4601ca4d0b6SKalle Valo 	ret = ath6kl_wmi_set_rx_frame_format_cmd(ar->wmi, idx,
4611ca4d0b6SKalle Valo 						 ar->rx_meta_ver, 0, 0);
4621ca4d0b6SKalle Valo 	if (ret) {
4631ca4d0b6SKalle Valo 		ath6kl_err("unable to set the rx frame format: %d\n", ret);
4641ca4d0b6SKalle Valo 		return ret;
465bdcd8170SKalle Valo 	}
466bdcd8170SKalle Valo 
4671ca4d0b6SKalle Valo 	if (ar->conf_flags & ATH6KL_CONF_IGNORE_PS_FAIL_EVT_IN_SCAN) {
4681ca4d0b6SKalle Valo 		ret = ath6kl_wmi_pmparams_cmd(ar->wmi, idx, 0, 1, 0, 0, 1,
46905aab177SKalle Valo 					      IGNORE_PS_FAIL_DURING_SCAN);
4701ca4d0b6SKalle Valo 		if (ret) {
4711ca4d0b6SKalle Valo 			ath6kl_err("unable to set power save fail event policy: %d\n",
4721ca4d0b6SKalle Valo 				   ret);
4731ca4d0b6SKalle Valo 			return ret;
4741ca4d0b6SKalle Valo 		}
475bdcd8170SKalle Valo 	}
476bdcd8170SKalle Valo 
4771ca4d0b6SKalle Valo 	if (!(ar->conf_flags & ATH6KL_CONF_IGNORE_ERP_BARKER)) {
4781ca4d0b6SKalle Valo 		ret = ath6kl_wmi_set_lpreamble_cmd(ar->wmi, idx, 0,
47905aab177SKalle Valo 						   WMI_FOLLOW_BARKER_IN_ERP);
4801ca4d0b6SKalle Valo 		if (ret) {
4811ca4d0b6SKalle Valo 			ath6kl_err("unable to set barker preamble policy: %d\n",
4821ca4d0b6SKalle Valo 				   ret);
4831ca4d0b6SKalle Valo 			return ret;
4841ca4d0b6SKalle Valo 		}
485bdcd8170SKalle Valo 	}
486bdcd8170SKalle Valo 
4871ca4d0b6SKalle Valo 	ret = ath6kl_wmi_set_keepalive_cmd(ar->wmi, idx,
4881ca4d0b6SKalle Valo 					   WLAN_CONFIG_KEEP_ALIVE_INTERVAL);
4891ca4d0b6SKalle Valo 	if (ret) {
4901ca4d0b6SKalle Valo 		ath6kl_err("unable to set keep alive interval: %d\n", ret);
4911ca4d0b6SKalle Valo 		return ret;
492bdcd8170SKalle Valo 	}
493bdcd8170SKalle Valo 
4941ca4d0b6SKalle Valo 	ret = ath6kl_wmi_disctimeout_cmd(ar->wmi, idx,
4951ca4d0b6SKalle Valo 					 WLAN_CONFIG_DISCONNECT_TIMEOUT);
4961ca4d0b6SKalle Valo 	if (ret) {
4971ca4d0b6SKalle Valo 		ath6kl_err("unable to set disconnect timeout: %d\n", ret);
4981ca4d0b6SKalle Valo 		return ret;
499bdcd8170SKalle Valo 	}
500bdcd8170SKalle Valo 
5011ca4d0b6SKalle Valo 	if (!(ar->conf_flags & ATH6KL_CONF_ENABLE_TX_BURST)) {
5021ca4d0b6SKalle Valo 		ret = ath6kl_wmi_set_wmm_txop(ar->wmi, idx, WMI_TXOP_DISABLED);
5031ca4d0b6SKalle Valo 		if (ret) {
5041ca4d0b6SKalle Valo 			ath6kl_err("unable to set txop bursting: %d\n", ret);
5051ca4d0b6SKalle Valo 			return ret;
5061ca4d0b6SKalle Valo 		}
507bdcd8170SKalle Valo 	}
508bdcd8170SKalle Valo 
509b64de356SVasanthakumar Thiagarajan 	if (ar->p2p && (ar->vif_max == 1 || idx)) {
5100ce59445SVasanthakumar Thiagarajan 		ret = ath6kl_wmi_info_req_cmd(ar->wmi, idx,
5116bbc7c35SJouni Malinen 					      P2P_FLAG_CAPABILITIES_REQ |
5124dea08e0SJouni Malinen 					      P2P_FLAG_MACADDR_REQ |
5134dea08e0SJouni Malinen 					      P2P_FLAG_HMODEL_REQ);
5144dea08e0SJouni Malinen 		if (ret) {
515cdeb8602SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_TRC,
516cdeb8602SKalle Valo 				   "failed to request P2P capabilities (%d) - assuming P2P not supported\n",
517cdeb8602SKalle Valo 				   ret);
5183db1cd5cSRusty Russell 			ar->p2p = false;
5196bbc7c35SJouni Malinen 		}
5206bbc7c35SJouni Malinen 	}
5216bbc7c35SJouni Malinen 
522b64de356SVasanthakumar Thiagarajan 	if (ar->p2p && (ar->vif_max == 1 || idx)) {
5236bbc7c35SJouni Malinen 		/* Enable Probe Request reporting for P2P */
5240ce59445SVasanthakumar Thiagarajan 		ret = ath6kl_wmi_probe_report_req_cmd(ar->wmi, idx, true);
5256bbc7c35SJouni Malinen 		if (ret) {
526cdeb8602SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_TRC,
527cdeb8602SKalle Valo 				   "failed to enable Probe Request reporting (%d)\n",
528cdeb8602SKalle Valo 				   ret);
5296bbc7c35SJouni Malinen 		}
5304dea08e0SJouni Malinen 	}
5314dea08e0SJouni Malinen 
5321ca4d0b6SKalle Valo 	return ret;
533bdcd8170SKalle Valo }
534bdcd8170SKalle Valo 
535bdcd8170SKalle Valo int ath6kl_configure_target(struct ath6kl *ar)
536bdcd8170SKalle Valo {
537bdcd8170SKalle Valo 	u32 param, ram_reserved_size;
5383226f68aSVasanthakumar Thiagarajan 	u8 fw_iftype, fw_mode = 0, fw_submode = 0;
53939586bf2SRyan Hsu 	int i, status;
540bdcd8170SKalle Valo 
541f29af978SKalle Valo 	param = !!(ar->conf_flags & ATH6KL_CONF_UART_DEBUG);
54224fc32b3SKalle Valo 	if (ath6kl_bmi_write_hi32(ar, hi_serial_enable, param)) {
543a10e2f2fSVasanthakumar Thiagarajan 		ath6kl_err("bmi_write_memory for uart debug failed\n");
544a10e2f2fSVasanthakumar Thiagarajan 		return -EIO;
545a10e2f2fSVasanthakumar Thiagarajan 	}
546a10e2f2fSVasanthakumar Thiagarajan 
5477b85832dSVasanthakumar Thiagarajan 	/*
5487b85832dSVasanthakumar Thiagarajan 	 * Note: Even though the firmware interface type is
5497b85832dSVasanthakumar Thiagarajan 	 * chosen as BSS_STA for all three interfaces, can
5507b85832dSVasanthakumar Thiagarajan 	 * be configured to IBSS/AP as long as the fw submode
5517b85832dSVasanthakumar Thiagarajan 	 * remains normal mode (0 - AP, STA and IBSS). But
5527b85832dSVasanthakumar Thiagarajan 	 * due to an target assert in firmware only one interface is
5537b85832dSVasanthakumar Thiagarajan 	 * configured for now.
5547b85832dSVasanthakumar Thiagarajan 	 */
555dd3751f7SVasanthakumar Thiagarajan 	fw_iftype = HI_OPTION_FW_MODE_BSS_STA;
556bdcd8170SKalle Valo 
55771f96ee6SKalle Valo 	for (i = 0; i < ar->vif_max; i++)
5587b85832dSVasanthakumar Thiagarajan 		fw_mode |= fw_iftype << (i * HI_OPTION_FW_MODE_BITS);
5597b85832dSVasanthakumar Thiagarajan 
5607b85832dSVasanthakumar Thiagarajan 	/*
5611e8d13b0SVasanthakumar Thiagarajan 	 * Submodes when fw does not support dynamic interface
5621e8d13b0SVasanthakumar Thiagarajan 	 * switching:
5633226f68aSVasanthakumar Thiagarajan 	 *		vif[0] - AP/STA/IBSS
5647b85832dSVasanthakumar Thiagarajan 	 *		vif[1] - "P2P dev"/"P2P GO"/"P2P Client"
5657b85832dSVasanthakumar Thiagarajan 	 *		vif[2] - "P2P dev"/"P2P GO"/"P2P Client"
5661e8d13b0SVasanthakumar Thiagarajan 	 * Otherwise, All the interface are initialized to p2p dev.
5677b85832dSVasanthakumar Thiagarajan 	 */
5683226f68aSVasanthakumar Thiagarajan 
5691e8d13b0SVasanthakumar Thiagarajan 	if (test_bit(ATH6KL_FW_CAPABILITY_STA_P2PDEV_DUPLEX,
5701e8d13b0SVasanthakumar Thiagarajan 		     ar->fw_capabilities)) {
5711e8d13b0SVasanthakumar Thiagarajan 		for (i = 0; i < ar->vif_max; i++)
5721e8d13b0SVasanthakumar Thiagarajan 			fw_submode |= HI_OPTION_FW_SUBMODE_P2PDEV <<
5731e8d13b0SVasanthakumar Thiagarajan 				(i * HI_OPTION_FW_SUBMODE_BITS);
5741e8d13b0SVasanthakumar Thiagarajan 	} else {
5753226f68aSVasanthakumar Thiagarajan 		for (i = 0; i < ar->max_norm_iface; i++)
5763226f68aSVasanthakumar Thiagarajan 			fw_submode |= HI_OPTION_FW_SUBMODE_NONE <<
5773226f68aSVasanthakumar Thiagarajan 				(i * HI_OPTION_FW_SUBMODE_BITS);
5783226f68aSVasanthakumar Thiagarajan 
57971f96ee6SKalle Valo 		for (i = ar->max_norm_iface; i < ar->vif_max; i++)
5803226f68aSVasanthakumar Thiagarajan 			fw_submode |= HI_OPTION_FW_SUBMODE_P2PDEV <<
5813226f68aSVasanthakumar Thiagarajan 				(i * HI_OPTION_FW_SUBMODE_BITS);
5827b85832dSVasanthakumar Thiagarajan 
583b64de356SVasanthakumar Thiagarajan 		if (ar->p2p && ar->vif_max == 1)
5847b85832dSVasanthakumar Thiagarajan 			fw_submode = HI_OPTION_FW_SUBMODE_P2PDEV;
5851e8d13b0SVasanthakumar Thiagarajan 	}
5867b85832dSVasanthakumar Thiagarajan 
58724fc32b3SKalle Valo 	if (ath6kl_bmi_write_hi32(ar, hi_app_host_interest,
58824fc32b3SKalle Valo 				  HTC_PROTOCOL_VERSION) != 0) {
589bdcd8170SKalle Valo 		ath6kl_err("bmi_write_memory for htc version failed\n");
590bdcd8170SKalle Valo 		return -EIO;
591bdcd8170SKalle Valo 	}
592bdcd8170SKalle Valo 
593bdcd8170SKalle Valo 	/* set the firmware mode to STA/IBSS/AP */
594bdcd8170SKalle Valo 	param = 0;
595bdcd8170SKalle Valo 
59680fb2686SKalle Valo 	if (ath6kl_bmi_read_hi32(ar, hi_option_flag, &param) != 0) {
597bdcd8170SKalle Valo 		ath6kl_err("bmi_read_memory for setting fwmode failed\n");
598bdcd8170SKalle Valo 		return -EIO;
599bdcd8170SKalle Valo 	}
600bdcd8170SKalle Valo 
60171f96ee6SKalle Valo 	param |= (ar->vif_max << HI_OPTION_NUM_DEV_SHIFT);
6027b85832dSVasanthakumar Thiagarajan 	param |= fw_mode << HI_OPTION_FW_MODE_SHIFT;
6037b85832dSVasanthakumar Thiagarajan 	param |= fw_submode << HI_OPTION_FW_SUBMODE_SHIFT;
6047b85832dSVasanthakumar Thiagarajan 
605bdcd8170SKalle Valo 	param |= (0 << HI_OPTION_MAC_ADDR_METHOD_SHIFT);
606bdcd8170SKalle Valo 	param |= (0 << HI_OPTION_FW_BRIDGE_SHIFT);
607bdcd8170SKalle Valo 
60824fc32b3SKalle Valo 	if (ath6kl_bmi_write_hi32(ar, hi_option_flag, param) != 0) {
609bdcd8170SKalle Valo 		ath6kl_err("bmi_write_memory for setting fwmode failed\n");
610bdcd8170SKalle Valo 		return -EIO;
611bdcd8170SKalle Valo 	}
612bdcd8170SKalle Valo 
613bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_TRC, "firmware mode set\n");
614bdcd8170SKalle Valo 
615bdcd8170SKalle Valo 	/*
616bdcd8170SKalle Valo 	 * Hardcode the address use for the extended board data
617bdcd8170SKalle Valo 	 * Ideally this should be pre-allocate by the OS at boot time
618bdcd8170SKalle Valo 	 * But since it is a new feature and board data is loaded
619bdcd8170SKalle Valo 	 * at init time, we have to workaround this from host.
620bdcd8170SKalle Valo 	 * It is difficult to patch the firmware boot code,
621bdcd8170SKalle Valo 	 * but possible in theory.
622bdcd8170SKalle Valo 	 */
623bdcd8170SKalle Valo 
62478803770SJessica Wu 	if ((ar->target_type == TARGET_TYPE_AR6003) ||
62578803770SJessica Wu 	    (ar->version.target_ver == AR6004_HW_1_3_VERSION) ||
62678803770SJessica Wu 	    (ar->version.target_ver == AR6004_HW_3_0_VERSION)) {
627991b27eaSKalle Valo 		param = ar->hw.board_ext_data_addr;
628991b27eaSKalle Valo 		ram_reserved_size = ar->hw.reserved_ram_size;
629bdcd8170SKalle Valo 
63024fc32b3SKalle Valo 		if (ath6kl_bmi_write_hi32(ar, hi_board_ext_data, param) != 0) {
631bdcd8170SKalle Valo 			ath6kl_err("bmi_write_memory for hi_board_ext_data failed\n");
632bdcd8170SKalle Valo 			return -EIO;
633bdcd8170SKalle Valo 		}
634991b27eaSKalle Valo 
63524fc32b3SKalle Valo 		if (ath6kl_bmi_write_hi32(ar, hi_end_ram_reserve_sz,
63624fc32b3SKalle Valo 					  ram_reserved_size) != 0) {
637bdcd8170SKalle Valo 			ath6kl_err("bmi_write_memory for hi_end_ram_reserve_sz failed\n");
638bdcd8170SKalle Valo 			return -EIO;
639bdcd8170SKalle Valo 		}
6406b42d308SKalle Valo 	}
641bdcd8170SKalle Valo 
642bdcd8170SKalle Valo 	/* set the block size for the target */
643bdcd8170SKalle Valo 	if (ath6kl_set_htc_params(ar, MBOX_YIELD_LIMIT, 0))
644bdcd8170SKalle Valo 		/* use default number of control buffers */
645bdcd8170SKalle Valo 		return -EIO;
646bdcd8170SKalle Valo 
64739586bf2SRyan Hsu 	/* Configure GPIO AR600x UART */
64824fc32b3SKalle Valo 	status = ath6kl_bmi_write_hi32(ar, hi_dbg_uart_txpin,
64924fc32b3SKalle Valo 				       ar->hw.uarttx_pin);
65039586bf2SRyan Hsu 	if (status)
65139586bf2SRyan Hsu 		return status;
65239586bf2SRyan Hsu 
65339586bf2SRyan Hsu 	/* Configure target refclk_hz */
654958e1be8SKalle Valo 	if (ar->hw.refclk_hz != 0) {
655958e1be8SKalle Valo 		status = ath6kl_bmi_write_hi32(ar, hi_refclk_hz,
656958e1be8SKalle Valo 					       ar->hw.refclk_hz);
65739586bf2SRyan Hsu 		if (status)
65839586bf2SRyan Hsu 			return status;
659958e1be8SKalle Valo 	}
66039586bf2SRyan Hsu 
661bdcd8170SKalle Valo 	return 0;
662bdcd8170SKalle Valo }
663bdcd8170SKalle Valo 
664bdcd8170SKalle Valo /* firmware upload */
665bdcd8170SKalle Valo static int ath6kl_get_fw(struct ath6kl *ar, const char *filename,
666bdcd8170SKalle Valo 			 u8 **fw, size_t *fw_len)
667bdcd8170SKalle Valo {
668bdcd8170SKalle Valo 	const struct firmware *fw_entry;
669bdcd8170SKalle Valo 	int ret;
670bdcd8170SKalle Valo 
671bdcd8170SKalle Valo 	ret = request_firmware(&fw_entry, filename, ar->dev);
672bdcd8170SKalle Valo 	if (ret)
673bdcd8170SKalle Valo 		return ret;
674bdcd8170SKalle Valo 
675bdcd8170SKalle Valo 	*fw_len = fw_entry->size;
676bdcd8170SKalle Valo 	*fw = kmemdup(fw_entry->data, fw_entry->size, GFP_KERNEL);
677bdcd8170SKalle Valo 
678bdcd8170SKalle Valo 	if (*fw == NULL)
679bdcd8170SKalle Valo 		ret = -ENOMEM;
680bdcd8170SKalle Valo 
681bdcd8170SKalle Valo 	release_firmware(fw_entry);
682bdcd8170SKalle Valo 
683bdcd8170SKalle Valo 	return ret;
684bdcd8170SKalle Valo }
685bdcd8170SKalle Valo 
68692ecbff4SSam Leffler #ifdef CONFIG_OF
68792ecbff4SSam Leffler /*
68892ecbff4SSam Leffler  * Check the device tree for a board-id and use it to construct
68992ecbff4SSam Leffler  * the pathname to the firmware file.  Used (for now) to find a
69092ecbff4SSam Leffler  * fallback to the "bdata.bin" file--typically a symlink to the
69192ecbff4SSam Leffler  * appropriate board-specific file.
69292ecbff4SSam Leffler  */
69392ecbff4SSam Leffler static bool check_device_tree(struct ath6kl *ar)
69492ecbff4SSam Leffler {
69592ecbff4SSam Leffler 	static const char *board_id_prop = "atheros,board-id";
69692ecbff4SSam Leffler 	struct device_node *node;
69792ecbff4SSam Leffler 	char board_filename[64];
69892ecbff4SSam Leffler 	const char *board_id;
69992ecbff4SSam Leffler 	int ret;
70092ecbff4SSam Leffler 
70192ecbff4SSam Leffler 	for_each_compatible_node(node, NULL, "atheros,ath6kl") {
70292ecbff4SSam Leffler 		board_id = of_get_property(node, board_id_prop, NULL);
70392ecbff4SSam Leffler 		if (board_id == NULL) {
70492ecbff4SSam Leffler 			ath6kl_warn("No \"%s\" property on %s node.\n",
70592ecbff4SSam Leffler 				    board_id_prop, node->name);
70692ecbff4SSam Leffler 			continue;
70792ecbff4SSam Leffler 		}
70892ecbff4SSam Leffler 		snprintf(board_filename, sizeof(board_filename),
709c0038972SKalle Valo 			 "%s/bdata.%s.bin", ar->hw.fw.dir, board_id);
71092ecbff4SSam Leffler 
71192ecbff4SSam Leffler 		ret = ath6kl_get_fw(ar, board_filename, &ar->fw_board,
71292ecbff4SSam Leffler 				    &ar->fw_board_len);
71392ecbff4SSam Leffler 		if (ret) {
71492ecbff4SSam Leffler 			ath6kl_err("Failed to get DT board file %s: %d\n",
71592ecbff4SSam Leffler 				   board_filename, ret);
71692ecbff4SSam Leffler 			continue;
71792ecbff4SSam Leffler 		}
71892ecbff4SSam Leffler 		return true;
71992ecbff4SSam Leffler 	}
72092ecbff4SSam Leffler 	return false;
72192ecbff4SSam Leffler }
72292ecbff4SSam Leffler #else
72392ecbff4SSam Leffler static bool check_device_tree(struct ath6kl *ar)
72492ecbff4SSam Leffler {
72592ecbff4SSam Leffler 	return false;
72692ecbff4SSam Leffler }
72792ecbff4SSam Leffler #endif /* CONFIG_OF */
72892ecbff4SSam Leffler 
729bdcd8170SKalle Valo static int ath6kl_fetch_board_file(struct ath6kl *ar)
730bdcd8170SKalle Valo {
731bdcd8170SKalle Valo 	const char *filename;
732bdcd8170SKalle Valo 	int ret;
733bdcd8170SKalle Valo 
734772c31eeSKalle Valo 	if (ar->fw_board != NULL)
735772c31eeSKalle Valo 		return 0;
736772c31eeSKalle Valo 
737d1a9421dSKalle Valo 	if (WARN_ON(ar->hw.fw_board == NULL))
738d1a9421dSKalle Valo 		return -EINVAL;
739d1a9421dSKalle Valo 
740d1a9421dSKalle Valo 	filename = ar->hw.fw_board;
741bdcd8170SKalle Valo 
742bdcd8170SKalle Valo 	ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
743bdcd8170SKalle Valo 			    &ar->fw_board_len);
744bdcd8170SKalle Valo 	if (ret == 0) {
745bdcd8170SKalle Valo 		/* managed to get proper board file */
746bdcd8170SKalle Valo 		return 0;
747bdcd8170SKalle Valo 	}
748bdcd8170SKalle Valo 
74992ecbff4SSam Leffler 	if (check_device_tree(ar)) {
75092ecbff4SSam Leffler 		/* got board file from device tree */
75192ecbff4SSam Leffler 		return 0;
75292ecbff4SSam Leffler 	}
75392ecbff4SSam Leffler 
754bdcd8170SKalle Valo 	/* there was no proper board file, try to use default instead */
755bdcd8170SKalle Valo 	ath6kl_warn("Failed to get board file %s (%d), trying to find default board file.\n",
756bdcd8170SKalle Valo 		    filename, ret);
757bdcd8170SKalle Valo 
758d1a9421dSKalle Valo 	filename = ar->hw.fw_default_board;
759bdcd8170SKalle Valo 
760bdcd8170SKalle Valo 	ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
761bdcd8170SKalle Valo 			    &ar->fw_board_len);
762bdcd8170SKalle Valo 	if (ret) {
763bdcd8170SKalle Valo 		ath6kl_err("Failed to get default board file %s: %d\n",
764bdcd8170SKalle Valo 			   filename, ret);
765bdcd8170SKalle Valo 		return ret;
766bdcd8170SKalle Valo 	}
767bdcd8170SKalle Valo 
768bdcd8170SKalle Valo 	ath6kl_warn("WARNING! No proper board file was not found, instead using a default board file.\n");
769bdcd8170SKalle Valo 	ath6kl_warn("Most likely your hardware won't work as specified. Install correct board file!\n");
770bdcd8170SKalle Valo 
771bdcd8170SKalle Valo 	return 0;
772bdcd8170SKalle Valo }
773bdcd8170SKalle Valo 
774772c31eeSKalle Valo static int ath6kl_fetch_otp_file(struct ath6kl *ar)
775772c31eeSKalle Valo {
776c0038972SKalle Valo 	char filename[100];
777772c31eeSKalle Valo 	int ret;
778772c31eeSKalle Valo 
779772c31eeSKalle Valo 	if (ar->fw_otp != NULL)
780772c31eeSKalle Valo 		return 0;
781772c31eeSKalle Valo 
782c0038972SKalle Valo 	if (ar->hw.fw.otp == NULL) {
783d1a9421dSKalle Valo 		ath6kl_dbg(ATH6KL_DBG_BOOT,
784d1a9421dSKalle Valo 			   "no OTP file configured for this hw\n");
785772c31eeSKalle Valo 		return 0;
786772c31eeSKalle Valo 	}
787772c31eeSKalle Valo 
788c0038972SKalle Valo 	snprintf(filename, sizeof(filename), "%s/%s",
789c0038972SKalle Valo 		 ar->hw.fw.dir, ar->hw.fw.otp);
790d1a9421dSKalle Valo 
791772c31eeSKalle Valo 	ret = ath6kl_get_fw(ar, filename, &ar->fw_otp,
792772c31eeSKalle Valo 			    &ar->fw_otp_len);
793772c31eeSKalle Valo 	if (ret) {
794772c31eeSKalle Valo 		ath6kl_err("Failed to get OTP file %s: %d\n",
795772c31eeSKalle Valo 			   filename, ret);
796772c31eeSKalle Valo 		return ret;
797772c31eeSKalle Valo 	}
798772c31eeSKalle Valo 
799772c31eeSKalle Valo 	return 0;
800772c31eeSKalle Valo }
801772c31eeSKalle Valo 
8025f1127ffSKalle Valo static int ath6kl_fetch_testmode_file(struct ath6kl *ar)
803772c31eeSKalle Valo {
804c0038972SKalle Valo 	char filename[100];
805772c31eeSKalle Valo 	int ret;
806772c31eeSKalle Valo 
8075f1127ffSKalle Valo 	if (ar->testmode == 0)
808772c31eeSKalle Valo 		return 0;
809772c31eeSKalle Valo 
8105f1127ffSKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "testmode %d\n", ar->testmode);
8115f1127ffSKalle Valo 
8125f1127ffSKalle Valo 	if (ar->testmode == 2) {
813cd23c1c9SAlex Yang 		if (ar->hw.fw.utf == NULL) {
814cd23c1c9SAlex Yang 			ath6kl_warn("testmode 2 not supported\n");
815cd23c1c9SAlex Yang 			return -EOPNOTSUPP;
816cd23c1c9SAlex Yang 		}
817cd23c1c9SAlex Yang 
818cd23c1c9SAlex Yang 		snprintf(filename, sizeof(filename), "%s/%s",
819cd23c1c9SAlex Yang 			 ar->hw.fw.dir, ar->hw.fw.utf);
820cd23c1c9SAlex Yang 	} else {
821c0038972SKalle Valo 		if (ar->hw.fw.tcmd == NULL) {
822cd23c1c9SAlex Yang 			ath6kl_warn("testmode 1 not supported\n");
823772c31eeSKalle Valo 			return -EOPNOTSUPP;
824772c31eeSKalle Valo 		}
825772c31eeSKalle Valo 
826c0038972SKalle Valo 		snprintf(filename, sizeof(filename), "%s/%s",
827c0038972SKalle Valo 			 ar->hw.fw.dir, ar->hw.fw.tcmd);
828cd23c1c9SAlex Yang 	}
8295f1127ffSKalle Valo 
830772c31eeSKalle Valo 	set_bit(TESTMODE, &ar->flag);
831772c31eeSKalle Valo 
8325f1127ffSKalle Valo 	ret = ath6kl_get_fw(ar, filename, &ar->fw, &ar->fw_len);
8335f1127ffSKalle Valo 	if (ret) {
8345f1127ffSKalle Valo 		ath6kl_err("Failed to get testmode %d firmware file %s: %d\n",
8355f1127ffSKalle Valo 			   ar->testmode, filename, ret);
8365f1127ffSKalle Valo 		return ret;
837772c31eeSKalle Valo 	}
838772c31eeSKalle Valo 
8395f1127ffSKalle Valo 	return 0;
8405f1127ffSKalle Valo }
8415f1127ffSKalle Valo 
8425f1127ffSKalle Valo static int ath6kl_fetch_fw_file(struct ath6kl *ar)
8435f1127ffSKalle Valo {
8445f1127ffSKalle Valo 	char filename[100];
8455f1127ffSKalle Valo 	int ret;
8465f1127ffSKalle Valo 
8475f1127ffSKalle Valo 	if (ar->fw != NULL)
8485f1127ffSKalle Valo 		return 0;
8495f1127ffSKalle Valo 
850c0038972SKalle Valo 	/* FIXME: remove WARN_ON() as we won't support FW API 1 for long */
851c0038972SKalle Valo 	if (WARN_ON(ar->hw.fw.fw == NULL))
852d1a9421dSKalle Valo 		return -EINVAL;
853d1a9421dSKalle Valo 
854c0038972SKalle Valo 	snprintf(filename, sizeof(filename), "%s/%s",
855c0038972SKalle Valo 		 ar->hw.fw.dir, ar->hw.fw.fw);
856772c31eeSKalle Valo 
857772c31eeSKalle Valo 	ret = ath6kl_get_fw(ar, filename, &ar->fw, &ar->fw_len);
858772c31eeSKalle Valo 	if (ret) {
859772c31eeSKalle Valo 		ath6kl_err("Failed to get firmware file %s: %d\n",
860772c31eeSKalle Valo 			   filename, ret);
861772c31eeSKalle Valo 		return ret;
862772c31eeSKalle Valo 	}
863772c31eeSKalle Valo 
864772c31eeSKalle Valo 	return 0;
865772c31eeSKalle Valo }
866772c31eeSKalle Valo 
867772c31eeSKalle Valo static int ath6kl_fetch_patch_file(struct ath6kl *ar)
868772c31eeSKalle Valo {
869c0038972SKalle Valo 	char filename[100];
870772c31eeSKalle Valo 	int ret;
871772c31eeSKalle Valo 
872d1a9421dSKalle Valo 	if (ar->fw_patch != NULL)
873772c31eeSKalle Valo 		return 0;
874772c31eeSKalle Valo 
875c0038972SKalle Valo 	if (ar->hw.fw.patch == NULL)
876d1a9421dSKalle Valo 		return 0;
877d1a9421dSKalle Valo 
878c0038972SKalle Valo 	snprintf(filename, sizeof(filename), "%s/%s",
879c0038972SKalle Valo 		 ar->hw.fw.dir, ar->hw.fw.patch);
880d1a9421dSKalle Valo 
881772c31eeSKalle Valo 	ret = ath6kl_get_fw(ar, filename, &ar->fw_patch,
882772c31eeSKalle Valo 			    &ar->fw_patch_len);
883772c31eeSKalle Valo 	if (ret) {
884772c31eeSKalle Valo 		ath6kl_err("Failed to get patch file %s: %d\n",
885772c31eeSKalle Valo 			   filename, ret);
886772c31eeSKalle Valo 		return ret;
887772c31eeSKalle Valo 	}
888772c31eeSKalle Valo 
889772c31eeSKalle Valo 	return 0;
890772c31eeSKalle Valo }
891772c31eeSKalle Valo 
892cd23c1c9SAlex Yang static int ath6kl_fetch_testscript_file(struct ath6kl *ar)
893cd23c1c9SAlex Yang {
894cd23c1c9SAlex Yang 	char filename[100];
895cd23c1c9SAlex Yang 	int ret;
896cd23c1c9SAlex Yang 
8975f1127ffSKalle Valo 	if (ar->testmode != 2)
898cd23c1c9SAlex Yang 		return 0;
899cd23c1c9SAlex Yang 
900cd23c1c9SAlex Yang 	if (ar->fw_testscript != NULL)
901cd23c1c9SAlex Yang 		return 0;
902cd23c1c9SAlex Yang 
903cd23c1c9SAlex Yang 	if (ar->hw.fw.testscript == NULL)
904cd23c1c9SAlex Yang 		return 0;
905cd23c1c9SAlex Yang 
906cd23c1c9SAlex Yang 	snprintf(filename, sizeof(filename), "%s/%s",
907cd23c1c9SAlex Yang 		 ar->hw.fw.dir, ar->hw.fw.testscript);
908cd23c1c9SAlex Yang 
909cd23c1c9SAlex Yang 	ret = ath6kl_get_fw(ar, filename, &ar->fw_testscript,
910cd23c1c9SAlex Yang 				&ar->fw_testscript_len);
911cd23c1c9SAlex Yang 	if (ret) {
912cd23c1c9SAlex Yang 		ath6kl_err("Failed to get testscript file %s: %d\n",
913cd23c1c9SAlex Yang 			   filename, ret);
914cd23c1c9SAlex Yang 		return ret;
915cd23c1c9SAlex Yang 	}
916cd23c1c9SAlex Yang 
917cd23c1c9SAlex Yang 	return 0;
918cd23c1c9SAlex Yang }
919cd23c1c9SAlex Yang 
92050d41234SKalle Valo static int ath6kl_fetch_fw_api1(struct ath6kl *ar)
921772c31eeSKalle Valo {
922772c31eeSKalle Valo 	int ret;
923772c31eeSKalle Valo 
924772c31eeSKalle Valo 	ret = ath6kl_fetch_otp_file(ar);
925772c31eeSKalle Valo 	if (ret)
926772c31eeSKalle Valo 		return ret;
927772c31eeSKalle Valo 
928772c31eeSKalle Valo 	ret = ath6kl_fetch_fw_file(ar);
929772c31eeSKalle Valo 	if (ret)
930772c31eeSKalle Valo 		return ret;
931772c31eeSKalle Valo 
932772c31eeSKalle Valo 	ret = ath6kl_fetch_patch_file(ar);
933772c31eeSKalle Valo 	if (ret)
934772c31eeSKalle Valo 		return ret;
935772c31eeSKalle Valo 
936cd23c1c9SAlex Yang 	ret = ath6kl_fetch_testscript_file(ar);
937cd23c1c9SAlex Yang 	if (ret)
938cd23c1c9SAlex Yang 		return ret;
939cd23c1c9SAlex Yang 
940772c31eeSKalle Valo 	return 0;
941772c31eeSKalle Valo }
942bdcd8170SKalle Valo 
94365a8b4ccSKalle Valo static int ath6kl_fetch_fw_apin(struct ath6kl *ar, const char *name)
94450d41234SKalle Valo {
94550d41234SKalle Valo 	size_t magic_len, len, ie_len;
94650d41234SKalle Valo 	const struct firmware *fw;
94750d41234SKalle Valo 	struct ath6kl_fw_ie *hdr;
948c0038972SKalle Valo 	char filename[100];
94950d41234SKalle Valo 	const u8 *data;
95097e0496dSKalle Valo 	int ret, ie_id, i, index, bit;
9518a137480SKalle Valo 	__le32 *val;
95250d41234SKalle Valo 
95365a8b4ccSKalle Valo 	snprintf(filename, sizeof(filename), "%s/%s", ar->hw.fw.dir, name);
95450d41234SKalle Valo 
95550d41234SKalle Valo 	ret = request_firmware(&fw, filename, ar->dev);
95650d41234SKalle Valo 	if (ret)
95750d41234SKalle Valo 		return ret;
95850d41234SKalle Valo 
95950d41234SKalle Valo 	data = fw->data;
96050d41234SKalle Valo 	len = fw->size;
96150d41234SKalle Valo 
96250d41234SKalle Valo 	/* magic also includes the null byte, check that as well */
96350d41234SKalle Valo 	magic_len = strlen(ATH6KL_FIRMWARE_MAGIC) + 1;
96450d41234SKalle Valo 
96550d41234SKalle Valo 	if (len < magic_len) {
96650d41234SKalle Valo 		ret = -EINVAL;
96750d41234SKalle Valo 		goto out;
96850d41234SKalle Valo 	}
96950d41234SKalle Valo 
97050d41234SKalle Valo 	if (memcmp(data, ATH6KL_FIRMWARE_MAGIC, magic_len) != 0) {
97150d41234SKalle Valo 		ret = -EINVAL;
97250d41234SKalle Valo 		goto out;
97350d41234SKalle Valo 	}
97450d41234SKalle Valo 
97550d41234SKalle Valo 	len -= magic_len;
97650d41234SKalle Valo 	data += magic_len;
97750d41234SKalle Valo 
97850d41234SKalle Valo 	/* loop elements */
97950d41234SKalle Valo 	while (len > sizeof(struct ath6kl_fw_ie)) {
98050d41234SKalle Valo 		/* hdr is unaligned! */
98150d41234SKalle Valo 		hdr = (struct ath6kl_fw_ie *) data;
98250d41234SKalle Valo 
98350d41234SKalle Valo 		ie_id = le32_to_cpup(&hdr->id);
98450d41234SKalle Valo 		ie_len = le32_to_cpup(&hdr->len);
98550d41234SKalle Valo 
98650d41234SKalle Valo 		len -= sizeof(*hdr);
98750d41234SKalle Valo 		data += sizeof(*hdr);
98850d41234SKalle Valo 
98950d41234SKalle Valo 		if (len < ie_len) {
99050d41234SKalle Valo 			ret = -EINVAL;
99150d41234SKalle Valo 			goto out;
99250d41234SKalle Valo 		}
99350d41234SKalle Valo 
99450d41234SKalle Valo 		switch (ie_id) {
995b5b6f6a9SNaveen Singh 		case ATH6KL_FW_IE_FW_VERSION:
996b5b6f6a9SNaveen Singh 			strlcpy(ar->wiphy->fw_version, data,
997*53cc3291SBen Greear 				min(sizeof(ar->wiphy->fw_version), ie_len+1));
998b5b6f6a9SNaveen Singh 
999b5b6f6a9SNaveen Singh 			ath6kl_dbg(ATH6KL_DBG_BOOT,
1000b5b6f6a9SNaveen Singh 				   "found fw version %s\n",
1001b5b6f6a9SNaveen Singh 				    ar->wiphy->fw_version);
1002b5b6f6a9SNaveen Singh 			break;
100350d41234SKalle Valo 		case ATH6KL_FW_IE_OTP_IMAGE:
1004ef548626SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT, "found otp image ie (%zd B)\n",
10056bc36431SKalle Valo 				   ie_len);
10066bc36431SKalle Valo 
100750d41234SKalle Valo 			ar->fw_otp = kmemdup(data, ie_len, GFP_KERNEL);
100850d41234SKalle Valo 
100950d41234SKalle Valo 			if (ar->fw_otp == NULL) {
101050d41234SKalle Valo 				ret = -ENOMEM;
101150d41234SKalle Valo 				goto out;
101250d41234SKalle Valo 			}
101350d41234SKalle Valo 
101450d41234SKalle Valo 			ar->fw_otp_len = ie_len;
101550d41234SKalle Valo 			break;
101650d41234SKalle Valo 		case ATH6KL_FW_IE_FW_IMAGE:
1017ef548626SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT, "found fw image ie (%zd B)\n",
10186bc36431SKalle Valo 				   ie_len);
10196bc36431SKalle Valo 
10205f1127ffSKalle Valo 			/* in testmode we already might have a fw file */
10215f1127ffSKalle Valo 			if (ar->fw != NULL)
10225f1127ffSKalle Valo 				break;
10235f1127ffSKalle Valo 
10248437754cSVivek Natarajan 			ar->fw = vmalloc(ie_len);
102550d41234SKalle Valo 
102650d41234SKalle Valo 			if (ar->fw == NULL) {
102750d41234SKalle Valo 				ret = -ENOMEM;
102850d41234SKalle Valo 				goto out;
102950d41234SKalle Valo 			}
103050d41234SKalle Valo 
10318437754cSVivek Natarajan 			memcpy(ar->fw, data, ie_len);
103250d41234SKalle Valo 			ar->fw_len = ie_len;
103350d41234SKalle Valo 			break;
103450d41234SKalle Valo 		case ATH6KL_FW_IE_PATCH_IMAGE:
1035ef548626SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT, "found patch image ie (%zd B)\n",
10366bc36431SKalle Valo 				   ie_len);
10376bc36431SKalle Valo 
103850d41234SKalle Valo 			ar->fw_patch = kmemdup(data, ie_len, GFP_KERNEL);
103950d41234SKalle Valo 
104050d41234SKalle Valo 			if (ar->fw_patch == NULL) {
104150d41234SKalle Valo 				ret = -ENOMEM;
104250d41234SKalle Valo 				goto out;
104350d41234SKalle Valo 			}
104450d41234SKalle Valo 
104550d41234SKalle Valo 			ar->fw_patch_len = ie_len;
104650d41234SKalle Valo 			break;
10478a137480SKalle Valo 		case ATH6KL_FW_IE_RESERVED_RAM_SIZE:
10488a137480SKalle Valo 			val = (__le32 *) data;
10498a137480SKalle Valo 			ar->hw.reserved_ram_size = le32_to_cpup(val);
10506bc36431SKalle Valo 
10516bc36431SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT,
105210d49878SHans Wennborg 				   "found reserved ram size ie %d\n",
10536bc36431SKalle Valo 				   ar->hw.reserved_ram_size);
10548a137480SKalle Valo 			break;
105597e0496dSKalle Valo 		case ATH6KL_FW_IE_CAPABILITIES:
10566bc36431SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT,
1057ef548626SKalle Valo 				   "found firmware capabilities ie (%zd B)\n",
10586bc36431SKalle Valo 				   ie_len);
10596bc36431SKalle Valo 
106097e0496dSKalle Valo 			for (i = 0; i < ATH6KL_FW_CAPABILITY_MAX; i++) {
1061277d90f4SKalle Valo 				index = i / 8;
106297e0496dSKalle Valo 				bit = i % 8;
106397e0496dSKalle Valo 
1064c85251f8SThomas Pedersen 				if (index == ie_len)
1065c85251f8SThomas Pedersen 					break;
1066c85251f8SThomas Pedersen 
106797e0496dSKalle Valo 				if (data[index] & (1 << bit))
106897e0496dSKalle Valo 					__set_bit(i, ar->fw_capabilities);
106997e0496dSKalle Valo 			}
10706bc36431SKalle Valo 
10716bc36431SKalle Valo 			ath6kl_dbg_dump(ATH6KL_DBG_BOOT, "capabilities", "",
10726bc36431SKalle Valo 					ar->fw_capabilities,
10736bc36431SKalle Valo 					sizeof(ar->fw_capabilities));
107497e0496dSKalle Valo 			break;
10751b4304daSKalle Valo 		case ATH6KL_FW_IE_PATCH_ADDR:
10761b4304daSKalle Valo 			if (ie_len != sizeof(*val))
10771b4304daSKalle Valo 				break;
10781b4304daSKalle Valo 
10791b4304daSKalle Valo 			val = (__le32 *) data;
10801b4304daSKalle Valo 			ar->hw.dataset_patch_addr = le32_to_cpup(val);
10816bc36431SKalle Valo 
10826bc36431SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT,
108303ef0250SKalle Valo 				   "found patch address ie 0x%x\n",
10846bc36431SKalle Valo 				   ar->hw.dataset_patch_addr);
10851b4304daSKalle Valo 			break;
108603ef0250SKalle Valo 		case ATH6KL_FW_IE_BOARD_ADDR:
108703ef0250SKalle Valo 			if (ie_len != sizeof(*val))
108803ef0250SKalle Valo 				break;
108903ef0250SKalle Valo 
109003ef0250SKalle Valo 			val = (__le32 *) data;
109103ef0250SKalle Valo 			ar->hw.board_addr = le32_to_cpup(val);
109203ef0250SKalle Valo 
109303ef0250SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT,
109403ef0250SKalle Valo 				   "found board address ie 0x%x\n",
109503ef0250SKalle Valo 				   ar->hw.board_addr);
109603ef0250SKalle Valo 			break;
1097368b1b0fSKalle Valo 		case ATH6KL_FW_IE_VIF_MAX:
1098368b1b0fSKalle Valo 			if (ie_len != sizeof(*val))
1099368b1b0fSKalle Valo 				break;
1100368b1b0fSKalle Valo 
1101368b1b0fSKalle Valo 			val = (__le32 *) data;
1102368b1b0fSKalle Valo 			ar->vif_max = min_t(unsigned int, le32_to_cpup(val),
1103368b1b0fSKalle Valo 					    ATH6KL_VIF_MAX);
1104368b1b0fSKalle Valo 
1105f143379dSVasanthakumar Thiagarajan 			if (ar->vif_max > 1 && !ar->p2p)
1106f143379dSVasanthakumar Thiagarajan 				ar->max_norm_iface = 2;
1107f143379dSVasanthakumar Thiagarajan 
1108368b1b0fSKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT,
1109368b1b0fSKalle Valo 				   "found vif max ie %d\n", ar->vif_max);
1110368b1b0fSKalle Valo 			break;
111150d41234SKalle Valo 		default:
11126bc36431SKalle Valo 			ath6kl_dbg(ATH6KL_DBG_BOOT, "Unknown fw ie: %u\n",
111350d41234SKalle Valo 				   le32_to_cpup(&hdr->id));
111450d41234SKalle Valo 			break;
111550d41234SKalle Valo 		}
111650d41234SKalle Valo 
111750d41234SKalle Valo 		len -= ie_len;
111850d41234SKalle Valo 		data += ie_len;
111950d41234SKalle Valo 	};
112050d41234SKalle Valo 
112150d41234SKalle Valo 	ret = 0;
112250d41234SKalle Valo out:
112350d41234SKalle Valo 	release_firmware(fw);
112450d41234SKalle Valo 
112550d41234SKalle Valo 	return ret;
112650d41234SKalle Valo }
112750d41234SKalle Valo 
112845eaa78fSKalle Valo int ath6kl_init_fetch_firmwares(struct ath6kl *ar)
112950d41234SKalle Valo {
113050d41234SKalle Valo 	int ret;
113150d41234SKalle Valo 
113250d41234SKalle Valo 	ret = ath6kl_fetch_board_file(ar);
113350d41234SKalle Valo 	if (ret)
113450d41234SKalle Valo 		return ret;
113550d41234SKalle Valo 
11365f1127ffSKalle Valo 	ret = ath6kl_fetch_testmode_file(ar);
11375f1127ffSKalle Valo 	if (ret)
11385f1127ffSKalle Valo 		return ret;
11395f1127ffSKalle Valo 
114078803770SJessica Wu 	ret = ath6kl_fetch_fw_apin(ar, ATH6KL_FW_API5_FILE);
114178803770SJessica Wu 	if (ret == 0) {
114278803770SJessica Wu 		ar->fw_api = 5;
114378803770SJessica Wu 		goto out;
114478803770SJessica Wu 	}
114578803770SJessica Wu 
1146b1f47e3aSThomas Pedersen 	ret = ath6kl_fetch_fw_apin(ar, ATH6KL_FW_API4_FILE);
1147b1f47e3aSThomas Pedersen 	if (ret == 0) {
1148b1f47e3aSThomas Pedersen 		ar->fw_api = 4;
1149b1f47e3aSThomas Pedersen 		goto out;
1150b1f47e3aSThomas Pedersen 	}
1151b1f47e3aSThomas Pedersen 
115265a8b4ccSKalle Valo 	ret = ath6kl_fetch_fw_apin(ar, ATH6KL_FW_API3_FILE);
11536bc36431SKalle Valo 	if (ret == 0) {
115465a8b4ccSKalle Valo 		ar->fw_api = 3;
115565a8b4ccSKalle Valo 		goto out;
115665a8b4ccSKalle Valo 	}
115765a8b4ccSKalle Valo 
115865a8b4ccSKalle Valo 	ret = ath6kl_fetch_fw_apin(ar, ATH6KL_FW_API2_FILE);
115965a8b4ccSKalle Valo 	if (ret == 0) {
116065a8b4ccSKalle Valo 		ar->fw_api = 2;
116165a8b4ccSKalle Valo 		goto out;
11626bc36431SKalle Valo 	}
116350d41234SKalle Valo 
116450d41234SKalle Valo 	ret = ath6kl_fetch_fw_api1(ar);
116550d41234SKalle Valo 	if (ret)
116650d41234SKalle Valo 		return ret;
116750d41234SKalle Valo 
116865a8b4ccSKalle Valo 	ar->fw_api = 1;
116965a8b4ccSKalle Valo 
117065a8b4ccSKalle Valo out:
117165a8b4ccSKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "using fw api %d\n", ar->fw_api);
11726bc36431SKalle Valo 
117350d41234SKalle Valo 	return 0;
117450d41234SKalle Valo }
117550d41234SKalle Valo 
1176bdcd8170SKalle Valo static int ath6kl_upload_board_file(struct ath6kl *ar)
1177bdcd8170SKalle Valo {
1178bdcd8170SKalle Valo 	u32 board_address, board_ext_address, param;
117931024d99SKevin Fang 	u32 board_data_size, board_ext_data_size;
1180bdcd8170SKalle Valo 	int ret;
1181bdcd8170SKalle Valo 
1182772c31eeSKalle Valo 	if (WARN_ON(ar->fw_board == NULL))
1183772c31eeSKalle Valo 		return -ENOENT;
1184bdcd8170SKalle Valo 
118531024d99SKevin Fang 	/*
118631024d99SKevin Fang 	 * Determine where in Target RAM to write Board Data.
118731024d99SKevin Fang 	 * For AR6004, host determine Target RAM address for
118831024d99SKevin Fang 	 * writing board data.
118931024d99SKevin Fang 	 */
11900d4d72bfSKalle Valo 	if (ar->hw.board_addr != 0) {
1191b0fc7c1aSKalle Valo 		board_address = ar->hw.board_addr;
119224fc32b3SKalle Valo 		ath6kl_bmi_write_hi32(ar, hi_board_data,
1193b0fc7c1aSKalle Valo 				      board_address);
119431024d99SKevin Fang 	} else {
11951c3d95edSFrederic Danis 		ret = ath6kl_bmi_read_hi32(ar, hi_board_data, &board_address);
11961c3d95edSFrederic Danis 		if (ret) {
11971c3d95edSFrederic Danis 			ath6kl_err("Failed to get board file target address.\n");
11981c3d95edSFrederic Danis 			return ret;
11991c3d95edSFrederic Danis 		}
120031024d99SKevin Fang 	}
120131024d99SKevin Fang 
1202bdcd8170SKalle Valo 	/* determine where in target ram to write extended board data */
12031c3d95edSFrederic Danis 	ret = ath6kl_bmi_read_hi32(ar, hi_board_ext_data, &board_ext_address);
12041c3d95edSFrederic Danis 	if (ret) {
12051c3d95edSFrederic Danis 		ath6kl_err("Failed to get extended board file target address.\n");
12061c3d95edSFrederic Danis 		return ret;
12071c3d95edSFrederic Danis 	}
1208bdcd8170SKalle Valo 
120950e2740bSKalle Valo 	if (ar->target_type == TARGET_TYPE_AR6003 &&
121050e2740bSKalle Valo 	    board_ext_address == 0) {
1211bdcd8170SKalle Valo 		ath6kl_err("Failed to get board file target address.\n");
1212bdcd8170SKalle Valo 		return -EINVAL;
1213bdcd8170SKalle Valo 	}
1214bdcd8170SKalle Valo 
121531024d99SKevin Fang 	switch (ar->target_type) {
121631024d99SKevin Fang 	case TARGET_TYPE_AR6003:
121731024d99SKevin Fang 		board_data_size = AR6003_BOARD_DATA_SZ;
121831024d99SKevin Fang 		board_ext_data_size = AR6003_BOARD_EXT_DATA_SZ;
1219fb1ac2efSPrasanna Kumar 		if (ar->fw_board_len > (board_data_size + board_ext_data_size))
1220fb1ac2efSPrasanna Kumar 			board_ext_data_size = AR6003_BOARD_EXT_DATA_SZ_V2;
122131024d99SKevin Fang 		break;
122231024d99SKevin Fang 	case TARGET_TYPE_AR6004:
122331024d99SKevin Fang 		board_data_size = AR6004_BOARD_DATA_SZ;
122431024d99SKevin Fang 		board_ext_data_size = AR6004_BOARD_EXT_DATA_SZ;
122531024d99SKevin Fang 		break;
122631024d99SKevin Fang 	default:
122731024d99SKevin Fang 		WARN_ON(1);
122831024d99SKevin Fang 		return -EINVAL;
122931024d99SKevin Fang 	}
123031024d99SKevin Fang 
123150e2740bSKalle Valo 	if (board_ext_address &&
123250e2740bSKalle Valo 	    ar->fw_board_len == (board_data_size + board_ext_data_size)) {
1233bdcd8170SKalle Valo 		/* write extended board data */
12346bc36431SKalle Valo 		ath6kl_dbg(ATH6KL_DBG_BOOT,
12356bc36431SKalle Valo 			   "writing extended board data to 0x%x (%d B)\n",
12366bc36431SKalle Valo 			   board_ext_address, board_ext_data_size);
12376bc36431SKalle Valo 
1238bdcd8170SKalle Valo 		ret = ath6kl_bmi_write(ar, board_ext_address,
123931024d99SKevin Fang 				       ar->fw_board + board_data_size,
124031024d99SKevin Fang 				       board_ext_data_size);
1241bdcd8170SKalle Valo 		if (ret) {
1242bdcd8170SKalle Valo 			ath6kl_err("Failed to write extended board data: %d\n",
1243bdcd8170SKalle Valo 				   ret);
1244bdcd8170SKalle Valo 			return ret;
1245bdcd8170SKalle Valo 		}
1246bdcd8170SKalle Valo 
1247bdcd8170SKalle Valo 		/* record that extended board data is initialized */
124831024d99SKevin Fang 		param = (board_ext_data_size << 16) | 1;
124931024d99SKevin Fang 
125024fc32b3SKalle Valo 		ath6kl_bmi_write_hi32(ar, hi_board_ext_data_config, param);
1251bdcd8170SKalle Valo 	}
1252bdcd8170SKalle Valo 
125331024d99SKevin Fang 	if (ar->fw_board_len < board_data_size) {
1254bdcd8170SKalle Valo 		ath6kl_err("Too small board file: %zu\n", ar->fw_board_len);
1255bdcd8170SKalle Valo 		ret = -EINVAL;
1256bdcd8170SKalle Valo 		return ret;
1257bdcd8170SKalle Valo 	}
1258bdcd8170SKalle Valo 
12596bc36431SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "writing board file to 0x%x (%d B)\n",
12606bc36431SKalle Valo 		   board_address, board_data_size);
12616bc36431SKalle Valo 
1262bdcd8170SKalle Valo 	ret = ath6kl_bmi_write(ar, board_address, ar->fw_board,
126331024d99SKevin Fang 			       board_data_size);
1264bdcd8170SKalle Valo 
1265bdcd8170SKalle Valo 	if (ret) {
1266bdcd8170SKalle Valo 		ath6kl_err("Board file bmi write failed: %d\n", ret);
1267bdcd8170SKalle Valo 		return ret;
1268bdcd8170SKalle Valo 	}
1269bdcd8170SKalle Valo 
1270bdcd8170SKalle Valo 	/* record the fact that Board Data IS initialized */
127178803770SJessica Wu 	if ((ar->version.target_ver == AR6004_HW_1_3_VERSION) ||
127278803770SJessica Wu 	    (ar->version.target_ver == AR6004_HW_3_0_VERSION))
127378803770SJessica Wu 		param = board_data_size;
127478803770SJessica Wu 	else
127578803770SJessica Wu 		param = 1;
127678803770SJessica Wu 
127778803770SJessica Wu 	ath6kl_bmi_write_hi32(ar, hi_board_data_initialized, param);
1278bdcd8170SKalle Valo 
1279bdcd8170SKalle Valo 	return ret;
1280bdcd8170SKalle Valo }
1281bdcd8170SKalle Valo 
1282bdcd8170SKalle Valo static int ath6kl_upload_otp(struct ath6kl *ar)
1283bdcd8170SKalle Valo {
1284bdcd8170SKalle Valo 	u32 address, param;
1285bef26a7fSKalle Valo 	bool from_hw = false;
1286bdcd8170SKalle Valo 	int ret;
1287bdcd8170SKalle Valo 
128850e2740bSKalle Valo 	if (ar->fw_otp == NULL)
128950e2740bSKalle Valo 		return 0;
1290bdcd8170SKalle Valo 
1291a01ac414SKalle Valo 	address = ar->hw.app_load_addr;
1292bdcd8170SKalle Valo 
1293ef548626SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "writing otp to 0x%x (%zd B)\n", address,
12946bc36431SKalle Valo 		   ar->fw_otp_len);
12956bc36431SKalle Valo 
1296bdcd8170SKalle Valo 	ret = ath6kl_bmi_fast_download(ar, address, ar->fw_otp,
1297bdcd8170SKalle Valo 				       ar->fw_otp_len);
1298bdcd8170SKalle Valo 	if (ret) {
1299bdcd8170SKalle Valo 		ath6kl_err("Failed to upload OTP file: %d\n", ret);
1300bdcd8170SKalle Valo 		return ret;
1301bdcd8170SKalle Valo 	}
1302bdcd8170SKalle Valo 
1303639d0b89SKalle Valo 	/* read firmware start address */
130480fb2686SKalle Valo 	ret = ath6kl_bmi_read_hi32(ar, hi_app_start, &address);
1305639d0b89SKalle Valo 
1306639d0b89SKalle Valo 	if (ret) {
1307639d0b89SKalle Valo 		ath6kl_err("Failed to read hi_app_start: %d\n", ret);
1308639d0b89SKalle Valo 		return ret;
1309639d0b89SKalle Valo 	}
1310639d0b89SKalle Valo 
1311bef26a7fSKalle Valo 	if (ar->hw.app_start_override_addr == 0) {
1312639d0b89SKalle Valo 		ar->hw.app_start_override_addr = address;
1313bef26a7fSKalle Valo 		from_hw = true;
1314bef26a7fSKalle Valo 	}
1315639d0b89SKalle Valo 
1316bef26a7fSKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "app_start_override_addr%s 0x%x\n",
1317bef26a7fSKalle Valo 		   from_hw ? " (from hw)" : "",
13186bc36431SKalle Valo 		   ar->hw.app_start_override_addr);
13196bc36431SKalle Valo 
1320bdcd8170SKalle Valo 	/* execute the OTP code */
1321bef26a7fSKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "executing OTP at 0x%x\n",
1322bef26a7fSKalle Valo 		   ar->hw.app_start_override_addr);
1323bdcd8170SKalle Valo 	param = 0;
1324bef26a7fSKalle Valo 	ath6kl_bmi_execute(ar, ar->hw.app_start_override_addr, &param);
1325bdcd8170SKalle Valo 
1326bdcd8170SKalle Valo 	return ret;
1327bdcd8170SKalle Valo }
1328bdcd8170SKalle Valo 
1329bdcd8170SKalle Valo static int ath6kl_upload_firmware(struct ath6kl *ar)
1330bdcd8170SKalle Valo {
1331bdcd8170SKalle Valo 	u32 address;
1332bdcd8170SKalle Valo 	int ret;
1333bdcd8170SKalle Valo 
1334772c31eeSKalle Valo 	if (WARN_ON(ar->fw == NULL))
133550e2740bSKalle Valo 		return 0;
1336bdcd8170SKalle Valo 
1337a01ac414SKalle Valo 	address = ar->hw.app_load_addr;
1338bdcd8170SKalle Valo 
1339ef548626SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "writing firmware to 0x%x (%zd B)\n",
13406bc36431SKalle Valo 		   address, ar->fw_len);
13416bc36431SKalle Valo 
1342bdcd8170SKalle Valo 	ret = ath6kl_bmi_fast_download(ar, address, ar->fw, ar->fw_len);
1343bdcd8170SKalle Valo 
1344bdcd8170SKalle Valo 	if (ret) {
1345bdcd8170SKalle Valo 		ath6kl_err("Failed to write firmware: %d\n", ret);
1346bdcd8170SKalle Valo 		return ret;
1347bdcd8170SKalle Valo 	}
1348bdcd8170SKalle Valo 
134931024d99SKevin Fang 	/*
135031024d99SKevin Fang 	 * Set starting address for firmware
135131024d99SKevin Fang 	 * Don't need to setup app_start override addr on AR6004
135231024d99SKevin Fang 	 */
135331024d99SKevin Fang 	if (ar->target_type != TARGET_TYPE_AR6004) {
1354a01ac414SKalle Valo 		address = ar->hw.app_start_override_addr;
1355bdcd8170SKalle Valo 		ath6kl_bmi_set_app_start(ar, address);
135631024d99SKevin Fang 	}
1357bdcd8170SKalle Valo 	return ret;
1358bdcd8170SKalle Valo }
1359bdcd8170SKalle Valo 
1360bdcd8170SKalle Valo static int ath6kl_upload_patch(struct ath6kl *ar)
1361bdcd8170SKalle Valo {
136224fc32b3SKalle Valo 	u32 address;
1363bdcd8170SKalle Valo 	int ret;
1364bdcd8170SKalle Valo 
136550e2740bSKalle Valo 	if (ar->fw_patch == NULL)
136650e2740bSKalle Valo 		return 0;
1367bdcd8170SKalle Valo 
1368a01ac414SKalle Valo 	address = ar->hw.dataset_patch_addr;
1369bdcd8170SKalle Valo 
1370ef548626SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "writing patch to 0x%x (%zd B)\n",
13716bc36431SKalle Valo 		   address, ar->fw_patch_len);
13726bc36431SKalle Valo 
1373bdcd8170SKalle Valo 	ret = ath6kl_bmi_write(ar, address, ar->fw_patch, ar->fw_patch_len);
1374bdcd8170SKalle Valo 	if (ret) {
1375bdcd8170SKalle Valo 		ath6kl_err("Failed to write patch file: %d\n", ret);
1376bdcd8170SKalle Valo 		return ret;
1377bdcd8170SKalle Valo 	}
1378bdcd8170SKalle Valo 
137924fc32b3SKalle Valo 	ath6kl_bmi_write_hi32(ar, hi_dset_list_head, address);
1380bdcd8170SKalle Valo 
1381bdcd8170SKalle Valo 	return 0;
1382bdcd8170SKalle Valo }
1383bdcd8170SKalle Valo 
1384cd23c1c9SAlex Yang static int ath6kl_upload_testscript(struct ath6kl *ar)
1385cd23c1c9SAlex Yang {
138624fc32b3SKalle Valo 	u32 address;
1387cd23c1c9SAlex Yang 	int ret;
1388cd23c1c9SAlex Yang 
13895f1127ffSKalle Valo 	if (ar->testmode != 2)
1390cd23c1c9SAlex Yang 		return 0;
1391cd23c1c9SAlex Yang 
1392cd23c1c9SAlex Yang 	if (ar->fw_testscript == NULL)
1393cd23c1c9SAlex Yang 		return 0;
1394cd23c1c9SAlex Yang 
1395cd23c1c9SAlex Yang 	address = ar->hw.testscript_addr;
1396cd23c1c9SAlex Yang 
1397cd23c1c9SAlex Yang 	ath6kl_dbg(ATH6KL_DBG_BOOT, "writing testscript to 0x%x (%zd B)\n",
1398cd23c1c9SAlex Yang 		   address, ar->fw_testscript_len);
1399cd23c1c9SAlex Yang 
1400cd23c1c9SAlex Yang 	ret = ath6kl_bmi_write(ar, address, ar->fw_testscript,
1401cd23c1c9SAlex Yang 		ar->fw_testscript_len);
1402cd23c1c9SAlex Yang 	if (ret) {
1403cd23c1c9SAlex Yang 		ath6kl_err("Failed to write testscript file: %d\n", ret);
1404cd23c1c9SAlex Yang 		return ret;
1405cd23c1c9SAlex Yang 	}
1406cd23c1c9SAlex Yang 
140724fc32b3SKalle Valo 	ath6kl_bmi_write_hi32(ar, hi_ota_testscript, address);
140878803770SJessica Wu 
140978803770SJessica Wu 	if ((ar->version.target_ver != AR6004_HW_1_3_VERSION) &&
141078803770SJessica Wu 	    (ar->version.target_ver != AR6004_HW_3_0_VERSION))
141124fc32b3SKalle Valo 		ath6kl_bmi_write_hi32(ar, hi_end_ram_reserve_sz, 4096);
141278803770SJessica Wu 
141324fc32b3SKalle Valo 	ath6kl_bmi_write_hi32(ar, hi_test_apps_related, 1);
1414cd23c1c9SAlex Yang 
1415cd23c1c9SAlex Yang 	return 0;
1416cd23c1c9SAlex Yang }
1417cd23c1c9SAlex Yang 
1418bdcd8170SKalle Valo static int ath6kl_init_upload(struct ath6kl *ar)
1419bdcd8170SKalle Valo {
1420bdcd8170SKalle Valo 	u32 param, options, sleep, address;
1421bdcd8170SKalle Valo 	int status = 0;
1422bdcd8170SKalle Valo 
142331024d99SKevin Fang 	if (ar->target_type != TARGET_TYPE_AR6003 &&
142431024d99SKevin Fang 	    ar->target_type != TARGET_TYPE_AR6004)
1425bdcd8170SKalle Valo 		return -EINVAL;
1426bdcd8170SKalle Valo 
1427bdcd8170SKalle Valo 	/* temporarily disable system sleep */
1428bdcd8170SKalle Valo 	address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
1429bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_read(ar, address, &param);
1430bdcd8170SKalle Valo 	if (status)
1431bdcd8170SKalle Valo 		return status;
1432bdcd8170SKalle Valo 
1433bdcd8170SKalle Valo 	options = param;
1434bdcd8170SKalle Valo 
1435bdcd8170SKalle Valo 	param |= ATH6KL_OPTION_SLEEP_DISABLE;
1436bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_write(ar, address, param);
1437bdcd8170SKalle Valo 	if (status)
1438bdcd8170SKalle Valo 		return status;
1439bdcd8170SKalle Valo 
1440bdcd8170SKalle Valo 	address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1441bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_read(ar, address, &param);
1442bdcd8170SKalle Valo 	if (status)
1443bdcd8170SKalle Valo 		return status;
1444bdcd8170SKalle Valo 
1445bdcd8170SKalle Valo 	sleep = param;
1446bdcd8170SKalle Valo 
1447bdcd8170SKalle Valo 	param |= SM(SYSTEM_SLEEP_DISABLE, 1);
1448bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_write(ar, address, param);
1449bdcd8170SKalle Valo 	if (status)
1450bdcd8170SKalle Valo 		return status;
1451bdcd8170SKalle Valo 
1452bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_TRC, "old options: %d, old sleep: %d\n",
1453bdcd8170SKalle Valo 		   options, sleep);
1454bdcd8170SKalle Valo 
1455bdcd8170SKalle Valo 	/* program analog PLL register */
145631024d99SKevin Fang 	/* no need to control 40/44MHz clock on AR6004 */
145731024d99SKevin Fang 	if (ar->target_type != TARGET_TYPE_AR6004) {
1458bdcd8170SKalle Valo 		status = ath6kl_bmi_reg_write(ar, ATH6KL_ANALOG_PLL_REGISTER,
1459bdcd8170SKalle Valo 					      0xF9104001);
146031024d99SKevin Fang 
1461bdcd8170SKalle Valo 		if (status)
1462bdcd8170SKalle Valo 			return status;
1463bdcd8170SKalle Valo 
1464bdcd8170SKalle Valo 		/* Run at 80/88MHz by default */
1465bdcd8170SKalle Valo 		param = SM(CPU_CLOCK_STANDARD, 1);
1466bdcd8170SKalle Valo 
1467bdcd8170SKalle Valo 		address = RTC_BASE_ADDRESS + CPU_CLOCK_ADDRESS;
1468bdcd8170SKalle Valo 		status = ath6kl_bmi_reg_write(ar, address, param);
1469bdcd8170SKalle Valo 		if (status)
1470bdcd8170SKalle Valo 			return status;
147131024d99SKevin Fang 	}
1472bdcd8170SKalle Valo 
1473bdcd8170SKalle Valo 	param = 0;
1474bdcd8170SKalle Valo 	address = RTC_BASE_ADDRESS + LPO_CAL_ADDRESS;
1475bdcd8170SKalle Valo 	param = SM(LPO_CAL_ENABLE, 1);
1476bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_write(ar, address, param);
1477bdcd8170SKalle Valo 	if (status)
1478bdcd8170SKalle Valo 		return status;
1479bdcd8170SKalle Valo 
1480bdcd8170SKalle Valo 	/* WAR to avoid SDIO CRC err */
1481a2e1be33SMohammed Shafi Shajakhan 	if (ar->hw.flags & ATH6KL_HW_SDIO_CRC_ERROR_WAR) {
1482bdcd8170SKalle Valo 		ath6kl_err("temporary war to avoid sdio crc error\n");
1483bdcd8170SKalle Valo 
1484fa338be0SVasanthakumar Thiagarajan 		param = 0x28;
1485fa338be0SVasanthakumar Thiagarajan 		address = GPIO_BASE_ADDRESS + GPIO_PIN9_ADDRESS;
1486fa338be0SVasanthakumar Thiagarajan 		status = ath6kl_bmi_reg_write(ar, address, param);
1487fa338be0SVasanthakumar Thiagarajan 		if (status)
1488fa338be0SVasanthakumar Thiagarajan 			return status;
1489fa338be0SVasanthakumar Thiagarajan 
1490bdcd8170SKalle Valo 		param = 0x20;
1491bdcd8170SKalle Valo 
1492bdcd8170SKalle Valo 		address = GPIO_BASE_ADDRESS + GPIO_PIN10_ADDRESS;
1493bdcd8170SKalle Valo 		status = ath6kl_bmi_reg_write(ar, address, param);
1494bdcd8170SKalle Valo 		if (status)
1495bdcd8170SKalle Valo 			return status;
1496bdcd8170SKalle Valo 
1497bdcd8170SKalle Valo 		address = GPIO_BASE_ADDRESS + GPIO_PIN11_ADDRESS;
1498bdcd8170SKalle Valo 		status = ath6kl_bmi_reg_write(ar, address, param);
1499bdcd8170SKalle Valo 		if (status)
1500bdcd8170SKalle Valo 			return status;
1501bdcd8170SKalle Valo 
1502bdcd8170SKalle Valo 		address = GPIO_BASE_ADDRESS + GPIO_PIN12_ADDRESS;
1503bdcd8170SKalle Valo 		status = ath6kl_bmi_reg_write(ar, address, param);
1504bdcd8170SKalle Valo 		if (status)
1505bdcd8170SKalle Valo 			return status;
1506bdcd8170SKalle Valo 
1507bdcd8170SKalle Valo 		address = GPIO_BASE_ADDRESS + GPIO_PIN13_ADDRESS;
1508bdcd8170SKalle Valo 		status = ath6kl_bmi_reg_write(ar, address, param);
1509bdcd8170SKalle Valo 		if (status)
1510bdcd8170SKalle Valo 			return status;
1511bdcd8170SKalle Valo 	}
1512bdcd8170SKalle Valo 
1513bdcd8170SKalle Valo 	/* write EEPROM data to Target RAM */
1514bdcd8170SKalle Valo 	status = ath6kl_upload_board_file(ar);
1515bdcd8170SKalle Valo 	if (status)
1516bdcd8170SKalle Valo 		return status;
1517bdcd8170SKalle Valo 
1518bdcd8170SKalle Valo 	/* transfer One time Programmable data */
1519bdcd8170SKalle Valo 	status = ath6kl_upload_otp(ar);
1520bdcd8170SKalle Valo 	if (status)
1521bdcd8170SKalle Valo 		return status;
1522bdcd8170SKalle Valo 
1523bdcd8170SKalle Valo 	/* Download Target firmware */
1524bdcd8170SKalle Valo 	status = ath6kl_upload_firmware(ar);
1525bdcd8170SKalle Valo 	if (status)
1526bdcd8170SKalle Valo 		return status;
1527bdcd8170SKalle Valo 
1528bdcd8170SKalle Valo 	status = ath6kl_upload_patch(ar);
1529bdcd8170SKalle Valo 	if (status)
1530bdcd8170SKalle Valo 		return status;
1531bdcd8170SKalle Valo 
1532cd23c1c9SAlex Yang 	/* Download the test script */
1533cd23c1c9SAlex Yang 	status = ath6kl_upload_testscript(ar);
1534cd23c1c9SAlex Yang 	if (status)
1535cd23c1c9SAlex Yang 		return status;
1536cd23c1c9SAlex Yang 
1537bdcd8170SKalle Valo 	/* Restore system sleep */
1538bdcd8170SKalle Valo 	address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1539bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_write(ar, address, sleep);
1540bdcd8170SKalle Valo 	if (status)
1541bdcd8170SKalle Valo 		return status;
1542bdcd8170SKalle Valo 
1543bdcd8170SKalle Valo 	address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
1544bdcd8170SKalle Valo 	param = options | 0x20;
1545bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_write(ar, address, param);
1546bdcd8170SKalle Valo 	if (status)
1547bdcd8170SKalle Valo 		return status;
1548bdcd8170SKalle Valo 
1549bdcd8170SKalle Valo 	return status;
1550bdcd8170SKalle Valo }
1551bdcd8170SKalle Valo 
155245eaa78fSKalle Valo int ath6kl_init_hw_params(struct ath6kl *ar)
1553a01ac414SKalle Valo {
15541b46dc04SKalle Valo 	const struct ath6kl_hw *uninitialized_var(hw);
1555856f4b31SKalle Valo 	int i;
1556bef26a7fSKalle Valo 
1557856f4b31SKalle Valo 	for (i = 0; i < ARRAY_SIZE(hw_list); i++) {
1558856f4b31SKalle Valo 		hw = &hw_list[i];
1559bef26a7fSKalle Valo 
1560856f4b31SKalle Valo 		if (hw->id == ar->version.target_ver)
1561a01ac414SKalle Valo 			break;
1562856f4b31SKalle Valo 	}
1563856f4b31SKalle Valo 
1564856f4b31SKalle Valo 	if (i == ARRAY_SIZE(hw_list)) {
1565a01ac414SKalle Valo 		ath6kl_err("Unsupported hardware version: 0x%x\n",
1566a01ac414SKalle Valo 			   ar->version.target_ver);
1567a01ac414SKalle Valo 		return -EINVAL;
1568a01ac414SKalle Valo 	}
1569a01ac414SKalle Valo 
1570856f4b31SKalle Valo 	ar->hw = *hw;
1571856f4b31SKalle Valo 
15726bc36431SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT,
15736bc36431SKalle Valo 		   "target_ver 0x%x target_type 0x%x dataset_patch 0x%x app_load_addr 0x%x\n",
15746bc36431SKalle Valo 		   ar->version.target_ver, ar->target_type,
15756bc36431SKalle Valo 		   ar->hw.dataset_patch_addr, ar->hw.app_load_addr);
15766bc36431SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT,
15776bc36431SKalle Valo 		   "app_start_override_addr 0x%x board_ext_data_addr 0x%x reserved_ram_size 0x%x",
15786bc36431SKalle Valo 		   ar->hw.app_start_override_addr, ar->hw.board_ext_data_addr,
15796bc36431SKalle Valo 		   ar->hw.reserved_ram_size);
158039586bf2SRyan Hsu 	ath6kl_dbg(ATH6KL_DBG_BOOT,
158139586bf2SRyan Hsu 		   "refclk_hz %d uarttx_pin %d",
158239586bf2SRyan Hsu 		   ar->hw.refclk_hz, ar->hw.uarttx_pin);
15836bc36431SKalle Valo 
1584a01ac414SKalle Valo 	return 0;
1585a01ac414SKalle Valo }
1586a01ac414SKalle Valo 
1587293badf4SKalle Valo static const char *ath6kl_init_get_hif_name(enum ath6kl_hif_type type)
1588293badf4SKalle Valo {
1589293badf4SKalle Valo 	switch (type) {
1590293badf4SKalle Valo 	case ATH6KL_HIF_TYPE_SDIO:
1591293badf4SKalle Valo 		return "sdio";
1592293badf4SKalle Valo 	case ATH6KL_HIF_TYPE_USB:
1593293badf4SKalle Valo 		return "usb";
1594293badf4SKalle Valo 	}
1595293badf4SKalle Valo 
1596293badf4SKalle Valo 	return NULL;
1597293badf4SKalle Valo }
1598293badf4SKalle Valo 
1599e72c2746SKalle Valo 
1600e72c2746SKalle Valo static const struct fw_capa_str_map {
1601e72c2746SKalle Valo 	int id;
1602e72c2746SKalle Valo 	const char *name;
1603e72c2746SKalle Valo } fw_capa_map[] = {
1604e72c2746SKalle Valo 	{ ATH6KL_FW_CAPABILITY_HOST_P2P, "host-p2p" },
1605e72c2746SKalle Valo 	{ ATH6KL_FW_CAPABILITY_SCHED_SCAN, "sched-scan" },
1606e72c2746SKalle Valo 	{ ATH6KL_FW_CAPABILITY_STA_P2PDEV_DUPLEX, "sta-p2pdev-duplex" },
1607e72c2746SKalle Valo 	{ ATH6KL_FW_CAPABILITY_INACTIVITY_TIMEOUT, "inactivity-timeout" },
1608e72c2746SKalle Valo 	{ ATH6KL_FW_CAPABILITY_RSN_CAP_OVERRIDE, "rsn-cap-override" },
1609e72c2746SKalle Valo 	{ ATH6KL_FW_CAPABILITY_WOW_MULTICAST_FILTER, "wow-mc-filter" },
1610e72c2746SKalle Valo 	{ ATH6KL_FW_CAPABILITY_BMISS_ENHANCE, "bmiss-enhance" },
1611e72c2746SKalle Valo 	{ ATH6KL_FW_CAPABILITY_SCHED_SCAN_MATCH_LIST, "sscan-match-list" },
1612e72c2746SKalle Valo 	{ ATH6KL_FW_CAPABILITY_RSSI_SCAN_THOLD, "rssi-scan-thold" },
1613e72c2746SKalle Valo 	{ ATH6KL_FW_CAPABILITY_CUSTOM_MAC_ADDR, "custom-mac-addr" },
1614e72c2746SKalle Valo 	{ ATH6KL_FW_CAPABILITY_TX_ERR_NOTIFY, "tx-err-notify" },
1615e72c2746SKalle Valo 	{ ATH6KL_FW_CAPABILITY_REGDOMAIN, "regdomain" },
1616e72c2746SKalle Valo 	{ ATH6KL_FW_CAPABILITY_SCHED_SCAN_V2, "sched-scan-v2" },
1617e72c2746SKalle Valo 	{ ATH6KL_FW_CAPABILITY_HEART_BEAT_POLL, "hb-poll" },
1618eba95bceSKalle Valo 	{ ATH6KL_FW_CAPABILITY_64BIT_RATES, "64bit-rates" },
1619eba95bceSKalle Valo 	{ ATH6KL_FW_CAPABILITY_AP_INACTIVITY_MINS, "ap-inactivity-mins" },
1620eba95bceSKalle Valo 	{ ATH6KL_FW_CAPABILITY_MAP_LP_ENDPOINT, "map-lp-endpoint" },
1621c1d32d30SJessica Wu 	{ ATH6KL_FW_CAPABILITY_RATETABLE_MCS15, "ratetable-mcs15" },
162278803770SJessica Wu 	{ ATH6KL_FW_CAPABILITY_NO_IP_CHECKSUM, "no-ip-checksum" },
1623e72c2746SKalle Valo };
1624e72c2746SKalle Valo 
1625e72c2746SKalle Valo static const char *ath6kl_init_get_fw_capa_name(unsigned int id)
1626e72c2746SKalle Valo {
1627e72c2746SKalle Valo 	int i;
1628e72c2746SKalle Valo 
1629e72c2746SKalle Valo 	for (i = 0; i < ARRAY_SIZE(fw_capa_map); i++) {
1630e72c2746SKalle Valo 		if (fw_capa_map[i].id == id)
1631e72c2746SKalle Valo 			return fw_capa_map[i].name;
1632e72c2746SKalle Valo 	}
1633e72c2746SKalle Valo 
1634e72c2746SKalle Valo 	return "<unknown>";
1635e72c2746SKalle Valo }
1636e72c2746SKalle Valo 
1637e72c2746SKalle Valo static void ath6kl_init_get_fwcaps(struct ath6kl *ar, char *buf, size_t buf_len)
1638e72c2746SKalle Valo {
1639e72c2746SKalle Valo 	u8 *data = (u8 *) ar->fw_capabilities;
1640e72c2746SKalle Valo 	size_t trunc_len, len = 0;
1641e72c2746SKalle Valo 	int i, index, bit;
1642e72c2746SKalle Valo 	char *trunc = "...";
1643e72c2746SKalle Valo 
1644e72c2746SKalle Valo 	for (i = 0; i < ATH6KL_FW_CAPABILITY_MAX; i++) {
1645e72c2746SKalle Valo 		index = i / 8;
1646e72c2746SKalle Valo 		bit = i % 8;
1647e72c2746SKalle Valo 
1648e72c2746SKalle Valo 		if (index >= sizeof(ar->fw_capabilities) * 4)
1649e72c2746SKalle Valo 			break;
1650e72c2746SKalle Valo 
1651e72c2746SKalle Valo 		if (buf_len - len < 4) {
1652e72c2746SKalle Valo 			ath6kl_warn("firmware capability buffer too small!\n");
1653e72c2746SKalle Valo 
1654e72c2746SKalle Valo 			/* add "..." to the end of string */
1655e72c2746SKalle Valo 			trunc_len = strlen(trunc) + 1;
1656e72c2746SKalle Valo 			strncpy(buf + buf_len - trunc_len, trunc, trunc_len);
1657e72c2746SKalle Valo 
1658e72c2746SKalle Valo 			return;
1659e72c2746SKalle Valo 		}
1660e72c2746SKalle Valo 
1661e72c2746SKalle Valo 		if (data[index] & (1 << bit)) {
1662e72c2746SKalle Valo 			len += scnprintf(buf + len, buf_len - len, "%s,",
1663e72c2746SKalle Valo 					    ath6kl_init_get_fw_capa_name(i));
1664e72c2746SKalle Valo 		}
1665e72c2746SKalle Valo 	}
1666e72c2746SKalle Valo 
1667e72c2746SKalle Valo 	/* overwrite the last comma */
1668e72c2746SKalle Valo 	if (len > 0)
1669e72c2746SKalle Valo 		len--;
1670e72c2746SKalle Valo 
1671e72c2746SKalle Valo 	buf[len] = '\0';
1672e72c2746SKalle Valo }
1673e72c2746SKalle Valo 
1674ec1461dcSKalle Valo static int ath6kl_init_hw_reset(struct ath6kl *ar)
1675ec1461dcSKalle Valo {
1676ec1461dcSKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "cold resetting the device");
1677ec1461dcSKalle Valo 
1678ec1461dcSKalle Valo 	return ath6kl_diag_write32(ar, RESET_CONTROL_ADDRESS,
1679ec1461dcSKalle Valo 				   cpu_to_le32(RESET_CONTROL_COLD_RST));
1680ec1461dcSKalle Valo }
1681ec1461dcSKalle Valo 
1682ede615d2SVasanthakumar Thiagarajan static int __ath6kl_init_hw_start(struct ath6kl *ar)
168320459ee2SKalle Valo {
168420459ee2SKalle Valo 	long timeleft;
168520459ee2SKalle Valo 	int ret, i;
1686e72c2746SKalle Valo 	char buf[200];
168720459ee2SKalle Valo 
16885fe4dffbSKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "hw start\n");
16895fe4dffbSKalle Valo 
169020459ee2SKalle Valo 	ret = ath6kl_hif_power_on(ar);
169120459ee2SKalle Valo 	if (ret)
169220459ee2SKalle Valo 		return ret;
169320459ee2SKalle Valo 
169420459ee2SKalle Valo 	ret = ath6kl_configure_target(ar);
169520459ee2SKalle Valo 	if (ret)
169620459ee2SKalle Valo 		goto err_power_off;
169720459ee2SKalle Valo 
169820459ee2SKalle Valo 	ret = ath6kl_init_upload(ar);
169920459ee2SKalle Valo 	if (ret)
170020459ee2SKalle Valo 		goto err_power_off;
170120459ee2SKalle Valo 
170220459ee2SKalle Valo 	/* Do we need to finish the BMI phase */
1703bf978145SMohammed Shafi Shajakhan 	ret = ath6kl_bmi_done(ar);
1704bf978145SMohammed Shafi Shajakhan 	if (ret)
170520459ee2SKalle Valo 		goto err_power_off;
170620459ee2SKalle Valo 
170720459ee2SKalle Valo 	/*
170820459ee2SKalle Valo 	 * The reason we have to wait for the target here is that the
170920459ee2SKalle Valo 	 * driver layer has to init BMI in order to set the host block
171020459ee2SKalle Valo 	 * size.
171120459ee2SKalle Valo 	 */
17124e1609c9SKalle Valo 	ret = ath6kl_htc_wait_target(ar->htc_target);
171344af3442SKalle Valo 
171444af3442SKalle Valo 	if (ret == -ETIMEDOUT) {
171544af3442SKalle Valo 		/*
171644af3442SKalle Valo 		 * Most likely USB target is in odd state after reboot and
171744af3442SKalle Valo 		 * needs a reset. A cold reset makes the whole device
171844af3442SKalle Valo 		 * disappear from USB bus and initialisation starts from
171944af3442SKalle Valo 		 * beginning.
172044af3442SKalle Valo 		 */
172144af3442SKalle Valo 		ath6kl_warn("htc wait target timed out, resetting device\n");
172244af3442SKalle Valo 		ath6kl_init_hw_reset(ar);
172344af3442SKalle Valo 		goto err_power_off;
172444af3442SKalle Valo 	} else if (ret) {
17254e1609c9SKalle Valo 		ath6kl_err("htc wait target failed: %d\n", ret);
172620459ee2SKalle Valo 		goto err_power_off;
172720459ee2SKalle Valo 	}
172820459ee2SKalle Valo 
17294e1609c9SKalle Valo 	ret = ath6kl_init_service_ep(ar);
17304e1609c9SKalle Valo 	if (ret) {
17314e1609c9SKalle Valo 		ath6kl_err("Endpoint service initilisation failed: %d\n", ret);
173220459ee2SKalle Valo 		goto err_cleanup_scatter;
173320459ee2SKalle Valo 	}
173420459ee2SKalle Valo 
173520459ee2SKalle Valo 	/* setup credit distribution */
1736e76ac2bfSKalle Valo 	ath6kl_htc_credit_setup(ar->htc_target, &ar->credit_state_info);
173720459ee2SKalle Valo 
173820459ee2SKalle Valo 	/* start HTC */
173920459ee2SKalle Valo 	ret = ath6kl_htc_start(ar->htc_target);
174020459ee2SKalle Valo 	if (ret) {
174120459ee2SKalle Valo 		/* FIXME: call this */
174220459ee2SKalle Valo 		ath6kl_cookie_cleanup(ar);
174320459ee2SKalle Valo 		goto err_cleanup_scatter;
174420459ee2SKalle Valo 	}
174520459ee2SKalle Valo 
174620459ee2SKalle Valo 	/* Wait for Wmi event to be ready */
174720459ee2SKalle Valo 	timeleft = wait_event_interruptible_timeout(ar->event_wq,
174820459ee2SKalle Valo 						    test_bit(WMI_READY,
174920459ee2SKalle Valo 							     &ar->flag),
175020459ee2SKalle Valo 						    WMI_TIMEOUT);
1751ab1ef141SRaja Mani 	if (timeleft <= 0) {
1752ab1ef141SRaja Mani 		clear_bit(WMI_READY, &ar->flag);
1753ab1ef141SRaja Mani 		ath6kl_err("wmi is not ready or wait was interrupted: %ld\n",
1754ab1ef141SRaja Mani 			   timeleft);
1755ab1ef141SRaja Mani 		ret = -EIO;
1756ab1ef141SRaja Mani 		goto err_htc_stop;
1757ab1ef141SRaja Mani 	}
175820459ee2SKalle Valo 
175920459ee2SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "firmware booted\n");
176020459ee2SKalle Valo 
1761293badf4SKalle Valo 	if (test_and_clear_bit(FIRST_BOOT, &ar->flag)) {
176265a8b4ccSKalle Valo 		ath6kl_info("%s %s fw %s api %d%s\n",
1763293badf4SKalle Valo 			    ar->hw.name,
1764293badf4SKalle Valo 			    ath6kl_init_get_hif_name(ar->hif_type),
1765293badf4SKalle Valo 			    ar->wiphy->fw_version,
176665a8b4ccSKalle Valo 			    ar->fw_api,
1767293badf4SKalle Valo 			    test_bit(TESTMODE, &ar->flag) ? " testmode" : "");
1768e72c2746SKalle Valo 		ath6kl_init_get_fwcaps(ar, buf, sizeof(buf));
1769e72c2746SKalle Valo 		ath6kl_info("firmware supports: %s\n", buf);
1770293badf4SKalle Valo 	}
1771293badf4SKalle Valo 
177220459ee2SKalle Valo 	if (ar->version.abi_ver != ATH6KL_ABI_VERSION) {
177320459ee2SKalle Valo 		ath6kl_err("abi version mismatch: host(0x%x), target(0x%x)\n",
177420459ee2SKalle Valo 			   ATH6KL_ABI_VERSION, ar->version.abi_ver);
177520459ee2SKalle Valo 		ret = -EIO;
177620459ee2SKalle Valo 		goto err_htc_stop;
177720459ee2SKalle Valo 	}
177820459ee2SKalle Valo 
177920459ee2SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_TRC, "%s: wmi is ready\n", __func__);
178020459ee2SKalle Valo 
178120459ee2SKalle Valo 	/* communicate the wmi protocol verision to the target */
178220459ee2SKalle Valo 	/* FIXME: return error */
178320459ee2SKalle Valo 	if ((ath6kl_set_host_app_area(ar)) != 0)
178420459ee2SKalle Valo 		ath6kl_err("unable to set the host app area\n");
178520459ee2SKalle Valo 
178671f96ee6SKalle Valo 	for (i = 0; i < ar->vif_max; i++) {
178720459ee2SKalle Valo 		ret = ath6kl_target_config_wlan_params(ar, i);
178820459ee2SKalle Valo 		if (ret)
178920459ee2SKalle Valo 			goto err_htc_stop;
179020459ee2SKalle Valo 	}
179120459ee2SKalle Valo 
179220459ee2SKalle Valo 	return 0;
179320459ee2SKalle Valo 
179420459ee2SKalle Valo err_htc_stop:
179520459ee2SKalle Valo 	ath6kl_htc_stop(ar->htc_target);
179620459ee2SKalle Valo err_cleanup_scatter:
179720459ee2SKalle Valo 	ath6kl_hif_cleanup_scatter(ar);
179820459ee2SKalle Valo err_power_off:
179920459ee2SKalle Valo 	ath6kl_hif_power_off(ar);
180020459ee2SKalle Valo 
180120459ee2SKalle Valo 	return ret;
180220459ee2SKalle Valo }
180320459ee2SKalle Valo 
1804ede615d2SVasanthakumar Thiagarajan int ath6kl_init_hw_start(struct ath6kl *ar)
1805ede615d2SVasanthakumar Thiagarajan {
1806ede615d2SVasanthakumar Thiagarajan 	int err;
1807ede615d2SVasanthakumar Thiagarajan 
1808ede615d2SVasanthakumar Thiagarajan 	err = __ath6kl_init_hw_start(ar);
1809ede615d2SVasanthakumar Thiagarajan 	if (err)
1810ede615d2SVasanthakumar Thiagarajan 		return err;
1811ede615d2SVasanthakumar Thiagarajan 	ar->state = ATH6KL_STATE_ON;
1812ede615d2SVasanthakumar Thiagarajan 	return 0;
1813ede615d2SVasanthakumar Thiagarajan }
1814ede615d2SVasanthakumar Thiagarajan 
1815ede615d2SVasanthakumar Thiagarajan static int __ath6kl_init_hw_stop(struct ath6kl *ar)
18165fe4dffbSKalle Valo {
18175fe4dffbSKalle Valo 	int ret;
18185fe4dffbSKalle Valo 
18195fe4dffbSKalle Valo 	ath6kl_dbg(ATH6KL_DBG_BOOT, "hw stop\n");
18205fe4dffbSKalle Valo 
18215fe4dffbSKalle Valo 	ath6kl_htc_stop(ar->htc_target);
18225fe4dffbSKalle Valo 
18235fe4dffbSKalle Valo 	ath6kl_hif_stop(ar);
18245fe4dffbSKalle Valo 
18255fe4dffbSKalle Valo 	ath6kl_bmi_reset(ar);
18265fe4dffbSKalle Valo 
18275fe4dffbSKalle Valo 	ret = ath6kl_hif_power_off(ar);
18285fe4dffbSKalle Valo 	if (ret)
18295fe4dffbSKalle Valo 		ath6kl_warn("failed to power off hif: %d\n", ret);
18305fe4dffbSKalle Valo 
1831ede615d2SVasanthakumar Thiagarajan 	return 0;
1832ede615d2SVasanthakumar Thiagarajan }
183376a9fbe2SKalle Valo 
1834ede615d2SVasanthakumar Thiagarajan int ath6kl_init_hw_stop(struct ath6kl *ar)
1835ede615d2SVasanthakumar Thiagarajan {
1836ede615d2SVasanthakumar Thiagarajan 	int err;
1837ede615d2SVasanthakumar Thiagarajan 
1838ede615d2SVasanthakumar Thiagarajan 	err = __ath6kl_init_hw_stop(ar);
1839ede615d2SVasanthakumar Thiagarajan 	if (err)
1840ede615d2SVasanthakumar Thiagarajan 		return err;
1841ede615d2SVasanthakumar Thiagarajan 	ar->state = ATH6KL_STATE_OFF;
18425fe4dffbSKalle Valo 	return 0;
18435fe4dffbSKalle Valo }
18445fe4dffbSKalle Valo 
184584caf800SVasanthakumar Thiagarajan void ath6kl_init_hw_restart(struct ath6kl *ar)
184684caf800SVasanthakumar Thiagarajan {
184758109df6SVasanthakumar Thiagarajan 	clear_bit(WMI_READY, &ar->flag);
184858109df6SVasanthakumar Thiagarajan 
184984caf800SVasanthakumar Thiagarajan 	ath6kl_cfg80211_stop_all(ar);
185084caf800SVasanthakumar Thiagarajan 
185158109df6SVasanthakumar Thiagarajan 	if (__ath6kl_init_hw_stop(ar)) {
185258109df6SVasanthakumar Thiagarajan 		ath6kl_dbg(ATH6KL_DBG_RECOVERY, "Failed to stop during fw error recovery\n");
185384caf800SVasanthakumar Thiagarajan 		return;
185458109df6SVasanthakumar Thiagarajan 	}
185584caf800SVasanthakumar Thiagarajan 
185684caf800SVasanthakumar Thiagarajan 	if (__ath6kl_init_hw_start(ar)) {
185784caf800SVasanthakumar Thiagarajan 		ath6kl_dbg(ATH6KL_DBG_RECOVERY, "Failed to restart during fw error recovery\n");
185884caf800SVasanthakumar Thiagarajan 		return;
185984caf800SVasanthakumar Thiagarajan 	}
186084caf800SVasanthakumar Thiagarajan }
186184caf800SVasanthakumar Thiagarajan 
1862bdcd8170SKalle Valo void ath6kl_stop_txrx(struct ath6kl *ar)
1863bdcd8170SKalle Valo {
1864990bd915SVasanthakumar Thiagarajan 	struct ath6kl_vif *vif, *tmp_vif;
18651d2a4456SVasanthakumar Thiagarajan 	int i;
1866bdcd8170SKalle Valo 
1867bdcd8170SKalle Valo 	set_bit(DESTROY_IN_PROGRESS, &ar->flag);
1868bdcd8170SKalle Valo 
1869bdcd8170SKalle Valo 	if (down_interruptible(&ar->sem)) {
1870bdcd8170SKalle Valo 		ath6kl_err("down_interruptible failed\n");
1871bdcd8170SKalle Valo 		return;
1872bdcd8170SKalle Valo 	}
1873bdcd8170SKalle Valo 
18741d2a4456SVasanthakumar Thiagarajan 	for (i = 0; i < AP_MAX_NUM_STA; i++)
18751d2a4456SVasanthakumar Thiagarajan 		aggr_reset_state(ar->sta_list[i].aggr_conn);
18761d2a4456SVasanthakumar Thiagarajan 
187711f6e40dSVasanthakumar Thiagarajan 	spin_lock_bh(&ar->list_lock);
1878990bd915SVasanthakumar Thiagarajan 	list_for_each_entry_safe(vif, tmp_vif, &ar->vif_list, list) {
1879990bd915SVasanthakumar Thiagarajan 		list_del(&vif->list);
188011f6e40dSVasanthakumar Thiagarajan 		spin_unlock_bh(&ar->list_lock);
1881355b3a98SMohammed Shafi Shajakhan 		ath6kl_cfg80211_vif_stop(vif, test_bit(WMI_READY, &ar->flag));
188227929723SVasanthakumar Thiagarajan 		rtnl_lock();
1883c25889e8SKalle Valo 		ath6kl_cfg80211_vif_cleanup(vif);
188427929723SVasanthakumar Thiagarajan 		rtnl_unlock();
188511f6e40dSVasanthakumar Thiagarajan 		spin_lock_bh(&ar->list_lock);
1886990bd915SVasanthakumar Thiagarajan 	}
188711f6e40dSVasanthakumar Thiagarajan 	spin_unlock_bh(&ar->list_lock);
1888bdcd8170SKalle Valo 
18896db8fa53SVasanthakumar Thiagarajan 	clear_bit(WMI_READY, &ar->flag);
18906db8fa53SVasanthakumar Thiagarajan 
1891f32036e8SVasanthakumar Thiagarajan 	if (ar->fw_recovery.enable)
1892f32036e8SVasanthakumar Thiagarajan 		del_timer_sync(&ar->fw_recovery.hb_timer);
1893f32036e8SVasanthakumar Thiagarajan 
18946db8fa53SVasanthakumar Thiagarajan 	/*
18956db8fa53SVasanthakumar Thiagarajan 	 * After wmi_shudown all WMI events will be dropped. We
18966db8fa53SVasanthakumar Thiagarajan 	 * need to cleanup the buffers allocated in AP mode and
18976db8fa53SVasanthakumar Thiagarajan 	 * give disconnect notification to stack, which usually
18986db8fa53SVasanthakumar Thiagarajan 	 * happens in the disconnect_event. Simulate the disconnect
18996db8fa53SVasanthakumar Thiagarajan 	 * event by calling the function directly. Sometimes
19006db8fa53SVasanthakumar Thiagarajan 	 * disconnect_event will be received when the debug logs
19016db8fa53SVasanthakumar Thiagarajan 	 * are collected.
19026db8fa53SVasanthakumar Thiagarajan 	 */
19036db8fa53SVasanthakumar Thiagarajan 	ath6kl_wmi_shutdown(ar->wmi);
19046db8fa53SVasanthakumar Thiagarajan 
19056db8fa53SVasanthakumar Thiagarajan 	clear_bit(WMI_ENABLED, &ar->flag);
19066db8fa53SVasanthakumar Thiagarajan 	if (ar->htc_target) {
19076db8fa53SVasanthakumar Thiagarajan 		ath6kl_dbg(ATH6KL_DBG_TRC, "%s: shut down htc\n", __func__);
19086db8fa53SVasanthakumar Thiagarajan 		ath6kl_htc_stop(ar->htc_target);
1909bdcd8170SKalle Valo 	}
1910bdcd8170SKalle Valo 
1911bdcd8170SKalle Valo 	/*
19126db8fa53SVasanthakumar Thiagarajan 	 * Try to reset the device if we can. The driver may have been
19136db8fa53SVasanthakumar Thiagarajan 	 * configure NOT to reset the target during a debug session.
1914bdcd8170SKalle Valo 	 */
1915ec1461dcSKalle Valo 	ath6kl_init_hw_reset(ar);
1916bdcd8170SKalle Valo 
1917e8ad9a06SVasanthakumar Thiagarajan 	up(&ar->sem);
1918bdcd8170SKalle Valo }
1919d6a434d6SKalle Valo EXPORT_SYMBOL(ath6kl_stop_txrx);
1920