blob: 5bb9a5d60d2cd9503b807e44cf7e33f914384fc3 [file] [log] [blame]
Andreas Noeverd6cc51c2014-06-03 22:04:00 +02001/*
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 Noevera25c8b22014-06-03 22:04:02 +020010#include <linux/pci.h>
Mika Westerbergbfe778a2017-06-06 15:25:01 +030011#include <linux/uuid.h>
Andreas Noevera25c8b22014-06-03 22:04:02 +020012
13#include "tb_regs.h"
Andreas Noeverd6cc51c2014-06-03 22:04:00 +020014#include "ctl.h"
15
16/**
Andreas Noevera25c8b22014-06-03 22:04:02 +020017 * struct tb_switch - a thunderbolt switch
Mika Westerbergbfe778a2017-06-06 15:25:01 +030018 * @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 Westerberg72ee3392017-06-06 15:25:05 +030026 * @vendor_name: Name of the vendor (or %NULL if not known)
27 * @device_name: Name of the device (or %NULL if not known)
Mika Westerbergbfe778a2017-06-06 15:25:01 +030028 * @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 Noevera25c8b22014-06-03 22:04:02 +020031 */
32struct tb_switch {
Mika Westerbergbfe778a2017-06-06 15:25:01 +030033 struct device dev;
Andreas Noevera25c8b22014-06-03 22:04:02 +020034 struct tb_regs_switch_header config;
35 struct tb_port *ports;
36 struct tb *tb;
Andreas Noeverc90553b2014-06-03 22:04:11 +020037 u64 uid;
Mika Westerbergbfe778a2017-06-06 15:25:01 +030038 uuid_be *uuid;
39 u16 vendor;
40 u16 device;
Mika Westerberg72ee3392017-06-06 15:25:05 +030041 const char *vendor_name;
42 const char *device_name;
Mika Westerbergbfe778a2017-06-06 15:25:01 +030043 int cap_plug_events;
44 bool is_unplugged;
Andreas Noevercd22e732014-06-12 23:11:46 +020045 u8 *drom;
Andreas Noevera25c8b22014-06-03 22:04:02 +020046};
47
48/**
49 * struct tb_port - a thunderbolt port, part of a tb_switch
50 */
51struct 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 Noever9da672a2014-06-03 22:04:05 +020055 int cap_phy; /* offset, zero if not found */
Andreas Noevera25c8b22014-06-03 22:04:02 +020056 u8 port; /* port number on switch */
Andreas Noevercd22e732014-06-12 23:11:46 +020057 bool disabled; /* disabled by eeprom */
58 struct tb_port *dual_link_port;
59 u8 link_nr:1;
Andreas Noevera25c8b22014-06-03 22:04:02 +020060};
61
62/**
Andreas Noever520b6702014-06-03 22:04:07 +020063 * 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 */
73struct 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 */
84enum 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 */
99struct 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 Westerberg9d3cce02017-06-06 15:25:00 +0300115/**
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 Westerberg81a54b52017-06-06 15:25:09 +0300121 * @handle_event: Handle thunderbolt event
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300122 */
123struct 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 Westerberg81a54b52017-06-06 15:25:09 +0300128 void (*handle_event)(struct tb *tb, enum tb_cfg_pkg_type,
129 const void *buf, size_t size);
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300130};
Andreas Noever520b6702014-06-03 22:04:07 +0200131
132/**
Andreas Noeverd6cc51c2014-06-03 22:04:00 +0200133 * struct tb - main thunderbolt bus structure
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300134 * @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 Noeverd6cc51c2014-06-03 22:04:00 +0200144 */
145struct tb {
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300146 struct device dev;
147 struct mutex lock;
Andreas Noeverd6cc51c2014-06-03 22:04:00 +0200148 struct tb_nhi *nhi;
149 struct tb_ctl *ctl;
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300150 struct workqueue_struct *wq;
Andreas Noevera25c8b22014-06-03 22:04:02 +0200151 struct tb_switch *root_switch;
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300152 const struct tb_cm_ops *cm_ops;
153 int index;
154 unsigned long privdata[0];
Andreas Noeverd6cc51c2014-06-03 22:04:00 +0200155};
156
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300157static inline void *tb_priv(struct tb *tb)
158{
159 return (void *)tb->privdata;
160}
161
Andreas Noevera25c8b22014-06-03 22:04:02 +0200162/* 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 */
174static inline struct tb_port *tb_upstream_port(struct tb_switch *sw)
175{
176 return &sw->ports[sw->config.upstream_port_number];
177}
178
179static inline u64 tb_route(struct tb_switch *sw)
180{
181 return ((u64) sw->config.route_hi) << 32 | sw->config.route_lo;
182}
183
184static 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
196static 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
208static 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 Westerberg16a12582017-06-06 15:24:53 +0300220static inline int tb_port_write(struct tb_port *port, const void *buffer,
Andreas Noevera25c8b22014-06-03 22:04:02 +0200221 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 Westerberg9d3cce02017-06-06 15:25:00 +0300262struct tb *tb_probe(struct tb_nhi *nhi);
Andreas Noevera25c8b22014-06-03 22:04:02 +0200263
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300264extern struct bus_type tb_bus_type;
265extern struct device_type tb_domain_type;
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300266extern struct device_type tb_switch_type;
Mika Westerberg9d3cce02017-06-06 15:25:00 +0300267
268int tb_domain_init(void);
269void tb_domain_exit(void);
270
271struct tb *tb_domain_alloc(struct tb_nhi *nhi, size_t privsize);
272int tb_domain_add(struct tb *tb);
273void tb_domain_remove(struct tb *tb);
274int tb_domain_suspend_noirq(struct tb *tb);
275int tb_domain_resume_noirq(struct tb *tb);
276
277static inline void tb_domain_put(struct tb *tb)
278{
279 put_device(&tb->dev);
280}
Andreas Noeverd6cc51c2014-06-03 22:04:00 +0200281
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300282struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent,
283 u64 route);
284int tb_switch_configure(struct tb_switch *sw);
285int tb_switch_add(struct tb_switch *sw);
286void tb_switch_remove(struct tb_switch *sw);
Andreas Noever23dd5bb2014-06-03 22:04:12 +0200287void tb_switch_suspend(struct tb_switch *sw);
288int tb_switch_resume(struct tb_switch *sw);
289int tb_switch_reset(struct tb *tb, u64 route);
Lukas Wunneraae20bb2016-03-20 13:57:20 +0100290void tb_sw_set_unplugged(struct tb_switch *sw);
Andreas Noever053596d2014-06-03 22:04:06 +0200291struct tb_switch *get_switch_at_route(struct tb_switch *sw, u64 route);
Andreas Noevera25c8b22014-06-03 22:04:02 +0200292
Mika Westerbergbfe778a2017-06-06 15:25:01 +0300293static inline void tb_switch_put(struct tb_switch *sw)
294{
295 put_device(&sw->dev);
296}
297
298static inline bool tb_is_switch(const struct device *dev)
299{
300 return dev->type == &tb_switch_type;
301}
302
303static 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 Noever9da672a2014-06-03 22:04:05 +0200310int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged);
Andreas Noever520b6702014-06-03 22:04:07 +0200311int tb_port_add_nfc_credits(struct tb_port *port, int credits);
312int tb_port_clear_counter(struct tb_port *port, int counter);
Andreas Noever9da672a2014-06-03 22:04:05 +0200313
Mika Westerbergda2da042017-06-06 15:24:58 +0300314int tb_switch_find_vse_cap(struct tb_switch *sw, enum tb_switch_vse_cap vsec);
315int tb_port_find_cap(struct tb_port *port, enum tb_port_cap cap);
Andreas Noevere2b87852014-06-03 22:04:03 +0200316
Andreas Noever520b6702014-06-03 22:04:07 +0200317struct tb_path *tb_path_alloc(struct tb *tb, int num_hops);
318void tb_path_free(struct tb_path *path);
319int tb_path_activate(struct tb_path *path);
320void tb_path_deactivate(struct tb_path *path);
321bool tb_path_is_invalid(struct tb_path *path);
322
Andreas Noevercd22e732014-06-12 23:11:46 +0200323int tb_drom_read(struct tb_switch *sw);
324int tb_drom_read_uid_only(struct tb_switch *sw, u64 *uid);
Andreas Noeverc90553b2014-06-03 22:04:11 +0200325
Andreas Noevera25c8b22014-06-03 22:04:02 +0200326
327static inline int tb_route_length(u64 route)
328{
329 return (fls64(route) + TB_ROUTE_SHIFT - 1) / TB_ROUTE_SHIFT;
330}
331
332static inline bool tb_is_upstream_port(struct tb_port *port)
333{
334 return port == tb_upstream_port(port->sw);
335}
336
Andreas Noever9da672a2014-06-03 22:04:05 +0200337/**
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 */
344static 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 Noeverd6cc51c2014-06-03 22:04:00 +0200350#endif