GNU Linux-libre 4.14.332-gnu1
[releases.git] / drivers / rpmsg / qcom_glink_native.c
1 /*
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/sizes.h>
27 #include <linux/slab.h>
28 #include <linux/workqueue.h>
29 #include <linux/mailbox_client.h>
30
31 #include "rpmsg_internal.h"
32 #include "qcom_glink_native.h"
33
34 #define GLINK_NAME_SIZE         32
35 #define GLINK_VERSION_1         1
36
37 #define RPM_GLINK_CID_MIN       1
38 #define RPM_GLINK_CID_MAX       65536
39
40 struct glink_msg {
41         __le16 cmd;
42         __le16 param1;
43         __le32 param2;
44         u8 data[];
45 } __packed;
46
47 /**
48  * struct glink_defer_cmd - deferred incoming control message
49  * @node:       list node
50  * @msg:        message header
51  * data:        payload of the message
52  *
53  * Copy of a received control message, to be added to @rx_queue and processed
54  * by @rx_work of @qcom_glink.
55  */
56 struct glink_defer_cmd {
57         struct list_head node;
58
59         struct glink_msg msg;
60         u8 data[];
61 };
62
63 /**
64  * struct glink_core_rx_intent - RX intent
65  * RX intent
66  *
67  * data: pointer to the data (may be NULL for zero-copy)
68  * id: remote or local intent ID
69  * size: size of the original intent (do not modify)
70  * reuse: To mark if the intent can be reused after first use
71  * in_use: To mark if intent is already in use for the channel
72  * offset: next write offset (initially 0)
73  */
74 struct glink_core_rx_intent {
75         void *data;
76         u32 id;
77         size_t size;
78         bool reuse;
79         bool in_use;
80         u32 offset;
81
82         struct list_head node;
83 };
84
85 /**
86  * struct qcom_glink - driver context, relates to one remote subsystem
87  * @dev:        reference to the associated struct device
88  * @mbox_client: mailbox client
89  * @mbox_chan:  mailbox channel
90  * @rx_pipe:    pipe object for receive FIFO
91  * @tx_pipe:    pipe object for transmit FIFO
92  * @irq:        IRQ for signaling incoming events
93  * @rx_work:    worker for handling received control messages
94  * @rx_lock:    protects the @rx_queue
95  * @rx_queue:   queue of received control messages to be processed in @rx_work
96  * @tx_lock:    synchronizes operations on the tx fifo
97  * @idr_lock:   synchronizes @lcids and @rcids modifications
98  * @lcids:      idr of all channels with a known local channel id
99  * @rcids:      idr of all channels with a known remote channel id
100  */
101 struct qcom_glink {
102         struct device *dev;
103
104         struct mbox_client mbox_client;
105         struct mbox_chan *mbox_chan;
106
107         struct qcom_glink_pipe *rx_pipe;
108         struct qcom_glink_pipe *tx_pipe;
109
110         int irq;
111
112         struct work_struct rx_work;
113         spinlock_t rx_lock;
114         struct list_head rx_queue;
115
116         struct mutex tx_lock;
117
118         spinlock_t idr_lock;
119         struct idr lcids;
120         struct idr rcids;
121         unsigned long features;
122
123         bool intentless;
124 };
125
126 enum {
127         GLINK_STATE_CLOSED,
128         GLINK_STATE_OPENING,
129         GLINK_STATE_OPEN,
130         GLINK_STATE_CLOSING,
131 };
132
133 /**
134  * struct glink_channel - internal representation of a channel
135  * @rpdev:      rpdev reference, only used for primary endpoints
136  * @ept:        rpmsg endpoint this channel is associated with
137  * @glink:      qcom_glink context handle
138  * @refcount:   refcount for the channel object
139  * @recv_lock:  guard for @ept.cb
140  * @name:       unique channel name/identifier
141  * @lcid:       channel id, in local space
142  * @rcid:       channel id, in remote space
143  * @intent_lock: lock for protection of @liids, @riids
144  * @liids:      idr of all local intents
145  * @riids:      idr of all remote intents
146  * @intent_work: worker responsible for transmitting rx_done packets
147  * @done_intents: list of intents that needs to be announced rx_done
148  * @buf:        receive buffer, for gathering fragments
149  * @buf_offset: write offset in @buf
150  * @buf_size:   size of current @buf
151  * @open_ack:   completed once remote has acked the open-request
152  * @open_req:   completed once open-request has been received
153  * @intent_req_lock: Synchronises multiple intent requests
154  * @intent_req_result: Result of intent request
155  * @intent_req_comp: Completion for intent_req signalling
156  */
157 struct glink_channel {
158         struct rpmsg_endpoint ept;
159
160         struct rpmsg_device *rpdev;
161         struct qcom_glink *glink;
162
163         struct kref refcount;
164
165         spinlock_t recv_lock;
166
167         char *name;
168         unsigned int lcid;
169         unsigned int rcid;
170
171         spinlock_t intent_lock;
172         struct idr liids;
173         struct idr riids;
174         struct work_struct intent_work;
175         struct list_head done_intents;
176
177         struct glink_core_rx_intent *buf;
178         int buf_offset;
179         int buf_size;
180
181         struct completion open_ack;
182         struct completion open_req;
183
184         struct mutex intent_req_lock;
185         bool intent_req_result;
186         struct completion intent_req_comp;
187 };
188
189 #define to_glink_channel(_ept) container_of(_ept, struct glink_channel, ept)
190
191 static const struct rpmsg_endpoint_ops glink_endpoint_ops;
192
193 #define RPM_CMD_VERSION                 0
194 #define RPM_CMD_VERSION_ACK             1
195 #define RPM_CMD_OPEN                    2
196 #define RPM_CMD_CLOSE                   3
197 #define RPM_CMD_OPEN_ACK                4
198 #define RPM_CMD_INTENT                  5
199 #define RPM_CMD_RX_DONE                 6
200 #define RPM_CMD_RX_INTENT_REQ           7
201 #define RPM_CMD_RX_INTENT_REQ_ACK       8
202 #define RPM_CMD_TX_DATA                 9
203 #define RPM_CMD_CLOSE_ACK               11
204 #define RPM_CMD_TX_DATA_CONT            12
205 #define RPM_CMD_READ_NOTIF              13
206 #define RPM_CMD_RX_DONE_W_REUSE         14
207
208 #define GLINK_FEATURE_INTENTLESS        BIT(1)
209
210 static void qcom_glink_rx_done_work(struct work_struct *work);
211
212 static struct glink_channel *qcom_glink_alloc_channel(struct qcom_glink *glink,
213                                                       const char *name)
214 {
215         struct glink_channel *channel;
216
217         channel = kzalloc(sizeof(*channel), GFP_KERNEL);
218         if (!channel)
219                 return ERR_PTR(-ENOMEM);
220
221         /* Setup glink internal glink_channel data */
222         spin_lock_init(&channel->recv_lock);
223         spin_lock_init(&channel->intent_lock);
224         mutex_init(&channel->intent_req_lock);
225
226         channel->glink = glink;
227         channel->name = kstrdup(name, GFP_KERNEL);
228         if (!channel->name) {
229                 kfree(channel);
230                 return ERR_PTR(-ENOMEM);
231         }
232
233         init_completion(&channel->open_req);
234         init_completion(&channel->open_ack);
235         init_completion(&channel->intent_req_comp);
236
237         INIT_LIST_HEAD(&channel->done_intents);
238         INIT_WORK(&channel->intent_work, qcom_glink_rx_done_work);
239
240         idr_init(&channel->liids);
241         idr_init(&channel->riids);
242         kref_init(&channel->refcount);
243
244         return channel;
245 }
246
247 static void qcom_glink_channel_release(struct kref *ref)
248 {
249         struct glink_channel *channel = container_of(ref, struct glink_channel,
250                                                      refcount);
251         struct glink_core_rx_intent *intent;
252         struct glink_core_rx_intent *tmp;
253         unsigned long flags;
254         int iid;
255
256         /* cancel pending rx_done work */
257         cancel_work_sync(&channel->intent_work);
258
259         spin_lock_irqsave(&channel->intent_lock, flags);
260         /* Free all non-reuse intents pending rx_done work */
261         list_for_each_entry_safe(intent, tmp, &channel->done_intents, node) {
262                 if (!intent->reuse) {
263                         kfree(intent->data);
264                         kfree(intent);
265                 }
266         }
267
268         idr_for_each_entry(&channel->liids, tmp, iid) {
269                 kfree(tmp->data);
270                 kfree(tmp);
271         }
272         idr_destroy(&channel->liids);
273
274         idr_for_each_entry(&channel->riids, tmp, iid)
275                 kfree(tmp);
276         idr_destroy(&channel->riids);
277         spin_unlock_irqrestore(&channel->intent_lock, flags);
278
279         kfree(channel->name);
280         kfree(channel);
281 }
282
283 static size_t qcom_glink_rx_avail(struct qcom_glink *glink)
284 {
285         return glink->rx_pipe->avail(glink->rx_pipe);
286 }
287
288 static void qcom_glink_rx_peak(struct qcom_glink *glink,
289                                void *data, unsigned int offset, size_t count)
290 {
291         glink->rx_pipe->peak(glink->rx_pipe, data, offset, count);
292 }
293
294 static void qcom_glink_rx_advance(struct qcom_glink *glink, size_t count)
295 {
296         glink->rx_pipe->advance(glink->rx_pipe, count);
297 }
298
299 static size_t qcom_glink_tx_avail(struct qcom_glink *glink)
300 {
301         return glink->tx_pipe->avail(glink->tx_pipe);
302 }
303
304 static void qcom_glink_tx_write(struct qcom_glink *glink,
305                                 const void *hdr, size_t hlen,
306                                 const void *data, size_t dlen)
307 {
308         glink->tx_pipe->write(glink->tx_pipe, hdr, hlen, data, dlen);
309 }
310
311 static int qcom_glink_tx(struct qcom_glink *glink,
312                          const void *hdr, size_t hlen,
313                          const void *data, size_t dlen, bool wait)
314 {
315         unsigned int tlen = hlen + dlen;
316         int ret;
317
318         /* Reject packets that are too big */
319         if (tlen >= glink->tx_pipe->length)
320                 return -EINVAL;
321
322         ret = mutex_lock_interruptible(&glink->tx_lock);
323         if (ret)
324                 return ret;
325
326         while (qcom_glink_tx_avail(glink) < tlen) {
327                 if (!wait) {
328                         ret = -EAGAIN;
329                         goto out;
330                 }
331
332                 usleep_range(10000, 15000);
333         }
334
335         qcom_glink_tx_write(glink, hdr, hlen, data, dlen);
336
337         mbox_send_message(glink->mbox_chan, NULL);
338         mbox_client_txdone(glink->mbox_chan, 0);
339
340 out:
341         mutex_unlock(&glink->tx_lock);
342
343         return ret;
344 }
345
346 static int qcom_glink_send_version(struct qcom_glink *glink)
347 {
348         struct glink_msg msg;
349
350         msg.cmd = cpu_to_le16(RPM_CMD_VERSION);
351         msg.param1 = cpu_to_le16(GLINK_VERSION_1);
352         msg.param2 = cpu_to_le32(glink->features);
353
354         return qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
355 }
356
357 static void qcom_glink_send_version_ack(struct qcom_glink *glink)
358 {
359         struct glink_msg msg;
360
361         msg.cmd = cpu_to_le16(RPM_CMD_VERSION_ACK);
362         msg.param1 = cpu_to_le16(GLINK_VERSION_1);
363         msg.param2 = cpu_to_le32(glink->features);
364
365         qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
366 }
367
368 static void qcom_glink_send_open_ack(struct qcom_glink *glink,
369                                      struct glink_channel *channel)
370 {
371         struct glink_msg msg;
372
373         msg.cmd = cpu_to_le16(RPM_CMD_OPEN_ACK);
374         msg.param1 = cpu_to_le16(channel->rcid);
375         msg.param2 = cpu_to_le32(0);
376
377         qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
378 }
379
380 static void qcom_glink_handle_intent_req_ack(struct qcom_glink *glink,
381                                              unsigned int cid, bool granted)
382 {
383         struct glink_channel *channel;
384         unsigned long flags;
385
386         spin_lock_irqsave(&glink->idr_lock, flags);
387         channel = idr_find(&glink->rcids, cid);
388         spin_unlock_irqrestore(&glink->idr_lock, flags);
389         if (!channel) {
390                 dev_err(glink->dev, "unable to find channel\n");
391                 return;
392         }
393
394         channel->intent_req_result = granted;
395         complete(&channel->intent_req_comp);
396 }
397
398 /**
399  * qcom_glink_send_open_req() - send a RPM_CMD_OPEN request to the remote
400  * @glink: Ptr to the glink edge
401  * @channel: Ptr to the channel that the open req is sent
402  *
403  * Allocates a local channel id and sends a RPM_CMD_OPEN message to the remote.
404  * Will return with refcount held, regardless of outcome.
405  *
406  * Returns 0 on success, negative errno otherwise.
407  */
408 static int qcom_glink_send_open_req(struct qcom_glink *glink,
409                                     struct glink_channel *channel)
410 {
411         struct {
412                 struct glink_msg msg;
413                 u8 name[GLINK_NAME_SIZE];
414         } __packed req;
415         int name_len = strlen(channel->name) + 1;
416         int req_len = ALIGN(sizeof(req.msg) + name_len, 8);
417         int ret;
418         unsigned long flags;
419
420         kref_get(&channel->refcount);
421
422         spin_lock_irqsave(&glink->idr_lock, flags);
423         ret = idr_alloc_cyclic(&glink->lcids, channel,
424                                RPM_GLINK_CID_MIN, RPM_GLINK_CID_MAX,
425                                GFP_ATOMIC);
426         spin_unlock_irqrestore(&glink->idr_lock, flags);
427         if (ret < 0)
428                 return ret;
429
430         channel->lcid = ret;
431
432         req.msg.cmd = cpu_to_le16(RPM_CMD_OPEN);
433         req.msg.param1 = cpu_to_le16(channel->lcid);
434         req.msg.param2 = cpu_to_le32(name_len);
435         strcpy(req.name, channel->name);
436
437         ret = qcom_glink_tx(glink, &req, req_len, NULL, 0, true);
438         if (ret)
439                 goto remove_idr;
440
441         return 0;
442
443 remove_idr:
444         spin_lock_irqsave(&glink->idr_lock, flags);
445         idr_remove(&glink->lcids, channel->lcid);
446         channel->lcid = 0;
447         spin_unlock_irqrestore(&glink->idr_lock, flags);
448
449         return ret;
450 }
451
452 static void qcom_glink_send_close_req(struct qcom_glink *glink,
453                                       struct glink_channel *channel)
454 {
455         struct glink_msg req;
456
457         req.cmd = cpu_to_le16(RPM_CMD_CLOSE);
458         req.param1 = cpu_to_le16(channel->lcid);
459         req.param2 = 0;
460
461         qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
462 }
463
464 static void qcom_glink_send_close_ack(struct qcom_glink *glink,
465                                       unsigned int rcid)
466 {
467         struct glink_msg req;
468
469         req.cmd = cpu_to_le16(RPM_CMD_CLOSE_ACK);
470         req.param1 = cpu_to_le16(rcid);
471         req.param2 = 0;
472
473         qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
474 }
475
476 static void qcom_glink_rx_done_work(struct work_struct *work)
477 {
478         struct glink_channel *channel = container_of(work, struct glink_channel,
479                                                      intent_work);
480         struct qcom_glink *glink = channel->glink;
481         struct glink_core_rx_intent *intent, *tmp;
482         struct {
483                 u16 id;
484                 u16 lcid;
485                 u32 liid;
486         } __packed cmd;
487
488         unsigned int cid = channel->lcid;
489         unsigned int iid;
490         bool reuse;
491         unsigned long flags;
492
493         spin_lock_irqsave(&channel->intent_lock, flags);
494         list_for_each_entry_safe(intent, tmp, &channel->done_intents, node) {
495                 list_del(&intent->node);
496                 spin_unlock_irqrestore(&channel->intent_lock, flags);
497                 iid = intent->id;
498                 reuse = intent->reuse;
499
500                 cmd.id = reuse ? RPM_CMD_RX_DONE_W_REUSE : RPM_CMD_RX_DONE;
501                 cmd.lcid = cid;
502                 cmd.liid = iid;
503
504                 qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
505                 if (!reuse) {
506                         kfree(intent->data);
507                         kfree(intent);
508                 }
509                 spin_lock_irqsave(&channel->intent_lock, flags);
510         }
511         spin_unlock_irqrestore(&channel->intent_lock, flags);
512 }
513
514 static void qcom_glink_rx_done(struct qcom_glink *glink,
515                                struct glink_channel *channel,
516                                struct glink_core_rx_intent *intent)
517 {
518         /* We don't send RX_DONE to intentless systems */
519         if (glink->intentless) {
520                 kfree(intent->data);
521                 kfree(intent);
522                 return;
523         }
524
525         /* Take it off the tree of receive intents */
526         if (!intent->reuse) {
527                 spin_lock(&channel->intent_lock);
528                 idr_remove(&channel->liids, intent->id);
529                 spin_unlock(&channel->intent_lock);
530         }
531
532         /* Schedule the sending of a rx_done indication */
533         spin_lock(&channel->intent_lock);
534         list_add_tail(&intent->node, &channel->done_intents);
535         spin_unlock(&channel->intent_lock);
536
537         schedule_work(&channel->intent_work);
538 }
539
540 /**
541  * qcom_glink_receive_version() - receive version/features from remote system
542  *
543  * @glink:      pointer to transport interface
544  * @r_version:  remote version
545  * @r_features: remote features
546  *
547  * This function is called in response to a remote-initiated version/feature
548  * negotiation sequence.
549  */
550 static void qcom_glink_receive_version(struct qcom_glink *glink,
551                                        u32 version,
552                                        u32 features)
553 {
554         switch (version) {
555         case 0:
556                 break;
557         case GLINK_VERSION_1:
558                 glink->features &= features;
559                 /* FALLTHROUGH */
560         default:
561                 qcom_glink_send_version_ack(glink);
562                 break;
563         }
564 }
565
566 /**
567  * qcom_glink_receive_version_ack() - receive negotiation ack from remote system
568  *
569  * @glink:      pointer to transport interface
570  * @r_version:  remote version response
571  * @r_features: remote features response
572  *
573  * This function is called in response to a local-initiated version/feature
574  * negotiation sequence and is the counter-offer from the remote side based
575  * upon the initial version and feature set requested.
576  */
577 static void qcom_glink_receive_version_ack(struct qcom_glink *glink,
578                                            u32 version,
579                                            u32 features)
580 {
581         switch (version) {
582         case 0:
583                 /* Version negotiation failed */
584                 break;
585         case GLINK_VERSION_1:
586                 if (features == glink->features)
587                         break;
588
589                 glink->features &= features;
590                 /* FALLTHROUGH */
591         default:
592                 qcom_glink_send_version(glink);
593                 break;
594         }
595 }
596
597 /**
598  * qcom_glink_send_intent_req_ack() - convert an rx intent request ack cmd to
599                                       wire format and transmit
600  * @glink:      The transport to transmit on.
601  * @channel:    The glink channel
602  * @granted:    The request response to encode.
603  *
604  * Return: 0 on success or standard Linux error code.
605  */
606 static int qcom_glink_send_intent_req_ack(struct qcom_glink *glink,
607                                           struct glink_channel *channel,
608                                           bool granted)
609 {
610         struct glink_msg msg;
611
612         msg.cmd = cpu_to_le16(RPM_CMD_RX_INTENT_REQ_ACK);
613         msg.param1 = cpu_to_le16(channel->lcid);
614         msg.param2 = cpu_to_le32(granted);
615
616         qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
617
618         return 0;
619 }
620
621 /**
622  * qcom_glink_advertise_intent - convert an rx intent cmd to wire format and
623  *                         transmit
624  * @glink:      The transport to transmit on.
625  * @channel:    The local channel
626  * @size:       The intent to pass on to remote.
627  *
628  * Return: 0 on success or standard Linux error code.
629  */
630 static int qcom_glink_advertise_intent(struct qcom_glink *glink,
631                                        struct glink_channel *channel,
632                                        struct glink_core_rx_intent *intent)
633 {
634         struct command {
635                 u16 id;
636                 u16 lcid;
637                 u32 count;
638                 u32 size;
639                 u32 liid;
640         } __packed;
641         struct command cmd;
642
643         cmd.id = cpu_to_le16(RPM_CMD_INTENT);
644         cmd.lcid = cpu_to_le16(channel->lcid);
645         cmd.count = cpu_to_le32(1);
646         cmd.size = cpu_to_le32(intent->size);
647         cmd.liid = cpu_to_le32(intent->id);
648
649         qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
650
651         return 0;
652 }
653
654 static struct glink_core_rx_intent *
655 qcom_glink_alloc_intent(struct qcom_glink *glink,
656                         struct glink_channel *channel,
657                         size_t size,
658                         bool reuseable)
659 {
660         struct glink_core_rx_intent *intent;
661         int ret;
662         unsigned long flags;
663
664         intent = kzalloc(sizeof(*intent), GFP_KERNEL);
665         if (!intent)
666                 return NULL;
667
668         intent->data = kzalloc(size, GFP_KERNEL);
669         if (!intent->data)
670                 goto free_intent;
671
672         spin_lock_irqsave(&channel->intent_lock, flags);
673         ret = idr_alloc_cyclic(&channel->liids, intent, 1, -1, GFP_ATOMIC);
674         if (ret < 0) {
675                 spin_unlock_irqrestore(&channel->intent_lock, flags);
676                 goto free_data;
677         }
678         spin_unlock_irqrestore(&channel->intent_lock, flags);
679
680         intent->id = ret;
681         intent->size = size;
682         intent->reuse = reuseable;
683
684         return intent;
685
686 free_data:
687         kfree(intent->data);
688 free_intent:
689         kfree(intent);
690         return NULL;
691 }
692
693 static void qcom_glink_handle_rx_done(struct qcom_glink *glink,
694                                       u32 cid, uint32_t iid,
695                                       bool reuse)
696 {
697         struct glink_core_rx_intent *intent;
698         struct glink_channel *channel;
699         unsigned long flags;
700
701         spin_lock_irqsave(&glink->idr_lock, flags);
702         channel = idr_find(&glink->rcids, cid);
703         spin_unlock_irqrestore(&glink->idr_lock, flags);
704         if (!channel) {
705                 dev_err(glink->dev, "invalid channel id received\n");
706                 return;
707         }
708
709         spin_lock_irqsave(&channel->intent_lock, flags);
710         intent = idr_find(&channel->riids, iid);
711
712         if (!intent) {
713                 spin_unlock_irqrestore(&channel->intent_lock, flags);
714                 dev_err(glink->dev, "invalid intent id received\n");
715                 return;
716         }
717
718         intent->in_use = false;
719
720         if (!reuse) {
721                 idr_remove(&channel->riids, intent->id);
722                 kfree(intent);
723         }
724         spin_unlock_irqrestore(&channel->intent_lock, flags);
725 }
726
727 /**
728  * qcom_glink_handle_intent_req() - Receive a request for rx_intent
729  *                                          from remote side
730  * if_ptr:      Pointer to the transport interface
731  * rcid:        Remote channel ID
732  * size:        size of the intent
733  *
734  * The function searches for the local channel to which the request for
735  * rx_intent has arrived and allocates and notifies the remote back
736  */
737 static void qcom_glink_handle_intent_req(struct qcom_glink *glink,
738                                          u32 cid, size_t size)
739 {
740         struct glink_core_rx_intent *intent;
741         struct glink_channel *channel;
742         unsigned long flags;
743
744         spin_lock_irqsave(&glink->idr_lock, flags);
745         channel = idr_find(&glink->rcids, cid);
746         spin_unlock_irqrestore(&glink->idr_lock, flags);
747
748         if (!channel) {
749                 pr_err("%s channel not found for cid %d\n", __func__, cid);
750                 return;
751         }
752
753         intent = qcom_glink_alloc_intent(glink, channel, size, false);
754         if (intent)
755                 qcom_glink_advertise_intent(glink, channel, intent);
756
757         qcom_glink_send_intent_req_ack(glink, channel, !!intent);
758 }
759
760 static int qcom_glink_rx_defer(struct qcom_glink *glink, size_t extra)
761 {
762         struct glink_defer_cmd *dcmd;
763
764         extra = ALIGN(extra, 8);
765
766         if (qcom_glink_rx_avail(glink) < sizeof(struct glink_msg) + extra) {
767                 dev_dbg(glink->dev, "Insufficient data in rx fifo");
768                 return -ENXIO;
769         }
770
771         dcmd = kzalloc(sizeof(*dcmd) + extra, GFP_ATOMIC);
772         if (!dcmd)
773                 return -ENOMEM;
774
775         INIT_LIST_HEAD(&dcmd->node);
776
777         qcom_glink_rx_peak(glink, &dcmd->msg, 0, sizeof(dcmd->msg) + extra);
778
779         spin_lock(&glink->rx_lock);
780         list_add_tail(&dcmd->node, &glink->rx_queue);
781         spin_unlock(&glink->rx_lock);
782
783         schedule_work(&glink->rx_work);
784         qcom_glink_rx_advance(glink, sizeof(dcmd->msg) + extra);
785
786         return 0;
787 }
788
789 static int qcom_glink_rx_data(struct qcom_glink *glink, size_t avail)
790 {
791         struct glink_core_rx_intent *intent;
792         struct glink_channel *channel;
793         struct {
794                 struct glink_msg msg;
795                 __le32 chunk_size;
796                 __le32 left_size;
797         } __packed hdr;
798         unsigned int chunk_size;
799         unsigned int left_size;
800         unsigned int rcid;
801         unsigned int liid;
802         int ret = 0;
803         unsigned long flags;
804
805         if (avail < sizeof(hdr)) {
806                 dev_dbg(glink->dev, "Not enough data in fifo\n");
807                 return -EAGAIN;
808         }
809
810         qcom_glink_rx_peak(glink, &hdr, 0, sizeof(hdr));
811         chunk_size = le32_to_cpu(hdr.chunk_size);
812         left_size = le32_to_cpu(hdr.left_size);
813
814         if (avail < sizeof(hdr) + chunk_size) {
815                 dev_dbg(glink->dev, "Payload not yet in fifo\n");
816                 return -EAGAIN;
817         }
818
819         rcid = le16_to_cpu(hdr.msg.param1);
820         spin_lock_irqsave(&glink->idr_lock, flags);
821         channel = idr_find(&glink->rcids, rcid);
822         spin_unlock_irqrestore(&glink->idr_lock, flags);
823         if (!channel) {
824                 dev_dbg(glink->dev, "Data on non-existing channel\n");
825
826                 /* Drop the message */
827                 goto advance_rx;
828         }
829
830         if (glink->intentless) {
831                 /* Might have an ongoing, fragmented, message to append */
832                 if (!channel->buf) {
833                         intent = kzalloc(sizeof(*intent), GFP_ATOMIC);
834                         if (!intent)
835                                 return -ENOMEM;
836
837                         intent->data = kmalloc(chunk_size + left_size,
838                                                GFP_ATOMIC);
839                         if (!intent->data) {
840                                 kfree(intent);
841                                 return -ENOMEM;
842                         }
843
844                         intent->id = 0xbabababa;
845                         intent->size = chunk_size + left_size;
846                         intent->offset = 0;
847
848                         channel->buf = intent;
849                 } else {
850                         intent = channel->buf;
851                 }
852         } else {
853                 liid = le32_to_cpu(hdr.msg.param2);
854
855                 spin_lock_irqsave(&channel->intent_lock, flags);
856                 intent = idr_find(&channel->liids, liid);
857                 spin_unlock_irqrestore(&channel->intent_lock, flags);
858
859                 if (!intent) {
860                         dev_err(glink->dev,
861                                 "no intent found for channel %s intent %d",
862                                 channel->name, liid);
863                         ret = -ENOENT;
864                         goto advance_rx;
865                 }
866         }
867
868         if (intent->size - intent->offset < chunk_size) {
869                 dev_err(glink->dev, "Insufficient space in intent\n");
870
871                 /* The packet header lied, drop payload */
872                 goto advance_rx;
873         }
874
875         qcom_glink_rx_peak(glink, intent->data + intent->offset,
876                            sizeof(hdr), chunk_size);
877         intent->offset += chunk_size;
878
879         /* Handle message when no fragments remain to be received */
880         if (!left_size) {
881                 spin_lock(&channel->recv_lock);
882                 if (channel->ept.cb) {
883                         channel->ept.cb(channel->ept.rpdev,
884                                         intent->data,
885                                         intent->offset,
886                                         channel->ept.priv,
887                                         RPMSG_ADDR_ANY);
888                 }
889                 spin_unlock(&channel->recv_lock);
890
891                 intent->offset = 0;
892                 channel->buf = NULL;
893
894                 qcom_glink_rx_done(glink, channel, intent);
895         }
896
897 advance_rx:
898         qcom_glink_rx_advance(glink, ALIGN(sizeof(hdr) + chunk_size, 8));
899
900         return ret;
901 }
902
903 static void qcom_glink_handle_intent(struct qcom_glink *glink,
904                                      unsigned int cid,
905                                      unsigned int count,
906                                      size_t avail)
907 {
908         struct glink_core_rx_intent *intent;
909         struct glink_channel *channel;
910         struct intent_pair {
911                 __le32 size;
912                 __le32 iid;
913         };
914
915         struct {
916                 struct glink_msg msg;
917                 struct intent_pair intents[];
918         } __packed * msg;
919
920         const size_t msglen = sizeof(*msg) + sizeof(struct intent_pair) * count;
921         int ret;
922         int i;
923         unsigned long flags;
924
925         if (avail < msglen) {
926                 dev_dbg(glink->dev, "Not enough data in fifo\n");
927                 return;
928         }
929
930         spin_lock_irqsave(&glink->idr_lock, flags);
931         channel = idr_find(&glink->rcids, cid);
932         spin_unlock_irqrestore(&glink->idr_lock, flags);
933         if (!channel) {
934                 dev_err(glink->dev, "intents for non-existing channel\n");
935                 qcom_glink_rx_advance(glink, ALIGN(msglen, 8));
936                 return;
937         }
938
939         msg = kmalloc(msglen, GFP_ATOMIC);
940         if (!msg)
941                 return;
942
943         qcom_glink_rx_peak(glink, msg, 0, msglen);
944
945         for (i = 0; i < count; ++i) {
946                 intent = kzalloc(sizeof(*intent), GFP_ATOMIC);
947                 if (!intent)
948                         break;
949
950                 intent->id = le32_to_cpu(msg->intents[i].iid);
951                 intent->size = le32_to_cpu(msg->intents[i].size);
952
953                 spin_lock_irqsave(&channel->intent_lock, flags);
954                 ret = idr_alloc(&channel->riids, intent,
955                                 intent->id, intent->id + 1, GFP_ATOMIC);
956                 spin_unlock_irqrestore(&channel->intent_lock, flags);
957
958                 if (ret < 0)
959                         dev_err(glink->dev, "failed to store remote intent\n");
960         }
961
962         kfree(msg);
963         qcom_glink_rx_advance(glink, ALIGN(msglen, 8));
964 }
965
966 static int qcom_glink_rx_open_ack(struct qcom_glink *glink, unsigned int lcid)
967 {
968         struct glink_channel *channel;
969
970         spin_lock(&glink->idr_lock);
971         channel = idr_find(&glink->lcids, lcid);
972         spin_unlock(&glink->idr_lock);
973         if (!channel) {
974                 dev_err(glink->dev, "Invalid open ack packet\n");
975                 return -EINVAL;
976         }
977
978         complete(&channel->open_ack);
979
980         return 0;
981 }
982
983 static irqreturn_t qcom_glink_native_intr(int irq, void *data)
984 {
985         struct qcom_glink *glink = data;
986         struct glink_msg msg;
987         unsigned int param1;
988         unsigned int param2;
989         unsigned int avail;
990         unsigned int cmd;
991         int ret = 0;
992
993         for (;;) {
994                 avail = qcom_glink_rx_avail(glink);
995                 if (avail < sizeof(msg))
996                         break;
997
998                 qcom_glink_rx_peak(glink, &msg, 0, sizeof(msg));
999
1000                 cmd = le16_to_cpu(msg.cmd);
1001                 param1 = le16_to_cpu(msg.param1);
1002                 param2 = le32_to_cpu(msg.param2);
1003
1004                 switch (cmd) {
1005                 case RPM_CMD_VERSION:
1006                 case RPM_CMD_VERSION_ACK:
1007                 case RPM_CMD_CLOSE:
1008                 case RPM_CMD_CLOSE_ACK:
1009                 case RPM_CMD_RX_INTENT_REQ:
1010                         ret = qcom_glink_rx_defer(glink, 0);
1011                         break;
1012                 case RPM_CMD_OPEN_ACK:
1013                         ret = qcom_glink_rx_open_ack(glink, param1);
1014                         qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1015                         break;
1016                 case RPM_CMD_OPEN:
1017                         ret = qcom_glink_rx_defer(glink, param2);
1018                         break;
1019                 case RPM_CMD_TX_DATA:
1020                 case RPM_CMD_TX_DATA_CONT:
1021                         ret = qcom_glink_rx_data(glink, avail);
1022                         break;
1023                 case RPM_CMD_READ_NOTIF:
1024                         qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1025
1026                         mbox_send_message(glink->mbox_chan, NULL);
1027                         mbox_client_txdone(glink->mbox_chan, 0);
1028                         break;
1029                 case RPM_CMD_INTENT:
1030                         qcom_glink_handle_intent(glink, param1, param2, avail);
1031                         break;
1032                 case RPM_CMD_RX_DONE:
1033                         qcom_glink_handle_rx_done(glink, param1, param2, false);
1034                         qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1035                         break;
1036                 case RPM_CMD_RX_DONE_W_REUSE:
1037                         qcom_glink_handle_rx_done(glink, param1, param2, true);
1038                         qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1039                         break;
1040                 case RPM_CMD_RX_INTENT_REQ_ACK:
1041                         qcom_glink_handle_intent_req_ack(glink, param1, param2);
1042                         qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1043                         break;
1044                 default:
1045                         dev_err(glink->dev, "unhandled rx cmd: %d\n", cmd);
1046                         ret = -EINVAL;
1047                         break;
1048                 }
1049
1050                 if (ret)
1051                         break;
1052         }
1053
1054         return IRQ_HANDLED;
1055 }
1056
1057 /* Locally initiated rpmsg_create_ept */
1058 static struct glink_channel *qcom_glink_create_local(struct qcom_glink *glink,
1059                                                      const char *name)
1060 {
1061         struct glink_channel *channel;
1062         int ret;
1063         unsigned long flags;
1064
1065         channel = qcom_glink_alloc_channel(glink, name);
1066         if (IS_ERR(channel))
1067                 return ERR_CAST(channel);
1068
1069         ret = qcom_glink_send_open_req(glink, channel);
1070         if (ret)
1071                 goto release_channel;
1072
1073         ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
1074         if (!ret)
1075                 goto err_timeout;
1076
1077         ret = wait_for_completion_timeout(&channel->open_req, 5 * HZ);
1078         if (!ret)
1079                 goto err_timeout;
1080
1081         qcom_glink_send_open_ack(glink, channel);
1082
1083         return channel;
1084
1085 err_timeout:
1086         /* qcom_glink_send_open_req() did register the channel in lcids*/
1087         spin_lock_irqsave(&glink->idr_lock, flags);
1088         idr_remove(&glink->lcids, channel->lcid);
1089         spin_unlock_irqrestore(&glink->idr_lock, flags);
1090
1091 release_channel:
1092         /* Release qcom_glink_send_open_req() reference */
1093         kref_put(&channel->refcount, qcom_glink_channel_release);
1094         /* Release qcom_glink_alloc_channel() reference */
1095         kref_put(&channel->refcount, qcom_glink_channel_release);
1096
1097         return ERR_PTR(-ETIMEDOUT);
1098 }
1099
1100 /* Remote initiated rpmsg_create_ept */
1101 static int qcom_glink_create_remote(struct qcom_glink *glink,
1102                                     struct glink_channel *channel)
1103 {
1104         int ret;
1105
1106         qcom_glink_send_open_ack(glink, channel);
1107
1108         ret = qcom_glink_send_open_req(glink, channel);
1109         if (ret)
1110                 goto close_link;
1111
1112         ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
1113         if (!ret) {
1114                 ret = -ETIMEDOUT;
1115                 goto close_link;
1116         }
1117
1118         return 0;
1119
1120 close_link:
1121         /*
1122          * Send a close request to "undo" our open-ack. The close-ack will
1123          * release qcom_glink_send_open_req() reference and the last reference
1124          * will be relesed after receiving remote_close or transport unregister
1125          * by calling qcom_glink_native_remove().
1126          */
1127         qcom_glink_send_close_req(glink, channel);
1128
1129         return ret;
1130 }
1131
1132 static struct rpmsg_endpoint *qcom_glink_create_ept(struct rpmsg_device *rpdev,
1133                                                     rpmsg_rx_cb_t cb,
1134                                                     void *priv,
1135                                                     struct rpmsg_channel_info
1136                                                                         chinfo)
1137 {
1138         struct glink_channel *parent = to_glink_channel(rpdev->ept);
1139         struct glink_channel *channel;
1140         struct qcom_glink *glink = parent->glink;
1141         struct rpmsg_endpoint *ept;
1142         const char *name = chinfo.name;
1143         int cid;
1144         int ret;
1145         unsigned long flags;
1146
1147         spin_lock_irqsave(&glink->idr_lock, flags);
1148         idr_for_each_entry(&glink->rcids, channel, cid) {
1149                 if (!strcmp(channel->name, name))
1150                         break;
1151         }
1152         spin_unlock_irqrestore(&glink->idr_lock, flags);
1153
1154         if (!channel) {
1155                 channel = qcom_glink_create_local(glink, name);
1156                 if (IS_ERR(channel))
1157                         return NULL;
1158         } else {
1159                 ret = qcom_glink_create_remote(glink, channel);
1160                 if (ret)
1161                         return NULL;
1162         }
1163
1164         ept = &channel->ept;
1165         ept->rpdev = rpdev;
1166         ept->cb = cb;
1167         ept->priv = priv;
1168         ept->ops = &glink_endpoint_ops;
1169
1170         return ept;
1171 }
1172
1173 static int qcom_glink_announce_create(struct rpmsg_device *rpdev)
1174 {
1175         struct glink_channel *channel = to_glink_channel(rpdev->ept);
1176         struct glink_core_rx_intent *intent;
1177         struct qcom_glink *glink = channel->glink;
1178         int num_intents = glink->intentless ? 0 : 5;
1179
1180         /* Channel is now open, advertise base set of intents */
1181         while (num_intents--) {
1182                 intent = qcom_glink_alloc_intent(glink, channel, SZ_1K, true);
1183                 if (!intent)
1184                         break;
1185
1186                 qcom_glink_advertise_intent(glink, channel, intent);
1187         }
1188
1189         return 0;
1190 }
1191
1192 static void qcom_glink_destroy_ept(struct rpmsg_endpoint *ept)
1193 {
1194         struct glink_channel *channel = to_glink_channel(ept);
1195         struct qcom_glink *glink = channel->glink;
1196         unsigned long flags;
1197
1198         spin_lock_irqsave(&channel->recv_lock, flags);
1199         channel->ept.cb = NULL;
1200         spin_unlock_irqrestore(&channel->recv_lock, flags);
1201
1202         /* Decouple the potential rpdev from the channel */
1203         channel->rpdev = NULL;
1204
1205         qcom_glink_send_close_req(glink, channel);
1206 }
1207
1208 static int qcom_glink_request_intent(struct qcom_glink *glink,
1209                                      struct glink_channel *channel,
1210                                      size_t size)
1211 {
1212         struct {
1213                 u16 id;
1214                 u16 cid;
1215                 u32 size;
1216         } __packed cmd;
1217
1218         int ret;
1219
1220         mutex_lock(&channel->intent_req_lock);
1221
1222         reinit_completion(&channel->intent_req_comp);
1223
1224         cmd.id = RPM_CMD_RX_INTENT_REQ;
1225         cmd.cid = channel->lcid;
1226         cmd.size = size;
1227
1228         ret = qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
1229         if (ret)
1230                 goto unlock;
1231
1232         ret = wait_for_completion_timeout(&channel->intent_req_comp, 10 * HZ);
1233         if (!ret) {
1234                 dev_err(glink->dev, "intent request timed out\n");
1235                 ret = -ETIMEDOUT;
1236         } else {
1237                 ret = channel->intent_req_result ? 0 : -ECANCELED;
1238         }
1239
1240 unlock:
1241         mutex_unlock(&channel->intent_req_lock);
1242         return ret;
1243 }
1244
1245 static int __qcom_glink_send(struct glink_channel *channel,
1246                              void *data, int len, bool wait)
1247 {
1248         struct qcom_glink *glink = channel->glink;
1249         struct glink_core_rx_intent *intent = NULL;
1250         struct glink_core_rx_intent *tmp;
1251         int iid = 0;
1252         struct {
1253                 struct glink_msg msg;
1254                 __le32 chunk_size;
1255                 __le32 left_size;
1256         } __packed req;
1257         int ret;
1258         unsigned long flags;
1259
1260         if (!glink->intentless) {
1261                 while (!intent) {
1262                         spin_lock_irqsave(&channel->intent_lock, flags);
1263                         idr_for_each_entry(&channel->riids, tmp, iid) {
1264                                 if (tmp->size >= len && !tmp->in_use) {
1265                                         tmp->in_use = true;
1266                                         intent = tmp;
1267                                         break;
1268                                 }
1269                         }
1270                         spin_unlock_irqrestore(&channel->intent_lock, flags);
1271
1272                         /* We found an available intent */
1273                         if (intent)
1274                                 break;
1275
1276                         if (!wait)
1277                                 return -EBUSY;
1278
1279                         ret = qcom_glink_request_intent(glink, channel, len);
1280                         if (ret < 0)
1281                                 return ret;
1282                 }
1283
1284                 iid = intent->id;
1285         }
1286
1287         req.msg.cmd = cpu_to_le16(RPM_CMD_TX_DATA);
1288         req.msg.param1 = cpu_to_le16(channel->lcid);
1289         req.msg.param2 = cpu_to_le32(iid);
1290         req.chunk_size = cpu_to_le32(len);
1291         req.left_size = cpu_to_le32(0);
1292
1293         ret = qcom_glink_tx(glink, &req, sizeof(req), data, len, wait);
1294
1295         /* Mark intent available if we failed */
1296         if (ret && intent)
1297                 intent->in_use = false;
1298
1299         return ret;
1300 }
1301
1302 static int qcom_glink_send(struct rpmsg_endpoint *ept, void *data, int len)
1303 {
1304         struct glink_channel *channel = to_glink_channel(ept);
1305
1306         return __qcom_glink_send(channel, data, len, true);
1307 }
1308
1309 static int qcom_glink_trysend(struct rpmsg_endpoint *ept, void *data, int len)
1310 {
1311         struct glink_channel *channel = to_glink_channel(ept);
1312
1313         return __qcom_glink_send(channel, data, len, false);
1314 }
1315
1316 /*
1317  * Finds the device_node for the glink child interested in this channel.
1318  */
1319 static struct device_node *qcom_glink_match_channel(struct device_node *node,
1320                                                     const char *channel)
1321 {
1322         struct device_node *child;
1323         const char *name;
1324         const char *key;
1325         int ret;
1326
1327         for_each_available_child_of_node(node, child) {
1328                 key = "qcom,glink-channels";
1329                 ret = of_property_read_string(child, key, &name);
1330                 if (ret)
1331                         continue;
1332
1333                 if (strcmp(name, channel) == 0)
1334                         return child;
1335         }
1336
1337         return NULL;
1338 }
1339
1340 static const struct rpmsg_device_ops glink_device_ops = {
1341         .create_ept = qcom_glink_create_ept,
1342         .announce_create = qcom_glink_announce_create,
1343 };
1344
1345 static const struct rpmsg_endpoint_ops glink_endpoint_ops = {
1346         .destroy_ept = qcom_glink_destroy_ept,
1347         .send = qcom_glink_send,
1348         .trysend = qcom_glink_trysend,
1349 };
1350
1351 static void qcom_glink_rpdev_release(struct device *dev)
1352 {
1353         struct rpmsg_device *rpdev = to_rpmsg_device(dev);
1354         struct glink_channel *channel = to_glink_channel(rpdev->ept);
1355
1356         channel->rpdev = NULL;
1357         kfree(rpdev->driver_override);
1358         kfree(rpdev);
1359 }
1360
1361 static int qcom_glink_rx_open(struct qcom_glink *glink, unsigned int rcid,
1362                               char *name)
1363 {
1364         struct glink_channel *channel;
1365         struct rpmsg_device *rpdev;
1366         bool create_device = false;
1367         struct device_node *node;
1368         int lcid;
1369         int ret;
1370         unsigned long flags;
1371
1372         spin_lock_irqsave(&glink->idr_lock, flags);
1373         idr_for_each_entry(&glink->lcids, channel, lcid) {
1374                 if (!strcmp(channel->name, name))
1375                         break;
1376         }
1377         spin_unlock_irqrestore(&glink->idr_lock, flags);
1378
1379         if (!channel) {
1380                 channel = qcom_glink_alloc_channel(glink, name);
1381                 if (IS_ERR(channel))
1382                         return PTR_ERR(channel);
1383
1384                 /* The opening dance was initiated by the remote */
1385                 create_device = true;
1386         }
1387
1388         spin_lock_irqsave(&glink->idr_lock, flags);
1389         ret = idr_alloc(&glink->rcids, channel, rcid, rcid + 1, GFP_ATOMIC);
1390         if (ret < 0) {
1391                 dev_err(glink->dev, "Unable to insert channel into rcid list\n");
1392                 spin_unlock_irqrestore(&glink->idr_lock, flags);
1393                 goto free_channel;
1394         }
1395         channel->rcid = ret;
1396         spin_unlock_irqrestore(&glink->idr_lock, flags);
1397
1398         complete(&channel->open_req);
1399
1400         if (create_device) {
1401                 rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL);
1402                 if (!rpdev) {
1403                         ret = -ENOMEM;
1404                         goto rcid_remove;
1405                 }
1406
1407                 rpdev->ept = &channel->ept;
1408                 strncpy(rpdev->id.name, name, RPMSG_NAME_SIZE);
1409                 rpdev->src = RPMSG_ADDR_ANY;
1410                 rpdev->dst = RPMSG_ADDR_ANY;
1411                 rpdev->ops = &glink_device_ops;
1412
1413                 node = qcom_glink_match_channel(glink->dev->of_node, name);
1414                 rpdev->dev.of_node = node;
1415                 rpdev->dev.parent = glink->dev;
1416                 rpdev->dev.release = qcom_glink_rpdev_release;
1417
1418                 ret = rpmsg_register_device(rpdev);
1419                 if (ret)
1420                         goto rcid_remove;
1421
1422                 channel->rpdev = rpdev;
1423         }
1424
1425         return 0;
1426
1427 rcid_remove:
1428         spin_lock_irqsave(&glink->idr_lock, flags);
1429         idr_remove(&glink->rcids, channel->rcid);
1430         channel->rcid = 0;
1431         spin_unlock_irqrestore(&glink->idr_lock, flags);
1432 free_channel:
1433         /* Release the reference, iff we took it */
1434         if (create_device)
1435                 kref_put(&channel->refcount, qcom_glink_channel_release);
1436
1437         return ret;
1438 }
1439
1440 static void qcom_glink_rx_close(struct qcom_glink *glink, unsigned int rcid)
1441 {
1442         struct rpmsg_channel_info chinfo;
1443         struct glink_channel *channel;
1444         unsigned long flags;
1445
1446         spin_lock_irqsave(&glink->idr_lock, flags);
1447         channel = idr_find(&glink->rcids, rcid);
1448         spin_unlock_irqrestore(&glink->idr_lock, flags);
1449         if (WARN(!channel, "close request on unknown channel\n"))
1450                 return;
1451
1452         /* cancel pending rx_done work */
1453         cancel_work_sync(&channel->intent_work);
1454
1455         if (channel->rpdev) {
1456                 strscpy_pad(chinfo.name, channel->name, sizeof(chinfo.name));
1457                 chinfo.src = RPMSG_ADDR_ANY;
1458                 chinfo.dst = RPMSG_ADDR_ANY;
1459
1460                 rpmsg_unregister_device(glink->dev, &chinfo);
1461         }
1462
1463         qcom_glink_send_close_ack(glink, channel->rcid);
1464
1465         spin_lock_irqsave(&glink->idr_lock, flags);
1466         idr_remove(&glink->rcids, channel->rcid);
1467         channel->rcid = 0;
1468         spin_unlock_irqrestore(&glink->idr_lock, flags);
1469
1470         kref_put(&channel->refcount, qcom_glink_channel_release);
1471 }
1472
1473 static void qcom_glink_rx_close_ack(struct qcom_glink *glink, unsigned int lcid)
1474 {
1475         struct glink_channel *channel;
1476         unsigned long flags;
1477
1478         spin_lock_irqsave(&glink->idr_lock, flags);
1479         channel = idr_find(&glink->lcids, lcid);
1480         if (WARN(!channel, "close ack on unknown channel\n")) {
1481                 spin_unlock_irqrestore(&glink->idr_lock, flags);
1482                 return;
1483         }
1484
1485         idr_remove(&glink->lcids, channel->lcid);
1486         channel->lcid = 0;
1487         spin_unlock_irqrestore(&glink->idr_lock, flags);
1488
1489         kref_put(&channel->refcount, qcom_glink_channel_release);
1490 }
1491
1492 static void qcom_glink_work(struct work_struct *work)
1493 {
1494         struct qcom_glink *glink = container_of(work, struct qcom_glink,
1495                                                 rx_work);
1496         struct glink_defer_cmd *dcmd;
1497         struct glink_msg *msg;
1498         unsigned long flags;
1499         unsigned int param1;
1500         unsigned int param2;
1501         unsigned int cmd;
1502
1503         for (;;) {
1504                 spin_lock_irqsave(&glink->rx_lock, flags);
1505                 if (list_empty(&glink->rx_queue)) {
1506                         spin_unlock_irqrestore(&glink->rx_lock, flags);
1507                         break;
1508                 }
1509                 dcmd = list_first_entry(&glink->rx_queue,
1510                                         struct glink_defer_cmd, node);
1511                 list_del(&dcmd->node);
1512                 spin_unlock_irqrestore(&glink->rx_lock, flags);
1513
1514                 msg = &dcmd->msg;
1515                 cmd = le16_to_cpu(msg->cmd);
1516                 param1 = le16_to_cpu(msg->param1);
1517                 param2 = le32_to_cpu(msg->param2);
1518
1519                 switch (cmd) {
1520                 case RPM_CMD_VERSION:
1521                         qcom_glink_receive_version(glink, param1, param2);
1522                         break;
1523                 case RPM_CMD_VERSION_ACK:
1524                         qcom_glink_receive_version_ack(glink, param1, param2);
1525                         break;
1526                 case RPM_CMD_OPEN:
1527                         qcom_glink_rx_open(glink, param1, msg->data);
1528                         break;
1529                 case RPM_CMD_CLOSE:
1530                         qcom_glink_rx_close(glink, param1);
1531                         break;
1532                 case RPM_CMD_CLOSE_ACK:
1533                         qcom_glink_rx_close_ack(glink, param1);
1534                         break;
1535                 case RPM_CMD_RX_INTENT_REQ:
1536                         qcom_glink_handle_intent_req(glink, param1, param2);
1537                         break;
1538                 default:
1539                         WARN(1, "Unknown defer object %d\n", cmd);
1540                         break;
1541                 }
1542
1543                 kfree(dcmd);
1544         }
1545 }
1546
1547 static void qcom_glink_cancel_rx_work(struct qcom_glink *glink)
1548 {
1549         struct glink_defer_cmd *dcmd;
1550         struct glink_defer_cmd *tmp;
1551
1552         /* cancel any pending deferred rx_work */
1553         cancel_work_sync(&glink->rx_work);
1554
1555         list_for_each_entry_safe(dcmd, tmp, &glink->rx_queue, node)
1556                 kfree(dcmd);
1557 }
1558
1559 struct qcom_glink *qcom_glink_native_probe(struct device *dev,
1560                                            unsigned long features,
1561                                            struct qcom_glink_pipe *rx,
1562                                            struct qcom_glink_pipe *tx,
1563                                            bool intentless)
1564 {
1565         int irq;
1566         int ret;
1567         struct qcom_glink *glink;
1568
1569         glink = devm_kzalloc(dev, sizeof(*glink), GFP_KERNEL);
1570         if (!glink)
1571                 return ERR_PTR(-ENOMEM);
1572
1573         glink->dev = dev;
1574         glink->tx_pipe = tx;
1575         glink->rx_pipe = rx;
1576
1577         glink->features = features;
1578         glink->intentless = intentless;
1579
1580         mutex_init(&glink->tx_lock);
1581         spin_lock_init(&glink->rx_lock);
1582         INIT_LIST_HEAD(&glink->rx_queue);
1583         INIT_WORK(&glink->rx_work, qcom_glink_work);
1584
1585         spin_lock_init(&glink->idr_lock);
1586         idr_init(&glink->lcids);
1587         idr_init(&glink->rcids);
1588
1589         glink->mbox_client.dev = dev;
1590         glink->mbox_chan = mbox_request_channel(&glink->mbox_client, 0);
1591         if (IS_ERR(glink->mbox_chan)) {
1592                 if (PTR_ERR(glink->mbox_chan) != -EPROBE_DEFER)
1593                         dev_err(dev, "failed to acquire IPC channel\n");
1594                 return ERR_CAST(glink->mbox_chan);
1595         }
1596
1597         irq = of_irq_get(dev->of_node, 0);
1598         ret = devm_request_irq(dev, irq,
1599                                qcom_glink_native_intr,
1600                                IRQF_NO_SUSPEND | IRQF_SHARED,
1601                                "glink-native", glink);
1602         if (ret) {
1603                 dev_err(dev, "failed to request IRQ\n");
1604                 return ERR_PTR(ret);
1605         }
1606
1607         glink->irq = irq;
1608
1609         ret = qcom_glink_send_version(glink);
1610         if (ret)
1611                 return ERR_PTR(ret);
1612
1613         return glink;
1614 }
1615 EXPORT_SYMBOL_GPL(qcom_glink_native_probe);
1616
1617 static int qcom_glink_remove_device(struct device *dev, void *data)
1618 {
1619         device_unregister(dev);
1620
1621         return 0;
1622 }
1623
1624 void qcom_glink_native_remove(struct qcom_glink *glink)
1625 {
1626         struct glink_channel *channel;
1627         int cid;
1628         int ret;
1629
1630         disable_irq(glink->irq);
1631         qcom_glink_cancel_rx_work(glink);
1632
1633         ret = device_for_each_child(glink->dev, NULL, qcom_glink_remove_device);
1634         if (ret)
1635                 dev_warn(glink->dev, "Can't remove GLINK devices: %d\n", ret);
1636
1637         /* Release any defunct local channels, waiting for close-ack */
1638         idr_for_each_entry(&glink->lcids, channel, cid)
1639                 kref_put(&channel->refcount, qcom_glink_channel_release);
1640
1641         /* Release any defunct local channels, waiting for close-req */
1642         idr_for_each_entry(&glink->rcids, channel, cid)
1643                 kref_put(&channel->refcount, qcom_glink_channel_release);
1644
1645         idr_destroy(&glink->lcids);
1646         idr_destroy(&glink->rcids);
1647         mbox_free_channel(glink->mbox_chan);
1648 }
1649 EXPORT_SYMBOL_GPL(qcom_glink_native_remove);
1650
1651 void qcom_glink_native_unregister(struct qcom_glink *glink)
1652 {
1653         device_unregister(glink->dev);
1654 }
1655 EXPORT_SYMBOL_GPL(qcom_glink_native_unregister);
1656
1657 MODULE_DESCRIPTION("Qualcomm GLINK driver");
1658 MODULE_LICENSE("GPL v2");