[PATCH v5 4/5] cdx: add sysfs for bus reset

From: Abhijit Gangurde
Date: Fri Sep 22 2023 - 06:20:34 EST


Add sysfs interface reset to reset all the devices on the CDX bus.

Co-developed-by: Puneet Gupta <puneet.gupta@xxxxxxx>
Signed-off-by: Puneet Gupta <puneet.gupta@xxxxxxx>
Co-developed-by: Nipun Gupta <nipun.gupta@xxxxxxx>
Signed-off-by: Nipun Gupta <nipun.gupta@xxxxxxx>
Signed-off-by: Abhijit Gangurde <abhijit.gangurde@xxxxxxx>
Reviewed-by: Pieter Jansen van Vuuren <pieter.jansen-van-vuuren@xxxxxxx>
Tested-by: Nikhil Agarwal <nikhil.agarwal@xxxxxxx>
---
Changes in v5:
- None

Changes in v4:
- None

Changes in v3:
- None

Changes in v2:
- Improve documentation
- sysfs entry reset takes bus number as an argument

Documentation/ABI/testing/sysfs-bus-cdx | 15 +++++++++
drivers/cdx/cdx.c | 41 +++++++++++++++++++++++++
2 files changed, 56 insertions(+)

diff --git a/Documentation/ABI/testing/sysfs-bus-cdx b/Documentation/ABI/testing/sysfs-bus-cdx
index 04c8dfe7e201..d25875359741 100644
--- a/Documentation/ABI/testing/sysfs-bus-cdx
+++ b/Documentation/ABI/testing/sysfs-bus-cdx
@@ -38,6 +38,21 @@ Description:

# echo 00 > /sys/bus/cdx/disable

+What: /sys/bus/cdx/reset
+Date: July 2023
+Contact: puneet.gupta@xxxxxxx
+Description:
+ Writing bus number in hex to this file will attempt to reset
+ all the devices present on the bus. Resetting a device would
+ clear all existing configuration of the device and put the
+ device in default state. The bus number for the cdx devices can
+ be found at /sys/bus/cdx/devices/cdx-BB:DD, where BB denotes
+ the bus number for the respective device.
+
+ For example ::
+
+ # echo 00 > /sys/bus/cdx/reset
+
What: /sys/bus/cdx/devices/.../vendor
Date: March 2023
Contact: nipun.gupta@xxxxxxx
diff --git a/drivers/cdx/cdx.c b/drivers/cdx/cdx.c
index 8da043756fa4..7687b9371210 100644
--- a/drivers/cdx/cdx.c
+++ b/drivers/cdx/cdx.c
@@ -114,6 +114,26 @@ int cdx_dev_reset(struct device *dev)
}
EXPORT_SYMBOL_GPL(cdx_dev_reset);

+/**
+ * reset_cdx_device - Reset a CDX device
+ * @dev: CDX device
+ * @data: Bus number
+ * If bus number matches to the device's bus then this device
+ * is reset else this is no op.
+ *
+ * Return: -errno on failure, 0 on success.
+ */
+static int reset_cdx_device(struct device *dev, void *data)
+{
+ struct cdx_device *cdx_dev = to_cdx_device(dev);
+ u8 bus_num = *((u8 *)data);
+
+ if (cdx_dev->bus_num == bus_num)
+ return cdx_dev_reset(dev);
+
+ return 0;
+}
+
/**
* cdx_unregister_device - Unregister a CDX device
* @dev: CDX device
@@ -520,10 +540,31 @@ static ssize_t rescan_store(const struct bus_type *bus,
}
static BUS_ATTR_WO(rescan);

+static ssize_t bus_reset_store(const struct bus_type *bus,
+ const char *buf, size_t count)
+{
+ u8 bus_id;
+ int ret;
+
+ if (kstrtou8(buf, 16, &bus_id))
+ return -EINVAL;
+
+ bus_id = BUS_ID(bus_id);
+ mutex_lock(&cdx_controller_lock);
+ /* Reset all the devices attached to cdx bus */
+ ret = bus_for_each_dev(bus, NULL, (void *)&bus_id, reset_cdx_device);
+ mutex_unlock(&cdx_controller_lock);
+
+ return ret < 0 ? ret : count;
+}
+static struct bus_attribute bus_attr_reset = __ATTR(reset, 0200, NULL,
+ bus_reset_store);
+
static struct attribute *cdx_bus_attrs[] = {
&bus_attr_enable.attr,
&bus_attr_disable.attr,
&bus_attr_rescan.attr,
+ &bus_attr_reset.attr,
NULL,
};
ATTRIBUTE_GROUPS(cdx_bus);
--
2.25.1