1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Thunderbolt driver - bus logic (NHI independent)
5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
6 * Copyright (C) 2018, Intel Corporation
12 #include <linux/nvmem-provider.h>
13 #include <linux/pci.h>
14 #include <linux/thunderbolt.h>
15 #include <linux/uuid.h>
22 * struct tb_switch_nvm - Structure holding switch NVM information
23 * @major: Major version number of the active NVM portion
24 * @minor: Minor version number of the active NVM portion
25 * @id: Identifier used with both NVM portions
26 * @active: Active portion NVMem device
27 * @non_active: Non-active portion NVMem device
28 * @buf: Buffer where the NVM image is stored before it is written to
29 * the actual NVM flash device
30 * @buf_data_size: Number of bytes actually consumed by the new NVM
32 * @authenticating: The switch is authenticating the new NVM
34 struct tb_switch_nvm
{
38 struct nvmem_device
*active
;
39 struct nvmem_device
*non_active
;
45 #define TB_SWITCH_KEY_SIZE 32
46 #define TB_SWITCH_MAX_DEPTH 6
47 #define USB4_SWITCH_MAX_DEPTH 5
50 * enum tb_switch_tmu_rate - TMU refresh rate
51 * @TB_SWITCH_TMU_RATE_OFF: %0 (Disable Time Sync handshake)
52 * @TB_SWITCH_TMU_RATE_HIFI: %16 us time interval between successive
53 * transmission of the Delay Request TSNOS
54 * (Time Sync Notification Ordered Set) on a Link
55 * @TB_SWITCH_TMU_RATE_NORMAL: %1 ms time interval between successive
56 * transmission of the Delay Request TSNOS on
59 enum tb_switch_tmu_rate
{
60 TB_SWITCH_TMU_RATE_OFF
= 0,
61 TB_SWITCH_TMU_RATE_HIFI
= 16,
62 TB_SWITCH_TMU_RATE_NORMAL
= 1000,
66 * struct tb_switch_tmu - Structure holding switch TMU configuration
67 * @cap: Offset to the TMU capability (%0 if not found)
68 * @has_ucap: Does the switch support uni-directional mode
69 * @rate: TMU refresh rate related to upstream switch. In case of root
70 * switch this holds the domain rate.
71 * @unidirectional: Is the TMU in uni-directional or bi-directional mode
72 * related to upstream switch. Don't case for root switch.
74 struct tb_switch_tmu
{
77 enum tb_switch_tmu_rate rate
;
82 * struct tb_switch - a thunderbolt switch
83 * @dev: Device for the switch
84 * @config: Switch configuration
85 * @ports: Ports in this switch
86 * @dma_port: If the switch has port supporting DMA configuration based
87 * mailbox this will hold the pointer to that (%NULL
88 * otherwise). If set it also means the switch has
90 * @tmu: The switch TMU configuration
91 * @tb: Pointer to the domain the switch belongs to
92 * @uid: Unique ID of the switch
93 * @uuid: UUID of the switch (or %NULL if not supported)
94 * @vendor: Vendor ID of the switch
95 * @device: Device ID of the switch
96 * @vendor_name: Name of the vendor (or %NULL if not known)
97 * @device_name: Name of the device (or %NULL if not known)
98 * @link_speed: Speed of the link in Gb/s
99 * @link_width: Width of the link (1 or 2)
100 * @generation: Switch Thunderbolt generation
101 * @cap_plug_events: Offset to the plug events capability (%0 if not found)
102 * @cap_lc: Offset to the link controller capability (%0 if not found)
103 * @is_unplugged: The switch is going away
104 * @drom: DROM of the switch (%NULL if not found)
105 * @nvm: Pointer to the NVM if the switch has one (%NULL otherwise)
106 * @no_nvm_upgrade: Prevent NVM upgrade of this switch
107 * @safe_mode: The switch is in safe-mode
108 * @boot: Whether the switch was already authorized on boot or not
109 * @rpm: The switch supports runtime PM
110 * @authorized: Whether the switch is authorized by user or policy
111 * @security_level: Switch supported security level
112 * @key: Contains the key used to challenge the device or %NULL if not
113 * supported. Size of the key is %TB_SWITCH_KEY_SIZE.
114 * @connection_id: Connection ID used with ICM messaging
115 * @connection_key: Connection key used with ICM messaging
116 * @link: Root switch link this switch is connected (ICM only)
117 * @depth: Depth in the chain this switch is connected (ICM only)
118 * @rpm_complete: Completion used to wait for runtime resume to
119 * complete (ICM only)
121 * When the switch is being added or removed to the domain (other
122 * switches) you need to have domain lock held.
126 struct tb_regs_switch_header config
;
127 struct tb_port
*ports
;
128 struct tb_dma_port
*dma_port
;
129 struct tb_switch_tmu tmu
;
135 const char *vendor_name
;
136 const char *device_name
;
137 unsigned int link_speed
;
138 unsigned int link_width
;
139 unsigned int generation
;
144 struct tb_switch_nvm
*nvm
;
149 unsigned int authorized
;
150 enum tb_security_level security_level
;
156 struct completion rpm_complete
;
160 * struct tb_port - a thunderbolt port, part of a tb_switch
161 * @config: Cached port configuration read from registers
162 * @sw: Switch the port belongs to
163 * @remote: Remote port (%NULL if not connected)
164 * @xdomain: Remote host (%NULL if not connected)
165 * @cap_phy: Offset, zero if not found
166 * @cap_tmu: Offset of the adapter specific TMU capability (%0 if not present)
167 * @cap_adap: Offset of the adapter specific capability (%0 if not present)
168 * @cap_usb4: Offset to the USB4 port capability (%0 if not present)
169 * @port: Port number on switch
170 * @disabled: Disabled by eeprom
171 * @bonded: true if the port is bonded (two lanes combined as one)
172 * @dual_link_port: If the switch is connected using two ports, points
174 * @link_nr: Is this primary or secondary port on the dual_link.
175 * @in_hopids: Currently allocated input HopIDs
176 * @out_hopids: Currently allocated output HopIDs
177 * @list: Used to link ports to DP resources list
180 struct tb_regs_port_header config
;
181 struct tb_switch
*sw
;
182 struct tb_port
*remote
;
183 struct tb_xdomain
*xdomain
;
191 struct tb_port
*dual_link_port
;
193 struct ida in_hopids
;
194 struct ida out_hopids
;
195 struct list_head list
;
199 * struct tb_path_hop - routing information for a tb_path
200 * @in_port: Ingress port of a switch
201 * @out_port: Egress port of a switch where the packet is routed out
202 * (must be on the same switch than @in_port)
203 * @in_hop_index: HopID where the path configuration entry is placed in
204 * the path config space of @in_port.
205 * @in_counter_index: Used counter index (not used in the driver
206 * currently, %-1 to disable)
207 * @next_hop_index: HopID of the packet when it is routed out from @out_port
208 * @initial_credits: Number of initial flow control credits allocated for
211 * Hop configuration is always done on the IN port of a switch.
212 * in_port and out_port have to be on the same switch. Packets arriving on
213 * in_port with "hop" = in_hop_index will get routed to through out_port. The
214 * next hop to take (on out_port->remote) is determined by
215 * next_hop_index. When routing packet to another switch (out->remote is
216 * set) the @next_hop_index must match the @in_hop_index of that next
217 * hop to make routing possible.
219 * in_counter_index is the index of a counter (in TB_CFG_COUNTERS) on the in
223 struct tb_port
*in_port
;
224 struct tb_port
*out_port
;
226 int in_counter_index
;
228 unsigned int initial_credits
;
232 * enum tb_path_port - path options mask
233 * @TB_PATH_NONE: Do not activate on any hop on path
234 * @TB_PATH_SOURCE: Activate on the first hop (out of src)
235 * @TB_PATH_INTERNAL: Activate on the intermediate hops (not the first/last)
236 * @TB_PATH_DESTINATION: Activate on the last hop (into dst)
237 * @TB_PATH_ALL: Activate on all hops on the path
242 TB_PATH_INTERNAL
= 2,
243 TB_PATH_DESTINATION
= 4,
248 * struct tb_path - a unidirectional path between two ports
249 * @tb: Pointer to the domain structure
250 * @name: Name of the path (used for debugging)
251 * @nfc_credits: Number of non flow controlled credits allocated for the path
252 * @ingress_shared_buffer: Shared buffering used for ingress ports on the path
253 * @egress_shared_buffer: Shared buffering used for egress ports on the path
254 * @ingress_fc_enable: Flow control for ingress ports on the path
255 * @egress_fc_enable: Flow control for egress ports on the path
256 * @priority: Priority group if the path
257 * @weight: Weight of the path inside the priority group
258 * @drop_packages: Drop packages from queue tail or head
259 * @activated: Is the path active
260 * @clear_fc: Clear all flow control from the path config space entries
261 * when deactivating this path
263 * @path_length: How many hops the path uses
265 * A path consists of a number of hops (see &struct tb_path_hop). To
266 * establish a PCIe tunnel two paths have to be created between the two
273 enum tb_path_port ingress_shared_buffer
;
274 enum tb_path_port egress_shared_buffer
;
275 enum tb_path_port ingress_fc_enable
;
276 enum tb_path_port egress_fc_enable
;
278 unsigned int priority
:3;
283 struct tb_path_hop
*hops
;
287 /* HopIDs 0-7 are reserved by the Thunderbolt protocol */
288 #define TB_PATH_MIN_HOPID 8
289 #define TB_PATH_MAX_HOPS 7
292 * struct tb_cm_ops - Connection manager specific operations vector
293 * @driver_ready: Called right after control channel is started. Used by
294 * ICM to send driver ready message to the firmware.
295 * @start: Starts the domain
296 * @stop: Stops the domain
297 * @suspend_noirq: Connection manager specific suspend_noirq
298 * @resume_noirq: Connection manager specific resume_noirq
299 * @suspend: Connection manager specific suspend
300 * @complete: Connection manager specific complete
301 * @runtime_suspend: Connection manager specific runtime_suspend
302 * @runtime_resume: Connection manager specific runtime_resume
303 * @runtime_suspend_switch: Runtime suspend a switch
304 * @runtime_resume_switch: Runtime resume a switch
305 * @handle_event: Handle thunderbolt event
306 * @get_boot_acl: Get boot ACL list
307 * @set_boot_acl: Set boot ACL list
308 * @approve_switch: Approve switch
309 * @add_switch_key: Add key to switch
310 * @challenge_switch_key: Challenge switch using key
311 * @disconnect_pcie_paths: Disconnects PCIe paths before NVM update
312 * @approve_xdomain_paths: Approve (establish) XDomain DMA paths
313 * @disconnect_xdomain_paths: Disconnect XDomain DMA paths
316 int (*driver_ready
)(struct tb
*tb
);
317 int (*start
)(struct tb
*tb
);
318 void (*stop
)(struct tb
*tb
);
319 int (*suspend_noirq
)(struct tb
*tb
);
320 int (*resume_noirq
)(struct tb
*tb
);
321 int (*suspend
)(struct tb
*tb
);
322 void (*complete
)(struct tb
*tb
);
323 int (*runtime_suspend
)(struct tb
*tb
);
324 int (*runtime_resume
)(struct tb
*tb
);
325 int (*runtime_suspend_switch
)(struct tb_switch
*sw
);
326 int (*runtime_resume_switch
)(struct tb_switch
*sw
);
327 void (*handle_event
)(struct tb
*tb
, enum tb_cfg_pkg_type
,
328 const void *buf
, size_t size
);
329 int (*get_boot_acl
)(struct tb
*tb
, uuid_t
*uuids
, size_t nuuids
);
330 int (*set_boot_acl
)(struct tb
*tb
, const uuid_t
*uuids
, size_t nuuids
);
331 int (*approve_switch
)(struct tb
*tb
, struct tb_switch
*sw
);
332 int (*add_switch_key
)(struct tb
*tb
, struct tb_switch
*sw
);
333 int (*challenge_switch_key
)(struct tb
*tb
, struct tb_switch
*sw
,
334 const u8
*challenge
, u8
*response
);
335 int (*disconnect_pcie_paths
)(struct tb
*tb
);
336 int (*approve_xdomain_paths
)(struct tb
*tb
, struct tb_xdomain
*xd
);
337 int (*disconnect_xdomain_paths
)(struct tb
*tb
, struct tb_xdomain
*xd
);
340 static inline void *tb_priv(struct tb
*tb
)
342 return (void *)tb
->privdata
;
345 #define TB_AUTOSUSPEND_DELAY 15000 /* ms */
347 /* helper functions & macros */
350 * tb_upstream_port() - return the upstream port of a switch
352 * Every switch has an upstream port (for the root switch it is the NHI).
354 * During switch alloc/init tb_upstream_port()->remote may be NULL, even for
355 * non root switches (on the NHI port remote is always NULL).
357 * Return: Returns the upstream port of the switch.
359 static inline struct tb_port
*tb_upstream_port(struct tb_switch
*sw
)
361 return &sw
->ports
[sw
->config
.upstream_port_number
];
365 * tb_is_upstream_port() - Is the port upstream facing
366 * @port: Port to check
368 * Returns true if @port is upstream facing port. In case of dual link
369 * ports both return true.
371 static inline bool tb_is_upstream_port(const struct tb_port
*port
)
373 const struct tb_port
*upstream_port
= tb_upstream_port(port
->sw
);
374 return port
== upstream_port
|| port
->dual_link_port
== upstream_port
;
377 static inline u64
tb_route(const struct tb_switch
*sw
)
379 return ((u64
) sw
->config
.route_hi
) << 32 | sw
->config
.route_lo
;
382 static inline struct tb_port
*tb_port_at(u64 route
, struct tb_switch
*sw
)
386 port
= route
>> (sw
->config
.depth
* 8);
387 if (WARN_ON(port
> sw
->config
.max_port_number
))
389 return &sw
->ports
[port
];
393 * tb_port_has_remote() - Does the port have switch connected downstream
394 * @port: Port to check
396 * Returns true only when the port is primary port and has remote set.
398 static inline bool tb_port_has_remote(const struct tb_port
*port
)
400 if (tb_is_upstream_port(port
))
404 if (port
->dual_link_port
&& port
->link_nr
)
410 static inline bool tb_port_is_null(const struct tb_port
*port
)
412 return port
&& port
->port
&& port
->config
.type
== TB_TYPE_PORT
;
415 static inline bool tb_port_is_pcie_down(const struct tb_port
*port
)
417 return port
&& port
->config
.type
== TB_TYPE_PCIE_DOWN
;
420 static inline bool tb_port_is_pcie_up(const struct tb_port
*port
)
422 return port
&& port
->config
.type
== TB_TYPE_PCIE_UP
;
425 static inline bool tb_port_is_dpin(const struct tb_port
*port
)
427 return port
&& port
->config
.type
== TB_TYPE_DP_HDMI_IN
;
430 static inline bool tb_port_is_dpout(const struct tb_port
*port
)
432 return port
&& port
->config
.type
== TB_TYPE_DP_HDMI_OUT
;
435 static inline bool tb_port_is_usb3_down(const struct tb_port
*port
)
437 return port
&& port
->config
.type
== TB_TYPE_USB3_DOWN
;
440 static inline bool tb_port_is_usb3_up(const struct tb_port
*port
)
442 return port
&& port
->config
.type
== TB_TYPE_USB3_UP
;
445 static inline int tb_sw_read(struct tb_switch
*sw
, void *buffer
,
446 enum tb_cfg_space space
, u32 offset
, u32 length
)
448 if (sw
->is_unplugged
)
450 return tb_cfg_read(sw
->tb
->ctl
,
459 static inline int tb_sw_write(struct tb_switch
*sw
, const void *buffer
,
460 enum tb_cfg_space space
, u32 offset
, u32 length
)
462 if (sw
->is_unplugged
)
464 return tb_cfg_write(sw
->tb
->ctl
,
473 static inline int tb_port_read(struct tb_port
*port
, void *buffer
,
474 enum tb_cfg_space space
, u32 offset
, u32 length
)
476 if (port
->sw
->is_unplugged
)
478 return tb_cfg_read(port
->sw
->tb
->ctl
,
487 static inline int tb_port_write(struct tb_port
*port
, const void *buffer
,
488 enum tb_cfg_space space
, u32 offset
, u32 length
)
490 if (port
->sw
->is_unplugged
)
492 return tb_cfg_write(port
->sw
->tb
->ctl
,
501 #define tb_err(tb, fmt, arg...) dev_err(&(tb)->nhi->pdev->dev, fmt, ## arg)
502 #define tb_WARN(tb, fmt, arg...) dev_WARN(&(tb)->nhi->pdev->dev, fmt, ## arg)
503 #define tb_warn(tb, fmt, arg...) dev_warn(&(tb)->nhi->pdev->dev, fmt, ## arg)
504 #define tb_info(tb, fmt, arg...) dev_info(&(tb)->nhi->pdev->dev, fmt, ## arg)
505 #define tb_dbg(tb, fmt, arg...) dev_dbg(&(tb)->nhi->pdev->dev, fmt, ## arg)
507 #define __TB_SW_PRINT(level, sw, fmt, arg...) \
509 const struct tb_switch *__sw = (sw); \
510 level(__sw->tb, "%llx: " fmt, \
511 tb_route(__sw), ## arg); \
513 #define tb_sw_WARN(sw, fmt, arg...) __TB_SW_PRINT(tb_WARN, sw, fmt, ##arg)
514 #define tb_sw_warn(sw, fmt, arg...) __TB_SW_PRINT(tb_warn, sw, fmt, ##arg)
515 #define tb_sw_info(sw, fmt, arg...) __TB_SW_PRINT(tb_info, sw, fmt, ##arg)
516 #define tb_sw_dbg(sw, fmt, arg...) __TB_SW_PRINT(tb_dbg, sw, fmt, ##arg)
518 #define __TB_PORT_PRINT(level, _port, fmt, arg...) \
520 const struct tb_port *__port = (_port); \
521 level(__port->sw->tb, "%llx:%x: " fmt, \
522 tb_route(__port->sw), __port->port, ## arg); \
524 #define tb_port_WARN(port, fmt, arg...) \
525 __TB_PORT_PRINT(tb_WARN, port, fmt, ##arg)
526 #define tb_port_warn(port, fmt, arg...) \
527 __TB_PORT_PRINT(tb_warn, port, fmt, ##arg)
528 #define tb_port_info(port, fmt, arg...) \
529 __TB_PORT_PRINT(tb_info, port, fmt, ##arg)
530 #define tb_port_dbg(port, fmt, arg...) \
531 __TB_PORT_PRINT(tb_dbg, port, fmt, ##arg)
533 struct tb
*icm_probe(struct tb_nhi
*nhi
);
534 struct tb
*tb_probe(struct tb_nhi
*nhi
);
536 extern struct device_type tb_domain_type
;
537 extern struct device_type tb_switch_type
;
539 int tb_domain_init(void);
540 void tb_domain_exit(void);
541 void tb_switch_exit(void);
542 int tb_xdomain_init(void);
543 void tb_xdomain_exit(void);
545 struct tb
*tb_domain_alloc(struct tb_nhi
*nhi
, size_t privsize
);
546 int tb_domain_add(struct tb
*tb
);
547 void tb_domain_remove(struct tb
*tb
);
548 int tb_domain_suspend_noirq(struct tb
*tb
);
549 int tb_domain_resume_noirq(struct tb
*tb
);
550 int tb_domain_suspend(struct tb
*tb
);
551 void tb_domain_complete(struct tb
*tb
);
552 int tb_domain_runtime_suspend(struct tb
*tb
);
553 int tb_domain_runtime_resume(struct tb
*tb
);
554 int tb_domain_approve_switch(struct tb
*tb
, struct tb_switch
*sw
);
555 int tb_domain_approve_switch_key(struct tb
*tb
, struct tb_switch
*sw
);
556 int tb_domain_challenge_switch_key(struct tb
*tb
, struct tb_switch
*sw
);
557 int tb_domain_disconnect_pcie_paths(struct tb
*tb
);
558 int tb_domain_approve_xdomain_paths(struct tb
*tb
, struct tb_xdomain
*xd
);
559 int tb_domain_disconnect_xdomain_paths(struct tb
*tb
, struct tb_xdomain
*xd
);
560 int tb_domain_disconnect_all_paths(struct tb
*tb
);
562 static inline struct tb
*tb_domain_get(struct tb
*tb
)
565 get_device(&tb
->dev
);
569 static inline void tb_domain_put(struct tb
*tb
)
571 put_device(&tb
->dev
);
574 struct tb_switch
*tb_switch_alloc(struct tb
*tb
, struct device
*parent
,
576 struct tb_switch
*tb_switch_alloc_safe_mode(struct tb
*tb
,
577 struct device
*parent
, u64 route
);
578 int tb_switch_configure(struct tb_switch
*sw
);
579 int tb_switch_add(struct tb_switch
*sw
);
580 void tb_switch_remove(struct tb_switch
*sw
);
581 void tb_switch_suspend(struct tb_switch
*sw
);
582 int tb_switch_resume(struct tb_switch
*sw
);
583 int tb_switch_reset(struct tb
*tb
, u64 route
);
584 void tb_sw_set_unplugged(struct tb_switch
*sw
);
585 struct tb_port
*tb_switch_find_port(struct tb_switch
*sw
,
586 enum tb_port_type type
);
587 struct tb_switch
*tb_switch_find_by_link_depth(struct tb
*tb
, u8 link
,
589 struct tb_switch
*tb_switch_find_by_uuid(struct tb
*tb
, const uuid_t
*uuid
);
590 struct tb_switch
*tb_switch_find_by_route(struct tb
*tb
, u64 route
);
593 * tb_switch_for_each_port() - Iterate over each switch port
594 * @sw: Switch whose ports to iterate
595 * @p: Port used as iterator
597 * Iterates over each switch port skipping the control port (port %0).
599 #define tb_switch_for_each_port(sw, p) \
600 for ((p) = &(sw)->ports[1]; \
601 (p) <= &(sw)->ports[(sw)->config.max_port_number]; (p)++)
603 static inline struct tb_switch
*tb_switch_get(struct tb_switch
*sw
)
606 get_device(&sw
->dev
);
610 static inline void tb_switch_put(struct tb_switch
*sw
)
612 put_device(&sw
->dev
);
615 static inline bool tb_is_switch(const struct device
*dev
)
617 return dev
->type
== &tb_switch_type
;
620 static inline struct tb_switch
*tb_to_switch(struct device
*dev
)
622 if (tb_is_switch(dev
))
623 return container_of(dev
, struct tb_switch
, dev
);
627 static inline struct tb_switch
*tb_switch_parent(struct tb_switch
*sw
)
629 return tb_to_switch(sw
->dev
.parent
);
632 static inline bool tb_switch_is_light_ridge(const struct tb_switch
*sw
)
634 return sw
->config
.device_id
== PCI_DEVICE_ID_INTEL_LIGHT_RIDGE
;
637 static inline bool tb_switch_is_eagle_ridge(const struct tb_switch
*sw
)
639 return sw
->config
.device_id
== PCI_DEVICE_ID_INTEL_EAGLE_RIDGE
;
642 static inline bool tb_switch_is_cactus_ridge(const struct tb_switch
*sw
)
644 switch (sw
->config
.device_id
) {
645 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C
:
646 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C
:
653 static inline bool tb_switch_is_falcon_ridge(const struct tb_switch
*sw
)
655 switch (sw
->config
.device_id
) {
656 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE
:
657 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE
:
664 static inline bool tb_switch_is_alpine_ridge(const struct tb_switch
*sw
)
666 switch (sw
->config
.device_id
) {
667 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE
:
668 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE
:
669 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE
:
670 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE
:
677 static inline bool tb_switch_is_titan_ridge(const struct tb_switch
*sw
)
679 switch (sw
->config
.device_id
) {
680 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE
:
681 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE
:
682 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_DD_BRIDGE
:
690 * tb_switch_is_usb4() - Is the switch USB4 compliant
691 * @sw: Switch to check
693 * Returns true if the @sw is USB4 compliant router, false otherwise.
695 static inline bool tb_switch_is_usb4(const struct tb_switch
*sw
)
697 return sw
->config
.thunderbolt_version
== USB4_VERSION_1_0
;
701 * tb_switch_is_icm() - Is the switch handled by ICM firmware
702 * @sw: Switch to check
704 * In case there is a need to differentiate whether ICM firmware or SW CM
705 * is handling @sw this function can be called. It is valid to call this
706 * after tb_switch_alloc() and tb_switch_configure() has been called
707 * (latter only for SW CM case).
709 static inline bool tb_switch_is_icm(const struct tb_switch
*sw
)
711 return !sw
->config
.enabled
;
714 int tb_switch_lane_bonding_enable(struct tb_switch
*sw
);
715 void tb_switch_lane_bonding_disable(struct tb_switch
*sw
);
717 bool tb_switch_query_dp_resource(struct tb_switch
*sw
, struct tb_port
*in
);
718 int tb_switch_alloc_dp_resource(struct tb_switch
*sw
, struct tb_port
*in
);
719 void tb_switch_dealloc_dp_resource(struct tb_switch
*sw
, struct tb_port
*in
);
721 int tb_switch_tmu_init(struct tb_switch
*sw
);
722 int tb_switch_tmu_post_time(struct tb_switch
*sw
);
723 int tb_switch_tmu_disable(struct tb_switch
*sw
);
724 int tb_switch_tmu_enable(struct tb_switch
*sw
);
726 static inline bool tb_switch_tmu_is_enabled(const struct tb_switch
*sw
)
728 return sw
->tmu
.rate
== TB_SWITCH_TMU_RATE_HIFI
&&
729 !sw
->tmu
.unidirectional
;
732 int tb_wait_for_port(struct tb_port
*port
, bool wait_if_unplugged
);
733 int tb_port_add_nfc_credits(struct tb_port
*port
, int credits
);
734 int tb_port_set_initial_credits(struct tb_port
*port
, u32 credits
);
735 int tb_port_clear_counter(struct tb_port
*port
, int counter
);
736 int tb_port_unlock(struct tb_port
*port
);
737 int tb_port_alloc_in_hopid(struct tb_port
*port
, int hopid
, int max_hopid
);
738 void tb_port_release_in_hopid(struct tb_port
*port
, int hopid
);
739 int tb_port_alloc_out_hopid(struct tb_port
*port
, int hopid
, int max_hopid
);
740 void tb_port_release_out_hopid(struct tb_port
*port
, int hopid
);
741 struct tb_port
*tb_next_port_on_path(struct tb_port
*start
, struct tb_port
*end
,
742 struct tb_port
*prev
);
744 int tb_switch_find_vse_cap(struct tb_switch
*sw
, enum tb_switch_vse_cap vsec
);
745 int tb_switch_find_cap(struct tb_switch
*sw
, enum tb_switch_cap cap
);
746 int tb_port_find_cap(struct tb_port
*port
, enum tb_port_cap cap
);
747 bool tb_port_is_enabled(struct tb_port
*port
);
749 bool tb_usb3_port_is_enabled(struct tb_port
*port
);
750 int tb_usb3_port_enable(struct tb_port
*port
, bool enable
);
752 bool tb_pci_port_is_enabled(struct tb_port
*port
);
753 int tb_pci_port_enable(struct tb_port
*port
, bool enable
);
755 int tb_dp_port_hpd_is_active(struct tb_port
*port
);
756 int tb_dp_port_hpd_clear(struct tb_port
*port
);
757 int tb_dp_port_set_hops(struct tb_port
*port
, unsigned int video
,
758 unsigned int aux_tx
, unsigned int aux_rx
);
759 bool tb_dp_port_is_enabled(struct tb_port
*port
);
760 int tb_dp_port_enable(struct tb_port
*port
, bool enable
);
762 struct tb_path
*tb_path_discover(struct tb_port
*src
, int src_hopid
,
763 struct tb_port
*dst
, int dst_hopid
,
764 struct tb_port
**last
, const char *name
);
765 struct tb_path
*tb_path_alloc(struct tb
*tb
, struct tb_port
*src
, int src_hopid
,
766 struct tb_port
*dst
, int dst_hopid
, int link_nr
,
768 void tb_path_free(struct tb_path
*path
);
769 int tb_path_activate(struct tb_path
*path
);
770 void tb_path_deactivate(struct tb_path
*path
);
771 bool tb_path_is_invalid(struct tb_path
*path
);
772 bool tb_path_switch_on_path(const struct tb_path
*path
,
773 const struct tb_switch
*sw
);
775 int tb_drom_read(struct tb_switch
*sw
);
776 int tb_drom_read_uid_only(struct tb_switch
*sw
, u64
*uid
);
778 int tb_lc_read_uuid(struct tb_switch
*sw
, u32
*uuid
);
779 int tb_lc_configure_link(struct tb_switch
*sw
);
780 void tb_lc_unconfigure_link(struct tb_switch
*sw
);
781 int tb_lc_set_sleep(struct tb_switch
*sw
);
782 bool tb_lc_lane_bonding_possible(struct tb_switch
*sw
);
783 bool tb_lc_dp_sink_query(struct tb_switch
*sw
, struct tb_port
*in
);
784 int tb_lc_dp_sink_alloc(struct tb_switch
*sw
, struct tb_port
*in
);
785 int tb_lc_dp_sink_dealloc(struct tb_switch
*sw
, struct tb_port
*in
);
787 static inline int tb_route_length(u64 route
)
789 return (fls64(route
) + TB_ROUTE_SHIFT
- 1) / TB_ROUTE_SHIFT
;
793 * tb_downstream_route() - get route to downstream switch
795 * Port must not be the upstream port (otherwise a loop is created).
797 * Return: Returns a route to the switch behind @port.
799 static inline u64
tb_downstream_route(struct tb_port
*port
)
801 return tb_route(port
->sw
)
802 | ((u64
) port
->port
<< (port
->sw
->config
.depth
* 8));
805 bool tb_xdomain_handle_request(struct tb
*tb
, enum tb_cfg_pkg_type type
,
806 const void *buf
, size_t size
);
807 struct tb_xdomain
*tb_xdomain_alloc(struct tb
*tb
, struct device
*parent
,
808 u64 route
, const uuid_t
*local_uuid
,
809 const uuid_t
*remote_uuid
);
810 void tb_xdomain_add(struct tb_xdomain
*xd
);
811 void tb_xdomain_remove(struct tb_xdomain
*xd
);
812 struct tb_xdomain
*tb_xdomain_find_by_link_depth(struct tb
*tb
, u8 link
,
815 int usb4_switch_setup(struct tb_switch
*sw
);
816 int usb4_switch_read_uid(struct tb_switch
*sw
, u64
*uid
);
817 int usb4_switch_drom_read(struct tb_switch
*sw
, unsigned int address
, void *buf
,
819 int usb4_switch_configure_link(struct tb_switch
*sw
);
820 void usb4_switch_unconfigure_link(struct tb_switch
*sw
);
821 bool usb4_switch_lane_bonding_possible(struct tb_switch
*sw
);
822 int usb4_switch_set_sleep(struct tb_switch
*sw
);
823 int usb4_switch_nvm_sector_size(struct tb_switch
*sw
);
824 int usb4_switch_nvm_read(struct tb_switch
*sw
, unsigned int address
, void *buf
,
826 int usb4_switch_nvm_write(struct tb_switch
*sw
, unsigned int address
,
827 const void *buf
, size_t size
);
828 int usb4_switch_nvm_authenticate(struct tb_switch
*sw
);
829 bool usb4_switch_query_dp_resource(struct tb_switch
*sw
, struct tb_port
*in
);
830 int usb4_switch_alloc_dp_resource(struct tb_switch
*sw
, struct tb_port
*in
);
831 int usb4_switch_dealloc_dp_resource(struct tb_switch
*sw
, struct tb_port
*in
);
832 struct tb_port
*usb4_switch_map_pcie_down(struct tb_switch
*sw
,
833 const struct tb_port
*port
);
834 struct tb_port
*usb4_switch_map_usb3_down(struct tb_switch
*sw
,
835 const struct tb_port
*port
);
837 int usb4_port_unlock(struct tb_port
*port
);