Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (c) 2016-2017, Linaro Ltd
0004  */
0005 
0006 #include <linux/idr.h>
0007 #include <linux/interrupt.h>
0008 #include <linux/io.h>
0009 #include <linux/list.h>
0010 #include <linux/mfd/syscon.h>
0011 #include <linux/module.h>
0012 #include <linux/of.h>
0013 #include <linux/of_address.h>
0014 #include <linux/of_irq.h>
0015 #include <linux/platform_device.h>
0016 #include <linux/regmap.h>
0017 #include <linux/rpmsg.h>
0018 #include <linux/sizes.h>
0019 #include <linux/slab.h>
0020 #include <linux/workqueue.h>
0021 #include <linux/mailbox_client.h>
0022 
0023 #include "rpmsg_internal.h"
0024 #include "qcom_glink_native.h"
0025 
0026 #define GLINK_NAME_SIZE     32
0027 #define GLINK_VERSION_1     1
0028 
0029 #define RPM_GLINK_CID_MIN   1
0030 #define RPM_GLINK_CID_MAX   65536
0031 
0032 struct glink_msg {
0033     __le16 cmd;
0034     __le16 param1;
0035     __le32 param2;
0036     u8 data[];
0037 } __packed;
0038 
0039 /**
0040  * struct glink_defer_cmd - deferred incoming control message
0041  * @node:   list node
0042  * @msg:    message header
0043  * @data:   payload of the message
0044  *
0045  * Copy of a received control message, to be added to @rx_queue and processed
0046  * by @rx_work of @qcom_glink.
0047  */
0048 struct glink_defer_cmd {
0049     struct list_head node;
0050 
0051     struct glink_msg msg;
0052     u8 data[];
0053 };
0054 
0055 /**
0056  * struct glink_core_rx_intent - RX intent
0057  * RX intent
0058  *
0059  * @data: pointer to the data (may be NULL for zero-copy)
0060  * @id: remote or local intent ID
0061  * @size: size of the original intent (do not modify)
0062  * @reuse: To mark if the intent can be reused after first use
0063  * @in_use: To mark if intent is already in use for the channel
0064  * @offset: next write offset (initially 0)
0065  * @node:   list node
0066  */
0067 struct glink_core_rx_intent {
0068     void *data;
0069     u32 id;
0070     size_t size;
0071     bool reuse;
0072     bool in_use;
0073     u32 offset;
0074 
0075     struct list_head node;
0076 };
0077 
0078 /**
0079  * struct qcom_glink - driver context, relates to one remote subsystem
0080  * @dev:    reference to the associated struct device
0081  * @mbox_client: mailbox client
0082  * @mbox_chan:  mailbox channel
0083  * @rx_pipe:    pipe object for receive FIFO
0084  * @tx_pipe:    pipe object for transmit FIFO
0085  * @irq:    IRQ for signaling incoming events
0086  * @rx_work:    worker for handling received control messages
0087  * @rx_lock:    protects the @rx_queue
0088  * @rx_queue:   queue of received control messages to be processed in @rx_work
0089  * @tx_lock:    synchronizes operations on the tx fifo
0090  * @idr_lock:   synchronizes @lcids and @rcids modifications
0091  * @lcids:  idr of all channels with a known local channel id
0092  * @rcids:  idr of all channels with a known remote channel id
0093  * @features:   remote features
0094  * @intentless: flag to indicate that there is no intent
0095  * @tx_avail_notify: Waitqueue for pending tx tasks
0096  * @sent_read_notify: flag to check cmd sent or not
0097  */
0098 struct qcom_glink {
0099     struct device *dev;
0100 
0101     struct mbox_client mbox_client;
0102     struct mbox_chan *mbox_chan;
0103 
0104     struct qcom_glink_pipe *rx_pipe;
0105     struct qcom_glink_pipe *tx_pipe;
0106 
0107     int irq;
0108 
0109     struct work_struct rx_work;
0110     spinlock_t rx_lock;
0111     struct list_head rx_queue;
0112 
0113     spinlock_t tx_lock;
0114 
0115     spinlock_t idr_lock;
0116     struct idr lcids;
0117     struct idr rcids;
0118     unsigned long features;
0119 
0120     bool intentless;
0121     wait_queue_head_t tx_avail_notify;
0122     bool sent_read_notify;
0123 };
0124 
0125 enum {
0126     GLINK_STATE_CLOSED,
0127     GLINK_STATE_OPENING,
0128     GLINK_STATE_OPEN,
0129     GLINK_STATE_CLOSING,
0130 };
0131 
0132 /**
0133  * struct glink_channel - internal representation of a channel
0134  * @rpdev:  rpdev reference, only used for primary endpoints
0135  * @ept:    rpmsg endpoint this channel is associated with
0136  * @glink:  qcom_glink context handle
0137  * @refcount:   refcount for the channel object
0138  * @recv_lock:  guard for @ept.cb
0139  * @name:   unique channel name/identifier
0140  * @lcid:   channel id, in local space
0141  * @rcid:   channel id, in remote space
0142  * @intent_lock: lock for protection of @liids, @riids
0143  * @liids:  idr of all local intents
0144  * @riids:  idr of all remote intents
0145  * @intent_work: worker responsible for transmitting rx_done packets
0146  * @done_intents: list of intents that needs to be announced rx_done
0147  * @buf:    receive buffer, for gathering fragments
0148  * @buf_offset: write offset in @buf
0149  * @buf_size:   size of current @buf
0150  * @open_ack:   completed once remote has acked the open-request
0151  * @open_req:   completed once open-request has been received
0152  * @intent_req_lock: Synchronises multiple intent requests
0153  * @intent_req_result: Result of intent request
0154  * @intent_req_comp: Completion for intent_req signalling
0155  */
0156 struct glink_channel {
0157     struct rpmsg_endpoint ept;
0158 
0159     struct rpmsg_device *rpdev;
0160     struct qcom_glink *glink;
0161 
0162     struct kref refcount;
0163 
0164     spinlock_t recv_lock;
0165 
0166     char *name;
0167     unsigned int lcid;
0168     unsigned int rcid;
0169 
0170     spinlock_t intent_lock;
0171     struct idr liids;
0172     struct idr riids;
0173     struct work_struct intent_work;
0174     struct list_head done_intents;
0175 
0176     struct glink_core_rx_intent *buf;
0177     int buf_offset;
0178     int buf_size;
0179 
0180     struct completion open_ack;
0181     struct completion open_req;
0182 
0183     struct mutex intent_req_lock;
0184     bool intent_req_result;
0185     struct completion intent_req_comp;
0186 };
0187 
0188 #define to_glink_channel(_ept) container_of(_ept, struct glink_channel, ept)
0189 
0190 static const struct rpmsg_endpoint_ops glink_endpoint_ops;
0191 
0192 #define RPM_CMD_VERSION         0
0193 #define RPM_CMD_VERSION_ACK     1
0194 #define RPM_CMD_OPEN            2
0195 #define RPM_CMD_CLOSE           3
0196 #define RPM_CMD_OPEN_ACK        4
0197 #define RPM_CMD_INTENT          5
0198 #define RPM_CMD_RX_DONE         6
0199 #define RPM_CMD_RX_INTENT_REQ       7
0200 #define RPM_CMD_RX_INTENT_REQ_ACK   8
0201 #define RPM_CMD_TX_DATA         9
0202 #define RPM_CMD_CLOSE_ACK       11
0203 #define RPM_CMD_TX_DATA_CONT        12
0204 #define RPM_CMD_READ_NOTIF      13
0205 #define RPM_CMD_RX_DONE_W_REUSE     14
0206 
0207 #define GLINK_FEATURE_INTENTLESS    BIT(1)
0208 
0209 static void qcom_glink_rx_done_work(struct work_struct *work);
0210 
0211 static struct glink_channel *qcom_glink_alloc_channel(struct qcom_glink *glink,
0212                               const char *name)
0213 {
0214     struct glink_channel *channel;
0215 
0216     channel = kzalloc(sizeof(*channel), GFP_KERNEL);
0217     if (!channel)
0218         return ERR_PTR(-ENOMEM);
0219 
0220     /* Setup glink internal glink_channel data */
0221     spin_lock_init(&channel->recv_lock);
0222     spin_lock_init(&channel->intent_lock);
0223     mutex_init(&channel->intent_req_lock);
0224 
0225     channel->glink = glink;
0226     channel->name = kstrdup(name, GFP_KERNEL);
0227 
0228     init_completion(&channel->open_req);
0229     init_completion(&channel->open_ack);
0230     init_completion(&channel->intent_req_comp);
0231 
0232     INIT_LIST_HEAD(&channel->done_intents);
0233     INIT_WORK(&channel->intent_work, qcom_glink_rx_done_work);
0234 
0235     idr_init(&channel->liids);
0236     idr_init(&channel->riids);
0237     kref_init(&channel->refcount);
0238 
0239     return channel;
0240 }
0241 
0242 static void qcom_glink_channel_release(struct kref *ref)
0243 {
0244     struct glink_channel *channel = container_of(ref, struct glink_channel,
0245                              refcount);
0246     struct glink_core_rx_intent *intent;
0247     struct glink_core_rx_intent *tmp;
0248     unsigned long flags;
0249     int iid;
0250 
0251     /* cancel pending rx_done work */
0252     cancel_work_sync(&channel->intent_work);
0253 
0254     spin_lock_irqsave(&channel->intent_lock, flags);
0255     /* Free all non-reuse intents pending rx_done work */
0256     list_for_each_entry_safe(intent, tmp, &channel->done_intents, node) {
0257         if (!intent->reuse) {
0258             kfree(intent->data);
0259             kfree(intent);
0260         }
0261     }
0262 
0263     idr_for_each_entry(&channel->liids, tmp, iid) {
0264         kfree(tmp->data);
0265         kfree(tmp);
0266     }
0267     idr_destroy(&channel->liids);
0268 
0269     idr_for_each_entry(&channel->riids, tmp, iid)
0270         kfree(tmp);
0271     idr_destroy(&channel->riids);
0272     spin_unlock_irqrestore(&channel->intent_lock, flags);
0273 
0274     kfree(channel->name);
0275     kfree(channel);
0276 }
0277 
0278 static size_t qcom_glink_rx_avail(struct qcom_glink *glink)
0279 {
0280     return glink->rx_pipe->avail(glink->rx_pipe);
0281 }
0282 
0283 static void qcom_glink_rx_peak(struct qcom_glink *glink,
0284                    void *data, unsigned int offset, size_t count)
0285 {
0286     glink->rx_pipe->peak(glink->rx_pipe, data, offset, count);
0287 }
0288 
0289 static void qcom_glink_rx_advance(struct qcom_glink *glink, size_t count)
0290 {
0291     glink->rx_pipe->advance(glink->rx_pipe, count);
0292 }
0293 
0294 static size_t qcom_glink_tx_avail(struct qcom_glink *glink)
0295 {
0296     return glink->tx_pipe->avail(glink->tx_pipe);
0297 }
0298 
0299 static void qcom_glink_tx_write(struct qcom_glink *glink,
0300                 const void *hdr, size_t hlen,
0301                 const void *data, size_t dlen)
0302 {
0303     glink->tx_pipe->write(glink->tx_pipe, hdr, hlen, data, dlen);
0304 }
0305 
0306 static void qcom_glink_send_read_notify(struct qcom_glink *glink)
0307 {
0308     struct glink_msg msg;
0309 
0310     msg.cmd = cpu_to_le16(RPM_CMD_READ_NOTIF);
0311     msg.param1 = 0;
0312     msg.param2 = 0;
0313 
0314     qcom_glink_tx_write(glink, &msg, sizeof(msg), NULL, 0);
0315 
0316     mbox_send_message(glink->mbox_chan, NULL);
0317     mbox_client_txdone(glink->mbox_chan, 0);
0318 }
0319 
0320 static int qcom_glink_tx(struct qcom_glink *glink,
0321              const void *hdr, size_t hlen,
0322              const void *data, size_t dlen, bool wait)
0323 {
0324     unsigned int tlen = hlen + dlen;
0325     unsigned long flags;
0326     int ret = 0;
0327 
0328     /* Reject packets that are too big */
0329     if (tlen >= glink->tx_pipe->length)
0330         return -EINVAL;
0331 
0332     spin_lock_irqsave(&glink->tx_lock, flags);
0333 
0334     while (qcom_glink_tx_avail(glink) < tlen) {
0335         if (!wait) {
0336             ret = -EAGAIN;
0337             goto out;
0338         }
0339 
0340         if (!glink->sent_read_notify) {
0341             glink->sent_read_notify = true;
0342             qcom_glink_send_read_notify(glink);
0343         }
0344 
0345         /* Wait without holding the tx_lock */
0346         spin_unlock_irqrestore(&glink->tx_lock, flags);
0347 
0348         wait_event_timeout(glink->tx_avail_notify,
0349                    qcom_glink_tx_avail(glink) >= tlen, 10 * HZ);
0350 
0351         spin_lock_irqsave(&glink->tx_lock, flags);
0352 
0353         if (qcom_glink_tx_avail(glink) >= tlen)
0354             glink->sent_read_notify = false;
0355     }
0356 
0357     qcom_glink_tx_write(glink, hdr, hlen, data, dlen);
0358 
0359     mbox_send_message(glink->mbox_chan, NULL);
0360     mbox_client_txdone(glink->mbox_chan, 0);
0361 
0362 out:
0363     spin_unlock_irqrestore(&glink->tx_lock, flags);
0364 
0365     return ret;
0366 }
0367 
0368 static int qcom_glink_send_version(struct qcom_glink *glink)
0369 {
0370     struct glink_msg msg;
0371 
0372     msg.cmd = cpu_to_le16(RPM_CMD_VERSION);
0373     msg.param1 = cpu_to_le16(GLINK_VERSION_1);
0374     msg.param2 = cpu_to_le32(glink->features);
0375 
0376     return qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
0377 }
0378 
0379 static void qcom_glink_send_version_ack(struct qcom_glink *glink)
0380 {
0381     struct glink_msg msg;
0382 
0383     msg.cmd = cpu_to_le16(RPM_CMD_VERSION_ACK);
0384     msg.param1 = cpu_to_le16(GLINK_VERSION_1);
0385     msg.param2 = cpu_to_le32(glink->features);
0386 
0387     qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
0388 }
0389 
0390 static void qcom_glink_send_open_ack(struct qcom_glink *glink,
0391                      struct glink_channel *channel)
0392 {
0393     struct glink_msg msg;
0394 
0395     msg.cmd = cpu_to_le16(RPM_CMD_OPEN_ACK);
0396     msg.param1 = cpu_to_le16(channel->rcid);
0397     msg.param2 = cpu_to_le32(0);
0398 
0399     qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
0400 }
0401 
0402 static void qcom_glink_handle_intent_req_ack(struct qcom_glink *glink,
0403                          unsigned int cid, bool granted)
0404 {
0405     struct glink_channel *channel;
0406     unsigned long flags;
0407 
0408     spin_lock_irqsave(&glink->idr_lock, flags);
0409     channel = idr_find(&glink->rcids, cid);
0410     spin_unlock_irqrestore(&glink->idr_lock, flags);
0411     if (!channel) {
0412         dev_err(glink->dev, "unable to find channel\n");
0413         return;
0414     }
0415 
0416     channel->intent_req_result = granted;
0417     complete(&channel->intent_req_comp);
0418 }
0419 
0420 /**
0421  * qcom_glink_send_open_req() - send a RPM_CMD_OPEN request to the remote
0422  * @glink: Ptr to the glink edge
0423  * @channel: Ptr to the channel that the open req is sent
0424  *
0425  * Allocates a local channel id and sends a RPM_CMD_OPEN message to the remote.
0426  * Will return with refcount held, regardless of outcome.
0427  *
0428  * Return: 0 on success, negative errno otherwise.
0429  */
0430 static int qcom_glink_send_open_req(struct qcom_glink *glink,
0431                     struct glink_channel *channel)
0432 {
0433     struct {
0434         struct glink_msg msg;
0435         u8 name[GLINK_NAME_SIZE];
0436     } __packed req;
0437     int name_len = strlen(channel->name) + 1;
0438     int req_len = ALIGN(sizeof(req.msg) + name_len, 8);
0439     int ret;
0440     unsigned long flags;
0441 
0442     kref_get(&channel->refcount);
0443 
0444     spin_lock_irqsave(&glink->idr_lock, flags);
0445     ret = idr_alloc_cyclic(&glink->lcids, channel,
0446                    RPM_GLINK_CID_MIN, RPM_GLINK_CID_MAX,
0447                    GFP_ATOMIC);
0448     spin_unlock_irqrestore(&glink->idr_lock, flags);
0449     if (ret < 0)
0450         return ret;
0451 
0452     channel->lcid = ret;
0453 
0454     req.msg.cmd = cpu_to_le16(RPM_CMD_OPEN);
0455     req.msg.param1 = cpu_to_le16(channel->lcid);
0456     req.msg.param2 = cpu_to_le32(name_len);
0457     strcpy(req.name, channel->name);
0458 
0459     ret = qcom_glink_tx(glink, &req, req_len, NULL, 0, true);
0460     if (ret)
0461         goto remove_idr;
0462 
0463     return 0;
0464 
0465 remove_idr:
0466     spin_lock_irqsave(&glink->idr_lock, flags);
0467     idr_remove(&glink->lcids, channel->lcid);
0468     channel->lcid = 0;
0469     spin_unlock_irqrestore(&glink->idr_lock, flags);
0470 
0471     return ret;
0472 }
0473 
0474 static void qcom_glink_send_close_req(struct qcom_glink *glink,
0475                       struct glink_channel *channel)
0476 {
0477     struct glink_msg req;
0478 
0479     req.cmd = cpu_to_le16(RPM_CMD_CLOSE);
0480     req.param1 = cpu_to_le16(channel->lcid);
0481     req.param2 = 0;
0482 
0483     qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
0484 }
0485 
0486 static void qcom_glink_send_close_ack(struct qcom_glink *glink,
0487                       unsigned int rcid)
0488 {
0489     struct glink_msg req;
0490 
0491     req.cmd = cpu_to_le16(RPM_CMD_CLOSE_ACK);
0492     req.param1 = cpu_to_le16(rcid);
0493     req.param2 = 0;
0494 
0495     qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true);
0496 }
0497 
0498 static void qcom_glink_rx_done_work(struct work_struct *work)
0499 {
0500     struct glink_channel *channel = container_of(work, struct glink_channel,
0501                              intent_work);
0502     struct qcom_glink *glink = channel->glink;
0503     struct glink_core_rx_intent *intent, *tmp;
0504     struct {
0505         u16 id;
0506         u16 lcid;
0507         u32 liid;
0508     } __packed cmd;
0509 
0510     unsigned int cid = channel->lcid;
0511     unsigned int iid;
0512     bool reuse;
0513     unsigned long flags;
0514 
0515     spin_lock_irqsave(&channel->intent_lock, flags);
0516     list_for_each_entry_safe(intent, tmp, &channel->done_intents, node) {
0517         list_del(&intent->node);
0518         spin_unlock_irqrestore(&channel->intent_lock, flags);
0519         iid = intent->id;
0520         reuse = intent->reuse;
0521 
0522         cmd.id = reuse ? RPM_CMD_RX_DONE_W_REUSE : RPM_CMD_RX_DONE;
0523         cmd.lcid = cid;
0524         cmd.liid = iid;
0525 
0526         qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
0527         if (!reuse) {
0528             kfree(intent->data);
0529             kfree(intent);
0530         }
0531         spin_lock_irqsave(&channel->intent_lock, flags);
0532     }
0533     spin_unlock_irqrestore(&channel->intent_lock, flags);
0534 }
0535 
0536 static void qcom_glink_rx_done(struct qcom_glink *glink,
0537                    struct glink_channel *channel,
0538                    struct glink_core_rx_intent *intent)
0539 {
0540     /* We don't send RX_DONE to intentless systems */
0541     if (glink->intentless) {
0542         kfree(intent->data);
0543         kfree(intent);
0544         return;
0545     }
0546 
0547     /* Take it off the tree of receive intents */
0548     if (!intent->reuse) {
0549         spin_lock(&channel->intent_lock);
0550         idr_remove(&channel->liids, intent->id);
0551         spin_unlock(&channel->intent_lock);
0552     }
0553 
0554     /* Schedule the sending of a rx_done indication */
0555     spin_lock(&channel->intent_lock);
0556     list_add_tail(&intent->node, &channel->done_intents);
0557     spin_unlock(&channel->intent_lock);
0558 
0559     schedule_work(&channel->intent_work);
0560 }
0561 
0562 /**
0563  * qcom_glink_receive_version() - receive version/features from remote system
0564  *
0565  * @glink:  pointer to transport interface
0566  * @version:    remote version
0567  * @features:   remote features
0568  *
0569  * This function is called in response to a remote-initiated version/feature
0570  * negotiation sequence.
0571  */
0572 static void qcom_glink_receive_version(struct qcom_glink *glink,
0573                        u32 version,
0574                        u32 features)
0575 {
0576     switch (version) {
0577     case 0:
0578         break;
0579     case GLINK_VERSION_1:
0580         glink->features &= features;
0581         fallthrough;
0582     default:
0583         qcom_glink_send_version_ack(glink);
0584         break;
0585     }
0586 }
0587 
0588 /**
0589  * qcom_glink_receive_version_ack() - receive negotiation ack from remote system
0590  *
0591  * @glink:  pointer to transport interface
0592  * @version:    remote version response
0593  * @features:   remote features response
0594  *
0595  * This function is called in response to a local-initiated version/feature
0596  * negotiation sequence and is the counter-offer from the remote side based
0597  * upon the initial version and feature set requested.
0598  */
0599 static void qcom_glink_receive_version_ack(struct qcom_glink *glink,
0600                        u32 version,
0601                        u32 features)
0602 {
0603     switch (version) {
0604     case 0:
0605         /* Version negotiation failed */
0606         break;
0607     case GLINK_VERSION_1:
0608         if (features == glink->features)
0609             break;
0610 
0611         glink->features &= features;
0612         fallthrough;
0613     default:
0614         qcom_glink_send_version(glink);
0615         break;
0616     }
0617 }
0618 
0619 /**
0620  * qcom_glink_send_intent_req_ack() - convert an rx intent request ack cmd to
0621  *  wire format and transmit
0622  * @glink:  The transport to transmit on.
0623  * @channel:    The glink channel
0624  * @granted:    The request response to encode.
0625  *
0626  * Return: 0 on success or standard Linux error code.
0627  */
0628 static int qcom_glink_send_intent_req_ack(struct qcom_glink *glink,
0629                       struct glink_channel *channel,
0630                       bool granted)
0631 {
0632     struct glink_msg msg;
0633 
0634     msg.cmd = cpu_to_le16(RPM_CMD_RX_INTENT_REQ_ACK);
0635     msg.param1 = cpu_to_le16(channel->lcid);
0636     msg.param2 = cpu_to_le32(granted);
0637 
0638     qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true);
0639 
0640     return 0;
0641 }
0642 
0643 /**
0644  * qcom_glink_advertise_intent - convert an rx intent cmd to wire format and
0645  *             transmit
0646  * @glink:  The transport to transmit on.
0647  * @channel:    The local channel
0648  * @intent: The intent to pass on to remote.
0649  *
0650  * Return: 0 on success or standard Linux error code.
0651  */
0652 static int qcom_glink_advertise_intent(struct qcom_glink *glink,
0653                        struct glink_channel *channel,
0654                        struct glink_core_rx_intent *intent)
0655 {
0656     struct command {
0657         __le16 id;
0658         __le16 lcid;
0659         __le32 count;
0660         __le32 size;
0661         __le32 liid;
0662     } __packed;
0663     struct command cmd;
0664 
0665     cmd.id = cpu_to_le16(RPM_CMD_INTENT);
0666     cmd.lcid = cpu_to_le16(channel->lcid);
0667     cmd.count = cpu_to_le32(1);
0668     cmd.size = cpu_to_le32(intent->size);
0669     cmd.liid = cpu_to_le32(intent->id);
0670 
0671     qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
0672 
0673     return 0;
0674 }
0675 
0676 static struct glink_core_rx_intent *
0677 qcom_glink_alloc_intent(struct qcom_glink *glink,
0678             struct glink_channel *channel,
0679             size_t size,
0680             bool reuseable)
0681 {
0682     struct glink_core_rx_intent *intent;
0683     int ret;
0684     unsigned long flags;
0685 
0686     intent = kzalloc(sizeof(*intent), GFP_KERNEL);
0687     if (!intent)
0688         return NULL;
0689 
0690     intent->data = kzalloc(size, GFP_KERNEL);
0691     if (!intent->data)
0692         goto free_intent;
0693 
0694     spin_lock_irqsave(&channel->intent_lock, flags);
0695     ret = idr_alloc_cyclic(&channel->liids, intent, 1, -1, GFP_ATOMIC);
0696     if (ret < 0) {
0697         spin_unlock_irqrestore(&channel->intent_lock, flags);
0698         goto free_data;
0699     }
0700     spin_unlock_irqrestore(&channel->intent_lock, flags);
0701 
0702     intent->id = ret;
0703     intent->size = size;
0704     intent->reuse = reuseable;
0705 
0706     return intent;
0707 
0708 free_data:
0709     kfree(intent->data);
0710 free_intent:
0711     kfree(intent);
0712     return NULL;
0713 }
0714 
0715 static void qcom_glink_handle_rx_done(struct qcom_glink *glink,
0716                       u32 cid, uint32_t iid,
0717                       bool reuse)
0718 {
0719     struct glink_core_rx_intent *intent;
0720     struct glink_channel *channel;
0721     unsigned long flags;
0722 
0723     spin_lock_irqsave(&glink->idr_lock, flags);
0724     channel = idr_find(&glink->rcids, cid);
0725     spin_unlock_irqrestore(&glink->idr_lock, flags);
0726     if (!channel) {
0727         dev_err(glink->dev, "invalid channel id received\n");
0728         return;
0729     }
0730 
0731     spin_lock_irqsave(&channel->intent_lock, flags);
0732     intent = idr_find(&channel->riids, iid);
0733 
0734     if (!intent) {
0735         spin_unlock_irqrestore(&channel->intent_lock, flags);
0736         dev_err(glink->dev, "invalid intent id received\n");
0737         return;
0738     }
0739 
0740     intent->in_use = false;
0741 
0742     if (!reuse) {
0743         idr_remove(&channel->riids, intent->id);
0744         kfree(intent);
0745     }
0746     spin_unlock_irqrestore(&channel->intent_lock, flags);
0747 }
0748 
0749 /**
0750  * qcom_glink_handle_intent_req() - Receive a request for rx_intent
0751  *                      from remote side
0752  * @glink:      Pointer to the transport interface
0753  * @cid:    Remote channel ID
0754  * @size:   size of the intent
0755  *
0756  * The function searches for the local channel to which the request for
0757  * rx_intent has arrived and allocates and notifies the remote back
0758  */
0759 static void qcom_glink_handle_intent_req(struct qcom_glink *glink,
0760                      u32 cid, size_t size)
0761 {
0762     struct glink_core_rx_intent *intent;
0763     struct glink_channel *channel;
0764     unsigned long flags;
0765 
0766     spin_lock_irqsave(&glink->idr_lock, flags);
0767     channel = idr_find(&glink->rcids, cid);
0768     spin_unlock_irqrestore(&glink->idr_lock, flags);
0769 
0770     if (!channel) {
0771         pr_err("%s channel not found for cid %d\n", __func__, cid);
0772         return;
0773     }
0774 
0775     intent = qcom_glink_alloc_intent(glink, channel, size, false);
0776     if (intent)
0777         qcom_glink_advertise_intent(glink, channel, intent);
0778 
0779     qcom_glink_send_intent_req_ack(glink, channel, !!intent);
0780 }
0781 
0782 static int qcom_glink_rx_defer(struct qcom_glink *glink, size_t extra)
0783 {
0784     struct glink_defer_cmd *dcmd;
0785 
0786     extra = ALIGN(extra, 8);
0787 
0788     if (qcom_glink_rx_avail(glink) < sizeof(struct glink_msg) + extra) {
0789         dev_dbg(glink->dev, "Insufficient data in rx fifo");
0790         return -ENXIO;
0791     }
0792 
0793     dcmd = kzalloc(struct_size(dcmd, data, extra), GFP_ATOMIC);
0794     if (!dcmd)
0795         return -ENOMEM;
0796 
0797     INIT_LIST_HEAD(&dcmd->node);
0798 
0799     qcom_glink_rx_peak(glink, &dcmd->msg, 0, sizeof(dcmd->msg) + extra);
0800 
0801     spin_lock(&glink->rx_lock);
0802     list_add_tail(&dcmd->node, &glink->rx_queue);
0803     spin_unlock(&glink->rx_lock);
0804 
0805     schedule_work(&glink->rx_work);
0806     qcom_glink_rx_advance(glink, sizeof(dcmd->msg) + extra);
0807 
0808     return 0;
0809 }
0810 
0811 static int qcom_glink_rx_data(struct qcom_glink *glink, size_t avail)
0812 {
0813     struct glink_core_rx_intent *intent;
0814     struct glink_channel *channel;
0815     struct {
0816         struct glink_msg msg;
0817         __le32 chunk_size;
0818         __le32 left_size;
0819     } __packed hdr;
0820     unsigned int chunk_size;
0821     unsigned int left_size;
0822     unsigned int rcid;
0823     unsigned int liid;
0824     int ret = 0;
0825     unsigned long flags;
0826 
0827     if (avail < sizeof(hdr)) {
0828         dev_dbg(glink->dev, "Not enough data in fifo\n");
0829         return -EAGAIN;
0830     }
0831 
0832     qcom_glink_rx_peak(glink, &hdr, 0, sizeof(hdr));
0833     chunk_size = le32_to_cpu(hdr.chunk_size);
0834     left_size = le32_to_cpu(hdr.left_size);
0835 
0836     if (avail < sizeof(hdr) + chunk_size) {
0837         dev_dbg(glink->dev, "Payload not yet in fifo\n");
0838         return -EAGAIN;
0839     }
0840 
0841     rcid = le16_to_cpu(hdr.msg.param1);
0842     spin_lock_irqsave(&glink->idr_lock, flags);
0843     channel = idr_find(&glink->rcids, rcid);
0844     spin_unlock_irqrestore(&glink->idr_lock, flags);
0845     if (!channel) {
0846         dev_dbg(glink->dev, "Data on non-existing channel\n");
0847 
0848         /* Drop the message */
0849         goto advance_rx;
0850     }
0851 
0852     if (glink->intentless) {
0853         /* Might have an ongoing, fragmented, message to append */
0854         if (!channel->buf) {
0855             intent = kzalloc(sizeof(*intent), GFP_ATOMIC);
0856             if (!intent)
0857                 return -ENOMEM;
0858 
0859             intent->data = kmalloc(chunk_size + left_size,
0860                            GFP_ATOMIC);
0861             if (!intent->data) {
0862                 kfree(intent);
0863                 return -ENOMEM;
0864             }
0865 
0866             intent->id = 0xbabababa;
0867             intent->size = chunk_size + left_size;
0868             intent->offset = 0;
0869 
0870             channel->buf = intent;
0871         } else {
0872             intent = channel->buf;
0873         }
0874     } else {
0875         liid = le32_to_cpu(hdr.msg.param2);
0876 
0877         spin_lock_irqsave(&channel->intent_lock, flags);
0878         intent = idr_find(&channel->liids, liid);
0879         spin_unlock_irqrestore(&channel->intent_lock, flags);
0880 
0881         if (!intent) {
0882             dev_err(glink->dev,
0883                 "no intent found for channel %s intent %d",
0884                 channel->name, liid);
0885             ret = -ENOENT;
0886             goto advance_rx;
0887         }
0888     }
0889 
0890     if (intent->size - intent->offset < chunk_size) {
0891         dev_err(glink->dev, "Insufficient space in intent\n");
0892 
0893         /* The packet header lied, drop payload */
0894         goto advance_rx;
0895     }
0896 
0897     qcom_glink_rx_peak(glink, intent->data + intent->offset,
0898                sizeof(hdr), chunk_size);
0899     intent->offset += chunk_size;
0900 
0901     /* Handle message when no fragments remain to be received */
0902     if (!left_size) {
0903         spin_lock(&channel->recv_lock);
0904         if (channel->ept.cb) {
0905             channel->ept.cb(channel->ept.rpdev,
0906                     intent->data,
0907                     intent->offset,
0908                     channel->ept.priv,
0909                     RPMSG_ADDR_ANY);
0910         }
0911         spin_unlock(&channel->recv_lock);
0912 
0913         intent->offset = 0;
0914         channel->buf = NULL;
0915 
0916         qcom_glink_rx_done(glink, channel, intent);
0917     }
0918 
0919 advance_rx:
0920     qcom_glink_rx_advance(glink, ALIGN(sizeof(hdr) + chunk_size, 8));
0921 
0922     return ret;
0923 }
0924 
0925 static void qcom_glink_handle_intent(struct qcom_glink *glink,
0926                      unsigned int cid,
0927                      unsigned int count,
0928                      size_t avail)
0929 {
0930     struct glink_core_rx_intent *intent;
0931     struct glink_channel *channel;
0932     struct intent_pair {
0933         __le32 size;
0934         __le32 iid;
0935     };
0936 
0937     struct {
0938         struct glink_msg msg;
0939         struct intent_pair intents[];
0940     } __packed * msg;
0941 
0942     const size_t msglen = struct_size(msg, intents, count);
0943     int ret;
0944     int i;
0945     unsigned long flags;
0946 
0947     if (avail < msglen) {
0948         dev_dbg(glink->dev, "Not enough data in fifo\n");
0949         return;
0950     }
0951 
0952     spin_lock_irqsave(&glink->idr_lock, flags);
0953     channel = idr_find(&glink->rcids, cid);
0954     spin_unlock_irqrestore(&glink->idr_lock, flags);
0955     if (!channel) {
0956         dev_err(glink->dev, "intents for non-existing channel\n");
0957         return;
0958     }
0959 
0960     msg = kmalloc(msglen, GFP_ATOMIC);
0961     if (!msg)
0962         return;
0963 
0964     qcom_glink_rx_peak(glink, msg, 0, msglen);
0965 
0966     for (i = 0; i < count; ++i) {
0967         intent = kzalloc(sizeof(*intent), GFP_ATOMIC);
0968         if (!intent)
0969             break;
0970 
0971         intent->id = le32_to_cpu(msg->intents[i].iid);
0972         intent->size = le32_to_cpu(msg->intents[i].size);
0973 
0974         spin_lock_irqsave(&channel->intent_lock, flags);
0975         ret = idr_alloc(&channel->riids, intent,
0976                 intent->id, intent->id + 1, GFP_ATOMIC);
0977         spin_unlock_irqrestore(&channel->intent_lock, flags);
0978 
0979         if (ret < 0)
0980             dev_err(glink->dev, "failed to store remote intent\n");
0981     }
0982 
0983     kfree(msg);
0984     qcom_glink_rx_advance(glink, ALIGN(msglen, 8));
0985 }
0986 
0987 static int qcom_glink_rx_open_ack(struct qcom_glink *glink, unsigned int lcid)
0988 {
0989     struct glink_channel *channel;
0990 
0991     spin_lock(&glink->idr_lock);
0992     channel = idr_find(&glink->lcids, lcid);
0993     spin_unlock(&glink->idr_lock);
0994     if (!channel) {
0995         dev_err(glink->dev, "Invalid open ack packet\n");
0996         return -EINVAL;
0997     }
0998 
0999     complete_all(&channel->open_ack);
1000 
1001     return 0;
1002 }
1003 
1004 static irqreturn_t qcom_glink_native_intr(int irq, void *data)
1005 {
1006     struct qcom_glink *glink = data;
1007     struct glink_msg msg;
1008     unsigned int param1;
1009     unsigned int param2;
1010     unsigned int avail;
1011     unsigned int cmd;
1012     int ret = 0;
1013 
1014     /* To wakeup any blocking writers */
1015     wake_up_all(&glink->tx_avail_notify);
1016 
1017     for (;;) {
1018         avail = qcom_glink_rx_avail(glink);
1019         if (avail < sizeof(msg))
1020             break;
1021 
1022         qcom_glink_rx_peak(glink, &msg, 0, sizeof(msg));
1023 
1024         cmd = le16_to_cpu(msg.cmd);
1025         param1 = le16_to_cpu(msg.param1);
1026         param2 = le32_to_cpu(msg.param2);
1027 
1028         switch (cmd) {
1029         case RPM_CMD_VERSION:
1030         case RPM_CMD_VERSION_ACK:
1031         case RPM_CMD_CLOSE:
1032         case RPM_CMD_CLOSE_ACK:
1033         case RPM_CMD_RX_INTENT_REQ:
1034             ret = qcom_glink_rx_defer(glink, 0);
1035             break;
1036         case RPM_CMD_OPEN_ACK:
1037             ret = qcom_glink_rx_open_ack(glink, param1);
1038             qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1039             break;
1040         case RPM_CMD_OPEN:
1041             ret = qcom_glink_rx_defer(glink, param2);
1042             break;
1043         case RPM_CMD_TX_DATA:
1044         case RPM_CMD_TX_DATA_CONT:
1045             ret = qcom_glink_rx_data(glink, avail);
1046             break;
1047         case RPM_CMD_READ_NOTIF:
1048             qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1049 
1050             mbox_send_message(glink->mbox_chan, NULL);
1051             mbox_client_txdone(glink->mbox_chan, 0);
1052             break;
1053         case RPM_CMD_INTENT:
1054             qcom_glink_handle_intent(glink, param1, param2, avail);
1055             break;
1056         case RPM_CMD_RX_DONE:
1057             qcom_glink_handle_rx_done(glink, param1, param2, false);
1058             qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1059             break;
1060         case RPM_CMD_RX_DONE_W_REUSE:
1061             qcom_glink_handle_rx_done(glink, param1, param2, true);
1062             qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1063             break;
1064         case RPM_CMD_RX_INTENT_REQ_ACK:
1065             qcom_glink_handle_intent_req_ack(glink, param1, param2);
1066             qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8));
1067             break;
1068         default:
1069             dev_err(glink->dev, "unhandled rx cmd: %d\n", cmd);
1070             ret = -EINVAL;
1071             break;
1072         }
1073 
1074         if (ret)
1075             break;
1076     }
1077 
1078     return IRQ_HANDLED;
1079 }
1080 
1081 /* Locally initiated rpmsg_create_ept */
1082 static struct glink_channel *qcom_glink_create_local(struct qcom_glink *glink,
1083                              const char *name)
1084 {
1085     struct glink_channel *channel;
1086     int ret;
1087     unsigned long flags;
1088 
1089     channel = qcom_glink_alloc_channel(glink, name);
1090     if (IS_ERR(channel))
1091         return ERR_CAST(channel);
1092 
1093     ret = qcom_glink_send_open_req(glink, channel);
1094     if (ret)
1095         goto release_channel;
1096 
1097     ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
1098     if (!ret)
1099         goto err_timeout;
1100 
1101     ret = wait_for_completion_timeout(&channel->open_req, 5 * HZ);
1102     if (!ret)
1103         goto err_timeout;
1104 
1105     qcom_glink_send_open_ack(glink, channel);
1106 
1107     return channel;
1108 
1109 err_timeout:
1110     /* qcom_glink_send_open_req() did register the channel in lcids*/
1111     spin_lock_irqsave(&glink->idr_lock, flags);
1112     idr_remove(&glink->lcids, channel->lcid);
1113     spin_unlock_irqrestore(&glink->idr_lock, flags);
1114 
1115 release_channel:
1116     /* Release qcom_glink_send_open_req() reference */
1117     kref_put(&channel->refcount, qcom_glink_channel_release);
1118     /* Release qcom_glink_alloc_channel() reference */
1119     kref_put(&channel->refcount, qcom_glink_channel_release);
1120 
1121     return ERR_PTR(-ETIMEDOUT);
1122 }
1123 
1124 /* Remote initiated rpmsg_create_ept */
1125 static int qcom_glink_create_remote(struct qcom_glink *glink,
1126                     struct glink_channel *channel)
1127 {
1128     int ret;
1129 
1130     qcom_glink_send_open_ack(glink, channel);
1131 
1132     ret = qcom_glink_send_open_req(glink, channel);
1133     if (ret)
1134         goto close_link;
1135 
1136     ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ);
1137     if (!ret) {
1138         ret = -ETIMEDOUT;
1139         goto close_link;
1140     }
1141 
1142     return 0;
1143 
1144 close_link:
1145     /*
1146      * Send a close request to "undo" our open-ack. The close-ack will
1147      * release qcom_glink_send_open_req() reference and the last reference
1148      * will be relesed after receiving remote_close or transport unregister
1149      * by calling qcom_glink_native_remove().
1150      */
1151     qcom_glink_send_close_req(glink, channel);
1152 
1153     return ret;
1154 }
1155 
1156 static struct rpmsg_endpoint *qcom_glink_create_ept(struct rpmsg_device *rpdev,
1157                             rpmsg_rx_cb_t cb,
1158                             void *priv,
1159                             struct rpmsg_channel_info
1160                                     chinfo)
1161 {
1162     struct glink_channel *parent = to_glink_channel(rpdev->ept);
1163     struct glink_channel *channel;
1164     struct qcom_glink *glink = parent->glink;
1165     struct rpmsg_endpoint *ept;
1166     const char *name = chinfo.name;
1167     int cid;
1168     int ret;
1169     unsigned long flags;
1170 
1171     spin_lock_irqsave(&glink->idr_lock, flags);
1172     idr_for_each_entry(&glink->rcids, channel, cid) {
1173         if (!strcmp(channel->name, name))
1174             break;
1175     }
1176     spin_unlock_irqrestore(&glink->idr_lock, flags);
1177 
1178     if (!channel) {
1179         channel = qcom_glink_create_local(glink, name);
1180         if (IS_ERR(channel))
1181             return NULL;
1182     } else {
1183         ret = qcom_glink_create_remote(glink, channel);
1184         if (ret)
1185             return NULL;
1186     }
1187 
1188     ept = &channel->ept;
1189     ept->rpdev = rpdev;
1190     ept->cb = cb;
1191     ept->priv = priv;
1192     ept->ops = &glink_endpoint_ops;
1193 
1194     return ept;
1195 }
1196 
1197 static int qcom_glink_announce_create(struct rpmsg_device *rpdev)
1198 {
1199     struct glink_channel *channel = to_glink_channel(rpdev->ept);
1200     struct device_node *np = rpdev->dev.of_node;
1201     struct qcom_glink *glink = channel->glink;
1202     struct glink_core_rx_intent *intent;
1203     const struct property *prop = NULL;
1204     __be32 defaults[] = { cpu_to_be32(SZ_1K), cpu_to_be32(5) };
1205     int num_intents;
1206     int num_groups = 1;
1207     __be32 *val = defaults;
1208     int size;
1209 
1210     if (glink->intentless || !completion_done(&channel->open_ack))
1211         return 0;
1212 
1213     prop = of_find_property(np, "qcom,intents", NULL);
1214     if (prop) {
1215         val = prop->value;
1216         num_groups = prop->length / sizeof(u32) / 2;
1217     }
1218 
1219     /* Channel is now open, advertise base set of intents */
1220     while (num_groups--) {
1221         size = be32_to_cpup(val++);
1222         num_intents = be32_to_cpup(val++);
1223         while (num_intents--) {
1224             intent = qcom_glink_alloc_intent(glink, channel, size,
1225                              true);
1226             if (!intent)
1227                 break;
1228 
1229             qcom_glink_advertise_intent(glink, channel, intent);
1230         }
1231     }
1232     return 0;
1233 }
1234 
1235 static void qcom_glink_destroy_ept(struct rpmsg_endpoint *ept)
1236 {
1237     struct glink_channel *channel = to_glink_channel(ept);
1238     struct qcom_glink *glink = channel->glink;
1239     unsigned long flags;
1240 
1241     spin_lock_irqsave(&channel->recv_lock, flags);
1242     channel->ept.cb = NULL;
1243     spin_unlock_irqrestore(&channel->recv_lock, flags);
1244 
1245     /* Decouple the potential rpdev from the channel */
1246     channel->rpdev = NULL;
1247 
1248     qcom_glink_send_close_req(glink, channel);
1249 }
1250 
1251 static int qcom_glink_request_intent(struct qcom_glink *glink,
1252                      struct glink_channel *channel,
1253                      size_t size)
1254 {
1255     struct {
1256         u16 id;
1257         u16 cid;
1258         u32 size;
1259     } __packed cmd;
1260 
1261     int ret;
1262 
1263     mutex_lock(&channel->intent_req_lock);
1264 
1265     reinit_completion(&channel->intent_req_comp);
1266 
1267     cmd.id = RPM_CMD_RX_INTENT_REQ;
1268     cmd.cid = channel->lcid;
1269     cmd.size = size;
1270 
1271     ret = qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true);
1272     if (ret)
1273         goto unlock;
1274 
1275     ret = wait_for_completion_timeout(&channel->intent_req_comp, 10 * HZ);
1276     if (!ret) {
1277         dev_err(glink->dev, "intent request timed out\n");
1278         ret = -ETIMEDOUT;
1279     } else {
1280         ret = channel->intent_req_result ? 0 : -ECANCELED;
1281     }
1282 
1283 unlock:
1284     mutex_unlock(&channel->intent_req_lock);
1285     return ret;
1286 }
1287 
1288 static int __qcom_glink_send(struct glink_channel *channel,
1289                  void *data, int len, bool wait)
1290 {
1291     struct qcom_glink *glink = channel->glink;
1292     struct glink_core_rx_intent *intent = NULL;
1293     struct glink_core_rx_intent *tmp;
1294     int iid = 0;
1295     struct {
1296         struct glink_msg msg;
1297         __le32 chunk_size;
1298         __le32 left_size;
1299     } __packed req;
1300     int ret;
1301     unsigned long flags;
1302     int chunk_size = len;
1303     int left_size = 0;
1304 
1305     if (!glink->intentless) {
1306         while (!intent) {
1307             spin_lock_irqsave(&channel->intent_lock, flags);
1308             idr_for_each_entry(&channel->riids, tmp, iid) {
1309                 if (tmp->size >= len && !tmp->in_use) {
1310                     if (!intent)
1311                         intent = tmp;
1312                     else if (intent->size > tmp->size)
1313                         intent = tmp;
1314                     if (intent->size == len)
1315                         break;
1316                 }
1317             }
1318             if (intent)
1319                 intent->in_use = true;
1320             spin_unlock_irqrestore(&channel->intent_lock, flags);
1321 
1322             /* We found an available intent */
1323             if (intent)
1324                 break;
1325 
1326             if (!wait)
1327                 return -EBUSY;
1328 
1329             ret = qcom_glink_request_intent(glink, channel, len);
1330             if (ret < 0)
1331                 return ret;
1332         }
1333 
1334         iid = intent->id;
1335     }
1336 
1337     if (wait && chunk_size > SZ_8K) {
1338         chunk_size = SZ_8K;
1339         left_size = len - chunk_size;
1340     }
1341     req.msg.cmd = cpu_to_le16(RPM_CMD_TX_DATA);
1342     req.msg.param1 = cpu_to_le16(channel->lcid);
1343     req.msg.param2 = cpu_to_le32(iid);
1344     req.chunk_size = cpu_to_le32(chunk_size);
1345     req.left_size = cpu_to_le32(left_size);
1346 
1347     ret = qcom_glink_tx(glink, &req, sizeof(req), data, chunk_size, wait);
1348 
1349     /* Mark intent available if we failed */
1350     if (ret && intent) {
1351         intent->in_use = false;
1352         return ret;
1353     }
1354 
1355     while (left_size > 0) {
1356         data = (void *)((char *)data + chunk_size);
1357         chunk_size = left_size;
1358         if (chunk_size > SZ_8K)
1359             chunk_size = SZ_8K;
1360         left_size -= chunk_size;
1361 
1362         req.msg.cmd = cpu_to_le16(RPM_CMD_TX_DATA_CONT);
1363         req.msg.param1 = cpu_to_le16(channel->lcid);
1364         req.msg.param2 = cpu_to_le32(iid);
1365         req.chunk_size = cpu_to_le32(chunk_size);
1366         req.left_size = cpu_to_le32(left_size);
1367 
1368         ret = qcom_glink_tx(glink, &req, sizeof(req), data,
1369                     chunk_size, wait);
1370 
1371         /* Mark intent available if we failed */
1372         if (ret && intent) {
1373             intent->in_use = false;
1374             break;
1375         }
1376     }
1377     return ret;
1378 }
1379 
1380 static int qcom_glink_send(struct rpmsg_endpoint *ept, void *data, int len)
1381 {
1382     struct glink_channel *channel = to_glink_channel(ept);
1383 
1384     return __qcom_glink_send(channel, data, len, true);
1385 }
1386 
1387 static int qcom_glink_trysend(struct rpmsg_endpoint *ept, void *data, int len)
1388 {
1389     struct glink_channel *channel = to_glink_channel(ept);
1390 
1391     return __qcom_glink_send(channel, data, len, false);
1392 }
1393 
1394 static int qcom_glink_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
1395 {
1396     struct glink_channel *channel = to_glink_channel(ept);
1397 
1398     return __qcom_glink_send(channel, data, len, true);
1399 }
1400 
1401 static int qcom_glink_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst)
1402 {
1403     struct glink_channel *channel = to_glink_channel(ept);
1404 
1405     return __qcom_glink_send(channel, data, len, false);
1406 }
1407 
1408 /*
1409  * Finds the device_node for the glink child interested in this channel.
1410  */
1411 static struct device_node *qcom_glink_match_channel(struct device_node *node,
1412                             const char *channel)
1413 {
1414     struct device_node *child;
1415     const char *name;
1416     const char *key;
1417     int ret;
1418 
1419     for_each_available_child_of_node(node, child) {
1420         key = "qcom,glink-channels";
1421         ret = of_property_read_string(child, key, &name);
1422         if (ret)
1423             continue;
1424 
1425         if (strcmp(name, channel) == 0)
1426             return child;
1427     }
1428 
1429     return NULL;
1430 }
1431 
1432 static const struct rpmsg_device_ops glink_device_ops = {
1433     .create_ept = qcom_glink_create_ept,
1434     .announce_create = qcom_glink_announce_create,
1435 };
1436 
1437 static const struct rpmsg_endpoint_ops glink_endpoint_ops = {
1438     .destroy_ept = qcom_glink_destroy_ept,
1439     .send = qcom_glink_send,
1440     .sendto = qcom_glink_sendto,
1441     .trysend = qcom_glink_trysend,
1442     .trysendto = qcom_glink_trysendto,
1443 };
1444 
1445 static void qcom_glink_rpdev_release(struct device *dev)
1446 {
1447     struct rpmsg_device *rpdev = to_rpmsg_device(dev);
1448 
1449     kfree(rpdev);
1450 }
1451 
1452 static int qcom_glink_rx_open(struct qcom_glink *glink, unsigned int rcid,
1453                   char *name)
1454 {
1455     struct glink_channel *channel;
1456     struct rpmsg_device *rpdev;
1457     bool create_device = false;
1458     struct device_node *node;
1459     int lcid;
1460     int ret;
1461     unsigned long flags;
1462 
1463     spin_lock_irqsave(&glink->idr_lock, flags);
1464     idr_for_each_entry(&glink->lcids, channel, lcid) {
1465         if (!strcmp(channel->name, name))
1466             break;
1467     }
1468     spin_unlock_irqrestore(&glink->idr_lock, flags);
1469 
1470     if (!channel) {
1471         channel = qcom_glink_alloc_channel(glink, name);
1472         if (IS_ERR(channel))
1473             return PTR_ERR(channel);
1474 
1475         /* The opening dance was initiated by the remote */
1476         create_device = true;
1477     }
1478 
1479     spin_lock_irqsave(&glink->idr_lock, flags);
1480     ret = idr_alloc(&glink->rcids, channel, rcid, rcid + 1, GFP_ATOMIC);
1481     if (ret < 0) {
1482         dev_err(glink->dev, "Unable to insert channel into rcid list\n");
1483         spin_unlock_irqrestore(&glink->idr_lock, flags);
1484         goto free_channel;
1485     }
1486     channel->rcid = ret;
1487     spin_unlock_irqrestore(&glink->idr_lock, flags);
1488 
1489     complete_all(&channel->open_req);
1490 
1491     if (create_device) {
1492         rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL);
1493         if (!rpdev) {
1494             ret = -ENOMEM;
1495             goto rcid_remove;
1496         }
1497 
1498         rpdev->ept = &channel->ept;
1499         strscpy_pad(rpdev->id.name, name, RPMSG_NAME_SIZE);
1500         rpdev->src = RPMSG_ADDR_ANY;
1501         rpdev->dst = RPMSG_ADDR_ANY;
1502         rpdev->ops = &glink_device_ops;
1503 
1504         node = qcom_glink_match_channel(glink->dev->of_node, name);
1505         rpdev->dev.of_node = node;
1506         rpdev->dev.parent = glink->dev;
1507         rpdev->dev.release = qcom_glink_rpdev_release;
1508 
1509         ret = rpmsg_register_device(rpdev);
1510         if (ret)
1511             goto rcid_remove;
1512 
1513         channel->rpdev = rpdev;
1514     }
1515 
1516     return 0;
1517 
1518 rcid_remove:
1519     spin_lock_irqsave(&glink->idr_lock, flags);
1520     idr_remove(&glink->rcids, channel->rcid);
1521     channel->rcid = 0;
1522     spin_unlock_irqrestore(&glink->idr_lock, flags);
1523 free_channel:
1524     /* Release the reference, iff we took it */
1525     if (create_device)
1526         kref_put(&channel->refcount, qcom_glink_channel_release);
1527 
1528     return ret;
1529 }
1530 
1531 static void qcom_glink_rx_close(struct qcom_glink *glink, unsigned int rcid)
1532 {
1533     struct rpmsg_channel_info chinfo;
1534     struct glink_channel *channel;
1535     unsigned long flags;
1536 
1537     spin_lock_irqsave(&glink->idr_lock, flags);
1538     channel = idr_find(&glink->rcids, rcid);
1539     spin_unlock_irqrestore(&glink->idr_lock, flags);
1540     if (WARN(!channel, "close request on unknown channel\n"))
1541         return;
1542 
1543     /* cancel pending rx_done work */
1544     cancel_work_sync(&channel->intent_work);
1545 
1546     if (channel->rpdev) {
1547         strscpy_pad(chinfo.name, channel->name, sizeof(chinfo.name));
1548         chinfo.src = RPMSG_ADDR_ANY;
1549         chinfo.dst = RPMSG_ADDR_ANY;
1550 
1551         rpmsg_unregister_device(glink->dev, &chinfo);
1552     }
1553     channel->rpdev = NULL;
1554 
1555     qcom_glink_send_close_ack(glink, channel->rcid);
1556 
1557     spin_lock_irqsave(&glink->idr_lock, flags);
1558     idr_remove(&glink->rcids, channel->rcid);
1559     channel->rcid = 0;
1560     spin_unlock_irqrestore(&glink->idr_lock, flags);
1561 
1562     kref_put(&channel->refcount, qcom_glink_channel_release);
1563 }
1564 
1565 static void qcom_glink_rx_close_ack(struct qcom_glink *glink, unsigned int lcid)
1566 {
1567     struct rpmsg_channel_info chinfo;
1568     struct glink_channel *channel;
1569     unsigned long flags;
1570 
1571     /* To wakeup any blocking writers */
1572     wake_up_all(&glink->tx_avail_notify);
1573 
1574     spin_lock_irqsave(&glink->idr_lock, flags);
1575     channel = idr_find(&glink->lcids, lcid);
1576     if (WARN(!channel, "close ack on unknown channel\n")) {
1577         spin_unlock_irqrestore(&glink->idr_lock, flags);
1578         return;
1579     }
1580 
1581     idr_remove(&glink->lcids, channel->lcid);
1582     channel->lcid = 0;
1583     spin_unlock_irqrestore(&glink->idr_lock, flags);
1584 
1585     /* Decouple the potential rpdev from the channel */
1586     if (channel->rpdev) {
1587         strscpy(chinfo.name, channel->name, sizeof(chinfo.name));
1588         chinfo.src = RPMSG_ADDR_ANY;
1589         chinfo.dst = RPMSG_ADDR_ANY;
1590 
1591         rpmsg_unregister_device(glink->dev, &chinfo);
1592     }
1593     channel->rpdev = NULL;
1594 
1595     kref_put(&channel->refcount, qcom_glink_channel_release);
1596 }
1597 
1598 static void qcom_glink_work(struct work_struct *work)
1599 {
1600     struct qcom_glink *glink = container_of(work, struct qcom_glink,
1601                         rx_work);
1602     struct glink_defer_cmd *dcmd;
1603     struct glink_msg *msg;
1604     unsigned long flags;
1605     unsigned int param1;
1606     unsigned int param2;
1607     unsigned int cmd;
1608 
1609     for (;;) {
1610         spin_lock_irqsave(&glink->rx_lock, flags);
1611         if (list_empty(&glink->rx_queue)) {
1612             spin_unlock_irqrestore(&glink->rx_lock, flags);
1613             break;
1614         }
1615         dcmd = list_first_entry(&glink->rx_queue,
1616                     struct glink_defer_cmd, node);
1617         list_del(&dcmd->node);
1618         spin_unlock_irqrestore(&glink->rx_lock, flags);
1619 
1620         msg = &dcmd->msg;
1621         cmd = le16_to_cpu(msg->cmd);
1622         param1 = le16_to_cpu(msg->param1);
1623         param2 = le32_to_cpu(msg->param2);
1624 
1625         switch (cmd) {
1626         case RPM_CMD_VERSION:
1627             qcom_glink_receive_version(glink, param1, param2);
1628             break;
1629         case RPM_CMD_VERSION_ACK:
1630             qcom_glink_receive_version_ack(glink, param1, param2);
1631             break;
1632         case RPM_CMD_OPEN:
1633             qcom_glink_rx_open(glink, param1, msg->data);
1634             break;
1635         case RPM_CMD_CLOSE:
1636             qcom_glink_rx_close(glink, param1);
1637             break;
1638         case RPM_CMD_CLOSE_ACK:
1639             qcom_glink_rx_close_ack(glink, param1);
1640             break;
1641         case RPM_CMD_RX_INTENT_REQ:
1642             qcom_glink_handle_intent_req(glink, param1, param2);
1643             break;
1644         default:
1645             WARN(1, "Unknown defer object %d\n", cmd);
1646             break;
1647         }
1648 
1649         kfree(dcmd);
1650     }
1651 }
1652 
1653 static void qcom_glink_cancel_rx_work(struct qcom_glink *glink)
1654 {
1655     struct glink_defer_cmd *dcmd;
1656     struct glink_defer_cmd *tmp;
1657 
1658     /* cancel any pending deferred rx_work */
1659     cancel_work_sync(&glink->rx_work);
1660 
1661     list_for_each_entry_safe(dcmd, tmp, &glink->rx_queue, node)
1662         kfree(dcmd);
1663 }
1664 
1665 static ssize_t rpmsg_name_show(struct device *dev,
1666                    struct device_attribute *attr, char *buf)
1667 {
1668     int ret = 0;
1669     const char *name;
1670 
1671     ret = of_property_read_string(dev->of_node, "label", &name);
1672     if (ret < 0)
1673         name = dev->of_node->name;
1674 
1675     return sysfs_emit(buf, "%s\n", name);
1676 }
1677 static DEVICE_ATTR_RO(rpmsg_name);
1678 
1679 static struct attribute *qcom_glink_attrs[] = {
1680     &dev_attr_rpmsg_name.attr,
1681     NULL
1682 };
1683 ATTRIBUTE_GROUPS(qcom_glink);
1684 
1685 static void qcom_glink_device_release(struct device *dev)
1686 {
1687     struct rpmsg_device *rpdev = to_rpmsg_device(dev);
1688     struct glink_channel *channel = to_glink_channel(rpdev->ept);
1689 
1690     /* Release qcom_glink_alloc_channel() reference */
1691     kref_put(&channel->refcount, qcom_glink_channel_release);
1692     kfree(rpdev);
1693 }
1694 
1695 static int qcom_glink_create_chrdev(struct qcom_glink *glink)
1696 {
1697     struct rpmsg_device *rpdev;
1698     struct glink_channel *channel;
1699 
1700     rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL);
1701     if (!rpdev)
1702         return -ENOMEM;
1703 
1704     channel = qcom_glink_alloc_channel(glink, "rpmsg_chrdev");
1705     if (IS_ERR(channel)) {
1706         kfree(rpdev);
1707         return PTR_ERR(channel);
1708     }
1709     channel->rpdev = rpdev;
1710 
1711     rpdev->ept = &channel->ept;
1712     rpdev->ops = &glink_device_ops;
1713     rpdev->dev.parent = glink->dev;
1714     rpdev->dev.release = qcom_glink_device_release;
1715 
1716     return rpmsg_ctrldev_register_device(rpdev);
1717 }
1718 
1719 struct qcom_glink *qcom_glink_native_probe(struct device *dev,
1720                        unsigned long features,
1721                        struct qcom_glink_pipe *rx,
1722                        struct qcom_glink_pipe *tx,
1723                        bool intentless)
1724 {
1725     int irq;
1726     int ret;
1727     struct qcom_glink *glink;
1728 
1729     glink = devm_kzalloc(dev, sizeof(*glink), GFP_KERNEL);
1730     if (!glink)
1731         return ERR_PTR(-ENOMEM);
1732 
1733     glink->dev = dev;
1734     glink->tx_pipe = tx;
1735     glink->rx_pipe = rx;
1736 
1737     glink->features = features;
1738     glink->intentless = intentless;
1739 
1740     spin_lock_init(&glink->tx_lock);
1741     spin_lock_init(&glink->rx_lock);
1742     INIT_LIST_HEAD(&glink->rx_queue);
1743     INIT_WORK(&glink->rx_work, qcom_glink_work);
1744     init_waitqueue_head(&glink->tx_avail_notify);
1745 
1746     spin_lock_init(&glink->idr_lock);
1747     idr_init(&glink->lcids);
1748     idr_init(&glink->rcids);
1749 
1750     glink->dev->groups = qcom_glink_groups;
1751 
1752     ret = device_add_groups(dev, qcom_glink_groups);
1753     if (ret)
1754         dev_err(dev, "failed to add groups\n");
1755 
1756     glink->mbox_client.dev = dev;
1757     glink->mbox_client.knows_txdone = true;
1758     glink->mbox_chan = mbox_request_channel(&glink->mbox_client, 0);
1759     if (IS_ERR(glink->mbox_chan)) {
1760         if (PTR_ERR(glink->mbox_chan) != -EPROBE_DEFER)
1761             dev_err(dev, "failed to acquire IPC channel\n");
1762         return ERR_CAST(glink->mbox_chan);
1763     }
1764 
1765     irq = of_irq_get(dev->of_node, 0);
1766     ret = devm_request_irq(dev, irq,
1767                    qcom_glink_native_intr,
1768                    IRQF_NO_SUSPEND | IRQF_SHARED,
1769                    "glink-native", glink);
1770     if (ret) {
1771         dev_err(dev, "failed to request IRQ\n");
1772         return ERR_PTR(ret);
1773     }
1774 
1775     glink->irq = irq;
1776 
1777     ret = qcom_glink_send_version(glink);
1778     if (ret)
1779         return ERR_PTR(ret);
1780 
1781     ret = qcom_glink_create_chrdev(glink);
1782     if (ret)
1783         dev_err(glink->dev, "failed to register chrdev\n");
1784 
1785     return glink;
1786 }
1787 EXPORT_SYMBOL_GPL(qcom_glink_native_probe);
1788 
1789 static int qcom_glink_remove_device(struct device *dev, void *data)
1790 {
1791     device_unregister(dev);
1792 
1793     return 0;
1794 }
1795 
1796 void qcom_glink_native_remove(struct qcom_glink *glink)
1797 {
1798     struct glink_channel *channel;
1799     int cid;
1800     int ret;
1801 
1802     disable_irq(glink->irq);
1803     qcom_glink_cancel_rx_work(glink);
1804 
1805     ret = device_for_each_child(glink->dev, NULL, qcom_glink_remove_device);
1806     if (ret)
1807         dev_warn(glink->dev, "Can't remove GLINK devices: %d\n", ret);
1808 
1809     /* Release any defunct local channels, waiting for close-ack */
1810     idr_for_each_entry(&glink->lcids, channel, cid)
1811         kref_put(&channel->refcount, qcom_glink_channel_release);
1812 
1813     /* Release any defunct local channels, waiting for close-req */
1814     idr_for_each_entry(&glink->rcids, channel, cid)
1815         kref_put(&channel->refcount, qcom_glink_channel_release);
1816 
1817     idr_destroy(&glink->lcids);
1818     idr_destroy(&glink->rcids);
1819     mbox_free_channel(glink->mbox_chan);
1820 }
1821 EXPORT_SYMBOL_GPL(qcom_glink_native_remove);
1822 
1823 void qcom_glink_native_unregister(struct qcom_glink *glink)
1824 {
1825     device_unregister(glink->dev);
1826 }
1827 EXPORT_SYMBOL_GPL(qcom_glink_native_unregister);
1828 
1829 MODULE_DESCRIPTION("Qualcomm GLINK driver");
1830 MODULE_LICENSE("GPL v2");