]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - drivers/rpmsg/qcom_glink_native.c
rpmsg: glink: Make RX FIFO peak accessor to take an offset
[mirror_ubuntu-jammy-kernel.git] / drivers / rpmsg / qcom_glink_native.c
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/slab.h>
27 #include <linux/workqueue.h>
28 #include <linux/mailbox_client.h>
29
30 #include "rpmsg_internal.h"
31 #include "qcom_glink_native.h"
32
33 #define GLINK_NAME_SIZE 32
34 #define GLINK_VERSION_1 1
35
36 #define RPM_GLINK_CID_MIN 1
37 #define RPM_GLINK_CID_MAX 65536
38
39 struct glink_msg {
40 __le16 cmd;
41 __le16 param1;
42 __le32 param2;
43 u8 data[];
44 } __packed;
45
46 /**
47 * struct glink_defer_cmd - deferred incoming control message
48 * @node: list node
49 * @msg: message header
50 * data: payload of the message
51 *
52 * Copy of a received control message, to be added to @rx_queue and processed
53 * by @rx_work of @qcom_glink.
54 */
55 struct glink_defer_cmd {
56 struct list_head node;
57
58 struct glink_msg msg;
59 u8 data[];
60 };
61
62 /**
63 * struct glink_core_rx_intent - RX intent
64 * RX intent
65 *
66 * data: pointer to the data (may be NULL for zero-copy)
67 * id: remote or local intent ID
68 * size: size of the original intent (do not modify)
69 * reuse: To mark if the intent can be reused after first use
70 * in_use: To mark if intent is already in use for the channel
71 * offset: next write offset (initially 0)
72 */
73 struct glink_core_rx_intent {
74 void *data;
75 u32 id;
76 size_t size;
77 bool reuse;
78 bool in_use;
79 u32 offset;
80 };
81
82 /**
83 * struct qcom_glink - driver context, relates to one remote subsystem
84 * @dev: reference to the associated struct device
85 * @mbox_client: mailbox client
86 * @mbox_chan: mailbox channel
87 * @rx_pipe: pipe object for receive FIFO
88 * @tx_pipe: pipe object for transmit FIFO
89 * @irq: IRQ for signaling incoming events
90 * @rx_work: worker for handling received control messages
91 * @rx_lock: protects the @rx_queue
92 * @rx_queue: queue of received control messages to be processed in @rx_work
93 * @tx_lock: synchronizes operations on the tx fifo
94 * @idr_lock: synchronizes @lcids and @rcids modifications
95 * @lcids: idr of all channels with a known local channel id
96 * @rcids: idr of all channels with a known remote channel id
97 */
98 struct qcom_glink {
99 struct device *dev;
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 struct mutex 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
141 * @liids: idr of all local intents
142 * @buf: receive buffer, for gathering fragments
143 * @buf_offset: write offset in @buf
144 * @buf_size: size of current @buf
145 * @open_ack: completed once remote has acked the open-request
146 * @open_req: completed once open-request has been received
147 */
148 struct glink_channel {
149 struct rpmsg_endpoint ept;
150
151 struct rpmsg_device *rpdev;
152 struct qcom_glink *glink;
153
154 struct kref refcount;
155
156 spinlock_t recv_lock;
157
158 char *name;
159 unsigned int lcid;
160 unsigned int rcid;
161
162 spinlock_t intent_lock;
163 struct idr liids;
164
165 struct glink_core_rx_intent *buf;
166 int buf_offset;
167 int buf_size;
168
169 struct completion open_ack;
170 struct completion open_req;
171 };
172
173 #define to_glink_channel(_ept) container_of(_ept, struct glink_channel, ept)
174
175 static const struct rpmsg_endpoint_ops glink_endpoint_ops;
176
177 #define RPM_CMD_VERSION 0
178 #define RPM_CMD_VERSION_ACK 1
179 #define RPM_CMD_OPEN 2
180 #define RPM_CMD_CLOSE 3
181 #define RPM_CMD_OPEN_ACK 4
182 #define RPM_CMD_INTENT 5
183 #define RPM_CMD_RX_INTENT_REQ 7
184 #define RPM_CMD_RX_INTENT_REQ_ACK 8
185 #define RPM_CMD_TX_DATA 9
186 #define RPM_CMD_CLOSE_ACK 11
187 #define RPM_CMD_TX_DATA_CONT 12
188 #define RPM_CMD_READ_NOTIF 13
189
190 #define GLINK_FEATURE_INTENTLESS BIT(1)
191
192 static struct glink_channel *qcom_glink_alloc_channel(struct qcom_glink *glink,
193 const char *name)
194 {
195 struct glink_channel *channel;
196
197 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
198 if (!channel)
199 return ERR_PTR(-ENOMEM);
200
201 /* Setup glink internal glink_channel data */
202 spin_lock_init(&channel->recv_lock);
203 spin_lock_init(&channel->intent_lock);
204 channel->glink = glink;
205 channel->name = kstrdup(name, GFP_KERNEL);
206
207 init_completion(&channel->open_req);
208 init_completion(&channel->open_ack);
209
210 idr_init(&channel->liids);
211 kref_init(&channel->refcount);
212
213 return channel;
214 }
215
216 static void qcom_glink_channel_release(struct kref *ref)
217 {
218 struct glink_channel *channel = container_of(ref, struct glink_channel,
219 refcount);
220 unsigned long flags;
221
222 spin_lock_irqsave(&channel->intent_lock, flags);
223 idr_destroy(&channel->liids);
224 spin_unlock_irqrestore(&channel->intent_lock, flags);
225
226 kfree(channel->name);
227 kfree(channel);
228 }
229
230 static size_t qcom_glink_rx_avail(struct qcom_glink *glink)
231 {
232 return glink->rx_pipe->avail(glink->rx_pipe);
233 }
234
235 static void qcom_glink_rx_peak(struct qcom_glink *glink,
236 void *data, unsigned int offset, size_t count)
237 {
238 glink->rx_pipe->peak(glink->rx_pipe, data, offset, count);
239 }
240
241 static void qcom_glink_rx_advance(struct qcom_glink *glink, size_t count)
242 {
243 glink->rx_pipe->advance(glink->rx_pipe, count);
244 }
245
246 static size_t qcom_glink_tx_avail(struct qcom_glink *glink)
247 {
248 return glink->tx_pipe->avail(glink->tx_pipe);
249 }
250
251 static void qcom_glink_tx_write(struct qcom_glink *glink,
252 const void *hdr, size_t hlen,
253 const void *data, size_t dlen)
254 {
255 glink->tx_pipe->write(glink->tx_pipe, hdr, hlen, data, dlen);
256 }
257
258 static int qcom_glink_tx(struct qcom_glink *glink,
259 const void *hdr, size_t hlen,
260 const void *data, size_t dlen, bool wait)
261 {
262 unsigned int tlen = hlen + dlen;
263 int ret;
264
265 /* Reject packets that are too big */
266 if (tlen >= glink->tx_pipe->length)
267 return -EINVAL;
268
269 ret = mutex_lock_interruptible(&glink->tx_lock);
270 if (ret)
271 return ret;
272
273 while (qcom_glink_tx_avail(glink) < tlen) {
274 if (!wait) {
275 ret = -EAGAIN;
276 goto out;
277 }
278
279 usleep_range(10000, 15000);
280 }
281
282 qcom_glink_tx_write(glink, hdr, hlen, data, dlen);
283
284 mbox_send_message(glink->mbox_chan, NULL);
285 mbox_client_txdone(glink->mbox_chan, 0);
286
287 out:
288 mutex_unlock(&glink->tx_lock);
289
290 return ret;
291 }
292
293 static int qcom_glink_send_version(struct qcom_glink *glink)
294 {
295 struct glink_msg msg;
296
297 msg.cmd = cpu_to_le16(RPM_CMD_VERSION);
298 msg.param1 = cpu_to_le16(GLINK_VERSION_1);
299 msg.param2 = cpu_to_le32(glink->features);
300
301 return qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
302 }
303
304 static void qcom_glink_send_version_ack(struct qcom_glink *glink)
305 {
306 struct glink_msg msg;
307
308 msg.cmd = cpu_to_le16(RPM_CMD_VERSION_ACK);
309 msg.param1 = cpu_to_le16(GLINK_VERSION_1);
310 msg.param2 = cpu_to_le32(glink->features);
311
312 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
313 }
314
315 static void qcom_glink_send_open_ack(struct qcom_glink *glink,
316 struct glink_channel *channel)
317 {
318 struct glink_msg msg;
319
320 msg.cmd = cpu_to_le16(RPM_CMD_OPEN_ACK);
321 msg.param1 = cpu_to_le16(channel->rcid);
322 msg.param2 = cpu_to_le32(0);
323
324 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
325 }
326
327 /**
328 * qcom_glink_send_open_req() - send a RPM_CMD_OPEN request to the remote
329 * @glink: Ptr to the glink edge
330 * @channel: Ptr to the channel that the open req is sent
331 *
332 * Allocates a local channel id and sends a RPM_CMD_OPEN message to the remote.
333 * Will return with refcount held, regardless of outcome.
334 *
335 * Returns 0 on success, negative errno otherwise.
336 */
337 static int qcom_glink_send_open_req(struct qcom_glink *glink,
338 struct glink_channel *channel)
339 {
340 struct {
341 struct glink_msg msg;
342 u8 name[GLINK_NAME_SIZE];
343 } __packed req;
344 int name_len = strlen(channel->name) + 1;
345 int req_len = ALIGN(sizeof(req.msg) + name_len, 8);
346 int ret;
347 unsigned long flags;
348
349 kref_get(&channel->refcount);
350
351 spin_lock_irqsave(&glink->idr_lock, flags);
352 ret = idr_alloc_cyclic(&glink->lcids, channel,
353 RPM_GLINK_CID_MIN, RPM_GLINK_CID_MAX,
354 GFP_ATOMIC);
355 spin_unlock_irqrestore(&glink->idr_lock, flags);
356 if (ret < 0)
357 return ret;
358
359 channel->lcid = ret;
360
361 req.msg.cmd = cpu_to_le16(RPM_CMD_OPEN);
362 req.msg.param1 = cpu_to_le16(channel->lcid);
363 req.msg.param2 = cpu_to_le32(name_len);
364 strcpy(req.name, channel->name);
365
366 ret = qcom_glink_tx(glink, &req, req_len, NULL, 0, true);
367 if (ret)
368 goto remove_idr;
369
370 return 0;
371
372 remove_idr:
373 spin_lock_irqsave(&glink->idr_lock, flags);
374 idr_remove(&glink->lcids, channel->lcid);
375 channel->lcid = 0;
376 spin_unlock_irqrestore(&glink->idr_lock, flags);
377
378 return ret;
379 }
380
381 static void qcom_glink_send_close_req(struct qcom_glink *glink,
382 struct glink_channel *channel)
383 {
384 struct glink_msg req;
385
386 req.cmd = cpu_to_le16(RPM_CMD_CLOSE);
387 req.param1 = cpu_to_le16(channel->lcid);
388 req.param2 = 0;
389
390 qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
391 }
392
393 static void qcom_glink_send_close_ack(struct qcom_glink *glink,
394 unsigned int rcid)
395 {
396 struct glink_msg req;
397
398 req.cmd = cpu_to_le16(RPM_CMD_CLOSE_ACK);
399 req.param1 = cpu_to_le16(rcid);
400 req.param2 = 0;
401
402 qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
403 }
404
405 /**
406 * qcom_glink_receive_version() - receive version/features from remote system
407 *
408 * @glink: pointer to transport interface
409 * @r_version: remote version
410 * @r_features: remote features
411 *
412 * This function is called in response to a remote-initiated version/feature
413 * negotiation sequence.
414 */
415 static void qcom_glink_receive_version(struct qcom_glink *glink,
416 u32 version,
417 u32 features)
418 {
419 switch (version) {
420 case 0:
421 break;
422 case GLINK_VERSION_1:
423 glink->features &= features;
424 /* FALLTHROUGH */
425 default:
426 qcom_glink_send_version_ack(glink);
427 break;
428 }
429 }
430
431 /**
432 * qcom_glink_receive_version_ack() - receive negotiation ack from remote system
433 *
434 * @glink: pointer to transport interface
435 * @r_version: remote version response
436 * @r_features: remote features response
437 *
438 * This function is called in response to a local-initiated version/feature
439 * negotiation sequence and is the counter-offer from the remote side based
440 * upon the initial version and feature set requested.
441 */
442 static void qcom_glink_receive_version_ack(struct qcom_glink *glink,
443 u32 version,
444 u32 features)
445 {
446 switch (version) {
447 case 0:
448 /* Version negotiation failed */
449 break;
450 case GLINK_VERSION_1:
451 if (features == glink->features)
452 break;
453
454 glink->features &= features;
455 /* FALLTHROUGH */
456 default:
457 qcom_glink_send_version(glink);
458 break;
459 }
460 }
461
462 /**
463 * qcom_glink_send_intent_req_ack() - convert an rx intent request ack cmd to
464 wire format and transmit
465 * @glink: The transport to transmit on.
466 * @channel: The glink channel
467 * @granted: The request response to encode.
468 *
469 * Return: 0 on success or standard Linux error code.
470 */
471 static int qcom_glink_send_intent_req_ack(struct qcom_glink *glink,
472 struct glink_channel *channel,
473 bool granted)
474 {
475 struct glink_msg msg;
476
477 msg.cmd = cpu_to_le16(RPM_CMD_RX_INTENT_REQ_ACK);
478 msg.param1 = cpu_to_le16(channel->lcid);
479 msg.param2 = cpu_to_le32(granted);
480
481 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
482
483 return 0;
484 }
485
486 /**
487 * qcom_glink_advertise_intent - convert an rx intent cmd to wire format and
488 * transmit
489 * @glink: The transport to transmit on.
490 * @channel: The local channel
491 * @size: The intent to pass on to remote.
492 *
493 * Return: 0 on success or standard Linux error code.
494 */
495 static int qcom_glink_advertise_intent(struct qcom_glink *glink,
496 struct glink_channel *channel,
497 struct glink_core_rx_intent *intent)
498 {
499 struct command {
500 u16 id;
501 u16 lcid;
502 u32 count;
503 u32 size;
504 u32 liid;
505 } __packed;
506 struct command cmd;
507
508 cmd.id = cpu_to_le16(RPM_CMD_INTENT);
509 cmd.lcid = cpu_to_le16(channel->lcid);
510 cmd.count = cpu_to_le32(1);
511 cmd.size = cpu_to_le32(intent->size);
512 cmd.liid = cpu_to_le32(intent->id);
513
514 qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
515
516 return 0;
517 }
518
519 static struct glink_core_rx_intent *
520 qcom_glink_alloc_intent(struct qcom_glink *glink,
521 struct glink_channel *channel,
522 size_t size,
523 bool reuseable)
524 {
525 struct glink_core_rx_intent *intent;
526 int ret;
527 unsigned long flags;
528
529 intent = kzalloc(sizeof(*intent), GFP_KERNEL);
530
531 if (!intent)
532 return NULL;
533
534 intent->data = kzalloc(size, GFP_KERNEL);
535 if (!intent->data)
536 return NULL;
537
538 spin_lock_irqsave(&channel->intent_lock, flags);
539 ret = idr_alloc_cyclic(&channel->liids, intent, 1, -1, GFP_ATOMIC);
540 if (ret < 0) {
541 spin_unlock_irqrestore(&channel->intent_lock, flags);
542 return NULL;
543 }
544 spin_unlock_irqrestore(&channel->intent_lock, flags);
545
546 intent->id = ret;
547 intent->size = size;
548 intent->reuse = reuseable;
549
550 return intent;
551 }
552
553 /**
554 * qcom_glink_handle_intent_req() - Receive a request for rx_intent
555 * from remote side
556 * if_ptr: Pointer to the transport interface
557 * rcid: Remote channel ID
558 * size: size of the intent
559 *
560 * The function searches for the local channel to which the request for
561 * rx_intent has arrived and allocates and notifies the remote back
562 */
563 static void qcom_glink_handle_intent_req(struct qcom_glink *glink,
564 u32 cid, size_t size)
565 {
566 struct glink_core_rx_intent *intent;
567 struct glink_channel *channel;
568 unsigned long flags;
569
570 spin_lock_irqsave(&glink->idr_lock, flags);
571 channel = idr_find(&glink->rcids, cid);
572 spin_unlock_irqrestore(&glink->idr_lock, flags);
573
574 if (!channel) {
575 pr_err("%s channel not found for cid %d\n", __func__, cid);
576 return;
577 }
578
579 intent = qcom_glink_alloc_intent(glink, channel, size, false);
580 if (intent)
581 qcom_glink_advertise_intent(glink, channel, intent);
582
583 qcom_glink_send_intent_req_ack(glink, channel, !!intent);
584 }
585
586 static int qcom_glink_rx_defer(struct qcom_glink *glink, size_t extra)
587 {
588 struct glink_defer_cmd *dcmd;
589
590 extra = ALIGN(extra, 8);
591
592 if (qcom_glink_rx_avail(glink) < sizeof(struct glink_msg) + extra) {
593 dev_dbg(glink->dev, "Insufficient data in rx fifo");
594 return -ENXIO;
595 }
596
597 dcmd = kzalloc(sizeof(*dcmd) + extra, GFP_ATOMIC);
598 if (!dcmd)
599 return -ENOMEM;
600
601 INIT_LIST_HEAD(&dcmd->node);
602
603 qcom_glink_rx_peak(glink, &dcmd->msg, 0, sizeof(dcmd->msg) + extra);
604
605 spin_lock(&glink->rx_lock);
606 list_add_tail(&dcmd->node, &glink->rx_queue);
607 spin_unlock(&glink->rx_lock);
608
609 schedule_work(&glink->rx_work);
610 qcom_glink_rx_advance(glink, sizeof(dcmd->msg) + extra);
611
612 return 0;
613 }
614
615 static int qcom_glink_rx_data(struct qcom_glink *glink, size_t avail)
616 {
617 struct glink_core_rx_intent *intent;
618 struct glink_channel *channel;
619 struct {
620 struct glink_msg msg;
621 __le32 chunk_size;
622 __le32 left_size;
623 } __packed hdr;
624 unsigned int chunk_size;
625 unsigned int left_size;
626 unsigned int rcid;
627 unsigned int liid;
628 int ret = 0;
629 unsigned long flags;
630
631 if (avail < sizeof(hdr)) {
632 dev_dbg(glink->dev, "Not enough data in fifo\n");
633 return -EAGAIN;
634 }
635
636 qcom_glink_rx_peak(glink, &hdr, 0, sizeof(hdr));
637 chunk_size = le32_to_cpu(hdr.chunk_size);
638 left_size = le32_to_cpu(hdr.left_size);
639
640 if (avail < sizeof(hdr) + chunk_size) {
641 dev_dbg(glink->dev, "Payload not yet in fifo\n");
642 return -EAGAIN;
643 }
644
645 if (WARN(chunk_size % 4, "Incoming data must be word aligned\n"))
646 return -EINVAL;
647
648 rcid = le16_to_cpu(hdr.msg.param1);
649 spin_lock_irqsave(&glink->idr_lock, flags);
650 channel = idr_find(&glink->rcids, rcid);
651 spin_unlock_irqrestore(&glink->idr_lock, flags);
652 if (!channel) {
653 dev_dbg(glink->dev, "Data on non-existing channel\n");
654
655 /* Drop the message */
656 goto advance_rx;
657 }
658
659 if (glink->intentless) {
660 /* Might have an ongoing, fragmented, message to append */
661 if (!channel->buf) {
662 intent = kzalloc(sizeof(*intent), GFP_ATOMIC);
663 if (!intent)
664 return -ENOMEM;
665
666 intent->data = kmalloc(chunk_size + left_size,
667 GFP_ATOMIC);
668 if (!intent->data) {
669 kfree(intent);
670 return -ENOMEM;
671 }
672
673 intent->id = 0xbabababa;
674 intent->size = chunk_size + left_size;
675 intent->offset = 0;
676
677 channel->buf = intent;
678 } else {
679 intent = channel->buf;
680 }
681 } else {
682 liid = le32_to_cpu(hdr.msg.param2);
683
684 spin_lock_irqsave(&channel->intent_lock, flags);
685 intent = idr_find(&channel->liids, liid);
686 spin_unlock_irqrestore(&channel->intent_lock, flags);
687
688 if (!intent) {
689 dev_err(glink->dev,
690 "no intent found for channel %s intent %d",
691 channel->name, liid);
692 goto advance_rx;
693 }
694 }
695
696 if (intent->size - intent->offset < chunk_size) {
697 dev_err(glink->dev, "Insufficient space in intent\n");
698
699 /* The packet header lied, drop payload */
700 goto advance_rx;
701 }
702
703 qcom_glink_rx_peak(glink, intent->data + intent->offset,
704 sizeof(hdr), chunk_size);
705 intent->offset += chunk_size;
706
707 /* Handle message when no fragments remain to be received */
708 if (!left_size) {
709 spin_lock(&channel->recv_lock);
710 if (channel->ept.cb) {
711 channel->ept.cb(channel->ept.rpdev,
712 intent->data,
713 intent->offset,
714 channel->ept.priv,
715 RPMSG_ADDR_ANY);
716 }
717 spin_unlock(&channel->recv_lock);
718
719 intent->offset = 0;
720 channel->buf = NULL;
721 }
722
723 advance_rx:
724 qcom_glink_rx_advance(glink, ALIGN(sizeof(hdr) + chunk_size, 8));
725
726 return ret;
727 }
728
729 static int qcom_glink_rx_open_ack(struct qcom_glink *glink, unsigned int lcid)
730 {
731 struct glink_channel *channel;
732
733 spin_lock(&glink->idr_lock);
734 channel = idr_find(&glink->lcids, lcid);
735 if (!channel) {
736 dev_err(glink->dev, "Invalid open ack packet\n");
737 return -EINVAL;
738 }
739 spin_unlock(&glink->idr_lock);
740
741 complete(&channel->open_ack);
742
743 return 0;
744 }
745
746 static irqreturn_t qcom_glink_native_intr(int irq, void *data)
747 {
748 struct qcom_glink *glink = data;
749 struct glink_msg msg;
750 unsigned int param1;
751 unsigned int param2;
752 unsigned int avail;
753 unsigned int cmd;
754 int ret;
755
756 for (;;) {
757 avail = qcom_glink_rx_avail(glink);
758 if (avail < sizeof(msg))
759 break;
760
761 qcom_glink_rx_peak(glink, &msg, 0, sizeof(msg));
762
763 cmd = le16_to_cpu(msg.cmd);
764 param1 = le16_to_cpu(msg.param1);
765 param2 = le32_to_cpu(msg.param2);
766
767 switch (cmd) {
768 case RPM_CMD_VERSION:
769 case RPM_CMD_VERSION_ACK:
770 case RPM_CMD_CLOSE:
771 case RPM_CMD_CLOSE_ACK:
772 case RPM_CMD_RX_INTENT_REQ:
773 ret = qcom_glink_rx_defer(glink, 0);
774 break;
775 case RPM_CMD_OPEN_ACK:
776 ret = qcom_glink_rx_open_ack(glink, param1);
777 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
778 break;
779 case RPM_CMD_OPEN:
780 ret = qcom_glink_rx_defer(glink, param2);
781 break;
782 case RPM_CMD_TX_DATA:
783 case RPM_CMD_TX_DATA_CONT:
784 ret = qcom_glink_rx_data(glink, avail);
785 break;
786 case RPM_CMD_READ_NOTIF:
787 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
788
789 mbox_send_message(glink->mbox_chan, NULL);
790 mbox_client_txdone(glink->mbox_chan, 0);
791
792 ret = 0;
793 break;
794 default:
795 dev_err(glink->dev, "unhandled rx cmd: %d\n", cmd);
796 ret = -EINVAL;
797 break;
798 }
799
800 if (ret)
801 break;
802 }
803
804 return IRQ_HANDLED;
805 }
806
807 /* Locally initiated rpmsg_create_ept */
808 static struct glink_channel *qcom_glink_create_local(struct qcom_glink *glink,
809 const char *name)
810 {
811 struct glink_channel *channel;
812 int ret;
813 unsigned long flags;
814
815 channel = qcom_glink_alloc_channel(glink, name);
816 if (IS_ERR(channel))
817 return ERR_CAST(channel);
818
819 ret = qcom_glink_send_open_req(glink, channel);
820 if (ret)
821 goto release_channel;
822
823 ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
824 if (!ret)
825 goto err_timeout;
826
827 ret = wait_for_completion_timeout(&channel->open_req, 5 * HZ);
828 if (!ret)
829 goto err_timeout;
830
831 qcom_glink_send_open_ack(glink, channel);
832
833 return channel;
834
835 err_timeout:
836 /* qcom_glink_send_open_req() did register the channel in lcids*/
837 spin_lock_irqsave(&glink->idr_lock, flags);
838 idr_remove(&glink->lcids, channel->lcid);
839 spin_unlock_irqrestore(&glink->idr_lock, flags);
840
841 release_channel:
842 /* Release qcom_glink_send_open_req() reference */
843 kref_put(&channel->refcount, qcom_glink_channel_release);
844 /* Release qcom_glink_alloc_channel() reference */
845 kref_put(&channel->refcount, qcom_glink_channel_release);
846
847 return ERR_PTR(-ETIMEDOUT);
848 }
849
850 /* Remote initiated rpmsg_create_ept */
851 static int qcom_glink_create_remote(struct qcom_glink *glink,
852 struct glink_channel *channel)
853 {
854 int ret;
855
856 qcom_glink_send_open_ack(glink, channel);
857
858 ret = qcom_glink_send_open_req(glink, channel);
859 if (ret)
860 goto close_link;
861
862 ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
863 if (!ret) {
864 ret = -ETIMEDOUT;
865 goto close_link;
866 }
867
868 return 0;
869
870 close_link:
871 /*
872 * Send a close request to "undo" our open-ack. The close-ack will
873 * release the last reference.
874 */
875 qcom_glink_send_close_req(glink, channel);
876
877 /* Release qcom_glink_send_open_req() reference */
878 kref_put(&channel->refcount, qcom_glink_channel_release);
879
880 return ret;
881 }
882
883 static struct rpmsg_endpoint *qcom_glink_create_ept(struct rpmsg_device *rpdev,
884 rpmsg_rx_cb_t cb,
885 void *priv,
886 struct rpmsg_channel_info
887 chinfo)
888 {
889 struct glink_channel *parent = to_glink_channel(rpdev->ept);
890 struct glink_channel *channel;
891 struct qcom_glink *glink = parent->glink;
892 struct rpmsg_endpoint *ept;
893 const char *name = chinfo.name;
894 int cid;
895 int ret;
896 unsigned long flags;
897
898 spin_lock_irqsave(&glink->idr_lock, flags);
899 idr_for_each_entry(&glink->rcids, channel, cid) {
900 if (!strcmp(channel->name, name))
901 break;
902 }
903 spin_unlock_irqrestore(&glink->idr_lock, flags);
904
905 if (!channel) {
906 channel = qcom_glink_create_local(glink, name);
907 if (IS_ERR(channel))
908 return NULL;
909 } else {
910 ret = qcom_glink_create_remote(glink, channel);
911 if (ret)
912 return NULL;
913 }
914
915 ept = &channel->ept;
916 ept->rpdev = rpdev;
917 ept->cb = cb;
918 ept->priv = priv;
919 ept->ops = &glink_endpoint_ops;
920
921 return ept;
922 }
923
924 static void qcom_glink_destroy_ept(struct rpmsg_endpoint *ept)
925 {
926 struct glink_channel *channel = to_glink_channel(ept);
927 struct qcom_glink *glink = channel->glink;
928 unsigned long flags;
929
930 spin_lock_irqsave(&channel->recv_lock, flags);
931 channel->ept.cb = NULL;
932 spin_unlock_irqrestore(&channel->recv_lock, flags);
933
934 /* Decouple the potential rpdev from the channel */
935 channel->rpdev = NULL;
936
937 qcom_glink_send_close_req(glink, channel);
938 }
939
940 static int __qcom_glink_send(struct glink_channel *channel,
941 void *data, int len, bool wait)
942 {
943 struct qcom_glink *glink = channel->glink;
944 struct {
945 struct glink_msg msg;
946 __le32 chunk_size;
947 __le32 left_size;
948 } __packed req;
949
950 req.msg.cmd = cpu_to_le16(RPM_CMD_TX_DATA);
951 req.msg.param1 = cpu_to_le16(channel->lcid);
952 req.msg.param2 = cpu_to_le32(channel->rcid);
953 req.chunk_size = cpu_to_le32(len);
954 req.left_size = cpu_to_le32(0);
955
956 return qcom_glink_tx(glink, &req, sizeof(req), data, len, wait);
957 }
958
959 static int qcom_glink_send(struct rpmsg_endpoint *ept, void *data, int len)
960 {
961 struct glink_channel *channel = to_glink_channel(ept);
962
963 return __qcom_glink_send(channel, data, len, true);
964 }
965
966 static int qcom_glink_trysend(struct rpmsg_endpoint *ept, void *data, int len)
967 {
968 struct glink_channel *channel = to_glink_channel(ept);
969
970 return __qcom_glink_send(channel, data, len, false);
971 }
972
973 /*
974 * Finds the device_node for the glink child interested in this channel.
975 */
976 static struct device_node *qcom_glink_match_channel(struct device_node *node,
977 const char *channel)
978 {
979 struct device_node *child;
980 const char *name;
981 const char *key;
982 int ret;
983
984 for_each_available_child_of_node(node, child) {
985 key = "qcom,glink-channels";
986 ret = of_property_read_string(child, key, &name);
987 if (ret)
988 continue;
989
990 if (strcmp(name, channel) == 0)
991 return child;
992 }
993
994 return NULL;
995 }
996
997 static const struct rpmsg_device_ops glink_device_ops = {
998 .create_ept = qcom_glink_create_ept,
999 };
1000
1001 static const struct rpmsg_endpoint_ops glink_endpoint_ops = {
1002 .destroy_ept = qcom_glink_destroy_ept,
1003 .send = qcom_glink_send,
1004 .trysend = qcom_glink_trysend,
1005 };
1006
1007 static void qcom_glink_rpdev_release(struct device *dev)
1008 {
1009 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
1010 struct glink_channel *channel = to_glink_channel(rpdev->ept);
1011
1012 channel->rpdev = NULL;
1013 kfree(rpdev);
1014 }
1015
1016 static int qcom_glink_rx_open(struct qcom_glink *glink, unsigned int rcid,
1017 char *name)
1018 {
1019 struct glink_channel *channel;
1020 struct rpmsg_device *rpdev;
1021 bool create_device = false;
1022 struct device_node *node;
1023 int lcid;
1024 int ret;
1025 unsigned long flags;
1026
1027 spin_lock_irqsave(&glink->idr_lock, flags);
1028 idr_for_each_entry(&glink->lcids, channel, lcid) {
1029 if (!strcmp(channel->name, name))
1030 break;
1031 }
1032 spin_unlock_irqrestore(&glink->idr_lock, flags);
1033
1034 if (!channel) {
1035 channel = qcom_glink_alloc_channel(glink, name);
1036 if (IS_ERR(channel))
1037 return PTR_ERR(channel);
1038
1039 /* The opening dance was initiated by the remote */
1040 create_device = true;
1041 }
1042
1043 spin_lock_irqsave(&glink->idr_lock, flags);
1044 ret = idr_alloc(&glink->rcids, channel, rcid, rcid + 1, GFP_ATOMIC);
1045 if (ret < 0) {
1046 dev_err(glink->dev, "Unable to insert channel into rcid list\n");
1047 spin_unlock_irqrestore(&glink->idr_lock, flags);
1048 goto free_channel;
1049 }
1050 channel->rcid = ret;
1051 spin_unlock_irqrestore(&glink->idr_lock, flags);
1052
1053 complete(&channel->open_req);
1054
1055 if (create_device) {
1056 rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL);
1057 if (!rpdev) {
1058 ret = -ENOMEM;
1059 goto rcid_remove;
1060 }
1061
1062 rpdev->ept = &channel->ept;
1063 strncpy(rpdev->id.name, name, RPMSG_NAME_SIZE);
1064 rpdev->src = RPMSG_ADDR_ANY;
1065 rpdev->dst = RPMSG_ADDR_ANY;
1066 rpdev->ops = &glink_device_ops;
1067
1068 node = qcom_glink_match_channel(glink->dev->of_node, name);
1069 rpdev->dev.of_node = node;
1070 rpdev->dev.parent = glink->dev;
1071 rpdev->dev.release = qcom_glink_rpdev_release;
1072
1073 ret = rpmsg_register_device(rpdev);
1074 if (ret)
1075 goto free_rpdev;
1076
1077 channel->rpdev = rpdev;
1078 }
1079
1080 return 0;
1081
1082 free_rpdev:
1083 kfree(rpdev);
1084 rcid_remove:
1085 spin_lock_irqsave(&glink->idr_lock, flags);
1086 idr_remove(&glink->rcids, channel->rcid);
1087 channel->rcid = 0;
1088 spin_unlock_irqrestore(&glink->idr_lock, flags);
1089 free_channel:
1090 /* Release the reference, iff we took it */
1091 if (create_device)
1092 kref_put(&channel->refcount, qcom_glink_channel_release);
1093
1094 return ret;
1095 }
1096
1097 static void qcom_glink_rx_close(struct qcom_glink *glink, unsigned int rcid)
1098 {
1099 struct rpmsg_channel_info chinfo;
1100 struct glink_channel *channel;
1101 unsigned long flags;
1102
1103 spin_lock_irqsave(&glink->idr_lock, flags);
1104 channel = idr_find(&glink->rcids, rcid);
1105 spin_unlock_irqrestore(&glink->idr_lock, flags);
1106 if (WARN(!channel, "close request on unknown channel\n"))
1107 return;
1108
1109 if (channel->rpdev) {
1110 strncpy(chinfo.name, channel->name, sizeof(chinfo.name));
1111 chinfo.src = RPMSG_ADDR_ANY;
1112 chinfo.dst = RPMSG_ADDR_ANY;
1113
1114 rpmsg_unregister_device(glink->dev, &chinfo);
1115 }
1116
1117 qcom_glink_send_close_ack(glink, channel->rcid);
1118
1119 spin_lock_irqsave(&glink->idr_lock, flags);
1120 idr_remove(&glink->rcids, channel->rcid);
1121 channel->rcid = 0;
1122 spin_unlock_irqrestore(&glink->idr_lock, flags);
1123
1124 kref_put(&channel->refcount, qcom_glink_channel_release);
1125 }
1126
1127 static void qcom_glink_rx_close_ack(struct qcom_glink *glink, unsigned int lcid)
1128 {
1129 struct glink_channel *channel;
1130 unsigned long flags;
1131
1132 spin_lock_irqsave(&glink->idr_lock, flags);
1133 channel = idr_find(&glink->lcids, lcid);
1134 if (WARN(!channel, "close ack on unknown channel\n")) {
1135 spin_unlock_irqrestore(&glink->idr_lock, flags);
1136 return;
1137 }
1138
1139 idr_remove(&glink->lcids, channel->lcid);
1140 channel->lcid = 0;
1141 spin_unlock_irqrestore(&glink->idr_lock, flags);
1142
1143 kref_put(&channel->refcount, qcom_glink_channel_release);
1144 }
1145
1146 static void qcom_glink_work(struct work_struct *work)
1147 {
1148 struct qcom_glink *glink = container_of(work, struct qcom_glink,
1149 rx_work);
1150 struct glink_defer_cmd *dcmd;
1151 struct glink_msg *msg;
1152 unsigned long flags;
1153 unsigned int param1;
1154 unsigned int param2;
1155 unsigned int cmd;
1156
1157 for (;;) {
1158 spin_lock_irqsave(&glink->rx_lock, flags);
1159 if (list_empty(&glink->rx_queue)) {
1160 spin_unlock_irqrestore(&glink->rx_lock, flags);
1161 break;
1162 }
1163 dcmd = list_first_entry(&glink->rx_queue,
1164 struct glink_defer_cmd, node);
1165 list_del(&dcmd->node);
1166 spin_unlock_irqrestore(&glink->rx_lock, flags);
1167
1168 msg = &dcmd->msg;
1169 cmd = le16_to_cpu(msg->cmd);
1170 param1 = le16_to_cpu(msg->param1);
1171 param2 = le32_to_cpu(msg->param2);
1172
1173 switch (cmd) {
1174 case RPM_CMD_VERSION:
1175 qcom_glink_receive_version(glink, param1, param2);
1176 break;
1177 case RPM_CMD_VERSION_ACK:
1178 qcom_glink_receive_version_ack(glink, param1, param2);
1179 break;
1180 case RPM_CMD_OPEN:
1181 qcom_glink_rx_open(glink, param1, msg->data);
1182 break;
1183 case RPM_CMD_CLOSE:
1184 qcom_glink_rx_close(glink, param1);
1185 break;
1186 case RPM_CMD_CLOSE_ACK:
1187 qcom_glink_rx_close_ack(glink, param1);
1188 break;
1189 case RPM_CMD_RX_INTENT_REQ:
1190 qcom_glink_handle_intent_req(glink, param1, param2);
1191 break;
1192 default:
1193 WARN(1, "Unknown defer object %d\n", cmd);
1194 break;
1195 }
1196
1197 kfree(dcmd);
1198 }
1199 }
1200
1201 struct qcom_glink *qcom_glink_native_probe(struct device *dev,
1202 unsigned long features,
1203 struct qcom_glink_pipe *rx,
1204 struct qcom_glink_pipe *tx,
1205 bool intentless)
1206 {
1207 int irq;
1208 int ret;
1209 struct qcom_glink *glink;
1210
1211 glink = devm_kzalloc(dev, sizeof(*glink), GFP_KERNEL);
1212 if (!glink)
1213 return ERR_PTR(-ENOMEM);
1214
1215 glink->dev = dev;
1216 glink->tx_pipe = tx;
1217 glink->rx_pipe = rx;
1218
1219 glink->features = features;
1220 glink->intentless = intentless;
1221
1222 mutex_init(&glink->tx_lock);
1223 spin_lock_init(&glink->rx_lock);
1224 INIT_LIST_HEAD(&glink->rx_queue);
1225 INIT_WORK(&glink->rx_work, qcom_glink_work);
1226
1227 spin_lock_init(&glink->idr_lock);
1228 idr_init(&glink->lcids);
1229 idr_init(&glink->rcids);
1230
1231 glink->mbox_client.dev = dev;
1232 glink->mbox_chan = mbox_request_channel(&glink->mbox_client, 0);
1233 if (IS_ERR(glink->mbox_chan)) {
1234 if (PTR_ERR(glink->mbox_chan) != -EPROBE_DEFER)
1235 dev_err(dev, "failed to acquire IPC channel\n");
1236 return ERR_CAST(glink->mbox_chan);
1237 }
1238
1239 irq = of_irq_get(dev->of_node, 0);
1240 ret = devm_request_irq(dev, irq,
1241 qcom_glink_native_intr,
1242 IRQF_NO_SUSPEND | IRQF_SHARED,
1243 "glink-native", glink);
1244 if (ret) {
1245 dev_err(dev, "failed to request IRQ\n");
1246 return ERR_PTR(ret);
1247 }
1248
1249 glink->irq = irq;
1250
1251 ret = qcom_glink_send_version(glink);
1252 if (ret)
1253 return ERR_PTR(ret);
1254
1255 return glink;
1256 }
1257
1258 static int qcom_glink_remove_device(struct device *dev, void *data)
1259 {
1260 device_unregister(dev);
1261
1262 return 0;
1263 }
1264
1265 void qcom_glink_native_remove(struct qcom_glink *glink)
1266 {
1267 struct glink_channel *channel;
1268 int cid;
1269 int ret;
1270 unsigned long flags;
1271
1272 disable_irq(glink->irq);
1273 cancel_work_sync(&glink->rx_work);
1274
1275 ret = device_for_each_child(glink->dev, NULL, qcom_glink_remove_device);
1276 if (ret)
1277 dev_warn(glink->dev, "Can't remove GLINK devices: %d\n", ret);
1278
1279 spin_lock_irqsave(&glink->idr_lock, flags);
1280 /* Release any defunct local channels, waiting for close-ack */
1281 idr_for_each_entry(&glink->lcids, channel, cid)
1282 kref_put(&channel->refcount, qcom_glink_channel_release);
1283
1284 idr_destroy(&glink->lcids);
1285 idr_destroy(&glink->rcids);
1286 spin_unlock_irqrestore(&glink->idr_lock, flags);
1287 mbox_free_channel(glink->mbox_chan);
1288 }
1289
1290 void qcom_glink_native_unregister(struct qcom_glink *glink)
1291 {
1292 device_unregister(glink->dev);
1293 }