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