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