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