PCI: Suspend/resume quirks for Apple thunderbolt
[deliverable/linux.git] / drivers / thunderbolt / tb.h
CommitLineData
d6cc51cd
AN
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
a25c8b2f
AN
10#include <linux/pci.h>
11
12#include "tb_regs.h"
d6cc51cd
AN
13#include "ctl.h"
14
a25c8b2f
AN
15/**
16 * struct tb_switch - a thunderbolt switch
17 */
18struct tb_switch {
19 struct tb_regs_switch_header config;
20 struct tb_port *ports;
21 struct tb *tb;
ca389f71 22 int cap_plug_events; /* offset, zero if not found */
053596d9 23 bool is_unplugged; /* unplugged, will go away */
a25c8b2f
AN
24};
25
26/**
27 * struct tb_port - a thunderbolt port, part of a tb_switch
28 */
29struct tb_port {
30 struct tb_regs_port_header config;
31 struct tb_switch *sw;
32 struct tb_port *remote; /* remote port, NULL if not connected */
9da672a4 33 int cap_phy; /* offset, zero if not found */
a25c8b2f
AN
34 u8 port; /* port number on switch */
35};
36
520b6702
AN
37/**
38 * struct tb_path_hop - routing information for a tb_path
39 *
40 * Hop configuration is always done on the IN port of a switch.
41 * in_port and out_port have to be on the same switch. Packets arriving on
42 * in_port with "hop" = in_hop_index will get routed to through out_port. The
43 * next hop to take (on out_port->remote) is determined by next_hop_index.
44 *
45 * in_counter_index is the index of a counter (in TB_CFG_COUNTERS) on the in
46 * port.
47 */
48struct tb_path_hop {
49 struct tb_port *in_port;
50 struct tb_port *out_port;
51 int in_hop_index;
52 int in_counter_index; /* write -1 to disable counters for this hop. */
53 int next_hop_index;
54};
55
56/**
57 * enum tb_path_port - path options mask
58 */
59enum tb_path_port {
60 TB_PATH_NONE = 0,
61 TB_PATH_SOURCE = 1, /* activate on the first hop (out of src) */
62 TB_PATH_INTERNAL = 2, /* activate on other hops (not the first/last) */
63 TB_PATH_DESTINATION = 4, /* activate on the last hop (into dst) */
64 TB_PATH_ALL = 7,
65};
66
67/**
68 * struct tb_path - a unidirectional path between two ports
69 *
70 * A path consists of a number of hops (see tb_path_hop). To establish a PCIe
71 * tunnel two paths have to be created between the two PCIe ports.
72 *
73 */
74struct tb_path {
75 struct tb *tb;
76 int nfc_credits; /* non flow controlled credits */
77 enum tb_path_port ingress_shared_buffer;
78 enum tb_path_port egress_shared_buffer;
79 enum tb_path_port ingress_fc_enable;
80 enum tb_path_port egress_fc_enable;
81
82 int priority:3;
83 int weight:4;
84 bool drop_packages;
85 bool activated;
86 struct tb_path_hop *hops;
87 int path_length; /* number of hops */
88};
89
90
d6cc51cd
AN
91/**
92 * struct tb - main thunderbolt bus structure
93 */
94struct tb {
95 struct mutex lock; /*
96 * Big lock. Must be held when accessing cfg or
97 * any struct tb_switch / struct tb_port.
98 */
99 struct tb_nhi *nhi;
100 struct tb_ctl *ctl;
101 struct workqueue_struct *wq; /* ordered workqueue for plug events */
a25c8b2f 102 struct tb_switch *root_switch;
3364f0c1 103 struct list_head tunnel_list; /* list of active PCIe tunnels */
d6cc51cd
AN
104 bool hotplug_active; /*
105 * tb_handle_hotplug will stop progressing plug
106 * events and exit if this is not set (it needs to
107 * acquire the lock one more time). Used to drain
108 * wq after cfg has been paused.
109 */
110
111};
112
a25c8b2f
AN
113/* helper functions & macros */
114
115/**
116 * tb_upstream_port() - return the upstream port of a switch
117 *
118 * Every switch has an upstream port (for the root switch it is the NHI).
119 *
120 * During switch alloc/init tb_upstream_port()->remote may be NULL, even for
121 * non root switches (on the NHI port remote is always NULL).
122 *
123 * Return: Returns the upstream port of the switch.
124 */
125static inline struct tb_port *tb_upstream_port(struct tb_switch *sw)
126{
127 return &sw->ports[sw->config.upstream_port_number];
128}
129
130static inline u64 tb_route(struct tb_switch *sw)
131{
132 return ((u64) sw->config.route_hi) << 32 | sw->config.route_lo;
133}
134
135static inline int tb_sw_read(struct tb_switch *sw, void *buffer,
136 enum tb_cfg_space space, u32 offset, u32 length)
137{
138 return tb_cfg_read(sw->tb->ctl,
139 buffer,
140 tb_route(sw),
141 0,
142 space,
143 offset,
144 length);
145}
146
147static inline int tb_sw_write(struct tb_switch *sw, void *buffer,
148 enum tb_cfg_space space, u32 offset, u32 length)
149{
150 return tb_cfg_write(sw->tb->ctl,
151 buffer,
152 tb_route(sw),
153 0,
154 space,
155 offset,
156 length);
157}
158
159static inline int tb_port_read(struct tb_port *port, void *buffer,
160 enum tb_cfg_space space, u32 offset, u32 length)
161{
162 return tb_cfg_read(port->sw->tb->ctl,
163 buffer,
164 tb_route(port->sw),
165 port->port,
166 space,
167 offset,
168 length);
169}
170
171static inline int tb_port_write(struct tb_port *port, void *buffer,
172 enum tb_cfg_space space, u32 offset, u32 length)
173{
174 return tb_cfg_write(port->sw->tb->ctl,
175 buffer,
176 tb_route(port->sw),
177 port->port,
178 space,
179 offset,
180 length);
181}
182
183#define tb_err(tb, fmt, arg...) dev_err(&(tb)->nhi->pdev->dev, fmt, ## arg)
184#define tb_WARN(tb, fmt, arg...) dev_WARN(&(tb)->nhi->pdev->dev, fmt, ## arg)
185#define tb_warn(tb, fmt, arg...) dev_warn(&(tb)->nhi->pdev->dev, fmt, ## arg)
186#define tb_info(tb, fmt, arg...) dev_info(&(tb)->nhi->pdev->dev, fmt, ## arg)
187
188
189#define __TB_SW_PRINT(level, sw, fmt, arg...) \
190 do { \
191 struct tb_switch *__sw = (sw); \
192 level(__sw->tb, "%llx: " fmt, \
193 tb_route(__sw), ## arg); \
194 } while (0)
195#define tb_sw_WARN(sw, fmt, arg...) __TB_SW_PRINT(tb_WARN, sw, fmt, ##arg)
196#define tb_sw_warn(sw, fmt, arg...) __TB_SW_PRINT(tb_warn, sw, fmt, ##arg)
197#define tb_sw_info(sw, fmt, arg...) __TB_SW_PRINT(tb_info, sw, fmt, ##arg)
198
199
200#define __TB_PORT_PRINT(level, _port, fmt, arg...) \
201 do { \
202 struct tb_port *__port = (_port); \
203 level(__port->sw->tb, "%llx:%x: " fmt, \
204 tb_route(__port->sw), __port->port, ## arg); \
205 } while (0)
206#define tb_port_WARN(port, fmt, arg...) \
207 __TB_PORT_PRINT(tb_WARN, port, fmt, ##arg)
208#define tb_port_warn(port, fmt, arg...) \
209 __TB_PORT_PRINT(tb_warn, port, fmt, ##arg)
210#define tb_port_info(port, fmt, arg...) \
211 __TB_PORT_PRINT(tb_info, port, fmt, ##arg)
212
213
d6cc51cd
AN
214struct tb *thunderbolt_alloc_and_start(struct tb_nhi *nhi);
215void thunderbolt_shutdown_and_free(struct tb *tb);
216
a25c8b2f
AN
217struct tb_switch *tb_switch_alloc(struct tb *tb, u64 route);
218void tb_switch_free(struct tb_switch *sw);
053596d9
AN
219void tb_sw_set_unpplugged(struct tb_switch *sw);
220struct tb_switch *get_switch_at_route(struct tb_switch *sw, u64 route);
a25c8b2f 221
9da672a4 222int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged);
520b6702
AN
223int tb_port_add_nfc_credits(struct tb_port *port, int credits);
224int tb_port_clear_counter(struct tb_port *port, int counter);
9da672a4 225
e2b8785e
AN
226int tb_find_cap(struct tb_port *port, enum tb_cfg_space space, u32 value);
227
520b6702
AN
228struct tb_path *tb_path_alloc(struct tb *tb, int num_hops);
229void tb_path_free(struct tb_path *path);
230int tb_path_activate(struct tb_path *path);
231void tb_path_deactivate(struct tb_path *path);
232bool tb_path_is_invalid(struct tb_path *path);
233
a25c8b2f
AN
234
235static inline int tb_route_length(u64 route)
236{
237 return (fls64(route) + TB_ROUTE_SHIFT - 1) / TB_ROUTE_SHIFT;
238}
239
240static inline bool tb_is_upstream_port(struct tb_port *port)
241{
242 return port == tb_upstream_port(port->sw);
243}
244
9da672a4
AN
245/**
246 * tb_downstream_route() - get route to downstream switch
247 *
248 * Port must not be the upstream port (otherwise a loop is created).
249 *
250 * Return: Returns a route to the switch behind @port.
251 */
252static inline u64 tb_downstream_route(struct tb_port *port)
253{
254 return tb_route(port->sw)
255 | ((u64) port->port << (port->sw->config.depth * 8));
256}
257
d6cc51cd 258#endif
This page took 0.035414 seconds and 5 git commands to generate.