blob: 1995f5b3ea6775e22f85dc97461a107fc5b4c561 [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);
Bjorn Anderssonc3dadc12019-10-04 15:27:01 -0700244 struct glink_core_rx_intent *intent;
Arun Kumar Neelakantamb85f6b62019-10-04 15:26:57 -0700245 struct glink_core_rx_intent *tmp;
Sricharan R933b45d2017-08-24 12:51:34 +0530246 unsigned long flags;
Arun Kumar Neelakantamb85f6b62019-10-04 15:26:57 -0700247 int iid;
Sricharan R933b45d2017-08-24 12:51:34 +0530248
Bjorn Anderssonc3dadc12019-10-04 15:27:01 -0700249 /* cancel pending rx_done work */
250 cancel_work_sync(&channel->intent_work);
251
Sricharan R933b45d2017-08-24 12:51:34 +0530252 spin_lock_irqsave(&channel->intent_lock, flags);
Bjorn Anderssonc3dadc12019-10-04 15:27:01 -0700253 /* Free all non-reuse intents pending rx_done work */
254 list_for_each_entry_safe(intent, tmp, &channel->done_intents, node) {
255 if (!intent->reuse) {
256 kfree(intent->data);
257 kfree(intent);
258 }
259 }
260
Arun Kumar Neelakantamb85f6b62019-10-04 15:26:57 -0700261 idr_for_each_entry(&channel->liids, tmp, iid) {
262 kfree(tmp->data);
263 kfree(tmp);
264 }
Sricharan R933b45d2017-08-24 12:51:34 +0530265 idr_destroy(&channel->liids);
Arun Kumar Neelakantamb85f6b62019-10-04 15:26:57 -0700266
267 idr_for_each_entry(&channel->riids, tmp, iid)
268 kfree(tmp);
Sricharan Rdacbb352017-08-24 12:51:39 +0530269 idr_destroy(&channel->riids);
Sricharan R933b45d2017-08-24 12:51:34 +0530270 spin_unlock_irqrestore(&channel->intent_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530271
272 kfree(channel->name);
273 kfree(channel);
274}
275
276static size_t qcom_glink_rx_avail(struct qcom_glink *glink)
277{
278 return glink->rx_pipe->avail(glink->rx_pipe);
279}
280
281static void qcom_glink_rx_peak(struct qcom_glink *glink,
Bjorn Anderssonb88eee92017-08-24 12:51:36 +0530282 void *data, unsigned int offset, size_t count)
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530283{
Bjorn Anderssonb88eee92017-08-24 12:51:36 +0530284 glink->rx_pipe->peak(glink->rx_pipe, data, offset, count);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530285}
286
287static void qcom_glink_rx_advance(struct qcom_glink *glink, size_t count)
288{
289 glink->rx_pipe->advance(glink->rx_pipe, count);
290}
291
292static size_t qcom_glink_tx_avail(struct qcom_glink *glink)
293{
294 return glink->tx_pipe->avail(glink->tx_pipe);
295}
296
297static void qcom_glink_tx_write(struct qcom_glink *glink,
298 const void *hdr, size_t hlen,
299 const void *data, size_t dlen)
300{
301 glink->tx_pipe->write(glink->tx_pipe, hdr, hlen, data, dlen);
302}
303
304static int qcom_glink_tx(struct qcom_glink *glink,
305 const void *hdr, size_t hlen,
306 const void *data, size_t dlen, bool wait)
307{
308 unsigned int tlen = hlen + dlen;
Bjorn Andersson29fc9b32018-02-13 11:04:04 -0800309 unsigned long flags;
310 int ret = 0;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530311
312 /* Reject packets that are too big */
313 if (tlen >= glink->tx_pipe->length)
314 return -EINVAL;
315
Bjorn Andersson29fc9b32018-02-13 11:04:04 -0800316 spin_lock_irqsave(&glink->tx_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530317
318 while (qcom_glink_tx_avail(glink) < tlen) {
319 if (!wait) {
Sricharan Ra7df9df2017-08-24 12:51:28 +0530320 ret = -EAGAIN;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530321 goto out;
322 }
323
Bjorn Andersson29fc9b32018-02-13 11:04:04 -0800324 /* Wait without holding the tx_lock */
325 spin_unlock_irqrestore(&glink->tx_lock, flags);
326
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530327 usleep_range(10000, 15000);
Bjorn Andersson29fc9b32018-02-13 11:04:04 -0800328
329 spin_lock_irqsave(&glink->tx_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530330 }
331
332 qcom_glink_tx_write(glink, hdr, hlen, data, dlen);
333
334 mbox_send_message(glink->mbox_chan, NULL);
335 mbox_client_txdone(glink->mbox_chan, 0);
336
337out:
Bjorn Andersson29fc9b32018-02-13 11:04:04 -0800338 spin_unlock_irqrestore(&glink->tx_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530339
340 return ret;
341}
342
343static int qcom_glink_send_version(struct qcom_glink *glink)
344{
345 struct glink_msg msg;
346
347 msg.cmd = cpu_to_le16(RPM_CMD_VERSION);
Sricharan Rd31ad612017-08-24 12:51:32 +0530348 msg.param1 = cpu_to_le16(GLINK_VERSION_1);
349 msg.param2 = cpu_to_le32(glink->features);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530350
351 return qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
352}
353
354static void qcom_glink_send_version_ack(struct qcom_glink *glink)
355{
356 struct glink_msg msg;
357
358 msg.cmd = cpu_to_le16(RPM_CMD_VERSION_ACK);
Sricharan Rd31ad612017-08-24 12:51:32 +0530359 msg.param1 = cpu_to_le16(GLINK_VERSION_1);
360 msg.param2 = cpu_to_le32(glink->features);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530361
362 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
363}
364
365static void qcom_glink_send_open_ack(struct qcom_glink *glink,
366 struct glink_channel *channel)
367{
368 struct glink_msg msg;
369
370 msg.cmd = cpu_to_le16(RPM_CMD_OPEN_ACK);
371 msg.param1 = cpu_to_le16(channel->rcid);
372 msg.param2 = cpu_to_le32(0);
373
374 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
375}
376
Sricharan R27b9c5b2017-08-24 12:51:41 +0530377static void qcom_glink_handle_intent_req_ack(struct qcom_glink *glink,
378 unsigned int cid, bool granted)
379{
380 struct glink_channel *channel;
381 unsigned long flags;
382
383 spin_lock_irqsave(&glink->idr_lock, flags);
384 channel = idr_find(&glink->rcids, cid);
385 spin_unlock_irqrestore(&glink->idr_lock, flags);
386 if (!channel) {
387 dev_err(glink->dev, "unable to find channel\n");
388 return;
389 }
390
391 channel->intent_req_result = granted;
392 complete(&channel->intent_req_comp);
393}
394
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530395/**
396 * qcom_glink_send_open_req() - send a RPM_CMD_OPEN request to the remote
397 * @glink: Ptr to the glink edge
398 * @channel: Ptr to the channel that the open req is sent
399 *
400 * Allocates a local channel id and sends a RPM_CMD_OPEN message to the remote.
401 * Will return with refcount held, regardless of outcome.
402 *
403 * Returns 0 on success, negative errno otherwise.
404 */
405static int qcom_glink_send_open_req(struct qcom_glink *glink,
406 struct glink_channel *channel)
407{
408 struct {
409 struct glink_msg msg;
410 u8 name[GLINK_NAME_SIZE];
411 } __packed req;
412 int name_len = strlen(channel->name) + 1;
413 int req_len = ALIGN(sizeof(req.msg) + name_len, 8);
414 int ret;
Sricharan R44f6df92017-08-24 12:51:33 +0530415 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530416
417 kref_get(&channel->refcount);
418
Sricharan R44f6df92017-08-24 12:51:33 +0530419 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530420 ret = idr_alloc_cyclic(&glink->lcids, channel,
421 RPM_GLINK_CID_MIN, RPM_GLINK_CID_MAX,
Sricharan R44f6df92017-08-24 12:51:33 +0530422 GFP_ATOMIC);
423 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530424 if (ret < 0)
425 return ret;
426
427 channel->lcid = ret;
428
429 req.msg.cmd = cpu_to_le16(RPM_CMD_OPEN);
430 req.msg.param1 = cpu_to_le16(channel->lcid);
431 req.msg.param2 = cpu_to_le32(name_len);
432 strcpy(req.name, channel->name);
433
434 ret = qcom_glink_tx(glink, &req, req_len, NULL, 0, true);
435 if (ret)
436 goto remove_idr;
437
438 return 0;
439
440remove_idr:
Sricharan R44f6df92017-08-24 12:51:33 +0530441 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530442 idr_remove(&glink->lcids, channel->lcid);
443 channel->lcid = 0;
Sricharan R44f6df92017-08-24 12:51:33 +0530444 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530445
446 return ret;
447}
448
449static void qcom_glink_send_close_req(struct qcom_glink *glink,
450 struct glink_channel *channel)
451{
452 struct glink_msg req;
453
454 req.cmd = cpu_to_le16(RPM_CMD_CLOSE);
455 req.param1 = cpu_to_le16(channel->lcid);
456 req.param2 = 0;
457
458 qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
459}
460
461static void qcom_glink_send_close_ack(struct qcom_glink *glink,
462 unsigned int rcid)
463{
464 struct glink_msg req;
465
466 req.cmd = cpu_to_le16(RPM_CMD_CLOSE_ACK);
467 req.param1 = cpu_to_le16(rcid);
468 req.param2 = 0;
469
470 qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
471}
472
Sricharan R1d2ea362017-08-24 12:51:37 +0530473static void qcom_glink_rx_done_work(struct work_struct *work)
474{
475 struct glink_channel *channel = container_of(work, struct glink_channel,
476 intent_work);
477 struct qcom_glink *glink = channel->glink;
478 struct glink_core_rx_intent *intent, *tmp;
479 struct {
480 u16 id;
481 u16 lcid;
482 u32 liid;
483 } __packed cmd;
484
485 unsigned int cid = channel->lcid;
486 unsigned int iid;
487 bool reuse;
488 unsigned long flags;
489
490 spin_lock_irqsave(&channel->intent_lock, flags);
491 list_for_each_entry_safe(intent, tmp, &channel->done_intents, node) {
492 list_del(&intent->node);
493 spin_unlock_irqrestore(&channel->intent_lock, flags);
494 iid = intent->id;
495 reuse = intent->reuse;
496
497 cmd.id = reuse ? RPM_CMD_RX_DONE_W_REUSE : RPM_CMD_RX_DONE;
498 cmd.lcid = cid;
499 cmd.liid = iid;
500
501 qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
502 if (!reuse) {
503 kfree(intent->data);
504 kfree(intent);
505 }
506 spin_lock_irqsave(&channel->intent_lock, flags);
507 }
508 spin_unlock_irqrestore(&channel->intent_lock, flags);
509}
510
511static void qcom_glink_rx_done(struct qcom_glink *glink,
512 struct glink_channel *channel,
513 struct glink_core_rx_intent *intent)
514{
515 /* We don't send RX_DONE to intentless systems */
516 if (glink->intentless) {
517 kfree(intent->data);
518 kfree(intent);
519 return;
520 }
521
522 /* Take it off the tree of receive intents */
523 if (!intent->reuse) {
524 spin_lock(&channel->intent_lock);
525 idr_remove(&channel->liids, intent->id);
526 spin_unlock(&channel->intent_lock);
527 }
528
529 /* Schedule the sending of a rx_done indication */
530 spin_lock(&channel->intent_lock);
531 list_add_tail(&intent->node, &channel->done_intents);
532 spin_unlock(&channel->intent_lock);
533
534 schedule_work(&channel->intent_work);
535}
536
Sricharan Rd31ad612017-08-24 12:51:32 +0530537/**
538 * qcom_glink_receive_version() - receive version/features from remote system
539 *
540 * @glink: pointer to transport interface
Srinivas Kandagatla6f0b9582018-06-18 13:33:38 +0100541 * @version: remote version
542 * @features: remote features
Sricharan Rd31ad612017-08-24 12:51:32 +0530543 *
544 * This function is called in response to a remote-initiated version/feature
545 * negotiation sequence.
546 */
547static void qcom_glink_receive_version(struct qcom_glink *glink,
548 u32 version,
549 u32 features)
550{
551 switch (version) {
552 case 0:
553 break;
554 case GLINK_VERSION_1:
555 glink->features &= features;
556 /* FALLTHROUGH */
557 default:
558 qcom_glink_send_version_ack(glink);
559 break;
560 }
561}
562
563/**
564 * qcom_glink_receive_version_ack() - receive negotiation ack from remote system
565 *
566 * @glink: pointer to transport interface
Srinivas Kandagatla6f0b9582018-06-18 13:33:38 +0100567 * @version: remote version response
568 * @features: remote features response
Sricharan Rd31ad612017-08-24 12:51:32 +0530569 *
570 * This function is called in response to a local-initiated version/feature
571 * negotiation sequence and is the counter-offer from the remote side based
572 * upon the initial version and feature set requested.
573 */
574static void qcom_glink_receive_version_ack(struct qcom_glink *glink,
575 u32 version,
576 u32 features)
577{
578 switch (version) {
579 case 0:
580 /* Version negotiation failed */
581 break;
582 case GLINK_VERSION_1:
583 if (features == glink->features)
584 break;
585
586 glink->features &= features;
587 /* FALLTHROUGH */
588 default:
589 qcom_glink_send_version(glink);
590 break;
591 }
592}
593
Sricharan R933b45d2017-08-24 12:51:34 +0530594/**
595 * qcom_glink_send_intent_req_ack() - convert an rx intent request ack cmd to
Srinivas Kandagatla6f0b9582018-06-18 13:33:38 +0100596 * wire format and transmit
Sricharan R933b45d2017-08-24 12:51:34 +0530597 * @glink: The transport to transmit on.
598 * @channel: The glink channel
599 * @granted: The request response to encode.
600 *
601 * Return: 0 on success or standard Linux error code.
602 */
603static int qcom_glink_send_intent_req_ack(struct qcom_glink *glink,
604 struct glink_channel *channel,
605 bool granted)
606{
607 struct glink_msg msg;
608
609 msg.cmd = cpu_to_le16(RPM_CMD_RX_INTENT_REQ_ACK);
610 msg.param1 = cpu_to_le16(channel->lcid);
611 msg.param2 = cpu_to_le32(granted);
612
613 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
614
615 return 0;
616}
617
618/**
619 * qcom_glink_advertise_intent - convert an rx intent cmd to wire format and
620 * transmit
621 * @glink: The transport to transmit on.
622 * @channel: The local channel
Srinivas Kandagatla6f0b9582018-06-18 13:33:38 +0100623 * @intent: The intent to pass on to remote.
Sricharan R933b45d2017-08-24 12:51:34 +0530624 *
625 * Return: 0 on success or standard Linux error code.
626 */
627static int qcom_glink_advertise_intent(struct qcom_glink *glink,
628 struct glink_channel *channel,
629 struct glink_core_rx_intent *intent)
630{
631 struct command {
Srinivas Kandagatla427613e2018-06-18 13:33:37 +0100632 __le16 id;
633 __le16 lcid;
634 __le32 count;
635 __le32 size;
636 __le32 liid;
Sricharan R933b45d2017-08-24 12:51:34 +0530637 } __packed;
638 struct command cmd;
639
640 cmd.id = cpu_to_le16(RPM_CMD_INTENT);
641 cmd.lcid = cpu_to_le16(channel->lcid);
642 cmd.count = cpu_to_le32(1);
643 cmd.size = cpu_to_le32(intent->size);
644 cmd.liid = cpu_to_le32(intent->id);
645
646 qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
647
648 return 0;
649}
650
651static struct glink_core_rx_intent *
652qcom_glink_alloc_intent(struct qcom_glink *glink,
653 struct glink_channel *channel,
654 size_t size,
655 bool reuseable)
656{
657 struct glink_core_rx_intent *intent;
658 int ret;
659 unsigned long flags;
660
661 intent = kzalloc(sizeof(*intent), GFP_KERNEL);
Sricharan R933b45d2017-08-24 12:51:34 +0530662 if (!intent)
663 return NULL;
664
665 intent->data = kzalloc(size, GFP_KERNEL);
666 if (!intent->data)
Dan Carpenterb775d152017-09-08 13:34:42 +0300667 goto free_intent;
Sricharan R933b45d2017-08-24 12:51:34 +0530668
669 spin_lock_irqsave(&channel->intent_lock, flags);
670 ret = idr_alloc_cyclic(&channel->liids, intent, 1, -1, GFP_ATOMIC);
671 if (ret < 0) {
672 spin_unlock_irqrestore(&channel->intent_lock, flags);
Dan Carpenterb775d152017-09-08 13:34:42 +0300673 goto free_data;
Sricharan R933b45d2017-08-24 12:51:34 +0530674 }
675 spin_unlock_irqrestore(&channel->intent_lock, flags);
676
677 intent->id = ret;
678 intent->size = size;
679 intent->reuse = reuseable;
680
681 return intent;
Dan Carpenterb775d152017-09-08 13:34:42 +0300682
683free_data:
684 kfree(intent->data);
685free_intent:
686 kfree(intent);
687 return NULL;
Sricharan R933b45d2017-08-24 12:51:34 +0530688}
689
Sricharan R88c60602017-08-24 12:51:42 +0530690static void qcom_glink_handle_rx_done(struct qcom_glink *glink,
691 u32 cid, uint32_t iid,
692 bool reuse)
693{
694 struct glink_core_rx_intent *intent;
695 struct glink_channel *channel;
696 unsigned long flags;
697
698 spin_lock_irqsave(&glink->idr_lock, flags);
699 channel = idr_find(&glink->rcids, cid);
700 spin_unlock_irqrestore(&glink->idr_lock, flags);
701 if (!channel) {
702 dev_err(glink->dev, "invalid channel id received\n");
703 return;
704 }
705
706 spin_lock_irqsave(&channel->intent_lock, flags);
707 intent = idr_find(&channel->riids, iid);
708
709 if (!intent) {
710 spin_unlock_irqrestore(&channel->intent_lock, flags);
711 dev_err(glink->dev, "invalid intent id received\n");
712 return;
713 }
714
715 intent->in_use = false;
716
717 if (!reuse) {
718 idr_remove(&channel->riids, intent->id);
719 kfree(intent);
720 }
721 spin_unlock_irqrestore(&channel->intent_lock, flags);
722}
723
Sricharan R933b45d2017-08-24 12:51:34 +0530724/**
725 * qcom_glink_handle_intent_req() - Receive a request for rx_intent
726 * from remote side
Srinivas Kandagatla6f0b9582018-06-18 13:33:38 +0100727 * @glink: Pointer to the transport interface
728 * @cid: Remote channel ID
729 * @size: size of the intent
Sricharan R933b45d2017-08-24 12:51:34 +0530730 *
731 * The function searches for the local channel to which the request for
732 * rx_intent has arrived and allocates and notifies the remote back
733 */
734static void qcom_glink_handle_intent_req(struct qcom_glink *glink,
735 u32 cid, size_t size)
736{
737 struct glink_core_rx_intent *intent;
738 struct glink_channel *channel;
739 unsigned long flags;
740
741 spin_lock_irqsave(&glink->idr_lock, flags);
742 channel = idr_find(&glink->rcids, cid);
743 spin_unlock_irqrestore(&glink->idr_lock, flags);
744
745 if (!channel) {
746 pr_err("%s channel not found for cid %d\n", __func__, cid);
747 return;
748 }
749
750 intent = qcom_glink_alloc_intent(glink, channel, size, false);
751 if (intent)
752 qcom_glink_advertise_intent(glink, channel, intent);
753
754 qcom_glink_send_intent_req_ack(glink, channel, !!intent);
755}
756
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530757static int qcom_glink_rx_defer(struct qcom_glink *glink, size_t extra)
758{
759 struct glink_defer_cmd *dcmd;
760
761 extra = ALIGN(extra, 8);
762
763 if (qcom_glink_rx_avail(glink) < sizeof(struct glink_msg) + extra) {
764 dev_dbg(glink->dev, "Insufficient data in rx fifo");
765 return -ENXIO;
766 }
767
768 dcmd = kzalloc(sizeof(*dcmd) + extra, GFP_ATOMIC);
769 if (!dcmd)
770 return -ENOMEM;
771
772 INIT_LIST_HEAD(&dcmd->node);
773
Bjorn Anderssonb88eee92017-08-24 12:51:36 +0530774 qcom_glink_rx_peak(glink, &dcmd->msg, 0, sizeof(dcmd->msg) + extra);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530775
776 spin_lock(&glink->rx_lock);
777 list_add_tail(&dcmd->node, &glink->rx_queue);
778 spin_unlock(&glink->rx_lock);
779
780 schedule_work(&glink->rx_work);
781 qcom_glink_rx_advance(glink, sizeof(dcmd->msg) + extra);
782
783 return 0;
784}
785
786static int qcom_glink_rx_data(struct qcom_glink *glink, size_t avail)
787{
Sricharan R64f95f82017-08-24 12:51:35 +0530788 struct glink_core_rx_intent *intent;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530789 struct glink_channel *channel;
790 struct {
791 struct glink_msg msg;
792 __le32 chunk_size;
793 __le32 left_size;
794 } __packed hdr;
795 unsigned int chunk_size;
796 unsigned int left_size;
797 unsigned int rcid;
Sricharan R64f95f82017-08-24 12:51:35 +0530798 unsigned int liid;
799 int ret = 0;
Sricharan R44f6df92017-08-24 12:51:33 +0530800 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530801
802 if (avail < sizeof(hdr)) {
803 dev_dbg(glink->dev, "Not enough data in fifo\n");
804 return -EAGAIN;
805 }
806
Bjorn Anderssonb88eee92017-08-24 12:51:36 +0530807 qcom_glink_rx_peak(glink, &hdr, 0, sizeof(hdr));
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530808 chunk_size = le32_to_cpu(hdr.chunk_size);
809 left_size = le32_to_cpu(hdr.left_size);
810
811 if (avail < sizeof(hdr) + chunk_size) {
812 dev_dbg(glink->dev, "Payload not yet in fifo\n");
813 return -EAGAIN;
814 }
815
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530816 rcid = le16_to_cpu(hdr.msg.param1);
Sricharan R44f6df92017-08-24 12:51:33 +0530817 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530818 channel = idr_find(&glink->rcids, rcid);
Sricharan R44f6df92017-08-24 12:51:33 +0530819 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530820 if (!channel) {
821 dev_dbg(glink->dev, "Data on non-existing channel\n");
822
823 /* Drop the message */
Sricharan R64f95f82017-08-24 12:51:35 +0530824 goto advance_rx;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530825 }
826
Sricharan R64f95f82017-08-24 12:51:35 +0530827 if (glink->intentless) {
828 /* Might have an ongoing, fragmented, message to append */
829 if (!channel->buf) {
830 intent = kzalloc(sizeof(*intent), GFP_ATOMIC);
831 if (!intent)
832 return -ENOMEM;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530833
Sricharan R64f95f82017-08-24 12:51:35 +0530834 intent->data = kmalloc(chunk_size + left_size,
835 GFP_ATOMIC);
836 if (!intent->data) {
837 kfree(intent);
838 return -ENOMEM;
839 }
840
841 intent->id = 0xbabababa;
842 intent->size = chunk_size + left_size;
843 intent->offset = 0;
844
845 channel->buf = intent;
846 } else {
847 intent = channel->buf;
848 }
849 } else {
850 liid = le32_to_cpu(hdr.msg.param2);
851
852 spin_lock_irqsave(&channel->intent_lock, flags);
853 intent = idr_find(&channel->liids, liid);
854 spin_unlock_irqrestore(&channel->intent_lock, flags);
855
856 if (!intent) {
857 dev_err(glink->dev,
858 "no intent found for channel %s intent %d",
859 channel->name, liid);
860 goto advance_rx;
861 }
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530862 }
863
Sricharan R64f95f82017-08-24 12:51:35 +0530864 if (intent->size - intent->offset < chunk_size) {
865 dev_err(glink->dev, "Insufficient space in intent\n");
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530866
867 /* The packet header lied, drop payload */
Sricharan R64f95f82017-08-24 12:51:35 +0530868 goto advance_rx;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530869 }
870
Sricharan R64f95f82017-08-24 12:51:35 +0530871 qcom_glink_rx_peak(glink, intent->data + intent->offset,
Bjorn Anderssonb88eee92017-08-24 12:51:36 +0530872 sizeof(hdr), chunk_size);
Sricharan R64f95f82017-08-24 12:51:35 +0530873 intent->offset += chunk_size;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530874
875 /* Handle message when no fragments remain to be received */
876 if (!left_size) {
877 spin_lock(&channel->recv_lock);
878 if (channel->ept.cb) {
879 channel->ept.cb(channel->ept.rpdev,
Sricharan R64f95f82017-08-24 12:51:35 +0530880 intent->data,
881 intent->offset,
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530882 channel->ept.priv,
883 RPMSG_ADDR_ANY);
884 }
885 spin_unlock(&channel->recv_lock);
886
Sricharan R64f95f82017-08-24 12:51:35 +0530887 intent->offset = 0;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530888 channel->buf = NULL;
Sricharan R1d2ea362017-08-24 12:51:37 +0530889
890 qcom_glink_rx_done(glink, channel, intent);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530891 }
892
Sricharan R64f95f82017-08-24 12:51:35 +0530893advance_rx:
Bjorn Anderssonb88eee92017-08-24 12:51:36 +0530894 qcom_glink_rx_advance(glink, ALIGN(sizeof(hdr) + chunk_size, 8));
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530895
Sricharan R64f95f82017-08-24 12:51:35 +0530896 return ret;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530897}
898
Sricharan Rdacbb352017-08-24 12:51:39 +0530899static void qcom_glink_handle_intent(struct qcom_glink *glink,
900 unsigned int cid,
901 unsigned int count,
902 size_t avail)
903{
904 struct glink_core_rx_intent *intent;
905 struct glink_channel *channel;
906 struct intent_pair {
907 __le32 size;
908 __le32 iid;
909 };
910
911 struct {
912 struct glink_msg msg;
913 struct intent_pair intents[];
914 } __packed * msg;
915
Gustavo A. R. Silva61c65f42019-08-29 13:17:21 -0500916 const size_t msglen = struct_size(msg, intents, count);
Sricharan Rdacbb352017-08-24 12:51:39 +0530917 int ret;
918 int i;
919 unsigned long flags;
920
921 if (avail < msglen) {
922 dev_dbg(glink->dev, "Not enough data in fifo\n");
923 return;
924 }
925
926 spin_lock_irqsave(&glink->idr_lock, flags);
927 channel = idr_find(&glink->rcids, cid);
928 spin_unlock_irqrestore(&glink->idr_lock, flags);
929 if (!channel) {
930 dev_err(glink->dev, "intents for non-existing channel\n");
931 return;
932 }
933
934 msg = kmalloc(msglen, GFP_ATOMIC);
935 if (!msg)
936 return;
937
938 qcom_glink_rx_peak(glink, msg, 0, msglen);
939
940 for (i = 0; i < count; ++i) {
941 intent = kzalloc(sizeof(*intent), GFP_ATOMIC);
942 if (!intent)
943 break;
944
945 intent->id = le32_to_cpu(msg->intents[i].iid);
946 intent->size = le32_to_cpu(msg->intents[i].size);
947
948 spin_lock_irqsave(&channel->intent_lock, flags);
949 ret = idr_alloc(&channel->riids, intent,
950 intent->id, intent->id + 1, GFP_ATOMIC);
951 spin_unlock_irqrestore(&channel->intent_lock, flags);
952
953 if (ret < 0)
954 dev_err(glink->dev, "failed to store remote intent\n");
955 }
956
957 kfree(msg);
958 qcom_glink_rx_advance(glink, ALIGN(msglen, 8));
959}
960
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530961static int qcom_glink_rx_open_ack(struct qcom_glink *glink, unsigned int lcid)
962{
963 struct glink_channel *channel;
964
Sricharan R44f6df92017-08-24 12:51:33 +0530965 spin_lock(&glink->idr_lock);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530966 channel = idr_find(&glink->lcids, lcid);
Bjorn Andersson5f5423f2017-08-30 16:26:23 -0700967 spin_unlock(&glink->idr_lock);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530968 if (!channel) {
969 dev_err(glink->dev, "Invalid open ack packet\n");
970 return -EINVAL;
971 }
972
973 complete(&channel->open_ack);
974
975 return 0;
976}
977
978static irqreturn_t qcom_glink_native_intr(int irq, void *data)
979{
980 struct qcom_glink *glink = data;
981 struct glink_msg msg;
982 unsigned int param1;
983 unsigned int param2;
984 unsigned int avail;
985 unsigned int cmd;
Colin Ian Kinged43d092017-09-03 14:06:01 +0100986 int ret = 0;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530987
988 for (;;) {
989 avail = qcom_glink_rx_avail(glink);
990 if (avail < sizeof(msg))
991 break;
992
Bjorn Anderssonb88eee92017-08-24 12:51:36 +0530993 qcom_glink_rx_peak(glink, &msg, 0, sizeof(msg));
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530994
995 cmd = le16_to_cpu(msg.cmd);
996 param1 = le16_to_cpu(msg.param1);
997 param2 = le32_to_cpu(msg.param2);
998
999 switch (cmd) {
1000 case RPM_CMD_VERSION:
1001 case RPM_CMD_VERSION_ACK:
1002 case RPM_CMD_CLOSE:
1003 case RPM_CMD_CLOSE_ACK:
Sricharan R933b45d2017-08-24 12:51:34 +05301004 case RPM_CMD_RX_INTENT_REQ:
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301005 ret = qcom_glink_rx_defer(glink, 0);
1006 break;
1007 case RPM_CMD_OPEN_ACK:
1008 ret = qcom_glink_rx_open_ack(glink, param1);
1009 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1010 break;
1011 case RPM_CMD_OPEN:
1012 ret = qcom_glink_rx_defer(glink, param2);
1013 break;
1014 case RPM_CMD_TX_DATA:
1015 case RPM_CMD_TX_DATA_CONT:
1016 ret = qcom_glink_rx_data(glink, avail);
1017 break;
1018 case RPM_CMD_READ_NOTIF:
1019 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1020
1021 mbox_send_message(glink->mbox_chan, NULL);
1022 mbox_client_txdone(glink->mbox_chan, 0);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301023 break;
Sricharan Rdacbb352017-08-24 12:51:39 +05301024 case RPM_CMD_INTENT:
1025 qcom_glink_handle_intent(glink, param1, param2, avail);
1026 break;
Sricharan R88c60602017-08-24 12:51:42 +05301027 case RPM_CMD_RX_DONE:
1028 qcom_glink_handle_rx_done(glink, param1, param2, false);
1029 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1030 break;
1031 case RPM_CMD_RX_DONE_W_REUSE:
1032 qcom_glink_handle_rx_done(glink, param1, param2, true);
1033 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1034 break;
Sricharan R27b9c5b2017-08-24 12:51:41 +05301035 case RPM_CMD_RX_INTENT_REQ_ACK:
1036 qcom_glink_handle_intent_req_ack(glink, param1, param2);
1037 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1038 break;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301039 default:
1040 dev_err(glink->dev, "unhandled rx cmd: %d\n", cmd);
1041 ret = -EINVAL;
1042 break;
1043 }
1044
1045 if (ret)
1046 break;
1047 }
1048
1049 return IRQ_HANDLED;
1050}
1051
1052/* Locally initiated rpmsg_create_ept */
1053static struct glink_channel *qcom_glink_create_local(struct qcom_glink *glink,
1054 const char *name)
1055{
1056 struct glink_channel *channel;
1057 int ret;
Sricharan R44f6df92017-08-24 12:51:33 +05301058 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301059
1060 channel = qcom_glink_alloc_channel(glink, name);
1061 if (IS_ERR(channel))
1062 return ERR_CAST(channel);
1063
1064 ret = qcom_glink_send_open_req(glink, channel);
1065 if (ret)
1066 goto release_channel;
1067
1068 ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
1069 if (!ret)
1070 goto err_timeout;
1071
1072 ret = wait_for_completion_timeout(&channel->open_req, 5 * HZ);
1073 if (!ret)
1074 goto err_timeout;
1075
1076 qcom_glink_send_open_ack(glink, channel);
1077
1078 return channel;
1079
1080err_timeout:
1081 /* qcom_glink_send_open_req() did register the channel in lcids*/
Sricharan R44f6df92017-08-24 12:51:33 +05301082 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301083 idr_remove(&glink->lcids, channel->lcid);
Sricharan R44f6df92017-08-24 12:51:33 +05301084 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301085
1086release_channel:
1087 /* Release qcom_glink_send_open_req() reference */
1088 kref_put(&channel->refcount, qcom_glink_channel_release);
1089 /* Release qcom_glink_alloc_channel() reference */
1090 kref_put(&channel->refcount, qcom_glink_channel_release);
1091
1092 return ERR_PTR(-ETIMEDOUT);
1093}
1094
1095/* Remote initiated rpmsg_create_ept */
1096static int qcom_glink_create_remote(struct qcom_glink *glink,
1097 struct glink_channel *channel)
1098{
1099 int ret;
1100
1101 qcom_glink_send_open_ack(glink, channel);
1102
1103 ret = qcom_glink_send_open_req(glink, channel);
1104 if (ret)
1105 goto close_link;
1106
1107 ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
1108 if (!ret) {
1109 ret = -ETIMEDOUT;
1110 goto close_link;
1111 }
1112
1113 return 0;
1114
1115close_link:
1116 /*
1117 * Send a close request to "undo" our open-ack. The close-ack will
Arun Kumar Neelakantamac74ea02019-10-04 15:26:58 -07001118 * release qcom_glink_send_open_req() reference and the last reference
1119 * will be relesed after receiving remote_close or transport unregister
1120 * by calling qcom_glink_native_remove().
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301121 */
1122 qcom_glink_send_close_req(glink, channel);
1123
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301124 return ret;
1125}
1126
1127static struct rpmsg_endpoint *qcom_glink_create_ept(struct rpmsg_device *rpdev,
1128 rpmsg_rx_cb_t cb,
1129 void *priv,
1130 struct rpmsg_channel_info
1131 chinfo)
1132{
1133 struct glink_channel *parent = to_glink_channel(rpdev->ept);
1134 struct glink_channel *channel;
1135 struct qcom_glink *glink = parent->glink;
1136 struct rpmsg_endpoint *ept;
1137 const char *name = chinfo.name;
1138 int cid;
1139 int ret;
Sricharan R44f6df92017-08-24 12:51:33 +05301140 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301141
Sricharan R44f6df92017-08-24 12:51:33 +05301142 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301143 idr_for_each_entry(&glink->rcids, channel, cid) {
1144 if (!strcmp(channel->name, name))
1145 break;
1146 }
Sricharan R44f6df92017-08-24 12:51:33 +05301147 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301148
1149 if (!channel) {
1150 channel = qcom_glink_create_local(glink, name);
1151 if (IS_ERR(channel))
1152 return NULL;
1153 } else {
1154 ret = qcom_glink_create_remote(glink, channel);
1155 if (ret)
1156 return NULL;
1157 }
1158
1159 ept = &channel->ept;
1160 ept->rpdev = rpdev;
1161 ept->cb = cb;
1162 ept->priv = priv;
1163 ept->ops = &glink_endpoint_ops;
1164
1165 return ept;
1166}
1167
Sricharan R6bf68cb2017-08-24 12:51:38 +05301168static int qcom_glink_announce_create(struct rpmsg_device *rpdev)
1169{
1170 struct glink_channel *channel = to_glink_channel(rpdev->ept);
Chris Lew07389372017-10-26 15:28:55 -07001171 struct device_node *np = rpdev->dev.of_node;
Sricharan R6bf68cb2017-08-24 12:51:38 +05301172 struct qcom_glink *glink = channel->glink;
Chris Lew07389372017-10-26 15:28:55 -07001173 struct glink_core_rx_intent *intent;
1174 const struct property *prop = NULL;
1175 __be32 defaults[] = { cpu_to_be32(SZ_1K), cpu_to_be32(5) };
1176 int num_intents;
1177 int num_groups = 1;
1178 __be32 *val = defaults;
1179 int size;
Sricharan R6bf68cb2017-08-24 12:51:38 +05301180
Chris Lew07389372017-10-26 15:28:55 -07001181 if (glink->intentless)
1182 return 0;
Sricharan R6bf68cb2017-08-24 12:51:38 +05301183
Chris Lew07389372017-10-26 15:28:55 -07001184 prop = of_find_property(np, "qcom,intents", NULL);
1185 if (prop) {
1186 val = prop->value;
1187 num_groups = prop->length / sizeof(u32) / 2;
Sricharan R6bf68cb2017-08-24 12:51:38 +05301188 }
1189
Chris Lew07389372017-10-26 15:28:55 -07001190 /* Channel is now open, advertise base set of intents */
1191 while (num_groups--) {
1192 size = be32_to_cpup(val++);
1193 num_intents = be32_to_cpup(val++);
1194 while (num_intents--) {
1195 intent = qcom_glink_alloc_intent(glink, channel, size,
1196 true);
1197 if (!intent)
1198 break;
1199
1200 qcom_glink_advertise_intent(glink, channel, intent);
1201 }
1202 }
Sricharan R6bf68cb2017-08-24 12:51:38 +05301203 return 0;
1204}
1205
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301206static void qcom_glink_destroy_ept(struct rpmsg_endpoint *ept)
1207{
1208 struct glink_channel *channel = to_glink_channel(ept);
1209 struct qcom_glink *glink = channel->glink;
1210 unsigned long flags;
1211
1212 spin_lock_irqsave(&channel->recv_lock, flags);
1213 channel->ept.cb = NULL;
1214 spin_unlock_irqrestore(&channel->recv_lock, flags);
1215
1216 /* Decouple the potential rpdev from the channel */
1217 channel->rpdev = NULL;
1218
1219 qcom_glink_send_close_req(glink, channel);
1220}
1221
Sricharan R27b9c5b2017-08-24 12:51:41 +05301222static int qcom_glink_request_intent(struct qcom_glink *glink,
1223 struct glink_channel *channel,
1224 size_t size)
1225{
1226 struct {
1227 u16 id;
1228 u16 cid;
1229 u32 size;
1230 } __packed cmd;
1231
1232 int ret;
1233
1234 mutex_lock(&channel->intent_req_lock);
1235
1236 reinit_completion(&channel->intent_req_comp);
1237
1238 cmd.id = RPM_CMD_RX_INTENT_REQ;
1239 cmd.cid = channel->lcid;
1240 cmd.size = size;
1241
1242 ret = qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
1243 if (ret)
Dan Carpenter0a7480b2017-09-08 13:33:45 +03001244 goto unlock;
Sricharan R27b9c5b2017-08-24 12:51:41 +05301245
1246 ret = wait_for_completion_timeout(&channel->intent_req_comp, 10 * HZ);
1247 if (!ret) {
1248 dev_err(glink->dev, "intent request timed out\n");
1249 ret = -ETIMEDOUT;
1250 } else {
1251 ret = channel->intent_req_result ? 0 : -ECANCELED;
1252 }
1253
Dan Carpenter0a7480b2017-09-08 13:33:45 +03001254unlock:
Sricharan R27b9c5b2017-08-24 12:51:41 +05301255 mutex_unlock(&channel->intent_req_lock);
1256 return ret;
1257}
1258
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301259static int __qcom_glink_send(struct glink_channel *channel,
1260 void *data, int len, bool wait)
1261{
1262 struct qcom_glink *glink = channel->glink;
Sricharan R11cb45a2017-08-24 12:51:40 +05301263 struct glink_core_rx_intent *intent = NULL;
1264 struct glink_core_rx_intent *tmp;
1265 int iid = 0;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301266 struct {
1267 struct glink_msg msg;
1268 __le32 chunk_size;
1269 __le32 left_size;
1270 } __packed req;
Sricharan R11cb45a2017-08-24 12:51:40 +05301271 int ret;
1272 unsigned long flags;
1273
1274 if (!glink->intentless) {
Sricharan R27b9c5b2017-08-24 12:51:41 +05301275 while (!intent) {
Sricharan R11cb45a2017-08-24 12:51:40 +05301276 spin_lock_irqsave(&channel->intent_lock, flags);
1277 idr_for_each_entry(&channel->riids, tmp, iid) {
1278 if (tmp->size >= len && !tmp->in_use) {
Chris Lew29031872017-10-26 15:28:56 -07001279 if (!intent)
1280 intent = tmp;
1281 else if (intent->size > tmp->size)
1282 intent = tmp;
1283 if (intent->size == len)
1284 break;
Sricharan R11cb45a2017-08-24 12:51:40 +05301285 }
1286 }
Chris Lew29031872017-10-26 15:28:56 -07001287 if (intent)
1288 intent->in_use = true;
Sricharan R11cb45a2017-08-24 12:51:40 +05301289 spin_unlock_irqrestore(&channel->intent_lock, flags);
1290
1291 /* We found an available intent */
Sricharan R27b9c5b2017-08-24 12:51:41 +05301292 if (intent)
1293 break;
1294
1295 if (!wait)
Sricharan R11cb45a2017-08-24 12:51:40 +05301296 return -EBUSY;
Sricharan R27b9c5b2017-08-24 12:51:41 +05301297
1298 ret = qcom_glink_request_intent(glink, channel, len);
1299 if (ret < 0)
1300 return ret;
Sricharan R11cb45a2017-08-24 12:51:40 +05301301 }
1302
1303 iid = intent->id;
1304 }
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301305
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301306 req.msg.cmd = cpu_to_le16(RPM_CMD_TX_DATA);
1307 req.msg.param1 = cpu_to_le16(channel->lcid);
Sricharan R11cb45a2017-08-24 12:51:40 +05301308 req.msg.param2 = cpu_to_le32(iid);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301309 req.chunk_size = cpu_to_le32(len);
1310 req.left_size = cpu_to_le32(0);
1311
Sricharan R11cb45a2017-08-24 12:51:40 +05301312 ret = qcom_glink_tx(glink, &req, sizeof(req), data, len, wait);
1313
1314 /* Mark intent available if we failed */
Colin Ian King906cf292017-09-03 13:46:14 +01001315 if (ret && intent)
Sricharan R11cb45a2017-08-24 12:51:40 +05301316 intent->in_use = false;
1317
1318 return ret;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301319}
1320
1321static int qcom_glink_send(struct rpmsg_endpoint *ept, void *data, int len)
1322{
1323 struct glink_channel *channel = to_glink_channel(ept);
1324
1325 return __qcom_glink_send(channel, data, len, true);
1326}
1327
1328static int qcom_glink_trysend(struct rpmsg_endpoint *ept, void *data, int len)
1329{
1330 struct glink_channel *channel = to_glink_channel(ept);
1331
1332 return __qcom_glink_send(channel, data, len, false);
1333}
1334
1335/*
1336 * Finds the device_node for the glink child interested in this channel.
1337 */
1338static struct device_node *qcom_glink_match_channel(struct device_node *node,
1339 const char *channel)
1340{
1341 struct device_node *child;
1342 const char *name;
1343 const char *key;
1344 int ret;
1345
1346 for_each_available_child_of_node(node, child) {
1347 key = "qcom,glink-channels";
1348 ret = of_property_read_string(child, key, &name);
1349 if (ret)
1350 continue;
1351
1352 if (strcmp(name, channel) == 0)
1353 return child;
1354 }
1355
1356 return NULL;
1357}
1358
1359static const struct rpmsg_device_ops glink_device_ops = {
1360 .create_ept = qcom_glink_create_ept,
Sricharan R6bf68cb2017-08-24 12:51:38 +05301361 .announce_create = qcom_glink_announce_create,
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301362};
1363
1364static const struct rpmsg_endpoint_ops glink_endpoint_ops = {
1365 .destroy_ept = qcom_glink_destroy_ept,
1366 .send = qcom_glink_send,
1367 .trysend = qcom_glink_trysend,
1368};
1369
1370static void qcom_glink_rpdev_release(struct device *dev)
1371{
1372 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
1373 struct glink_channel *channel = to_glink_channel(rpdev->ept);
1374
1375 channel->rpdev = NULL;
1376 kfree(rpdev);
1377}
1378
1379static int qcom_glink_rx_open(struct qcom_glink *glink, unsigned int rcid,
1380 char *name)
1381{
1382 struct glink_channel *channel;
1383 struct rpmsg_device *rpdev;
1384 bool create_device = false;
1385 struct device_node *node;
1386 int lcid;
1387 int ret;
Sricharan R44f6df92017-08-24 12:51:33 +05301388 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301389
Sricharan R44f6df92017-08-24 12:51:33 +05301390 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301391 idr_for_each_entry(&glink->lcids, channel, lcid) {
1392 if (!strcmp(channel->name, name))
1393 break;
1394 }
Sricharan R44f6df92017-08-24 12:51:33 +05301395 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301396
1397 if (!channel) {
1398 channel = qcom_glink_alloc_channel(glink, name);
1399 if (IS_ERR(channel))
1400 return PTR_ERR(channel);
1401
1402 /* The opening dance was initiated by the remote */
1403 create_device = true;
1404 }
1405
Sricharan R44f6df92017-08-24 12:51:33 +05301406 spin_lock_irqsave(&glink->idr_lock, flags);
1407 ret = idr_alloc(&glink->rcids, channel, rcid, rcid + 1, GFP_ATOMIC);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301408 if (ret < 0) {
1409 dev_err(glink->dev, "Unable to insert channel into rcid list\n");
Sricharan R44f6df92017-08-24 12:51:33 +05301410 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301411 goto free_channel;
1412 }
1413 channel->rcid = ret;
Sricharan R44f6df92017-08-24 12:51:33 +05301414 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301415
1416 complete(&channel->open_req);
1417
1418 if (create_device) {
1419 rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL);
1420 if (!rpdev) {
1421 ret = -ENOMEM;
1422 goto rcid_remove;
1423 }
1424
1425 rpdev->ept = &channel->ept;
1426 strncpy(rpdev->id.name, name, RPMSG_NAME_SIZE);
1427 rpdev->src = RPMSG_ADDR_ANY;
1428 rpdev->dst = RPMSG_ADDR_ANY;
1429 rpdev->ops = &glink_device_ops;
1430
1431 node = qcom_glink_match_channel(glink->dev->of_node, name);
1432 rpdev->dev.of_node = node;
1433 rpdev->dev.parent = glink->dev;
1434 rpdev->dev.release = qcom_glink_rpdev_release;
1435
1436 ret = rpmsg_register_device(rpdev);
1437 if (ret)
Chris Lewf7e71492019-10-04 15:27:00 -07001438 goto rcid_remove;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301439
1440 channel->rpdev = rpdev;
1441 }
1442
1443 return 0;
1444
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301445rcid_remove:
Sricharan R44f6df92017-08-24 12:51:33 +05301446 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301447 idr_remove(&glink->rcids, channel->rcid);
1448 channel->rcid = 0;
Sricharan R44f6df92017-08-24 12:51:33 +05301449 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301450free_channel:
1451 /* Release the reference, iff we took it */
1452 if (create_device)
1453 kref_put(&channel->refcount, qcom_glink_channel_release);
1454
1455 return ret;
1456}
1457
1458static void qcom_glink_rx_close(struct qcom_glink *glink, unsigned int rcid)
1459{
1460 struct rpmsg_channel_info chinfo;
1461 struct glink_channel *channel;
Sricharan R44f6df92017-08-24 12:51:33 +05301462 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301463
Sricharan R44f6df92017-08-24 12:51:33 +05301464 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301465 channel = idr_find(&glink->rcids, rcid);
Sricharan R44f6df92017-08-24 12:51:33 +05301466 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301467 if (WARN(!channel, "close request on unknown channel\n"))
1468 return;
1469
Sricharan R1d2ea362017-08-24 12:51:37 +05301470 /* cancel pending rx_done work */
1471 cancel_work_sync(&channel->intent_work);
1472
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301473 if (channel->rpdev) {
1474 strncpy(chinfo.name, channel->name, sizeof(chinfo.name));
1475 chinfo.src = RPMSG_ADDR_ANY;
1476 chinfo.dst = RPMSG_ADDR_ANY;
1477
1478 rpmsg_unregister_device(glink->dev, &chinfo);
1479 }
1480
1481 qcom_glink_send_close_ack(glink, channel->rcid);
1482
Sricharan R44f6df92017-08-24 12:51:33 +05301483 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301484 idr_remove(&glink->rcids, channel->rcid);
1485 channel->rcid = 0;
Sricharan R44f6df92017-08-24 12:51:33 +05301486 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301487
1488 kref_put(&channel->refcount, qcom_glink_channel_release);
1489}
1490
1491static void qcom_glink_rx_close_ack(struct qcom_glink *glink, unsigned int lcid)
1492{
1493 struct glink_channel *channel;
Sricharan R44f6df92017-08-24 12:51:33 +05301494 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301495
Sricharan R44f6df92017-08-24 12:51:33 +05301496 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301497 channel = idr_find(&glink->lcids, lcid);
Sricharan R44f6df92017-08-24 12:51:33 +05301498 if (WARN(!channel, "close ack on unknown channel\n")) {
1499 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301500 return;
Sricharan R44f6df92017-08-24 12:51:33 +05301501 }
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301502
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301503 idr_remove(&glink->lcids, channel->lcid);
1504 channel->lcid = 0;
Sricharan R44f6df92017-08-24 12:51:33 +05301505 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301506
1507 kref_put(&channel->refcount, qcom_glink_channel_release);
1508}
1509
1510static void qcom_glink_work(struct work_struct *work)
1511{
1512 struct qcom_glink *glink = container_of(work, struct qcom_glink,
1513 rx_work);
1514 struct glink_defer_cmd *dcmd;
1515 struct glink_msg *msg;
1516 unsigned long flags;
1517 unsigned int param1;
1518 unsigned int param2;
1519 unsigned int cmd;
1520
1521 for (;;) {
1522 spin_lock_irqsave(&glink->rx_lock, flags);
1523 if (list_empty(&glink->rx_queue)) {
1524 spin_unlock_irqrestore(&glink->rx_lock, flags);
1525 break;
1526 }
1527 dcmd = list_first_entry(&glink->rx_queue,
1528 struct glink_defer_cmd, node);
1529 list_del(&dcmd->node);
1530 spin_unlock_irqrestore(&glink->rx_lock, flags);
1531
1532 msg = &dcmd->msg;
1533 cmd = le16_to_cpu(msg->cmd);
1534 param1 = le16_to_cpu(msg->param1);
1535 param2 = le32_to_cpu(msg->param2);
1536
1537 switch (cmd) {
1538 case RPM_CMD_VERSION:
Sricharan Rd31ad612017-08-24 12:51:32 +05301539 qcom_glink_receive_version(glink, param1, param2);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301540 break;
1541 case RPM_CMD_VERSION_ACK:
Sricharan Rd31ad612017-08-24 12:51:32 +05301542 qcom_glink_receive_version_ack(glink, param1, param2);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301543 break;
1544 case RPM_CMD_OPEN:
1545 qcom_glink_rx_open(glink, param1, msg->data);
1546 break;
1547 case RPM_CMD_CLOSE:
1548 qcom_glink_rx_close(glink, param1);
1549 break;
1550 case RPM_CMD_CLOSE_ACK:
1551 qcom_glink_rx_close_ack(glink, param1);
1552 break;
Sricharan R933b45d2017-08-24 12:51:34 +05301553 case RPM_CMD_RX_INTENT_REQ:
1554 qcom_glink_handle_intent_req(glink, param1, param2);
1555 break;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301556 default:
1557 WARN(1, "Unknown defer object %d\n", cmd);
1558 break;
1559 }
1560
1561 kfree(dcmd);
1562 }
1563}
1564
Bjorn Andersson278bcb72019-10-04 15:27:02 -07001565static void qcom_glink_cancel_rx_work(struct qcom_glink *glink)
1566{
1567 struct glink_defer_cmd *dcmd;
1568 struct glink_defer_cmd *tmp;
1569
1570 /* cancel any pending deferred rx_work */
1571 cancel_work_sync(&glink->rx_work);
1572
1573 list_for_each_entry_safe(dcmd, tmp, &glink->rx_queue, node)
1574 kfree(dcmd);
1575}
1576
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301577struct qcom_glink *qcom_glink_native_probe(struct device *dev,
Sricharan Rd31ad612017-08-24 12:51:32 +05301578 unsigned long features,
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301579 struct qcom_glink_pipe *rx,
Sricharan R933b45d2017-08-24 12:51:34 +05301580 struct qcom_glink_pipe *tx,
1581 bool intentless)
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301582{
1583 int irq;
1584 int ret;
1585 struct qcom_glink *glink;
1586
1587 glink = devm_kzalloc(dev, sizeof(*glink), GFP_KERNEL);
1588 if (!glink)
1589 return ERR_PTR(-ENOMEM);
1590
1591 glink->dev = dev;
1592 glink->tx_pipe = tx;
1593 glink->rx_pipe = rx;
1594
Sricharan Rd31ad612017-08-24 12:51:32 +05301595 glink->features = features;
Sricharan R933b45d2017-08-24 12:51:34 +05301596 glink->intentless = intentless;
Sricharan Rd31ad612017-08-24 12:51:32 +05301597
Bjorn Andersson29fc9b32018-02-13 11:04:04 -08001598 spin_lock_init(&glink->tx_lock);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301599 spin_lock_init(&glink->rx_lock);
1600 INIT_LIST_HEAD(&glink->rx_queue);
1601 INIT_WORK(&glink->rx_work, qcom_glink_work);
1602
Sricharan R44f6df92017-08-24 12:51:33 +05301603 spin_lock_init(&glink->idr_lock);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301604 idr_init(&glink->lcids);
1605 idr_init(&glink->rcids);
1606
Chris Lew475452f2018-07-27 17:47:22 +05301607 ret = of_property_read_string(dev->of_node, "label", &glink->name);
1608 if (ret < 0)
1609 glink->name = dev->of_node->name;
1610
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301611 glink->mbox_client.dev = dev;
Bjorn Andersson38a9acb2017-11-15 22:08:33 -08001612 glink->mbox_client.knows_txdone = true;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301613 glink->mbox_chan = mbox_request_channel(&glink->mbox_client, 0);
1614 if (IS_ERR(glink->mbox_chan)) {
1615 if (PTR_ERR(glink->mbox_chan) != -EPROBE_DEFER)
1616 dev_err(dev, "failed to acquire IPC channel\n");
1617 return ERR_CAST(glink->mbox_chan);
1618 }
1619
1620 irq = of_irq_get(dev->of_node, 0);
1621 ret = devm_request_irq(dev, irq,
1622 qcom_glink_native_intr,
1623 IRQF_NO_SUSPEND | IRQF_SHARED,
1624 "glink-native", glink);
1625 if (ret) {
1626 dev_err(dev, "failed to request IRQ\n");
1627 return ERR_PTR(ret);
1628 }
1629
1630 glink->irq = irq;
1631
1632 ret = qcom_glink_send_version(glink);
1633 if (ret)
1634 return ERR_PTR(ret);
1635
1636 return glink;
1637}
Bjorn Anderssonf8ff9772017-08-31 09:27:35 -07001638EXPORT_SYMBOL_GPL(qcom_glink_native_probe);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301639
1640static int qcom_glink_remove_device(struct device *dev, void *data)
1641{
1642 device_unregister(dev);
1643
1644 return 0;
1645}
1646
1647void qcom_glink_native_remove(struct qcom_glink *glink)
1648{
1649 struct glink_channel *channel;
1650 int cid;
1651 int ret;
1652
1653 disable_irq(glink->irq);
Bjorn Andersson278bcb72019-10-04 15:27:02 -07001654 qcom_glink_cancel_rx_work(glink);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301655
1656 ret = device_for_each_child(glink->dev, NULL, qcom_glink_remove_device);
1657 if (ret)
1658 dev_warn(glink->dev, "Can't remove GLINK devices: %d\n", ret);
1659
1660 /* Release any defunct local channels, waiting for close-ack */
1661 idr_for_each_entry(&glink->lcids, channel, cid)
1662 kref_put(&channel->refcount, qcom_glink_channel_release);
1663
Chris Lewb6462932019-10-04 15:26:59 -07001664 /* Release any defunct local channels, waiting for close-req */
1665 idr_for_each_entry(&glink->rcids, channel, cid)
1666 kref_put(&channel->refcount, qcom_glink_channel_release);
1667
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301668 idr_destroy(&glink->lcids);
1669 idr_destroy(&glink->rcids);
Sricharan R76cf1102017-08-24 12:51:29 +05301670 mbox_free_channel(glink->mbox_chan);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301671}
Bjorn Anderssonf8ff9772017-08-31 09:27:35 -07001672EXPORT_SYMBOL_GPL(qcom_glink_native_remove);
Bjorn Anderssoncaf989c2017-08-24 12:51:30 +05301673
1674void qcom_glink_native_unregister(struct qcom_glink *glink)
1675{
1676 device_unregister(glink->dev);
1677}
Bjorn Anderssonf8ff9772017-08-31 09:27:35 -07001678EXPORT_SYMBOL_GPL(qcom_glink_native_unregister);
Bjorn Andersson1e0d5612017-11-14 21:45:34 -08001679
1680MODULE_DESCRIPTION("Qualcomm GLINK driver");
1681MODULE_LICENSE("GPL v2");