Merge tag 'pci-v6.3-changes' of git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci
[linux-block.git] / drivers / pci / pci.c
index 5641786bd020663ebfcb81e09a45b1ed645a339b..7a67611dc5f48f1b4cdc89c154bf4a4ce01ba483 100644 (file)
@@ -167,9 +167,6 @@ static int __init pcie_port_pm_setup(char *str)
 }
 __setup("pcie_port_pm=", pcie_port_pm_setup);
 
-/* Time to wait after a reset for device to become responsive */
-#define PCIE_RESET_READY_POLL_MS 60000
-
 /**
  * pci_bus_max_busnr - returns maximum PCI bus number of given bus' children
  * @bus: pointer to PCI bus structure to search
@@ -1174,7 +1171,7 @@ static int pci_dev_wait(struct pci_dev *dev, char *reset_type, int timeout)
                        return -ENOTTY;
                }
 
-               if (delay > 1000)
+               if (delay > PCI_RESET_WAIT)
                        pci_info(dev, "not ready %dms after %s; waiting\n",
                                 delay - 1, reset_type);
 
@@ -1183,7 +1180,7 @@ static int pci_dev_wait(struct pci_dev *dev, char *reset_type, int timeout)
                pci_read_config_dword(dev, PCI_COMMAND, &id);
        }
 
-       if (delay > 1000)
+       if (delay > PCI_RESET_WAIT)
                pci_info(dev, "ready %dms after %s\n", delay - 1,
                         reset_type);
 
@@ -4941,24 +4938,31 @@ static int pci_bus_max_d3cold_delay(const struct pci_bus *bus)
 /**
  * pci_bridge_wait_for_secondary_bus - Wait for secondary bus to be accessible
  * @dev: PCI bridge
+ * @reset_type: reset type in human-readable form
+ * @timeout: maximum time to wait for devices on secondary bus (milliseconds)
  *
  * Handle necessary delays before access to the devices on the secondary
- * side of the bridge are permitted after D3cold to D0 transition.
+ * side of the bridge are permitted after D3cold to D0 transition
+ * or Conventional Reset.
  *
  * For PCIe this means the delays in PCIe 5.0 section 6.6.1. For
  * conventional PCI it means Tpvrh + Trhfa specified in PCI 3.0 section
  * 4.3.2.
+ *
+ * Return 0 on success or -ENOTTY if the first device on the secondary bus
+ * failed to become accessible.
  */
-void pci_bridge_wait_for_secondary_bus(struct pci_dev *dev)
+int pci_bridge_wait_for_secondary_bus(struct pci_dev *dev, char *reset_type,
+                                     int timeout)
 {
        struct pci_dev *child;
        int delay;
 
        if (pci_dev_is_disconnected(dev))
-               return;
+               return 0;
 
-       if (!pci_is_bridge(dev) || !dev->bridge_d3)
-               return;
+       if (!pci_is_bridge(dev))
+               return 0;
 
        down_read(&pci_bus_sem);
 
@@ -4970,14 +4974,14 @@ void pci_bridge_wait_for_secondary_bus(struct pci_dev *dev)
         */
        if (!dev->subordinate || list_empty(&dev->subordinate->devices)) {
                up_read(&pci_bus_sem);
-               return;
+               return 0;
        }
 
        /* Take d3cold_delay requirements into account */
        delay = pci_bus_max_d3cold_delay(dev->subordinate);
        if (!delay) {
                up_read(&pci_bus_sem);
-               return;
+               return 0;
        }
 
        child = list_first_entry(&dev->subordinate->devices, struct pci_dev,
@@ -4986,14 +4990,12 @@ void pci_bridge_wait_for_secondary_bus(struct pci_dev *dev)
 
        /*
         * Conventional PCI and PCI-X we need to wait Tpvrh + Trhfa before
-        * accessing the device after reset (that is 1000 ms + 100 ms). In
-        * practice this should not be needed because we don't do power
-        * management for them (see pci_bridge_d3_possible()).
+        * accessing the device after reset (that is 1000 ms + 100 ms).
         */
        if (!pci_is_pcie(dev)) {
                pci_dbg(dev, "waiting %d ms for secondary bus\n", 1000 + delay);
                msleep(1000 + delay);
-               return;
+               return 0;
        }
 
        /*
@@ -5010,11 +5012,11 @@ void pci_bridge_wait_for_secondary_bus(struct pci_dev *dev)
         * configuration requests if we only wait for 100 ms (see
         * https://bugzilla.kernel.org/show_bug.cgi?id=203885).
         *
-        * Therefore we wait for 100 ms and check for the device presence.
-        * If it is still not present give it an additional 100 ms.
+        * Therefore we wait for 100 ms and check for the device presence
+        * until the timeout expires.
         */
        if (!pcie_downstream_port(dev))
-               return;
+               return 0;
 
        if (pcie_get_speed_cap(dev) <= PCIE_SPEED_5_0GT) {
                pci_dbg(dev, "waiting %d ms for downstream link\n", delay);
@@ -5025,14 +5027,11 @@ void pci_bridge_wait_for_secondary_bus(struct pci_dev *dev)
                if (!pcie_wait_for_link_delay(dev, true, delay)) {
                        /* Did not train, no need to wait any further */
                        pci_info(dev, "Data Link Layer Link Active not set in 1000 msec\n");
-                       return;
+                       return -ENOTTY;
                }
        }
 
-       if (!pci_device_is_present(child)) {
-               pci_dbg(child, "waiting additional %d ms to become accessible\n", delay);
-               msleep(delay);
-       }
+       return pci_dev_wait(child, reset_type, timeout - delay);
 }
 
 void pci_reset_secondary_bus(struct pci_dev *dev)
@@ -5051,15 +5050,6 @@ void pci_reset_secondary_bus(struct pci_dev *dev)
 
        ctrl &= ~PCI_BRIDGE_CTL_BUS_RESET;
        pci_write_config_word(dev, PCI_BRIDGE_CONTROL, ctrl);
-
-       /*
-        * Trhfa for conventional PCI is 2^25 clock cycles.
-        * Assuming a minimum 33MHz clock this results in a 1s
-        * delay before we can consider subordinate devices to
-        * be re-initialized.  PCIe has some ways to shorten this,
-        * but we don't make use of them yet.
-        */
-       ssleep(1);
 }
 
 void __weak pcibios_reset_secondary_bus(struct pci_dev *dev)
@@ -5078,7 +5068,8 @@ int pci_bridge_secondary_bus_reset(struct pci_dev *dev)
 {
        pcibios_reset_secondary_bus(dev);
 
-       return pci_dev_wait(dev, "bus reset", PCIE_RESET_READY_POLL_MS);
+       return pci_bridge_wait_for_secondary_bus(dev, "bus reset",
+                                                PCIE_RESET_READY_POLL_MS);
 }
 EXPORT_SYMBOL_GPL(pci_bridge_secondary_bus_reset);
 
@@ -6026,6 +6017,7 @@ int pcie_set_readrq(struct pci_dev *dev, int rq)
 {
        u16 v;
        int ret;
+       struct pci_host_bridge *bridge = pci_find_host_bridge(dev->bus);
 
        if (rq < 128 || rq > 4096 || !is_power_of_2(rq))
                return -EINVAL;
@@ -6044,6 +6036,15 @@ int pcie_set_readrq(struct pci_dev *dev, int rq)
 
        v = (ffs(rq) - 8) << 12;
 
+       if (bridge->no_inc_mrrs) {
+               int max_mrrs = pcie_get_readrq(dev);
+
+               if (rq > max_mrrs) {
+                       pci_info(dev, "can't set Max_Read_Request_Size to %d; max is %d\n", rq, max_mrrs);
+                       return -EINVAL;
+               }
+       }
+
        ret = pcie_capability_clear_and_set_word(dev, PCI_EXP_DEVCTL,
                                                  PCI_EXP_DEVCTL_READRQ, v);