Skip to contentSkip to conversation

fpga: dfl: afu: harden port enable logic

PATCH Superseded Russ Weight 3 participants 8 messages Feb 2, 2021 – Feb 4, 2021

Subsystems (2)

v3 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 fpga: dfl: afu: harden port enable logic

Port enable is not complete until ACK = 0. Change __afu_port_enable() to guarantee that the enable process is complete by polling for ACK == 0.

---
v3:
- afu_port_err_clear() changed to prioritize port_enable failure over other a detected mismatch in port errors.
- reorganized code in port_reset() to be more readable.
v2:
- Fixed typo in commit message

---
 drivers/fpga/dfl-afu-error.c |  8 ++++----
 drivers/fpga/dfl-afu-main.c  | 31 ++++++++++++++++++++++---------
 drivers/fpga/dfl-afu.h       |  2 +-
 3 files changed, 27 insertions(+), 14 deletions(-)
drivers/fpga/dfl-afu-error.c +4 -4
index c4691187cca9..2ced610059cc 100644
@@ -52,7 +52,7 @@ static int afu_port_err_clear(struct device *dev, u64 err)
 	struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
 	struct platform_device *pdev = to_platform_device(dev);
 	void __iomem *base_err, *base_hdr;
-	int ret = -EBUSY;
+	int enable_ret = 0, ret = -EBUSY;
 	u64 v;
 
 	base_err = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_ERROR);
@@ -102,12 +102,12 @@ static int afu_port_err_clear(struct device *dev, u64 err)
 	/* Clear mask */
 	__afu_port_err_mask(dev, false);
 
-	/* Enable the Port by clear the reset */
-	__afu_port_enable(pdev);
+	/* Enable the Port by clearing the reset */
+	enable_ret = __afu_port_enable(pdev);
 
 done:
 	mutex_unlock(&pdata->lock);
-	return ret;
+	return enable_ret ? enable_ret : ret;
 }
 
 static ssize_t errors_show(struct device *dev, struct device_attribute *attr,
drivers/fpga/dfl-afu-main.c +22 -9
index 753cda4b2568..729eb306062e 100644
@@ -21,6 +21,9 @@
 
 #include "dfl-afu.h"
 
+#define RST_POLL_INVL 10 /* us */
+#define RST_POLL_TIMEOUT 1000 /* us */
+
 /**
  * __afu_port_enable - enable a port by clear reset
  * @pdev: port platform device.
@@ -32,7 +35,7 @@
  *
  * The caller needs to hold lock for protection.
  */
-void __afu_port_enable(struct platform_device *pdev)
+int __afu_port_enable(struct platform_device *pdev)
 {
 	struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
 	void __iomem *base;
@@ -41,7 +44,7 @@ void __afu_port_enable(struct platform_device *pdev)
 	WARN_ON(!pdata->disable_count);
 
 	if (--pdata->disable_count != 0)
-		return;
+		return 0;
 
 	base = dfl_get_feature_ioaddr_by_id(&pdev->dev, PORT_FEATURE_ID_HEADER);
 
@@ -49,10 +52,20 @@ void __afu_port_enable(struct platform_device *pdev)
 	v = readq(base + PORT_HDR_CTRL);
 	v &= ~PORT_CTRL_SFTRST;
 	writeq(v, base + PORT_HDR_CTRL);
-}
 
-#define RST_POLL_INVL 10 /* us */
-#define RST_POLL_TIMEOUT 1000 /* us */
+	/*
+	 * HW clears the ack bit to indicate that the port is fully out
+	 * of reset.
+	 */
+	if (readq_poll_timeout(base + PORT_HDR_CTRL, v,
+			       !(v & PORT_CTRL_SFTRST_ACK),
+			       RST_POLL_INVL, RST_POLL_TIMEOUT)) {
+		dev_err(&pdev->dev, "timeout, failure to enable device\n");
+		return -ETIMEDOUT;
+	}
+
+	return 0;
+}
 
 /**
  * __afu_port_disable - disable a port by hold reset
@@ -111,9 +124,9 @@ static int __port_reset(struct platform_device *pdev)
 
 	ret = __afu_port_disable(pdev);
 	if (!ret)
-		__afu_port_enable(pdev);
+		return ret;
 
-	return ret;
+	return __afu_port_enable(pdev);
 }
 
 static int port_reset(struct platform_device *pdev)
@@ -872,11 +885,11 @@ static int afu_dev_destroy(struct platform_device *pdev)
 static int port_enable_set(struct platform_device *pdev, bool enable)
 {
 	struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
-	int ret = 0;
+	int ret;
 
 	mutex_lock(&pdata->lock);
 	if (enable)
-		__afu_port_enable(pdev);
+		ret = __afu_port_enable(pdev);
 	else
 		ret = __afu_port_disable(pdev);
 	mutex_unlock(&pdata->lock);
drivers/fpga/dfl-afu.h +1 -1
index 576e94960086..e5020e2b1f3d 100644
@@ -80,7 +80,7 @@ struct dfl_afu {
 };
 
 /* hold pdata->lock when call __afu_port_enable/disable */
-void __afu_port_enable(struct platform_device *pdev);
+int __afu_port_enable(struct platform_device *pdev);
 int __afu_port_disable(struct platform_device *pdev);
 
 void afu_mmio_region_init(struct dfl_feature_platform_data *pdata);
Signature · 1 line
2.25.1
#1 lore raw
Quoted 38 lines

Subject: [PATCH v3 1/1] fpga: dfl: afu: harden port enable logic

Port enable is not complete until ACK = 0. Change __afu_port_enable() to guarantee that the enable process is complete by polling for ACK == 0.

---
v3:
- afu_port_err_clear() changed to prioritize port_enable failure over other a detected mismatch in port errors.
- reorganized code in port_reset() to be more readable.
v2:
- Fixed typo in commit message

---
 drivers/fpga/dfl-afu-error.c |  8 ++++----
 drivers/fpga/dfl-afu-main.c  | 31 ++++++++++++++++++++++---------
 drivers/fpga/dfl-afu.h       |  2 +-
 3 files changed, 27 insertions(+), 14 deletions(-)
drivers/fpga/dfl-afu-error.c +1 -1
index c4691187cca9..2ced610059cc 100644
@@ -52,7 +52,7 @@ static int afu_port_err_clear(struct device *dev, u64 err)
 	struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
 	struct platform_device *pdev = to_platform_device(dev);
 	void __iomem *base_err, *base_hdr;
-	int ret = -EBUSY;
+	int enable_ret = 0, ret = -EBUSY;
 	u64 v;
 
 	base_err = dfl_get_feature_ioaddr_by_id(dev,

PORT_FEATURE_ID_ERROR);

drivers/fpga/dfl-afu-error.c +0 -0
@@ -102,12 +102,12 @@ static int afu_port_err_clear(struct device *dev, u64
 err)
 	/* Clear mask */
 	__afu_port_err_mask(dev, false);

drivers/fpga/dfl-afu-error.c static int afu_port_err_clear(struct device *dev, u64 · line 111

Quoted 10 lines
drivers/fpga/dfl-afu-error.c +3 -3
 
-	/* Enable the Port by clear the reset */
-	__afu_port_enable(pdev);
+	/* Enable the Port by clearing the reset */
+	enable_ret = __afu_port_enable(pdev);
 
 done:
 	mutex_unlock(&pdata->lock);
-	return ret;
+	return enable_ret ? enable_ret : ret;

Maybe we should add some error message to notify user, there are more errors happened, as some ret value is not returned.

Quoted 43 lines
drivers/fpga/dfl-afu-error.c +0 -0
 }
 

static ssize_t errors_show(struct device *dev, struct device_attribute *attr,

drivers/fpga/dfl-afu-main.c +4 -1
index 753cda4b2568..729eb306062e 100644
@@ -21,6 +21,9 @@
 
 #include "dfl-afu.h"
 
+#define RST_POLL_INVL 10 /* us */
+#define RST_POLL_TIMEOUT 1000 /* us */
+
 /**
  * __afu_port_enable - enable a port by clear reset
  * @pdev: port platform device.
@@ -32,7 +35,7 @@
  *
  * The caller needs to hold lock for protection.
  */
-void __afu_port_enable(struct platform_device *pdev)
+int __afu_port_enable(struct platform_device *pdev)
 {
 	struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev-
 >dev);
drivers/fpga/dfl-afu-main.c +1 -2
 	void __iomem *base;
@@ -41,7 +44,7 @@ void __afu_port_enable(struct platform_device *pdev)
 	WARN_ON(!pdata->disable_count);
 
 	if (--pdata->disable_count != 0)
-		return;
+		return 0;
 
 	base = dfl_get_feature_ioaddr_by_id(&pdev->dev,
 PORT_FEATURE_ID_HEADER);
@@ -49,10 +52,20 @@ void __afu_port_enable(struct platform_device *pdev)
 	v = readq(base + PORT_HDR_CTRL);
 	v &= ~PORT_CTRL_SFTRST;
 	writeq(v, base + PORT_HDR_CTRL);
-}
 

drivers/fpga/dfl-afu-main.c void __afu_port_enable(struct platform_device *pdev) · line 63

Quoted 10 lines
drivers/fpga/dfl-afu-main.c +8 -2
-#define RST_POLL_INVL 10 /* us */
-#define RST_POLL_TIMEOUT 1000 /* us */
+	/*
+	 * HW clears the ack bit to indicate that the port is fully out
+	 * of reset.
+	 */
+	if (readq_poll_timeout(base + PORT_HDR_CTRL, v,
+			       !(v & PORT_CTRL_SFTRST_ACK),
+			       RST_POLL_INVL, RST_POLL_TIMEOUT)) {
+		dev_err(&pdev->dev, "timeout, failure to enable device\n");

Maybe we can change dev_err message in port disable to "disable device" as well. : )

Hao

Quoted 51 lines
drivers/fpga/dfl-afu-main.c +9 -4
+		return -ETIMEDOUT;
+	}
+
+	return 0;
+}
 
 /**
  * __afu_port_disable - disable a port by hold reset
@@ -111,9 +124,9 @@ static int __port_reset(struct platform_device *pdev)
 
 	ret = __afu_port_disable(pdev);
 	if (!ret)
-		__afu_port_enable(pdev);
+		return ret;
 
-	return ret;
+	return __afu_port_enable(pdev);
 }
 
 static int port_reset(struct platform_device *pdev)
@@ -872,11 +885,11 @@ static int afu_dev_destroy(struct platform_device
 *pdev)
 static int port_enable_set(struct platform_device *pdev, bool enable)
 {
 	struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev-
 >dev);
-	int ret = 0;
+	int ret;
 
 	mutex_lock(&pdata->lock);
 	if (enable)
-		__afu_port_enable(pdev);
+		ret = __afu_port_enable(pdev);
 	else
drivers/fpga/dfl-afu-main.c +0 -0
 		ret = __afu_port_disable(pdev);
 	mutex_unlock(&pdata->lock);
drivers/fpga/dfl-afu.h +1 -2
index 576e94960086..e5020e2b1f3d 100644
@@ -80,7 +80,7 @@ struct dfl_afu {
 };
 
 /* hold pdata->lock when call __afu_port_enable/disable */
-void __afu_port_enable(struct platform_device *pdev);
+int __afu_port_enable(struct platform_device *pdev);
 int __afu_port_disable(struct platform_device *pdev);
 
 void afu_mmio_region_init(struct dfl_feature_platform_data *pdata);
--

2.25.1

#2 lore raw
Quoted 44 lines · Hao
Quoted 44 lines

Subject: [PATCH v3 1/1] fpga: dfl: afu: harden port enable logic

Port enable is not complete until ACK = 0. Change __afu_port_enable() to guarantee that the enable process is complete by polling for ACK == 0.

---
v3:
- afu_port_err_clear() changed to prioritize port_enable failure over other a detected mismatch in port errors.
- reorganized code in port_reset() to be more readable.
v2:
- Fixed typo in commit message

---
 drivers/fpga/dfl-afu-error.c |  8 ++++----
 drivers/fpga/dfl-afu-main.c  | 31 ++++++++++++++++++++++---------
 drivers/fpga/dfl-afu.h       |  2 +-
 3 files changed, 27 insertions(+), 14 deletions(-)
drivers/fpga/dfl-afu-error.c +1 -1
index c4691187cca9..2ced610059cc 100644
@@ -52,7 +52,7 @@ static int afu_port_err_clear(struct device *dev, u64 err)
 struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
 struct platform_device *pdev = to_platform_device(dev);
 void __iomem *base_err, *base_hdr;
-int ret = -EBUSY;
+int enable_ret = 0, ret = -EBUSY;
 u64 v;
 
 base_err = dfl_get_feature_ioaddr_by_id(dev,

PORT_FEATURE_ID_ERROR);

drivers/fpga/dfl-afu-error.c +2 -2
@@ -102,12 +102,12 @@ static int afu_port_err_clear(struct device *dev, u64
 err)
 /* Clear mask */
 __afu_port_err_mask(dev, false);
 
-/* Enable the Port by clear the reset */
-__afu_port_enable(pdev);
+/* Enable the Port by clearing the reset */
+enable_ret = __afu_port_enable(pdev);
 
Quoted 6 lines

drivers/fpga/dfl-afu-error.c static int afu_port_err_clear(struct device *dev, u64 err) · line 110

Quoted 4 lines
drivers/fpga/dfl-afu-error.c +1 -1
 done:
 mutex_unlock(&pdata->lock);
-return ret;
+return enable_ret ? enable_ret : ret;

Maybe we should add some error message to notify user, there are more errors happened, as some ret value is not returned.

It is the -EINVAL error case that would get lost if there was a double error. This error indicates that the value written to sysfs by the user does not correspond to the current port errors. This is not a hardware error, and could even be a user error. Do you think a warning in the error log is needed here?

Quoted 44 lines
Quoted 44 lines
drivers/fpga/dfl-afu-error.c +0 -0
 }
 

static ssize_t errors_show(struct device *dev, struct device_attribute *attr,

drivers/fpga/dfl-afu-main.c +4 -1
index 753cda4b2568..729eb306062e 100644
@@ -21,6 +21,9 @@
 
 #include "dfl-afu.h"
 
+#define RST_POLL_INVL 10 /* us */
+#define RST_POLL_TIMEOUT 1000 /* us */
+
 /**
  * __afu_port_enable - enable a port by clear reset
  * @pdev: port platform device.
@@ -32,7 +35,7 @@
  *
  * The caller needs to hold lock for protection.
  */
-void __afu_port_enable(struct platform_device *pdev)
+int __afu_port_enable(struct platform_device *pdev)
 {
 struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev-
 > dev);

void __iomem *base;

drivers/fpga/dfl-afu-main.c +1 -2
@@ -41,7 +44,7 @@ void __afu_port_enable(struct platform_device *pdev)
 WARN_ON(!pdata->disable_count);
 
 if (--pdata->disable_count != 0)
-return;
+return 0;
 
 base = dfl_get_feature_ioaddr_by_id(&pdev->dev,
 PORT_FEATURE_ID_HEADER);
@@ -49,10 +52,20 @@ void __afu_port_enable(struct platform_device *pdev)
 v = readq(base + PORT_HDR_CTRL);
 v &= ~PORT_CTRL_SFTRST;
 writeq(v, base + PORT_HDR_CTRL);
-}
 
Quoted 11 lines

drivers/fpga/dfl-afu-main.c void __afu_port_enable(struct platform_device *pdev) · line 63

Quoted 10 lines
drivers/fpga/dfl-afu-main.c +8 -2
-#define RST_POLL_INVL 10 /* us */
-#define RST_POLL_TIMEOUT 1000 /* us */
+/*
+ * HW clears the ack bit to indicate that the port is fully out
+ * of reset.
+ */
+if (readq_poll_timeout(base + PORT_HDR_CTRL, v,
+       !(v & PORT_CTRL_SFTRST_ACK),
+       RST_POLL_INVL, RST_POLL_TIMEOUT)) {
+dev_err(&pdev->dev, "timeout, failure to enable device\n");

Maybe we can change dev_err message in port disable to "disable device" as well. : )

Thank you. I'll submit a new version of the patch with this fix.

- Russ

Quoted 54 lines

Hao

Quoted 51 lines
drivers/fpga/dfl-afu-main.c +9 -4
+return -ETIMEDOUT;
+}
+
+return 0;
+}
 
 /**
  * __afu_port_disable - disable a port by hold reset
@@ -111,9 +124,9 @@ static int __port_reset(struct platform_device *pdev)
 
 ret = __afu_port_disable(pdev);
 if (!ret)
-__afu_port_enable(pdev);
+return ret;
 
-return ret;
+return __afu_port_enable(pdev);
 }
 
 static int port_reset(struct platform_device *pdev)
@@ -872,11 +885,11 @@ static int afu_dev_destroy(struct platform_device
 *pdev)
 static int port_enable_set(struct platform_device *pdev, bool enable)
 {
 struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev-
 > dev);
-int ret = 0;
+int ret;
 
 mutex_lock(&pdata->lock);
 if (enable)
-__afu_port_enable(pdev);
+ret = __afu_port_enable(pdev);
 else

ret = __afu_port_disable(pdev);
mutex_unlock(&pdata->lock);

drivers/fpga/dfl-afu.h +1 -2
index 576e94960086..e5020e2b1f3d 100644
@@ -80,7 +80,7 @@ struct dfl_afu {
 };
 
 /* hold pdata->lock when call __afu_port_enable/disable */
-void __afu_port_enable(struct platform_device *pdev);
+int __afu_port_enable(struct platform_device *pdev);
 int __afu_port_disable(struct platform_device *pdev);
 
 void afu_mmio_region_init(struct dfl_feature_platform_data *pdata);
--

2.25.1

#3 lore raw
Quoted 51 lines · Hao
Quoted 32 lines
Quoted 32 lines

Subject: [PATCH v3 1/1] fpga: dfl: afu: harden port enable logic

Port enable is not complete until ACK = 0. Change __afu_port_enable() to guarantee that the enable process is complete by polling for ACK == 0.

---
v3:
- afu_port_err_clear() changed to prioritize port_enable failure over other a detected mismatch in port errors.
- reorganized code in port_reset() to be more readable.
v2:
- Fixed typo in commit message

---
 drivers/fpga/dfl-afu-error.c |  8 ++++----
 drivers/fpga/dfl-afu-main.c  | 31 ++++++++++++++++++++++---------
 drivers/fpga/dfl-afu.h       |  2 +-
 3 files changed, 27 insertions(+), 14 deletions(-)
drivers/fpga/dfl-afu-error.c +1 -1
index c4691187cca9..2ced610059cc 100644
@@ -52,7 +52,7 @@ static int afu_port_err_clear(struct device *dev, u64 err)
 struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
 struct platform_device *pdev = to_platform_device(dev);
 void __iomem *base_err, *base_hdr;
-int ret = -EBUSY;
+int enable_ret = 0, ret = -EBUSY;
 u64 v;
 
Quoted 3 lines

drivers/fpga/dfl-afu-error.c static int afu_port_err_clear(struct device *dev,

Quoted 3 lines
drivers/fpga/dfl-afu-error.c +0 -0
 base_err = dfl_get_feature_ioaddr_by_id(dev,

PORT_FEATURE_ID_ERROR);

drivers/fpga/dfl-afu-error.c +0 -0
@@ -102,12 +102,12 @@ static int afu_port_err_clear(struct device *dev,

u64

Quoted 9 lines
Quoted 9 lines
drivers/fpga/dfl-afu-error.c +2 -2
 err)
 /* Clear mask */
 __afu_port_err_mask(dev, false);
 
-/* Enable the Port by clear the reset */
-__afu_port_enable(pdev);
+/* Enable the Port by clearing the reset */
+enable_ret = __afu_port_enable(pdev);
 
Quoted 5 lines

drivers/fpga/dfl-afu-error.c static int afu_port_err_clear(struct device *dev, u64 err) · line 110

Quoted 4 lines
drivers/fpga/dfl-afu-error.c +1 -1
 done:
 mutex_unlock(&pdata->lock);
-return ret;
+return enable_ret ? enable_ret : ret;

Maybe we should add some error message to notify user, there are more

Quoted 6 lines

errors happened,

Quoted 1 line

as some ret value is not returned.

It is the -EINVAL error case that would get lost if there was a double error. This error indicates that the value written to sysfs by the user does not correspond to the current port errors. This is not a hardware error, and could even be a user error. Do you think a warning in the error log is needed here?

I think so, as there are actually two errors there, I feel it's better to let user know their input is not accepted too than just keeping silence, right? as this error should be triggered by user input?

Hao

Quoted 115 lines
Quoted 32 lines
Quoted 32 lines
drivers/fpga/dfl-afu-error.c +0 -0
 }
 

static ssize_t errors_show(struct device *dev, struct device_attribute *attr,

drivers/fpga/dfl-afu-main.c +4 -1
index 753cda4b2568..729eb306062e 100644
@@ -21,6 +21,9 @@
 
 #include "dfl-afu.h"
 
+#define RST_POLL_INVL 10 /* us */
+#define RST_POLL_TIMEOUT 1000 /* us */
+
 /**
  * __afu_port_enable - enable a port by clear reset
  * @pdev: port platform device.
@@ -32,7 +35,7 @@
  *
  * The caller needs to hold lock for protection.
  */
-void __afu_port_enable(struct platform_device *pdev)
+int __afu_port_enable(struct platform_device *pdev)
 {
 struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev-
 > dev);

void __iomem *base;

drivers/fpga/dfl-afu-main.c +0 -0
@@ -41,7 +44,7 @@ void __afu_port_enable(struct platform_device *pdev)
 WARN_ON(!pdata->disable_count);
 
Quoted 8 lines

drivers/fpga/dfl-afu-main.c void __afu_port_enable(struct platform_device

Quoted 8 lines
drivers/fpga/dfl-afu-main.c +1 -1
 if (--pdata->disable_count != 0)
-return;
+return 0;
 
 base = dfl_get_feature_ioaddr_by_id(&pdev->dev,
 PORT_FEATURE_ID_HEADER);
@@ -49,10 +52,20 @@ void __afu_port_enable(struct platform_device

*pdev)

Quoted 5 lines
Quoted 5 lines
drivers/fpga/dfl-afu-main.c +0 -1
 v = readq(base + PORT_HDR_CTRL);
 v &= ~PORT_CTRL_SFTRST;
 writeq(v, base + PORT_HDR_CTRL);
-}
 
Quoted 11 lines

drivers/fpga/dfl-afu-main.c void __afu_port_enable(struct platform_device *pdev) · line 63

Quoted 10 lines
drivers/fpga/dfl-afu-main.c +8 -2
-#define RST_POLL_INVL 10 /* us */
-#define RST_POLL_TIMEOUT 1000 /* us */
+/*
+ * HW clears the ack bit to indicate that the port is fully out
+ * of reset.
+ */
+if (readq_poll_timeout(base + PORT_HDR_CTRL, v,
+       !(v & PORT_CTRL_SFTRST_ACK),
+       RST_POLL_INVL, RST_POLL_TIMEOUT)) {
+dev_err(&pdev->dev, "timeout, failure to enable device\n");

Maybe we can change dev_err message in port disable to "disable device" as

well. : )
Thank you. I'll submit a new version of the patch with this fix.

- Russ

Quoted 54 lines

Hao

Quoted 51 lines
drivers/fpga/dfl-afu-main.c +9 -4
+return -ETIMEDOUT;
+}
+
+return 0;
+}
 
 /**
  * __afu_port_disable - disable a port by hold reset
@@ -111,9 +124,9 @@ static int __port_reset(struct platform_device *pdev)
 
 ret = __afu_port_disable(pdev);
 if (!ret)
-__afu_port_enable(pdev);
+return ret;
 
-return ret;
+return __afu_port_enable(pdev);
 }
 
 static int port_reset(struct platform_device *pdev)
@@ -872,11 +885,11 @@ static int afu_dev_destroy(struct platform_device
 *pdev)
 static int port_enable_set(struct platform_device *pdev, bool enable)
 {
 struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev-
 > dev);
-int ret = 0;
+int ret;
 
 mutex_lock(&pdata->lock);
 if (enable)
-__afu_port_enable(pdev);
+ret = __afu_port_enable(pdev);
 else

ret = __afu_port_disable(pdev);
mutex_unlock(&pdata->lock);

drivers/fpga/dfl-afu.h +1 -2
index 576e94960086..e5020e2b1f3d 100644
@@ -80,7 +80,7 @@ struct dfl_afu {
 };
 
 /* hold pdata->lock when call __afu_port_enable/disable */
-void __afu_port_enable(struct platform_device *pdev);
+int __afu_port_enable(struct platform_device *pdev);
 int __afu_port_disable(struct platform_device *pdev);
 
 void afu_mmio_region_init(struct dfl_feature_platform_data *pdata);
--

2.25.1

#4 lore raw
Quoted 52 lines · Hao
Quoted 52 lines · Hao
Quoted 32 lines
Quoted 32 lines

Subject: [PATCH v3 1/1] fpga: dfl: afu: harden port enable logic

Port enable is not complete until ACK = 0. Change __afu_port_enable() to guarantee that the enable process is complete by polling for ACK == 0.

---
v3:
- afu_port_err_clear() changed to prioritize port_enable failure over other a detected mismatch in port errors.
- reorganized code in port_reset() to be more readable.
v2:
- Fixed typo in commit message

---
 drivers/fpga/dfl-afu-error.c |  8 ++++----
 drivers/fpga/dfl-afu-main.c  | 31 ++++++++++++++++++++++---------
 drivers/fpga/dfl-afu.h       |  2 +-
 3 files changed, 27 insertions(+), 14 deletions(-)
drivers/fpga/dfl-afu-error.c +1 -1
index c4691187cca9..2ced610059cc 100644
@@ -52,7 +52,7 @@ static int afu_port_err_clear(struct device *dev, u64 err)
 struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
 struct platform_device *pdev = to_platform_device(dev);
 void __iomem *base_err, *base_hdr;
-int ret = -EBUSY;
+int enable_ret = 0, ret = -EBUSY;
 u64 v;
 
Quoted 3 lines

drivers/fpga/dfl-afu-error.c static int afu_port_err_clear(struct device *dev,

Quoted 3 lines
drivers/fpga/dfl-afu-error.c +0 -0
 base_err = dfl_get_feature_ioaddr_by_id(dev,

PORT_FEATURE_ID_ERROR);

drivers/fpga/dfl-afu-error.c +0 -0
@@ -102,12 +102,12 @@ static int afu_port_err_clear(struct device *dev,

u64

Quoted 9 lines
Quoted 9 lines
drivers/fpga/dfl-afu-error.c +2 -2
 err)
 /* Clear mask */
 __afu_port_err_mask(dev, false);
 
-/* Enable the Port by clear the reset */
-__afu_port_enable(pdev);
+/* Enable the Port by clearing the reset */
+enable_ret = __afu_port_enable(pdev);
 
Quoted 5 lines

drivers/fpga/dfl-afu-error.c static int afu_port_err_clear(struct device *dev, u64 err) · line 110

Quoted 4 lines
drivers/fpga/dfl-afu-error.c +1 -1
 done:
 mutex_unlock(&pdata->lock);
-return ret;
+return enable_ret ? enable_ret : ret;

Maybe we should add some error message to notify user, there are more

errors happened,

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

as some ret value is not returned.

It is the -EINVAL error case that would get lost if there was a double error. This error indicates that the value written to sysfs by the user does not correspond to the current port errors. This is not a hardware error, and could even be a user error. Do you think a warning in the error log is needed here?

I think so, as there are actually two errors there, I feel it's better to let user know their input is not accepted too than just keeping silence, right? as this error should be triggered by user input?

In the single error case, the EINVAL would be returned to the user-space caller. That is the common case. In the double error case, the EINVAL would be lost. Either way, it is helpful to know what the expected/actual values are. I have added a warning message to the kernel log for the next version of the patch which also shows the expected/actual values.

- Russ

Quoted 115 lines

Hao

Quoted 112 lines
Quoted 30 lines
Quoted 30 lines
drivers/fpga/dfl-afu-error.c +0 -0
 }
 

static ssize_t errors_show(struct device *dev, struct device_attribute *attr,

drivers/fpga/dfl-afu-main.c +4 -1
index 753cda4b2568..729eb306062e 100644
@@ -21,6 +21,9 @@
 
 #include "dfl-afu.h"
 
+#define RST_POLL_INVL 10 /* us */
+#define RST_POLL_TIMEOUT 1000 /* us */
+
 /**
  * __afu_port_enable - enable a port by clear reset
  * @pdev: port platform device.
@@ -32,7 +35,7 @@
  *
  * The caller needs to hold lock for protection.
  */
-void __afu_port_enable(struct platform_device *pdev)
+int __afu_port_enable(struct platform_device *pdev)
 {
 struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev-
 > dev);

void __iomem *base;

drivers/fpga/dfl-afu-main.c +0 -0
@@ -41,7 +44,7 @@ void __afu_port_enable(struct platform_device *pdev)
 WARN_ON(!pdata->disable_count);
 
Quoted 8 lines

drivers/fpga/dfl-afu-main.c void __afu_port_enable(struct platform_device

Quoted 8 lines
drivers/fpga/dfl-afu-main.c +1 -1
 if (--pdata->disable_count != 0)
-return;
+return 0;
 
 base = dfl_get_feature_ioaddr_by_id(&pdev->dev,
 PORT_FEATURE_ID_HEADER);
@@ -49,10 +52,20 @@ void __afu_port_enable(struct platform_device

*pdev)

Quoted 5 lines
Quoted 5 lines
drivers/fpga/dfl-afu-main.c +0 -1
 v = readq(base + PORT_HDR_CTRL);
 v &= ~PORT_CTRL_SFTRST;
 writeq(v, base + PORT_HDR_CTRL);
-}
 
Quoted 11 lines

drivers/fpga/dfl-afu-main.c void __afu_port_enable(struct platform_device *pdev) · line 63

Quoted 10 lines
drivers/fpga/dfl-afu-main.c +8 -2
-#define RST_POLL_INVL 10 /* us */
-#define RST_POLL_TIMEOUT 1000 /* us */
+/*
+ * HW clears the ack bit to indicate that the port is fully out
+ * of reset.
+ */
+if (readq_poll_timeout(base + PORT_HDR_CTRL, v,
+       !(v & PORT_CTRL_SFTRST_ACK),
+       RST_POLL_INVL, RST_POLL_TIMEOUT)) {
+dev_err(&pdev->dev, "timeout, failure to enable device\n");

Maybe we can change dev_err message in port disable to "disable device" as

well. : )
Thank you. I'll submit a new version of the patch with this fix.

- Russ

Quoted 53 lines

Hao

Quoted 51 lines
drivers/fpga/dfl-afu-main.c +9 -4
+return -ETIMEDOUT;
+}
+
+return 0;
+}
 
 /**
  * __afu_port_disable - disable a port by hold reset
@@ -111,9 +124,9 @@ static int __port_reset(struct platform_device *pdev)
 
 ret = __afu_port_disable(pdev);
 if (!ret)
-__afu_port_enable(pdev);
+return ret;
 
-return ret;
+return __afu_port_enable(pdev);
 }
 
 static int port_reset(struct platform_device *pdev)
@@ -872,11 +885,11 @@ static int afu_dev_destroy(struct platform_device
 *pdev)
 static int port_enable_set(struct platform_device *pdev, bool enable)
 {
 struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev-
 > dev);
-int ret = 0;
+int ret;
 
 mutex_lock(&pdata->lock);
 if (enable)
-__afu_port_enable(pdev);
+ret = __afu_port_enable(pdev);
 else

ret = __afu_port_disable(pdev);
mutex_unlock(&pdata->lock);

drivers/fpga/dfl-afu.h +1 -2
index 576e94960086..e5020e2b1f3d 100644
@@ -80,7 +80,7 @@ struct dfl_afu {
 };
 
 /* hold pdata->lock when call __afu_port_enable/disable */
-void __afu_port_enable(struct platform_device *pdev);
+int __afu_port_enable(struct platform_device *pdev);
 int __afu_port_disable(struct platform_device *pdev);
 
 void afu_mmio_region_init(struct dfl_feature_platform_data *pdata);
--

2.25.1

reviewer
#5 lore raw
In reply to Russ Weight

..snip..

drivers/fpga/dfl-afu.h struct dfl_afu { · line 85

Quoted 12 lines · Russ Weight
drivers/fpga/dfl-afu.h +1 -1
index 576e94960086..e5020e2b1f3d 100644
@@ -80,7 +80,7 @@ struct dfl_afu {
 };
 
 /* hold pdata->lock when call __afu_port_enable/disable */
-void __afu_port_enable(struct platform_device *pdev);
+int __afu_port_enable(struct platform_device *pdev);
 int __afu_port_disable(struct platform_device *pdev);
 

Should the '__' prefix be removed from __afu_port* ?

This would make the function names consistent with the other decls

Tom

Quoted 1 line
drivers/fpga/dfl-afu.h +0 -0
 void afu_mmio_region_init(struct dfl_feature_platform_data *pdata);
#6 lore raw
Quoted 10 lines · Tom Rix

..snip..

Quoted 7 lines · Russ Weight
drivers/fpga/dfl-afu.h +0 -0
index 576e94960086..e5020e2b1f3d 100644
@@ -80,7 +80,7 @@ struct dfl_afu {
 };
 
Quoted 8 lines

drivers/fpga/dfl-afu.h struct dfl_afu { · line 85

Quoted 5 lines
drivers/fpga/dfl-afu.h +1 -1
 /* hold pdata->lock when call __afu_port_enable/disable */
-void __afu_port_enable(struct platform_device *pdev);
+int __afu_port_enable(struct platform_device *pdev);
 int __afu_port_disable(struct platform_device *pdev);
 

Should the '__' prefix be removed from __afu_port* ?

This would make the function names consistent with the other decls

The '__' prefix is used here to help highlight the fact that these functions go not manage the locking themselves and must be called while holding the port mutex. There are additional functions, such as__port_reset(), that are following this same convention. I think these are OK as they are.

- Russ

Quoted 4 lines

Tom

Quoted 1 line
drivers/fpga/dfl-afu.h +0 -0
 void afu_mmio_region_init(struct dfl_feature_platform_data *pdata);
reviewer Reviewed-by
#7 lore raw
Quoted 19 lines · Russ Weight
Quoted 17 lines · Tom Rix

..snip..

drivers/fpga/dfl-afu.h struct dfl_afu { · line 85

Quoted 12 lines · Russ Weight
drivers/fpga/dfl-afu.h +1 -1
index 576e94960086..e5020e2b1f3d 100644
@@ -80,7 +80,7 @@ struct dfl_afu {
 };
 
 /* hold pdata->lock when call __afu_port_enable/disable */
-void __afu_port_enable(struct platform_device *pdev);
+int __afu_port_enable(struct platform_device *pdev);
 int __afu_port_disable(struct platform_device *pdev);
 

Should the '__' prefix be removed from __afu_port* ?

Quoted 5 lines
Quoted 1 line

This would make the function names consistent with the other decls

The '__' prefix is used here to help highlight the fact that these functions go not manage the locking themselves and must be called while holding the port mutex. There are additional functions, such as__port_reset(), that are following this same convention. I think these are OK as they are.

ok

Tom

Quoted 6 lines

- Russ

Quoted 3 lines

Tom

Quoted 1 line
drivers/fpga/dfl-afu.h +0 -0
 void afu_mmio_region_init(struct dfl_feature_platform_data *pdata);