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