[PATCH -v2 1/3] pci: add rescan to /sys/.../pci_bus/.../ After remove the device from /sys, we have to rescan all or find out the bridge and access /sys../device/rescan there. this patch add /sys/.../pci_bus/.../rescan. So user can rescan more easy. that is more clean and easy to understand. like after remove 0000:c4:00.0, you can rescan 0000:c4 directly. -v2: According to Jesse, use function instead of exposing attr, so could hide #ifdef in header file. also add code to remove rescan file in remove path. Signed-off-by: Yinghai Lu <yinghai@xxxxxxxxxx> --- Documentation/ABI/testing/sysfs-bus-pci | 9 +++++++++ drivers/pci/bus.c | 4 ++++ drivers/pci/pci-sysfs.c | 31 +++++++++++++++++++++++++++++++ drivers/pci/pci.h | 10 ++++++++++ drivers/pci/probe.c | 5 +++++ drivers/pci/remove.c | 1 + 6 files changed, 60 insertions(+) Index: linux-2.6/drivers/pci/pci-sysfs.c =================================================================== --- linux-2.6.orig/drivers/pci/pci-sysfs.c +++ linux-2.6/drivers/pci/pci-sysfs.c @@ -318,6 +318,37 @@ remove_store(struct device *dev, struct count = ret; return count; } + +static ssize_t +dev_bus_rescan_store(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + unsigned long val; + struct pci_bus *bus = to_pci_bus(dev); + + if (strict_strtoul(buf, 0, &val) < 0) + return -EINVAL; + + if (val) { + mutex_lock(&pci_remove_rescan_mutex); + pci_rescan_bus(bus); + mutex_unlock(&pci_remove_rescan_mutex); + } + return count; +} + +DEVICE_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_bus_rescan_store); + +int pci_bus_create_rescan_file(struct pci_bus *bus) +{ + return device_create_file(&bus->dev, &dev_attr_rescan); +} + +void pci_bus_remove_rescan_file(struct pci_bus *bus) +{ + device_remove_file(&bus->dev, &dev_attr_rescan); +} + #endif struct device_attribute pci_dev_attrs[] = { Index: linux-2.6/drivers/pci/bus.c =================================================================== --- linux-2.6.orig/drivers/pci/bus.c +++ linux-2.6/drivers/pci/bus.c @@ -163,6 +163,10 @@ int pci_bus_add_child(struct pci_bus *bu bus->is_added = 1; + retval = pci_bus_create_rescan_file(bus); + if (retval) + return retval; + retval = device_create_file(&bus->dev, &dev_attr_cpuaffinity); if (retval) return retval; Index: linux-2.6/drivers/pci/pci.h =================================================================== --- linux-2.6.orig/drivers/pci/pci.h +++ linux-2.6/drivers/pci/pci.h @@ -159,9 +159,19 @@ extern struct device_attribute pci_dev_a extern struct device_attribute dev_attr_cpuaffinity; extern struct device_attribute dev_attr_cpulistaffinity; #ifdef CONFIG_HOTPLUG +int pci_bus_create_rescan_file(struct pci_bus *bus); +void pci_bus_remove_rescan_file(struct pci_bus *bus); +extern struct device_attribute dev_attr_rescan; extern struct bus_attribute pci_bus_attrs[]; #else #define pci_bus_attrs NULL +static inline int pci_bus_create_rescan_file(struct device *dev) +{ + return 0; +} +static inline void pci_bus_remove_rescan_file(struct device *dev) +{ +} #endif Index: linux-2.6/drivers/pci/probe.c =================================================================== --- linux-2.6.orig/drivers/pci/probe.c +++ linux-2.6/drivers/pci/probe.c @@ -1471,6 +1471,9 @@ struct pci_bus * pci_create_bus(struct d error = device_create_file(&b->dev, &dev_attr_cpuaffinity); if (error) goto dev_create_file_err; + error = pci_bus_create_rescan_file(b); + if (error) + goto dev_create_bus_rescan_err; /* Create legacy_io and legacy_mem files for this bus */ pci_create_legacy_files(b); @@ -1481,6 +1484,8 @@ struct pci_bus * pci_create_bus(struct d return b; +dev_create_bus_rescan_err: + device_remove_file(&b->dev, &dev_attr_cpuaffinity); dev_create_file_err: device_unregister(&b->dev); class_dev_reg_err: Index: linux-2.6/Documentation/ABI/testing/sysfs-bus-pci =================================================================== --- linux-2.6.orig/Documentation/ABI/testing/sysfs-bus-pci +++ linux-2.6/Documentation/ABI/testing/sysfs-bus-pci @@ -74,6 +74,15 @@ Description: hot-remove the PCI device and any of its children. Depends on CONFIG_HOTPLUG. +What: /sys/bus/pci/devices/.../pci_bus/.../rescan +Date: May 2011 +Contact: Linux PCI developers <linux-pci@xxxxxxxxxxxxxxx> +Description: + Writing a non-zero value to this attribute will + force a rescan of the bus and all child buses, + and re-discover devices removed earlier from this + part of the device tree. Depends on CONFIG_HOTPLUG. + What: /sys/bus/pci/devices/.../rescan Date: January 2009 Contact: Linux PCI developers <linux-pci@xxxxxxxxxxxxxxx> Index: linux-2.6/drivers/pci/remove.c =================================================================== --- linux-2.6.orig/drivers/pci/remove.c +++ linux-2.6/drivers/pci/remove.c @@ -73,6 +73,7 @@ void pci_remove_bus(struct pci_bus *pci_ return; pci_remove_legacy_files(pci_bus); + pci_bus_remove_rescan_file(pci_bus); device_remove_file(&pci_bus->dev, &dev_attr_cpuaffinity); device_remove_file(&pci_bus->dev, &dev_attr_cpulistaffinity); device_unregister(&pci_bus->dev); -- To unsubscribe from this list: send the line "unsubscribe linux-pci" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html