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> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/workqueue.h> |
| 28 | #include <linux/mailbox_client.h> |
| 29 | |
| 30 | #include "rpmsg_internal.h" |
| 31 | #include "qcom_glink_native.h" |
| 32 | |
| 33 | #define GLINK_NAME_SIZE 32 |
Sricharan R | d31ad61 | 2017-08-24 12:51:32 +0530 | [diff] [blame] | 34 | #define GLINK_VERSION_1 1 |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 35 | |
| 36 | #define RPM_GLINK_CID_MIN 1 |
| 37 | #define RPM_GLINK_CID_MAX 65536 |
| 38 | |
| 39 | struct glink_msg { |
| 40 | __le16 cmd; |
| 41 | __le16 param1; |
| 42 | __le32 param2; |
| 43 | u8 data[]; |
| 44 | } __packed; |
| 45 | |
| 46 | /** |
| 47 | * struct glink_defer_cmd - deferred incoming control message |
| 48 | * @node: list node |
| 49 | * @msg: message header |
| 50 | * data: payload of the message |
| 51 | * |
| 52 | * Copy of a received control message, to be added to @rx_queue and processed |
| 53 | * by @rx_work of @qcom_glink. |
| 54 | */ |
| 55 | struct glink_defer_cmd { |
| 56 | struct list_head node; |
| 57 | |
| 58 | struct glink_msg msg; |
| 59 | u8 data[]; |
| 60 | }; |
| 61 | |
| 62 | /** |
Sricharan R | 933b45d | 2017-08-24 12:51:34 +0530 | [diff] [blame^] | 63 | * struct glink_core_rx_intent - RX intent |
| 64 | * RX intent |
| 65 | * |
| 66 | * data: pointer to the data (may be NULL for zero-copy) |
| 67 | * id: remote or local intent ID |
| 68 | * size: size of the original intent (do not modify) |
| 69 | * reuse: To mark if the intent can be reused after first use |
| 70 | * in_use: To mark if intent is already in use for the channel |
| 71 | * offset: next write offset (initially 0) |
| 72 | */ |
| 73 | struct glink_core_rx_intent { |
| 74 | void *data; |
| 75 | u32 id; |
| 76 | size_t size; |
| 77 | bool reuse; |
| 78 | bool in_use; |
| 79 | u32 offset; |
| 80 | }; |
| 81 | |
| 82 | /** |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 83 | * struct qcom_glink - driver context, relates to one remote subsystem |
| 84 | * @dev: reference to the associated struct device |
| 85 | * @mbox_client: mailbox client |
| 86 | * @mbox_chan: mailbox channel |
| 87 | * @rx_pipe: pipe object for receive FIFO |
| 88 | * @tx_pipe: pipe object for transmit FIFO |
| 89 | * @irq: IRQ for signaling incoming events |
| 90 | * @rx_work: worker for handling received control messages |
| 91 | * @rx_lock: protects the @rx_queue |
| 92 | * @rx_queue: queue of received control messages to be processed in @rx_work |
| 93 | * @tx_lock: synchronizes operations on the tx fifo |
| 94 | * @idr_lock: synchronizes @lcids and @rcids modifications |
| 95 | * @lcids: idr of all channels with a known local channel id |
| 96 | * @rcids: idr of all channels with a known remote channel id |
| 97 | */ |
| 98 | struct qcom_glink { |
| 99 | struct device *dev; |
| 100 | |
| 101 | struct mbox_client mbox_client; |
| 102 | struct mbox_chan *mbox_chan; |
| 103 | |
| 104 | struct qcom_glink_pipe *rx_pipe; |
| 105 | struct qcom_glink_pipe *tx_pipe; |
| 106 | |
| 107 | int irq; |
| 108 | |
| 109 | struct work_struct rx_work; |
| 110 | spinlock_t rx_lock; |
| 111 | struct list_head rx_queue; |
| 112 | |
| 113 | struct mutex tx_lock; |
| 114 | |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 115 | spinlock_t idr_lock; |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 116 | struct idr lcids; |
| 117 | struct idr rcids; |
Sricharan R | d31ad61 | 2017-08-24 12:51:32 +0530 | [diff] [blame] | 118 | unsigned long features; |
| 119 | |
| 120 | bool intentless; |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | enum { |
| 124 | GLINK_STATE_CLOSED, |
| 125 | GLINK_STATE_OPENING, |
| 126 | GLINK_STATE_OPEN, |
| 127 | GLINK_STATE_CLOSING, |
| 128 | }; |
| 129 | |
| 130 | /** |
| 131 | * struct glink_channel - internal representation of a channel |
| 132 | * @rpdev: rpdev reference, only used for primary endpoints |
| 133 | * @ept: rpmsg endpoint this channel is associated with |
| 134 | * @glink: qcom_glink context handle |
| 135 | * @refcount: refcount for the channel object |
| 136 | * @recv_lock: guard for @ept.cb |
| 137 | * @name: unique channel name/identifier |
| 138 | * @lcid: channel id, in local space |
| 139 | * @rcid: channel id, in remote space |
Sricharan R | 933b45d | 2017-08-24 12:51:34 +0530 | [diff] [blame^] | 140 | * @intent_lock: lock for protection of @liids |
| 141 | * @liids: idr of all local intents |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 142 | * @buf: receive buffer, for gathering fragments |
| 143 | * @buf_offset: write offset in @buf |
| 144 | * @buf_size: size of current @buf |
| 145 | * @open_ack: completed once remote has acked the open-request |
| 146 | * @open_req: completed once open-request has been received |
| 147 | */ |
| 148 | struct glink_channel { |
| 149 | struct rpmsg_endpoint ept; |
| 150 | |
| 151 | struct rpmsg_device *rpdev; |
| 152 | struct qcom_glink *glink; |
| 153 | |
| 154 | struct kref refcount; |
| 155 | |
| 156 | spinlock_t recv_lock; |
| 157 | |
| 158 | char *name; |
| 159 | unsigned int lcid; |
| 160 | unsigned int rcid; |
| 161 | |
Sricharan R | 933b45d | 2017-08-24 12:51:34 +0530 | [diff] [blame^] | 162 | spinlock_t intent_lock; |
| 163 | struct idr liids; |
| 164 | |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 165 | void *buf; |
| 166 | int buf_offset; |
| 167 | int buf_size; |
| 168 | |
| 169 | struct completion open_ack; |
| 170 | struct completion open_req; |
| 171 | }; |
| 172 | |
| 173 | #define to_glink_channel(_ept) container_of(_ept, struct glink_channel, ept) |
| 174 | |
| 175 | static const struct rpmsg_endpoint_ops glink_endpoint_ops; |
| 176 | |
| 177 | #define RPM_CMD_VERSION 0 |
| 178 | #define RPM_CMD_VERSION_ACK 1 |
| 179 | #define RPM_CMD_OPEN 2 |
| 180 | #define RPM_CMD_CLOSE 3 |
| 181 | #define RPM_CMD_OPEN_ACK 4 |
Sricharan R | 933b45d | 2017-08-24 12:51:34 +0530 | [diff] [blame^] | 182 | #define RPM_CMD_INTENT 5 |
| 183 | #define RPM_CMD_RX_INTENT_REQ 7 |
| 184 | #define RPM_CMD_RX_INTENT_REQ_ACK 8 |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 185 | #define RPM_CMD_TX_DATA 9 |
| 186 | #define RPM_CMD_CLOSE_ACK 11 |
| 187 | #define RPM_CMD_TX_DATA_CONT 12 |
| 188 | #define RPM_CMD_READ_NOTIF 13 |
| 189 | |
| 190 | #define GLINK_FEATURE_INTENTLESS BIT(1) |
| 191 | |
| 192 | static struct glink_channel *qcom_glink_alloc_channel(struct qcom_glink *glink, |
| 193 | const char *name) |
| 194 | { |
| 195 | struct glink_channel *channel; |
| 196 | |
| 197 | channel = kzalloc(sizeof(*channel), GFP_KERNEL); |
| 198 | if (!channel) |
| 199 | return ERR_PTR(-ENOMEM); |
| 200 | |
| 201 | /* Setup glink internal glink_channel data */ |
| 202 | spin_lock_init(&channel->recv_lock); |
Sricharan R | 933b45d | 2017-08-24 12:51:34 +0530 | [diff] [blame^] | 203 | spin_lock_init(&channel->intent_lock); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 204 | channel->glink = glink; |
| 205 | channel->name = kstrdup(name, GFP_KERNEL); |
| 206 | |
| 207 | init_completion(&channel->open_req); |
| 208 | init_completion(&channel->open_ack); |
| 209 | |
Sricharan R | 933b45d | 2017-08-24 12:51:34 +0530 | [diff] [blame^] | 210 | idr_init(&channel->liids); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 211 | kref_init(&channel->refcount); |
| 212 | |
| 213 | return channel; |
| 214 | } |
| 215 | |
| 216 | static void qcom_glink_channel_release(struct kref *ref) |
| 217 | { |
| 218 | struct glink_channel *channel = container_of(ref, struct glink_channel, |
| 219 | refcount); |
Sricharan R | 933b45d | 2017-08-24 12:51:34 +0530 | [diff] [blame^] | 220 | unsigned long flags; |
| 221 | |
| 222 | spin_lock_irqsave(&channel->intent_lock, flags); |
| 223 | idr_destroy(&channel->liids); |
| 224 | spin_unlock_irqrestore(&channel->intent_lock, flags); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 225 | |
| 226 | kfree(channel->name); |
| 227 | kfree(channel); |
| 228 | } |
| 229 | |
| 230 | static size_t qcom_glink_rx_avail(struct qcom_glink *glink) |
| 231 | { |
| 232 | return glink->rx_pipe->avail(glink->rx_pipe); |
| 233 | } |
| 234 | |
| 235 | static void qcom_glink_rx_peak(struct qcom_glink *glink, |
| 236 | void *data, size_t count) |
| 237 | { |
| 238 | glink->rx_pipe->peak(glink->rx_pipe, data, count); |
| 239 | } |
| 240 | |
| 241 | static void qcom_glink_rx_advance(struct qcom_glink *glink, size_t count) |
| 242 | { |
| 243 | glink->rx_pipe->advance(glink->rx_pipe, count); |
| 244 | } |
| 245 | |
| 246 | static size_t qcom_glink_tx_avail(struct qcom_glink *glink) |
| 247 | { |
| 248 | return glink->tx_pipe->avail(glink->tx_pipe); |
| 249 | } |
| 250 | |
| 251 | static void qcom_glink_tx_write(struct qcom_glink *glink, |
| 252 | const void *hdr, size_t hlen, |
| 253 | const void *data, size_t dlen) |
| 254 | { |
| 255 | glink->tx_pipe->write(glink->tx_pipe, hdr, hlen, data, dlen); |
| 256 | } |
| 257 | |
| 258 | static int qcom_glink_tx(struct qcom_glink *glink, |
| 259 | const void *hdr, size_t hlen, |
| 260 | const void *data, size_t dlen, bool wait) |
| 261 | { |
| 262 | unsigned int tlen = hlen + dlen; |
| 263 | int ret; |
| 264 | |
| 265 | /* Reject packets that are too big */ |
| 266 | if (tlen >= glink->tx_pipe->length) |
| 267 | return -EINVAL; |
| 268 | |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 269 | ret = mutex_lock_interruptible(&glink->tx_lock); |
| 270 | if (ret) |
| 271 | return ret; |
| 272 | |
| 273 | while (qcom_glink_tx_avail(glink) < tlen) { |
| 274 | if (!wait) { |
Sricharan R | a7df9df | 2017-08-24 12:51:28 +0530 | [diff] [blame] | 275 | ret = -EAGAIN; |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 276 | goto out; |
| 277 | } |
| 278 | |
| 279 | usleep_range(10000, 15000); |
| 280 | } |
| 281 | |
| 282 | qcom_glink_tx_write(glink, hdr, hlen, data, dlen); |
| 283 | |
| 284 | mbox_send_message(glink->mbox_chan, NULL); |
| 285 | mbox_client_txdone(glink->mbox_chan, 0); |
| 286 | |
| 287 | out: |
| 288 | mutex_unlock(&glink->tx_lock); |
| 289 | |
| 290 | return ret; |
| 291 | } |
| 292 | |
| 293 | static int qcom_glink_send_version(struct qcom_glink *glink) |
| 294 | { |
| 295 | struct glink_msg msg; |
| 296 | |
| 297 | msg.cmd = cpu_to_le16(RPM_CMD_VERSION); |
Sricharan R | d31ad61 | 2017-08-24 12:51:32 +0530 | [diff] [blame] | 298 | msg.param1 = cpu_to_le16(GLINK_VERSION_1); |
| 299 | msg.param2 = cpu_to_le32(glink->features); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 300 | |
| 301 | return qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true); |
| 302 | } |
| 303 | |
| 304 | static void qcom_glink_send_version_ack(struct qcom_glink *glink) |
| 305 | { |
| 306 | struct glink_msg msg; |
| 307 | |
| 308 | msg.cmd = cpu_to_le16(RPM_CMD_VERSION_ACK); |
Sricharan R | d31ad61 | 2017-08-24 12:51:32 +0530 | [diff] [blame] | 309 | msg.param1 = cpu_to_le16(GLINK_VERSION_1); |
| 310 | msg.param2 = cpu_to_le32(glink->features); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 311 | |
| 312 | qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true); |
| 313 | } |
| 314 | |
| 315 | static void qcom_glink_send_open_ack(struct qcom_glink *glink, |
| 316 | struct glink_channel *channel) |
| 317 | { |
| 318 | struct glink_msg msg; |
| 319 | |
| 320 | msg.cmd = cpu_to_le16(RPM_CMD_OPEN_ACK); |
| 321 | msg.param1 = cpu_to_le16(channel->rcid); |
| 322 | msg.param2 = cpu_to_le32(0); |
| 323 | |
| 324 | qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * qcom_glink_send_open_req() - send a RPM_CMD_OPEN request to the remote |
| 329 | * @glink: Ptr to the glink edge |
| 330 | * @channel: Ptr to the channel that the open req is sent |
| 331 | * |
| 332 | * Allocates a local channel id and sends a RPM_CMD_OPEN message to the remote. |
| 333 | * Will return with refcount held, regardless of outcome. |
| 334 | * |
| 335 | * Returns 0 on success, negative errno otherwise. |
| 336 | */ |
| 337 | static int qcom_glink_send_open_req(struct qcom_glink *glink, |
| 338 | struct glink_channel *channel) |
| 339 | { |
| 340 | struct { |
| 341 | struct glink_msg msg; |
| 342 | u8 name[GLINK_NAME_SIZE]; |
| 343 | } __packed req; |
| 344 | int name_len = strlen(channel->name) + 1; |
| 345 | int req_len = ALIGN(sizeof(req.msg) + name_len, 8); |
| 346 | int ret; |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 347 | unsigned long flags; |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 348 | |
| 349 | kref_get(&channel->refcount); |
| 350 | |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 351 | spin_lock_irqsave(&glink->idr_lock, flags); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 352 | ret = idr_alloc_cyclic(&glink->lcids, channel, |
| 353 | RPM_GLINK_CID_MIN, RPM_GLINK_CID_MAX, |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 354 | GFP_ATOMIC); |
| 355 | spin_unlock_irqrestore(&glink->idr_lock, flags); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 356 | if (ret < 0) |
| 357 | return ret; |
| 358 | |
| 359 | channel->lcid = ret; |
| 360 | |
| 361 | req.msg.cmd = cpu_to_le16(RPM_CMD_OPEN); |
| 362 | req.msg.param1 = cpu_to_le16(channel->lcid); |
| 363 | req.msg.param2 = cpu_to_le32(name_len); |
| 364 | strcpy(req.name, channel->name); |
| 365 | |
| 366 | ret = qcom_glink_tx(glink, &req, req_len, NULL, 0, true); |
| 367 | if (ret) |
| 368 | goto remove_idr; |
| 369 | |
| 370 | return 0; |
| 371 | |
| 372 | remove_idr: |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 373 | spin_lock_irqsave(&glink->idr_lock, flags); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 374 | idr_remove(&glink->lcids, channel->lcid); |
| 375 | channel->lcid = 0; |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 376 | spin_unlock_irqrestore(&glink->idr_lock, flags); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 377 | |
| 378 | return ret; |
| 379 | } |
| 380 | |
| 381 | static void qcom_glink_send_close_req(struct qcom_glink *glink, |
| 382 | struct glink_channel *channel) |
| 383 | { |
| 384 | struct glink_msg req; |
| 385 | |
| 386 | req.cmd = cpu_to_le16(RPM_CMD_CLOSE); |
| 387 | req.param1 = cpu_to_le16(channel->lcid); |
| 388 | req.param2 = 0; |
| 389 | |
| 390 | qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true); |
| 391 | } |
| 392 | |
| 393 | static void qcom_glink_send_close_ack(struct qcom_glink *glink, |
| 394 | unsigned int rcid) |
| 395 | { |
| 396 | struct glink_msg req; |
| 397 | |
| 398 | req.cmd = cpu_to_le16(RPM_CMD_CLOSE_ACK); |
| 399 | req.param1 = cpu_to_le16(rcid); |
| 400 | req.param2 = 0; |
| 401 | |
| 402 | qcom_glink_tx(glink, &req, sizeof(req), NULL, 0, true); |
| 403 | } |
| 404 | |
Sricharan R | d31ad61 | 2017-08-24 12:51:32 +0530 | [diff] [blame] | 405 | /** |
| 406 | * qcom_glink_receive_version() - receive version/features from remote system |
| 407 | * |
| 408 | * @glink: pointer to transport interface |
| 409 | * @r_version: remote version |
| 410 | * @r_features: remote features |
| 411 | * |
| 412 | * This function is called in response to a remote-initiated version/feature |
| 413 | * negotiation sequence. |
| 414 | */ |
| 415 | static void qcom_glink_receive_version(struct qcom_glink *glink, |
| 416 | u32 version, |
| 417 | u32 features) |
| 418 | { |
| 419 | switch (version) { |
| 420 | case 0: |
| 421 | break; |
| 422 | case GLINK_VERSION_1: |
| 423 | glink->features &= features; |
| 424 | /* FALLTHROUGH */ |
| 425 | default: |
| 426 | qcom_glink_send_version_ack(glink); |
| 427 | break; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * qcom_glink_receive_version_ack() - receive negotiation ack from remote system |
| 433 | * |
| 434 | * @glink: pointer to transport interface |
| 435 | * @r_version: remote version response |
| 436 | * @r_features: remote features response |
| 437 | * |
| 438 | * This function is called in response to a local-initiated version/feature |
| 439 | * negotiation sequence and is the counter-offer from the remote side based |
| 440 | * upon the initial version and feature set requested. |
| 441 | */ |
| 442 | static void qcom_glink_receive_version_ack(struct qcom_glink *glink, |
| 443 | u32 version, |
| 444 | u32 features) |
| 445 | { |
| 446 | switch (version) { |
| 447 | case 0: |
| 448 | /* Version negotiation failed */ |
| 449 | break; |
| 450 | case GLINK_VERSION_1: |
| 451 | if (features == glink->features) |
| 452 | break; |
| 453 | |
| 454 | glink->features &= features; |
| 455 | /* FALLTHROUGH */ |
| 456 | default: |
| 457 | qcom_glink_send_version(glink); |
| 458 | break; |
| 459 | } |
| 460 | } |
| 461 | |
Sricharan R | 933b45d | 2017-08-24 12:51:34 +0530 | [diff] [blame^] | 462 | /** |
| 463 | * qcom_glink_send_intent_req_ack() - convert an rx intent request ack cmd to |
| 464 | wire format and transmit |
| 465 | * @glink: The transport to transmit on. |
| 466 | * @channel: The glink channel |
| 467 | * @granted: The request response to encode. |
| 468 | * |
| 469 | * Return: 0 on success or standard Linux error code. |
| 470 | */ |
| 471 | static int qcom_glink_send_intent_req_ack(struct qcom_glink *glink, |
| 472 | struct glink_channel *channel, |
| 473 | bool granted) |
| 474 | { |
| 475 | struct glink_msg msg; |
| 476 | |
| 477 | msg.cmd = cpu_to_le16(RPM_CMD_RX_INTENT_REQ_ACK); |
| 478 | msg.param1 = cpu_to_le16(channel->lcid); |
| 479 | msg.param2 = cpu_to_le32(granted); |
| 480 | |
| 481 | qcom_glink_tx(glink, &msg, sizeof(msg), NULL, 0, true); |
| 482 | |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * qcom_glink_advertise_intent - convert an rx intent cmd to wire format and |
| 488 | * transmit |
| 489 | * @glink: The transport to transmit on. |
| 490 | * @channel: The local channel |
| 491 | * @size: The intent to pass on to remote. |
| 492 | * |
| 493 | * Return: 0 on success or standard Linux error code. |
| 494 | */ |
| 495 | static int qcom_glink_advertise_intent(struct qcom_glink *glink, |
| 496 | struct glink_channel *channel, |
| 497 | struct glink_core_rx_intent *intent) |
| 498 | { |
| 499 | struct command { |
| 500 | u16 id; |
| 501 | u16 lcid; |
| 502 | u32 count; |
| 503 | u32 size; |
| 504 | u32 liid; |
| 505 | } __packed; |
| 506 | struct command cmd; |
| 507 | |
| 508 | cmd.id = cpu_to_le16(RPM_CMD_INTENT); |
| 509 | cmd.lcid = cpu_to_le16(channel->lcid); |
| 510 | cmd.count = cpu_to_le32(1); |
| 511 | cmd.size = cpu_to_le32(intent->size); |
| 512 | cmd.liid = cpu_to_le32(intent->id); |
| 513 | |
| 514 | qcom_glink_tx(glink, &cmd, sizeof(cmd), NULL, 0, true); |
| 515 | |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | static struct glink_core_rx_intent * |
| 520 | qcom_glink_alloc_intent(struct qcom_glink *glink, |
| 521 | struct glink_channel *channel, |
| 522 | size_t size, |
| 523 | bool reuseable) |
| 524 | { |
| 525 | struct glink_core_rx_intent *intent; |
| 526 | int ret; |
| 527 | unsigned long flags; |
| 528 | |
| 529 | intent = kzalloc(sizeof(*intent), GFP_KERNEL); |
| 530 | |
| 531 | if (!intent) |
| 532 | return NULL; |
| 533 | |
| 534 | intent->data = kzalloc(size, GFP_KERNEL); |
| 535 | if (!intent->data) |
| 536 | return NULL; |
| 537 | |
| 538 | spin_lock_irqsave(&channel->intent_lock, flags); |
| 539 | ret = idr_alloc_cyclic(&channel->liids, intent, 1, -1, GFP_ATOMIC); |
| 540 | if (ret < 0) { |
| 541 | spin_unlock_irqrestore(&channel->intent_lock, flags); |
| 542 | return NULL; |
| 543 | } |
| 544 | spin_unlock_irqrestore(&channel->intent_lock, flags); |
| 545 | |
| 546 | intent->id = ret; |
| 547 | intent->size = size; |
| 548 | intent->reuse = reuseable; |
| 549 | |
| 550 | return intent; |
| 551 | } |
| 552 | |
| 553 | /** |
| 554 | * qcom_glink_handle_intent_req() - Receive a request for rx_intent |
| 555 | * from remote side |
| 556 | * if_ptr: Pointer to the transport interface |
| 557 | * rcid: Remote channel ID |
| 558 | * size: size of the intent |
| 559 | * |
| 560 | * The function searches for the local channel to which the request for |
| 561 | * rx_intent has arrived and allocates and notifies the remote back |
| 562 | */ |
| 563 | static void qcom_glink_handle_intent_req(struct qcom_glink *glink, |
| 564 | u32 cid, size_t size) |
| 565 | { |
| 566 | struct glink_core_rx_intent *intent; |
| 567 | struct glink_channel *channel; |
| 568 | unsigned long flags; |
| 569 | |
| 570 | spin_lock_irqsave(&glink->idr_lock, flags); |
| 571 | channel = idr_find(&glink->rcids, cid); |
| 572 | spin_unlock_irqrestore(&glink->idr_lock, flags); |
| 573 | |
| 574 | if (!channel) { |
| 575 | pr_err("%s channel not found for cid %d\n", __func__, cid); |
| 576 | return; |
| 577 | } |
| 578 | |
| 579 | intent = qcom_glink_alloc_intent(glink, channel, size, false); |
| 580 | if (intent) |
| 581 | qcom_glink_advertise_intent(glink, channel, intent); |
| 582 | |
| 583 | qcom_glink_send_intent_req_ack(glink, channel, !!intent); |
| 584 | } |
| 585 | |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 586 | static int qcom_glink_rx_defer(struct qcom_glink *glink, size_t extra) |
| 587 | { |
| 588 | struct glink_defer_cmd *dcmd; |
| 589 | |
| 590 | extra = ALIGN(extra, 8); |
| 591 | |
| 592 | if (qcom_glink_rx_avail(glink) < sizeof(struct glink_msg) + extra) { |
| 593 | dev_dbg(glink->dev, "Insufficient data in rx fifo"); |
| 594 | return -ENXIO; |
| 595 | } |
| 596 | |
| 597 | dcmd = kzalloc(sizeof(*dcmd) + extra, GFP_ATOMIC); |
| 598 | if (!dcmd) |
| 599 | return -ENOMEM; |
| 600 | |
| 601 | INIT_LIST_HEAD(&dcmd->node); |
| 602 | |
| 603 | qcom_glink_rx_peak(glink, &dcmd->msg, sizeof(dcmd->msg) + extra); |
| 604 | |
| 605 | spin_lock(&glink->rx_lock); |
| 606 | list_add_tail(&dcmd->node, &glink->rx_queue); |
| 607 | spin_unlock(&glink->rx_lock); |
| 608 | |
| 609 | schedule_work(&glink->rx_work); |
| 610 | qcom_glink_rx_advance(glink, sizeof(dcmd->msg) + extra); |
| 611 | |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | static int qcom_glink_rx_data(struct qcom_glink *glink, size_t avail) |
| 616 | { |
| 617 | struct glink_channel *channel; |
| 618 | struct { |
| 619 | struct glink_msg msg; |
| 620 | __le32 chunk_size; |
| 621 | __le32 left_size; |
| 622 | } __packed hdr; |
| 623 | unsigned int chunk_size; |
| 624 | unsigned int left_size; |
| 625 | unsigned int rcid; |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 626 | unsigned long flags; |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 627 | |
| 628 | if (avail < sizeof(hdr)) { |
| 629 | dev_dbg(glink->dev, "Not enough data in fifo\n"); |
| 630 | return -EAGAIN; |
| 631 | } |
| 632 | |
| 633 | qcom_glink_rx_peak(glink, &hdr, sizeof(hdr)); |
| 634 | chunk_size = le32_to_cpu(hdr.chunk_size); |
| 635 | left_size = le32_to_cpu(hdr.left_size); |
| 636 | |
| 637 | if (avail < sizeof(hdr) + chunk_size) { |
| 638 | dev_dbg(glink->dev, "Payload not yet in fifo\n"); |
| 639 | return -EAGAIN; |
| 640 | } |
| 641 | |
| 642 | if (WARN(chunk_size % 4, "Incoming data must be word aligned\n")) |
| 643 | return -EINVAL; |
| 644 | |
| 645 | rcid = le16_to_cpu(hdr.msg.param1); |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 646 | spin_lock_irqsave(&glink->idr_lock, flags); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 647 | channel = idr_find(&glink->rcids, rcid); |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 648 | spin_unlock_irqrestore(&glink->idr_lock, flags); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 649 | if (!channel) { |
| 650 | dev_dbg(glink->dev, "Data on non-existing channel\n"); |
| 651 | |
| 652 | /* Drop the message */ |
| 653 | qcom_glink_rx_advance(glink, |
| 654 | ALIGN(sizeof(hdr) + chunk_size, 8)); |
| 655 | return 0; |
| 656 | } |
| 657 | |
| 658 | /* Might have an ongoing, fragmented, message to append */ |
| 659 | if (!channel->buf) { |
| 660 | channel->buf = kmalloc(chunk_size + left_size, GFP_ATOMIC); |
| 661 | if (!channel->buf) |
| 662 | return -ENOMEM; |
| 663 | |
| 664 | channel->buf_size = chunk_size + left_size; |
| 665 | channel->buf_offset = 0; |
| 666 | } |
| 667 | |
| 668 | qcom_glink_rx_advance(glink, sizeof(hdr)); |
| 669 | |
| 670 | if (channel->buf_size - channel->buf_offset < chunk_size) { |
| 671 | dev_err(glink->dev, "Insufficient space in input buffer\n"); |
| 672 | |
| 673 | /* The packet header lied, drop payload */ |
| 674 | qcom_glink_rx_advance(glink, chunk_size); |
| 675 | return -ENOMEM; |
| 676 | } |
| 677 | |
| 678 | qcom_glink_rx_peak(glink, channel->buf + channel->buf_offset, |
| 679 | chunk_size); |
| 680 | channel->buf_offset += chunk_size; |
| 681 | |
| 682 | /* Handle message when no fragments remain to be received */ |
| 683 | if (!left_size) { |
| 684 | spin_lock(&channel->recv_lock); |
| 685 | if (channel->ept.cb) { |
| 686 | channel->ept.cb(channel->ept.rpdev, |
| 687 | channel->buf, |
| 688 | channel->buf_offset, |
| 689 | channel->ept.priv, |
| 690 | RPMSG_ADDR_ANY); |
| 691 | } |
| 692 | spin_unlock(&channel->recv_lock); |
| 693 | |
| 694 | kfree(channel->buf); |
| 695 | channel->buf = NULL; |
| 696 | channel->buf_size = 0; |
| 697 | } |
| 698 | |
| 699 | /* Each message starts at 8 byte aligned address */ |
| 700 | qcom_glink_rx_advance(glink, ALIGN(chunk_size, 8)); |
| 701 | |
| 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | static int qcom_glink_rx_open_ack(struct qcom_glink *glink, unsigned int lcid) |
| 706 | { |
| 707 | struct glink_channel *channel; |
| 708 | |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 709 | spin_lock(&glink->idr_lock); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 710 | channel = idr_find(&glink->lcids, lcid); |
| 711 | if (!channel) { |
| 712 | dev_err(glink->dev, "Invalid open ack packet\n"); |
| 713 | return -EINVAL; |
| 714 | } |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 715 | spin_unlock(&glink->idr_lock); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 716 | |
| 717 | complete(&channel->open_ack); |
| 718 | |
| 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | static irqreturn_t qcom_glink_native_intr(int irq, void *data) |
| 723 | { |
| 724 | struct qcom_glink *glink = data; |
| 725 | struct glink_msg msg; |
| 726 | unsigned int param1; |
| 727 | unsigned int param2; |
| 728 | unsigned int avail; |
| 729 | unsigned int cmd; |
| 730 | int ret; |
| 731 | |
| 732 | for (;;) { |
| 733 | avail = qcom_glink_rx_avail(glink); |
| 734 | if (avail < sizeof(msg)) |
| 735 | break; |
| 736 | |
| 737 | qcom_glink_rx_peak(glink, &msg, sizeof(msg)); |
| 738 | |
| 739 | cmd = le16_to_cpu(msg.cmd); |
| 740 | param1 = le16_to_cpu(msg.param1); |
| 741 | param2 = le32_to_cpu(msg.param2); |
| 742 | |
| 743 | switch (cmd) { |
| 744 | case RPM_CMD_VERSION: |
| 745 | case RPM_CMD_VERSION_ACK: |
| 746 | case RPM_CMD_CLOSE: |
| 747 | case RPM_CMD_CLOSE_ACK: |
Sricharan R | 933b45d | 2017-08-24 12:51:34 +0530 | [diff] [blame^] | 748 | case RPM_CMD_RX_INTENT_REQ: |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 749 | ret = qcom_glink_rx_defer(glink, 0); |
| 750 | break; |
| 751 | case RPM_CMD_OPEN_ACK: |
| 752 | ret = qcom_glink_rx_open_ack(glink, param1); |
| 753 | qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8)); |
| 754 | break; |
| 755 | case RPM_CMD_OPEN: |
| 756 | ret = qcom_glink_rx_defer(glink, param2); |
| 757 | break; |
| 758 | case RPM_CMD_TX_DATA: |
| 759 | case RPM_CMD_TX_DATA_CONT: |
| 760 | ret = qcom_glink_rx_data(glink, avail); |
| 761 | break; |
| 762 | case RPM_CMD_READ_NOTIF: |
| 763 | qcom_glink_rx_advance(glink, ALIGN(sizeof(msg), 8)); |
| 764 | |
| 765 | mbox_send_message(glink->mbox_chan, NULL); |
| 766 | mbox_client_txdone(glink->mbox_chan, 0); |
| 767 | |
| 768 | ret = 0; |
| 769 | break; |
| 770 | default: |
| 771 | dev_err(glink->dev, "unhandled rx cmd: %d\n", cmd); |
| 772 | ret = -EINVAL; |
| 773 | break; |
| 774 | } |
| 775 | |
| 776 | if (ret) |
| 777 | break; |
| 778 | } |
| 779 | |
| 780 | return IRQ_HANDLED; |
| 781 | } |
| 782 | |
| 783 | /* Locally initiated rpmsg_create_ept */ |
| 784 | static struct glink_channel *qcom_glink_create_local(struct qcom_glink *glink, |
| 785 | const char *name) |
| 786 | { |
| 787 | struct glink_channel *channel; |
| 788 | int ret; |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 789 | unsigned long flags; |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 790 | |
| 791 | channel = qcom_glink_alloc_channel(glink, name); |
| 792 | if (IS_ERR(channel)) |
| 793 | return ERR_CAST(channel); |
| 794 | |
| 795 | ret = qcom_glink_send_open_req(glink, channel); |
| 796 | if (ret) |
| 797 | goto release_channel; |
| 798 | |
| 799 | ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ); |
| 800 | if (!ret) |
| 801 | goto err_timeout; |
| 802 | |
| 803 | ret = wait_for_completion_timeout(&channel->open_req, 5 * HZ); |
| 804 | if (!ret) |
| 805 | goto err_timeout; |
| 806 | |
| 807 | qcom_glink_send_open_ack(glink, channel); |
| 808 | |
| 809 | return channel; |
| 810 | |
| 811 | err_timeout: |
| 812 | /* qcom_glink_send_open_req() did register the channel in lcids*/ |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 813 | spin_lock_irqsave(&glink->idr_lock, flags); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 814 | idr_remove(&glink->lcids, channel->lcid); |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 815 | spin_unlock_irqrestore(&glink->idr_lock, flags); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 816 | |
| 817 | release_channel: |
| 818 | /* Release qcom_glink_send_open_req() reference */ |
| 819 | kref_put(&channel->refcount, qcom_glink_channel_release); |
| 820 | /* Release qcom_glink_alloc_channel() reference */ |
| 821 | kref_put(&channel->refcount, qcom_glink_channel_release); |
| 822 | |
| 823 | return ERR_PTR(-ETIMEDOUT); |
| 824 | } |
| 825 | |
| 826 | /* Remote initiated rpmsg_create_ept */ |
| 827 | static int qcom_glink_create_remote(struct qcom_glink *glink, |
| 828 | struct glink_channel *channel) |
| 829 | { |
| 830 | int ret; |
| 831 | |
| 832 | qcom_glink_send_open_ack(glink, channel); |
| 833 | |
| 834 | ret = qcom_glink_send_open_req(glink, channel); |
| 835 | if (ret) |
| 836 | goto close_link; |
| 837 | |
| 838 | ret = wait_for_completion_timeout(&channel->open_ack, 5 * HZ); |
| 839 | if (!ret) { |
| 840 | ret = -ETIMEDOUT; |
| 841 | goto close_link; |
| 842 | } |
| 843 | |
| 844 | return 0; |
| 845 | |
| 846 | close_link: |
| 847 | /* |
| 848 | * Send a close request to "undo" our open-ack. The close-ack will |
| 849 | * release the last reference. |
| 850 | */ |
| 851 | qcom_glink_send_close_req(glink, channel); |
| 852 | |
| 853 | /* Release qcom_glink_send_open_req() reference */ |
| 854 | kref_put(&channel->refcount, qcom_glink_channel_release); |
| 855 | |
| 856 | return ret; |
| 857 | } |
| 858 | |
| 859 | static struct rpmsg_endpoint *qcom_glink_create_ept(struct rpmsg_device *rpdev, |
| 860 | rpmsg_rx_cb_t cb, |
| 861 | void *priv, |
| 862 | struct rpmsg_channel_info |
| 863 | chinfo) |
| 864 | { |
| 865 | struct glink_channel *parent = to_glink_channel(rpdev->ept); |
| 866 | struct glink_channel *channel; |
| 867 | struct qcom_glink *glink = parent->glink; |
| 868 | struct rpmsg_endpoint *ept; |
| 869 | const char *name = chinfo.name; |
| 870 | int cid; |
| 871 | int ret; |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 872 | unsigned long flags; |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 873 | |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 874 | spin_lock_irqsave(&glink->idr_lock, flags); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 875 | idr_for_each_entry(&glink->rcids, channel, cid) { |
| 876 | if (!strcmp(channel->name, name)) |
| 877 | break; |
| 878 | } |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 879 | spin_unlock_irqrestore(&glink->idr_lock, flags); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 880 | |
| 881 | if (!channel) { |
| 882 | channel = qcom_glink_create_local(glink, name); |
| 883 | if (IS_ERR(channel)) |
| 884 | return NULL; |
| 885 | } else { |
| 886 | ret = qcom_glink_create_remote(glink, channel); |
| 887 | if (ret) |
| 888 | return NULL; |
| 889 | } |
| 890 | |
| 891 | ept = &channel->ept; |
| 892 | ept->rpdev = rpdev; |
| 893 | ept->cb = cb; |
| 894 | ept->priv = priv; |
| 895 | ept->ops = &glink_endpoint_ops; |
| 896 | |
| 897 | return ept; |
| 898 | } |
| 899 | |
| 900 | static void qcom_glink_destroy_ept(struct rpmsg_endpoint *ept) |
| 901 | { |
| 902 | struct glink_channel *channel = to_glink_channel(ept); |
| 903 | struct qcom_glink *glink = channel->glink; |
| 904 | unsigned long flags; |
| 905 | |
| 906 | spin_lock_irqsave(&channel->recv_lock, flags); |
| 907 | channel->ept.cb = NULL; |
| 908 | spin_unlock_irqrestore(&channel->recv_lock, flags); |
| 909 | |
| 910 | /* Decouple the potential rpdev from the channel */ |
| 911 | channel->rpdev = NULL; |
| 912 | |
| 913 | qcom_glink_send_close_req(glink, channel); |
| 914 | } |
| 915 | |
| 916 | static int __qcom_glink_send(struct glink_channel *channel, |
| 917 | void *data, int len, bool wait) |
| 918 | { |
| 919 | struct qcom_glink *glink = channel->glink; |
| 920 | struct { |
| 921 | struct glink_msg msg; |
| 922 | __le32 chunk_size; |
| 923 | __le32 left_size; |
| 924 | } __packed req; |
| 925 | |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 926 | req.msg.cmd = cpu_to_le16(RPM_CMD_TX_DATA); |
| 927 | req.msg.param1 = cpu_to_le16(channel->lcid); |
| 928 | req.msg.param2 = cpu_to_le32(channel->rcid); |
| 929 | req.chunk_size = cpu_to_le32(len); |
| 930 | req.left_size = cpu_to_le32(0); |
| 931 | |
| 932 | return qcom_glink_tx(glink, &req, sizeof(req), data, len, wait); |
| 933 | } |
| 934 | |
| 935 | static int qcom_glink_send(struct rpmsg_endpoint *ept, void *data, int len) |
| 936 | { |
| 937 | struct glink_channel *channel = to_glink_channel(ept); |
| 938 | |
| 939 | return __qcom_glink_send(channel, data, len, true); |
| 940 | } |
| 941 | |
| 942 | static int qcom_glink_trysend(struct rpmsg_endpoint *ept, void *data, int len) |
| 943 | { |
| 944 | struct glink_channel *channel = to_glink_channel(ept); |
| 945 | |
| 946 | return __qcom_glink_send(channel, data, len, false); |
| 947 | } |
| 948 | |
| 949 | /* |
| 950 | * Finds the device_node for the glink child interested in this channel. |
| 951 | */ |
| 952 | static struct device_node *qcom_glink_match_channel(struct device_node *node, |
| 953 | const char *channel) |
| 954 | { |
| 955 | struct device_node *child; |
| 956 | const char *name; |
| 957 | const char *key; |
| 958 | int ret; |
| 959 | |
| 960 | for_each_available_child_of_node(node, child) { |
| 961 | key = "qcom,glink-channels"; |
| 962 | ret = of_property_read_string(child, key, &name); |
| 963 | if (ret) |
| 964 | continue; |
| 965 | |
| 966 | if (strcmp(name, channel) == 0) |
| 967 | return child; |
| 968 | } |
| 969 | |
| 970 | return NULL; |
| 971 | } |
| 972 | |
| 973 | static const struct rpmsg_device_ops glink_device_ops = { |
| 974 | .create_ept = qcom_glink_create_ept, |
| 975 | }; |
| 976 | |
| 977 | static const struct rpmsg_endpoint_ops glink_endpoint_ops = { |
| 978 | .destroy_ept = qcom_glink_destroy_ept, |
| 979 | .send = qcom_glink_send, |
| 980 | .trysend = qcom_glink_trysend, |
| 981 | }; |
| 982 | |
| 983 | static void qcom_glink_rpdev_release(struct device *dev) |
| 984 | { |
| 985 | struct rpmsg_device *rpdev = to_rpmsg_device(dev); |
| 986 | struct glink_channel *channel = to_glink_channel(rpdev->ept); |
| 987 | |
| 988 | channel->rpdev = NULL; |
| 989 | kfree(rpdev); |
| 990 | } |
| 991 | |
| 992 | static int qcom_glink_rx_open(struct qcom_glink *glink, unsigned int rcid, |
| 993 | char *name) |
| 994 | { |
| 995 | struct glink_channel *channel; |
| 996 | struct rpmsg_device *rpdev; |
| 997 | bool create_device = false; |
| 998 | struct device_node *node; |
| 999 | int lcid; |
| 1000 | int ret; |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 1001 | unsigned long flags; |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 1002 | |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 1003 | spin_lock_irqsave(&glink->idr_lock, flags); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 1004 | idr_for_each_entry(&glink->lcids, channel, lcid) { |
| 1005 | if (!strcmp(channel->name, name)) |
| 1006 | break; |
| 1007 | } |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 1008 | spin_unlock_irqrestore(&glink->idr_lock, flags); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 1009 | |
| 1010 | if (!channel) { |
| 1011 | channel = qcom_glink_alloc_channel(glink, name); |
| 1012 | if (IS_ERR(channel)) |
| 1013 | return PTR_ERR(channel); |
| 1014 | |
| 1015 | /* The opening dance was initiated by the remote */ |
| 1016 | create_device = true; |
| 1017 | } |
| 1018 | |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 1019 | spin_lock_irqsave(&glink->idr_lock, flags); |
| 1020 | ret = idr_alloc(&glink->rcids, channel, rcid, rcid + 1, GFP_ATOMIC); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 1021 | if (ret < 0) { |
| 1022 | dev_err(glink->dev, "Unable to insert channel into rcid list\n"); |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 1023 | spin_unlock_irqrestore(&glink->idr_lock, flags); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 1024 | goto free_channel; |
| 1025 | } |
| 1026 | channel->rcid = ret; |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 1027 | spin_unlock_irqrestore(&glink->idr_lock, flags); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 1028 | |
| 1029 | complete(&channel->open_req); |
| 1030 | |
| 1031 | if (create_device) { |
| 1032 | rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL); |
| 1033 | if (!rpdev) { |
| 1034 | ret = -ENOMEM; |
| 1035 | goto rcid_remove; |
| 1036 | } |
| 1037 | |
| 1038 | rpdev->ept = &channel->ept; |
| 1039 | strncpy(rpdev->id.name, name, RPMSG_NAME_SIZE); |
| 1040 | rpdev->src = RPMSG_ADDR_ANY; |
| 1041 | rpdev->dst = RPMSG_ADDR_ANY; |
| 1042 | rpdev->ops = &glink_device_ops; |
| 1043 | |
| 1044 | node = qcom_glink_match_channel(glink->dev->of_node, name); |
| 1045 | rpdev->dev.of_node = node; |
| 1046 | rpdev->dev.parent = glink->dev; |
| 1047 | rpdev->dev.release = qcom_glink_rpdev_release; |
| 1048 | |
| 1049 | ret = rpmsg_register_device(rpdev); |
| 1050 | if (ret) |
| 1051 | goto free_rpdev; |
| 1052 | |
| 1053 | channel->rpdev = rpdev; |
| 1054 | } |
| 1055 | |
| 1056 | return 0; |
| 1057 | |
| 1058 | free_rpdev: |
| 1059 | kfree(rpdev); |
| 1060 | rcid_remove: |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 1061 | spin_lock_irqsave(&glink->idr_lock, flags); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 1062 | idr_remove(&glink->rcids, channel->rcid); |
| 1063 | channel->rcid = 0; |
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 | free_channel: |
| 1066 | /* Release the reference, iff we took it */ |
| 1067 | if (create_device) |
| 1068 | kref_put(&channel->refcount, qcom_glink_channel_release); |
| 1069 | |
| 1070 | return ret; |
| 1071 | } |
| 1072 | |
| 1073 | static void qcom_glink_rx_close(struct qcom_glink *glink, unsigned int rcid) |
| 1074 | { |
| 1075 | struct rpmsg_channel_info chinfo; |
| 1076 | struct glink_channel *channel; |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 1077 | unsigned long flags; |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 1078 | |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 1079 | spin_lock_irqsave(&glink->idr_lock, flags); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 1080 | channel = idr_find(&glink->rcids, rcid); |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 1081 | spin_unlock_irqrestore(&glink->idr_lock, flags); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 1082 | if (WARN(!channel, "close request on unknown channel\n")) |
| 1083 | return; |
| 1084 | |
| 1085 | if (channel->rpdev) { |
| 1086 | strncpy(chinfo.name, channel->name, sizeof(chinfo.name)); |
| 1087 | chinfo.src = RPMSG_ADDR_ANY; |
| 1088 | chinfo.dst = RPMSG_ADDR_ANY; |
| 1089 | |
| 1090 | rpmsg_unregister_device(glink->dev, &chinfo); |
| 1091 | } |
| 1092 | |
| 1093 | qcom_glink_send_close_ack(glink, channel->rcid); |
| 1094 | |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 1095 | spin_lock_irqsave(&glink->idr_lock, flags); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 1096 | idr_remove(&glink->rcids, channel->rcid); |
| 1097 | channel->rcid = 0; |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 1098 | spin_unlock_irqrestore(&glink->idr_lock, flags); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 1099 | |
| 1100 | kref_put(&channel->refcount, qcom_glink_channel_release); |
| 1101 | } |
| 1102 | |
| 1103 | static void qcom_glink_rx_close_ack(struct qcom_glink *glink, unsigned int lcid) |
| 1104 | { |
| 1105 | struct glink_channel *channel; |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 1106 | unsigned long flags; |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 1107 | |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 1108 | spin_lock_irqsave(&glink->idr_lock, flags); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 1109 | channel = idr_find(&glink->lcids, lcid); |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 1110 | if (WARN(!channel, "close ack on unknown channel\n")) { |
| 1111 | spin_unlock_irqrestore(&glink->idr_lock, flags); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 1112 | return; |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 1113 | } |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 1114 | |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 1115 | idr_remove(&glink->lcids, channel->lcid); |
| 1116 | channel->lcid = 0; |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 1117 | spin_unlock_irqrestore(&glink->idr_lock, flags); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 1118 | |
| 1119 | kref_put(&channel->refcount, qcom_glink_channel_release); |
| 1120 | } |
| 1121 | |
| 1122 | static void qcom_glink_work(struct work_struct *work) |
| 1123 | { |
| 1124 | struct qcom_glink *glink = container_of(work, struct qcom_glink, |
| 1125 | rx_work); |
| 1126 | struct glink_defer_cmd *dcmd; |
| 1127 | struct glink_msg *msg; |
| 1128 | unsigned long flags; |
| 1129 | unsigned int param1; |
| 1130 | unsigned int param2; |
| 1131 | unsigned int cmd; |
| 1132 | |
| 1133 | for (;;) { |
| 1134 | spin_lock_irqsave(&glink->rx_lock, flags); |
| 1135 | if (list_empty(&glink->rx_queue)) { |
| 1136 | spin_unlock_irqrestore(&glink->rx_lock, flags); |
| 1137 | break; |
| 1138 | } |
| 1139 | dcmd = list_first_entry(&glink->rx_queue, |
| 1140 | struct glink_defer_cmd, node); |
| 1141 | list_del(&dcmd->node); |
| 1142 | spin_unlock_irqrestore(&glink->rx_lock, flags); |
| 1143 | |
| 1144 | msg = &dcmd->msg; |
| 1145 | cmd = le16_to_cpu(msg->cmd); |
| 1146 | param1 = le16_to_cpu(msg->param1); |
| 1147 | param2 = le32_to_cpu(msg->param2); |
| 1148 | |
| 1149 | switch (cmd) { |
| 1150 | case RPM_CMD_VERSION: |
Sricharan R | d31ad61 | 2017-08-24 12:51:32 +0530 | [diff] [blame] | 1151 | qcom_glink_receive_version(glink, param1, param2); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 1152 | break; |
| 1153 | case RPM_CMD_VERSION_ACK: |
Sricharan R | d31ad61 | 2017-08-24 12:51:32 +0530 | [diff] [blame] | 1154 | qcom_glink_receive_version_ack(glink, param1, param2); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 1155 | break; |
| 1156 | case RPM_CMD_OPEN: |
| 1157 | qcom_glink_rx_open(glink, param1, msg->data); |
| 1158 | break; |
| 1159 | case RPM_CMD_CLOSE: |
| 1160 | qcom_glink_rx_close(glink, param1); |
| 1161 | break; |
| 1162 | case RPM_CMD_CLOSE_ACK: |
| 1163 | qcom_glink_rx_close_ack(glink, param1); |
| 1164 | break; |
Sricharan R | 933b45d | 2017-08-24 12:51:34 +0530 | [diff] [blame^] | 1165 | case RPM_CMD_RX_INTENT_REQ: |
| 1166 | qcom_glink_handle_intent_req(glink, param1, param2); |
| 1167 | break; |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 1168 | default: |
| 1169 | WARN(1, "Unknown defer object %d\n", cmd); |
| 1170 | break; |
| 1171 | } |
| 1172 | |
| 1173 | kfree(dcmd); |
| 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | struct qcom_glink *qcom_glink_native_probe(struct device *dev, |
Sricharan R | d31ad61 | 2017-08-24 12:51:32 +0530 | [diff] [blame] | 1178 | unsigned long features, |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 1179 | struct qcom_glink_pipe *rx, |
Sricharan R | 933b45d | 2017-08-24 12:51:34 +0530 | [diff] [blame^] | 1180 | struct qcom_glink_pipe *tx, |
| 1181 | bool intentless) |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 1182 | { |
| 1183 | int irq; |
| 1184 | int ret; |
| 1185 | struct qcom_glink *glink; |
| 1186 | |
| 1187 | glink = devm_kzalloc(dev, sizeof(*glink), GFP_KERNEL); |
| 1188 | if (!glink) |
| 1189 | return ERR_PTR(-ENOMEM); |
| 1190 | |
| 1191 | glink->dev = dev; |
| 1192 | glink->tx_pipe = tx; |
| 1193 | glink->rx_pipe = rx; |
| 1194 | |
Sricharan R | d31ad61 | 2017-08-24 12:51:32 +0530 | [diff] [blame] | 1195 | glink->features = features; |
Sricharan R | 933b45d | 2017-08-24 12:51:34 +0530 | [diff] [blame^] | 1196 | glink->intentless = intentless; |
Sricharan R | d31ad61 | 2017-08-24 12:51:32 +0530 | [diff] [blame] | 1197 | |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 1198 | mutex_init(&glink->tx_lock); |
| 1199 | spin_lock_init(&glink->rx_lock); |
| 1200 | INIT_LIST_HEAD(&glink->rx_queue); |
| 1201 | INIT_WORK(&glink->rx_work, qcom_glink_work); |
| 1202 | |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 1203 | spin_lock_init(&glink->idr_lock); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 1204 | idr_init(&glink->lcids); |
| 1205 | idr_init(&glink->rcids); |
| 1206 | |
| 1207 | glink->mbox_client.dev = dev; |
| 1208 | glink->mbox_chan = mbox_request_channel(&glink->mbox_client, 0); |
| 1209 | if (IS_ERR(glink->mbox_chan)) { |
| 1210 | if (PTR_ERR(glink->mbox_chan) != -EPROBE_DEFER) |
| 1211 | dev_err(dev, "failed to acquire IPC channel\n"); |
| 1212 | return ERR_CAST(glink->mbox_chan); |
| 1213 | } |
| 1214 | |
| 1215 | irq = of_irq_get(dev->of_node, 0); |
| 1216 | ret = devm_request_irq(dev, irq, |
| 1217 | qcom_glink_native_intr, |
| 1218 | IRQF_NO_SUSPEND | IRQF_SHARED, |
| 1219 | "glink-native", glink); |
| 1220 | if (ret) { |
| 1221 | dev_err(dev, "failed to request IRQ\n"); |
| 1222 | return ERR_PTR(ret); |
| 1223 | } |
| 1224 | |
| 1225 | glink->irq = irq; |
| 1226 | |
| 1227 | ret = qcom_glink_send_version(glink); |
| 1228 | if (ret) |
| 1229 | return ERR_PTR(ret); |
| 1230 | |
| 1231 | return glink; |
| 1232 | } |
| 1233 | |
| 1234 | static int qcom_glink_remove_device(struct device *dev, void *data) |
| 1235 | { |
| 1236 | device_unregister(dev); |
| 1237 | |
| 1238 | return 0; |
| 1239 | } |
| 1240 | |
| 1241 | void qcom_glink_native_remove(struct qcom_glink *glink) |
| 1242 | { |
| 1243 | struct glink_channel *channel; |
| 1244 | int cid; |
| 1245 | int ret; |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 1246 | unsigned long flags; |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 1247 | |
| 1248 | disable_irq(glink->irq); |
| 1249 | cancel_work_sync(&glink->rx_work); |
| 1250 | |
| 1251 | ret = device_for_each_child(glink->dev, NULL, qcom_glink_remove_device); |
| 1252 | if (ret) |
| 1253 | dev_warn(glink->dev, "Can't remove GLINK devices: %d\n", ret); |
| 1254 | |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 1255 | spin_lock_irqsave(&glink->idr_lock, flags); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 1256 | /* Release any defunct local channels, waiting for close-ack */ |
| 1257 | idr_for_each_entry(&glink->lcids, channel, cid) |
| 1258 | kref_put(&channel->refcount, qcom_glink_channel_release); |
| 1259 | |
| 1260 | idr_destroy(&glink->lcids); |
| 1261 | idr_destroy(&glink->rcids); |
Sricharan R | 44f6df9 | 2017-08-24 12:51:33 +0530 | [diff] [blame] | 1262 | spin_unlock_irqrestore(&glink->idr_lock, flags); |
Sricharan R | 76cf110 | 2017-08-24 12:51:29 +0530 | [diff] [blame] | 1263 | mbox_free_channel(glink->mbox_chan); |
Bjorn Andersson | 835764dd | 2017-08-24 12:51:26 +0530 | [diff] [blame] | 1264 | } |
Bjorn Andersson | caf989c | 2017-08-24 12:51:30 +0530 | [diff] [blame] | 1265 | |
| 1266 | void qcom_glink_native_unregister(struct qcom_glink *glink) |
| 1267 | { |
| 1268 | device_unregister(glink->dev); |
| 1269 | } |