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