blob: e52d39b25266a53bfcdef715b3c5061664408cfa [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Andreas Noeverd6cc51c2014-06-03 22:04:00 +02002/*
Mika Westerberg15c67842018-10-01 12:31:22 +03003 * Thunderbolt driver - bus logic (NHI independent)
Andreas Noeverd6cc51c2014-06-03 22:04:00 +02004 *
5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
Mika Westerberg15c67842018-10-01 12:31:22 +03006 * Copyright (C) 2018, Intel Corporation
Andreas Noeverd6cc51c2014-06-03 22:04:00 +02007 */
8
9#ifndef TB_H_
10#define TB_H_
11
Mika Westerberge6b245c2017-06-06 15:25:17 +030012#include <linux/nvmem-provider.h>
Andreas Noevera25c8b22014-06-03 22:04:02 +020013#include <linux/pci.h>
Mika Westerbergd1ff7022017-10-02 13:38:34 +030014#include <linux/thunderbolt.h>
Mika Westerbergbfe778a2017-06-06 15:25:01 +030015#include <linux/uuid.h>
Andreas Noevera25c8b22014-06-03 22:04:02 +020016
17#include "tb_regs.h"
Andreas Noeverd6cc51c2014-06-03 22:04:00 +020018#include "ctl.h"
Mika Westerberg3e136762017-06-06 15:25:14 +030019#include "dma_port.h"
Andreas Noeverd6cc51c2014-06-03 22:04:00 +020020
21/**
Mika Westerberge6b245c2017-06-06 15:25:17 +030022 * struct tb_switch_nvm - Structure holding switch NVM information
23 * @major: Major version number of the active NVM portion
24 * @minor: Minor version number of the active NVM portion
25 * @id: Identifier used with both NVM portions
26 * @active: Active portion NVMem device
27 * @non_active: Non-active portion NVMem device
28 * @buf: Buffer where the NVM image is stored before it is written to
29 * the actual NVM flash device
30 * @buf_data_size: Number of bytes actually consumed by the new NVM
31 * image
32 * @authenticating: The switch is authenticating the new NVM
33 */
34struct tb_switch_nvm {
35 u8 major;
36 u8 minor;
37 int id;
38 struct nvmem_device *active;
39 struct nvmem_device *non_active;
40 void *buf;
41 size_t buf_data_size;
42 bool authenticating;
43};
44
Mika Westerbergf67cf492017-06-06 15:25:16 +030045#define TB_SWITCH_KEY_SIZE 32
Mika Westerbergf0342e72018-12-30 12:14:46 +020046#define TB_SWITCH_MAX_DEPTH 6
Mika Westerbergf67cf492017-06-06 15:25:16 +030047
48/**
Andreas Noevera25c8b22014-06-03 22:04:02 +020049 * struct tb_switch - a thunderbolt switch
Mika Westerbergbfe778a2017-06-06 15:25:01 +030050 * @dev: Device for the switch
51 * @config: Switch configuration
52 * @ports: Ports in this switch
Mika Westerberg3e136762017-06-06 15:25:14 +030053 * @dma_port: If the switch has port supporting DMA configuration based
54 * mailbox this will hold the pointer to that (%NULL
Mika Westerberge6b245c2017-06-06 15:25:17 +030055 * otherwise). If set it also means the switch has
56 * upgradeable NVM.
Mika Westerbergbfe778a2017-06-06 15:25:01 +030057 * @tb: Pointer to the domain the switch belongs to
58 * @uid: Unique ID of the switch
59 * @uuid: UUID of the switch (or %NULL if not supported)
60 * @vendor: Vendor ID of the switch
61 * @device: Device ID of the switch
Mika Westerberg72ee3392017-06-06 15:25:05 +030062 * @vendor_name: Name of the vendor (or %NULL if not known)
63 * @device_name: Name of the device (or %NULL if not known)
Mika Westerberg2c3c4192017-06-06 15:25:13 +030064 * @generation: Switch Thunderbolt generation
Mika Westerbergbfe778a2017-06-06 15:25:01 +030065 * @cap_plug_events: Offset to the plug events capability (%0 if not found)
Mika Westerberga9be5582019-01-09 16:42:12 +020066 * @cap_lc: Offset to the link controller capability (%0 if not found)
Mika Westerbergbfe778a2017-06-06 15:25:01 +030067 * @is_unplugged: The switch is going away
68 * @drom: DROM of the switch (%NULL if not found)
Mika Westerberge6b245c2017-06-06 15:25:17 +030069 * @nvm: Pointer to the NVM if the switch has one (%NULL otherwise)
70 * @no_nvm_upgrade: Prevent NVM upgrade of this switch
71 * @safe_mode: The switch is in safe-mode
Yehezkel Bernat14862ee2018-01-22 12:50:09 +020072 * @boot: Whether the switch was already authorized on boot or not
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +030073 * @rpm: The switch supports runtime PM
Mika Westerbergf67cf492017-06-06 15:25:16 +030074 * @authorized: Whether the switch is authorized by user or policy
Mika Westerbergf67cf492017-06-06 15:25:16 +030075 * @security_level: Switch supported security level
76 * @key: Contains the key used to challenge the device or %NULL if not
77 * supported. Size of the key is %TB_SWITCH_KEY_SIZE.
78 * @connection_id: Connection ID used with ICM messaging
79 * @connection_key: Connection key used with ICM messaging
80 * @link: Root switch link this switch is connected (ICM only)
81 * @depth: Depth in the chain this switch is connected (ICM only)
82 *
83 * When the switch is being added or removed to the domain (other
Mika Westerberg09f11b62019-03-19 16:48:41 +020084 * switches) you need to have domain lock held.
Andreas Noevera25c8b22014-06-03 22:04:02 +020085 */
86struct tb_switch {
Mika Westerbergbfe778a2017-06-06 15:25:01 +030087 struct device dev;
Andreas Noevera25c8b22014-06-03 22:04:02 +020088 struct tb_regs_switch_header config;
89 struct tb_port *ports;
Mika Westerberg3e136762017-06-06 15:25:14 +030090 struct tb_dma_port *dma_port;
Andreas Noevera25c8b22014-06-03 22:04:02 +020091 struct tb *tb;
Andreas Noeverc90553b2014-06-03 22:04:11 +020092 u64 uid;
Christoph Hellwig7c39ffe2017-07-18 15:30:05 +020093 uuid_t *uuid;
Mika Westerbergbfe778a2017-06-06 15:25:01 +030094 u16 vendor;
95 u16 device;
Mika Westerberg72ee3392017-06-06 15:25:05 +030096 const char *vendor_name;
97 const char *device_name;
Mika Westerberg2c3c4192017-06-06 15:25:13 +030098 unsigned int generation;
Mika Westerbergbfe778a2017-06-06 15:25:01 +030099 int cap_plug_events;
Mika Westerberga9be5582019-01-09 16:42:12 +0200100 int cap_lc;
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300101 bool is_unplugged;
Andreas Noevercd22e732014-06-12 23:11:46 +0200102 u8 *drom;
Mika Westerberge6b245c2017-06-06 15:25:17 +0300103 struct tb_switch_nvm *nvm;
104 bool no_nvm_upgrade;
105 bool safe_mode;
Yehezkel Bernat14862ee2018-01-22 12:50:09 +0200106 bool boot;
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300107 bool rpm;
Mika Westerbergf67cf492017-06-06 15:25:16 +0300108 unsigned int authorized;
Mika Westerbergf67cf492017-06-06 15:25:16 +0300109 enum tb_security_level security_level;
110 u8 *key;
111 u8 connection_id;
112 u8 connection_key;
113 u8 link;
114 u8 depth;
Andreas Noevera25c8b22014-06-03 22:04:02 +0200115};
116
117/**
118 * struct tb_port - a thunderbolt port, part of a tb_switch
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300119 * @config: Cached port configuration read from registers
120 * @sw: Switch the port belongs to
121 * @remote: Remote port (%NULL if not connected)
122 * @xdomain: Remote host (%NULL if not connected)
123 * @cap_phy: Offset, zero if not found
124 * @port: Port number on switch
125 * @disabled: Disabled by eeprom
126 * @dual_link_port: If the switch is connected using two ports, points
127 * to the other port.
128 * @link_nr: Is this primary or secondary port on the dual_link.
Andreas Noevera25c8b22014-06-03 22:04:02 +0200129 */
130struct tb_port {
131 struct tb_regs_port_header config;
132 struct tb_switch *sw;
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300133 struct tb_port *remote;
134 struct tb_xdomain *xdomain;
135 int cap_phy;
136 u8 port;
137 bool disabled;
Andreas Noevercd22e732014-06-12 23:11:46 +0200138 struct tb_port *dual_link_port;
139 u8 link_nr:1;
Andreas Noevera25c8b22014-06-03 22:04:02 +0200140};
141
142/**
Andreas Noever520b6702014-06-03 22:04:07 +0200143 * struct tb_path_hop - routing information for a tb_path
144 *
145 * Hop configuration is always done on the IN port of a switch.
146 * in_port and out_port have to be on the same switch. Packets arriving on
147 * in_port with "hop" = in_hop_index will get routed to through out_port. The
148 * next hop to take (on out_port->remote) is determined by next_hop_index.
149 *
150 * in_counter_index is the index of a counter (in TB_CFG_COUNTERS) on the in
151 * port.
152 */
153struct tb_path_hop {
154 struct tb_port *in_port;
155 struct tb_port *out_port;
156 int in_hop_index;
157 int in_counter_index; /* write -1 to disable counters for this hop. */
158 int next_hop_index;
159};
160
161/**
162 * enum tb_path_port - path options mask
163 */
164enum tb_path_port {
165 TB_PATH_NONE = 0,
166 TB_PATH_SOURCE = 1, /* activate on the first hop (out of src) */
167 TB_PATH_INTERNAL = 2, /* activate on other hops (not the first/last) */
168 TB_PATH_DESTINATION = 4, /* activate on the last hop (into dst) */
169 TB_PATH_ALL = 7,
170};
171
172/**
173 * struct tb_path - a unidirectional path between two ports
174 *
175 * A path consists of a number of hops (see tb_path_hop). To establish a PCIe
176 * tunnel two paths have to be created between the two PCIe ports.
177 *
178 */
179struct tb_path {
180 struct tb *tb;
181 int nfc_credits; /* non flow controlled credits */
182 enum tb_path_port ingress_shared_buffer;
183 enum tb_path_port egress_shared_buffer;
184 enum tb_path_port ingress_fc_enable;
185 enum tb_path_port egress_fc_enable;
186
187 int priority:3;
188 int weight:4;
189 bool drop_packages;
190 bool activated;
191 struct tb_path_hop *hops;
192 int path_length; /* number of hops */
193};
194
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300195/**
196 * struct tb_cm_ops - Connection manager specific operations vector
Mika Westerbergf67cf492017-06-06 15:25:16 +0300197 * @driver_ready: Called right after control channel is started. Used by
198 * ICM to send driver ready message to the firmware.
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300199 * @start: Starts the domain
200 * @stop: Stops the domain
201 * @suspend_noirq: Connection manager specific suspend_noirq
202 * @resume_noirq: Connection manager specific resume_noirq
Mika Westerbergf67cf492017-06-06 15:25:16 +0300203 * @suspend: Connection manager specific suspend
204 * @complete: Connection manager specific complete
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300205 * @runtime_suspend: Connection manager specific runtime_suspend
206 * @runtime_resume: Connection manager specific runtime_resume
Mika Westerberg81a54b52017-06-06 15:25:09 +0300207 * @handle_event: Handle thunderbolt event
Mika Westerberg9aaa3b82018-01-21 12:08:04 +0200208 * @get_boot_acl: Get boot ACL list
209 * @set_boot_acl: Set boot ACL list
Mika Westerbergf67cf492017-06-06 15:25:16 +0300210 * @approve_switch: Approve switch
211 * @add_switch_key: Add key to switch
212 * @challenge_switch_key: Challenge switch using key
Mika Westerberge6b245c2017-06-06 15:25:17 +0300213 * @disconnect_pcie_paths: Disconnects PCIe paths before NVM update
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300214 * @approve_xdomain_paths: Approve (establish) XDomain DMA paths
215 * @disconnect_xdomain_paths: Disconnect XDomain DMA paths
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300216 */
217struct tb_cm_ops {
Mika Westerbergf67cf492017-06-06 15:25:16 +0300218 int (*driver_ready)(struct tb *tb);
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300219 int (*start)(struct tb *tb);
220 void (*stop)(struct tb *tb);
221 int (*suspend_noirq)(struct tb *tb);
222 int (*resume_noirq)(struct tb *tb);
Mika Westerbergf67cf492017-06-06 15:25:16 +0300223 int (*suspend)(struct tb *tb);
224 void (*complete)(struct tb *tb);
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300225 int (*runtime_suspend)(struct tb *tb);
226 int (*runtime_resume)(struct tb *tb);
Mika Westerberg81a54b52017-06-06 15:25:09 +0300227 void (*handle_event)(struct tb *tb, enum tb_cfg_pkg_type,
228 const void *buf, size_t size);
Mika Westerberg9aaa3b82018-01-21 12:08:04 +0200229 int (*get_boot_acl)(struct tb *tb, uuid_t *uuids, size_t nuuids);
230 int (*set_boot_acl)(struct tb *tb, const uuid_t *uuids, size_t nuuids);
Mika Westerbergf67cf492017-06-06 15:25:16 +0300231 int (*approve_switch)(struct tb *tb, struct tb_switch *sw);
232 int (*add_switch_key)(struct tb *tb, struct tb_switch *sw);
233 int (*challenge_switch_key)(struct tb *tb, struct tb_switch *sw,
234 const u8 *challenge, u8 *response);
Mika Westerberge6b245c2017-06-06 15:25:17 +0300235 int (*disconnect_pcie_paths)(struct tb *tb);
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300236 int (*approve_xdomain_paths)(struct tb *tb, struct tb_xdomain *xd);
237 int (*disconnect_xdomain_paths)(struct tb *tb, struct tb_xdomain *xd);
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300238};
Andreas Noever520b6702014-06-03 22:04:07 +0200239
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300240static inline void *tb_priv(struct tb *tb)
241{
242 return (void *)tb->privdata;
243}
244
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300245#define TB_AUTOSUSPEND_DELAY 15000 /* ms */
246
Andreas Noevera25c8b22014-06-03 22:04:02 +0200247/* helper functions & macros */
248
249/**
250 * tb_upstream_port() - return the upstream port of a switch
251 *
252 * Every switch has an upstream port (for the root switch it is the NHI).
253 *
254 * During switch alloc/init tb_upstream_port()->remote may be NULL, even for
255 * non root switches (on the NHI port remote is always NULL).
256 *
257 * Return: Returns the upstream port of the switch.
258 */
259static inline struct tb_port *tb_upstream_port(struct tb_switch *sw)
260{
261 return &sw->ports[sw->config.upstream_port_number];
262}
263
264static inline u64 tb_route(struct tb_switch *sw)
265{
266 return ((u64) sw->config.route_hi) << 32 | sw->config.route_lo;
267}
268
Mika Westerbergf67cf492017-06-06 15:25:16 +0300269static inline struct tb_port *tb_port_at(u64 route, struct tb_switch *sw)
270{
271 u8 port;
272
273 port = route >> (sw->config.depth * 8);
274 if (WARN_ON(port > sw->config.max_port_number))
275 return NULL;
276 return &sw->ports[port];
277}
278
Andreas Noevera25c8b22014-06-03 22:04:02 +0200279static inline int tb_sw_read(struct tb_switch *sw, void *buffer,
280 enum tb_cfg_space space, u32 offset, u32 length)
281{
Mika Westerberg47083842019-03-19 17:07:37 +0200282 if (sw->is_unplugged)
283 return -ENODEV;
Andreas Noevera25c8b22014-06-03 22:04:02 +0200284 return tb_cfg_read(sw->tb->ctl,
285 buffer,
286 tb_route(sw),
287 0,
288 space,
289 offset,
290 length);
291}
292
293static inline int tb_sw_write(struct tb_switch *sw, void *buffer,
294 enum tb_cfg_space space, u32 offset, u32 length)
295{
Mika Westerberg47083842019-03-19 17:07:37 +0200296 if (sw->is_unplugged)
297 return -ENODEV;
Andreas Noevera25c8b22014-06-03 22:04:02 +0200298 return tb_cfg_write(sw->tb->ctl,
299 buffer,
300 tb_route(sw),
301 0,
302 space,
303 offset,
304 length);
305}
306
307static inline int tb_port_read(struct tb_port *port, void *buffer,
308 enum tb_cfg_space space, u32 offset, u32 length)
309{
Mika Westerberg47083842019-03-19 17:07:37 +0200310 if (port->sw->is_unplugged)
311 return -ENODEV;
Andreas Noevera25c8b22014-06-03 22:04:02 +0200312 return tb_cfg_read(port->sw->tb->ctl,
313 buffer,
314 tb_route(port->sw),
315 port->port,
316 space,
317 offset,
318 length);
319}
320
Mika Westerberg16a12582017-06-06 15:24:53 +0300321static inline int tb_port_write(struct tb_port *port, const void *buffer,
Andreas Noevera25c8b22014-06-03 22:04:02 +0200322 enum tb_cfg_space space, u32 offset, u32 length)
323{
Mika Westerberg47083842019-03-19 17:07:37 +0200324 if (port->sw->is_unplugged)
325 return -ENODEV;
Andreas Noevera25c8b22014-06-03 22:04:02 +0200326 return tb_cfg_write(port->sw->tb->ctl,
327 buffer,
328 tb_route(port->sw),
329 port->port,
330 space,
331 offset,
332 length);
333}
334
335#define tb_err(tb, fmt, arg...) dev_err(&(tb)->nhi->pdev->dev, fmt, ## arg)
336#define tb_WARN(tb, fmt, arg...) dev_WARN(&(tb)->nhi->pdev->dev, fmt, ## arg)
337#define tb_warn(tb, fmt, arg...) dev_warn(&(tb)->nhi->pdev->dev, fmt, ## arg)
338#define tb_info(tb, fmt, arg...) dev_info(&(tb)->nhi->pdev->dev, fmt, ## arg)
Mika Westerbergdaa51402018-10-01 12:31:19 +0300339#define tb_dbg(tb, fmt, arg...) dev_dbg(&(tb)->nhi->pdev->dev, fmt, ## arg)
Andreas Noevera25c8b22014-06-03 22:04:02 +0200340
341#define __TB_SW_PRINT(level, sw, fmt, arg...) \
342 do { \
343 struct tb_switch *__sw = (sw); \
344 level(__sw->tb, "%llx: " fmt, \
345 tb_route(__sw), ## arg); \
346 } while (0)
347#define tb_sw_WARN(sw, fmt, arg...) __TB_SW_PRINT(tb_WARN, sw, fmt, ##arg)
348#define tb_sw_warn(sw, fmt, arg...) __TB_SW_PRINT(tb_warn, sw, fmt, ##arg)
349#define tb_sw_info(sw, fmt, arg...) __TB_SW_PRINT(tb_info, sw, fmt, ##arg)
Mika Westerbergdaa51402018-10-01 12:31:19 +0300350#define tb_sw_dbg(sw, fmt, arg...) __TB_SW_PRINT(tb_dbg, sw, fmt, ##arg)
Andreas Noevera25c8b22014-06-03 22:04:02 +0200351
352#define __TB_PORT_PRINT(level, _port, fmt, arg...) \
353 do { \
354 struct tb_port *__port = (_port); \
355 level(__port->sw->tb, "%llx:%x: " fmt, \
356 tb_route(__port->sw), __port->port, ## arg); \
357 } while (0)
358#define tb_port_WARN(port, fmt, arg...) \
359 __TB_PORT_PRINT(tb_WARN, port, fmt, ##arg)
360#define tb_port_warn(port, fmt, arg...) \
361 __TB_PORT_PRINT(tb_warn, port, fmt, ##arg)
362#define tb_port_info(port, fmt, arg...) \
363 __TB_PORT_PRINT(tb_info, port, fmt, ##arg)
Mika Westerbergdaa51402018-10-01 12:31:19 +0300364#define tb_port_dbg(port, fmt, arg...) \
365 __TB_PORT_PRINT(tb_dbg, port, fmt, ##arg)
Andreas Noevera25c8b22014-06-03 22:04:02 +0200366
Mika Westerbergf67cf492017-06-06 15:25:16 +0300367struct tb *icm_probe(struct tb_nhi *nhi);
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300368struct tb *tb_probe(struct tb_nhi *nhi);
Andreas Noevera25c8b22014-06-03 22:04:02 +0200369
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300370extern struct device_type tb_domain_type;
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300371extern struct device_type tb_switch_type;
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300372
373int tb_domain_init(void);
374void tb_domain_exit(void);
Mika Westerberge6b245c2017-06-06 15:25:17 +0300375void tb_switch_exit(void);
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300376int tb_xdomain_init(void);
377void tb_xdomain_exit(void);
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300378
379struct tb *tb_domain_alloc(struct tb_nhi *nhi, size_t privsize);
380int tb_domain_add(struct tb *tb);
381void tb_domain_remove(struct tb *tb);
382int tb_domain_suspend_noirq(struct tb *tb);
383int tb_domain_resume_noirq(struct tb *tb);
Mika Westerbergf67cf492017-06-06 15:25:16 +0300384int tb_domain_suspend(struct tb *tb);
385void tb_domain_complete(struct tb *tb);
Mika Westerberg2d8ff0b2018-07-25 11:48:39 +0300386int tb_domain_runtime_suspend(struct tb *tb);
387int tb_domain_runtime_resume(struct tb *tb);
Mika Westerbergf67cf492017-06-06 15:25:16 +0300388int tb_domain_approve_switch(struct tb *tb, struct tb_switch *sw);
389int tb_domain_approve_switch_key(struct tb *tb, struct tb_switch *sw);
390int tb_domain_challenge_switch_key(struct tb *tb, struct tb_switch *sw);
Mika Westerberge6b245c2017-06-06 15:25:17 +0300391int tb_domain_disconnect_pcie_paths(struct tb *tb);
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300392int tb_domain_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd);
393int tb_domain_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd);
394int tb_domain_disconnect_all_paths(struct tb *tb);
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300395
396static inline void tb_domain_put(struct tb *tb)
397{
398 put_device(&tb->dev);
399}
Andreas Noeverd6cc51c2014-06-03 22:04:00 +0200400
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300401struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent,
402 u64 route);
Mika Westerberge6b245c2017-06-06 15:25:17 +0300403struct tb_switch *tb_switch_alloc_safe_mode(struct tb *tb,
404 struct device *parent, u64 route);
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300405int tb_switch_configure(struct tb_switch *sw);
406int tb_switch_add(struct tb_switch *sw);
407void tb_switch_remove(struct tb_switch *sw);
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200408void tb_switch_suspend(struct tb_switch *sw);
409int tb_switch_resume(struct tb_switch *sw);
410int tb_switch_reset(struct tb *tb, u64 route);
Lukas Wunneraae20bb2016-03-20 13:57:20 +0100411void tb_sw_set_unplugged(struct tb_switch *sw);
Mika Westerbergf67cf492017-06-06 15:25:16 +0300412struct tb_switch *tb_switch_find_by_link_depth(struct tb *tb, u8 link,
413 u8 depth);
Christoph Hellwig7c39ffe2017-07-18 15:30:05 +0200414struct tb_switch *tb_switch_find_by_uuid(struct tb *tb, const uuid_t *uuid);
Radion Mirchevsky8e9267b2017-10-04 15:24:14 +0300415struct tb_switch *tb_switch_find_by_route(struct tb *tb, u64 route);
Mika Westerbergf67cf492017-06-06 15:25:16 +0300416
Mika Westerbergb6b0ea72017-10-04 15:19:20 +0300417static inline struct tb_switch *tb_switch_get(struct tb_switch *sw)
418{
419 if (sw)
420 get_device(&sw->dev);
421 return sw;
422}
423
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300424static inline void tb_switch_put(struct tb_switch *sw)
425{
426 put_device(&sw->dev);
427}
428
429static inline bool tb_is_switch(const struct device *dev)
430{
431 return dev->type == &tb_switch_type;
432}
433
434static inline struct tb_switch *tb_to_switch(struct device *dev)
435{
436 if (tb_is_switch(dev))
437 return container_of(dev, struct tb_switch, dev);
438 return NULL;
439}
440
Mika Westerberg8b0110d2019-01-08 18:55:09 +0200441static inline bool tb_switch_is_lr(const struct tb_switch *sw)
442{
443 return sw->config.device_id == PCI_DEVICE_ID_INTEL_LIGHT_RIDGE;
444}
445
446static inline bool tb_switch_is_er(const struct tb_switch *sw)
447{
448 return sw->config.device_id == PCI_DEVICE_ID_INTEL_EAGLE_RIDGE;
449}
450
Andreas Noever9da672a2014-06-03 22:04:05 +0200451int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged);
Andreas Noever520b6702014-06-03 22:04:07 +0200452int tb_port_add_nfc_credits(struct tb_port *port, int credits);
453int tb_port_clear_counter(struct tb_port *port, int counter);
Andreas Noever9da672a2014-06-03 22:04:05 +0200454
Mika Westerbergda2da042017-06-06 15:24:58 +0300455int tb_switch_find_vse_cap(struct tb_switch *sw, enum tb_switch_vse_cap vsec);
456int tb_port_find_cap(struct tb_port *port, enum tb_port_cap cap);
Andreas Noevere2b87852014-06-03 22:04:03 +0200457
Andreas Noever520b6702014-06-03 22:04:07 +0200458struct tb_path *tb_path_alloc(struct tb *tb, int num_hops);
459void tb_path_free(struct tb_path *path);
460int tb_path_activate(struct tb_path *path);
461void tb_path_deactivate(struct tb_path *path);
462bool tb_path_is_invalid(struct tb_path *path);
463
Andreas Noevercd22e732014-06-12 23:11:46 +0200464int tb_drom_read(struct tb_switch *sw);
465int tb_drom_read_uid_only(struct tb_switch *sw, u64 *uid);
Andreas Noeverc90553b2014-06-03 22:04:11 +0200466
Mika Westerberga9be5582019-01-09 16:42:12 +0200467int tb_lc_read_uuid(struct tb_switch *sw, u32 *uuid);
Andreas Noevera25c8b22014-06-03 22:04:02 +0200468
469static inline int tb_route_length(u64 route)
470{
471 return (fls64(route) + TB_ROUTE_SHIFT - 1) / TB_ROUTE_SHIFT;
472}
473
474static inline bool tb_is_upstream_port(struct tb_port *port)
475{
476 return port == tb_upstream_port(port->sw);
477}
478
Andreas Noever9da672a2014-06-03 22:04:05 +0200479/**
480 * tb_downstream_route() - get route to downstream switch
481 *
482 * Port must not be the upstream port (otherwise a loop is created).
483 *
484 * Return: Returns a route to the switch behind @port.
485 */
486static inline u64 tb_downstream_route(struct tb_port *port)
487{
488 return tb_route(port->sw)
489 | ((u64) port->port << (port->sw->config.depth * 8));
490}
491
Mika Westerbergd1ff7022017-10-02 13:38:34 +0300492bool tb_xdomain_handle_request(struct tb *tb, enum tb_cfg_pkg_type type,
493 const void *buf, size_t size);
494struct tb_xdomain *tb_xdomain_alloc(struct tb *tb, struct device *parent,
495 u64 route, const uuid_t *local_uuid,
496 const uuid_t *remote_uuid);
497void tb_xdomain_add(struct tb_xdomain *xd);
498void tb_xdomain_remove(struct tb_xdomain *xd);
499struct tb_xdomain *tb_xdomain_find_by_link_depth(struct tb *tb, u8 link,
500 u8 depth);
501
Andreas Noeverd6cc51c2014-06-03 22:04:00 +0200502#endif