xref: /openbmc/linux/drivers/hid/intel-ish-hid/ishtp-fw-loader.c (revision 7b7fd0ac7dc1ffcaf24d9bca0f051b0168e43cd4)
191b22810SRushikesh S Kadam // SPDX-License-Identifier: GPL-2.0
291b22810SRushikesh S Kadam /*
391b22810SRushikesh S Kadam  * ISH-TP client driver for ISH firmware loading
491b22810SRushikesh S Kadam  *
591b22810SRushikesh S Kadam  * Copyright (c) 2019, Intel Corporation.
691b22810SRushikesh S Kadam  */
791b22810SRushikesh S Kadam 
891b22810SRushikesh S Kadam #include <linux/firmware.h>
991b22810SRushikesh S Kadam #include <linux/module.h>
1091b22810SRushikesh S Kadam #include <linux/pci.h>
1191b22810SRushikesh S Kadam #include <linux/intel-ish-client-if.h>
1291b22810SRushikesh S Kadam #include <linux/property.h>
1391b22810SRushikesh S Kadam #include <asm/cacheflush.h>
1491b22810SRushikesh S Kadam 
1591b22810SRushikesh S Kadam /* Number of times we attempt to load the firmware before giving up */
1691b22810SRushikesh S Kadam #define MAX_LOAD_ATTEMPTS			3
1791b22810SRushikesh S Kadam 
1891b22810SRushikesh S Kadam /* ISH TX/RX ring buffer pool size */
1991b22810SRushikesh S Kadam #define LOADER_CL_RX_RING_SIZE			1
2091b22810SRushikesh S Kadam #define LOADER_CL_TX_RING_SIZE			1
2191b22810SRushikesh S Kadam 
2291b22810SRushikesh S Kadam /*
2391b22810SRushikesh S Kadam  * ISH Shim firmware loader reserves 4 Kb buffer in SRAM. The buffer is
2491b22810SRushikesh S Kadam  * used to temporarily hold the data transferred from host to Shim
2591b22810SRushikesh S Kadam  * firmware loader. Reason for the odd size of 3968 bytes? Each IPC
2691b22810SRushikesh S Kadam  * transfer is 128 bytes (= 4 bytes header + 124 bytes payload). So the
2791b22810SRushikesh S Kadam  * 4 Kb buffer can hold maximum of 32 IPC transfers, which means we can
2891b22810SRushikesh S Kadam  * have a max payload of 3968 bytes (= 32 x 124 payload).
2991b22810SRushikesh S Kadam  */
3091b22810SRushikesh S Kadam #define LOADER_SHIM_IPC_BUF_SIZE		3968
3191b22810SRushikesh S Kadam 
3291b22810SRushikesh S Kadam /**
3391b22810SRushikesh S Kadam  * enum ish_loader_commands -	ISH loader host commands.
34509405cdSLee Jones  * @LOADER_CMD_XFER_QUERY:	Query the Shim firmware loader for
3591b22810SRushikesh S Kadam  *				capabilities
36509405cdSLee Jones  * @LOADER_CMD_XFER_FRAGMENT:	Transfer one firmware image fragment at a
3791b22810SRushikesh S Kadam  *				time. The command may be executed
3891b22810SRushikesh S Kadam  *				multiple times until the entire firmware
3991b22810SRushikesh S Kadam  *				image is downloaded to SRAM.
40509405cdSLee Jones  * @LOADER_CMD_START:		Start executing the main firmware.
4191b22810SRushikesh S Kadam  */
4291b22810SRushikesh S Kadam enum ish_loader_commands {
4391b22810SRushikesh S Kadam 	LOADER_CMD_XFER_QUERY = 0,
4491b22810SRushikesh S Kadam 	LOADER_CMD_XFER_FRAGMENT,
4591b22810SRushikesh S Kadam 	LOADER_CMD_START,
4691b22810SRushikesh S Kadam };
4791b22810SRushikesh S Kadam 
4891b22810SRushikesh S Kadam /* Command bit mask */
4991b22810SRushikesh S Kadam #define	CMD_MASK				GENMASK(6, 0)
5091b22810SRushikesh S Kadam #define	IS_RESPONSE				BIT(7)
5191b22810SRushikesh S Kadam 
5291b22810SRushikesh S Kadam /*
5391b22810SRushikesh S Kadam  * ISH firmware max delay for one transmit failure is 1 Hz,
5491b22810SRushikesh S Kadam  * and firmware will retry 2 times, so 3 Hz is used for timeout.
5591b22810SRushikesh S Kadam  */
5691b22810SRushikesh S Kadam #define ISHTP_SEND_TIMEOUT			(3 * HZ)
5791b22810SRushikesh S Kadam 
5891b22810SRushikesh S Kadam /*
5991b22810SRushikesh S Kadam  * Loader transfer modes:
6091b22810SRushikesh S Kadam  *
6191b22810SRushikesh S Kadam  * LOADER_XFER_MODE_ISHTP mode uses the existing ISH-TP mechanism to
6291b22810SRushikesh S Kadam  * transfer data. This may use IPC or DMA if supported in firmware.
6391b22810SRushikesh S Kadam  * The buffer size is limited to 4 Kb by the IPC/ISH-TP protocol for
6491b22810SRushikesh S Kadam  * both IPC & DMA (legacy).
6591b22810SRushikesh S Kadam  *
6691b22810SRushikesh S Kadam  * LOADER_XFER_MODE_DIRECT_DMA - firmware loading is a bit different
6791b22810SRushikesh S Kadam  * from the sensor data streaming. Here we download a large (300+ Kb)
6891b22810SRushikesh S Kadam  * image directly to ISH SRAM memory. There is limited benefit of
6991b22810SRushikesh S Kadam  * DMA'ing 300 Kb image in 4 Kb chucks limit. Hence, we introduce
7091b22810SRushikesh S Kadam  * this "direct dma" mode, where we do not use ISH-TP for DMA, but
7191b22810SRushikesh S Kadam  * instead manage the DMA directly in kernel driver and Shim firmware
7291b22810SRushikesh S Kadam  * loader (allocate buffer, break in chucks and transfer). This allows
7391b22810SRushikesh S Kadam  * to overcome 4 Kb limit, and optimize the data flow path in firmware.
7491b22810SRushikesh S Kadam  */
7591b22810SRushikesh S Kadam #define LOADER_XFER_MODE_DIRECT_DMA		BIT(0)
7691b22810SRushikesh S Kadam #define LOADER_XFER_MODE_ISHTP			BIT(1)
7791b22810SRushikesh S Kadam 
7891b22810SRushikesh S Kadam /* ISH Transport Loader client unique GUID */
79bf9167a8SArnd Bergmann static const struct ishtp_device_id loader_ishtp_id_table[] = {
80bf9167a8SArnd Bergmann 	{ .guid = GUID_INIT(0xc804d06a, 0x55bd, 0x4ea7,
81bf9167a8SArnd Bergmann 		  0xad, 0xed, 0x1e, 0x31, 0x22, 0x8c, 0x76, 0xdc) },
82bf9167a8SArnd Bergmann 	{ }
83bf9167a8SArnd Bergmann };
84bf9167a8SArnd Bergmann MODULE_DEVICE_TABLE(ishtp, loader_ishtp_id_table);
8591b22810SRushikesh S Kadam 
8691b22810SRushikesh S Kadam #define FILENAME_SIZE				256
8791b22810SRushikesh S Kadam 
8891b22810SRushikesh S Kadam /*
8991b22810SRushikesh S Kadam  * The firmware loading latency will be minimum if we can DMA the
9091b22810SRushikesh S Kadam  * entire ISH firmware image in one go. This requires that we allocate
9191b22810SRushikesh S Kadam  * a large DMA buffer in kernel, which could be problematic on some
9291b22810SRushikesh S Kadam  * platforms. So here we limit the DMA buffer size via a module_param.
9391b22810SRushikesh S Kadam  * We default to 4 pages, but a customer can set it to higher limit if
9491b22810SRushikesh S Kadam  * deemed appropriate for his platform.
9591b22810SRushikesh S Kadam  */
9691b22810SRushikesh S Kadam static int dma_buf_size_limit = 4 * PAGE_SIZE;
9791b22810SRushikesh S Kadam 
9891b22810SRushikesh S Kadam /**
9991b22810SRushikesh S Kadam  * struct loader_msg_hdr - Header for ISH Loader commands.
10091b22810SRushikesh S Kadam  * @command:		LOADER_CMD* commands. Bit 7 is the response.
101509405cdSLee Jones  * @reserved:		Reserved space
10291b22810SRushikesh S Kadam  * @status:		Command response status. Non 0, is error
10391b22810SRushikesh S Kadam  *			condition.
10491b22810SRushikesh S Kadam  *
10591b22810SRushikesh S Kadam  * This structure is used as header for every command/data sent/received
10691b22810SRushikesh S Kadam  * between Host driver and ISH Shim firmware loader.
10791b22810SRushikesh S Kadam  */
10891b22810SRushikesh S Kadam struct loader_msg_hdr {
10991b22810SRushikesh S Kadam 	u8 command;
11091b22810SRushikesh S Kadam 	u8 reserved[2];
11191b22810SRushikesh S Kadam 	u8 status;
11291b22810SRushikesh S Kadam } __packed;
11391b22810SRushikesh S Kadam 
11491b22810SRushikesh S Kadam struct loader_xfer_query {
11591b22810SRushikesh S Kadam 	struct loader_msg_hdr hdr;
11691b22810SRushikesh S Kadam 	u32 image_size;
11791b22810SRushikesh S Kadam } __packed;
11891b22810SRushikesh S Kadam 
11991b22810SRushikesh S Kadam struct ish_fw_version {
12091b22810SRushikesh S Kadam 	u16 major;
12191b22810SRushikesh S Kadam 	u16 minor;
12291b22810SRushikesh S Kadam 	u16 hotfix;
12391b22810SRushikesh S Kadam 	u16 build;
12491b22810SRushikesh S Kadam } __packed;
12591b22810SRushikesh S Kadam 
12691b22810SRushikesh S Kadam union loader_version {
12791b22810SRushikesh S Kadam 	u32 value;
12891b22810SRushikesh S Kadam 	struct {
12991b22810SRushikesh S Kadam 		u8 major;
13091b22810SRushikesh S Kadam 		u8 minor;
13191b22810SRushikesh S Kadam 		u8 hotfix;
13291b22810SRushikesh S Kadam 		u8 build;
13391b22810SRushikesh S Kadam 	};
13491b22810SRushikesh S Kadam } __packed;
13591b22810SRushikesh S Kadam 
13691b22810SRushikesh S Kadam struct loader_capability {
13791b22810SRushikesh S Kadam 	u32 max_fw_image_size;
13891b22810SRushikesh S Kadam 	u32 xfer_mode;
13991b22810SRushikesh S Kadam 	u32 max_dma_buf_size; /* only for dma mode, multiples of cacheline */
14091b22810SRushikesh S Kadam } __packed;
14191b22810SRushikesh S Kadam 
14291b22810SRushikesh S Kadam struct shim_fw_info {
14391b22810SRushikesh S Kadam 	struct ish_fw_version ish_fw_version;
14491b22810SRushikesh S Kadam 	u32 protocol_version;
14591b22810SRushikesh S Kadam 	union loader_version ldr_version;
14691b22810SRushikesh S Kadam 	struct loader_capability ldr_capability;
14791b22810SRushikesh S Kadam } __packed;
14891b22810SRushikesh S Kadam 
14991b22810SRushikesh S Kadam struct loader_xfer_query_response {
15091b22810SRushikesh S Kadam 	struct loader_msg_hdr hdr;
15191b22810SRushikesh S Kadam 	struct shim_fw_info fw_info;
15291b22810SRushikesh S Kadam } __packed;
15391b22810SRushikesh S Kadam 
15491b22810SRushikesh S Kadam struct loader_xfer_fragment {
15591b22810SRushikesh S Kadam 	struct loader_msg_hdr hdr;
15691b22810SRushikesh S Kadam 	u32 xfer_mode;
15791b22810SRushikesh S Kadam 	u32 offset;
15891b22810SRushikesh S Kadam 	u32 size;
15991b22810SRushikesh S Kadam 	u32 is_last;
16091b22810SRushikesh S Kadam } __packed;
16191b22810SRushikesh S Kadam 
16291b22810SRushikesh S Kadam struct loader_xfer_ipc_fragment {
16391b22810SRushikesh S Kadam 	struct loader_xfer_fragment fragment;
16491b22810SRushikesh S Kadam 	u8 data[] ____cacheline_aligned; /* variable length payload here */
16591b22810SRushikesh S Kadam } __packed;
16691b22810SRushikesh S Kadam 
16791b22810SRushikesh S Kadam struct loader_xfer_dma_fragment {
16891b22810SRushikesh S Kadam 	struct loader_xfer_fragment fragment;
16991b22810SRushikesh S Kadam 	u64 ddr_phys_addr;
17091b22810SRushikesh S Kadam } __packed;
17191b22810SRushikesh S Kadam 
17291b22810SRushikesh S Kadam struct loader_start {
17391b22810SRushikesh S Kadam 	struct loader_msg_hdr hdr;
17491b22810SRushikesh S Kadam } __packed;
17591b22810SRushikesh S Kadam 
17691b22810SRushikesh S Kadam /**
17791b22810SRushikesh S Kadam  * struct response_info - Encapsulate firmware response related
17891b22810SRushikesh S Kadam  *			information for passing between function
17991b22810SRushikesh S Kadam  *			loader_cl_send() and process_recv() callback.
180509405cdSLee Jones  * @data:		Copy the data received from firmware here.
181509405cdSLee Jones  * @max_size:		Max size allocated for the @data buffer. If the
18291b22810SRushikesh S Kadam  *			received data exceeds this value, we log an
18391b22810SRushikesh S Kadam  *			error.
184509405cdSLee Jones  * @size:		Actual size of data received from firmware.
185509405cdSLee Jones  * @error:		Returns 0 for success, negative error code for a
18691b22810SRushikesh S Kadam  *			failure in function process_recv().
187509405cdSLee Jones  * @received:		Set to true on receiving a valid firmware
18891b22810SRushikesh S Kadam  *			response to host command
189509405cdSLee Jones  * @wait_queue:		Wait queue for Host firmware loading where the
19091b22810SRushikesh S Kadam  *			client sends message to ISH firmware and waits
19191b22810SRushikesh S Kadam  *			for response
19291b22810SRushikesh S Kadam  */
19391b22810SRushikesh S Kadam struct response_info {
19491b22810SRushikesh S Kadam 	void *data;
19591b22810SRushikesh S Kadam 	size_t max_size;
19691b22810SRushikesh S Kadam 	size_t size;
19791b22810SRushikesh S Kadam 	int error;
19891b22810SRushikesh S Kadam 	bool received;
19991b22810SRushikesh S Kadam 	wait_queue_head_t wait_queue;
20091b22810SRushikesh S Kadam };
20191b22810SRushikesh S Kadam 
202509405cdSLee Jones /*
20391b22810SRushikesh S Kadam  * struct ishtp_cl_data - Encapsulate per ISH-TP Client Data.
20491b22810SRushikesh S Kadam  * @work_ishtp_reset:	Work queue for reset handling.
20591b22810SRushikesh S Kadam  * @work_fw_load:	Work queue for host firmware loading.
206509405cdSLee Jones  * @flag_retry:		Flag for indicating host firmware loading should
20791b22810SRushikesh S Kadam  *			be retried.
208509405cdSLee Jones  * @retry_count:	Count the number of retries.
20991b22810SRushikesh S Kadam  *
21091b22810SRushikesh S Kadam  * This structure is used to store data per client.
21191b22810SRushikesh S Kadam  */
21291b22810SRushikesh S Kadam struct ishtp_cl_data {
21391b22810SRushikesh S Kadam 	struct ishtp_cl *loader_ishtp_cl;
21491b22810SRushikesh S Kadam 	struct ishtp_cl_device *cl_device;
21591b22810SRushikesh S Kadam 
21691b22810SRushikesh S Kadam 	/*
21791b22810SRushikesh S Kadam 	 * Used for passing firmware response information between
21891b22810SRushikesh S Kadam 	 * loader_cl_send() and process_recv() callback.
21991b22810SRushikesh S Kadam 	 */
22091b22810SRushikesh S Kadam 	struct response_info response;
22191b22810SRushikesh S Kadam 
22291b22810SRushikesh S Kadam 	struct work_struct work_ishtp_reset;
22391b22810SRushikesh S Kadam 	struct work_struct work_fw_load;
22491b22810SRushikesh S Kadam 
22591b22810SRushikesh S Kadam 	/*
22691b22810SRushikesh S Kadam 	 * In certain failure scenrios, it makes sense to reset the ISH
22791b22810SRushikesh S Kadam 	 * subsystem and retry Host firmware loading (e.g. bad message
22891b22810SRushikesh S Kadam 	 * packet, ENOMEM, etc.). On the other hand, failures due to
22991b22810SRushikesh S Kadam 	 * protocol mismatch, etc., are not recoverable. We do not
23091b22810SRushikesh S Kadam 	 * retry them.
23191b22810SRushikesh S Kadam 	 *
23291b22810SRushikesh S Kadam 	 * If set, the flag indicates that we should re-try the
23391b22810SRushikesh S Kadam 	 * particular failure.
23491b22810SRushikesh S Kadam 	 */
23591b22810SRushikesh S Kadam 	bool flag_retry;
23691b22810SRushikesh S Kadam 	int retry_count;
23791b22810SRushikesh S Kadam };
23891b22810SRushikesh S Kadam 
23991b22810SRushikesh S Kadam #define IPC_FRAGMENT_DATA_PREAMBLE				\
24091b22810SRushikesh S Kadam 	offsetof(struct loader_xfer_ipc_fragment, data)
24191b22810SRushikesh S Kadam 
24291b22810SRushikesh S Kadam #define cl_data_to_dev(client_data) ishtp_device((client_data)->cl_device)
24391b22810SRushikesh S Kadam 
24491b22810SRushikesh S Kadam /**
24591b22810SRushikesh S Kadam  * get_firmware_variant() - Gets the filename of firmware image to be
24691b22810SRushikesh S Kadam  *			loaded based on platform variant.
247509405cdSLee Jones  * @client_data:	Client data instance.
248509405cdSLee Jones  * @filename:		Returns firmware filename.
24991b22810SRushikesh S Kadam  *
25091b22810SRushikesh S Kadam  * Queries the firmware-name device property string.
25191b22810SRushikesh S Kadam  *
25291b22810SRushikesh S Kadam  * Return: 0 for success, negative error code for failure.
25391b22810SRushikesh S Kadam  */
get_firmware_variant(struct ishtp_cl_data * client_data,char * filename)25491b22810SRushikesh S Kadam static int get_firmware_variant(struct ishtp_cl_data *client_data,
25591b22810SRushikesh S Kadam 				char *filename)
25691b22810SRushikesh S Kadam {
25791b22810SRushikesh S Kadam 	int rv;
25891b22810SRushikesh S Kadam 	const char *val;
25991b22810SRushikesh S Kadam 	struct device *devc = ishtp_get_pci_device(client_data->cl_device);
26091b22810SRushikesh S Kadam 
26191b22810SRushikesh S Kadam 	rv = device_property_read_string(devc, "firmware-name", &val);
26291b22810SRushikesh S Kadam 	if (rv < 0) {
26391b22810SRushikesh S Kadam 		dev_err(devc,
26491b22810SRushikesh S Kadam 			"Error: ISH firmware-name device property required\n");
26591b22810SRushikesh S Kadam 		return rv;
26691b22810SRushikesh S Kadam 	}
26791b22810SRushikesh S Kadam 	return snprintf(filename, FILENAME_SIZE, "intel/%s", val);
26891b22810SRushikesh S Kadam }
26991b22810SRushikesh S Kadam 
27091b22810SRushikesh S Kadam /**
27198b6b62cSYang Li  * loader_cl_send() - Send message from host to firmware
27298b6b62cSYang Li  *
27391b22810SRushikesh S Kadam  * @client_data:	Client data instance
274509405cdSLee Jones  * @out_msg:		Message buffer to be sent to firmware
275509405cdSLee Jones  * @out_size:		Size of out going message
276509405cdSLee Jones  * @in_msg:		Message buffer where the incoming data copied.
27791b22810SRushikesh S Kadam  *			This buffer is allocated by calling
278509405cdSLee Jones  * @in_size:		Max size of incoming message
27991b22810SRushikesh S Kadam  *
28091b22810SRushikesh S Kadam  * Return: Number of bytes copied in the in_msg on success, negative
28191b22810SRushikesh S Kadam  * error code on failure.
28291b22810SRushikesh S Kadam  */
loader_cl_send(struct ishtp_cl_data * client_data,u8 * out_msg,size_t out_size,u8 * in_msg,size_t in_size)28391b22810SRushikesh S Kadam static int loader_cl_send(struct ishtp_cl_data *client_data,
28491b22810SRushikesh S Kadam 			  u8 *out_msg, size_t out_size,
28591b22810SRushikesh S Kadam 			  u8 *in_msg, size_t in_size)
28691b22810SRushikesh S Kadam {
28791b22810SRushikesh S Kadam 	int rv;
28891b22810SRushikesh S Kadam 	struct loader_msg_hdr *out_hdr = (struct loader_msg_hdr *)out_msg;
28991b22810SRushikesh S Kadam 	struct ishtp_cl *loader_ishtp_cl = client_data->loader_ishtp_cl;
29091b22810SRushikesh S Kadam 
29191b22810SRushikesh S Kadam 	dev_dbg(cl_data_to_dev(client_data),
29291b22810SRushikesh S Kadam 		"%s: command=%02lx is_response=%u status=%02x\n",
29391b22810SRushikesh S Kadam 		__func__,
29491b22810SRushikesh S Kadam 		out_hdr->command & CMD_MASK,
29591b22810SRushikesh S Kadam 		out_hdr->command & IS_RESPONSE ? 1 : 0,
29691b22810SRushikesh S Kadam 		out_hdr->status);
29791b22810SRushikesh S Kadam 
29891b22810SRushikesh S Kadam 	/* Setup in coming buffer & size */
29991b22810SRushikesh S Kadam 	client_data->response.data = in_msg;
30091b22810SRushikesh S Kadam 	client_data->response.max_size = in_size;
30191b22810SRushikesh S Kadam 	client_data->response.error = 0;
30291b22810SRushikesh S Kadam 	client_data->response.received = false;
30391b22810SRushikesh S Kadam 
30491b22810SRushikesh S Kadam 	rv = ishtp_cl_send(loader_ishtp_cl, out_msg, out_size);
30591b22810SRushikesh S Kadam 	if (rv < 0) {
30691b22810SRushikesh S Kadam 		dev_err(cl_data_to_dev(client_data),
30791b22810SRushikesh S Kadam 			"ishtp_cl_send error %d\n", rv);
30891b22810SRushikesh S Kadam 		return rv;
30991b22810SRushikesh S Kadam 	}
31091b22810SRushikesh S Kadam 
31191b22810SRushikesh S Kadam 	wait_event_interruptible_timeout(client_data->response.wait_queue,
31291b22810SRushikesh S Kadam 					 client_data->response.received,
31391b22810SRushikesh S Kadam 					 ISHTP_SEND_TIMEOUT);
31491b22810SRushikesh S Kadam 	if (!client_data->response.received) {
31591b22810SRushikesh S Kadam 		dev_err(cl_data_to_dev(client_data),
31691b22810SRushikesh S Kadam 			"Timed out for response to command=%02lx",
31791b22810SRushikesh S Kadam 			out_hdr->command & CMD_MASK);
31891b22810SRushikesh S Kadam 		return -ETIMEDOUT;
31991b22810SRushikesh S Kadam 	}
32091b22810SRushikesh S Kadam 
32191b22810SRushikesh S Kadam 	if (client_data->response.error < 0)
32291b22810SRushikesh S Kadam 		return client_data->response.error;
32391b22810SRushikesh S Kadam 
32491b22810SRushikesh S Kadam 	return client_data->response.size;
32591b22810SRushikesh S Kadam }
32691b22810SRushikesh S Kadam 
32791b22810SRushikesh S Kadam /**
32891b22810SRushikesh S Kadam  * process_recv() -	Receive and parse incoming packet
32991b22810SRushikesh S Kadam  * @loader_ishtp_cl:	Client instance to get stats
33091b22810SRushikesh S Kadam  * @rb_in_proc:		ISH received message buffer
33191b22810SRushikesh S Kadam  *
33291b22810SRushikesh S Kadam  * Parse the incoming packet. If it is a response packet then it will
33391b22810SRushikesh S Kadam  * update received and wake up the caller waiting to for the response.
33491b22810SRushikesh S Kadam  */
process_recv(struct ishtp_cl * loader_ishtp_cl,struct ishtp_cl_rb * rb_in_proc)33591b22810SRushikesh S Kadam static void process_recv(struct ishtp_cl *loader_ishtp_cl,
33691b22810SRushikesh S Kadam 			 struct ishtp_cl_rb *rb_in_proc)
33791b22810SRushikesh S Kadam {
33891b22810SRushikesh S Kadam 	struct loader_msg_hdr *hdr;
33991b22810SRushikesh S Kadam 	size_t data_len = rb_in_proc->buf_idx;
34091b22810SRushikesh S Kadam 	struct ishtp_cl_data *client_data =
34191b22810SRushikesh S Kadam 		ishtp_get_client_data(loader_ishtp_cl);
34291b22810SRushikesh S Kadam 
34391b22810SRushikesh S Kadam 	/* Sanity check */
34491b22810SRushikesh S Kadam 	if (!client_data->response.data) {
34591b22810SRushikesh S Kadam 		dev_err(cl_data_to_dev(client_data),
34691b22810SRushikesh S Kadam 			"Receiving buffer is null. Should be allocated by calling function\n");
34791b22810SRushikesh S Kadam 		client_data->response.error = -EINVAL;
34891b22810SRushikesh S Kadam 		goto end;
34991b22810SRushikesh S Kadam 	}
35091b22810SRushikesh S Kadam 
35191b22810SRushikesh S Kadam 	if (client_data->response.received) {
35291b22810SRushikesh S Kadam 		dev_err(cl_data_to_dev(client_data),
35391b22810SRushikesh S Kadam 			"Previous firmware message not yet processed\n");
35491b22810SRushikesh S Kadam 		client_data->response.error = -EINVAL;
35591b22810SRushikesh S Kadam 		goto end;
35691b22810SRushikesh S Kadam 	}
35791b22810SRushikesh S Kadam 	/*
35891b22810SRushikesh S Kadam 	 * All firmware messages have a header. Check buffer size
35991b22810SRushikesh S Kadam 	 * before accessing elements inside.
36091b22810SRushikesh S Kadam 	 */
36191b22810SRushikesh S Kadam 	if (!rb_in_proc->buffer.data) {
36291b22810SRushikesh S Kadam 		dev_warn(cl_data_to_dev(client_data),
36391b22810SRushikesh S Kadam 			 "rb_in_proc->buffer.data returned null");
36491b22810SRushikesh S Kadam 		client_data->response.error = -EBADMSG;
36591b22810SRushikesh S Kadam 		goto end;
36691b22810SRushikesh S Kadam 	}
36791b22810SRushikesh S Kadam 
36891b22810SRushikesh S Kadam 	if (data_len < sizeof(struct loader_msg_hdr)) {
36991b22810SRushikesh S Kadam 		dev_err(cl_data_to_dev(client_data),
37091b22810SRushikesh S Kadam 			"data size %zu is less than header %zu\n",
37191b22810SRushikesh S Kadam 			data_len, sizeof(struct loader_msg_hdr));
37291b22810SRushikesh S Kadam 		client_data->response.error = -EMSGSIZE;
37391b22810SRushikesh S Kadam 		goto end;
37491b22810SRushikesh S Kadam 	}
37591b22810SRushikesh S Kadam 
37691b22810SRushikesh S Kadam 	hdr = (struct loader_msg_hdr *)rb_in_proc->buffer.data;
37791b22810SRushikesh S Kadam 
37891b22810SRushikesh S Kadam 	dev_dbg(cl_data_to_dev(client_data),
37991b22810SRushikesh S Kadam 		"%s: command=%02lx is_response=%u status=%02x\n",
38091b22810SRushikesh S Kadam 		__func__,
38191b22810SRushikesh S Kadam 		hdr->command & CMD_MASK,
38291b22810SRushikesh S Kadam 		hdr->command & IS_RESPONSE ? 1 : 0,
38391b22810SRushikesh S Kadam 		hdr->status);
38491b22810SRushikesh S Kadam 
38591b22810SRushikesh S Kadam 	if (((hdr->command & CMD_MASK) != LOADER_CMD_XFER_QUERY) &&
38691b22810SRushikesh S Kadam 	    ((hdr->command & CMD_MASK) != LOADER_CMD_XFER_FRAGMENT) &&
38791b22810SRushikesh S Kadam 	    ((hdr->command & CMD_MASK) != LOADER_CMD_START)) {
38891b22810SRushikesh S Kadam 		dev_err(cl_data_to_dev(client_data),
38991b22810SRushikesh S Kadam 			"Invalid command=%02lx\n",
39091b22810SRushikesh S Kadam 			hdr->command & CMD_MASK);
39191b22810SRushikesh S Kadam 		client_data->response.error = -EPROTO;
39291b22810SRushikesh S Kadam 		goto end;
39391b22810SRushikesh S Kadam 	}
39491b22810SRushikesh S Kadam 
39591b22810SRushikesh S Kadam 	if (data_len > client_data->response.max_size) {
39691b22810SRushikesh S Kadam 		dev_err(cl_data_to_dev(client_data),
39791b22810SRushikesh S Kadam 			"Received buffer size %zu is larger than allocated buffer %zu\n",
39891b22810SRushikesh S Kadam 			data_len, client_data->response.max_size);
39991b22810SRushikesh S Kadam 		client_data->response.error = -EMSGSIZE;
40091b22810SRushikesh S Kadam 		goto end;
40191b22810SRushikesh S Kadam 	}
40291b22810SRushikesh S Kadam 
40391b22810SRushikesh S Kadam 	/* We expect only "response" messages from firmware */
40491b22810SRushikesh S Kadam 	if (!(hdr->command & IS_RESPONSE)) {
40591b22810SRushikesh S Kadam 		dev_err(cl_data_to_dev(client_data),
40691b22810SRushikesh S Kadam 			"Invalid response to command\n");
40791b22810SRushikesh S Kadam 		client_data->response.error = -EIO;
40891b22810SRushikesh S Kadam 		goto end;
40991b22810SRushikesh S Kadam 	}
41091b22810SRushikesh S Kadam 
41191b22810SRushikesh S Kadam 	if (hdr->status) {
41291b22810SRushikesh S Kadam 		dev_err(cl_data_to_dev(client_data),
41391b22810SRushikesh S Kadam 			"Loader returned status %d\n",
41491b22810SRushikesh S Kadam 			hdr->status);
41591b22810SRushikesh S Kadam 		client_data->response.error = -EIO;
41691b22810SRushikesh S Kadam 		goto end;
41791b22810SRushikesh S Kadam 	}
41891b22810SRushikesh S Kadam 
41991b22810SRushikesh S Kadam 	/* Update the actual received buffer size */
42091b22810SRushikesh S Kadam 	client_data->response.size = data_len;
42191b22810SRushikesh S Kadam 
42291b22810SRushikesh S Kadam 	/*
42391b22810SRushikesh S Kadam 	 * Copy the buffer received in firmware response for the
42491b22810SRushikesh S Kadam 	 * calling thread.
42591b22810SRushikesh S Kadam 	 */
42691b22810SRushikesh S Kadam 	memcpy(client_data->response.data,
42791b22810SRushikesh S Kadam 	       rb_in_proc->buffer.data, data_len);
42891b22810SRushikesh S Kadam 
42991b22810SRushikesh S Kadam 	/* Set flag before waking up the caller */
43091b22810SRushikesh S Kadam 	client_data->response.received = true;
43191b22810SRushikesh S Kadam 
43291b22810SRushikesh S Kadam end:
43391b22810SRushikesh S Kadam 	/* Free the buffer */
43491b22810SRushikesh S Kadam 	ishtp_cl_io_rb_recycle(rb_in_proc);
43591b22810SRushikesh S Kadam 	rb_in_proc = NULL;
43691b22810SRushikesh S Kadam 
43791b22810SRushikesh S Kadam 	/* Wake the calling thread */
43891b22810SRushikesh S Kadam 	wake_up_interruptible(&client_data->response.wait_queue);
43991b22810SRushikesh S Kadam }
44091b22810SRushikesh S Kadam 
44191b22810SRushikesh S Kadam /**
44291b22810SRushikesh S Kadam  * loader_cl_event_cb() - bus driver callback for incoming message
443509405cdSLee Jones  * @cl_device:		Pointer to the ishtp client device for which this
44491b22810SRushikesh S Kadam  *			message is targeted
44591b22810SRushikesh S Kadam  *
44691b22810SRushikesh S Kadam  * Remove the packet from the list and process the message by calling
44791b22810SRushikesh S Kadam  * process_recv
44891b22810SRushikesh S Kadam  */
loader_cl_event_cb(struct ishtp_cl_device * cl_device)44991b22810SRushikesh S Kadam static void loader_cl_event_cb(struct ishtp_cl_device *cl_device)
45091b22810SRushikesh S Kadam {
45191b22810SRushikesh S Kadam 	struct ishtp_cl_rb *rb_in_proc;
45291b22810SRushikesh S Kadam 	struct ishtp_cl	*loader_ishtp_cl = ishtp_get_drvdata(cl_device);
45391b22810SRushikesh S Kadam 
45491b22810SRushikesh S Kadam 	while ((rb_in_proc = ishtp_cl_rx_get_rb(loader_ishtp_cl)) != NULL) {
45591b22810SRushikesh S Kadam 		/* Process the data packet from firmware */
45691b22810SRushikesh S Kadam 		process_recv(loader_ishtp_cl, rb_in_proc);
45791b22810SRushikesh S Kadam 	}
45891b22810SRushikesh S Kadam }
45991b22810SRushikesh S Kadam 
46091b22810SRushikesh S Kadam /**
46191b22810SRushikesh S Kadam  * ish_query_loader_prop() -  Query ISH Shim firmware loader
46291b22810SRushikesh S Kadam  * @client_data:	Client data instance
463f8e5388aSAustin Kim  * @fw:			Pointer to firmware data struct in host memory
46491b22810SRushikesh S Kadam  * @fw_info:		Loader firmware properties
46591b22810SRushikesh S Kadam  *
46691b22810SRushikesh S Kadam  * This function queries the ISH Shim firmware loader for capabilities.
46791b22810SRushikesh S Kadam  *
46891b22810SRushikesh S Kadam  * Return: 0 for success, negative error code for failure.
46991b22810SRushikesh S Kadam  */
ish_query_loader_prop(struct ishtp_cl_data * client_data,const struct firmware * fw,struct shim_fw_info * fw_info)47091b22810SRushikesh S Kadam static int ish_query_loader_prop(struct ishtp_cl_data *client_data,
47191b22810SRushikesh S Kadam 				 const struct firmware *fw,
47291b22810SRushikesh S Kadam 				 struct shim_fw_info *fw_info)
47391b22810SRushikesh S Kadam {
47491b22810SRushikesh S Kadam 	int rv;
47591b22810SRushikesh S Kadam 	struct loader_xfer_query ldr_xfer_query;
47691b22810SRushikesh S Kadam 	struct loader_xfer_query_response ldr_xfer_query_resp;
47791b22810SRushikesh S Kadam 
47891b22810SRushikesh S Kadam 	memset(&ldr_xfer_query, 0, sizeof(ldr_xfer_query));
47991b22810SRushikesh S Kadam 	ldr_xfer_query.hdr.command = LOADER_CMD_XFER_QUERY;
48091b22810SRushikesh S Kadam 	ldr_xfer_query.image_size = fw->size;
48191b22810SRushikesh S Kadam 	rv = loader_cl_send(client_data,
48291b22810SRushikesh S Kadam 			    (u8 *)&ldr_xfer_query,
48391b22810SRushikesh S Kadam 			    sizeof(ldr_xfer_query),
48491b22810SRushikesh S Kadam 			    (u8 *)&ldr_xfer_query_resp,
48591b22810SRushikesh S Kadam 			    sizeof(ldr_xfer_query_resp));
48691b22810SRushikesh S Kadam 	if (rv < 0) {
48791b22810SRushikesh S Kadam 		client_data->flag_retry = true;
4880b66fb3eSArnd Bergmann 		*fw_info = (struct shim_fw_info){};
48991b22810SRushikesh S Kadam 		return rv;
49091b22810SRushikesh S Kadam 	}
49191b22810SRushikesh S Kadam 
49291b22810SRushikesh S Kadam 	/* On success, the return value is the received buffer size */
49391b22810SRushikesh S Kadam 	if (rv != sizeof(struct loader_xfer_query_response)) {
49491b22810SRushikesh S Kadam 		dev_err(cl_data_to_dev(client_data),
49591b22810SRushikesh S Kadam 			"data size %d is not equal to size of loader_xfer_query_response %zu\n",
49691b22810SRushikesh S Kadam 			rv, sizeof(struct loader_xfer_query_response));
49791b22810SRushikesh S Kadam 		client_data->flag_retry = true;
4980b66fb3eSArnd Bergmann 		*fw_info = (struct shim_fw_info){};
49991b22810SRushikesh S Kadam 		return -EMSGSIZE;
50091b22810SRushikesh S Kadam 	}
50191b22810SRushikesh S Kadam 
50291b22810SRushikesh S Kadam 	/* Save fw_info for use outside this function */
50391b22810SRushikesh S Kadam 	*fw_info = ldr_xfer_query_resp.fw_info;
50491b22810SRushikesh S Kadam 
50591b22810SRushikesh S Kadam 	/* Loader firmware properties */
50691b22810SRushikesh S Kadam 	dev_dbg(cl_data_to_dev(client_data),
50791b22810SRushikesh S Kadam 		"ish_fw_version: major=%d minor=%d hotfix=%d build=%d protocol_version=0x%x loader_version=%d\n",
50891b22810SRushikesh S Kadam 		fw_info->ish_fw_version.major,
50991b22810SRushikesh S Kadam 		fw_info->ish_fw_version.minor,
51091b22810SRushikesh S Kadam 		fw_info->ish_fw_version.hotfix,
51191b22810SRushikesh S Kadam 		fw_info->ish_fw_version.build,
51291b22810SRushikesh S Kadam 		fw_info->protocol_version,
51391b22810SRushikesh S Kadam 		fw_info->ldr_version.value);
51491b22810SRushikesh S Kadam 
51591b22810SRushikesh S Kadam 	dev_dbg(cl_data_to_dev(client_data),
51691b22810SRushikesh S Kadam 		"loader_capability: max_fw_image_size=0x%x xfer_mode=%d max_dma_buf_size=0x%x dma_buf_size_limit=0x%x\n",
51791b22810SRushikesh S Kadam 		fw_info->ldr_capability.max_fw_image_size,
51891b22810SRushikesh S Kadam 		fw_info->ldr_capability.xfer_mode,
51991b22810SRushikesh S Kadam 		fw_info->ldr_capability.max_dma_buf_size,
52091b22810SRushikesh S Kadam 		dma_buf_size_limit);
52191b22810SRushikesh S Kadam 
52291b22810SRushikesh S Kadam 	/* Sanity checks */
52391b22810SRushikesh S Kadam 	if (fw_info->ldr_capability.max_fw_image_size < fw->size) {
52491b22810SRushikesh S Kadam 		dev_err(cl_data_to_dev(client_data),
52591b22810SRushikesh S Kadam 			"ISH firmware size %zu is greater than Shim firmware loader max supported %d\n",
52691b22810SRushikesh S Kadam 			fw->size,
52791b22810SRushikesh S Kadam 			fw_info->ldr_capability.max_fw_image_size);
52891b22810SRushikesh S Kadam 		return -ENOSPC;
52991b22810SRushikesh S Kadam 	}
53091b22810SRushikesh S Kadam 
53191b22810SRushikesh S Kadam 	/* For DMA the buffer size should be multiple of cacheline size */
53291b22810SRushikesh S Kadam 	if ((fw_info->ldr_capability.xfer_mode & LOADER_XFER_MODE_DIRECT_DMA) &&
53391b22810SRushikesh S Kadam 	    (fw_info->ldr_capability.max_dma_buf_size % L1_CACHE_BYTES)) {
53491b22810SRushikesh S Kadam 		dev_err(cl_data_to_dev(client_data),
5352eb3c3e6SColin Ian King 			"Shim firmware loader buffer size %d should be multiple of cacheline\n",
53691b22810SRushikesh S Kadam 			fw_info->ldr_capability.max_dma_buf_size);
53791b22810SRushikesh S Kadam 		return -EINVAL;
53891b22810SRushikesh S Kadam 	}
53991b22810SRushikesh S Kadam 
54091b22810SRushikesh S Kadam 	return 0;
54191b22810SRushikesh S Kadam }
54291b22810SRushikesh S Kadam 
54391b22810SRushikesh S Kadam /**
544509405cdSLee Jones  * ish_fw_xfer_ishtp() - Loads ISH firmware using ishtp interface
54591b22810SRushikesh S Kadam  * @client_data:	Client data instance
54691b22810SRushikesh S Kadam  * @fw:			Pointer to firmware data struct in host memory
54791b22810SRushikesh S Kadam  *
54891b22810SRushikesh S Kadam  * This function uses ISH-TP to transfer ISH firmware from host to
54991b22810SRushikesh S Kadam  * ISH SRAM. Lower layers may use IPC or DMA depending on firmware
55091b22810SRushikesh S Kadam  * support.
55191b22810SRushikesh S Kadam  *
55291b22810SRushikesh S Kadam  * Return: 0 for success, negative error code for failure.
55391b22810SRushikesh S Kadam  */
ish_fw_xfer_ishtp(struct ishtp_cl_data * client_data,const struct firmware * fw)55491b22810SRushikesh S Kadam static int ish_fw_xfer_ishtp(struct ishtp_cl_data *client_data,
55591b22810SRushikesh S Kadam 			     const struct firmware *fw)
55691b22810SRushikesh S Kadam {
55791b22810SRushikesh S Kadam 	int rv;
55891b22810SRushikesh S Kadam 	u32 fragment_offset, fragment_size, payload_max_size;
55991b22810SRushikesh S Kadam 	struct loader_xfer_ipc_fragment *ldr_xfer_ipc_frag;
56091b22810SRushikesh S Kadam 	struct loader_msg_hdr ldr_xfer_ipc_ack;
56191b22810SRushikesh S Kadam 
56291b22810SRushikesh S Kadam 	payload_max_size =
56391b22810SRushikesh S Kadam 		LOADER_SHIM_IPC_BUF_SIZE - IPC_FRAGMENT_DATA_PREAMBLE;
56491b22810SRushikesh S Kadam 
56591b22810SRushikesh S Kadam 	ldr_xfer_ipc_frag = kzalloc(LOADER_SHIM_IPC_BUF_SIZE, GFP_KERNEL);
56691b22810SRushikesh S Kadam 	if (!ldr_xfer_ipc_frag) {
56791b22810SRushikesh S Kadam 		client_data->flag_retry = true;
56891b22810SRushikesh S Kadam 		return -ENOMEM;
56991b22810SRushikesh S Kadam 	}
57091b22810SRushikesh S Kadam 
57191b22810SRushikesh S Kadam 	ldr_xfer_ipc_frag->fragment.hdr.command = LOADER_CMD_XFER_FRAGMENT;
57291b22810SRushikesh S Kadam 	ldr_xfer_ipc_frag->fragment.xfer_mode = LOADER_XFER_MODE_ISHTP;
57391b22810SRushikesh S Kadam 
57491b22810SRushikesh S Kadam 	/* Break the firmware image into fragments and send as ISH-TP payload */
57591b22810SRushikesh S Kadam 	fragment_offset = 0;
57691b22810SRushikesh S Kadam 	while (fragment_offset < fw->size) {
57791b22810SRushikesh S Kadam 		if (fragment_offset + payload_max_size < fw->size) {
57891b22810SRushikesh S Kadam 			fragment_size = payload_max_size;
57991b22810SRushikesh S Kadam 			ldr_xfer_ipc_frag->fragment.is_last = 0;
58091b22810SRushikesh S Kadam 		} else {
58191b22810SRushikesh S Kadam 			fragment_size = fw->size - fragment_offset;
58291b22810SRushikesh S Kadam 			ldr_xfer_ipc_frag->fragment.is_last = 1;
58391b22810SRushikesh S Kadam 		}
58491b22810SRushikesh S Kadam 
58591b22810SRushikesh S Kadam 		ldr_xfer_ipc_frag->fragment.offset = fragment_offset;
58691b22810SRushikesh S Kadam 		ldr_xfer_ipc_frag->fragment.size = fragment_size;
58791b22810SRushikesh S Kadam 		memcpy(ldr_xfer_ipc_frag->data,
58891b22810SRushikesh S Kadam 		       &fw->data[fragment_offset],
58991b22810SRushikesh S Kadam 		       fragment_size);
59091b22810SRushikesh S Kadam 
59191b22810SRushikesh S Kadam 		dev_dbg(cl_data_to_dev(client_data),
59291b22810SRushikesh S Kadam 			"xfer_mode=ipc offset=0x%08x size=0x%08x is_last=%d\n",
59391b22810SRushikesh S Kadam 			ldr_xfer_ipc_frag->fragment.offset,
59491b22810SRushikesh S Kadam 			ldr_xfer_ipc_frag->fragment.size,
59591b22810SRushikesh S Kadam 			ldr_xfer_ipc_frag->fragment.is_last);
59691b22810SRushikesh S Kadam 
59791b22810SRushikesh S Kadam 		rv = loader_cl_send(client_data,
59891b22810SRushikesh S Kadam 				    (u8 *)ldr_xfer_ipc_frag,
59991b22810SRushikesh S Kadam 				    IPC_FRAGMENT_DATA_PREAMBLE + fragment_size,
60091b22810SRushikesh S Kadam 				    (u8 *)&ldr_xfer_ipc_ack,
60191b22810SRushikesh S Kadam 				    sizeof(ldr_xfer_ipc_ack));
60291b22810SRushikesh S Kadam 		if (rv < 0) {
60391b22810SRushikesh S Kadam 			client_data->flag_retry = true;
60491b22810SRushikesh S Kadam 			goto end_err_resp_buf_release;
60591b22810SRushikesh S Kadam 		}
60691b22810SRushikesh S Kadam 
60791b22810SRushikesh S Kadam 		fragment_offset += fragment_size;
60891b22810SRushikesh S Kadam 	}
60991b22810SRushikesh S Kadam 
61091b22810SRushikesh S Kadam 	kfree(ldr_xfer_ipc_frag);
61191b22810SRushikesh S Kadam 	return 0;
61291b22810SRushikesh S Kadam 
61391b22810SRushikesh S Kadam end_err_resp_buf_release:
61491b22810SRushikesh S Kadam 	/* Free ISH buffer if not done already, in error case */
61591b22810SRushikesh S Kadam 	kfree(ldr_xfer_ipc_frag);
61691b22810SRushikesh S Kadam 	return rv;
61791b22810SRushikesh S Kadam }
61891b22810SRushikesh S Kadam 
61991b22810SRushikesh S Kadam /**
62091b22810SRushikesh S Kadam  * ish_fw_xfer_direct_dma() - Loads ISH firmware using direct dma
62191b22810SRushikesh S Kadam  * @client_data:	Client data instance
62291b22810SRushikesh S Kadam  * @fw:			Pointer to firmware data struct in host memory
62391b22810SRushikesh S Kadam  * @fw_info:		Loader firmware properties
62491b22810SRushikesh S Kadam  *
62591b22810SRushikesh S Kadam  * Host firmware load is a unique case where we need to download
62691b22810SRushikesh S Kadam  * a large firmware image (200+ Kb). This function implements
62791b22810SRushikesh S Kadam  * direct DMA transfer in kernel and ISH firmware. This allows
62891b22810SRushikesh S Kadam  * us to overcome the ISH-TP 4 Kb limit, and allows us to DMA
62991b22810SRushikesh S Kadam  * directly to ISH UMA at location of choice.
63091b22810SRushikesh S Kadam  * Function depends on corresponding support in ISH firmware.
63191b22810SRushikesh S Kadam  *
63291b22810SRushikesh S Kadam  * Return: 0 for success, negative error code for failure.
63391b22810SRushikesh S Kadam  */
ish_fw_xfer_direct_dma(struct ishtp_cl_data * client_data,const struct firmware * fw,const struct shim_fw_info fw_info)63491b22810SRushikesh S Kadam static int ish_fw_xfer_direct_dma(struct ishtp_cl_data *client_data,
63591b22810SRushikesh S Kadam 				  const struct firmware *fw,
63691b22810SRushikesh S Kadam 				  const struct shim_fw_info fw_info)
63791b22810SRushikesh S Kadam {
638*fb1643d0SSurajSonawane2415 	int rv = 0;
63991b22810SRushikesh S Kadam 	void *dma_buf;
64091b22810SRushikesh S Kadam 	dma_addr_t dma_buf_phy;
64191b22810SRushikesh S Kadam 	u32 fragment_offset, fragment_size, payload_max_size;
64291b22810SRushikesh S Kadam 	struct loader_msg_hdr ldr_xfer_dma_frag_ack;
64391b22810SRushikesh S Kadam 	struct loader_xfer_dma_fragment ldr_xfer_dma_frag;
64491b22810SRushikesh S Kadam 	struct device *devc = ishtp_get_pci_device(client_data->cl_device);
64591b22810SRushikesh S Kadam 	u32 shim_fw_buf_size =
64691b22810SRushikesh S Kadam 		fw_info.ldr_capability.max_dma_buf_size;
64791b22810SRushikesh S Kadam 
64891b22810SRushikesh S Kadam 	/*
64991b22810SRushikesh S Kadam 	 * payload_max_size should be set to minimum of
65091b22810SRushikesh S Kadam 	 *  (1) Size of firmware to be loaded,
65191b22810SRushikesh S Kadam 	 *  (2) Max DMA buffer size supported by Shim firmware,
65291b22810SRushikesh S Kadam 	 *  (3) DMA buffer size limit set by boot_param dma_buf_size_limit.
65391b22810SRushikesh S Kadam 	 */
65491b22810SRushikesh S Kadam 	payload_max_size = min3(fw->size,
65591b22810SRushikesh S Kadam 				(size_t)shim_fw_buf_size,
65691b22810SRushikesh S Kadam 				(size_t)dma_buf_size_limit);
65791b22810SRushikesh S Kadam 
65891b22810SRushikesh S Kadam 	/*
65991b22810SRushikesh S Kadam 	 * Buffer size should be multiple of cacheline size
66091b22810SRushikesh S Kadam 	 * if it's not, select the previous cacheline boundary.
66191b22810SRushikesh S Kadam 	 */
66291b22810SRushikesh S Kadam 	payload_max_size &= ~(L1_CACHE_BYTES - 1);
66391b22810SRushikesh S Kadam 
664f97ec5d7SGwendal Grignou 	dma_buf = dma_alloc_coherent(devc, payload_max_size, &dma_buf_phy, GFP_KERNEL);
66591b22810SRushikesh S Kadam 	if (!dma_buf) {
66691b22810SRushikesh S Kadam 		client_data->flag_retry = true;
66791b22810SRushikesh S Kadam 		return -ENOMEM;
66891b22810SRushikesh S Kadam 	}
66991b22810SRushikesh S Kadam 
67091b22810SRushikesh S Kadam 	ldr_xfer_dma_frag.fragment.hdr.command = LOADER_CMD_XFER_FRAGMENT;
67191b22810SRushikesh S Kadam 	ldr_xfer_dma_frag.fragment.xfer_mode = LOADER_XFER_MODE_DIRECT_DMA;
67291b22810SRushikesh S Kadam 	ldr_xfer_dma_frag.ddr_phys_addr = (u64)dma_buf_phy;
67391b22810SRushikesh S Kadam 
67491b22810SRushikesh S Kadam 	/* Send the firmware image in chucks of payload_max_size */
67591b22810SRushikesh S Kadam 	fragment_offset = 0;
67691b22810SRushikesh S Kadam 	while (fragment_offset < fw->size) {
67791b22810SRushikesh S Kadam 		if (fragment_offset + payload_max_size < fw->size) {
67891b22810SRushikesh S Kadam 			fragment_size = payload_max_size;
67991b22810SRushikesh S Kadam 			ldr_xfer_dma_frag.fragment.is_last = 0;
68091b22810SRushikesh S Kadam 		} else {
68191b22810SRushikesh S Kadam 			fragment_size = fw->size - fragment_offset;
68291b22810SRushikesh S Kadam 			ldr_xfer_dma_frag.fragment.is_last = 1;
68391b22810SRushikesh S Kadam 		}
68491b22810SRushikesh S Kadam 
68591b22810SRushikesh S Kadam 		ldr_xfer_dma_frag.fragment.offset = fragment_offset;
68691b22810SRushikesh S Kadam 		ldr_xfer_dma_frag.fragment.size = fragment_size;
68791b22810SRushikesh S Kadam 		memcpy(dma_buf, &fw->data[fragment_offset], fragment_size);
68891b22810SRushikesh S Kadam 
689f97ec5d7SGwendal Grignou 		/* Flush cache to be sure the data is in main memory. */
69091b22810SRushikesh S Kadam 		clflush_cache_range(dma_buf, payload_max_size);
69191b22810SRushikesh S Kadam 
69291b22810SRushikesh S Kadam 		dev_dbg(cl_data_to_dev(client_data),
69391b22810SRushikesh S Kadam 			"xfer_mode=dma offset=0x%08x size=0x%x is_last=%d ddr_phys_addr=0x%016llx\n",
69491b22810SRushikesh S Kadam 			ldr_xfer_dma_frag.fragment.offset,
69591b22810SRushikesh S Kadam 			ldr_xfer_dma_frag.fragment.size,
69691b22810SRushikesh S Kadam 			ldr_xfer_dma_frag.fragment.is_last,
69791b22810SRushikesh S Kadam 			ldr_xfer_dma_frag.ddr_phys_addr);
69891b22810SRushikesh S Kadam 
69991b22810SRushikesh S Kadam 		rv = loader_cl_send(client_data,
70091b22810SRushikesh S Kadam 				    (u8 *)&ldr_xfer_dma_frag,
70191b22810SRushikesh S Kadam 				    sizeof(ldr_xfer_dma_frag),
70291b22810SRushikesh S Kadam 				    (u8 *)&ldr_xfer_dma_frag_ack,
70391b22810SRushikesh S Kadam 				    sizeof(ldr_xfer_dma_frag_ack));
70491b22810SRushikesh S Kadam 		if (rv < 0) {
70591b22810SRushikesh S Kadam 			client_data->flag_retry = true;
70691b22810SRushikesh S Kadam 			goto end_err_resp_buf_release;
70791b22810SRushikesh S Kadam 		}
70891b22810SRushikesh S Kadam 
70991b22810SRushikesh S Kadam 		fragment_offset += fragment_size;
71091b22810SRushikesh S Kadam 	}
71191b22810SRushikesh S Kadam 
71291b22810SRushikesh S Kadam end_err_resp_buf_release:
713f97ec5d7SGwendal Grignou 	dma_free_coherent(devc, payload_max_size, dma_buf, dma_buf_phy);
71491b22810SRushikesh S Kadam 	return rv;
71591b22810SRushikesh S Kadam }
71691b22810SRushikesh S Kadam 
71791b22810SRushikesh S Kadam /**
718509405cdSLee Jones  * ish_fw_start() -	Start executing ISH main firmware
71991b22810SRushikesh S Kadam  * @client_data:	client data instance
72091b22810SRushikesh S Kadam  *
72191b22810SRushikesh S Kadam  * This function sends message to Shim firmware loader to start
72291b22810SRushikesh S Kadam  * the execution of ISH main firmware.
72391b22810SRushikesh S Kadam  *
72491b22810SRushikesh S Kadam  * Return: 0 for success, negative error code for failure.
72591b22810SRushikesh S Kadam  */
ish_fw_start(struct ishtp_cl_data * client_data)72691b22810SRushikesh S Kadam static int ish_fw_start(struct ishtp_cl_data *client_data)
72791b22810SRushikesh S Kadam {
72891b22810SRushikesh S Kadam 	struct loader_start ldr_start;
72991b22810SRushikesh S Kadam 	struct loader_msg_hdr ldr_start_ack;
73091b22810SRushikesh S Kadam 
73191b22810SRushikesh S Kadam 	memset(&ldr_start, 0, sizeof(ldr_start));
73291b22810SRushikesh S Kadam 	ldr_start.hdr.command = LOADER_CMD_START;
73391b22810SRushikesh S Kadam 	return loader_cl_send(client_data,
73491b22810SRushikesh S Kadam 			    (u8 *)&ldr_start,
73591b22810SRushikesh S Kadam 			    sizeof(ldr_start),
73691b22810SRushikesh S Kadam 			    (u8 *)&ldr_start_ack,
73791b22810SRushikesh S Kadam 			    sizeof(ldr_start_ack));
73891b22810SRushikesh S Kadam }
73991b22810SRushikesh S Kadam 
74091b22810SRushikesh S Kadam /**
741509405cdSLee Jones  * load_fw_from_host() - Loads ISH firmware from host
74291b22810SRushikesh S Kadam  * @client_data:	Client data instance
74391b22810SRushikesh S Kadam  *
74491b22810SRushikesh S Kadam  * This function loads the ISH firmware to ISH SRAM and starts execution
74591b22810SRushikesh S Kadam  *
74691b22810SRushikesh S Kadam  * Return: 0 for success, negative error code for failure.
74791b22810SRushikesh S Kadam  */
load_fw_from_host(struct ishtp_cl_data * client_data)74891b22810SRushikesh S Kadam static int load_fw_from_host(struct ishtp_cl_data *client_data)
74991b22810SRushikesh S Kadam {
75091b22810SRushikesh S Kadam 	int rv;
75191b22810SRushikesh S Kadam 	u32 xfer_mode;
75291b22810SRushikesh S Kadam 	char *filename;
75391b22810SRushikesh S Kadam 	const struct firmware *fw;
75491b22810SRushikesh S Kadam 	struct shim_fw_info fw_info;
75591b22810SRushikesh S Kadam 	struct ishtp_cl *loader_ishtp_cl = client_data->loader_ishtp_cl;
75691b22810SRushikesh S Kadam 
75791b22810SRushikesh S Kadam 	client_data->flag_retry = false;
75891b22810SRushikesh S Kadam 
75991b22810SRushikesh S Kadam 	filename = kzalloc(FILENAME_SIZE, GFP_KERNEL);
76091b22810SRushikesh S Kadam 	if (!filename) {
76191b22810SRushikesh S Kadam 		client_data->flag_retry = true;
76291b22810SRushikesh S Kadam 		rv = -ENOMEM;
76391b22810SRushikesh S Kadam 		goto end_error;
76491b22810SRushikesh S Kadam 	}
76591b22810SRushikesh S Kadam 
76691b22810SRushikesh S Kadam 	/* Get filename of the ISH firmware to be loaded */
76791b22810SRushikesh S Kadam 	rv = get_firmware_variant(client_data, filename);
76891b22810SRushikesh S Kadam 	if (rv < 0)
76991b22810SRushikesh S Kadam 		goto end_err_filename_buf_release;
77091b22810SRushikesh S Kadam 
77191b22810SRushikesh S Kadam 	rv = request_firmware(&fw, filename, cl_data_to_dev(client_data));
77291b22810SRushikesh S Kadam 	if (rv < 0)
77391b22810SRushikesh S Kadam 		goto end_err_filename_buf_release;
77491b22810SRushikesh S Kadam 
77591b22810SRushikesh S Kadam 	/* Step 1: Query Shim firmware loader properties */
77691b22810SRushikesh S Kadam 
77791b22810SRushikesh S Kadam 	rv = ish_query_loader_prop(client_data, fw, &fw_info);
77891b22810SRushikesh S Kadam 	if (rv < 0)
77991b22810SRushikesh S Kadam 		goto end_err_fw_release;
78091b22810SRushikesh S Kadam 
78191b22810SRushikesh S Kadam 	/* Step 2: Send the main firmware image to be loaded, to ISH SRAM */
78291b22810SRushikesh S Kadam 
78391b22810SRushikesh S Kadam 	xfer_mode = fw_info.ldr_capability.xfer_mode;
78491b22810SRushikesh S Kadam 	if (xfer_mode & LOADER_XFER_MODE_DIRECT_DMA) {
78591b22810SRushikesh S Kadam 		rv = ish_fw_xfer_direct_dma(client_data, fw, fw_info);
78691b22810SRushikesh S Kadam 	} else if (xfer_mode & LOADER_XFER_MODE_ISHTP) {
78791b22810SRushikesh S Kadam 		rv = ish_fw_xfer_ishtp(client_data, fw);
78891b22810SRushikesh S Kadam 	} else {
78991b22810SRushikesh S Kadam 		dev_err(cl_data_to_dev(client_data),
79091b22810SRushikesh S Kadam 			"No transfer mode selected in firmware\n");
79191b22810SRushikesh S Kadam 		rv = -EINVAL;
79291b22810SRushikesh S Kadam 	}
79391b22810SRushikesh S Kadam 	if (rv < 0)
79491b22810SRushikesh S Kadam 		goto end_err_fw_release;
79591b22810SRushikesh S Kadam 
79691b22810SRushikesh S Kadam 	/* Step 3: Start ISH main firmware exeuction */
79791b22810SRushikesh S Kadam 
79891b22810SRushikesh S Kadam 	rv = ish_fw_start(client_data);
79991b22810SRushikesh S Kadam 	if (rv < 0)
80091b22810SRushikesh S Kadam 		goto end_err_fw_release;
80191b22810SRushikesh S Kadam 
80291b22810SRushikesh S Kadam 	release_firmware(fw);
80391b22810SRushikesh S Kadam 	dev_info(cl_data_to_dev(client_data), "ISH firmware %s loaded\n",
80491b22810SRushikesh S Kadam 		 filename);
80521acee4eSDan Carpenter 	kfree(filename);
80691b22810SRushikesh S Kadam 	return 0;
80791b22810SRushikesh S Kadam 
80891b22810SRushikesh S Kadam end_err_fw_release:
80991b22810SRushikesh S Kadam 	release_firmware(fw);
81091b22810SRushikesh S Kadam end_err_filename_buf_release:
81191b22810SRushikesh S Kadam 	kfree(filename);
81291b22810SRushikesh S Kadam end_error:
81391b22810SRushikesh S Kadam 	/* Keep a count of retries, and give up after 3 attempts */
81491b22810SRushikesh S Kadam 	if (client_data->flag_retry &&
81591b22810SRushikesh S Kadam 	    client_data->retry_count++ < MAX_LOAD_ATTEMPTS) {
81691b22810SRushikesh S Kadam 		dev_warn(cl_data_to_dev(client_data),
81791b22810SRushikesh S Kadam 			 "ISH host firmware load failed %d. Resetting ISH, and trying again..\n",
81891b22810SRushikesh S Kadam 			 rv);
81991b22810SRushikesh S Kadam 		ish_hw_reset(ishtp_get_ishtp_device(loader_ishtp_cl));
82091b22810SRushikesh S Kadam 	} else {
82191b22810SRushikesh S Kadam 		dev_err(cl_data_to_dev(client_data),
82291b22810SRushikesh S Kadam 			"ISH host firmware load failed %d\n", rv);
82391b22810SRushikesh S Kadam 	}
82491b22810SRushikesh S Kadam 	return rv;
82591b22810SRushikesh S Kadam }
82691b22810SRushikesh S Kadam 
load_fw_from_host_handler(struct work_struct * work)82791b22810SRushikesh S Kadam static void load_fw_from_host_handler(struct work_struct *work)
82891b22810SRushikesh S Kadam {
82991b22810SRushikesh S Kadam 	struct ishtp_cl_data *client_data;
83091b22810SRushikesh S Kadam 
83191b22810SRushikesh S Kadam 	client_data = container_of(work, struct ishtp_cl_data,
83291b22810SRushikesh S Kadam 				   work_fw_load);
83391b22810SRushikesh S Kadam 	load_fw_from_host(client_data);
83491b22810SRushikesh S Kadam }
83591b22810SRushikesh S Kadam 
83691b22810SRushikesh S Kadam /**
83791b22810SRushikesh S Kadam  * loader_init() -	Init function for ISH-TP client
83891b22810SRushikesh S Kadam  * @loader_ishtp_cl:	ISH-TP client instance
83991b22810SRushikesh S Kadam  * @reset:		true if called for init after reset
84091b22810SRushikesh S Kadam  *
84191b22810SRushikesh S Kadam  * Return: 0 for success, negative error code for failure
84291b22810SRushikesh S Kadam  */
loader_init(struct ishtp_cl * loader_ishtp_cl,int reset)84391b22810SRushikesh S Kadam static int loader_init(struct ishtp_cl *loader_ishtp_cl, int reset)
84491b22810SRushikesh S Kadam {
84591b22810SRushikesh S Kadam 	int rv;
84691b22810SRushikesh S Kadam 	struct ishtp_fw_client *fw_client;
84791b22810SRushikesh S Kadam 	struct ishtp_cl_data *client_data =
84891b22810SRushikesh S Kadam 		ishtp_get_client_data(loader_ishtp_cl);
84991b22810SRushikesh S Kadam 
85091b22810SRushikesh S Kadam 	dev_dbg(cl_data_to_dev(client_data), "reset flag: %d\n", reset);
85191b22810SRushikesh S Kadam 
85291b22810SRushikesh S Kadam 	rv = ishtp_cl_link(loader_ishtp_cl);
85391b22810SRushikesh S Kadam 	if (rv < 0) {
85491b22810SRushikesh S Kadam 		dev_err(cl_data_to_dev(client_data), "ishtp_cl_link failed\n");
85591b22810SRushikesh S Kadam 		return rv;
85691b22810SRushikesh S Kadam 	}
85791b22810SRushikesh S Kadam 
85891b22810SRushikesh S Kadam 	/* Connect to firmware client */
85991b22810SRushikesh S Kadam 	ishtp_set_tx_ring_size(loader_ishtp_cl, LOADER_CL_TX_RING_SIZE);
86091b22810SRushikesh S Kadam 	ishtp_set_rx_ring_size(loader_ishtp_cl, LOADER_CL_RX_RING_SIZE);
86191b22810SRushikesh S Kadam 
86291b22810SRushikesh S Kadam 	fw_client =
86391b22810SRushikesh S Kadam 		ishtp_fw_cl_get_client(ishtp_get_ishtp_device(loader_ishtp_cl),
864bf9167a8SArnd Bergmann 				       &loader_ishtp_id_table[0].guid);
86591b22810SRushikesh S Kadam 	if (!fw_client) {
86691b22810SRushikesh S Kadam 		dev_err(cl_data_to_dev(client_data),
86791b22810SRushikesh S Kadam 			"ISH client uuid not found\n");
86891b22810SRushikesh S Kadam 		rv = -ENOENT;
86991b22810SRushikesh S Kadam 		goto err_cl_unlink;
87091b22810SRushikesh S Kadam 	}
87191b22810SRushikesh S Kadam 
87291b22810SRushikesh S Kadam 	ishtp_cl_set_fw_client_id(loader_ishtp_cl,
87391b22810SRushikesh S Kadam 				  ishtp_get_fw_client_id(fw_client));
87491b22810SRushikesh S Kadam 	ishtp_set_connection_state(loader_ishtp_cl, ISHTP_CL_CONNECTING);
87591b22810SRushikesh S Kadam 
87691b22810SRushikesh S Kadam 	rv = ishtp_cl_connect(loader_ishtp_cl);
87791b22810SRushikesh S Kadam 	if (rv < 0) {
87891b22810SRushikesh S Kadam 		dev_err(cl_data_to_dev(client_data), "Client connect fail\n");
87991b22810SRushikesh S Kadam 		goto err_cl_unlink;
88091b22810SRushikesh S Kadam 	}
88191b22810SRushikesh S Kadam 
88291b22810SRushikesh S Kadam 	dev_dbg(cl_data_to_dev(client_data), "Client connected\n");
88391b22810SRushikesh S Kadam 
88491b22810SRushikesh S Kadam 	ishtp_register_event_cb(client_data->cl_device, loader_cl_event_cb);
88591b22810SRushikesh S Kadam 
88691b22810SRushikesh S Kadam 	return 0;
88791b22810SRushikesh S Kadam 
88891b22810SRushikesh S Kadam err_cl_unlink:
88991b22810SRushikesh S Kadam 	ishtp_cl_unlink(loader_ishtp_cl);
89091b22810SRushikesh S Kadam 	return rv;
89191b22810SRushikesh S Kadam }
89291b22810SRushikesh S Kadam 
loader_deinit(struct ishtp_cl * loader_ishtp_cl)89391b22810SRushikesh S Kadam static void loader_deinit(struct ishtp_cl *loader_ishtp_cl)
89491b22810SRushikesh S Kadam {
89591b22810SRushikesh S Kadam 	ishtp_set_connection_state(loader_ishtp_cl, ISHTP_CL_DISCONNECTING);
89691b22810SRushikesh S Kadam 	ishtp_cl_disconnect(loader_ishtp_cl);
89791b22810SRushikesh S Kadam 	ishtp_cl_unlink(loader_ishtp_cl);
89891b22810SRushikesh S Kadam 	ishtp_cl_flush_queues(loader_ishtp_cl);
89991b22810SRushikesh S Kadam 
90091b22810SRushikesh S Kadam 	/* Disband and free all Tx and Rx client-level rings */
90191b22810SRushikesh S Kadam 	ishtp_cl_free(loader_ishtp_cl);
90291b22810SRushikesh S Kadam }
90391b22810SRushikesh S Kadam 
reset_handler(struct work_struct * work)90491b22810SRushikesh S Kadam static void reset_handler(struct work_struct *work)
90591b22810SRushikesh S Kadam {
90691b22810SRushikesh S Kadam 	int rv;
90791b22810SRushikesh S Kadam 	struct ishtp_cl_data *client_data;
90891b22810SRushikesh S Kadam 	struct ishtp_cl *loader_ishtp_cl;
90991b22810SRushikesh S Kadam 	struct ishtp_cl_device *cl_device;
91091b22810SRushikesh S Kadam 
91191b22810SRushikesh S Kadam 	client_data = container_of(work, struct ishtp_cl_data,
91291b22810SRushikesh S Kadam 				   work_ishtp_reset);
91391b22810SRushikesh S Kadam 
91491b22810SRushikesh S Kadam 	loader_ishtp_cl = client_data->loader_ishtp_cl;
91591b22810SRushikesh S Kadam 	cl_device = client_data->cl_device;
91691b22810SRushikesh S Kadam 
91791b22810SRushikesh S Kadam 	/* Unlink, flush queues & start again */
91891b22810SRushikesh S Kadam 	ishtp_cl_unlink(loader_ishtp_cl);
91991b22810SRushikesh S Kadam 	ishtp_cl_flush_queues(loader_ishtp_cl);
92091b22810SRushikesh S Kadam 	ishtp_cl_free(loader_ishtp_cl);
92191b22810SRushikesh S Kadam 
92291b22810SRushikesh S Kadam 	loader_ishtp_cl = ishtp_cl_allocate(cl_device);
92391b22810SRushikesh S Kadam 	if (!loader_ishtp_cl)
92491b22810SRushikesh S Kadam 		return;
92591b22810SRushikesh S Kadam 
92691b22810SRushikesh S Kadam 	ishtp_set_drvdata(cl_device, loader_ishtp_cl);
92791b22810SRushikesh S Kadam 	ishtp_set_client_data(loader_ishtp_cl, client_data);
92891b22810SRushikesh S Kadam 	client_data->loader_ishtp_cl = loader_ishtp_cl;
92991b22810SRushikesh S Kadam 	client_data->cl_device = cl_device;
93091b22810SRushikesh S Kadam 
93191b22810SRushikesh S Kadam 	rv = loader_init(loader_ishtp_cl, 1);
93291b22810SRushikesh S Kadam 	if (rv < 0) {
93391b22810SRushikesh S Kadam 		dev_err(ishtp_device(cl_device), "Reset Failed\n");
93491b22810SRushikesh S Kadam 		return;
93591b22810SRushikesh S Kadam 	}
93691b22810SRushikesh S Kadam 
93791b22810SRushikesh S Kadam 	/* ISH firmware loading from host */
93891b22810SRushikesh S Kadam 	load_fw_from_host(client_data);
93991b22810SRushikesh S Kadam }
94091b22810SRushikesh S Kadam 
94191b22810SRushikesh S Kadam /**
94291b22810SRushikesh S Kadam  * loader_ishtp_cl_probe() - ISH-TP client driver probe
94391b22810SRushikesh S Kadam  * @cl_device:		ISH-TP client device instance
94491b22810SRushikesh S Kadam  *
94591b22810SRushikesh S Kadam  * This function gets called on device create on ISH-TP bus
94691b22810SRushikesh S Kadam  *
94791b22810SRushikesh S Kadam  * Return: 0 for success, negative error code for failure
94891b22810SRushikesh S Kadam  */
loader_ishtp_cl_probe(struct ishtp_cl_device * cl_device)94991b22810SRushikesh S Kadam static int loader_ishtp_cl_probe(struct ishtp_cl_device *cl_device)
95091b22810SRushikesh S Kadam {
95191b22810SRushikesh S Kadam 	struct ishtp_cl *loader_ishtp_cl;
95291b22810SRushikesh S Kadam 	struct ishtp_cl_data *client_data;
95391b22810SRushikesh S Kadam 	int rv;
95491b22810SRushikesh S Kadam 
95591b22810SRushikesh S Kadam 	client_data = devm_kzalloc(ishtp_device(cl_device),
95691b22810SRushikesh S Kadam 				   sizeof(*client_data),
95791b22810SRushikesh S Kadam 				   GFP_KERNEL);
95891b22810SRushikesh S Kadam 	if (!client_data)
95991b22810SRushikesh S Kadam 		return -ENOMEM;
96091b22810SRushikesh S Kadam 
96191b22810SRushikesh S Kadam 	loader_ishtp_cl = ishtp_cl_allocate(cl_device);
96291b22810SRushikesh S Kadam 	if (!loader_ishtp_cl)
96391b22810SRushikesh S Kadam 		return -ENOMEM;
96491b22810SRushikesh S Kadam 
96591b22810SRushikesh S Kadam 	ishtp_set_drvdata(cl_device, loader_ishtp_cl);
96691b22810SRushikesh S Kadam 	ishtp_set_client_data(loader_ishtp_cl, client_data);
96791b22810SRushikesh S Kadam 	client_data->loader_ishtp_cl = loader_ishtp_cl;
96891b22810SRushikesh S Kadam 	client_data->cl_device = cl_device;
96991b22810SRushikesh S Kadam 
97091b22810SRushikesh S Kadam 	init_waitqueue_head(&client_data->response.wait_queue);
97191b22810SRushikesh S Kadam 
97291b22810SRushikesh S Kadam 	INIT_WORK(&client_data->work_ishtp_reset,
97391b22810SRushikesh S Kadam 		  reset_handler);
97491b22810SRushikesh S Kadam 	INIT_WORK(&client_data->work_fw_load,
97591b22810SRushikesh S Kadam 		  load_fw_from_host_handler);
97691b22810SRushikesh S Kadam 
97791b22810SRushikesh S Kadam 	rv = loader_init(loader_ishtp_cl, 0);
97891b22810SRushikesh S Kadam 	if (rv < 0) {
97991b22810SRushikesh S Kadam 		ishtp_cl_free(loader_ishtp_cl);
98091b22810SRushikesh S Kadam 		return rv;
98191b22810SRushikesh S Kadam 	}
98291b22810SRushikesh S Kadam 	ishtp_get_device(cl_device);
98391b22810SRushikesh S Kadam 
98491b22810SRushikesh S Kadam 	client_data->retry_count = 0;
98591b22810SRushikesh S Kadam 
98691b22810SRushikesh S Kadam 	/* ISH firmware loading from host */
98791b22810SRushikesh S Kadam 	schedule_work(&client_data->work_fw_load);
98891b22810SRushikesh S Kadam 
98991b22810SRushikesh S Kadam 	return 0;
99091b22810SRushikesh S Kadam }
99191b22810SRushikesh S Kadam 
99291b22810SRushikesh S Kadam /**
99391b22810SRushikesh S Kadam  * loader_ishtp_cl_remove() - ISH-TP client driver remove
99491b22810SRushikesh S Kadam  * @cl_device:		ISH-TP client device instance
99591b22810SRushikesh S Kadam  *
99691b22810SRushikesh S Kadam  * This function gets called on device remove on ISH-TP bus
99791b22810SRushikesh S Kadam  *
99891b22810SRushikesh S Kadam  * Return: 0
99991b22810SRushikesh S Kadam  */
loader_ishtp_cl_remove(struct ishtp_cl_device * cl_device)1000e71da1fdSUwe Kleine-König static void loader_ishtp_cl_remove(struct ishtp_cl_device *cl_device)
100191b22810SRushikesh S Kadam {
100291b22810SRushikesh S Kadam 	struct ishtp_cl_data *client_data;
100391b22810SRushikesh S Kadam 	struct ishtp_cl	*loader_ishtp_cl = ishtp_get_drvdata(cl_device);
100491b22810SRushikesh S Kadam 
100591b22810SRushikesh S Kadam 	client_data = ishtp_get_client_data(loader_ishtp_cl);
100691b22810SRushikesh S Kadam 
100791b22810SRushikesh S Kadam 	/*
100891b22810SRushikesh S Kadam 	 * The sequence of the following two cancel_work_sync() is
100991b22810SRushikesh S Kadam 	 * important. The work_fw_load can in turn schedue
101091b22810SRushikesh S Kadam 	 * work_ishtp_reset, so first cancel work_fw_load then
101191b22810SRushikesh S Kadam 	 * cancel work_ishtp_reset.
101291b22810SRushikesh S Kadam 	 */
101391b22810SRushikesh S Kadam 	cancel_work_sync(&client_data->work_fw_load);
101491b22810SRushikesh S Kadam 	cancel_work_sync(&client_data->work_ishtp_reset);
101591b22810SRushikesh S Kadam 	loader_deinit(loader_ishtp_cl);
101691b22810SRushikesh S Kadam 	ishtp_put_device(cl_device);
101791b22810SRushikesh S Kadam }
101891b22810SRushikesh S Kadam 
101991b22810SRushikesh S Kadam /**
102091b22810SRushikesh S Kadam  * loader_ishtp_cl_reset() - ISH-TP client driver reset
102191b22810SRushikesh S Kadam  * @cl_device:		ISH-TP client device instance
102291b22810SRushikesh S Kadam  *
102391b22810SRushikesh S Kadam  * This function gets called on device reset on ISH-TP bus
102491b22810SRushikesh S Kadam  *
102591b22810SRushikesh S Kadam  * Return: 0
102691b22810SRushikesh S Kadam  */
loader_ishtp_cl_reset(struct ishtp_cl_device * cl_device)102791b22810SRushikesh S Kadam static int loader_ishtp_cl_reset(struct ishtp_cl_device *cl_device)
102891b22810SRushikesh S Kadam {
102991b22810SRushikesh S Kadam 	struct ishtp_cl_data *client_data;
103091b22810SRushikesh S Kadam 	struct ishtp_cl	*loader_ishtp_cl = ishtp_get_drvdata(cl_device);
103191b22810SRushikesh S Kadam 
103291b22810SRushikesh S Kadam 	client_data = ishtp_get_client_data(loader_ishtp_cl);
103391b22810SRushikesh S Kadam 
103491b22810SRushikesh S Kadam 	schedule_work(&client_data->work_ishtp_reset);
103591b22810SRushikesh S Kadam 
103691b22810SRushikesh S Kadam 	return 0;
103791b22810SRushikesh S Kadam }
103891b22810SRushikesh S Kadam 
103991b22810SRushikesh S Kadam static struct ishtp_cl_driver	loader_ishtp_cl_driver = {
104091b22810SRushikesh S Kadam 	.name = "ish-loader",
1041bf9167a8SArnd Bergmann 	.id = loader_ishtp_id_table,
104291b22810SRushikesh S Kadam 	.probe = loader_ishtp_cl_probe,
104391b22810SRushikesh S Kadam 	.remove = loader_ishtp_cl_remove,
104491b22810SRushikesh S Kadam 	.reset = loader_ishtp_cl_reset,
104591b22810SRushikesh S Kadam };
104691b22810SRushikesh S Kadam 
ish_loader_init(void)104791b22810SRushikesh S Kadam static int __init ish_loader_init(void)
104891b22810SRushikesh S Kadam {
104991b22810SRushikesh S Kadam 	return ishtp_cl_driver_register(&loader_ishtp_cl_driver, THIS_MODULE);
105091b22810SRushikesh S Kadam }
105191b22810SRushikesh S Kadam 
ish_loader_exit(void)105291b22810SRushikesh S Kadam static void __exit ish_loader_exit(void)
105391b22810SRushikesh S Kadam {
105491b22810SRushikesh S Kadam 	ishtp_cl_driver_unregister(&loader_ishtp_cl_driver);
105591b22810SRushikesh S Kadam }
105691b22810SRushikesh S Kadam 
105791b22810SRushikesh S Kadam late_initcall(ish_loader_init);
105891b22810SRushikesh S Kadam module_exit(ish_loader_exit);
105991b22810SRushikesh S Kadam 
106091b22810SRushikesh S Kadam module_param(dma_buf_size_limit, int, 0644);
106191b22810SRushikesh S Kadam MODULE_PARM_DESC(dma_buf_size_limit, "Limit the DMA buf size to this value in bytes");
106291b22810SRushikesh S Kadam 
106391b22810SRushikesh S Kadam MODULE_DESCRIPTION("ISH ISH-TP Host firmware Loader Client Driver");
106491b22810SRushikesh S Kadam MODULE_AUTHOR("Rushikesh S Kadam <rushikesh.s.kadam@intel.com>");
106591b22810SRushikesh S Kadam 
106691b22810SRushikesh S Kadam MODULE_LICENSE("GPL v2");
1067