1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt driver - path/tunnel functionality
5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
6 * Copyright (C) 2019, Intel Corporation
9 #include <linux/slab.h>
10 #include <linux/errno.h>
11 #include <linux/delay.h>
12 #include <linux/ktime.h>
16 static void tb_dump_hop(const struct tb_path_hop *hop, const struct tb_regs_hop *regs)
18 const struct tb_port *port = hop->in_port;
20 tb_port_dbg(port, " In HopID: %d => Out port: %d Out HopID: %d\n",
21 hop->in_hop_index, regs->out_port, regs->next_hop);
22 tb_port_dbg(port, " Weight: %d Priority: %d Credits: %d Drop: %d\n",
23 regs->weight, regs->priority,
24 regs->initial_credits, regs->drop_packages);
25 tb_port_dbg(port, " Counter enabled: %d Counter index: %d\n",
26 regs->counter_enable, regs->counter);
27 tb_port_dbg(port, " Flow Control (In/Eg): %d/%d Shared Buffer (In/Eg): %d/%d\n",
28 regs->ingress_fc, regs->egress_fc,
29 regs->ingress_shared_buffer, regs->egress_shared_buffer);
30 tb_port_dbg(port, " Unknown1: %#x Unknown2: %#x Unknown3: %#x\n",
31 regs->unknown1, regs->unknown2, regs->unknown3);
34 static struct tb_port *tb_path_find_dst_port(struct tb_port *src, int src_hopid,
37 struct tb_port *port, *out_port = NULL;
38 struct tb_regs_hop hop;
45 for (i = 0; port && i < TB_PATH_MAX_HOPS; i++) {
48 ret = tb_port_read(port, &hop, TB_CFG_HOPS, 2 * hopid, 2);
50 tb_port_warn(port, "failed to read path at %d\n", hopid);
57 out_port = &sw->ports[hop.out_port];
59 port = out_port->remote;
62 return out_port && hopid == dst_hopid ? out_port : NULL;
65 static int tb_path_find_src_hopid(struct tb_port *src,
66 const struct tb_port *dst, int dst_hopid)
71 for (i = TB_PATH_MIN_HOPID; i <= src->config.max_in_hop_id; i++) {
72 out = tb_path_find_dst_port(src, i, dst_hopid);
81 * tb_path_discover() - Discover a path
82 * @src: First input port of a path
83 * @src_hopid: Starting HopID of a path (%-1 if don't care)
84 * @dst: Expected destination port of the path (%NULL if don't care)
85 * @dst_hopid: HopID to the @dst (%-1 if don't care)
86 * @last: Last port is filled here if not %NULL
87 * @name: Name of the path
88 * @alloc_hopid: Allocate HopIDs for the ports
90 * Follows a path starting from @src and @src_hopid to the last output
91 * port of the path. Allocates HopIDs for the visited ports (if
92 * @alloc_hopid is true). Call tb_path_free() to release the path and
93 * allocated HopIDs when the path is not needed anymore.
95 * Note function discovers also incomplete paths so caller should check
96 * that the @dst port is the expected one. If it is not, the path can be
97 * cleaned up by calling tb_path_deactivate() before tb_path_free().
99 * Return: Discovered path on success, %NULL in case of failure
101 struct tb_path *tb_path_discover(struct tb_port *src, int src_hopid,
102 struct tb_port *dst, int dst_hopid,
103 struct tb_port **last, const char *name,
106 struct tb_port *out_port;
107 struct tb_regs_hop hop;
108 struct tb_path *path;
109 struct tb_switch *sw;
114 if (src_hopid < 0 && dst) {
116 * For incomplete paths the intermediate HopID can be
117 * different from the one used by the protocol adapter
118 * so in that case find a path that ends on @dst with
119 * matching @dst_hopid. That should give us the correct
120 * HopID for the @src.
122 src_hopid = tb_path_find_src_hopid(src, dst, dst_hopid);
131 for (i = 0; p && i < TB_PATH_MAX_HOPS; i++) {
134 ret = tb_port_read(p, &hop, TB_CFG_HOPS, 2 * h, 2);
136 tb_port_warn(p, "failed to read path at %d\n", h);
140 /* If the hop is not enabled we got an incomplete path */
144 out_port = &sw->ports[hop.out_port];
149 p = out_port->remote;
153 path = kzalloc(sizeof(*path), GFP_KERNEL);
158 path->tb = src->sw->tb;
159 path->path_length = num_hops;
160 path->activated = true;
161 path->alloc_hopid = alloc_hopid;
163 path->hops = kcalloc(num_hops, sizeof(*path->hops), GFP_KERNEL);
172 for (i = 0; i < num_hops; i++) {
177 ret = tb_port_read(p, &hop, TB_CFG_HOPS, 2 * h, 2);
179 tb_port_warn(p, "failed to read path at %d\n", h);
183 if (alloc_hopid && tb_port_alloc_in_hopid(p, h, h) < 0)
186 out_port = &sw->ports[hop.out_port];
187 next_hop = hop.next_hop;
190 tb_port_alloc_out_hopid(out_port, next_hop, next_hop) < 0) {
191 tb_port_release_in_hopid(p, h);
195 path->hops[i].in_port = p;
196 path->hops[i].in_hop_index = h;
197 path->hops[i].in_counter_index = -1;
198 path->hops[i].out_port = out_port;
199 path->hops[i].next_hop_index = next_hop;
202 p = out_port->remote;
208 tb_port_warn(src, "failed to discover path starting at HopID %d\n",
215 * tb_path_alloc() - allocate a thunderbolt path between two ports
216 * @tb: Domain pointer
217 * @src: Source port of the path
218 * @src_hopid: HopID used for the first ingress port in the path
219 * @dst: Destination port of the path
220 * @dst_hopid: HopID used for the last egress port in the path
221 * @link_nr: Preferred link if there are dual links on the path
222 * @name: Name of the path
224 * Creates path between two ports starting with given @src_hopid. Reserves
225 * HopIDs for each port (they can be different from @src_hopid depending on
226 * how many HopIDs each port already have reserved). If there are dual
227 * links on the path, prioritizes using @link_nr but takes into account
228 * that the lanes may be bonded.
230 * Return: Returns a tb_path on success or NULL on failure.
232 struct tb_path *tb_path_alloc(struct tb *tb, struct tb_port *src, int src_hopid,
233 struct tb_port *dst, int dst_hopid, int link_nr,
236 struct tb_port *in_port, *out_port, *first_port, *last_port;
237 int in_hopid, out_hopid;
238 struct tb_path *path;
242 path = kzalloc(sizeof(*path), GFP_KERNEL);
246 first_port = last_port = NULL;
248 tb_for_each_port_on_path(src, dst, in_port) {
250 first_port = in_port;
255 /* Check that src and dst are reachable */
256 if (first_port != src || last_port != dst) {
261 /* Each hop takes two ports */
264 path->hops = kcalloc(num_hops, sizeof(*path->hops), GFP_KERNEL);
270 path->alloc_hopid = true;
272 in_hopid = src_hopid;
275 for (i = 0; i < num_hops; i++) {
276 in_port = tb_next_port_on_path(src, dst, out_port);
280 /* When lanes are bonded primary link must be used */
281 if (!in_port->bonded && in_port->dual_link_port &&
282 in_port->link_nr != link_nr)
283 in_port = in_port->dual_link_port;
285 ret = tb_port_alloc_in_hopid(in_port, in_hopid, in_hopid);
290 out_port = tb_next_port_on_path(src, dst, in_port);
295 * Pick up right port when going from non-bonded to
296 * bonded or from bonded to non-bonded.
298 if (out_port->dual_link_port) {
299 if (!in_port->bonded && out_port->bonded &&
302 * Use primary link when going from
303 * non-bonded to bonded.
305 out_port = out_port->dual_link_port;
306 } else if (!out_port->bonded &&
307 out_port->link_nr != link_nr) {
309 * If out port is not bonded follow
312 out_port = out_port->dual_link_port;
316 if (i == num_hops - 1)
317 ret = tb_port_alloc_out_hopid(out_port, dst_hopid,
320 ret = tb_port_alloc_out_hopid(out_port, -1, -1);
326 path->hops[i].in_hop_index = in_hopid;
327 path->hops[i].in_port = in_port;
328 path->hops[i].in_counter_index = -1;
329 path->hops[i].out_port = out_port;
330 path->hops[i].next_hop_index = out_hopid;
332 in_hopid = out_hopid;
336 path->path_length = num_hops;
347 * tb_path_free() - free a path
348 * @path: Path to free
350 * Frees a path. The path does not need to be deactivated.
352 void tb_path_free(struct tb_path *path)
354 if (path->alloc_hopid) {
357 for (i = 0; i < path->path_length; i++) {
358 const struct tb_path_hop *hop = &path->hops[i];
361 tb_port_release_in_hopid(hop->in_port,
364 tb_port_release_out_hopid(hop->out_port,
365 hop->next_hop_index);
373 static void __tb_path_deallocate_nfc(struct tb_path *path, int first_hop)
376 for (i = first_hop; i < path->path_length; i++) {
377 res = tb_port_add_nfc_credits(path->hops[i].in_port,
378 -path->hops[i].nfc_credits);
380 tb_port_warn(path->hops[i].in_port,
381 "nfc credits deallocation failed for hop %d\n",
386 static int __tb_path_deactivate_hop(struct tb_port *port, int hop_index,
389 struct tb_regs_hop hop;
393 /* Disable the path */
394 ret = tb_port_read(port, &hop, TB_CFG_HOPS, 2 * hop_index, 2);
398 /* Already disabled */
404 ret = tb_port_write(port, &hop, TB_CFG_HOPS, 2 * hop_index, 2);
408 /* Wait until it is drained */
409 timeout = ktime_add_ms(ktime_get(), 500);
411 ret = tb_port_read(port, &hop, TB_CFG_HOPS, 2 * hop_index, 2);
418 * Clear flow control. Protocol adapters
419 * IFC and ISE bits are vendor defined
420 * in the USB4 spec so we clear them
421 * only for pre-USB4 adapters.
423 if (!tb_switch_is_usb4(port->sw)) {
425 hop.ingress_shared_buffer = 0;
428 hop.egress_shared_buffer = 0;
430 return tb_port_write(port, &hop, TB_CFG_HOPS,
437 usleep_range(10, 20);
438 } while (ktime_before(ktime_get(), timeout));
443 static void __tb_path_deactivate_hops(struct tb_path *path, int first_hop)
447 for (i = first_hop; i < path->path_length; i++) {
448 res = __tb_path_deactivate_hop(path->hops[i].in_port,
449 path->hops[i].in_hop_index,
451 if (res && res != -ENODEV)
452 tb_port_warn(path->hops[i].in_port,
453 "hop deactivation failed for hop %d, index %d\n",
454 i, path->hops[i].in_hop_index);
458 void tb_path_deactivate(struct tb_path *path)
460 if (!path->activated) {
461 tb_WARN(path->tb, "trying to deactivate an inactive path\n");
465 "deactivating %s path from %llx:%u to %llx:%u\n",
466 path->name, tb_route(path->hops[0].in_port->sw),
467 path->hops[0].in_port->port,
468 tb_route(path->hops[path->path_length - 1].out_port->sw),
469 path->hops[path->path_length - 1].out_port->port);
470 __tb_path_deactivate_hops(path, 0);
471 __tb_path_deallocate_nfc(path, 0);
472 path->activated = false;
476 * tb_path_activate() - activate a path
477 * @path: Path to activate
479 * Activate a path starting with the last hop and iterating backwards. The
480 * caller must fill path->hops before calling tb_path_activate().
482 * Return: Returns 0 on success or an error code on failure.
484 int tb_path_activate(struct tb_path *path)
487 enum tb_path_port out_mask, in_mask;
488 if (path->activated) {
489 tb_WARN(path->tb, "trying to activate already activated path\n");
494 "activating %s path from %llx:%u to %llx:%u\n",
495 path->name, tb_route(path->hops[0].in_port->sw),
496 path->hops[0].in_port->port,
497 tb_route(path->hops[path->path_length - 1].out_port->sw),
498 path->hops[path->path_length - 1].out_port->port);
500 /* Clear counters. */
501 for (i = path->path_length - 1; i >= 0; i--) {
502 if (path->hops[i].in_counter_index == -1)
504 res = tb_port_clear_counter(path->hops[i].in_port,
505 path->hops[i].in_counter_index);
510 /* Add non flow controlled credits. */
511 for (i = path->path_length - 1; i >= 0; i--) {
512 res = tb_port_add_nfc_credits(path->hops[i].in_port,
513 path->hops[i].nfc_credits);
515 __tb_path_deallocate_nfc(path, i);
521 for (i = path->path_length - 1; i >= 0; i--) {
522 struct tb_regs_hop hop = { 0 };
524 /* If it is left active deactivate it first */
525 __tb_path_deactivate_hop(path->hops[i].in_port,
526 path->hops[i].in_hop_index, path->clear_fc);
529 hop.next_hop = path->hops[i].next_hop_index;
530 hop.out_port = path->hops[i].out_port->port;
531 hop.initial_credits = path->hops[i].initial_credits;
536 out_mask = (i == path->path_length - 1) ?
537 TB_PATH_DESTINATION : TB_PATH_INTERNAL;
538 in_mask = (i == 0) ? TB_PATH_SOURCE : TB_PATH_INTERNAL;
539 hop.weight = path->weight;
541 hop.priority = path->priority;
542 hop.drop_packages = path->drop_packages;
543 hop.counter = path->hops[i].in_counter_index;
544 hop.counter_enable = path->hops[i].in_counter_index != -1;
545 hop.ingress_fc = path->ingress_fc_enable & in_mask;
546 hop.egress_fc = path->egress_fc_enable & out_mask;
547 hop.ingress_shared_buffer = path->ingress_shared_buffer
549 hop.egress_shared_buffer = path->egress_shared_buffer
553 tb_port_dbg(path->hops[i].in_port, "Writing hop %d\n", i);
554 tb_dump_hop(&path->hops[i], &hop);
555 res = tb_port_write(path->hops[i].in_port, &hop, TB_CFG_HOPS,
556 2 * path->hops[i].in_hop_index, 2);
558 __tb_path_deactivate_hops(path, i);
559 __tb_path_deallocate_nfc(path, 0);
563 path->activated = true;
564 tb_dbg(path->tb, "path activation complete\n");
567 tb_WARN(path->tb, "path activation failed\n");
572 * tb_path_is_invalid() - check whether any ports on the path are invalid
573 * @path: Path to check
575 * Return: Returns true if the path is invalid, false otherwise.
577 bool tb_path_is_invalid(struct tb_path *path)
580 for (i = 0; i < path->path_length; i++) {
581 if (path->hops[i].in_port->sw->is_unplugged)
583 if (path->hops[i].out_port->sw->is_unplugged)
590 * tb_path_port_on_path() - Does the path go through certain port
591 * @path: Path to check
592 * @port: Switch to check
594 * Goes over all hops on path and checks if @port is any of them.
595 * Direction does not matter.
597 bool tb_path_port_on_path(const struct tb_path *path, const struct tb_port *port)
601 for (i = 0; i < path->path_length; i++) {
602 if (path->hops[i].in_port == port ||
603 path->hops[i].out_port == port)