1
2
3
4
5
6
7#ifndef TB_H_
8#define TB_H_
9
10#include <linux/pci.h>
11
12#include "tb_regs.h"
13#include "ctl.h"
14
15
16
17
18struct tb_switch {
19 struct tb_regs_switch_header config;
20 struct tb_port *ports;
21 struct tb *tb;
22 u64 uid;
23 int cap_plug_events;
24 bool is_unplugged;
25 u8 *drom;
26};
27
28
29
30
31struct tb_port {
32 struct tb_regs_port_header config;
33 struct tb_switch *sw;
34 struct tb_port *remote;
35 int cap_phy;
36 u8 port;
37 bool disabled;
38 struct tb_port *dual_link_port;
39 u8 link_nr:1;
40};
41
42
43
44
45
46
47
48
49
50
51
52
53struct tb_path_hop {
54 struct tb_port *in_port;
55 struct tb_port *out_port;
56 int in_hop_index;
57 int in_counter_index;
58 int next_hop_index;
59};
60
61
62
63
64enum tb_path_port {
65 TB_PATH_NONE = 0,
66 TB_PATH_SOURCE = 1,
67 TB_PATH_INTERNAL = 2,
68 TB_PATH_DESTINATION = 4,
69 TB_PATH_ALL = 7,
70};
71
72
73
74
75
76
77
78
79struct tb_path {
80 struct tb *tb;
81 int nfc_credits;
82 enum tb_path_port ingress_shared_buffer;
83 enum tb_path_port egress_shared_buffer;
84 enum tb_path_port ingress_fc_enable;
85 enum tb_path_port egress_fc_enable;
86
87 int priority:3;
88 int weight:4;
89 bool drop_packages;
90 bool activated;
91 struct tb_path_hop *hops;
92 int path_length;
93};
94
95
96
97
98
99struct tb {
100 struct mutex lock;
101
102
103
104 struct tb_nhi *nhi;
105 struct tb_ctl *ctl;
106 struct workqueue_struct *wq;
107 struct tb_switch *root_switch;
108 struct list_head tunnel_list;
109 bool hotplug_active;
110
111
112
113
114
115
116};
117
118
119
120
121
122
123
124
125
126
127
128
129
130static inline struct tb_port *tb_upstream_port(struct tb_switch *sw)
131{
132 return &sw->ports[sw->config.upstream_port_number];
133}
134
135static inline u64 tb_route(struct tb_switch *sw)
136{
137 return ((u64) sw->config.route_hi) << 32 | sw->config.route_lo;
138}
139
140static inline int tb_sw_read(struct tb_switch *sw, void *buffer,
141 enum tb_cfg_space space, u32 offset, u32 length)
142{
143 return tb_cfg_read(sw->tb->ctl,
144 buffer,
145 tb_route(sw),
146 0,
147 space,
148 offset,
149 length);
150}
151
152static inline int tb_sw_write(struct tb_switch *sw, void *buffer,
153 enum tb_cfg_space space, u32 offset, u32 length)
154{
155 return tb_cfg_write(sw->tb->ctl,
156 buffer,
157 tb_route(sw),
158 0,
159 space,
160 offset,
161 length);
162}
163
164static inline int tb_port_read(struct tb_port *port, void *buffer,
165 enum tb_cfg_space space, u32 offset, u32 length)
166{
167 return tb_cfg_read(port->sw->tb->ctl,
168 buffer,
169 tb_route(port->sw),
170 port->port,
171 space,
172 offset,
173 length);
174}
175
176static inline int tb_port_write(struct tb_port *port, void *buffer,
177 enum tb_cfg_space space, u32 offset, u32 length)
178{
179 return tb_cfg_write(port->sw->tb->ctl,
180 buffer,
181 tb_route(port->sw),
182 port->port,
183 space,
184 offset,
185 length);
186}
187
188#define tb_err(tb, fmt, arg...) dev_err(&(tb)->nhi->pdev->dev, fmt, ## arg)
189#define tb_WARN(tb, fmt, arg...) dev_WARN(&(tb)->nhi->pdev->dev, fmt, ## arg)
190#define tb_warn(tb, fmt, arg...) dev_warn(&(tb)->nhi->pdev->dev, fmt, ## arg)
191#define tb_info(tb, fmt, arg...) dev_info(&(tb)->nhi->pdev->dev, fmt, ## arg)
192
193
194#define __TB_SW_PRINT(level, sw, fmt, arg...) \
195 do { \
196 struct tb_switch *__sw = (sw); \
197 level(__sw->tb, "%llx: " fmt, \
198 tb_route(__sw), ## arg); \
199 } while (0)
200#define tb_sw_WARN(sw, fmt, arg...) __TB_SW_PRINT(tb_WARN, sw, fmt, ##arg)
201#define tb_sw_warn(sw, fmt, arg...) __TB_SW_PRINT(tb_warn, sw, fmt, ##arg)
202#define tb_sw_info(sw, fmt, arg...) __TB_SW_PRINT(tb_info, sw, fmt, ##arg)
203
204
205#define __TB_PORT_PRINT(level, _port, fmt, arg...) \
206 do { \
207 struct tb_port *__port = (_port); \
208 level(__port->sw->tb, "%llx:%x: " fmt, \
209 tb_route(__port->sw), __port->port, ## arg); \
210 } while (0)
211#define tb_port_WARN(port, fmt, arg...) \
212 __TB_PORT_PRINT(tb_WARN, port, fmt, ##arg)
213#define tb_port_warn(port, fmt, arg...) \
214 __TB_PORT_PRINT(tb_warn, port, fmt, ##arg)
215#define tb_port_info(port, fmt, arg...) \
216 __TB_PORT_PRINT(tb_info, port, fmt, ##arg)
217
218
219struct tb *thunderbolt_alloc_and_start(struct tb_nhi *nhi);
220void thunderbolt_shutdown_and_free(struct tb *tb);
221void thunderbolt_suspend(struct tb *tb);
222void thunderbolt_resume(struct tb *tb);
223
224struct tb_switch *tb_switch_alloc(struct tb *tb, u64 route);
225void tb_switch_free(struct tb_switch *sw);
226void tb_switch_suspend(struct tb_switch *sw);
227int tb_switch_resume(struct tb_switch *sw);
228int tb_switch_reset(struct tb *tb, u64 route);
229void tb_sw_set_unplugged(struct tb_switch *sw);
230struct tb_switch *get_switch_at_route(struct tb_switch *sw, u64 route);
231
232int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged);
233int tb_port_add_nfc_credits(struct tb_port *port, int credits);
234int tb_port_clear_counter(struct tb_port *port, int counter);
235
236int tb_find_cap(struct tb_port *port, enum tb_cfg_space space, enum tb_cap cap);
237
238struct tb_path *tb_path_alloc(struct tb *tb, int num_hops);
239void tb_path_free(struct tb_path *path);
240int tb_path_activate(struct tb_path *path);
241void tb_path_deactivate(struct tb_path *path);
242bool tb_path_is_invalid(struct tb_path *path);
243
244int tb_drom_read(struct tb_switch *sw);
245int tb_drom_read_uid_only(struct tb_switch *sw, u64 *uid);
246
247
248static inline int tb_route_length(u64 route)
249{
250 return (fls64(route) + TB_ROUTE_SHIFT - 1) / TB_ROUTE_SHIFT;
251}
252
253static inline bool tb_is_upstream_port(struct tb_port *port)
254{
255 return port == tb_upstream_port(port->sw);
256}
257
258
259
260
261
262
263
264
265static inline u64 tb_downstream_route(struct tb_port *port)
266{
267 return tb_route(port->sw)
268 | ((u64) port->port << (port->sw->config.depth * 8));
269}
270
271#endif
272