xref: /wlan-dirver/qca-wifi-host-cmn/hif/src/usb/if_usb.c (revision 11f5a63a6cbdda84849a730de22f0a71e635d58c)
1 /*
2  * Copyright (c) 2013-2019 The Linux Foundation. All rights reserved.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all
7  * copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <linux/usb.h>
20 #include <linux/usb/hcd.h>
21 #include "if_usb.h"
22 #include "hif_usb_internal.h"
23 #include "target_type.h"		/* TARGET_TYPE_ */
24 #include "regtable_usb.h"
25 #include "ol_fw.h"
26 #include "hif_debug.h"
27 #include "epping_main.h"
28 #include "hif_main.h"
29 #include "usb_api.h"
30 
31 #define DELAY_FOR_TARGET_READY 200	/* 200ms */
32 
33 /* Save memory addresses where we save FW ram dump, and then we could obtain
34  * them by symbol table.
35  */
36 uint32_t fw_stack_addr;
37 void *fw_ram_seg_addr[FW_RAM_SEG_CNT];
38 
39 
40 
41 static int hif_usb_unload_dev_num = -1;
42 struct hif_usb_softc *g_usb_sc;
43 
44 /**
45  * hif_usb_diag_write_cold_reset() - reset SOC by sending a diag command
46  * @scn: pointer to ol_softc structure
47  *
48  * Return: QDF_STATUS_SUCCESS if success else an appropriate QDF_STATUS error
49  */
50 static inline QDF_STATUS
51 hif_usb_diag_write_cold_reset(struct hif_softc *scn)
52 {
53 	struct hif_opaque_softc *hif_hdl = GET_HIF_OPAQUE_HDL(scn);
54 	struct hif_target_info *tgt_info = &scn->target_info;
55 
56 	/* For Genoa, chip-reset is handled in CNSS driver */
57 	if (tgt_info->target_type == TARGET_TYPE_QCN7605)
58 		return QDF_STATUS_SUCCESS;
59 
60 	HIF_DBG("%s: resetting SOC", __func__);
61 
62 	return hif_diag_write_access(hif_hdl,
63 				(ROME_USB_SOC_RESET_CONTROL_COLD_RST_LSB |
64 				ROME_USB_RTC_SOC_BASE_ADDRESS),
65 				SOC_RESET_CONTROL_COLD_RST_SET(1));
66 }
67 
68 /**
69  * hif_usb_procfs_init() - create init procfs
70  * @scn: pointer to hif_usb_softc structure
71  *
72  * Return: int 0 if success else an appropriate error number
73  */
74 static int
75 hif_usb_procfs_init(struct hif_softc *scn)
76 {
77 	int ret = 0;
78 
79 	HIF_ENTER();
80 
81 	if (athdiag_procfs_init(scn) != 0) {
82 		HIF_ERROR("athdiag_procfs_init failed");
83 		ret = A_ERROR;
84 	}
85 
86 	scn->athdiag_procfs_inited = true;
87 
88 	HIF_EXIT();
89 	return ret;
90 }
91 
92 /**
93  * hif_nointrs(): disable IRQ
94  * @scn: pointer to struct hif_softc
95  *
96  * This function stops interrupt(s)
97  *
98  * Return: none
99  */
100 void hif_usb_nointrs(struct hif_softc *scn)
101 {
102 
103 }
104 
105 /**
106  * hif_usb_reboot() - called at reboot time to reset WLAN SOC
107  * @nb: pointer to notifier_block registered during register_reboot_notifier
108  * @val: code indicating reboot reason
109  * @v: unused pointer
110  *
111  * Return: int 0 if success else an appropriate error number
112  */
113 static int hif_usb_reboot(struct notifier_block *nb, unsigned long val,
114 				void *v)
115 {
116 	struct hif_usb_softc *sc;
117 
118 	HIF_ENTER();
119 	sc = container_of(nb, struct hif_usb_softc, reboot_notifier);
120 	/* do cold reset */
121 	hif_usb_diag_write_cold_reset(HIF_GET_SOFTC(sc));
122 	HIF_EXIT();
123 	return NOTIFY_DONE;
124 }
125 
126 /**
127  * hif_usb_disable_lpm() - Disable lpm feature of usb2.0
128  * @udev: pointer to usb_device for which LPM is to be disabled
129  *
130  * LPM needs to be disabled to avoid usb2.0 probe timeout
131  *
132  * Return: int 0 if success else an appropriate error number
133  */
134 static int hif_usb_disable_lpm(struct usb_device *udev)
135 {
136 	struct usb_hcd *hcd;
137 	int ret = -EPERM;
138 
139 	HIF_ENTER();
140 
141 	if (!udev || !udev->bus) {
142 		HIF_ERROR("Invalid input parameters");
143 		goto exit;
144 	}
145 
146 	hcd = bus_to_hcd(udev->bus);
147 	if (udev->usb2_hw_lpm_enabled) {
148 		if (hcd->driver->set_usb2_hw_lpm) {
149 			ret = hcd->driver->set_usb2_hw_lpm(hcd, udev, false);
150 			if (!ret) {
151 				udev->usb2_hw_lpm_enabled = false;
152 				udev->usb2_hw_lpm_capable = false;
153 				HIF_TRACE("%s: LPM is disabled", __func__);
154 			} else {
155 				HIF_TRACE("%s: Fail to disable LPM",
156 						__func__);
157 			}
158 		} else {
159 			HIF_TRACE("%s: hcd doesn't support LPM",
160 						__func__);
161 		}
162 	} else {
163 		HIF_TRACE("%s: LPM isn't enabled", __func__);
164 	}
165 exit:
166 	HIF_EXIT();
167 	return ret;
168 }
169 
170 /**
171  * hif_usb_enable_bus() - enable usb bus
172  * @ol_sc: hif_softc struct
173  * @dev: device pointer
174  * @bdev: bus dev pointer
175  * @bid: bus id pointer
176  * @type: enum hif_enable_type such as HIF_ENABLE_TYPE_PROBE
177  *
178  * Return: QDF_STATUS_SUCCESS on success and error QDF status on failure
179  */
180 QDF_STATUS hif_usb_enable_bus(struct hif_softc *scn,
181 			struct device *dev, void *bdev,
182 			const struct hif_bus_id *bid,
183 			enum hif_enable_type type)
184 
185 {
186 	struct usb_interface *interface = (struct usb_interface *)bdev;
187 	struct usb_device_id *id = (struct usb_device_id *)bid;
188 	int ret = 0;
189 	struct hif_usb_softc *sc;
190 	struct usb_device *usbdev = interface_to_usbdev(interface);
191 	int vendor_id, product_id;
192 	struct hif_target_info *tgt_info;
193 	struct hif_opaque_softc *hif_hdl = GET_HIF_OPAQUE_HDL(scn);
194 	u32 hif_type;
195 	u32 target_type;
196 
197 	if (!scn) {
198 		HIF_ERROR("%s: hif_ctx is NULL", __func__);
199 		goto err_usb;
200 	}
201 
202 	sc = HIF_GET_USB_SOFTC(scn);
203 
204 	HIF_INFO("%s hif_softc %pK usbdev %pK interface %pK\n",
205 		__func__,
206 		scn,
207 		usbdev,
208 		interface);
209 
210 	vendor_id = qdf_le16_to_cpu(usbdev->descriptor.idVendor);
211 	product_id = qdf_le16_to_cpu(usbdev->descriptor.idProduct);
212 
213 	HIF_ERROR("%s: con_mode = 0x%x, vendor_id = 0x%x product_id = 0x%x",
214 		__func__, hif_get_conparam(scn), vendor_id, product_id);
215 
216 	sc->pdev = (void *)usbdev;
217 	sc->dev = &usbdev->dev;
218 	sc->devid = id->idProduct;
219 
220 	hif_get_device_type(product_id, 0, &hif_type, &target_type);
221 	tgt_info = hif_get_target_info_handle(hif_hdl);
222 	if (target_type == TARGET_TYPE_QCN7605)
223 		tgt_info->target_type = TARGET_TYPE_QCN7605;
224 
225 	/*
226 	 * For Genoa, skip set_configuration, since it is handled
227 	 * by CNSS driver.
228 	 */
229 	if (target_type != TARGET_TYPE_QCN7605) {
230 		usb_get_dev(usbdev);
231 		if ((usb_control_msg(usbdev, usb_sndctrlpipe(usbdev, 0),
232 				     USB_REQ_SET_CONFIGURATION, 0, 1, 0,
233 				     NULL, 0, HZ)) < 0) {
234 			HIF_ERROR("%s[%d]", __func__, __LINE__);
235 			goto err_usb;
236 		}
237 		usb_set_interface(usbdev, 0, 0);
238 		sc->reboot_notifier.notifier_call = hif_usb_reboot;
239 		register_reboot_notifier(&sc->reboot_notifier);
240 	}
241 
242 	/* disable lpm to avoid usb2.0 probe timeout */
243 	hif_usb_disable_lpm(usbdev);
244 
245 	/* params need to be added - TODO
246 	 * scn->enableuartprint = 1;
247 	 * scn->enablefwlog = 0;
248 	 * scn->max_no_of_peers = 1;
249 	 */
250 
251 	sc->interface = interface;
252 	if (hif_usb_device_init(sc) != QDF_STATUS_SUCCESS) {
253 		HIF_ERROR("ath: %s: hif_usb_device_init failed", __func__);
254 		goto err_reset;
255 	}
256 
257 	if (hif_usb_procfs_init(scn))
258 		goto err_reset;
259 
260 	hif_usb_unload_dev_num = usbdev->devnum;
261 	g_usb_sc = sc;
262 	HIF_EXIT();
263 	return 0;
264 
265 err_reset:
266 	hif_usb_diag_write_cold_reset(scn);
267 	g_usb_sc = NULL;
268 	hif_usb_unload_dev_num = -1;
269 	if (target_type != TARGET_TYPE_QCN7605)
270 		unregister_reboot_notifier(&sc->reboot_notifier);
271 err_usb:
272 	ret = QDF_STATUS_E_FAILURE;
273 	if (target_type != TARGET_TYPE_QCN7605)
274 		usb_put_dev(usbdev);
275 	return ret;
276 }
277 
278 
279 /**
280  * hif_usb_close(): close bus, delete hif_sc
281  * @ol_sc: soft_sc struct
282  *
283  * Return: none
284  */
285 void hif_usb_close(struct hif_softc *scn)
286 {
287 	g_usb_sc = NULL;
288 }
289 
290 /**
291  * hif_usb_disable_bus(): This function disables usb bus
292  * @hif_ctx: pointer to struct hif_softc
293  *
294  * Return: none
295  */
296 void hif_usb_disable_bus(struct hif_softc *hif_ctx)
297 {
298 	struct hif_usb_softc *sc = HIF_GET_USB_SOFTC(hif_ctx);
299 	struct usb_interface *interface = sc->interface;
300 	struct usb_device *udev = interface_to_usbdev(interface);
301 	struct hif_target_info *tgt_info = &hif_ctx->target_info;
302 
303 	HIF_TRACE("%s: trying to remove hif_usb!", __func__);
304 
305 	/* disable lpm to avoid following cold reset will
306 	 * cause xHCI U1/U2 timeout
307 	 */
308 	if (tgt_info->target_type != TARGET_TYPE_QCN7605)
309 		usb_disable_lpm(udev);
310 
311 	/* wait for disable lpm */
312 	set_current_state(TASK_INTERRUPTIBLE);
313 	schedule_timeout(msecs_to_jiffies(DELAY_FOR_TARGET_READY));
314 	set_current_state(TASK_RUNNING);
315 
316 	/* do cold reset */
317 	hif_usb_diag_write_cold_reset(hif_ctx);
318 
319 	if (g_usb_sc->suspend_state)
320 		hif_bus_resume(GET_HIF_OPAQUE_HDL(hif_ctx));
321 
322 	if (tgt_info->target_type != TARGET_TYPE_QCN7605) {
323 		unregister_reboot_notifier(&sc->reboot_notifier);
324 		usb_put_dev(udev);
325 	}
326 
327 	hif_usb_device_deinit(sc);
328 
329 	HIF_TRACE("%s hif_usb removed !!!!!!", __func__);
330 }
331 
332 /**
333  * hif_usb_bus_suspend() - suspend the bus
334  * @hif_ctx: hif_ctx
335  *
336  * This function suspends the bus, but usb doesn't need to suspend.
337  * Therefore just remove all the pending urb transactions
338  *
339  * Return: 0 for success and non-zero for failure
340  */
341 int hif_usb_bus_suspend(struct hif_softc *hif_ctx)
342 {
343 	struct hif_usb_softc *sc = HIF_GET_USB_SOFTC(hif_ctx);
344 	struct HIF_DEVICE_USB *device = HIF_GET_USB_DEVICE(hif_ctx);
345 
346 	HIF_ENTER();
347 	sc->suspend_state = 1;
348 	usb_hif_flush_all(device);
349 	HIF_EXIT();
350 	return 0;
351 }
352 
353 /**
354  * hif_usb_bus_resume() - hif resume API
355  * @hif_ctx: struct hif_opaque_softc
356  *
357  * This function resumes the bus. but usb doesn't need to resume.
358  * Post recv urbs for RX data pipe
359  *
360  * Return: 0 for success and non-zero for failure
361  */
362 int hif_usb_bus_resume(struct hif_softc *hif_ctx)
363 {
364 	struct hif_usb_softc *sc = HIF_GET_USB_SOFTC(hif_ctx);
365 	struct HIF_DEVICE_USB *device = HIF_GET_USB_DEVICE(hif_ctx);
366 
367 	HIF_ENTER();
368 	sc->suspend_state = 0;
369 	usb_hif_start_recv_pipes(device);
370 
371 	HIF_EXIT();
372 	return 0;
373 }
374 
375 /**
376  * hif_usb_bus_reset_resume() - resume the bus after reset
377  * @scn: struct hif_opaque_softc
378  *
379  * This function is called to tell the driver that USB device has been resumed
380  * and it has also been reset. The driver should redo any necessary
381  * initialization. This function resets WLAN SOC.
382  *
383  * Return: int 0 for success, non zero for failure
384  */
385 int hif_usb_bus_reset_resume(struct hif_softc *hif_ctx)
386 {
387 	int ret = 0;
388 
389 	HIF_ENTER();
390 	if (hif_usb_diag_write_cold_reset(hif_ctx) != QDF_STATUS_SUCCESS)
391 		ret = 1;
392 
393 	HIF_EXIT();
394 	return ret;
395 }
396 
397 /**
398  * hif_usb_open()- initialization routine for usb bus
399  * @ol_sc: ol_sc
400  * @bus_type: bus type
401  *
402  * Return: QDF_STATUS_SUCCESS on success and error QDF status on failure
403  */
404 QDF_STATUS hif_usb_open(struct hif_softc *hif_ctx,
405 		enum qdf_bus_type bus_type)
406 {
407 	hif_ctx->bus_type = bus_type;
408 	return QDF_STATUS_SUCCESS;
409 }
410 
411 /**
412  * hif_usb_disable_isr(): disable isr
413  * @hif_ctx: struct hif_softc
414  *
415  * Return: void
416  */
417 void hif_usb_disable_isr(struct hif_softc *hif_ctx)
418 {
419 	/* TODO */
420 }
421 
422 /**
423  * hif_usb_reg_tbl_attach()- attach hif, target register tables
424  * @scn: pointer to ol_softc structure
425  *
426  * Attach host and target register tables based on target_type, target_version
427  *
428  * Return: none
429  */
430 void hif_usb_reg_tbl_attach(struct hif_softc *scn)
431 {
432 	u_int32_t hif_type, target_type;
433 	int32_t ret = 0;
434 	uint32_t chip_id;
435 	QDF_STATUS rv;
436 	struct hif_target_info *tgt_info = &scn->target_info;
437 	struct hif_opaque_softc *hif_hdl = GET_HIF_OPAQUE_HDL(scn);
438 
439 	if (!scn->hostdef && !scn->targetdef) {
440 		switch (tgt_info->target_type) {
441 		case TARGET_TYPE_AR6320:
442 			switch (tgt_info->target_version) {
443 			case AR6320_REV1_VERSION:
444 			case AR6320_REV1_1_VERSION:
445 			case AR6320_REV1_3_VERSION:
446 				hif_type = HIF_TYPE_AR6320;
447 				target_type = TARGET_TYPE_AR6320;
448 				break;
449 			case AR6320_REV2_1_VERSION:
450 			case AR6320_REV3_VERSION:
451 			case QCA9377_REV1_1_VERSION:
452 			case QCA9379_REV1_VERSION:
453 				hif_type = HIF_TYPE_AR6320V2;
454 				target_type = TARGET_TYPE_AR6320V2;
455 				break;
456 			default:
457 				ret = -1;
458 				break;
459 			}
460 			break;
461 		default:
462 			ret = -1;
463 			break;
464 		}
465 
466 		if (ret)
467 			return;
468 
469 		/* assign target register table if we find
470 		 * corresponding type
471 		 */
472 		hif_register_tbl_attach(scn, hif_type);
473 		target_register_tbl_attach(scn, target_type);
474 		/* read the chip revision*/
475 		rv = hif_diag_read_access(hif_hdl,
476 					(CHIP_ID_ADDRESS |
477 					RTC_SOC_BASE_ADDRESS),
478 					&chip_id);
479 		if (rv != QDF_STATUS_SUCCESS) {
480 			HIF_ERROR("%s: get chip id val (%d)", __func__,
481 				rv);
482 		}
483 		tgt_info->target_revision =
484 				CHIP_ID_REVISION_GET(chip_id);
485 	}
486 }
487 
488 /**
489  * hif_usb_get_hw_info()- attach register table for USB
490  * @hif_ctx: pointer to hif_softc structure
491 
492  * This function is used to attach the host and target register tables.
493  * Ideally, we should not attach register tables as a part of this function.
494  * There is scope of cleanup to move register table attach during
495  * initialization for USB bus.
496  *
497  * The reason we are doing register table attach for USB here is that, it relies
498  * on target_info->target_type and target_info->target_version,
499  * which get populated during bmi_firmware_download. "hif_get_fw_info" is the
500  * only initialization related call into HIF there after.
501  *
502  * To fix this, we can move the "get target info, functionality currently in
503  * bmi_firmware_download into hif initialization functions. This change will
504  * affect all buses. Can be taken up as a part of convergence.
505  *
506  * Return: none
507  */
508 void hif_usb_get_hw_info(struct hif_softc *hif_ctx)
509 {
510 	hif_usb_reg_tbl_attach(hif_ctx);
511 }
512 
513 #if defined(CONFIG_PLD_USB_CNSS) && !defined(CONFIG_BYPASS_QMI)
514 /**
515  * hif_bus_configure() - configure the bus
516  * @scn: pointer to the hif context.
517  *
518  * return: 0 for success. nonzero for failure.
519  */
520 int hif_usb_bus_configure(struct hif_softc *scn)
521 {
522 	struct pld_wlan_enable_cfg cfg;
523 	enum pld_driver_mode mode;
524 	uint32_t con_mode = hif_get_conparam(scn);
525 
526 	if (QDF_GLOBAL_FTM_MODE == con_mode)
527 		mode = PLD_FTM;
528 	else if (QDF_GLOBAL_COLDBOOT_CALIB_MODE == con_mode)
529 		mode = PLD_COLDBOOT_CALIBRATION;
530 	else if (QDF_IS_EPPING_ENABLED(con_mode))
531 		mode = PLD_EPPING;
532 	else
533 		mode = PLD_MISSION;
534 
535 	return pld_wlan_enable(scn->qdf_dev->dev, &cfg, mode);
536 }
537 #else
538 /**
539  * hif_bus_configure() - configure the bus
540  * @scn: pointer to the hif context.
541  *
542  * return: 0 for success. nonzero for failure.
543  */
544 int hif_usb_bus_configure(struct hif_softc *scn)
545 {
546 	return 0;
547 }
548 #endif
549 
550 /**
551  * hif_usb_irq_enable() - hif_usb_irq_enable
552  * @scn: hif_softc
553  * @ce_id: ce_id
554  *
555  * Return: void
556  */
557 void hif_usb_irq_enable(struct hif_softc *scn, int ce_id)
558 {
559 }
560 
561 /**
562  * hif_usb_irq_disable() - hif_usb_irq_disable
563  * @scn: hif_softc
564  * @ce_id: ce_id
565  *
566  * Return: void
567  */
568 void hif_usb_irq_disable(struct hif_softc *scn, int ce_id)
569 {
570 }
571 
572 /**
573  * hif_usb_shutdown_bus_device() - This function shuts down the device
574  * @scn: hif opaque pointer
575  *
576  * Return: void
577  */
578 void hif_usb_shutdown_bus_device(struct hif_softc *scn)
579 {
580 }
581 
582 /**
583  * hif_trigger_dump() - trigger various dump cmd
584  * @scn: struct hif_opaque_softc
585  * @cmd_id: dump command id
586  * @start: start/stop dump
587  *
588  * Return: None
589  */
590 void hif_trigger_dump(struct hif_opaque_softc *scn, uint8_t cmd_id, bool start)
591 {
592 }
593 
594 /**
595  * hif_wlan_disable() - call the platform driver to disable wlan
596  * @scn: scn
597  *
598  * Return: void
599  */
600 void hif_wlan_disable(struct hif_softc *scn)
601 {
602 }
603 
604 /**
605  * hif_fw_assert_ramdump_pattern() - handle firmware assert with ramdump pattern
606  * @sc: pointer to hif_usb_softc structure
607  *
608  * Return: void
609  */
610 
611 void hif_fw_assert_ramdump_pattern(struct hif_usb_softc *sc)
612 {
613 	uint32_t *reg, pattern, i = 0;
614 	uint32_t len;
615 	uint8_t *data;
616 	uint8_t *ram_ptr = NULL;
617 	char *fw_ram_seg_name[FW_RAM_SEG_CNT] = {"DRAM", "IRAM", "AXI"};
618 	size_t fw_ram_reg_size[FW_RAM_SEG_CNT] = {
619 				  FW_RAMDUMP_DRAMSIZE,
620 				  FW_RAMDUMP_IRAMSIZE,
621 				  FW_RAMDUMP_AXISIZE };
622 
623 	data = sc->fw_data;
624 	len = sc->fw_data_len;
625 	pattern = *((uint32_t *) data);
626 
627 	qdf_assert(sc->ramdump_index < FW_RAM_SEG_CNT);
628 	i = sc->ramdump_index;
629 	reg = (uint32_t *) (data + 4);
630 	if (sc->fw_ram_dumping == 0) {
631 		sc->fw_ram_dumping = 1;
632 		HIF_ERROR("Firmware %s dump:\n", fw_ram_seg_name[i]);
633 		sc->ramdump[i] =
634 			qdf_mem_malloc(sizeof(struct fw_ramdump) +
635 					fw_ram_reg_size[i]);
636 		if (!sc->ramdump[i])
637 			QDF_BUG(0);
638 
639 		(sc->ramdump[i])->mem = (uint8_t *) (sc->ramdump[i] + 1);
640 		fw_ram_seg_addr[i] = (sc->ramdump[i])->mem;
641 		HIF_ERROR("FW %s start addr = %#08x\n",
642 			fw_ram_seg_name[i], *reg);
643 		HIF_ERROR("Memory addr for %s = %pK\n",
644 			fw_ram_seg_name[i],
645 			(sc->ramdump[i])->mem);
646 		(sc->ramdump[i])->start_addr = *reg;
647 		(sc->ramdump[i])->length = 0;
648 	}
649 	reg++;
650 	ram_ptr = (sc->ramdump[i])->mem + (sc->ramdump[i])->length;
651 	(sc->ramdump[i])->length += (len - 8);
652 	if (sc->ramdump[i]->length <= fw_ram_reg_size[i]) {
653 		qdf_mem_copy(ram_ptr, (uint8_t *) reg, len - 8);
654 	} else {
655 		HIF_ERROR("memory copy overlap\n");
656 		QDF_BUG(0);
657 	}
658 
659 	if (pattern == FW_RAMDUMP_END_PATTERN) {
660 		HIF_ERROR("%s memory size = %d\n", fw_ram_seg_name[i],
661 				(sc->ramdump[i])->length);
662 		if (i == (FW_RAM_SEG_CNT - 1))
663 			QDF_BUG(0);
664 
665 		sc->ramdump_index++;
666 		sc->fw_ram_dumping = 0;
667 	}
668 }
669 
670 /**
671  * hif_usb_ramdump_handler(): dump bus debug registers
672  * @scn: struct hif_opaque_softc
673  *
674  * This function is to receive information of firmware crash dump, and
675  * save it in host memory. It consists of 5 parts: registers, call stack,
676  * DRAM dump, IRAM dump, and AXI dump, and they are reported to host in order.
677  *
678  * registers: wrapped in a USB packet by starting as FW_ASSERT_PATTERN and
679  *            60 registers.
680  * call stack: wrapped in multiple USB packets, and each of them starts as
681  *             FW_REG_PATTERN and contains multiple double-words. The tail
682  *             of the last packet is FW_REG_END_PATTERN.
683  * DRAM dump: wrapped in multiple USB pakcets, and each of them start as
684  *            FW_RAMDUMP_PATTERN and contains multiple double-wors. The tail
685  *            of the last packet is FW_RAMDUMP_END_PATTERN;
686  * IRAM dump and AXI dump are with the same format as DRAM dump.
687  *
688  * Return: 0 for success or error code
689  */
690 
691 void hif_usb_ramdump_handler(struct hif_opaque_softc *scn)
692 {
693 	uint32_t *reg, pattern, i, start_addr = 0;
694 	uint32_t len;
695 	uint8_t *data;
696 	uint8_t str_buf[128];
697 	uint32_t remaining;
698 	struct hif_usb_softc *sc = HIF_GET_USB_SOFTC(scn);
699 	struct hif_softc *hif_ctx = HIF_GET_SOFTC(scn);
700 	struct hif_target_info *tgt_info = &hif_ctx->target_info;
701 
702 	data = sc->fw_data;
703 	len = sc->fw_data_len;
704 	pattern = *((uint32_t *) data);
705 
706 	if (pattern == FW_ASSERT_PATTERN) {
707 		HIF_ERROR("Firmware crash detected...\n");
708 		HIF_ERROR("target_type: %d.target_version %d. target_revision%d.",
709 			tgt_info->target_type,
710 			tgt_info->target_version,
711 			tgt_info->target_revision);
712 
713 		reg = (uint32_t *) (data + 4);
714 		print_hex_dump(KERN_DEBUG, " ", DUMP_PREFIX_OFFSET, 16, 4, reg,
715 				min_t(uint32_t, len - 4, FW_REG_DUMP_CNT * 4),
716 				false);
717 		sc->fw_ram_dumping = 0;
718 
719 	} else if (pattern == FW_REG_PATTERN) {
720 		reg = (uint32_t *) (data + 4);
721 		start_addr = *reg++;
722 		if (sc->fw_ram_dumping == 0) {
723 			qdf_nofl_err("Firmware stack dump:");
724 			sc->fw_ram_dumping = 1;
725 			fw_stack_addr = start_addr;
726 		}
727 		remaining = len - 8;
728 		/* len is in byte, but it's printed in double-word. */
729 		for (i = 0; i < (len - 8); i += 16) {
730 			if ((*reg == FW_REG_END_PATTERN) && (i == len - 12)) {
731 				sc->fw_ram_dumping = 0;
732 				qdf_nofl_err("Stack start address = %#08x",
733 					     fw_stack_addr);
734 				break;
735 			}
736 			hex_dump_to_buffer(reg, remaining, 16, 4, str_buf,
737 						sizeof(str_buf), false);
738 			qdf_nofl_err("%#08x: %s", start_addr + i, str_buf);
739 			remaining -= 16;
740 			reg += 4;
741 		}
742 	} else if ((!sc->enable_self_recovery) &&
743 			((pattern & FW_RAMDUMP_PATTERN_MASK) ==
744 						FW_RAMDUMP_PATTERN)) {
745 		hif_fw_assert_ramdump_pattern(sc);
746 	}
747 }
748 
749 #ifndef QCA_WIFI_3_0
750 /**
751  * hif_check_fw_reg(): hif_check_fw_reg
752  * @scn: scn
753  * @state:
754  *
755  * Return: int
756  */
757 int hif_check_fw_reg(struct hif_opaque_softc *scn)
758 {
759 	return 0;
760 }
761 #endif
762 
763 /**
764  * hif_usb_needs_bmi() - return true if the soc needs bmi through the driver
765  * @scn: hif context
766  *
767  * Return: true if soc needs driver bmi otherwise false
768  */
769 bool hif_usb_needs_bmi(struct hif_softc *scn)
770 {
771 	struct hif_target_info *tgt_info = &scn->target_info;
772 
773 	/* BMI is not supported in Genoa */
774 	if (tgt_info->target_type == TARGET_TYPE_QCN7605)
775 		return false;
776 
777 	return true;
778 }
779