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