linux-fpga.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/3] fpga: Use standard class dev_release function
@ 2021-05-24 18:50 Russ Weight
  2021-05-24 18:50 ` [PATCH v3 1/3] fpga: mgr: Use standard dev_release for class driver Russ Weight
                   ` (3 more replies)
  0 siblings, 4 replies; 17+ messages in thread
From: Russ Weight @ 2021-05-24 18:50 UTC (permalink / raw)
  To: mdf, linux-fpga
  Cc: trix, lgoncalv, yilun.xu, hao.wu, matthew.gerlach, richard.gong,
	Russ Weight

The FPGA framework has a convention of using managed resource
functions to allow parent drivers to manage the data structures
allocated by the class drivers. They use an empty *_dev_release()
function to satisfy the class driver.

This is inconsistent with linux driver model.

These changes remove the managed resource functions and populate
the class dev_release callback functions. They also merge the
create and register functions into a single register function for
each of the fpga-mgr, fpga-region, and fpga-bridge class drivers.

For more context, refer to this email thread:

https://marc.info/?l=linux-fpga&m=162127412218557&w=2

I turned on the configs assocated with each of the modified files,
but I must have been missing some dependencies, because not all
of them compiled. I did a run-time test specifically with the
dfl-fme infrastructure. This would have exercised the region,
bridge, and fpga-mgr frameworks.

Changelog v2 -> v3:
  - Cleaned up comment headers for fpga_mgr_register(), fpga_bridge_register(),
    and fpga_region_register().
  - Fixed error return on ida_simple_get() failure for fpga_mgr_register(),
    fpga_bridge_register(), and fpga_region_register().
  - Fixed error return value for fpga_bridge_register(): ERR_PTR(ret) instead
    of NULL.

Changelog v1 -> v2:
  - Restored devm_fpga_mgr_register() functionality to the fpga-mgr
    class driver, adapted for the combined create/register functionality.
  - All previous callers of devm_fpga_mgr_register() will continue to call
    devm_fpga_mgr_register().
  - replaced unnecessary ternary operators in return statements with
    standard if conditions.

Russ Weight (3):
  fpga: mgr: Use standard dev_release for class driver
  fpga: bridge: Use standard dev_release for class driver
  fpga: region: Use standard dev_release for class driver

 drivers/fpga/altera-cvp.c           |  12 +-
 drivers/fpga/altera-fpga2sdram.c    |  12 +-
 drivers/fpga/altera-freeze-bridge.c |  10 +-
 drivers/fpga/altera-hps2fpga.c      |  12 +-
 drivers/fpga/altera-pr-ip-core.c    |   8 +-
 drivers/fpga/altera-ps-spi.c        |  10 +-
 drivers/fpga/dfl-fme-br.c           |  10 +-
 drivers/fpga/dfl-fme-mgr.c          |  10 +-
 drivers/fpga/dfl-fme-region.c       |  10 +-
 drivers/fpga/dfl.c                  |  10 +-
 drivers/fpga/fpga-bridge.c          | 119 +++++---------------
 drivers/fpga/fpga-mgr.c             | 163 +++++++---------------------
 drivers/fpga/fpga-region.c          | 103 ++++--------------
 drivers/fpga/ice40-spi.c            |  10 +-
 drivers/fpga/machxo2-spi.c          |  10 +-
 drivers/fpga/of-fpga-region.c       |  10 +-
 drivers/fpga/socfpga-a10.c          |  16 +--
 drivers/fpga/socfpga.c              |  10 +-
 drivers/fpga/stratix10-soc.c        |  15 +--
 drivers/fpga/ts73xx-fpga.c          |  10 +-
 drivers/fpga/xilinx-pr-decoupler.c  |  17 +--
 drivers/fpga/xilinx-spi.c           |  12 +-
 drivers/fpga/zynq-fpga.c            |  16 +--
 drivers/fpga/zynqmp-fpga.c          |  10 +-
 include/linux/fpga/fpga-bridge.h    |  12 +-
 include/linux/fpga/fpga-mgr.h       |  17 ++-
 include/linux/fpga/fpga-region.h    |  12 +-
 27 files changed, 196 insertions(+), 470 deletions(-)

-- 
2.25.1


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH v3 1/3] fpga: mgr: Use standard dev_release for class driver
  2021-05-24 18:50 [PATCH v3 0/3] fpga: Use standard class dev_release function Russ Weight
@ 2021-05-24 18:50 ` Russ Weight
  2021-05-24 20:16   ` Tom Rix
                     ` (2 more replies)
  2021-05-24 18:50 ` [PATCH v3 2/3] fpga: bridge: " Russ Weight
                   ` (2 subsequent siblings)
  3 siblings, 3 replies; 17+ messages in thread
From: Russ Weight @ 2021-05-24 18:50 UTC (permalink / raw)
  To: mdf, linux-fpga
  Cc: trix, lgoncalv, yilun.xu, hao.wu, matthew.gerlach, richard.gong,
	Russ Weight

The FPGA manager class driver data structure is being treated as a
managed resource instead of using standard dev_release call-back
to release the class data structure. This change removes the
managed resource code for the freeing of the class data structure
and combines the create() and register() functions into a single
register() function.

The devm_fpga_mgr_register() function is retained.

Signed-off-by: Russ Weight <russell.h.weight@intel.com>
---
v3:
  - Cleaned up comment header for fpga_mgr_register()
  - Fix error return on ida_simple_get() failure
v2:
  - Restored devm_fpga_mgr_register() functionality, adapted for the combined
    create/register functionality.
  - All previous callers of devm_fpga_mgr_register() will continue to call
    devm_fpga_mgr_register().
  - replaced unnecessary ternary operators in return statements with standard
    if conditions.
---
 drivers/fpga/altera-cvp.c        |  12 +--
 drivers/fpga/altera-pr-ip-core.c |   8 +-
 drivers/fpga/altera-ps-spi.c     |  10 +-
 drivers/fpga/dfl-fme-mgr.c       |  10 +-
 drivers/fpga/fpga-mgr.c          | 163 ++++++++-----------------------
 drivers/fpga/ice40-spi.c         |  10 +-
 drivers/fpga/machxo2-spi.c       |  10 +-
 drivers/fpga/socfpga-a10.c       |  16 ++-
 drivers/fpga/socfpga.c           |  10 +-
 drivers/fpga/stratix10-soc.c     |  15 +--
 drivers/fpga/ts73xx-fpga.c       |  10 +-
 drivers/fpga/xilinx-spi.c        |  12 +--
 drivers/fpga/zynq-fpga.c         |  16 ++-
 drivers/fpga/zynqmp-fpga.c       |  10 +-
 include/linux/fpga/fpga-mgr.h    |  17 ++--
 15 files changed, 112 insertions(+), 217 deletions(-)

diff --git a/drivers/fpga/altera-cvp.c b/drivers/fpga/altera-cvp.c
index 4e0edb60bfba..b61020ceb0a1 100644
--- a/drivers/fpga/altera-cvp.c
+++ b/drivers/fpga/altera-cvp.c
@@ -652,19 +652,15 @@ static int altera_cvp_probe(struct pci_dev *pdev,
 	snprintf(conf->mgr_name, sizeof(conf->mgr_name), "%s @%s",
 		 ALTERA_CVP_MGR_NAME, pci_name(pdev));
 
-	mgr = devm_fpga_mgr_create(&pdev->dev, conf->mgr_name,
-				   &altera_cvp_ops, conf);
-	if (!mgr) {
-		ret = -ENOMEM;
+	mgr = fpga_mgr_register(&pdev->dev, conf->mgr_name,
+				&altera_cvp_ops, conf);
+	if (IS_ERR(mgr)) {
+		ret = PTR_ERR(mgr);
 		goto err_unmap;
 	}
 
 	pci_set_drvdata(pdev, mgr);
 
-	ret = fpga_mgr_register(mgr);
-	if (ret)
-		goto err_unmap;
-
 	return 0;
 
 err_unmap:
diff --git a/drivers/fpga/altera-pr-ip-core.c b/drivers/fpga/altera-pr-ip-core.c
index 5b130c4d9882..0980e134cc43 100644
--- a/drivers/fpga/altera-pr-ip-core.c
+++ b/drivers/fpga/altera-pr-ip-core.c
@@ -191,11 +191,11 @@ int alt_pr_register(struct device *dev, void __iomem *reg_base)
 		(val & ALT_PR_CSR_STATUS_MSK) >> ALT_PR_CSR_STATUS_SFT,
 		(int)(val & ALT_PR_CSR_PR_START));
 
-	mgr = devm_fpga_mgr_create(dev, dev_name(dev), &alt_pr_ops, priv);
-	if (!mgr)
-		return -ENOMEM;
+	mgr = devm_fpga_mgr_register(dev, dev_name(dev), &alt_pr_ops, priv);
+	if (IS_ERR(mgr))
+		return PTR_ERR(mgr);
 
-	return devm_fpga_mgr_register(dev, mgr);
+	return 0;
 }
 EXPORT_SYMBOL_GPL(alt_pr_register);
 
diff --git a/drivers/fpga/altera-ps-spi.c b/drivers/fpga/altera-ps-spi.c
index 23bfd4d1ad0f..d3f77b0312b2 100644
--- a/drivers/fpga/altera-ps-spi.c
+++ b/drivers/fpga/altera-ps-spi.c
@@ -302,12 +302,12 @@ static int altera_ps_probe(struct spi_device *spi)
 	snprintf(conf->mgr_name, sizeof(conf->mgr_name), "%s %s",
 		 dev_driver_string(&spi->dev), dev_name(&spi->dev));
 
-	mgr = devm_fpga_mgr_create(&spi->dev, conf->mgr_name,
-				   &altera_ps_ops, conf);
-	if (!mgr)
-		return -ENOMEM;
+	mgr = devm_fpga_mgr_register(&spi->dev, conf->mgr_name,
+				     &altera_ps_ops, conf);
+	if (IS_ERR(mgr))
+		return PTR_ERR(mgr);
 
-	return devm_fpga_mgr_register(&spi->dev, mgr);
+	return 0;
 }
 
 static const struct spi_device_id altera_ps_spi_ids[] = {
diff --git a/drivers/fpga/dfl-fme-mgr.c b/drivers/fpga/dfl-fme-mgr.c
index d5861d13b306..d71eba817b3b 100644
--- a/drivers/fpga/dfl-fme-mgr.c
+++ b/drivers/fpga/dfl-fme-mgr.c
@@ -308,14 +308,14 @@ static int fme_mgr_probe(struct platform_device *pdev)
 
 	fme_mgr_get_compat_id(priv->ioaddr, compat_id);
 
-	mgr = devm_fpga_mgr_create(dev, "DFL FME FPGA Manager",
-				   &fme_mgr_ops, priv);
-	if (!mgr)
-		return -ENOMEM;
+	mgr = devm_fpga_mgr_register(dev, "DFL FME FPGA Manager",
+				     &fme_mgr_ops, priv);
+	if (IS_ERR(mgr))
+		return PTR_ERR(mgr);
 
 	mgr->compat_id = compat_id;
 
-	return devm_fpga_mgr_register(dev, mgr);
+	return 0;
 }
 
 static struct platform_driver fme_mgr_driver = {
diff --git a/drivers/fpga/fpga-mgr.c b/drivers/fpga/fpga-mgr.c
index b85bc47c91a9..22f0dd5c2834 100644
--- a/drivers/fpga/fpga-mgr.c
+++ b/drivers/fpga/fpga-mgr.c
@@ -550,20 +550,20 @@ void fpga_mgr_unlock(struct fpga_manager *mgr)
 EXPORT_SYMBOL_GPL(fpga_mgr_unlock);
 
 /**
- * fpga_mgr_create - create and initialize a FPGA manager struct
+ * fpga_mgr_register - create and register a FPGA Manager device
  * @dev:	fpga manager device from pdev
  * @name:	fpga manager name
  * @mops:	pointer to structure of fpga manager ops
  * @priv:	fpga manager private data
  *
- * The caller of this function is responsible for freeing the struct with
- * fpga_mgr_free().  Using devm_fpga_mgr_create() instead is recommended.
+ * The caller of this function is responsible for calling fpga_mgr_unregister().
+ * Using devm_fpga_mgr_register instead is recommended.
  *
- * Return: pointer to struct fpga_manager or NULL
+ * Returns a struct fpga_manager pointer on success, or ERR_PTR() on error.
  */
-struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
-				     const struct fpga_manager_ops *mops,
-				     void *priv)
+struct fpga_manager *
+fpga_mgr_register(struct device *dev, const char *name,
+		  const struct fpga_manager_ops *mops, void *priv)
 {
 	struct fpga_manager *mgr;
 	int id, ret;
@@ -572,21 +572,23 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
 	    !mops->write_init || (!mops->write && !mops->write_sg) ||
 	    (mops->write && mops->write_sg)) {
 		dev_err(dev, "Attempt to register without fpga_manager_ops\n");
-		return NULL;
+		return ERR_PTR(-EINVAL);
 	}
 
 	if (!name || !strlen(name)) {
 		dev_err(dev, "Attempt to register with no name!\n");
-		return NULL;
+		return ERR_PTR(-EINVAL);
 	}
 
 	mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
 	if (!mgr)
-		return NULL;
+		return ERR_PTR(-ENOMEM);
 
 	id = ida_simple_get(&fpga_mgr_ida, 0, 0, GFP_KERNEL);
-	if (id < 0)
+	if (id < 0) {
+		ret = id;
 		goto error_kfree;
+	}
 
 	mutex_init(&mgr->ref_mutex);
 
@@ -594,7 +596,6 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
 	mgr->mops = mops;
 	mgr->priv = priv;
 
-	device_initialize(&mgr->dev);
 	mgr->dev.class = fpga_mgr_class;
 	mgr->dev.groups = mops->groups;
 	mgr->dev.parent = dev;
@@ -605,84 +606,6 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
 	if (ret)
 		goto error_device;
 
-	return mgr;
-
-error_device:
-	ida_simple_remove(&fpga_mgr_ida, id);
-error_kfree:
-	kfree(mgr);
-
-	return NULL;
-}
-EXPORT_SYMBOL_GPL(fpga_mgr_create);
-
-/**
- * fpga_mgr_free - free a FPGA manager created with fpga_mgr_create()
- * @mgr:	fpga manager struct
- */
-void fpga_mgr_free(struct fpga_manager *mgr)
-{
-	ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
-	kfree(mgr);
-}
-EXPORT_SYMBOL_GPL(fpga_mgr_free);
-
-static void devm_fpga_mgr_release(struct device *dev, void *res)
-{
-	struct fpga_mgr_devres *dr = res;
-
-	fpga_mgr_free(dr->mgr);
-}
-
-/**
- * devm_fpga_mgr_create - create and initialize a managed FPGA manager struct
- * @dev:	fpga manager device from pdev
- * @name:	fpga manager name
- * @mops:	pointer to structure of fpga manager ops
- * @priv:	fpga manager private data
- *
- * This function is intended for use in a FPGA manager driver's probe function.
- * After the manager driver creates the manager struct with
- * devm_fpga_mgr_create(), it should register it with fpga_mgr_register().  The
- * manager driver's remove function should call fpga_mgr_unregister().  The
- * manager struct allocated with this function will be freed automatically on
- * driver detach.  This includes the case of a probe function returning error
- * before calling fpga_mgr_register(), the struct will still get cleaned up.
- *
- * Return: pointer to struct fpga_manager or NULL
- */
-struct fpga_manager *devm_fpga_mgr_create(struct device *dev, const char *name,
-					  const struct fpga_manager_ops *mops,
-					  void *priv)
-{
-	struct fpga_mgr_devres *dr;
-
-	dr = devres_alloc(devm_fpga_mgr_release, sizeof(*dr), GFP_KERNEL);
-	if (!dr)
-		return NULL;
-
-	dr->mgr = fpga_mgr_create(dev, name, mops, priv);
-	if (!dr->mgr) {
-		devres_free(dr);
-		return NULL;
-	}
-
-	devres_add(dev, dr);
-
-	return dr->mgr;
-}
-EXPORT_SYMBOL_GPL(devm_fpga_mgr_create);
-
-/**
- * fpga_mgr_register - register a FPGA manager
- * @mgr: fpga manager struct
- *
- * Return: 0 on success, negative error code otherwise.
- */
-int fpga_mgr_register(struct fpga_manager *mgr)
-{
-	int ret;
-
 	/*
 	 * Initialize framework state by requesting low level driver read state
 	 * from device.  FPGA may be in reset mode or may have been programmed
@@ -690,18 +613,20 @@ int fpga_mgr_register(struct fpga_manager *mgr)
 	 */
 	mgr->state = mgr->mops->state(mgr);
 
-	ret = device_add(&mgr->dev);
-	if (ret)
-		goto error_device;
-
-	dev_info(&mgr->dev, "%s registered\n", mgr->name);
+	ret = device_register(&mgr->dev);
+	if (ret) {
+		put_device(&mgr->dev);
+		return ERR_PTR(ret);
+	}
 
-	return 0;
+	return mgr;
 
 error_device:
-	ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
+	ida_simple_remove(&fpga_mgr_ida, id);
+error_kfree:
+	kfree(mgr);
 
-	return ret;
+	return ERR_PTR(ret);
 }
 EXPORT_SYMBOL_GPL(fpga_mgr_register);
 
@@ -726,14 +651,6 @@ void fpga_mgr_unregister(struct fpga_manager *mgr)
 }
 EXPORT_SYMBOL_GPL(fpga_mgr_unregister);
 
-static int fpga_mgr_devres_match(struct device *dev, void *res,
-				 void *match_data)
-{
-	struct fpga_mgr_devres *dr = res;
-
-	return match_data == dr->mgr;
-}
-
 static void devm_fpga_mgr_unregister(struct device *dev, void *res)
 {
 	struct fpga_mgr_devres *dr = res;
@@ -743,44 +660,44 @@ static void devm_fpga_mgr_unregister(struct device *dev, void *res)
 
 /**
  * devm_fpga_mgr_register - resource managed variant of fpga_mgr_register()
- * @dev: managing device for this FPGA manager
- * @mgr: fpga manager struct
+ * @dev:	fpga manager device from pdev
+ * @name:	fpga manager name
+ * @mops:	pointer to structure of fpga manager ops
+ * @priv:	fpga manager private data
  *
  * This is the devres variant of fpga_mgr_register() for which the unregister
  * function will be called automatically when the managing device is detached.
  */
-int devm_fpga_mgr_register(struct device *dev, struct fpga_manager *mgr)
+struct fpga_manager *
+devm_fpga_mgr_register(struct device *dev, const char *name,
+		       const struct fpga_manager_ops *mops, void *priv)
 {
 	struct fpga_mgr_devres *dr;
-	int ret;
-
-	/*
-	 * Make sure that the struct fpga_manager * that is passed in is
-	 * managed itself.
-	 */
-	if (WARN_ON(!devres_find(dev, devm_fpga_mgr_release,
-				 fpga_mgr_devres_match, mgr)))
-		return -EINVAL;
+	struct fpga_manager *mgr;
 
 	dr = devres_alloc(devm_fpga_mgr_unregister, sizeof(*dr), GFP_KERNEL);
 	if (!dr)
-		return -ENOMEM;
+		return ERR_PTR(-ENOMEM);
 
-	ret = fpga_mgr_register(mgr);
-	if (ret) {
+	mgr = fpga_mgr_register(dev, name, mops, priv);
+	if (IS_ERR(mgr)) {
 		devres_free(dr);
-		return ret;
+		return mgr;
 	}
 
 	dr->mgr = mgr;
 	devres_add(dev, dr);
 
-	return 0;
+	return mgr;
 }
 EXPORT_SYMBOL_GPL(devm_fpga_mgr_register);
 
 static void fpga_mgr_dev_release(struct device *dev)
 {
+	struct fpga_manager *mgr = to_fpga_manager(dev);
+
+	ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
+	kfree(mgr);
 }
 
 static int __init fpga_mgr_class_init(void)
diff --git a/drivers/fpga/ice40-spi.c b/drivers/fpga/ice40-spi.c
index 69dec5af23c3..91ac25f339f3 100644
--- a/drivers/fpga/ice40-spi.c
+++ b/drivers/fpga/ice40-spi.c
@@ -178,12 +178,12 @@ static int ice40_fpga_probe(struct spi_device *spi)
 		return ret;
 	}
 
-	mgr = devm_fpga_mgr_create(dev, "Lattice iCE40 FPGA Manager",
-				   &ice40_fpga_ops, priv);
-	if (!mgr)
-		return -ENOMEM;
+	mgr = devm_fpga_mgr_register(dev, "Lattice iCE40 FPGA Manager",
+				     &ice40_fpga_ops, priv);
+	if (IS_ERR(mgr))
+		return PTR_ERR(mgr);
 
-	return devm_fpga_mgr_register(dev, mgr);
+	return 0;
 }
 
 static const struct of_device_id ice40_fpga_of_match[] = {
diff --git a/drivers/fpga/machxo2-spi.c b/drivers/fpga/machxo2-spi.c
index 114a64d2b7a4..0137c1e2e706 100644
--- a/drivers/fpga/machxo2-spi.c
+++ b/drivers/fpga/machxo2-spi.c
@@ -366,12 +366,12 @@ static int machxo2_spi_probe(struct spi_device *spi)
 		return -EINVAL;
 	}
 
-	mgr = devm_fpga_mgr_create(dev, "Lattice MachXO2 SPI FPGA Manager",
-				   &machxo2_ops, spi);
-	if (!mgr)
-		return -ENOMEM;
+	mgr = devm_fpga_mgr_register(dev, "Lattice MachXO2 SPI FPGA Manager",
+				     &machxo2_ops, spi);
+	if (IS_ERR(mgr))
+		return PTR_ERR(mgr);
 
-	return devm_fpga_mgr_register(dev, mgr);
+	return 0;
 }
 
 static const struct of_device_id of_match[] = {
diff --git a/drivers/fpga/socfpga-a10.c b/drivers/fpga/socfpga-a10.c
index 573d88bdf730..ac8e89b8a5cc 100644
--- a/drivers/fpga/socfpga-a10.c
+++ b/drivers/fpga/socfpga-a10.c
@@ -508,19 +508,15 @@ static int socfpga_a10_fpga_probe(struct platform_device *pdev)
 		return -EBUSY;
 	}
 
-	mgr = devm_fpga_mgr_create(dev, "SoCFPGA Arria10 FPGA Manager",
-				   &socfpga_a10_fpga_mgr_ops, priv);
-	if (!mgr)
-		return -ENOMEM;
-
-	platform_set_drvdata(pdev, mgr);
-
-	ret = fpga_mgr_register(mgr);
-	if (ret) {
+	mgr = fpga_mgr_register(dev, "SoCFPGA Arria10 FPGA Manager",
+				&socfpga_a10_fpga_mgr_ops, priv);
+	if (IS_ERR(mgr)) {
 		clk_disable_unprepare(priv->clk);
-		return ret;
+		return PTR_ERR(mgr);
 	}
 
+	platform_set_drvdata(pdev, mgr);
+
 	return 0;
 }
 
diff --git a/drivers/fpga/socfpga.c b/drivers/fpga/socfpga.c
index 1f467173fc1f..ebfa9f4d24ef 100644
--- a/drivers/fpga/socfpga.c
+++ b/drivers/fpga/socfpga.c
@@ -571,12 +571,12 @@ static int socfpga_fpga_probe(struct platform_device *pdev)
 	if (ret)
 		return ret;
 
-	mgr = devm_fpga_mgr_create(dev, "Altera SOCFPGA FPGA Manager",
-				   &socfpga_fpga_ops, priv);
-	if (!mgr)
-		return -ENOMEM;
+	mgr = devm_fpga_mgr_register(dev, "Altera SOCFPGA FPGA Manager",
+				     &socfpga_fpga_ops, priv);
+	if (IS_ERR(mgr))
+		return PTR_ERR(mgr);
 
-	return devm_fpga_mgr_register(dev, mgr);
+	return 0;
 }
 
 #ifdef CONFIG_OF
diff --git a/drivers/fpga/stratix10-soc.c b/drivers/fpga/stratix10-soc.c
index 657a70c5fc99..e0bd91b3f7f8 100644
--- a/drivers/fpga/stratix10-soc.c
+++ b/drivers/fpga/stratix10-soc.c
@@ -425,18 +425,11 @@ static int s10_probe(struct platform_device *pdev)
 
 	init_completion(&priv->status_return_completion);
 
-	mgr = fpga_mgr_create(dev, "Stratix10 SOC FPGA Manager",
-			      &s10_ops, priv);
-	if (!mgr) {
-		dev_err(dev, "unable to create FPGA manager\n");
-		ret = -ENOMEM;
-		goto probe_err;
-	}
-
-	ret = fpga_mgr_register(mgr);
-	if (ret) {
+	mgr = fpga_mgr_register(dev, "Stratix10 SOC FPGA Manager",
+				&s10_ops, priv);
+	if (IS_ERR(mgr)) {
 		dev_err(dev, "unable to register FPGA manager\n");
-		fpga_mgr_free(mgr);
+		ret = PTR_ERR(mgr);
 		goto probe_err;
 	}
 
diff --git a/drivers/fpga/ts73xx-fpga.c b/drivers/fpga/ts73xx-fpga.c
index 101f016c6ed8..5efbec2b9f9f 100644
--- a/drivers/fpga/ts73xx-fpga.c
+++ b/drivers/fpga/ts73xx-fpga.c
@@ -122,12 +122,12 @@ static int ts73xx_fpga_probe(struct platform_device *pdev)
 	if (IS_ERR(priv->io_base))
 		return PTR_ERR(priv->io_base);
 
-	mgr = devm_fpga_mgr_create(kdev, "TS-73xx FPGA Manager",
-				   &ts73xx_fpga_ops, priv);
-	if (!mgr)
-		return -ENOMEM;
+	mgr = devm_fpga_mgr_register(kdev, "TS-73xx FPGA Manager",
+				     &ts73xx_fpga_ops, priv);
+	if (IS_ERR(mgr))
+		return PTR_ERR(mgr);
 
-	return devm_fpga_mgr_register(kdev, mgr);
+	return 0;
 }
 
 static struct platform_driver ts73xx_fpga_driver = {
diff --git a/drivers/fpga/xilinx-spi.c b/drivers/fpga/xilinx-spi.c
index fee4d0abf6bf..efda475f7a98 100644
--- a/drivers/fpga/xilinx-spi.c
+++ b/drivers/fpga/xilinx-spi.c
@@ -247,13 +247,13 @@ static int xilinx_spi_probe(struct spi_device *spi)
 		return dev_err_probe(&spi->dev, PTR_ERR(conf->done),
 				     "Failed to get DONE gpio\n");
 
-	mgr = devm_fpga_mgr_create(&spi->dev,
-				   "Xilinx Slave Serial FPGA Manager",
-				   &xilinx_spi_ops, conf);
-	if (!mgr)
-		return -ENOMEM;
+	mgr = devm_fpga_mgr_register(&spi->dev,
+				     "Xilinx Slave Serial FPGA Manager",
+				     &xilinx_spi_ops, conf);
+	if (IS_ERR(mgr))
+		return PTR_ERR(mgr);
 
-	return devm_fpga_mgr_register(&spi->dev, mgr);
+	return 0;
 }
 
 static const struct of_device_id xlnx_spi_of_match[] = {
diff --git a/drivers/fpga/zynq-fpga.c b/drivers/fpga/zynq-fpga.c
index 07fa8d9ec675..74bbb9710f4e 100644
--- a/drivers/fpga/zynq-fpga.c
+++ b/drivers/fpga/zynq-fpga.c
@@ -609,20 +609,16 @@ static int zynq_fpga_probe(struct platform_device *pdev)
 
 	clk_disable(priv->clk);
 
-	mgr = devm_fpga_mgr_create(dev, "Xilinx Zynq FPGA Manager",
-				   &zynq_fpga_ops, priv);
-	if (!mgr)
-		return -ENOMEM;
-
-	platform_set_drvdata(pdev, mgr);
-
-	err = fpga_mgr_register(mgr);
-	if (err) {
+	mgr = fpga_mgr_register(dev, "Xilinx Zynq FPGA Manager",
+				&zynq_fpga_ops, priv);
+	if (IS_ERR(mgr)) {
 		dev_err(dev, "unable to register FPGA manager\n");
 		clk_unprepare(priv->clk);
-		return err;
+		return PTR_ERR(mgr);
 	}
 
+	platform_set_drvdata(pdev, mgr);
+
 	return 0;
 }
 
diff --git a/drivers/fpga/zynqmp-fpga.c b/drivers/fpga/zynqmp-fpga.c
index 125743c9797f..1a6d520985be 100644
--- a/drivers/fpga/zynqmp-fpga.c
+++ b/drivers/fpga/zynqmp-fpga.c
@@ -102,12 +102,12 @@ static int zynqmp_fpga_probe(struct platform_device *pdev)
 
 	priv->dev = dev;
 
-	mgr = devm_fpga_mgr_create(dev, "Xilinx ZynqMP FPGA Manager",
-				   &zynqmp_fpga_ops, priv);
-	if (!mgr)
-		return -ENOMEM;
+	mgr = devm_fpga_mgr_register(dev, "Xilinx ZynqMP FPGA Manager",
+				     &zynqmp_fpga_ops, priv);
+	if (IS_ERR(mgr))
+		return PTR_ERR(mgr);
 
-	return devm_fpga_mgr_register(dev, mgr);
+	return 0;
 }
 
 static const struct of_device_id zynqmp_fpga_of_match[] = {
diff --git a/include/linux/fpga/fpga-mgr.h b/include/linux/fpga/fpga-mgr.h
index 2bc3030a69e5..d1ccf8f8c38a 100644
--- a/include/linux/fpga/fpga-mgr.h
+++ b/include/linux/fpga/fpga-mgr.h
@@ -191,17 +191,14 @@ struct fpga_manager *fpga_mgr_get(struct device *dev);
 
 void fpga_mgr_put(struct fpga_manager *mgr);
 
-struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
-				     const struct fpga_manager_ops *mops,
-				     void *priv);
-void fpga_mgr_free(struct fpga_manager *mgr);
-int fpga_mgr_register(struct fpga_manager *mgr);
+struct fpga_manager *fpga_mgr_register(struct device *dev, const char *name,
+				       const struct fpga_manager_ops *mops,
+				       void *priv);
 void fpga_mgr_unregister(struct fpga_manager *mgr);
 
-int devm_fpga_mgr_register(struct device *dev, struct fpga_manager *mgr);
-
-struct fpga_manager *devm_fpga_mgr_create(struct device *dev, const char *name,
-					  const struct fpga_manager_ops *mops,
-					  void *priv);
+struct fpga_manager *
+devm_fpga_mgr_register(struct device *dev, const char *name,
+		       const struct fpga_manager_ops *mops,
+		       void *priv);
 
 #endif /*_LINUX_FPGA_MGR_H */
-- 
2.25.1


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH v3 2/3] fpga: bridge: Use standard dev_release for class driver
  2021-05-24 18:50 [PATCH v3 0/3] fpga: Use standard class dev_release function Russ Weight
  2021-05-24 18:50 ` [PATCH v3 1/3] fpga: mgr: Use standard dev_release for class driver Russ Weight
@ 2021-05-24 18:50 ` Russ Weight
  2021-05-28 15:51   ` Xu Yilun
  2021-05-24 18:50 ` [PATCH v3 3/3] fpga: region: " Russ Weight
  2021-05-24 18:52 ` [PATCH v3 0/3] fpga: Use standard class dev_release function Russ Weight
  3 siblings, 1 reply; 17+ messages in thread
From: Russ Weight @ 2021-05-24 18:50 UTC (permalink / raw)
  To: mdf, linux-fpga
  Cc: trix, lgoncalv, yilun.xu, hao.wu, matthew.gerlach, richard.gong,
	Russ Weight

The FPGA bridge class driver data structure is being treated as a
managed resource instead of using standard dev_release call-back
to release the class data structure. This change removes the
managed resource code and combines the create() and register()
functions into a single register() function.

Signed-off-by: Russ Weight <russell.h.weight@intel.com>
---
v3:
  - Cleaned up comment header for fpga_bridge_register()
  - Fix error return values for fpga_bridge_register()
v2:
  - No changes
---
 drivers/fpga/altera-fpga2sdram.c    |  12 +--
 drivers/fpga/altera-freeze-bridge.c |  10 +--
 drivers/fpga/altera-hps2fpga.c      |  12 +--
 drivers/fpga/dfl-fme-br.c           |  10 +--
 drivers/fpga/fpga-bridge.c          | 119 ++++++----------------------
 drivers/fpga/xilinx-pr-decoupler.c  |  17 ++--
 include/linux/fpga/fpga-bridge.h    |  12 +--
 7 files changed, 52 insertions(+), 140 deletions(-)

diff --git a/drivers/fpga/altera-fpga2sdram.c b/drivers/fpga/altera-fpga2sdram.c
index a78e49c63c64..66063507116b 100644
--- a/drivers/fpga/altera-fpga2sdram.c
+++ b/drivers/fpga/altera-fpga2sdram.c
@@ -121,17 +121,13 @@ static int alt_fpga_bridge_probe(struct platform_device *pdev)
 	/* Get f2s bridge configuration saved in handoff register */
 	regmap_read(sysmgr, SYSMGR_ISWGRP_HANDOFF3, &priv->mask);
 
-	br = devm_fpga_bridge_create(dev, F2S_BRIDGE_NAME,
-				     &altera_fpga2sdram_br_ops, priv);
-	if (!br)
-		return -ENOMEM;
+	br = fpga_bridge_register(dev, F2S_BRIDGE_NAME,
+				  &altera_fpga2sdram_br_ops, priv);
+	if (IS_ERR(br))
+		return PTR_ERR(mgr);
 
 	platform_set_drvdata(pdev, br);
 
-	ret = fpga_bridge_register(br);
-	if (ret)
-		return ret;
-
 	dev_info(dev, "driver initialized with handoff %08x\n", priv->mask);
 
 	if (!of_property_read_u32(dev->of_node, "bridge-enable", &enable)) {
diff --git a/drivers/fpga/altera-freeze-bridge.c b/drivers/fpga/altera-freeze-bridge.c
index dd58c4aea92e..bfbfa43cd05b 100644
--- a/drivers/fpga/altera-freeze-bridge.c
+++ b/drivers/fpga/altera-freeze-bridge.c
@@ -244,14 +244,14 @@ static int altera_freeze_br_probe(struct platform_device *pdev)
 
 	priv->base_addr = base_addr;
 
-	br = devm_fpga_bridge_create(dev, FREEZE_BRIDGE_NAME,
-				     &altera_freeze_br_br_ops, priv);
-	if (!br)
-		return -ENOMEM;
+	br = fpga_bridge_register(dev, FREEZE_BRIDGE_NAME,
+				  &altera_freeze_br_br_ops, priv);
+	if (IS_ERR(br))
+		return PTR_ERR(br);
 
 	platform_set_drvdata(pdev, br);
 
-	return fpga_bridge_register(br);
+	return 0;
 }
 
 static int altera_freeze_br_remove(struct platform_device *pdev)
diff --git a/drivers/fpga/altera-hps2fpga.c b/drivers/fpga/altera-hps2fpga.c
index 77b95f251821..aa758426c22b 100644
--- a/drivers/fpga/altera-hps2fpga.c
+++ b/drivers/fpga/altera-hps2fpga.c
@@ -180,19 +180,15 @@ static int alt_fpga_bridge_probe(struct platform_device *pdev)
 		}
 	}
 
-	br = devm_fpga_bridge_create(dev, priv->name,
-				     &altera_hps2fpga_br_ops, priv);
-	if (!br) {
-		ret = -ENOMEM;
+	br = fpga_bridge_register(dev, priv->name,
+				  &altera_hps2fpga_br_ops, priv);
+	if (IS_ERR(br)) {
+		ret = PTR_ERR(br);
 		goto err;
 	}
 
 	platform_set_drvdata(pdev, br);
 
-	ret = fpga_bridge_register(br);
-	if (ret)
-		goto err;
-
 	return 0;
 
 err:
diff --git a/drivers/fpga/dfl-fme-br.c b/drivers/fpga/dfl-fme-br.c
index 3ff9f3a687ce..808d1f4d76df 100644
--- a/drivers/fpga/dfl-fme-br.c
+++ b/drivers/fpga/dfl-fme-br.c
@@ -68,14 +68,14 @@ static int fme_br_probe(struct platform_device *pdev)
 
 	priv->pdata = dev_get_platdata(dev);
 
-	br = devm_fpga_bridge_create(dev, "DFL FPGA FME Bridge",
-				     &fme_bridge_ops, priv);
-	if (!br)
-		return -ENOMEM;
+	br = fpga_bridge_register(dev, "DFL FPGA FME Bridge",
+				  &fme_bridge_ops, priv);
+	if (IS_ERR(br))
+		return PTR_ERR(br);
 
 	platform_set_drvdata(pdev, br);
 
-	return fpga_bridge_register(br);
+	return 0;
 }
 
 static int fme_br_remove(struct platform_device *pdev)
diff --git a/drivers/fpga/fpga-bridge.c b/drivers/fpga/fpga-bridge.c
index e9266b2a357f..54a08a682e89 100644
--- a/drivers/fpga/fpga-bridge.c
+++ b/drivers/fpga/fpga-bridge.c
@@ -312,36 +312,35 @@ static struct attribute *fpga_bridge_attrs[] = {
 ATTRIBUTE_GROUPS(fpga_bridge);
 
 /**
- * fpga_bridge_create - create and initialize a struct fpga_bridge
+ * fpga_bridge_register - create and register a FPGA Bridge device
  * @dev:	FPGA bridge device from pdev
  * @name:	FPGA bridge name
  * @br_ops:	pointer to structure of fpga bridge ops
  * @priv:	FPGA bridge private data
  *
- * The caller of this function is responsible for freeing the bridge with
- * fpga_bridge_free().  Using devm_fpga_bridge_create() instead is recommended.
- *
- * Return: struct fpga_bridge or NULL
+ * Returns a struct fpga_bridge pointer on success, or ERR_PTR() on error.
  */
-struct fpga_bridge *fpga_bridge_create(struct device *dev, const char *name,
-				       const struct fpga_bridge_ops *br_ops,
-				       void *priv)
+struct fpga_bridge *fpga_bridge_register(struct device *dev, const char *name,
+					 const struct fpga_bridge_ops *br_ops,
+					 void *priv)
 {
 	struct fpga_bridge *bridge;
 	int id, ret;
 
 	if (!name || !strlen(name)) {
 		dev_err(dev, "Attempt to register with no name!\n");
-		return NULL;
+		return ERR_PTR(-EINVAL);
 	}
 
 	bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
 	if (!bridge)
-		return NULL;
+		return ERR_PTR(-ENOMEM);
 
 	id = ida_simple_get(&fpga_bridge_ida, 0, 0, GFP_KERNEL);
-	if (id < 0)
+	if (id < 0) {
+		ret = id;
 		goto error_kfree;
+	}
 
 	mutex_init(&bridge->mutex);
 	INIT_LIST_HEAD(&bridge->node);
@@ -350,7 +349,6 @@ struct fpga_bridge *fpga_bridge_create(struct device *dev, const char *name,
 	bridge->br_ops = br_ops;
 	bridge->priv = priv;
 
-	device_initialize(&bridge->dev);
 	bridge->dev.groups = br_ops->groups;
 	bridge->dev.class = fpga_bridge_class;
 	bridge->dev.parent = dev;
@@ -361,6 +359,16 @@ struct fpga_bridge *fpga_bridge_create(struct device *dev, const char *name,
 	if (ret)
 		goto error_device;
 
+	ret = device_register(&bridge->dev);
+	if (ret) {
+		put_device(&bridge->dev);
+		return ERR_PTR(ret);
+	}
+
+	of_platform_populate(dev->of_node, NULL, NULL, dev);
+
+	dev_info(dev->parent, "fpga bridge [%s] registered\n", bridge->name);
+
 	return bridge;
 
 error_device:
@@ -368,88 +376,7 @@ struct fpga_bridge *fpga_bridge_create(struct device *dev, const char *name,
 error_kfree:
 	kfree(bridge);
 
-	return NULL;
-}
-EXPORT_SYMBOL_GPL(fpga_bridge_create);
-
-/**
- * fpga_bridge_free - free a fpga bridge created by fpga_bridge_create()
- * @bridge:	FPGA bridge struct
- */
-void fpga_bridge_free(struct fpga_bridge *bridge)
-{
-	ida_simple_remove(&fpga_bridge_ida, bridge->dev.id);
-	kfree(bridge);
-}
-EXPORT_SYMBOL_GPL(fpga_bridge_free);
-
-static void devm_fpga_bridge_release(struct device *dev, void *res)
-{
-	struct fpga_bridge *bridge = *(struct fpga_bridge **)res;
-
-	fpga_bridge_free(bridge);
-}
-
-/**
- * devm_fpga_bridge_create - create and init a managed struct fpga_bridge
- * @dev:	FPGA bridge device from pdev
- * @name:	FPGA bridge name
- * @br_ops:	pointer to structure of fpga bridge ops
- * @priv:	FPGA bridge private data
- *
- * This function is intended for use in a FPGA bridge driver's probe function.
- * After the bridge driver creates the struct with devm_fpga_bridge_create(), it
- * should register the bridge with fpga_bridge_register().  The bridge driver's
- * remove function should call fpga_bridge_unregister().  The bridge struct
- * allocated with this function will be freed automatically on driver detach.
- * This includes the case of a probe function returning error before calling
- * fpga_bridge_register(), the struct will still get cleaned up.
- *
- *  Return: struct fpga_bridge or NULL
- */
-struct fpga_bridge
-*devm_fpga_bridge_create(struct device *dev, const char *name,
-			 const struct fpga_bridge_ops *br_ops, void *priv)
-{
-	struct fpga_bridge **ptr, *bridge;
-
-	ptr = devres_alloc(devm_fpga_bridge_release, sizeof(*ptr), GFP_KERNEL);
-	if (!ptr)
-		return NULL;
-
-	bridge = fpga_bridge_create(dev, name, br_ops, priv);
-	if (!bridge) {
-		devres_free(ptr);
-	} else {
-		*ptr = bridge;
-		devres_add(dev, ptr);
-	}
-
-	return bridge;
-}
-EXPORT_SYMBOL_GPL(devm_fpga_bridge_create);
-
-/**
- * fpga_bridge_register - register a FPGA bridge
- *
- * @bridge: FPGA bridge struct
- *
- * Return: 0 for success, error code otherwise.
- */
-int fpga_bridge_register(struct fpga_bridge *bridge)
-{
-	struct device *dev = &bridge->dev;
-	int ret;
-
-	ret = device_add(dev);
-	if (ret)
-		return ret;
-
-	of_platform_populate(dev->of_node, NULL, NULL, dev);
-
-	dev_info(dev->parent, "fpga bridge [%s] registered\n", bridge->name);
-
-	return 0;
+	return ERR_PTR(ret);
 }
 EXPORT_SYMBOL_GPL(fpga_bridge_register);
 
@@ -475,6 +402,10 @@ EXPORT_SYMBOL_GPL(fpga_bridge_unregister);
 
 static void fpga_bridge_dev_release(struct device *dev)
 {
+	struct fpga_bridge *bridge = to_fpga_bridge(dev);
+
+	ida_simple_remove(&fpga_bridge_ida, bridge->dev.id);
+	kfree(bridge);
 }
 
 static int __init fpga_bridge_dev_init(void)
diff --git a/drivers/fpga/xilinx-pr-decoupler.c b/drivers/fpga/xilinx-pr-decoupler.c
index ea2bde6e5bc4..c004e52b9464 100644
--- a/drivers/fpga/xilinx-pr-decoupler.c
+++ b/drivers/fpga/xilinx-pr-decoupler.c
@@ -138,22 +138,17 @@ static int xlnx_pr_decoupler_probe(struct platform_device *pdev)
 
 	clk_disable(priv->clk);
 
-	br = devm_fpga_bridge_create(&pdev->dev, priv->ipconfig->name,
-				     &xlnx_pr_decoupler_br_ops, priv);
-	if (!br) {
-		err = -ENOMEM;
-		goto err_clk;
-	}
-
-	platform_set_drvdata(pdev, br);
-
-	err = fpga_bridge_register(br);
-	if (err) {
+	br = fpga_bridge_register(&pdev->dev, priv->ipconfig->name,
+				  &xlnx_pr_decoupler_br_ops, priv);
+	if (IS_ERR(br)) {
+		err = PTR_ERR(br);
 		dev_err(&pdev->dev, "unable to register %s",
 			priv->ipconfig->name);
 		goto err_clk;
 	}
 
+	platform_set_drvdata(pdev, br);
+
 	return 0;
 
 err_clk:
diff --git a/include/linux/fpga/fpga-bridge.h b/include/linux/fpga/fpga-bridge.h
index 817600a32c93..86f306c99fa5 100644
--- a/include/linux/fpga/fpga-bridge.h
+++ b/include/linux/fpga/fpga-bridge.h
@@ -62,15 +62,9 @@ int of_fpga_bridge_get_to_list(struct device_node *np,
 			       struct fpga_image_info *info,
 			       struct list_head *bridge_list);
 
-struct fpga_bridge *fpga_bridge_create(struct device *dev, const char *name,
-				       const struct fpga_bridge_ops *br_ops,
-				       void *priv);
-void fpga_bridge_free(struct fpga_bridge *br);
-int fpga_bridge_register(struct fpga_bridge *br);
+struct fpga_bridge *fpga_bridge_register(struct device *dev, const char *name,
+					 const struct fpga_bridge_ops *br_ops,
+					 void *priv);
 void fpga_bridge_unregister(struct fpga_bridge *br);
 
-struct fpga_bridge
-*devm_fpga_bridge_create(struct device *dev, const char *name,
-			 const struct fpga_bridge_ops *br_ops, void *priv);
-
 #endif /* _LINUX_FPGA_BRIDGE_H */
-- 
2.25.1


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH v3 3/3] fpga: region: Use standard dev_release for class driver
  2021-05-24 18:50 [PATCH v3 0/3] fpga: Use standard class dev_release function Russ Weight
  2021-05-24 18:50 ` [PATCH v3 1/3] fpga: mgr: Use standard dev_release for class driver Russ Weight
  2021-05-24 18:50 ` [PATCH v3 2/3] fpga: bridge: " Russ Weight
@ 2021-05-24 18:50 ` Russ Weight
  2021-05-28 16:10   ` Xu Yilun
  2021-05-24 18:52 ` [PATCH v3 0/3] fpga: Use standard class dev_release function Russ Weight
  3 siblings, 1 reply; 17+ messages in thread
From: Russ Weight @ 2021-05-24 18:50 UTC (permalink / raw)
  To: mdf, linux-fpga
  Cc: trix, lgoncalv, yilun.xu, hao.wu, matthew.gerlach, richard.gong,
	Russ Weight

The FPGA region class driver data structure is being treated as a
managed resource instead of using standard dev_release call-back
to release the class data structure. This change removes the
managed resource code and combines the create() and register()
functions into a single register() function.

Signed-off-by: Russ Weight <russell.h.weight@intel.com>
---
v3:
  - Cleaned up comment header for fpga_region_register()
  - Fix fpga_region_register() error return on ida_simple_get() failure
v2:
  - No changes
---
 drivers/fpga/dfl-fme-region.c    |  10 +--
 drivers/fpga/dfl.c               |  10 +--
 drivers/fpga/fpga-region.c       | 103 ++++++-------------------------
 drivers/fpga/of-fpga-region.c    |  10 +--
 include/linux/fpga/fpga-region.h |  12 +---
 5 files changed, 32 insertions(+), 113 deletions(-)

diff --git a/drivers/fpga/dfl-fme-region.c b/drivers/fpga/dfl-fme-region.c
index 1eeb42af1012..631c8ce160e3 100644
--- a/drivers/fpga/dfl-fme-region.c
+++ b/drivers/fpga/dfl-fme-region.c
@@ -39,9 +39,9 @@ static int fme_region_probe(struct platform_device *pdev)
 	if (IS_ERR(mgr))
 		return -EPROBE_DEFER;
 
-	region = devm_fpga_region_create(dev, mgr, fme_region_get_bridges);
-	if (!region) {
-		ret = -ENOMEM;
+	region = fpga_region_register(dev, mgr, fme_region_get_bridges);
+	if (IS_ERR(region)) {
+		ret = PTR_ERR(region);
 		goto eprobe_mgr_put;
 	}
 
@@ -49,10 +49,6 @@ static int fme_region_probe(struct platform_device *pdev)
 	region->compat_id = mgr->compat_id;
 	platform_set_drvdata(pdev, region);
 
-	ret = fpga_region_register(region);
-	if (ret)
-		goto eprobe_mgr_put;
-
 	dev_dbg(dev, "DFL FME FPGA Region probed\n");
 
 	return 0;
diff --git a/drivers/fpga/dfl.c b/drivers/fpga/dfl.c
index 511b20ff35a3..2fb3a5bfe4df 100644
--- a/drivers/fpga/dfl.c
+++ b/drivers/fpga/dfl.c
@@ -1400,9 +1400,9 @@ dfl_fpga_feature_devs_enumerate(struct dfl_fpga_enum_info *info)
 	if (!cdev)
 		return ERR_PTR(-ENOMEM);
 
-	cdev->region = devm_fpga_region_create(info->dev, NULL, NULL);
-	if (!cdev->region) {
-		ret = -ENOMEM;
+	cdev->region = fpga_region_register(info->dev, NULL, NULL);
+	if (IS_ERR(cdev->region)) {
+		ret = PTR_ERR(cdev->region);
 		goto free_cdev_exit;
 	}
 
@@ -1410,10 +1410,6 @@ dfl_fpga_feature_devs_enumerate(struct dfl_fpga_enum_info *info)
 	mutex_init(&cdev->lock);
 	INIT_LIST_HEAD(&cdev->port_dev_list);
 
-	ret = fpga_region_register(cdev->region);
-	if (ret)
-		goto free_cdev_exit;
-
 	/* create and init build info for enumeration */
 	binfo = devm_kzalloc(info->dev, sizeof(*binfo), GFP_KERNEL);
 	if (!binfo) {
diff --git a/drivers/fpga/fpga-region.c b/drivers/fpga/fpga-region.c
index c3134b89c3fe..f65f60367e2c 100644
--- a/drivers/fpga/fpga-region.c
+++ b/drivers/fpga/fpga-region.c
@@ -180,39 +180,35 @@ static struct attribute *fpga_region_attrs[] = {
 ATTRIBUTE_GROUPS(fpga_region);
 
 /**
- * fpga_region_create - alloc and init a struct fpga_region
+ * fpga_region_register - create and register a FPGA Region device
  * @dev: device parent
  * @mgr: manager that programs this region
  * @get_bridges: optional function to get bridges to a list
  *
- * The caller of this function is responsible for freeing the resulting region
- * struct with fpga_region_free().  Using devm_fpga_region_create() instead is
- * recommended.
- *
- * Return: struct fpga_region or NULL
+ * Returns a struct fpga_region pointer on success, or ERR_PTR() on error.
  */
-struct fpga_region
-*fpga_region_create(struct device *dev,
-		    struct fpga_manager *mgr,
-		    int (*get_bridges)(struct fpga_region *))
+struct fpga_region *
+fpga_region_register(struct device *dev, struct fpga_manager *mgr,
+		     int (*get_bridges)(struct fpga_region *))
 {
 	struct fpga_region *region;
 	int id, ret = 0;
 
 	region = kzalloc(sizeof(*region), GFP_KERNEL);
 	if (!region)
-		return NULL;
+		return ERR_PTR(-ENOMEM);
 
 	id = ida_simple_get(&fpga_region_ida, 0, 0, GFP_KERNEL);
-	if (id < 0)
+	if (id < 0) {
+		ret = id;
 		goto err_free;
+	}
 
 	region->mgr = mgr;
 	region->get_bridges = get_bridges;
 	mutex_init(&region->mutex);
 	INIT_LIST_HEAD(&region->bridge_list);
 
-	device_initialize(&region->dev);
 	region->dev.class = fpga_region_class;
 	region->dev.parent = dev;
 	region->dev.of_node = dev->of_node;
@@ -222,6 +218,12 @@ struct fpga_region
 	if (ret)
 		goto err_remove;
 
+	ret = device_register(&region->dev);
+	if (ret) {
+		put_device(&region->dev);
+		return ERR_PTR(ret);
+	}
+
 	return region;
 
 err_remove:
@@ -229,76 +231,7 @@ struct fpga_region
 err_free:
 	kfree(region);
 
-	return NULL;
-}
-EXPORT_SYMBOL_GPL(fpga_region_create);
-
-/**
- * fpga_region_free - free a FPGA region created by fpga_region_create()
- * @region: FPGA region
- */
-void fpga_region_free(struct fpga_region *region)
-{
-	ida_simple_remove(&fpga_region_ida, region->dev.id);
-	kfree(region);
-}
-EXPORT_SYMBOL_GPL(fpga_region_free);
-
-static void devm_fpga_region_release(struct device *dev, void *res)
-{
-	struct fpga_region *region = *(struct fpga_region **)res;
-
-	fpga_region_free(region);
-}
-
-/**
- * devm_fpga_region_create - create and initialize a managed FPGA region struct
- * @dev: device parent
- * @mgr: manager that programs this region
- * @get_bridges: optional function to get bridges to a list
- *
- * This function is intended for use in a FPGA region driver's probe function.
- * After the region driver creates the region struct with
- * devm_fpga_region_create(), it should register it with fpga_region_register().
- * The region driver's remove function should call fpga_region_unregister().
- * The region struct allocated with this function will be freed automatically on
- * driver detach.  This includes the case of a probe function returning error
- * before calling fpga_region_register(), the struct will still get cleaned up.
- *
- * Return: struct fpga_region or NULL
- */
-struct fpga_region
-*devm_fpga_region_create(struct device *dev,
-			 struct fpga_manager *mgr,
-			 int (*get_bridges)(struct fpga_region *))
-{
-	struct fpga_region **ptr, *region;
-
-	ptr = devres_alloc(devm_fpga_region_release, sizeof(*ptr), GFP_KERNEL);
-	if (!ptr)
-		return NULL;
-
-	region = fpga_region_create(dev, mgr, get_bridges);
-	if (!region) {
-		devres_free(ptr);
-	} else {
-		*ptr = region;
-		devres_add(dev, ptr);
-	}
-
-	return region;
-}
-EXPORT_SYMBOL_GPL(devm_fpga_region_create);
-
-/**
- * fpga_region_register - register a FPGA region
- * @region: FPGA region
- *
- * Return: 0 or -errno
- */
-int fpga_region_register(struct fpga_region *region)
-{
-	return device_add(&region->dev);
+	return ERR_PTR(ret);
 }
 EXPORT_SYMBOL_GPL(fpga_region_register);
 
@@ -316,6 +249,10 @@ EXPORT_SYMBOL_GPL(fpga_region_unregister);
 
 static void fpga_region_dev_release(struct device *dev)
 {
+	struct fpga_region *region = to_fpga_region(dev);
+
+	ida_simple_remove(&fpga_region_ida, region->dev.id);
+	kfree(region);
 }
 
 /**
diff --git a/drivers/fpga/of-fpga-region.c b/drivers/fpga/of-fpga-region.c
index e405309baadc..466e083654ae 100644
--- a/drivers/fpga/of-fpga-region.c
+++ b/drivers/fpga/of-fpga-region.c
@@ -405,16 +405,12 @@ static int of_fpga_region_probe(struct platform_device *pdev)
 	if (IS_ERR(mgr))
 		return -EPROBE_DEFER;
 
-	region = devm_fpga_region_create(dev, mgr, of_fpga_region_get_bridges);
-	if (!region) {
-		ret = -ENOMEM;
+	region = fpga_region_register(dev, mgr, of_fpga_region_get_bridges);
+	if (IS_ERR(region)) {
+		ret = PTR_ERR(region);
 		goto eprobe_mgr_put;
 	}
 
-	ret = fpga_region_register(region);
-	if (ret)
-		goto eprobe_mgr_put;
-
 	of_platform_populate(np, fpga_region_of_match, NULL, &region->dev);
 	platform_set_drvdata(pdev, region);
 
diff --git a/include/linux/fpga/fpga-region.h b/include/linux/fpga/fpga-region.h
index 27cb706275db..ca793f43af14 100644
--- a/include/linux/fpga/fpga-region.h
+++ b/include/linux/fpga/fpga-region.h
@@ -37,15 +37,9 @@ struct fpga_region *fpga_region_class_find(
 
 int fpga_region_program_fpga(struct fpga_region *region);
 
-struct fpga_region
-*fpga_region_create(struct device *dev, struct fpga_manager *mgr,
-		    int (*get_bridges)(struct fpga_region *));
-void fpga_region_free(struct fpga_region *region);
-int fpga_region_register(struct fpga_region *region);
+struct fpga_region *
+fpga_region_register(struct device *dev, struct fpga_manager *mgr,
+		     int (*get_bridges)(struct fpga_region *));
 void fpga_region_unregister(struct fpga_region *region);
 
-struct fpga_region
-*devm_fpga_region_create(struct device *dev, struct fpga_manager *mgr,
-			int (*get_bridges)(struct fpga_region *));
-
 #endif /* _FPGA_REGION_H */
-- 
2.25.1


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v3 0/3] fpga: Use standard class dev_release function
  2021-05-24 18:50 [PATCH v3 0/3] fpga: Use standard class dev_release function Russ Weight
                   ` (2 preceding siblings ...)
  2021-05-24 18:50 ` [PATCH v3 3/3] fpga: region: " Russ Weight
@ 2021-05-24 18:52 ` Russ Weight
  3 siblings, 0 replies; 17+ messages in thread
From: Russ Weight @ 2021-05-24 18:52 UTC (permalink / raw)
  To: mdf, linux-fpga
  Cc: trix, lgoncalv, yilun.xu, hao.wu, matthew.gerlach, richard.gong

If you have a chance to review these patches, please provide review tags.

Thanks,
- Russ

On 5/24/21 11:50 AM, Russ Weight wrote:
> The FPGA framework has a convention of using managed resource
> functions to allow parent drivers to manage the data structures
> allocated by the class drivers. They use an empty *_dev_release()
> function to satisfy the class driver.
>
> This is inconsistent with linux driver model.
>
> These changes remove the managed resource functions and populate
> the class dev_release callback functions. They also merge the
> create and register functions into a single register function for
> each of the fpga-mgr, fpga-region, and fpga-bridge class drivers.
>
> For more context, refer to this email thread:
>
> https://marc.info/?l=linux-fpga&m=162127412218557&w=2
>
> I turned on the configs assocated with each of the modified files,
> but I must have been missing some dependencies, because not all
> of them compiled. I did a run-time test specifically with the
> dfl-fme infrastructure. This would have exercised the region,
> bridge, and fpga-mgr frameworks.
>
> Changelog v2 -> v3:
>   - Cleaned up comment headers for fpga_mgr_register(), fpga_bridge_register(),
>     and fpga_region_register().
>   - Fixed error return on ida_simple_get() failure for fpga_mgr_register(),
>     fpga_bridge_register(), and fpga_region_register().
>   - Fixed error return value for fpga_bridge_register(): ERR_PTR(ret) instead
>     of NULL.
>
> Changelog v1 -> v2:
>   - Restored devm_fpga_mgr_register() functionality to the fpga-mgr
>     class driver, adapted for the combined create/register functionality.
>   - All previous callers of devm_fpga_mgr_register() will continue to call
>     devm_fpga_mgr_register().
>   - replaced unnecessary ternary operators in return statements with
>     standard if conditions.
>
> Russ Weight (3):
>   fpga: mgr: Use standard dev_release for class driver
>   fpga: bridge: Use standard dev_release for class driver
>   fpga: region: Use standard dev_release for class driver
>
>  drivers/fpga/altera-cvp.c           |  12 +-
>  drivers/fpga/altera-fpga2sdram.c    |  12 +-
>  drivers/fpga/altera-freeze-bridge.c |  10 +-
>  drivers/fpga/altera-hps2fpga.c      |  12 +-
>  drivers/fpga/altera-pr-ip-core.c    |   8 +-
>  drivers/fpga/altera-ps-spi.c        |  10 +-
>  drivers/fpga/dfl-fme-br.c           |  10 +-
>  drivers/fpga/dfl-fme-mgr.c          |  10 +-
>  drivers/fpga/dfl-fme-region.c       |  10 +-
>  drivers/fpga/dfl.c                  |  10 +-
>  drivers/fpga/fpga-bridge.c          | 119 +++++---------------
>  drivers/fpga/fpga-mgr.c             | 163 +++++++---------------------
>  drivers/fpga/fpga-region.c          | 103 ++++--------------
>  drivers/fpga/ice40-spi.c            |  10 +-
>  drivers/fpga/machxo2-spi.c          |  10 +-
>  drivers/fpga/of-fpga-region.c       |  10 +-
>  drivers/fpga/socfpga-a10.c          |  16 +--
>  drivers/fpga/socfpga.c              |  10 +-
>  drivers/fpga/stratix10-soc.c        |  15 +--
>  drivers/fpga/ts73xx-fpga.c          |  10 +-
>  drivers/fpga/xilinx-pr-decoupler.c  |  17 +--
>  drivers/fpga/xilinx-spi.c           |  12 +-
>  drivers/fpga/zynq-fpga.c            |  16 +--
>  drivers/fpga/zynqmp-fpga.c          |  10 +-
>  include/linux/fpga/fpga-bridge.h    |  12 +-
>  include/linux/fpga/fpga-mgr.h       |  17 ++-
>  include/linux/fpga/fpga-region.h    |  12 +-
>  27 files changed, 196 insertions(+), 470 deletions(-)
>


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v3 1/3] fpga: mgr: Use standard dev_release for class driver
  2021-05-24 18:50 ` [PATCH v3 1/3] fpga: mgr: Use standard dev_release for class driver Russ Weight
@ 2021-05-24 20:16   ` Tom Rix
  2021-05-25 16:51     ` Russ Weight
  2021-05-24 22:39   ` Moritz Fischer
  2021-05-28 15:41   ` Xu Yilun
  2 siblings, 1 reply; 17+ messages in thread
From: Tom Rix @ 2021-05-24 20:16 UTC (permalink / raw)
  To: Russ Weight, mdf, linux-fpga
  Cc: lgoncalv, yilun.xu, hao.wu, matthew.gerlach, richard.gong

Russ,

I think the signature fpga_mgr_register, should change to

int fpga_mgr_register(struct device *, const struct fpga_mager_ops 
*mops, void *priv);

I'll explain below ..

On 5/24/21 11:50 AM, Russ Weight wrote:
> The FPGA manager class driver data structure is being treated as a
> managed resource instead of using standard dev_release call-back
> to release the class data structure. This change removes the
> managed resource code for the freeing of the class data structure
> and combines the create() and register() functions into a single
> register() function.
>
> The devm_fpga_mgr_register() function is retained.
>
> Signed-off-by: Russ Weight <russell.h.weight@intel.com>
> ---
> v3:
>    - Cleaned up comment header for fpga_mgr_register()
>    - Fix error return on ida_simple_get() failure
> v2:
>    - Restored devm_fpga_mgr_register() functionality, adapted for the combined
>      create/register functionality.
>    - All previous callers of devm_fpga_mgr_register() will continue to call
>      devm_fpga_mgr_register().
>    - replaced unnecessary ternary operators in return statements with standard
>      if conditions.
> ---
>   drivers/fpga/altera-cvp.c        |  12 +--
>   drivers/fpga/altera-pr-ip-core.c |   8 +-
>   drivers/fpga/altera-ps-spi.c     |  10 +-
>   drivers/fpga/dfl-fme-mgr.c       |  10 +-
>   drivers/fpga/fpga-mgr.c          | 163 ++++++++-----------------------
>   drivers/fpga/ice40-spi.c         |  10 +-
>   drivers/fpga/machxo2-spi.c       |  10 +-
>   drivers/fpga/socfpga-a10.c       |  16 ++-
>   drivers/fpga/socfpga.c           |  10 +-
>   drivers/fpga/stratix10-soc.c     |  15 +--
>   drivers/fpga/ts73xx-fpga.c       |  10 +-
>   drivers/fpga/xilinx-spi.c        |  12 +--
>   drivers/fpga/zynq-fpga.c         |  16 ++-
>   drivers/fpga/zynqmp-fpga.c       |  10 +-
>   include/linux/fpga/fpga-mgr.h    |  17 ++--
>   15 files changed, 112 insertions(+), 217 deletions(-)
>
> diff --git a/drivers/fpga/altera-cvp.c b/drivers/fpga/altera-cvp.c
> index 4e0edb60bfba..b61020ceb0a1 100644
> --- a/drivers/fpga/altera-cvp.c
> +++ b/drivers/fpga/altera-cvp.c
> @@ -652,19 +652,15 @@ static int altera_cvp_probe(struct pci_dev *pdev,
>   	snprintf(conf->mgr_name, sizeof(conf->mgr_name), "%s @%s",
>   		 ALTERA_CVP_MGR_NAME, pci_name(pdev));
>   
> -	mgr = devm_fpga_mgr_create(&pdev->dev, conf->mgr_name,
> -				   &altera_cvp_ops, conf);
> -	if (!mgr) {
> -		ret = -ENOMEM;
> +	mgr = fpga_mgr_register(&pdev->dev, conf->mgr_name,
> +				&altera_cvp_ops, conf);
> +	if (IS_ERR(mgr)) {
> +		ret = PTR_ERR(mgr);
>   		goto err_unmap;
>   	}
>   
>   	pci_set_drvdata(pdev, mgr);

This is a wrapper around

dev_set_drvdata()

>   
> -	ret = fpga_mgr_register(mgr);
> -	if (ret)
> -		goto err_unmap;
> -
>   	return 0;
>   
>   err_unmap:
> diff --git a/drivers/fpga/altera-pr-ip-core.c b/drivers/fpga/altera-pr-ip-core.c
> index 5b130c4d9882..0980e134cc43 100644
> --- a/drivers/fpga/altera-pr-ip-core.c
> +++ b/drivers/fpga/altera-pr-ip-core.c
> @@ -191,11 +191,11 @@ int alt_pr_register(struct device *dev, void __iomem *reg_base)
>   		(val & ALT_PR_CSR_STATUS_MSK) >> ALT_PR_CSR_STATUS_SFT,
>   		(int)(val & ALT_PR_CSR_PR_START));
>   
> -	mgr = devm_fpga_mgr_create(dev, dev_name(dev), &alt_pr_ops, priv);
using the name of the device
> -	if (!mgr)
> -		return -ENOMEM;
> +	mgr = devm_fpga_mgr_register(dev, dev_name(dev), &alt_pr_ops, priv);
> +	if (IS_ERR(mgr))
> +		return PTR_ERR(mgr);
mgr is unused
>   
> -	return devm_fpga_mgr_register(dev, mgr);
> +	return 0;
>   }
>   EXPORT_SYMBOL_GPL(alt_pr_register);
>   
> diff --git a/drivers/fpga/altera-ps-spi.c b/drivers/fpga/altera-ps-spi.c
> index 23bfd4d1ad0f..d3f77b0312b2 100644
> --- a/drivers/fpga/altera-ps-spi.c
> +++ b/drivers/fpga/altera-ps-spi.c
> @@ -302,12 +302,12 @@ static int altera_ps_probe(struct spi_device *spi)
>   	snprintf(conf->mgr_name, sizeof(conf->mgr_name), "%s %s",
>   		 dev_driver_string(&spi->dev), dev_name(&spi->dev));
a combo of devices name, is that really necessary ?
>   
> -	mgr = devm_fpga_mgr_create(&spi->dev, conf->mgr_name,
> -				   &altera_ps_ops, conf);
> -	if (!mgr)
> -		return -ENOMEM;
> +	mgr = devm_fpga_mgr_register(&spi->dev, conf->mgr_name,
> +				     &altera_ps_ops, conf);
> +	if (IS_ERR(mgr))
> +		return PTR_ERR(mgr);
mgr is unused
>   
> -	return devm_fpga_mgr_register(&spi->dev, mgr);
> +	return 0;
>   }
>   
>   static const struct spi_device_id altera_ps_spi_ids[] = {
> diff --git a/drivers/fpga/dfl-fme-mgr.c b/drivers/fpga/dfl-fme-mgr.c
> index d5861d13b306..d71eba817b3b 100644
> --- a/drivers/fpga/dfl-fme-mgr.c
> +++ b/drivers/fpga/dfl-fme-mgr.c
> @@ -308,14 +308,14 @@ static int fme_mgr_probe(struct platform_device *pdev)
>   
>   	fme_mgr_get_compat_id(priv->ioaddr, compat_id);
>   
> -	mgr = devm_fpga_mgr_create(dev, "DFL FME FPGA Manager",
> -				   &fme_mgr_ops, priv);
> -	if (!mgr)
> -		return -ENOMEM;
> +	mgr = devm_fpga_mgr_register(dev, "DFL FME FPGA Manager",
> +				     &fme_mgr_ops, priv);
drv name is 'dfl-fme-mgr', is this close enough ?
> +	if (IS_ERR(mgr))
> +		return PTR_ERR(mgr);
>   
>   	mgr->compat_id = compat_id;
>   
> -	return devm_fpga_mgr_register(dev, mgr);
> +	return 0;
>   }
>   
>   static struct platform_driver fme_mgr_driver = {
> diff --git a/drivers/fpga/fpga-mgr.c b/drivers/fpga/fpga-mgr.c
> index b85bc47c91a9..22f0dd5c2834 100644
> --- a/drivers/fpga/fpga-mgr.c
> +++ b/drivers/fpga/fpga-mgr.c
> @@ -550,20 +550,20 @@ void fpga_mgr_unlock(struct fpga_manager *mgr)
>   EXPORT_SYMBOL_GPL(fpga_mgr_unlock);
>   
>   /**
> - * fpga_mgr_create - create and initialize a FPGA manager struct
> + * fpga_mgr_register - create and register a FPGA Manager device
>    * @dev:	fpga manager device from pdev

nit, space or tab after ':' ? this is inconsistent everywhere.


>    * @name:	fpga manager name
>    * @mops:	pointer to structure of fpga manager ops
>    * @priv:	fpga manager private data
>    *
> - * The caller of this function is responsible for freeing the struct with
> - * fpga_mgr_free().  Using devm_fpga_mgr_create() instead is recommended.
> + * The caller of this function is responsible for calling fpga_mgr_unregister().
> + * Using devm_fpga_mgr_register instead is recommended.
>    *
> - * Return: pointer to struct fpga_manager or NULL
> + * Returns a struct fpga_manager pointer on success, or ERR_PTR() on error.
>    */
> -struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
> -				     const struct fpga_manager_ops *mops,
> -				     void *priv)
> +struct fpga_manager *
> +fpga_mgr_register(struct device *dev, const char *name,
> +		  const struct fpga_manager_ops *mops, void *priv)
>   {
>   	struct fpga_manager *mgr;
>   	int id, ret;
> @@ -572,21 +572,23 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
>   	    !mops->write_init || (!mops->write && !mops->write_sg) ||
>   	    (mops->write && mops->write_sg)) {
>   		dev_err(dev, "Attempt to register without fpga_manager_ops\n");
> -		return NULL;
> +		return ERR_PTR(-EINVAL);

return -EINVAL;

And similar

The user's error checking is simplified to an int check.

The clunky ERR_PTR()/PTR_ERR() can be removed

>   	}
>   
>   	if (!name || !strlen(name)) {
>   		dev_err(dev, "Attempt to register with no name!\n");
> -		return NULL;
> +		return ERR_PTR(-EINVAL);
>   	}
>   
>   	mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
>   	if (!mgr)
> -		return NULL;
> +		return ERR_PTR(-ENOMEM);
>   
>   	id = ida_simple_get(&fpga_mgr_ida, 0, 0, GFP_KERNEL);
> -	if (id < 0)
> +	if (id < 0) {
> +		ret = id;
>   		goto error_kfree;
> +	}
>   
>   	mutex_init(&mgr->ref_mutex);
>   
> @@ -594,7 +596,6 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,

Just use the device name, do not give the user an option and simplify 
the calling parameters.

mgr->name = dev_name(dev)

>   	mgr->mops = mops;
>   	mgr->priv = priv;
>   
> -	device_initialize(&mgr->dev);
>   	mgr->dev.class = fpga_mgr_class;
>   	mgr->dev.groups = mops->groups;
>   	mgr->dev.parent = dev;
> @@ -605,84 +606,6 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
>   	if (ret)
>   		goto error_device;
>   

The only use of returned mgr seems to be a variant of setting the 
dev->driver_data = mgr

so just make it the default and don't pass back the ptr.

dev_set_drvdata(dev, mgr);

return 0;


Tom



^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v3 1/3] fpga: mgr: Use standard dev_release for class driver
  2021-05-24 18:50 ` [PATCH v3 1/3] fpga: mgr: Use standard dev_release for class driver Russ Weight
  2021-05-24 20:16   ` Tom Rix
@ 2021-05-24 22:39   ` Moritz Fischer
  2021-05-24 23:07     ` Russ Weight
  2021-05-28 15:41   ` Xu Yilun
  2 siblings, 1 reply; 17+ messages in thread
From: Moritz Fischer @ 2021-05-24 22:39 UTC (permalink / raw)
  To: Russ Weight
  Cc: mdf, linux-fpga, trix, lgoncalv, yilun.xu, hao.wu,
	matthew.gerlach, richard.gong

On Mon, May 24, 2021 at 11:50:47AM -0700, Russ Weight wrote:
> The FPGA manager class driver data structure is being treated as a
> managed resource instead of using standard dev_release call-back
> to release the class data structure. This change removes the
> managed resource code for the freeing of the class data structure
> and combines the create() and register() functions into a single
> register() function.
> 
> The devm_fpga_mgr_register() function is retained.
Should this be maybe called devm_fpga_mgr_create_and_register() ?

> 
> Signed-off-by: Russ Weight <russell.h.weight@intel.com>
> ---
> v3:
>   - Cleaned up comment header for fpga_mgr_register()
>   - Fix error return on ida_simple_get() failure
> v2:
>   - Restored devm_fpga_mgr_register() functionality, adapted for the combined
>     create/register functionality.
>   - All previous callers of devm_fpga_mgr_register() will continue to call
>     devm_fpga_mgr_register().
>   - replaced unnecessary ternary operators in return statements with standard
>     if conditions.
> ---
>  drivers/fpga/altera-cvp.c        |  12 +--
>  drivers/fpga/altera-pr-ip-core.c |   8 +-
>  drivers/fpga/altera-ps-spi.c     |  10 +-
>  drivers/fpga/dfl-fme-mgr.c       |  10 +-
>  drivers/fpga/fpga-mgr.c          | 163 ++++++++-----------------------
>  drivers/fpga/ice40-spi.c         |  10 +-
>  drivers/fpga/machxo2-spi.c       |  10 +-
>  drivers/fpga/socfpga-a10.c       |  16 ++-
>  drivers/fpga/socfpga.c           |  10 +-
>  drivers/fpga/stratix10-soc.c     |  15 +--
>  drivers/fpga/ts73xx-fpga.c       |  10 +-
>  drivers/fpga/xilinx-spi.c        |  12 +--
>  drivers/fpga/zynq-fpga.c         |  16 ++-
>  drivers/fpga/zynqmp-fpga.c       |  10 +-
>  include/linux/fpga/fpga-mgr.h    |  17 ++--
>  15 files changed, 112 insertions(+), 217 deletions(-)
> 
> diff --git a/drivers/fpga/altera-cvp.c b/drivers/fpga/altera-cvp.c
> index 4e0edb60bfba..b61020ceb0a1 100644
> --- a/drivers/fpga/altera-cvp.c
> +++ b/drivers/fpga/altera-cvp.c
> @@ -652,19 +652,15 @@ static int altera_cvp_probe(struct pci_dev *pdev,
>  	snprintf(conf->mgr_name, sizeof(conf->mgr_name), "%s @%s",
>  		 ALTERA_CVP_MGR_NAME, pci_name(pdev));
>  
> -	mgr = devm_fpga_mgr_create(&pdev->dev, conf->mgr_name,
> -				   &altera_cvp_ops, conf);
> -	if (!mgr) {
> -		ret = -ENOMEM;
> +	mgr = fpga_mgr_register(&pdev->dev, conf->mgr_name,
> +				&altera_cvp_ops, conf);
> +	if (IS_ERR(mgr)) {
> +		ret = PTR_ERR(mgr);
>  		goto err_unmap;
>  	}
>  
>  	pci_set_drvdata(pdev, mgr);
>  
> -	ret = fpga_mgr_register(mgr);
> -	if (ret)
> -		goto err_unmap;
> -
>  	return 0;
>  
>  err_unmap:
> diff --git a/drivers/fpga/altera-pr-ip-core.c b/drivers/fpga/altera-pr-ip-core.c
> index 5b130c4d9882..0980e134cc43 100644
> --- a/drivers/fpga/altera-pr-ip-core.c
> +++ b/drivers/fpga/altera-pr-ip-core.c
> @@ -191,11 +191,11 @@ int alt_pr_register(struct device *dev, void __iomem *reg_base)
>  		(val & ALT_PR_CSR_STATUS_MSK) >> ALT_PR_CSR_STATUS_SFT,
>  		(int)(val & ALT_PR_CSR_PR_START));
>  
> -	mgr = devm_fpga_mgr_create(dev, dev_name(dev), &alt_pr_ops, priv);
> -	if (!mgr)
> -		return -ENOMEM;
> +	mgr = devm_fpga_mgr_register(dev, dev_name(dev), &alt_pr_ops, priv);
> +	if (IS_ERR(mgr))
> +		return PTR_ERR(mgr);
>  
> -	return devm_fpga_mgr_register(dev, mgr);
> +	return 0;
>  }
>  EXPORT_SYMBOL_GPL(alt_pr_register);
>  
> diff --git a/drivers/fpga/altera-ps-spi.c b/drivers/fpga/altera-ps-spi.c
> index 23bfd4d1ad0f..d3f77b0312b2 100644
> --- a/drivers/fpga/altera-ps-spi.c
> +++ b/drivers/fpga/altera-ps-spi.c
> @@ -302,12 +302,12 @@ static int altera_ps_probe(struct spi_device *spi)
>  	snprintf(conf->mgr_name, sizeof(conf->mgr_name), "%s %s",
>  		 dev_driver_string(&spi->dev), dev_name(&spi->dev));
>  
> -	mgr = devm_fpga_mgr_create(&spi->dev, conf->mgr_name,
> -				   &altera_ps_ops, conf);
> -	if (!mgr)
> -		return -ENOMEM;
> +	mgr = devm_fpga_mgr_register(&spi->dev, conf->mgr_name,
> +				     &altera_ps_ops, conf);
> +	if (IS_ERR(mgr))
> +		return PTR_ERR(mgr);
>  
> -	return devm_fpga_mgr_register(&spi->dev, mgr);
> +	return 0;
>  }
>  
>  static const struct spi_device_id altera_ps_spi_ids[] = {
> diff --git a/drivers/fpga/dfl-fme-mgr.c b/drivers/fpga/dfl-fme-mgr.c
> index d5861d13b306..d71eba817b3b 100644
> --- a/drivers/fpga/dfl-fme-mgr.c
> +++ b/drivers/fpga/dfl-fme-mgr.c
> @@ -308,14 +308,14 @@ static int fme_mgr_probe(struct platform_device *pdev)
>  
>  	fme_mgr_get_compat_id(priv->ioaddr, compat_id);
>  
> -	mgr = devm_fpga_mgr_create(dev, "DFL FME FPGA Manager",
> -				   &fme_mgr_ops, priv);
> -	if (!mgr)
> -		return -ENOMEM;
> +	mgr = devm_fpga_mgr_register(dev, "DFL FME FPGA Manager",
> +				     &fme_mgr_ops, priv);
> +	if (IS_ERR(mgr))
> +		return PTR_ERR(mgr);
>  
>  	mgr->compat_id = compat_id;
>  
> -	return devm_fpga_mgr_register(dev, mgr);
> +	return 0;
>  }
>  
>  static struct platform_driver fme_mgr_driver = {
> diff --git a/drivers/fpga/fpga-mgr.c b/drivers/fpga/fpga-mgr.c
> index b85bc47c91a9..22f0dd5c2834 100644
> --- a/drivers/fpga/fpga-mgr.c
> +++ b/drivers/fpga/fpga-mgr.c
> @@ -550,20 +550,20 @@ void fpga_mgr_unlock(struct fpga_manager *mgr)
>  EXPORT_SYMBOL_GPL(fpga_mgr_unlock);
>  
>  /**
> - * fpga_mgr_create - create and initialize a FPGA manager struct
> + * fpga_mgr_register - create and register a FPGA Manager device
>   * @dev:	fpga manager device from pdev
>   * @name:	fpga manager name
>   * @mops:	pointer to structure of fpga manager ops
>   * @priv:	fpga manager private data
>   *
> - * The caller of this function is responsible for freeing the struct with
> - * fpga_mgr_free().  Using devm_fpga_mgr_create() instead is recommended.
> + * The caller of this function is responsible for calling fpga_mgr_unregister().
> + * Using devm_fpga_mgr_register instead is recommended.
>   *
> - * Return: pointer to struct fpga_manager or NULL
> + * Returns a struct fpga_manager pointer on success, or ERR_PTR() on error.
>   */
> -struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
> -				     const struct fpga_manager_ops *mops,
> -				     void *priv)
> +struct fpga_manager *
> +fpga_mgr_register(struct device *dev, const char *name,
> +		  const struct fpga_manager_ops *mops, void *priv)
>  {
>  	struct fpga_manager *mgr;
>  	int id, ret;
> @@ -572,21 +572,23 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
>  	    !mops->write_init || (!mops->write && !mops->write_sg) ||
>  	    (mops->write && mops->write_sg)) {
>  		dev_err(dev, "Attempt to register without fpga_manager_ops\n");
> -		return NULL;
> +		return ERR_PTR(-EINVAL);
>  	}
>  
>  	if (!name || !strlen(name)) {
>  		dev_err(dev, "Attempt to register with no name!\n");
> -		return NULL;
> +		return ERR_PTR(-EINVAL);
>  	}
>  
>  	mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
>  	if (!mgr)
> -		return NULL;
> +		return ERR_PTR(-ENOMEM);
>  
>  	id = ida_simple_get(&fpga_mgr_ida, 0, 0, GFP_KERNEL);
> -	if (id < 0)
> +	if (id < 0) {
> +		ret = id;
>  		goto error_kfree;
> +	}
>  
>  	mutex_init(&mgr->ref_mutex);
>  
> @@ -594,7 +596,6 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
>  	mgr->mops = mops;
>  	mgr->priv = priv;
>  
> -	device_initialize(&mgr->dev);
>  	mgr->dev.class = fpga_mgr_class;
>  	mgr->dev.groups = mops->groups;
>  	mgr->dev.parent = dev;
> @@ -605,84 +606,6 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
>  	if (ret)
>  		goto error_device;
>  
> -	return mgr;
> -
> -error_device:
> -	ida_simple_remove(&fpga_mgr_ida, id);
> -error_kfree:
> -	kfree(mgr);
> -
> -	return NULL;
> -}
> -EXPORT_SYMBOL_GPL(fpga_mgr_create);
> -
> -/**
> - * fpga_mgr_free - free a FPGA manager created with fpga_mgr_create()
> - * @mgr:	fpga manager struct
> - */
> -void fpga_mgr_free(struct fpga_manager *mgr)
> -{
> -	ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
> -	kfree(mgr);
> -}
> -EXPORT_SYMBOL_GPL(fpga_mgr_free);
> -
> -static void devm_fpga_mgr_release(struct device *dev, void *res)
> -{
> -	struct fpga_mgr_devres *dr = res;
> -
> -	fpga_mgr_free(dr->mgr);
> -}
> -
> -/**
> - * devm_fpga_mgr_create - create and initialize a managed FPGA manager struct
> - * @dev:	fpga manager device from pdev
> - * @name:	fpga manager name
> - * @mops:	pointer to structure of fpga manager ops
> - * @priv:	fpga manager private data
> - *
> - * This function is intended for use in a FPGA manager driver's probe function.
> - * After the manager driver creates the manager struct with
> - * devm_fpga_mgr_create(), it should register it with fpga_mgr_register().  The
> - * manager driver's remove function should call fpga_mgr_unregister().  The
> - * manager struct allocated with this function will be freed automatically on
> - * driver detach.  This includes the case of a probe function returning error
> - * before calling fpga_mgr_register(), the struct will still get cleaned up.
> - *
> - * Return: pointer to struct fpga_manager or NULL
> - */
> -struct fpga_manager *devm_fpga_mgr_create(struct device *dev, const char *name,
> -					  const struct fpga_manager_ops *mops,
> -					  void *priv)
> -{
> -	struct fpga_mgr_devres *dr;
> -
> -	dr = devres_alloc(devm_fpga_mgr_release, sizeof(*dr), GFP_KERNEL);
> -	if (!dr)
> -		return NULL;
> -
> -	dr->mgr = fpga_mgr_create(dev, name, mops, priv);
> -	if (!dr->mgr) {
> -		devres_free(dr);
> -		return NULL;
> -	}
> -
> -	devres_add(dev, dr);
> -
> -	return dr->mgr;
> -}
> -EXPORT_SYMBOL_GPL(devm_fpga_mgr_create);
> -
> -/**
> - * fpga_mgr_register - register a FPGA manager
> - * @mgr: fpga manager struct
> - *
> - * Return: 0 on success, negative error code otherwise.
> - */
> -int fpga_mgr_register(struct fpga_manager *mgr)
> -{
> -	int ret;
> -
>  	/*
>  	 * Initialize framework state by requesting low level driver read state
>  	 * from device.  FPGA may be in reset mode or may have been programmed
> @@ -690,18 +613,20 @@ int fpga_mgr_register(struct fpga_manager *mgr)
>  	 */
>  	mgr->state = mgr->mops->state(mgr);
>  
> -	ret = device_add(&mgr->dev);
> -	if (ret)
> -		goto error_device;
> -
> -	dev_info(&mgr->dev, "%s registered\n", mgr->name);
> +	ret = device_register(&mgr->dev);
> +	if (ret) {
> +		put_device(&mgr->dev);
> +		return ERR_PTR(ret);
> +	}
>  
> -	return 0;
> +	return mgr;
>  
>  error_device:
> -	ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
> +	ida_simple_remove(&fpga_mgr_ida, id);
> +error_kfree:
> +	kfree(mgr);
>  
> -	return ret;
> +	return ERR_PTR(ret);
>  }
>  EXPORT_SYMBOL_GPL(fpga_mgr_register);
>  
> @@ -726,14 +651,6 @@ void fpga_mgr_unregister(struct fpga_manager *mgr)
>  }
>  EXPORT_SYMBOL_GPL(fpga_mgr_unregister);
>  
> -static int fpga_mgr_devres_match(struct device *dev, void *res,
> -				 void *match_data)
> -{
> -	struct fpga_mgr_devres *dr = res;
> -
> -	return match_data == dr->mgr;
> -}
> -
>  static void devm_fpga_mgr_unregister(struct device *dev, void *res)
>  {
>  	struct fpga_mgr_devres *dr = res;
> @@ -743,44 +660,44 @@ static void devm_fpga_mgr_unregister(struct device *dev, void *res)
>  
>  /**
>   * devm_fpga_mgr_register - resource managed variant of fpga_mgr_register()
> - * @dev: managing device for this FPGA manager
> - * @mgr: fpga manager struct
> + * @dev:	fpga manager device from pdev
> + * @name:	fpga manager name
> + * @mops:	pointer to structure of fpga manager ops
> + * @priv:	fpga manager private data
>   *
>   * This is the devres variant of fpga_mgr_register() for which the unregister
>   * function will be called automatically when the managing device is detached.
>   */
> -int devm_fpga_mgr_register(struct device *dev, struct fpga_manager *mgr)
> +struct fpga_manager *
> +devm_fpga_mgr_register(struct device *dev, const char *name,
> +		       const struct fpga_manager_ops *mops, void *priv)
>  {
>  	struct fpga_mgr_devres *dr;
> -	int ret;
> -
> -	/*
> -	 * Make sure that the struct fpga_manager * that is passed in is
> -	 * managed itself.
> -	 */
> -	if (WARN_ON(!devres_find(dev, devm_fpga_mgr_release,
> -				 fpga_mgr_devres_match, mgr)))
> -		return -EINVAL;
> +	struct fpga_manager *mgr;
>  
>  	dr = devres_alloc(devm_fpga_mgr_unregister, sizeof(*dr), GFP_KERNEL);
>  	if (!dr)
> -		return -ENOMEM;
> +		return ERR_PTR(-ENOMEM);
>  
> -	ret = fpga_mgr_register(mgr);
> -	if (ret) {
> +	mgr = fpga_mgr_register(dev, name, mops, priv);
> +	if (IS_ERR(mgr)) {
>  		devres_free(dr);
> -		return ret;
> +		return mgr;
>  	}
>  
>  	dr->mgr = mgr;
>  	devres_add(dev, dr);
>  
> -	return 0;
> +	return mgr;
>  }
>  EXPORT_SYMBOL_GPL(devm_fpga_mgr_register);
>  
>  static void fpga_mgr_dev_release(struct device *dev)
>  {
> +	struct fpga_manager *mgr = to_fpga_manager(dev);
> +
> +	ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
> +	kfree(mgr);
>  }
>  
>  static int __init fpga_mgr_class_init(void)
> diff --git a/drivers/fpga/ice40-spi.c b/drivers/fpga/ice40-spi.c
> index 69dec5af23c3..91ac25f339f3 100644
> --- a/drivers/fpga/ice40-spi.c
> +++ b/drivers/fpga/ice40-spi.c
> @@ -178,12 +178,12 @@ static int ice40_fpga_probe(struct spi_device *spi)
>  		return ret;
>  	}
>  
> -	mgr = devm_fpga_mgr_create(dev, "Lattice iCE40 FPGA Manager",
> -				   &ice40_fpga_ops, priv);
> -	if (!mgr)
> -		return -ENOMEM;
> +	mgr = devm_fpga_mgr_register(dev, "Lattice iCE40 FPGA Manager",
> +				     &ice40_fpga_ops, priv);
> +	if (IS_ERR(mgr))
> +		return PTR_ERR(mgr);
>  
> -	return devm_fpga_mgr_register(dev, mgr);
> +	return 0;
>  }
>  
>  static const struct of_device_id ice40_fpga_of_match[] = {
> diff --git a/drivers/fpga/machxo2-spi.c b/drivers/fpga/machxo2-spi.c
> index 114a64d2b7a4..0137c1e2e706 100644
> --- a/drivers/fpga/machxo2-spi.c
> +++ b/drivers/fpga/machxo2-spi.c
> @@ -366,12 +366,12 @@ static int machxo2_spi_probe(struct spi_device *spi)
>  		return -EINVAL;
>  	}
>  
> -	mgr = devm_fpga_mgr_create(dev, "Lattice MachXO2 SPI FPGA Manager",
> -				   &machxo2_ops, spi);
> -	if (!mgr)
> -		return -ENOMEM;
> +	mgr = devm_fpga_mgr_register(dev, "Lattice MachXO2 SPI FPGA Manager",
> +				     &machxo2_ops, spi);
> +	if (IS_ERR(mgr))
> +		return PTR_ERR(mgr);
>  
> -	return devm_fpga_mgr_register(dev, mgr);
> +	return 0;
>  }
>  
>  static const struct of_device_id of_match[] = {
> diff --git a/drivers/fpga/socfpga-a10.c b/drivers/fpga/socfpga-a10.c
> index 573d88bdf730..ac8e89b8a5cc 100644
> --- a/drivers/fpga/socfpga-a10.c
> +++ b/drivers/fpga/socfpga-a10.c
> @@ -508,19 +508,15 @@ static int socfpga_a10_fpga_probe(struct platform_device *pdev)
>  		return -EBUSY;
>  	}
>  
> -	mgr = devm_fpga_mgr_create(dev, "SoCFPGA Arria10 FPGA Manager",
> -				   &socfpga_a10_fpga_mgr_ops, priv);
> -	if (!mgr)
> -		return -ENOMEM;
> -
> -	platform_set_drvdata(pdev, mgr);
> -
> -	ret = fpga_mgr_register(mgr);
> -	if (ret) {
> +	mgr = fpga_mgr_register(dev, "SoCFPGA Arria10 FPGA Manager",
> +				&socfpga_a10_fpga_mgr_ops, priv);
> +	if (IS_ERR(mgr)) {
>  		clk_disable_unprepare(priv->clk);
> -		return ret;
> +		return PTR_ERR(mgr);
>  	}
>  
> +	platform_set_drvdata(pdev, mgr);
> +
>  	return 0;
>  }
>  
> diff --git a/drivers/fpga/socfpga.c b/drivers/fpga/socfpga.c
> index 1f467173fc1f..ebfa9f4d24ef 100644
> --- a/drivers/fpga/socfpga.c
> +++ b/drivers/fpga/socfpga.c
> @@ -571,12 +571,12 @@ static int socfpga_fpga_probe(struct platform_device *pdev)
>  	if (ret)
>  		return ret;
>  
> -	mgr = devm_fpga_mgr_create(dev, "Altera SOCFPGA FPGA Manager",
> -				   &socfpga_fpga_ops, priv);
> -	if (!mgr)
> -		return -ENOMEM;
> +	mgr = devm_fpga_mgr_register(dev, "Altera SOCFPGA FPGA Manager",
> +				     &socfpga_fpga_ops, priv);
> +	if (IS_ERR(mgr))
> +		return PTR_ERR(mgr);
>  
> -	return devm_fpga_mgr_register(dev, mgr);
> +	return 0;
>  }
>  
>  #ifdef CONFIG_OF
> diff --git a/drivers/fpga/stratix10-soc.c b/drivers/fpga/stratix10-soc.c
> index 657a70c5fc99..e0bd91b3f7f8 100644
> --- a/drivers/fpga/stratix10-soc.c
> +++ b/drivers/fpga/stratix10-soc.c
> @@ -425,18 +425,11 @@ static int s10_probe(struct platform_device *pdev)
>  
>  	init_completion(&priv->status_return_completion);
>  
> -	mgr = fpga_mgr_create(dev, "Stratix10 SOC FPGA Manager",
> -			      &s10_ops, priv);
> -	if (!mgr) {
> -		dev_err(dev, "unable to create FPGA manager\n");
> -		ret = -ENOMEM;
> -		goto probe_err;
> -	}
> -
> -	ret = fpga_mgr_register(mgr);
> -	if (ret) {
> +	mgr = fpga_mgr_register(dev, "Stratix10 SOC FPGA Manager",
> +				&s10_ops, priv);
> +	if (IS_ERR(mgr)) {
>  		dev_err(dev, "unable to register FPGA manager\n");
> -		fpga_mgr_free(mgr);
> +		ret = PTR_ERR(mgr);
>  		goto probe_err;
>  	}
>  
> diff --git a/drivers/fpga/ts73xx-fpga.c b/drivers/fpga/ts73xx-fpga.c
> index 101f016c6ed8..5efbec2b9f9f 100644
> --- a/drivers/fpga/ts73xx-fpga.c
> +++ b/drivers/fpga/ts73xx-fpga.c
> @@ -122,12 +122,12 @@ static int ts73xx_fpga_probe(struct platform_device *pdev)
>  	if (IS_ERR(priv->io_base))
>  		return PTR_ERR(priv->io_base);
>  
> -	mgr = devm_fpga_mgr_create(kdev, "TS-73xx FPGA Manager",
> -				   &ts73xx_fpga_ops, priv);
> -	if (!mgr)
> -		return -ENOMEM;
> +	mgr = devm_fpga_mgr_register(kdev, "TS-73xx FPGA Manager",
> +				     &ts73xx_fpga_ops, priv);
> +	if (IS_ERR(mgr))
> +		return PTR_ERR(mgr);
>  
> -	return devm_fpga_mgr_register(kdev, mgr);
> +	return 0;
>  }
>  
>  static struct platform_driver ts73xx_fpga_driver = {
> diff --git a/drivers/fpga/xilinx-spi.c b/drivers/fpga/xilinx-spi.c
> index fee4d0abf6bf..efda475f7a98 100644
> --- a/drivers/fpga/xilinx-spi.c
> +++ b/drivers/fpga/xilinx-spi.c
> @@ -247,13 +247,13 @@ static int xilinx_spi_probe(struct spi_device *spi)
>  		return dev_err_probe(&spi->dev, PTR_ERR(conf->done),
>  				     "Failed to get DONE gpio\n");
>  
> -	mgr = devm_fpga_mgr_create(&spi->dev,
> -				   "Xilinx Slave Serial FPGA Manager",
> -				   &xilinx_spi_ops, conf);
> -	if (!mgr)
> -		return -ENOMEM;
> +	mgr = devm_fpga_mgr_register(&spi->dev,
> +				     "Xilinx Slave Serial FPGA Manager",
> +				     &xilinx_spi_ops, conf);
> +	if (IS_ERR(mgr))
> +		return PTR_ERR(mgr);
>  
> -	return devm_fpga_mgr_register(&spi->dev, mgr);
> +	return 0;
>  }
>  
>  static const struct of_device_id xlnx_spi_of_match[] = {
> diff --git a/drivers/fpga/zynq-fpga.c b/drivers/fpga/zynq-fpga.c
> index 07fa8d9ec675..74bbb9710f4e 100644
> --- a/drivers/fpga/zynq-fpga.c
> +++ b/drivers/fpga/zynq-fpga.c
> @@ -609,20 +609,16 @@ static int zynq_fpga_probe(struct platform_device *pdev)
>  
>  	clk_disable(priv->clk);
>  
> -	mgr = devm_fpga_mgr_create(dev, "Xilinx Zynq FPGA Manager",
> -				   &zynq_fpga_ops, priv);
> -	if (!mgr)
> -		return -ENOMEM;
> -
> -	platform_set_drvdata(pdev, mgr);
> -
> -	err = fpga_mgr_register(mgr);
> -	if (err) {
> +	mgr = fpga_mgr_register(dev, "Xilinx Zynq FPGA Manager",
> +				&zynq_fpga_ops, priv);
> +	if (IS_ERR(mgr)) {
>  		dev_err(dev, "unable to register FPGA manager\n");
>  		clk_unprepare(priv->clk);
> -		return err;
> +		return PTR_ERR(mgr);
>  	}
>  
> +	platform_set_drvdata(pdev, mgr);
> +
>  	return 0;
>  }
>  
> diff --git a/drivers/fpga/zynqmp-fpga.c b/drivers/fpga/zynqmp-fpga.c
> index 125743c9797f..1a6d520985be 100644
> --- a/drivers/fpga/zynqmp-fpga.c
> +++ b/drivers/fpga/zynqmp-fpga.c
> @@ -102,12 +102,12 @@ static int zynqmp_fpga_probe(struct platform_device *pdev)
>  
>  	priv->dev = dev;
>  
> -	mgr = devm_fpga_mgr_create(dev, "Xilinx ZynqMP FPGA Manager",
> -				   &zynqmp_fpga_ops, priv);
> -	if (!mgr)
> -		return -ENOMEM;
> +	mgr = devm_fpga_mgr_register(dev, "Xilinx ZynqMP FPGA Manager",
> +				     &zynqmp_fpga_ops, priv);
> +	if (IS_ERR(mgr))
> +		return PTR_ERR(mgr);
>  
> -	return devm_fpga_mgr_register(dev, mgr);
> +	return 0;
>  }
>  
>  static const struct of_device_id zynqmp_fpga_of_match[] = {
> diff --git a/include/linux/fpga/fpga-mgr.h b/include/linux/fpga/fpga-mgr.h
> index 2bc3030a69e5..d1ccf8f8c38a 100644
> --- a/include/linux/fpga/fpga-mgr.h
> +++ b/include/linux/fpga/fpga-mgr.h
> @@ -191,17 +191,14 @@ struct fpga_manager *fpga_mgr_get(struct device *dev);
>  
>  void fpga_mgr_put(struct fpga_manager *mgr);
>  
> -struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
> -				     const struct fpga_manager_ops *mops,
> -				     void *priv);
> -void fpga_mgr_free(struct fpga_manager *mgr);
> -int fpga_mgr_register(struct fpga_manager *mgr);
> +struct fpga_manager *fpga_mgr_register(struct device *dev, const char *name,
> +				       const struct fpga_manager_ops *mops,
> +				       void *priv);
>  void fpga_mgr_unregister(struct fpga_manager *mgr);
>  
> -int devm_fpga_mgr_register(struct device *dev, struct fpga_manager *mgr);
> -
> -struct fpga_manager *devm_fpga_mgr_create(struct device *dev, const char *name,
> -					  const struct fpga_manager_ops *mops,
> -					  void *priv);
> +struct fpga_manager *
> +devm_fpga_mgr_register(struct device *dev, const char *name,
> +		       const struct fpga_manager_ops *mops,
> +		       void *priv);
>  
>  #endif /*_LINUX_FPGA_MGR_H */
> -- 
> 2.25.1
> 

Moritz

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v3 1/3] fpga: mgr: Use standard dev_release for class driver
  2021-05-24 22:39   ` Moritz Fischer
@ 2021-05-24 23:07     ` Russ Weight
  2021-05-25 17:39       ` Russ Weight
  0 siblings, 1 reply; 17+ messages in thread
From: Russ Weight @ 2021-05-24 23:07 UTC (permalink / raw)
  To: Moritz Fischer
  Cc: linux-fpga, trix, lgoncalv, yilun.xu, hao.wu, matthew.gerlach,
	richard.gong



On 5/24/21 3:39 PM, Moritz Fischer wrote:
> On Mon, May 24, 2021 at 11:50:47AM -0700, Russ Weight wrote:
>> The FPGA manager class driver data structure is being treated as a
>> managed resource instead of using standard dev_release call-back
>> to release the class data structure. This change removes the
>> managed resource code for the freeing of the class data structure
>> and combines the create() and register() functions into a single
>> register() function.
>>
>> The devm_fpga_mgr_register() function is retained.
> Should this be maybe called devm_fpga_mgr_create_and_register() ?
Yes - I think that is more descriptive. I'll make that change.

- Russ

>
>> Signed-off-by: Russ Weight <russell.h.weight@intel.com>
>> ---
>> v3:
>>   - Cleaned up comment header for fpga_mgr_register()
>>   - Fix error return on ida_simple_get() failure
>> v2:
>>   - Restored devm_fpga_mgr_register() functionality, adapted for the combined
>>     create/register functionality.
>>   - All previous callers of devm_fpga_mgr_register() will continue to call
>>     devm_fpga_mgr_register().
>>   - replaced unnecessary ternary operators in return statements with standard
>>     if conditions.
>> ---
>>  drivers/fpga/altera-cvp.c        |  12 +--
>>  drivers/fpga/altera-pr-ip-core.c |   8 +-
>>  drivers/fpga/altera-ps-spi.c     |  10 +-
>>  drivers/fpga/dfl-fme-mgr.c       |  10 +-
>>  drivers/fpga/fpga-mgr.c          | 163 ++++++++-----------------------
>>  drivers/fpga/ice40-spi.c         |  10 +-
>>  drivers/fpga/machxo2-spi.c       |  10 +-
>>  drivers/fpga/socfpga-a10.c       |  16 ++-
>>  drivers/fpga/socfpga.c           |  10 +-
>>  drivers/fpga/stratix10-soc.c     |  15 +--
>>  drivers/fpga/ts73xx-fpga.c       |  10 +-
>>  drivers/fpga/xilinx-spi.c        |  12 +--
>>  drivers/fpga/zynq-fpga.c         |  16 ++-
>>  drivers/fpga/zynqmp-fpga.c       |  10 +-
>>  include/linux/fpga/fpga-mgr.h    |  17 ++--
>>  15 files changed, 112 insertions(+), 217 deletions(-)
>>
>> diff --git a/drivers/fpga/altera-cvp.c b/drivers/fpga/altera-cvp.c
>> index 4e0edb60bfba..b61020ceb0a1 100644
>> --- a/drivers/fpga/altera-cvp.c
>> +++ b/drivers/fpga/altera-cvp.c
>> @@ -652,19 +652,15 @@ static int altera_cvp_probe(struct pci_dev *pdev,
>>  	snprintf(conf->mgr_name, sizeof(conf->mgr_name), "%s @%s",
>>  		 ALTERA_CVP_MGR_NAME, pci_name(pdev));
>>  
>> -	mgr = devm_fpga_mgr_create(&pdev->dev, conf->mgr_name,
>> -				   &altera_cvp_ops, conf);
>> -	if (!mgr) {
>> -		ret = -ENOMEM;
>> +	mgr = fpga_mgr_register(&pdev->dev, conf->mgr_name,
>> +				&altera_cvp_ops, conf);
>> +	if (IS_ERR(mgr)) {
>> +		ret = PTR_ERR(mgr);
>>  		goto err_unmap;
>>  	}
>>  
>>  	pci_set_drvdata(pdev, mgr);
>>  
>> -	ret = fpga_mgr_register(mgr);
>> -	if (ret)
>> -		goto err_unmap;
>> -
>>  	return 0;
>>  
>>  err_unmap:
>> diff --git a/drivers/fpga/altera-pr-ip-core.c b/drivers/fpga/altera-pr-ip-core.c
>> index 5b130c4d9882..0980e134cc43 100644
>> --- a/drivers/fpga/altera-pr-ip-core.c
>> +++ b/drivers/fpga/altera-pr-ip-core.c
>> @@ -191,11 +191,11 @@ int alt_pr_register(struct device *dev, void __iomem *reg_base)
>>  		(val & ALT_PR_CSR_STATUS_MSK) >> ALT_PR_CSR_STATUS_SFT,
>>  		(int)(val & ALT_PR_CSR_PR_START));
>>  
>> -	mgr = devm_fpga_mgr_create(dev, dev_name(dev), &alt_pr_ops, priv);
>> -	if (!mgr)
>> -		return -ENOMEM;
>> +	mgr = devm_fpga_mgr_register(dev, dev_name(dev), &alt_pr_ops, priv);
>> +	if (IS_ERR(mgr))
>> +		return PTR_ERR(mgr);
>>  
>> -	return devm_fpga_mgr_register(dev, mgr);
>> +	return 0;
>>  }
>>  EXPORT_SYMBOL_GPL(alt_pr_register);
>>  
>> diff --git a/drivers/fpga/altera-ps-spi.c b/drivers/fpga/altera-ps-spi.c
>> index 23bfd4d1ad0f..d3f77b0312b2 100644
>> --- a/drivers/fpga/altera-ps-spi.c
>> +++ b/drivers/fpga/altera-ps-spi.c
>> @@ -302,12 +302,12 @@ static int altera_ps_probe(struct spi_device *spi)
>>  	snprintf(conf->mgr_name, sizeof(conf->mgr_name), "%s %s",
>>  		 dev_driver_string(&spi->dev), dev_name(&spi->dev));
>>  
>> -	mgr = devm_fpga_mgr_create(&spi->dev, conf->mgr_name,
>> -				   &altera_ps_ops, conf);
>> -	if (!mgr)
>> -		return -ENOMEM;
>> +	mgr = devm_fpga_mgr_register(&spi->dev, conf->mgr_name,
>> +				     &altera_ps_ops, conf);
>> +	if (IS_ERR(mgr))
>> +		return PTR_ERR(mgr);
>>  
>> -	return devm_fpga_mgr_register(&spi->dev, mgr);
>> +	return 0;
>>  }
>>  
>>  static const struct spi_device_id altera_ps_spi_ids[] = {
>> diff --git a/drivers/fpga/dfl-fme-mgr.c b/drivers/fpga/dfl-fme-mgr.c
>> index d5861d13b306..d71eba817b3b 100644
>> --- a/drivers/fpga/dfl-fme-mgr.c
>> +++ b/drivers/fpga/dfl-fme-mgr.c
>> @@ -308,14 +308,14 @@ static int fme_mgr_probe(struct platform_device *pdev)
>>  
>>  	fme_mgr_get_compat_id(priv->ioaddr, compat_id);
>>  
>> -	mgr = devm_fpga_mgr_create(dev, "DFL FME FPGA Manager",
>> -				   &fme_mgr_ops, priv);
>> -	if (!mgr)
>> -		return -ENOMEM;
>> +	mgr = devm_fpga_mgr_register(dev, "DFL FME FPGA Manager",
>> +				     &fme_mgr_ops, priv);
>> +	if (IS_ERR(mgr))
>> +		return PTR_ERR(mgr);
>>  
>>  	mgr->compat_id = compat_id;
>>  
>> -	return devm_fpga_mgr_register(dev, mgr);
>> +	return 0;
>>  }
>>  
>>  static struct platform_driver fme_mgr_driver = {
>> diff --git a/drivers/fpga/fpga-mgr.c b/drivers/fpga/fpga-mgr.c
>> index b85bc47c91a9..22f0dd5c2834 100644
>> --- a/drivers/fpga/fpga-mgr.c
>> +++ b/drivers/fpga/fpga-mgr.c
>> @@ -550,20 +550,20 @@ void fpga_mgr_unlock(struct fpga_manager *mgr)
>>  EXPORT_SYMBOL_GPL(fpga_mgr_unlock);
>>  
>>  /**
>> - * fpga_mgr_create - create and initialize a FPGA manager struct
>> + * fpga_mgr_register - create and register a FPGA Manager device
>>   * @dev:	fpga manager device from pdev
>>   * @name:	fpga manager name
>>   * @mops:	pointer to structure of fpga manager ops
>>   * @priv:	fpga manager private data
>>   *
>> - * The caller of this function is responsible for freeing the struct with
>> - * fpga_mgr_free().  Using devm_fpga_mgr_create() instead is recommended.
>> + * The caller of this function is responsible for calling fpga_mgr_unregister().
>> + * Using devm_fpga_mgr_register instead is recommended.
>>   *
>> - * Return: pointer to struct fpga_manager or NULL
>> + * Returns a struct fpga_manager pointer on success, or ERR_PTR() on error.
>>   */
>> -struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
>> -				     const struct fpga_manager_ops *mops,
>> -				     void *priv)
>> +struct fpga_manager *
>> +fpga_mgr_register(struct device *dev, const char *name,
>> +		  const struct fpga_manager_ops *mops, void *priv)
>>  {
>>  	struct fpga_manager *mgr;
>>  	int id, ret;
>> @@ -572,21 +572,23 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
>>  	    !mops->write_init || (!mops->write && !mops->write_sg) ||
>>  	    (mops->write && mops->write_sg)) {
>>  		dev_err(dev, "Attempt to register without fpga_manager_ops\n");
>> -		return NULL;
>> +		return ERR_PTR(-EINVAL);
>>  	}
>>  
>>  	if (!name || !strlen(name)) {
>>  		dev_err(dev, "Attempt to register with no name!\n");
>> -		return NULL;
>> +		return ERR_PTR(-EINVAL);
>>  	}
>>  
>>  	mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
>>  	if (!mgr)
>> -		return NULL;
>> +		return ERR_PTR(-ENOMEM);
>>  
>>  	id = ida_simple_get(&fpga_mgr_ida, 0, 0, GFP_KERNEL);
>> -	if (id < 0)
>> +	if (id < 0) {
>> +		ret = id;
>>  		goto error_kfree;
>> +	}
>>  
>>  	mutex_init(&mgr->ref_mutex);
>>  
>> @@ -594,7 +596,6 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
>>  	mgr->mops = mops;
>>  	mgr->priv = priv;
>>  
>> -	device_initialize(&mgr->dev);
>>  	mgr->dev.class = fpga_mgr_class;
>>  	mgr->dev.groups = mops->groups;
>>  	mgr->dev.parent = dev;
>> @@ -605,84 +606,6 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
>>  	if (ret)
>>  		goto error_device;
>>  
>> -	return mgr;
>> -
>> -error_device:
>> -	ida_simple_remove(&fpga_mgr_ida, id);
>> -error_kfree:
>> -	kfree(mgr);
>> -
>> -	return NULL;
>> -}
>> -EXPORT_SYMBOL_GPL(fpga_mgr_create);
>> -
>> -/**
>> - * fpga_mgr_free - free a FPGA manager created with fpga_mgr_create()
>> - * @mgr:	fpga manager struct
>> - */
>> -void fpga_mgr_free(struct fpga_manager *mgr)
>> -{
>> -	ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
>> -	kfree(mgr);
>> -}
>> -EXPORT_SYMBOL_GPL(fpga_mgr_free);
>> -
>> -static void devm_fpga_mgr_release(struct device *dev, void *res)
>> -{
>> -	struct fpga_mgr_devres *dr = res;
>> -
>> -	fpga_mgr_free(dr->mgr);
>> -}
>> -
>> -/**
>> - * devm_fpga_mgr_create - create and initialize a managed FPGA manager struct
>> - * @dev:	fpga manager device from pdev
>> - * @name:	fpga manager name
>> - * @mops:	pointer to structure of fpga manager ops
>> - * @priv:	fpga manager private data
>> - *
>> - * This function is intended for use in a FPGA manager driver's probe function.
>> - * After the manager driver creates the manager struct with
>> - * devm_fpga_mgr_create(), it should register it with fpga_mgr_register().  The
>> - * manager driver's remove function should call fpga_mgr_unregister().  The
>> - * manager struct allocated with this function will be freed automatically on
>> - * driver detach.  This includes the case of a probe function returning error
>> - * before calling fpga_mgr_register(), the struct will still get cleaned up.
>> - *
>> - * Return: pointer to struct fpga_manager or NULL
>> - */
>> -struct fpga_manager *devm_fpga_mgr_create(struct device *dev, const char *name,
>> -					  const struct fpga_manager_ops *mops,
>> -					  void *priv)
>> -{
>> -	struct fpga_mgr_devres *dr;
>> -
>> -	dr = devres_alloc(devm_fpga_mgr_release, sizeof(*dr), GFP_KERNEL);
>> -	if (!dr)
>> -		return NULL;
>> -
>> -	dr->mgr = fpga_mgr_create(dev, name, mops, priv);
>> -	if (!dr->mgr) {
>> -		devres_free(dr);
>> -		return NULL;
>> -	}
>> -
>> -	devres_add(dev, dr);
>> -
>> -	return dr->mgr;
>> -}
>> -EXPORT_SYMBOL_GPL(devm_fpga_mgr_create);
>> -
>> -/**
>> - * fpga_mgr_register - register a FPGA manager
>> - * @mgr: fpga manager struct
>> - *
>> - * Return: 0 on success, negative error code otherwise.
>> - */
>> -int fpga_mgr_register(struct fpga_manager *mgr)
>> -{
>> -	int ret;
>> -
>>  	/*
>>  	 * Initialize framework state by requesting low level driver read state
>>  	 * from device.  FPGA may be in reset mode or may have been programmed
>> @@ -690,18 +613,20 @@ int fpga_mgr_register(struct fpga_manager *mgr)
>>  	 */
>>  	mgr->state = mgr->mops->state(mgr);
>>  
>> -	ret = device_add(&mgr->dev);
>> -	if (ret)
>> -		goto error_device;
>> -
>> -	dev_info(&mgr->dev, "%s registered\n", mgr->name);
>> +	ret = device_register(&mgr->dev);
>> +	if (ret) {
>> +		put_device(&mgr->dev);
>> +		return ERR_PTR(ret);
>> +	}
>>  
>> -	return 0;
>> +	return mgr;
>>  
>>  error_device:
>> -	ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
>> +	ida_simple_remove(&fpga_mgr_ida, id);
>> +error_kfree:
>> +	kfree(mgr);
>>  
>> -	return ret;
>> +	return ERR_PTR(ret);
>>  }
>>  EXPORT_SYMBOL_GPL(fpga_mgr_register);
>>  
>> @@ -726,14 +651,6 @@ void fpga_mgr_unregister(struct fpga_manager *mgr)
>>  }
>>  EXPORT_SYMBOL_GPL(fpga_mgr_unregister);
>>  
>> -static int fpga_mgr_devres_match(struct device *dev, void *res,
>> -				 void *match_data)
>> -{
>> -	struct fpga_mgr_devres *dr = res;
>> -
>> -	return match_data == dr->mgr;
>> -}
>> -
>>  static void devm_fpga_mgr_unregister(struct device *dev, void *res)
>>  {
>>  	struct fpga_mgr_devres *dr = res;
>> @@ -743,44 +660,44 @@ static void devm_fpga_mgr_unregister(struct device *dev, void *res)
>>  
>>  /**
>>   * devm_fpga_mgr_register - resource managed variant of fpga_mgr_register()
>> - * @dev: managing device for this FPGA manager
>> - * @mgr: fpga manager struct
>> + * @dev:	fpga manager device from pdev
>> + * @name:	fpga manager name
>> + * @mops:	pointer to structure of fpga manager ops
>> + * @priv:	fpga manager private data
>>   *
>>   * This is the devres variant of fpga_mgr_register() for which the unregister
>>   * function will be called automatically when the managing device is detached.
>>   */
>> -int devm_fpga_mgr_register(struct device *dev, struct fpga_manager *mgr)
>> +struct fpga_manager *
>> +devm_fpga_mgr_register(struct device *dev, const char *name,
>> +		       const struct fpga_manager_ops *mops, void *priv)
>>  {
>>  	struct fpga_mgr_devres *dr;
>> -	int ret;
>> -
>> -	/*
>> -	 * Make sure that the struct fpga_manager * that is passed in is
>> -	 * managed itself.
>> -	 */
>> -	if (WARN_ON(!devres_find(dev, devm_fpga_mgr_release,
>> -				 fpga_mgr_devres_match, mgr)))
>> -		return -EINVAL;
>> +	struct fpga_manager *mgr;
>>  
>>  	dr = devres_alloc(devm_fpga_mgr_unregister, sizeof(*dr), GFP_KERNEL);
>>  	if (!dr)
>> -		return -ENOMEM;
>> +		return ERR_PTR(-ENOMEM);
>>  
>> -	ret = fpga_mgr_register(mgr);
>> -	if (ret) {
>> +	mgr = fpga_mgr_register(dev, name, mops, priv);
>> +	if (IS_ERR(mgr)) {
>>  		devres_free(dr);
>> -		return ret;
>> +		return mgr;
>>  	}
>>  
>>  	dr->mgr = mgr;
>>  	devres_add(dev, dr);
>>  
>> -	return 0;
>> +	return mgr;
>>  }
>>  EXPORT_SYMBOL_GPL(devm_fpga_mgr_register);
>>  
>>  static void fpga_mgr_dev_release(struct device *dev)
>>  {
>> +	struct fpga_manager *mgr = to_fpga_manager(dev);
>> +
>> +	ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
>> +	kfree(mgr);
>>  }
>>  
>>  static int __init fpga_mgr_class_init(void)
>> diff --git a/drivers/fpga/ice40-spi.c b/drivers/fpga/ice40-spi.c
>> index 69dec5af23c3..91ac25f339f3 100644
>> --- a/drivers/fpga/ice40-spi.c
>> +++ b/drivers/fpga/ice40-spi.c
>> @@ -178,12 +178,12 @@ static int ice40_fpga_probe(struct spi_device *spi)
>>  		return ret;
>>  	}
>>  
>> -	mgr = devm_fpga_mgr_create(dev, "Lattice iCE40 FPGA Manager",
>> -				   &ice40_fpga_ops, priv);
>> -	if (!mgr)
>> -		return -ENOMEM;
>> +	mgr = devm_fpga_mgr_register(dev, "Lattice iCE40 FPGA Manager",
>> +				     &ice40_fpga_ops, priv);
>> +	if (IS_ERR(mgr))
>> +		return PTR_ERR(mgr);
>>  
>> -	return devm_fpga_mgr_register(dev, mgr);
>> +	return 0;
>>  }
>>  
>>  static const struct of_device_id ice40_fpga_of_match[] = {
>> diff --git a/drivers/fpga/machxo2-spi.c b/drivers/fpga/machxo2-spi.c
>> index 114a64d2b7a4..0137c1e2e706 100644
>> --- a/drivers/fpga/machxo2-spi.c
>> +++ b/drivers/fpga/machxo2-spi.c
>> @@ -366,12 +366,12 @@ static int machxo2_spi_probe(struct spi_device *spi)
>>  		return -EINVAL;
>>  	}
>>  
>> -	mgr = devm_fpga_mgr_create(dev, "Lattice MachXO2 SPI FPGA Manager",
>> -				   &machxo2_ops, spi);
>> -	if (!mgr)
>> -		return -ENOMEM;
>> +	mgr = devm_fpga_mgr_register(dev, "Lattice MachXO2 SPI FPGA Manager",
>> +				     &machxo2_ops, spi);
>> +	if (IS_ERR(mgr))
>> +		return PTR_ERR(mgr);
>>  
>> -	return devm_fpga_mgr_register(dev, mgr);
>> +	return 0;
>>  }
>>  
>>  static const struct of_device_id of_match[] = {
>> diff --git a/drivers/fpga/socfpga-a10.c b/drivers/fpga/socfpga-a10.c
>> index 573d88bdf730..ac8e89b8a5cc 100644
>> --- a/drivers/fpga/socfpga-a10.c
>> +++ b/drivers/fpga/socfpga-a10.c
>> @@ -508,19 +508,15 @@ static int socfpga_a10_fpga_probe(struct platform_device *pdev)
>>  		return -EBUSY;
>>  	}
>>  
>> -	mgr = devm_fpga_mgr_create(dev, "SoCFPGA Arria10 FPGA Manager",
>> -				   &socfpga_a10_fpga_mgr_ops, priv);
>> -	if (!mgr)
>> -		return -ENOMEM;
>> -
>> -	platform_set_drvdata(pdev, mgr);
>> -
>> -	ret = fpga_mgr_register(mgr);
>> -	if (ret) {
>> +	mgr = fpga_mgr_register(dev, "SoCFPGA Arria10 FPGA Manager",
>> +				&socfpga_a10_fpga_mgr_ops, priv);
>> +	if (IS_ERR(mgr)) {
>>  		clk_disable_unprepare(priv->clk);
>> -		return ret;
>> +		return PTR_ERR(mgr);
>>  	}
>>  
>> +	platform_set_drvdata(pdev, mgr);
>> +
>>  	return 0;
>>  }
>>  
>> diff --git a/drivers/fpga/socfpga.c b/drivers/fpga/socfpga.c
>> index 1f467173fc1f..ebfa9f4d24ef 100644
>> --- a/drivers/fpga/socfpga.c
>> +++ b/drivers/fpga/socfpga.c
>> @@ -571,12 +571,12 @@ static int socfpga_fpga_probe(struct platform_device *pdev)
>>  	if (ret)
>>  		return ret;
>>  
>> -	mgr = devm_fpga_mgr_create(dev, "Altera SOCFPGA FPGA Manager",
>> -				   &socfpga_fpga_ops, priv);
>> -	if (!mgr)
>> -		return -ENOMEM;
>> +	mgr = devm_fpga_mgr_register(dev, "Altera SOCFPGA FPGA Manager",
>> +				     &socfpga_fpga_ops, priv);
>> +	if (IS_ERR(mgr))
>> +		return PTR_ERR(mgr);
>>  
>> -	return devm_fpga_mgr_register(dev, mgr);
>> +	return 0;
>>  }
>>  
>>  #ifdef CONFIG_OF
>> diff --git a/drivers/fpga/stratix10-soc.c b/drivers/fpga/stratix10-soc.c
>> index 657a70c5fc99..e0bd91b3f7f8 100644
>> --- a/drivers/fpga/stratix10-soc.c
>> +++ b/drivers/fpga/stratix10-soc.c
>> @@ -425,18 +425,11 @@ static int s10_probe(struct platform_device *pdev)
>>  
>>  	init_completion(&priv->status_return_completion);
>>  
>> -	mgr = fpga_mgr_create(dev, "Stratix10 SOC FPGA Manager",
>> -			      &s10_ops, priv);
>> -	if (!mgr) {
>> -		dev_err(dev, "unable to create FPGA manager\n");
>> -		ret = -ENOMEM;
>> -		goto probe_err;
>> -	}
>> -
>> -	ret = fpga_mgr_register(mgr);
>> -	if (ret) {
>> +	mgr = fpga_mgr_register(dev, "Stratix10 SOC FPGA Manager",
>> +				&s10_ops, priv);
>> +	if (IS_ERR(mgr)) {
>>  		dev_err(dev, "unable to register FPGA manager\n");
>> -		fpga_mgr_free(mgr);
>> +		ret = PTR_ERR(mgr);
>>  		goto probe_err;
>>  	}
>>  
>> diff --git a/drivers/fpga/ts73xx-fpga.c b/drivers/fpga/ts73xx-fpga.c
>> index 101f016c6ed8..5efbec2b9f9f 100644
>> --- a/drivers/fpga/ts73xx-fpga.c
>> +++ b/drivers/fpga/ts73xx-fpga.c
>> @@ -122,12 +122,12 @@ static int ts73xx_fpga_probe(struct platform_device *pdev)
>>  	if (IS_ERR(priv->io_base))
>>  		return PTR_ERR(priv->io_base);
>>  
>> -	mgr = devm_fpga_mgr_create(kdev, "TS-73xx FPGA Manager",
>> -				   &ts73xx_fpga_ops, priv);
>> -	if (!mgr)
>> -		return -ENOMEM;
>> +	mgr = devm_fpga_mgr_register(kdev, "TS-73xx FPGA Manager",
>> +				     &ts73xx_fpga_ops, priv);
>> +	if (IS_ERR(mgr))
>> +		return PTR_ERR(mgr);
>>  
>> -	return devm_fpga_mgr_register(kdev, mgr);
>> +	return 0;
>>  }
>>  
>>  static struct platform_driver ts73xx_fpga_driver = {
>> diff --git a/drivers/fpga/xilinx-spi.c b/drivers/fpga/xilinx-spi.c
>> index fee4d0abf6bf..efda475f7a98 100644
>> --- a/drivers/fpga/xilinx-spi.c
>> +++ b/drivers/fpga/xilinx-spi.c
>> @@ -247,13 +247,13 @@ static int xilinx_spi_probe(struct spi_device *spi)
>>  		return dev_err_probe(&spi->dev, PTR_ERR(conf->done),
>>  				     "Failed to get DONE gpio\n");
>>  
>> -	mgr = devm_fpga_mgr_create(&spi->dev,
>> -				   "Xilinx Slave Serial FPGA Manager",
>> -				   &xilinx_spi_ops, conf);
>> -	if (!mgr)
>> -		return -ENOMEM;
>> +	mgr = devm_fpga_mgr_register(&spi->dev,
>> +				     "Xilinx Slave Serial FPGA Manager",
>> +				     &xilinx_spi_ops, conf);
>> +	if (IS_ERR(mgr))
>> +		return PTR_ERR(mgr);
>>  
>> -	return devm_fpga_mgr_register(&spi->dev, mgr);
>> +	return 0;
>>  }
>>  
>>  static const struct of_device_id xlnx_spi_of_match[] = {
>> diff --git a/drivers/fpga/zynq-fpga.c b/drivers/fpga/zynq-fpga.c
>> index 07fa8d9ec675..74bbb9710f4e 100644
>> --- a/drivers/fpga/zynq-fpga.c
>> +++ b/drivers/fpga/zynq-fpga.c
>> @@ -609,20 +609,16 @@ static int zynq_fpga_probe(struct platform_device *pdev)
>>  
>>  	clk_disable(priv->clk);
>>  
>> -	mgr = devm_fpga_mgr_create(dev, "Xilinx Zynq FPGA Manager",
>> -				   &zynq_fpga_ops, priv);
>> -	if (!mgr)
>> -		return -ENOMEM;
>> -
>> -	platform_set_drvdata(pdev, mgr);
>> -
>> -	err = fpga_mgr_register(mgr);
>> -	if (err) {
>> +	mgr = fpga_mgr_register(dev, "Xilinx Zynq FPGA Manager",
>> +				&zynq_fpga_ops, priv);
>> +	if (IS_ERR(mgr)) {
>>  		dev_err(dev, "unable to register FPGA manager\n");
>>  		clk_unprepare(priv->clk);
>> -		return err;
>> +		return PTR_ERR(mgr);
>>  	}
>>  
>> +	platform_set_drvdata(pdev, mgr);
>> +
>>  	return 0;
>>  }
>>  
>> diff --git a/drivers/fpga/zynqmp-fpga.c b/drivers/fpga/zynqmp-fpga.c
>> index 125743c9797f..1a6d520985be 100644
>> --- a/drivers/fpga/zynqmp-fpga.c
>> +++ b/drivers/fpga/zynqmp-fpga.c
>> @@ -102,12 +102,12 @@ static int zynqmp_fpga_probe(struct platform_device *pdev)
>>  
>>  	priv->dev = dev;
>>  
>> -	mgr = devm_fpga_mgr_create(dev, "Xilinx ZynqMP FPGA Manager",
>> -				   &zynqmp_fpga_ops, priv);
>> -	if (!mgr)
>> -		return -ENOMEM;
>> +	mgr = devm_fpga_mgr_register(dev, "Xilinx ZynqMP FPGA Manager",
>> +				     &zynqmp_fpga_ops, priv);
>> +	if (IS_ERR(mgr))
>> +		return PTR_ERR(mgr);
>>  
>> -	return devm_fpga_mgr_register(dev, mgr);
>> +	return 0;
>>  }
>>  
>>  static const struct of_device_id zynqmp_fpga_of_match[] = {
>> diff --git a/include/linux/fpga/fpga-mgr.h b/include/linux/fpga/fpga-mgr.h
>> index 2bc3030a69e5..d1ccf8f8c38a 100644
>> --- a/include/linux/fpga/fpga-mgr.h
>> +++ b/include/linux/fpga/fpga-mgr.h
>> @@ -191,17 +191,14 @@ struct fpga_manager *fpga_mgr_get(struct device *dev);
>>  
>>  void fpga_mgr_put(struct fpga_manager *mgr);
>>  
>> -struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
>> -				     const struct fpga_manager_ops *mops,
>> -				     void *priv);
>> -void fpga_mgr_free(struct fpga_manager *mgr);
>> -int fpga_mgr_register(struct fpga_manager *mgr);
>> +struct fpga_manager *fpga_mgr_register(struct device *dev, const char *name,
>> +				       const struct fpga_manager_ops *mops,
>> +				       void *priv);
>>  void fpga_mgr_unregister(struct fpga_manager *mgr);
>>  
>> -int devm_fpga_mgr_register(struct device *dev, struct fpga_manager *mgr);
>> -
>> -struct fpga_manager *devm_fpga_mgr_create(struct device *dev, const char *name,
>> -					  const struct fpga_manager_ops *mops,
>> -					  void *priv);
>> +struct fpga_manager *
>> +devm_fpga_mgr_register(struct device *dev, const char *name,
>> +		       const struct fpga_manager_ops *mops,
>> +		       void *priv);
>>  
>>  #endif /*_LINUX_FPGA_MGR_H */
>> -- 
>> 2.25.1
>>
> Moritz


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v3 1/3] fpga: mgr: Use standard dev_release for class driver
  2021-05-24 20:16   ` Tom Rix
@ 2021-05-25 16:51     ` Russ Weight
  2021-05-28 14:46       ` Xu Yilun
  0 siblings, 1 reply; 17+ messages in thread
From: Russ Weight @ 2021-05-25 16:51 UTC (permalink / raw)
  To: Tom Rix, mdf, linux-fpga
  Cc: lgoncalv, yilun.xu, hao.wu, matthew.gerlach, richard.gong

Hi Tom,

On 5/24/21 1:16 PM, Tom Rix wrote:
> Russ,
>
> I think the signature fpga_mgr_register, should change to
>
> int fpga_mgr_register(struct device *, const struct fpga_mager_ops *mops, void *priv);
>
> I'll explain below ..
>
> On 5/24/21 11:50 AM, Russ Weight wrote:
>> The FPGA manager class driver data structure is being treated as a
>> managed resource instead of using standard dev_release call-back
>> to release the class data structure. This change removes the
>> managed resource code for the freeing of the class data structure
>> and combines the create() and register() functions into a single
>> register() function.
>>
>> The devm_fpga_mgr_register() function is retained.
>>
>> Signed-off-by: Russ Weight <russell.h.weight@intel.com>
>> ---
>> v3:
>>    - Cleaned up comment header for fpga_mgr_register()
>>    - Fix error return on ida_simple_get() failure
>> v2:
>>    - Restored devm_fpga_mgr_register() functionality, adapted for the combined
>>      create/register functionality.
>>    - All previous callers of devm_fpga_mgr_register() will continue to call
>>      devm_fpga_mgr_register().
>>    - replaced unnecessary ternary operators in return statements with standard
>>      if conditions.
>> ---
>>   drivers/fpga/altera-cvp.c        |  12 +--
>>   drivers/fpga/altera-pr-ip-core.c |   8 +-
>>   drivers/fpga/altera-ps-spi.c     |  10 +-
>>   drivers/fpga/dfl-fme-mgr.c       |  10 +-
>>   drivers/fpga/fpga-mgr.c          | 163 ++++++++-----------------------
>>   drivers/fpga/ice40-spi.c         |  10 +-
>>   drivers/fpga/machxo2-spi.c       |  10 +-
>>   drivers/fpga/socfpga-a10.c       |  16 ++-
>>   drivers/fpga/socfpga.c           |  10 +-
>>   drivers/fpga/stratix10-soc.c     |  15 +--
>>   drivers/fpga/ts73xx-fpga.c       |  10 +-
>>   drivers/fpga/xilinx-spi.c        |  12 +--
>>   drivers/fpga/zynq-fpga.c         |  16 ++-
>>   drivers/fpga/zynqmp-fpga.c       |  10 +-
>>   include/linux/fpga/fpga-mgr.h    |  17 ++--
>>   15 files changed, 112 insertions(+), 217 deletions(-)
>>
>> diff --git a/drivers/fpga/altera-cvp.c b/drivers/fpga/altera-cvp.c
>> index 4e0edb60bfba..b61020ceb0a1 100644
>> --- a/drivers/fpga/altera-cvp.c
>> +++ b/drivers/fpga/altera-cvp.c
>> @@ -652,19 +652,15 @@ static int altera_cvp_probe(struct pci_dev *pdev,
>>       snprintf(conf->mgr_name, sizeof(conf->mgr_name), "%s @%s",
>>            ALTERA_CVP_MGR_NAME, pci_name(pdev));
>>   -    mgr = devm_fpga_mgr_create(&pdev->dev, conf->mgr_name,
>> -                   &altera_cvp_ops, conf);
>> -    if (!mgr) {
>> -        ret = -ENOMEM;
>> +    mgr = fpga_mgr_register(&pdev->dev, conf->mgr_name,
>> +                &altera_cvp_ops, conf);
>> +    if (IS_ERR(mgr)) {
>> +        ret = PTR_ERR(mgr);
>>           goto err_unmap;
>>       }
>>         pci_set_drvdata(pdev, mgr);
>
> This is a wrapper around
>
> dev_set_drvdata()
>
>>   -    ret = fpga_mgr_register(mgr);
>> -    if (ret)
>> -        goto err_unmap;
>> -
>>       return 0;
>>     err_unmap:
>> diff --git a/drivers/fpga/altera-pr-ip-core.c b/drivers/fpga/altera-pr-ip-core.c
>> index 5b130c4d9882..0980e134cc43 100644
>> --- a/drivers/fpga/altera-pr-ip-core.c
>> +++ b/drivers/fpga/altera-pr-ip-core.c
>> @@ -191,11 +191,11 @@ int alt_pr_register(struct device *dev, void __iomem *reg_base)
>>           (val & ALT_PR_CSR_STATUS_MSK) >> ALT_PR_CSR_STATUS_SFT,
>>           (int)(val & ALT_PR_CSR_PR_START));
>>   -    mgr = devm_fpga_mgr_create(dev, dev_name(dev), &alt_pr_ops, priv);
> using the name of the device
>> -    if (!mgr)
>> -        return -ENOMEM;
>> +    mgr = devm_fpga_mgr_register(dev, dev_name(dev), &alt_pr_ops, priv);
>> +    if (IS_ERR(mgr))
>> +        return PTR_ERR(mgr);
> mgr is unused
>>   -    return devm_fpga_mgr_register(dev, mgr);
>> +    return 0;
>>   }
>>   EXPORT_SYMBOL_GPL(alt_pr_register);
>>   diff --git a/drivers/fpga/altera-ps-spi.c b/drivers/fpga/altera-ps-spi.c
>> index 23bfd4d1ad0f..d3f77b0312b2 100644
>> --- a/drivers/fpga/altera-ps-spi.c
>> +++ b/drivers/fpga/altera-ps-spi.c
>> @@ -302,12 +302,12 @@ static int altera_ps_probe(struct spi_device *spi)
>>       snprintf(conf->mgr_name, sizeof(conf->mgr_name), "%s %s",
>>            dev_driver_string(&spi->dev), dev_name(&spi->dev));
> a combo of devices name, is that really necessary ?

I would like to limit the scope of this patch to address:
(1) a populated dev_release function
(2) combining the device_initialize() and device_add() calls into single call
    to device_register()

Changing the name or removing the sysfs name file could have implications for
users who expect it to be there or, worse yet, have a script that looks for it.
I think these could be done in a separate patch if needed.

>>   -    mgr = devm_fpga_mgr_create(&spi->dev, conf->mgr_name,
>> -                   &altera_ps_ops, conf);
>> -    if (!mgr)
>> -        return -ENOMEM;
>> +    mgr = devm_fpga_mgr_register(&spi->dev, conf->mgr_name,
>> +                     &altera_ps_ops, conf);
>> +    if (IS_ERR(mgr))
>> +        return PTR_ERR(mgr);
> mgr is unused
>>   -    return devm_fpga_mgr_register(&spi->dev, mgr);
>> +    return 0;
>>   }
>>     static const struct spi_device_id altera_ps_spi_ids[] = {
>> diff --git a/drivers/fpga/dfl-fme-mgr.c b/drivers/fpga/dfl-fme-mgr.c
>> index d5861d13b306..d71eba817b3b 100644
>> --- a/drivers/fpga/dfl-fme-mgr.c
>> +++ b/drivers/fpga/dfl-fme-mgr.c
>> @@ -308,14 +308,14 @@ static int fme_mgr_probe(struct platform_device *pdev)
>>         fme_mgr_get_compat_id(priv->ioaddr, compat_id);
>>   -    mgr = devm_fpga_mgr_create(dev, "DFL FME FPGA Manager",
>> -                   &fme_mgr_ops, priv);
>> -    if (!mgr)
>> -        return -ENOMEM;
>> +    mgr = devm_fpga_mgr_register(dev, "DFL FME FPGA Manager",
>> +                     &fme_mgr_ops, priv);
> drv name is 'dfl-fme-mgr', is this close enough ?
>> +    if (IS_ERR(mgr))
>> +        return PTR_ERR(mgr);
>>         mgr->compat_id = compat_id;
>>   -    return devm_fpga_mgr_register(dev, mgr);
>> +    return 0;
>>   }
>>     static struct platform_driver fme_mgr_driver = {
>> diff --git a/drivers/fpga/fpga-mgr.c b/drivers/fpga/fpga-mgr.c
>> index b85bc47c91a9..22f0dd5c2834 100644
>> --- a/drivers/fpga/fpga-mgr.c
>> +++ b/drivers/fpga/fpga-mgr.c
>> @@ -550,20 +550,20 @@ void fpga_mgr_unlock(struct fpga_manager *mgr)
>>   EXPORT_SYMBOL_GPL(fpga_mgr_unlock);
>>     /**
>> - * fpga_mgr_create - create and initialize a FPGA manager struct
>> + * fpga_mgr_register - create and register a FPGA Manager device
>>    * @dev:    fpga manager device from pdev
>
> nit, space or tab after ':' ? this is inconsistent everywhere.
>
>
>>    * @name:    fpga manager name
>>    * @mops:    pointer to structure of fpga manager ops
>>    * @priv:    fpga manager private data
>>    *
>> - * The caller of this function is responsible for freeing the struct with
>> - * fpga_mgr_free().  Using devm_fpga_mgr_create() instead is recommended.
>> + * The caller of this function is responsible for calling fpga_mgr_unregister().
>> + * Using devm_fpga_mgr_register instead is recommended.
>>    *
>> - * Return: pointer to struct fpga_manager or NULL
>> + * Returns a struct fpga_manager pointer on success, or ERR_PTR() on error.
>>    */
>> -struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
>> -                     const struct fpga_manager_ops *mops,
>> -                     void *priv)
>> +struct fpga_manager *
>> +fpga_mgr_register(struct device *dev, const char *name,
>> +          const struct fpga_manager_ops *mops, void *priv)
>>   {
>>       struct fpga_manager *mgr;
>>       int id, ret;
>> @@ -572,21 +572,23 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
>>           !mops->write_init || (!mops->write && !mops->write_sg) ||
>>           (mops->write && mops->write_sg)) {
>>           dev_err(dev, "Attempt to register without fpga_manager_ops\n");
>> -        return NULL;
>> +        return ERR_PTR(-EINVAL);
>
> return -EINVAL;
>
> And similar
>
> The user's error checking is simplified to an int check.
>
> The clunky ERR_PTR()/PTR_ERR() can be removed
>
>>       }
>>         if (!name || !strlen(name)) {
>>           dev_err(dev, "Attempt to register with no name!\n");
>> -        return NULL;
>> +        return ERR_PTR(-EINVAL);
>>       }
>>         mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
>>       if (!mgr)
>> -        return NULL;
>> +        return ERR_PTR(-ENOMEM);
>>         id = ida_simple_get(&fpga_mgr_ida, 0, 0, GFP_KERNEL);
>> -    if (id < 0)
>> +    if (id < 0) {
>> +        ret = id;
>>           goto error_kfree;
>> +    }
>>         mutex_init(&mgr->ref_mutex);
>>   @@ -594,7 +596,6 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
>
> Just use the device name, do not give the user an option and simplify the calling parameters.
>
> mgr->name = dev_name(dev)
>
>>       mgr->mops = mops;
>>       mgr->priv = priv;
>>   -    device_initialize(&mgr->dev);
>>       mgr->dev.class = fpga_mgr_class;
>>       mgr->dev.groups = mops->groups;
>>       mgr->dev.parent = dev;
>> @@ -605,84 +606,6 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
>>       if (ret)
>>           goto error_device;
>>   
>
> The only use of returned mgr seems to be a variant of setting the dev->driver_data = mgr
>
> so just make it the default and don't pass back the ptr.
>
> dev_set_drvdata(dev, mgr);

I also noticed that in most cases the return pointer is not used. I get what
you are saying - it would be a nice simplification if the pointer did not need
to be returned.

However, I believe we are talking about modifying the driver_data for the
parent of the class driver, right? And the parent driver could spawn
multiple class drivers, or sub drivers. Having one of those sub-drivers control
the contents of it's driver-data doesn't feel right. It seems like the parent
driver should have exclusive control over this pointer.

I'm curious what others think...

- Russ

>
> return 0;
>
>
> Tom
>
>


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v3 1/3] fpga: mgr: Use standard dev_release for class driver
  2021-05-24 23:07     ` Russ Weight
@ 2021-05-25 17:39       ` Russ Weight
  2021-05-25 19:00         ` Tom Rix
  0 siblings, 1 reply; 17+ messages in thread
From: Russ Weight @ 2021-05-25 17:39 UTC (permalink / raw)
  To: Moritz Fischer
  Cc: linux-fpga, trix, lgoncalv, yilun.xu, hao.wu, matthew.gerlach,
	richard.gong

Moritz,

On 5/24/21 4:07 PM, Russ Weight wrote:
>
> On 5/24/21 3:39 PM, Moritz Fischer wrote:
>> On Mon, May 24, 2021 at 11:50:47AM -0700, Russ Weight wrote:
>>> The FPGA manager class driver data structure is being treated as a
>>> managed resource instead of using standard dev_release call-back
>>> to release the class data structure. This change removes the
>>> managed resource code for the freeing of the class data structure
>>> and combines the create() and register() functions into a single
>>> register() function.
>>>
>>> The devm_fpga_mgr_register() function is retained.
>> Should this be maybe called devm_fpga_mgr_create_and_register() ?
> Yes - I think that is more descriptive. I'll make that change.

Before I make the change, consider this:

The core device_register() function is defined as:

> int device_register(struct device *dev)
> {
>         device_initialize(dev); # called by previous fpga-mgr-create()
>         return device_add(dev); # called by previous fpga_mgr_register()
> }
> EXPORT_SYMBOL_GPL(device_register);

In a sense, our prior naming convention was inconsistent with the driver
core, where the register() function encompasses both the both initialization
of the device and addition into the driver core.

It is true that we are allocating the data structure as well in the merged
registration function, but that could may be considered part of the
initialization step.

What do you think? Would it be OK to stick with the shorter name of
devm_fpga_mgr_register()?

- Russ

>
> - Russ
>
>>> Signed-off-by: Russ Weight <russell.h.weight@intel.com>
>>> ---
>>> v3:
>>>   - Cleaned up comment header for fpga_mgr_register()
>>>   - Fix error return on ida_simple_get() failure
>>> v2:
>>>   - Restored devm_fpga_mgr_register() functionality, adapted for the combined
>>>     create/register functionality.
>>>   - All previous callers of devm_fpga_mgr_register() will continue to call
>>>     devm_fpga_mgr_register().
>>>   - replaced unnecessary ternary operators in return statements with standard
>>>     if conditions.
>>> ---
>>>  drivers/fpga/altera-cvp.c        |  12 +--
>>>  drivers/fpga/altera-pr-ip-core.c |   8 +-
>>>  drivers/fpga/altera-ps-spi.c     |  10 +-
>>>  drivers/fpga/dfl-fme-mgr.c       |  10 +-
>>>  drivers/fpga/fpga-mgr.c          | 163 ++++++++-----------------------
>>>  drivers/fpga/ice40-spi.c         |  10 +-
>>>  drivers/fpga/machxo2-spi.c       |  10 +-
>>>  drivers/fpga/socfpga-a10.c       |  16 ++-
>>>  drivers/fpga/socfpga.c           |  10 +-
>>>  drivers/fpga/stratix10-soc.c     |  15 +--
>>>  drivers/fpga/ts73xx-fpga.c       |  10 +-
>>>  drivers/fpga/xilinx-spi.c        |  12 +--
>>>  drivers/fpga/zynq-fpga.c         |  16 ++-
>>>  drivers/fpga/zynqmp-fpga.c       |  10 +-
>>>  include/linux/fpga/fpga-mgr.h    |  17 ++--
>>>  15 files changed, 112 insertions(+), 217 deletions(-)
>>>
>>> diff --git a/drivers/fpga/altera-cvp.c b/drivers/fpga/altera-cvp.c
>>> index 4e0edb60bfba..b61020ceb0a1 100644
>>> --- a/drivers/fpga/altera-cvp.c
>>> +++ b/drivers/fpga/altera-cvp.c
>>> @@ -652,19 +652,15 @@ static int altera_cvp_probe(struct pci_dev *pdev,
>>>  	snprintf(conf->mgr_name, sizeof(conf->mgr_name), "%s @%s",
>>>  		 ALTERA_CVP_MGR_NAME, pci_name(pdev));
>>>  
>>> -	mgr = devm_fpga_mgr_create(&pdev->dev, conf->mgr_name,
>>> -				   &altera_cvp_ops, conf);
>>> -	if (!mgr) {
>>> -		ret = -ENOMEM;
>>> +	mgr = fpga_mgr_register(&pdev->dev, conf->mgr_name,
>>> +				&altera_cvp_ops, conf);
>>> +	if (IS_ERR(mgr)) {
>>> +		ret = PTR_ERR(mgr);
>>>  		goto err_unmap;
>>>  	}
>>>  
>>>  	pci_set_drvdata(pdev, mgr);
>>>  
>>> -	ret = fpga_mgr_register(mgr);
>>> -	if (ret)
>>> -		goto err_unmap;
>>> -
>>>  	return 0;
>>>  
>>>  err_unmap:
>>> diff --git a/drivers/fpga/altera-pr-ip-core.c b/drivers/fpga/altera-pr-ip-core.c
>>> index 5b130c4d9882..0980e134cc43 100644
>>> --- a/drivers/fpga/altera-pr-ip-core.c
>>> +++ b/drivers/fpga/altera-pr-ip-core.c
>>> @@ -191,11 +191,11 @@ int alt_pr_register(struct device *dev, void __iomem *reg_base)
>>>  		(val & ALT_PR_CSR_STATUS_MSK) >> ALT_PR_CSR_STATUS_SFT,
>>>  		(int)(val & ALT_PR_CSR_PR_START));
>>>  
>>> -	mgr = devm_fpga_mgr_create(dev, dev_name(dev), &alt_pr_ops, priv);
>>> -	if (!mgr)
>>> -		return -ENOMEM;
>>> +	mgr = devm_fpga_mgr_register(dev, dev_name(dev), &alt_pr_ops, priv);
>>> +	if (IS_ERR(mgr))
>>> +		return PTR_ERR(mgr);
>>>  
>>> -	return devm_fpga_mgr_register(dev, mgr);
>>> +	return 0;
>>>  }
>>>  EXPORT_SYMBOL_GPL(alt_pr_register);
>>>  
>>> diff --git a/drivers/fpga/altera-ps-spi.c b/drivers/fpga/altera-ps-spi.c
>>> index 23bfd4d1ad0f..d3f77b0312b2 100644
>>> --- a/drivers/fpga/altera-ps-spi.c
>>> +++ b/drivers/fpga/altera-ps-spi.c
>>> @@ -302,12 +302,12 @@ static int altera_ps_probe(struct spi_device *spi)
>>>  	snprintf(conf->mgr_name, sizeof(conf->mgr_name), "%s %s",
>>>  		 dev_driver_string(&spi->dev), dev_name(&spi->dev));
>>>  
>>> -	mgr = devm_fpga_mgr_create(&spi->dev, conf->mgr_name,
>>> -				   &altera_ps_ops, conf);
>>> -	if (!mgr)
>>> -		return -ENOMEM;
>>> +	mgr = devm_fpga_mgr_register(&spi->dev, conf->mgr_name,
>>> +				     &altera_ps_ops, conf);
>>> +	if (IS_ERR(mgr))
>>> +		return PTR_ERR(mgr);
>>>  
>>> -	return devm_fpga_mgr_register(&spi->dev, mgr);
>>> +	return 0;
>>>  }
>>>  
>>>  static const struct spi_device_id altera_ps_spi_ids[] = {
>>> diff --git a/drivers/fpga/dfl-fme-mgr.c b/drivers/fpga/dfl-fme-mgr.c
>>> index d5861d13b306..d71eba817b3b 100644
>>> --- a/drivers/fpga/dfl-fme-mgr.c
>>> +++ b/drivers/fpga/dfl-fme-mgr.c
>>> @@ -308,14 +308,14 @@ static int fme_mgr_probe(struct platform_device *pdev)
>>>  
>>>  	fme_mgr_get_compat_id(priv->ioaddr, compat_id);
>>>  
>>> -	mgr = devm_fpga_mgr_create(dev, "DFL FME FPGA Manager",
>>> -				   &fme_mgr_ops, priv);
>>> -	if (!mgr)
>>> -		return -ENOMEM;
>>> +	mgr = devm_fpga_mgr_register(dev, "DFL FME FPGA Manager",
>>> +				     &fme_mgr_ops, priv);
>>> +	if (IS_ERR(mgr))
>>> +		return PTR_ERR(mgr);
>>>  
>>>  	mgr->compat_id = compat_id;
>>>  
>>> -	return devm_fpga_mgr_register(dev, mgr);
>>> +	return 0;
>>>  }
>>>  
>>>  static struct platform_driver fme_mgr_driver = {
>>> diff --git a/drivers/fpga/fpga-mgr.c b/drivers/fpga/fpga-mgr.c
>>> index b85bc47c91a9..22f0dd5c2834 100644
>>> --- a/drivers/fpga/fpga-mgr.c
>>> +++ b/drivers/fpga/fpga-mgr.c
>>> @@ -550,20 +550,20 @@ void fpga_mgr_unlock(struct fpga_manager *mgr)
>>>  EXPORT_SYMBOL_GPL(fpga_mgr_unlock);
>>>  
>>>  /**
>>> - * fpga_mgr_create - create and initialize a FPGA manager struct
>>> + * fpga_mgr_register - create and register a FPGA Manager device
>>>   * @dev:	fpga manager device from pdev
>>>   * @name:	fpga manager name
>>>   * @mops:	pointer to structure of fpga manager ops
>>>   * @priv:	fpga manager private data
>>>   *
>>> - * The caller of this function is responsible for freeing the struct with
>>> - * fpga_mgr_free().  Using devm_fpga_mgr_create() instead is recommended.
>>> + * The caller of this function is responsible for calling fpga_mgr_unregister().
>>> + * Using devm_fpga_mgr_register instead is recommended.
>>>   *
>>> - * Return: pointer to struct fpga_manager or NULL
>>> + * Returns a struct fpga_manager pointer on success, or ERR_PTR() on error.
>>>   */
>>> -struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
>>> -				     const struct fpga_manager_ops *mops,
>>> -				     void *priv)
>>> +struct fpga_manager *
>>> +fpga_mgr_register(struct device *dev, const char *name,
>>> +		  const struct fpga_manager_ops *mops, void *priv)
>>>  {
>>>  	struct fpga_manager *mgr;
>>>  	int id, ret;
>>> @@ -572,21 +572,23 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
>>>  	    !mops->write_init || (!mops->write && !mops->write_sg) ||
>>>  	    (mops->write && mops->write_sg)) {
>>>  		dev_err(dev, "Attempt to register without fpga_manager_ops\n");
>>> -		return NULL;
>>> +		return ERR_PTR(-EINVAL);
>>>  	}
>>>  
>>>  	if (!name || !strlen(name)) {
>>>  		dev_err(dev, "Attempt to register with no name!\n");
>>> -		return NULL;
>>> +		return ERR_PTR(-EINVAL);
>>>  	}
>>>  
>>>  	mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
>>>  	if (!mgr)
>>> -		return NULL;
>>> +		return ERR_PTR(-ENOMEM);
>>>  
>>>  	id = ida_simple_get(&fpga_mgr_ida, 0, 0, GFP_KERNEL);
>>> -	if (id < 0)
>>> +	if (id < 0) {
>>> +		ret = id;
>>>  		goto error_kfree;
>>> +	}
>>>  
>>>  	mutex_init(&mgr->ref_mutex);
>>>  
>>> @@ -594,7 +596,6 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
>>>  	mgr->mops = mops;
>>>  	mgr->priv = priv;
>>>  
>>> -	device_initialize(&mgr->dev);
>>>  	mgr->dev.class = fpga_mgr_class;
>>>  	mgr->dev.groups = mops->groups;
>>>  	mgr->dev.parent = dev;
>>> @@ -605,84 +606,6 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
>>>  	if (ret)
>>>  		goto error_device;
>>>  
>>> -	return mgr;
>>> -
>>> -error_device:
>>> -	ida_simple_remove(&fpga_mgr_ida, id);
>>> -error_kfree:
>>> -	kfree(mgr);
>>> -
>>> -	return NULL;
>>> -}
>>> -EXPORT_SYMBOL_GPL(fpga_mgr_create);
>>> -
>>> -/**
>>> - * fpga_mgr_free - free a FPGA manager created with fpga_mgr_create()
>>> - * @mgr:	fpga manager struct
>>> - */
>>> -void fpga_mgr_free(struct fpga_manager *mgr)
>>> -{
>>> -	ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
>>> -	kfree(mgr);
>>> -}
>>> -EXPORT_SYMBOL_GPL(fpga_mgr_free);
>>> -
>>> -static void devm_fpga_mgr_release(struct device *dev, void *res)
>>> -{
>>> -	struct fpga_mgr_devres *dr = res;
>>> -
>>> -	fpga_mgr_free(dr->mgr);
>>> -}
>>> -
>>> -/**
>>> - * devm_fpga_mgr_create - create and initialize a managed FPGA manager struct
>>> - * @dev:	fpga manager device from pdev
>>> - * @name:	fpga manager name
>>> - * @mops:	pointer to structure of fpga manager ops
>>> - * @priv:	fpga manager private data
>>> - *
>>> - * This function is intended for use in a FPGA manager driver's probe function.
>>> - * After the manager driver creates the manager struct with
>>> - * devm_fpga_mgr_create(), it should register it with fpga_mgr_register().  The
>>> - * manager driver's remove function should call fpga_mgr_unregister().  The
>>> - * manager struct allocated with this function will be freed automatically on
>>> - * driver detach.  This includes the case of a probe function returning error
>>> - * before calling fpga_mgr_register(), the struct will still get cleaned up.
>>> - *
>>> - * Return: pointer to struct fpga_manager or NULL
>>> - */
>>> -struct fpga_manager *devm_fpga_mgr_create(struct device *dev, const char *name,
>>> -					  const struct fpga_manager_ops *mops,
>>> -					  void *priv)
>>> -{
>>> -	struct fpga_mgr_devres *dr;
>>> -
>>> -	dr = devres_alloc(devm_fpga_mgr_release, sizeof(*dr), GFP_KERNEL);
>>> -	if (!dr)
>>> -		return NULL;
>>> -
>>> -	dr->mgr = fpga_mgr_create(dev, name, mops, priv);
>>> -	if (!dr->mgr) {
>>> -		devres_free(dr);
>>> -		return NULL;
>>> -	}
>>> -
>>> -	devres_add(dev, dr);
>>> -
>>> -	return dr->mgr;
>>> -}
>>> -EXPORT_SYMBOL_GPL(devm_fpga_mgr_create);
>>> -
>>> -/**
>>> - * fpga_mgr_register - register a FPGA manager
>>> - * @mgr: fpga manager struct
>>> - *
>>> - * Return: 0 on success, negative error code otherwise.
>>> - */
>>> -int fpga_mgr_register(struct fpga_manager *mgr)
>>> -{
>>> -	int ret;
>>> -
>>>  	/*
>>>  	 * Initialize framework state by requesting low level driver read state
>>>  	 * from device.  FPGA may be in reset mode or may have been programmed
>>> @@ -690,18 +613,20 @@ int fpga_mgr_register(struct fpga_manager *mgr)
>>>  	 */
>>>  	mgr->state = mgr->mops->state(mgr);
>>>  
>>> -	ret = device_add(&mgr->dev);
>>> -	if (ret)
>>> -		goto error_device;
>>> -
>>> -	dev_info(&mgr->dev, "%s registered\n", mgr->name);
>>> +	ret = device_register(&mgr->dev);
>>> +	if (ret) {
>>> +		put_device(&mgr->dev);
>>> +		return ERR_PTR(ret);
>>> +	}
>>>  
>>> -	return 0;
>>> +	return mgr;
>>>  
>>>  error_device:
>>> -	ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
>>> +	ida_simple_remove(&fpga_mgr_ida, id);
>>> +error_kfree:
>>> +	kfree(mgr);
>>>  
>>> -	return ret;
>>> +	return ERR_PTR(ret);
>>>  }
>>>  EXPORT_SYMBOL_GPL(fpga_mgr_register);
>>>  
>>> @@ -726,14 +651,6 @@ void fpga_mgr_unregister(struct fpga_manager *mgr)
>>>  }
>>>  EXPORT_SYMBOL_GPL(fpga_mgr_unregister);
>>>  
>>> -static int fpga_mgr_devres_match(struct device *dev, void *res,
>>> -				 void *match_data)
>>> -{
>>> -	struct fpga_mgr_devres *dr = res;
>>> -
>>> -	return match_data == dr->mgr;
>>> -}
>>> -
>>>  static void devm_fpga_mgr_unregister(struct device *dev, void *res)
>>>  {
>>>  	struct fpga_mgr_devres *dr = res;
>>> @@ -743,44 +660,44 @@ static void devm_fpga_mgr_unregister(struct device *dev, void *res)
>>>  
>>>  /**
>>>   * devm_fpga_mgr_register - resource managed variant of fpga_mgr_register()
>>> - * @dev: managing device for this FPGA manager
>>> - * @mgr: fpga manager struct
>>> + * @dev:	fpga manager device from pdev
>>> + * @name:	fpga manager name
>>> + * @mops:	pointer to structure of fpga manager ops
>>> + * @priv:	fpga manager private data
>>>   *
>>>   * This is the devres variant of fpga_mgr_register() for which the unregister
>>>   * function will be called automatically when the managing device is detached.
>>>   */
>>> -int devm_fpga_mgr_register(struct device *dev, struct fpga_manager *mgr)
>>> +struct fpga_manager *
>>> +devm_fpga_mgr_register(struct device *dev, const char *name,
>>> +		       const struct fpga_manager_ops *mops, void *priv)
>>>  {
>>>  	struct fpga_mgr_devres *dr;
>>> -	int ret;
>>> -
>>> -	/*
>>> -	 * Make sure that the struct fpga_manager * that is passed in is
>>> -	 * managed itself.
>>> -	 */
>>> -	if (WARN_ON(!devres_find(dev, devm_fpga_mgr_release,
>>> -				 fpga_mgr_devres_match, mgr)))
>>> -		return -EINVAL;
>>> +	struct fpga_manager *mgr;
>>>  
>>>  	dr = devres_alloc(devm_fpga_mgr_unregister, sizeof(*dr), GFP_KERNEL);
>>>  	if (!dr)
>>> -		return -ENOMEM;
>>> +		return ERR_PTR(-ENOMEM);
>>>  
>>> -	ret = fpga_mgr_register(mgr);
>>> -	if (ret) {
>>> +	mgr = fpga_mgr_register(dev, name, mops, priv);
>>> +	if (IS_ERR(mgr)) {
>>>  		devres_free(dr);
>>> -		return ret;
>>> +		return mgr;
>>>  	}
>>>  
>>>  	dr->mgr = mgr;
>>>  	devres_add(dev, dr);
>>>  
>>> -	return 0;
>>> +	return mgr;
>>>  }
>>>  EXPORT_SYMBOL_GPL(devm_fpga_mgr_register);
>>>  
>>>  static void fpga_mgr_dev_release(struct device *dev)
>>>  {
>>> +	struct fpga_manager *mgr = to_fpga_manager(dev);
>>> +
>>> +	ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
>>> +	kfree(mgr);
>>>  }
>>>  
>>>  static int __init fpga_mgr_class_init(void)
>>> diff --git a/drivers/fpga/ice40-spi.c b/drivers/fpga/ice40-spi.c
>>> index 69dec5af23c3..91ac25f339f3 100644
>>> --- a/drivers/fpga/ice40-spi.c
>>> +++ b/drivers/fpga/ice40-spi.c
>>> @@ -178,12 +178,12 @@ static int ice40_fpga_probe(struct spi_device *spi)
>>>  		return ret;
>>>  	}
>>>  
>>> -	mgr = devm_fpga_mgr_create(dev, "Lattice iCE40 FPGA Manager",
>>> -				   &ice40_fpga_ops, priv);
>>> -	if (!mgr)
>>> -		return -ENOMEM;
>>> +	mgr = devm_fpga_mgr_register(dev, "Lattice iCE40 FPGA Manager",
>>> +				     &ice40_fpga_ops, priv);
>>> +	if (IS_ERR(mgr))
>>> +		return PTR_ERR(mgr);
>>>  
>>> -	return devm_fpga_mgr_register(dev, mgr);
>>> +	return 0;
>>>  }
>>>  
>>>  static const struct of_device_id ice40_fpga_of_match[] = {
>>> diff --git a/drivers/fpga/machxo2-spi.c b/drivers/fpga/machxo2-spi.c
>>> index 114a64d2b7a4..0137c1e2e706 100644
>>> --- a/drivers/fpga/machxo2-spi.c
>>> +++ b/drivers/fpga/machxo2-spi.c
>>> @@ -366,12 +366,12 @@ static int machxo2_spi_probe(struct spi_device *spi)
>>>  		return -EINVAL;
>>>  	}
>>>  
>>> -	mgr = devm_fpga_mgr_create(dev, "Lattice MachXO2 SPI FPGA Manager",
>>> -				   &machxo2_ops, spi);
>>> -	if (!mgr)
>>> -		return -ENOMEM;
>>> +	mgr = devm_fpga_mgr_register(dev, "Lattice MachXO2 SPI FPGA Manager",
>>> +				     &machxo2_ops, spi);
>>> +	if (IS_ERR(mgr))
>>> +		return PTR_ERR(mgr);
>>>  
>>> -	return devm_fpga_mgr_register(dev, mgr);
>>> +	return 0;
>>>  }
>>>  
>>>  static const struct of_device_id of_match[] = {
>>> diff --git a/drivers/fpga/socfpga-a10.c b/drivers/fpga/socfpga-a10.c
>>> index 573d88bdf730..ac8e89b8a5cc 100644
>>> --- a/drivers/fpga/socfpga-a10.c
>>> +++ b/drivers/fpga/socfpga-a10.c
>>> @@ -508,19 +508,15 @@ static int socfpga_a10_fpga_probe(struct platform_device *pdev)
>>>  		return -EBUSY;
>>>  	}
>>>  
>>> -	mgr = devm_fpga_mgr_create(dev, "SoCFPGA Arria10 FPGA Manager",
>>> -				   &socfpga_a10_fpga_mgr_ops, priv);
>>> -	if (!mgr)
>>> -		return -ENOMEM;
>>> -
>>> -	platform_set_drvdata(pdev, mgr);
>>> -
>>> -	ret = fpga_mgr_register(mgr);
>>> -	if (ret) {
>>> +	mgr = fpga_mgr_register(dev, "SoCFPGA Arria10 FPGA Manager",
>>> +				&socfpga_a10_fpga_mgr_ops, priv);
>>> +	if (IS_ERR(mgr)) {
>>>  		clk_disable_unprepare(priv->clk);
>>> -		return ret;
>>> +		return PTR_ERR(mgr);
>>>  	}
>>>  
>>> +	platform_set_drvdata(pdev, mgr);
>>> +
>>>  	return 0;
>>>  }
>>>  
>>> diff --git a/drivers/fpga/socfpga.c b/drivers/fpga/socfpga.c
>>> index 1f467173fc1f..ebfa9f4d24ef 100644
>>> --- a/drivers/fpga/socfpga.c
>>> +++ b/drivers/fpga/socfpga.c
>>> @@ -571,12 +571,12 @@ static int socfpga_fpga_probe(struct platform_device *pdev)
>>>  	if (ret)
>>>  		return ret;
>>>  
>>> -	mgr = devm_fpga_mgr_create(dev, "Altera SOCFPGA FPGA Manager",
>>> -				   &socfpga_fpga_ops, priv);
>>> -	if (!mgr)
>>> -		return -ENOMEM;
>>> +	mgr = devm_fpga_mgr_register(dev, "Altera SOCFPGA FPGA Manager",
>>> +				     &socfpga_fpga_ops, priv);
>>> +	if (IS_ERR(mgr))
>>> +		return PTR_ERR(mgr);
>>>  
>>> -	return devm_fpga_mgr_register(dev, mgr);
>>> +	return 0;
>>>  }
>>>  
>>>  #ifdef CONFIG_OF
>>> diff --git a/drivers/fpga/stratix10-soc.c b/drivers/fpga/stratix10-soc.c
>>> index 657a70c5fc99..e0bd91b3f7f8 100644
>>> --- a/drivers/fpga/stratix10-soc.c
>>> +++ b/drivers/fpga/stratix10-soc.c
>>> @@ -425,18 +425,11 @@ static int s10_probe(struct platform_device *pdev)
>>>  
>>>  	init_completion(&priv->status_return_completion);
>>>  
>>> -	mgr = fpga_mgr_create(dev, "Stratix10 SOC FPGA Manager",
>>> -			      &s10_ops, priv);
>>> -	if (!mgr) {
>>> -		dev_err(dev, "unable to create FPGA manager\n");
>>> -		ret = -ENOMEM;
>>> -		goto probe_err;
>>> -	}
>>> -
>>> -	ret = fpga_mgr_register(mgr);
>>> -	if (ret) {
>>> +	mgr = fpga_mgr_register(dev, "Stratix10 SOC FPGA Manager",
>>> +				&s10_ops, priv);
>>> +	if (IS_ERR(mgr)) {
>>>  		dev_err(dev, "unable to register FPGA manager\n");
>>> -		fpga_mgr_free(mgr);
>>> +		ret = PTR_ERR(mgr);
>>>  		goto probe_err;
>>>  	}
>>>  
>>> diff --git a/drivers/fpga/ts73xx-fpga.c b/drivers/fpga/ts73xx-fpga.c
>>> index 101f016c6ed8..5efbec2b9f9f 100644
>>> --- a/drivers/fpga/ts73xx-fpga.c
>>> +++ b/drivers/fpga/ts73xx-fpga.c
>>> @@ -122,12 +122,12 @@ static int ts73xx_fpga_probe(struct platform_device *pdev)
>>>  	if (IS_ERR(priv->io_base))
>>>  		return PTR_ERR(priv->io_base);
>>>  
>>> -	mgr = devm_fpga_mgr_create(kdev, "TS-73xx FPGA Manager",
>>> -				   &ts73xx_fpga_ops, priv);
>>> -	if (!mgr)
>>> -		return -ENOMEM;
>>> +	mgr = devm_fpga_mgr_register(kdev, "TS-73xx FPGA Manager",
>>> +				     &ts73xx_fpga_ops, priv);
>>> +	if (IS_ERR(mgr))
>>> +		return PTR_ERR(mgr);
>>>  
>>> -	return devm_fpga_mgr_register(kdev, mgr);
>>> +	return 0;
>>>  }
>>>  
>>>  static struct platform_driver ts73xx_fpga_driver = {
>>> diff --git a/drivers/fpga/xilinx-spi.c b/drivers/fpga/xilinx-spi.c
>>> index fee4d0abf6bf..efda475f7a98 100644
>>> --- a/drivers/fpga/xilinx-spi.c
>>> +++ b/drivers/fpga/xilinx-spi.c
>>> @@ -247,13 +247,13 @@ static int xilinx_spi_probe(struct spi_device *spi)
>>>  		return dev_err_probe(&spi->dev, PTR_ERR(conf->done),
>>>  				     "Failed to get DONE gpio\n");
>>>  
>>> -	mgr = devm_fpga_mgr_create(&spi->dev,
>>> -				   "Xilinx Slave Serial FPGA Manager",
>>> -				   &xilinx_spi_ops, conf);
>>> -	if (!mgr)
>>> -		return -ENOMEM;
>>> +	mgr = devm_fpga_mgr_register(&spi->dev,
>>> +				     "Xilinx Slave Serial FPGA Manager",
>>> +				     &xilinx_spi_ops, conf);
>>> +	if (IS_ERR(mgr))
>>> +		return PTR_ERR(mgr);
>>>  
>>> -	return devm_fpga_mgr_register(&spi->dev, mgr);
>>> +	return 0;
>>>  }
>>>  
>>>  static const struct of_device_id xlnx_spi_of_match[] = {
>>> diff --git a/drivers/fpga/zynq-fpga.c b/drivers/fpga/zynq-fpga.c
>>> index 07fa8d9ec675..74bbb9710f4e 100644
>>> --- a/drivers/fpga/zynq-fpga.c
>>> +++ b/drivers/fpga/zynq-fpga.c
>>> @@ -609,20 +609,16 @@ static int zynq_fpga_probe(struct platform_device *pdev)
>>>  
>>>  	clk_disable(priv->clk);
>>>  
>>> -	mgr = devm_fpga_mgr_create(dev, "Xilinx Zynq FPGA Manager",
>>> -				   &zynq_fpga_ops, priv);
>>> -	if (!mgr)
>>> -		return -ENOMEM;
>>> -
>>> -	platform_set_drvdata(pdev, mgr);
>>> -
>>> -	err = fpga_mgr_register(mgr);
>>> -	if (err) {
>>> +	mgr = fpga_mgr_register(dev, "Xilinx Zynq FPGA Manager",
>>> +				&zynq_fpga_ops, priv);
>>> +	if (IS_ERR(mgr)) {
>>>  		dev_err(dev, "unable to register FPGA manager\n");
>>>  		clk_unprepare(priv->clk);
>>> -		return err;
>>> +		return PTR_ERR(mgr);
>>>  	}
>>>  
>>> +	platform_set_drvdata(pdev, mgr);
>>> +
>>>  	return 0;
>>>  }
>>>  
>>> diff --git a/drivers/fpga/zynqmp-fpga.c b/drivers/fpga/zynqmp-fpga.c
>>> index 125743c9797f..1a6d520985be 100644
>>> --- a/drivers/fpga/zynqmp-fpga.c
>>> +++ b/drivers/fpga/zynqmp-fpga.c
>>> @@ -102,12 +102,12 @@ static int zynqmp_fpga_probe(struct platform_device *pdev)
>>>  
>>>  	priv->dev = dev;
>>>  
>>> -	mgr = devm_fpga_mgr_create(dev, "Xilinx ZynqMP FPGA Manager",
>>> -				   &zynqmp_fpga_ops, priv);
>>> -	if (!mgr)
>>> -		return -ENOMEM;
>>> +	mgr = devm_fpga_mgr_register(dev, "Xilinx ZynqMP FPGA Manager",
>>> +				     &zynqmp_fpga_ops, priv);
>>> +	if (IS_ERR(mgr))
>>> +		return PTR_ERR(mgr);
>>>  
>>> -	return devm_fpga_mgr_register(dev, mgr);
>>> +	return 0;
>>>  }
>>>  
>>>  static const struct of_device_id zynqmp_fpga_of_match[] = {
>>> diff --git a/include/linux/fpga/fpga-mgr.h b/include/linux/fpga/fpga-mgr.h
>>> index 2bc3030a69e5..d1ccf8f8c38a 100644
>>> --- a/include/linux/fpga/fpga-mgr.h
>>> +++ b/include/linux/fpga/fpga-mgr.h
>>> @@ -191,17 +191,14 @@ struct fpga_manager *fpga_mgr_get(struct device *dev);
>>>  
>>>  void fpga_mgr_put(struct fpga_manager *mgr);
>>>  
>>> -struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
>>> -				     const struct fpga_manager_ops *mops,
>>> -				     void *priv);
>>> -void fpga_mgr_free(struct fpga_manager *mgr);
>>> -int fpga_mgr_register(struct fpga_manager *mgr);
>>> +struct fpga_manager *fpga_mgr_register(struct device *dev, const char *name,
>>> +				       const struct fpga_manager_ops *mops,
>>> +				       void *priv);
>>>  void fpga_mgr_unregister(struct fpga_manager *mgr);
>>>  
>>> -int devm_fpga_mgr_register(struct device *dev, struct fpga_manager *mgr);
>>> -
>>> -struct fpga_manager *devm_fpga_mgr_create(struct device *dev, const char *name,
>>> -					  const struct fpga_manager_ops *mops,
>>> -					  void *priv);
>>> +struct fpga_manager *
>>> +devm_fpga_mgr_register(struct device *dev, const char *name,
>>> +		       const struct fpga_manager_ops *mops,
>>> +		       void *priv);
>>>  
>>>  #endif /*_LINUX_FPGA_MGR_H */
>>> -- 
>>> 2.25.1
>>>
>> Moritz


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v3 1/3] fpga: mgr: Use standard dev_release for class driver
  2021-05-25 17:39       ` Russ Weight
@ 2021-05-25 19:00         ` Tom Rix
  0 siblings, 0 replies; 17+ messages in thread
From: Tom Rix @ 2021-05-25 19:00 UTC (permalink / raw)
  To: Russ Weight, Moritz Fischer
  Cc: linux-fpga, lgoncalv, yilun.xu, hao.wu, matthew.gerlach, richard.gong


On 5/25/21 10:39 AM, Russ Weight wrote:
> Moritz,
>
> On 5/24/21 4:07 PM, Russ Weight wrote:
>> On 5/24/21 3:39 PM, Moritz Fischer wrote:
>>> On Mon, May 24, 2021 at 11:50:47AM -0700, Russ Weight wrote:
>>>> The FPGA manager class driver data structure is being treated as a
>>>> managed resource instead of using standard dev_release call-back
>>>> to release the class data structure. This change removes the
>>>> managed resource code for the freeing of the class data structure
>>>> and combines the create() and register() functions into a single
>>>> register() function.
>>>>
>>>> The devm_fpga_mgr_register() function is retained.
>>> Should this be maybe called devm_fpga_mgr_create_and_register() ?
>> Yes - I think that is more descriptive. I'll make that change.
> Before I make the change, consider this:
>
> The core device_register() function is defined as:
>
>> int device_register(struct device *dev)
>> {
>>          device_initialize(dev); # called by previous fpga-mgr-create()
>>          return device_add(dev); # called by previous fpga_mgr_register()
>> }
>> EXPORT_SYMBOL_GPL(device_register);
> In a sense, our prior naming convention was inconsistent with the driver
> core, where the register() function encompasses both the both initialization
> of the device and addition into the driver core.
>
> It is true that we are allocating the data structure as well in the merged
> registration function, but that could may be considered part of the
> initialization step.
>
> What do you think? Would it be OK to stick with the shorter name of
> devm_fpga_mgr_register()?

my 2 cents is to stick with the shorter name.

It's already pretty long ;)

Tom

>
> - Russ
>
>> - Russ
>>
>>>> Signed-off-by: Russ Weight <russell.h.weight@intel.com>
>>>> ---
>>>> v3:
>>>>    - Cleaned up comment header for fpga_mgr_register()
>>>>    - Fix error return on ida_simple_get() failure
>>>> v2:
>>>>    - Restored devm_fpga_mgr_register() functionality, adapted for the combined
>>>>      create/register functionality.
>>>>    - All previous callers of devm_fpga_mgr_register() will continue to call
>>>>      devm_fpga_mgr_register().
>>>>    - replaced unnecessary ternary operators in return statements with standard
>>>>      if conditions.
>>>> ---
>>>>   drivers/fpga/altera-cvp.c        |  12 +--
>>>>   drivers/fpga/altera-pr-ip-core.c |   8 +-
>>>>   drivers/fpga/altera-ps-spi.c     |  10 +-
>>>>   drivers/fpga/dfl-fme-mgr.c       |  10 +-
>>>>   drivers/fpga/fpga-mgr.c          | 163 ++++++++-----------------------
>>>>   drivers/fpga/ice40-spi.c         |  10 +-
>>>>   drivers/fpga/machxo2-spi.c       |  10 +-
>>>>   drivers/fpga/socfpga-a10.c       |  16 ++-
>>>>   drivers/fpga/socfpga.c           |  10 +-
>>>>   drivers/fpga/stratix10-soc.c     |  15 +--
>>>>   drivers/fpga/ts73xx-fpga.c       |  10 +-
>>>>   drivers/fpga/xilinx-spi.c        |  12 +--
>>>>   drivers/fpga/zynq-fpga.c         |  16 ++-
>>>>   drivers/fpga/zynqmp-fpga.c       |  10 +-
>>>>   include/linux/fpga/fpga-mgr.h    |  17 ++--
>>>>   15 files changed, 112 insertions(+), 217 deletions(-)
>>>>
>>>> diff --git a/drivers/fpga/altera-cvp.c b/drivers/fpga/altera-cvp.c
>>>> index 4e0edb60bfba..b61020ceb0a1 100644
>>>> --- a/drivers/fpga/altera-cvp.c
>>>> +++ b/drivers/fpga/altera-cvp.c
>>>> @@ -652,19 +652,15 @@ static int altera_cvp_probe(struct pci_dev *pdev,
>>>>   	snprintf(conf->mgr_name, sizeof(conf->mgr_name), "%s @%s",
>>>>   		 ALTERA_CVP_MGR_NAME, pci_name(pdev));
>>>>   
>>>> -	mgr = devm_fpga_mgr_create(&pdev->dev, conf->mgr_name,
>>>> -				   &altera_cvp_ops, conf);
>>>> -	if (!mgr) {
>>>> -		ret = -ENOMEM;
>>>> +	mgr = fpga_mgr_register(&pdev->dev, conf->mgr_name,
>>>> +				&altera_cvp_ops, conf);
>>>> +	if (IS_ERR(mgr)) {
>>>> +		ret = PTR_ERR(mgr);
>>>>   		goto err_unmap;
>>>>   	}
>>>>   
>>>>   	pci_set_drvdata(pdev, mgr);
>>>>   
>>>> -	ret = fpga_mgr_register(mgr);
>>>> -	if (ret)
>>>> -		goto err_unmap;
>>>> -
>>>>   	return 0;
>>>>   
>>>>   err_unmap:
>>>> diff --git a/drivers/fpga/altera-pr-ip-core.c b/drivers/fpga/altera-pr-ip-core.c
>>>> index 5b130c4d9882..0980e134cc43 100644
>>>> --- a/drivers/fpga/altera-pr-ip-core.c
>>>> +++ b/drivers/fpga/altera-pr-ip-core.c
>>>> @@ -191,11 +191,11 @@ int alt_pr_register(struct device *dev, void __iomem *reg_base)
>>>>   		(val & ALT_PR_CSR_STATUS_MSK) >> ALT_PR_CSR_STATUS_SFT,
>>>>   		(int)(val & ALT_PR_CSR_PR_START));
>>>>   
>>>> -	mgr = devm_fpga_mgr_create(dev, dev_name(dev), &alt_pr_ops, priv);
>>>> -	if (!mgr)
>>>> -		return -ENOMEM;
>>>> +	mgr = devm_fpga_mgr_register(dev, dev_name(dev), &alt_pr_ops, priv);
>>>> +	if (IS_ERR(mgr))
>>>> +		return PTR_ERR(mgr);
>>>>   
>>>> -	return devm_fpga_mgr_register(dev, mgr);
>>>> +	return 0;
>>>>   }
>>>>   EXPORT_SYMBOL_GPL(alt_pr_register);
>>>>   
>>>> diff --git a/drivers/fpga/altera-ps-spi.c b/drivers/fpga/altera-ps-spi.c
>>>> index 23bfd4d1ad0f..d3f77b0312b2 100644
>>>> --- a/drivers/fpga/altera-ps-spi.c
>>>> +++ b/drivers/fpga/altera-ps-spi.c
>>>> @@ -302,12 +302,12 @@ static int altera_ps_probe(struct spi_device *spi)
>>>>   	snprintf(conf->mgr_name, sizeof(conf->mgr_name), "%s %s",
>>>>   		 dev_driver_string(&spi->dev), dev_name(&spi->dev));
>>>>   
>>>> -	mgr = devm_fpga_mgr_create(&spi->dev, conf->mgr_name,
>>>> -				   &altera_ps_ops, conf);
>>>> -	if (!mgr)
>>>> -		return -ENOMEM;
>>>> +	mgr = devm_fpga_mgr_register(&spi->dev, conf->mgr_name,
>>>> +				     &altera_ps_ops, conf);
>>>> +	if (IS_ERR(mgr))
>>>> +		return PTR_ERR(mgr);
>>>>   
>>>> -	return devm_fpga_mgr_register(&spi->dev, mgr);
>>>> +	return 0;
>>>>   }
>>>>   
>>>>   static const struct spi_device_id altera_ps_spi_ids[] = {
>>>> diff --git a/drivers/fpga/dfl-fme-mgr.c b/drivers/fpga/dfl-fme-mgr.c
>>>> index d5861d13b306..d71eba817b3b 100644
>>>> --- a/drivers/fpga/dfl-fme-mgr.c
>>>> +++ b/drivers/fpga/dfl-fme-mgr.c
>>>> @@ -308,14 +308,14 @@ static int fme_mgr_probe(struct platform_device *pdev)
>>>>   
>>>>   	fme_mgr_get_compat_id(priv->ioaddr, compat_id);
>>>>   
>>>> -	mgr = devm_fpga_mgr_create(dev, "DFL FME FPGA Manager",
>>>> -				   &fme_mgr_ops, priv);
>>>> -	if (!mgr)
>>>> -		return -ENOMEM;
>>>> +	mgr = devm_fpga_mgr_register(dev, "DFL FME FPGA Manager",
>>>> +				     &fme_mgr_ops, priv);
>>>> +	if (IS_ERR(mgr))
>>>> +		return PTR_ERR(mgr);
>>>>   
>>>>   	mgr->compat_id = compat_id;
>>>>   
>>>> -	return devm_fpga_mgr_register(dev, mgr);
>>>> +	return 0;
>>>>   }
>>>>   
>>>>   static struct platform_driver fme_mgr_driver = {
>>>> diff --git a/drivers/fpga/fpga-mgr.c b/drivers/fpga/fpga-mgr.c
>>>> index b85bc47c91a9..22f0dd5c2834 100644
>>>> --- a/drivers/fpga/fpga-mgr.c
>>>> +++ b/drivers/fpga/fpga-mgr.c
>>>> @@ -550,20 +550,20 @@ void fpga_mgr_unlock(struct fpga_manager *mgr)
>>>>   EXPORT_SYMBOL_GPL(fpga_mgr_unlock);
>>>>   
>>>>   /**
>>>> - * fpga_mgr_create - create and initialize a FPGA manager struct
>>>> + * fpga_mgr_register - create and register a FPGA Manager device
>>>>    * @dev:	fpga manager device from pdev
>>>>    * @name:	fpga manager name
>>>>    * @mops:	pointer to structure of fpga manager ops
>>>>    * @priv:	fpga manager private data
>>>>    *
>>>> - * The caller of this function is responsible for freeing the struct with
>>>> - * fpga_mgr_free().  Using devm_fpga_mgr_create() instead is recommended.
>>>> + * The caller of this function is responsible for calling fpga_mgr_unregister().
>>>> + * Using devm_fpga_mgr_register instead is recommended.
>>>>    *
>>>> - * Return: pointer to struct fpga_manager or NULL
>>>> + * Returns a struct fpga_manager pointer on success, or ERR_PTR() on error.
>>>>    */
>>>> -struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
>>>> -				     const struct fpga_manager_ops *mops,
>>>> -				     void *priv)
>>>> +struct fpga_manager *
>>>> +fpga_mgr_register(struct device *dev, const char *name,
>>>> +		  const struct fpga_manager_ops *mops, void *priv)
>>>>   {
>>>>   	struct fpga_manager *mgr;
>>>>   	int id, ret;
>>>> @@ -572,21 +572,23 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
>>>>   	    !mops->write_init || (!mops->write && !mops->write_sg) ||
>>>>   	    (mops->write && mops->write_sg)) {
>>>>   		dev_err(dev, "Attempt to register without fpga_manager_ops\n");
>>>> -		return NULL;
>>>> +		return ERR_PTR(-EINVAL);
>>>>   	}
>>>>   
>>>>   	if (!name || !strlen(name)) {
>>>>   		dev_err(dev, "Attempt to register with no name!\n");
>>>> -		return NULL;
>>>> +		return ERR_PTR(-EINVAL);
>>>>   	}
>>>>   
>>>>   	mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
>>>>   	if (!mgr)
>>>> -		return NULL;
>>>> +		return ERR_PTR(-ENOMEM);
>>>>   
>>>>   	id = ida_simple_get(&fpga_mgr_ida, 0, 0, GFP_KERNEL);
>>>> -	if (id < 0)
>>>> +	if (id < 0) {
>>>> +		ret = id;
>>>>   		goto error_kfree;
>>>> +	}
>>>>   
>>>>   	mutex_init(&mgr->ref_mutex);
>>>>   
>>>> @@ -594,7 +596,6 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
>>>>   	mgr->mops = mops;
>>>>   	mgr->priv = priv;
>>>>   
>>>> -	device_initialize(&mgr->dev);
>>>>   	mgr->dev.class = fpga_mgr_class;
>>>>   	mgr->dev.groups = mops->groups;
>>>>   	mgr->dev.parent = dev;
>>>> @@ -605,84 +606,6 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
>>>>   	if (ret)
>>>>   		goto error_device;
>>>>   
>>>> -	return mgr;
>>>> -
>>>> -error_device:
>>>> -	ida_simple_remove(&fpga_mgr_ida, id);
>>>> -error_kfree:
>>>> -	kfree(mgr);
>>>> -
>>>> -	return NULL;
>>>> -}
>>>> -EXPORT_SYMBOL_GPL(fpga_mgr_create);
>>>> -
>>>> -/**
>>>> - * fpga_mgr_free - free a FPGA manager created with fpga_mgr_create()
>>>> - * @mgr:	fpga manager struct
>>>> - */
>>>> -void fpga_mgr_free(struct fpga_manager *mgr)
>>>> -{
>>>> -	ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
>>>> -	kfree(mgr);
>>>> -}
>>>> -EXPORT_SYMBOL_GPL(fpga_mgr_free);
>>>> -
>>>> -static void devm_fpga_mgr_release(struct device *dev, void *res)
>>>> -{
>>>> -	struct fpga_mgr_devres *dr = res;
>>>> -
>>>> -	fpga_mgr_free(dr->mgr);
>>>> -}
>>>> -
>>>> -/**
>>>> - * devm_fpga_mgr_create - create and initialize a managed FPGA manager struct
>>>> - * @dev:	fpga manager device from pdev
>>>> - * @name:	fpga manager name
>>>> - * @mops:	pointer to structure of fpga manager ops
>>>> - * @priv:	fpga manager private data
>>>> - *
>>>> - * This function is intended for use in a FPGA manager driver's probe function.
>>>> - * After the manager driver creates the manager struct with
>>>> - * devm_fpga_mgr_create(), it should register it with fpga_mgr_register().  The
>>>> - * manager driver's remove function should call fpga_mgr_unregister().  The
>>>> - * manager struct allocated with this function will be freed automatically on
>>>> - * driver detach.  This includes the case of a probe function returning error
>>>> - * before calling fpga_mgr_register(), the struct will still get cleaned up.
>>>> - *
>>>> - * Return: pointer to struct fpga_manager or NULL
>>>> - */
>>>> -struct fpga_manager *devm_fpga_mgr_create(struct device *dev, const char *name,
>>>> -					  const struct fpga_manager_ops *mops,
>>>> -					  void *priv)
>>>> -{
>>>> -	struct fpga_mgr_devres *dr;
>>>> -
>>>> -	dr = devres_alloc(devm_fpga_mgr_release, sizeof(*dr), GFP_KERNEL);
>>>> -	if (!dr)
>>>> -		return NULL;
>>>> -
>>>> -	dr->mgr = fpga_mgr_create(dev, name, mops, priv);
>>>> -	if (!dr->mgr) {
>>>> -		devres_free(dr);
>>>> -		return NULL;
>>>> -	}
>>>> -
>>>> -	devres_add(dev, dr);
>>>> -
>>>> -	return dr->mgr;
>>>> -}
>>>> -EXPORT_SYMBOL_GPL(devm_fpga_mgr_create);
>>>> -
>>>> -/**
>>>> - * fpga_mgr_register - register a FPGA manager
>>>> - * @mgr: fpga manager struct
>>>> - *
>>>> - * Return: 0 on success, negative error code otherwise.
>>>> - */
>>>> -int fpga_mgr_register(struct fpga_manager *mgr)
>>>> -{
>>>> -	int ret;
>>>> -
>>>>   	/*
>>>>   	 * Initialize framework state by requesting low level driver read state
>>>>   	 * from device.  FPGA may be in reset mode or may have been programmed
>>>> @@ -690,18 +613,20 @@ int fpga_mgr_register(struct fpga_manager *mgr)
>>>>   	 */
>>>>   	mgr->state = mgr->mops->state(mgr);
>>>>   
>>>> -	ret = device_add(&mgr->dev);
>>>> -	if (ret)
>>>> -		goto error_device;
>>>> -
>>>> -	dev_info(&mgr->dev, "%s registered\n", mgr->name);
>>>> +	ret = device_register(&mgr->dev);
>>>> +	if (ret) {
>>>> +		put_device(&mgr->dev);
>>>> +		return ERR_PTR(ret);
>>>> +	}
>>>>   
>>>> -	return 0;
>>>> +	return mgr;
>>>>   
>>>>   error_device:
>>>> -	ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
>>>> +	ida_simple_remove(&fpga_mgr_ida, id);
>>>> +error_kfree:
>>>> +	kfree(mgr);
>>>>   
>>>> -	return ret;
>>>> +	return ERR_PTR(ret);
>>>>   }
>>>>   EXPORT_SYMBOL_GPL(fpga_mgr_register);
>>>>   
>>>> @@ -726,14 +651,6 @@ void fpga_mgr_unregister(struct fpga_manager *mgr)
>>>>   }
>>>>   EXPORT_SYMBOL_GPL(fpga_mgr_unregister);
>>>>   
>>>> -static int fpga_mgr_devres_match(struct device *dev, void *res,
>>>> -				 void *match_data)
>>>> -{
>>>> -	struct fpga_mgr_devres *dr = res;
>>>> -
>>>> -	return match_data == dr->mgr;
>>>> -}
>>>> -
>>>>   static void devm_fpga_mgr_unregister(struct device *dev, void *res)
>>>>   {
>>>>   	struct fpga_mgr_devres *dr = res;
>>>> @@ -743,44 +660,44 @@ static void devm_fpga_mgr_unregister(struct device *dev, void *res)
>>>>   
>>>>   /**
>>>>    * devm_fpga_mgr_register - resource managed variant of fpga_mgr_register()
>>>> - * @dev: managing device for this FPGA manager
>>>> - * @mgr: fpga manager struct
>>>> + * @dev:	fpga manager device from pdev
>>>> + * @name:	fpga manager name
>>>> + * @mops:	pointer to structure of fpga manager ops
>>>> + * @priv:	fpga manager private data
>>>>    *
>>>>    * This is the devres variant of fpga_mgr_register() for which the unregister
>>>>    * function will be called automatically when the managing device is detached.
>>>>    */
>>>> -int devm_fpga_mgr_register(struct device *dev, struct fpga_manager *mgr)
>>>> +struct fpga_manager *
>>>> +devm_fpga_mgr_register(struct device *dev, const char *name,
>>>> +		       const struct fpga_manager_ops *mops, void *priv)
>>>>   {
>>>>   	struct fpga_mgr_devres *dr;
>>>> -	int ret;
>>>> -
>>>> -	/*
>>>> -	 * Make sure that the struct fpga_manager * that is passed in is
>>>> -	 * managed itself.
>>>> -	 */
>>>> -	if (WARN_ON(!devres_find(dev, devm_fpga_mgr_release,
>>>> -				 fpga_mgr_devres_match, mgr)))
>>>> -		return -EINVAL;
>>>> +	struct fpga_manager *mgr;
>>>>   
>>>>   	dr = devres_alloc(devm_fpga_mgr_unregister, sizeof(*dr), GFP_KERNEL);
>>>>   	if (!dr)
>>>> -		return -ENOMEM;
>>>> +		return ERR_PTR(-ENOMEM);
>>>>   
>>>> -	ret = fpga_mgr_register(mgr);
>>>> -	if (ret) {
>>>> +	mgr = fpga_mgr_register(dev, name, mops, priv);
>>>> +	if (IS_ERR(mgr)) {
>>>>   		devres_free(dr);
>>>> -		return ret;
>>>> +		return mgr;
>>>>   	}
>>>>   
>>>>   	dr->mgr = mgr;
>>>>   	devres_add(dev, dr);
>>>>   
>>>> -	return 0;
>>>> +	return mgr;
>>>>   }
>>>>   EXPORT_SYMBOL_GPL(devm_fpga_mgr_register);
>>>>   
>>>>   static void fpga_mgr_dev_release(struct device *dev)
>>>>   {
>>>> +	struct fpga_manager *mgr = to_fpga_manager(dev);
>>>> +
>>>> +	ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
>>>> +	kfree(mgr);
>>>>   }
>>>>   
>>>>   static int __init fpga_mgr_class_init(void)
>>>> diff --git a/drivers/fpga/ice40-spi.c b/drivers/fpga/ice40-spi.c
>>>> index 69dec5af23c3..91ac25f339f3 100644
>>>> --- a/drivers/fpga/ice40-spi.c
>>>> +++ b/drivers/fpga/ice40-spi.c
>>>> @@ -178,12 +178,12 @@ static int ice40_fpga_probe(struct spi_device *spi)
>>>>   		return ret;
>>>>   	}
>>>>   
>>>> -	mgr = devm_fpga_mgr_create(dev, "Lattice iCE40 FPGA Manager",
>>>> -				   &ice40_fpga_ops, priv);
>>>> -	if (!mgr)
>>>> -		return -ENOMEM;
>>>> +	mgr = devm_fpga_mgr_register(dev, "Lattice iCE40 FPGA Manager",
>>>> +				     &ice40_fpga_ops, priv);
>>>> +	if (IS_ERR(mgr))
>>>> +		return PTR_ERR(mgr);
>>>>   
>>>> -	return devm_fpga_mgr_register(dev, mgr);
>>>> +	return 0;
>>>>   }
>>>>   
>>>>   static const struct of_device_id ice40_fpga_of_match[] = {
>>>> diff --git a/drivers/fpga/machxo2-spi.c b/drivers/fpga/machxo2-spi.c
>>>> index 114a64d2b7a4..0137c1e2e706 100644
>>>> --- a/drivers/fpga/machxo2-spi.c
>>>> +++ b/drivers/fpga/machxo2-spi.c
>>>> @@ -366,12 +366,12 @@ static int machxo2_spi_probe(struct spi_device *spi)
>>>>   		return -EINVAL;
>>>>   	}
>>>>   
>>>> -	mgr = devm_fpga_mgr_create(dev, "Lattice MachXO2 SPI FPGA Manager",
>>>> -				   &machxo2_ops, spi);
>>>> -	if (!mgr)
>>>> -		return -ENOMEM;
>>>> +	mgr = devm_fpga_mgr_register(dev, "Lattice MachXO2 SPI FPGA Manager",
>>>> +				     &machxo2_ops, spi);
>>>> +	if (IS_ERR(mgr))
>>>> +		return PTR_ERR(mgr);
>>>>   
>>>> -	return devm_fpga_mgr_register(dev, mgr);
>>>> +	return 0;
>>>>   }
>>>>   
>>>>   static const struct of_device_id of_match[] = {
>>>> diff --git a/drivers/fpga/socfpga-a10.c b/drivers/fpga/socfpga-a10.c
>>>> index 573d88bdf730..ac8e89b8a5cc 100644
>>>> --- a/drivers/fpga/socfpga-a10.c
>>>> +++ b/drivers/fpga/socfpga-a10.c
>>>> @@ -508,19 +508,15 @@ static int socfpga_a10_fpga_probe(struct platform_device *pdev)
>>>>   		return -EBUSY;
>>>>   	}
>>>>   
>>>> -	mgr = devm_fpga_mgr_create(dev, "SoCFPGA Arria10 FPGA Manager",
>>>> -				   &socfpga_a10_fpga_mgr_ops, priv);
>>>> -	if (!mgr)
>>>> -		return -ENOMEM;
>>>> -
>>>> -	platform_set_drvdata(pdev, mgr);
>>>> -
>>>> -	ret = fpga_mgr_register(mgr);
>>>> -	if (ret) {
>>>> +	mgr = fpga_mgr_register(dev, "SoCFPGA Arria10 FPGA Manager",
>>>> +				&socfpga_a10_fpga_mgr_ops, priv);
>>>> +	if (IS_ERR(mgr)) {
>>>>   		clk_disable_unprepare(priv->clk);
>>>> -		return ret;
>>>> +		return PTR_ERR(mgr);
>>>>   	}
>>>>   
>>>> +	platform_set_drvdata(pdev, mgr);
>>>> +
>>>>   	return 0;
>>>>   }
>>>>   
>>>> diff --git a/drivers/fpga/socfpga.c b/drivers/fpga/socfpga.c
>>>> index 1f467173fc1f..ebfa9f4d24ef 100644
>>>> --- a/drivers/fpga/socfpga.c
>>>> +++ b/drivers/fpga/socfpga.c
>>>> @@ -571,12 +571,12 @@ static int socfpga_fpga_probe(struct platform_device *pdev)
>>>>   	if (ret)
>>>>   		return ret;
>>>>   
>>>> -	mgr = devm_fpga_mgr_create(dev, "Altera SOCFPGA FPGA Manager",
>>>> -				   &socfpga_fpga_ops, priv);
>>>> -	if (!mgr)
>>>> -		return -ENOMEM;
>>>> +	mgr = devm_fpga_mgr_register(dev, "Altera SOCFPGA FPGA Manager",
>>>> +				     &socfpga_fpga_ops, priv);
>>>> +	if (IS_ERR(mgr))
>>>> +		return PTR_ERR(mgr);
>>>>   
>>>> -	return devm_fpga_mgr_register(dev, mgr);
>>>> +	return 0;
>>>>   }
>>>>   
>>>>   #ifdef CONFIG_OF
>>>> diff --git a/drivers/fpga/stratix10-soc.c b/drivers/fpga/stratix10-soc.c
>>>> index 657a70c5fc99..e0bd91b3f7f8 100644
>>>> --- a/drivers/fpga/stratix10-soc.c
>>>> +++ b/drivers/fpga/stratix10-soc.c
>>>> @@ -425,18 +425,11 @@ static int s10_probe(struct platform_device *pdev)
>>>>   
>>>>   	init_completion(&priv->status_return_completion);
>>>>   
>>>> -	mgr = fpga_mgr_create(dev, "Stratix10 SOC FPGA Manager",
>>>> -			      &s10_ops, priv);
>>>> -	if (!mgr) {
>>>> -		dev_err(dev, "unable to create FPGA manager\n");
>>>> -		ret = -ENOMEM;
>>>> -		goto probe_err;
>>>> -	}
>>>> -
>>>> -	ret = fpga_mgr_register(mgr);
>>>> -	if (ret) {
>>>> +	mgr = fpga_mgr_register(dev, "Stratix10 SOC FPGA Manager",
>>>> +				&s10_ops, priv);
>>>> +	if (IS_ERR(mgr)) {
>>>>   		dev_err(dev, "unable to register FPGA manager\n");
>>>> -		fpga_mgr_free(mgr);
>>>> +		ret = PTR_ERR(mgr);
>>>>   		goto probe_err;
>>>>   	}
>>>>   
>>>> diff --git a/drivers/fpga/ts73xx-fpga.c b/drivers/fpga/ts73xx-fpga.c
>>>> index 101f016c6ed8..5efbec2b9f9f 100644
>>>> --- a/drivers/fpga/ts73xx-fpga.c
>>>> +++ b/drivers/fpga/ts73xx-fpga.c
>>>> @@ -122,12 +122,12 @@ static int ts73xx_fpga_probe(struct platform_device *pdev)
>>>>   	if (IS_ERR(priv->io_base))
>>>>   		return PTR_ERR(priv->io_base);
>>>>   
>>>> -	mgr = devm_fpga_mgr_create(kdev, "TS-73xx FPGA Manager",
>>>> -				   &ts73xx_fpga_ops, priv);
>>>> -	if (!mgr)
>>>> -		return -ENOMEM;
>>>> +	mgr = devm_fpga_mgr_register(kdev, "TS-73xx FPGA Manager",
>>>> +				     &ts73xx_fpga_ops, priv);
>>>> +	if (IS_ERR(mgr))
>>>> +		return PTR_ERR(mgr);
>>>>   
>>>> -	return devm_fpga_mgr_register(kdev, mgr);
>>>> +	return 0;
>>>>   }
>>>>   
>>>>   static struct platform_driver ts73xx_fpga_driver = {
>>>> diff --git a/drivers/fpga/xilinx-spi.c b/drivers/fpga/xilinx-spi.c
>>>> index fee4d0abf6bf..efda475f7a98 100644
>>>> --- a/drivers/fpga/xilinx-spi.c
>>>> +++ b/drivers/fpga/xilinx-spi.c
>>>> @@ -247,13 +247,13 @@ static int xilinx_spi_probe(struct spi_device *spi)
>>>>   		return dev_err_probe(&spi->dev, PTR_ERR(conf->done),
>>>>   				     "Failed to get DONE gpio\n");
>>>>   
>>>> -	mgr = devm_fpga_mgr_create(&spi->dev,
>>>> -				   "Xilinx Slave Serial FPGA Manager",
>>>> -				   &xilinx_spi_ops, conf);
>>>> -	if (!mgr)
>>>> -		return -ENOMEM;
>>>> +	mgr = devm_fpga_mgr_register(&spi->dev,
>>>> +				     "Xilinx Slave Serial FPGA Manager",
>>>> +				     &xilinx_spi_ops, conf);
>>>> +	if (IS_ERR(mgr))
>>>> +		return PTR_ERR(mgr);
>>>>   
>>>> -	return devm_fpga_mgr_register(&spi->dev, mgr);
>>>> +	return 0;
>>>>   }
>>>>   
>>>>   static const struct of_device_id xlnx_spi_of_match[] = {
>>>> diff --git a/drivers/fpga/zynq-fpga.c b/drivers/fpga/zynq-fpga.c
>>>> index 07fa8d9ec675..74bbb9710f4e 100644
>>>> --- a/drivers/fpga/zynq-fpga.c
>>>> +++ b/drivers/fpga/zynq-fpga.c
>>>> @@ -609,20 +609,16 @@ static int zynq_fpga_probe(struct platform_device *pdev)
>>>>   
>>>>   	clk_disable(priv->clk);
>>>>   
>>>> -	mgr = devm_fpga_mgr_create(dev, "Xilinx Zynq FPGA Manager",
>>>> -				   &zynq_fpga_ops, priv);
>>>> -	if (!mgr)
>>>> -		return -ENOMEM;
>>>> -
>>>> -	platform_set_drvdata(pdev, mgr);
>>>> -
>>>> -	err = fpga_mgr_register(mgr);
>>>> -	if (err) {
>>>> +	mgr = fpga_mgr_register(dev, "Xilinx Zynq FPGA Manager",
>>>> +				&zynq_fpga_ops, priv);
>>>> +	if (IS_ERR(mgr)) {
>>>>   		dev_err(dev, "unable to register FPGA manager\n");
>>>>   		clk_unprepare(priv->clk);
>>>> -		return err;
>>>> +		return PTR_ERR(mgr);
>>>>   	}
>>>>   
>>>> +	platform_set_drvdata(pdev, mgr);
>>>> +
>>>>   	return 0;
>>>>   }
>>>>   
>>>> diff --git a/drivers/fpga/zynqmp-fpga.c b/drivers/fpga/zynqmp-fpga.c
>>>> index 125743c9797f..1a6d520985be 100644
>>>> --- a/drivers/fpga/zynqmp-fpga.c
>>>> +++ b/drivers/fpga/zynqmp-fpga.c
>>>> @@ -102,12 +102,12 @@ static int zynqmp_fpga_probe(struct platform_device *pdev)
>>>>   
>>>>   	priv->dev = dev;
>>>>   
>>>> -	mgr = devm_fpga_mgr_create(dev, "Xilinx ZynqMP FPGA Manager",
>>>> -				   &zynqmp_fpga_ops, priv);
>>>> -	if (!mgr)
>>>> -		return -ENOMEM;
>>>> +	mgr = devm_fpga_mgr_register(dev, "Xilinx ZynqMP FPGA Manager",
>>>> +				     &zynqmp_fpga_ops, priv);
>>>> +	if (IS_ERR(mgr))
>>>> +		return PTR_ERR(mgr);
>>>>   
>>>> -	return devm_fpga_mgr_register(dev, mgr);
>>>> +	return 0;
>>>>   }
>>>>   
>>>>   static const struct of_device_id zynqmp_fpga_of_match[] = {
>>>> diff --git a/include/linux/fpga/fpga-mgr.h b/include/linux/fpga/fpga-mgr.h
>>>> index 2bc3030a69e5..d1ccf8f8c38a 100644
>>>> --- a/include/linux/fpga/fpga-mgr.h
>>>> +++ b/include/linux/fpga/fpga-mgr.h
>>>> @@ -191,17 +191,14 @@ struct fpga_manager *fpga_mgr_get(struct device *dev);
>>>>   
>>>>   void fpga_mgr_put(struct fpga_manager *mgr);
>>>>   
>>>> -struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
>>>> -				     const struct fpga_manager_ops *mops,
>>>> -				     void *priv);
>>>> -void fpga_mgr_free(struct fpga_manager *mgr);
>>>> -int fpga_mgr_register(struct fpga_manager *mgr);
>>>> +struct fpga_manager *fpga_mgr_register(struct device *dev, const char *name,
>>>> +				       const struct fpga_manager_ops *mops,
>>>> +				       void *priv);
>>>>   void fpga_mgr_unregister(struct fpga_manager *mgr);
>>>>   
>>>> -int devm_fpga_mgr_register(struct device *dev, struct fpga_manager *mgr);
>>>> -
>>>> -struct fpga_manager *devm_fpga_mgr_create(struct device *dev, const char *name,
>>>> -					  const struct fpga_manager_ops *mops,
>>>> -					  void *priv);
>>>> +struct fpga_manager *
>>>> +devm_fpga_mgr_register(struct device *dev, const char *name,
>>>> +		       const struct fpga_manager_ops *mops,
>>>> +		       void *priv);
>>>>   
>>>>   #endif /*_LINUX_FPGA_MGR_H */
>>>> -- 
>>>> 2.25.1
>>>>
>>> Moritz


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v3 1/3] fpga: mgr: Use standard dev_release for class driver
  2021-05-25 16:51     ` Russ Weight
@ 2021-05-28 14:46       ` Xu Yilun
  0 siblings, 0 replies; 17+ messages in thread
From: Xu Yilun @ 2021-05-28 14:46 UTC (permalink / raw)
  To: Russ Weight
  Cc: Tom Rix, mdf, linux-fpga, lgoncalv, hao.wu, matthew.gerlach,
	richard.gong

On Tue, May 25, 2021 at 09:51:35AM -0700, Russ Weight wrote:
> Hi Tom,
> 
> On 5/24/21 1:16 PM, Tom Rix wrote:
> > Russ,
> >
> > I think the signature fpga_mgr_register, should change to
> >
> > int fpga_mgr_register(struct device *, const struct fpga_mager_ops *mops, void *priv);
> >
> > I'll explain below ..
> >
> > On 5/24/21 11:50 AM, Russ Weight wrote:
> >> The FPGA manager class driver data structure is being treated as a
> >> managed resource instead of using standard dev_release call-back
> >> to release the class data structure. This change removes the
> >> managed resource code for the freeing of the class data structure
> >> and combines the create() and register() functions into a single
> >> register() function.
> >>
> >> The devm_fpga_mgr_register() function is retained.
> >>
> >> Signed-off-by: Russ Weight <russell.h.weight@intel.com>
> >> ---
> >> v3:
> >>    - Cleaned up comment header for fpga_mgr_register()
> >>    - Fix error return on ida_simple_get() failure
> >> v2:
> >>    - Restored devm_fpga_mgr_register() functionality, adapted for the combined
> >>      create/register functionality.
> >>    - All previous callers of devm_fpga_mgr_register() will continue to call
> >>      devm_fpga_mgr_register().
> >>    - replaced unnecessary ternary operators in return statements with standard
> >>      if conditions.
> >> ---
> >>   drivers/fpga/altera-cvp.c        |  12 +--
> >>   drivers/fpga/altera-pr-ip-core.c |   8 +-
> >>   drivers/fpga/altera-ps-spi.c     |  10 +-
> >>   drivers/fpga/dfl-fme-mgr.c       |  10 +-
> >>   drivers/fpga/fpga-mgr.c          | 163 ++++++++-----------------------
> >>   drivers/fpga/ice40-spi.c         |  10 +-
> >>   drivers/fpga/machxo2-spi.c       |  10 +-
> >>   drivers/fpga/socfpga-a10.c       |  16 ++-
> >>   drivers/fpga/socfpga.c           |  10 +-
> >>   drivers/fpga/stratix10-soc.c     |  15 +--
> >>   drivers/fpga/ts73xx-fpga.c       |  10 +-
> >>   drivers/fpga/xilinx-spi.c        |  12 +--
> >>   drivers/fpga/zynq-fpga.c         |  16 ++-
> >>   drivers/fpga/zynqmp-fpga.c       |  10 +-
> >>   include/linux/fpga/fpga-mgr.h    |  17 ++--
> >>   15 files changed, 112 insertions(+), 217 deletions(-)
> >>
> >> diff --git a/drivers/fpga/altera-cvp.c b/drivers/fpga/altera-cvp.c
> >> index 4e0edb60bfba..b61020ceb0a1 100644
> >> --- a/drivers/fpga/altera-cvp.c
> >> +++ b/drivers/fpga/altera-cvp.c
> >> @@ -652,19 +652,15 @@ static int altera_cvp_probe(struct pci_dev *pdev,
> >>       snprintf(conf->mgr_name, sizeof(conf->mgr_name), "%s @%s",
> >>            ALTERA_CVP_MGR_NAME, pci_name(pdev));
> >>   -    mgr = devm_fpga_mgr_create(&pdev->dev, conf->mgr_name,
> >> -                   &altera_cvp_ops, conf);
> >> -    if (!mgr) {
> >> -        ret = -ENOMEM;
> >> +    mgr = fpga_mgr_register(&pdev->dev, conf->mgr_name,
> >> +                &altera_cvp_ops, conf);
> >> +    if (IS_ERR(mgr)) {
> >> +        ret = PTR_ERR(mgr);
> >>           goto err_unmap;
> >>       }
> >>         pci_set_drvdata(pdev, mgr);
> >
> > This is a wrapper around
> >
> > dev_set_drvdata()
> >
> >>   -    ret = fpga_mgr_register(mgr);
> >> -    if (ret)
> >> -        goto err_unmap;
> >> -
> >>       return 0;
> >>     err_unmap:
> >> diff --git a/drivers/fpga/altera-pr-ip-core.c b/drivers/fpga/altera-pr-ip-core.c
> >> index 5b130c4d9882..0980e134cc43 100644
> >> --- a/drivers/fpga/altera-pr-ip-core.c
> >> +++ b/drivers/fpga/altera-pr-ip-core.c
> >> @@ -191,11 +191,11 @@ int alt_pr_register(struct device *dev, void __iomem *reg_base)
> >>           (val & ALT_PR_CSR_STATUS_MSK) >> ALT_PR_CSR_STATUS_SFT,
> >>           (int)(val & ALT_PR_CSR_PR_START));
> >>   -    mgr = devm_fpga_mgr_create(dev, dev_name(dev), &alt_pr_ops, priv);
> > using the name of the device
> >> -    if (!mgr)
> >> -        return -ENOMEM;
> >> +    mgr = devm_fpga_mgr_register(dev, dev_name(dev), &alt_pr_ops, priv);
> >> +    if (IS_ERR(mgr))
> >> +        return PTR_ERR(mgr);
> > mgr is unused
> >>   -    return devm_fpga_mgr_register(dev, mgr);
> >> +    return 0;
> >>   }
> >>   EXPORT_SYMBOL_GPL(alt_pr_register);
> >>   diff --git a/drivers/fpga/altera-ps-spi.c b/drivers/fpga/altera-ps-spi.c
> >> index 23bfd4d1ad0f..d3f77b0312b2 100644
> >> --- a/drivers/fpga/altera-ps-spi.c
> >> +++ b/drivers/fpga/altera-ps-spi.c
> >> @@ -302,12 +302,12 @@ static int altera_ps_probe(struct spi_device *spi)
> >>       snprintf(conf->mgr_name, sizeof(conf->mgr_name), "%s %s",
> >>            dev_driver_string(&spi->dev), dev_name(&spi->dev));
> > a combo of devices name, is that really necessary ?
> 
> I would like to limit the scope of this patch to address:
> (1) a populated dev_release function
> (2) combining the device_initialize() and device_add() calls into single call
>     to device_register()
> 
> Changing the name or removing the sysfs name file could have implications for
> users who expect it to be there or, worse yet, have a script that looks for it.
> I think these could be done in a separate patch if needed.

I agree.

> 
> >>   -    mgr = devm_fpga_mgr_create(&spi->dev, conf->mgr_name,
> >> -                   &altera_ps_ops, conf);
> >> -    if (!mgr)
> >> -        return -ENOMEM;
> >> +    mgr = devm_fpga_mgr_register(&spi->dev, conf->mgr_name,
> >> +                     &altera_ps_ops, conf);
> >> +    if (IS_ERR(mgr))
> >> +        return PTR_ERR(mgr);
> > mgr is unused
> >>   -    return devm_fpga_mgr_register(&spi->dev, mgr);
> >> +    return 0;
> >>   }
> >>     static const struct spi_device_id altera_ps_spi_ids[] = {
> >> diff --git a/drivers/fpga/dfl-fme-mgr.c b/drivers/fpga/dfl-fme-mgr.c
> >> index d5861d13b306..d71eba817b3b 100644
> >> --- a/drivers/fpga/dfl-fme-mgr.c
> >> +++ b/drivers/fpga/dfl-fme-mgr.c
> >> @@ -308,14 +308,14 @@ static int fme_mgr_probe(struct platform_device *pdev)
> >>         fme_mgr_get_compat_id(priv->ioaddr, compat_id);
> >>   -    mgr = devm_fpga_mgr_create(dev, "DFL FME FPGA Manager",
> >> -                   &fme_mgr_ops, priv);
> >> -    if (!mgr)
> >> -        return -ENOMEM;
> >> +    mgr = devm_fpga_mgr_register(dev, "DFL FME FPGA Manager",
> >> +                     &fme_mgr_ops, priv);
> > drv name is 'dfl-fme-mgr', is this close enough ?
> >> +    if (IS_ERR(mgr))
> >> +        return PTR_ERR(mgr);
> >>         mgr->compat_id = compat_id;
> >>   -    return devm_fpga_mgr_register(dev, mgr);
> >> +    return 0;
> >>   }
> >>     static struct platform_driver fme_mgr_driver = {
> >> diff --git a/drivers/fpga/fpga-mgr.c b/drivers/fpga/fpga-mgr.c
> >> index b85bc47c91a9..22f0dd5c2834 100644
> >> --- a/drivers/fpga/fpga-mgr.c
> >> +++ b/drivers/fpga/fpga-mgr.c
> >> @@ -550,20 +550,20 @@ void fpga_mgr_unlock(struct fpga_manager *mgr)
> >>   EXPORT_SYMBOL_GPL(fpga_mgr_unlock);
> >>     /**
> >> - * fpga_mgr_create - create and initialize a FPGA manager struct
> >> + * fpga_mgr_register - create and register a FPGA Manager device
> >>    * @dev:    fpga manager device from pdev
> >
> > nit, space or tab after ':' ? this is inconsistent everywhere.
> >
> >
> >>    * @name:    fpga manager name
> >>    * @mops:    pointer to structure of fpga manager ops
> >>    * @priv:    fpga manager private data
> >>    *
> >> - * The caller of this function is responsible for freeing the struct with
> >> - * fpga_mgr_free().  Using devm_fpga_mgr_create() instead is recommended.
> >> + * The caller of this function is responsible for calling fpga_mgr_unregister().
> >> + * Using devm_fpga_mgr_register instead is recommended.
> >>    *
> >> - * Return: pointer to struct fpga_manager or NULL
> >> + * Returns a struct fpga_manager pointer on success, or ERR_PTR() on error.
> >>    */
> >> -struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
> >> -                     const struct fpga_manager_ops *mops,
> >> -                     void *priv)
> >> +struct fpga_manager *
> >> +fpga_mgr_register(struct device *dev, const char *name,
> >> +          const struct fpga_manager_ops *mops, void *priv)
> >>   {
> >>       struct fpga_manager *mgr;
> >>       int id, ret;
> >> @@ -572,21 +572,23 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
> >>           !mops->write_init || (!mops->write && !mops->write_sg) ||
> >>           (mops->write && mops->write_sg)) {
> >>           dev_err(dev, "Attempt to register without fpga_manager_ops\n");
> >> -        return NULL;
> >> +        return ERR_PTR(-EINVAL);
> >
> > return -EINVAL;
> >
> > And similar
> >
> > The user's error checking is simplified to an int check.
> >
> > The clunky ERR_PTR()/PTR_ERR() can be removed
> >
> >>       }
> >>         if (!name || !strlen(name)) {
> >>           dev_err(dev, "Attempt to register with no name!\n");
> >> -        return NULL;
> >> +        return ERR_PTR(-EINVAL);
> >>       }
> >>         mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
> >>       if (!mgr)
> >> -        return NULL;
> >> +        return ERR_PTR(-ENOMEM);
> >>         id = ida_simple_get(&fpga_mgr_ida, 0, 0, GFP_KERNEL);
> >> -    if (id < 0)
> >> +    if (id < 0) {
> >> +        ret = id;
> >>           goto error_kfree;
> >> +    }
> >>         mutex_init(&mgr->ref_mutex);
> >>   @@ -594,7 +596,6 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
> >
> > Just use the device name, do not give the user an option and simplify the calling parameters.
> >
> > mgr->name = dev_name(dev)
> >
> >>       mgr->mops = mops;
> >>       mgr->priv = priv;
> >>   -    device_initialize(&mgr->dev);
> >>       mgr->dev.class = fpga_mgr_class;
> >>       mgr->dev.groups = mops->groups;
> >>       mgr->dev.parent = dev;
> >> @@ -605,84 +606,6 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
> >>       if (ret)
> >>           goto error_device;
> >>   
> >
> > The only use of returned mgr seems to be a variant of setting the dev->driver_data = mgr
> >
> > so just make it the default and don't pass back the ptr.
> >
> > dev_set_drvdata(dev, mgr);
> 
> I also noticed that in most cases the return pointer is not used. I get what
> you are saying - it would be a nice simplification if the pointer did not need
> to be returned.
> 
> However, I believe we are talking about modifying the driver_data for the
> parent of the class driver, right? And the parent driver could spawn
> multiple class drivers, or sub drivers. Having one of those sub-drivers control
> the contents of it's driver-data doesn't feel right. It seems like the parent
> driver should have exclusive control over this pointer.

I also agree.

Thanks,
Yilun

> 
> I'm curious what others think...
> 
> - Russ
> 
> >
> > return 0;
> >
> >
> > Tom
> >
> >

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v3 1/3] fpga: mgr: Use standard dev_release for class driver
  2021-05-24 18:50 ` [PATCH v3 1/3] fpga: mgr: Use standard dev_release for class driver Russ Weight
  2021-05-24 20:16   ` Tom Rix
  2021-05-24 22:39   ` Moritz Fischer
@ 2021-05-28 15:41   ` Xu Yilun
  2021-05-28 17:26     ` Russ Weight
  2 siblings, 1 reply; 17+ messages in thread
From: Xu Yilun @ 2021-05-28 15:41 UTC (permalink / raw)
  To: Russ Weight
  Cc: mdf, linux-fpga, trix, lgoncalv, hao.wu, matthew.gerlach, richard.gong

On Mon, May 24, 2021 at 11:50:47AM -0700, Russ Weight wrote:
> The FPGA manager class driver data structure is being treated as a
> managed resource instead of using standard dev_release call-back
> to release the class data structure. This change removes the
> managed resource code for the freeing of the class data structure
> and combines the create() and register() functions into a single
> register() function.
> 
> The devm_fpga_mgr_register() function is retained.
> 
> Signed-off-by: Russ Weight <russell.h.weight@intel.com>
> ---
> v3:
>   - Cleaned up comment header for fpga_mgr_register()
>   - Fix error return on ida_simple_get() failure
> v2:
>   - Restored devm_fpga_mgr_register() functionality, adapted for the combined
>     create/register functionality.
>   - All previous callers of devm_fpga_mgr_register() will continue to call
>     devm_fpga_mgr_register().
>   - replaced unnecessary ternary operators in return statements with standard
>     if conditions.
> ---
>  drivers/fpga/altera-cvp.c        |  12 +--
>  drivers/fpga/altera-pr-ip-core.c |   8 +-
>  drivers/fpga/altera-ps-spi.c     |  10 +-
>  drivers/fpga/dfl-fme-mgr.c       |  10 +-
>  drivers/fpga/fpga-mgr.c          | 163 ++++++++-----------------------
>  drivers/fpga/ice40-spi.c         |  10 +-
>  drivers/fpga/machxo2-spi.c       |  10 +-
>  drivers/fpga/socfpga-a10.c       |  16 ++-
>  drivers/fpga/socfpga.c           |  10 +-
>  drivers/fpga/stratix10-soc.c     |  15 +--
>  drivers/fpga/ts73xx-fpga.c       |  10 +-
>  drivers/fpga/xilinx-spi.c        |  12 +--
>  drivers/fpga/zynq-fpga.c         |  16 ++-
>  drivers/fpga/zynqmp-fpga.c       |  10 +-
>  include/linux/fpga/fpga-mgr.h    |  17 ++--
>  15 files changed, 112 insertions(+), 217 deletions(-)
> 
> diff --git a/drivers/fpga/altera-cvp.c b/drivers/fpga/altera-cvp.c
> index 4e0edb60bfba..b61020ceb0a1 100644
> --- a/drivers/fpga/altera-cvp.c
> +++ b/drivers/fpga/altera-cvp.c
> @@ -652,19 +652,15 @@ static int altera_cvp_probe(struct pci_dev *pdev,
>  	snprintf(conf->mgr_name, sizeof(conf->mgr_name), "%s @%s",
>  		 ALTERA_CVP_MGR_NAME, pci_name(pdev));
>  
> -	mgr = devm_fpga_mgr_create(&pdev->dev, conf->mgr_name,
> -				   &altera_cvp_ops, conf);
> -	if (!mgr) {
> -		ret = -ENOMEM;
> +	mgr = fpga_mgr_register(&pdev->dev, conf->mgr_name,
> +				&altera_cvp_ops, conf);
> +	if (IS_ERR(mgr)) {
> +		ret = PTR_ERR(mgr);
>  		goto err_unmap;
>  	}
>  
>  	pci_set_drvdata(pdev, mgr);
>  
> -	ret = fpga_mgr_register(mgr);
> -	if (ret)
> -		goto err_unmap;
> -
>  	return 0;
>  
>  err_unmap:
> diff --git a/drivers/fpga/altera-pr-ip-core.c b/drivers/fpga/altera-pr-ip-core.c
> index 5b130c4d9882..0980e134cc43 100644
> --- a/drivers/fpga/altera-pr-ip-core.c
> +++ b/drivers/fpga/altera-pr-ip-core.c
> @@ -191,11 +191,11 @@ int alt_pr_register(struct device *dev, void __iomem *reg_base)
>  		(val & ALT_PR_CSR_STATUS_MSK) >> ALT_PR_CSR_STATUS_SFT,
>  		(int)(val & ALT_PR_CSR_PR_START));
>  
> -	mgr = devm_fpga_mgr_create(dev, dev_name(dev), &alt_pr_ops, priv);
> -	if (!mgr)
> -		return -ENOMEM;
> +	mgr = devm_fpga_mgr_register(dev, dev_name(dev), &alt_pr_ops, priv);
> +	if (IS_ERR(mgr))
> +		return PTR_ERR(mgr);
>  
> -	return devm_fpga_mgr_register(dev, mgr);
> +	return 0;
>  }
>  EXPORT_SYMBOL_GPL(alt_pr_register);
>  
> diff --git a/drivers/fpga/altera-ps-spi.c b/drivers/fpga/altera-ps-spi.c
> index 23bfd4d1ad0f..d3f77b0312b2 100644
> --- a/drivers/fpga/altera-ps-spi.c
> +++ b/drivers/fpga/altera-ps-spi.c
> @@ -302,12 +302,12 @@ static int altera_ps_probe(struct spi_device *spi)
>  	snprintf(conf->mgr_name, sizeof(conf->mgr_name), "%s %s",
>  		 dev_driver_string(&spi->dev), dev_name(&spi->dev));
>  
> -	mgr = devm_fpga_mgr_create(&spi->dev, conf->mgr_name,
> -				   &altera_ps_ops, conf);
> -	if (!mgr)
> -		return -ENOMEM;
> +	mgr = devm_fpga_mgr_register(&spi->dev, conf->mgr_name,
> +				     &altera_ps_ops, conf);
> +	if (IS_ERR(mgr))
> +		return PTR_ERR(mgr);
>  
> -	return devm_fpga_mgr_register(&spi->dev, mgr);
> +	return 0;
>  }
>  
>  static const struct spi_device_id altera_ps_spi_ids[] = {
> diff --git a/drivers/fpga/dfl-fme-mgr.c b/drivers/fpga/dfl-fme-mgr.c
> index d5861d13b306..d71eba817b3b 100644
> --- a/drivers/fpga/dfl-fme-mgr.c
> +++ b/drivers/fpga/dfl-fme-mgr.c
> @@ -308,14 +308,14 @@ static int fme_mgr_probe(struct platform_device *pdev)
>  
>  	fme_mgr_get_compat_id(priv->ioaddr, compat_id);
>  
> -	mgr = devm_fpga_mgr_create(dev, "DFL FME FPGA Manager",
> -				   &fme_mgr_ops, priv);
> -	if (!mgr)
> -		return -ENOMEM;
> +	mgr = devm_fpga_mgr_register(dev, "DFL FME FPGA Manager",
> +				     &fme_mgr_ops, priv);
> +	if (IS_ERR(mgr))
> +		return PTR_ERR(mgr);
>  
>  	mgr->compat_id = compat_id;

It seems problematic the compat_id is set after the registration of fpga_mgr,
although I didn't found its usage in fpga-mgr.c.

But there is also a compat_id for fpga-region class, which will be shown
to user via sysfs. In that case, it may be a real problem.

How about we add the compat_id param for fpga_mgr/region_register()?

>  
> -	return devm_fpga_mgr_register(dev, mgr);
> +	return 0;
>  }
>  
>  static struct platform_driver fme_mgr_driver = {
> diff --git a/drivers/fpga/fpga-mgr.c b/drivers/fpga/fpga-mgr.c
> index b85bc47c91a9..22f0dd5c2834 100644
> --- a/drivers/fpga/fpga-mgr.c
> +++ b/drivers/fpga/fpga-mgr.c
> @@ -550,20 +550,20 @@ void fpga_mgr_unlock(struct fpga_manager *mgr)
>  EXPORT_SYMBOL_GPL(fpga_mgr_unlock);
>  
>  /**
> - * fpga_mgr_create - create and initialize a FPGA manager struct
> + * fpga_mgr_register - create and register a FPGA Manager device
>   * @dev:	fpga manager device from pdev
>   * @name:	fpga manager name
>   * @mops:	pointer to structure of fpga manager ops
>   * @priv:	fpga manager private data
>   *
> - * The caller of this function is responsible for freeing the struct with
> - * fpga_mgr_free().  Using devm_fpga_mgr_create() instead is recommended.
> + * The caller of this function is responsible for calling fpga_mgr_unregister().
> + * Using devm_fpga_mgr_register instead is recommended.
>   *
> - * Return: pointer to struct fpga_manager or NULL
> + * Returns a struct fpga_manager pointer on success, or ERR_PTR() on error.

Any reason we change the format of the Return description?

Thanks,
Yilun

>   */
> -struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
> -				     const struct fpga_manager_ops *mops,
> -				     void *priv)
> +struct fpga_manager *
> +fpga_mgr_register(struct device *dev, const char *name,
> +		  const struct fpga_manager_ops *mops, void *priv)
>  {
>  	struct fpga_manager *mgr;
>  	int id, ret;
> @@ -572,21 +572,23 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
>  	    !mops->write_init || (!mops->write && !mops->write_sg) ||
>  	    (mops->write && mops->write_sg)) {
>  		dev_err(dev, "Attempt to register without fpga_manager_ops\n");
> -		return NULL;
> +		return ERR_PTR(-EINVAL);
>  	}
>  
>  	if (!name || !strlen(name)) {
>  		dev_err(dev, "Attempt to register with no name!\n");
> -		return NULL;
> +		return ERR_PTR(-EINVAL);
>  	}
>  
>  	mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
>  	if (!mgr)
> -		return NULL;
> +		return ERR_PTR(-ENOMEM);
>  
>  	id = ida_simple_get(&fpga_mgr_ida, 0, 0, GFP_KERNEL);
> -	if (id < 0)
> +	if (id < 0) {
> +		ret = id;
>  		goto error_kfree;
> +	}
>  
>  	mutex_init(&mgr->ref_mutex);
>  
> @@ -594,7 +596,6 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
>  	mgr->mops = mops;
>  	mgr->priv = priv;
>  
> -	device_initialize(&mgr->dev);
>  	mgr->dev.class = fpga_mgr_class;
>  	mgr->dev.groups = mops->groups;
>  	mgr->dev.parent = dev;
> @@ -605,84 +606,6 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
>  	if (ret)
>  		goto error_device;
>  
> -	return mgr;
> -
> -error_device:
> -	ida_simple_remove(&fpga_mgr_ida, id);
> -error_kfree:
> -	kfree(mgr);
> -
> -	return NULL;
> -}
> -EXPORT_SYMBOL_GPL(fpga_mgr_create);
> -
> -/**
> - * fpga_mgr_free - free a FPGA manager created with fpga_mgr_create()
> - * @mgr:	fpga manager struct
> - */
> -void fpga_mgr_free(struct fpga_manager *mgr)
> -{
> -	ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
> -	kfree(mgr);
> -}
> -EXPORT_SYMBOL_GPL(fpga_mgr_free);
> -
> -static void devm_fpga_mgr_release(struct device *dev, void *res)
> -{
> -	struct fpga_mgr_devres *dr = res;
> -
> -	fpga_mgr_free(dr->mgr);
> -}
> -
> -/**
> - * devm_fpga_mgr_create - create and initialize a managed FPGA manager struct
> - * @dev:	fpga manager device from pdev
> - * @name:	fpga manager name
> - * @mops:	pointer to structure of fpga manager ops
> - * @priv:	fpga manager private data
> - *
> - * This function is intended for use in a FPGA manager driver's probe function.
> - * After the manager driver creates the manager struct with
> - * devm_fpga_mgr_create(), it should register it with fpga_mgr_register().  The
> - * manager driver's remove function should call fpga_mgr_unregister().  The
> - * manager struct allocated with this function will be freed automatically on
> - * driver detach.  This includes the case of a probe function returning error
> - * before calling fpga_mgr_register(), the struct will still get cleaned up.
> - *
> - * Return: pointer to struct fpga_manager or NULL
> - */
> -struct fpga_manager *devm_fpga_mgr_create(struct device *dev, const char *name,
> -					  const struct fpga_manager_ops *mops,
> -					  void *priv)
> -{
> -	struct fpga_mgr_devres *dr;
> -
> -	dr = devres_alloc(devm_fpga_mgr_release, sizeof(*dr), GFP_KERNEL);
> -	if (!dr)
> -		return NULL;
> -
> -	dr->mgr = fpga_mgr_create(dev, name, mops, priv);
> -	if (!dr->mgr) {
> -		devres_free(dr);
> -		return NULL;
> -	}
> -
> -	devres_add(dev, dr);
> -
> -	return dr->mgr;
> -}
> -EXPORT_SYMBOL_GPL(devm_fpga_mgr_create);
> -
> -/**
> - * fpga_mgr_register - register a FPGA manager
> - * @mgr: fpga manager struct
> - *
> - * Return: 0 on success, negative error code otherwise.
> - */
> -int fpga_mgr_register(struct fpga_manager *mgr)
> -{
> -	int ret;
> -
>  	/*
>  	 * Initialize framework state by requesting low level driver read state
>  	 * from device.  FPGA may be in reset mode or may have been programmed
> @@ -690,18 +613,20 @@ int fpga_mgr_register(struct fpga_manager *mgr)
>  	 */
>  	mgr->state = mgr->mops->state(mgr);
>  
> -	ret = device_add(&mgr->dev);
> -	if (ret)
> -		goto error_device;
> -
> -	dev_info(&mgr->dev, "%s registered\n", mgr->name);
> +	ret = device_register(&mgr->dev);
> +	if (ret) {
> +		put_device(&mgr->dev);
> +		return ERR_PTR(ret);
> +	}
>  
> -	return 0;
> +	return mgr;
>  
>  error_device:
> -	ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
> +	ida_simple_remove(&fpga_mgr_ida, id);
> +error_kfree:
> +	kfree(mgr);
>  
> -	return ret;
> +	return ERR_PTR(ret);
>  }
>  EXPORT_SYMBOL_GPL(fpga_mgr_register);
>  
> @@ -726,14 +651,6 @@ void fpga_mgr_unregister(struct fpga_manager *mgr)
>  }
>  EXPORT_SYMBOL_GPL(fpga_mgr_unregister);
>  
> -static int fpga_mgr_devres_match(struct device *dev, void *res,
> -				 void *match_data)
> -{
> -	struct fpga_mgr_devres *dr = res;
> -
> -	return match_data == dr->mgr;
> -}
> -
>  static void devm_fpga_mgr_unregister(struct device *dev, void *res)
>  {
>  	struct fpga_mgr_devres *dr = res;
> @@ -743,44 +660,44 @@ static void devm_fpga_mgr_unregister(struct device *dev, void *res)
>  
>  /**
>   * devm_fpga_mgr_register - resource managed variant of fpga_mgr_register()
> - * @dev: managing device for this FPGA manager
> - * @mgr: fpga manager struct
> + * @dev:	fpga manager device from pdev
> + * @name:	fpga manager name
> + * @mops:	pointer to structure of fpga manager ops
> + * @priv:	fpga manager private data
>   *
>   * This is the devres variant of fpga_mgr_register() for which the unregister
>   * function will be called automatically when the managing device is detached.
>   */
> -int devm_fpga_mgr_register(struct device *dev, struct fpga_manager *mgr)
> +struct fpga_manager *
> +devm_fpga_mgr_register(struct device *dev, const char *name,
> +		       const struct fpga_manager_ops *mops, void *priv)
>  {
>  	struct fpga_mgr_devres *dr;
> -	int ret;
> -
> -	/*
> -	 * Make sure that the struct fpga_manager * that is passed in is
> -	 * managed itself.
> -	 */
> -	if (WARN_ON(!devres_find(dev, devm_fpga_mgr_release,
> -				 fpga_mgr_devres_match, mgr)))
> -		return -EINVAL;
> +	struct fpga_manager *mgr;
>  
>  	dr = devres_alloc(devm_fpga_mgr_unregister, sizeof(*dr), GFP_KERNEL);
>  	if (!dr)
> -		return -ENOMEM;
> +		return ERR_PTR(-ENOMEM);
>  
> -	ret = fpga_mgr_register(mgr);
> -	if (ret) {
> +	mgr = fpga_mgr_register(dev, name, mops, priv);
> +	if (IS_ERR(mgr)) {
>  		devres_free(dr);
> -		return ret;
> +		return mgr;
>  	}
>  
>  	dr->mgr = mgr;
>  	devres_add(dev, dr);
>  
> -	return 0;
> +	return mgr;
>  }
>  EXPORT_SYMBOL_GPL(devm_fpga_mgr_register);
>  
>  static void fpga_mgr_dev_release(struct device *dev)
>  {
> +	struct fpga_manager *mgr = to_fpga_manager(dev);
> +
> +	ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
> +	kfree(mgr);
>  }
>  
>  static int __init fpga_mgr_class_init(void)
> diff --git a/drivers/fpga/ice40-spi.c b/drivers/fpga/ice40-spi.c
> index 69dec5af23c3..91ac25f339f3 100644
> --- a/drivers/fpga/ice40-spi.c
> +++ b/drivers/fpga/ice40-spi.c
> @@ -178,12 +178,12 @@ static int ice40_fpga_probe(struct spi_device *spi)
>  		return ret;
>  	}
>  
> -	mgr = devm_fpga_mgr_create(dev, "Lattice iCE40 FPGA Manager",
> -				   &ice40_fpga_ops, priv);
> -	if (!mgr)
> -		return -ENOMEM;
> +	mgr = devm_fpga_mgr_register(dev, "Lattice iCE40 FPGA Manager",
> +				     &ice40_fpga_ops, priv);
> +	if (IS_ERR(mgr))
> +		return PTR_ERR(mgr);
>  
> -	return devm_fpga_mgr_register(dev, mgr);
> +	return 0;
>  }
>  
>  static const struct of_device_id ice40_fpga_of_match[] = {
> diff --git a/drivers/fpga/machxo2-spi.c b/drivers/fpga/machxo2-spi.c
> index 114a64d2b7a4..0137c1e2e706 100644
> --- a/drivers/fpga/machxo2-spi.c
> +++ b/drivers/fpga/machxo2-spi.c
> @@ -366,12 +366,12 @@ static int machxo2_spi_probe(struct spi_device *spi)
>  		return -EINVAL;
>  	}
>  
> -	mgr = devm_fpga_mgr_create(dev, "Lattice MachXO2 SPI FPGA Manager",
> -				   &machxo2_ops, spi);
> -	if (!mgr)
> -		return -ENOMEM;
> +	mgr = devm_fpga_mgr_register(dev, "Lattice MachXO2 SPI FPGA Manager",
> +				     &machxo2_ops, spi);
> +	if (IS_ERR(mgr))
> +		return PTR_ERR(mgr);
>  
> -	return devm_fpga_mgr_register(dev, mgr);
> +	return 0;
>  }
>  
>  static const struct of_device_id of_match[] = {
> diff --git a/drivers/fpga/socfpga-a10.c b/drivers/fpga/socfpga-a10.c
> index 573d88bdf730..ac8e89b8a5cc 100644
> --- a/drivers/fpga/socfpga-a10.c
> +++ b/drivers/fpga/socfpga-a10.c
> @@ -508,19 +508,15 @@ static int socfpga_a10_fpga_probe(struct platform_device *pdev)
>  		return -EBUSY;
>  	}
>  
> -	mgr = devm_fpga_mgr_create(dev, "SoCFPGA Arria10 FPGA Manager",
> -				   &socfpga_a10_fpga_mgr_ops, priv);
> -	if (!mgr)
> -		return -ENOMEM;
> -
> -	platform_set_drvdata(pdev, mgr);
> -
> -	ret = fpga_mgr_register(mgr);
> -	if (ret) {
> +	mgr = fpga_mgr_register(dev, "SoCFPGA Arria10 FPGA Manager",
> +				&socfpga_a10_fpga_mgr_ops, priv);
> +	if (IS_ERR(mgr)) {
>  		clk_disable_unprepare(priv->clk);
> -		return ret;
> +		return PTR_ERR(mgr);
>  	}
>  
> +	platform_set_drvdata(pdev, mgr);
> +
>  	return 0;
>  }
>  
> diff --git a/drivers/fpga/socfpga.c b/drivers/fpga/socfpga.c
> index 1f467173fc1f..ebfa9f4d24ef 100644
> --- a/drivers/fpga/socfpga.c
> +++ b/drivers/fpga/socfpga.c
> @@ -571,12 +571,12 @@ static int socfpga_fpga_probe(struct platform_device *pdev)
>  	if (ret)
>  		return ret;
>  
> -	mgr = devm_fpga_mgr_create(dev, "Altera SOCFPGA FPGA Manager",
> -				   &socfpga_fpga_ops, priv);
> -	if (!mgr)
> -		return -ENOMEM;
> +	mgr = devm_fpga_mgr_register(dev, "Altera SOCFPGA FPGA Manager",
> +				     &socfpga_fpga_ops, priv);
> +	if (IS_ERR(mgr))
> +		return PTR_ERR(mgr);
>  
> -	return devm_fpga_mgr_register(dev, mgr);
> +	return 0;
>  }
>  
>  #ifdef CONFIG_OF
> diff --git a/drivers/fpga/stratix10-soc.c b/drivers/fpga/stratix10-soc.c
> index 657a70c5fc99..e0bd91b3f7f8 100644
> --- a/drivers/fpga/stratix10-soc.c
> +++ b/drivers/fpga/stratix10-soc.c
> @@ -425,18 +425,11 @@ static int s10_probe(struct platform_device *pdev)
>  
>  	init_completion(&priv->status_return_completion);
>  
> -	mgr = fpga_mgr_create(dev, "Stratix10 SOC FPGA Manager",
> -			      &s10_ops, priv);
> -	if (!mgr) {
> -		dev_err(dev, "unable to create FPGA manager\n");
> -		ret = -ENOMEM;
> -		goto probe_err;
> -	}
> -
> -	ret = fpga_mgr_register(mgr);
> -	if (ret) {
> +	mgr = fpga_mgr_register(dev, "Stratix10 SOC FPGA Manager",
> +				&s10_ops, priv);
> +	if (IS_ERR(mgr)) {
>  		dev_err(dev, "unable to register FPGA manager\n");
> -		fpga_mgr_free(mgr);
> +		ret = PTR_ERR(mgr);
>  		goto probe_err;
>  	}
>  
> diff --git a/drivers/fpga/ts73xx-fpga.c b/drivers/fpga/ts73xx-fpga.c
> index 101f016c6ed8..5efbec2b9f9f 100644
> --- a/drivers/fpga/ts73xx-fpga.c
> +++ b/drivers/fpga/ts73xx-fpga.c
> @@ -122,12 +122,12 @@ static int ts73xx_fpga_probe(struct platform_device *pdev)
>  	if (IS_ERR(priv->io_base))
>  		return PTR_ERR(priv->io_base);
>  
> -	mgr = devm_fpga_mgr_create(kdev, "TS-73xx FPGA Manager",
> -				   &ts73xx_fpga_ops, priv);
> -	if (!mgr)
> -		return -ENOMEM;
> +	mgr = devm_fpga_mgr_register(kdev, "TS-73xx FPGA Manager",
> +				     &ts73xx_fpga_ops, priv);
> +	if (IS_ERR(mgr))
> +		return PTR_ERR(mgr);
>  
> -	return devm_fpga_mgr_register(kdev, mgr);
> +	return 0;
>  }
>  
>  static struct platform_driver ts73xx_fpga_driver = {
> diff --git a/drivers/fpga/xilinx-spi.c b/drivers/fpga/xilinx-spi.c
> index fee4d0abf6bf..efda475f7a98 100644
> --- a/drivers/fpga/xilinx-spi.c
> +++ b/drivers/fpga/xilinx-spi.c
> @@ -247,13 +247,13 @@ static int xilinx_spi_probe(struct spi_device *spi)
>  		return dev_err_probe(&spi->dev, PTR_ERR(conf->done),
>  				     "Failed to get DONE gpio\n");
>  
> -	mgr = devm_fpga_mgr_create(&spi->dev,
> -				   "Xilinx Slave Serial FPGA Manager",
> -				   &xilinx_spi_ops, conf);
> -	if (!mgr)
> -		return -ENOMEM;
> +	mgr = devm_fpga_mgr_register(&spi->dev,
> +				     "Xilinx Slave Serial FPGA Manager",
> +				     &xilinx_spi_ops, conf);
> +	if (IS_ERR(mgr))
> +		return PTR_ERR(mgr);
>  
> -	return devm_fpga_mgr_register(&spi->dev, mgr);
> +	return 0;
>  }
>  
>  static const struct of_device_id xlnx_spi_of_match[] = {
> diff --git a/drivers/fpga/zynq-fpga.c b/drivers/fpga/zynq-fpga.c
> index 07fa8d9ec675..74bbb9710f4e 100644
> --- a/drivers/fpga/zynq-fpga.c
> +++ b/drivers/fpga/zynq-fpga.c
> @@ -609,20 +609,16 @@ static int zynq_fpga_probe(struct platform_device *pdev)
>  
>  	clk_disable(priv->clk);
>  
> -	mgr = devm_fpga_mgr_create(dev, "Xilinx Zynq FPGA Manager",
> -				   &zynq_fpga_ops, priv);
> -	if (!mgr)
> -		return -ENOMEM;
> -
> -	platform_set_drvdata(pdev, mgr);
> -
> -	err = fpga_mgr_register(mgr);
> -	if (err) {
> +	mgr = fpga_mgr_register(dev, "Xilinx Zynq FPGA Manager",
> +				&zynq_fpga_ops, priv);
> +	if (IS_ERR(mgr)) {
>  		dev_err(dev, "unable to register FPGA manager\n");
>  		clk_unprepare(priv->clk);
> -		return err;
> +		return PTR_ERR(mgr);
>  	}
>  
> +	platform_set_drvdata(pdev, mgr);
> +
>  	return 0;
>  }
>  
> diff --git a/drivers/fpga/zynqmp-fpga.c b/drivers/fpga/zynqmp-fpga.c
> index 125743c9797f..1a6d520985be 100644
> --- a/drivers/fpga/zynqmp-fpga.c
> +++ b/drivers/fpga/zynqmp-fpga.c
> @@ -102,12 +102,12 @@ static int zynqmp_fpga_probe(struct platform_device *pdev)
>  
>  	priv->dev = dev;
>  
> -	mgr = devm_fpga_mgr_create(dev, "Xilinx ZynqMP FPGA Manager",
> -				   &zynqmp_fpga_ops, priv);
> -	if (!mgr)
> -		return -ENOMEM;
> +	mgr = devm_fpga_mgr_register(dev, "Xilinx ZynqMP FPGA Manager",
> +				     &zynqmp_fpga_ops, priv);
> +	if (IS_ERR(mgr))
> +		return PTR_ERR(mgr);
>  
> -	return devm_fpga_mgr_register(dev, mgr);
> +	return 0;
>  }
>  
>  static const struct of_device_id zynqmp_fpga_of_match[] = {
> diff --git a/include/linux/fpga/fpga-mgr.h b/include/linux/fpga/fpga-mgr.h
> index 2bc3030a69e5..d1ccf8f8c38a 100644
> --- a/include/linux/fpga/fpga-mgr.h
> +++ b/include/linux/fpga/fpga-mgr.h
> @@ -191,17 +191,14 @@ struct fpga_manager *fpga_mgr_get(struct device *dev);
>  
>  void fpga_mgr_put(struct fpga_manager *mgr);
>  
> -struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
> -				     const struct fpga_manager_ops *mops,
> -				     void *priv);
> -void fpga_mgr_free(struct fpga_manager *mgr);
> -int fpga_mgr_register(struct fpga_manager *mgr);
> +struct fpga_manager *fpga_mgr_register(struct device *dev, const char *name,
> +				       const struct fpga_manager_ops *mops,
> +				       void *priv);
>  void fpga_mgr_unregister(struct fpga_manager *mgr);
>  
> -int devm_fpga_mgr_register(struct device *dev, struct fpga_manager *mgr);
> -
> -struct fpga_manager *devm_fpga_mgr_create(struct device *dev, const char *name,
> -					  const struct fpga_manager_ops *mops,
> -					  void *priv);
> +struct fpga_manager *
> +devm_fpga_mgr_register(struct device *dev, const char *name,
> +		       const struct fpga_manager_ops *mops,
> +		       void *priv);
>  
>  #endif /*_LINUX_FPGA_MGR_H */
> -- 
> 2.25.1

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v3 2/3] fpga: bridge: Use standard dev_release for class driver
  2021-05-24 18:50 ` [PATCH v3 2/3] fpga: bridge: " Russ Weight
@ 2021-05-28 15:51   ` Xu Yilun
  0 siblings, 0 replies; 17+ messages in thread
From: Xu Yilun @ 2021-05-28 15:51 UTC (permalink / raw)
  To: Russ Weight
  Cc: mdf, linux-fpga, trix, lgoncalv, hao.wu, matthew.gerlach, richard.gong

On Mon, May 24, 2021 at 11:50:48AM -0700, Russ Weight wrote:
> The FPGA bridge class driver data structure is being treated as a
> managed resource instead of using standard dev_release call-back
> to release the class data structure. This change removes the
> managed resource code and combines the create() and register()
> functions into a single register() function.
> 
> Signed-off-by: Russ Weight <russell.h.weight@intel.com>
> ---
> v3:
>   - Cleaned up comment header for fpga_bridge_register()
>   - Fix error return values for fpga_bridge_register()
> v2:
>   - No changes
> ---
>  drivers/fpga/altera-fpga2sdram.c    |  12 +--
>  drivers/fpga/altera-freeze-bridge.c |  10 +--
>  drivers/fpga/altera-hps2fpga.c      |  12 +--
>  drivers/fpga/dfl-fme-br.c           |  10 +--
>  drivers/fpga/fpga-bridge.c          | 119 ++++++----------------------
>  drivers/fpga/xilinx-pr-decoupler.c  |  17 ++--
>  include/linux/fpga/fpga-bridge.h    |  12 +--
>  7 files changed, 52 insertions(+), 140 deletions(-)
> 
> diff --git a/drivers/fpga/altera-fpga2sdram.c b/drivers/fpga/altera-fpga2sdram.c
> index a78e49c63c64..66063507116b 100644
> --- a/drivers/fpga/altera-fpga2sdram.c
> +++ b/drivers/fpga/altera-fpga2sdram.c
> @@ -121,17 +121,13 @@ static int alt_fpga_bridge_probe(struct platform_device *pdev)
>  	/* Get f2s bridge configuration saved in handoff register */
>  	regmap_read(sysmgr, SYSMGR_ISWGRP_HANDOFF3, &priv->mask);
>  
> -	br = devm_fpga_bridge_create(dev, F2S_BRIDGE_NAME,
> -				     &altera_fpga2sdram_br_ops, priv);
> -	if (!br)
> -		return -ENOMEM;
> +	br = fpga_bridge_register(dev, F2S_BRIDGE_NAME,
> +				  &altera_fpga2sdram_br_ops, priv);
> +	if (IS_ERR(br))
> +		return PTR_ERR(mgr);
>  
>  	platform_set_drvdata(pdev, br);
>  
> -	ret = fpga_bridge_register(br);
> -	if (ret)
> -		return ret;
> -
>  	dev_info(dev, "driver initialized with handoff %08x\n", priv->mask);
>  
>  	if (!of_property_read_u32(dev->of_node, "bridge-enable", &enable)) {
> diff --git a/drivers/fpga/altera-freeze-bridge.c b/drivers/fpga/altera-freeze-bridge.c
> index dd58c4aea92e..bfbfa43cd05b 100644
> --- a/drivers/fpga/altera-freeze-bridge.c
> +++ b/drivers/fpga/altera-freeze-bridge.c
> @@ -244,14 +244,14 @@ static int altera_freeze_br_probe(struct platform_device *pdev)
>  
>  	priv->base_addr = base_addr;
>  
> -	br = devm_fpga_bridge_create(dev, FREEZE_BRIDGE_NAME,
> -				     &altera_freeze_br_br_ops, priv);
> -	if (!br)
> -		return -ENOMEM;
> +	br = fpga_bridge_register(dev, FREEZE_BRIDGE_NAME,
> +				  &altera_freeze_br_br_ops, priv);
> +	if (IS_ERR(br))
> +		return PTR_ERR(br);
>  
>  	platform_set_drvdata(pdev, br);
>  
> -	return fpga_bridge_register(br);
> +	return 0;
>  }
>  
>  static int altera_freeze_br_remove(struct platform_device *pdev)
> diff --git a/drivers/fpga/altera-hps2fpga.c b/drivers/fpga/altera-hps2fpga.c
> index 77b95f251821..aa758426c22b 100644
> --- a/drivers/fpga/altera-hps2fpga.c
> +++ b/drivers/fpga/altera-hps2fpga.c
> @@ -180,19 +180,15 @@ static int alt_fpga_bridge_probe(struct platform_device *pdev)
>  		}
>  	}
>  
> -	br = devm_fpga_bridge_create(dev, priv->name,
> -				     &altera_hps2fpga_br_ops, priv);
> -	if (!br) {
> -		ret = -ENOMEM;
> +	br = fpga_bridge_register(dev, priv->name,
> +				  &altera_hps2fpga_br_ops, priv);
> +	if (IS_ERR(br)) {
> +		ret = PTR_ERR(br);
>  		goto err;
>  	}
>  
>  	platform_set_drvdata(pdev, br);
>  
> -	ret = fpga_bridge_register(br);
> -	if (ret)
> -		goto err;
> -
>  	return 0;
>  
>  err:
> diff --git a/drivers/fpga/dfl-fme-br.c b/drivers/fpga/dfl-fme-br.c
> index 3ff9f3a687ce..808d1f4d76df 100644
> --- a/drivers/fpga/dfl-fme-br.c
> +++ b/drivers/fpga/dfl-fme-br.c
> @@ -68,14 +68,14 @@ static int fme_br_probe(struct platform_device *pdev)
>  
>  	priv->pdata = dev_get_platdata(dev);
>  
> -	br = devm_fpga_bridge_create(dev, "DFL FPGA FME Bridge",
> -				     &fme_bridge_ops, priv);
> -	if (!br)
> -		return -ENOMEM;
> +	br = fpga_bridge_register(dev, "DFL FPGA FME Bridge",
> +				  &fme_bridge_ops, priv);
> +	if (IS_ERR(br))
> +		return PTR_ERR(br);
>  
>  	platform_set_drvdata(pdev, br);
>  
> -	return fpga_bridge_register(br);
> +	return 0;
>  }
>  
>  static int fme_br_remove(struct platform_device *pdev)
> diff --git a/drivers/fpga/fpga-bridge.c b/drivers/fpga/fpga-bridge.c
> index e9266b2a357f..54a08a682e89 100644
> --- a/drivers/fpga/fpga-bridge.c
> +++ b/drivers/fpga/fpga-bridge.c
> @@ -312,36 +312,35 @@ static struct attribute *fpga_bridge_attrs[] = {
>  ATTRIBUTE_GROUPS(fpga_bridge);
>  
>  /**
> - * fpga_bridge_create - create and initialize a struct fpga_bridge
> + * fpga_bridge_register - create and register a FPGA Bridge device
>   * @dev:	FPGA bridge device from pdev
>   * @name:	FPGA bridge name
>   * @br_ops:	pointer to structure of fpga bridge ops
>   * @priv:	FPGA bridge private data
>   *
> - * The caller of this function is responsible for freeing the bridge with
> - * fpga_bridge_free().  Using devm_fpga_bridge_create() instead is recommended.
> - *
> - * Return: struct fpga_bridge or NULL
> + * Returns a struct fpga_bridge pointer on success, or ERR_PTR() on error.
>   */
> -struct fpga_bridge *fpga_bridge_create(struct device *dev, const char *name,
> -				       const struct fpga_bridge_ops *br_ops,
> -				       void *priv)
> +struct fpga_bridge *fpga_bridge_register(struct device *dev, const char *name,
> +					 const struct fpga_bridge_ops *br_ops,
> +					 void *priv)
>  {
>  	struct fpga_bridge *bridge;
>  	int id, ret;
>  
>  	if (!name || !strlen(name)) {
>  		dev_err(dev, "Attempt to register with no name!\n");
> -		return NULL;
> +		return ERR_PTR(-EINVAL);
>  	}
>  
>  	bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
>  	if (!bridge)
> -		return NULL;
> +		return ERR_PTR(-ENOMEM);
>  
>  	id = ida_simple_get(&fpga_bridge_ida, 0, 0, GFP_KERNEL);
> -	if (id < 0)
> +	if (id < 0) {
> +		ret = id;
>  		goto error_kfree;
> +	}
>  
>  	mutex_init(&bridge->mutex);
>  	INIT_LIST_HEAD(&bridge->node);
> @@ -350,7 +349,6 @@ struct fpga_bridge *fpga_bridge_create(struct device *dev, const char *name,
>  	bridge->br_ops = br_ops;
>  	bridge->priv = priv;
>  
> -	device_initialize(&bridge->dev);
>  	bridge->dev.groups = br_ops->groups;
>  	bridge->dev.class = fpga_bridge_class;
>  	bridge->dev.parent = dev;
> @@ -361,6 +359,16 @@ struct fpga_bridge *fpga_bridge_create(struct device *dev, const char *name,
>  	if (ret)
>  		goto error_device;
>  
> +	ret = device_register(&bridge->dev);
> +	if (ret) {
> +		put_device(&bridge->dev);
> +		return ERR_PTR(ret);
> +	}
> +
> +	of_platform_populate(dev->of_node, NULL, NULL, dev);
> +
> +	dev_info(dev->parent, "fpga bridge [%s] registered\n", bridge->name);
> +
>  	return bridge;
>  
>  error_device:
> @@ -368,88 +376,7 @@ struct fpga_bridge *fpga_bridge_create(struct device *dev, const char *name,
>  error_kfree:
>  	kfree(bridge);
>  
> -	return NULL;
> -}
> -EXPORT_SYMBOL_GPL(fpga_bridge_create);
> -
> -/**
> - * fpga_bridge_free - free a fpga bridge created by fpga_bridge_create()
> - * @bridge:	FPGA bridge struct
> - */
> -void fpga_bridge_free(struct fpga_bridge *bridge)
> -{
> -	ida_simple_remove(&fpga_bridge_ida, bridge->dev.id);
> -	kfree(bridge);
> -}
> -EXPORT_SYMBOL_GPL(fpga_bridge_free);
> -
> -static void devm_fpga_bridge_release(struct device *dev, void *res)
> -{
> -	struct fpga_bridge *bridge = *(struct fpga_bridge **)res;
> -
> -	fpga_bridge_free(bridge);
> -}
> -
> -/**
> - * devm_fpga_bridge_create - create and init a managed struct fpga_bridge
> - * @dev:	FPGA bridge device from pdev
> - * @name:	FPGA bridge name
> - * @br_ops:	pointer to structure of fpga bridge ops
> - * @priv:	FPGA bridge private data
> - *
> - * This function is intended for use in a FPGA bridge driver's probe function.
> - * After the bridge driver creates the struct with devm_fpga_bridge_create(), it
> - * should register the bridge with fpga_bridge_register().  The bridge driver's
> - * remove function should call fpga_bridge_unregister().  The bridge struct
> - * allocated with this function will be freed automatically on driver detach.
> - * This includes the case of a probe function returning error before calling
> - * fpga_bridge_register(), the struct will still get cleaned up.
> - *
> - *  Return: struct fpga_bridge or NULL
> - */
> -struct fpga_bridge
> -*devm_fpga_bridge_create(struct device *dev, const char *name,
> -			 const struct fpga_bridge_ops *br_ops, void *priv)
> -{
> -	struct fpga_bridge **ptr, *bridge;
> -
> -	ptr = devres_alloc(devm_fpga_bridge_release, sizeof(*ptr), GFP_KERNEL);
> -	if (!ptr)
> -		return NULL;
> -
> -	bridge = fpga_bridge_create(dev, name, br_ops, priv);
> -	if (!bridge) {
> -		devres_free(ptr);
> -	} else {
> -		*ptr = bridge;
> -		devres_add(dev, ptr);
> -	}
> -
> -	return bridge;
> -}
> -EXPORT_SYMBOL_GPL(devm_fpga_bridge_create);
> -
> -/**
> - * fpga_bridge_register - register a FPGA bridge
> - *
> - * @bridge: FPGA bridge struct
> - *
> - * Return: 0 for success, error code otherwise.
> - */
> -int fpga_bridge_register(struct fpga_bridge *bridge)
> -{
> -	struct device *dev = &bridge->dev;
> -	int ret;
> -
> -	ret = device_add(dev);
> -	if (ret)
> -		return ret;
> -
> -	of_platform_populate(dev->of_node, NULL, NULL, dev);
> -
> -	dev_info(dev->parent, "fpga bridge [%s] registered\n", bridge->name);
> -
> -	return 0;
> +	return ERR_PTR(ret);
>  }
>  EXPORT_SYMBOL_GPL(fpga_bridge_register);
>  
> @@ -475,6 +402,10 @@ EXPORT_SYMBOL_GPL(fpga_bridge_unregister);
>  
>  static void fpga_bridge_dev_release(struct device *dev)
>  {
> +	struct fpga_bridge *bridge = to_fpga_bridge(dev);
> +
> +	ida_simple_remove(&fpga_bridge_ida, bridge->dev.id);
> +	kfree(bridge);
>  }
>  
>  static int __init fpga_bridge_dev_init(void)
> diff --git a/drivers/fpga/xilinx-pr-decoupler.c b/drivers/fpga/xilinx-pr-decoupler.c
> index ea2bde6e5bc4..c004e52b9464 100644
> --- a/drivers/fpga/xilinx-pr-decoupler.c
> +++ b/drivers/fpga/xilinx-pr-decoupler.c
> @@ -138,22 +138,17 @@ static int xlnx_pr_decoupler_probe(struct platform_device *pdev)
>  
>  	clk_disable(priv->clk);
>  
> -	br = devm_fpga_bridge_create(&pdev->dev, priv->ipconfig->name,
> -				     &xlnx_pr_decoupler_br_ops, priv);
> -	if (!br) {
> -		err = -ENOMEM;
> -		goto err_clk;
> -	}
> -
> -	platform_set_drvdata(pdev, br);
> -
> -	err = fpga_bridge_register(br);
> -	if (err) {
> +	br = fpga_bridge_register(&pdev->dev, priv->ipconfig->name,
> +				  &xlnx_pr_decoupler_br_ops, priv);
> +	if (IS_ERR(br)) {
> +		err = PTR_ERR(br);
>  		dev_err(&pdev->dev, "unable to register %s",
>  			priv->ipconfig->name);
>  		goto err_clk;
>  	}
>  
> +	platform_set_drvdata(pdev, br);
> +
>  	return 0;
>  
>  err_clk:
> diff --git a/include/linux/fpga/fpga-bridge.h b/include/linux/fpga/fpga-bridge.h
> index 817600a32c93..86f306c99fa5 100644
> --- a/include/linux/fpga/fpga-bridge.h
> +++ b/include/linux/fpga/fpga-bridge.h
> @@ -62,15 +62,9 @@ int of_fpga_bridge_get_to_list(struct device_node *np,
>  			       struct fpga_image_info *info,
>  			       struct list_head *bridge_list);
>  
> -struct fpga_bridge *fpga_bridge_create(struct device *dev, const char *name,
> -				       const struct fpga_bridge_ops *br_ops,
> -				       void *priv);
> -void fpga_bridge_free(struct fpga_bridge *br);
> -int fpga_bridge_register(struct fpga_bridge *br);
> +struct fpga_bridge *fpga_bridge_register(struct device *dev, const char *name,
> +					 const struct fpga_bridge_ops *br_ops,
> +					 void *priv);
>  void fpga_bridge_unregister(struct fpga_bridge *br);
>  
> -struct fpga_bridge
> -*devm_fpga_bridge_create(struct device *dev, const char *name,
> -			 const struct fpga_bridge_ops *br_ops, void *priv);
> -
>  #endif /* _LINUX_FPGA_BRIDGE_H */

Looks good to me.

Thanks,
Yilun

> -- 
> 2.25.1

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v3 3/3] fpga: region: Use standard dev_release for class driver
  2021-05-24 18:50 ` [PATCH v3 3/3] fpga: region: " Russ Weight
@ 2021-05-28 16:10   ` Xu Yilun
  2021-05-28 17:43     ` Russ Weight
  0 siblings, 1 reply; 17+ messages in thread
From: Xu Yilun @ 2021-05-28 16:10 UTC (permalink / raw)
  To: Russ Weight
  Cc: mdf, linux-fpga, trix, lgoncalv, hao.wu, matthew.gerlach, richard.gong

On Mon, May 24, 2021 at 11:50:49AM -0700, Russ Weight wrote:
> The FPGA region class driver data structure is being treated as a
> managed resource instead of using standard dev_release call-back
> to release the class data structure. This change removes the
> managed resource code and combines the create() and register()
> functions into a single register() function.
> 
> Signed-off-by: Russ Weight <russell.h.weight@intel.com>
> ---
> v3:
>   - Cleaned up comment header for fpga_region_register()
>   - Fix fpga_region_register() error return on ida_simple_get() failure
> v2:
>   - No changes
> ---
>  drivers/fpga/dfl-fme-region.c    |  10 +--
>  drivers/fpga/dfl.c               |  10 +--
>  drivers/fpga/fpga-region.c       | 103 ++++++-------------------------
>  drivers/fpga/of-fpga-region.c    |  10 +--
>  include/linux/fpga/fpga-region.h |  12 +---
>  5 files changed, 32 insertions(+), 113 deletions(-)
> 
> diff --git a/drivers/fpga/dfl-fme-region.c b/drivers/fpga/dfl-fme-region.c
> index 1eeb42af1012..631c8ce160e3 100644
> --- a/drivers/fpga/dfl-fme-region.c
> +++ b/drivers/fpga/dfl-fme-region.c
> @@ -39,9 +39,9 @@ static int fme_region_probe(struct platform_device *pdev)
>  	if (IS_ERR(mgr))
>  		return -EPROBE_DEFER;
>  
> -	region = devm_fpga_region_create(dev, mgr, fme_region_get_bridges);
> -	if (!region) {
> -		ret = -ENOMEM;
> +	region = fpga_region_register(dev, mgr, fme_region_get_bridges);
> +	if (IS_ERR(region)) {
> +		ret = PTR_ERR(region);
>  		goto eprobe_mgr_put;
>  	}
>  
> @@ -49,10 +49,6 @@ static int fme_region_probe(struct platform_device *pdev)
>  	region->compat_id = mgr->compat_id;

This is the issue I've mentioned in Patch #1. There is a chance user
get zero value via compat_id sysfs node.

>  	platform_set_drvdata(pdev, region);
>  
> -	ret = fpga_region_register(region);
> -	if (ret)
> -		goto eprobe_mgr_put;
> -
>  	dev_dbg(dev, "DFL FME FPGA Region probed\n");
>  
>  	return 0;
> diff --git a/drivers/fpga/dfl.c b/drivers/fpga/dfl.c
> index 511b20ff35a3..2fb3a5bfe4df 100644
> --- a/drivers/fpga/dfl.c
> +++ b/drivers/fpga/dfl.c
> @@ -1400,9 +1400,9 @@ dfl_fpga_feature_devs_enumerate(struct dfl_fpga_enum_info *info)
>  	if (!cdev)
>  		return ERR_PTR(-ENOMEM);
>  
> -	cdev->region = devm_fpga_region_create(info->dev, NULL, NULL);
> -	if (!cdev->region) {
> -		ret = -ENOMEM;
> +	cdev->region = fpga_region_register(info->dev, NULL, NULL);
> +	if (IS_ERR(cdev->region)) {
> +		ret = PTR_ERR(cdev->region);
>  		goto free_cdev_exit;
>  	}
>  
> @@ -1410,10 +1410,6 @@ dfl_fpga_feature_devs_enumerate(struct dfl_fpga_enum_info *info)
>  	mutex_init(&cdev->lock);
>  	INIT_LIST_HEAD(&cdev->port_dev_list);

How about we moves fpga_region_register() here? It was here before.

Thanks,
Yilun

>  
> -	ret = fpga_region_register(cdev->region);
> -	if (ret)
> -		goto free_cdev_exit;
> -
>  	/* create and init build info for enumeration */
>  	binfo = devm_kzalloc(info->dev, sizeof(*binfo), GFP_KERNEL);
>  	if (!binfo) {
> diff --git a/drivers/fpga/fpga-region.c b/drivers/fpga/fpga-region.c
> index c3134b89c3fe..f65f60367e2c 100644
> --- a/drivers/fpga/fpga-region.c
> +++ b/drivers/fpga/fpga-region.c
> @@ -180,39 +180,35 @@ static struct attribute *fpga_region_attrs[] = {
>  ATTRIBUTE_GROUPS(fpga_region);
>  
>  /**
> - * fpga_region_create - alloc and init a struct fpga_region
> + * fpga_region_register - create and register a FPGA Region device
>   * @dev: device parent
>   * @mgr: manager that programs this region
>   * @get_bridges: optional function to get bridges to a list
>   *
> - * The caller of this function is responsible for freeing the resulting region
> - * struct with fpga_region_free().  Using devm_fpga_region_create() instead is
> - * recommended.
> - *
> - * Return: struct fpga_region or NULL
> + * Returns a struct fpga_region pointer on success, or ERR_PTR() on error.
>   */
> -struct fpga_region
> -*fpga_region_create(struct device *dev,
> -		    struct fpga_manager *mgr,
> -		    int (*get_bridges)(struct fpga_region *))
> +struct fpga_region *
> +fpga_region_register(struct device *dev, struct fpga_manager *mgr,
> +		     int (*get_bridges)(struct fpga_region *))
>  {
>  	struct fpga_region *region;
>  	int id, ret = 0;
>  
>  	region = kzalloc(sizeof(*region), GFP_KERNEL);
>  	if (!region)
> -		return NULL;
> +		return ERR_PTR(-ENOMEM);
>  
>  	id = ida_simple_get(&fpga_region_ida, 0, 0, GFP_KERNEL);
> -	if (id < 0)
> +	if (id < 0) {
> +		ret = id;
>  		goto err_free;
> +	}
>  
>  	region->mgr = mgr;
>  	region->get_bridges = get_bridges;
>  	mutex_init(&region->mutex);
>  	INIT_LIST_HEAD(&region->bridge_list);
>  
> -	device_initialize(&region->dev);
>  	region->dev.class = fpga_region_class;
>  	region->dev.parent = dev;
>  	region->dev.of_node = dev->of_node;
> @@ -222,6 +218,12 @@ struct fpga_region
>  	if (ret)
>  		goto err_remove;
>  
> +	ret = device_register(&region->dev);
> +	if (ret) {
> +		put_device(&region->dev);
> +		return ERR_PTR(ret);
> +	}
> +
>  	return region;
>  
>  err_remove:
> @@ -229,76 +231,7 @@ struct fpga_region
>  err_free:
>  	kfree(region);
>  
> -	return NULL;
> -}
> -EXPORT_SYMBOL_GPL(fpga_region_create);
> -
> -/**
> - * fpga_region_free - free a FPGA region created by fpga_region_create()
> - * @region: FPGA region
> - */
> -void fpga_region_free(struct fpga_region *region)
> -{
> -	ida_simple_remove(&fpga_region_ida, region->dev.id);
> -	kfree(region);
> -}
> -EXPORT_SYMBOL_GPL(fpga_region_free);
> -
> -static void devm_fpga_region_release(struct device *dev, void *res)
> -{
> -	struct fpga_region *region = *(struct fpga_region **)res;
> -
> -	fpga_region_free(region);
> -}
> -
> -/**
> - * devm_fpga_region_create - create and initialize a managed FPGA region struct
> - * @dev: device parent
> - * @mgr: manager that programs this region
> - * @get_bridges: optional function to get bridges to a list
> - *
> - * This function is intended for use in a FPGA region driver's probe function.
> - * After the region driver creates the region struct with
> - * devm_fpga_region_create(), it should register it with fpga_region_register().
> - * The region driver's remove function should call fpga_region_unregister().
> - * The region struct allocated with this function will be freed automatically on
> - * driver detach.  This includes the case of a probe function returning error
> - * before calling fpga_region_register(), the struct will still get cleaned up.
> - *
> - * Return: struct fpga_region or NULL
> - */
> -struct fpga_region
> -*devm_fpga_region_create(struct device *dev,
> -			 struct fpga_manager *mgr,
> -			 int (*get_bridges)(struct fpga_region *))
> -{
> -	struct fpga_region **ptr, *region;
> -
> -	ptr = devres_alloc(devm_fpga_region_release, sizeof(*ptr), GFP_KERNEL);
> -	if (!ptr)
> -		return NULL;
> -
> -	region = fpga_region_create(dev, mgr, get_bridges);
> -	if (!region) {
> -		devres_free(ptr);
> -	} else {
> -		*ptr = region;
> -		devres_add(dev, ptr);
> -	}
> -
> -	return region;
> -}
> -EXPORT_SYMBOL_GPL(devm_fpga_region_create);
> -
> -/**
> - * fpga_region_register - register a FPGA region
> - * @region: FPGA region
> - *
> - * Return: 0 or -errno
> - */
> -int fpga_region_register(struct fpga_region *region)
> -{
> -	return device_add(&region->dev);
> +	return ERR_PTR(ret);
>  }
>  EXPORT_SYMBOL_GPL(fpga_region_register);
>  
> @@ -316,6 +249,10 @@ EXPORT_SYMBOL_GPL(fpga_region_unregister);
>  
>  static void fpga_region_dev_release(struct device *dev)
>  {
> +	struct fpga_region *region = to_fpga_region(dev);
> +
> +	ida_simple_remove(&fpga_region_ida, region->dev.id);
> +	kfree(region);
>  }
>  
>  /**
> diff --git a/drivers/fpga/of-fpga-region.c b/drivers/fpga/of-fpga-region.c
> index e405309baadc..466e083654ae 100644
> --- a/drivers/fpga/of-fpga-region.c
> +++ b/drivers/fpga/of-fpga-region.c
> @@ -405,16 +405,12 @@ static int of_fpga_region_probe(struct platform_device *pdev)
>  	if (IS_ERR(mgr))
>  		return -EPROBE_DEFER;
>  
> -	region = devm_fpga_region_create(dev, mgr, of_fpga_region_get_bridges);
> -	if (!region) {
> -		ret = -ENOMEM;
> +	region = fpga_region_register(dev, mgr, of_fpga_region_get_bridges);
> +	if (IS_ERR(region)) {
> +		ret = PTR_ERR(region);
>  		goto eprobe_mgr_put;
>  	}
>  
> -	ret = fpga_region_register(region);
> -	if (ret)
> -		goto eprobe_mgr_put;
> -
>  	of_platform_populate(np, fpga_region_of_match, NULL, &region->dev);
>  	platform_set_drvdata(pdev, region);
>  
> diff --git a/include/linux/fpga/fpga-region.h b/include/linux/fpga/fpga-region.h
> index 27cb706275db..ca793f43af14 100644
> --- a/include/linux/fpga/fpga-region.h
> +++ b/include/linux/fpga/fpga-region.h
> @@ -37,15 +37,9 @@ struct fpga_region *fpga_region_class_find(
>  
>  int fpga_region_program_fpga(struct fpga_region *region);
>  
> -struct fpga_region
> -*fpga_region_create(struct device *dev, struct fpga_manager *mgr,
> -		    int (*get_bridges)(struct fpga_region *));
> -void fpga_region_free(struct fpga_region *region);
> -int fpga_region_register(struct fpga_region *region);
> +struct fpga_region *
> +fpga_region_register(struct device *dev, struct fpga_manager *mgr,
> +		     int (*get_bridges)(struct fpga_region *));
>  void fpga_region_unregister(struct fpga_region *region);
>  
> -struct fpga_region
> -*devm_fpga_region_create(struct device *dev, struct fpga_manager *mgr,
> -			int (*get_bridges)(struct fpga_region *));
> -
>  #endif /* _FPGA_REGION_H */
> -- 
> 2.25.1

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v3 1/3] fpga: mgr: Use standard dev_release for class driver
  2021-05-28 15:41   ` Xu Yilun
@ 2021-05-28 17:26     ` Russ Weight
  0 siblings, 0 replies; 17+ messages in thread
From: Russ Weight @ 2021-05-28 17:26 UTC (permalink / raw)
  To: Xu Yilun
  Cc: mdf, linux-fpga, trix, lgoncalv, hao.wu, matthew.gerlach, richard.gong



On 5/28/21 8:41 AM, Xu Yilun wrote:
> On Mon, May 24, 2021 at 11:50:47AM -0700, Russ Weight wrote:
>> The FPGA manager class driver data structure is being treated as a
>> managed resource instead of using standard dev_release call-back
>> to release the class data structure. This change removes the
>> managed resource code for the freeing of the class data structure
>> and combines the create() and register() functions into a single
>> register() function.
>>
>> The devm_fpga_mgr_register() function is retained.
>>
>> Signed-off-by: Russ Weight <russell.h.weight@intel.com>
>> ---
>> v3:
>>   - Cleaned up comment header for fpga_mgr_register()
>>   - Fix error return on ida_simple_get() failure
>> v2:
>>   - Restored devm_fpga_mgr_register() functionality, adapted for the combined
>>     create/register functionality.
>>   - All previous callers of devm_fpga_mgr_register() will continue to call
>>     devm_fpga_mgr_register().
>>   - replaced unnecessary ternary operators in return statements with standard
>>     if conditions.
>> ---
>>  drivers/fpga/altera-cvp.c        |  12 +--
>>  drivers/fpga/altera-pr-ip-core.c |   8 +-
>>  drivers/fpga/altera-ps-spi.c     |  10 +-
>>  drivers/fpga/dfl-fme-mgr.c       |  10 +-
>>  drivers/fpga/fpga-mgr.c          | 163 ++++++++-----------------------
>>  drivers/fpga/ice40-spi.c         |  10 +-
>>  drivers/fpga/machxo2-spi.c       |  10 +-
>>  drivers/fpga/socfpga-a10.c       |  16 ++-
>>  drivers/fpga/socfpga.c           |  10 +-
>>  drivers/fpga/stratix10-soc.c     |  15 +--
>>  drivers/fpga/ts73xx-fpga.c       |  10 +-
>>  drivers/fpga/xilinx-spi.c        |  12 +--
>>  drivers/fpga/zynq-fpga.c         |  16 ++-
>>  drivers/fpga/zynqmp-fpga.c       |  10 +-
>>  include/linux/fpga/fpga-mgr.h    |  17 ++--
>>  15 files changed, 112 insertions(+), 217 deletions(-)
>>
>> diff --git a/drivers/fpga/altera-cvp.c b/drivers/fpga/altera-cvp.c
>> index 4e0edb60bfba..b61020ceb0a1 100644
>> --- a/drivers/fpga/altera-cvp.c
>> +++ b/drivers/fpga/altera-cvp.c
>> @@ -652,19 +652,15 @@ static int altera_cvp_probe(struct pci_dev *pdev,
>>  	snprintf(conf->mgr_name, sizeof(conf->mgr_name), "%s @%s",
>>  		 ALTERA_CVP_MGR_NAME, pci_name(pdev));
>>  
>> -	mgr = devm_fpga_mgr_create(&pdev->dev, conf->mgr_name,
>> -				   &altera_cvp_ops, conf);
>> -	if (!mgr) {
>> -		ret = -ENOMEM;
>> +	mgr = fpga_mgr_register(&pdev->dev, conf->mgr_name,
>> +				&altera_cvp_ops, conf);
>> +	if (IS_ERR(mgr)) {
>> +		ret = PTR_ERR(mgr);
>>  		goto err_unmap;
>>  	}
>>  
>>  	pci_set_drvdata(pdev, mgr);
>>  
>> -	ret = fpga_mgr_register(mgr);
>> -	if (ret)
>> -		goto err_unmap;
>> -
>>  	return 0;
>>  
>>  err_unmap:
>> diff --git a/drivers/fpga/altera-pr-ip-core.c b/drivers/fpga/altera-pr-ip-core.c
>> index 5b130c4d9882..0980e134cc43 100644
>> --- a/drivers/fpga/altera-pr-ip-core.c
>> +++ b/drivers/fpga/altera-pr-ip-core.c
>> @@ -191,11 +191,11 @@ int alt_pr_register(struct device *dev, void __iomem *reg_base)
>>  		(val & ALT_PR_CSR_STATUS_MSK) >> ALT_PR_CSR_STATUS_SFT,
>>  		(int)(val & ALT_PR_CSR_PR_START));
>>  
>> -	mgr = devm_fpga_mgr_create(dev, dev_name(dev), &alt_pr_ops, priv);
>> -	if (!mgr)
>> -		return -ENOMEM;
>> +	mgr = devm_fpga_mgr_register(dev, dev_name(dev), &alt_pr_ops, priv);
>> +	if (IS_ERR(mgr))
>> +		return PTR_ERR(mgr);
>>  
>> -	return devm_fpga_mgr_register(dev, mgr);
>> +	return 0;
>>  }
>>  EXPORT_SYMBOL_GPL(alt_pr_register);
>>  
>> diff --git a/drivers/fpga/altera-ps-spi.c b/drivers/fpga/altera-ps-spi.c
>> index 23bfd4d1ad0f..d3f77b0312b2 100644
>> --- a/drivers/fpga/altera-ps-spi.c
>> +++ b/drivers/fpga/altera-ps-spi.c
>> @@ -302,12 +302,12 @@ static int altera_ps_probe(struct spi_device *spi)
>>  	snprintf(conf->mgr_name, sizeof(conf->mgr_name), "%s %s",
>>  		 dev_driver_string(&spi->dev), dev_name(&spi->dev));
>>  
>> -	mgr = devm_fpga_mgr_create(&spi->dev, conf->mgr_name,
>> -				   &altera_ps_ops, conf);
>> -	if (!mgr)
>> -		return -ENOMEM;
>> +	mgr = devm_fpga_mgr_register(&spi->dev, conf->mgr_name,
>> +				     &altera_ps_ops, conf);
>> +	if (IS_ERR(mgr))
>> +		return PTR_ERR(mgr);
>>  
>> -	return devm_fpga_mgr_register(&spi->dev, mgr);
>> +	return 0;
>>  }
>>  
>>  static const struct spi_device_id altera_ps_spi_ids[] = {
>> diff --git a/drivers/fpga/dfl-fme-mgr.c b/drivers/fpga/dfl-fme-mgr.c
>> index d5861d13b306..d71eba817b3b 100644
>> --- a/drivers/fpga/dfl-fme-mgr.c
>> +++ b/drivers/fpga/dfl-fme-mgr.c
>> @@ -308,14 +308,14 @@ static int fme_mgr_probe(struct platform_device *pdev)
>>  
>>  	fme_mgr_get_compat_id(priv->ioaddr, compat_id);
>>  
>> -	mgr = devm_fpga_mgr_create(dev, "DFL FME FPGA Manager",
>> -				   &fme_mgr_ops, priv);
>> -	if (!mgr)
>> -		return -ENOMEM;
>> +	mgr = devm_fpga_mgr_register(dev, "DFL FME FPGA Manager",
>> +				     &fme_mgr_ops, priv);
>> +	if (IS_ERR(mgr))
>> +		return PTR_ERR(mgr);
>>  
>>  	mgr->compat_id = compat_id;
> It seems problematic the compat_id is set after the registration of fpga_mgr,
> although I didn't found its usage in fpga-mgr.c.
>
> But there is also a compat_id for fpga-region class, which will be shown
> to user via sysfs. In that case, it may be a real problem.
>
> How about we add the compat_id param for fpga_mgr/region_register()?

Yes, I think you are right. I'll make the suggested changes.

>
>>  
>> -	return devm_fpga_mgr_register(dev, mgr);
>> +	return 0;
>>  }
>>  
>>  static struct platform_driver fme_mgr_driver = {
>> diff --git a/drivers/fpga/fpga-mgr.c b/drivers/fpga/fpga-mgr.c
>> index b85bc47c91a9..22f0dd5c2834 100644
>> --- a/drivers/fpga/fpga-mgr.c
>> +++ b/drivers/fpga/fpga-mgr.c
>> @@ -550,20 +550,20 @@ void fpga_mgr_unlock(struct fpga_manager *mgr)
>>  EXPORT_SYMBOL_GPL(fpga_mgr_unlock);
>>  
>>  /**
>> - * fpga_mgr_create - create and initialize a FPGA manager struct
>> + * fpga_mgr_register - create and register a FPGA Manager device
>>   * @dev:	fpga manager device from pdev
>>   * @name:	fpga manager name
>>   * @mops:	pointer to structure of fpga manager ops
>>   * @priv:	fpga manager private data
>>   *
>> - * The caller of this function is responsible for freeing the struct with
>> - * fpga_mgr_free().  Using devm_fpga_mgr_create() instead is recommended.
>> + * The caller of this function is responsible for calling fpga_mgr_unregister().
>> + * Using devm_fpga_mgr_register instead is recommended.
>>   *
>> - * Return: pointer to struct fpga_manager or NULL
>> + * Returns a struct fpga_manager pointer on success, or ERR_PTR() on error.
> Any reason we change the format of the Return description?

I was focusing on the change to the return value and didn't pay attention to
the format. I'll change the format back.

Thanks,
- Russ
>
> Thanks,
> Yilun
>
>>   */
>> -struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
>> -				     const struct fpga_manager_ops *mops,
>> -				     void *priv)
>> +struct fpga_manager *
>> +fpga_mgr_register(struct device *dev, const char *name,
>> +		  const struct fpga_manager_ops *mops, void *priv)
>>  {
>>  	struct fpga_manager *mgr;
>>  	int id, ret;
>> @@ -572,21 +572,23 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
>>  	    !mops->write_init || (!mops->write && !mops->write_sg) ||
>>  	    (mops->write && mops->write_sg)) {
>>  		dev_err(dev, "Attempt to register without fpga_manager_ops\n");
>> -		return NULL;
>> +		return ERR_PTR(-EINVAL);
>>  	}
>>  
>>  	if (!name || !strlen(name)) {
>>  		dev_err(dev, "Attempt to register with no name!\n");
>> -		return NULL;
>> +		return ERR_PTR(-EINVAL);
>>  	}
>>  
>>  	mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
>>  	if (!mgr)
>> -		return NULL;
>> +		return ERR_PTR(-ENOMEM);
>>  
>>  	id = ida_simple_get(&fpga_mgr_ida, 0, 0, GFP_KERNEL);
>> -	if (id < 0)
>> +	if (id < 0) {
>> +		ret = id;
>>  		goto error_kfree;
>> +	}
>>  
>>  	mutex_init(&mgr->ref_mutex);
>>  
>> @@ -594,7 +596,6 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
>>  	mgr->mops = mops;
>>  	mgr->priv = priv;
>>  
>> -	device_initialize(&mgr->dev);
>>  	mgr->dev.class = fpga_mgr_class;
>>  	mgr->dev.groups = mops->groups;
>>  	mgr->dev.parent = dev;
>> @@ -605,84 +606,6 @@ struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
>>  	if (ret)
>>  		goto error_device;
>>  
>> -	return mgr;
>> -
>> -error_device:
>> -	ida_simple_remove(&fpga_mgr_ida, id);
>> -error_kfree:
>> -	kfree(mgr);
>> -
>> -	return NULL;
>> -}
>> -EXPORT_SYMBOL_GPL(fpga_mgr_create);
>> -
>> -/**
>> - * fpga_mgr_free - free a FPGA manager created with fpga_mgr_create()
>> - * @mgr:	fpga manager struct
>> - */
>> -void fpga_mgr_free(struct fpga_manager *mgr)
>> -{
>> -	ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
>> -	kfree(mgr);
>> -}
>> -EXPORT_SYMBOL_GPL(fpga_mgr_free);
>> -
>> -static void devm_fpga_mgr_release(struct device *dev, void *res)
>> -{
>> -	struct fpga_mgr_devres *dr = res;
>> -
>> -	fpga_mgr_free(dr->mgr);
>> -}
>> -
>> -/**
>> - * devm_fpga_mgr_create - create and initialize a managed FPGA manager struct
>> - * @dev:	fpga manager device from pdev
>> - * @name:	fpga manager name
>> - * @mops:	pointer to structure of fpga manager ops
>> - * @priv:	fpga manager private data
>> - *
>> - * This function is intended for use in a FPGA manager driver's probe function.
>> - * After the manager driver creates the manager struct with
>> - * devm_fpga_mgr_create(), it should register it with fpga_mgr_register().  The
>> - * manager driver's remove function should call fpga_mgr_unregister().  The
>> - * manager struct allocated with this function will be freed automatically on
>> - * driver detach.  This includes the case of a probe function returning error
>> - * before calling fpga_mgr_register(), the struct will still get cleaned up.
>> - *
>> - * Return: pointer to struct fpga_manager or NULL
>> - */
>> -struct fpga_manager *devm_fpga_mgr_create(struct device *dev, const char *name,
>> -					  const struct fpga_manager_ops *mops,
>> -					  void *priv)
>> -{
>> -	struct fpga_mgr_devres *dr;
>> -
>> -	dr = devres_alloc(devm_fpga_mgr_release, sizeof(*dr), GFP_KERNEL);
>> -	if (!dr)
>> -		return NULL;
>> -
>> -	dr->mgr = fpga_mgr_create(dev, name, mops, priv);
>> -	if (!dr->mgr) {
>> -		devres_free(dr);
>> -		return NULL;
>> -	}
>> -
>> -	devres_add(dev, dr);
>> -
>> -	return dr->mgr;
>> -}
>> -EXPORT_SYMBOL_GPL(devm_fpga_mgr_create);
>> -
>> -/**
>> - * fpga_mgr_register - register a FPGA manager
>> - * @mgr: fpga manager struct
>> - *
>> - * Return: 0 on success, negative error code otherwise.
>> - */
>> -int fpga_mgr_register(struct fpga_manager *mgr)
>> -{
>> -	int ret;
>> -
>>  	/*
>>  	 * Initialize framework state by requesting low level driver read state
>>  	 * from device.  FPGA may be in reset mode or may have been programmed
>> @@ -690,18 +613,20 @@ int fpga_mgr_register(struct fpga_manager *mgr)
>>  	 */
>>  	mgr->state = mgr->mops->state(mgr);
>>  
>> -	ret = device_add(&mgr->dev);
>> -	if (ret)
>> -		goto error_device;
>> -
>> -	dev_info(&mgr->dev, "%s registered\n", mgr->name);
>> +	ret = device_register(&mgr->dev);
>> +	if (ret) {
>> +		put_device(&mgr->dev);
>> +		return ERR_PTR(ret);
>> +	}
>>  
>> -	return 0;
>> +	return mgr;
>>  
>>  error_device:
>> -	ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
>> +	ida_simple_remove(&fpga_mgr_ida, id);
>> +error_kfree:
>> +	kfree(mgr);
>>  
>> -	return ret;
>> +	return ERR_PTR(ret);
>>  }
>>  EXPORT_SYMBOL_GPL(fpga_mgr_register);
>>  
>> @@ -726,14 +651,6 @@ void fpga_mgr_unregister(struct fpga_manager *mgr)
>>  }
>>  EXPORT_SYMBOL_GPL(fpga_mgr_unregister);
>>  
>> -static int fpga_mgr_devres_match(struct device *dev, void *res,
>> -				 void *match_data)
>> -{
>> -	struct fpga_mgr_devres *dr = res;
>> -
>> -	return match_data == dr->mgr;
>> -}
>> -
>>  static void devm_fpga_mgr_unregister(struct device *dev, void *res)
>>  {
>>  	struct fpga_mgr_devres *dr = res;
>> @@ -743,44 +660,44 @@ static void devm_fpga_mgr_unregister(struct device *dev, void *res)
>>  
>>  /**
>>   * devm_fpga_mgr_register - resource managed variant of fpga_mgr_register()
>> - * @dev: managing device for this FPGA manager
>> - * @mgr: fpga manager struct
>> + * @dev:	fpga manager device from pdev
>> + * @name:	fpga manager name
>> + * @mops:	pointer to structure of fpga manager ops
>> + * @priv:	fpga manager private data
>>   *
>>   * This is the devres variant of fpga_mgr_register() for which the unregister
>>   * function will be called automatically when the managing device is detached.
>>   */
>> -int devm_fpga_mgr_register(struct device *dev, struct fpga_manager *mgr)
>> +struct fpga_manager *
>> +devm_fpga_mgr_register(struct device *dev, const char *name,
>> +		       const struct fpga_manager_ops *mops, void *priv)
>>  {
>>  	struct fpga_mgr_devres *dr;
>> -	int ret;
>> -
>> -	/*
>> -	 * Make sure that the struct fpga_manager * that is passed in is
>> -	 * managed itself.
>> -	 */
>> -	if (WARN_ON(!devres_find(dev, devm_fpga_mgr_release,
>> -				 fpga_mgr_devres_match, mgr)))
>> -		return -EINVAL;
>> +	struct fpga_manager *mgr;
>>  
>>  	dr = devres_alloc(devm_fpga_mgr_unregister, sizeof(*dr), GFP_KERNEL);
>>  	if (!dr)
>> -		return -ENOMEM;
>> +		return ERR_PTR(-ENOMEM);
>>  
>> -	ret = fpga_mgr_register(mgr);
>> -	if (ret) {
>> +	mgr = fpga_mgr_register(dev, name, mops, priv);
>> +	if (IS_ERR(mgr)) {
>>  		devres_free(dr);
>> -		return ret;
>> +		return mgr;
>>  	}
>>  
>>  	dr->mgr = mgr;
>>  	devres_add(dev, dr);
>>  
>> -	return 0;
>> +	return mgr;
>>  }
>>  EXPORT_SYMBOL_GPL(devm_fpga_mgr_register);
>>  
>>  static void fpga_mgr_dev_release(struct device *dev)
>>  {
>> +	struct fpga_manager *mgr = to_fpga_manager(dev);
>> +
>> +	ida_simple_remove(&fpga_mgr_ida, mgr->dev.id);
>> +	kfree(mgr);
>>  }
>>  
>>  static int __init fpga_mgr_class_init(void)
>> diff --git a/drivers/fpga/ice40-spi.c b/drivers/fpga/ice40-spi.c
>> index 69dec5af23c3..91ac25f339f3 100644
>> --- a/drivers/fpga/ice40-spi.c
>> +++ b/drivers/fpga/ice40-spi.c
>> @@ -178,12 +178,12 @@ static int ice40_fpga_probe(struct spi_device *spi)
>>  		return ret;
>>  	}
>>  
>> -	mgr = devm_fpga_mgr_create(dev, "Lattice iCE40 FPGA Manager",
>> -				   &ice40_fpga_ops, priv);
>> -	if (!mgr)
>> -		return -ENOMEM;
>> +	mgr = devm_fpga_mgr_register(dev, "Lattice iCE40 FPGA Manager",
>> +				     &ice40_fpga_ops, priv);
>> +	if (IS_ERR(mgr))
>> +		return PTR_ERR(mgr);
>>  
>> -	return devm_fpga_mgr_register(dev, mgr);
>> +	return 0;
>>  }
>>  
>>  static const struct of_device_id ice40_fpga_of_match[] = {
>> diff --git a/drivers/fpga/machxo2-spi.c b/drivers/fpga/machxo2-spi.c
>> index 114a64d2b7a4..0137c1e2e706 100644
>> --- a/drivers/fpga/machxo2-spi.c
>> +++ b/drivers/fpga/machxo2-spi.c
>> @@ -366,12 +366,12 @@ static int machxo2_spi_probe(struct spi_device *spi)
>>  		return -EINVAL;
>>  	}
>>  
>> -	mgr = devm_fpga_mgr_create(dev, "Lattice MachXO2 SPI FPGA Manager",
>> -				   &machxo2_ops, spi);
>> -	if (!mgr)
>> -		return -ENOMEM;
>> +	mgr = devm_fpga_mgr_register(dev, "Lattice MachXO2 SPI FPGA Manager",
>> +				     &machxo2_ops, spi);
>> +	if (IS_ERR(mgr))
>> +		return PTR_ERR(mgr);
>>  
>> -	return devm_fpga_mgr_register(dev, mgr);
>> +	return 0;
>>  }
>>  
>>  static const struct of_device_id of_match[] = {
>> diff --git a/drivers/fpga/socfpga-a10.c b/drivers/fpga/socfpga-a10.c
>> index 573d88bdf730..ac8e89b8a5cc 100644
>> --- a/drivers/fpga/socfpga-a10.c
>> +++ b/drivers/fpga/socfpga-a10.c
>> @@ -508,19 +508,15 @@ static int socfpga_a10_fpga_probe(struct platform_device *pdev)
>>  		return -EBUSY;
>>  	}
>>  
>> -	mgr = devm_fpga_mgr_create(dev, "SoCFPGA Arria10 FPGA Manager",
>> -				   &socfpga_a10_fpga_mgr_ops, priv);
>> -	if (!mgr)
>> -		return -ENOMEM;
>> -
>> -	platform_set_drvdata(pdev, mgr);
>> -
>> -	ret = fpga_mgr_register(mgr);
>> -	if (ret) {
>> +	mgr = fpga_mgr_register(dev, "SoCFPGA Arria10 FPGA Manager",
>> +				&socfpga_a10_fpga_mgr_ops, priv);
>> +	if (IS_ERR(mgr)) {
>>  		clk_disable_unprepare(priv->clk);
>> -		return ret;
>> +		return PTR_ERR(mgr);
>>  	}
>>  
>> +	platform_set_drvdata(pdev, mgr);
>> +
>>  	return 0;
>>  }
>>  
>> diff --git a/drivers/fpga/socfpga.c b/drivers/fpga/socfpga.c
>> index 1f467173fc1f..ebfa9f4d24ef 100644
>> --- a/drivers/fpga/socfpga.c
>> +++ b/drivers/fpga/socfpga.c
>> @@ -571,12 +571,12 @@ static int socfpga_fpga_probe(struct platform_device *pdev)
>>  	if (ret)
>>  		return ret;
>>  
>> -	mgr = devm_fpga_mgr_create(dev, "Altera SOCFPGA FPGA Manager",
>> -				   &socfpga_fpga_ops, priv);
>> -	if (!mgr)
>> -		return -ENOMEM;
>> +	mgr = devm_fpga_mgr_register(dev, "Altera SOCFPGA FPGA Manager",
>> +				     &socfpga_fpga_ops, priv);
>> +	if (IS_ERR(mgr))
>> +		return PTR_ERR(mgr);
>>  
>> -	return devm_fpga_mgr_register(dev, mgr);
>> +	return 0;
>>  }
>>  
>>  #ifdef CONFIG_OF
>> diff --git a/drivers/fpga/stratix10-soc.c b/drivers/fpga/stratix10-soc.c
>> index 657a70c5fc99..e0bd91b3f7f8 100644
>> --- a/drivers/fpga/stratix10-soc.c
>> +++ b/drivers/fpga/stratix10-soc.c
>> @@ -425,18 +425,11 @@ static int s10_probe(struct platform_device *pdev)
>>  
>>  	init_completion(&priv->status_return_completion);
>>  
>> -	mgr = fpga_mgr_create(dev, "Stratix10 SOC FPGA Manager",
>> -			      &s10_ops, priv);
>> -	if (!mgr) {
>> -		dev_err(dev, "unable to create FPGA manager\n");
>> -		ret = -ENOMEM;
>> -		goto probe_err;
>> -	}
>> -
>> -	ret = fpga_mgr_register(mgr);
>> -	if (ret) {
>> +	mgr = fpga_mgr_register(dev, "Stratix10 SOC FPGA Manager",
>> +				&s10_ops, priv);
>> +	if (IS_ERR(mgr)) {
>>  		dev_err(dev, "unable to register FPGA manager\n");
>> -		fpga_mgr_free(mgr);
>> +		ret = PTR_ERR(mgr);
>>  		goto probe_err;
>>  	}
>>  
>> diff --git a/drivers/fpga/ts73xx-fpga.c b/drivers/fpga/ts73xx-fpga.c
>> index 101f016c6ed8..5efbec2b9f9f 100644
>> --- a/drivers/fpga/ts73xx-fpga.c
>> +++ b/drivers/fpga/ts73xx-fpga.c
>> @@ -122,12 +122,12 @@ static int ts73xx_fpga_probe(struct platform_device *pdev)
>>  	if (IS_ERR(priv->io_base))
>>  		return PTR_ERR(priv->io_base);
>>  
>> -	mgr = devm_fpga_mgr_create(kdev, "TS-73xx FPGA Manager",
>> -				   &ts73xx_fpga_ops, priv);
>> -	if (!mgr)
>> -		return -ENOMEM;
>> +	mgr = devm_fpga_mgr_register(kdev, "TS-73xx FPGA Manager",
>> +				     &ts73xx_fpga_ops, priv);
>> +	if (IS_ERR(mgr))
>> +		return PTR_ERR(mgr);
>>  
>> -	return devm_fpga_mgr_register(kdev, mgr);
>> +	return 0;
>>  }
>>  
>>  static struct platform_driver ts73xx_fpga_driver = {
>> diff --git a/drivers/fpga/xilinx-spi.c b/drivers/fpga/xilinx-spi.c
>> index fee4d0abf6bf..efda475f7a98 100644
>> --- a/drivers/fpga/xilinx-spi.c
>> +++ b/drivers/fpga/xilinx-spi.c
>> @@ -247,13 +247,13 @@ static int xilinx_spi_probe(struct spi_device *spi)
>>  		return dev_err_probe(&spi->dev, PTR_ERR(conf->done),
>>  				     "Failed to get DONE gpio\n");
>>  
>> -	mgr = devm_fpga_mgr_create(&spi->dev,
>> -				   "Xilinx Slave Serial FPGA Manager",
>> -				   &xilinx_spi_ops, conf);
>> -	if (!mgr)
>> -		return -ENOMEM;
>> +	mgr = devm_fpga_mgr_register(&spi->dev,
>> +				     "Xilinx Slave Serial FPGA Manager",
>> +				     &xilinx_spi_ops, conf);
>> +	if (IS_ERR(mgr))
>> +		return PTR_ERR(mgr);
>>  
>> -	return devm_fpga_mgr_register(&spi->dev, mgr);
>> +	return 0;
>>  }
>>  
>>  static const struct of_device_id xlnx_spi_of_match[] = {
>> diff --git a/drivers/fpga/zynq-fpga.c b/drivers/fpga/zynq-fpga.c
>> index 07fa8d9ec675..74bbb9710f4e 100644
>> --- a/drivers/fpga/zynq-fpga.c
>> +++ b/drivers/fpga/zynq-fpga.c
>> @@ -609,20 +609,16 @@ static int zynq_fpga_probe(struct platform_device *pdev)
>>  
>>  	clk_disable(priv->clk);
>>  
>> -	mgr = devm_fpga_mgr_create(dev, "Xilinx Zynq FPGA Manager",
>> -				   &zynq_fpga_ops, priv);
>> -	if (!mgr)
>> -		return -ENOMEM;
>> -
>> -	platform_set_drvdata(pdev, mgr);
>> -
>> -	err = fpga_mgr_register(mgr);
>> -	if (err) {
>> +	mgr = fpga_mgr_register(dev, "Xilinx Zynq FPGA Manager",
>> +				&zynq_fpga_ops, priv);
>> +	if (IS_ERR(mgr)) {
>>  		dev_err(dev, "unable to register FPGA manager\n");
>>  		clk_unprepare(priv->clk);
>> -		return err;
>> +		return PTR_ERR(mgr);
>>  	}
>>  
>> +	platform_set_drvdata(pdev, mgr);
>> +
>>  	return 0;
>>  }
>>  
>> diff --git a/drivers/fpga/zynqmp-fpga.c b/drivers/fpga/zynqmp-fpga.c
>> index 125743c9797f..1a6d520985be 100644
>> --- a/drivers/fpga/zynqmp-fpga.c
>> +++ b/drivers/fpga/zynqmp-fpga.c
>> @@ -102,12 +102,12 @@ static int zynqmp_fpga_probe(struct platform_device *pdev)
>>  
>>  	priv->dev = dev;
>>  
>> -	mgr = devm_fpga_mgr_create(dev, "Xilinx ZynqMP FPGA Manager",
>> -				   &zynqmp_fpga_ops, priv);
>> -	if (!mgr)
>> -		return -ENOMEM;
>> +	mgr = devm_fpga_mgr_register(dev, "Xilinx ZynqMP FPGA Manager",
>> +				     &zynqmp_fpga_ops, priv);
>> +	if (IS_ERR(mgr))
>> +		return PTR_ERR(mgr);
>>  
>> -	return devm_fpga_mgr_register(dev, mgr);
>> +	return 0;
>>  }
>>  
>>  static const struct of_device_id zynqmp_fpga_of_match[] = {
>> diff --git a/include/linux/fpga/fpga-mgr.h b/include/linux/fpga/fpga-mgr.h
>> index 2bc3030a69e5..d1ccf8f8c38a 100644
>> --- a/include/linux/fpga/fpga-mgr.h
>> +++ b/include/linux/fpga/fpga-mgr.h
>> @@ -191,17 +191,14 @@ struct fpga_manager *fpga_mgr_get(struct device *dev);
>>  
>>  void fpga_mgr_put(struct fpga_manager *mgr);
>>  
>> -struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
>> -				     const struct fpga_manager_ops *mops,
>> -				     void *priv);
>> -void fpga_mgr_free(struct fpga_manager *mgr);
>> -int fpga_mgr_register(struct fpga_manager *mgr);
>> +struct fpga_manager *fpga_mgr_register(struct device *dev, const char *name,
>> +				       const struct fpga_manager_ops *mops,
>> +				       void *priv);
>>  void fpga_mgr_unregister(struct fpga_manager *mgr);
>>  
>> -int devm_fpga_mgr_register(struct device *dev, struct fpga_manager *mgr);
>> -
>> -struct fpga_manager *devm_fpga_mgr_create(struct device *dev, const char *name,
>> -					  const struct fpga_manager_ops *mops,
>> -					  void *priv);
>> +struct fpga_manager *
>> +devm_fpga_mgr_register(struct device *dev, const char *name,
>> +		       const struct fpga_manager_ops *mops,
>> +		       void *priv);
>>  
>>  #endif /*_LINUX_FPGA_MGR_H */
>> -- 
>> 2.25.1


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v3 3/3] fpga: region: Use standard dev_release for class driver
  2021-05-28 16:10   ` Xu Yilun
@ 2021-05-28 17:43     ` Russ Weight
  0 siblings, 0 replies; 17+ messages in thread
From: Russ Weight @ 2021-05-28 17:43 UTC (permalink / raw)
  To: Xu Yilun
  Cc: mdf, linux-fpga, trix, lgoncalv, hao.wu, matthew.gerlach, richard.gong



On 5/28/21 9:10 AM, Xu Yilun wrote:
> On Mon, May 24, 2021 at 11:50:49AM -0700, Russ Weight wrote:
>> The FPGA region class driver data structure is being treated as a
>> managed resource instead of using standard dev_release call-back
>> to release the class data structure. This change removes the
>> managed resource code and combines the create() and register()
>> functions into a single register() function.
>>
>> Signed-off-by: Russ Weight <russell.h.weight@intel.com>
>> ---
>> v3:
>>   - Cleaned up comment header for fpga_region_register()
>>   - Fix fpga_region_register() error return on ida_simple_get() failure
>> v2:
>>   - No changes
>> ---
>>  drivers/fpga/dfl-fme-region.c    |  10 +--
>>  drivers/fpga/dfl.c               |  10 +--
>>  drivers/fpga/fpga-region.c       | 103 ++++++-------------------------
>>  drivers/fpga/of-fpga-region.c    |  10 +--
>>  include/linux/fpga/fpga-region.h |  12 +---
>>  5 files changed, 32 insertions(+), 113 deletions(-)
>>
>> diff --git a/drivers/fpga/dfl-fme-region.c b/drivers/fpga/dfl-fme-region.c
>> index 1eeb42af1012..631c8ce160e3 100644
>> --- a/drivers/fpga/dfl-fme-region.c
>> +++ b/drivers/fpga/dfl-fme-region.c
>> @@ -39,9 +39,9 @@ static int fme_region_probe(struct platform_device *pdev)
>>  	if (IS_ERR(mgr))
>>  		return -EPROBE_DEFER;
>>  
>> -	region = devm_fpga_region_create(dev, mgr, fme_region_get_bridges);
>> -	if (!region) {
>> -		ret = -ENOMEM;
>> +	region = fpga_region_register(dev, mgr, fme_region_get_bridges);
>> +	if (IS_ERR(region)) {
>> +		ret = PTR_ERR(region);
>>  		goto eprobe_mgr_put;
>>  	}
>>  
>> @@ -49,10 +49,6 @@ static int fme_region_probe(struct platform_device *pdev)
>>  	region->compat_id = mgr->compat_id;
> This is the issue I've mentioned in Patch #1. There is a chance user
> get zero value via compat_id sysfs node.
Yes - I'll fix this.

>
>>  	platform_set_drvdata(pdev, region);
>>  
>> -	ret = fpga_region_register(region);
>> -	if (ret)
>> -		goto eprobe_mgr_put;
>> -
>>  	dev_dbg(dev, "DFL FME FPGA Region probed\n");
>>  
>>  	return 0;
>> diff --git a/drivers/fpga/dfl.c b/drivers/fpga/dfl.c
>> index 511b20ff35a3..2fb3a5bfe4df 100644
>> --- a/drivers/fpga/dfl.c
>> +++ b/drivers/fpga/dfl.c
>> @@ -1400,9 +1400,9 @@ dfl_fpga_feature_devs_enumerate(struct dfl_fpga_enum_info *info)
>>  	if (!cdev)
>>  		return ERR_PTR(-ENOMEM);
>>  
>> -	cdev->region = devm_fpga_region_create(info->dev, NULL, NULL);
>> -	if (!cdev->region) {
>> -		ret = -ENOMEM;
>> +	cdev->region = fpga_region_register(info->dev, NULL, NULL);
>> +	if (IS_ERR(cdev->region)) {
>> +		ret = PTR_ERR(cdev->region);
>>  		goto free_cdev_exit;
>>  	}
>>  
>> @@ -1410,10 +1410,6 @@ dfl_fpga_feature_devs_enumerate(struct dfl_fpga_enum_info *info)
>>  	mutex_init(&cdev->lock);
>>  	INIT_LIST_HEAD(&cdev->port_dev_list);
> How about we moves fpga_region_register() here? It was here before.

Sure - I'll do that.

Thanks,
- Russ
>
> Thanks,
> Yilun
>
>>  
>> -	ret = fpga_region_register(cdev->region);
>> -	if (ret)
>> -		goto free_cdev_exit;
>> -
>>  	/* create and init build info for enumeration */
>>  	binfo = devm_kzalloc(info->dev, sizeof(*binfo), GFP_KERNEL);
>>  	if (!binfo) {
>> diff --git a/drivers/fpga/fpga-region.c b/drivers/fpga/fpga-region.c
>> index c3134b89c3fe..f65f60367e2c 100644
>> --- a/drivers/fpga/fpga-region.c
>> +++ b/drivers/fpga/fpga-region.c
>> @@ -180,39 +180,35 @@ static struct attribute *fpga_region_attrs[] = {
>>  ATTRIBUTE_GROUPS(fpga_region);
>>  
>>  /**
>> - * fpga_region_create - alloc and init a struct fpga_region
>> + * fpga_region_register - create and register a FPGA Region device
>>   * @dev: device parent
>>   * @mgr: manager that programs this region
>>   * @get_bridges: optional function to get bridges to a list
>>   *
>> - * The caller of this function is responsible for freeing the resulting region
>> - * struct with fpga_region_free().  Using devm_fpga_region_create() instead is
>> - * recommended.
>> - *
>> - * Return: struct fpga_region or NULL
>> + * Returns a struct fpga_region pointer on success, or ERR_PTR() on error.
>>   */
>> -struct fpga_region
>> -*fpga_region_create(struct device *dev,
>> -		    struct fpga_manager *mgr,
>> -		    int (*get_bridges)(struct fpga_region *))
>> +struct fpga_region *
>> +fpga_region_register(struct device *dev, struct fpga_manager *mgr,
>> +		     int (*get_bridges)(struct fpga_region *))
>>  {
>>  	struct fpga_region *region;
>>  	int id, ret = 0;
>>  
>>  	region = kzalloc(sizeof(*region), GFP_KERNEL);
>>  	if (!region)
>> -		return NULL;
>> +		return ERR_PTR(-ENOMEM);
>>  
>>  	id = ida_simple_get(&fpga_region_ida, 0, 0, GFP_KERNEL);
>> -	if (id < 0)
>> +	if (id < 0) {
>> +		ret = id;
>>  		goto err_free;
>> +	}
>>  
>>  	region->mgr = mgr;
>>  	region->get_bridges = get_bridges;
>>  	mutex_init(&region->mutex);
>>  	INIT_LIST_HEAD(&region->bridge_list);
>>  
>> -	device_initialize(&region->dev);
>>  	region->dev.class = fpga_region_class;
>>  	region->dev.parent = dev;
>>  	region->dev.of_node = dev->of_node;
>> @@ -222,6 +218,12 @@ struct fpga_region
>>  	if (ret)
>>  		goto err_remove;
>>  
>> +	ret = device_register(&region->dev);
>> +	if (ret) {
>> +		put_device(&region->dev);
>> +		return ERR_PTR(ret);
>> +	}
>> +
>>  	return region;
>>  
>>  err_remove:
>> @@ -229,76 +231,7 @@ struct fpga_region
>>  err_free:
>>  	kfree(region);
>>  
>> -	return NULL;
>> -}
>> -EXPORT_SYMBOL_GPL(fpga_region_create);
>> -
>> -/**
>> - * fpga_region_free - free a FPGA region created by fpga_region_create()
>> - * @region: FPGA region
>> - */
>> -void fpga_region_free(struct fpga_region *region)
>> -{
>> -	ida_simple_remove(&fpga_region_ida, region->dev.id);
>> -	kfree(region);
>> -}
>> -EXPORT_SYMBOL_GPL(fpga_region_free);
>> -
>> -static void devm_fpga_region_release(struct device *dev, void *res)
>> -{
>> -	struct fpga_region *region = *(struct fpga_region **)res;
>> -
>> -	fpga_region_free(region);
>> -}
>> -
>> -/**
>> - * devm_fpga_region_create - create and initialize a managed FPGA region struct
>> - * @dev: device parent
>> - * @mgr: manager that programs this region
>> - * @get_bridges: optional function to get bridges to a list
>> - *
>> - * This function is intended for use in a FPGA region driver's probe function.
>> - * After the region driver creates the region struct with
>> - * devm_fpga_region_create(), it should register it with fpga_region_register().
>> - * The region driver's remove function should call fpga_region_unregister().
>> - * The region struct allocated with this function will be freed automatically on
>> - * driver detach.  This includes the case of a probe function returning error
>> - * before calling fpga_region_register(), the struct will still get cleaned up.
>> - *
>> - * Return: struct fpga_region or NULL
>> - */
>> -struct fpga_region
>> -*devm_fpga_region_create(struct device *dev,
>> -			 struct fpga_manager *mgr,
>> -			 int (*get_bridges)(struct fpga_region *))
>> -{
>> -	struct fpga_region **ptr, *region;
>> -
>> -	ptr = devres_alloc(devm_fpga_region_release, sizeof(*ptr), GFP_KERNEL);
>> -	if (!ptr)
>> -		return NULL;
>> -
>> -	region = fpga_region_create(dev, mgr, get_bridges);
>> -	if (!region) {
>> -		devres_free(ptr);
>> -	} else {
>> -		*ptr = region;
>> -		devres_add(dev, ptr);
>> -	}
>> -
>> -	return region;
>> -}
>> -EXPORT_SYMBOL_GPL(devm_fpga_region_create);
>> -
>> -/**
>> - * fpga_region_register - register a FPGA region
>> - * @region: FPGA region
>> - *
>> - * Return: 0 or -errno
>> - */
>> -int fpga_region_register(struct fpga_region *region)
>> -{
>> -	return device_add(&region->dev);
>> +	return ERR_PTR(ret);
>>  }
>>  EXPORT_SYMBOL_GPL(fpga_region_register);
>>  
>> @@ -316,6 +249,10 @@ EXPORT_SYMBOL_GPL(fpga_region_unregister);
>>  
>>  static void fpga_region_dev_release(struct device *dev)
>>  {
>> +	struct fpga_region *region = to_fpga_region(dev);
>> +
>> +	ida_simple_remove(&fpga_region_ida, region->dev.id);
>> +	kfree(region);
>>  }
>>  
>>  /**
>> diff --git a/drivers/fpga/of-fpga-region.c b/drivers/fpga/of-fpga-region.c
>> index e405309baadc..466e083654ae 100644
>> --- a/drivers/fpga/of-fpga-region.c
>> +++ b/drivers/fpga/of-fpga-region.c
>> @@ -405,16 +405,12 @@ static int of_fpga_region_probe(struct platform_device *pdev)
>>  	if (IS_ERR(mgr))
>>  		return -EPROBE_DEFER;
>>  
>> -	region = devm_fpga_region_create(dev, mgr, of_fpga_region_get_bridges);
>> -	if (!region) {
>> -		ret = -ENOMEM;
>> +	region = fpga_region_register(dev, mgr, of_fpga_region_get_bridges);
>> +	if (IS_ERR(region)) {
>> +		ret = PTR_ERR(region);
>>  		goto eprobe_mgr_put;
>>  	}
>>  
>> -	ret = fpga_region_register(region);
>> -	if (ret)
>> -		goto eprobe_mgr_put;
>> -
>>  	of_platform_populate(np, fpga_region_of_match, NULL, &region->dev);
>>  	platform_set_drvdata(pdev, region);
>>  
>> diff --git a/include/linux/fpga/fpga-region.h b/include/linux/fpga/fpga-region.h
>> index 27cb706275db..ca793f43af14 100644
>> --- a/include/linux/fpga/fpga-region.h
>> +++ b/include/linux/fpga/fpga-region.h
>> @@ -37,15 +37,9 @@ struct fpga_region *fpga_region_class_find(
>>  
>>  int fpga_region_program_fpga(struct fpga_region *region);
>>  
>> -struct fpga_region
>> -*fpga_region_create(struct device *dev, struct fpga_manager *mgr,
>> -		    int (*get_bridges)(struct fpga_region *));
>> -void fpga_region_free(struct fpga_region *region);
>> -int fpga_region_register(struct fpga_region *region);
>> +struct fpga_region *
>> +fpga_region_register(struct device *dev, struct fpga_manager *mgr,
>> +		     int (*get_bridges)(struct fpga_region *));
>>  void fpga_region_unregister(struct fpga_region *region);
>>  
>> -struct fpga_region
>> -*devm_fpga_region_create(struct device *dev, struct fpga_manager *mgr,
>> -			int (*get_bridges)(struct fpga_region *));
>> -
>>  #endif /* _FPGA_REGION_H */
>> -- 
>> 2.25.1


^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2021-05-28 17:57 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-24 18:50 [PATCH v3 0/3] fpga: Use standard class dev_release function Russ Weight
2021-05-24 18:50 ` [PATCH v3 1/3] fpga: mgr: Use standard dev_release for class driver Russ Weight
2021-05-24 20:16   ` Tom Rix
2021-05-25 16:51     ` Russ Weight
2021-05-28 14:46       ` Xu Yilun
2021-05-24 22:39   ` Moritz Fischer
2021-05-24 23:07     ` Russ Weight
2021-05-25 17:39       ` Russ Weight
2021-05-25 19:00         ` Tom Rix
2021-05-28 15:41   ` Xu Yilun
2021-05-28 17:26     ` Russ Weight
2021-05-24 18:50 ` [PATCH v3 2/3] fpga: bridge: " Russ Weight
2021-05-28 15:51   ` Xu Yilun
2021-05-24 18:50 ` [PATCH v3 3/3] fpga: region: " Russ Weight
2021-05-28 16:10   ` Xu Yilun
2021-05-28 17:43     ` Russ Weight
2021-05-24 18:52 ` [PATCH v3 0/3] fpga: Use standard class dev_release function Russ Weight

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).