1 /*
2  * Copyright (c) 2014 Redpine Signals Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <linux/firmware.h>
18 #include <net/bluetooth/bluetooth.h>
19 #include "rsi_mgmt.h"
20 #include "rsi_hal.h"
21 #include "rsi_sdio.h"
22 #include "rsi_common.h"
23 
24 /* FLASH Firmware */
25 static struct ta_metadata metadata_flash_content[] = {
26 	{"flash_content", 0x00010000},
27 	{"rsi/rs9113_wlan_qspi.rps", 0x00010000},
28 	{"rsi/rs9113_wlan_bt_dual_mode.rps", 0x00010000},
29 	{"flash_content", 0x00010000},
30 	{"rsi/rs9113_ap_bt_dual_mode.rps", 0x00010000},
31 
32 };
33 
34 static struct ta_metadata metadata[] = {{"pmemdata_dummy", 0x00000000},
35 	{"rsi/rs9116_wlan.rps", 0x00000000},
36 	{"rsi/rs9116_wlan_bt_classic.rps", 0x00000000},
37 	{"rsi/pmemdata_dummy", 0x00000000},
38 	{"rsi/rs9116_wlan_bt_classic.rps", 0x00000000}
39 };
40 
41 int rsi_send_pkt_to_bus(struct rsi_common *common, struct sk_buff *skb)
42 {
43 	struct rsi_hw *adapter = common->priv;
44 	int status;
45 
46 	if (common->coex_mode > 1)
47 		mutex_lock(&common->tx_bus_mutex);
48 
49 	status = adapter->host_intf_ops->write_pkt(common->priv,
50 						   skb->data, skb->len);
51 
52 	if (common->coex_mode > 1)
53 		mutex_unlock(&common->tx_bus_mutex);
54 
55 	return status;
56 }
57 
58 int rsi_prepare_mgmt_desc(struct rsi_common *common, struct sk_buff *skb)
59 {
60 	struct rsi_hw *adapter = common->priv;
61 	struct ieee80211_hdr *wh = NULL;
62 	struct ieee80211_tx_info *info;
63 	struct ieee80211_conf *conf = &adapter->hw->conf;
64 	struct ieee80211_vif *vif;
65 	struct rsi_mgmt_desc *mgmt_desc;
66 	struct skb_info *tx_params;
67 	struct rsi_xtended_desc *xtend_desc = NULL;
68 	u8 header_size;
69 	u32 dword_align_bytes = 0;
70 
71 	if (skb->len > MAX_MGMT_PKT_SIZE) {
72 		rsi_dbg(INFO_ZONE, "%s: Dropping mgmt pkt > 512\n", __func__);
73 		return -EINVAL;
74 	}
75 
76 	info = IEEE80211_SKB_CB(skb);
77 	tx_params = (struct skb_info *)info->driver_data;
78 	vif = tx_params->vif;
79 
80 	/* Update header size */
81 	header_size = FRAME_DESC_SZ + sizeof(struct rsi_xtended_desc);
82 	if (header_size > skb_headroom(skb)) {
83 		rsi_dbg(ERR_ZONE,
84 			"%s: Failed to add extended descriptor\n",
85 			__func__);
86 		return -ENOSPC;
87 	}
88 	skb_push(skb, header_size);
89 	dword_align_bytes = ((unsigned long)skb->data & 0x3f);
90 	if (dword_align_bytes > skb_headroom(skb)) {
91 		rsi_dbg(ERR_ZONE,
92 			"%s: Failed to add dword align\n", __func__);
93 		return -ENOSPC;
94 	}
95 	skb_push(skb, dword_align_bytes);
96 	header_size += dword_align_bytes;
97 
98 	tx_params->internal_hdr_size = header_size;
99 	memset(&skb->data[0], 0, header_size);
100 	wh = (struct ieee80211_hdr *)&skb->data[header_size];
101 
102 	mgmt_desc = (struct rsi_mgmt_desc *)skb->data;
103 	xtend_desc = (struct rsi_xtended_desc *)&skb->data[FRAME_DESC_SZ];
104 
105 	rsi_set_len_qno(&mgmt_desc->len_qno, (skb->len - FRAME_DESC_SZ),
106 			RSI_WIFI_MGMT_Q);
107 	mgmt_desc->frame_type = TX_DOT11_MGMT;
108 	mgmt_desc->header_len = MIN_802_11_HDR_LEN;
109 	mgmt_desc->xtend_desc_size = header_size - FRAME_DESC_SZ;
110 
111 	if (ieee80211_is_probe_req(wh->frame_control))
112 		mgmt_desc->frame_info = cpu_to_le16(RSI_INSERT_SEQ_IN_FW);
113 	mgmt_desc->frame_info |= cpu_to_le16(RATE_INFO_ENABLE);
114 	if (is_broadcast_ether_addr(wh->addr1))
115 		mgmt_desc->frame_info |= cpu_to_le16(RSI_BROADCAST_PKT);
116 
117 	mgmt_desc->seq_ctrl =
118 		cpu_to_le16(IEEE80211_SEQ_TO_SN(le16_to_cpu(wh->seq_ctrl)));
119 	if ((common->band == NL80211_BAND_2GHZ) && !common->p2p_enabled)
120 		mgmt_desc->rate_info = cpu_to_le16(RSI_RATE_1);
121 	else
122 		mgmt_desc->rate_info = cpu_to_le16(RSI_RATE_6);
123 
124 	if (conf_is_ht40(conf))
125 		mgmt_desc->bbp_info = cpu_to_le16(FULL40M_ENABLE);
126 
127 	if (ieee80211_is_probe_resp(wh->frame_control)) {
128 		mgmt_desc->misc_flags |= (RSI_ADD_DELTA_TSF_VAP_ID |
129 					  RSI_FETCH_RETRY_CNT_FRM_HST);
130 #define PROBE_RESP_RETRY_CNT	3
131 		xtend_desc->retry_cnt = PROBE_RESP_RETRY_CNT;
132 	}
133 
134 	if (((vif->type == NL80211_IFTYPE_AP) ||
135 	     (vif->type == NL80211_IFTYPE_P2P_GO)) &&
136 	    (ieee80211_is_action(wh->frame_control))) {
137 		struct rsi_sta *rsta = rsi_find_sta(common, wh->addr1);
138 
139 		if (rsta)
140 			mgmt_desc->sta_id = tx_params->sta_id;
141 		else
142 			return -EINVAL;
143 	}
144 	mgmt_desc->rate_info |=
145 		cpu_to_le16((tx_params->vap_id << RSI_DESC_VAP_ID_OFST) &
146 			    RSI_DESC_VAP_ID_MASK);
147 
148 	return 0;
149 }
150 
151 /* This function prepares descriptor for given data packet */
152 int rsi_prepare_data_desc(struct rsi_common *common, struct sk_buff *skb)
153 {
154 	struct rsi_hw *adapter = common->priv;
155 	struct ieee80211_vif *vif;
156 	struct ieee80211_hdr *wh = NULL;
157 	struct ieee80211_tx_info *info;
158 	struct skb_info *tx_params;
159 	struct rsi_data_desc *data_desc;
160 	struct rsi_xtended_desc *xtend_desc;
161 	u8 ieee80211_size = MIN_802_11_HDR_LEN;
162 	u8 header_size;
163 	u8 vap_id = 0;
164 	u8 dword_align_bytes;
165 	u16 seq_num;
166 
167 	info = IEEE80211_SKB_CB(skb);
168 	vif = info->control.vif;
169 	tx_params = (struct skb_info *)info->driver_data;
170 
171 	header_size = FRAME_DESC_SZ + sizeof(struct rsi_xtended_desc);
172 	if (header_size > skb_headroom(skb)) {
173 		rsi_dbg(ERR_ZONE, "%s: Unable to send pkt\n", __func__);
174 		return -ENOSPC;
175 	}
176 	skb_push(skb, header_size);
177 	dword_align_bytes = ((unsigned long)skb->data & 0x3f);
178 	if (header_size > skb_headroom(skb)) {
179 		rsi_dbg(ERR_ZONE, "%s: Not enough headroom\n", __func__);
180 		return -ENOSPC;
181 	}
182 	skb_push(skb, dword_align_bytes);
183 	header_size += dword_align_bytes;
184 
185 	tx_params->internal_hdr_size = header_size;
186 	data_desc = (struct rsi_data_desc *)skb->data;
187 	memset(data_desc, 0, header_size);
188 
189 	xtend_desc = (struct rsi_xtended_desc *)&skb->data[FRAME_DESC_SZ];
190 	wh = (struct ieee80211_hdr *)&skb->data[header_size];
191 	seq_num = IEEE80211_SEQ_TO_SN(le16_to_cpu(wh->seq_ctrl));
192 
193 	data_desc->xtend_desc_size = header_size - FRAME_DESC_SZ;
194 
195 	if (ieee80211_is_data_qos(wh->frame_control)) {
196 		ieee80211_size += 2;
197 		data_desc->mac_flags |= cpu_to_le16(RSI_QOS_ENABLE);
198 	}
199 
200 	if (((vif->type == NL80211_IFTYPE_STATION) ||
201 	     (vif->type == NL80211_IFTYPE_P2P_CLIENT)) &&
202 	    (adapter->ps_state == PS_ENABLED))
203 		wh->frame_control |= cpu_to_le16(RSI_SET_PS_ENABLE);
204 
205 	if ((!(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT)) &&
206 	    tx_params->have_key) {
207 		if (rsi_is_cipher_wep(common))
208 			ieee80211_size += 4;
209 		else
210 			ieee80211_size += 8;
211 		data_desc->mac_flags |= cpu_to_le16(RSI_ENCRYPT_PKT);
212 	}
213 	rsi_set_len_qno(&data_desc->len_qno, (skb->len - FRAME_DESC_SZ),
214 			RSI_WIFI_DATA_Q);
215 	data_desc->header_len = ieee80211_size;
216 
217 	if (common->rate_config[common->band].fixed_enabled) {
218 		/* Send fixed rate */
219 		u16 fixed_rate = common->rate_config[common->band].fixed_hw_rate;
220 
221 		data_desc->frame_info = cpu_to_le16(RATE_INFO_ENABLE);
222 		data_desc->rate_info = cpu_to_le16(fixed_rate);
223 
224 		if (conf_is_ht40(&common->priv->hw->conf))
225 			data_desc->bbp_info = cpu_to_le16(FULL40M_ENABLE);
226 
227 		if (common->vif_info[0].sgi && (fixed_rate & 0x100)) {
228 		       /* Only MCS rates */
229 			data_desc->rate_info |=
230 				cpu_to_le16(ENABLE_SHORTGI_RATE);
231 		}
232 	}
233 
234 	if (skb->protocol == cpu_to_be16(ETH_P_PAE)) {
235 		rsi_dbg(INFO_ZONE, "*** Tx EAPOL ***\n");
236 
237 		data_desc->frame_info = cpu_to_le16(RATE_INFO_ENABLE);
238 		if (common->band == NL80211_BAND_5GHZ)
239 			data_desc->rate_info = cpu_to_le16(RSI_RATE_6);
240 		else
241 			data_desc->rate_info = cpu_to_le16(RSI_RATE_1);
242 		data_desc->mac_flags |= cpu_to_le16(RSI_REKEY_PURPOSE);
243 		data_desc->misc_flags |= RSI_FETCH_RETRY_CNT_FRM_HST;
244 #define EAPOL_RETRY_CNT 15
245 		xtend_desc->retry_cnt = EAPOL_RETRY_CNT;
246 
247 		if (common->eapol4_confirm)
248 			skb->priority = VO_Q;
249 		else
250 			rsi_set_len_qno(&data_desc->len_qno,
251 					(skb->len - FRAME_DESC_SZ),
252 					RSI_WIFI_MGMT_Q);
253 		if (((skb->len - header_size) == EAPOL4_PACKET_LEN) ||
254 		    ((skb->len - header_size) == EAPOL4_PACKET_LEN - 2)) {
255 			data_desc->misc_flags |=
256 				RSI_DESC_REQUIRE_CFM_TO_HOST;
257 			xtend_desc->confirm_frame_type = EAPOL4_CONFIRM;
258 		}
259 	}
260 
261 	data_desc->mac_flags |= cpu_to_le16(seq_num & 0xfff);
262 	data_desc->qid_tid = ((skb->priority & 0xf) |
263 			      ((tx_params->tid & 0xf) << 4));
264 	data_desc->sta_id = tx_params->sta_id;
265 
266 	if ((is_broadcast_ether_addr(wh->addr1)) ||
267 	    (is_multicast_ether_addr(wh->addr1))) {
268 		data_desc->frame_info = cpu_to_le16(RATE_INFO_ENABLE);
269 		data_desc->frame_info |= cpu_to_le16(RSI_BROADCAST_PKT);
270 		data_desc->sta_id = vap_id;
271 
272 		if ((vif->type == NL80211_IFTYPE_AP) ||
273 		    (vif->type == NL80211_IFTYPE_P2P_GO)) {
274 			if (common->band == NL80211_BAND_5GHZ)
275 				data_desc->rate_info = cpu_to_le16(RSI_RATE_6);
276 			else
277 				data_desc->rate_info = cpu_to_le16(RSI_RATE_1);
278 		}
279 	}
280 	if (((vif->type == NL80211_IFTYPE_AP) ||
281 	     (vif->type == NL80211_IFTYPE_P2P_GO)) &&
282 	    (ieee80211_has_moredata(wh->frame_control)))
283 		data_desc->frame_info |= cpu_to_le16(MORE_DATA_PRESENT);
284 
285 	data_desc->rate_info |=
286 		cpu_to_le16((tx_params->vap_id << RSI_DESC_VAP_ID_OFST) &
287 			    RSI_DESC_VAP_ID_MASK);
288 
289 	return 0;
290 }
291 
292 /* This function sends received data packet from driver to device */
293 int rsi_send_data_pkt(struct rsi_common *common, struct sk_buff *skb)
294 {
295 	struct rsi_hw *adapter = common->priv;
296 	struct ieee80211_vif *vif;
297 	struct ieee80211_tx_info *info;
298 	int status = -EINVAL;
299 
300 	if (!skb)
301 		return 0;
302 	if (common->iface_down)
303 		goto err;
304 
305 	info = IEEE80211_SKB_CB(skb);
306 	if (!info->control.vif)
307 		goto err;
308 	vif = info->control.vif;
309 
310 	if (((vif->type == NL80211_IFTYPE_STATION) ||
311 	     (vif->type == NL80211_IFTYPE_P2P_CLIENT)) &&
312 	    (!vif->cfg.assoc))
313 		goto err;
314 
315 	status = rsi_send_pkt_to_bus(common, skb);
316 	if (status)
317 		rsi_dbg(ERR_ZONE, "%s: Failed to write pkt\n", __func__);
318 
319 err:
320 	++common->tx_stats.total_tx_pkt_freed[skb->priority];
321 	rsi_indicate_tx_status(adapter, skb, status);
322 	return status;
323 }
324 
325 /**
326  * rsi_send_mgmt_pkt() - This functions sends the received management packet
327  *			 from driver to device.
328  * @common: Pointer to the driver private structure.
329  * @skb: Pointer to the socket buffer structure.
330  *
331  * Return: status: 0 on success, -1 on failure.
332  */
333 int rsi_send_mgmt_pkt(struct rsi_common *common,
334 		      struct sk_buff *skb)
335 {
336 	struct rsi_hw *adapter = common->priv;
337 	struct ieee80211_bss_conf *bss;
338 	struct ieee80211_hdr *wh;
339 	struct ieee80211_tx_info *info;
340 	struct skb_info *tx_params;
341 	struct rsi_mgmt_desc *mgmt_desc;
342 	struct rsi_xtended_desc *xtend_desc;
343 	int status = -E2BIG;
344 	u8 header_size;
345 
346 	info = IEEE80211_SKB_CB(skb);
347 	tx_params = (struct skb_info *)info->driver_data;
348 	header_size = tx_params->internal_hdr_size;
349 
350 	if (tx_params->flags & INTERNAL_MGMT_PKT) {
351 		status = adapter->host_intf_ops->write_pkt(common->priv,
352 							   (u8 *)skb->data,
353 							   skb->len);
354 		if (status) {
355 			rsi_dbg(ERR_ZONE,
356 				"%s: Failed to write the packet\n", __func__);
357 		}
358 		dev_kfree_skb(skb);
359 		return status;
360 	}
361 
362 	bss = &info->control.vif->bss_conf;
363 	wh = (struct ieee80211_hdr *)&skb->data[header_size];
364 	mgmt_desc = (struct rsi_mgmt_desc *)skb->data;
365 	xtend_desc = (struct rsi_xtended_desc *)&skb->data[FRAME_DESC_SZ];
366 
367 	/* Indicate to firmware to give cfm for probe */
368 	if (ieee80211_is_probe_req(wh->frame_control) &&
369 	    !info->control.vif->cfg.assoc) {
370 		rsi_dbg(INFO_ZONE,
371 			"%s: blocking mgmt queue\n", __func__);
372 		mgmt_desc->misc_flags = RSI_DESC_REQUIRE_CFM_TO_HOST;
373 		xtend_desc->confirm_frame_type = PROBEREQ_CONFIRM;
374 		common->mgmt_q_block = true;
375 		rsi_dbg(INFO_ZONE, "Mgmt queue blocked\n");
376 	}
377 
378 	status = rsi_send_pkt_to_bus(common, skb);
379 	if (status)
380 		rsi_dbg(ERR_ZONE, "%s: Failed to write the packet\n", __func__);
381 
382 	rsi_indicate_tx_status(common->priv, skb, status);
383 	return status;
384 }
385 
386 int rsi_send_bt_pkt(struct rsi_common *common, struct sk_buff *skb)
387 {
388 	int status = -EINVAL;
389 	u8 header_size = 0;
390 	struct rsi_bt_desc *bt_desc;
391 	u8 queueno = ((skb->data[1] >> 4) & 0xf);
392 
393 	if (queueno == RSI_BT_MGMT_Q) {
394 		status = rsi_send_pkt_to_bus(common, skb);
395 		if (status)
396 			rsi_dbg(ERR_ZONE, "%s: Failed to write bt mgmt pkt\n",
397 				__func__);
398 		goto out;
399 	}
400 	header_size = FRAME_DESC_SZ;
401 	if (header_size > skb_headroom(skb)) {
402 		rsi_dbg(ERR_ZONE, "%s: Not enough headroom\n", __func__);
403 		status = -ENOSPC;
404 		goto out;
405 	}
406 	skb_push(skb, header_size);
407 	memset(skb->data, 0, header_size);
408 	bt_desc = (struct rsi_bt_desc *)skb->data;
409 
410 	rsi_set_len_qno(&bt_desc->len_qno, (skb->len - FRAME_DESC_SZ),
411 			RSI_BT_DATA_Q);
412 	bt_desc->bt_pkt_type = cpu_to_le16(bt_cb(skb)->pkt_type);
413 
414 	status = rsi_send_pkt_to_bus(common, skb);
415 	if (status)
416 		rsi_dbg(ERR_ZONE, "%s: Failed to write bt pkt\n", __func__);
417 
418 out:
419 	dev_kfree_skb(skb);
420 	return status;
421 }
422 
423 int rsi_prepare_beacon(struct rsi_common *common, struct sk_buff *skb)
424 {
425 	struct rsi_hw *adapter = (struct rsi_hw *)common->priv;
426 	struct rsi_data_desc *bcn_frm;
427 	struct ieee80211_hw *hw = common->priv->hw;
428 	struct ieee80211_conf *conf = &hw->conf;
429 	struct ieee80211_vif *vif;
430 	struct sk_buff *mac_bcn;
431 	u8 vap_id = 0, i;
432 	u16 tim_offset = 0;
433 
434 	for (i = 0; i < RSI_MAX_VIFS; i++) {
435 		vif = adapter->vifs[i];
436 		if (!vif)
437 			continue;
438 		if ((vif->type == NL80211_IFTYPE_AP) ||
439 		    (vif->type == NL80211_IFTYPE_P2P_GO))
440 			break;
441 	}
442 	if (!vif)
443 		return -EINVAL;
444 	mac_bcn = ieee80211_beacon_get_tim(adapter->hw,
445 					   vif,
446 					   &tim_offset, NULL, 0);
447 	if (!mac_bcn) {
448 		rsi_dbg(ERR_ZONE, "Failed to get beacon from mac80211\n");
449 		return -EINVAL;
450 	}
451 
452 	common->beacon_cnt++;
453 	bcn_frm = (struct rsi_data_desc *)skb->data;
454 	rsi_set_len_qno(&bcn_frm->len_qno, mac_bcn->len, RSI_WIFI_DATA_Q);
455 	bcn_frm->header_len = MIN_802_11_HDR_LEN;
456 	bcn_frm->frame_info = cpu_to_le16(RSI_DATA_DESC_MAC_BBP_INFO |
457 					  RSI_DATA_DESC_NO_ACK_IND |
458 					  RSI_DATA_DESC_BEACON_FRAME |
459 					  RSI_DATA_DESC_INSERT_TSF |
460 					  RSI_DATA_DESC_INSERT_SEQ_NO |
461 					  RATE_INFO_ENABLE);
462 	bcn_frm->rate_info = cpu_to_le16(vap_id << 14);
463 	bcn_frm->qid_tid = BEACON_HW_Q;
464 
465 	if (conf_is_ht40_plus(conf)) {
466 		bcn_frm->bbp_info = cpu_to_le16(LOWER_20_ENABLE);
467 		bcn_frm->bbp_info |= cpu_to_le16(LOWER_20_ENABLE >> 12);
468 	} else if (conf_is_ht40_minus(conf)) {
469 		bcn_frm->bbp_info = cpu_to_le16(UPPER_20_ENABLE);
470 		bcn_frm->bbp_info |= cpu_to_le16(UPPER_20_ENABLE >> 12);
471 	}
472 
473 	if (common->band == NL80211_BAND_2GHZ)
474 		bcn_frm->rate_info |= cpu_to_le16(RSI_RATE_1);
475 	else
476 		bcn_frm->rate_info |= cpu_to_le16(RSI_RATE_6);
477 
478 	if (mac_bcn->data[tim_offset + 2] == 0)
479 		bcn_frm->frame_info |= cpu_to_le16(RSI_DATA_DESC_DTIM_BEACON);
480 
481 	memcpy(&skb->data[FRAME_DESC_SZ], mac_bcn->data, mac_bcn->len);
482 	skb_put(skb, mac_bcn->len + FRAME_DESC_SZ);
483 
484 	dev_kfree_skb(mac_bcn);
485 
486 	return 0;
487 }
488 
489 static void bl_cmd_timeout(struct timer_list *t)
490 {
491 	struct rsi_hw *adapter = from_timer(adapter, t, bl_cmd_timer);
492 
493 	adapter->blcmd_timer_expired = true;
494 	del_timer(&adapter->bl_cmd_timer);
495 }
496 
497 static int bl_start_cmd_timer(struct rsi_hw *adapter, u32 timeout)
498 {
499 	timer_setup(&adapter->bl_cmd_timer, bl_cmd_timeout, 0);
500 	adapter->bl_cmd_timer.expires = (msecs_to_jiffies(timeout) + jiffies);
501 
502 	adapter->blcmd_timer_expired = false;
503 	add_timer(&adapter->bl_cmd_timer);
504 
505 	return 0;
506 }
507 
508 static int bl_stop_cmd_timer(struct rsi_hw *adapter)
509 {
510 	adapter->blcmd_timer_expired = false;
511 	if (timer_pending(&adapter->bl_cmd_timer))
512 		del_timer(&adapter->bl_cmd_timer);
513 
514 	return 0;
515 }
516 
517 static int bl_write_cmd(struct rsi_hw *adapter, u8 cmd, u8 exp_resp,
518 			u16 *cmd_resp)
519 {
520 	struct rsi_host_intf_ops *hif_ops = adapter->host_intf_ops;
521 	u32 regin_val = 0, regout_val = 0;
522 	u32 regin_input = 0;
523 	u8 output = 0;
524 	int status;
525 
526 	regin_input = (REGIN_INPUT | adapter->priv->coex_mode);
527 
528 	while (!adapter->blcmd_timer_expired) {
529 		regin_val = 0;
530 		status = hif_ops->master_reg_read(adapter, SWBL_REGIN,
531 						  &regin_val, 2);
532 		if (status < 0) {
533 			rsi_dbg(ERR_ZONE,
534 				"%s: Command %0x REGIN reading failed..\n",
535 				__func__, cmd);
536 			return status;
537 		}
538 		mdelay(1);
539 		if ((regin_val >> 12) != REGIN_VALID)
540 			break;
541 	}
542 	if (adapter->blcmd_timer_expired) {
543 		rsi_dbg(ERR_ZONE,
544 			"%s: Command %0x REGIN reading timed out..\n",
545 			__func__, cmd);
546 		return -ETIMEDOUT;
547 	}
548 
549 	rsi_dbg(INFO_ZONE,
550 		"Issuing write to Regin val:%0x sending cmd:%0x\n",
551 		regin_val, (cmd | regin_input << 8));
552 	status = hif_ops->master_reg_write(adapter, SWBL_REGIN,
553 					   (cmd | regin_input << 8), 2);
554 	if (status < 0)
555 		return status;
556 	mdelay(1);
557 
558 	if (cmd == LOAD_HOSTED_FW || cmd == JUMP_TO_ZERO_PC) {
559 		/* JUMP_TO_ZERO_PC doesn't expect
560 		 * any response. So return from here
561 		 */
562 		return 0;
563 	}
564 
565 	while (!adapter->blcmd_timer_expired) {
566 		regout_val = 0;
567 		status = hif_ops->master_reg_read(adapter, SWBL_REGOUT,
568 					     &regout_val, 2);
569 		if (status < 0) {
570 			rsi_dbg(ERR_ZONE,
571 				"%s: Command %0x REGOUT reading failed..\n",
572 				__func__, cmd);
573 			return status;
574 		}
575 		mdelay(1);
576 		if ((regout_val >> 8) == REGOUT_VALID)
577 			break;
578 	}
579 	if (adapter->blcmd_timer_expired) {
580 		rsi_dbg(ERR_ZONE,
581 			"%s: Command %0x REGOUT reading timed out..\n",
582 			__func__, cmd);
583 		return status;
584 	}
585 
586 	*cmd_resp = ((u16 *)&regout_val)[0] & 0xffff;
587 
588 	output = ((u8 *)&regout_val)[0] & 0xff;
589 
590 	status = hif_ops->master_reg_write(adapter, SWBL_REGOUT,
591 					   (cmd | REGOUT_INVALID << 8), 2);
592 	if (status < 0) {
593 		rsi_dbg(ERR_ZONE,
594 			"%s: Command %0x REGOUT writing failed..\n",
595 			__func__, cmd);
596 		return status;
597 	}
598 	mdelay(1);
599 
600 	if (output != exp_resp) {
601 		rsi_dbg(ERR_ZONE,
602 			"%s: Recvd resp %x for cmd %0x\n",
603 			__func__, output, cmd);
604 		return -EINVAL;
605 	}
606 	rsi_dbg(INFO_ZONE,
607 		"%s: Recvd Expected resp %x for cmd %0x\n",
608 		__func__, output, cmd);
609 
610 	return 0;
611 }
612 
613 static int bl_cmd(struct rsi_hw *adapter, u8 cmd, u8 exp_resp, char *str)
614 {
615 	u16 regout_val = 0;
616 	u32 timeout;
617 	int status;
618 
619 	if ((cmd == EOF_REACHED) || (cmd == PING_VALID) || (cmd == PONG_VALID))
620 		timeout = BL_BURN_TIMEOUT;
621 	else
622 		timeout = BL_CMD_TIMEOUT;
623 
624 	bl_start_cmd_timer(adapter, timeout);
625 	status = bl_write_cmd(adapter, cmd, exp_resp, &regout_val);
626 	if (status < 0) {
627 		bl_stop_cmd_timer(adapter);
628 		rsi_dbg(ERR_ZONE,
629 			"%s: Command %s (%0x) writing failed..\n",
630 			__func__, str, cmd);
631 		return status;
632 	}
633 	bl_stop_cmd_timer(adapter);
634 	return 0;
635 }
636 
637 #define CHECK_SUM_OFFSET 20
638 #define LEN_OFFSET 8
639 #define ADDR_OFFSET 16
640 static int bl_write_header(struct rsi_hw *adapter, u8 *flash_content,
641 			   u32 content_size)
642 {
643 	struct rsi_host_intf_ops *hif_ops = adapter->host_intf_ops;
644 	struct bl_header *bl_hdr;
645 	u32 write_addr, write_len;
646 	int status;
647 
648 	bl_hdr = kzalloc(sizeof(*bl_hdr), GFP_KERNEL);
649 	if (!bl_hdr)
650 		return -ENOMEM;
651 
652 	bl_hdr->flags = 0;
653 	bl_hdr->image_no = cpu_to_le32(adapter->priv->coex_mode);
654 	bl_hdr->check_sum =
655 		cpu_to_le32(*(u32 *)&flash_content[CHECK_SUM_OFFSET]);
656 	bl_hdr->flash_start_address =
657 		cpu_to_le32(*(u32 *)&flash_content[ADDR_OFFSET]);
658 	bl_hdr->flash_len = cpu_to_le32(*(u32 *)&flash_content[LEN_OFFSET]);
659 	write_len = sizeof(struct bl_header);
660 
661 	if (adapter->rsi_host_intf == RSI_HOST_INTF_USB) {
662 		write_addr = PING_BUFFER_ADDRESS;
663 		status = hif_ops->write_reg_multiple(adapter, write_addr,
664 						 (u8 *)bl_hdr, write_len);
665 		if (status < 0) {
666 			rsi_dbg(ERR_ZONE,
667 				"%s: Failed to load Version/CRC structure\n",
668 				__func__);
669 			goto fail;
670 		}
671 	} else {
672 		write_addr = PING_BUFFER_ADDRESS >> 16;
673 		status = hif_ops->master_access_msword(adapter, write_addr);
674 		if (status < 0) {
675 			rsi_dbg(ERR_ZONE,
676 				"%s: Unable to set ms word to common reg\n",
677 				__func__);
678 			goto fail;
679 		}
680 		write_addr = RSI_SD_REQUEST_MASTER |
681 			     (PING_BUFFER_ADDRESS & 0xFFFF);
682 		status = hif_ops->write_reg_multiple(adapter, write_addr,
683 						 (u8 *)bl_hdr, write_len);
684 		if (status < 0) {
685 			rsi_dbg(ERR_ZONE,
686 				"%s: Failed to load Version/CRC structure\n",
687 				__func__);
688 			goto fail;
689 		}
690 	}
691 	status = 0;
692 fail:
693 	kfree(bl_hdr);
694 	return status;
695 }
696 
697 static u32 read_flash_capacity(struct rsi_hw *adapter)
698 {
699 	u32 flash_sz = 0;
700 
701 	if ((adapter->host_intf_ops->master_reg_read(adapter, FLASH_SIZE_ADDR,
702 						     &flash_sz, 2)) < 0) {
703 		rsi_dbg(ERR_ZONE,
704 			"%s: Flash size reading failed..\n",
705 			__func__);
706 		return 0;
707 	}
708 	rsi_dbg(INIT_ZONE, "Flash capacity: %d KiloBytes\n", flash_sz);
709 
710 	return (flash_sz * 1024); /* Return size in kbytes */
711 }
712 
713 static int ping_pong_write(struct rsi_hw *adapter, u8 cmd, u8 *addr, u32 size)
714 {
715 	struct rsi_host_intf_ops *hif_ops = adapter->host_intf_ops;
716 	u32 block_size = adapter->block_size;
717 	u32 cmd_addr;
718 	u16 cmd_resp, cmd_req;
719 	u8 *str;
720 	int status;
721 
722 	if (cmd == PING_WRITE) {
723 		cmd_addr = PING_BUFFER_ADDRESS;
724 		cmd_resp = PONG_AVAIL;
725 		cmd_req = PING_VALID;
726 		str = "PING_VALID";
727 	} else {
728 		cmd_addr = PONG_BUFFER_ADDRESS;
729 		cmd_resp = PING_AVAIL;
730 		cmd_req = PONG_VALID;
731 		str = "PONG_VALID";
732 	}
733 
734 	status = hif_ops->load_data_master_write(adapter, cmd_addr, size,
735 					    block_size, addr);
736 	if (status) {
737 		rsi_dbg(ERR_ZONE, "%s: Unable to write blk at addr %0x\n",
738 			__func__, *addr);
739 		return status;
740 	}
741 
742 	status = bl_cmd(adapter, cmd_req, cmd_resp, str);
743 	if (status)
744 		return status;
745 
746 	return 0;
747 }
748 
749 static int auto_fw_upgrade(struct rsi_hw *adapter, u8 *flash_content,
750 			   u32 content_size)
751 {
752 	u8 cmd;
753 	u32 temp_content_size, num_flash, index;
754 	u32 flash_start_address;
755 	int status;
756 
757 	if (content_size > MAX_FLASH_FILE_SIZE) {
758 		rsi_dbg(ERR_ZONE,
759 			"%s: Flash Content size is more than 400K %u\n",
760 			__func__, MAX_FLASH_FILE_SIZE);
761 		return -EINVAL;
762 	}
763 
764 	flash_start_address = *(u32 *)&flash_content[FLASH_START_ADDRESS];
765 	rsi_dbg(INFO_ZONE, "flash start address: %08x\n", flash_start_address);
766 
767 	if (flash_start_address < FW_IMAGE_MIN_ADDRESS) {
768 		rsi_dbg(ERR_ZONE,
769 			"%s: Fw image Flash Start Address is less than 64K\n",
770 			__func__);
771 		return -EINVAL;
772 	}
773 
774 	if (flash_start_address % FLASH_SECTOR_SIZE) {
775 		rsi_dbg(ERR_ZONE,
776 			"%s: Flash Start Address is not multiple of 4K\n",
777 			__func__);
778 		return -EINVAL;
779 	}
780 
781 	if ((flash_start_address + content_size) > adapter->flash_capacity) {
782 		rsi_dbg(ERR_ZONE,
783 			"%s: Flash Content will cross max flash size\n",
784 			__func__);
785 		return -EINVAL;
786 	}
787 
788 	temp_content_size  = content_size;
789 	num_flash = content_size / FLASH_WRITE_CHUNK_SIZE;
790 
791 	rsi_dbg(INFO_ZONE, "content_size: %d, num_flash: %d\n",
792 		content_size, num_flash);
793 
794 	for (index = 0; index <= num_flash; index++) {
795 		rsi_dbg(INFO_ZONE, "flash index: %d\n", index);
796 		if (index != num_flash) {
797 			content_size = FLASH_WRITE_CHUNK_SIZE;
798 			rsi_dbg(INFO_ZONE, "QSPI content_size:%d\n",
799 				content_size);
800 		} else {
801 			content_size =
802 				temp_content_size % FLASH_WRITE_CHUNK_SIZE;
803 			rsi_dbg(INFO_ZONE,
804 				"Writing last sector content_size:%d\n",
805 				content_size);
806 			if (!content_size) {
807 				rsi_dbg(INFO_ZONE, "instruction size zero\n");
808 				break;
809 			}
810 		}
811 
812 		if (index % 2)
813 			cmd = PING_WRITE;
814 		else
815 			cmd = PONG_WRITE;
816 
817 		status = ping_pong_write(adapter, cmd, flash_content,
818 					 content_size);
819 		if (status) {
820 			rsi_dbg(ERR_ZONE, "%s: Unable to load %d block\n",
821 				__func__, index);
822 			return status;
823 		}
824 
825 		rsi_dbg(INFO_ZONE,
826 			"%s: Successfully loaded %d instructions\n",
827 			__func__, index);
828 		flash_content += content_size;
829 	}
830 
831 	status = bl_cmd(adapter, EOF_REACHED, FW_LOADING_SUCCESSFUL,
832 			"EOF_REACHED");
833 	if (status)
834 		return status;
835 
836 	rsi_dbg(INFO_ZONE, "FW loading is done and FW is running..\n");
837 	return 0;
838 }
839 
840 static int rsi_hal_prepare_fwload(struct rsi_hw *adapter)
841 {
842 	struct rsi_host_intf_ops *hif_ops = adapter->host_intf_ops;
843 	u32 regout_val = 0;
844 	int status;
845 
846 	bl_start_cmd_timer(adapter, BL_CMD_TIMEOUT);
847 
848 	while (!adapter->blcmd_timer_expired) {
849 		status = hif_ops->master_reg_read(adapter, SWBL_REGOUT,
850 						  &regout_val,
851 						  RSI_COMMON_REG_SIZE);
852 		if (status < 0) {
853 			bl_stop_cmd_timer(adapter);
854 			rsi_dbg(ERR_ZONE,
855 				"%s: REGOUT read failed\n", __func__);
856 			return status;
857 		}
858 		mdelay(1);
859 		if ((regout_val >> 8) == REGOUT_VALID)
860 			break;
861 	}
862 	if (adapter->blcmd_timer_expired) {
863 		rsi_dbg(ERR_ZONE, "%s: REGOUT read timedout\n", __func__);
864 		rsi_dbg(ERR_ZONE,
865 			"%s: Soft boot loader not present\n", __func__);
866 		return -ETIMEDOUT;
867 	}
868 	bl_stop_cmd_timer(adapter);
869 
870 	rsi_dbg(INFO_ZONE, "Received Board Version Number: %x\n",
871 		(regout_val & 0xff));
872 
873 	status = hif_ops->master_reg_write(adapter, SWBL_REGOUT,
874 					   (REGOUT_INVALID |
875 					    REGOUT_INVALID << 8),
876 					   RSI_COMMON_REG_SIZE);
877 	if (status < 0)
878 		rsi_dbg(ERR_ZONE, "%s: REGOUT writing failed..\n", __func__);
879 	else
880 		rsi_dbg(INFO_ZONE,
881 			"===> Device is ready to load firmware <===\n");
882 
883 	return status;
884 }
885 
886 static int rsi_load_9113_firmware(struct rsi_hw *adapter)
887 {
888 	struct rsi_common *common = adapter->priv;
889 	const struct firmware *fw_entry = NULL;
890 	u32 content_size;
891 	u16 tmp_regout_val = 0;
892 	struct ta_metadata *metadata_p;
893 	int status;
894 
895 	status = bl_cmd(adapter, CONFIG_AUTO_READ_MODE, CMD_PASS,
896 			"AUTO_READ_CMD");
897 	if (status < 0)
898 		return status;
899 
900 	adapter->flash_capacity = read_flash_capacity(adapter);
901 	if (adapter->flash_capacity <= 0) {
902 		rsi_dbg(ERR_ZONE,
903 			"%s: Unable to read flash size from EEPROM\n",
904 			__func__);
905 		return -EINVAL;
906 	}
907 
908 	metadata_p = &metadata_flash_content[adapter->priv->coex_mode];
909 
910 	rsi_dbg(INIT_ZONE, "%s: Loading file %s\n", __func__, metadata_p->name);
911 	adapter->fw_file_name = metadata_p->name;
912 
913 	status = request_firmware(&fw_entry, metadata_p->name, adapter->device);
914 	if (status < 0) {
915 		rsi_dbg(ERR_ZONE, "%s: Failed to open file %s\n",
916 			__func__, metadata_p->name);
917 		return status;
918 	}
919 	content_size = fw_entry->size;
920 	rsi_dbg(INFO_ZONE, "FW Length = %d bytes\n", content_size);
921 
922 	/* Get the firmware version */
923 	common->lmac_ver.ver.info.fw_ver[0] =
924 		fw_entry->data[LMAC_VER_OFFSET_9113] & 0xFF;
925 	common->lmac_ver.ver.info.fw_ver[1] =
926 		fw_entry->data[LMAC_VER_OFFSET_9113 + 1] & 0xFF;
927 	common->lmac_ver.major =
928 		fw_entry->data[LMAC_VER_OFFSET_9113 + 2] & 0xFF;
929 	common->lmac_ver.release_num =
930 		fw_entry->data[LMAC_VER_OFFSET_9113 + 3] & 0xFF;
931 	common->lmac_ver.minor =
932 		fw_entry->data[LMAC_VER_OFFSET_9113 + 4] & 0xFF;
933 	common->lmac_ver.patch_num = 0;
934 	rsi_print_version(common);
935 
936 	status = bl_write_header(adapter, (u8 *)fw_entry->data, content_size);
937 	if (status) {
938 		rsi_dbg(ERR_ZONE,
939 			"%s: RPS Image header loading failed\n",
940 			__func__);
941 		goto fail;
942 	}
943 
944 	bl_start_cmd_timer(adapter, BL_CMD_TIMEOUT);
945 	status = bl_write_cmd(adapter, CHECK_CRC, CMD_PASS, &tmp_regout_val);
946 	if (status) {
947 		bl_stop_cmd_timer(adapter);
948 		rsi_dbg(ERR_ZONE,
949 			"%s: CHECK_CRC Command writing failed..\n",
950 			__func__);
951 		if ((tmp_regout_val & 0xff) == CMD_FAIL) {
952 			rsi_dbg(ERR_ZONE,
953 				"CRC Fail.. Proceeding to Upgrade mode\n");
954 			goto fw_upgrade;
955 		}
956 	}
957 	bl_stop_cmd_timer(adapter);
958 
959 	status = bl_cmd(adapter, POLLING_MODE, CMD_PASS, "POLLING_MODE");
960 	if (status)
961 		goto fail;
962 
963 load_image_cmd:
964 	status = bl_cmd(adapter, LOAD_HOSTED_FW, LOADING_INITIATED,
965 			"LOAD_HOSTED_FW");
966 	if (status)
967 		goto fail;
968 	rsi_dbg(INFO_ZONE, "Load Image command passed..\n");
969 	goto success;
970 
971 fw_upgrade:
972 	status = bl_cmd(adapter, BURN_HOSTED_FW, SEND_RPS_FILE, "FW_UPGRADE");
973 	if (status)
974 		goto fail;
975 
976 	rsi_dbg(INFO_ZONE, "Burn Command Pass.. Upgrading the firmware\n");
977 
978 	status = auto_fw_upgrade(adapter, (u8 *)fw_entry->data, content_size);
979 	if (status == 0) {
980 		rsi_dbg(ERR_ZONE, "Firmware upgradation Done\n");
981 		goto load_image_cmd;
982 	}
983 	rsi_dbg(ERR_ZONE, "Firmware upgrade failed\n");
984 
985 	status = bl_cmd(adapter, CONFIG_AUTO_READ_MODE, CMD_PASS,
986 			"AUTO_READ_MODE");
987 	if (status)
988 		goto fail;
989 
990 success:
991 	rsi_dbg(ERR_ZONE, "***** Firmware Loading successful *****\n");
992 	release_firmware(fw_entry);
993 	return 0;
994 
995 fail:
996 	rsi_dbg(ERR_ZONE, "##### Firmware loading failed #####\n");
997 	release_firmware(fw_entry);
998 	return status;
999 }
1000 
1001 static int rsi_load_9116_firmware(struct rsi_hw *adapter)
1002 {
1003 	struct rsi_common *common = adapter->priv;
1004 	struct rsi_host_intf_ops *hif_ops = adapter->host_intf_ops;
1005 	const struct firmware *fw_entry;
1006 	struct ta_metadata *metadata_p;
1007 	u8 *ta_firmware, *fw_p;
1008 	struct bootload_ds bootload_ds;
1009 	u32 instructions_sz, base_address;
1010 	u16 block_size = adapter->block_size;
1011 	u32 dest, len;
1012 	int status, cnt;
1013 
1014 	rsi_dbg(INIT_ZONE, "***** Load 9116 TA Instructions *****\n");
1015 
1016 	if (adapter->rsi_host_intf == RSI_HOST_INTF_USB) {
1017 		status = bl_cmd(adapter, POLLING_MODE, CMD_PASS,
1018 				"POLLING_MODE");
1019 		if (status < 0)
1020 			return status;
1021 	}
1022 
1023 	status = hif_ops->master_reg_write(adapter, MEM_ACCESS_CTRL_FROM_HOST,
1024 					   RAM_384K_ACCESS_FROM_TA,
1025 					   RSI_9116_REG_SIZE);
1026 	if (status < 0) {
1027 		rsi_dbg(ERR_ZONE, "%s: Unable to access full RAM memory\n",
1028 			__func__);
1029 		return status;
1030 	}
1031 
1032 	metadata_p = &metadata[adapter->priv->coex_mode];
1033 	rsi_dbg(INIT_ZONE, "%s: loading file %s\n", __func__, metadata_p->name);
1034 	status = request_firmware(&fw_entry, metadata_p->name, adapter->device);
1035 	if (status < 0) {
1036 		rsi_dbg(ERR_ZONE, "%s: Failed to open file %s\n",
1037 			__func__, metadata_p->name);
1038 		return status;
1039 	}
1040 
1041 	ta_firmware = kmemdup(fw_entry->data, fw_entry->size, GFP_KERNEL);
1042 	if (!ta_firmware) {
1043 		status = -ENOMEM;
1044 		goto fail_release_fw;
1045 	}
1046 	fw_p = ta_firmware;
1047 	instructions_sz = fw_entry->size;
1048 	rsi_dbg(INFO_ZONE, "FW Length = %d bytes\n", instructions_sz);
1049 
1050 	common->lmac_ver.major = ta_firmware[LMAC_VER_OFFSET_9116];
1051 	common->lmac_ver.minor = ta_firmware[LMAC_VER_OFFSET_9116 + 1];
1052 	common->lmac_ver.release_num = ta_firmware[LMAC_VER_OFFSET_9116 + 2];
1053 	common->lmac_ver.patch_num = ta_firmware[LMAC_VER_OFFSET_9116 + 3];
1054 	common->lmac_ver.ver.info.fw_ver[0] =
1055 		ta_firmware[LMAC_VER_OFFSET_9116 + 4];
1056 
1057 	if (instructions_sz % FW_ALIGN_SIZE)
1058 		instructions_sz +=
1059 			(FW_ALIGN_SIZE - (instructions_sz % FW_ALIGN_SIZE));
1060 	rsi_dbg(INFO_ZONE, "instructions_sz : %d\n", instructions_sz);
1061 
1062 	if (*(u16 *)fw_p == RSI_9116_FW_MAGIC_WORD) {
1063 		memcpy(&bootload_ds, fw_p, sizeof(struct bootload_ds));
1064 		fw_p += le16_to_cpu(bootload_ds.offset);
1065 		rsi_dbg(INFO_ZONE, "FW start = %x\n", *(u32 *)fw_p);
1066 
1067 		cnt = 0;
1068 		do {
1069 			rsi_dbg(ERR_ZONE, "%s: Loading chunk %d\n",
1070 				__func__, cnt);
1071 
1072 			dest = le32_to_cpu(bootload_ds.bl_entry[cnt].dst_addr);
1073 			len = le32_to_cpu(bootload_ds.bl_entry[cnt].control) &
1074 			      RSI_BL_CTRL_LEN_MASK;
1075 			rsi_dbg(INFO_ZONE, "length %d destination %x\n",
1076 				len, dest);
1077 
1078 			status = hif_ops->load_data_master_write(adapter, dest,
1079 								 len,
1080 								 block_size,
1081 								 fw_p);
1082 			if (status < 0) {
1083 				rsi_dbg(ERR_ZONE,
1084 					"Failed to load chunk %d\n", cnt);
1085 				break;
1086 			}
1087 			fw_p += len;
1088 			if (le32_to_cpu(bootload_ds.bl_entry[cnt].control) &
1089 			    RSI_BL_CTRL_LAST_ENTRY)
1090 				break;
1091 			cnt++;
1092 		} while (1);
1093 	} else {
1094 		base_address = metadata_p->address;
1095 		status = hif_ops->load_data_master_write(adapter,
1096 							 base_address,
1097 							 instructions_sz,
1098 							 block_size,
1099 							 ta_firmware);
1100 	}
1101 	if (status) {
1102 		rsi_dbg(ERR_ZONE,
1103 			"%s: Unable to load %s blk\n",
1104 			__func__, metadata_p->name);
1105 		goto fail_free_fw;
1106 	}
1107 
1108 	rsi_dbg(INIT_ZONE, "%s: Successfully loaded %s instructions\n",
1109 		__func__, metadata_p->name);
1110 
1111 	if (adapter->rsi_host_intf == RSI_HOST_INTF_SDIO) {
1112 		if (hif_ops->ta_reset(adapter))
1113 			rsi_dbg(ERR_ZONE, "Unable to put ta in reset\n");
1114 	} else {
1115 		if (bl_cmd(adapter, JUMP_TO_ZERO_PC,
1116 			   CMD_PASS, "JUMP_TO_ZERO") < 0)
1117 			rsi_dbg(INFO_ZONE, "Jump to zero command failed\n");
1118 		else
1119 			rsi_dbg(INFO_ZONE, "Jump to zero command successful\n");
1120 	}
1121 
1122 fail_free_fw:
1123 	kfree(ta_firmware);
1124 fail_release_fw:
1125 	release_firmware(fw_entry);
1126 
1127 	return status;
1128 }
1129 
1130 int rsi_hal_device_init(struct rsi_hw *adapter)
1131 {
1132 	struct rsi_common *common = adapter->priv;
1133 	int status;
1134 
1135 	switch (adapter->device_model) {
1136 	case RSI_DEV_9113:
1137 		status = rsi_hal_prepare_fwload(adapter);
1138 		if (status < 0)
1139 			return status;
1140 		if (rsi_load_9113_firmware(adapter)) {
1141 			rsi_dbg(ERR_ZONE,
1142 				"%s: Failed to load TA instructions\n",
1143 				__func__);
1144 			return -EINVAL;
1145 		}
1146 		break;
1147 	case RSI_DEV_9116:
1148 		status = rsi_hal_prepare_fwload(adapter);
1149 		if (status < 0)
1150 			return status;
1151 		if (rsi_load_9116_firmware(adapter)) {
1152 			rsi_dbg(ERR_ZONE,
1153 				"%s: Failed to load firmware to 9116 device\n",
1154 				__func__);
1155 			return -EINVAL;
1156 		}
1157 		break;
1158 	default:
1159 		return -EINVAL;
1160 	}
1161 	common->fsm_state = FSM_CARD_NOT_READY;
1162 
1163 	return 0;
1164 }
1165 EXPORT_SYMBOL_GPL(rsi_hal_device_init);
1166 
1167