tb.h 31.4 KB
Newer Older
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 10 11
 */

#ifndef TB_H_
#define TB_H_

12
#include <linux/nvmem-provider.h>
13
#include <linux/pci.h>
14
#include <linux/thunderbolt.h>
15
#include <linux/uuid.h>
16 17

#include "tb_regs.h"
18
#include "ctl.h"
19
#include "dma_port.h"
20

21 22 23 24 25 26 27 28
#define NVM_MIN_SIZE		SZ_32K
#define NVM_MAX_SIZE		SZ_512K

/* Intel specific NVM offsets */
#define NVM_DEVID		0x05
#define NVM_VERSION		0x08
#define NVM_FLASH_SIZE		0x45

29
/**
30 31
 * struct tb_nvm - Structure holding NVM information
 * @dev: Owner of the NVM
32 33 34 35 36 37 38 39 40
 * @major: Major version number of the active NVM portion
 * @minor: Minor version number of the active NVM portion
 * @id: Identifier used with both NVM portions
 * @active: Active portion NVMem device
 * @non_active: Non-active portion NVMem device
 * @buf: Buffer where the NVM image is stored before it is written to
 *	 the actual NVM flash device
 * @buf_data_size: Number of bytes actually consumed by the new NVM
 *		   image
41
 * @authenticating: The device is authenticating the new NVM
42
 * @flushed: The image has been flushed to the storage area
43 44 45
 *
 * The user of this structure needs to handle serialization of possible
 * concurrent access.
46
 */
47 48
struct tb_nvm {
	struct device *dev;
49 50 51 52 53 54 55 56
	u8 major;
	u8 minor;
	int id;
	struct nvmem_device *active;
	struct nvmem_device *non_active;
	void *buf;
	size_t buf_data_size;
	bool authenticating;
57
	bool flushed;
58 59
};

60
#define TB_SWITCH_KEY_SIZE		32
61
#define TB_SWITCH_MAX_DEPTH		6
62
#define USB4_SWITCH_MAX_DEPTH		5
63

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
/**
 * enum tb_switch_tmu_rate - TMU refresh rate
 * @TB_SWITCH_TMU_RATE_OFF: %0 (Disable Time Sync handshake)
 * @TB_SWITCH_TMU_RATE_HIFI: %16 us time interval between successive
 *			     transmission of the Delay Request TSNOS
 *			     (Time Sync Notification Ordered Set) on a Link
 * @TB_SWITCH_TMU_RATE_NORMAL: %1 ms time interval between successive
 *			       transmission of the Delay Request TSNOS on
 *			       a Link
 */
enum tb_switch_tmu_rate {
	TB_SWITCH_TMU_RATE_OFF = 0,
	TB_SWITCH_TMU_RATE_HIFI = 16,
	TB_SWITCH_TMU_RATE_NORMAL = 1000,
};

/**
 * struct tb_switch_tmu - Structure holding switch TMU configuration
 * @cap: Offset to the TMU capability (%0 if not found)
 * @has_ucap: Does the switch support uni-directional mode
 * @rate: TMU refresh rate related to upstream switch. In case of root
 *	  switch this holds the domain rate.
 * @unidirectional: Is the TMU in uni-directional or bi-directional mode
 *		    related to upstream switch. Don't case for root switch.
 */
struct tb_switch_tmu {
	int cap;
	bool has_ucap;
	enum tb_switch_tmu_rate rate;
	bool unidirectional;
};

96 97
/**
 * struct tb_switch - a thunderbolt switch
98 99 100
 * @dev: Device for the switch
 * @config: Switch configuration
 * @ports: Ports in this switch
101 102
 * @dma_port: If the switch has port supporting DMA configuration based
 *	      mailbox this will hold the pointer to that (%NULL
103 104
 *	      otherwise). If set it also means the switch has
 *	      upgradeable NVM.
105
 * @tmu: The switch TMU configuration
106 107 108 109 110
 * @tb: Pointer to the domain the switch belongs to
 * @uid: Unique ID of the switch
 * @uuid: UUID of the switch (or %NULL if not supported)
 * @vendor: Vendor ID of the switch
 * @device: Device ID of the switch
111 112
 * @vendor_name: Name of the vendor (or %NULL if not known)
 * @device_name: Name of the device (or %NULL if not known)
113 114
 * @link_speed: Speed of the link in Gb/s
 * @link_width: Width of the link (1 or 2)
115
 * @link_usb4: Upstream link is USB4
116
 * @generation: Switch Thunderbolt generation
117
 * @cap_plug_events: Offset to the plug events capability (%0 if not found)
118
 * @cap_lc: Offset to the link controller capability (%0 if not found)
119 120
 * @is_unplugged: The switch is going away
 * @drom: DROM of the switch (%NULL if not found)
121 122 123
 * @nvm: Pointer to the NVM if the switch has one (%NULL otherwise)
 * @no_nvm_upgrade: Prevent NVM upgrade of this switch
 * @safe_mode: The switch is in safe-mode
124
 * @boot: Whether the switch was already authorized on boot or not
125
 * @rpm: The switch supports runtime PM
126 127 128 129 130 131 132 133
 * @authorized: Whether the switch is authorized by user or policy
 * @security_level: Switch supported security level
 * @key: Contains the key used to challenge the device or %NULL if not
 *	 supported. Size of the key is %TB_SWITCH_KEY_SIZE.
 * @connection_id: Connection ID used with ICM messaging
 * @connection_key: Connection key used with ICM messaging
 * @link: Root switch link this switch is connected (ICM only)
 * @depth: Depth in the chain this switch is connected (ICM only)
134 135
 * @rpm_complete: Completion used to wait for runtime resume to
 *		  complete (ICM only)
136
 * @quirks: Quirks used for this Thunderbolt switch
137 138
 *
 * When the switch is being added or removed to the domain (other
139
 * switches) you need to have domain lock held.
140 141
 */
struct tb_switch {
142
	struct device dev;
143 144
	struct tb_regs_switch_header config;
	struct tb_port *ports;
145
	struct tb_dma_port *dma_port;
146
	struct tb_switch_tmu tmu;
147
	struct tb *tb;
148
	u64 uid;
149
	uuid_t *uuid;
150 151
	u16 vendor;
	u16 device;
152 153
	const char *vendor_name;
	const char *device_name;
154 155
	unsigned int link_speed;
	unsigned int link_width;
156
	bool link_usb4;
157
	unsigned int generation;
158
	int cap_plug_events;
159
	int cap_lc;
160
	bool is_unplugged;
161
	u8 *drom;
162
	struct tb_nvm *nvm;
163 164
	bool no_nvm_upgrade;
	bool safe_mode;
165
	bool boot;
166
	bool rpm;
167 168 169 170 171 172 173
	unsigned int authorized;
	enum tb_security_level security_level;
	u8 *key;
	u8 connection_id;
	u8 connection_key;
	u8 link;
	u8 depth;
174
	struct completion rpm_complete;
175
	unsigned long quirks;
176 177 178 179
};

/**
 * struct tb_port - a thunderbolt port, part of a tb_switch
180 181 182 183 184
 * @config: Cached port configuration read from registers
 * @sw: Switch the port belongs to
 * @remote: Remote port (%NULL if not connected)
 * @xdomain: Remote host (%NULL if not connected)
 * @cap_phy: Offset, zero if not found
185
 * @cap_tmu: Offset of the adapter specific TMU capability (%0 if not present)
186
 * @cap_adap: Offset of the adapter specific capability (%0 if not present)
187
 * @cap_usb4: Offset to the USB4 port capability (%0 if not present)
188
 * @port: Port number on switch
189
 * @disabled: Disabled by eeprom or enabled but not implemented
190
 * @bonded: true if the port is bonded (two lanes combined as one)
191 192 193
 * @dual_link_port: If the switch is connected using two ports, points
 *		    to the other port.
 * @link_nr: Is this primary or secondary port on the dual_link.
194 195
 * @in_hopids: Currently allocated input HopIDs
 * @out_hopids: Currently allocated output HopIDs
196
 * @list: Used to link ports to DP resources list
197 198 199 200
 */
struct tb_port {
	struct tb_regs_port_header config;
	struct tb_switch *sw;
201 202 203
	struct tb_port *remote;
	struct tb_xdomain *xdomain;
	int cap_phy;
204
	int cap_tmu;
205
	int cap_adap;
206
	int cap_usb4;
207 208
	u8 port;
	bool disabled;
209
	bool bonded;
210 211
	struct tb_port *dual_link_port;
	u8 link_nr:1;
212 213
	struct ida in_hopids;
	struct ida out_hopids;
214
	struct list_head list;
215 216
};

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
/**
 * tb_retimer: Thunderbolt retimer
 * @dev: Device for the retimer
 * @tb: Pointer to the domain the retimer belongs to
 * @index: Retimer index facing the router USB4 port
 * @vendor: Vendor ID of the retimer
 * @device: Device ID of the retimer
 * @port: Pointer to the lane 0 adapter
 * @nvm: Pointer to the NVM if the retimer has one (%NULL otherwise)
 * @auth_status: Status of last NVM authentication
 */
struct tb_retimer {
	struct device dev;
	struct tb *tb;
	u8 index;
	u32 vendor;
	u32 device;
	struct tb_port *port;
	struct tb_nvm *nvm;
	u32 auth_status;
};

239 240
/**
 * struct tb_path_hop - routing information for a tb_path
241 242 243 244 245 246 247 248
 * @in_port: Ingress port of a switch
 * @out_port: Egress port of a switch where the packet is routed out
 *	      (must be on the same switch than @in_port)
 * @in_hop_index: HopID where the path configuration entry is placed in
 *		  the path config space of @in_port.
 * @in_counter_index: Used counter index (not used in the driver
 *		      currently, %-1 to disable)
 * @next_hop_index: HopID of the packet when it is routed out from @out_port
249 250
 * @initial_credits: Number of initial flow control credits allocated for
 *		     the path
251 252 253 254
 *
 * Hop configuration is always done on the IN port of a switch.
 * in_port and out_port have to be on the same switch. Packets arriving on
 * in_port with "hop" = in_hop_index will get routed to through out_port. The
255 256 257 258
 * next hop to take (on out_port->remote) is determined by
 * next_hop_index. When routing packet to another switch (out->remote is
 * set) the @next_hop_index must match the @in_hop_index of that next
 * hop to make routing possible.
259 260 261 262 263 264 265 266
 *
 * in_counter_index is the index of a counter (in TB_CFG_COUNTERS) on the in
 * port.
 */
struct tb_path_hop {
	struct tb_port *in_port;
	struct tb_port *out_port;
	int in_hop_index;
267
	int in_counter_index;
268
	int next_hop_index;
269
	unsigned int initial_credits;
270 271 272 273
};

/**
 * enum tb_path_port - path options mask
274 275 276 277 278
 * @TB_PATH_NONE: Do not activate on any hop on path
 * @TB_PATH_SOURCE: Activate on the first hop (out of src)
 * @TB_PATH_INTERNAL: Activate on the intermediate hops (not the first/last)
 * @TB_PATH_DESTINATION: Activate on the last hop (into dst)
 * @TB_PATH_ALL: Activate on all hops on the path
279 280 281
 */
enum tb_path_port {
	TB_PATH_NONE = 0,
282 283 284
	TB_PATH_SOURCE = 1,
	TB_PATH_INTERNAL = 2,
	TB_PATH_DESTINATION = 4,
285 286 287 288 289
	TB_PATH_ALL = 7,
};

/**
 * struct tb_path - a unidirectional path between two ports
290 291 292 293 294 295 296 297 298 299 300
 * @tb: Pointer to the domain structure
 * @name: Name of the path (used for debugging)
 * @nfc_credits: Number of non flow controlled credits allocated for the path
 * @ingress_shared_buffer: Shared buffering used for ingress ports on the path
 * @egress_shared_buffer: Shared buffering used for egress ports on the path
 * @ingress_fc_enable: Flow control for ingress ports on the path
 * @egress_fc_enable: Flow control for egress ports on the path
 * @priority: Priority group if the path
 * @weight: Weight of the path inside the priority group
 * @drop_packages: Drop packages from queue tail or head
 * @activated: Is the path active
301 302
 * @clear_fc: Clear all flow control from the path config space entries
 *	      when deactivating this path
303 304
 * @hops: Path hops
 * @path_length: How many hops the path uses
305
 *
306 307 308
 * A path consists of a number of hops (see &struct tb_path_hop). To
 * establish a PCIe tunnel two paths have to be created between the two
 * PCIe ports.
309 310 311
 */
struct tb_path {
	struct tb *tb;
312 313
	const char *name;
	int nfc_credits;
314 315 316 317 318
	enum tb_path_port ingress_shared_buffer;
	enum tb_path_port egress_shared_buffer;
	enum tb_path_port ingress_fc_enable;
	enum tb_path_port egress_fc_enable;

319
	unsigned int priority:3;
320 321 322
	int weight:4;
	bool drop_packages;
	bool activated;
323
	bool clear_fc;
324
	struct tb_path_hop *hops;
325
	int path_length;
326 327
};

328 329
/* HopIDs 0-7 are reserved by the Thunderbolt protocol */
#define TB_PATH_MIN_HOPID	8
330 331 332 333 334
/*
 * Support paths from the farthest (depth 6) router to the host and back
 * to the same level (not necessarily to the same router).
 */
#define TB_PATH_MAX_HOPS	(7 * 2)
335

336 337
/**
 * struct tb_cm_ops - Connection manager specific operations vector
338 339
 * @driver_ready: Called right after control channel is started. Used by
 *		  ICM to send driver ready message to the firmware.
340 341 342 343
 * @start: Starts the domain
 * @stop: Stops the domain
 * @suspend_noirq: Connection manager specific suspend_noirq
 * @resume_noirq: Connection manager specific resume_noirq
344 345
 * @suspend: Connection manager specific suspend
 * @complete: Connection manager specific complete
346 347
 * @runtime_suspend: Connection manager specific runtime_suspend
 * @runtime_resume: Connection manager specific runtime_resume
348 349
 * @runtime_suspend_switch: Runtime suspend a switch
 * @runtime_resume_switch: Runtime resume a switch
350
 * @handle_event: Handle thunderbolt event
351 352
 * @get_boot_acl: Get boot ACL list
 * @set_boot_acl: Set boot ACL list
353 354 355
 * @approve_switch: Approve switch
 * @add_switch_key: Add key to switch
 * @challenge_switch_key: Challenge switch using key
356
 * @disconnect_pcie_paths: Disconnects PCIe paths before NVM update
357 358
 * @approve_xdomain_paths: Approve (establish) XDomain DMA paths
 * @disconnect_xdomain_paths: Disconnect XDomain DMA paths
359 360
 */
struct tb_cm_ops {
361
	int (*driver_ready)(struct tb *tb);
362 363 364 365
	int (*start)(struct tb *tb);
	void (*stop)(struct tb *tb);
	int (*suspend_noirq)(struct tb *tb);
	int (*resume_noirq)(struct tb *tb);
366 367
	int (*suspend)(struct tb *tb);
	void (*complete)(struct tb *tb);
368 369
	int (*runtime_suspend)(struct tb *tb);
	int (*runtime_resume)(struct tb *tb);
370 371
	int (*runtime_suspend_switch)(struct tb_switch *sw);
	int (*runtime_resume_switch)(struct tb_switch *sw);
372 373
	void (*handle_event)(struct tb *tb, enum tb_cfg_pkg_type,
			     const void *buf, size_t size);
374 375
	int (*get_boot_acl)(struct tb *tb, uuid_t *uuids, size_t nuuids);
	int (*set_boot_acl)(struct tb *tb, const uuid_t *uuids, size_t nuuids);
376 377 378 379
	int (*approve_switch)(struct tb *tb, struct tb_switch *sw);
	int (*add_switch_key)(struct tb *tb, struct tb_switch *sw);
	int (*challenge_switch_key)(struct tb *tb, struct tb_switch *sw,
				    const u8 *challenge, u8 *response);
380
	int (*disconnect_pcie_paths)(struct tb *tb);
381 382
	int (*approve_xdomain_paths)(struct tb *tb, struct tb_xdomain *xd);
	int (*disconnect_xdomain_paths)(struct tb *tb, struct tb_xdomain *xd);
383
};
384

385 386 387 388 389
static inline void *tb_priv(struct tb *tb)
{
	return (void *)tb->privdata;
}

390 391
#define TB_AUTOSUSPEND_DELAY		15000 /* ms */

392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
/* helper functions & macros */

/**
 * tb_upstream_port() - return the upstream port of a switch
 *
 * Every switch has an upstream port (for the root switch it is the NHI).
 *
 * During switch alloc/init tb_upstream_port()->remote may be NULL, even for
 * non root switches (on the NHI port remote is always NULL).
 *
 * Return: Returns the upstream port of the switch.
 */
static inline struct tb_port *tb_upstream_port(struct tb_switch *sw)
{
	return &sw->ports[sw->config.upstream_port_number];
}

409 410 411 412 413 414 415 416 417 418 419 420 421
/**
 * tb_is_upstream_port() - Is the port upstream facing
 * @port: Port to check
 *
 * Returns true if @port is upstream facing port. In case of dual link
 * ports both return true.
 */
static inline bool tb_is_upstream_port(const struct tb_port *port)
{
	const struct tb_port *upstream_port = tb_upstream_port(port->sw);
	return port == upstream_port || port->dual_link_port == upstream_port;
}

422
static inline u64 tb_route(const struct tb_switch *sw)
423 424 425 426
{
	return ((u64) sw->config.route_hi) << 32 | sw->config.route_lo;
}

427 428 429 430 431 432 433 434 435 436
static inline struct tb_port *tb_port_at(u64 route, struct tb_switch *sw)
{
	u8 port;

	port = route >> (sw->config.depth * 8);
	if (WARN_ON(port > sw->config.max_port_number))
		return NULL;
	return &sw->ports[port];
}

437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
/**
 * tb_port_has_remote() - Does the port have switch connected downstream
 * @port: Port to check
 *
 * Returns true only when the port is primary port and has remote set.
 */
static inline bool tb_port_has_remote(const struct tb_port *port)
{
	if (tb_is_upstream_port(port))
		return false;
	if (!port->remote)
		return false;
	if (port->dual_link_port && port->link_nr)
		return false;

	return true;
}

455 456 457 458 459
static inline bool tb_port_is_null(const struct tb_port *port)
{
	return port && port->port && port->config.type == TB_TYPE_PORT;
}

460 461 462 463 464
static inline bool tb_port_is_pcie_down(const struct tb_port *port)
{
	return port && port->config.type == TB_TYPE_PCIE_DOWN;
}

465 466 467 468 469
static inline bool tb_port_is_pcie_up(const struct tb_port *port)
{
	return port && port->config.type == TB_TYPE_PCIE_UP;
}

470 471 472 473 474 475 476 477 478 479
static inline bool tb_port_is_dpin(const struct tb_port *port)
{
	return port && port->config.type == TB_TYPE_DP_HDMI_IN;
}

static inline bool tb_port_is_dpout(const struct tb_port *port)
{
	return port && port->config.type == TB_TYPE_DP_HDMI_OUT;
}

480 481 482 483 484 485 486 487 488 489
static inline bool tb_port_is_usb3_down(const struct tb_port *port)
{
	return port && port->config.type == TB_TYPE_USB3_DOWN;
}

static inline bool tb_port_is_usb3_up(const struct tb_port *port)
{
	return port && port->config.type == TB_TYPE_USB3_UP;
}

490 491 492
static inline int tb_sw_read(struct tb_switch *sw, void *buffer,
			     enum tb_cfg_space space, u32 offset, u32 length)
{
493 494
	if (sw->is_unplugged)
		return -ENODEV;
495 496 497 498 499 500 501 502 503
	return tb_cfg_read(sw->tb->ctl,
			   buffer,
			   tb_route(sw),
			   0,
			   space,
			   offset,
			   length);
}

504
static inline int tb_sw_write(struct tb_switch *sw, const void *buffer,
505 506
			      enum tb_cfg_space space, u32 offset, u32 length)
{
507 508
	if (sw->is_unplugged)
		return -ENODEV;
509 510 511 512 513 514 515 516 517 518 519 520
	return tb_cfg_write(sw->tb->ctl,
			    buffer,
			    tb_route(sw),
			    0,
			    space,
			    offset,
			    length);
}

static inline int tb_port_read(struct tb_port *port, void *buffer,
			       enum tb_cfg_space space, u32 offset, u32 length)
{
521 522
	if (port->sw->is_unplugged)
		return -ENODEV;
523 524 525 526 527 528 529 530 531
	return tb_cfg_read(port->sw->tb->ctl,
			   buffer,
			   tb_route(port->sw),
			   port->port,
			   space,
			   offset,
			   length);
}

532
static inline int tb_port_write(struct tb_port *port, const void *buffer,
533 534
				enum tb_cfg_space space, u32 offset, u32 length)
{
535 536
	if (port->sw->is_unplugged)
		return -ENODEV;
537 538 539 540 541 542 543 544 545 546 547 548 549
	return tb_cfg_write(port->sw->tb->ctl,
			    buffer,
			    tb_route(port->sw),
			    port->port,
			    space,
			    offset,
			    length);
}

#define tb_err(tb, fmt, arg...) dev_err(&(tb)->nhi->pdev->dev, fmt, ## arg)
#define tb_WARN(tb, fmt, arg...) dev_WARN(&(tb)->nhi->pdev->dev, fmt, ## arg)
#define tb_warn(tb, fmt, arg...) dev_warn(&(tb)->nhi->pdev->dev, fmt, ## arg)
#define tb_info(tb, fmt, arg...) dev_info(&(tb)->nhi->pdev->dev, fmt, ## arg)
550
#define tb_dbg(tb, fmt, arg...) dev_dbg(&(tb)->nhi->pdev->dev, fmt, ## arg)
551 552 553

#define __TB_SW_PRINT(level, sw, fmt, arg...)           \
	do {                                            \
554
		const struct tb_switch *__sw = (sw);    \
555 556 557 558 559 560
		level(__sw->tb, "%llx: " fmt,           \
		      tb_route(__sw), ## arg);          \
	} while (0)
#define tb_sw_WARN(sw, fmt, arg...) __TB_SW_PRINT(tb_WARN, sw, fmt, ##arg)
#define tb_sw_warn(sw, fmt, arg...) __TB_SW_PRINT(tb_warn, sw, fmt, ##arg)
#define tb_sw_info(sw, fmt, arg...) __TB_SW_PRINT(tb_info, sw, fmt, ##arg)
561
#define tb_sw_dbg(sw, fmt, arg...) __TB_SW_PRINT(tb_dbg, sw, fmt, ##arg)
562 563 564

#define __TB_PORT_PRINT(level, _port, fmt, arg...)                      \
	do {                                                            \
565
		const struct tb_port *__port = (_port);                 \
566 567 568 569 570 571 572 573 574
		level(__port->sw->tb, "%llx:%x: " fmt,                  \
		      tb_route(__port->sw), __port->port, ## arg);      \
	} while (0)
#define tb_port_WARN(port, fmt, arg...) \
	__TB_PORT_PRINT(tb_WARN, port, fmt, ##arg)
#define tb_port_warn(port, fmt, arg...) \
	__TB_PORT_PRINT(tb_warn, port, fmt, ##arg)
#define tb_port_info(port, fmt, arg...) \
	__TB_PORT_PRINT(tb_info, port, fmt, ##arg)
575 576
#define tb_port_dbg(port, fmt, arg...) \
	__TB_PORT_PRINT(tb_dbg, port, fmt, ##arg)
577

578
struct tb *icm_probe(struct tb_nhi *nhi);
579 580 581
struct tb *tb_probe(struct tb_nhi *nhi);

extern struct device_type tb_domain_type;
582
extern struct device_type tb_retimer_type;
583
extern struct device_type tb_switch_type;
584 585 586

int tb_domain_init(void);
void tb_domain_exit(void);
587 588
int tb_xdomain_init(void);
void tb_xdomain_exit(void);
589

590 591 592 593 594
struct tb *tb_domain_alloc(struct tb_nhi *nhi, size_t privsize);
int tb_domain_add(struct tb *tb);
void tb_domain_remove(struct tb *tb);
int tb_domain_suspend_noirq(struct tb *tb);
int tb_domain_resume_noirq(struct tb *tb);
595 596
int tb_domain_suspend(struct tb *tb);
void tb_domain_complete(struct tb *tb);
597 598
int tb_domain_runtime_suspend(struct tb *tb);
int tb_domain_runtime_resume(struct tb *tb);
599 600 601
int tb_domain_approve_switch(struct tb *tb, struct tb_switch *sw);
int tb_domain_approve_switch_key(struct tb *tb, struct tb_switch *sw);
int tb_domain_challenge_switch_key(struct tb *tb, struct tb_switch *sw);
602
int tb_domain_disconnect_pcie_paths(struct tb *tb);
603 604 605
int tb_domain_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd);
int tb_domain_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd);
int tb_domain_disconnect_all_paths(struct tb *tb);
606

607 608 609 610 611 612 613
static inline struct tb *tb_domain_get(struct tb *tb)
{
	if (tb)
		get_device(&tb->dev);
	return tb;
}

614 615 616 617
static inline void tb_domain_put(struct tb *tb)
{
	put_device(&tb->dev);
}
618

619 620 621 622 623 624 625 626 627
struct tb_nvm *tb_nvm_alloc(struct device *dev);
int tb_nvm_add_active(struct tb_nvm *nvm, size_t size, nvmem_reg_read_t reg_read);
int tb_nvm_write_buf(struct tb_nvm *nvm, unsigned int offset, void *val,
		     size_t bytes);
int tb_nvm_add_non_active(struct tb_nvm *nvm, size_t size,
			  nvmem_reg_write_t reg_write);
void tb_nvm_free(struct tb_nvm *nvm);
void tb_nvm_exit(void);

628 629
struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent,
				  u64 route);
630 631
struct tb_switch *tb_switch_alloc_safe_mode(struct tb *tb,
			struct device *parent, u64 route);
632 633 634
int tb_switch_configure(struct tb_switch *sw);
int tb_switch_add(struct tb_switch *sw);
void tb_switch_remove(struct tb_switch *sw);
635 636 637
void tb_switch_suspend(struct tb_switch *sw);
int tb_switch_resume(struct tb_switch *sw);
int tb_switch_reset(struct tb *tb, u64 route);
638
void tb_sw_set_unplugged(struct tb_switch *sw);
639 640
struct tb_port *tb_switch_find_port(struct tb_switch *sw,
				    enum tb_port_type type);
641 642
struct tb_switch *tb_switch_find_by_link_depth(struct tb *tb, u8 link,
					       u8 depth);
643
struct tb_switch *tb_switch_find_by_uuid(struct tb *tb, const uuid_t *uuid);
644
struct tb_switch *tb_switch_find_by_route(struct tb *tb, u64 route);
645

646 647 648 649 650 651 652 653 654 655 656
/**
 * tb_switch_for_each_port() - Iterate over each switch port
 * @sw: Switch whose ports to iterate
 * @p: Port used as iterator
 *
 * Iterates over each switch port skipping the control port (port %0).
 */
#define tb_switch_for_each_port(sw, p)					\
	for ((p) = &(sw)->ports[1];					\
	     (p) <= &(sw)->ports[(sw)->config.max_port_number]; (p)++)

657 658 659 660 661 662 663
static inline struct tb_switch *tb_switch_get(struct tb_switch *sw)
{
	if (sw)
		get_device(&sw->dev);
	return sw;
}

664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
static inline void tb_switch_put(struct tb_switch *sw)
{
	put_device(&sw->dev);
}

static inline bool tb_is_switch(const struct device *dev)
{
	return dev->type == &tb_switch_type;
}

static inline struct tb_switch *tb_to_switch(struct device *dev)
{
	if (tb_is_switch(dev))
		return container_of(dev, struct tb_switch, dev);
	return NULL;
}

681 682 683 684 685
static inline struct tb_switch *tb_switch_parent(struct tb_switch *sw)
{
	return tb_to_switch(sw->dev.parent);
}

686
static inline bool tb_switch_is_light_ridge(const struct tb_switch *sw)
687 688 689 690
{
	return sw->config.device_id == PCI_DEVICE_ID_INTEL_LIGHT_RIDGE;
}

691
static inline bool tb_switch_is_eagle_ridge(const struct tb_switch *sw)
692 693 694 695
{
	return sw->config.device_id == PCI_DEVICE_ID_INTEL_EAGLE_RIDGE;
}

696
static inline bool tb_switch_is_cactus_ridge(const struct tb_switch *sw)
697 698 699 700 701 702 703 704 705 706
{
	switch (sw->config.device_id) {
	case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C:
	case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
		return true;
	default:
		return false;
	}
}

707
static inline bool tb_switch_is_falcon_ridge(const struct tb_switch *sw)
708 709 710 711 712 713 714 715 716 717
{
	switch (sw->config.device_id) {
	case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE:
	case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE:
		return true;
	default:
		return false;
	}
}

718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
static inline bool tb_switch_is_alpine_ridge(const struct tb_switch *sw)
{
	switch (sw->config.device_id) {
	case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
	case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
	case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
	case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
		return true;
	default:
		return false;
	}
}

static inline bool tb_switch_is_titan_ridge(const struct tb_switch *sw)
{
	switch (sw->config.device_id) {
	case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE:
	case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE:
	case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_DD_BRIDGE:
		return true;
	default:
		return false;
	}
}

743 744 745 746 747 748 749 750 751 752 753
/**
 * tb_switch_is_usb4() - Is the switch USB4 compliant
 * @sw: Switch to check
 *
 * Returns true if the @sw is USB4 compliant router, false otherwise.
 */
static inline bool tb_switch_is_usb4(const struct tb_switch *sw)
{
	return sw->config.thunderbolt_version == USB4_VERSION_1_0;
}

754 755 756 757 758 759 760 761 762 763 764 765 766 767
/**
 * tb_switch_is_icm() - Is the switch handled by ICM firmware
 * @sw: Switch to check
 *
 * In case there is a need to differentiate whether ICM firmware or SW CM
 * is handling @sw this function can be called. It is valid to call this
 * after tb_switch_alloc() and tb_switch_configure() has been called
 * (latter only for SW CM case).
 */
static inline bool tb_switch_is_icm(const struct tb_switch *sw)
{
	return !sw->config.enabled;
}

768 769 770
int tb_switch_lane_bonding_enable(struct tb_switch *sw);
void tb_switch_lane_bonding_disable(struct tb_switch *sw);

771 772 773 774
bool tb_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in);
int tb_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in);
void tb_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in);

775 776 777 778 779 780 781 782 783 784 785
int tb_switch_tmu_init(struct tb_switch *sw);
int tb_switch_tmu_post_time(struct tb_switch *sw);
int tb_switch_tmu_disable(struct tb_switch *sw);
int tb_switch_tmu_enable(struct tb_switch *sw);

static inline bool tb_switch_tmu_is_enabled(const struct tb_switch *sw)
{
	return sw->tmu.rate == TB_SWITCH_TMU_RATE_HIFI &&
	       !sw->tmu.unidirectional;
}

786
int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged);
787
int tb_port_add_nfc_credits(struct tb_port *port, int credits);
788
int tb_port_set_initial_credits(struct tb_port *port, u32 credits);
789
int tb_port_clear_counter(struct tb_port *port, int counter);
790
int tb_port_unlock(struct tb_port *port);
791 792 793 794
int tb_port_alloc_in_hopid(struct tb_port *port, int hopid, int max_hopid);
void tb_port_release_in_hopid(struct tb_port *port, int hopid);
int tb_port_alloc_out_hopid(struct tb_port *port, int hopid, int max_hopid);
void tb_port_release_out_hopid(struct tb_port *port, int hopid);
795 796
struct tb_port *tb_next_port_on_path(struct tb_port *start, struct tb_port *end,
				     struct tb_port *prev);
797

798 799 800 801 802 803 804 805 806 807 808 809
/**
 * tb_for_each_port_on_path() - Iterate over each port on path
 * @src: Source port
 * @dst: Destination port
 * @p: Port used as iterator
 *
 * Walks over each port on path from @src to @dst.
 */
#define tb_for_each_port_on_path(src, dst, p)				\
	for ((p) = tb_next_port_on_path((src), (dst), NULL); (p);	\
	     (p) = tb_next_port_on_path((src), (dst), (p)))

810 811
int tb_port_get_link_speed(struct tb_port *port);

812
int tb_switch_find_vse_cap(struct tb_switch *sw, enum tb_switch_vse_cap vsec);
813
int tb_switch_find_cap(struct tb_switch *sw, enum tb_switch_cap cap);
814
int tb_port_find_cap(struct tb_port *port, enum tb_port_cap cap);
815
bool tb_port_is_enabled(struct tb_port *port);
816

817 818 819
bool tb_usb3_port_is_enabled(struct tb_port *port);
int tb_usb3_port_enable(struct tb_port *port, bool enable);

820
bool tb_pci_port_is_enabled(struct tb_port *port);
821 822
int tb_pci_port_enable(struct tb_port *port, bool enable);

823 824 825 826 827 828 829
int tb_dp_port_hpd_is_active(struct tb_port *port);
int tb_dp_port_hpd_clear(struct tb_port *port);
int tb_dp_port_set_hops(struct tb_port *port, unsigned int video,
			unsigned int aux_tx, unsigned int aux_rx);
bool tb_dp_port_is_enabled(struct tb_port *port);
int tb_dp_port_enable(struct tb_port *port, bool enable);

830 831 832
struct tb_path *tb_path_discover(struct tb_port *src, int src_hopid,
				 struct tb_port *dst, int dst_hopid,
				 struct tb_port **last, const char *name);
833 834 835
struct tb_path *tb_path_alloc(struct tb *tb, struct tb_port *src, int src_hopid,
			      struct tb_port *dst, int dst_hopid, int link_nr,
			      const char *name);
836 837 838 839
void tb_path_free(struct tb_path *path);
int tb_path_activate(struct tb_path *path);
void tb_path_deactivate(struct tb_path *path);
bool tb_path_is_invalid(struct tb_path *path);
840 841
bool tb_path_port_on_path(const struct tb_path *path,
			  const struct tb_port *port);
842

843 844
int tb_drom_read(struct tb_switch *sw);
int tb_drom_read_uid_only(struct tb_switch *sw, u64 *uid);
845

846
int tb_lc_read_uuid(struct tb_switch *sw, u32 *uuid);
847 848
int tb_lc_configure_link(struct tb_switch *sw);
void tb_lc_unconfigure_link(struct tb_switch *sw);
849
int tb_lc_set_sleep(struct tb_switch *sw);
850
bool tb_lc_lane_bonding_possible(struct tb_switch *sw);
851 852 853
bool tb_lc_dp_sink_query(struct tb_switch *sw, struct tb_port *in);
int tb_lc_dp_sink_alloc(struct tb_switch *sw, struct tb_port *in);
int tb_lc_dp_sink_dealloc(struct tb_switch *sw, struct tb_port *in);
854
int tb_lc_force_power(struct tb_switch *sw);
855 856 857 858 859 860

static inline int tb_route_length(u64 route)
{
	return (fls64(route) + TB_ROUTE_SHIFT - 1) / TB_ROUTE_SHIFT;
}

861 862 863 864 865 866 867 868 869 870 871 872 873
/**
 * tb_downstream_route() - get route to downstream switch
 *
 * Port must not be the upstream port (otherwise a loop is created).
 *
 * Return: Returns a route to the switch behind @port.
 */
static inline u64 tb_downstream_route(struct tb_port *port)
{
	return tb_route(port->sw)
	       | ((u64) port->port << (port->sw->config.depth * 8));
}

874 875 876 877 878 879 880 881 882 883
bool tb_xdomain_handle_request(struct tb *tb, enum tb_cfg_pkg_type type,
			       const void *buf, size_t size);
struct tb_xdomain *tb_xdomain_alloc(struct tb *tb, struct device *parent,
				    u64 route, const uuid_t *local_uuid,
				    const uuid_t *remote_uuid);
void tb_xdomain_add(struct tb_xdomain *xd);
void tb_xdomain_remove(struct tb_xdomain *xd);
struct tb_xdomain *tb_xdomain_find_by_link_depth(struct tb *tb, u8 link,
						 u8 depth);

884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
int tb_retimer_scan(struct tb_port *port);
void tb_retimer_remove_all(struct tb_port *port);

static inline bool tb_is_retimer(const struct device *dev)
{
	return dev->type == &tb_retimer_type;
}

static inline struct tb_retimer *tb_to_retimer(struct device *dev)
{
	if (tb_is_retimer(dev))
		return container_of(dev, struct tb_retimer, dev);
	return NULL;
}

899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
int usb4_switch_setup(struct tb_switch *sw);
int usb4_switch_read_uid(struct tb_switch *sw, u64 *uid);
int usb4_switch_drom_read(struct tb_switch *sw, unsigned int address, void *buf,
			  size_t size);
int usb4_switch_configure_link(struct tb_switch *sw);
void usb4_switch_unconfigure_link(struct tb_switch *sw);
bool usb4_switch_lane_bonding_possible(struct tb_switch *sw);
int usb4_switch_set_sleep(struct tb_switch *sw);
int usb4_switch_nvm_sector_size(struct tb_switch *sw);
int usb4_switch_nvm_read(struct tb_switch *sw, unsigned int address, void *buf,
			 size_t size);
int usb4_switch_nvm_write(struct tb_switch *sw, unsigned int address,
			  const void *buf, size_t size);
int usb4_switch_nvm_authenticate(struct tb_switch *sw);
bool usb4_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in);
int usb4_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in);
int usb4_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in);
struct tb_port *usb4_switch_map_pcie_down(struct tb_switch *sw,
					  const struct tb_port *port);
918 919
struct tb_port *usb4_switch_map_usb3_down(struct tb_switch *sw,
					  const struct tb_port *port);
920 921

int usb4_port_unlock(struct tb_port *port);
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
int usb4_port_enumerate_retimers(struct tb_port *port);

int usb4_port_retimer_read(struct tb_port *port, u8 index, u8 reg, void *buf,
			   u8 size);
int usb4_port_retimer_write(struct tb_port *port, u8 index, u8 reg,
			    const void *buf, u8 size);
int usb4_port_retimer_is_last(struct tb_port *port, u8 index);
int usb4_port_retimer_nvm_sector_size(struct tb_port *port, u8 index);
int usb4_port_retimer_nvm_write(struct tb_port *port, u8 index,
				unsigned int address, const void *buf,
				size_t size);
int usb4_port_retimer_nvm_authenticate(struct tb_port *port, u8 index);
int usb4_port_retimer_nvm_authenticate_status(struct tb_port *port, u8 index,
					      u32 *status);
int usb4_port_retimer_nvm_read(struct tb_port *port, u8 index,
			       unsigned int address, void *buf, size_t size);
938 939 940 941 942 943 944 945 946

int usb4_usb3_port_max_link_rate(struct tb_port *port);
int usb4_usb3_port_actual_link_rate(struct tb_port *port);
int usb4_usb3_port_allocated_bandwidth(struct tb_port *port, int *upstream_bw,
				       int *downstream_bw);
int usb4_usb3_port_allocate_bandwidth(struct tb_port *port, int *upstream_bw,
				      int *downstream_bw);
int usb4_usb3_port_release_bandwidth(struct tb_port *port, int *upstream_bw,
				     int *downstream_bw);
947 948 949 950 951 952

/* keep link controller awake during update */
#define QUIRK_FORCE_POWER_LINK_CONTROLLER		BIT(0)

void tb_check_quirks(struct tb_switch *sw);

953
#endif