[PATCH] shpchp: use the PCI core for hotplug resource management

This patch converts the standard hotplug controller driver to use
the PCI core for resource management. This eliminates a whole lot
of duplicated code, and integrates shpchp in the system's normal
PCI handling code.

Signed-off-by: Rajesh Shah <rajesh.shah@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
diff --git a/drivers/pci/hotplug/shpchp_sysfs.c b/drivers/pci/hotplug/shpchp_sysfs.c
index c9445eb..b0e781d 100644
--- a/drivers/pci/hotplug/shpchp_sysfs.c
+++ b/drivers/pci/hotplug/shpchp_sysfs.c
@@ -40,43 +40,49 @@
 
 static ssize_t show_ctrl (struct device *dev, struct device_attribute *attr, char *buf)
 {
-	struct pci_dev *pci_dev;
-	struct controller *ctrl;
+	struct pci_dev *pdev;
 	char * out = buf;
-	int index;
-	struct pci_resource *res;
+	int index, busnr;
+	struct resource *res;
+	struct pci_bus *bus;
 
-	pci_dev = container_of (dev, struct pci_dev, dev);
-	ctrl = pci_get_drvdata(pci_dev);
+	pdev = container_of (dev, struct pci_dev, dev);
+	bus = pdev->subordinate;
 
 	out += sprintf(buf, "Free resources: memory\n");
-	index = 11;
-	res = ctrl->mem_head;
-	while (res && index--) {
-		out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
-		res = res->next;
+	for (index = 0; index < PCI_BUS_NUM_RESOURCES; index++) {
+		res = bus->resource[index];
+		if (res && (res->flags & IORESOURCE_MEM) &&
+				!(res->flags & IORESOURCE_PREFETCH)) {
+			out += sprintf(out, "start = %8.8lx, length = %8.8lx\n",
+					res->start, (res->end - res->start));
+		}
 	}
 	out += sprintf(out, "Free resources: prefetchable memory\n");
-	index = 11;
-	res = ctrl->p_mem_head;
-	while (res && index--) {
-		out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
-		res = res->next;
+	for (index = 0; index < PCI_BUS_NUM_RESOURCES; index++) {
+		res = bus->resource[index];
+		if (res && (res->flags & IORESOURCE_MEM) &&
+			       (res->flags & IORESOURCE_PREFETCH)) {
+			out += sprintf(out, "start = %8.8lx, length = %8.8lx\n",
+					res->start, (res->end - res->start));
+		}
 	}
 	out += sprintf(out, "Free resources: IO\n");
-	index = 11;
-	res = ctrl->io_head;
-	while (res && index--) {
-		out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
-		res = res->next;
+	for (index = 0; index < PCI_BUS_NUM_RESOURCES; index++) {
+		res = bus->resource[index];
+		if (res && (res->flags & IORESOURCE_IO)) {
+			out += sprintf(out, "start = %8.8lx, length = %8.8lx\n",
+					res->start, (res->end - res->start));
+		}
 	}
 	out += sprintf(out, "Free resources: bus numbers\n");
-	index = 11;
-	res = ctrl->bus_head;
-	while (res && index--) {
-		out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
-		res = res->next;
+	for (busnr = bus->secondary; busnr <= bus->subordinate; busnr++) {
+		if (!pci_find_bus(pci_domain_nr(bus), busnr))
+			break;
 	}
+	if (busnr < bus->subordinate)
+		out += sprintf(out, "start = %8.8x, length = %8.8x\n",
+				busnr, (bus->subordinate - busnr));
 
 	return out - buf;
 }
@@ -84,16 +90,16 @@
 
 static ssize_t show_dev (struct device *dev, struct device_attribute *attr, char *buf)
 {
-	struct pci_dev *pci_dev;
+	struct pci_dev *pdev, *fdev;
 	struct controller *ctrl;
 	char * out = buf;
 	int index;
-	struct pci_resource *res;
+	struct resource *res;
 	struct pci_func *new_slot;
 	struct slot *slot;
 
-	pci_dev = container_of (dev, struct pci_dev, dev);
-	ctrl = pci_get_drvdata(pci_dev);
+	pdev = container_of (dev, struct pci_dev, dev);
+	ctrl = pci_get_drvdata(pdev);
 
 	slot=ctrl->slot;
 
@@ -101,34 +107,47 @@
 		new_slot = shpchp_slot_find(slot->bus, slot->device, 0);
 		if (!new_slot)
 			break;
+		fdev = new_slot->pci_dev;
+		if (!fdev)
+			break;
 		out += sprintf(out, "assigned resources: memory\n");
-		index = 11;
-		res = new_slot->mem_head;
-		while (res && index--) {
-			out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
-			res = res->next;
+		for (index=0; index <= PCI_NUM_RESOURCES; index++) {
+			res = &(fdev->resource[index]);
+			if (res && (res->flags & IORESOURCE_MEM) &&
+					!(res->flags & IORESOURCE_PREFETCH)) {
+				out += sprintf(out,
+					"start = %8.8lx, length = %8.8lx\n",
+					res->start, (res->end - res->start));
+			}
 		}
 		out += sprintf(out, "assigned resources: prefetchable memory\n");
-		index = 11;
-		res = new_slot->p_mem_head;
-		while (res && index--) {
-			out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
-			res = res->next;
+		for (index=0; index <= PCI_NUM_RESOURCES; index++) {
+			res = &(fdev->resource[index]);
+			if (res && (res->flags & (IORESOURCE_MEM |
+						IORESOURCE_PREFETCH))) {
+				out += sprintf(out,
+					"start = %8.8lx, length = %8.8lx\n",
+					res->start, (res->end - res->start));
+			}
 		}
 		out += sprintf(out, "assigned resources: IO\n");
-		index = 11;
-		res = new_slot->io_head;
-		while (res && index--) {
-			out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
-			res = res->next;
+		for (index=0; index <= PCI_NUM_RESOURCES; index++) {
+			res = &(fdev->resource[index]);
+			if (res && (res->flags & IORESOURCE_IO)) {
+				out += sprintf(out,
+					"start = %8.8lx, length = %8.8lx\n",
+					res->start, (res->end - res->start));
+			}
 		}
 		out += sprintf(out, "assigned resources: bus numbers\n");
-		index = 11;
-		res = new_slot->bus_head;
-		while (res && index--) {
-			out += sprintf(out, "start = %8.8x, length = %8.8x\n", res->base, res->length);
-			res = res->next;
-		}
+		if (fdev->subordinate)
+			out += sprintf(out, "start = %8.8x, length = %8.8x\n",
+				fdev->subordinate->secondary,
+				(fdev->subordinate->subordinate -
+				 fdev->subordinate->secondary));
+		else
+			out += sprintf(out, "start = %8.8x, length = %8.8x\n",
+					fdev->bus->number, 1);
 		slot=slot->next;
 	}