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