xref: /openbmc/linux/drivers/thunderbolt/tb.h (revision 3ddc8b84)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Thunderbolt driver - bus logic (NHI independent)
4  *
5  * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
6  * Copyright (C) 2018, Intel Corporation
7  */
8 
9 #ifndef TB_H_
10 #define TB_H_
11 
12 #include <linux/nvmem-provider.h>
13 #include <linux/pci.h>
14 #include <linux/thunderbolt.h>
15 #include <linux/uuid.h>
16 #include <linux/bitfield.h>
17 
18 #include "tb_regs.h"
19 #include "ctl.h"
20 #include "dma_port.h"
21 
22 /* Keep link controller awake during update */
23 #define QUIRK_FORCE_POWER_LINK_CONTROLLER		BIT(0)
24 /* Disable CLx if not supported */
25 #define QUIRK_NO_CLX					BIT(1)
26 
27 /**
28  * struct tb_nvm - Structure holding NVM information
29  * @dev: Owner of the NVM
30  * @major: Major version number of the active NVM portion
31  * @minor: Minor version number of the active NVM portion
32  * @id: Identifier used with both NVM portions
33  * @active: Active portion NVMem device
34  * @active_size: Size in bytes of the active NVM
35  * @non_active: Non-active portion NVMem device
36  * @buf: Buffer where the NVM image is stored before it is written to
37  *	 the actual NVM flash device
38  * @buf_data_start: Where the actual image starts after skipping
39  *		    possible headers
40  * @buf_data_size: Number of bytes actually consumed by the new NVM
41  *		   image
42  * @authenticating: The device is authenticating the new NVM
43  * @flushed: The image has been flushed to the storage area
44  * @vops: Router vendor specific NVM operations (optional)
45  *
46  * The user of this structure needs to handle serialization of possible
47  * concurrent access.
48  */
49 struct tb_nvm {
50 	struct device *dev;
51 	u32 major;
52 	u32 minor;
53 	int id;
54 	struct nvmem_device *active;
55 	size_t active_size;
56 	struct nvmem_device *non_active;
57 	void *buf;
58 	void *buf_data_start;
59 	size_t buf_data_size;
60 	bool authenticating;
61 	bool flushed;
62 	const struct tb_nvm_vendor_ops *vops;
63 };
64 
65 enum tb_nvm_write_ops {
66 	WRITE_AND_AUTHENTICATE = 1,
67 	WRITE_ONLY = 2,
68 	AUTHENTICATE_ONLY = 3,
69 };
70 
71 #define TB_SWITCH_KEY_SIZE		32
72 #define TB_SWITCH_MAX_DEPTH		6
73 #define USB4_SWITCH_MAX_DEPTH		5
74 
75 /**
76  * enum tb_switch_tmu_mode - TMU mode
77  * @TB_SWITCH_TMU_MODE_OFF: TMU is off
78  * @TB_SWITCH_TMU_MODE_LOWRES: Uni-directional, normal mode
79  * @TB_SWITCH_TMU_MODE_HIFI_UNI: Uni-directional, HiFi mode
80  * @TB_SWITCH_TMU_MODE_HIFI_BI: Bi-directional, HiFi mode
81  * @TB_SWITCH_TMU_MODE_MEDRES_ENHANCED_UNI: Enhanced Uni-directional, MedRes mode
82  *
83  * Ordering is based on TMU accuracy level (highest last).
84  */
85 enum tb_switch_tmu_mode {
86 	TB_SWITCH_TMU_MODE_OFF,
87 	TB_SWITCH_TMU_MODE_LOWRES,
88 	TB_SWITCH_TMU_MODE_HIFI_UNI,
89 	TB_SWITCH_TMU_MODE_HIFI_BI,
90 	TB_SWITCH_TMU_MODE_MEDRES_ENHANCED_UNI,
91 };
92 
93 /**
94  * struct tb_switch_tmu - Structure holding router TMU configuration
95  * @cap: Offset to the TMU capability (%0 if not found)
96  * @has_ucap: Does the switch support uni-directional mode
97  * @mode: TMU mode related to the upstream router. Reflects the HW
98  *	  setting. Don't care for host router.
99  * @mode_request: TMU mode requested to set. Related to upstream router.
100  *		   Don't care for host router.
101  */
102 struct tb_switch_tmu {
103 	int cap;
104 	bool has_ucap;
105 	enum tb_switch_tmu_mode mode;
106 	enum tb_switch_tmu_mode mode_request;
107 };
108 
109 /**
110  * struct tb_switch - a thunderbolt switch
111  * @dev: Device for the switch
112  * @config: Switch configuration
113  * @ports: Ports in this switch
114  * @dma_port: If the switch has port supporting DMA configuration based
115  *	      mailbox this will hold the pointer to that (%NULL
116  *	      otherwise). If set it also means the switch has
117  *	      upgradeable NVM.
118  * @tmu: The switch TMU configuration
119  * @tb: Pointer to the domain the switch belongs to
120  * @uid: Unique ID of the switch
121  * @uuid: UUID of the switch (or %NULL if not supported)
122  * @vendor: Vendor ID of the switch
123  * @device: Device ID of the switch
124  * @vendor_name: Name of the vendor (or %NULL if not known)
125  * @device_name: Name of the device (or %NULL if not known)
126  * @link_speed: Speed of the link in Gb/s
127  * @link_width: Width of the upstream facing link
128  * @link_usb4: Upstream link is USB4
129  * @generation: Switch Thunderbolt generation
130  * @cap_plug_events: Offset to the plug events capability (%0 if not found)
131  * @cap_vsec_tmu: Offset to the TMU vendor specific capability (%0 if not found)
132  * @cap_lc: Offset to the link controller capability (%0 if not found)
133  * @cap_lp: Offset to the low power (CLx for TBT) capability (%0 if not found)
134  * @is_unplugged: The switch is going away
135  * @drom: DROM of the switch (%NULL if not found)
136  * @nvm: Pointer to the NVM if the switch has one (%NULL otherwise)
137  * @no_nvm_upgrade: Prevent NVM upgrade of this switch
138  * @safe_mode: The switch is in safe-mode
139  * @boot: Whether the switch was already authorized on boot or not
140  * @rpm: The switch supports runtime PM
141  * @authorized: Whether the switch is authorized by user or policy
142  * @security_level: Switch supported security level
143  * @debugfs_dir: Pointer to the debugfs structure
144  * @key: Contains the key used to challenge the device or %NULL if not
145  *	 supported. Size of the key is %TB_SWITCH_KEY_SIZE.
146  * @connection_id: Connection ID used with ICM messaging
147  * @connection_key: Connection key used with ICM messaging
148  * @link: Root switch link this switch is connected (ICM only)
149  * @depth: Depth in the chain this switch is connected (ICM only)
150  * @rpm_complete: Completion used to wait for runtime resume to
151  *		  complete (ICM only)
152  * @quirks: Quirks used for this Thunderbolt switch
153  * @credit_allocation: Are the below buffer allocation parameters valid
154  * @max_usb3_credits: Router preferred number of buffers for USB 3.x
155  * @min_dp_aux_credits: Router preferred minimum number of buffers for DP AUX
156  * @min_dp_main_credits: Router preferred minimum number of buffers for DP MAIN
157  * @max_pcie_credits: Router preferred number of buffers for PCIe
158  * @max_dma_credits: Router preferred number of buffers for DMA/P2P
159  * @clx: CLx states on the upstream link of the router
160  *
161  * When the switch is being added or removed to the domain (other
162  * switches) you need to have domain lock held.
163  *
164  * In USB4 terminology this structure represents a router.
165  *
166  * Note @link_width is not the same as whether link is bonded or not.
167  * For Gen 4 links the link is also bonded when it is asymmetric. The
168  * correct way to find out whether the link is bonded or not is to look
169  * @bonded field of the upstream port.
170  */
171 struct tb_switch {
172 	struct device dev;
173 	struct tb_regs_switch_header config;
174 	struct tb_port *ports;
175 	struct tb_dma_port *dma_port;
176 	struct tb_switch_tmu tmu;
177 	struct tb *tb;
178 	u64 uid;
179 	uuid_t *uuid;
180 	u16 vendor;
181 	u16 device;
182 	const char *vendor_name;
183 	const char *device_name;
184 	unsigned int link_speed;
185 	enum tb_link_width link_width;
186 	bool link_usb4;
187 	unsigned int generation;
188 	int cap_plug_events;
189 	int cap_vsec_tmu;
190 	int cap_lc;
191 	int cap_lp;
192 	bool is_unplugged;
193 	u8 *drom;
194 	struct tb_nvm *nvm;
195 	bool no_nvm_upgrade;
196 	bool safe_mode;
197 	bool boot;
198 	bool rpm;
199 	unsigned int authorized;
200 	enum tb_security_level security_level;
201 	struct dentry *debugfs_dir;
202 	u8 *key;
203 	u8 connection_id;
204 	u8 connection_key;
205 	u8 link;
206 	u8 depth;
207 	struct completion rpm_complete;
208 	unsigned long quirks;
209 	bool credit_allocation;
210 	unsigned int max_usb3_credits;
211 	unsigned int min_dp_aux_credits;
212 	unsigned int min_dp_main_credits;
213 	unsigned int max_pcie_credits;
214 	unsigned int max_dma_credits;
215 	unsigned int clx;
216 };
217 
218 /**
219  * struct tb_bandwidth_group - Bandwidth management group
220  * @tb: Pointer to the domain the group belongs to
221  * @index: Index of the group (aka Group_ID). Valid values %1-%7
222  * @ports: DP IN adapters belonging to this group are linked here
223  *
224  * Any tunnel that requires isochronous bandwidth (that's DP for now) is
225  * attached to a bandwidth group. All tunnels going through the same
226  * USB4 links share the same group and can dynamically distribute the
227  * bandwidth within the group.
228  */
229 struct tb_bandwidth_group {
230 	struct tb *tb;
231 	int index;
232 	struct list_head ports;
233 };
234 
235 /**
236  * struct tb_port - a thunderbolt port, part of a tb_switch
237  * @config: Cached port configuration read from registers
238  * @sw: Switch the port belongs to
239  * @remote: Remote port (%NULL if not connected)
240  * @xdomain: Remote host (%NULL if not connected)
241  * @cap_phy: Offset, zero if not found
242  * @cap_tmu: Offset of the adapter specific TMU capability (%0 if not present)
243  * @cap_adap: Offset of the adapter specific capability (%0 if not present)
244  * @cap_usb4: Offset to the USB4 port capability (%0 if not present)
245  * @usb4: Pointer to the USB4 port structure (only if @cap_usb4 is != %0)
246  * @port: Port number on switch
247  * @disabled: Disabled by eeprom or enabled but not implemented
248  * @bonded: true if the port is bonded (two lanes combined as one)
249  * @dual_link_port: If the switch is connected using two ports, points
250  *		    to the other port.
251  * @link_nr: Is this primary or secondary port on the dual_link.
252  * @in_hopids: Currently allocated input HopIDs
253  * @out_hopids: Currently allocated output HopIDs
254  * @list: Used to link ports to DP resources list
255  * @total_credits: Total number of buffers available for this port
256  * @ctl_credits: Buffers reserved for control path
257  * @dma_credits: Number of credits allocated for DMA tunneling for all
258  *		 DMA paths through this port.
259  * @group: Bandwidth allocation group the adapter is assigned to. Only
260  *	   used for DP IN adapters for now.
261  * @group_list: The adapter is linked to the group's list of ports through this
262  * @max_bw: Maximum possible bandwidth through this adapter if set to
263  *	    non-zero.
264  *
265  * In USB4 terminology this structure represents an adapter (protocol or
266  * lane adapter).
267  */
268 struct tb_port {
269 	struct tb_regs_port_header config;
270 	struct tb_switch *sw;
271 	struct tb_port *remote;
272 	struct tb_xdomain *xdomain;
273 	int cap_phy;
274 	int cap_tmu;
275 	int cap_adap;
276 	int cap_usb4;
277 	struct usb4_port *usb4;
278 	u8 port;
279 	bool disabled;
280 	bool bonded;
281 	struct tb_port *dual_link_port;
282 	u8 link_nr:1;
283 	struct ida in_hopids;
284 	struct ida out_hopids;
285 	struct list_head list;
286 	unsigned int total_credits;
287 	unsigned int ctl_credits;
288 	unsigned int dma_credits;
289 	struct tb_bandwidth_group *group;
290 	struct list_head group_list;
291 	unsigned int max_bw;
292 };
293 
294 /**
295  * struct usb4_port - USB4 port device
296  * @dev: Device for the port
297  * @port: Pointer to the lane 0 adapter
298  * @can_offline: Does the port have necessary platform support to moved
299  *		 it into offline mode and back
300  * @offline: The port is currently in offline mode
301  * @margining: Pointer to margining structure if enabled
302  */
303 struct usb4_port {
304 	struct device dev;
305 	struct tb_port *port;
306 	bool can_offline;
307 	bool offline;
308 #ifdef CONFIG_USB4_DEBUGFS_MARGINING
309 	struct tb_margining *margining;
310 #endif
311 };
312 
313 /**
314  * tb_retimer: Thunderbolt retimer
315  * @dev: Device for the retimer
316  * @tb: Pointer to the domain the retimer belongs to
317  * @index: Retimer index facing the router USB4 port
318  * @vendor: Vendor ID of the retimer
319  * @device: Device ID of the retimer
320  * @port: Pointer to the lane 0 adapter
321  * @nvm: Pointer to the NVM if the retimer has one (%NULL otherwise)
322  * @no_nvm_upgrade: Prevent NVM upgrade of this retimer
323  * @auth_status: Status of last NVM authentication
324  */
325 struct tb_retimer {
326 	struct device dev;
327 	struct tb *tb;
328 	u8 index;
329 	u32 vendor;
330 	u32 device;
331 	struct tb_port *port;
332 	struct tb_nvm *nvm;
333 	bool no_nvm_upgrade;
334 	u32 auth_status;
335 };
336 
337 /**
338  * struct tb_path_hop - routing information for a tb_path
339  * @in_port: Ingress port of a switch
340  * @out_port: Egress port of a switch where the packet is routed out
341  *	      (must be on the same switch than @in_port)
342  * @in_hop_index: HopID where the path configuration entry is placed in
343  *		  the path config space of @in_port.
344  * @in_counter_index: Used counter index (not used in the driver
345  *		      currently, %-1 to disable)
346  * @next_hop_index: HopID of the packet when it is routed out from @out_port
347  * @initial_credits: Number of initial flow control credits allocated for
348  *		     the path
349  * @nfc_credits: Number of non-flow controlled buffers allocated for the
350  *		 @in_port.
351  *
352  * Hop configuration is always done on the IN port of a switch.
353  * in_port and out_port have to be on the same switch. Packets arriving on
354  * in_port with "hop" = in_hop_index will get routed to through out_port. The
355  * next hop to take (on out_port->remote) is determined by
356  * next_hop_index. When routing packet to another switch (out->remote is
357  * set) the @next_hop_index must match the @in_hop_index of that next
358  * hop to make routing possible.
359  *
360  * in_counter_index is the index of a counter (in TB_CFG_COUNTERS) on the in
361  * port.
362  */
363 struct tb_path_hop {
364 	struct tb_port *in_port;
365 	struct tb_port *out_port;
366 	int in_hop_index;
367 	int in_counter_index;
368 	int next_hop_index;
369 	unsigned int initial_credits;
370 	unsigned int nfc_credits;
371 };
372 
373 /**
374  * enum tb_path_port - path options mask
375  * @TB_PATH_NONE: Do not activate on any hop on path
376  * @TB_PATH_SOURCE: Activate on the first hop (out of src)
377  * @TB_PATH_INTERNAL: Activate on the intermediate hops (not the first/last)
378  * @TB_PATH_DESTINATION: Activate on the last hop (into dst)
379  * @TB_PATH_ALL: Activate on all hops on the path
380  */
381 enum tb_path_port {
382 	TB_PATH_NONE = 0,
383 	TB_PATH_SOURCE = 1,
384 	TB_PATH_INTERNAL = 2,
385 	TB_PATH_DESTINATION = 4,
386 	TB_PATH_ALL = 7,
387 };
388 
389 /**
390  * struct tb_path - a unidirectional path between two ports
391  * @tb: Pointer to the domain structure
392  * @name: Name of the path (used for debugging)
393  * @ingress_shared_buffer: Shared buffering used for ingress ports on the path
394  * @egress_shared_buffer: Shared buffering used for egress ports on the path
395  * @ingress_fc_enable: Flow control for ingress ports on the path
396  * @egress_fc_enable: Flow control for egress ports on the path
397  * @priority: Priority group if the path
398  * @weight: Weight of the path inside the priority group
399  * @drop_packages: Drop packages from queue tail or head
400  * @activated: Is the path active
401  * @clear_fc: Clear all flow control from the path config space entries
402  *	      when deactivating this path
403  * @hops: Path hops
404  * @path_length: How many hops the path uses
405  * @alloc_hopid: Does this path consume port HopID
406  *
407  * A path consists of a number of hops (see &struct tb_path_hop). To
408  * establish a PCIe tunnel two paths have to be created between the two
409  * PCIe ports.
410  */
411 struct tb_path {
412 	struct tb *tb;
413 	const char *name;
414 	enum tb_path_port ingress_shared_buffer;
415 	enum tb_path_port egress_shared_buffer;
416 	enum tb_path_port ingress_fc_enable;
417 	enum tb_path_port egress_fc_enable;
418 
419 	unsigned int priority:3;
420 	int weight:4;
421 	bool drop_packages;
422 	bool activated;
423 	bool clear_fc;
424 	struct tb_path_hop *hops;
425 	int path_length;
426 	bool alloc_hopid;
427 };
428 
429 /* HopIDs 0-7 are reserved by the Thunderbolt protocol */
430 #define TB_PATH_MIN_HOPID	8
431 /*
432  * Support paths from the farthest (depth 6) router to the host and back
433  * to the same level (not necessarily to the same router).
434  */
435 #define TB_PATH_MAX_HOPS	(7 * 2)
436 
437 /* Possible wake types */
438 #define TB_WAKE_ON_CONNECT	BIT(0)
439 #define TB_WAKE_ON_DISCONNECT	BIT(1)
440 #define TB_WAKE_ON_USB4		BIT(2)
441 #define TB_WAKE_ON_USB3		BIT(3)
442 #define TB_WAKE_ON_PCIE		BIT(4)
443 #define TB_WAKE_ON_DP		BIT(5)
444 
445 /* CL states */
446 #define TB_CL0S			BIT(0)
447 #define TB_CL1			BIT(1)
448 #define TB_CL2			BIT(2)
449 
450 /**
451  * struct tb_cm_ops - Connection manager specific operations vector
452  * @driver_ready: Called right after control channel is started. Used by
453  *		  ICM to send driver ready message to the firmware.
454  * @start: Starts the domain
455  * @stop: Stops the domain
456  * @suspend_noirq: Connection manager specific suspend_noirq
457  * @resume_noirq: Connection manager specific resume_noirq
458  * @suspend: Connection manager specific suspend
459  * @freeze_noirq: Connection manager specific freeze_noirq
460  * @thaw_noirq: Connection manager specific thaw_noirq
461  * @complete: Connection manager specific complete
462  * @runtime_suspend: Connection manager specific runtime_suspend
463  * @runtime_resume: Connection manager specific runtime_resume
464  * @runtime_suspend_switch: Runtime suspend a switch
465  * @runtime_resume_switch: Runtime resume a switch
466  * @handle_event: Handle thunderbolt event
467  * @get_boot_acl: Get boot ACL list
468  * @set_boot_acl: Set boot ACL list
469  * @disapprove_switch: Disapprove switch (disconnect PCIe tunnel)
470  * @approve_switch: Approve switch
471  * @add_switch_key: Add key to switch
472  * @challenge_switch_key: Challenge switch using key
473  * @disconnect_pcie_paths: Disconnects PCIe paths before NVM update
474  * @approve_xdomain_paths: Approve (establish) XDomain DMA paths
475  * @disconnect_xdomain_paths: Disconnect XDomain DMA paths
476  * @usb4_switch_op: Optional proxy for USB4 router operations. If set
477  *		    this will be called whenever USB4 router operation is
478  *		    performed. If this returns %-EOPNOTSUPP then the
479  *		    native USB4 router operation is called.
480  * @usb4_switch_nvm_authenticate_status: Optional callback that the CM
481  *					 implementation can be used to
482  *					 return status of USB4 NVM_AUTH
483  *					 router operation.
484  */
485 struct tb_cm_ops {
486 	int (*driver_ready)(struct tb *tb);
487 	int (*start)(struct tb *tb);
488 	void (*stop)(struct tb *tb);
489 	int (*suspend_noirq)(struct tb *tb);
490 	int (*resume_noirq)(struct tb *tb);
491 	int (*suspend)(struct tb *tb);
492 	int (*freeze_noirq)(struct tb *tb);
493 	int (*thaw_noirq)(struct tb *tb);
494 	void (*complete)(struct tb *tb);
495 	int (*runtime_suspend)(struct tb *tb);
496 	int (*runtime_resume)(struct tb *tb);
497 	int (*runtime_suspend_switch)(struct tb_switch *sw);
498 	int (*runtime_resume_switch)(struct tb_switch *sw);
499 	void (*handle_event)(struct tb *tb, enum tb_cfg_pkg_type,
500 			     const void *buf, size_t size);
501 	int (*get_boot_acl)(struct tb *tb, uuid_t *uuids, size_t nuuids);
502 	int (*set_boot_acl)(struct tb *tb, const uuid_t *uuids, size_t nuuids);
503 	int (*disapprove_switch)(struct tb *tb, struct tb_switch *sw);
504 	int (*approve_switch)(struct tb *tb, struct tb_switch *sw);
505 	int (*add_switch_key)(struct tb *tb, struct tb_switch *sw);
506 	int (*challenge_switch_key)(struct tb *tb, struct tb_switch *sw,
507 				    const u8 *challenge, u8 *response);
508 	int (*disconnect_pcie_paths)(struct tb *tb);
509 	int (*approve_xdomain_paths)(struct tb *tb, struct tb_xdomain *xd,
510 				     int transmit_path, int transmit_ring,
511 				     int receive_path, int receive_ring);
512 	int (*disconnect_xdomain_paths)(struct tb *tb, struct tb_xdomain *xd,
513 					int transmit_path, int transmit_ring,
514 					int receive_path, int receive_ring);
515 	int (*usb4_switch_op)(struct tb_switch *sw, u16 opcode, u32 *metadata,
516 			      u8 *status, const void *tx_data, size_t tx_data_len,
517 			      void *rx_data, size_t rx_data_len);
518 	int (*usb4_switch_nvm_authenticate_status)(struct tb_switch *sw,
519 						   u32 *status);
520 };
521 
522 static inline void *tb_priv(struct tb *tb)
523 {
524 	return (void *)tb->privdata;
525 }
526 
527 #define TB_AUTOSUSPEND_DELAY		15000 /* ms */
528 
529 /* helper functions & macros */
530 
531 /**
532  * tb_upstream_port() - return the upstream port of a switch
533  *
534  * Every switch has an upstream port (for the root switch it is the NHI).
535  *
536  * During switch alloc/init tb_upstream_port()->remote may be NULL, even for
537  * non root switches (on the NHI port remote is always NULL).
538  *
539  * Return: Returns the upstream port of the switch.
540  */
541 static inline struct tb_port *tb_upstream_port(struct tb_switch *sw)
542 {
543 	return &sw->ports[sw->config.upstream_port_number];
544 }
545 
546 /**
547  * tb_is_upstream_port() - Is the port upstream facing
548  * @port: Port to check
549  *
550  * Returns true if @port is upstream facing port. In case of dual link
551  * ports both return true.
552  */
553 static inline bool tb_is_upstream_port(const struct tb_port *port)
554 {
555 	const struct tb_port *upstream_port = tb_upstream_port(port->sw);
556 	return port == upstream_port || port->dual_link_port == upstream_port;
557 }
558 
559 static inline u64 tb_route(const struct tb_switch *sw)
560 {
561 	return ((u64) sw->config.route_hi) << 32 | sw->config.route_lo;
562 }
563 
564 static inline struct tb_port *tb_port_at(u64 route, struct tb_switch *sw)
565 {
566 	u8 port;
567 
568 	port = route >> (sw->config.depth * 8);
569 	if (WARN_ON(port > sw->config.max_port_number))
570 		return NULL;
571 	return &sw->ports[port];
572 }
573 
574 /**
575  * tb_port_has_remote() - Does the port have switch connected downstream
576  * @port: Port to check
577  *
578  * Returns true only when the port is primary port and has remote set.
579  */
580 static inline bool tb_port_has_remote(const struct tb_port *port)
581 {
582 	if (tb_is_upstream_port(port))
583 		return false;
584 	if (!port->remote)
585 		return false;
586 	if (port->dual_link_port && port->link_nr)
587 		return false;
588 
589 	return true;
590 }
591 
592 static inline bool tb_port_is_null(const struct tb_port *port)
593 {
594 	return port && port->port && port->config.type == TB_TYPE_PORT;
595 }
596 
597 static inline bool tb_port_is_nhi(const struct tb_port *port)
598 {
599 	return port && port->config.type == TB_TYPE_NHI;
600 }
601 
602 static inline bool tb_port_is_pcie_down(const struct tb_port *port)
603 {
604 	return port && port->config.type == TB_TYPE_PCIE_DOWN;
605 }
606 
607 static inline bool tb_port_is_pcie_up(const struct tb_port *port)
608 {
609 	return port && port->config.type == TB_TYPE_PCIE_UP;
610 }
611 
612 static inline bool tb_port_is_dpin(const struct tb_port *port)
613 {
614 	return port && port->config.type == TB_TYPE_DP_HDMI_IN;
615 }
616 
617 static inline bool tb_port_is_dpout(const struct tb_port *port)
618 {
619 	return port && port->config.type == TB_TYPE_DP_HDMI_OUT;
620 }
621 
622 static inline bool tb_port_is_usb3_down(const struct tb_port *port)
623 {
624 	return port && port->config.type == TB_TYPE_USB3_DOWN;
625 }
626 
627 static inline bool tb_port_is_usb3_up(const struct tb_port *port)
628 {
629 	return port && port->config.type == TB_TYPE_USB3_UP;
630 }
631 
632 static inline int tb_sw_read(struct tb_switch *sw, void *buffer,
633 			     enum tb_cfg_space space, u32 offset, u32 length)
634 {
635 	if (sw->is_unplugged)
636 		return -ENODEV;
637 	return tb_cfg_read(sw->tb->ctl,
638 			   buffer,
639 			   tb_route(sw),
640 			   0,
641 			   space,
642 			   offset,
643 			   length);
644 }
645 
646 static inline int tb_sw_write(struct tb_switch *sw, const void *buffer,
647 			      enum tb_cfg_space space, u32 offset, u32 length)
648 {
649 	if (sw->is_unplugged)
650 		return -ENODEV;
651 	return tb_cfg_write(sw->tb->ctl,
652 			    buffer,
653 			    tb_route(sw),
654 			    0,
655 			    space,
656 			    offset,
657 			    length);
658 }
659 
660 static inline int tb_port_read(struct tb_port *port, void *buffer,
661 			       enum tb_cfg_space space, u32 offset, u32 length)
662 {
663 	if (port->sw->is_unplugged)
664 		return -ENODEV;
665 	return tb_cfg_read(port->sw->tb->ctl,
666 			   buffer,
667 			   tb_route(port->sw),
668 			   port->port,
669 			   space,
670 			   offset,
671 			   length);
672 }
673 
674 static inline int tb_port_write(struct tb_port *port, const void *buffer,
675 				enum tb_cfg_space space, u32 offset, u32 length)
676 {
677 	if (port->sw->is_unplugged)
678 		return -ENODEV;
679 	return tb_cfg_write(port->sw->tb->ctl,
680 			    buffer,
681 			    tb_route(port->sw),
682 			    port->port,
683 			    space,
684 			    offset,
685 			    length);
686 }
687 
688 #define tb_err(tb, fmt, arg...) dev_err(&(tb)->nhi->pdev->dev, fmt, ## arg)
689 #define tb_WARN(tb, fmt, arg...) dev_WARN(&(tb)->nhi->pdev->dev, fmt, ## arg)
690 #define tb_warn(tb, fmt, arg...) dev_warn(&(tb)->nhi->pdev->dev, fmt, ## arg)
691 #define tb_info(tb, fmt, arg...) dev_info(&(tb)->nhi->pdev->dev, fmt, ## arg)
692 #define tb_dbg(tb, fmt, arg...) dev_dbg(&(tb)->nhi->pdev->dev, fmt, ## arg)
693 
694 #define __TB_SW_PRINT(level, sw, fmt, arg...)           \
695 	do {                                            \
696 		const struct tb_switch *__sw = (sw);    \
697 		level(__sw->tb, "%llx: " fmt,           \
698 		      tb_route(__sw), ## arg);          \
699 	} while (0)
700 #define tb_sw_WARN(sw, fmt, arg...) __TB_SW_PRINT(tb_WARN, sw, fmt, ##arg)
701 #define tb_sw_warn(sw, fmt, arg...) __TB_SW_PRINT(tb_warn, sw, fmt, ##arg)
702 #define tb_sw_info(sw, fmt, arg...) __TB_SW_PRINT(tb_info, sw, fmt, ##arg)
703 #define tb_sw_dbg(sw, fmt, arg...) __TB_SW_PRINT(tb_dbg, sw, fmt, ##arg)
704 
705 #define __TB_PORT_PRINT(level, _port, fmt, arg...)                      \
706 	do {                                                            \
707 		const struct tb_port *__port = (_port);                 \
708 		level(__port->sw->tb, "%llx:%u: " fmt,                  \
709 		      tb_route(__port->sw), __port->port, ## arg);      \
710 	} while (0)
711 #define tb_port_WARN(port, fmt, arg...) \
712 	__TB_PORT_PRINT(tb_WARN, port, fmt, ##arg)
713 #define tb_port_warn(port, fmt, arg...) \
714 	__TB_PORT_PRINT(tb_warn, port, fmt, ##arg)
715 #define tb_port_info(port, fmt, arg...) \
716 	__TB_PORT_PRINT(tb_info, port, fmt, ##arg)
717 #define tb_port_dbg(port, fmt, arg...) \
718 	__TB_PORT_PRINT(tb_dbg, port, fmt, ##arg)
719 
720 struct tb *icm_probe(struct tb_nhi *nhi);
721 struct tb *tb_probe(struct tb_nhi *nhi);
722 
723 extern struct device_type tb_domain_type;
724 extern struct device_type tb_retimer_type;
725 extern struct device_type tb_switch_type;
726 extern struct device_type usb4_port_device_type;
727 
728 int tb_domain_init(void);
729 void tb_domain_exit(void);
730 int tb_xdomain_init(void);
731 void tb_xdomain_exit(void);
732 
733 struct tb *tb_domain_alloc(struct tb_nhi *nhi, int timeout_msec, size_t privsize);
734 int tb_domain_add(struct tb *tb);
735 void tb_domain_remove(struct tb *tb);
736 int tb_domain_suspend_noirq(struct tb *tb);
737 int tb_domain_resume_noirq(struct tb *tb);
738 int tb_domain_suspend(struct tb *tb);
739 int tb_domain_freeze_noirq(struct tb *tb);
740 int tb_domain_thaw_noirq(struct tb *tb);
741 void tb_domain_complete(struct tb *tb);
742 int tb_domain_runtime_suspend(struct tb *tb);
743 int tb_domain_runtime_resume(struct tb *tb);
744 int tb_domain_disapprove_switch(struct tb *tb, struct tb_switch *sw);
745 int tb_domain_approve_switch(struct tb *tb, struct tb_switch *sw);
746 int tb_domain_approve_switch_key(struct tb *tb, struct tb_switch *sw);
747 int tb_domain_challenge_switch_key(struct tb *tb, struct tb_switch *sw);
748 int tb_domain_disconnect_pcie_paths(struct tb *tb);
749 int tb_domain_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
750 				    int transmit_path, int transmit_ring,
751 				    int receive_path, int receive_ring);
752 int tb_domain_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
753 				       int transmit_path, int transmit_ring,
754 				       int receive_path, int receive_ring);
755 int tb_domain_disconnect_all_paths(struct tb *tb);
756 
757 static inline struct tb *tb_domain_get(struct tb *tb)
758 {
759 	if (tb)
760 		get_device(&tb->dev);
761 	return tb;
762 }
763 
764 static inline void tb_domain_put(struct tb *tb)
765 {
766 	put_device(&tb->dev);
767 }
768 
769 struct tb_nvm *tb_nvm_alloc(struct device *dev);
770 int tb_nvm_read_version(struct tb_nvm *nvm);
771 int tb_nvm_validate(struct tb_nvm *nvm);
772 int tb_nvm_write_headers(struct tb_nvm *nvm);
773 int tb_nvm_add_active(struct tb_nvm *nvm, nvmem_reg_read_t reg_read);
774 int tb_nvm_write_buf(struct tb_nvm *nvm, unsigned int offset, void *val,
775 		     size_t bytes);
776 int tb_nvm_add_non_active(struct tb_nvm *nvm, nvmem_reg_write_t reg_write);
777 void tb_nvm_free(struct tb_nvm *nvm);
778 void tb_nvm_exit(void);
779 
780 typedef int (*read_block_fn)(void *, unsigned int, void *, size_t);
781 typedef int (*write_block_fn)(void *, unsigned int, const void *, size_t);
782 
783 int tb_nvm_read_data(unsigned int address, void *buf, size_t size,
784 		     unsigned int retries, read_block_fn read_block,
785 		     void *read_block_data);
786 int tb_nvm_write_data(unsigned int address, const void *buf, size_t size,
787 		      unsigned int retries, write_block_fn write_next_block,
788 		      void *write_block_data);
789 
790 int tb_switch_nvm_read(struct tb_switch *sw, unsigned int address, void *buf,
791 		       size_t size);
792 struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent,
793 				  u64 route);
794 struct tb_switch *tb_switch_alloc_safe_mode(struct tb *tb,
795 			struct device *parent, u64 route);
796 int tb_switch_configure(struct tb_switch *sw);
797 int tb_switch_configuration_valid(struct tb_switch *sw);
798 int tb_switch_add(struct tb_switch *sw);
799 void tb_switch_remove(struct tb_switch *sw);
800 void tb_switch_suspend(struct tb_switch *sw, bool runtime);
801 int tb_switch_resume(struct tb_switch *sw);
802 int tb_switch_reset(struct tb_switch *sw);
803 int tb_switch_wait_for_bit(struct tb_switch *sw, u32 offset, u32 bit,
804 			   u32 value, int timeout_msec);
805 void tb_sw_set_unplugged(struct tb_switch *sw);
806 struct tb_port *tb_switch_find_port(struct tb_switch *sw,
807 				    enum tb_port_type type);
808 struct tb_switch *tb_switch_find_by_link_depth(struct tb *tb, u8 link,
809 					       u8 depth);
810 struct tb_switch *tb_switch_find_by_uuid(struct tb *tb, const uuid_t *uuid);
811 struct tb_switch *tb_switch_find_by_route(struct tb *tb, u64 route);
812 
813 /**
814  * tb_switch_for_each_port() - Iterate over each switch port
815  * @sw: Switch whose ports to iterate
816  * @p: Port used as iterator
817  *
818  * Iterates over each switch port skipping the control port (port %0).
819  */
820 #define tb_switch_for_each_port(sw, p)					\
821 	for ((p) = &(sw)->ports[1];					\
822 	     (p) <= &(sw)->ports[(sw)->config.max_port_number]; (p)++)
823 
824 static inline struct tb_switch *tb_switch_get(struct tb_switch *sw)
825 {
826 	if (sw)
827 		get_device(&sw->dev);
828 	return sw;
829 }
830 
831 static inline void tb_switch_put(struct tb_switch *sw)
832 {
833 	put_device(&sw->dev);
834 }
835 
836 static inline bool tb_is_switch(const struct device *dev)
837 {
838 	return dev->type == &tb_switch_type;
839 }
840 
841 static inline struct tb_switch *tb_to_switch(const struct device *dev)
842 {
843 	if (tb_is_switch(dev))
844 		return container_of(dev, struct tb_switch, dev);
845 	return NULL;
846 }
847 
848 static inline struct tb_switch *tb_switch_parent(struct tb_switch *sw)
849 {
850 	return tb_to_switch(sw->dev.parent);
851 }
852 
853 /**
854  * tb_switch_downstream_port() - Return downstream facing port of parent router
855  * @sw: Device router pointer
856  *
857  * Only call for device routers. Returns the downstream facing port of
858  * the parent router.
859  */
860 static inline struct tb_port *tb_switch_downstream_port(struct tb_switch *sw)
861 {
862 	if (WARN_ON(!tb_route(sw)))
863 		return NULL;
864 	return tb_port_at(tb_route(sw), tb_switch_parent(sw));
865 }
866 
867 static inline bool tb_switch_is_light_ridge(const struct tb_switch *sw)
868 {
869 	return sw->config.vendor_id == PCI_VENDOR_ID_INTEL &&
870 	       sw->config.device_id == PCI_DEVICE_ID_INTEL_LIGHT_RIDGE;
871 }
872 
873 static inline bool tb_switch_is_eagle_ridge(const struct tb_switch *sw)
874 {
875 	return sw->config.vendor_id == PCI_VENDOR_ID_INTEL &&
876 	       sw->config.device_id == PCI_DEVICE_ID_INTEL_EAGLE_RIDGE;
877 }
878 
879 static inline bool tb_switch_is_cactus_ridge(const struct tb_switch *sw)
880 {
881 	if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
882 		switch (sw->config.device_id) {
883 		case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C:
884 		case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
885 			return true;
886 		}
887 	}
888 	return false;
889 }
890 
891 static inline bool tb_switch_is_falcon_ridge(const struct tb_switch *sw)
892 {
893 	if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
894 		switch (sw->config.device_id) {
895 		case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE:
896 		case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE:
897 			return true;
898 		}
899 	}
900 	return false;
901 }
902 
903 static inline bool tb_switch_is_alpine_ridge(const struct tb_switch *sw)
904 {
905 	if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
906 		switch (sw->config.device_id) {
907 		case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
908 		case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE:
909 		case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
910 		case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
911 		case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
912 			return true;
913 		}
914 	}
915 	return false;
916 }
917 
918 static inline bool tb_switch_is_titan_ridge(const struct tb_switch *sw)
919 {
920 	if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
921 		switch (sw->config.device_id) {
922 		case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE:
923 		case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE:
924 		case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_DD_BRIDGE:
925 			return true;
926 		}
927 	}
928 	return false;
929 }
930 
931 static inline bool tb_switch_is_tiger_lake(const struct tb_switch *sw)
932 {
933 	if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
934 		switch (sw->config.device_id) {
935 		case PCI_DEVICE_ID_INTEL_TGL_NHI0:
936 		case PCI_DEVICE_ID_INTEL_TGL_NHI1:
937 		case PCI_DEVICE_ID_INTEL_TGL_H_NHI0:
938 		case PCI_DEVICE_ID_INTEL_TGL_H_NHI1:
939 			return true;
940 		}
941 	}
942 	return false;
943 }
944 
945 /**
946  * tb_switch_is_icm() - Is the switch handled by ICM firmware
947  * @sw: Switch to check
948  *
949  * In case there is a need to differentiate whether ICM firmware or SW CM
950  * is handling @sw this function can be called. It is valid to call this
951  * after tb_switch_alloc() and tb_switch_configure() has been called
952  * (latter only for SW CM case).
953  */
954 static inline bool tb_switch_is_icm(const struct tb_switch *sw)
955 {
956 	return !sw->config.enabled;
957 }
958 
959 int tb_switch_lane_bonding_enable(struct tb_switch *sw);
960 void tb_switch_lane_bonding_disable(struct tb_switch *sw);
961 int tb_switch_configure_link(struct tb_switch *sw);
962 void tb_switch_unconfigure_link(struct tb_switch *sw);
963 
964 bool tb_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in);
965 int tb_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in);
966 void tb_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in);
967 
968 int tb_switch_tmu_init(struct tb_switch *sw);
969 int tb_switch_tmu_post_time(struct tb_switch *sw);
970 int tb_switch_tmu_disable(struct tb_switch *sw);
971 int tb_switch_tmu_enable(struct tb_switch *sw);
972 int tb_switch_tmu_configure(struct tb_switch *sw, enum tb_switch_tmu_mode mode);
973 
974 /**
975  * tb_switch_tmu_is_configured() - Is given TMU mode configured
976  * @sw: Router whose mode to check
977  * @mode: Mode to check
978  *
979  * Checks if given router TMU mode is configured to @mode. Note the
980  * router TMU might not be enabled to this mode.
981  */
982 static inline bool tb_switch_tmu_is_configured(const struct tb_switch *sw,
983 					       enum tb_switch_tmu_mode mode)
984 {
985 	return sw->tmu.mode_request == mode;
986 }
987 
988 /**
989  * tb_switch_tmu_is_enabled() - Checks if the specified TMU mode is enabled
990  * @sw: Router whose TMU mode to check
991  *
992  * Return true if hardware TMU configuration matches the requested
993  * configuration (and is not %TB_SWITCH_TMU_MODE_OFF).
994  */
995 static inline bool tb_switch_tmu_is_enabled(const struct tb_switch *sw)
996 {
997 	return sw->tmu.mode != TB_SWITCH_TMU_MODE_OFF &&
998 	       sw->tmu.mode == sw->tmu.mode_request;
999 }
1000 
1001 bool tb_port_clx_is_enabled(struct tb_port *port, unsigned int clx);
1002 
1003 int tb_switch_clx_init(struct tb_switch *sw);
1004 bool tb_switch_clx_is_supported(const struct tb_switch *sw);
1005 int tb_switch_clx_enable(struct tb_switch *sw, unsigned int clx);
1006 int tb_switch_clx_disable(struct tb_switch *sw);
1007 
1008 /**
1009  * tb_switch_clx_is_enabled() - Checks if the CLx is enabled
1010  * @sw: Router to check for the CLx
1011  * @clx: The CLx states to check for
1012  *
1013  * Checks if the specified CLx is enabled on the router upstream link.
1014  * Returns true if any of the given states is enabled.
1015  *
1016  * Not applicable for a host router.
1017  */
1018 static inline bool tb_switch_clx_is_enabled(const struct tb_switch *sw,
1019 					    unsigned int clx)
1020 {
1021 	return sw->clx & clx;
1022 }
1023 
1024 int tb_switch_pcie_l1_enable(struct tb_switch *sw);
1025 
1026 int tb_switch_xhci_connect(struct tb_switch *sw);
1027 void tb_switch_xhci_disconnect(struct tb_switch *sw);
1028 
1029 int tb_port_state(struct tb_port *port);
1030 int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged);
1031 int tb_port_add_nfc_credits(struct tb_port *port, int credits);
1032 int tb_port_clear_counter(struct tb_port *port, int counter);
1033 int tb_port_unlock(struct tb_port *port);
1034 int tb_port_enable(struct tb_port *port);
1035 int tb_port_disable(struct tb_port *port);
1036 int tb_port_alloc_in_hopid(struct tb_port *port, int hopid, int max_hopid);
1037 void tb_port_release_in_hopid(struct tb_port *port, int hopid);
1038 int tb_port_alloc_out_hopid(struct tb_port *port, int hopid, int max_hopid);
1039 void tb_port_release_out_hopid(struct tb_port *port, int hopid);
1040 struct tb_port *tb_next_port_on_path(struct tb_port *start, struct tb_port *end,
1041 				     struct tb_port *prev);
1042 
1043 static inline bool tb_port_use_credit_allocation(const struct tb_port *port)
1044 {
1045 	return tb_port_is_null(port) && port->sw->credit_allocation;
1046 }
1047 
1048 /**
1049  * tb_for_each_port_on_path() - Iterate over each port on path
1050  * @src: Source port
1051  * @dst: Destination port
1052  * @p: Port used as iterator
1053  *
1054  * Walks over each port on path from @src to @dst.
1055  */
1056 #define tb_for_each_port_on_path(src, dst, p)				\
1057 	for ((p) = tb_next_port_on_path((src), (dst), NULL); (p);	\
1058 	     (p) = tb_next_port_on_path((src), (dst), (p)))
1059 
1060 int tb_port_get_link_speed(struct tb_port *port);
1061 int tb_port_get_link_width(struct tb_port *port);
1062 int tb_port_set_link_width(struct tb_port *port, enum tb_link_width width);
1063 int tb_port_lane_bonding_enable(struct tb_port *port);
1064 void tb_port_lane_bonding_disable(struct tb_port *port);
1065 int tb_port_wait_for_link_width(struct tb_port *port, unsigned int width_mask,
1066 				int timeout_msec);
1067 int tb_port_update_credits(struct tb_port *port);
1068 
1069 int tb_switch_find_vse_cap(struct tb_switch *sw, enum tb_switch_vse_cap vsec);
1070 int tb_switch_find_cap(struct tb_switch *sw, enum tb_switch_cap cap);
1071 int tb_switch_next_cap(struct tb_switch *sw, unsigned int offset);
1072 int tb_port_find_cap(struct tb_port *port, enum tb_port_cap cap);
1073 int tb_port_next_cap(struct tb_port *port, unsigned int offset);
1074 bool tb_port_is_enabled(struct tb_port *port);
1075 
1076 bool tb_usb3_port_is_enabled(struct tb_port *port);
1077 int tb_usb3_port_enable(struct tb_port *port, bool enable);
1078 
1079 bool tb_pci_port_is_enabled(struct tb_port *port);
1080 int tb_pci_port_enable(struct tb_port *port, bool enable);
1081 
1082 int tb_dp_port_hpd_is_active(struct tb_port *port);
1083 int tb_dp_port_hpd_clear(struct tb_port *port);
1084 int tb_dp_port_set_hops(struct tb_port *port, unsigned int video,
1085 			unsigned int aux_tx, unsigned int aux_rx);
1086 bool tb_dp_port_is_enabled(struct tb_port *port);
1087 int tb_dp_port_enable(struct tb_port *port, bool enable);
1088 
1089 struct tb_path *tb_path_discover(struct tb_port *src, int src_hopid,
1090 				 struct tb_port *dst, int dst_hopid,
1091 				 struct tb_port **last, const char *name,
1092 				 bool alloc_hopid);
1093 struct tb_path *tb_path_alloc(struct tb *tb, struct tb_port *src, int src_hopid,
1094 			      struct tb_port *dst, int dst_hopid, int link_nr,
1095 			      const char *name);
1096 void tb_path_free(struct tb_path *path);
1097 int tb_path_activate(struct tb_path *path);
1098 void tb_path_deactivate(struct tb_path *path);
1099 bool tb_path_is_invalid(struct tb_path *path);
1100 bool tb_path_port_on_path(const struct tb_path *path,
1101 			  const struct tb_port *port);
1102 
1103 /**
1104  * tb_path_for_each_hop() - Iterate over each hop on path
1105  * @path: Path whose hops to iterate
1106  * @hop: Hop used as iterator
1107  *
1108  * Iterates over each hop on path.
1109  */
1110 #define tb_path_for_each_hop(path, hop)					\
1111 	for ((hop) = &(path)->hops[0];					\
1112 	     (hop) <= &(path)->hops[(path)->path_length - 1]; (hop)++)
1113 
1114 int tb_drom_read(struct tb_switch *sw);
1115 int tb_drom_read_uid_only(struct tb_switch *sw, u64 *uid);
1116 
1117 int tb_lc_read_uuid(struct tb_switch *sw, u32 *uuid);
1118 int tb_lc_configure_port(struct tb_port *port);
1119 void tb_lc_unconfigure_port(struct tb_port *port);
1120 int tb_lc_configure_xdomain(struct tb_port *port);
1121 void tb_lc_unconfigure_xdomain(struct tb_port *port);
1122 int tb_lc_start_lane_initialization(struct tb_port *port);
1123 bool tb_lc_is_clx_supported(struct tb_port *port);
1124 bool tb_lc_is_usb_plugged(struct tb_port *port);
1125 bool tb_lc_is_xhci_connected(struct tb_port *port);
1126 int tb_lc_xhci_connect(struct tb_port *port);
1127 void tb_lc_xhci_disconnect(struct tb_port *port);
1128 int tb_lc_set_wake(struct tb_switch *sw, unsigned int flags);
1129 int tb_lc_set_sleep(struct tb_switch *sw);
1130 bool tb_lc_lane_bonding_possible(struct tb_switch *sw);
1131 bool tb_lc_dp_sink_query(struct tb_switch *sw, struct tb_port *in);
1132 int tb_lc_dp_sink_alloc(struct tb_switch *sw, struct tb_port *in);
1133 int tb_lc_dp_sink_dealloc(struct tb_switch *sw, struct tb_port *in);
1134 int tb_lc_force_power(struct tb_switch *sw);
1135 
1136 static inline int tb_route_length(u64 route)
1137 {
1138 	return (fls64(route) + TB_ROUTE_SHIFT - 1) / TB_ROUTE_SHIFT;
1139 }
1140 
1141 /**
1142  * tb_downstream_route() - get route to downstream switch
1143  *
1144  * Port must not be the upstream port (otherwise a loop is created).
1145  *
1146  * Return: Returns a route to the switch behind @port.
1147  */
1148 static inline u64 tb_downstream_route(struct tb_port *port)
1149 {
1150 	return tb_route(port->sw)
1151 	       | ((u64) port->port << (port->sw->config.depth * 8));
1152 }
1153 
1154 bool tb_is_xdomain_enabled(void);
1155 bool tb_xdomain_handle_request(struct tb *tb, enum tb_cfg_pkg_type type,
1156 			       const void *buf, size_t size);
1157 struct tb_xdomain *tb_xdomain_alloc(struct tb *tb, struct device *parent,
1158 				    u64 route, const uuid_t *local_uuid,
1159 				    const uuid_t *remote_uuid);
1160 void tb_xdomain_add(struct tb_xdomain *xd);
1161 void tb_xdomain_remove(struct tb_xdomain *xd);
1162 struct tb_xdomain *tb_xdomain_find_by_link_depth(struct tb *tb, u8 link,
1163 						 u8 depth);
1164 
1165 static inline struct tb_switch *tb_xdomain_parent(struct tb_xdomain *xd)
1166 {
1167 	return tb_to_switch(xd->dev.parent);
1168 }
1169 
1170 /**
1171  * tb_xdomain_downstream_port() - Return downstream facing port of parent router
1172  * @xd: Xdomain pointer
1173  *
1174  * Returns the downstream port the XDomain is connected to.
1175  */
1176 static inline struct tb_port *tb_xdomain_downstream_port(struct tb_xdomain *xd)
1177 {
1178 	return tb_port_at(xd->route, tb_xdomain_parent(xd));
1179 }
1180 
1181 int tb_retimer_nvm_read(struct tb_retimer *rt, unsigned int address, void *buf,
1182 			size_t size);
1183 int tb_retimer_scan(struct tb_port *port, bool add);
1184 void tb_retimer_remove_all(struct tb_port *port);
1185 
1186 static inline bool tb_is_retimer(const struct device *dev)
1187 {
1188 	return dev->type == &tb_retimer_type;
1189 }
1190 
1191 static inline struct tb_retimer *tb_to_retimer(struct device *dev)
1192 {
1193 	if (tb_is_retimer(dev))
1194 		return container_of(dev, struct tb_retimer, dev);
1195 	return NULL;
1196 }
1197 
1198 /**
1199  * usb4_switch_version() - Returns USB4 version of the router
1200  * @sw: Router to check
1201  *
1202  * Returns major version of USB4 router (%1 for v1, %2 for v2 and so
1203  * on). Can be called to pre-USB4 router too and in that case returns %0.
1204  */
1205 static inline unsigned int usb4_switch_version(const struct tb_switch *sw)
1206 {
1207 	return FIELD_GET(USB4_VERSION_MAJOR_MASK, sw->config.thunderbolt_version);
1208 }
1209 
1210 /**
1211  * tb_switch_is_usb4() - Is the switch USB4 compliant
1212  * @sw: Switch to check
1213  *
1214  * Returns true if the @sw is USB4 compliant router, false otherwise.
1215  */
1216 static inline bool tb_switch_is_usb4(const struct tb_switch *sw)
1217 {
1218 	return usb4_switch_version(sw) > 0;
1219 }
1220 
1221 int usb4_switch_setup(struct tb_switch *sw);
1222 int usb4_switch_configuration_valid(struct tb_switch *sw);
1223 int usb4_switch_read_uid(struct tb_switch *sw, u64 *uid);
1224 int usb4_switch_drom_read(struct tb_switch *sw, unsigned int address, void *buf,
1225 			  size_t size);
1226 bool usb4_switch_lane_bonding_possible(struct tb_switch *sw);
1227 int usb4_switch_set_wake(struct tb_switch *sw, unsigned int flags);
1228 int usb4_switch_set_sleep(struct tb_switch *sw);
1229 int usb4_switch_nvm_sector_size(struct tb_switch *sw);
1230 int usb4_switch_nvm_read(struct tb_switch *sw, unsigned int address, void *buf,
1231 			 size_t size);
1232 int usb4_switch_nvm_set_offset(struct tb_switch *sw, unsigned int address);
1233 int usb4_switch_nvm_write(struct tb_switch *sw, unsigned int address,
1234 			  const void *buf, size_t size);
1235 int usb4_switch_nvm_authenticate(struct tb_switch *sw);
1236 int usb4_switch_nvm_authenticate_status(struct tb_switch *sw, u32 *status);
1237 int usb4_switch_credits_init(struct tb_switch *sw);
1238 bool usb4_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in);
1239 int usb4_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in);
1240 int usb4_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in);
1241 struct tb_port *usb4_switch_map_pcie_down(struct tb_switch *sw,
1242 					  const struct tb_port *port);
1243 struct tb_port *usb4_switch_map_usb3_down(struct tb_switch *sw,
1244 					  const struct tb_port *port);
1245 int usb4_switch_add_ports(struct tb_switch *sw);
1246 void usb4_switch_remove_ports(struct tb_switch *sw);
1247 
1248 int usb4_port_unlock(struct tb_port *port);
1249 int usb4_port_hotplug_enable(struct tb_port *port);
1250 int usb4_port_configure(struct tb_port *port);
1251 void usb4_port_unconfigure(struct tb_port *port);
1252 int usb4_port_configure_xdomain(struct tb_port *port, struct tb_xdomain *xd);
1253 void usb4_port_unconfigure_xdomain(struct tb_port *port);
1254 int usb4_port_router_offline(struct tb_port *port);
1255 int usb4_port_router_online(struct tb_port *port);
1256 int usb4_port_enumerate_retimers(struct tb_port *port);
1257 bool usb4_port_clx_supported(struct tb_port *port);
1258 int usb4_port_margining_caps(struct tb_port *port, u32 *caps);
1259 int usb4_port_hw_margin(struct tb_port *port, unsigned int lanes,
1260 			unsigned int ber_level, bool timing, bool right_high,
1261 			u32 *results);
1262 int usb4_port_sw_margin(struct tb_port *port, unsigned int lanes, bool timing,
1263 			bool right_high, u32 counter);
1264 int usb4_port_sw_margin_errors(struct tb_port *port, u32 *errors);
1265 
1266 int usb4_port_retimer_set_inbound_sbtx(struct tb_port *port, u8 index);
1267 int usb4_port_retimer_unset_inbound_sbtx(struct tb_port *port, u8 index);
1268 int usb4_port_retimer_read(struct tb_port *port, u8 index, u8 reg, void *buf,
1269 			   u8 size);
1270 int usb4_port_retimer_write(struct tb_port *port, u8 index, u8 reg,
1271 			    const void *buf, u8 size);
1272 int usb4_port_retimer_is_last(struct tb_port *port, u8 index);
1273 int usb4_port_retimer_nvm_sector_size(struct tb_port *port, u8 index);
1274 int usb4_port_retimer_nvm_set_offset(struct tb_port *port, u8 index,
1275 				     unsigned int address);
1276 int usb4_port_retimer_nvm_write(struct tb_port *port, u8 index,
1277 				unsigned int address, const void *buf,
1278 				size_t size);
1279 int usb4_port_retimer_nvm_authenticate(struct tb_port *port, u8 index);
1280 int usb4_port_retimer_nvm_authenticate_status(struct tb_port *port, u8 index,
1281 					      u32 *status);
1282 int usb4_port_retimer_nvm_read(struct tb_port *port, u8 index,
1283 			       unsigned int address, void *buf, size_t size);
1284 
1285 int usb4_usb3_port_max_link_rate(struct tb_port *port);
1286 int usb4_usb3_port_actual_link_rate(struct tb_port *port);
1287 int usb4_usb3_port_allocated_bandwidth(struct tb_port *port, int *upstream_bw,
1288 				       int *downstream_bw);
1289 int usb4_usb3_port_allocate_bandwidth(struct tb_port *port, int *upstream_bw,
1290 				      int *downstream_bw);
1291 int usb4_usb3_port_release_bandwidth(struct tb_port *port, int *upstream_bw,
1292 				     int *downstream_bw);
1293 
1294 int usb4_dp_port_set_cm_id(struct tb_port *port, int cm_id);
1295 bool usb4_dp_port_bandwidth_mode_supported(struct tb_port *port);
1296 bool usb4_dp_port_bandwidth_mode_enabled(struct tb_port *port);
1297 int usb4_dp_port_set_cm_bandwidth_mode_supported(struct tb_port *port,
1298 						 bool supported);
1299 int usb4_dp_port_group_id(struct tb_port *port);
1300 int usb4_dp_port_set_group_id(struct tb_port *port, int group_id);
1301 int usb4_dp_port_nrd(struct tb_port *port, int *rate, int *lanes);
1302 int usb4_dp_port_set_nrd(struct tb_port *port, int rate, int lanes);
1303 int usb4_dp_port_granularity(struct tb_port *port);
1304 int usb4_dp_port_set_granularity(struct tb_port *port, int granularity);
1305 int usb4_dp_port_set_estimated_bandwidth(struct tb_port *port, int bw);
1306 int usb4_dp_port_allocated_bandwidth(struct tb_port *port);
1307 int usb4_dp_port_allocate_bandwidth(struct tb_port *port, int bw);
1308 int usb4_dp_port_requested_bandwidth(struct tb_port *port);
1309 
1310 int usb4_pci_port_set_ext_encapsulation(struct tb_port *port, bool enable);
1311 
1312 static inline bool tb_is_usb4_port_device(const struct device *dev)
1313 {
1314 	return dev->type == &usb4_port_device_type;
1315 }
1316 
1317 static inline struct usb4_port *tb_to_usb4_port_device(struct device *dev)
1318 {
1319 	if (tb_is_usb4_port_device(dev))
1320 		return container_of(dev, struct usb4_port, dev);
1321 	return NULL;
1322 }
1323 
1324 struct usb4_port *usb4_port_device_add(struct tb_port *port);
1325 void usb4_port_device_remove(struct usb4_port *usb4);
1326 int usb4_port_device_resume(struct usb4_port *usb4);
1327 
1328 static inline bool usb4_port_device_is_offline(const struct usb4_port *usb4)
1329 {
1330 	return usb4->offline;
1331 }
1332 
1333 void tb_check_quirks(struct tb_switch *sw);
1334 
1335 #ifdef CONFIG_ACPI
1336 bool tb_acpi_add_links(struct tb_nhi *nhi);
1337 
1338 bool tb_acpi_is_native(void);
1339 bool tb_acpi_may_tunnel_usb3(void);
1340 bool tb_acpi_may_tunnel_dp(void);
1341 bool tb_acpi_may_tunnel_pcie(void);
1342 bool tb_acpi_is_xdomain_allowed(void);
1343 
1344 int tb_acpi_init(void);
1345 void tb_acpi_exit(void);
1346 int tb_acpi_power_on_retimers(struct tb_port *port);
1347 int tb_acpi_power_off_retimers(struct tb_port *port);
1348 #else
1349 static inline bool tb_acpi_add_links(struct tb_nhi *nhi) { return false; }
1350 
1351 static inline bool tb_acpi_is_native(void) { return true; }
1352 static inline bool tb_acpi_may_tunnel_usb3(void) { return true; }
1353 static inline bool tb_acpi_may_tunnel_dp(void) { return true; }
1354 static inline bool tb_acpi_may_tunnel_pcie(void) { return true; }
1355 static inline bool tb_acpi_is_xdomain_allowed(void) { return true; }
1356 
1357 static inline int tb_acpi_init(void) { return 0; }
1358 static inline void tb_acpi_exit(void) { }
1359 static inline int tb_acpi_power_on_retimers(struct tb_port *port) { return 0; }
1360 static inline int tb_acpi_power_off_retimers(struct tb_port *port) { return 0; }
1361 #endif
1362 
1363 #ifdef CONFIG_DEBUG_FS
1364 void tb_debugfs_init(void);
1365 void tb_debugfs_exit(void);
1366 void tb_switch_debugfs_init(struct tb_switch *sw);
1367 void tb_switch_debugfs_remove(struct tb_switch *sw);
1368 void tb_xdomain_debugfs_init(struct tb_xdomain *xd);
1369 void tb_xdomain_debugfs_remove(struct tb_xdomain *xd);
1370 void tb_service_debugfs_init(struct tb_service *svc);
1371 void tb_service_debugfs_remove(struct tb_service *svc);
1372 #else
1373 static inline void tb_debugfs_init(void) { }
1374 static inline void tb_debugfs_exit(void) { }
1375 static inline void tb_switch_debugfs_init(struct tb_switch *sw) { }
1376 static inline void tb_switch_debugfs_remove(struct tb_switch *sw) { }
1377 static inline void tb_xdomain_debugfs_init(struct tb_xdomain *xd) { }
1378 static inline void tb_xdomain_debugfs_remove(struct tb_xdomain *xd) { }
1379 static inline void tb_service_debugfs_init(struct tb_service *svc) { }
1380 static inline void tb_service_debugfs_remove(struct tb_service *svc) { }
1381 #endif
1382 
1383 #endif
1384