Skip to contentSkip to conversation

UIO support for dfl devices

PATCH Superseded Xu Yilun 4 participants 24 messages Oct 16, 2020 – Nov 9, 2020

Subsystems (2)

v1 of 12 Superseded Latest is v12 →

lore
Share & tools
lore

Xu Yilun

author maintainer
#0 lore raw

cover UIO support for dfl devices

This patchset supports some dfl device drivers written in userspace.

The usage is like:

 # echo dfl_dev.1 > /sys/bus/dfl/drivers/<kernel driver>/unbind
 # echo dfl-uio-pdev > /sys/bus/dfl/devices/dfl_dev.1/driver_override
 # echo dfl_dev.1 > /sys/bus/dfl/drivers_probe

Xu Yilun (2):
fpga: dfl: add driver_override support
fpga: dfl: add the userspace I/O device support for DFL devices

 Documentation/ABI/testing/sysfs-bus-dfl | 28 +++++++++--
 drivers/fpga/Kconfig                    | 10 ++++
 drivers/fpga/Makefile                   |  1 +
 drivers/fpga/dfl-uio-pdev.c             | 83 +++++++++++++++++++++++++++++++++
 drivers/fpga/dfl.c                      | 54 ++++++++++++++++++++-
 include/linux/dfl.h                     |  2 +
 6 files changed, 173 insertions(+), 5 deletions(-)
 create mode 100644 drivers/fpga/dfl-uio-pdev.c
Signature · 1 line
2.7.4
author maintainer
#1 lore raw

PATCH 1/2 fpga: dfl: add driver_override support

Add support for overriding the default matching of a dfl device to a dfl driver. It follows the same way that can be used for PCI and platform devices. This patch adds the 'driver_override' sysfs file.

---
 Documentation/ABI/testing/sysfs-bus-dfl | 28 ++++++++++++++---
 drivers/fpga/dfl.c                      | 54 ++++++++++++++++++++++++++++++++-
 include/linux/dfl.h                     |  2 ++
 3 files changed, 79 insertions(+), 5 deletions(-)
Documentation/ABI/testing/sysfs-bus-dfl +24 -4
index 23543be..db7e8d3 100644
@@ -1,15 +1,35 @@
 What:		/sys/bus/dfl/devices/dfl_dev.X/type
-Date:		Aug 2020
-KernelVersion:	5.10
+Date:		Oct 2020
+KernelVersion:	5.11
 Contact:	Xu Yilun <[email protected]>
 Description:	Read-only. It returns type of DFL FIU of the device. Now DFL
 		supports 2 FIU types, 0 for FME, 1 for PORT.
 		Format: 0x%x
 
 What:		/sys/bus/dfl/devices/dfl_dev.X/feature_id
-Date:		Aug 2020
-KernelVersion:	5.10
+Date:		Oct 2020
+KernelVersion:	5.11
 Contact:	Xu Yilun <[email protected]>
 Description:	Read-only. It returns feature identifier local to its DFL FIU
 		type.
 		Format: 0x%x
+
+What:           /sys/bus/dfl/devices/.../driver_override
+Date:           Oct 2020
+KernelVersion:  5.11
+Contact:        Xu Yilun <[email protected]>
+Description:    This file allows the driver for a device to be specified. When
+                specified, only a driver with a name matching the value written
+                to driver_override will have an opportunity to bind to the
+                device. The override is specified by writing a string to the
+                driver_override file (echo dfl-uio-pdev > driver_override) and
+                may be cleared with an empty string (echo > driver_override).
+                This returns the device to standard matching rules binding.
+                Writing to driver_override does not automatically unbind the
+                device from its current driver or make any attempt to
+                automatically load the specified driver.  If no driver with a
+                matching name is currently loaded in the kernel, the device
+                will not bind to any driver.  This also allows devices to
+                opt-out of driver binding using a driver_override name such as
+                "none".  Only a single driver may be specified in the override,
+                there is no support for parsing delimiters.
drivers/fpga/dfl.c +53 -1

78 lines not on this page · raw message

include/linux/dfl.h +2 -0
index 7affba2f..e1b2471 100644
@@ -32,6 +32,7 @@ enum dfl_id_type {
  * @num_irqs: number of IRQs supported by this dfl device.
  * @cdev: pointer to DFL FPGA container device this dfl device belongs to.
  * @id_entry: matched id entry in dfl driver's id table.
+ * @driver_override: driver name to force a match
  */
 struct dfl_device {
 	struct device dev;
@@ -43,6 +44,7 @@ struct dfl_device {
 	unsigned int num_irqs;
 	struct dfl_fpga_cdev *cdev;
 	const struct dfl_device_id *id_entry;
+	char *driver_override;
 };
 
 /**
Signature · 1 line
2.7.4
reviewer
#2 lore raw

Re: [PATCH 1/2] fpga: dfl: add driver_override support

Quoted 25 lines · Xu Yilun

Add support for overriding the default matching of a dfl device to a dfl driver. It follows the same way that can be used for PCI and platform devices. This patch adds the 'driver_override' sysfs file.

---
 Documentation/ABI/testing/sysfs-bus-dfl | 28 ++++++++++++++---
 drivers/fpga/dfl.c                      | 54 ++++++++++++++++++++++++++++++++-
 include/linux/dfl.h                     |  2 ++
 3 files changed, 79 insertions(+), 5 deletions(-)
Documentation/ABI/testing/sysfs-bus-dfl +2 -2
index 23543be..db7e8d3 100644
@@ -1,15 +1,35 @@
 What:		/sys/bus/dfl/devices/dfl_dev.X/type
-Date:		Aug 2020
-KernelVersion:	5.10
+Date:		Oct 2020
+KernelVersion:	5.11
 Contact:	Xu Yilun <[email protected]>
 Description:	Read-only. It returns type of DFL FIU of the device. Now DFL
 		supports 2 FIU types, 0 for FME, 1 for PORT.
 		Format: 0x%x

Documentation/ABI/testing/sysfs-bus-dfl line 15

Quoted 10 lines
Documentation/ABI/testing/sysfs-bus-dfl +2 -2
 
 What:		/sys/bus/dfl/devices/dfl_dev.X/feature_id
-Date:		Aug 2020
-KernelVersion:	5.10
+Date:		Oct 2020
+KernelVersion:	5.11
 Contact:	Xu Yilun <[email protected]>
 Description:	Read-only. It returns feature identifier local to its DFL FIU
 		type.
 		Format: 0x%x

These updates, do not match the comment.

Consider splitting this out.

Documentation/ABI/testing/sysfs-bus-dfl line 20

Quoted 5 lines
Documentation/ABI/testing/sysfs-bus-dfl +5 -0
+
+What:           /sys/bus/dfl/devices/.../driver_override
+Date:           Oct 2020
+KernelVersion:  5.11
+Contact:        Xu Yilun <[email protected]>

I am looking at description and trying to make it consistent with sysfs-bus-pci

Documentation/ABI/testing/sysfs-bus-dfl line 21

Quoted 1 line
Documentation/ABI/testing/sysfs-bus-dfl +1 -0
+Description:    This file allows the driver for a device to be specified.

'to be specified which will override the standard dfl bus feature id to driver mapping.'

Quoted 31 lines
Documentation/ABI/testing/sysfs-bus-dfl +14 -0
 When
+                specified, only a driver with a name matching the value written
+                to driver_override will have an opportunity to bind to the
+                device. The override is specified by writing a string to the
+                driver_override file (echo dfl-uio-pdev > driver_override) and
+                may be cleared with an empty string (echo > driver_override).
+                This returns the device to standard matching rules binding.
+                Writing to driver_override does not automatically unbind the
+                device from its current driver or make any attempt to
+                automatically load the specified driver.  If no driver with a
+                matching name is currently loaded in the kernel, the device
+                will not bind to any driver.  This also allows devices to
+                opt-out of driver binding using a driver_override name such as
+                "none".  Only a single driver may be specified in the override,
+                there is no support for parsing delimiters.
drivers/fpga/dfl.c +4 -0
index 511b20f..bc35750 100644
@@ -262,6 +262,10 @@ static int dfl_bus_match(struct device *dev, struct device_driver *drv)
 	struct dfl_driver *ddrv = to_dfl_drv(drv);
 	const struct dfl_device_id *id_entry;
 
+	/* When driver_override is set, only bind to the matching driver */
+	if (ddev->driver_override)
+		return !strcmp(ddev->driver_override, drv->name);
+
 	id_entry = ddrv->id_table;
 	if (id_entry) {
 		while (id_entry->feature_id) {

drivers/fpga/dfl.c static int dfl_bus_uevent(struct device *dev, struct kobj_uevent_env *env) · line 309

Quoted 3 lines
drivers/fpga/dfl.c +0 -0
@@ -303,6 +307,53 @@ static int dfl_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
 			      ddev->type, ddev->feature_id);
 }
 

I am looking at other implementations of driver_override* and looking for consistency.

drivers/fpga/dfl.c static int dfl_bus_uevent(struct device *dev, struct kobj_uevent_env *env) · line 317

Quoted 8 lines
drivers/fpga/dfl.c +8 -0
+static ssize_t driver_override_show(struct device *dev,
+				    struct device_attribute *attr, char *buf)
+{
+	struct dfl_device *ddev = to_dfl_dev(dev);
+	ssize_t len;
+
+	device_lock(dev);
+	len = sprintf(buf, "%s\n", ddev->driver_override);

len = snprintf(buf, PAGE_SIZE ...

Quoted 51 lines
drivers/fpga/dfl.c +40 -0
+	device_unlock(dev);
+	return len;
+}
+
+static ssize_t driver_override_store(struct device *dev,
+				     struct device_attribute *attr,
+				     const char *buf, size_t count)
+{
+	struct dfl_device *ddev = to_dfl_dev(dev);
+	char *driver_override, *old, *cp;
+
+	/* We need to keep extra room for a newline */
+	if (count >= (PAGE_SIZE - 1))
+		return -EINVAL;
+
+	driver_override = kstrndup(buf, count, GFP_KERNEL);
+	if (!driver_override)
+		return -ENOMEM;
+
+	cp = strchr(driver_override, '\n');
+	if (cp)
+		*cp = '\0';
+
+	device_lock(dev);
+	old = ddev->driver_override;
+	if (strlen(driver_override)) {
+		ddev->driver_override = driver_override;
+	} else {
+		kfree(driver_override);
+		ddev->driver_override = NULL;
+	}
+	device_unlock(dev);
+
+	kfree(old);
+
+	return count;
+}
+static DEVICE_ATTR_RW(driver_override);
+
 static ssize_t
 type_show(struct device *dev, struct device_attribute *attr, char *buf)
 {
@@ -324,6 +375,7 @@ static DEVICE_ATTR_RO(feature_id);
 static struct attribute *dfl_dev_attrs[] = {
 	&dev_attr_type.attr,
 	&dev_attr_feature_id.attr,
+	&dev_attr_driver_override.attr,
 	NULL,
 };
 ATTRIBUTE_GROUPS(dfl_dev);

drivers/fpga/dfl.c static int dfl_devs_add(struct dfl_feature_platform_data *pdata) · line 524

Quoted 5 lines
drivers/fpga/dfl.c +1 -1
@@ -469,7 +521,7 @@ static int dfl_devs_add(struct dfl_feature_platform_data *pdata)
 
 int __dfl_driver_register(struct dfl_driver *dfl_drv, struct module *owner)
 {
-	if (!dfl_drv || !dfl_drv->probe || !dfl_drv->id_table)
+	if (!dfl_drv || !dfl_drv->probe)

id_table is still needed for the normal case.

Instead of removing this check, could you add something like

|| (!dfl_drv->is_override && !dfl_drv->id_table)

Tom

Quoted 23 lines
drivers/fpga/dfl.c +0 -0
 		return -EINVAL;
 
 	dfl_drv->drv.owner = owner;
include/linux/dfl.h +2 -0
index 7affba2f..e1b2471 100644
@@ -32,6 +32,7 @@ enum dfl_id_type {
  * @num_irqs: number of IRQs supported by this dfl device.
  * @cdev: pointer to DFL FPGA container device this dfl device belongs to.
  * @id_entry: matched id entry in dfl driver's id table.
+ * @driver_override: driver name to force a match
  */
 struct dfl_device {
 	struct device dev;
@@ -43,6 +44,7 @@ struct dfl_device {
 	unsigned int num_irqs;
 	struct dfl_fpga_cdev *cdev;
 	const struct dfl_device_id *id_entry;
+	char *driver_override;
 };
 
 /**
author maintainer
#3 lore raw

Re: [PATCH 1/2] fpga: dfl: add driver_override support

Quoted 40 lines · Tom Rix
Quoted 25 lines · Xu Yilun

Add support for overriding the default matching of a dfl device to a dfl driver. It follows the same way that can be used for PCI and platform devices. This patch adds the 'driver_override' sysfs file.

---
 Documentation/ABI/testing/sysfs-bus-dfl | 28 ++++++++++++++---
 drivers/fpga/dfl.c                      | 54 ++++++++++++++++++++++++++++++++-
 include/linux/dfl.h                     |  2 ++
 3 files changed, 79 insertions(+), 5 deletions(-)
Documentation/ABI/testing/sysfs-bus-dfl +2 -2
index 23543be..db7e8d3 100644
@@ -1,15 +1,35 @@
 What:		/sys/bus/dfl/devices/dfl_dev.X/type
-Date:		Aug 2020
-KernelVersion:	5.10
+Date:		Oct 2020
+KernelVersion:	5.11
 Contact:	Xu Yilun <[email protected]>
 Description:	Read-only. It returns type of DFL FIU of the device. Now DFL
 		supports 2 FIU types, 0 for FME, 1 for PORT.
 		Format: 0x%x

Documentation/ABI/testing/sysfs-bus-dfl line 15

Quoted 10 lines
Documentation/ABI/testing/sysfs-bus-dfl +2 -2
 
 What:		/sys/bus/dfl/devices/dfl_dev.X/feature_id
-Date:		Aug 2020
-KernelVersion:	5.10
+Date:		Oct 2020
+KernelVersion:	5.11
 Contact:	Xu Yilun <[email protected]>
 Description:	Read-only. It returns feature identifier local to its DFL FIU
 		type.
 		Format: 0x%x

These updates, do not match the comment.

Quoted 1 line

Consider splitting this out.

I'm sorry it's a typo. The above code should not be changed.

Quoted 10 lines

Documentation/ABI/testing/sysfs-bus-dfl line 20

Quoted 6 lines
Documentation/ABI/testing/sysfs-bus-dfl +5 -0
+
+What:           /sys/bus/dfl/devices/.../driver_override
+Date:           Oct 2020
+KernelVersion:  5.11
+Contact:        Xu Yilun <[email protected]>

I am looking at description and trying to make it consistent with sysfs-bus-pci

Documentation/ABI/testing/sysfs-bus-dfl line 21

Quoted 1 line
Documentation/ABI/testing/sysfs-bus-dfl +1 -0
+Description:    This file allows the driver for a device to be specified.

'to be specified which will override the standard dfl bus feature id to driver mapping.'

Yes, it could be improved.

Actually now it is the "type" and "feature id" matching, the 2 fields are defined for dfl_driver.id_table. In future for dfl v1, it may be GUID matching, which will be added to id_table. So how about we make it more generic:

'to be specified which will override the standard ID table matching.'

Quoted 39 lines
Quoted 33 lines
Documentation/ABI/testing/sysfs-bus-dfl +14 -0
 When
+                specified, only a driver with a name matching the value written
+                to driver_override will have an opportunity to bind to the
+                device. The override is specified by writing a string to the
+                driver_override file (echo dfl-uio-pdev > driver_override) and
+                may be cleared with an empty string (echo > driver_override).
+                This returns the device to standard matching rules binding.
+                Writing to driver_override does not automatically unbind the
+                device from its current driver or make any attempt to
+                automatically load the specified driver.  If no driver with a
+                matching name is currently loaded in the kernel, the device
+                will not bind to any driver.  This also allows devices to
+                opt-out of driver binding using a driver_override name such as
+                "none".  Only a single driver may be specified in the override,
+                there is no support for parsing delimiters.
drivers/fpga/dfl.c +4 -0
index 511b20f..bc35750 100644
@@ -262,6 +262,10 @@ static int dfl_bus_match(struct device *dev, struct device_driver *drv)
 	struct dfl_driver *ddrv = to_dfl_drv(drv);
 	const struct dfl_device_id *id_entry;
 
+	/* When driver_override is set, only bind to the matching driver */
+	if (ddev->driver_override)
+		return !strcmp(ddev->driver_override, drv->name);
+
 	id_entry = ddrv->id_table;
 	if (id_entry) {
 		while (id_entry->feature_id) {

drivers/fpga/dfl.c static int dfl_bus_uevent(struct device *dev, struct kobj_uevent_env *env) · line 309

Quoted 3 lines
drivers/fpga/dfl.c +0 -0
@@ -303,6 +307,53 @@ static int dfl_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
 			      ddev->type, ddev->feature_id);
 }
 

I am looking at other implementations of driver_override* and looking for consistency.

Quoted 9 lines

drivers/fpga/dfl.c static int dfl_bus_uevent(struct device *dev, struct kobj_uevent_env *env) · line 317

Quoted 8 lines
drivers/fpga/dfl.c +8 -0
+static ssize_t driver_override_show(struct device *dev,
+				    struct device_attribute *attr, char *buf)
+{
+	struct dfl_device *ddev = to_dfl_dev(dev);
+	ssize_t len;
+
+	device_lock(dev);
+	len = sprintf(buf, "%s\n", ddev->driver_override);

len = snprintf(buf, PAGE_SIZE ...

It is good to me.

Some bus drivers use snprintf, some use sprintf.

I think it is reasonable snprintf is used here, unlike %d, %u ... it is uncertain for the output size of %s.

Quoted 59 lines
Quoted 51 lines
drivers/fpga/dfl.c +40 -0
+	device_unlock(dev);
+	return len;
+}
+
+static ssize_t driver_override_store(struct device *dev,
+				     struct device_attribute *attr,
+				     const char *buf, size_t count)
+{
+	struct dfl_device *ddev = to_dfl_dev(dev);
+	char *driver_override, *old, *cp;
+
+	/* We need to keep extra room for a newline */
+	if (count >= (PAGE_SIZE - 1))
+		return -EINVAL;
+
+	driver_override = kstrndup(buf, count, GFP_KERNEL);
+	if (!driver_override)
+		return -ENOMEM;
+
+	cp = strchr(driver_override, '\n');
+	if (cp)
+		*cp = '\0';
+
+	device_lock(dev);
+	old = ddev->driver_override;
+	if (strlen(driver_override)) {
+		ddev->driver_override = driver_override;
+	} else {
+		kfree(driver_override);
+		ddev->driver_override = NULL;
+	}
+	device_unlock(dev);
+
+	kfree(old);
+
+	return count;
+}
+static DEVICE_ATTR_RW(driver_override);
+
 static ssize_t
 type_show(struct device *dev, struct device_attribute *attr, char *buf)
 {
@@ -324,6 +375,7 @@ static DEVICE_ATTR_RO(feature_id);
 static struct attribute *dfl_dev_attrs[] = {
 	&dev_attr_type.attr,
 	&dev_attr_feature_id.attr,
+	&dev_attr_driver_override.attr,
 	NULL,
 };
 ATTRIBUTE_GROUPS(dfl_dev);

drivers/fpga/dfl.c static int dfl_devs_add(struct dfl_feature_platform_data *pdata) · line 524

Quoted 5 lines
drivers/fpga/dfl.c +1 -1
@@ -469,7 +521,7 @@ static int dfl_devs_add(struct dfl_feature_platform_data *pdata)
 
 int __dfl_driver_register(struct dfl_driver *dfl_drv, struct module *owner)
 {
-	if (!dfl_drv || !dfl_drv->probe || !dfl_drv->id_table)
+	if (!dfl_drv || !dfl_drv->probe)

id_table is still needed for the normal case.

Quoted 3 lines

Instead of removing this check, could you add something like

|| (!dfl_drv->is_override && !dfl_drv->id_table)

I don't think it is needed. Seems is_override and !id_table are duplicated conditions for this implementation. And it may make confusing, e.g. could a driver been force matched when is_override is not set?

I think we could make it simple, if the dfl driver didn't provide the id_table, normally it could not match any device. I think it could be easily understood by dfl driver developers.

Thanks,
Yilun

Quoted 26 lines

Tom

Quoted 23 lines
drivers/fpga/dfl.c +0 -0
 		return -EINVAL;
 
 	dfl_drv->drv.owner = owner;
include/linux/dfl.h +2 -0
index 7affba2f..e1b2471 100644
@@ -32,6 +32,7 @@ enum dfl_id_type {
  * @num_irqs: number of IRQs supported by this dfl device.
  * @cdev: pointer to DFL FPGA container device this dfl device belongs to.
  * @id_entry: matched id entry in dfl driver's id table.
+ * @driver_override: driver name to force a match
  */
 struct dfl_device {
 	struct device dev;
@@ -43,6 +44,7 @@ struct dfl_device {
 	unsigned int num_irqs;
 	struct dfl_fpga_cdev *cdev;
 	const struct dfl_device_id *id_entry;
+	char *driver_override;
 };
 
 /**
#4 lore raw

RE: [PATCH 1/2] fpga: dfl: add driver_override support

Quoted 123 lines · Tom Rix
Quoted 10 lines · Xu Yilun
Quoted 8 lines

Add support for overriding the default matching of a dfl device to a dfl driver. It follows the same way that can be used for PCI and platform devices. This patch adds the 'driver_override' sysfs file.

---
 Documentation/ABI/testing/sysfs-bus-dfl | 28 ++++++++++++++---
 drivers/fpga/dfl.c                      | 54

++++++++++++++++++++++++++++++++-

Quoted 4 lines

Review

Quoted 4 lines
 include/linux/dfl.h                     |  2 ++
 3 files changed, 79 insertions(+), 5 deletions(-)
(quoted diff) +0 -0

b/Documentation/ABI/testing/sysfs-bus-dfl

Quoted 11 lines

Documentation/ABI/testing/sysfs-bus-dfl line 5

Quoted 11 lines

index 23543be..db7e8d3 100644

Documentation/ABI/testing/sysfs-bus-dfl +2 -2
@@ -1,15 +1,35 @@
 What:		/sys/bus/dfl/devices/dfl_dev.X/type
-Date:		Aug 2020
-KernelVersion:	5.10
+Date:		Oct 2020
+KernelVersion:	5.11
 Contact:	Xu Yilun <[email protected]>
 Description:	Read-only. It returns type of DFL FIU of the device.

Now DFL

Quoted 10 lines

Documentation/ABI/testing/sysfs-bus-dfl

Quoted 10 lines
Documentation/ABI/testing/sysfs-bus-dfl +2 -2
 		supports 2 FIU types, 0 for FME, 1 for PORT.
 		Format: 0x%x
 
 What:		/sys/bus/dfl/devices/dfl_dev.X/feature_id
-Date:		Aug 2020
-KernelVersion:	5.10
+Date:		Oct 2020
+KernelVersion:	5.11
 Contact:	Xu Yilun <[email protected]>
 Description:	Read-only. It returns feature identifier local to its DFL

FIU

Quoted 6 lines

Documentation/ABI/testing/sysfs-bus-dfl line 15

Quoted 2 lines
Documentation/ABI/testing/sysfs-bus-dfl +0 -0
 		type.
 		Format: 0x%x

These updates, do not match the comment.

Consider splitting this out.

I'm sorry it's a typo. The above code should not be changed.

Quoted 7 lines

Documentation/ABI/testing/sysfs-bus-dfl line 20

Quoted 6 lines
Documentation/ABI/testing/sysfs-bus-dfl +5 -0
+
+What:           /sys/bus/dfl/devices/.../driver_override
+Date:           Oct 2020
+KernelVersion:  5.11
+Contact:        Xu Yilun <[email protected]>

I am looking at description and trying to make it consistent with sysfs-bus-

pci

Quoted 3 lines

Documentation/ABI/testing/sysfs-bus-dfl line 21

Quoted 1 line
Documentation/ABI/testing/sysfs-bus-dfl +1 -0
+Description:    This file allows the driver for a device to be specified.

'to be specified which will override the standard dfl bus feature id to driver

mapping.'

Yes, it could be improved.

Actually now it is the "type" and "feature id" matching, the 2 fields are defined for dfl_driver.id_table. In future for dfl v1, it may be GUID matching, which will be added to id_table. So how about we make it more generic:

'to be specified which will override the standard ID table matching.'

Quoted 14 lines
Quoted 14 lines
Documentation/ABI/testing/sysfs-bus-dfl +11 -0
 When
+                specified, only a driver with a name matching the value written
+                to driver_override will have an opportunity to bind to the
+                device. The override is specified by writing a string to the
+                driver_override file (echo dfl-uio-pdev > driver_override) and
+                may be cleared with an empty string (echo > driver_override).
+                This returns the device to standard matching rules binding.
+                Writing to driver_override does not automatically unbind the
+                device from its current driver or make any attempt to
+                automatically load the specified driver.  If no driver with a
+                matching name is currently loaded in the kernel, the device
+                will not bind to any driver.  This also allows devices to
Quoted 8 lines

drivers/fpga/dfl.c static int dfl_bus_match(struct device *dev,

Quoted 8 lines
Documentation/ABI/testing/sysfs-bus-dfl +3 -0
+                opt-out of driver binding using a driver_override name such as
+                "none".  Only a single driver may be specified in the override,
+                there is no support for parsing delimiters.
drivers/fpga/dfl.c +0 -0
index 511b20f..bc35750 100644
@@ -262,6 +262,10 @@ static int dfl_bus_match(struct device *dev,

struct device_driver *drv)

Quoted 11 lines

drivers/fpga/dfl.c static int dfl_bus_uevent(struct device *dev,

Quoted 11 lines
drivers/fpga/dfl.c +4 -0
 	struct dfl_driver *ddrv = to_dfl_drv(drv);
 	const struct dfl_device_id *id_entry;
 
+	/* When driver_override is set, only bind to the matching driver */
+	if (ddev->driver_override)
+		return !strcmp(ddev->driver_override, drv->name);
+
 	id_entry = ddrv->id_table;
 	if (id_entry) {
 		while (id_entry->feature_id) {
@@ -303,6 +307,53 @@ static int dfl_bus_uevent(struct device *dev,

struct kobj_uevent_env *env)

Quoted 5 lines

drivers/fpga/dfl.c static int dfl_bus_uevent(struct device *dev, struct kobj_uevent_env *env) · line 309

Quoted 3 lines
drivers/fpga/dfl.c +0 -0
 			      ddev->type, ddev->feature_id);
 }
 

I am looking at other implementations of driver_override* and looking for

consistency.

Quoted 10 lines

drivers/fpga/dfl.c static int dfl_bus_uevent(struct device *dev, struct kobj_uevent_env *env) · line 317

Quoted 9 lines
drivers/fpga/dfl.c +8 -0
+static ssize_t driver_override_show(struct device *dev,
+				    struct device_attribute *attr, char *buf)
+{
+	struct dfl_device *ddev = to_dfl_dev(dev);
+	ssize_t len;
+
+	device_lock(dev);
+	len = sprintf(buf, "%s\n", ddev->driver_override);

len = snprintf(buf, PAGE_SIZE ...

Quoted 6 lines

It is good to me.

Some bus drivers use snprintf, some use sprintf.

I think it is reasonable snprintf is used here, unlike %d, %u ... it is uncertain for the output size of %s.

you limited the size < a page in store function for driver_override?

Hao

Quoted 103 lines
Quoted 44 lines
Quoted 44 lines
drivers/fpga/dfl.c +39 -0
+	device_unlock(dev);
+	return len;
+}
+
+static ssize_t driver_override_store(struct device *dev,
+				     struct device_attribute *attr,
+				     const char *buf, size_t count)
+{
+	struct dfl_device *ddev = to_dfl_dev(dev);
+	char *driver_override, *old, *cp;
+
+	/* We need to keep extra room for a newline */
+	if (count >= (PAGE_SIZE - 1))
+		return -EINVAL;
+
+	driver_override = kstrndup(buf, count, GFP_KERNEL);
+	if (!driver_override)
+		return -ENOMEM;
+
+	cp = strchr(driver_override, '\n');
+	if (cp)
+		*cp = '\0';
+
+	device_lock(dev);
+	old = ddev->driver_override;
+	if (strlen(driver_override)) {
+		ddev->driver_override = driver_override;
+	} else {
+		kfree(driver_override);
+		ddev->driver_override = NULL;
+	}
+	device_unlock(dev);
+
+	kfree(old);
+
+	return count;
+}
+static DEVICE_ATTR_RW(driver_override);
+
 static ssize_t
 type_show(struct device *dev, struct device_attribute *attr, char *buf)
 {
@@ -324,6 +375,7 @@ static DEVICE_ATTR_RO(feature_id);
Quoted 8 lines

drivers/fpga/dfl.c static int dfl_devs_add(struct

Quoted 8 lines
drivers/fpga/dfl.c +1 -0
 static struct attribute *dfl_dev_attrs[] = {
 	&dev_attr_type.attr,
 	&dev_attr_feature_id.attr,
+	&dev_attr_driver_override.attr,
 	NULL,
 };
 ATTRIBUTE_GROUPS(dfl_dev);
@@ -469,7 +521,7 @@ static int dfl_devs_add(struct

dfl_feature_platform_data *pdata)

Quoted 11 lines

drivers/fpga/dfl.c static int dfl_devs_add(struct dfl_feature_platform_data *pdata) · line 524

Quoted 5 lines
drivers/fpga/dfl.c +1 -1
 int __dfl_driver_register(struct dfl_driver *dfl_drv, struct module *owner)
 {
-	if (!dfl_drv || !dfl_drv->probe || !dfl_drv->id_table)
+	if (!dfl_drv || !dfl_drv->probe)

id_table is still needed for the normal case.

Instead of removing this check, could you add something like

|| (!dfl_drv->is_override && !dfl_drv->id_table)

I don't think it is needed. Seems is_override and !id_table are duplicated conditions for this implementation. And it may make confusing, e.g. could a driver been force matched when is_override is not set?

I think we could make it simple, if the dfl driver didn't provide the id_table, normally it could not match any device. I think it could be easily understood by dfl driver developers.

Thanks,
Yilun

Quoted 26 lines

Tom

Quoted 23 lines
drivers/fpga/dfl.c +0 -0
 		return -EINVAL;
 
 	dfl_drv->drv.owner = owner;
include/linux/dfl.h +2 -0
index 7affba2f..e1b2471 100644
@@ -32,6 +32,7 @@ enum dfl_id_type {
  * @num_irqs: number of IRQs supported by this dfl device.
  * @cdev: pointer to DFL FPGA container device this dfl device belongs to.
  * @id_entry: matched id entry in dfl driver's id table.
+ * @driver_override: driver name to force a match
  */
 struct dfl_device {
 	struct device dev;
@@ -43,6 +44,7 @@ struct dfl_device {
 	unsigned int num_irqs;
 	struct dfl_fpga_cdev *cdev;
 	const struct dfl_device_id *id_entry;
+	char *driver_override;
 };
 
 /**
author maintainer
#5 lore raw

Re: [PATCH 1/2] fpga: dfl: add driver_override support

Quoted 123 lines · Hao
Quoted 123 lines · Tom Rix
Quoted 10 lines · Xu Yilun
Quoted 8 lines

Add support for overriding the default matching of a dfl device to a dfl driver. It follows the same way that can be used for PCI and platform devices. This patch adds the 'driver_override' sysfs file.

---
 Documentation/ABI/testing/sysfs-bus-dfl | 28 ++++++++++++++---
 drivers/fpga/dfl.c                      | 54

++++++++++++++++++++++++++++++++-

Quoted 4 lines

Review

Quoted 4 lines
 include/linux/dfl.h                     |  2 ++
 3 files changed, 79 insertions(+), 5 deletions(-)
(quoted diff) +0 -0

b/Documentation/ABI/testing/sysfs-bus-dfl

Quoted 11 lines

Documentation/ABI/testing/sysfs-bus-dfl line 5

Quoted 11 lines

index 23543be..db7e8d3 100644

Documentation/ABI/testing/sysfs-bus-dfl +2 -2
@@ -1,15 +1,35 @@
 What:/sys/bus/dfl/devices/dfl_dev.X/type
-Date:Aug 2020
-KernelVersion:5.10
+Date:Oct 2020
+KernelVersion:5.11
 Contact:Xu Yilun <[email protected]>
 Description:Read-only. It returns type of DFL FIU of the device.

Now DFL

Quoted 10 lines

Documentation/ABI/testing/sysfs-bus-dfl line 13

Quoted 10 lines
Documentation/ABI/testing/sysfs-bus-dfl +2 -2
 supports 2 FIU types, 0 for FME, 1 for PORT.
 Format: 0x%x
 
 What:/sys/bus/dfl/devices/dfl_dev.X/feature_id
-Date:Aug 2020
-KernelVersion:5.10
+Date:Oct 2020
+KernelVersion:5.11
 Contact:Xu Yilun <[email protected]>
 Description:Read-only. It returns feature identifier local to its DFL

FIU

Quoted 6 lines

Documentation/ABI/testing/sysfs-bus-dfl line 15

Quoted 2 lines
Documentation/ABI/testing/sysfs-bus-dfl +0 -0
 type.
 Format: 0x%x

These updates, do not match the comment.

Consider splitting this out.

I'm sorry it's a typo. The above code should not be changed.

Quoted 7 lines

Documentation/ABI/testing/sysfs-bus-dfl line 20

Quoted 6 lines
Documentation/ABI/testing/sysfs-bus-dfl +5 -0
+
+What:           /sys/bus/dfl/devices/.../driver_override
+Date:           Oct 2020
+KernelVersion:  5.11
+Contact:        Xu Yilun <[email protected]>

I am looking at description and trying to make it consistent with sysfs-bus-

pci

Quoted 3 lines

Documentation/ABI/testing/sysfs-bus-dfl line 21

Quoted 1 line
Documentation/ABI/testing/sysfs-bus-dfl +1 -0
+Description:    This file allows the driver for a device to be specified.

'to be specified which will override the standard dfl bus feature id to driver

mapping.'

Yes, it could be improved.

Actually now it is the "type" and "feature id" matching, the 2 fields are defined for dfl_driver.id_table. In future for dfl v1, it may be GUID matching, which will be added to id_table. So how about we make it more generic:

'to be specified which will override the standard ID table matching.'

Quoted 14 lines
Quoted 14 lines
Documentation/ABI/testing/sysfs-bus-dfl +11 -0
 When
+                specified, only a driver with a name matching the value written
+                to driver_override will have an opportunity to bind to the
+                device. The override is specified by writing a string to the
+                driver_override file (echo dfl-uio-pdev > driver_override) and
+                may be cleared with an empty string (echo > driver_override).
+                This returns the device to standard matching rules binding.
+                Writing to driver_override does not automatically unbind the
+                device from its current driver or make any attempt to
+                automatically load the specified driver.  If no driver with a
+                matching name is currently loaded in the kernel, the device
+                will not bind to any driver.  This also allows devices to
Quoted 8 lines

drivers/fpga/dfl.c static int dfl_bus_match(struct device *dev,

Quoted 8 lines
Documentation/ABI/testing/sysfs-bus-dfl +3 -0
+                opt-out of driver binding using a driver_override name such as
+                "none".  Only a single driver may be specified in the override,
+                there is no support for parsing delimiters.
drivers/fpga/dfl.c +0 -0
index 511b20f..bc35750 100644
@@ -262,6 +262,10 @@ static int dfl_bus_match(struct device *dev,

struct device_driver *drv)

Quoted 11 lines

drivers/fpga/dfl.c static int dfl_bus_uevent(struct device *dev,

Quoted 11 lines
drivers/fpga/dfl.c +4 -0
 struct dfl_driver *ddrv = to_dfl_drv(drv);
 const struct dfl_device_id *id_entry;
 
+/* When driver_override is set, only bind to the matching driver */
+if (ddev->driver_override)
+return !strcmp(ddev->driver_override, drv->name);
+
 id_entry = ddrv->id_table;
 if (id_entry) {
 while (id_entry->feature_id) {
@@ -303,6 +307,53 @@ static int dfl_bus_uevent(struct device *dev,

struct kobj_uevent_env *env)

Quoted 5 lines

drivers/fpga/dfl.c static int dfl_bus_uevent(struct device *dev, struct kobj_uevent_env *env) · line 309

Quoted 3 lines
drivers/fpga/dfl.c +0 -0
       ddev->type, ddev->feature_id);
 }
 

I am looking at other implementations of driver_override* and looking for

consistency.

Quoted 10 lines

drivers/fpga/dfl.c static int dfl_bus_uevent(struct device *dev, struct kobj_uevent_env *env) · line 317

Quoted 9 lines
drivers/fpga/dfl.c +8 -0
+static ssize_t driver_override_show(struct device *dev,
+    struct device_attribute *attr, char *buf)
+{
+struct dfl_device *ddev = to_dfl_dev(dev);
+ssize_t len;
+
+device_lock(dev);
+len = sprintf(buf, "%s\n", ddev->driver_override);

len = snprintf(buf, PAGE_SIZE ...

Quoted 8 lines
Quoted 6 lines

It is good to me.

Some bus drivers use snprintf, some use sprintf.

I think it is reasonable snprintf is used here, unlike %d, %u ... it is uncertain for the output size of %s.

you limited the size < a page in store function for driver_override?

Yes. So normally the sprintf should be OK. But I think it may be safer if the driver_override pointer is corrupted in some unexpected cases.

Thanks,
Yilun

Quoted 106 lines

Hao

Quoted 103 lines
Quoted 44 lines
Quoted 44 lines
drivers/fpga/dfl.c +39 -0
+device_unlock(dev);
+return len;
+}
+
+static ssize_t driver_override_store(struct device *dev,
+     struct device_attribute *attr,
+     const char *buf, size_t count)
+{
+struct dfl_device *ddev = to_dfl_dev(dev);
+char *driver_override, *old, *cp;
+
+/* We need to keep extra room for a newline */
+if (count >= (PAGE_SIZE - 1))
+return -EINVAL;
+
+driver_override = kstrndup(buf, count, GFP_KERNEL);
+if (!driver_override)
+return -ENOMEM;
+
+cp = strchr(driver_override, '\n');
+if (cp)
+*cp = '\0';
+
+device_lock(dev);
+old = ddev->driver_override;
+if (strlen(driver_override)) {
+ddev->driver_override = driver_override;
+} else {
+kfree(driver_override);
+ddev->driver_override = NULL;
+}
+device_unlock(dev);
+
+kfree(old);
+
+return count;
+}
+static DEVICE_ATTR_RW(driver_override);
+
 static ssize_t
 type_show(struct device *dev, struct device_attribute *attr, char *buf)
 {
@@ -324,6 +375,7 @@ static DEVICE_ATTR_RO(feature_id);
Quoted 8 lines

drivers/fpga/dfl.c static int dfl_devs_add(struct

Quoted 8 lines
drivers/fpga/dfl.c +1 -0
 static struct attribute *dfl_dev_attrs[] = {
 &dev_attr_type.attr,
 &dev_attr_feature_id.attr,
+&dev_attr_driver_override.attr,
 NULL,
 };
 ATTRIBUTE_GROUPS(dfl_dev);
@@ -469,7 +521,7 @@ static int dfl_devs_add(struct

dfl_feature_platform_data *pdata)

Quoted 11 lines

drivers/fpga/dfl.c static int dfl_devs_add(struct dfl_feature_platform_data *pdata) · line 524

Quoted 5 lines
drivers/fpga/dfl.c +1 -1
 int __dfl_driver_register(struct dfl_driver *dfl_drv, struct module *owner)
 {
-if (!dfl_drv || !dfl_drv->probe || !dfl_drv->id_table)
+if (!dfl_drv || !dfl_drv->probe)

id_table is still needed for the normal case.

Instead of removing this check, could you add something like

|| (!dfl_drv->is_override && !dfl_drv->id_table)

I don't think it is needed. Seems is_override and !id_table are duplicated conditions for this implementation. And it may make confusing, e.g. could a driver been force matched when is_override is not set?

I think we could make it simple, if the dfl driver didn't provide the id_table, normally it could not match any device. I think it could be easily understood by dfl driver developers.

Thanks,
Yilun

Quoted 26 lines

Tom

Quoted 23 lines
drivers/fpga/dfl.c +0 -0
 return -EINVAL;
 
 dfl_drv->drv.owner = owner;
include/linux/dfl.h +2 -0
index 7affba2f..e1b2471 100644
@@ -32,6 +32,7 @@ enum dfl_id_type {
  * @num_irqs: number of IRQs supported by this dfl device.
  * @cdev: pointer to DFL FPGA container device this dfl device belongs to.
  * @id_entry: matched id entry in dfl driver's id table.
+ * @driver_override: driver name to force a match
  */
 struct dfl_device {
 struct device dev;
@@ -43,6 +44,7 @@ struct dfl_device {
 unsigned int num_irqs;
 struct dfl_fpga_cdev *cdev;
 const struct dfl_device_id *id_entry;
+char *driver_override;
 };
 
 /**
#6 lore raw

Re: [PATCH 1/2] fpga: dfl: add driver_override support

Quoted 124 lines · Xu Yilun
Quoted 123 lines · Hao
Quoted 123 lines · Tom Rix
Quoted 10 lines · Xu Yilun
Quoted 8 lines

Add support for overriding the default matching of a dfl device to a dfl driver. It follows the same way that can be used for PCI and platform devices. This patch adds the 'driver_override' sysfs file.

---
 Documentation/ABI/testing/sysfs-bus-dfl | 28 ++++++++++++++---
 drivers/fpga/dfl.c                      | 54

++++++++++++++++++++++++++++++++-

Quoted 4 lines

Review

Quoted 4 lines
 include/linux/dfl.h                     |  2 ++
 3 files changed, 79 insertions(+), 5 deletions(-)
(quoted diff) +0 -0

b/Documentation/ABI/testing/sysfs-bus-dfl

Quoted 11 lines

Documentation/ABI/testing/sysfs-bus-dfl line 5

Quoted 11 lines

index 23543be..db7e8d3 100644

Documentation/ABI/testing/sysfs-bus-dfl +2 -2
@@ -1,15 +1,35 @@
 What:/sys/bus/dfl/devices/dfl_dev.X/type
-Date:Aug 2020
-KernelVersion:5.10
+Date:Oct 2020
+KernelVersion:5.11
 Contact:Xu Yilun <[email protected]>
 Description:Read-only. It returns type of DFL FIU of the device.

Now DFL

Quoted 10 lines

Documentation/ABI/testing/sysfs-bus-dfl line 13

Quoted 10 lines
Documentation/ABI/testing/sysfs-bus-dfl +2 -2
 supports 2 FIU types, 0 for FME, 1 for PORT.
 Format: 0x%x
 
 What:/sys/bus/dfl/devices/dfl_dev.X/feature_id
-Date:Aug 2020
-KernelVersion:5.10
+Date:Oct 2020
+KernelVersion:5.11
 Contact:Xu Yilun <[email protected]>
 Description:Read-only. It returns feature identifier local to its DFL

FIU

Quoted 6 lines

Documentation/ABI/testing/sysfs-bus-dfl line 15

Quoted 2 lines
Documentation/ABI/testing/sysfs-bus-dfl +0 -0
 type.
 Format: 0x%x

These updates, do not match the comment.

Consider splitting this out.

I'm sorry it's a typo. The above code should not be changed.

Quoted 7 lines

Documentation/ABI/testing/sysfs-bus-dfl line 20

Quoted 6 lines
Documentation/ABI/testing/sysfs-bus-dfl +5 -0
+
+What:           /sys/bus/dfl/devices/.../driver_override
+Date:           Oct 2020
+KernelVersion:  5.11
+Contact:        Xu Yilun <[email protected]>

I am looking at description and trying to make it consistent with sysfs-bus-

pci

Quoted 3 lines

Documentation/ABI/testing/sysfs-bus-dfl line 21

Quoted 1 line
Documentation/ABI/testing/sysfs-bus-dfl +1 -0
+Description:    This file allows the driver for a device to be specified.

'to be specified which will override the standard dfl bus feature id to driver

mapping.'

Yes, it could be improved.

Actually now it is the "type" and "feature id" matching, the 2 fields are defined for dfl_driver.id_table. In future for dfl v1, it may be GUID matching, which will be added to id_table. So how about we make it more generic:

'to be specified which will override the standard ID table matching.'

Quoted 14 lines
Quoted 14 lines
Documentation/ABI/testing/sysfs-bus-dfl +11 -0
 When
+                specified, only a driver with a name matching the value written
+                to driver_override will have an opportunity to bind to the
+                device. The override is specified by writing a string to the
+                driver_override file (echo dfl-uio-pdev > driver_override) and
+                may be cleared with an empty string (echo > driver_override).
+                This returns the device to standard matching rules binding.
+                Writing to driver_override does not automatically unbind the
+                device from its current driver or make any attempt to
+                automatically load the specified driver.  If no driver with a
+                matching name is currently loaded in the kernel, the device
+                will not bind to any driver.  This also allows devices to
Quoted 8 lines

drivers/fpga/dfl.c static int dfl_bus_match(struct device *dev,

Quoted 8 lines
Documentation/ABI/testing/sysfs-bus-dfl +3 -0
+                opt-out of driver binding using a driver_override name such as
+                "none".  Only a single driver may be specified in the override,
+                there is no support for parsing delimiters.
drivers/fpga/dfl.c +0 -0
index 511b20f..bc35750 100644
@@ -262,6 +262,10 @@ static int dfl_bus_match(struct device *dev,

struct device_driver *drv)

Quoted 11 lines

drivers/fpga/dfl.c static int dfl_bus_uevent(struct device *dev,

Quoted 11 lines
drivers/fpga/dfl.c +4 -0
 struct dfl_driver *ddrv = to_dfl_drv(drv);
 const struct dfl_device_id *id_entry;
 
+/* When driver_override is set, only bind to the matching driver */
+if (ddev->driver_override)
+return !strcmp(ddev->driver_override, drv->name);
+
 id_entry = ddrv->id_table;
 if (id_entry) {
 while (id_entry->feature_id) {
@@ -303,6 +307,53 @@ static int dfl_bus_uevent(struct device *dev,

struct kobj_uevent_env *env)

Quoted 5 lines

drivers/fpga/dfl.c static int dfl_bus_uevent(struct device *dev, struct kobj_uevent_env *env) · line 309

Quoted 3 lines
drivers/fpga/dfl.c +0 -0
       ddev->type, ddev->feature_id);
 }
 

I am looking at other implementations of driver_override* and looking for

consistency.

Quoted 10 lines

drivers/fpga/dfl.c static int dfl_bus_uevent(struct device *dev, struct kobj_uevent_env *env) · line 317

Quoted 9 lines
drivers/fpga/dfl.c +8 -0
+static ssize_t driver_override_show(struct device *dev,
+    struct device_attribute *attr, char *buf)
+{
+struct dfl_device *ddev = to_dfl_dev(dev);
+ssize_t len;
+
+device_lock(dev);
+len = sprintf(buf, "%s\n", ddev->driver_override);

len = snprintf(buf, PAGE_SIZE ...

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

It is good to me.

Some bus drivers use snprintf, some use sprintf.

I think it is reasonable snprintf is used here, unlike %d, %u ... it is uncertain for the output size of %s.

No, no one should care at all about this.

If you are caring, then you are using sysfs wrong.

Just use sprintf(), you never care about the size of the buffer for a show/store function.

Or better yet, use the new sysfs_emit() call, that is what everything will be converted over to anyway.

Quoted 4 lines
Quoted 1 line

you limited the size < a page in store function for driver_override?

Yes. So normally the sprintf should be OK. But I think it may be safer if the driver_override pointer is corrupted in some unexpected cases.

How can that happen?

greg k-h

author maintainer
#7 lore raw

Re: [PATCH 1/2] fpga: dfl: add driver_override support

In reply to Xu Yilun
Quoted 114 lines · Xu Yilun
Quoted 40 lines · Tom Rix
Quoted 25 lines · Xu Yilun

Add support for overriding the default matching of a dfl device to a dfl driver. It follows the same way that can be used for PCI and platform devices. This patch adds the 'driver_override' sysfs file.

---
 Documentation/ABI/testing/sysfs-bus-dfl | 28 ++++++++++++++---
 drivers/fpga/dfl.c                      | 54 ++++++++++++++++++++++++++++++++-
 include/linux/dfl.h                     |  2 ++
 3 files changed, 79 insertions(+), 5 deletions(-)
Documentation/ABI/testing/sysfs-bus-dfl +2 -2
index 23543be..db7e8d3 100644
@@ -1,15 +1,35 @@
 What:		/sys/bus/dfl/devices/dfl_dev.X/type
-Date:		Aug 2020
-KernelVersion:	5.10
+Date:		Oct 2020
+KernelVersion:	5.11
 Contact:	Xu Yilun <[email protected]>
 Description:	Read-only. It returns type of DFL FIU of the device. Now DFL
 		supports 2 FIU types, 0 for FME, 1 for PORT.
 		Format: 0x%x

Documentation/ABI/testing/sysfs-bus-dfl line 15

Quoted 10 lines
Documentation/ABI/testing/sysfs-bus-dfl +2 -2
 
 What:		/sys/bus/dfl/devices/dfl_dev.X/feature_id
-Date:		Aug 2020
-KernelVersion:	5.10
+Date:		Oct 2020
+KernelVersion:	5.11
 Contact:	Xu Yilun <[email protected]>
 Description:	Read-only. It returns feature identifier local to its DFL FIU
 		type.
 		Format: 0x%x

These updates, do not match the comment.

Quoted 1 line

Consider splitting this out.

I'm sorry it's a typo. The above code should not be changed.

Quoted 10 lines

Documentation/ABI/testing/sysfs-bus-dfl line 20

Quoted 6 lines
Documentation/ABI/testing/sysfs-bus-dfl +5 -0
+
+What:           /sys/bus/dfl/devices/.../driver_override
+Date:           Oct 2020
+KernelVersion:  5.11
+Contact:        Xu Yilun <[email protected]>

I am looking at description and trying to make it consistent with sysfs-bus-pci

Documentation/ABI/testing/sysfs-bus-dfl line 21

Quoted 1 line
Documentation/ABI/testing/sysfs-bus-dfl +1 -0
+Description:    This file allows the driver for a device to be specified.

'to be specified which will override the standard dfl bus feature id to driver mapping.'

Yes, it could be improved.

Actually now it is the "type" and "feature id" matching, the 2 fields are defined for dfl_driver.id_table. In future for dfl v1, it may be GUID matching, which will be added to id_table. So how about we make it more generic:

'to be specified which will override the standard ID table matching.'

Quoted 39 lines
Quoted 33 lines
Documentation/ABI/testing/sysfs-bus-dfl +14 -0
 When
+                specified, only a driver with a name matching the value written
+                to driver_override will have an opportunity to bind to the
+                device. The override is specified by writing a string to the
+                driver_override file (echo dfl-uio-pdev > driver_override) and
+                may be cleared with an empty string (echo > driver_override).
+                This returns the device to standard matching rules binding.
+                Writing to driver_override does not automatically unbind the
+                device from its current driver or make any attempt to
+                automatically load the specified driver.  If no driver with a
+                matching name is currently loaded in the kernel, the device
+                will not bind to any driver.  This also allows devices to
+                opt-out of driver binding using a driver_override name such as
+                "none".  Only a single driver may be specified in the override,
+                there is no support for parsing delimiters.
drivers/fpga/dfl.c +4 -0
index 511b20f..bc35750 100644
@@ -262,6 +262,10 @@ static int dfl_bus_match(struct device *dev, struct device_driver *drv)
 	struct dfl_driver *ddrv = to_dfl_drv(drv);
 	const struct dfl_device_id *id_entry;
 
+	/* When driver_override is set, only bind to the matching driver */
+	if (ddev->driver_override)
+		return !strcmp(ddev->driver_override, drv->name);
+
 	id_entry = ddrv->id_table;
 	if (id_entry) {
 		while (id_entry->feature_id) {

drivers/fpga/dfl.c static int dfl_bus_uevent(struct device *dev, struct kobj_uevent_env *env) · line 309

Quoted 3 lines
drivers/fpga/dfl.c +0 -0
@@ -303,6 +307,53 @@ static int dfl_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
 			      ddev->type, ddev->feature_id);
 }
 

I am looking at other implementations of driver_override* and looking for consistency.

Quoted 9 lines

drivers/fpga/dfl.c static int dfl_bus_uevent(struct device *dev, struct kobj_uevent_env *env) · line 317

Quoted 8 lines
drivers/fpga/dfl.c +8 -0
+static ssize_t driver_override_show(struct device *dev,
+				    struct device_attribute *attr, char *buf)
+{
+	struct dfl_device *ddev = to_dfl_dev(dev);
+	ssize_t len;
+
+	device_lock(dev);
+	len = sprintf(buf, "%s\n", ddev->driver_override);

len = snprintf(buf, PAGE_SIZE ...

Quoted 6 lines

It is good to me.

Some bus drivers use snprintf, some use sprintf.

I think it is reasonable snprintf is used here, unlike %d, %u ... it is uncertain for the output size of %s.

Sorry, I checked the Documentation/filesystems/sysfs.rst again and found I didn't remember it correctly.

The snprintf is must not be used, sprintf() or scnprintf() should be used. So I prefer sprintf() here, following other implementations.

sprintf() should be safe here, as we already limited the input of driver_override string to less then PAGE_SIZE on store().

Thanks,
Yilun

Quoted 102 lines
Quoted 60 lines
Quoted 52 lines
drivers/fpga/dfl.c +40 -0
+	device_unlock(dev);
+	return len;
+}
+
+static ssize_t driver_override_store(struct device *dev,
+				     struct device_attribute *attr,
+				     const char *buf, size_t count)
+{
+	struct dfl_device *ddev = to_dfl_dev(dev);
+	char *driver_override, *old, *cp;
+
+	/* We need to keep extra room for a newline */
+	if (count >= (PAGE_SIZE - 1))
+		return -EINVAL;
+
+	driver_override = kstrndup(buf, count, GFP_KERNEL);
+	if (!driver_override)
+		return -ENOMEM;
+
+	cp = strchr(driver_override, '\n');
+	if (cp)
+		*cp = '\0';
+
+	device_lock(dev);
+	old = ddev->driver_override;
+	if (strlen(driver_override)) {
+		ddev->driver_override = driver_override;
+	} else {
+		kfree(driver_override);
+		ddev->driver_override = NULL;
+	}
+	device_unlock(dev);
+
+	kfree(old);
+
+	return count;
+}
+static DEVICE_ATTR_RW(driver_override);
+
 static ssize_t
 type_show(struct device *dev, struct device_attribute *attr, char *buf)
 {
@@ -324,6 +375,7 @@ static DEVICE_ATTR_RO(feature_id);
 static struct attribute *dfl_dev_attrs[] = {
 	&dev_attr_type.attr,
 	&dev_attr_feature_id.attr,
+	&dev_attr_driver_override.attr,
 	NULL,
 };
 ATTRIBUTE_GROUPS(dfl_dev);

drivers/fpga/dfl.c static int dfl_devs_add(struct dfl_feature_platform_data *pdata) · line 524

Quoted 5 lines
drivers/fpga/dfl.c +1 -1
@@ -469,7 +521,7 @@ static int dfl_devs_add(struct dfl_feature_platform_data *pdata)
 
 int __dfl_driver_register(struct dfl_driver *dfl_drv, struct module *owner)
 {
-	if (!dfl_drv || !dfl_drv->probe || !dfl_drv->id_table)
+	if (!dfl_drv || !dfl_drv->probe)

id_table is still needed for the normal case.

Quoted 3 lines

Instead of removing this check, could you add something like

|| (!dfl_drv->is_override && !dfl_drv->id_table)

I don't think it is needed. Seems is_override and !id_table are duplicated conditions for this implementation. And it may make confusing, e.g. could a driver been force matched when is_override is not set?

I think we could make it simple, if the dfl driver didn't provide the id_table, normally it could not match any device. I think it could be easily understood by dfl driver developers.

Thanks,
Yilun

Quoted 26 lines

Tom

Quoted 23 lines
drivers/fpga/dfl.c +0 -0
 		return -EINVAL;
 
 	dfl_drv->drv.owner = owner;
include/linux/dfl.h +2 -0
index 7affba2f..e1b2471 100644
@@ -32,6 +32,7 @@ enum dfl_id_type {
  * @num_irqs: number of IRQs supported by this dfl device.
  * @cdev: pointer to DFL FPGA container device this dfl device belongs to.
  * @id_entry: matched id entry in dfl driver's id table.
+ * @driver_override: driver name to force a match
  */
 struct dfl_device {
 	struct device dev;
@@ -43,6 +44,7 @@ struct dfl_device {
 	unsigned int num_irqs;
 	struct dfl_fpga_cdev *cdev;
 	const struct dfl_device_id *id_entry;
+	char *driver_override;
 };
 
 /**
#8 lore raw

Re: [PATCH 1/2] fpga: dfl: add driver_override support

Quoted 122 lines · Xu Yilun
Quoted 114 lines · Xu Yilun
Quoted 40 lines · Tom Rix
Quoted 25 lines · Xu Yilun

Add support for overriding the default matching of a dfl device to a dfl driver. It follows the same way that can be used for PCI and platform devices. This patch adds the 'driver_override' sysfs file.

---
 Documentation/ABI/testing/sysfs-bus-dfl | 28 ++++++++++++++---
 drivers/fpga/dfl.c                      | 54 ++++++++++++++++++++++++++++++++-
 include/linux/dfl.h                     |  2 ++
 3 files changed, 79 insertions(+), 5 deletions(-)
Documentation/ABI/testing/sysfs-bus-dfl +2 -2
index 23543be..db7e8d3 100644
@@ -1,15 +1,35 @@
 What:		/sys/bus/dfl/devices/dfl_dev.X/type
-Date:		Aug 2020
-KernelVersion:	5.10
+Date:		Oct 2020
+KernelVersion:	5.11
 Contact:	Xu Yilun <[email protected]>
 Description:	Read-only. It returns type of DFL FIU of the device. Now DFL
 		supports 2 FIU types, 0 for FME, 1 for PORT.
 		Format: 0x%x

Documentation/ABI/testing/sysfs-bus-dfl line 15

Quoted 10 lines
Documentation/ABI/testing/sysfs-bus-dfl +2 -2
 
 What:		/sys/bus/dfl/devices/dfl_dev.X/feature_id
-Date:		Aug 2020
-KernelVersion:	5.10
+Date:		Oct 2020
+KernelVersion:	5.11
 Contact:	Xu Yilun <[email protected]>
 Description:	Read-only. It returns feature identifier local to its DFL FIU
 		type.
 		Format: 0x%x

These updates, do not match the comment.

Quoted 1 line

Consider splitting this out.

I'm sorry it's a typo. The above code should not be changed.

Quoted 10 lines

Documentation/ABI/testing/sysfs-bus-dfl line 20

Quoted 6 lines
Documentation/ABI/testing/sysfs-bus-dfl +5 -0
+
+What:           /sys/bus/dfl/devices/.../driver_override
+Date:           Oct 2020
+KernelVersion:  5.11
+Contact:        Xu Yilun <[email protected]>

I am looking at description and trying to make it consistent with sysfs-bus-pci

Documentation/ABI/testing/sysfs-bus-dfl line 21

Quoted 1 line
Documentation/ABI/testing/sysfs-bus-dfl +1 -0
+Description:    This file allows the driver for a device to be specified.

'to be specified which will override the standard dfl bus feature id to driver mapping.'

Yes, it could be improved.

Actually now it is the "type" and "feature id" matching, the 2 fields are defined for dfl_driver.id_table. In future for dfl v1, it may be GUID matching, which will be added to id_table. So how about we make it more generic:

'to be specified which will override the standard ID table matching.'

Quoted 39 lines
Quoted 33 lines
Documentation/ABI/testing/sysfs-bus-dfl +14 -0
 When
+                specified, only a driver with a name matching the value written
+                to driver_override will have an opportunity to bind to the
+                device. The override is specified by writing a string to the
+                driver_override file (echo dfl-uio-pdev > driver_override) and
+                may be cleared with an empty string (echo > driver_override).
+                This returns the device to standard matching rules binding.
+                Writing to driver_override does not automatically unbind the
+                device from its current driver or make any attempt to
+                automatically load the specified driver.  If no driver with a
+                matching name is currently loaded in the kernel, the device
+                will not bind to any driver.  This also allows devices to
+                opt-out of driver binding using a driver_override name such as
+                "none".  Only a single driver may be specified in the override,
+                there is no support for parsing delimiters.
drivers/fpga/dfl.c +4 -0
index 511b20f..bc35750 100644
@@ -262,6 +262,10 @@ static int dfl_bus_match(struct device *dev, struct device_driver *drv)
 	struct dfl_driver *ddrv = to_dfl_drv(drv);
 	const struct dfl_device_id *id_entry;
 
+	/* When driver_override is set, only bind to the matching driver */
+	if (ddev->driver_override)
+		return !strcmp(ddev->driver_override, drv->name);
+
 	id_entry = ddrv->id_table;
 	if (id_entry) {
 		while (id_entry->feature_id) {

drivers/fpga/dfl.c static int dfl_bus_uevent(struct device *dev, struct kobj_uevent_env *env) · line 309

Quoted 3 lines
drivers/fpga/dfl.c +0 -0
@@ -303,6 +307,53 @@ static int dfl_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
 			      ddev->type, ddev->feature_id);
 }
 

I am looking at other implementations of driver_override* and looking for consistency.

Quoted 9 lines

drivers/fpga/dfl.c static int dfl_bus_uevent(struct device *dev, struct kobj_uevent_env *env) · line 317

Quoted 8 lines
drivers/fpga/dfl.c +8 -0
+static ssize_t driver_override_show(struct device *dev,
+				    struct device_attribute *attr, char *buf)
+{
+	struct dfl_device *ddev = to_dfl_dev(dev);
+	ssize_t len;
+
+	device_lock(dev);
+	len = sprintf(buf, "%s\n", ddev->driver_override);

len = snprintf(buf, PAGE_SIZE ...

Quoted 6 lines

It is good to me.

Some bus drivers use snprintf, some use sprintf.

I think it is reasonable snprintf is used here, unlike %d, %u ... it is uncertain for the output size of %s.

Quoted 8 lines

Sorry, I checked the Documentation/filesystems/sysfs.rst again and found I didn't remember it correctly.

The snprintf is must not be used, sprintf() or scnprintf() should be used. So I prefer sprintf() here, following other implementations.

sprintf() should be safe here, as we already limited the input of driver_override string to less then PAGE_SIZE on store().

As already mentioned, please use sysfs_emit() instead.

thanks,

greg k-h

author maintainer
#9 lore raw

Re: [PATCH 1/2] fpga: dfl: add driver_override support

Quoted 126 lines · Greg KH
Quoted 125 lines · Xu Yilun
Quoted 114 lines · Xu Yilun
Quoted 40 lines · Tom Rix
Quoted 25 lines · Xu Yilun

Add support for overriding the default matching of a dfl device to a dfl driver. It follows the same way that can be used for PCI and platform devices. This patch adds the 'driver_override' sysfs file.

---
 Documentation/ABI/testing/sysfs-bus-dfl | 28 ++++++++++++++---
 drivers/fpga/dfl.c                      | 54 ++++++++++++++++++++++++++++++++-
 include/linux/dfl.h                     |  2 ++
 3 files changed, 79 insertions(+), 5 deletions(-)
Documentation/ABI/testing/sysfs-bus-dfl +2 -2
index 23543be..db7e8d3 100644
@@ -1,15 +1,35 @@
 What:		/sys/bus/dfl/devices/dfl_dev.X/type
-Date:		Aug 2020
-KernelVersion:	5.10
+Date:		Oct 2020
+KernelVersion:	5.11
 Contact:	Xu Yilun <[email protected]>
 Description:	Read-only. It returns type of DFL FIU of the device. Now DFL
 		supports 2 FIU types, 0 for FME, 1 for PORT.
 		Format: 0x%x

Documentation/ABI/testing/sysfs-bus-dfl line 15

Quoted 10 lines
Documentation/ABI/testing/sysfs-bus-dfl +2 -2
 
 What:		/sys/bus/dfl/devices/dfl_dev.X/feature_id
-Date:		Aug 2020
-KernelVersion:	5.10
+Date:		Oct 2020
+KernelVersion:	5.11
 Contact:	Xu Yilun <[email protected]>
 Description:	Read-only. It returns feature identifier local to its DFL FIU
 		type.
 		Format: 0x%x

These updates, do not match the comment.

Quoted 1 line

Consider splitting this out.

I'm sorry it's a typo. The above code should not be changed.

Quoted 10 lines

Documentation/ABI/testing/sysfs-bus-dfl line 20

Quoted 6 lines
Documentation/ABI/testing/sysfs-bus-dfl +5 -0
+
+What:           /sys/bus/dfl/devices/.../driver_override
+Date:           Oct 2020
+KernelVersion:  5.11
+Contact:        Xu Yilun <[email protected]>

I am looking at description and trying to make it consistent with sysfs-bus-pci

Documentation/ABI/testing/sysfs-bus-dfl line 21

Quoted 1 line
Documentation/ABI/testing/sysfs-bus-dfl +1 -0
+Description:    This file allows the driver for a device to be specified.

'to be specified which will override the standard dfl bus feature id to driver mapping.'

Yes, it could be improved.

Actually now it is the "type" and "feature id" matching, the 2 fields are defined for dfl_driver.id_table. In future for dfl v1, it may be GUID matching, which will be added to id_table. So how about we make it more generic:

'to be specified which will override the standard ID table matching.'

Quoted 39 lines
Quoted 33 lines
Documentation/ABI/testing/sysfs-bus-dfl +14 -0
 When
+                specified, only a driver with a name matching the value written
+                to driver_override will have an opportunity to bind to the
+                device. The override is specified by writing a string to the
+                driver_override file (echo dfl-uio-pdev > driver_override) and
+                may be cleared with an empty string (echo > driver_override).
+                This returns the device to standard matching rules binding.
+                Writing to driver_override does not automatically unbind the
+                device from its current driver or make any attempt to
+                automatically load the specified driver.  If no driver with a
+                matching name is currently loaded in the kernel, the device
+                will not bind to any driver.  This also allows devices to
+                opt-out of driver binding using a driver_override name such as
+                "none".  Only a single driver may be specified in the override,
+                there is no support for parsing delimiters.
drivers/fpga/dfl.c +4 -0
index 511b20f..bc35750 100644
@@ -262,6 +262,10 @@ static int dfl_bus_match(struct device *dev, struct device_driver *drv)
 	struct dfl_driver *ddrv = to_dfl_drv(drv);
 	const struct dfl_device_id *id_entry;
 
+	/* When driver_override is set, only bind to the matching driver */
+	if (ddev->driver_override)
+		return !strcmp(ddev->driver_override, drv->name);
+
 	id_entry = ddrv->id_table;
 	if (id_entry) {
 		while (id_entry->feature_id) {

drivers/fpga/dfl.c static int dfl_bus_uevent(struct device *dev, struct kobj_uevent_env *env) · line 309

Quoted 3 lines
drivers/fpga/dfl.c +0 -0
@@ -303,6 +307,53 @@ static int dfl_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
 			      ddev->type, ddev->feature_id);
 }
 

I am looking at other implementations of driver_override* and looking for consistency.

Quoted 9 lines

drivers/fpga/dfl.c static int dfl_bus_uevent(struct device *dev, struct kobj_uevent_env *env) · line 317

Quoted 8 lines
drivers/fpga/dfl.c +8 -0
+static ssize_t driver_override_show(struct device *dev,
+				    struct device_attribute *attr, char *buf)
+{
+	struct dfl_device *ddev = to_dfl_dev(dev);
+	ssize_t len;
+
+	device_lock(dev);
+	len = sprintf(buf, "%s\n", ddev->driver_override);

len = snprintf(buf, PAGE_SIZE ...

Quoted 6 lines

It is good to me.

Some bus drivers use snprintf, some use sprintf.

I think it is reasonable snprintf is used here, unlike %d, %u ... it is uncertain for the output size of %s.

Sorry, I checked the Documentation/filesystems/sysfs.rst again and found I didn't remember it correctly.

Quoted 7 lines
Quoted 5 lines

The snprintf is must not be used, sprintf() or scnprintf() should be used. So I prefer sprintf() here, following other implementations.

sprintf() should be safe here, as we already limited the input of driver_override string to less then PAGE_SIZE on store().

As already mentioned, please use sysfs_emit() instead.

Yes, I'll do it.

Thanks,
Yilun

Quoted 4 lines

thanks,

greg k-h

reviewer
#10 lore raw

Re: [PATCH 1/2] fpga: dfl: add driver_override support

In reply to Xu Yilun
Quoted 50 lines · Xu Yilun
Quoted 38 lines · Tom Rix
Quoted 25 lines · Xu Yilun

Add support for overriding the default matching of a dfl device to a dfl driver. It follows the same way that can be used for PCI and platform devices. This patch adds the 'driver_override' sysfs file.

---
 Documentation/ABI/testing/sysfs-bus-dfl | 28 ++++++++++++++---
 drivers/fpga/dfl.c                      | 54 ++++++++++++++++++++++++++++++++-
 include/linux/dfl.h                     |  2 ++
 3 files changed, 79 insertions(+), 5 deletions(-)
Documentation/ABI/testing/sysfs-bus-dfl +2 -2
index 23543be..db7e8d3 100644
@@ -1,15 +1,35 @@
 What:		/sys/bus/dfl/devices/dfl_dev.X/type
-Date:		Aug 2020
-KernelVersion:	5.10
+Date:		Oct 2020
+KernelVersion:	5.11
 Contact:	Xu Yilun <[email protected]>
 Description:	Read-only. It returns type of DFL FIU of the device. Now DFL
 		supports 2 FIU types, 0 for FME, 1 for PORT.
 		Format: 0x%x

Documentation/ABI/testing/sysfs-bus-dfl line 15

Quoted 10 lines
Documentation/ABI/testing/sysfs-bus-dfl +2 -2
 
 What:		/sys/bus/dfl/devices/dfl_dev.X/feature_id
-Date:		Aug 2020
-KernelVersion:	5.10
+Date:		Oct 2020
+KernelVersion:	5.11
 Contact:	Xu Yilun <[email protected]>
 Description:	Read-only. It returns feature identifier local to its DFL FIU
 		type.
 		Format: 0x%x

These updates, do not match the comment.

Quoted 1 line

Consider splitting this out.

I'm sorry it's a typo. The above code should not be changed.

Quoted 8 lines

Documentation/ABI/testing/sysfs-bus-dfl line 20

Quoted 5 lines
Documentation/ABI/testing/sysfs-bus-dfl +5 -0
+
+What:           /sys/bus/dfl/devices/.../driver_override
+Date:           Oct 2020
+KernelVersion:  5.11
+Contact:        Xu Yilun <[email protected]>

I am looking at description and trying to make it consistent with sysfs-bus-pci

Documentation/ABI/testing/sysfs-bus-dfl line 21

Quoted 1 line
Documentation/ABI/testing/sysfs-bus-dfl +1 -0
+Description:    This file allows the driver for a device to be specified.

'to be specified which will override the standard dfl bus feature id to driver mapping.'

Quoted 8 lines

Yes, it could be improved.

Actually now it is the "type" and "feature id" matching, the 2 fields are defined for dfl_driver.id_table. In future for dfl v1, it may be GUID matching, which will be added to id_table. So how about we make it more generic:

'to be specified which will override the standard ID table matching.'

yes, this is good.

Quoted 47 lines
Quoted 38 lines
Quoted 33 lines
Documentation/ABI/testing/sysfs-bus-dfl +14 -0
 When
+                specified, only a driver with a name matching the value written
+                to driver_override will have an opportunity to bind to the
+                device. The override is specified by writing a string to the
+                driver_override file (echo dfl-uio-pdev > driver_override) and
+                may be cleared with an empty string (echo > driver_override).
+                This returns the device to standard matching rules binding.
+                Writing to driver_override does not automatically unbind the
+                device from its current driver or make any attempt to
+                automatically load the specified driver.  If no driver with a
+                matching name is currently loaded in the kernel, the device
+                will not bind to any driver.  This also allows devices to
+                opt-out of driver binding using a driver_override name such as
+                "none".  Only a single driver may be specified in the override,
+                there is no support for parsing delimiters.
drivers/fpga/dfl.c +4 -0
index 511b20f..bc35750 100644
@@ -262,6 +262,10 @@ static int dfl_bus_match(struct device *dev, struct device_driver *drv)
 	struct dfl_driver *ddrv = to_dfl_drv(drv);
 	const struct dfl_device_id *id_entry;
 
+	/* When driver_override is set, only bind to the matching driver */
+	if (ddev->driver_override)
+		return !strcmp(ddev->driver_override, drv->name);
+
 	id_entry = ddrv->id_table;
 	if (id_entry) {
 		while (id_entry->feature_id) {

drivers/fpga/dfl.c static int dfl_bus_uevent(struct device *dev, struct kobj_uevent_env *env) · line 309

Quoted 3 lines
drivers/fpga/dfl.c +0 -0
@@ -303,6 +307,53 @@ static int dfl_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
 			      ddev->type, ddev->feature_id);
 }
 

I am looking at other implementations of driver_override* and looking for consistency.

Quoted 9 lines

drivers/fpga/dfl.c static int dfl_bus_uevent(struct device *dev, struct kobj_uevent_env *env) · line 317

Quoted 8 lines
drivers/fpga/dfl.c +8 -0
+static ssize_t driver_override_show(struct device *dev,
+				    struct device_attribute *attr, char *buf)
+{
+	struct dfl_device *ddev = to_dfl_dev(dev);
+	ssize_t len;
+
+	device_lock(dev);
+	len = sprintf(buf, "%s\n", ddev->driver_override);

len = snprintf(buf, PAGE_SIZE ...

Quoted 6 lines

It is good to me.

Some bus drivers use snprintf, some use sprintf.

I think it is reasonable snprintf is used here, unlike %d, %u ... it is uncertain for the output size of %s.

I am not sure if you are agreeing to do this change.

I was looking at the other driver_override_show() functions.

Generally the 'n' variants of string functions should be used.

Quoted 61 lines
Quoted 61 lines
Quoted 52 lines
drivers/fpga/dfl.c +40 -0
+	device_unlock(dev);
+	return len;
+}
+
+static ssize_t driver_override_store(struct device *dev,
+				     struct device_attribute *attr,
+				     const char *buf, size_t count)
+{
+	struct dfl_device *ddev = to_dfl_dev(dev);
+	char *driver_override, *old, *cp;
+
+	/* We need to keep extra room for a newline */
+	if (count >= (PAGE_SIZE - 1))
+		return -EINVAL;
+
+	driver_override = kstrndup(buf, count, GFP_KERNEL);
+	if (!driver_override)
+		return -ENOMEM;
+
+	cp = strchr(driver_override, '\n');
+	if (cp)
+		*cp = '\0';
+
+	device_lock(dev);
+	old = ddev->driver_override;
+	if (strlen(driver_override)) {
+		ddev->driver_override = driver_override;
+	} else {
+		kfree(driver_override);
+		ddev->driver_override = NULL;
+	}
+	device_unlock(dev);
+
+	kfree(old);
+
+	return count;
+}
+static DEVICE_ATTR_RW(driver_override);
+
 static ssize_t
 type_show(struct device *dev, struct device_attribute *attr, char *buf)
 {
@@ -324,6 +375,7 @@ static DEVICE_ATTR_RO(feature_id);
 static struct attribute *dfl_dev_attrs[] = {
 	&dev_attr_type.attr,
 	&dev_attr_feature_id.attr,
+	&dev_attr_driver_override.attr,
 	NULL,
 };
 ATTRIBUTE_GROUPS(dfl_dev);

drivers/fpga/dfl.c static int dfl_devs_add(struct dfl_feature_platform_data *pdata) · line 524

Quoted 5 lines
drivers/fpga/dfl.c +1 -1
@@ -469,7 +521,7 @@ static int dfl_devs_add(struct dfl_feature_platform_data *pdata)
 
 int __dfl_driver_register(struct dfl_driver *dfl_drv, struct module *owner)
 {
-	if (!dfl_drv || !dfl_drv->probe || !dfl_drv->id_table)
+	if (!dfl_drv || !dfl_drv->probe)

id_table is still needed for the normal case.

Instead of removing this check, could you add something like

Quoted 9 lines
Quoted 1 line

|| (!dfl_drv->is_override && !dfl_drv->id_table)

I don't think it is needed. Seems is_override and !id_table are duplicated conditions for this implementation. And it may make confusing, e.g. could a driver been force matched when is_override is not set?

I think we could make it simple, if the dfl driver didn't provide the id_table, normally it could not match any device. I think it could be easily understood by dfl driver developers.

Then an ASSERT should be added in dfl_bus_match() for id_entry.

Tom

Quoted 28 lines

Thanks,
Yilun

Quoted 25 lines

Tom

Quoted 23 lines
drivers/fpga/dfl.c +0 -0
 		return -EINVAL;
 
 	dfl_drv->drv.owner = owner;
include/linux/dfl.h +2 -0
index 7affba2f..e1b2471 100644
@@ -32,6 +32,7 @@ enum dfl_id_type {
  * @num_irqs: number of IRQs supported by this dfl device.
  * @cdev: pointer to DFL FPGA container device this dfl device belongs to.
  * @id_entry: matched id entry in dfl driver's id table.
+ * @driver_override: driver name to force a match
  */
 struct dfl_device {
 	struct device dev;
@@ -43,6 +44,7 @@ struct dfl_device {
 	unsigned int num_irqs;
 	struct dfl_fpga_cdev *cdev;
 	const struct dfl_device_id *id_entry;
+	char *driver_override;
 };
 
 /**
author maintainer
#11 lore raw

Re: [PATCH 1/2] fpga: dfl: add driver_override support

Quoted 13 lines
Quoted 13 lines
Quoted 9 lines

drivers/fpga/dfl.c static int dfl_devs_add(struct dfl_feature_platform_data *pdata) · line 524

Quoted 4 lines

int __dfl_driver_register(struct dfl_driver *dfl_drv, struct module *owner) {

drivers/fpga/dfl.c +1 -1
-	if (!dfl_drv || !dfl_drv->probe || !dfl_drv->id_table)
+	if (!dfl_drv || !dfl_drv->probe)

id_table is still needed for the normal case.

Instead of removing this check, could you add something like

|| (!dfl_drv->is_override && !dfl_drv->id_table)

I don't think it is needed. Seems is_override and !id_table are duplicated conditions for this implementation. And it may make confusing, e.g. could a driver been force matched when is_override is not set?

Quoted 5 lines
Quoted 4 lines

I think we could make it simple, if the dfl driver didn't provide the id_table, normally it could not match any device. I think it could be easily understood by dfl driver developers.

Then an ASSERT should be added in dfl_bus_match() for id_entry.

I didn't get your idea. What Assertion should be added for id_entry? BUG_ON(id_entry == NULL)? Then dfl-uio-pdev can't be inserted before driver_override is set.

I think it is normal case that a driver is successfully registered but doesn't match any device because it provides no id_table.

Thanks,
Yilun.

#12 lore raw

Re: [PATCH 1/2] fpga: dfl: add driver_override support

Quoted 2 lines · Xu Yilun

I think it is normal case that a driver is successfully registered but doesn't match any device because it provides no id_table.

How is that "normal"? What would ever cause that driver to be bound to a device then?

And you better not say userspace is responsible for it...

thanks,

greg k-h

author maintainer
#13 lore raw

Re: [PATCH 1/2] fpga: dfl: add driver_override support

Quoted 6 lines · Greg KH
Quoted 2 lines · Xu Yilun

I think it is normal case that a driver is successfully registered but doesn't match any device because it provides no id_table.

How is that "normal"? What would ever cause that driver to be bound to a device then?

This patchset adds the driver_override sysfs node, to let userspace specify which driver (by name matching) to be bound to a device. The driver "dfl-uio-pdev" doesn't provide an id_table, it could only be bound to a device whose driver_override is set to "dfl-uio-pdev".

Sorry, this is actually not "normal". Usually dfl drivers with valid id_table should be used to drive the dfl devices. But we also want to give an option for userspace to take full control of the device, some IP blocks in FPGA are written for specific purposes by FPGA user so a userspace driver may serve them better.

Quoted 2 lines

And you better not say userspace is responsible for it...

Actually it is the userspace's decision which device they want to access directly ...

I'm not sure if this idea is OK. I see similar implementation for pci/platform devices.

Thanks,
Yilun

Quoted 4 lines

thanks,

greg k-h

#14 lore raw

Re: [PATCH 1/2] fpga: dfl: add driver_override support

Quoted 20 lines · Xu Yilun
Quoted 6 lines · Greg KH
Quoted 2 lines · Xu Yilun

I think it is normal case that a driver is successfully registered but doesn't match any device because it provides no id_table.

How is that "normal"? What would ever cause that driver to be bound to a device then?

This patchset adds the driver_override sysfs node, to let userspace specify which driver (by name matching) to be bound to a device. The driver "dfl-uio-pdev" doesn't provide an id_table, it could only be bound to a device whose driver_override is set to "dfl-uio-pdev".

Sorry, this is actually not "normal". Usually dfl drivers with valid id_table should be used to drive the dfl devices. But we also want to give an option for userspace to take full control of the device, some IP blocks in FPGA are written for specific purposes by FPGA user so a userspace driver may serve them better.

Quoted 1 line
Quoted 7 lines
Quoted 1 line

And you better not say userspace is responsible for it...

Actually it is the userspace's decision which device they want to access directly ...

I'm not sure if this idea is OK. I see similar implementation for pci/platform devices.

That is there for debugging and for being able to support things when the kernel is not updated with a new device id yet.

The virtio people took this to a new extreme and use it to bind real devices through to virtual machines, but really, that's horrid. And they have problems with it as is seen in random patches at times.

Don't make this the only way to support this, that's not an ok api.

greg k-h

author maintainer
#15 lore raw

Re: [PATCH 1/2] fpga: dfl: add driver_override support

Hi Greg,

Quoted 23 lines · Greg KH
Quoted 22 lines · Xu Yilun
Quoted 6 lines · Greg KH
Quoted 2 lines · Xu Yilun

I think it is normal case that a driver is successfully registered but doesn't match any device because it provides no id_table.

How is that "normal"? What would ever cause that driver to be bound to a device then?

This patchset adds the driver_override sysfs node, to let userspace specify which driver (by name matching) to be bound to a device. The driver "dfl-uio-pdev" doesn't provide an id_table, it could only be bound to a device whose driver_override is set to "dfl-uio-pdev".

Sorry, this is actually not "normal". Usually dfl drivers with valid id_table should be used to drive the dfl devices. But we also want to give an option for userspace to take full control of the device, some IP blocks in FPGA are written for specific purposes by FPGA user so a userspace driver may serve them better.

Quoted 2 lines

And you better not say userspace is responsible for it...

Quoted 8 lines
Quoted 5 lines

Actually it is the userspace's decision which device they want to access directly ...

I'm not sure if this idea is OK. I see similar implementation for pci/platform devices.

That is there for debugging and for being able to support things when the kernel is not updated with a new device id yet.

We are trying to use the driver_override for similar purposes on FPGA. The FPGA card could be reprogramed with a newly developed Hardware component by users. Sometimes only the FPGA users know which dfl device they want to operate on. This is why we give the option to users for direct I/O access.

Also FPGA users have the requirement for diagnostic of the newly developed components.

It is possible we define an id_table for dfl_uio_pdev driver, but then users have to modify the driver code everytime they create & reprogram a new HW component.

Quoted 4 lines

The virtio people took this to a new extreme and use it to bind real devices through to virtual machines, but really, that's horrid. And they have problems with it as is seen in random patches at times.

Sorry I didn't have much knowledge on this.
Does the sysfs 'driver_override' or 'new_id' have problems, or the userspace direct I/O access mechanism (UIO/VFIO) has problems?

If it is the former, I could consider some other way to generate the UIO devices for DFL. But could you help specify more about the issues?

Thanks very much,
Yilun

Quoted 4 lines

Don't make this the only way to support this, that's not an ok api.

greg k-h

author maintainer
#16 lore raw

Re: [PATCH 1/2] fpga: dfl: add driver_override support

Hi Greg,

We created a 'driver_override' api for dfl bus, and a driver for userspace I/O access for dfl devices.

Some new comments inline.

Quoted 41 lines · Xu Yilun

Hi Greg,

Quoted 23 lines · Greg KH
Quoted 22 lines · Xu Yilun
Quoted 6 lines · Greg KH
Quoted 2 lines · Xu Yilun

I think it is normal case that a driver is successfully registered but doesn't match any device because it provides no id_table.

How is that "normal"? What would ever cause that driver to be bound to a device then?

This patchset adds the driver_override sysfs node, to let userspace specify which driver (by name matching) to be bound to a device. The driver "dfl-uio-pdev" doesn't provide an id_table, it could only be bound to a device whose driver_override is set to "dfl-uio-pdev".

Sorry, this is actually not "normal". Usually dfl drivers with valid id_table should be used to drive the dfl devices. But we also want to give an option for userspace to take full control of the device, some IP blocks in FPGA are written for specific purposes by FPGA user so a userspace driver may serve them better.

Quoted 2 lines

And you better not say userspace is responsible for it...

Quoted 8 lines
Quoted 5 lines

Actually it is the userspace's decision which device they want to access directly ...

I'm not sure if this idea is OK. I see similar implementation for pci/platform devices.

That is there for debugging and for being able to support things when the kernel is not updated with a new device id yet.

We are trying to use the driver_override for similar purposes on FPGA. The FPGA card could be reprogramed with a newly developed Hardware component by users. Sometimes only the FPGA users know which dfl device they want to operate on. This is why we give the option to users for direct I/O access.

Quoted 11 lines

Also FPGA users have the requirement for diagnostic of the newly developed components.

It is possible we define an id_table for dfl_uio_pdev driver, but then users have to modify the driver code everytime they create & reprogram a new HW component.

Quoted 4 lines

The virtio people took this to a new extreme and use it to bind real devices through to virtual machines, but really, that's horrid. And they have problems with it as is seen in random patches at times.

I searched the previous patches, it seems the main issue is the user could unbind the vfio-pci for a device and bind it to another kernel driver, while its iommu group is still being used by vfio for other devices in the same group, then the integrity of the group is compromised. The conclusion is the user should be responsible for the correct operation of bind/unbind/driver_override/new_id ...

In this patchset, we use the driver_override for UIO case, I think the issue doesn't impact it, is it?

Quoted 2 lines
Quoted 2 lines

Don't make this the only way to support this, that's not an ok api.

We are not going to match driver for every dfl device by user assignment. We are developing generic kernel drivers for them, e.g. dfl-n3000-nios, dfl-emif, as you can see in recent patches. They follows the generic id_table matching.

We use the driver_override for UIO case. Similar to what you've mentioned, FPGA users develop their own hardware components on FPGA, so they need to run diagnostic tools for white-box test.
They also need to support some hardware designed for special purposes and does not fit into one of the standard kernel subsystems.

The FPGA users are the developer of some hardware component, they have the knowledge of the new dfl devices. So the driver_override give the option to the users for driver assignment.

For these purposes, is it OK we use the driver_override?

Thanks,
Yilun

Quoted 2 lines
Quoted 2 lines

greg k-h

reviewer
#17 lore raw

Re: [PATCH 1/2] fpga: dfl: add driver_override support

In reply to Xu Yilun
Quoted 17 lines · Xu Yilun
Quoted 17 lines
Quoted 17 lines
Quoted 9 lines

drivers/fpga/dfl.c static int dfl_devs_add(struct dfl_feature_platform_data *pdata) · line 524

Quoted 4 lines

int __dfl_driver_register(struct dfl_driver *dfl_drv, struct module *owner) {

drivers/fpga/dfl.c +1 -1
-	if (!dfl_drv || !dfl_drv->probe || !dfl_drv->id_table)
+	if (!dfl_drv || !dfl_drv->probe)

id_table is still needed for the normal case.

Instead of removing this check, could you add something like

|| (!dfl_drv->is_override && !dfl_drv->id_table)

I don't think it is needed. Seems is_override and !id_table are duplicated conditions for this implementation. And it may make confusing, e.g. could a driver been force matched when is_override is not set?

I think we could make it simple, if the dfl driver didn't provide the id_table, normally it could not match any device. I think it could be easily understood by dfl driver developers.

Quoted 7 lines
Quoted 1 line

Then an ASSERT should be added in dfl_bus_match() for id_entry.

I didn't get your idea. What Assertion should be added for id_entry? BUG_ON(id_entry == NULL)? Then dfl-uio-pdev can't be inserted before driver_override is set.

I think it is normal case that a driver is successfully registered but doesn't match any device because it provides no id_table.

Ok, i see your point.

Thanks for the explanation.

Tom

Quoted 4 lines

Thanks,
Yilun.

author maintainer
#18 lore raw

PATCH 2/2 fpga: dfl: add the userspace I/O device support for DFL devices

In reply to Xu Yilun

This patch supports the DFL drivers be written in userspace. This is realized by exposing the userspace I/O device interfaces. The driver leverages the uio_pdrv_genirq, it adds the uio_pdrv_genirq platform device with the DFL device's resources, and let the generic UIO platform device driver provide support to userspace access to kernel interrupts and memory locations.

---
 drivers/fpga/Kconfig        | 10 ++++++
 drivers/fpga/Makefile       |  1 +
 drivers/fpga/dfl-uio-pdev.c | 83 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 94 insertions(+)
 create mode 100644 drivers/fpga/dfl-uio-pdev.c
drivers/fpga/Kconfig +10 -0
index 5d7f0ae..e054722 100644
@@ -202,6 +202,16 @@ config FPGA_DFL_NIOS_INTEL_PAC_N3000
 	  the card. It also instantiates the SPI master (spi-altera) for
 	  the card's BMC (Board Management Controller).
 
+config FPGA_DFL_UIO_PDEV
+	tristate "FPGA DFL Driver for Userspace I/O platform devices"
+	depends on FPGA_DFL && UIO_PDRV_GENIRQ
+	help
+	  Enable this to allow some DFL drivers be written in userspace. It
+	  adds the uio_pdrv_genirq platform device with the DFL device's
+	  resources, and let the generic UIO platform device driver provide
+	  support to userspace access to kernel interrupts and memory
+	  locations.
+
 config FPGA_DFL_PCI
 	tristate "FPGA DFL PCIe Device Driver"
 	depends on PCI && FPGA_DFL
drivers/fpga/Makefile +1 -0
index 18dc9885..e07b3d5 100644
@@ -45,6 +45,7 @@ dfl-afu-objs := dfl-afu-main.o dfl-afu-region.o dfl-afu-dma-region.o
 dfl-afu-objs += dfl-afu-error.o
 
 obj-$(CONFIG_FPGA_DFL_NIOS_INTEL_PAC_N3000)	+= dfl-n3000-nios.o
+obj-$(CONFIG_FPGA_DFL_UIO_PDEV)	+= dfl-uio-pdev.o
 
 # Drivers for FPGAs which implement DFL
 obj-$(CONFIG_FPGA_DFL_PCI)		+= dfl-pci.o
drivers/fpga/dfl-uio-pdev.c new file +83 -0

83 lines not on this page · raw message

Signature · 1 line
2.7.4
reviewer
#19 lore raw

Re: [PATCH 2/2] fpga: dfl: add the userspace I/O device support for DFL devices

Quoted 20 lines · Xu Yilun

This patch supports the DFL drivers be written in userspace. This is realized by exposing the userspace I/O device interfaces. The driver leverages the uio_pdrv_genirq, it adds the uio_pdrv_genirq platform device with the DFL device's resources, and let the generic UIO platform device driver provide support to userspace access to kernel interrupts and memory locations.

---
 drivers/fpga/Kconfig        | 10 ++++++
 drivers/fpga/Makefile       |  1 +
 drivers/fpga/dfl-uio-pdev.c | 83 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 94 insertions(+)
 create mode 100644 drivers/fpga/dfl-uio-pdev.c
drivers/fpga/Kconfig +0 -0
index 5d7f0ae..e054722 100644

drivers/fpga/Kconfig config FPGA_DFL_NIOS_INTEL_PAC_N3000 · line 211

Quoted 10 lines
drivers/fpga/Kconfig +7 -0
@@ -202,6 +202,16 @@ config FPGA_DFL_NIOS_INTEL_PAC_N3000
 	  the card. It also instantiates the SPI master (spi-altera) for
 	  the card's BMC (Board Management Controller).
 
+config FPGA_DFL_UIO_PDEV
+	tristate "FPGA DFL Driver for Userspace I/O platform devices"
+	depends on FPGA_DFL && UIO_PDRV_GENIRQ
+	help
+	  Enable this to allow some DFL drivers be written in userspace. It
+	  adds the uio_pdrv_genirq platform device with the DFL device's
+	  resources, and let the generic UIO platform device driver provide

'and lets the'

Quoted 38 lines
drivers/fpga/Kconfig +3 -0
+	  support to userspace access to kernel interrupts and memory
+	  locations.
+
 config FPGA_DFL_PCI
 	tristate "FPGA DFL PCIe Device Driver"
 	depends on PCI && FPGA_DFL
drivers/fpga/Makefile +1 -0
index 18dc9885..e07b3d5 100644
@@ -45,6 +45,7 @@ dfl-afu-objs := dfl-afu-main.o dfl-afu-region.o dfl-afu-dma-region.o
 dfl-afu-objs += dfl-afu-error.o
 
 obj-$(CONFIG_FPGA_DFL_NIOS_INTEL_PAC_N3000)	+= dfl-n3000-nios.o
+obj-$(CONFIG_FPGA_DFL_UIO_PDEV)	+= dfl-uio-pdev.o
 
 # Drivers for FPGAs which implement DFL
 obj-$(CONFIG_FPGA_DFL_PCI)		+= dfl-pci.o
drivers/fpga/dfl-uio-pdev.c new file +14 -0
new file mode 100644
index 0000000..d35b846
@@ -0,0 +1,83 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * DFL driver for Userspace I/O platform devices
+ *
+ * Copyright (C) 2020 Intel Corporation, Inc.
+ */
+#include <linux/dfl.h>
+#include <linux/errno.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+#include <linux/uio_driver.h>
+

drivers/fpga/dfl-uio-pdev.c line 24

Quoted 10 lines
drivers/fpga/dfl-uio-pdev.c new file +10 -0
+#define DRIVER_NAME "dfl-uio-pdev"
+
+static int dfl_uio_pdev_probe(struct dfl_device *ddev)
+{
+	struct device *dev = &ddev->dev;
+	struct platform_device_info pdevinfo = { 0 };
+	struct uio_info uio_pdata = { 0 };
+	struct platform_device *uio_pdev;
+	struct resource *res;
+	int i, idx = 0;

idx is not needed.

drivers/fpga/dfl-uio-pdev.c line 32

Quoted 8 lines
drivers/fpga/dfl-uio-pdev.c new file +8 -0
+
+	pdevinfo.name = "uio_pdrv_genirq";
+
+	res = kcalloc(ddev->num_irqs + 1, sizeof(*res), GFP_KERNEL);
+	if (!res)
+		return -ENOMEM;
+
+	res[idx].parent = &ddev->mmio_res;

res[0].parent =

drivers/fpga/dfl-uio-pdev.c line 40

Quoted 8 lines
drivers/fpga/dfl-uio-pdev.c new file +8 -0
+	res[idx].flags = IORESOURCE_MEM;
+	res[idx].start = ddev->mmio_res.start;
+	res[idx].end = ddev->mmio_res.end;
+	++idx;
+
+	/* then add irq resource */
+	for (i = 0; i < ddev->num_irqs; i++) {
+		res[idx].flags = IORESOURCE_IRQ;

res[i+1].flags =

Tom

Quoted 43 lines
drivers/fpga/dfl-uio-pdev.c new file +43 -0
+		res[idx].start = ddev->irqs[i];
+		res[idx].end = ddev->irqs[i];
+		++idx;
+	}
+
+	uio_pdata.name = DRIVER_NAME;
+	uio_pdata.version = "0";
+
+	pdevinfo.res = res;
+	pdevinfo.num_res = idx;
+	pdevinfo.parent = &ddev->dev;
+	pdevinfo.id = PLATFORM_DEVID_AUTO;
+	pdevinfo.data = &uio_pdata;
+	pdevinfo.size_data = sizeof(uio_pdata);
+
+	uio_pdev = platform_device_register_full(&pdevinfo);
+	if (!IS_ERR(uio_pdev))
+		dev_set_drvdata(dev, uio_pdev);
+
+	kfree(res);
+
+	return PTR_ERR_OR_ZERO(uio_pdev);
+}
+
+static void dfl_uio_pdev_remove(struct dfl_device *ddev)
+{
+	struct platform_device *uio_pdev = dev_get_drvdata(&ddev->dev);
+
+	platform_device_unregister(uio_pdev);
+}
+
+static struct dfl_driver dfl_uio_pdev_driver = {
+	.drv	= {
+		.name       = DRIVER_NAME,
+	},
+	.probe	= dfl_uio_pdev_probe,
+	.remove	= dfl_uio_pdev_remove,
+};
+module_dfl_driver(dfl_uio_pdev_driver);
+
+MODULE_DESCRIPTION("DFL driver for Userspace I/O platform devices");
+MODULE_AUTHOR("Intel Corporation");
+MODULE_LICENSE("GPL v2");
author maintainer
#20 lore raw

Re: [PATCH 2/2] fpga: dfl: add the userspace I/O device support for DFL devices

Quoted 25 lines · Tom Rix
Quoted 23 lines · Xu Yilun

This patch supports the DFL drivers be written in userspace. This is realized by exposing the userspace I/O device interfaces. The driver leverages the uio_pdrv_genirq, it adds the uio_pdrv_genirq platform device with the DFL device's resources, and let the generic UIO platform device driver provide support to userspace access to kernel interrupts and memory locations.

---
 drivers/fpga/Kconfig        | 10 ++++++
 drivers/fpga/Makefile       |  1 +
 drivers/fpga/dfl-uio-pdev.c | 83 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 94 insertions(+)
 create mode 100644 drivers/fpga/dfl-uio-pdev.c
drivers/fpga/Kconfig +0 -0
index 5d7f0ae..e054722 100644
@@ -202,6 +202,16 @@ config FPGA_DFL_NIOS_INTEL_PAC_N3000
 	  the card. It also instantiates the SPI master (spi-altera) for
 	  the card's BMC (Board Management Controller).
 
Quoted 8 lines

drivers/fpga/Kconfig config FPGA_DFL_NIOS_INTEL_PAC_N3000 · line 211

Quoted 7 lines
drivers/fpga/Kconfig +7 -0
+config FPGA_DFL_UIO_PDEV
+	tristate "FPGA DFL Driver for Userspace I/O platform devices"
+	depends on FPGA_DFL && UIO_PDRV_GENIRQ
+	help
+	  Enable this to allow some DFL drivers be written in userspace. It
+	  adds the uio_pdrv_genirq platform device with the DFL device's
+	  resources, and let the generic UIO platform device driver provide

'and lets the'

Yes.

Quoted 42 lines
Quoted 42 lines
drivers/fpga/Kconfig +3 -0
+	  support to userspace access to kernel interrupts and memory
+	  locations.
+
 config FPGA_DFL_PCI
 	tristate "FPGA DFL PCIe Device Driver"
 	depends on PCI && FPGA_DFL
drivers/fpga/Makefile +1 -0
index 18dc9885..e07b3d5 100644
@@ -45,6 +45,7 @@ dfl-afu-objs := dfl-afu-main.o dfl-afu-region.o dfl-afu-dma-region.o
 dfl-afu-objs += dfl-afu-error.o
 
 obj-$(CONFIG_FPGA_DFL_NIOS_INTEL_PAC_N3000)	+= dfl-n3000-nios.o
+obj-$(CONFIG_FPGA_DFL_UIO_PDEV)	+= dfl-uio-pdev.o
 
 # Drivers for FPGAs which implement DFL
 obj-$(CONFIG_FPGA_DFL_PCI)		+= dfl-pci.o
drivers/fpga/dfl-uio-pdev.c new file +18 -0
new file mode 100644
index 0000000..d35b846
@@ -0,0 +1,83 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * DFL driver for Userspace I/O platform devices
+ *
+ * Copyright (C) 2020 Intel Corporation, Inc.
+ */
+#include <linux/dfl.h>
+#include <linux/errno.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+#include <linux/uio_driver.h>
+
+#define DRIVER_NAME "dfl-uio-pdev"
+
+static int dfl_uio_pdev_probe(struct dfl_device *ddev)
+{
Quoted 8 lines

drivers/fpga/dfl-uio-pdev.c line 24

Quoted 6 lines
drivers/fpga/dfl-uio-pdev.c new file +6 -0
+	struct device *dev = &ddev->dev;
+	struct platform_device_info pdevinfo = { 0 };
+	struct uio_info uio_pdata = { 0 };
+	struct platform_device *uio_pdev;
+	struct resource *res;
+	int i, idx = 0;

idx is not needed.

I could remove the idx. But I think I could ++res during the resource filling.

Thanks,
Yilun

Quoted 66 lines

drivers/fpga/dfl-uio-pdev.c line 32

Quoted 9 lines
drivers/fpga/dfl-uio-pdev.c new file +8 -0
+
+	pdevinfo.name = "uio_pdrv_genirq";
+
+	res = kcalloc(ddev->num_irqs + 1, sizeof(*res), GFP_KERNEL);
+	if (!res)
+		return -ENOMEM;
+
+	res[idx].parent = &ddev->mmio_res;

res[0].parent =

drivers/fpga/dfl-uio-pdev.c line 40

Quoted 8 lines
drivers/fpga/dfl-uio-pdev.c new file +8 -0
+	res[idx].flags = IORESOURCE_MEM;
+	res[idx].start = ddev->mmio_res.start;
+	res[idx].end = ddev->mmio_res.end;
+	++idx;
+
+	/* then add irq resource */
+	for (i = 0; i < ddev->num_irqs; i++) {
+		res[idx].flags = IORESOURCE_IRQ;

res[i+1].flags =

Tom

Quoted 43 lines
drivers/fpga/dfl-uio-pdev.c new file +43 -0
+		res[idx].start = ddev->irqs[i];
+		res[idx].end = ddev->irqs[i];
+		++idx;
+	}
+
+	uio_pdata.name = DRIVER_NAME;
+	uio_pdata.version = "0";
+
+	pdevinfo.res = res;
+	pdevinfo.num_res = idx;
+	pdevinfo.parent = &ddev->dev;
+	pdevinfo.id = PLATFORM_DEVID_AUTO;
+	pdevinfo.data = &uio_pdata;
+	pdevinfo.size_data = sizeof(uio_pdata);
+
+	uio_pdev = platform_device_register_full(&pdevinfo);
+	if (!IS_ERR(uio_pdev))
+		dev_set_drvdata(dev, uio_pdev);
+
+	kfree(res);
+
+	return PTR_ERR_OR_ZERO(uio_pdev);
+}
+
+static void dfl_uio_pdev_remove(struct dfl_device *ddev)
+{
+	struct platform_device *uio_pdev = dev_get_drvdata(&ddev->dev);
+
+	platform_device_unregister(uio_pdev);
+}
+
+static struct dfl_driver dfl_uio_pdev_driver = {
+	.drv	= {
+		.name       = DRIVER_NAME,
+	},
+	.probe	= dfl_uio_pdev_probe,
+	.remove	= dfl_uio_pdev_remove,
+};
+module_dfl_driver(dfl_uio_pdev_driver);
+
+MODULE_DESCRIPTION("DFL driver for Userspace I/O platform devices");
+MODULE_AUTHOR("Intel Corporation");
+MODULE_LICENSE("GPL v2");
reviewer
#21 lore raw

Re: [PATCH 2/2] fpga: dfl: add the userspace I/O device support for DFL devices

Quoted 84 lines · Xu Yilun
Quoted 24 lines · Tom Rix
Quoted 23 lines · Xu Yilun

This patch supports the DFL drivers be written in userspace. This is realized by exposing the userspace I/O device interfaces. The driver leverages the uio_pdrv_genirq, it adds the uio_pdrv_genirq platform device with the DFL device's resources, and let the generic UIO platform device driver provide support to userspace access to kernel interrupts and memory locations.

---
 drivers/fpga/Kconfig        | 10 ++++++
 drivers/fpga/Makefile       |  1 +
 drivers/fpga/dfl-uio-pdev.c | 83 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 94 insertions(+)
 create mode 100644 drivers/fpga/dfl-uio-pdev.c
drivers/fpga/Kconfig +0 -0
index 5d7f0ae..e054722 100644
@@ -202,6 +202,16 @@ config FPGA_DFL_NIOS_INTEL_PAC_N3000
 	  the card. It also instantiates the SPI master (spi-altera) for
 	  the card's BMC (Board Management Controller).
 
Quoted 8 lines

drivers/fpga/Kconfig config FPGA_DFL_NIOS_INTEL_PAC_N3000 · line 211

Quoted 7 lines
drivers/fpga/Kconfig +7 -0
+config FPGA_DFL_UIO_PDEV
+	tristate "FPGA DFL Driver for Userspace I/O platform devices"
+	depends on FPGA_DFL && UIO_PDRV_GENIRQ
+	help
+	  Enable this to allow some DFL drivers be written in userspace. It
+	  adds the uio_pdrv_genirq platform device with the DFL device's
+	  resources, and let the generic UIO platform device driver provide

'and lets the'

Yes.

Quoted 41 lines
Quoted 41 lines
drivers/fpga/Kconfig +3 -0
+	  support to userspace access to kernel interrupts and memory
+	  locations.
+
 config FPGA_DFL_PCI
 	tristate "FPGA DFL PCIe Device Driver"
 	depends on PCI && FPGA_DFL
drivers/fpga/Makefile +1 -0
index 18dc9885..e07b3d5 100644
@@ -45,6 +45,7 @@ dfl-afu-objs := dfl-afu-main.o dfl-afu-region.o dfl-afu-dma-region.o
 dfl-afu-objs += dfl-afu-error.o
 
 obj-$(CONFIG_FPGA_DFL_NIOS_INTEL_PAC_N3000)	+= dfl-n3000-nios.o
+obj-$(CONFIG_FPGA_DFL_UIO_PDEV)	+= dfl-uio-pdev.o
 
 # Drivers for FPGAs which implement DFL
 obj-$(CONFIG_FPGA_DFL_PCI)		+= dfl-pci.o
drivers/fpga/dfl-uio-pdev.c new file +17 -0
new file mode 100644
index 0000000..d35b846
@@ -0,0 +1,83 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * DFL driver for Userspace I/O platform devices
+ *
+ * Copyright (C) 2020 Intel Corporation, Inc.
+ */
+#include <linux/dfl.h>
+#include <linux/errno.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+#include <linux/uio_driver.h>
+
+#define DRIVER_NAME "dfl-uio-pdev"
+
+static int dfl_uio_pdev_probe(struct dfl_device *ddev)
Quoted 8 lines

drivers/fpga/dfl-uio-pdev.c line 24

Quoted 7 lines
drivers/fpga/dfl-uio-pdev.c new file +7 -0
+{
+	struct device *dev = &ddev->dev;
+	struct platform_device_info pdevinfo = { 0 };
+	struct uio_info uio_pdata = { 0 };
+	struct platform_device *uio_pdev;
+	struct resource *res;
+	int i, idx = 0;

idx is not needed.

Quoted 2 lines

I could remove the idx. But I think I could ++res during the resource filling.

ok.

You may want to check the code generation, my feeling is ++res will be worse.

Tom

Quoted 68 lines

Thanks,
Yilun

Quoted 64 lines

drivers/fpga/dfl-uio-pdev.c line 32

Quoted 8 lines
drivers/fpga/dfl-uio-pdev.c new file +8 -0
+
+	pdevinfo.name = "uio_pdrv_genirq";
+
+	res = kcalloc(ddev->num_irqs + 1, sizeof(*res), GFP_KERNEL);
+	if (!res)
+		return -ENOMEM;
+
+	res[idx].parent = &ddev->mmio_res;

res[0].parent =

drivers/fpga/dfl-uio-pdev.c line 40

Quoted 8 lines
drivers/fpga/dfl-uio-pdev.c new file +8 -0
+	res[idx].flags = IORESOURCE_MEM;
+	res[idx].start = ddev->mmio_res.start;
+	res[idx].end = ddev->mmio_res.end;
+	++idx;
+
+	/* then add irq resource */
+	for (i = 0; i < ddev->num_irqs; i++) {
+		res[idx].flags = IORESOURCE_IRQ;

res[i+1].flags =

Tom

Quoted 43 lines
drivers/fpga/dfl-uio-pdev.c new file +43 -0
+		res[idx].start = ddev->irqs[i];
+		res[idx].end = ddev->irqs[i];
+		++idx;
+	}
+
+	uio_pdata.name = DRIVER_NAME;
+	uio_pdata.version = "0";
+
+	pdevinfo.res = res;
+	pdevinfo.num_res = idx;
+	pdevinfo.parent = &ddev->dev;
+	pdevinfo.id = PLATFORM_DEVID_AUTO;
+	pdevinfo.data = &uio_pdata;
+	pdevinfo.size_data = sizeof(uio_pdata);
+
+	uio_pdev = platform_device_register_full(&pdevinfo);
+	if (!IS_ERR(uio_pdev))
+		dev_set_drvdata(dev, uio_pdev);
+
+	kfree(res);
+
+	return PTR_ERR_OR_ZERO(uio_pdev);
+}
+
+static void dfl_uio_pdev_remove(struct dfl_device *ddev)
+{
+	struct platform_device *uio_pdev = dev_get_drvdata(&ddev->dev);
+
+	platform_device_unregister(uio_pdev);
+}
+
+static struct dfl_driver dfl_uio_pdev_driver = {
+	.drv	= {
+		.name       = DRIVER_NAME,
+	},
+	.probe	= dfl_uio_pdev_probe,
+	.remove	= dfl_uio_pdev_remove,
+};
+module_dfl_driver(dfl_uio_pdev_driver);
+
+MODULE_DESCRIPTION("DFL driver for Userspace I/O platform devices");
+MODULE_AUTHOR("Intel Corporation");
+MODULE_LICENSE("GPL v2");
reviewer
#22 lore raw
In reply to Xu Yilun
Quoted 13 lines · Xu Yilun

This patchset supports some dfl device drivers written in userspace.

The usage is like:

 # echo dfl_dev.1 > /sys/bus/dfl/drivers/<kernel driver>/unbind
 # echo dfl-uio-pdev > /sys/bus/dfl/devices/dfl_dev.1/driver_override
 # echo dfl_dev.1 > /sys/bus/dfl/drivers_probe

Xu Yilun (2):
fpga: dfl: add driver_override support
fpga: dfl: add the userspace I/O device support for DFL devices

Quoted 8 lines
 Documentation/ABI/testing/sysfs-bus-dfl | 28 +++++++++--
 drivers/fpga/Kconfig                    | 10 ++++
 drivers/fpga/Makefile                   |  1 +
 drivers/fpga/dfl-uio-pdev.c             | 83 +++++++++++++++++++++++++++++++++
 drivers/fpga/dfl.c                      | 54 ++++++++++++++++++++-
 include/linux/dfl.h                     |  2 +
 6 files changed, 173 insertions(+), 5 deletions(-)
 create mode 100644 drivers/fpga/dfl-uio-pdev.c

This is a neat new feature.

Should something be added to Documentation/fpga/dfl.rst ?

Overall, patchset looks good.

Tom

author maintainer
#23 lore raw
Quoted 24 lines · Tom Rix
Quoted 13 lines · Xu Yilun

This patchset supports some dfl device drivers written in userspace.

The usage is like:

 # echo dfl_dev.1 > /sys/bus/dfl/drivers/<kernel driver>/unbind
 # echo dfl-uio-pdev > /sys/bus/dfl/devices/dfl_dev.1/driver_override
 # echo dfl_dev.1 > /sys/bus/dfl/drivers_probe

Xu Yilun (2):
fpga: dfl: add driver_override support
fpga: dfl: add the userspace I/O device support for DFL devices

Quoted 8 lines
 Documentation/ABI/testing/sysfs-bus-dfl | 28 +++++++++--
 drivers/fpga/Kconfig                    | 10 ++++
 drivers/fpga/Makefile                   |  1 +
 drivers/fpga/dfl-uio-pdev.c             | 83 +++++++++++++++++++++++++++++++++
 drivers/fpga/dfl.c                      | 54 ++++++++++++++++++++-
 include/linux/dfl.h                     |  2 +
 6 files changed, 173 insertions(+), 5 deletions(-)
 create mode 100644 drivers/fpga/dfl-uio-pdev.c
Quoted 3 lines

This is a neat new feature.

Should something be added to Documentation/fpga/dfl.rst ?

Yes, I could add the Doc.

Quoted 4 lines

Overall, patchset looks good.

Tom