[PATCH v3 6/7] EDAC/versalnet: fix the remove controller crash
From: Shubhrajyoti Datta
Date: Fri Nov 27 2026 - 11:39:35 EST
Use the number of enabled controllers detected instead of registering
all the controllers. The enabled controllers are contiguous. Add a
num_ctrl field to mc_priv and use it in probe, init, remove,
error handling, and the rpmsg callback, allowing the driver to
support platforms with varying numbers of memory controllers enabled.
This will avoid we attempting to remove a controller that is not
enabled.
Fixes: d5fe2fec6c40 ("EDAC: Add a driver for the AMD Versal NET DDR controller")
Signed-off-by: Shubhrajyoti Datta <shubhrajyoti.datta@xxxxxxx>
---
Changes in v3:
Implement the detection instead of reading from device tree
update the numctrl check
implement get_dtype
Changes in v2:
Adding the device tree parameter was suggested in the review in v1
https://lore.kernel.org/all/34ffba5a-8e9a-49b9-9e36-c31d0e4369f8@xxxxxxx/
drivers/edac/versalnet_edac.c | 49 +++++++++++++++++++++++++----------
1 file changed, 36 insertions(+), 13 deletions(-)
diff --git a/drivers/edac/versalnet_edac.c b/drivers/edac/versalnet_edac.c
index 7d8b8f2ecaea..8a5da8fb3403 100644
--- a/drivers/edac/versalnet_edac.c
+++ b/drivers/edac/versalnet_edac.c
@@ -140,6 +140,7 @@ struct ecc_status {
* struct mc_priv - DDR memory controller private instance data.
* @message: Buffer for framing the event specific info.
* @stat: ECC status information.
+ * @num_ctrl: Number of DDR memory controllers.
* @error_id: The error id.
* @error_level: The error level.
* @dwidth: Per-controller width of data bus excluding ECC bits.
@@ -161,6 +162,7 @@ struct ecc_status {
struct mc_priv {
char message[256];
struct ecc_status stat;
+ u32 num_ctrl;
u32 error_id;
u32 error_level;
enum dev_type dwidth[NUM_CONTROLLERS];
@@ -449,7 +451,7 @@ static void handle_error(struct mc_priv *priv, struct ecc_status *stat,
phys_addr_t pfn;
int err;
- if (WARN_ON_ONCE(ctl_num >= NUM_CONTROLLERS))
+ if (WARN_ON_ONCE(ctl_num >= priv->num_ctrl))
return;
mci = priv->mci[ctl_num];
@@ -720,7 +722,7 @@ static int rpmsg_cb(struct rpmsg_device *rpdev, void *data,
p = &mc_priv->stat;
memset(p, 0, sizeof(struct ecc_status));
p->error_type = MC5_ERR_TYPE_CE;
- for (i = 0 ; i < NUM_CONTROLLERS; i++) {
+ for (i = 0 ; i < mc_priv->num_ctrl; i++) {
if (get_ddr_info(&mc_priv->regs[i * REGS_PER_CONTROLLER], mc_priv)) {
adec_data = mc_priv->adec + ADEC_NUM * i;
handle_error(mc_priv, &mc_priv->stat, i, adec_data);
@@ -731,7 +733,7 @@ static int rpmsg_cb(struct rpmsg_device *rpdev, void *data,
p = &mc_priv->stat;
memset(p, 0, sizeof(struct ecc_status));
p->error_type = MC5_ERR_TYPE_UE;
- for (i = 0 ; i < NUM_CONTROLLERS; i++) {
+ for (i = 0 ; i < mc_priv->num_ctrl; i++) {
if (get_ddr_info(&mc_priv->regs[i * REGS_PER_CONTROLLER], mc_priv)) {
adec_data = mc_priv->adec + ADEC_NUM * i;
handle_error(mc_priv, &mc_priv->stat, i, adec_data);
@@ -874,18 +876,12 @@ static void remove_one_mc(struct mc_priv *priv, int i)
device_unregister(dev);
}
-static int init_one_mc(struct mc_priv *priv, struct platform_device *pdev, int i)
+static enum dev_type get_dtype(struct mc_priv *priv, int i)
{
- u32 num_chans, rank, dwidth, config;
- struct edac_mc_layer layers[2];
- struct mem_ctl_info *mci;
- struct device *dev;
+ u32 dwidth, config;
enum dev_type dt;
- int rc;
config = priv->adec[CONF + i * ADEC_NUM];
- num_chans = FIELD_GET(MC5_NUM_CHANS_MASK, config);
- rank = 1 << FIELD_GET(MC5_RANK_MASK, config);
dwidth = FIELD_GET(MC5_BUS_WIDTH_MASK, config);
switch (dwidth) {
@@ -902,6 +898,23 @@ static int init_one_mc(struct mc_priv *priv, struct platform_device *pdev, int i
dt = DEV_UNKNOWN;
}
+ return dt;
+}
+
+static int init_one_mc(struct mc_priv *priv, struct platform_device *pdev, int i)
+{
+ struct edac_mc_layer layers[2];
+ u32 num_chans, rank, config;
+ struct mem_ctl_info *mci;
+ struct device *dev;
+ enum dev_type dt;
+ int rc;
+
+ config = priv->adec[CONF + i * ADEC_NUM];
+ num_chans = FIELD_GET(MC5_NUM_CHANS_MASK, config);
+ rank = 1 << FIELD_GET(MC5_RANK_MASK, config);
+
+ dt = get_dtype(priv, i);
if (dt == DEV_UNKNOWN)
return 0;
@@ -966,7 +979,7 @@ static int init_versalnet(struct mc_priv *priv, struct platform_device *pdev)
{
int rc, i;
- for (i = 0; i < NUM_CONTROLLERS; i++) {
+ for (i = 0; i < priv->num_ctrl; i++) {
rc = init_one_mc(priv, pdev, i);
if (rc) {
while (i--)
@@ -980,7 +993,7 @@ static int init_versalnet(struct mc_priv *priv, struct platform_device *pdev)
static void remove_versalnet(struct mc_priv *priv)
{
- for (int i = 0; i < NUM_CONTROLLERS; i++)
+ for (int i = 0; i < priv->num_ctrl; i++)
remove_one_mc(priv, i);
}
@@ -989,6 +1002,7 @@ static int mc_probe(struct platform_device *pdev)
struct device_node *r5_core_node;
struct mc_priv *priv;
struct rproc *rp;
+ enum dev_type dt;
int rc, i;
r5_core_node = of_parse_phandle(pdev->dev.of_node, "amd,rproc", 0);
@@ -1042,6 +1056,15 @@ static int mc_probe(struct platform_device *pdev)
dev_err(&pdev->dev, "Failed to get DDR configuration\n");
goto err_init;
}
+ dt = get_dtype(priv, i);
+ if (dt != DEV_UNKNOWN)
+ priv->num_ctrl++;
+ }
+
+ if (!priv->num_ctrl) {
+ dev_err(&pdev->dev, "No DDR controllers enabled\n");
+ rc = -ENODEV;
+ goto err_init;
}
rc = init_versalnet(priv, pdev);
--
2.34.1