Skip to content

Commit 47fec71

Browse files
djbwdavejiang
authored andcommitted
cxl/port: Cleanup handling of the nr_dports 0 -> 1 transition
There are multiple setup actions that can occur for a switch port after it is known that it has at least one active downstream link. That work is currently split between __devm_cxl_add_dport(), the add_dport() helper, and cxl_port_add_dport() where decoder setup occurs. Clean this up by moving all @DPORT object setup responsibilities into add_dport() and all port effects into cxl_port_add_dport(). add_dport() handles taking a reference on @dport->dport_dev, and cxl_port_add_dport() grows the awareness to setup the port component registers. This removes an awkward open-coded xa_erase() from the middle of __devm_cxl_add_dport() and instead tasks cxl_port_add_dport() with calling the common @DPORT destruction path if anything goes wrong. After this @port->nr_dports is always the count of @dports in the @port->dports xarray, and cxl_dport_remove() is symmetric with add_dport(). With ->nr_dports now reliably tracking the number of dports the use of ida_is_empty() can be dropped. Recall that the ida is only cleared on "release" of decoder objects, and release can be arbitrarily delayed past unregistration. Lastly port->component_reg_phys is no longer reset to CXL_RESOURCE_NONE post setup, no reason is seen to carry that forward. Reviewed-by: Dave Jiang <dave.jiang@intel.com> Reviewed-by: Jonathan Cameron <jonathan.cameron@huawei.com> Tested-by: Terry Bowman <terry.bowman@amd.com> Signed-off-by: Dan Williams <dan.j.williams@intel.com> Link: https://patch.msgid.link/20260131000403.2135324-2-dan.j.williams@intel.com Signed-off-by: Dave Jiang <dave.jiang@intel.com>
1 parent 9a8920c commit 47fec71

1 file changed

Lines changed: 15 additions & 16 deletions

File tree

drivers/cxl/core/port.c

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,11 +1066,15 @@ static int add_dport(struct cxl_port *port, struct cxl_dport *dport)
10661066
return -EBUSY;
10671067
}
10681068

1069+
/* Arrange for dport_dev to be valid through remove_dport() */
1070+
struct device *dev __free(put_device) = get_device(dport->dport_dev);
1071+
10691072
rc = xa_insert(&port->dports, (unsigned long)dport->dport_dev, dport,
10701073
GFP_KERNEL);
10711074
if (rc)
10721075
return rc;
10731076

1077+
retain_and_null_ptr(dev);
10741078
port->nr_dports++;
10751079
return 0;
10761080
}
@@ -1099,6 +1103,7 @@ static void cxl_dport_remove(void *data)
10991103
struct cxl_dport *dport = data;
11001104
struct cxl_port *port = dport->port;
11011105

1106+
port->nr_dports--;
11021107
xa_erase(&port->dports, (unsigned long) dport->dport_dev);
11031108
put_device(dport->dport_dev);
11041109
}
@@ -1181,21 +1186,6 @@ __devm_cxl_add_dport(struct cxl_port *port, struct device *dport_dev,
11811186
if (rc)
11821187
return ERR_PTR(rc);
11831188

1184-
/*
1185-
* Setup port register if this is the first dport showed up. Having
1186-
* a dport also means that there is at least 1 active link.
1187-
*/
1188-
if (port->nr_dports == 1 &&
1189-
port->component_reg_phys != CXL_RESOURCE_NONE) {
1190-
rc = cxl_port_setup_regs(port, port->component_reg_phys);
1191-
if (rc) {
1192-
xa_erase(&port->dports, (unsigned long)dport->dport_dev);
1193-
return ERR_PTR(rc);
1194-
}
1195-
port->component_reg_phys = CXL_RESOURCE_NONE;
1196-
}
1197-
1198-
get_device(dport_dev);
11991189
rc = devm_add_action_or_reset(host, cxl_dport_remove, dport);
12001190
if (rc)
12011191
return ERR_PTR(rc);
@@ -1622,7 +1612,16 @@ static struct cxl_dport *cxl_port_add_dport(struct cxl_port *port,
16221612

16231613
cxl_switch_parse_cdat(new_dport);
16241614

1625-
if (ida_is_empty(&port->decoder_ida)) {
1615+
if (port->nr_dports == 1) {
1616+
/*
1617+
* Some host bridges are known to not have component regsisters
1618+
* available until a root port has trained CXL. Perform that
1619+
* setup now.
1620+
*/
1621+
rc = cxl_port_setup_regs(port, port->component_reg_phys);
1622+
if (rc)
1623+
return ERR_PTR(rc);
1624+
16261625
rc = devm_cxl_switch_port_decoders_setup(port);
16271626
if (rc)
16281627
return ERR_PTR(rc);

0 commit comments

Comments
 (0)