Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Thunderbolt driver - path/tunnel functionality
  4 *
  5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
  6 * Copyright (C) 2019, Intel Corporation
  7 */
  8
  9#include <linux/slab.h>
 10#include <linux/errno.h>
 11#include <linux/delay.h>
 12#include <linux/ktime.h>
 13
 14#include "tb.h"
 15
 16static void tb_dump_hop(const struct tb_path_hop *hop, const struct tb_regs_hop *regs)
 17{
 18	const struct tb_port *port = hop->in_port;
 19
 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 PM: %d\n",
 23		    regs->weight, regs->priority, regs->initial_credits,
 24		    regs->drop_packages, regs->pmps);
 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);
 32}
 33
 34static struct tb_port *tb_path_find_dst_port(struct tb_port *src, int src_hopid,
 35					     int dst_hopid)
 36{
 37	struct tb_port *port, *out_port = NULL;
 38	struct tb_regs_hop hop;
 39	struct tb_switch *sw;
 40	int i, ret, hopid;
 41
 42	hopid = src_hopid;
 43	port = src;
 44
 45	for (i = 0; port && i < TB_PATH_MAX_HOPS; i++) {
 46		sw = port->sw;
 47
 48		ret = tb_port_read(port, &hop, TB_CFG_HOPS, 2 * hopid, 2);
 49		if (ret) {
 50			tb_port_warn(port, "failed to read path at %d\n", hopid);
 51			return NULL;
 52		}
 53
 54		if (!hop.enable)
 55			return NULL;
 56
 57		out_port = &sw->ports[hop.out_port];
 58		hopid = hop.next_hop;
 59		port = out_port->remote;
 60	}
 61
 62	return out_port && hopid == dst_hopid ? out_port : NULL;
 63}
 64
 65static int tb_path_find_src_hopid(struct tb_port *src,
 66	const struct tb_port *dst, int dst_hopid)
 67{
 68	struct tb_port *out;
 69	int i;
 70
 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);
 73		if (out == dst)
 74			return i;
 75	}
 76
 77	return 0;
 78}
 79
 80/**
 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
 89 *
 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.
 94 *
 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().
 98 *
 99 * Return: Discovered path on success, %NULL in case of failure
100 */
101struct 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,
104				 bool alloc_hopid)
105{
106	struct tb_port *out_port;
107	struct tb_regs_hop hop;
108	struct tb_path *path;
109	struct tb_switch *sw;
110	struct tb_port *p;
111	size_t num_hops;
112	int ret, i, h;
113
114	if (src_hopid < 0 && dst) {
115		/*
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.
121		 */
122		src_hopid = tb_path_find_src_hopid(src, dst, dst_hopid);
123		if (!src_hopid)
124			return NULL;
125	}
126
127	p = src;
128	h = src_hopid;
129	num_hops = 0;
130
131	for (i = 0; p && i < TB_PATH_MAX_HOPS; i++) {
132		sw = p->sw;
133
134		ret = tb_port_read(p, &hop, TB_CFG_HOPS, 2 * h, 2);
135		if (ret) {
136			tb_port_warn(p, "failed to read path at %d\n", h);
137			return NULL;
138		}
139
140		/* If the hop is not enabled we got an incomplete path */
141		if (!hop.enable)
142			break;
143
144		out_port = &sw->ports[hop.out_port];
145		if (last)
146			*last = out_port;
147
148		h = hop.next_hop;
149		p = out_port->remote;
150		num_hops++;
151	}
152
153	path = kzalloc(sizeof(*path), GFP_KERNEL);
154	if (!path)
155		return NULL;
156
157	path->name = name;
158	path->tb = src->sw->tb;
159	path->path_length = num_hops;
160	path->activated = true;
161	path->alloc_hopid = alloc_hopid;
162
163	path->hops = kcalloc(num_hops, sizeof(*path->hops), GFP_KERNEL);
164	if (!path->hops) {
165		kfree(path);
166		return NULL;
167	}
168
169	tb_dbg(path->tb, "discovering %s path starting from %llx:%u\n",
170	       path->name, tb_route(src->sw), src->port);
171
172	p = src;
173	h = src_hopid;
174
175	for (i = 0; i < num_hops; i++) {
176		int next_hop;
177
178		sw = p->sw;
179
180		ret = tb_port_read(p, &hop, TB_CFG_HOPS, 2 * h, 2);
181		if (ret) {
182			tb_port_warn(p, "failed to read path at %d\n", h);
183			goto err;
184		}
185
186		if (alloc_hopid && tb_port_alloc_in_hopid(p, h, h) < 0)
187			goto err;
188
189		out_port = &sw->ports[hop.out_port];
190		next_hop = hop.next_hop;
191
192		if (alloc_hopid &&
193		    tb_port_alloc_out_hopid(out_port, next_hop, next_hop) < 0) {
194			tb_port_release_in_hopid(p, h);
195			goto err;
196		}
197
198		path->hops[i].in_port = p;
199		path->hops[i].in_hop_index = h;
200		path->hops[i].in_counter_index = -1;
201		path->hops[i].out_port = out_port;
202		path->hops[i].next_hop_index = next_hop;
203
204		tb_dump_hop(&path->hops[i], &hop);
205
206		h = next_hop;
207		p = out_port->remote;
208	}
209
210	tb_dbg(path->tb, "path discovery complete\n");
211	return path;
212
213err:
214	tb_port_warn(src, "failed to discover path starting at HopID %d\n",
215		     src_hopid);
216	tb_path_free(path);
217	return NULL;
218}
219
220/**
221 * tb_path_alloc() - allocate a thunderbolt path between two ports
222 * @tb: Domain pointer
223 * @src: Source port of the path
224 * @src_hopid: HopID used for the first ingress port in the path
225 * @dst: Destination port of the path
226 * @dst_hopid: HopID used for the last egress port in the path
227 * @link_nr: Preferred link if there are dual links on the path
228 * @name: Name of the path
229 *
230 * Creates path between two ports starting with given @src_hopid. Reserves
231 * HopIDs for each port (they can be different from @src_hopid depending on
232 * how many HopIDs each port already have reserved). If there are dual
233 * links on the path, prioritizes using @link_nr but takes into account
234 * that the lanes may be bonded.
235 *
236 * Return: Returns a tb_path on success or NULL on failure.
237 */
238struct tb_path *tb_path_alloc(struct tb *tb, struct tb_port *src, int src_hopid,
239			      struct tb_port *dst, int dst_hopid, int link_nr,
240			      const char *name)
241{
242	struct tb_port *in_port, *out_port, *first_port, *last_port;
243	int in_hopid, out_hopid;
244	struct tb_path *path;
245	size_t num_hops;
246	int i, ret;
247
248	path = kzalloc(sizeof(*path), GFP_KERNEL);
249	if (!path)
250		return NULL;
251
252	first_port = last_port = NULL;
253	i = 0;
254	tb_for_each_port_on_path(src, dst, in_port) {
255		if (!first_port)
256			first_port = in_port;
257		last_port = in_port;
258		i++;
259	}
260
261	/* Check that src and dst are reachable */
262	if (first_port != src || last_port != dst) {
263		kfree(path);
264		return NULL;
265	}
266
267	/* Each hop takes two ports */
268	num_hops = i / 2;
269
270	path->hops = kcalloc(num_hops, sizeof(*path->hops), GFP_KERNEL);
271	if (!path->hops) {
272		kfree(path);
273		return NULL;
274	}
275
276	path->alloc_hopid = true;
277
278	in_hopid = src_hopid;
279	out_port = NULL;
280
281	for (i = 0; i < num_hops; i++) {
282		in_port = tb_next_port_on_path(src, dst, out_port);
283		if (!in_port)
284			goto err;
285
286		/* When lanes are bonded primary link must be used */
287		if (!in_port->bonded && in_port->dual_link_port &&
288		    in_port->link_nr != link_nr)
289			in_port = in_port->dual_link_port;
290
291		ret = tb_port_alloc_in_hopid(in_port, in_hopid, in_hopid);
292		if (ret < 0)
293			goto err;
294		in_hopid = ret;
295
296		out_port = tb_next_port_on_path(src, dst, in_port);
297		if (!out_port)
298			goto err;
299
300		/*
301		 * Pick up right port when going from non-bonded to
302		 * bonded or from bonded to non-bonded.
303		 */
304		if (out_port->dual_link_port) {
305			if (!in_port->bonded && out_port->bonded &&
306			    out_port->link_nr) {
307				/*
308				 * Use primary link when going from
309				 * non-bonded to bonded.
310				 */
311				out_port = out_port->dual_link_port;
312			} else if (!out_port->bonded &&
313				   out_port->link_nr != link_nr) {
314				/*
315				 * If out port is not bonded follow
316				 * link_nr.
317				 */
318				out_port = out_port->dual_link_port;
319			}
320		}
321
322		if (i == num_hops - 1)
323			ret = tb_port_alloc_out_hopid(out_port, dst_hopid,
324						      dst_hopid);
325		else
326			ret = tb_port_alloc_out_hopid(out_port, -1, -1);
327
328		if (ret < 0)
329			goto err;
330		out_hopid = ret;
331
332		path->hops[i].in_hop_index = in_hopid;
333		path->hops[i].in_port = in_port;
334		path->hops[i].in_counter_index = -1;
335		path->hops[i].out_port = out_port;
336		path->hops[i].next_hop_index = out_hopid;
337
338		in_hopid = out_hopid;
339	}
340
341	path->tb = tb;
342	path->path_length = num_hops;
343	path->name = name;
344
345	return path;
346
347err:
348	tb_path_free(path);
349	return NULL;
350}
351
352/**
353 * tb_path_free() - free a path
354 * @path: Path to free
355 *
356 * Frees a path. The path does not need to be deactivated.
357 */
358void tb_path_free(struct tb_path *path)
359{
360	if (path->alloc_hopid) {
361		int i;
362
363		for (i = 0; i < path->path_length; i++) {
364			const struct tb_path_hop *hop = &path->hops[i];
365
366			if (hop->in_port)
367				tb_port_release_in_hopid(hop->in_port,
368							 hop->in_hop_index);
369			if (hop->out_port)
370				tb_port_release_out_hopid(hop->out_port,
371							  hop->next_hop_index);
372		}
373	}
374
375	kfree(path->hops);
376	kfree(path);
377}
378
379static void __tb_path_deallocate_nfc(struct tb_path *path, int first_hop)
380{
381	int i, res;
382	for (i = first_hop; i < path->path_length; i++) {
383		res = tb_port_add_nfc_credits(path->hops[i].in_port,
384					      -path->hops[i].nfc_credits);
385		if (res)
386			tb_port_warn(path->hops[i].in_port,
387				     "nfc credits deallocation failed for hop %d\n",
388				     i);
389	}
390}
391
392static int __tb_path_deactivate_hop(struct tb_port *port, int hop_index,
393				    bool clear_fc)
394{
395	struct tb_regs_hop hop;
396	ktime_t timeout;
397	int ret;
398
399	/* Disable the path */
400	ret = tb_port_read(port, &hop, TB_CFG_HOPS, 2 * hop_index, 2);
401	if (ret)
402		return ret;
403
404	/* Already disabled */
405	if (!hop.enable)
406		return 0;
407
408	hop.enable = 0;
409
410	ret = tb_port_write(port, &hop, TB_CFG_HOPS, 2 * hop_index, 2);
411	if (ret)
412		return ret;
413
414	/* Wait until it is drained */
415	timeout = ktime_add_ms(ktime_get(), 500);
416	do {
417		ret = tb_port_read(port, &hop, TB_CFG_HOPS, 2 * hop_index, 2);
418		if (ret)
419			return ret;
420
421		if (!hop.pending) {
422			if (clear_fc) {
423				/*
424				 * Clear flow control. Protocol adapters
425				 * IFC and ISE bits are vendor defined
426				 * in the USB4 spec so we clear them
427				 * only for pre-USB4 adapters.
428				 */
429				if (!tb_switch_is_usb4(port->sw)) {
430					hop.ingress_fc = 0;
431					hop.ingress_shared_buffer = 0;
432				}
433				hop.egress_fc = 0;
 
434				hop.egress_shared_buffer = 0;
435
436				return tb_port_write(port, &hop, TB_CFG_HOPS,
437						     2 * hop_index, 2);
438			}
439
440			return 0;
441		}
442
443		usleep_range(10, 20);
444	} while (ktime_before(ktime_get(), timeout));
445
446	return -ETIMEDOUT;
447}
448
449static void __tb_path_deactivate_hops(struct tb_path *path, int first_hop)
450{
451	int i, res;
452
453	for (i = first_hop; i < path->path_length; i++) {
454		res = __tb_path_deactivate_hop(path->hops[i].in_port,
455					       path->hops[i].in_hop_index,
456					       path->clear_fc);
457		if (res && res != -ENODEV)
458			tb_port_warn(path->hops[i].in_port,
459				     "hop deactivation failed for hop %d, index %d\n",
460				     i, path->hops[i].in_hop_index);
461	}
462}
463
464void tb_path_deactivate(struct tb_path *path)
465{
466	if (!path->activated) {
467		tb_WARN(path->tb, "trying to deactivate an inactive path\n");
468		return;
469	}
470	tb_dbg(path->tb,
471	       "deactivating %s path from %llx:%u to %llx:%u\n",
472	       path->name, tb_route(path->hops[0].in_port->sw),
473	       path->hops[0].in_port->port,
474	       tb_route(path->hops[path->path_length - 1].out_port->sw),
475	       path->hops[path->path_length - 1].out_port->port);
476	__tb_path_deactivate_hops(path, 0);
477	__tb_path_deallocate_nfc(path, 0);
478	path->activated = false;
479}
480
481/**
482 * tb_path_activate() - activate a path
483 * @path: Path to activate
484 *
485 * Activate a path starting with the last hop and iterating backwards. The
486 * caller must fill path->hops before calling tb_path_activate().
487 *
488 * Return: Returns 0 on success or an error code on failure.
489 */
490int tb_path_activate(struct tb_path *path)
491{
492	int i, res;
493	enum tb_path_port out_mask, in_mask;
494	if (path->activated) {
495		tb_WARN(path->tb, "trying to activate already activated path\n");
496		return -EINVAL;
497	}
498
499	tb_dbg(path->tb,
500	       "activating %s path from %llx:%u to %llx:%u\n",
501	       path->name, tb_route(path->hops[0].in_port->sw),
502	       path->hops[0].in_port->port,
503	       tb_route(path->hops[path->path_length - 1].out_port->sw),
504	       path->hops[path->path_length - 1].out_port->port);
505
506	/* Clear counters. */
507	for (i = path->path_length - 1; i >= 0; i--) {
508		if (path->hops[i].in_counter_index == -1)
509			continue;
510		res = tb_port_clear_counter(path->hops[i].in_port,
511					    path->hops[i].in_counter_index);
512		if (res)
513			goto err;
514	}
515
516	/* Add non flow controlled credits. */
517	for (i = path->path_length - 1; i >= 0; i--) {
518		res = tb_port_add_nfc_credits(path->hops[i].in_port,
519					      path->hops[i].nfc_credits);
520		if (res) {
521			__tb_path_deallocate_nfc(path, i);
522			goto err;
523		}
524	}
525
526	/* Activate hops. */
527	for (i = path->path_length - 1; i >= 0; i--) {
528		struct tb_regs_hop hop = { 0 };
529
530		/* If it is left active deactivate it first */
531		__tb_path_deactivate_hop(path->hops[i].in_port,
532				path->hops[i].in_hop_index, path->clear_fc);
533
534		/* dword 0 */
535		hop.next_hop = path->hops[i].next_hop_index;
536		hop.out_port = path->hops[i].out_port->port;
537		hop.initial_credits = path->hops[i].initial_credits;
538		hop.pmps = path->hops[i].pm_support;
539		hop.unknown1 = 0;
540		hop.enable = 1;
541
542		/* dword 1 */
543		out_mask = (i == path->path_length - 1) ?
544				TB_PATH_DESTINATION : TB_PATH_INTERNAL;
545		in_mask = (i == 0) ? TB_PATH_SOURCE : TB_PATH_INTERNAL;
546		hop.weight = path->weight;
547		hop.unknown2 = 0;
548		hop.priority = path->priority;
549		hop.drop_packages = path->drop_packages;
550		hop.counter = path->hops[i].in_counter_index;
551		hop.counter_enable = path->hops[i].in_counter_index != -1;
552		hop.ingress_fc = path->ingress_fc_enable & in_mask;
553		hop.egress_fc = path->egress_fc_enable & out_mask;
554		hop.ingress_shared_buffer = path->ingress_shared_buffer
555					    & in_mask;
556		hop.egress_shared_buffer = path->egress_shared_buffer
557					    & out_mask;
558		hop.unknown3 = 0;
559
560		tb_port_dbg(path->hops[i].in_port, "Writing hop %d\n", i);
561		tb_dump_hop(&path->hops[i], &hop);
562		res = tb_port_write(path->hops[i].in_port, &hop, TB_CFG_HOPS,
563				    2 * path->hops[i].in_hop_index, 2);
564		if (res) {
565			__tb_path_deactivate_hops(path, i);
566			__tb_path_deallocate_nfc(path, 0);
567			goto err;
568		}
569	}
570	path->activated = true;
571	tb_dbg(path->tb, "path activation complete\n");
572	return 0;
573err:
574	tb_WARN(path->tb, "path activation failed\n");
575	return res;
576}
577
578/**
579 * tb_path_is_invalid() - check whether any ports on the path are invalid
580 * @path: Path to check
581 *
582 * Return: Returns true if the path is invalid, false otherwise.
583 */
584bool tb_path_is_invalid(struct tb_path *path)
585{
586	int i = 0;
587	for (i = 0; i < path->path_length; i++) {
588		if (path->hops[i].in_port->sw->is_unplugged)
589			return true;
590		if (path->hops[i].out_port->sw->is_unplugged)
591			return true;
592	}
593	return false;
594}
595
596/**
597 * tb_path_port_on_path() - Does the path go through certain port
598 * @path: Path to check
599 * @port: Switch to check
600 *
601 * Goes over all hops on path and checks if @port is any of them.
602 * Direction does not matter.
603 */
604bool tb_path_port_on_path(const struct tb_path *path, const struct tb_port *port)
605{
606	int i;
607
608	for (i = 0; i < path->path_length; i++) {
609		if (path->hops[i].in_port == port ||
610		    path->hops[i].out_port == port)
611			return true;
612	}
613
614	return false;
615}
v5.4
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Thunderbolt driver - path/tunnel functionality
  4 *
  5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
  6 * Copyright (C) 2019, Intel Corporation
  7 */
  8
  9#include <linux/slab.h>
 10#include <linux/errno.h>
 11#include <linux/delay.h>
 12#include <linux/ktime.h>
 13
 14#include "tb.h"
 15
 16static void tb_dump_hop(const struct tb_path_hop *hop, const struct tb_regs_hop *regs)
 17{
 18	const struct tb_port *port = hop->in_port;
 19
 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);
 32}
 33
 34static struct tb_port *tb_path_find_dst_port(struct tb_port *src, int src_hopid,
 35					     int dst_hopid)
 36{
 37	struct tb_port *port, *out_port = NULL;
 38	struct tb_regs_hop hop;
 39	struct tb_switch *sw;
 40	int i, ret, hopid;
 41
 42	hopid = src_hopid;
 43	port = src;
 44
 45	for (i = 0; port && i < TB_PATH_MAX_HOPS; i++) {
 46		sw = port->sw;
 47
 48		ret = tb_port_read(port, &hop, TB_CFG_HOPS, 2 * hopid, 2);
 49		if (ret) {
 50			tb_port_warn(port, "failed to read path at %d\n", hopid);
 51			return NULL;
 52		}
 53
 54		if (!hop.enable)
 55			return NULL;
 56
 57		out_port = &sw->ports[hop.out_port];
 58		hopid = hop.next_hop;
 59		port = out_port->remote;
 60	}
 61
 62	return out_port && hopid == dst_hopid ? out_port : NULL;
 63}
 64
 65static int tb_path_find_src_hopid(struct tb_port *src,
 66	const struct tb_port *dst, int dst_hopid)
 67{
 68	struct tb_port *out;
 69	int i;
 70
 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);
 73		if (out == dst)
 74			return i;
 75	}
 76
 77	return 0;
 78}
 79
 80/**
 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 *
 89 * Follows a path starting from @src and @src_hopid to the last output
 90 * port of the path. Allocates HopIDs for the visited ports. Call
 91 * tb_path_free() to release the path and allocated HopIDs when the path
 92 * is not needed anymore.
 93 *
 94 * Note function discovers also incomplete paths so caller should check
 95 * that the @dst port is the expected one. If it is not, the path can be
 96 * cleaned up by calling tb_path_deactivate() before tb_path_free().
 97 *
 98 * Return: Discovered path on success, %NULL in case of failure
 99 */
100struct tb_path *tb_path_discover(struct tb_port *src, int src_hopid,
101				 struct tb_port *dst, int dst_hopid,
102				 struct tb_port **last, const char *name)
 
103{
104	struct tb_port *out_port;
105	struct tb_regs_hop hop;
106	struct tb_path *path;
107	struct tb_switch *sw;
108	struct tb_port *p;
109	size_t num_hops;
110	int ret, i, h;
111
112	if (src_hopid < 0 && dst) {
113		/*
114		 * For incomplete paths the intermediate HopID can be
115		 * different from the one used by the protocol adapter
116		 * so in that case find a path that ends on @dst with
117		 * matching @dst_hopid. That should give us the correct
118		 * HopID for the @src.
119		 */
120		src_hopid = tb_path_find_src_hopid(src, dst, dst_hopid);
121		if (!src_hopid)
122			return NULL;
123	}
124
125	p = src;
126	h = src_hopid;
127	num_hops = 0;
128
129	for (i = 0; p && i < TB_PATH_MAX_HOPS; i++) {
130		sw = p->sw;
131
132		ret = tb_port_read(p, &hop, TB_CFG_HOPS, 2 * h, 2);
133		if (ret) {
134			tb_port_warn(p, "failed to read path at %d\n", h);
135			return NULL;
136		}
137
138		/* If the hop is not enabled we got an incomplete path */
139		if (!hop.enable)
140			break;
141
142		out_port = &sw->ports[hop.out_port];
143		if (last)
144			*last = out_port;
145
146		h = hop.next_hop;
147		p = out_port->remote;
148		num_hops++;
149	}
150
151	path = kzalloc(sizeof(*path), GFP_KERNEL);
152	if (!path)
153		return NULL;
154
155	path->name = name;
156	path->tb = src->sw->tb;
157	path->path_length = num_hops;
158	path->activated = true;
 
159
160	path->hops = kcalloc(num_hops, sizeof(*path->hops), GFP_KERNEL);
161	if (!path->hops) {
162		kfree(path);
163		return NULL;
164	}
165
 
 
 
166	p = src;
167	h = src_hopid;
168
169	for (i = 0; i < num_hops; i++) {
170		int next_hop;
171
172		sw = p->sw;
173
174		ret = tb_port_read(p, &hop, TB_CFG_HOPS, 2 * h, 2);
175		if (ret) {
176			tb_port_warn(p, "failed to read path at %d\n", h);
177			goto err;
178		}
179
180		if (tb_port_alloc_in_hopid(p, h, h) < 0)
181			goto err;
182
183		out_port = &sw->ports[hop.out_port];
184		next_hop = hop.next_hop;
185
186		if (tb_port_alloc_out_hopid(out_port, next_hop, next_hop) < 0) {
 
187			tb_port_release_in_hopid(p, h);
188			goto err;
189		}
190
191		path->hops[i].in_port = p;
192		path->hops[i].in_hop_index = h;
193		path->hops[i].in_counter_index = -1;
194		path->hops[i].out_port = out_port;
195		path->hops[i].next_hop_index = next_hop;
196
 
 
197		h = next_hop;
198		p = out_port->remote;
199	}
200
 
201	return path;
202
203err:
204	tb_port_warn(src, "failed to discover path starting at HopID %d\n",
205		     src_hopid);
206	tb_path_free(path);
207	return NULL;
208}
209
210/**
211 * tb_path_alloc() - allocate a thunderbolt path between two ports
212 * @tb: Domain pointer
213 * @src: Source port of the path
214 * @src_hopid: HopID used for the first ingress port in the path
215 * @dst: Destination port of the path
216 * @dst_hopid: HopID used for the last egress port in the path
217 * @link_nr: Preferred link if there are dual links on the path
218 * @name: Name of the path
219 *
220 * Creates path between two ports starting with given @src_hopid. Reserves
221 * HopIDs for each port (they can be different from @src_hopid depending on
222 * how many HopIDs each port already have reserved). If there are dual
223 * links on the path, prioritizes using @link_nr.
 
224 *
225 * Return: Returns a tb_path on success or NULL on failure.
226 */
227struct tb_path *tb_path_alloc(struct tb *tb, struct tb_port *src, int src_hopid,
228			      struct tb_port *dst, int dst_hopid, int link_nr,
229			      const char *name)
230{
231	struct tb_port *in_port, *out_port;
232	int in_hopid, out_hopid;
233	struct tb_path *path;
234	size_t num_hops;
235	int i, ret;
236
237	path = kzalloc(sizeof(*path), GFP_KERNEL);
238	if (!path)
239		return NULL;
240
241	/*
242	 * Number of hops on a path is the distance between the two
243	 * switches plus the source adapter port.
244	 */
245	num_hops = abs(tb_route_length(tb_route(src->sw)) -
246		       tb_route_length(tb_route(dst->sw))) + 1;
 
 
 
 
 
 
 
 
 
 
 
247
248	path->hops = kcalloc(num_hops, sizeof(*path->hops), GFP_KERNEL);
249	if (!path->hops) {
250		kfree(path);
251		return NULL;
252	}
253
 
 
254	in_hopid = src_hopid;
255	out_port = NULL;
256
257	for (i = 0; i < num_hops; i++) {
258		in_port = tb_next_port_on_path(src, dst, out_port);
259		if (!in_port)
260			goto err;
261
262		if (in_port->dual_link_port && in_port->link_nr != link_nr)
 
 
263			in_port = in_port->dual_link_port;
264
265		ret = tb_port_alloc_in_hopid(in_port, in_hopid, in_hopid);
266		if (ret < 0)
267			goto err;
268		in_hopid = ret;
269
270		out_port = tb_next_port_on_path(src, dst, in_port);
271		if (!out_port)
272			goto err;
273
274		if (out_port->dual_link_port && out_port->link_nr != link_nr)
275			out_port = out_port->dual_link_port;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
276
277		if (i == num_hops - 1)
278			ret = tb_port_alloc_out_hopid(out_port, dst_hopid,
279						      dst_hopid);
280		else
281			ret = tb_port_alloc_out_hopid(out_port, -1, -1);
282
283		if (ret < 0)
284			goto err;
285		out_hopid = ret;
286
287		path->hops[i].in_hop_index = in_hopid;
288		path->hops[i].in_port = in_port;
289		path->hops[i].in_counter_index = -1;
290		path->hops[i].out_port = out_port;
291		path->hops[i].next_hop_index = out_hopid;
292
293		in_hopid = out_hopid;
294	}
295
296	path->tb = tb;
297	path->path_length = num_hops;
298	path->name = name;
299
300	return path;
301
302err:
303	tb_path_free(path);
304	return NULL;
305}
306
307/**
308 * tb_path_free() - free a path
309 * @path: Path to free
310 *
311 * Frees a path. The path does not need to be deactivated.
312 */
313void tb_path_free(struct tb_path *path)
314{
315	int i;
 
316
317	for (i = 0; i < path->path_length; i++) {
318		const struct tb_path_hop *hop = &path->hops[i];
319
320		if (hop->in_port)
321			tb_port_release_in_hopid(hop->in_port,
322						 hop->in_hop_index);
323		if (hop->out_port)
324			tb_port_release_out_hopid(hop->out_port,
325						  hop->next_hop_index);
 
326	}
327
328	kfree(path->hops);
329	kfree(path);
330}
331
332static void __tb_path_deallocate_nfc(struct tb_path *path, int first_hop)
333{
334	int i, res;
335	for (i = first_hop; i < path->path_length; i++) {
336		res = tb_port_add_nfc_credits(path->hops[i].in_port,
337					      -path->nfc_credits);
338		if (res)
339			tb_port_warn(path->hops[i].in_port,
340				     "nfc credits deallocation failed for hop %d\n",
341				     i);
342	}
343}
344
345static int __tb_path_deactivate_hop(struct tb_port *port, int hop_index,
346				    bool clear_fc)
347{
348	struct tb_regs_hop hop;
349	ktime_t timeout;
350	int ret;
351
352	/* Disable the path */
353	ret = tb_port_read(port, &hop, TB_CFG_HOPS, 2 * hop_index, 2);
354	if (ret)
355		return ret;
356
357	/* Already disabled */
358	if (!hop.enable)
359		return 0;
360
361	hop.enable = 0;
362
363	ret = tb_port_write(port, &hop, TB_CFG_HOPS, 2 * hop_index, 2);
364	if (ret)
365		return ret;
366
367	/* Wait until it is drained */
368	timeout = ktime_add_ms(ktime_get(), 500);
369	do {
370		ret = tb_port_read(port, &hop, TB_CFG_HOPS, 2 * hop_index, 2);
371		if (ret)
372			return ret;
373
374		if (!hop.pending) {
375			if (clear_fc) {
376				/* Clear flow control */
377				hop.ingress_fc = 0;
 
 
 
 
 
 
 
 
378				hop.egress_fc = 0;
379				hop.ingress_shared_buffer = 0;
380				hop.egress_shared_buffer = 0;
381
382				return tb_port_write(port, &hop, TB_CFG_HOPS,
383						     2 * hop_index, 2);
384			}
385
386			return 0;
387		}
388
389		usleep_range(10, 20);
390	} while (ktime_before(ktime_get(), timeout));
391
392	return -ETIMEDOUT;
393}
394
395static void __tb_path_deactivate_hops(struct tb_path *path, int first_hop)
396{
397	int i, res;
398
399	for (i = first_hop; i < path->path_length; i++) {
400		res = __tb_path_deactivate_hop(path->hops[i].in_port,
401					       path->hops[i].in_hop_index,
402					       path->clear_fc);
403		if (res && res != -ENODEV)
404			tb_port_warn(path->hops[i].in_port,
405				     "hop deactivation failed for hop %d, index %d\n",
406				     i, path->hops[i].in_hop_index);
407	}
408}
409
410void tb_path_deactivate(struct tb_path *path)
411{
412	if (!path->activated) {
413		tb_WARN(path->tb, "trying to deactivate an inactive path\n");
414		return;
415	}
416	tb_dbg(path->tb,
417	       "deactivating %s path from %llx:%x to %llx:%x\n",
418	       path->name, tb_route(path->hops[0].in_port->sw),
419	       path->hops[0].in_port->port,
420	       tb_route(path->hops[path->path_length - 1].out_port->sw),
421	       path->hops[path->path_length - 1].out_port->port);
422	__tb_path_deactivate_hops(path, 0);
423	__tb_path_deallocate_nfc(path, 0);
424	path->activated = false;
425}
426
427/**
428 * tb_path_activate() - activate a path
 
429 *
430 * Activate a path starting with the last hop and iterating backwards. The
431 * caller must fill path->hops before calling tb_path_activate().
432 *
433 * Return: Returns 0 on success or an error code on failure.
434 */
435int tb_path_activate(struct tb_path *path)
436{
437	int i, res;
438	enum tb_path_port out_mask, in_mask;
439	if (path->activated) {
440		tb_WARN(path->tb, "trying to activate already activated path\n");
441		return -EINVAL;
442	}
443
444	tb_dbg(path->tb,
445	       "activating %s path from %llx:%x to %llx:%x\n",
446	       path->name, tb_route(path->hops[0].in_port->sw),
447	       path->hops[0].in_port->port,
448	       tb_route(path->hops[path->path_length - 1].out_port->sw),
449	       path->hops[path->path_length - 1].out_port->port);
450
451	/* Clear counters. */
452	for (i = path->path_length - 1; i >= 0; i--) {
453		if (path->hops[i].in_counter_index == -1)
454			continue;
455		res = tb_port_clear_counter(path->hops[i].in_port,
456					    path->hops[i].in_counter_index);
457		if (res)
458			goto err;
459	}
460
461	/* Add non flow controlled credits. */
462	for (i = path->path_length - 1; i >= 0; i--) {
463		res = tb_port_add_nfc_credits(path->hops[i].in_port,
464					      path->nfc_credits);
465		if (res) {
466			__tb_path_deallocate_nfc(path, i);
467			goto err;
468		}
469	}
470
471	/* Activate hops. */
472	for (i = path->path_length - 1; i >= 0; i--) {
473		struct tb_regs_hop hop = { 0 };
474
475		/* If it is left active deactivate it first */
476		__tb_path_deactivate_hop(path->hops[i].in_port,
477				path->hops[i].in_hop_index, path->clear_fc);
478
479		/* dword 0 */
480		hop.next_hop = path->hops[i].next_hop_index;
481		hop.out_port = path->hops[i].out_port->port;
482		hop.initial_credits = path->hops[i].initial_credits;
 
483		hop.unknown1 = 0;
484		hop.enable = 1;
485
486		/* dword 1 */
487		out_mask = (i == path->path_length - 1) ?
488				TB_PATH_DESTINATION : TB_PATH_INTERNAL;
489		in_mask = (i == 0) ? TB_PATH_SOURCE : TB_PATH_INTERNAL;
490		hop.weight = path->weight;
491		hop.unknown2 = 0;
492		hop.priority = path->priority;
493		hop.drop_packages = path->drop_packages;
494		hop.counter = path->hops[i].in_counter_index;
495		hop.counter_enable = path->hops[i].in_counter_index != -1;
496		hop.ingress_fc = path->ingress_fc_enable & in_mask;
497		hop.egress_fc = path->egress_fc_enable & out_mask;
498		hop.ingress_shared_buffer = path->ingress_shared_buffer
499					    & in_mask;
500		hop.egress_shared_buffer = path->egress_shared_buffer
501					    & out_mask;
502		hop.unknown3 = 0;
503
504		tb_port_dbg(path->hops[i].in_port, "Writing hop %d\n", i);
505		tb_dump_hop(&path->hops[i], &hop);
506		res = tb_port_write(path->hops[i].in_port, &hop, TB_CFG_HOPS,
507				    2 * path->hops[i].in_hop_index, 2);
508		if (res) {
509			__tb_path_deactivate_hops(path, i);
510			__tb_path_deallocate_nfc(path, 0);
511			goto err;
512		}
513	}
514	path->activated = true;
515	tb_dbg(path->tb, "path activation complete\n");
516	return 0;
517err:
518	tb_WARN(path->tb, "path activation failed\n");
519	return res;
520}
521
522/**
523 * tb_path_is_invalid() - check whether any ports on the path are invalid
 
524 *
525 * Return: Returns true if the path is invalid, false otherwise.
526 */
527bool tb_path_is_invalid(struct tb_path *path)
528{
529	int i = 0;
530	for (i = 0; i < path->path_length; i++) {
531		if (path->hops[i].in_port->sw->is_unplugged)
532			return true;
533		if (path->hops[i].out_port->sw->is_unplugged)
534			return true;
535	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
536	return false;
537}