xref: /linux/include/linux/thunderbolt.h (revision 96ff396e6e0eb754b73f1870ef7e72f2f80cdbe0)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Thunderbolt service API
4  *
5  * Copyright (C) 2014 Andreas Noever <andreas.noever@gmail.com>
6  * Copyright (C) 2017, Intel Corporation
7  * Authors: Michael Jamet <michael.jamet@intel.com>
8  *          Mika Westerberg <mika.westerberg@linux.intel.com>
9  */
10 
11 #ifndef THUNDERBOLT_H_
12 #define THUNDERBOLT_H_
13 
14 #include <linux/types.h>
15 
16 struct config_group;
17 struct fwnode_handle;
18 struct device;
19 
20 #if IS_REACHABLE(CONFIG_USB4)
21 
22 #include <linux/device.h>
23 #include <linux/idr.h>
24 #include <linux/list.h>
25 #include <linux/lockdep.h>
26 #include <linux/mutex.h>
27 #include <linux/device-id/tb.h>
28 #include <linux/pci.h>
29 #include <linux/uuid.h>
30 #include <linux/workqueue.h>
31 
32 enum tb_cfg_pkg_type {
33 	TB_CFG_PKG_READ = 1,
34 	TB_CFG_PKG_WRITE = 2,
35 	TB_CFG_PKG_ERROR = 3,
36 	TB_CFG_PKG_NOTIFY_ACK = 4,
37 	TB_CFG_PKG_EVENT = 5,
38 	TB_CFG_PKG_XDOMAIN_REQ = 6,
39 	TB_CFG_PKG_XDOMAIN_RESP = 7,
40 	TB_CFG_PKG_OVERRIDE = 8,
41 	TB_CFG_PKG_RESET = 9,
42 	TB_CFG_PKG_ICM_EVENT = 10,
43 	TB_CFG_PKG_ICM_CMD = 11,
44 	TB_CFG_PKG_ICM_RESP = 12,
45 };
46 
47 /**
48  * enum tb_security_level - Thunderbolt security level
49  * @TB_SECURITY_NONE: No security, legacy mode
50  * @TB_SECURITY_USER: User approval required at minimum
51  * @TB_SECURITY_SECURE: One time saved key required at minimum
52  * @TB_SECURITY_DPONLY: Only tunnel Display port (and USB)
53  * @TB_SECURITY_USBONLY: Only tunnel USB controller of the connected
54  *			 Thunderbolt dock (and Display Port). All PCIe
55  *			 links downstream of the dock are removed.
56  * @TB_SECURITY_NOPCIE: For USB4 systems this level is used when the
57  *			PCIe tunneling is disabled from the BIOS.
58  */
59 enum tb_security_level {
60 	TB_SECURITY_NONE,
61 	TB_SECURITY_USER,
62 	TB_SECURITY_SECURE,
63 	TB_SECURITY_DPONLY,
64 	TB_SECURITY_USBONLY,
65 	TB_SECURITY_NOPCIE,
66 };
67 
68 /**
69  * struct tb - main thunderbolt bus structure
70  * @dev: Domain device
71  * @lock: Big lock. Must be held when accessing any struct
72  *	  tb_switch / struct tb_port.
73  * @nhi: Pointer to the NHI structure
74  * @ctl: Control channel for this domain
75  * @wq: Ordered workqueue for all domain specific work
76  * @root_switch: Root switch of this domain
77  * @cm_ops: Connection manager specific operations vector
78  * @index: Linux assigned domain number
79  * @security_level: Current security level
80  * @nboot_acl: Number of boot ACLs the domain supports
81  * @privdata: Private connection manager specific data
82  */
83 struct tb {
84 	struct device dev;
85 	struct mutex lock;
86 	struct tb_nhi *nhi;
87 	struct tb_ctl *ctl;
88 	struct workqueue_struct *wq;
89 	struct tb_switch *root_switch;
90 	const struct tb_cm_ops *cm_ops;
91 	int index;
92 	enum tb_security_level security_level;
93 	size_t nboot_acl;
94 	unsigned long privdata[];
95 };
96 
97 extern const struct bus_type tb_bus_type;
98 extern const struct device_type tb_service_type;
99 extern const struct device_type tb_xdomain_type;
100 
101 #define TB_LINKS_PER_PHY_PORT	2
102 
103 static inline unsigned int tb_phy_port_from_link(unsigned int link)
104 {
105 	return (link - 1) / TB_LINKS_PER_PHY_PORT;
106 }
107 
108 /**
109  * struct tb_property_dir - XDomain property directory
110  * @uuid: Directory UUID or %NULL if root directory
111  * @properties: List of properties in this directory
112  *
113  * User needs to provide serialization if needed.
114  */
115 struct tb_property_dir {
116 	const uuid_t *uuid;
117 	struct list_head properties;
118 };
119 
120 enum tb_property_type {
121 	TB_PROPERTY_TYPE_UNKNOWN = 0x00,
122 	TB_PROPERTY_TYPE_DIRECTORY = 0x44,
123 	TB_PROPERTY_TYPE_DATA = 0x64,
124 	TB_PROPERTY_TYPE_TEXT = 0x74,
125 	TB_PROPERTY_TYPE_VALUE = 0x76,
126 };
127 
128 #define TB_PROPERTY_KEY_SIZE	8
129 
130 /**
131  * struct tb_property - XDomain property
132  * @list: Used to link properties together in a directory
133  * @key: Key for the property (always terminated).
134  * @type: Type of the property
135  * @length: Length of the property data in dwords
136  * @value: Property value
137  *
138  * Users use @type to determine which field in @value is filled.
139  */
140 struct tb_property {
141 	struct list_head list;
142 	char key[TB_PROPERTY_KEY_SIZE + 1];
143 	enum tb_property_type type;
144 	size_t length;
145 	union {
146 		struct tb_property_dir *dir;
147 		u8 *data;
148 		char *text;
149 		u32 immediate;
150 	} value;
151 };
152 
153 struct tb_property_dir *tb_property_parse_dir(const u32 *block,
154 					      size_t block_len);
155 ssize_t tb_property_format_dir(const struct tb_property_dir *dir, u32 *block,
156 			       size_t block_len);
157 struct tb_property_dir *tb_property_copy_dir(const struct tb_property_dir *dir);
158 int tb_property_merge_dir(struct tb_property_dir *parent,
159 			  const struct tb_property_dir *dir,
160 			  bool replace);
161 struct tb_property_dir *tb_property_create_dir(const uuid_t *uuid);
162 void tb_property_free_dir(struct tb_property_dir *dir);
163 int tb_property_add_immediate(struct tb_property_dir *parent, const char *key,
164 			      u32 value);
165 int tb_property_add_data(struct tb_property_dir *parent, const char *key,
166 			 const void *buf, size_t buflen);
167 int tb_property_add_text(struct tb_property_dir *parent, const char *key,
168 			 const char *text);
169 int tb_property_add_dir(struct tb_property_dir *parent, const char *key,
170 			struct tb_property_dir *dir);
171 void tb_property_remove(struct tb_property *tb_property);
172 struct tb_property *tb_property_find(struct tb_property_dir *dir,
173 			const char *key, enum tb_property_type type);
174 struct tb_property *tb_property_get_next(struct tb_property_dir *dir,
175 					 struct tb_property *prev);
176 
177 #define tb_property_for_each(dir, property)			\
178 	for (property = tb_property_get_next(dir, NULL);	\
179 	     property;						\
180 	     property = tb_property_get_next(dir, property))
181 
182 int tb_register_property_dir(const char *key, struct tb_property_dir *dir);
183 void tb_unregister_property_dir(const char *key, struct tb_property_dir *dir);
184 
185 /**
186  * enum tb_link_width - Thunderbolt/USB4 link width
187  * @TB_LINK_WIDTH_SINGLE: Single lane link
188  * @TB_LINK_WIDTH_DUAL: Dual lane symmetric link
189  * @TB_LINK_WIDTH_ASYM_TX: Dual lane asymmetric Gen 4 link with 3 transmitters
190  * @TB_LINK_WIDTH_ASYM_RX: Dual lane asymmetric Gen 4 link with 3 receivers
191  */
192 enum tb_link_width {
193 	TB_LINK_WIDTH_SINGLE = BIT(0),
194 	TB_LINK_WIDTH_DUAL = BIT(1),
195 	TB_LINK_WIDTH_ASYM_TX = BIT(2),
196 	TB_LINK_WIDTH_ASYM_RX = BIT(3),
197 };
198 
199 /**
200  * struct tb_xdomain - Cross-domain (XDomain) connection
201  * @dev: XDomain device
202  * @tb: Pointer to the domain
203  * @remote_uuid: UUID of the remote domain (host)
204  * @local_uuid: Cached local UUID
205  * @route: Route string the other domain can be reached
206  * @vendor: Vendor ID of the remote domain
207  * @device: Device ID of the demote domain
208  * @local_max_hopid: Maximum input HopID of this host
209  * @remote_max_hopid: Maximum input HopID of the remote host
210  * @lock: Lock to serialize access to the following fields of this structure
211  * @vendor_name: Name of the vendor (or %NULL if not known)
212  * @device_name: Name of the device (or %NULL if not known)
213  * @link_speed: Speed of the link in Gb/s
214  * @link_width: Width of the downstream facing link
215  * @link_usb4: Downstream link is USB4
216  * @is_unplugged: The XDomain is unplugged
217  * @removing: Set by tb_xdomain_remove() under @lock to prevent
218  *	      concurrent delayed work queueing
219  * @needs_uuid: If the XDomain does not have @remote_uuid it will be
220  *		queried first
221  * @service_ids: Used to generate IDs for the services
222  * @in_hopids: Input HopIDs for DMA tunneling
223  * @out_hopids: Output HopIDs for DMA tunneling
224  * @local_property_block: Local block of properties
225  * @local_property_block_gen: Generation of @local_property_block
226  * @local_property_block_len: Length of the @local_property_block in dwords
227  * @remote_properties: Properties exported by the remote domain
228  * @remote_property_block_gen: Generation of @remote_properties
229  * @state: Next XDomain discovery state to run
230  * @state_work: Work used to run the next state
231  * @state_retries: Number of retries remain for the state
232  * @properties_changed_work: Work used to notify the remote domain that
233  *			     our properties have changed
234  * @properties_changed_retries: Number of times left to send properties
235  *				changed notification
236  * @bonding_possible: True if lane bonding is possible on local side
237  * @target_link_width: Target link width from the remote host
238  * @ntunnels: Keeps track of how many tunnels go through this XDomain
239  * @link: Root switch link the remote domain is connected (ICM only)
240  * @depth: Depth in the chain the remote domain is connected (ICM only)
241  *
242  * This structure represents connection across two domains (hosts).
243  * Each XDomain contains zero or more services which are exposed as
244  * &struct tb_service objects.
245  *
246  * Service drivers may access this structure if they need to enumerate
247  * non-standard properties but they need hold @lock when doing so
248  * because properties can be changed asynchronously in response to
249  * changes in the remote domain.
250  */
251 struct tb_xdomain {
252 	struct device dev;
253 	struct tb *tb;
254 	uuid_t *remote_uuid;
255 	const uuid_t *local_uuid;
256 	u64 route;
257 	u16 vendor;
258 	u16 device;
259 	unsigned int local_max_hopid;
260 	unsigned int remote_max_hopid;
261 	struct mutex lock;
262 	const char *vendor_name;
263 	const char *device_name;
264 	unsigned int link_speed;
265 	enum tb_link_width link_width;
266 	bool link_usb4;
267 	bool is_unplugged;
268 	bool removing;
269 	bool needs_uuid;
270 	struct ida service_ids;
271 	struct ida in_hopids;
272 	struct ida out_hopids;
273 	u32 *local_property_block;
274 	u32 local_property_block_gen;
275 	u32 local_property_block_len;
276 	struct tb_property_dir *remote_properties;
277 	u32 remote_property_block_gen;
278 	int state;
279 	struct delayed_work state_work;
280 	int state_retries;
281 	struct delayed_work properties_changed_work;
282 	int properties_changed_retries;
283 	bool bonding_possible;
284 	u8 target_link_width;
285 	atomic_t ntunnels;
286 	u8 link;
287 	u8 depth;
288 };
289 
290 int tb_xdomain_lane_bonding_enable(struct tb_xdomain *xd);
291 void tb_xdomain_lane_bonding_disable(struct tb_xdomain *xd);
292 int tb_xdomain_alloc_in_hopid(struct tb_xdomain *xd, int hopid);
293 void tb_xdomain_release_in_hopid(struct tb_xdomain *xd, int hopid);
294 int tb_xdomain_alloc_out_hopid(struct tb_xdomain *xd, int hopid);
295 void tb_xdomain_release_out_hopid(struct tb_xdomain *xd, int hopid);
296 int tb_xdomain_enable_paths(struct tb_xdomain *xd, int transmit_path,
297 			    int transmit_ring, int receive_path,
298 			    int receive_ring);
299 int tb_xdomain_disable_paths(struct tb_xdomain *xd, int transmit_path,
300 			     int transmit_ring, int receive_path,
301 			     int receive_ring);
302 
303 static inline int tb_xdomain_disable_all_paths(struct tb_xdomain *xd)
304 {
305 	return tb_xdomain_disable_paths(xd, -1, -1, -1, -1);
306 }
307 
308 struct tb_xdomain *tb_xdomain_find_by_uuid(struct tb *tb, const uuid_t *uuid);
309 struct tb_xdomain *tb_xdomain_find_by_route(struct tb *tb, u64 route);
310 
311 static inline struct tb_xdomain *
312 tb_xdomain_find_by_uuid_locked(struct tb *tb, const uuid_t *uuid)
313 {
314 	struct tb_xdomain *xd;
315 
316 	mutex_lock(&tb->lock);
317 	xd = tb_xdomain_find_by_uuid(tb, uuid);
318 	mutex_unlock(&tb->lock);
319 
320 	return xd;
321 }
322 
323 static inline struct tb_xdomain *
324 tb_xdomain_find_by_route_locked(struct tb *tb, u64 route)
325 {
326 	struct tb_xdomain *xd;
327 
328 	mutex_lock(&tb->lock);
329 	xd = tb_xdomain_find_by_route(tb, route);
330 	mutex_unlock(&tb->lock);
331 
332 	return xd;
333 }
334 
335 static inline struct tb_xdomain *tb_xdomain_get(struct tb_xdomain *xd)
336 {
337 	if (xd)
338 		get_device(&xd->dev);
339 	return xd;
340 }
341 
342 static inline void tb_xdomain_put(struct tb_xdomain *xd)
343 {
344 	if (xd)
345 		put_device(&xd->dev);
346 }
347 
348 static inline bool tb_is_xdomain(const struct device *dev)
349 {
350 	return dev->type == &tb_xdomain_type;
351 }
352 
353 static inline struct tb_xdomain *tb_to_xdomain(struct device *dev)
354 {
355 	if (tb_is_xdomain(dev))
356 		return container_of(dev, struct tb_xdomain, dev);
357 	return NULL;
358 }
359 
360 int tb_xdomain_response(struct tb_xdomain *xd, const void *response,
361 			size_t size, enum tb_cfg_pkg_type type);
362 int tb_xdomain_request(struct tb_xdomain *xd, const void *request,
363 		       size_t request_size, enum tb_cfg_pkg_type request_type,
364 		       void *response, size_t response_size,
365 		       enum tb_cfg_pkg_type response_type,
366 		       unsigned int timeout_msec);
367 
368 /**
369  * struct tb_protocol_handler - Protocol specific handler
370  * @uuid: XDomain messages with this UUID are dispatched to this handler
371  * @callback: Callback called with the XDomain message. Returning %1
372  *	      here tells the XDomain core that the message was handled
373  *	      by this handler and should not be forwared to other
374  *	      handlers.
375  * @data: Data passed with the callback
376  * @list: Handlers are linked using this
377  *
378  * Thunderbolt services can hook into incoming XDomain requests by
379  * registering protocol handler. Only limitation is that the XDomain
380  * discovery protocol UUID cannot be registered since it is handled by
381  * the core XDomain code.
382  *
383  * The @callback must check that the message is really directed to the
384  * service the driver implements.
385  */
386 struct tb_protocol_handler {
387 	const uuid_t *uuid;
388 	int (*callback)(const void *buf, size_t size, void *data);
389 	void *data;
390 	struct list_head list;
391 };
392 
393 int tb_register_protocol_handler(struct tb_protocol_handler *handler);
394 void tb_unregister_protocol_handler(struct tb_protocol_handler *handler);
395 
396 /**
397  * struct tb_service - Thunderbolt service
398  * @dev: XDomain device
399  * @id: ID of the service (shown in sysfs)
400  * @key: Protocol key from the properties directory
401  * @prtcid: Protocol ID from the properties directory
402  * @prtcvers: Protocol version from the properties directory
403  * @prtcrevs: Protocol software revision from the properties directory
404  * @prtcstns: Protocol settings mask from the properties directory
405  * @lock: Protects this structure
406  * @local_properties: Properties owned by the service driver
407  * @remote_properties: Properties read from the remote service. These
408  *		       are read-only.
409  * @debugfs_dir: Pointer to the service debugfs directory. Always created
410  *		 when debugfs is enabled. Can be used by service drivers to
411  *		 add their own entries under the service.
412  *
413  * Each domain exposes set of services it supports as collection of
414  * properties. For each service there will be one corresponding
415  * &struct tb_service. Service drivers are bound to these.
416  *
417  * Service drivers can add their own dynamic properties to
418  * @local_properties but whenever they do so @lock must be held.
419  */
420 struct tb_service {
421 	struct device dev;
422 	int id;
423 	const char *key;
424 	u32 prtcid;
425 	u32 prtcvers;
426 	u32 prtcrevs;
427 	u32 prtcstns;
428 	struct mutex lock;
429 	struct tb_property_dir *local_properties;
430 	struct tb_property_dir *remote_properties;
431 	struct dentry *debugfs_dir;
432 };
433 
434 static inline struct tb_service *tb_service_get(struct tb_service *svc)
435 {
436 	if (svc)
437 		get_device(&svc->dev);
438 	return svc;
439 }
440 
441 static inline void tb_service_put(struct tb_service *svc)
442 {
443 	if (svc)
444 		put_device(&svc->dev);
445 }
446 
447 static inline bool tb_is_service(const struct device *dev)
448 {
449 	return dev->type == &tb_service_type;
450 }
451 
452 static inline struct tb_service *tb_to_service(struct device *dev)
453 {
454 	if (tb_is_service(dev))
455 		return container_of(dev, struct tb_service, dev);
456 	return NULL;
457 }
458 
459 /**
460  * struct tb_service_driver - Thunderbolt service driver
461  * @driver: Driver structure
462  * @probe: Called when the driver is probed
463  * @remove: Called when the driver is removed (optional)
464  * @shutdown: Called at shutdown time to stop the service (optional)
465  * @id_table: Table of service identifiers the driver supports
466  */
467 struct tb_service_driver {
468 	struct device_driver driver;
469 	int (*probe)(struct tb_service *svc);
470 	void (*remove)(struct tb_service *svc);
471 	void (*shutdown)(struct tb_service *svc);
472 	const struct tb_service_id *id_table;
473 };
474 
475 #define TB_SERVICE(key, id)				\
476 	.match_flags = TBSVC_MATCH_PROTOCOL_KEY |	\
477 		       TBSVC_MATCH_PROTOCOL_ID,		\
478 	.protocol_key = (key),				\
479 	.protocol_id = (id)
480 
481 int tb_register_service_driver(struct tb_service_driver *drv);
482 void tb_unregister_service_driver(struct tb_service_driver *drv);
483 
484 static inline void *tb_service_get_drvdata(const struct tb_service *svc)
485 {
486 	return dev_get_drvdata(&svc->dev);
487 }
488 
489 static inline void tb_service_set_drvdata(struct tb_service *svc, void *data)
490 {
491 	dev_set_drvdata(&svc->dev, data);
492 }
493 
494 static inline struct tb_xdomain *tb_service_parent(struct tb_service *svc)
495 {
496 	return tb_to_xdomain(svc->dev.parent);
497 }
498 
499 void tb_service_properties_changed(struct tb_service *svc);
500 
501 /**
502  * struct tb_nhi - thunderbolt native host interface
503  * @lock: Must be held during ring creation/destruction. Is acquired by
504  *	  interrupt_work when dispatching interrupts to individual rings.
505  * @dev: Device associated with this NHI instance
506  * @ops: NHI specific optional ops
507  * @iobase: MMIO space of the NHI
508  * @tx_rings: All Tx rings available on this host controller
509  * @rx_rings: All Rx rings available on this host controller
510  * @going_away: The host controller device is about to disappear so when
511  *		this flag is set, avoid touching the hardware anymore.
512  * @iommu_dma_protection: An IOMMU will isolate external-facing ports.
513  * @interrupt_work: Work scheduled to handle ring interrupt when no
514  *		    MSI-X is used.
515  * @hop_count: Number of rings (end point hops) supported by NHI.
516  * @quirks: NHI specific quirks if any
517  * @domain_released: Completed when domain has been fully released
518  * @host_reset: Host router was reset on driver load, or forced on system
519  *		shutdown/reboot. When set, tb_stop() asserts DPR on connected
520  *		downstream ports to signal disconnect before tearing down the
521  *		router tree. Only Thunderbolt 3 devices are reset; USB4
522  *		routers are skipped.
523  */
524 struct tb_nhi {
525 	spinlock_t lock;
526 	struct device *dev;
527 	const struct tb_nhi_ops *ops;
528 	void __iomem *iobase;
529 	struct tb_ring **tx_rings;
530 	struct tb_ring **rx_rings;
531 	bool going_away;
532 	bool iommu_dma_protection;
533 	struct work_struct interrupt_work;
534 	u32 hop_count;
535 	unsigned long quirks;
536 	struct completion domain_released;
537 	bool host_reset;
538 };
539 
540 /**
541  * struct tb_ring - thunderbolt TX or RX ring associated with a NHI
542  * @lock: Lock serializing actions to this ring. Must be acquired after
543  *	  nhi->lock.
544  * @nhi: Pointer to the native host controller interface
545  * @size: Size of the ring
546  * @hop: Hop (DMA channel) associated with this ring
547  * @head: Head of the ring (write next descriptor here)
548  * @tail: Tail of the ring (complete next descriptor here)
549  * @descriptors: Allocated descriptors for this ring
550  * @descriptors_dma: DMA address of descriptors for this ring
551  * @queue: Queue holding frames to be transferred over this ring
552  * @in_flight: Queue holding frames that are currently in flight
553  * @work: Interrupt work structure
554  * @is_tx: Is the ring Tx or Rx
555  * @running: Is the ring running
556  * @irq: MSI-X irq number if the ring uses MSI-X. %0 otherwise.
557  * @vector: MSI-X vector number the ring uses (only set if @irq is > 0)
558  * @flags: Ring specific flags
559  * @e2e_tx_hop: Transmit HopID when E2E is enabled. Only applicable to
560  *		RX ring. For TX ring this should be set to %0.
561  * @sof_mask: Bit mask used to detect start of frame PDF
562  * @eof_mask: Bit mask used to detect end of frame PDF
563  * @start_poll: Called when ring interrupt is triggered to start
564  *		polling. Passing %NULL keeps the ring in interrupt mode.
565  * @poll_data: Data passed to @start_poll
566  * @interval_nsec: Interval counter if interrupt throttling is to be
567  *		   used with this ring (in ns)
568  * @wait: Used to signal that the ring may be empty now
569  * @lock_key: Lock validator class key per-ring
570  */
571 struct tb_ring {
572 	spinlock_t lock;
573 	struct tb_nhi *nhi;
574 	int size;
575 	int hop;
576 	int head;
577 	int tail;
578 	struct ring_desc *descriptors;
579 	dma_addr_t descriptors_dma;
580 	struct list_head queue;
581 	struct list_head in_flight;
582 	struct work_struct work;
583 	bool is_tx:1;
584 	bool running:1;
585 	int irq;
586 	u8 vector;
587 	unsigned int flags;
588 	int e2e_tx_hop;
589 	u16 sof_mask;
590 	u16 eof_mask;
591 	void (*start_poll)(void *data);
592 	void *poll_data;
593 	unsigned int interval_nsec;
594 	wait_queue_head_t wait;
595 	struct lock_class_key lock_key;
596 };
597 
598 /* Leave ring interrupt enabled on suspend */
599 #define RING_FLAG_NO_SUSPEND	BIT(0)
600 /* Configure the ring to be in frame mode */
601 #define RING_FLAG_FRAME		BIT(1)
602 /* Enable end-to-end flow control */
603 #define RING_FLAG_E2E		BIT(2)
604 /* Do not enable interrupt for the ring */
605 #define RING_FLAG_NO_INTERRUPT	BIT(3)
606 
607 struct ring_frame;
608 typedef void (*ring_cb)(struct tb_ring *, struct ring_frame *, bool canceled);
609 
610 /**
611  * enum ring_desc_flags - Flags for DMA ring descriptor
612  * @RING_DESC_ISOCH: Enable isonchronous DMA (Tx only)
613  * @RING_DESC_CRC_ERROR: In frame mode CRC check failed for the frame (Rx only)
614  * @RING_DESC_COMPLETED: Descriptor completed (set by NHI)
615  * @RING_DESC_POSTED: Always set this
616  * @RING_DESC_BUFFER_OVERRUN: RX buffer overrun
617  * @RING_DESC_INTERRUPT: Request an interrupt on completion
618  */
619 enum ring_desc_flags {
620 	RING_DESC_ISOCH = 0x1,
621 	RING_DESC_CRC_ERROR = 0x1,
622 	RING_DESC_COMPLETED = 0x2,
623 	RING_DESC_POSTED = 0x4,
624 	RING_DESC_BUFFER_OVERRUN = 0x04,
625 	RING_DESC_INTERRUPT = 0x8,
626 };
627 
628 /**
629  * struct ring_frame - For use with ring_rx/ring_tx
630  * @buffer_phy: DMA mapped address of the frame
631  * @callback: Callback called when the frame is finished (optional)
632  * @list: Frame is linked to a queue using this
633  * @size: Size of the frame in bytes (%0 means %4096)
634  * @flags: Flags for the frame (see &enum ring_desc_flags)
635  * @eof: End of frame protocol defined field
636  * @sof: Start of frame protocol defined field
637  */
638 struct ring_frame {
639 	dma_addr_t buffer_phy;
640 	ring_cb callback;
641 	struct list_head list;
642 	u32 size:12;
643 	u32 flags:12;
644 	u32 eof:4;
645 	u32 sof:4;
646 };
647 
648 /* Minimum size for ring_rx */
649 #define TB_FRAME_SIZE		256
650 #define TB_MAX_FRAME_SIZE	4096
651 
652 static inline size_t tb_ring_frame_size(const struct ring_frame *frame)
653 {
654 	if (frame->size)
655 		return frame->size;
656 	return TB_MAX_FRAME_SIZE;
657 }
658 
659 static inline size_t tb_ring_size(const struct tb_ring *ring)
660 {
661 	return ring->size;
662 }
663 
664 struct tb_ring *tb_ring_alloc_tx(struct tb_nhi *nhi, int hop, int size,
665 				 unsigned int flags);
666 struct tb_ring *tb_ring_alloc_rx(struct tb_nhi *nhi, int hop, int size,
667 				 unsigned int flags, int e2e_tx_hop,
668 				 u16 sof_mask, u16 eof_mask,
669 				 void (*start_poll)(void *), void *poll_data);
670 void tb_ring_start(struct tb_ring *ring);
671 bool tb_ring_flush(struct tb_ring *ring, unsigned int timeout_msec);
672 void tb_ring_stop(struct tb_ring *ring);
673 void tb_ring_free(struct tb_ring *ring);
674 
675 int __tb_ring_enqueue(struct tb_ring *ring, struct ring_frame *frame);
676 
677 /**
678  * tb_ring_rx() - enqueue a frame on an RX ring
679  * @ring: Ring to enqueue the frame
680  * @frame: Frame to enqueue
681  *
682  * @frame->buffer, @frame->buffer_phy have to be set. The buffer must
683  * contain at least %TB_FRAME_SIZE bytes.
684  *
685  * @frame->callback will be invoked with @frame->size, @frame->flags,
686  * @frame->eof, @frame->sof set once the frame has been received.
687  *
688  * If ring_stop() is called after the packet has been enqueued
689  * @frame->callback will be called with canceled set to true.
690  *
691  * Return: %-ESHUTDOWN if ring_stop() has been called, %0 otherwise.
692  */
693 static inline int tb_ring_rx(struct tb_ring *ring, struct ring_frame *frame)
694 {
695 	WARN_ON(ring->is_tx);
696 	return __tb_ring_enqueue(ring, frame);
697 }
698 
699 /**
700  * tb_ring_tx() - enqueue a frame on an TX ring
701  * @ring: Ring the enqueue the frame
702  * @frame: Frame to enqueue
703  *
704  * @frame->buffer, @frame->buffer_phy, @frame->size, @frame->eof and
705  * @frame->sof have to be set.
706  *
707  * @frame->callback will be invoked with once the frame has been transmitted.
708  *
709  * If ring_stop() is called after the packet has been enqueued @frame->callback
710  * will be called with canceled set to true.
711  *
712  * Return: %-ESHUTDOWN if ring_stop has been called, %0 otherwise.
713  */
714 static inline int tb_ring_tx(struct tb_ring *ring, struct ring_frame *frame)
715 {
716 	WARN_ON(!ring->is_tx);
717 	return __tb_ring_enqueue(ring, frame);
718 }
719 
720 /* Used only when the ring is in polling mode */
721 struct ring_frame *tb_ring_poll(struct tb_ring *ring);
722 void tb_ring_poll_complete(struct tb_ring *ring);
723 
724 int tb_ring_throttling(struct tb_ring *ring, unsigned int interval_nsec);
725 
726 /**
727  * tb_ring_dma_device() - Return device used for DMA mapping
728  * @ring: Ring whose DMA device is retrieved
729  *
730  * Use this function when you are mapping DMA for buffers that are
731  * passed to the ring for sending/receiving.
732  *
733  * Return: Pointer to device used for DMA mapping.
734  */
735 static inline struct device *tb_ring_dma_device(struct tb_ring *ring)
736 {
737 	return ring->nhi->dev;
738 }
739 
740 bool usb4_usb3_port_match(struct device *usb4_port_dev,
741 			  const struct fwnode_handle *usb3_port_fwnode);
742 
743 #if IS_REACHABLE(CONFIG_CONFIGFS_FS)
744 int tb_configfs_register_group(struct config_group *group);
745 void tb_configfs_unregister_group(struct config_group *group);
746 #endif
747 
748 #else /* CONFIG_USB4 */
749 static inline bool usb4_usb3_port_match(struct device *usb4_port_dev,
750 					const struct fwnode_handle *usb3_port_fwnode)
751 {
752 	return false;
753 }
754 #endif /* CONFIG_USB4 */
755 
756 #endif /* THUNDERBOLT_H_ */
757