xref: /openbmc/linux/drivers/net/wwan/iosm/iosm_ipc_imem_ops.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1edf6423cSM Chetan Kumar // SPDX-License-Identifier: GPL-2.0-only
2edf6423cSM Chetan Kumar /*
3edf6423cSM Chetan Kumar  * Copyright (C) 2020-21 Intel Corporation.
4edf6423cSM Chetan Kumar  */
5edf6423cSM Chetan Kumar 
6edf6423cSM Chetan Kumar #include <linux/delay.h>
7edf6423cSM Chetan Kumar 
8edf6423cSM Chetan Kumar #include "iosm_ipc_chnl_cfg.h"
98d9be063SM Chetan Kumar #include "iosm_ipc_devlink.h"
10edf6423cSM Chetan Kumar #include "iosm_ipc_imem.h"
11edf6423cSM Chetan Kumar #include "iosm_ipc_imem_ops.h"
12edf6423cSM Chetan Kumar #include "iosm_ipc_port.h"
13edf6423cSM Chetan Kumar #include "iosm_ipc_task_queue.h"
14edf6423cSM Chetan Kumar 
15edf6423cSM Chetan Kumar /* Open a packet data online channel between the network layer and CP. */
ipc_imem_sys_wwan_open(struct iosm_imem * ipc_imem,int if_id)16edf6423cSM Chetan Kumar int ipc_imem_sys_wwan_open(struct iosm_imem *ipc_imem, int if_id)
17edf6423cSM Chetan Kumar {
18edf6423cSM Chetan Kumar 	dev_dbg(ipc_imem->dev, "%s if id: %d",
19edf6423cSM Chetan Kumar 		ipc_imem_phase_get_string(ipc_imem->phase), if_id);
20edf6423cSM Chetan Kumar 
21edf6423cSM Chetan Kumar 	/* The network interface is only supported in the runtime phase. */
22edf6423cSM Chetan Kumar 	if (ipc_imem_phase_update(ipc_imem) != IPC_P_RUN) {
23edf6423cSM Chetan Kumar 		dev_err(ipc_imem->dev, "net:%d : refused phase %s", if_id,
24edf6423cSM Chetan Kumar 			ipc_imem_phase_get_string(ipc_imem->phase));
25edf6423cSM Chetan Kumar 		return -EIO;
26edf6423cSM Chetan Kumar 	}
27edf6423cSM Chetan Kumar 
285bb4eea0SM Chetan Kumar 	return ipc_mux_open_session(ipc_imem->mux, if_id);
29edf6423cSM Chetan Kumar }
30edf6423cSM Chetan Kumar 
31edf6423cSM Chetan Kumar /* Release a net link to CP. */
ipc_imem_sys_wwan_close(struct iosm_imem * ipc_imem,int if_id,int channel_id)32edf6423cSM Chetan Kumar void ipc_imem_sys_wwan_close(struct iosm_imem *ipc_imem, int if_id,
33edf6423cSM Chetan Kumar 			     int channel_id)
34edf6423cSM Chetan Kumar {
35edf6423cSM Chetan Kumar 	if (ipc_imem->mux && if_id >= IP_MUX_SESSION_START &&
36edf6423cSM Chetan Kumar 	    if_id <= IP_MUX_SESSION_END)
375bb4eea0SM Chetan Kumar 		ipc_mux_close_session(ipc_imem->mux, if_id);
38edf6423cSM Chetan Kumar }
39edf6423cSM Chetan Kumar 
40edf6423cSM Chetan Kumar /* Tasklet call to do uplink transfer. */
ipc_imem_tq_cdev_write(struct iosm_imem * ipc_imem,int arg,void * msg,size_t size)41edf6423cSM Chetan Kumar static int ipc_imem_tq_cdev_write(struct iosm_imem *ipc_imem, int arg,
42edf6423cSM Chetan Kumar 				  void *msg, size_t size)
43edf6423cSM Chetan Kumar {
44edf6423cSM Chetan Kumar 	ipc_imem_ul_send(ipc_imem);
45edf6423cSM Chetan Kumar 
46edf6423cSM Chetan Kumar 	return 0;
47edf6423cSM Chetan Kumar }
48edf6423cSM Chetan Kumar 
49edf6423cSM Chetan Kumar /* Through tasklet to do sio write. */
ipc_imem_call_cdev_write(struct iosm_imem * ipc_imem)50edf6423cSM Chetan Kumar static int ipc_imem_call_cdev_write(struct iosm_imem *ipc_imem)
51edf6423cSM Chetan Kumar {
52edf6423cSM Chetan Kumar 	return ipc_task_queue_send_task(ipc_imem, ipc_imem_tq_cdev_write, 0,
53edf6423cSM Chetan Kumar 					NULL, 0, false);
54edf6423cSM Chetan Kumar }
55edf6423cSM Chetan Kumar 
56edf6423cSM Chetan Kumar /* Function for transfer UL data */
ipc_imem_sys_wwan_transmit(struct iosm_imem * ipc_imem,int if_id,int channel_id,struct sk_buff * skb)57edf6423cSM Chetan Kumar int ipc_imem_sys_wwan_transmit(struct iosm_imem *ipc_imem,
58edf6423cSM Chetan Kumar 			       int if_id, int channel_id, struct sk_buff *skb)
59edf6423cSM Chetan Kumar {
60edf6423cSM Chetan Kumar 	int ret = -EINVAL;
61edf6423cSM Chetan Kumar 
62edf6423cSM Chetan Kumar 	if (!ipc_imem || channel_id < 0)
63edf6423cSM Chetan Kumar 		goto out;
64edf6423cSM Chetan Kumar 
65edf6423cSM Chetan Kumar 	/* Is CP Running? */
66edf6423cSM Chetan Kumar 	if (ipc_imem->phase != IPC_P_RUN) {
67edf6423cSM Chetan Kumar 		dev_dbg(ipc_imem->dev, "phase %s transmit",
68edf6423cSM Chetan Kumar 			ipc_imem_phase_get_string(ipc_imem->phase));
69edf6423cSM Chetan Kumar 		ret = -EIO;
70edf6423cSM Chetan Kumar 		goto out;
71edf6423cSM Chetan Kumar 	}
72edf6423cSM Chetan Kumar 
73edf6423cSM Chetan Kumar 	/* Route the UL packet through IP MUX Layer */
745bb4eea0SM Chetan Kumar 	ret = ipc_mux_ul_trigger_encode(ipc_imem->mux, if_id, skb);
75edf6423cSM Chetan Kumar out:
76edf6423cSM Chetan Kumar 	return ret;
77edf6423cSM Chetan Kumar }
78edf6423cSM Chetan Kumar 
79edf6423cSM Chetan Kumar /* Initialize wwan channel */
ipc_imem_wwan_channel_init(struct iosm_imem * ipc_imem,enum ipc_mux_protocol mux_type)80*60829145SM Chetan Kumar int ipc_imem_wwan_channel_init(struct iosm_imem *ipc_imem,
81edf6423cSM Chetan Kumar 			       enum ipc_mux_protocol mux_type)
82edf6423cSM Chetan Kumar {
83edf6423cSM Chetan Kumar 	struct ipc_chnl_cfg chnl_cfg = { 0 };
84edf6423cSM Chetan Kumar 
85edf6423cSM Chetan Kumar 	ipc_imem->cp_version = ipc_mmio_get_cp_version(ipc_imem->mmio);
86edf6423cSM Chetan Kumar 
87edf6423cSM Chetan Kumar 	/* If modem version is invalid (0xffffffff), do not initialize WWAN. */
88edf6423cSM Chetan Kumar 	if (ipc_imem->cp_version == -1) {
89edf6423cSM Chetan Kumar 		dev_err(ipc_imem->dev, "invalid CP version");
90*60829145SM Chetan Kumar 		return -EIO;
91edf6423cSM Chetan Kumar 	}
92edf6423cSM Chetan Kumar 
93edf6423cSM Chetan Kumar 	ipc_chnl_cfg_get(&chnl_cfg, ipc_imem->nr_of_channels);
9402d2d2eaSM Chetan Kumar 
9502d2d2eaSM Chetan Kumar 	if (ipc_imem->mmio->mux_protocol == MUX_AGGREGATION &&
9602d2d2eaSM Chetan Kumar 	    ipc_imem->nr_of_channels == IPC_MEM_IP_CHL_ID_0) {
9702d2d2eaSM Chetan Kumar 		chnl_cfg.ul_nr_of_entries = IPC_MEM_MAX_TDS_MUX_AGGR_UL;
9802d2d2eaSM Chetan Kumar 		chnl_cfg.dl_nr_of_entries = IPC_MEM_MAX_TDS_MUX_AGGR_DL;
9902d2d2eaSM Chetan Kumar 		chnl_cfg.dl_buf_size = IPC_MEM_MAX_ADB_BUF_SIZE;
10002d2d2eaSM Chetan Kumar 	}
10102d2d2eaSM Chetan Kumar 
102edf6423cSM Chetan Kumar 	ipc_imem_channel_init(ipc_imem, IPC_CTYPE_WWAN, chnl_cfg,
103edf6423cSM Chetan Kumar 			      IRQ_MOD_OFF);
104edf6423cSM Chetan Kumar 
105edf6423cSM Chetan Kumar 	/* WWAN registration. */
106edf6423cSM Chetan Kumar 	ipc_imem->wwan = ipc_wwan_init(ipc_imem, ipc_imem->dev);
107*60829145SM Chetan Kumar 	if (!ipc_imem->wwan) {
108edf6423cSM Chetan Kumar 		dev_err(ipc_imem->dev,
109edf6423cSM Chetan Kumar 			"failed to register the ipc_wwan interfaces");
110*60829145SM Chetan Kumar 		return -ENOMEM;
111*60829145SM Chetan Kumar 	}
112*60829145SM Chetan Kumar 
113*60829145SM Chetan Kumar 	return 0;
114edf6423cSM Chetan Kumar }
115edf6423cSM Chetan Kumar 
116edf6423cSM Chetan Kumar /* Map SKB to DMA for transfer */
ipc_imem_map_skb_to_dma(struct iosm_imem * ipc_imem,struct sk_buff * skb)117edf6423cSM Chetan Kumar static int ipc_imem_map_skb_to_dma(struct iosm_imem *ipc_imem,
118edf6423cSM Chetan Kumar 				   struct sk_buff *skb)
119edf6423cSM Chetan Kumar {
120edf6423cSM Chetan Kumar 	struct iosm_pcie *ipc_pcie = ipc_imem->pcie;
121edf6423cSM Chetan Kumar 	char *buf = skb->data;
122edf6423cSM Chetan Kumar 	int len = skb->len;
123edf6423cSM Chetan Kumar 	dma_addr_t mapping;
124edf6423cSM Chetan Kumar 	int ret;
125edf6423cSM Chetan Kumar 
126edf6423cSM Chetan Kumar 	ret = ipc_pcie_addr_map(ipc_pcie, buf, len, &mapping, DMA_TO_DEVICE);
127edf6423cSM Chetan Kumar 
128edf6423cSM Chetan Kumar 	if (ret)
129edf6423cSM Chetan Kumar 		goto err;
130edf6423cSM Chetan Kumar 
131edf6423cSM Chetan Kumar 	BUILD_BUG_ON(sizeof(*IPC_CB(skb)) > sizeof(skb->cb));
132edf6423cSM Chetan Kumar 
133edf6423cSM Chetan Kumar 	IPC_CB(skb)->mapping = mapping;
134edf6423cSM Chetan Kumar 	IPC_CB(skb)->direction = DMA_TO_DEVICE;
135edf6423cSM Chetan Kumar 	IPC_CB(skb)->len = len;
136edf6423cSM Chetan Kumar 	IPC_CB(skb)->op_type = (u8)UL_DEFAULT;
137edf6423cSM Chetan Kumar 
138edf6423cSM Chetan Kumar err:
139edf6423cSM Chetan Kumar 	return ret;
140edf6423cSM Chetan Kumar }
141edf6423cSM Chetan Kumar 
142edf6423cSM Chetan Kumar /* return true if channel is ready for use */
ipc_imem_is_channel_active(struct iosm_imem * ipc_imem,struct ipc_mem_channel * channel)143edf6423cSM Chetan Kumar static bool ipc_imem_is_channel_active(struct iosm_imem *ipc_imem,
144edf6423cSM Chetan Kumar 				       struct ipc_mem_channel *channel)
145edf6423cSM Chetan Kumar {
146edf6423cSM Chetan Kumar 	enum ipc_phase phase;
147edf6423cSM Chetan Kumar 
148edf6423cSM Chetan Kumar 	/* Update the current operation phase. */
149edf6423cSM Chetan Kumar 	phase = ipc_imem->phase;
150edf6423cSM Chetan Kumar 
151edf6423cSM Chetan Kumar 	/* Select the operation depending on the execution stage. */
152edf6423cSM Chetan Kumar 	switch (phase) {
153edf6423cSM Chetan Kumar 	case IPC_P_RUN:
154edf6423cSM Chetan Kumar 	case IPC_P_PSI:
155edf6423cSM Chetan Kumar 	case IPC_P_EBL:
156edf6423cSM Chetan Kumar 		break;
157edf6423cSM Chetan Kumar 
158edf6423cSM Chetan Kumar 	case IPC_P_ROM:
159edf6423cSM Chetan Kumar 		/* Prepare the PSI image for the CP ROM driver and
160edf6423cSM Chetan Kumar 		 * suspend the flash app.
161edf6423cSM Chetan Kumar 		 */
162edf6423cSM Chetan Kumar 		if (channel->state != IMEM_CHANNEL_RESERVED) {
163edf6423cSM Chetan Kumar 			dev_err(ipc_imem->dev,
164edf6423cSM Chetan Kumar 				"ch[%d]:invalid channel state %d,expected %d",
165edf6423cSM Chetan Kumar 				channel->channel_id, channel->state,
166edf6423cSM Chetan Kumar 				IMEM_CHANNEL_RESERVED);
167edf6423cSM Chetan Kumar 			goto channel_unavailable;
168edf6423cSM Chetan Kumar 		}
169edf6423cSM Chetan Kumar 		goto channel_available;
170edf6423cSM Chetan Kumar 
171edf6423cSM Chetan Kumar 	default:
172edf6423cSM Chetan Kumar 		/* Ignore uplink actions in all other phases. */
173edf6423cSM Chetan Kumar 		dev_err(ipc_imem->dev, "ch[%d]: confused phase %d",
174edf6423cSM Chetan Kumar 			channel->channel_id, phase);
175edf6423cSM Chetan Kumar 		goto channel_unavailable;
176edf6423cSM Chetan Kumar 	}
177edf6423cSM Chetan Kumar 	/* Check the full availability of the channel. */
178edf6423cSM Chetan Kumar 	if (channel->state != IMEM_CHANNEL_ACTIVE) {
179edf6423cSM Chetan Kumar 		dev_err(ipc_imem->dev, "ch[%d]: confused channel state %d",
180edf6423cSM Chetan Kumar 			channel->channel_id, channel->state);
181edf6423cSM Chetan Kumar 		goto channel_unavailable;
182edf6423cSM Chetan Kumar 	}
183edf6423cSM Chetan Kumar 
184edf6423cSM Chetan Kumar channel_available:
185edf6423cSM Chetan Kumar 	return true;
186edf6423cSM Chetan Kumar 
187edf6423cSM Chetan Kumar channel_unavailable:
188edf6423cSM Chetan Kumar 	return false;
189edf6423cSM Chetan Kumar }
190edf6423cSM Chetan Kumar 
19100ef3256SM Chetan Kumar /**
19200ef3256SM Chetan Kumar  * ipc_imem_sys_port_close - Release a sio link to CP.
19300ef3256SM Chetan Kumar  * @ipc_imem:          Imem instance.
19400ef3256SM Chetan Kumar  * @channel:           Channel instance.
19500ef3256SM Chetan Kumar  */
ipc_imem_sys_port_close(struct iosm_imem * ipc_imem,struct ipc_mem_channel * channel)19600ef3256SM Chetan Kumar void ipc_imem_sys_port_close(struct iosm_imem *ipc_imem,
19700ef3256SM Chetan Kumar 			     struct ipc_mem_channel *channel)
198edf6423cSM Chetan Kumar {
199edf6423cSM Chetan Kumar 	enum ipc_phase curr_phase;
200edf6423cSM Chetan Kumar 	int status = 0;
201edf6423cSM Chetan Kumar 	u32 tail = 0;
202edf6423cSM Chetan Kumar 
203edf6423cSM Chetan Kumar 	curr_phase = ipc_imem->phase;
204edf6423cSM Chetan Kumar 
205edf6423cSM Chetan Kumar 	/* If current phase is IPC_P_OFF or SIO ID is -ve then
206edf6423cSM Chetan Kumar 	 * channel is already freed. Nothing to do.
207edf6423cSM Chetan Kumar 	 */
208edf6423cSM Chetan Kumar 	if (curr_phase == IPC_P_OFF) {
209edf6423cSM Chetan Kumar 		dev_err(ipc_imem->dev,
210edf6423cSM Chetan Kumar 			"nothing to do. Current Phase: %s",
211edf6423cSM Chetan Kumar 			ipc_imem_phase_get_string(curr_phase));
212edf6423cSM Chetan Kumar 		return;
213edf6423cSM Chetan Kumar 	}
214edf6423cSM Chetan Kumar 
215edf6423cSM Chetan Kumar 	if (channel->state == IMEM_CHANNEL_FREE) {
216edf6423cSM Chetan Kumar 		dev_err(ipc_imem->dev, "ch[%d]: invalid channel state %d",
217edf6423cSM Chetan Kumar 			channel->channel_id, channel->state);
218edf6423cSM Chetan Kumar 		return;
219edf6423cSM Chetan Kumar 	}
220edf6423cSM Chetan Kumar 
221edf6423cSM Chetan Kumar 	/* If there are any pending TDs then wait for Timeout/Completion before
222edf6423cSM Chetan Kumar 	 * closing pipe.
223edf6423cSM Chetan Kumar 	 */
224edf6423cSM Chetan Kumar 	if (channel->ul_pipe.old_tail != channel->ul_pipe.old_head) {
225edf6423cSM Chetan Kumar 		ipc_imem->app_notify_ul_pend = 1;
226edf6423cSM Chetan Kumar 
227edf6423cSM Chetan Kumar 		/* Suspend the user app and wait a certain time for processing
228edf6423cSM Chetan Kumar 		 * UL Data.
229edf6423cSM Chetan Kumar 		 */
230edf6423cSM Chetan Kumar 		status = wait_for_completion_interruptible_timeout
231edf6423cSM Chetan Kumar 			 (&ipc_imem->ul_pend_sem,
232edf6423cSM Chetan Kumar 			  msecs_to_jiffies(IPC_PEND_DATA_TIMEOUT));
233edf6423cSM Chetan Kumar 		if (status == 0) {
234edf6423cSM Chetan Kumar 			dev_dbg(ipc_imem->dev,
235edf6423cSM Chetan Kumar 				"Pend data Timeout UL-Pipe:%d Head:%d Tail:%d",
236edf6423cSM Chetan Kumar 				channel->ul_pipe.pipe_nr,
237edf6423cSM Chetan Kumar 				channel->ul_pipe.old_head,
238edf6423cSM Chetan Kumar 				channel->ul_pipe.old_tail);
239edf6423cSM Chetan Kumar 		}
240edf6423cSM Chetan Kumar 
241edf6423cSM Chetan Kumar 		ipc_imem->app_notify_ul_pend = 0;
242edf6423cSM Chetan Kumar 	}
243edf6423cSM Chetan Kumar 
244edf6423cSM Chetan Kumar 	/* If there are any pending TDs then wait for Timeout/Completion before
245edf6423cSM Chetan Kumar 	 * closing pipe.
246edf6423cSM Chetan Kumar 	 */
247edf6423cSM Chetan Kumar 	ipc_protocol_get_head_tail_index(ipc_imem->ipc_protocol,
248edf6423cSM Chetan Kumar 					 &channel->dl_pipe, NULL, &tail);
249edf6423cSM Chetan Kumar 
250edf6423cSM Chetan Kumar 	if (tail != channel->dl_pipe.old_tail) {
251edf6423cSM Chetan Kumar 		ipc_imem->app_notify_dl_pend = 1;
252edf6423cSM Chetan Kumar 
253edf6423cSM Chetan Kumar 		/* Suspend the user app and wait a certain time for processing
254edf6423cSM Chetan Kumar 		 * DL Data.
255edf6423cSM Chetan Kumar 		 */
256edf6423cSM Chetan Kumar 		status = wait_for_completion_interruptible_timeout
257edf6423cSM Chetan Kumar 			 (&ipc_imem->dl_pend_sem,
258edf6423cSM Chetan Kumar 			  msecs_to_jiffies(IPC_PEND_DATA_TIMEOUT));
259edf6423cSM Chetan Kumar 		if (status == 0) {
260edf6423cSM Chetan Kumar 			dev_dbg(ipc_imem->dev,
261edf6423cSM Chetan Kumar 				"Pend data Timeout DL-Pipe:%d Head:%d Tail:%d",
262edf6423cSM Chetan Kumar 				channel->dl_pipe.pipe_nr,
263edf6423cSM Chetan Kumar 				channel->dl_pipe.old_head,
264edf6423cSM Chetan Kumar 				channel->dl_pipe.old_tail);
265edf6423cSM Chetan Kumar 		}
266edf6423cSM Chetan Kumar 
267edf6423cSM Chetan Kumar 		ipc_imem->app_notify_dl_pend = 0;
268edf6423cSM Chetan Kumar 	}
269edf6423cSM Chetan Kumar 
270edf6423cSM Chetan Kumar 	/* Due to wait for completion in messages, there is a small window
271edf6423cSM Chetan Kumar 	 * between closing the pipe and updating the channel is closed. In this
272edf6423cSM Chetan Kumar 	 * small window there could be HP update from Host Driver. Hence update
273edf6423cSM Chetan Kumar 	 * the channel state as CLOSING to aviod unnecessary interrupt
274edf6423cSM Chetan Kumar 	 * towards CP.
275edf6423cSM Chetan Kumar 	 */
276edf6423cSM Chetan Kumar 	channel->state = IMEM_CHANNEL_CLOSING;
277edf6423cSM Chetan Kumar 
278edf6423cSM Chetan Kumar 	ipc_imem_pipe_close(ipc_imem, &channel->ul_pipe);
279edf6423cSM Chetan Kumar 	ipc_imem_pipe_close(ipc_imem, &channel->dl_pipe);
280edf6423cSM Chetan Kumar 
281edf6423cSM Chetan Kumar 	ipc_imem_channel_free(channel);
282edf6423cSM Chetan Kumar }
283edf6423cSM Chetan Kumar 
284edf6423cSM Chetan Kumar /* Open a PORT link to CP and return the channel */
ipc_imem_sys_port_open(struct iosm_imem * ipc_imem,int chl_id,int hp_id)285edf6423cSM Chetan Kumar struct ipc_mem_channel *ipc_imem_sys_port_open(struct iosm_imem *ipc_imem,
286edf6423cSM Chetan Kumar 					       int chl_id, int hp_id)
287edf6423cSM Chetan Kumar {
288edf6423cSM Chetan Kumar 	struct ipc_mem_channel *channel;
289edf6423cSM Chetan Kumar 	int ch_id;
290edf6423cSM Chetan Kumar 
291edf6423cSM Chetan Kumar 	/* The PORT interface is only supported in the runtime phase. */
292edf6423cSM Chetan Kumar 	if (ipc_imem_phase_update(ipc_imem) != IPC_P_RUN) {
293edf6423cSM Chetan Kumar 		dev_err(ipc_imem->dev, "PORT open refused, phase %s",
294edf6423cSM Chetan Kumar 			ipc_imem_phase_get_string(ipc_imem->phase));
295edf6423cSM Chetan Kumar 		return NULL;
296edf6423cSM Chetan Kumar 	}
297edf6423cSM Chetan Kumar 
298edf6423cSM Chetan Kumar 	ch_id = ipc_imem_channel_alloc(ipc_imem, chl_id, IPC_CTYPE_CTRL);
299edf6423cSM Chetan Kumar 
300edf6423cSM Chetan Kumar 	if (ch_id < 0) {
301edf6423cSM Chetan Kumar 		dev_err(ipc_imem->dev, "reservation of an PORT chnl id failed");
302edf6423cSM Chetan Kumar 		return NULL;
303edf6423cSM Chetan Kumar 	}
304edf6423cSM Chetan Kumar 
305edf6423cSM Chetan Kumar 	channel = ipc_imem_channel_open(ipc_imem, ch_id, hp_id);
306edf6423cSM Chetan Kumar 
307edf6423cSM Chetan Kumar 	if (!channel) {
308edf6423cSM Chetan Kumar 		dev_err(ipc_imem->dev, "PORT channel id open failed");
309edf6423cSM Chetan Kumar 		return NULL;
310edf6423cSM Chetan Kumar 	}
311edf6423cSM Chetan Kumar 
312edf6423cSM Chetan Kumar 	return channel;
313edf6423cSM Chetan Kumar }
314edf6423cSM Chetan Kumar 
315edf6423cSM Chetan Kumar /* transfer skb to modem */
ipc_imem_sys_cdev_write(struct iosm_cdev * ipc_cdev,struct sk_buff * skb)316edf6423cSM Chetan Kumar int ipc_imem_sys_cdev_write(struct iosm_cdev *ipc_cdev, struct sk_buff *skb)
317edf6423cSM Chetan Kumar {
318edf6423cSM Chetan Kumar 	struct ipc_mem_channel *channel = ipc_cdev->channel;
319edf6423cSM Chetan Kumar 	struct iosm_imem *ipc_imem = ipc_cdev->ipc_imem;
320edf6423cSM Chetan Kumar 	int ret = -EIO;
321edf6423cSM Chetan Kumar 
322edf6423cSM Chetan Kumar 	if (!ipc_imem_is_channel_active(ipc_imem, channel) ||
323edf6423cSM Chetan Kumar 	    ipc_imem->phase == IPC_P_OFF_REQ)
324edf6423cSM Chetan Kumar 		goto out;
325edf6423cSM Chetan Kumar 
326edf6423cSM Chetan Kumar 	ret = ipc_imem_map_skb_to_dma(ipc_imem, skb);
327edf6423cSM Chetan Kumar 
328edf6423cSM Chetan Kumar 	if (ret)
329edf6423cSM Chetan Kumar 		goto out;
330edf6423cSM Chetan Kumar 
331edf6423cSM Chetan Kumar 	/* Add skb to the uplink skbuf accumulator. */
332edf6423cSM Chetan Kumar 	skb_queue_tail(&channel->ul_list, skb);
333edf6423cSM Chetan Kumar 
334edf6423cSM Chetan Kumar 	ret = ipc_imem_call_cdev_write(ipc_imem);
335edf6423cSM Chetan Kumar 
336edf6423cSM Chetan Kumar 	if (ret) {
337edf6423cSM Chetan Kumar 		skb_dequeue_tail(&channel->ul_list);
338edf6423cSM Chetan Kumar 		dev_err(ipc_cdev->dev, "channel id[%d] write failed\n",
339edf6423cSM Chetan Kumar 			ipc_cdev->channel->channel_id);
340edf6423cSM Chetan Kumar 	}
341edf6423cSM Chetan Kumar out:
342edf6423cSM Chetan Kumar 	return ret;
343edf6423cSM Chetan Kumar }
3448d9be063SM Chetan Kumar 
3458d9be063SM Chetan Kumar /* Open a SIO link to CP and return the channel instance */
ipc_imem_sys_devlink_open(struct iosm_imem * ipc_imem)3468d9be063SM Chetan Kumar struct ipc_mem_channel *ipc_imem_sys_devlink_open(struct iosm_imem *ipc_imem)
3478d9be063SM Chetan Kumar {
3488d9be063SM Chetan Kumar 	struct ipc_mem_channel *channel;
3498d9be063SM Chetan Kumar 	enum ipc_phase phase;
3508d9be063SM Chetan Kumar 	int channel_id;
3518d9be063SM Chetan Kumar 
3528d9be063SM Chetan Kumar 	phase = ipc_imem_phase_update(ipc_imem);
3538d9be063SM Chetan Kumar 	switch (phase) {
3548d9be063SM Chetan Kumar 	case IPC_P_OFF:
3558d9be063SM Chetan Kumar 	case IPC_P_ROM:
3568d9be063SM Chetan Kumar 		/* Get a channel id as flash id and reserve it. */
3578d9be063SM Chetan Kumar 		channel_id = ipc_imem_channel_alloc(ipc_imem,
3588d9be063SM Chetan Kumar 						    IPC_MEM_CTRL_CHL_ID_7,
3598d9be063SM Chetan Kumar 						    IPC_CTYPE_CTRL);
3608d9be063SM Chetan Kumar 
3618d9be063SM Chetan Kumar 		if (channel_id < 0) {
3628d9be063SM Chetan Kumar 			dev_err(ipc_imem->dev,
3638d9be063SM Chetan Kumar 				"reservation of a flash channel id failed");
3648d9be063SM Chetan Kumar 			goto error;
3658d9be063SM Chetan Kumar 		}
3668d9be063SM Chetan Kumar 
3678d9be063SM Chetan Kumar 		ipc_imem->ipc_devlink->devlink_sio.channel_id = channel_id;
3688d9be063SM Chetan Kumar 		channel = &ipc_imem->channels[channel_id];
3698d9be063SM Chetan Kumar 
3708d9be063SM Chetan Kumar 		/* Enqueue chip info data to be read */
3718d9be063SM Chetan Kumar 		if (ipc_imem_devlink_trigger_chip_info(ipc_imem)) {
3728d9be063SM Chetan Kumar 			dev_err(ipc_imem->dev, "Enqueue of chip info failed");
3738d9be063SM Chetan Kumar 			channel->state = IMEM_CHANNEL_FREE;
3748d9be063SM Chetan Kumar 			goto error;
3758d9be063SM Chetan Kumar 		}
3768d9be063SM Chetan Kumar 
3778d9be063SM Chetan Kumar 		return channel;
3788d9be063SM Chetan Kumar 
3798d9be063SM Chetan Kumar 	case IPC_P_PSI:
3808d9be063SM Chetan Kumar 	case IPC_P_EBL:
3818d9be063SM Chetan Kumar 		ipc_imem->cp_version = ipc_mmio_get_cp_version(ipc_imem->mmio);
3828d9be063SM Chetan Kumar 		if (ipc_imem->cp_version == -1) {
3838d9be063SM Chetan Kumar 			dev_err(ipc_imem->dev, "invalid CP version");
3848d9be063SM Chetan Kumar 			goto error;
3858d9be063SM Chetan Kumar 		}
3868d9be063SM Chetan Kumar 
3878d9be063SM Chetan Kumar 		channel_id = ipc_imem->ipc_devlink->devlink_sio.channel_id;
3888d9be063SM Chetan Kumar 		return ipc_imem_channel_open(ipc_imem, channel_id,
3898d9be063SM Chetan Kumar 					     IPC_HP_CDEV_OPEN);
3908d9be063SM Chetan Kumar 
3918d9be063SM Chetan Kumar 	default:
3928d9be063SM Chetan Kumar 		/* CP is in the wrong state (e.g. CRASH or CD_READY) */
3938d9be063SM Chetan Kumar 		dev_err(ipc_imem->dev, "SIO open refused, phase %d", phase);
3948d9be063SM Chetan Kumar 	}
3958d9be063SM Chetan Kumar error:
3968d9be063SM Chetan Kumar 	return NULL;
3978d9be063SM Chetan Kumar }
3988d9be063SM Chetan Kumar 
3998d9be063SM Chetan Kumar /* Release a SIO channel link to CP. */
ipc_imem_sys_devlink_close(struct iosm_devlink * ipc_devlink)4008d9be063SM Chetan Kumar void ipc_imem_sys_devlink_close(struct iosm_devlink *ipc_devlink)
4018d9be063SM Chetan Kumar {
4028d9be063SM Chetan Kumar 	struct iosm_imem *ipc_imem = ipc_devlink->pcie->imem;
4038d9be063SM Chetan Kumar 	int boot_check_timeout = BOOT_CHECK_DEFAULT_TIMEOUT;
4048d9be063SM Chetan Kumar 	enum ipc_mem_exec_stage exec_stage;
4058d9be063SM Chetan Kumar 	struct ipc_mem_channel *channel;
4068d9be063SM Chetan Kumar 	int status = 0;
4078d9be063SM Chetan Kumar 	u32 tail = 0;
4088d9be063SM Chetan Kumar 
4098d9be063SM Chetan Kumar 	channel = ipc_imem->ipc_devlink->devlink_sio.channel;
4108d9be063SM Chetan Kumar 	/* Increase the total wait time to boot_check_timeout */
4118d9be063SM Chetan Kumar 	do {
4128d9be063SM Chetan Kumar 		exec_stage = ipc_mmio_get_exec_stage(ipc_imem->mmio);
4138d9be063SM Chetan Kumar 		if (exec_stage == IPC_MEM_EXEC_STAGE_RUN ||
4148d9be063SM Chetan Kumar 		    exec_stage == IPC_MEM_EXEC_STAGE_PSI)
4158d9be063SM Chetan Kumar 			break;
4168d9be063SM Chetan Kumar 		msleep(20);
4178d9be063SM Chetan Kumar 		boot_check_timeout -= 20;
4188d9be063SM Chetan Kumar 	} while (boot_check_timeout > 0);
4198d9be063SM Chetan Kumar 
4208d9be063SM Chetan Kumar 	/* If there are any pending TDs then wait for Timeout/Completion before
4218d9be063SM Chetan Kumar 	 * closing pipe.
4228d9be063SM Chetan Kumar 	 */
4238d9be063SM Chetan Kumar 	if (channel->ul_pipe.old_tail != channel->ul_pipe.old_head) {
4248d9be063SM Chetan Kumar 		status = wait_for_completion_interruptible_timeout
4258d9be063SM Chetan Kumar 			(&ipc_imem->ul_pend_sem,
4268d9be063SM Chetan Kumar 			 msecs_to_jiffies(IPC_PEND_DATA_TIMEOUT));
4278d9be063SM Chetan Kumar 		if (status == 0) {
4288d9be063SM Chetan Kumar 			dev_dbg(ipc_imem->dev,
4298d9be063SM Chetan Kumar 				"Data Timeout on UL-Pipe:%d Head:%d Tail:%d",
4308d9be063SM Chetan Kumar 				channel->ul_pipe.pipe_nr,
4318d9be063SM Chetan Kumar 				channel->ul_pipe.old_head,
4328d9be063SM Chetan Kumar 				channel->ul_pipe.old_tail);
4338d9be063SM Chetan Kumar 		}
4348d9be063SM Chetan Kumar 	}
4358d9be063SM Chetan Kumar 
4368d9be063SM Chetan Kumar 	ipc_protocol_get_head_tail_index(ipc_imem->ipc_protocol,
4378d9be063SM Chetan Kumar 					 &channel->dl_pipe, NULL, &tail);
4388d9be063SM Chetan Kumar 
4398d9be063SM Chetan Kumar 	if (tail != channel->dl_pipe.old_tail) {
4408d9be063SM Chetan Kumar 		status = wait_for_completion_interruptible_timeout
4418d9be063SM Chetan Kumar 			(&ipc_imem->dl_pend_sem,
4428d9be063SM Chetan Kumar 			 msecs_to_jiffies(IPC_PEND_DATA_TIMEOUT));
4438d9be063SM Chetan Kumar 		if (status == 0) {
4448d9be063SM Chetan Kumar 			dev_dbg(ipc_imem->dev,
4458d9be063SM Chetan Kumar 				"Data Timeout on DL-Pipe:%d Head:%d Tail:%d",
4468d9be063SM Chetan Kumar 				channel->dl_pipe.pipe_nr,
4478d9be063SM Chetan Kumar 				channel->dl_pipe.old_head,
4488d9be063SM Chetan Kumar 				channel->dl_pipe.old_tail);
4498d9be063SM Chetan Kumar 		}
4508d9be063SM Chetan Kumar 	}
4518d9be063SM Chetan Kumar 
4528d9be063SM Chetan Kumar 	/* Due to wait for completion in messages, there is a small window
4538d9be063SM Chetan Kumar 	 * between closing the pipe and updating the channel is closed. In this
4548d9be063SM Chetan Kumar 	 * small window there could be HP update from Host Driver. Hence update
4558d9be063SM Chetan Kumar 	 * the channel state as CLOSING to aviod unnecessary interrupt
4568d9be063SM Chetan Kumar 	 * towards CP.
4578d9be063SM Chetan Kumar 	 */
4588d9be063SM Chetan Kumar 	channel->state = IMEM_CHANNEL_CLOSING;
4598d9be063SM Chetan Kumar 	/* Release the pipe resources */
4608d9be063SM Chetan Kumar 	ipc_imem_pipe_cleanup(ipc_imem, &channel->ul_pipe);
4618d9be063SM Chetan Kumar 	ipc_imem_pipe_cleanup(ipc_imem, &channel->dl_pipe);
46207d3f274SM Chetan Kumar 	ipc_imem->nr_of_channels--;
4638d9be063SM Chetan Kumar }
4648d9be063SM Chetan Kumar 
ipc_imem_sys_devlink_notify_rx(struct iosm_devlink * ipc_devlink,struct sk_buff * skb)4658d9be063SM Chetan Kumar void ipc_imem_sys_devlink_notify_rx(struct iosm_devlink *ipc_devlink,
4668d9be063SM Chetan Kumar 				    struct sk_buff *skb)
4678d9be063SM Chetan Kumar {
4688d9be063SM Chetan Kumar 	skb_queue_tail(&ipc_devlink->devlink_sio.rx_list, skb);
4698d9be063SM Chetan Kumar 	complete(&ipc_devlink->devlink_sio.read_sem);
4708d9be063SM Chetan Kumar }
4718d9be063SM Chetan Kumar 
4728d9be063SM Chetan Kumar /* PSI transfer */
ipc_imem_sys_psi_transfer(struct iosm_imem * ipc_imem,struct ipc_mem_channel * channel,unsigned char * buf,int count)4738d9be063SM Chetan Kumar static int ipc_imem_sys_psi_transfer(struct iosm_imem *ipc_imem,
4748d9be063SM Chetan Kumar 				     struct ipc_mem_channel *channel,
4758d9be063SM Chetan Kumar 				     unsigned char *buf, int count)
4768d9be063SM Chetan Kumar {
4778d9be063SM Chetan Kumar 	int psi_start_timeout = PSI_START_DEFAULT_TIMEOUT;
4788d9be063SM Chetan Kumar 	enum ipc_mem_exec_stage exec_stage;
4798d9be063SM Chetan Kumar 
4808d9be063SM Chetan Kumar 	dma_addr_t mapping = 0;
4818d9be063SM Chetan Kumar 	int ret;
4828d9be063SM Chetan Kumar 
4838d9be063SM Chetan Kumar 	ret = ipc_pcie_addr_map(ipc_imem->pcie, buf, count, &mapping,
4848d9be063SM Chetan Kumar 				DMA_TO_DEVICE);
4858d9be063SM Chetan Kumar 	if (ret)
4868d9be063SM Chetan Kumar 		goto pcie_addr_map_fail;
4878d9be063SM Chetan Kumar 
4888d9be063SM Chetan Kumar 	/* Save the PSI information for the CP ROM driver on the doorbell
4898d9be063SM Chetan Kumar 	 * scratchpad.
4908d9be063SM Chetan Kumar 	 */
4918d9be063SM Chetan Kumar 	ipc_mmio_set_psi_addr_and_size(ipc_imem->mmio, mapping, count);
4928d9be063SM Chetan Kumar 	ipc_doorbell_fire(ipc_imem->pcie, 0, IPC_MEM_EXEC_STAGE_BOOT);
4938d9be063SM Chetan Kumar 
4948d9be063SM Chetan Kumar 	ret = wait_for_completion_interruptible_timeout
4958d9be063SM Chetan Kumar 		(&channel->ul_sem,
4968d9be063SM Chetan Kumar 		 msecs_to_jiffies(IPC_PSI_TRANSFER_TIMEOUT));
4978d9be063SM Chetan Kumar 
4988d9be063SM Chetan Kumar 	if (ret <= 0) {
4998d9be063SM Chetan Kumar 		dev_err(ipc_imem->dev, "Failed PSI transfer to CP, Error-%d",
5008d9be063SM Chetan Kumar 			ret);
5018d9be063SM Chetan Kumar 		goto psi_transfer_fail;
5028d9be063SM Chetan Kumar 	}
5038d9be063SM Chetan Kumar 	/* If the PSI download fails, return the CP boot ROM exit code */
5048d9be063SM Chetan Kumar 	if (ipc_imem->rom_exit_code != IMEM_ROM_EXIT_OPEN_EXT &&
5058d9be063SM Chetan Kumar 	    ipc_imem->rom_exit_code != IMEM_ROM_EXIT_CERT_EXT) {
5068d9be063SM Chetan Kumar 		ret = (-1) * ((int)ipc_imem->rom_exit_code);
5078d9be063SM Chetan Kumar 		goto psi_transfer_fail;
5088d9be063SM Chetan Kumar 	}
5098d9be063SM Chetan Kumar 
5108d9be063SM Chetan Kumar 	dev_dbg(ipc_imem->dev, "PSI image successfully downloaded");
5118d9be063SM Chetan Kumar 
5128d9be063SM Chetan Kumar 	/* Wait psi_start_timeout milliseconds until the CP PSI image is
5138d9be063SM Chetan Kumar 	 * running and updates the execution_stage field with
5148d9be063SM Chetan Kumar 	 * IPC_MEM_EXEC_STAGE_PSI. Verify the execution stage.
5158d9be063SM Chetan Kumar 	 */
5168d9be063SM Chetan Kumar 	do {
5178d9be063SM Chetan Kumar 		exec_stage = ipc_mmio_get_exec_stage(ipc_imem->mmio);
5188d9be063SM Chetan Kumar 
5198d9be063SM Chetan Kumar 		if (exec_stage == IPC_MEM_EXEC_STAGE_PSI)
5208d9be063SM Chetan Kumar 			break;
5218d9be063SM Chetan Kumar 
5228d9be063SM Chetan Kumar 		msleep(20);
5238d9be063SM Chetan Kumar 		psi_start_timeout -= 20;
5248d9be063SM Chetan Kumar 	} while (psi_start_timeout > 0);
5258d9be063SM Chetan Kumar 
5268d9be063SM Chetan Kumar 	if (exec_stage != IPC_MEM_EXEC_STAGE_PSI)
5278d9be063SM Chetan Kumar 		goto psi_transfer_fail; /* Unknown status of CP PSI process. */
5288d9be063SM Chetan Kumar 
5298d9be063SM Chetan Kumar 	ipc_imem->phase = IPC_P_PSI;
5308d9be063SM Chetan Kumar 
5318d9be063SM Chetan Kumar 	/* Enter the PSI phase. */
5328d9be063SM Chetan Kumar 	dev_dbg(ipc_imem->dev, "execution_stage[%X] eq. PSI", exec_stage);
5338d9be063SM Chetan Kumar 
5348d9be063SM Chetan Kumar 	/* Request the RUNNING state from CP and wait until it was reached
5358d9be063SM Chetan Kumar 	 * or timeout.
5368d9be063SM Chetan Kumar 	 */
5378d9be063SM Chetan Kumar 	ipc_imem_ipc_init_check(ipc_imem);
5388d9be063SM Chetan Kumar 
5398d9be063SM Chetan Kumar 	ret = wait_for_completion_interruptible_timeout
5408d9be063SM Chetan Kumar 		(&channel->ul_sem, msecs_to_jiffies(IPC_PSI_TRANSFER_TIMEOUT));
5418d9be063SM Chetan Kumar 	if (ret <= 0) {
5428d9be063SM Chetan Kumar 		dev_err(ipc_imem->dev,
5438d9be063SM Chetan Kumar 			"Failed PSI RUNNING state on CP, Error-%d", ret);
5448d9be063SM Chetan Kumar 		goto psi_transfer_fail;
5458d9be063SM Chetan Kumar 	}
5468d9be063SM Chetan Kumar 
5478d9be063SM Chetan Kumar 	if (ipc_mmio_get_ipc_state(ipc_imem->mmio) !=
5488d9be063SM Chetan Kumar 			IPC_MEM_DEVICE_IPC_RUNNING) {
5498d9be063SM Chetan Kumar 		dev_err(ipc_imem->dev,
5508d9be063SM Chetan Kumar 			"ch[%d] %s: unexpected CP IPC state %d, not RUNNING",
5518d9be063SM Chetan Kumar 			channel->channel_id,
5528d9be063SM Chetan Kumar 			ipc_imem_phase_get_string(ipc_imem->phase),
5538d9be063SM Chetan Kumar 			ipc_mmio_get_ipc_state(ipc_imem->mmio));
5548d9be063SM Chetan Kumar 
5558d9be063SM Chetan Kumar 		goto psi_transfer_fail;
5568d9be063SM Chetan Kumar 	}
5578d9be063SM Chetan Kumar 
5588d9be063SM Chetan Kumar 	/* Create the flash channel for the transfer of the images. */
5598d9be063SM Chetan Kumar 	if (!ipc_imem_sys_devlink_open(ipc_imem)) {
5608d9be063SM Chetan Kumar 		dev_err(ipc_imem->dev, "can't open flash_channel");
5618d9be063SM Chetan Kumar 		goto psi_transfer_fail;
5628d9be063SM Chetan Kumar 	}
5638d9be063SM Chetan Kumar 
5648d9be063SM Chetan Kumar 	ret = 0;
5658d9be063SM Chetan Kumar psi_transfer_fail:
5668d9be063SM Chetan Kumar 	ipc_pcie_addr_unmap(ipc_imem->pcie, count, mapping, DMA_TO_DEVICE);
5678d9be063SM Chetan Kumar pcie_addr_map_fail:
5688d9be063SM Chetan Kumar 	return ret;
5698d9be063SM Chetan Kumar }
5708d9be063SM Chetan Kumar 
ipc_imem_sys_devlink_write(struct iosm_devlink * ipc_devlink,unsigned char * buf,int count)5718d9be063SM Chetan Kumar int ipc_imem_sys_devlink_write(struct iosm_devlink *ipc_devlink,
5728d9be063SM Chetan Kumar 			       unsigned char *buf, int count)
5738d9be063SM Chetan Kumar {
5748d9be063SM Chetan Kumar 	struct iosm_imem *ipc_imem = ipc_devlink->pcie->imem;
5758d9be063SM Chetan Kumar 	struct ipc_mem_channel *channel;
5768d9be063SM Chetan Kumar 	struct sk_buff *skb;
5778d9be063SM Chetan Kumar 	dma_addr_t mapping;
5788d9be063SM Chetan Kumar 	int ret;
5798d9be063SM Chetan Kumar 
5808d9be063SM Chetan Kumar 	channel = ipc_imem->ipc_devlink->devlink_sio.channel;
5818d9be063SM Chetan Kumar 
5828d9be063SM Chetan Kumar 	/* In the ROM phase the PSI image is passed to CP about a specific
5838d9be063SM Chetan Kumar 	 *  shared memory area and doorbell scratchpad directly.
5848d9be063SM Chetan Kumar 	 */
5858d9be063SM Chetan Kumar 	if (ipc_imem->phase == IPC_P_ROM) {
5868d9be063SM Chetan Kumar 		ret = ipc_imem_sys_psi_transfer(ipc_imem, channel, buf, count);
5878d9be063SM Chetan Kumar 		/* If the PSI transfer fails then send crash
5888d9be063SM Chetan Kumar 		 * Signature.
5898d9be063SM Chetan Kumar 		 */
5908d9be063SM Chetan Kumar 		if (ret > 0)
5918d9be063SM Chetan Kumar 			ipc_imem_msg_send_feature_set(ipc_imem,
5928d9be063SM Chetan Kumar 						      IPC_MEM_INBAND_CRASH_SIG,
5938d9be063SM Chetan Kumar 						      false);
5948d9be063SM Chetan Kumar 		goto out;
5958d9be063SM Chetan Kumar 	}
5968d9be063SM Chetan Kumar 
5978d9be063SM Chetan Kumar 	/* Allocate skb memory for the uplink buffer. */
5988d9be063SM Chetan Kumar 	skb = ipc_pcie_alloc_skb(ipc_devlink->pcie, count, GFP_KERNEL, &mapping,
5998d9be063SM Chetan Kumar 				 DMA_TO_DEVICE, 0);
6008d9be063SM Chetan Kumar 	if (!skb) {
6018d9be063SM Chetan Kumar 		ret = -ENOMEM;
6028d9be063SM Chetan Kumar 		goto out;
6038d9be063SM Chetan Kumar 	}
6048d9be063SM Chetan Kumar 
605f45892f7SShang XiaoJing 	skb_put_data(skb, buf, count);
6068d9be063SM Chetan Kumar 
6078d9be063SM Chetan Kumar 	IPC_CB(skb)->op_type = UL_USR_OP_BLOCKED;
6088d9be063SM Chetan Kumar 
6098d9be063SM Chetan Kumar 	/* Add skb to the uplink skbuf accumulator. */
6108d9be063SM Chetan Kumar 	skb_queue_tail(&channel->ul_list, skb);
6118d9be063SM Chetan Kumar 
6128d9be063SM Chetan Kumar 	/* Inform the IPC tasklet to pass uplink IP packets to CP. */
6138d9be063SM Chetan Kumar 	if (!ipc_imem_call_cdev_write(ipc_imem)) {
6148d9be063SM Chetan Kumar 		ret = wait_for_completion_interruptible(&channel->ul_sem);
6158d9be063SM Chetan Kumar 
6168d9be063SM Chetan Kumar 		if (ret < 0) {
6178d9be063SM Chetan Kumar 			dev_err(ipc_imem->dev,
6188d9be063SM Chetan Kumar 				"ch[%d] no CP confirmation, status = %d",
6198d9be063SM Chetan Kumar 				channel->channel_id, ret);
6208d9be063SM Chetan Kumar 			ipc_pcie_kfree_skb(ipc_devlink->pcie, skb);
6218d9be063SM Chetan Kumar 			goto out;
6228d9be063SM Chetan Kumar 		}
6238d9be063SM Chetan Kumar 	}
6248d9be063SM Chetan Kumar 	ret = 0;
6258d9be063SM Chetan Kumar out:
6268d9be063SM Chetan Kumar 	return ret;
6278d9be063SM Chetan Kumar }
6288d9be063SM Chetan Kumar 
ipc_imem_sys_devlink_read(struct iosm_devlink * devlink,u8 * data,u32 bytes_to_read,u32 * bytes_read)6298d9be063SM Chetan Kumar int ipc_imem_sys_devlink_read(struct iosm_devlink *devlink, u8 *data,
6308d9be063SM Chetan Kumar 			      u32 bytes_to_read, u32 *bytes_read)
6318d9be063SM Chetan Kumar {
6328d9be063SM Chetan Kumar 	struct sk_buff *skb = NULL;
6338d9be063SM Chetan Kumar 	int rc = 0;
6348d9be063SM Chetan Kumar 
6358d9be063SM Chetan Kumar 	/* check skb is available in rx_list or wait for skb */
6368d9be063SM Chetan Kumar 	devlink->devlink_sio.devlink_read_pend = 1;
6378d9be063SM Chetan Kumar 	while (!skb && !(skb = skb_dequeue(&devlink->devlink_sio.rx_list))) {
6388d9be063SM Chetan Kumar 		if (!wait_for_completion_interruptible_timeout
6398d9be063SM Chetan Kumar 				(&devlink->devlink_sio.read_sem,
6408d9be063SM Chetan Kumar 				 msecs_to_jiffies(IPC_READ_TIMEOUT))) {
6418d9be063SM Chetan Kumar 			dev_err(devlink->dev, "Read timedout");
6428d9be063SM Chetan Kumar 			rc =  -ETIMEDOUT;
6438d9be063SM Chetan Kumar 			goto devlink_read_fail;
6448d9be063SM Chetan Kumar 		}
6458d9be063SM Chetan Kumar 	}
6468d9be063SM Chetan Kumar 	devlink->devlink_sio.devlink_read_pend = 0;
6478d9be063SM Chetan Kumar 	if (bytes_to_read < skb->len) {
6488d9be063SM Chetan Kumar 		dev_err(devlink->dev, "Invalid size,expected len %d", skb->len);
6498d9be063SM Chetan Kumar 		rc = -EINVAL;
6508d9be063SM Chetan Kumar 		goto devlink_read_fail;
6518d9be063SM Chetan Kumar 	}
6528d9be063SM Chetan Kumar 	*bytes_read = skb->len;
6538d9be063SM Chetan Kumar 	memcpy(data, skb->data, skb->len);
6548d9be063SM Chetan Kumar 
6558d9be063SM Chetan Kumar devlink_read_fail:
65600ef3256SM Chetan Kumar 	dev_kfree_skb(skb);
6578d9be063SM Chetan Kumar 	return rc;
6588d9be063SM Chetan Kumar }
659