mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Shubhrajyoti Datta <shubhrajyoti.datta@amd.com>
To: <linux-kernel@vger.kernel.org>, <linux-edac@vger.kernel.org>
Cc: <git@amd.com>, <shubhrajyoti.datta@gmail.com>,
	Radhey Shyam Pandey <radhey.shyam.pandey@amd.com>,
	Borislav Petkov <bp@alien8.de>, Tony Luck <tony.luck@intel.com>,
	Sai Krishna Potthuri <sai.krishna.potthuri@amd.com>,
	Shubhrajyoti Datta <shubhrajyoti.datta@amd.com>
Subject: [PATCH v3 6/7] EDAC/versalnet: fix the remove controller crash
Date: Fri, 27 Nov 2026 21:59:15 +0530	[thread overview]
Message-ID: <20261127163853.3563991-7-shubhrajyoti.datta@amd.com> (raw)
In-Reply-To: <20261127163853.3563991-1-shubhrajyoti.datta@amd.com>

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@amd.com>
---

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@amd.com/

 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


  parent reply	other threads:[~2026-11-27 16:39 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27 16:29 [PATCH v3 0/7] EDAC/versalnet: Fix error handling, teardown, and robustness Shubhrajyoti Datta
2026-11-27 16:29 ` [PATCH v3 1/7] EDAC/versalnet: Add bounds validation in rpmsg_cb() Shubhrajyoti Datta
2026-11-27 16:29 ` [PATCH v3 2/7] EDAC/versalnet: Make dwidth per-controller Shubhrajyoti Datta
2026-11-27 16:29 ` [PATCH v3 3/7] EDAC/versalnet: Wait for RPMsg endpoint initialization Shubhrajyoti Datta
2026-11-27 16:51   ` Shubhrajyoti Datta
2026-11-27 16:29 ` [PATCH v3 4/7] EDAC/versalnet: Guard against NULL drvdata in rpmsg_cb() Shubhrajyoti Datta
2026-11-27 16:29 ` [PATCH v3 5/7] EDAC/versalnet: Make get_ddr_config() return errors Shubhrajyoti Datta
2026-11-27 16:29 ` Shubhrajyoti Datta [this message]
2026-11-27 16:29 ` [PATCH v3 7/7] EDAC/versalnet: Move platform_set_drvdata() to mc_probe() Shubhrajyoti Datta

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261127163853.3563991-7-shubhrajyoti.datta@amd.com \
    --to=shubhrajyoti.datta@amd.com \
    --cc=bp@alien8.de \
    --cc=git@amd.com \
    --cc=linux-edac@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=radhey.shyam.pandey@amd.com \
    --cc=sai.krishna.potthuri@amd.com \
    --cc=shubhrajyoti.datta@gmail.com \
    --cc=tony.luck@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®