aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/pci
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/pci')
-rw-r--r--drivers/pci/Kconfig13
-rw-r--r--drivers/pci/bus.c30
-rw-r--r--drivers/pci/hotplug/acpi_pcihp.c2
-rw-r--r--drivers/pci/hotplug/acpiphp_core.c2
-rw-r--r--drivers/pci/hotplug/acpiphp_glue.c33
-rw-r--r--drivers/pci/hotplug/acpiphp_ibm.c2
-rw-r--r--drivers/pci/hotplug/cpci_hotplug_pci.c2
-rw-r--r--drivers/pci/hotplug/cpcihp_generic.c2
-rw-r--r--drivers/pci/hotplug/cpcihp_zt5550.c4
-rw-r--r--drivers/pci/hotplug/cpqphp_core.c4
-rw-r--r--drivers/pci/hotplug/cpqphp_pci.c2
-rw-r--r--drivers/pci/hotplug/fakephp.c2
-rw-r--r--drivers/pci/hotplug/ibmphp_core.c4
-rw-r--r--drivers/pci/hotplug/ibmphp_ebda.c6
-rw-r--r--drivers/pci/hotplug/pci_hotplug_core.c2
-rw-r--r--drivers/pci/hotplug/pciehp.h6
-rw-r--r--drivers/pci/hotplug/pciehp_core.c6
-rw-r--r--drivers/pci/hotplug/pciehp_hpc.c133
-rw-r--r--drivers/pci/hotplug/pciehp_pci.c2
-rw-r--r--drivers/pci/hotplug/pcihp_skeleton.c2
-rw-r--r--drivers/pci/hotplug/rpadlpar_core.c2
-rw-r--r--drivers/pci/hotplug/rpaphp.h2
-rw-r--r--drivers/pci/hotplug/rpaphp_core.c2
-rw-r--r--drivers/pci/hotplug/sgi_hotplug.c2
-rw-r--r--drivers/pci/hotplug/shpchp.h4
-rw-r--r--drivers/pci/hotplug/shpchp_core.c4
-rw-r--r--drivers/pci/hotplug/shpchp_pci.c2
-rw-r--r--drivers/pci/iov.c15
-rw-r--r--drivers/pci/pci-acpi.c40
-rw-r--r--drivers/pci/pci-driver.c62
-rw-r--r--drivers/pci/pci-sysfs.c9
-rw-r--r--drivers/pci/pci.c114
-rw-r--r--drivers/pci/pci.h10
-rw-r--r--drivers/pci/pcie/Kconfig25
-rw-r--r--drivers/pci/pcie/aer/aer_inject.c2
-rw-r--r--drivers/pci/pcie/aer/aerdrv_core.c4
-rw-r--r--drivers/pci/pcie/aspm.c18
-rw-r--r--drivers/pci/pcie/portdrv.h12
-rw-r--r--drivers/pci/pcie/portdrv_core.c16
-rw-r--r--drivers/pci/probe.c303
-rw-r--r--drivers/pci/quirks.c200
-rw-r--r--drivers/pci/remove.c55
-rw-r--r--drivers/pci/setup-bus.c660
-rw-r--r--drivers/pci/setup-res.c94
-rw-r--r--drivers/pci/xen-pcifront.c17
45 files changed, 1292 insertions, 641 deletions
diff --git a/drivers/pci/Kconfig b/drivers/pci/Kconfig
index 37856f7c7781..848bfb84c04c 100644
--- a/drivers/pci/Kconfig
+++ b/drivers/pci/Kconfig
@@ -31,6 +31,19 @@ config PCI_DEBUG
31 31
32 When in doubt, say N. 32 When in doubt, say N.
33 33
34config PCI_REALLOC_ENABLE_AUTO
35 bool "Enable PCI resource re-allocation detection"
36 depends on PCI
37 help
38 Say Y here if you want the PCI core to detect if PCI resource
39 re-allocation needs to be enabled. You can always use pci=realloc=on
40 or pci=realloc=off to override it. Note this feature is a no-op
41 unless PCI_IOV support is also enabled; in that case it will
42 automatically re-allocate PCI resources if SR-IOV BARs have not
43 been allocated by the BIOS.
44
45 When in doubt, say N.
46
34config PCI_STUB 47config PCI_STUB
35 tristate "PCI Stub driver" 48 tristate "PCI Stub driver"
36 depends on PCI 49 depends on PCI
diff --git a/drivers/pci/bus.c b/drivers/pci/bus.c
index 398f5d859791..4ce5ef2f2826 100644
--- a/drivers/pci/bus.c
+++ b/drivers/pci/bus.c
@@ -18,28 +18,36 @@
18 18
19#include "pci.h" 19#include "pci.h"
20 20
21void pci_add_resource(struct list_head *resources, struct resource *res) 21void pci_add_resource_offset(struct list_head *resources, struct resource *res,
22 resource_size_t offset)
22{ 23{
23 struct pci_bus_resource *bus_res; 24 struct pci_host_bridge_window *window;
24 25
25 bus_res = kzalloc(sizeof(struct pci_bus_resource), GFP_KERNEL); 26 window = kzalloc(sizeof(struct pci_host_bridge_window), GFP_KERNEL);
26 if (!bus_res) { 27 if (!window) {
27 printk(KERN_ERR "PCI: can't add bus resource %pR\n", res); 28 printk(KERN_ERR "PCI: can't add host bridge window %pR\n", res);
28 return; 29 return;
29 } 30 }
30 31
31 bus_res->res = res; 32 window->res = res;
32 list_add_tail(&bus_res->list, resources); 33 window->offset = offset;
34 list_add_tail(&window->list, resources);
35}
36EXPORT_SYMBOL(pci_add_resource_offset);
37
38void pci_add_resource(struct list_head *resources, struct resource *res)
39{
40 pci_add_resource_offset(resources, res, 0);
33} 41}
34EXPORT_SYMBOL(pci_add_resource); 42EXPORT_SYMBOL(pci_add_resource);
35 43
36void pci_free_resource_list(struct list_head *resources) 44void pci_free_resource_list(struct list_head *resources)
37{ 45{
38 struct pci_bus_resource *bus_res, *tmp; 46 struct pci_host_bridge_window *window, *tmp;
39 47
40 list_for_each_entry_safe(bus_res, tmp, resources, list) { 48 list_for_each_entry_safe(window, tmp, resources, list) {
41 list_del(&bus_res->list); 49 list_del(&window->list);
42 kfree(bus_res); 50 kfree(window);
43 } 51 }
44} 52}
45EXPORT_SYMBOL(pci_free_resource_list); 53EXPORT_SYMBOL(pci_free_resource_list);
diff --git a/drivers/pci/hotplug/acpi_pcihp.c b/drivers/pci/hotplug/acpi_pcihp.c
index 095f29e13734..2a47e82821da 100644
--- a/drivers/pci/hotplug/acpi_pcihp.c
+++ b/drivers/pci/hotplug/acpi_pcihp.c
@@ -44,7 +44,7 @@
44#define METHOD_NAME__SUN "_SUN" 44#define METHOD_NAME__SUN "_SUN"
45#define METHOD_NAME_OSHP "OSHP" 45#define METHOD_NAME_OSHP "OSHP"
46 46
47static int debug_acpi; 47static bool debug_acpi;
48 48
49static acpi_status 49static acpi_status
50decode_type0_hpx_record(union acpi_object *record, struct hotplug_params *hpx) 50decode_type0_hpx_record(union acpi_object *record, struct hotplug_params *hpx)
diff --git a/drivers/pci/hotplug/acpiphp_core.c b/drivers/pci/hotplug/acpiphp_core.c
index efa9f2de51c1..aa41631e9e02 100644
--- a/drivers/pci/hotplug/acpiphp_core.c
+++ b/drivers/pci/hotplug/acpiphp_core.c
@@ -47,7 +47,7 @@
47/* name size which is used for entries in pcihpfs */ 47/* name size which is used for entries in pcihpfs */
48#define SLOT_NAME_SIZE 21 /* {_SUN} */ 48#define SLOT_NAME_SIZE 21 /* {_SUN} */
49 49
50static int debug; 50static bool debug;
51int acpiphp_debug; 51int acpiphp_debug;
52 52
53/* local variables */ 53/* local variables */
diff --git a/drivers/pci/hotplug/acpiphp_glue.c b/drivers/pci/hotplug/acpiphp_glue.c
index 9ddf69e3bbef..806c44fa645a 100644
--- a/drivers/pci/hotplug/acpiphp_glue.c
+++ b/drivers/pci/hotplug/acpiphp_glue.c
@@ -800,20 +800,10 @@ static int __ref enable_device(struct acpiphp_slot *slot)
800 if (slot->flags & SLOT_ENABLED) 800 if (slot->flags & SLOT_ENABLED)
801 goto err_exit; 801 goto err_exit;
802 802
803 /* sanity check: dev should be NULL when hot-plugged in */
804 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
805 if (dev) {
806 /* This case shouldn't happen */
807 err("pci_dev structure already exists.\n");
808 pci_dev_put(dev);
809 retval = -1;
810 goto err_exit;
811 }
812
813 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0)); 803 num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
814 if (num == 0) { 804 if (num == 0) {
815 err("No new device found\n"); 805 /* Maybe only part of funcs are added. */
816 retval = -1; 806 dbg("No new device found\n");
817 goto err_exit; 807 goto err_exit;
818 } 808 }
819 809
@@ -848,11 +838,16 @@ static int __ref enable_device(struct acpiphp_slot *slot)
848 838
849 pci_bus_add_devices(bus); 839 pci_bus_add_devices(bus);
850 840
841 slot->flags |= SLOT_ENABLED;
851 list_for_each_entry(func, &slot->funcs, sibling) { 842 list_for_each_entry(func, &slot->funcs, sibling) {
852 dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 843 dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
853 func->function)); 844 func->function));
854 if (!dev) 845 if (!dev) {
846 /* Do not set SLOT_ENABLED flag if some funcs
847 are not added. */
848 slot->flags &= (~SLOT_ENABLED);
855 continue; 849 continue;
850 }
856 851
857 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE && 852 if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
858 dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) { 853 dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) {
@@ -867,7 +862,6 @@ static int __ref enable_device(struct acpiphp_slot *slot)
867 pci_dev_put(dev); 862 pci_dev_put(dev);
868 } 863 }
869 864
870 slot->flags |= SLOT_ENABLED;
871 865
872 err_exit: 866 err_exit:
873 return retval; 867 return retval;
@@ -892,9 +886,12 @@ static int disable_device(struct acpiphp_slot *slot)
892{ 886{
893 struct acpiphp_func *func; 887 struct acpiphp_func *func;
894 struct pci_dev *pdev; 888 struct pci_dev *pdev;
889 struct pci_bus *bus = slot->bridge->pci_bus;
895 890
896 /* is this slot already disabled? */ 891 /* The slot will be enabled when func 0 is added, so check
897 if (!(slot->flags & SLOT_ENABLED)) 892 func 0 before disable the slot. */
893 pdev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
894 if (!pdev)
898 goto err_exit; 895 goto err_exit;
899 896
900 list_for_each_entry(func, &slot->funcs, sibling) { 897 list_for_each_entry(func, &slot->funcs, sibling) {
@@ -913,7 +910,7 @@ static int disable_device(struct acpiphp_slot *slot)
913 disable_bridges(pdev->subordinate); 910 disable_bridges(pdev->subordinate);
914 pci_disable_device(pdev); 911 pci_disable_device(pdev);
915 } 912 }
916 pci_remove_bus_device(pdev); 913 __pci_remove_bus_device(pdev);
917 pci_dev_put(pdev); 914 pci_dev_put(pdev);
918 } 915 }
919 } 916 }
@@ -1070,7 +1067,7 @@ static void acpiphp_sanitize_bus(struct pci_bus *bus)
1070 res->end) { 1067 res->end) {
1071 /* Could not assign a required resources 1068 /* Could not assign a required resources
1072 * for this device, remove it */ 1069 * for this device, remove it */
1073 pci_remove_bus_device(dev); 1070 pci_stop_and_remove_bus_device(dev);
1074 break; 1071 break;
1075 } 1072 }
1076 } 1073 }
diff --git a/drivers/pci/hotplug/acpiphp_ibm.c b/drivers/pci/hotplug/acpiphp_ibm.c
index e525263210ee..c35e8ad6db01 100644
--- a/drivers/pci/hotplug/acpiphp_ibm.c
+++ b/drivers/pci/hotplug/acpiphp_ibm.c
@@ -43,7 +43,7 @@
43#define DRIVER_AUTHOR "Irene Zubarev <zubarev@us.ibm.com>, Vernon Mauery <vernux@us.ibm.com>" 43#define DRIVER_AUTHOR "Irene Zubarev <zubarev@us.ibm.com>, Vernon Mauery <vernux@us.ibm.com>"
44#define DRIVER_DESC "ACPI Hot Plug PCI Controller Driver IBM extension" 44#define DRIVER_DESC "ACPI Hot Plug PCI Controller Driver IBM extension"
45 45
46static int debug; 46static bool debug;
47 47
48MODULE_AUTHOR(DRIVER_AUTHOR); 48MODULE_AUTHOR(DRIVER_AUTHOR);
49MODULE_DESCRIPTION(DRIVER_DESC); 49MODULE_DESCRIPTION(DRIVER_DESC);
diff --git a/drivers/pci/hotplug/cpci_hotplug_pci.c b/drivers/pci/hotplug/cpci_hotplug_pci.c
index 829c327cfb5e..ae853ccd0cd5 100644
--- a/drivers/pci/hotplug/cpci_hotplug_pci.c
+++ b/drivers/pci/hotplug/cpci_hotplug_pci.c
@@ -341,7 +341,7 @@ int cpci_unconfigure_slot(struct slot* slot)
341 dev = pci_get_slot(slot->bus, 341 dev = pci_get_slot(slot->bus,
342 PCI_DEVFN(PCI_SLOT(slot->devfn), i)); 342 PCI_DEVFN(PCI_SLOT(slot->devfn), i));
343 if (dev) { 343 if (dev) {
344 pci_remove_bus_device(dev); 344 pci_stop_and_remove_bus_device(dev);
345 pci_dev_put(dev); 345 pci_dev_put(dev);
346 } 346 }
347 } 347 }
diff --git a/drivers/pci/hotplug/cpcihp_generic.c b/drivers/pci/hotplug/cpcihp_generic.c
index fb3f84661bdc..81af764c629b 100644
--- a/drivers/pci/hotplug/cpcihp_generic.c
+++ b/drivers/pci/hotplug/cpcihp_generic.c
@@ -62,7 +62,7 @@
62#define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME , ## arg) 62#define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME , ## arg)
63 63
64/* local variables */ 64/* local variables */
65static int debug; 65static bool debug;
66static char *bridge; 66static char *bridge;
67static u8 bridge_busnr; 67static u8 bridge_busnr;
68static u8 bridge_slot; 68static u8 bridge_slot;
diff --git a/drivers/pci/hotplug/cpcihp_zt5550.c b/drivers/pci/hotplug/cpcihp_zt5550.c
index 41f6a8d79c81..6bf8d2ab164f 100644
--- a/drivers/pci/hotplug/cpcihp_zt5550.c
+++ b/drivers/pci/hotplug/cpcihp_zt5550.c
@@ -57,8 +57,8 @@
57#define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME , ## arg) 57#define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME , ## arg)
58 58
59/* local variables */ 59/* local variables */
60static int debug; 60static bool debug;
61static int poll; 61static bool poll;
62static struct cpci_hp_controller_ops zt5550_hpc_ops; 62static struct cpci_hp_controller_ops zt5550_hpc_ops;
63static struct cpci_hp_controller zt5550_hpc; 63static struct cpci_hp_controller zt5550_hpc;
64 64
diff --git a/drivers/pci/hotplug/cpqphp_core.c b/drivers/pci/hotplug/cpqphp_core.c
index f1ce99cceac6..187a199da93c 100644
--- a/drivers/pci/hotplug/cpqphp_core.c
+++ b/drivers/pci/hotplug/cpqphp_core.c
@@ -57,8 +57,8 @@ struct irq_routing_table *cpqhp_routing_table;
57static void __iomem *smbios_table; 57static void __iomem *smbios_table;
58static void __iomem *smbios_start; 58static void __iomem *smbios_start;
59static void __iomem *cpqhp_rom_start; 59static void __iomem *cpqhp_rom_start;
60static int power_mode; 60static bool power_mode;
61static int debug; 61static bool debug;
62static int initialized; 62static int initialized;
63 63
64#define DRIVER_VERSION "0.9.8" 64#define DRIVER_VERSION "0.9.8"
diff --git a/drivers/pci/hotplug/cpqphp_pci.c b/drivers/pci/hotplug/cpqphp_pci.c
index 6173b9a4544e..1c8494021a42 100644
--- a/drivers/pci/hotplug/cpqphp_pci.c
+++ b/drivers/pci/hotplug/cpqphp_pci.c
@@ -127,7 +127,7 @@ int cpqhp_unconfigure_device(struct pci_func* func)
127 struct pci_dev* temp = pci_get_bus_and_slot(func->bus, PCI_DEVFN(func->device, j)); 127 struct pci_dev* temp = pci_get_bus_and_slot(func->bus, PCI_DEVFN(func->device, j));
128 if (temp) { 128 if (temp) {
129 pci_dev_put(temp); 129 pci_dev_put(temp);
130 pci_remove_bus_device(temp); 130 pci_stop_and_remove_bus_device(temp);
131 } 131 }
132 } 132 }
133 return 0; 133 return 0;
diff --git a/drivers/pci/hotplug/fakephp.c b/drivers/pci/hotplug/fakephp.c
index 17d10e2e8fb6..a019c9a712be 100644
--- a/drivers/pci/hotplug/fakephp.c
+++ b/drivers/pci/hotplug/fakephp.c
@@ -40,7 +40,7 @@ static ssize_t legacy_show(struct kobject *kobj, struct attribute *attr,
40 40
41static void remove_callback(void *data) 41static void remove_callback(void *data)
42{ 42{
43 pci_remove_bus_device((struct pci_dev *)data); 43 pci_stop_and_remove_bus_device((struct pci_dev *)data);
44} 44}
45 45
46static ssize_t legacy_store(struct kobject *kobj, struct attribute *attr, 46static ssize_t legacy_store(struct kobject *kobj, struct attribute *attr,
diff --git a/drivers/pci/hotplug/ibmphp_core.c b/drivers/pci/hotplug/ibmphp_core.c
index d934dd4fa873..4fda7e6a86a7 100644
--- a/drivers/pci/hotplug/ibmphp_core.c
+++ b/drivers/pci/hotplug/ibmphp_core.c
@@ -49,7 +49,7 @@
49 49
50int ibmphp_debug; 50int ibmphp_debug;
51 51
52static int debug; 52static bool debug;
53module_param(debug, bool, S_IRUGO | S_IWUSR); 53module_param(debug, bool, S_IRUGO | S_IWUSR);
54MODULE_PARM_DESC (debug, "Debugging mode enabled or not"); 54MODULE_PARM_DESC (debug, "Debugging mode enabled or not");
55MODULE_LICENSE ("GPL"); 55MODULE_LICENSE ("GPL");
@@ -721,7 +721,7 @@ static void ibm_unconfigure_device(struct pci_func *func)
721 for (j = 0; j < 0x08; j++) { 721 for (j = 0; j < 0x08; j++) {
722 temp = pci_get_bus_and_slot(func->busno, (func->device << 3) | j); 722 temp = pci_get_bus_and_slot(func->busno, (func->device << 3) | j);
723 if (temp) { 723 if (temp) {
724 pci_remove_bus_device(temp); 724 pci_stop_and_remove_bus_device(temp);
725 pci_dev_put(temp); 725 pci_dev_put(temp);
726 } 726 }
727 } 727 }
diff --git a/drivers/pci/hotplug/ibmphp_ebda.c b/drivers/pci/hotplug/ibmphp_ebda.c
index 2850e64dedae..714ca5c4ed50 100644
--- a/drivers/pci/hotplug/ibmphp_ebda.c
+++ b/drivers/pci/hotplug/ibmphp_ebda.c
@@ -368,8 +368,10 @@ int __init ibmphp_access_ebda (void)
368 debug ("rio blk id: %x\n", blk_id); 368 debug ("rio blk id: %x\n", blk_id);
369 369
370 rio_table_ptr = kzalloc(sizeof(struct rio_table_hdr), GFP_KERNEL); 370 rio_table_ptr = kzalloc(sizeof(struct rio_table_hdr), GFP_KERNEL);
371 if (!rio_table_ptr) 371 if (!rio_table_ptr) {
372 return -ENOMEM; 372 rc = -ENOMEM;
373 goto out;
374 }
373 rio_table_ptr->ver_num = readb (io_mem + offset); 375 rio_table_ptr->ver_num = readb (io_mem + offset);
374 rio_table_ptr->scal_count = readb (io_mem + offset + 1); 376 rio_table_ptr->scal_count = readb (io_mem + offset + 1);
375 rio_table_ptr->riodev_count = readb (io_mem + offset + 2); 377 rio_table_ptr->riodev_count = readb (io_mem + offset + 2);
diff --git a/drivers/pci/hotplug/pci_hotplug_core.c b/drivers/pci/hotplug/pci_hotplug_core.c
index 6d2eea93298f..202f4a969eb5 100644
--- a/drivers/pci/hotplug/pci_hotplug_core.c
+++ b/drivers/pci/hotplug/pci_hotplug_core.c
@@ -51,7 +51,7 @@
51 51
52 52
53/* local variables */ 53/* local variables */
54static int debug; 54static bool debug;
55 55
56#define DRIVER_VERSION "0.5" 56#define DRIVER_VERSION "0.5"
57#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Scott Murray <scottm@somanetworks.com>" 57#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Scott Murray <scottm@somanetworks.com>"
diff --git a/drivers/pci/hotplug/pciehp.h b/drivers/pci/hotplug/pciehp.h
index 9a33fdde2d16..4b7cce1de6ec 100644
--- a/drivers/pci/hotplug/pciehp.h
+++ b/drivers/pci/hotplug/pciehp.h
@@ -40,10 +40,10 @@
40 40
41#define MY_NAME "pciehp" 41#define MY_NAME "pciehp"
42 42
43extern int pciehp_poll_mode; 43extern bool pciehp_poll_mode;
44extern int pciehp_poll_time; 44extern int pciehp_poll_time;
45extern int pciehp_debug; 45extern bool pciehp_debug;
46extern int pciehp_force; 46extern bool pciehp_force;
47extern struct workqueue_struct *pciehp_wq; 47extern struct workqueue_struct *pciehp_wq;
48 48
49#define dbg(format, arg...) \ 49#define dbg(format, arg...) \
diff --git a/drivers/pci/hotplug/pciehp_core.c b/drivers/pci/hotplug/pciehp_core.c
index b8c99d35ac97..365c6b96c642 100644
--- a/drivers/pci/hotplug/pciehp_core.c
+++ b/drivers/pci/hotplug/pciehp_core.c
@@ -38,10 +38,10 @@
38#include <linux/time.h> 38#include <linux/time.h>
39 39
40/* Global variables */ 40/* Global variables */
41int pciehp_debug; 41bool pciehp_debug;
42int pciehp_poll_mode; 42bool pciehp_poll_mode;
43int pciehp_poll_time; 43int pciehp_poll_time;
44int pciehp_force; 44bool pciehp_force;
45struct workqueue_struct *pciehp_wq; 45struct workqueue_struct *pciehp_wq;
46 46
47#define DRIVER_VERSION "0.4" 47#define DRIVER_VERSION "0.4"
diff --git a/drivers/pci/hotplug/pciehp_hpc.c b/drivers/pci/hotplug/pciehp_hpc.c
index bcdbb1643621..a960faec1021 100644
--- a/drivers/pci/hotplug/pciehp_hpc.c
+++ b/drivers/pci/hotplug/pciehp_hpc.c
@@ -241,34 +241,79 @@ static int pcie_write_cmd(struct controller *ctrl, u16 cmd, u16 mask)
241 return retval; 241 return retval;
242} 242}
243 243
244static inline int check_link_active(struct controller *ctrl) 244static bool check_link_active(struct controller *ctrl)
245{ 245{
246 u16 link_status; 246 bool ret = false;
247 u16 lnk_status;
247 248
248 if (pciehp_readw(ctrl, PCI_EXP_LNKSTA, &link_status)) 249 if (pciehp_readw(ctrl, PCI_EXP_LNKSTA, &lnk_status))
249 return 0; 250 return ret;
250 return !!(link_status & PCI_EXP_LNKSTA_DLLLA); 251
252 ret = !!(lnk_status & PCI_EXP_LNKSTA_DLLLA);
253
254 if (ret)
255 ctrl_dbg(ctrl, "%s: lnk_status = %x\n", __func__, lnk_status);
256
257 return ret;
251} 258}
252 259
253static void pcie_wait_link_active(struct controller *ctrl) 260static void __pcie_wait_link_active(struct controller *ctrl, bool active)
254{ 261{
255 int timeout = 1000; 262 int timeout = 1000;
256 263
257 if (check_link_active(ctrl)) 264 if (check_link_active(ctrl) == active)
258 return; 265 return;
259 while (timeout > 0) { 266 while (timeout > 0) {
260 msleep(10); 267 msleep(10);
261 timeout -= 10; 268 timeout -= 10;
262 if (check_link_active(ctrl)) 269 if (check_link_active(ctrl) == active)
263 return; 270 return;
264 } 271 }
265 ctrl_dbg(ctrl, "Data Link Layer Link Active not set in 1000 msec\n"); 272 ctrl_dbg(ctrl, "Data Link Layer Link Active not %s in 1000 msec\n",
273 active ? "set" : "cleared");
274}
275
276static void pcie_wait_link_active(struct controller *ctrl)
277{
278 __pcie_wait_link_active(ctrl, true);
279}
280
281static void pcie_wait_link_not_active(struct controller *ctrl)
282{
283 __pcie_wait_link_active(ctrl, false);
284}
285
286static bool pci_bus_check_dev(struct pci_bus *bus, int devfn)
287{
288 u32 l;
289 int count = 0;
290 int delay = 1000, step = 20;
291 bool found = false;
292
293 do {
294 found = pci_bus_read_dev_vendor_id(bus, devfn, &l, 0);
295 count++;
296
297 if (found)
298 break;
299
300 msleep(step);
301 delay -= step;
302 } while (delay > 0);
303
304 if (count > 1 && pciehp_debug)
305 printk(KERN_DEBUG "pci %04x:%02x:%02x.%d id reading try %d times with interval %d ms to get %08x\n",
306 pci_domain_nr(bus), bus->number, PCI_SLOT(devfn),
307 PCI_FUNC(devfn), count, step, l);
308
309 return found;
266} 310}
267 311
268int pciehp_check_link_status(struct controller *ctrl) 312int pciehp_check_link_status(struct controller *ctrl)
269{ 313{
270 u16 lnk_status; 314 u16 lnk_status;
271 int retval = 0; 315 int retval = 0;
316 bool found = false;
272 317
273 /* 318 /*
274 * Data Link Layer Link Active Reporting must be capable for 319 * Data Link Layer Link Active Reporting must be capable for
@@ -280,13 +325,10 @@ int pciehp_check_link_status(struct controller *ctrl)
280 else 325 else
281 msleep(1000); 326 msleep(1000);
282 327
283 /* 328 /* wait 100ms before read pci conf, and try in 1s */
284 * Need to wait for 1000 ms after Data Link Layer Link Active 329 msleep(100);
285 * (DLLLA) bit reads 1b before sending configuration request. 330 found = pci_bus_check_dev(ctrl->pcie->port->subordinate,
286 * We need it before checking Link Training (LT) bit becuase 331 PCI_DEVFN(0, 0));
287 * LT is still set even after DLLLA bit is set on some platform.
288 */
289 msleep(1000);
290 332
291 retval = pciehp_readw(ctrl, PCI_EXP_LNKSTA, &lnk_status); 333 retval = pciehp_readw(ctrl, PCI_EXP_LNKSTA, &lnk_status);
292 if (retval) { 334 if (retval) {
@@ -302,19 +344,50 @@ int pciehp_check_link_status(struct controller *ctrl)
302 return retval; 344 return retval;
303 } 345 }
304 346
305 /*
306 * If the port supports Link speeds greater than 5.0 GT/s, we
307 * must wait for 100 ms after Link training completes before
308 * sending configuration request.
309 */
310 if (ctrl->pcie->port->subordinate->max_bus_speed > PCIE_SPEED_5_0GT)
311 msleep(100);
312
313 pcie_update_link_speed(ctrl->pcie->port->subordinate, lnk_status); 347 pcie_update_link_speed(ctrl->pcie->port->subordinate, lnk_status);
314 348
349 if (!found && !retval)
350 retval = -1;
351
315 return retval; 352 return retval;
316} 353}
317 354
355static int __pciehp_link_set(struct controller *ctrl, bool enable)
356{
357 u16 lnk_ctrl;
358 int retval = 0;
359
360 retval = pciehp_readw(ctrl, PCI_EXP_LNKCTL, &lnk_ctrl);
361 if (retval) {
362 ctrl_err(ctrl, "Cannot read LNKCTRL register\n");
363 return retval;
364 }
365
366 if (enable)
367 lnk_ctrl &= ~PCI_EXP_LNKCTL_LD;
368 else
369 lnk_ctrl |= PCI_EXP_LNKCTL_LD;
370
371 retval = pciehp_writew(ctrl, PCI_EXP_LNKCTL, lnk_ctrl);
372 if (retval) {
373 ctrl_err(ctrl, "Cannot write LNKCTRL register\n");
374 return retval;
375 }
376 ctrl_dbg(ctrl, "%s: lnk_ctrl = %x\n", __func__, lnk_ctrl);
377
378 return retval;
379}
380
381static int pciehp_link_enable(struct controller *ctrl)
382{
383 return __pciehp_link_set(ctrl, true);
384}
385
386static int pciehp_link_disable(struct controller *ctrl)
387{
388 return __pciehp_link_set(ctrl, false);
389}
390
318int pciehp_get_attention_status(struct slot *slot, u8 *status) 391int pciehp_get_attention_status(struct slot *slot, u8 *status)
319{ 392{
320 struct controller *ctrl = slot->ctrl; 393 struct controller *ctrl = slot->ctrl;
@@ -533,6 +606,10 @@ int pciehp_power_on_slot(struct slot * slot)
533 ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__, 606 ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
534 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, slot_cmd); 607 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, slot_cmd);
535 608
609 retval = pciehp_link_enable(ctrl);
610 if (retval)
611 ctrl_err(ctrl, "%s: Can not enable the link!\n", __func__);
612
536 return retval; 613 return retval;
537} 614}
538 615
@@ -543,6 +620,14 @@ int pciehp_power_off_slot(struct slot * slot)
543 u16 cmd_mask; 620 u16 cmd_mask;
544 int retval; 621 int retval;
545 622
623 /* Disable the link at first */
624 pciehp_link_disable(ctrl);
625 /* wait the link is down */
626 if (ctrl->link_active_reporting)
627 pcie_wait_link_not_active(ctrl);
628 else
629 msleep(1000);
630
546 slot_cmd = POWER_OFF; 631 slot_cmd = POWER_OFF;
547 cmd_mask = PCI_EXP_SLTCTL_PCC; 632 cmd_mask = PCI_EXP_SLTCTL_PCC;
548 retval = pcie_write_cmd(ctrl, slot_cmd, cmd_mask); 633 retval = pcie_write_cmd(ctrl, slot_cmd, cmd_mask);
diff --git a/drivers/pci/hotplug/pciehp_pci.c b/drivers/pci/hotplug/pciehp_pci.c
index a4031dfe938e..47d9dc06b109 100644
--- a/drivers/pci/hotplug/pciehp_pci.c
+++ b/drivers/pci/hotplug/pciehp_pci.c
@@ -141,7 +141,7 @@ int pciehp_unconfigure_device(struct slot *p_slot)
141 break; 141 break;
142 } 142 }
143 } 143 }
144 pci_remove_bus_device(temp); 144 pci_stop_and_remove_bus_device(temp);
145 /* 145 /*
146 * Ensure that no new Requests will be generated from 146 * Ensure that no new Requests will be generated from
147 * the device. 147 * the device.
diff --git a/drivers/pci/hotplug/pcihp_skeleton.c b/drivers/pci/hotplug/pcihp_skeleton.c
index 5175d9b26f0b..b20ceaaa31f4 100644
--- a/drivers/pci/hotplug/pcihp_skeleton.c
+++ b/drivers/pci/hotplug/pcihp_skeleton.c
@@ -59,7 +59,7 @@ static LIST_HEAD(slot_list);
59#define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME , ## arg) 59#define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME , ## arg)
60 60
61/* local variables */ 61/* local variables */
62static int debug; 62static bool debug;
63static int num_slots; 63static int num_slots;
64 64
65#define DRIVER_VERSION "0.3" 65#define DRIVER_VERSION "0.3"
diff --git a/drivers/pci/hotplug/rpadlpar_core.c b/drivers/pci/hotplug/rpadlpar_core.c
index c56a9413e1af..1e117c2a3cad 100644
--- a/drivers/pci/hotplug/rpadlpar_core.c
+++ b/drivers/pci/hotplug/rpadlpar_core.c
@@ -389,7 +389,7 @@ int dlpar_remove_pci_slot(char *drc_name, struct device_node *dn)
389 BUG_ON(!bus->self); 389 BUG_ON(!bus->self);
390 pr_debug("PCI: Now removing bridge device %s\n", pci_name(bus->self)); 390 pr_debug("PCI: Now removing bridge device %s\n", pci_name(bus->self));
391 eeh_remove_bus_device(bus->self); 391 eeh_remove_bus_device(bus->self);
392 pci_remove_bus_device(bus->self); 392 pci_stop_and_remove_bus_device(bus->self);
393 393
394 return 0; 394 return 0;
395} 395}
diff --git a/drivers/pci/hotplug/rpaphp.h b/drivers/pci/hotplug/rpaphp.h
index 419919a87b0f..df5677440a08 100644
--- a/drivers/pci/hotplug/rpaphp.h
+++ b/drivers/pci/hotplug/rpaphp.h
@@ -46,7 +46,7 @@
46#define PRESENT 1 /* Card in slot */ 46#define PRESENT 1 /* Card in slot */
47 47
48#define MY_NAME "rpaphp" 48#define MY_NAME "rpaphp"
49extern int rpaphp_debug; 49extern bool rpaphp_debug;
50#define dbg(format, arg...) \ 50#define dbg(format, arg...) \
51 do { \ 51 do { \
52 if (rpaphp_debug) \ 52 if (rpaphp_debug) \
diff --git a/drivers/pci/hotplug/rpaphp_core.c b/drivers/pci/hotplug/rpaphp_core.c
index 758adb5f47fd..127d6e600185 100644
--- a/drivers/pci/hotplug/rpaphp_core.c
+++ b/drivers/pci/hotplug/rpaphp_core.c
@@ -37,7 +37,7 @@
37 /* and pci_do_scan_bus */ 37 /* and pci_do_scan_bus */
38#include "rpaphp.h" 38#include "rpaphp.h"
39 39
40int rpaphp_debug; 40bool rpaphp_debug;
41LIST_HEAD(rpaphp_slot_head); 41LIST_HEAD(rpaphp_slot_head);
42 42
43#define DRIVER_VERSION "0.1" 43#define DRIVER_VERSION "0.1"
diff --git a/drivers/pci/hotplug/sgi_hotplug.c b/drivers/pci/hotplug/sgi_hotplug.c
index 72d507b6a2aa..de573113c102 100644
--- a/drivers/pci/hotplug/sgi_hotplug.c
+++ b/drivers/pci/hotplug/sgi_hotplug.c
@@ -554,7 +554,7 @@ static int disable_slot(struct hotplug_slot *bss_hotplug_slot)
554 PCI_FUNC(func))); 554 PCI_FUNC(func)));
555 if (dev) { 555 if (dev) {
556 sn_bus_free_data(dev); 556 sn_bus_free_data(dev);
557 pci_remove_bus_device(dev); 557 pci_stop_and_remove_bus_device(dev);
558 pci_dev_put(dev); 558 pci_dev_put(dev);
559 } 559 }
560 } 560 }
diff --git a/drivers/pci/hotplug/shpchp.h b/drivers/pci/hotplug/shpchp.h
index e0c90e643b5f..ca64932e658b 100644
--- a/drivers/pci/hotplug/shpchp.h
+++ b/drivers/pci/hotplug/shpchp.h
@@ -43,9 +43,9 @@
43 #define MY_NAME THIS_MODULE->name 43 #define MY_NAME THIS_MODULE->name
44#endif 44#endif
45 45
46extern int shpchp_poll_mode; 46extern bool shpchp_poll_mode;
47extern int shpchp_poll_time; 47extern int shpchp_poll_time;
48extern int shpchp_debug; 48extern bool shpchp_debug;
49extern struct workqueue_struct *shpchp_wq; 49extern struct workqueue_struct *shpchp_wq;
50extern struct workqueue_struct *shpchp_ordered_wq; 50extern struct workqueue_struct *shpchp_ordered_wq;
51 51
diff --git a/drivers/pci/hotplug/shpchp_core.c b/drivers/pci/hotplug/shpchp_core.c
index dd7e0c51a33e..7414fd9ad1d2 100644
--- a/drivers/pci/hotplug/shpchp_core.c
+++ b/drivers/pci/hotplug/shpchp_core.c
@@ -36,8 +36,8 @@
36#include "shpchp.h" 36#include "shpchp.h"
37 37
38/* Global variables */ 38/* Global variables */
39int shpchp_debug; 39bool shpchp_debug;
40int shpchp_poll_mode; 40bool shpchp_poll_mode;
41int shpchp_poll_time; 41int shpchp_poll_time;
42struct workqueue_struct *shpchp_wq; 42struct workqueue_struct *shpchp_wq;
43struct workqueue_struct *shpchp_ordered_wq; 43struct workqueue_struct *shpchp_ordered_wq;
diff --git a/drivers/pci/hotplug/shpchp_pci.c b/drivers/pci/hotplug/shpchp_pci.c
index a2ccfcd3c298..df7e4bfadae3 100644
--- a/drivers/pci/hotplug/shpchp_pci.c
+++ b/drivers/pci/hotplug/shpchp_pci.c
@@ -124,7 +124,7 @@ int shpchp_unconfigure_device(struct slot *p_slot)
124 break; 124 break;
125 } 125 }
126 } 126 }
127 pci_remove_bus_device(temp); 127 pci_stop_and_remove_bus_device(temp);
128 pci_dev_put(temp); 128 pci_dev_put(temp);
129 } 129 }
130 return rc; 130 return rc;
diff --git a/drivers/pci/iov.c b/drivers/pci/iov.c
index 0321fa3b4226..6554e1a0f634 100644
--- a/drivers/pci/iov.c
+++ b/drivers/pci/iov.c
@@ -142,7 +142,7 @@ failed2:
142failed1: 142failed1:
143 pci_dev_put(dev); 143 pci_dev_put(dev);
144 mutex_lock(&iov->dev->sriov->lock); 144 mutex_lock(&iov->dev->sriov->lock);
145 pci_remove_bus_device(virtfn); 145 pci_stop_and_remove_bus_device(virtfn);
146 virtfn_remove_bus(dev->bus, virtfn_bus(dev, id)); 146 virtfn_remove_bus(dev->bus, virtfn_bus(dev, id));
147 mutex_unlock(&iov->dev->sriov->lock); 147 mutex_unlock(&iov->dev->sriov->lock);
148 148
@@ -173,10 +173,16 @@ static void virtfn_remove(struct pci_dev *dev, int id, int reset)
173 173
174 sprintf(buf, "virtfn%u", id); 174 sprintf(buf, "virtfn%u", id);
175 sysfs_remove_link(&dev->dev.kobj, buf); 175 sysfs_remove_link(&dev->dev.kobj, buf);
176 sysfs_remove_link(&virtfn->dev.kobj, "physfn"); 176 /*
177 * pci_stop_dev() could have been called for this virtfn already,
178 * so the directory for the virtfn may have been removed before.
179 * Double check to avoid spurious sysfs warnings.
180 */
181 if (virtfn->dev.kobj.sd)
182 sysfs_remove_link(&virtfn->dev.kobj, "physfn");
177 183
178 mutex_lock(&iov->dev->sriov->lock); 184 mutex_lock(&iov->dev->sriov->lock);
179 pci_remove_bus_device(virtfn); 185 pci_stop_and_remove_bus_device(virtfn);
180 virtfn_remove_bus(dev->bus, virtfn_bus(dev, id)); 186 virtfn_remove_bus(dev->bus, virtfn_bus(dev, id));
181 mutex_unlock(&iov->dev->sriov->lock); 187 mutex_unlock(&iov->dev->sriov->lock);
182 188
@@ -347,8 +353,6 @@ static int sriov_enable(struct pci_dev *dev, int nr_virtfn)
347 return rc; 353 return rc;
348 } 354 }
349 355
350 pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz);
351
352 iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE; 356 iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE;
353 pci_cfg_access_lock(dev); 357 pci_cfg_access_lock(dev);
354 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl); 358 pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl);
@@ -466,6 +470,7 @@ found:
466 return -EIO; 470 return -EIO;
467 471
468 pgsz &= ~(pgsz - 1); 472 pgsz &= ~(pgsz - 1);
473 pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz);
469 474
470 nres = 0; 475 nres = 0;
471 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { 476 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
diff --git a/drivers/pci/pci-acpi.c b/drivers/pci/pci-acpi.c
index 060fd22a1103..0f150f271c2a 100644
--- a/drivers/pci/pci-acpi.c
+++ b/drivers/pci/pci-acpi.c
@@ -277,40 +277,6 @@ static int acpi_pci_sleep_wake(struct pci_dev *dev, bool enable)
277 return 0; 277 return 0;
278} 278}
279 279
280/**
281 * acpi_dev_run_wake - Enable/disable wake-up for given device.
282 * @phys_dev: Device to enable/disable the platform to wake-up the system for.
283 * @enable: Whether enable or disable the wake-up functionality.
284 *
285 * Find the ACPI device object corresponding to @pci_dev and try to
286 * enable/disable the GPE associated with it.
287 */
288static int acpi_dev_run_wake(struct device *phys_dev, bool enable)
289{
290 struct acpi_device *dev;
291 acpi_handle handle;
292
293 if (!device_run_wake(phys_dev))
294 return -EINVAL;
295
296 handle = DEVICE_ACPI_HANDLE(phys_dev);
297 if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &dev))) {
298 dev_dbg(phys_dev, "ACPI handle has no context in %s!\n",
299 __func__);
300 return -ENODEV;
301 }
302
303 if (enable) {
304 acpi_enable_wakeup_device_power(dev, ACPI_STATE_S0);
305 acpi_enable_gpe(dev->wakeup.gpe_device, dev->wakeup.gpe_number);
306 } else {
307 acpi_disable_gpe(dev->wakeup.gpe_device, dev->wakeup.gpe_number);
308 acpi_disable_wakeup_device_power(dev);
309 }
310
311 return 0;
312}
313
314static void acpi_pci_propagate_run_wake(struct pci_bus *bus, bool enable) 280static void acpi_pci_propagate_run_wake(struct pci_bus *bus, bool enable)
315{ 281{
316 while (bus->parent) { 282 while (bus->parent) {
@@ -318,14 +284,14 @@ static void acpi_pci_propagate_run_wake(struct pci_bus *bus, bool enable)
318 284
319 if (bridge->pme_interrupt) 285 if (bridge->pme_interrupt)
320 return; 286 return;
321 if (!acpi_dev_run_wake(&bridge->dev, enable)) 287 if (!acpi_pm_device_run_wake(&bridge->dev, enable))
322 return; 288 return;
323 bus = bus->parent; 289 bus = bus->parent;
324 } 290 }
325 291
326 /* We have reached the root bus. */ 292 /* We have reached the root bus. */
327 if (bus->bridge) 293 if (bus->bridge)
328 acpi_dev_run_wake(bus->bridge, enable); 294 acpi_pm_device_run_wake(bus->bridge, enable);
329} 295}
330 296
331static int acpi_pci_run_wake(struct pci_dev *dev, bool enable) 297static int acpi_pci_run_wake(struct pci_dev *dev, bool enable)
@@ -333,7 +299,7 @@ static int acpi_pci_run_wake(struct pci_dev *dev, bool enable)
333 if (dev->pme_interrupt) 299 if (dev->pme_interrupt)
334 return 0; 300 return 0;
335 301
336 if (!acpi_dev_run_wake(&dev->dev, enable)) 302 if (!acpi_pm_device_run_wake(&dev->dev, enable))
337 return 0; 303 return 0;
338 304
339 acpi_pci_propagate_run_wake(dev->bus, enable); 305 acpi_pci_propagate_run_wake(dev->bus, enable);
diff --git a/drivers/pci/pci-driver.c b/drivers/pci/pci-driver.c
index 3623d65f8b86..6b54b23b990b 100644
--- a/drivers/pci/pci-driver.c
+++ b/drivers/pci/pci-driver.c
@@ -72,9 +72,7 @@ int pci_add_dynid(struct pci_driver *drv,
72 list_add_tail(&dynid->node, &drv->dynids.list); 72 list_add_tail(&dynid->node, &drv->dynids.list);
73 spin_unlock(&drv->dynids.lock); 73 spin_unlock(&drv->dynids.lock);
74 74
75 get_driver(&drv->driver);
76 retval = driver_attach(&drv->driver); 75 retval = driver_attach(&drv->driver);
77 put_driver(&drv->driver);
78 76
79 return retval; 77 return retval;
80} 78}
@@ -190,43 +188,34 @@ store_remove_id(struct device_driver *driver, const char *buf, size_t count)
190static DRIVER_ATTR(remove_id, S_IWUSR, NULL, store_remove_id); 188static DRIVER_ATTR(remove_id, S_IWUSR, NULL, store_remove_id);
191 189
192static int 190static int
193pci_create_newid_file(struct pci_driver *drv) 191pci_create_newid_files(struct pci_driver *drv)
194{ 192{
195 int error = 0; 193 int error = 0;
196 if (drv->probe != NULL)
197 error = driver_create_file(&drv->driver, &driver_attr_new_id);
198 return error;
199}
200 194
201static void pci_remove_newid_file(struct pci_driver *drv) 195 if (drv->probe != NULL) {
202{ 196 error = driver_create_file(&drv->driver, &driver_attr_new_id);
203 driver_remove_file(&drv->driver, &driver_attr_new_id); 197 if (error == 0) {
204} 198 error = driver_create_file(&drv->driver,
205 199 &driver_attr_remove_id);
206static int 200 if (error)
207pci_create_removeid_file(struct pci_driver *drv) 201 driver_remove_file(&drv->driver,
208{ 202 &driver_attr_new_id);
209 int error = 0; 203 }
210 if (drv->probe != NULL) 204 }
211 error = driver_create_file(&drv->driver,&driver_attr_remove_id);
212 return error; 205 return error;
213} 206}
214 207
215static void pci_remove_removeid_file(struct pci_driver *drv) 208static void pci_remove_newid_files(struct pci_driver *drv)
216{ 209{
217 driver_remove_file(&drv->driver, &driver_attr_remove_id); 210 driver_remove_file(&drv->driver, &driver_attr_remove_id);
211 driver_remove_file(&drv->driver, &driver_attr_new_id);
218} 212}
219#else /* !CONFIG_HOTPLUG */ 213#else /* !CONFIG_HOTPLUG */
220static inline int pci_create_newid_file(struct pci_driver *drv) 214static inline int pci_create_newid_files(struct pci_driver *drv)
221{
222 return 0;
223}
224static inline void pci_remove_newid_file(struct pci_driver *drv) {}
225static inline int pci_create_removeid_file(struct pci_driver *drv)
226{ 215{
227 return 0; 216 return 0;
228} 217}
229static inline void pci_remove_removeid_file(struct pci_driver *drv) {} 218static inline void pci_remove_newid_files(struct pci_driver *drv) {}
230#endif 219#endif
231 220
232/** 221/**
@@ -430,6 +419,16 @@ static void pci_device_shutdown(struct device *dev)
430 drv->shutdown(pci_dev); 419 drv->shutdown(pci_dev);
431 pci_msi_shutdown(pci_dev); 420 pci_msi_shutdown(pci_dev);
432 pci_msix_shutdown(pci_dev); 421 pci_msix_shutdown(pci_dev);
422
423 /*
424 * Devices may be enabled to wake up by runtime PM, but they need not
425 * be supposed to wake up the system from its "power off" state (e.g.
426 * ACPI S5). Therefore disable wakeup for all devices that aren't
427 * supposed to wake up the system at this point. The state argument
428 * will be ignored by pci_enable_wake().
429 */
430 if (!device_may_wakeup(dev))
431 pci_enable_wake(pci_dev, PCI_UNKNOWN, false);
433} 432}
434 433
435#ifdef CONFIG_PM 434#ifdef CONFIG_PM
@@ -1138,18 +1137,12 @@ int __pci_register_driver(struct pci_driver *drv, struct module *owner,
1138 if (error) 1137 if (error)
1139 goto out; 1138 goto out;
1140 1139
1141 error = pci_create_newid_file(drv); 1140 error = pci_create_newid_files(drv);
1142 if (error) 1141 if (error)
1143 goto out_newid; 1142 goto out_newid;
1144
1145 error = pci_create_removeid_file(drv);
1146 if (error)
1147 goto out_removeid;
1148out: 1143out:
1149 return error; 1144 return error;
1150 1145
1151out_removeid:
1152 pci_remove_newid_file(drv);
1153out_newid: 1146out_newid:
1154 driver_unregister(&drv->driver); 1147 driver_unregister(&drv->driver);
1155 goto out; 1148 goto out;
@@ -1168,8 +1161,7 @@ out_newid:
1168void 1161void
1169pci_unregister_driver(struct pci_driver *drv) 1162pci_unregister_driver(struct pci_driver *drv)
1170{ 1163{
1171 pci_remove_removeid_file(drv); 1164 pci_remove_newid_files(drv);
1172 pci_remove_newid_file(drv);
1173 driver_unregister(&drv->driver); 1165 driver_unregister(&drv->driver);
1174 pci_free_dynids(drv); 1166 pci_free_dynids(drv);
1175} 1167}
diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
index 106be0d08f81..a55e248618cd 100644
--- a/drivers/pci/pci-sysfs.c
+++ b/drivers/pci/pci-sysfs.c
@@ -330,7 +330,7 @@ static void remove_callback(struct device *dev)
330 struct pci_dev *pdev = to_pci_dev(dev); 330 struct pci_dev *pdev = to_pci_dev(dev);
331 331
332 mutex_lock(&pci_remove_rescan_mutex); 332 mutex_lock(&pci_remove_rescan_mutex);
333 pci_remove_bus_device(pdev); 333 pci_stop_and_remove_bus_device(pdev);
334 mutex_unlock(&pci_remove_rescan_mutex); 334 mutex_unlock(&pci_remove_rescan_mutex);
335} 335}
336 336
@@ -366,7 +366,10 @@ dev_bus_rescan_store(struct device *dev, struct device_attribute *attr,
366 366
367 if (val) { 367 if (val) {
368 mutex_lock(&pci_remove_rescan_mutex); 368 mutex_lock(&pci_remove_rescan_mutex);
369 pci_rescan_bus(bus); 369 if (!pci_is_root_bus(bus) && list_empty(&bus->devices))
370 pci_rescan_bus_bridge_resize(bus->self);
371 else
372 pci_rescan_bus(bus);
370 mutex_unlock(&pci_remove_rescan_mutex); 373 mutex_unlock(&pci_remove_rescan_mutex);
371 } 374 }
372 return count; 375 return count;
@@ -432,7 +435,7 @@ pci_read_config(struct file *filp, struct kobject *kobj,
432 u8 *data = (u8*) buf; 435 u8 *data = (u8*) buf;
433 436
434 /* Several chips lock up trying to read undefined config space */ 437 /* Several chips lock up trying to read undefined config space */
435 if (security_capable(&init_user_ns, filp->f_cred, CAP_SYS_ADMIN) == 0) { 438 if (security_capable(filp->f_cred, &init_user_ns, CAP_SYS_ADMIN) == 0) {
436 size = dev->cfg_size; 439 size = dev->cfg_size;
437 } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) { 440 } else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
438 size = 128; 441 size = 128;
diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c
index 192be5dbde56..815674415267 100644
--- a/drivers/pci/pci.c
+++ b/drivers/pci/pci.c
@@ -94,6 +94,9 @@ u8 pci_cache_line_size;
94 */ 94 */
95unsigned int pcibios_max_latency = 255; 95unsigned int pcibios_max_latency = 255;
96 96
97/* If set, the PCIe ARI capability will not be used. */
98static bool pcie_ari_disabled;
99
97/** 100/**
98 * pci_bus_max_busnr - returns maximum PCI bus number of given bus' children 101 * pci_bus_max_busnr - returns maximum PCI bus number of given bus' children
99 * @bus: pointer to PCI bus structure to search 102 * @bus: pointer to PCI bus structure to search
@@ -825,6 +828,19 @@ EXPORT_SYMBOL(pci_choose_state);
825#define pcie_cap_has_sltctl2(type, flags) \ 828#define pcie_cap_has_sltctl2(type, flags) \
826 ((flags & PCI_EXP_FLAGS_VERS) > 1) 829 ((flags & PCI_EXP_FLAGS_VERS) > 1)
827 830
831static struct pci_cap_saved_state *pci_find_saved_cap(
832 struct pci_dev *pci_dev, char cap)
833{
834 struct pci_cap_saved_state *tmp;
835 struct hlist_node *pos;
836
837 hlist_for_each_entry(tmp, pos, &pci_dev->saved_cap_space, next) {
838 if (tmp->cap.cap_nr == cap)
839 return tmp;
840 }
841 return NULL;
842}
843
828static int pci_save_pcie_state(struct pci_dev *dev) 844static int pci_save_pcie_state(struct pci_dev *dev)
829{ 845{
830 int pos, i = 0; 846 int pos, i = 0;
@@ -959,6 +975,7 @@ void pci_restore_state(struct pci_dev *dev)
959{ 975{
960 int i; 976 int i;
961 u32 val; 977 u32 val;
978 int tries;
962 979
963 if (!dev->state_saved) 980 if (!dev->state_saved)
964 return; 981 return;
@@ -973,12 +990,16 @@ void pci_restore_state(struct pci_dev *dev)
973 */ 990 */
974 for (i = 15; i >= 0; i--) { 991 for (i = 15; i >= 0; i--) {
975 pci_read_config_dword(dev, i * 4, &val); 992 pci_read_config_dword(dev, i * 4, &val);
976 if (val != dev->saved_config_space[i]) { 993 tries = 10;
994 while (tries && val != dev->saved_config_space[i]) {
977 dev_dbg(&dev->dev, "restoring config " 995 dev_dbg(&dev->dev, "restoring config "
978 "space at offset %#x (was %#x, writing %#x)\n", 996 "space at offset %#x (was %#x, writing %#x)\n",
979 i, val, (int)dev->saved_config_space[i]); 997 i, val, (int)dev->saved_config_space[i]);
980 pci_write_config_dword(dev,i * 4, 998 pci_write_config_dword(dev,i * 4,
981 dev->saved_config_space[i]); 999 dev->saved_config_space[i]);
1000 pci_read_config_dword(dev, i * 4, &val);
1001 mdelay(10);
1002 tries--;
982 } 1003 }
983 } 1004 }
984 pci_restore_pcix_state(dev); 1005 pci_restore_pcix_state(dev);
@@ -1864,6 +1885,12 @@ void platform_pci_wakeup_init(struct pci_dev *dev)
1864 platform_pci_sleep_wake(dev, false); 1885 platform_pci_sleep_wake(dev, false);
1865} 1886}
1866 1887
1888static void pci_add_saved_cap(struct pci_dev *pci_dev,
1889 struct pci_cap_saved_state *new_cap)
1890{
1891 hlist_add_head(&new_cap->next, &pci_dev->saved_cap_space);
1892}
1893
1867/** 1894/**
1868 * pci_add_save_buffer - allocate buffer for saving given capability registers 1895 * pci_add_save_buffer - allocate buffer for saving given capability registers
1869 * @dev: the PCI device 1896 * @dev: the PCI device
@@ -1911,6 +1938,15 @@ void pci_allocate_cap_save_buffers(struct pci_dev *dev)
1911 "unable to preallocate PCI-X save buffer\n"); 1938 "unable to preallocate PCI-X save buffer\n");
1912} 1939}
1913 1940
1941void pci_free_cap_save_buffers(struct pci_dev *dev)
1942{
1943 struct pci_cap_saved_state *tmp;
1944 struct hlist_node *pos, *n;
1945
1946 hlist_for_each_entry_safe(tmp, pos, n, &dev->saved_cap_space, next)
1947 kfree(tmp);
1948}
1949
1914/** 1950/**
1915 * pci_enable_ari - enable ARI forwarding if hardware support it 1951 * pci_enable_ari - enable ARI forwarding if hardware support it
1916 * @dev: the PCI device 1952 * @dev: the PCI device
@@ -1922,7 +1958,7 @@ void pci_enable_ari(struct pci_dev *dev)
1922 u16 flags, ctrl; 1958 u16 flags, ctrl;
1923 struct pci_dev *bridge; 1959 struct pci_dev *bridge;
1924 1960
1925 if (!pci_is_pcie(dev) || dev->devfn) 1961 if (pcie_ari_disabled || !pci_is_pcie(dev) || dev->devfn)
1926 return; 1962 return;
1927 1963
1928 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI); 1964 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ARI);
@@ -2802,7 +2838,7 @@ pci_intx(struct pci_dev *pdev, int enable)
2802 2838
2803/** 2839/**
2804 * pci_intx_mask_supported - probe for INTx masking support 2840 * pci_intx_mask_supported - probe for INTx masking support
2805 * @pdev: the PCI device to operate on 2841 * @dev: the PCI device to operate on
2806 * 2842 *
2807 * Check if the device dev support INTx masking via the config space 2843 * Check if the device dev support INTx masking via the config space
2808 * command word. 2844 * command word.
@@ -2884,7 +2920,7 @@ done:
2884 2920
2885/** 2921/**
2886 * pci_check_and_mask_intx - mask INTx on pending interrupt 2922 * pci_check_and_mask_intx - mask INTx on pending interrupt
2887 * @pdev: the PCI device to operate on 2923 * @dev: the PCI device to operate on
2888 * 2924 *
2889 * Check if the device dev has its INTx line asserted, mask it and 2925 * Check if the device dev has its INTx line asserted, mask it and
2890 * return true in that case. False is returned if not interrupt was 2926 * return true in that case. False is returned if not interrupt was
@@ -2898,7 +2934,7 @@ EXPORT_SYMBOL_GPL(pci_check_and_mask_intx);
2898 2934
2899/** 2935/**
2900 * pci_check_and_mask_intx - unmask INTx of no interrupt is pending 2936 * pci_check_and_mask_intx - unmask INTx of no interrupt is pending
2901 * @pdev: the PCI device to operate on 2937 * @dev: the PCI device to operate on
2902 * 2938 *
2903 * Check if the device dev has its INTx line asserted, unmask it if not 2939 * Check if the device dev has its INTx line asserted, unmask it if not
2904 * and return true. False is returned and the mask remains active if 2940 * and return true. False is returned and the mask remains active if
@@ -3661,6 +3697,68 @@ int pci_is_reassigndev(struct pci_dev *dev)
3661 return (pci_specified_resource_alignment(dev) != 0); 3697 return (pci_specified_resource_alignment(dev) != 0);
3662} 3698}
3663 3699
3700/*
3701 * This function disables memory decoding and releases memory resources
3702 * of the device specified by kernel's boot parameter 'pci=resource_alignment='.
3703 * It also rounds up size to specified alignment.
3704 * Later on, the kernel will assign page-aligned memory resource back
3705 * to the device.
3706 */
3707void pci_reassigndev_resource_alignment(struct pci_dev *dev)
3708{
3709 int i;
3710 struct resource *r;
3711 resource_size_t align, size;
3712 u16 command;
3713
3714 if (!pci_is_reassigndev(dev))
3715 return;
3716
3717 if (dev->hdr_type == PCI_HEADER_TYPE_NORMAL &&
3718 (dev->class >> 8) == PCI_CLASS_BRIDGE_HOST) {
3719 dev_warn(&dev->dev,
3720 "Can't reassign resources to host bridge.\n");
3721 return;
3722 }
3723
3724 dev_info(&dev->dev,
3725 "Disabling memory decoding and releasing memory resources.\n");
3726 pci_read_config_word(dev, PCI_COMMAND, &command);
3727 command &= ~PCI_COMMAND_MEMORY;
3728 pci_write_config_word(dev, PCI_COMMAND, command);
3729
3730 align = pci_specified_resource_alignment(dev);
3731 for (i = 0; i < PCI_BRIDGE_RESOURCES; i++) {
3732 r = &dev->resource[i];
3733 if (!(r->flags & IORESOURCE_MEM))
3734 continue;
3735 size = resource_size(r);
3736 if (size < align) {
3737 size = align;
3738 dev_info(&dev->dev,
3739 "Rounding up size of resource #%d to %#llx.\n",
3740 i, (unsigned long long)size);
3741 }
3742 r->end = size - 1;
3743 r->start = 0;
3744 }
3745 /* Need to disable bridge's resource window,
3746 * to enable the kernel to reassign new resource
3747 * window later on.
3748 */
3749 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
3750 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
3751 for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) {
3752 r = &dev->resource[i];
3753 if (!(r->flags & IORESOURCE_MEM))
3754 continue;
3755 r->end = resource_size(r) - 1;
3756 r->start = 0;
3757 }
3758 pci_disable_bridge_window(dev);
3759 }
3760}
3761
3664ssize_t pci_set_resource_alignment_param(const char *buf, size_t count) 3762ssize_t pci_set_resource_alignment_param(const char *buf, size_t count)
3665{ 3763{
3666 if (count > RESOURCE_ALIGNMENT_PARAM_SIZE - 1) 3764 if (count > RESOURCE_ALIGNMENT_PARAM_SIZE - 1)
@@ -3739,10 +3837,14 @@ static int __init pci_setup(char *str)
3739 pci_no_msi(); 3837 pci_no_msi();
3740 } else if (!strcmp(str, "noaer")) { 3838 } else if (!strcmp(str, "noaer")) {
3741 pci_no_aer(); 3839 pci_no_aer();
3840 } else if (!strncmp(str, "realloc=", 8)) {
3841 pci_realloc_get_opt(str + 8);
3742 } else if (!strncmp(str, "realloc", 7)) { 3842 } else if (!strncmp(str, "realloc", 7)) {
3743 pci_realloc(); 3843 pci_realloc_get_opt("on");
3744 } else if (!strcmp(str, "nodomains")) { 3844 } else if (!strcmp(str, "nodomains")) {
3745 pci_no_domains(); 3845 pci_no_domains();
3846 } else if (!strncmp(str, "noari", 5)) {
3847 pcie_ari_disabled = true;
3746 } else if (!strncmp(str, "cbiosize=", 9)) { 3848 } else if (!strncmp(str, "cbiosize=", 9)) {
3747 pci_cardbus_io_size = memparse(str + 9, &str); 3849 pci_cardbus_io_size = memparse(str + 9, &str);
3748 } else if (!strncmp(str, "cbmemsize=", 10)) { 3850 } else if (!strncmp(str, "cbmemsize=", 10)) {
diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index 1009a5e88e53..e4943479b234 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -73,6 +73,7 @@ extern int __pci_pme_wakeup(struct pci_dev *dev, void *ign);
73extern void pci_pm_init(struct pci_dev *dev); 73extern void pci_pm_init(struct pci_dev *dev);
74extern void platform_pci_wakeup_init(struct pci_dev *dev); 74extern void platform_pci_wakeup_init(struct pci_dev *dev);
75extern void pci_allocate_cap_save_buffers(struct pci_dev *dev); 75extern void pci_allocate_cap_save_buffers(struct pci_dev *dev);
76void pci_free_cap_save_buffers(struct pci_dev *dev);
76 77
77static inline void pci_wakeup_event(struct pci_dev *dev) 78static inline void pci_wakeup_event(struct pci_dev *dev)
78{ 79{
@@ -148,7 +149,7 @@ static inline void pci_no_msi(void) { }
148static inline void pci_msi_init_pci_dev(struct pci_dev *dev) { } 149static inline void pci_msi_init_pci_dev(struct pci_dev *dev) { }
149#endif 150#endif
150 151
151extern void pci_realloc(void); 152void pci_realloc_get_opt(char *);
152 153
153static inline int pci_no_d1d2(struct pci_dev *dev) 154static inline int pci_no_d1d2(struct pci_dev *dev)
154{ 155{
@@ -207,6 +208,8 @@ enum pci_bar_type {
207 pci_bar_mem64, /* A 64-bit memory BAR */ 208 pci_bar_mem64, /* A 64-bit memory BAR */
208}; 209};
209 210
211bool pci_bus_read_dev_vendor_id(struct pci_bus *bus, int devfn, u32 *pl,
212 int crs_timeout);
210extern int pci_setup_device(struct pci_dev *dev); 213extern int pci_setup_device(struct pci_dev *dev);
211extern int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type, 214extern int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
212 struct resource *res, unsigned int reg); 215 struct resource *res, unsigned int reg);
@@ -225,11 +228,8 @@ static inline int pci_ari_enabled(struct pci_bus *bus)
225 return bus->self && bus->self->ari_enabled; 228 return bus->self && bus->self->ari_enabled;
226} 229}
227 230
228#ifdef CONFIG_PCI_QUIRKS 231void pci_reassigndev_resource_alignment(struct pci_dev *dev);
229extern int pci_is_reassigndev(struct pci_dev *dev);
230resource_size_t pci_specified_resource_alignment(struct pci_dev *dev);
231extern void pci_disable_bridge_window(struct pci_dev *dev); 232extern void pci_disable_bridge_window(struct pci_dev *dev);
232#endif
233 233
234/* Single Root I/O Virtualization */ 234/* Single Root I/O Virtualization */
235struct pci_sriov { 235struct pci_sriov {
diff --git a/drivers/pci/pcie/Kconfig b/drivers/pci/pcie/Kconfig
index 72962cc92e0a..6c8bc5809787 100644
--- a/drivers/pci/pcie/Kconfig
+++ b/drivers/pci/pcie/Kconfig
@@ -55,6 +55,31 @@ config PCIEASPM_DEBUG
55 This enables PCI Express ASPM debug support. It will add per-device 55 This enables PCI Express ASPM debug support. It will add per-device
56 interface to control ASPM. 56 interface to control ASPM.
57 57
58choice
59 prompt "Default ASPM policy"
60 default PCIEASPM_DEFAULT
61 depends on PCIEASPM
62
63config PCIEASPM_DEFAULT
64 bool "BIOS default"
65 depends on PCIEASPM
66 help
67 Use the BIOS defaults for PCI Express ASPM.
68
69config PCIEASPM_POWERSAVE
70 bool "Powersave"
71 depends on PCIEASPM
72 help
73 Enable PCI Express ASPM L0s and L1 where possible, even if the
74 BIOS did not.
75
76config PCIEASPM_PERFORMANCE
77 bool "Performance"
78 depends on PCIEASPM
79 help
80 Disable PCI Express ASPM L0s and L1, even if the BIOS enabled them.
81endchoice
82
58config PCIE_PME 83config PCIE_PME
59 def_bool y 84 def_bool y
60 depends on PCIEPORTBUS && PM_RUNTIME && EXPERIMENTAL && ACPI 85 depends on PCIEPORTBUS && PM_RUNTIME && EXPERIMENTAL && ACPI
diff --git a/drivers/pci/pcie/aer/aer_inject.c b/drivers/pci/pcie/aer/aer_inject.c
index 95489cd9a555..52229863e9fe 100644
--- a/drivers/pci/pcie/aer/aer_inject.c
+++ b/drivers/pci/pcie/aer/aer_inject.c
@@ -28,7 +28,7 @@
28#include "aerdrv.h" 28#include "aerdrv.h"
29 29
30/* Override the existing corrected and uncorrected error masks */ 30/* Override the existing corrected and uncorrected error masks */
31static int aer_mask_override; 31static bool aer_mask_override;
32module_param(aer_mask_override, bool, 0); 32module_param(aer_mask_override, bool, 0);
33 33
34struct aer_error_inj { 34struct aer_error_inj {
diff --git a/drivers/pci/pcie/aer/aerdrv_core.c b/drivers/pci/pcie/aer/aerdrv_core.c
index 9674e9f30d49..0ca053538146 100644
--- a/drivers/pci/pcie/aer/aerdrv_core.c
+++ b/drivers/pci/pcie/aer/aerdrv_core.c
@@ -27,8 +27,8 @@
27#include <linux/kfifo.h> 27#include <linux/kfifo.h>
28#include "aerdrv.h" 28#include "aerdrv.h"
29 29
30static int forceload; 30static bool forceload;
31static int nosourceid; 31static bool nosourceid;
32module_param(forceload, bool, 0); 32module_param(forceload, bool, 0);
33module_param(nosourceid, bool, 0); 33module_param(nosourceid, bool, 0);
34 34
diff --git a/drivers/pci/pcie/aspm.c b/drivers/pci/pcie/aspm.c
index 1cfbf228fbb1..b500840a143b 100644
--- a/drivers/pci/pcie/aspm.c
+++ b/drivers/pci/pcie/aspm.c
@@ -76,7 +76,15 @@ static LIST_HEAD(link_list);
76#define POLICY_DEFAULT 0 /* BIOS default setting */ 76#define POLICY_DEFAULT 0 /* BIOS default setting */
77#define POLICY_PERFORMANCE 1 /* high performance */ 77#define POLICY_PERFORMANCE 1 /* high performance */
78#define POLICY_POWERSAVE 2 /* high power saving */ 78#define POLICY_POWERSAVE 2 /* high power saving */
79
80#ifdef CONFIG_PCIEASPM_PERFORMANCE
81static int aspm_policy = POLICY_PERFORMANCE;
82#elif defined CONFIG_PCIEASPM_POWERSAVE
83static int aspm_policy = POLICY_POWERSAVE;
84#else
79static int aspm_policy; 85static int aspm_policy;
86#endif
87
80static const char *policy_str[] = { 88static const char *policy_str[] = {
81 [POLICY_DEFAULT] = "default", 89 [POLICY_DEFAULT] = "default",
82 [POLICY_PERFORMANCE] = "performance", 90 [POLICY_PERFORMANCE] = "performance",
@@ -508,6 +516,16 @@ static int pcie_aspm_sanity_check(struct pci_dev *pdev)
508 pos = pci_pcie_cap(child); 516 pos = pci_pcie_cap(child);
509 if (!pos) 517 if (!pos)
510 return -EINVAL; 518 return -EINVAL;
519
520 /*
521 * If ASPM is disabled then we're not going to change
522 * the BIOS state. It's safe to continue even if it's a
523 * pre-1.1 device
524 */
525
526 if (aspm_disabled)
527 continue;
528
511 /* 529 /*
512 * Disable ASPM for pre-1.1 PCIe device, we follow MS to use 530 * Disable ASPM for pre-1.1 PCIe device, we follow MS to use
513 * RBER bit to determine if a function is 1.1 version device 531 * RBER bit to determine if a function is 1.1 version device
diff --git a/drivers/pci/pcie/portdrv.h b/drivers/pci/pcie/portdrv.h
index bd00a01aef14..eea2ca2375e6 100644
--- a/drivers/pci/pcie/portdrv.h
+++ b/drivers/pci/pcie/portdrv.h
@@ -34,6 +34,18 @@ struct pci_dev;
34 34
35extern void pcie_clear_root_pme_status(struct pci_dev *dev); 35extern void pcie_clear_root_pme_status(struct pci_dev *dev);
36 36
37#ifdef CONFIG_HOTPLUG_PCI_PCIE
38extern bool pciehp_msi_disabled;
39
40static inline bool pciehp_no_msi(void)
41{
42 return pciehp_msi_disabled;
43}
44
45#else /* !CONFIG_HOTPLUG_PCI_PCIE */
46static inline bool pciehp_no_msi(void) { return false; }
47#endif /* !CONFIG_HOTPLUG_PCI_PCIE */
48
37#ifdef CONFIG_PCIE_PME 49#ifdef CONFIG_PCIE_PME
38extern bool pcie_pme_msi_disabled; 50extern bool pcie_pme_msi_disabled;
39 51
diff --git a/drivers/pci/pcie/portdrv_core.c b/drivers/pci/pcie/portdrv_core.c
index 595654a1a6a6..2f589a54f9bd 100644
--- a/drivers/pci/pcie/portdrv_core.c
+++ b/drivers/pci/pcie/portdrv_core.c
@@ -19,6 +19,17 @@
19#include "../pci.h" 19#include "../pci.h"
20#include "portdrv.h" 20#include "portdrv.h"
21 21
22bool pciehp_msi_disabled;
23
24static int __init pciehp_setup(char *str)
25{
26 if (!strncmp(str, "nomsi", 5))
27 pciehp_msi_disabled = true;
28
29 return 1;
30}
31__setup("pcie_hp=", pciehp_setup);
32
22/** 33/**
23 * release_pcie_device - free PCI Express port service device structure 34 * release_pcie_device - free PCI Express port service device structure
24 * @dev: Port service device to release 35 * @dev: Port service device to release
@@ -189,8 +200,9 @@ static int init_service_irqs(struct pci_dev *dev, int *irqs, int mask)
189{ 200{
190 int i, irq = -1; 201 int i, irq = -1;
191 202
192 /* We have to use INTx if MSI cannot be used for PCIe PME. */ 203 /* We have to use INTx if MSI cannot be used for PCIe PME or pciehp. */
193 if ((mask & PCIE_PORT_SERVICE_PME) && pcie_pme_no_msi()) { 204 if (((mask & PCIE_PORT_SERVICE_PME) && pcie_pme_no_msi()) ||
205 ((mask & PCIE_PORT_SERVICE_HP) && pciehp_no_msi())) {
194 if (dev->pin) 206 if (dev->pin)
195 irq = dev->irq; 207 irq = dev->irq;
196 goto no_msi; 208 goto no_msi;
diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c
index 7cc9e2f0f47c..5e1ca3c58a7d 100644
--- a/drivers/pci/probe.c
+++ b/drivers/pci/probe.c
@@ -15,6 +15,8 @@
15#define CARDBUS_LATENCY_TIMER 176 /* secondary latency timer */ 15#define CARDBUS_LATENCY_TIMER 176 /* secondary latency timer */
16#define CARDBUS_RESERVE_BUSNR 3 16#define CARDBUS_RESERVE_BUSNR 3
17 17
18static LIST_HEAD(pci_host_bridges);
19
18/* Ugh. Need to stop exporting this to modules. */ 20/* Ugh. Need to stop exporting this to modules. */
19LIST_HEAD(pci_root_buses); 21LIST_HEAD(pci_root_buses);
20EXPORT_SYMBOL(pci_root_buses); 22EXPORT_SYMBOL(pci_root_buses);
@@ -42,6 +44,82 @@ int no_pci_devices(void)
42} 44}
43EXPORT_SYMBOL(no_pci_devices); 45EXPORT_SYMBOL(no_pci_devices);
44 46
47static struct pci_host_bridge *pci_host_bridge(struct pci_dev *dev)
48{
49 struct pci_bus *bus;
50 struct pci_host_bridge *bridge;
51
52 bus = dev->bus;
53 while (bus->parent)
54 bus = bus->parent;
55
56 list_for_each_entry(bridge, &pci_host_bridges, list) {
57 if (bridge->bus == bus)
58 return bridge;
59 }
60
61 return NULL;
62}
63
64static bool resource_contains(struct resource *res1, struct resource *res2)
65{
66 return res1->start <= res2->start && res1->end >= res2->end;
67}
68
69void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
70 struct resource *res)
71{
72 struct pci_host_bridge *bridge = pci_host_bridge(dev);
73 struct pci_host_bridge_window *window;
74 resource_size_t offset = 0;
75
76 list_for_each_entry(window, &bridge->windows, list) {
77 if (resource_type(res) != resource_type(window->res))
78 continue;
79
80 if (resource_contains(window->res, res)) {
81 offset = window->offset;
82 break;
83 }
84 }
85
86 region->start = res->start - offset;
87 region->end = res->end - offset;
88}
89EXPORT_SYMBOL(pcibios_resource_to_bus);
90
91static bool region_contains(struct pci_bus_region *region1,
92 struct pci_bus_region *region2)
93{
94 return region1->start <= region2->start && region1->end >= region2->end;
95}
96
97void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
98 struct pci_bus_region *region)
99{
100 struct pci_host_bridge *bridge = pci_host_bridge(dev);
101 struct pci_host_bridge_window *window;
102 struct pci_bus_region bus_region;
103 resource_size_t offset = 0;
104
105 list_for_each_entry(window, &bridge->windows, list) {
106 if (resource_type(res) != resource_type(window->res))
107 continue;
108
109 bus_region.start = window->res->start - window->offset;
110 bus_region.end = window->res->end - window->offset;
111
112 if (region_contains(&bus_region, region)) {
113 offset = window->offset;
114 break;
115 }
116 }
117
118 res->start = region->start + offset;
119 res->end = region->end + offset;
120}
121EXPORT_SYMBOL(pcibios_bus_to_resource);
122
45/* 123/*
46 * PCI Bus Class 124 * PCI Bus Class
47 */ 125 */
@@ -135,6 +213,7 @@ int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
135{ 213{
136 u32 l, sz, mask; 214 u32 l, sz, mask;
137 u16 orig_cmd; 215 u16 orig_cmd;
216 struct pci_bus_region region;
138 217
139 mask = type ? PCI_ROM_ADDRESS_MASK : ~0; 218 mask = type ? PCI_ROM_ADDRESS_MASK : ~0;
140 219
@@ -214,11 +293,13 @@ int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
214 /* Address above 32-bit boundary; disable the BAR */ 293 /* Address above 32-bit boundary; disable the BAR */
215 pci_write_config_dword(dev, pos, 0); 294 pci_write_config_dword(dev, pos, 0);
216 pci_write_config_dword(dev, pos + 4, 0); 295 pci_write_config_dword(dev, pos + 4, 0);
217 res->start = 0; 296 region.start = 0;
218 res->end = sz64; 297 region.end = sz64;
298 pcibios_bus_to_resource(dev, res, &region);
219 } else { 299 } else {
220 res->start = l64; 300 region.start = l64;
221 res->end = l64 + sz64; 301 region.end = l64 + sz64;
302 pcibios_bus_to_resource(dev, res, &region);
222 dev_printk(KERN_DEBUG, &dev->dev, "reg %x: %pR\n", 303 dev_printk(KERN_DEBUG, &dev->dev, "reg %x: %pR\n",
223 pos, res); 304 pos, res);
224 } 305 }
@@ -228,8 +309,9 @@ int __pci_read_base(struct pci_dev *dev, enum pci_bar_type type,
228 if (!sz) 309 if (!sz)
229 goto fail; 310 goto fail;
230 311
231 res->start = l; 312 region.start = l;
232 res->end = l + sz; 313 region.end = l + sz;
314 pcibios_bus_to_resource(dev, res, &region);
233 315
234 dev_printk(KERN_DEBUG, &dev->dev, "reg %x: %pR\n", pos, res); 316 dev_printk(KERN_DEBUG, &dev->dev, "reg %x: %pR\n", pos, res);
235 } 317 }
@@ -266,7 +348,8 @@ static void __devinit pci_read_bridge_io(struct pci_bus *child)
266 struct pci_dev *dev = child->self; 348 struct pci_dev *dev = child->self;
267 u8 io_base_lo, io_limit_lo; 349 u8 io_base_lo, io_limit_lo;
268 unsigned long base, limit; 350 unsigned long base, limit;
269 struct resource *res; 351 struct pci_bus_region region;
352 struct resource *res, res2;
270 353
271 res = child->resource[0]; 354 res = child->resource[0];
272 pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo); 355 pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo);
@@ -284,10 +367,14 @@ static void __devinit pci_read_bridge_io(struct pci_bus *child)
284 367
285 if (base && base <= limit) { 368 if (base && base <= limit) {
286 res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO; 369 res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO;
370 res2.flags = res->flags;
371 region.start = base;
372 region.end = limit + 0xfff;
373 pcibios_bus_to_resource(dev, &res2, &region);
287 if (!res->start) 374 if (!res->start)
288 res->start = base; 375 res->start = res2.start;
289 if (!res->end) 376 if (!res->end)
290 res->end = limit + 0xfff; 377 res->end = res2.end;
291 dev_printk(KERN_DEBUG, &dev->dev, " bridge window %pR\n", res); 378 dev_printk(KERN_DEBUG, &dev->dev, " bridge window %pR\n", res);
292 } 379 }
293} 380}
@@ -297,6 +384,7 @@ static void __devinit pci_read_bridge_mmio(struct pci_bus *child)
297 struct pci_dev *dev = child->self; 384 struct pci_dev *dev = child->self;
298 u16 mem_base_lo, mem_limit_lo; 385 u16 mem_base_lo, mem_limit_lo;
299 unsigned long base, limit; 386 unsigned long base, limit;
387 struct pci_bus_region region;
300 struct resource *res; 388 struct resource *res;
301 389
302 res = child->resource[1]; 390 res = child->resource[1];
@@ -306,8 +394,9 @@ static void __devinit pci_read_bridge_mmio(struct pci_bus *child)
306 limit = (mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16; 394 limit = (mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16;
307 if (base && base <= limit) { 395 if (base && base <= limit) {
308 res->flags = (mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | IORESOURCE_MEM; 396 res->flags = (mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) | IORESOURCE_MEM;
309 res->start = base; 397 region.start = base;
310 res->end = limit + 0xfffff; 398 region.end = limit + 0xfffff;
399 pcibios_bus_to_resource(dev, res, &region);
311 dev_printk(KERN_DEBUG, &dev->dev, " bridge window %pR\n", res); 400 dev_printk(KERN_DEBUG, &dev->dev, " bridge window %pR\n", res);
312 } 401 }
313} 402}
@@ -317,6 +406,7 @@ static void __devinit pci_read_bridge_mmio_pref(struct pci_bus *child)
317 struct pci_dev *dev = child->self; 406 struct pci_dev *dev = child->self;
318 u16 mem_base_lo, mem_limit_lo; 407 u16 mem_base_lo, mem_limit_lo;
319 unsigned long base, limit; 408 unsigned long base, limit;
409 struct pci_bus_region region;
320 struct resource *res; 410 struct resource *res;
321 411
322 res = child->resource[2]; 412 res = child->resource[2];
@@ -353,8 +443,9 @@ static void __devinit pci_read_bridge_mmio_pref(struct pci_bus *child)
353 IORESOURCE_MEM | IORESOURCE_PREFETCH; 443 IORESOURCE_MEM | IORESOURCE_PREFETCH;
354 if (res->flags & PCI_PREF_RANGE_TYPE_64) 444 if (res->flags & PCI_PREF_RANGE_TYPE_64)
355 res->flags |= IORESOURCE_MEM_64; 445 res->flags |= IORESOURCE_MEM_64;
356 res->start = base; 446 region.start = base;
357 res->end = limit + 0xfffff; 447 region.end = limit + 0xfffff;
448 pcibios_bus_to_resource(dev, res, &region);
358 dev_printk(KERN_DEBUG, &dev->dev, " bridge window %pR\n", res); 449 dev_printk(KERN_DEBUG, &dev->dev, " bridge window %pR\n", res);
359 } 450 }
360} 451}
@@ -651,6 +742,11 @@ int __devinit pci_scan_bridge(struct pci_bus *bus, struct pci_dev *dev, int max,
651 dev_dbg(&dev->dev, "scanning [bus %02x-%02x] behind bridge, pass %d\n", 742 dev_dbg(&dev->dev, "scanning [bus %02x-%02x] behind bridge, pass %d\n",
652 secondary, subordinate, pass); 743 secondary, subordinate, pass);
653 744
745 if (!primary && (primary != bus->number) && secondary && subordinate) {
746 dev_warn(&dev->dev, "Primary bus is hard wired to 0\n");
747 primary = bus->number;
748 }
749
654 /* Check if setup is sensible at all */ 750 /* Check if setup is sensible at all */
655 if (!pass && 751 if (!pass &&
656 (primary != bus->number || secondary <= bus->number)) { 752 (primary != bus->number || secondary <= bus->number)) {
@@ -895,6 +991,8 @@ int pci_setup_device(struct pci_dev *dev)
895 u8 hdr_type; 991 u8 hdr_type;
896 struct pci_slot *slot; 992 struct pci_slot *slot;
897 int pos = 0; 993 int pos = 0;
994 struct pci_bus_region region;
995 struct resource *res;
898 996
899 if (pci_read_config_byte(dev, PCI_HEADER_TYPE, &hdr_type)) 997 if (pci_read_config_byte(dev, PCI_HEADER_TYPE, &hdr_type))
900 return -EIO; 998 return -EIO;
@@ -921,12 +1019,10 @@ int pci_setup_device(struct pci_dev *dev)
921 1019
922 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class); 1020 pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
923 dev->revision = class & 0xff; 1021 dev->revision = class & 0xff;
924 class >>= 8; /* upper 3 bytes */ 1022 dev->class = class >> 8; /* upper 3 bytes */
925 dev->class = class;
926 class >>= 8;
927 1023
928 dev_printk(KERN_DEBUG, &dev->dev, "[%04x:%04x] type %d class %#08x\n", 1024 dev_printk(KERN_DEBUG, &dev->dev, "[%04x:%04x] type %02x class %#08x\n",
929 dev->vendor, dev->device, dev->hdr_type, class); 1025 dev->vendor, dev->device, dev->hdr_type, dev->class);
930 1026
931 /* need to have dev->class ready */ 1027 /* need to have dev->class ready */
932 dev->cfg_size = pci_cfg_space_size(dev); 1028 dev->cfg_size = pci_cfg_space_size(dev);
@@ -958,20 +1054,28 @@ int pci_setup_device(struct pci_dev *dev)
958 u8 progif; 1054 u8 progif;
959 pci_read_config_byte(dev, PCI_CLASS_PROG, &progif); 1055 pci_read_config_byte(dev, PCI_CLASS_PROG, &progif);
960 if ((progif & 1) == 0) { 1056 if ((progif & 1) == 0) {
961 dev->resource[0].start = 0x1F0; 1057 region.start = 0x1F0;
962 dev->resource[0].end = 0x1F7; 1058 region.end = 0x1F7;
963 dev->resource[0].flags = LEGACY_IO_RESOURCE; 1059 res = &dev->resource[0];
964 dev->resource[1].start = 0x3F6; 1060 res->flags = LEGACY_IO_RESOURCE;
965 dev->resource[1].end = 0x3F6; 1061 pcibios_bus_to_resource(dev, res, &region);
966 dev->resource[1].flags = LEGACY_IO_RESOURCE; 1062 region.start = 0x3F6;
1063 region.end = 0x3F6;
1064 res = &dev->resource[1];
1065 res->flags = LEGACY_IO_RESOURCE;
1066 pcibios_bus_to_resource(dev, res, &region);
967 } 1067 }
968 if ((progif & 4) == 0) { 1068 if ((progif & 4) == 0) {
969 dev->resource[2].start = 0x170; 1069 region.start = 0x170;
970 dev->resource[2].end = 0x177; 1070 region.end = 0x177;
971 dev->resource[2].flags = LEGACY_IO_RESOURCE; 1071 res = &dev->resource[2];
972 dev->resource[3].start = 0x376; 1072 res->flags = LEGACY_IO_RESOURCE;
973 dev->resource[3].end = 0x376; 1073 pcibios_bus_to_resource(dev, res, &region);
974 dev->resource[3].flags = LEGACY_IO_RESOURCE; 1074 region.start = 0x376;
1075 region.end = 0x376;
1076 res = &dev->resource[3];
1077 res->flags = LEGACY_IO_RESOURCE;
1078 pcibios_bus_to_resource(dev, res, &region);
975 } 1079 }
976 } 1080 }
977 break; 1081 break;
@@ -1008,8 +1112,8 @@ int pci_setup_device(struct pci_dev *dev)
1008 return -EIO; 1112 return -EIO;
1009 1113
1010 bad: 1114 bad:
1011 dev_err(&dev->dev, "ignoring class %02x (doesn't match header " 1115 dev_err(&dev->dev, "ignoring class %#08x (doesn't match header "
1012 "type %02x)\n", class, dev->hdr_type); 1116 "type %02x)\n", dev->class, dev->hdr_type);
1013 dev->class = PCI_CLASS_NOT_DEFINED; 1117 dev->class = PCI_CLASS_NOT_DEFINED;
1014 } 1118 }
1015 1119
@@ -1021,6 +1125,7 @@ static void pci_release_capabilities(struct pci_dev *dev)
1021{ 1125{
1022 pci_vpd_release(dev); 1126 pci_vpd_release(dev);
1023 pci_iov_release(dev); 1127 pci_iov_release(dev);
1128 pci_free_cap_save_buffers(dev);
1024} 1129}
1025 1130
1026/** 1131/**
@@ -1113,40 +1218,54 @@ struct pci_dev *alloc_pci_dev(void)
1113} 1218}
1114EXPORT_SYMBOL(alloc_pci_dev); 1219EXPORT_SYMBOL(alloc_pci_dev);
1115 1220
1116/* 1221bool pci_bus_read_dev_vendor_id(struct pci_bus *bus, int devfn, u32 *l,
1117 * Read the config data for a PCI device, sanity-check it 1222 int crs_timeout)
1118 * and fill in the dev structure...
1119 */
1120static struct pci_dev *pci_scan_device(struct pci_bus *bus, int devfn)
1121{ 1223{
1122 struct pci_dev *dev;
1123 u32 l;
1124 int delay = 1; 1224 int delay = 1;
1125 1225
1126 if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, &l)) 1226 if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, l))
1127 return NULL; 1227 return false;
1128 1228
1129 /* some broken boards return 0 or ~0 if a slot is empty: */ 1229 /* some broken boards return 0 or ~0 if a slot is empty: */
1130 if (l == 0xffffffff || l == 0x00000000 || 1230 if (*l == 0xffffffff || *l == 0x00000000 ||
1131 l == 0x0000ffff || l == 0xffff0000) 1231 *l == 0x0000ffff || *l == 0xffff0000)
1132 return NULL; 1232 return false;
1133 1233
1134 /* Configuration request Retry Status */ 1234 /* Configuration request Retry Status */
1135 while (l == 0xffff0001) { 1235 while (*l == 0xffff0001) {
1236 if (!crs_timeout)
1237 return false;
1238
1136 msleep(delay); 1239 msleep(delay);
1137 delay *= 2; 1240 delay *= 2;
1138 if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, &l)) 1241 if (pci_bus_read_config_dword(bus, devfn, PCI_VENDOR_ID, l))
1139 return NULL; 1242 return false;
1140 /* Card hasn't responded in 60 seconds? Must be stuck. */ 1243 /* Card hasn't responded in 60 seconds? Must be stuck. */
1141 if (delay > 60 * 1000) { 1244 if (delay > crs_timeout) {
1142 printk(KERN_WARNING "pci %04x:%02x:%02x.%d: not " 1245 printk(KERN_WARNING "pci %04x:%02x:%02x.%d: not "
1143 "responding\n", pci_domain_nr(bus), 1246 "responding\n", pci_domain_nr(bus),
1144 bus->number, PCI_SLOT(devfn), 1247 bus->number, PCI_SLOT(devfn),
1145 PCI_FUNC(devfn)); 1248 PCI_FUNC(devfn));
1146 return NULL; 1249 return false;
1147 } 1250 }
1148 } 1251 }
1149 1252
1253 return true;
1254}
1255EXPORT_SYMBOL(pci_bus_read_dev_vendor_id);
1256
1257/*
1258 * Read the config data for a PCI device, sanity-check it
1259 * and fill in the dev structure...
1260 */
1261static struct pci_dev *pci_scan_device(struct pci_bus *bus, int devfn)
1262{
1263 struct pci_dev *dev;
1264 u32 l;
1265
1266 if (!pci_bus_read_dev_vendor_id(bus, devfn, &l, 60*1000))
1267 return NULL;
1268
1150 dev = alloc_pci_dev(); 1269 dev = alloc_pci_dev();
1151 if (!dev) 1270 if (!dev)
1152 return NULL; 1271 return NULL;
@@ -1207,6 +1326,9 @@ void pci_device_add(struct pci_dev *dev, struct pci_bus *bus)
1207 /* Fix up broken headers */ 1326 /* Fix up broken headers */
1208 pci_fixup_device(pci_fixup_header, dev); 1327 pci_fixup_device(pci_fixup_header, dev);
1209 1328
1329 /* moved out from quirk header fixup code */
1330 pci_reassigndev_resource_alignment(dev);
1331
1210 /* Clear the state_saved flag. */ 1332 /* Clear the state_saved flag. */
1211 dev->state_saved = false; 1333 dev->state_saved = false;
1212 1334
@@ -1525,21 +1647,27 @@ unsigned int __devinit pci_scan_child_bus(struct pci_bus *bus)
1525struct pci_bus *pci_create_root_bus(struct device *parent, int bus, 1647struct pci_bus *pci_create_root_bus(struct device *parent, int bus,
1526 struct pci_ops *ops, void *sysdata, struct list_head *resources) 1648 struct pci_ops *ops, void *sysdata, struct list_head *resources)
1527{ 1649{
1528 int error, i; 1650 int error;
1651 struct pci_host_bridge *bridge;
1529 struct pci_bus *b, *b2; 1652 struct pci_bus *b, *b2;
1530 struct device *dev; 1653 struct device *dev;
1531 struct pci_bus_resource *bus_res, *n; 1654 struct pci_host_bridge_window *window, *n;
1532 struct resource *res; 1655 struct resource *res;
1656 resource_size_t offset;
1657 char bus_addr[64];
1658 char *fmt;
1659
1660 bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
1661 if (!bridge)
1662 return NULL;
1533 1663
1534 b = pci_alloc_bus(); 1664 b = pci_alloc_bus();
1535 if (!b) 1665 if (!b)
1536 return NULL; 1666 goto err_bus;
1537 1667
1538 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 1668 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1539 if (!dev) { 1669 if (!dev)
1540 kfree(b); 1670 goto err_dev;
1541 return NULL;
1542 }
1543 1671
1544 b->sysdata = sysdata; 1672 b->sysdata = sysdata;
1545 b->ops = ops; 1673 b->ops = ops;
@@ -1551,10 +1679,6 @@ struct pci_bus *pci_create_root_bus(struct device *parent, int bus,
1551 goto err_out; 1679 goto err_out;
1552 } 1680 }
1553 1681
1554 down_write(&pci_bus_sem);
1555 list_add_tail(&b->node, &pci_root_buses);
1556 up_write(&pci_bus_sem);
1557
1558 dev->parent = parent; 1682 dev->parent = parent;
1559 dev->release = pci_release_bus_bridge_dev; 1683 dev->release = pci_release_bus_bridge_dev;
1560 dev_set_name(dev, "pci%04x:%02x", pci_domain_nr(b), bus); 1684 dev_set_name(dev, "pci%04x:%02x", pci_domain_nr(b), bus);
@@ -1580,31 +1704,53 @@ struct pci_bus *pci_create_root_bus(struct device *parent, int bus,
1580 1704
1581 b->number = b->secondary = bus; 1705 b->number = b->secondary = bus;
1582 1706
1583 /* Add initial resources to the bus */ 1707 bridge->bus = b;
1584 list_for_each_entry_safe(bus_res, n, resources, list) 1708 INIT_LIST_HEAD(&bridge->windows);
1585 list_move_tail(&bus_res->list, &b->resources);
1586 1709
1587 if (parent) 1710 if (parent)
1588 dev_info(parent, "PCI host bridge to bus %s\n", dev_name(&b->dev)); 1711 dev_info(parent, "PCI host bridge to bus %s\n", dev_name(&b->dev));
1589 else 1712 else
1590 printk(KERN_INFO "PCI host bridge to bus %s\n", dev_name(&b->dev)); 1713 printk(KERN_INFO "PCI host bridge to bus %s\n", dev_name(&b->dev));
1591 1714
1592 pci_bus_for_each_resource(b, res, i) { 1715 /* Add initial resources to the bus */
1593 if (res) 1716 list_for_each_entry_safe(window, n, resources, list) {
1594 dev_info(&b->dev, "root bus resource %pR\n", res); 1717 list_move_tail(&window->list, &bridge->windows);
1718 res = window->res;
1719 offset = window->offset;
1720 pci_bus_add_resource(b, res, 0);
1721 if (offset) {
1722 if (resource_type(res) == IORESOURCE_IO)
1723 fmt = " (bus address [%#06llx-%#06llx])";
1724 else
1725 fmt = " (bus address [%#010llx-%#010llx])";
1726 snprintf(bus_addr, sizeof(bus_addr), fmt,
1727 (unsigned long long) (res->start - offset),
1728 (unsigned long long) (res->end - offset));
1729 } else
1730 bus_addr[0] = '\0';
1731 dev_info(&b->dev, "root bus resource %pR%s\n", res, bus_addr);
1595 } 1732 }
1596 1733
1734 down_write(&pci_bus_sem);
1735 list_add_tail(&bridge->list, &pci_host_bridges);
1736 list_add_tail(&b->node, &pci_root_buses);
1737 up_write(&pci_bus_sem);
1738
1597 return b; 1739 return b;
1598 1740
1599class_dev_reg_err: 1741class_dev_reg_err:
1600 device_unregister(dev); 1742 device_unregister(dev);
1601dev_reg_err: 1743dev_reg_err:
1602 down_write(&pci_bus_sem); 1744 down_write(&pci_bus_sem);
1745 list_del(&bridge->list);
1603 list_del(&b->node); 1746 list_del(&b->node);
1604 up_write(&pci_bus_sem); 1747 up_write(&pci_bus_sem);
1605err_out: 1748err_out:
1606 kfree(dev); 1749 kfree(dev);
1750err_dev:
1607 kfree(b); 1751 kfree(b);
1752err_bus:
1753 kfree(bridge);
1608 return NULL; 1754 return NULL;
1609} 1755}
1610 1756
@@ -1662,36 +1808,29 @@ EXPORT_SYMBOL(pci_scan_bus);
1662 1808
1663#ifdef CONFIG_HOTPLUG 1809#ifdef CONFIG_HOTPLUG
1664/** 1810/**
1665 * pci_rescan_bus - scan a PCI bus for devices. 1811 * pci_rescan_bus_bridge_resize - scan a PCI bus for devices.
1666 * @bus: PCI bus to scan 1812 * @bridge: PCI bridge for the bus to scan
1667 * 1813 *
1668 * Scan a PCI bus and child buses for new devices, adds them, 1814 * Scan a PCI bus and child buses for new devices, add them,
1669 * and enables them. 1815 * and enable them, resizing bridge mmio/io resource if necessary
1816 * and possible. The caller must ensure the child devices are already
1817 * removed for resizing to occur.
1670 * 1818 *
1671 * Returns the max number of subordinate bus discovered. 1819 * Returns the max number of subordinate bus discovered.
1672 */ 1820 */
1673unsigned int __ref pci_rescan_bus(struct pci_bus *bus) 1821unsigned int __ref pci_rescan_bus_bridge_resize(struct pci_dev *bridge)
1674{ 1822{
1675 unsigned int max; 1823 unsigned int max;
1676 struct pci_dev *dev; 1824 struct pci_bus *bus = bridge->subordinate;
1677 1825
1678 max = pci_scan_child_bus(bus); 1826 max = pci_scan_child_bus(bus);
1679 1827
1680 down_read(&pci_bus_sem); 1828 pci_assign_unassigned_bridge_resources(bridge);
1681 list_for_each_entry(dev, &bus->devices, bus_list)
1682 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
1683 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
1684 if (dev->subordinate)
1685 pci_bus_size_bridges(dev->subordinate);
1686 up_read(&pci_bus_sem);
1687 1829
1688 pci_bus_assign_resources(bus);
1689 pci_enable_bridges(bus);
1690 pci_bus_add_devices(bus); 1830 pci_bus_add_devices(bus);
1691 1831
1692 return max; 1832 return max;
1693} 1833}
1694EXPORT_SYMBOL_GPL(pci_rescan_bus);
1695 1834
1696EXPORT_SYMBOL(pci_add_new_bus); 1835EXPORT_SYMBOL(pci_add_new_bus);
1697EXPORT_SYMBOL(pci_scan_slot); 1836EXPORT_SYMBOL(pci_scan_slot);
diff --git a/drivers/pci/quirks.c b/drivers/pci/quirks.c
index 64765474676f..4bf71028556b 100644
--- a/drivers/pci/quirks.c
+++ b/drivers/pci/quirks.c
@@ -26,73 +26,12 @@
26#include <linux/dmi.h> 26#include <linux/dmi.h>
27#include <linux/pci-aspm.h> 27#include <linux/pci-aspm.h>
28#include <linux/ioport.h> 28#include <linux/ioport.h>
29#include <linux/sched.h>
30#include <linux/ktime.h>
29#include <asm/dma.h> /* isa_dma_bridge_buggy */ 31#include <asm/dma.h> /* isa_dma_bridge_buggy */
30#include "pci.h" 32#include "pci.h"
31 33
32/* 34/*
33 * This quirk function disables memory decoding and releases memory resources
34 * of the device specified by kernel's boot parameter 'pci=resource_alignment='.
35 * It also rounds up size to specified alignment.
36 * Later on, the kernel will assign page-aligned memory resource back
37 * to the device.
38 */
39static void __devinit quirk_resource_alignment(struct pci_dev *dev)
40{
41 int i;
42 struct resource *r;
43 resource_size_t align, size;
44 u16 command;
45
46 if (!pci_is_reassigndev(dev))
47 return;
48
49 if (dev->hdr_type == PCI_HEADER_TYPE_NORMAL &&
50 (dev->class >> 8) == PCI_CLASS_BRIDGE_HOST) {
51 dev_warn(&dev->dev,
52 "Can't reassign resources to host bridge.\n");
53 return;
54 }
55
56 dev_info(&dev->dev,
57 "Disabling memory decoding and releasing memory resources.\n");
58 pci_read_config_word(dev, PCI_COMMAND, &command);
59 command &= ~PCI_COMMAND_MEMORY;
60 pci_write_config_word(dev, PCI_COMMAND, command);
61
62 align = pci_specified_resource_alignment(dev);
63 for (i=0; i < PCI_BRIDGE_RESOURCES; i++) {
64 r = &dev->resource[i];
65 if (!(r->flags & IORESOURCE_MEM))
66 continue;
67 size = resource_size(r);
68 if (size < align) {
69 size = align;
70 dev_info(&dev->dev,
71 "Rounding up size of resource #%d to %#llx.\n",
72 i, (unsigned long long)size);
73 }
74 r->end = size - 1;
75 r->start = 0;
76 }
77 /* Need to disable bridge's resource window,
78 * to enable the kernel to reassign new resource
79 * window later on.
80 */
81 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
82 (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
83 for (i = PCI_BRIDGE_RESOURCES; i < PCI_NUM_RESOURCES; i++) {
84 r = &dev->resource[i];
85 if (!(r->flags & IORESOURCE_MEM))
86 continue;
87 r->end = resource_size(r) - 1;
88 r->start = 0;
89 }
90 pci_disable_bridge_window(dev);
91 }
92}
93DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, quirk_resource_alignment);
94
95/*
96 * Decoding should be disabled for a PCI device during BAR sizing to avoid 35 * Decoding should be disabled for a PCI device during BAR sizing to avoid
97 * conflict. But doing so may cause problems on host bridge and perhaps other 36 * conflict. But doing so may cause problems on host bridge and perhaps other
98 * key system devices. For devices that need to have mmio decoding always-on, 37 * key system devices. For devices that need to have mmio decoding always-on,
@@ -100,10 +39,10 @@ DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, quirk_resource_alignment);
100 */ 39 */
101static void __devinit quirk_mmio_always_on(struct pci_dev *dev) 40static void __devinit quirk_mmio_always_on(struct pci_dev *dev)
102{ 41{
103 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_HOST) 42 dev->mmio_always_on = 1;
104 dev->mmio_always_on = 1;
105} 43}
106DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID, PCI_ANY_ID, quirk_mmio_always_on); 44DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_ANY_ID, PCI_ANY_ID,
45 PCI_CLASS_BRIDGE_HOST, 8, quirk_mmio_always_on);
107 46
108/* The Mellanox Tavor device gives false positive parity errors 47/* The Mellanox Tavor device gives false positive parity errors
109 * Mark this device with a broken_parity_status, to allow 48 * Mark this device with a broken_parity_status, to allow
@@ -1002,12 +941,12 @@ DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C597_0, quirk_vt
1002 */ 941 */
1003static void quirk_cardbus_legacy(struct pci_dev *dev) 942static void quirk_cardbus_legacy(struct pci_dev *dev)
1004{ 943{
1005 if ((PCI_CLASS_BRIDGE_CARDBUS << 8) ^ dev->class)
1006 return;
1007 pci_write_config_dword(dev, PCI_CB_LEGACY_MODE_BASE, 0); 944 pci_write_config_dword(dev, PCI_CB_LEGACY_MODE_BASE, 0);
1008} 945}
1009DECLARE_PCI_FIXUP_FINAL(PCI_ANY_ID, PCI_ANY_ID, quirk_cardbus_legacy); 946DECLARE_PCI_FIXUP_CLASS_FINAL(PCI_ANY_ID, PCI_ANY_ID,
1010DECLARE_PCI_FIXUP_RESUME_EARLY(PCI_ANY_ID, PCI_ANY_ID, quirk_cardbus_legacy); 947 PCI_CLASS_BRIDGE_CARDBUS, 8, quirk_cardbus_legacy);
948DECLARE_PCI_FIXUP_CLASS_RESUME_EARLY(PCI_ANY_ID, PCI_ANY_ID,
949 PCI_CLASS_BRIDGE_CARDBUS, 8, quirk_cardbus_legacy);
1011 950
1012/* 951/*
1013 * Following the PCI ordering rules is optional on the AMD762. I'm not 952 * Following the PCI ordering rules is optional on the AMD762. I'm not
@@ -1164,17 +1103,20 @@ DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_10, qui
1164 1103
1165static void __devinit quirk_no_ata_d3(struct pci_dev *pdev) 1104static void __devinit quirk_no_ata_d3(struct pci_dev *pdev)
1166{ 1105{
1167 /* Quirk the legacy ATA devices only. The AHCI ones are ok */ 1106 pdev->dev_flags |= PCI_DEV_FLAGS_NO_D3;
1168 if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE)
1169 pdev->dev_flags |= PCI_DEV_FLAGS_NO_D3;
1170} 1107}
1171DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_SERVERWORKS, PCI_ANY_ID, quirk_no_ata_d3); 1108/* Quirk the legacy ATA devices only. The AHCI ones are ok */
1172DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_ATI, PCI_ANY_ID, quirk_no_ata_d3); 1109DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_SERVERWORKS, PCI_ANY_ID,
1110 PCI_CLASS_STORAGE_IDE, 8, quirk_no_ata_d3);
1111DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_ATI, PCI_ANY_ID,
1112 PCI_CLASS_STORAGE_IDE, 8, quirk_no_ata_d3);
1173/* ALi loses some register settings that we cannot then restore */ 1113/* ALi loses some register settings that we cannot then restore */
1174DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_AL, PCI_ANY_ID, quirk_no_ata_d3); 1114DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_AL, PCI_ANY_ID,
1115 PCI_CLASS_STORAGE_IDE, 8, quirk_no_ata_d3);
1175/* VIA comes back fine but we need to keep it alive or ACPI GTM failures 1116/* VIA comes back fine but we need to keep it alive or ACPI GTM failures
1176 occur when mode detecting */ 1117 occur when mode detecting */
1177DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_VIA, PCI_ANY_ID, quirk_no_ata_d3); 1118DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_VIA, PCI_ANY_ID,
1119 PCI_CLASS_STORAGE_IDE, 8, quirk_no_ata_d3);
1178 1120
1179/* This was originally an Alpha specific thing, but it really fits here. 1121/* This was originally an Alpha specific thing, but it really fits here.
1180 * The i82375 PCI/EISA bridge appears as non-classified. Fix that. 1122 * The i82375 PCI/EISA bridge appears as non-classified. Fix that.
@@ -1873,8 +1815,7 @@ static void __devinit quirk_netmos(struct pci_dev *dev)
1873 case PCI_DEVICE_ID_NETMOS_9745: 1815 case PCI_DEVICE_ID_NETMOS_9745:
1874 case PCI_DEVICE_ID_NETMOS_9845: 1816 case PCI_DEVICE_ID_NETMOS_9845:
1875 case PCI_DEVICE_ID_NETMOS_9855: 1817 case PCI_DEVICE_ID_NETMOS_9855:
1876 if ((dev->class >> 8) == PCI_CLASS_COMMUNICATION_SERIAL && 1818 if (num_parallel) {
1877 num_parallel) {
1878 dev_info(&dev->dev, "Netmos %04x (%u parallel, " 1819 dev_info(&dev->dev, "Netmos %04x (%u parallel, "
1879 "%u serial); changing class SERIAL to OTHER " 1820 "%u serial); changing class SERIAL to OTHER "
1880 "(use parport_serial)\n", 1821 "(use parport_serial)\n",
@@ -1884,7 +1825,8 @@ static void __devinit quirk_netmos(struct pci_dev *dev)
1884 } 1825 }
1885 } 1826 }
1886} 1827}
1887DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_NETMOS, PCI_ANY_ID, quirk_netmos); 1828DECLARE_PCI_FIXUP_CLASS_HEADER(PCI_VENDOR_ID_NETMOS, PCI_ANY_ID,
1829 PCI_CLASS_COMMUNICATION_SERIAL, 8, quirk_netmos);
1888 1830
1889static void __devinit quirk_e100_interrupt(struct pci_dev *dev) 1831static void __devinit quirk_e100_interrupt(struct pci_dev *dev)
1890{ 1832{
@@ -1952,7 +1894,8 @@ static void __devinit quirk_e100_interrupt(struct pci_dev *dev)
1952 1894
1953 iounmap(csr); 1895 iounmap(csr);
1954} 1896}
1955DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, PCI_ANY_ID, quirk_e100_interrupt); 1897DECLARE_PCI_FIXUP_CLASS_FINAL(PCI_VENDOR_ID_INTEL, PCI_ANY_ID,
1898 PCI_CLASS_NETWORK_ETHERNET, 8, quirk_e100_interrupt);
1956 1899
1957/* 1900/*
1958 * The 82575 and 82598 may experience data corruption issues when transitioning 1901 * The 82575 and 82598 may experience data corruption issues when transitioning
@@ -2161,6 +2104,24 @@ DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_BROADCOM,
2161 PCI_DEVICE_ID_NX2_5709S, 2104 PCI_DEVICE_ID_NX2_5709S,
2162 quirk_brcm_570x_limit_vpd); 2105 quirk_brcm_570x_limit_vpd);
2163 2106
2107static void __devinit quirk_brcm_5719_limit_mrrs(struct pci_dev *dev)
2108{
2109 u32 rev;
2110
2111 pci_read_config_dword(dev, 0xf4, &rev);
2112
2113 /* Only CAP the MRRS if the device is a 5719 A0 */
2114 if (rev == 0x05719000) {
2115 int readrq = pcie_get_readrq(dev);
2116 if (readrq > 2048)
2117 pcie_set_readrq(dev, 2048);
2118 }
2119}
2120
2121DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_BROADCOM,
2122 PCI_DEVICE_ID_TIGON3_5719,
2123 quirk_brcm_5719_limit_mrrs);
2124
2164/* Originally in EDAC sources for i82875P: 2125/* Originally in EDAC sources for i82875P:
2165 * Intel tells BIOS developers to hide device 6 which 2126 * Intel tells BIOS developers to hide device 6 which
2166 * configures the overflow device access containing 2127 * configures the overflow device access containing
@@ -2816,12 +2777,11 @@ DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_INTEL, 0x3c28, vtd_mask_spec_errors);
2816static void __devinit fixup_ti816x_class(struct pci_dev* dev) 2777static void __devinit fixup_ti816x_class(struct pci_dev* dev)
2817{ 2778{
2818 /* TI 816x devices do not have class code set when in PCIe boot mode */ 2779 /* TI 816x devices do not have class code set when in PCIe boot mode */
2819 if (dev->class == PCI_CLASS_NOT_DEFINED) { 2780 dev_info(&dev->dev, "Setting PCI class for 816x PCIe device\n");
2820 dev_info(&dev->dev, "Setting PCI class for 816x PCIe device\n"); 2781 dev->class = PCI_CLASS_MULTIMEDIA_VIDEO;
2821 dev->class = PCI_CLASS_MULTIMEDIA_VIDEO;
2822 }
2823} 2782}
2824DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_TI, 0xb800, fixup_ti816x_class); 2783DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_TI, 0xb800,
2784 PCI_CLASS_NOT_DEFINED, 0, fixup_ti816x_class);
2825 2785
2826/* Some PCIe devices do not work reliably with the claimed maximum 2786/* Some PCIe devices do not work reliably with the claimed maximum
2827 * payload size supported. 2787 * payload size supported.
@@ -2906,17 +2866,73 @@ DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x65f8, quirk_intel_mc_errata);
2906DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x65f9, quirk_intel_mc_errata); 2866DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x65f9, quirk_intel_mc_errata);
2907DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x65fa, quirk_intel_mc_errata); 2867DECLARE_PCI_FIXUP_HEADER(PCI_VENDOR_ID_INTEL, 0x65fa, quirk_intel_mc_errata);
2908 2868
2869
2870static void do_one_fixup_debug(void (*fn)(struct pci_dev *dev), struct pci_dev *dev)
2871{
2872 ktime_t calltime, delta, rettime;
2873 unsigned long long duration;
2874
2875 printk(KERN_DEBUG "calling %pF @ %i for %s\n",
2876 fn, task_pid_nr(current), dev_name(&dev->dev));
2877 calltime = ktime_get();
2878 fn(dev);
2879 rettime = ktime_get();
2880 delta = ktime_sub(rettime, calltime);
2881 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2882 printk(KERN_DEBUG "pci fixup %pF returned after %lld usecs for %s\n",
2883 fn, duration, dev_name(&dev->dev));
2884}
2885
2886/*
2887 * Some BIOS implementations leave the Intel GPU interrupts enabled,
2888 * even though no one is handling them (f.e. i915 driver is never loaded).
2889 * Additionally the interrupt destination is not set up properly
2890 * and the interrupt ends up -somewhere-.
2891 *
2892 * These spurious interrupts are "sticky" and the kernel disables
2893 * the (shared) interrupt line after 100.000+ generated interrupts.
2894 *
2895 * Fix it by disabling the still enabled interrupts.
2896 * This resolves crashes often seen on monitor unplug.
2897 */
2898#define I915_DEIER_REG 0x4400c
2899static void __devinit disable_igfx_irq(struct pci_dev *dev)
2900{
2901 void __iomem *regs = pci_iomap(dev, 0, 0);
2902 if (regs == NULL) {
2903 dev_warn(&dev->dev, "igfx quirk: Can't iomap PCI device\n");
2904 return;
2905 }
2906
2907 /* Check if any interrupt line is still enabled */
2908 if (readl(regs + I915_DEIER_REG) != 0) {
2909 dev_warn(&dev->dev, "BIOS left Intel GPU interrupts enabled; "
2910 "disabling\n");
2911
2912 writel(0, regs + I915_DEIER_REG);
2913 }
2914
2915 pci_iounmap(dev, regs);
2916}
2917DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x0102, disable_igfx_irq);
2918DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x010a, disable_igfx_irq);
2919
2909static void pci_do_fixups(struct pci_dev *dev, struct pci_fixup *f, 2920static void pci_do_fixups(struct pci_dev *dev, struct pci_fixup *f,
2910 struct pci_fixup *end) 2921 struct pci_fixup *end)
2911{ 2922{
2912 while (f < end) { 2923 for (; f < end; f++)
2913 if ((f->vendor == dev->vendor || f->vendor == (u16) PCI_ANY_ID) && 2924 if ((f->class == (u32) (dev->class >> f->class_shift) ||
2914 (f->device == dev->device || f->device == (u16) PCI_ANY_ID)) { 2925 f->class == (u32) PCI_ANY_ID) &&
2926 (f->vendor == dev->vendor ||
2927 f->vendor == (u16) PCI_ANY_ID) &&
2928 (f->device == dev->device ||
2929 f->device == (u16) PCI_ANY_ID)) {
2915 dev_dbg(&dev->dev, "calling %pF\n", f->hook); 2930 dev_dbg(&dev->dev, "calling %pF\n", f->hook);
2916 f->hook(dev); 2931 if (initcall_debug)
2932 do_one_fixup_debug(f->hook, dev);
2933 else
2934 f->hook(dev);
2917 } 2935 }
2918 f++;
2919 }
2920} 2936}
2921 2937
2922extern struct pci_fixup __start_pci_fixups_early[]; 2938extern struct pci_fixup __start_pci_fixups_early[];
diff --git a/drivers/pci/remove.c b/drivers/pci/remove.c
index 6def3624c688..fd77e2bde2e8 100644
--- a/drivers/pci/remove.c
+++ b/drivers/pci/remove.c
@@ -77,8 +77,9 @@ void pci_remove_bus(struct pci_bus *pci_bus)
77} 77}
78EXPORT_SYMBOL(pci_remove_bus); 78EXPORT_SYMBOL(pci_remove_bus);
79 79
80static void __pci_remove_behind_bridge(struct pci_dev *dev);
80/** 81/**
81 * pci_remove_bus_device - remove a PCI device and any children 82 * pci_stop_and_remove_bus_device - remove a PCI device and any children
82 * @dev: the device to remove 83 * @dev: the device to remove
83 * 84 *
84 * Remove a PCI device from the device lists, informing the drivers 85 * Remove a PCI device from the device lists, informing the drivers
@@ -89,46 +90,72 @@ EXPORT_SYMBOL(pci_remove_bus);
89 * device lists, remove the /proc entry, and notify userspace 90 * device lists, remove the /proc entry, and notify userspace
90 * (/sbin/hotplug). 91 * (/sbin/hotplug).
91 */ 92 */
92static void __pci_remove_bus_device(struct pci_dev *dev) 93void __pci_remove_bus_device(struct pci_dev *dev)
93{ 94{
94 if (dev->subordinate) { 95 if (dev->subordinate) {
95 struct pci_bus *b = dev->subordinate; 96 struct pci_bus *b = dev->subordinate;
96 97
97 pci_remove_behind_bridge(dev); 98 __pci_remove_behind_bridge(dev);
98 pci_remove_bus(b); 99 pci_remove_bus(b);
99 dev->subordinate = NULL; 100 dev->subordinate = NULL;
100 } 101 }
101 102
102 pci_destroy_dev(dev); 103 pci_destroy_dev(dev);
103} 104}
104void pci_remove_bus_device(struct pci_dev *dev) 105EXPORT_SYMBOL(__pci_remove_bus_device);
106
107void pci_stop_and_remove_bus_device(struct pci_dev *dev)
105{ 108{
106 pci_stop_bus_device(dev); 109 pci_stop_bus_device(dev);
107 __pci_remove_bus_device(dev); 110 __pci_remove_bus_device(dev);
108} 111}
109 112
113static void __pci_remove_behind_bridge(struct pci_dev *dev)
114{
115 struct list_head *l, *n;
116
117 if (dev->subordinate)
118 list_for_each_safe(l, n, &dev->subordinate->devices)
119 __pci_remove_bus_device(pci_dev_b(l));
120}
121
122static void pci_stop_behind_bridge(struct pci_dev *dev)
123{
124 struct list_head *l, *n;
125
126 if (dev->subordinate)
127 list_for_each_safe(l, n, &dev->subordinate->devices)
128 pci_stop_bus_device(pci_dev_b(l));
129}
130
110/** 131/**
111 * pci_remove_behind_bridge - remove all devices behind a PCI bridge 132 * pci_stop_and_remove_behind_bridge - stop and remove all devices behind
133 * a PCI bridge
112 * @dev: PCI bridge device 134 * @dev: PCI bridge device
113 * 135 *
114 * Remove all devices on the bus, except for the parent bridge. 136 * Remove all devices on the bus, except for the parent bridge.
115 * This also removes any child buses, and any devices they may 137 * This also removes any child buses, and any devices they may
116 * contain in a depth-first manner. 138 * contain in a depth-first manner.
117 */ 139 */
118void pci_remove_behind_bridge(struct pci_dev *dev) 140void pci_stop_and_remove_behind_bridge(struct pci_dev *dev)
119{ 141{
120 struct list_head *l, *n; 142 pci_stop_behind_bridge(dev);
121 143 __pci_remove_behind_bridge(dev);
122 if (dev->subordinate)
123 list_for_each_safe(l, n, &dev->subordinate->devices)
124 __pci_remove_bus_device(pci_dev_b(l));
125} 144}
126 145
127static void pci_stop_bus_devices(struct pci_bus *bus) 146static void pci_stop_bus_devices(struct pci_bus *bus)
128{ 147{
129 struct list_head *l, *n; 148 struct list_head *l, *n;
130 149
131 list_for_each_safe(l, n, &bus->devices) { 150 /*
151 * VFs could be removed by pci_stop_and_remove_bus_device() in the
152 * pci_stop_bus_devices() code path for PF.
153 * aka, bus->devices get updated in the process.
154 * but VFs are inserted after PFs when SRIOV is enabled for PF,
155 * We can iterate the list backwards to get prev valid PF instead
156 * of removed VF.
157 */
158 list_for_each_prev_safe(l, n, &bus->devices) {
132 struct pci_dev *dev = pci_dev_b(l); 159 struct pci_dev *dev = pci_dev_b(l);
133 pci_stop_bus_device(dev); 160 pci_stop_bus_device(dev);
134 } 161 }
@@ -150,6 +177,6 @@ void pci_stop_bus_device(struct pci_dev *dev)
150 pci_stop_dev(dev); 177 pci_stop_dev(dev);
151} 178}
152 179
153EXPORT_SYMBOL(pci_remove_bus_device); 180EXPORT_SYMBOL(pci_stop_and_remove_bus_device);
154EXPORT_SYMBOL(pci_remove_behind_bridge); 181EXPORT_SYMBOL(pci_stop_and_remove_behind_bridge);
155EXPORT_SYMBOL_GPL(pci_stop_bus_device); 182EXPORT_SYMBOL_GPL(pci_stop_bus_device);
diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
index 86b69f85f900..8fa2d4be88de 100644
--- a/drivers/pci/setup-bus.c
+++ b/drivers/pci/setup-bus.c
@@ -25,10 +25,13 @@
25#include <linux/ioport.h> 25#include <linux/ioport.h>
26#include <linux/cache.h> 26#include <linux/cache.h>
27#include <linux/slab.h> 27#include <linux/slab.h>
28#include <asm-generic/pci-bridge.h>
28#include "pci.h" 29#include "pci.h"
29 30
30struct resource_list_x { 31unsigned int pci_flags;
31 struct resource_list_x *next; 32
33struct pci_dev_resource {
34 struct list_head list;
32 struct resource *res; 35 struct resource *res;
33 struct pci_dev *dev; 36 struct pci_dev *dev;
34 resource_size_t start; 37 resource_size_t start;
@@ -38,21 +41,14 @@ struct resource_list_x {
38 unsigned long flags; 41 unsigned long flags;
39}; 42};
40 43
41#define free_list(type, head) do { \ 44static void free_list(struct list_head *head)
42 struct type *list, *tmp; \
43 for (list = (head)->next; list;) { \
44 tmp = list; \
45 list = list->next; \
46 kfree(tmp); \
47 } \
48 (head)->next = NULL; \
49} while (0)
50
51int pci_realloc_enable = 0;
52#define pci_realloc_enabled() pci_realloc_enable
53void pci_realloc(void)
54{ 45{
55 pci_realloc_enable = 1; 46 struct pci_dev_resource *dev_res, *tmp;
47
48 list_for_each_entry_safe(dev_res, tmp, head, list) {
49 list_del(&dev_res->list);
50 kfree(dev_res);
51 }
56} 52}
57 53
58/** 54/**
@@ -64,21 +60,18 @@ void pci_realloc(void)
64 * @add_size: additional size to be optionally added 60 * @add_size: additional size to be optionally added
65 * to the resource 61 * to the resource
66 */ 62 */
67static void add_to_list(struct resource_list_x *head, 63static int add_to_list(struct list_head *head,
68 struct pci_dev *dev, struct resource *res, 64 struct pci_dev *dev, struct resource *res,
69 resource_size_t add_size, resource_size_t min_align) 65 resource_size_t add_size, resource_size_t min_align)
70{ 66{
71 struct resource_list_x *list = head; 67 struct pci_dev_resource *tmp;
72 struct resource_list_x *ln = list->next;
73 struct resource_list_x *tmp;
74 68
75 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL); 69 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
76 if (!tmp) { 70 if (!tmp) {
77 pr_warning("add_to_list: kmalloc() failed!\n"); 71 pr_warning("add_to_list: kmalloc() failed!\n");
78 return; 72 return -ENOMEM;
79 } 73 }
80 74
81 tmp->next = ln;
82 tmp->res = res; 75 tmp->res = res;
83 tmp->dev = dev; 76 tmp->dev = dev;
84 tmp->start = res->start; 77 tmp->start = res->start;
@@ -86,19 +79,100 @@ static void add_to_list(struct resource_list_x *head,
86 tmp->flags = res->flags; 79 tmp->flags = res->flags;
87 tmp->add_size = add_size; 80 tmp->add_size = add_size;
88 tmp->min_align = min_align; 81 tmp->min_align = min_align;
89 list->next = tmp; 82
83 list_add(&tmp->list, head);
84
85 return 0;
90} 86}
91 87
92static void add_to_failed_list(struct resource_list_x *head, 88static void remove_from_list(struct list_head *head,
93 struct pci_dev *dev, struct resource *res) 89 struct resource *res)
94{ 90{
95 add_to_list(head, dev, res, 91 struct pci_dev_resource *dev_res, *tmp;
96 0 /* dont care */, 92
97 0 /* dont care */); 93 list_for_each_entry_safe(dev_res, tmp, head, list) {
94 if (dev_res->res == res) {
95 list_del(&dev_res->list);
96 kfree(dev_res);
97 break;
98 }
99 }
100}
101
102static resource_size_t get_res_add_size(struct list_head *head,
103 struct resource *res)
104{
105 struct pci_dev_resource *dev_res;
106
107 list_for_each_entry(dev_res, head, list) {
108 if (dev_res->res == res) {
109 int idx = res - &dev_res->dev->resource[0];
110
111 dev_printk(KERN_DEBUG, &dev_res->dev->dev,
112 "res[%d]=%pR get_res_add_size add_size %llx\n",
113 idx, dev_res->res,
114 (unsigned long long)dev_res->add_size);
115
116 return dev_res->add_size;
117 }
118 }
119
120 return 0;
121}
122
123/* Sort resources by alignment */
124static void pdev_sort_resources(struct pci_dev *dev, struct list_head *head)
125{
126 int i;
127
128 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
129 struct resource *r;
130 struct pci_dev_resource *dev_res, *tmp;
131 resource_size_t r_align;
132 struct list_head *n;
133
134 r = &dev->resource[i];
135
136 if (r->flags & IORESOURCE_PCI_FIXED)
137 continue;
138
139 if (!(r->flags) || r->parent)
140 continue;
141
142 r_align = pci_resource_alignment(dev, r);
143 if (!r_align) {
144 dev_warn(&dev->dev, "BAR %d: %pR has bogus alignment\n",
145 i, r);
146 continue;
147 }
148
149 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
150 if (!tmp)
151 panic("pdev_sort_resources(): "
152 "kmalloc() failed!\n");
153 tmp->res = r;
154 tmp->dev = dev;
155
156 /* fallback is smallest one or list is empty*/
157 n = head;
158 list_for_each_entry(dev_res, head, list) {
159 resource_size_t align;
160
161 align = pci_resource_alignment(dev_res->dev,
162 dev_res->res);
163
164 if (r_align > align) {
165 n = &dev_res->list;
166 break;
167 }
168 }
169 /* Insert it just before n*/
170 list_add_tail(&tmp->list, n);
171 }
98} 172}
99 173
100static void __dev_sort_resources(struct pci_dev *dev, 174static void __dev_sort_resources(struct pci_dev *dev,
101 struct resource_list *head) 175 struct list_head *head)
102{ 176{
103 u16 class = dev->class >> 8; 177 u16 class = dev->class >> 8;
104 178
@@ -136,49 +210,54 @@ static inline void reset_resource(struct resource *res)
136 * additional resources for the element, provided the element 210 * additional resources for the element, provided the element
137 * is in the head list. 211 * is in the head list.
138 */ 212 */
139static void reassign_resources_sorted(struct resource_list_x *realloc_head, 213static void reassign_resources_sorted(struct list_head *realloc_head,
140 struct resource_list *head) 214 struct list_head *head)
141{ 215{
142 struct resource *res; 216 struct resource *res;
143 struct resource_list_x *list, *tmp, *prev; 217 struct pci_dev_resource *add_res, *tmp;
144 struct resource_list *hlist; 218 struct pci_dev_resource *dev_res;
145 resource_size_t add_size; 219 resource_size_t add_size;
146 int idx; 220 int idx;
147 221
148 prev = realloc_head; 222 list_for_each_entry_safe(add_res, tmp, realloc_head, list) {
149 for (list = realloc_head->next; list;) { 223 bool found_match = false;
150 res = list->res; 224
225 res = add_res->res;
151 /* skip resource that has been reset */ 226 /* skip resource that has been reset */
152 if (!res->flags) 227 if (!res->flags)
153 goto out; 228 goto out;
154 229
155 /* skip this resource if not found in head list */ 230 /* skip this resource if not found in head list */
156 for (hlist = head->next; hlist && hlist->res != res; 231 list_for_each_entry(dev_res, head, list) {
157 hlist = hlist->next); 232 if (dev_res->res == res) {
158 if (!hlist) { /* just skip */ 233 found_match = true;
159 prev = list; 234 break;
160 list = list->next; 235 }
161 continue;
162 } 236 }
237 if (!found_match)/* just skip */
238 continue;
163 239
164 idx = res - &list->dev->resource[0]; 240 idx = res - &add_res->dev->resource[0];
165 add_size=list->add_size; 241 add_size = add_res->add_size;
166 if (!resource_size(res)) { 242 if (!resource_size(res)) {
167 res->start = list->start; 243 res->start = add_res->start;
168 res->end = res->start + add_size - 1; 244 res->end = res->start + add_size - 1;
169 if(pci_assign_resource(list->dev, idx)) 245 if (pci_assign_resource(add_res->dev, idx))
170 reset_resource(res); 246 reset_resource(res);
171 } else { 247 } else {
172 resource_size_t align = list->min_align; 248 resource_size_t align = add_res->min_align;
173 res->flags |= list->flags & (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN); 249 res->flags |= add_res->flags &
174 if (pci_reassign_resource(list->dev, idx, add_size, align)) 250 (IORESOURCE_STARTALIGN|IORESOURCE_SIZEALIGN);
175 dev_printk(KERN_DEBUG, &list->dev->dev, "failed to add optional resources res=%pR\n", 251 if (pci_reassign_resource(add_res->dev, idx,
176 res); 252 add_size, align))
253 dev_printk(KERN_DEBUG, &add_res->dev->dev,
254 "failed to add %llx res[%d]=%pR\n",
255 (unsigned long long)add_size,
256 idx, res);
177 } 257 }
178out: 258out:
179 tmp = list; 259 list_del(&add_res->list);
180 prev->next = list = list->next; 260 kfree(add_res);
181 kfree(tmp);
182 } 261 }
183} 262}
184 263
@@ -192,35 +271,99 @@ out:
192 * Satisfy resource requests of each element in the list. Add 271 * Satisfy resource requests of each element in the list. Add
193 * requests that could not satisfied to the failed_list. 272 * requests that could not satisfied to the failed_list.
194 */ 273 */
195static void assign_requested_resources_sorted(struct resource_list *head, 274static void assign_requested_resources_sorted(struct list_head *head,
196 struct resource_list_x *fail_head) 275 struct list_head *fail_head)
197{ 276{
198 struct resource *res; 277 struct resource *res;
199 struct resource_list *list; 278 struct pci_dev_resource *dev_res;
200 int idx; 279 int idx;
201 280
202 for (list = head->next; list; list = list->next) { 281 list_for_each_entry(dev_res, head, list) {
203 res = list->res; 282 res = dev_res->res;
204 idx = res - &list->dev->resource[0]; 283 idx = res - &dev_res->dev->resource[0];
205 if (resource_size(res) && pci_assign_resource(list->dev, idx)) { 284 if (resource_size(res) &&
206 if (fail_head && !pci_is_root_bus(list->dev->bus)) { 285 pci_assign_resource(dev_res->dev, idx)) {
286 if (fail_head && !pci_is_root_bus(dev_res->dev->bus)) {
207 /* 287 /*
208 * if the failed res is for ROM BAR, and it will 288 * if the failed res is for ROM BAR, and it will
209 * be enabled later, don't add it to the list 289 * be enabled later, don't add it to the list
210 */ 290 */
211 if (!((idx == PCI_ROM_RESOURCE) && 291 if (!((idx == PCI_ROM_RESOURCE) &&
212 (!(res->flags & IORESOURCE_ROM_ENABLE)))) 292 (!(res->flags & IORESOURCE_ROM_ENABLE))))
213 add_to_failed_list(fail_head, list->dev, res); 293 add_to_list(fail_head,
294 dev_res->dev, res,
295 0 /* dont care */,
296 0 /* dont care */);
214 } 297 }
215 reset_resource(res); 298 reset_resource(res);
216 } 299 }
217 } 300 }
218} 301}
219 302
220static void __assign_resources_sorted(struct resource_list *head, 303static void __assign_resources_sorted(struct list_head *head,
221 struct resource_list_x *realloc_head, 304 struct list_head *realloc_head,
222 struct resource_list_x *fail_head) 305 struct list_head *fail_head)
223{ 306{
307 /*
308 * Should not assign requested resources at first.
309 * they could be adjacent, so later reassign can not reallocate
310 * them one by one in parent resource window.
311 * Try to assign requested + add_size at begining
312 * if could do that, could get out early.
313 * if could not do that, we still try to assign requested at first,
314 * then try to reassign add_size for some resources.
315 */
316 LIST_HEAD(save_head);
317 LIST_HEAD(local_fail_head);
318 struct pci_dev_resource *save_res;
319 struct pci_dev_resource *dev_res;
320
321 /* Check if optional add_size is there */
322 if (!realloc_head || list_empty(realloc_head))
323 goto requested_and_reassign;
324
325 /* Save original start, end, flags etc at first */
326 list_for_each_entry(dev_res, head, list) {
327 if (add_to_list(&save_head, dev_res->dev, dev_res->res, 0, 0)) {
328 free_list(&save_head);
329 goto requested_and_reassign;
330 }
331 }
332
333 /* Update res in head list with add_size in realloc_head list */
334 list_for_each_entry(dev_res, head, list)
335 dev_res->res->end += get_res_add_size(realloc_head,
336 dev_res->res);
337
338 /* Try updated head list with add_size added */
339 assign_requested_resources_sorted(head, &local_fail_head);
340
341 /* all assigned with add_size ? */
342 if (list_empty(&local_fail_head)) {
343 /* Remove head list from realloc_head list */
344 list_for_each_entry(dev_res, head, list)
345 remove_from_list(realloc_head, dev_res->res);
346 free_list(&save_head);
347 free_list(head);
348 return;
349 }
350
351 free_list(&local_fail_head);
352 /* Release assigned resource */
353 list_for_each_entry(dev_res, head, list)
354 if (dev_res->res->parent)
355 release_resource(dev_res->res);
356 /* Restore start/end/flags from saved list */
357 list_for_each_entry(save_res, &save_head, list) {
358 struct resource *res = save_res->res;
359
360 res->start = save_res->start;
361 res->end = save_res->end;
362 res->flags = save_res->flags;
363 }
364 free_list(&save_head);
365
366requested_and_reassign:
224 /* Satisfy the must-have resource requests */ 367 /* Satisfy the must-have resource requests */
225 assign_requested_resources_sorted(head, fail_head); 368 assign_requested_resources_sorted(head, fail_head);
226 369
@@ -228,28 +371,27 @@ static void __assign_resources_sorted(struct resource_list *head,
228 requests */ 371 requests */
229 if (realloc_head) 372 if (realloc_head)
230 reassign_resources_sorted(realloc_head, head); 373 reassign_resources_sorted(realloc_head, head);
231 free_list(resource_list, head); 374 free_list(head);
232} 375}
233 376
234static void pdev_assign_resources_sorted(struct pci_dev *dev, 377static void pdev_assign_resources_sorted(struct pci_dev *dev,
235 struct resource_list_x *fail_head) 378 struct list_head *add_head,
379 struct list_head *fail_head)
236{ 380{
237 struct resource_list head; 381 LIST_HEAD(head);
238 382
239 head.next = NULL;
240 __dev_sort_resources(dev, &head); 383 __dev_sort_resources(dev, &head);
241 __assign_resources_sorted(&head, NULL, fail_head); 384 __assign_resources_sorted(&head, add_head, fail_head);
242 385
243} 386}
244 387
245static void pbus_assign_resources_sorted(const struct pci_bus *bus, 388static void pbus_assign_resources_sorted(const struct pci_bus *bus,
246 struct resource_list_x *realloc_head, 389 struct list_head *realloc_head,
247 struct resource_list_x *fail_head) 390 struct list_head *fail_head)
248{ 391{
249 struct pci_dev *dev; 392 struct pci_dev *dev;
250 struct resource_list head; 393 LIST_HEAD(head);
251 394
252 head.next = NULL;
253 list_for_each_entry(dev, &bus->devices, bus_list) 395 list_for_each_entry(dev, &bus->devices, bus_list)
254 __dev_sort_resources(dev, &head); 396 __dev_sort_resources(dev, &head);
255 397
@@ -548,20 +690,6 @@ static resource_size_t calculate_memsize(resource_size_t size,
548 return size; 690 return size;
549} 691}
550 692
551static resource_size_t get_res_add_size(struct resource_list_x *realloc_head,
552 struct resource *res)
553{
554 struct resource_list_x *list;
555
556 /* check if it is in realloc_head list */
557 for (list = realloc_head->next; list && list->res != res;
558 list = list->next);
559 if (list)
560 return list->add_size;
561
562 return 0;
563}
564
565/** 693/**
566 * pbus_size_io() - size the io window of a given bus 694 * pbus_size_io() - size the io window of a given bus
567 * 695 *
@@ -576,7 +704,7 @@ static resource_size_t get_res_add_size(struct resource_list_x *realloc_head,
576 * We must be careful with the ISA aliasing though. 704 * We must be careful with the ISA aliasing though.
577 */ 705 */
578static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size, 706static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
579 resource_size_t add_size, struct resource_list_x *realloc_head) 707 resource_size_t add_size, struct list_head *realloc_head)
580{ 708{
581 struct pci_dev *dev; 709 struct pci_dev *dev;
582 struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO); 710 struct resource *b_res = find_free_bus_resource(bus, IORESOURCE_IO);
@@ -612,7 +740,7 @@ static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
612 if (children_add_size > add_size) 740 if (children_add_size > add_size)
613 add_size = children_add_size; 741 add_size = children_add_size;
614 size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 : 742 size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
615 calculate_iosize(size, min_size+add_size, size1, 743 calculate_iosize(size, min_size, add_size + size1,
616 resource_size(b_res), 4096); 744 resource_size(b_res), 4096);
617 if (!size0 && !size1) { 745 if (!size0 && !size1) {
618 if (b_res->start || b_res->end) 746 if (b_res->start || b_res->end)
@@ -626,8 +754,12 @@ static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
626 b_res->start = 4096; 754 b_res->start = 4096;
627 b_res->end = b_res->start + size0 - 1; 755 b_res->end = b_res->start + size0 - 1;
628 b_res->flags |= IORESOURCE_STARTALIGN; 756 b_res->flags |= IORESOURCE_STARTALIGN;
629 if (size1 > size0 && realloc_head) 757 if (size1 > size0 && realloc_head) {
630 add_to_list(realloc_head, bus->self, b_res, size1-size0, 4096); 758 add_to_list(realloc_head, bus->self, b_res, size1-size0, 4096);
759 dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window "
760 "%pR to [bus %02x-%02x] add_size %lx\n", b_res,
761 bus->secondary, bus->subordinate, size1-size0);
762 }
631} 763}
632 764
633/** 765/**
@@ -644,7 +776,7 @@ static void pbus_size_io(struct pci_bus *bus, resource_size_t min_size,
644static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, 776static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
645 unsigned long type, resource_size_t min_size, 777 unsigned long type, resource_size_t min_size,
646 resource_size_t add_size, 778 resource_size_t add_size,
647 struct resource_list_x *realloc_head) 779 struct list_head *realloc_head)
648{ 780{
649 struct pci_dev *dev; 781 struct pci_dev *dev;
650 resource_size_t min_align, align, size, size0, size1; 782 resource_size_t min_align, align, size, size0, size1;
@@ -726,7 +858,7 @@ static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
726 if (children_add_size > add_size) 858 if (children_add_size > add_size)
727 add_size = children_add_size; 859 add_size = children_add_size;
728 size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 : 860 size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 :
729 calculate_memsize(size, min_size+add_size, 0, 861 calculate_memsize(size, min_size, add_size,
730 resource_size(b_res), min_align); 862 resource_size(b_res), min_align);
731 if (!size0 && !size1) { 863 if (!size0 && !size1) {
732 if (b_res->start || b_res->end) 864 if (b_res->start || b_res->end)
@@ -739,8 +871,12 @@ static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
739 b_res->start = min_align; 871 b_res->start = min_align;
740 b_res->end = size0 + min_align - 1; 872 b_res->end = size0 + min_align - 1;
741 b_res->flags |= IORESOURCE_STARTALIGN | mem64_mask; 873 b_res->flags |= IORESOURCE_STARTALIGN | mem64_mask;
742 if (size1 > size0 && realloc_head) 874 if (size1 > size0 && realloc_head) {
743 add_to_list(realloc_head, bus->self, b_res, size1-size0, min_align); 875 add_to_list(realloc_head, bus->self, b_res, size1-size0, min_align);
876 dev_printk(KERN_DEBUG, &bus->self->dev, "bridge window "
877 "%pR to [bus %02x-%02x] add_size %llx\n", b_res,
878 bus->secondary, bus->subordinate, (unsigned long long)size1-size0);
879 }
744 return 1; 880 return 1;
745} 881}
746 882
@@ -754,25 +890,48 @@ unsigned long pci_cardbus_resource_alignment(struct resource *res)
754} 890}
755 891
756static void pci_bus_size_cardbus(struct pci_bus *bus, 892static void pci_bus_size_cardbus(struct pci_bus *bus,
757 struct resource_list_x *realloc_head) 893 struct list_head *realloc_head)
758{ 894{
759 struct pci_dev *bridge = bus->self; 895 struct pci_dev *bridge = bus->self;
760 struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES]; 896 struct resource *b_res = &bridge->resource[PCI_BRIDGE_RESOURCES];
897 resource_size_t b_res_3_size = pci_cardbus_mem_size * 2;
761 u16 ctrl; 898 u16 ctrl;
762 899
900 if (b_res[0].parent)
901 goto handle_b_res_1;
763 /* 902 /*
764 * Reserve some resources for CardBus. We reserve 903 * Reserve some resources for CardBus. We reserve
765 * a fixed amount of bus space for CardBus bridges. 904 * a fixed amount of bus space for CardBus bridges.
766 */ 905 */
767 b_res[0].start = 0; 906 b_res[0].start = pci_cardbus_io_size;
768 b_res[0].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN; 907 b_res[0].end = b_res[0].start + pci_cardbus_io_size - 1;
769 if (realloc_head) 908 b_res[0].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
770 add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size, 0 /* dont care */); 909 if (realloc_head) {
910 b_res[0].end -= pci_cardbus_io_size;
911 add_to_list(realloc_head, bridge, b_res, pci_cardbus_io_size,
912 pci_cardbus_io_size);
913 }
771 914
772 b_res[1].start = 0; 915handle_b_res_1:
773 b_res[1].flags |= IORESOURCE_IO | IORESOURCE_SIZEALIGN; 916 if (b_res[1].parent)
774 if (realloc_head) 917 goto handle_b_res_2;
775 add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size, 0 /* dont care */); 918 b_res[1].start = pci_cardbus_io_size;
919 b_res[1].end = b_res[1].start + pci_cardbus_io_size - 1;
920 b_res[1].flags |= IORESOURCE_IO | IORESOURCE_STARTALIGN;
921 if (realloc_head) {
922 b_res[1].end -= pci_cardbus_io_size;
923 add_to_list(realloc_head, bridge, b_res+1, pci_cardbus_io_size,
924 pci_cardbus_io_size);
925 }
926
927handle_b_res_2:
928 /* MEM1 must not be pref mmio */
929 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
930 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM1) {
931 ctrl &= ~PCI_CB_BRIDGE_CTL_PREFETCH_MEM1;
932 pci_write_config_word(bridge, PCI_CB_BRIDGE_CONTROL, ctrl);
933 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
934 }
776 935
777 /* 936 /*
778 * Check whether prefetchable memory is supported 937 * Check whether prefetchable memory is supported
@@ -785,38 +944,46 @@ static void pci_bus_size_cardbus(struct pci_bus *bus,
785 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl); 944 pci_read_config_word(bridge, PCI_CB_BRIDGE_CONTROL, &ctrl);
786 } 945 }
787 946
947 if (b_res[2].parent)
948 goto handle_b_res_3;
788 /* 949 /*
789 * If we have prefetchable memory support, allocate 950 * If we have prefetchable memory support, allocate
790 * two regions. Otherwise, allocate one region of 951 * two regions. Otherwise, allocate one region of
791 * twice the size. 952 * twice the size.
792 */ 953 */
793 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) { 954 if (ctrl & PCI_CB_BRIDGE_CTL_PREFETCH_MEM0) {
794 b_res[2].start = 0; 955 b_res[2].start = pci_cardbus_mem_size;
795 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH | IORESOURCE_SIZEALIGN; 956 b_res[2].end = b_res[2].start + pci_cardbus_mem_size - 1;
796 if (realloc_head) 957 b_res[2].flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH |
797 add_to_list(realloc_head, bridge, b_res+2, pci_cardbus_mem_size, 0 /* dont care */); 958 IORESOURCE_STARTALIGN;
798 959 if (realloc_head) {
799 b_res[3].start = 0; 960 b_res[2].end -= pci_cardbus_mem_size;
800 b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN; 961 add_to_list(realloc_head, bridge, b_res+2,
801 if (realloc_head) 962 pci_cardbus_mem_size, pci_cardbus_mem_size);
802 add_to_list(realloc_head, bridge, b_res+3, pci_cardbus_mem_size, 0 /* dont care */); 963 }
803 } else { 964
804 b_res[3].start = 0; 965 /* reduce that to half */
805 b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_SIZEALIGN; 966 b_res_3_size = pci_cardbus_mem_size;
806 if (realloc_head) 967 }
807 add_to_list(realloc_head, bridge, b_res+3, pci_cardbus_mem_size * 2, 0 /* dont care */); 968
969handle_b_res_3:
970 if (b_res[3].parent)
971 goto handle_done;
972 b_res[3].start = pci_cardbus_mem_size;
973 b_res[3].end = b_res[3].start + b_res_3_size - 1;
974 b_res[3].flags |= IORESOURCE_MEM | IORESOURCE_STARTALIGN;
975 if (realloc_head) {
976 b_res[3].end -= b_res_3_size;
977 add_to_list(realloc_head, bridge, b_res+3, b_res_3_size,
978 pci_cardbus_mem_size);
808 } 979 }
809 980
810 /* set the size of the resource to zero, so that the resource does not 981handle_done:
811 * get assigned during required-resource allocation cycle but gets assigned 982 ;
812 * during the optional-resource allocation cycle.
813 */
814 b_res[0].start = b_res[1].start = b_res[2].start = b_res[3].start = 1;
815 b_res[0].end = b_res[1].end = b_res[2].end = b_res[3].end = 0;
816} 983}
817 984
818void __ref __pci_bus_size_bridges(struct pci_bus *bus, 985void __ref __pci_bus_size_bridges(struct pci_bus *bus,
819 struct resource_list_x *realloc_head) 986 struct list_head *realloc_head)
820{ 987{
821 struct pci_dev *dev; 988 struct pci_dev *dev;
822 unsigned long mask, prefmask; 989 unsigned long mask, prefmask;
@@ -858,7 +1025,8 @@ void __ref __pci_bus_size_bridges(struct pci_bus *bus,
858 * Follow thru 1025 * Follow thru
859 */ 1026 */
860 default: 1027 default:
861 pbus_size_io(bus, 0, additional_io_size, realloc_head); 1028 pbus_size_io(bus, realloc_head ? 0 : additional_io_size,
1029 additional_io_size, realloc_head);
862 /* If the bridge supports prefetchable range, size it 1030 /* If the bridge supports prefetchable range, size it
863 separately. If it doesn't, or its prefetchable window 1031 separately. If it doesn't, or its prefetchable window
864 has already been allocated by arch code, try 1032 has already been allocated by arch code, try
@@ -866,11 +1034,15 @@ void __ref __pci_bus_size_bridges(struct pci_bus *bus,
866 resources. */ 1034 resources. */
867 mask = IORESOURCE_MEM; 1035 mask = IORESOURCE_MEM;
868 prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH; 1036 prefmask = IORESOURCE_MEM | IORESOURCE_PREFETCH;
869 if (pbus_size_mem(bus, prefmask, prefmask, 0, additional_mem_size, realloc_head)) 1037 if (pbus_size_mem(bus, prefmask, prefmask,
1038 realloc_head ? 0 : additional_mem_size,
1039 additional_mem_size, realloc_head))
870 mask = prefmask; /* Success, size non-prefetch only. */ 1040 mask = prefmask; /* Success, size non-prefetch only. */
871 else 1041 else
872 additional_mem_size += additional_mem_size; 1042 additional_mem_size += additional_mem_size;
873 pbus_size_mem(bus, mask, IORESOURCE_MEM, 0, additional_mem_size, realloc_head); 1043 pbus_size_mem(bus, mask, IORESOURCE_MEM,
1044 realloc_head ? 0 : additional_mem_size,
1045 additional_mem_size, realloc_head);
874 break; 1046 break;
875 } 1047 }
876} 1048}
@@ -882,8 +1054,8 @@ void __ref pci_bus_size_bridges(struct pci_bus *bus)
882EXPORT_SYMBOL(pci_bus_size_bridges); 1054EXPORT_SYMBOL(pci_bus_size_bridges);
883 1055
884static void __ref __pci_bus_assign_resources(const struct pci_bus *bus, 1056static void __ref __pci_bus_assign_resources(const struct pci_bus *bus,
885 struct resource_list_x *realloc_head, 1057 struct list_head *realloc_head,
886 struct resource_list_x *fail_head) 1058 struct list_head *fail_head)
887{ 1059{
888 struct pci_bus *b; 1060 struct pci_bus *b;
889 struct pci_dev *dev; 1061 struct pci_dev *dev;
@@ -922,17 +1094,19 @@ void __ref pci_bus_assign_resources(const struct pci_bus *bus)
922EXPORT_SYMBOL(pci_bus_assign_resources); 1094EXPORT_SYMBOL(pci_bus_assign_resources);
923 1095
924static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge, 1096static void __ref __pci_bridge_assign_resources(const struct pci_dev *bridge,
925 struct resource_list_x *fail_head) 1097 struct list_head *add_head,
1098 struct list_head *fail_head)
926{ 1099{
927 struct pci_bus *b; 1100 struct pci_bus *b;
928 1101
929 pdev_assign_resources_sorted((struct pci_dev *)bridge, fail_head); 1102 pdev_assign_resources_sorted((struct pci_dev *)bridge,
1103 add_head, fail_head);
930 1104
931 b = bridge->subordinate; 1105 b = bridge->subordinate;
932 if (!b) 1106 if (!b)
933 return; 1107 return;
934 1108
935 __pci_bus_assign_resources(b, NULL, fail_head); 1109 __pci_bus_assign_resources(b, add_head, fail_head);
936 1110
937 switch (bridge->class >> 8) { 1111 switch (bridge->class >> 8) {
938 case PCI_CLASS_BRIDGE_PCI: 1112 case PCI_CLASS_BRIDGE_PCI:
@@ -1095,6 +1269,58 @@ static int __init pci_get_max_depth(void)
1095 return depth; 1269 return depth;
1096} 1270}
1097 1271
1272/*
1273 * -1: undefined, will auto detect later
1274 * 0: disabled by user
1275 * 1: disabled by auto detect
1276 * 2: enabled by user
1277 * 3: enabled by auto detect
1278 */
1279enum enable_type {
1280 undefined = -1,
1281 user_disabled,
1282 auto_disabled,
1283 user_enabled,
1284 auto_enabled,
1285};
1286
1287static enum enable_type pci_realloc_enable __initdata = undefined;
1288void __init pci_realloc_get_opt(char *str)
1289{
1290 if (!strncmp(str, "off", 3))
1291 pci_realloc_enable = user_disabled;
1292 else if (!strncmp(str, "on", 2))
1293 pci_realloc_enable = user_enabled;
1294}
1295static bool __init pci_realloc_enabled(void)
1296{
1297 return pci_realloc_enable >= user_enabled;
1298}
1299
1300static void __init pci_realloc_detect(void)
1301{
1302#if defined(CONFIG_PCI_IOV) && defined(CONFIG_PCI_REALLOC_ENABLE_AUTO)
1303 struct pci_dev *dev = NULL;
1304
1305 if (pci_realloc_enable != undefined)
1306 return;
1307
1308 for_each_pci_dev(dev) {
1309 int i;
1310
1311 for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++) {
1312 struct resource *r = &dev->resource[i];
1313
1314 /* Not assigned, or rejected by kernel ? */
1315 if (r->flags && !r->start) {
1316 pci_realloc_enable = auto_enabled;
1317
1318 return;
1319 }
1320 }
1321 }
1322#endif
1323}
1098 1324
1099/* 1325/*
1100 * first try will not touch pci bridge res 1326 * first try will not touch pci bridge res
@@ -1105,59 +1331,57 @@ void __init
1105pci_assign_unassigned_resources(void) 1331pci_assign_unassigned_resources(void)
1106{ 1332{
1107 struct pci_bus *bus; 1333 struct pci_bus *bus;
1108 struct resource_list_x realloc_list; /* list of resources that 1334 LIST_HEAD(realloc_head); /* list of resources that
1109 want additional resources */ 1335 want additional resources */
1336 struct list_head *add_list = NULL;
1110 int tried_times = 0; 1337 int tried_times = 0;
1111 enum release_type rel_type = leaf_only; 1338 enum release_type rel_type = leaf_only;
1112 struct resource_list_x head, *list; 1339 LIST_HEAD(fail_head);
1340 struct pci_dev_resource *fail_res;
1113 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 1341 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1114 IORESOURCE_PREFETCH; 1342 IORESOURCE_PREFETCH;
1115 unsigned long failed_type; 1343 int pci_try_num = 1;
1116 int max_depth = pci_get_max_depth();
1117 int pci_try_num;
1118
1119 1344
1120 head.next = NULL; 1345 /* don't realloc if asked to do so */
1121 realloc_list.next = NULL; 1346 pci_realloc_detect();
1347 if (pci_realloc_enabled()) {
1348 int max_depth = pci_get_max_depth();
1122 1349
1123 pci_try_num = max_depth + 1; 1350 pci_try_num = max_depth + 1;
1124 printk(KERN_DEBUG "PCI: max bus depth: %d pci_try_num: %d\n", 1351 printk(KERN_DEBUG "PCI: max bus depth: %d pci_try_num: %d\n",
1125 max_depth, pci_try_num); 1352 max_depth, pci_try_num);
1353 }
1126 1354
1127again: 1355again:
1356 /*
1357 * last try will use add_list, otherwise will try good to have as
1358 * must have, so can realloc parent bridge resource
1359 */
1360 if (tried_times + 1 == pci_try_num)
1361 add_list = &realloc_head;
1128 /* Depth first, calculate sizes and alignments of all 1362 /* Depth first, calculate sizes and alignments of all
1129 subordinate buses. */ 1363 subordinate buses. */
1130 list_for_each_entry(bus, &pci_root_buses, node) 1364 list_for_each_entry(bus, &pci_root_buses, node)
1131 __pci_bus_size_bridges(bus, &realloc_list); 1365 __pci_bus_size_bridges(bus, add_list);
1132 1366
1133 /* Depth last, allocate resources and update the hardware. */ 1367 /* Depth last, allocate resources and update the hardware. */
1134 list_for_each_entry(bus, &pci_root_buses, node) 1368 list_for_each_entry(bus, &pci_root_buses, node)
1135 __pci_bus_assign_resources(bus, &realloc_list, &head); 1369 __pci_bus_assign_resources(bus, add_list, &fail_head);
1136 BUG_ON(realloc_list.next); 1370 if (add_list)
1371 BUG_ON(!list_empty(add_list));
1137 tried_times++; 1372 tried_times++;
1138 1373
1139 /* any device complain? */ 1374 /* any device complain? */
1140 if (!head.next) 1375 if (list_empty(&fail_head))
1141 goto enable_and_dump; 1376 goto enable_and_dump;
1142 1377
1143 /* don't realloc if asked to do so */ 1378 if (tried_times >= pci_try_num) {
1144 if (!pci_realloc_enabled()) { 1379 if (pci_realloc_enable == undefined)
1145 free_list(resource_list_x, &head); 1380 printk(KERN_INFO "Some PCI device resources are unassigned, try booting with pci=realloc\n");
1146 goto enable_and_dump; 1381 else if (pci_realloc_enable == auto_enabled)
1147 } 1382 printk(KERN_INFO "Automatically enabled pci realloc, if you have problem, try booting with pci=realloc=off\n");
1148 1383
1149 failed_type = 0; 1384 free_list(&fail_head);
1150 for (list = head.next; list;) {
1151 failed_type |= list->flags;
1152 list = list->next;
1153 }
1154 /*
1155 * io port are tight, don't try extra
1156 * or if reach the limit, don't want to try more
1157 */
1158 failed_type &= type_mask;
1159 if ((failed_type == IORESOURCE_IO) || (tried_times >= pci_try_num)) {
1160 free_list(resource_list_x, &head);
1161 goto enable_and_dump; 1385 goto enable_and_dump;
1162 } 1386 }
1163 1387
@@ -1172,25 +1396,23 @@ again:
1172 * Try to release leaf bridge's resources that doesn't fit resource of 1396 * Try to release leaf bridge's resources that doesn't fit resource of
1173 * child device under that bridge 1397 * child device under that bridge
1174 */ 1398 */
1175 for (list = head.next; list;) { 1399 list_for_each_entry(fail_res, &fail_head, list) {
1176 bus = list->dev->bus; 1400 bus = fail_res->dev->bus;
1177 pci_bus_release_bridge_resources(bus, list->flags & type_mask, 1401 pci_bus_release_bridge_resources(bus,
1178 rel_type); 1402 fail_res->flags & type_mask,
1179 list = list->next; 1403 rel_type);
1180 } 1404 }
1181 /* restore size and flags */ 1405 /* restore size and flags */
1182 for (list = head.next; list;) { 1406 list_for_each_entry(fail_res, &fail_head, list) {
1183 struct resource *res = list->res; 1407 struct resource *res = fail_res->res;
1184 1408
1185 res->start = list->start; 1409 res->start = fail_res->start;
1186 res->end = list->end; 1410 res->end = fail_res->end;
1187 res->flags = list->flags; 1411 res->flags = fail_res->flags;
1188 if (list->dev->subordinate) 1412 if (fail_res->dev->subordinate)
1189 res->flags = 0; 1413 res->flags = 0;
1190
1191 list = list->next;
1192 } 1414 }
1193 free_list(resource_list_x, &head); 1415 free_list(&fail_head);
1194 1416
1195 goto again; 1417 goto again;
1196 1418
@@ -1207,26 +1429,27 @@ enable_and_dump:
1207void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge) 1429void pci_assign_unassigned_bridge_resources(struct pci_dev *bridge)
1208{ 1430{
1209 struct pci_bus *parent = bridge->subordinate; 1431 struct pci_bus *parent = bridge->subordinate;
1432 LIST_HEAD(add_list); /* list of resources that
1433 want additional resources */
1210 int tried_times = 0; 1434 int tried_times = 0;
1211 struct resource_list_x head, *list; 1435 LIST_HEAD(fail_head);
1436 struct pci_dev_resource *fail_res;
1212 int retval; 1437 int retval;
1213 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM | 1438 unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM |
1214 IORESOURCE_PREFETCH; 1439 IORESOURCE_PREFETCH;
1215 1440
1216 head.next = NULL;
1217
1218again: 1441again:
1219 pci_bus_size_bridges(parent); 1442 __pci_bus_size_bridges(parent, &add_list);
1220 __pci_bridge_assign_resources(bridge, &head); 1443 __pci_bridge_assign_resources(bridge, &add_list, &fail_head);
1221 1444 BUG_ON(!list_empty(&add_list));
1222 tried_times++; 1445 tried_times++;
1223 1446
1224 if (!head.next) 1447 if (list_empty(&fail_head))
1225 goto enable_all; 1448 goto enable_all;
1226 1449
1227 if (tried_times >= 2) { 1450 if (tried_times >= 2) {
1228 /* still fail, don't need to try more */ 1451 /* still fail, don't need to try more */
1229 free_list(resource_list_x, &head); 1452 free_list(&fail_head);
1230 goto enable_all; 1453 goto enable_all;
1231 } 1454 }
1232 1455
@@ -1237,27 +1460,24 @@ again:
1237 * Try to release leaf bridge's resources that doesn't fit resource of 1460 * Try to release leaf bridge's resources that doesn't fit resource of
1238 * child device under that bridge 1461 * child device under that bridge
1239 */ 1462 */
1240 for (list = head.next; list;) { 1463 list_for_each_entry(fail_res, &fail_head, list) {
1241 struct pci_bus *bus = list->dev->bus; 1464 struct pci_bus *bus = fail_res->dev->bus;
1242 unsigned long flags = list->flags; 1465 unsigned long flags = fail_res->flags;
1243 1466
1244 pci_bus_release_bridge_resources(bus, flags & type_mask, 1467 pci_bus_release_bridge_resources(bus, flags & type_mask,
1245 whole_subtree); 1468 whole_subtree);
1246 list = list->next;
1247 } 1469 }
1248 /* restore size and flags */ 1470 /* restore size and flags */
1249 for (list = head.next; list;) { 1471 list_for_each_entry(fail_res, &fail_head, list) {
1250 struct resource *res = list->res; 1472 struct resource *res = fail_res->res;
1251 1473
1252 res->start = list->start; 1474 res->start = fail_res->start;
1253 res->end = list->end; 1475 res->end = fail_res->end;
1254 res->flags = list->flags; 1476 res->flags = fail_res->flags;
1255 if (list->dev->subordinate) 1477 if (fail_res->dev->subordinate)
1256 res->flags = 0; 1478 res->flags = 0;
1257
1258 list = list->next;
1259 } 1479 }
1260 free_list(resource_list_x, &head); 1480 free_list(&fail_head);
1261 1481
1262 goto again; 1482 goto again;
1263 1483
@@ -1267,3 +1487,41 @@ enable_all:
1267 pci_enable_bridges(parent); 1487 pci_enable_bridges(parent);
1268} 1488}
1269EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources); 1489EXPORT_SYMBOL_GPL(pci_assign_unassigned_bridge_resources);
1490
1491#ifdef CONFIG_HOTPLUG
1492/**
1493 * pci_rescan_bus - scan a PCI bus for devices.
1494 * @bus: PCI bus to scan
1495 *
1496 * Scan a PCI bus and child buses for new devices, adds them,
1497 * and enables them.
1498 *
1499 * Returns the max number of subordinate bus discovered.
1500 */
1501unsigned int __ref pci_rescan_bus(struct pci_bus *bus)
1502{
1503 unsigned int max;
1504 struct pci_dev *dev;
1505 LIST_HEAD(add_list); /* list of resources that
1506 want additional resources */
1507
1508 max = pci_scan_child_bus(bus);
1509
1510 down_read(&pci_bus_sem);
1511 list_for_each_entry(dev, &bus->devices, bus_list)
1512 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
1513 dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
1514 if (dev->subordinate)
1515 __pci_bus_size_bridges(dev->subordinate,
1516 &add_list);
1517 up_read(&pci_bus_sem);
1518 __pci_bus_assign_resources(bus, &add_list, NULL);
1519 BUG_ON(!list_empty(&add_list));
1520
1521 pci_enable_bridges(bus);
1522 pci_bus_add_devices(bus);
1523
1524 return max;
1525}
1526EXPORT_SYMBOL_GPL(pci_rescan_bus);
1527#endif
diff --git a/drivers/pci/setup-res.c b/drivers/pci/setup-res.c
index b66bfdbd21f7..eea85dafc763 100644
--- a/drivers/pci/setup-res.c
+++ b/drivers/pci/setup-res.c
@@ -114,7 +114,6 @@ int pci_claim_resource(struct pci_dev *dev, int resource)
114} 114}
115EXPORT_SYMBOL(pci_claim_resource); 115EXPORT_SYMBOL(pci_claim_resource);
116 116
117#ifdef CONFIG_PCI_QUIRKS
118void pci_disable_bridge_window(struct pci_dev *dev) 117void pci_disable_bridge_window(struct pci_dev *dev)
119{ 118{
120 dev_info(&dev->dev, "disabling bridge mem windows\n"); 119 dev_info(&dev->dev, "disabling bridge mem windows\n");
@@ -127,9 +126,6 @@ void pci_disable_bridge_window(struct pci_dev *dev)
127 pci_write_config_dword(dev, PCI_PREF_MEMORY_BASE, 0x0000fff0); 126 pci_write_config_dword(dev, PCI_PREF_MEMORY_BASE, 0x0000fff0);
128 pci_write_config_dword(dev, PCI_PREF_BASE_UPPER32, 0xffffffff); 127 pci_write_config_dword(dev, PCI_PREF_BASE_UPPER32, 0xffffffff);
129} 128}
130#endif /* CONFIG_PCI_QUIRKS */
131
132
133 129
134static int __pci_assign_resource(struct pci_bus *bus, struct pci_dev *dev, 130static int __pci_assign_resource(struct pci_bus *bus, struct pci_dev *dev,
135 int resno, resource_size_t size, resource_size_t align) 131 int resno, resource_size_t size, resource_size_t align)
@@ -158,22 +154,44 @@ static int __pci_assign_resource(struct pci_bus *bus, struct pci_dev *dev,
158 return ret; 154 return ret;
159} 155}
160 156
157/*
158 * Generic function that returns a value indicating that the device's
159 * original BIOS BAR address was not saved and so is not available for
160 * reinstatement.
161 *
162 * Can be over-ridden by architecture specific code that implements
163 * reinstatement functionality rather than leaving it disabled when
164 * normal allocation attempts fail.
165 */
166resource_size_t __weak pcibios_retrieve_fw_addr(struct pci_dev *dev, int idx)
167{
168 return 0;
169}
170
161static int pci_revert_fw_address(struct resource *res, struct pci_dev *dev, 171static int pci_revert_fw_address(struct resource *res, struct pci_dev *dev,
162 int resno, resource_size_t size) 172 int resno, resource_size_t size)
163{ 173{
164 struct resource *root, *conflict; 174 struct resource *root, *conflict;
165 resource_size_t start, end; 175 resource_size_t fw_addr, start, end;
166 int ret = 0; 176 int ret = 0;
167 177
168 if (res->flags & IORESOURCE_IO) 178 fw_addr = pcibios_retrieve_fw_addr(dev, resno);
169 root = &ioport_resource; 179 if (!fw_addr)
170 else 180 return 1;
171 root = &iomem_resource;
172 181
173 start = res->start; 182 start = res->start;
174 end = res->end; 183 end = res->end;
175 res->start = dev->fw_addr[resno]; 184 res->start = fw_addr;
176 res->end = res->start + size - 1; 185 res->end = res->start + size - 1;
186
187 root = pci_find_parent_resource(dev, res);
188 if (!root) {
189 if (res->flags & IORESOURCE_IO)
190 root = &ioport_resource;
191 else
192 root = &iomem_resource;
193 }
194
177 dev_info(&dev->dev, "BAR %d: trying firmware assignment %pR\n", 195 dev_info(&dev->dev, "BAR %d: trying firmware assignment %pR\n",
178 resno, res); 196 resno, res);
179 conflict = request_resource_conflict(root, res); 197 conflict = request_resource_conflict(root, res);
@@ -228,16 +246,17 @@ int pci_reassign_resource(struct pci_dev *dev, int resno, resource_size_t addsiz
228 int ret; 246 int ret;
229 247
230 if (!res->parent) { 248 if (!res->parent) {
231 dev_info(&dev->dev, "BAR %d: can't reassign an unassigned resouce %pR " 249 dev_info(&dev->dev, "BAR %d: can't reassign an unassigned resource %pR "
232 "\n", resno, res); 250 "\n", resno, res);
233 return -EINVAL; 251 return -EINVAL;
234 } 252 }
235 253
236 new_size = resource_size(res) + addsize + min_align; 254 /* already aligned with min_align */
255 new_size = resource_size(res) + addsize;
237 ret = _pci_assign_resource(dev, resno, new_size, min_align); 256 ret = _pci_assign_resource(dev, resno, new_size, min_align);
238 if (!ret) { 257 if (!ret) {
239 res->flags &= ~IORESOURCE_STARTALIGN; 258 res->flags &= ~IORESOURCE_STARTALIGN;
240 dev_info(&dev->dev, "BAR %d: assigned %pR\n", resno, res); 259 dev_info(&dev->dev, "BAR %d: reassigned %pR\n", resno, res);
241 if (resno < PCI_BRIDGE_RESOURCES) 260 if (resno < PCI_BRIDGE_RESOURCES)
242 pci_update_resource(dev, resno); 261 pci_update_resource(dev, resno);
243 } 262 }
@@ -267,7 +286,7 @@ int pci_assign_resource(struct pci_dev *dev, int resno)
267 * where firmware left it. That at least has a chance of 286 * where firmware left it. That at least has a chance of
268 * working, which is better than just leaving it disabled. 287 * working, which is better than just leaving it disabled.
269 */ 288 */
270 if (ret < 0 && dev->fw_addr[resno]) 289 if (ret < 0)
271 ret = pci_revert_fw_address(res, dev, resno, size); 290 ret = pci_revert_fw_address(res, dev, resno, size);
272 291
273 if (!ret) { 292 if (!ret) {
@@ -279,53 +298,6 @@ int pci_assign_resource(struct pci_dev *dev, int resno)
279 return ret; 298 return ret;
280} 299}
281 300
282
283/* Sort resources by alignment */
284void pdev_sort_resources(struct pci_dev *dev, struct resource_list *head)
285{
286 int i;
287
288 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
289 struct resource *r;
290 struct resource_list *list, *tmp;
291 resource_size_t r_align;
292
293 r = &dev->resource[i];
294
295 if (r->flags & IORESOURCE_PCI_FIXED)
296 continue;
297
298 if (!(r->flags) || r->parent)
299 continue;
300
301 r_align = pci_resource_alignment(dev, r);
302 if (!r_align) {
303 dev_warn(&dev->dev, "BAR %d: %pR has bogus alignment\n",
304 i, r);
305 continue;
306 }
307 for (list = head; ; list = list->next) {
308 resource_size_t align = 0;
309 struct resource_list *ln = list->next;
310
311 if (ln)
312 align = pci_resource_alignment(ln->dev, ln->res);
313
314 if (r_align > align) {
315 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
316 if (!tmp)
317 panic("pdev_sort_resources(): "
318 "kmalloc() failed!\n");
319 tmp->next = ln;
320 tmp->res = r;
321 tmp->dev = dev;
322 list->next = tmp;
323 break;
324 }
325 }
326 }
327}
328
329int pci_enable_resources(struct pci_dev *dev, int mask) 301int pci_enable_resources(struct pci_dev *dev, int mask)
330{ 302{
331 u16 cmd, old_cmd; 303 u16 cmd, old_cmd;
diff --git a/drivers/pci/xen-pcifront.c b/drivers/pci/xen-pcifront.c
index c18fab289ad5..d6cc62cb4cf7 100644
--- a/drivers/pci/xen-pcifront.c
+++ b/drivers/pci/xen-pcifront.c
@@ -189,7 +189,7 @@ static int pcifront_bus_read(struct pci_bus *bus, unsigned int devfn,
189 189
190 if (verbose_request) 190 if (verbose_request)
191 dev_info(&pdev->xdev->dev, 191 dev_info(&pdev->xdev->dev,
192 "read dev=%04x:%02x:%02x.%01x - offset %x size %d\n", 192 "read dev=%04x:%02x:%02x.%d - offset %x size %d\n",
193 pci_domain_nr(bus), bus->number, PCI_SLOT(devfn), 193 pci_domain_nr(bus), bus->number, PCI_SLOT(devfn),
194 PCI_FUNC(devfn), where, size); 194 PCI_FUNC(devfn), where, size);
195 195
@@ -228,7 +228,7 @@ static int pcifront_bus_write(struct pci_bus *bus, unsigned int devfn,
228 228
229 if (verbose_request) 229 if (verbose_request)
230 dev_info(&pdev->xdev->dev, 230 dev_info(&pdev->xdev->dev,
231 "write dev=%04x:%02x:%02x.%01x - " 231 "write dev=%04x:%02x:%02x.%d - "
232 "offset %x size %d val %x\n", 232 "offset %x size %d val %x\n",
233 pci_domain_nr(bus), bus->number, 233 pci_domain_nr(bus), bus->number,
234 PCI_SLOT(devfn), PCI_FUNC(devfn), where, size, val); 234 PCI_SLOT(devfn), PCI_FUNC(devfn), where, size, val);
@@ -433,7 +433,7 @@ static int __devinit pcifront_scan_bus(struct pcifront_device *pdev,
433 d = pci_scan_single_device(b, devfn); 433 d = pci_scan_single_device(b, devfn);
434 if (d) 434 if (d)
435 dev_info(&pdev->xdev->dev, "New device on " 435 dev_info(&pdev->xdev->dev, "New device on "
436 "%04x:%02x:%02x.%02x found.\n", domain, bus, 436 "%04x:%02x:%02x.%d found.\n", domain, bus,
437 PCI_SLOT(devfn), PCI_FUNC(devfn)); 437 PCI_SLOT(devfn), PCI_FUNC(devfn));
438 } 438 }
439 439
@@ -545,7 +545,7 @@ static void free_root_bus_devs(struct pci_bus *bus)
545 dev = container_of(bus->devices.next, struct pci_dev, 545 dev = container_of(bus->devices.next, struct pci_dev,
546 bus_list); 546 bus_list);
547 dev_dbg(&dev->dev, "removing device\n"); 547 dev_dbg(&dev->dev, "removing device\n");
548 pci_remove_bus_device(dev); 548 pci_stop_and_remove_bus_device(dev);
549 } 549 }
550} 550}
551 551
@@ -594,7 +594,7 @@ static pci_ers_result_t pcifront_common_process(int cmd,
594 } 594 }
595 pdrv = pcidev->driver; 595 pdrv = pcidev->driver;
596 596
597 if (get_driver(&pdrv->driver)) { 597 if (pdrv) {
598 if (pdrv->err_handler && pdrv->err_handler->error_detected) { 598 if (pdrv->err_handler && pdrv->err_handler->error_detected) {
599 dev_dbg(&pcidev->dev, 599 dev_dbg(&pcidev->dev,
600 "trying to call AER service\n"); 600 "trying to call AER service\n");
@@ -624,7 +624,6 @@ static pci_ers_result_t pcifront_common_process(int cmd,
624 } 624 }
625 } 625 }
626 } 626 }
627 put_driver(&pdrv->driver);
628 } 627 }
629 if (!flag) 628 if (!flag)
630 result = PCI_ERS_RESULT_NONE; 629 result = PCI_ERS_RESULT_NONE;
@@ -1042,15 +1041,15 @@ static int pcifront_detach_devices(struct pcifront_device *pdev)
1042 pci_dev = pci_get_slot(pci_bus, PCI_DEVFN(slot, func)); 1041 pci_dev = pci_get_slot(pci_bus, PCI_DEVFN(slot, func));
1043 if (!pci_dev) { 1042 if (!pci_dev) {
1044 dev_dbg(&pdev->xdev->dev, 1043 dev_dbg(&pdev->xdev->dev,
1045 "Cannot get PCI device %04x:%02x:%02x.%02x\n", 1044 "Cannot get PCI device %04x:%02x:%02x.%d\n",
1046 domain, bus, slot, func); 1045 domain, bus, slot, func);
1047 continue; 1046 continue;
1048 } 1047 }
1049 pci_remove_bus_device(pci_dev); 1048 pci_stop_and_remove_bus_device(pci_dev);
1050 pci_dev_put(pci_dev); 1049 pci_dev_put(pci_dev);
1051 1050
1052 dev_dbg(&pdev->xdev->dev, 1051 dev_dbg(&pdev->xdev->dev,
1053 "PCI device %04x:%02x:%02x.%02x removed.\n", 1052 "PCI device %04x:%02x:%02x.%d removed.\n",
1054 domain, bus, slot, func); 1053 domain, bus, slot, func);
1055 } 1054 }
1056 1055