diff options
Diffstat (limited to 'drivers/pci/pci.c')
-rw-r--r-- | drivers/pci/pci.c | 291 |
1 files changed, 133 insertions, 158 deletions
diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index 563901cd9c06..af0cc3456dc1 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c | |||
@@ -28,6 +28,7 @@ | |||
28 | #include <linux/pm_runtime.h> | 28 | #include <linux/pm_runtime.h> |
29 | #include <linux/pci_hotplug.h> | 29 | #include <linux/pci_hotplug.h> |
30 | #include <linux/vmalloc.h> | 30 | #include <linux/vmalloc.h> |
31 | #include <linux/pci-ats.h> | ||
31 | #include <asm/setup.h> | 32 | #include <asm/setup.h> |
32 | #include <asm/dma.h> | 33 | #include <asm/dma.h> |
33 | #include <linux/aer.h> | 34 | #include <linux/aer.h> |
@@ -455,7 +456,7 @@ struct resource *pci_find_parent_resource(const struct pci_dev *dev, | |||
455 | pci_bus_for_each_resource(bus, r, i) { | 456 | pci_bus_for_each_resource(bus, r, i) { |
456 | if (!r) | 457 | if (!r) |
457 | continue; | 458 | continue; |
458 | if (res->start && resource_contains(r, res)) { | 459 | if (resource_contains(r, res)) { |
459 | 460 | ||
460 | /* | 461 | /* |
461 | * If the window is prefetchable but the BAR is | 462 | * If the window is prefetchable but the BAR is |
@@ -574,8 +575,7 @@ static const struct pci_platform_pm_ops *pci_platform_pm; | |||
574 | int pci_set_platform_pm(const struct pci_platform_pm_ops *ops) | 575 | int pci_set_platform_pm(const struct pci_platform_pm_ops *ops) |
575 | { | 576 | { |
576 | if (!ops->is_manageable || !ops->set_state || !ops->get_state || | 577 | if (!ops->is_manageable || !ops->set_state || !ops->get_state || |
577 | !ops->choose_state || !ops->sleep_wake || !ops->run_wake || | 578 | !ops->choose_state || !ops->set_wakeup || !ops->need_resume) |
578 | !ops->need_resume) | ||
579 | return -EINVAL; | 579 | return -EINVAL; |
580 | pci_platform_pm = ops; | 580 | pci_platform_pm = ops; |
581 | return 0; | 581 | return 0; |
@@ -603,16 +603,10 @@ static inline pci_power_t platform_pci_choose_state(struct pci_dev *dev) | |||
603 | pci_platform_pm->choose_state(dev) : PCI_POWER_ERROR; | 603 | pci_platform_pm->choose_state(dev) : PCI_POWER_ERROR; |
604 | } | 604 | } |
605 | 605 | ||
606 | static inline int platform_pci_sleep_wake(struct pci_dev *dev, bool enable) | 606 | static inline int platform_pci_set_wakeup(struct pci_dev *dev, bool enable) |
607 | { | 607 | { |
608 | return pci_platform_pm ? | 608 | return pci_platform_pm ? |
609 | pci_platform_pm->sleep_wake(dev, enable) : -ENODEV; | 609 | pci_platform_pm->set_wakeup(dev, enable) : -ENODEV; |
610 | } | ||
611 | |||
612 | static inline int platform_pci_run_wake(struct pci_dev *dev, bool enable) | ||
613 | { | ||
614 | return pci_platform_pm ? | ||
615 | pci_platform_pm->run_wake(dev, enable) : -ENODEV; | ||
616 | } | 610 | } |
617 | 611 | ||
618 | static inline bool platform_pci_need_resume(struct pci_dev *dev) | 612 | static inline bool platform_pci_need_resume(struct pci_dev *dev) |
@@ -1173,6 +1167,8 @@ void pci_restore_state(struct pci_dev *dev) | |||
1173 | 1167 | ||
1174 | /* PCI Express register must be restored first */ | 1168 | /* PCI Express register must be restored first */ |
1175 | pci_restore_pcie_state(dev); | 1169 | pci_restore_pcie_state(dev); |
1170 | pci_restore_pasid_state(dev); | ||
1171 | pci_restore_pri_state(dev); | ||
1176 | pci_restore_ats_state(dev); | 1172 | pci_restore_ats_state(dev); |
1177 | pci_restore_vc_state(dev); | 1173 | pci_restore_vc_state(dev); |
1178 | 1174 | ||
@@ -1806,6 +1802,28 @@ static void __pci_pme_active(struct pci_dev *dev, bool enable) | |||
1806 | } | 1802 | } |
1807 | 1803 | ||
1808 | /** | 1804 | /** |
1805 | * pci_pme_restore - Restore PME configuration after config space restore. | ||
1806 | * @dev: PCI device to update. | ||
1807 | */ | ||
1808 | void pci_pme_restore(struct pci_dev *dev) | ||
1809 | { | ||
1810 | u16 pmcsr; | ||
1811 | |||
1812 | if (!dev->pme_support) | ||
1813 | return; | ||
1814 | |||
1815 | pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr); | ||
1816 | if (dev->wakeup_prepared) { | ||
1817 | pmcsr |= PCI_PM_CTRL_PME_ENABLE; | ||
1818 | pmcsr &= ~PCI_PM_CTRL_PME_STATUS; | ||
1819 | } else { | ||
1820 | pmcsr &= ~PCI_PM_CTRL_PME_ENABLE; | ||
1821 | pmcsr |= PCI_PM_CTRL_PME_STATUS; | ||
1822 | } | ||
1823 | pci_write_config_word(dev, dev->pm_cap + PCI_PM_CTRL, pmcsr); | ||
1824 | } | ||
1825 | |||
1826 | /** | ||
1809 | * pci_pme_active - enable or disable PCI device's PME# function | 1827 | * pci_pme_active - enable or disable PCI device's PME# function |
1810 | * @dev: PCI device to handle. | 1828 | * @dev: PCI device to handle. |
1811 | * @enable: 'true' to enable PME# generation; 'false' to disable it. | 1829 | * @enable: 'true' to enable PME# generation; 'false' to disable it. |
@@ -1872,10 +1890,9 @@ void pci_pme_active(struct pci_dev *dev, bool enable) | |||
1872 | EXPORT_SYMBOL(pci_pme_active); | 1890 | EXPORT_SYMBOL(pci_pme_active); |
1873 | 1891 | ||
1874 | /** | 1892 | /** |
1875 | * __pci_enable_wake - enable PCI device as wakeup event source | 1893 | * pci_enable_wake - enable PCI device as wakeup event source |
1876 | * @dev: PCI device affected | 1894 | * @dev: PCI device affected |
1877 | * @state: PCI state from which device will issue wakeup events | 1895 | * @state: PCI state from which device will issue wakeup events |
1878 | * @runtime: True if the events are to be generated at run time | ||
1879 | * @enable: True to enable event generation; false to disable | 1896 | * @enable: True to enable event generation; false to disable |
1880 | * | 1897 | * |
1881 | * This enables the device as a wakeup event source, or disables it. | 1898 | * This enables the device as a wakeup event source, or disables it. |
@@ -1891,14 +1908,10 @@ EXPORT_SYMBOL(pci_pme_active); | |||
1891 | * Error code depending on the platform is returned if both the platform and | 1908 | * Error code depending on the platform is returned if both the platform and |
1892 | * the native mechanism fail to enable the generation of wake-up events | 1909 | * the native mechanism fail to enable the generation of wake-up events |
1893 | */ | 1910 | */ |
1894 | int __pci_enable_wake(struct pci_dev *dev, pci_power_t state, | 1911 | int pci_enable_wake(struct pci_dev *dev, pci_power_t state, bool enable) |
1895 | bool runtime, bool enable) | ||
1896 | { | 1912 | { |
1897 | int ret = 0; | 1913 | int ret = 0; |
1898 | 1914 | ||
1899 | if (enable && !runtime && !device_may_wakeup(&dev->dev)) | ||
1900 | return -EINVAL; | ||
1901 | |||
1902 | /* Don't do the same thing twice in a row for one device. */ | 1915 | /* Don't do the same thing twice in a row for one device. */ |
1903 | if (!!enable == !!dev->wakeup_prepared) | 1916 | if (!!enable == !!dev->wakeup_prepared) |
1904 | return 0; | 1917 | return 0; |
@@ -1916,24 +1929,20 @@ int __pci_enable_wake(struct pci_dev *dev, pci_power_t state, | |||
1916 | pci_pme_active(dev, true); | 1929 | pci_pme_active(dev, true); |
1917 | else | 1930 | else |
1918 | ret = 1; | 1931 | ret = 1; |
1919 | error = runtime ? platform_pci_run_wake(dev, true) : | 1932 | error = platform_pci_set_wakeup(dev, true); |
1920 | platform_pci_sleep_wake(dev, true); | ||
1921 | if (ret) | 1933 | if (ret) |
1922 | ret = error; | 1934 | ret = error; |
1923 | if (!ret) | 1935 | if (!ret) |
1924 | dev->wakeup_prepared = true; | 1936 | dev->wakeup_prepared = true; |
1925 | } else { | 1937 | } else { |
1926 | if (runtime) | 1938 | platform_pci_set_wakeup(dev, false); |
1927 | platform_pci_run_wake(dev, false); | ||
1928 | else | ||
1929 | platform_pci_sleep_wake(dev, false); | ||
1930 | pci_pme_active(dev, false); | 1939 | pci_pme_active(dev, false); |
1931 | dev->wakeup_prepared = false; | 1940 | dev->wakeup_prepared = false; |
1932 | } | 1941 | } |
1933 | 1942 | ||
1934 | return ret; | 1943 | return ret; |
1935 | } | 1944 | } |
1936 | EXPORT_SYMBOL(__pci_enable_wake); | 1945 | EXPORT_SYMBOL(pci_enable_wake); |
1937 | 1946 | ||
1938 | /** | 1947 | /** |
1939 | * pci_wake_from_d3 - enable/disable device to wake up from D3_hot or D3_cold | 1948 | * pci_wake_from_d3 - enable/disable device to wake up from D3_hot or D3_cold |
@@ -1960,12 +1969,13 @@ EXPORT_SYMBOL(pci_wake_from_d3); | |||
1960 | /** | 1969 | /** |
1961 | * pci_target_state - find an appropriate low power state for a given PCI dev | 1970 | * pci_target_state - find an appropriate low power state for a given PCI dev |
1962 | * @dev: PCI device | 1971 | * @dev: PCI device |
1972 | * @wakeup: Whether or not wakeup functionality will be enabled for the device. | ||
1963 | * | 1973 | * |
1964 | * Use underlying platform code to find a supported low power state for @dev. | 1974 | * Use underlying platform code to find a supported low power state for @dev. |
1965 | * If the platform can't manage @dev, return the deepest state from which it | 1975 | * If the platform can't manage @dev, return the deepest state from which it |
1966 | * can generate wake events, based on any available PME info. | 1976 | * can generate wake events, based on any available PME info. |
1967 | */ | 1977 | */ |
1968 | static pci_power_t pci_target_state(struct pci_dev *dev) | 1978 | static pci_power_t pci_target_state(struct pci_dev *dev, bool wakeup) |
1969 | { | 1979 | { |
1970 | pci_power_t target_state = PCI_D3hot; | 1980 | pci_power_t target_state = PCI_D3hot; |
1971 | 1981 | ||
@@ -2002,7 +2012,7 @@ static pci_power_t pci_target_state(struct pci_dev *dev) | |||
2002 | if (dev->current_state == PCI_D3cold) | 2012 | if (dev->current_state == PCI_D3cold) |
2003 | target_state = PCI_D3cold; | 2013 | target_state = PCI_D3cold; |
2004 | 2014 | ||
2005 | if (device_may_wakeup(&dev->dev)) { | 2015 | if (wakeup) { |
2006 | /* | 2016 | /* |
2007 | * Find the deepest state from which the device can generate | 2017 | * Find the deepest state from which the device can generate |
2008 | * wake-up events, make it the target state and enable device | 2018 | * wake-up events, make it the target state and enable device |
@@ -2028,13 +2038,14 @@ static pci_power_t pci_target_state(struct pci_dev *dev) | |||
2028 | */ | 2038 | */ |
2029 | int pci_prepare_to_sleep(struct pci_dev *dev) | 2039 | int pci_prepare_to_sleep(struct pci_dev *dev) |
2030 | { | 2040 | { |
2031 | pci_power_t target_state = pci_target_state(dev); | 2041 | bool wakeup = device_may_wakeup(&dev->dev); |
2042 | pci_power_t target_state = pci_target_state(dev, wakeup); | ||
2032 | int error; | 2043 | int error; |
2033 | 2044 | ||
2034 | if (target_state == PCI_POWER_ERROR) | 2045 | if (target_state == PCI_POWER_ERROR) |
2035 | return -EIO; | 2046 | return -EIO; |
2036 | 2047 | ||
2037 | pci_enable_wake(dev, target_state, device_may_wakeup(&dev->dev)); | 2048 | pci_enable_wake(dev, target_state, wakeup); |
2038 | 2049 | ||
2039 | error = pci_set_power_state(dev, target_state); | 2050 | error = pci_set_power_state(dev, target_state); |
2040 | 2051 | ||
@@ -2067,20 +2078,21 @@ EXPORT_SYMBOL(pci_back_from_sleep); | |||
2067 | */ | 2078 | */ |
2068 | int pci_finish_runtime_suspend(struct pci_dev *dev) | 2079 | int pci_finish_runtime_suspend(struct pci_dev *dev) |
2069 | { | 2080 | { |
2070 | pci_power_t target_state = pci_target_state(dev); | 2081 | pci_power_t target_state; |
2071 | int error; | 2082 | int error; |
2072 | 2083 | ||
2084 | target_state = pci_target_state(dev, device_can_wakeup(&dev->dev)); | ||
2073 | if (target_state == PCI_POWER_ERROR) | 2085 | if (target_state == PCI_POWER_ERROR) |
2074 | return -EIO; | 2086 | return -EIO; |
2075 | 2087 | ||
2076 | dev->runtime_d3cold = target_state == PCI_D3cold; | 2088 | dev->runtime_d3cold = target_state == PCI_D3cold; |
2077 | 2089 | ||
2078 | __pci_enable_wake(dev, target_state, true, pci_dev_run_wake(dev)); | 2090 | pci_enable_wake(dev, target_state, pci_dev_run_wake(dev)); |
2079 | 2091 | ||
2080 | error = pci_set_power_state(dev, target_state); | 2092 | error = pci_set_power_state(dev, target_state); |
2081 | 2093 | ||
2082 | if (error) { | 2094 | if (error) { |
2083 | __pci_enable_wake(dev, target_state, true, false); | 2095 | pci_enable_wake(dev, target_state, false); |
2084 | dev->runtime_d3cold = false; | 2096 | dev->runtime_d3cold = false; |
2085 | } | 2097 | } |
2086 | 2098 | ||
@@ -2099,20 +2111,20 @@ bool pci_dev_run_wake(struct pci_dev *dev) | |||
2099 | { | 2111 | { |
2100 | struct pci_bus *bus = dev->bus; | 2112 | struct pci_bus *bus = dev->bus; |
2101 | 2113 | ||
2102 | if (device_run_wake(&dev->dev)) | 2114 | if (device_can_wakeup(&dev->dev)) |
2103 | return true; | 2115 | return true; |
2104 | 2116 | ||
2105 | if (!dev->pme_support) | 2117 | if (!dev->pme_support) |
2106 | return false; | 2118 | return false; |
2107 | 2119 | ||
2108 | /* PME-capable in principle, but not from the intended sleep state */ | 2120 | /* PME-capable in principle, but not from the target power state */ |
2109 | if (!pci_pme_capable(dev, pci_target_state(dev))) | 2121 | if (!pci_pme_capable(dev, pci_target_state(dev, false))) |
2110 | return false; | 2122 | return false; |
2111 | 2123 | ||
2112 | while (bus->parent) { | 2124 | while (bus->parent) { |
2113 | struct pci_dev *bridge = bus->self; | 2125 | struct pci_dev *bridge = bus->self; |
2114 | 2126 | ||
2115 | if (device_run_wake(&bridge->dev)) | 2127 | if (device_can_wakeup(&bridge->dev)) |
2116 | return true; | 2128 | return true; |
2117 | 2129 | ||
2118 | bus = bus->parent; | 2130 | bus = bus->parent; |
@@ -2120,7 +2132,7 @@ bool pci_dev_run_wake(struct pci_dev *dev) | |||
2120 | 2132 | ||
2121 | /* We have reached the root bus. */ | 2133 | /* We have reached the root bus. */ |
2122 | if (bus->bridge) | 2134 | if (bus->bridge) |
2123 | return device_run_wake(bus->bridge); | 2135 | return device_can_wakeup(bus->bridge); |
2124 | 2136 | ||
2125 | return false; | 2137 | return false; |
2126 | } | 2138 | } |
@@ -2141,9 +2153,10 @@ EXPORT_SYMBOL_GPL(pci_dev_run_wake); | |||
2141 | bool pci_dev_keep_suspended(struct pci_dev *pci_dev) | 2153 | bool pci_dev_keep_suspended(struct pci_dev *pci_dev) |
2142 | { | 2154 | { |
2143 | struct device *dev = &pci_dev->dev; | 2155 | struct device *dev = &pci_dev->dev; |
2156 | bool wakeup = device_may_wakeup(dev); | ||
2144 | 2157 | ||
2145 | if (!pm_runtime_suspended(dev) | 2158 | if (!pm_runtime_suspended(dev) |
2146 | || pci_target_state(pci_dev) != pci_dev->current_state | 2159 | || pci_target_state(pci_dev, wakeup) != pci_dev->current_state |
2147 | || platform_pci_need_resume(pci_dev) | 2160 | || platform_pci_need_resume(pci_dev) |
2148 | || (pci_dev->dev_flags & PCI_DEV_FLAGS_NEEDS_RESUME)) | 2161 | || (pci_dev->dev_flags & PCI_DEV_FLAGS_NEEDS_RESUME)) |
2149 | return false; | 2162 | return false; |
@@ -2161,7 +2174,7 @@ bool pci_dev_keep_suspended(struct pci_dev *pci_dev) | |||
2161 | spin_lock_irq(&dev->power.lock); | 2174 | spin_lock_irq(&dev->power.lock); |
2162 | 2175 | ||
2163 | if (pm_runtime_suspended(dev) && pci_dev->current_state < PCI_D3cold && | 2176 | if (pm_runtime_suspended(dev) && pci_dev->current_state < PCI_D3cold && |
2164 | !device_may_wakeup(dev)) | 2177 | !wakeup) |
2165 | __pci_pme_active(pci_dev, false); | 2178 | __pci_pme_active(pci_dev, false); |
2166 | 2179 | ||
2167 | spin_unlock_irq(&dev->power.lock); | 2180 | spin_unlock_irq(&dev->power.lock); |
@@ -3709,46 +3722,6 @@ void pci_intx(struct pci_dev *pdev, int enable) | |||
3709 | } | 3722 | } |
3710 | EXPORT_SYMBOL_GPL(pci_intx); | 3723 | EXPORT_SYMBOL_GPL(pci_intx); |
3711 | 3724 | ||
3712 | /** | ||
3713 | * pci_intx_mask_supported - probe for INTx masking support | ||
3714 | * @dev: the PCI device to operate on | ||
3715 | * | ||
3716 | * Check if the device dev support INTx masking via the config space | ||
3717 | * command word. | ||
3718 | */ | ||
3719 | bool pci_intx_mask_supported(struct pci_dev *dev) | ||
3720 | { | ||
3721 | bool mask_supported = false; | ||
3722 | u16 orig, new; | ||
3723 | |||
3724 | if (dev->broken_intx_masking) | ||
3725 | return false; | ||
3726 | |||
3727 | pci_cfg_access_lock(dev); | ||
3728 | |||
3729 | pci_read_config_word(dev, PCI_COMMAND, &orig); | ||
3730 | pci_write_config_word(dev, PCI_COMMAND, | ||
3731 | orig ^ PCI_COMMAND_INTX_DISABLE); | ||
3732 | pci_read_config_word(dev, PCI_COMMAND, &new); | ||
3733 | |||
3734 | /* | ||
3735 | * There's no way to protect against hardware bugs or detect them | ||
3736 | * reliably, but as long as we know what the value should be, let's | ||
3737 | * go ahead and check it. | ||
3738 | */ | ||
3739 | if ((new ^ orig) & ~PCI_COMMAND_INTX_DISABLE) { | ||
3740 | dev_err(&dev->dev, "Command register changed from 0x%x to 0x%x: driver or hardware bug?\n", | ||
3741 | orig, new); | ||
3742 | } else if ((new ^ orig) & PCI_COMMAND_INTX_DISABLE) { | ||
3743 | mask_supported = true; | ||
3744 | pci_write_config_word(dev, PCI_COMMAND, orig); | ||
3745 | } | ||
3746 | |||
3747 | pci_cfg_access_unlock(dev); | ||
3748 | return mask_supported; | ||
3749 | } | ||
3750 | EXPORT_SYMBOL_GPL(pci_intx_mask_supported); | ||
3751 | |||
3752 | static bool pci_check_and_set_intx_mask(struct pci_dev *dev, bool mask) | 3725 | static bool pci_check_and_set_intx_mask(struct pci_dev *dev, bool mask) |
3753 | { | 3726 | { |
3754 | struct pci_bus *bus = dev->bus; | 3727 | struct pci_bus *bus = dev->bus; |
@@ -3799,7 +3772,7 @@ done: | |||
3799 | * @dev: the PCI device to operate on | 3772 | * @dev: the PCI device to operate on |
3800 | * | 3773 | * |
3801 | * Check if the device dev has its INTx line asserted, mask it and | 3774 | * Check if the device dev has its INTx line asserted, mask it and |
3802 | * return true in that case. False is returned if not interrupt was | 3775 | * return true in that case. False is returned if no interrupt was |
3803 | * pending. | 3776 | * pending. |
3804 | */ | 3777 | */ |
3805 | bool pci_check_and_mask_intx(struct pci_dev *dev) | 3778 | bool pci_check_and_mask_intx(struct pci_dev *dev) |
@@ -4069,40 +4042,6 @@ static int pci_dev_reset_slot_function(struct pci_dev *dev, int probe) | |||
4069 | return pci_reset_hotplug_slot(dev->slot->hotplug, probe); | 4042 | return pci_reset_hotplug_slot(dev->slot->hotplug, probe); |
4070 | } | 4043 | } |
4071 | 4044 | ||
4072 | static int __pci_dev_reset(struct pci_dev *dev, int probe) | ||
4073 | { | ||
4074 | int rc; | ||
4075 | |||
4076 | might_sleep(); | ||
4077 | |||
4078 | rc = pci_dev_specific_reset(dev, probe); | ||
4079 | if (rc != -ENOTTY) | ||
4080 | goto done; | ||
4081 | |||
4082 | if (pcie_has_flr(dev)) { | ||
4083 | if (!probe) | ||
4084 | pcie_flr(dev); | ||
4085 | rc = 0; | ||
4086 | goto done; | ||
4087 | } | ||
4088 | |||
4089 | rc = pci_af_flr(dev, probe); | ||
4090 | if (rc != -ENOTTY) | ||
4091 | goto done; | ||
4092 | |||
4093 | rc = pci_pm_reset(dev, probe); | ||
4094 | if (rc != -ENOTTY) | ||
4095 | goto done; | ||
4096 | |||
4097 | rc = pci_dev_reset_slot_function(dev, probe); | ||
4098 | if (rc != -ENOTTY) | ||
4099 | goto done; | ||
4100 | |||
4101 | rc = pci_parent_bus_reset(dev, probe); | ||
4102 | done: | ||
4103 | return rc; | ||
4104 | } | ||
4105 | |||
4106 | static void pci_dev_lock(struct pci_dev *dev) | 4045 | static void pci_dev_lock(struct pci_dev *dev) |
4107 | { | 4046 | { |
4108 | pci_cfg_access_lock(dev); | 4047 | pci_cfg_access_lock(dev); |
@@ -4128,26 +4067,18 @@ static void pci_dev_unlock(struct pci_dev *dev) | |||
4128 | pci_cfg_access_unlock(dev); | 4067 | pci_cfg_access_unlock(dev); |
4129 | } | 4068 | } |
4130 | 4069 | ||
4131 | /** | 4070 | static void pci_dev_save_and_disable(struct pci_dev *dev) |
4132 | * pci_reset_notify - notify device driver of reset | ||
4133 | * @dev: device to be notified of reset | ||
4134 | * @prepare: 'true' if device is about to be reset; 'false' if reset attempt | ||
4135 | * completed | ||
4136 | * | ||
4137 | * Must be called prior to device access being disabled and after device | ||
4138 | * access is restored. | ||
4139 | */ | ||
4140 | static void pci_reset_notify(struct pci_dev *dev, bool prepare) | ||
4141 | { | 4071 | { |
4142 | const struct pci_error_handlers *err_handler = | 4072 | const struct pci_error_handlers *err_handler = |
4143 | dev->driver ? dev->driver->err_handler : NULL; | 4073 | dev->driver ? dev->driver->err_handler : NULL; |
4144 | if (err_handler && err_handler->reset_notify) | ||
4145 | err_handler->reset_notify(dev, prepare); | ||
4146 | } | ||
4147 | 4074 | ||
4148 | static void pci_dev_save_and_disable(struct pci_dev *dev) | 4075 | /* |
4149 | { | 4076 | * dev->driver->err_handler->reset_prepare() is protected against |
4150 | pci_reset_notify(dev, true); | 4077 | * races with ->remove() by the device lock, which must be held by |
4078 | * the caller. | ||
4079 | */ | ||
4080 | if (err_handler && err_handler->reset_prepare) | ||
4081 | err_handler->reset_prepare(dev); | ||
4151 | 4082 | ||
4152 | /* | 4083 | /* |
4153 | * Wake-up device prior to save. PM registers default to D0 after | 4084 | * Wake-up device prior to save. PM registers default to D0 after |
@@ -4169,23 +4100,18 @@ static void pci_dev_save_and_disable(struct pci_dev *dev) | |||
4169 | 4100 | ||
4170 | static void pci_dev_restore(struct pci_dev *dev) | 4101 | static void pci_dev_restore(struct pci_dev *dev) |
4171 | { | 4102 | { |
4172 | pci_restore_state(dev); | 4103 | const struct pci_error_handlers *err_handler = |
4173 | pci_reset_notify(dev, false); | 4104 | dev->driver ? dev->driver->err_handler : NULL; |
4174 | } | ||
4175 | |||
4176 | static int pci_dev_reset(struct pci_dev *dev, int probe) | ||
4177 | { | ||
4178 | int rc; | ||
4179 | |||
4180 | if (!probe) | ||
4181 | pci_dev_lock(dev); | ||
4182 | |||
4183 | rc = __pci_dev_reset(dev, probe); | ||
4184 | 4105 | ||
4185 | if (!probe) | 4106 | pci_restore_state(dev); |
4186 | pci_dev_unlock(dev); | ||
4187 | 4107 | ||
4188 | return rc; | 4108 | /* |
4109 | * dev->driver->err_handler->reset_done() is protected against | ||
4110 | * races with ->remove() by the device lock, which must be held by | ||
4111 | * the caller. | ||
4112 | */ | ||
4113 | if (err_handler && err_handler->reset_done) | ||
4114 | err_handler->reset_done(dev); | ||
4189 | } | 4115 | } |
4190 | 4116 | ||
4191 | /** | 4117 | /** |
@@ -4207,7 +4133,13 @@ static int pci_dev_reset(struct pci_dev *dev, int probe) | |||
4207 | */ | 4133 | */ |
4208 | int __pci_reset_function(struct pci_dev *dev) | 4134 | int __pci_reset_function(struct pci_dev *dev) |
4209 | { | 4135 | { |
4210 | return pci_dev_reset(dev, 0); | 4136 | int ret; |
4137 | |||
4138 | pci_dev_lock(dev); | ||
4139 | ret = __pci_reset_function_locked(dev); | ||
4140 | pci_dev_unlock(dev); | ||
4141 | |||
4142 | return ret; | ||
4211 | } | 4143 | } |
4212 | EXPORT_SYMBOL_GPL(__pci_reset_function); | 4144 | EXPORT_SYMBOL_GPL(__pci_reset_function); |
4213 | 4145 | ||
@@ -4232,7 +4164,27 @@ EXPORT_SYMBOL_GPL(__pci_reset_function); | |||
4232 | */ | 4164 | */ |
4233 | int __pci_reset_function_locked(struct pci_dev *dev) | 4165 | int __pci_reset_function_locked(struct pci_dev *dev) |
4234 | { | 4166 | { |
4235 | return __pci_dev_reset(dev, 0); | 4167 | int rc; |
4168 | |||
4169 | might_sleep(); | ||
4170 | |||
4171 | rc = pci_dev_specific_reset(dev, 0); | ||
4172 | if (rc != -ENOTTY) | ||
4173 | return rc; | ||
4174 | if (pcie_has_flr(dev)) { | ||
4175 | pcie_flr(dev); | ||
4176 | return 0; | ||
4177 | } | ||
4178 | rc = pci_af_flr(dev, 0); | ||
4179 | if (rc != -ENOTTY) | ||
4180 | return rc; | ||
4181 | rc = pci_pm_reset(dev, 0); | ||
4182 | if (rc != -ENOTTY) | ||
4183 | return rc; | ||
4184 | rc = pci_dev_reset_slot_function(dev, 0); | ||
4185 | if (rc != -ENOTTY) | ||
4186 | return rc; | ||
4187 | return pci_parent_bus_reset(dev, 0); | ||
4236 | } | 4188 | } |
4237 | EXPORT_SYMBOL_GPL(__pci_reset_function_locked); | 4189 | EXPORT_SYMBOL_GPL(__pci_reset_function_locked); |
4238 | 4190 | ||
@@ -4249,7 +4201,26 @@ EXPORT_SYMBOL_GPL(__pci_reset_function_locked); | |||
4249 | */ | 4201 | */ |
4250 | int pci_probe_reset_function(struct pci_dev *dev) | 4202 | int pci_probe_reset_function(struct pci_dev *dev) |
4251 | { | 4203 | { |
4252 | return pci_dev_reset(dev, 1); | 4204 | int rc; |
4205 | |||
4206 | might_sleep(); | ||
4207 | |||
4208 | rc = pci_dev_specific_reset(dev, 1); | ||
4209 | if (rc != -ENOTTY) | ||
4210 | return rc; | ||
4211 | if (pcie_has_flr(dev)) | ||
4212 | return 0; | ||
4213 | rc = pci_af_flr(dev, 1); | ||
4214 | if (rc != -ENOTTY) | ||
4215 | return rc; | ||
4216 | rc = pci_pm_reset(dev, 1); | ||
4217 | if (rc != -ENOTTY) | ||
4218 | return rc; | ||
4219 | rc = pci_dev_reset_slot_function(dev, 1); | ||
4220 | if (rc != -ENOTTY) | ||
4221 | return rc; | ||
4222 | |||
4223 | return pci_parent_bus_reset(dev, 1); | ||
4253 | } | 4224 | } |
4254 | 4225 | ||
4255 | /** | 4226 | /** |
@@ -4272,15 +4243,17 @@ int pci_reset_function(struct pci_dev *dev) | |||
4272 | { | 4243 | { |
4273 | int rc; | 4244 | int rc; |
4274 | 4245 | ||
4275 | rc = pci_dev_reset(dev, 1); | 4246 | rc = pci_probe_reset_function(dev); |
4276 | if (rc) | 4247 | if (rc) |
4277 | return rc; | 4248 | return rc; |
4278 | 4249 | ||
4250 | pci_dev_lock(dev); | ||
4279 | pci_dev_save_and_disable(dev); | 4251 | pci_dev_save_and_disable(dev); |
4280 | 4252 | ||
4281 | rc = pci_dev_reset(dev, 0); | 4253 | rc = __pci_reset_function_locked(dev); |
4282 | 4254 | ||
4283 | pci_dev_restore(dev); | 4255 | pci_dev_restore(dev); |
4256 | pci_dev_unlock(dev); | ||
4284 | 4257 | ||
4285 | return rc; | 4258 | return rc; |
4286 | } | 4259 | } |
@@ -4296,20 +4269,18 @@ int pci_try_reset_function(struct pci_dev *dev) | |||
4296 | { | 4269 | { |
4297 | int rc; | 4270 | int rc; |
4298 | 4271 | ||
4299 | rc = pci_dev_reset(dev, 1); | 4272 | rc = pci_probe_reset_function(dev); |
4300 | if (rc) | 4273 | if (rc) |
4301 | return rc; | 4274 | return rc; |
4302 | 4275 | ||
4303 | pci_dev_save_and_disable(dev); | 4276 | if (!pci_dev_trylock(dev)) |
4277 | return -EAGAIN; | ||
4304 | 4278 | ||
4305 | if (pci_dev_trylock(dev)) { | 4279 | pci_dev_save_and_disable(dev); |
4306 | rc = __pci_dev_reset(dev, 0); | 4280 | rc = __pci_reset_function_locked(dev); |
4307 | pci_dev_unlock(dev); | 4281 | pci_dev_unlock(dev); |
4308 | } else | ||
4309 | rc = -EAGAIN; | ||
4310 | 4282 | ||
4311 | pci_dev_restore(dev); | 4283 | pci_dev_restore(dev); |
4312 | |||
4313 | return rc; | 4284 | return rc; |
4314 | } | 4285 | } |
4315 | EXPORT_SYMBOL_GPL(pci_try_reset_function); | 4286 | EXPORT_SYMBOL_GPL(pci_try_reset_function); |
@@ -4459,7 +4430,9 @@ static void pci_bus_save_and_disable(struct pci_bus *bus) | |||
4459 | struct pci_dev *dev; | 4430 | struct pci_dev *dev; |
4460 | 4431 | ||
4461 | list_for_each_entry(dev, &bus->devices, bus_list) { | 4432 | list_for_each_entry(dev, &bus->devices, bus_list) { |
4433 | pci_dev_lock(dev); | ||
4462 | pci_dev_save_and_disable(dev); | 4434 | pci_dev_save_and_disable(dev); |
4435 | pci_dev_unlock(dev); | ||
4463 | if (dev->subordinate) | 4436 | if (dev->subordinate) |
4464 | pci_bus_save_and_disable(dev->subordinate); | 4437 | pci_bus_save_and_disable(dev->subordinate); |
4465 | } | 4438 | } |
@@ -4474,7 +4447,9 @@ static void pci_bus_restore(struct pci_bus *bus) | |||
4474 | struct pci_dev *dev; | 4447 | struct pci_dev *dev; |
4475 | 4448 | ||
4476 | list_for_each_entry(dev, &bus->devices, bus_list) { | 4449 | list_for_each_entry(dev, &bus->devices, bus_list) { |
4450 | pci_dev_lock(dev); | ||
4477 | pci_dev_restore(dev); | 4451 | pci_dev_restore(dev); |
4452 | pci_dev_unlock(dev); | ||
4478 | if (dev->subordinate) | 4453 | if (dev->subordinate) |
4479 | pci_bus_restore(dev->subordinate); | 4454 | pci_bus_restore(dev->subordinate); |
4480 | } | 4455 | } |