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