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