Commit 7ce54221 authored by Gil Fine's avatar Gil Fine Committed by Mika Westerberg

thunderbolt: Introduce tb_switch_downstream_port()

Introduce tb_switch_downstream_port() helper function that returns the
downstream port of a parent switch that is connected to the upstream
port of specified switch. From now on, we use it all across the driver
where applicable.

While there fix a whitespace in comment and rename 'downstream' to
'down' to be consistent with the rest of the driver.
Signed-off-by: default avatarGil Fine <gil.fine@intel.com>
Signed-off-by: default avatarMika Westerberg <mika.westerberg@linux.intel.com>
parent ef65afa0
...@@ -296,16 +296,15 @@ static bool tb_acpi_bus_match(struct device *dev) ...@@ -296,16 +296,15 @@ static bool tb_acpi_bus_match(struct device *dev)
static struct acpi_device *tb_acpi_switch_find_companion(struct tb_switch *sw) static struct acpi_device *tb_acpi_switch_find_companion(struct tb_switch *sw)
{ {
struct tb_switch *parent_sw = tb_switch_parent(sw);
struct acpi_device *adev = NULL; struct acpi_device *adev = NULL;
struct tb_switch *parent_sw;
/* /*
* Device routers exists under the downstream facing USB4 port * Device routers exists under the downstream facing USB4 port
* of the parent router. Their _ADR is always 0. * of the parent router. Their _ADR is always 0.
*/ */
parent_sw = tb_switch_parent(sw);
if (parent_sw) { if (parent_sw) {
struct tb_port *port = tb_port_at(tb_route(sw), parent_sw); struct tb_port *port = tb_switch_downstream_port(sw);
struct acpi_device *port_adev; struct acpi_device *port_adev;
port_adev = acpi_find_child_by_adr(ACPI_COMPANION(&parent_sw->dev), port_adev = acpi_find_child_by_adr(ACPI_COMPANION(&parent_sw->dev),
......
...@@ -644,13 +644,14 @@ static int add_switch(struct tb_switch *parent_sw, struct tb_switch *sw) ...@@ -644,13 +644,14 @@ static int add_switch(struct tb_switch *parent_sw, struct tb_switch *sw)
return ret; return ret;
} }
static void update_switch(struct tb_switch *parent_sw, struct tb_switch *sw, static void update_switch(struct tb_switch *sw, u64 route, u8 connection_id,
u64 route, u8 connection_id, u8 connection_key, u8 connection_key, u8 link, u8 depth, bool boot)
u8 link, u8 depth, bool boot)
{ {
struct tb_switch *parent_sw = tb_switch_parent(sw);
/* Disconnect from parent */ /* Disconnect from parent */
tb_port_at(tb_route(sw), parent_sw)->remote = NULL; tb_switch_downstream_port(sw)->remote = NULL;
/* Re-connect via updated port*/ /* Re-connect via updated port */
tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw); tb_port_at(route, parent_sw)->remote = tb_upstream_port(sw);
/* Update with the new addressing information */ /* Update with the new addressing information */
...@@ -671,10 +672,7 @@ static void update_switch(struct tb_switch *parent_sw, struct tb_switch *sw, ...@@ -671,10 +672,7 @@ static void update_switch(struct tb_switch *parent_sw, struct tb_switch *sw,
static void remove_switch(struct tb_switch *sw) static void remove_switch(struct tb_switch *sw)
{ {
struct tb_switch *parent_sw; tb_switch_downstream_port(sw)->remote = NULL;
parent_sw = tb_to_switch(sw->dev.parent);
tb_port_at(tb_route(sw), parent_sw)->remote = NULL;
tb_switch_remove(sw); tb_switch_remove(sw);
} }
...@@ -755,7 +753,6 @@ icm_fr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr) ...@@ -755,7 +753,6 @@ icm_fr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)
if (sw) { if (sw) {
u8 phy_port, sw_phy_port; u8 phy_port, sw_phy_port;
parent_sw = tb_to_switch(sw->dev.parent);
sw_phy_port = tb_phy_port_from_link(sw->link); sw_phy_port = tb_phy_port_from_link(sw->link);
phy_port = tb_phy_port_from_link(link); phy_port = tb_phy_port_from_link(link);
...@@ -785,7 +782,7 @@ icm_fr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr) ...@@ -785,7 +782,7 @@ icm_fr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr)
route = tb_route(sw); route = tb_route(sw);
} }
update_switch(parent_sw, sw, route, pkg->connection_id, update_switch(sw, route, pkg->connection_id,
pkg->connection_key, link, depth, boot); pkg->connection_key, link, depth, boot);
tb_switch_put(sw); tb_switch_put(sw);
return; return;
...@@ -1236,9 +1233,8 @@ __icm_tr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr, ...@@ -1236,9 +1233,8 @@ __icm_tr_device_connected(struct tb *tb, const struct icm_pkg_header *hdr,
if (sw) { if (sw) {
/* Update the switch if it is still in the same place */ /* Update the switch if it is still in the same place */
if (tb_route(sw) == route && !!sw->authorized == authorized) { if (tb_route(sw) == route && !!sw->authorized == authorized) {
parent_sw = tb_to_switch(sw->dev.parent); update_switch(sw, route, pkg->connection_id, 0, 0, 0,
update_switch(parent_sw, sw, route, pkg->connection_id, boot);
0, 0, 0, boot);
tb_switch_put(sw); tb_switch_put(sw);
return; return;
} }
......
...@@ -2754,7 +2754,6 @@ static int tb_switch_update_link_attributes(struct tb_switch *sw) ...@@ -2754,7 +2754,6 @@ static int tb_switch_update_link_attributes(struct tb_switch *sw)
*/ */
int tb_switch_lane_bonding_enable(struct tb_switch *sw) int tb_switch_lane_bonding_enable(struct tb_switch *sw)
{ {
struct tb_switch *parent = tb_to_switch(sw->dev.parent);
struct tb_port *up, *down; struct tb_port *up, *down;
u64 route = tb_route(sw); u64 route = tb_route(sw);
int ret; int ret;
...@@ -2766,7 +2765,7 @@ int tb_switch_lane_bonding_enable(struct tb_switch *sw) ...@@ -2766,7 +2765,7 @@ int tb_switch_lane_bonding_enable(struct tb_switch *sw)
return 0; return 0;
up = tb_upstream_port(sw); up = tb_upstream_port(sw);
down = tb_port_at(route, parent); down = tb_switch_downstream_port(sw);
if (!tb_port_is_width_supported(up, 2) || if (!tb_port_is_width_supported(up, 2) ||
!tb_port_is_width_supported(down, 2)) !tb_port_is_width_supported(down, 2))
...@@ -2808,7 +2807,6 @@ int tb_switch_lane_bonding_enable(struct tb_switch *sw) ...@@ -2808,7 +2807,6 @@ int tb_switch_lane_bonding_enable(struct tb_switch *sw)
*/ */
void tb_switch_lane_bonding_disable(struct tb_switch *sw) void tb_switch_lane_bonding_disable(struct tb_switch *sw)
{ {
struct tb_switch *parent = tb_to_switch(sw->dev.parent);
struct tb_port *up, *down; struct tb_port *up, *down;
if (!tb_route(sw)) if (!tb_route(sw))
...@@ -2818,7 +2816,7 @@ void tb_switch_lane_bonding_disable(struct tb_switch *sw) ...@@ -2818,7 +2816,7 @@ void tb_switch_lane_bonding_disable(struct tb_switch *sw)
if (!up->bonded) if (!up->bonded)
return; return;
down = tb_port_at(tb_route(sw), parent); down = tb_switch_downstream_port(sw);
tb_port_lane_bonding_disable(up); tb_port_lane_bonding_disable(up);
tb_port_lane_bonding_disable(down); tb_port_lane_bonding_disable(down);
...@@ -3476,7 +3474,6 @@ struct tb_port *tb_switch_find_port(struct tb_switch *sw, ...@@ -3476,7 +3474,6 @@ struct tb_port *tb_switch_find_port(struct tb_switch *sw,
static int tb_switch_pm_secondary_resolve(struct tb_switch *sw) static int tb_switch_pm_secondary_resolve(struct tb_switch *sw)
{ {
struct tb_switch *parent = tb_switch_parent(sw);
struct tb_port *up, *down; struct tb_port *up, *down;
int ret; int ret;
...@@ -3484,7 +3481,7 @@ static int tb_switch_pm_secondary_resolve(struct tb_switch *sw) ...@@ -3484,7 +3481,7 @@ static int tb_switch_pm_secondary_resolve(struct tb_switch *sw)
return 0; return 0;
up = tb_upstream_port(sw); up = tb_upstream_port(sw);
down = tb_port_at(tb_route(sw), parent); down = tb_switch_downstream_port(sw);
ret = tb_port_pm_secondary_enable(up); ret = tb_port_pm_secondary_enable(up);
if (ret) if (ret)
return ret; return ret;
...@@ -3494,7 +3491,6 @@ static int tb_switch_pm_secondary_resolve(struct tb_switch *sw) ...@@ -3494,7 +3491,6 @@ static int tb_switch_pm_secondary_resolve(struct tb_switch *sw)
static int __tb_switch_enable_clx(struct tb_switch *sw, enum tb_clx clx) static int __tb_switch_enable_clx(struct tb_switch *sw, enum tb_clx clx)
{ {
struct tb_switch *parent = tb_switch_parent(sw);
bool up_clx_support, down_clx_support; bool up_clx_support, down_clx_support;
struct tb_port *up, *down; struct tb_port *up, *down;
int ret; int ret;
...@@ -3510,7 +3506,7 @@ static int __tb_switch_enable_clx(struct tb_switch *sw, enum tb_clx clx) ...@@ -3510,7 +3506,7 @@ static int __tb_switch_enable_clx(struct tb_switch *sw, enum tb_clx clx)
return 0; return 0;
/* Enable CLx only for first hop router (depth = 1) */ /* Enable CLx only for first hop router (depth = 1) */
if (tb_route(parent)) if (tb_route(tb_switch_parent(sw)))
return 0; return 0;
ret = tb_switch_pm_secondary_resolve(sw); ret = tb_switch_pm_secondary_resolve(sw);
...@@ -3518,7 +3514,7 @@ static int __tb_switch_enable_clx(struct tb_switch *sw, enum tb_clx clx) ...@@ -3518,7 +3514,7 @@ static int __tb_switch_enable_clx(struct tb_switch *sw, enum tb_clx clx)
return ret; return ret;
up = tb_upstream_port(sw); up = tb_upstream_port(sw);
down = tb_port_at(tb_route(sw), parent); down = tb_switch_downstream_port(sw);
up_clx_support = tb_port_clx_supported(up, clx); up_clx_support = tb_port_clx_supported(up, clx);
down_clx_support = tb_port_clx_supported(down, clx); down_clx_support = tb_port_clx_supported(down, clx);
...@@ -3594,7 +3590,6 @@ int tb_switch_enable_clx(struct tb_switch *sw, enum tb_clx clx) ...@@ -3594,7 +3590,6 @@ int tb_switch_enable_clx(struct tb_switch *sw, enum tb_clx clx)
static int __tb_switch_disable_clx(struct tb_switch *sw, enum tb_clx clx) static int __tb_switch_disable_clx(struct tb_switch *sw, enum tb_clx clx)
{ {
struct tb_switch *parent = tb_switch_parent(sw);
struct tb_port *up, *down; struct tb_port *up, *down;
int ret; int ret;
...@@ -3609,11 +3604,11 @@ static int __tb_switch_disable_clx(struct tb_switch *sw, enum tb_clx clx) ...@@ -3609,11 +3604,11 @@ static int __tb_switch_disable_clx(struct tb_switch *sw, enum tb_clx clx)
return 0; return 0;
/* Disable CLx only for first hop router (depth = 1) */ /* Disable CLx only for first hop router (depth = 1) */
if (tb_route(parent)) if (tb_route(tb_switch_parent(sw)))
return 0; return 0;
up = tb_upstream_port(sw); up = tb_upstream_port(sw);
down = tb_port_at(tb_route(sw), parent); down = tb_switch_downstream_port(sw);
ret = tb_port_clx_disable(up, clx); ret = tb_port_clx_disable(up, clx);
if (ret) if (ret)
return ret; return ret;
......
...@@ -628,7 +628,7 @@ static int tb_tunnel_usb3(struct tb *tb, struct tb_switch *sw) ...@@ -628,7 +628,7 @@ static int tb_tunnel_usb3(struct tb *tb, struct tb_switch *sw)
* Look up available down port. Since we are chaining it should * Look up available down port. Since we are chaining it should
* be found right above this switch. * be found right above this switch.
*/ */
port = tb_port_at(tb_route(sw), parent); port = tb_switch_downstream_port(sw);
down = tb_find_usb3_down(parent, port); down = tb_find_usb3_down(parent, port);
if (!down) if (!down)
return 0; return 0;
...@@ -1378,7 +1378,6 @@ static int tb_tunnel_pci(struct tb *tb, struct tb_switch *sw) ...@@ -1378,7 +1378,6 @@ static int tb_tunnel_pci(struct tb *tb, struct tb_switch *sw)
{ {
struct tb_port *up, *down, *port; struct tb_port *up, *down, *port;
struct tb_cm *tcm = tb_priv(tb); struct tb_cm *tcm = tb_priv(tb);
struct tb_switch *parent_sw;
struct tb_tunnel *tunnel; struct tb_tunnel *tunnel;
up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP); up = tb_switch_find_port(sw, TB_TYPE_PCIE_UP);
...@@ -1389,9 +1388,8 @@ static int tb_tunnel_pci(struct tb *tb, struct tb_switch *sw) ...@@ -1389,9 +1388,8 @@ static int tb_tunnel_pci(struct tb *tb, struct tb_switch *sw)
* Look up available down port. Since we are chaining it should * Look up available down port. Since we are chaining it should
* be found right above this switch. * be found right above this switch.
*/ */
parent_sw = tb_to_switch(sw->dev.parent); port = tb_switch_downstream_port(sw);
port = tb_port_at(tb_route(sw), parent_sw); down = tb_find_pcie_down(tb_switch_parent(sw), port);
down = tb_find_pcie_down(parent_sw, port);
if (!down) if (!down)
return 0; return 0;
......
...@@ -857,6 +857,20 @@ static inline struct tb_switch *tb_switch_parent(struct tb_switch *sw) ...@@ -857,6 +857,20 @@ static inline struct tb_switch *tb_switch_parent(struct tb_switch *sw)
return tb_to_switch(sw->dev.parent); return tb_to_switch(sw->dev.parent);
} }
/**
* tb_switch_downstream_port() - Return downstream facing port of parent router
* @sw: Device router pointer
*
* Only call for device routers. Returns the downstream facing port of
* the parent router.
*/
static inline struct tb_port *tb_switch_downstream_port(struct tb_switch *sw)
{
if (WARN_ON(!tb_route(sw)))
return NULL;
return tb_port_at(tb_route(sw), tb_switch_parent(sw));
}
static inline bool tb_switch_is_light_ridge(const struct tb_switch *sw) static inline bool tb_switch_is_light_ridge(const struct tb_switch *sw)
{ {
return sw->config.vendor_id == PCI_VENDOR_ID_INTEL && return sw->config.vendor_id == PCI_VENDOR_ID_INTEL &&
......
...@@ -398,11 +398,10 @@ int tb_switch_tmu_disable(struct tb_switch *sw) ...@@ -398,11 +398,10 @@ int tb_switch_tmu_disable(struct tb_switch *sw)
if (tb_route(sw)) { if (tb_route(sw)) {
bool unidirectional = sw->tmu.unidirectional; bool unidirectional = sw->tmu.unidirectional;
struct tb_switch *parent = tb_switch_parent(sw);
struct tb_port *down, *up; struct tb_port *down, *up;
int ret; int ret;
down = tb_port_at(tb_route(sw), parent); down = tb_switch_downstream_port(sw);
up = tb_upstream_port(sw); up = tb_upstream_port(sw);
/* /*
* In case of uni-directional time sync, TMU handshake is * In case of uni-directional time sync, TMU handshake is
...@@ -442,10 +441,9 @@ int tb_switch_tmu_disable(struct tb_switch *sw) ...@@ -442,10 +441,9 @@ int tb_switch_tmu_disable(struct tb_switch *sw)
static void __tb_switch_tmu_off(struct tb_switch *sw, bool unidirectional) static void __tb_switch_tmu_off(struct tb_switch *sw, bool unidirectional)
{ {
struct tb_switch *parent = tb_switch_parent(sw);
struct tb_port *down, *up; struct tb_port *down, *up;
down = tb_port_at(tb_route(sw), parent); down = tb_switch_downstream_port(sw);
up = tb_upstream_port(sw); up = tb_upstream_port(sw);
/* /*
* In case of any failure in one of the steps when setting * In case of any failure in one of the steps when setting
...@@ -457,7 +455,8 @@ static void __tb_switch_tmu_off(struct tb_switch *sw, bool unidirectional) ...@@ -457,7 +455,8 @@ static void __tb_switch_tmu_off(struct tb_switch *sw, bool unidirectional)
tb_port_tmu_time_sync_disable(down); tb_port_tmu_time_sync_disable(down);
tb_port_tmu_time_sync_disable(up); tb_port_tmu_time_sync_disable(up);
if (unidirectional) if (unidirectional)
tb_switch_tmu_rate_write(parent, TB_SWITCH_TMU_RATE_OFF); tb_switch_tmu_rate_write(tb_switch_parent(sw),
TB_SWITCH_TMU_RATE_OFF);
else else
tb_switch_tmu_rate_write(sw, TB_SWITCH_TMU_RATE_OFF); tb_switch_tmu_rate_write(sw, TB_SWITCH_TMU_RATE_OFF);
...@@ -472,12 +471,11 @@ static void __tb_switch_tmu_off(struct tb_switch *sw, bool unidirectional) ...@@ -472,12 +471,11 @@ static void __tb_switch_tmu_off(struct tb_switch *sw, bool unidirectional)
*/ */
static int __tb_switch_tmu_enable_bidirectional(struct tb_switch *sw) static int __tb_switch_tmu_enable_bidirectional(struct tb_switch *sw)
{ {
struct tb_switch *parent = tb_switch_parent(sw);
struct tb_port *up, *down; struct tb_port *up, *down;
int ret; int ret;
up = tb_upstream_port(sw); up = tb_upstream_port(sw);
down = tb_port_at(tb_route(sw), parent); down = tb_switch_downstream_port(sw);
ret = tb_port_tmu_unidirectional_disable(up); ret = tb_port_tmu_unidirectional_disable(up);
if (ret) if (ret)
...@@ -537,13 +535,13 @@ static int tb_switch_tmu_unidirectional_enable(struct tb_switch *sw) ...@@ -537,13 +535,13 @@ static int tb_switch_tmu_unidirectional_enable(struct tb_switch *sw)
*/ */
static int __tb_switch_tmu_enable_unidirectional(struct tb_switch *sw) static int __tb_switch_tmu_enable_unidirectional(struct tb_switch *sw)
{ {
struct tb_switch *parent = tb_switch_parent(sw);
struct tb_port *up, *down; struct tb_port *up, *down;
int ret; int ret;
up = tb_upstream_port(sw); up = tb_upstream_port(sw);
down = tb_port_at(tb_route(sw), parent); down = tb_switch_downstream_port(sw);
ret = tb_switch_tmu_rate_write(parent, sw->tmu.rate_request); ret = tb_switch_tmu_rate_write(tb_switch_parent(sw),
sw->tmu.rate_request);
if (ret) if (ret)
return ret; return ret;
...@@ -576,10 +574,9 @@ static int __tb_switch_tmu_enable_unidirectional(struct tb_switch *sw) ...@@ -576,10 +574,9 @@ static int __tb_switch_tmu_enable_unidirectional(struct tb_switch *sw)
static void __tb_switch_tmu_change_mode_prev(struct tb_switch *sw) static void __tb_switch_tmu_change_mode_prev(struct tb_switch *sw)
{ {
struct tb_switch *parent = tb_switch_parent(sw);
struct tb_port *down, *up; struct tb_port *down, *up;
down = tb_port_at(tb_route(sw), parent); down = tb_switch_downstream_port(sw);
up = tb_upstream_port(sw); up = tb_upstream_port(sw);
/* /*
* In case of any failure in one of the steps when change mode, * In case of any failure in one of the steps when change mode,
...@@ -589,7 +586,7 @@ static void __tb_switch_tmu_change_mode_prev(struct tb_switch *sw) ...@@ -589,7 +586,7 @@ static void __tb_switch_tmu_change_mode_prev(struct tb_switch *sw)
*/ */
tb_port_tmu_set_unidirectional(down, sw->tmu.unidirectional); tb_port_tmu_set_unidirectional(down, sw->tmu.unidirectional);
if (sw->tmu.unidirectional_request) if (sw->tmu.unidirectional_request)
tb_switch_tmu_rate_write(parent, sw->tmu.rate); tb_switch_tmu_rate_write(tb_switch_parent(sw), sw->tmu.rate);
else else
tb_switch_tmu_rate_write(sw, sw->tmu.rate); tb_switch_tmu_rate_write(sw, sw->tmu.rate);
...@@ -599,18 +596,18 @@ static void __tb_switch_tmu_change_mode_prev(struct tb_switch *sw) ...@@ -599,18 +596,18 @@ static void __tb_switch_tmu_change_mode_prev(struct tb_switch *sw)
static int __tb_switch_tmu_change_mode(struct tb_switch *sw) static int __tb_switch_tmu_change_mode(struct tb_switch *sw)
{ {
struct tb_switch *parent = tb_switch_parent(sw);
struct tb_port *up, *down; struct tb_port *up, *down;
int ret; int ret;
up = tb_upstream_port(sw); up = tb_upstream_port(sw);
down = tb_port_at(tb_route(sw), parent); down = tb_switch_downstream_port(sw);
ret = tb_port_tmu_set_unidirectional(down, sw->tmu.unidirectional_request); ret = tb_port_tmu_set_unidirectional(down, sw->tmu.unidirectional_request);
if (ret) if (ret)
goto out; goto out;
if (sw->tmu.unidirectional_request) if (sw->tmu.unidirectional_request)
ret = tb_switch_tmu_rate_write(parent, sw->tmu.rate_request); ret = tb_switch_tmu_rate_write(tb_switch_parent(sw),
sw->tmu.rate_request);
else else
ret = tb_switch_tmu_rate_write(sw, sw->tmu.rate_request); ret = tb_switch_tmu_rate_write(sw, sw->tmu.rate_request);
if (ret) if (ret)
......
...@@ -234,8 +234,8 @@ static bool link_is_usb4(struct tb_port *port) ...@@ -234,8 +234,8 @@ static bool link_is_usb4(struct tb_port *port)
*/ */
int usb4_switch_setup(struct tb_switch *sw) int usb4_switch_setup(struct tb_switch *sw)
{ {
struct tb_port *downstream_port; struct tb_switch *parent = tb_switch_parent(sw);
struct tb_switch *parent; struct tb_port *down;
bool tbt3, xhci; bool tbt3, xhci;
u32 val = 0; u32 val = 0;
int ret; int ret;
...@@ -249,9 +249,8 @@ int usb4_switch_setup(struct tb_switch *sw) ...@@ -249,9 +249,8 @@ int usb4_switch_setup(struct tb_switch *sw)
if (ret) if (ret)
return ret; return ret;
parent = tb_switch_parent(sw); down = tb_switch_downstream_port(sw);
downstream_port = tb_port_at(tb_route(sw), parent); sw->link_usb4 = link_is_usb4(down);
sw->link_usb4 = link_is_usb4(downstream_port);
tb_sw_dbg(sw, "link: %s\n", sw->link_usb4 ? "USB4" : "TBT"); tb_sw_dbg(sw, "link: %s\n", sw->link_usb4 ? "USB4" : "TBT");
xhci = val & ROUTER_CS_6_HCI; xhci = val & ROUTER_CS_6_HCI;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment