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