blob: f505f58b797d7b3088e3eb8587cb12bc16304636 [file] [log] [blame]
Suman Anna84369fb2018-05-31 12:11:02 -05001// SPDX-License-Identifier: GPL-2.0
Bjorn Andersson835764dd2017-08-24 12:51:26 +05302/*
3 * Copyright (c) 2016-2017, Linaro Ltd
Bjorn Andersson835764dd2017-08-24 12:51:26 +05304 */
5
6#include <linux/idr.h>
7#include <linux/interrupt.h>
8#include <linux/io.h>
9#include <linux/list.h>
10#include <linux/mfd/syscon.h>
11#include <linux/module.h>
12#include <linux/of.h>
13#include <linux/of_address.h>
14#include <linux/of_irq.h>
15#include <linux/platform_device.h>
16#include <linux/regmap.h>
17#include <linux/rpmsg.h>
Sricharan R6bf68cb2017-08-24 12:51:38 +053018#include <linux/sizes.h>
Bjorn Andersson835764dd2017-08-24 12:51:26 +053019#include <linux/slab.h>
20#include <linux/workqueue.h>
21#include <linux/mailbox_client.h>
22
23#include "rpmsg_internal.h"
24#include "qcom_glink_native.h"
25
26#define GLINK_NAME_SIZE 32
Sricharan Rd31ad612017-08-24 12:51:32 +053027#define GLINK_VERSION_1 1
Bjorn Andersson835764dd2017-08-24 12:51:26 +053028
29#define RPM_GLINK_CID_MIN 1
30#define RPM_GLINK_CID_MAX 65536
31
32struct glink_msg {
33 __le16 cmd;
34 __le16 param1;
35 __le32 param2;
36 u8 data[];
37} __packed;
38
39/**
40 * struct glink_defer_cmd - deferred incoming control message
41 * @node: list node
42 * @msg: message header
43 * data: payload of the message
44 *
45 * Copy of a received control message, to be added to @rx_queue and processed
46 * by @rx_work of @qcom_glink.
47 */
48struct glink_defer_cmd {
49 struct list_head node;
50
51 struct glink_msg msg;
52 u8 data[];
53};
54
55/**
Sricharan R933b45d2017-08-24 12:51:34 +053056 * struct glink_core_rx_intent - RX intent
57 * RX intent
58 *
59 * data: pointer to the data (may be NULL for zero-copy)
60 * id: remote or local intent ID
61 * size: size of the original intent (do not modify)
62 * reuse: To mark if the intent can be reused after first use
63 * in_use: To mark if intent is already in use for the channel
64 * offset: next write offset (initially 0)
65 */
66struct glink_core_rx_intent {
67 void *data;
68 u32 id;
69 size_t size;
70 bool reuse;
71 bool in_use;
72 u32 offset;
Sricharan R1d2ea362017-08-24 12:51:37 +053073
74 struct list_head node;
Sricharan R933b45d2017-08-24 12:51:34 +053075};
76
77/**
Bjorn Andersson835764dd2017-08-24 12:51:26 +053078 * struct qcom_glink - driver context, relates to one remote subsystem
79 * @dev: reference to the associated struct device
80 * @mbox_client: mailbox client
81 * @mbox_chan: mailbox channel
82 * @rx_pipe: pipe object for receive FIFO
83 * @tx_pipe: pipe object for transmit FIFO
84 * @irq: IRQ for signaling incoming events
85 * @rx_work: worker for handling received control messages
86 * @rx_lock: protects the @rx_queue
87 * @rx_queue: queue of received control messages to be processed in @rx_work
88 * @tx_lock: synchronizes operations on the tx fifo
89 * @idr_lock: synchronizes @lcids and @rcids modifications
90 * @lcids: idr of all channels with a known local channel id
91 * @rcids: idr of all channels with a known remote channel id
92 */
93struct qcom_glink {
94 struct device *dev;
95
96 struct mbox_client mbox_client;
97 struct mbox_chan *mbox_chan;
98
99 struct qcom_glink_pipe *rx_pipe;
100 struct qcom_glink_pipe *tx_pipe;
101
102 int irq;
103
104 struct work_struct rx_work;
105 spinlock_t rx_lock;
106 struct list_head rx_queue;
107
Bjorn Andersson29fc9b32018-02-13 11:04:04 -0800108 spinlock_t tx_lock;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530109
Sricharan R44f6df92017-08-24 12:51:33 +0530110 spinlock_t idr_lock;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530111 struct idr lcids;
112 struct idr rcids;
Sricharan Rd31ad612017-08-24 12:51:32 +0530113 unsigned long features;
114
115 bool intentless;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530116};
117
118enum {
119 GLINK_STATE_CLOSED,
120 GLINK_STATE_OPENING,
121 GLINK_STATE_OPEN,
122 GLINK_STATE_CLOSING,
123};
124
125/**
126 * struct glink_channel - internal representation of a channel
127 * @rpdev: rpdev reference, only used for primary endpoints
128 * @ept: rpmsg endpoint this channel is associated with
129 * @glink: qcom_glink context handle
130 * @refcount: refcount for the channel object
131 * @recv_lock: guard for @ept.cb
132 * @name: unique channel name/identifier
133 * @lcid: channel id, in local space
134 * @rcid: channel id, in remote space
Sricharan Rdacbb352017-08-24 12:51:39 +0530135 * @intent_lock: lock for protection of @liids, @riids
Sricharan R933b45d2017-08-24 12:51:34 +0530136 * @liids: idr of all local intents
Sricharan Rdacbb352017-08-24 12:51:39 +0530137 * @riids: idr of all remote intents
Sricharan R1d2ea362017-08-24 12:51:37 +0530138 * @intent_work: worker responsible for transmitting rx_done packets
139 * @done_intents: list of intents that needs to be announced rx_done
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530140 * @buf: receive buffer, for gathering fragments
141 * @buf_offset: write offset in @buf
142 * @buf_size: size of current @buf
143 * @open_ack: completed once remote has acked the open-request
144 * @open_req: completed once open-request has been received
Sricharan R27b9c5b2017-08-24 12:51:41 +0530145 * @intent_req_lock: Synchronises multiple intent requests
146 * @intent_req_result: Result of intent request
147 * @intent_req_comp: Completion for intent_req signalling
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530148 */
149struct glink_channel {
150 struct rpmsg_endpoint ept;
151
152 struct rpmsg_device *rpdev;
153 struct qcom_glink *glink;
154
155 struct kref refcount;
156
157 spinlock_t recv_lock;
158
159 char *name;
160 unsigned int lcid;
161 unsigned int rcid;
162
Sricharan R933b45d2017-08-24 12:51:34 +0530163 spinlock_t intent_lock;
164 struct idr liids;
Sricharan Rdacbb352017-08-24 12:51:39 +0530165 struct idr riids;
Sricharan R1d2ea362017-08-24 12:51:37 +0530166 struct work_struct intent_work;
167 struct list_head done_intents;
Sricharan R933b45d2017-08-24 12:51:34 +0530168
Sricharan R64f95f82017-08-24 12:51:35 +0530169 struct glink_core_rx_intent *buf;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530170 int buf_offset;
171 int buf_size;
172
173 struct completion open_ack;
174 struct completion open_req;
Sricharan R27b9c5b2017-08-24 12:51:41 +0530175
176 struct mutex intent_req_lock;
177 bool intent_req_result;
178 struct completion intent_req_comp;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530179};
180
181#define to_glink_channel(_ept) container_of(_ept, struct glink_channel, ept)
182
183static const struct rpmsg_endpoint_ops glink_endpoint_ops;
184
185#define RPM_CMD_VERSION 0
186#define RPM_CMD_VERSION_ACK 1
187#define RPM_CMD_OPEN 2
188#define RPM_CMD_CLOSE 3
189#define RPM_CMD_OPEN_ACK 4
Sricharan R933b45d2017-08-24 12:51:34 +0530190#define RPM_CMD_INTENT 5
Sricharan R1d2ea362017-08-24 12:51:37 +0530191#define RPM_CMD_RX_DONE 6
Sricharan R933b45d2017-08-24 12:51:34 +0530192#define RPM_CMD_RX_INTENT_REQ 7
193#define RPM_CMD_RX_INTENT_REQ_ACK 8
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530194#define RPM_CMD_TX_DATA 9
195#define RPM_CMD_CLOSE_ACK 11
196#define RPM_CMD_TX_DATA_CONT 12
197#define RPM_CMD_READ_NOTIF 13
Sricharan R1d2ea362017-08-24 12:51:37 +0530198#define RPM_CMD_RX_DONE_W_REUSE 14
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530199
200#define GLINK_FEATURE_INTENTLESS BIT(1)
201
Sricharan R1d2ea362017-08-24 12:51:37 +0530202static void qcom_glink_rx_done_work(struct work_struct *work);
203
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530204static struct glink_channel *qcom_glink_alloc_channel(struct qcom_glink *glink,
205 const char *name)
206{
207 struct glink_channel *channel;
208
209 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
210 if (!channel)
211 return ERR_PTR(-ENOMEM);
212
213 /* Setup glink internal glink_channel data */
214 spin_lock_init(&channel->recv_lock);
Sricharan R933b45d2017-08-24 12:51:34 +0530215 spin_lock_init(&channel->intent_lock);
Wei Yongjunfb416f62018-01-02 11:47:03 +0000216 mutex_init(&channel->intent_req_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);
Arun Kumar Neelakantam2394fac2017-10-30 11:11:24 +0530223 init_completion(&channel->intent_req_comp);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530224
Sricharan R1d2ea362017-08-24 12:51:37 +0530225 INIT_LIST_HEAD(&channel->done_intents);
226 INIT_WORK(&channel->intent_work, qcom_glink_rx_done_work);
227
Sricharan R933b45d2017-08-24 12:51:34 +0530228 idr_init(&channel->liids);
Sricharan Rdacbb352017-08-24 12:51:39 +0530229 idr_init(&channel->riids);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530230 kref_init(&channel->refcount);
231
232 return channel;
233}
234
235static void qcom_glink_channel_release(struct kref *ref)
236{
237 struct glink_channel *channel = container_of(ref, struct glink_channel,
238 refcount);
Sricharan R933b45d2017-08-24 12:51:34 +0530239 unsigned long flags;
240
241 spin_lock_irqsave(&channel->intent_lock, flags);
242 idr_destroy(&channel->liids);
Sricharan Rdacbb352017-08-24 12:51:39 +0530243 idr_destroy(&channel->riids);
Sricharan R933b45d2017-08-24 12:51:34 +0530244 spin_unlock_irqrestore(&channel->intent_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530245
246 kfree(channel->name);
247 kfree(channel);
248}
249
250static size_t qcom_glink_rx_avail(struct qcom_glink *glink)
251{
252 return glink->rx_pipe->avail(glink->rx_pipe);
253}
254
255static void qcom_glink_rx_peak(struct qcom_glink *glink,
Bjorn Anderssonb88eee92017-08-24 12:51:36 +0530256 void *data, unsigned int offset, size_t count)
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530257{
Bjorn Anderssonb88eee92017-08-24 12:51:36 +0530258 glink->rx_pipe->peak(glink->rx_pipe, data, offset, count);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530259}
260
261static void qcom_glink_rx_advance(struct qcom_glink *glink, size_t count)
262{
263 glink->rx_pipe->advance(glink->rx_pipe, count);
264}
265
266static size_t qcom_glink_tx_avail(struct qcom_glink *glink)
267{
268 return glink->tx_pipe->avail(glink->tx_pipe);
269}
270
271static void qcom_glink_tx_write(struct qcom_glink *glink,
272 const void *hdr, size_t hlen,
273 const void *data, size_t dlen)
274{
275 glink->tx_pipe->write(glink->tx_pipe, hdr, hlen, data, dlen);
276}
277
278static int qcom_glink_tx(struct qcom_glink *glink,
279 const void *hdr, size_t hlen,
280 const void *data, size_t dlen, bool wait)
281{
282 unsigned int tlen = hlen + dlen;
Bjorn Andersson29fc9b32018-02-13 11:04:04 -0800283 unsigned long flags;
284 int ret = 0;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530285
286 /* Reject packets that are too big */
287 if (tlen >= glink->tx_pipe->length)
288 return -EINVAL;
289
Bjorn Andersson29fc9b32018-02-13 11:04:04 -0800290 spin_lock_irqsave(&glink->tx_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530291
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
Bjorn Andersson29fc9b32018-02-13 11:04:04 -0800298 /* Wait without holding the tx_lock */
299 spin_unlock_irqrestore(&glink->tx_lock, flags);
300
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530301 usleep_range(10000, 15000);
Bjorn Andersson29fc9b32018-02-13 11:04:04 -0800302
303 spin_lock_irqsave(&glink->tx_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530304 }
305
306 qcom_glink_tx_write(glink, hdr, hlen, data, dlen);
307
308 mbox_send_message(glink->mbox_chan, NULL);
309 mbox_client_txdone(glink->mbox_chan, 0);
310
311out:
Bjorn Andersson29fc9b32018-02-13 11:04:04 -0800312 spin_unlock_irqrestore(&glink->tx_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530313
314 return ret;
315}
316
317static int qcom_glink_send_version(struct qcom_glink *glink)
318{
319 struct glink_msg msg;
320
321 msg.cmd = cpu_to_le16(RPM_CMD_VERSION);
Sricharan Rd31ad612017-08-24 12:51:32 +0530322 msg.param1 = cpu_to_le16(GLINK_VERSION_1);
323 msg.param2 = cpu_to_le32(glink->features);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530324
325 return qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
326}
327
328static void qcom_glink_send_version_ack(struct qcom_glink *glink)
329{
330 struct glink_msg msg;
331
332 msg.cmd = cpu_to_le16(RPM_CMD_VERSION_ACK);
Sricharan Rd31ad612017-08-24 12:51:32 +0530333 msg.param1 = cpu_to_le16(GLINK_VERSION_1);
334 msg.param2 = cpu_to_le32(glink->features);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530335
336 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
337}
338
339static void qcom_glink_send_open_ack(struct qcom_glink *glink,
340 struct glink_channel *channel)
341{
342 struct glink_msg msg;
343
344 msg.cmd = cpu_to_le16(RPM_CMD_OPEN_ACK);
345 msg.param1 = cpu_to_le16(channel->rcid);
346 msg.param2 = cpu_to_le32(0);
347
348 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
349}
350
Sricharan R27b9c5b2017-08-24 12:51:41 +0530351static void qcom_glink_handle_intent_req_ack(struct qcom_glink *glink,
352 unsigned int cid, bool granted)
353{
354 struct glink_channel *channel;
355 unsigned long flags;
356
357 spin_lock_irqsave(&glink->idr_lock, flags);
358 channel = idr_find(&glink->rcids, cid);
359 spin_unlock_irqrestore(&glink->idr_lock, flags);
360 if (!channel) {
361 dev_err(glink->dev, "unable to find channel\n");
362 return;
363 }
364
365 channel->intent_req_result = granted;
366 complete(&channel->intent_req_comp);
367}
368
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530369/**
370 * qcom_glink_send_open_req() - send a RPM_CMD_OPEN request to the remote
371 * @glink: Ptr to the glink edge
372 * @channel: Ptr to the channel that the open req is sent
373 *
374 * Allocates a local channel id and sends a RPM_CMD_OPEN message to the remote.
375 * Will return with refcount held, regardless of outcome.
376 *
377 * Returns 0 on success, negative errno otherwise.
378 */
379static int qcom_glink_send_open_req(struct qcom_glink *glink,
380 struct glink_channel *channel)
381{
382 struct {
383 struct glink_msg msg;
384 u8 name[GLINK_NAME_SIZE];
385 } __packed req;
386 int name_len = strlen(channel->name) + 1;
387 int req_len = ALIGN(sizeof(req.msg) + name_len, 8);
388 int ret;
Sricharan R44f6df92017-08-24 12:51:33 +0530389 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530390
391 kref_get(&channel->refcount);
392
Sricharan R44f6df92017-08-24 12:51:33 +0530393 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530394 ret = idr_alloc_cyclic(&glink->lcids, channel,
395 RPM_GLINK_CID_MIN, RPM_GLINK_CID_MAX,
Sricharan R44f6df92017-08-24 12:51:33 +0530396 GFP_ATOMIC);
397 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530398 if (ret < 0)
399 return ret;
400
401 channel->lcid = ret;
402
403 req.msg.cmd = cpu_to_le16(RPM_CMD_OPEN);
404 req.msg.param1 = cpu_to_le16(channel->lcid);
405 req.msg.param2 = cpu_to_le32(name_len);
406 strcpy(req.name, channel->name);
407
408 ret = qcom_glink_tx(glink, &req, req_len, NULL, 0, true);
409 if (ret)
410 goto remove_idr;
411
412 return 0;
413
414remove_idr:
Sricharan R44f6df92017-08-24 12:51:33 +0530415 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530416 idr_remove(&glink->lcids, channel->lcid);
417 channel->lcid = 0;
Sricharan R44f6df92017-08-24 12:51:33 +0530418 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530419
420 return ret;
421}
422
423static void qcom_glink_send_close_req(struct qcom_glink *glink,
424 struct glink_channel *channel)
425{
426 struct glink_msg req;
427
428 req.cmd = cpu_to_le16(RPM_CMD_CLOSE);
429 req.param1 = cpu_to_le16(channel->lcid);
430 req.param2 = 0;
431
432 qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
433}
434
435static void qcom_glink_send_close_ack(struct qcom_glink *glink,
436 unsigned int rcid)
437{
438 struct glink_msg req;
439
440 req.cmd = cpu_to_le16(RPM_CMD_CLOSE_ACK);
441 req.param1 = cpu_to_le16(rcid);
442 req.param2 = 0;
443
444 qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
445}
446
Sricharan R1d2ea362017-08-24 12:51:37 +0530447static void qcom_glink_rx_done_work(struct work_struct *work)
448{
449 struct glink_channel *channel = container_of(work, struct glink_channel,
450 intent_work);
451 struct qcom_glink *glink = channel->glink;
452 struct glink_core_rx_intent *intent, *tmp;
453 struct {
454 u16 id;
455 u16 lcid;
456 u32 liid;
457 } __packed cmd;
458
459 unsigned int cid = channel->lcid;
460 unsigned int iid;
461 bool reuse;
462 unsigned long flags;
463
464 spin_lock_irqsave(&channel->intent_lock, flags);
465 list_for_each_entry_safe(intent, tmp, &channel->done_intents, node) {
466 list_del(&intent->node);
467 spin_unlock_irqrestore(&channel->intent_lock, flags);
468 iid = intent->id;
469 reuse = intent->reuse;
470
471 cmd.id = reuse ? RPM_CMD_RX_DONE_W_REUSE : RPM_CMD_RX_DONE;
472 cmd.lcid = cid;
473 cmd.liid = iid;
474
475 qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
476 if (!reuse) {
477 kfree(intent->data);
478 kfree(intent);
479 }
480 spin_lock_irqsave(&channel->intent_lock, flags);
481 }
482 spin_unlock_irqrestore(&channel->intent_lock, flags);
483}
484
485static void qcom_glink_rx_done(struct qcom_glink *glink,
486 struct glink_channel *channel,
487 struct glink_core_rx_intent *intent)
488{
489 /* We don't send RX_DONE to intentless systems */
490 if (glink->intentless) {
491 kfree(intent->data);
492 kfree(intent);
493 return;
494 }
495
496 /* Take it off the tree of receive intents */
497 if (!intent->reuse) {
498 spin_lock(&channel->intent_lock);
499 idr_remove(&channel->liids, intent->id);
500 spin_unlock(&channel->intent_lock);
501 }
502
503 /* Schedule the sending of a rx_done indication */
504 spin_lock(&channel->intent_lock);
505 list_add_tail(&intent->node, &channel->done_intents);
506 spin_unlock(&channel->intent_lock);
507
508 schedule_work(&channel->intent_work);
509}
510
Sricharan Rd31ad612017-08-24 12:51:32 +0530511/**
512 * qcom_glink_receive_version() - receive version/features from remote system
513 *
514 * @glink: pointer to transport interface
515 * @r_version: remote version
516 * @r_features: remote features
517 *
518 * This function is called in response to a remote-initiated version/feature
519 * negotiation sequence.
520 */
521static void qcom_glink_receive_version(struct qcom_glink *glink,
522 u32 version,
523 u32 features)
524{
525 switch (version) {
526 case 0:
527 break;
528 case GLINK_VERSION_1:
529 glink->features &= features;
530 /* FALLTHROUGH */
531 default:
532 qcom_glink_send_version_ack(glink);
533 break;
534 }
535}
536
537/**
538 * qcom_glink_receive_version_ack() - receive negotiation ack from remote system
539 *
540 * @glink: pointer to transport interface
541 * @r_version: remote version response
542 * @r_features: remote features response
543 *
544 * This function is called in response to a local-initiated version/feature
545 * negotiation sequence and is the counter-offer from the remote side based
546 * upon the initial version and feature set requested.
547 */
548static void qcom_glink_receive_version_ack(struct qcom_glink *glink,
549 u32 version,
550 u32 features)
551{
552 switch (version) {
553 case 0:
554 /* Version negotiation failed */
555 break;
556 case GLINK_VERSION_1:
557 if (features == glink->features)
558 break;
559
560 glink->features &= features;
561 /* FALLTHROUGH */
562 default:
563 qcom_glink_send_version(glink);
564 break;
565 }
566}
567
Sricharan R933b45d2017-08-24 12:51:34 +0530568/**
569 * qcom_glink_send_intent_req_ack() - convert an rx intent request ack cmd to
570 wire format and transmit
571 * @glink: The transport to transmit on.
572 * @channel: The glink channel
573 * @granted: The request response to encode.
574 *
575 * Return: 0 on success or standard Linux error code.
576 */
577static int qcom_glink_send_intent_req_ack(struct qcom_glink *glink,
578 struct glink_channel *channel,
579 bool granted)
580{
581 struct glink_msg msg;
582
583 msg.cmd = cpu_to_le16(RPM_CMD_RX_INTENT_REQ_ACK);
584 msg.param1 = cpu_to_le16(channel->lcid);
585 msg.param2 = cpu_to_le32(granted);
586
587 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
588
589 return 0;
590}
591
592/**
593 * qcom_glink_advertise_intent - convert an rx intent cmd to wire format and
594 * transmit
595 * @glink: The transport to transmit on.
596 * @channel: The local channel
597 * @size: The intent to pass on to remote.
598 *
599 * Return: 0 on success or standard Linux error code.
600 */
601static int qcom_glink_advertise_intent(struct qcom_glink *glink,
602 struct glink_channel *channel,
603 struct glink_core_rx_intent *intent)
604{
605 struct command {
606 u16 id;
607 u16 lcid;
608 u32 count;
609 u32 size;
610 u32 liid;
611 } __packed;
612 struct command cmd;
613
614 cmd.id = cpu_to_le16(RPM_CMD_INTENT);
615 cmd.lcid = cpu_to_le16(channel->lcid);
616 cmd.count = cpu_to_le32(1);
617 cmd.size = cpu_to_le32(intent->size);
618 cmd.liid = cpu_to_le32(intent->id);
619
620 qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
621
622 return 0;
623}
624
625static struct glink_core_rx_intent *
626qcom_glink_alloc_intent(struct qcom_glink *glink,
627 struct glink_channel *channel,
628 size_t size,
629 bool reuseable)
630{
631 struct glink_core_rx_intent *intent;
632 int ret;
633 unsigned long flags;
634
635 intent = kzalloc(sizeof(*intent), GFP_KERNEL);
Sricharan R933b45d2017-08-24 12:51:34 +0530636 if (!intent)
637 return NULL;
638
639 intent->data = kzalloc(size, GFP_KERNEL);
640 if (!intent->data)
Dan Carpenterb775d152017-09-08 13:34:42 +0300641 goto free_intent;
Sricharan R933b45d2017-08-24 12:51:34 +0530642
643 spin_lock_irqsave(&channel->intent_lock, flags);
644 ret = idr_alloc_cyclic(&channel->liids, intent, 1, -1, GFP_ATOMIC);
645 if (ret < 0) {
646 spin_unlock_irqrestore(&channel->intent_lock, flags);
Dan Carpenterb775d152017-09-08 13:34:42 +0300647 goto free_data;
Sricharan R933b45d2017-08-24 12:51:34 +0530648 }
649 spin_unlock_irqrestore(&channel->intent_lock, flags);
650
651 intent->id = ret;
652 intent->size = size;
653 intent->reuse = reuseable;
654
655 return intent;
Dan Carpenterb775d152017-09-08 13:34:42 +0300656
657free_data:
658 kfree(intent->data);
659free_intent:
660 kfree(intent);
661 return NULL;
Sricharan R933b45d2017-08-24 12:51:34 +0530662}
663
Sricharan R88c60602017-08-24 12:51:42 +0530664static void qcom_glink_handle_rx_done(struct qcom_glink *glink,
665 u32 cid, uint32_t iid,
666 bool reuse)
667{
668 struct glink_core_rx_intent *intent;
669 struct glink_channel *channel;
670 unsigned long flags;
671
672 spin_lock_irqsave(&glink->idr_lock, flags);
673 channel = idr_find(&glink->rcids, cid);
674 spin_unlock_irqrestore(&glink->idr_lock, flags);
675 if (!channel) {
676 dev_err(glink->dev, "invalid channel id received\n");
677 return;
678 }
679
680 spin_lock_irqsave(&channel->intent_lock, flags);
681 intent = idr_find(&channel->riids, iid);
682
683 if (!intent) {
684 spin_unlock_irqrestore(&channel->intent_lock, flags);
685 dev_err(glink->dev, "invalid intent id received\n");
686 return;
687 }
688
689 intent->in_use = false;
690
691 if (!reuse) {
692 idr_remove(&channel->riids, intent->id);
693 kfree(intent);
694 }
695 spin_unlock_irqrestore(&channel->intent_lock, flags);
696}
697
Sricharan R933b45d2017-08-24 12:51:34 +0530698/**
699 * qcom_glink_handle_intent_req() - Receive a request for rx_intent
700 * from remote side
701 * if_ptr: Pointer to the transport interface
702 * rcid: Remote channel ID
703 * size: size of the intent
704 *
705 * The function searches for the local channel to which the request for
706 * rx_intent has arrived and allocates and notifies the remote back
707 */
708static void qcom_glink_handle_intent_req(struct qcom_glink *glink,
709 u32 cid, size_t size)
710{
711 struct glink_core_rx_intent *intent;
712 struct glink_channel *channel;
713 unsigned long flags;
714
715 spin_lock_irqsave(&glink->idr_lock, flags);
716 channel = idr_find(&glink->rcids, cid);
717 spin_unlock_irqrestore(&glink->idr_lock, flags);
718
719 if (!channel) {
720 pr_err("%s channel not found for cid %d\n", __func__, cid);
721 return;
722 }
723
724 intent = qcom_glink_alloc_intent(glink, channel, size, false);
725 if (intent)
726 qcom_glink_advertise_intent(glink, channel, intent);
727
728 qcom_glink_send_intent_req_ack(glink, channel, !!intent);
729}
730
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530731static int qcom_glink_rx_defer(struct qcom_glink *glink, size_t extra)
732{
733 struct glink_defer_cmd *dcmd;
734
735 extra = ALIGN(extra, 8);
736
737 if (qcom_glink_rx_avail(glink) < sizeof(struct glink_msg) + extra) {
738 dev_dbg(glink->dev, "Insufficient data in rx fifo");
739 return -ENXIO;
740 }
741
742 dcmd = kzalloc(sizeof(*dcmd) + extra, GFP_ATOMIC);
743 if (!dcmd)
744 return -ENOMEM;
745
746 INIT_LIST_HEAD(&dcmd->node);
747
Bjorn Anderssonb88eee92017-08-24 12:51:36 +0530748 qcom_glink_rx_peak(glink, &dcmd->msg, 0, sizeof(dcmd->msg) + extra);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530749
750 spin_lock(&glink->rx_lock);
751 list_add_tail(&dcmd->node, &glink->rx_queue);
752 spin_unlock(&glink->rx_lock);
753
754 schedule_work(&glink->rx_work);
755 qcom_glink_rx_advance(glink, sizeof(dcmd->msg) + extra);
756
757 return 0;
758}
759
760static int qcom_glink_rx_data(struct qcom_glink *glink, size_t avail)
761{
Sricharan R64f95f82017-08-24 12:51:35 +0530762 struct glink_core_rx_intent *intent;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530763 struct glink_channel *channel;
764 struct {
765 struct glink_msg msg;
766 __le32 chunk_size;
767 __le32 left_size;
768 } __packed hdr;
769 unsigned int chunk_size;
770 unsigned int left_size;
771 unsigned int rcid;
Sricharan R64f95f82017-08-24 12:51:35 +0530772 unsigned int liid;
773 int ret = 0;
Sricharan R44f6df92017-08-24 12:51:33 +0530774 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530775
776 if (avail < sizeof(hdr)) {
777 dev_dbg(glink->dev, "Not enough data in fifo\n");
778 return -EAGAIN;
779 }
780
Bjorn Anderssonb88eee92017-08-24 12:51:36 +0530781 qcom_glink_rx_peak(glink, &hdr, 0, sizeof(hdr));
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530782 chunk_size = le32_to_cpu(hdr.chunk_size);
783 left_size = le32_to_cpu(hdr.left_size);
784
785 if (avail < sizeof(hdr) + chunk_size) {
786 dev_dbg(glink->dev, "Payload not yet in fifo\n");
787 return -EAGAIN;
788 }
789
790 if (WARN(chunk_size % 4, "Incoming data must be word aligned\n"))
791 return -EINVAL;
792
793 rcid = le16_to_cpu(hdr.msg.param1);
Sricharan R44f6df92017-08-24 12:51:33 +0530794 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530795 channel = idr_find(&glink->rcids, rcid);
Sricharan R44f6df92017-08-24 12:51:33 +0530796 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530797 if (!channel) {
798 dev_dbg(glink->dev, "Data on non-existing channel\n");
799
800 /* Drop the message */
Sricharan R64f95f82017-08-24 12:51:35 +0530801 goto advance_rx;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530802 }
803
Sricharan R64f95f82017-08-24 12:51:35 +0530804 if (glink->intentless) {
805 /* Might have an ongoing, fragmented, message to append */
806 if (!channel->buf) {
807 intent = kzalloc(sizeof(*intent), GFP_ATOMIC);
808 if (!intent)
809 return -ENOMEM;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530810
Sricharan R64f95f82017-08-24 12:51:35 +0530811 intent->data = kmalloc(chunk_size + left_size,
812 GFP_ATOMIC);
813 if (!intent->data) {
814 kfree(intent);
815 return -ENOMEM;
816 }
817
818 intent->id = 0xbabababa;
819 intent->size = chunk_size + left_size;
820 intent->offset = 0;
821
822 channel->buf = intent;
823 } else {
824 intent = channel->buf;
825 }
826 } else {
827 liid = le32_to_cpu(hdr.msg.param2);
828
829 spin_lock_irqsave(&channel->intent_lock, flags);
830 intent = idr_find(&channel->liids, liid);
831 spin_unlock_irqrestore(&channel->intent_lock, flags);
832
833 if (!intent) {
834 dev_err(glink->dev,
835 "no intent found for channel %s intent %d",
836 channel->name, liid);
837 goto advance_rx;
838 }
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530839 }
840
Sricharan R64f95f82017-08-24 12:51:35 +0530841 if (intent->size - intent->offset < chunk_size) {
842 dev_err(glink->dev, "Insufficient space in intent\n");
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530843
844 /* The packet header lied, drop payload */
Sricharan R64f95f82017-08-24 12:51:35 +0530845 goto advance_rx;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530846 }
847
Sricharan R64f95f82017-08-24 12:51:35 +0530848 qcom_glink_rx_peak(glink, intent->data + intent->offset,
Bjorn Anderssonb88eee92017-08-24 12:51:36 +0530849 sizeof(hdr), chunk_size);
Sricharan R64f95f82017-08-24 12:51:35 +0530850 intent->offset += chunk_size;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530851
852 /* Handle message when no fragments remain to be received */
853 if (!left_size) {
854 spin_lock(&channel->recv_lock);
855 if (channel->ept.cb) {
856 channel->ept.cb(channel->ept.rpdev,
Sricharan R64f95f82017-08-24 12:51:35 +0530857 intent->data,
858 intent->offset,
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530859 channel->ept.priv,
860 RPMSG_ADDR_ANY);
861 }
862 spin_unlock(&channel->recv_lock);
863
Sricharan R64f95f82017-08-24 12:51:35 +0530864 intent->offset = 0;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530865 channel->buf = NULL;
Sricharan R1d2ea362017-08-24 12:51:37 +0530866
867 qcom_glink_rx_done(glink, channel, intent);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530868 }
869
Sricharan R64f95f82017-08-24 12:51:35 +0530870advance_rx:
Bjorn Anderssonb88eee92017-08-24 12:51:36 +0530871 qcom_glink_rx_advance(glink, ALIGN(sizeof(hdr) + chunk_size, 8));
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530872
Sricharan R64f95f82017-08-24 12:51:35 +0530873 return ret;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530874}
875
Sricharan Rdacbb352017-08-24 12:51:39 +0530876static void qcom_glink_handle_intent(struct qcom_glink *glink,
877 unsigned int cid,
878 unsigned int count,
879 size_t avail)
880{
881 struct glink_core_rx_intent *intent;
882 struct glink_channel *channel;
883 struct intent_pair {
884 __le32 size;
885 __le32 iid;
886 };
887
888 struct {
889 struct glink_msg msg;
890 struct intent_pair intents[];
891 } __packed * msg;
892
893 const size_t msglen = sizeof(*msg) + sizeof(struct intent_pair) * count;
894 int ret;
895 int i;
896 unsigned long flags;
897
898 if (avail < msglen) {
899 dev_dbg(glink->dev, "Not enough data in fifo\n");
900 return;
901 }
902
903 spin_lock_irqsave(&glink->idr_lock, flags);
904 channel = idr_find(&glink->rcids, cid);
905 spin_unlock_irqrestore(&glink->idr_lock, flags);
906 if (!channel) {
907 dev_err(glink->dev, "intents for non-existing channel\n");
908 return;
909 }
910
911 msg = kmalloc(msglen, GFP_ATOMIC);
912 if (!msg)
913 return;
914
915 qcom_glink_rx_peak(glink, msg, 0, msglen);
916
917 for (i = 0; i < count; ++i) {
918 intent = kzalloc(sizeof(*intent), GFP_ATOMIC);
919 if (!intent)
920 break;
921
922 intent->id = le32_to_cpu(msg->intents[i].iid);
923 intent->size = le32_to_cpu(msg->intents[i].size);
924
925 spin_lock_irqsave(&channel->intent_lock, flags);
926 ret = idr_alloc(&channel->riids, intent,
927 intent->id, intent->id + 1, GFP_ATOMIC);
928 spin_unlock_irqrestore(&channel->intent_lock, flags);
929
930 if (ret < 0)
931 dev_err(glink->dev, "failed to store remote intent\n");
932 }
933
934 kfree(msg);
935 qcom_glink_rx_advance(glink, ALIGN(msglen, 8));
936}
937
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530938static int qcom_glink_rx_open_ack(struct qcom_glink *glink, unsigned int lcid)
939{
940 struct glink_channel *channel;
941
Sricharan R44f6df92017-08-24 12:51:33 +0530942 spin_lock(&glink->idr_lock);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530943 channel = idr_find(&glink->lcids, lcid);
Bjorn Andersson5f5423f2017-08-30 16:26:23 -0700944 spin_unlock(&glink->idr_lock);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530945 if (!channel) {
946 dev_err(glink->dev, "Invalid open ack packet\n");
947 return -EINVAL;
948 }
949
950 complete(&channel->open_ack);
951
952 return 0;
953}
954
955static irqreturn_t qcom_glink_native_intr(int irq, void *data)
956{
957 struct qcom_glink *glink = data;
958 struct glink_msg msg;
959 unsigned int param1;
960 unsigned int param2;
961 unsigned int avail;
962 unsigned int cmd;
Colin Ian Kinged43d092017-09-03 14:06:01 +0100963 int ret = 0;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530964
965 for (;;) {
966 avail = qcom_glink_rx_avail(glink);
967 if (avail < sizeof(msg))
968 break;
969
Bjorn Anderssonb88eee92017-08-24 12:51:36 +0530970 qcom_glink_rx_peak(glink, &msg, 0, sizeof(msg));
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530971
972 cmd = le16_to_cpu(msg.cmd);
973 param1 = le16_to_cpu(msg.param1);
974 param2 = le32_to_cpu(msg.param2);
975
976 switch (cmd) {
977 case RPM_CMD_VERSION:
978 case RPM_CMD_VERSION_ACK:
979 case RPM_CMD_CLOSE:
980 case RPM_CMD_CLOSE_ACK:
Sricharan R933b45d2017-08-24 12:51:34 +0530981 case RPM_CMD_RX_INTENT_REQ:
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530982 ret = qcom_glink_rx_defer(glink, 0);
983 break;
984 case RPM_CMD_OPEN_ACK:
985 ret = qcom_glink_rx_open_ack(glink, param1);
986 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
987 break;
988 case RPM_CMD_OPEN:
989 ret = qcom_glink_rx_defer(glink, param2);
990 break;
991 case RPM_CMD_TX_DATA:
992 case RPM_CMD_TX_DATA_CONT:
993 ret = qcom_glink_rx_data(glink, avail);
994 break;
995 case RPM_CMD_READ_NOTIF:
996 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
997
998 mbox_send_message(glink->mbox_chan, NULL);
999 mbox_client_txdone(glink->mbox_chan, 0);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301000 break;
Sricharan Rdacbb352017-08-24 12:51:39 +05301001 case RPM_CMD_INTENT:
1002 qcom_glink_handle_intent(glink, param1, param2, avail);
1003 break;
Sricharan R88c60602017-08-24 12:51:42 +05301004 case RPM_CMD_RX_DONE:
1005 qcom_glink_handle_rx_done(glink, param1, param2, false);
1006 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1007 break;
1008 case RPM_CMD_RX_DONE_W_REUSE:
1009 qcom_glink_handle_rx_done(glink, param1, param2, true);
1010 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1011 break;
Sricharan R27b9c5b2017-08-24 12:51:41 +05301012 case RPM_CMD_RX_INTENT_REQ_ACK:
1013 qcom_glink_handle_intent_req_ack(glink, param1, param2);
1014 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1015 break;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301016 default:
1017 dev_err(glink->dev, "unhandled rx cmd: %d\n", cmd);
1018 ret = -EINVAL;
1019 break;
1020 }
1021
1022 if (ret)
1023 break;
1024 }
1025
1026 return IRQ_HANDLED;
1027}
1028
1029/* Locally initiated rpmsg_create_ept */
1030static struct glink_channel *qcom_glink_create_local(struct qcom_glink *glink,
1031 const char *name)
1032{
1033 struct glink_channel *channel;
1034 int ret;
Sricharan R44f6df92017-08-24 12:51:33 +05301035 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301036
1037 channel = qcom_glink_alloc_channel(glink, name);
1038 if (IS_ERR(channel))
1039 return ERR_CAST(channel);
1040
1041 ret = qcom_glink_send_open_req(glink, channel);
1042 if (ret)
1043 goto release_channel;
1044
1045 ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
1046 if (!ret)
1047 goto err_timeout;
1048
1049 ret = wait_for_completion_timeout(&channel->open_req, 5 * HZ);
1050 if (!ret)
1051 goto err_timeout;
1052
1053 qcom_glink_send_open_ack(glink, channel);
1054
1055 return channel;
1056
1057err_timeout:
1058 /* qcom_glink_send_open_req() did register the channel in lcids*/
Sricharan R44f6df92017-08-24 12:51:33 +05301059 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301060 idr_remove(&glink->lcids, channel->lcid);
Sricharan R44f6df92017-08-24 12:51:33 +05301061 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301062
1063release_channel:
1064 /* Release qcom_glink_send_open_req() reference */
1065 kref_put(&channel->refcount, qcom_glink_channel_release);
1066 /* Release qcom_glink_alloc_channel() reference */
1067 kref_put(&channel->refcount, qcom_glink_channel_release);
1068
1069 return ERR_PTR(-ETIMEDOUT);
1070}
1071
1072/* Remote initiated rpmsg_create_ept */
1073static int qcom_glink_create_remote(struct qcom_glink *glink,
1074 struct glink_channel *channel)
1075{
1076 int ret;
1077
1078 qcom_glink_send_open_ack(glink, channel);
1079
1080 ret = qcom_glink_send_open_req(glink, channel);
1081 if (ret)
1082 goto close_link;
1083
1084 ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
1085 if (!ret) {
1086 ret = -ETIMEDOUT;
1087 goto close_link;
1088 }
1089
1090 return 0;
1091
1092close_link:
1093 /*
1094 * Send a close request to "undo" our open-ack. The close-ack will
1095 * release the last reference.
1096 */
1097 qcom_glink_send_close_req(glink, channel);
1098
1099 /* Release qcom_glink_send_open_req() reference */
1100 kref_put(&channel->refcount, qcom_glink_channel_release);
1101
1102 return ret;
1103}
1104
1105static struct rpmsg_endpoint *qcom_glink_create_ept(struct rpmsg_device *rpdev,
1106 rpmsg_rx_cb_t cb,
1107 void *priv,
1108 struct rpmsg_channel_info
1109 chinfo)
1110{
1111 struct glink_channel *parent = to_glink_channel(rpdev->ept);
1112 struct glink_channel *channel;
1113 struct qcom_glink *glink = parent->glink;
1114 struct rpmsg_endpoint *ept;
1115 const char *name = chinfo.name;
1116 int cid;
1117 int ret;
Sricharan R44f6df92017-08-24 12:51:33 +05301118 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301119
Sricharan R44f6df92017-08-24 12:51:33 +05301120 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301121 idr_for_each_entry(&glink->rcids, channel, cid) {
1122 if (!strcmp(channel->name, name))
1123 break;
1124 }
Sricharan R44f6df92017-08-24 12:51:33 +05301125 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301126
1127 if (!channel) {
1128 channel = qcom_glink_create_local(glink, name);
1129 if (IS_ERR(channel))
1130 return NULL;
1131 } else {
1132 ret = qcom_glink_create_remote(glink, channel);
1133 if (ret)
1134 return NULL;
1135 }
1136
1137 ept = &channel->ept;
1138 ept->rpdev = rpdev;
1139 ept->cb = cb;
1140 ept->priv = priv;
1141 ept->ops = &glink_endpoint_ops;
1142
1143 return ept;
1144}
1145
Sricharan R6bf68cb2017-08-24 12:51:38 +05301146static int qcom_glink_announce_create(struct rpmsg_device *rpdev)
1147{
1148 struct glink_channel *channel = to_glink_channel(rpdev->ept);
Chris Lew07389372017-10-26 15:28:55 -07001149 struct device_node *np = rpdev->dev.of_node;
Sricharan R6bf68cb2017-08-24 12:51:38 +05301150 struct qcom_glink *glink = channel->glink;
Chris Lew07389372017-10-26 15:28:55 -07001151 struct glink_core_rx_intent *intent;
1152 const struct property *prop = NULL;
1153 __be32 defaults[] = { cpu_to_be32(SZ_1K), cpu_to_be32(5) };
1154 int num_intents;
1155 int num_groups = 1;
1156 __be32 *val = defaults;
1157 int size;
Sricharan R6bf68cb2017-08-24 12:51:38 +05301158
Chris Lew07389372017-10-26 15:28:55 -07001159 if (glink->intentless)
1160 return 0;
Sricharan R6bf68cb2017-08-24 12:51:38 +05301161
Chris Lew07389372017-10-26 15:28:55 -07001162 prop = of_find_property(np, "qcom,intents", NULL);
1163 if (prop) {
1164 val = prop->value;
1165 num_groups = prop->length / sizeof(u32) / 2;
Sricharan R6bf68cb2017-08-24 12:51:38 +05301166 }
1167
Chris Lew07389372017-10-26 15:28:55 -07001168 /* Channel is now open, advertise base set of intents */
1169 while (num_groups--) {
1170 size = be32_to_cpup(val++);
1171 num_intents = be32_to_cpup(val++);
1172 while (num_intents--) {
1173 intent = qcom_glink_alloc_intent(glink, channel, size,
1174 true);
1175 if (!intent)
1176 break;
1177
1178 qcom_glink_advertise_intent(glink, channel, intent);
1179 }
1180 }
Sricharan R6bf68cb2017-08-24 12:51:38 +05301181 return 0;
1182}
1183
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301184static void qcom_glink_destroy_ept(struct rpmsg_endpoint *ept)
1185{
1186 struct glink_channel *channel = to_glink_channel(ept);
1187 struct qcom_glink *glink = channel->glink;
1188 unsigned long flags;
1189
1190 spin_lock_irqsave(&channel->recv_lock, flags);
1191 channel->ept.cb = NULL;
1192 spin_unlock_irqrestore(&channel->recv_lock, flags);
1193
1194 /* Decouple the potential rpdev from the channel */
1195 channel->rpdev = NULL;
1196
1197 qcom_glink_send_close_req(glink, channel);
1198}
1199
Sricharan R27b9c5b2017-08-24 12:51:41 +05301200static int qcom_glink_request_intent(struct qcom_glink *glink,
1201 struct glink_channel *channel,
1202 size_t size)
1203{
1204 struct {
1205 u16 id;
1206 u16 cid;
1207 u32 size;
1208 } __packed cmd;
1209
1210 int ret;
1211
1212 mutex_lock(&channel->intent_req_lock);
1213
1214 reinit_completion(&channel->intent_req_comp);
1215
1216 cmd.id = RPM_CMD_RX_INTENT_REQ;
1217 cmd.cid = channel->lcid;
1218 cmd.size = size;
1219
1220 ret = qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
1221 if (ret)
Dan Carpenter0a7480b2017-09-08 13:33:45 +03001222 goto unlock;
Sricharan R27b9c5b2017-08-24 12:51:41 +05301223
1224 ret = wait_for_completion_timeout(&channel->intent_req_comp, 10 * HZ);
1225 if (!ret) {
1226 dev_err(glink->dev, "intent request timed out\n");
1227 ret = -ETIMEDOUT;
1228 } else {
1229 ret = channel->intent_req_result ? 0 : -ECANCELED;
1230 }
1231
Dan Carpenter0a7480b2017-09-08 13:33:45 +03001232unlock:
Sricharan R27b9c5b2017-08-24 12:51:41 +05301233 mutex_unlock(&channel->intent_req_lock);
1234 return ret;
1235}
1236
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301237static int __qcom_glink_send(struct glink_channel *channel,
1238 void *data, int len, bool wait)
1239{
1240 struct qcom_glink *glink = channel->glink;
Sricharan R11cb45a2017-08-24 12:51:40 +05301241 struct glink_core_rx_intent *intent = NULL;
1242 struct glink_core_rx_intent *tmp;
1243 int iid = 0;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301244 struct {
1245 struct glink_msg msg;
1246 __le32 chunk_size;
1247 __le32 left_size;
1248 } __packed req;
Sricharan R11cb45a2017-08-24 12:51:40 +05301249 int ret;
1250 unsigned long flags;
1251
1252 if (!glink->intentless) {
Sricharan R27b9c5b2017-08-24 12:51:41 +05301253 while (!intent) {
Sricharan R11cb45a2017-08-24 12:51:40 +05301254 spin_lock_irqsave(&channel->intent_lock, flags);
1255 idr_for_each_entry(&channel->riids, tmp, iid) {
1256 if (tmp->size >= len && !tmp->in_use) {
Chris Lew29031872017-10-26 15:28:56 -07001257 if (!intent)
1258 intent = tmp;
1259 else if (intent->size > tmp->size)
1260 intent = tmp;
1261 if (intent->size == len)
1262 break;
Sricharan R11cb45a2017-08-24 12:51:40 +05301263 }
1264 }
Chris Lew29031872017-10-26 15:28:56 -07001265 if (intent)
1266 intent->in_use = true;
Sricharan R11cb45a2017-08-24 12:51:40 +05301267 spin_unlock_irqrestore(&channel->intent_lock, flags);
1268
1269 /* We found an available intent */
Sricharan R27b9c5b2017-08-24 12:51:41 +05301270 if (intent)
1271 break;
1272
1273 if (!wait)
Sricharan R11cb45a2017-08-24 12:51:40 +05301274 return -EBUSY;
Sricharan R27b9c5b2017-08-24 12:51:41 +05301275
1276 ret = qcom_glink_request_intent(glink, channel, len);
1277 if (ret < 0)
1278 return ret;
Sricharan R11cb45a2017-08-24 12:51:40 +05301279 }
1280
1281 iid = intent->id;
1282 }
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301283
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301284 req.msg.cmd = cpu_to_le16(RPM_CMD_TX_DATA);
1285 req.msg.param1 = cpu_to_le16(channel->lcid);
Sricharan R11cb45a2017-08-24 12:51:40 +05301286 req.msg.param2 = cpu_to_le32(iid);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301287 req.chunk_size = cpu_to_le32(len);
1288 req.left_size = cpu_to_le32(0);
1289
Sricharan R11cb45a2017-08-24 12:51:40 +05301290 ret = qcom_glink_tx(glink, &req, sizeof(req), data, len, wait);
1291
1292 /* Mark intent available if we failed */
Colin Ian King906cf292017-09-03 13:46:14 +01001293 if (ret && intent)
Sricharan R11cb45a2017-08-24 12:51:40 +05301294 intent->in_use = false;
1295
1296 return ret;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301297}
1298
1299static int qcom_glink_send(struct rpmsg_endpoint *ept, void *data, int len)
1300{
1301 struct glink_channel *channel = to_glink_channel(ept);
1302
1303 return __qcom_glink_send(channel, data, len, true);
1304}
1305
1306static int qcom_glink_trysend(struct rpmsg_endpoint *ept, void *data, int len)
1307{
1308 struct glink_channel *channel = to_glink_channel(ept);
1309
1310 return __qcom_glink_send(channel, data, len, false);
1311}
1312
1313/*
1314 * Finds the device_node for the glink child interested in this channel.
1315 */
1316static struct device_node *qcom_glink_match_channel(struct device_node *node,
1317 const char *channel)
1318{
1319 struct device_node *child;
1320 const char *name;
1321 const char *key;
1322 int ret;
1323
1324 for_each_available_child_of_node(node, child) {
1325 key = "qcom,glink-channels";
1326 ret = of_property_read_string(child, key, &name);
1327 if (ret)
1328 continue;
1329
1330 if (strcmp(name, channel) == 0)
1331 return child;
1332 }
1333
1334 return NULL;
1335}
1336
1337static const struct rpmsg_device_ops glink_device_ops = {
1338 .create_ept = qcom_glink_create_ept,
Sricharan R6bf68cb2017-08-24 12:51:38 +05301339 .announce_create = qcom_glink_announce_create,
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301340};
1341
1342static const struct rpmsg_endpoint_ops glink_endpoint_ops = {
1343 .destroy_ept = qcom_glink_destroy_ept,
1344 .send = qcom_glink_send,
1345 .trysend = qcom_glink_trysend,
1346};
1347
1348static void qcom_glink_rpdev_release(struct device *dev)
1349{
1350 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
1351 struct glink_channel *channel = to_glink_channel(rpdev->ept);
1352
1353 channel->rpdev = NULL;
1354 kfree(rpdev);
1355}
1356
1357static int qcom_glink_rx_open(struct qcom_glink *glink, unsigned int rcid,
1358 char *name)
1359{
1360 struct glink_channel *channel;
1361 struct rpmsg_device *rpdev;
1362 bool create_device = false;
1363 struct device_node *node;
1364 int lcid;
1365 int ret;
Sricharan R44f6df92017-08-24 12:51:33 +05301366 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301367
Sricharan R44f6df92017-08-24 12:51:33 +05301368 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301369 idr_for_each_entry(&glink->lcids, channel, lcid) {
1370 if (!strcmp(channel->name, name))
1371 break;
1372 }
Sricharan R44f6df92017-08-24 12:51:33 +05301373 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301374
1375 if (!channel) {
1376 channel = qcom_glink_alloc_channel(glink, name);
1377 if (IS_ERR(channel))
1378 return PTR_ERR(channel);
1379
1380 /* The opening dance was initiated by the remote */
1381 create_device = true;
1382 }
1383
Sricharan R44f6df92017-08-24 12:51:33 +05301384 spin_lock_irqsave(&glink->idr_lock, flags);
1385 ret = idr_alloc(&glink->rcids, channel, rcid, rcid + 1, GFP_ATOMIC);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301386 if (ret < 0) {
1387 dev_err(glink->dev, "Unable to insert channel into rcid list\n");
Sricharan R44f6df92017-08-24 12:51:33 +05301388 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301389 goto free_channel;
1390 }
1391 channel->rcid = ret;
Sricharan R44f6df92017-08-24 12:51:33 +05301392 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301393
1394 complete(&channel->open_req);
1395
1396 if (create_device) {
1397 rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL);
1398 if (!rpdev) {
1399 ret = -ENOMEM;
1400 goto rcid_remove;
1401 }
1402
1403 rpdev->ept = &channel->ept;
1404 strncpy(rpdev->id.name, name, RPMSG_NAME_SIZE);
1405 rpdev->src = RPMSG_ADDR_ANY;
1406 rpdev->dst = RPMSG_ADDR_ANY;
1407 rpdev->ops = &glink_device_ops;
1408
1409 node = qcom_glink_match_channel(glink->dev->of_node, name);
1410 rpdev->dev.of_node = node;
1411 rpdev->dev.parent = glink->dev;
1412 rpdev->dev.release = qcom_glink_rpdev_release;
1413
1414 ret = rpmsg_register_device(rpdev);
1415 if (ret)
1416 goto free_rpdev;
1417
1418 channel->rpdev = rpdev;
1419 }
1420
1421 return 0;
1422
1423free_rpdev:
1424 kfree(rpdev);
1425rcid_remove:
Sricharan R44f6df92017-08-24 12:51:33 +05301426 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301427 idr_remove(&glink->rcids, channel->rcid);
1428 channel->rcid = 0;
Sricharan R44f6df92017-08-24 12:51:33 +05301429 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301430free_channel:
1431 /* Release the reference, iff we took it */
1432 if (create_device)
1433 kref_put(&channel->refcount, qcom_glink_channel_release);
1434
1435 return ret;
1436}
1437
1438static void qcom_glink_rx_close(struct qcom_glink *glink, unsigned int rcid)
1439{
1440 struct rpmsg_channel_info chinfo;
1441 struct glink_channel *channel;
Sricharan R44f6df92017-08-24 12:51:33 +05301442 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301443
Sricharan R44f6df92017-08-24 12:51:33 +05301444 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301445 channel = idr_find(&glink->rcids, rcid);
Sricharan R44f6df92017-08-24 12:51:33 +05301446 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301447 if (WARN(!channel, "close request on unknown channel\n"))
1448 return;
1449
Sricharan R1d2ea362017-08-24 12:51:37 +05301450 /* cancel pending rx_done work */
1451 cancel_work_sync(&channel->intent_work);
1452
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301453 if (channel->rpdev) {
1454 strncpy(chinfo.name, channel->name, sizeof(chinfo.name));
1455 chinfo.src = RPMSG_ADDR_ANY;
1456 chinfo.dst = RPMSG_ADDR_ANY;
1457
1458 rpmsg_unregister_device(glink->dev, &chinfo);
1459 }
1460
1461 qcom_glink_send_close_ack(glink, channel->rcid);
1462
Sricharan R44f6df92017-08-24 12:51:33 +05301463 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301464 idr_remove(&glink->rcids, channel->rcid);
1465 channel->rcid = 0;
Sricharan R44f6df92017-08-24 12:51:33 +05301466 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301467
1468 kref_put(&channel->refcount, qcom_glink_channel_release);
1469}
1470
1471static void qcom_glink_rx_close_ack(struct qcom_glink *glink, unsigned int lcid)
1472{
1473 struct glink_channel *channel;
Sricharan R44f6df92017-08-24 12:51:33 +05301474 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301475
Sricharan R44f6df92017-08-24 12:51:33 +05301476 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301477 channel = idr_find(&glink->lcids, lcid);
Sricharan R44f6df92017-08-24 12:51:33 +05301478 if (WARN(!channel, "close ack on unknown channel\n")) {
1479 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301480 return;
Sricharan R44f6df92017-08-24 12:51:33 +05301481 }
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301482
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301483 idr_remove(&glink->lcids, channel->lcid);
1484 channel->lcid = 0;
Sricharan R44f6df92017-08-24 12:51:33 +05301485 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301486
1487 kref_put(&channel->refcount, qcom_glink_channel_release);
1488}
1489
1490static void qcom_glink_work(struct work_struct *work)
1491{
1492 struct qcom_glink *glink = container_of(work, struct qcom_glink,
1493 rx_work);
1494 struct glink_defer_cmd *dcmd;
1495 struct glink_msg *msg;
1496 unsigned long flags;
1497 unsigned int param1;
1498 unsigned int param2;
1499 unsigned int cmd;
1500
1501 for (;;) {
1502 spin_lock_irqsave(&glink->rx_lock, flags);
1503 if (list_empty(&glink->rx_queue)) {
1504 spin_unlock_irqrestore(&glink->rx_lock, flags);
1505 break;
1506 }
1507 dcmd = list_first_entry(&glink->rx_queue,
1508 struct glink_defer_cmd, node);
1509 list_del(&dcmd->node);
1510 spin_unlock_irqrestore(&glink->rx_lock, flags);
1511
1512 msg = &dcmd->msg;
1513 cmd = le16_to_cpu(msg->cmd);
1514 param1 = le16_to_cpu(msg->param1);
1515 param2 = le32_to_cpu(msg->param2);
1516
1517 switch (cmd) {
1518 case RPM_CMD_VERSION:
Sricharan Rd31ad612017-08-24 12:51:32 +05301519 qcom_glink_receive_version(glink, param1, param2);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301520 break;
1521 case RPM_CMD_VERSION_ACK:
Sricharan Rd31ad612017-08-24 12:51:32 +05301522 qcom_glink_receive_version_ack(glink, param1, param2);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301523 break;
1524 case RPM_CMD_OPEN:
1525 qcom_glink_rx_open(glink, param1, msg->data);
1526 break;
1527 case RPM_CMD_CLOSE:
1528 qcom_glink_rx_close(glink, param1);
1529 break;
1530 case RPM_CMD_CLOSE_ACK:
1531 qcom_glink_rx_close_ack(glink, param1);
1532 break;
Sricharan R933b45d2017-08-24 12:51:34 +05301533 case RPM_CMD_RX_INTENT_REQ:
1534 qcom_glink_handle_intent_req(glink, param1, param2);
1535 break;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301536 default:
1537 WARN(1, "Unknown defer object %d\n", cmd);
1538 break;
1539 }
1540
1541 kfree(dcmd);
1542 }
1543}
1544
1545struct qcom_glink *qcom_glink_native_probe(struct device *dev,
Sricharan Rd31ad612017-08-24 12:51:32 +05301546 unsigned long features,
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301547 struct qcom_glink_pipe *rx,
Sricharan R933b45d2017-08-24 12:51:34 +05301548 struct qcom_glink_pipe *tx,
1549 bool intentless)
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301550{
1551 int irq;
1552 int ret;
1553 struct qcom_glink *glink;
1554
1555 glink = devm_kzalloc(dev, sizeof(*glink), GFP_KERNEL);
1556 if (!glink)
1557 return ERR_PTR(-ENOMEM);
1558
1559 glink->dev = dev;
1560 glink->tx_pipe = tx;
1561 glink->rx_pipe = rx;
1562
Sricharan Rd31ad612017-08-24 12:51:32 +05301563 glink->features = features;
Sricharan R933b45d2017-08-24 12:51:34 +05301564 glink->intentless = intentless;
Sricharan Rd31ad612017-08-24 12:51:32 +05301565
Bjorn Andersson29fc9b32018-02-13 11:04:04 -08001566 spin_lock_init(&glink->tx_lock);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301567 spin_lock_init(&glink->rx_lock);
1568 INIT_LIST_HEAD(&glink->rx_queue);
1569 INIT_WORK(&glink->rx_work, qcom_glink_work);
1570
Sricharan R44f6df92017-08-24 12:51:33 +05301571 spin_lock_init(&glink->idr_lock);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301572 idr_init(&glink->lcids);
1573 idr_init(&glink->rcids);
1574
1575 glink->mbox_client.dev = dev;
Bjorn Andersson38a9acb2017-11-15 22:08:33 -08001576 glink->mbox_client.knows_txdone = true;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301577 glink->mbox_chan = mbox_request_channel(&glink->mbox_client, 0);
1578 if (IS_ERR(glink->mbox_chan)) {
1579 if (PTR_ERR(glink->mbox_chan) != -EPROBE_DEFER)
1580 dev_err(dev, "failed to acquire IPC channel\n");
1581 return ERR_CAST(glink->mbox_chan);
1582 }
1583
1584 irq = of_irq_get(dev->of_node, 0);
1585 ret = devm_request_irq(dev, irq,
1586 qcom_glink_native_intr,
1587 IRQF_NO_SUSPEND | IRQF_SHARED,
1588 "glink-native", glink);
1589 if (ret) {
1590 dev_err(dev, "failed to request IRQ\n");
1591 return ERR_PTR(ret);
1592 }
1593
1594 glink->irq = irq;
1595
1596 ret = qcom_glink_send_version(glink);
1597 if (ret)
1598 return ERR_PTR(ret);
1599
1600 return glink;
1601}
Bjorn Anderssonf8ff9772017-08-31 09:27:35 -07001602EXPORT_SYMBOL_GPL(qcom_glink_native_probe);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301603
1604static int qcom_glink_remove_device(struct device *dev, void *data)
1605{
1606 device_unregister(dev);
1607
1608 return 0;
1609}
1610
1611void qcom_glink_native_remove(struct qcom_glink *glink)
1612{
1613 struct glink_channel *channel;
1614 int cid;
1615 int ret;
Sricharan R44f6df92017-08-24 12:51:33 +05301616 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301617
1618 disable_irq(glink->irq);
1619 cancel_work_sync(&glink->rx_work);
1620
1621 ret = device_for_each_child(glink->dev, NULL, qcom_glink_remove_device);
1622 if (ret)
1623 dev_warn(glink->dev, "Can't remove GLINK devices: %d\n", ret);
1624
Sricharan R44f6df92017-08-24 12:51:33 +05301625 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301626 /* Release any defunct local channels, waiting for close-ack */
1627 idr_for_each_entry(&glink->lcids, channel, cid)
1628 kref_put(&channel->refcount, qcom_glink_channel_release);
1629
1630 idr_destroy(&glink->lcids);
1631 idr_destroy(&glink->rcids);
Sricharan R44f6df92017-08-24 12:51:33 +05301632 spin_unlock_irqrestore(&glink->idr_lock, flags);
Sricharan R76cf1102017-08-24 12:51:29 +05301633 mbox_free_channel(glink->mbox_chan);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301634}
Bjorn Anderssonf8ff9772017-08-31 09:27:35 -07001635EXPORT_SYMBOL_GPL(qcom_glink_native_remove);
Bjorn Anderssoncaf989c2017-08-24 12:51:30 +05301636
1637void qcom_glink_native_unregister(struct qcom_glink *glink)
1638{
1639 device_unregister(glink->dev);
1640}
Bjorn Anderssonf8ff9772017-08-31 09:27:35 -07001641EXPORT_SYMBOL_GPL(qcom_glink_native_unregister);
Bjorn Andersson1e0d5612017-11-14 21:45:34 -08001642
1643MODULE_DESCRIPTION("Qualcomm GLINK driver");
1644MODULE_LICENSE("GPL v2");