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