xref: /linux/drivers/net/wireless/ath/ath6kl/init.c (revision 6bbc7c35ed0fb61c7739e91d5ee7016455770511)
1bdcd8170SKalle Valo 
2bdcd8170SKalle Valo /*
3bdcd8170SKalle Valo  * Copyright (c) 2011 Atheros Communications Inc.
4bdcd8170SKalle Valo  *
5bdcd8170SKalle Valo  * Permission to use, copy, modify, and/or distribute this software for any
6bdcd8170SKalle Valo  * purpose with or without fee is hereby granted, provided that the above
7bdcd8170SKalle Valo  * copyright notice and this permission notice appear in all copies.
8bdcd8170SKalle Valo  *
9bdcd8170SKalle Valo  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10bdcd8170SKalle Valo  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11bdcd8170SKalle Valo  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12bdcd8170SKalle Valo  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13bdcd8170SKalle Valo  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14bdcd8170SKalle Valo  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15bdcd8170SKalle Valo  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16bdcd8170SKalle Valo  */
17bdcd8170SKalle Valo 
18bdcd8170SKalle Valo #include <linux/mmc/sdio_func.h>
19bdcd8170SKalle Valo #include "core.h"
20bdcd8170SKalle Valo #include "cfg80211.h"
21bdcd8170SKalle Valo #include "target.h"
22bdcd8170SKalle Valo #include "debug.h"
23bdcd8170SKalle Valo #include "hif-ops.h"
24bdcd8170SKalle Valo 
25bdcd8170SKalle Valo unsigned int debug_mask;
26003353b0SKalle Valo static unsigned int testmode;
27bdcd8170SKalle Valo 
28bdcd8170SKalle Valo module_param(debug_mask, uint, 0644);
29003353b0SKalle Valo module_param(testmode, uint, 0644);
30bdcd8170SKalle Valo 
31bdcd8170SKalle Valo /*
32bdcd8170SKalle Valo  * Include definitions here that can be used to tune the WLAN module
33bdcd8170SKalle Valo  * behavior. Different customers can tune the behavior as per their needs,
34bdcd8170SKalle Valo  * here.
35bdcd8170SKalle Valo  */
36bdcd8170SKalle Valo 
37bdcd8170SKalle Valo /*
38bdcd8170SKalle Valo  * This configuration item enable/disable keepalive support.
39bdcd8170SKalle Valo  * Keepalive support: In the absence of any data traffic to AP, null
40bdcd8170SKalle Valo  * frames will be sent to the AP at periodic interval, to keep the association
41bdcd8170SKalle Valo  * active. This configuration item defines the periodic interval.
42bdcd8170SKalle Valo  * Use value of zero to disable keepalive support
43bdcd8170SKalle Valo  * Default: 60 seconds
44bdcd8170SKalle Valo  */
45bdcd8170SKalle Valo #define WLAN_CONFIG_KEEP_ALIVE_INTERVAL 60
46bdcd8170SKalle Valo 
47bdcd8170SKalle Valo /*
48bdcd8170SKalle Valo  * This configuration item sets the value of disconnect timeout
49bdcd8170SKalle Valo  * Firmware delays sending the disconnec event to the host for this
50bdcd8170SKalle Valo  * timeout after is gets disconnected from the current AP.
51bdcd8170SKalle Valo  * If the firmware successly roams within the disconnect timeout
52bdcd8170SKalle Valo  * it sends a new connect event
53bdcd8170SKalle Valo  */
54bdcd8170SKalle Valo #define WLAN_CONFIG_DISCONNECT_TIMEOUT 10
55bdcd8170SKalle Valo 
56bdcd8170SKalle Valo #define CONFIG_AR600x_DEBUG_UART_TX_PIN 8
57bdcd8170SKalle Valo 
58bdcd8170SKalle Valo enum addr_type {
59bdcd8170SKalle Valo 	DATASET_PATCH_ADDR,
60bdcd8170SKalle Valo 	APP_LOAD_ADDR,
61bdcd8170SKalle Valo 	APP_START_OVERRIDE_ADDR,
62bdcd8170SKalle Valo };
63bdcd8170SKalle Valo 
64bdcd8170SKalle Valo #define ATH6KL_DATA_OFFSET    64
65bdcd8170SKalle Valo struct sk_buff *ath6kl_buf_alloc(int size)
66bdcd8170SKalle Valo {
67bdcd8170SKalle Valo 	struct sk_buff *skb;
68bdcd8170SKalle Valo 	u16 reserved;
69bdcd8170SKalle Valo 
70bdcd8170SKalle Valo 	/* Add chacheline space at front and back of buffer */
71bdcd8170SKalle Valo 	reserved = (2 * L1_CACHE_BYTES) + ATH6KL_DATA_OFFSET +
721df94a85SVasanthakumar Thiagarajan 		   sizeof(struct htc_packet) + ATH6KL_HTC_ALIGN_BYTES;
73bdcd8170SKalle Valo 	skb = dev_alloc_skb(size + reserved);
74bdcd8170SKalle Valo 
75bdcd8170SKalle Valo 	if (skb)
76bdcd8170SKalle Valo 		skb_reserve(skb, reserved - L1_CACHE_BYTES);
77bdcd8170SKalle Valo 	return skb;
78bdcd8170SKalle Valo }
79bdcd8170SKalle Valo 
80bdcd8170SKalle Valo void ath6kl_init_profile_info(struct ath6kl *ar)
81bdcd8170SKalle Valo {
82bdcd8170SKalle Valo 	ar->ssid_len = 0;
83bdcd8170SKalle Valo 	memset(ar->ssid, 0, sizeof(ar->ssid));
84bdcd8170SKalle Valo 
85bdcd8170SKalle Valo 	ar->dot11_auth_mode = OPEN_AUTH;
86bdcd8170SKalle Valo 	ar->auth_mode = NONE_AUTH;
87bdcd8170SKalle Valo 	ar->prwise_crypto = NONE_CRYPT;
88bdcd8170SKalle Valo 	ar->prwise_crypto_len = 0;
89bdcd8170SKalle Valo 	ar->grp_crypto = NONE_CRYPT;
9038acde3cSEdward Lu 	ar->grp_crypto_len = 0;
91bdcd8170SKalle Valo 	memset(ar->wep_key_list, 0, sizeof(ar->wep_key_list));
92bdcd8170SKalle Valo 	memset(ar->req_bssid, 0, sizeof(ar->req_bssid));
93bdcd8170SKalle Valo 	memset(ar->bssid, 0, sizeof(ar->bssid));
94bdcd8170SKalle Valo 	ar->bss_ch = 0;
95bdcd8170SKalle Valo 	ar->nw_type = ar->next_mode = INFRA_NETWORK;
96bdcd8170SKalle Valo }
97bdcd8170SKalle Valo 
98bdcd8170SKalle Valo static u8 ath6kl_get_fw_iftype(struct ath6kl *ar)
99bdcd8170SKalle Valo {
100bdcd8170SKalle Valo 	switch (ar->nw_type) {
101bdcd8170SKalle Valo 	case INFRA_NETWORK:
102bdcd8170SKalle Valo 		return HI_OPTION_FW_MODE_BSS_STA;
103bdcd8170SKalle Valo 	case ADHOC_NETWORK:
104bdcd8170SKalle Valo 		return HI_OPTION_FW_MODE_IBSS;
105bdcd8170SKalle Valo 	case AP_NETWORK:
106bdcd8170SKalle Valo 		return HI_OPTION_FW_MODE_AP;
107bdcd8170SKalle Valo 	default:
108bdcd8170SKalle Valo 		ath6kl_err("Unsupported interface type :%d\n", ar->nw_type);
109bdcd8170SKalle Valo 		return 0xff;
110bdcd8170SKalle Valo 	}
111bdcd8170SKalle Valo }
112bdcd8170SKalle Valo 
113bdcd8170SKalle Valo static int ath6kl_set_host_app_area(struct ath6kl *ar)
114bdcd8170SKalle Valo {
115bdcd8170SKalle Valo 	u32 address, data;
116bdcd8170SKalle Valo 	struct host_app_area host_app_area;
117bdcd8170SKalle Valo 
118bdcd8170SKalle Valo 	/* Fetch the address of the host_app_area_s
119bdcd8170SKalle Valo 	 * instance in the host interest area */
120bdcd8170SKalle Valo 	address = ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_app_host_interest));
12131024d99SKevin Fang 	address = TARG_VTOP(ar->target_type, address);
122bdcd8170SKalle Valo 
123addb44beSKalle Valo 	if (ath6kl_diag_read32(ar, address, &data))
124bdcd8170SKalle Valo 		return -EIO;
125bdcd8170SKalle Valo 
12631024d99SKevin Fang 	address = TARG_VTOP(ar->target_type, data);
127bdcd8170SKalle Valo 	host_app_area.wmi_protocol_ver = WMI_PROTOCOL_VERSION;
128addb44beSKalle Valo 	if (ath6kl_diag_write(ar, address, (u8 *) &host_app_area,
129addb44beSKalle Valo 			      sizeof(struct host_app_area)))
130bdcd8170SKalle Valo 		return -EIO;
131bdcd8170SKalle Valo 
132bdcd8170SKalle Valo 	return 0;
133bdcd8170SKalle Valo }
134bdcd8170SKalle Valo 
135bdcd8170SKalle Valo static inline void set_ac2_ep_map(struct ath6kl *ar,
136bdcd8170SKalle Valo 				  u8 ac,
137bdcd8170SKalle Valo 				  enum htc_endpoint_id ep)
138bdcd8170SKalle Valo {
139bdcd8170SKalle Valo 	ar->ac2ep_map[ac] = ep;
140bdcd8170SKalle Valo 	ar->ep2ac_map[ep] = ac;
141bdcd8170SKalle Valo }
142bdcd8170SKalle Valo 
143bdcd8170SKalle Valo /* connect to a service */
144bdcd8170SKalle Valo static int ath6kl_connectservice(struct ath6kl *ar,
145bdcd8170SKalle Valo 				 struct htc_service_connect_req  *con_req,
146bdcd8170SKalle Valo 				 char *desc)
147bdcd8170SKalle Valo {
148bdcd8170SKalle Valo 	int status;
149bdcd8170SKalle Valo 	struct htc_service_connect_resp response;
150bdcd8170SKalle Valo 
151bdcd8170SKalle Valo 	memset(&response, 0, sizeof(response));
152bdcd8170SKalle Valo 
153ad226ec2SKalle Valo 	status = ath6kl_htc_conn_service(ar->htc_target, con_req, &response);
154bdcd8170SKalle Valo 	if (status) {
155bdcd8170SKalle Valo 		ath6kl_err("failed to connect to %s service status:%d\n",
156bdcd8170SKalle Valo 			   desc, status);
157bdcd8170SKalle Valo 		return status;
158bdcd8170SKalle Valo 	}
159bdcd8170SKalle Valo 
160bdcd8170SKalle Valo 	switch (con_req->svc_id) {
161bdcd8170SKalle Valo 	case WMI_CONTROL_SVC:
162bdcd8170SKalle Valo 		if (test_bit(WMI_ENABLED, &ar->flag))
163bdcd8170SKalle Valo 			ath6kl_wmi_set_control_ep(ar->wmi, response.endpoint);
164bdcd8170SKalle Valo 		ar->ctrl_ep = response.endpoint;
165bdcd8170SKalle Valo 		break;
166bdcd8170SKalle Valo 	case WMI_DATA_BE_SVC:
167bdcd8170SKalle Valo 		set_ac2_ep_map(ar, WMM_AC_BE, response.endpoint);
168bdcd8170SKalle Valo 		break;
169bdcd8170SKalle Valo 	case WMI_DATA_BK_SVC:
170bdcd8170SKalle Valo 		set_ac2_ep_map(ar, WMM_AC_BK, response.endpoint);
171bdcd8170SKalle Valo 		break;
172bdcd8170SKalle Valo 	case WMI_DATA_VI_SVC:
173bdcd8170SKalle Valo 		set_ac2_ep_map(ar, WMM_AC_VI, response.endpoint);
174bdcd8170SKalle Valo 		break;
175bdcd8170SKalle Valo 	case WMI_DATA_VO_SVC:
176bdcd8170SKalle Valo 		set_ac2_ep_map(ar, WMM_AC_VO, response.endpoint);
177bdcd8170SKalle Valo 		break;
178bdcd8170SKalle Valo 	default:
179bdcd8170SKalle Valo 		ath6kl_err("service id is not mapped %d\n", con_req->svc_id);
180bdcd8170SKalle Valo 		return -EINVAL;
181bdcd8170SKalle Valo 	}
182bdcd8170SKalle Valo 
183bdcd8170SKalle Valo 	return 0;
184bdcd8170SKalle Valo }
185bdcd8170SKalle Valo 
186bdcd8170SKalle Valo static int ath6kl_init_service_ep(struct ath6kl *ar)
187bdcd8170SKalle Valo {
188bdcd8170SKalle Valo 	struct htc_service_connect_req connect;
189bdcd8170SKalle Valo 
190bdcd8170SKalle Valo 	memset(&connect, 0, sizeof(connect));
191bdcd8170SKalle Valo 
192bdcd8170SKalle Valo 	/* these fields are the same for all service endpoints */
193bdcd8170SKalle Valo 	connect.ep_cb.rx = ath6kl_rx;
194bdcd8170SKalle Valo 	connect.ep_cb.rx_refill = ath6kl_rx_refill;
195bdcd8170SKalle Valo 	connect.ep_cb.tx_full = ath6kl_tx_queue_full;
196bdcd8170SKalle Valo 
197bdcd8170SKalle Valo 	/*
198bdcd8170SKalle Valo 	 * Set the max queue depth so that our ath6kl_tx_queue_full handler
199bdcd8170SKalle Valo 	 * gets called.
200bdcd8170SKalle Valo 	*/
201bdcd8170SKalle Valo 	connect.max_txq_depth = MAX_DEFAULT_SEND_QUEUE_DEPTH;
202bdcd8170SKalle Valo 	connect.ep_cb.rx_refill_thresh = ATH6KL_MAX_RX_BUFFERS / 4;
203bdcd8170SKalle Valo 	if (!connect.ep_cb.rx_refill_thresh)
204bdcd8170SKalle Valo 		connect.ep_cb.rx_refill_thresh++;
205bdcd8170SKalle Valo 
206bdcd8170SKalle Valo 	/* connect to control service */
207bdcd8170SKalle Valo 	connect.svc_id = WMI_CONTROL_SVC;
208bdcd8170SKalle Valo 	if (ath6kl_connectservice(ar, &connect, "WMI CONTROL"))
209bdcd8170SKalle Valo 		return -EIO;
210bdcd8170SKalle Valo 
211bdcd8170SKalle Valo 	connect.flags |= HTC_FLGS_TX_BNDL_PAD_EN;
212bdcd8170SKalle Valo 
213bdcd8170SKalle Valo 	/*
214bdcd8170SKalle Valo 	 * Limit the HTC message size on the send path, although e can
215bdcd8170SKalle Valo 	 * receive A-MSDU frames of 4K, we will only send ethernet-sized
216bdcd8170SKalle Valo 	 * (802.3) frames on the send path.
217bdcd8170SKalle Valo 	 */
218bdcd8170SKalle Valo 	connect.max_rxmsg_sz = WMI_MAX_TX_DATA_FRAME_LENGTH;
219bdcd8170SKalle Valo 
220bdcd8170SKalle Valo 	/*
221bdcd8170SKalle Valo 	 * To reduce the amount of committed memory for larger A_MSDU
222bdcd8170SKalle Valo 	 * frames, use the recv-alloc threshold mechanism for larger
223bdcd8170SKalle Valo 	 * packets.
224bdcd8170SKalle Valo 	 */
225bdcd8170SKalle Valo 	connect.ep_cb.rx_alloc_thresh = ATH6KL_BUFFER_SIZE;
226bdcd8170SKalle Valo 	connect.ep_cb.rx_allocthresh = ath6kl_alloc_amsdu_rxbuf;
227bdcd8170SKalle Valo 
228bdcd8170SKalle Valo 	/*
229bdcd8170SKalle Valo 	 * For the remaining data services set the connection flag to
230bdcd8170SKalle Valo 	 * reduce dribbling, if configured to do so.
231bdcd8170SKalle Valo 	 */
232bdcd8170SKalle Valo 	connect.conn_flags |= HTC_CONN_FLGS_REDUCE_CRED_DRIB;
233bdcd8170SKalle Valo 	connect.conn_flags &= ~HTC_CONN_FLGS_THRESH_MASK;
234bdcd8170SKalle Valo 	connect.conn_flags |= HTC_CONN_FLGS_THRESH_LVL_HALF;
235bdcd8170SKalle Valo 
236bdcd8170SKalle Valo 	connect.svc_id = WMI_DATA_BE_SVC;
237bdcd8170SKalle Valo 
238bdcd8170SKalle Valo 	if (ath6kl_connectservice(ar, &connect, "WMI DATA BE"))
239bdcd8170SKalle Valo 		return -EIO;
240bdcd8170SKalle Valo 
241bdcd8170SKalle Valo 	/* connect to back-ground map this to WMI LOW_PRI */
242bdcd8170SKalle Valo 	connect.svc_id = WMI_DATA_BK_SVC;
243bdcd8170SKalle Valo 	if (ath6kl_connectservice(ar, &connect, "WMI DATA BK"))
244bdcd8170SKalle Valo 		return -EIO;
245bdcd8170SKalle Valo 
246bdcd8170SKalle Valo 	/* connect to Video service, map this to to HI PRI */
247bdcd8170SKalle Valo 	connect.svc_id = WMI_DATA_VI_SVC;
248bdcd8170SKalle Valo 	if (ath6kl_connectservice(ar, &connect, "WMI DATA VI"))
249bdcd8170SKalle Valo 		return -EIO;
250bdcd8170SKalle Valo 
251bdcd8170SKalle Valo 	/*
252bdcd8170SKalle Valo 	 * Connect to VO service, this is currently not mapped to a WMI
253bdcd8170SKalle Valo 	 * priority stream due to historical reasons. WMI originally
254bdcd8170SKalle Valo 	 * defined 3 priorities over 3 mailboxes We can change this when
255bdcd8170SKalle Valo 	 * WMI is reworked so that priorities are not dependent on
256bdcd8170SKalle Valo 	 * mailboxes.
257bdcd8170SKalle Valo 	 */
258bdcd8170SKalle Valo 	connect.svc_id = WMI_DATA_VO_SVC;
259bdcd8170SKalle Valo 	if (ath6kl_connectservice(ar, &connect, "WMI DATA VO"))
260bdcd8170SKalle Valo 		return -EIO;
261bdcd8170SKalle Valo 
262bdcd8170SKalle Valo 	return 0;
263bdcd8170SKalle Valo }
264bdcd8170SKalle Valo 
265bdcd8170SKalle Valo static void ath6kl_init_control_info(struct ath6kl *ar)
266bdcd8170SKalle Valo {
267bdcd8170SKalle Valo 	u8 ctr;
268bdcd8170SKalle Valo 
269bdcd8170SKalle Valo 	clear_bit(WMI_ENABLED, &ar->flag);
270bdcd8170SKalle Valo 	ath6kl_init_profile_info(ar);
271bdcd8170SKalle Valo 	ar->def_txkey_index = 0;
272bdcd8170SKalle Valo 	memset(ar->wep_key_list, 0, sizeof(ar->wep_key_list));
273bdcd8170SKalle Valo 	ar->ch_hint = 0;
274bdcd8170SKalle Valo 	ar->listen_intvl_t = A_DEFAULT_LISTEN_INTERVAL;
275bdcd8170SKalle Valo 	ar->listen_intvl_b = 0;
276bdcd8170SKalle Valo 	ar->tx_pwr = 0;
277bdcd8170SKalle Valo 	clear_bit(SKIP_SCAN, &ar->flag);
278bdcd8170SKalle Valo 	set_bit(WMM_ENABLED, &ar->flag);
279bdcd8170SKalle Valo 	ar->intra_bss = 1;
280bdcd8170SKalle Valo 	memset(&ar->sc_params, 0, sizeof(ar->sc_params));
281bdcd8170SKalle Valo 	ar->sc_params.short_scan_ratio = WMI_SHORTSCANRATIO_DEFAULT;
282bdcd8170SKalle Valo 	ar->sc_params.scan_ctrl_flags = DEFAULT_SCAN_CTRL_FLAGS;
283e5090444SVivek Natarajan 	ar->lrssi_roam_threshold = DEF_LRSSI_ROAM_THRESHOLD;
284bdcd8170SKalle Valo 
285bdcd8170SKalle Valo 	memset((u8 *)ar->sta_list, 0,
286bdcd8170SKalle Valo 	       AP_MAX_NUM_STA * sizeof(struct ath6kl_sta));
287bdcd8170SKalle Valo 
288bdcd8170SKalle Valo 	spin_lock_init(&ar->mcastpsq_lock);
289bdcd8170SKalle Valo 
290bdcd8170SKalle Valo 	/* Init the PS queues */
291bdcd8170SKalle Valo 	for (ctr = 0; ctr < AP_MAX_NUM_STA; ctr++) {
292bdcd8170SKalle Valo 		spin_lock_init(&ar->sta_list[ctr].psq_lock);
293bdcd8170SKalle Valo 		skb_queue_head_init(&ar->sta_list[ctr].psq);
294bdcd8170SKalle Valo 	}
295bdcd8170SKalle Valo 
296bdcd8170SKalle Valo 	skb_queue_head_init(&ar->mcastpsq);
297bdcd8170SKalle Valo 
298bdcd8170SKalle Valo 	memcpy(ar->ap_country_code, DEF_AP_COUNTRY_CODE, 3);
299bdcd8170SKalle Valo }
300bdcd8170SKalle Valo 
301bdcd8170SKalle Valo /*
302bdcd8170SKalle Valo  * Set HTC/Mbox operational parameters, this can only be called when the
303bdcd8170SKalle Valo  * target is in the BMI phase.
304bdcd8170SKalle Valo  */
305bdcd8170SKalle Valo static int ath6kl_set_htc_params(struct ath6kl *ar, u32 mbox_isr_yield_val,
306bdcd8170SKalle Valo 				 u8 htc_ctrl_buf)
307bdcd8170SKalle Valo {
308bdcd8170SKalle Valo 	int status;
309bdcd8170SKalle Valo 	u32 blk_size;
310bdcd8170SKalle Valo 
311bdcd8170SKalle Valo 	blk_size = ar->mbox_info.block_size;
312bdcd8170SKalle Valo 
313bdcd8170SKalle Valo 	if (htc_ctrl_buf)
314bdcd8170SKalle Valo 		blk_size |=  ((u32)htc_ctrl_buf) << 16;
315bdcd8170SKalle Valo 
316bdcd8170SKalle Valo 	/* set the host interest area for the block size */
317bdcd8170SKalle Valo 	status = ath6kl_bmi_write(ar,
318bdcd8170SKalle Valo 			ath6kl_get_hi_item_addr(ar,
319bdcd8170SKalle Valo 			HI_ITEM(hi_mbox_io_block_sz)),
320bdcd8170SKalle Valo 			(u8 *)&blk_size,
321bdcd8170SKalle Valo 			4);
322bdcd8170SKalle Valo 	if (status) {
323bdcd8170SKalle Valo 		ath6kl_err("bmi_write_memory for IO block size failed\n");
324bdcd8170SKalle Valo 		goto out;
325bdcd8170SKalle Valo 	}
326bdcd8170SKalle Valo 
327bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_TRC, "block size set: %d (target addr:0x%X)\n",
328bdcd8170SKalle Valo 		   blk_size,
329bdcd8170SKalle Valo 		   ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_mbox_io_block_sz)));
330bdcd8170SKalle Valo 
331bdcd8170SKalle Valo 	if (mbox_isr_yield_val) {
332bdcd8170SKalle Valo 		/* set the host interest area for the mbox ISR yield limit */
333bdcd8170SKalle Valo 		status = ath6kl_bmi_write(ar,
334bdcd8170SKalle Valo 				ath6kl_get_hi_item_addr(ar,
335bdcd8170SKalle Valo 				HI_ITEM(hi_mbox_isr_yield_limit)),
336bdcd8170SKalle Valo 				(u8 *)&mbox_isr_yield_val,
337bdcd8170SKalle Valo 				4);
338bdcd8170SKalle Valo 		if (status) {
339bdcd8170SKalle Valo 			ath6kl_err("bmi_write_memory for yield limit failed\n");
340bdcd8170SKalle Valo 			goto out;
341bdcd8170SKalle Valo 		}
342bdcd8170SKalle Valo 	}
343bdcd8170SKalle Valo 
344bdcd8170SKalle Valo out:
345bdcd8170SKalle Valo 	return status;
346bdcd8170SKalle Valo }
347bdcd8170SKalle Valo 
348bdcd8170SKalle Valo #define REG_DUMP_COUNT_AR6003   60
349bdcd8170SKalle Valo #define REGISTER_DUMP_LEN_MAX   60
350bdcd8170SKalle Valo 
351bdcd8170SKalle Valo static void ath6kl_dump_target_assert_info(struct ath6kl *ar)
352bdcd8170SKalle Valo {
353bdcd8170SKalle Valo 	u32 address;
354bdcd8170SKalle Valo 	u32 regdump_loc = 0;
355bdcd8170SKalle Valo 	int status;
356bdcd8170SKalle Valo 	u32 regdump_val[REGISTER_DUMP_LEN_MAX];
357bdcd8170SKalle Valo 	u32 i;
358bdcd8170SKalle Valo 
359bdcd8170SKalle Valo 	if (ar->target_type != TARGET_TYPE_AR6003)
360bdcd8170SKalle Valo 		return;
361bdcd8170SKalle Valo 
362bdcd8170SKalle Valo 	/* the reg dump pointer is copied to the host interest area */
363bdcd8170SKalle Valo 	address = ath6kl_get_hi_item_addr(ar, HI_ITEM(hi_failure_state));
36431024d99SKevin Fang 	address = TARG_VTOP(ar->target_type, address);
365bdcd8170SKalle Valo 
366bdcd8170SKalle Valo 	/* read RAM location through diagnostic window */
367addb44beSKalle Valo 	status = ath6kl_diag_read32(ar, address, &regdump_loc);
368bdcd8170SKalle Valo 
369bdcd8170SKalle Valo 	if (status || !regdump_loc) {
370bdcd8170SKalle Valo 		ath6kl_err("failed to get ptr to register dump area\n");
371bdcd8170SKalle Valo 		return;
372bdcd8170SKalle Valo 	}
373bdcd8170SKalle Valo 
374bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_TRC, "location of register dump data: 0x%X\n",
375bdcd8170SKalle Valo 		regdump_loc);
37631024d99SKevin Fang 	regdump_loc = TARG_VTOP(ar->target_type, regdump_loc);
377bdcd8170SKalle Valo 
378bdcd8170SKalle Valo 	/* fetch register dump data */
379addb44beSKalle Valo 	status = ath6kl_diag_read(ar, regdump_loc, (u8 *)&regdump_val[0],
380addb44beSKalle Valo 				  REG_DUMP_COUNT_AR6003 * (sizeof(u32)));
381bdcd8170SKalle Valo 
382bdcd8170SKalle Valo 	if (status) {
383bdcd8170SKalle Valo 		ath6kl_err("failed to get register dump\n");
384bdcd8170SKalle Valo 		return;
385bdcd8170SKalle Valo 	}
386bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_TRC, "Register Dump:\n");
387bdcd8170SKalle Valo 
388bdcd8170SKalle Valo 	for (i = 0; i < REG_DUMP_COUNT_AR6003; i++)
389bdcd8170SKalle Valo 		ath6kl_dbg(ATH6KL_DBG_TRC, " %d :  0x%8.8X\n",
390bdcd8170SKalle Valo 			   i, regdump_val[i]);
391bdcd8170SKalle Valo 
392bdcd8170SKalle Valo }
393bdcd8170SKalle Valo 
394bdcd8170SKalle Valo void ath6kl_target_failure(struct ath6kl *ar)
395bdcd8170SKalle Valo {
396bdcd8170SKalle Valo 	ath6kl_err("target asserted\n");
397bdcd8170SKalle Valo 
398bdcd8170SKalle Valo 	/* try dumping target assertion information (if any) */
399bdcd8170SKalle Valo 	ath6kl_dump_target_assert_info(ar);
400bdcd8170SKalle Valo 
401bdcd8170SKalle Valo }
402bdcd8170SKalle Valo 
403bdcd8170SKalle Valo static int ath6kl_target_config_wlan_params(struct ath6kl *ar)
404bdcd8170SKalle Valo {
405bdcd8170SKalle Valo 	int status = 0;
4064dea08e0SJouni Malinen 	int ret;
407bdcd8170SKalle Valo 
408bdcd8170SKalle Valo 	/*
409bdcd8170SKalle Valo 	 * Configure the device for rx dot11 header rules. "0,0" are the
410bdcd8170SKalle Valo 	 * default values. Required if checksum offload is needed. Set
411bdcd8170SKalle Valo 	 * RxMetaVersion to 2.
412bdcd8170SKalle Valo 	 */
413bdcd8170SKalle Valo 	if (ath6kl_wmi_set_rx_frame_format_cmd(ar->wmi,
414bdcd8170SKalle Valo 					       ar->rx_meta_ver, 0, 0)) {
415bdcd8170SKalle Valo 		ath6kl_err("unable to set the rx frame format\n");
416bdcd8170SKalle Valo 		status = -EIO;
417bdcd8170SKalle Valo 	}
418bdcd8170SKalle Valo 
419bdcd8170SKalle Valo 	if (ar->conf_flags & ATH6KL_CONF_IGNORE_PS_FAIL_EVT_IN_SCAN)
420bdcd8170SKalle Valo 		if ((ath6kl_wmi_pmparams_cmd(ar->wmi, 0, 1, 0, 0, 1,
421bdcd8170SKalle Valo 		     IGNORE_POWER_SAVE_FAIL_EVENT_DURING_SCAN)) != 0) {
422bdcd8170SKalle Valo 			ath6kl_err("unable to set power save fail event policy\n");
423bdcd8170SKalle Valo 			status = -EIO;
424bdcd8170SKalle Valo 		}
425bdcd8170SKalle Valo 
426bdcd8170SKalle Valo 	if (!(ar->conf_flags & ATH6KL_CONF_IGNORE_ERP_BARKER))
427bdcd8170SKalle Valo 		if ((ath6kl_wmi_set_lpreamble_cmd(ar->wmi, 0,
428bdcd8170SKalle Valo 		     WMI_DONOT_IGNORE_BARKER_IN_ERP)) != 0) {
429bdcd8170SKalle Valo 			ath6kl_err("unable to set barker preamble policy\n");
430bdcd8170SKalle Valo 			status = -EIO;
431bdcd8170SKalle Valo 		}
432bdcd8170SKalle Valo 
433bdcd8170SKalle Valo 	if (ath6kl_wmi_set_keepalive_cmd(ar->wmi,
434bdcd8170SKalle Valo 			WLAN_CONFIG_KEEP_ALIVE_INTERVAL)) {
435bdcd8170SKalle Valo 		ath6kl_err("unable to set keep alive interval\n");
436bdcd8170SKalle Valo 		status = -EIO;
437bdcd8170SKalle Valo 	}
438bdcd8170SKalle Valo 
439bdcd8170SKalle Valo 	if (ath6kl_wmi_disctimeout_cmd(ar->wmi,
440bdcd8170SKalle Valo 			WLAN_CONFIG_DISCONNECT_TIMEOUT)) {
441bdcd8170SKalle Valo 		ath6kl_err("unable to set disconnect timeout\n");
442bdcd8170SKalle Valo 		status = -EIO;
443bdcd8170SKalle Valo 	}
444bdcd8170SKalle Valo 
445bdcd8170SKalle Valo 	if (!(ar->conf_flags & ATH6KL_CONF_ENABLE_TX_BURST))
446bdcd8170SKalle Valo 		if (ath6kl_wmi_set_wmm_txop(ar->wmi, WMI_TXOP_DISABLED)) {
447bdcd8170SKalle Valo 			ath6kl_err("unable to set txop bursting\n");
448bdcd8170SKalle Valo 			status = -EIO;
449bdcd8170SKalle Valo 		}
450bdcd8170SKalle Valo 
451*6bbc7c35SJouni Malinen 	if (ar->p2p) {
452*6bbc7c35SJouni Malinen 		ret = ath6kl_wmi_info_req_cmd(ar->wmi,
453*6bbc7c35SJouni Malinen 					      P2P_FLAG_CAPABILITIES_REQ |
4544dea08e0SJouni Malinen 					      P2P_FLAG_MACADDR_REQ |
4554dea08e0SJouni Malinen 					      P2P_FLAG_HMODEL_REQ);
4564dea08e0SJouni Malinen 		if (ret) {
4574dea08e0SJouni Malinen 			ath6kl_dbg(ATH6KL_DBG_TRC, "failed to request P2P "
458*6bbc7c35SJouni Malinen 				   "capabilities (%d) - assuming P2P not "
459*6bbc7c35SJouni Malinen 				   "supported\n", ret);
460*6bbc7c35SJouni Malinen 			ar->p2p = 0;
461*6bbc7c35SJouni Malinen 		}
462*6bbc7c35SJouni Malinen 	}
463*6bbc7c35SJouni Malinen 
464*6bbc7c35SJouni Malinen 	if (ar->p2p) {
465*6bbc7c35SJouni Malinen 		/* Enable Probe Request reporting for P2P */
466*6bbc7c35SJouni Malinen 		ret = ath6kl_wmi_probe_report_req_cmd(ar->wmi, true);
467*6bbc7c35SJouni Malinen 		if (ret) {
468*6bbc7c35SJouni Malinen 			ath6kl_dbg(ATH6KL_DBG_TRC, "failed to enable Probe "
469*6bbc7c35SJouni Malinen 				   "Request reporting (%d)\n", ret);
470*6bbc7c35SJouni Malinen 		}
4714dea08e0SJouni Malinen 	}
4724dea08e0SJouni Malinen 
473bdcd8170SKalle Valo 	return status;
474bdcd8170SKalle Valo }
475bdcd8170SKalle Valo 
476bdcd8170SKalle Valo int ath6kl_configure_target(struct ath6kl *ar)
477bdcd8170SKalle Valo {
478bdcd8170SKalle Valo 	u32 param, ram_reserved_size;
479bdcd8170SKalle Valo 	u8 fw_iftype;
480bdcd8170SKalle Valo 
481bdcd8170SKalle Valo 	fw_iftype = ath6kl_get_fw_iftype(ar);
482bdcd8170SKalle Valo 	if (fw_iftype == 0xff)
483bdcd8170SKalle Valo 		return -EINVAL;
484bdcd8170SKalle Valo 
485bdcd8170SKalle Valo 	/* Tell target which HTC version it is used*/
486bdcd8170SKalle Valo 	param = HTC_PROTOCOL_VERSION;
487bdcd8170SKalle Valo 	if (ath6kl_bmi_write(ar,
488bdcd8170SKalle Valo 			     ath6kl_get_hi_item_addr(ar,
489bdcd8170SKalle Valo 			     HI_ITEM(hi_app_host_interest)),
490bdcd8170SKalle Valo 			     (u8 *)&param, 4) != 0) {
491bdcd8170SKalle Valo 		ath6kl_err("bmi_write_memory for htc version failed\n");
492bdcd8170SKalle Valo 		return -EIO;
493bdcd8170SKalle Valo 	}
494bdcd8170SKalle Valo 
495bdcd8170SKalle Valo 	/* set the firmware mode to STA/IBSS/AP */
496bdcd8170SKalle Valo 	param = 0;
497bdcd8170SKalle Valo 
498bdcd8170SKalle Valo 	if (ath6kl_bmi_read(ar,
499bdcd8170SKalle Valo 			    ath6kl_get_hi_item_addr(ar,
500bdcd8170SKalle Valo 			    HI_ITEM(hi_option_flag)),
501bdcd8170SKalle Valo 			    (u8 *)&param, 4) != 0) {
502bdcd8170SKalle Valo 		ath6kl_err("bmi_read_memory for setting fwmode failed\n");
503bdcd8170SKalle Valo 		return -EIO;
504bdcd8170SKalle Valo 	}
505bdcd8170SKalle Valo 
506bdcd8170SKalle Valo 	param |= (1 << HI_OPTION_NUM_DEV_SHIFT);
507bdcd8170SKalle Valo 	param |= (fw_iftype << HI_OPTION_FW_MODE_SHIFT);
508*6bbc7c35SJouni Malinen 	if (ar->p2p && fw_iftype == HI_OPTION_FW_MODE_BSS_STA) {
509*6bbc7c35SJouni Malinen 		param |= HI_OPTION_FW_SUBMODE_P2PDEV <<
510*6bbc7c35SJouni Malinen 			HI_OPTION_FW_SUBMODE_SHIFT;
511*6bbc7c35SJouni Malinen 	}
512bdcd8170SKalle Valo 	param |= (0 << HI_OPTION_MAC_ADDR_METHOD_SHIFT);
513bdcd8170SKalle Valo 	param |= (0 << HI_OPTION_FW_BRIDGE_SHIFT);
514bdcd8170SKalle Valo 
515bdcd8170SKalle Valo 	if (ath6kl_bmi_write(ar,
516bdcd8170SKalle Valo 			     ath6kl_get_hi_item_addr(ar,
517bdcd8170SKalle Valo 			     HI_ITEM(hi_option_flag)),
518bdcd8170SKalle Valo 			     (u8 *)&param,
519bdcd8170SKalle Valo 			     4) != 0) {
520bdcd8170SKalle Valo 		ath6kl_err("bmi_write_memory for setting fwmode failed\n");
521bdcd8170SKalle Valo 		return -EIO;
522bdcd8170SKalle Valo 	}
523bdcd8170SKalle Valo 
524bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_TRC, "firmware mode set\n");
525bdcd8170SKalle Valo 
526bdcd8170SKalle Valo 	/*
527bdcd8170SKalle Valo 	 * Hardcode the address use for the extended board data
528bdcd8170SKalle Valo 	 * Ideally this should be pre-allocate by the OS at boot time
529bdcd8170SKalle Valo 	 * But since it is a new feature and board data is loaded
530bdcd8170SKalle Valo 	 * at init time, we have to workaround this from host.
531bdcd8170SKalle Valo 	 * It is difficult to patch the firmware boot code,
532bdcd8170SKalle Valo 	 * but possible in theory.
533bdcd8170SKalle Valo 	 */
534bdcd8170SKalle Valo 
53531024d99SKevin Fang 	if (ar->target_type == TARGET_TYPE_AR6003 ||
53631024d99SKevin Fang 	    ar->target_type == TARGET_TYPE_AR6004) {
537bdcd8170SKalle Valo 		if (ar->version.target_ver == AR6003_REV2_VERSION) {
538bdcd8170SKalle Valo 			param = AR6003_REV2_BOARD_EXT_DATA_ADDRESS;
539bdcd8170SKalle Valo 			ram_reserved_size =  AR6003_REV2_RAM_RESERVE_SIZE;
54031024d99SKevin Fang 		} else if (ar->version.target_ver == AR6004_REV1_VERSION) {
54131024d99SKevin Fang 			param = AR6004_REV1_BOARD_EXT_DATA_ADDRESS;
54231024d99SKevin Fang 			ram_reserved_size =  AR6004_REV1_RAM_RESERVE_SIZE;
543bdcd8170SKalle Valo 		} else {
544bdcd8170SKalle Valo 			param = AR6003_REV3_BOARD_EXT_DATA_ADDRESS;
545bdcd8170SKalle Valo 			ram_reserved_size =  AR6003_REV3_RAM_RESERVE_SIZE;
546bdcd8170SKalle Valo 		}
547bdcd8170SKalle Valo 
548bdcd8170SKalle Valo 		if (ath6kl_bmi_write(ar,
549bdcd8170SKalle Valo 				     ath6kl_get_hi_item_addr(ar,
550bdcd8170SKalle Valo 				     HI_ITEM(hi_board_ext_data)),
551bdcd8170SKalle Valo 				     (u8 *)&param, 4) != 0) {
552bdcd8170SKalle Valo 			ath6kl_err("bmi_write_memory for hi_board_ext_data failed\n");
553bdcd8170SKalle Valo 			return -EIO;
554bdcd8170SKalle Valo 		}
555bdcd8170SKalle Valo 		if (ath6kl_bmi_write(ar,
556bdcd8170SKalle Valo 				     ath6kl_get_hi_item_addr(ar,
557bdcd8170SKalle Valo 				     HI_ITEM(hi_end_ram_reserve_sz)),
558bdcd8170SKalle Valo 				     (u8 *)&ram_reserved_size, 4) != 0) {
559bdcd8170SKalle Valo 			ath6kl_err("bmi_write_memory for hi_end_ram_reserve_sz failed\n");
560bdcd8170SKalle Valo 			return -EIO;
561bdcd8170SKalle Valo 		}
562bdcd8170SKalle Valo 	}
563bdcd8170SKalle Valo 
564bdcd8170SKalle Valo 	/* set the block size for the target */
565bdcd8170SKalle Valo 	if (ath6kl_set_htc_params(ar, MBOX_YIELD_LIMIT, 0))
566bdcd8170SKalle Valo 		/* use default number of control buffers */
567bdcd8170SKalle Valo 		return -EIO;
568bdcd8170SKalle Valo 
569bdcd8170SKalle Valo 	return 0;
570bdcd8170SKalle Valo }
571bdcd8170SKalle Valo 
572bdcd8170SKalle Valo struct ath6kl *ath6kl_core_alloc(struct device *sdev)
573bdcd8170SKalle Valo {
574bdcd8170SKalle Valo 	struct net_device *dev;
575bdcd8170SKalle Valo 	struct ath6kl *ar;
576bdcd8170SKalle Valo 	struct wireless_dev *wdev;
577bdcd8170SKalle Valo 
578bdcd8170SKalle Valo 	wdev = ath6kl_cfg80211_init(sdev);
579bdcd8170SKalle Valo 	if (!wdev) {
580bdcd8170SKalle Valo 		ath6kl_err("ath6kl_cfg80211_init failed\n");
581bdcd8170SKalle Valo 		return NULL;
582bdcd8170SKalle Valo 	}
583bdcd8170SKalle Valo 
584bdcd8170SKalle Valo 	ar = wdev_priv(wdev);
585bdcd8170SKalle Valo 	ar->dev = sdev;
586bdcd8170SKalle Valo 	ar->wdev = wdev;
587bdcd8170SKalle Valo 	wdev->iftype = NL80211_IFTYPE_STATION;
588bdcd8170SKalle Valo 
589d999ba3eSVasanthakumar Thiagarajan 	if (ath6kl_debug_init(ar)) {
590d999ba3eSVasanthakumar Thiagarajan 		ath6kl_err("Failed to initialize debugfs\n");
591d999ba3eSVasanthakumar Thiagarajan 		ath6kl_cfg80211_deinit(ar);
592d999ba3eSVasanthakumar Thiagarajan 		return NULL;
593d999ba3eSVasanthakumar Thiagarajan 	}
594d999ba3eSVasanthakumar Thiagarajan 
595bdcd8170SKalle Valo 	dev = alloc_netdev(0, "wlan%d", ether_setup);
596bdcd8170SKalle Valo 	if (!dev) {
597bdcd8170SKalle Valo 		ath6kl_err("no memory for network device instance\n");
598bdcd8170SKalle Valo 		ath6kl_cfg80211_deinit(ar);
599bdcd8170SKalle Valo 		return NULL;
600bdcd8170SKalle Valo 	}
601bdcd8170SKalle Valo 
602bdcd8170SKalle Valo 	dev->ieee80211_ptr = wdev;
603bdcd8170SKalle Valo 	SET_NETDEV_DEV(dev, wiphy_dev(wdev->wiphy));
604bdcd8170SKalle Valo 	wdev->netdev = dev;
605bdcd8170SKalle Valo 	ar->sme_state = SME_DISCONNECTED;
606bdcd8170SKalle Valo 	ar->auto_auth_stage = AUTH_IDLE;
607bdcd8170SKalle Valo 
608bdcd8170SKalle Valo 	init_netdev(dev);
609bdcd8170SKalle Valo 
610bdcd8170SKalle Valo 	ar->net_dev = dev;
611575b5f34SRaja Mani 	set_bit(WLAN_ENABLED, &ar->flag);
612bdcd8170SKalle Valo 
613bdcd8170SKalle Valo 	ar->wlan_pwr_state = WLAN_POWER_STATE_ON;
614bdcd8170SKalle Valo 
615bdcd8170SKalle Valo 	spin_lock_init(&ar->lock);
616bdcd8170SKalle Valo 
617bdcd8170SKalle Valo 	ath6kl_init_control_info(ar);
618bdcd8170SKalle Valo 	init_waitqueue_head(&ar->event_wq);
619bdcd8170SKalle Valo 	sema_init(&ar->sem, 1);
620bdcd8170SKalle Valo 	clear_bit(DESTROY_IN_PROGRESS, &ar->flag);
621bdcd8170SKalle Valo 
622bdcd8170SKalle Valo 	INIT_LIST_HEAD(&ar->amsdu_rx_buffer_queue);
623bdcd8170SKalle Valo 
624bdcd8170SKalle Valo 	setup_timer(&ar->disconnect_timer, disconnect_timer_handler,
625bdcd8170SKalle Valo 		    (unsigned long) dev);
626bdcd8170SKalle Valo 
627bdcd8170SKalle Valo 	return ar;
628bdcd8170SKalle Valo }
629bdcd8170SKalle Valo 
630bdcd8170SKalle Valo int ath6kl_unavail_ev(struct ath6kl *ar)
631bdcd8170SKalle Valo {
632bdcd8170SKalle Valo 	ath6kl_destroy(ar->net_dev, 1);
633bdcd8170SKalle Valo 
634bdcd8170SKalle Valo 	return 0;
635bdcd8170SKalle Valo }
636bdcd8170SKalle Valo 
637bdcd8170SKalle Valo /* firmware upload */
638bdcd8170SKalle Valo static u32 ath6kl_get_load_address(u32 target_ver, enum addr_type type)
639bdcd8170SKalle Valo {
640bdcd8170SKalle Valo 	WARN_ON(target_ver != AR6003_REV2_VERSION &&
64131024d99SKevin Fang 		target_ver != AR6003_REV3_VERSION &&
64231024d99SKevin Fang 		target_ver != AR6004_REV1_VERSION);
643bdcd8170SKalle Valo 
644bdcd8170SKalle Valo 	switch (type) {
645bdcd8170SKalle Valo 	case DATASET_PATCH_ADDR:
646bdcd8170SKalle Valo 		return (target_ver == AR6003_REV2_VERSION) ?
647bdcd8170SKalle Valo 			AR6003_REV2_DATASET_PATCH_ADDRESS :
648bdcd8170SKalle Valo 			AR6003_REV3_DATASET_PATCH_ADDRESS;
649bdcd8170SKalle Valo 	case APP_LOAD_ADDR:
650bdcd8170SKalle Valo 		return (target_ver == AR6003_REV2_VERSION) ?
651bdcd8170SKalle Valo 			AR6003_REV2_APP_LOAD_ADDRESS :
652bdcd8170SKalle Valo 			0x1234;
653bdcd8170SKalle Valo 	case APP_START_OVERRIDE_ADDR:
654bdcd8170SKalle Valo 		return (target_ver == AR6003_REV2_VERSION) ?
655bdcd8170SKalle Valo 			AR6003_REV2_APP_START_OVERRIDE :
656bdcd8170SKalle Valo 			AR6003_REV3_APP_START_OVERRIDE;
657bdcd8170SKalle Valo 	default:
658bdcd8170SKalle Valo 		return 0;
659bdcd8170SKalle Valo 	}
660bdcd8170SKalle Valo }
661bdcd8170SKalle Valo 
662bdcd8170SKalle Valo static int ath6kl_get_fw(struct ath6kl *ar, const char *filename,
663bdcd8170SKalle Valo 			 u8 **fw, size_t *fw_len)
664bdcd8170SKalle Valo {
665bdcd8170SKalle Valo 	const struct firmware *fw_entry;
666bdcd8170SKalle Valo 	int ret;
667bdcd8170SKalle Valo 
668bdcd8170SKalle Valo 	ret = request_firmware(&fw_entry, filename, ar->dev);
669bdcd8170SKalle Valo 	if (ret)
670bdcd8170SKalle Valo 		return ret;
671bdcd8170SKalle Valo 
672bdcd8170SKalle Valo 	*fw_len = fw_entry->size;
673bdcd8170SKalle Valo 	*fw = kmemdup(fw_entry->data, fw_entry->size, GFP_KERNEL);
674bdcd8170SKalle Valo 
675bdcd8170SKalle Valo 	if (*fw == NULL)
676bdcd8170SKalle Valo 		ret = -ENOMEM;
677bdcd8170SKalle Valo 
678bdcd8170SKalle Valo 	release_firmware(fw_entry);
679bdcd8170SKalle Valo 
680bdcd8170SKalle Valo 	return ret;
681bdcd8170SKalle Valo }
682bdcd8170SKalle Valo 
683bdcd8170SKalle Valo static int ath6kl_fetch_board_file(struct ath6kl *ar)
684bdcd8170SKalle Valo {
685bdcd8170SKalle Valo 	const char *filename;
686bdcd8170SKalle Valo 	int ret;
687bdcd8170SKalle Valo 
688bdcd8170SKalle Valo 	switch (ar->version.target_ver) {
689bdcd8170SKalle Valo 	case AR6003_REV2_VERSION:
690bdcd8170SKalle Valo 		filename = AR6003_REV2_BOARD_DATA_FILE;
691bdcd8170SKalle Valo 		break;
69231024d99SKevin Fang 	case AR6004_REV1_VERSION:
69331024d99SKevin Fang 		filename = AR6004_REV1_BOARD_DATA_FILE;
69431024d99SKevin Fang 		break;
695bdcd8170SKalle Valo 	default:
696bdcd8170SKalle Valo 		filename = AR6003_REV3_BOARD_DATA_FILE;
697bdcd8170SKalle Valo 		break;
698bdcd8170SKalle Valo 	}
699bdcd8170SKalle Valo 
700bdcd8170SKalle Valo 	ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
701bdcd8170SKalle Valo 			    &ar->fw_board_len);
702bdcd8170SKalle Valo 	if (ret == 0) {
703bdcd8170SKalle Valo 		/* managed to get proper board file */
704bdcd8170SKalle Valo 		return 0;
705bdcd8170SKalle Valo 	}
706bdcd8170SKalle Valo 
707bdcd8170SKalle Valo 	/* there was no proper board file, try to use default instead */
708bdcd8170SKalle Valo 	ath6kl_warn("Failed to get board file %s (%d), trying to find default board file.\n",
709bdcd8170SKalle Valo 		    filename, ret);
710bdcd8170SKalle Valo 
711bdcd8170SKalle Valo 	switch (ar->version.target_ver) {
712bdcd8170SKalle Valo 	case AR6003_REV2_VERSION:
713bdcd8170SKalle Valo 		filename = AR6003_REV2_DEFAULT_BOARD_DATA_FILE;
714bdcd8170SKalle Valo 		break;
71531024d99SKevin Fang 	case AR6004_REV1_VERSION:
71631024d99SKevin Fang 		filename = AR6004_REV1_DEFAULT_BOARD_DATA_FILE;
71731024d99SKevin Fang 		break;
718bdcd8170SKalle Valo 	default:
719bdcd8170SKalle Valo 		filename = AR6003_REV3_DEFAULT_BOARD_DATA_FILE;
720bdcd8170SKalle Valo 		break;
721bdcd8170SKalle Valo 	}
722bdcd8170SKalle Valo 
723bdcd8170SKalle Valo 	ret = ath6kl_get_fw(ar, filename, &ar->fw_board,
724bdcd8170SKalle Valo 			    &ar->fw_board_len);
725bdcd8170SKalle Valo 	if (ret) {
726bdcd8170SKalle Valo 		ath6kl_err("Failed to get default board file %s: %d\n",
727bdcd8170SKalle Valo 			   filename, ret);
728bdcd8170SKalle Valo 		return ret;
729bdcd8170SKalle Valo 	}
730bdcd8170SKalle Valo 
731bdcd8170SKalle Valo 	ath6kl_warn("WARNING! No proper board file was not found, instead using a default board file.\n");
732bdcd8170SKalle Valo 	ath6kl_warn("Most likely your hardware won't work as specified. Install correct board file!\n");
733bdcd8170SKalle Valo 
734bdcd8170SKalle Valo 	return 0;
735bdcd8170SKalle Valo }
736bdcd8170SKalle Valo 
737bdcd8170SKalle Valo 
738bdcd8170SKalle Valo static int ath6kl_upload_board_file(struct ath6kl *ar)
739bdcd8170SKalle Valo {
740bdcd8170SKalle Valo 	u32 board_address, board_ext_address, param;
74131024d99SKevin Fang 	u32 board_data_size, board_ext_data_size;
742bdcd8170SKalle Valo 	int ret;
743bdcd8170SKalle Valo 
744bdcd8170SKalle Valo 	if (ar->fw_board == NULL) {
745bdcd8170SKalle Valo 		ret = ath6kl_fetch_board_file(ar);
746bdcd8170SKalle Valo 		if (ret)
747bdcd8170SKalle Valo 			return ret;
748bdcd8170SKalle Valo 	}
749bdcd8170SKalle Valo 
75031024d99SKevin Fang 	/*
75131024d99SKevin Fang 	 * Determine where in Target RAM to write Board Data.
75231024d99SKevin Fang 	 * For AR6004, host determine Target RAM address for
75331024d99SKevin Fang 	 * writing board data.
75431024d99SKevin Fang 	 */
75531024d99SKevin Fang 	if (ar->target_type == TARGET_TYPE_AR6004) {
75631024d99SKevin Fang 		board_address = AR6004_REV1_BOARD_DATA_ADDRESS;
75731024d99SKevin Fang 		ath6kl_bmi_write(ar,
75831024d99SKevin Fang 				ath6kl_get_hi_item_addr(ar,
75931024d99SKevin Fang 				HI_ITEM(hi_board_data)),
76031024d99SKevin Fang 				(u8 *) &board_address, 4);
76131024d99SKevin Fang 	} else {
762bdcd8170SKalle Valo 		ath6kl_bmi_read(ar,
763bdcd8170SKalle Valo 				ath6kl_get_hi_item_addr(ar,
764bdcd8170SKalle Valo 				HI_ITEM(hi_board_data)),
765bdcd8170SKalle Valo 				(u8 *) &board_address, 4);
76631024d99SKevin Fang 	}
76731024d99SKevin Fang 
768bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_TRC, "board data download addr: 0x%x\n",
769bdcd8170SKalle Valo 		   board_address);
770bdcd8170SKalle Valo 
771bdcd8170SKalle Valo 	/* determine where in target ram to write extended board data */
772bdcd8170SKalle Valo 	ath6kl_bmi_read(ar,
773bdcd8170SKalle Valo 			ath6kl_get_hi_item_addr(ar,
774bdcd8170SKalle Valo 			HI_ITEM(hi_board_ext_data)),
775bdcd8170SKalle Valo 			(u8 *) &board_ext_address, 4);
776bdcd8170SKalle Valo 
777bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_TRC, "board file download addr: 0x%x\n",
778bdcd8170SKalle Valo 		   board_ext_address);
779bdcd8170SKalle Valo 
780bdcd8170SKalle Valo 	if (board_ext_address == 0) {
781bdcd8170SKalle Valo 		ath6kl_err("Failed to get board file target address.\n");
782bdcd8170SKalle Valo 		return -EINVAL;
783bdcd8170SKalle Valo 	}
784bdcd8170SKalle Valo 
78531024d99SKevin Fang 	switch (ar->target_type) {
78631024d99SKevin Fang 	case TARGET_TYPE_AR6003:
78731024d99SKevin Fang 		board_data_size = AR6003_BOARD_DATA_SZ;
78831024d99SKevin Fang 		board_ext_data_size = AR6003_BOARD_EXT_DATA_SZ;
78931024d99SKevin Fang 		break;
79031024d99SKevin Fang 	case TARGET_TYPE_AR6004:
79131024d99SKevin Fang 		board_data_size = AR6004_BOARD_DATA_SZ;
79231024d99SKevin Fang 		board_ext_data_size = AR6004_BOARD_EXT_DATA_SZ;
79331024d99SKevin Fang 		break;
79431024d99SKevin Fang 	default:
79531024d99SKevin Fang 		WARN_ON(1);
79631024d99SKevin Fang 		return -EINVAL;
79731024d99SKevin Fang 		break;
79831024d99SKevin Fang 	}
79931024d99SKevin Fang 
80031024d99SKevin Fang 	if (ar->fw_board_len == (board_data_size +
80131024d99SKevin Fang 				 board_ext_data_size)) {
80231024d99SKevin Fang 
803bdcd8170SKalle Valo 		/* write extended board data */
804bdcd8170SKalle Valo 		ret = ath6kl_bmi_write(ar, board_ext_address,
80531024d99SKevin Fang 				       ar->fw_board + board_data_size,
80631024d99SKevin Fang 				       board_ext_data_size);
807bdcd8170SKalle Valo 		if (ret) {
808bdcd8170SKalle Valo 			ath6kl_err("Failed to write extended board data: %d\n",
809bdcd8170SKalle Valo 				   ret);
810bdcd8170SKalle Valo 			return ret;
811bdcd8170SKalle Valo 		}
812bdcd8170SKalle Valo 
813bdcd8170SKalle Valo 		/* record that extended board data is initialized */
81431024d99SKevin Fang 		param = (board_ext_data_size << 16) | 1;
81531024d99SKevin Fang 
816bdcd8170SKalle Valo 		ath6kl_bmi_write(ar,
817bdcd8170SKalle Valo 				 ath6kl_get_hi_item_addr(ar,
818bdcd8170SKalle Valo 				 HI_ITEM(hi_board_ext_data_config)),
819bdcd8170SKalle Valo 				 (unsigned char *) &param, 4);
820bdcd8170SKalle Valo 	}
821bdcd8170SKalle Valo 
82231024d99SKevin Fang 	if (ar->fw_board_len < board_data_size) {
823bdcd8170SKalle Valo 		ath6kl_err("Too small board file: %zu\n", ar->fw_board_len);
824bdcd8170SKalle Valo 		ret = -EINVAL;
825bdcd8170SKalle Valo 		return ret;
826bdcd8170SKalle Valo 	}
827bdcd8170SKalle Valo 
828bdcd8170SKalle Valo 	ret = ath6kl_bmi_write(ar, board_address, ar->fw_board,
82931024d99SKevin Fang 			       board_data_size);
830bdcd8170SKalle Valo 
831bdcd8170SKalle Valo 	if (ret) {
832bdcd8170SKalle Valo 		ath6kl_err("Board file bmi write failed: %d\n", ret);
833bdcd8170SKalle Valo 		return ret;
834bdcd8170SKalle Valo 	}
835bdcd8170SKalle Valo 
836bdcd8170SKalle Valo 	/* record the fact that Board Data IS initialized */
837bdcd8170SKalle Valo 	param = 1;
838bdcd8170SKalle Valo 	ath6kl_bmi_write(ar,
839bdcd8170SKalle Valo 			 ath6kl_get_hi_item_addr(ar,
840bdcd8170SKalle Valo 			 HI_ITEM(hi_board_data_initialized)),
841bdcd8170SKalle Valo 			 (u8 *)&param, 4);
842bdcd8170SKalle Valo 
843bdcd8170SKalle Valo 	return ret;
844bdcd8170SKalle Valo }
845bdcd8170SKalle Valo 
846bdcd8170SKalle Valo static int ath6kl_upload_otp(struct ath6kl *ar)
847bdcd8170SKalle Valo {
848bdcd8170SKalle Valo 	const char *filename;
849bdcd8170SKalle Valo 	u32 address, param;
850bdcd8170SKalle Valo 	int ret;
851bdcd8170SKalle Valo 
852bdcd8170SKalle Valo 	switch (ar->version.target_ver) {
853bdcd8170SKalle Valo 	case AR6003_REV2_VERSION:
854bdcd8170SKalle Valo 		filename = AR6003_REV2_OTP_FILE;
855bdcd8170SKalle Valo 		break;
85631024d99SKevin Fang 	case AR6004_REV1_VERSION:
85731024d99SKevin Fang 		ath6kl_dbg(ATH6KL_DBG_TRC, "AR6004 doesn't need OTP file\n");
85831024d99SKevin Fang 		return 0;
85931024d99SKevin Fang 		break;
860bdcd8170SKalle Valo 	default:
861bdcd8170SKalle Valo 		filename = AR6003_REV3_OTP_FILE;
862bdcd8170SKalle Valo 		break;
863bdcd8170SKalle Valo 	}
864bdcd8170SKalle Valo 
865bdcd8170SKalle Valo 	if (ar->fw_otp == NULL) {
866bdcd8170SKalle Valo 		ret = ath6kl_get_fw(ar, filename, &ar->fw_otp,
867bdcd8170SKalle Valo 				    &ar->fw_otp_len);
868bdcd8170SKalle Valo 		if (ret) {
869bdcd8170SKalle Valo 			ath6kl_err("Failed to get OTP file %s: %d\n",
870bdcd8170SKalle Valo 				   filename, ret);
871bdcd8170SKalle Valo 			return ret;
872bdcd8170SKalle Valo 		}
873bdcd8170SKalle Valo 	}
874bdcd8170SKalle Valo 
875bdcd8170SKalle Valo 	address = ath6kl_get_load_address(ar->version.target_ver,
876bdcd8170SKalle Valo 					  APP_LOAD_ADDR);
877bdcd8170SKalle Valo 
878bdcd8170SKalle Valo 	ret = ath6kl_bmi_fast_download(ar, address, ar->fw_otp,
879bdcd8170SKalle Valo 				       ar->fw_otp_len);
880bdcd8170SKalle Valo 	if (ret) {
881bdcd8170SKalle Valo 		ath6kl_err("Failed to upload OTP file: %d\n", ret);
882bdcd8170SKalle Valo 		return ret;
883bdcd8170SKalle Valo 	}
884bdcd8170SKalle Valo 
885bdcd8170SKalle Valo 	/* execute the OTP code */
886bdcd8170SKalle Valo 	param = 0;
887bdcd8170SKalle Valo 	address = ath6kl_get_load_address(ar->version.target_ver,
888bdcd8170SKalle Valo 					  APP_START_OVERRIDE_ADDR);
889bdcd8170SKalle Valo 	ath6kl_bmi_execute(ar, address, &param);
890bdcd8170SKalle Valo 
891bdcd8170SKalle Valo 	return ret;
892bdcd8170SKalle Valo }
893bdcd8170SKalle Valo 
894bdcd8170SKalle Valo static int ath6kl_upload_firmware(struct ath6kl *ar)
895bdcd8170SKalle Valo {
896bdcd8170SKalle Valo 	const char *filename;
897bdcd8170SKalle Valo 	u32 address;
898bdcd8170SKalle Valo 	int ret;
899bdcd8170SKalle Valo 
900003353b0SKalle Valo 	if (testmode) {
901003353b0SKalle Valo 		switch (ar->version.target_ver) {
902003353b0SKalle Valo 		case AR6003_REV2_VERSION:
903003353b0SKalle Valo 			filename = AR6003_REV2_TCMD_FIRMWARE_FILE;
904003353b0SKalle Valo 			break;
905003353b0SKalle Valo 		case AR6003_REV3_VERSION:
906003353b0SKalle Valo 			filename = AR6003_REV3_TCMD_FIRMWARE_FILE;
907003353b0SKalle Valo 			break;
908003353b0SKalle Valo 		case AR6004_REV1_VERSION:
909003353b0SKalle Valo 			ath6kl_warn("testmode not supported with ar6004\n");
910003353b0SKalle Valo 			return -EOPNOTSUPP;
911003353b0SKalle Valo 		default:
912003353b0SKalle Valo 			ath6kl_warn("unknown target version: 0x%x\n",
913003353b0SKalle Valo 				       ar->version.target_ver);
914003353b0SKalle Valo 			return -EINVAL;
915003353b0SKalle Valo 		}
916003353b0SKalle Valo 
917003353b0SKalle Valo 		set_bit(TESTMODE, &ar->flag);
918003353b0SKalle Valo 
919003353b0SKalle Valo 		goto get_fw;
920003353b0SKalle Valo 	}
921003353b0SKalle Valo 
922bdcd8170SKalle Valo 	switch (ar->version.target_ver) {
923bdcd8170SKalle Valo 	case AR6003_REV2_VERSION:
924bdcd8170SKalle Valo 		filename = AR6003_REV2_FIRMWARE_FILE;
925bdcd8170SKalle Valo 		break;
92631024d99SKevin Fang 	case AR6004_REV1_VERSION:
92731024d99SKevin Fang 		filename = AR6004_REV1_FIRMWARE_FILE;
92831024d99SKevin Fang 		break;
929bdcd8170SKalle Valo 	default:
930bdcd8170SKalle Valo 		filename = AR6003_REV3_FIRMWARE_FILE;
931bdcd8170SKalle Valo 		break;
932bdcd8170SKalle Valo 	}
933bdcd8170SKalle Valo 
934003353b0SKalle Valo get_fw:
935003353b0SKalle Valo 
936bdcd8170SKalle Valo 	if (ar->fw == NULL) {
937bdcd8170SKalle Valo 		ret = ath6kl_get_fw(ar, filename, &ar->fw, &ar->fw_len);
938bdcd8170SKalle Valo 		if (ret) {
939bdcd8170SKalle Valo 			ath6kl_err("Failed to get firmware file %s: %d\n",
940bdcd8170SKalle Valo 				   filename, ret);
941bdcd8170SKalle Valo 			return ret;
942bdcd8170SKalle Valo 		}
943bdcd8170SKalle Valo 	}
944bdcd8170SKalle Valo 
945bdcd8170SKalle Valo 	address = ath6kl_get_load_address(ar->version.target_ver,
946bdcd8170SKalle Valo 					  APP_LOAD_ADDR);
947bdcd8170SKalle Valo 
948bdcd8170SKalle Valo 	ret = ath6kl_bmi_fast_download(ar, address, ar->fw, ar->fw_len);
949bdcd8170SKalle Valo 
950bdcd8170SKalle Valo 	if (ret) {
951bdcd8170SKalle Valo 		ath6kl_err("Failed to write firmware: %d\n", ret);
952bdcd8170SKalle Valo 		return ret;
953bdcd8170SKalle Valo 	}
954bdcd8170SKalle Valo 
95531024d99SKevin Fang 	/*
95631024d99SKevin Fang 	 * Set starting address for firmware
95731024d99SKevin Fang 	 * Don't need to setup app_start override addr on AR6004
95831024d99SKevin Fang 	 */
95931024d99SKevin Fang 	if (ar->target_type != TARGET_TYPE_AR6004) {
960bdcd8170SKalle Valo 		address = ath6kl_get_load_address(ar->version.target_ver,
961bdcd8170SKalle Valo 						  APP_START_OVERRIDE_ADDR);
962bdcd8170SKalle Valo 		ath6kl_bmi_set_app_start(ar, address);
96331024d99SKevin Fang 	}
964bdcd8170SKalle Valo 	return ret;
965bdcd8170SKalle Valo }
966bdcd8170SKalle Valo 
967bdcd8170SKalle Valo static int ath6kl_upload_patch(struct ath6kl *ar)
968bdcd8170SKalle Valo {
969bdcd8170SKalle Valo 	const char *filename;
970bdcd8170SKalle Valo 	u32 address, param;
971bdcd8170SKalle Valo 	int ret;
972bdcd8170SKalle Valo 
973bdcd8170SKalle Valo 	switch (ar->version.target_ver) {
974bdcd8170SKalle Valo 	case AR6003_REV2_VERSION:
975bdcd8170SKalle Valo 		filename = AR6003_REV2_PATCH_FILE;
976bdcd8170SKalle Valo 		break;
97731024d99SKevin Fang 	case AR6004_REV1_VERSION:
97831024d99SKevin Fang 		/* FIXME: implement for AR6004 */
97931024d99SKevin Fang 		return 0;
98031024d99SKevin Fang 		break;
981bdcd8170SKalle Valo 	default:
982bdcd8170SKalle Valo 		filename = AR6003_REV3_PATCH_FILE;
983bdcd8170SKalle Valo 		break;
984bdcd8170SKalle Valo 	}
985bdcd8170SKalle Valo 
986bdcd8170SKalle Valo 	if (ar->fw_patch == NULL) {
987bdcd8170SKalle Valo 		ret = ath6kl_get_fw(ar, filename, &ar->fw_patch,
988bdcd8170SKalle Valo 				    &ar->fw_patch_len);
989bdcd8170SKalle Valo 		if (ret) {
990bdcd8170SKalle Valo 			ath6kl_err("Failed to get patch file %s: %d\n",
991bdcd8170SKalle Valo 				   filename, ret);
992bdcd8170SKalle Valo 			return ret;
993bdcd8170SKalle Valo 		}
994bdcd8170SKalle Valo 	}
995bdcd8170SKalle Valo 
996bdcd8170SKalle Valo 	address = ath6kl_get_load_address(ar->version.target_ver,
997bdcd8170SKalle Valo 					  DATASET_PATCH_ADDR);
998bdcd8170SKalle Valo 
999bdcd8170SKalle Valo 	ret = ath6kl_bmi_write(ar, address, ar->fw_patch, ar->fw_patch_len);
1000bdcd8170SKalle Valo 	if (ret) {
1001bdcd8170SKalle Valo 		ath6kl_err("Failed to write patch file: %d\n", ret);
1002bdcd8170SKalle Valo 		return ret;
1003bdcd8170SKalle Valo 	}
1004bdcd8170SKalle Valo 
1005bdcd8170SKalle Valo 	param = address;
1006bdcd8170SKalle Valo 	ath6kl_bmi_write(ar,
1007bdcd8170SKalle Valo 			 ath6kl_get_hi_item_addr(ar,
1008bdcd8170SKalle Valo 			 HI_ITEM(hi_dset_list_head)),
1009bdcd8170SKalle Valo 			 (unsigned char *) &param, 4);
1010bdcd8170SKalle Valo 
1011bdcd8170SKalle Valo 	return 0;
1012bdcd8170SKalle Valo }
1013bdcd8170SKalle Valo 
1014bdcd8170SKalle Valo static int ath6kl_init_upload(struct ath6kl *ar)
1015bdcd8170SKalle Valo {
1016bdcd8170SKalle Valo 	u32 param, options, sleep, address;
1017bdcd8170SKalle Valo 	int status = 0;
1018bdcd8170SKalle Valo 
101931024d99SKevin Fang 	if (ar->target_type != TARGET_TYPE_AR6003 &&
102031024d99SKevin Fang 		ar->target_type != TARGET_TYPE_AR6004)
1021bdcd8170SKalle Valo 		return -EINVAL;
1022bdcd8170SKalle Valo 
1023bdcd8170SKalle Valo 	/* temporarily disable system sleep */
1024bdcd8170SKalle Valo 	address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
1025bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_read(ar, address, &param);
1026bdcd8170SKalle Valo 	if (status)
1027bdcd8170SKalle Valo 		return status;
1028bdcd8170SKalle Valo 
1029bdcd8170SKalle Valo 	options = param;
1030bdcd8170SKalle Valo 
1031bdcd8170SKalle Valo 	param |= ATH6KL_OPTION_SLEEP_DISABLE;
1032bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_write(ar, address, param);
1033bdcd8170SKalle Valo 	if (status)
1034bdcd8170SKalle Valo 		return status;
1035bdcd8170SKalle Valo 
1036bdcd8170SKalle Valo 	address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1037bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_read(ar, address, &param);
1038bdcd8170SKalle Valo 	if (status)
1039bdcd8170SKalle Valo 		return status;
1040bdcd8170SKalle Valo 
1041bdcd8170SKalle Valo 	sleep = param;
1042bdcd8170SKalle Valo 
1043bdcd8170SKalle Valo 	param |= SM(SYSTEM_SLEEP_DISABLE, 1);
1044bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_write(ar, address, param);
1045bdcd8170SKalle Valo 	if (status)
1046bdcd8170SKalle Valo 		return status;
1047bdcd8170SKalle Valo 
1048bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_TRC, "old options: %d, old sleep: %d\n",
1049bdcd8170SKalle Valo 		   options, sleep);
1050bdcd8170SKalle Valo 
1051bdcd8170SKalle Valo 	/* program analog PLL register */
105231024d99SKevin Fang 	/* no need to control 40/44MHz clock on AR6004 */
105331024d99SKevin Fang 	if (ar->target_type != TARGET_TYPE_AR6004) {
1054bdcd8170SKalle Valo 		status = ath6kl_bmi_reg_write(ar, ATH6KL_ANALOG_PLL_REGISTER,
1055bdcd8170SKalle Valo 					      0xF9104001);
105631024d99SKevin Fang 
1057bdcd8170SKalle Valo 		if (status)
1058bdcd8170SKalle Valo 			return status;
1059bdcd8170SKalle Valo 
1060bdcd8170SKalle Valo 		/* Run at 80/88MHz by default */
1061bdcd8170SKalle Valo 		param = SM(CPU_CLOCK_STANDARD, 1);
1062bdcd8170SKalle Valo 
1063bdcd8170SKalle Valo 		address = RTC_BASE_ADDRESS + CPU_CLOCK_ADDRESS;
1064bdcd8170SKalle Valo 		status = ath6kl_bmi_reg_write(ar, address, param);
1065bdcd8170SKalle Valo 		if (status)
1066bdcd8170SKalle Valo 			return status;
106731024d99SKevin Fang 	}
1068bdcd8170SKalle Valo 
1069bdcd8170SKalle Valo 	param = 0;
1070bdcd8170SKalle Valo 	address = RTC_BASE_ADDRESS + LPO_CAL_ADDRESS;
1071bdcd8170SKalle Valo 	param = SM(LPO_CAL_ENABLE, 1);
1072bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_write(ar, address, param);
1073bdcd8170SKalle Valo 	if (status)
1074bdcd8170SKalle Valo 		return status;
1075bdcd8170SKalle Valo 
1076bdcd8170SKalle Valo 	/* WAR to avoid SDIO CRC err */
1077bdcd8170SKalle Valo 	if (ar->version.target_ver == AR6003_REV2_VERSION) {
1078bdcd8170SKalle Valo 		ath6kl_err("temporary war to avoid sdio crc error\n");
1079bdcd8170SKalle Valo 
1080bdcd8170SKalle Valo 		param = 0x20;
1081bdcd8170SKalle Valo 
1082bdcd8170SKalle Valo 		address = GPIO_BASE_ADDRESS + GPIO_PIN10_ADDRESS;
1083bdcd8170SKalle Valo 		status = ath6kl_bmi_reg_write(ar, address, param);
1084bdcd8170SKalle Valo 		if (status)
1085bdcd8170SKalle Valo 			return status;
1086bdcd8170SKalle Valo 
1087bdcd8170SKalle Valo 		address = GPIO_BASE_ADDRESS + GPIO_PIN11_ADDRESS;
1088bdcd8170SKalle Valo 		status = ath6kl_bmi_reg_write(ar, address, param);
1089bdcd8170SKalle Valo 		if (status)
1090bdcd8170SKalle Valo 			return status;
1091bdcd8170SKalle Valo 
1092bdcd8170SKalle Valo 		address = GPIO_BASE_ADDRESS + GPIO_PIN12_ADDRESS;
1093bdcd8170SKalle Valo 		status = ath6kl_bmi_reg_write(ar, address, param);
1094bdcd8170SKalle Valo 		if (status)
1095bdcd8170SKalle Valo 			return status;
1096bdcd8170SKalle Valo 
1097bdcd8170SKalle Valo 		address = GPIO_BASE_ADDRESS + GPIO_PIN13_ADDRESS;
1098bdcd8170SKalle Valo 		status = ath6kl_bmi_reg_write(ar, address, param);
1099bdcd8170SKalle Valo 		if (status)
1100bdcd8170SKalle Valo 			return status;
1101bdcd8170SKalle Valo 	}
1102bdcd8170SKalle Valo 
1103bdcd8170SKalle Valo 	/* write EEPROM data to Target RAM */
1104bdcd8170SKalle Valo 	status = ath6kl_upload_board_file(ar);
1105bdcd8170SKalle Valo 	if (status)
1106bdcd8170SKalle Valo 		return status;
1107bdcd8170SKalle Valo 
1108bdcd8170SKalle Valo 	/* transfer One time Programmable data */
1109bdcd8170SKalle Valo 	status = ath6kl_upload_otp(ar);
1110bdcd8170SKalle Valo 	if (status)
1111bdcd8170SKalle Valo 		return status;
1112bdcd8170SKalle Valo 
1113bdcd8170SKalle Valo 	/* Download Target firmware */
1114bdcd8170SKalle Valo 	status = ath6kl_upload_firmware(ar);
1115bdcd8170SKalle Valo 	if (status)
1116bdcd8170SKalle Valo 		return status;
1117bdcd8170SKalle Valo 
1118bdcd8170SKalle Valo 	status = ath6kl_upload_patch(ar);
1119bdcd8170SKalle Valo 	if (status)
1120bdcd8170SKalle Valo 		return status;
1121bdcd8170SKalle Valo 
1122bdcd8170SKalle Valo 	/* Restore system sleep */
1123bdcd8170SKalle Valo 	address = RTC_BASE_ADDRESS + SYSTEM_SLEEP_ADDRESS;
1124bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_write(ar, address, sleep);
1125bdcd8170SKalle Valo 	if (status)
1126bdcd8170SKalle Valo 		return status;
1127bdcd8170SKalle Valo 
1128bdcd8170SKalle Valo 	address = MBOX_BASE_ADDRESS + LOCAL_SCRATCH_ADDRESS;
1129bdcd8170SKalle Valo 	param = options | 0x20;
1130bdcd8170SKalle Valo 	status = ath6kl_bmi_reg_write(ar, address, param);
1131bdcd8170SKalle Valo 	if (status)
1132bdcd8170SKalle Valo 		return status;
1133bdcd8170SKalle Valo 
1134bdcd8170SKalle Valo 	/* Configure GPIO AR6003 UART */
1135bdcd8170SKalle Valo 	param = CONFIG_AR600x_DEBUG_UART_TX_PIN;
1136bdcd8170SKalle Valo 	status = ath6kl_bmi_write(ar,
1137bdcd8170SKalle Valo 				  ath6kl_get_hi_item_addr(ar,
1138bdcd8170SKalle Valo 				  HI_ITEM(hi_dbg_uart_txpin)),
1139bdcd8170SKalle Valo 				  (u8 *)&param, 4);
1140bdcd8170SKalle Valo 
1141bdcd8170SKalle Valo 	return status;
1142bdcd8170SKalle Valo }
1143bdcd8170SKalle Valo 
1144bdcd8170SKalle Valo static int ath6kl_init(struct net_device *dev)
1145bdcd8170SKalle Valo {
1146bdcd8170SKalle Valo 	struct ath6kl *ar = ath6kl_priv(dev);
1147bdcd8170SKalle Valo 	int status = 0;
1148bdcd8170SKalle Valo 	s32 timeleft;
1149bdcd8170SKalle Valo 
1150bdcd8170SKalle Valo 	if (!ar)
1151bdcd8170SKalle Valo 		return -EIO;
1152bdcd8170SKalle Valo 
1153bdcd8170SKalle Valo 	/* Do we need to finish the BMI phase */
1154bdcd8170SKalle Valo 	if (ath6kl_bmi_done(ar)) {
1155bdcd8170SKalle Valo 		status = -EIO;
1156bdcd8170SKalle Valo 		goto ath6kl_init_done;
1157bdcd8170SKalle Valo 	}
1158bdcd8170SKalle Valo 
1159bdcd8170SKalle Valo 	/* Indicate that WMI is enabled (although not ready yet) */
1160bdcd8170SKalle Valo 	set_bit(WMI_ENABLED, &ar->flag);
11612865785eSVasanthakumar Thiagarajan 	ar->wmi = ath6kl_wmi_init(ar);
1162bdcd8170SKalle Valo 	if (!ar->wmi) {
1163bdcd8170SKalle Valo 		ath6kl_err("failed to initialize wmi\n");
1164bdcd8170SKalle Valo 		status = -EIO;
1165bdcd8170SKalle Valo 		goto ath6kl_init_done;
1166bdcd8170SKalle Valo 	}
1167bdcd8170SKalle Valo 
1168bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_TRC, "%s: got wmi @ 0x%p.\n", __func__, ar->wmi);
1169bdcd8170SKalle Valo 
1170852bd9d9SVasanthakumar Thiagarajan 	wlan_node_table_init(&ar->scan_table);
1171852bd9d9SVasanthakumar Thiagarajan 
1172bdcd8170SKalle Valo 	/*
1173bdcd8170SKalle Valo 	 * The reason we have to wait for the target here is that the
1174bdcd8170SKalle Valo 	 * driver layer has to init BMI in order to set the host block
1175bdcd8170SKalle Valo 	 * size.
1176bdcd8170SKalle Valo 	 */
1177ad226ec2SKalle Valo 	if (ath6kl_htc_wait_target(ar->htc_target)) {
1178bdcd8170SKalle Valo 		status = -EIO;
1179852bd9d9SVasanthakumar Thiagarajan 		goto err_node_cleanup;
1180bdcd8170SKalle Valo 	}
1181bdcd8170SKalle Valo 
1182bdcd8170SKalle Valo 	if (ath6kl_init_service_ep(ar)) {
1183bdcd8170SKalle Valo 		status = -EIO;
1184bdcd8170SKalle Valo 		goto err_cleanup_scatter;
1185bdcd8170SKalle Valo 	}
1186bdcd8170SKalle Valo 
1187bdcd8170SKalle Valo 	/* setup access class priority mappings */
1188bdcd8170SKalle Valo 	ar->ac_stream_pri_map[WMM_AC_BK] = 0; /* lowest  */
1189bdcd8170SKalle Valo 	ar->ac_stream_pri_map[WMM_AC_BE] = 1;
1190bdcd8170SKalle Valo 	ar->ac_stream_pri_map[WMM_AC_VI] = 2;
1191bdcd8170SKalle Valo 	ar->ac_stream_pri_map[WMM_AC_VO] = 3; /* highest */
1192bdcd8170SKalle Valo 
1193bdcd8170SKalle Valo 	/* give our connected endpoints some buffers */
1194bdcd8170SKalle Valo 	ath6kl_rx_refill(ar->htc_target, ar->ctrl_ep);
1195bdcd8170SKalle Valo 	ath6kl_rx_refill(ar->htc_target, ar->ac2ep_map[WMM_AC_BE]);
1196bdcd8170SKalle Valo 
1197bdcd8170SKalle Valo 	/* allocate some buffers that handle larger AMSDU frames */
1198bdcd8170SKalle Valo 	ath6kl_refill_amsdu_rxbufs(ar, ATH6KL_MAX_AMSDU_RX_BUFFERS);
1199bdcd8170SKalle Valo 
1200bdcd8170SKalle Valo 	/* setup credit distribution */
1201bdcd8170SKalle Valo 	ath6k_setup_credit_dist(ar->htc_target, &ar->credit_state_info);
1202bdcd8170SKalle Valo 
1203bdcd8170SKalle Valo 	ath6kl_cookie_init(ar);
1204bdcd8170SKalle Valo 
1205bdcd8170SKalle Valo 	/* start HTC */
1206ad226ec2SKalle Valo 	status = ath6kl_htc_start(ar->htc_target);
1207bdcd8170SKalle Valo 
1208bdcd8170SKalle Valo 	if (status) {
1209bdcd8170SKalle Valo 		ath6kl_cookie_cleanup(ar);
1210bdcd8170SKalle Valo 		goto err_rxbuf_cleanup;
1211bdcd8170SKalle Valo 	}
1212bdcd8170SKalle Valo 
1213bdcd8170SKalle Valo 	/* Wait for Wmi event to be ready */
1214bdcd8170SKalle Valo 	timeleft = wait_event_interruptible_timeout(ar->event_wq,
1215bdcd8170SKalle Valo 						    test_bit(WMI_READY,
1216bdcd8170SKalle Valo 							     &ar->flag),
1217bdcd8170SKalle Valo 						    WMI_TIMEOUT);
1218bdcd8170SKalle Valo 
1219bdcd8170SKalle Valo 	if (ar->version.abi_ver != ATH6KL_ABI_VERSION) {
1220bdcd8170SKalle Valo 		ath6kl_err("abi version mismatch: host(0x%x), target(0x%x)\n",
1221bdcd8170SKalle Valo 			   ATH6KL_ABI_VERSION, ar->version.abi_ver);
1222bdcd8170SKalle Valo 		status = -EIO;
1223bdcd8170SKalle Valo 		goto err_htc_stop;
1224bdcd8170SKalle Valo 	}
1225bdcd8170SKalle Valo 
1226bdcd8170SKalle Valo 	if (!timeleft || signal_pending(current)) {
1227bdcd8170SKalle Valo 		ath6kl_err("wmi is not ready or wait was interrupted\n");
1228bdcd8170SKalle Valo 		status = -EIO;
1229bdcd8170SKalle Valo 		goto err_htc_stop;
1230bdcd8170SKalle Valo 	}
1231bdcd8170SKalle Valo 
1232bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_TRC, "%s: wmi is ready\n", __func__);
1233bdcd8170SKalle Valo 
1234bdcd8170SKalle Valo 	/* communicate the wmi protocol verision to the target */
1235bdcd8170SKalle Valo 	if ((ath6kl_set_host_app_area(ar)) != 0)
1236bdcd8170SKalle Valo 		ath6kl_err("unable to set the host app area\n");
1237bdcd8170SKalle Valo 
1238bdcd8170SKalle Valo 	ar->conf_flags = ATH6KL_CONF_IGNORE_ERP_BARKER |
1239bdcd8170SKalle Valo 			 ATH6KL_CONF_ENABLE_11N | ATH6KL_CONF_ENABLE_TX_BURST;
1240bdcd8170SKalle Valo 
1241bdcd8170SKalle Valo 	status = ath6kl_target_config_wlan_params(ar);
1242bdcd8170SKalle Valo 	if (!status)
1243bdcd8170SKalle Valo 		goto ath6kl_init_done;
1244bdcd8170SKalle Valo 
1245bdcd8170SKalle Valo err_htc_stop:
1246ad226ec2SKalle Valo 	ath6kl_htc_stop(ar->htc_target);
1247bdcd8170SKalle Valo err_rxbuf_cleanup:
1248ad226ec2SKalle Valo 	ath6kl_htc_flush_rx_buf(ar->htc_target);
1249bdcd8170SKalle Valo 	ath6kl_cleanup_amsdu_rxbufs(ar);
1250bdcd8170SKalle Valo err_cleanup_scatter:
1251bdcd8170SKalle Valo 	ath6kl_hif_cleanup_scatter(ar);
1252852bd9d9SVasanthakumar Thiagarajan err_node_cleanup:
1253852bd9d9SVasanthakumar Thiagarajan 	wlan_node_table_cleanup(&ar->scan_table);
1254bdcd8170SKalle Valo 	ath6kl_wmi_shutdown(ar->wmi);
1255bdcd8170SKalle Valo 	clear_bit(WMI_ENABLED, &ar->flag);
1256bdcd8170SKalle Valo 	ar->wmi = NULL;
1257bdcd8170SKalle Valo 
1258bdcd8170SKalle Valo ath6kl_init_done:
1259bdcd8170SKalle Valo 	return status;
1260bdcd8170SKalle Valo }
1261bdcd8170SKalle Valo 
1262bdcd8170SKalle Valo int ath6kl_core_init(struct ath6kl *ar)
1263bdcd8170SKalle Valo {
1264bdcd8170SKalle Valo 	int ret = 0;
1265bdcd8170SKalle Valo 	struct ath6kl_bmi_target_info targ_info;
1266bdcd8170SKalle Valo 
1267bdcd8170SKalle Valo 	ar->ath6kl_wq = create_singlethread_workqueue("ath6kl");
1268bdcd8170SKalle Valo 	if (!ar->ath6kl_wq)
1269bdcd8170SKalle Valo 		return -ENOMEM;
1270bdcd8170SKalle Valo 
1271bdcd8170SKalle Valo 	ret = ath6kl_bmi_init(ar);
1272bdcd8170SKalle Valo 	if (ret)
1273bdcd8170SKalle Valo 		goto err_wq;
1274bdcd8170SKalle Valo 
1275bdcd8170SKalle Valo 	ret = ath6kl_bmi_get_target_info(ar, &targ_info);
1276bdcd8170SKalle Valo 	if (ret)
1277bdcd8170SKalle Valo 		goto err_bmi_cleanup;
1278bdcd8170SKalle Valo 
1279bdcd8170SKalle Valo 	ar->version.target_ver = le32_to_cpu(targ_info.version);
1280bdcd8170SKalle Valo 	ar->target_type = le32_to_cpu(targ_info.type);
1281bdcd8170SKalle Valo 	ar->wdev->wiphy->hw_version = le32_to_cpu(targ_info.version);
1282bdcd8170SKalle Valo 
1283bdcd8170SKalle Valo 	ret = ath6kl_configure_target(ar);
1284bdcd8170SKalle Valo 	if (ret)
1285bdcd8170SKalle Valo 		goto err_bmi_cleanup;
1286bdcd8170SKalle Valo 
1287ad226ec2SKalle Valo 	ar->htc_target = ath6kl_htc_create(ar);
1288bdcd8170SKalle Valo 
1289bdcd8170SKalle Valo 	if (!ar->htc_target) {
1290bdcd8170SKalle Valo 		ret = -ENOMEM;
1291bdcd8170SKalle Valo 		goto err_bmi_cleanup;
1292bdcd8170SKalle Valo 	}
1293bdcd8170SKalle Valo 
1294bdcd8170SKalle Valo 	ar->aggr_cntxt = aggr_init(ar->net_dev);
1295bdcd8170SKalle Valo 	if (!ar->aggr_cntxt) {
1296bdcd8170SKalle Valo 		ath6kl_err("failed to initialize aggr\n");
1297bdcd8170SKalle Valo 		ret = -ENOMEM;
1298bdcd8170SKalle Valo 		goto err_htc_cleanup;
1299bdcd8170SKalle Valo 	}
1300bdcd8170SKalle Valo 
1301bdcd8170SKalle Valo 	ret = ath6kl_init_upload(ar);
1302bdcd8170SKalle Valo 	if (ret)
1303bdcd8170SKalle Valo 		goto err_htc_cleanup;
1304bdcd8170SKalle Valo 
1305bdcd8170SKalle Valo 	ret = ath6kl_init(ar->net_dev);
1306bdcd8170SKalle Valo 	if (ret)
1307bdcd8170SKalle Valo 		goto err_htc_cleanup;
1308bdcd8170SKalle Valo 
1309bdcd8170SKalle Valo 	/* This runs the init function if registered */
1310bdcd8170SKalle Valo 	ret = register_netdev(ar->net_dev);
1311bdcd8170SKalle Valo 	if (ret) {
1312bdcd8170SKalle Valo 		ath6kl_err("register_netdev failed\n");
1313bdcd8170SKalle Valo 		ath6kl_destroy(ar->net_dev, 0);
1314bdcd8170SKalle Valo 		return ret;
1315bdcd8170SKalle Valo 	}
1316bdcd8170SKalle Valo 
1317bdcd8170SKalle Valo 	set_bit(NETDEV_REGISTERED, &ar->flag);
1318bdcd8170SKalle Valo 
1319bdcd8170SKalle Valo 	ath6kl_dbg(ATH6KL_DBG_TRC, "%s: name=%s dev=0x%p, ar=0x%p\n",
1320bdcd8170SKalle Valo 			__func__, ar->net_dev->name, ar->net_dev, ar);
1321bdcd8170SKalle Valo 
1322bdcd8170SKalle Valo 	return ret;
1323bdcd8170SKalle Valo 
1324bdcd8170SKalle Valo err_htc_cleanup:
1325ad226ec2SKalle Valo 	ath6kl_htc_cleanup(ar->htc_target);
1326bdcd8170SKalle Valo err_bmi_cleanup:
1327bdcd8170SKalle Valo 	ath6kl_bmi_cleanup(ar);
1328bdcd8170SKalle Valo err_wq:
1329bdcd8170SKalle Valo 	destroy_workqueue(ar->ath6kl_wq);
1330bdcd8170SKalle Valo 	return ret;
1331bdcd8170SKalle Valo }
1332bdcd8170SKalle Valo 
1333bdcd8170SKalle Valo void ath6kl_stop_txrx(struct ath6kl *ar)
1334bdcd8170SKalle Valo {
1335bdcd8170SKalle Valo 	struct net_device *ndev = ar->net_dev;
1336bdcd8170SKalle Valo 
1337bdcd8170SKalle Valo 	if (!ndev)
1338bdcd8170SKalle Valo 		return;
1339bdcd8170SKalle Valo 
1340bdcd8170SKalle Valo 	set_bit(DESTROY_IN_PROGRESS, &ar->flag);
1341bdcd8170SKalle Valo 
1342bdcd8170SKalle Valo 	if (down_interruptible(&ar->sem)) {
1343bdcd8170SKalle Valo 		ath6kl_err("down_interruptible failed\n");
1344bdcd8170SKalle Valo 		return;
1345bdcd8170SKalle Valo 	}
1346bdcd8170SKalle Valo 
1347bdcd8170SKalle Valo 	if (ar->wlan_pwr_state != WLAN_POWER_STATE_CUT_PWR)
1348bdcd8170SKalle Valo 		ath6kl_stop_endpoint(ndev, false, true);
1349bdcd8170SKalle Valo 
1350575b5f34SRaja Mani 	clear_bit(WLAN_ENABLED, &ar->flag);
1351bdcd8170SKalle Valo }
1352bdcd8170SKalle Valo 
1353bdcd8170SKalle Valo /*
1354bdcd8170SKalle Valo  * We need to differentiate between the surprise and planned removal of the
1355bdcd8170SKalle Valo  * device because of the following consideration:
1356bdcd8170SKalle Valo  *
1357bdcd8170SKalle Valo  * - In case of surprise removal, the hcd already frees up the pending
1358bdcd8170SKalle Valo  *   for the device and hence there is no need to unregister the function
1359bdcd8170SKalle Valo  *   driver inorder to get these requests. For planned removal, the function
1360bdcd8170SKalle Valo  *   driver has to explicitly unregister itself to have the hcd return all the
1361bdcd8170SKalle Valo  *   pending requests before the data structures for the devices are freed up.
1362bdcd8170SKalle Valo  *   Note that as per the current implementation, the function driver will
1363bdcd8170SKalle Valo  *   end up releasing all the devices since there is no API to selectively
1364bdcd8170SKalle Valo  *   release a particular device.
1365bdcd8170SKalle Valo  *
1366bdcd8170SKalle Valo  * - Certain commands issued to the target can be skipped for surprise
1367bdcd8170SKalle Valo  *   removal since they will anyway not go through.
1368bdcd8170SKalle Valo  */
1369bdcd8170SKalle Valo void ath6kl_destroy(struct net_device *dev, unsigned int unregister)
1370bdcd8170SKalle Valo {
1371bdcd8170SKalle Valo 	struct ath6kl *ar;
1372bdcd8170SKalle Valo 
1373bdcd8170SKalle Valo 	if (!dev || !ath6kl_priv(dev)) {
1374bdcd8170SKalle Valo 		ath6kl_err("failed to get device structure\n");
1375bdcd8170SKalle Valo 		return;
1376bdcd8170SKalle Valo 	}
1377bdcd8170SKalle Valo 
1378bdcd8170SKalle Valo 	ar = ath6kl_priv(dev);
1379bdcd8170SKalle Valo 
1380bdcd8170SKalle Valo 	destroy_workqueue(ar->ath6kl_wq);
1381bdcd8170SKalle Valo 
1382bdcd8170SKalle Valo 	if (ar->htc_target)
1383ad226ec2SKalle Valo 		ath6kl_htc_cleanup(ar->htc_target);
1384bdcd8170SKalle Valo 
1385bdcd8170SKalle Valo 	aggr_module_destroy(ar->aggr_cntxt);
1386bdcd8170SKalle Valo 
1387bdcd8170SKalle Valo 	ath6kl_cookie_cleanup(ar);
1388bdcd8170SKalle Valo 
1389bdcd8170SKalle Valo 	ath6kl_cleanup_amsdu_rxbufs(ar);
1390bdcd8170SKalle Valo 
1391bdcd8170SKalle Valo 	ath6kl_bmi_cleanup(ar);
1392bdcd8170SKalle Valo 
1393bdf5396bSKalle Valo 	ath6kl_debug_cleanup(ar);
1394bdf5396bSKalle Valo 
1395bdcd8170SKalle Valo 	if (unregister && test_bit(NETDEV_REGISTERED, &ar->flag)) {
1396bdcd8170SKalle Valo 		unregister_netdev(dev);
1397bdcd8170SKalle Valo 		clear_bit(NETDEV_REGISTERED, &ar->flag);
1398bdcd8170SKalle Valo 	}
1399bdcd8170SKalle Valo 
1400bdcd8170SKalle Valo 	free_netdev(dev);
1401bdcd8170SKalle Valo 
1402852bd9d9SVasanthakumar Thiagarajan 	wlan_node_table_cleanup(&ar->scan_table);
1403852bd9d9SVasanthakumar Thiagarajan 
140419703573SRaja Mani 	kfree(ar->fw_board);
140519703573SRaja Mani 	kfree(ar->fw_otp);
140619703573SRaja Mani 	kfree(ar->fw);
140719703573SRaja Mani 	kfree(ar->fw_patch);
140819703573SRaja Mani 
1409bdcd8170SKalle Valo 	ath6kl_cfg80211_deinit(ar);
1410bdcd8170SKalle Valo }
1411