Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Thunderbolt driver - bus logic (NHI independent)
0004  *
0005  * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
0006  * Copyright (C) 2018, Intel Corporation
0007  */
0008 
0009 #ifndef TB_H_
0010 #define TB_H_
0011 
0012 #include <linux/nvmem-provider.h>
0013 #include <linux/pci.h>
0014 #include <linux/thunderbolt.h>
0015 #include <linux/uuid.h>
0016 #include <linux/bitfield.h>
0017 
0018 #include "tb_regs.h"
0019 #include "ctl.h"
0020 #include "dma_port.h"
0021 
0022 #define NVM_MIN_SIZE        SZ_32K
0023 #define NVM_MAX_SIZE        SZ_512K
0024 #define NVM_DATA_DWORDS     16
0025 
0026 /* Intel specific NVM offsets */
0027 #define NVM_DEVID       0x05
0028 #define NVM_VERSION     0x08
0029 #define NVM_FLASH_SIZE      0x45
0030 
0031 /**
0032  * struct tb_nvm - Structure holding NVM information
0033  * @dev: Owner of the NVM
0034  * @major: Major version number of the active NVM portion
0035  * @minor: Minor version number of the active NVM portion
0036  * @id: Identifier used with both NVM portions
0037  * @active: Active portion NVMem device
0038  * @non_active: Non-active portion NVMem device
0039  * @buf: Buffer where the NVM image is stored before it is written to
0040  *   the actual NVM flash device
0041  * @buf_data_size: Number of bytes actually consumed by the new NVM
0042  *         image
0043  * @authenticating: The device is authenticating the new NVM
0044  * @flushed: The image has been flushed to the storage area
0045  *
0046  * The user of this structure needs to handle serialization of possible
0047  * concurrent access.
0048  */
0049 struct tb_nvm {
0050     struct device *dev;
0051     u8 major;
0052     u8 minor;
0053     int id;
0054     struct nvmem_device *active;
0055     struct nvmem_device *non_active;
0056     void *buf;
0057     size_t buf_data_size;
0058     bool authenticating;
0059     bool flushed;
0060 };
0061 
0062 enum tb_nvm_write_ops {
0063     WRITE_AND_AUTHENTICATE = 1,
0064     WRITE_ONLY = 2,
0065     AUTHENTICATE_ONLY = 3,
0066 };
0067 
0068 #define TB_SWITCH_KEY_SIZE      32
0069 #define TB_SWITCH_MAX_DEPTH     6
0070 #define USB4_SWITCH_MAX_DEPTH       5
0071 
0072 /**
0073  * enum tb_switch_tmu_rate - TMU refresh rate
0074  * @TB_SWITCH_TMU_RATE_OFF: %0 (Disable Time Sync handshake)
0075  * @TB_SWITCH_TMU_RATE_HIFI: %16 us time interval between successive
0076  *               transmission of the Delay Request TSNOS
0077  *               (Time Sync Notification Ordered Set) on a Link
0078  * @TB_SWITCH_TMU_RATE_NORMAL: %1 ms time interval between successive
0079  *                 transmission of the Delay Request TSNOS on
0080  *                 a Link
0081  */
0082 enum tb_switch_tmu_rate {
0083     TB_SWITCH_TMU_RATE_OFF = 0,
0084     TB_SWITCH_TMU_RATE_HIFI = 16,
0085     TB_SWITCH_TMU_RATE_NORMAL = 1000,
0086 };
0087 
0088 /**
0089  * struct tb_switch_tmu - Structure holding switch TMU configuration
0090  * @cap: Offset to the TMU capability (%0 if not found)
0091  * @has_ucap: Does the switch support uni-directional mode
0092  * @rate: TMU refresh rate related to upstream switch. In case of root
0093  *    switch this holds the domain rate. Reflects the HW setting.
0094  * @unidirectional: Is the TMU in uni-directional or bi-directional mode
0095  *          related to upstream switch. Don't care for root switch.
0096  *          Reflects the HW setting.
0097  * @unidirectional_request: Is the new TMU mode: uni-directional or bi-directional
0098  *              that is requested to be set. Related to upstream switch.
0099  *              Don't care for root switch.
0100  * @rate_request: TMU new refresh rate related to upstream switch that is
0101  *        requested to be set. In case of root switch, this holds
0102  *        the new domain rate that is requested to be set.
0103  */
0104 struct tb_switch_tmu {
0105     int cap;
0106     bool has_ucap;
0107     enum tb_switch_tmu_rate rate;
0108     bool unidirectional;
0109     bool unidirectional_request;
0110     enum tb_switch_tmu_rate rate_request;
0111 };
0112 
0113 enum tb_clx {
0114     TB_CLX_DISABLE,
0115     /* CL0s and CL1 are enabled and supported together */
0116     TB_CL1,
0117     TB_CL2,
0118 };
0119 
0120 /**
0121  * struct tb_switch - a thunderbolt switch
0122  * @dev: Device for the switch
0123  * @config: Switch configuration
0124  * @ports: Ports in this switch
0125  * @dma_port: If the switch has port supporting DMA configuration based
0126  *        mailbox this will hold the pointer to that (%NULL
0127  *        otherwise). If set it also means the switch has
0128  *        upgradeable NVM.
0129  * @tmu: The switch TMU configuration
0130  * @tb: Pointer to the domain the switch belongs to
0131  * @uid: Unique ID of the switch
0132  * @uuid: UUID of the switch (or %NULL if not supported)
0133  * @vendor: Vendor ID of the switch
0134  * @device: Device ID of the switch
0135  * @vendor_name: Name of the vendor (or %NULL if not known)
0136  * @device_name: Name of the device (or %NULL if not known)
0137  * @link_speed: Speed of the link in Gb/s
0138  * @link_width: Width of the link (1 or 2)
0139  * @link_usb4: Upstream link is USB4
0140  * @generation: Switch Thunderbolt generation
0141  * @cap_plug_events: Offset to the plug events capability (%0 if not found)
0142  * @cap_vsec_tmu: Offset to the TMU vendor specific capability (%0 if not found)
0143  * @cap_lc: Offset to the link controller capability (%0 if not found)
0144  * @cap_lp: Offset to the low power (CLx for TBT) capability (%0 if not found)
0145  * @is_unplugged: The switch is going away
0146  * @drom: DROM of the switch (%NULL if not found)
0147  * @nvm: Pointer to the NVM if the switch has one (%NULL otherwise)
0148  * @no_nvm_upgrade: Prevent NVM upgrade of this switch
0149  * @safe_mode: The switch is in safe-mode
0150  * @boot: Whether the switch was already authorized on boot or not
0151  * @rpm: The switch supports runtime PM
0152  * @authorized: Whether the switch is authorized by user or policy
0153  * @security_level: Switch supported security level
0154  * @debugfs_dir: Pointer to the debugfs structure
0155  * @key: Contains the key used to challenge the device or %NULL if not
0156  *   supported. Size of the key is %TB_SWITCH_KEY_SIZE.
0157  * @connection_id: Connection ID used with ICM messaging
0158  * @connection_key: Connection key used with ICM messaging
0159  * @link: Root switch link this switch is connected (ICM only)
0160  * @depth: Depth in the chain this switch is connected (ICM only)
0161  * @rpm_complete: Completion used to wait for runtime resume to
0162  *        complete (ICM only)
0163  * @quirks: Quirks used for this Thunderbolt switch
0164  * @credit_allocation: Are the below buffer allocation parameters valid
0165  * @max_usb3_credits: Router preferred number of buffers for USB 3.x
0166  * @min_dp_aux_credits: Router preferred minimum number of buffers for DP AUX
0167  * @min_dp_main_credits: Router preferred minimum number of buffers for DP MAIN
0168  * @max_pcie_credits: Router preferred number of buffers for PCIe
0169  * @max_dma_credits: Router preferred number of buffers for DMA/P2P
0170  * @clx: CLx state on the upstream link of the router
0171  *
0172  * When the switch is being added or removed to the domain (other
0173  * switches) you need to have domain lock held.
0174  *
0175  * In USB4 terminology this structure represents a router.
0176  */
0177 struct tb_switch {
0178     struct device dev;
0179     struct tb_regs_switch_header config;
0180     struct tb_port *ports;
0181     struct tb_dma_port *dma_port;
0182     struct tb_switch_tmu tmu;
0183     struct tb *tb;
0184     u64 uid;
0185     uuid_t *uuid;
0186     u16 vendor;
0187     u16 device;
0188     const char *vendor_name;
0189     const char *device_name;
0190     unsigned int link_speed;
0191     unsigned int link_width;
0192     bool link_usb4;
0193     unsigned int generation;
0194     int cap_plug_events;
0195     int cap_vsec_tmu;
0196     int cap_lc;
0197     int cap_lp;
0198     bool is_unplugged;
0199     u8 *drom;
0200     struct tb_nvm *nvm;
0201     bool no_nvm_upgrade;
0202     bool safe_mode;
0203     bool boot;
0204     bool rpm;
0205     unsigned int authorized;
0206     enum tb_security_level security_level;
0207     struct dentry *debugfs_dir;
0208     u8 *key;
0209     u8 connection_id;
0210     u8 connection_key;
0211     u8 link;
0212     u8 depth;
0213     struct completion rpm_complete;
0214     unsigned long quirks;
0215     bool credit_allocation;
0216     unsigned int max_usb3_credits;
0217     unsigned int min_dp_aux_credits;
0218     unsigned int min_dp_main_credits;
0219     unsigned int max_pcie_credits;
0220     unsigned int max_dma_credits;
0221     enum tb_clx clx;
0222 };
0223 
0224 /**
0225  * struct tb_port - a thunderbolt port, part of a tb_switch
0226  * @config: Cached port configuration read from registers
0227  * @sw: Switch the port belongs to
0228  * @remote: Remote port (%NULL if not connected)
0229  * @xdomain: Remote host (%NULL if not connected)
0230  * @cap_phy: Offset, zero if not found
0231  * @cap_tmu: Offset of the adapter specific TMU capability (%0 if not present)
0232  * @cap_adap: Offset of the adapter specific capability (%0 if not present)
0233  * @cap_usb4: Offset to the USB4 port capability (%0 if not present)
0234  * @usb4: Pointer to the USB4 port structure (only if @cap_usb4 is != %0)
0235  * @port: Port number on switch
0236  * @disabled: Disabled by eeprom or enabled but not implemented
0237  * @bonded: true if the port is bonded (two lanes combined as one)
0238  * @dual_link_port: If the switch is connected using two ports, points
0239  *          to the other port.
0240  * @link_nr: Is this primary or secondary port on the dual_link.
0241  * @in_hopids: Currently allocated input HopIDs
0242  * @out_hopids: Currently allocated output HopIDs
0243  * @list: Used to link ports to DP resources list
0244  * @total_credits: Total number of buffers available for this port
0245  * @ctl_credits: Buffers reserved for control path
0246  * @dma_credits: Number of credits allocated for DMA tunneling for all
0247  *       DMA paths through this port.
0248  *
0249  * In USB4 terminology this structure represents an adapter (protocol or
0250  * lane adapter).
0251  */
0252 struct tb_port {
0253     struct tb_regs_port_header config;
0254     struct tb_switch *sw;
0255     struct tb_port *remote;
0256     struct tb_xdomain *xdomain;
0257     int cap_phy;
0258     int cap_tmu;
0259     int cap_adap;
0260     int cap_usb4;
0261     struct usb4_port *usb4;
0262     u8 port;
0263     bool disabled;
0264     bool bonded;
0265     struct tb_port *dual_link_port;
0266     u8 link_nr:1;
0267     struct ida in_hopids;
0268     struct ida out_hopids;
0269     struct list_head list;
0270     unsigned int total_credits;
0271     unsigned int ctl_credits;
0272     unsigned int dma_credits;
0273 };
0274 
0275 /**
0276  * struct usb4_port - USB4 port device
0277  * @dev: Device for the port
0278  * @port: Pointer to the lane 0 adapter
0279  * @can_offline: Does the port have necessary platform support to moved
0280  *       it into offline mode and back
0281  * @offline: The port is currently in offline mode
0282  */
0283 struct usb4_port {
0284     struct device dev;
0285     struct tb_port *port;
0286     bool can_offline;
0287     bool offline;
0288 };
0289 
0290 /**
0291  * tb_retimer: Thunderbolt retimer
0292  * @dev: Device for the retimer
0293  * @tb: Pointer to the domain the retimer belongs to
0294  * @index: Retimer index facing the router USB4 port
0295  * @vendor: Vendor ID of the retimer
0296  * @device: Device ID of the retimer
0297  * @port: Pointer to the lane 0 adapter
0298  * @nvm: Pointer to the NVM if the retimer has one (%NULL otherwise)
0299  * @auth_status: Status of last NVM authentication
0300  */
0301 struct tb_retimer {
0302     struct device dev;
0303     struct tb *tb;
0304     u8 index;
0305     u32 vendor;
0306     u32 device;
0307     struct tb_port *port;
0308     struct tb_nvm *nvm;
0309     u32 auth_status;
0310 };
0311 
0312 /**
0313  * struct tb_path_hop - routing information for a tb_path
0314  * @in_port: Ingress port of a switch
0315  * @out_port: Egress port of a switch where the packet is routed out
0316  *        (must be on the same switch than @in_port)
0317  * @in_hop_index: HopID where the path configuration entry is placed in
0318  *        the path config space of @in_port.
0319  * @in_counter_index: Used counter index (not used in the driver
0320  *            currently, %-1 to disable)
0321  * @next_hop_index: HopID of the packet when it is routed out from @out_port
0322  * @initial_credits: Number of initial flow control credits allocated for
0323  *           the path
0324  * @nfc_credits: Number of non-flow controlled buffers allocated for the
0325  *       @in_port.
0326  *
0327  * Hop configuration is always done on the IN port of a switch.
0328  * in_port and out_port have to be on the same switch. Packets arriving on
0329  * in_port with "hop" = in_hop_index will get routed to through out_port. The
0330  * next hop to take (on out_port->remote) is determined by
0331  * next_hop_index. When routing packet to another switch (out->remote is
0332  * set) the @next_hop_index must match the @in_hop_index of that next
0333  * hop to make routing possible.
0334  *
0335  * in_counter_index is the index of a counter (in TB_CFG_COUNTERS) on the in
0336  * port.
0337  */
0338 struct tb_path_hop {
0339     struct tb_port *in_port;
0340     struct tb_port *out_port;
0341     int in_hop_index;
0342     int in_counter_index;
0343     int next_hop_index;
0344     unsigned int initial_credits;
0345     unsigned int nfc_credits;
0346 };
0347 
0348 /**
0349  * enum tb_path_port - path options mask
0350  * @TB_PATH_NONE: Do not activate on any hop on path
0351  * @TB_PATH_SOURCE: Activate on the first hop (out of src)
0352  * @TB_PATH_INTERNAL: Activate on the intermediate hops (not the first/last)
0353  * @TB_PATH_DESTINATION: Activate on the last hop (into dst)
0354  * @TB_PATH_ALL: Activate on all hops on the path
0355  */
0356 enum tb_path_port {
0357     TB_PATH_NONE = 0,
0358     TB_PATH_SOURCE = 1,
0359     TB_PATH_INTERNAL = 2,
0360     TB_PATH_DESTINATION = 4,
0361     TB_PATH_ALL = 7,
0362 };
0363 
0364 /**
0365  * struct tb_path - a unidirectional path between two ports
0366  * @tb: Pointer to the domain structure
0367  * @name: Name of the path (used for debugging)
0368  * @ingress_shared_buffer: Shared buffering used for ingress ports on the path
0369  * @egress_shared_buffer: Shared buffering used for egress ports on the path
0370  * @ingress_fc_enable: Flow control for ingress ports on the path
0371  * @egress_fc_enable: Flow control for egress ports on the path
0372  * @priority: Priority group if the path
0373  * @weight: Weight of the path inside the priority group
0374  * @drop_packages: Drop packages from queue tail or head
0375  * @activated: Is the path active
0376  * @clear_fc: Clear all flow control from the path config space entries
0377  *        when deactivating this path
0378  * @hops: Path hops
0379  * @path_length: How many hops the path uses
0380  * @alloc_hopid: Does this path consume port HopID
0381  *
0382  * A path consists of a number of hops (see &struct tb_path_hop). To
0383  * establish a PCIe tunnel two paths have to be created between the two
0384  * PCIe ports.
0385  */
0386 struct tb_path {
0387     struct tb *tb;
0388     const char *name;
0389     enum tb_path_port ingress_shared_buffer;
0390     enum tb_path_port egress_shared_buffer;
0391     enum tb_path_port ingress_fc_enable;
0392     enum tb_path_port egress_fc_enable;
0393 
0394     unsigned int priority:3;
0395     int weight:4;
0396     bool drop_packages;
0397     bool activated;
0398     bool clear_fc;
0399     struct tb_path_hop *hops;
0400     int path_length;
0401     bool alloc_hopid;
0402 };
0403 
0404 /* HopIDs 0-7 are reserved by the Thunderbolt protocol */
0405 #define TB_PATH_MIN_HOPID   8
0406 /*
0407  * Support paths from the farthest (depth 6) router to the host and back
0408  * to the same level (not necessarily to the same router).
0409  */
0410 #define TB_PATH_MAX_HOPS    (7 * 2)
0411 
0412 /* Possible wake types */
0413 #define TB_WAKE_ON_CONNECT  BIT(0)
0414 #define TB_WAKE_ON_DISCONNECT   BIT(1)
0415 #define TB_WAKE_ON_USB4     BIT(2)
0416 #define TB_WAKE_ON_USB3     BIT(3)
0417 #define TB_WAKE_ON_PCIE     BIT(4)
0418 #define TB_WAKE_ON_DP       BIT(5)
0419 
0420 /**
0421  * struct tb_cm_ops - Connection manager specific operations vector
0422  * @driver_ready: Called right after control channel is started. Used by
0423  *        ICM to send driver ready message to the firmware.
0424  * @start: Starts the domain
0425  * @stop: Stops the domain
0426  * @suspend_noirq: Connection manager specific suspend_noirq
0427  * @resume_noirq: Connection manager specific resume_noirq
0428  * @suspend: Connection manager specific suspend
0429  * @freeze_noirq: Connection manager specific freeze_noirq
0430  * @thaw_noirq: Connection manager specific thaw_noirq
0431  * @complete: Connection manager specific complete
0432  * @runtime_suspend: Connection manager specific runtime_suspend
0433  * @runtime_resume: Connection manager specific runtime_resume
0434  * @runtime_suspend_switch: Runtime suspend a switch
0435  * @runtime_resume_switch: Runtime resume a switch
0436  * @handle_event: Handle thunderbolt event
0437  * @get_boot_acl: Get boot ACL list
0438  * @set_boot_acl: Set boot ACL list
0439  * @disapprove_switch: Disapprove switch (disconnect PCIe tunnel)
0440  * @approve_switch: Approve switch
0441  * @add_switch_key: Add key to switch
0442  * @challenge_switch_key: Challenge switch using key
0443  * @disconnect_pcie_paths: Disconnects PCIe paths before NVM update
0444  * @approve_xdomain_paths: Approve (establish) XDomain DMA paths
0445  * @disconnect_xdomain_paths: Disconnect XDomain DMA paths
0446  * @usb4_switch_op: Optional proxy for USB4 router operations. If set
0447  *          this will be called whenever USB4 router operation is
0448  *          performed. If this returns %-EOPNOTSUPP then the
0449  *          native USB4 router operation is called.
0450  * @usb4_switch_nvm_authenticate_status: Optional callback that the CM
0451  *                   implementation can be used to
0452  *                   return status of USB4 NVM_AUTH
0453  *                   router operation.
0454  */
0455 struct tb_cm_ops {
0456     int (*driver_ready)(struct tb *tb);
0457     int (*start)(struct tb *tb);
0458     void (*stop)(struct tb *tb);
0459     int (*suspend_noirq)(struct tb *tb);
0460     int (*resume_noirq)(struct tb *tb);
0461     int (*suspend)(struct tb *tb);
0462     int (*freeze_noirq)(struct tb *tb);
0463     int (*thaw_noirq)(struct tb *tb);
0464     void (*complete)(struct tb *tb);
0465     int (*runtime_suspend)(struct tb *tb);
0466     int (*runtime_resume)(struct tb *tb);
0467     int (*runtime_suspend_switch)(struct tb_switch *sw);
0468     int (*runtime_resume_switch)(struct tb_switch *sw);
0469     void (*handle_event)(struct tb *tb, enum tb_cfg_pkg_type,
0470                  const void *buf, size_t size);
0471     int (*get_boot_acl)(struct tb *tb, uuid_t *uuids, size_t nuuids);
0472     int (*set_boot_acl)(struct tb *tb, const uuid_t *uuids, size_t nuuids);
0473     int (*disapprove_switch)(struct tb *tb, struct tb_switch *sw);
0474     int (*approve_switch)(struct tb *tb, struct tb_switch *sw);
0475     int (*add_switch_key)(struct tb *tb, struct tb_switch *sw);
0476     int (*challenge_switch_key)(struct tb *tb, struct tb_switch *sw,
0477                     const u8 *challenge, u8 *response);
0478     int (*disconnect_pcie_paths)(struct tb *tb);
0479     int (*approve_xdomain_paths)(struct tb *tb, struct tb_xdomain *xd,
0480                      int transmit_path, int transmit_ring,
0481                      int receive_path, int receive_ring);
0482     int (*disconnect_xdomain_paths)(struct tb *tb, struct tb_xdomain *xd,
0483                     int transmit_path, int transmit_ring,
0484                     int receive_path, int receive_ring);
0485     int (*usb4_switch_op)(struct tb_switch *sw, u16 opcode, u32 *metadata,
0486                   u8 *status, const void *tx_data, size_t tx_data_len,
0487                   void *rx_data, size_t rx_data_len);
0488     int (*usb4_switch_nvm_authenticate_status)(struct tb_switch *sw,
0489                            u32 *status);
0490 };
0491 
0492 static inline void *tb_priv(struct tb *tb)
0493 {
0494     return (void *)tb->privdata;
0495 }
0496 
0497 #define TB_AUTOSUSPEND_DELAY        15000 /* ms */
0498 
0499 /* helper functions & macros */
0500 
0501 /**
0502  * tb_upstream_port() - return the upstream port of a switch
0503  *
0504  * Every switch has an upstream port (for the root switch it is the NHI).
0505  *
0506  * During switch alloc/init tb_upstream_port()->remote may be NULL, even for
0507  * non root switches (on the NHI port remote is always NULL).
0508  *
0509  * Return: Returns the upstream port of the switch.
0510  */
0511 static inline struct tb_port *tb_upstream_port(struct tb_switch *sw)
0512 {
0513     return &sw->ports[sw->config.upstream_port_number];
0514 }
0515 
0516 /**
0517  * tb_is_upstream_port() - Is the port upstream facing
0518  * @port: Port to check
0519  *
0520  * Returns true if @port is upstream facing port. In case of dual link
0521  * ports both return true.
0522  */
0523 static inline bool tb_is_upstream_port(const struct tb_port *port)
0524 {
0525     const struct tb_port *upstream_port = tb_upstream_port(port->sw);
0526     return port == upstream_port || port->dual_link_port == upstream_port;
0527 }
0528 
0529 static inline u64 tb_route(const struct tb_switch *sw)
0530 {
0531     return ((u64) sw->config.route_hi) << 32 | sw->config.route_lo;
0532 }
0533 
0534 static inline struct tb_port *tb_port_at(u64 route, struct tb_switch *sw)
0535 {
0536     u8 port;
0537 
0538     port = route >> (sw->config.depth * 8);
0539     if (WARN_ON(port > sw->config.max_port_number))
0540         return NULL;
0541     return &sw->ports[port];
0542 }
0543 
0544 /**
0545  * tb_port_has_remote() - Does the port have switch connected downstream
0546  * @port: Port to check
0547  *
0548  * Returns true only when the port is primary port and has remote set.
0549  */
0550 static inline bool tb_port_has_remote(const struct tb_port *port)
0551 {
0552     if (tb_is_upstream_port(port))
0553         return false;
0554     if (!port->remote)
0555         return false;
0556     if (port->dual_link_port && port->link_nr)
0557         return false;
0558 
0559     return true;
0560 }
0561 
0562 static inline bool tb_port_is_null(const struct tb_port *port)
0563 {
0564     return port && port->port && port->config.type == TB_TYPE_PORT;
0565 }
0566 
0567 static inline bool tb_port_is_nhi(const struct tb_port *port)
0568 {
0569     return port && port->config.type == TB_TYPE_NHI;
0570 }
0571 
0572 static inline bool tb_port_is_pcie_down(const struct tb_port *port)
0573 {
0574     return port && port->config.type == TB_TYPE_PCIE_DOWN;
0575 }
0576 
0577 static inline bool tb_port_is_pcie_up(const struct tb_port *port)
0578 {
0579     return port && port->config.type == TB_TYPE_PCIE_UP;
0580 }
0581 
0582 static inline bool tb_port_is_dpin(const struct tb_port *port)
0583 {
0584     return port && port->config.type == TB_TYPE_DP_HDMI_IN;
0585 }
0586 
0587 static inline bool tb_port_is_dpout(const struct tb_port *port)
0588 {
0589     return port && port->config.type == TB_TYPE_DP_HDMI_OUT;
0590 }
0591 
0592 static inline bool tb_port_is_usb3_down(const struct tb_port *port)
0593 {
0594     return port && port->config.type == TB_TYPE_USB3_DOWN;
0595 }
0596 
0597 static inline bool tb_port_is_usb3_up(const struct tb_port *port)
0598 {
0599     return port && port->config.type == TB_TYPE_USB3_UP;
0600 }
0601 
0602 static inline int tb_sw_read(struct tb_switch *sw, void *buffer,
0603                  enum tb_cfg_space space, u32 offset, u32 length)
0604 {
0605     if (sw->is_unplugged)
0606         return -ENODEV;
0607     return tb_cfg_read(sw->tb->ctl,
0608                buffer,
0609                tb_route(sw),
0610                0,
0611                space,
0612                offset,
0613                length);
0614 }
0615 
0616 static inline int tb_sw_write(struct tb_switch *sw, const void *buffer,
0617                   enum tb_cfg_space space, u32 offset, u32 length)
0618 {
0619     if (sw->is_unplugged)
0620         return -ENODEV;
0621     return tb_cfg_write(sw->tb->ctl,
0622                 buffer,
0623                 tb_route(sw),
0624                 0,
0625                 space,
0626                 offset,
0627                 length);
0628 }
0629 
0630 static inline int tb_port_read(struct tb_port *port, void *buffer,
0631                    enum tb_cfg_space space, u32 offset, u32 length)
0632 {
0633     if (port->sw->is_unplugged)
0634         return -ENODEV;
0635     return tb_cfg_read(port->sw->tb->ctl,
0636                buffer,
0637                tb_route(port->sw),
0638                port->port,
0639                space,
0640                offset,
0641                length);
0642 }
0643 
0644 static inline int tb_port_write(struct tb_port *port, const void *buffer,
0645                 enum tb_cfg_space space, u32 offset, u32 length)
0646 {
0647     if (port->sw->is_unplugged)
0648         return -ENODEV;
0649     return tb_cfg_write(port->sw->tb->ctl,
0650                 buffer,
0651                 tb_route(port->sw),
0652                 port->port,
0653                 space,
0654                 offset,
0655                 length);
0656 }
0657 
0658 #define tb_err(tb, fmt, arg...) dev_err(&(tb)->nhi->pdev->dev, fmt, ## arg)
0659 #define tb_WARN(tb, fmt, arg...) dev_WARN(&(tb)->nhi->pdev->dev, fmt, ## arg)
0660 #define tb_warn(tb, fmt, arg...) dev_warn(&(tb)->nhi->pdev->dev, fmt, ## arg)
0661 #define tb_info(tb, fmt, arg...) dev_info(&(tb)->nhi->pdev->dev, fmt, ## arg)
0662 #define tb_dbg(tb, fmt, arg...) dev_dbg(&(tb)->nhi->pdev->dev, fmt, ## arg)
0663 
0664 #define __TB_SW_PRINT(level, sw, fmt, arg...)           \
0665     do {                                            \
0666         const struct tb_switch *__sw = (sw);    \
0667         level(__sw->tb, "%llx: " fmt,           \
0668               tb_route(__sw), ## arg);          \
0669     } while (0)
0670 #define tb_sw_WARN(sw, fmt, arg...) __TB_SW_PRINT(tb_WARN, sw, fmt, ##arg)
0671 #define tb_sw_warn(sw, fmt, arg...) __TB_SW_PRINT(tb_warn, sw, fmt, ##arg)
0672 #define tb_sw_info(sw, fmt, arg...) __TB_SW_PRINT(tb_info, sw, fmt, ##arg)
0673 #define tb_sw_dbg(sw, fmt, arg...) __TB_SW_PRINT(tb_dbg, sw, fmt, ##arg)
0674 
0675 #define __TB_PORT_PRINT(level, _port, fmt, arg...)                      \
0676     do {                                                            \
0677         const struct tb_port *__port = (_port);                 \
0678         level(__port->sw->tb, "%llx:%u: " fmt,                  \
0679               tb_route(__port->sw), __port->port, ## arg);      \
0680     } while (0)
0681 #define tb_port_WARN(port, fmt, arg...) \
0682     __TB_PORT_PRINT(tb_WARN, port, fmt, ##arg)
0683 #define tb_port_warn(port, fmt, arg...) \
0684     __TB_PORT_PRINT(tb_warn, port, fmt, ##arg)
0685 #define tb_port_info(port, fmt, arg...) \
0686     __TB_PORT_PRINT(tb_info, port, fmt, ##arg)
0687 #define tb_port_dbg(port, fmt, arg...) \
0688     __TB_PORT_PRINT(tb_dbg, port, fmt, ##arg)
0689 
0690 struct tb *icm_probe(struct tb_nhi *nhi);
0691 struct tb *tb_probe(struct tb_nhi *nhi);
0692 
0693 extern struct device_type tb_domain_type;
0694 extern struct device_type tb_retimer_type;
0695 extern struct device_type tb_switch_type;
0696 extern struct device_type usb4_port_device_type;
0697 
0698 int tb_domain_init(void);
0699 void tb_domain_exit(void);
0700 int tb_xdomain_init(void);
0701 void tb_xdomain_exit(void);
0702 
0703 struct tb *tb_domain_alloc(struct tb_nhi *nhi, int timeout_msec, size_t privsize);
0704 int tb_domain_add(struct tb *tb);
0705 void tb_domain_remove(struct tb *tb);
0706 int tb_domain_suspend_noirq(struct tb *tb);
0707 int tb_domain_resume_noirq(struct tb *tb);
0708 int tb_domain_suspend(struct tb *tb);
0709 int tb_domain_freeze_noirq(struct tb *tb);
0710 int tb_domain_thaw_noirq(struct tb *tb);
0711 void tb_domain_complete(struct tb *tb);
0712 int tb_domain_runtime_suspend(struct tb *tb);
0713 int tb_domain_runtime_resume(struct tb *tb);
0714 int tb_domain_disapprove_switch(struct tb *tb, struct tb_switch *sw);
0715 int tb_domain_approve_switch(struct tb *tb, struct tb_switch *sw);
0716 int tb_domain_approve_switch_key(struct tb *tb, struct tb_switch *sw);
0717 int tb_domain_challenge_switch_key(struct tb *tb, struct tb_switch *sw);
0718 int tb_domain_disconnect_pcie_paths(struct tb *tb);
0719 int tb_domain_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
0720                     int transmit_path, int transmit_ring,
0721                     int receive_path, int receive_ring);
0722 int tb_domain_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
0723                        int transmit_path, int transmit_ring,
0724                        int receive_path, int receive_ring);
0725 int tb_domain_disconnect_all_paths(struct tb *tb);
0726 
0727 static inline struct tb *tb_domain_get(struct tb *tb)
0728 {
0729     if (tb)
0730         get_device(&tb->dev);
0731     return tb;
0732 }
0733 
0734 static inline void tb_domain_put(struct tb *tb)
0735 {
0736     put_device(&tb->dev);
0737 }
0738 
0739 struct tb_nvm *tb_nvm_alloc(struct device *dev);
0740 int tb_nvm_add_active(struct tb_nvm *nvm, size_t size, nvmem_reg_read_t reg_read);
0741 int tb_nvm_write_buf(struct tb_nvm *nvm, unsigned int offset, void *val,
0742              size_t bytes);
0743 int tb_nvm_add_non_active(struct tb_nvm *nvm, size_t size,
0744               nvmem_reg_write_t reg_write);
0745 void tb_nvm_free(struct tb_nvm *nvm);
0746 void tb_nvm_exit(void);
0747 
0748 typedef int (*read_block_fn)(void *, unsigned int, void *, size_t);
0749 typedef int (*write_block_fn)(void *, unsigned int, const void *, size_t);
0750 
0751 int tb_nvm_read_data(unsigned int address, void *buf, size_t size,
0752              unsigned int retries, read_block_fn read_block,
0753              void *read_block_data);
0754 int tb_nvm_write_data(unsigned int address, const void *buf, size_t size,
0755               unsigned int retries, write_block_fn write_next_block,
0756               void *write_block_data);
0757 
0758 struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent,
0759                   u64 route);
0760 struct tb_switch *tb_switch_alloc_safe_mode(struct tb *tb,
0761             struct device *parent, u64 route);
0762 int tb_switch_configure(struct tb_switch *sw);
0763 int tb_switch_add(struct tb_switch *sw);
0764 void tb_switch_remove(struct tb_switch *sw);
0765 void tb_switch_suspend(struct tb_switch *sw, bool runtime);
0766 int tb_switch_resume(struct tb_switch *sw);
0767 int tb_switch_reset(struct tb_switch *sw);
0768 int tb_switch_wait_for_bit(struct tb_switch *sw, u32 offset, u32 bit,
0769                u32 value, int timeout_msec);
0770 void tb_sw_set_unplugged(struct tb_switch *sw);
0771 struct tb_port *tb_switch_find_port(struct tb_switch *sw,
0772                     enum tb_port_type type);
0773 struct tb_switch *tb_switch_find_by_link_depth(struct tb *tb, u8 link,
0774                            u8 depth);
0775 struct tb_switch *tb_switch_find_by_uuid(struct tb *tb, const uuid_t *uuid);
0776 struct tb_switch *tb_switch_find_by_route(struct tb *tb, u64 route);
0777 
0778 /**
0779  * tb_switch_for_each_port() - Iterate over each switch port
0780  * @sw: Switch whose ports to iterate
0781  * @p: Port used as iterator
0782  *
0783  * Iterates over each switch port skipping the control port (port %0).
0784  */
0785 #define tb_switch_for_each_port(sw, p)                  \
0786     for ((p) = &(sw)->ports[1];                 \
0787          (p) <= &(sw)->ports[(sw)->config.max_port_number]; (p)++)
0788 
0789 static inline struct tb_switch *tb_switch_get(struct tb_switch *sw)
0790 {
0791     if (sw)
0792         get_device(&sw->dev);
0793     return sw;
0794 }
0795 
0796 static inline void tb_switch_put(struct tb_switch *sw)
0797 {
0798     put_device(&sw->dev);
0799 }
0800 
0801 static inline bool tb_is_switch(const struct device *dev)
0802 {
0803     return dev->type == &tb_switch_type;
0804 }
0805 
0806 static inline struct tb_switch *tb_to_switch(struct device *dev)
0807 {
0808     if (tb_is_switch(dev))
0809         return container_of(dev, struct tb_switch, dev);
0810     return NULL;
0811 }
0812 
0813 static inline struct tb_switch *tb_switch_parent(struct tb_switch *sw)
0814 {
0815     return tb_to_switch(sw->dev.parent);
0816 }
0817 
0818 static inline bool tb_switch_is_light_ridge(const struct tb_switch *sw)
0819 {
0820     return sw->config.vendor_id == PCI_VENDOR_ID_INTEL &&
0821            sw->config.device_id == PCI_DEVICE_ID_INTEL_LIGHT_RIDGE;
0822 }
0823 
0824 static inline bool tb_switch_is_eagle_ridge(const struct tb_switch *sw)
0825 {
0826     return sw->config.vendor_id == PCI_VENDOR_ID_INTEL &&
0827            sw->config.device_id == PCI_DEVICE_ID_INTEL_EAGLE_RIDGE;
0828 }
0829 
0830 static inline bool tb_switch_is_cactus_ridge(const struct tb_switch *sw)
0831 {
0832     if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
0833         switch (sw->config.device_id) {
0834         case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C:
0835         case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
0836             return true;
0837         }
0838     }
0839     return false;
0840 }
0841 
0842 static inline bool tb_switch_is_falcon_ridge(const struct tb_switch *sw)
0843 {
0844     if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
0845         switch (sw->config.device_id) {
0846         case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE:
0847         case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE:
0848             return true;
0849         }
0850     }
0851     return false;
0852 }
0853 
0854 static inline bool tb_switch_is_alpine_ridge(const struct tb_switch *sw)
0855 {
0856     if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
0857         switch (sw->config.device_id) {
0858         case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
0859         case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE:
0860         case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
0861         case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
0862         case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
0863             return true;
0864         }
0865     }
0866     return false;
0867 }
0868 
0869 static inline bool tb_switch_is_titan_ridge(const struct tb_switch *sw)
0870 {
0871     if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
0872         switch (sw->config.device_id) {
0873         case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE:
0874         case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE:
0875         case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_DD_BRIDGE:
0876             return true;
0877         }
0878     }
0879     return false;
0880 }
0881 
0882 static inline bool tb_switch_is_tiger_lake(const struct tb_switch *sw)
0883 {
0884     if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
0885         switch (sw->config.device_id) {
0886         case PCI_DEVICE_ID_INTEL_TGL_NHI0:
0887         case PCI_DEVICE_ID_INTEL_TGL_NHI1:
0888         case PCI_DEVICE_ID_INTEL_TGL_H_NHI0:
0889         case PCI_DEVICE_ID_INTEL_TGL_H_NHI1:
0890             return true;
0891         }
0892     }
0893     return false;
0894 }
0895 
0896 /**
0897  * tb_switch_is_usb4() - Is the switch USB4 compliant
0898  * @sw: Switch to check
0899  *
0900  * Returns true if the @sw is USB4 compliant router, false otherwise.
0901  */
0902 static inline bool tb_switch_is_usb4(const struct tb_switch *sw)
0903 {
0904     return sw->config.thunderbolt_version == USB4_VERSION_1_0;
0905 }
0906 
0907 /**
0908  * tb_switch_is_icm() - Is the switch handled by ICM firmware
0909  * @sw: Switch to check
0910  *
0911  * In case there is a need to differentiate whether ICM firmware or SW CM
0912  * is handling @sw this function can be called. It is valid to call this
0913  * after tb_switch_alloc() and tb_switch_configure() has been called
0914  * (latter only for SW CM case).
0915  */
0916 static inline bool tb_switch_is_icm(const struct tb_switch *sw)
0917 {
0918     return !sw->config.enabled;
0919 }
0920 
0921 int tb_switch_lane_bonding_enable(struct tb_switch *sw);
0922 void tb_switch_lane_bonding_disable(struct tb_switch *sw);
0923 int tb_switch_configure_link(struct tb_switch *sw);
0924 void tb_switch_unconfigure_link(struct tb_switch *sw);
0925 
0926 bool tb_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in);
0927 int tb_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in);
0928 void tb_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in);
0929 
0930 int tb_switch_tmu_init(struct tb_switch *sw);
0931 int tb_switch_tmu_post_time(struct tb_switch *sw);
0932 int tb_switch_tmu_disable(struct tb_switch *sw);
0933 int tb_switch_tmu_enable(struct tb_switch *sw);
0934 void tb_switch_tmu_configure(struct tb_switch *sw,
0935                  enum tb_switch_tmu_rate rate,
0936                  bool unidirectional);
0937 void tb_switch_enable_tmu_1st_child(struct tb_switch *sw,
0938                     enum tb_switch_tmu_rate rate);
0939 /**
0940  * tb_switch_tmu_is_enabled() - Checks if the specified TMU mode is enabled
0941  * @sw: Router whose TMU mode to check
0942  * @unidirectional: If uni-directional (bi-directional otherwise)
0943  *
0944  * Return true if hardware TMU configuration matches the one passed in
0945  * as parameter. That is HiFi/Normal and either uni-directional or bi-directional.
0946  */
0947 static inline bool tb_switch_tmu_is_enabled(const struct tb_switch *sw,
0948                         bool unidirectional)
0949 {
0950     return sw->tmu.rate == sw->tmu.rate_request &&
0951            sw->tmu.unidirectional == unidirectional;
0952 }
0953 
0954 static inline const char *tb_switch_clx_name(enum tb_clx clx)
0955 {
0956     switch (clx) {
0957     /* CL0s and CL1 are enabled and supported together */
0958     case TB_CL1:
0959         return "CL0s/CL1";
0960     default:
0961         return "unknown";
0962     }
0963 }
0964 
0965 int tb_switch_enable_clx(struct tb_switch *sw, enum tb_clx clx);
0966 int tb_switch_disable_clx(struct tb_switch *sw, enum tb_clx clx);
0967 
0968 /**
0969  * tb_switch_is_clx_enabled() - Checks if the CLx is enabled
0970  * @sw: Router to check for the CLx
0971  * @clx: The CLx state to check for
0972  *
0973  * Checks if the specified CLx is enabled on the router upstream link.
0974  * Not applicable for a host router.
0975  */
0976 static inline bool tb_switch_is_clx_enabled(const struct tb_switch *sw,
0977                         enum tb_clx clx)
0978 {
0979     return sw->clx == clx;
0980 }
0981 
0982 /**
0983  * tb_switch_is_clx_supported() - Is CLx supported on this type of router
0984  * @sw: The router to check CLx support for
0985  */
0986 static inline bool tb_switch_is_clx_supported(const struct tb_switch *sw)
0987 {
0988     return tb_switch_is_usb4(sw) || tb_switch_is_titan_ridge(sw);
0989 }
0990 
0991 int tb_switch_mask_clx_objections(struct tb_switch *sw);
0992 
0993 int tb_switch_pcie_l1_enable(struct tb_switch *sw);
0994 
0995 int tb_switch_xhci_connect(struct tb_switch *sw);
0996 void tb_switch_xhci_disconnect(struct tb_switch *sw);
0997 
0998 int tb_port_state(struct tb_port *port);
0999 int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged);
1000 int tb_port_add_nfc_credits(struct tb_port *port, int credits);
1001 int tb_port_clear_counter(struct tb_port *port, int counter);
1002 int tb_port_unlock(struct tb_port *port);
1003 int tb_port_enable(struct tb_port *port);
1004 int tb_port_disable(struct tb_port *port);
1005 int tb_port_alloc_in_hopid(struct tb_port *port, int hopid, int max_hopid);
1006 void tb_port_release_in_hopid(struct tb_port *port, int hopid);
1007 int tb_port_alloc_out_hopid(struct tb_port *port, int hopid, int max_hopid);
1008 void tb_port_release_out_hopid(struct tb_port *port, int hopid);
1009 struct tb_port *tb_next_port_on_path(struct tb_port *start, struct tb_port *end,
1010                      struct tb_port *prev);
1011 
1012 static inline bool tb_port_use_credit_allocation(const struct tb_port *port)
1013 {
1014     return tb_port_is_null(port) && port->sw->credit_allocation;
1015 }
1016 
1017 /**
1018  * tb_for_each_port_on_path() - Iterate over each port on path
1019  * @src: Source port
1020  * @dst: Destination port
1021  * @p: Port used as iterator
1022  *
1023  * Walks over each port on path from @src to @dst.
1024  */
1025 #define tb_for_each_port_on_path(src, dst, p)               \
1026     for ((p) = tb_next_port_on_path((src), (dst), NULL); (p);   \
1027          (p) = tb_next_port_on_path((src), (dst), (p)))
1028 
1029 int tb_port_get_link_speed(struct tb_port *port);
1030 int tb_port_get_link_width(struct tb_port *port);
1031 int tb_port_set_link_width(struct tb_port *port, unsigned int width);
1032 int tb_port_set_lane_bonding(struct tb_port *port, bool bonding);
1033 int tb_port_lane_bonding_enable(struct tb_port *port);
1034 void tb_port_lane_bonding_disable(struct tb_port *port);
1035 int tb_port_wait_for_link_width(struct tb_port *port, int width,
1036                 int timeout_msec);
1037 int tb_port_update_credits(struct tb_port *port);
1038 
1039 int tb_switch_find_vse_cap(struct tb_switch *sw, enum tb_switch_vse_cap vsec);
1040 int tb_switch_find_cap(struct tb_switch *sw, enum tb_switch_cap cap);
1041 int tb_switch_next_cap(struct tb_switch *sw, unsigned int offset);
1042 int tb_port_find_cap(struct tb_port *port, enum tb_port_cap cap);
1043 int tb_port_next_cap(struct tb_port *port, unsigned int offset);
1044 bool tb_port_is_enabled(struct tb_port *port);
1045 
1046 bool tb_usb3_port_is_enabled(struct tb_port *port);
1047 int tb_usb3_port_enable(struct tb_port *port, bool enable);
1048 
1049 bool tb_pci_port_is_enabled(struct tb_port *port);
1050 int tb_pci_port_enable(struct tb_port *port, bool enable);
1051 
1052 int tb_dp_port_hpd_is_active(struct tb_port *port);
1053 int tb_dp_port_hpd_clear(struct tb_port *port);
1054 int tb_dp_port_set_hops(struct tb_port *port, unsigned int video,
1055             unsigned int aux_tx, unsigned int aux_rx);
1056 bool tb_dp_port_is_enabled(struct tb_port *port);
1057 int tb_dp_port_enable(struct tb_port *port, bool enable);
1058 
1059 struct tb_path *tb_path_discover(struct tb_port *src, int src_hopid,
1060                  struct tb_port *dst, int dst_hopid,
1061                  struct tb_port **last, const char *name,
1062                  bool alloc_hopid);
1063 struct tb_path *tb_path_alloc(struct tb *tb, struct tb_port *src, int src_hopid,
1064                   struct tb_port *dst, int dst_hopid, int link_nr,
1065                   const char *name);
1066 void tb_path_free(struct tb_path *path);
1067 int tb_path_activate(struct tb_path *path);
1068 void tb_path_deactivate(struct tb_path *path);
1069 bool tb_path_is_invalid(struct tb_path *path);
1070 bool tb_path_port_on_path(const struct tb_path *path,
1071               const struct tb_port *port);
1072 
1073 /**
1074  * tb_path_for_each_hop() - Iterate over each hop on path
1075  * @path: Path whose hops to iterate
1076  * @hop: Hop used as iterator
1077  *
1078  * Iterates over each hop on path.
1079  */
1080 #define tb_path_for_each_hop(path, hop)                 \
1081     for ((hop) = &(path)->hops[0];                  \
1082          (hop) <= &(path)->hops[(path)->path_length - 1]; (hop)++)
1083 
1084 int tb_drom_read(struct tb_switch *sw);
1085 int tb_drom_read_uid_only(struct tb_switch *sw, u64 *uid);
1086 
1087 int tb_lc_read_uuid(struct tb_switch *sw, u32 *uuid);
1088 int tb_lc_configure_port(struct tb_port *port);
1089 void tb_lc_unconfigure_port(struct tb_port *port);
1090 int tb_lc_configure_xdomain(struct tb_port *port);
1091 void tb_lc_unconfigure_xdomain(struct tb_port *port);
1092 int tb_lc_start_lane_initialization(struct tb_port *port);
1093 bool tb_lc_is_clx_supported(struct tb_port *port);
1094 bool tb_lc_is_usb_plugged(struct tb_port *port);
1095 bool tb_lc_is_xhci_connected(struct tb_port *port);
1096 int tb_lc_xhci_connect(struct tb_port *port);
1097 void tb_lc_xhci_disconnect(struct tb_port *port);
1098 int tb_lc_set_wake(struct tb_switch *sw, unsigned int flags);
1099 int tb_lc_set_sleep(struct tb_switch *sw);
1100 bool tb_lc_lane_bonding_possible(struct tb_switch *sw);
1101 bool tb_lc_dp_sink_query(struct tb_switch *sw, struct tb_port *in);
1102 int tb_lc_dp_sink_alloc(struct tb_switch *sw, struct tb_port *in);
1103 int tb_lc_dp_sink_dealloc(struct tb_switch *sw, struct tb_port *in);
1104 int tb_lc_force_power(struct tb_switch *sw);
1105 
1106 static inline int tb_route_length(u64 route)
1107 {
1108     return (fls64(route) + TB_ROUTE_SHIFT - 1) / TB_ROUTE_SHIFT;
1109 }
1110 
1111 /**
1112  * tb_downstream_route() - get route to downstream switch
1113  *
1114  * Port must not be the upstream port (otherwise a loop is created).
1115  *
1116  * Return: Returns a route to the switch behind @port.
1117  */
1118 static inline u64 tb_downstream_route(struct tb_port *port)
1119 {
1120     return tb_route(port->sw)
1121            | ((u64) port->port << (port->sw->config.depth * 8));
1122 }
1123 
1124 bool tb_is_xdomain_enabled(void);
1125 bool tb_xdomain_handle_request(struct tb *tb, enum tb_cfg_pkg_type type,
1126                    const void *buf, size_t size);
1127 struct tb_xdomain *tb_xdomain_alloc(struct tb *tb, struct device *parent,
1128                     u64 route, const uuid_t *local_uuid,
1129                     const uuid_t *remote_uuid);
1130 void tb_xdomain_add(struct tb_xdomain *xd);
1131 void tb_xdomain_remove(struct tb_xdomain *xd);
1132 struct tb_xdomain *tb_xdomain_find_by_link_depth(struct tb *tb, u8 link,
1133                          u8 depth);
1134 
1135 int tb_retimer_scan(struct tb_port *port, bool add);
1136 void tb_retimer_remove_all(struct tb_port *port);
1137 
1138 static inline bool tb_is_retimer(const struct device *dev)
1139 {
1140     return dev->type == &tb_retimer_type;
1141 }
1142 
1143 static inline struct tb_retimer *tb_to_retimer(struct device *dev)
1144 {
1145     if (tb_is_retimer(dev))
1146         return container_of(dev, struct tb_retimer, dev);
1147     return NULL;
1148 }
1149 
1150 int usb4_switch_setup(struct tb_switch *sw);
1151 int usb4_switch_read_uid(struct tb_switch *sw, u64 *uid);
1152 int usb4_switch_drom_read(struct tb_switch *sw, unsigned int address, void *buf,
1153               size_t size);
1154 bool usb4_switch_lane_bonding_possible(struct tb_switch *sw);
1155 int usb4_switch_set_wake(struct tb_switch *sw, unsigned int flags);
1156 int usb4_switch_set_sleep(struct tb_switch *sw);
1157 int usb4_switch_nvm_sector_size(struct tb_switch *sw);
1158 int usb4_switch_nvm_read(struct tb_switch *sw, unsigned int address, void *buf,
1159              size_t size);
1160 int usb4_switch_nvm_set_offset(struct tb_switch *sw, unsigned int address);
1161 int usb4_switch_nvm_write(struct tb_switch *sw, unsigned int address,
1162               const void *buf, size_t size);
1163 int usb4_switch_nvm_authenticate(struct tb_switch *sw);
1164 int usb4_switch_nvm_authenticate_status(struct tb_switch *sw, u32 *status);
1165 int usb4_switch_credits_init(struct tb_switch *sw);
1166 bool usb4_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in);
1167 int usb4_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in);
1168 int usb4_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in);
1169 struct tb_port *usb4_switch_map_pcie_down(struct tb_switch *sw,
1170                       const struct tb_port *port);
1171 struct tb_port *usb4_switch_map_usb3_down(struct tb_switch *sw,
1172                       const struct tb_port *port);
1173 int usb4_switch_add_ports(struct tb_switch *sw);
1174 void usb4_switch_remove_ports(struct tb_switch *sw);
1175 
1176 int usb4_port_unlock(struct tb_port *port);
1177 int usb4_port_configure(struct tb_port *port);
1178 void usb4_port_unconfigure(struct tb_port *port);
1179 int usb4_port_configure_xdomain(struct tb_port *port);
1180 void usb4_port_unconfigure_xdomain(struct tb_port *port);
1181 int usb4_port_router_offline(struct tb_port *port);
1182 int usb4_port_router_online(struct tb_port *port);
1183 int usb4_port_enumerate_retimers(struct tb_port *port);
1184 bool usb4_port_clx_supported(struct tb_port *port);
1185 
1186 int usb4_port_retimer_set_inbound_sbtx(struct tb_port *port, u8 index);
1187 int usb4_port_retimer_read(struct tb_port *port, u8 index, u8 reg, void *buf,
1188                u8 size);
1189 int usb4_port_retimer_write(struct tb_port *port, u8 index, u8 reg,
1190                 const void *buf, u8 size);
1191 int usb4_port_retimer_is_last(struct tb_port *port, u8 index);
1192 int usb4_port_retimer_nvm_sector_size(struct tb_port *port, u8 index);
1193 int usb4_port_retimer_nvm_set_offset(struct tb_port *port, u8 index,
1194                      unsigned int address);
1195 int usb4_port_retimer_nvm_write(struct tb_port *port, u8 index,
1196                 unsigned int address, const void *buf,
1197                 size_t size);
1198 int usb4_port_retimer_nvm_authenticate(struct tb_port *port, u8 index);
1199 int usb4_port_retimer_nvm_authenticate_status(struct tb_port *port, u8 index,
1200                           u32 *status);
1201 int usb4_port_retimer_nvm_read(struct tb_port *port, u8 index,
1202                    unsigned int address, void *buf, size_t size);
1203 
1204 int usb4_usb3_port_max_link_rate(struct tb_port *port);
1205 int usb4_usb3_port_actual_link_rate(struct tb_port *port);
1206 int usb4_usb3_port_allocated_bandwidth(struct tb_port *port, int *upstream_bw,
1207                        int *downstream_bw);
1208 int usb4_usb3_port_allocate_bandwidth(struct tb_port *port, int *upstream_bw,
1209                       int *downstream_bw);
1210 int usb4_usb3_port_release_bandwidth(struct tb_port *port, int *upstream_bw,
1211                      int *downstream_bw);
1212 
1213 static inline bool tb_is_usb4_port_device(const struct device *dev)
1214 {
1215     return dev->type == &usb4_port_device_type;
1216 }
1217 
1218 static inline struct usb4_port *tb_to_usb4_port_device(struct device *dev)
1219 {
1220     if (tb_is_usb4_port_device(dev))
1221         return container_of(dev, struct usb4_port, dev);
1222     return NULL;
1223 }
1224 
1225 struct usb4_port *usb4_port_device_add(struct tb_port *port);
1226 void usb4_port_device_remove(struct usb4_port *usb4);
1227 int usb4_port_device_resume(struct usb4_port *usb4);
1228 
1229 /* Keep link controller awake during update */
1230 #define QUIRK_FORCE_POWER_LINK_CONTROLLER       BIT(0)
1231 
1232 void tb_check_quirks(struct tb_switch *sw);
1233 
1234 #ifdef CONFIG_ACPI
1235 void tb_acpi_add_links(struct tb_nhi *nhi);
1236 
1237 bool tb_acpi_is_native(void);
1238 bool tb_acpi_may_tunnel_usb3(void);
1239 bool tb_acpi_may_tunnel_dp(void);
1240 bool tb_acpi_may_tunnel_pcie(void);
1241 bool tb_acpi_is_xdomain_allowed(void);
1242 
1243 int tb_acpi_init(void);
1244 void tb_acpi_exit(void);
1245 int tb_acpi_power_on_retimers(struct tb_port *port);
1246 int tb_acpi_power_off_retimers(struct tb_port *port);
1247 #else
1248 static inline void tb_acpi_add_links(struct tb_nhi *nhi) { }
1249 
1250 static inline bool tb_acpi_is_native(void) { return true; }
1251 static inline bool tb_acpi_may_tunnel_usb3(void) { return true; }
1252 static inline bool tb_acpi_may_tunnel_dp(void) { return true; }
1253 static inline bool tb_acpi_may_tunnel_pcie(void) { return true; }
1254 static inline bool tb_acpi_is_xdomain_allowed(void) { return true; }
1255 
1256 static inline int tb_acpi_init(void) { return 0; }
1257 static inline void tb_acpi_exit(void) { }
1258 static inline int tb_acpi_power_on_retimers(struct tb_port *port) { return 0; }
1259 static inline int tb_acpi_power_off_retimers(struct tb_port *port) { return 0; }
1260 #endif
1261 
1262 #ifdef CONFIG_DEBUG_FS
1263 void tb_debugfs_init(void);
1264 void tb_debugfs_exit(void);
1265 void tb_switch_debugfs_init(struct tb_switch *sw);
1266 void tb_switch_debugfs_remove(struct tb_switch *sw);
1267 void tb_service_debugfs_init(struct tb_service *svc);
1268 void tb_service_debugfs_remove(struct tb_service *svc);
1269 #else
1270 static inline void tb_debugfs_init(void) { }
1271 static inline void tb_debugfs_exit(void) { }
1272 static inline void tb_switch_debugfs_init(struct tb_switch *sw) { }
1273 static inline void tb_switch_debugfs_remove(struct tb_switch *sw) { }
1274 static inline void tb_service_debugfs_init(struct tb_service *svc) { }
1275 static inline void tb_service_debugfs_remove(struct tb_service *svc) { }
1276 #endif
1277 
1278 #endif