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