PCI: pciehp: Remove assumptions about which commands cause completion events
[cascardo/linux.git] / drivers / pci / hotplug / pciehp_hpc.c
index 1463412..a3a5c65 100644 (file)
@@ -104,11 +104,10 @@ static inline void pciehp_free_irq(struct controller *ctrl)
                free_irq(ctrl->pcie->irq, ctrl);
 }
 
-static int pcie_poll_cmd(struct controller *ctrl)
+static int pcie_poll_cmd(struct controller *ctrl, int timeout)
 {
        struct pci_dev *pdev = ctrl_dev(ctrl);
        u16 slot_status;
-       int timeout = 1000;
 
        pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
        if (slot_status & PCI_EXP_SLTSTA_CC) {
@@ -129,18 +128,52 @@ static int pcie_poll_cmd(struct controller *ctrl)
        return 0;       /* timeout */
 }
 
-static void pcie_wait_cmd(struct controller *ctrl, int poll)
+static void pcie_wait_cmd(struct controller *ctrl)
 {
        unsigned int msecs = pciehp_poll_mode ? 2500 : 1000;
-       unsigned long timeout = msecs_to_jiffies(msecs);
+       unsigned long duration = msecs_to_jiffies(msecs);
+       unsigned long cmd_timeout = ctrl->cmd_started + duration;
+       unsigned long now, timeout;
        int rc;
 
-       if (poll)
-               rc = pcie_poll_cmd(ctrl);
+       /*
+        * If the controller does not generate notifications for command
+        * completions, we never need to wait between writes.
+        */
+       if (ctrl->no_cmd_complete)
+               return;
+
+       if (!ctrl->cmd_busy)
+               return;
+
+       /*
+        * Even if the command has already timed out, we want to call
+        * pcie_poll_cmd() so it can clear PCI_EXP_SLTSTA_CC.
+        */
+       now = jiffies;
+       if (time_before_eq(cmd_timeout, now))
+               timeout = 1;
        else
+               timeout = cmd_timeout - now;
+
+       if (ctrl->slot_ctrl & PCI_EXP_SLTCTL_HPIE &&
+           ctrl->slot_ctrl & PCI_EXP_SLTCTL_CCIE)
                rc = wait_event_timeout(ctrl->queue, !ctrl->cmd_busy, timeout);
+       else
+               rc = pcie_poll_cmd(ctrl, timeout);
+
+       /*
+        * Controllers with errata like Intel CF118 don't generate
+        * completion notifications unless the power/indicator/interlock
+        * control bits are changed.  On such controllers, we'll emit this
+        * timeout message when we wait for completion of commands that
+        * don't change those bits, e.g., commands that merely enable
+        * interrupts.
+        */
        if (!rc)
-               ctrl_dbg(ctrl, "Command not completed in 1000 msec\n");
+               ctrl_info(ctrl, "Timeout on hotplug command %#010x (issued %u msec ago)\n",
+                         ctrl->slot_ctrl,
+                         jiffies_to_msecs(now - ctrl->cmd_started));
 }
 
 /**
@@ -152,36 +185,12 @@ static void pcie_wait_cmd(struct controller *ctrl, int poll)
 static void pcie_write_cmd(struct controller *ctrl, u16 cmd, u16 mask)
 {
        struct pci_dev *pdev = ctrl_dev(ctrl);
-       u16 slot_status;
        u16 slot_ctrl;
 
        mutex_lock(&ctrl->ctrl_lock);
 
-       pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
-       if (slot_status & PCI_EXP_SLTSTA_CC) {
-               pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
-                                          PCI_EXP_SLTSTA_CC);
-               if (!ctrl->no_cmd_complete) {
-                       /*
-                        * After 1 sec and CMD_COMPLETED still not set, just
-                        * proceed forward to issue the next command according
-                        * to spec. Just print out the error message.
-                        */
-                       ctrl_dbg(ctrl, "CMD_COMPLETED not clear after 1 sec\n");
-               } else if (!NO_CMD_CMPL(ctrl)) {
-                       /*
-                        * This controller seems to notify of command completed
-                        * event even though it supports none of power
-                        * controller, attention led, power led and EMI.
-                        */
-                       ctrl_dbg(ctrl, "Unexpected CMD_COMPLETED. Need to "
-                                "wait for command completed event.\n");
-                       ctrl->no_cmd_complete = 0;
-               } else {
-                       ctrl_dbg(ctrl, "Unexpected CMD_COMPLETED. Maybe "
-                                "the controller is broken.\n");
-               }
-       }
+       /* Wait for any previous command that might still be in progress */
+       pcie_wait_cmd(ctrl);
 
        pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
        slot_ctrl &= ~mask;
@@ -189,22 +198,9 @@ static void pcie_write_cmd(struct controller *ctrl, u16 cmd, u16 mask)
        ctrl->cmd_busy = 1;
        smp_mb();
        pcie_capability_write_word(pdev, PCI_EXP_SLTCTL, slot_ctrl);
+       ctrl->cmd_started = jiffies;
+       ctrl->slot_ctrl = slot_ctrl;
 
-       /*
-        * Wait for command completion.
-        */
-       if (!ctrl->no_cmd_complete) {
-               int poll = 0;
-               /*
-                * if hotplug interrupt is not enabled or command
-                * completed interrupt is not enabled, we need to poll
-                * command completed event.
-                */
-               if (!(slot_ctrl & PCI_EXP_SLTCTL_HPIE) ||
-                   !(slot_ctrl & PCI_EXP_SLTCTL_CCIE))
-                       poll = 1;
-                pcie_wait_cmd(ctrl, poll);
-       }
        mutex_unlock(&ctrl->ctrl_lock);
 }
 
@@ -276,15 +272,15 @@ int pciehp_check_link_status(struct controller *ctrl)
        bool found;
        u16 lnk_status;
 
-        /*
-         * Data Link Layer Link Active Reporting must be capable for
-         * hot-plug capable downstream port. But old controller might
-         * not implement it. In this case, we wait for 1000 ms.
-         */
-        if (ctrl->link_active_reporting)
-                pcie_wait_link_active(ctrl);
-        else
-                msleep(1000);
+       /*
+        * Data Link Layer Link Active Reporting must be capable for
+        * hot-plug capable downstream port. But old controller might
+        * not implement it. In this case, we wait for 1000 ms.
+       */
+       if (ctrl->link_active_reporting)
+               pcie_wait_link_active(ctrl);
+       else
+               msleep(1000);
 
        /* wait 100ms before read pci conf, and try in 1s */
        msleep(100);
@@ -295,7 +291,7 @@ int pciehp_check_link_status(struct controller *ctrl)
        ctrl_dbg(ctrl, "%s: lnk_status = %x\n", __func__, lnk_status);
        if ((lnk_status & PCI_EXP_LNKSTA_LT) ||
            !(lnk_status & PCI_EXP_LNKSTA_NLW)) {
-               ctrl_err(ctrl, "Link Training Error occurs \n");
+               ctrl_err(ctrl, "Link Training Error occurs\n");
                return -1;
        }
 
@@ -414,7 +410,7 @@ void pciehp_set_attention_status(struct slot *slot, u8 value)
                return;
 
        switch (value) {
-       case 0 :        /* turn off */
+       case 0        /* turn off */
                slot_cmd = PCI_EXP_SLTCTL_ATTN_IND_OFF;
                break;
        case 1:         /* turn on */
@@ -470,7 +466,7 @@ void pciehp_green_led_blink(struct slot *slot)
                 PCI_EXP_SLTCTL_PWR_IND_BLINK);
 }
 
-int pciehp_power_on_slot(struct slot * slot)
+int pciehp_power_on_slot(struct slot *slot)
 {
        struct controller *ctrl = slot->ctrl;
        struct pci_dev *pdev = ctrl_dev(ctrl);
@@ -496,7 +492,7 @@ int pciehp_power_on_slot(struct slot * slot)
        return retval;
 }
 
-void pciehp_power_off_slot(struct slot * slot)
+void pciehp_power_off_slot(struct slot *slot)
 {
        struct controller *ctrl = slot->ctrl;
 
@@ -756,7 +752,7 @@ static inline void dbg_ctrl(struct controller *ctrl)
        ctrl_info(ctrl, "Slot Control           : 0x%04x\n", reg16);
 }
 
-#define FLAG(x,y)      (((x) & (y)) ? '+' : '-')
+#define FLAG(x, y)     (((x) & (y)) ? '+' : '-')
 
 struct controller *pcie_init(struct pcie_device *dev)
 {
@@ -775,22 +771,22 @@ struct controller *pcie_init(struct pcie_device *dev)
        mutex_init(&ctrl->ctrl_lock);
        init_waitqueue_head(&ctrl->queue);
        dbg_ctrl(ctrl);
+
        /*
         * Controller doesn't notify of command completion if the "No
-        * Command Completed Support" bit is set in Slot Capability
-        * register or the controller supports none of power
-        * controller, attention led, power led and EMI.
+        * Command Completed Support" bit is set in Slot Capabilities.
+        * If set, it means the controller can accept hotplug commands
+        * with no delay between them.
         */
-       if (NO_CMD_CMPL(ctrl) ||
-           !(POWER_CTRL(ctrl) | ATTN_LED(ctrl) | PWR_LED(ctrl) | EMI(ctrl)))
-           ctrl->no_cmd_complete = 1;
-
-        /* Check if Data Link Layer Link Active Reporting is implemented */
-        pcie_capability_read_dword(pdev, PCI_EXP_LNKCAP, &link_cap);
-        if (link_cap & PCI_EXP_LNKCAP_DLLLARC) {
-                ctrl_dbg(ctrl, "Link Active Reporting supported\n");
-                ctrl->link_active_reporting = 1;
-        }
+       if (NO_CMD_CMPL(ctrl))
+               ctrl->no_cmd_complete = 1;
+
+       /* Check if Data Link Layer Link Active Reporting is implemented */
+       pcie_capability_read_dword(pdev, PCI_EXP_LNKCAP, &link_cap);
+       if (link_cap & PCI_EXP_LNKCAP_DLLLARC) {
+               ctrl_dbg(ctrl, "Link Active Reporting supported\n");
+               ctrl->link_active_reporting = 1;
+       }
 
        /* Clear all remaining event bits in Slot Status register */
        pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,