1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Stream data over Thunderbolt/USB4 cable 4 * 5 * Copyright (C) 2026, Intel Corporation 6 * Authors: Alan Borzeszkowski <alan.borzeszkowski@linux.intel.com> 7 * Mika Westerberg <mika.westerberg@linux.intel.com> 8 */ 9 10 #define pr_fmt(fmt) "tbstream: " fmt 11 12 #include <linux/delay.h> 13 #include <linux/configfs.h> 14 #include <linux/file.h> 15 #include <linux/fs.h> 16 #include <linux/idr.h> 17 #include <linux/ktime.h> 18 #include <linux/miscdevice.h> 19 #include <linux/module.h> 20 #include <linux/mutex.h> 21 #include <linux/poll.h> 22 #include <linux/sizes.h> 23 #include <linux/thunderbolt.h> 24 #include <linux/uaccess.h> 25 #include <linux/uio.h> 26 #include <linux/uuid.h> 27 #include <linux/wait.h> 28 29 /* 30 * USB4STREAM - Stream data directly over Thunderbolt/USB4 cable 31 * 32 * HopIDs are configured by the user. In Linux this is done through 33 * ConfigFS. Once that is done paths are be established the first time 34 * the stream is opened. Typically the read side is opened first to make 35 * sure all the data will be received. 36 * 37 * End-to-end flow control is mandatory on both sides. 38 * 39 * Data is sent to the other side as tunneled DATA packets. All the data 40 * is owned by the user and passed as-is from the writer to the reader. 41 * 42 * Once the stream device is closed, a CLOSE packet is sent to the peer 43 * so it can take the necessary action. On Linux this typically results 44 * in EOF being returned to the reader. 45 * 46 * Tunneled packet types: 47 * 48 * +-------+---------+------------------+ 49 * | PDF | Type | Payload size | 50 * +-------+---------+------------------+ 51 * | 2 | DATA | up to 4 KiB | 52 * | 3 | CLOSE | up to 256 bytes | 53 * +-------+---------+------------------+ 54 * 55 * Each stream can optionally publish configuration values under its own 56 * XDomain property directory. The name of the directory is the name of 57 * the stream in question and the UUID is up to the stream. For example 58 * if the stream exposes video output then the directory name could be 59 * "video". 60 * 61 * Below values are reserved and can be used by the stream: 62 * 63 * +----------+-----------+-------------------------+ 64 * | Key | Type | Contents | 65 * +----------+-----------+-------------------------+ 66 * | inhopid | IMMEDIATE | Configured input HopID | 67 * | outhopid | IMMEDIATE | Configured output HopID | 68 * +----------+-----------+-------------------------+ 69 * 70 * It is allowed to add more stream specific properties as well if the 71 * above are not enough. 72 */ 73 74 #define TBSTREAM_DEV_RING_SIZE 256 75 #define TBSTREAM_DEV_MIN_RING_SIZE 32 76 #define TBSTREAM_DEV_MAX_RING_SIZE 4096 77 #define TBSTREAM_DEV_THROTTLING 8192 78 #define TBSTREAM_DEV_MAX_THROTTLING 16776960 79 80 /** 81 * enum tbstream_frame_pdf - PDF numbers for tunneled frames 82 * @TBSTREAM_FRAME_START: PDF of the start of the frame 83 * @TBSTREAM_DATA: PDF of the DATA frame 84 * @TBSTREAM_CLOSE: PDF of the CLOSE frame 85 */ 86 enum tbstream_frame_pdf { 87 TBSTREAM_FRAME_START = 1, 88 TBSTREAM_DATA, 89 TBSTREAM_CLOSE, 90 }; 91 92 /** 93 * struct tbstream_frame - Frame submitted to/from the rings 94 * @sdev: Pointer to the stream device 95 * @page: Page holding the packet 96 * @offset: Offset inside @page if partial read is done 97 * @completed: %true if the RX frame is completed 98 * @frame: Underlying frame structure 99 */ 100 struct tbstream_frame { 101 struct tbstream_dev *sdev; 102 struct page *page; 103 unsigned int offset; 104 bool completed; 105 struct ring_frame frame; 106 }; 107 108 /** 109 * struct tbstream_ring - Stream RX/TX ring structure 110 * @ring: Pointer to the API ring 111 * @prod: Current value of producer 112 * @cons: Current value of consumer 113 * @frames: Holds the ring frames 114 */ 115 struct tbstream_ring { 116 struct tb_ring *ring; 117 unsigned long prod; 118 unsigned long cons; 119 struct tbstream_frame *frames; 120 }; 121 122 /** 123 * struct tbstream_dev - Stream character device 124 * @group: ConfigFS group for this device 125 * @stream: Pointer to the stream if it is attached (%NULL otherwise) 126 * @misc: Character device used for tunneling 127 * @kref: Reference count 128 * @index: Unique identifier for the character device 129 * @in_hopid: In HopID 130 * @out_hopid: Out HopID 131 * @ring_size: Size of the rings 132 * @throttling: Interrupt throttling rate in ns 133 * @busy_poll: Instead of interrupts, busy poll the rings 134 * @rx_pending: Receive ring has completions that need to be advanced 135 * @users: Number of times @cdev has been opened 136 * @close_received: CLOSE packet appeared in the RX ring 137 * @closed: CLOSE packet was handled in the read side 138 * @removed: Userspace removed the ConfigFS group underneath. 139 * @wait: Waitqueue for open, read and write 140 * @lock: Lock protecting this structure 141 * @tx_ring: Transmit ring 142 * @rx_ring: Receive ring 143 * @list: Stream devices are linked through this 144 * 145 * @close_received is used on write side to notify the writer that the 146 * other side sent CLOSE. @closed on the other hand is used on the read 147 * side to make read(2) return EOF to the caller. 148 */ 149 struct tbstream_dev { 150 struct config_group group; 151 struct tbstream *stream; 152 struct miscdevice misc; 153 struct kref kref; 154 int index; 155 int in_hopid; 156 int out_hopid; 157 unsigned int ring_size; 158 unsigned int throttling; 159 bool busy_poll; 160 bool rx_pending; 161 int users; 162 bool close_received; 163 bool closed; 164 bool removed; 165 wait_queue_head_t wait; 166 struct mutex lock; 167 struct tbstream_ring tx_ring; 168 struct tbstream_ring rx_ring; 169 struct list_head list; 170 }; 171 172 /** 173 * struct tbstream_group - Config group for stream 174 * @group: ConfigFS group for @stream 175 * @stream: Stream the ConfigFS group is attached to. %NULL if there is 176 * no stream attached. 177 * @lock: Lock protecting this structure 178 * @dev_list: List of stream devices 179 * 180 * This is the ConfigFS directory for one connection to another host. 181 * There can be several &struct stream_dev linked through @dev_list of 182 * this structure. Reference count managed through @group. 183 */ 184 struct tbstream_group { 185 struct config_group group; 186 struct tbstream *stream; 187 struct mutex lock; 188 struct list_head dev_list; 189 }; 190 191 /** 192 * struct tbstream - Stream service private data 193 * @kref: Reference count 194 * @svc: Pointer to the service device 195 * @list: Streams are linked through this in @stream_list 196 * 197 * This represents the actual physical connection between two hosts. 198 */ 199 struct tbstream { 200 struct kref kref; 201 struct tb_service *svc; 202 struct list_head list; 203 }; 204 205 static DEFINE_IDA(tbstream_indices); 206 207 /* Protects tbstream_list */ 208 static DEFINE_MUTEX(tbstream_lock); 209 static LIST_HEAD(tbstream_list); 210 211 /* Serializes tbstream_get()/put() */ 212 static DEFINE_MUTEX(tbstream_kref_lock); 213 214 /* Serializes tbstream_dev_get()/put() */ 215 static DEFINE_MUTEX(tbstream_dev_kref_lock); 216 217 /* Stream property directory UUID: 3a1cb984-c4d9-4469-a277-ce2fdfd11f0d */ 218 static const uuid_t tbstream_dir_uuid = 219 UUID_INIT(0x3a1cb984, 0xc4d9, 0x4469, 220 0xa2, 0x77, 0xce, 0x2f, 0xdf, 0xd1, 0x1f, 0x0d); 221 222 static struct tb_property_dir *tbstream_dir; 223 224 static void tbstream_release(struct kref *kref) 225 { 226 struct tbstream *stream = container_of(kref, typeof(*stream), kref); 227 228 tb_service_put(stream->svc); 229 kfree(stream); 230 } 231 232 static void tbstream_put(struct tbstream *stream) 233 { 234 if (stream) { 235 guard(mutex)(&tbstream_kref_lock); 236 kref_put(&stream->kref, tbstream_release); 237 } 238 } 239 240 static struct tbstream *tbstream_get(struct tbstream *stream) 241 { 242 if (stream) { 243 guard(mutex)(&tbstream_kref_lock); 244 kref_get(&stream->kref); 245 } 246 return stream; 247 } 248 249 static inline bool tbstream_valid(const struct tbstream *stream) 250 { 251 if (stream) 252 return !tb_service_parent(stream->svc)->is_unplugged; 253 return false; 254 } 255 256 static void tbstream_ring_free(struct tbstream_ring *ring) 257 { 258 struct device *dma_dev = tb_ring_dma_device(ring->ring); 259 enum dma_data_direction dir; 260 int i; 261 262 if (ring->ring->is_tx) 263 dir = DMA_TO_DEVICE; 264 else 265 dir = DMA_FROM_DEVICE; 266 267 for (i = 0; i < tb_ring_size(ring->ring); i++) { 268 struct tbstream_frame *sf = &ring->frames[i]; 269 270 if (sf->frame.buffer_phy) 271 dma_unmap_page(dma_dev, sf->frame.buffer_phy, 272 TB_MAX_FRAME_SIZE, dir); 273 sf->frame.buffer_phy = 0; 274 if (sf->page) 275 __free_page(sf->page); 276 sf->page = NULL; 277 } 278 279 ring->prod = 0; 280 ring->cons = 0; 281 kfree(ring->frames); 282 } 283 284 static inline bool tbstream_ring_available(const struct tbstream_ring *ring) 285 { 286 return ring->prod > ring->cons; 287 } 288 289 static void tbstream_ring_poll(struct tbstream_ring *ring) 290 { 291 struct ring_frame *frame; 292 293 while ((frame = tb_ring_poll(ring->ring))) 294 frame->callback(ring->ring, frame, false); 295 } 296 297 static inline struct tb_xdomain *tbstream_dev_xdomain(struct tbstream_dev *sdev) 298 { 299 if (sdev->stream) 300 return tb_service_parent(sdev->stream->svc); 301 return NULL; 302 } 303 304 static void tbstream_dev_release(struct kref *kref) 305 { 306 struct tbstream_dev *sdev = container_of(kref, struct tbstream_dev, kref); 307 308 if (sdev->stream) { 309 struct tb_xdomain *xd = tbstream_dev_xdomain(sdev); 310 311 if (sdev->out_hopid > 0) 312 tb_xdomain_release_out_hopid(xd, sdev->out_hopid); 313 if (sdev->in_hopid > 0) 314 tb_xdomain_release_in_hopid(xd, sdev->in_hopid); 315 316 tbstream_put(sdev->stream); 317 } 318 ida_free(&tbstream_indices, sdev->index); 319 kfree(sdev->misc.name); 320 kfree(sdev); 321 } 322 323 static inline void tbstream_dev_put(struct tbstream_dev *sdev) 324 { 325 guard(mutex)(&tbstream_dev_kref_lock); 326 kref_put(&sdev->kref, tbstream_dev_release); 327 } 328 329 static inline struct tbstream_dev *tbstream_dev_get(struct tbstream_dev *sdev) 330 { 331 guard(mutex)(&tbstream_dev_kref_lock); 332 kref_get(&sdev->kref); 333 return sdev; 334 } 335 336 static inline struct tbstream_dev *to_tbstream_dev(struct miscdevice *misc) 337 { 338 return container_of(misc, struct tbstream_dev, misc); 339 } 340 341 static inline int tbstream_dev_valid(const struct tbstream_dev *sdev) 342 { 343 const struct tbstream *stream = sdev->stream; 344 345 if (!tbstream_valid(stream)) 346 return -ENXIO; 347 if (sdev->in_hopid <= 0 || sdev->out_hopid <= 0) 348 return -EINVAL; 349 return 0; 350 } 351 352 static inline bool tbstream_dev_removed(const struct tbstream_dev *sdev) 353 { 354 return sdev->removed; 355 } 356 357 static inline bool tbstream_dev_close_received(const struct tbstream_dev *sdev) 358 { 359 return READ_ONCE(sdev->close_received); 360 } 361 362 static inline bool tbstream_dev_closed(const struct tbstream_dev *sdev) 363 { 364 return READ_ONCE(sdev->closed); 365 } 366 367 static inline bool tbstream_dev_rx_pending(const struct tbstream_dev *sdev) 368 { 369 return READ_ONCE(sdev->rx_pending); 370 } 371 372 static void 373 tbstream_dev_rx_callback(struct tb_ring *ring, struct ring_frame *frame, 374 bool canceled) 375 { 376 struct tbstream_frame *sf = container_of(frame, typeof(*sf), frame); 377 struct tbstream_dev *sdev = sf->sdev; 378 __poll_t mask; 379 380 if (canceled) 381 return; 382 383 sf->completed = true; 384 sdev->rx_ring.prod++; 385 386 mask = EPOLLIN | EPOLLRDNORM; 387 /* 388 * The CLOSE packet does not have a payload so the flags do not 389 * matter. The read_iter() deals with the flags. 390 */ 391 if (sf->frame.eof == TBSTREAM_CLOSE) { 392 WRITE_ONCE(sdev->close_received, true); 393 mask |= EPOLLHUP; 394 } 395 396 wake_up_interruptible_poll(&sdev->wait, mask); 397 } 398 399 static struct tbstream_frame * 400 tbstream_dev_completed_rx(struct tbstream_dev *sdev) 401 { 402 struct device *dma_dev = tb_ring_dma_device(sdev->rx_ring.ring); 403 struct tbstream_frame *sf; 404 int index; 405 406 index = sdev->rx_ring.cons % tb_ring_size(sdev->rx_ring.ring); 407 sf = &sdev->rx_ring.frames[index]; 408 if (!sf->completed) 409 return NULL; 410 411 dma_sync_single_for_cpu(dma_dev, sf->frame.buffer_phy, 412 tb_ring_frame_size(&sf->frame), 413 DMA_FROM_DEVICE); 414 return sf; 415 } 416 417 static int tbstream_dev_consume_rx(struct tbstream_dev *sdev) 418 { 419 struct device *dma_dev = tb_ring_dma_device(sdev->rx_ring.ring); 420 struct tbstream_frame *sf; 421 int index; 422 423 index = sdev->rx_ring.cons % tb_ring_size(sdev->rx_ring.ring); 424 sdev->rx_ring.cons++; 425 426 sf = &sdev->rx_ring.frames[index]; 427 sf->completed = false; 428 sf->offset = 0; 429 sf->frame.size = 0; 430 431 dma_sync_single_for_device(dma_dev, sf->frame.buffer_phy, 432 tb_ring_frame_size(&sf->frame), 433 DMA_FROM_DEVICE); 434 435 return tb_ring_rx(sdev->rx_ring.ring, &sf->frame); 436 } 437 438 static int tbstream_dev_alloc_rx_buffers(struct tbstream_dev *sdev) 439 { 440 size_t ring_size = tb_ring_size(sdev->rx_ring.ring); 441 int i; 442 443 sdev->rx_ring.frames = kzalloc_objs(struct tbstream_frame, ring_size); 444 if (!sdev->rx_ring.frames) 445 return -ENOMEM; 446 447 for (i = 0; i < ring_size; i++) { 448 struct device *dma_dev = tb_ring_dma_device(sdev->rx_ring.ring); 449 struct tbstream_frame *sf = &sdev->rx_ring.frames[i]; 450 dma_addr_t dma_addr; 451 452 sf->page = alloc_page(GFP_KERNEL); 453 if (!sf->page) 454 return -ENOMEM; 455 456 dma_addr = dma_map_page(dma_dev, sf->page, 0, TB_MAX_FRAME_SIZE, 457 DMA_FROM_DEVICE); 458 if (dma_mapping_error(dma_dev, dma_addr)) { 459 __free_page(sf->page); 460 sf->page = NULL; 461 return -ENOMEM; 462 } 463 464 sf->sdev = sdev; 465 sf->frame.callback = tbstream_dev_rx_callback; 466 sf->frame.buffer_phy = dma_addr; 467 468 tb_ring_rx(sdev->rx_ring.ring, &sf->frame); 469 } 470 471 sdev->rx_ring.cons = 0; 472 sdev->rx_ring.prod = 0; 473 return 0; 474 } 475 476 static void 477 tbstream_dev_tx_callback(struct tb_ring *ring, struct ring_frame *frame, 478 bool canceled) 479 { 480 struct tbstream_frame *sf = container_of(frame, typeof(*sf), frame); 481 struct tbstream_dev *sdev = sf->sdev; 482 483 if (canceled) 484 return; 485 486 sdev->tx_ring.prod++; 487 if (sf->frame.eof == TBSTREAM_DATA) 488 wake_up_interruptible_poll(&sdev->wait, EPOLLOUT | EPOLLWRNORM); 489 } 490 491 static int tbstream_dev_alloc_tx_buffers(struct tbstream_dev *sdev) 492 { 493 struct device *dma_dev = tb_ring_dma_device(sdev->tx_ring.ring); 494 size_t ring_size = tb_ring_size(sdev->tx_ring.ring); 495 int i; 496 497 sdev->tx_ring.frames = kzalloc_objs(struct tbstream_frame, ring_size); 498 if (!sdev->tx_ring.frames) 499 return -ENOMEM; 500 501 for (i = 0; i < ring_size; i++) { 502 struct tbstream_frame *sf = &sdev->tx_ring.frames[i]; 503 dma_addr_t dma_addr; 504 505 sf->page = alloc_page(GFP_KERNEL); 506 if (!sf->page) 507 return -ENOMEM; 508 509 dma_addr = dma_map_page(dma_dev, sf->page, 0, TB_MAX_FRAME_SIZE, 510 DMA_TO_DEVICE); 511 if (dma_mapping_error(dma_dev, dma_addr)) { 512 __free_page(sf->page); 513 sf->page = NULL; 514 return -ENOMEM; 515 } 516 517 sf->sdev = sdev; 518 sf->frame.callback = tbstream_dev_tx_callback; 519 sf->frame.buffer_phy = dma_addr; 520 sf->frame.sof = TBSTREAM_FRAME_START; 521 } 522 523 sdev->tx_ring.cons = 0; 524 sdev->tx_ring.prod = ring_size - 1; 525 return 0; 526 } 527 528 static struct tbstream_frame * 529 tbstream_dev_alloc_tx(struct tbstream_dev *sdev, enum tbstream_frame_pdf pdf, 530 struct iov_iter *from, size_t size) 531 { 532 struct device *dma_dev = tb_ring_dma_device(sdev->tx_ring.ring); 533 struct tbstream_frame *sf; 534 int index; 535 536 if (!tbstream_ring_available(&sdev->tx_ring)) 537 return ERR_PTR(-ENOBUFS); 538 539 index = sdev->tx_ring.cons % tb_ring_size(sdev->tx_ring.ring); 540 sdev->tx_ring.cons++; 541 542 sf = &sdev->tx_ring.frames[index]; 543 sf->frame.size = size < TB_MAX_FRAME_SIZE ? size : 0; 544 sf->frame.eof = pdf; 545 546 dma_sync_single_for_cpu(dma_dev, sf->frame.buffer_phy, size, 547 DMA_TO_DEVICE); 548 if (pdf == TBSTREAM_DATA) { 549 if (copy_page_from_iter(sf->page, 0, size, from) != size) { 550 sdev->tx_ring.cons--; 551 return ERR_PTR(-EFAULT); 552 } 553 } else { 554 memset(page_address(sf->page), 0, size); 555 } 556 dma_sync_single_for_device(dma_dev, sf->frame.buffer_phy, size, 557 DMA_TO_DEVICE); 558 return sf; 559 } 560 561 static int 562 tbstream_dev_send_data(struct tbstream_dev *sdev, struct iov_iter *from, 563 size_t size) 564 { 565 struct tbstream_frame *sf; 566 567 sf = tbstream_dev_alloc_tx(sdev, TBSTREAM_DATA, from, size); 568 if (IS_ERR(sf)) 569 return PTR_ERR(sf); 570 return tb_ring_tx(sdev->tx_ring.ring, &sf->frame); 571 } 572 573 static int tbstream_dev_send_close(struct tbstream_dev *sdev) 574 { 575 struct tbstream_frame *sf; 576 ktime_t timeout; 577 578 /* 579 * Wait for the ring to have available slots before we send the 580 * CLOSE packet. 581 */ 582 timeout = ktime_add_ms(ktime_get(), 500); 583 do { 584 if (tbstream_ring_available(&sdev->tx_ring)) 585 break; 586 /* 587 * For busy polling we need to advance the ring here as 588 * well to make the slots available. 589 */ 590 if (sdev->busy_poll) 591 tbstream_ring_poll(&sdev->tx_ring); 592 fsleep(15); 593 } while (ktime_before(ktime_get(), timeout)); 594 595 sf = tbstream_dev_alloc_tx(sdev, TBSTREAM_CLOSE, NULL, SZ_256); 596 if (IS_ERR(sf)) 597 return PTR_ERR(sf); 598 return tb_ring_tx(sdev->tx_ring.ring, &sf->frame); 599 } 600 601 static void tbstream_dev_start_poll(void *data) 602 { 603 struct tbstream_dev *sdev = data; 604 605 WRITE_ONCE(sdev->rx_pending, true); 606 wake_up_interruptible_poll(&sdev->wait, EPOLLIN | EPOLLRDNORM); 607 } 608 609 /* sdev->lock must be held */ 610 static void tbstream_dev_advance_rx(struct tbstream_dev *sdev) 611 { 612 /* 613 * Clear before running the completions so that an interrupt 614 * that arrives while we are doing that is not missed. 615 */ 616 WRITE_ONCE(sdev->rx_pending, false); 617 tbstream_ring_poll(&sdev->rx_ring); 618 } 619 620 /* sdev->lock must be held */ 621 static void tbstream_dev_complete_rx(struct tbstream_dev *sdev) 622 { 623 if (!sdev->busy_poll) 624 tb_ring_poll_complete(sdev->rx_ring.ring); 625 } 626 627 static int tbstream_dev_start(struct tbstream_dev *sdev) 628 { 629 struct tb_xdomain *xd = tbstream_dev_xdomain(sdev); 630 unsigned int flags = RING_FLAG_FRAME | RING_FLAG_E2E; 631 void (*start_poll)(void *) = NULL; 632 u16 sof_mask, eof_mask; 633 struct tb_ring *ring; 634 int ret, e2e_tx_hop; 635 636 if (sdev->busy_poll) 637 flags |= RING_FLAG_NO_INTERRUPT; 638 else 639 start_poll = tbstream_dev_start_poll; 640 641 ring = tb_ring_alloc_tx(xd->tb->nhi, -1, sdev->ring_size, flags); 642 if (!ring) 643 return -ENOMEM; 644 sdev->tx_ring.ring = ring; 645 646 ret = tbstream_dev_alloc_tx_buffers(sdev); 647 if (ret) 648 goto err_free_tx; 649 650 e2e_tx_hop = ring->hop; 651 sof_mask = BIT(TBSTREAM_FRAME_START); 652 eof_mask = BIT(TBSTREAM_DATA) | BIT(TBSTREAM_CLOSE); 653 654 ring = tb_ring_alloc_rx(xd->tb->nhi, -1, sdev->ring_size, flags, 655 e2e_tx_hop, sof_mask, eof_mask, start_poll, 656 sdev); 657 if (!ring) { 658 ret = -ENOMEM; 659 goto err_free_tx_buffers; 660 } 661 sdev->rx_ring.ring = ring; 662 663 ret = tb_xdomain_enable_paths(xd, sdev->out_hopid, 664 sdev->tx_ring.ring->hop, 665 sdev->in_hopid, 666 sdev->rx_ring.ring->hop); 667 if (ret) 668 goto err_free_rx; 669 670 tb_ring_throttling(sdev->tx_ring.ring, sdev->throttling); 671 tb_ring_throttling(sdev->rx_ring.ring, sdev->throttling); 672 673 sdev->rx_pending = false; 674 675 tb_ring_start(sdev->tx_ring.ring); 676 tb_ring_start(sdev->rx_ring.ring); 677 678 ret = tbstream_dev_alloc_rx_buffers(sdev); 679 if (ret) 680 goto err_stop; 681 return 0; 682 683 err_stop: 684 tb_ring_stop(sdev->rx_ring.ring); 685 tb_ring_stop(sdev->tx_ring.ring); 686 tb_xdomain_disable_paths(xd, sdev->out_hopid, 687 sdev->tx_ring.ring->hop, 688 sdev->in_hopid, 689 sdev->rx_ring.ring->hop); 690 if (sdev->rx_ring.frames) 691 tbstream_ring_free(&sdev->rx_ring); 692 err_free_rx: 693 tb_ring_free(sdev->rx_ring.ring); 694 err_free_tx_buffers: 695 tbstream_ring_free(&sdev->tx_ring); 696 err_free_tx: 697 tb_ring_free(sdev->tx_ring.ring); 698 699 return ret; 700 } 701 702 static bool tbstream_dev_tx_drained(const struct tbstream_dev *sdev) 703 { 704 const struct tbstream_ring *ring = &sdev->tx_ring; 705 706 /* 707 * Everything is completed when number of free TX slots is back 708 * to the maximum. 709 */ 710 return ring->prod - ring->cons == tb_ring_size(ring->ring) - 1; 711 } 712 713 static void tbstream_dev_stop(struct tbstream_dev *sdev) 714 { 715 struct tb_xdomain *xd; 716 717 if (sdev->busy_poll) { 718 /* 719 * When busy polling we must advance the ring ourselves 720 * to push all outstanding frames on the wire. 721 */ 722 ktime_t timeout = ktime_add_ms(ktime_get(), 500); 723 724 do { 725 if (tbstream_dev_tx_drained(sdev)) 726 break; 727 tbstream_ring_poll(&sdev->tx_ring); 728 fsleep(15); 729 } while (ktime_before(ktime_get(), timeout)); 730 } else { 731 tb_ring_flush(sdev->tx_ring.ring, 500); 732 } 733 734 tb_ring_stop(sdev->tx_ring.ring); 735 tb_ring_stop(sdev->rx_ring.ring); 736 737 xd = tbstream_dev_xdomain(sdev); 738 if (xd) { 739 tb_xdomain_disable_paths(xd, sdev->out_hopid, 740 sdev->tx_ring.ring->hop, 741 sdev->in_hopid, 742 sdev->rx_ring.ring->hop); 743 } 744 745 tbstream_ring_free(&sdev->rx_ring); 746 tb_ring_free(sdev->rx_ring.ring); 747 sdev->rx_ring.ring = NULL; 748 tbstream_ring_free(&sdev->tx_ring); 749 tb_ring_free(sdev->tx_ring.ring); 750 sdev->tx_ring.ring = NULL; 751 } 752 753 /* Use only with read_iter/write_iter() to handle nowait */ 754 static int tbstream_dev_lock(struct tbstream_dev *sdev, bool nowait) 755 { 756 if (nowait) { 757 if (!mutex_trylock(&sdev->lock)) 758 return -EAGAIN; 759 } else { 760 if (mutex_lock_interruptible(&sdev->lock)) 761 return -ERESTARTSYS; 762 } 763 return 0; 764 } 765 766 /* Must not be called with @sdev->lock held */ 767 static int tbstream_dev_busy_poll_wait(struct tbstream_dev *sdev, 768 struct tbstream_ring *ring) 769 { 770 for (;;) { 771 if (signal_pending(current)) 772 return -ERESTARTSYS; 773 if (tb_ring_poll_pending(ring->ring)) 774 return 0; 775 /* 776 * For TX ring we need to check the RX side too because 777 * it might have received CLOSE packet. 778 */ 779 if (ring == &sdev->tx_ring && 780 tb_ring_poll_pending(sdev->rx_ring.ring)) 781 return 0; 782 if (tbstream_dev_valid(sdev) != 0 || 783 tbstream_dev_closed(sdev) || tbstream_dev_removed(sdev)) 784 return 0; 785 cond_resched(); 786 } 787 } 788 789 static bool 790 tbstream_dev_has_event(struct tbstream_dev *sdev, struct tbstream_ring *ring) 791 { 792 if (tbstream_dev_valid(sdev) != 0) 793 return true; 794 if (tbstream_dev_closed(sdev)) 795 return true; 796 if (tbstream_dev_removed(sdev)) 797 return true; 798 if (tbstream_dev_rx_pending(sdev)) 799 return true; 800 if (tbstream_ring_available(ring)) 801 return true; 802 return tb_ring_poll_pending(sdev->rx_ring.ring); 803 } 804 805 static ssize_t 806 tbstream_dev_fops_read_iter(struct kiocb *kiocb, struct iov_iter *to) 807 { 808 struct file *file = kiocb->ki_filp; 809 bool nowait = file->f_flags & O_NONBLOCK || kiocb->ki_flags & IOCB_NOWAIT; 810 struct tbstream_dev *sdev = to_tbstream_dev(file->private_data); 811 size_t nbytes; 812 int ret; 813 814 ret = tbstream_dev_valid(sdev); 815 if (ret) 816 return ret; 817 818 ret = tbstream_dev_lock(sdev, nowait); 819 if (ret) 820 return ret; 821 822 for (;;) { 823 /* Advance RX completions */ 824 tbstream_dev_advance_rx(sdev); 825 826 ret = tbstream_dev_valid(sdev); 827 if (ret) { 828 mutex_unlock(&sdev->lock); 829 return ret; 830 } 831 832 if (tbstream_dev_closed(sdev) || tbstream_dev_removed(sdev)) { 833 mutex_unlock(&sdev->lock); 834 return 0; 835 } 836 837 if (tbstream_ring_available(&sdev->rx_ring)) 838 break; 839 840 /* Polled all we could. Re-enable the interrupt now. */ 841 tbstream_dev_complete_rx(sdev); 842 mutex_unlock(&sdev->lock); 843 844 if (nowait) 845 return -EAGAIN; 846 847 if (sdev->busy_poll) { 848 ret = tbstream_dev_busy_poll_wait(sdev, &sdev->rx_ring); 849 if (ret) 850 return ret; 851 } else { 852 ret = wait_event_interruptible(sdev->wait, 853 tbstream_dev_has_event(sdev, &sdev->rx_ring)); 854 if (ret) 855 return ret; 856 } 857 858 ret = tbstream_dev_lock(sdev, nowait); 859 if (ret) 860 return ret; 861 } 862 863 nbytes = 0; 864 while (iov_iter_count(to)) { 865 struct tbstream_frame *sf; 866 size_t size, sf_size; 867 868 sf = tbstream_dev_completed_rx(sdev); 869 if (!sf) 870 break; 871 /* 872 * CLOSE tunneled packet. If userspace already read 873 * something then we stop processing now and return 874 * those bytes. Next time the first frame will be CLOSE 875 * in which case we return EOF to the user. 876 */ 877 if (sf->frame.eof == TBSTREAM_CLOSE) { 878 if (!nbytes) { 879 tbstream_dev_consume_rx(sdev); 880 WRITE_ONCE(sdev->closed, true); 881 } 882 break; 883 } else if (sf->frame.flags & 884 (RING_DESC_CRC_ERROR | RING_DESC_BUFFER_OVERRUN)) { 885 /* 886 * If something was already read return that now 887 * and next read will report the error. 888 */ 889 if (!nbytes) { 890 pr_warn("corrupted frame received, flags %#x\n", 891 sf->frame.flags); 892 tbstream_dev_consume_rx(sdev); 893 ret = -EIO; 894 } 895 break; 896 } 897 898 sf_size = tb_ring_frame_size(&sf->frame); 899 size = min(iov_iter_count(to), sf_size); 900 901 if (copy_page_to_iter(sf->page, sf->offset, size, to) != size) { 902 ret = -EFAULT; 903 break; 904 } 905 906 /* 907 * If not all data from the frame is read so leave it in 908 * place and update the offset accordingly so next read 909 * gets the rest. 910 */ 911 if (size < sf_size) { 912 sf->offset += size; 913 sf->frame.size = sf_size - size; 914 } else { 915 ret = tbstream_dev_consume_rx(sdev); 916 if (ret) 917 break; 918 } 919 920 nbytes += size; 921 } 922 923 mutex_unlock(&sdev->lock); 924 if (ret) 925 return ret; 926 return nbytes; 927 } 928 929 static void tbstream_dev_advance_both(struct tbstream_dev *sdev) 930 { 931 /* 932 * When busy polling, advance TX completions manually. 933 * 934 * We also need to advance the RX side in both modes to be able 935 * to receive CLOSE packet from the other peer if there is no 936 * reader. 937 */ 938 if (sdev->busy_poll) { 939 tbstream_ring_poll(&sdev->tx_ring); 940 tbstream_dev_advance_rx(sdev); 941 } else if (tbstream_dev_rx_pending(sdev) || 942 tb_ring_poll_pending(sdev->rx_ring.ring)) { 943 tbstream_dev_advance_rx(sdev); 944 tbstream_dev_complete_rx(sdev); 945 } 946 } 947 948 static ssize_t 949 tbstream_dev_fops_write_iter(struct kiocb *kiocb, struct iov_iter *from) 950 { 951 struct file *file = kiocb->ki_filp; 952 bool nowait = file->f_flags & O_NONBLOCK || kiocb->ki_flags & IOCB_NOWAIT; 953 struct tbstream_dev *sdev = to_tbstream_dev(file->private_data); 954 size_t nbytes; 955 int ret; 956 957 ret = tbstream_dev_valid(sdev); 958 if (ret) 959 return ret; 960 961 ret = tbstream_dev_lock(sdev, nowait); 962 if (ret) 963 return ret; 964 965 for (;;) { 966 /* Advance TX (and RX) completions */ 967 tbstream_dev_advance_both(sdev); 968 969 ret = tbstream_dev_valid(sdev); 970 if (ret) { 971 mutex_unlock(&sdev->lock); 972 return ret; 973 } 974 975 if (tbstream_dev_close_received(sdev)) { 976 mutex_unlock(&sdev->lock); 977 return -EPIPE; 978 } 979 980 if (tbstream_dev_removed(sdev)) { 981 mutex_unlock(&sdev->lock); 982 return -ENXIO; 983 } 984 985 if (tbstream_ring_available(&sdev->tx_ring)) 986 break; 987 988 mutex_unlock(&sdev->lock); 989 990 if (nowait) 991 return -EAGAIN; 992 993 if (sdev->busy_poll) { 994 ret = tbstream_dev_busy_poll_wait(sdev, &sdev->tx_ring); 995 if (ret) 996 return ret; 997 } else { 998 ret = wait_event_interruptible(sdev->wait, 999 tbstream_dev_has_event(sdev, &sdev->tx_ring) || 1000 tbstream_dev_close_received(sdev)); 1001 if (ret) 1002 return ret; 1003 } 1004 1005 ret = tbstream_dev_lock(sdev, nowait); 1006 if (ret) 1007 return ret; 1008 } 1009 1010 nbytes = 0; 1011 while (iov_iter_count(from)) { 1012 size_t size; 1013 1014 size = min(iov_iter_count(from), TB_MAX_FRAME_SIZE); 1015 ret = tbstream_dev_send_data(sdev, from, size); 1016 if (ret) { 1017 /* 1018 * If there are no more buffers we are done for 1019 * this write. 1020 */ 1021 if (ret == -ENOBUFS) 1022 ret = 0; 1023 break; 1024 } 1025 1026 nbytes += size; 1027 } 1028 1029 mutex_unlock(&sdev->lock); 1030 if (ret) 1031 return ret; 1032 return nbytes; 1033 } 1034 1035 static __poll_t 1036 tbstream_dev_fops_poll(struct file *file, struct poll_table_struct *wait) 1037 { 1038 struct tbstream_dev *sdev = to_tbstream_dev(file->private_data); 1039 __poll_t mask = 0; 1040 1041 /* 1042 * Without interrupts there is nothing that can wake us up so 1043 * return failure instead. 1044 */ 1045 if (sdev->busy_poll) 1046 return EPOLLERR; 1047 1048 poll_wait(file, &sdev->wait, wait); 1049 guard(mutex)(&sdev->lock); 1050 if (tbstream_dev_valid(sdev) != 0) 1051 return EPOLLHUP | EPOLLERR; 1052 1053 /* 1054 * The RX completions are only advanced from here and from 1055 * read(2) so do that now, otherwise we would never report 1056 * anything to be available. 1057 */ 1058 tbstream_dev_advance_rx(sdev); 1059 1060 if (tbstream_dev_close_received(sdev)) 1061 mask |= EPOLLHUP; 1062 if (tbstream_ring_available(&sdev->tx_ring)) 1063 mask |= EPOLLOUT | EPOLLWRNORM; 1064 if (tbstream_ring_available(&sdev->rx_ring)) { 1065 mask |= EPOLLIN | EPOLLRDNORM; 1066 } else { 1067 tbstream_dev_complete_rx(sdev); 1068 if (tb_ring_poll_pending(sdev->rx_ring.ring)) 1069 mask |= EPOLLIN | EPOLLRDNORM; 1070 } 1071 1072 return mask; 1073 } 1074 1075 static int tbstream_dev_fops_open(struct inode *inode, struct file *file) 1076 { 1077 struct tbstream_dev *sdev = to_tbstream_dev(file->private_data); 1078 int ret; 1079 1080 tbstream_dev_get(sdev); 1081 1082 if (mutex_lock_interruptible(&sdev->lock)) { 1083 tbstream_dev_put(sdev); 1084 return -ERESTARTSYS; 1085 } 1086 1087 /* 1088 * If there is no stream attached yet, block until it appears 1089 * unless this is opened in non-blocking mode. 1090 */ 1091 while ((ret = tbstream_dev_valid(sdev))) { 1092 mutex_unlock(&sdev->lock); 1093 1094 if (ret != -ENXIO || (file->f_flags & O_NONBLOCK)) 1095 goto err_put; 1096 1097 ret = wait_event_interruptible(sdev->wait, 1098 tbstream_dev_valid(sdev) == 0 || 1099 tbstream_dev_removed(sdev)); 1100 if (ret) 1101 goto err_put; 1102 1103 if (tbstream_dev_removed(sdev)) { 1104 ret = -ENXIO; 1105 goto err_put; 1106 } 1107 1108 if (mutex_lock_interruptible(&sdev->lock)) { 1109 ret = -ERESTARTSYS; 1110 goto err_put; 1111 } 1112 } 1113 1114 /* Only on first open we allocate rings and enable paths */ 1115 if (!sdev->users++) { 1116 ret = tbstream_dev_start(sdev); 1117 if (ret) { 1118 sdev->users--; 1119 goto err_unlock; 1120 } 1121 1122 sdev->close_received = false; 1123 sdev->closed = false; 1124 } 1125 1126 mutex_unlock(&sdev->lock); 1127 1128 /* Stream handles IOCB_NOWAIT just fine */ 1129 file->f_mode |= FMODE_NOWAIT; 1130 return 0; 1131 1132 err_unlock: 1133 mutex_unlock(&sdev->lock); 1134 err_put: 1135 tbstream_dev_put(sdev); 1136 1137 return ret; 1138 } 1139 1140 static int tbstream_dev_fops_release(struct inode *inode, struct file *file) 1141 { 1142 struct tbstream_dev *sdev = to_tbstream_dev(file->private_data); 1143 1144 mutex_lock(&sdev->lock); 1145 if (--sdev->users == 0) { 1146 /* 1147 * Advance now in case there is CLOSE waiting in the RX 1148 * ring. 1149 */ 1150 tbstream_dev_advance_both(sdev); 1151 /* 1152 * Send CLOSE tunneled packet to notify the other end 1153 * that we are closing the file, if it is not closed 1154 * already. 1155 */ 1156 if (!tbstream_dev_close_received(sdev) && 1157 tbstream_dev_valid(sdev) == 0) 1158 tbstream_dev_send_close(sdev); 1159 tbstream_dev_stop(sdev); 1160 } 1161 mutex_unlock(&sdev->lock); 1162 1163 tbstream_dev_put(sdev); 1164 return 0; 1165 } 1166 1167 static const struct file_operations tbstream_dev_fops = { 1168 .owner = THIS_MODULE, 1169 .llseek = noop_llseek, 1170 .read_iter = tbstream_dev_fops_read_iter, 1171 .write_iter = tbstream_dev_fops_write_iter, 1172 .poll = tbstream_dev_fops_poll, 1173 .open = tbstream_dev_fops_open, 1174 .release = tbstream_dev_fops_release, 1175 }; 1176 1177 static inline struct tbstream_dev * 1178 tbstream_dev_from_group(struct config_group *group) 1179 { 1180 return container_of(group, struct tbstream_dev, group); 1181 } 1182 1183 static ssize_t tbstream_dev_busy_poll_show(struct config_item *item, char *buf) 1184 { 1185 struct config_group *group = to_config_group(item); 1186 struct tbstream_dev *sdev = tbstream_dev_from_group(group); 1187 1188 return sysfs_emit(buf, "%u\n", sdev->busy_poll); 1189 } 1190 1191 static ssize_t 1192 tbstream_dev_busy_poll_store(struct config_item *item, const char *buf, 1193 size_t count) 1194 { 1195 struct config_group *group = to_config_group(item); 1196 struct tbstream_dev *sdev = tbstream_dev_from_group(group); 1197 bool busy_poll; 1198 int ret; 1199 1200 ret = kstrtobool(buf, &busy_poll); 1201 if (ret) 1202 return ret; 1203 1204 guard(mutex)(&sdev->lock); 1205 if (sdev->users) 1206 return -EBUSY; 1207 sdev->busy_poll = busy_poll; 1208 return count; 1209 } 1210 CONFIGFS_ATTR(tbstream_dev_, busy_poll); 1211 1212 static ssize_t tbstream_dev_index_show(struct config_item *item, char *buf) 1213 { 1214 struct config_group *group = to_config_group(item); 1215 struct tbstream_dev *sdev = tbstream_dev_from_group(group); 1216 1217 return sysfs_emit(buf, "%d\n", sdev->index); 1218 } 1219 CONFIGFS_ATTR_RO(tbstream_dev_, index); 1220 1221 static ssize_t tbstream_dev_in_hopid_show(struct config_item *item, char *buf) 1222 { 1223 struct config_group *group = to_config_group(item); 1224 struct tbstream_dev *sdev = tbstream_dev_from_group(group); 1225 1226 return sysfs_emit(buf, "%d\n", sdev->in_hopid); 1227 } 1228 1229 /* svc->lock must be held */ 1230 static void service_remove_properties(struct tb_service *svc, const char *name) 1231 { 1232 struct tb_property *p; 1233 1234 if (!svc->local_properties) 1235 return; 1236 1237 p = tb_property_find(svc->local_properties, name, 1238 TB_PROPERTY_TYPE_DIRECTORY); 1239 if (p) { 1240 tb_property_free_dir(p->value.dir); 1241 tb_property_remove(p); 1242 1243 dev_dbg(&svc->dev, "removed local directory %s\n", name); 1244 1245 /* 1246 * Is the service directory empty already? If it is then 1247 * we can release it as well. 1248 */ 1249 tb_property_for_each(svc->local_properties, p) { 1250 if (p->type == TB_PROPERTY_TYPE_DIRECTORY) 1251 return; 1252 } 1253 1254 tb_property_free_dir(svc->local_properties); 1255 svc->local_properties = NULL; 1256 } 1257 } 1258 1259 static int service_update_properties(struct tb_service *svc, const char *name, 1260 int in_hopid, int out_hopid) 1261 { 1262 struct tb_property_dir *dir; 1263 struct tb_property *p; 1264 1265 guard(mutex)(&svc->lock); 1266 1267 if (in_hopid < 8 || out_hopid < 8) { 1268 service_remove_properties(svc, name); 1269 return 0; 1270 } 1271 1272 if (!svc->local_properties) { 1273 /* 1274 * Add the service directory first time we 1275 * populate the entries. 1276 */ 1277 svc->local_properties = tb_property_copy_dir(tbstream_dir); 1278 if (!svc->local_properties) 1279 return -ENOMEM; 1280 } 1281 1282 p = tb_property_find(svc->local_properties, name, 1283 TB_PROPERTY_TYPE_DIRECTORY); 1284 if (p) { 1285 dir = p->value.dir; 1286 1287 p = tb_property_find(dir, "inhopid", TB_PROPERTY_TYPE_VALUE); 1288 if (p && p->value.immediate != in_hopid) 1289 p->value.immediate = in_hopid; 1290 p = tb_property_find(dir, "outhopid", TB_PROPERTY_TYPE_VALUE); 1291 if (p && p->value.immediate != out_hopid) 1292 p->value.immediate = out_hopid; 1293 1294 dev_dbg(&svc->dev, 1295 "updated local directory %s: in HopID %d, out HopID %d\n", 1296 name, in_hopid, out_hopid); 1297 } else { 1298 uuid_t uuid; 1299 int ret; 1300 1301 uuid_gen(&uuid); 1302 dir = tb_property_create_dir(&uuid); 1303 if (!dir) 1304 return -ENOMEM; 1305 1306 tb_property_add_immediate(dir, "inhopid", in_hopid); 1307 tb_property_add_immediate(dir, "outhopid", out_hopid); 1308 1309 ret = tb_property_add_dir(svc->local_properties, name, dir); 1310 if (ret) { 1311 tb_property_free_dir(dir); 1312 return ret; 1313 } 1314 1315 dev_dbg(&svc->dev, 1316 "added local directory %s: in HopID %d, out HopID %d\n", 1317 name, in_hopid, out_hopid); 1318 } 1319 1320 return 0; 1321 } 1322 1323 static int tbstream_dev_update_properties(struct tbstream_dev *sdev) 1324 { 1325 struct tbstream *stream; 1326 int ret; 1327 1328 stream = tbstream_get(sdev->stream); 1329 if (!stream) 1330 return 0; 1331 1332 ret = service_update_properties(stream->svc, 1333 config_item_name(&sdev->group.cg_item), 1334 sdev->in_hopid, sdev->out_hopid); 1335 if (!ret) 1336 tb_service_properties_changed(stream->svc); 1337 1338 tbstream_put(stream); 1339 return ret; 1340 } 1341 1342 static int tbstream_dev_alloc_in_hopid(struct tbstream_dev *sdev, int hopid) 1343 { 1344 struct tb_xdomain *xd = tbstream_dev_xdomain(sdev); 1345 int ret; 1346 1347 if (sdev->in_hopid > 0 && sdev->in_hopid != hopid) 1348 tb_xdomain_release_in_hopid(xd, sdev->in_hopid); 1349 if (!hopid) { 1350 sdev->in_hopid = hopid; 1351 return 0; 1352 } 1353 ret = tb_xdomain_alloc_in_hopid(xd, hopid); 1354 if (ret < 0) 1355 return ret; 1356 /* 1357 * If specific HopID was asked by the user and we did not get 1358 * that one then release and return error instead. 1359 */ 1360 if (hopid > 0 && hopid != ret) { 1361 tb_xdomain_release_in_hopid(xd, ret); 1362 return -EBUSY; 1363 } 1364 sdev->in_hopid = ret; 1365 return 0; 1366 } 1367 1368 static int tbstream_dev_alloc_out_hopid(struct tbstream_dev *sdev, int hopid) 1369 { 1370 struct tb_xdomain *xd = tbstream_dev_xdomain(sdev); 1371 int ret; 1372 1373 if (sdev->out_hopid > 0 && sdev->out_hopid != hopid) 1374 tb_xdomain_release_out_hopid(xd, sdev->out_hopid); 1375 if (!hopid) { 1376 sdev->out_hopid = hopid; 1377 return 0; 1378 } 1379 ret = tb_xdomain_alloc_out_hopid(xd, hopid); 1380 if (ret < 0) 1381 return ret; 1382 if (hopid > 0 && hopid != ret) { 1383 tb_xdomain_release_out_hopid(xd, ret); 1384 return -EBUSY; 1385 } 1386 sdev->out_hopid = ret; 1387 return 0; 1388 } 1389 1390 static ssize_t 1391 tbstream_dev_in_hopid_store(struct config_item *item, const char *buf, 1392 size_t count) 1393 { 1394 struct config_group *group = to_config_group(item); 1395 struct tbstream_dev *sdev = tbstream_dev_from_group(group); 1396 int ret, in_hopid; 1397 1398 ret = kstrtoint(buf, 0, &in_hopid); 1399 if (ret) 1400 return ret; 1401 1402 guard(mutex)(&sdev->lock); 1403 if (sdev->users) 1404 return -EBUSY; 1405 if (sdev->stream) { 1406 ret = tbstream_dev_alloc_in_hopid(sdev, in_hopid); 1407 if (ret) 1408 return ret; 1409 ret = tbstream_dev_update_properties(sdev); 1410 } else { 1411 sdev->in_hopid = in_hopid; 1412 } 1413 return ret ? ret : count; 1414 } 1415 CONFIGFS_ATTR(tbstream_dev_, in_hopid); 1416 1417 static ssize_t tbstream_dev_out_hopid_show(struct config_item *item, char *buf) 1418 { 1419 struct config_group *group = to_config_group(item); 1420 struct tbstream_dev *sdev = tbstream_dev_from_group(group); 1421 1422 return sysfs_emit(buf, "%d\n", sdev->out_hopid); 1423 } 1424 1425 static ssize_t 1426 tbstream_dev_out_hopid_store(struct config_item *item, const char *buf, 1427 size_t count) 1428 { 1429 struct config_group *group = to_config_group(item); 1430 struct tbstream_dev *sdev = tbstream_dev_from_group(group); 1431 int ret, out_hopid; 1432 1433 ret = kstrtoint(buf, 0, &out_hopid); 1434 if (ret) 1435 return ret; 1436 1437 guard(mutex)(&sdev->lock); 1438 if (sdev->users) 1439 return -EBUSY; 1440 if (sdev->stream) { 1441 ret = tbstream_dev_alloc_out_hopid(sdev, out_hopid); 1442 if (ret) 1443 return ret; 1444 ret = tbstream_dev_update_properties(sdev); 1445 } else { 1446 sdev->out_hopid = out_hopid; 1447 } 1448 return ret ? ret : count; 1449 } 1450 CONFIGFS_ATTR(tbstream_dev_, out_hopid); 1451 1452 static ssize_t tbstream_dev_ring_size_show(struct config_item *item, char *buf) 1453 { 1454 struct config_group *group = to_config_group(item); 1455 struct tbstream_dev *sdev = tbstream_dev_from_group(group); 1456 1457 return sysfs_emit(buf, "%u\n", sdev->ring_size); 1458 } 1459 1460 static ssize_t 1461 tbstream_dev_ring_size_store(struct config_item *item, const char *buf, 1462 size_t count) 1463 { 1464 struct config_group *group = to_config_group(item); 1465 struct tbstream_dev *sdev = tbstream_dev_from_group(group); 1466 unsigned int ring_size; 1467 int ret; 1468 1469 ret = kstrtouint(buf, 0, &ring_size); 1470 if (ret) 1471 return ret; 1472 1473 if (ring_size < TBSTREAM_DEV_MIN_RING_SIZE || 1474 ring_size > TBSTREAM_DEV_MAX_RING_SIZE) 1475 return -EINVAL; 1476 1477 guard(mutex)(&sdev->lock); 1478 if (sdev->users) 1479 return -EBUSY; 1480 sdev->ring_size = ring_size; 1481 return count; 1482 } 1483 CONFIGFS_ATTR(tbstream_dev_, ring_size); 1484 1485 static ssize_t tbstream_dev_throttling_show(struct config_item *item, char *buf) 1486 { 1487 struct config_group *group = to_config_group(item); 1488 struct tbstream_dev *sdev = tbstream_dev_from_group(group); 1489 1490 return sysfs_emit(buf, "%u\n", sdev->throttling); 1491 } 1492 1493 static ssize_t 1494 tbstream_dev_throttling_store(struct config_item *item, const char *buf, 1495 size_t count) 1496 { 1497 struct config_group *group = to_config_group(item); 1498 struct tbstream_dev *sdev = tbstream_dev_from_group(group); 1499 unsigned int throttling; 1500 int ret; 1501 1502 ret = kstrtouint(buf, 0, &throttling); 1503 if (ret) 1504 return ret; 1505 1506 if (throttling > TBSTREAM_DEV_MAX_THROTTLING) 1507 return -EINVAL; 1508 1509 guard(mutex)(&sdev->lock); 1510 if (sdev->users) 1511 return -EBUSY; 1512 sdev->throttling = throttling; 1513 return count; 1514 } 1515 CONFIGFS_ATTR(tbstream_dev_, throttling); 1516 1517 static struct configfs_attribute *tbstream_dev_attrs[] = { 1518 &tbstream_dev_attr_busy_poll, 1519 &tbstream_dev_attr_index, 1520 &tbstream_dev_attr_in_hopid, 1521 &tbstream_dev_attr_out_hopid, 1522 &tbstream_dev_attr_ring_size, 1523 &tbstream_dev_attr_throttling, 1524 NULL, 1525 }; 1526 1527 static void tbstream_dev_item_release(struct config_item *item) 1528 { 1529 struct config_group *group = to_config_group(item); 1530 struct tbstream_dev *sdev = tbstream_dev_from_group(group); 1531 1532 misc_deregister(&sdev->misc); 1533 tbstream_dev_put(sdev); 1534 } 1535 1536 static struct configfs_item_operations tbstream_dev_item_ops = { 1537 .release = tbstream_dev_item_release, 1538 }; 1539 1540 static const struct config_item_type tbstream_dev_type = { 1541 .ct_owner = THIS_MODULE, 1542 .ct_item_ops = &tbstream_dev_item_ops, 1543 .ct_attrs = tbstream_dev_attrs, 1544 }; 1545 1546 static void service_get_hopids(struct tb_service *svc, const char *name, 1547 int *in_hopid, int *out_hopid) 1548 { 1549 struct tb_property_dir *dir; 1550 struct tb_property *p; 1551 1552 guard(mutex)(&svc->lock); 1553 1554 /* See if we have directory entry with the matching name */ 1555 p = tb_property_find(svc->remote_properties, name, 1556 TB_PROPERTY_TYPE_DIRECTORY); 1557 if (!p) 1558 return; 1559 1560 dir = p->value.dir; 1561 1562 /* 1563 * We need to reverse the HopIDs on our end so that in becomes 1564 * out and vice versa. 1565 */ 1566 p = tb_property_find(dir, "inhopid", TB_PROPERTY_TYPE_VALUE); 1567 if (p && p->value.immediate >= 8) 1568 *out_hopid = p->value.immediate; 1569 p = tb_property_find(dir, "outhopid", TB_PROPERTY_TYPE_VALUE); 1570 if (p && p->value.immediate >= 8) 1571 *in_hopid = p->value.immediate; 1572 } 1573 1574 static void 1575 tbstream_dev_attach_stream(struct tbstream_dev *sdev, struct tbstream_group *sg) 1576 { 1577 const char *name = config_item_name(&sdev->group.cg_item); 1578 struct tbstream *stream; 1579 1580 stream = tbstream_get(sg->stream); 1581 if (!stream) 1582 return; 1583 1584 scoped_guard(mutex, &sdev->lock) { 1585 sdev->stream = stream; 1586 /* 1587 * If there is no existing configuration (or automatic 1588 * configuration is being used) check if the other side 1589 * has configuration for this and use it. 1590 */ 1591 if (sdev->in_hopid <= 0 && sdev->out_hopid <= 0) 1592 service_get_hopids(stream->svc, name, &sdev->in_hopid, 1593 &sdev->out_hopid); 1594 if (sdev->in_hopid) 1595 tbstream_dev_alloc_in_hopid(sdev, sdev->in_hopid); 1596 if (sdev->out_hopid) 1597 tbstream_dev_alloc_out_hopid(sdev, sdev->out_hopid); 1598 } 1599 1600 service_update_properties(stream->svc, name, sdev->in_hopid, 1601 sdev->out_hopid); 1602 tb_service_properties_changed(stream->svc); 1603 1604 /* Notify any openerers that the stream is now attached */ 1605 wake_up_interruptible(&sdev->wait); 1606 } 1607 1608 static void tbstream_dev_detach_stream(struct tbstream_dev *sdev) 1609 { 1610 const char *name = config_item_name(&sdev->group.cg_item); 1611 struct tbstream *stream; 1612 struct tb_xdomain *xd; 1613 1614 scoped_guard(mutex, &sdev->lock) { 1615 stream = sdev->stream; 1616 if (!stream) 1617 return; 1618 sdev->stream = NULL; 1619 xd = tb_service_parent(stream->svc); 1620 if (sdev->out_hopid > 0) 1621 tb_xdomain_release_out_hopid(xd, sdev->out_hopid); 1622 if (sdev->in_hopid > 0) 1623 tb_xdomain_release_in_hopid(xd, sdev->in_hopid); 1624 } 1625 1626 service_update_properties(stream->svc, name, 0, 0); 1627 tb_service_properties_changed(stream->svc); 1628 1629 tbstream_put(stream); 1630 1631 /* Notify any task that the stream is not valid anymore */ 1632 wake_up_interruptible_poll(&sdev->wait, EPOLLHUP | EPOLLERR); 1633 } 1634 1635 static inline struct tbstream_group * 1636 to_tbstream_group(struct config_group *group) 1637 { 1638 return container_of(group, struct tbstream_group, group); 1639 } 1640 1641 static struct config_group * 1642 tbstream_dev_make_group(struct config_group *group, const char *name) 1643 { 1644 struct tbstream_group *sg = to_tbstream_group(group); 1645 struct tbstream_dev *sdev; 1646 int ret, index; 1647 1648 /* 1649 * We want the names to be suitable for passing as property 1650 * directory names. 1651 */ 1652 if (strlen(name) > TB_PROPERTY_KEY_SIZE) 1653 return ERR_PTR(-ENAMETOOLONG); 1654 1655 sdev = kzalloc_obj(*sdev); 1656 if (!sdev) 1657 return ERR_PTR(-ENOMEM); 1658 1659 index = ida_alloc(&tbstream_indices, GFP_KERNEL); 1660 if (index < 0) { 1661 kfree(sdev); 1662 return ERR_PTR(index); 1663 } 1664 1665 sdev->index = index; 1666 sdev->ring_size = TBSTREAM_DEV_RING_SIZE; 1667 sdev->throttling = TBSTREAM_DEV_THROTTLING; 1668 mutex_init(&sdev->lock); 1669 init_waitqueue_head(&sdev->wait); 1670 INIT_LIST_HEAD(&sdev->list); 1671 /* This point forward tbstream_dev_put() must be used to release sdev */ 1672 kref_init(&sdev->kref); 1673 1674 config_group_init_type_name(&sdev->group, name, &tbstream_dev_type); 1675 1676 scoped_guard(mutex, &sg->lock) 1677 list_add_tail(&sdev->list, &sg->dev_list); 1678 1679 tbstream_dev_attach_stream(sdev, sg); 1680 1681 sdev->misc.name = kasprintf(GFP_KERNEL, "tbstream%d", index); 1682 sdev->misc.minor = MISC_DYNAMIC_MINOR; 1683 sdev->misc.fops = &tbstream_dev_fops; 1684 1685 ret = misc_register(&sdev->misc); 1686 if (ret) { 1687 tbstream_dev_detach_stream(sdev); 1688 scoped_guard(mutex, &sg->lock) 1689 list_del(&sdev->list); 1690 /* Calls tbstream_dev_put() */ 1691 config_group_put(&sdev->group); 1692 return ERR_PTR(ret); 1693 } 1694 1695 return &sdev->group; 1696 } 1697 1698 static void 1699 tbstream_dev_drop_item(struct config_group *group, struct config_item *item) 1700 { 1701 struct config_group *sdev_group = to_config_group(item); 1702 struct tbstream_dev *sdev = tbstream_dev_from_group(sdev_group); 1703 struct tbstream_group *sg = to_tbstream_group(group); 1704 1705 scoped_guard(mutex, &sg->lock) 1706 list_del(&sdev->list); 1707 /* Notify any task that the underlying group was removed */ 1708 sdev->removed = true; 1709 wake_up_interruptible_poll(&sdev->wait, EPOLLHUP | EPOLLERR); 1710 config_item_put(item); 1711 } 1712 1713 static struct configfs_group_operations tbstream_dev_group_ops = { 1714 .make_group = tbstream_dev_make_group, 1715 .drop_item = tbstream_dev_drop_item, 1716 }; 1717 1718 static void tbstream_item_release(struct config_item *item) 1719 { 1720 struct config_group *group = to_config_group(item); 1721 struct tbstream_group *sg = to_tbstream_group(group); 1722 1723 tbstream_put(sg->stream); 1724 kfree(sg); 1725 } 1726 1727 static struct configfs_item_operations tbstream_item_ops = { 1728 .release = tbstream_item_release, 1729 }; 1730 1731 static const struct config_item_type tbstream_dev_group_type = { 1732 .ct_owner = THIS_MODULE, 1733 .ct_group_ops = &tbstream_dev_group_ops, 1734 .ct_item_ops = &tbstream_item_ops, 1735 }; 1736 1737 static struct config_group * 1738 tbstream_make_group(struct config_group *group, const char *name) 1739 { 1740 struct tbstream_group *sg; 1741 struct tbstream *stream; 1742 int domain, index; 1743 u64 route; 1744 1745 /* Make sure the format is correct */ 1746 if (sscanf(name, "%u-%llx.%u", &domain, &route, &index) != 3) 1747 return ERR_PTR(-EINVAL); 1748 1749 sg = kzalloc_obj(*sg); 1750 if (!sg) 1751 return ERR_PTR(-ENOMEM); 1752 1753 mutex_init(&sg->lock); 1754 INIT_LIST_HEAD(&sg->dev_list); 1755 1756 guard(mutex)(&tbstream_lock); 1757 list_for_each_entry(stream, &tbstream_list, list) { 1758 tbstream_get(stream); 1759 if (sysfs_streq(name, dev_name(&stream->svc->dev))) { 1760 sg->stream = stream; 1761 break; 1762 } 1763 tbstream_put(stream); 1764 } 1765 1766 config_group_init_type_name(&sg->group, name, &tbstream_dev_group_type); 1767 return &sg->group; 1768 } 1769 1770 static struct configfs_group_operations tbstream_group_ops = { 1771 .make_group = tbstream_make_group, 1772 }; 1773 1774 static const struct config_item_type tbstream_group_type = { 1775 .ct_owner = THIS_MODULE, 1776 .ct_group_ops = &tbstream_group_ops, 1777 }; 1778 1779 static struct config_group tbstream_group = { 1780 .cg_item = { 1781 .ci_namebuf = "stream", 1782 .ci_type = &tbstream_group_type, 1783 }, 1784 }; 1785 1786 /* Returns reference count increased */ 1787 static struct tbstream_group *tbstream_group_find(struct tbstream *stream) 1788 { 1789 const char *name = dev_name(&stream->svc->dev); 1790 struct config_item *item; 1791 1792 guard(mutex)(&tbstream_group.cg_subsys->su_mutex); 1793 item = config_group_find_item(&tbstream_group, name); 1794 if (!item) 1795 return NULL; 1796 return to_tbstream_group(to_config_group(item)); 1797 } 1798 1799 static void tbstream_group_attach_stream(struct tbstream *stream) 1800 { 1801 struct tbstream_group *sg; 1802 struct tbstream_dev *sdev; 1803 1804 sg = tbstream_group_find(stream); 1805 if (!sg) 1806 return; 1807 1808 guard(mutex)(&sg->lock); 1809 if (WARN_ON(sg->stream)) { 1810 config_group_put(&sg->group); 1811 return; 1812 } 1813 sg->stream = tbstream_get(stream); 1814 /* 1815 * If there are existing stream devices, attach the stream to 1816 * them now. 1817 */ 1818 list_for_each_entry(sdev, &sg->dev_list, list) { 1819 tbstream_dev_get(sdev); 1820 tbstream_dev_attach_stream(sdev, sg); 1821 tbstream_dev_put(sdev); 1822 } 1823 1824 config_group_put(&sg->group); 1825 } 1826 1827 static void tbstream_group_detach_stream(struct tbstream *stream) 1828 { 1829 struct tbstream_group *sg; 1830 struct tbstream_dev *sdev; 1831 1832 sg = tbstream_group_find(stream); 1833 if (!sg) 1834 return; 1835 1836 guard(mutex)(&sg->lock); 1837 if (sg->stream) { 1838 /* Detach this stream from the stream devices */ 1839 list_for_each_entry_reverse(sdev, &sg->dev_list, list) { 1840 tbstream_dev_get(sdev); 1841 tbstream_dev_detach_stream(sdev); 1842 tbstream_dev_put(sdev); 1843 } 1844 tbstream_put(sg->stream); 1845 sg->stream = NULL; 1846 } 1847 1848 config_group_put(&sg->group); 1849 } 1850 1851 static int tbstream_probe(struct tb_service *svc) 1852 { 1853 struct tbstream *stream; 1854 1855 stream = kzalloc_obj(*stream); 1856 if (!stream) 1857 return -ENOMEM; 1858 1859 /* After this point, release stream by calling tbstream_put() */ 1860 kref_init(&stream->kref); 1861 stream->svc = tb_service_get(svc); 1862 INIT_LIST_HEAD(&stream->list); 1863 1864 scoped_guard(mutex, &tbstream_lock) 1865 list_add_tail(&stream->list, &tbstream_list); 1866 1867 tbstream_group_attach_stream(stream); 1868 tb_service_set_drvdata(svc, stream); 1869 return 0; 1870 } 1871 1872 static void tbstream_remove(struct tb_service *svc) 1873 { 1874 struct tbstream *stream = tb_service_get_drvdata(svc); 1875 1876 tbstream_group_detach_stream(stream); 1877 scoped_guard(mutex, &tbstream_lock) 1878 list_del(&stream->list); 1879 tbstream_put(stream); 1880 } 1881 1882 static int __maybe_unused tbstream_suspend(struct device *dev) 1883 { 1884 struct tb_service *svc = tb_to_service(dev); 1885 struct tbstream *stream = tb_service_get_drvdata(svc); 1886 struct tbstream_group *sg; 1887 struct tbstream_dev *sdev; 1888 1889 sg = tbstream_group_find(stream); 1890 if (!sg) 1891 return 0; 1892 1893 list_for_each_entry_reverse(sdev, &sg->dev_list, list) { 1894 tbstream_dev_get(sdev); 1895 /* Stop the stream (if it was open) */ 1896 if (sdev->users) 1897 tbstream_dev_stop(sdev); 1898 tbstream_dev_put(sdev); 1899 } 1900 1901 config_group_put(&sg->group); 1902 return 0; 1903 } 1904 1905 static int __maybe_unused tbstream_resume(struct device *dev) 1906 { 1907 struct tb_service *svc = tb_to_service(dev); 1908 struct tbstream *stream = tb_service_get_drvdata(svc); 1909 struct tbstream_group *sg; 1910 struct tbstream_dev *sdev; 1911 1912 sg = tbstream_group_find(stream); 1913 if (!sg) 1914 return 0; 1915 1916 list_for_each_entry(sdev, &sg->dev_list, list) { 1917 tbstream_dev_get(sdev); 1918 if (sdev->users) { 1919 int ret; 1920 1921 ret = tbstream_dev_start(sdev); 1922 if (ret) { 1923 tbstream_dev_put(sdev); 1924 config_group_put(&sg->group); 1925 return ret; 1926 } 1927 } 1928 tbstream_dev_put(sdev); 1929 } 1930 1931 config_group_put(&sg->group); 1932 return 0; 1933 } 1934 1935 static const struct dev_pm_ops tbstream_pm_ops = { 1936 SET_SYSTEM_SLEEP_PM_OPS(tbstream_suspend, tbstream_resume) 1937 }; 1938 1939 static const struct tb_service_id tbstream_ids[] = { 1940 { TB_SERVICE("stream", 1) }, 1941 { } 1942 }; 1943 MODULE_DEVICE_TABLE(tbsvc, tbstream_ids); 1944 1945 static struct tb_service_driver tbstream_driver = { 1946 .driver = { 1947 .owner = THIS_MODULE, 1948 .name = "thunderbolt_stream", 1949 .pm = &tbstream_pm_ops, 1950 }, 1951 .probe = tbstream_probe, 1952 .remove = tbstream_remove, 1953 .id_table = tbstream_ids, 1954 }; 1955 1956 static int __init tbstream_init(void) 1957 { 1958 int ret; 1959 1960 tbstream_dir = tb_property_create_dir(&tbstream_dir_uuid); 1961 if (!tbstream_dir) 1962 return -ENOMEM; 1963 1964 tb_property_add_immediate(tbstream_dir, "prtcid", 1); 1965 tb_property_add_immediate(tbstream_dir, "prtcvers", 1); 1966 tb_property_add_immediate(tbstream_dir, "prtcrevs", 0); 1967 tb_property_add_immediate(tbstream_dir, "prtcstns", 0); 1968 1969 ret = tb_register_property_dir("stream", tbstream_dir); 1970 if (ret) 1971 goto err_free_dir; 1972 1973 config_group_init(&tbstream_group); 1974 ret = tb_configfs_register_group(&tbstream_group); 1975 if (ret) 1976 goto err_unregister_dir; 1977 1978 ret = tb_register_service_driver(&tbstream_driver); 1979 if (ret) 1980 goto err_unregister_group; 1981 return 0; 1982 1983 err_unregister_group: 1984 tb_configfs_unregister_group(&tbstream_group); 1985 err_unregister_dir: 1986 tb_unregister_property_dir("stream", tbstream_dir); 1987 err_free_dir: 1988 tb_property_free_dir(tbstream_dir); 1989 return ret; 1990 } 1991 module_init(tbstream_init); 1992 1993 static void __exit tbstream_exit(void) 1994 { 1995 tb_unregister_service_driver(&tbstream_driver); 1996 tb_configfs_unregister_group(&tbstream_group); 1997 tb_unregister_property_dir("stream", tbstream_dir); 1998 tb_property_free_dir(tbstream_dir); 1999 ida_destroy(&tbstream_indices); 2000 } 2001 module_exit(tbstream_exit); 2002 2003 MODULE_AUTHOR("Alan Borzeszkowski <alan.borzeszkowski@linux.intel.com>"); 2004 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>"); 2005 MODULE_DESCRIPTION("Stream data over Thunderbolt/USB4 cable"); 2006 MODULE_LICENSE("GPL"); 2007