162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Thunderbolt driver - bus logic (NHI independent)
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
662306a36Sopenharmony_ci * Copyright (C) 2019, Intel Corporation
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <linux/slab.h>
1062306a36Sopenharmony_ci#include <linux/errno.h>
1162306a36Sopenharmony_ci#include <linux/delay.h>
1262306a36Sopenharmony_ci#include <linux/pm_runtime.h>
1362306a36Sopenharmony_ci#include <linux/platform_data/x86/apple.h>
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#include "tb.h"
1662306a36Sopenharmony_ci#include "tb_regs.h"
1762306a36Sopenharmony_ci#include "tunnel.h"
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci#define TB_TIMEOUT	100	/* ms */
2062306a36Sopenharmony_ci#define MAX_GROUPS	7	/* max Group_ID is 7 */
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci/**
2362306a36Sopenharmony_ci * struct tb_cm - Simple Thunderbolt connection manager
2462306a36Sopenharmony_ci * @tunnel_list: List of active tunnels
2562306a36Sopenharmony_ci * @dp_resources: List of available DP resources for DP tunneling
2662306a36Sopenharmony_ci * @hotplug_active: tb_handle_hotplug will stop progressing plug
2762306a36Sopenharmony_ci *		    events and exit if this is not set (it needs to
2862306a36Sopenharmony_ci *		    acquire the lock one more time). Used to drain wq
2962306a36Sopenharmony_ci *		    after cfg has been paused.
3062306a36Sopenharmony_ci * @remove_work: Work used to remove any unplugged routers after
3162306a36Sopenharmony_ci *		 runtime resume
3262306a36Sopenharmony_ci * @groups: Bandwidth groups used in this domain.
3362306a36Sopenharmony_ci */
3462306a36Sopenharmony_cistruct tb_cm {
3562306a36Sopenharmony_ci	struct list_head tunnel_list;
3662306a36Sopenharmony_ci	struct list_head dp_resources;
3762306a36Sopenharmony_ci	bool hotplug_active;
3862306a36Sopenharmony_ci	struct delayed_work remove_work;
3962306a36Sopenharmony_ci	struct tb_bandwidth_group groups[MAX_GROUPS];
4062306a36Sopenharmony_ci};
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_cistatic inline struct tb *tcm_to_tb(struct tb_cm *tcm)
4362306a36Sopenharmony_ci{
4462306a36Sopenharmony_ci	return ((void *)tcm - sizeof(struct tb));
4562306a36Sopenharmony_ci}
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_cistruct tb_hotplug_event {
4862306a36Sopenharmony_ci	struct work_struct work;
4962306a36Sopenharmony_ci	struct tb *tb;
5062306a36Sopenharmony_ci	u64 route;
5162306a36Sopenharmony_ci	u8 port;
5262306a36Sopenharmony_ci	bool unplug;
5362306a36Sopenharmony_ci};
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_cistatic void tb_init_bandwidth_groups(struct tb_cm *tcm)
5662306a36Sopenharmony_ci{
5762306a36Sopenharmony_ci	int i;
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) {
6062306a36Sopenharmony_ci		struct tb_bandwidth_group *group = &tcm->groups[i];
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci		group->tb = tcm_to_tb(tcm);
6362306a36Sopenharmony_ci		group->index = i + 1;
6462306a36Sopenharmony_ci		INIT_LIST_HEAD(&group->ports);
6562306a36Sopenharmony_ci	}
6662306a36Sopenharmony_ci}
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_cistatic void tb_bandwidth_group_attach_port(struct tb_bandwidth_group *group,
6962306a36Sopenharmony_ci					   struct tb_port *in)
7062306a36Sopenharmony_ci{
7162306a36Sopenharmony_ci	if (!group || WARN_ON(in->group))
7262306a36Sopenharmony_ci		return;
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci	in->group = group;
7562306a36Sopenharmony_ci	list_add_tail(&in->group_list, &group->ports);
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	tb_port_dbg(in, "attached to bandwidth group %d\n", group->index);
7862306a36Sopenharmony_ci}
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_cistatic struct tb_bandwidth_group *tb_find_free_bandwidth_group(struct tb_cm *tcm)
8162306a36Sopenharmony_ci{
8262306a36Sopenharmony_ci	int i;
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) {
8562306a36Sopenharmony_ci		struct tb_bandwidth_group *group = &tcm->groups[i];
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci		if (list_empty(&group->ports))
8862306a36Sopenharmony_ci			return group;
8962306a36Sopenharmony_ci	}
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci	return NULL;
9262306a36Sopenharmony_ci}
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_cistatic struct tb_bandwidth_group *
9562306a36Sopenharmony_citb_attach_bandwidth_group(struct tb_cm *tcm, struct tb_port *in,
9662306a36Sopenharmony_ci			  struct tb_port *out)
9762306a36Sopenharmony_ci{
9862306a36Sopenharmony_ci	struct tb_bandwidth_group *group;
9962306a36Sopenharmony_ci	struct tb_tunnel *tunnel;
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci	/*
10262306a36Sopenharmony_ci	 * Find all DP tunnels that go through all the same USB4 links
10362306a36Sopenharmony_ci	 * as this one. Because we always setup tunnels the same way we
10462306a36Sopenharmony_ci	 * can just check for the routers at both ends of the tunnels
10562306a36Sopenharmony_ci	 * and if they are the same we have a match.
10662306a36Sopenharmony_ci	 */
10762306a36Sopenharmony_ci	list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
10862306a36Sopenharmony_ci		if (!tb_tunnel_is_dp(tunnel))
10962306a36Sopenharmony_ci			continue;
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci		if (tunnel->src_port->sw == in->sw &&
11262306a36Sopenharmony_ci		    tunnel->dst_port->sw == out->sw) {
11362306a36Sopenharmony_ci			group = tunnel->src_port->group;
11462306a36Sopenharmony_ci			if (group) {
11562306a36Sopenharmony_ci				tb_bandwidth_group_attach_port(group, in);
11662306a36Sopenharmony_ci				return group;
11762306a36Sopenharmony_ci			}
11862306a36Sopenharmony_ci		}
11962306a36Sopenharmony_ci	}
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci	/* Pick up next available group then */
12262306a36Sopenharmony_ci	group = tb_find_free_bandwidth_group(tcm);
12362306a36Sopenharmony_ci	if (group)
12462306a36Sopenharmony_ci		tb_bandwidth_group_attach_port(group, in);
12562306a36Sopenharmony_ci	else
12662306a36Sopenharmony_ci		tb_port_warn(in, "no available bandwidth groups\n");
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci	return group;
12962306a36Sopenharmony_ci}
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_cistatic void tb_discover_bandwidth_group(struct tb_cm *tcm, struct tb_port *in,
13262306a36Sopenharmony_ci					struct tb_port *out)
13362306a36Sopenharmony_ci{
13462306a36Sopenharmony_ci	if (usb4_dp_port_bandwidth_mode_enabled(in)) {
13562306a36Sopenharmony_ci		int index, i;
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci		index = usb4_dp_port_group_id(in);
13862306a36Sopenharmony_ci		for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) {
13962306a36Sopenharmony_ci			if (tcm->groups[i].index == index) {
14062306a36Sopenharmony_ci				tb_bandwidth_group_attach_port(&tcm->groups[i], in);
14162306a36Sopenharmony_ci				return;
14262306a36Sopenharmony_ci			}
14362306a36Sopenharmony_ci		}
14462306a36Sopenharmony_ci	}
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci	tb_attach_bandwidth_group(tcm, in, out);
14762306a36Sopenharmony_ci}
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_cistatic void tb_detach_bandwidth_group(struct tb_port *in)
15062306a36Sopenharmony_ci{
15162306a36Sopenharmony_ci	struct tb_bandwidth_group *group = in->group;
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci	if (group) {
15462306a36Sopenharmony_ci		in->group = NULL;
15562306a36Sopenharmony_ci		list_del_init(&in->group_list);
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci		tb_port_dbg(in, "detached from bandwidth group %d\n", group->index);
15862306a36Sopenharmony_ci	}
15962306a36Sopenharmony_ci}
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_cistatic void tb_handle_hotplug(struct work_struct *work);
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_cistatic void tb_queue_hotplug(struct tb *tb, u64 route, u8 port, bool unplug)
16462306a36Sopenharmony_ci{
16562306a36Sopenharmony_ci	struct tb_hotplug_event *ev;
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci	ev = kmalloc(sizeof(*ev), GFP_KERNEL);
16862306a36Sopenharmony_ci	if (!ev)
16962306a36Sopenharmony_ci		return;
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci	ev->tb = tb;
17262306a36Sopenharmony_ci	ev->route = route;
17362306a36Sopenharmony_ci	ev->port = port;
17462306a36Sopenharmony_ci	ev->unplug = unplug;
17562306a36Sopenharmony_ci	INIT_WORK(&ev->work, tb_handle_hotplug);
17662306a36Sopenharmony_ci	queue_work(tb->wq, &ev->work);
17762306a36Sopenharmony_ci}
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci/* enumeration & hot plug handling */
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_cistatic void tb_add_dp_resources(struct tb_switch *sw)
18262306a36Sopenharmony_ci{
18362306a36Sopenharmony_ci	struct tb_cm *tcm = tb_priv(sw->tb);
18462306a36Sopenharmony_ci	struct tb_port *port;
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci	tb_switch_for_each_port(sw, port) {
18762306a36Sopenharmony_ci		if (!tb_port_is_dpin(port))
18862306a36Sopenharmony_ci			continue;
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci		if (!tb_switch_query_dp_resource(sw, port))
19162306a36Sopenharmony_ci			continue;
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci		list_add_tail(&port->list, &tcm->dp_resources);
19462306a36Sopenharmony_ci		tb_port_dbg(port, "DP IN resource available\n");
19562306a36Sopenharmony_ci	}
19662306a36Sopenharmony_ci}
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_cistatic void tb_remove_dp_resources(struct tb_switch *sw)
19962306a36Sopenharmony_ci{
20062306a36Sopenharmony_ci	struct tb_cm *tcm = tb_priv(sw->tb);
20162306a36Sopenharmony_ci	struct tb_port *port, *tmp;
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ci	/* Clear children resources first */
20462306a36Sopenharmony_ci	tb_switch_for_each_port(sw, port) {
20562306a36Sopenharmony_ci		if (tb_port_has_remote(port))
20662306a36Sopenharmony_ci			tb_remove_dp_resources(port->remote->sw);
20762306a36Sopenharmony_ci	}
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ci	list_for_each_entry_safe(port, tmp, &tcm->dp_resources, list) {
21062306a36Sopenharmony_ci		if (port->sw == sw) {
21162306a36Sopenharmony_ci			tb_port_dbg(port, "DP OUT resource unavailable\n");
21262306a36Sopenharmony_ci			list_del_init(&port->list);
21362306a36Sopenharmony_ci		}
21462306a36Sopenharmony_ci	}
21562306a36Sopenharmony_ci}
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_cistatic void tb_discover_dp_resource(struct tb *tb, struct tb_port *port)
21862306a36Sopenharmony_ci{
21962306a36Sopenharmony_ci	struct tb_cm *tcm = tb_priv(tb);
22062306a36Sopenharmony_ci	struct tb_port *p;
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ci	list_for_each_entry(p, &tcm->dp_resources, list) {
22362306a36Sopenharmony_ci		if (p == port)
22462306a36Sopenharmony_ci			return;
22562306a36Sopenharmony_ci	}
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ci	tb_port_dbg(port, "DP %s resource available discovered\n",
22862306a36Sopenharmony_ci		    tb_port_is_dpin(port) ? "IN" : "OUT");
22962306a36Sopenharmony_ci	list_add_tail(&port->list, &tcm->dp_resources);
23062306a36Sopenharmony_ci}
23162306a36Sopenharmony_ci
23262306a36Sopenharmony_cistatic void tb_discover_dp_resources(struct tb *tb)
23362306a36Sopenharmony_ci{
23462306a36Sopenharmony_ci	struct tb_cm *tcm = tb_priv(tb);
23562306a36Sopenharmony_ci	struct tb_tunnel *tunnel;
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_ci	list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
23862306a36Sopenharmony_ci		if (tb_tunnel_is_dp(tunnel))
23962306a36Sopenharmony_ci			tb_discover_dp_resource(tb, tunnel->dst_port);
24062306a36Sopenharmony_ci	}
24162306a36Sopenharmony_ci}
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_ci/* Enables CL states up to host router */
24462306a36Sopenharmony_cistatic int tb_enable_clx(struct tb_switch *sw)
24562306a36Sopenharmony_ci{
24662306a36Sopenharmony_ci	struct tb_cm *tcm = tb_priv(sw->tb);
24762306a36Sopenharmony_ci	unsigned int clx = TB_CL0S | TB_CL1;
24862306a36Sopenharmony_ci	const struct tb_tunnel *tunnel;
24962306a36Sopenharmony_ci	int ret;
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci	/*
25262306a36Sopenharmony_ci	 * Currently only enable CLx for the first link. This is enough
25362306a36Sopenharmony_ci	 * to allow the CPU to save energy at least on Intel hardware
25462306a36Sopenharmony_ci	 * and makes it slightly simpler to implement. We may change
25562306a36Sopenharmony_ci	 * this in the future to cover the whole topology if it turns
25662306a36Sopenharmony_ci	 * out to be beneficial.
25762306a36Sopenharmony_ci	 */
25862306a36Sopenharmony_ci	while (sw && sw->config.depth > 1)
25962306a36Sopenharmony_ci		sw = tb_switch_parent(sw);
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ci	if (!sw)
26262306a36Sopenharmony_ci		return 0;
26362306a36Sopenharmony_ci
26462306a36Sopenharmony_ci	if (sw->config.depth != 1)
26562306a36Sopenharmony_ci		return 0;
26662306a36Sopenharmony_ci
26762306a36Sopenharmony_ci	/*
26862306a36Sopenharmony_ci	 * If we are re-enabling then check if there is an active DMA
26962306a36Sopenharmony_ci	 * tunnel and in that case bail out.
27062306a36Sopenharmony_ci	 */
27162306a36Sopenharmony_ci	list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
27262306a36Sopenharmony_ci		if (tb_tunnel_is_dma(tunnel)) {
27362306a36Sopenharmony_ci			if (tb_tunnel_port_on_path(tunnel, tb_upstream_port(sw)))
27462306a36Sopenharmony_ci				return 0;
27562306a36Sopenharmony_ci		}
27662306a36Sopenharmony_ci	}
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_ci	/*
27962306a36Sopenharmony_ci	 * Initially try with CL2. If that's not supported by the
28062306a36Sopenharmony_ci	 * topology try with CL0s and CL1 and then give up.
28162306a36Sopenharmony_ci	 */
28262306a36Sopenharmony_ci	ret = tb_switch_clx_enable(sw, clx | TB_CL2);
28362306a36Sopenharmony_ci	if (ret == -EOPNOTSUPP)
28462306a36Sopenharmony_ci		ret = tb_switch_clx_enable(sw, clx);
28562306a36Sopenharmony_ci	return ret == -EOPNOTSUPP ? 0 : ret;
28662306a36Sopenharmony_ci}
28762306a36Sopenharmony_ci
28862306a36Sopenharmony_ci/* Disables CL states up to the host router */
28962306a36Sopenharmony_cistatic void tb_disable_clx(struct tb_switch *sw)
29062306a36Sopenharmony_ci{
29162306a36Sopenharmony_ci	do {
29262306a36Sopenharmony_ci		if (tb_switch_clx_disable(sw) < 0)
29362306a36Sopenharmony_ci			tb_sw_warn(sw, "failed to disable CL states\n");
29462306a36Sopenharmony_ci		sw = tb_switch_parent(sw);
29562306a36Sopenharmony_ci	} while (sw);
29662306a36Sopenharmony_ci}
29762306a36Sopenharmony_ci
29862306a36Sopenharmony_cistatic int tb_increase_switch_tmu_accuracy(struct device *dev, void *data)
29962306a36Sopenharmony_ci{
30062306a36Sopenharmony_ci	struct tb_switch *sw;
30162306a36Sopenharmony_ci
30262306a36Sopenharmony_ci	sw = tb_to_switch(dev);
30362306a36Sopenharmony_ci	if (!sw)
30462306a36Sopenharmony_ci		return 0;
30562306a36Sopenharmony_ci
30662306a36Sopenharmony_ci	if (tb_switch_tmu_is_configured(sw, TB_SWITCH_TMU_MODE_LOWRES)) {
30762306a36Sopenharmony_ci		enum tb_switch_tmu_mode mode;
30862306a36Sopenharmony_ci		int ret;
30962306a36Sopenharmony_ci
31062306a36Sopenharmony_ci		if (tb_switch_clx_is_enabled(sw, TB_CL1))
31162306a36Sopenharmony_ci			mode = TB_SWITCH_TMU_MODE_HIFI_UNI;
31262306a36Sopenharmony_ci		else
31362306a36Sopenharmony_ci			mode = TB_SWITCH_TMU_MODE_HIFI_BI;
31462306a36Sopenharmony_ci
31562306a36Sopenharmony_ci		ret = tb_switch_tmu_configure(sw, mode);
31662306a36Sopenharmony_ci		if (ret)
31762306a36Sopenharmony_ci			return ret;
31862306a36Sopenharmony_ci
31962306a36Sopenharmony_ci		return tb_switch_tmu_enable(sw);
32062306a36Sopenharmony_ci	}
32162306a36Sopenharmony_ci
32262306a36Sopenharmony_ci	return 0;
32362306a36Sopenharmony_ci}
32462306a36Sopenharmony_ci
32562306a36Sopenharmony_cistatic void tb_increase_tmu_accuracy(struct tb_tunnel *tunnel)
32662306a36Sopenharmony_ci{
32762306a36Sopenharmony_ci	struct tb_switch *sw;
32862306a36Sopenharmony_ci
32962306a36Sopenharmony_ci	if (!tunnel)
33062306a36Sopenharmony_ci		return;
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_ci	/*
33362306a36Sopenharmony_ci	 * Once first DP tunnel is established we change the TMU
33462306a36Sopenharmony_ci	 * accuracy of first depth child routers (and the host router)
33562306a36Sopenharmony_ci	 * to the highest. This is needed for the DP tunneling to work
33662306a36Sopenharmony_ci	 * but also allows CL0s.
33762306a36Sopenharmony_ci	 *
33862306a36Sopenharmony_ci	 * If both routers are v2 then we don't need to do anything as
33962306a36Sopenharmony_ci	 * they are using enhanced TMU mode that allows all CLx.
34062306a36Sopenharmony_ci	 */
34162306a36Sopenharmony_ci	sw = tunnel->tb->root_switch;
34262306a36Sopenharmony_ci	device_for_each_child(&sw->dev, NULL, tb_increase_switch_tmu_accuracy);
34362306a36Sopenharmony_ci}
34462306a36Sopenharmony_ci
34562306a36Sopenharmony_cistatic int tb_enable_tmu(struct tb_switch *sw)
34662306a36Sopenharmony_ci{
34762306a36Sopenharmony_ci	int ret;
34862306a36Sopenharmony_ci
34962306a36Sopenharmony_ci	/*
35062306a36Sopenharmony_ci	 * If both routers at the end of the link are v2 we simply
35162306a36Sopenharmony_ci	 * enable the enhanched uni-directional mode. That covers all
35262306a36Sopenharmony_ci	 * the CL states. For v1 and before we need to use the normal
35362306a36Sopenharmony_ci	 * rate to allow CL1 (when supported). Otherwise we keep the TMU
35462306a36Sopenharmony_ci	 * running at the highest accuracy.
35562306a36Sopenharmony_ci	 */
35662306a36Sopenharmony_ci	ret = tb_switch_tmu_configure(sw,
35762306a36Sopenharmony_ci			TB_SWITCH_TMU_MODE_MEDRES_ENHANCED_UNI);
35862306a36Sopenharmony_ci	if (ret == -EOPNOTSUPP) {
35962306a36Sopenharmony_ci		if (tb_switch_clx_is_enabled(sw, TB_CL1))
36062306a36Sopenharmony_ci			ret = tb_switch_tmu_configure(sw,
36162306a36Sopenharmony_ci					TB_SWITCH_TMU_MODE_LOWRES);
36262306a36Sopenharmony_ci		else
36362306a36Sopenharmony_ci			ret = tb_switch_tmu_configure(sw,
36462306a36Sopenharmony_ci					TB_SWITCH_TMU_MODE_HIFI_BI);
36562306a36Sopenharmony_ci	}
36662306a36Sopenharmony_ci	if (ret)
36762306a36Sopenharmony_ci		return ret;
36862306a36Sopenharmony_ci
36962306a36Sopenharmony_ci	/* If it is already enabled in correct mode, don't touch it */
37062306a36Sopenharmony_ci	if (tb_switch_tmu_is_enabled(sw))
37162306a36Sopenharmony_ci		return 0;
37262306a36Sopenharmony_ci
37362306a36Sopenharmony_ci	ret = tb_switch_tmu_disable(sw);
37462306a36Sopenharmony_ci	if (ret)
37562306a36Sopenharmony_ci		return ret;
37662306a36Sopenharmony_ci
37762306a36Sopenharmony_ci	ret = tb_switch_tmu_post_time(sw);
37862306a36Sopenharmony_ci	if (ret)
37962306a36Sopenharmony_ci		return ret;
38062306a36Sopenharmony_ci
38162306a36Sopenharmony_ci	return tb_switch_tmu_enable(sw);
38262306a36Sopenharmony_ci}
38362306a36Sopenharmony_ci
38462306a36Sopenharmony_cistatic void tb_switch_discover_tunnels(struct tb_switch *sw,
38562306a36Sopenharmony_ci				       struct list_head *list,
38662306a36Sopenharmony_ci				       bool alloc_hopids)
38762306a36Sopenharmony_ci{
38862306a36Sopenharmony_ci	struct tb *tb = sw->tb;
38962306a36Sopenharmony_ci	struct tb_port *port;
39062306a36Sopenharmony_ci
39162306a36Sopenharmony_ci	tb_switch_for_each_port(sw, port) {
39262306a36Sopenharmony_ci		struct tb_tunnel *tunnel = NULL;
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_ci		switch (port->config.type) {
39562306a36Sopenharmony_ci		case TB_TYPE_DP_HDMI_IN:
39662306a36Sopenharmony_ci			tunnel = tb_tunnel_discover_dp(tb, port, alloc_hopids);
39762306a36Sopenharmony_ci			tb_increase_tmu_accuracy(tunnel);
39862306a36Sopenharmony_ci			break;
39962306a36Sopenharmony_ci
40062306a36Sopenharmony_ci		case TB_TYPE_PCIE_DOWN:
40162306a36Sopenharmony_ci			tunnel = tb_tunnel_discover_pci(tb, port, alloc_hopids);
40262306a36Sopenharmony_ci			break;
40362306a36Sopenharmony_ci
40462306a36Sopenharmony_ci		case TB_TYPE_USB3_DOWN:
40562306a36Sopenharmony_ci			tunnel = tb_tunnel_discover_usb3(tb, port, alloc_hopids);
40662306a36Sopenharmony_ci			break;
40762306a36Sopenharmony_ci
40862306a36Sopenharmony_ci		default:
40962306a36Sopenharmony_ci			break;
41062306a36Sopenharmony_ci		}
41162306a36Sopenharmony_ci
41262306a36Sopenharmony_ci		if (tunnel)
41362306a36Sopenharmony_ci			list_add_tail(&tunnel->list, list);
41462306a36Sopenharmony_ci	}
41562306a36Sopenharmony_ci
41662306a36Sopenharmony_ci	tb_switch_for_each_port(sw, port) {
41762306a36Sopenharmony_ci		if (tb_port_has_remote(port)) {
41862306a36Sopenharmony_ci			tb_switch_discover_tunnels(port->remote->sw, list,
41962306a36Sopenharmony_ci						   alloc_hopids);
42062306a36Sopenharmony_ci		}
42162306a36Sopenharmony_ci	}
42262306a36Sopenharmony_ci}
42362306a36Sopenharmony_ci
42462306a36Sopenharmony_cistatic void tb_discover_tunnels(struct tb *tb)
42562306a36Sopenharmony_ci{
42662306a36Sopenharmony_ci	struct tb_cm *tcm = tb_priv(tb);
42762306a36Sopenharmony_ci	struct tb_tunnel *tunnel;
42862306a36Sopenharmony_ci
42962306a36Sopenharmony_ci	tb_switch_discover_tunnels(tb->root_switch, &tcm->tunnel_list, true);
43062306a36Sopenharmony_ci
43162306a36Sopenharmony_ci	list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
43262306a36Sopenharmony_ci		if (tb_tunnel_is_pci(tunnel)) {
43362306a36Sopenharmony_ci			struct tb_switch *parent = tunnel->dst_port->sw;
43462306a36Sopenharmony_ci
43562306a36Sopenharmony_ci			while (parent != tunnel->src_port->sw) {
43662306a36Sopenharmony_ci				parent->boot = true;
43762306a36Sopenharmony_ci				parent = tb_switch_parent(parent);
43862306a36Sopenharmony_ci			}
43962306a36Sopenharmony_ci		} else if (tb_tunnel_is_dp(tunnel)) {
44062306a36Sopenharmony_ci			struct tb_port *in = tunnel->src_port;
44162306a36Sopenharmony_ci			struct tb_port *out = tunnel->dst_port;
44262306a36Sopenharmony_ci
44362306a36Sopenharmony_ci			/* Keep the domain from powering down */
44462306a36Sopenharmony_ci			pm_runtime_get_sync(&in->sw->dev);
44562306a36Sopenharmony_ci			pm_runtime_get_sync(&out->sw->dev);
44662306a36Sopenharmony_ci
44762306a36Sopenharmony_ci			tb_discover_bandwidth_group(tcm, in, out);
44862306a36Sopenharmony_ci		}
44962306a36Sopenharmony_ci	}
45062306a36Sopenharmony_ci}
45162306a36Sopenharmony_ci
45262306a36Sopenharmony_cistatic int tb_port_configure_xdomain(struct tb_port *port, struct tb_xdomain *xd)
45362306a36Sopenharmony_ci{
45462306a36Sopenharmony_ci	if (tb_switch_is_usb4(port->sw))
45562306a36Sopenharmony_ci		return usb4_port_configure_xdomain(port, xd);
45662306a36Sopenharmony_ci	return tb_lc_configure_xdomain(port);
45762306a36Sopenharmony_ci}
45862306a36Sopenharmony_ci
45962306a36Sopenharmony_cistatic void tb_port_unconfigure_xdomain(struct tb_port *port)
46062306a36Sopenharmony_ci{
46162306a36Sopenharmony_ci	if (tb_switch_is_usb4(port->sw))
46262306a36Sopenharmony_ci		usb4_port_unconfigure_xdomain(port);
46362306a36Sopenharmony_ci	else
46462306a36Sopenharmony_ci		tb_lc_unconfigure_xdomain(port);
46562306a36Sopenharmony_ci
46662306a36Sopenharmony_ci	tb_port_enable(port->dual_link_port);
46762306a36Sopenharmony_ci}
46862306a36Sopenharmony_ci
46962306a36Sopenharmony_cistatic void tb_scan_xdomain(struct tb_port *port)
47062306a36Sopenharmony_ci{
47162306a36Sopenharmony_ci	struct tb_switch *sw = port->sw;
47262306a36Sopenharmony_ci	struct tb *tb = sw->tb;
47362306a36Sopenharmony_ci	struct tb_xdomain *xd;
47462306a36Sopenharmony_ci	u64 route;
47562306a36Sopenharmony_ci
47662306a36Sopenharmony_ci	if (!tb_is_xdomain_enabled())
47762306a36Sopenharmony_ci		return;
47862306a36Sopenharmony_ci
47962306a36Sopenharmony_ci	route = tb_downstream_route(port);
48062306a36Sopenharmony_ci	xd = tb_xdomain_find_by_route(tb, route);
48162306a36Sopenharmony_ci	if (xd) {
48262306a36Sopenharmony_ci		tb_xdomain_put(xd);
48362306a36Sopenharmony_ci		return;
48462306a36Sopenharmony_ci	}
48562306a36Sopenharmony_ci
48662306a36Sopenharmony_ci	xd = tb_xdomain_alloc(tb, &sw->dev, route, tb->root_switch->uuid,
48762306a36Sopenharmony_ci			      NULL);
48862306a36Sopenharmony_ci	if (xd) {
48962306a36Sopenharmony_ci		tb_port_at(route, sw)->xdomain = xd;
49062306a36Sopenharmony_ci		tb_port_configure_xdomain(port, xd);
49162306a36Sopenharmony_ci		tb_xdomain_add(xd);
49262306a36Sopenharmony_ci	}
49362306a36Sopenharmony_ci}
49462306a36Sopenharmony_ci
49562306a36Sopenharmony_ci/**
49662306a36Sopenharmony_ci * tb_find_unused_port() - return the first inactive port on @sw
49762306a36Sopenharmony_ci * @sw: Switch to find the port on
49862306a36Sopenharmony_ci * @type: Port type to look for
49962306a36Sopenharmony_ci */
50062306a36Sopenharmony_cistatic struct tb_port *tb_find_unused_port(struct tb_switch *sw,
50162306a36Sopenharmony_ci					   enum tb_port_type type)
50262306a36Sopenharmony_ci{
50362306a36Sopenharmony_ci	struct tb_port *port;
50462306a36Sopenharmony_ci
50562306a36Sopenharmony_ci	tb_switch_for_each_port(sw, port) {
50662306a36Sopenharmony_ci		if (tb_is_upstream_port(port))
50762306a36Sopenharmony_ci			continue;
50862306a36Sopenharmony_ci		if (port->config.type != type)
50962306a36Sopenharmony_ci			continue;
51062306a36Sopenharmony_ci		if (!port->cap_adap)
51162306a36Sopenharmony_ci			continue;
51262306a36Sopenharmony_ci		if (tb_port_is_enabled(port))
51362306a36Sopenharmony_ci			continue;
51462306a36Sopenharmony_ci		return port;
51562306a36Sopenharmony_ci	}
51662306a36Sopenharmony_ci	return NULL;
51762306a36Sopenharmony_ci}
51862306a36Sopenharmony_ci
51962306a36Sopenharmony_cistatic struct tb_port *tb_find_usb3_down(struct tb_switch *sw,
52062306a36Sopenharmony_ci					 const struct tb_port *port)
52162306a36Sopenharmony_ci{
52262306a36Sopenharmony_ci	struct tb_port *down;
52362306a36Sopenharmony_ci
52462306a36Sopenharmony_ci	down = usb4_switch_map_usb3_down(sw, port);
52562306a36Sopenharmony_ci	if (down && !tb_usb3_port_is_enabled(down))
52662306a36Sopenharmony_ci		return down;
52762306a36Sopenharmony_ci	return NULL;
52862306a36Sopenharmony_ci}
52962306a36Sopenharmony_ci
53062306a36Sopenharmony_cistatic struct tb_tunnel *tb_find_tunnel(struct tb *tb, enum tb_tunnel_type type,
53162306a36Sopenharmony_ci					struct tb_port *src_port,
53262306a36Sopenharmony_ci					struct tb_port *dst_port)
53362306a36Sopenharmony_ci{
53462306a36Sopenharmony_ci	struct tb_cm *tcm = tb_priv(tb);
53562306a36Sopenharmony_ci	struct tb_tunnel *tunnel;
53662306a36Sopenharmony_ci
53762306a36Sopenharmony_ci	list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
53862306a36Sopenharmony_ci		if (tunnel->type == type &&
53962306a36Sopenharmony_ci		    ((src_port && src_port == tunnel->src_port) ||
54062306a36Sopenharmony_ci		     (dst_port && dst_port == tunnel->dst_port))) {
54162306a36Sopenharmony_ci			return tunnel;
54262306a36Sopenharmony_ci		}
54362306a36Sopenharmony_ci	}
54462306a36Sopenharmony_ci
54562306a36Sopenharmony_ci	return NULL;
54662306a36Sopenharmony_ci}
54762306a36Sopenharmony_ci
54862306a36Sopenharmony_cistatic struct tb_tunnel *tb_find_first_usb3_tunnel(struct tb *tb,
54962306a36Sopenharmony_ci						   struct tb_port *src_port,
55062306a36Sopenharmony_ci						   struct tb_port *dst_port)
55162306a36Sopenharmony_ci{
55262306a36Sopenharmony_ci	struct tb_port *port, *usb3_down;
55362306a36Sopenharmony_ci	struct tb_switch *sw;
55462306a36Sopenharmony_ci
55562306a36Sopenharmony_ci	/* Pick the router that is deepest in the topology */
55662306a36Sopenharmony_ci	if (dst_port->sw->config.depth > src_port->sw->config.depth)
55762306a36Sopenharmony_ci		sw = dst_port->sw;
55862306a36Sopenharmony_ci	else
55962306a36Sopenharmony_ci		sw = src_port->sw;
56062306a36Sopenharmony_ci
56162306a36Sopenharmony_ci	/* Can't be the host router */
56262306a36Sopenharmony_ci	if (sw == tb->root_switch)
56362306a36Sopenharmony_ci		return NULL;
56462306a36Sopenharmony_ci
56562306a36Sopenharmony_ci	/* Find the downstream USB4 port that leads to this router */
56662306a36Sopenharmony_ci	port = tb_port_at(tb_route(sw), tb->root_switch);
56762306a36Sopenharmony_ci	/* Find the corresponding host router USB3 downstream port */
56862306a36Sopenharmony_ci	usb3_down = usb4_switch_map_usb3_down(tb->root_switch, port);
56962306a36Sopenharmony_ci	if (!usb3_down)
57062306a36Sopenharmony_ci		return NULL;
57162306a36Sopenharmony_ci
57262306a36Sopenharmony_ci	return tb_find_tunnel(tb, TB_TUNNEL_USB3, usb3_down, NULL);
57362306a36Sopenharmony_ci}
57462306a36Sopenharmony_ci
57562306a36Sopenharmony_cistatic int tb_available_bandwidth(struct tb *tb, struct tb_port *src_port,
57662306a36Sopenharmony_ci	struct tb_port *dst_port, int *available_up, int *available_down)
57762306a36Sopenharmony_ci{
57862306a36Sopenharmony_ci	int usb3_consumed_up, usb3_consumed_down, ret;
57962306a36Sopenharmony_ci	struct tb_cm *tcm = tb_priv(tb);
58062306a36Sopenharmony_ci	struct tb_tunnel *tunnel;
58162306a36Sopenharmony_ci	struct tb_port *port;
58262306a36Sopenharmony_ci
58362306a36Sopenharmony_ci	tb_dbg(tb, "calculating available bandwidth between %llx:%u <-> %llx:%u\n",
58462306a36Sopenharmony_ci	       tb_route(src_port->sw), src_port->port, tb_route(dst_port->sw),
58562306a36Sopenharmony_ci	       dst_port->port);
58662306a36Sopenharmony_ci
58762306a36Sopenharmony_ci	tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
58862306a36Sopenharmony_ci	if (tunnel && tunnel->src_port != src_port &&
58962306a36Sopenharmony_ci	    tunnel->dst_port != dst_port) {
59062306a36Sopenharmony_ci		ret = tb_tunnel_consumed_bandwidth(tunnel, &usb3_consumed_up,
59162306a36Sopenharmony_ci						   &usb3_consumed_down);
59262306a36Sopenharmony_ci		if (ret)
59362306a36Sopenharmony_ci			return ret;
59462306a36Sopenharmony_ci	} else {
59562306a36Sopenharmony_ci		usb3_consumed_up = 0;
59662306a36Sopenharmony_ci		usb3_consumed_down = 0;
59762306a36Sopenharmony_ci	}
59862306a36Sopenharmony_ci
59962306a36Sopenharmony_ci	/* Maximum possible bandwidth asymmetric Gen 4 link is 120 Gb/s */
60062306a36Sopenharmony_ci	*available_up = *available_down = 120000;
60162306a36Sopenharmony_ci
60262306a36Sopenharmony_ci	/* Find the minimum available bandwidth over all links */
60362306a36Sopenharmony_ci	tb_for_each_port_on_path(src_port, dst_port, port) {
60462306a36Sopenharmony_ci		int link_speed, link_width, up_bw, down_bw;
60562306a36Sopenharmony_ci
60662306a36Sopenharmony_ci		if (!tb_port_is_null(port))
60762306a36Sopenharmony_ci			continue;
60862306a36Sopenharmony_ci
60962306a36Sopenharmony_ci		if (tb_is_upstream_port(port)) {
61062306a36Sopenharmony_ci			link_speed = port->sw->link_speed;
61162306a36Sopenharmony_ci			/*
61262306a36Sopenharmony_ci			 * sw->link_width is from upstream perspective
61362306a36Sopenharmony_ci			 * so we use the opposite for downstream of the
61462306a36Sopenharmony_ci			 * host router.
61562306a36Sopenharmony_ci			 */
61662306a36Sopenharmony_ci			if (port->sw->link_width == TB_LINK_WIDTH_ASYM_TX) {
61762306a36Sopenharmony_ci				up_bw = link_speed * 3 * 1000;
61862306a36Sopenharmony_ci				down_bw = link_speed * 1 * 1000;
61962306a36Sopenharmony_ci			} else if (port->sw->link_width == TB_LINK_WIDTH_ASYM_RX) {
62062306a36Sopenharmony_ci				up_bw = link_speed * 1 * 1000;
62162306a36Sopenharmony_ci				down_bw = link_speed * 3 * 1000;
62262306a36Sopenharmony_ci			} else {
62362306a36Sopenharmony_ci				up_bw = link_speed * port->sw->link_width * 1000;
62462306a36Sopenharmony_ci				down_bw = up_bw;
62562306a36Sopenharmony_ci			}
62662306a36Sopenharmony_ci		} else {
62762306a36Sopenharmony_ci			link_speed = tb_port_get_link_speed(port);
62862306a36Sopenharmony_ci			if (link_speed < 0)
62962306a36Sopenharmony_ci				return link_speed;
63062306a36Sopenharmony_ci
63162306a36Sopenharmony_ci			link_width = tb_port_get_link_width(port);
63262306a36Sopenharmony_ci			if (link_width < 0)
63362306a36Sopenharmony_ci				return link_width;
63462306a36Sopenharmony_ci
63562306a36Sopenharmony_ci			if (link_width == TB_LINK_WIDTH_ASYM_TX) {
63662306a36Sopenharmony_ci				up_bw = link_speed * 1 * 1000;
63762306a36Sopenharmony_ci				down_bw = link_speed * 3 * 1000;
63862306a36Sopenharmony_ci			} else if (link_width == TB_LINK_WIDTH_ASYM_RX) {
63962306a36Sopenharmony_ci				up_bw = link_speed * 3 * 1000;
64062306a36Sopenharmony_ci				down_bw = link_speed * 1 * 1000;
64162306a36Sopenharmony_ci			} else {
64262306a36Sopenharmony_ci				up_bw = link_speed * link_width * 1000;
64362306a36Sopenharmony_ci				down_bw = up_bw;
64462306a36Sopenharmony_ci			}
64562306a36Sopenharmony_ci		}
64662306a36Sopenharmony_ci
64762306a36Sopenharmony_ci		/* Leave 10% guard band */
64862306a36Sopenharmony_ci		up_bw -= up_bw / 10;
64962306a36Sopenharmony_ci		down_bw -= down_bw / 10;
65062306a36Sopenharmony_ci
65162306a36Sopenharmony_ci		tb_port_dbg(port, "link total bandwidth %d/%d Mb/s\n", up_bw,
65262306a36Sopenharmony_ci			    down_bw);
65362306a36Sopenharmony_ci
65462306a36Sopenharmony_ci		/*
65562306a36Sopenharmony_ci		 * Find all DP tunnels that cross the port and reduce
65662306a36Sopenharmony_ci		 * their consumed bandwidth from the available.
65762306a36Sopenharmony_ci		 */
65862306a36Sopenharmony_ci		list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
65962306a36Sopenharmony_ci			int dp_consumed_up, dp_consumed_down;
66062306a36Sopenharmony_ci
66162306a36Sopenharmony_ci			if (tb_tunnel_is_invalid(tunnel))
66262306a36Sopenharmony_ci				continue;
66362306a36Sopenharmony_ci
66462306a36Sopenharmony_ci			if (!tb_tunnel_is_dp(tunnel))
66562306a36Sopenharmony_ci				continue;
66662306a36Sopenharmony_ci
66762306a36Sopenharmony_ci			if (!tb_tunnel_port_on_path(tunnel, port))
66862306a36Sopenharmony_ci				continue;
66962306a36Sopenharmony_ci
67062306a36Sopenharmony_ci			/*
67162306a36Sopenharmony_ci			 * Ignore the DP tunnel between src_port and
67262306a36Sopenharmony_ci			 * dst_port because it is the same tunnel and we
67362306a36Sopenharmony_ci			 * may be re-calculating estimated bandwidth.
67462306a36Sopenharmony_ci			 */
67562306a36Sopenharmony_ci			if (tunnel->src_port == src_port &&
67662306a36Sopenharmony_ci			    tunnel->dst_port == dst_port)
67762306a36Sopenharmony_ci				continue;
67862306a36Sopenharmony_ci
67962306a36Sopenharmony_ci			ret = tb_tunnel_consumed_bandwidth(tunnel,
68062306a36Sopenharmony_ci							   &dp_consumed_up,
68162306a36Sopenharmony_ci							   &dp_consumed_down);
68262306a36Sopenharmony_ci			if (ret)
68362306a36Sopenharmony_ci				return ret;
68462306a36Sopenharmony_ci
68562306a36Sopenharmony_ci			up_bw -= dp_consumed_up;
68662306a36Sopenharmony_ci			down_bw -= dp_consumed_down;
68762306a36Sopenharmony_ci		}
68862306a36Sopenharmony_ci
68962306a36Sopenharmony_ci		/*
69062306a36Sopenharmony_ci		 * If USB3 is tunneled from the host router down to the
69162306a36Sopenharmony_ci		 * branch leading to port we need to take USB3 consumed
69262306a36Sopenharmony_ci		 * bandwidth into account regardless whether it actually
69362306a36Sopenharmony_ci		 * crosses the port.
69462306a36Sopenharmony_ci		 */
69562306a36Sopenharmony_ci		up_bw -= usb3_consumed_up;
69662306a36Sopenharmony_ci		down_bw -= usb3_consumed_down;
69762306a36Sopenharmony_ci
69862306a36Sopenharmony_ci		if (up_bw < *available_up)
69962306a36Sopenharmony_ci			*available_up = up_bw;
70062306a36Sopenharmony_ci		if (down_bw < *available_down)
70162306a36Sopenharmony_ci			*available_down = down_bw;
70262306a36Sopenharmony_ci	}
70362306a36Sopenharmony_ci
70462306a36Sopenharmony_ci	if (*available_up < 0)
70562306a36Sopenharmony_ci		*available_up = 0;
70662306a36Sopenharmony_ci	if (*available_down < 0)
70762306a36Sopenharmony_ci		*available_down = 0;
70862306a36Sopenharmony_ci
70962306a36Sopenharmony_ci	return 0;
71062306a36Sopenharmony_ci}
71162306a36Sopenharmony_ci
71262306a36Sopenharmony_cistatic int tb_release_unused_usb3_bandwidth(struct tb *tb,
71362306a36Sopenharmony_ci					    struct tb_port *src_port,
71462306a36Sopenharmony_ci					    struct tb_port *dst_port)
71562306a36Sopenharmony_ci{
71662306a36Sopenharmony_ci	struct tb_tunnel *tunnel;
71762306a36Sopenharmony_ci
71862306a36Sopenharmony_ci	tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
71962306a36Sopenharmony_ci	return tunnel ? tb_tunnel_release_unused_bandwidth(tunnel) : 0;
72062306a36Sopenharmony_ci}
72162306a36Sopenharmony_ci
72262306a36Sopenharmony_cistatic void tb_reclaim_usb3_bandwidth(struct tb *tb, struct tb_port *src_port,
72362306a36Sopenharmony_ci				      struct tb_port *dst_port)
72462306a36Sopenharmony_ci{
72562306a36Sopenharmony_ci	int ret, available_up, available_down;
72662306a36Sopenharmony_ci	struct tb_tunnel *tunnel;
72762306a36Sopenharmony_ci
72862306a36Sopenharmony_ci	tunnel = tb_find_first_usb3_tunnel(tb, src_port, dst_port);
72962306a36Sopenharmony_ci	if (!tunnel)
73062306a36Sopenharmony_ci		return;
73162306a36Sopenharmony_ci
73262306a36Sopenharmony_ci	tb_dbg(tb, "reclaiming unused bandwidth for USB3\n");
73362306a36Sopenharmony_ci
73462306a36Sopenharmony_ci	/*
73562306a36Sopenharmony_ci	 * Calculate available bandwidth for the first hop USB3 tunnel.
73662306a36Sopenharmony_ci	 * That determines the whole USB3 bandwidth for this branch.
73762306a36Sopenharmony_ci	 */
73862306a36Sopenharmony_ci	ret = tb_available_bandwidth(tb, tunnel->src_port, tunnel->dst_port,
73962306a36Sopenharmony_ci				     &available_up, &available_down);
74062306a36Sopenharmony_ci	if (ret) {
74162306a36Sopenharmony_ci		tb_warn(tb, "failed to calculate available bandwidth\n");
74262306a36Sopenharmony_ci		return;
74362306a36Sopenharmony_ci	}
74462306a36Sopenharmony_ci
74562306a36Sopenharmony_ci	tb_dbg(tb, "available bandwidth for USB3 %d/%d Mb/s\n",
74662306a36Sopenharmony_ci	       available_up, available_down);
74762306a36Sopenharmony_ci
74862306a36Sopenharmony_ci	tb_tunnel_reclaim_available_bandwidth(tunnel, &available_up, &available_down);
74962306a36Sopenharmony_ci}
75062306a36Sopenharmony_ci
75162306a36Sopenharmony_cistatic int tb_tunnel_usb3(struct tb *tb, struct tb_switch *sw)
75262306a36Sopenharmony_ci{
75362306a36Sopenharmony_ci	struct tb_switch *parent = tb_switch_parent(sw);
75462306a36Sopenharmony_ci	int ret, available_up, available_down;
75562306a36Sopenharmony_ci	struct tb_port *up, *down, *port;
75662306a36Sopenharmony_ci	struct tb_cm *tcm = tb_priv(tb);
75762306a36Sopenharmony_ci	struct tb_tunnel *tunnel;
75862306a36Sopenharmony_ci
75962306a36Sopenharmony_ci	if (!tb_acpi_may_tunnel_usb3()) {
76062306a36Sopenharmony_ci		tb_dbg(tb, "USB3 tunneling disabled, not creating tunnel\n");
76162306a36Sopenharmony_ci		return 0;
76262306a36Sopenharmony_ci	}
76362306a36Sopenharmony_ci
76462306a36Sopenharmony_ci	up = tb_switch_find_port(sw, TB_TYPE_USB3_UP);
76562306a36Sopenharmony_ci	if (!up)
76662306a36Sopenharmony_ci		return 0;
76762306a36Sopenharmony_ci
76862306a36Sopenharmony_ci	if (!sw->link_usb4)
76962306a36Sopenharmony_ci		return 0;
77062306a36Sopenharmony_ci
77162306a36Sopenharmony_ci	/*
77262306a36Sopenharmony_ci	 * Look up available down port. Since we are chaining it should
77362306a36Sopenharmony_ci	 * be found right above this switch.
77462306a36Sopenharmony_ci	 */
77562306a36Sopenharmony_ci	port = tb_switch_downstream_port(sw);
77662306a36Sopenharmony_ci	down = tb_find_usb3_down(parent, port);
77762306a36Sopenharmony_ci	if (!down)
77862306a36Sopenharmony_ci		return 0;
77962306a36Sopenharmony_ci
78062306a36Sopenharmony_ci	if (tb_route(parent)) {
78162306a36Sopenharmony_ci		struct tb_port *parent_up;
78262306a36Sopenharmony_ci		/*
78362306a36Sopenharmony_ci		 * Check first that the parent switch has its upstream USB3
78462306a36Sopenharmony_ci		 * port enabled. Otherwise the chain is not complete and
78562306a36Sopenharmony_ci		 * there is no point setting up a new tunnel.
78662306a36Sopenharmony_ci		 */
78762306a36Sopenharmony_ci		parent_up = tb_switch_find_port(parent, TB_TYPE_USB3_UP);
78862306a36Sopenharmony_ci		if (!parent_up || !tb_port_is_enabled(parent_up))
78962306a36Sopenharmony_ci			return 0;
79062306a36Sopenharmony_ci
79162306a36Sopenharmony_ci		/* Make all unused bandwidth available for the new tunnel */
79262306a36Sopenharmony_ci		ret = tb_release_unused_usb3_bandwidth(tb, down, up);
79362306a36Sopenharmony_ci		if (ret)
79462306a36Sopenharmony_ci			return ret;
79562306a36Sopenharmony_ci	}
79662306a36Sopenharmony_ci
79762306a36Sopenharmony_ci	ret = tb_available_bandwidth(tb, down, up, &available_up,
79862306a36Sopenharmony_ci				     &available_down);
79962306a36Sopenharmony_ci	if (ret)
80062306a36Sopenharmony_ci		goto err_reclaim;
80162306a36Sopenharmony_ci
80262306a36Sopenharmony_ci	tb_port_dbg(up, "available bandwidth for new USB3 tunnel %d/%d Mb/s\n",
80362306a36Sopenharmony_ci		    available_up, available_down);
80462306a36Sopenharmony_ci
80562306a36Sopenharmony_ci	tunnel = tb_tunnel_alloc_usb3(tb, up, down, available_up,
80662306a36Sopenharmony_ci				      available_down);
80762306a36Sopenharmony_ci	if (!tunnel) {
80862306a36Sopenharmony_ci		ret = -ENOMEM;
80962306a36Sopenharmony_ci		goto err_reclaim;
81062306a36Sopenharmony_ci	}
81162306a36Sopenharmony_ci
81262306a36Sopenharmony_ci	if (tb_tunnel_activate(tunnel)) {
81362306a36Sopenharmony_ci		tb_port_info(up,
81462306a36Sopenharmony_ci			     "USB3 tunnel activation failed, aborting\n");
81562306a36Sopenharmony_ci		ret = -EIO;
81662306a36Sopenharmony_ci		goto err_free;
81762306a36Sopenharmony_ci	}
81862306a36Sopenharmony_ci
81962306a36Sopenharmony_ci	list_add_tail(&tunnel->list, &tcm->tunnel_list);
82062306a36Sopenharmony_ci	if (tb_route(parent))
82162306a36Sopenharmony_ci		tb_reclaim_usb3_bandwidth(tb, down, up);
82262306a36Sopenharmony_ci
82362306a36Sopenharmony_ci	return 0;
82462306a36Sopenharmony_ci
82562306a36Sopenharmony_cierr_free:
82662306a36Sopenharmony_ci	tb_tunnel_free(tunnel);
82762306a36Sopenharmony_cierr_reclaim:
82862306a36Sopenharmony_ci	if (tb_route(parent))
82962306a36Sopenharmony_ci		tb_reclaim_usb3_bandwidth(tb, down, up);
83062306a36Sopenharmony_ci
83162306a36Sopenharmony_ci	return ret;
83262306a36Sopenharmony_ci}
83362306a36Sopenharmony_ci
83462306a36Sopenharmony_cistatic int tb_create_usb3_tunnels(struct tb_switch *sw)
83562306a36Sopenharmony_ci{
83662306a36Sopenharmony_ci	struct tb_port *port;
83762306a36Sopenharmony_ci	int ret;
83862306a36Sopenharmony_ci
83962306a36Sopenharmony_ci	if (!tb_acpi_may_tunnel_usb3())
84062306a36Sopenharmony_ci		return 0;
84162306a36Sopenharmony_ci
84262306a36Sopenharmony_ci	if (tb_route(sw)) {
84362306a36Sopenharmony_ci		ret = tb_tunnel_usb3(sw->tb, sw);
84462306a36Sopenharmony_ci		if (ret)
84562306a36Sopenharmony_ci			return ret;
84662306a36Sopenharmony_ci	}
84762306a36Sopenharmony_ci
84862306a36Sopenharmony_ci	tb_switch_for_each_port(sw, port) {
84962306a36Sopenharmony_ci		if (!tb_port_has_remote(port))
85062306a36Sopenharmony_ci			continue;
85162306a36Sopenharmony_ci		ret = tb_create_usb3_tunnels(port->remote->sw);
85262306a36Sopenharmony_ci		if (ret)
85362306a36Sopenharmony_ci			return ret;
85462306a36Sopenharmony_ci	}
85562306a36Sopenharmony_ci
85662306a36Sopenharmony_ci	return 0;
85762306a36Sopenharmony_ci}
85862306a36Sopenharmony_ci
85962306a36Sopenharmony_cistatic void tb_scan_port(struct tb_port *port);
86062306a36Sopenharmony_ci
86162306a36Sopenharmony_ci/*
86262306a36Sopenharmony_ci * tb_scan_switch() - scan for and initialize downstream switches
86362306a36Sopenharmony_ci */
86462306a36Sopenharmony_cistatic void tb_scan_switch(struct tb_switch *sw)
86562306a36Sopenharmony_ci{
86662306a36Sopenharmony_ci	struct tb_port *port;
86762306a36Sopenharmony_ci
86862306a36Sopenharmony_ci	pm_runtime_get_sync(&sw->dev);
86962306a36Sopenharmony_ci
87062306a36Sopenharmony_ci	tb_switch_for_each_port(sw, port)
87162306a36Sopenharmony_ci		tb_scan_port(port);
87262306a36Sopenharmony_ci
87362306a36Sopenharmony_ci	pm_runtime_mark_last_busy(&sw->dev);
87462306a36Sopenharmony_ci	pm_runtime_put_autosuspend(&sw->dev);
87562306a36Sopenharmony_ci}
87662306a36Sopenharmony_ci
87762306a36Sopenharmony_ci/*
87862306a36Sopenharmony_ci * tb_scan_port() - check for and initialize switches below port
87962306a36Sopenharmony_ci */
88062306a36Sopenharmony_cistatic void tb_scan_port(struct tb_port *port)
88162306a36Sopenharmony_ci{
88262306a36Sopenharmony_ci	struct tb_cm *tcm = tb_priv(port->sw->tb);
88362306a36Sopenharmony_ci	struct tb_port *upstream_port;
88462306a36Sopenharmony_ci	bool discovery = false;
88562306a36Sopenharmony_ci	struct tb_switch *sw;
88662306a36Sopenharmony_ci
88762306a36Sopenharmony_ci	if (tb_is_upstream_port(port))
88862306a36Sopenharmony_ci		return;
88962306a36Sopenharmony_ci
89062306a36Sopenharmony_ci	if (tb_port_is_dpout(port) && tb_dp_port_hpd_is_active(port) == 1 &&
89162306a36Sopenharmony_ci	    !tb_dp_port_is_enabled(port)) {
89262306a36Sopenharmony_ci		tb_port_dbg(port, "DP adapter HPD set, queuing hotplug\n");
89362306a36Sopenharmony_ci		tb_queue_hotplug(port->sw->tb, tb_route(port->sw), port->port,
89462306a36Sopenharmony_ci				 false);
89562306a36Sopenharmony_ci		return;
89662306a36Sopenharmony_ci	}
89762306a36Sopenharmony_ci
89862306a36Sopenharmony_ci	if (port->config.type != TB_TYPE_PORT)
89962306a36Sopenharmony_ci		return;
90062306a36Sopenharmony_ci	if (port->dual_link_port && port->link_nr)
90162306a36Sopenharmony_ci		return; /*
90262306a36Sopenharmony_ci			 * Downstream switch is reachable through two ports.
90362306a36Sopenharmony_ci			 * Only scan on the primary port (link_nr == 0).
90462306a36Sopenharmony_ci			 */
90562306a36Sopenharmony_ci
90662306a36Sopenharmony_ci	if (port->usb4)
90762306a36Sopenharmony_ci		pm_runtime_get_sync(&port->usb4->dev);
90862306a36Sopenharmony_ci
90962306a36Sopenharmony_ci	if (tb_wait_for_port(port, false) <= 0)
91062306a36Sopenharmony_ci		goto out_rpm_put;
91162306a36Sopenharmony_ci	if (port->remote) {
91262306a36Sopenharmony_ci		tb_port_dbg(port, "port already has a remote\n");
91362306a36Sopenharmony_ci		goto out_rpm_put;
91462306a36Sopenharmony_ci	}
91562306a36Sopenharmony_ci
91662306a36Sopenharmony_ci	tb_retimer_scan(port, true);
91762306a36Sopenharmony_ci
91862306a36Sopenharmony_ci	sw = tb_switch_alloc(port->sw->tb, &port->sw->dev,
91962306a36Sopenharmony_ci			     tb_downstream_route(port));
92062306a36Sopenharmony_ci	if (IS_ERR(sw)) {
92162306a36Sopenharmony_ci		/*
92262306a36Sopenharmony_ci		 * If there is an error accessing the connected switch
92362306a36Sopenharmony_ci		 * it may be connected to another domain. Also we allow
92462306a36Sopenharmony_ci		 * the other domain to be connected to a max depth switch.
92562306a36Sopenharmony_ci		 */
92662306a36Sopenharmony_ci		if (PTR_ERR(sw) == -EIO || PTR_ERR(sw) == -EADDRNOTAVAIL)
92762306a36Sopenharmony_ci			tb_scan_xdomain(port);
92862306a36Sopenharmony_ci		goto out_rpm_put;
92962306a36Sopenharmony_ci	}
93062306a36Sopenharmony_ci
93162306a36Sopenharmony_ci	if (tb_switch_configure(sw)) {
93262306a36Sopenharmony_ci		tb_switch_put(sw);
93362306a36Sopenharmony_ci		goto out_rpm_put;
93462306a36Sopenharmony_ci	}
93562306a36Sopenharmony_ci
93662306a36Sopenharmony_ci	/*
93762306a36Sopenharmony_ci	 * If there was previously another domain connected remove it
93862306a36Sopenharmony_ci	 * first.
93962306a36Sopenharmony_ci	 */
94062306a36Sopenharmony_ci	if (port->xdomain) {
94162306a36Sopenharmony_ci		tb_xdomain_remove(port->xdomain);
94262306a36Sopenharmony_ci		tb_port_unconfigure_xdomain(port);
94362306a36Sopenharmony_ci		port->xdomain = NULL;
94462306a36Sopenharmony_ci	}
94562306a36Sopenharmony_ci
94662306a36Sopenharmony_ci	/*
94762306a36Sopenharmony_ci	 * Do not send uevents until we have discovered all existing
94862306a36Sopenharmony_ci	 * tunnels and know which switches were authorized already by
94962306a36Sopenharmony_ci	 * the boot firmware.
95062306a36Sopenharmony_ci	 */
95162306a36Sopenharmony_ci	if (!tcm->hotplug_active) {
95262306a36Sopenharmony_ci		dev_set_uevent_suppress(&sw->dev, true);
95362306a36Sopenharmony_ci		discovery = true;
95462306a36Sopenharmony_ci	}
95562306a36Sopenharmony_ci
95662306a36Sopenharmony_ci	/*
95762306a36Sopenharmony_ci	 * At the moment Thunderbolt 2 and beyond (devices with LC) we
95862306a36Sopenharmony_ci	 * can support runtime PM.
95962306a36Sopenharmony_ci	 */
96062306a36Sopenharmony_ci	sw->rpm = sw->generation > 1;
96162306a36Sopenharmony_ci
96262306a36Sopenharmony_ci	if (tb_switch_add(sw)) {
96362306a36Sopenharmony_ci		tb_switch_put(sw);
96462306a36Sopenharmony_ci		goto out_rpm_put;
96562306a36Sopenharmony_ci	}
96662306a36Sopenharmony_ci
96762306a36Sopenharmony_ci	/* Link the switches using both links if available */
96862306a36Sopenharmony_ci	upstream_port = tb_upstream_port(sw);
96962306a36Sopenharmony_ci	port->remote = upstream_port;
97062306a36Sopenharmony_ci	upstream_port->remote = port;
97162306a36Sopenharmony_ci	if (port->dual_link_port && upstream_port->dual_link_port) {
97262306a36Sopenharmony_ci		port->dual_link_port->remote = upstream_port->dual_link_port;
97362306a36Sopenharmony_ci		upstream_port->dual_link_port->remote = port->dual_link_port;
97462306a36Sopenharmony_ci	}
97562306a36Sopenharmony_ci
97662306a36Sopenharmony_ci	/* Enable lane bonding if supported */
97762306a36Sopenharmony_ci	tb_switch_lane_bonding_enable(sw);
97862306a36Sopenharmony_ci	/* Set the link configured */
97962306a36Sopenharmony_ci	tb_switch_configure_link(sw);
98062306a36Sopenharmony_ci	/*
98162306a36Sopenharmony_ci	 * CL0s and CL1 are enabled and supported together.
98262306a36Sopenharmony_ci	 * Silently ignore CLx enabling in case CLx is not supported.
98362306a36Sopenharmony_ci	 */
98462306a36Sopenharmony_ci	if (discovery)
98562306a36Sopenharmony_ci		tb_sw_dbg(sw, "discovery, not touching CL states\n");
98662306a36Sopenharmony_ci	else if (tb_enable_clx(sw))
98762306a36Sopenharmony_ci		tb_sw_warn(sw, "failed to enable CL states\n");
98862306a36Sopenharmony_ci
98962306a36Sopenharmony_ci	if (tb_enable_tmu(sw))
99062306a36Sopenharmony_ci		tb_sw_warn(sw, "failed to enable TMU\n");
99162306a36Sopenharmony_ci
99262306a36Sopenharmony_ci	/*
99362306a36Sopenharmony_ci	 * Configuration valid needs to be set after the TMU has been
99462306a36Sopenharmony_ci	 * enabled for the upstream port of the router so we do it here.
99562306a36Sopenharmony_ci	 */
99662306a36Sopenharmony_ci	tb_switch_configuration_valid(sw);
99762306a36Sopenharmony_ci
99862306a36Sopenharmony_ci	/* Scan upstream retimers */
99962306a36Sopenharmony_ci	tb_retimer_scan(upstream_port, true);
100062306a36Sopenharmony_ci
100162306a36Sopenharmony_ci	/*
100262306a36Sopenharmony_ci	 * Create USB 3.x tunnels only when the switch is plugged to the
100362306a36Sopenharmony_ci	 * domain. This is because we scan the domain also during discovery
100462306a36Sopenharmony_ci	 * and want to discover existing USB 3.x tunnels before we create
100562306a36Sopenharmony_ci	 * any new.
100662306a36Sopenharmony_ci	 */
100762306a36Sopenharmony_ci	if (tcm->hotplug_active && tb_tunnel_usb3(sw->tb, sw))
100862306a36Sopenharmony_ci		tb_sw_warn(sw, "USB3 tunnel creation failed\n");
100962306a36Sopenharmony_ci
101062306a36Sopenharmony_ci	tb_add_dp_resources(sw);
101162306a36Sopenharmony_ci	tb_scan_switch(sw);
101262306a36Sopenharmony_ci
101362306a36Sopenharmony_ciout_rpm_put:
101462306a36Sopenharmony_ci	if (port->usb4) {
101562306a36Sopenharmony_ci		pm_runtime_mark_last_busy(&port->usb4->dev);
101662306a36Sopenharmony_ci		pm_runtime_put_autosuspend(&port->usb4->dev);
101762306a36Sopenharmony_ci	}
101862306a36Sopenharmony_ci}
101962306a36Sopenharmony_ci
102062306a36Sopenharmony_cistatic void tb_deactivate_and_free_tunnel(struct tb_tunnel *tunnel)
102162306a36Sopenharmony_ci{
102262306a36Sopenharmony_ci	struct tb_port *src_port, *dst_port;
102362306a36Sopenharmony_ci	struct tb *tb;
102462306a36Sopenharmony_ci
102562306a36Sopenharmony_ci	if (!tunnel)
102662306a36Sopenharmony_ci		return;
102762306a36Sopenharmony_ci
102862306a36Sopenharmony_ci	tb_tunnel_deactivate(tunnel);
102962306a36Sopenharmony_ci	list_del(&tunnel->list);
103062306a36Sopenharmony_ci
103162306a36Sopenharmony_ci	tb = tunnel->tb;
103262306a36Sopenharmony_ci	src_port = tunnel->src_port;
103362306a36Sopenharmony_ci	dst_port = tunnel->dst_port;
103462306a36Sopenharmony_ci
103562306a36Sopenharmony_ci	switch (tunnel->type) {
103662306a36Sopenharmony_ci	case TB_TUNNEL_DP:
103762306a36Sopenharmony_ci		tb_detach_bandwidth_group(src_port);
103862306a36Sopenharmony_ci		/*
103962306a36Sopenharmony_ci		 * In case of DP tunnel make sure the DP IN resource is
104062306a36Sopenharmony_ci		 * deallocated properly.
104162306a36Sopenharmony_ci		 */
104262306a36Sopenharmony_ci		tb_switch_dealloc_dp_resource(src_port->sw, src_port);
104362306a36Sopenharmony_ci		/* Now we can allow the domain to runtime suspend again */
104462306a36Sopenharmony_ci		pm_runtime_mark_last_busy(&dst_port->sw->dev);
104562306a36Sopenharmony_ci		pm_runtime_put_autosuspend(&dst_port->sw->dev);
104662306a36Sopenharmony_ci		pm_runtime_mark_last_busy(&src_port->sw->dev);
104762306a36Sopenharmony_ci		pm_runtime_put_autosuspend(&src_port->sw->dev);
104862306a36Sopenharmony_ci		fallthrough;
104962306a36Sopenharmony_ci
105062306a36Sopenharmony_ci	case TB_TUNNEL_USB3:
105162306a36Sopenharmony_ci		tb_reclaim_usb3_bandwidth(tb, src_port, dst_port);
105262306a36Sopenharmony_ci		break;
105362306a36Sopenharmony_ci
105462306a36Sopenharmony_ci	default:
105562306a36Sopenharmony_ci		/*
105662306a36Sopenharmony_ci		 * PCIe and DMA tunnels do not consume guaranteed
105762306a36Sopenharmony_ci		 * bandwidth.
105862306a36Sopenharmony_ci		 */
105962306a36Sopenharmony_ci		break;
106062306a36Sopenharmony_ci	}
106162306a36Sopenharmony_ci
106262306a36Sopenharmony_ci	tb_tunnel_free(tunnel);
106362306a36Sopenharmony_ci}
106462306a36Sopenharmony_ci
106562306a36Sopenharmony_ci/*
106662306a36Sopenharmony_ci * tb_free_invalid_tunnels() - destroy tunnels of devices that have gone away
106762306a36Sopenharmony_ci */
106862306a36Sopenharmony_cistatic void tb_free_invalid_tunnels(struct tb *tb)
106962306a36Sopenharmony_ci{
107062306a36Sopenharmony_ci	struct tb_cm *tcm = tb_priv(tb);
107162306a36Sopenharmony_ci	struct tb_tunnel *tunnel;
107262306a36Sopenharmony_ci	struct tb_tunnel *n;
107362306a36Sopenharmony_ci
107462306a36Sopenharmony_ci	list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
107562306a36Sopenharmony_ci		if (tb_tunnel_is_invalid(tunnel))
107662306a36Sopenharmony_ci			tb_deactivate_and_free_tunnel(tunnel);
107762306a36Sopenharmony_ci	}
107862306a36Sopenharmony_ci}
107962306a36Sopenharmony_ci
108062306a36Sopenharmony_ci/*
108162306a36Sopenharmony_ci * tb_free_unplugged_children() - traverse hierarchy and free unplugged switches
108262306a36Sopenharmony_ci */
108362306a36Sopenharmony_cistatic void tb_free_unplugged_children(struct tb_switch *sw)
108462306a36Sopenharmony_ci{
108562306a36Sopenharmony_ci	struct tb_port *port;
108662306a36Sopenharmony_ci
108762306a36Sopenharmony_ci	tb_switch_for_each_port(sw, port) {
108862306a36Sopenharmony_ci		if (!tb_port_has_remote(port))
108962306a36Sopenharmony_ci			continue;
109062306a36Sopenharmony_ci
109162306a36Sopenharmony_ci		if (port->remote->sw->is_unplugged) {
109262306a36Sopenharmony_ci			tb_retimer_remove_all(port);
109362306a36Sopenharmony_ci			tb_remove_dp_resources(port->remote->sw);
109462306a36Sopenharmony_ci			tb_switch_unconfigure_link(port->remote->sw);
109562306a36Sopenharmony_ci			tb_switch_lane_bonding_disable(port->remote->sw);
109662306a36Sopenharmony_ci			tb_switch_remove(port->remote->sw);
109762306a36Sopenharmony_ci			port->remote = NULL;
109862306a36Sopenharmony_ci			if (port->dual_link_port)
109962306a36Sopenharmony_ci				port->dual_link_port->remote = NULL;
110062306a36Sopenharmony_ci		} else {
110162306a36Sopenharmony_ci			tb_free_unplugged_children(port->remote->sw);
110262306a36Sopenharmony_ci		}
110362306a36Sopenharmony_ci	}
110462306a36Sopenharmony_ci}
110562306a36Sopenharmony_ci
110662306a36Sopenharmony_cistatic struct tb_port *tb_find_pcie_down(struct tb_switch *sw,
110762306a36Sopenharmony_ci					 const struct tb_port *port)
110862306a36Sopenharmony_ci{
110962306a36Sopenharmony_ci	struct tb_port *down = NULL;
111062306a36Sopenharmony_ci
111162306a36Sopenharmony_ci	/*
111262306a36Sopenharmony_ci	 * To keep plugging devices consistently in the same PCIe
111362306a36Sopenharmony_ci	 * hierarchy, do mapping here for switch downstream PCIe ports.
111462306a36Sopenharmony_ci	 */
111562306a36Sopenharmony_ci	if (tb_switch_is_usb4(sw)) {
111662306a36Sopenharmony_ci		down = usb4_switch_map_pcie_down(sw, port);
111762306a36Sopenharmony_ci	} else if (!tb_route(sw)) {
111862306a36Sopenharmony_ci		int phy_port = tb_phy_port_from_link(port->port);
111962306a36Sopenharmony_ci		int index;
112062306a36Sopenharmony_ci
112162306a36Sopenharmony_ci		/*
112262306a36Sopenharmony_ci		 * Hard-coded Thunderbolt port to PCIe down port mapping
112362306a36Sopenharmony_ci		 * per controller.
112462306a36Sopenharmony_ci		 */
112562306a36Sopenharmony_ci		if (tb_switch_is_cactus_ridge(sw) ||
112662306a36Sopenharmony_ci		    tb_switch_is_alpine_ridge(sw))
112762306a36Sopenharmony_ci			index = !phy_port ? 6 : 7;
112862306a36Sopenharmony_ci		else if (tb_switch_is_falcon_ridge(sw))
112962306a36Sopenharmony_ci			index = !phy_port ? 6 : 8;
113062306a36Sopenharmony_ci		else if (tb_switch_is_titan_ridge(sw))
113162306a36Sopenharmony_ci			index = !phy_port ? 8 : 9;
113262306a36Sopenharmony_ci		else
113362306a36Sopenharmony_ci			goto out;
113462306a36Sopenharmony_ci
113562306a36Sopenharmony_ci		/* Validate the hard-coding */
113662306a36Sopenharmony_ci		if (WARN_ON(index > sw->config.max_port_number))
113762306a36Sopenharmony_ci			goto out;
113862306a36Sopenharmony_ci
113962306a36Sopenharmony_ci		down = &sw->ports[index];
114062306a36Sopenharmony_ci	}
114162306a36Sopenharmony_ci
114262306a36Sopenharmony_ci	if (down) {
114362306a36Sopenharmony_ci		if (WARN_ON(!tb_port_is_pcie_down(down)))
114462306a36Sopenharmony_ci			goto out;
114562306a36Sopenharmony_ci		if (tb_pci_port_is_enabled(down))
114662306a36Sopenharmony_ci			goto out;
114762306a36Sopenharmony_ci
114862306a36Sopenharmony_ci		return down;
114962306a36Sopenharmony_ci	}
115062306a36Sopenharmony_ci
115162306a36Sopenharmony_ciout:
115262306a36Sopenharmony_ci	return tb_find_unused_port(sw, TB_TYPE_PCIE_DOWN);
115362306a36Sopenharmony_ci}
115462306a36Sopenharmony_ci
115562306a36Sopenharmony_cistatic void
115662306a36Sopenharmony_citb_recalc_estimated_bandwidth_for_group(struct tb_bandwidth_group *group)
115762306a36Sopenharmony_ci{
115862306a36Sopenharmony_ci	struct tb_tunnel *first_tunnel;
115962306a36Sopenharmony_ci	struct tb *tb = group->tb;
116062306a36Sopenharmony_ci	struct tb_port *in;
116162306a36Sopenharmony_ci	int ret;
116262306a36Sopenharmony_ci
116362306a36Sopenharmony_ci	tb_dbg(tb, "re-calculating bandwidth estimation for group %u\n",
116462306a36Sopenharmony_ci	       group->index);
116562306a36Sopenharmony_ci
116662306a36Sopenharmony_ci	first_tunnel = NULL;
116762306a36Sopenharmony_ci	list_for_each_entry(in, &group->ports, group_list) {
116862306a36Sopenharmony_ci		int estimated_bw, estimated_up, estimated_down;
116962306a36Sopenharmony_ci		struct tb_tunnel *tunnel;
117062306a36Sopenharmony_ci		struct tb_port *out;
117162306a36Sopenharmony_ci
117262306a36Sopenharmony_ci		if (!usb4_dp_port_bandwidth_mode_enabled(in))
117362306a36Sopenharmony_ci			continue;
117462306a36Sopenharmony_ci
117562306a36Sopenharmony_ci		tunnel = tb_find_tunnel(tb, TB_TUNNEL_DP, in, NULL);
117662306a36Sopenharmony_ci		if (WARN_ON(!tunnel))
117762306a36Sopenharmony_ci			break;
117862306a36Sopenharmony_ci
117962306a36Sopenharmony_ci		if (!first_tunnel) {
118062306a36Sopenharmony_ci			/*
118162306a36Sopenharmony_ci			 * Since USB3 bandwidth is shared by all DP
118262306a36Sopenharmony_ci			 * tunnels under the host router USB4 port, even
118362306a36Sopenharmony_ci			 * if they do not begin from the host router, we
118462306a36Sopenharmony_ci			 * can release USB3 bandwidth just once and not
118562306a36Sopenharmony_ci			 * for each tunnel separately.
118662306a36Sopenharmony_ci			 */
118762306a36Sopenharmony_ci			first_tunnel = tunnel;
118862306a36Sopenharmony_ci			ret = tb_release_unused_usb3_bandwidth(tb,
118962306a36Sopenharmony_ci				first_tunnel->src_port, first_tunnel->dst_port);
119062306a36Sopenharmony_ci			if (ret) {
119162306a36Sopenharmony_ci				tb_port_warn(in,
119262306a36Sopenharmony_ci					"failed to release unused bandwidth\n");
119362306a36Sopenharmony_ci				break;
119462306a36Sopenharmony_ci			}
119562306a36Sopenharmony_ci		}
119662306a36Sopenharmony_ci
119762306a36Sopenharmony_ci		out = tunnel->dst_port;
119862306a36Sopenharmony_ci		ret = tb_available_bandwidth(tb, in, out, &estimated_up,
119962306a36Sopenharmony_ci					     &estimated_down);
120062306a36Sopenharmony_ci		if (ret) {
120162306a36Sopenharmony_ci			tb_port_warn(in,
120262306a36Sopenharmony_ci				"failed to re-calculate estimated bandwidth\n");
120362306a36Sopenharmony_ci			break;
120462306a36Sopenharmony_ci		}
120562306a36Sopenharmony_ci
120662306a36Sopenharmony_ci		/*
120762306a36Sopenharmony_ci		 * Estimated bandwidth includes:
120862306a36Sopenharmony_ci		 *  - already allocated bandwidth for the DP tunnel
120962306a36Sopenharmony_ci		 *  - available bandwidth along the path
121062306a36Sopenharmony_ci		 *  - bandwidth allocated for USB 3.x but not used.
121162306a36Sopenharmony_ci		 */
121262306a36Sopenharmony_ci		tb_port_dbg(in, "re-calculated estimated bandwidth %u/%u Mb/s\n",
121362306a36Sopenharmony_ci			    estimated_up, estimated_down);
121462306a36Sopenharmony_ci
121562306a36Sopenharmony_ci		if (in->sw->config.depth < out->sw->config.depth)
121662306a36Sopenharmony_ci			estimated_bw = estimated_down;
121762306a36Sopenharmony_ci		else
121862306a36Sopenharmony_ci			estimated_bw = estimated_up;
121962306a36Sopenharmony_ci
122062306a36Sopenharmony_ci		if (usb4_dp_port_set_estimated_bandwidth(in, estimated_bw))
122162306a36Sopenharmony_ci			tb_port_warn(in, "failed to update estimated bandwidth\n");
122262306a36Sopenharmony_ci	}
122362306a36Sopenharmony_ci
122462306a36Sopenharmony_ci	if (first_tunnel)
122562306a36Sopenharmony_ci		tb_reclaim_usb3_bandwidth(tb, first_tunnel->src_port,
122662306a36Sopenharmony_ci					  first_tunnel->dst_port);
122762306a36Sopenharmony_ci
122862306a36Sopenharmony_ci	tb_dbg(tb, "bandwidth estimation for group %u done\n", group->index);
122962306a36Sopenharmony_ci}
123062306a36Sopenharmony_ci
123162306a36Sopenharmony_cistatic void tb_recalc_estimated_bandwidth(struct tb *tb)
123262306a36Sopenharmony_ci{
123362306a36Sopenharmony_ci	struct tb_cm *tcm = tb_priv(tb);
123462306a36Sopenharmony_ci	int i;
123562306a36Sopenharmony_ci
123662306a36Sopenharmony_ci	tb_dbg(tb, "bandwidth consumption changed, re-calculating estimated bandwidth\n");
123762306a36Sopenharmony_ci
123862306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(tcm->groups); i++) {
123962306a36Sopenharmony_ci		struct tb_bandwidth_group *group = &tcm->groups[i];
124062306a36Sopenharmony_ci
124162306a36Sopenharmony_ci		if (!list_empty(&group->ports))
124262306a36Sopenharmony_ci			tb_recalc_estimated_bandwidth_for_group(group);
124362306a36Sopenharmony_ci	}
124462306a36Sopenharmony_ci
124562306a36Sopenharmony_ci	tb_dbg(tb, "bandwidth re-calculation done\n");
124662306a36Sopenharmony_ci}
124762306a36Sopenharmony_ci
124862306a36Sopenharmony_cistatic struct tb_port *tb_find_dp_out(struct tb *tb, struct tb_port *in)
124962306a36Sopenharmony_ci{
125062306a36Sopenharmony_ci	struct tb_port *host_port, *port;
125162306a36Sopenharmony_ci	struct tb_cm *tcm = tb_priv(tb);
125262306a36Sopenharmony_ci
125362306a36Sopenharmony_ci	host_port = tb_route(in->sw) ?
125462306a36Sopenharmony_ci		tb_port_at(tb_route(in->sw), tb->root_switch) : NULL;
125562306a36Sopenharmony_ci
125662306a36Sopenharmony_ci	list_for_each_entry(port, &tcm->dp_resources, list) {
125762306a36Sopenharmony_ci		if (!tb_port_is_dpout(port))
125862306a36Sopenharmony_ci			continue;
125962306a36Sopenharmony_ci
126062306a36Sopenharmony_ci		if (tb_port_is_enabled(port)) {
126162306a36Sopenharmony_ci			tb_port_dbg(port, "DP OUT in use\n");
126262306a36Sopenharmony_ci			continue;
126362306a36Sopenharmony_ci		}
126462306a36Sopenharmony_ci
126562306a36Sopenharmony_ci		tb_port_dbg(port, "DP OUT available\n");
126662306a36Sopenharmony_ci
126762306a36Sopenharmony_ci		/*
126862306a36Sopenharmony_ci		 * Keep the DP tunnel under the topology starting from
126962306a36Sopenharmony_ci		 * the same host router downstream port.
127062306a36Sopenharmony_ci		 */
127162306a36Sopenharmony_ci		if (host_port && tb_route(port->sw)) {
127262306a36Sopenharmony_ci			struct tb_port *p;
127362306a36Sopenharmony_ci
127462306a36Sopenharmony_ci			p = tb_port_at(tb_route(port->sw), tb->root_switch);
127562306a36Sopenharmony_ci			if (p != host_port)
127662306a36Sopenharmony_ci				continue;
127762306a36Sopenharmony_ci		}
127862306a36Sopenharmony_ci
127962306a36Sopenharmony_ci		return port;
128062306a36Sopenharmony_ci	}
128162306a36Sopenharmony_ci
128262306a36Sopenharmony_ci	return NULL;
128362306a36Sopenharmony_ci}
128462306a36Sopenharmony_ci
128562306a36Sopenharmony_cistatic void tb_tunnel_dp(struct tb *tb)
128662306a36Sopenharmony_ci{
128762306a36Sopenharmony_ci	int available_up, available_down, ret, link_nr;
128862306a36Sopenharmony_ci	struct tb_cm *tcm = tb_priv(tb);
128962306a36Sopenharmony_ci	struct tb_port *port, *in, *out;
129062306a36Sopenharmony_ci	struct tb_tunnel *tunnel;
129162306a36Sopenharmony_ci
129262306a36Sopenharmony_ci	if (!tb_acpi_may_tunnel_dp()) {
129362306a36Sopenharmony_ci		tb_dbg(tb, "DP tunneling disabled, not creating tunnel\n");
129462306a36Sopenharmony_ci		return;
129562306a36Sopenharmony_ci	}
129662306a36Sopenharmony_ci
129762306a36Sopenharmony_ci	/*
129862306a36Sopenharmony_ci	 * Find pair of inactive DP IN and DP OUT adapters and then
129962306a36Sopenharmony_ci	 * establish a DP tunnel between them.
130062306a36Sopenharmony_ci	 */
130162306a36Sopenharmony_ci	tb_dbg(tb, "looking for DP IN <-> DP OUT pairs:\n");
130262306a36Sopenharmony_ci
130362306a36Sopenharmony_ci	in = NULL;
130462306a36Sopenharmony_ci	out = NULL;
130562306a36Sopenharmony_ci	list_for_each_entry(port, &tcm->dp_resources, list) {
130662306a36Sopenharmony_ci		if (!tb_port_is_dpin(port))
130762306a36Sopenharmony_ci			continue;
130862306a36Sopenharmony_ci
130962306a36Sopenharmony_ci		if (tb_port_is_enabled(port)) {
131062306a36Sopenharmony_ci			tb_port_dbg(port, "DP IN in use\n");
131162306a36Sopenharmony_ci			continue;
131262306a36Sopenharmony_ci		}
131362306a36Sopenharmony_ci
131462306a36Sopenharmony_ci		tb_port_dbg(port, "DP IN available\n");
131562306a36Sopenharmony_ci
131662306a36Sopenharmony_ci		out = tb_find_dp_out(tb, port);
131762306a36Sopenharmony_ci		if (out) {
131862306a36Sopenharmony_ci			in = port;
131962306a36Sopenharmony_ci			break;
132062306a36Sopenharmony_ci		}
132162306a36Sopenharmony_ci	}
132262306a36Sopenharmony_ci
132362306a36Sopenharmony_ci	if (!in) {
132462306a36Sopenharmony_ci		tb_dbg(tb, "no suitable DP IN adapter available, not tunneling\n");
132562306a36Sopenharmony_ci		return;
132662306a36Sopenharmony_ci	}
132762306a36Sopenharmony_ci	if (!out) {
132862306a36Sopenharmony_ci		tb_dbg(tb, "no suitable DP OUT adapter available, not tunneling\n");
132962306a36Sopenharmony_ci		return;
133062306a36Sopenharmony_ci	}
133162306a36Sopenharmony_ci
133262306a36Sopenharmony_ci	/*
133362306a36Sopenharmony_ci	 * This is only applicable to links that are not bonded (so
133462306a36Sopenharmony_ci	 * when Thunderbolt 1 hardware is involved somewhere in the
133562306a36Sopenharmony_ci	 * topology). For these try to share the DP bandwidth between
133662306a36Sopenharmony_ci	 * the two lanes.
133762306a36Sopenharmony_ci	 */
133862306a36Sopenharmony_ci	link_nr = 1;
133962306a36Sopenharmony_ci	list_for_each_entry(tunnel, &tcm->tunnel_list, list) {
134062306a36Sopenharmony_ci		if (tb_tunnel_is_dp(tunnel)) {
134162306a36Sopenharmony_ci			link_nr = 0;
134262306a36Sopenharmony_ci			break;
134362306a36Sopenharmony_ci		}
134462306a36Sopenharmony_ci	}
134562306a36Sopenharmony_ci
134662306a36Sopenharmony_ci	/*
134762306a36Sopenharmony_ci	 * DP stream needs the domain to be active so runtime resume
134862306a36Sopenharmony_ci	 * both ends of the tunnel.
134962306a36Sopenharmony_ci	 *
135062306a36Sopenharmony_ci	 * This should bring the routers in the middle active as well
135162306a36Sopenharmony_ci	 * and keeps the domain from runtime suspending while the DP
135262306a36Sopenharmony_ci	 * tunnel is active.
135362306a36Sopenharmony_ci	 */
135462306a36Sopenharmony_ci	pm_runtime_get_sync(&in->sw->dev);
135562306a36Sopenharmony_ci	pm_runtime_get_sync(&out->sw->dev);
135662306a36Sopenharmony_ci
135762306a36Sopenharmony_ci	if (tb_switch_alloc_dp_resource(in->sw, in)) {
135862306a36Sopenharmony_ci		tb_port_dbg(in, "no resource available for DP IN, not tunneling\n");
135962306a36Sopenharmony_ci		goto err_rpm_put;
136062306a36Sopenharmony_ci	}
136162306a36Sopenharmony_ci
136262306a36Sopenharmony_ci	if (!tb_attach_bandwidth_group(tcm, in, out))
136362306a36Sopenharmony_ci		goto err_dealloc_dp;
136462306a36Sopenharmony_ci
136562306a36Sopenharmony_ci	/* Make all unused USB3 bandwidth available for the new DP tunnel */
136662306a36Sopenharmony_ci	ret = tb_release_unused_usb3_bandwidth(tb, in, out);
136762306a36Sopenharmony_ci	if (ret) {
136862306a36Sopenharmony_ci		tb_warn(tb, "failed to release unused bandwidth\n");
136962306a36Sopenharmony_ci		goto err_detach_group;
137062306a36Sopenharmony_ci	}
137162306a36Sopenharmony_ci
137262306a36Sopenharmony_ci	ret = tb_available_bandwidth(tb, in, out, &available_up, &available_down);
137362306a36Sopenharmony_ci	if (ret)
137462306a36Sopenharmony_ci		goto err_reclaim_usb;
137562306a36Sopenharmony_ci
137662306a36Sopenharmony_ci	tb_dbg(tb, "available bandwidth for new DP tunnel %u/%u Mb/s\n",
137762306a36Sopenharmony_ci	       available_up, available_down);
137862306a36Sopenharmony_ci
137962306a36Sopenharmony_ci	tunnel = tb_tunnel_alloc_dp(tb, in, out, link_nr, available_up,
138062306a36Sopenharmony_ci				    available_down);
138162306a36Sopenharmony_ci	if (!tunnel) {
138262306a36Sopenharmony_ci		tb_port_dbg(out, "could not allocate DP tunnel\n");
138362306a36Sopenharmony_ci		goto err_reclaim_usb;
138462306a36Sopenharmony_ci	}
138562306a36Sopenharmony_ci
138662306a36Sopenharmony_ci	if (tb_tunnel_activate(tunnel)) {
138762306a36Sopenharmony_ci		tb_port_info(out, "DP tunnel activation failed, aborting\n");
138862306a36Sopenharmony_ci		goto err_free;
138962306a36Sopenharmony_ci	}
139062306a36Sopenharmony_ci
139162306a36Sopenharmony_ci	list_add_tail(&tunnel->list, &tcm->tunnel_list);
139262306a36Sopenharmony_ci	tb_reclaim_usb3_bandwidth(tb, in, out);
139362306a36Sopenharmony_ci
139462306a36Sopenharmony_ci	/* Update the domain with the new bandwidth estimation */
139562306a36Sopenharmony_ci	tb_recalc_estimated_bandwidth(tb);
139662306a36Sopenharmony_ci
139762306a36Sopenharmony_ci	/*
139862306a36Sopenharmony_ci	 * In case of DP tunnel exists, change host router's 1st children
139962306a36Sopenharmony_ci	 * TMU mode to HiFi for CL0s to work.
140062306a36Sopenharmony_ci	 */
140162306a36Sopenharmony_ci	tb_increase_tmu_accuracy(tunnel);
140262306a36Sopenharmony_ci	return;
140362306a36Sopenharmony_ci
140462306a36Sopenharmony_cierr_free:
140562306a36Sopenharmony_ci	tb_tunnel_free(tunnel);
140662306a36Sopenharmony_cierr_reclaim_usb:
140762306a36Sopenharmony_ci	tb_reclaim_usb3_bandwidth(tb, in, out);
140862306a36Sopenharmony_cierr_detach_group:
140962306a36Sopenharmony_ci	tb_detach_bandwidth_group(in);
141062306a36Sopenharmony_cierr_dealloc_dp:
141162306a36Sopenharmony_ci	tb_switch_dealloc_dp_resource(in->sw, in);
141262306a36Sopenharmony_cierr_rpm_put:
141362306a36Sopenharmony_ci	pm_runtime_mark_last_busy(&out->sw->dev);
141462306a36Sopenharmony_ci	pm_runtime_put_autosuspend(&out->sw->dev);
141562306a36Sopenharmony_ci	pm_runtime_mark_last_busy(&in->sw->dev);
141662306a36Sopenharmony_ci	pm_runtime_put_autosuspend(&in->sw->dev);
141762306a36Sopenharmony_ci}
141862306a36Sopenharmony_ci
141962306a36Sopenharmony_cistatic void tb_dp_resource_unavailable(struct tb *tb, struct tb_port *port)
142062306a36Sopenharmony_ci{
142162306a36Sopenharmony_ci	struct tb_port *in, *out;
142262306a36Sopenharmony_ci	struct tb_tunnel *tunnel;
142362306a36Sopenharmony_ci
142462306a36Sopenharmony_ci	if (tb_port_is_dpin(port)) {
142562306a36Sopenharmony_ci		tb_port_dbg(port, "DP IN resource unavailable\n");
142662306a36Sopenharmony_ci		in = port;
142762306a36Sopenharmony_ci		out = NULL;
142862306a36Sopenharmony_ci	} else {
142962306a36Sopenharmony_ci		tb_port_dbg(port, "DP OUT resource unavailable\n");
143062306a36Sopenharmony_ci		in = NULL;
143162306a36Sopenharmony_ci		out = port;
143262306a36Sopenharmony_ci	}
143362306a36Sopenharmony_ci
143462306a36Sopenharmony_ci	tunnel = tb_find_tunnel(tb, TB_TUNNEL_DP, in, out);
143562306a36Sopenharmony_ci	tb_deactivate_and_free_tunnel(tunnel);
143662306a36Sopenharmony_ci	list_del_init(&port->list);
143762306a36Sopenharmony_ci
143862306a36Sopenharmony_ci	/*
143962306a36Sopenharmony_ci	 * See if there is another DP OUT port that can be used for
144062306a36Sopenharmony_ci	 * to create another tunnel.
144162306a36Sopenharmony_ci	 */
144262306a36Sopenharmony_ci	tb_recalc_estimated_bandwidth(tb);
144362306a36Sopenharmony_ci	tb_tunnel_dp(tb);
144462306a36Sopenharmony_ci}
144562306a36Sopenharmony_ci
144662306a36Sopenharmony_cistatic void tb_dp_resource_available(struct tb *tb, struct tb_port *port)
144762306a36Sopenharmony_ci{
144862306a36Sopenharmony_ci	struct tb_cm *tcm = tb_priv(tb);
144962306a36Sopenharmony_ci	struct tb_port *p;
145062306a36Sopenharmony_ci
145162306a36Sopenharmony_ci	if (tb_port_is_enabled(port))
145262306a36Sopenharmony_ci		return;
145362306a36Sopenharmony_ci
145462306a36Sopenharmony_ci	list_for_each_entry(p, &tcm->dp_resources, list) {
145562306a36Sopenharmony_ci		if (p == port)
145662306a36Sopenharmony_ci			return;
145762306a36Sopenharmony_ci	}
145862306a36Sopenharmony_ci
145962306a36Sopenharmony_ci	tb_port_dbg(port, "DP %s resource available\n",
146062306a36Sopenharmony_ci		    tb_port_is_dpin(port) ? "IN" : "OUT");
146162306a36Sopenharmony_ci	list_add_tail(&port->list, &tcm->dp_resources);
146262306a36Sopenharmony_ci
146362306a36Sopenharmony_ci	/* Look for suitable DP IN <-> DP OUT pairs now */
146462306a36Sopenharmony_ci	tb_tunnel_dp(tb);
146562306a36Sopenharmony_ci}
146662306a36Sopenharmony_ci
146762306a36Sopenharmony_cistatic void tb_disconnect_and_release_dp(struct tb *tb)
146862306a36Sopenharmony_ci{
146962306a36Sopenharmony_ci	struct tb_cm *tcm = tb_priv(tb);
147062306a36Sopenharmony_ci	struct tb_tunnel *tunnel, *n;
147162306a36Sopenharmony_ci
147262306a36Sopenharmony_ci	/*
147362306a36Sopenharmony_ci	 * Tear down all DP tunnels and release their resources. They
147462306a36Sopenharmony_ci	 * will be re-established after resume based on plug events.
147562306a36Sopenharmony_ci	 */
147662306a36Sopenharmony_ci	list_for_each_entry_safe_reverse(tunnel, n, &tcm->tunnel_list, list) {
147762306a36Sopenharmony_ci		if (tb_tunnel_is_dp(tunnel))
147862306a36Sopenharmony_ci			tb_deactivate_and_free_tunnel(tunnel);
147962306a36Sopenharmony_ci	}
148062306a36Sopenharmony_ci
148162306a36Sopenharmony_ci	while (!list_empty(&tcm->dp_resources)) {
148262306a36Sopenharmony_ci		struct tb_port *port;
148362306a36Sopenharmony_ci
148462306a36Sopenharmony_ci		port = list_first_entry(&tcm->dp_resources,
148562306a36Sopenharmony_ci					struct tb_port, list);
148662306a36Sopenharmony_ci		list_del_init(&port->list);
148762306a36Sopenharmony_ci	}
148862306a36Sopenharmony_ci}
148962306a36Sopenharmony_ci
149062306a36Sopenharmony_cistatic int tb_disconnect_pci(struct tb *tb, struct tb_switch *sw)
149162306a36Sopenharmony_ci{
149262306a36Sopenharmony_ci	struct tb_tunnel *tunnel;
149362306a36Sopenharmony_ci	struct tb_port *up;
149462306a36Sopenharmony_ci
149562306a36Sopenharmony_ci	up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP);
149662306a36Sopenharmony_ci	if (WARN_ON(!up))
149762306a36Sopenharmony_ci		return -ENODEV;
149862306a36Sopenharmony_ci
149962306a36Sopenharmony_ci	tunnel = tb_find_tunnel(tb, TB_TUNNEL_PCI, NULL, up);
150062306a36Sopenharmony_ci	if (WARN_ON(!tunnel))
150162306a36Sopenharmony_ci		return -ENODEV;
150262306a36Sopenharmony_ci
150362306a36Sopenharmony_ci	tb_switch_xhci_disconnect(sw);
150462306a36Sopenharmony_ci
150562306a36Sopenharmony_ci	tb_tunnel_deactivate(tunnel);
150662306a36Sopenharmony_ci	list_del(&tunnel->list);
150762306a36Sopenharmony_ci	tb_tunnel_free(tunnel);
150862306a36Sopenharmony_ci	return 0;
150962306a36Sopenharmony_ci}
151062306a36Sopenharmony_ci
151162306a36Sopenharmony_cistatic int tb_tunnel_pci(struct tb *tb, struct tb_switch *sw)
151262306a36Sopenharmony_ci{
151362306a36Sopenharmony_ci	struct tb_port *up, *down, *port;
151462306a36Sopenharmony_ci	struct tb_cm *tcm = tb_priv(tb);
151562306a36Sopenharmony_ci	struct tb_tunnel *tunnel;
151662306a36Sopenharmony_ci
151762306a36Sopenharmony_ci	up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP);
151862306a36Sopenharmony_ci	if (!up)
151962306a36Sopenharmony_ci		return 0;
152062306a36Sopenharmony_ci
152162306a36Sopenharmony_ci	/*
152262306a36Sopenharmony_ci	 * Look up available down port. Since we are chaining it should
152362306a36Sopenharmony_ci	 * be found right above this switch.
152462306a36Sopenharmony_ci	 */
152562306a36Sopenharmony_ci	port = tb_switch_downstream_port(sw);
152662306a36Sopenharmony_ci	down = tb_find_pcie_down(tb_switch_parent(sw), port);
152762306a36Sopenharmony_ci	if (!down)
152862306a36Sopenharmony_ci		return 0;
152962306a36Sopenharmony_ci
153062306a36Sopenharmony_ci	tunnel = tb_tunnel_alloc_pci(tb, up, down);
153162306a36Sopenharmony_ci	if (!tunnel)
153262306a36Sopenharmony_ci		return -ENOMEM;
153362306a36Sopenharmony_ci
153462306a36Sopenharmony_ci	if (tb_tunnel_activate(tunnel)) {
153562306a36Sopenharmony_ci		tb_port_info(up,
153662306a36Sopenharmony_ci			     "PCIe tunnel activation failed, aborting\n");
153762306a36Sopenharmony_ci		tb_tunnel_free(tunnel);
153862306a36Sopenharmony_ci		return -EIO;
153962306a36Sopenharmony_ci	}
154062306a36Sopenharmony_ci
154162306a36Sopenharmony_ci	/*
154262306a36Sopenharmony_ci	 * PCIe L1 is needed to enable CL0s for Titan Ridge so enable it
154362306a36Sopenharmony_ci	 * here.
154462306a36Sopenharmony_ci	 */
154562306a36Sopenharmony_ci	if (tb_switch_pcie_l1_enable(sw))
154662306a36Sopenharmony_ci		tb_sw_warn(sw, "failed to enable PCIe L1 for Titan Ridge\n");
154762306a36Sopenharmony_ci
154862306a36Sopenharmony_ci	if (tb_switch_xhci_connect(sw))
154962306a36Sopenharmony_ci		tb_sw_warn(sw, "failed to connect xHCI\n");
155062306a36Sopenharmony_ci
155162306a36Sopenharmony_ci	list_add_tail(&tunnel->list, &tcm->tunnel_list);
155262306a36Sopenharmony_ci	return 0;
155362306a36Sopenharmony_ci}
155462306a36Sopenharmony_ci
155562306a36Sopenharmony_cistatic int tb_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
155662306a36Sopenharmony_ci				    int transmit_path, int transmit_ring,
155762306a36Sopenharmony_ci				    int receive_path, int receive_ring)
155862306a36Sopenharmony_ci{
155962306a36Sopenharmony_ci	struct tb_cm *tcm = tb_priv(tb);
156062306a36Sopenharmony_ci	struct tb_port *nhi_port, *dst_port;
156162306a36Sopenharmony_ci	struct tb_tunnel *tunnel;
156262306a36Sopenharmony_ci	struct tb_switch *sw;
156362306a36Sopenharmony_ci	int ret;
156462306a36Sopenharmony_ci
156562306a36Sopenharmony_ci	sw = tb_to_switch(xd->dev.parent);
156662306a36Sopenharmony_ci	dst_port = tb_port_at(xd->route, sw);
156762306a36Sopenharmony_ci	nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI);
156862306a36Sopenharmony_ci
156962306a36Sopenharmony_ci	mutex_lock(&tb->lock);
157062306a36Sopenharmony_ci
157162306a36Sopenharmony_ci	/*
157262306a36Sopenharmony_ci	 * When tunneling DMA paths the link should not enter CL states
157362306a36Sopenharmony_ci	 * so disable them now.
157462306a36Sopenharmony_ci	 */
157562306a36Sopenharmony_ci	tb_disable_clx(sw);
157662306a36Sopenharmony_ci
157762306a36Sopenharmony_ci	tunnel = tb_tunnel_alloc_dma(tb, nhi_port, dst_port, transmit_path,
157862306a36Sopenharmony_ci				     transmit_ring, receive_path, receive_ring);
157962306a36Sopenharmony_ci	if (!tunnel) {
158062306a36Sopenharmony_ci		ret = -ENOMEM;
158162306a36Sopenharmony_ci		goto err_clx;
158262306a36Sopenharmony_ci	}
158362306a36Sopenharmony_ci
158462306a36Sopenharmony_ci	if (tb_tunnel_activate(tunnel)) {
158562306a36Sopenharmony_ci		tb_port_info(nhi_port,
158662306a36Sopenharmony_ci			     "DMA tunnel activation failed, aborting\n");
158762306a36Sopenharmony_ci		ret = -EIO;
158862306a36Sopenharmony_ci		goto err_free;
158962306a36Sopenharmony_ci	}
159062306a36Sopenharmony_ci
159162306a36Sopenharmony_ci	list_add_tail(&tunnel->list, &tcm->tunnel_list);
159262306a36Sopenharmony_ci	mutex_unlock(&tb->lock);
159362306a36Sopenharmony_ci	return 0;
159462306a36Sopenharmony_ci
159562306a36Sopenharmony_cierr_free:
159662306a36Sopenharmony_ci	tb_tunnel_free(tunnel);
159762306a36Sopenharmony_cierr_clx:
159862306a36Sopenharmony_ci	tb_enable_clx(sw);
159962306a36Sopenharmony_ci	mutex_unlock(&tb->lock);
160062306a36Sopenharmony_ci
160162306a36Sopenharmony_ci	return ret;
160262306a36Sopenharmony_ci}
160362306a36Sopenharmony_ci
160462306a36Sopenharmony_cistatic void __tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
160562306a36Sopenharmony_ci					  int transmit_path, int transmit_ring,
160662306a36Sopenharmony_ci					  int receive_path, int receive_ring)
160762306a36Sopenharmony_ci{
160862306a36Sopenharmony_ci	struct tb_cm *tcm = tb_priv(tb);
160962306a36Sopenharmony_ci	struct tb_port *nhi_port, *dst_port;
161062306a36Sopenharmony_ci	struct tb_tunnel *tunnel, *n;
161162306a36Sopenharmony_ci	struct tb_switch *sw;
161262306a36Sopenharmony_ci
161362306a36Sopenharmony_ci	sw = tb_to_switch(xd->dev.parent);
161462306a36Sopenharmony_ci	dst_port = tb_port_at(xd->route, sw);
161562306a36Sopenharmony_ci	nhi_port = tb_switch_find_port(tb->root_switch, TB_TYPE_NHI);
161662306a36Sopenharmony_ci
161762306a36Sopenharmony_ci	list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
161862306a36Sopenharmony_ci		if (!tb_tunnel_is_dma(tunnel))
161962306a36Sopenharmony_ci			continue;
162062306a36Sopenharmony_ci		if (tunnel->src_port != nhi_port || tunnel->dst_port != dst_port)
162162306a36Sopenharmony_ci			continue;
162262306a36Sopenharmony_ci
162362306a36Sopenharmony_ci		if (tb_tunnel_match_dma(tunnel, transmit_path, transmit_ring,
162462306a36Sopenharmony_ci					receive_path, receive_ring))
162562306a36Sopenharmony_ci			tb_deactivate_and_free_tunnel(tunnel);
162662306a36Sopenharmony_ci	}
162762306a36Sopenharmony_ci
162862306a36Sopenharmony_ci	/*
162962306a36Sopenharmony_ci	 * Try to re-enable CL states now, it is OK if this fails
163062306a36Sopenharmony_ci	 * because we may still have another DMA tunnel active through
163162306a36Sopenharmony_ci	 * the same host router USB4 downstream port.
163262306a36Sopenharmony_ci	 */
163362306a36Sopenharmony_ci	tb_enable_clx(sw);
163462306a36Sopenharmony_ci}
163562306a36Sopenharmony_ci
163662306a36Sopenharmony_cistatic int tb_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
163762306a36Sopenharmony_ci				       int transmit_path, int transmit_ring,
163862306a36Sopenharmony_ci				       int receive_path, int receive_ring)
163962306a36Sopenharmony_ci{
164062306a36Sopenharmony_ci	if (!xd->is_unplugged) {
164162306a36Sopenharmony_ci		mutex_lock(&tb->lock);
164262306a36Sopenharmony_ci		__tb_disconnect_xdomain_paths(tb, xd, transmit_path,
164362306a36Sopenharmony_ci					      transmit_ring, receive_path,
164462306a36Sopenharmony_ci					      receive_ring);
164562306a36Sopenharmony_ci		mutex_unlock(&tb->lock);
164662306a36Sopenharmony_ci	}
164762306a36Sopenharmony_ci	return 0;
164862306a36Sopenharmony_ci}
164962306a36Sopenharmony_ci
165062306a36Sopenharmony_ci/* hotplug handling */
165162306a36Sopenharmony_ci
165262306a36Sopenharmony_ci/*
165362306a36Sopenharmony_ci * tb_handle_hotplug() - handle hotplug event
165462306a36Sopenharmony_ci *
165562306a36Sopenharmony_ci * Executes on tb->wq.
165662306a36Sopenharmony_ci */
165762306a36Sopenharmony_cistatic void tb_handle_hotplug(struct work_struct *work)
165862306a36Sopenharmony_ci{
165962306a36Sopenharmony_ci	struct tb_hotplug_event *ev = container_of(work, typeof(*ev), work);
166062306a36Sopenharmony_ci	struct tb *tb = ev->tb;
166162306a36Sopenharmony_ci	struct tb_cm *tcm = tb_priv(tb);
166262306a36Sopenharmony_ci	struct tb_switch *sw;
166362306a36Sopenharmony_ci	struct tb_port *port;
166462306a36Sopenharmony_ci
166562306a36Sopenharmony_ci	/* Bring the domain back from sleep if it was suspended */
166662306a36Sopenharmony_ci	pm_runtime_get_sync(&tb->dev);
166762306a36Sopenharmony_ci
166862306a36Sopenharmony_ci	mutex_lock(&tb->lock);
166962306a36Sopenharmony_ci	if (!tcm->hotplug_active)
167062306a36Sopenharmony_ci		goto out; /* during init, suspend or shutdown */
167162306a36Sopenharmony_ci
167262306a36Sopenharmony_ci	sw = tb_switch_find_by_route(tb, ev->route);
167362306a36Sopenharmony_ci	if (!sw) {
167462306a36Sopenharmony_ci		tb_warn(tb,
167562306a36Sopenharmony_ci			"hotplug event from non existent switch %llx:%x (unplug: %d)\n",
167662306a36Sopenharmony_ci			ev->route, ev->port, ev->unplug);
167762306a36Sopenharmony_ci		goto out;
167862306a36Sopenharmony_ci	}
167962306a36Sopenharmony_ci	if (ev->port > sw->config.max_port_number) {
168062306a36Sopenharmony_ci		tb_warn(tb,
168162306a36Sopenharmony_ci			"hotplug event from non existent port %llx:%x (unplug: %d)\n",
168262306a36Sopenharmony_ci			ev->route, ev->port, ev->unplug);
168362306a36Sopenharmony_ci		goto put_sw;
168462306a36Sopenharmony_ci	}
168562306a36Sopenharmony_ci	port = &sw->ports[ev->port];
168662306a36Sopenharmony_ci	if (tb_is_upstream_port(port)) {
168762306a36Sopenharmony_ci		tb_dbg(tb, "hotplug event for upstream port %llx:%x (unplug: %d)\n",
168862306a36Sopenharmony_ci		       ev->route, ev->port, ev->unplug);
168962306a36Sopenharmony_ci		goto put_sw;
169062306a36Sopenharmony_ci	}
169162306a36Sopenharmony_ci
169262306a36Sopenharmony_ci	pm_runtime_get_sync(&sw->dev);
169362306a36Sopenharmony_ci
169462306a36Sopenharmony_ci	if (ev->unplug) {
169562306a36Sopenharmony_ci		tb_retimer_remove_all(port);
169662306a36Sopenharmony_ci
169762306a36Sopenharmony_ci		if (tb_port_has_remote(port)) {
169862306a36Sopenharmony_ci			tb_port_dbg(port, "switch unplugged\n");
169962306a36Sopenharmony_ci			tb_sw_set_unplugged(port->remote->sw);
170062306a36Sopenharmony_ci			tb_free_invalid_tunnels(tb);
170162306a36Sopenharmony_ci			tb_remove_dp_resources(port->remote->sw);
170262306a36Sopenharmony_ci			tb_switch_tmu_disable(port->remote->sw);
170362306a36Sopenharmony_ci			tb_switch_unconfigure_link(port->remote->sw);
170462306a36Sopenharmony_ci			tb_switch_lane_bonding_disable(port->remote->sw);
170562306a36Sopenharmony_ci			tb_switch_remove(port->remote->sw);
170662306a36Sopenharmony_ci			port->remote = NULL;
170762306a36Sopenharmony_ci			if (port->dual_link_port)
170862306a36Sopenharmony_ci				port->dual_link_port->remote = NULL;
170962306a36Sopenharmony_ci			/* Maybe we can create another DP tunnel */
171062306a36Sopenharmony_ci			tb_recalc_estimated_bandwidth(tb);
171162306a36Sopenharmony_ci			tb_tunnel_dp(tb);
171262306a36Sopenharmony_ci		} else if (port->xdomain) {
171362306a36Sopenharmony_ci			struct tb_xdomain *xd = tb_xdomain_get(port->xdomain);
171462306a36Sopenharmony_ci
171562306a36Sopenharmony_ci			tb_port_dbg(port, "xdomain unplugged\n");
171662306a36Sopenharmony_ci			/*
171762306a36Sopenharmony_ci			 * Service drivers are unbound during
171862306a36Sopenharmony_ci			 * tb_xdomain_remove() so setting XDomain as
171962306a36Sopenharmony_ci			 * unplugged here prevents deadlock if they call
172062306a36Sopenharmony_ci			 * tb_xdomain_disable_paths(). We will tear down
172162306a36Sopenharmony_ci			 * all the tunnels below.
172262306a36Sopenharmony_ci			 */
172362306a36Sopenharmony_ci			xd->is_unplugged = true;
172462306a36Sopenharmony_ci			tb_xdomain_remove(xd);
172562306a36Sopenharmony_ci			port->xdomain = NULL;
172662306a36Sopenharmony_ci			__tb_disconnect_xdomain_paths(tb, xd, -1, -1, -1, -1);
172762306a36Sopenharmony_ci			tb_xdomain_put(xd);
172862306a36Sopenharmony_ci			tb_port_unconfigure_xdomain(port);
172962306a36Sopenharmony_ci		} else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) {
173062306a36Sopenharmony_ci			tb_dp_resource_unavailable(tb, port);
173162306a36Sopenharmony_ci		} else if (!port->port) {
173262306a36Sopenharmony_ci			tb_sw_dbg(sw, "xHCI disconnect request\n");
173362306a36Sopenharmony_ci			tb_switch_xhci_disconnect(sw);
173462306a36Sopenharmony_ci		} else {
173562306a36Sopenharmony_ci			tb_port_dbg(port,
173662306a36Sopenharmony_ci				   "got unplug event for disconnected port, ignoring\n");
173762306a36Sopenharmony_ci		}
173862306a36Sopenharmony_ci	} else if (port->remote) {
173962306a36Sopenharmony_ci		tb_port_dbg(port, "got plug event for connected port, ignoring\n");
174062306a36Sopenharmony_ci	} else if (!port->port && sw->authorized) {
174162306a36Sopenharmony_ci		tb_sw_dbg(sw, "xHCI connect request\n");
174262306a36Sopenharmony_ci		tb_switch_xhci_connect(sw);
174362306a36Sopenharmony_ci	} else {
174462306a36Sopenharmony_ci		if (tb_port_is_null(port)) {
174562306a36Sopenharmony_ci			tb_port_dbg(port, "hotplug: scanning\n");
174662306a36Sopenharmony_ci			tb_scan_port(port);
174762306a36Sopenharmony_ci			if (!port->remote)
174862306a36Sopenharmony_ci				tb_port_dbg(port, "hotplug: no switch found\n");
174962306a36Sopenharmony_ci		} else if (tb_port_is_dpout(port) || tb_port_is_dpin(port)) {
175062306a36Sopenharmony_ci			tb_dp_resource_available(tb, port);
175162306a36Sopenharmony_ci		}
175262306a36Sopenharmony_ci	}
175362306a36Sopenharmony_ci
175462306a36Sopenharmony_ci	pm_runtime_mark_last_busy(&sw->dev);
175562306a36Sopenharmony_ci	pm_runtime_put_autosuspend(&sw->dev);
175662306a36Sopenharmony_ci
175762306a36Sopenharmony_ciput_sw:
175862306a36Sopenharmony_ci	tb_switch_put(sw);
175962306a36Sopenharmony_ciout:
176062306a36Sopenharmony_ci	mutex_unlock(&tb->lock);
176162306a36Sopenharmony_ci
176262306a36Sopenharmony_ci	pm_runtime_mark_last_busy(&tb->dev);
176362306a36Sopenharmony_ci	pm_runtime_put_autosuspend(&tb->dev);
176462306a36Sopenharmony_ci
176562306a36Sopenharmony_ci	kfree(ev);
176662306a36Sopenharmony_ci}
176762306a36Sopenharmony_ci
176862306a36Sopenharmony_cistatic int tb_alloc_dp_bandwidth(struct tb_tunnel *tunnel, int *requested_up,
176962306a36Sopenharmony_ci				 int *requested_down)
177062306a36Sopenharmony_ci{
177162306a36Sopenharmony_ci	int allocated_up, allocated_down, available_up, available_down, ret;
177262306a36Sopenharmony_ci	int requested_up_corrected, requested_down_corrected, granularity;
177362306a36Sopenharmony_ci	int max_up, max_down, max_up_rounded, max_down_rounded;
177462306a36Sopenharmony_ci	struct tb *tb = tunnel->tb;
177562306a36Sopenharmony_ci	struct tb_port *in, *out;
177662306a36Sopenharmony_ci
177762306a36Sopenharmony_ci	ret = tb_tunnel_allocated_bandwidth(tunnel, &allocated_up, &allocated_down);
177862306a36Sopenharmony_ci	if (ret)
177962306a36Sopenharmony_ci		return ret;
178062306a36Sopenharmony_ci
178162306a36Sopenharmony_ci	in = tunnel->src_port;
178262306a36Sopenharmony_ci	out = tunnel->dst_port;
178362306a36Sopenharmony_ci
178462306a36Sopenharmony_ci	tb_port_dbg(in, "bandwidth allocated currently %d/%d Mb/s\n",
178562306a36Sopenharmony_ci		    allocated_up, allocated_down);
178662306a36Sopenharmony_ci
178762306a36Sopenharmony_ci	/*
178862306a36Sopenharmony_ci	 * If we get rounded up request from graphics side, say HBR2 x 4
178962306a36Sopenharmony_ci	 * that is 17500 instead of 17280 (this is because of the
179062306a36Sopenharmony_ci	 * granularity), we allow it too. Here the graphics has already
179162306a36Sopenharmony_ci	 * negotiated with the DPRX the maximum possible rates (which is
179262306a36Sopenharmony_ci	 * 17280 in this case).
179362306a36Sopenharmony_ci	 *
179462306a36Sopenharmony_ci	 * Since the link cannot go higher than 17280 we use that in our
179562306a36Sopenharmony_ci	 * calculations but the DP IN adapter Allocated BW write must be
179662306a36Sopenharmony_ci	 * the same value (17500) otherwise the adapter will mark it as
179762306a36Sopenharmony_ci	 * failed for graphics.
179862306a36Sopenharmony_ci	 */
179962306a36Sopenharmony_ci	ret = tb_tunnel_maximum_bandwidth(tunnel, &max_up, &max_down);
180062306a36Sopenharmony_ci	if (ret)
180162306a36Sopenharmony_ci		return ret;
180262306a36Sopenharmony_ci
180362306a36Sopenharmony_ci	ret = usb4_dp_port_granularity(in);
180462306a36Sopenharmony_ci	if (ret < 0)
180562306a36Sopenharmony_ci		return ret;
180662306a36Sopenharmony_ci	granularity = ret;
180762306a36Sopenharmony_ci
180862306a36Sopenharmony_ci	max_up_rounded = roundup(max_up, granularity);
180962306a36Sopenharmony_ci	max_down_rounded = roundup(max_down, granularity);
181062306a36Sopenharmony_ci
181162306a36Sopenharmony_ci	/*
181262306a36Sopenharmony_ci	 * This will "fix" the request down to the maximum supported
181362306a36Sopenharmony_ci	 * rate * lanes if it is at the maximum rounded up level.
181462306a36Sopenharmony_ci	 */
181562306a36Sopenharmony_ci	requested_up_corrected = *requested_up;
181662306a36Sopenharmony_ci	if (requested_up_corrected == max_up_rounded)
181762306a36Sopenharmony_ci		requested_up_corrected = max_up;
181862306a36Sopenharmony_ci	else if (requested_up_corrected < 0)
181962306a36Sopenharmony_ci		requested_up_corrected = 0;
182062306a36Sopenharmony_ci	requested_down_corrected = *requested_down;
182162306a36Sopenharmony_ci	if (requested_down_corrected == max_down_rounded)
182262306a36Sopenharmony_ci		requested_down_corrected = max_down;
182362306a36Sopenharmony_ci	else if (requested_down_corrected < 0)
182462306a36Sopenharmony_ci		requested_down_corrected = 0;
182562306a36Sopenharmony_ci
182662306a36Sopenharmony_ci	tb_port_dbg(in, "corrected bandwidth request %d/%d Mb/s\n",
182762306a36Sopenharmony_ci		    requested_up_corrected, requested_down_corrected);
182862306a36Sopenharmony_ci
182962306a36Sopenharmony_ci	if ((*requested_up >= 0 && requested_up_corrected > max_up_rounded) ||
183062306a36Sopenharmony_ci	    (*requested_down >= 0 && requested_down_corrected > max_down_rounded)) {
183162306a36Sopenharmony_ci		tb_port_dbg(in, "bandwidth request too high (%d/%d Mb/s > %d/%d Mb/s)\n",
183262306a36Sopenharmony_ci			    requested_up_corrected, requested_down_corrected,
183362306a36Sopenharmony_ci			    max_up_rounded, max_down_rounded);
183462306a36Sopenharmony_ci		return -ENOBUFS;
183562306a36Sopenharmony_ci	}
183662306a36Sopenharmony_ci
183762306a36Sopenharmony_ci	if ((*requested_up >= 0 && requested_up_corrected <= allocated_up) ||
183862306a36Sopenharmony_ci	    (*requested_down >= 0 && requested_down_corrected <= allocated_down)) {
183962306a36Sopenharmony_ci		/*
184062306a36Sopenharmony_ci		 * If requested bandwidth is less or equal than what is
184162306a36Sopenharmony_ci		 * currently allocated to that tunnel we simply change
184262306a36Sopenharmony_ci		 * the reservation of the tunnel. Since all the tunnels
184362306a36Sopenharmony_ci		 * going out from the same USB4 port are in the same
184462306a36Sopenharmony_ci		 * group the released bandwidth will be taken into
184562306a36Sopenharmony_ci		 * account for the other tunnels automatically below.
184662306a36Sopenharmony_ci		 */
184762306a36Sopenharmony_ci		return tb_tunnel_alloc_bandwidth(tunnel, requested_up,
184862306a36Sopenharmony_ci						 requested_down);
184962306a36Sopenharmony_ci	}
185062306a36Sopenharmony_ci
185162306a36Sopenharmony_ci	/*
185262306a36Sopenharmony_ci	 * More bandwidth is requested. Release all the potential
185362306a36Sopenharmony_ci	 * bandwidth from USB3 first.
185462306a36Sopenharmony_ci	 */
185562306a36Sopenharmony_ci	ret = tb_release_unused_usb3_bandwidth(tb, in, out);
185662306a36Sopenharmony_ci	if (ret)
185762306a36Sopenharmony_ci		return ret;
185862306a36Sopenharmony_ci
185962306a36Sopenharmony_ci	/*
186062306a36Sopenharmony_ci	 * Then go over all tunnels that cross the same USB4 ports (they
186162306a36Sopenharmony_ci	 * are also in the same group but we use the same function here
186262306a36Sopenharmony_ci	 * that we use with the normal bandwidth allocation).
186362306a36Sopenharmony_ci	 */
186462306a36Sopenharmony_ci	ret = tb_available_bandwidth(tb, in, out, &available_up, &available_down);
186562306a36Sopenharmony_ci	if (ret)
186662306a36Sopenharmony_ci		goto reclaim;
186762306a36Sopenharmony_ci
186862306a36Sopenharmony_ci	tb_port_dbg(in, "bandwidth available for allocation %d/%d Mb/s\n",
186962306a36Sopenharmony_ci		    available_up, available_down);
187062306a36Sopenharmony_ci
187162306a36Sopenharmony_ci	if ((*requested_up >= 0 && available_up >= requested_up_corrected) ||
187262306a36Sopenharmony_ci	    (*requested_down >= 0 && available_down >= requested_down_corrected)) {
187362306a36Sopenharmony_ci		ret = tb_tunnel_alloc_bandwidth(tunnel, requested_up,
187462306a36Sopenharmony_ci						requested_down);
187562306a36Sopenharmony_ci	} else {
187662306a36Sopenharmony_ci		ret = -ENOBUFS;
187762306a36Sopenharmony_ci	}
187862306a36Sopenharmony_ci
187962306a36Sopenharmony_cireclaim:
188062306a36Sopenharmony_ci	tb_reclaim_usb3_bandwidth(tb, in, out);
188162306a36Sopenharmony_ci	return ret;
188262306a36Sopenharmony_ci}
188362306a36Sopenharmony_ci
188462306a36Sopenharmony_cistatic void tb_handle_dp_bandwidth_request(struct work_struct *work)
188562306a36Sopenharmony_ci{
188662306a36Sopenharmony_ci	struct tb_hotplug_event *ev = container_of(work, typeof(*ev), work);
188762306a36Sopenharmony_ci	int requested_bw, requested_up, requested_down, ret;
188862306a36Sopenharmony_ci	struct tb_port *in, *out;
188962306a36Sopenharmony_ci	struct tb_tunnel *tunnel;
189062306a36Sopenharmony_ci	struct tb *tb = ev->tb;
189162306a36Sopenharmony_ci	struct tb_cm *tcm = tb_priv(tb);
189262306a36Sopenharmony_ci	struct tb_switch *sw;
189362306a36Sopenharmony_ci
189462306a36Sopenharmony_ci	pm_runtime_get_sync(&tb->dev);
189562306a36Sopenharmony_ci
189662306a36Sopenharmony_ci	mutex_lock(&tb->lock);
189762306a36Sopenharmony_ci	if (!tcm->hotplug_active)
189862306a36Sopenharmony_ci		goto unlock;
189962306a36Sopenharmony_ci
190062306a36Sopenharmony_ci	sw = tb_switch_find_by_route(tb, ev->route);
190162306a36Sopenharmony_ci	if (!sw) {
190262306a36Sopenharmony_ci		tb_warn(tb, "bandwidth request from non-existent router %llx\n",
190362306a36Sopenharmony_ci			ev->route);
190462306a36Sopenharmony_ci		goto unlock;
190562306a36Sopenharmony_ci	}
190662306a36Sopenharmony_ci
190762306a36Sopenharmony_ci	in = &sw->ports[ev->port];
190862306a36Sopenharmony_ci	if (!tb_port_is_dpin(in)) {
190962306a36Sopenharmony_ci		tb_port_warn(in, "bandwidth request to non-DP IN adapter\n");
191062306a36Sopenharmony_ci		goto put_sw;
191162306a36Sopenharmony_ci	}
191262306a36Sopenharmony_ci
191362306a36Sopenharmony_ci	tb_port_dbg(in, "handling bandwidth allocation request\n");
191462306a36Sopenharmony_ci
191562306a36Sopenharmony_ci	if (!usb4_dp_port_bandwidth_mode_enabled(in)) {
191662306a36Sopenharmony_ci		tb_port_warn(in, "bandwidth allocation mode not enabled\n");
191762306a36Sopenharmony_ci		goto put_sw;
191862306a36Sopenharmony_ci	}
191962306a36Sopenharmony_ci
192062306a36Sopenharmony_ci	ret = usb4_dp_port_requested_bandwidth(in);
192162306a36Sopenharmony_ci	if (ret < 0) {
192262306a36Sopenharmony_ci		if (ret == -ENODATA)
192362306a36Sopenharmony_ci			tb_port_dbg(in, "no bandwidth request active\n");
192462306a36Sopenharmony_ci		else
192562306a36Sopenharmony_ci			tb_port_warn(in, "failed to read requested bandwidth\n");
192662306a36Sopenharmony_ci		goto put_sw;
192762306a36Sopenharmony_ci	}
192862306a36Sopenharmony_ci	requested_bw = ret;
192962306a36Sopenharmony_ci
193062306a36Sopenharmony_ci	tb_port_dbg(in, "requested bandwidth %d Mb/s\n", requested_bw);
193162306a36Sopenharmony_ci
193262306a36Sopenharmony_ci	tunnel = tb_find_tunnel(tb, TB_TUNNEL_DP, in, NULL);
193362306a36Sopenharmony_ci	if (!tunnel) {
193462306a36Sopenharmony_ci		tb_port_warn(in, "failed to find tunnel\n");
193562306a36Sopenharmony_ci		goto put_sw;
193662306a36Sopenharmony_ci	}
193762306a36Sopenharmony_ci
193862306a36Sopenharmony_ci	out = tunnel->dst_port;
193962306a36Sopenharmony_ci
194062306a36Sopenharmony_ci	if (in->sw->config.depth < out->sw->config.depth) {
194162306a36Sopenharmony_ci		requested_up = -1;
194262306a36Sopenharmony_ci		requested_down = requested_bw;
194362306a36Sopenharmony_ci	} else {
194462306a36Sopenharmony_ci		requested_up = requested_bw;
194562306a36Sopenharmony_ci		requested_down = -1;
194662306a36Sopenharmony_ci	}
194762306a36Sopenharmony_ci
194862306a36Sopenharmony_ci	ret = tb_alloc_dp_bandwidth(tunnel, &requested_up, &requested_down);
194962306a36Sopenharmony_ci	if (ret) {
195062306a36Sopenharmony_ci		if (ret == -ENOBUFS)
195162306a36Sopenharmony_ci			tb_port_warn(in, "not enough bandwidth available\n");
195262306a36Sopenharmony_ci		else
195362306a36Sopenharmony_ci			tb_port_warn(in, "failed to change bandwidth allocation\n");
195462306a36Sopenharmony_ci	} else {
195562306a36Sopenharmony_ci		tb_port_dbg(in, "bandwidth allocation changed to %d/%d Mb/s\n",
195662306a36Sopenharmony_ci			    requested_up, requested_down);
195762306a36Sopenharmony_ci
195862306a36Sopenharmony_ci		/* Update other clients about the allocation change */
195962306a36Sopenharmony_ci		tb_recalc_estimated_bandwidth(tb);
196062306a36Sopenharmony_ci	}
196162306a36Sopenharmony_ci
196262306a36Sopenharmony_ciput_sw:
196362306a36Sopenharmony_ci	tb_switch_put(sw);
196462306a36Sopenharmony_ciunlock:
196562306a36Sopenharmony_ci	mutex_unlock(&tb->lock);
196662306a36Sopenharmony_ci
196762306a36Sopenharmony_ci	pm_runtime_mark_last_busy(&tb->dev);
196862306a36Sopenharmony_ci	pm_runtime_put_autosuspend(&tb->dev);
196962306a36Sopenharmony_ci
197062306a36Sopenharmony_ci	kfree(ev);
197162306a36Sopenharmony_ci}
197262306a36Sopenharmony_ci
197362306a36Sopenharmony_cistatic void tb_queue_dp_bandwidth_request(struct tb *tb, u64 route, u8 port)
197462306a36Sopenharmony_ci{
197562306a36Sopenharmony_ci	struct tb_hotplug_event *ev;
197662306a36Sopenharmony_ci
197762306a36Sopenharmony_ci	ev = kmalloc(sizeof(*ev), GFP_KERNEL);
197862306a36Sopenharmony_ci	if (!ev)
197962306a36Sopenharmony_ci		return;
198062306a36Sopenharmony_ci
198162306a36Sopenharmony_ci	ev->tb = tb;
198262306a36Sopenharmony_ci	ev->route = route;
198362306a36Sopenharmony_ci	ev->port = port;
198462306a36Sopenharmony_ci	INIT_WORK(&ev->work, tb_handle_dp_bandwidth_request);
198562306a36Sopenharmony_ci	queue_work(tb->wq, &ev->work);
198662306a36Sopenharmony_ci}
198762306a36Sopenharmony_ci
198862306a36Sopenharmony_cistatic void tb_handle_notification(struct tb *tb, u64 route,
198962306a36Sopenharmony_ci				   const struct cfg_error_pkg *error)
199062306a36Sopenharmony_ci{
199162306a36Sopenharmony_ci
199262306a36Sopenharmony_ci	switch (error->error) {
199362306a36Sopenharmony_ci	case TB_CFG_ERROR_PCIE_WAKE:
199462306a36Sopenharmony_ci	case TB_CFG_ERROR_DP_CON_CHANGE:
199562306a36Sopenharmony_ci	case TB_CFG_ERROR_DPTX_DISCOVERY:
199662306a36Sopenharmony_ci		if (tb_cfg_ack_notification(tb->ctl, route, error))
199762306a36Sopenharmony_ci			tb_warn(tb, "could not ack notification on %llx\n",
199862306a36Sopenharmony_ci				route);
199962306a36Sopenharmony_ci		break;
200062306a36Sopenharmony_ci
200162306a36Sopenharmony_ci	case TB_CFG_ERROR_DP_BW:
200262306a36Sopenharmony_ci		if (tb_cfg_ack_notification(tb->ctl, route, error))
200362306a36Sopenharmony_ci			tb_warn(tb, "could not ack notification on %llx\n",
200462306a36Sopenharmony_ci				route);
200562306a36Sopenharmony_ci		tb_queue_dp_bandwidth_request(tb, route, error->port);
200662306a36Sopenharmony_ci		break;
200762306a36Sopenharmony_ci
200862306a36Sopenharmony_ci	default:
200962306a36Sopenharmony_ci		/* Ignore for now */
201062306a36Sopenharmony_ci		break;
201162306a36Sopenharmony_ci	}
201262306a36Sopenharmony_ci}
201362306a36Sopenharmony_ci
201462306a36Sopenharmony_ci/*
201562306a36Sopenharmony_ci * tb_schedule_hotplug_handler() - callback function for the control channel
201662306a36Sopenharmony_ci *
201762306a36Sopenharmony_ci * Delegates to tb_handle_hotplug.
201862306a36Sopenharmony_ci */
201962306a36Sopenharmony_cistatic void tb_handle_event(struct tb *tb, enum tb_cfg_pkg_type type,
202062306a36Sopenharmony_ci			    const void *buf, size_t size)
202162306a36Sopenharmony_ci{
202262306a36Sopenharmony_ci	const struct cfg_event_pkg *pkg = buf;
202362306a36Sopenharmony_ci	u64 route = tb_cfg_get_route(&pkg->header);
202462306a36Sopenharmony_ci
202562306a36Sopenharmony_ci	switch (type) {
202662306a36Sopenharmony_ci	case TB_CFG_PKG_ERROR:
202762306a36Sopenharmony_ci		tb_handle_notification(tb, route, (const struct cfg_error_pkg *)buf);
202862306a36Sopenharmony_ci		return;
202962306a36Sopenharmony_ci	case TB_CFG_PKG_EVENT:
203062306a36Sopenharmony_ci		break;
203162306a36Sopenharmony_ci	default:
203262306a36Sopenharmony_ci		tb_warn(tb, "unexpected event %#x, ignoring\n", type);
203362306a36Sopenharmony_ci		return;
203462306a36Sopenharmony_ci	}
203562306a36Sopenharmony_ci
203662306a36Sopenharmony_ci	if (tb_cfg_ack_plug(tb->ctl, route, pkg->port, pkg->unplug)) {
203762306a36Sopenharmony_ci		tb_warn(tb, "could not ack plug event on %llx:%x\n", route,
203862306a36Sopenharmony_ci			pkg->port);
203962306a36Sopenharmony_ci	}
204062306a36Sopenharmony_ci
204162306a36Sopenharmony_ci	tb_queue_hotplug(tb, route, pkg->port, pkg->unplug);
204262306a36Sopenharmony_ci}
204362306a36Sopenharmony_ci
204462306a36Sopenharmony_cistatic void tb_stop(struct tb *tb)
204562306a36Sopenharmony_ci{
204662306a36Sopenharmony_ci	struct tb_cm *tcm = tb_priv(tb);
204762306a36Sopenharmony_ci	struct tb_tunnel *tunnel;
204862306a36Sopenharmony_ci	struct tb_tunnel *n;
204962306a36Sopenharmony_ci
205062306a36Sopenharmony_ci	cancel_delayed_work(&tcm->remove_work);
205162306a36Sopenharmony_ci	/* tunnels are only present after everything has been initialized */
205262306a36Sopenharmony_ci	list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
205362306a36Sopenharmony_ci		/*
205462306a36Sopenharmony_ci		 * DMA tunnels require the driver to be functional so we
205562306a36Sopenharmony_ci		 * tear them down. Other protocol tunnels can be left
205662306a36Sopenharmony_ci		 * intact.
205762306a36Sopenharmony_ci		 */
205862306a36Sopenharmony_ci		if (tb_tunnel_is_dma(tunnel))
205962306a36Sopenharmony_ci			tb_tunnel_deactivate(tunnel);
206062306a36Sopenharmony_ci		tb_tunnel_free(tunnel);
206162306a36Sopenharmony_ci	}
206262306a36Sopenharmony_ci	tb_switch_remove(tb->root_switch);
206362306a36Sopenharmony_ci	tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
206462306a36Sopenharmony_ci}
206562306a36Sopenharmony_ci
206662306a36Sopenharmony_cistatic int tb_scan_finalize_switch(struct device *dev, void *data)
206762306a36Sopenharmony_ci{
206862306a36Sopenharmony_ci	if (tb_is_switch(dev)) {
206962306a36Sopenharmony_ci		struct tb_switch *sw = tb_to_switch(dev);
207062306a36Sopenharmony_ci
207162306a36Sopenharmony_ci		/*
207262306a36Sopenharmony_ci		 * If we found that the switch was already setup by the
207362306a36Sopenharmony_ci		 * boot firmware, mark it as authorized now before we
207462306a36Sopenharmony_ci		 * send uevent to userspace.
207562306a36Sopenharmony_ci		 */
207662306a36Sopenharmony_ci		if (sw->boot)
207762306a36Sopenharmony_ci			sw->authorized = 1;
207862306a36Sopenharmony_ci
207962306a36Sopenharmony_ci		dev_set_uevent_suppress(dev, false);
208062306a36Sopenharmony_ci		kobject_uevent(&dev->kobj, KOBJ_ADD);
208162306a36Sopenharmony_ci		device_for_each_child(dev, NULL, tb_scan_finalize_switch);
208262306a36Sopenharmony_ci	}
208362306a36Sopenharmony_ci
208462306a36Sopenharmony_ci	return 0;
208562306a36Sopenharmony_ci}
208662306a36Sopenharmony_ci
208762306a36Sopenharmony_cistatic int tb_start(struct tb *tb)
208862306a36Sopenharmony_ci{
208962306a36Sopenharmony_ci	struct tb_cm *tcm = tb_priv(tb);
209062306a36Sopenharmony_ci	int ret;
209162306a36Sopenharmony_ci
209262306a36Sopenharmony_ci	tb->root_switch = tb_switch_alloc(tb, &tb->dev, 0);
209362306a36Sopenharmony_ci	if (IS_ERR(tb->root_switch))
209462306a36Sopenharmony_ci		return PTR_ERR(tb->root_switch);
209562306a36Sopenharmony_ci
209662306a36Sopenharmony_ci	/*
209762306a36Sopenharmony_ci	 * ICM firmware upgrade needs running firmware and in native
209862306a36Sopenharmony_ci	 * mode that is not available so disable firmware upgrade of the
209962306a36Sopenharmony_ci	 * root switch.
210062306a36Sopenharmony_ci	 *
210162306a36Sopenharmony_ci	 * However, USB4 routers support NVM firmware upgrade if they
210262306a36Sopenharmony_ci	 * implement the necessary router operations.
210362306a36Sopenharmony_ci	 */
210462306a36Sopenharmony_ci	tb->root_switch->no_nvm_upgrade = !tb_switch_is_usb4(tb->root_switch);
210562306a36Sopenharmony_ci	/* All USB4 routers support runtime PM */
210662306a36Sopenharmony_ci	tb->root_switch->rpm = tb_switch_is_usb4(tb->root_switch);
210762306a36Sopenharmony_ci
210862306a36Sopenharmony_ci	ret = tb_switch_configure(tb->root_switch);
210962306a36Sopenharmony_ci	if (ret) {
211062306a36Sopenharmony_ci		tb_switch_put(tb->root_switch);
211162306a36Sopenharmony_ci		return ret;
211262306a36Sopenharmony_ci	}
211362306a36Sopenharmony_ci
211462306a36Sopenharmony_ci	/* Announce the switch to the world */
211562306a36Sopenharmony_ci	ret = tb_switch_add(tb->root_switch);
211662306a36Sopenharmony_ci	if (ret) {
211762306a36Sopenharmony_ci		tb_switch_put(tb->root_switch);
211862306a36Sopenharmony_ci		return ret;
211962306a36Sopenharmony_ci	}
212062306a36Sopenharmony_ci
212162306a36Sopenharmony_ci	/*
212262306a36Sopenharmony_ci	 * To support highest CLx state, we set host router's TMU to
212362306a36Sopenharmony_ci	 * Normal mode.
212462306a36Sopenharmony_ci	 */
212562306a36Sopenharmony_ci	tb_switch_tmu_configure(tb->root_switch, TB_SWITCH_TMU_MODE_LOWRES);
212662306a36Sopenharmony_ci	/* Enable TMU if it is off */
212762306a36Sopenharmony_ci	tb_switch_tmu_enable(tb->root_switch);
212862306a36Sopenharmony_ci	/* Full scan to discover devices added before the driver was loaded. */
212962306a36Sopenharmony_ci	tb_scan_switch(tb->root_switch);
213062306a36Sopenharmony_ci	/* Find out tunnels created by the boot firmware */
213162306a36Sopenharmony_ci	tb_discover_tunnels(tb);
213262306a36Sopenharmony_ci	/* Add DP resources from the DP tunnels created by the boot firmware */
213362306a36Sopenharmony_ci	tb_discover_dp_resources(tb);
213462306a36Sopenharmony_ci	/*
213562306a36Sopenharmony_ci	 * If the boot firmware did not create USB 3.x tunnels create them
213662306a36Sopenharmony_ci	 * now for the whole topology.
213762306a36Sopenharmony_ci	 */
213862306a36Sopenharmony_ci	tb_create_usb3_tunnels(tb->root_switch);
213962306a36Sopenharmony_ci	/* Add DP IN resources for the root switch */
214062306a36Sopenharmony_ci	tb_add_dp_resources(tb->root_switch);
214162306a36Sopenharmony_ci	/* Make the discovered switches available to the userspace */
214262306a36Sopenharmony_ci	device_for_each_child(&tb->root_switch->dev, NULL,
214362306a36Sopenharmony_ci			      tb_scan_finalize_switch);
214462306a36Sopenharmony_ci
214562306a36Sopenharmony_ci	/* Allow tb_handle_hotplug to progress events */
214662306a36Sopenharmony_ci	tcm->hotplug_active = true;
214762306a36Sopenharmony_ci	return 0;
214862306a36Sopenharmony_ci}
214962306a36Sopenharmony_ci
215062306a36Sopenharmony_cistatic int tb_suspend_noirq(struct tb *tb)
215162306a36Sopenharmony_ci{
215262306a36Sopenharmony_ci	struct tb_cm *tcm = tb_priv(tb);
215362306a36Sopenharmony_ci
215462306a36Sopenharmony_ci	tb_dbg(tb, "suspending...\n");
215562306a36Sopenharmony_ci	tb_disconnect_and_release_dp(tb);
215662306a36Sopenharmony_ci	tb_switch_suspend(tb->root_switch, false);
215762306a36Sopenharmony_ci	tcm->hotplug_active = false; /* signal tb_handle_hotplug to quit */
215862306a36Sopenharmony_ci	tb_dbg(tb, "suspend finished\n");
215962306a36Sopenharmony_ci
216062306a36Sopenharmony_ci	return 0;
216162306a36Sopenharmony_ci}
216262306a36Sopenharmony_ci
216362306a36Sopenharmony_cistatic void tb_restore_children(struct tb_switch *sw)
216462306a36Sopenharmony_ci{
216562306a36Sopenharmony_ci	struct tb_port *port;
216662306a36Sopenharmony_ci
216762306a36Sopenharmony_ci	/* No need to restore if the router is already unplugged */
216862306a36Sopenharmony_ci	if (sw->is_unplugged)
216962306a36Sopenharmony_ci		return;
217062306a36Sopenharmony_ci
217162306a36Sopenharmony_ci	if (tb_enable_clx(sw))
217262306a36Sopenharmony_ci		tb_sw_warn(sw, "failed to re-enable CL states\n");
217362306a36Sopenharmony_ci
217462306a36Sopenharmony_ci	if (tb_enable_tmu(sw))
217562306a36Sopenharmony_ci		tb_sw_warn(sw, "failed to restore TMU configuration\n");
217662306a36Sopenharmony_ci
217762306a36Sopenharmony_ci	tb_switch_configuration_valid(sw);
217862306a36Sopenharmony_ci
217962306a36Sopenharmony_ci	tb_switch_for_each_port(sw, port) {
218062306a36Sopenharmony_ci		if (!tb_port_has_remote(port) && !port->xdomain)
218162306a36Sopenharmony_ci			continue;
218262306a36Sopenharmony_ci
218362306a36Sopenharmony_ci		if (port->remote) {
218462306a36Sopenharmony_ci			tb_switch_lane_bonding_enable(port->remote->sw);
218562306a36Sopenharmony_ci			tb_switch_configure_link(port->remote->sw);
218662306a36Sopenharmony_ci
218762306a36Sopenharmony_ci			tb_restore_children(port->remote->sw);
218862306a36Sopenharmony_ci		} else if (port->xdomain) {
218962306a36Sopenharmony_ci			tb_port_configure_xdomain(port, port->xdomain);
219062306a36Sopenharmony_ci		}
219162306a36Sopenharmony_ci	}
219262306a36Sopenharmony_ci}
219362306a36Sopenharmony_ci
219462306a36Sopenharmony_cistatic int tb_resume_noirq(struct tb *tb)
219562306a36Sopenharmony_ci{
219662306a36Sopenharmony_ci	struct tb_cm *tcm = tb_priv(tb);
219762306a36Sopenharmony_ci	struct tb_tunnel *tunnel, *n;
219862306a36Sopenharmony_ci	unsigned int usb3_delay = 0;
219962306a36Sopenharmony_ci	LIST_HEAD(tunnels);
220062306a36Sopenharmony_ci
220162306a36Sopenharmony_ci	tb_dbg(tb, "resuming...\n");
220262306a36Sopenharmony_ci
220362306a36Sopenharmony_ci	/* remove any pci devices the firmware might have setup */
220462306a36Sopenharmony_ci	tb_switch_reset(tb->root_switch);
220562306a36Sopenharmony_ci
220662306a36Sopenharmony_ci	tb_switch_resume(tb->root_switch);
220762306a36Sopenharmony_ci	tb_free_invalid_tunnels(tb);
220862306a36Sopenharmony_ci	tb_free_unplugged_children(tb->root_switch);
220962306a36Sopenharmony_ci	tb_restore_children(tb->root_switch);
221062306a36Sopenharmony_ci
221162306a36Sopenharmony_ci	/*
221262306a36Sopenharmony_ci	 * If we get here from suspend to disk the boot firmware or the
221362306a36Sopenharmony_ci	 * restore kernel might have created tunnels of its own. Since
221462306a36Sopenharmony_ci	 * we cannot be sure they are usable for us we find and tear
221562306a36Sopenharmony_ci	 * them down.
221662306a36Sopenharmony_ci	 */
221762306a36Sopenharmony_ci	tb_switch_discover_tunnels(tb->root_switch, &tunnels, false);
221862306a36Sopenharmony_ci	list_for_each_entry_safe_reverse(tunnel, n, &tunnels, list) {
221962306a36Sopenharmony_ci		if (tb_tunnel_is_usb3(tunnel))
222062306a36Sopenharmony_ci			usb3_delay = 500;
222162306a36Sopenharmony_ci		tb_tunnel_deactivate(tunnel);
222262306a36Sopenharmony_ci		tb_tunnel_free(tunnel);
222362306a36Sopenharmony_ci	}
222462306a36Sopenharmony_ci
222562306a36Sopenharmony_ci	/* Re-create our tunnels now */
222662306a36Sopenharmony_ci	list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list) {
222762306a36Sopenharmony_ci		/* USB3 requires delay before it can be re-activated */
222862306a36Sopenharmony_ci		if (tb_tunnel_is_usb3(tunnel)) {
222962306a36Sopenharmony_ci			msleep(usb3_delay);
223062306a36Sopenharmony_ci			/* Only need to do it once */
223162306a36Sopenharmony_ci			usb3_delay = 0;
223262306a36Sopenharmony_ci		}
223362306a36Sopenharmony_ci		tb_tunnel_restart(tunnel);
223462306a36Sopenharmony_ci	}
223562306a36Sopenharmony_ci	if (!list_empty(&tcm->tunnel_list)) {
223662306a36Sopenharmony_ci		/*
223762306a36Sopenharmony_ci		 * the pcie links need some time to get going.
223862306a36Sopenharmony_ci		 * 100ms works for me...
223962306a36Sopenharmony_ci		 */
224062306a36Sopenharmony_ci		tb_dbg(tb, "tunnels restarted, sleeping for 100ms\n");
224162306a36Sopenharmony_ci		msleep(100);
224262306a36Sopenharmony_ci	}
224362306a36Sopenharmony_ci	 /* Allow tb_handle_hotplug to progress events */
224462306a36Sopenharmony_ci	tcm->hotplug_active = true;
224562306a36Sopenharmony_ci	tb_dbg(tb, "resume finished\n");
224662306a36Sopenharmony_ci
224762306a36Sopenharmony_ci	return 0;
224862306a36Sopenharmony_ci}
224962306a36Sopenharmony_ci
225062306a36Sopenharmony_cistatic int tb_free_unplugged_xdomains(struct tb_switch *sw)
225162306a36Sopenharmony_ci{
225262306a36Sopenharmony_ci	struct tb_port *port;
225362306a36Sopenharmony_ci	int ret = 0;
225462306a36Sopenharmony_ci
225562306a36Sopenharmony_ci	tb_switch_for_each_port(sw, port) {
225662306a36Sopenharmony_ci		if (tb_is_upstream_port(port))
225762306a36Sopenharmony_ci			continue;
225862306a36Sopenharmony_ci		if (port->xdomain && port->xdomain->is_unplugged) {
225962306a36Sopenharmony_ci			tb_retimer_remove_all(port);
226062306a36Sopenharmony_ci			tb_xdomain_remove(port->xdomain);
226162306a36Sopenharmony_ci			tb_port_unconfigure_xdomain(port);
226262306a36Sopenharmony_ci			port->xdomain = NULL;
226362306a36Sopenharmony_ci			ret++;
226462306a36Sopenharmony_ci		} else if (port->remote) {
226562306a36Sopenharmony_ci			ret += tb_free_unplugged_xdomains(port->remote->sw);
226662306a36Sopenharmony_ci		}
226762306a36Sopenharmony_ci	}
226862306a36Sopenharmony_ci
226962306a36Sopenharmony_ci	return ret;
227062306a36Sopenharmony_ci}
227162306a36Sopenharmony_ci
227262306a36Sopenharmony_cistatic int tb_freeze_noirq(struct tb *tb)
227362306a36Sopenharmony_ci{
227462306a36Sopenharmony_ci	struct tb_cm *tcm = tb_priv(tb);
227562306a36Sopenharmony_ci
227662306a36Sopenharmony_ci	tcm->hotplug_active = false;
227762306a36Sopenharmony_ci	return 0;
227862306a36Sopenharmony_ci}
227962306a36Sopenharmony_ci
228062306a36Sopenharmony_cistatic int tb_thaw_noirq(struct tb *tb)
228162306a36Sopenharmony_ci{
228262306a36Sopenharmony_ci	struct tb_cm *tcm = tb_priv(tb);
228362306a36Sopenharmony_ci
228462306a36Sopenharmony_ci	tcm->hotplug_active = true;
228562306a36Sopenharmony_ci	return 0;
228662306a36Sopenharmony_ci}
228762306a36Sopenharmony_ci
228862306a36Sopenharmony_cistatic void tb_complete(struct tb *tb)
228962306a36Sopenharmony_ci{
229062306a36Sopenharmony_ci	/*
229162306a36Sopenharmony_ci	 * Release any unplugged XDomains and if there is a case where
229262306a36Sopenharmony_ci	 * another domain is swapped in place of unplugged XDomain we
229362306a36Sopenharmony_ci	 * need to run another rescan.
229462306a36Sopenharmony_ci	 */
229562306a36Sopenharmony_ci	mutex_lock(&tb->lock);
229662306a36Sopenharmony_ci	if (tb_free_unplugged_xdomains(tb->root_switch))
229762306a36Sopenharmony_ci		tb_scan_switch(tb->root_switch);
229862306a36Sopenharmony_ci	mutex_unlock(&tb->lock);
229962306a36Sopenharmony_ci}
230062306a36Sopenharmony_ci
230162306a36Sopenharmony_cistatic int tb_runtime_suspend(struct tb *tb)
230262306a36Sopenharmony_ci{
230362306a36Sopenharmony_ci	struct tb_cm *tcm = tb_priv(tb);
230462306a36Sopenharmony_ci
230562306a36Sopenharmony_ci	mutex_lock(&tb->lock);
230662306a36Sopenharmony_ci	tb_switch_suspend(tb->root_switch, true);
230762306a36Sopenharmony_ci	tcm->hotplug_active = false;
230862306a36Sopenharmony_ci	mutex_unlock(&tb->lock);
230962306a36Sopenharmony_ci
231062306a36Sopenharmony_ci	return 0;
231162306a36Sopenharmony_ci}
231262306a36Sopenharmony_ci
231362306a36Sopenharmony_cistatic void tb_remove_work(struct work_struct *work)
231462306a36Sopenharmony_ci{
231562306a36Sopenharmony_ci	struct tb_cm *tcm = container_of(work, struct tb_cm, remove_work.work);
231662306a36Sopenharmony_ci	struct tb *tb = tcm_to_tb(tcm);
231762306a36Sopenharmony_ci
231862306a36Sopenharmony_ci	mutex_lock(&tb->lock);
231962306a36Sopenharmony_ci	if (tb->root_switch) {
232062306a36Sopenharmony_ci		tb_free_unplugged_children(tb->root_switch);
232162306a36Sopenharmony_ci		tb_free_unplugged_xdomains(tb->root_switch);
232262306a36Sopenharmony_ci	}
232362306a36Sopenharmony_ci	mutex_unlock(&tb->lock);
232462306a36Sopenharmony_ci}
232562306a36Sopenharmony_ci
232662306a36Sopenharmony_cistatic int tb_runtime_resume(struct tb *tb)
232762306a36Sopenharmony_ci{
232862306a36Sopenharmony_ci	struct tb_cm *tcm = tb_priv(tb);
232962306a36Sopenharmony_ci	struct tb_tunnel *tunnel, *n;
233062306a36Sopenharmony_ci
233162306a36Sopenharmony_ci	mutex_lock(&tb->lock);
233262306a36Sopenharmony_ci	tb_switch_resume(tb->root_switch);
233362306a36Sopenharmony_ci	tb_free_invalid_tunnels(tb);
233462306a36Sopenharmony_ci	tb_restore_children(tb->root_switch);
233562306a36Sopenharmony_ci	list_for_each_entry_safe(tunnel, n, &tcm->tunnel_list, list)
233662306a36Sopenharmony_ci		tb_tunnel_restart(tunnel);
233762306a36Sopenharmony_ci	tcm->hotplug_active = true;
233862306a36Sopenharmony_ci	mutex_unlock(&tb->lock);
233962306a36Sopenharmony_ci
234062306a36Sopenharmony_ci	/*
234162306a36Sopenharmony_ci	 * Schedule cleanup of any unplugged devices. Run this in a
234262306a36Sopenharmony_ci	 * separate thread to avoid possible deadlock if the device
234362306a36Sopenharmony_ci	 * removal runtime resumes the unplugged device.
234462306a36Sopenharmony_ci	 */
234562306a36Sopenharmony_ci	queue_delayed_work(tb->wq, &tcm->remove_work, msecs_to_jiffies(50));
234662306a36Sopenharmony_ci	return 0;
234762306a36Sopenharmony_ci}
234862306a36Sopenharmony_ci
234962306a36Sopenharmony_cistatic const struct tb_cm_ops tb_cm_ops = {
235062306a36Sopenharmony_ci	.start = tb_start,
235162306a36Sopenharmony_ci	.stop = tb_stop,
235262306a36Sopenharmony_ci	.suspend_noirq = tb_suspend_noirq,
235362306a36Sopenharmony_ci	.resume_noirq = tb_resume_noirq,
235462306a36Sopenharmony_ci	.freeze_noirq = tb_freeze_noirq,
235562306a36Sopenharmony_ci	.thaw_noirq = tb_thaw_noirq,
235662306a36Sopenharmony_ci	.complete = tb_complete,
235762306a36Sopenharmony_ci	.runtime_suspend = tb_runtime_suspend,
235862306a36Sopenharmony_ci	.runtime_resume = tb_runtime_resume,
235962306a36Sopenharmony_ci	.handle_event = tb_handle_event,
236062306a36Sopenharmony_ci	.disapprove_switch = tb_disconnect_pci,
236162306a36Sopenharmony_ci	.approve_switch = tb_tunnel_pci,
236262306a36Sopenharmony_ci	.approve_xdomain_paths = tb_approve_xdomain_paths,
236362306a36Sopenharmony_ci	.disconnect_xdomain_paths = tb_disconnect_xdomain_paths,
236462306a36Sopenharmony_ci};
236562306a36Sopenharmony_ci
236662306a36Sopenharmony_ci/*
236762306a36Sopenharmony_ci * During suspend the Thunderbolt controller is reset and all PCIe
236862306a36Sopenharmony_ci * tunnels are lost. The NHI driver will try to reestablish all tunnels
236962306a36Sopenharmony_ci * during resume. This adds device links between the tunneled PCIe
237062306a36Sopenharmony_ci * downstream ports and the NHI so that the device core will make sure
237162306a36Sopenharmony_ci * NHI is resumed first before the rest.
237262306a36Sopenharmony_ci */
237362306a36Sopenharmony_cistatic bool tb_apple_add_links(struct tb_nhi *nhi)
237462306a36Sopenharmony_ci{
237562306a36Sopenharmony_ci	struct pci_dev *upstream, *pdev;
237662306a36Sopenharmony_ci	bool ret;
237762306a36Sopenharmony_ci
237862306a36Sopenharmony_ci	if (!x86_apple_machine)
237962306a36Sopenharmony_ci		return false;
238062306a36Sopenharmony_ci
238162306a36Sopenharmony_ci	switch (nhi->pdev->device) {
238262306a36Sopenharmony_ci	case PCI_DEVICE_ID_INTEL_LIGHT_RIDGE:
238362306a36Sopenharmony_ci	case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
238462306a36Sopenharmony_ci	case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_NHI:
238562306a36Sopenharmony_ci	case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_NHI:
238662306a36Sopenharmony_ci		break;
238762306a36Sopenharmony_ci	default:
238862306a36Sopenharmony_ci		return false;
238962306a36Sopenharmony_ci	}
239062306a36Sopenharmony_ci
239162306a36Sopenharmony_ci	upstream = pci_upstream_bridge(nhi->pdev);
239262306a36Sopenharmony_ci	while (upstream) {
239362306a36Sopenharmony_ci		if (!pci_is_pcie(upstream))
239462306a36Sopenharmony_ci			return false;
239562306a36Sopenharmony_ci		if (pci_pcie_type(upstream) == PCI_EXP_TYPE_UPSTREAM)
239662306a36Sopenharmony_ci			break;
239762306a36Sopenharmony_ci		upstream = pci_upstream_bridge(upstream);
239862306a36Sopenharmony_ci	}
239962306a36Sopenharmony_ci
240062306a36Sopenharmony_ci	if (!upstream)
240162306a36Sopenharmony_ci		return false;
240262306a36Sopenharmony_ci
240362306a36Sopenharmony_ci	/*
240462306a36Sopenharmony_ci	 * For each hotplug downstream port, create add device link
240562306a36Sopenharmony_ci	 * back to NHI so that PCIe tunnels can be re-established after
240662306a36Sopenharmony_ci	 * sleep.
240762306a36Sopenharmony_ci	 */
240862306a36Sopenharmony_ci	ret = false;
240962306a36Sopenharmony_ci	for_each_pci_bridge(pdev, upstream->subordinate) {
241062306a36Sopenharmony_ci		const struct device_link *link;
241162306a36Sopenharmony_ci
241262306a36Sopenharmony_ci		if (!pci_is_pcie(pdev))
241362306a36Sopenharmony_ci			continue;
241462306a36Sopenharmony_ci		if (pci_pcie_type(pdev) != PCI_EXP_TYPE_DOWNSTREAM ||
241562306a36Sopenharmony_ci		    !pdev->is_hotplug_bridge)
241662306a36Sopenharmony_ci			continue;
241762306a36Sopenharmony_ci
241862306a36Sopenharmony_ci		link = device_link_add(&pdev->dev, &nhi->pdev->dev,
241962306a36Sopenharmony_ci				       DL_FLAG_AUTOREMOVE_SUPPLIER |
242062306a36Sopenharmony_ci				       DL_FLAG_PM_RUNTIME);
242162306a36Sopenharmony_ci		if (link) {
242262306a36Sopenharmony_ci			dev_dbg(&nhi->pdev->dev, "created link from %s\n",
242362306a36Sopenharmony_ci				dev_name(&pdev->dev));
242462306a36Sopenharmony_ci			ret = true;
242562306a36Sopenharmony_ci		} else {
242662306a36Sopenharmony_ci			dev_warn(&nhi->pdev->dev, "device link creation from %s failed\n",
242762306a36Sopenharmony_ci				 dev_name(&pdev->dev));
242862306a36Sopenharmony_ci		}
242962306a36Sopenharmony_ci	}
243062306a36Sopenharmony_ci
243162306a36Sopenharmony_ci	return ret;
243262306a36Sopenharmony_ci}
243362306a36Sopenharmony_ci
243462306a36Sopenharmony_cistruct tb *tb_probe(struct tb_nhi *nhi)
243562306a36Sopenharmony_ci{
243662306a36Sopenharmony_ci	struct tb_cm *tcm;
243762306a36Sopenharmony_ci	struct tb *tb;
243862306a36Sopenharmony_ci
243962306a36Sopenharmony_ci	tb = tb_domain_alloc(nhi, TB_TIMEOUT, sizeof(*tcm));
244062306a36Sopenharmony_ci	if (!tb)
244162306a36Sopenharmony_ci		return NULL;
244262306a36Sopenharmony_ci
244362306a36Sopenharmony_ci	if (tb_acpi_may_tunnel_pcie())
244462306a36Sopenharmony_ci		tb->security_level = TB_SECURITY_USER;
244562306a36Sopenharmony_ci	else
244662306a36Sopenharmony_ci		tb->security_level = TB_SECURITY_NOPCIE;
244762306a36Sopenharmony_ci
244862306a36Sopenharmony_ci	tb->cm_ops = &tb_cm_ops;
244962306a36Sopenharmony_ci
245062306a36Sopenharmony_ci	tcm = tb_priv(tb);
245162306a36Sopenharmony_ci	INIT_LIST_HEAD(&tcm->tunnel_list);
245262306a36Sopenharmony_ci	INIT_LIST_HEAD(&tcm->dp_resources);
245362306a36Sopenharmony_ci	INIT_DELAYED_WORK(&tcm->remove_work, tb_remove_work);
245462306a36Sopenharmony_ci	tb_init_bandwidth_groups(tcm);
245562306a36Sopenharmony_ci
245662306a36Sopenharmony_ci	tb_dbg(tb, "using software connection manager\n");
245762306a36Sopenharmony_ci
245862306a36Sopenharmony_ci	/*
245962306a36Sopenharmony_ci	 * Device links are needed to make sure we establish tunnels
246062306a36Sopenharmony_ci	 * before the PCIe/USB stack is resumed so complain here if we
246162306a36Sopenharmony_ci	 * found them missing.
246262306a36Sopenharmony_ci	 */
246362306a36Sopenharmony_ci	if (!tb_apple_add_links(nhi) && !tb_acpi_add_links(nhi))
246462306a36Sopenharmony_ci		tb_warn(tb, "device links to tunneled native ports are missing!\n");
246562306a36Sopenharmony_ci
246662306a36Sopenharmony_ci	return tb;
246762306a36Sopenharmony_ci}
2468