xref: /linux/drivers/bus/mhi/ep/main.c (revision 889600e21e3be388a6817c2a0dac0411df860751)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * MHI Endpoint bus stack
4  *
5  * Copyright (C) 2022 Linaro Ltd.
6  * Author: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
7  */
8 
9 #include <linux/bitfield.h>
10 #include <linux/delay.h>
11 #include <linux/dma-direction.h>
12 #include <linux/interrupt.h>
13 #include <linux/io.h>
14 #include <linux/irq.h>
15 #include <linux/mhi_ep.h>
16 #include <linux/module.h>
17 #include "internal.h"
18 
19 #define M0_WAIT_DELAY_MS	100
20 #define M0_WAIT_COUNT		100
21 
22 static DEFINE_IDA(mhi_ep_cntrl_ida);
23 
24 static int mhi_ep_create_device(struct mhi_ep_cntrl *mhi_cntrl, u32 ch_id);
25 static int mhi_ep_destroy_device(struct device *dev, void *data);
26 
mhi_ep_send_event(struct mhi_ep_cntrl * mhi_cntrl,u32 ring_idx,struct mhi_ring_element * el,bool bei)27 static int mhi_ep_send_event(struct mhi_ep_cntrl *mhi_cntrl, u32 ring_idx,
28 			     struct mhi_ring_element *el, bool bei)
29 {
30 	struct device *dev = &mhi_cntrl->mhi_dev->dev;
31 	union mhi_ep_ring_ctx *ctx;
32 	struct mhi_ep_ring *ring;
33 	int ret;
34 
35 	mutex_lock(&mhi_cntrl->event_lock);
36 	ring = &mhi_cntrl->mhi_event[ring_idx].ring;
37 	ctx = (union mhi_ep_ring_ctx *)&mhi_cntrl->ev_ctx_cache[ring_idx];
38 	if (!ring->started) {
39 		ret = mhi_ep_ring_start(mhi_cntrl, ring, ctx);
40 		if (ret) {
41 			dev_err(dev, "Error starting event ring (%u)\n", ring_idx);
42 			goto err_unlock;
43 		}
44 	}
45 
46 	/* Add element to the event ring */
47 	ret = mhi_ep_ring_add_element(ring, el);
48 	if (ret) {
49 		dev_err(dev, "Error adding element to event ring (%u)\n", ring_idx);
50 		goto err_unlock;
51 	}
52 
53 	mutex_unlock(&mhi_cntrl->event_lock);
54 
55 	/*
56 	 * As per the MHI specification, section 4.3, Interrupt moderation:
57 	 *
58 	 * 1. If BEI flag is not set, cancel any pending intmodt work if started
59 	 * for the event ring and raise IRQ immediately.
60 	 *
61 	 * 2. If both BEI and intmodt are set, and if no IRQ is pending for the
62 	 * same event ring, start the IRQ delayed work as per the value of
63 	 * intmodt. If previous IRQ is pending, then do nothing as the pending
64 	 * IRQ is enough for the host to process the current event ring element.
65 	 *
66 	 * 3. If BEI is set and intmodt is not set, no need to raise IRQ.
67 	 */
68 	if (!bei) {
69 		if (READ_ONCE(ring->irq_pending))
70 			cancel_delayed_work(&ring->intmodt_work);
71 
72 		mhi_cntrl->raise_irq(mhi_cntrl, ring->irq_vector);
73 	} else if (ring->intmodt && !READ_ONCE(ring->irq_pending)) {
74 		WRITE_ONCE(ring->irq_pending, true);
75 		schedule_delayed_work(&ring->intmodt_work, msecs_to_jiffies(ring->intmodt));
76 	}
77 
78 	return 0;
79 
80 err_unlock:
81 	mutex_unlock(&mhi_cntrl->event_lock);
82 
83 	return ret;
84 }
85 
mhi_ep_send_completion_event(struct mhi_ep_cntrl * mhi_cntrl,struct mhi_ep_ring * ring,struct mhi_ring_element * tre,u32 len,enum mhi_ev_ccs code)86 static int mhi_ep_send_completion_event(struct mhi_ep_cntrl *mhi_cntrl, struct mhi_ep_ring *ring,
87 					struct mhi_ring_element *tre, u32 len, enum mhi_ev_ccs code)
88 {
89 	struct mhi_ring_element *event;
90 	int ret;
91 
92 	event = kmem_cache_zalloc(mhi_cntrl->ev_ring_el_cache, GFP_KERNEL);
93 	if (!event)
94 		return -ENOMEM;
95 
96 	event->ptr = cpu_to_le64(ring->rbase + ring->rd_offset * sizeof(*tre));
97 	event->dword[0] = MHI_TRE_EV_DWORD0(code, len);
98 	event->dword[1] = MHI_TRE_EV_DWORD1(ring->ch_id, MHI_PKT_TYPE_TX_EVENT);
99 
100 	ret = mhi_ep_send_event(mhi_cntrl, ring->er_index, event, MHI_TRE_DATA_GET_BEI(tre));
101 	kmem_cache_free(mhi_cntrl->ev_ring_el_cache, event);
102 
103 	return ret;
104 }
105 
mhi_ep_send_state_change_event(struct mhi_ep_cntrl * mhi_cntrl,enum mhi_state state)106 int mhi_ep_send_state_change_event(struct mhi_ep_cntrl *mhi_cntrl, enum mhi_state state)
107 {
108 	struct mhi_ring_element *event;
109 	int ret;
110 
111 	event = kmem_cache_zalloc(mhi_cntrl->ev_ring_el_cache, GFP_KERNEL);
112 	if (!event)
113 		return -ENOMEM;
114 
115 	event->dword[0] = MHI_SC_EV_DWORD0(state);
116 	event->dword[1] = MHI_SC_EV_DWORD1(MHI_PKT_TYPE_STATE_CHANGE_EVENT);
117 
118 	ret = mhi_ep_send_event(mhi_cntrl, 0, event, 0);
119 	kmem_cache_free(mhi_cntrl->ev_ring_el_cache, event);
120 
121 	return ret;
122 }
123 
mhi_ep_send_ee_event(struct mhi_ep_cntrl * mhi_cntrl,enum mhi_ee_type exec_env)124 int mhi_ep_send_ee_event(struct mhi_ep_cntrl *mhi_cntrl, enum mhi_ee_type exec_env)
125 {
126 	struct mhi_ring_element *event;
127 	int ret;
128 
129 	event = kmem_cache_zalloc(mhi_cntrl->ev_ring_el_cache, GFP_KERNEL);
130 	if (!event)
131 		return -ENOMEM;
132 
133 	event->dword[0] = MHI_EE_EV_DWORD0(exec_env);
134 	event->dword[1] = MHI_SC_EV_DWORD1(MHI_PKT_TYPE_EE_EVENT);
135 
136 	ret = mhi_ep_send_event(mhi_cntrl, 0, event, 0);
137 	kmem_cache_free(mhi_cntrl->ev_ring_el_cache, event);
138 
139 	return ret;
140 }
141 
mhi_ep_send_cmd_comp_event(struct mhi_ep_cntrl * mhi_cntrl,enum mhi_ev_ccs code)142 static int mhi_ep_send_cmd_comp_event(struct mhi_ep_cntrl *mhi_cntrl, enum mhi_ev_ccs code)
143 {
144 	struct mhi_ep_ring *ring = &mhi_cntrl->mhi_cmd->ring;
145 	struct mhi_ring_element *event;
146 	int ret;
147 
148 	event = kmem_cache_zalloc(mhi_cntrl->ev_ring_el_cache, GFP_KERNEL);
149 	if (!event)
150 		return -ENOMEM;
151 
152 	event->ptr = cpu_to_le64(ring->rbase + ring->rd_offset * sizeof(struct mhi_ring_element));
153 	event->dword[0] = MHI_CC_EV_DWORD0(code);
154 	event->dword[1] = MHI_CC_EV_DWORD1(MHI_PKT_TYPE_CMD_COMPLETION_EVENT);
155 
156 	ret = mhi_ep_send_event(mhi_cntrl, 0, event, 0);
157 	kmem_cache_free(mhi_cntrl->ev_ring_el_cache, event);
158 
159 	return ret;
160 }
161 
mhi_ep_process_cmd_ring(struct mhi_ep_ring * ring,struct mhi_ring_element * el)162 static int mhi_ep_process_cmd_ring(struct mhi_ep_ring *ring, struct mhi_ring_element *el)
163 {
164 	struct mhi_ep_cntrl *mhi_cntrl = ring->mhi_cntrl;
165 	struct device *dev = &mhi_cntrl->mhi_dev->dev;
166 	struct mhi_result result = {};
167 	struct mhi_ep_chan *mhi_chan;
168 	struct mhi_ep_ring *ch_ring;
169 	u32 tmp, ch_id;
170 	int ret;
171 
172 	ch_id = MHI_TRE_GET_CMD_CHID(el);
173 
174 	/* Check if the channel is supported by the controller */
175 	if ((ch_id >= mhi_cntrl->max_chan) || !mhi_cntrl->mhi_chan[ch_id].name) {
176 		dev_dbg(dev, "Channel (%u) not supported!\n", ch_id);
177 		return -ENODEV;
178 	}
179 
180 	mhi_chan = &mhi_cntrl->mhi_chan[ch_id];
181 	ch_ring = &mhi_cntrl->mhi_chan[ch_id].ring;
182 
183 	switch (MHI_TRE_GET_CMD_TYPE(el)) {
184 	case MHI_PKT_TYPE_START_CHAN_CMD:
185 		dev_dbg(dev, "Received START command for channel (%u)\n", ch_id);
186 
187 		mutex_lock(&mhi_chan->lock);
188 		/* Initialize and configure the corresponding channel ring */
189 		if (!ch_ring->started) {
190 			ret = mhi_ep_ring_start(mhi_cntrl, ch_ring,
191 				(union mhi_ep_ring_ctx *)&mhi_cntrl->ch_ctx_cache[ch_id]);
192 			if (ret) {
193 				dev_err(dev, "Failed to start ring for channel (%u)\n", ch_id);
194 				ret = mhi_ep_send_cmd_comp_event(mhi_cntrl,
195 							MHI_EV_CC_UNDEFINED_ERR);
196 				if (ret)
197 					dev_err(dev, "Error sending completion event: %d\n", ret);
198 
199 				goto err_unlock;
200 			}
201 
202 			mhi_chan->rd_offset = ch_ring->rd_offset;
203 		}
204 
205 		/* Set channel state to RUNNING */
206 		mhi_chan->state = MHI_CH_STATE_RUNNING;
207 		tmp = le32_to_cpu(mhi_cntrl->ch_ctx_cache[ch_id].chcfg);
208 		tmp &= ~CHAN_CTX_CHSTATE_MASK;
209 		tmp |= FIELD_PREP(CHAN_CTX_CHSTATE_MASK, MHI_CH_STATE_RUNNING);
210 		mhi_cntrl->ch_ctx_cache[ch_id].chcfg = cpu_to_le32(tmp);
211 
212 		ret = mhi_ep_send_cmd_comp_event(mhi_cntrl, MHI_EV_CC_SUCCESS);
213 		if (ret) {
214 			dev_err(dev, "Error sending command completion event (%u)\n",
215 				MHI_EV_CC_SUCCESS);
216 			goto err_unlock;
217 		}
218 
219 		mutex_unlock(&mhi_chan->lock);
220 
221 		/*
222 		 * Create MHI device only during UL channel start. Since the MHI
223 		 * channels operate in a pair, we'll associate both UL and DL
224 		 * channels to the same device.
225 		 *
226 		 * We also need to check for mhi_dev != NULL because, the host
227 		 * will issue START_CHAN command during resume and we don't
228 		 * destroy the device during suspend.
229 		 */
230 		if (!(ch_id % 2) && !mhi_chan->mhi_dev) {
231 			ret = mhi_ep_create_device(mhi_cntrl, ch_id);
232 			if (ret) {
233 				dev_err(dev, "Error creating device for channel (%u)\n", ch_id);
234 				mutex_lock(&mhi_cntrl->state_lock);
235 				mhi_ep_handle_syserr(mhi_cntrl);
236 				mutex_unlock(&mhi_cntrl->state_lock);
237 				return ret;
238 			}
239 		}
240 
241 		/* Finally, enable DB for the channel */
242 		mhi_ep_mmio_enable_chdb(mhi_cntrl, ch_id);
243 
244 		break;
245 	case MHI_PKT_TYPE_STOP_CHAN_CMD:
246 		dev_dbg(dev, "Received STOP command for channel (%u)\n", ch_id);
247 		if (!ch_ring->started) {
248 			dev_err(dev, "Channel (%u) not opened\n", ch_id);
249 			return -ENODEV;
250 		}
251 
252 		mutex_lock(&mhi_chan->lock);
253 		/* Disable DB for the channel */
254 		mhi_ep_mmio_disable_chdb(mhi_cntrl, ch_id);
255 
256 		/* Send channel disconnect status to client drivers */
257 		if (mhi_chan->xfer_cb) {
258 			result.transaction_status = -ENOTCONN;
259 			result.bytes_xferd = 0;
260 			mhi_chan->xfer_cb(mhi_chan->mhi_dev, &result);
261 		}
262 
263 		/* Set channel state to STOP */
264 		mhi_chan->state = MHI_CH_STATE_STOP;
265 		tmp = le32_to_cpu(mhi_cntrl->ch_ctx_cache[ch_id].chcfg);
266 		tmp &= ~CHAN_CTX_CHSTATE_MASK;
267 		tmp |= FIELD_PREP(CHAN_CTX_CHSTATE_MASK, MHI_CH_STATE_STOP);
268 		mhi_cntrl->ch_ctx_cache[ch_id].chcfg = cpu_to_le32(tmp);
269 
270 		ret = mhi_ep_send_cmd_comp_event(mhi_cntrl, MHI_EV_CC_SUCCESS);
271 		if (ret) {
272 			dev_err(dev, "Error sending command completion event (%u)\n",
273 				MHI_EV_CC_SUCCESS);
274 			goto err_unlock;
275 		}
276 
277 		mutex_unlock(&mhi_chan->lock);
278 		break;
279 	case MHI_PKT_TYPE_RESET_CHAN_CMD:
280 		dev_dbg(dev, "Received RESET command for channel (%u)\n", ch_id);
281 		if (!ch_ring->started) {
282 			dev_err(dev, "Channel (%u) not opened\n", ch_id);
283 			return -ENODEV;
284 		}
285 
286 		mutex_lock(&mhi_chan->lock);
287 		/* Stop and reset the transfer ring */
288 		mhi_ep_ring_reset(mhi_cntrl, ch_ring);
289 
290 		/* Send channel disconnect status to client driver */
291 		if (mhi_chan->xfer_cb) {
292 			result.transaction_status = -ENOTCONN;
293 			result.bytes_xferd = 0;
294 			mhi_chan->xfer_cb(mhi_chan->mhi_dev, &result);
295 		}
296 
297 		/* Set channel state to DISABLED */
298 		mhi_chan->state = MHI_CH_STATE_DISABLED;
299 		tmp = le32_to_cpu(mhi_cntrl->ch_ctx_cache[ch_id].chcfg);
300 		tmp &= ~CHAN_CTX_CHSTATE_MASK;
301 		tmp |= FIELD_PREP(CHAN_CTX_CHSTATE_MASK, MHI_CH_STATE_DISABLED);
302 		mhi_cntrl->ch_ctx_cache[ch_id].chcfg = cpu_to_le32(tmp);
303 
304 		ret = mhi_ep_send_cmd_comp_event(mhi_cntrl, MHI_EV_CC_SUCCESS);
305 		if (ret) {
306 			dev_err(dev, "Error sending command completion event (%u)\n",
307 				MHI_EV_CC_SUCCESS);
308 			goto err_unlock;
309 		}
310 
311 		mutex_unlock(&mhi_chan->lock);
312 		break;
313 	default:
314 		dev_err(dev, "Invalid command received: %lu for channel (%u)\n",
315 			MHI_TRE_GET_CMD_TYPE(el), ch_id);
316 		return -EINVAL;
317 	}
318 
319 	return 0;
320 
321 err_unlock:
322 	mutex_unlock(&mhi_chan->lock);
323 
324 	return ret;
325 }
326 
mhi_ep_queue_is_empty(struct mhi_ep_device * mhi_dev,enum dma_data_direction dir)327 bool mhi_ep_queue_is_empty(struct mhi_ep_device *mhi_dev, enum dma_data_direction dir)
328 {
329 	struct mhi_ep_chan *mhi_chan = (dir == DMA_FROM_DEVICE) ? mhi_dev->dl_chan :
330 								mhi_dev->ul_chan;
331 	struct mhi_ep_cntrl *mhi_cntrl = mhi_dev->mhi_cntrl;
332 	struct mhi_ep_ring *ring = &mhi_cntrl->mhi_chan[mhi_chan->chan].ring;
333 
334 	return !!(mhi_chan->rd_offset == ring->wr_offset);
335 }
336 EXPORT_SYMBOL_GPL(mhi_ep_queue_is_empty);
337 
mhi_ep_read_completion(struct mhi_ep_buf_info * buf_info)338 static void mhi_ep_read_completion(struct mhi_ep_buf_info *buf_info)
339 {
340 	struct mhi_ep_device *mhi_dev = buf_info->mhi_dev;
341 	struct mhi_ep_cntrl *mhi_cntrl = mhi_dev->mhi_cntrl;
342 	struct mhi_ep_chan *mhi_chan = mhi_dev->ul_chan;
343 	struct mhi_ep_ring *ring = &mhi_cntrl->mhi_chan[mhi_chan->chan].ring;
344 	struct mhi_ring_element *el = &ring->ring_cache[ring->rd_offset];
345 	struct mhi_result result = {};
346 	int ret;
347 
348 	if (mhi_chan->xfer_cb) {
349 		result.buf_addr = buf_info->cb_buf;
350 		result.dir = mhi_chan->dir;
351 		result.bytes_xferd = buf_info->size;
352 
353 		mhi_chan->xfer_cb(mhi_dev, &result);
354 	}
355 
356 	/*
357 	 * The host will split the data packet into multiple TREs if it can't fit
358 	 * the packet in a single TRE. In that case, CHAIN flag will be set by the
359 	 * host for all TREs except the last one.
360 	 */
361 	if (buf_info->code != MHI_EV_CC_OVERFLOW) {
362 		if (MHI_TRE_DATA_GET_CHAIN(el)) {
363 			/*
364 			 * IEOB (Interrupt on End of Block) flag will be set by the host if
365 			 * it expects the completion event for all TREs of a TD.
366 			 */
367 			if (MHI_TRE_DATA_GET_IEOB(el)) {
368 				ret = mhi_ep_send_completion_event(mhi_cntrl, ring, el,
369 							     MHI_TRE_DATA_GET_LEN(el),
370 							     MHI_EV_CC_EOB);
371 				if (ret) {
372 					dev_err(&mhi_chan->mhi_dev->dev,
373 						"Error sending transfer compl. event\n");
374 					goto err_free_tre_buf;
375 				}
376 			}
377 		} else {
378 			/*
379 			 * IEOT (Interrupt on End of Transfer) flag will be set by the host
380 			 * for the last TRE of the TD and expects the completion event for
381 			 * the same.
382 			 */
383 			if (MHI_TRE_DATA_GET_IEOT(el)) {
384 				ret = mhi_ep_send_completion_event(mhi_cntrl, ring, el,
385 							     MHI_TRE_DATA_GET_LEN(el),
386 							     MHI_EV_CC_EOT);
387 				if (ret) {
388 					dev_err(&mhi_chan->mhi_dev->dev,
389 						"Error sending transfer compl. event\n");
390 					goto err_free_tre_buf;
391 				}
392 			}
393 		}
394 	}
395 
396 	mhi_ep_ring_inc_index(ring);
397 
398 err_free_tre_buf:
399 	kmem_cache_free(mhi_cntrl->tre_buf_cache, buf_info->cb_buf);
400 }
401 
mhi_ep_read_channel(struct mhi_ep_cntrl * mhi_cntrl,struct mhi_ep_ring * ring)402 static int mhi_ep_read_channel(struct mhi_ep_cntrl *mhi_cntrl,
403 			       struct mhi_ep_ring *ring)
404 {
405 	struct mhi_ep_chan *mhi_chan = &mhi_cntrl->mhi_chan[ring->ch_id];
406 	struct device *dev = &mhi_cntrl->mhi_dev->dev;
407 	size_t tr_len, read_offset;
408 	struct mhi_ep_buf_info buf_info = {};
409 	u32 len = MHI_EP_DEFAULT_MTU;
410 	struct mhi_ring_element *el;
411 	void *buf_addr;
412 	int ret;
413 
414 	do {
415 		/* Don't process the transfer ring if the channel is not in RUNNING state */
416 		if (mhi_chan->state != MHI_CH_STATE_RUNNING) {
417 			dev_err(dev, "Channel not available\n");
418 			return -ENODEV;
419 		}
420 
421 		el = &ring->ring_cache[mhi_chan->rd_offset];
422 
423 		/* Check if there is data pending to be read from previous read operation */
424 		if (mhi_chan->tre_bytes_left) {
425 			dev_dbg(dev, "TRE bytes remaining: %u\n", mhi_chan->tre_bytes_left);
426 			tr_len = min(len, mhi_chan->tre_bytes_left);
427 		} else {
428 			mhi_chan->tre_loc = MHI_TRE_DATA_GET_PTR(el);
429 			mhi_chan->tre_size = MHI_TRE_DATA_GET_LEN(el);
430 			mhi_chan->tre_bytes_left = mhi_chan->tre_size;
431 
432 			tr_len = min(len, mhi_chan->tre_size);
433 		}
434 
435 		read_offset = mhi_chan->tre_size - mhi_chan->tre_bytes_left;
436 
437 		buf_addr = kmem_cache_zalloc(mhi_cntrl->tre_buf_cache, GFP_KERNEL);
438 		if (!buf_addr)
439 			return -ENOMEM;
440 
441 		buf_info.host_addr = mhi_chan->tre_loc + read_offset;
442 		buf_info.dev_addr = buf_addr;
443 		buf_info.size = tr_len;
444 		buf_info.cb = mhi_ep_read_completion;
445 		buf_info.cb_buf = buf_addr;
446 		buf_info.mhi_dev = mhi_chan->mhi_dev;
447 
448 		if (mhi_chan->tre_bytes_left - tr_len)
449 			buf_info.code = MHI_EV_CC_OVERFLOW;
450 
451 		dev_dbg(dev, "Reading %zd bytes from channel (%u)\n", tr_len, ring->ch_id);
452 		ret = mhi_cntrl->read_async(mhi_cntrl, &buf_info);
453 		if (ret) {
454 			dev_err(&mhi_chan->mhi_dev->dev, "Error reading from channel\n");
455 			goto err_free_buf_addr;
456 		}
457 
458 		mhi_chan->tre_bytes_left -= tr_len;
459 
460 		if (!mhi_chan->tre_bytes_left)
461 			mhi_chan->rd_offset = (mhi_chan->rd_offset + 1) % ring->ring_size;
462 	/* Read until the some buffer is left or the ring becomes not empty */
463 	} while (!mhi_ep_queue_is_empty(mhi_chan->mhi_dev, DMA_TO_DEVICE));
464 
465 	return 0;
466 
467 err_free_buf_addr:
468 	kmem_cache_free(mhi_cntrl->tre_buf_cache, buf_addr);
469 
470 	return ret;
471 }
472 
mhi_ep_process_ch_ring(struct mhi_ep_ring * ring)473 static int mhi_ep_process_ch_ring(struct mhi_ep_ring *ring)
474 {
475 	struct mhi_ep_cntrl *mhi_cntrl = ring->mhi_cntrl;
476 	struct mhi_result result = {};
477 	struct mhi_ep_chan *mhi_chan;
478 	int ret;
479 
480 	mhi_chan = &mhi_cntrl->mhi_chan[ring->ch_id];
481 
482 	/*
483 	 * Bail out if transfer callback is not registered for the channel.
484 	 * This is most likely due to the client driver not loaded at this point.
485 	 */
486 	if (!mhi_chan->xfer_cb) {
487 		dev_err(&mhi_chan->mhi_dev->dev, "Client driver not available\n");
488 		return -ENODEV;
489 	}
490 
491 	if (ring->ch_id % 2) {
492 		/* DL channel */
493 		result.dir = mhi_chan->dir;
494 		mhi_chan->xfer_cb(mhi_chan->mhi_dev, &result);
495 	} else {
496 		/* UL channel */
497 		ret = mhi_ep_read_channel(mhi_cntrl, ring);
498 		if (ret) {
499 			dev_err(&mhi_chan->mhi_dev->dev, "Failed to read channel\n");
500 			return ret;
501 		}
502 	}
503 
504 	return 0;
505 }
506 
mhi_ep_skb_completion(struct mhi_ep_buf_info * buf_info)507 static void mhi_ep_skb_completion(struct mhi_ep_buf_info *buf_info)
508 {
509 	struct mhi_ep_device *mhi_dev = buf_info->mhi_dev;
510 	struct mhi_ep_cntrl *mhi_cntrl = mhi_dev->mhi_cntrl;
511 	struct mhi_ep_chan *mhi_chan = mhi_dev->dl_chan;
512 	struct mhi_ep_ring *ring = &mhi_cntrl->mhi_chan[mhi_chan->chan].ring;
513 	struct mhi_ring_element *el = &ring->ring_cache[ring->rd_offset];
514 	struct device *dev = &mhi_dev->dev;
515 	struct mhi_result result = {};
516 	int ret;
517 
518 	if (mhi_chan->xfer_cb) {
519 		result.buf_addr = buf_info->cb_buf;
520 		result.dir = mhi_chan->dir;
521 		result.bytes_xferd = buf_info->size;
522 
523 		mhi_chan->xfer_cb(mhi_dev, &result);
524 	}
525 
526 	ret = mhi_ep_send_completion_event(mhi_cntrl, ring, el, buf_info->size,
527 					   buf_info->code);
528 	if (ret) {
529 		dev_err(dev, "Error sending transfer completion event\n");
530 		return;
531 	}
532 
533 	mhi_ep_ring_inc_index(ring);
534 }
535 
536 /* TODO: Handle partially formed TDs */
mhi_ep_queue_skb(struct mhi_ep_device * mhi_dev,struct sk_buff * skb)537 int mhi_ep_queue_skb(struct mhi_ep_device *mhi_dev, struct sk_buff *skb)
538 {
539 	struct mhi_ep_cntrl *mhi_cntrl = mhi_dev->mhi_cntrl;
540 	struct mhi_ep_chan *mhi_chan = mhi_dev->dl_chan;
541 	struct device *dev = &mhi_chan->mhi_dev->dev;
542 	struct mhi_ep_buf_info buf_info = {};
543 	struct mhi_ring_element *el;
544 	u32 buf_left, read_offset;
545 	struct mhi_ep_ring *ring;
546 	size_t tr_len;
547 	u32 tre_len;
548 	int ret;
549 
550 	buf_left = skb->len;
551 	ring = &mhi_cntrl->mhi_chan[mhi_chan->chan].ring;
552 
553 	mutex_lock(&mhi_chan->lock);
554 
555 	do {
556 		/* Don't process the transfer ring if the channel is not in RUNNING state */
557 		if (mhi_chan->state != MHI_CH_STATE_RUNNING) {
558 			dev_err(dev, "Channel not available\n");
559 			ret = -ENODEV;
560 			goto err_exit;
561 		}
562 
563 		if (mhi_ep_queue_is_empty(mhi_dev, DMA_FROM_DEVICE)) {
564 			dev_err(dev, "TRE not available!\n");
565 			ret = -ENOSPC;
566 			goto err_exit;
567 		}
568 
569 		el = &ring->ring_cache[mhi_chan->rd_offset];
570 		tre_len = MHI_TRE_DATA_GET_LEN(el);
571 
572 		tr_len = min(buf_left, tre_len);
573 		read_offset = skb->len - buf_left;
574 
575 		buf_info.dev_addr = skb->data + read_offset;
576 		buf_info.host_addr = MHI_TRE_DATA_GET_PTR(el);
577 		buf_info.size = tr_len;
578 		buf_info.cb = mhi_ep_skb_completion;
579 		buf_info.cb_buf = skb;
580 		buf_info.mhi_dev = mhi_dev;
581 
582 		/*
583 		 * For all TREs queued by the host for DL channel, only the EOT flag will be set.
584 		 * If the packet doesn't fit into a single TRE, send the OVERFLOW event to
585 		 * the host so that the host can adjust the packet boundary to next TREs. Else send
586 		 * the EOT event to the host indicating the packet boundary.
587 		 */
588 		if (buf_left - tr_len)
589 			buf_info.code = MHI_EV_CC_OVERFLOW;
590 		else
591 			buf_info.code = MHI_EV_CC_EOT;
592 
593 		dev_dbg(dev, "Writing %zd bytes to channel (%u)\n", tr_len, ring->ch_id);
594 		ret = mhi_cntrl->write_async(mhi_cntrl, &buf_info);
595 		if (ret) {
596 			dev_err(dev, "Error writing to the channel\n");
597 			goto err_exit;
598 		}
599 
600 		buf_left -= tr_len;
601 
602 		/*
603 		 * Update the read offset cached in mhi_chan. Actual read offset
604 		 * will be updated by the completion handler.
605 		 */
606 		mhi_chan->rd_offset = (mhi_chan->rd_offset + 1) % ring->ring_size;
607 	} while (buf_left);
608 
609 	mutex_unlock(&mhi_chan->lock);
610 
611 	return 0;
612 
613 err_exit:
614 	mutex_unlock(&mhi_chan->lock);
615 
616 	return ret;
617 }
618 EXPORT_SYMBOL_GPL(mhi_ep_queue_skb);
619 
mhi_ep_cache_host_cfg(struct mhi_ep_cntrl * mhi_cntrl)620 static int mhi_ep_cache_host_cfg(struct mhi_ep_cntrl *mhi_cntrl)
621 {
622 	size_t cmd_ctx_host_size, ch_ctx_host_size, ev_ctx_host_size;
623 	struct device *dev = &mhi_cntrl->mhi_dev->dev;
624 	int ret;
625 
626 	/* Update the number of event rings (NER) programmed by the host */
627 	mhi_ep_mmio_update_ner(mhi_cntrl);
628 
629 	dev_dbg(dev, "Number of Event rings: %u, HW Event rings: %u\n",
630 		 mhi_cntrl->event_rings, mhi_cntrl->hw_event_rings);
631 
632 	ch_ctx_host_size = sizeof(struct mhi_chan_ctxt) * mhi_cntrl->max_chan;
633 	ev_ctx_host_size = sizeof(struct mhi_event_ctxt) * mhi_cntrl->event_rings;
634 	cmd_ctx_host_size = sizeof(struct mhi_cmd_ctxt) * NR_OF_CMD_RINGS;
635 
636 	/* Get the channel context base pointer from host */
637 	mhi_ep_mmio_get_chc_base(mhi_cntrl);
638 
639 	/* Allocate and map memory for caching host channel context */
640 	ret = mhi_cntrl->alloc_map(mhi_cntrl, mhi_cntrl->ch_ctx_host_pa,
641 				   &mhi_cntrl->ch_ctx_cache_phys,
642 				   (void __iomem **) &mhi_cntrl->ch_ctx_cache,
643 				   ch_ctx_host_size);
644 	if (ret) {
645 		dev_err(dev, "Failed to allocate and map ch_ctx_cache\n");
646 		return ret;
647 	}
648 
649 	/* Get the event context base pointer from host */
650 	mhi_ep_mmio_get_erc_base(mhi_cntrl);
651 
652 	/* Allocate and map memory for caching host event context */
653 	ret = mhi_cntrl->alloc_map(mhi_cntrl, mhi_cntrl->ev_ctx_host_pa,
654 				   &mhi_cntrl->ev_ctx_cache_phys,
655 				   (void __iomem **) &mhi_cntrl->ev_ctx_cache,
656 				   ev_ctx_host_size);
657 	if (ret) {
658 		dev_err(dev, "Failed to allocate and map ev_ctx_cache\n");
659 		goto err_ch_ctx;
660 	}
661 
662 	/* Get the command context base pointer from host */
663 	mhi_ep_mmio_get_crc_base(mhi_cntrl);
664 
665 	/* Allocate and map memory for caching host command context */
666 	ret = mhi_cntrl->alloc_map(mhi_cntrl, mhi_cntrl->cmd_ctx_host_pa,
667 				   &mhi_cntrl->cmd_ctx_cache_phys,
668 				   (void __iomem **) &mhi_cntrl->cmd_ctx_cache,
669 				   cmd_ctx_host_size);
670 	if (ret) {
671 		dev_err(dev, "Failed to allocate and map cmd_ctx_cache\n");
672 		goto err_ev_ctx;
673 	}
674 
675 	/* Initialize command ring */
676 	ret = mhi_ep_ring_start(mhi_cntrl, &mhi_cntrl->mhi_cmd->ring,
677 				(union mhi_ep_ring_ctx *)mhi_cntrl->cmd_ctx_cache);
678 	if (ret) {
679 		dev_err(dev, "Failed to start the command ring\n");
680 		goto err_cmd_ctx;
681 	}
682 
683 	return ret;
684 
685 err_cmd_ctx:
686 	mhi_cntrl->unmap_free(mhi_cntrl, mhi_cntrl->cmd_ctx_host_pa, mhi_cntrl->cmd_ctx_cache_phys,
687 			      (void __iomem *) mhi_cntrl->cmd_ctx_cache, cmd_ctx_host_size);
688 
689 err_ev_ctx:
690 	mhi_cntrl->unmap_free(mhi_cntrl, mhi_cntrl->ev_ctx_host_pa, mhi_cntrl->ev_ctx_cache_phys,
691 			      (void __iomem *) mhi_cntrl->ev_ctx_cache, ev_ctx_host_size);
692 
693 err_ch_ctx:
694 	mhi_cntrl->unmap_free(mhi_cntrl, mhi_cntrl->ch_ctx_host_pa, mhi_cntrl->ch_ctx_cache_phys,
695 			      (void __iomem *) mhi_cntrl->ch_ctx_cache, ch_ctx_host_size);
696 
697 	return ret;
698 }
699 
mhi_ep_free_host_cfg(struct mhi_ep_cntrl * mhi_cntrl)700 static void mhi_ep_free_host_cfg(struct mhi_ep_cntrl *mhi_cntrl)
701 {
702 	size_t cmd_ctx_host_size, ch_ctx_host_size, ev_ctx_host_size;
703 
704 	ch_ctx_host_size = sizeof(struct mhi_chan_ctxt) * mhi_cntrl->max_chan;
705 	ev_ctx_host_size = sizeof(struct mhi_event_ctxt) * mhi_cntrl->event_rings;
706 	cmd_ctx_host_size = sizeof(struct mhi_cmd_ctxt) * NR_OF_CMD_RINGS;
707 
708 	mhi_cntrl->unmap_free(mhi_cntrl, mhi_cntrl->cmd_ctx_host_pa, mhi_cntrl->cmd_ctx_cache_phys,
709 			      (void __iomem *) mhi_cntrl->cmd_ctx_cache, cmd_ctx_host_size);
710 
711 	mhi_cntrl->unmap_free(mhi_cntrl, mhi_cntrl->ev_ctx_host_pa, mhi_cntrl->ev_ctx_cache_phys,
712 			      (void __iomem *) mhi_cntrl->ev_ctx_cache, ev_ctx_host_size);
713 
714 	mhi_cntrl->unmap_free(mhi_cntrl, mhi_cntrl->ch_ctx_host_pa, mhi_cntrl->ch_ctx_cache_phys,
715 			      (void __iomem *) mhi_cntrl->ch_ctx_cache, ch_ctx_host_size);
716 }
717 
mhi_ep_enable_int(struct mhi_ep_cntrl * mhi_cntrl)718 static void mhi_ep_enable_int(struct mhi_ep_cntrl *mhi_cntrl)
719 {
720 	/*
721 	 * Doorbell interrupts are enabled when the corresponding channel gets started.
722 	 * Enabling all interrupts here triggers spurious irqs as some of the interrupts
723 	 * associated with hw channels always get triggered.
724 	 */
725 	mhi_ep_mmio_enable_ctrl_interrupt(mhi_cntrl);
726 	mhi_ep_mmio_enable_cmdb_interrupt(mhi_cntrl);
727 }
728 
mhi_ep_enable(struct mhi_ep_cntrl * mhi_cntrl)729 static int mhi_ep_enable(struct mhi_ep_cntrl *mhi_cntrl)
730 {
731 	struct device *dev = &mhi_cntrl->mhi_dev->dev;
732 	enum mhi_state state;
733 	bool mhi_reset;
734 	u32 count = 0;
735 	int ret;
736 
737 	/* Wait for Host to set the M0 state */
738 	do {
739 		msleep(M0_WAIT_DELAY_MS);
740 		mhi_ep_mmio_get_mhi_state(mhi_cntrl, &state, &mhi_reset);
741 		if (mhi_reset) {
742 			/* Clear the MHI reset if host is in reset state */
743 			mhi_ep_mmio_clear_reset(mhi_cntrl);
744 			dev_info(dev, "Detected Host reset while waiting for M0\n");
745 		}
746 		count++;
747 	} while (state != MHI_STATE_M0 && count < M0_WAIT_COUNT);
748 
749 	if (state != MHI_STATE_M0) {
750 		dev_err(dev, "Host failed to enter M0\n");
751 		return -ETIMEDOUT;
752 	}
753 
754 	ret = mhi_ep_cache_host_cfg(mhi_cntrl);
755 	if (ret) {
756 		dev_err(dev, "Failed to cache host config\n");
757 		return ret;
758 	}
759 
760 	mhi_ep_mmio_set_env(mhi_cntrl, MHI_EE_AMSS);
761 
762 	/* Enable all interrupts now */
763 	mhi_ep_enable_int(mhi_cntrl);
764 
765 	return 0;
766 }
767 
mhi_ep_cmd_ring_worker(struct work_struct * work)768 static void mhi_ep_cmd_ring_worker(struct work_struct *work)
769 {
770 	struct mhi_ep_cntrl *mhi_cntrl = container_of(work, struct mhi_ep_cntrl, cmd_ring_work);
771 	struct mhi_ep_ring *ring = &mhi_cntrl->mhi_cmd->ring;
772 	struct device *dev = &mhi_cntrl->mhi_dev->dev;
773 	struct mhi_ring_element *el;
774 	int ret;
775 
776 	/* Update the write offset for the ring */
777 	ret = mhi_ep_update_wr_offset(ring);
778 	if (ret) {
779 		dev_err(dev, "Error updating write offset for ring\n");
780 		return;
781 	}
782 
783 	/* Sanity check to make sure there are elements in the ring */
784 	if (ring->rd_offset == ring->wr_offset)
785 		return;
786 
787 	/*
788 	 * Process command ring element till write offset. In case of an error, just try to
789 	 * process next element.
790 	 */
791 	while (ring->rd_offset != ring->wr_offset) {
792 		el = &ring->ring_cache[ring->rd_offset];
793 
794 		ret = mhi_ep_process_cmd_ring(ring, el);
795 		if (ret && ret != -ENODEV)
796 			dev_err(dev, "Error processing cmd ring element: %zu\n", ring->rd_offset);
797 
798 		mhi_ep_ring_inc_index(ring);
799 	}
800 }
801 
mhi_ep_ch_ring_worker(struct work_struct * work)802 static void mhi_ep_ch_ring_worker(struct work_struct *work)
803 {
804 	struct mhi_ep_cntrl *mhi_cntrl = container_of(work, struct mhi_ep_cntrl, ch_ring_work);
805 	struct device *dev = &mhi_cntrl->mhi_dev->dev;
806 	struct mhi_ep_ring_item *itr, *tmp;
807 	struct mhi_ep_ring *ring;
808 	struct mhi_ep_chan *chan;
809 	unsigned long flags;
810 	LIST_HEAD(head);
811 	int ret;
812 
813 	spin_lock_irqsave(&mhi_cntrl->list_lock, flags);
814 	list_splice_tail_init(&mhi_cntrl->ch_db_list, &head);
815 	spin_unlock_irqrestore(&mhi_cntrl->list_lock, flags);
816 
817 	/* Process each queued channel ring. In case of an error, just process next element. */
818 	list_for_each_entry_safe(itr, tmp, &head, node) {
819 		list_del(&itr->node);
820 		ring = itr->ring;
821 
822 		chan = &mhi_cntrl->mhi_chan[ring->ch_id];
823 		mutex_lock(&chan->lock);
824 
825 		/*
826 		 * The ring could've stopped while we waited to grab the (chan->lock), so do
827 		 * a sanity check before going further.
828 		 */
829 		if (!ring->started) {
830 			mutex_unlock(&chan->lock);
831 			kfree(itr);
832 			continue;
833 		}
834 
835 		/* Update the write offset for the ring */
836 		ret = mhi_ep_update_wr_offset(ring);
837 		if (ret) {
838 			dev_err(dev, "Error updating write offset for ring\n");
839 			mutex_unlock(&chan->lock);
840 			kmem_cache_free(mhi_cntrl->ring_item_cache, itr);
841 			continue;
842 		}
843 
844 		/* Sanity check to make sure there are elements in the ring */
845 		if (chan->rd_offset == ring->wr_offset) {
846 			mutex_unlock(&chan->lock);
847 			kmem_cache_free(mhi_cntrl->ring_item_cache, itr);
848 			continue;
849 		}
850 
851 		dev_dbg(dev, "Processing the ring for channel (%u)\n", ring->ch_id);
852 		ret = mhi_ep_process_ch_ring(ring);
853 		if (ret) {
854 			dev_err(dev, "Error processing ring for channel (%u): %d\n",
855 				ring->ch_id, ret);
856 			mutex_unlock(&chan->lock);
857 			kmem_cache_free(mhi_cntrl->ring_item_cache, itr);
858 			continue;
859 		}
860 
861 		mutex_unlock(&chan->lock);
862 		kmem_cache_free(mhi_cntrl->ring_item_cache, itr);
863 	}
864 }
865 
mhi_ep_state_worker(struct work_struct * work)866 static void mhi_ep_state_worker(struct work_struct *work)
867 {
868 	struct mhi_ep_cntrl *mhi_cntrl = container_of(work, struct mhi_ep_cntrl, state_work);
869 	struct device *dev = &mhi_cntrl->mhi_dev->dev;
870 	struct mhi_ep_state_transition *itr, *tmp;
871 	unsigned long flags;
872 	LIST_HEAD(head);
873 	int ret;
874 
875 	spin_lock_irqsave(&mhi_cntrl->list_lock, flags);
876 	list_splice_tail_init(&mhi_cntrl->st_transition_list, &head);
877 	spin_unlock_irqrestore(&mhi_cntrl->list_lock, flags);
878 
879 	list_for_each_entry_safe(itr, tmp, &head, node) {
880 		list_del(&itr->node);
881 		dev_dbg(dev, "Handling MHI state transition to %s\n",
882 			 mhi_state_str(itr->state));
883 
884 		switch (itr->state) {
885 		case MHI_STATE_M0:
886 			ret = mhi_ep_set_m0_state(mhi_cntrl);
887 			if (ret)
888 				dev_err(dev, "Failed to transition to M0 state\n");
889 			break;
890 		case MHI_STATE_M3:
891 			ret = mhi_ep_set_m3_state(mhi_cntrl);
892 			if (ret)
893 				dev_err(dev, "Failed to transition to M3 state\n");
894 			break;
895 		default:
896 			dev_err(dev, "Invalid MHI state transition: %d\n", itr->state);
897 			break;
898 		}
899 		kfree(itr);
900 	}
901 }
902 
mhi_ep_queue_channel_db(struct mhi_ep_cntrl * mhi_cntrl,unsigned long ch_int,u32 ch_idx)903 static void mhi_ep_queue_channel_db(struct mhi_ep_cntrl *mhi_cntrl, unsigned long ch_int,
904 				    u32 ch_idx)
905 {
906 	struct mhi_ep_ring_item *item;
907 	struct mhi_ep_ring *ring;
908 	bool work = !!ch_int;
909 	LIST_HEAD(head);
910 	u32 i;
911 
912 	/* First add the ring items to a local list */
913 	for_each_set_bit(i, &ch_int, 32) {
914 		/* Channel index varies for each register: 0, 32, 64, 96 */
915 		u32 ch_id = ch_idx + i;
916 
917 		ring = &mhi_cntrl->mhi_chan[ch_id].ring;
918 		item = kmem_cache_zalloc(mhi_cntrl->ring_item_cache, GFP_ATOMIC);
919 		if (!item)
920 			return;
921 
922 		item->ring = ring;
923 		list_add_tail(&item->node, &head);
924 	}
925 
926 	/* Now, splice the local list into ch_db_list and queue the work item */
927 	if (work) {
928 		spin_lock(&mhi_cntrl->list_lock);
929 		list_splice_tail_init(&head, &mhi_cntrl->ch_db_list);
930 		spin_unlock(&mhi_cntrl->list_lock);
931 
932 		queue_work(mhi_cntrl->wq, &mhi_cntrl->ch_ring_work);
933 	}
934 }
935 
936 /*
937  * Channel interrupt statuses are contained in 4 registers each of 32bit length.
938  * For checking all interrupts, we need to loop through each registers and then
939  * check for bits set.
940  */
mhi_ep_check_channel_interrupt(struct mhi_ep_cntrl * mhi_cntrl)941 static void mhi_ep_check_channel_interrupt(struct mhi_ep_cntrl *mhi_cntrl)
942 {
943 	u32 ch_int, ch_idx, i;
944 
945 	/* Bail out if there is no channel doorbell interrupt */
946 	if (!mhi_ep_mmio_read_chdb_status_interrupts(mhi_cntrl))
947 		return;
948 
949 	for (i = 0; i < MHI_MASK_ROWS_CH_DB; i++) {
950 		ch_idx = i * MHI_MASK_CH_LEN;
951 
952 		/* Only process channel interrupt if the mask is enabled */
953 		ch_int = mhi_cntrl->chdb[i].status & mhi_cntrl->chdb[i].mask;
954 		if (ch_int) {
955 			mhi_ep_queue_channel_db(mhi_cntrl, ch_int, ch_idx);
956 			mhi_ep_mmio_write(mhi_cntrl, MHI_CHDB_INT_CLEAR_n(i),
957 							mhi_cntrl->chdb[i].status);
958 		}
959 	}
960 }
961 
mhi_ep_process_ctrl_interrupt(struct mhi_ep_cntrl * mhi_cntrl,enum mhi_state state)962 static void mhi_ep_process_ctrl_interrupt(struct mhi_ep_cntrl *mhi_cntrl,
963 					 enum mhi_state state)
964 {
965 	struct mhi_ep_state_transition *item;
966 
967 	item = kzalloc_obj(*item, GFP_ATOMIC);
968 	if (!item)
969 		return;
970 
971 	item->state = state;
972 	spin_lock(&mhi_cntrl->list_lock);
973 	list_add_tail(&item->node, &mhi_cntrl->st_transition_list);
974 	spin_unlock(&mhi_cntrl->list_lock);
975 
976 	queue_work(mhi_cntrl->wq, &mhi_cntrl->state_work);
977 }
978 
979 /*
980  * Interrupt handler that services interrupts raised by the host writing to
981  * MHICTRL and Command ring doorbell (CRDB) registers for state change and
982  * channel interrupts.
983  */
mhi_ep_irq(int irq,void * data)984 static irqreturn_t mhi_ep_irq(int irq, void *data)
985 {
986 	struct mhi_ep_cntrl *mhi_cntrl = data;
987 	struct device *dev = &mhi_cntrl->mhi_dev->dev;
988 	enum mhi_state state;
989 	u32 int_value;
990 	bool mhi_reset;
991 
992 	/* Acknowledge the ctrl interrupt */
993 	int_value = mhi_ep_mmio_read(mhi_cntrl, MHI_CTRL_INT_STATUS);
994 	mhi_ep_mmio_write(mhi_cntrl, MHI_CTRL_INT_CLEAR, int_value);
995 
996 	/* Check for ctrl interrupt */
997 	if (FIELD_GET(MHI_CTRL_INT_STATUS_MSK, int_value)) {
998 		dev_dbg(dev, "Processing ctrl interrupt\n");
999 		mhi_ep_mmio_get_mhi_state(mhi_cntrl, &state, &mhi_reset);
1000 		if (mhi_reset) {
1001 			dev_info(dev, "Host triggered MHI reset!\n");
1002 			disable_irq_nosync(mhi_cntrl->irq);
1003 			schedule_work(&mhi_cntrl->reset_work);
1004 			return IRQ_HANDLED;
1005 		}
1006 
1007 		mhi_ep_process_ctrl_interrupt(mhi_cntrl, state);
1008 	}
1009 
1010 	/* Check for command doorbell interrupt */
1011 	if (FIELD_GET(MHI_CTRL_INT_STATUS_CRDB_MSK, int_value)) {
1012 		dev_dbg(dev, "Processing command doorbell interrupt\n");
1013 		queue_work(mhi_cntrl->wq, &mhi_cntrl->cmd_ring_work);
1014 	}
1015 
1016 	/* Check for channel interrupts */
1017 	mhi_ep_check_channel_interrupt(mhi_cntrl);
1018 
1019 	return IRQ_HANDLED;
1020 }
1021 
mhi_ep_abort_transfer(struct mhi_ep_cntrl * mhi_cntrl)1022 static void mhi_ep_abort_transfer(struct mhi_ep_cntrl *mhi_cntrl)
1023 {
1024 	struct mhi_ep_ring *ch_ring, *ev_ring;
1025 	struct mhi_result result = {};
1026 	struct mhi_ep_chan *mhi_chan;
1027 	int i;
1028 
1029 	/* Disable all the channels to prevent new transfers */
1030 	for (i = 0; i < mhi_cntrl->max_chan; i++) {
1031 		mhi_chan = &mhi_cntrl->mhi_chan[i];
1032 		if (!mhi_chan->ring.started)
1033 			continue;
1034 
1035 		mutex_lock(&mhi_chan->lock);
1036 		mhi_chan->state = MHI_CH_STATE_DISABLED;
1037 		mutex_unlock(&mhi_chan->lock);
1038 	}
1039 
1040 	/* Drain ring workers and in-flight transfers before notifying disconnect */
1041 	flush_workqueue(mhi_cntrl->wq);
1042 	if (mhi_cntrl->flush_async)
1043 		mhi_cntrl->flush_async(mhi_cntrl);
1044 
1045 	/* Send channel disconnect status to client drivers */
1046 	for (i = 0; i < mhi_cntrl->max_chan; i++) {
1047 		mhi_chan = &mhi_cntrl->mhi_chan[i];
1048 		if (!mhi_chan->ring.started)
1049 			continue;
1050 
1051 		mutex_lock(&mhi_chan->lock);
1052 		if (mhi_chan->xfer_cb) {
1053 			result.transaction_status = -ENOTCONN;
1054 			result.bytes_xferd = 0;
1055 			mhi_chan->xfer_cb(mhi_chan->mhi_dev, &result);
1056 		}
1057 		mutex_unlock(&mhi_chan->lock);
1058 	}
1059 
1060 	/* Destroy devices associated with all channels */
1061 	device_for_each_child(&mhi_cntrl->mhi_dev->dev, NULL, mhi_ep_destroy_device);
1062 
1063 	/* Stop and reset the transfer rings */
1064 	for (i = 0; i < mhi_cntrl->max_chan; i++) {
1065 		mhi_chan = &mhi_cntrl->mhi_chan[i];
1066 		if (!mhi_chan->ring.started)
1067 			continue;
1068 
1069 		ch_ring = &mhi_cntrl->mhi_chan[i].ring;
1070 		mutex_lock(&mhi_chan->lock);
1071 		mhi_ep_ring_reset(mhi_cntrl, ch_ring);
1072 		mutex_unlock(&mhi_chan->lock);
1073 	}
1074 
1075 	/* Stop and reset the event rings */
1076 	for (i = 0; i < mhi_cntrl->event_rings; i++) {
1077 		ev_ring = &mhi_cntrl->mhi_event[i].ring;
1078 		if (!ev_ring->started)
1079 			continue;
1080 
1081 		mutex_lock(&mhi_cntrl->event_lock);
1082 		mhi_ep_ring_reset(mhi_cntrl, ev_ring);
1083 		mutex_unlock(&mhi_cntrl->event_lock);
1084 	}
1085 
1086 	/* Stop and reset the command ring */
1087 	mhi_ep_ring_reset(mhi_cntrl, &mhi_cntrl->mhi_cmd->ring);
1088 
1089 	mhi_ep_free_host_cfg(mhi_cntrl);
1090 	mhi_ep_mmio_mask_interrupts(mhi_cntrl);
1091 
1092 	mhi_cntrl->enabled = false;
1093 }
1094 
mhi_ep_reset_worker(struct work_struct * work)1095 static void mhi_ep_reset_worker(struct work_struct *work)
1096 {
1097 	struct mhi_ep_cntrl *mhi_cntrl = container_of(work, struct mhi_ep_cntrl, reset_work);
1098 	enum mhi_state cur_state;
1099 
1100 	mhi_ep_power_down(mhi_cntrl);
1101 
1102 	/* Reset MMIO to signal host that the MHI_RESET is completed in endpoint */
1103 	mhi_ep_mmio_reset(mhi_cntrl);
1104 
1105 	mutex_lock(&mhi_cntrl->state_lock);
1106 	cur_state = mhi_cntrl->mhi_state;
1107 	mutex_unlock(&mhi_cntrl->state_lock);
1108 
1109 	/*
1110 	 * Only proceed further if the reset is due to SYS_ERR. The host will
1111 	 * issue reset during shutdown also and we don't need to do re-init in
1112 	 * that case.
1113 	 */
1114 	if (cur_state == MHI_STATE_SYS_ERR)
1115 		mhi_ep_power_up(mhi_cntrl);
1116 }
1117 
1118 /*
1119  * We don't need to do anything special other than setting the MHI SYS_ERR
1120  * state. The host will reset all contexts and issue MHI RESET so that we
1121  * could also recover from error state.
1122  */
mhi_ep_handle_syserr(struct mhi_ep_cntrl * mhi_cntrl)1123 void mhi_ep_handle_syserr(struct mhi_ep_cntrl *mhi_cntrl)
1124 {
1125 	struct device *dev = &mhi_cntrl->mhi_dev->dev;
1126 	int ret;
1127 
1128 	ret = mhi_ep_set_mhi_state(mhi_cntrl, MHI_STATE_SYS_ERR);
1129 	if (ret)
1130 		return;
1131 
1132 	/* Signal host that the device went to SYS_ERR state */
1133 	ret = mhi_ep_send_state_change_event(mhi_cntrl, MHI_STATE_SYS_ERR);
1134 	if (ret)
1135 		dev_err(dev, "Failed sending SYS_ERR state change event: %d\n", ret);
1136 }
1137 
mhi_ep_power_up(struct mhi_ep_cntrl * mhi_cntrl)1138 int mhi_ep_power_up(struct mhi_ep_cntrl *mhi_cntrl)
1139 {
1140 	struct device *dev = &mhi_cntrl->mhi_dev->dev;
1141 	int ret, i;
1142 
1143 	/*
1144 	 * Mask all interrupts until the state machine is ready. Interrupts will
1145 	 * be enabled later with mhi_ep_enable().
1146 	 */
1147 	mhi_ep_mmio_mask_interrupts(mhi_cntrl);
1148 	mhi_ep_mmio_init(mhi_cntrl);
1149 
1150 	mhi_cntrl->mhi_event = kzalloc_objs(*mhi_cntrl->mhi_event,
1151 					    mhi_cntrl->event_rings);
1152 	if (!mhi_cntrl->mhi_event)
1153 		return -ENOMEM;
1154 
1155 	/* Initialize command, channel and event rings */
1156 	mhi_ep_ring_init(&mhi_cntrl->mhi_cmd->ring, RING_TYPE_CMD, 0);
1157 	for (i = 0; i < mhi_cntrl->max_chan; i++)
1158 		mhi_ep_ring_init(&mhi_cntrl->mhi_chan[i].ring, RING_TYPE_CH, i);
1159 	for (i = 0; i < mhi_cntrl->event_rings; i++)
1160 		mhi_ep_ring_init(&mhi_cntrl->mhi_event[i].ring, RING_TYPE_ER, i);
1161 
1162 	mutex_lock(&mhi_cntrl->state_lock);
1163 	mhi_cntrl->mhi_state = MHI_STATE_RESET;
1164 	mutex_unlock(&mhi_cntrl->state_lock);
1165 
1166 	/* Set AMSS EE before signaling ready state */
1167 	mhi_ep_mmio_set_env(mhi_cntrl, MHI_EE_AMSS);
1168 
1169 	/* All set, notify the host that we are ready */
1170 	ret = mhi_ep_set_ready_state(mhi_cntrl);
1171 	if (ret)
1172 		goto err_free_event;
1173 
1174 	dev_dbg(dev, "READY state notification sent to the host\n");
1175 
1176 	ret = mhi_ep_enable(mhi_cntrl);
1177 	if (ret) {
1178 		dev_err(dev, "Failed to enable MHI endpoint\n");
1179 		goto err_free_event;
1180 	}
1181 
1182 	enable_irq(mhi_cntrl->irq);
1183 	mhi_cntrl->enabled = true;
1184 
1185 	return 0;
1186 
1187 err_free_event:
1188 	kfree(mhi_cntrl->mhi_event);
1189 
1190 	return ret;
1191 }
1192 EXPORT_SYMBOL_GPL(mhi_ep_power_up);
1193 
mhi_ep_power_down(struct mhi_ep_cntrl * mhi_cntrl)1194 void mhi_ep_power_down(struct mhi_ep_cntrl *mhi_cntrl)
1195 {
1196 	if (mhi_cntrl->enabled) {
1197 		mhi_ep_abort_transfer(mhi_cntrl);
1198 		kfree(mhi_cntrl->mhi_event);
1199 		disable_irq(mhi_cntrl->irq);
1200 	}
1201 }
1202 EXPORT_SYMBOL_GPL(mhi_ep_power_down);
1203 
mhi_ep_suspend_channels(struct mhi_ep_cntrl * mhi_cntrl)1204 void mhi_ep_suspend_channels(struct mhi_ep_cntrl *mhi_cntrl)
1205 {
1206 	struct mhi_ep_chan *mhi_chan;
1207 	u32 tmp;
1208 	int i;
1209 
1210 	for (i = 0; i < mhi_cntrl->max_chan; i++) {
1211 		mhi_chan = &mhi_cntrl->mhi_chan[i];
1212 
1213 		if (!mhi_chan->mhi_dev)
1214 			continue;
1215 
1216 		mutex_lock(&mhi_chan->lock);
1217 		/* Skip if the channel is not currently running */
1218 		tmp = le32_to_cpu(mhi_cntrl->ch_ctx_cache[i].chcfg);
1219 		if (FIELD_GET(CHAN_CTX_CHSTATE_MASK, tmp) != MHI_CH_STATE_RUNNING) {
1220 			mutex_unlock(&mhi_chan->lock);
1221 			continue;
1222 		}
1223 
1224 		dev_dbg(&mhi_chan->mhi_dev->dev, "Suspending channel\n");
1225 		/* Set channel state to SUSPENDED */
1226 		mhi_chan->state = MHI_CH_STATE_SUSPENDED;
1227 		tmp &= ~CHAN_CTX_CHSTATE_MASK;
1228 		tmp |= FIELD_PREP(CHAN_CTX_CHSTATE_MASK, MHI_CH_STATE_SUSPENDED);
1229 		mhi_cntrl->ch_ctx_cache[i].chcfg = cpu_to_le32(tmp);
1230 		mutex_unlock(&mhi_chan->lock);
1231 	}
1232 }
1233 
mhi_ep_resume_channels(struct mhi_ep_cntrl * mhi_cntrl)1234 void mhi_ep_resume_channels(struct mhi_ep_cntrl *mhi_cntrl)
1235 {
1236 	struct mhi_ep_chan *mhi_chan;
1237 	u32 tmp;
1238 	int i;
1239 
1240 	for (i = 0; i < mhi_cntrl->max_chan; i++) {
1241 		mhi_chan = &mhi_cntrl->mhi_chan[i];
1242 
1243 		if (!mhi_chan->mhi_dev)
1244 			continue;
1245 
1246 		mutex_lock(&mhi_chan->lock);
1247 		/* Skip if the channel is not currently suspended */
1248 		tmp = le32_to_cpu(mhi_cntrl->ch_ctx_cache[i].chcfg);
1249 		if (FIELD_GET(CHAN_CTX_CHSTATE_MASK, tmp) != MHI_CH_STATE_SUSPENDED) {
1250 			mutex_unlock(&mhi_chan->lock);
1251 			continue;
1252 		}
1253 
1254 		dev_dbg(&mhi_chan->mhi_dev->dev, "Resuming channel\n");
1255 		/* Set channel state to RUNNING */
1256 		mhi_chan->state = MHI_CH_STATE_RUNNING;
1257 		tmp &= ~CHAN_CTX_CHSTATE_MASK;
1258 		tmp |= FIELD_PREP(CHAN_CTX_CHSTATE_MASK, MHI_CH_STATE_RUNNING);
1259 		mhi_cntrl->ch_ctx_cache[i].chcfg = cpu_to_le32(tmp);
1260 		mutex_unlock(&mhi_chan->lock);
1261 	}
1262 }
1263 
mhi_ep_release_device(struct device * dev)1264 static void mhi_ep_release_device(struct device *dev)
1265 {
1266 	struct mhi_ep_device *mhi_dev = to_mhi_ep_device(dev);
1267 
1268 	if (mhi_dev->dev_type == MHI_DEVICE_CONTROLLER)
1269 		mhi_dev->mhi_cntrl->mhi_dev = NULL;
1270 
1271 	/*
1272 	 * We need to set the mhi_chan->mhi_dev to NULL here since the MHI
1273 	 * devices for the channels will only get created in mhi_ep_create_device()
1274 	 * if the mhi_dev associated with it is NULL.
1275 	 */
1276 	if (mhi_dev->ul_chan)
1277 		mhi_dev->ul_chan->mhi_dev = NULL;
1278 
1279 	if (mhi_dev->dl_chan)
1280 		mhi_dev->dl_chan->mhi_dev = NULL;
1281 
1282 	kfree(mhi_dev);
1283 }
1284 
mhi_ep_alloc_device(struct mhi_ep_cntrl * mhi_cntrl,enum mhi_device_type dev_type)1285 static struct mhi_ep_device *mhi_ep_alloc_device(struct mhi_ep_cntrl *mhi_cntrl,
1286 						 enum mhi_device_type dev_type)
1287 {
1288 	struct mhi_ep_device *mhi_dev;
1289 	struct device *dev;
1290 
1291 	mhi_dev = kzalloc_obj(*mhi_dev);
1292 	if (!mhi_dev)
1293 		return ERR_PTR(-ENOMEM);
1294 
1295 	dev = &mhi_dev->dev;
1296 	device_initialize(dev);
1297 	dev->bus = &mhi_ep_bus_type;
1298 	dev->release = mhi_ep_release_device;
1299 
1300 	/* Controller device is always allocated first */
1301 	if (dev_type == MHI_DEVICE_CONTROLLER)
1302 		/* for MHI controller device, parent is the bus device (e.g. PCI EPF) */
1303 		dev->parent = mhi_cntrl->cntrl_dev;
1304 	else
1305 		/* for MHI client devices, parent is the MHI controller device */
1306 		dev->parent = &mhi_cntrl->mhi_dev->dev;
1307 
1308 	mhi_dev->mhi_cntrl = mhi_cntrl;
1309 	mhi_dev->dev_type = dev_type;
1310 
1311 	return mhi_dev;
1312 }
1313 
1314 /*
1315  * MHI channels are always defined in pairs with UL as the even numbered
1316  * channel and DL as odd numbered one. This function gets UL channel (primary)
1317  * as the ch_id and always looks after the next entry in channel list for
1318  * the corresponding DL channel (secondary).
1319  */
mhi_ep_create_device(struct mhi_ep_cntrl * mhi_cntrl,u32 ch_id)1320 static int mhi_ep_create_device(struct mhi_ep_cntrl *mhi_cntrl, u32 ch_id)
1321 {
1322 	struct mhi_ep_chan *mhi_chan = &mhi_cntrl->mhi_chan[ch_id];
1323 	struct device *dev = mhi_cntrl->cntrl_dev;
1324 	struct mhi_ep_device *mhi_dev;
1325 	int ret;
1326 
1327 	/* Check if the channel name is same for both UL and DL */
1328 	if (strcmp(mhi_chan->name, mhi_chan[1].name)) {
1329 		dev_err(dev, "UL and DL channel names are not same: (%s) != (%s)\n",
1330 			mhi_chan->name, mhi_chan[1].name);
1331 		return -EINVAL;
1332 	}
1333 
1334 	mhi_dev = mhi_ep_alloc_device(mhi_cntrl, MHI_DEVICE_XFER);
1335 	if (IS_ERR(mhi_dev))
1336 		return PTR_ERR(mhi_dev);
1337 
1338 	/* Configure primary channel */
1339 	mhi_dev->ul_chan = mhi_chan;
1340 	get_device(&mhi_dev->dev);
1341 	mhi_chan->mhi_dev = mhi_dev;
1342 
1343 	/* Configure secondary channel as well */
1344 	mhi_chan++;
1345 	mhi_dev->dl_chan = mhi_chan;
1346 	get_device(&mhi_dev->dev);
1347 	mhi_chan->mhi_dev = mhi_dev;
1348 
1349 	/* Channel name is same for both UL and DL */
1350 	mhi_dev->name = mhi_chan->name;
1351 	ret = dev_set_name(&mhi_dev->dev, "%s_%s",
1352 		     dev_name(&mhi_cntrl->mhi_dev->dev),
1353 		     mhi_dev->name);
1354 	if (ret)
1355 		goto err_put_channels;
1356 
1357 	ret = device_add(&mhi_dev->dev);
1358 	if (ret)
1359 		goto err_put_channels;
1360 
1361 	return 0;
1362 
1363 err_put_channels:
1364 	put_device(&mhi_dev->dev); /* DL channel reference */
1365 	put_device(&mhi_dev->dev); /* UL channel reference */
1366 	put_device(&mhi_dev->dev); /* device_initialize() reference */
1367 
1368 	return ret;
1369 }
1370 
mhi_ep_destroy_device(struct device * dev,void * data)1371 static int mhi_ep_destroy_device(struct device *dev, void *data)
1372 {
1373 	struct mhi_ep_device *mhi_dev;
1374 	struct mhi_ep_cntrl *mhi_cntrl;
1375 	struct mhi_ep_chan *ul_chan, *dl_chan;
1376 
1377 	if (dev->bus != &mhi_ep_bus_type)
1378 		return 0;
1379 
1380 	mhi_dev = to_mhi_ep_device(dev);
1381 	mhi_cntrl = mhi_dev->mhi_cntrl;
1382 
1383 	/* Only destroy devices created for channels */
1384 	if (mhi_dev->dev_type == MHI_DEVICE_CONTROLLER)
1385 		return 0;
1386 
1387 	ul_chan = mhi_dev->ul_chan;
1388 	dl_chan = mhi_dev->dl_chan;
1389 
1390 	if (ul_chan)
1391 		put_device(&ul_chan->mhi_dev->dev);
1392 
1393 	if (dl_chan)
1394 		put_device(&dl_chan->mhi_dev->dev);
1395 
1396 	dev_dbg(&mhi_cntrl->mhi_dev->dev, "Destroying device for chan:%s\n",
1397 		 mhi_dev->name);
1398 
1399 	/* Notify the client and remove the device from MHI bus */
1400 	device_del(dev);
1401 	put_device(dev);
1402 
1403 	return 0;
1404 }
1405 
mhi_ep_chan_init(struct mhi_ep_cntrl * mhi_cntrl,const struct mhi_ep_cntrl_config * config)1406 static int mhi_ep_chan_init(struct mhi_ep_cntrl *mhi_cntrl,
1407 			    const struct mhi_ep_cntrl_config *config)
1408 {
1409 	const struct mhi_ep_channel_config *ch_cfg;
1410 	struct device *dev = mhi_cntrl->cntrl_dev;
1411 	u32 chan, i;
1412 	int ret = -EINVAL;
1413 
1414 	mhi_cntrl->max_chan = config->max_channels;
1415 
1416 	/*
1417 	 * Allocate max_channels supported by the MHI endpoint and populate
1418 	 * only the defined channels
1419 	 */
1420 	mhi_cntrl->mhi_chan = kzalloc_objs(*mhi_cntrl->mhi_chan,
1421 					   mhi_cntrl->max_chan);
1422 	if (!mhi_cntrl->mhi_chan)
1423 		return -ENOMEM;
1424 
1425 	for (i = 0; i < config->num_channels; i++) {
1426 		struct mhi_ep_chan *mhi_chan;
1427 
1428 		ch_cfg = &config->ch_cfg[i];
1429 
1430 		chan = ch_cfg->num;
1431 		if (chan >= mhi_cntrl->max_chan) {
1432 			dev_err(dev, "Channel (%u) exceeds maximum available channels (%u)\n",
1433 				chan, mhi_cntrl->max_chan);
1434 			goto error_chan_cfg;
1435 		}
1436 
1437 		/* Bi-directional and direction less channels are not supported */
1438 		if (ch_cfg->dir == DMA_BIDIRECTIONAL || ch_cfg->dir == DMA_NONE) {
1439 			dev_err(dev, "Invalid direction (%u) for channel (%u)\n",
1440 				ch_cfg->dir, chan);
1441 			goto error_chan_cfg;
1442 		}
1443 
1444 		mhi_chan = &mhi_cntrl->mhi_chan[chan];
1445 		mhi_chan->name = ch_cfg->name;
1446 		mhi_chan->chan = chan;
1447 		mhi_chan->dir = ch_cfg->dir;
1448 		mutex_init(&mhi_chan->lock);
1449 	}
1450 
1451 	return 0;
1452 
1453 error_chan_cfg:
1454 	kfree(mhi_cntrl->mhi_chan);
1455 
1456 	return ret;
1457 }
1458 
1459 /*
1460  * Allocate channel and command rings here. Event rings will be allocated
1461  * in mhi_ep_power_up() as the config comes from the host.
1462  */
mhi_ep_register_controller(struct mhi_ep_cntrl * mhi_cntrl,const struct mhi_ep_cntrl_config * config)1463 int mhi_ep_register_controller(struct mhi_ep_cntrl *mhi_cntrl,
1464 				const struct mhi_ep_cntrl_config *config)
1465 {
1466 	struct mhi_ep_device *mhi_dev;
1467 	int ret;
1468 
1469 	if (!mhi_cntrl || !mhi_cntrl->cntrl_dev || !mhi_cntrl->mmio || !mhi_cntrl->irq)
1470 		return -EINVAL;
1471 
1472 	if (!mhi_cntrl->read_sync || !mhi_cntrl->write_sync ||
1473 	    !mhi_cntrl->read_async || !mhi_cntrl->write_async)
1474 		return -EINVAL;
1475 
1476 	ret = mhi_ep_chan_init(mhi_cntrl, config);
1477 	if (ret)
1478 		return ret;
1479 
1480 	mhi_cntrl->mhi_cmd = kzalloc_objs(*mhi_cntrl->mhi_cmd, NR_OF_CMD_RINGS);
1481 	if (!mhi_cntrl->mhi_cmd) {
1482 		ret = -ENOMEM;
1483 		goto err_free_ch;
1484 	}
1485 
1486 	mhi_cntrl->ev_ring_el_cache = kmem_cache_create("mhi_ep_event_ring_el",
1487 							sizeof(struct mhi_ring_element), 0,
1488 							0, NULL);
1489 	if (!mhi_cntrl->ev_ring_el_cache) {
1490 		ret = -ENOMEM;
1491 		goto err_free_cmd;
1492 	}
1493 
1494 	mhi_cntrl->tre_buf_cache = kmem_cache_create("mhi_ep_tre_buf", MHI_EP_DEFAULT_MTU, 0,
1495 						      0, NULL);
1496 	if (!mhi_cntrl->tre_buf_cache) {
1497 		ret = -ENOMEM;
1498 		goto err_destroy_ev_ring_el_cache;
1499 	}
1500 
1501 	mhi_cntrl->ring_item_cache = kmem_cache_create("mhi_ep_ring_item",
1502 							sizeof(struct mhi_ep_ring_item), 0,
1503 							0, NULL);
1504 	if (!mhi_cntrl->ring_item_cache) {
1505 		ret = -ENOMEM;
1506 		goto err_destroy_tre_buf_cache;
1507 	}
1508 
1509 	INIT_WORK(&mhi_cntrl->state_work, mhi_ep_state_worker);
1510 	INIT_WORK(&mhi_cntrl->reset_work, mhi_ep_reset_worker);
1511 	INIT_WORK(&mhi_cntrl->cmd_ring_work, mhi_ep_cmd_ring_worker);
1512 	INIT_WORK(&mhi_cntrl->ch_ring_work, mhi_ep_ch_ring_worker);
1513 
1514 	mhi_cntrl->wq = alloc_workqueue("mhi_ep_wq", WQ_PERCPU, 0);
1515 	if (!mhi_cntrl->wq) {
1516 		ret = -ENOMEM;
1517 		goto err_destroy_ring_item_cache;
1518 	}
1519 
1520 	INIT_LIST_HEAD(&mhi_cntrl->st_transition_list);
1521 	INIT_LIST_HEAD(&mhi_cntrl->ch_db_list);
1522 	spin_lock_init(&mhi_cntrl->list_lock);
1523 	mutex_init(&mhi_cntrl->state_lock);
1524 	mutex_init(&mhi_cntrl->event_lock);
1525 
1526 	/* Set MHI version and AMSS EE before enumeration */
1527 	mhi_ep_mmio_write(mhi_cntrl, EP_MHIVER, config->mhi_version);
1528 	mhi_ep_mmio_set_env(mhi_cntrl, MHI_EE_AMSS);
1529 
1530 	/* Set controller index */
1531 	ret = ida_alloc(&mhi_ep_cntrl_ida, GFP_KERNEL);
1532 	if (ret < 0)
1533 		goto err_destroy_wq;
1534 
1535 	mhi_cntrl->index = ret;
1536 
1537 	irq_set_status_flags(mhi_cntrl->irq, IRQ_NOAUTOEN);
1538 	ret = request_irq(mhi_cntrl->irq, mhi_ep_irq, IRQF_TRIGGER_HIGH,
1539 			  "doorbell_irq", mhi_cntrl);
1540 	if (ret) {
1541 		dev_err(mhi_cntrl->cntrl_dev, "Failed to request Doorbell IRQ\n");
1542 		goto err_ida_free;
1543 	}
1544 
1545 	/* Allocate the controller device */
1546 	mhi_dev = mhi_ep_alloc_device(mhi_cntrl, MHI_DEVICE_CONTROLLER);
1547 	if (IS_ERR(mhi_dev)) {
1548 		dev_err(mhi_cntrl->cntrl_dev, "Failed to allocate controller device\n");
1549 		ret = PTR_ERR(mhi_dev);
1550 		goto err_free_irq;
1551 	}
1552 
1553 	ret = dev_set_name(&mhi_dev->dev, "mhi_ep%u", mhi_cntrl->index);
1554 	if (ret)
1555 		goto err_put_dev;
1556 
1557 	mhi_dev->name = dev_name(&mhi_dev->dev);
1558 	mhi_cntrl->mhi_dev = mhi_dev;
1559 
1560 	ret = device_add(&mhi_dev->dev);
1561 	if (ret)
1562 		goto err_put_dev;
1563 
1564 	dev_dbg(&mhi_dev->dev, "MHI EP Controller registered\n");
1565 
1566 	return 0;
1567 
1568 err_put_dev:
1569 	put_device(&mhi_dev->dev);
1570 err_free_irq:
1571 	free_irq(mhi_cntrl->irq, mhi_cntrl);
1572 err_ida_free:
1573 	ida_free(&mhi_ep_cntrl_ida, mhi_cntrl->index);
1574 err_destroy_wq:
1575 	destroy_workqueue(mhi_cntrl->wq);
1576 err_destroy_ring_item_cache:
1577 	kmem_cache_destroy(mhi_cntrl->ring_item_cache);
1578 err_destroy_ev_ring_el_cache:
1579 	kmem_cache_destroy(mhi_cntrl->ev_ring_el_cache);
1580 err_destroy_tre_buf_cache:
1581 	kmem_cache_destroy(mhi_cntrl->tre_buf_cache);
1582 err_free_cmd:
1583 	kfree(mhi_cntrl->mhi_cmd);
1584 err_free_ch:
1585 	kfree(mhi_cntrl->mhi_chan);
1586 
1587 	return ret;
1588 }
1589 EXPORT_SYMBOL_GPL(mhi_ep_register_controller);
1590 
1591 /*
1592  * It is expected that the controller drivers will power down the MHI EP stack
1593  * using "mhi_ep_power_down()" before calling this function to unregister themselves.
1594  */
mhi_ep_unregister_controller(struct mhi_ep_cntrl * mhi_cntrl)1595 void mhi_ep_unregister_controller(struct mhi_ep_cntrl *mhi_cntrl)
1596 {
1597 	struct mhi_ep_device *mhi_dev = mhi_cntrl->mhi_dev;
1598 
1599 	destroy_workqueue(mhi_cntrl->wq);
1600 
1601 	free_irq(mhi_cntrl->irq, mhi_cntrl);
1602 
1603 	kmem_cache_destroy(mhi_cntrl->tre_buf_cache);
1604 	kmem_cache_destroy(mhi_cntrl->ev_ring_el_cache);
1605 	kmem_cache_destroy(mhi_cntrl->ring_item_cache);
1606 	kfree(mhi_cntrl->mhi_cmd);
1607 	kfree(mhi_cntrl->mhi_chan);
1608 
1609 	device_del(&mhi_dev->dev);
1610 	put_device(&mhi_dev->dev);
1611 
1612 	ida_free(&mhi_ep_cntrl_ida, mhi_cntrl->index);
1613 }
1614 EXPORT_SYMBOL_GPL(mhi_ep_unregister_controller);
1615 
mhi_ep_probe(struct device * dev)1616 static int mhi_ep_probe(struct device *dev)
1617 {
1618 	struct mhi_ep_device *mhi_dev = to_mhi_ep_device(dev);
1619 	struct mhi_ep_driver *mhi_drv = to_mhi_ep_driver(dev->driver);
1620 	struct mhi_ep_chan *ul_chan = mhi_dev->ul_chan;
1621 	struct mhi_ep_chan *dl_chan = mhi_dev->dl_chan;
1622 
1623 	ul_chan->xfer_cb = mhi_drv->ul_xfer_cb;
1624 	dl_chan->xfer_cb = mhi_drv->dl_xfer_cb;
1625 
1626 	return mhi_drv->probe(mhi_dev, mhi_dev->id);
1627 }
1628 
mhi_ep_remove(struct device * dev)1629 static void mhi_ep_remove(struct device *dev)
1630 {
1631 	struct mhi_ep_device *mhi_dev = to_mhi_ep_device(dev);
1632 	struct mhi_ep_driver *mhi_drv = to_mhi_ep_driver(dev->driver);
1633 	struct mhi_ep_cntrl *mhi_cntrl = mhi_dev->mhi_cntrl;
1634 	struct mhi_result result = {};
1635 	struct mhi_ep_chan *mhi_chan;
1636 	int dir;
1637 
1638 	/* Skip if it is a controller device */
1639 	if (mhi_dev->dev_type == MHI_DEVICE_CONTROLLER)
1640 		return;
1641 
1642 	/* Disable the channels to prevent new transfers */
1643 	for (dir = 0; dir < 2; dir++) {
1644 		mhi_chan = dir ? mhi_dev->ul_chan : mhi_dev->dl_chan;
1645 
1646 		if (!mhi_chan)
1647 			continue;
1648 
1649 		mutex_lock(&mhi_chan->lock);
1650 		mhi_chan->state = MHI_CH_STATE_DISABLED;
1651 		mutex_unlock(&mhi_chan->lock);
1652 	}
1653 
1654 	/* Flush in-flight transfers before notifying disconnect */
1655 	if (mhi_cntrl->flush_async)
1656 		mhi_cntrl->flush_async(mhi_cntrl);
1657 
1658 	/* Disconnect the channels associated with the driver */
1659 	for (dir = 0; dir < 2; dir++) {
1660 		mhi_chan = dir ? mhi_dev->ul_chan : mhi_dev->dl_chan;
1661 
1662 		if (!mhi_chan)
1663 			continue;
1664 
1665 		mutex_lock(&mhi_chan->lock);
1666 		/* Send channel disconnect status to the client driver */
1667 		if (mhi_chan->xfer_cb) {
1668 			result.transaction_status = -ENOTCONN;
1669 			result.bytes_xferd = 0;
1670 			mhi_chan->xfer_cb(mhi_chan->mhi_dev, &result);
1671 		}
1672 
1673 		mhi_chan->xfer_cb = NULL;
1674 		mutex_unlock(&mhi_chan->lock);
1675 	}
1676 
1677 	/* Remove the client driver now */
1678 	mhi_drv->remove(mhi_dev);
1679 }
1680 
__mhi_ep_driver_register(struct mhi_ep_driver * mhi_drv,struct module * owner)1681 int __mhi_ep_driver_register(struct mhi_ep_driver *mhi_drv, struct module *owner)
1682 {
1683 	struct device_driver *driver = &mhi_drv->driver;
1684 
1685 	if (!mhi_drv->probe || !mhi_drv->remove)
1686 		return -EINVAL;
1687 
1688 	/* Client drivers should have callbacks defined for both channels */
1689 	if (!mhi_drv->ul_xfer_cb || !mhi_drv->dl_xfer_cb)
1690 		return -EINVAL;
1691 
1692 	driver->bus = &mhi_ep_bus_type;
1693 	driver->owner = owner;
1694 
1695 	return driver_register(driver);
1696 }
1697 EXPORT_SYMBOL_GPL(__mhi_ep_driver_register);
1698 
mhi_ep_driver_unregister(struct mhi_ep_driver * mhi_drv)1699 void mhi_ep_driver_unregister(struct mhi_ep_driver *mhi_drv)
1700 {
1701 	driver_unregister(&mhi_drv->driver);
1702 }
1703 EXPORT_SYMBOL_GPL(mhi_ep_driver_unregister);
1704 
mhi_ep_uevent(const struct device * dev,struct kobj_uevent_env * env)1705 static int mhi_ep_uevent(const struct device *dev, struct kobj_uevent_env *env)
1706 {
1707 	const struct mhi_ep_device *mhi_dev = to_mhi_ep_device(dev);
1708 
1709 	return add_uevent_var(env, "MODALIAS=" MHI_EP_DEVICE_MODALIAS_FMT,
1710 					mhi_dev->name);
1711 }
1712 
mhi_ep_match(struct device * dev,const struct device_driver * drv)1713 static int mhi_ep_match(struct device *dev, const struct device_driver *drv)
1714 {
1715 	struct mhi_ep_device *mhi_dev = to_mhi_ep_device(dev);
1716 	const struct mhi_ep_driver *mhi_drv = to_mhi_ep_driver(drv);
1717 	const struct mhi_device_id *id;
1718 
1719 	/*
1720 	 * If the device is a controller type then there is no client driver
1721 	 * associated with it
1722 	 */
1723 	if (mhi_dev->dev_type == MHI_DEVICE_CONTROLLER)
1724 		return 0;
1725 
1726 	for (id = mhi_drv->id_table; id->chan[0]; id++)
1727 		if (!strcmp(mhi_dev->name, id->chan)) {
1728 			mhi_dev->id = id;
1729 			return 1;
1730 		}
1731 
1732 	return 0;
1733 };
1734 
1735 const struct bus_type mhi_ep_bus_type = {
1736 	.name = "mhi_ep",
1737 	.dev_name = "mhi_ep",
1738 	.match = mhi_ep_match,
1739 	.uevent = mhi_ep_uevent,
1740 	.probe = mhi_ep_probe,
1741 	.remove = mhi_ep_remove,
1742 };
1743 
mhi_ep_init(void)1744 static int __init mhi_ep_init(void)
1745 {
1746 	return bus_register(&mhi_ep_bus_type);
1747 }
1748 
mhi_ep_exit(void)1749 static void __exit mhi_ep_exit(void)
1750 {
1751 	bus_unregister(&mhi_ep_bus_type);
1752 }
1753 
1754 postcore_initcall(mhi_ep_init);
1755 module_exit(mhi_ep_exit);
1756 
1757 MODULE_LICENSE("GPL v2");
1758 MODULE_DESCRIPTION("MHI Bus Endpoint stack");
1759 MODULE_AUTHOR("Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>");
1760