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