blob: 46e2530f61dad0d06900579610ff7570fe5b5dd4 [file] [log] [blame]
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301/*
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
Sricharan Rd31ad612017-08-24 12:51:32 +053034#define GLINK_VERSION_1 1
Bjorn Andersson835764dd2017-08-24 12:51:26 +053035
36#define RPM_GLINK_CID_MIN 1
37#define RPM_GLINK_CID_MAX 65536
38
39struct 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 */
55struct glink_defer_cmd {
56 struct list_head node;
57
58 struct glink_msg msg;
59 u8 data[];
60};
61
62/**
63 * struct qcom_glink - driver context, relates to one remote subsystem
64 * @dev: reference to the associated struct device
65 * @mbox_client: mailbox client
66 * @mbox_chan: mailbox channel
67 * @rx_pipe: pipe object for receive FIFO
68 * @tx_pipe: pipe object for transmit FIFO
69 * @irq: IRQ for signaling incoming events
70 * @rx_work: worker for handling received control messages
71 * @rx_lock: protects the @rx_queue
72 * @rx_queue: queue of received control messages to be processed in @rx_work
73 * @tx_lock: synchronizes operations on the tx fifo
74 * @idr_lock: synchronizes @lcids and @rcids modifications
75 * @lcids: idr of all channels with a known local channel id
76 * @rcids: idr of all channels with a known remote channel id
77 */
78struct qcom_glink {
79 struct device *dev;
80
81 struct mbox_client mbox_client;
82 struct mbox_chan *mbox_chan;
83
84 struct qcom_glink_pipe *rx_pipe;
85 struct qcom_glink_pipe *tx_pipe;
86
87 int irq;
88
89 struct work_struct rx_work;
90 spinlock_t rx_lock;
91 struct list_head rx_queue;
92
93 struct mutex tx_lock;
94
95 struct mutex idr_lock;
96 struct idr lcids;
97 struct idr rcids;
Sricharan Rd31ad612017-08-24 12:51:32 +053098 unsigned long features;
99
100 bool intentless;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530101};
102
103enum {
104 GLINK_STATE_CLOSED,
105 GLINK_STATE_OPENING,
106 GLINK_STATE_OPEN,
107 GLINK_STATE_CLOSING,
108};
109
110/**
111 * struct glink_channel - internal representation of a channel
112 * @rpdev: rpdev reference, only used for primary endpoints
113 * @ept: rpmsg endpoint this channel is associated with
114 * @glink: qcom_glink context handle
115 * @refcount: refcount for the channel object
116 * @recv_lock: guard for @ept.cb
117 * @name: unique channel name/identifier
118 * @lcid: channel id, in local space
119 * @rcid: channel id, in remote space
120 * @buf: receive buffer, for gathering fragments
121 * @buf_offset: write offset in @buf
122 * @buf_size: size of current @buf
123 * @open_ack: completed once remote has acked the open-request
124 * @open_req: completed once open-request has been received
125 */
126struct glink_channel {
127 struct rpmsg_endpoint ept;
128
129 struct rpmsg_device *rpdev;
130 struct qcom_glink *glink;
131
132 struct kref refcount;
133
134 spinlock_t recv_lock;
135
136 char *name;
137 unsigned int lcid;
138 unsigned int rcid;
139
140 void *buf;
141 int buf_offset;
142 int buf_size;
143
144 struct completion open_ack;
145 struct completion open_req;
146};
147
148#define to_glink_channel(_ept) container_of(_ept, struct glink_channel, ept)
149
150static const struct rpmsg_endpoint_ops glink_endpoint_ops;
151
152#define RPM_CMD_VERSION 0
153#define RPM_CMD_VERSION_ACK 1
154#define RPM_CMD_OPEN 2
155#define RPM_CMD_CLOSE 3
156#define RPM_CMD_OPEN_ACK 4
157#define RPM_CMD_TX_DATA 9
158#define RPM_CMD_CLOSE_ACK 11
159#define RPM_CMD_TX_DATA_CONT 12
160#define RPM_CMD_READ_NOTIF 13
161
162#define GLINK_FEATURE_INTENTLESS BIT(1)
163
164static struct glink_channel *qcom_glink_alloc_channel(struct qcom_glink *glink,
165 const char *name)
166{
167 struct glink_channel *channel;
168
169 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
170 if (!channel)
171 return ERR_PTR(-ENOMEM);
172
173 /* Setup glink internal glink_channel data */
174 spin_lock_init(&channel->recv_lock);
175 channel->glink = glink;
176 channel->name = kstrdup(name, GFP_KERNEL);
177
178 init_completion(&channel->open_req);
179 init_completion(&channel->open_ack);
180
181 kref_init(&channel->refcount);
182
183 return channel;
184}
185
186static void qcom_glink_channel_release(struct kref *ref)
187{
188 struct glink_channel *channel = container_of(ref, struct glink_channel,
189 refcount);
190
191 kfree(channel->name);
192 kfree(channel);
193}
194
195static size_t qcom_glink_rx_avail(struct qcom_glink *glink)
196{
197 return glink->rx_pipe->avail(glink->rx_pipe);
198}
199
200static void qcom_glink_rx_peak(struct qcom_glink *glink,
201 void *data, size_t count)
202{
203 glink->rx_pipe->peak(glink->rx_pipe, data, count);
204}
205
206static void qcom_glink_rx_advance(struct qcom_glink *glink, size_t count)
207{
208 glink->rx_pipe->advance(glink->rx_pipe, count);
209}
210
211static size_t qcom_glink_tx_avail(struct qcom_glink *glink)
212{
213 return glink->tx_pipe->avail(glink->tx_pipe);
214}
215
216static void qcom_glink_tx_write(struct qcom_glink *glink,
217 const void *hdr, size_t hlen,
218 const void *data, size_t dlen)
219{
220 glink->tx_pipe->write(glink->tx_pipe, hdr, hlen, data, dlen);
221}
222
223static int qcom_glink_tx(struct qcom_glink *glink,
224 const void *hdr, size_t hlen,
225 const void *data, size_t dlen, bool wait)
226{
227 unsigned int tlen = hlen + dlen;
228 int ret;
229
230 /* Reject packets that are too big */
231 if (tlen >= glink->tx_pipe->length)
232 return -EINVAL;
233
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530234 ret = mutex_lock_interruptible(&glink->tx_lock);
235 if (ret)
236 return ret;
237
238 while (qcom_glink_tx_avail(glink) < tlen) {
239 if (!wait) {
Sricharan Ra7df9df2017-08-24 12:51:28 +0530240 ret = -EAGAIN;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530241 goto out;
242 }
243
244 usleep_range(10000, 15000);
245 }
246
247 qcom_glink_tx_write(glink, hdr, hlen, data, dlen);
248
249 mbox_send_message(glink->mbox_chan, NULL);
250 mbox_client_txdone(glink->mbox_chan, 0);
251
252out:
253 mutex_unlock(&glink->tx_lock);
254
255 return ret;
256}
257
258static int qcom_glink_send_version(struct qcom_glink *glink)
259{
260 struct glink_msg msg;
261
262 msg.cmd = cpu_to_le16(RPM_CMD_VERSION);
Sricharan Rd31ad612017-08-24 12:51:32 +0530263 msg.param1 = cpu_to_le16(GLINK_VERSION_1);
264 msg.param2 = cpu_to_le32(glink->features);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530265
266 return qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
267}
268
269static void qcom_glink_send_version_ack(struct qcom_glink *glink)
270{
271 struct glink_msg msg;
272
273 msg.cmd = cpu_to_le16(RPM_CMD_VERSION_ACK);
Sricharan Rd31ad612017-08-24 12:51:32 +0530274 msg.param1 = cpu_to_le16(GLINK_VERSION_1);
275 msg.param2 = cpu_to_le32(glink->features);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530276
277 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
278}
279
280static void qcom_glink_send_open_ack(struct qcom_glink *glink,
281 struct glink_channel *channel)
282{
283 struct glink_msg msg;
284
285 msg.cmd = cpu_to_le16(RPM_CMD_OPEN_ACK);
286 msg.param1 = cpu_to_le16(channel->rcid);
287 msg.param2 = cpu_to_le32(0);
288
289 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
290}
291
292/**
293 * qcom_glink_send_open_req() - send a RPM_CMD_OPEN request to the remote
294 * @glink: Ptr to the glink edge
295 * @channel: Ptr to the channel that the open req is sent
296 *
297 * Allocates a local channel id and sends a RPM_CMD_OPEN message to the remote.
298 * Will return with refcount held, regardless of outcome.
299 *
300 * Returns 0 on success, negative errno otherwise.
301 */
302static int qcom_glink_send_open_req(struct qcom_glink *glink,
303 struct glink_channel *channel)
304{
305 struct {
306 struct glink_msg msg;
307 u8 name[GLINK_NAME_SIZE];
308 } __packed req;
309 int name_len = strlen(channel->name) + 1;
310 int req_len = ALIGN(sizeof(req.msg) + name_len, 8);
311 int ret;
312
313 kref_get(&channel->refcount);
314
315 mutex_lock(&glink->idr_lock);
316 ret = idr_alloc_cyclic(&glink->lcids, channel,
317 RPM_GLINK_CID_MIN, RPM_GLINK_CID_MAX,
318 GFP_KERNEL);
319 mutex_unlock(&glink->idr_lock);
320 if (ret < 0)
321 return ret;
322
323 channel->lcid = ret;
324
325 req.msg.cmd = cpu_to_le16(RPM_CMD_OPEN);
326 req.msg.param1 = cpu_to_le16(channel->lcid);
327 req.msg.param2 = cpu_to_le32(name_len);
328 strcpy(req.name, channel->name);
329
330 ret = qcom_glink_tx(glink, &req, req_len, NULL, 0, true);
331 if (ret)
332 goto remove_idr;
333
334 return 0;
335
336remove_idr:
337 mutex_lock(&glink->idr_lock);
338 idr_remove(&glink->lcids, channel->lcid);
339 channel->lcid = 0;
340 mutex_unlock(&glink->idr_lock);
341
342 return ret;
343}
344
345static void qcom_glink_send_close_req(struct qcom_glink *glink,
346 struct glink_channel *channel)
347{
348 struct glink_msg req;
349
350 req.cmd = cpu_to_le16(RPM_CMD_CLOSE);
351 req.param1 = cpu_to_le16(channel->lcid);
352 req.param2 = 0;
353
354 qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
355}
356
357static void qcom_glink_send_close_ack(struct qcom_glink *glink,
358 unsigned int rcid)
359{
360 struct glink_msg req;
361
362 req.cmd = cpu_to_le16(RPM_CMD_CLOSE_ACK);
363 req.param1 = cpu_to_le16(rcid);
364 req.param2 = 0;
365
366 qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
367}
368
Sricharan Rd31ad612017-08-24 12:51:32 +0530369/**
370 * qcom_glink_receive_version() - receive version/features from remote system
371 *
372 * @glink: pointer to transport interface
373 * @r_version: remote version
374 * @r_features: remote features
375 *
376 * This function is called in response to a remote-initiated version/feature
377 * negotiation sequence.
378 */
379static void qcom_glink_receive_version(struct qcom_glink *glink,
380 u32 version,
381 u32 features)
382{
383 switch (version) {
384 case 0:
385 break;
386 case GLINK_VERSION_1:
387 glink->features &= features;
388 /* FALLTHROUGH */
389 default:
390 qcom_glink_send_version_ack(glink);
391 break;
392 }
393}
394
395/**
396 * qcom_glink_receive_version_ack() - receive negotiation ack from remote system
397 *
398 * @glink: pointer to transport interface
399 * @r_version: remote version response
400 * @r_features: remote features response
401 *
402 * This function is called in response to a local-initiated version/feature
403 * negotiation sequence and is the counter-offer from the remote side based
404 * upon the initial version and feature set requested.
405 */
406static void qcom_glink_receive_version_ack(struct qcom_glink *glink,
407 u32 version,
408 u32 features)
409{
410 switch (version) {
411 case 0:
412 /* Version negotiation failed */
413 break;
414 case GLINK_VERSION_1:
415 if (features == glink->features)
416 break;
417
418 glink->features &= features;
419 /* FALLTHROUGH */
420 default:
421 qcom_glink_send_version(glink);
422 break;
423 }
424}
425
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530426static int qcom_glink_rx_defer(struct qcom_glink *glink, size_t extra)
427{
428 struct glink_defer_cmd *dcmd;
429
430 extra = ALIGN(extra, 8);
431
432 if (qcom_glink_rx_avail(glink) < sizeof(struct glink_msg) + extra) {
433 dev_dbg(glink->dev, "Insufficient data in rx fifo");
434 return -ENXIO;
435 }
436
437 dcmd = kzalloc(sizeof(*dcmd) + extra, GFP_ATOMIC);
438 if (!dcmd)
439 return -ENOMEM;
440
441 INIT_LIST_HEAD(&dcmd->node);
442
443 qcom_glink_rx_peak(glink, &dcmd->msg, sizeof(dcmd->msg) + extra);
444
445 spin_lock(&glink->rx_lock);
446 list_add_tail(&dcmd->node, &glink->rx_queue);
447 spin_unlock(&glink->rx_lock);
448
449 schedule_work(&glink->rx_work);
450 qcom_glink_rx_advance(glink, sizeof(dcmd->msg) + extra);
451
452 return 0;
453}
454
455static int qcom_glink_rx_data(struct qcom_glink *glink, size_t avail)
456{
457 struct glink_channel *channel;
458 struct {
459 struct glink_msg msg;
460 __le32 chunk_size;
461 __le32 left_size;
462 } __packed hdr;
463 unsigned int chunk_size;
464 unsigned int left_size;
465 unsigned int rcid;
466
467 if (avail < sizeof(hdr)) {
468 dev_dbg(glink->dev, "Not enough data in fifo\n");
469 return -EAGAIN;
470 }
471
472 qcom_glink_rx_peak(glink, &hdr, sizeof(hdr));
473 chunk_size = le32_to_cpu(hdr.chunk_size);
474 left_size = le32_to_cpu(hdr.left_size);
475
476 if (avail < sizeof(hdr) + chunk_size) {
477 dev_dbg(glink->dev, "Payload not yet in fifo\n");
478 return -EAGAIN;
479 }
480
481 if (WARN(chunk_size % 4, "Incoming data must be word aligned\n"))
482 return -EINVAL;
483
484 rcid = le16_to_cpu(hdr.msg.param1);
485 channel = idr_find(&glink->rcids, rcid);
486 if (!channel) {
487 dev_dbg(glink->dev, "Data on non-existing channel\n");
488
489 /* Drop the message */
490 qcom_glink_rx_advance(glink,
491 ALIGN(sizeof(hdr) + chunk_size, 8));
492 return 0;
493 }
494
495 /* Might have an ongoing, fragmented, message to append */
496 if (!channel->buf) {
497 channel->buf = kmalloc(chunk_size + left_size, GFP_ATOMIC);
498 if (!channel->buf)
499 return -ENOMEM;
500
501 channel->buf_size = chunk_size + left_size;
502 channel->buf_offset = 0;
503 }
504
505 qcom_glink_rx_advance(glink, sizeof(hdr));
506
507 if (channel->buf_size - channel->buf_offset < chunk_size) {
508 dev_err(glink->dev, "Insufficient space in input buffer\n");
509
510 /* The packet header lied, drop payload */
511 qcom_glink_rx_advance(glink, chunk_size);
512 return -ENOMEM;
513 }
514
515 qcom_glink_rx_peak(glink, channel->buf + channel->buf_offset,
516 chunk_size);
517 channel->buf_offset += chunk_size;
518
519 /* Handle message when no fragments remain to be received */
520 if (!left_size) {
521 spin_lock(&channel->recv_lock);
522 if (channel->ept.cb) {
523 channel->ept.cb(channel->ept.rpdev,
524 channel->buf,
525 channel->buf_offset,
526 channel->ept.priv,
527 RPMSG_ADDR_ANY);
528 }
529 spin_unlock(&channel->recv_lock);
530
531 kfree(channel->buf);
532 channel->buf = NULL;
533 channel->buf_size = 0;
534 }
535
536 /* Each message starts at 8 byte aligned address */
537 qcom_glink_rx_advance(glink, ALIGN(chunk_size, 8));
538
539 return 0;
540}
541
542static int qcom_glink_rx_open_ack(struct qcom_glink *glink, unsigned int lcid)
543{
544 struct glink_channel *channel;
545
546 channel = idr_find(&glink->lcids, lcid);
547 if (!channel) {
548 dev_err(glink->dev, "Invalid open ack packet\n");
549 return -EINVAL;
550 }
551
552 complete(&channel->open_ack);
553
554 return 0;
555}
556
557static irqreturn_t qcom_glink_native_intr(int irq, void *data)
558{
559 struct qcom_glink *glink = data;
560 struct glink_msg msg;
561 unsigned int param1;
562 unsigned int param2;
563 unsigned int avail;
564 unsigned int cmd;
565 int ret;
566
567 for (;;) {
568 avail = qcom_glink_rx_avail(glink);
569 if (avail < sizeof(msg))
570 break;
571
572 qcom_glink_rx_peak(glink, &msg, sizeof(msg));
573
574 cmd = le16_to_cpu(msg.cmd);
575 param1 = le16_to_cpu(msg.param1);
576 param2 = le32_to_cpu(msg.param2);
577
578 switch (cmd) {
579 case RPM_CMD_VERSION:
580 case RPM_CMD_VERSION_ACK:
581 case RPM_CMD_CLOSE:
582 case RPM_CMD_CLOSE_ACK:
583 ret = qcom_glink_rx_defer(glink, 0);
584 break;
585 case RPM_CMD_OPEN_ACK:
586 ret = qcom_glink_rx_open_ack(glink, param1);
587 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
588 break;
589 case RPM_CMD_OPEN:
590 ret = qcom_glink_rx_defer(glink, param2);
591 break;
592 case RPM_CMD_TX_DATA:
593 case RPM_CMD_TX_DATA_CONT:
594 ret = qcom_glink_rx_data(glink, avail);
595 break;
596 case RPM_CMD_READ_NOTIF:
597 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
598
599 mbox_send_message(glink->mbox_chan, NULL);
600 mbox_client_txdone(glink->mbox_chan, 0);
601
602 ret = 0;
603 break;
604 default:
605 dev_err(glink->dev, "unhandled rx cmd: %d\n", cmd);
606 ret = -EINVAL;
607 break;
608 }
609
610 if (ret)
611 break;
612 }
613
614 return IRQ_HANDLED;
615}
616
617/* Locally initiated rpmsg_create_ept */
618static struct glink_channel *qcom_glink_create_local(struct qcom_glink *glink,
619 const char *name)
620{
621 struct glink_channel *channel;
622 int ret;
623
624 channel = qcom_glink_alloc_channel(glink, name);
625 if (IS_ERR(channel))
626 return ERR_CAST(channel);
627
628 ret = qcom_glink_send_open_req(glink, channel);
629 if (ret)
630 goto release_channel;
631
632 ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
633 if (!ret)
634 goto err_timeout;
635
636 ret = wait_for_completion_timeout(&channel->open_req, 5 * HZ);
637 if (!ret)
638 goto err_timeout;
639
640 qcom_glink_send_open_ack(glink, channel);
641
642 return channel;
643
644err_timeout:
645 /* qcom_glink_send_open_req() did register the channel in lcids*/
646 mutex_lock(&glink->idr_lock);
647 idr_remove(&glink->lcids, channel->lcid);
648 mutex_unlock(&glink->idr_lock);
649
650release_channel:
651 /* Release qcom_glink_send_open_req() reference */
652 kref_put(&channel->refcount, qcom_glink_channel_release);
653 /* Release qcom_glink_alloc_channel() reference */
654 kref_put(&channel->refcount, qcom_glink_channel_release);
655
656 return ERR_PTR(-ETIMEDOUT);
657}
658
659/* Remote initiated rpmsg_create_ept */
660static int qcom_glink_create_remote(struct qcom_glink *glink,
661 struct glink_channel *channel)
662{
663 int ret;
664
665 qcom_glink_send_open_ack(glink, channel);
666
667 ret = qcom_glink_send_open_req(glink, channel);
668 if (ret)
669 goto close_link;
670
671 ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
672 if (!ret) {
673 ret = -ETIMEDOUT;
674 goto close_link;
675 }
676
677 return 0;
678
679close_link:
680 /*
681 * Send a close request to "undo" our open-ack. The close-ack will
682 * release the last reference.
683 */
684 qcom_glink_send_close_req(glink, channel);
685
686 /* Release qcom_glink_send_open_req() reference */
687 kref_put(&channel->refcount, qcom_glink_channel_release);
688
689 return ret;
690}
691
692static struct rpmsg_endpoint *qcom_glink_create_ept(struct rpmsg_device *rpdev,
693 rpmsg_rx_cb_t cb,
694 void *priv,
695 struct rpmsg_channel_info
696 chinfo)
697{
698 struct glink_channel *parent = to_glink_channel(rpdev->ept);
699 struct glink_channel *channel;
700 struct qcom_glink *glink = parent->glink;
701 struct rpmsg_endpoint *ept;
702 const char *name = chinfo.name;
703 int cid;
704 int ret;
705
706 idr_for_each_entry(&glink->rcids, channel, cid) {
707 if (!strcmp(channel->name, name))
708 break;
709 }
710
711 if (!channel) {
712 channel = qcom_glink_create_local(glink, name);
713 if (IS_ERR(channel))
714 return NULL;
715 } else {
716 ret = qcom_glink_create_remote(glink, channel);
717 if (ret)
718 return NULL;
719 }
720
721 ept = &channel->ept;
722 ept->rpdev = rpdev;
723 ept->cb = cb;
724 ept->priv = priv;
725 ept->ops = &glink_endpoint_ops;
726
727 return ept;
728}
729
730static void qcom_glink_destroy_ept(struct rpmsg_endpoint *ept)
731{
732 struct glink_channel *channel = to_glink_channel(ept);
733 struct qcom_glink *glink = channel->glink;
734 unsigned long flags;
735
736 spin_lock_irqsave(&channel->recv_lock, flags);
737 channel->ept.cb = NULL;
738 spin_unlock_irqrestore(&channel->recv_lock, flags);
739
740 /* Decouple the potential rpdev from the channel */
741 channel->rpdev = NULL;
742
743 qcom_glink_send_close_req(glink, channel);
744}
745
746static int __qcom_glink_send(struct glink_channel *channel,
747 void *data, int len, bool wait)
748{
749 struct qcom_glink *glink = channel->glink;
750 struct {
751 struct glink_msg msg;
752 __le32 chunk_size;
753 __le32 left_size;
754 } __packed req;
755
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530756 req.msg.cmd = cpu_to_le16(RPM_CMD_TX_DATA);
757 req.msg.param1 = cpu_to_le16(channel->lcid);
758 req.msg.param2 = cpu_to_le32(channel->rcid);
759 req.chunk_size = cpu_to_le32(len);
760 req.left_size = cpu_to_le32(0);
761
762 return qcom_glink_tx(glink, &req, sizeof(req), data, len, wait);
763}
764
765static int qcom_glink_send(struct rpmsg_endpoint *ept, void *data, int len)
766{
767 struct glink_channel *channel = to_glink_channel(ept);
768
769 return __qcom_glink_send(channel, data, len, true);
770}
771
772static int qcom_glink_trysend(struct rpmsg_endpoint *ept, void *data, int len)
773{
774 struct glink_channel *channel = to_glink_channel(ept);
775
776 return __qcom_glink_send(channel, data, len, false);
777}
778
779/*
780 * Finds the device_node for the glink child interested in this channel.
781 */
782static struct device_node *qcom_glink_match_channel(struct device_node *node,
783 const char *channel)
784{
785 struct device_node *child;
786 const char *name;
787 const char *key;
788 int ret;
789
790 for_each_available_child_of_node(node, child) {
791 key = "qcom,glink-channels";
792 ret = of_property_read_string(child, key, &name);
793 if (ret)
794 continue;
795
796 if (strcmp(name, channel) == 0)
797 return child;
798 }
799
800 return NULL;
801}
802
803static const struct rpmsg_device_ops glink_device_ops = {
804 .create_ept = qcom_glink_create_ept,
805};
806
807static const struct rpmsg_endpoint_ops glink_endpoint_ops = {
808 .destroy_ept = qcom_glink_destroy_ept,
809 .send = qcom_glink_send,
810 .trysend = qcom_glink_trysend,
811};
812
813static void qcom_glink_rpdev_release(struct device *dev)
814{
815 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
816 struct glink_channel *channel = to_glink_channel(rpdev->ept);
817
818 channel->rpdev = NULL;
819 kfree(rpdev);
820}
821
822static int qcom_glink_rx_open(struct qcom_glink *glink, unsigned int rcid,
823 char *name)
824{
825 struct glink_channel *channel;
826 struct rpmsg_device *rpdev;
827 bool create_device = false;
828 struct device_node *node;
829 int lcid;
830 int ret;
831
832 idr_for_each_entry(&glink->lcids, channel, lcid) {
833 if (!strcmp(channel->name, name))
834 break;
835 }
836
837 if (!channel) {
838 channel = qcom_glink_alloc_channel(glink, name);
839 if (IS_ERR(channel))
840 return PTR_ERR(channel);
841
842 /* The opening dance was initiated by the remote */
843 create_device = true;
844 }
845
846 mutex_lock(&glink->idr_lock);
847 ret = idr_alloc(&glink->rcids, channel, rcid, rcid + 1, GFP_KERNEL);
848 if (ret < 0) {
849 dev_err(glink->dev, "Unable to insert channel into rcid list\n");
850 mutex_unlock(&glink->idr_lock);
851 goto free_channel;
852 }
853 channel->rcid = ret;
854 mutex_unlock(&glink->idr_lock);
855
856 complete(&channel->open_req);
857
858 if (create_device) {
859 rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL);
860 if (!rpdev) {
861 ret = -ENOMEM;
862 goto rcid_remove;
863 }
864
865 rpdev->ept = &channel->ept;
866 strncpy(rpdev->id.name, name, RPMSG_NAME_SIZE);
867 rpdev->src = RPMSG_ADDR_ANY;
868 rpdev->dst = RPMSG_ADDR_ANY;
869 rpdev->ops = &glink_device_ops;
870
871 node = qcom_glink_match_channel(glink->dev->of_node, name);
872 rpdev->dev.of_node = node;
873 rpdev->dev.parent = glink->dev;
874 rpdev->dev.release = qcom_glink_rpdev_release;
875
876 ret = rpmsg_register_device(rpdev);
877 if (ret)
878 goto free_rpdev;
879
880 channel->rpdev = rpdev;
881 }
882
883 return 0;
884
885free_rpdev:
886 kfree(rpdev);
887rcid_remove:
888 mutex_lock(&glink->idr_lock);
889 idr_remove(&glink->rcids, channel->rcid);
890 channel->rcid = 0;
891 mutex_unlock(&glink->idr_lock);
892free_channel:
893 /* Release the reference, iff we took it */
894 if (create_device)
895 kref_put(&channel->refcount, qcom_glink_channel_release);
896
897 return ret;
898}
899
900static void qcom_glink_rx_close(struct qcom_glink *glink, unsigned int rcid)
901{
902 struct rpmsg_channel_info chinfo;
903 struct glink_channel *channel;
904
905 channel = idr_find(&glink->rcids, rcid);
906 if (WARN(!channel, "close request on unknown channel\n"))
907 return;
908
909 if (channel->rpdev) {
910 strncpy(chinfo.name, channel->name, sizeof(chinfo.name));
911 chinfo.src = RPMSG_ADDR_ANY;
912 chinfo.dst = RPMSG_ADDR_ANY;
913
914 rpmsg_unregister_device(glink->dev, &chinfo);
915 }
916
917 qcom_glink_send_close_ack(glink, channel->rcid);
918
919 mutex_lock(&glink->idr_lock);
920 idr_remove(&glink->rcids, channel->rcid);
921 channel->rcid = 0;
922 mutex_unlock(&glink->idr_lock);
923
924 kref_put(&channel->refcount, qcom_glink_channel_release);
925}
926
927static void qcom_glink_rx_close_ack(struct qcom_glink *glink, unsigned int lcid)
928{
929 struct glink_channel *channel;
930
931 channel = idr_find(&glink->lcids, lcid);
932 if (WARN(!channel, "close ack on unknown channel\n"))
933 return;
934
935 mutex_lock(&glink->idr_lock);
936 idr_remove(&glink->lcids, channel->lcid);
937 channel->lcid = 0;
938 mutex_unlock(&glink->idr_lock);
939
940 kref_put(&channel->refcount, qcom_glink_channel_release);
941}
942
943static void qcom_glink_work(struct work_struct *work)
944{
945 struct qcom_glink *glink = container_of(work, struct qcom_glink,
946 rx_work);
947 struct glink_defer_cmd *dcmd;
948 struct glink_msg *msg;
949 unsigned long flags;
950 unsigned int param1;
951 unsigned int param2;
952 unsigned int cmd;
953
954 for (;;) {
955 spin_lock_irqsave(&glink->rx_lock, flags);
956 if (list_empty(&glink->rx_queue)) {
957 spin_unlock_irqrestore(&glink->rx_lock, flags);
958 break;
959 }
960 dcmd = list_first_entry(&glink->rx_queue,
961 struct glink_defer_cmd, node);
962 list_del(&dcmd->node);
963 spin_unlock_irqrestore(&glink->rx_lock, flags);
964
965 msg = &dcmd->msg;
966 cmd = le16_to_cpu(msg->cmd);
967 param1 = le16_to_cpu(msg->param1);
968 param2 = le32_to_cpu(msg->param2);
969
970 switch (cmd) {
971 case RPM_CMD_VERSION:
Sricharan Rd31ad612017-08-24 12:51:32 +0530972 qcom_glink_receive_version(glink, param1, param2);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530973 break;
974 case RPM_CMD_VERSION_ACK:
Sricharan Rd31ad612017-08-24 12:51:32 +0530975 qcom_glink_receive_version_ack(glink, param1, param2);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530976 break;
977 case RPM_CMD_OPEN:
978 qcom_glink_rx_open(glink, param1, msg->data);
979 break;
980 case RPM_CMD_CLOSE:
981 qcom_glink_rx_close(glink, param1);
982 break;
983 case RPM_CMD_CLOSE_ACK:
984 qcom_glink_rx_close_ack(glink, param1);
985 break;
986 default:
987 WARN(1, "Unknown defer object %d\n", cmd);
988 break;
989 }
990
991 kfree(dcmd);
992 }
993}
994
995struct qcom_glink *qcom_glink_native_probe(struct device *dev,
Sricharan Rd31ad612017-08-24 12:51:32 +0530996 unsigned long features,
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530997 struct qcom_glink_pipe *rx,
998 struct qcom_glink_pipe *tx)
999{
1000 int irq;
1001 int ret;
1002 struct qcom_glink *glink;
1003
1004 glink = devm_kzalloc(dev, sizeof(*glink), GFP_KERNEL);
1005 if (!glink)
1006 return ERR_PTR(-ENOMEM);
1007
1008 glink->dev = dev;
1009 glink->tx_pipe = tx;
1010 glink->rx_pipe = rx;
1011
Sricharan Rd31ad612017-08-24 12:51:32 +05301012 glink->features = features;
1013
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301014 mutex_init(&glink->tx_lock);
1015 spin_lock_init(&glink->rx_lock);
1016 INIT_LIST_HEAD(&glink->rx_queue);
1017 INIT_WORK(&glink->rx_work, qcom_glink_work);
1018
1019 mutex_init(&glink->idr_lock);
1020 idr_init(&glink->lcids);
1021 idr_init(&glink->rcids);
1022
1023 glink->mbox_client.dev = dev;
1024 glink->mbox_chan = mbox_request_channel(&glink->mbox_client, 0);
1025 if (IS_ERR(glink->mbox_chan)) {
1026 if (PTR_ERR(glink->mbox_chan) != -EPROBE_DEFER)
1027 dev_err(dev, "failed to acquire IPC channel\n");
1028 return ERR_CAST(glink->mbox_chan);
1029 }
1030
1031 irq = of_irq_get(dev->of_node, 0);
1032 ret = devm_request_irq(dev, irq,
1033 qcom_glink_native_intr,
1034 IRQF_NO_SUSPEND | IRQF_SHARED,
1035 "glink-native", glink);
1036 if (ret) {
1037 dev_err(dev, "failed to request IRQ\n");
1038 return ERR_PTR(ret);
1039 }
1040
1041 glink->irq = irq;
1042
1043 ret = qcom_glink_send_version(glink);
1044 if (ret)
1045 return ERR_PTR(ret);
1046
1047 return glink;
1048}
1049
1050static int qcom_glink_remove_device(struct device *dev, void *data)
1051{
1052 device_unregister(dev);
1053
1054 return 0;
1055}
1056
1057void qcom_glink_native_remove(struct qcom_glink *glink)
1058{
1059 struct glink_channel *channel;
1060 int cid;
1061 int ret;
1062
1063 disable_irq(glink->irq);
1064 cancel_work_sync(&glink->rx_work);
1065
1066 ret = device_for_each_child(glink->dev, NULL, qcom_glink_remove_device);
1067 if (ret)
1068 dev_warn(glink->dev, "Can't remove GLINK devices: %d\n", ret);
1069
1070 /* Release any defunct local channels, waiting for close-ack */
1071 idr_for_each_entry(&glink->lcids, channel, cid)
1072 kref_put(&channel->refcount, qcom_glink_channel_release);
1073
1074 idr_destroy(&glink->lcids);
1075 idr_destroy(&glink->rcids);
Sricharan R76cf1102017-08-24 12:51:29 +05301076 mbox_free_channel(glink->mbox_chan);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301077}
Bjorn Anderssoncaf989c2017-08-24 12:51:30 +05301078
1079void qcom_glink_native_unregister(struct qcom_glink *glink)
1080{
1081 device_unregister(glink->dev);
1082}