Skip to contentSkip to conversation

fpga: dfl: fix kernel warning on port release/assign for SRIOV

PATCH Superseded Peter Colberg 2 participants 21 messages Sep 19, 2024 – Oct 26, 2024

Subsystems (2)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover fpga: dfl: fix kernel warning on port release/assign for SRIOV

With the Intel FPGA PAC D5005, DFL ports are registered as platform devices in PF mode. The port device must be removed from the host when the user wants to configure the port as a VF for use by a user-space driver, e.g., for pass-through to a virtual machine. The FME device ioctls DFL_FPGA_FME_PORT_RELEASE/ASSIGN are assigned for this purpose.

In the previous implementation, the port platform device is not completely destroyed on port release: it is removed from the system by platform_device_del(), but the platform device instance is retained. When DFL_FPGA_FME_PORT_ASSIGN is called, the platform device is added back with platform_device_add(), which conflicts with this comment of device_add(): "Do not call this routine more than once for any device structure", and would previously cause a kernel warning at runtime.

This patch completely unregisters the port platform device on release and registers a new device on assign. But the main work is to remove the dependency on struct dfl_feature_platform_data for many internal DFL APIs. This structure holds many DFL enumeration infos for feature devices. Many DFL APIs are expected to work with these infos even when the port platform device is unregistered. But after this change, the platform_data will be freed on port release. Hence this patch introduces a new structure dfl_feature_dev_data, which acts similarly to the previous dfl_feature_platform_data. dfl_feature_platform_data then only needs a pointer to dfl_feature_dev_data to query DFL enumeration infos.

Changes since v2:
- Restructure series to break monolithic v1 patch into logical, self-contained patches, instead of per-file patches as in v2. In particular, the next-to-last patch only contains non-functional changes that are strictly limited to replacing pdata with fdata.
- Omit unneeded null pointer check from fme_open(), the same as for afu_open(). Refactor dfl_fpga_inode_to_feature_dev_data() to directly return the platform data and retrieve the device from the data.
- Fix missing free() of type in binfo_create_feature_dev_data(). Free allocated FIU type when devm_kmemdup() returns a NULL pointer.
- Store fdata instead of pdata as file descriptor private data in fme_open() since pdata is only used to look up fdata.
- Remove pdata pointers that are only used to look up fdata, in dfl_fpga_dev_feature_uinit() and dfl_feature_ioctl_set_irq().
- Omit unneeded argument pdata from dfl_feature_instance_init().
- Drop redundant 0 in zero initializer of dfl_feature_platform_data in feature_dev_register().
- Revert defining pointer to device when the pointer is only used once per function, in afu_dma_region_add() and afu_dma_region_add().
- Revert minor, unneeded code formatting changes to reduce noise.

Changes since v1:
- Split monolithic patch into series at request of maintainer.
- Substitute binfo->type for removed function feature_dev_id_type() in parse_feature_irqs().
- Return ERR_PTR(-ENOMEM) on !feature->params in binfo_create_feature_dev_data().
- Reorder cdev as first member of struct dfl_feature_platform_data such that container_of() to obtain pdata evaluates to a no-op.
- Change afu_ioctl_*() to receive dfl_feature_dev_data instead of dfl_feature_platform_data.
- Change fme_hdr_ioctl_*() to receive dfl_feature_dev_data instead of dfl_feature_platform_data.
- Replace local variable pdata with fdata in afu_mmap().
- Remove unused local variable pdata in afu_dev_{init,destroy}().
- Remove unused local variable pdata in fme_dev_{init,destroy}().
- Reorder local variables in afu_dma_unpin_pages() to reverse Christmas tree order.
- Align kernel-doc function name for __dfl_fpga_cdev_find_port_data().
- Substitute @fdata for @pdata in kernel-doc comments for dfl_fme_create_mgr() and dfl_fme_destroy_mgr().

Peter Colberg (8):
fpga: dfl: omit unneeded argument pdata from
dfl_feature_instance_init()
fpga: dfl: omit unneeded null pointer check from {afu,fme}_open()
fpga: dfl: afu: use parent device to log errors on port enable/disable
fpga: dfl: afu: define local pointer to feature device
fpga: dfl: pass feature platform data instead of device as argument
fpga: dfl: factor out feature data creation from
build_info_commit_dev()
fpga: dfl: store FIU type in feature platform data
fpga: dfl: refactor functions to take/return feature device data

Xu Yilun (1):
fpga: dfl: fix kernel warning on port release/assign for SRIOV

 drivers/fpga/dfl-afu-dma-region.c | 117 ++++----
 drivers/fpga/dfl-afu-error.c      |  59 ++--
 drivers/fpga/dfl-afu-main.c       | 278 +++++++++----------
 drivers/fpga/dfl-afu-region.c     |  51 ++--
 drivers/fpga/dfl-afu.h            |  26 +-
 drivers/fpga/dfl-fme-br.c         |  24 +-
 drivers/fpga/dfl-fme-error.c      |  98 +++----
 drivers/fpga/dfl-fme-main.c       |  95 +++----
 drivers/fpga/dfl-fme-pr.c         |  86 +++---
 drivers/fpga/dfl.c                | 439 ++++++++++++++----------------
 drivers/fpga/dfl.h                | 140 ++++++----
 11 files changed, 712 insertions(+), 701 deletions(-)
Signature · 1 line
2.46.1
#1 lore raw

PATCH v3 1/9 fpga: dfl: omit unneeded argument pdata from dfl_feature_instance_init()

The argument pdata passed to dfl_feature_instance_init() was never used.

---
 drivers/fpga/dfl.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/fpga/dfl.c +1 -3
index c406b949026f..8512a1da6570 100644
@@ -520,7 +520,6 @@ void dfl_fpga_dev_feature_uinit(struct platform_device *pdev)
 EXPORT_SYMBOL_GPL(dfl_fpga_dev_feature_uinit);
 
 static int dfl_feature_instance_init(struct platform_device *pdev,
-				     struct dfl_feature_platform_data *pdata,
 				     struct dfl_feature *feature,
 				     struct dfl_feature_driver *drv)
 {
@@ -587,8 +586,7 @@ int dfl_fpga_dev_feature_init(struct platform_device *pdev,
 	while (drv->ops) {
 		dfl_fpga_dev_for_each_feature(pdata, feature) {
 			if (dfl_feature_drv_match(feature, drv)) {
-				ret = dfl_feature_instance_init(pdev, pdata,
-								feature, drv);
+				ret = dfl_feature_instance_init(pdev, feature, drv);
 				if (ret)
 					goto exit;
 			}
Signature · 1 line
2.46.1
#2 lore raw

PATCH v3 2/9 fpga: dfl: omit unneeded null pointer check from {afu,fme}_open()

In reply to Peter Colberg

The feature platform device is guaranteed to have an associated platform data. Refactor dfl_fpga_inode_to_feature_dev_data() to directly return the platform data and retrieve the device from the data.

---
 drivers/fpga/dfl-afu-main.c | 8 ++------
 drivers/fpga/dfl-fme-main.c | 7 ++-----
 drivers/fpga/dfl.h          | 6 +++---
 3 files changed, 7 insertions(+), 14 deletions(-)
drivers/fpga/dfl-afu-main.c +2 -6
index 6b97c073849e..6125e2faada8 100644
@@ -595,14 +595,10 @@ static struct dfl_feature_driver port_feature_drvs[] = {
 
 static int afu_open(struct inode *inode, struct file *filp)
 {
-	struct platform_device *fdev = dfl_fpga_inode_to_feature_dev(inode);
-	struct dfl_feature_platform_data *pdata;
+	struct dfl_feature_platform_data *pdata = dfl_fpga_inode_to_feature_dev_data(inode);
+	struct platform_device *fdev = pdata->dev;
 	int ret;
 
-	pdata = dev_get_platdata(&fdev->dev);
-	if (WARN_ON(!pdata))
-		return -ENODEV;
-
 	mutex_lock(&pdata->lock);
 	ret = dfl_feature_dev_use_begin(pdata, filp->f_flags & O_EXCL);
 	if (!ret) {
drivers/fpga/dfl-fme-main.c +2 -5
index 864924f68f5e..480a187289bb 100644
@@ -598,13 +598,10 @@ static long fme_ioctl_check_extension(struct dfl_feature_platform_data *pdata,
 
 static int fme_open(struct inode *inode, struct file *filp)
 {
-	struct platform_device *fdev = dfl_fpga_inode_to_feature_dev(inode);
-	struct dfl_feature_platform_data *pdata = dev_get_platdata(&fdev->dev);
+	struct dfl_feature_platform_data *pdata = dfl_fpga_inode_to_feature_dev_data(inode);
+	struct platform_device *fdev = pdata->dev;
 	int ret;
 
-	if (WARN_ON(!pdata))
-		return -ENODEV;
-
 	mutex_lock(&pdata->lock);
 	ret = dfl_feature_dev_use_begin(pdata, filp->f_flags & O_EXCL);
 	if (!ret) {
drivers/fpga/dfl.h +3 -3
index 5063d73b0d82..2285215f444e 100644
@@ -398,14 +398,14 @@ int dfl_fpga_dev_ops_register(struct platform_device *pdev,
 			      struct module *owner);
 void dfl_fpga_dev_ops_unregister(struct platform_device *pdev);
 
-static inline
-struct platform_device *dfl_fpga_inode_to_feature_dev(struct inode *inode)
+static inline struct dfl_feature_platform_data *
+dfl_fpga_inode_to_feature_dev_data(struct inode *inode)
 {
 	struct dfl_feature_platform_data *pdata;
 
 	pdata = container_of(inode->i_cdev, struct dfl_feature_platform_data,
 			     cdev);
-	return pdata->dev;
+	return pdata;
 }
 
 #define dfl_fpga_dev_for_each_feature(pdata, feature)			    \
Signature · 1 line
2.46.1
#3 lore raw

Re: [PATCH v3 2/9] fpga: dfl: omit unneeded null pointer check from {afu,fme}_open()

Quoted 3 lines · Peter Colberg

The feature platform device is guaranteed to have an associated platform data. Refactor dfl_fpga_inode_to_feature_dev_data() to directly return the platform data and retrieve the device from the data.

The code is good. But please elaborate on the purpose of these intermediate change.

Thanks,
Yilun

#4 lore raw

Re: [PATCH v3 2/9] fpga: dfl: omit unneeded null pointer check from {afu,fme}_open()

Quoted 10 lines · Xu Yilun
Quoted 3 lines · Peter Colberg

The feature platform device is guaranteed to have an associated platform data. Refactor dfl_fpga_inode_to_feature_dev_data() to directly return the platform data and retrieve the device from the data.

The code is good. But please elaborate on the purpose of these intermediate change.

Thanks,
Yilun

This has been done in the revised patch "fpga: dfl: return platform data from dfl_fpga_inode_to_feature_dev_data()".

Thanks,
Peter

#5 lore raw

Re: [PATCH v3 2/9] fpga: dfl: omit unneeded null pointer check from {afu,fme}_open()

In reply to Peter Colberg
Quoted 3 lines · Peter Colberg

The feature platform device is guaranteed to have an associated platform data. Refactor dfl_fpga_inode_to_feature_dev_data() to directly return the platform data and retrieve the device from the data.

These changelog better describes the change, but the short log does not. Please update.

Thanks,
Yilun

Quoted 76 lines
---
 drivers/fpga/dfl-afu-main.c | 8 ++------
 drivers/fpga/dfl-fme-main.c | 7 ++-----
 drivers/fpga/dfl.h          | 6 +++---
 3 files changed, 7 insertions(+), 14 deletions(-)
drivers/fpga/dfl-afu-main.c +2 -6
index 6b97c073849e..6125e2faada8 100644
@@ -595,14 +595,10 @@ static struct dfl_feature_driver port_feature_drvs[] = {
 
 static int afu_open(struct inode *inode, struct file *filp)
 {
-	struct platform_device *fdev = dfl_fpga_inode_to_feature_dev(inode);
-	struct dfl_feature_platform_data *pdata;
+	struct dfl_feature_platform_data *pdata = dfl_fpga_inode_to_feature_dev_data(inode);
+	struct platform_device *fdev = pdata->dev;
 	int ret;
 
-	pdata = dev_get_platdata(&fdev->dev);
-	if (WARN_ON(!pdata))
-		return -ENODEV;
-
 	mutex_lock(&pdata->lock);
 	ret = dfl_feature_dev_use_begin(pdata, filp->f_flags & O_EXCL);
 	if (!ret) {
drivers/fpga/dfl-fme-main.c +2 -5
index 864924f68f5e..480a187289bb 100644
@@ -598,13 +598,10 @@ static long fme_ioctl_check_extension(struct dfl_feature_platform_data *pdata,
 
 static int fme_open(struct inode *inode, struct file *filp)
 {
-	struct platform_device *fdev = dfl_fpga_inode_to_feature_dev(inode);
-	struct dfl_feature_platform_data *pdata = dev_get_platdata(&fdev->dev);
+	struct dfl_feature_platform_data *pdata = dfl_fpga_inode_to_feature_dev_data(inode);
+	struct platform_device *fdev = pdata->dev;
 	int ret;
 
-	if (WARN_ON(!pdata))
-		return -ENODEV;
-
 	mutex_lock(&pdata->lock);
 	ret = dfl_feature_dev_use_begin(pdata, filp->f_flags & O_EXCL);
 	if (!ret) {
drivers/fpga/dfl.h +3 -3
index 5063d73b0d82..2285215f444e 100644
@@ -398,14 +398,14 @@ int dfl_fpga_dev_ops_register(struct platform_device *pdev,
 			      struct module *owner);
 void dfl_fpga_dev_ops_unregister(struct platform_device *pdev);
 
-static inline
-struct platform_device *dfl_fpga_inode_to_feature_dev(struct inode *inode)
+static inline struct dfl_feature_platform_data *
+dfl_fpga_inode_to_feature_dev_data(struct inode *inode)
 {
 	struct dfl_feature_platform_data *pdata;
 
 	pdata = container_of(inode->i_cdev, struct dfl_feature_platform_data,
 			     cdev);
-	return pdata->dev;
+	return pdata;
 }
 
 #define dfl_fpga_dev_for_each_feature(pdata, feature)			    \
Signature · 1 line
2.46.1
#6 lore raw

Re: [PATCH v3 2/9] fpga: dfl: omit unneeded null pointer check from {afu,fme}_open()

Quoted 10 lines · Xu Yilun
Quoted 3 lines · Peter Colberg

The feature platform device is guaranteed to have an associated platform data. Refactor dfl_fpga_inode_to_feature_dev_data() to directly return the platform data and retrieve the device from the data.

These changelog better describes the change, but the short log does not. Please update.

Thanks,
Yilun

The subject has been revised in the patch "fpga: dfl: return platform data from dfl_fpga_inode_to_feature_dev_data()".

Thanks,
Peter

Quoted 77 lines
Quoted 77 lines
---
 drivers/fpga/dfl-afu-main.c | 8 ++------
 drivers/fpga/dfl-fme-main.c | 7 ++-----
 drivers/fpga/dfl.h          | 6 +++---
 3 files changed, 7 insertions(+), 14 deletions(-)
drivers/fpga/dfl-afu-main.c +2 -6
index 6b97c073849e..6125e2faada8 100644
@@ -595,14 +595,10 @@ static struct dfl_feature_driver port_feature_drvs[] = {
 
 static int afu_open(struct inode *inode, struct file *filp)
 {
-	struct platform_device *fdev = dfl_fpga_inode_to_feature_dev(inode);
-	struct dfl_feature_platform_data *pdata;
+	struct dfl_feature_platform_data *pdata = dfl_fpga_inode_to_feature_dev_data(inode);
+	struct platform_device *fdev = pdata->dev;
 	int ret;
 
-	pdata = dev_get_platdata(&fdev->dev);
-	if (WARN_ON(!pdata))
-		return -ENODEV;
-
 	mutex_lock(&pdata->lock);
 	ret = dfl_feature_dev_use_begin(pdata, filp->f_flags & O_EXCL);
 	if (!ret) {
drivers/fpga/dfl-fme-main.c +2 -5
index 864924f68f5e..480a187289bb 100644
@@ -598,13 +598,10 @@ static long fme_ioctl_check_extension(struct dfl_feature_platform_data *pdata,
 
 static int fme_open(struct inode *inode, struct file *filp)
 {
-	struct platform_device *fdev = dfl_fpga_inode_to_feature_dev(inode);
-	struct dfl_feature_platform_data *pdata = dev_get_platdata(&fdev->dev);
+	struct dfl_feature_platform_data *pdata = dfl_fpga_inode_to_feature_dev_data(inode);
+	struct platform_device *fdev = pdata->dev;
 	int ret;
 
-	if (WARN_ON(!pdata))
-		return -ENODEV;
-
 	mutex_lock(&pdata->lock);
 	ret = dfl_feature_dev_use_begin(pdata, filp->f_flags & O_EXCL);
 	if (!ret) {
drivers/fpga/dfl.h +3 -3
index 5063d73b0d82..2285215f444e 100644
@@ -398,14 +398,14 @@ int dfl_fpga_dev_ops_register(struct platform_device *pdev,
 			      struct module *owner);
 void dfl_fpga_dev_ops_unregister(struct platform_device *pdev);
 
-static inline
-struct platform_device *dfl_fpga_inode_to_feature_dev(struct inode *inode)
+static inline struct dfl_feature_platform_data *
+dfl_fpga_inode_to_feature_dev_data(struct inode *inode)
 {
 	struct dfl_feature_platform_data *pdata;
 
 	pdata = container_of(inode->i_cdev, struct dfl_feature_platform_data,
 			     cdev);
-	return pdata->dev;
+	return pdata;
 }
 
 #define dfl_fpga_dev_for_each_feature(pdata, feature)			    \
Signature · 1 line
2.46.1
#7 lore raw

PATCH v3 3/9 fpga: dfl: afu: use parent device to log errors on port enable/disable

In reply to Peter Colberg

AFU port enable/disable may be triggered from userspace at any point, e.g., after a port has been released. This commit prepares a subsequent commit that destroys the port platform device on port release, which is then no longer available during port enable/disable. Use the parent, physical, e.g., PCIe FPGA device instead for logging errors.

---
 drivers/fpga/dfl-afu-main.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/fpga/dfl-afu-main.c +4 -2
index 6125e2faada8..c0f2db72b5b9 100644
@@ -60,7 +60,8 @@ int __afu_port_enable(struct platform_device *pdev)
 	if (readq_poll_timeout(base + PORT_HDR_CTRL, v,
 			       !(v & PORT_CTRL_SFTRST_ACK),
 			       RST_POLL_INVL, RST_POLL_TIMEOUT)) {
-		dev_err(&pdev->dev, "timeout, failure to enable device\n");
+		dev_err(pdata->dfl_cdev->parent,
+			"timeout, failure to enable device\n");
 		return -ETIMEDOUT;
 	}
 
@@ -99,7 +100,8 @@ int __afu_port_disable(struct platform_device *pdev)
 	if (readq_poll_timeout(base + PORT_HDR_CTRL, v,
 			       v & PORT_CTRL_SFTRST_ACK,
 			       RST_POLL_INVL, RST_POLL_TIMEOUT)) {
-		dev_err(&pdev->dev, "timeout, failure to disable device\n");
+		dev_err(pdata->dfl_cdev->parent,
+			"timeout, failure to disable device\n");
 		return -ETIMEDOUT;
 	}
 
Signature · 1 line
2.46.1
#8 lore raw

PATCH v3 4/9 fpga: dfl: afu: define local pointer to feature device

In reply to Peter Colberg

Define local pointer to pdata->dev->dev to avoid repetition.

---
 drivers/fpga/dfl-afu-dma-region.c | 9 +++++----
 drivers/fpga/dfl-afu-region.c     | 5 +++--
 2 files changed, 8 insertions(+), 6 deletions(-)
drivers/fpga/dfl-afu-dma-region.c +5 -4
index 02b60fde0430..814191e623e1 100644
@@ -301,6 +301,7 @@ afu_dma_region_find_iova(struct dfl_feature_platform_data *pdata, u64 iova)
 int afu_dma_map_region(struct dfl_feature_platform_data *pdata,
 		       u64 user_addr, u64 length, u64 *iova)
 {
+	struct device *dev = &pdata->dev->dev;
 	struct dfl_afu_dma_region *region;
 	int ret;
 
@@ -325,13 +326,13 @@ int afu_dma_map_region(struct dfl_feature_platform_data *pdata,
 	/* Pin the user memory region */
 	ret = afu_dma_pin_pages(pdata, region);
 	if (ret) {
-		dev_err(&pdata->dev->dev, "failed to pin memory region\n");
+		dev_err(dev, "failed to pin memory region\n");
 		goto free_region;
 	}
 
 	/* Only accept continuous pages, return error else */
 	if (!afu_dma_check_continuous_pages(region)) {
-		dev_err(&pdata->dev->dev, "pages are not continuous\n");
+		dev_err(dev, "pages are not continuous\n");
 		ret = -EINVAL;
 		goto unpin_pages;
 	}
@@ -342,7 +343,7 @@ int afu_dma_map_region(struct dfl_feature_platform_data *pdata,
 				    region->length,
 				    DMA_BIDIRECTIONAL);
 	if (dma_mapping_error(dfl_fpga_pdata_to_parent(pdata), region->iova)) {
-		dev_err(&pdata->dev->dev, "failed to map for dma\n");
+		dev_err(dev, "failed to map for dma\n");
 		ret = -EFAULT;
 		goto unpin_pages;
 	}
@@ -353,7 +354,7 @@ int afu_dma_map_region(struct dfl_feature_platform_data *pdata,
 	ret = afu_dma_region_add(pdata, region);
 	mutex_unlock(&pdata->lock);
 	if (ret) {
-		dev_err(&pdata->dev->dev, "failed to add dma region\n");
+		dev_err(dev, "failed to add dma region\n");
 		goto unmap_dma;
 	}
 
drivers/fpga/dfl-afu-region.c +3 -2
index 2e7b41629406..8f0e9485214a 100644
@@ -50,11 +50,12 @@ static struct dfl_afu_mmio_region *get_region_by_index(struct dfl_afu *afu,
 int afu_mmio_region_add(struct dfl_feature_platform_data *pdata,
 			u32 region_index, u64 region_size, u64 phys, u32 flags)
 {
+	struct device *dev = &pdata->dev->dev;
 	struct dfl_afu_mmio_region *region;
 	struct dfl_afu *afu;
 	int ret = 0;
 
-	region = devm_kzalloc(&pdata->dev->dev, sizeof(*region), GFP_KERNEL);
+	region = devm_kzalloc(dev, sizeof(*region), GFP_KERNEL);
 	if (!region)
 		return -ENOMEM;
 
@@ -85,7 +86,7 @@ int afu_mmio_region_add(struct dfl_feature_platform_data *pdata,
 	return 0;
 
 exit:
-	devm_kfree(&pdata->dev->dev, region);
+	devm_kfree(dev, region);
 	return ret;
 }
 
Signature · 1 line
2.46.1
#9 lore raw

PATCH v3 5/9 fpga: dfl: pass feature platform data instead of device as argument

In reply to Peter Colberg

For functions which use the feature platform data, instead of invoking dev_get_platdata() on the device, directly pass the data as an argument.

---
 drivers/fpga/dfl-afu-error.c | 29 +++++++-------
 drivers/fpga/dfl-afu-main.c  | 76 +++++++++++++++++++-----------------
 drivers/fpga/dfl-afu.h       |  4 +-
 drivers/fpga/dfl-fme-br.c    | 26 ++++++------
 drivers/fpga/dfl-fme-error.c | 32 ++++++++-------
 drivers/fpga/dfl-fme-main.c  | 18 ++++++---
 drivers/fpga/dfl-fme-pr.c    |  6 +--
 drivers/fpga/dfl.c           | 71 ++++++++++++++-------------------
 drivers/fpga/dfl.h           | 35 +++++++++--------
 9 files changed, 150 insertions(+), 147 deletions(-)
drivers/fpga/dfl-afu-error.c +15 -14
index ab7be6217368..ad6ea19faaa0 100644
@@ -28,11 +28,11 @@
 #define ERROR_MASK		GENMASK_ULL(63, 0)
 
 /* mask or unmask port errors by the error mask register. */
-static void __afu_port_err_mask(struct device *dev, bool mask)
+static void __afu_port_err_mask(struct dfl_feature_platform_data *pdata, bool mask)
 {
 	void __iomem *base;
 
-	base = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_ERROR);
+	base = dfl_get_feature_ioaddr_by_id(pdata, PORT_FEATURE_ID_ERROR);
 
 	writeq(mask ? ERROR_MASK : 0, base + PORT_ERROR_MASK);
 }
@@ -42,7 +42,7 @@ static void afu_port_err_mask(struct device *dev, bool mask)
 	struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
 
 	mutex_lock(&pdata->lock);
-	__afu_port_err_mask(dev, mask);
+	__afu_port_err_mask(pdata, mask);
 	mutex_unlock(&pdata->lock);
 }
 
@@ -50,13 +50,12 @@ static void afu_port_err_mask(struct device *dev, bool mask)
 static int afu_port_err_clear(struct device *dev, u64 err)
 {
 	struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
-	struct platform_device *pdev = to_platform_device(dev);
 	void __iomem *base_err, *base_hdr;
 	int enable_ret = 0, ret = -EBUSY;
 	u64 v;
 
-	base_err = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_ERROR);
-	base_hdr = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_HEADER);
+	base_err = dfl_get_feature_ioaddr_by_id(pdata, PORT_FEATURE_ID_ERROR);
+	base_hdr = dfl_get_feature_ioaddr_by_id(pdata, PORT_FEATURE_ID_HEADER);
 
 	mutex_lock(&pdata->lock);
 
@@ -80,12 +79,12 @@ static int afu_port_err_clear(struct device *dev, u64 err)
 	}
 
 	/* Halt Port by keeping Port in reset */
-	ret = __afu_port_disable(pdev);
+	ret = __afu_port_disable(pdata);
 	if (ret)
 		goto done;
 
 	/* Mask all errors */
-	__afu_port_err_mask(dev, true);
+	__afu_port_err_mask(pdata, true);
 
 	/* Clear errors if err input matches with current port errors.*/
 	v = readq(base_err + PORT_ERROR);
@@ -102,10 +101,10 @@ static int afu_port_err_clear(struct device *dev, u64 err)
 	}
 
 	/* Clear mask */
-	__afu_port_err_mask(dev, false);
+	__afu_port_err_mask(pdata, false);
 
 	/* Enable the Port by clearing the reset */
-	enable_ret = __afu_port_enable(pdev);
+	enable_ret = __afu_port_enable(pdata);
 
 done:
 	mutex_unlock(&pdata->lock);
@@ -119,7 +118,7 @@ static ssize_t errors_show(struct device *dev, struct device_attribute *attr,
 	void __iomem *base;
 	u64 error;
 
-	base = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_ERROR);
+	base = dfl_get_feature_ioaddr_by_id(pdata, PORT_FEATURE_ID_ERROR);
 
 	mutex_lock(&pdata->lock);
 	error = readq(base + PORT_ERROR);
@@ -150,7 +149,7 @@ static ssize_t first_error_show(struct device *dev,
 	void __iomem *base;
 	u64 error;
 
-	base = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_ERROR);
+	base = dfl_get_feature_ioaddr_by_id(pdata, PORT_FEATURE_ID_ERROR);
 
 	mutex_lock(&pdata->lock);
 	error = readq(base + PORT_FIRST_ERROR);
@@ -168,7 +167,7 @@ static ssize_t first_malformed_req_show(struct device *dev,
 	void __iomem *base;
 	u64 req0, req1;
 
-	base = dfl_get_feature_ioaddr_by_id(dev, PORT_FEATURE_ID_ERROR);
+	base = dfl_get_feature_ioaddr_by_id(pdata, PORT_FEATURE_ID_ERROR);
 
 	mutex_lock(&pdata->lock);
 	req0 = readq(base + PORT_MALFORMED_REQ0);
@@ -190,13 +189,15 @@ static struct attribute *port_err_attrs[] = {
 static umode_t port_err_attrs_visible(struct kobject *kobj,
 				      struct attribute *attr, int n)
 {
+	struct dfl_feature_platform_data *pdata;
 	struct device *dev = kobj_to_dev(kobj);
 
+	pdata = dev_get_platdata(dev);
 	/*
 	 * sysfs entries are visible only if related private feature is
 	 * enumerated.
 	 */
-	if (!dfl_get_feature_by_id(dev, PORT_FEATURE_ID_ERROR))
+	if (!dfl_get_feature_by_id(pdata, PORT_FEATURE_ID_ERROR))
 		return 0;
 
 	return attr->mode;
drivers/fpga/dfl-afu-main.c +40 -36

273 lines not on this page · raw message

drivers/fpga/dfl-afu.h +2 -2
index 7bef3e300aa2..6d1e79240c70 100644
@@ -77,8 +77,8 @@ struct dfl_afu {
 };
 
 /* hold pdata->lock when call __afu_port_enable/disable */
-int __afu_port_enable(struct platform_device *pdev);
-int __afu_port_disable(struct platform_device *pdev);
+int __afu_port_enable(struct dfl_feature_platform_data *pdata);
+int __afu_port_disable(struct dfl_feature_platform_data *pdata);
 
 void afu_mmio_region_init(struct dfl_feature_platform_data *pdata);
 int afu_mmio_region_add(struct dfl_feature_platform_data *pdata,
drivers/fpga/dfl-fme-br.c +13 -13
index 0b01b3895277..c19ddb02a161 100644
@@ -22,34 +22,34 @@
 struct fme_br_priv {
 	struct dfl_fme_br_pdata *pdata;
 	struct dfl_fpga_port_ops *port_ops;
-	struct platform_device *port_pdev;
+	struct dfl_feature_platform_data *port_pdata;
 };
 
 static int fme_bridge_enable_set(struct fpga_bridge *bridge, bool enable)
 {
+	struct dfl_feature_platform_data *port_pdata;
 	struct fme_br_priv *priv = bridge->priv;
-	struct platform_device *port_pdev;
 	struct dfl_fpga_port_ops *ops;
 
-	if (!priv->port_pdev) {
-		port_pdev = dfl_fpga_cdev_find_port(priv->pdata->cdev,
-						    &priv->pdata->port_id,
-						    dfl_fpga_check_port_id);
-		if (!port_pdev)
+	if (!priv->port_pdata) {
+		port_pdata = dfl_fpga_cdev_find_port_data(priv->pdata->cdev,
+							  &priv->pdata->port_id,
+							  dfl_fpga_check_port_id);
+		if (!port_pdata)
 			return -ENODEV;
 
-		priv->port_pdev = port_pdev;
+		priv->port_pdata = port_pdata;
 	}
 
-	if (priv->port_pdev && !priv->port_ops) {
-		ops = dfl_fpga_port_ops_get(priv->port_pdev);
+	if (priv->port_pdata && !priv->port_ops) {
+		ops = dfl_fpga_port_ops_get(priv->port_pdata);
 		if (!ops || !ops->enable_set)
 			return -ENOENT;
 
 		priv->port_ops = ops;
 	}
 
-	return priv->port_ops->enable_set(priv->port_pdev, enable);
+	return priv->port_ops->enable_set(priv->port_pdata, enable);
 }
 
 static const struct fpga_bridge_ops fme_bridge_ops = {
@@ -85,8 +85,8 @@ static void fme_br_remove(struct platform_device *pdev)
 
 	fpga_bridge_unregister(br);
 
-	if (priv->port_pdev)
-		put_device(&priv->port_pdev->dev);
+	if (priv->port_pdata)
+		put_device(&priv->port_pdata->dev->dev);
 	if (priv->port_ops)
 		dfl_fpga_port_ops_put(priv->port_ops);
 }
drivers/fpga/dfl-fme-error.c +18 -14
index 51c2892ec06d..39b8e3b450d7 100644
@@ -46,7 +46,7 @@ static ssize_t pcie0_errors_show(struct device *dev,
 	void __iomem *base;
 	u64 value;
 
-	base = dfl_get_feature_ioaddr_by_id(dev, FME_FEATURE_ID_GLOBAL_ERR);
+	base = dfl_get_feature_ioaddr_by_id(pdata, FME_FEATURE_ID_GLOBAL_ERR);
 
 	mutex_lock(&pdata->lock);
 	value = readq(base + PCIE0_ERROR);
@@ -67,7 +67,7 @@ static ssize_t pcie0_errors_store(struct device *dev,
 	if (kstrtou64(buf, 0, &val))
 		return -EINVAL;
 
-	base = dfl_get_feature_ioaddr_by_id(dev, FME_FEATURE_ID_GLOBAL_ERR);
+	base = dfl_get_feature_ioaddr_by_id(pdata, FME_FEATURE_ID_GLOBAL_ERR);
 
 	mutex_lock(&pdata->lock);
 	writeq(GENMASK_ULL(63, 0), base + PCIE0_ERROR_MASK);
@@ -91,7 +91,7 @@ static ssize_t pcie1_errors_show(struct device *dev,
 	void __iomem *base;
 	u64 value;
 
-	base = dfl_get_feature_ioaddr_by_id(dev, FME_FEATURE_ID_GLOBAL_ERR);
+	base = dfl_get_feature_ioaddr_by_id(pdata, FME_FEATURE_ID_GLOBAL_ERR);
 
 	mutex_lock(&pdata->lock);
 	value = readq(base + PCIE1_ERROR);
@@ -112,7 +112,7 @@ static ssize_t pcie1_errors_store(struct device *dev,
 	if (kstrtou64(buf, 0, &val))
 		return -EINVAL;
 
-	base = dfl_get_feature_ioaddr_by_id(dev, FME_FEATURE_ID_GLOBAL_ERR);
+	base = dfl_get_feature_ioaddr_by_id(pdata, FME_FEATURE_ID_GLOBAL_ERR);
 
 	mutex_lock(&pdata->lock);
 	writeq(GENMASK_ULL(63, 0), base + PCIE1_ERROR_MASK);
@@ -132,9 +132,10 @@ static DEVICE_ATTR_RW(pcie1_errors);
 static ssize_t nonfatal_errors_show(struct device *dev,
 				    struct device_attribute *attr, char *buf)
 {
+	struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
 	void __iomem *base;
 
-	base = dfl_get_feature_ioaddr_by_id(dev, FME_FEATURE_ID_GLOBAL_ERR);
+	base = dfl_get_feature_ioaddr_by_id(pdata, FME_FEATURE_ID_GLOBAL_ERR);
 
 	return sprintf(buf, "0x%llx\n",
 		       (unsigned long long)readq(base + RAS_NONFAT_ERROR));
@@ -144,9 +145,10 @@ static DEVICE_ATTR_RO(nonfatal_errors);
 static ssize_t catfatal_errors_show(struct device *dev,
 				    struct device_attribute *attr, char *buf)
 {
+	struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
 	void __iomem *base;
 
-	base = dfl_get_feature_ioaddr_by_id(dev, FME_FEATURE_ID_GLOBAL_ERR);
+	base = dfl_get_feature_ioaddr_by_id(pdata, FME_FEATURE_ID_GLOBAL_ERR);
 
 	return sprintf(buf, "0x%llx\n",
 		       (unsigned long long)readq(base + RAS_CATFAT_ERROR));
@@ -160,7 +162,7 @@ static ssize_t inject_errors_show(struct device *dev,
 	void __iomem *base;
 	u64 v;
 
-	base = dfl_get_feature_ioaddr_by_id(dev, FME_FEATURE_ID_GLOBAL_ERR);
+	base = dfl_get_feature_ioaddr_by_id(pdata, FME_FEATURE_ID_GLOBAL_ERR);
 
 	mutex_lock(&pdata->lock);
 	v = readq(base + RAS_ERROR_INJECT);
@@ -185,7 +187,7 @@ static ssize_t inject_errors_store(struct device *dev,
 	if (inject_error & ~INJECT_ERROR_MASK)
 		return -EINVAL;
 
-	base = dfl_get_feature_ioaddr_by_id(dev, FME_FEATURE_ID_GLOBAL_ERR);
+	base = dfl_get_feature_ioaddr_by_id(pdata, FME_FEATURE_ID_GLOBAL_ERR);
 
 	mutex_lock(&pdata->lock);
 	v = readq(base + RAS_ERROR_INJECT);
@@ -205,7 +207,7 @@ static ssize_t fme_errors_show(struct device *dev,
 	void __iomem *base;
 	u64 value;
 
-	base = dfl_get_feature_ioaddr_by_id(dev, FME_FEATURE_ID_GLOBAL_ERR);
+	base = dfl_get_feature_ioaddr_by_id(pdata, FME_FEATURE_ID_GLOBAL_ERR);
 
 	mutex_lock(&pdata->lock);
 	value = readq(base + FME_ERROR);
@@ -226,7 +228,7 @@ static ssize_t fme_errors_store(struct device *dev,
 	if (kstrtou64(buf, 0, &val))
 		return -EINVAL;
 
-	base = dfl_get_feature_ioaddr_by_id(dev, FME_FEATURE_ID_GLOBAL_ERR);
+	base = dfl_get_feature_ioaddr_by_id(pdata, FME_FEATURE_ID_GLOBAL_ERR);
 
 	mutex_lock(&pdata->lock);
 	writeq(GENMASK_ULL(63, 0), base + FME_ERROR_MASK);
@@ -252,7 +254,7 @@ static ssize_t first_error_show(struct device *dev,
 	void __iomem *base;
 	u64 value;
 
-	base = dfl_get_feature_ioaddr_by_id(dev, FME_FEATURE_ID_GLOBAL_ERR);
+	base = dfl_get_feature_ioaddr_by_id(pdata, FME_FEATURE_ID_GLOBAL_ERR);
 
 	mutex_lock(&pdata->lock);
 	value = readq(base + FME_FIRST_ERROR);
@@ -269,7 +271,7 @@ static ssize_t next_error_show(struct device *dev,
 	void __iomem *base;
 	u64 value;
 
-	base = dfl_get_feature_ioaddr_by_id(dev, FME_FEATURE_ID_GLOBAL_ERR);
+	base = dfl_get_feature_ioaddr_by_id(pdata, FME_FEATURE_ID_GLOBAL_ERR);
 
 	mutex_lock(&pdata->lock);
 	value = readq(base + FME_NEXT_ERROR);
@@ -294,13 +296,15 @@ static struct attribute *fme_global_err_attrs[] = {
 static umode_t fme_global_err_attrs_visible(struct kobject *kobj,
 					    struct attribute *attr, int n)
 {
+	struct dfl_feature_platform_data *pdata;
 	struct device *dev = kobj_to_dev(kobj);
 
+	pdata = dev_get_platdata(dev);
 	/*
 	 * sysfs entries are visible only if related private feature is
 	 * enumerated.
 	 */
-	if (!dfl_get_feature_by_id(dev, FME_FEATURE_ID_GLOBAL_ERR))
+	if (!dfl_get_feature_by_id(pdata, FME_FEATURE_ID_GLOBAL_ERR))
 		return 0;
 
 	return attr->mode;
@@ -317,7 +321,7 @@ static void fme_err_mask(struct device *dev, bool mask)
 	struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
 	void __iomem *base;
 
-	base = dfl_get_feature_ioaddr_by_id(dev, FME_FEATURE_ID_GLOBAL_ERR);
+	base = dfl_get_feature_ioaddr_by_id(pdata, FME_FEATURE_ID_GLOBAL_ERR);
 
 	mutex_lock(&pdata->lock);
 
drivers/fpga/dfl-fme-main.c +12 -6
index 480a187289bb..4964e15e910b 100644
@@ -28,10 +28,11 @@
 static ssize_t ports_num_show(struct device *dev,
 			      struct device_attribute *attr, char *buf)
 {
+	struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
 	void __iomem *base;
 	u64 v;
 
-	base = dfl_get_feature_ioaddr_by_id(dev, FME_FEATURE_ID_HEADER);
+	base = dfl_get_feature_ioaddr_by_id(pdata, FME_FEATURE_ID_HEADER);
 
 	v = readq(base + FME_HDR_CAP);
 
@@ -47,10 +48,11 @@ static DEVICE_ATTR_RO(ports_num);
 static ssize_t bitstream_id_show(struct device *dev,
 				 struct device_attribute *attr, char *buf)
 {
+	struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
 	void __iomem *base;
 	u64 v;
 
-	base = dfl_get_feature_ioaddr_by_id(dev, FME_FEATURE_ID_HEADER);
+	base = dfl_get_feature_ioaddr_by_id(pdata, FME_FEATURE_ID_HEADER);
 
 	v = readq(base + FME_HDR_BITSTREAM_ID);
 
@@ -65,10 +67,11 @@ static DEVICE_ATTR_RO(bitstream_id);
 static ssize_t bitstream_metadata_show(struct device *dev,
 				       struct device_attribute *attr, char *buf)
 {
+	struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
 	void __iomem *base;
 	u64 v;
 
-	base = dfl_get_feature_ioaddr_by_id(dev, FME_FEATURE_ID_HEADER);
+	base = dfl_get_feature_ioaddr_by_id(pdata, FME_FEATURE_ID_HEADER);
 
 	v = readq(base + FME_HDR_BITSTREAM_MD);
 
@@ -79,10 +82,11 @@ static DEVICE_ATTR_RO(bitstream_metadata);
 static ssize_t cache_size_show(struct device *dev,
 			       struct device_attribute *attr, char *buf)
 {
+	struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
 	void __iomem *base;
 	u64 v;
 
-	base = dfl_get_feature_ioaddr_by_id(dev, FME_FEATURE_ID_HEADER);
+	base = dfl_get_feature_ioaddr_by_id(pdata, FME_FEATURE_ID_HEADER);
 
 	v = readq(base + FME_HDR_CAP);
 
@@ -94,10 +98,11 @@ static DEVICE_ATTR_RO(cache_size);
 static ssize_t fabric_version_show(struct device *dev,
 				   struct device_attribute *attr, char *buf)
 {
+	struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
 	void __iomem *base;
 	u64 v;
 
-	base = dfl_get_feature_ioaddr_by_id(dev, FME_FEATURE_ID_HEADER);
+	base = dfl_get_feature_ioaddr_by_id(pdata, FME_FEATURE_ID_HEADER);
 
 	v = readq(base + FME_HDR_CAP);
 
@@ -109,10 +114,11 @@ static DEVICE_ATTR_RO(fabric_version);
 static ssize_t socket_id_show(struct device *dev,
 			      struct device_attribute *attr, char *buf)
 {
+	struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
 	void __iomem *base;
 	u64 v;
 
-	base = dfl_get_feature_ioaddr_by_id(dev, FME_FEATURE_ID_HEADER);
+	base = dfl_get_feature_ioaddr_by_id(pdata, FME_FEATURE_ID_HEADER);
 
 	v = readq(base + FME_HDR_CAP);
 
drivers/fpga/dfl-fme-pr.c +2 -4
index cdcf6dea4cc9..97fc0e402edf 100644
@@ -87,8 +87,7 @@ static int fme_pr(struct platform_device *pdev, unsigned long arg)
 		return -EINVAL;
 
 	/* get fme header region */
-	fme_hdr = dfl_get_feature_ioaddr_by_id(&pdev->dev,
-					       FME_FEATURE_ID_HEADER);
+	fme_hdr = dfl_get_feature_ioaddr_by_id(pdata, FME_FEATURE_ID_HEADER);
 
 	/* check port id */
 	v = readq(fme_hdr + FME_HDR_CAP);
@@ -378,8 +377,7 @@ static int pr_mgmt_init(struct platform_device *pdev,
 	int ret = -ENODEV, i = 0;
 	u64 fme_cap, port_offset;
 
-	fme_hdr = dfl_get_feature_ioaddr_by_id(&pdev->dev,
-					       FME_FEATURE_ID_HEADER);
+	fme_hdr = dfl_get_feature_ioaddr_by_id(pdata, FME_FEATURE_ID_HEADER);
 
 	mutex_lock(&pdata->lock);
 	priv = dfl_fpga_pdata_get_private(pdata);
drivers/fpga/dfl.c +30 -41

187 lines not on this page · raw message

drivers/fpga/dfl.h +18 -17
index 2285215f444e..8ef9f33e22c1 100644
@@ -206,6 +206,8 @@
 #define PORT_UINT_CAP_INT_NUM	GENMASK_ULL(11, 0)	/* Interrupts num */
 #define PORT_UINT_CAP_FST_VECT	GENMASK_ULL(23, 12)	/* First Vector */
 
+struct dfl_feature_platform_data;
+
 /**
  * struct dfl_fpga_port_ops - port ops
  *
@@ -219,15 +221,15 @@ struct dfl_fpga_port_ops {
 	const char *name;
 	struct module *owner;
 	struct list_head node;
-	int (*get_id)(struct platform_device *pdev);
-	int (*enable_set)(struct platform_device *pdev, bool enable);
+	int (*get_id)(struct dfl_feature_platform_data *pdata);
+	int (*enable_set)(struct dfl_feature_platform_data *pdata, bool enable);
 };
 
 void dfl_fpga_port_ops_add(struct dfl_fpga_port_ops *ops);
 void dfl_fpga_port_ops_del(struct dfl_fpga_port_ops *ops);
-struct dfl_fpga_port_ops *dfl_fpga_port_ops_get(struct platform_device *pdev);
+struct dfl_fpga_port_ops *dfl_fpga_port_ops_get(struct dfl_feature_platform_data *pdata);
 void dfl_fpga_port_ops_put(struct dfl_fpga_port_ops *ops);
-int dfl_fpga_check_port_id(struct platform_device *pdev, void *pport_id);
+int dfl_fpga_check_port_id(struct dfl_feature_platform_data *pdata, void *pport_id);
 
 /**
  * struct dfl_feature_id - dfl private feature id
@@ -413,9 +415,8 @@ dfl_fpga_inode_to_feature_dev_data(struct inode *inode)
 	   (feature) < (pdata)->features + (pdata)->num; (feature)++)
 
 static inline
-struct dfl_feature *dfl_get_feature_by_id(struct device *dev, u16 id)
+struct dfl_feature *dfl_get_feature_by_id(struct dfl_feature_platform_data *pdata, u16 id)
 {
-	struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
 	struct dfl_feature *feature;
 
 	dfl_fpga_dev_for_each_feature(pdata, feature)
@@ -426,9 +427,9 @@ struct dfl_feature *dfl_get_feature_by_id(struct device *dev, u16 id)
 }
 
 static inline
-void __iomem *dfl_get_feature_ioaddr_by_id(struct device *dev, u16 id)
+void __iomem *dfl_get_feature_ioaddr_by_id(struct dfl_feature_platform_data *pdata, u16 id)
 {
-	struct dfl_feature *feature = dfl_get_feature_by_id(dev, id);
+	struct dfl_feature *feature = dfl_get_feature_by_id(pdata, id);
 
 	if (feature && feature->ioaddr)
 		return feature->ioaddr;
@@ -527,21 +528,21 @@ void dfl_fpga_feature_devs_remove(struct dfl_fpga_cdev *cdev);
  * device returned by __dfl_fpga_cdev_find_port and dfl_fpga_cdev_find_port
  * functions.
  */
-struct platform_device *
-__dfl_fpga_cdev_find_port(struct dfl_fpga_cdev *cdev, void *data,
-			  int (*match)(struct platform_device *, void *));
+struct dfl_feature_platform_data *
+__dfl_fpga_cdev_find_port_data(struct dfl_fpga_cdev *cdev, void *data,
+			       int (*match)(struct dfl_feature_platform_data *, void *));
 
-static inline struct platform_device *
-dfl_fpga_cdev_find_port(struct dfl_fpga_cdev *cdev, void *data,
-			int (*match)(struct platform_device *, void *))
+static inline struct dfl_feature_platform_data *
+dfl_fpga_cdev_find_port_data(struct dfl_fpga_cdev *cdev, void *data,
+			     int (*match)(struct dfl_feature_platform_data *, void *))
 {
-	struct platform_device *pdev;
+	struct dfl_feature_platform_data *pdata;
 
 	mutex_lock(&cdev->lock);
-	pdev = __dfl_fpga_cdev_find_port(cdev, data, match);
+	pdata = __dfl_fpga_cdev_find_port_data(cdev, data, match);
 	mutex_unlock(&cdev->lock);
 
-	return pdev;
+	return pdata;
 }
 
 int dfl_fpga_cdev_release_port(struct dfl_fpga_cdev *cdev, int port_id);
Signature · 1 line
2.46.1
#10 lore raw

PATCH v3 6/9 fpga: dfl: factor out feature data creation from build_info_commit_dev()

In reply to Peter Colberg

Add a separate function binfo_create_feature_dev_data() which allocates and populates the feature platform data, and call the function from build_info_commit_dev() which registers the feature platform device.

---
 drivers/fpga/dfl.c | 74 ++++++++++++++++++++++++++--------------------
 1 file changed, 42 insertions(+), 32 deletions(-)
drivers/fpga/dfl.c +42 -32
index 4c79d433d216..e644eb9fde39 100644
@@ -749,12 +749,8 @@ static void dfl_fpga_cdev_add_port_data(struct dfl_fpga_cdev *cdev,
 	mutex_unlock(&cdev->lock);
 }
 
-/*
- * register current feature device, it is called when we need to switch to
- * another feature parsing or we have parsed all features on given device
- * feature list.
- */
-static int build_info_commit_dev(struct build_feature_devs_info *binfo)
+static struct dfl_feature_platform_data *
+binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 {
 	struct platform_device *fdev = binfo->feature_dev;
 	struct dfl_feature_platform_data *pdata;
@@ -764,7 +760,7 @@ static int build_info_commit_dev(struct build_feature_devs_info *binfo)
 
 	type = feature_dev_id_type(fdev);
 	if (WARN_ON_ONCE(type >= DFL_ID_MAX))
-		return -EINVAL;
+		return ERR_PTR(-EINVAL);
 
 	/*
 	 * we do not need to care for the memory which is associated with
@@ -774,7 +770,7 @@ static int build_info_commit_dev(struct build_feature_devs_info *binfo)
 	 */
 	pdata = kzalloc(struct_size(pdata, features, binfo->feature_num), GFP_KERNEL);
 	if (!pdata)
-		return -ENOMEM;
+		return ERR_PTR(-ENOMEM);
 
 	pdata->dev = fdev;
 	pdata->num = binfo->feature_num;
@@ -799,7 +795,7 @@ static int build_info_commit_dev(struct build_feature_devs_info *binfo)
 	fdev->resource = kcalloc(binfo->feature_num, sizeof(*fdev->resource),
 				 GFP_KERNEL);
 	if (!fdev->resource)
-		return -ENOMEM;
+		return ERR_PTR(-ENOMEM);
 
 	/* fill features and resource information for feature dev */
 	list_for_each_entry_safe(finfo, p, &binfo->sub_features, node) {
@@ -818,7 +814,7 @@ static int build_info_commit_dev(struct build_feature_devs_info *binfo)
 						       finfo->params, finfo->param_size,
 						       GFP_KERNEL);
 			if (!feature->params)
-				return -ENOMEM;
+				return ERR_PTR(-ENOMEM);
 
 			feature->param_size = finfo->param_size;
 		}
@@ -834,8 +830,10 @@ static int build_info_commit_dev(struct build_feature_devs_info *binfo)
 			feature->ioaddr =
 				devm_ioremap_resource(binfo->dev,
 						      &finfo->mmio_res);
-			if (IS_ERR(feature->ioaddr))
-				return PTR_ERR(feature->ioaddr);
+			if (IS_ERR(feature->ioaddr)) {
+				ret = PTR_ERR(feature->ioaddr);
+				return ERR_PTR(ret);
+			}
 		} else {
 			feature->resource_index = res_idx;
 			fdev->resource[res_idx++] = finfo->mmio_res;
@@ -845,7 +843,7 @@ static int build_info_commit_dev(struct build_feature_devs_info *binfo)
 			ctx = devm_kcalloc(binfo->dev, finfo->nr_irqs,
 					   sizeof(*ctx), GFP_KERNEL);
 			if (!ctx)
-				return -ENOMEM;
+				return ERR_PTR(-ENOMEM);
 
 			for (i = 0; i < finfo->nr_irqs; i++)
 				ctx[i].irq =
@@ -859,25 +857,7 @@ static int build_info_commit_dev(struct build_feature_devs_info *binfo)
 		kfree(finfo);
 	}
 
-	ret = platform_device_add(binfo->feature_dev);
-	if (!ret) {
-		if (type == PORT_ID)
-			dfl_fpga_cdev_add_port_data(binfo->cdev,
-						    pdata);
-		else
-			binfo->cdev->fme_dev =
-					get_device(&binfo->feature_dev->dev);
-		/*
-		 * reset it to avoid build_info_free() freeing their resource.
-		 *
-		 * The resource of successfully registered feature devices
-		 * will be freed by platform_device_unregister(). See the
-		 * comments in build_info_create_dev().
-		 */
-		binfo->feature_dev = NULL;
-	}
-
-	return ret;
+	return pdata;
 }
 
 static int
@@ -912,6 +892,36 @@ build_info_create_dev(struct build_feature_devs_info *binfo,
 	return 0;
 }
 
+static int build_info_commit_dev(struct build_feature_devs_info *binfo)
+{
+	struct dfl_feature_platform_data *pdata;
+	int ret;
+
+	pdata = binfo_create_feature_dev_data(binfo);
+	if (IS_ERR(pdata))
+		return PTR_ERR(pdata);
+
+	ret = platform_device_add(binfo->feature_dev);
+	if (ret)
+		return ret;
+
+	if (feature_dev_id_type(binfo->feature_dev) == PORT_ID)
+		dfl_fpga_cdev_add_port_data(binfo->cdev, pdata);
+	else
+		binfo->cdev->fme_dev = get_device(&binfo->feature_dev->dev);
+
+	/*
+	 * reset it to avoid build_info_free() freeing their resource.
+	 *
+	 * The resource of successfully registered feature devices
+	 * will be freed by platform_device_unregister(). See the
+	 * comments in build_info_create_dev().
+	 */
+	binfo->feature_dev = NULL;
+
+	return 0;
+}
+
 static void build_info_free(struct build_feature_devs_info *binfo)
 {
 	struct dfl_feature_info *finfo, *p;
Signature · 1 line
2.46.1
#11 lore raw

Re: [PATCH v3 6/9] fpga: dfl: factor out feature data creation from build_info_commit_dev()

Quoted 66 lines · Peter Colberg

Add a separate function binfo_create_feature_dev_data() which allocates and populates the feature platform data, and call the function from build_info_commit_dev() which registers the feature platform device.

---
 drivers/fpga/dfl.c | 74 ++++++++++++++++++++++++++--------------------
 1 file changed, 42 insertions(+), 32 deletions(-)
drivers/fpga/dfl.c +6 -10
index 4c79d433d216..e644eb9fde39 100644
@@ -749,12 +749,8 @@ static void dfl_fpga_cdev_add_port_data(struct dfl_fpga_cdev *cdev,
 	mutex_unlock(&cdev->lock);
 }
 
-/*
- * register current feature device, it is called when we need to switch to
- * another feature parsing or we have parsed all features on given device
- * feature list.
- */
-static int build_info_commit_dev(struct build_feature_devs_info *binfo)
+static struct dfl_feature_platform_data *
+binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 {
 	struct platform_device *fdev = binfo->feature_dev;
 	struct dfl_feature_platform_data *pdata;
@@ -764,7 +760,7 @@ static int build_info_commit_dev(struct build_feature_devs_info *binfo)
 
 	type = feature_dev_id_type(fdev);
 	if (WARN_ON_ONCE(type >= DFL_ID_MAX))
-		return -EINVAL;
+		return ERR_PTR(-EINVAL);
 
 	/*
 	 * we do not need to care for the memory which is associated with
@@ -774,7 +770,7 @@ static int build_info_commit_dev(struct build_feature_devs_info *binfo)
 	 */
 	pdata = kzalloc(struct_size(pdata, features, binfo->feature_num), GFP_KERNEL);
 	if (!pdata)
-		return -ENOMEM;
+		return ERR_PTR(-ENOMEM);
 
 	pdata->dev = fdev;
 	pdata->num = binfo->feature_num;
@@ -799,7 +795,7 @@ static int build_info_commit_dev(struct build_feature_devs_info *binfo)
 	fdev->resource = kcalloc(binfo->feature_num, sizeof(*fdev->resource),
 				 GFP_KERNEL);
 	if (!fdev->resource)
-		return -ENOMEM;
+		return ERR_PTR(-ENOMEM);
 
 	/* fill features and resource information for feature dev */
 	list_for_each_entry_safe(finfo, p, &binfo->sub_features, node) {
@@ -818,7 +814,7 @@ static int build_info_commit_dev(struct build_feature_devs_info *binfo)
 						       finfo->params, finfo->param_size,
 						       GFP_KERNEL);
 			if (!feature->params)
-				return -ENOMEM;
+				return ERR_PTR(-ENOMEM);
 
 			feature->param_size = finfo->param_size;
 		}

drivers/fpga/dfl.c static int build_info_commit_dev(struct build_feature_devs_info *binfo) · line 835

Quoted 8 lines
drivers/fpga/dfl.c +3 -2
@@ -834,8 +830,10 @@ static int build_info_commit_dev(struct build_feature_devs_info *binfo)
 			feature->ioaddr =
 				devm_ioremap_resource(binfo->dev,
 						      &finfo->mmio_res);
-			if (IS_ERR(feature->ioaddr))
-				return PTR_ERR(feature->ioaddr);
+			if (IS_ERR(feature->ioaddr)) {
+				ret = PTR_ERR(feature->ioaddr);
+				return ERR_PTR(ret);

Why change the type back and forth?

return feature->ioaddr;

is it OK?

Thanks,
Yilun

#12 lore raw

Re: [PATCH v3 6/9] fpga: dfl: factor out feature data creation from build_info_commit_dev()

Quoted 78 lines · Xu Yilun
Quoted 66 lines · Peter Colberg

Add a separate function binfo_create_feature_dev_data() which allocates and populates the feature platform data, and call the function from build_info_commit_dev() which registers the feature platform device.

---
 drivers/fpga/dfl.c | 74 ++++++++++++++++++++++++++--------------------
 1 file changed, 42 insertions(+), 32 deletions(-)
drivers/fpga/dfl.c +6 -10
index 4c79d433d216..e644eb9fde39 100644
@@ -749,12 +749,8 @@ static void dfl_fpga_cdev_add_port_data(struct dfl_fpga_cdev *cdev,
 	mutex_unlock(&cdev->lock);
 }
 
-/*
- * register current feature device, it is called when we need to switch to
- * another feature parsing or we have parsed all features on given device
- * feature list.
- */
-static int build_info_commit_dev(struct build_feature_devs_info *binfo)
+static struct dfl_feature_platform_data *
+binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 {
 	struct platform_device *fdev = binfo->feature_dev;
 	struct dfl_feature_platform_data *pdata;
@@ -764,7 +760,7 @@ static int build_info_commit_dev(struct build_feature_devs_info *binfo)
 
 	type = feature_dev_id_type(fdev);
 	if (WARN_ON_ONCE(type >= DFL_ID_MAX))
-		return -EINVAL;
+		return ERR_PTR(-EINVAL);
 
 	/*
 	 * we do not need to care for the memory which is associated with
@@ -774,7 +770,7 @@ static int build_info_commit_dev(struct build_feature_devs_info *binfo)
 	 */
 	pdata = kzalloc(struct_size(pdata, features, binfo->feature_num), GFP_KERNEL);
 	if (!pdata)
-		return -ENOMEM;
+		return ERR_PTR(-ENOMEM);
 
 	pdata->dev = fdev;
 	pdata->num = binfo->feature_num;
@@ -799,7 +795,7 @@ static int build_info_commit_dev(struct build_feature_devs_info *binfo)
 	fdev->resource = kcalloc(binfo->feature_num, sizeof(*fdev->resource),
 				 GFP_KERNEL);
 	if (!fdev->resource)
-		return -ENOMEM;
+		return ERR_PTR(-ENOMEM);
 
 	/* fill features and resource information for feature dev */
 	list_for_each_entry_safe(finfo, p, &binfo->sub_features, node) {
@@ -818,7 +814,7 @@ static int build_info_commit_dev(struct build_feature_devs_info *binfo)
 						       finfo->params, finfo->param_size,
 						       GFP_KERNEL);
 			if (!feature->params)
-				return -ENOMEM;
+				return ERR_PTR(-ENOMEM);
 
 			feature->param_size = finfo->param_size;
 		}

drivers/fpga/dfl.c static int build_info_commit_dev(struct build_feature_devs_info *binfo) · line 835

Quoted 8 lines
drivers/fpga/dfl.c +3 -2
@@ -834,8 +830,10 @@ static int build_info_commit_dev(struct build_feature_devs_info *binfo)
 			feature->ioaddr =
 				devm_ioremap_resource(binfo->dev,
 						      &finfo->mmio_res);
-			if (IS_ERR(feature->ioaddr))
-				return PTR_ERR(feature->ioaddr);
+			if (IS_ERR(feature->ioaddr)) {
+				ret = PTR_ERR(feature->ioaddr);
+				return ERR_PTR(ret);

Why change the type back and forth?

Quoted 6 lines

return feature->ioaddr;

is it OK?

Thanks,
Yilun

Yes, this has been done in the revised patch "fpga: dfl: factor out feature data creation from build_info_commit_dev()".

Thanks,
Peter

#13 lore raw

PATCH v3 7/9 fpga: dfl: store FIU type in feature platform data

In reply to Peter Colberg

Remove the local function feature_dev_id_type() in favour of persisting the FIU type in struct dfl_feature_platform_data. Add type to struct build_feature_devs_info and drop argument to build_info_create_dev().

---
 drivers/fpga/dfl.c | 57 ++++++++++++++++++++++------------------------
 drivers/fpga/dfl.h |  3 +++
 2 files changed, 30 insertions(+), 30 deletions(-)
drivers/fpga/dfl.c +27 -30
index e644eb9fde39..9610ef1ec2ff 100644
@@ -119,17 +119,6 @@ static void dfl_id_free(enum dfl_id_type type, int id)
 	mutex_unlock(&dfl_id_mutex);
 }
 
-static enum dfl_id_type feature_dev_id_type(struct platform_device *pdev)
-{
-	int i;
-
-	for (i = 0; i < ARRAY_SIZE(dfl_devs); i++)
-		if (!strcmp(dfl_devs[i].name, pdev->name))
-			return i;
-
-	return DFL_ID_MAX;
-}
-
 static enum dfl_id_type dfh_id_to_type(u16 id)
 {
 	int i;
@@ -379,7 +368,7 @@ dfl_dev_add(struct dfl_feature_platform_data *pdata,
 	if (ret)
 		goto put_dev;
 
-	ddev->type = feature_dev_id_type(pdev);
+	ddev->type = pdata->type;
 	ddev->feature_id = feature->id;
 	ddev->revision = feature->revision;
 	ddev->dfh_version = feature->dfh_version;
@@ -693,6 +682,7 @@ EXPORT_SYMBOL_GPL(dfl_fpga_dev_ops_unregister);
  * @irq_table: Linux IRQ numbers for all irqs, indexed by local irq index of
  *	       this device.
  * @feature_dev: current feature device.
+ * @type: the current FIU type.
  * @ioaddr: header register region address of current FIU in enumeration.
  * @start: register resource start of current FIU.
  * @len: max register resource length of current FIU.
@@ -706,6 +696,7 @@ struct build_feature_devs_info {
 	int *irq_table;
 
 	struct platform_device *feature_dev;
+	enum dfl_id_type type;
 	void __iomem *ioaddr;
 	resource_size_t start;
 	resource_size_t len;
@@ -752,13 +743,12 @@ static void dfl_fpga_cdev_add_port_data(struct dfl_fpga_cdev *cdev,
 static struct dfl_feature_platform_data *
 binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 {
+	enum dfl_id_type type = binfo->type;
 	struct platform_device *fdev = binfo->feature_dev;
 	struct dfl_feature_platform_data *pdata;
 	struct dfl_feature_info *finfo, *p;
-	enum dfl_id_type type;
 	int ret, index = 0, res_idx = 0;
 
-	type = feature_dev_id_type(fdev);
 	if (WARN_ON_ONCE(type >= DFL_ID_MAX))
 		return ERR_PTR(-EINVAL);
 
@@ -773,6 +763,7 @@ binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 		return ERR_PTR(-ENOMEM);
 
 	pdata->dev = fdev;
+	pdata->type = type;
 	pdata->num = binfo->feature_num;
 	pdata->dfl_cdev = binfo->cdev;
 	pdata->id = FEATURE_DEV_ID_UNUSED;
@@ -861,14 +852,11 @@ binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 }
 
 static int
-build_info_create_dev(struct build_feature_devs_info *binfo,
-		      enum dfl_id_type type)
+build_info_create_dev(struct build_feature_devs_info *binfo)
 {
+	enum dfl_id_type type = binfo->type;
 	struct platform_device *fdev;
 
-	if (type >= DFL_ID_MAX)
-		return -EINVAL;
-
 	/*
 	 * we use -ENODEV as the initialization indicator which indicates
 	 * whether the id need to be reclaimed
@@ -905,7 +893,7 @@ static int build_info_commit_dev(struct build_feature_devs_info *binfo)
 	if (ret)
 		return ret;
 
-	if (feature_dev_id_type(binfo->feature_dev) == PORT_ID)
+	if (binfo->type == PORT_ID)
 		dfl_fpga_cdev_add_port_data(binfo->cdev, pdata);
 	else
 		binfo->cdev->fme_dev = get_device(&binfo->feature_dev->dev);
@@ -919,6 +907,9 @@ static int build_info_commit_dev(struct build_feature_devs_info *binfo)
 	 */
 	binfo->feature_dev = NULL;
 
+	/* reset the binfo for next FIU */
+	binfo->type = DFL_ID_MAX;
+
 	return 0;
 }
 
@@ -931,8 +922,7 @@ static void build_info_free(struct build_feature_devs_info *binfo)
 	 * build_info_create_dev()
 	 */
 	if (binfo->feature_dev && binfo->feature_dev->id >= 0) {
-		dfl_id_free(feature_dev_id_type(binfo->feature_dev),
-			    binfo->feature_dev->id);
+		dfl_id_free(binfo->type, binfo->feature_dev->id);
 
 		list_for_each_entry_safe(finfo, p, &binfo->sub_features, node) {
 			list_del(&finfo->node);
@@ -1030,7 +1020,7 @@ static int parse_feature_irqs(struct build_feature_devs_info *binfo,
 		 * Instead, features with interrupt functionality provide
 		 * the information in feature specific registers.
 		 */
-		type = feature_dev_id_type(binfo->feature_dev);
+		type = binfo->type;
 		if (type == PORT_ID) {
 			switch (fid) {
 			case PORT_FEATURE_ID_UINT:
@@ -1222,7 +1212,7 @@ static int parse_feature_port_afu(struct build_feature_devs_info *binfo,
 	return create_feature_instance(binfo, ofst, size, FEATURE_ID_AFU);
 }
 
-#define is_feature_dev_detected(binfo) (!!(binfo)->feature_dev)
+#define is_feature_dev_detected(binfo) ((binfo)->type != DFL_ID_MAX)
 
 static int parse_feature_afu(struct build_feature_devs_info *binfo,
 			     resource_size_t ofst)
@@ -1232,7 +1222,7 @@ static int parse_feature_afu(struct build_feature_devs_info *binfo,
 		return -EINVAL;
 	}
 
-	switch (feature_dev_id_type(binfo->feature_dev)) {
+	switch (binfo->type) {
 	case PORT_ID:
 		return parse_feature_port_afu(binfo, ofst);
 	default:
@@ -1278,6 +1268,7 @@ static void build_info_complete(struct build_feature_devs_info *binfo)
 static int parse_feature_fiu(struct build_feature_devs_info *binfo,
 			     resource_size_t ofst)
 {
+	enum dfl_id_type type;
 	int ret = 0;
 	u32 offset;
 	u16 id;
@@ -1299,8 +1290,14 @@ static int parse_feature_fiu(struct build_feature_devs_info *binfo,
 	v = readq(binfo->ioaddr + DFH);
 	id = FIELD_GET(DFH_ID, v);
 
+	type = dfh_id_to_type(id);
+	if (type >= DFL_ID_MAX)
+		return -EINVAL;
+
+	binfo->type = type;
+
 	/* create platform device for dfl feature dev */
-	ret = build_info_create_dev(binfo, dfh_id_to_type(id));
+	ret = build_info_create_dev(binfo);
 	if (ret)
 		return ret;
 
@@ -1520,13 +1517,13 @@ EXPORT_SYMBOL_GPL(dfl_fpga_enum_info_add_irq);
 
 static int remove_feature_dev(struct device *dev, void *data)
 {
+	struct dfl_feature_platform_data *pdata = dev_get_platdata(dev);
 	struct platform_device *pdev = to_platform_device(dev);
-	enum dfl_id_type type = feature_dev_id_type(pdev);
 	int id = pdev->id;
 
 	platform_device_unregister(pdev);
 
-	dfl_id_free(type, id);
+	dfl_id_free(pdata->type, id);
 
 	return 0;
 }
@@ -1578,6 +1575,7 @@ dfl_fpga_feature_devs_enumerate(struct dfl_fpga_enum_info *info)
 		goto unregister_region_exit;
 	}
 
+	binfo->type = DFL_ID_MAX;
 	binfo->dev = info->dev;
 	binfo->cdev = cdev;
 
@@ -1630,8 +1628,7 @@ void dfl_fpga_feature_devs_remove(struct dfl_fpga_cdev *cdev)
 
 		/* remove released ports */
 		if (!device_is_registered(&port_dev->dev)) {
-			dfl_id_free(feature_dev_id_type(port_dev),
-				    port_dev->id);
+			dfl_id_free(pdata->type, port_dev->id);
 			platform_device_put(port_dev);
 		}
 
drivers/fpga/dfl.h +3 -0
index 8ef9f33e22c1..d3a8a8ef908b 100644
@@ -17,6 +17,7 @@
 #include <linux/bitfield.h>
 #include <linux/cdev.h>
 #include <linux/delay.h>
+#include <linux/dfl.h>
 #include <linux/eventfd.h>
 #include <linux/fs.h>
 #include <linux/interrupt.h>
@@ -308,6 +309,7 @@ struct dfl_feature {
  * @lock: mutex to protect platform data.
  * @cdev: cdev of feature dev.
  * @dev: ptr to platform device linked with this platform data.
+ * @type: type of DFL FIU for the feature dev. See enum dfl_id_type.
  * @dfl_cdev: ptr to container device.
  * @id: id used for this feature device.
  * @disable_count: count for port disable.
@@ -322,6 +324,7 @@ struct dfl_feature_platform_data {
 	struct mutex lock;
 	struct cdev cdev;
 	struct platform_device *dev;
+	enum dfl_id_type type;
 	struct dfl_fpga_cdev *dfl_cdev;
 	int id;
 	unsigned int disable_count;
Signature · 1 line
2.46.1
#14 lore raw

Re: [PATCH v3 7/9] fpga: dfl: store FIU type in feature platform data

Quoted 59 lines · Peter Colberg

Remove the local function feature_dev_id_type() in favour of persisting the FIU type in struct dfl_feature_platform_data. Add type to struct build_feature_devs_info and drop argument to build_info_create_dev().

---
 drivers/fpga/dfl.c | 57 ++++++++++++++++++++++------------------------
 drivers/fpga/dfl.h |  3 +++
 2 files changed, 30 insertions(+), 30 deletions(-)
drivers/fpga/dfl.c +3 -12
index e644eb9fde39..9610ef1ec2ff 100644
@@ -119,17 +119,6 @@ static void dfl_id_free(enum dfl_id_type type, int id)
 	mutex_unlock(&dfl_id_mutex);
 }
 
-static enum dfl_id_type feature_dev_id_type(struct platform_device *pdev)
-{
-	int i;
-
-	for (i = 0; i < ARRAY_SIZE(dfl_devs); i++)
-		if (!strcmp(dfl_devs[i].name, pdev->name))
-			return i;
-
-	return DFL_ID_MAX;
-}
-
 static enum dfl_id_type dfh_id_to_type(u16 id)
 {
 	int i;
@@ -379,7 +368,7 @@ dfl_dev_add(struct dfl_feature_platform_data *pdata,
 	if (ret)
 		goto put_dev;
 
-	ddev->type = feature_dev_id_type(pdev);
+	ddev->type = pdata->type;
 	ddev->feature_id = feature->id;
 	ddev->revision = feature->revision;
 	ddev->dfh_version = feature->dfh_version;
@@ -693,6 +682,7 @@ EXPORT_SYMBOL_GPL(dfl_fpga_dev_ops_unregister);
  * @irq_table: Linux IRQ numbers for all irqs, indexed by local irq index of
  *	       this device.
  * @feature_dev: current feature device.
+ * @type: the current FIU type.
  * @ioaddr: header register region address of current FIU in enumeration.
  * @start: register resource start of current FIU.
  * @len: max register resource length of current FIU.
@@ -706,6 +696,7 @@ struct build_feature_devs_info {
 	int *irq_table;
 
 	struct platform_device *feature_dev;
+	enum dfl_id_type type;
 	void __iomem *ioaddr;
 	resource_size_t start;
 	resource_size_t len;

drivers/fpga/dfl.c static void dfl_fpga_cdev_add_port_data(struct dfl_fpga_cdev *cdev, · line 758

Quoted 8 lines
drivers/fpga/dfl.c +1 -1
@@ -752,13 +743,12 @@ static void dfl_fpga_cdev_add_port_data(struct dfl_fpga_cdev *cdev,
 static struct dfl_feature_platform_data *
 binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 {
+	enum dfl_id_type type = binfo->type;
 	struct platform_device *fdev = binfo->feature_dev;
 	struct dfl_feature_platform_data *pdata;
 	struct dfl_feature_info *finfo, *p;
-	enum dfl_id_type type;

Move this line up breaks the reverse Xmas tree, seems no need.

Quoted 70 lines
drivers/fpga/dfl.c +9 -10
 	int ret, index = 0, res_idx = 0;
 
-	type = feature_dev_id_type(fdev);
 	if (WARN_ON_ONCE(type >= DFL_ID_MAX))
 		return ERR_PTR(-EINVAL);
 
@@ -773,6 +763,7 @@ binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 		return ERR_PTR(-ENOMEM);
 
 	pdata->dev = fdev;
+	pdata->type = type;
 	pdata->num = binfo->feature_num;
 	pdata->dfl_cdev = binfo->cdev;
 	pdata->id = FEATURE_DEV_ID_UNUSED;
@@ -861,14 +852,11 @@ binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 }
 
 static int
-build_info_create_dev(struct build_feature_devs_info *binfo,
-		      enum dfl_id_type type)
+build_info_create_dev(struct build_feature_devs_info *binfo)
 {
+	enum dfl_id_type type = binfo->type;
 	struct platform_device *fdev;
 
-	if (type >= DFL_ID_MAX)
-		return -EINVAL;
-
 	/*
 	 * we use -ENODEV as the initialization indicator which indicates
 	 * whether the id need to be reclaimed
@@ -905,7 +893,7 @@ static int build_info_commit_dev(struct build_feature_devs_info *binfo)
 	if (ret)
 		return ret;
 
-	if (feature_dev_id_type(binfo->feature_dev) == PORT_ID)
+	if (binfo->type == PORT_ID)
 		dfl_fpga_cdev_add_port_data(binfo->cdev, pdata);
 	else
 		binfo->cdev->fme_dev = get_device(&binfo->feature_dev->dev);
@@ -919,6 +907,9 @@ static int build_info_commit_dev(struct build_feature_devs_info *binfo)
 	 */
 	binfo->feature_dev = NULL;
 
+	/* reset the binfo for next FIU */
+	binfo->type = DFL_ID_MAX;
+
 	return 0;
 }
 
@@ -931,8 +922,7 @@ static void build_info_free(struct build_feature_devs_info *binfo)
 	 * build_info_create_dev()
 	 */
 	if (binfo->feature_dev && binfo->feature_dev->id >= 0) {
-		dfl_id_free(feature_dev_id_type(binfo->feature_dev),
-			    binfo->feature_dev->id);
+		dfl_id_free(binfo->type, binfo->feature_dev->id);
 
 		list_for_each_entry_safe(finfo, p, &binfo->sub_features, node) {
 			list_del(&finfo->node);
@@ -1030,7 +1020,7 @@ static int parse_feature_irqs(struct build_feature_devs_info *binfo,
 		 * Instead, features with interrupt functionality provide
 		 * the information in feature specific registers.
 		 */
-		type = feature_dev_id_type(binfo->feature_dev);
+		type = binfo->type;
 		if (type == PORT_ID) {
 			switch (fid) {
 			case PORT_FEATURE_ID_UINT:

drivers/fpga/dfl.c static int parse_feature_port_afu(struct build_feature_devs_info *binfo, · line 1215

Quoted 5 lines
drivers/fpga/dfl.c +1 -1
@@ -1222,7 +1212,7 @@ static int parse_feature_port_afu(struct build_feature_devs_info *binfo,
 	return create_feature_instance(binfo, ofst, size, FEATURE_ID_AFU);
 }
 
-#define is_feature_dev_detected(binfo) (!!(binfo)->feature_dev)
+#define is_feature_dev_detected(binfo) ((binfo)->type != DFL_ID_MAX)

Seems too early to make this change. The 2 fields don't always align in this phase ..

Quoted 23 lines
drivers/fpga/dfl.c +2 -1
 static int parse_feature_afu(struct build_feature_devs_info *binfo,
 			     resource_size_t ofst)
@@ -1232,7 +1222,7 @@ static int parse_feature_afu(struct build_feature_devs_info *binfo,
 		return -EINVAL;
 	}
 
-	switch (feature_dev_id_type(binfo->feature_dev)) {
+	switch (binfo->type) {
 	case PORT_ID:
 		return parse_feature_port_afu(binfo, ofst);
 	default:
@@ -1278,6 +1268,7 @@ static void build_info_complete(struct build_feature_devs_info *binfo)
 static int parse_feature_fiu(struct build_feature_devs_info *binfo,
 			     resource_size_t ofst)
 {
+	enum dfl_id_type type;
 	int ret = 0;
 	u32 offset;
 	u16 id;
@@ -1299,8 +1290,14 @@ static int parse_feature_fiu(struct build_feature_devs_info *binfo,
 	v = readq(binfo->ioaddr + DFH);
 	id = FIELD_GET(DFH_ID, v);

drivers/fpga/dfl.c static int parse_feature_fiu(struct build_feature_devs_info *binfo, · line 1300

Quoted 10 lines
drivers/fpga/dfl.c +7 -1
 
+	type = dfh_id_to_type(id);
+	if (type >= DFL_ID_MAX)
+		return -EINVAL;
+
+	binfo->type = type;
+
 	/* create platform device for dfl feature dev */
-	ret = build_info_create_dev(binfo, dfh_id_to_type(id));
+	ret = build_info_create_dev(binfo);

.. like here, binfo->type is valid but binfo->feature_dev may not.

Thanks,
Yilun

#15 lore raw

Re: [PATCH v3 7/9] fpga: dfl: store FIU type in feature platform data

Quoted 54 lines · Xu Yilun
Quoted 53 lines · Peter Colberg

Remove the local function feature_dev_id_type() in favour of persisting the FIU type in struct dfl_feature_platform_data. Add type to struct build_feature_devs_info and drop argument to build_info_create_dev().

---
 drivers/fpga/dfl.c | 57 ++++++++++++++++++++++------------------------
 drivers/fpga/dfl.h |  3 +++
 2 files changed, 30 insertions(+), 30 deletions(-)
drivers/fpga/dfl.c +2 -12
index e644eb9fde39..9610ef1ec2ff 100644
@@ -119,17 +119,6 @@ static void dfl_id_free(enum dfl_id_type type, int id)
 	mutex_unlock(&dfl_id_mutex);
 }
 
-static enum dfl_id_type feature_dev_id_type(struct platform_device *pdev)
-{
-	int i;
-
-	for (i = 0; i < ARRAY_SIZE(dfl_devs); i++)
-		if (!strcmp(dfl_devs[i].name, pdev->name))
-			return i;
-
-	return DFL_ID_MAX;
-}
-
 static enum dfl_id_type dfh_id_to_type(u16 id)
 {
 	int i;
@@ -379,7 +368,7 @@ dfl_dev_add(struct dfl_feature_platform_data *pdata,
 	if (ret)
 		goto put_dev;
 
-	ddev->type = feature_dev_id_type(pdev);
+	ddev->type = pdata->type;
 	ddev->feature_id = feature->id;
 	ddev->revision = feature->revision;
 	ddev->dfh_version = feature->dfh_version;
@@ -693,6 +682,7 @@ EXPORT_SYMBOL_GPL(dfl_fpga_dev_ops_unregister);
  * @irq_table: Linux IRQ numbers for all irqs, indexed by local irq index of
  *	       this device.
  * @feature_dev: current feature device.
+ * @type: the current FIU type.
  * @ioaddr: header register region address of current FIU in enumeration.
  * @start: register resource start of current FIU.
  * @len: max register resource length of current FIU.
@@ -706,6 +696,7 @@ struct build_feature_devs_info {
 	int *irq_table;
 
Quoted 16 lines
Quoted 6 lines
drivers/fpga/dfl.c +1 -0
 	struct platform_device *feature_dev;
+	enum dfl_id_type type;
 	void __iomem *ioaddr;
 	resource_size_t start;
 	resource_size_t len;

drivers/fpga/dfl.c static void dfl_fpga_cdev_add_port_data(struct dfl_fpga_cdev *cdev, · line 758

Quoted 8 lines
drivers/fpga/dfl.c +1 -1
@@ -752,13 +743,12 @@ static void dfl_fpga_cdev_add_port_data(struct dfl_fpga_cdev *cdev,
 static struct dfl_feature_platform_data *
 binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 {
+	enum dfl_id_type type = binfo->type;
 	struct platform_device *fdev = binfo->feature_dev;
 	struct dfl_feature_platform_data *pdata;
 	struct dfl_feature_info *finfo, *p;
-	enum dfl_id_type type;

Move this line up breaks the reverse Xmas tree, seems no need.

This has been done in the revised patch "fpga: dfl: store FIU type in feature platform data".

Quoted 116 lines
Quoted 71 lines
drivers/fpga/dfl.c +9 -10
 	int ret, index = 0, res_idx = 0;
 
-	type = feature_dev_id_type(fdev);
 	if (WARN_ON_ONCE(type >= DFL_ID_MAX))
 		return ERR_PTR(-EINVAL);
 
@@ -773,6 +763,7 @@ binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 		return ERR_PTR(-ENOMEM);
 
 	pdata->dev = fdev;
+	pdata->type = type;
 	pdata->num = binfo->feature_num;
 	pdata->dfl_cdev = binfo->cdev;
 	pdata->id = FEATURE_DEV_ID_UNUSED;
@@ -861,14 +852,11 @@ binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 }
 
 static int
-build_info_create_dev(struct build_feature_devs_info *binfo,
-		      enum dfl_id_type type)
+build_info_create_dev(struct build_feature_devs_info *binfo)
 {
+	enum dfl_id_type type = binfo->type;
 	struct platform_device *fdev;
 
-	if (type >= DFL_ID_MAX)
-		return -EINVAL;
-
 	/*
 	 * we use -ENODEV as the initialization indicator which indicates
 	 * whether the id need to be reclaimed
@@ -905,7 +893,7 @@ static int build_info_commit_dev(struct build_feature_devs_info *binfo)
 	if (ret)
 		return ret;
 
-	if (feature_dev_id_type(binfo->feature_dev) == PORT_ID)
+	if (binfo->type == PORT_ID)
 		dfl_fpga_cdev_add_port_data(binfo->cdev, pdata);
 	else
 		binfo->cdev->fme_dev = get_device(&binfo->feature_dev->dev);
@@ -919,6 +907,9 @@ static int build_info_commit_dev(struct build_feature_devs_info *binfo)
 	 */
 	binfo->feature_dev = NULL;
 
+	/* reset the binfo for next FIU */
+	binfo->type = DFL_ID_MAX;
+
 	return 0;
 }
 
@@ -931,8 +922,7 @@ static void build_info_free(struct build_feature_devs_info *binfo)
 	 * build_info_create_dev()
 	 */
 	if (binfo->feature_dev && binfo->feature_dev->id >= 0) {
-		dfl_id_free(feature_dev_id_type(binfo->feature_dev),
-			    binfo->feature_dev->id);
+		dfl_id_free(binfo->type, binfo->feature_dev->id);
 
 		list_for_each_entry_safe(finfo, p, &binfo->sub_features, node) {
 			list_del(&finfo->node);
@@ -1030,7 +1020,7 @@ static int parse_feature_irqs(struct build_feature_devs_info *binfo,
 		 * Instead, features with interrupt functionality provide
 		 * the information in feature specific registers.
 		 */
-		type = feature_dev_id_type(binfo->feature_dev);
+		type = binfo->type;
 		if (type == PORT_ID) {
 			switch (fid) {
 			case PORT_FEATURE_ID_UINT:

drivers/fpga/dfl.c static int parse_feature_port_afu(struct build_feature_devs_info *binfo, · line 1215

Quoted 5 lines
drivers/fpga/dfl.c +1 -1
@@ -1222,7 +1212,7 @@ static int parse_feature_port_afu(struct build_feature_devs_info *binfo,
 	return create_feature_instance(binfo, ofst, size, FEATURE_ID_AFU);
 }
 
-#define is_feature_dev_detected(binfo) (!!(binfo)->feature_dev)
+#define is_feature_dev_detected(binfo) ((binfo)->type != DFL_ID_MAX)

Seems too early to make this change. The 2 fields don't always align in this phase ..

Quoted 23 lines
drivers/fpga/dfl.c +2 -1
 static int parse_feature_afu(struct build_feature_devs_info *binfo,
 			     resource_size_t ofst)
@@ -1232,7 +1222,7 @@ static int parse_feature_afu(struct build_feature_devs_info *binfo,
 		return -EINVAL;
 	}
 
-	switch (feature_dev_id_type(binfo->feature_dev)) {
+	switch (binfo->type) {
 	case PORT_ID:
 		return parse_feature_port_afu(binfo, ofst);
 	default:
@@ -1278,6 +1268,7 @@ static void build_info_complete(struct build_feature_devs_info *binfo)
 static int parse_feature_fiu(struct build_feature_devs_info *binfo,
 			     resource_size_t ofst)
 {
+	enum dfl_id_type type;
 	int ret = 0;
 	u32 offset;
 	u16 id;
@@ -1299,8 +1290,14 @@ static int parse_feature_fiu(struct build_feature_devs_info *binfo,
 	v = readq(binfo->ioaddr + DFH);
 	id = FIELD_GET(DFH_ID, v);

drivers/fpga/dfl.c static int parse_feature_fiu(struct build_feature_devs_info *binfo, · line 1300

Quoted 10 lines
drivers/fpga/dfl.c +7 -1
 
+	type = dfh_id_to_type(id);
+	if (type >= DFL_ID_MAX)
+		return -EINVAL;
+
+	binfo->type = type;
+
 	/* create platform device for dfl feature dev */
-	ret = build_info_create_dev(binfo, dfh_id_to_type(id));
+	ret = build_info_create_dev(binfo);

.. like here, binfo->type is valid but binfo->feature_dev may not.

Quoted 2 lines

Thanks,
Yilun

The conversion of is_feature_dev_detected() to use the FIU type has been moved to a new patch "fpga: dfl: convert is_feature_dev_detected() to use FIU type" later in the series.

Thanks,
Peter

#16 lore raw

PATCH v3 8/9 fpga: dfl: refactor functions to take/return feature device data

In reply to Peter Colberg

This change prepares a subsequent commit which factors out the DFL enumeration infos from the structure dfl_feature_platform_data into a new structure dfl_feature_dev_data, whose lifetime is independent of the feature device which may be destroyed during port release.

Define an alias dfl_feature_dev_data for dfl_feature_platform_data, and an alias to_dfl_feature_dev_data() for dev_get_platdata(), and refactor functions to take/return dfl_feature_dev_data instead. The aliases will be replaced with the actual implementations in the subsequent commit.

This change does not introduce any functional changes.

---
 drivers/fpga/dfl-afu-dma-region.c | 110 ++++++------
 drivers/fpga/dfl-afu-error.c      |  60 +++----
 drivers/fpga/dfl-afu-main.c       | 276 +++++++++++++++---------------
 drivers/fpga/dfl-afu-region.c     |  48 +++---
 drivers/fpga/dfl-afu.h            |  26 +--
 drivers/fpga/dfl-fme-br.c         |  22 +--
 drivers/fpga/dfl-fme-error.c      | 102 +++++------
 drivers/fpga/dfl-fme-main.c       |  98 +++++------
 drivers/fpga/dfl-fme-pr.c         |  84 ++++-----
 drivers/fpga/dfl.c                | 172 +++++++++----------
 drivers/fpga/dfl.h                |  77 +++++----
 11 files changed, 539 insertions(+), 536 deletions(-)
drivers/fpga/dfl-afu-dma-region.c +55 -55
index 814191e623e1..5aa7b8884374 100644
@@ -16,26 +16,26 @@
 
 #include "dfl-afu.h"
 
-void afu_dma_region_init(struct dfl_feature_platform_data *pdata)
+void afu_dma_region_init(struct dfl_feature_dev_data *fdata)
 {
-	struct dfl_afu *afu = dfl_fpga_pdata_get_private(pdata);
+	struct dfl_afu *afu = dfl_fpga_fdata_get_private(fdata);
 
 	afu->dma_regions = RB_ROOT;
 }
 
 /**
  * afu_dma_pin_pages - pin pages of given dma memory region
- * @pdata: feature device platform data
+ * @fdata: feature dev data
  * @region: dma memory region to be pinned
  *
  * Pin all the pages of given dfl_afu_dma_region.
  * Return 0 for success or negative error code.
  */
-static int afu_dma_pin_pages(struct dfl_feature_platform_data *pdata,
+static int afu_dma_pin_pages(struct dfl_feature_dev_data *fdata,
 			     struct dfl_afu_dma_region *region)
 {
 	int npages = region->length >> PAGE_SHIFT;
-	struct device *dev = &pdata->dev->dev;
+	struct device *dev = &fdata->dev->dev;
 	int ret, pinned;
 
 	ret = account_locked_vm(current->mm, npages, true);
@@ -73,17 +73,17 @@ static int afu_dma_pin_pages(struct dfl_feature_platform_data *pdata,
 
 /**
  * afu_dma_unpin_pages - unpin pages of given dma memory region
- * @pdata: feature device platform data
+ * @fdata: feature dev data
  * @region: dma memory region to be unpinned
  *
  * Unpin all the pages of given dfl_afu_dma_region.
  * Return 0 for success or negative error code.
  */
-static void afu_dma_unpin_pages(struct dfl_feature_platform_data *pdata,
+static void afu_dma_unpin_pages(struct dfl_feature_dev_data *fdata,
 				struct dfl_afu_dma_region *region)
 {
 	long npages = region->length >> PAGE_SHIFT;
-	struct device *dev = &pdata->dev->dev;
+	struct device *dev = &fdata->dev->dev;
 
 	unpin_user_pages(region->pages, npages);
 	kfree(region->pages);
@@ -133,20 +133,20 @@ static bool dma_region_check_iova(struct dfl_afu_dma_region *region,
 
 /**
  * afu_dma_region_add - add given dma region to rbtree
- * @pdata: feature device platform data
+ * @fdata: feature dev data
  * @region: dma region to be added
  *
  * Return 0 for success, -EEXIST if dma region has already been added.
  *
- * Needs to be called with pdata->lock heold.
+ * Needs to be called with fdata->lock held.
  */
-static int afu_dma_region_add(struct dfl_feature_platform_data *pdata,
+static int afu_dma_region_add(struct dfl_feature_dev_data *fdata,
 			      struct dfl_afu_dma_region *region)
 {
-	struct dfl_afu *afu = dfl_fpga_pdata_get_private(pdata);
+	struct dfl_afu *afu = dfl_fpga_fdata_get_private(fdata);
 	struct rb_node **new, *parent = NULL;
 
-	dev_dbg(&pdata->dev->dev, "add region (iova = %llx)\n",
+	dev_dbg(&fdata->dev->dev, "add region (iova = %llx)\n",
 		(unsigned long long)region->iova);
 
 	new = &afu->dma_regions.rb_node;
@@ -177,50 +177,50 @@ static int afu_dma_region_add(struct dfl_feature_platform_data *pdata,
 
 /**
  * afu_dma_region_remove - remove given dma region from rbtree
- * @pdata: feature device platform data
+ * @fdata: feature dev data
  * @region: dma region to be removed
  *
- * Needs to be called with pdata->lock heold.
+ * Needs to be called with fdata->lock held.
  */
-static void afu_dma_region_remove(struct dfl_feature_platform_data *pdata,
+static void afu_dma_region_remove(struct dfl_feature_dev_data *fdata,
 				  struct dfl_afu_dma_region *region)
 {
 	struct dfl_afu *afu;
 
-	dev_dbg(&pdata->dev->dev, "del region (iova = %llx)\n",
+	dev_dbg(&fdata->dev->dev, "del region (iova = %llx)\n",
 		(unsigned long long)region->iova);
 
-	afu = dfl_fpga_pdata_get_private(pdata);
+	afu = dfl_fpga_fdata_get_private(fdata);
 	rb_erase(&region->node, &afu->dma_regions);
 }
 
 /**
  * afu_dma_region_destroy - destroy all regions in rbtree
- * @pdata: feature device platform data
+ * @fdata: feature dev data
  *
- * Needs to be called with pdata->lock heold.
+ * Needs to be called with fdata->lock held.
  */
-void afu_dma_region_destroy(struct dfl_feature_platform_data *pdata)
+void afu_dma_region_destroy(struct dfl_feature_dev_data *fdata)
 {
-	struct dfl_afu *afu = dfl_fpga_pdata_get_private(pdata);
+	struct dfl_afu *afu = dfl_fpga_fdata_get_private(fdata);
 	struct rb_node *node = rb_first(&afu->dma_regions);
 	struct dfl_afu_dma_region *region;
 
 	while (node) {
 		region = container_of(node, struct dfl_afu_dma_region, node);
 
-		dev_dbg(&pdata->dev->dev, "del region (iova = %llx)\n",
+		dev_dbg(&fdata->dev->dev, "del region (iova = %llx)\n",
 			(unsigned long long)region->iova);
 
 		rb_erase(node, &afu->dma_regions);
 
 		if (region->iova)
-			dma_unmap_page(dfl_fpga_pdata_to_parent(pdata),
+			dma_unmap_page(dfl_fpga_fdata_to_parent(fdata),
 				       region->iova, region->length,
 				       DMA_BIDIRECTIONAL);
 
 		if (region->pages)
-			afu_dma_unpin_pages(pdata, region);
+			afu_dma_unpin_pages(fdata, region);
 
 		node = rb_next(node);
 		kfree(region);
@@ -229,7 +229,7 @@ void afu_dma_region_destroy(struct dfl_feature_platform_data *pdata)
 
 /**
  * afu_dma_region_find - find the dma region from rbtree based on iova and size
- * @pdata: feature device platform data
+ * @fdata: feature dev data
  * @iova: address of the dma memory area
  * @size: size of the dma memory area
  *
@@ -239,14 +239,14 @@ void afu_dma_region_destroy(struct dfl_feature_platform_data *pdata)
  *   [@iova, @iova+size)
  * If nothing is matched returns NULL.
  *
- * Needs to be called with pdata->lock held.
+ * Needs to be called with fdata->lock held.
  */
 struct dfl_afu_dma_region *
-afu_dma_region_find(struct dfl_feature_platform_data *pdata, u64 iova, u64 size)
+afu_dma_region_find(struct dfl_feature_dev_data *fdata, u64 iova, u64 size)
 {
-	struct dfl_afu *afu = dfl_fpga_pdata_get_private(pdata);
+	struct dfl_afu *afu = dfl_fpga_fdata_get_private(fdata);
 	struct rb_node *node = afu->dma_regions.rb_node;
-	struct device *dev = &pdata->dev->dev;
+	struct device *dev = &fdata->dev->dev;
 
 	while (node) {
 		struct dfl_afu_dma_region *region;
@@ -276,20 +276,20 @@ afu_dma_region_find(struct dfl_feature_platform_data *pdata, u64 iova, u64 size)
 
 /**
  * afu_dma_region_find_iova - find the dma region from rbtree by iova
- * @pdata: feature device platform data
+ * @fdata: feature dev data
  * @iova: address of the dma region
  *
- * Needs to be called with pdata->lock held.
+ * Needs to be called with fdata->lock held.
  */
 static struct dfl_afu_dma_region *
-afu_dma_region_find_iova(struct dfl_feature_platform_data *pdata, u64 iova)
+afu_dma_region_find_iova(struct dfl_feature_dev_data *fdata, u64 iova)
 {
-	return afu_dma_region_find(pdata, iova, 0);
+	return afu_dma_region_find(fdata, iova, 0);
 }
 
 /**
  * afu_dma_map_region - map memory region for dma
- * @pdata: feature device platform data
+ * @fdata: feature dev data
  * @user_addr: address of the memory region
  * @length: size of the memory region
  * @iova: pointer of iova address
@@ -298,10 +298,10 @@ afu_dma_region_find_iova(struct dfl_feature_platform_data *pdata, u64 iova)
  * of the memory region via @iova.
  * Return 0 for success, otherwise error code.
  */
-int afu_dma_map_region(struct dfl_feature_platform_data *pdata,
+int afu_dma_map_region(struct dfl_feature_dev_data *fdata,
 		       u64 user_addr, u64 length, u64 *iova)
 {
-	struct device *dev = &pdata->dev->dev;
+	struct device *dev = &fdata->dev->dev;
 	struct dfl_afu_dma_region *region;
 	int ret;
 
@@ -324,7 +324,7 @@ int afu_dma_map_region(struct dfl_feature_platform_data *pdata,
 	region->length = length;
 
 	/* Pin the user memory region */
-	ret = afu_dma_pin_pages(pdata, region);
+	ret = afu_dma_pin_pages(fdata, region);
 	if (ret) {
 		dev_err(dev, "failed to pin memory region\n");
 		goto free_region;
@@ -338,11 +338,11 @@ int afu_dma_map_region(struct dfl_feature_platform_data *pdata,
 	}
 
 	/* As pages are continuous then start to do DMA mapping */
-	region->iova = dma_map_page(dfl_fpga_pdata_to_parent(pdata),
+	region->iova = dma_map_page(dfl_fpga_fdata_to_parent(fdata),
 				    region->pages[0], 0,
 				    region->length,
 				    DMA_BIDIRECTIONAL);
-	if (dma_mapping_error(dfl_fpga_pdata_to_parent(pdata), region->iova)) {
+	if (dma_mapping_error(dfl_fpga_fdata_to_parent(fdata), region->iova)) {
 		dev_err(dev, "failed to map for dma\n");
 		ret = -EFAULT;
 		goto unpin_pages;
@@ -350,9 +350,9 @@ int afu_dma_map_region(struct dfl_feature_platform_data *pdata,
 
 	*iova = region->iova;
 
-	mutex_lock(&pdata->lock);
-	ret = afu_dma_region_add(pdata, region);
-	mutex_unlock(&pdata->lock);
+	mutex_lock(&fdata->lock);
+	ret = afu_dma_region_add(fdata, region);
+	mutex_unlock(&fdata->lock);
 	if (ret) {
 		dev_err(dev, "failed to add dma region\n");
 		goto unmap_dma;
@@ -361,10 +361,10 @@ int afu_dma_map_region(struct dfl_feature_platform_data *pdata,
 	return 0;
 
 unmap_dma:
-	dma_unmap_page(dfl_fpga_pdata_to_parent(pdata),
+	dma_unmap_page(dfl_fpga_fdata_to_parent(fdata),
 		       region->iova, region->length, DMA_BIDIRECTIONAL);
 unpin_pages:
-	afu_dma_unpin_pages(pdata, region);
+	afu_dma_unpin_pages(fdata, region);
 free_region:
 	kfree(region);
 	return ret;
@@ -372,34 +372,34 @@ int afu_dma_map_region(struct dfl_feature_platform_data *pdata,
 
 /**
  * afu_dma_unmap_region - unmap dma memory region
- * @pdata: feature device platform data
+ * @fdata: feature dev data
  * @iova: dma address of the region
  *
  * Unmap dma memory region based on @iova.
  * Return 0 for success, otherwise error code.
  */
-int afu_dma_unmap_region(struct dfl_feature_platform_data *pdata, u64 iova)
+int afu_dma_unmap_region(struct dfl_feature_dev_data *fdata, u64 iova)
 {
 	struct dfl_afu_dma_region *region;
 
-	mutex_lock(&pdata->lock);
-	region = afu_dma_region_find_iova(pdata, iova);
+	mutex_lock(&fdata->lock);
+	region = afu_dma_region_find_iova(fdata, iova);
 	if (!region) {
-		mutex_unlock(&pdata->lock);
+		mutex_unlock(&fdata->lock);
 		return -EINVAL;
 	}
 
 	if (region->in_use) {
-		mutex_unlock(&pdata->lock);
+		mutex_unlock(&fdata->lock);
 		return -EBUSY;
 	}
 
-	afu_dma_region_remove(pdata, region);
-	mutex_unlock(&pdata->lock);
+	afu_dma_region_remove(fdata, region);
+	mutex_unlock(&fdata->lock);
 
-	dma_unmap_page(dfl_fpga_pdata_to_parent(pdata),
+	dma_unmap_page(dfl_fpga_fdata_to_parent(fdata),
 		       region->iova, region->length, DMA_BIDIRECTIONAL);
-	afu_dma_unpin_pages(pdata, region);
+	afu_dma_unpin_pages(fdata, region);
 	kfree(region);
 
 	return 0;
drivers/fpga/dfl-afu-error.c +30 -30

152 lines not on this page · raw message

drivers/fpga/dfl-afu-main.c +138 -138

704 lines not on this page · raw message

drivers/fpga/dfl-afu-region.c +24 -24

141 lines not on this page · raw message

drivers/fpga/dfl-afu.h +13 -13
index 6d1e79240c70..03be4f0969c7 100644
@@ -76,27 +76,27 @@ struct dfl_afu {
 	struct rb_root dma_regions;
 };
 
-/* hold pdata->lock when call __afu_port_enable/disable */
-int __afu_port_enable(struct dfl_feature_platform_data *pdata);
-int __afu_port_disable(struct dfl_feature_platform_data *pdata);
+/* hold fdata->lock when call __afu_port_enable/disable */
+int __afu_port_enable(struct dfl_feature_dev_data *fdata);
+int __afu_port_disable(struct dfl_feature_dev_data *fdata);
 
-void afu_mmio_region_init(struct dfl_feature_platform_data *pdata);
-int afu_mmio_region_add(struct dfl_feature_platform_data *pdata,
+void afu_mmio_region_init(struct dfl_feature_dev_data *fdata);
+int afu_mmio_region_add(struct dfl_feature_dev_data *fdata,
 			u32 region_index, u64 region_size, u64 phys, u32 flags);
-void afu_mmio_region_destroy(struct dfl_feature_platform_data *pdata);
-int afu_mmio_region_get_by_index(struct dfl_feature_platform_data *pdata,
+void afu_mmio_region_destroy(struct dfl_feature_dev_data *fdata);
+int afu_mmio_region_get_by_index(struct dfl_feature_dev_data *fdata,
 				 u32 region_index,
 				 struct dfl_afu_mmio_region *pregion);
-int afu_mmio_region_get_by_offset(struct dfl_feature_platform_data *pdata,
+int afu_mmio_region_get_by_offset(struct dfl_feature_dev_data *fdata,
 				  u64 offset, u64 size,
 				  struct dfl_afu_mmio_region *pregion);
-void afu_dma_region_init(struct dfl_feature_platform_data *pdata);
-void afu_dma_region_destroy(struct dfl_feature_platform_data *pdata);
-int afu_dma_map_region(struct dfl_feature_platform_data *pdata,
+void afu_dma_region_init(struct dfl_feature_dev_data *fdata);
+void afu_dma_region_destroy(struct dfl_feature_dev_data *fdata);
+int afu_dma_map_region(struct dfl_feature_dev_data *fdata,
 		       u64 user_addr, u64 length, u64 *iova);
-int afu_dma_unmap_region(struct dfl_feature_platform_data *pdata, u64 iova);
+int afu_dma_unmap_region(struct dfl_feature_dev_data *fdata, u64 iova);
 struct dfl_afu_dma_region *
-afu_dma_region_find(struct dfl_feature_platform_data *pdata,
+afu_dma_region_find(struct dfl_feature_dev_data *fdata,
 		    u64 iova, u64 size);
 
 extern const struct dfl_feature_ops port_err_ops;
drivers/fpga/dfl-fme-br.c +11 -11
index c19ddb02a161..5c60a38ec76c 100644
@@ -22,34 +22,34 @@
 struct fme_br_priv {
 	struct dfl_fme_br_pdata *pdata;
 	struct dfl_fpga_port_ops *port_ops;
-	struct dfl_feature_platform_data *port_pdata;
+	struct dfl_feature_dev_data *port_fdata;
 };
 
 static int fme_bridge_enable_set(struct fpga_bridge *bridge, bool enable)
 {
-	struct dfl_feature_platform_data *port_pdata;
 	struct fme_br_priv *priv = bridge->priv;
+	struct dfl_feature_dev_data *port_fdata;
 	struct dfl_fpga_port_ops *ops;
 
-	if (!priv->port_pdata) {
-		port_pdata = dfl_fpga_cdev_find_port_data(priv->pdata->cdev,
+	if (!priv->port_fdata) {
+		port_fdata = dfl_fpga_cdev_find_port_data(priv->pdata->cdev,
 							  &priv->pdata->port_id,
 							  dfl_fpga_check_port_id);
-		if (!port_pdata)
+		if (!port_fdata)
 			return -ENODEV;
 
-		priv->port_pdata = port_pdata;
+		priv->port_fdata = port_fdata;
 	}
 
-	if (priv->port_pdata && !priv->port_ops) {
-		ops = dfl_fpga_port_ops_get(priv->port_pdata);
+	if (priv->port_fdata && !priv->port_ops) {
+		ops = dfl_fpga_port_ops_get(priv->port_fdata);
 		if (!ops || !ops->enable_set)
 			return -ENOENT;
 
 		priv->port_ops = ops;
 	}
 
-	return priv->port_ops->enable_set(priv->port_pdata, enable);
+	return priv->port_ops->enable_set(priv->port_fdata, enable);
 }
 
 static const struct fpga_bridge_ops fme_bridge_ops = {
@@ -85,8 +85,8 @@ static void fme_br_remove(struct platform_device *pdev)
 
 	fpga_bridge_unregister(br);
 
-	if (priv->port_pdata)
-		put_device(&priv->port_pdata->dev->dev);
+	if (priv->port_fdata)
+		put_device(&priv->port_fdata->dev->dev);
 	if (priv->port_ops)
 		dfl_fpga_port_ops_put(priv->port_ops);
 }
drivers/fpga/dfl-fme-error.c +51 -51

285 lines not on this page · raw message

drivers/fpga/dfl-fme-main.c +49 -49

263 lines not on this page · raw message

drivers/fpga/dfl-fme-pr.c +42 -42

229 lines not on this page · raw message

drivers/fpga/dfl.c +86 -86

440 lines not on this page · raw message

drivers/fpga/dfl.h +40 -37

169 lines not on this page · raw message

Signature · 1 line
2.46.1
#17 lore raw

PATCH v3 9/9 fpga: dfl: fix kernel warning on port release/assign for SRIOV

In reply to Peter Colberg

From: Xu Yilun <[email protected]>

With the Intel FPGA PAC D5005, DFL ports are registered as platform devices in PF mode. The port device must be removed from the host when the user wants to configure the port as a VF for use by a user-space driver, e.g., for pass-through to a virtual machine. The FME device ioctls DFL_FPGA_FME_PORT_RELEASE/ASSIGN are assigned for this purpose.

In the previous implementation, the port platform device is not completely destroyed on port release: it is removed from the system by platform_device_del(), but the platform device instance is retained. When DFL_FPGA_FME_PORT_ASSIGN is called, the platform device is added back with platform_device_add(), which conflicts with this comment of device_add(): "Do not call this routine more than once for any device structure", and would previously cause a kernel warning at runtime.

This patch completely unregisters the port platform device on release and registers a new device on assign. But the main work is to remove the dependency on struct dfl_feature_platform_data for many internal DFL APIs. This structure holds many DFL enumeration infos for feature devices. Many DFL APIs are expected to work with these infos even when the port platform device is unregistered. But after this change, the platform_data will be freed on port release. Hence this patch introduces a new structure dfl_feature_dev_data, which acts similarly to the previous dfl_feature_platform_data. dfl_feature_platform_data then only needs a pointer to dfl_feature_dev_data to query DFL enumeration infos.

---
 drivers/fpga/dfl-fme-br.c |   2 -
 drivers/fpga/dfl.c        | 207 ++++++++++++++++++--------------------
 drivers/fpga/dfl.h        |  57 +++++++----
 3 files changed, 133 insertions(+), 133 deletions(-)
drivers/fpga/dfl-fme-br.c +0 -2
index 5c60a38ec76c..a298a041877b 100644
@@ -85,8 +85,6 @@ static void fme_br_remove(struct platform_device *pdev)
 
 	fpga_bridge_unregister(br);
 
-	if (priv->port_fdata)
-		put_device(&priv->port_fdata->dev->dev);
 	if (priv->port_ops)
 		dfl_fpga_port_ops_put(priv->port_ops);
 }
drivers/fpga/dfl.c +96 -111

397 lines not on this page · raw message

drivers/fpga/dfl.h +37 -20

112 lines not on this page · raw message

Signature · 1 line
2.46.1
#18 lore raw
Quoted 2 lines · Peter Colberg

From: Xu Yilun <[email protected]>

Please describe what this patch does at the beginning. And below background descriptions follow.

Quoted 14 lines

With the Intel FPGA PAC D5005, DFL ports are registered as platform devices in PF mode. The port device must be removed from the host when the user wants to configure the port as a VF for use by a user-space driver, e.g., for pass-through to a virtual machine. The FME device ioctls DFL_FPGA_FME_PORT_RELEASE/ASSIGN are assigned for this purpose.

In the previous implementation, the port platform device is not completely destroyed on port release: it is removed from the system by platform_device_del(), but the platform device instance is retained. When DFL_FPGA_FME_PORT_ASSIGN is called, the platform device is added back with platform_device_add(), which conflicts with this comment of device_add(): "Do not call this routine more than once for any device structure", and would previously cause a kernel warning at runtime.

Quoted 2 lines

This patch completely unregisters the port platform device on release and registers a new device on assign. But the main work is to remove

The main work of this series, not this patch.

Quoted 24 lines

the dependency on struct dfl_feature_platform_data for many internal DFL APIs. This structure holds many DFL enumeration infos for feature devices. Many DFL APIs are expected to work with these infos even when the port platform device is unregistered. But after this change, the platform_data will be freed on port release. Hence this patch introduces a new structure dfl_feature_dev_data, which acts similarly to the previous dfl_feature_platform_data. dfl_feature_platform_data then only needs a pointer to dfl_feature_dev_data to query DFL enumeration infos.

---
 drivers/fpga/dfl-fme-br.c |   2 -
 drivers/fpga/dfl.c        | 207 ++++++++++++++++++--------------------
 drivers/fpga/dfl.h        |  57 +++++++----
 3 files changed, 133 insertions(+), 133 deletions(-)
drivers/fpga/dfl-fme-br.c +0 -0
index 5c60a38ec76c..a298a041877b 100644

drivers/fpga/dfl-fme-br.c static void fme_br_remove(struct platform_device *pdev) · line 89

Quoted 5 lines
drivers/fpga/dfl-fme-br.c +0 -2
@@ -85,8 +85,6 @@ static void fme_br_remove(struct platform_device *pdev)
 
 	fpga_bridge_unregister(br);
 
-	if (priv->port_fdata)
-		put_device(&priv->port_fdata->dev->dev);

I can't remember why all the get_device/put_device() are not needed anymore.

Quoted 172 lines
drivers/fpga/dfl-fme-br.c +0 -0
 	if (priv->port_ops)
 		dfl_fpga_port_ops_put(priv->port_ops);
 }
drivers/fpga/dfl.c +42 -33
index 52f58d029ca4..a77d7692b170 100644
@@ -160,7 +160,7 @@ struct dfl_fpga_port_ops *dfl_fpga_port_ops_get(struct dfl_feature_dev_data *fda
 
 	list_for_each_entry(ops, &dfl_port_ops_list, node) {
 		/* match port_ops using the name of platform device */
-		if (!strcmp(fdata->dev->name, ops->name)) {
+		if (!strcmp(fdata->pdev_name, ops->name)) {
 			if (!try_module_get(ops->owner))
 				ops = NULL;
 			goto done;
@@ -681,7 +681,6 @@ EXPORT_SYMBOL_GPL(dfl_fpga_dev_ops_unregister);
  * @nr_irqs: number of irqs for all feature devices.
  * @irq_table: Linux IRQ numbers for all irqs, indexed by local irq index of
  *	       this device.
- * @feature_dev: current feature device.
  * @type: the current FIU type.
  * @ioaddr: header register region address of current FIU in enumeration.
  * @start: register resource start of current FIU.
@@ -695,7 +694,6 @@ struct build_feature_devs_info {
 	unsigned int nr_irqs;
 	int *irq_table;
 
-	struct platform_device *feature_dev;
 	enum dfl_id_type type;
 	void __iomem *ioaddr;
 	resource_size_t start;
@@ -736,7 +734,6 @@ static void dfl_fpga_cdev_add_port_data(struct dfl_fpga_cdev *cdev,
 {
 	mutex_lock(&cdev->lock);
 	list_add(&fdata->node, &cdev->port_dev_list);
-	get_device(&fdata->dev->dev);
 	mutex_unlock(&cdev->lock);
 }
 
@@ -744,7 +741,6 @@ static struct dfl_feature_dev_data *
 binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 {
 	enum dfl_id_type type = binfo->type;
-	struct platform_device *fdev = binfo->feature_dev;
 	struct dfl_feature_info *finfo, *p;
 	struct dfl_feature_dev_data *fdata;
 	int ret, index = 0, res_idx = 0;
@@ -752,18 +748,27 @@ binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 	if (WARN_ON_ONCE(type >= DFL_ID_MAX))
 		return ERR_PTR(-EINVAL);
 
-	/*
-	 * we do not need to care for the memory which is associated with
-	 * the platform device. After calling platform_device_unregister(),
-	 * it will be automatically freed by device's release() callback,
-	 * platform_device_release().
-	 */
-	fdata = kzalloc(struct_size(fdata, features, binfo->feature_num), GFP_KERNEL);
+	fdata = devm_kzalloc(binfo->dev, sizeof(*fdata), GFP_KERNEL);
 	if (!fdata)
 		return ERR_PTR(-ENOMEM);
 
-	fdata->dev = fdev;
+	fdata->features = devm_kcalloc(binfo->dev, binfo->feature_num,
+				       sizeof(*fdata->features), GFP_KERNEL);
+	if (!fdata->features)
+		return ERR_PTR(-ENOMEM);
+
+	fdata->resources = devm_kcalloc(binfo->dev, binfo->feature_num,
+					sizeof(*fdata->resources), GFP_KERNEL);
+	if (!fdata->resources)
+		return ERR_PTR(-ENOMEM);
+
 	fdata->type = type;
+
+	fdata->pdev_id = dfl_id_alloc(type, binfo->dev);
+	if (fdata->pdev_id < 0)
+		return ERR_PTR(fdata->pdev_id);
+
+	fdata->pdev_name = dfl_devs[type].name;
 	fdata->num = binfo->feature_num;
 	fdata->dfl_cdev = binfo->cdev;
 	fdata->id = FEATURE_DEV_ID_UNUSED;
@@ -779,15 +784,6 @@ binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 	 */
 	WARN_ON(fdata->disable_count);
 
-	fdev->dev.platform_data = fdata;
-
-	/* each sub feature has one MMIO resource */
-	fdev->num_resources = binfo->feature_num;
-	fdev->resource = kcalloc(binfo->feature_num, sizeof(*fdev->resource),
-				 GFP_KERNEL);
-	if (!fdev->resource)
-		return ERR_PTR(-ENOMEM);
-
 	/* fill features and resource information for feature dev */
 	list_for_each_entry_safe(finfo, p, &binfo->sub_features, node) {
 		struct dfl_feature *feature = &fdata->features[index++];
@@ -795,7 +791,6 @@ binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 		unsigned int i;
 
 		/* save resource information for each feature */
-		feature->dev = fdev;
 		feature->id = finfo->fid;
 		feature->revision = finfo->revision;
 		feature->dfh_version = finfo->dfh_version;
@@ -804,8 +799,10 @@ binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 			feature->params = devm_kmemdup(binfo->dev,
 						       finfo->params, finfo->param_size,
 						       GFP_KERNEL);
-			if (!feature->params)
-				return ERR_PTR(-ENOMEM);
+			if (!feature->params) {
+				ret = -ENOMEM;
+				goto err_free_id;
+			}
 
 			feature->param_size = finfo->param_size;
 		}
@@ -823,19 +820,20 @@ binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 						      &finfo->mmio_res);
 			if (IS_ERR(feature->ioaddr)) {
 				ret = PTR_ERR(feature->ioaddr);
-				return ERR_PTR(ret);
+				goto err_free_id;
 			}
 		} else {
 			feature->resource_index = res_idx;
-			fdev->resource[res_idx++] = finfo->mmio_res;
+			fdata->resources[res_idx++] = finfo->mmio_res;
 		}
 
 		if (finfo->nr_irqs) {
 			ctx = devm_kcalloc(binfo->dev, finfo->nr_irqs,
 					   sizeof(*ctx), GFP_KERNEL);
-			if (!ctx)
-				return ERR_PTR(-ENOMEM);
-
+			if (!ctx) {
+				ret = -ENOMEM;
+				goto err_free_id;
+			}
 			for (i = 0; i < finfo->nr_irqs; i++)
 				ctx[i].irq =
 					binfo->irq_table[finfo->irq_base + i];
@@ -848,36 +846,67 @@ binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 		kfree(finfo);
 	}
 
+	fdata->resource_num = res_idx;
+
 	return fdata;
+
+err_free_id:
+	dfl_id_free(type, fdata->pdev_id);
+
+	return ERR_PTR(ret);
 }
 
-static int
-build_info_create_dev(struct build_feature_devs_info *binfo)
+/*
+ * register current feature device, it is called when we need to switch to
+ * another feature parsing or we have parsed all features on given device
+ * feature list.
+ */
+static int feature_dev_register(struct dfl_feature_dev_data *fdata)
 {
-	enum dfl_id_type type = binfo->type;
+	struct dfl_feature_platform_data pdata = {};

drivers/fpga/dfl.c binfo_create_feature_dev_data(struct build_feature_devs_info *binfo) · line 871

Quoted 10 lines
drivers/fpga/dfl.c +3 -5
 	struct platform_device *fdev;
+	struct dfl_feature *feature;
+	int ret;
 
-	/*
-	 * we use -ENODEV as the initialization indicator which indicates
-	 * whether the id need to be reclaimed
-	 */
-	fdev = platform_device_alloc(dfl_devs[type].name, -ENODEV);
+	fdev = platform_device_alloc(fdata->pdev_name, fdata->pdev_id);

I see a part of the change is to delay the platform_device_alloc() so that the feature platform device registration work could be gathered in one function. Not sure if it is necessary for the port platform release issue, or could be an independent enhancement patch.

I raise this cause I still feel hard to understand all these changes in this patch and look for any chance to further split it.

BTW: Overall this series is much improved than the last version.

Thanks,
Yilun

#19 lore raw
Quoted 6 lines · Xu Yilun
Quoted 2 lines · Peter Colberg

From: Xu Yilun <[email protected]>

Please describe what this patch does at the beginning. And below background descriptions follow.

The description has been fully revised in the new last patch "fpga:
dfl: destroy/recreate feature platform device on port release/assign".

Quoted 45 lines
Quoted 15 lines

With the Intel FPGA PAC D5005, DFL ports are registered as platform devices in PF mode. The port device must be removed from the host when the user wants to configure the port as a VF for use by a user-space driver, e.g., for pass-through to a virtual machine. The FME device ioctls DFL_FPGA_FME_PORT_RELEASE/ASSIGN are assigned for this purpose.

In the previous implementation, the port platform device is not completely destroyed on port release: it is removed from the system by platform_device_del(), but the platform device instance is retained. When DFL_FPGA_FME_PORT_ASSIGN is called, the platform device is added back with platform_device_add(), which conflicts with this comment of device_add(): "Do not call this routine more than once for any device structure", and would previously cause a kernel warning at runtime.

Quoted 2 lines

This patch completely unregisters the port platform device on release and registers a new device on assign. But the main work is to remove

The main work of this series, not this patch.

Quoted 25 lines

the dependency on struct dfl_feature_platform_data for many internal DFL APIs. This structure holds many DFL enumeration infos for feature devices. Many DFL APIs are expected to work with these infos even when the port platform device is unregistered. But after this change, the platform_data will be freed on port release. Hence this patch introduces a new structure dfl_feature_dev_data, which acts similarly to the previous dfl_feature_platform_data. dfl_feature_platform_data then only needs a pointer to dfl_feature_dev_data to query DFL enumeration infos.

---
 drivers/fpga/dfl-fme-br.c |   2 -
 drivers/fpga/dfl.c        | 207 ++++++++++++++++++--------------------
 drivers/fpga/dfl.h        |  57 +++++++----
 3 files changed, 133 insertions(+), 133 deletions(-)
drivers/fpga/dfl-fme-br.c +0 -0
index 5c60a38ec76c..a298a041877b 100644
@@ -85,8 +85,6 @@ static void fme_br_remove(struct platform_device *pdev)
 
Quoted 6 lines

drivers/fpga/dfl-fme-br.c static void fme_br_remove(struct platform_device *pdev) · line 89

Quoted 4 lines
drivers/fpga/dfl-fme-br.c +0 -2
 	fpga_bridge_unregister(br);
 
-	if (priv->port_fdata)
-		put_device(&priv->port_fdata->dev->dev);

I can't remember why all the get_device/put_device() are not needed anymore.

This is now detailed in the new patch "fpga: dfl: destroy/recreate feature platform device on port release/assign".

Quoted 177 lines
Quoted 177 lines
drivers/fpga/dfl-fme-br.c +0 -0
 	if (priv->port_ops)
 		dfl_fpga_port_ops_put(priv->port_ops);
 }
drivers/fpga/dfl.c +44 -33
index 52f58d029ca4..a77d7692b170 100644
@@ -160,7 +160,7 @@ struct dfl_fpga_port_ops *dfl_fpga_port_ops_get(struct dfl_feature_dev_data *fda
 
 	list_for_each_entry(ops, &dfl_port_ops_list, node) {
 		/* match port_ops using the name of platform device */
-		if (!strcmp(fdata->dev->name, ops->name)) {
+		if (!strcmp(fdata->pdev_name, ops->name)) {
 			if (!try_module_get(ops->owner))
 				ops = NULL;
 			goto done;
@@ -681,7 +681,6 @@ EXPORT_SYMBOL_GPL(dfl_fpga_dev_ops_unregister);
  * @nr_irqs: number of irqs for all feature devices.
  * @irq_table: Linux IRQ numbers for all irqs, indexed by local irq index of
  *	       this device.
- * @feature_dev: current feature device.
  * @type: the current FIU type.
  * @ioaddr: header register region address of current FIU in enumeration.
  * @start: register resource start of current FIU.
@@ -695,7 +694,6 @@ struct build_feature_devs_info {
 	unsigned int nr_irqs;
 	int *irq_table;
 
-	struct platform_device *feature_dev;
 	enum dfl_id_type type;
 	void __iomem *ioaddr;
 	resource_size_t start;
@@ -736,7 +734,6 @@ static void dfl_fpga_cdev_add_port_data(struct dfl_fpga_cdev *cdev,
 {
 	mutex_lock(&cdev->lock);
 	list_add(&fdata->node, &cdev->port_dev_list);
-	get_device(&fdata->dev->dev);
 	mutex_unlock(&cdev->lock);
 }
 
@@ -744,7 +741,6 @@ static struct dfl_feature_dev_data *
 binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 {
 	enum dfl_id_type type = binfo->type;
-	struct platform_device *fdev = binfo->feature_dev;
 	struct dfl_feature_info *finfo, *p;
 	struct dfl_feature_dev_data *fdata;
 	int ret, index = 0, res_idx = 0;
@@ -752,18 +748,27 @@ binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 	if (WARN_ON_ONCE(type >= DFL_ID_MAX))
 		return ERR_PTR(-EINVAL);
 
-	/*
-	 * we do not need to care for the memory which is associated with
-	 * the platform device. After calling platform_device_unregister(),
-	 * it will be automatically freed by device's release() callback,
-	 * platform_device_release().
-	 */
-	fdata = kzalloc(struct_size(fdata, features, binfo->feature_num), GFP_KERNEL);
+	fdata = devm_kzalloc(binfo->dev, sizeof(*fdata), GFP_KERNEL);
 	if (!fdata)
 		return ERR_PTR(-ENOMEM);
 
-	fdata->dev = fdev;
+	fdata->features = devm_kcalloc(binfo->dev, binfo->feature_num,
+				       sizeof(*fdata->features), GFP_KERNEL);
+	if (!fdata->features)
+		return ERR_PTR(-ENOMEM);
+
+	fdata->resources = devm_kcalloc(binfo->dev, binfo->feature_num,
+					sizeof(*fdata->resources), GFP_KERNEL);
+	if (!fdata->resources)
+		return ERR_PTR(-ENOMEM);
+
 	fdata->type = type;
+
+	fdata->pdev_id = dfl_id_alloc(type, binfo->dev);
+	if (fdata->pdev_id < 0)
+		return ERR_PTR(fdata->pdev_id);
+
+	fdata->pdev_name = dfl_devs[type].name;
 	fdata->num = binfo->feature_num;
 	fdata->dfl_cdev = binfo->cdev;
 	fdata->id = FEATURE_DEV_ID_UNUSED;
@@ -779,15 +784,6 @@ binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 	 */
 	WARN_ON(fdata->disable_count);
 
-	fdev->dev.platform_data = fdata;
-
-	/* each sub feature has one MMIO resource */
-	fdev->num_resources = binfo->feature_num;
-	fdev->resource = kcalloc(binfo->feature_num, sizeof(*fdev->resource),
-				 GFP_KERNEL);
-	if (!fdev->resource)
-		return ERR_PTR(-ENOMEM);
-
 	/* fill features and resource information for feature dev */
 	list_for_each_entry_safe(finfo, p, &binfo->sub_features, node) {
 		struct dfl_feature *feature = &fdata->features[index++];
@@ -795,7 +791,6 @@ binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 		unsigned int i;
 
 		/* save resource information for each feature */
-		feature->dev = fdev;
 		feature->id = finfo->fid;
 		feature->revision = finfo->revision;
 		feature->dfh_version = finfo->dfh_version;
@@ -804,8 +799,10 @@ binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 			feature->params = devm_kmemdup(binfo->dev,
 						       finfo->params, finfo->param_size,
 						       GFP_KERNEL);
-			if (!feature->params)
-				return ERR_PTR(-ENOMEM);
+			if (!feature->params) {
+				ret = -ENOMEM;
+				goto err_free_id;
+			}
 
 			feature->param_size = finfo->param_size;
 		}
@@ -823,19 +820,20 @@ binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 						      &finfo->mmio_res);
 			if (IS_ERR(feature->ioaddr)) {
 				ret = PTR_ERR(feature->ioaddr);
-				return ERR_PTR(ret);
+				goto err_free_id;
 			}
 		} else {
 			feature->resource_index = res_idx;
-			fdev->resource[res_idx++] = finfo->mmio_res;
+			fdata->resources[res_idx++] = finfo->mmio_res;
 		}
 
 		if (finfo->nr_irqs) {
 			ctx = devm_kcalloc(binfo->dev, finfo->nr_irqs,
 					   sizeof(*ctx), GFP_KERNEL);
-			if (!ctx)
-				return ERR_PTR(-ENOMEM);
-
+			if (!ctx) {
+				ret = -ENOMEM;
+				goto err_free_id;
+			}
 			for (i = 0; i < finfo->nr_irqs; i++)
 				ctx[i].irq =
 					binfo->irq_table[finfo->irq_base + i];
@@ -848,36 +846,67 @@ binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 		kfree(finfo);
 	}
 
+	fdata->resource_num = res_idx;
+
 	return fdata;
+
+err_free_id:
+	dfl_id_free(type, fdata->pdev_id);
+
+	return ERR_PTR(ret);
 }
 
-static int
-build_info_create_dev(struct build_feature_devs_info *binfo)
+/*
+ * register current feature device, it is called when we need to switch to
+ * another feature parsing or we have parsed all features on given device
+ * feature list.
+ */
+static int feature_dev_register(struct dfl_feature_dev_data *fdata)
 {
-	enum dfl_id_type type = binfo->type;
+	struct dfl_feature_platform_data pdata = {};
 	struct platform_device *fdev;
+	struct dfl_feature *feature;
+	int ret;
 
Quoted 11 lines

drivers/fpga/dfl.c binfo_create_feature_dev_data(struct build_feature_devs_info *binfo) · line 871

Quoted 6 lines
drivers/fpga/dfl.c +1 -5
-	/*
-	 * we use -ENODEV as the initialization indicator which indicates
-	 * whether the id need to be reclaimed
-	 */
-	fdev = platform_device_alloc(dfl_devs[type].name, -ENODEV);
+	fdev = platform_device_alloc(fdata->pdev_name, fdata->pdev_id);

I see a part of the change is to delay the platform_device_alloc() so that the feature platform device registration work could be gathered in one function. Not sure if it is necessary for the port platform release issue, or could be an independent enhancement patch.

This has been factored out into a separate patch "fpga: dfl: allocate platform device after feature device data". Delaying the call to platform_device_alloc() to feature_dev_register() is essential to the overall series since the last patch reuses feature_dev_register() for the port reassign. If platform_device_alloc() were not part of feature_dev_register(), the call to platform_device_alloc() would have to be repeated in dfl_fpga_cdev_assign_port().

Quoted 3 lines

I raise this cause I still feel hard to understand all these changes in this patch and look for any chance to further split it.

The smaller patches of the v4 series are hopefully much more straight- forward to follow now. The refactoring uncovered a leak of the feature device id for released ports that has been fixed in the new patch "fpga: dfl: store platform device id in feature device data".

Quoted 5 lines

BTW: Overall this series is much improved than the last version.

Thanks,
Yilun

Thank you for your quick review and guidance for this series.

Thanks,
Peter

#20 lore raw
Quoted 58 lines · Peter
Quoted 6 lines · Xu Yilun
Quoted 2 lines · Peter Colberg

From: Xu Yilun <[email protected]>

Please describe what this patch does at the beginning. And below background descriptions follow.

The description has been fully revised in the new last patch "fpga:
dfl: destroy/recreate feature platform device on port release/assign".

Quoted 47 lines
Quoted 15 lines

With the Intel FPGA PAC D5005, DFL ports are registered as platform devices in PF mode. The port device must be removed from the host when the user wants to configure the port as a VF for use by a user-space driver, e.g., for pass-through to a virtual machine. The FME device ioctls DFL_FPGA_FME_PORT_RELEASE/ASSIGN are assigned for this purpose.

In the previous implementation, the port platform device is not completely destroyed on port release: it is removed from the system by platform_device_del(), but the platform device instance is retained. When DFL_FPGA_FME_PORT_ASSIGN is called, the platform device is added back with platform_device_add(), which conflicts with this comment of device_add(): "Do not call this routine more than once for any device structure", and would previously cause a kernel warning at runtime.

Quoted 2 lines

This patch completely unregisters the port platform device on release and registers a new device on assign. But the main work is to remove

The main work of this series, not this patch.

Quoted 27 lines

the dependency on struct dfl_feature_platform_data for many internal DFL APIs. This structure holds many DFL enumeration infos for feature devices. Many DFL APIs are expected to work with these infos even when the port platform device is unregistered. But after this change, the platform_data will be freed on port release. Hence this patch introduces a new structure dfl_feature_dev_data, which acts similarly to the previous dfl_feature_platform_data. dfl_feature_platform_data then only needs a pointer to dfl_feature_dev_data to query DFL enumeration infos.

---
 drivers/fpga/dfl-fme-br.c |   2 -
 drivers/fpga/dfl.c        | 207 ++++++++++++++++++--------------------
 drivers/fpga/dfl.h        |  57 +++++++----
 3 files changed, 133 insertions(+), 133 deletions(-)
drivers/fpga/dfl-fme-br.c +0 -0
index 5c60a38ec76c..a298a041877b 100644
@@ -85,8 +85,6 @@ static void fme_br_remove(struct platform_device *pdev)
 
 	fpga_bridge_unregister(br);
 
Quoted 7 lines
Quoted 4 lines

drivers/fpga/dfl-fme-br.c static void fme_br_remove(struct platform_device *pdev) · line 89

Quoted 2 lines
drivers/fpga/dfl-fme-br.c +0 -2
-	if (priv->port_fdata)
-		put_device(&priv->port_fdata->dev->dev);

I can't remember why all the get_device/put_device() are not needed anymore.

This is now detailed in the new patch "fpga: dfl: destroy/recreate feature platform device on port release/assign".

My apologies, the correct patch is "fpga: dfl: drop unneeded get_device() and put_device() of feature device".

Thanks,
Peter

Quoted 217 lines
Quoted 178 lines
Quoted 178 lines
drivers/fpga/dfl-fme-br.c +0 -0
 	if (priv->port_ops)
 		dfl_fpga_port_ops_put(priv->port_ops);
 }
drivers/fpga/dfl.c +44 -33
index 52f58d029ca4..a77d7692b170 100644
@@ -160,7 +160,7 @@ struct dfl_fpga_port_ops *dfl_fpga_port_ops_get(struct dfl_feature_dev_data *fda
 
 	list_for_each_entry(ops, &dfl_port_ops_list, node) {
 		/* match port_ops using the name of platform device */
-		if (!strcmp(fdata->dev->name, ops->name)) {
+		if (!strcmp(fdata->pdev_name, ops->name)) {
 			if (!try_module_get(ops->owner))
 				ops = NULL;
 			goto done;
@@ -681,7 +681,6 @@ EXPORT_SYMBOL_GPL(dfl_fpga_dev_ops_unregister);
  * @nr_irqs: number of irqs for all feature devices.
  * @irq_table: Linux IRQ numbers for all irqs, indexed by local irq index of
  *	       this device.
- * @feature_dev: current feature device.
  * @type: the current FIU type.
  * @ioaddr: header register region address of current FIU in enumeration.
  * @start: register resource start of current FIU.
@@ -695,7 +694,6 @@ struct build_feature_devs_info {
 	unsigned int nr_irqs;
 	int *irq_table;
 
-	struct platform_device *feature_dev;
 	enum dfl_id_type type;
 	void __iomem *ioaddr;
 	resource_size_t start;
@@ -736,7 +734,6 @@ static void dfl_fpga_cdev_add_port_data(struct dfl_fpga_cdev *cdev,
 {
 	mutex_lock(&cdev->lock);
 	list_add(&fdata->node, &cdev->port_dev_list);
-	get_device(&fdata->dev->dev);
 	mutex_unlock(&cdev->lock);
 }
 
@@ -744,7 +741,6 @@ static struct dfl_feature_dev_data *
 binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 {
 	enum dfl_id_type type = binfo->type;
-	struct platform_device *fdev = binfo->feature_dev;
 	struct dfl_feature_info *finfo, *p;
 	struct dfl_feature_dev_data *fdata;
 	int ret, index = 0, res_idx = 0;
@@ -752,18 +748,27 @@ binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 	if (WARN_ON_ONCE(type >= DFL_ID_MAX))
 		return ERR_PTR(-EINVAL);
 
-	/*
-	 * we do not need to care for the memory which is associated with
-	 * the platform device. After calling platform_device_unregister(),
-	 * it will be automatically freed by device's release() callback,
-	 * platform_device_release().
-	 */
-	fdata = kzalloc(struct_size(fdata, features, binfo->feature_num), GFP_KERNEL);
+	fdata = devm_kzalloc(binfo->dev, sizeof(*fdata), GFP_KERNEL);
 	if (!fdata)
 		return ERR_PTR(-ENOMEM);
 
-	fdata->dev = fdev;
+	fdata->features = devm_kcalloc(binfo->dev, binfo->feature_num,
+				       sizeof(*fdata->features), GFP_KERNEL);
+	if (!fdata->features)
+		return ERR_PTR(-ENOMEM);
+
+	fdata->resources = devm_kcalloc(binfo->dev, binfo->feature_num,
+					sizeof(*fdata->resources), GFP_KERNEL);
+	if (!fdata->resources)
+		return ERR_PTR(-ENOMEM);
+
 	fdata->type = type;
+
+	fdata->pdev_id = dfl_id_alloc(type, binfo->dev);
+	if (fdata->pdev_id < 0)
+		return ERR_PTR(fdata->pdev_id);
+
+	fdata->pdev_name = dfl_devs[type].name;
 	fdata->num = binfo->feature_num;
 	fdata->dfl_cdev = binfo->cdev;
 	fdata->id = FEATURE_DEV_ID_UNUSED;
@@ -779,15 +784,6 @@ binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 	 */
 	WARN_ON(fdata->disable_count);
 
-	fdev->dev.platform_data = fdata;
-
-	/* each sub feature has one MMIO resource */
-	fdev->num_resources = binfo->feature_num;
-	fdev->resource = kcalloc(binfo->feature_num, sizeof(*fdev->resource),
-				 GFP_KERNEL);
-	if (!fdev->resource)
-		return ERR_PTR(-ENOMEM);
-
 	/* fill features and resource information for feature dev */
 	list_for_each_entry_safe(finfo, p, &binfo->sub_features, node) {
 		struct dfl_feature *feature = &fdata->features[index++];
@@ -795,7 +791,6 @@ binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 		unsigned int i;
 
 		/* save resource information for each feature */
-		feature->dev = fdev;
 		feature->id = finfo->fid;
 		feature->revision = finfo->revision;
 		feature->dfh_version = finfo->dfh_version;
@@ -804,8 +799,10 @@ binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 			feature->params = devm_kmemdup(binfo->dev,
 						       finfo->params, finfo->param_size,
 						       GFP_KERNEL);
-			if (!feature->params)
-				return ERR_PTR(-ENOMEM);
+			if (!feature->params) {
+				ret = -ENOMEM;
+				goto err_free_id;
+			}
 
 			feature->param_size = finfo->param_size;
 		}
@@ -823,19 +820,20 @@ binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 						      &finfo->mmio_res);
 			if (IS_ERR(feature->ioaddr)) {
 				ret = PTR_ERR(feature->ioaddr);
-				return ERR_PTR(ret);
+				goto err_free_id;
 			}
 		} else {
 			feature->resource_index = res_idx;
-			fdev->resource[res_idx++] = finfo->mmio_res;
+			fdata->resources[res_idx++] = finfo->mmio_res;
 		}
 
 		if (finfo->nr_irqs) {
 			ctx = devm_kcalloc(binfo->dev, finfo->nr_irqs,
 					   sizeof(*ctx), GFP_KERNEL);
-			if (!ctx)
-				return ERR_PTR(-ENOMEM);
-
+			if (!ctx) {
+				ret = -ENOMEM;
+				goto err_free_id;
+			}
 			for (i = 0; i < finfo->nr_irqs; i++)
 				ctx[i].irq =
 					binfo->irq_table[finfo->irq_base + i];
@@ -848,36 +846,67 @@ binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 		kfree(finfo);
 	}
 
+	fdata->resource_num = res_idx;
+
 	return fdata;
+
+err_free_id:
+	dfl_id_free(type, fdata->pdev_id);
+
+	return ERR_PTR(ret);
 }
 
-static int
-build_info_create_dev(struct build_feature_devs_info *binfo)
+/*
+ * register current feature device, it is called when we need to switch to
+ * another feature parsing or we have parsed all features on given device
+ * feature list.
+ */
+static int feature_dev_register(struct dfl_feature_dev_data *fdata)
 {
-	enum dfl_id_type type = binfo->type;
+	struct dfl_feature_platform_data pdata = {};
 	struct platform_device *fdev;
+	struct dfl_feature *feature;
+	int ret;
 
Quoted 11 lines

drivers/fpga/dfl.c binfo_create_feature_dev_data(struct build_feature_devs_info *binfo) · line 871

Quoted 6 lines
drivers/fpga/dfl.c +1 -5
-	/*
-	 * we use -ENODEV as the initialization indicator which indicates
-	 * whether the id need to be reclaimed
-	 */
-	fdev = platform_device_alloc(dfl_devs[type].name, -ENODEV);
+	fdev = platform_device_alloc(fdata->pdev_name, fdata->pdev_id);

I see a part of the change is to delay the platform_device_alloc() so that the feature platform device registration work could be gathered in one function. Not sure if it is necessary for the port platform release issue, or could be an independent enhancement patch.

This has been factored out into a separate patch "fpga: dfl: allocate platform device after feature device data". Delaying the call to platform_device_alloc() to feature_dev_register() is essential to the overall series since the last patch reuses feature_dev_register() for the port reassign. If platform_device_alloc() were not part of feature_dev_register(), the call to platform_device_alloc() would have to be repeated in dfl_fpga_cdev_assign_port().

Quoted 3 lines

I raise this cause I still feel hard to understand all these changes in this patch and look for any chance to further split it.

The smaller patches of the v4 series are hopefully much more straight- forward to follow now. The refactoring uncovered a leak of the feature device id for released ports that has been fixed in the new patch "fpga: dfl: store platform device id in feature device data".

Quoted 5 lines

BTW: Overall this series is much improved than the last version.

Thanks,
Yilun

Thank you for your quick review and guidance for this series.

Thanks,
Peter