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