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