Merge tag 'f2fs-for-5.13-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/jaegeu...
[linux-2.6-microblaze.git] / drivers / rpmsg / qcom_glink_native.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2016-2017, Linaro Ltd
4  */
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>
18 #include <linux/sizes.h>
19 #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
27 #define GLINK_VERSION_1         1
28
29 #define RPM_GLINK_CID_MIN       1
30 #define RPM_GLINK_CID_MAX       65536
31
32 struct glink_msg {
33         __le16 cmd;
34         __le16 param1;
35         __le32 param2;
36         u8 data[];
37 } __packed;
38
39 /**
40  * struct glink_defer_cmd - deferred incoming control message
41  * @node:       list node
42  * @msg:        message header
43  * @data:       payload of the message
44  *
45  * Copy of a received control message, to be added to @rx_queue and processed
46  * by @rx_work of @qcom_glink.
47  */
48 struct glink_defer_cmd {
49         struct list_head node;
50
51         struct glink_msg msg;
52         u8 data[];
53 };
54
55 /**
56  * struct glink_core_rx_intent - RX intent
57  * RX intent
58  *
59  * @data: pointer to the data (may be NULL for zero-copy)
60  * @id: remote or local intent ID
61  * @size: size of the original intent (do not modify)
62  * @reuse: To mark if the intent can be reused after first use
63  * @in_use: To mark if intent is already in use for the channel
64  * @offset: next write offset (initially 0)
65  * @node:       list node
66  */
67 struct glink_core_rx_intent {
68         void *data;
69         u32 id;
70         size_t size;
71         bool reuse;
72         bool in_use;
73         u32 offset;
74
75         struct list_head node;
76 };
77
78 /**
79  * 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
93  * @features:   remote features
94  * @intentless: flag to indicate that there is no intent
95  */
96 struct qcom_glink {
97         struct device *dev;
98
99         const char *name;
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         spinlock_t tx_lock;
114
115         spinlock_t idr_lock;
116         struct idr lcids;
117         struct idr rcids;
118         unsigned long features;
119
120         bool intentless;
121 };
122
123 enum {
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
140  * @intent_lock: lock for protection of @liids, @riids
141  * @liids:      idr of all local intents
142  * @riids:      idr of all remote intents
143  * @intent_work: worker responsible for transmitting rx_done packets
144  * @done_intents: list of intents that needs to be announced rx_done
145  * @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
150  * @intent_req_lock: Synchronises multiple intent requests
151  * @intent_req_result: Result of intent request
152  * @intent_req_comp: Completion for intent_req signalling
153  */
154 struct 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
168         spinlock_t intent_lock;
169         struct idr liids;
170         struct idr riids;
171         struct work_struct intent_work;
172         struct list_head done_intents;
173
174         struct glink_core_rx_intent *buf;
175         int buf_offset;
176         int buf_size;
177
178         struct completion open_ack;
179         struct completion open_req;
180
181         struct mutex intent_req_lock;
182         bool intent_req_result;
183         struct completion intent_req_comp;
184 };
185
186 #define to_glink_channel(_ept) container_of(_ept, struct glink_channel, ept)
187
188 static 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
195 #define RPM_CMD_INTENT                  5
196 #define RPM_CMD_RX_DONE                 6
197 #define RPM_CMD_RX_INTENT_REQ           7
198 #define RPM_CMD_RX_INTENT_REQ_ACK       8
199 #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
203 #define RPM_CMD_RX_DONE_W_REUSE         14
204
205 #define GLINK_FEATURE_INTENTLESS        BIT(1)
206
207 static void qcom_glink_rx_done_work(struct work_struct *work);
208
209 static 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);
220         spin_lock_init(&channel->intent_lock);
221         mutex_init(&channel->intent_req_lock);
222
223         channel->glink = glink;
224         channel->name = kstrdup(name, GFP_KERNEL);
225
226         init_completion(&channel->open_req);
227         init_completion(&channel->open_ack);
228         init_completion(&channel->intent_req_comp);
229
230         INIT_LIST_HEAD(&channel->done_intents);
231         INIT_WORK(&channel->intent_work, qcom_glink_rx_done_work);
232
233         idr_init(&channel->liids);
234         idr_init(&channel->riids);
235         kref_init(&channel->refcount);
236
237         return channel;
238 }
239
240 static void qcom_glink_channel_release(struct kref *ref)
241 {
242         struct glink_channel *channel = container_of(ref, struct glink_channel,
243                                                      refcount);
244         struct glink_core_rx_intent *intent;
245         struct glink_core_rx_intent *tmp;
246         unsigned long flags;
247         int iid;
248
249         /* cancel pending rx_done work */
250         cancel_work_sync(&channel->intent_work);
251
252         spin_lock_irqsave(&channel->intent_lock, flags);
253         /* 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
261         idr_for_each_entry(&channel->liids, tmp, iid) {
262                 kfree(tmp->data);
263                 kfree(tmp);
264         }
265         idr_destroy(&channel->liids);
266
267         idr_for_each_entry(&channel->riids, tmp, iid)
268                 kfree(tmp);
269         idr_destroy(&channel->riids);
270         spin_unlock_irqrestore(&channel->intent_lock, flags);
271
272         kfree(channel->name);
273         kfree(channel);
274 }
275
276 static size_t qcom_glink_rx_avail(struct qcom_glink *glink)
277 {
278         return glink->rx_pipe->avail(glink->rx_pipe);
279 }
280
281 static void qcom_glink_rx_peak(struct qcom_glink *glink,
282                                void *data, unsigned int offset, size_t count)
283 {
284         glink->rx_pipe->peak(glink->rx_pipe, data, offset, count);
285 }
286
287 static void qcom_glink_rx_advance(struct qcom_glink *glink, size_t count)
288 {
289         glink->rx_pipe->advance(glink->rx_pipe, count);
290 }
291
292 static size_t qcom_glink_tx_avail(struct qcom_glink *glink)
293 {
294         return glink->tx_pipe->avail(glink->tx_pipe);
295 }
296
297 static 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
304 static 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;
309         unsigned long flags;
310         int ret = 0;
311
312         /* Reject packets that are too big */
313         if (tlen >= glink->tx_pipe->length)
314                 return -EINVAL;
315
316         spin_lock_irqsave(&glink->tx_lock, flags);
317
318         while (qcom_glink_tx_avail(glink) < tlen) {
319                 if (!wait) {
320                         ret = -EAGAIN;
321                         goto out;
322                 }
323
324                 /* Wait without holding the tx_lock */
325                 spin_unlock_irqrestore(&glink->tx_lock, flags);
326
327                 usleep_range(10000, 15000);
328
329                 spin_lock_irqsave(&glink->tx_lock, flags);
330         }
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
337 out:
338         spin_unlock_irqrestore(&glink->tx_lock, flags);
339
340         return ret;
341 }
342
343 static 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);
348         msg.param1 = cpu_to_le16(GLINK_VERSION_1);
349         msg.param2 = cpu_to_le32(glink->features);
350
351         return qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
352 }
353
354 static 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);
359         msg.param1 = cpu_to_le16(GLINK_VERSION_1);
360         msg.param2 = cpu_to_le32(glink->features);
361
362         qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
363 }
364
365 static 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
377 static 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
395 /**
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  */
405 static 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;
415         unsigned long flags;
416
417         kref_get(&channel->refcount);
418
419         spin_lock_irqsave(&glink->idr_lock, flags);
420         ret = idr_alloc_cyclic(&glink->lcids, channel,
421                                RPM_GLINK_CID_MIN, RPM_GLINK_CID_MAX,
422                                GFP_ATOMIC);
423         spin_unlock_irqrestore(&glink->idr_lock, flags);
424         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
440 remove_idr:
441         spin_lock_irqsave(&glink->idr_lock, flags);
442         idr_remove(&glink->lcids, channel->lcid);
443         channel->lcid = 0;
444         spin_unlock_irqrestore(&glink->idr_lock, flags);
445
446         return ret;
447 }
448
449 static 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
461 static 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
473 static 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
511 static 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
537 /**
538  * qcom_glink_receive_version() - receive version/features from remote system
539  *
540  * @glink:      pointer to transport interface
541  * @version:    remote version
542  * @features:   remote features
543  *
544  * This function is called in response to a remote-initiated version/feature
545  * negotiation sequence.
546  */
547 static 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
567  * @version:    remote version response
568  * @features:   remote features response
569  *
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  */
574 static 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
594 /**
595  * qcom_glink_send_intent_req_ack() - convert an rx intent request ack cmd to
596  *      wire format and transmit
597  * @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  */
603 static 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
623  * @intent:     The intent to pass on to remote.
624  *
625  * Return: 0 on success or standard Linux error code.
626  */
627 static 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 {
632                 __le16 id;
633                 __le16 lcid;
634                 __le32 count;
635                 __le32 size;
636                 __le32 liid;
637         } __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
651 static struct glink_core_rx_intent *
652 qcom_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);
662         if (!intent)
663                 return NULL;
664
665         intent->data = kzalloc(size, GFP_KERNEL);
666         if (!intent->data)
667                 goto free_intent;
668
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);
673                 goto free_data;
674         }
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;
682
683 free_data:
684         kfree(intent->data);
685 free_intent:
686         kfree(intent);
687         return NULL;
688 }
689
690 static 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
724 /**
725  * qcom_glink_handle_intent_req() - Receive a request for rx_intent
726  *                                          from remote side
727  * @glink:      Pointer to the transport interface
728  * @cid:        Remote channel ID
729  * @size:       size of the intent
730  *
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  */
734 static 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
757 static 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
774         qcom_glink_rx_peak(glink, &dcmd->msg, 0, sizeof(dcmd->msg) + extra);
775
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
786 static int qcom_glink_rx_data(struct qcom_glink *glink, size_t avail)
787 {
788         struct glink_core_rx_intent *intent;
789         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;
798         unsigned int liid;
799         int ret = 0;
800         unsigned long flags;
801
802         if (avail < sizeof(hdr)) {
803                 dev_dbg(glink->dev, "Not enough data in fifo\n");
804                 return -EAGAIN;
805         }
806
807         qcom_glink_rx_peak(glink, &hdr, 0, sizeof(hdr));
808         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
816         rcid = le16_to_cpu(hdr.msg.param1);
817         spin_lock_irqsave(&glink->idr_lock, flags);
818         channel = idr_find(&glink->rcids, rcid);
819         spin_unlock_irqrestore(&glink->idr_lock, flags);
820         if (!channel) {
821                 dev_dbg(glink->dev, "Data on non-existing channel\n");
822
823                 /* Drop the message */
824                 goto advance_rx;
825         }
826
827         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;
833
834                         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                         ret = -ENOENT;
861                         goto advance_rx;
862                 }
863         }
864
865         if (intent->size - intent->offset < chunk_size) {
866                 dev_err(glink->dev, "Insufficient space in intent\n");
867
868                 /* The packet header lied, drop payload */
869                 goto advance_rx;
870         }
871
872         qcom_glink_rx_peak(glink, intent->data + intent->offset,
873                            sizeof(hdr), chunk_size);
874         intent->offset += chunk_size;
875
876         /* Handle message when no fragments remain to be received */
877         if (!left_size) {
878                 spin_lock(&channel->recv_lock);
879                 if (channel->ept.cb) {
880                         channel->ept.cb(channel->ept.rpdev,
881                                         intent->data,
882                                         intent->offset,
883                                         channel->ept.priv,
884                                         RPMSG_ADDR_ANY);
885                 }
886                 spin_unlock(&channel->recv_lock);
887
888                 intent->offset = 0;
889                 channel->buf = NULL;
890
891                 qcom_glink_rx_done(glink, channel, intent);
892         }
893
894 advance_rx:
895         qcom_glink_rx_advance(glink, ALIGN(sizeof(hdr) + chunk_size, 8));
896
897         return ret;
898 }
899
900 static void qcom_glink_handle_intent(struct qcom_glink *glink,
901                                      unsigned int cid,
902                                      unsigned int count,
903                                      size_t avail)
904 {
905         struct glink_core_rx_intent *intent;
906         struct glink_channel *channel;
907         struct intent_pair {
908                 __le32 size;
909                 __le32 iid;
910         };
911
912         struct {
913                 struct glink_msg msg;
914                 struct intent_pair intents[];
915         } __packed * msg;
916
917         const size_t msglen = struct_size(msg, intents, count);
918         int ret;
919         int i;
920         unsigned long flags;
921
922         if (avail < msglen) {
923                 dev_dbg(glink->dev, "Not enough data in fifo\n");
924                 return;
925         }
926
927         spin_lock_irqsave(&glink->idr_lock, flags);
928         channel = idr_find(&glink->rcids, cid);
929         spin_unlock_irqrestore(&glink->idr_lock, flags);
930         if (!channel) {
931                 dev_err(glink->dev, "intents for non-existing channel\n");
932                 return;
933         }
934
935         msg = kmalloc(msglen, GFP_ATOMIC);
936         if (!msg)
937                 return;
938
939         qcom_glink_rx_peak(glink, msg, 0, msglen);
940
941         for (i = 0; i < count; ++i) {
942                 intent = kzalloc(sizeof(*intent), GFP_ATOMIC);
943                 if (!intent)
944                         break;
945
946                 intent->id = le32_to_cpu(msg->intents[i].iid);
947                 intent->size = le32_to_cpu(msg->intents[i].size);
948
949                 spin_lock_irqsave(&channel->intent_lock, flags);
950                 ret = idr_alloc(&channel->riids, intent,
951                                 intent->id, intent->id + 1, GFP_ATOMIC);
952                 spin_unlock_irqrestore(&channel->intent_lock, flags);
953
954                 if (ret < 0)
955                         dev_err(glink->dev, "failed to store remote intent\n");
956         }
957
958         kfree(msg);
959         qcom_glink_rx_advance(glink, ALIGN(msglen, 8));
960 }
961
962 static int qcom_glink_rx_open_ack(struct qcom_glink *glink, unsigned int lcid)
963 {
964         struct glink_channel *channel;
965
966         spin_lock(&glink->idr_lock);
967         channel = idr_find(&glink->lcids, lcid);
968         spin_unlock(&glink->idr_lock);
969         if (!channel) {
970                 dev_err(glink->dev, "Invalid open ack packet\n");
971                 return -EINVAL;
972         }
973
974         complete_all(&channel->open_ack);
975
976         return 0;
977 }
978
979 static irqreturn_t qcom_glink_native_intr(int irq, void *data)
980 {
981         struct qcom_glink *glink = data;
982         struct glink_msg msg;
983         unsigned int param1;
984         unsigned int param2;
985         unsigned int avail;
986         unsigned int cmd;
987         int ret = 0;
988
989         for (;;) {
990                 avail = qcom_glink_rx_avail(glink);
991                 if (avail < sizeof(msg))
992                         break;
993
994                 qcom_glink_rx_peak(glink, &msg, 0, sizeof(msg));
995
996                 cmd = le16_to_cpu(msg.cmd);
997                 param1 = le16_to_cpu(msg.param1);
998                 param2 = le32_to_cpu(msg.param2);
999
1000                 switch (cmd) {
1001                 case RPM_CMD_VERSION:
1002                 case RPM_CMD_VERSION_ACK:
1003                 case RPM_CMD_CLOSE:
1004                 case RPM_CMD_CLOSE_ACK:
1005                 case RPM_CMD_RX_INTENT_REQ:
1006                         ret = qcom_glink_rx_defer(glink, 0);
1007                         break;
1008                 case RPM_CMD_OPEN_ACK:
1009                         ret = qcom_glink_rx_open_ack(glink, param1);
1010                         qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1011                         break;
1012                 case RPM_CMD_OPEN:
1013                         ret = qcom_glink_rx_defer(glink, param2);
1014                         break;
1015                 case RPM_CMD_TX_DATA:
1016                 case RPM_CMD_TX_DATA_CONT:
1017                         ret = qcom_glink_rx_data(glink, avail);
1018                         break;
1019                 case RPM_CMD_READ_NOTIF:
1020                         qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1021
1022                         mbox_send_message(glink->mbox_chan, NULL);
1023                         mbox_client_txdone(glink->mbox_chan, 0);
1024                         break;
1025                 case RPM_CMD_INTENT:
1026                         qcom_glink_handle_intent(glink, param1, param2, avail);
1027                         break;
1028                 case RPM_CMD_RX_DONE:
1029                         qcom_glink_handle_rx_done(glink, param1, param2, false);
1030                         qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1031                         break;
1032                 case RPM_CMD_RX_DONE_W_REUSE:
1033                         qcom_glink_handle_rx_done(glink, param1, param2, true);
1034                         qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1035                         break;
1036                 case RPM_CMD_RX_INTENT_REQ_ACK:
1037                         qcom_glink_handle_intent_req_ack(glink, param1, param2);
1038                         qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1039                         break;
1040                 default:
1041                         dev_err(glink->dev, "unhandled rx cmd: %d\n", cmd);
1042                         ret = -EINVAL;
1043                         break;
1044                 }
1045
1046                 if (ret)
1047                         break;
1048         }
1049
1050         return IRQ_HANDLED;
1051 }
1052
1053 /* Locally initiated rpmsg_create_ept */
1054 static struct glink_channel *qcom_glink_create_local(struct qcom_glink *glink,
1055                                                      const char *name)
1056 {
1057         struct glink_channel *channel;
1058         int ret;
1059         unsigned long flags;
1060
1061         channel = qcom_glink_alloc_channel(glink, name);
1062         if (IS_ERR(channel))
1063                 return ERR_CAST(channel);
1064
1065         ret = qcom_glink_send_open_req(glink, channel);
1066         if (ret)
1067                 goto release_channel;
1068
1069         ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
1070         if (!ret)
1071                 goto err_timeout;
1072
1073         ret = wait_for_completion_timeout(&channel->open_req, 5 * HZ);
1074         if (!ret)
1075                 goto err_timeout;
1076
1077         qcom_glink_send_open_ack(glink, channel);
1078
1079         return channel;
1080
1081 err_timeout:
1082         /* qcom_glink_send_open_req() did register the channel in lcids*/
1083         spin_lock_irqsave(&glink->idr_lock, flags);
1084         idr_remove(&glink->lcids, channel->lcid);
1085         spin_unlock_irqrestore(&glink->idr_lock, flags);
1086
1087 release_channel:
1088         /* Release qcom_glink_send_open_req() reference */
1089         kref_put(&channel->refcount, qcom_glink_channel_release);
1090         /* Release qcom_glink_alloc_channel() reference */
1091         kref_put(&channel->refcount, qcom_glink_channel_release);
1092
1093         return ERR_PTR(-ETIMEDOUT);
1094 }
1095
1096 /* Remote initiated rpmsg_create_ept */
1097 static int qcom_glink_create_remote(struct qcom_glink *glink,
1098                                     struct glink_channel *channel)
1099 {
1100         int ret;
1101
1102         qcom_glink_send_open_ack(glink, channel);
1103
1104         ret = qcom_glink_send_open_req(glink, channel);
1105         if (ret)
1106                 goto close_link;
1107
1108         ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
1109         if (!ret) {
1110                 ret = -ETIMEDOUT;
1111                 goto close_link;
1112         }
1113
1114         return 0;
1115
1116 close_link:
1117         /*
1118          * Send a close request to "undo" our open-ack. The close-ack will
1119          * release qcom_glink_send_open_req() reference and the last reference
1120          * will be relesed after receiving remote_close or transport unregister
1121          * by calling qcom_glink_native_remove().
1122          */
1123         qcom_glink_send_close_req(glink, channel);
1124
1125         return ret;
1126 }
1127
1128 static struct rpmsg_endpoint *qcom_glink_create_ept(struct rpmsg_device *rpdev,
1129                                                     rpmsg_rx_cb_t cb,
1130                                                     void *priv,
1131                                                     struct rpmsg_channel_info
1132                                                                         chinfo)
1133 {
1134         struct glink_channel *parent = to_glink_channel(rpdev->ept);
1135         struct glink_channel *channel;
1136         struct qcom_glink *glink = parent->glink;
1137         struct rpmsg_endpoint *ept;
1138         const char *name = chinfo.name;
1139         int cid;
1140         int ret;
1141         unsigned long flags;
1142
1143         spin_lock_irqsave(&glink->idr_lock, flags);
1144         idr_for_each_entry(&glink->rcids, channel, cid) {
1145                 if (!strcmp(channel->name, name))
1146                         break;
1147         }
1148         spin_unlock_irqrestore(&glink->idr_lock, flags);
1149
1150         if (!channel) {
1151                 channel = qcom_glink_create_local(glink, name);
1152                 if (IS_ERR(channel))
1153                         return NULL;
1154         } else {
1155                 ret = qcom_glink_create_remote(glink, channel);
1156                 if (ret)
1157                         return NULL;
1158         }
1159
1160         ept = &channel->ept;
1161         ept->rpdev = rpdev;
1162         ept->cb = cb;
1163         ept->priv = priv;
1164         ept->ops = &glink_endpoint_ops;
1165
1166         return ept;
1167 }
1168
1169 static int qcom_glink_announce_create(struct rpmsg_device *rpdev)
1170 {
1171         struct glink_channel *channel = to_glink_channel(rpdev->ept);
1172         struct device_node *np = rpdev->dev.of_node;
1173         struct qcom_glink *glink = channel->glink;
1174         struct glink_core_rx_intent *intent;
1175         const struct property *prop = NULL;
1176         __be32 defaults[] = { cpu_to_be32(SZ_1K), cpu_to_be32(5) };
1177         int num_intents;
1178         int num_groups = 1;
1179         __be32 *val = defaults;
1180         int size;
1181
1182         if (glink->intentless || !completion_done(&channel->open_ack))
1183                 return 0;
1184
1185         prop = of_find_property(np, "qcom,intents", NULL);
1186         if (prop) {
1187                 val = prop->value;
1188                 num_groups = prop->length / sizeof(u32) / 2;
1189         }
1190
1191         /* Channel is now open, advertise base set of intents */
1192         while (num_groups--) {
1193                 size = be32_to_cpup(val++);
1194                 num_intents = be32_to_cpup(val++);
1195                 while (num_intents--) {
1196                         intent = qcom_glink_alloc_intent(glink, channel, size,
1197                                                          true);
1198                         if (!intent)
1199                                 break;
1200
1201                         qcom_glink_advertise_intent(glink, channel, intent);
1202                 }
1203         }
1204         return 0;
1205 }
1206
1207 static void qcom_glink_destroy_ept(struct rpmsg_endpoint *ept)
1208 {
1209         struct glink_channel *channel = to_glink_channel(ept);
1210         struct qcom_glink *glink = channel->glink;
1211         unsigned long flags;
1212
1213         spin_lock_irqsave(&channel->recv_lock, flags);
1214         channel->ept.cb = NULL;
1215         spin_unlock_irqrestore(&channel->recv_lock, flags);
1216
1217         /* Decouple the potential rpdev from the channel */
1218         channel->rpdev = NULL;
1219
1220         qcom_glink_send_close_req(glink, channel);
1221 }
1222
1223 static int qcom_glink_request_intent(struct qcom_glink *glink,
1224                                      struct glink_channel *channel,
1225                                      size_t size)
1226 {
1227         struct {
1228                 u16 id;
1229                 u16 cid;
1230                 u32 size;
1231         } __packed cmd;
1232
1233         int ret;
1234
1235         mutex_lock(&channel->intent_req_lock);
1236
1237         reinit_completion(&channel->intent_req_comp);
1238
1239         cmd.id = RPM_CMD_RX_INTENT_REQ;
1240         cmd.cid = channel->lcid;
1241         cmd.size = size;
1242
1243         ret = qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
1244         if (ret)
1245                 goto unlock;
1246
1247         ret = wait_for_completion_timeout(&channel->intent_req_comp, 10 * HZ);
1248         if (!ret) {
1249                 dev_err(glink->dev, "intent request timed out\n");
1250                 ret = -ETIMEDOUT;
1251         } else {
1252                 ret = channel->intent_req_result ? 0 : -ECANCELED;
1253         }
1254
1255 unlock:
1256         mutex_unlock(&channel->intent_req_lock);
1257         return ret;
1258 }
1259
1260 static int __qcom_glink_send(struct glink_channel *channel,
1261                              void *data, int len, bool wait)
1262 {
1263         struct qcom_glink *glink = channel->glink;
1264         struct glink_core_rx_intent *intent = NULL;
1265         struct glink_core_rx_intent *tmp;
1266         int iid = 0;
1267         struct {
1268                 struct glink_msg msg;
1269                 __le32 chunk_size;
1270                 __le32 left_size;
1271         } __packed req;
1272         int ret;
1273         unsigned long flags;
1274
1275         if (!glink->intentless) {
1276                 while (!intent) {
1277                         spin_lock_irqsave(&channel->intent_lock, flags);
1278                         idr_for_each_entry(&channel->riids, tmp, iid) {
1279                                 if (tmp->size >= len && !tmp->in_use) {
1280                                         if (!intent)
1281                                                 intent = tmp;
1282                                         else if (intent->size > tmp->size)
1283                                                 intent = tmp;
1284                                         if (intent->size == len)
1285                                                 break;
1286                                 }
1287                         }
1288                         if (intent)
1289                                 intent->in_use = true;
1290                         spin_unlock_irqrestore(&channel->intent_lock, flags);
1291
1292                         /* We found an available intent */
1293                         if (intent)
1294                                 break;
1295
1296                         if (!wait)
1297                                 return -EBUSY;
1298
1299                         ret = qcom_glink_request_intent(glink, channel, len);
1300                         if (ret < 0)
1301                                 return ret;
1302                 }
1303
1304                 iid = intent->id;
1305         }
1306
1307         req.msg.cmd = cpu_to_le16(RPM_CMD_TX_DATA);
1308         req.msg.param1 = cpu_to_le16(channel->lcid);
1309         req.msg.param2 = cpu_to_le32(iid);
1310         req.chunk_size = cpu_to_le32(len);
1311         req.left_size = cpu_to_le32(0);
1312
1313         ret = qcom_glink_tx(glink, &req, sizeof(req), data, len, wait);
1314
1315         /* Mark intent available if we failed */
1316         if (ret && intent)
1317                 intent->in_use = false;
1318
1319         return ret;
1320 }
1321
1322 static int qcom_glink_send(struct rpmsg_endpoint *ept, void *data, int len)
1323 {
1324         struct glink_channel *channel = to_glink_channel(ept);
1325
1326         return __qcom_glink_send(channel, data, len, true);
1327 }
1328
1329 static int qcom_glink_trysend(struct rpmsg_endpoint *ept, void *data, int len)
1330 {
1331         struct glink_channel *channel = to_glink_channel(ept);
1332
1333         return __qcom_glink_send(channel, data, len, false);
1334 }
1335
1336 static int qcom_glink_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
1337 {
1338         struct glink_channel *channel = to_glink_channel(ept);
1339
1340         return __qcom_glink_send(channel, data, len, true);
1341 }
1342
1343 static int qcom_glink_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
1344 {
1345         struct glink_channel *channel = to_glink_channel(ept);
1346
1347         return __qcom_glink_send(channel, data, len, false);
1348 }
1349
1350 /*
1351  * Finds the device_node for the glink child interested in this channel.
1352  */
1353 static struct device_node *qcom_glink_match_channel(struct device_node *node,
1354                                                     const char *channel)
1355 {
1356         struct device_node *child;
1357         const char *name;
1358         const char *key;
1359         int ret;
1360
1361         for_each_available_child_of_node(node, child) {
1362                 key = "qcom,glink-channels";
1363                 ret = of_property_read_string(child, key, &name);
1364                 if (ret)
1365                         continue;
1366
1367                 if (strcmp(name, channel) == 0)
1368                         return child;
1369         }
1370
1371         return NULL;
1372 }
1373
1374 static const struct rpmsg_device_ops glink_device_ops = {
1375         .create_ept = qcom_glink_create_ept,
1376         .announce_create = qcom_glink_announce_create,
1377 };
1378
1379 static const struct rpmsg_endpoint_ops glink_endpoint_ops = {
1380         .destroy_ept = qcom_glink_destroy_ept,
1381         .send = qcom_glink_send,
1382         .sendto = qcom_glink_sendto,
1383         .trysend = qcom_glink_trysend,
1384         .trysendto = qcom_glink_trysendto,
1385 };
1386
1387 static void qcom_glink_rpdev_release(struct device *dev)
1388 {
1389         struct rpmsg_device *rpdev = to_rpmsg_device(dev);
1390         struct glink_channel *channel = to_glink_channel(rpdev->ept);
1391
1392         channel->rpdev = NULL;
1393         kfree(rpdev);
1394 }
1395
1396 static int qcom_glink_rx_open(struct qcom_glink *glink, unsigned int rcid,
1397                               char *name)
1398 {
1399         struct glink_channel *channel;
1400         struct rpmsg_device *rpdev;
1401         bool create_device = false;
1402         struct device_node *node;
1403         int lcid;
1404         int ret;
1405         unsigned long flags;
1406
1407         spin_lock_irqsave(&glink->idr_lock, flags);
1408         idr_for_each_entry(&glink->lcids, channel, lcid) {
1409                 if (!strcmp(channel->name, name))
1410                         break;
1411         }
1412         spin_unlock_irqrestore(&glink->idr_lock, flags);
1413
1414         if (!channel) {
1415                 channel = qcom_glink_alloc_channel(glink, name);
1416                 if (IS_ERR(channel))
1417                         return PTR_ERR(channel);
1418
1419                 /* The opening dance was initiated by the remote */
1420                 create_device = true;
1421         }
1422
1423         spin_lock_irqsave(&glink->idr_lock, flags);
1424         ret = idr_alloc(&glink->rcids, channel, rcid, rcid + 1, GFP_ATOMIC);
1425         if (ret < 0) {
1426                 dev_err(glink->dev, "Unable to insert channel into rcid list\n");
1427                 spin_unlock_irqrestore(&glink->idr_lock, flags);
1428                 goto free_channel;
1429         }
1430         channel->rcid = ret;
1431         spin_unlock_irqrestore(&glink->idr_lock, flags);
1432
1433         complete_all(&channel->open_req);
1434
1435         if (create_device) {
1436                 rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL);
1437                 if (!rpdev) {
1438                         ret = -ENOMEM;
1439                         goto rcid_remove;
1440                 }
1441
1442                 rpdev->ept = &channel->ept;
1443                 strncpy(rpdev->id.name, name, RPMSG_NAME_SIZE);
1444                 rpdev->src = RPMSG_ADDR_ANY;
1445                 rpdev->dst = RPMSG_ADDR_ANY;
1446                 rpdev->ops = &glink_device_ops;
1447
1448                 node = qcom_glink_match_channel(glink->dev->of_node, name);
1449                 rpdev->dev.of_node = node;
1450                 rpdev->dev.parent = glink->dev;
1451                 rpdev->dev.release = qcom_glink_rpdev_release;
1452
1453                 ret = rpmsg_register_device(rpdev);
1454                 if (ret)
1455                         goto rcid_remove;
1456
1457                 channel->rpdev = rpdev;
1458         }
1459
1460         return 0;
1461
1462 rcid_remove:
1463         spin_lock_irqsave(&glink->idr_lock, flags);
1464         idr_remove(&glink->rcids, channel->rcid);
1465         channel->rcid = 0;
1466         spin_unlock_irqrestore(&glink->idr_lock, flags);
1467 free_channel:
1468         /* Release the reference, iff we took it */
1469         if (create_device)
1470                 kref_put(&channel->refcount, qcom_glink_channel_release);
1471
1472         return ret;
1473 }
1474
1475 static void qcom_glink_rx_close(struct qcom_glink *glink, unsigned int rcid)
1476 {
1477         struct rpmsg_channel_info chinfo;
1478         struct glink_channel *channel;
1479         unsigned long flags;
1480
1481         spin_lock_irqsave(&glink->idr_lock, flags);
1482         channel = idr_find(&glink->rcids, rcid);
1483         spin_unlock_irqrestore(&glink->idr_lock, flags);
1484         if (WARN(!channel, "close request on unknown channel\n"))
1485                 return;
1486
1487         /* cancel pending rx_done work */
1488         cancel_work_sync(&channel->intent_work);
1489
1490         if (channel->rpdev) {
1491                 strncpy(chinfo.name, channel->name, sizeof(chinfo.name));
1492                 chinfo.src = RPMSG_ADDR_ANY;
1493                 chinfo.dst = RPMSG_ADDR_ANY;
1494
1495                 rpmsg_unregister_device(glink->dev, &chinfo);
1496         }
1497
1498         qcom_glink_send_close_ack(glink, channel->rcid);
1499
1500         spin_lock_irqsave(&glink->idr_lock, flags);
1501         idr_remove(&glink->rcids, channel->rcid);
1502         channel->rcid = 0;
1503         spin_unlock_irqrestore(&glink->idr_lock, flags);
1504
1505         kref_put(&channel->refcount, qcom_glink_channel_release);
1506 }
1507
1508 static void qcom_glink_rx_close_ack(struct qcom_glink *glink, unsigned int lcid)
1509 {
1510         struct glink_channel *channel;
1511         unsigned long flags;
1512
1513         spin_lock_irqsave(&glink->idr_lock, flags);
1514         channel = idr_find(&glink->lcids, lcid);
1515         if (WARN(!channel, "close ack on unknown channel\n")) {
1516                 spin_unlock_irqrestore(&glink->idr_lock, flags);
1517                 return;
1518         }
1519
1520         idr_remove(&glink->lcids, channel->lcid);
1521         channel->lcid = 0;
1522         spin_unlock_irqrestore(&glink->idr_lock, flags);
1523
1524         kref_put(&channel->refcount, qcom_glink_channel_release);
1525 }
1526
1527 static void qcom_glink_work(struct work_struct *work)
1528 {
1529         struct qcom_glink *glink = container_of(work, struct qcom_glink,
1530                                                 rx_work);
1531         struct glink_defer_cmd *dcmd;
1532         struct glink_msg *msg;
1533         unsigned long flags;
1534         unsigned int param1;
1535         unsigned int param2;
1536         unsigned int cmd;
1537
1538         for (;;) {
1539                 spin_lock_irqsave(&glink->rx_lock, flags);
1540                 if (list_empty(&glink->rx_queue)) {
1541                         spin_unlock_irqrestore(&glink->rx_lock, flags);
1542                         break;
1543                 }
1544                 dcmd = list_first_entry(&glink->rx_queue,
1545                                         struct glink_defer_cmd, node);
1546                 list_del(&dcmd->node);
1547                 spin_unlock_irqrestore(&glink->rx_lock, flags);
1548
1549                 msg = &dcmd->msg;
1550                 cmd = le16_to_cpu(msg->cmd);
1551                 param1 = le16_to_cpu(msg->param1);
1552                 param2 = le32_to_cpu(msg->param2);
1553
1554                 switch (cmd) {
1555                 case RPM_CMD_VERSION:
1556                         qcom_glink_receive_version(glink, param1, param2);
1557                         break;
1558                 case RPM_CMD_VERSION_ACK:
1559                         qcom_glink_receive_version_ack(glink, param1, param2);
1560                         break;
1561                 case RPM_CMD_OPEN:
1562                         qcom_glink_rx_open(glink, param1, msg->data);
1563                         break;
1564                 case RPM_CMD_CLOSE:
1565                         qcom_glink_rx_close(glink, param1);
1566                         break;
1567                 case RPM_CMD_CLOSE_ACK:
1568                         qcom_glink_rx_close_ack(glink, param1);
1569                         break;
1570                 case RPM_CMD_RX_INTENT_REQ:
1571                         qcom_glink_handle_intent_req(glink, param1, param2);
1572                         break;
1573                 default:
1574                         WARN(1, "Unknown defer object %d\n", cmd);
1575                         break;
1576                 }
1577
1578                 kfree(dcmd);
1579         }
1580 }
1581
1582 static void qcom_glink_cancel_rx_work(struct qcom_glink *glink)
1583 {
1584         struct glink_defer_cmd *dcmd;
1585         struct glink_defer_cmd *tmp;
1586
1587         /* cancel any pending deferred rx_work */
1588         cancel_work_sync(&glink->rx_work);
1589
1590         list_for_each_entry_safe(dcmd, tmp, &glink->rx_queue, node)
1591                 kfree(dcmd);
1592 }
1593
1594 static ssize_t rpmsg_name_show(struct device *dev,
1595                                struct device_attribute *attr, char *buf)
1596 {
1597         int ret = 0;
1598         const char *name;
1599
1600         ret = of_property_read_string(dev->of_node, "label", &name);
1601         if (ret < 0)
1602                 name = dev->of_node->name;
1603
1604         return snprintf(buf, RPMSG_NAME_SIZE, "%s\n", name);
1605 }
1606 static DEVICE_ATTR_RO(rpmsg_name);
1607
1608 static struct attribute *qcom_glink_attrs[] = {
1609         &dev_attr_rpmsg_name.attr,
1610         NULL
1611 };
1612 ATTRIBUTE_GROUPS(qcom_glink);
1613
1614 static void qcom_glink_device_release(struct device *dev)
1615 {
1616         struct rpmsg_device *rpdev = to_rpmsg_device(dev);
1617         struct glink_channel *channel = to_glink_channel(rpdev->ept);
1618
1619         /* Release qcom_glink_alloc_channel() reference */
1620         kref_put(&channel->refcount, qcom_glink_channel_release);
1621         kfree(rpdev);
1622 }
1623
1624 static int qcom_glink_create_chrdev(struct qcom_glink *glink)
1625 {
1626         struct rpmsg_device *rpdev;
1627         struct glink_channel *channel;
1628
1629         rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL);
1630         if (!rpdev)
1631                 return -ENOMEM;
1632
1633         channel = qcom_glink_alloc_channel(glink, "rpmsg_chrdev");
1634         if (IS_ERR(channel)) {
1635                 kfree(rpdev);
1636                 return PTR_ERR(channel);
1637         }
1638         channel->rpdev = rpdev;
1639
1640         rpdev->ept = &channel->ept;
1641         rpdev->ops = &glink_device_ops;
1642         rpdev->dev.parent = glink->dev;
1643         rpdev->dev.release = qcom_glink_device_release;
1644
1645         return rpmsg_chrdev_register_device(rpdev);
1646 }
1647
1648 struct qcom_glink *qcom_glink_native_probe(struct device *dev,
1649                                            unsigned long features,
1650                                            struct qcom_glink_pipe *rx,
1651                                            struct qcom_glink_pipe *tx,
1652                                            bool intentless)
1653 {
1654         int irq;
1655         int ret;
1656         struct qcom_glink *glink;
1657
1658         glink = devm_kzalloc(dev, sizeof(*glink), GFP_KERNEL);
1659         if (!glink)
1660                 return ERR_PTR(-ENOMEM);
1661
1662         glink->dev = dev;
1663         glink->tx_pipe = tx;
1664         glink->rx_pipe = rx;
1665
1666         glink->features = features;
1667         glink->intentless = intentless;
1668
1669         spin_lock_init(&glink->tx_lock);
1670         spin_lock_init(&glink->rx_lock);
1671         INIT_LIST_HEAD(&glink->rx_queue);
1672         INIT_WORK(&glink->rx_work, qcom_glink_work);
1673
1674         spin_lock_init(&glink->idr_lock);
1675         idr_init(&glink->lcids);
1676         idr_init(&glink->rcids);
1677
1678         glink->dev->groups = qcom_glink_groups;
1679
1680         ret = device_add_groups(dev, qcom_glink_groups);
1681         if (ret)
1682                 dev_err(dev, "failed to add groups\n");
1683
1684         ret = of_property_read_string(dev->of_node, "label", &glink->name);
1685         if (ret < 0)
1686                 glink->name = dev->of_node->name;
1687
1688         glink->mbox_client.dev = dev;
1689         glink->mbox_client.knows_txdone = true;
1690         glink->mbox_chan = mbox_request_channel(&glink->mbox_client, 0);
1691         if (IS_ERR(glink->mbox_chan)) {
1692                 if (PTR_ERR(glink->mbox_chan) != -EPROBE_DEFER)
1693                         dev_err(dev, "failed to acquire IPC channel\n");
1694                 return ERR_CAST(glink->mbox_chan);
1695         }
1696
1697         irq = of_irq_get(dev->of_node, 0);
1698         ret = devm_request_irq(dev, irq,
1699                                qcom_glink_native_intr,
1700                                IRQF_NO_SUSPEND | IRQF_SHARED,
1701                                "glink-native", glink);
1702         if (ret) {
1703                 dev_err(dev, "failed to request IRQ\n");
1704                 return ERR_PTR(ret);
1705         }
1706
1707         glink->irq = irq;
1708
1709         ret = qcom_glink_send_version(glink);
1710         if (ret)
1711                 return ERR_PTR(ret);
1712
1713         ret = qcom_glink_create_chrdev(glink);
1714         if (ret)
1715                 dev_err(glink->dev, "failed to register chrdev\n");
1716
1717         return glink;
1718 }
1719 EXPORT_SYMBOL_GPL(qcom_glink_native_probe);
1720
1721 static int qcom_glink_remove_device(struct device *dev, void *data)
1722 {
1723         device_unregister(dev);
1724
1725         return 0;
1726 }
1727
1728 void qcom_glink_native_remove(struct qcom_glink *glink)
1729 {
1730         struct glink_channel *channel;
1731         int cid;
1732         int ret;
1733
1734         disable_irq(glink->irq);
1735         qcom_glink_cancel_rx_work(glink);
1736
1737         ret = device_for_each_child(glink->dev, NULL, qcom_glink_remove_device);
1738         if (ret)
1739                 dev_warn(glink->dev, "Can't remove GLINK devices: %d\n", ret);
1740
1741         /* Release any defunct local channels, waiting for close-ack */
1742         idr_for_each_entry(&glink->lcids, channel, cid)
1743                 kref_put(&channel->refcount, qcom_glink_channel_release);
1744
1745         /* Release any defunct local channels, waiting for close-req */
1746         idr_for_each_entry(&glink->rcids, channel, cid)
1747                 kref_put(&channel->refcount, qcom_glink_channel_release);
1748
1749         idr_destroy(&glink->lcids);
1750         idr_destroy(&glink->rcids);
1751         mbox_free_channel(glink->mbox_chan);
1752 }
1753 EXPORT_SYMBOL_GPL(qcom_glink_native_remove);
1754
1755 void qcom_glink_native_unregister(struct qcom_glink *glink)
1756 {
1757         device_unregister(glink->dev);
1758 }
1759 EXPORT_SYMBOL_GPL(qcom_glink_native_unregister);
1760
1761 MODULE_DESCRIPTION("Qualcomm GLINK driver");
1762 MODULE_LICENSE("GPL v2");