blob: 5dccbf18046e0756e1fcaf047c30a1ddda936c80 [file] [log] [blame]
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301/*
2 * Copyright (c) 2016-2017, Linaro Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <linux/idr.h>
15#include <linux/interrupt.h>
16#include <linux/io.h>
17#include <linux/list.h>
18#include <linux/mfd/syscon.h>
19#include <linux/module.h>
20#include <linux/of.h>
21#include <linux/of_address.h>
22#include <linux/of_irq.h>
23#include <linux/platform_device.h>
24#include <linux/regmap.h>
25#include <linux/rpmsg.h>
26#include <linux/slab.h>
27#include <linux/workqueue.h>
28#include <linux/mailbox_client.h>
29
30#include "rpmsg_internal.h"
31#include "qcom_glink_native.h"
32
33#define GLINK_NAME_SIZE 32
Sricharan Rd31ad612017-08-24 12:51:32 +053034#define GLINK_VERSION_1 1
Bjorn Andersson835764dd2017-08-24 12:51:26 +053035
36#define RPM_GLINK_CID_MIN 1
37#define RPM_GLINK_CID_MAX 65536
38
39struct glink_msg {
40 __le16 cmd;
41 __le16 param1;
42 __le32 param2;
43 u8 data[];
44} __packed;
45
46/**
47 * struct glink_defer_cmd - deferred incoming control message
48 * @node: list node
49 * @msg: message header
50 * data: payload of the message
51 *
52 * Copy of a received control message, to be added to @rx_queue and processed
53 * by @rx_work of @qcom_glink.
54 */
55struct glink_defer_cmd {
56 struct list_head node;
57
58 struct glink_msg msg;
59 u8 data[];
60};
61
62/**
Sricharan R933b45d2017-08-24 12:51:34 +053063 * struct glink_core_rx_intent - RX intent
64 * RX intent
65 *
66 * data: pointer to the data (may be NULL for zero-copy)
67 * id: remote or local intent ID
68 * size: size of the original intent (do not modify)
69 * reuse: To mark if the intent can be reused after first use
70 * in_use: To mark if intent is already in use for the channel
71 * offset: next write offset (initially 0)
72 */
73struct glink_core_rx_intent {
74 void *data;
75 u32 id;
76 size_t size;
77 bool reuse;
78 bool in_use;
79 u32 offset;
80};
81
82/**
Bjorn Andersson835764dd2017-08-24 12:51:26 +053083 * struct qcom_glink - driver context, relates to one remote subsystem
84 * @dev: reference to the associated struct device
85 * @mbox_client: mailbox client
86 * @mbox_chan: mailbox channel
87 * @rx_pipe: pipe object for receive FIFO
88 * @tx_pipe: pipe object for transmit FIFO
89 * @irq: IRQ for signaling incoming events
90 * @rx_work: worker for handling received control messages
91 * @rx_lock: protects the @rx_queue
92 * @rx_queue: queue of received control messages to be processed in @rx_work
93 * @tx_lock: synchronizes operations on the tx fifo
94 * @idr_lock: synchronizes @lcids and @rcids modifications
95 * @lcids: idr of all channels with a known local channel id
96 * @rcids: idr of all channels with a known remote channel id
97 */
98struct qcom_glink {
99 struct device *dev;
100
101 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
113 struct mutex tx_lock;
114
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 R933b45d2017-08-24 12:51:34 +0530140 * @intent_lock: lock for protection of @liids
141 * @liids: idr of all local intents
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530142 * @buf: receive buffer, for gathering fragments
143 * @buf_offset: write offset in @buf
144 * @buf_size: size of current @buf
145 * @open_ack: completed once remote has acked the open-request
146 * @open_req: completed once open-request has been received
147 */
148struct glink_channel {
149 struct rpmsg_endpoint ept;
150
151 struct rpmsg_device *rpdev;
152 struct qcom_glink *glink;
153
154 struct kref refcount;
155
156 spinlock_t recv_lock;
157
158 char *name;
159 unsigned int lcid;
160 unsigned int rcid;
161
Sricharan R933b45d2017-08-24 12:51:34 +0530162 spinlock_t intent_lock;
163 struct idr liids;
164
Sricharan R64f95f82017-08-24 12:51:35 +0530165 struct glink_core_rx_intent *buf;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530166 int buf_offset;
167 int buf_size;
168
169 struct completion open_ack;
170 struct completion open_req;
171};
172
173#define to_glink_channel(_ept) container_of(_ept, struct glink_channel, ept)
174
175static const struct rpmsg_endpoint_ops glink_endpoint_ops;
176
177#define RPM_CMD_VERSION 0
178#define RPM_CMD_VERSION_ACK 1
179#define RPM_CMD_OPEN 2
180#define RPM_CMD_CLOSE 3
181#define RPM_CMD_OPEN_ACK 4
Sricharan R933b45d2017-08-24 12:51:34 +0530182#define RPM_CMD_INTENT 5
183#define RPM_CMD_RX_INTENT_REQ 7
184#define RPM_CMD_RX_INTENT_REQ_ACK 8
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530185#define RPM_CMD_TX_DATA 9
186#define RPM_CMD_CLOSE_ACK 11
187#define RPM_CMD_TX_DATA_CONT 12
188#define RPM_CMD_READ_NOTIF 13
189
190#define GLINK_FEATURE_INTENTLESS BIT(1)
191
192static struct glink_channel *qcom_glink_alloc_channel(struct qcom_glink *glink,
193 const char *name)
194{
195 struct glink_channel *channel;
196
197 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
198 if (!channel)
199 return ERR_PTR(-ENOMEM);
200
201 /* Setup glink internal glink_channel data */
202 spin_lock_init(&channel->recv_lock);
Sricharan R933b45d2017-08-24 12:51:34 +0530203 spin_lock_init(&channel->intent_lock);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530204 channel->glink = glink;
205 channel->name = kstrdup(name, GFP_KERNEL);
206
207 init_completion(&channel->open_req);
208 init_completion(&channel->open_ack);
209
Sricharan R933b45d2017-08-24 12:51:34 +0530210 idr_init(&channel->liids);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530211 kref_init(&channel->refcount);
212
213 return channel;
214}
215
216static void qcom_glink_channel_release(struct kref *ref)
217{
218 struct glink_channel *channel = container_of(ref, struct glink_channel,
219 refcount);
Sricharan R933b45d2017-08-24 12:51:34 +0530220 unsigned long flags;
221
222 spin_lock_irqsave(&channel->intent_lock, flags);
223 idr_destroy(&channel->liids);
224 spin_unlock_irqrestore(&channel->intent_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530225
226 kfree(channel->name);
227 kfree(channel);
228}
229
230static size_t qcom_glink_rx_avail(struct qcom_glink *glink)
231{
232 return glink->rx_pipe->avail(glink->rx_pipe);
233}
234
235static void qcom_glink_rx_peak(struct qcom_glink *glink,
236 void *data, size_t count)
237{
238 glink->rx_pipe->peak(glink->rx_pipe, data, count);
239}
240
241static void qcom_glink_rx_advance(struct qcom_glink *glink, size_t count)
242{
243 glink->rx_pipe->advance(glink->rx_pipe, count);
244}
245
246static size_t qcom_glink_tx_avail(struct qcom_glink *glink)
247{
248 return glink->tx_pipe->avail(glink->tx_pipe);
249}
250
251static void qcom_glink_tx_write(struct qcom_glink *glink,
252 const void *hdr, size_t hlen,
253 const void *data, size_t dlen)
254{
255 glink->tx_pipe->write(glink->tx_pipe, hdr, hlen, data, dlen);
256}
257
258static int qcom_glink_tx(struct qcom_glink *glink,
259 const void *hdr, size_t hlen,
260 const void *data, size_t dlen, bool wait)
261{
262 unsigned int tlen = hlen + dlen;
263 int ret;
264
265 /* Reject packets that are too big */
266 if (tlen >= glink->tx_pipe->length)
267 return -EINVAL;
268
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530269 ret = mutex_lock_interruptible(&glink->tx_lock);
270 if (ret)
271 return ret;
272
273 while (qcom_glink_tx_avail(glink) < tlen) {
274 if (!wait) {
Sricharan Ra7df9df2017-08-24 12:51:28 +0530275 ret = -EAGAIN;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530276 goto out;
277 }
278
279 usleep_range(10000, 15000);
280 }
281
282 qcom_glink_tx_write(glink, hdr, hlen, data, dlen);
283
284 mbox_send_message(glink->mbox_chan, NULL);
285 mbox_client_txdone(glink->mbox_chan, 0);
286
287out:
288 mutex_unlock(&glink->tx_lock);
289
290 return ret;
291}
292
293static int qcom_glink_send_version(struct qcom_glink *glink)
294{
295 struct glink_msg msg;
296
297 msg.cmd = cpu_to_le16(RPM_CMD_VERSION);
Sricharan Rd31ad612017-08-24 12:51:32 +0530298 msg.param1 = cpu_to_le16(GLINK_VERSION_1);
299 msg.param2 = cpu_to_le32(glink->features);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530300
301 return qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
302}
303
304static void qcom_glink_send_version_ack(struct qcom_glink *glink)
305{
306 struct glink_msg msg;
307
308 msg.cmd = cpu_to_le16(RPM_CMD_VERSION_ACK);
Sricharan Rd31ad612017-08-24 12:51:32 +0530309 msg.param1 = cpu_to_le16(GLINK_VERSION_1);
310 msg.param2 = cpu_to_le32(glink->features);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530311
312 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
313}
314
315static void qcom_glink_send_open_ack(struct qcom_glink *glink,
316 struct glink_channel *channel)
317{
318 struct glink_msg msg;
319
320 msg.cmd = cpu_to_le16(RPM_CMD_OPEN_ACK);
321 msg.param1 = cpu_to_le16(channel->rcid);
322 msg.param2 = cpu_to_le32(0);
323
324 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
325}
326
327/**
328 * qcom_glink_send_open_req() - send a RPM_CMD_OPEN request to the remote
329 * @glink: Ptr to the glink edge
330 * @channel: Ptr to the channel that the open req is sent
331 *
332 * Allocates a local channel id and sends a RPM_CMD_OPEN message to the remote.
333 * Will return with refcount held, regardless of outcome.
334 *
335 * Returns 0 on success, negative errno otherwise.
336 */
337static int qcom_glink_send_open_req(struct qcom_glink *glink,
338 struct glink_channel *channel)
339{
340 struct {
341 struct glink_msg msg;
342 u8 name[GLINK_NAME_SIZE];
343 } __packed req;
344 int name_len = strlen(channel->name) + 1;
345 int req_len = ALIGN(sizeof(req.msg) + name_len, 8);
346 int ret;
Sricharan R44f6df92017-08-24 12:51:33 +0530347 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530348
349 kref_get(&channel->refcount);
350
Sricharan R44f6df92017-08-24 12:51:33 +0530351 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530352 ret = idr_alloc_cyclic(&glink->lcids, channel,
353 RPM_GLINK_CID_MIN, RPM_GLINK_CID_MAX,
Sricharan R44f6df92017-08-24 12:51:33 +0530354 GFP_ATOMIC);
355 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530356 if (ret < 0)
357 return ret;
358
359 channel->lcid = ret;
360
361 req.msg.cmd = cpu_to_le16(RPM_CMD_OPEN);
362 req.msg.param1 = cpu_to_le16(channel->lcid);
363 req.msg.param2 = cpu_to_le32(name_len);
364 strcpy(req.name, channel->name);
365
366 ret = qcom_glink_tx(glink, &req, req_len, NULL, 0, true);
367 if (ret)
368 goto remove_idr;
369
370 return 0;
371
372remove_idr:
Sricharan R44f6df92017-08-24 12:51:33 +0530373 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530374 idr_remove(&glink->lcids, channel->lcid);
375 channel->lcid = 0;
Sricharan R44f6df92017-08-24 12:51:33 +0530376 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530377
378 return ret;
379}
380
381static void qcom_glink_send_close_req(struct qcom_glink *glink,
382 struct glink_channel *channel)
383{
384 struct glink_msg req;
385
386 req.cmd = cpu_to_le16(RPM_CMD_CLOSE);
387 req.param1 = cpu_to_le16(channel->lcid);
388 req.param2 = 0;
389
390 qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
391}
392
393static void qcom_glink_send_close_ack(struct qcom_glink *glink,
394 unsigned int rcid)
395{
396 struct glink_msg req;
397
398 req.cmd = cpu_to_le16(RPM_CMD_CLOSE_ACK);
399 req.param1 = cpu_to_le16(rcid);
400 req.param2 = 0;
401
402 qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
403}
404
Sricharan Rd31ad612017-08-24 12:51:32 +0530405/**
406 * qcom_glink_receive_version() - receive version/features from remote system
407 *
408 * @glink: pointer to transport interface
409 * @r_version: remote version
410 * @r_features: remote features
411 *
412 * This function is called in response to a remote-initiated version/feature
413 * negotiation sequence.
414 */
415static void qcom_glink_receive_version(struct qcom_glink *glink,
416 u32 version,
417 u32 features)
418{
419 switch (version) {
420 case 0:
421 break;
422 case GLINK_VERSION_1:
423 glink->features &= features;
424 /* FALLTHROUGH */
425 default:
426 qcom_glink_send_version_ack(glink);
427 break;
428 }
429}
430
431/**
432 * qcom_glink_receive_version_ack() - receive negotiation ack from remote system
433 *
434 * @glink: pointer to transport interface
435 * @r_version: remote version response
436 * @r_features: remote features response
437 *
438 * This function is called in response to a local-initiated version/feature
439 * negotiation sequence and is the counter-offer from the remote side based
440 * upon the initial version and feature set requested.
441 */
442static void qcom_glink_receive_version_ack(struct qcom_glink *glink,
443 u32 version,
444 u32 features)
445{
446 switch (version) {
447 case 0:
448 /* Version negotiation failed */
449 break;
450 case GLINK_VERSION_1:
451 if (features == glink->features)
452 break;
453
454 glink->features &= features;
455 /* FALLTHROUGH */
456 default:
457 qcom_glink_send_version(glink);
458 break;
459 }
460}
461
Sricharan R933b45d2017-08-24 12:51:34 +0530462/**
463 * qcom_glink_send_intent_req_ack() - convert an rx intent request ack cmd to
464 wire format and transmit
465 * @glink: The transport to transmit on.
466 * @channel: The glink channel
467 * @granted: The request response to encode.
468 *
469 * Return: 0 on success or standard Linux error code.
470 */
471static int qcom_glink_send_intent_req_ack(struct qcom_glink *glink,
472 struct glink_channel *channel,
473 bool granted)
474{
475 struct glink_msg msg;
476
477 msg.cmd = cpu_to_le16(RPM_CMD_RX_INTENT_REQ_ACK);
478 msg.param1 = cpu_to_le16(channel->lcid);
479 msg.param2 = cpu_to_le32(granted);
480
481 qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
482
483 return 0;
484}
485
486/**
487 * qcom_glink_advertise_intent - convert an rx intent cmd to wire format and
488 * transmit
489 * @glink: The transport to transmit on.
490 * @channel: The local channel
491 * @size: The intent to pass on to remote.
492 *
493 * Return: 0 on success or standard Linux error code.
494 */
495static int qcom_glink_advertise_intent(struct qcom_glink *glink,
496 struct glink_channel *channel,
497 struct glink_core_rx_intent *intent)
498{
499 struct command {
500 u16 id;
501 u16 lcid;
502 u32 count;
503 u32 size;
504 u32 liid;
505 } __packed;
506 struct command cmd;
507
508 cmd.id = cpu_to_le16(RPM_CMD_INTENT);
509 cmd.lcid = cpu_to_le16(channel->lcid);
510 cmd.count = cpu_to_le32(1);
511 cmd.size = cpu_to_le32(intent->size);
512 cmd.liid = cpu_to_le32(intent->id);
513
514 qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
515
516 return 0;
517}
518
519static struct glink_core_rx_intent *
520qcom_glink_alloc_intent(struct qcom_glink *glink,
521 struct glink_channel *channel,
522 size_t size,
523 bool reuseable)
524{
525 struct glink_core_rx_intent *intent;
526 int ret;
527 unsigned long flags;
528
529 intent = kzalloc(sizeof(*intent), GFP_KERNEL);
530
531 if (!intent)
532 return NULL;
533
534 intent->data = kzalloc(size, GFP_KERNEL);
535 if (!intent->data)
536 return NULL;
537
538 spin_lock_irqsave(&channel->intent_lock, flags);
539 ret = idr_alloc_cyclic(&channel->liids, intent, 1, -1, GFP_ATOMIC);
540 if (ret < 0) {
541 spin_unlock_irqrestore(&channel->intent_lock, flags);
542 return NULL;
543 }
544 spin_unlock_irqrestore(&channel->intent_lock, flags);
545
546 intent->id = ret;
547 intent->size = size;
548 intent->reuse = reuseable;
549
550 return intent;
551}
552
553/**
554 * qcom_glink_handle_intent_req() - Receive a request for rx_intent
555 * from remote side
556 * if_ptr: Pointer to the transport interface
557 * rcid: Remote channel ID
558 * size: size of the intent
559 *
560 * The function searches for the local channel to which the request for
561 * rx_intent has arrived and allocates and notifies the remote back
562 */
563static void qcom_glink_handle_intent_req(struct qcom_glink *glink,
564 u32 cid, size_t size)
565{
566 struct glink_core_rx_intent *intent;
567 struct glink_channel *channel;
568 unsigned long flags;
569
570 spin_lock_irqsave(&glink->idr_lock, flags);
571 channel = idr_find(&glink->rcids, cid);
572 spin_unlock_irqrestore(&glink->idr_lock, flags);
573
574 if (!channel) {
575 pr_err("%s channel not found for cid %d\n", __func__, cid);
576 return;
577 }
578
579 intent = qcom_glink_alloc_intent(glink, channel, size, false);
580 if (intent)
581 qcom_glink_advertise_intent(glink, channel, intent);
582
583 qcom_glink_send_intent_req_ack(glink, channel, !!intent);
584}
585
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530586static int qcom_glink_rx_defer(struct qcom_glink *glink, size_t extra)
587{
588 struct glink_defer_cmd *dcmd;
589
590 extra = ALIGN(extra, 8);
591
592 if (qcom_glink_rx_avail(glink) < sizeof(struct glink_msg) + extra) {
593 dev_dbg(glink->dev, "Insufficient data in rx fifo");
594 return -ENXIO;
595 }
596
597 dcmd = kzalloc(sizeof(*dcmd) + extra, GFP_ATOMIC);
598 if (!dcmd)
599 return -ENOMEM;
600
601 INIT_LIST_HEAD(&dcmd->node);
602
603 qcom_glink_rx_peak(glink, &dcmd->msg, sizeof(dcmd->msg) + extra);
604
605 spin_lock(&glink->rx_lock);
606 list_add_tail(&dcmd->node, &glink->rx_queue);
607 spin_unlock(&glink->rx_lock);
608
609 schedule_work(&glink->rx_work);
610 qcom_glink_rx_advance(glink, sizeof(dcmd->msg) + extra);
611
612 return 0;
613}
614
615static int qcom_glink_rx_data(struct qcom_glink *glink, size_t avail)
616{
Sricharan R64f95f82017-08-24 12:51:35 +0530617 struct glink_core_rx_intent *intent;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530618 struct glink_channel *channel;
619 struct {
620 struct glink_msg msg;
621 __le32 chunk_size;
622 __le32 left_size;
623 } __packed hdr;
624 unsigned int chunk_size;
625 unsigned int left_size;
626 unsigned int rcid;
Sricharan R64f95f82017-08-24 12:51:35 +0530627 unsigned int liid;
628 int ret = 0;
Sricharan R44f6df92017-08-24 12:51:33 +0530629 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530630
631 if (avail < sizeof(hdr)) {
632 dev_dbg(glink->dev, "Not enough data in fifo\n");
633 return -EAGAIN;
634 }
635
636 qcom_glink_rx_peak(glink, &hdr, sizeof(hdr));
637 chunk_size = le32_to_cpu(hdr.chunk_size);
638 left_size = le32_to_cpu(hdr.left_size);
639
640 if (avail < sizeof(hdr) + chunk_size) {
641 dev_dbg(glink->dev, "Payload not yet in fifo\n");
642 return -EAGAIN;
643 }
644
645 if (WARN(chunk_size % 4, "Incoming data must be word aligned\n"))
646 return -EINVAL;
647
648 rcid = le16_to_cpu(hdr.msg.param1);
Sricharan R44f6df92017-08-24 12:51:33 +0530649 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530650 channel = idr_find(&glink->rcids, rcid);
Sricharan R44f6df92017-08-24 12:51:33 +0530651 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530652 if (!channel) {
653 dev_dbg(glink->dev, "Data on non-existing channel\n");
654
655 /* Drop the message */
Sricharan R64f95f82017-08-24 12:51:35 +0530656 goto advance_rx;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530657 }
658
Sricharan R64f95f82017-08-24 12:51:35 +0530659 if (glink->intentless) {
660 /* Might have an ongoing, fragmented, message to append */
661 if (!channel->buf) {
662 intent = kzalloc(sizeof(*intent), GFP_ATOMIC);
663 if (!intent)
664 return -ENOMEM;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530665
Sricharan R64f95f82017-08-24 12:51:35 +0530666 intent->data = kmalloc(chunk_size + left_size,
667 GFP_ATOMIC);
668 if (!intent->data) {
669 kfree(intent);
670 return -ENOMEM;
671 }
672
673 intent->id = 0xbabababa;
674 intent->size = chunk_size + left_size;
675 intent->offset = 0;
676
677 channel->buf = intent;
678 } else {
679 intent = channel->buf;
680 }
681 } else {
682 liid = le32_to_cpu(hdr.msg.param2);
683
684 spin_lock_irqsave(&channel->intent_lock, flags);
685 intent = idr_find(&channel->liids, liid);
686 spin_unlock_irqrestore(&channel->intent_lock, flags);
687
688 if (!intent) {
689 dev_err(glink->dev,
690 "no intent found for channel %s intent %d",
691 channel->name, liid);
692 goto advance_rx;
693 }
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530694 }
695
Sricharan R64f95f82017-08-24 12:51:35 +0530696 if (intent->size - intent->offset < chunk_size) {
697 dev_err(glink->dev, "Insufficient space in intent\n");
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530698
699 /* The packet header lied, drop payload */
Sricharan R64f95f82017-08-24 12:51:35 +0530700 goto advance_rx;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530701 }
702
Sricharan R64f95f82017-08-24 12:51:35 +0530703 qcom_glink_rx_advance(glink, ALIGN(sizeof(hdr), 8));
704 qcom_glink_rx_peak(glink, intent->data + intent->offset,
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530705 chunk_size);
Sricharan R64f95f82017-08-24 12:51:35 +0530706 intent->offset += chunk_size;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530707
708 /* Handle message when no fragments remain to be received */
709 if (!left_size) {
710 spin_lock(&channel->recv_lock);
711 if (channel->ept.cb) {
712 channel->ept.cb(channel->ept.rpdev,
Sricharan R64f95f82017-08-24 12:51:35 +0530713 intent->data,
714 intent->offset,
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530715 channel->ept.priv,
716 RPMSG_ADDR_ANY);
717 }
718 spin_unlock(&channel->recv_lock);
719
Sricharan R64f95f82017-08-24 12:51:35 +0530720 intent->offset = 0;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530721 channel->buf = NULL;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530722 }
723
Sricharan R64f95f82017-08-24 12:51:35 +0530724advance_rx:
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530725 qcom_glink_rx_advance(glink, ALIGN(chunk_size, 8));
726
Sricharan R64f95f82017-08-24 12:51:35 +0530727 return ret;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530728}
729
730static int qcom_glink_rx_open_ack(struct qcom_glink *glink, unsigned int lcid)
731{
732 struct glink_channel *channel;
733
Sricharan R44f6df92017-08-24 12:51:33 +0530734 spin_lock(&glink->idr_lock);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530735 channel = idr_find(&glink->lcids, lcid);
736 if (!channel) {
737 dev_err(glink->dev, "Invalid open ack packet\n");
738 return -EINVAL;
739 }
Sricharan R44f6df92017-08-24 12:51:33 +0530740 spin_unlock(&glink->idr_lock);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530741
742 complete(&channel->open_ack);
743
744 return 0;
745}
746
747static irqreturn_t qcom_glink_native_intr(int irq, void *data)
748{
749 struct qcom_glink *glink = data;
750 struct glink_msg msg;
751 unsigned int param1;
752 unsigned int param2;
753 unsigned int avail;
754 unsigned int cmd;
755 int ret;
756
757 for (;;) {
758 avail = qcom_glink_rx_avail(glink);
759 if (avail < sizeof(msg))
760 break;
761
762 qcom_glink_rx_peak(glink, &msg, sizeof(msg));
763
764 cmd = le16_to_cpu(msg.cmd);
765 param1 = le16_to_cpu(msg.param1);
766 param2 = le32_to_cpu(msg.param2);
767
768 switch (cmd) {
769 case RPM_CMD_VERSION:
770 case RPM_CMD_VERSION_ACK:
771 case RPM_CMD_CLOSE:
772 case RPM_CMD_CLOSE_ACK:
Sricharan R933b45d2017-08-24 12:51:34 +0530773 case RPM_CMD_RX_INTENT_REQ:
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530774 ret = qcom_glink_rx_defer(glink, 0);
775 break;
776 case RPM_CMD_OPEN_ACK:
777 ret = qcom_glink_rx_open_ack(glink, param1);
778 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
779 break;
780 case RPM_CMD_OPEN:
781 ret = qcom_glink_rx_defer(glink, param2);
782 break;
783 case RPM_CMD_TX_DATA:
784 case RPM_CMD_TX_DATA_CONT:
785 ret = qcom_glink_rx_data(glink, avail);
786 break;
787 case RPM_CMD_READ_NOTIF:
788 qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
789
790 mbox_send_message(glink->mbox_chan, NULL);
791 mbox_client_txdone(glink->mbox_chan, 0);
792
793 ret = 0;
794 break;
795 default:
796 dev_err(glink->dev, "unhandled rx cmd: %d\n", cmd);
797 ret = -EINVAL;
798 break;
799 }
800
801 if (ret)
802 break;
803 }
804
805 return IRQ_HANDLED;
806}
807
808/* Locally initiated rpmsg_create_ept */
809static struct glink_channel *qcom_glink_create_local(struct qcom_glink *glink,
810 const char *name)
811{
812 struct glink_channel *channel;
813 int ret;
Sricharan R44f6df92017-08-24 12:51:33 +0530814 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530815
816 channel = qcom_glink_alloc_channel(glink, name);
817 if (IS_ERR(channel))
818 return ERR_CAST(channel);
819
820 ret = qcom_glink_send_open_req(glink, channel);
821 if (ret)
822 goto release_channel;
823
824 ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
825 if (!ret)
826 goto err_timeout;
827
828 ret = wait_for_completion_timeout(&channel->open_req, 5 * HZ);
829 if (!ret)
830 goto err_timeout;
831
832 qcom_glink_send_open_ack(glink, channel);
833
834 return channel;
835
836err_timeout:
837 /* qcom_glink_send_open_req() did register the channel in lcids*/
Sricharan R44f6df92017-08-24 12:51:33 +0530838 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530839 idr_remove(&glink->lcids, channel->lcid);
Sricharan R44f6df92017-08-24 12:51:33 +0530840 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530841
842release_channel:
843 /* Release qcom_glink_send_open_req() reference */
844 kref_put(&channel->refcount, qcom_glink_channel_release);
845 /* Release qcom_glink_alloc_channel() reference */
846 kref_put(&channel->refcount, qcom_glink_channel_release);
847
848 return ERR_PTR(-ETIMEDOUT);
849}
850
851/* Remote initiated rpmsg_create_ept */
852static int qcom_glink_create_remote(struct qcom_glink *glink,
853 struct glink_channel *channel)
854{
855 int ret;
856
857 qcom_glink_send_open_ack(glink, channel);
858
859 ret = qcom_glink_send_open_req(glink, channel);
860 if (ret)
861 goto close_link;
862
863 ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
864 if (!ret) {
865 ret = -ETIMEDOUT;
866 goto close_link;
867 }
868
869 return 0;
870
871close_link:
872 /*
873 * Send a close request to "undo" our open-ack. The close-ack will
874 * release the last reference.
875 */
876 qcom_glink_send_close_req(glink, channel);
877
878 /* Release qcom_glink_send_open_req() reference */
879 kref_put(&channel->refcount, qcom_glink_channel_release);
880
881 return ret;
882}
883
884static struct rpmsg_endpoint *qcom_glink_create_ept(struct rpmsg_device *rpdev,
885 rpmsg_rx_cb_t cb,
886 void *priv,
887 struct rpmsg_channel_info
888 chinfo)
889{
890 struct glink_channel *parent = to_glink_channel(rpdev->ept);
891 struct glink_channel *channel;
892 struct qcom_glink *glink = parent->glink;
893 struct rpmsg_endpoint *ept;
894 const char *name = chinfo.name;
895 int cid;
896 int ret;
Sricharan R44f6df92017-08-24 12:51:33 +0530897 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530898
Sricharan R44f6df92017-08-24 12:51:33 +0530899 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530900 idr_for_each_entry(&glink->rcids, channel, cid) {
901 if (!strcmp(channel->name, name))
902 break;
903 }
Sricharan R44f6df92017-08-24 12:51:33 +0530904 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530905
906 if (!channel) {
907 channel = qcom_glink_create_local(glink, name);
908 if (IS_ERR(channel))
909 return NULL;
910 } else {
911 ret = qcom_glink_create_remote(glink, channel);
912 if (ret)
913 return NULL;
914 }
915
916 ept = &channel->ept;
917 ept->rpdev = rpdev;
918 ept->cb = cb;
919 ept->priv = priv;
920 ept->ops = &glink_endpoint_ops;
921
922 return ept;
923}
924
925static void qcom_glink_destroy_ept(struct rpmsg_endpoint *ept)
926{
927 struct glink_channel *channel = to_glink_channel(ept);
928 struct qcom_glink *glink = channel->glink;
929 unsigned long flags;
930
931 spin_lock_irqsave(&channel->recv_lock, flags);
932 channel->ept.cb = NULL;
933 spin_unlock_irqrestore(&channel->recv_lock, flags);
934
935 /* Decouple the potential rpdev from the channel */
936 channel->rpdev = NULL;
937
938 qcom_glink_send_close_req(glink, channel);
939}
940
941static int __qcom_glink_send(struct glink_channel *channel,
942 void *data, int len, bool wait)
943{
944 struct qcom_glink *glink = channel->glink;
945 struct {
946 struct glink_msg msg;
947 __le32 chunk_size;
948 __le32 left_size;
949 } __packed req;
950
Bjorn Andersson835764dd2017-08-24 12:51:26 +0530951 req.msg.cmd = cpu_to_le16(RPM_CMD_TX_DATA);
952 req.msg.param1 = cpu_to_le16(channel->lcid);
953 req.msg.param2 = cpu_to_le32(channel->rcid);
954 req.chunk_size = cpu_to_le32(len);
955 req.left_size = cpu_to_le32(0);
956
957 return qcom_glink_tx(glink, &req, sizeof(req), data, len, wait);
958}
959
960static int qcom_glink_send(struct rpmsg_endpoint *ept, void *data, int len)
961{
962 struct glink_channel *channel = to_glink_channel(ept);
963
964 return __qcom_glink_send(channel, data, len, true);
965}
966
967static int qcom_glink_trysend(struct rpmsg_endpoint *ept, void *data, int len)
968{
969 struct glink_channel *channel = to_glink_channel(ept);
970
971 return __qcom_glink_send(channel, data, len, false);
972}
973
974/*
975 * Finds the device_node for the glink child interested in this channel.
976 */
977static struct device_node *qcom_glink_match_channel(struct device_node *node,
978 const char *channel)
979{
980 struct device_node *child;
981 const char *name;
982 const char *key;
983 int ret;
984
985 for_each_available_child_of_node(node, child) {
986 key = "qcom,glink-channels";
987 ret = of_property_read_string(child, key, &name);
988 if (ret)
989 continue;
990
991 if (strcmp(name, channel) == 0)
992 return child;
993 }
994
995 return NULL;
996}
997
998static const struct rpmsg_device_ops glink_device_ops = {
999 .create_ept = qcom_glink_create_ept,
1000};
1001
1002static const struct rpmsg_endpoint_ops glink_endpoint_ops = {
1003 .destroy_ept = qcom_glink_destroy_ept,
1004 .send = qcom_glink_send,
1005 .trysend = qcom_glink_trysend,
1006};
1007
1008static void qcom_glink_rpdev_release(struct device *dev)
1009{
1010 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
1011 struct glink_channel *channel = to_glink_channel(rpdev->ept);
1012
1013 channel->rpdev = NULL;
1014 kfree(rpdev);
1015}
1016
1017static int qcom_glink_rx_open(struct qcom_glink *glink, unsigned int rcid,
1018 char *name)
1019{
1020 struct glink_channel *channel;
1021 struct rpmsg_device *rpdev;
1022 bool create_device = false;
1023 struct device_node *node;
1024 int lcid;
1025 int ret;
Sricharan R44f6df92017-08-24 12:51:33 +05301026 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301027
Sricharan R44f6df92017-08-24 12:51:33 +05301028 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301029 idr_for_each_entry(&glink->lcids, channel, lcid) {
1030 if (!strcmp(channel->name, name))
1031 break;
1032 }
Sricharan R44f6df92017-08-24 12:51:33 +05301033 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301034
1035 if (!channel) {
1036 channel = qcom_glink_alloc_channel(glink, name);
1037 if (IS_ERR(channel))
1038 return PTR_ERR(channel);
1039
1040 /* The opening dance was initiated by the remote */
1041 create_device = true;
1042 }
1043
Sricharan R44f6df92017-08-24 12:51:33 +05301044 spin_lock_irqsave(&glink->idr_lock, flags);
1045 ret = idr_alloc(&glink->rcids, channel, rcid, rcid + 1, GFP_ATOMIC);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301046 if (ret < 0) {
1047 dev_err(glink->dev, "Unable to insert channel into rcid list\n");
Sricharan R44f6df92017-08-24 12:51:33 +05301048 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301049 goto free_channel;
1050 }
1051 channel->rcid = ret;
Sricharan R44f6df92017-08-24 12:51:33 +05301052 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301053
1054 complete(&channel->open_req);
1055
1056 if (create_device) {
1057 rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL);
1058 if (!rpdev) {
1059 ret = -ENOMEM;
1060 goto rcid_remove;
1061 }
1062
1063 rpdev->ept = &channel->ept;
1064 strncpy(rpdev->id.name, name, RPMSG_NAME_SIZE);
1065 rpdev->src = RPMSG_ADDR_ANY;
1066 rpdev->dst = RPMSG_ADDR_ANY;
1067 rpdev->ops = &glink_device_ops;
1068
1069 node = qcom_glink_match_channel(glink->dev->of_node, name);
1070 rpdev->dev.of_node = node;
1071 rpdev->dev.parent = glink->dev;
1072 rpdev->dev.release = qcom_glink_rpdev_release;
1073
1074 ret = rpmsg_register_device(rpdev);
1075 if (ret)
1076 goto free_rpdev;
1077
1078 channel->rpdev = rpdev;
1079 }
1080
1081 return 0;
1082
1083free_rpdev:
1084 kfree(rpdev);
1085rcid_remove:
Sricharan R44f6df92017-08-24 12:51:33 +05301086 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301087 idr_remove(&glink->rcids, channel->rcid);
1088 channel->rcid = 0;
Sricharan R44f6df92017-08-24 12:51:33 +05301089 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301090free_channel:
1091 /* Release the reference, iff we took it */
1092 if (create_device)
1093 kref_put(&channel->refcount, qcom_glink_channel_release);
1094
1095 return ret;
1096}
1097
1098static void qcom_glink_rx_close(struct qcom_glink *glink, unsigned int rcid)
1099{
1100 struct rpmsg_channel_info chinfo;
1101 struct glink_channel *channel;
Sricharan R44f6df92017-08-24 12:51:33 +05301102 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301103
Sricharan R44f6df92017-08-24 12:51:33 +05301104 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301105 channel = idr_find(&glink->rcids, rcid);
Sricharan R44f6df92017-08-24 12:51:33 +05301106 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301107 if (WARN(!channel, "close request on unknown channel\n"))
1108 return;
1109
1110 if (channel->rpdev) {
1111 strncpy(chinfo.name, channel->name, sizeof(chinfo.name));
1112 chinfo.src = RPMSG_ADDR_ANY;
1113 chinfo.dst = RPMSG_ADDR_ANY;
1114
1115 rpmsg_unregister_device(glink->dev, &chinfo);
1116 }
1117
1118 qcom_glink_send_close_ack(glink, channel->rcid);
1119
Sricharan R44f6df92017-08-24 12:51:33 +05301120 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301121 idr_remove(&glink->rcids, channel->rcid);
1122 channel->rcid = 0;
Sricharan R44f6df92017-08-24 12:51:33 +05301123 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301124
1125 kref_put(&channel->refcount, qcom_glink_channel_release);
1126}
1127
1128static void qcom_glink_rx_close_ack(struct qcom_glink *glink, unsigned int lcid)
1129{
1130 struct glink_channel *channel;
Sricharan R44f6df92017-08-24 12:51:33 +05301131 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301132
Sricharan R44f6df92017-08-24 12:51:33 +05301133 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301134 channel = idr_find(&glink->lcids, lcid);
Sricharan R44f6df92017-08-24 12:51:33 +05301135 if (WARN(!channel, "close ack on unknown channel\n")) {
1136 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301137 return;
Sricharan R44f6df92017-08-24 12:51:33 +05301138 }
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301139
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301140 idr_remove(&glink->lcids, channel->lcid);
1141 channel->lcid = 0;
Sricharan R44f6df92017-08-24 12:51:33 +05301142 spin_unlock_irqrestore(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301143
1144 kref_put(&channel->refcount, qcom_glink_channel_release);
1145}
1146
1147static void qcom_glink_work(struct work_struct *work)
1148{
1149 struct qcom_glink *glink = container_of(work, struct qcom_glink,
1150 rx_work);
1151 struct glink_defer_cmd *dcmd;
1152 struct glink_msg *msg;
1153 unsigned long flags;
1154 unsigned int param1;
1155 unsigned int param2;
1156 unsigned int cmd;
1157
1158 for (;;) {
1159 spin_lock_irqsave(&glink->rx_lock, flags);
1160 if (list_empty(&glink->rx_queue)) {
1161 spin_unlock_irqrestore(&glink->rx_lock, flags);
1162 break;
1163 }
1164 dcmd = list_first_entry(&glink->rx_queue,
1165 struct glink_defer_cmd, node);
1166 list_del(&dcmd->node);
1167 spin_unlock_irqrestore(&glink->rx_lock, flags);
1168
1169 msg = &dcmd->msg;
1170 cmd = le16_to_cpu(msg->cmd);
1171 param1 = le16_to_cpu(msg->param1);
1172 param2 = le32_to_cpu(msg->param2);
1173
1174 switch (cmd) {
1175 case RPM_CMD_VERSION:
Sricharan Rd31ad612017-08-24 12:51:32 +05301176 qcom_glink_receive_version(glink, param1, param2);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301177 break;
1178 case RPM_CMD_VERSION_ACK:
Sricharan Rd31ad612017-08-24 12:51:32 +05301179 qcom_glink_receive_version_ack(glink, param1, param2);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301180 break;
1181 case RPM_CMD_OPEN:
1182 qcom_glink_rx_open(glink, param1, msg->data);
1183 break;
1184 case RPM_CMD_CLOSE:
1185 qcom_glink_rx_close(glink, param1);
1186 break;
1187 case RPM_CMD_CLOSE_ACK:
1188 qcom_glink_rx_close_ack(glink, param1);
1189 break;
Sricharan R933b45d2017-08-24 12:51:34 +05301190 case RPM_CMD_RX_INTENT_REQ:
1191 qcom_glink_handle_intent_req(glink, param1, param2);
1192 break;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301193 default:
1194 WARN(1, "Unknown defer object %d\n", cmd);
1195 break;
1196 }
1197
1198 kfree(dcmd);
1199 }
1200}
1201
1202struct qcom_glink *qcom_glink_native_probe(struct device *dev,
Sricharan Rd31ad612017-08-24 12:51:32 +05301203 unsigned long features,
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301204 struct qcom_glink_pipe *rx,
Sricharan R933b45d2017-08-24 12:51:34 +05301205 struct qcom_glink_pipe *tx,
1206 bool intentless)
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301207{
1208 int irq;
1209 int ret;
1210 struct qcom_glink *glink;
1211
1212 glink = devm_kzalloc(dev, sizeof(*glink), GFP_KERNEL);
1213 if (!glink)
1214 return ERR_PTR(-ENOMEM);
1215
1216 glink->dev = dev;
1217 glink->tx_pipe = tx;
1218 glink->rx_pipe = rx;
1219
Sricharan Rd31ad612017-08-24 12:51:32 +05301220 glink->features = features;
Sricharan R933b45d2017-08-24 12:51:34 +05301221 glink->intentless = intentless;
Sricharan Rd31ad612017-08-24 12:51:32 +05301222
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301223 mutex_init(&glink->tx_lock);
1224 spin_lock_init(&glink->rx_lock);
1225 INIT_LIST_HEAD(&glink->rx_queue);
1226 INIT_WORK(&glink->rx_work, qcom_glink_work);
1227
Sricharan R44f6df92017-08-24 12:51:33 +05301228 spin_lock_init(&glink->idr_lock);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301229 idr_init(&glink->lcids);
1230 idr_init(&glink->rcids);
1231
1232 glink->mbox_client.dev = dev;
1233 glink->mbox_chan = mbox_request_channel(&glink->mbox_client, 0);
1234 if (IS_ERR(glink->mbox_chan)) {
1235 if (PTR_ERR(glink->mbox_chan) != -EPROBE_DEFER)
1236 dev_err(dev, "failed to acquire IPC channel\n");
1237 return ERR_CAST(glink->mbox_chan);
1238 }
1239
1240 irq = of_irq_get(dev->of_node, 0);
1241 ret = devm_request_irq(dev, irq,
1242 qcom_glink_native_intr,
1243 IRQF_NO_SUSPEND | IRQF_SHARED,
1244 "glink-native", glink);
1245 if (ret) {
1246 dev_err(dev, "failed to request IRQ\n");
1247 return ERR_PTR(ret);
1248 }
1249
1250 glink->irq = irq;
1251
1252 ret = qcom_glink_send_version(glink);
1253 if (ret)
1254 return ERR_PTR(ret);
1255
1256 return glink;
1257}
1258
1259static int qcom_glink_remove_device(struct device *dev, void *data)
1260{
1261 device_unregister(dev);
1262
1263 return 0;
1264}
1265
1266void qcom_glink_native_remove(struct qcom_glink *glink)
1267{
1268 struct glink_channel *channel;
1269 int cid;
1270 int ret;
Sricharan R44f6df92017-08-24 12:51:33 +05301271 unsigned long flags;
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301272
1273 disable_irq(glink->irq);
1274 cancel_work_sync(&glink->rx_work);
1275
1276 ret = device_for_each_child(glink->dev, NULL, qcom_glink_remove_device);
1277 if (ret)
1278 dev_warn(glink->dev, "Can't remove GLINK devices: %d\n", ret);
1279
Sricharan R44f6df92017-08-24 12:51:33 +05301280 spin_lock_irqsave(&glink->idr_lock, flags);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301281 /* Release any defunct local channels, waiting for close-ack */
1282 idr_for_each_entry(&glink->lcids, channel, cid)
1283 kref_put(&channel->refcount, qcom_glink_channel_release);
1284
1285 idr_destroy(&glink->lcids);
1286 idr_destroy(&glink->rcids);
Sricharan R44f6df92017-08-24 12:51:33 +05301287 spin_unlock_irqrestore(&glink->idr_lock, flags);
Sricharan R76cf1102017-08-24 12:51:29 +05301288 mbox_free_channel(glink->mbox_chan);
Bjorn Andersson835764dd2017-08-24 12:51:26 +05301289}
Bjorn Anderssoncaf989c2017-08-24 12:51:30 +05301290
1291void qcom_glink_native_unregister(struct qcom_glink *glink)
1292{
1293 device_unregister(glink->dev);
1294}