pciehp: poll cmd completion if hotplug interrupt is disabled
[safe/jmp/linux-2.6] / drivers / pci / probe.c
index 6186f4d..3706ce7 100644 (file)
@@ -9,6 +9,7 @@
 #include <linux/slab.h>
 #include <linux/module.h>
 #include <linux/cpumask.h>
+#include <linux/pci-aspm.h>
 #include "pci.h"
 
 #define CARDBUS_LATENCY_TIMER  176     /* secondary latency timer */
 LIST_HEAD(pci_root_buses);
 EXPORT_SYMBOL(pci_root_buses);
 
-LIST_HEAD(pci_devices);
+
+static int find_anything(struct device *dev, void *data)
+{
+       return 1;
+}
+
+/*
+ * Some device drivers need know if pci is initiated.
+ * Basically, we think pci is not initiated when there
+ * is no device to be found on the pci_bus_type.
+ */
+int no_pci_devices(void)
+{
+       struct device *dev;
+       int no_devices;
+
+       dev = bus_find_device(&pci_bus_type, NULL, NULL, find_anything);
+       no_devices = (dev == NULL);
+       put_device(dev);
+       return no_devices;
+}
+EXPORT_SYMBOL(no_pci_devices);
 
 #ifdef HAVE_PCI_LEGACY
 /**
@@ -33,34 +55,31 @@ LIST_HEAD(pci_devices);
  */
 static void pci_create_legacy_files(struct pci_bus *b)
 {
-       b->legacy_io = kmalloc(sizeof(struct bin_attribute) * 2,
+       b->legacy_io = kzalloc(sizeof(struct bin_attribute) * 2,
                               GFP_ATOMIC);
        if (b->legacy_io) {
-               memset(b->legacy_io, 0, sizeof(struct bin_attribute) * 2);
                b->legacy_io->attr.name = "legacy_io";
                b->legacy_io->size = 0xffff;
                b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
-               b->legacy_io->attr.owner = THIS_MODULE;
                b->legacy_io->read = pci_read_legacy_io;
                b->legacy_io->write = pci_write_legacy_io;
-               class_device_create_bin_file(&b->class_dev, b->legacy_io);
+               device_create_bin_file(&b->dev, b->legacy_io);
 
                /* Allocated above after the legacy_io struct */
                b->legacy_mem = b->legacy_io + 1;
                b->legacy_mem->attr.name = "legacy_mem";
                b->legacy_mem->size = 1024*1024;
                b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
-               b->legacy_mem->attr.owner = THIS_MODULE;
                b->legacy_mem->mmap = pci_mmap_legacy_mem;
-               class_device_create_bin_file(&b->class_dev, b->legacy_mem);
+               device_create_bin_file(&b->dev, b->legacy_mem);
        }
 }
 
 void pci_remove_legacy_files(struct pci_bus *b)
 {
        if (b->legacy_io) {
-               class_device_remove_bin_file(&b->class_dev, b->legacy_io);
-               class_device_remove_bin_file(&b->class_dev, b->legacy_mem);
+               device_remove_bin_file(&b->dev, b->legacy_io);
+               device_remove_bin_file(&b->dev, b->legacy_mem);
                kfree(b->legacy_io); /* both are allocated here */
        }
 }
@@ -72,24 +91,46 @@ void pci_remove_legacy_files(struct pci_bus *bus) { return; }
 /*
  * PCI Bus Class Devices
  */
-static ssize_t pci_bus_show_cpuaffinity(struct class_device *class_dev, char *buf)
+static ssize_t pci_bus_show_cpuaffinity(struct device *dev,
+                                       int type,
+                                       struct device_attribute *attr,
+                                       char *buf)
 {
-       cpumask_t cpumask = pcibus_to_cpumask(to_pci_bus(class_dev));
        int ret;
-
-       ret = cpumask_scnprintf(buf, PAGE_SIZE, cpumask);
-       if (ret < PAGE_SIZE)
-               buf[ret++] = '\n';
+       cpumask_t cpumask;
+
+       cpumask = pcibus_to_cpumask(to_pci_bus(dev));
+       ret = type?
+               cpulist_scnprintf(buf, PAGE_SIZE-2, cpumask):
+               cpumask_scnprintf(buf, PAGE_SIZE-2, cpumask);
+       buf[ret++] = '\n';
+       buf[ret] = '\0';
        return ret;
 }
-CLASS_DEVICE_ATTR(cpuaffinity, S_IRUGO, pci_bus_show_cpuaffinity, NULL);
+
+static ssize_t inline pci_bus_show_cpumaskaffinity(struct device *dev,
+                                       struct device_attribute *attr,
+                                       char *buf)
+{
+       return pci_bus_show_cpuaffinity(dev, 0, attr, buf);
+}
+
+static ssize_t inline pci_bus_show_cpulistaffinity(struct device *dev,
+                                       struct device_attribute *attr,
+                                       char *buf)
+{
+       return pci_bus_show_cpuaffinity(dev, 1, attr, buf);
+}
+
+DEVICE_ATTR(cpuaffinity,     S_IRUGO, pci_bus_show_cpumaskaffinity, NULL);
+DEVICE_ATTR(cpulistaffinity, S_IRUGO, pci_bus_show_cpulistaffinity, NULL);
 
 /*
  * PCI Bus Class
  */
-static void release_pcibus_dev(struct class_device *class_dev)
+static void release_pcibus_dev(struct device *dev)
 {
-       struct pci_bus *pci_bus = to_pci_bus(class_dev);
+       struct pci_bus *pci_bus = to_pci_bus(dev);
 
        if (pci_bus->bridge)
                put_device(pci_bus->bridge);
@@ -98,7 +139,7 @@ static void release_pcibus_dev(struct class_device *class_dev)
 
 static struct class pcibus_class = {
        .name           = "pci_bus",
-       .release        = &release_pcibus_dev,
+       .dev_release    = &release_pcibus_dev,
 };
 
 static int __init pcibus_class_init(void)
@@ -143,6 +184,32 @@ static u32 pci_size(u32 base, u32 maxbase, u32 mask)
        return size;
 }
 
+static u64 pci_size64(u64 base, u64 maxbase, u64 mask)
+{
+       u64 size = mask & maxbase;      /* Find the significant bits */
+       if (!size)
+               return 0;
+
+       /* Get the lowest of them to find the decode size, and
+          from that the extent.  */
+       size = (size & ~(size-1)) - 1;
+
+       /* base == maxbase can be valid only if the BAR has
+          already been programmed with all 1s.  */
+       if (base == maxbase && ((base | size) & mask) != mask)
+               return 0;
+
+       return size;
+}
+
+static inline int is_64bit_memory(u32 mask)
+{
+       if ((mask & (PCI_BASE_ADDRESS_SPACE|PCI_BASE_ADDRESS_MEM_TYPE_MASK)) ==
+           (PCI_BASE_ADDRESS_SPACE_MEMORY|PCI_BASE_ADDRESS_MEM_TYPE_64))
+               return 1;
+       return 0;
+}
+
 static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
 {
        unsigned int pos, reg, next;
@@ -150,6 +217,10 @@ static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
        struct resource *res;
 
        for(pos=0; pos<howmany; pos = next) {
+               u64 l64;
+               u64 sz64;
+               u32 raw_sz;
+
                next = pos+1;
                res = &dev->resource[pos];
                res->name = pci_name(dev);
@@ -162,9 +233,16 @@ static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
                        continue;
                if (l == 0xffffffff)
                        l = 0;
-               if ((l & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_MEMORY) {
-                       sz = pci_size(l, sz, PCI_BASE_ADDRESS_MEM_MASK);
-                       if (!sz)
+               raw_sz = sz;
+               if ((l & PCI_BASE_ADDRESS_SPACE) ==
+                               PCI_BASE_ADDRESS_SPACE_MEMORY) {
+                       sz = pci_size(l, sz, (u32)PCI_BASE_ADDRESS_MEM_MASK);
+                       /*
+                        * For 64bit prefetchable memory sz could be 0, if the
+                        * real size is bigger than 4G, so we need to check
+                        * szhi for that.
+                        */
+                       if (!is_64bit_memory(l) && !sz)
                                continue;
                        res->start = l & PCI_BASE_ADDRESS_MEM_MASK;
                        res->flags |= l & ~PCI_BASE_ADDRESS_MEM_MASK;
@@ -176,28 +254,40 @@ static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
                        res->flags |= l & ~PCI_BASE_ADDRESS_IO_MASK;
                }
                res->end = res->start + (unsigned long) sz;
-               res->flags |= pci_calc_resource_flags(l);
-               if ((l & (PCI_BASE_ADDRESS_SPACE | PCI_BASE_ADDRESS_MEM_TYPE_MASK))
-                   == (PCI_BASE_ADDRESS_SPACE_MEMORY | PCI_BASE_ADDRESS_MEM_TYPE_64)) {
-                       pci_read_config_dword(dev, reg+4, &l);
+               res->flags |= pci_calc_resource_flags(l) | IORESOURCE_SIZEALIGN;
+               if (is_64bit_memory(l)) {
+                       u32 szhi, lhi;
+
+                       pci_read_config_dword(dev, reg+4, &lhi);
+                       pci_write_config_dword(dev, reg+4, ~0);
+                       pci_read_config_dword(dev, reg+4, &szhi);
+                       pci_write_config_dword(dev, reg+4, lhi);
+                       sz64 = ((u64)szhi << 32) | raw_sz;
+                       l64 = ((u64)lhi << 32) | l;
+                       sz64 = pci_size64(l64, sz64, PCI_BASE_ADDRESS_MEM_MASK);
                        next++;
 #if BITS_PER_LONG == 64
-                       res->start |= ((unsigned long) l) << 32;
-                       res->end = res->start + sz;
-                       pci_write_config_dword(dev, reg+4, ~0);
-                       pci_read_config_dword(dev, reg+4, &sz);
-                       pci_write_config_dword(dev, reg+4, l);
-                       sz = pci_size(l, sz, 0xffffffff);
-                       if (sz) {
-                               /* This BAR needs > 4GB?  Wow. */
-                               res->end |= (unsigned long)sz<<32;
+                       if (!sz64) {
+                               res->start = 0;
+                               res->end = 0;
+                               res->flags = 0;
+                               continue;
                        }
+                       res->start = l64 & PCI_BASE_ADDRESS_MEM_MASK;
+                       res->end = res->start + sz64;
 #else
-                       if (l) {
-                               printk(KERN_ERR "PCI: Unable to handle 64-bit address for device %s\n", pci_name(dev));
+                       if (sz64 > 0x100000000ULL) {
+                               printk(KERN_ERR "PCI: Unable to handle 64-bit "
+                                       "BAR for device %s\n", pci_name(dev));
                                res->start = 0;
                                res->flags = 0;
-                               continue;
+                       } else if (lhi) {
+                               /* 64-bit wide address, treat as disabled */
+                               pci_write_config_dword(dev, reg,
+                                       l & ~(u32)PCI_BASE_ADDRESS_MEM_MASK);
+                               pci_write_config_dword(dev, reg+4, 0);
+                               res->start = 0;
+                               res->end = sz;
                        }
 #endif
                }
@@ -213,11 +303,12 @@ static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
                if (l == 0xffffffff)
                        l = 0;
                if (sz && sz != 0xffffffff) {
-                       sz = pci_size(l, sz, PCI_ROM_ADDRESS_MASK);
+                       sz = pci_size(l, sz, (u32)PCI_ROM_ADDRESS_MASK);
                        if (sz) {
                                res->flags = (l & IORESOURCE_ROM_ENABLE) |
                                  IORESOURCE_MEM | IORESOURCE_PREFETCH |
-                                 IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
+                                 IORESOURCE_READONLY | IORESOURCE_CACHEABLE |
+                                 IORESOURCE_SIZEALIGN;
                                res->start = l & PCI_ROM_ADDRESS_MASK;
                                res->end = res->start + (unsigned long) sz;
                        }
@@ -239,9 +330,8 @@ void __devinit pci_read_bridge_bases(struct pci_bus *child)
 
        if (dev->transparent) {
                printk(KERN_INFO "PCI: Transparent bridge - %s\n", pci_name(dev));
-               for(i = 0; i < PCI_BUS_NUM_RESOURCES; i++)
-                       child->resource[i] = child->parent->resource[i];
-               return;
+               for(i = 3; i < PCI_BUS_NUM_RESOURCES; i++)
+                       child->resource[i] = child->parent->resource[i - 3];
        }
 
        for(i=0; i<3; i++)
@@ -263,8 +353,10 @@ void __devinit pci_read_bridge_bases(struct pci_bus *child)
 
        if (base <= limit) {
                res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO;
-               res->start = base;
-               res->end = limit + 0xfff;
+               if (!res->start)
+                       res->start = base;
+               if (!res->end)
+                       res->end = limit + 0xfff;
        }
 
        res = child->resource[1];
@@ -313,13 +405,12 @@ void __devinit pci_read_bridge_bases(struct pci_bus *child)
        }
 }
 
-static struct pci_bus * __devinit pci_alloc_bus(void)
+static struct pci_bus * pci_alloc_bus(void)
 {
        struct pci_bus *b;
 
-       b = kmalloc(sizeof(*b), GFP_KERNEL);
+       b = kzalloc(sizeof(*b), GFP_KERNEL);
        if (b) {
-               memset(b, 0, sizeof(*b));
                INIT_LIST_HEAD(&b->node);
                INIT_LIST_HEAD(&b->children);
                INIT_LIST_HEAD(&b->devices);
@@ -327,8 +418,8 @@ static struct pci_bus * __devinit pci_alloc_bus(void)
        return b;
 }
 
-static struct pci_bus * __devinit
-pci_alloc_child_bus(struct pci_bus *parent, struct pci_dev *bridge, int busnr)
+static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent,
+                                          struct pci_dev *bridge, int busnr)
 {
        struct pci_bus *child;
        int i;
@@ -344,12 +435,15 @@ pci_alloc_child_bus(struct pci_bus *parent, struct pci_dev *bridge, int busnr)
        child->parent = parent;
        child->ops = parent->ops;
        child->sysdata = parent->sysdata;
+       child->bus_flags = parent->bus_flags;
        child->bridge = get_device(&bridge->dev);
 
-       child->class_dev.class = &pcibus_class;
-       sprintf(child->class_dev.class_id, "%04x:%02x", pci_domain_nr(child), busnr);
-       class_device_register(&child->class_dev);
-       class_device_create_file(&child->class_dev, &class_device_attr_cpuaffinity);
+       /* initialize some portions of the bus device, but don't register it
+        * now as the parent is not properly set up yet.  This device will get
+        * registered later in pci_bus_add_devices()
+        */
+       child->dev.class = &pcibus_class;
+       sprintf(child->dev.bus_id, "%04x:%02x", pci_domain_nr(child), busnr);
 
        /*
         * Set up the primary, secondary and subordinate
@@ -369,34 +463,35 @@ pci_alloc_child_bus(struct pci_bus *parent, struct pci_dev *bridge, int busnr)
        return child;
 }
 
-struct pci_bus * __devinit pci_add_new_bus(struct pci_bus *parent, struct pci_dev *dev, int busnr)
+struct pci_bus *__ref pci_add_new_bus(struct pci_bus *parent, struct pci_dev *dev, int busnr)
 {
        struct pci_bus *child;
 
        child = pci_alloc_child_bus(parent, dev, busnr);
-       if (child)
+       if (child) {
+               down_write(&pci_bus_sem);
                list_add_tail(&child->node, &parent->children);
+               up_write(&pci_bus_sem);
+       }
        return child;
 }
 
-static void pci_enable_crs(struct pci_dev *dev)
+static void pci_fixup_parent_subordinate_busnr(struct pci_bus *child, int max)
 {
-       u16 cap, rpctl;
-       int rpcap = pci_find_capability(dev, PCI_CAP_ID_EXP);
-       if (!rpcap)
-               return;
+       struct pci_bus *parent = child->parent;
 
-       pci_read_config_word(dev, rpcap + PCI_CAP_FLAGS, &cap);
-       if (((cap & PCI_EXP_FLAGS_TYPE) >> 4) != PCI_EXP_TYPE_ROOT_PORT)
+       /* Attempts to fix that up are really dangerous unless
+          we're going to re-assign all bus numbers. */
+       if (!pcibios_assign_all_busses())
                return;
 
-       pci_read_config_word(dev, rpcap + PCI_EXP_RTCTL, &rpctl);
-       rpctl |= PCI_EXP_RTCTL_CRSSVE;
-       pci_write_config_word(dev, rpcap + PCI_EXP_RTCTL, rpctl);
+       while (parent->parent && parent->subordinate < max) {
+               parent->subordinate = max;
+               pci_write_config_byte(parent->self, PCI_SUBORDINATE_BUS, max);
+               parent = parent->parent;
+       }
 }
 
-unsigned int __devinit pci_scan_child_bus(struct pci_bus *bus);
-
 /*
  * If it's a bridge, configure it and scan the bus behind it.
  * For CardBus bridges, we don't scan behind as the devices will
@@ -407,11 +502,11 @@ unsigned int __devinit pci_scan_child_bus(struct pci_bus *bus);
  * them, we proceed to assigning numbers to the remaining buses in
  * order to avoid overlaps between old and new bus numbers.
  */
-int __devinit pci_scan_bridge(struct pci_bus *bus, struct pci_dev * dev, int max, int pass)
+int __devinit pci_scan_bridge(struct pci_bus *bus, struct pci_dev *dev, int max, int pass)
 {
        struct pci_bus *child;
        int is_cardbus = (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS);
-       u32 buses, i;
+       u32 buses, i, j = 0;
        u16 bctl;
 
        pci_read_config_dword(dev, PCI_PRIMARY_BUS, &buses);
@@ -425,8 +520,6 @@ int __devinit pci_scan_bridge(struct pci_bus *bus, struct pci_dev * dev, int max
        pci_write_config_word(dev, PCI_BRIDGE_CONTROL,
                              bctl & ~PCI_BRIDGE_CTL_MASTER_ABORT);
 
-       pci_enable_crs(dev);
-
        if ((buses & 0xffff00) && !pcibios_assign_all_busses() && !is_cardbus) {
                unsigned int cmax, busnr;
                /*
@@ -434,7 +527,7 @@ int __devinit pci_scan_bridge(struct pci_bus *bus, struct pci_dev * dev, int max
                 * pass and just note the configuration.
                 */
                if (pass)
-                       return max;
+                       goto out;
                busnr = (buses >> 8) & 0xFF;
 
                /*
@@ -444,12 +537,12 @@ int __devinit pci_scan_bridge(struct pci_bus *bus, struct pci_dev * dev, int max
                if (pci_find_bus(pci_domain_nr(bus), busnr)) {
                        printk(KERN_INFO "PCI: Bus %04x:%02x already known\n",
                                        pci_domain_nr(bus), busnr);
-                       return max;
+                       goto out;
                }
 
-               child = pci_alloc_child_bus(bus, dev, busnr);
+               child = pci_add_new_bus(bus, dev, busnr);
                if (!child)
-                       return max;
+                       goto out;
                child->primary = buses & 0xFF;
                child->subordinate = (buses >> 16) & 0xFF;
                child->bridge_ctl = bctl;
@@ -464,8 +557,18 @@ int __devinit pci_scan_bridge(struct pci_bus *bus, struct pci_dev * dev, int max
                 * We need to assign a number to this bus which we always
                 * do in the second pass.
                 */
-               if (!pass)
-                       return max;
+               if (!pass) {
+                       if (pcibios_assign_all_busses())
+                               /* Temporarily disable forwarding of the
+                                  configuration cycles on all bridges in
+                                  this bus segment to avoid possible
+                                  conflicts in the second pass between two
+                                  bridges programmed with overlapping
+                                  bus ranges. */
+                               pci_write_config_dword(dev, PCI_PRIMARY_BUS,
+                                                      buses & ~0xffffff);
+                       goto out;
+               }
 
                /* Clear errors */
                pci_write_config_word(dev, PCI_STATUS, 0xffff);
@@ -473,8 +576,8 @@ int __devinit pci_scan_bridge(struct pci_bus *bus, struct pci_dev * dev, int max
                /* Prevent assigning a bus number that already exists.
                 * This can happen when a bridge is hot-plugged */
                if (pci_find_bus(pci_domain_nr(bus), max+1))
-                       return max;
-               child = pci_alloc_child_bus(bus, dev, ++max);
+                       goto out;
+               child = pci_add_new_bus(bus, dev, ++max);
                buses = (buses & 0xff000000)
                      | ((unsigned int)(child->primary)     <<  0)
                      | ((unsigned int)(child->secondary)   <<  8)
@@ -495,21 +598,52 @@ int __devinit pci_scan_bridge(struct pci_bus *bus, struct pci_dev * dev, int max
                pci_write_config_dword(dev, PCI_PRIMARY_BUS, buses);
 
                if (!is_cardbus) {
-                       child->bridge_ctl = PCI_BRIDGE_CTL_NO_ISA;
-
+                       child->bridge_ctl = bctl;
+                       /*
+                        * Adjust subordinate busnr in parent buses.
+                        * We do this before scanning for children because
+                        * some devices may not be detected if the bios
+                        * was lazy.
+                        */
+                       pci_fixup_parent_subordinate_busnr(child, max);
                        /* Now we can scan all subordinate buses... */
                        max = pci_scan_child_bus(child);
+                       /*
+                        * now fix it up again since we have found
+                        * the real value of max.
+                        */
+                       pci_fixup_parent_subordinate_busnr(child, max);
                } else {
                        /*
                         * For CardBus bridges, we leave 4 bus numbers
                         * as cards with a PCI-to-PCI bridge can be
                         * inserted later.
                         */
-                       for (i=0; i<CARDBUS_RESERVE_BUSNR; i++)
+                       for (i=0; i<CARDBUS_RESERVE_BUSNR; i++) {
+                               struct pci_bus *parent = bus;
                                if (pci_find_bus(pci_domain_nr(bus),
                                                        max+i+1))
                                        break;
+                               while (parent->parent) {
+                                       if ((!pcibios_assign_all_busses()) &&
+                                           (parent->subordinate > max) &&
+                                           (parent->subordinate <= max+i)) {
+                                               j = 1;
+                                       }
+                                       parent = parent->parent;
+                               }
+                               if (j) {
+                                       /*
+                                        * Often, there are two cardbus bridges
+                                        * -- try to leave one valid bus number
+                                        * for each one.
+                                        */
+                                       i /= 2;
+                                       break;
+                               }
+                       }
                        max += i;
+                       pci_fixup_parent_subordinate_busnr(child, max);
                }
                /*
                 * Set the subordinate bus number to its real value.
@@ -518,9 +652,30 @@ int __devinit pci_scan_bridge(struct pci_bus *bus, struct pci_dev * dev, int max
                pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, max);
        }
 
-       pci_write_config_word(dev, PCI_BRIDGE_CONTROL, bctl);
+       sprintf(child->name,
+               (is_cardbus ? "PCI CardBus %04x:%02x" : "PCI Bus %04x:%02x"),
+               pci_domain_nr(bus), child->number);
+
+       /* Has only triggered on CardBus, fixup is in yenta_socket */
+       while (bus->parent) {
+               if ((child->subordinate > bus->subordinate) ||
+                   (child->number > bus->subordinate) ||
+                   (child->number < bus->number) ||
+                   (child->subordinate < bus->number)) {
+                       pr_debug("PCI: Bus #%02x (-#%02x) is %s "
+                               "hidden behind%s bridge #%02x (-#%02x)\n",
+                               child->number, child->subordinate,
+                               (bus->number > child->subordinate &&
+                                bus->subordinate < child->number) ?
+                                       "wholly" : "partially",
+                               bus->self->transparent ? " transparent" : "",
+                               bus->number, bus->subordinate);
+               }
+               bus = bus->parent;
+       }
 
-       sprintf(child->name, (is_cardbus ? "PCI CardBus #%02x" : "PCI Bus #%02x"), child->number);
+out:
+       pci_write_config_word(dev, PCI_BRIDGE_CONTROL, bctl);
 
        return max;
 }
@@ -534,11 +689,14 @@ static void pci_read_irq(struct pci_dev *dev)
        unsigned char irq;
 
        pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq);
+       dev->pin = irq;
        if (irq)
                pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &irq);
        dev->irq = irq;
 }
 
+#define LEGACY_IO_RESOURCE     (IORESOURCE_IO | IORESOURCE_PCI_FIXED)
+
 /**
  * pci_setup_device - fill in class and map information of a device
  * @dev: the device structure to fill
@@ -557,6 +715,7 @@ static int pci_setup_device(struct pci_dev * dev)
                dev->bus->number, PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
 
        pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
+       dev->revision = class & 0xff;
        class >>= 8;                                /* upper 3 bytes */
        dev->class = class;
        class >>= 8;
@@ -565,7 +724,7 @@ static int pci_setup_device(struct pci_dev * dev)
                 dev->vendor, dev->device, class, dev->hdr_type);
 
        /* "Unknown power state" */
-       dev->current_state = 4;
+       dev->current_state = PCI_UNKNOWN;
 
        /* Early fixups, before probing the BARs */
        pci_fixup_device(pci_fixup_early, dev);
@@ -579,6 +738,33 @@ static int pci_setup_device(struct pci_dev * dev)
                pci_read_bases(dev, 6, PCI_ROM_ADDRESS);
                pci_read_config_word(dev, PCI_SUBSYSTEM_VENDOR_ID, &dev->subsystem_vendor);
                pci_read_config_word(dev, PCI_SUBSYSTEM_ID, &dev->subsystem_device);
+
+               /*
+                *      Do the ugly legacy mode stuff here rather than broken chip
+                *      quirk code. Legacy mode ATA controllers have fixed
+                *      addresses. These are not always echoed in BAR0-3, and
+                *      BAR0-3 in a few cases contain junk!
+                */
+               if (class == PCI_CLASS_STORAGE_IDE) {
+                       u8 progif;
+                       pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
+                       if ((progif & 1) == 0) {
+                               dev->resource[0].start = 0x1F0;
+                               dev->resource[0].end = 0x1F7;
+                               dev->resource[0].flags = LEGACY_IO_RESOURCE;
+                               dev->resource[1].start = 0x3F6;
+                               dev->resource[1].end = 0x3F6;
+                               dev->resource[1].flags = LEGACY_IO_RESOURCE;
+                       }
+                       if ((progif & 4) == 0) {
+                               dev->resource[2].start = 0x170;
+                               dev->resource[2].end = 0x177;
+                               dev->resource[2].flags = LEGACY_IO_RESOURCE;
+                               dev->resource[3].start = 0x376;
+                               dev->resource[3].end = 0x376;
+                               dev->resource[3].flags = LEGACY_IO_RESOURCE;
+                       }
+               }
                break;
 
        case PCI_HEADER_TYPE_BRIDGE:                /* bridge header */
@@ -587,6 +773,7 @@ static int pci_setup_device(struct pci_dev * dev)
                /* The PCI-to-PCI bridge spec requires that subtractive
                   decoding (i.e. transparent) bridge must have programming
                   interface code of 0x01. */ 
+               pci_read_irq(dev);
                dev->transparent = ((dev->class & 0xff) == 1);
                pci_read_bases(dev, 2, PCI_ROM_ADDRESS1);
                break;
@@ -627,11 +814,26 @@ static void pci_release_dev(struct device *dev)
        struct pci_dev *pci_dev;
 
        pci_dev = to_pci_dev(dev);
+       pci_vpd_release(pci_dev);
        kfree(pci_dev);
 }
 
+static void set_pcie_port_type(struct pci_dev *pdev)
+{
+       int pos;
+       u16 reg16;
+
+       pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
+       if (!pos)
+               return;
+       pdev->is_pcie = 1;
+       pci_read_config_word(pdev, pos + PCI_EXP_FLAGS, &reg16);
+       pdev->pcie_type = (reg16 & PCI_EXP_FLAGS_TYPE) >> 4;
+}
+
 /**
  * pci_cfg_space_size - get the configuration space size of the PCI device.
+ * @dev: PCI device
  *
  * Regular PCI devices have 256 bytes, but PCI-X 2 and PCI Express devices
  * have 4096 bytes.  Even if the device is capable, that doesn't mean we can
@@ -640,7 +842,22 @@ static void pci_release_dev(struct device *dev)
  * reading the dword at 0x100 which must either be 0 or a valid extended
  * capability header.
  */
-static int pci_cfg_space_size(struct pci_dev *dev)
+int pci_cfg_space_size_ext(struct pci_dev *dev)
+{
+       u32 status;
+
+       if (pci_read_config_dword(dev, 256, &status) != PCIBIOS_SUCCESSFUL)
+               goto fail;
+       if (status == 0xffffffff)
+               goto fail;
+
+       return PCI_CFG_SPACE_EXP_SIZE;
+
+ fail:
+       return PCI_CFG_SPACE_SIZE;
+}
+
+int pci_cfg_space_size(struct pci_dev *dev)
 {
        int pos;
        u32 status;
@@ -656,12 +873,7 @@ static int pci_cfg_space_size(struct pci_dev *dev)
                        goto fail;
        }
 
-       if (pci_read_config_dword(dev, 256, &status) != PCIBIOS_SUCCESSFUL)
-               goto fail;
-       if (status == 0xffffffff)
-               goto fail;
-
-       return PCI_CFG_SPACE_EXP_SIZE;
+       return pci_cfg_space_size_ext(dev);
 
  fail:
        return PCI_CFG_SPACE_SIZE;
@@ -672,12 +884,27 @@ static void pci_release_bus_bridge_dev(struct device *dev)
        kfree(dev);
 }
 
+struct pci_dev *alloc_pci_dev(void)
+{
+       struct pci_dev *dev;
+
+       dev = kzalloc(sizeof(struct pci_dev), GFP_KERNEL);
+       if (!dev)
+               return NULL;
+
+       INIT_LIST_HEAD(&dev->bus_list);
+
+       pci_msi_init_pci_dev(dev);
+
+       return dev;
+}
+EXPORT_SYMBOL(alloc_pci_dev);
+
 /*
  * Read the config data for a PCI device, sanity-check it
  * and fill in the dev structure...
  */
-static struct pci_dev * __devinit
-pci_scan_device(struct pci_bus *bus, int devfn)
+static struct pci_dev *pci_scan_device(struct pci_bus *bus, int devfn)
 {
        struct pci_dev *dev;
        u32 l;
@@ -711,11 +938,10 @@ pci_scan_device(struct pci_bus *bus, int devfn)
        if (pci_bus_read_config_byte(bus, devfn, PCI_HEADER_TYPE, &hdr_type))
                return NULL;
 
-       dev = kmalloc(sizeof(struct pci_dev), GFP_KERNEL);
+       dev = alloc_pci_dev();
        if (!dev)
                return NULL;
 
-       memset(dev, 0, sizeof(struct pci_dev));
        dev->bus = bus;
        dev->sysdata = bus->sysdata;
        dev->dev.parent = bus->bridge;
@@ -726,6 +952,8 @@ pci_scan_device(struct pci_bus *bus, int devfn)
        dev->vendor = l & 0xffff;
        dev->device = (l >> 16) & 0xffff;
        dev->cfg_size = pci_cfg_space_size(dev);
+       dev->error_state = pci_channel_io_normal;
+       set_pcie_port_type(dev);
 
        /* Assume 32-bit PCI; let 64-bit PCI cards (which are far rarer)
           set this higher, assuming the system even supports it.  */
@@ -734,29 +962,25 @@ pci_scan_device(struct pci_bus *bus, int devfn)
                kfree(dev);
                return NULL;
        }
-       device_initialize(&dev->dev);
-       dev->dev.release = pci_release_dev;
-       pci_dev_get(dev);
-
-       pci_name_device(dev);
 
-       dev->dev.dma_mask = &dev->dma_mask;
-       dev->dev.coherent_dma_mask = 0xffffffffull;
+       pci_vpd_pci22_init(dev);
 
        return dev;
 }
 
-struct pci_dev * __devinit
-pci_scan_single_device(struct pci_bus *bus, int devfn)
+void pci_device_add(struct pci_dev *dev, struct pci_bus *bus)
 {
-       struct pci_dev *dev;
+       device_initialize(&dev->dev);
+       dev->dev.release = pci_release_dev;
+       pci_dev_get(dev);
 
-       dev = pci_scan_device(bus, devfn);
-       pci_scan_msi_device(dev);
+       dev->dev.dma_mask = &dev->dma_mask;
+       dev->dev.dma_parms = &dev->dma_parms;
+       dev->dev.coherent_dma_mask = 0xffffffffull;
+
+       pci_set_dma_max_seg_size(dev, 65536);
+       pci_set_dma_seg_boundary(dev, 0xffffffff);
 
-       if (!dev)
-               return NULL;
-       
        /* Fix up broken headers */
        pci_fixup_device(pci_fixup_header, dev);
 
@@ -764,11 +988,24 @@ pci_scan_single_device(struct pci_bus *bus, int devfn)
         * Add the device to our list of discovered devices
         * and the bus list for fixup functions, etc.
         */
-       INIT_LIST_HEAD(&dev->global_list);
+       down_write(&pci_bus_sem);
        list_add_tail(&dev->bus_list, &bus->devices);
+       up_write(&pci_bus_sem);
+}
+
+struct pci_dev *__ref pci_scan_single_device(struct pci_bus *bus, int devfn)
+{
+       struct pci_dev *dev;
+
+       dev = pci_scan_device(bus, devfn);
+       if (!dev)
+               return NULL;
+
+       pci_device_add(dev, bus);
 
        return dev;
 }
+EXPORT_SYMBOL(pci_scan_single_device);
 
 /**
  * pci_scan_slot - scan a PCI slot on a bus for devices.
@@ -777,9 +1014,9 @@ pci_scan_single_device(struct pci_bus *bus, int devfn)
  *
  * Scan a PCI slot on the specified PCI bus for devices, adding
  * discovered devices to the @bus->devices list.  New devices
- * will have an empty dev->global_list head.
+ * will not have is_added set.
  */
-int __devinit pci_scan_slot(struct pci_bus *bus, int devfn)
+int pci_scan_slot(struct pci_bus *bus, int devfn)
 {
        int func, nr = 0;
        int scan_all_fns;
@@ -809,6 +1046,10 @@ int __devinit pci_scan_slot(struct pci_bus *bus, int devfn)
                                break;
                }
        }
+
+       if (bus->self)
+               pcie_aspm_init_link_state(bus->self);
+
        return nr;
 }
 
@@ -848,21 +1089,12 @@ unsigned int __devinit pci_scan_child_bus(struct pci_bus *bus)
        return max;
 }
 
-unsigned int __devinit pci_do_scan_bus(struct pci_bus *bus)
+void __attribute__((weak)) set_pci_bus_resources_arch_default(struct pci_bus *b)
 {
-       unsigned int max;
-
-       max = pci_scan_child_bus(bus);
-
-       /*
-        * Make the discovered devices available.
-        */
-       pci_bus_add_devices(bus);
-
-       return max;
 }
 
-struct pci_bus * __devinit pci_scan_bus_parented(struct device *parent, int bus, struct pci_ops *ops, void *sysdata)
+struct pci_bus * pci_create_bus(struct device *parent,
+               int bus, struct pci_ops *ops, void *sysdata)
 {
        int error;
        struct pci_bus *b;
@@ -886,7 +1118,10 @@ struct pci_bus * __devinit pci_scan_bus_parented(struct device *parent, int bus,
                pr_debug("PCI: Bus %04x:%02x already known\n", pci_domain_nr(b), bus);
                goto err_out;
        }
+
+       down_write(&pci_bus_sem);
        list_add_tail(&b->node, &pci_root_buses);
+       up_write(&pci_bus_sem);
 
        memset(dev, 0, sizeof(*dev));
        dev->parent = parent;
@@ -897,50 +1132,121 @@ struct pci_bus * __devinit pci_scan_bus_parented(struct device *parent, int bus,
                goto dev_reg_err;
        b->bridge = get_device(dev);
 
-       b->class_dev.class = &pcibus_class;
-       sprintf(b->class_dev.class_id, "%04x:%02x", pci_domain_nr(b), bus);
-       error = class_device_register(&b->class_dev);
+       if (!parent)
+               set_dev_node(b->bridge, pcibus_to_node(b));
+
+       b->dev.class = &pcibus_class;
+       b->dev.parent = b->bridge;
+       sprintf(b->dev.bus_id, "%04x:%02x", pci_domain_nr(b), bus);
+       error = device_register(&b->dev);
        if (error)
                goto class_dev_reg_err;
-       error = class_device_create_file(&b->class_dev, &class_device_attr_cpuaffinity);
+       error = device_create_file(&b->dev, &dev_attr_cpuaffinity);
        if (error)
-               goto class_dev_create_file_err;
+               goto dev_create_file_err;
 
        /* Create legacy_io and legacy_mem files for this bus */
        pci_create_legacy_files(b);
 
-       error = sysfs_create_link(&b->class_dev.kobj, &b->bridge->kobj, "bridge");
-       if (error)
-               goto sys_create_link_err;
-
        b->number = b->secondary = bus;
        b->resource[0] = &ioport_resource;
        b->resource[1] = &iomem_resource;
 
-       b->subordinate = pci_scan_child_bus(b);
+       set_pci_bus_resources_arch_default(b);
 
        return b;
 
-sys_create_link_err:
-       class_device_remove_file(&b->class_dev, &class_device_attr_cpuaffinity);
-class_dev_create_file_err:
-       class_device_unregister(&b->class_dev);
+dev_create_file_err:
+       device_unregister(&b->dev);
 class_dev_reg_err:
        device_unregister(dev);
 dev_reg_err:
+       down_write(&pci_bus_sem);
        list_del(&b->node);
+       up_write(&pci_bus_sem);
 err_out:
        kfree(dev);
        kfree(b);
        return NULL;
 }
+
+struct pci_bus * __devinit pci_scan_bus_parented(struct device *parent,
+               int bus, struct pci_ops *ops, void *sysdata)
+{
+       struct pci_bus *b;
+
+       b = pci_create_bus(parent, bus, ops, sysdata);
+       if (b)
+               b->subordinate = pci_scan_child_bus(b);
+       return b;
+}
 EXPORT_SYMBOL(pci_scan_bus_parented);
 
 #ifdef CONFIG_HOTPLUG
 EXPORT_SYMBOL(pci_add_new_bus);
-EXPORT_SYMBOL(pci_do_scan_bus);
 EXPORT_SYMBOL(pci_scan_slot);
 EXPORT_SYMBOL(pci_scan_bridge);
-EXPORT_SYMBOL(pci_scan_single_device);
 EXPORT_SYMBOL_GPL(pci_scan_child_bus);
 #endif
+
+static int __init pci_sort_bf_cmp(const struct pci_dev *a, const struct pci_dev *b)
+{
+       if      (pci_domain_nr(a->bus) < pci_domain_nr(b->bus)) return -1;
+       else if (pci_domain_nr(a->bus) > pci_domain_nr(b->bus)) return  1;
+
+       if      (a->bus->number < b->bus->number) return -1;
+       else if (a->bus->number > b->bus->number) return  1;
+
+       if      (a->devfn < b->devfn) return -1;
+       else if (a->devfn > b->devfn) return  1;
+
+       return 0;
+}
+
+/*
+ * Yes, this forcably breaks the klist abstraction temporarily.  It
+ * just wants to sort the klist, not change reference counts and
+ * take/drop locks rapidly in the process.  It does all this while
+ * holding the lock for the list, so objects can't otherwise be
+ * added/removed while we're swizzling.
+ */
+static void __init pci_insertion_sort_klist(struct pci_dev *a, struct list_head *list)
+{
+       struct list_head *pos;
+       struct klist_node *n;
+       struct device *dev;
+       struct pci_dev *b;
+
+       list_for_each(pos, list) {
+               n = container_of(pos, struct klist_node, n_node);
+               dev = container_of(n, struct device, knode_bus);
+               b = to_pci_dev(dev);
+               if (pci_sort_bf_cmp(a, b) <= 0) {
+                       list_move_tail(&a->dev.knode_bus.n_node, &b->dev.knode_bus.n_node);
+                       return;
+               }
+       }
+       list_move_tail(&a->dev.knode_bus.n_node, list);
+}
+
+void __init pci_sort_breadthfirst(void)
+{
+       LIST_HEAD(sorted_devices);
+       struct list_head *pos, *tmp;
+       struct klist_node *n;
+       struct device *dev;
+       struct pci_dev *pdev;
+       struct klist *device_klist;
+
+       device_klist = bus_get_device_klist(&pci_bus_type);
+
+       spin_lock(&device_klist->k_lock);
+       list_for_each_safe(pos, tmp, &device_klist->k_list) {
+               n = container_of(pos, struct klist_node, n_node);
+               dev = container_of(n, struct device, knode_bus);
+               pdev = to_pci_dev(dev);
+               pci_insertion_sort_klist(pdev, &sorted_devices);
+       }
+       list_splice(&sorted_devices, &device_klist->k_list);
+       spin_unlock(&device_klist->k_lock);
+}