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