Subject: Re: [PATCH v2 13/28] thunderbolt: Add helper function to iterate from one port to another

From: Andy Shevchenko <[email-masked]>
Date: Thu, 7 Feb 2019 16:33:12 +0200

On Wed, Feb 06, 2019 at 04:17:23PM +0300, Mika Westerberg wrote:
> We need to be able to walk from one port to another when we are creating
> paths where there are multiple switches between two ports. For this
> reason introduce a new function tb_port_get_next() and a new macro
> tb_for_each_port().

> +struct tb_port *tb_port_get_next(struct tb_port *start, struct tb_port *end,
> +				 struct tb_port *prev)
> +{
> +	struct tb_port *port, *next;
> +
> +	if (!prev)
> +		return start;
> +
> +	if (prev->sw == end->sw) {

> +		if (prev != end)
> +			return end;
> +		return NULL;

I would prefer to see the similar pattern as you used below, i.e. when we have
an "bail out" condition, check for it.

		if (prev == end)
			return NULL;
		return end;

> +	}
> +
> +	/* Switch back to use primary links for walking */
> +	if (prev->dual_link_port && prev->link_nr)
> +		port = prev->dual_link_port;
> +	else
> +		port = prev;
> +
> +	if (start->sw->config.depth < end->sw->config.depth) {
> +		if (port->remote &&
> +		    port->remote->sw->config.depth > port->sw->config.depth)
> +			next = port->remote;
> +		else
> +			next = tb_port_at(tb_route(end->sw), port->sw);
> +	} else if (start->sw->config.depth > end->sw->config.depth) {
> +		if (tb_is_upstream_port(port))
> +			next = port->remote;
> +		else
> +			next = tb_upstream_port(port->sw);
> +	} else {

> +		/* Must be the same switch then */
> +		if (start->sw != end->sw)
> +			return NULL;
> +		return end;

Here is a good pattern.

> +	}
> +
> +	/* If prev was dual link return another end of that link then */
> +	if (next->dual_link_port && next->link_nr != prev->link_nr)
> +		return next->dual_link_port;
> +
> +	return next;
> +}

-- 
With Best Regards,
Andy Shevchenko