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