1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt Cactus Ridge driver - path/tunnel functionality
5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
8 #include <linux/slab.h>
9 #include <linux/errno.h>
10 #include <linux/delay.h>
11 #include <linux/ktime.h>
16 static void tb_dump_hop(struct tb_port *port, struct tb_regs_hop *hop)
18 tb_port_dbg(port, " Hop through port %d to hop %d (%s)\n",
19 hop->out_port, hop->next_hop,
20 hop->enable ? "enabled" : "disabled");
21 tb_port_dbg(port, " Weight: %d Priority: %d Credits: %d Drop: %d\n",
22 hop->weight, hop->priority,
23 hop->initial_credits, hop->drop_packages);
24 tb_port_dbg(port, " Counter enabled: %d Counter index: %d\n",
25 hop->counter_enable, hop->counter);
26 tb_port_dbg(port, " Flow Control (In/Eg): %d/%d Shared Buffer (In/Eg): %d/%d\n",
27 hop->ingress_fc, hop->egress_fc,
28 hop->ingress_shared_buffer, hop->egress_shared_buffer);
29 tb_port_dbg(port, " Unknown1: %#x Unknown2: %#x Unknown3: %#x\n",
30 hop->unknown1, hop->unknown2, hop->unknown3);
34 * tb_path_alloc() - allocate a thunderbolt path between two ports
36 * @src: Source port of the path
37 * @src_hopid: HopID used for the first ingress port in the path
38 * @dst: Destination port of the path
39 * @dst_hopid: HopID used for the last egress port in the path
40 * @link_nr: Preferred link if there are dual links on the path
41 * @name: Name of the path
43 * Creates path between two ports starting with given @src_hopid. Reserves
44 * HopIDs for each port (they can be different from @src_hopid depending on
45 * how many HopIDs each port already have reserved). If there are dual
46 * links on the path, prioritizes using @link_nr.
48 * Return: Returns a tb_path on success or NULL on failure.
50 struct tb_path *tb_path_alloc(struct tb *tb, struct tb_port *src, int src_hopid,
51 struct tb_port *dst, int dst_hopid, int link_nr,
54 struct tb_port *in_port, *out_port;
55 int in_hopid, out_hopid;
60 path = kzalloc(sizeof(*path), GFP_KERNEL);
65 * Number of hops on a path is the distance between the two
66 * switches plus the source adapter port.
68 num_hops = abs(tb_route_length(tb_route(src->sw)) -
69 tb_route_length(tb_route(dst->sw))) + 1;
71 path->hops = kcalloc(num_hops, sizeof(*path->hops), GFP_KERNEL);
80 for (i = 0; i < num_hops; i++) {
81 in_port = tb_next_port_on_path(src, dst, out_port);
85 if (in_port->dual_link_port && in_port->link_nr != link_nr)
86 in_port = in_port->dual_link_port;
88 ret = tb_port_alloc_in_hopid(in_port, in_hopid, in_hopid);
93 out_port = tb_next_port_on_path(src, dst, in_port);
97 if (out_port->dual_link_port && out_port->link_nr != link_nr)
98 out_port = out_port->dual_link_port;
100 if (i == num_hops - 1)
101 ret = tb_port_alloc_out_hopid(out_port, dst_hopid,
104 ret = tb_port_alloc_out_hopid(out_port, -1, -1);
110 path->hops[i].in_hop_index = in_hopid;
111 path->hops[i].in_port = in_port;
112 path->hops[i].in_counter_index = -1;
113 path->hops[i].out_port = out_port;
114 path->hops[i].next_hop_index = out_hopid;
116 in_hopid = out_hopid;
120 path->path_length = num_hops;
131 * tb_path_free() - free a deactivated path
133 void tb_path_free(struct tb_path *path)
137 if (path->activated) {
138 tb_WARN(path->tb, "trying to free an activated path\n")
142 for (i = 0; i < path->path_length; i++) {
143 const struct tb_path_hop *hop = &path->hops[i];
146 tb_port_release_in_hopid(hop->in_port,
149 tb_port_release_out_hopid(hop->out_port,
150 hop->next_hop_index);
157 static void __tb_path_deallocate_nfc(struct tb_path *path, int first_hop)
160 for (i = first_hop; i < path->path_length; i++) {
161 res = tb_port_add_nfc_credits(path->hops[i].in_port,
164 tb_port_warn(path->hops[i].in_port,
165 "nfc credits deallocation failed for hop %d\n",
170 static int __tb_path_deactivate_hop(struct tb_port *port, int hop_index)
172 struct tb_regs_hop hop;
176 /* Disable the path */
177 ret = tb_port_read(port, &hop, TB_CFG_HOPS, 2 * hop_index, 2);
181 /* Already disabled */
187 ret = tb_port_write(port, &hop, TB_CFG_HOPS, 2 * hop_index, 2);
191 /* Wait until it is drained */
192 timeout = ktime_add_ms(ktime_get(), 500);
194 ret = tb_port_read(port, &hop, TB_CFG_HOPS, 2 * hop_index, 2);
201 usleep_range(10, 20);
202 } while (ktime_before(ktime_get(), timeout));
207 static void __tb_path_deactivate_hops(struct tb_path *path, int first_hop)
211 for (i = first_hop; i < path->path_length; i++) {
212 res = __tb_path_deactivate_hop(path->hops[i].in_port,
213 path->hops[i].in_hop_index);
214 if (res && res != -ENODEV)
215 tb_port_warn(path->hops[i].in_port,
216 "hop deactivation failed for hop %d, index %d\n",
217 i, path->hops[i].in_hop_index);
221 void tb_path_deactivate(struct tb_path *path)
223 if (!path->activated) {
224 tb_WARN(path->tb, "trying to deactivate an inactive path\n");
228 "deactivating %s path from %llx:%x to %llx:%x\n",
229 path->name, tb_route(path->hops[0].in_port->sw),
230 path->hops[0].in_port->port,
231 tb_route(path->hops[path->path_length - 1].out_port->sw),
232 path->hops[path->path_length - 1].out_port->port);
233 __tb_path_deactivate_hops(path, 0);
234 __tb_path_deallocate_nfc(path, 0);
235 path->activated = false;
239 * tb_path_activate() - activate a path
241 * Activate a path starting with the last hop and iterating backwards. The
242 * caller must fill path->hops before calling tb_path_activate().
244 * Return: Returns 0 on success or an error code on failure.
246 int tb_path_activate(struct tb_path *path)
249 enum tb_path_port out_mask, in_mask;
250 if (path->activated) {
251 tb_WARN(path->tb, "trying to activate already activated path\n");
256 "activating %s path from %llx:%x to %llx:%x\n",
257 path->name, tb_route(path->hops[0].in_port->sw),
258 path->hops[0].in_port->port,
259 tb_route(path->hops[path->path_length - 1].out_port->sw),
260 path->hops[path->path_length - 1].out_port->port);
262 /* Clear counters. */
263 for (i = path->path_length - 1; i >= 0; i--) {
264 if (path->hops[i].in_counter_index == -1)
266 res = tb_port_clear_counter(path->hops[i].in_port,
267 path->hops[i].in_counter_index);
272 /* Add non flow controlled credits. */
273 for (i = path->path_length - 1; i >= 0; i--) {
274 res = tb_port_add_nfc_credits(path->hops[i].in_port,
277 __tb_path_deallocate_nfc(path, i);
283 for (i = path->path_length - 1; i >= 0; i--) {
284 struct tb_regs_hop hop = { 0 };
287 * We do (currently) not tear down paths setup by the firmeware.
288 * If a firmware device is unplugged and plugged in again then
289 * it can happen that we reuse some of the hops from the (now
290 * defunct) firmeware path. This causes the hotplug operation to
291 * fail (the pci device does not show up). Clearing the hop
292 * before overwriting it fixes the problem.
294 * Should be removed once we discover and tear down firmeware
297 res = tb_port_write(path->hops[i].in_port, &hop, TB_CFG_HOPS,
298 2 * path->hops[i].in_hop_index, 2);
300 __tb_path_deactivate_hops(path, i);
301 __tb_path_deallocate_nfc(path, 0);
306 hop.next_hop = path->hops[i].next_hop_index;
307 hop.out_port = path->hops[i].out_port->port;
308 /* TODO: figure out why these are good values */
309 hop.initial_credits = (i == path->path_length - 1) ? 16 : 7;
314 out_mask = (i == path->path_length - 1) ?
315 TB_PATH_DESTINATION : TB_PATH_INTERNAL;
316 in_mask = (i == 0) ? TB_PATH_SOURCE : TB_PATH_INTERNAL;
317 hop.weight = path->weight;
319 hop.priority = path->priority;
320 hop.drop_packages = path->drop_packages;
321 hop.counter = path->hops[i].in_counter_index;
322 hop.counter_enable = path->hops[i].in_counter_index != -1;
323 hop.ingress_fc = path->ingress_fc_enable & in_mask;
324 hop.egress_fc = path->egress_fc_enable & out_mask;
325 hop.ingress_shared_buffer = path->ingress_shared_buffer
327 hop.egress_shared_buffer = path->egress_shared_buffer
331 tb_port_info(path->hops[i].in_port, "Writing hop %d, index %d",
332 i, path->hops[i].in_hop_index);
333 tb_dump_hop(path->hops[i].in_port, &hop);
334 res = tb_port_write(path->hops[i].in_port, &hop, TB_CFG_HOPS,
335 2 * path->hops[i].in_hop_index, 2);
337 __tb_path_deactivate_hops(path, i);
338 __tb_path_deallocate_nfc(path, 0);
342 path->activated = true;
343 tb_info(path->tb, "path activation complete\n");
346 tb_WARN(path->tb, "path activation failed\n");
351 * tb_path_is_invalid() - check whether any ports on the path are invalid
353 * Return: Returns true if the path is invalid, false otherwise.
355 bool tb_path_is_invalid(struct tb_path *path)
358 for (i = 0; i < path->path_length; i++) {
359 if (path->hops[i].in_port->sw->is_unplugged)
361 if (path->hops[i].out_port->sw->is_unplugged)