Merge branch 'intx' of master.kernel.org:/pub/scm/linux/kernel/git/jgarzik/misc-2.6
[pandora-kernel.git] / drivers / pci / probe.c
index f89dbc3..6a3c1e7 100644 (file)
@@ -339,6 +339,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..
@@ -356,8 +357,13 @@ pci_alloc_child_bus(struct pci_bus *parent, struct pci_dev *bridge, int busnr)
 
        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
@@ -375,6 +381,12 @@ 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)
@@ -667,6 +679,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 = IORESOURCE_IO;
+                               dev->resource[1].start = 0x3F6;
+                               dev->resource[1].end = 0x3F6;
+                               dev->resource[1].flags = IORESOURCE_IO;
+                       }
+                       if ((progif & 4) == 0) {
+                               dev->resource[2].start = 0x170;
+                               dev->resource[2].end = 0x177;
+                               dev->resource[2].flags = IORESOURCE_IO;
+                               dev->resource[3].start = 0x376;
+                               dev->resource[3].end = 0x376;
+                               dev->resource[3].flags = IORESOURCE_IO;
+                       }
+               }
                break;
 
        case PCI_HEADER_TYPE_BRIDGE:                /* bridge header */
@@ -815,6 +854,7 @@ 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;
 
        /* Assume 32-bit PCI; let 64-bit PCI cards (which are far rarer)
           set this higher, assuming the system even supports it.  */
@@ -833,6 +873,7 @@ void __devinit pci_device_add(struct pci_dev *dev, struct pci_bus *bus)
        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;
 
@@ -1054,3 +1095,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();
+}
+