Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Thunderbolt Cactus Ridge driver - bus logic (NHI independent) |
| 3 | * |
| 4 | * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com> |
| 5 | */ |
| 6 | |
| 7 | #ifndef TB_H_ |
| 8 | #define TB_H_ |
| 9 | |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 10 | #include <linux/pci.h> |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 11 | #include <linux/uuid.h> |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 12 | |
| 13 | #include "tb_regs.h" |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 14 | #include "ctl.h" |
| 15 | |
| 16 | /** |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 17 | * struct tb_switch - a thunderbolt switch |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 18 | * @dev: Device for the switch |
| 19 | * @config: Switch configuration |
| 20 | * @ports: Ports in this switch |
| 21 | * @tb: Pointer to the domain the switch belongs to |
| 22 | * @uid: Unique ID of the switch |
| 23 | * @uuid: UUID of the switch (or %NULL if not supported) |
| 24 | * @vendor: Vendor ID of the switch |
| 25 | * @device: Device ID of the switch |
Mika Westerberg | 72ee339 | 2017-06-06 15:25:05 +0300 | [diff] [blame] | 26 | * @vendor_name: Name of the vendor (or %NULL if not known) |
| 27 | * @device_name: Name of the device (or %NULL if not known) |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 28 | * @cap_plug_events: Offset to the plug events capability (%0 if not found) |
| 29 | * @is_unplugged: The switch is going away |
| 30 | * @drom: DROM of the switch (%NULL if not found) |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 31 | */ |
| 32 | struct tb_switch { |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 33 | struct device dev; |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 34 | struct tb_regs_switch_header config; |
| 35 | struct tb_port *ports; |
| 36 | struct tb *tb; |
Andreas Noever | c90553b | 2014-06-03 22:04:11 +0200 | [diff] [blame] | 37 | u64 uid; |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 38 | uuid_be *uuid; |
| 39 | u16 vendor; |
| 40 | u16 device; |
Mika Westerberg | 72ee339 | 2017-06-06 15:25:05 +0300 | [diff] [blame] | 41 | const char *vendor_name; |
| 42 | const char *device_name; |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 43 | int cap_plug_events; |
| 44 | bool is_unplugged; |
Andreas Noever | cd22e73 | 2014-06-12 23:11:46 +0200 | [diff] [blame] | 45 | u8 *drom; |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | /** |
| 49 | * struct tb_port - a thunderbolt port, part of a tb_switch |
| 50 | */ |
| 51 | struct tb_port { |
| 52 | struct tb_regs_port_header config; |
| 53 | struct tb_switch *sw; |
| 54 | struct tb_port *remote; /* remote port, NULL if not connected */ |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 55 | int cap_phy; /* offset, zero if not found */ |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 56 | u8 port; /* port number on switch */ |
Andreas Noever | cd22e73 | 2014-06-12 23:11:46 +0200 | [diff] [blame] | 57 | bool disabled; /* disabled by eeprom */ |
| 58 | struct tb_port *dual_link_port; |
| 59 | u8 link_nr:1; |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | /** |
Andreas Noever | 520b670 | 2014-06-03 22:04:07 +0200 | [diff] [blame] | 63 | * struct tb_path_hop - routing information for a tb_path |
| 64 | * |
| 65 | * Hop configuration is always done on the IN port of a switch. |
| 66 | * in_port and out_port have to be on the same switch. Packets arriving on |
| 67 | * in_port with "hop" = in_hop_index will get routed to through out_port. The |
| 68 | * next hop to take (on out_port->remote) is determined by next_hop_index. |
| 69 | * |
| 70 | * in_counter_index is the index of a counter (in TB_CFG_COUNTERS) on the in |
| 71 | * port. |
| 72 | */ |
| 73 | struct tb_path_hop { |
| 74 | struct tb_port *in_port; |
| 75 | struct tb_port *out_port; |
| 76 | int in_hop_index; |
| 77 | int in_counter_index; /* write -1 to disable counters for this hop. */ |
| 78 | int next_hop_index; |
| 79 | }; |
| 80 | |
| 81 | /** |
| 82 | * enum tb_path_port - path options mask |
| 83 | */ |
| 84 | enum tb_path_port { |
| 85 | TB_PATH_NONE = 0, |
| 86 | TB_PATH_SOURCE = 1, /* activate on the first hop (out of src) */ |
| 87 | TB_PATH_INTERNAL = 2, /* activate on other hops (not the first/last) */ |
| 88 | TB_PATH_DESTINATION = 4, /* activate on the last hop (into dst) */ |
| 89 | TB_PATH_ALL = 7, |
| 90 | }; |
| 91 | |
| 92 | /** |
| 93 | * struct tb_path - a unidirectional path between two ports |
| 94 | * |
| 95 | * A path consists of a number of hops (see tb_path_hop). To establish a PCIe |
| 96 | * tunnel two paths have to be created between the two PCIe ports. |
| 97 | * |
| 98 | */ |
| 99 | struct tb_path { |
| 100 | struct tb *tb; |
| 101 | int nfc_credits; /* non flow controlled credits */ |
| 102 | enum tb_path_port ingress_shared_buffer; |
| 103 | enum tb_path_port egress_shared_buffer; |
| 104 | enum tb_path_port ingress_fc_enable; |
| 105 | enum tb_path_port egress_fc_enable; |
| 106 | |
| 107 | int priority:3; |
| 108 | int weight:4; |
| 109 | bool drop_packages; |
| 110 | bool activated; |
| 111 | struct tb_path_hop *hops; |
| 112 | int path_length; /* number of hops */ |
| 113 | }; |
| 114 | |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 115 | /** |
| 116 | * struct tb_cm_ops - Connection manager specific operations vector |
| 117 | * @start: Starts the domain |
| 118 | * @stop: Stops the domain |
| 119 | * @suspend_noirq: Connection manager specific suspend_noirq |
| 120 | * @resume_noirq: Connection manager specific resume_noirq |
Mika Westerberg | 81a54b5 | 2017-06-06 15:25:09 +0300 | [diff] [blame^] | 121 | * @handle_event: Handle thunderbolt event |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 122 | */ |
| 123 | struct tb_cm_ops { |
| 124 | int (*start)(struct tb *tb); |
| 125 | void (*stop)(struct tb *tb); |
| 126 | int (*suspend_noirq)(struct tb *tb); |
| 127 | int (*resume_noirq)(struct tb *tb); |
Mika Westerberg | 81a54b5 | 2017-06-06 15:25:09 +0300 | [diff] [blame^] | 128 | void (*handle_event)(struct tb *tb, enum tb_cfg_pkg_type, |
| 129 | const void *buf, size_t size); |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 130 | }; |
Andreas Noever | 520b670 | 2014-06-03 22:04:07 +0200 | [diff] [blame] | 131 | |
| 132 | /** |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 133 | * struct tb - main thunderbolt bus structure |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 134 | * @dev: Domain device |
| 135 | * @lock: Big lock. Must be held when accessing cfg or any struct |
| 136 | * tb_switch / struct tb_port. |
| 137 | * @nhi: Pointer to the NHI structure |
| 138 | * @ctl: Control channel for this domain |
| 139 | * @wq: Ordered workqueue for all domain specific work |
| 140 | * @root_switch: Root switch of this domain |
| 141 | * @cm_ops: Connection manager specific operations vector |
| 142 | * @index: Linux assigned domain number |
| 143 | * @privdata: Private connection manager specific data |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 144 | */ |
| 145 | struct tb { |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 146 | struct device dev; |
| 147 | struct mutex lock; |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 148 | struct tb_nhi *nhi; |
| 149 | struct tb_ctl *ctl; |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 150 | struct workqueue_struct *wq; |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 151 | struct tb_switch *root_switch; |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 152 | const struct tb_cm_ops *cm_ops; |
| 153 | int index; |
| 154 | unsigned long privdata[0]; |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 155 | }; |
| 156 | |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 157 | static inline void *tb_priv(struct tb *tb) |
| 158 | { |
| 159 | return (void *)tb->privdata; |
| 160 | } |
| 161 | |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 162 | /* helper functions & macros */ |
| 163 | |
| 164 | /** |
| 165 | * tb_upstream_port() - return the upstream port of a switch |
| 166 | * |
| 167 | * Every switch has an upstream port (for the root switch it is the NHI). |
| 168 | * |
| 169 | * During switch alloc/init tb_upstream_port()->remote may be NULL, even for |
| 170 | * non root switches (on the NHI port remote is always NULL). |
| 171 | * |
| 172 | * Return: Returns the upstream port of the switch. |
| 173 | */ |
| 174 | static inline struct tb_port *tb_upstream_port(struct tb_switch *sw) |
| 175 | { |
| 176 | return &sw->ports[sw->config.upstream_port_number]; |
| 177 | } |
| 178 | |
| 179 | static inline u64 tb_route(struct tb_switch *sw) |
| 180 | { |
| 181 | return ((u64) sw->config.route_hi) << 32 | sw->config.route_lo; |
| 182 | } |
| 183 | |
| 184 | static inline int tb_sw_read(struct tb_switch *sw, void *buffer, |
| 185 | enum tb_cfg_space space, u32 offset, u32 length) |
| 186 | { |
| 187 | return tb_cfg_read(sw->tb->ctl, |
| 188 | buffer, |
| 189 | tb_route(sw), |
| 190 | 0, |
| 191 | space, |
| 192 | offset, |
| 193 | length); |
| 194 | } |
| 195 | |
| 196 | static inline int tb_sw_write(struct tb_switch *sw, void *buffer, |
| 197 | enum tb_cfg_space space, u32 offset, u32 length) |
| 198 | { |
| 199 | return tb_cfg_write(sw->tb->ctl, |
| 200 | buffer, |
| 201 | tb_route(sw), |
| 202 | 0, |
| 203 | space, |
| 204 | offset, |
| 205 | length); |
| 206 | } |
| 207 | |
| 208 | static inline int tb_port_read(struct tb_port *port, void *buffer, |
| 209 | enum tb_cfg_space space, u32 offset, u32 length) |
| 210 | { |
| 211 | return tb_cfg_read(port->sw->tb->ctl, |
| 212 | buffer, |
| 213 | tb_route(port->sw), |
| 214 | port->port, |
| 215 | space, |
| 216 | offset, |
| 217 | length); |
| 218 | } |
| 219 | |
Mika Westerberg | 16a1258 | 2017-06-06 15:24:53 +0300 | [diff] [blame] | 220 | static inline int tb_port_write(struct tb_port *port, const void *buffer, |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 221 | enum tb_cfg_space space, u32 offset, u32 length) |
| 222 | { |
| 223 | return tb_cfg_write(port->sw->tb->ctl, |
| 224 | buffer, |
| 225 | tb_route(port->sw), |
| 226 | port->port, |
| 227 | space, |
| 228 | offset, |
| 229 | length); |
| 230 | } |
| 231 | |
| 232 | #define tb_err(tb, fmt, arg...) dev_err(&(tb)->nhi->pdev->dev, fmt, ## arg) |
| 233 | #define tb_WARN(tb, fmt, arg...) dev_WARN(&(tb)->nhi->pdev->dev, fmt, ## arg) |
| 234 | #define tb_warn(tb, fmt, arg...) dev_warn(&(tb)->nhi->pdev->dev, fmt, ## arg) |
| 235 | #define tb_info(tb, fmt, arg...) dev_info(&(tb)->nhi->pdev->dev, fmt, ## arg) |
| 236 | |
| 237 | |
| 238 | #define __TB_SW_PRINT(level, sw, fmt, arg...) \ |
| 239 | do { \ |
| 240 | struct tb_switch *__sw = (sw); \ |
| 241 | level(__sw->tb, "%llx: " fmt, \ |
| 242 | tb_route(__sw), ## arg); \ |
| 243 | } while (0) |
| 244 | #define tb_sw_WARN(sw, fmt, arg...) __TB_SW_PRINT(tb_WARN, sw, fmt, ##arg) |
| 245 | #define tb_sw_warn(sw, fmt, arg...) __TB_SW_PRINT(tb_warn, sw, fmt, ##arg) |
| 246 | #define tb_sw_info(sw, fmt, arg...) __TB_SW_PRINT(tb_info, sw, fmt, ##arg) |
| 247 | |
| 248 | |
| 249 | #define __TB_PORT_PRINT(level, _port, fmt, arg...) \ |
| 250 | do { \ |
| 251 | struct tb_port *__port = (_port); \ |
| 252 | level(__port->sw->tb, "%llx:%x: " fmt, \ |
| 253 | tb_route(__port->sw), __port->port, ## arg); \ |
| 254 | } while (0) |
| 255 | #define tb_port_WARN(port, fmt, arg...) \ |
| 256 | __TB_PORT_PRINT(tb_WARN, port, fmt, ##arg) |
| 257 | #define tb_port_warn(port, fmt, arg...) \ |
| 258 | __TB_PORT_PRINT(tb_warn, port, fmt, ##arg) |
| 259 | #define tb_port_info(port, fmt, arg...) \ |
| 260 | __TB_PORT_PRINT(tb_info, port, fmt, ##arg) |
| 261 | |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 262 | struct tb *tb_probe(struct tb_nhi *nhi); |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 263 | |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 264 | extern struct bus_type tb_bus_type; |
| 265 | extern struct device_type tb_domain_type; |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 266 | extern struct device_type tb_switch_type; |
Mika Westerberg | 9d3cce0 | 2017-06-06 15:25:00 +0300 | [diff] [blame] | 267 | |
| 268 | int tb_domain_init(void); |
| 269 | void tb_domain_exit(void); |
| 270 | |
| 271 | struct tb *tb_domain_alloc(struct tb_nhi *nhi, size_t privsize); |
| 272 | int tb_domain_add(struct tb *tb); |
| 273 | void tb_domain_remove(struct tb *tb); |
| 274 | int tb_domain_suspend_noirq(struct tb *tb); |
| 275 | int tb_domain_resume_noirq(struct tb *tb); |
| 276 | |
| 277 | static inline void tb_domain_put(struct tb *tb) |
| 278 | { |
| 279 | put_device(&tb->dev); |
| 280 | } |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 281 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 282 | struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent, |
| 283 | u64 route); |
| 284 | int tb_switch_configure(struct tb_switch *sw); |
| 285 | int tb_switch_add(struct tb_switch *sw); |
| 286 | void tb_switch_remove(struct tb_switch *sw); |
Andreas Noever | 23dd5bb | 2014-06-03 22:04:12 +0200 | [diff] [blame] | 287 | void tb_switch_suspend(struct tb_switch *sw); |
| 288 | int tb_switch_resume(struct tb_switch *sw); |
| 289 | int tb_switch_reset(struct tb *tb, u64 route); |
Lukas Wunner | aae20bb | 2016-03-20 13:57:20 +0100 | [diff] [blame] | 290 | void tb_sw_set_unplugged(struct tb_switch *sw); |
Andreas Noever | 053596d | 2014-06-03 22:04:06 +0200 | [diff] [blame] | 291 | struct tb_switch *get_switch_at_route(struct tb_switch *sw, u64 route); |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 292 | |
Mika Westerberg | bfe778a | 2017-06-06 15:25:01 +0300 | [diff] [blame] | 293 | static inline void tb_switch_put(struct tb_switch *sw) |
| 294 | { |
| 295 | put_device(&sw->dev); |
| 296 | } |
| 297 | |
| 298 | static inline bool tb_is_switch(const struct device *dev) |
| 299 | { |
| 300 | return dev->type == &tb_switch_type; |
| 301 | } |
| 302 | |
| 303 | static inline struct tb_switch *tb_to_switch(struct device *dev) |
| 304 | { |
| 305 | if (tb_is_switch(dev)) |
| 306 | return container_of(dev, struct tb_switch, dev); |
| 307 | return NULL; |
| 308 | } |
| 309 | |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 310 | int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged); |
Andreas Noever | 520b670 | 2014-06-03 22:04:07 +0200 | [diff] [blame] | 311 | int tb_port_add_nfc_credits(struct tb_port *port, int credits); |
| 312 | int tb_port_clear_counter(struct tb_port *port, int counter); |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 313 | |
Mika Westerberg | da2da04 | 2017-06-06 15:24:58 +0300 | [diff] [blame] | 314 | int tb_switch_find_vse_cap(struct tb_switch *sw, enum tb_switch_vse_cap vsec); |
| 315 | int tb_port_find_cap(struct tb_port *port, enum tb_port_cap cap); |
Andreas Noever | e2b8785 | 2014-06-03 22:04:03 +0200 | [diff] [blame] | 316 | |
Andreas Noever | 520b670 | 2014-06-03 22:04:07 +0200 | [diff] [blame] | 317 | struct tb_path *tb_path_alloc(struct tb *tb, int num_hops); |
| 318 | void tb_path_free(struct tb_path *path); |
| 319 | int tb_path_activate(struct tb_path *path); |
| 320 | void tb_path_deactivate(struct tb_path *path); |
| 321 | bool tb_path_is_invalid(struct tb_path *path); |
| 322 | |
Andreas Noever | cd22e73 | 2014-06-12 23:11:46 +0200 | [diff] [blame] | 323 | int tb_drom_read(struct tb_switch *sw); |
| 324 | int tb_drom_read_uid_only(struct tb_switch *sw, u64 *uid); |
Andreas Noever | c90553b | 2014-06-03 22:04:11 +0200 | [diff] [blame] | 325 | |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 326 | |
| 327 | static inline int tb_route_length(u64 route) |
| 328 | { |
| 329 | return (fls64(route) + TB_ROUTE_SHIFT - 1) / TB_ROUTE_SHIFT; |
| 330 | } |
| 331 | |
| 332 | static inline bool tb_is_upstream_port(struct tb_port *port) |
| 333 | { |
| 334 | return port == tb_upstream_port(port->sw); |
| 335 | } |
| 336 | |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame] | 337 | /** |
| 338 | * tb_downstream_route() - get route to downstream switch |
| 339 | * |
| 340 | * Port must not be the upstream port (otherwise a loop is created). |
| 341 | * |
| 342 | * Return: Returns a route to the switch behind @port. |
| 343 | */ |
| 344 | static inline u64 tb_downstream_route(struct tb_port *port) |
| 345 | { |
| 346 | return tb_route(port->sw) |
| 347 | | ((u64) port->port << (port->sw->config.depth * 8)); |
| 348 | } |
| 349 | |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 350 | #endif |