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