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