blob: e2ce4e638258b7a64de8e9f41e6167f09aaf8c74 [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
Srinivas Kandagatla6f0b9582018-06-18 13:33:38 +010043 * @data: payload of the message
Bjorn Andersson835764dd2017-08-24 12:51:26 +053044 *
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 *
Srinivas Kandagatla6f0b9582018-06-18 13:33:38 +010059 * @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 * @node: list node
Sricharan R933b45d2017-08-24 12:51:34 +053066 */
67struct glink_core_rx_intent {
68 void *data;
69 u32 id;
70 size_t size;
71 bool reuse;
72 bool in_use;
73 u32 offset;
Sricharan R1d2ea362017-08-24 12:51:37 +053074
75 struct list_head node;
Sricharan R933b45d2017-08-24 12:51:34 +053076};
77
78/**
Bjorn Andersson835764dd2017-08-24 12:51:26 +053079 * struct qcom_glink - driver context, relates to one remote subsystem
80 * @dev: reference to the associated struct device
81 * @mbox_client: mailbox client
82 * @mbox_chan: mailbox channel
83 * @rx_pipe: pipe object for receive FIFO
84 * @tx_pipe: pipe object for transmit FIFO
85 * @irq: IRQ for signaling incoming events
86 * @rx_work: worker for handling received control messages
87 * @rx_lock: protects the @rx_queue
88 * @rx_queue: queue of received control messages to be processed in @rx_work
89 * @tx_lock: synchronizes operations on the tx fifo
90 * @idr_lock: synchronizes @lcids and @rcids modifications
91 * @lcids: idr of all channels with a known local channel id
92 * @rcids: idr of all channels with a known remote channel id
Srinivas Kandagatla6f0b9582018-06-18 13:33:38 +010093 * @features: remote features
94 * @intentless: flag to indicate that there is no intent
Bjorn Andersson835764dd2017-08-24 12:51:26 +053095 */
96struct qcom_glink {
97 struct device *dev;
98
Chris Lew475452f2018-07-27 17:47:22 +053099 const char *name;
100
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530101 struct mbox_client mbox_client;
102 struct mbox_chan *mbox_chan;
103
104 struct qcom_glink_pipe *rx_pipe;
105 struct qcom_glink_pipe *tx_pipe;
106
107 int irq;
108
109 struct work_struct rx_work;
110 spinlock_t rx_lock;
111 struct list_head rx_queue;
112
Bjorn Andersson29fc9b32018-02-13 11:04:04 -0800113 spinlock_t tx_lock;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530114
Sricharan R44f6df92017-08-24 12:51:33 +0530115 spinlock_t idr_lock;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530116 struct idr lcids;
117 struct idr rcids;
Sricharan Rd31ad612017-08-24 12:51:32 +0530118 unsigned long features;
119
120 bool intentless;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530121};
122
123enum {
124 GLINK_STATE_CLOSED,
125 GLINK_STATE_OPENING,
126 GLINK_STATE_OPEN,
127 GLINK_STATE_CLOSING,
128};
129
130/**
131 * struct glink_channel - internal representation of a channel
132 * @rpdev: rpdev reference, only used for primary endpoints
133 * @ept: rpmsg endpoint this channel is associated with
134 * @glink: qcom_glink context handle
135 * @refcount: refcount for the channel object
136 * @recv_lock: guard for @ept.cb
137 * @name: unique channel name/identifier
138 * @lcid: channel id, in local space
139 * @rcid: channel id, in remote space
Sricharan Rdacbb352017-08-24 12:51:39 +0530140 * @intent_lock: lock for protection of @liids, @riids
Sricharan R933b45d2017-08-24 12:51:34 +0530141 * @liids: idr of all local intents
Sricharan Rdacbb352017-08-24 12:51:39 +0530142 * @riids: idr of all remote intents
Sricharan R1d2ea362017-08-24 12:51:37 +0530143 * @intent_work: worker responsible for transmitting rx_done packets
144 * @done_intents: list of intents that needs to be announced rx_done
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530145 * @buf: receive buffer, for gathering fragments
146 * @buf_offset: write offset in @buf
147 * @buf_size: size of current @buf
148 * @open_ack: completed once remote has acked the open-request
149 * @open_req: completed once open-request has been received
Sricharan R27b9c5b2017-08-24 12:51:41 +0530150 * @intent_req_lock: Synchronises multiple intent requests
151 * @intent_req_result: Result of intent request
152 * @intent_req_comp: Completion for intent_req signalling
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530153 */
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;
Sricharan R27b9c5b2017-08-24 12:51:41 +0530180
181 struct mutex intent_req_lock;
182 bool intent_req_result;
183 struct completion intent_req_comp;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530184};
185
186#define to_glink_channel(_ept) container_of(_ept, struct glink_channel, ept)
187
188static const struct rpmsg_endpoint_ops glink_endpoint_ops;
189
190#define RPM_CMD_VERSION 0
191#define RPM_CMD_VERSION_ACK 1
192#define RPM_CMD_OPEN 2
193#define RPM_CMD_CLOSE 3
194#define RPM_CMD_OPEN_ACK 4
Sricharan R933b45d2017-08-24 12:51:34 +0530195#define RPM_CMD_INTENT 5
Sricharan R1d2ea362017-08-24 12:51:37 +0530196#define RPM_CMD_RX_DONE 6
Sricharan R933b45d2017-08-24 12:51:34 +0530197#define RPM_CMD_RX_INTENT_REQ 7
198#define RPM_CMD_RX_INTENT_REQ_ACK 8
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530199#define RPM_CMD_TX_DATA 9
200#define RPM_CMD_CLOSE_ACK 11
201#define RPM_CMD_TX_DATA_CONT 12
202#define RPM_CMD_READ_NOTIF 13
Sricharan R1d2ea362017-08-24 12:51:37 +0530203#define RPM_CMD_RX_DONE_W_REUSE 14
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530204
205#define GLINK_FEATURE_INTENTLESS BIT(1)
206
Sricharan R1d2ea362017-08-24 12:51:37 +0530207static void qcom_glink_rx_done_work(struct work_struct *work);
208
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530209static struct glink_channel *qcom_glink_alloc_channel(struct qcom_glink *glink,
210 const char *name)
211{
212 struct glink_channel *channel;
213
214 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
215 if (!channel)
216 return ERR_PTR(-ENOMEM);
217
218 /* Setup glink internal glink_channel data */
219 spin_lock_init(&channel->recv_lock);
Sricharan R933b45d2017-08-24 12:51:34 +0530220 spin_lock_init(&channel->intent_lock);
Wei Yongjunfb416f62018-01-02 11:47:03 +0000221 mutex_init(&channel->intent_req_lock);
Sricharan R1d2ea362017-08-24 12:51:37 +0530222
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530223 channel->glink = glink;
224 channel->name = kstrdup(name, GFP_KERNEL);
225
226 init_completion(&channel->open_req);
227 init_completion(&channel->open_ack);
Arun Kumar Neelakantam2394fac2017-10-30 11:11:24 +0530228 init_completion(&channel->intent_req_comp);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530229
Sricharan R1d2ea362017-08-24 12:51:37 +0530230 INIT_LIST_HEAD(&channel->done_intents);
231 INIT_WORK(&channel->intent_work, qcom_glink_rx_done_work);
232
Sricharan R933b45d2017-08-24 12:51:34 +0530233 idr_init(&channel->liids);
Sricharan Rdacbb352017-08-24 12:51:39 +0530234 idr_init(&channel->riids);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530235 kref_init(&channel->refcount);
236
237 return channel;
238}
239
240static void qcom_glink_channel_release(struct kref *ref)
241{
242 struct glink_channel *channel = container_of(ref, struct glink_channel,
243 refcount);
Sricharan R933b45d2017-08-24 12:51:34 +0530244 unsigned long flags;
245
246 spin_lock_irqsave(&channel->intent_lock, flags);
247 idr_destroy(&channel->liids);
Sricharan Rdacbb352017-08-24 12:51:39 +0530248 idr_destroy(&channel->riids);
Sricharan R933b45d2017-08-24 12:51:34 +0530249 spin_unlock_irqrestore(&channel->intent_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530250
251 kfree(channel->name);
252 kfree(channel);
253}
254
255static size_t qcom_glink_rx_avail(struct qcom_glink *glink)
256{
257 return glink->rx_pipe->avail(glink->rx_pipe);
258}
259
260static void qcom_glink_rx_peak(struct qcom_glink *glink,
Bjorn Anderssonb88eee92017-08-24 12:51:36 +0530261 void *data, unsigned int offset, size_t count)
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530262{
Bjorn Anderssonb88eee92017-08-24 12:51:36 +0530263 glink->rx_pipe->peak(glink->rx_pipe, data, offset, count);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530264}
265
266static void qcom_glink_rx_advance(struct qcom_glink *glink, size_t count)
267{
268 glink->rx_pipe->advance(glink->rx_pipe, count);
269}
270
271static size_t qcom_glink_tx_avail(struct qcom_glink *glink)
272{
273 return glink->tx_pipe->avail(glink->tx_pipe);
274}
275
276static void qcom_glink_tx_write(struct qcom_glink *glink,
277 const void *hdr, size_t hlen,
278 const void *data, size_t dlen)
279{
280 glink->tx_pipe->write(glink->tx_pipe, hdr, hlen, data, dlen);
281}
282
283static int qcom_glink_tx(struct qcom_glink *glink,
284 const void *hdr, size_t hlen,
285 const void *data, size_t dlen, bool wait)
286{
287 unsigned int tlen = hlen + dlen;
Bjorn Andersson29fc9b32018-02-13 11:04:04 -0800288 unsigned long flags;
289 int ret = 0;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530290
291 /* Reject packets that are too big */
292 if (tlen >= glink->tx_pipe->length)
293 return -EINVAL;
294
Bjorn Andersson29fc9b32018-02-13 11:04:04 -0800295 spin_lock_irqsave(&glink->tx_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530296
297 while (qcom_glink_tx_avail(glink) < tlen) {
298 if (!wait) {
Sricharan Ra7df9df2017-08-24 12:51:28 +0530299 ret = -EAGAIN;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530300 goto out;
301 }
302
Bjorn Andersson29fc9b32018-02-13 11:04:04 -0800303 /* Wait without holding the tx_lock */
304 spin_unlock_irqrestore(&glink->tx_lock, flags);
305
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530306 usleep_range(10000, 15000);
Bjorn Andersson29fc9b32018-02-13 11:04:04 -0800307
308 spin_lock_irqsave(&glink->tx_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530309 }
310
311 qcom_glink_tx_write(glink, hdr, hlen, data, dlen);
312
313 mbox_send_message(glink->mbox_chan, NULL);
314 mbox_client_txdone(glink->mbox_chan, 0);
315
316out:
Bjorn Andersson29fc9b32018-02-13 11:04:04 -0800317 spin_unlock_irqrestore(&glink->tx_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530318
319 return ret;
320}
321
322static int qcom_glink_send_version(struct qcom_glink *glink)
323{
324 struct glink_msg msg;
325
326 msg.cmd = cpu_to_le16(RPM_CMD_VERSION);
Sricharan Rd31ad612017-08-24 12:51:32 +0530327 msg.param1 = cpu_to_le16(GLINK_VERSION_1);
328 msg.param2 = cpu_to_le32(glink->features);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530329
330 return qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
331}
332
333static void qcom_glink_send_version_ack(struct qcom_glink *glink)
334{
335 struct glink_msg msg;
336
337 msg.cmd = cpu_to_le16(RPM_CMD_VERSION_ACK);
Sricharan Rd31ad612017-08-24 12:51:32 +0530338 msg.param1 = cpu_to_le16(GLINK_VERSION_1);
339 msg.param2 = cpu_to_le32(glink->features);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530340
341 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
342}
343
344static void qcom_glink_send_open_ack(struct qcom_glink *glink,
345 struct glink_channel *channel)
346{
347 struct glink_msg msg;
348
349 msg.cmd = cpu_to_le16(RPM_CMD_OPEN_ACK);
350 msg.param1 = cpu_to_le16(channel->rcid);
351 msg.param2 = cpu_to_le32(0);
352
353 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
354}
355
Sricharan R27b9c5b2017-08-24 12:51:41 +0530356static void qcom_glink_handle_intent_req_ack(struct qcom_glink *glink,
357 unsigned int cid, bool granted)
358{
359 struct glink_channel *channel;
360 unsigned long flags;
361
362 spin_lock_irqsave(&glink->idr_lock, flags);
363 channel = idr_find(&glink->rcids, cid);
364 spin_unlock_irqrestore(&glink->idr_lock, flags);
365 if (!channel) {
366 dev_err(glink->dev, "unable to find channel\n");
367 return;
368 }
369
370 channel->intent_req_result = granted;
371 complete(&channel->intent_req_comp);
372}
373
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530374/**
375 * qcom_glink_send_open_req() - send a RPM_CMD_OPEN request to the remote
376 * @glink: Ptr to the glink edge
377 * @channel: Ptr to the channel that the open req is sent
378 *
379 * Allocates a local channel id and sends a RPM_CMD_OPEN message to the remote.
380 * Will return with refcount held, regardless of outcome.
381 *
382 * Returns 0 on success, negative errno otherwise.
383 */
384static int qcom_glink_send_open_req(struct qcom_glink *glink,
385 struct glink_channel *channel)
386{
387 struct {
388 struct glink_msg msg;
389 u8 name[GLINK_NAME_SIZE];
390 } __packed req;
391 int name_len = strlen(channel->name) + 1;
392 int req_len = ALIGN(sizeof(req.msg) + name_len, 8);
393 int ret;
Sricharan R44f6df92017-08-24 12:51:33 +0530394 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530395
396 kref_get(&channel->refcount);
397
Sricharan R44f6df92017-08-24 12:51:33 +0530398 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530399 ret = idr_alloc_cyclic(&glink->lcids, channel,
400 RPM_GLINK_CID_MIN, RPM_GLINK_CID_MAX,
Sricharan R44f6df92017-08-24 12:51:33 +0530401 GFP_ATOMIC);
402 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530403 if (ret < 0)
404 return ret;
405
406 channel->lcid = ret;
407
408 req.msg.cmd = cpu_to_le16(RPM_CMD_OPEN);
409 req.msg.param1 = cpu_to_le16(channel->lcid);
410 req.msg.param2 = cpu_to_le32(name_len);
411 strcpy(req.name, channel->name);
412
413 ret = qcom_glink_tx(glink, &req, req_len, NULL, 0, true);
414 if (ret)
415 goto remove_idr;
416
417 return 0;
418
419remove_idr:
Sricharan R44f6df92017-08-24 12:51:33 +0530420 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530421 idr_remove(&glink->lcids, channel->lcid);
422 channel->lcid = 0;
Sricharan R44f6df92017-08-24 12:51:33 +0530423 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530424
425 return ret;
426}
427
428static void qcom_glink_send_close_req(struct qcom_glink *glink,
429 struct glink_channel *channel)
430{
431 struct glink_msg req;
432
433 req.cmd = cpu_to_le16(RPM_CMD_CLOSE);
434 req.param1 = cpu_to_le16(channel->lcid);
435 req.param2 = 0;
436
437 qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
438}
439
440static void qcom_glink_send_close_ack(struct qcom_glink *glink,
441 unsigned int rcid)
442{
443 struct glink_msg req;
444
445 req.cmd = cpu_to_le16(RPM_CMD_CLOSE_ACK);
446 req.param1 = cpu_to_le16(rcid);
447 req.param2 = 0;
448
449 qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
450}
451
Sricharan R1d2ea362017-08-24 12:51:37 +0530452static void qcom_glink_rx_done_work(struct work_struct *work)
453{
454 struct glink_channel *channel = container_of(work, struct glink_channel,
455 intent_work);
456 struct qcom_glink *glink = channel->glink;
457 struct glink_core_rx_intent *intent, *tmp;
458 struct {
459 u16 id;
460 u16 lcid;
461 u32 liid;
462 } __packed cmd;
463
464 unsigned int cid = channel->lcid;
465 unsigned int iid;
466 bool reuse;
467 unsigned long flags;
468
469 spin_lock_irqsave(&channel->intent_lock, flags);
470 list_for_each_entry_safe(intent, tmp, &channel->done_intents, node) {
471 list_del(&intent->node);
472 spin_unlock_irqrestore(&channel->intent_lock, flags);
473 iid = intent->id;
474 reuse = intent->reuse;
475
476 cmd.id = reuse ? RPM_CMD_RX_DONE_W_REUSE : RPM_CMD_RX_DONE;
477 cmd.lcid = cid;
478 cmd.liid = iid;
479
480 qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
481 if (!reuse) {
482 kfree(intent->data);
483 kfree(intent);
484 }
485 spin_lock_irqsave(&channel->intent_lock, flags);
486 }
487 spin_unlock_irqrestore(&channel->intent_lock, flags);
488}
489
490static void qcom_glink_rx_done(struct qcom_glink *glink,
491 struct glink_channel *channel,
492 struct glink_core_rx_intent *intent)
493{
494 /* We don't send RX_DONE to intentless systems */
495 if (glink->intentless) {
496 kfree(intent->data);
497 kfree(intent);
498 return;
499 }
500
501 /* Take it off the tree of receive intents */
502 if (!intent->reuse) {
503 spin_lock(&channel->intent_lock);
504 idr_remove(&channel->liids, intent->id);
505 spin_unlock(&channel->intent_lock);
506 }
507
508 /* Schedule the sending of a rx_done indication */
509 spin_lock(&channel->intent_lock);
510 list_add_tail(&intent->node, &channel->done_intents);
511 spin_unlock(&channel->intent_lock);
512
513 schedule_work(&channel->intent_work);
514}
515
Sricharan Rd31ad612017-08-24 12:51:32 +0530516/**
517 * qcom_glink_receive_version() - receive version/features from remote system
518 *
519 * @glink: pointer to transport interface
Srinivas Kandagatla6f0b9582018-06-18 13:33:38 +0100520 * @version: remote version
521 * @features: remote features
Sricharan Rd31ad612017-08-24 12:51:32 +0530522 *
523 * This function is called in response to a remote-initiated version/feature
524 * negotiation sequence.
525 */
526static void qcom_glink_receive_version(struct qcom_glink *glink,
527 u32 version,
528 u32 features)
529{
530 switch (version) {
531 case 0:
532 break;
533 case GLINK_VERSION_1:
534 glink->features &= features;
535 /* FALLTHROUGH */
536 default:
537 qcom_glink_send_version_ack(glink);
538 break;
539 }
540}
541
542/**
543 * qcom_glink_receive_version_ack() - receive negotiation ack from remote system
544 *
545 * @glink: pointer to transport interface
Srinivas Kandagatla6f0b9582018-06-18 13:33:38 +0100546 * @version: remote version response
547 * @features: remote features response
Sricharan Rd31ad612017-08-24 12:51:32 +0530548 *
549 * This function is called in response to a local-initiated version/feature
550 * negotiation sequence and is the counter-offer from the remote side based
551 * upon the initial version and feature set requested.
552 */
553static void qcom_glink_receive_version_ack(struct qcom_glink *glink,
554 u32 version,
555 u32 features)
556{
557 switch (version) {
558 case 0:
559 /* Version negotiation failed */
560 break;
561 case GLINK_VERSION_1:
562 if (features == glink->features)
563 break;
564
565 glink->features &= features;
566 /* FALLTHROUGH */
567 default:
568 qcom_glink_send_version(glink);
569 break;
570 }
571}
572
Sricharan R933b45d2017-08-24 12:51:34 +0530573/**
574 * qcom_glink_send_intent_req_ack() - convert an rx intent request ack cmd to
Srinivas Kandagatla6f0b9582018-06-18 13:33:38 +0100575 * wire format and transmit
Sricharan R933b45d2017-08-24 12:51:34 +0530576 * @glink: The transport to transmit on.
577 * @channel: The glink channel
578 * @granted: The request response to encode.
579 *
580 * Return: 0 on success or standard Linux error code.
581 */
582static int qcom_glink_send_intent_req_ack(struct qcom_glink *glink,
583 struct glink_channel *channel,
584 bool granted)
585{
586 struct glink_msg msg;
587
588 msg.cmd = cpu_to_le16(RPM_CMD_RX_INTENT_REQ_ACK);
589 msg.param1 = cpu_to_le16(channel->lcid);
590 msg.param2 = cpu_to_le32(granted);
591
592 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
593
594 return 0;
595}
596
597/**
598 * qcom_glink_advertise_intent - convert an rx intent cmd to wire format and
599 * transmit
600 * @glink: The transport to transmit on.
601 * @channel: The local channel
Srinivas Kandagatla6f0b9582018-06-18 13:33:38 +0100602 * @intent: The intent to pass on to remote.
Sricharan R933b45d2017-08-24 12:51:34 +0530603 *
604 * Return: 0 on success or standard Linux error code.
605 */
606static int qcom_glink_advertise_intent(struct qcom_glink *glink,
607 struct glink_channel *channel,
608 struct glink_core_rx_intent *intent)
609{
610 struct command {
Srinivas Kandagatla427613e2018-06-18 13:33:37 +0100611 __le16 id;
612 __le16 lcid;
613 __le32 count;
614 __le32 size;
615 __le32 liid;
Sricharan R933b45d2017-08-24 12:51:34 +0530616 } __packed;
617 struct command cmd;
618
619 cmd.id = cpu_to_le16(RPM_CMD_INTENT);
620 cmd.lcid = cpu_to_le16(channel->lcid);
621 cmd.count = cpu_to_le32(1);
622 cmd.size = cpu_to_le32(intent->size);
623 cmd.liid = cpu_to_le32(intent->id);
624
625 qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
626
627 return 0;
628}
629
630static struct glink_core_rx_intent *
631qcom_glink_alloc_intent(struct qcom_glink *glink,
632 struct glink_channel *channel,
633 size_t size,
634 bool reuseable)
635{
636 struct glink_core_rx_intent *intent;
637 int ret;
638 unsigned long flags;
639
640 intent = kzalloc(sizeof(*intent), GFP_KERNEL);
Sricharan R933b45d2017-08-24 12:51:34 +0530641 if (!intent)
642 return NULL;
643
644 intent->data = kzalloc(size, GFP_KERNEL);
645 if (!intent->data)
Dan Carpenterb775d152017-09-08 13:34:42 +0300646 goto free_intent;
Sricharan R933b45d2017-08-24 12:51:34 +0530647
648 spin_lock_irqsave(&channel->intent_lock, flags);
649 ret = idr_alloc_cyclic(&channel->liids, intent, 1, -1, GFP_ATOMIC);
650 if (ret < 0) {
651 spin_unlock_irqrestore(&channel->intent_lock, flags);
Dan Carpenterb775d152017-09-08 13:34:42 +0300652 goto free_data;
Sricharan R933b45d2017-08-24 12:51:34 +0530653 }
654 spin_unlock_irqrestore(&channel->intent_lock, flags);
655
656 intent->id = ret;
657 intent->size = size;
658 intent->reuse = reuseable;
659
660 return intent;
Dan Carpenterb775d152017-09-08 13:34:42 +0300661
662free_data:
663 kfree(intent->data);
664free_intent:
665 kfree(intent);
666 return NULL;
Sricharan R933b45d2017-08-24 12:51:34 +0530667}
668
Sricharan R88c60602017-08-24 12:51:42 +0530669static void qcom_glink_handle_rx_done(struct qcom_glink *glink,
670 u32 cid, uint32_t iid,
671 bool reuse)
672{
673 struct glink_core_rx_intent *intent;
674 struct glink_channel *channel;
675 unsigned long flags;
676
677 spin_lock_irqsave(&glink->idr_lock, flags);
678 channel = idr_find(&glink->rcids, cid);
679 spin_unlock_irqrestore(&glink->idr_lock, flags);
680 if (!channel) {
681 dev_err(glink->dev, "invalid channel id received\n");
682 return;
683 }
684
685 spin_lock_irqsave(&channel->intent_lock, flags);
686 intent = idr_find(&channel->riids, iid);
687
688 if (!intent) {
689 spin_unlock_irqrestore(&channel->intent_lock, flags);
690 dev_err(glink->dev, "invalid intent id received\n");
691 return;
692 }
693
694 intent->in_use = false;
695
696 if (!reuse) {
697 idr_remove(&channel->riids, intent->id);
698 kfree(intent);
699 }
700 spin_unlock_irqrestore(&channel->intent_lock, flags);
701}
702
Sricharan R933b45d2017-08-24 12:51:34 +0530703/**
704 * qcom_glink_handle_intent_req() - Receive a request for rx_intent
705 * from remote side
Srinivas Kandagatla6f0b9582018-06-18 13:33:38 +0100706 * @glink: Pointer to the transport interface
707 * @cid: Remote channel ID
708 * @size: size of the intent
Sricharan R933b45d2017-08-24 12:51:34 +0530709 *
710 * The function searches for the local channel to which the request for
711 * rx_intent has arrived and allocates and notifies the remote back
712 */
713static void qcom_glink_handle_intent_req(struct qcom_glink *glink,
714 u32 cid, size_t size)
715{
716 struct glink_core_rx_intent *intent;
717 struct glink_channel *channel;
718 unsigned long flags;
719
720 spin_lock_irqsave(&glink->idr_lock, flags);
721 channel = idr_find(&glink->rcids, cid);
722 spin_unlock_irqrestore(&glink->idr_lock, flags);
723
724 if (!channel) {
725 pr_err("%s channel not found for cid %d\n", __func__, cid);
726 return;
727 }
728
729 intent = qcom_glink_alloc_intent(glink, channel, size, false);
730 if (intent)
731 qcom_glink_advertise_intent(glink, channel, intent);
732
733 qcom_glink_send_intent_req_ack(glink, channel, !!intent);
734}
735
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530736static int qcom_glink_rx_defer(struct qcom_glink *glink, size_t extra)
737{
738 struct glink_defer_cmd *dcmd;
739
740 extra = ALIGN(extra, 8);
741
742 if (qcom_glink_rx_avail(glink) < sizeof(struct glink_msg) + extra) {
743 dev_dbg(glink->dev, "Insufficient data in rx fifo");
744 return -ENXIO;
745 }
746
747 dcmd = kzalloc(sizeof(*dcmd) + extra, GFP_ATOMIC);
748 if (!dcmd)
749 return -ENOMEM;
750
751 INIT_LIST_HEAD(&dcmd->node);
752
Bjorn Anderssonb88eee92017-08-24 12:51:36 +0530753 qcom_glink_rx_peak(glink, &dcmd->msg, 0, sizeof(dcmd->msg) + extra);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530754
755 spin_lock(&glink->rx_lock);
756 list_add_tail(&dcmd->node, &glink->rx_queue);
757 spin_unlock(&glink->rx_lock);
758
759 schedule_work(&glink->rx_work);
760 qcom_glink_rx_advance(glink, sizeof(dcmd->msg) + extra);
761
762 return 0;
763}
764
765static int qcom_glink_rx_data(struct qcom_glink *glink, size_t avail)
766{
Sricharan R64f95f82017-08-24 12:51:35 +0530767 struct glink_core_rx_intent *intent;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530768 struct glink_channel *channel;
769 struct {
770 struct glink_msg msg;
771 __le32 chunk_size;
772 __le32 left_size;
773 } __packed hdr;
774 unsigned int chunk_size;
775 unsigned int left_size;
776 unsigned int rcid;
Sricharan R64f95f82017-08-24 12:51:35 +0530777 unsigned int liid;
778 int ret = 0;
Sricharan R44f6df92017-08-24 12:51:33 +0530779 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530780
781 if (avail < sizeof(hdr)) {
782 dev_dbg(glink->dev, "Not enough data in fifo\n");
783 return -EAGAIN;
784 }
785
Bjorn Anderssonb88eee92017-08-24 12:51:36 +0530786 qcom_glink_rx_peak(glink, &hdr, 0, sizeof(hdr));
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530787 chunk_size = le32_to_cpu(hdr.chunk_size);
788 left_size = le32_to_cpu(hdr.left_size);
789
790 if (avail < sizeof(hdr) + chunk_size) {
791 dev_dbg(glink->dev, "Payload not yet in fifo\n");
792 return -EAGAIN;
793 }
794
795 if (WARN(chunk_size % 4, "Incoming data must be word aligned\n"))
796 return -EINVAL;
797
798 rcid = le16_to_cpu(hdr.msg.param1);
Sricharan R44f6df92017-08-24 12:51:33 +0530799 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530800 channel = idr_find(&glink->rcids, rcid);
Sricharan R44f6df92017-08-24 12:51:33 +0530801 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530802 if (!channel) {
803 dev_dbg(glink->dev, "Data on non-existing channel\n");
804
805 /* Drop the message */
Sricharan R64f95f82017-08-24 12:51:35 +0530806 goto advance_rx;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530807 }
808
Sricharan R64f95f82017-08-24 12:51:35 +0530809 if (glink->intentless) {
810 /* Might have an ongoing, fragmented, message to append */
811 if (!channel->buf) {
812 intent = kzalloc(sizeof(*intent), GFP_ATOMIC);
813 if (!intent)
814 return -ENOMEM;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530815
Sricharan R64f95f82017-08-24 12:51:35 +0530816 intent->data = kmalloc(chunk_size + left_size,
817 GFP_ATOMIC);
818 if (!intent->data) {
819 kfree(intent);
820 return -ENOMEM;
821 }
822
823 intent->id = 0xbabababa;
824 intent->size = chunk_size + left_size;
825 intent->offset = 0;
826
827 channel->buf = intent;
828 } else {
829 intent = channel->buf;
830 }
831 } else {
832 liid = le32_to_cpu(hdr.msg.param2);
833
834 spin_lock_irqsave(&channel->intent_lock, flags);
835 intent = idr_find(&channel->liids, liid);
836 spin_unlock_irqrestore(&channel->intent_lock, flags);
837
838 if (!intent) {
839 dev_err(glink->dev,
840 "no intent found for channel %s intent %d",
841 channel->name, liid);
842 goto advance_rx;
843 }
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530844 }
845
Sricharan R64f95f82017-08-24 12:51:35 +0530846 if (intent->size - intent->offset < chunk_size) {
847 dev_err(glink->dev, "Insufficient space in intent\n");
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530848
849 /* The packet header lied, drop payload */
Sricharan R64f95f82017-08-24 12:51:35 +0530850 goto advance_rx;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530851 }
852
Sricharan R64f95f82017-08-24 12:51:35 +0530853 qcom_glink_rx_peak(glink, intent->data + intent->offset,
Bjorn Anderssonb88eee92017-08-24 12:51:36 +0530854 sizeof(hdr), chunk_size);
Sricharan R64f95f82017-08-24 12:51:35 +0530855 intent->offset += chunk_size;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530856
857 /* Handle message when no fragments remain to be received */
858 if (!left_size) {
859 spin_lock(&channel->recv_lock);
860 if (channel->ept.cb) {
861 channel->ept.cb(channel->ept.rpdev,
Sricharan R64f95f82017-08-24 12:51:35 +0530862 intent->data,
863 intent->offset,
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530864 channel->ept.priv,
865 RPMSG_ADDR_ANY);
866 }
867 spin_unlock(&channel->recv_lock);
868
Sricharan R64f95f82017-08-24 12:51:35 +0530869 intent->offset = 0;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530870 channel->buf = NULL;
Sricharan R1d2ea362017-08-24 12:51:37 +0530871
872 qcom_glink_rx_done(glink, channel, intent);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530873 }
874
Sricharan R64f95f82017-08-24 12:51:35 +0530875advance_rx:
Bjorn Anderssonb88eee92017-08-24 12:51:36 +0530876 qcom_glink_rx_advance(glink, ALIGN(sizeof(hdr) + chunk_size, 8));
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530877
Sricharan R64f95f82017-08-24 12:51:35 +0530878 return ret;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530879}
880
Sricharan Rdacbb352017-08-24 12:51:39 +0530881static void qcom_glink_handle_intent(struct qcom_glink *glink,
882 unsigned int cid,
883 unsigned int count,
884 size_t avail)
885{
886 struct glink_core_rx_intent *intent;
887 struct glink_channel *channel;
888 struct intent_pair {
889 __le32 size;
890 __le32 iid;
891 };
892
893 struct {
894 struct glink_msg msg;
895 struct intent_pair intents[];
896 } __packed * msg;
897
898 const size_t msglen = sizeof(*msg) + sizeof(struct intent_pair) * count;
899 int ret;
900 int i;
901 unsigned long flags;
902
903 if (avail < msglen) {
904 dev_dbg(glink->dev, "Not enough data in fifo\n");
905 return;
906 }
907
908 spin_lock_irqsave(&glink->idr_lock, flags);
909 channel = idr_find(&glink->rcids, cid);
910 spin_unlock_irqrestore(&glink->idr_lock, flags);
911 if (!channel) {
912 dev_err(glink->dev, "intents for non-existing channel\n");
913 return;
914 }
915
916 msg = kmalloc(msglen, GFP_ATOMIC);
917 if (!msg)
918 return;
919
920 qcom_glink_rx_peak(glink, msg, 0, msglen);
921
922 for (i = 0; i < count; ++i) {
923 intent = kzalloc(sizeof(*intent), GFP_ATOMIC);
924 if (!intent)
925 break;
926
927 intent->id = le32_to_cpu(msg->intents[i].iid);
928 intent->size = le32_to_cpu(msg->intents[i].size);
929
930 spin_lock_irqsave(&channel->intent_lock, flags);
931 ret = idr_alloc(&channel->riids, intent,
932 intent->id, intent->id + 1, GFP_ATOMIC);
933 spin_unlock_irqrestore(&channel->intent_lock, flags);
934
935 if (ret < 0)
936 dev_err(glink->dev, "failed to store remote intent\n");
937 }
938
939 kfree(msg);
940 qcom_glink_rx_advance(glink, ALIGN(msglen, 8));
941}
942
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530943static int qcom_glink_rx_open_ack(struct qcom_glink *glink, unsigned int lcid)
944{
945 struct glink_channel *channel;
946
Sricharan R44f6df92017-08-24 12:51:33 +0530947 spin_lock(&glink->idr_lock);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530948 channel = idr_find(&glink->lcids, lcid);
Bjorn Andersson5f5423f2017-08-30 16:26:23 -0700949 spin_unlock(&glink->idr_lock);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530950 if (!channel) {
951 dev_err(glink->dev, "Invalid open ack packet\n");
952 return -EINVAL;
953 }
954
955 complete(&channel->open_ack);
956
957 return 0;
958}
959
960static irqreturn_t qcom_glink_native_intr(int irq, void *data)
961{
962 struct qcom_glink *glink = data;
963 struct glink_msg msg;
964 unsigned int param1;
965 unsigned int param2;
966 unsigned int avail;
967 unsigned int cmd;
Colin Ian Kinged43d092017-09-03 14:06:01 +0100968 int ret = 0;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530969
970 for (;;) {
971 avail = qcom_glink_rx_avail(glink);
972 if (avail < sizeof(msg))
973 break;
974
Bjorn Anderssonb88eee92017-08-24 12:51:36 +0530975 qcom_glink_rx_peak(glink, &msg, 0, sizeof(msg));
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530976
977 cmd = le16_to_cpu(msg.cmd);
978 param1 = le16_to_cpu(msg.param1);
979 param2 = le32_to_cpu(msg.param2);
980
981 switch (cmd) {
982 case RPM_CMD_VERSION:
983 case RPM_CMD_VERSION_ACK:
984 case RPM_CMD_CLOSE:
985 case RPM_CMD_CLOSE_ACK:
Sricharan R933b45d2017-08-24 12:51:34 +0530986 case RPM_CMD_RX_INTENT_REQ:
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530987 ret = qcom_glink_rx_defer(glink, 0);
988 break;
989 case RPM_CMD_OPEN_ACK:
990 ret = qcom_glink_rx_open_ack(glink, param1);
991 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
992 break;
993 case RPM_CMD_OPEN:
994 ret = qcom_glink_rx_defer(glink, param2);
995 break;
996 case RPM_CMD_TX_DATA:
997 case RPM_CMD_TX_DATA_CONT:
998 ret = qcom_glink_rx_data(glink, avail);
999 break;
1000 case RPM_CMD_READ_NOTIF:
1001 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1002
1003 mbox_send_message(glink->mbox_chan, NULL);
1004 mbox_client_txdone(glink->mbox_chan, 0);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301005 break;
Sricharan Rdacbb352017-08-24 12:51:39 +05301006 case RPM_CMD_INTENT:
1007 qcom_glink_handle_intent(glink, param1, param2, avail);
1008 break;
Sricharan R88c60602017-08-24 12:51:42 +05301009 case RPM_CMD_RX_DONE:
1010 qcom_glink_handle_rx_done(glink, param1, param2, false);
1011 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1012 break;
1013 case RPM_CMD_RX_DONE_W_REUSE:
1014 qcom_glink_handle_rx_done(glink, param1, param2, true);
1015 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1016 break;
Sricharan R27b9c5b2017-08-24 12:51:41 +05301017 case RPM_CMD_RX_INTENT_REQ_ACK:
1018 qcom_glink_handle_intent_req_ack(glink, param1, param2);
1019 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1020 break;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301021 default:
1022 dev_err(glink->dev, "unhandled rx cmd: %d\n", cmd);
1023 ret = -EINVAL;
1024 break;
1025 }
1026
1027 if (ret)
1028 break;
1029 }
1030
1031 return IRQ_HANDLED;
1032}
1033
1034/* Locally initiated rpmsg_create_ept */
1035static struct glink_channel *qcom_glink_create_local(struct qcom_glink *glink,
1036 const char *name)
1037{
1038 struct glink_channel *channel;
1039 int ret;
Sricharan R44f6df92017-08-24 12:51:33 +05301040 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301041
1042 channel = qcom_glink_alloc_channel(glink, name);
1043 if (IS_ERR(channel))
1044 return ERR_CAST(channel);
1045
1046 ret = qcom_glink_send_open_req(glink, channel);
1047 if (ret)
1048 goto release_channel;
1049
1050 ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
1051 if (!ret)
1052 goto err_timeout;
1053
1054 ret = wait_for_completion_timeout(&channel->open_req, 5 * HZ);
1055 if (!ret)
1056 goto err_timeout;
1057
1058 qcom_glink_send_open_ack(glink, channel);
1059
1060 return channel;
1061
1062err_timeout:
1063 /* qcom_glink_send_open_req() did register the channel in lcids*/
Sricharan R44f6df92017-08-24 12:51:33 +05301064 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301065 idr_remove(&glink->lcids, channel->lcid);
Sricharan R44f6df92017-08-24 12:51:33 +05301066 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301067
1068release_channel:
1069 /* Release qcom_glink_send_open_req() reference */
1070 kref_put(&channel->refcount, qcom_glink_channel_release);
1071 /* Release qcom_glink_alloc_channel() reference */
1072 kref_put(&channel->refcount, qcom_glink_channel_release);
1073
1074 return ERR_PTR(-ETIMEDOUT);
1075}
1076
1077/* Remote initiated rpmsg_create_ept */
1078static int qcom_glink_create_remote(struct qcom_glink *glink,
1079 struct glink_channel *channel)
1080{
1081 int ret;
1082
1083 qcom_glink_send_open_ack(glink, channel);
1084
1085 ret = qcom_glink_send_open_req(glink, channel);
1086 if (ret)
1087 goto close_link;
1088
1089 ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
1090 if (!ret) {
1091 ret = -ETIMEDOUT;
1092 goto close_link;
1093 }
1094
1095 return 0;
1096
1097close_link:
1098 /*
1099 * Send a close request to "undo" our open-ack. The close-ack will
1100 * release the last reference.
1101 */
1102 qcom_glink_send_close_req(glink, channel);
1103
1104 /* Release qcom_glink_send_open_req() reference */
1105 kref_put(&channel->refcount, qcom_glink_channel_release);
1106
1107 return ret;
1108}
1109
1110static struct rpmsg_endpoint *qcom_glink_create_ept(struct rpmsg_device *rpdev,
1111 rpmsg_rx_cb_t cb,
1112 void *priv,
1113 struct rpmsg_channel_info
1114 chinfo)
1115{
1116 struct glink_channel *parent = to_glink_channel(rpdev->ept);
1117 struct glink_channel *channel;
1118 struct qcom_glink *glink = parent->glink;
1119 struct rpmsg_endpoint *ept;
1120 const char *name = chinfo.name;
1121 int cid;
1122 int ret;
Sricharan R44f6df92017-08-24 12:51:33 +05301123 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301124
Sricharan R44f6df92017-08-24 12:51:33 +05301125 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301126 idr_for_each_entry(&glink->rcids, channel, cid) {
1127 if (!strcmp(channel->name, name))
1128 break;
1129 }
Sricharan R44f6df92017-08-24 12:51:33 +05301130 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301131
1132 if (!channel) {
1133 channel = qcom_glink_create_local(glink, name);
1134 if (IS_ERR(channel))
1135 return NULL;
1136 } else {
1137 ret = qcom_glink_create_remote(glink, channel);
1138 if (ret)
1139 return NULL;
1140 }
1141
1142 ept = &channel->ept;
1143 ept->rpdev = rpdev;
1144 ept->cb = cb;
1145 ept->priv = priv;
1146 ept->ops = &glink_endpoint_ops;
1147
1148 return ept;
1149}
1150
Sricharan R6bf68cb2017-08-24 12:51:38 +05301151static int qcom_glink_announce_create(struct rpmsg_device *rpdev)
1152{
1153 struct glink_channel *channel = to_glink_channel(rpdev->ept);
Chris Lew07389372017-10-26 15:28:55 -07001154 struct device_node *np = rpdev->dev.of_node;
Sricharan R6bf68cb2017-08-24 12:51:38 +05301155 struct qcom_glink *glink = channel->glink;
Chris Lew07389372017-10-26 15:28:55 -07001156 struct glink_core_rx_intent *intent;
1157 const struct property *prop = NULL;
1158 __be32 defaults[] = { cpu_to_be32(SZ_1K), cpu_to_be32(5) };
1159 int num_intents;
1160 int num_groups = 1;
1161 __be32 *val = defaults;
1162 int size;
Sricharan R6bf68cb2017-08-24 12:51:38 +05301163
Chris Lew07389372017-10-26 15:28:55 -07001164 if (glink->intentless)
1165 return 0;
Sricharan R6bf68cb2017-08-24 12:51:38 +05301166
Chris Lew07389372017-10-26 15:28:55 -07001167 prop = of_find_property(np, "qcom,intents", NULL);
1168 if (prop) {
1169 val = prop->value;
1170 num_groups = prop->length / sizeof(u32) / 2;
Sricharan R6bf68cb2017-08-24 12:51:38 +05301171 }
1172
Chris Lew07389372017-10-26 15:28:55 -07001173 /* Channel is now open, advertise base set of intents */
1174 while (num_groups--) {
1175 size = be32_to_cpup(val++);
1176 num_intents = be32_to_cpup(val++);
1177 while (num_intents--) {
1178 intent = qcom_glink_alloc_intent(glink, channel, size,
1179 true);
1180 if (!intent)
1181 break;
1182
1183 qcom_glink_advertise_intent(glink, channel, intent);
1184 }
1185 }
Sricharan R6bf68cb2017-08-24 12:51:38 +05301186 return 0;
1187}
1188
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301189static void qcom_glink_destroy_ept(struct rpmsg_endpoint *ept)
1190{
1191 struct glink_channel *channel = to_glink_channel(ept);
1192 struct qcom_glink *glink = channel->glink;
1193 unsigned long flags;
1194
1195 spin_lock_irqsave(&channel->recv_lock, flags);
1196 channel->ept.cb = NULL;
1197 spin_unlock_irqrestore(&channel->recv_lock, flags);
1198
1199 /* Decouple the potential rpdev from the channel */
1200 channel->rpdev = NULL;
1201
1202 qcom_glink_send_close_req(glink, channel);
1203}
1204
Sricharan R27b9c5b2017-08-24 12:51:41 +05301205static int qcom_glink_request_intent(struct qcom_glink *glink,
1206 struct glink_channel *channel,
1207 size_t size)
1208{
1209 struct {
1210 u16 id;
1211 u16 cid;
1212 u32 size;
1213 } __packed cmd;
1214
1215 int ret;
1216
1217 mutex_lock(&channel->intent_req_lock);
1218
1219 reinit_completion(&channel->intent_req_comp);
1220
1221 cmd.id = RPM_CMD_RX_INTENT_REQ;
1222 cmd.cid = channel->lcid;
1223 cmd.size = size;
1224
1225 ret = qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
1226 if (ret)
Dan Carpenter0a7480b2017-09-08 13:33:45 +03001227 goto unlock;
Sricharan R27b9c5b2017-08-24 12:51:41 +05301228
1229 ret = wait_for_completion_timeout(&channel->intent_req_comp, 10 * HZ);
1230 if (!ret) {
1231 dev_err(glink->dev, "intent request timed out\n");
1232 ret = -ETIMEDOUT;
1233 } else {
1234 ret = channel->intent_req_result ? 0 : -ECANCELED;
1235 }
1236
Dan Carpenter0a7480b2017-09-08 13:33:45 +03001237unlock:
Sricharan R27b9c5b2017-08-24 12:51:41 +05301238 mutex_unlock(&channel->intent_req_lock);
1239 return ret;
1240}
1241
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301242static int __qcom_glink_send(struct glink_channel *channel,
1243 void *data, int len, bool wait)
1244{
1245 struct qcom_glink *glink = channel->glink;
Sricharan R11cb45a2017-08-24 12:51:40 +05301246 struct glink_core_rx_intent *intent = NULL;
1247 struct glink_core_rx_intent *tmp;
1248 int iid = 0;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301249 struct {
1250 struct glink_msg msg;
1251 __le32 chunk_size;
1252 __le32 left_size;
1253 } __packed req;
Sricharan R11cb45a2017-08-24 12:51:40 +05301254 int ret;
1255 unsigned long flags;
1256
1257 if (!glink->intentless) {
Sricharan R27b9c5b2017-08-24 12:51:41 +05301258 while (!intent) {
Sricharan R11cb45a2017-08-24 12:51:40 +05301259 spin_lock_irqsave(&channel->intent_lock, flags);
1260 idr_for_each_entry(&channel->riids, tmp, iid) {
1261 if (tmp->size >= len && !tmp->in_use) {
Chris Lew29031872017-10-26 15:28:56 -07001262 if (!intent)
1263 intent = tmp;
1264 else if (intent->size > tmp->size)
1265 intent = tmp;
1266 if (intent->size == len)
1267 break;
Sricharan R11cb45a2017-08-24 12:51:40 +05301268 }
1269 }
Chris Lew29031872017-10-26 15:28:56 -07001270 if (intent)
1271 intent->in_use = true;
Sricharan R11cb45a2017-08-24 12:51:40 +05301272 spin_unlock_irqrestore(&channel->intent_lock, flags);
1273
1274 /* We found an available intent */
Sricharan R27b9c5b2017-08-24 12:51:41 +05301275 if (intent)
1276 break;
1277
1278 if (!wait)
Sricharan R11cb45a2017-08-24 12:51:40 +05301279 return -EBUSY;
Sricharan R27b9c5b2017-08-24 12:51:41 +05301280
1281 ret = qcom_glink_request_intent(glink, channel, len);
1282 if (ret < 0)
1283 return ret;
Sricharan R11cb45a2017-08-24 12:51:40 +05301284 }
1285
1286 iid = intent->id;
1287 }
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301288
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301289 req.msg.cmd = cpu_to_le16(RPM_CMD_TX_DATA);
1290 req.msg.param1 = cpu_to_le16(channel->lcid);
Sricharan R11cb45a2017-08-24 12:51:40 +05301291 req.msg.param2 = cpu_to_le32(iid);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301292 req.chunk_size = cpu_to_le32(len);
1293 req.left_size = cpu_to_le32(0);
1294
Sricharan R11cb45a2017-08-24 12:51:40 +05301295 ret = qcom_glink_tx(glink, &req, sizeof(req), data, len, wait);
1296
1297 /* Mark intent available if we failed */
Colin Ian King906cf292017-09-03 13:46:14 +01001298 if (ret && intent)
Sricharan R11cb45a2017-08-24 12:51:40 +05301299 intent->in_use = false;
1300
1301 return ret;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301302}
1303
1304static int qcom_glink_send(struct rpmsg_endpoint *ept, void *data, int len)
1305{
1306 struct glink_channel *channel = to_glink_channel(ept);
1307
1308 return __qcom_glink_send(channel, data, len, true);
1309}
1310
1311static int qcom_glink_trysend(struct rpmsg_endpoint *ept, void *data, int len)
1312{
1313 struct glink_channel *channel = to_glink_channel(ept);
1314
1315 return __qcom_glink_send(channel, data, len, false);
1316}
1317
1318/*
1319 * Finds the device_node for the glink child interested in this channel.
1320 */
1321static struct device_node *qcom_glink_match_channel(struct device_node *node,
1322 const char *channel)
1323{
1324 struct device_node *child;
1325 const char *name;
1326 const char *key;
1327 int ret;
1328
1329 for_each_available_child_of_node(node, child) {
1330 key = "qcom,glink-channels";
1331 ret = of_property_read_string(child, key, &name);
1332 if (ret)
1333 continue;
1334
1335 if (strcmp(name, channel) == 0)
1336 return child;
1337 }
1338
1339 return NULL;
1340}
1341
1342static const struct rpmsg_device_ops glink_device_ops = {
1343 .create_ept = qcom_glink_create_ept,
Sricharan R6bf68cb2017-08-24 12:51:38 +05301344 .announce_create = qcom_glink_announce_create,
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301345};
1346
1347static const struct rpmsg_endpoint_ops glink_endpoint_ops = {
1348 .destroy_ept = qcom_glink_destroy_ept,
1349 .send = qcom_glink_send,
1350 .trysend = qcom_glink_trysend,
1351};
1352
1353static void qcom_glink_rpdev_release(struct device *dev)
1354{
1355 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
1356 struct glink_channel *channel = to_glink_channel(rpdev->ept);
1357
1358 channel->rpdev = NULL;
1359 kfree(rpdev);
1360}
1361
1362static int qcom_glink_rx_open(struct qcom_glink *glink, unsigned int rcid,
1363 char *name)
1364{
1365 struct glink_channel *channel;
1366 struct rpmsg_device *rpdev;
1367 bool create_device = false;
1368 struct device_node *node;
1369 int lcid;
1370 int ret;
Sricharan R44f6df92017-08-24 12:51:33 +05301371 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301372
Sricharan R44f6df92017-08-24 12:51:33 +05301373 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301374 idr_for_each_entry(&glink->lcids, channel, lcid) {
1375 if (!strcmp(channel->name, name))
1376 break;
1377 }
Sricharan R44f6df92017-08-24 12:51:33 +05301378 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301379
1380 if (!channel) {
1381 channel = qcom_glink_alloc_channel(glink, name);
1382 if (IS_ERR(channel))
1383 return PTR_ERR(channel);
1384
1385 /* The opening dance was initiated by the remote */
1386 create_device = true;
1387 }
1388
Sricharan R44f6df92017-08-24 12:51:33 +05301389 spin_lock_irqsave(&glink->idr_lock, flags);
1390 ret = idr_alloc(&glink->rcids, channel, rcid, rcid + 1, GFP_ATOMIC);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301391 if (ret < 0) {
1392 dev_err(glink->dev, "Unable to insert channel into rcid list\n");
Sricharan R44f6df92017-08-24 12:51:33 +05301393 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301394 goto free_channel;
1395 }
1396 channel->rcid = ret;
Sricharan R44f6df92017-08-24 12:51:33 +05301397 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301398
1399 complete(&channel->open_req);
1400
1401 if (create_device) {
1402 rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL);
1403 if (!rpdev) {
1404 ret = -ENOMEM;
1405 goto rcid_remove;
1406 }
1407
1408 rpdev->ept = &channel->ept;
1409 strncpy(rpdev->id.name, name, RPMSG_NAME_SIZE);
1410 rpdev->src = RPMSG_ADDR_ANY;
1411 rpdev->dst = RPMSG_ADDR_ANY;
1412 rpdev->ops = &glink_device_ops;
1413
1414 node = qcom_glink_match_channel(glink->dev->of_node, name);
1415 rpdev->dev.of_node = node;
1416 rpdev->dev.parent = glink->dev;
1417 rpdev->dev.release = qcom_glink_rpdev_release;
1418
1419 ret = rpmsg_register_device(rpdev);
1420 if (ret)
1421 goto free_rpdev;
1422
1423 channel->rpdev = rpdev;
1424 }
1425
1426 return 0;
1427
1428free_rpdev:
1429 kfree(rpdev);
1430rcid_remove:
Sricharan R44f6df92017-08-24 12:51:33 +05301431 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301432 idr_remove(&glink->rcids, channel->rcid);
1433 channel->rcid = 0;
Sricharan R44f6df92017-08-24 12:51:33 +05301434 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301435free_channel:
1436 /* Release the reference, iff we took it */
1437 if (create_device)
1438 kref_put(&channel->refcount, qcom_glink_channel_release);
1439
1440 return ret;
1441}
1442
1443static void qcom_glink_rx_close(struct qcom_glink *glink, unsigned int rcid)
1444{
1445 struct rpmsg_channel_info chinfo;
1446 struct glink_channel *channel;
Sricharan R44f6df92017-08-24 12:51:33 +05301447 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301448
Sricharan R44f6df92017-08-24 12:51:33 +05301449 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301450 channel = idr_find(&glink->rcids, rcid);
Sricharan R44f6df92017-08-24 12:51:33 +05301451 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301452 if (WARN(!channel, "close request on unknown channel\n"))
1453 return;
1454
Sricharan R1d2ea362017-08-24 12:51:37 +05301455 /* cancel pending rx_done work */
1456 cancel_work_sync(&channel->intent_work);
1457
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301458 if (channel->rpdev) {
1459 strncpy(chinfo.name, channel->name, sizeof(chinfo.name));
1460 chinfo.src = RPMSG_ADDR_ANY;
1461 chinfo.dst = RPMSG_ADDR_ANY;
1462
1463 rpmsg_unregister_device(glink->dev, &chinfo);
1464 }
1465
1466 qcom_glink_send_close_ack(glink, channel->rcid);
1467
Sricharan R44f6df92017-08-24 12:51:33 +05301468 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301469 idr_remove(&glink->rcids, channel->rcid);
1470 channel->rcid = 0;
Sricharan R44f6df92017-08-24 12:51:33 +05301471 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301472
1473 kref_put(&channel->refcount, qcom_glink_channel_release);
1474}
1475
1476static void qcom_glink_rx_close_ack(struct qcom_glink *glink, unsigned int lcid)
1477{
1478 struct glink_channel *channel;
Sricharan R44f6df92017-08-24 12:51:33 +05301479 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301480
Sricharan R44f6df92017-08-24 12:51:33 +05301481 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301482 channel = idr_find(&glink->lcids, lcid);
Sricharan R44f6df92017-08-24 12:51:33 +05301483 if (WARN(!channel, "close ack on unknown channel\n")) {
1484 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301485 return;
Sricharan R44f6df92017-08-24 12:51:33 +05301486 }
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301487
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301488 idr_remove(&glink->lcids, channel->lcid);
1489 channel->lcid = 0;
Sricharan R44f6df92017-08-24 12:51:33 +05301490 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301491
1492 kref_put(&channel->refcount, qcom_glink_channel_release);
1493}
1494
1495static void qcom_glink_work(struct work_struct *work)
1496{
1497 struct qcom_glink *glink = container_of(work, struct qcom_glink,
1498 rx_work);
1499 struct glink_defer_cmd *dcmd;
1500 struct glink_msg *msg;
1501 unsigned long flags;
1502 unsigned int param1;
1503 unsigned int param2;
1504 unsigned int cmd;
1505
1506 for (;;) {
1507 spin_lock_irqsave(&glink->rx_lock, flags);
1508 if (list_empty(&glink->rx_queue)) {
1509 spin_unlock_irqrestore(&glink->rx_lock, flags);
1510 break;
1511 }
1512 dcmd = list_first_entry(&glink->rx_queue,
1513 struct glink_defer_cmd, node);
1514 list_del(&dcmd->node);
1515 spin_unlock_irqrestore(&glink->rx_lock, flags);
1516
1517 msg = &dcmd->msg;
1518 cmd = le16_to_cpu(msg->cmd);
1519 param1 = le16_to_cpu(msg->param1);
1520 param2 = le32_to_cpu(msg->param2);
1521
1522 switch (cmd) {
1523 case RPM_CMD_VERSION:
Sricharan Rd31ad612017-08-24 12:51:32 +05301524 qcom_glink_receive_version(glink, param1, param2);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301525 break;
1526 case RPM_CMD_VERSION_ACK:
Sricharan Rd31ad612017-08-24 12:51:32 +05301527 qcom_glink_receive_version_ack(glink, param1, param2);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301528 break;
1529 case RPM_CMD_OPEN:
1530 qcom_glink_rx_open(glink, param1, msg->data);
1531 break;
1532 case RPM_CMD_CLOSE:
1533 qcom_glink_rx_close(glink, param1);
1534 break;
1535 case RPM_CMD_CLOSE_ACK:
1536 qcom_glink_rx_close_ack(glink, param1);
1537 break;
Sricharan R933b45d2017-08-24 12:51:34 +05301538 case RPM_CMD_RX_INTENT_REQ:
1539 qcom_glink_handle_intent_req(glink, param1, param2);
1540 break;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301541 default:
1542 WARN(1, "Unknown defer object %d\n", cmd);
1543 break;
1544 }
1545
1546 kfree(dcmd);
1547 }
1548}
1549
1550struct qcom_glink *qcom_glink_native_probe(struct device *dev,
Sricharan Rd31ad612017-08-24 12:51:32 +05301551 unsigned long features,
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301552 struct qcom_glink_pipe *rx,
Sricharan R933b45d2017-08-24 12:51:34 +05301553 struct qcom_glink_pipe *tx,
1554 bool intentless)
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301555{
1556 int irq;
1557 int ret;
1558 struct qcom_glink *glink;
1559
1560 glink = devm_kzalloc(dev, sizeof(*glink), GFP_KERNEL);
1561 if (!glink)
1562 return ERR_PTR(-ENOMEM);
1563
1564 glink->dev = dev;
1565 glink->tx_pipe = tx;
1566 glink->rx_pipe = rx;
1567
Sricharan Rd31ad612017-08-24 12:51:32 +05301568 glink->features = features;
Sricharan R933b45d2017-08-24 12:51:34 +05301569 glink->intentless = intentless;
Sricharan Rd31ad612017-08-24 12:51:32 +05301570
Bjorn Andersson29fc9b32018-02-13 11:04:04 -08001571 spin_lock_init(&glink->tx_lock);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301572 spin_lock_init(&glink->rx_lock);
1573 INIT_LIST_HEAD(&glink->rx_queue);
1574 INIT_WORK(&glink->rx_work, qcom_glink_work);
1575
Sricharan R44f6df92017-08-24 12:51:33 +05301576 spin_lock_init(&glink->idr_lock);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301577 idr_init(&glink->lcids);
1578 idr_init(&glink->rcids);
1579
Chris Lew475452f2018-07-27 17:47:22 +05301580 ret = of_property_read_string(dev->of_node, "label", &glink->name);
1581 if (ret < 0)
1582 glink->name = dev->of_node->name;
1583
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301584 glink->mbox_client.dev = dev;
Bjorn Andersson38a9acb2017-11-15 22:08:33 -08001585 glink->mbox_client.knows_txdone = true;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301586 glink->mbox_chan = mbox_request_channel(&glink->mbox_client, 0);
1587 if (IS_ERR(glink->mbox_chan)) {
1588 if (PTR_ERR(glink->mbox_chan) != -EPROBE_DEFER)
1589 dev_err(dev, "failed to acquire IPC channel\n");
1590 return ERR_CAST(glink->mbox_chan);
1591 }
1592
1593 irq = of_irq_get(dev->of_node, 0);
1594 ret = devm_request_irq(dev, irq,
1595 qcom_glink_native_intr,
1596 IRQF_NO_SUSPEND | IRQF_SHARED,
1597 "glink-native", glink);
1598 if (ret) {
1599 dev_err(dev, "failed to request IRQ\n");
1600 return ERR_PTR(ret);
1601 }
1602
1603 glink->irq = irq;
1604
1605 ret = qcom_glink_send_version(glink);
1606 if (ret)
1607 return ERR_PTR(ret);
1608
1609 return glink;
1610}
Bjorn Anderssonf8ff9772017-08-31 09:27:35 -07001611EXPORT_SYMBOL_GPL(qcom_glink_native_probe);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301612
1613static int qcom_glink_remove_device(struct device *dev, void *data)
1614{
1615 device_unregister(dev);
1616
1617 return 0;
1618}
1619
1620void qcom_glink_native_remove(struct qcom_glink *glink)
1621{
1622 struct glink_channel *channel;
1623 int cid;
1624 int ret;
Sricharan R44f6df92017-08-24 12:51:33 +05301625 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301626
1627 disable_irq(glink->irq);
1628 cancel_work_sync(&glink->rx_work);
1629
1630 ret = device_for_each_child(glink->dev, NULL, qcom_glink_remove_device);
1631 if (ret)
1632 dev_warn(glink->dev, "Can't remove GLINK devices: %d\n", ret);
1633
Sricharan R44f6df92017-08-24 12:51:33 +05301634 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301635 /* Release any defunct local channels, waiting for close-ack */
1636 idr_for_each_entry(&glink->lcids, channel, cid)
1637 kref_put(&channel->refcount, qcom_glink_channel_release);
1638
1639 idr_destroy(&glink->lcids);
1640 idr_destroy(&glink->rcids);
Sricharan R44f6df92017-08-24 12:51:33 +05301641 spin_unlock_irqrestore(&glink->idr_lock, flags);
Sricharan R76cf1102017-08-24 12:51:29 +05301642 mbox_free_channel(glink->mbox_chan);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301643}
Bjorn Anderssonf8ff9772017-08-31 09:27:35 -07001644EXPORT_SYMBOL_GPL(qcom_glink_native_remove);
Bjorn Anderssoncaf989c2017-08-24 12:51:30 +05301645
1646void qcom_glink_native_unregister(struct qcom_glink *glink)
1647{
1648 device_unregister(glink->dev);
1649}
Bjorn Anderssonf8ff9772017-08-31 09:27:35 -07001650EXPORT_SYMBOL_GPL(qcom_glink_native_unregister);
Bjorn Andersson1e0d5612017-11-14 21:45:34 -08001651
1652MODULE_DESCRIPTION("Qualcomm GLINK driver");
1653MODULE_LICENSE("GPL v2");