PCI: Remove 3 incorrect MSI quirks.
[safe/jmp/linux-2.6] / drivers / pci / probe.c
index 3bc0fcd..463a5a9 100644 (file)
@@ -22,6 +22,18 @@ EXPORT_SYMBOL(pci_root_buses);
 
 LIST_HEAD(pci_devices);
 
+/*
+ * Some device drivers need know if pci is initiated.
+ * Basically, we think pci is not initiated when there
+ * is no device in list of pci_devices.
+ */
+int no_pci_devices(void)
+{
+       return list_empty(&pci_devices);
+}
+
+EXPORT_SYMBOL(no_pci_devices);
+
 #ifdef HAVE_PCI_LEGACY
 /**
  * pci_create_legacy_files - create legacy I/O port and memory files
@@ -33,14 +45,12 @@ 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);
@@ -50,7 +60,6 @@ static void pci_create_legacy_files(struct pci_bus *b)
                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);
        }
@@ -145,6 +154,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;
@@ -152,6 +187,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);
@@ -164,9 +203,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) {
+               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);
-                       if (!sz)
+                       /*
+                        * 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;
@@ -179,27 +225,39 @@ static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
                }
                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);
+               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
                }
@@ -218,8 +276,7 @@ static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
                        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_MEM | IORESOURCE_READONLY;
                                res->start = l & PCI_ROM_ADDRESS_MASK;
                                res->end = res->start + (unsigned long) sz;
                        }
@@ -227,7 +284,7 @@ static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
        }
 }
 
-void __devinit pci_read_bridge_bases(struct pci_bus *child)
+void pci_read_bridge_bases(struct pci_bus *child)
 {
        struct pci_dev *dev = child->self;
        u8 io_base_lo, io_limit_lo;
@@ -316,13 +373,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);
@@ -335,6 +391,7 @@ pci_alloc_child_bus(struct pci_bus *parent, struct pci_dev *bridge, int busnr)
 {
        struct pci_bus *child;
        int i;
+       int retval;
 
        /*
         * Allocate a new bus, and inherit stuff from the parent..
@@ -347,12 +404,18 @@ 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);
+       retval = class_device_register(&child->class_dev);
+       if (retval)
+               goto error_register;
+       retval = class_device_create_file(&child->class_dev,
+                                         &class_device_attr_cpuaffinity);
+       if (retval)
+               goto error_file_create;
 
        /*
         * Set up the primary, secondary and subordinate
@@ -370,17 +433,23 @@ pci_alloc_child_bus(struct pci_bus *parent, struct pci_dev *bridge, int busnr)
        bridge->subordinate = child;
 
        return child;
+
+error_file_create:
+       class_device_unregister(&child->class_dev);
+error_register:
+       kfree(child);
+       return NULL;
 }
 
-struct pci_bus * __devinit pci_add_new_bus(struct pci_bus *parent, struct pci_dev *dev, int busnr)
+struct pci_bus *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) {
-               spin_lock(&pci_bus_lock);
+               down_write(&pci_bus_sem);
                list_add_tail(&child->node, &parent->children);
-               spin_unlock(&pci_bus_lock);
+               up_write(&pci_bus_sem);
        }
        return child;
 }
@@ -401,7 +470,7 @@ static void pci_enable_crs(struct pci_dev *dev)
        pci_write_config_word(dev, rpcap + PCI_EXP_RTCTL, rpctl);
 }
 
-static void __devinit pci_fixup_parent_subordinate_busnr(struct pci_bus *child, int max)
+static void pci_fixup_parent_subordinate_busnr(struct pci_bus *child, int max)
 {
        struct pci_bus *parent = child->parent;
 
@@ -417,7 +486,7 @@ static void __devinit pci_fixup_parent_subordinate_busnr(struct pci_bus *child,
        }
 }
 
-unsigned int __devinit pci_scan_child_bus(struct pci_bus *bus);
+unsigned int pci_scan_child_bus(struct pci_bus *bus);
 
 /*
  * If it's a bridge, configure it and scan the bus behind it.
@@ -429,7 +498,7 @@ 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 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);
@@ -527,7 +596,7 @@ 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 = bctl | PCI_BRIDGE_CTL_NO_ISA;
+                       child->bridge_ctl = bctl;
                        /*
                         * Adjust subordinate busnr in parent buses.
                         * We do this before scanning for children because
@@ -583,18 +652,20 @@ int __devinit pci_scan_bridge(struct pci_bus *bus, struct pci_dev * dev, int max
 
        sprintf(child->name, (is_cardbus ? "PCI CardBus #%02x" : "PCI Bus #%02x"), 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)) {
-                       printk(KERN_WARNING "PCI: Bus #%02x (-#%02x) may be "
-                              "hidden behind%s bridge #%02x (-#%02x)%s\n",
-                              child->number, child->subordinate,
-                              bus->self->transparent ? " transparent" : " ",
-                              bus->number, bus->subordinate,
-                              pcibios_assign_all_busses() ? " " :
-                              " (try 'pci=assign-busses')");
+                       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;
        }
@@ -620,6 +691,8 @@ static void pci_read_irq(struct pci_dev *dev)
        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
@@ -638,6 +711,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;
@@ -660,6 +734,57 @@ 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;
+                       struct pci_bus_region region;
+
+                       pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
+                       if ((progif & 1) == 0) {
+                               struct resource resource = {
+                                       .start = 0x1F0,
+                                       .end = 0x1F7,
+                                       .flags = LEGACY_IO_RESOURCE,
+                               };
+
+                               pcibios_resource_to_bus(dev, &region, &resource);
+                               dev->resource[0].start = region.start;
+                               dev->resource[0].end = region.end;
+                               dev->resource[0].flags = resource.flags;
+                               resource.start = 0x3F6;
+                               resource.end = 0x3F6;
+                               resource.flags = LEGACY_IO_RESOURCE;
+                               pcibios_resource_to_bus(dev, &region, &resource);
+                               dev->resource[1].start = region.start;
+                               dev->resource[1].end = region.end;
+                               dev->resource[1].flags = resource.flags;
+                       }
+                       if ((progif & 4) == 0) {
+                               struct resource resource = {
+                                       .start = 0x170,
+                                       .end = 0x177,
+                                       .flags = LEGACY_IO_RESOURCE,
+                               };
+
+                               pcibios_resource_to_bus(dev, &region, &resource);
+                               dev->resource[2].start = region.start;
+                               dev->resource[2].end = region.end;
+                               dev->resource[2].flags = resource.flags;
+                               resource.start = 0x376;
+                               resource.end = 0x376;
+                               resource.flags = LEGACY_IO_RESOURCE;
+                               pcibios_resource_to_bus(dev, &region, &resource);
+                               dev->resource[3].start = region.start;
+                               dev->resource[3].end = region.end;
+                               dev->resource[3].flags = resource.flags;
+                       }
+               }
                break;
 
        case PCI_HEADER_TYPE_BRIDGE:                /* bridge header */
@@ -712,6 +837,19 @@ static void pci_release_dev(struct device *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
@@ -755,6 +893,23 @@ 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->global_list);
+       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...
@@ -794,11 +949,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;
@@ -809,6 +963,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.  */
@@ -821,12 +977,13 @@ pci_scan_device(struct pci_bus *bus, int devfn)
        return dev;
 }
 
-void __devinit pci_device_add(struct pci_dev *dev, struct pci_bus *bus)
+void pci_device_add(struct pci_dev *dev, struct pci_bus *bus)
 {
        device_initialize(&dev->dev);
        dev->dev.release = pci_release_dev;
        pci_dev_get(dev);
 
+       set_dev_node(&dev->dev, pcibus_to_node(bus));
        dev->dev.dma_mask = &dev->dma_mask;
        dev->dev.coherent_dma_mask = 0xffffffffull;
 
@@ -838,13 +995,12 @@ void __devinit pci_device_add(struct pci_dev *dev, struct pci_bus *bus)
         * and the bus list for fixup functions, etc.
         */
        INIT_LIST_HEAD(&dev->global_list);
-       spin_lock(&pci_bus_lock);
+       down_write(&pci_bus_sem);
        list_add_tail(&dev->bus_list, &bus->devices);
-       spin_unlock(&pci_bus_lock);
+       up_write(&pci_bus_sem);
 }
 
-struct pci_dev * __devinit
-pci_scan_single_device(struct pci_bus *bus, int devfn)
+struct pci_dev *pci_scan_single_device(struct pci_bus *bus, int devfn)
 {
        struct pci_dev *dev;
 
@@ -853,7 +1009,6 @@ pci_scan_single_device(struct pci_bus *bus, int devfn)
                return NULL;
 
        pci_device_add(dev, bus);
-       pci_scan_msi_device(dev);
 
        return dev;
 }
@@ -867,7 +1022,7 @@ pci_scan_single_device(struct pci_bus *bus, int devfn)
  * discovered devices to the @bus->devices list.  New devices
  * will have an empty dev->global_list head.
  */
-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;
@@ -900,7 +1055,7 @@ int __devinit pci_scan_slot(struct pci_bus *bus, int devfn)
        return nr;
 }
 
-unsigned int __devinit pci_scan_child_bus(struct pci_bus *bus)
+unsigned int pci_scan_child_bus(struct pci_bus *bus)
 {
        unsigned int devfn, pass, max = bus->secondary;
        struct pci_dev *dev;
@@ -950,7 +1105,7 @@ unsigned int __devinit pci_do_scan_bus(struct pci_bus *bus)
        return max;
 }
 
-struct pci_bus * __devinit pci_create_bus(struct device *parent,
+struct pci_bus * pci_create_bus(struct device *parent,
                int bus, struct pci_ops *ops, void *sysdata)
 {
        int error;
@@ -975,9 +1130,10 @@ struct pci_bus * __devinit pci_create_bus(struct device *parent,
                pr_debug("PCI: Bus %04x:%02x already known\n", pci_domain_nr(b), bus);
                goto err_out;
        }
-       spin_lock(&pci_bus_lock);
+
+       down_write(&pci_bus_sem);
        list_add_tail(&b->node, &pci_root_buses);
-       spin_unlock(&pci_bus_lock);
+       up_write(&pci_bus_sem);
 
        memset(dev, 0, sizeof(*dev));
        dev->parent = parent;
@@ -1017,9 +1173,9 @@ class_dev_create_file_err:
 class_dev_reg_err:
        device_unregister(dev);
 dev_reg_err:
-       spin_lock(&pci_bus_lock);
+       down_write(&pci_bus_sem);
        list_del(&b->node);
-       spin_unlock(&pci_bus_lock);
+       up_write(&pci_bus_sem);
 err_out:
        kfree(dev);
        kfree(b);
@@ -1027,7 +1183,7 @@ err_out:
 }
 EXPORT_SYMBOL_GPL(pci_create_bus);
 
-struct pci_bus * __devinit pci_scan_bus_parented(struct device *parent,
+struct pci_bus *pci_scan_bus_parented(struct device *parent,
                int bus, struct pci_ops *ops, void *sysdata)
 {
        struct pci_bus *b;
@@ -1047,3 +1203,95 @@ 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);
+}
+
+static void __init pci_sort_breadthfirst_klist(void)
+{
+       LIST_HEAD(sorted_devices);
+       struct list_head *pos, *tmp;
+       struct klist_node *n;
+       struct device *dev;
+       struct pci_dev *pdev;
+
+       spin_lock(&pci_bus_type.klist_devices.k_lock);
+       list_for_each_safe(pos, tmp, &pci_bus_type.klist_devices.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, &pci_bus_type.klist_devices.k_list);
+       spin_unlock(&pci_bus_type.klist_devices.k_lock);
+}
+
+static void __init pci_insertion_sort_devices(struct pci_dev *a, struct list_head *list)
+{
+       struct pci_dev *b;
+
+       list_for_each_entry(b, list, global_list) {
+               if (pci_sort_bf_cmp(a, b) <= 0) {
+                       list_move_tail(&a->global_list, &b->global_list);
+                       return;
+               }
+       }
+       list_move_tail(&a->global_list, list);
+}
+
+static void __init pci_sort_breadthfirst_devices(void)
+{
+       LIST_HEAD(sorted_devices);
+       struct pci_dev *dev, *tmp;
+
+       down_write(&pci_bus_sem);
+       list_for_each_entry_safe(dev, tmp, &pci_devices, global_list) {
+               pci_insertion_sort_devices(dev, &sorted_devices);
+       }
+       list_splice(&sorted_devices, &pci_devices);
+       up_write(&pci_bus_sem);
+}
+
+void __init pci_sort_breadthfirst(void)
+{
+       pci_sort_breadthfirst_devices();
+       pci_sort_breadthfirst_klist();
+}
+