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