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