blob: 8592e7b15a2c362a88485c35f71aea2a56c223ed [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>
Sricharan R6bf68cb2017-08-24 12:51:38 +053026#include <linux/sizes.h>
Bjorn Andersson835764dd2017-08-24 12:51:26 +053027#include <linux/slab.h>
28#include <linux/workqueue.h>
29#include <linux/mailbox_client.h>
30
31#include "rpmsg_internal.h"
32#include "qcom_glink_native.h"
33
34#define GLINK_NAME_SIZE 32
Sricharan Rd31ad612017-08-24 12:51:32 +053035#define GLINK_VERSION_1 1
Bjorn Andersson835764dd2017-08-24 12:51:26 +053036
37#define RPM_GLINK_CID_MIN 1
38#define RPM_GLINK_CID_MAX 65536
39
40struct glink_msg {
41 __le16 cmd;
42 __le16 param1;
43 __le32 param2;
44 u8 data[];
45} __packed;
46
47/**
48 * struct glink_defer_cmd - deferred incoming control message
49 * @node: list node
50 * @msg: message header
51 * data: payload of the message
52 *
53 * Copy of a received control message, to be added to @rx_queue and processed
54 * by @rx_work of @qcom_glink.
55 */
56struct glink_defer_cmd {
57 struct list_head node;
58
59 struct glink_msg msg;
60 u8 data[];
61};
62
63/**
Sricharan R933b45d2017-08-24 12:51:34 +053064 * struct glink_core_rx_intent - RX intent
65 * RX intent
66 *
67 * data: pointer to the data (may be NULL for zero-copy)
68 * id: remote or local intent ID
69 * size: size of the original intent (do not modify)
70 * reuse: To mark if the intent can be reused after first use
71 * in_use: To mark if intent is already in use for the channel
72 * offset: next write offset (initially 0)
73 */
74struct glink_core_rx_intent {
75 void *data;
76 u32 id;
77 size_t size;
78 bool reuse;
79 bool in_use;
80 u32 offset;
Sricharan R1d2ea362017-08-24 12:51:37 +053081
82 struct list_head node;
Sricharan R933b45d2017-08-24 12:51:34 +053083};
84
85/**
Bjorn Andersson835764dd2017-08-24 12:51:26 +053086 * struct qcom_glink - driver context, relates to one remote subsystem
87 * @dev: reference to the associated struct device
88 * @mbox_client: mailbox client
89 * @mbox_chan: mailbox channel
90 * @rx_pipe: pipe object for receive FIFO
91 * @tx_pipe: pipe object for transmit FIFO
92 * @irq: IRQ for signaling incoming events
93 * @rx_work: worker for handling received control messages
94 * @rx_lock: protects the @rx_queue
95 * @rx_queue: queue of received control messages to be processed in @rx_work
96 * @tx_lock: synchronizes operations on the tx fifo
97 * @idr_lock: synchronizes @lcids and @rcids modifications
98 * @lcids: idr of all channels with a known local channel id
99 * @rcids: idr of all channels with a known remote channel id
100 */
101struct qcom_glink {
102 struct device *dev;
103
104 struct mbox_client mbox_client;
105 struct mbox_chan *mbox_chan;
106
107 struct qcom_glink_pipe *rx_pipe;
108 struct qcom_glink_pipe *tx_pipe;
109
110 int irq;
111
112 struct work_struct rx_work;
113 spinlock_t rx_lock;
114 struct list_head rx_queue;
115
116 struct mutex tx_lock;
117
Sricharan R44f6df92017-08-24 12:51:33 +0530118 spinlock_t idr_lock;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530119 struct idr lcids;
120 struct idr rcids;
Sricharan Rd31ad612017-08-24 12:51:32 +0530121 unsigned long features;
122
123 bool intentless;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530124};
125
126enum {
127 GLINK_STATE_CLOSED,
128 GLINK_STATE_OPENING,
129 GLINK_STATE_OPEN,
130 GLINK_STATE_CLOSING,
131};
132
133/**
134 * struct glink_channel - internal representation of a channel
135 * @rpdev: rpdev reference, only used for primary endpoints
136 * @ept: rpmsg endpoint this channel is associated with
137 * @glink: qcom_glink context handle
138 * @refcount: refcount for the channel object
139 * @recv_lock: guard for @ept.cb
140 * @name: unique channel name/identifier
141 * @lcid: channel id, in local space
142 * @rcid: channel id, in remote space
Sricharan Rdacbb352017-08-24 12:51:39 +0530143 * @intent_lock: lock for protection of @liids, @riids
Sricharan R933b45d2017-08-24 12:51:34 +0530144 * @liids: idr of all local intents
Sricharan Rdacbb352017-08-24 12:51:39 +0530145 * @riids: idr of all remote intents
Sricharan R1d2ea362017-08-24 12:51:37 +0530146 * @intent_work: worker responsible for transmitting rx_done packets
147 * @done_intents: list of intents that needs to be announced rx_done
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530148 * @buf: receive buffer, for gathering fragments
149 * @buf_offset: write offset in @buf
150 * @buf_size: size of current @buf
151 * @open_ack: completed once remote has acked the open-request
152 * @open_req: completed once open-request has been received
153 */
154struct glink_channel {
155 struct rpmsg_endpoint ept;
156
157 struct rpmsg_device *rpdev;
158 struct qcom_glink *glink;
159
160 struct kref refcount;
161
162 spinlock_t recv_lock;
163
164 char *name;
165 unsigned int lcid;
166 unsigned int rcid;
167
Sricharan R933b45d2017-08-24 12:51:34 +0530168 spinlock_t intent_lock;
169 struct idr liids;
Sricharan Rdacbb352017-08-24 12:51:39 +0530170 struct idr riids;
Sricharan R1d2ea362017-08-24 12:51:37 +0530171 struct work_struct intent_work;
172 struct list_head done_intents;
Sricharan R933b45d2017-08-24 12:51:34 +0530173
Sricharan R64f95f82017-08-24 12:51:35 +0530174 struct glink_core_rx_intent *buf;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530175 int buf_offset;
176 int buf_size;
177
178 struct completion open_ack;
179 struct completion open_req;
180};
181
182#define to_glink_channel(_ept) container_of(_ept, struct glink_channel, ept)
183
184static const struct rpmsg_endpoint_ops glink_endpoint_ops;
185
186#define RPM_CMD_VERSION 0
187#define RPM_CMD_VERSION_ACK 1
188#define RPM_CMD_OPEN 2
189#define RPM_CMD_CLOSE 3
190#define RPM_CMD_OPEN_ACK 4
Sricharan R933b45d2017-08-24 12:51:34 +0530191#define RPM_CMD_INTENT 5
Sricharan R1d2ea362017-08-24 12:51:37 +0530192#define RPM_CMD_RX_DONE 6
Sricharan R933b45d2017-08-24 12:51:34 +0530193#define RPM_CMD_RX_INTENT_REQ 7
194#define RPM_CMD_RX_INTENT_REQ_ACK 8
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530195#define RPM_CMD_TX_DATA 9
196#define RPM_CMD_CLOSE_ACK 11
197#define RPM_CMD_TX_DATA_CONT 12
198#define RPM_CMD_READ_NOTIF 13
Sricharan R1d2ea362017-08-24 12:51:37 +0530199#define RPM_CMD_RX_DONE_W_REUSE 14
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530200
201#define GLINK_FEATURE_INTENTLESS BIT(1)
202
Sricharan R1d2ea362017-08-24 12:51:37 +0530203static void qcom_glink_rx_done_work(struct work_struct *work);
204
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530205static struct glink_channel *qcom_glink_alloc_channel(struct qcom_glink *glink,
206 const char *name)
207{
208 struct glink_channel *channel;
209
210 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
211 if (!channel)
212 return ERR_PTR(-ENOMEM);
213
214 /* Setup glink internal glink_channel data */
215 spin_lock_init(&channel->recv_lock);
Sricharan R933b45d2017-08-24 12:51:34 +0530216 spin_lock_init(&channel->intent_lock);
Sricharan R1d2ea362017-08-24 12:51:37 +0530217
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530218 channel->glink = glink;
219 channel->name = kstrdup(name, GFP_KERNEL);
220
221 init_completion(&channel->open_req);
222 init_completion(&channel->open_ack);
223
Sricharan R1d2ea362017-08-24 12:51:37 +0530224 INIT_LIST_HEAD(&channel->done_intents);
225 INIT_WORK(&channel->intent_work, qcom_glink_rx_done_work);
226
Sricharan R933b45d2017-08-24 12:51:34 +0530227 idr_init(&channel->liids);
Sricharan Rdacbb352017-08-24 12:51:39 +0530228 idr_init(&channel->riids);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530229 kref_init(&channel->refcount);
230
231 return channel;
232}
233
234static void qcom_glink_channel_release(struct kref *ref)
235{
236 struct glink_channel *channel = container_of(ref, struct glink_channel,
237 refcount);
Sricharan R933b45d2017-08-24 12:51:34 +0530238 unsigned long flags;
239
240 spin_lock_irqsave(&channel->intent_lock, flags);
241 idr_destroy(&channel->liids);
Sricharan Rdacbb352017-08-24 12:51:39 +0530242 idr_destroy(&channel->riids);
Sricharan R933b45d2017-08-24 12:51:34 +0530243 spin_unlock_irqrestore(&channel->intent_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530244
245 kfree(channel->name);
246 kfree(channel);
247}
248
249static size_t qcom_glink_rx_avail(struct qcom_glink *glink)
250{
251 return glink->rx_pipe->avail(glink->rx_pipe);
252}
253
254static void qcom_glink_rx_peak(struct qcom_glink *glink,
Bjorn Anderssonb88eee92017-08-24 12:51:36 +0530255 void *data, unsigned int offset, size_t count)
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530256{
Bjorn Anderssonb88eee92017-08-24 12:51:36 +0530257 glink->rx_pipe->peak(glink->rx_pipe, data, offset, count);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530258}
259
260static void qcom_glink_rx_advance(struct qcom_glink *glink, size_t count)
261{
262 glink->rx_pipe->advance(glink->rx_pipe, count);
263}
264
265static size_t qcom_glink_tx_avail(struct qcom_glink *glink)
266{
267 return glink->tx_pipe->avail(glink->tx_pipe);
268}
269
270static void qcom_glink_tx_write(struct qcom_glink *glink,
271 const void *hdr, size_t hlen,
272 const void *data, size_t dlen)
273{
274 glink->tx_pipe->write(glink->tx_pipe, hdr, hlen, data, dlen);
275}
276
277static int qcom_glink_tx(struct qcom_glink *glink,
278 const void *hdr, size_t hlen,
279 const void *data, size_t dlen, bool wait)
280{
281 unsigned int tlen = hlen + dlen;
282 int ret;
283
284 /* Reject packets that are too big */
285 if (tlen >= glink->tx_pipe->length)
286 return -EINVAL;
287
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530288 ret = mutex_lock_interruptible(&glink->tx_lock);
289 if (ret)
290 return ret;
291
292 while (qcom_glink_tx_avail(glink) < tlen) {
293 if (!wait) {
Sricharan Ra7df9df2017-08-24 12:51:28 +0530294 ret = -EAGAIN;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530295 goto out;
296 }
297
298 usleep_range(10000, 15000);
299 }
300
301 qcom_glink_tx_write(glink, hdr, hlen, data, dlen);
302
303 mbox_send_message(glink->mbox_chan, NULL);
304 mbox_client_txdone(glink->mbox_chan, 0);
305
306out:
307 mutex_unlock(&glink->tx_lock);
308
309 return ret;
310}
311
312static int qcom_glink_send_version(struct qcom_glink *glink)
313{
314 struct glink_msg msg;
315
316 msg.cmd = cpu_to_le16(RPM_CMD_VERSION);
Sricharan Rd31ad612017-08-24 12:51:32 +0530317 msg.param1 = cpu_to_le16(GLINK_VERSION_1);
318 msg.param2 = cpu_to_le32(glink->features);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530319
320 return qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
321}
322
323static void qcom_glink_send_version_ack(struct qcom_glink *glink)
324{
325 struct glink_msg msg;
326
327 msg.cmd = cpu_to_le16(RPM_CMD_VERSION_ACK);
Sricharan Rd31ad612017-08-24 12:51:32 +0530328 msg.param1 = cpu_to_le16(GLINK_VERSION_1);
329 msg.param2 = cpu_to_le32(glink->features);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530330
331 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
332}
333
334static void qcom_glink_send_open_ack(struct qcom_glink *glink,
335 struct glink_channel *channel)
336{
337 struct glink_msg msg;
338
339 msg.cmd = cpu_to_le16(RPM_CMD_OPEN_ACK);
340 msg.param1 = cpu_to_le16(channel->rcid);
341 msg.param2 = cpu_to_le32(0);
342
343 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
344}
345
346/**
347 * qcom_glink_send_open_req() - send a RPM_CMD_OPEN request to the remote
348 * @glink: Ptr to the glink edge
349 * @channel: Ptr to the channel that the open req is sent
350 *
351 * Allocates a local channel id and sends a RPM_CMD_OPEN message to the remote.
352 * Will return with refcount held, regardless of outcome.
353 *
354 * Returns 0 on success, negative errno otherwise.
355 */
356static int qcom_glink_send_open_req(struct qcom_glink *glink,
357 struct glink_channel *channel)
358{
359 struct {
360 struct glink_msg msg;
361 u8 name[GLINK_NAME_SIZE];
362 } __packed req;
363 int name_len = strlen(channel->name) + 1;
364 int req_len = ALIGN(sizeof(req.msg) + name_len, 8);
365 int ret;
Sricharan R44f6df92017-08-24 12:51:33 +0530366 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530367
368 kref_get(&channel->refcount);
369
Sricharan R44f6df92017-08-24 12:51:33 +0530370 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530371 ret = idr_alloc_cyclic(&glink->lcids, channel,
372 RPM_GLINK_CID_MIN, RPM_GLINK_CID_MAX,
Sricharan R44f6df92017-08-24 12:51:33 +0530373 GFP_ATOMIC);
374 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530375 if (ret < 0)
376 return ret;
377
378 channel->lcid = ret;
379
380 req.msg.cmd = cpu_to_le16(RPM_CMD_OPEN);
381 req.msg.param1 = cpu_to_le16(channel->lcid);
382 req.msg.param2 = cpu_to_le32(name_len);
383 strcpy(req.name, channel->name);
384
385 ret = qcom_glink_tx(glink, &req, req_len, NULL, 0, true);
386 if (ret)
387 goto remove_idr;
388
389 return 0;
390
391remove_idr:
Sricharan R44f6df92017-08-24 12:51:33 +0530392 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530393 idr_remove(&glink->lcids, channel->lcid);
394 channel->lcid = 0;
Sricharan R44f6df92017-08-24 12:51:33 +0530395 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530396
397 return ret;
398}
399
400static void qcom_glink_send_close_req(struct qcom_glink *glink,
401 struct glink_channel *channel)
402{
403 struct glink_msg req;
404
405 req.cmd = cpu_to_le16(RPM_CMD_CLOSE);
406 req.param1 = cpu_to_le16(channel->lcid);
407 req.param2 = 0;
408
409 qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
410}
411
412static void qcom_glink_send_close_ack(struct qcom_glink *glink,
413 unsigned int rcid)
414{
415 struct glink_msg req;
416
417 req.cmd = cpu_to_le16(RPM_CMD_CLOSE_ACK);
418 req.param1 = cpu_to_le16(rcid);
419 req.param2 = 0;
420
421 qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
422}
423
Sricharan R1d2ea362017-08-24 12:51:37 +0530424static void qcom_glink_rx_done_work(struct work_struct *work)
425{
426 struct glink_channel *channel = container_of(work, struct glink_channel,
427 intent_work);
428 struct qcom_glink *glink = channel->glink;
429 struct glink_core_rx_intent *intent, *tmp;
430 struct {
431 u16 id;
432 u16 lcid;
433 u32 liid;
434 } __packed cmd;
435
436 unsigned int cid = channel->lcid;
437 unsigned int iid;
438 bool reuse;
439 unsigned long flags;
440
441 spin_lock_irqsave(&channel->intent_lock, flags);
442 list_for_each_entry_safe(intent, tmp, &channel->done_intents, node) {
443 list_del(&intent->node);
444 spin_unlock_irqrestore(&channel->intent_lock, flags);
445 iid = intent->id;
446 reuse = intent->reuse;
447
448 cmd.id = reuse ? RPM_CMD_RX_DONE_W_REUSE : RPM_CMD_RX_DONE;
449 cmd.lcid = cid;
450 cmd.liid = iid;
451
452 qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
453 if (!reuse) {
454 kfree(intent->data);
455 kfree(intent);
456 }
457 spin_lock_irqsave(&channel->intent_lock, flags);
458 }
459 spin_unlock_irqrestore(&channel->intent_lock, flags);
460}
461
462static void qcom_glink_rx_done(struct qcom_glink *glink,
463 struct glink_channel *channel,
464 struct glink_core_rx_intent *intent)
465{
466 /* We don't send RX_DONE to intentless systems */
467 if (glink->intentless) {
468 kfree(intent->data);
469 kfree(intent);
470 return;
471 }
472
473 /* Take it off the tree of receive intents */
474 if (!intent->reuse) {
475 spin_lock(&channel->intent_lock);
476 idr_remove(&channel->liids, intent->id);
477 spin_unlock(&channel->intent_lock);
478 }
479
480 /* Schedule the sending of a rx_done indication */
481 spin_lock(&channel->intent_lock);
482 list_add_tail(&intent->node, &channel->done_intents);
483 spin_unlock(&channel->intent_lock);
484
485 schedule_work(&channel->intent_work);
486}
487
Sricharan Rd31ad612017-08-24 12:51:32 +0530488/**
489 * qcom_glink_receive_version() - receive version/features from remote system
490 *
491 * @glink: pointer to transport interface
492 * @r_version: remote version
493 * @r_features: remote features
494 *
495 * This function is called in response to a remote-initiated version/feature
496 * negotiation sequence.
497 */
498static void qcom_glink_receive_version(struct qcom_glink *glink,
499 u32 version,
500 u32 features)
501{
502 switch (version) {
503 case 0:
504 break;
505 case GLINK_VERSION_1:
506 glink->features &= features;
507 /* FALLTHROUGH */
508 default:
509 qcom_glink_send_version_ack(glink);
510 break;
511 }
512}
513
514/**
515 * qcom_glink_receive_version_ack() - receive negotiation ack from remote system
516 *
517 * @glink: pointer to transport interface
518 * @r_version: remote version response
519 * @r_features: remote features response
520 *
521 * This function is called in response to a local-initiated version/feature
522 * negotiation sequence and is the counter-offer from the remote side based
523 * upon the initial version and feature set requested.
524 */
525static void qcom_glink_receive_version_ack(struct qcom_glink *glink,
526 u32 version,
527 u32 features)
528{
529 switch (version) {
530 case 0:
531 /* Version negotiation failed */
532 break;
533 case GLINK_VERSION_1:
534 if (features == glink->features)
535 break;
536
537 glink->features &= features;
538 /* FALLTHROUGH */
539 default:
540 qcom_glink_send_version(glink);
541 break;
542 }
543}
544
Sricharan R933b45d2017-08-24 12:51:34 +0530545/**
546 * qcom_glink_send_intent_req_ack() - convert an rx intent request ack cmd to
547 wire format and transmit
548 * @glink: The transport to transmit on.
549 * @channel: The glink channel
550 * @granted: The request response to encode.
551 *
552 * Return: 0 on success or standard Linux error code.
553 */
554static int qcom_glink_send_intent_req_ack(struct qcom_glink *glink,
555 struct glink_channel *channel,
556 bool granted)
557{
558 struct glink_msg msg;
559
560 msg.cmd = cpu_to_le16(RPM_CMD_RX_INTENT_REQ_ACK);
561 msg.param1 = cpu_to_le16(channel->lcid);
562 msg.param2 = cpu_to_le32(granted);
563
564 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
565
566 return 0;
567}
568
569/**
570 * qcom_glink_advertise_intent - convert an rx intent cmd to wire format and
571 * transmit
572 * @glink: The transport to transmit on.
573 * @channel: The local channel
574 * @size: The intent to pass on to remote.
575 *
576 * Return: 0 on success or standard Linux error code.
577 */
578static int qcom_glink_advertise_intent(struct qcom_glink *glink,
579 struct glink_channel *channel,
580 struct glink_core_rx_intent *intent)
581{
582 struct command {
583 u16 id;
584 u16 lcid;
585 u32 count;
586 u32 size;
587 u32 liid;
588 } __packed;
589 struct command cmd;
590
591 cmd.id = cpu_to_le16(RPM_CMD_INTENT);
592 cmd.lcid = cpu_to_le16(channel->lcid);
593 cmd.count = cpu_to_le32(1);
594 cmd.size = cpu_to_le32(intent->size);
595 cmd.liid = cpu_to_le32(intent->id);
596
597 qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
598
599 return 0;
600}
601
602static struct glink_core_rx_intent *
603qcom_glink_alloc_intent(struct qcom_glink *glink,
604 struct glink_channel *channel,
605 size_t size,
606 bool reuseable)
607{
608 struct glink_core_rx_intent *intent;
609 int ret;
610 unsigned long flags;
611
612 intent = kzalloc(sizeof(*intent), GFP_KERNEL);
613
614 if (!intent)
615 return NULL;
616
617 intent->data = kzalloc(size, GFP_KERNEL);
618 if (!intent->data)
619 return NULL;
620
621 spin_lock_irqsave(&channel->intent_lock, flags);
622 ret = idr_alloc_cyclic(&channel->liids, intent, 1, -1, GFP_ATOMIC);
623 if (ret < 0) {
624 spin_unlock_irqrestore(&channel->intent_lock, flags);
625 return NULL;
626 }
627 spin_unlock_irqrestore(&channel->intent_lock, flags);
628
629 intent->id = ret;
630 intent->size = size;
631 intent->reuse = reuseable;
632
633 return intent;
634}
635
636/**
637 * qcom_glink_handle_intent_req() - Receive a request for rx_intent
638 * from remote side
639 * if_ptr: Pointer to the transport interface
640 * rcid: Remote channel ID
641 * size: size of the intent
642 *
643 * The function searches for the local channel to which the request for
644 * rx_intent has arrived and allocates and notifies the remote back
645 */
646static void qcom_glink_handle_intent_req(struct qcom_glink *glink,
647 u32 cid, size_t size)
648{
649 struct glink_core_rx_intent *intent;
650 struct glink_channel *channel;
651 unsigned long flags;
652
653 spin_lock_irqsave(&glink->idr_lock, flags);
654 channel = idr_find(&glink->rcids, cid);
655 spin_unlock_irqrestore(&glink->idr_lock, flags);
656
657 if (!channel) {
658 pr_err("%s channel not found for cid %d\n", __func__, cid);
659 return;
660 }
661
662 intent = qcom_glink_alloc_intent(glink, channel, size, false);
663 if (intent)
664 qcom_glink_advertise_intent(glink, channel, intent);
665
666 qcom_glink_send_intent_req_ack(glink, channel, !!intent);
667}
668
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530669static int qcom_glink_rx_defer(struct qcom_glink *glink, size_t extra)
670{
671 struct glink_defer_cmd *dcmd;
672
673 extra = ALIGN(extra, 8);
674
675 if (qcom_glink_rx_avail(glink) < sizeof(struct glink_msg) + extra) {
676 dev_dbg(glink->dev, "Insufficient data in rx fifo");
677 return -ENXIO;
678 }
679
680 dcmd = kzalloc(sizeof(*dcmd) + extra, GFP_ATOMIC);
681 if (!dcmd)
682 return -ENOMEM;
683
684 INIT_LIST_HEAD(&dcmd->node);
685
Bjorn Anderssonb88eee92017-08-24 12:51:36 +0530686 qcom_glink_rx_peak(glink, &dcmd->msg, 0, sizeof(dcmd->msg) + extra);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530687
688 spin_lock(&glink->rx_lock);
689 list_add_tail(&dcmd->node, &glink->rx_queue);
690 spin_unlock(&glink->rx_lock);
691
692 schedule_work(&glink->rx_work);
693 qcom_glink_rx_advance(glink, sizeof(dcmd->msg) + extra);
694
695 return 0;
696}
697
698static int qcom_glink_rx_data(struct qcom_glink *glink, size_t avail)
699{
Sricharan R64f95f82017-08-24 12:51:35 +0530700 struct glink_core_rx_intent *intent;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530701 struct glink_channel *channel;
702 struct {
703 struct glink_msg msg;
704 __le32 chunk_size;
705 __le32 left_size;
706 } __packed hdr;
707 unsigned int chunk_size;
708 unsigned int left_size;
709 unsigned int rcid;
Sricharan R64f95f82017-08-24 12:51:35 +0530710 unsigned int liid;
711 int ret = 0;
Sricharan R44f6df92017-08-24 12:51:33 +0530712 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530713
714 if (avail < sizeof(hdr)) {
715 dev_dbg(glink->dev, "Not enough data in fifo\n");
716 return -EAGAIN;
717 }
718
Bjorn Anderssonb88eee92017-08-24 12:51:36 +0530719 qcom_glink_rx_peak(glink, &hdr, 0, sizeof(hdr));
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530720 chunk_size = le32_to_cpu(hdr.chunk_size);
721 left_size = le32_to_cpu(hdr.left_size);
722
723 if (avail < sizeof(hdr) + chunk_size) {
724 dev_dbg(glink->dev, "Payload not yet in fifo\n");
725 return -EAGAIN;
726 }
727
728 if (WARN(chunk_size % 4, "Incoming data must be word aligned\n"))
729 return -EINVAL;
730
731 rcid = le16_to_cpu(hdr.msg.param1);
Sricharan R44f6df92017-08-24 12:51:33 +0530732 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530733 channel = idr_find(&glink->rcids, rcid);
Sricharan R44f6df92017-08-24 12:51:33 +0530734 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530735 if (!channel) {
736 dev_dbg(glink->dev, "Data on non-existing channel\n");
737
738 /* Drop the message */
Sricharan R64f95f82017-08-24 12:51:35 +0530739 goto advance_rx;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530740 }
741
Sricharan R64f95f82017-08-24 12:51:35 +0530742 if (glink->intentless) {
743 /* Might have an ongoing, fragmented, message to append */
744 if (!channel->buf) {
745 intent = kzalloc(sizeof(*intent), GFP_ATOMIC);
746 if (!intent)
747 return -ENOMEM;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530748
Sricharan R64f95f82017-08-24 12:51:35 +0530749 intent->data = kmalloc(chunk_size + left_size,
750 GFP_ATOMIC);
751 if (!intent->data) {
752 kfree(intent);
753 return -ENOMEM;
754 }
755
756 intent->id = 0xbabababa;
757 intent->size = chunk_size + left_size;
758 intent->offset = 0;
759
760 channel->buf = intent;
761 } else {
762 intent = channel->buf;
763 }
764 } else {
765 liid = le32_to_cpu(hdr.msg.param2);
766
767 spin_lock_irqsave(&channel->intent_lock, flags);
768 intent = idr_find(&channel->liids, liid);
769 spin_unlock_irqrestore(&channel->intent_lock, flags);
770
771 if (!intent) {
772 dev_err(glink->dev,
773 "no intent found for channel %s intent %d",
774 channel->name, liid);
775 goto advance_rx;
776 }
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530777 }
778
Sricharan R64f95f82017-08-24 12:51:35 +0530779 if (intent->size - intent->offset < chunk_size) {
780 dev_err(glink->dev, "Insufficient space in intent\n");
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530781
782 /* The packet header lied, drop payload */
Sricharan R64f95f82017-08-24 12:51:35 +0530783 goto advance_rx;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530784 }
785
Sricharan R64f95f82017-08-24 12:51:35 +0530786 qcom_glink_rx_peak(glink, intent->data + intent->offset,
Bjorn Anderssonb88eee92017-08-24 12:51:36 +0530787 sizeof(hdr), chunk_size);
Sricharan R64f95f82017-08-24 12:51:35 +0530788 intent->offset += chunk_size;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530789
790 /* Handle message when no fragments remain to be received */
791 if (!left_size) {
792 spin_lock(&channel->recv_lock);
793 if (channel->ept.cb) {
794 channel->ept.cb(channel->ept.rpdev,
Sricharan R64f95f82017-08-24 12:51:35 +0530795 intent->data,
796 intent->offset,
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530797 channel->ept.priv,
798 RPMSG_ADDR_ANY);
799 }
800 spin_unlock(&channel->recv_lock);
801
Sricharan R64f95f82017-08-24 12:51:35 +0530802 intent->offset = 0;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530803 channel->buf = NULL;
Sricharan R1d2ea362017-08-24 12:51:37 +0530804
805 qcom_glink_rx_done(glink, channel, intent);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530806 }
807
Sricharan R64f95f82017-08-24 12:51:35 +0530808advance_rx:
Bjorn Anderssonb88eee92017-08-24 12:51:36 +0530809 qcom_glink_rx_advance(glink, ALIGN(sizeof(hdr) + chunk_size, 8));
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530810
Sricharan R64f95f82017-08-24 12:51:35 +0530811 return ret;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530812}
813
Sricharan Rdacbb352017-08-24 12:51:39 +0530814static void qcom_glink_handle_intent(struct qcom_glink *glink,
815 unsigned int cid,
816 unsigned int count,
817 size_t avail)
818{
819 struct glink_core_rx_intent *intent;
820 struct glink_channel *channel;
821 struct intent_pair {
822 __le32 size;
823 __le32 iid;
824 };
825
826 struct {
827 struct glink_msg msg;
828 struct intent_pair intents[];
829 } __packed * msg;
830
831 const size_t msglen = sizeof(*msg) + sizeof(struct intent_pair) * count;
832 int ret;
833 int i;
834 unsigned long flags;
835
836 if (avail < msglen) {
837 dev_dbg(glink->dev, "Not enough data in fifo\n");
838 return;
839 }
840
841 spin_lock_irqsave(&glink->idr_lock, flags);
842 channel = idr_find(&glink->rcids, cid);
843 spin_unlock_irqrestore(&glink->idr_lock, flags);
844 if (!channel) {
845 dev_err(glink->dev, "intents for non-existing channel\n");
846 return;
847 }
848
849 msg = kmalloc(msglen, GFP_ATOMIC);
850 if (!msg)
851 return;
852
853 qcom_glink_rx_peak(glink, msg, 0, msglen);
854
855 for (i = 0; i < count; ++i) {
856 intent = kzalloc(sizeof(*intent), GFP_ATOMIC);
857 if (!intent)
858 break;
859
860 intent->id = le32_to_cpu(msg->intents[i].iid);
861 intent->size = le32_to_cpu(msg->intents[i].size);
862
863 spin_lock_irqsave(&channel->intent_lock, flags);
864 ret = idr_alloc(&channel->riids, intent,
865 intent->id, intent->id + 1, GFP_ATOMIC);
866 spin_unlock_irqrestore(&channel->intent_lock, flags);
867
868 if (ret < 0)
869 dev_err(glink->dev, "failed to store remote intent\n");
870 }
871
872 kfree(msg);
873 qcom_glink_rx_advance(glink, ALIGN(msglen, 8));
874}
875
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530876static int qcom_glink_rx_open_ack(struct qcom_glink *glink, unsigned int lcid)
877{
878 struct glink_channel *channel;
879
Sricharan R44f6df92017-08-24 12:51:33 +0530880 spin_lock(&glink->idr_lock);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530881 channel = idr_find(&glink->lcids, lcid);
882 if (!channel) {
883 dev_err(glink->dev, "Invalid open ack packet\n");
884 return -EINVAL;
885 }
Sricharan R44f6df92017-08-24 12:51:33 +0530886 spin_unlock(&glink->idr_lock);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530887
888 complete(&channel->open_ack);
889
890 return 0;
891}
892
893static irqreturn_t qcom_glink_native_intr(int irq, void *data)
894{
895 struct qcom_glink *glink = data;
896 struct glink_msg msg;
897 unsigned int param1;
898 unsigned int param2;
899 unsigned int avail;
900 unsigned int cmd;
901 int ret;
902
903 for (;;) {
904 avail = qcom_glink_rx_avail(glink);
905 if (avail < sizeof(msg))
906 break;
907
Bjorn Anderssonb88eee92017-08-24 12:51:36 +0530908 qcom_glink_rx_peak(glink, &msg, 0, sizeof(msg));
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530909
910 cmd = le16_to_cpu(msg.cmd);
911 param1 = le16_to_cpu(msg.param1);
912 param2 = le32_to_cpu(msg.param2);
913
914 switch (cmd) {
915 case RPM_CMD_VERSION:
916 case RPM_CMD_VERSION_ACK:
917 case RPM_CMD_CLOSE:
918 case RPM_CMD_CLOSE_ACK:
Sricharan R933b45d2017-08-24 12:51:34 +0530919 case RPM_CMD_RX_INTENT_REQ:
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530920 ret = qcom_glink_rx_defer(glink, 0);
921 break;
922 case RPM_CMD_OPEN_ACK:
923 ret = qcom_glink_rx_open_ack(glink, param1);
924 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
925 break;
926 case RPM_CMD_OPEN:
927 ret = qcom_glink_rx_defer(glink, param2);
928 break;
929 case RPM_CMD_TX_DATA:
930 case RPM_CMD_TX_DATA_CONT:
931 ret = qcom_glink_rx_data(glink, avail);
932 break;
933 case RPM_CMD_READ_NOTIF:
934 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
935
936 mbox_send_message(glink->mbox_chan, NULL);
937 mbox_client_txdone(glink->mbox_chan, 0);
938
939 ret = 0;
940 break;
Sricharan Rdacbb352017-08-24 12:51:39 +0530941 case RPM_CMD_INTENT:
942 qcom_glink_handle_intent(glink, param1, param2, avail);
943 break;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530944 default:
945 dev_err(glink->dev, "unhandled rx cmd: %d\n", cmd);
946 ret = -EINVAL;
947 break;
948 }
949
950 if (ret)
951 break;
952 }
953
954 return IRQ_HANDLED;
955}
956
957/* Locally initiated rpmsg_create_ept */
958static struct glink_channel *qcom_glink_create_local(struct qcom_glink *glink,
959 const char *name)
960{
961 struct glink_channel *channel;
962 int ret;
Sricharan R44f6df92017-08-24 12:51:33 +0530963 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530964
965 channel = qcom_glink_alloc_channel(glink, name);
966 if (IS_ERR(channel))
967 return ERR_CAST(channel);
968
969 ret = qcom_glink_send_open_req(glink, channel);
970 if (ret)
971 goto release_channel;
972
973 ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
974 if (!ret)
975 goto err_timeout;
976
977 ret = wait_for_completion_timeout(&channel->open_req, 5 * HZ);
978 if (!ret)
979 goto err_timeout;
980
981 qcom_glink_send_open_ack(glink, channel);
982
983 return channel;
984
985err_timeout:
986 /* qcom_glink_send_open_req() did register the channel in lcids*/
Sricharan R44f6df92017-08-24 12:51:33 +0530987 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530988 idr_remove(&glink->lcids, channel->lcid);
Sricharan R44f6df92017-08-24 12:51:33 +0530989 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530990
991release_channel:
992 /* Release qcom_glink_send_open_req() reference */
993 kref_put(&channel->refcount, qcom_glink_channel_release);
994 /* Release qcom_glink_alloc_channel() reference */
995 kref_put(&channel->refcount, qcom_glink_channel_release);
996
997 return ERR_PTR(-ETIMEDOUT);
998}
999
1000/* Remote initiated rpmsg_create_ept */
1001static int qcom_glink_create_remote(struct qcom_glink *glink,
1002 struct glink_channel *channel)
1003{
1004 int ret;
1005
1006 qcom_glink_send_open_ack(glink, channel);
1007
1008 ret = qcom_glink_send_open_req(glink, channel);
1009 if (ret)
1010 goto close_link;
1011
1012 ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
1013 if (!ret) {
1014 ret = -ETIMEDOUT;
1015 goto close_link;
1016 }
1017
1018 return 0;
1019
1020close_link:
1021 /*
1022 * Send a close request to "undo" our open-ack. The close-ack will
1023 * release the last reference.
1024 */
1025 qcom_glink_send_close_req(glink, channel);
1026
1027 /* Release qcom_glink_send_open_req() reference */
1028 kref_put(&channel->refcount, qcom_glink_channel_release);
1029
1030 return ret;
1031}
1032
1033static struct rpmsg_endpoint *qcom_glink_create_ept(struct rpmsg_device *rpdev,
1034 rpmsg_rx_cb_t cb,
1035 void *priv,
1036 struct rpmsg_channel_info
1037 chinfo)
1038{
1039 struct glink_channel *parent = to_glink_channel(rpdev->ept);
1040 struct glink_channel *channel;
1041 struct qcom_glink *glink = parent->glink;
1042 struct rpmsg_endpoint *ept;
1043 const char *name = chinfo.name;
1044 int cid;
1045 int ret;
Sricharan R44f6df92017-08-24 12:51:33 +05301046 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301047
Sricharan R44f6df92017-08-24 12:51:33 +05301048 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301049 idr_for_each_entry(&glink->rcids, channel, cid) {
1050 if (!strcmp(channel->name, name))
1051 break;
1052 }
Sricharan R44f6df92017-08-24 12:51:33 +05301053 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301054
1055 if (!channel) {
1056 channel = qcom_glink_create_local(glink, name);
1057 if (IS_ERR(channel))
1058 return NULL;
1059 } else {
1060 ret = qcom_glink_create_remote(glink, channel);
1061 if (ret)
1062 return NULL;
1063 }
1064
1065 ept = &channel->ept;
1066 ept->rpdev = rpdev;
1067 ept->cb = cb;
1068 ept->priv = priv;
1069 ept->ops = &glink_endpoint_ops;
1070
1071 return ept;
1072}
1073
Sricharan R6bf68cb2017-08-24 12:51:38 +05301074static int qcom_glink_announce_create(struct rpmsg_device *rpdev)
1075{
1076 struct glink_channel *channel = to_glink_channel(rpdev->ept);
1077 struct glink_core_rx_intent *intent;
1078 struct qcom_glink *glink = channel->glink;
1079 int num_intents = glink->intentless ? 0 : 5;
1080
1081 /* Channel is now open, advertise base set of intents */
1082 while (num_intents--) {
1083 intent = qcom_glink_alloc_intent(glink, channel, SZ_1K, true);
1084 if (!intent)
1085 break;
1086
1087 qcom_glink_advertise_intent(glink, channel, intent);
1088 }
1089
1090 return 0;
1091}
1092
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301093static void qcom_glink_destroy_ept(struct rpmsg_endpoint *ept)
1094{
1095 struct glink_channel *channel = to_glink_channel(ept);
1096 struct qcom_glink *glink = channel->glink;
1097 unsigned long flags;
1098
1099 spin_lock_irqsave(&channel->recv_lock, flags);
1100 channel->ept.cb = NULL;
1101 spin_unlock_irqrestore(&channel->recv_lock, flags);
1102
1103 /* Decouple the potential rpdev from the channel */
1104 channel->rpdev = NULL;
1105
1106 qcom_glink_send_close_req(glink, channel);
1107}
1108
1109static int __qcom_glink_send(struct glink_channel *channel,
1110 void *data, int len, bool wait)
1111{
1112 struct qcom_glink *glink = channel->glink;
Sricharan R11cb45a2017-08-24 12:51:40 +05301113 struct glink_core_rx_intent *intent = NULL;
1114 struct glink_core_rx_intent *tmp;
1115 int iid = 0;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301116 struct {
1117 struct glink_msg msg;
1118 __le32 chunk_size;
1119 __le32 left_size;
1120 } __packed req;
Sricharan R11cb45a2017-08-24 12:51:40 +05301121 int ret;
1122 unsigned long flags;
1123
1124 if (!glink->intentless) {
1125 if (!intent) {
1126 spin_lock_irqsave(&channel->intent_lock, flags);
1127 idr_for_each_entry(&channel->riids, tmp, iid) {
1128 if (tmp->size >= len && !tmp->in_use) {
1129 tmp->in_use = true;
1130 intent = tmp;
1131 break;
1132 }
1133 }
1134 spin_unlock_irqrestore(&channel->intent_lock, flags);
1135
1136 /* We found an available intent */
1137 if (!intent)
1138 return -EBUSY;
1139 }
1140
1141 iid = intent->id;
1142 }
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301143
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301144 req.msg.cmd = cpu_to_le16(RPM_CMD_TX_DATA);
1145 req.msg.param1 = cpu_to_le16(channel->lcid);
Sricharan R11cb45a2017-08-24 12:51:40 +05301146 req.msg.param2 = cpu_to_le32(iid);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301147 req.chunk_size = cpu_to_le32(len);
1148 req.left_size = cpu_to_le32(0);
1149
Sricharan R11cb45a2017-08-24 12:51:40 +05301150 ret = qcom_glink_tx(glink, &req, sizeof(req), data, len, wait);
1151
1152 /* Mark intent available if we failed */
1153 if (ret)
1154 intent->in_use = false;
1155
1156 return ret;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301157}
1158
1159static int qcom_glink_send(struct rpmsg_endpoint *ept, void *data, int len)
1160{
1161 struct glink_channel *channel = to_glink_channel(ept);
1162
1163 return __qcom_glink_send(channel, data, len, true);
1164}
1165
1166static int qcom_glink_trysend(struct rpmsg_endpoint *ept, void *data, int len)
1167{
1168 struct glink_channel *channel = to_glink_channel(ept);
1169
1170 return __qcom_glink_send(channel, data, len, false);
1171}
1172
1173/*
1174 * Finds the device_node for the glink child interested in this channel.
1175 */
1176static struct device_node *qcom_glink_match_channel(struct device_node *node,
1177 const char *channel)
1178{
1179 struct device_node *child;
1180 const char *name;
1181 const char *key;
1182 int ret;
1183
1184 for_each_available_child_of_node(node, child) {
1185 key = "qcom,glink-channels";
1186 ret = of_property_read_string(child, key, &name);
1187 if (ret)
1188 continue;
1189
1190 if (strcmp(name, channel) == 0)
1191 return child;
1192 }
1193
1194 return NULL;
1195}
1196
1197static const struct rpmsg_device_ops glink_device_ops = {
1198 .create_ept = qcom_glink_create_ept,
Sricharan R6bf68cb2017-08-24 12:51:38 +05301199 .announce_create = qcom_glink_announce_create,
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301200};
1201
1202static const struct rpmsg_endpoint_ops glink_endpoint_ops = {
1203 .destroy_ept = qcom_glink_destroy_ept,
1204 .send = qcom_glink_send,
1205 .trysend = qcom_glink_trysend,
1206};
1207
1208static void qcom_glink_rpdev_release(struct device *dev)
1209{
1210 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
1211 struct glink_channel *channel = to_glink_channel(rpdev->ept);
1212
1213 channel->rpdev = NULL;
1214 kfree(rpdev);
1215}
1216
1217static int qcom_glink_rx_open(struct qcom_glink *glink, unsigned int rcid,
1218 char *name)
1219{
1220 struct glink_channel *channel;
1221 struct rpmsg_device *rpdev;
1222 bool create_device = false;
1223 struct device_node *node;
1224 int lcid;
1225 int ret;
Sricharan R44f6df92017-08-24 12:51:33 +05301226 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301227
Sricharan R44f6df92017-08-24 12:51:33 +05301228 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301229 idr_for_each_entry(&glink->lcids, channel, lcid) {
1230 if (!strcmp(channel->name, name))
1231 break;
1232 }
Sricharan R44f6df92017-08-24 12:51:33 +05301233 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301234
1235 if (!channel) {
1236 channel = qcom_glink_alloc_channel(glink, name);
1237 if (IS_ERR(channel))
1238 return PTR_ERR(channel);
1239
1240 /* The opening dance was initiated by the remote */
1241 create_device = true;
1242 }
1243
Sricharan R44f6df92017-08-24 12:51:33 +05301244 spin_lock_irqsave(&glink->idr_lock, flags);
1245 ret = idr_alloc(&glink->rcids, channel, rcid, rcid + 1, GFP_ATOMIC);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301246 if (ret < 0) {
1247 dev_err(glink->dev, "Unable to insert channel into rcid list\n");
Sricharan R44f6df92017-08-24 12:51:33 +05301248 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301249 goto free_channel;
1250 }
1251 channel->rcid = ret;
Sricharan R44f6df92017-08-24 12:51:33 +05301252 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301253
1254 complete(&channel->open_req);
1255
1256 if (create_device) {
1257 rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL);
1258 if (!rpdev) {
1259 ret = -ENOMEM;
1260 goto rcid_remove;
1261 }
1262
1263 rpdev->ept = &channel->ept;
1264 strncpy(rpdev->id.name, name, RPMSG_NAME_SIZE);
1265 rpdev->src = RPMSG_ADDR_ANY;
1266 rpdev->dst = RPMSG_ADDR_ANY;
1267 rpdev->ops = &glink_device_ops;
1268
1269 node = qcom_glink_match_channel(glink->dev->of_node, name);
1270 rpdev->dev.of_node = node;
1271 rpdev->dev.parent = glink->dev;
1272 rpdev->dev.release = qcom_glink_rpdev_release;
1273
1274 ret = rpmsg_register_device(rpdev);
1275 if (ret)
1276 goto free_rpdev;
1277
1278 channel->rpdev = rpdev;
1279 }
1280
1281 return 0;
1282
1283free_rpdev:
1284 kfree(rpdev);
1285rcid_remove:
Sricharan R44f6df92017-08-24 12:51:33 +05301286 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301287 idr_remove(&glink->rcids, channel->rcid);
1288 channel->rcid = 0;
Sricharan R44f6df92017-08-24 12:51:33 +05301289 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301290free_channel:
1291 /* Release the reference, iff we took it */
1292 if (create_device)
1293 kref_put(&channel->refcount, qcom_glink_channel_release);
1294
1295 return ret;
1296}
1297
1298static void qcom_glink_rx_close(struct qcom_glink *glink, unsigned int rcid)
1299{
1300 struct rpmsg_channel_info chinfo;
1301 struct glink_channel *channel;
Sricharan R44f6df92017-08-24 12:51:33 +05301302 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301303
Sricharan R44f6df92017-08-24 12:51:33 +05301304 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301305 channel = idr_find(&glink->rcids, rcid);
Sricharan R44f6df92017-08-24 12:51:33 +05301306 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301307 if (WARN(!channel, "close request on unknown channel\n"))
1308 return;
1309
Sricharan R1d2ea362017-08-24 12:51:37 +05301310 /* cancel pending rx_done work */
1311 cancel_work_sync(&channel->intent_work);
1312
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301313 if (channel->rpdev) {
1314 strncpy(chinfo.name, channel->name, sizeof(chinfo.name));
1315 chinfo.src = RPMSG_ADDR_ANY;
1316 chinfo.dst = RPMSG_ADDR_ANY;
1317
1318 rpmsg_unregister_device(glink->dev, &chinfo);
1319 }
1320
1321 qcom_glink_send_close_ack(glink, channel->rcid);
1322
Sricharan R44f6df92017-08-24 12:51:33 +05301323 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301324 idr_remove(&glink->rcids, channel->rcid);
1325 channel->rcid = 0;
Sricharan R44f6df92017-08-24 12:51:33 +05301326 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301327
1328 kref_put(&channel->refcount, qcom_glink_channel_release);
1329}
1330
1331static void qcom_glink_rx_close_ack(struct qcom_glink *glink, unsigned int lcid)
1332{
1333 struct glink_channel *channel;
Sricharan R44f6df92017-08-24 12:51:33 +05301334 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301335
Sricharan R44f6df92017-08-24 12:51:33 +05301336 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301337 channel = idr_find(&glink->lcids, lcid);
Sricharan R44f6df92017-08-24 12:51:33 +05301338 if (WARN(!channel, "close ack on unknown channel\n")) {
1339 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301340 return;
Sricharan R44f6df92017-08-24 12:51:33 +05301341 }
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301342
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301343 idr_remove(&glink->lcids, channel->lcid);
1344 channel->lcid = 0;
Sricharan R44f6df92017-08-24 12:51:33 +05301345 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301346
1347 kref_put(&channel->refcount, qcom_glink_channel_release);
1348}
1349
1350static void qcom_glink_work(struct work_struct *work)
1351{
1352 struct qcom_glink *glink = container_of(work, struct qcom_glink,
1353 rx_work);
1354 struct glink_defer_cmd *dcmd;
1355 struct glink_msg *msg;
1356 unsigned long flags;
1357 unsigned int param1;
1358 unsigned int param2;
1359 unsigned int cmd;
1360
1361 for (;;) {
1362 spin_lock_irqsave(&glink->rx_lock, flags);
1363 if (list_empty(&glink->rx_queue)) {
1364 spin_unlock_irqrestore(&glink->rx_lock, flags);
1365 break;
1366 }
1367 dcmd = list_first_entry(&glink->rx_queue,
1368 struct glink_defer_cmd, node);
1369 list_del(&dcmd->node);
1370 spin_unlock_irqrestore(&glink->rx_lock, flags);
1371
1372 msg = &dcmd->msg;
1373 cmd = le16_to_cpu(msg->cmd);
1374 param1 = le16_to_cpu(msg->param1);
1375 param2 = le32_to_cpu(msg->param2);
1376
1377 switch (cmd) {
1378 case RPM_CMD_VERSION:
Sricharan Rd31ad612017-08-24 12:51:32 +05301379 qcom_glink_receive_version(glink, param1, param2);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301380 break;
1381 case RPM_CMD_VERSION_ACK:
Sricharan Rd31ad612017-08-24 12:51:32 +05301382 qcom_glink_receive_version_ack(glink, param1, param2);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301383 break;
1384 case RPM_CMD_OPEN:
1385 qcom_glink_rx_open(glink, param1, msg->data);
1386 break;
1387 case RPM_CMD_CLOSE:
1388 qcom_glink_rx_close(glink, param1);
1389 break;
1390 case RPM_CMD_CLOSE_ACK:
1391 qcom_glink_rx_close_ack(glink, param1);
1392 break;
Sricharan R933b45d2017-08-24 12:51:34 +05301393 case RPM_CMD_RX_INTENT_REQ:
1394 qcom_glink_handle_intent_req(glink, param1, param2);
1395 break;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301396 default:
1397 WARN(1, "Unknown defer object %d\n", cmd);
1398 break;
1399 }
1400
1401 kfree(dcmd);
1402 }
1403}
1404
1405struct qcom_glink *qcom_glink_native_probe(struct device *dev,
Sricharan Rd31ad612017-08-24 12:51:32 +05301406 unsigned long features,
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301407 struct qcom_glink_pipe *rx,
Sricharan R933b45d2017-08-24 12:51:34 +05301408 struct qcom_glink_pipe *tx,
1409 bool intentless)
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301410{
1411 int irq;
1412 int ret;
1413 struct qcom_glink *glink;
1414
1415 glink = devm_kzalloc(dev, sizeof(*glink), GFP_KERNEL);
1416 if (!glink)
1417 return ERR_PTR(-ENOMEM);
1418
1419 glink->dev = dev;
1420 glink->tx_pipe = tx;
1421 glink->rx_pipe = rx;
1422
Sricharan Rd31ad612017-08-24 12:51:32 +05301423 glink->features = features;
Sricharan R933b45d2017-08-24 12:51:34 +05301424 glink->intentless = intentless;
Sricharan Rd31ad612017-08-24 12:51:32 +05301425
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301426 mutex_init(&glink->tx_lock);
1427 spin_lock_init(&glink->rx_lock);
1428 INIT_LIST_HEAD(&glink->rx_queue);
1429 INIT_WORK(&glink->rx_work, qcom_glink_work);
1430
Sricharan R44f6df92017-08-24 12:51:33 +05301431 spin_lock_init(&glink->idr_lock);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301432 idr_init(&glink->lcids);
1433 idr_init(&glink->rcids);
1434
1435 glink->mbox_client.dev = dev;
1436 glink->mbox_chan = mbox_request_channel(&glink->mbox_client, 0);
1437 if (IS_ERR(glink->mbox_chan)) {
1438 if (PTR_ERR(glink->mbox_chan) != -EPROBE_DEFER)
1439 dev_err(dev, "failed to acquire IPC channel\n");
1440 return ERR_CAST(glink->mbox_chan);
1441 }
1442
1443 irq = of_irq_get(dev->of_node, 0);
1444 ret = devm_request_irq(dev, irq,
1445 qcom_glink_native_intr,
1446 IRQF_NO_SUSPEND | IRQF_SHARED,
1447 "glink-native", glink);
1448 if (ret) {
1449 dev_err(dev, "failed to request IRQ\n");
1450 return ERR_PTR(ret);
1451 }
1452
1453 glink->irq = irq;
1454
1455 ret = qcom_glink_send_version(glink);
1456 if (ret)
1457 return ERR_PTR(ret);
1458
1459 return glink;
1460}
1461
1462static int qcom_glink_remove_device(struct device *dev, void *data)
1463{
1464 device_unregister(dev);
1465
1466 return 0;
1467}
1468
1469void qcom_glink_native_remove(struct qcom_glink *glink)
1470{
1471 struct glink_channel *channel;
1472 int cid;
1473 int ret;
Sricharan R44f6df92017-08-24 12:51:33 +05301474 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301475
1476 disable_irq(glink->irq);
1477 cancel_work_sync(&glink->rx_work);
1478
1479 ret = device_for_each_child(glink->dev, NULL, qcom_glink_remove_device);
1480 if (ret)
1481 dev_warn(glink->dev, "Can't remove GLINK devices: %d\n", ret);
1482
Sricharan R44f6df92017-08-24 12:51:33 +05301483 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301484 /* Release any defunct local channels, waiting for close-ack */
1485 idr_for_each_entry(&glink->lcids, channel, cid)
1486 kref_put(&channel->refcount, qcom_glink_channel_release);
1487
1488 idr_destroy(&glink->lcids);
1489 idr_destroy(&glink->rcids);
Sricharan R44f6df92017-08-24 12:51:33 +05301490 spin_unlock_irqrestore(&glink->idr_lock, flags);
Sricharan R76cf1102017-08-24 12:51:29 +05301491 mbox_free_channel(glink->mbox_chan);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301492}
Bjorn Anderssoncaf989c2017-08-24 12:51:30 +05301493
1494void qcom_glink_native_unregister(struct qcom_glink *glink)
1495{
1496 device_unregister(glink->dev);
1497}