blob: f3edf971e8c54c5c9811367efbeb17a93da35609 [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
Sricharan R44f6df92017-08-24 12:51:33 +053095 spinlock_t idr_lock;
Bjorn Andersson835764dd2017-08-24 12:51:26 +053096 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;
Sricharan R44f6df92017-08-24 12:51:33 +0530312 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530313
314 kref_get(&channel->refcount);
315
Sricharan R44f6df92017-08-24 12:51:33 +0530316 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530317 ret = idr_alloc_cyclic(&glink->lcids, channel,
318 RPM_GLINK_CID_MIN, RPM_GLINK_CID_MAX,
Sricharan R44f6df92017-08-24 12:51:33 +0530319 GFP_ATOMIC);
320 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530321 if (ret < 0)
322 return ret;
323
324 channel->lcid = ret;
325
326 req.msg.cmd = cpu_to_le16(RPM_CMD_OPEN);
327 req.msg.param1 = cpu_to_le16(channel->lcid);
328 req.msg.param2 = cpu_to_le32(name_len);
329 strcpy(req.name, channel->name);
330
331 ret = qcom_glink_tx(glink, &req, req_len, NULL, 0, true);
332 if (ret)
333 goto remove_idr;
334
335 return 0;
336
337remove_idr:
Sricharan R44f6df92017-08-24 12:51:33 +0530338 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530339 idr_remove(&glink->lcids, channel->lcid);
340 channel->lcid = 0;
Sricharan R44f6df92017-08-24 12:51:33 +0530341 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530342
343 return ret;
344}
345
346static void qcom_glink_send_close_req(struct qcom_glink *glink,
347 struct glink_channel *channel)
348{
349 struct glink_msg req;
350
351 req.cmd = cpu_to_le16(RPM_CMD_CLOSE);
352 req.param1 = cpu_to_le16(channel->lcid);
353 req.param2 = 0;
354
355 qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
356}
357
358static void qcom_glink_send_close_ack(struct qcom_glink *glink,
359 unsigned int rcid)
360{
361 struct glink_msg req;
362
363 req.cmd = cpu_to_le16(RPM_CMD_CLOSE_ACK);
364 req.param1 = cpu_to_le16(rcid);
365 req.param2 = 0;
366
367 qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
368}
369
Sricharan Rd31ad612017-08-24 12:51:32 +0530370/**
371 * qcom_glink_receive_version() - receive version/features from remote system
372 *
373 * @glink: pointer to transport interface
374 * @r_version: remote version
375 * @r_features: remote features
376 *
377 * This function is called in response to a remote-initiated version/feature
378 * negotiation sequence.
379 */
380static void qcom_glink_receive_version(struct qcom_glink *glink,
381 u32 version,
382 u32 features)
383{
384 switch (version) {
385 case 0:
386 break;
387 case GLINK_VERSION_1:
388 glink->features &= features;
389 /* FALLTHROUGH */
390 default:
391 qcom_glink_send_version_ack(glink);
392 break;
393 }
394}
395
396/**
397 * qcom_glink_receive_version_ack() - receive negotiation ack from remote system
398 *
399 * @glink: pointer to transport interface
400 * @r_version: remote version response
401 * @r_features: remote features response
402 *
403 * This function is called in response to a local-initiated version/feature
404 * negotiation sequence and is the counter-offer from the remote side based
405 * upon the initial version and feature set requested.
406 */
407static void qcom_glink_receive_version_ack(struct qcom_glink *glink,
408 u32 version,
409 u32 features)
410{
411 switch (version) {
412 case 0:
413 /* Version negotiation failed */
414 break;
415 case GLINK_VERSION_1:
416 if (features == glink->features)
417 break;
418
419 glink->features &= features;
420 /* FALLTHROUGH */
421 default:
422 qcom_glink_send_version(glink);
423 break;
424 }
425}
426
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530427static int qcom_glink_rx_defer(struct qcom_glink *glink, size_t extra)
428{
429 struct glink_defer_cmd *dcmd;
430
431 extra = ALIGN(extra, 8);
432
433 if (qcom_glink_rx_avail(glink) < sizeof(struct glink_msg) + extra) {
434 dev_dbg(glink->dev, "Insufficient data in rx fifo");
435 return -ENXIO;
436 }
437
438 dcmd = kzalloc(sizeof(*dcmd) + extra, GFP_ATOMIC);
439 if (!dcmd)
440 return -ENOMEM;
441
442 INIT_LIST_HEAD(&dcmd->node);
443
444 qcom_glink_rx_peak(glink, &dcmd->msg, sizeof(dcmd->msg) + extra);
445
446 spin_lock(&glink->rx_lock);
447 list_add_tail(&dcmd->node, &glink->rx_queue);
448 spin_unlock(&glink->rx_lock);
449
450 schedule_work(&glink->rx_work);
451 qcom_glink_rx_advance(glink, sizeof(dcmd->msg) + extra);
452
453 return 0;
454}
455
456static int qcom_glink_rx_data(struct qcom_glink *glink, size_t avail)
457{
458 struct glink_channel *channel;
459 struct {
460 struct glink_msg msg;
461 __le32 chunk_size;
462 __le32 left_size;
463 } __packed hdr;
464 unsigned int chunk_size;
465 unsigned int left_size;
466 unsigned int rcid;
Sricharan R44f6df92017-08-24 12:51:33 +0530467 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530468
469 if (avail < sizeof(hdr)) {
470 dev_dbg(glink->dev, "Not enough data in fifo\n");
471 return -EAGAIN;
472 }
473
474 qcom_glink_rx_peak(glink, &hdr, sizeof(hdr));
475 chunk_size = le32_to_cpu(hdr.chunk_size);
476 left_size = le32_to_cpu(hdr.left_size);
477
478 if (avail < sizeof(hdr) + chunk_size) {
479 dev_dbg(glink->dev, "Payload not yet in fifo\n");
480 return -EAGAIN;
481 }
482
483 if (WARN(chunk_size % 4, "Incoming data must be word aligned\n"))
484 return -EINVAL;
485
486 rcid = le16_to_cpu(hdr.msg.param1);
Sricharan R44f6df92017-08-24 12:51:33 +0530487 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530488 channel = idr_find(&glink->rcids, rcid);
Sricharan R44f6df92017-08-24 12:51:33 +0530489 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530490 if (!channel) {
491 dev_dbg(glink->dev, "Data on non-existing channel\n");
492
493 /* Drop the message */
494 qcom_glink_rx_advance(glink,
495 ALIGN(sizeof(hdr) + chunk_size, 8));
496 return 0;
497 }
498
499 /* Might have an ongoing, fragmented, message to append */
500 if (!channel->buf) {
501 channel->buf = kmalloc(chunk_size + left_size, GFP_ATOMIC);
502 if (!channel->buf)
503 return -ENOMEM;
504
505 channel->buf_size = chunk_size + left_size;
506 channel->buf_offset = 0;
507 }
508
509 qcom_glink_rx_advance(glink, sizeof(hdr));
510
511 if (channel->buf_size - channel->buf_offset < chunk_size) {
512 dev_err(glink->dev, "Insufficient space in input buffer\n");
513
514 /* The packet header lied, drop payload */
515 qcom_glink_rx_advance(glink, chunk_size);
516 return -ENOMEM;
517 }
518
519 qcom_glink_rx_peak(glink, channel->buf + channel->buf_offset,
520 chunk_size);
521 channel->buf_offset += chunk_size;
522
523 /* Handle message when no fragments remain to be received */
524 if (!left_size) {
525 spin_lock(&channel->recv_lock);
526 if (channel->ept.cb) {
527 channel->ept.cb(channel->ept.rpdev,
528 channel->buf,
529 channel->buf_offset,
530 channel->ept.priv,
531 RPMSG_ADDR_ANY);
532 }
533 spin_unlock(&channel->recv_lock);
534
535 kfree(channel->buf);
536 channel->buf = NULL;
537 channel->buf_size = 0;
538 }
539
540 /* Each message starts at 8 byte aligned address */
541 qcom_glink_rx_advance(glink, ALIGN(chunk_size, 8));
542
543 return 0;
544}
545
546static int qcom_glink_rx_open_ack(struct qcom_glink *glink, unsigned int lcid)
547{
548 struct glink_channel *channel;
549
Sricharan R44f6df92017-08-24 12:51:33 +0530550 spin_lock(&glink->idr_lock);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530551 channel = idr_find(&glink->lcids, lcid);
552 if (!channel) {
553 dev_err(glink->dev, "Invalid open ack packet\n");
554 return -EINVAL;
555 }
Sricharan R44f6df92017-08-24 12:51:33 +0530556 spin_unlock(&glink->idr_lock);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530557
558 complete(&channel->open_ack);
559
560 return 0;
561}
562
563static irqreturn_t qcom_glink_native_intr(int irq, void *data)
564{
565 struct qcom_glink *glink = data;
566 struct glink_msg msg;
567 unsigned int param1;
568 unsigned int param2;
569 unsigned int avail;
570 unsigned int cmd;
571 int ret;
572
573 for (;;) {
574 avail = qcom_glink_rx_avail(glink);
575 if (avail < sizeof(msg))
576 break;
577
578 qcom_glink_rx_peak(glink, &msg, sizeof(msg));
579
580 cmd = le16_to_cpu(msg.cmd);
581 param1 = le16_to_cpu(msg.param1);
582 param2 = le32_to_cpu(msg.param2);
583
584 switch (cmd) {
585 case RPM_CMD_VERSION:
586 case RPM_CMD_VERSION_ACK:
587 case RPM_CMD_CLOSE:
588 case RPM_CMD_CLOSE_ACK:
589 ret = qcom_glink_rx_defer(glink, 0);
590 break;
591 case RPM_CMD_OPEN_ACK:
592 ret = qcom_glink_rx_open_ack(glink, param1);
593 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
594 break;
595 case RPM_CMD_OPEN:
596 ret = qcom_glink_rx_defer(glink, param2);
597 break;
598 case RPM_CMD_TX_DATA:
599 case RPM_CMD_TX_DATA_CONT:
600 ret = qcom_glink_rx_data(glink, avail);
601 break;
602 case RPM_CMD_READ_NOTIF:
603 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
604
605 mbox_send_message(glink->mbox_chan, NULL);
606 mbox_client_txdone(glink->mbox_chan, 0);
607
608 ret = 0;
609 break;
610 default:
611 dev_err(glink->dev, "unhandled rx cmd: %d\n", cmd);
612 ret = -EINVAL;
613 break;
614 }
615
616 if (ret)
617 break;
618 }
619
620 return IRQ_HANDLED;
621}
622
623/* Locally initiated rpmsg_create_ept */
624static struct glink_channel *qcom_glink_create_local(struct qcom_glink *glink,
625 const char *name)
626{
627 struct glink_channel *channel;
628 int ret;
Sricharan R44f6df92017-08-24 12:51:33 +0530629 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530630
631 channel = qcom_glink_alloc_channel(glink, name);
632 if (IS_ERR(channel))
633 return ERR_CAST(channel);
634
635 ret = qcom_glink_send_open_req(glink, channel);
636 if (ret)
637 goto release_channel;
638
639 ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
640 if (!ret)
641 goto err_timeout;
642
643 ret = wait_for_completion_timeout(&channel->open_req, 5 * HZ);
644 if (!ret)
645 goto err_timeout;
646
647 qcom_glink_send_open_ack(glink, channel);
648
649 return channel;
650
651err_timeout:
652 /* qcom_glink_send_open_req() did register the channel in lcids*/
Sricharan R44f6df92017-08-24 12:51:33 +0530653 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530654 idr_remove(&glink->lcids, channel->lcid);
Sricharan R44f6df92017-08-24 12:51:33 +0530655 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530656
657release_channel:
658 /* Release qcom_glink_send_open_req() reference */
659 kref_put(&channel->refcount, qcom_glink_channel_release);
660 /* Release qcom_glink_alloc_channel() reference */
661 kref_put(&channel->refcount, qcom_glink_channel_release);
662
663 return ERR_PTR(-ETIMEDOUT);
664}
665
666/* Remote initiated rpmsg_create_ept */
667static int qcom_glink_create_remote(struct qcom_glink *glink,
668 struct glink_channel *channel)
669{
670 int ret;
671
672 qcom_glink_send_open_ack(glink, channel);
673
674 ret = qcom_glink_send_open_req(glink, channel);
675 if (ret)
676 goto close_link;
677
678 ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
679 if (!ret) {
680 ret = -ETIMEDOUT;
681 goto close_link;
682 }
683
684 return 0;
685
686close_link:
687 /*
688 * Send a close request to "undo" our open-ack. The close-ack will
689 * release the last reference.
690 */
691 qcom_glink_send_close_req(glink, channel);
692
693 /* Release qcom_glink_send_open_req() reference */
694 kref_put(&channel->refcount, qcom_glink_channel_release);
695
696 return ret;
697}
698
699static struct rpmsg_endpoint *qcom_glink_create_ept(struct rpmsg_device *rpdev,
700 rpmsg_rx_cb_t cb,
701 void *priv,
702 struct rpmsg_channel_info
703 chinfo)
704{
705 struct glink_channel *parent = to_glink_channel(rpdev->ept);
706 struct glink_channel *channel;
707 struct qcom_glink *glink = parent->glink;
708 struct rpmsg_endpoint *ept;
709 const char *name = chinfo.name;
710 int cid;
711 int ret;
Sricharan R44f6df92017-08-24 12:51:33 +0530712 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530713
Sricharan R44f6df92017-08-24 12:51:33 +0530714 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530715 idr_for_each_entry(&glink->rcids, channel, cid) {
716 if (!strcmp(channel->name, name))
717 break;
718 }
Sricharan R44f6df92017-08-24 12:51:33 +0530719 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530720
721 if (!channel) {
722 channel = qcom_glink_create_local(glink, name);
723 if (IS_ERR(channel))
724 return NULL;
725 } else {
726 ret = qcom_glink_create_remote(glink, channel);
727 if (ret)
728 return NULL;
729 }
730
731 ept = &channel->ept;
732 ept->rpdev = rpdev;
733 ept->cb = cb;
734 ept->priv = priv;
735 ept->ops = &glink_endpoint_ops;
736
737 return ept;
738}
739
740static void qcom_glink_destroy_ept(struct rpmsg_endpoint *ept)
741{
742 struct glink_channel *channel = to_glink_channel(ept);
743 struct qcom_glink *glink = channel->glink;
744 unsigned long flags;
745
746 spin_lock_irqsave(&channel->recv_lock, flags);
747 channel->ept.cb = NULL;
748 spin_unlock_irqrestore(&channel->recv_lock, flags);
749
750 /* Decouple the potential rpdev from the channel */
751 channel->rpdev = NULL;
752
753 qcom_glink_send_close_req(glink, channel);
754}
755
756static int __qcom_glink_send(struct glink_channel *channel,
757 void *data, int len, bool wait)
758{
759 struct qcom_glink *glink = channel->glink;
760 struct {
761 struct glink_msg msg;
762 __le32 chunk_size;
763 __le32 left_size;
764 } __packed req;
765
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530766 req.msg.cmd = cpu_to_le16(RPM_CMD_TX_DATA);
767 req.msg.param1 = cpu_to_le16(channel->lcid);
768 req.msg.param2 = cpu_to_le32(channel->rcid);
769 req.chunk_size = cpu_to_le32(len);
770 req.left_size = cpu_to_le32(0);
771
772 return qcom_glink_tx(glink, &req, sizeof(req), data, len, wait);
773}
774
775static int qcom_glink_send(struct rpmsg_endpoint *ept, void *data, int len)
776{
777 struct glink_channel *channel = to_glink_channel(ept);
778
779 return __qcom_glink_send(channel, data, len, true);
780}
781
782static int qcom_glink_trysend(struct rpmsg_endpoint *ept, void *data, int len)
783{
784 struct glink_channel *channel = to_glink_channel(ept);
785
786 return __qcom_glink_send(channel, data, len, false);
787}
788
789/*
790 * Finds the device_node for the glink child interested in this channel.
791 */
792static struct device_node *qcom_glink_match_channel(struct device_node *node,
793 const char *channel)
794{
795 struct device_node *child;
796 const char *name;
797 const char *key;
798 int ret;
799
800 for_each_available_child_of_node(node, child) {
801 key = "qcom,glink-channels";
802 ret = of_property_read_string(child, key, &name);
803 if (ret)
804 continue;
805
806 if (strcmp(name, channel) == 0)
807 return child;
808 }
809
810 return NULL;
811}
812
813static const struct rpmsg_device_ops glink_device_ops = {
814 .create_ept = qcom_glink_create_ept,
815};
816
817static const struct rpmsg_endpoint_ops glink_endpoint_ops = {
818 .destroy_ept = qcom_glink_destroy_ept,
819 .send = qcom_glink_send,
820 .trysend = qcom_glink_trysend,
821};
822
823static void qcom_glink_rpdev_release(struct device *dev)
824{
825 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
826 struct glink_channel *channel = to_glink_channel(rpdev->ept);
827
828 channel->rpdev = NULL;
829 kfree(rpdev);
830}
831
832static int qcom_glink_rx_open(struct qcom_glink *glink, unsigned int rcid,
833 char *name)
834{
835 struct glink_channel *channel;
836 struct rpmsg_device *rpdev;
837 bool create_device = false;
838 struct device_node *node;
839 int lcid;
840 int ret;
Sricharan R44f6df92017-08-24 12:51:33 +0530841 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530842
Sricharan R44f6df92017-08-24 12:51:33 +0530843 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530844 idr_for_each_entry(&glink->lcids, channel, lcid) {
845 if (!strcmp(channel->name, name))
846 break;
847 }
Sricharan R44f6df92017-08-24 12:51:33 +0530848 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530849
850 if (!channel) {
851 channel = qcom_glink_alloc_channel(glink, name);
852 if (IS_ERR(channel))
853 return PTR_ERR(channel);
854
855 /* The opening dance was initiated by the remote */
856 create_device = true;
857 }
858
Sricharan R44f6df92017-08-24 12:51:33 +0530859 spin_lock_irqsave(&glink->idr_lock, flags);
860 ret = idr_alloc(&glink->rcids, channel, rcid, rcid + 1, GFP_ATOMIC);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530861 if (ret < 0) {
862 dev_err(glink->dev, "Unable to insert channel into rcid list\n");
Sricharan R44f6df92017-08-24 12:51:33 +0530863 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530864 goto free_channel;
865 }
866 channel->rcid = ret;
Sricharan R44f6df92017-08-24 12:51:33 +0530867 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530868
869 complete(&channel->open_req);
870
871 if (create_device) {
872 rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL);
873 if (!rpdev) {
874 ret = -ENOMEM;
875 goto rcid_remove;
876 }
877
878 rpdev->ept = &channel->ept;
879 strncpy(rpdev->id.name, name, RPMSG_NAME_SIZE);
880 rpdev->src = RPMSG_ADDR_ANY;
881 rpdev->dst = RPMSG_ADDR_ANY;
882 rpdev->ops = &glink_device_ops;
883
884 node = qcom_glink_match_channel(glink->dev->of_node, name);
885 rpdev->dev.of_node = node;
886 rpdev->dev.parent = glink->dev;
887 rpdev->dev.release = qcom_glink_rpdev_release;
888
889 ret = rpmsg_register_device(rpdev);
890 if (ret)
891 goto free_rpdev;
892
893 channel->rpdev = rpdev;
894 }
895
896 return 0;
897
898free_rpdev:
899 kfree(rpdev);
900rcid_remove:
Sricharan R44f6df92017-08-24 12:51:33 +0530901 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530902 idr_remove(&glink->rcids, channel->rcid);
903 channel->rcid = 0;
Sricharan R44f6df92017-08-24 12:51:33 +0530904 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530905free_channel:
906 /* Release the reference, iff we took it */
907 if (create_device)
908 kref_put(&channel->refcount, qcom_glink_channel_release);
909
910 return ret;
911}
912
913static void qcom_glink_rx_close(struct qcom_glink *glink, unsigned int rcid)
914{
915 struct rpmsg_channel_info chinfo;
916 struct glink_channel *channel;
Sricharan R44f6df92017-08-24 12:51:33 +0530917 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530918
Sricharan R44f6df92017-08-24 12:51:33 +0530919 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530920 channel = idr_find(&glink->rcids, rcid);
Sricharan R44f6df92017-08-24 12:51:33 +0530921 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530922 if (WARN(!channel, "close request on unknown channel\n"))
923 return;
924
925 if (channel->rpdev) {
926 strncpy(chinfo.name, channel->name, sizeof(chinfo.name));
927 chinfo.src = RPMSG_ADDR_ANY;
928 chinfo.dst = RPMSG_ADDR_ANY;
929
930 rpmsg_unregister_device(glink->dev, &chinfo);
931 }
932
933 qcom_glink_send_close_ack(glink, channel->rcid);
934
Sricharan R44f6df92017-08-24 12:51:33 +0530935 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530936 idr_remove(&glink->rcids, channel->rcid);
937 channel->rcid = 0;
Sricharan R44f6df92017-08-24 12:51:33 +0530938 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530939
940 kref_put(&channel->refcount, qcom_glink_channel_release);
941}
942
943static void qcom_glink_rx_close_ack(struct qcom_glink *glink, unsigned int lcid)
944{
945 struct glink_channel *channel;
Sricharan R44f6df92017-08-24 12:51:33 +0530946 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530947
Sricharan R44f6df92017-08-24 12:51:33 +0530948 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530949 channel = idr_find(&glink->lcids, lcid);
Sricharan R44f6df92017-08-24 12:51:33 +0530950 if (WARN(!channel, "close ack on unknown channel\n")) {
951 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530952 return;
Sricharan R44f6df92017-08-24 12:51:33 +0530953 }
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530954
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530955 idr_remove(&glink->lcids, channel->lcid);
956 channel->lcid = 0;
Sricharan R44f6df92017-08-24 12:51:33 +0530957 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530958
959 kref_put(&channel->refcount, qcom_glink_channel_release);
960}
961
962static void qcom_glink_work(struct work_struct *work)
963{
964 struct qcom_glink *glink = container_of(work, struct qcom_glink,
965 rx_work);
966 struct glink_defer_cmd *dcmd;
967 struct glink_msg *msg;
968 unsigned long flags;
969 unsigned int param1;
970 unsigned int param2;
971 unsigned int cmd;
972
973 for (;;) {
974 spin_lock_irqsave(&glink->rx_lock, flags);
975 if (list_empty(&glink->rx_queue)) {
976 spin_unlock_irqrestore(&glink->rx_lock, flags);
977 break;
978 }
979 dcmd = list_first_entry(&glink->rx_queue,
980 struct glink_defer_cmd, node);
981 list_del(&dcmd->node);
982 spin_unlock_irqrestore(&glink->rx_lock, flags);
983
984 msg = &dcmd->msg;
985 cmd = le16_to_cpu(msg->cmd);
986 param1 = le16_to_cpu(msg->param1);
987 param2 = le32_to_cpu(msg->param2);
988
989 switch (cmd) {
990 case RPM_CMD_VERSION:
Sricharan Rd31ad612017-08-24 12:51:32 +0530991 qcom_glink_receive_version(glink, param1, param2);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530992 break;
993 case RPM_CMD_VERSION_ACK:
Sricharan Rd31ad612017-08-24 12:51:32 +0530994 qcom_glink_receive_version_ack(glink, param1, param2);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530995 break;
996 case RPM_CMD_OPEN:
997 qcom_glink_rx_open(glink, param1, msg->data);
998 break;
999 case RPM_CMD_CLOSE:
1000 qcom_glink_rx_close(glink, param1);
1001 break;
1002 case RPM_CMD_CLOSE_ACK:
1003 qcom_glink_rx_close_ack(glink, param1);
1004 break;
1005 default:
1006 WARN(1, "Unknown defer object %d\n", cmd);
1007 break;
1008 }
1009
1010 kfree(dcmd);
1011 }
1012}
1013
1014struct qcom_glink *qcom_glink_native_probe(struct device *dev,
Sricharan Rd31ad612017-08-24 12:51:32 +05301015 unsigned long features,
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301016 struct qcom_glink_pipe *rx,
1017 struct qcom_glink_pipe *tx)
1018{
1019 int irq;
1020 int ret;
1021 struct qcom_glink *glink;
1022
1023 glink = devm_kzalloc(dev, sizeof(*glink), GFP_KERNEL);
1024 if (!glink)
1025 return ERR_PTR(-ENOMEM);
1026
1027 glink->dev = dev;
1028 glink->tx_pipe = tx;
1029 glink->rx_pipe = rx;
1030
Sricharan Rd31ad612017-08-24 12:51:32 +05301031 glink->features = features;
1032
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301033 mutex_init(&glink->tx_lock);
1034 spin_lock_init(&glink->rx_lock);
1035 INIT_LIST_HEAD(&glink->rx_queue);
1036 INIT_WORK(&glink->rx_work, qcom_glink_work);
1037
Sricharan R44f6df92017-08-24 12:51:33 +05301038 spin_lock_init(&glink->idr_lock);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301039 idr_init(&glink->lcids);
1040 idr_init(&glink->rcids);
1041
1042 glink->mbox_client.dev = dev;
1043 glink->mbox_chan = mbox_request_channel(&glink->mbox_client, 0);
1044 if (IS_ERR(glink->mbox_chan)) {
1045 if (PTR_ERR(glink->mbox_chan) != -EPROBE_DEFER)
1046 dev_err(dev, "failed to acquire IPC channel\n");
1047 return ERR_CAST(glink->mbox_chan);
1048 }
1049
1050 irq = of_irq_get(dev->of_node, 0);
1051 ret = devm_request_irq(dev, irq,
1052 qcom_glink_native_intr,
1053 IRQF_NO_SUSPEND | IRQF_SHARED,
1054 "glink-native", glink);
1055 if (ret) {
1056 dev_err(dev, "failed to request IRQ\n");
1057 return ERR_PTR(ret);
1058 }
1059
1060 glink->irq = irq;
1061
1062 ret = qcom_glink_send_version(glink);
1063 if (ret)
1064 return ERR_PTR(ret);
1065
1066 return glink;
1067}
1068
1069static int qcom_glink_remove_device(struct device *dev, void *data)
1070{
1071 device_unregister(dev);
1072
1073 return 0;
1074}
1075
1076void qcom_glink_native_remove(struct qcom_glink *glink)
1077{
1078 struct glink_channel *channel;
1079 int cid;
1080 int ret;
Sricharan R44f6df92017-08-24 12:51:33 +05301081 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301082
1083 disable_irq(glink->irq);
1084 cancel_work_sync(&glink->rx_work);
1085
1086 ret = device_for_each_child(glink->dev, NULL, qcom_glink_remove_device);
1087 if (ret)
1088 dev_warn(glink->dev, "Can't remove GLINK devices: %d\n", ret);
1089
Sricharan R44f6df92017-08-24 12:51:33 +05301090 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301091 /* Release any defunct local channels, waiting for close-ack */
1092 idr_for_each_entry(&glink->lcids, channel, cid)
1093 kref_put(&channel->refcount, qcom_glink_channel_release);
1094
1095 idr_destroy(&glink->lcids);
1096 idr_destroy(&glink->rcids);
Sricharan R44f6df92017-08-24 12:51:33 +05301097 spin_unlock_irqrestore(&glink->idr_lock, flags);
Sricharan R76cf1102017-08-24 12:51:29 +05301098 mbox_free_channel(glink->mbox_chan);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301099}
Bjorn Anderssoncaf989c2017-08-24 12:51:30 +05301100
1101void qcom_glink_native_unregister(struct qcom_glink *glink)
1102{
1103 device_unregister(glink->dev);
1104}