xref: /linux/drivers/rpmsg/qcom_glink_native.c (revision 63307d015b91e626c97bb82e88054af3d0b74643)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2016-2017, Linaro Ltd
4  */
5 
6 #include <linux/idr.h>
7 #include <linux/interrupt.h>
8 #include <linux/io.h>
9 #include <linux/list.h>
10 #include <linux/mfd/syscon.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/of_address.h>
14 #include <linux/of_irq.h>
15 #include <linux/platform_device.h>
16 #include <linux/regmap.h>
17 #include <linux/rpmsg.h>
18 #include <linux/sizes.h>
19 #include <linux/slab.h>
20 #include <linux/workqueue.h>
21 #include <linux/mailbox_client.h>
22 
23 #include "rpmsg_internal.h"
24 #include "qcom_glink_native.h"
25 
26 #define GLINK_NAME_SIZE		32
27 #define GLINK_VERSION_1		1
28 
29 #define RPM_GLINK_CID_MIN	1
30 #define RPM_GLINK_CID_MAX	65536
31 
32 struct glink_msg {
33 	__le16 cmd;
34 	__le16 param1;
35 	__le32 param2;
36 	u8 data[];
37 } __packed;
38 
39 /**
40  * struct glink_defer_cmd - deferred incoming control message
41  * @node:	list node
42  * @msg:	message header
43  * @data:	payload of the message
44  *
45  * Copy of a received control message, to be added to @rx_queue and processed
46  * by @rx_work of @qcom_glink.
47  */
48 struct glink_defer_cmd {
49 	struct list_head node;
50 
51 	struct glink_msg msg;
52 	u8 data[];
53 };
54 
55 /**
56  * struct glink_core_rx_intent - RX intent
57  * RX intent
58  *
59  * @data: pointer to the data (may be NULL for zero-copy)
60  * @id: remote or local intent ID
61  * @size: size of the original intent (do not modify)
62  * @reuse: To mark if the intent can be reused after first use
63  * @in_use: To mark if intent is already in use for the channel
64  * @offset: next write offset (initially 0)
65  * @node:	list node
66  */
67 struct glink_core_rx_intent {
68 	void *data;
69 	u32 id;
70 	size_t size;
71 	bool reuse;
72 	bool in_use;
73 	u32 offset;
74 
75 	struct list_head node;
76 };
77 
78 /**
79  * struct qcom_glink - driver context, relates to one remote subsystem
80  * @dev:	reference to the associated struct device
81  * @mbox_client: mailbox client
82  * @mbox_chan:  mailbox channel
83  * @rx_pipe:	pipe object for receive FIFO
84  * @tx_pipe:	pipe object for transmit FIFO
85  * @irq:	IRQ for signaling incoming events
86  * @rx_work:	worker for handling received control messages
87  * @rx_lock:	protects the @rx_queue
88  * @rx_queue:	queue of received control messages to be processed in @rx_work
89  * @tx_lock:	synchronizes operations on the tx fifo
90  * @idr_lock:	synchronizes @lcids and @rcids modifications
91  * @lcids:	idr of all channels with a known local channel id
92  * @rcids:	idr of all channels with a known remote channel id
93  * @features:	remote features
94  * @intentless:	flag to indicate that there is no intent
95  */
96 struct qcom_glink {
97 	struct device *dev;
98 
99 	const char *name;
100 
101 	struct mbox_client mbox_client;
102 	struct mbox_chan *mbox_chan;
103 
104 	struct qcom_glink_pipe *rx_pipe;
105 	struct qcom_glink_pipe *tx_pipe;
106 
107 	int irq;
108 
109 	struct work_struct rx_work;
110 	spinlock_t rx_lock;
111 	struct list_head rx_queue;
112 
113 	spinlock_t tx_lock;
114 
115 	spinlock_t idr_lock;
116 	struct idr lcids;
117 	struct idr rcids;
118 	unsigned long features;
119 
120 	bool intentless;
121 };
122 
123 enum {
124 	GLINK_STATE_CLOSED,
125 	GLINK_STATE_OPENING,
126 	GLINK_STATE_OPEN,
127 	GLINK_STATE_CLOSING,
128 };
129 
130 /**
131  * struct glink_channel - internal representation of a channel
132  * @rpdev:	rpdev reference, only used for primary endpoints
133  * @ept:	rpmsg endpoint this channel is associated with
134  * @glink:	qcom_glink context handle
135  * @refcount:	refcount for the channel object
136  * @recv_lock:	guard for @ept.cb
137  * @name:	unique channel name/identifier
138  * @lcid:	channel id, in local space
139  * @rcid:	channel id, in remote space
140  * @intent_lock: lock for protection of @liids, @riids
141  * @liids:	idr of all local intents
142  * @riids:	idr of all remote intents
143  * @intent_work: worker responsible for transmitting rx_done packets
144  * @done_intents: list of intents that needs to be announced rx_done
145  * @buf:	receive buffer, for gathering fragments
146  * @buf_offset:	write offset in @buf
147  * @buf_size:	size of current @buf
148  * @open_ack:	completed once remote has acked the open-request
149  * @open_req:	completed once open-request has been received
150  * @intent_req_lock: Synchronises multiple intent requests
151  * @intent_req_result: Result of intent request
152  * @intent_req_comp: Completion for intent_req signalling
153  */
154 struct glink_channel {
155 	struct rpmsg_endpoint ept;
156 
157 	struct rpmsg_device *rpdev;
158 	struct qcom_glink *glink;
159 
160 	struct kref refcount;
161 
162 	spinlock_t recv_lock;
163 
164 	char *name;
165 	unsigned int lcid;
166 	unsigned int rcid;
167 
168 	spinlock_t intent_lock;
169 	struct idr liids;
170 	struct idr riids;
171 	struct work_struct intent_work;
172 	struct list_head done_intents;
173 
174 	struct glink_core_rx_intent *buf;
175 	int buf_offset;
176 	int buf_size;
177 
178 	struct completion open_ack;
179 	struct completion open_req;
180 
181 	struct mutex intent_req_lock;
182 	bool intent_req_result;
183 	struct completion intent_req_comp;
184 };
185 
186 #define to_glink_channel(_ept) container_of(_ept, struct glink_channel, ept)
187 
188 static const struct rpmsg_endpoint_ops glink_endpoint_ops;
189 
190 #define RPM_CMD_VERSION			0
191 #define RPM_CMD_VERSION_ACK		1
192 #define RPM_CMD_OPEN			2
193 #define RPM_CMD_CLOSE			3
194 #define RPM_CMD_OPEN_ACK		4
195 #define RPM_CMD_INTENT			5
196 #define RPM_CMD_RX_DONE			6
197 #define RPM_CMD_RX_INTENT_REQ		7
198 #define RPM_CMD_RX_INTENT_REQ_ACK	8
199 #define RPM_CMD_TX_DATA			9
200 #define RPM_CMD_CLOSE_ACK		11
201 #define RPM_CMD_TX_DATA_CONT		12
202 #define RPM_CMD_READ_NOTIF		13
203 #define RPM_CMD_RX_DONE_W_REUSE		14
204 
205 #define GLINK_FEATURE_INTENTLESS	BIT(1)
206 
207 static void qcom_glink_rx_done_work(struct work_struct *work);
208 
209 static struct glink_channel *qcom_glink_alloc_channel(struct qcom_glink *glink,
210 						      const char *name)
211 {
212 	struct glink_channel *channel;
213 
214 	channel = kzalloc(sizeof(*channel), GFP_KERNEL);
215 	if (!channel)
216 		return ERR_PTR(-ENOMEM);
217 
218 	/* Setup glink internal glink_channel data */
219 	spin_lock_init(&channel->recv_lock);
220 	spin_lock_init(&channel->intent_lock);
221 	mutex_init(&channel->intent_req_lock);
222 
223 	channel->glink = glink;
224 	channel->name = kstrdup(name, GFP_KERNEL);
225 
226 	init_completion(&channel->open_req);
227 	init_completion(&channel->open_ack);
228 	init_completion(&channel->intent_req_comp);
229 
230 	INIT_LIST_HEAD(&channel->done_intents);
231 	INIT_WORK(&channel->intent_work, qcom_glink_rx_done_work);
232 
233 	idr_init(&channel->liids);
234 	idr_init(&channel->riids);
235 	kref_init(&channel->refcount);
236 
237 	return channel;
238 }
239 
240 static void qcom_glink_channel_release(struct kref *ref)
241 {
242 	struct glink_channel *channel = container_of(ref, struct glink_channel,
243 						     refcount);
244 	unsigned long flags;
245 
246 	spin_lock_irqsave(&channel->intent_lock, flags);
247 	idr_destroy(&channel->liids);
248 	idr_destroy(&channel->riids);
249 	spin_unlock_irqrestore(&channel->intent_lock, flags);
250 
251 	kfree(channel->name);
252 	kfree(channel);
253 }
254 
255 static size_t qcom_glink_rx_avail(struct qcom_glink *glink)
256 {
257 	return glink->rx_pipe->avail(glink->rx_pipe);
258 }
259 
260 static void qcom_glink_rx_peak(struct qcom_glink *glink,
261 			       void *data, unsigned int offset, size_t count)
262 {
263 	glink->rx_pipe->peak(glink->rx_pipe, data, offset, count);
264 }
265 
266 static void qcom_glink_rx_advance(struct qcom_glink *glink, size_t count)
267 {
268 	glink->rx_pipe->advance(glink->rx_pipe, count);
269 }
270 
271 static size_t qcom_glink_tx_avail(struct qcom_glink *glink)
272 {
273 	return glink->tx_pipe->avail(glink->tx_pipe);
274 }
275 
276 static void qcom_glink_tx_write(struct qcom_glink *glink,
277 				const void *hdr, size_t hlen,
278 				const void *data, size_t dlen)
279 {
280 	glink->tx_pipe->write(glink->tx_pipe, hdr, hlen, data, dlen);
281 }
282 
283 static int qcom_glink_tx(struct qcom_glink *glink,
284 			 const void *hdr, size_t hlen,
285 			 const void *data, size_t dlen, bool wait)
286 {
287 	unsigned int tlen = hlen + dlen;
288 	unsigned long flags;
289 	int ret = 0;
290 
291 	/* Reject packets that are too big */
292 	if (tlen >= glink->tx_pipe->length)
293 		return -EINVAL;
294 
295 	spin_lock_irqsave(&glink->tx_lock, flags);
296 
297 	while (qcom_glink_tx_avail(glink) < tlen) {
298 		if (!wait) {
299 			ret = -EAGAIN;
300 			goto out;
301 		}
302 
303 		/* Wait without holding the tx_lock */
304 		spin_unlock_irqrestore(&glink->tx_lock, flags);
305 
306 		usleep_range(10000, 15000);
307 
308 		spin_lock_irqsave(&glink->tx_lock, flags);
309 	}
310 
311 	qcom_glink_tx_write(glink, hdr, hlen, data, dlen);
312 
313 	mbox_send_message(glink->mbox_chan, NULL);
314 	mbox_client_txdone(glink->mbox_chan, 0);
315 
316 out:
317 	spin_unlock_irqrestore(&glink->tx_lock, flags);
318 
319 	return ret;
320 }
321 
322 static int qcom_glink_send_version(struct qcom_glink *glink)
323 {
324 	struct glink_msg msg;
325 
326 	msg.cmd = cpu_to_le16(RPM_CMD_VERSION);
327 	msg.param1 = cpu_to_le16(GLINK_VERSION_1);
328 	msg.param2 = cpu_to_le32(glink->features);
329 
330 	return qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
331 }
332 
333 static void qcom_glink_send_version_ack(struct qcom_glink *glink)
334 {
335 	struct glink_msg msg;
336 
337 	msg.cmd = cpu_to_le16(RPM_CMD_VERSION_ACK);
338 	msg.param1 = cpu_to_le16(GLINK_VERSION_1);
339 	msg.param2 = cpu_to_le32(glink->features);
340 
341 	qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
342 }
343 
344 static void qcom_glink_send_open_ack(struct qcom_glink *glink,
345 				     struct glink_channel *channel)
346 {
347 	struct glink_msg msg;
348 
349 	msg.cmd = cpu_to_le16(RPM_CMD_OPEN_ACK);
350 	msg.param1 = cpu_to_le16(channel->rcid);
351 	msg.param2 = cpu_to_le32(0);
352 
353 	qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
354 }
355 
356 static void qcom_glink_handle_intent_req_ack(struct qcom_glink *glink,
357 					     unsigned int cid, bool granted)
358 {
359 	struct glink_channel *channel;
360 	unsigned long flags;
361 
362 	spin_lock_irqsave(&glink->idr_lock, flags);
363 	channel = idr_find(&glink->rcids, cid);
364 	spin_unlock_irqrestore(&glink->idr_lock, flags);
365 	if (!channel) {
366 		dev_err(glink->dev, "unable to find channel\n");
367 		return;
368 	}
369 
370 	channel->intent_req_result = granted;
371 	complete(&channel->intent_req_comp);
372 }
373 
374 /**
375  * qcom_glink_send_open_req() - send a RPM_CMD_OPEN request to the remote
376  * @glink: Ptr to the glink edge
377  * @channel: Ptr to the channel that the open req is sent
378  *
379  * Allocates a local channel id and sends a RPM_CMD_OPEN message to the remote.
380  * Will return with refcount held, regardless of outcome.
381  *
382  * Returns 0 on success, negative errno otherwise.
383  */
384 static int qcom_glink_send_open_req(struct qcom_glink *glink,
385 				    struct glink_channel *channel)
386 {
387 	struct {
388 		struct glink_msg msg;
389 		u8 name[GLINK_NAME_SIZE];
390 	} __packed req;
391 	int name_len = strlen(channel->name) + 1;
392 	int req_len = ALIGN(sizeof(req.msg) + name_len, 8);
393 	int ret;
394 	unsigned long flags;
395 
396 	kref_get(&channel->refcount);
397 
398 	spin_lock_irqsave(&glink->idr_lock, flags);
399 	ret = idr_alloc_cyclic(&glink->lcids, channel,
400 			       RPM_GLINK_CID_MIN, RPM_GLINK_CID_MAX,
401 			       GFP_ATOMIC);
402 	spin_unlock_irqrestore(&glink->idr_lock, flags);
403 	if (ret < 0)
404 		return ret;
405 
406 	channel->lcid = ret;
407 
408 	req.msg.cmd = cpu_to_le16(RPM_CMD_OPEN);
409 	req.msg.param1 = cpu_to_le16(channel->lcid);
410 	req.msg.param2 = cpu_to_le32(name_len);
411 	strcpy(req.name, channel->name);
412 
413 	ret = qcom_glink_tx(glink, &req, req_len, NULL, 0, true);
414 	if (ret)
415 		goto remove_idr;
416 
417 	return 0;
418 
419 remove_idr:
420 	spin_lock_irqsave(&glink->idr_lock, flags);
421 	idr_remove(&glink->lcids, channel->lcid);
422 	channel->lcid = 0;
423 	spin_unlock_irqrestore(&glink->idr_lock, flags);
424 
425 	return ret;
426 }
427 
428 static void qcom_glink_send_close_req(struct qcom_glink *glink,
429 				      struct glink_channel *channel)
430 {
431 	struct glink_msg req;
432 
433 	req.cmd = cpu_to_le16(RPM_CMD_CLOSE);
434 	req.param1 = cpu_to_le16(channel->lcid);
435 	req.param2 = 0;
436 
437 	qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
438 }
439 
440 static void qcom_glink_send_close_ack(struct qcom_glink *glink,
441 				      unsigned int rcid)
442 {
443 	struct glink_msg req;
444 
445 	req.cmd = cpu_to_le16(RPM_CMD_CLOSE_ACK);
446 	req.param1 = cpu_to_le16(rcid);
447 	req.param2 = 0;
448 
449 	qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
450 }
451 
452 static void qcom_glink_rx_done_work(struct work_struct *work)
453 {
454 	struct glink_channel *channel = container_of(work, struct glink_channel,
455 						     intent_work);
456 	struct qcom_glink *glink = channel->glink;
457 	struct glink_core_rx_intent *intent, *tmp;
458 	struct {
459 		u16 id;
460 		u16 lcid;
461 		u32 liid;
462 	} __packed cmd;
463 
464 	unsigned int cid = channel->lcid;
465 	unsigned int iid;
466 	bool reuse;
467 	unsigned long flags;
468 
469 	spin_lock_irqsave(&channel->intent_lock, flags);
470 	list_for_each_entry_safe(intent, tmp, &channel->done_intents, node) {
471 		list_del(&intent->node);
472 		spin_unlock_irqrestore(&channel->intent_lock, flags);
473 		iid = intent->id;
474 		reuse = intent->reuse;
475 
476 		cmd.id = reuse ? RPM_CMD_RX_DONE_W_REUSE : RPM_CMD_RX_DONE;
477 		cmd.lcid = cid;
478 		cmd.liid = iid;
479 
480 		qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
481 		if (!reuse) {
482 			kfree(intent->data);
483 			kfree(intent);
484 		}
485 		spin_lock_irqsave(&channel->intent_lock, flags);
486 	}
487 	spin_unlock_irqrestore(&channel->intent_lock, flags);
488 }
489 
490 static void qcom_glink_rx_done(struct qcom_glink *glink,
491 			       struct glink_channel *channel,
492 			       struct glink_core_rx_intent *intent)
493 {
494 	/* We don't send RX_DONE to intentless systems */
495 	if (glink->intentless) {
496 		kfree(intent->data);
497 		kfree(intent);
498 		return;
499 	}
500 
501 	/* Take it off the tree of receive intents */
502 	if (!intent->reuse) {
503 		spin_lock(&channel->intent_lock);
504 		idr_remove(&channel->liids, intent->id);
505 		spin_unlock(&channel->intent_lock);
506 	}
507 
508 	/* Schedule the sending of a rx_done indication */
509 	spin_lock(&channel->intent_lock);
510 	list_add_tail(&intent->node, &channel->done_intents);
511 	spin_unlock(&channel->intent_lock);
512 
513 	schedule_work(&channel->intent_work);
514 }
515 
516 /**
517  * qcom_glink_receive_version() - receive version/features from remote system
518  *
519  * @glink:	pointer to transport interface
520  * @version:	remote version
521  * @features:	remote features
522  *
523  * This function is called in response to a remote-initiated version/feature
524  * negotiation sequence.
525  */
526 static void qcom_glink_receive_version(struct qcom_glink *glink,
527 				       u32 version,
528 				       u32 features)
529 {
530 	switch (version) {
531 	case 0:
532 		break;
533 	case GLINK_VERSION_1:
534 		glink->features &= features;
535 		/* FALLTHROUGH */
536 	default:
537 		qcom_glink_send_version_ack(glink);
538 		break;
539 	}
540 }
541 
542 /**
543  * qcom_glink_receive_version_ack() - receive negotiation ack from remote system
544  *
545  * @glink:	pointer to transport interface
546  * @version:	remote version response
547  * @features:	remote features response
548  *
549  * This function is called in response to a local-initiated version/feature
550  * negotiation sequence and is the counter-offer from the remote side based
551  * upon the initial version and feature set requested.
552  */
553 static void qcom_glink_receive_version_ack(struct qcom_glink *glink,
554 					   u32 version,
555 					   u32 features)
556 {
557 	switch (version) {
558 	case 0:
559 		/* Version negotiation failed */
560 		break;
561 	case GLINK_VERSION_1:
562 		if (features == glink->features)
563 			break;
564 
565 		glink->features &= features;
566 		/* FALLTHROUGH */
567 	default:
568 		qcom_glink_send_version(glink);
569 		break;
570 	}
571 }
572 
573 /**
574  * qcom_glink_send_intent_req_ack() - convert an rx intent request ack cmd to
575  * 	wire format and transmit
576  * @glink:	The transport to transmit on.
577  * @channel:	The glink channel
578  * @granted:	The request response to encode.
579  *
580  * Return: 0 on success or standard Linux error code.
581  */
582 static int qcom_glink_send_intent_req_ack(struct qcom_glink *glink,
583 					  struct glink_channel *channel,
584 					  bool granted)
585 {
586 	struct glink_msg msg;
587 
588 	msg.cmd = cpu_to_le16(RPM_CMD_RX_INTENT_REQ_ACK);
589 	msg.param1 = cpu_to_le16(channel->lcid);
590 	msg.param2 = cpu_to_le32(granted);
591 
592 	qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
593 
594 	return 0;
595 }
596 
597 /**
598  * qcom_glink_advertise_intent - convert an rx intent cmd to wire format and
599  *			   transmit
600  * @glink:	The transport to transmit on.
601  * @channel:	The local channel
602  * @intent:	The intent to pass on to remote.
603  *
604  * Return: 0 on success or standard Linux error code.
605  */
606 static int qcom_glink_advertise_intent(struct qcom_glink *glink,
607 				       struct glink_channel *channel,
608 				       struct glink_core_rx_intent *intent)
609 {
610 	struct command {
611 		__le16 id;
612 		__le16 lcid;
613 		__le32 count;
614 		__le32 size;
615 		__le32 liid;
616 	} __packed;
617 	struct command cmd;
618 
619 	cmd.id = cpu_to_le16(RPM_CMD_INTENT);
620 	cmd.lcid = cpu_to_le16(channel->lcid);
621 	cmd.count = cpu_to_le32(1);
622 	cmd.size = cpu_to_le32(intent->size);
623 	cmd.liid = cpu_to_le32(intent->id);
624 
625 	qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
626 
627 	return 0;
628 }
629 
630 static struct glink_core_rx_intent *
631 qcom_glink_alloc_intent(struct qcom_glink *glink,
632 			struct glink_channel *channel,
633 			size_t size,
634 			bool reuseable)
635 {
636 	struct glink_core_rx_intent *intent;
637 	int ret;
638 	unsigned long flags;
639 
640 	intent = kzalloc(sizeof(*intent), GFP_KERNEL);
641 	if (!intent)
642 		return NULL;
643 
644 	intent->data = kzalloc(size, GFP_KERNEL);
645 	if (!intent->data)
646 		goto free_intent;
647 
648 	spin_lock_irqsave(&channel->intent_lock, flags);
649 	ret = idr_alloc_cyclic(&channel->liids, intent, 1, -1, GFP_ATOMIC);
650 	if (ret < 0) {
651 		spin_unlock_irqrestore(&channel->intent_lock, flags);
652 		goto free_data;
653 	}
654 	spin_unlock_irqrestore(&channel->intent_lock, flags);
655 
656 	intent->id = ret;
657 	intent->size = size;
658 	intent->reuse = reuseable;
659 
660 	return intent;
661 
662 free_data:
663 	kfree(intent->data);
664 free_intent:
665 	kfree(intent);
666 	return NULL;
667 }
668 
669 static void qcom_glink_handle_rx_done(struct qcom_glink *glink,
670 				      u32 cid, uint32_t iid,
671 				      bool reuse)
672 {
673 	struct glink_core_rx_intent *intent;
674 	struct glink_channel *channel;
675 	unsigned long flags;
676 
677 	spin_lock_irqsave(&glink->idr_lock, flags);
678 	channel = idr_find(&glink->rcids, cid);
679 	spin_unlock_irqrestore(&glink->idr_lock, flags);
680 	if (!channel) {
681 		dev_err(glink->dev, "invalid channel id received\n");
682 		return;
683 	}
684 
685 	spin_lock_irqsave(&channel->intent_lock, flags);
686 	intent = idr_find(&channel->riids, iid);
687 
688 	if (!intent) {
689 		spin_unlock_irqrestore(&channel->intent_lock, flags);
690 		dev_err(glink->dev, "invalid intent id received\n");
691 		return;
692 	}
693 
694 	intent->in_use = false;
695 
696 	if (!reuse) {
697 		idr_remove(&channel->riids, intent->id);
698 		kfree(intent);
699 	}
700 	spin_unlock_irqrestore(&channel->intent_lock, flags);
701 }
702 
703 /**
704  * qcom_glink_handle_intent_req() - Receive a request for rx_intent
705  *					    from remote side
706  * @glink:      Pointer to the transport interface
707  * @cid:	Remote channel ID
708  * @size:	size of the intent
709  *
710  * The function searches for the local channel to which the request for
711  * rx_intent has arrived and allocates and notifies the remote back
712  */
713 static void qcom_glink_handle_intent_req(struct qcom_glink *glink,
714 					 u32 cid, size_t size)
715 {
716 	struct glink_core_rx_intent *intent;
717 	struct glink_channel *channel;
718 	unsigned long flags;
719 
720 	spin_lock_irqsave(&glink->idr_lock, flags);
721 	channel = idr_find(&glink->rcids, cid);
722 	spin_unlock_irqrestore(&glink->idr_lock, flags);
723 
724 	if (!channel) {
725 		pr_err("%s channel not found for cid %d\n", __func__, cid);
726 		return;
727 	}
728 
729 	intent = qcom_glink_alloc_intent(glink, channel, size, false);
730 	if (intent)
731 		qcom_glink_advertise_intent(glink, channel, intent);
732 
733 	qcom_glink_send_intent_req_ack(glink, channel, !!intent);
734 }
735 
736 static int qcom_glink_rx_defer(struct qcom_glink *glink, size_t extra)
737 {
738 	struct glink_defer_cmd *dcmd;
739 
740 	extra = ALIGN(extra, 8);
741 
742 	if (qcom_glink_rx_avail(glink) < sizeof(struct glink_msg) + extra) {
743 		dev_dbg(glink->dev, "Insufficient data in rx fifo");
744 		return -ENXIO;
745 	}
746 
747 	dcmd = kzalloc(sizeof(*dcmd) + extra, GFP_ATOMIC);
748 	if (!dcmd)
749 		return -ENOMEM;
750 
751 	INIT_LIST_HEAD(&dcmd->node);
752 
753 	qcom_glink_rx_peak(glink, &dcmd->msg, 0, sizeof(dcmd->msg) + extra);
754 
755 	spin_lock(&glink->rx_lock);
756 	list_add_tail(&dcmd->node, &glink->rx_queue);
757 	spin_unlock(&glink->rx_lock);
758 
759 	schedule_work(&glink->rx_work);
760 	qcom_glink_rx_advance(glink, sizeof(dcmd->msg) + extra);
761 
762 	return 0;
763 }
764 
765 static int qcom_glink_rx_data(struct qcom_glink *glink, size_t avail)
766 {
767 	struct glink_core_rx_intent *intent;
768 	struct glink_channel *channel;
769 	struct {
770 		struct glink_msg msg;
771 		__le32 chunk_size;
772 		__le32 left_size;
773 	} __packed hdr;
774 	unsigned int chunk_size;
775 	unsigned int left_size;
776 	unsigned int rcid;
777 	unsigned int liid;
778 	int ret = 0;
779 	unsigned long flags;
780 
781 	if (avail < sizeof(hdr)) {
782 		dev_dbg(glink->dev, "Not enough data in fifo\n");
783 		return -EAGAIN;
784 	}
785 
786 	qcom_glink_rx_peak(glink, &hdr, 0, sizeof(hdr));
787 	chunk_size = le32_to_cpu(hdr.chunk_size);
788 	left_size = le32_to_cpu(hdr.left_size);
789 
790 	if (avail < sizeof(hdr) + chunk_size) {
791 		dev_dbg(glink->dev, "Payload not yet in fifo\n");
792 		return -EAGAIN;
793 	}
794 
795 	rcid = le16_to_cpu(hdr.msg.param1);
796 	spin_lock_irqsave(&glink->idr_lock, flags);
797 	channel = idr_find(&glink->rcids, rcid);
798 	spin_unlock_irqrestore(&glink->idr_lock, flags);
799 	if (!channel) {
800 		dev_dbg(glink->dev, "Data on non-existing channel\n");
801 
802 		/* Drop the message */
803 		goto advance_rx;
804 	}
805 
806 	if (glink->intentless) {
807 		/* Might have an ongoing, fragmented, message to append */
808 		if (!channel->buf) {
809 			intent = kzalloc(sizeof(*intent), GFP_ATOMIC);
810 			if (!intent)
811 				return -ENOMEM;
812 
813 			intent->data = kmalloc(chunk_size + left_size,
814 					       GFP_ATOMIC);
815 			if (!intent->data) {
816 				kfree(intent);
817 				return -ENOMEM;
818 			}
819 
820 			intent->id = 0xbabababa;
821 			intent->size = chunk_size + left_size;
822 			intent->offset = 0;
823 
824 			channel->buf = intent;
825 		} else {
826 			intent = channel->buf;
827 		}
828 	} else {
829 		liid = le32_to_cpu(hdr.msg.param2);
830 
831 		spin_lock_irqsave(&channel->intent_lock, flags);
832 		intent = idr_find(&channel->liids, liid);
833 		spin_unlock_irqrestore(&channel->intent_lock, flags);
834 
835 		if (!intent) {
836 			dev_err(glink->dev,
837 				"no intent found for channel %s intent %d",
838 				channel->name, liid);
839 			goto advance_rx;
840 		}
841 	}
842 
843 	if (intent->size - intent->offset < chunk_size) {
844 		dev_err(glink->dev, "Insufficient space in intent\n");
845 
846 		/* The packet header lied, drop payload */
847 		goto advance_rx;
848 	}
849 
850 	qcom_glink_rx_peak(glink, intent->data + intent->offset,
851 			   sizeof(hdr), chunk_size);
852 	intent->offset += chunk_size;
853 
854 	/* Handle message when no fragments remain to be received */
855 	if (!left_size) {
856 		spin_lock(&channel->recv_lock);
857 		if (channel->ept.cb) {
858 			channel->ept.cb(channel->ept.rpdev,
859 					intent->data,
860 					intent->offset,
861 					channel->ept.priv,
862 					RPMSG_ADDR_ANY);
863 		}
864 		spin_unlock(&channel->recv_lock);
865 
866 		intent->offset = 0;
867 		channel->buf = NULL;
868 
869 		qcom_glink_rx_done(glink, channel, intent);
870 	}
871 
872 advance_rx:
873 	qcom_glink_rx_advance(glink, ALIGN(sizeof(hdr) + chunk_size, 8));
874 
875 	return ret;
876 }
877 
878 static void qcom_glink_handle_intent(struct qcom_glink *glink,
879 				     unsigned int cid,
880 				     unsigned int count,
881 				     size_t avail)
882 {
883 	struct glink_core_rx_intent *intent;
884 	struct glink_channel *channel;
885 	struct intent_pair {
886 		__le32 size;
887 		__le32 iid;
888 	};
889 
890 	struct {
891 		struct glink_msg msg;
892 		struct intent_pair intents[];
893 	} __packed * msg;
894 
895 	const size_t msglen = sizeof(*msg) + sizeof(struct intent_pair) * count;
896 	int ret;
897 	int i;
898 	unsigned long flags;
899 
900 	if (avail < msglen) {
901 		dev_dbg(glink->dev, "Not enough data in fifo\n");
902 		return;
903 	}
904 
905 	spin_lock_irqsave(&glink->idr_lock, flags);
906 	channel = idr_find(&glink->rcids, cid);
907 	spin_unlock_irqrestore(&glink->idr_lock, flags);
908 	if (!channel) {
909 		dev_err(glink->dev, "intents for non-existing channel\n");
910 		return;
911 	}
912 
913 	msg = kmalloc(msglen, GFP_ATOMIC);
914 	if (!msg)
915 		return;
916 
917 	qcom_glink_rx_peak(glink, msg, 0, msglen);
918 
919 	for (i = 0; i < count; ++i) {
920 		intent = kzalloc(sizeof(*intent), GFP_ATOMIC);
921 		if (!intent)
922 			break;
923 
924 		intent->id = le32_to_cpu(msg->intents[i].iid);
925 		intent->size = le32_to_cpu(msg->intents[i].size);
926 
927 		spin_lock_irqsave(&channel->intent_lock, flags);
928 		ret = idr_alloc(&channel->riids, intent,
929 				intent->id, intent->id + 1, GFP_ATOMIC);
930 		spin_unlock_irqrestore(&channel->intent_lock, flags);
931 
932 		if (ret < 0)
933 			dev_err(glink->dev, "failed to store remote intent\n");
934 	}
935 
936 	kfree(msg);
937 	qcom_glink_rx_advance(glink, ALIGN(msglen, 8));
938 }
939 
940 static int qcom_glink_rx_open_ack(struct qcom_glink *glink, unsigned int lcid)
941 {
942 	struct glink_channel *channel;
943 
944 	spin_lock(&glink->idr_lock);
945 	channel = idr_find(&glink->lcids, lcid);
946 	spin_unlock(&glink->idr_lock);
947 	if (!channel) {
948 		dev_err(glink->dev, "Invalid open ack packet\n");
949 		return -EINVAL;
950 	}
951 
952 	complete(&channel->open_ack);
953 
954 	return 0;
955 }
956 
957 static irqreturn_t qcom_glink_native_intr(int irq, void *data)
958 {
959 	struct qcom_glink *glink = data;
960 	struct glink_msg msg;
961 	unsigned int param1;
962 	unsigned int param2;
963 	unsigned int avail;
964 	unsigned int cmd;
965 	int ret = 0;
966 
967 	for (;;) {
968 		avail = qcom_glink_rx_avail(glink);
969 		if (avail < sizeof(msg))
970 			break;
971 
972 		qcom_glink_rx_peak(glink, &msg, 0, sizeof(msg));
973 
974 		cmd = le16_to_cpu(msg.cmd);
975 		param1 = le16_to_cpu(msg.param1);
976 		param2 = le32_to_cpu(msg.param2);
977 
978 		switch (cmd) {
979 		case RPM_CMD_VERSION:
980 		case RPM_CMD_VERSION_ACK:
981 		case RPM_CMD_CLOSE:
982 		case RPM_CMD_CLOSE_ACK:
983 		case RPM_CMD_RX_INTENT_REQ:
984 			ret = qcom_glink_rx_defer(glink, 0);
985 			break;
986 		case RPM_CMD_OPEN_ACK:
987 			ret = qcom_glink_rx_open_ack(glink, param1);
988 			qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
989 			break;
990 		case RPM_CMD_OPEN:
991 			ret = qcom_glink_rx_defer(glink, param2);
992 			break;
993 		case RPM_CMD_TX_DATA:
994 		case RPM_CMD_TX_DATA_CONT:
995 			ret = qcom_glink_rx_data(glink, avail);
996 			break;
997 		case RPM_CMD_READ_NOTIF:
998 			qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
999 
1000 			mbox_send_message(glink->mbox_chan, NULL);
1001 			mbox_client_txdone(glink->mbox_chan, 0);
1002 			break;
1003 		case RPM_CMD_INTENT:
1004 			qcom_glink_handle_intent(glink, param1, param2, avail);
1005 			break;
1006 		case RPM_CMD_RX_DONE:
1007 			qcom_glink_handle_rx_done(glink, param1, param2, false);
1008 			qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1009 			break;
1010 		case RPM_CMD_RX_DONE_W_REUSE:
1011 			qcom_glink_handle_rx_done(glink, param1, param2, true);
1012 			qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1013 			break;
1014 		case RPM_CMD_RX_INTENT_REQ_ACK:
1015 			qcom_glink_handle_intent_req_ack(glink, param1, param2);
1016 			qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1017 			break;
1018 		default:
1019 			dev_err(glink->dev, "unhandled rx cmd: %d\n", cmd);
1020 			ret = -EINVAL;
1021 			break;
1022 		}
1023 
1024 		if (ret)
1025 			break;
1026 	}
1027 
1028 	return IRQ_HANDLED;
1029 }
1030 
1031 /* Locally initiated rpmsg_create_ept */
1032 static struct glink_channel *qcom_glink_create_local(struct qcom_glink *glink,
1033 						     const char *name)
1034 {
1035 	struct glink_channel *channel;
1036 	int ret;
1037 	unsigned long flags;
1038 
1039 	channel = qcom_glink_alloc_channel(glink, name);
1040 	if (IS_ERR(channel))
1041 		return ERR_CAST(channel);
1042 
1043 	ret = qcom_glink_send_open_req(glink, channel);
1044 	if (ret)
1045 		goto release_channel;
1046 
1047 	ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
1048 	if (!ret)
1049 		goto err_timeout;
1050 
1051 	ret = wait_for_completion_timeout(&channel->open_req, 5 * HZ);
1052 	if (!ret)
1053 		goto err_timeout;
1054 
1055 	qcom_glink_send_open_ack(glink, channel);
1056 
1057 	return channel;
1058 
1059 err_timeout:
1060 	/* qcom_glink_send_open_req() did register the channel in lcids*/
1061 	spin_lock_irqsave(&glink->idr_lock, flags);
1062 	idr_remove(&glink->lcids, channel->lcid);
1063 	spin_unlock_irqrestore(&glink->idr_lock, flags);
1064 
1065 release_channel:
1066 	/* Release qcom_glink_send_open_req() reference */
1067 	kref_put(&channel->refcount, qcom_glink_channel_release);
1068 	/* Release qcom_glink_alloc_channel() reference */
1069 	kref_put(&channel->refcount, qcom_glink_channel_release);
1070 
1071 	return ERR_PTR(-ETIMEDOUT);
1072 }
1073 
1074 /* Remote initiated rpmsg_create_ept */
1075 static int qcom_glink_create_remote(struct qcom_glink *glink,
1076 				    struct glink_channel *channel)
1077 {
1078 	int ret;
1079 
1080 	qcom_glink_send_open_ack(glink, channel);
1081 
1082 	ret = qcom_glink_send_open_req(glink, channel);
1083 	if (ret)
1084 		goto close_link;
1085 
1086 	ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
1087 	if (!ret) {
1088 		ret = -ETIMEDOUT;
1089 		goto close_link;
1090 	}
1091 
1092 	return 0;
1093 
1094 close_link:
1095 	/*
1096 	 * Send a close request to "undo" our open-ack. The close-ack will
1097 	 * release the last reference.
1098 	 */
1099 	qcom_glink_send_close_req(glink, channel);
1100 
1101 	/* Release qcom_glink_send_open_req() reference */
1102 	kref_put(&channel->refcount, qcom_glink_channel_release);
1103 
1104 	return ret;
1105 }
1106 
1107 static struct rpmsg_endpoint *qcom_glink_create_ept(struct rpmsg_device *rpdev,
1108 						    rpmsg_rx_cb_t cb,
1109 						    void *priv,
1110 						    struct rpmsg_channel_info
1111 									chinfo)
1112 {
1113 	struct glink_channel *parent = to_glink_channel(rpdev->ept);
1114 	struct glink_channel *channel;
1115 	struct qcom_glink *glink = parent->glink;
1116 	struct rpmsg_endpoint *ept;
1117 	const char *name = chinfo.name;
1118 	int cid;
1119 	int ret;
1120 	unsigned long flags;
1121 
1122 	spin_lock_irqsave(&glink->idr_lock, flags);
1123 	idr_for_each_entry(&glink->rcids, channel, cid) {
1124 		if (!strcmp(channel->name, name))
1125 			break;
1126 	}
1127 	spin_unlock_irqrestore(&glink->idr_lock, flags);
1128 
1129 	if (!channel) {
1130 		channel = qcom_glink_create_local(glink, name);
1131 		if (IS_ERR(channel))
1132 			return NULL;
1133 	} else {
1134 		ret = qcom_glink_create_remote(glink, channel);
1135 		if (ret)
1136 			return NULL;
1137 	}
1138 
1139 	ept = &channel->ept;
1140 	ept->rpdev = rpdev;
1141 	ept->cb = cb;
1142 	ept->priv = priv;
1143 	ept->ops = &glink_endpoint_ops;
1144 
1145 	return ept;
1146 }
1147 
1148 static int qcom_glink_announce_create(struct rpmsg_device *rpdev)
1149 {
1150 	struct glink_channel *channel = to_glink_channel(rpdev->ept);
1151 	struct device_node *np = rpdev->dev.of_node;
1152 	struct qcom_glink *glink = channel->glink;
1153 	struct glink_core_rx_intent *intent;
1154 	const struct property *prop = NULL;
1155 	__be32 defaults[] = { cpu_to_be32(SZ_1K), cpu_to_be32(5) };
1156 	int num_intents;
1157 	int num_groups = 1;
1158 	__be32 *val = defaults;
1159 	int size;
1160 
1161 	if (glink->intentless)
1162 		return 0;
1163 
1164 	prop = of_find_property(np, "qcom,intents", NULL);
1165 	if (prop) {
1166 		val = prop->value;
1167 		num_groups = prop->length / sizeof(u32) / 2;
1168 	}
1169 
1170 	/* Channel is now open, advertise base set of intents */
1171 	while (num_groups--) {
1172 		size = be32_to_cpup(val++);
1173 		num_intents = be32_to_cpup(val++);
1174 		while (num_intents--) {
1175 			intent = qcom_glink_alloc_intent(glink, channel, size,
1176 							 true);
1177 			if (!intent)
1178 				break;
1179 
1180 			qcom_glink_advertise_intent(glink, channel, intent);
1181 		}
1182 	}
1183 	return 0;
1184 }
1185 
1186 static void qcom_glink_destroy_ept(struct rpmsg_endpoint *ept)
1187 {
1188 	struct glink_channel *channel = to_glink_channel(ept);
1189 	struct qcom_glink *glink = channel->glink;
1190 	unsigned long flags;
1191 
1192 	spin_lock_irqsave(&channel->recv_lock, flags);
1193 	channel->ept.cb = NULL;
1194 	spin_unlock_irqrestore(&channel->recv_lock, flags);
1195 
1196 	/* Decouple the potential rpdev from the channel */
1197 	channel->rpdev = NULL;
1198 
1199 	qcom_glink_send_close_req(glink, channel);
1200 }
1201 
1202 static int qcom_glink_request_intent(struct qcom_glink *glink,
1203 				     struct glink_channel *channel,
1204 				     size_t size)
1205 {
1206 	struct {
1207 		u16 id;
1208 		u16 cid;
1209 		u32 size;
1210 	} __packed cmd;
1211 
1212 	int ret;
1213 
1214 	mutex_lock(&channel->intent_req_lock);
1215 
1216 	reinit_completion(&channel->intent_req_comp);
1217 
1218 	cmd.id = RPM_CMD_RX_INTENT_REQ;
1219 	cmd.cid = channel->lcid;
1220 	cmd.size = size;
1221 
1222 	ret = qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
1223 	if (ret)
1224 		goto unlock;
1225 
1226 	ret = wait_for_completion_timeout(&channel->intent_req_comp, 10 * HZ);
1227 	if (!ret) {
1228 		dev_err(glink->dev, "intent request timed out\n");
1229 		ret = -ETIMEDOUT;
1230 	} else {
1231 		ret = channel->intent_req_result ? 0 : -ECANCELED;
1232 	}
1233 
1234 unlock:
1235 	mutex_unlock(&channel->intent_req_lock);
1236 	return ret;
1237 }
1238 
1239 static int __qcom_glink_send(struct glink_channel *channel,
1240 			     void *data, int len, bool wait)
1241 {
1242 	struct qcom_glink *glink = channel->glink;
1243 	struct glink_core_rx_intent *intent = NULL;
1244 	struct glink_core_rx_intent *tmp;
1245 	int iid = 0;
1246 	struct {
1247 		struct glink_msg msg;
1248 		__le32 chunk_size;
1249 		__le32 left_size;
1250 	} __packed req;
1251 	int ret;
1252 	unsigned long flags;
1253 
1254 	if (!glink->intentless) {
1255 		while (!intent) {
1256 			spin_lock_irqsave(&channel->intent_lock, flags);
1257 			idr_for_each_entry(&channel->riids, tmp, iid) {
1258 				if (tmp->size >= len && !tmp->in_use) {
1259 					if (!intent)
1260 						intent = tmp;
1261 					else if (intent->size > tmp->size)
1262 						intent = tmp;
1263 					if (intent->size == len)
1264 						break;
1265 				}
1266 			}
1267 			if (intent)
1268 				intent->in_use = true;
1269 			spin_unlock_irqrestore(&channel->intent_lock, flags);
1270 
1271 			/* We found an available intent */
1272 			if (intent)
1273 				break;
1274 
1275 			if (!wait)
1276 				return -EBUSY;
1277 
1278 			ret = qcom_glink_request_intent(glink, channel, len);
1279 			if (ret < 0)
1280 				return ret;
1281 		}
1282 
1283 		iid = intent->id;
1284 	}
1285 
1286 	req.msg.cmd = cpu_to_le16(RPM_CMD_TX_DATA);
1287 	req.msg.param1 = cpu_to_le16(channel->lcid);
1288 	req.msg.param2 = cpu_to_le32(iid);
1289 	req.chunk_size = cpu_to_le32(len);
1290 	req.left_size = cpu_to_le32(0);
1291 
1292 	ret = qcom_glink_tx(glink, &req, sizeof(req), data, len, wait);
1293 
1294 	/* Mark intent available if we failed */
1295 	if (ret && intent)
1296 		intent->in_use = false;
1297 
1298 	return ret;
1299 }
1300 
1301 static int qcom_glink_send(struct rpmsg_endpoint *ept, void *data, int len)
1302 {
1303 	struct glink_channel *channel = to_glink_channel(ept);
1304 
1305 	return __qcom_glink_send(channel, data, len, true);
1306 }
1307 
1308 static int qcom_glink_trysend(struct rpmsg_endpoint *ept, void *data, int len)
1309 {
1310 	struct glink_channel *channel = to_glink_channel(ept);
1311 
1312 	return __qcom_glink_send(channel, data, len, false);
1313 }
1314 
1315 /*
1316  * Finds the device_node for the glink child interested in this channel.
1317  */
1318 static struct device_node *qcom_glink_match_channel(struct device_node *node,
1319 						    const char *channel)
1320 {
1321 	struct device_node *child;
1322 	const char *name;
1323 	const char *key;
1324 	int ret;
1325 
1326 	for_each_available_child_of_node(node, child) {
1327 		key = "qcom,glink-channels";
1328 		ret = of_property_read_string(child, key, &name);
1329 		if (ret)
1330 			continue;
1331 
1332 		if (strcmp(name, channel) == 0)
1333 			return child;
1334 	}
1335 
1336 	return NULL;
1337 }
1338 
1339 static const struct rpmsg_device_ops glink_device_ops = {
1340 	.create_ept = qcom_glink_create_ept,
1341 	.announce_create = qcom_glink_announce_create,
1342 };
1343 
1344 static const struct rpmsg_endpoint_ops glink_endpoint_ops = {
1345 	.destroy_ept = qcom_glink_destroy_ept,
1346 	.send = qcom_glink_send,
1347 	.trysend = qcom_glink_trysend,
1348 };
1349 
1350 static void qcom_glink_rpdev_release(struct device *dev)
1351 {
1352 	struct rpmsg_device *rpdev = to_rpmsg_device(dev);
1353 	struct glink_channel *channel = to_glink_channel(rpdev->ept);
1354 
1355 	channel->rpdev = NULL;
1356 	kfree(rpdev);
1357 }
1358 
1359 static int qcom_glink_rx_open(struct qcom_glink *glink, unsigned int rcid,
1360 			      char *name)
1361 {
1362 	struct glink_channel *channel;
1363 	struct rpmsg_device *rpdev;
1364 	bool create_device = false;
1365 	struct device_node *node;
1366 	int lcid;
1367 	int ret;
1368 	unsigned long flags;
1369 
1370 	spin_lock_irqsave(&glink->idr_lock, flags);
1371 	idr_for_each_entry(&glink->lcids, channel, lcid) {
1372 		if (!strcmp(channel->name, name))
1373 			break;
1374 	}
1375 	spin_unlock_irqrestore(&glink->idr_lock, flags);
1376 
1377 	if (!channel) {
1378 		channel = qcom_glink_alloc_channel(glink, name);
1379 		if (IS_ERR(channel))
1380 			return PTR_ERR(channel);
1381 
1382 		/* The opening dance was initiated by the remote */
1383 		create_device = true;
1384 	}
1385 
1386 	spin_lock_irqsave(&glink->idr_lock, flags);
1387 	ret = idr_alloc(&glink->rcids, channel, rcid, rcid + 1, GFP_ATOMIC);
1388 	if (ret < 0) {
1389 		dev_err(glink->dev, "Unable to insert channel into rcid list\n");
1390 		spin_unlock_irqrestore(&glink->idr_lock, flags);
1391 		goto free_channel;
1392 	}
1393 	channel->rcid = ret;
1394 	spin_unlock_irqrestore(&glink->idr_lock, flags);
1395 
1396 	complete(&channel->open_req);
1397 
1398 	if (create_device) {
1399 		rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL);
1400 		if (!rpdev) {
1401 			ret = -ENOMEM;
1402 			goto rcid_remove;
1403 		}
1404 
1405 		rpdev->ept = &channel->ept;
1406 		strncpy(rpdev->id.name, name, RPMSG_NAME_SIZE);
1407 		rpdev->src = RPMSG_ADDR_ANY;
1408 		rpdev->dst = RPMSG_ADDR_ANY;
1409 		rpdev->ops = &glink_device_ops;
1410 
1411 		node = qcom_glink_match_channel(glink->dev->of_node, name);
1412 		rpdev->dev.of_node = node;
1413 		rpdev->dev.parent = glink->dev;
1414 		rpdev->dev.release = qcom_glink_rpdev_release;
1415 
1416 		ret = rpmsg_register_device(rpdev);
1417 		if (ret)
1418 			goto free_rpdev;
1419 
1420 		channel->rpdev = rpdev;
1421 	}
1422 
1423 	return 0;
1424 
1425 free_rpdev:
1426 	kfree(rpdev);
1427 rcid_remove:
1428 	spin_lock_irqsave(&glink->idr_lock, flags);
1429 	idr_remove(&glink->rcids, channel->rcid);
1430 	channel->rcid = 0;
1431 	spin_unlock_irqrestore(&glink->idr_lock, flags);
1432 free_channel:
1433 	/* Release the reference, iff we took it */
1434 	if (create_device)
1435 		kref_put(&channel->refcount, qcom_glink_channel_release);
1436 
1437 	return ret;
1438 }
1439 
1440 static void qcom_glink_rx_close(struct qcom_glink *glink, unsigned int rcid)
1441 {
1442 	struct rpmsg_channel_info chinfo;
1443 	struct glink_channel *channel;
1444 	unsigned long flags;
1445 
1446 	spin_lock_irqsave(&glink->idr_lock, flags);
1447 	channel = idr_find(&glink->rcids, rcid);
1448 	spin_unlock_irqrestore(&glink->idr_lock, flags);
1449 	if (WARN(!channel, "close request on unknown channel\n"))
1450 		return;
1451 
1452 	/* cancel pending rx_done work */
1453 	cancel_work_sync(&channel->intent_work);
1454 
1455 	if (channel->rpdev) {
1456 		strncpy(chinfo.name, channel->name, sizeof(chinfo.name));
1457 		chinfo.src = RPMSG_ADDR_ANY;
1458 		chinfo.dst = RPMSG_ADDR_ANY;
1459 
1460 		rpmsg_unregister_device(glink->dev, &chinfo);
1461 	}
1462 
1463 	qcom_glink_send_close_ack(glink, channel->rcid);
1464 
1465 	spin_lock_irqsave(&glink->idr_lock, flags);
1466 	idr_remove(&glink->rcids, channel->rcid);
1467 	channel->rcid = 0;
1468 	spin_unlock_irqrestore(&glink->idr_lock, flags);
1469 
1470 	kref_put(&channel->refcount, qcom_glink_channel_release);
1471 }
1472 
1473 static void qcom_glink_rx_close_ack(struct qcom_glink *glink, unsigned int lcid)
1474 {
1475 	struct glink_channel *channel;
1476 	unsigned long flags;
1477 
1478 	spin_lock_irqsave(&glink->idr_lock, flags);
1479 	channel = idr_find(&glink->lcids, lcid);
1480 	if (WARN(!channel, "close ack on unknown channel\n")) {
1481 		spin_unlock_irqrestore(&glink->idr_lock, flags);
1482 		return;
1483 	}
1484 
1485 	idr_remove(&glink->lcids, channel->lcid);
1486 	channel->lcid = 0;
1487 	spin_unlock_irqrestore(&glink->idr_lock, flags);
1488 
1489 	kref_put(&channel->refcount, qcom_glink_channel_release);
1490 }
1491 
1492 static void qcom_glink_work(struct work_struct *work)
1493 {
1494 	struct qcom_glink *glink = container_of(work, struct qcom_glink,
1495 						rx_work);
1496 	struct glink_defer_cmd *dcmd;
1497 	struct glink_msg *msg;
1498 	unsigned long flags;
1499 	unsigned int param1;
1500 	unsigned int param2;
1501 	unsigned int cmd;
1502 
1503 	for (;;) {
1504 		spin_lock_irqsave(&glink->rx_lock, flags);
1505 		if (list_empty(&glink->rx_queue)) {
1506 			spin_unlock_irqrestore(&glink->rx_lock, flags);
1507 			break;
1508 		}
1509 		dcmd = list_first_entry(&glink->rx_queue,
1510 					struct glink_defer_cmd, node);
1511 		list_del(&dcmd->node);
1512 		spin_unlock_irqrestore(&glink->rx_lock, flags);
1513 
1514 		msg = &dcmd->msg;
1515 		cmd = le16_to_cpu(msg->cmd);
1516 		param1 = le16_to_cpu(msg->param1);
1517 		param2 = le32_to_cpu(msg->param2);
1518 
1519 		switch (cmd) {
1520 		case RPM_CMD_VERSION:
1521 			qcom_glink_receive_version(glink, param1, param2);
1522 			break;
1523 		case RPM_CMD_VERSION_ACK:
1524 			qcom_glink_receive_version_ack(glink, param1, param2);
1525 			break;
1526 		case RPM_CMD_OPEN:
1527 			qcom_glink_rx_open(glink, param1, msg->data);
1528 			break;
1529 		case RPM_CMD_CLOSE:
1530 			qcom_glink_rx_close(glink, param1);
1531 			break;
1532 		case RPM_CMD_CLOSE_ACK:
1533 			qcom_glink_rx_close_ack(glink, param1);
1534 			break;
1535 		case RPM_CMD_RX_INTENT_REQ:
1536 			qcom_glink_handle_intent_req(glink, param1, param2);
1537 			break;
1538 		default:
1539 			WARN(1, "Unknown defer object %d\n", cmd);
1540 			break;
1541 		}
1542 
1543 		kfree(dcmd);
1544 	}
1545 }
1546 
1547 struct qcom_glink *qcom_glink_native_probe(struct device *dev,
1548 					   unsigned long features,
1549 					   struct qcom_glink_pipe *rx,
1550 					   struct qcom_glink_pipe *tx,
1551 					   bool intentless)
1552 {
1553 	int irq;
1554 	int ret;
1555 	struct qcom_glink *glink;
1556 
1557 	glink = devm_kzalloc(dev, sizeof(*glink), GFP_KERNEL);
1558 	if (!glink)
1559 		return ERR_PTR(-ENOMEM);
1560 
1561 	glink->dev = dev;
1562 	glink->tx_pipe = tx;
1563 	glink->rx_pipe = rx;
1564 
1565 	glink->features = features;
1566 	glink->intentless = intentless;
1567 
1568 	spin_lock_init(&glink->tx_lock);
1569 	spin_lock_init(&glink->rx_lock);
1570 	INIT_LIST_HEAD(&glink->rx_queue);
1571 	INIT_WORK(&glink->rx_work, qcom_glink_work);
1572 
1573 	spin_lock_init(&glink->idr_lock);
1574 	idr_init(&glink->lcids);
1575 	idr_init(&glink->rcids);
1576 
1577 	ret = of_property_read_string(dev->of_node, "label", &glink->name);
1578 	if (ret < 0)
1579 		glink->name = dev->of_node->name;
1580 
1581 	glink->mbox_client.dev = dev;
1582 	glink->mbox_client.knows_txdone = true;
1583 	glink->mbox_chan = mbox_request_channel(&glink->mbox_client, 0);
1584 	if (IS_ERR(glink->mbox_chan)) {
1585 		if (PTR_ERR(glink->mbox_chan) != -EPROBE_DEFER)
1586 			dev_err(dev, "failed to acquire IPC channel\n");
1587 		return ERR_CAST(glink->mbox_chan);
1588 	}
1589 
1590 	irq = of_irq_get(dev->of_node, 0);
1591 	ret = devm_request_irq(dev, irq,
1592 			       qcom_glink_native_intr,
1593 			       IRQF_NO_SUSPEND | IRQF_SHARED,
1594 			       "glink-native", glink);
1595 	if (ret) {
1596 		dev_err(dev, "failed to request IRQ\n");
1597 		return ERR_PTR(ret);
1598 	}
1599 
1600 	glink->irq = irq;
1601 
1602 	ret = qcom_glink_send_version(glink);
1603 	if (ret)
1604 		return ERR_PTR(ret);
1605 
1606 	return glink;
1607 }
1608 EXPORT_SYMBOL_GPL(qcom_glink_native_probe);
1609 
1610 static int qcom_glink_remove_device(struct device *dev, void *data)
1611 {
1612 	device_unregister(dev);
1613 
1614 	return 0;
1615 }
1616 
1617 void qcom_glink_native_remove(struct qcom_glink *glink)
1618 {
1619 	struct glink_channel *channel;
1620 	int cid;
1621 	int ret;
1622 	unsigned long flags;
1623 
1624 	disable_irq(glink->irq);
1625 	cancel_work_sync(&glink->rx_work);
1626 
1627 	ret = device_for_each_child(glink->dev, NULL, qcom_glink_remove_device);
1628 	if (ret)
1629 		dev_warn(glink->dev, "Can't remove GLINK devices: %d\n", ret);
1630 
1631 	spin_lock_irqsave(&glink->idr_lock, flags);
1632 	/* Release any defunct local channels, waiting for close-ack */
1633 	idr_for_each_entry(&glink->lcids, channel, cid)
1634 		kref_put(&channel->refcount, qcom_glink_channel_release);
1635 
1636 	idr_destroy(&glink->lcids);
1637 	idr_destroy(&glink->rcids);
1638 	spin_unlock_irqrestore(&glink->idr_lock, flags);
1639 	mbox_free_channel(glink->mbox_chan);
1640 }
1641 EXPORT_SYMBOL_GPL(qcom_glink_native_remove);
1642 
1643 void qcom_glink_native_unregister(struct qcom_glink *glink)
1644 {
1645 	device_unregister(glink->dev);
1646 }
1647 EXPORT_SYMBOL_GPL(qcom_glink_native_unregister);
1648 
1649 MODULE_DESCRIPTION("Qualcomm GLINK driver");
1650 MODULE_LICENSE("GPL v2");
1651