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