1 // SPDX-License-Identifier: GPL-2.0+
3 * PCI Express PCI Hot Plug Driver
5 * Copyright (C) 1995,2001 Compaq Computer Corporation
6 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
7 * Copyright (C) 2001 IBM Corp.
8 * Copyright (C) 2003-2004 Intel Corporation
10 * All rights reserved.
12 * Send feedback to <greg@kroah.com>,<kristen.c.accardi@intel.com>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/types.h>
18 #include <linux/signal.h>
19 #include <linux/jiffies.h>
20 #include <linux/kthread.h>
21 #include <linux/pci.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/interrupt.h>
24 #include <linux/time.h>
25 #include <linux/slab.h>
30 static inline struct pci_dev *ctrl_dev(struct controller *ctrl)
32 return ctrl->pcie->port;
35 static irqreturn_t pciehp_isr(int irq, void *dev_id);
36 static irqreturn_t pciehp_ist(int irq, void *dev_id);
37 static int pciehp_poll(void *data);
39 static inline int pciehp_request_irq(struct controller *ctrl)
41 int retval, irq = ctrl->pcie->irq;
43 if (pciehp_poll_mode) {
44 ctrl->poll_thread = kthread_run(&pciehp_poll, ctrl,
46 slot_name(ctrl->slot));
47 return PTR_ERR_OR_ZERO(ctrl->poll_thread);
50 /* Installs the interrupt handler */
51 retval = request_threaded_irq(irq, pciehp_isr, pciehp_ist,
52 IRQF_SHARED, MY_NAME, ctrl);
54 ctrl_err(ctrl, "Cannot get irq %d for the hotplug controller\n",
59 static inline void pciehp_free_irq(struct controller *ctrl)
62 kthread_stop(ctrl->poll_thread);
64 free_irq(ctrl->pcie->irq, ctrl);
67 static int pcie_poll_cmd(struct controller *ctrl, int timeout)
69 struct pci_dev *pdev = ctrl_dev(ctrl);
73 pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
74 if (slot_status == (u16) ~0) {
75 ctrl_info(ctrl, "%s: no response from device\n",
80 if (slot_status & PCI_EXP_SLTSTA_CC) {
81 pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
92 return 0; /* timeout */
95 static void pcie_wait_cmd(struct controller *ctrl)
97 unsigned int msecs = pciehp_poll_mode ? 2500 : 1000;
98 unsigned long duration = msecs_to_jiffies(msecs);
99 unsigned long cmd_timeout = ctrl->cmd_started + duration;
100 unsigned long now, timeout;
104 * If the controller does not generate notifications for command
105 * completions, we never need to wait between writes.
107 if (NO_CMD_CMPL(ctrl))
114 * Even if the command has already timed out, we want to call
115 * pcie_poll_cmd() so it can clear PCI_EXP_SLTSTA_CC.
118 if (time_before_eq(cmd_timeout, now))
121 timeout = cmd_timeout - now;
123 if (ctrl->slot_ctrl & PCI_EXP_SLTCTL_HPIE &&
124 ctrl->slot_ctrl & PCI_EXP_SLTCTL_CCIE)
125 rc = wait_event_timeout(ctrl->queue, !ctrl->cmd_busy, timeout);
127 rc = pcie_poll_cmd(ctrl, jiffies_to_msecs(timeout));
130 ctrl_info(ctrl, "Timeout on hotplug command %#06x (issued %u msec ago)\n",
132 jiffies_to_msecs(jiffies - ctrl->cmd_started));
135 #define CC_ERRATUM_MASK (PCI_EXP_SLTCTL_PCC | \
136 PCI_EXP_SLTCTL_PIC | \
137 PCI_EXP_SLTCTL_AIC | \
140 static void pcie_do_write_cmd(struct controller *ctrl, u16 cmd,
143 struct pci_dev *pdev = ctrl_dev(ctrl);
144 u16 slot_ctrl_orig, slot_ctrl;
146 mutex_lock(&ctrl->ctrl_lock);
149 * Always wait for any previous command that might still be in progress
153 pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
154 if (slot_ctrl == (u16) ~0) {
155 ctrl_info(ctrl, "%s: no response from device\n", __func__);
159 slot_ctrl_orig = slot_ctrl;
161 slot_ctrl |= (cmd & mask);
164 pcie_capability_write_word(pdev, PCI_EXP_SLTCTL, slot_ctrl);
165 ctrl->cmd_started = jiffies;
166 ctrl->slot_ctrl = slot_ctrl;
169 * Controllers with the Intel CF118 and similar errata advertise
170 * Command Completed support, but they only set Command Completed
171 * if we change the "Control" bits for power, power indicator,
172 * attention indicator, or interlock. If we only change the
173 * "Enable" bits, they never set the Command Completed bit.
175 if (pdev->broken_cmd_compl &&
176 (slot_ctrl_orig & CC_ERRATUM_MASK) == (slot_ctrl & CC_ERRATUM_MASK))
180 * Optionally wait for the hardware to be ready for a new command,
181 * indicating completion of the above issued command.
187 mutex_unlock(&ctrl->ctrl_lock);
191 * pcie_write_cmd - Issue controller command
192 * @ctrl: controller to which the command is issued
193 * @cmd: command value written to slot control register
194 * @mask: bitmask of slot control register to be modified
196 static void pcie_write_cmd(struct controller *ctrl, u16 cmd, u16 mask)
198 pcie_do_write_cmd(ctrl, cmd, mask, true);
201 /* Same as above without waiting for the hardware to latch */
202 static void pcie_write_cmd_nowait(struct controller *ctrl, u16 cmd, u16 mask)
204 pcie_do_write_cmd(ctrl, cmd, mask, false);
207 bool pciehp_check_link_active(struct controller *ctrl)
209 struct pci_dev *pdev = ctrl_dev(ctrl);
213 pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status);
214 ret = !!(lnk_status & PCI_EXP_LNKSTA_DLLLA);
217 ctrl_dbg(ctrl, "%s: lnk_status = %x\n", __func__, lnk_status);
222 static void pcie_wait_link_active(struct controller *ctrl)
224 struct pci_dev *pdev = ctrl_dev(ctrl);
226 pcie_wait_for_link(pdev, true);
229 static bool pci_bus_check_dev(struct pci_bus *bus, int devfn)
233 int delay = 1000, step = 20;
237 found = pci_bus_read_dev_vendor_id(bus, devfn, &l, 0);
247 if (count > 1 && pciehp_debug)
248 printk(KERN_DEBUG "pci %04x:%02x:%02x.%d id reading try %d times with interval %d ms to get %08x\n",
249 pci_domain_nr(bus), bus->number, PCI_SLOT(devfn),
250 PCI_FUNC(devfn), count, step, l);
255 int pciehp_check_link_status(struct controller *ctrl)
257 struct pci_dev *pdev = ctrl_dev(ctrl);
262 * Data Link Layer Link Active Reporting must be capable for
263 * hot-plug capable downstream port. But old controller might
264 * not implement it. In this case, we wait for 1000 ms.
266 if (ctrl->link_active_reporting)
267 pcie_wait_link_active(ctrl);
271 /* wait 100ms before read pci conf, and try in 1s */
273 found = pci_bus_check_dev(ctrl->pcie->port->subordinate,
276 /* ignore link or presence changes up to this point */
278 atomic_and(~(PCI_EXP_SLTSTA_DLLSC | PCI_EXP_SLTSTA_PDC),
279 &ctrl->pending_events);
281 pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status);
282 ctrl_dbg(ctrl, "%s: lnk_status = %x\n", __func__, lnk_status);
283 if ((lnk_status & PCI_EXP_LNKSTA_LT) ||
284 !(lnk_status & PCI_EXP_LNKSTA_NLW)) {
285 ctrl_err(ctrl, "link training error: status %#06x\n",
290 pcie_update_link_speed(ctrl->pcie->port->subordinate, lnk_status);
298 static int __pciehp_link_set(struct controller *ctrl, bool enable)
300 struct pci_dev *pdev = ctrl_dev(ctrl);
303 pcie_capability_read_word(pdev, PCI_EXP_LNKCTL, &lnk_ctrl);
306 lnk_ctrl &= ~PCI_EXP_LNKCTL_LD;
308 lnk_ctrl |= PCI_EXP_LNKCTL_LD;
310 pcie_capability_write_word(pdev, PCI_EXP_LNKCTL, lnk_ctrl);
311 ctrl_dbg(ctrl, "%s: lnk_ctrl = %x\n", __func__, lnk_ctrl);
315 static int pciehp_link_enable(struct controller *ctrl)
317 return __pciehp_link_set(ctrl, true);
320 int pciehp_get_raw_indicator_status(struct hotplug_slot *hotplug_slot,
323 struct slot *slot = hotplug_slot->private;
324 struct pci_dev *pdev = ctrl_dev(slot->ctrl);
327 pci_config_pm_runtime_get(pdev);
328 pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
329 pci_config_pm_runtime_put(pdev);
330 *status = (slot_ctrl & (PCI_EXP_SLTCTL_AIC | PCI_EXP_SLTCTL_PIC)) >> 6;
334 void pciehp_get_attention_status(struct slot *slot, u8 *status)
336 struct controller *ctrl = slot->ctrl;
337 struct pci_dev *pdev = ctrl_dev(ctrl);
340 pci_config_pm_runtime_get(pdev);
341 pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
342 pci_config_pm_runtime_put(pdev);
343 ctrl_dbg(ctrl, "%s: SLOTCTRL %x, value read %x\n", __func__,
344 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, slot_ctrl);
346 switch (slot_ctrl & PCI_EXP_SLTCTL_AIC) {
347 case PCI_EXP_SLTCTL_ATTN_IND_ON:
348 *status = 1; /* On */
350 case PCI_EXP_SLTCTL_ATTN_IND_BLINK:
351 *status = 2; /* Blink */
353 case PCI_EXP_SLTCTL_ATTN_IND_OFF:
354 *status = 0; /* Off */
362 void pciehp_get_power_status(struct slot *slot, u8 *status)
364 struct controller *ctrl = slot->ctrl;
365 struct pci_dev *pdev = ctrl_dev(ctrl);
368 pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
369 ctrl_dbg(ctrl, "%s: SLOTCTRL %x value read %x\n", __func__,
370 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, slot_ctrl);
372 switch (slot_ctrl & PCI_EXP_SLTCTL_PCC) {
373 case PCI_EXP_SLTCTL_PWR_ON:
374 *status = 1; /* On */
376 case PCI_EXP_SLTCTL_PWR_OFF:
377 *status = 0; /* Off */
385 void pciehp_get_latch_status(struct slot *slot, u8 *status)
387 struct pci_dev *pdev = ctrl_dev(slot->ctrl);
390 pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
391 *status = !!(slot_status & PCI_EXP_SLTSTA_MRLSS);
394 void pciehp_get_adapter_status(struct slot *slot, u8 *status)
396 struct pci_dev *pdev = ctrl_dev(slot->ctrl);
399 pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
400 *status = !!(slot_status & PCI_EXP_SLTSTA_PDS);
403 int pciehp_query_power_fault(struct slot *slot)
405 struct pci_dev *pdev = ctrl_dev(slot->ctrl);
408 pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
409 return !!(slot_status & PCI_EXP_SLTSTA_PFD);
412 int pciehp_set_raw_indicator_status(struct hotplug_slot *hotplug_slot,
415 struct slot *slot = hotplug_slot->private;
416 struct controller *ctrl = slot->ctrl;
417 struct pci_dev *pdev = ctrl_dev(ctrl);
419 pci_config_pm_runtime_get(pdev);
420 pcie_write_cmd_nowait(ctrl, status << 6,
421 PCI_EXP_SLTCTL_AIC | PCI_EXP_SLTCTL_PIC);
422 pci_config_pm_runtime_put(pdev);
426 void pciehp_set_attention_status(struct slot *slot, u8 value)
428 struct controller *ctrl = slot->ctrl;
435 case 0: /* turn off */
436 slot_cmd = PCI_EXP_SLTCTL_ATTN_IND_OFF;
438 case 1: /* turn on */
439 slot_cmd = PCI_EXP_SLTCTL_ATTN_IND_ON;
441 case 2: /* turn blink */
442 slot_cmd = PCI_EXP_SLTCTL_ATTN_IND_BLINK;
447 pcie_write_cmd_nowait(ctrl, slot_cmd, PCI_EXP_SLTCTL_AIC);
448 ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
449 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, slot_cmd);
452 void pciehp_green_led_on(struct slot *slot)
454 struct controller *ctrl = slot->ctrl;
459 pcie_write_cmd_nowait(ctrl, PCI_EXP_SLTCTL_PWR_IND_ON,
461 ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
462 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
463 PCI_EXP_SLTCTL_PWR_IND_ON);
466 void pciehp_green_led_off(struct slot *slot)
468 struct controller *ctrl = slot->ctrl;
473 pcie_write_cmd_nowait(ctrl, PCI_EXP_SLTCTL_PWR_IND_OFF,
475 ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
476 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
477 PCI_EXP_SLTCTL_PWR_IND_OFF);
480 void pciehp_green_led_blink(struct slot *slot)
482 struct controller *ctrl = slot->ctrl;
487 pcie_write_cmd_nowait(ctrl, PCI_EXP_SLTCTL_PWR_IND_BLINK,
489 ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
490 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
491 PCI_EXP_SLTCTL_PWR_IND_BLINK);
494 int pciehp_power_on_slot(struct slot *slot)
496 struct controller *ctrl = slot->ctrl;
497 struct pci_dev *pdev = ctrl_dev(ctrl);
501 /* Clear power-fault bit from previous power failures */
502 pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
503 if (slot_status & PCI_EXP_SLTSTA_PFD)
504 pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
506 ctrl->power_fault_detected = 0;
508 pcie_write_cmd(ctrl, PCI_EXP_SLTCTL_PWR_ON, PCI_EXP_SLTCTL_PCC);
509 ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
510 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
511 PCI_EXP_SLTCTL_PWR_ON);
513 retval = pciehp_link_enable(ctrl);
515 ctrl_err(ctrl, "%s: Can not enable the link!\n", __func__);
520 void pciehp_power_off_slot(struct slot *slot)
522 struct controller *ctrl = slot->ctrl;
524 pcie_write_cmd(ctrl, PCI_EXP_SLTCTL_PWR_OFF, PCI_EXP_SLTCTL_PCC);
525 ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
526 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
527 PCI_EXP_SLTCTL_PWR_OFF);
530 static irqreturn_t pciehp_isr(int irq, void *dev_id)
532 struct controller *ctrl = (struct controller *)dev_id;
533 struct pci_dev *pdev = ctrl_dev(ctrl);
534 struct device *parent = pdev->dev.parent;
535 u16 status, events = 0;
538 * Interrupts only occur in D3hot or shallower (PCIe r4.0, sec 6.7.3.4).
540 if (pdev->current_state == PCI_D3cold)
544 * Keep the port accessible by holding a runtime PM ref on its parent.
545 * Defer resume of the parent to the IRQ thread if it's suspended.
546 * Mask the interrupt until then.
549 pm_runtime_get_noresume(parent);
550 if (!pm_runtime_active(parent)) {
551 pm_runtime_put(parent);
552 disable_irq_nosync(irq);
553 atomic_or(RERUN_ISR, &ctrl->pending_events);
554 return IRQ_WAKE_THREAD;
559 pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &status);
560 if (status == (u16) ~0) {
561 ctrl_info(ctrl, "%s: no response from device\n", __func__);
563 pm_runtime_put(parent);
568 * Slot Status contains plain status bits as well as event
569 * notification bits; right now we only want the event bits.
571 status &= PCI_EXP_SLTSTA_ABP | PCI_EXP_SLTSTA_PFD |
572 PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_CC |
573 PCI_EXP_SLTSTA_DLLSC;
576 * If we've already reported a power fault, don't report it again
577 * until we've done something to handle it.
579 if (ctrl->power_fault_detected)
580 status &= ~PCI_EXP_SLTSTA_PFD;
581 else if (status & PCI_EXP_SLTSTA_PFD)
582 ctrl->power_fault_detected = true;
587 pm_runtime_put(parent);
592 pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, status);
595 * In MSI mode, all event bits must be zero before the port
596 * will send a new interrupt (PCIe Base Spec r5.0 sec 6.7.3.4).
597 * So re-read the Slot Status register in case a bit was set
598 * between read and write.
600 if (pci_dev_msi_enabled(pdev) && !pciehp_poll_mode)
604 ctrl_dbg(ctrl, "pending interrupts %#06x from Slot Status\n", events);
606 pm_runtime_put(parent);
609 * Command Completed notifications are not deferred to the
610 * IRQ thread because it may be waiting for their arrival.
612 if (events & PCI_EXP_SLTSTA_CC) {
615 wake_up(&ctrl->queue);
617 if (events == PCI_EXP_SLTSTA_CC)
620 events &= ~PCI_EXP_SLTSTA_CC;
623 if (pdev->ignore_hotplug) {
624 ctrl_dbg(ctrl, "ignoring hotplug event %#06x\n", events);
628 /* Save pending events for consumption by IRQ thread. */
629 atomic_or(events, &ctrl->pending_events);
630 return IRQ_WAKE_THREAD;
633 static irqreturn_t pciehp_ist(int irq, void *dev_id)
635 struct controller *ctrl = (struct controller *)dev_id;
636 struct pci_dev *pdev = ctrl_dev(ctrl);
637 struct slot *slot = ctrl->slot;
641 ctrl->ist_running = true;
642 pci_config_pm_runtime_get(pdev);
644 /* rerun pciehp_isr() if the port was inaccessible on interrupt */
645 if (atomic_fetch_and(~RERUN_ISR, &ctrl->pending_events) & RERUN_ISR) {
646 ret = pciehp_isr(irq, dev_id);
648 if (ret != IRQ_WAKE_THREAD)
652 synchronize_hardirq(irq);
653 events = atomic_xchg(&ctrl->pending_events, 0);
659 /* Check Attention Button Pressed */
660 if (events & PCI_EXP_SLTSTA_ABP) {
661 ctrl_info(ctrl, "Slot(%s): Attention button pressed\n",
663 pciehp_handle_button_press(slot);
666 /* Check Power Fault Detected */
667 if (events & PCI_EXP_SLTSTA_PFD) {
668 ctrl_err(ctrl, "Slot(%s): Power fault\n", slot_name(slot));
669 pciehp_set_attention_status(slot, 1);
670 pciehp_green_led_off(slot);
674 * Disable requests have higher priority than Presence Detect Changed
675 * or Data Link Layer State Changed events.
677 down_read_nested(&ctrl->reset_lock, ctrl->depth);
678 if (events & DISABLE_SLOT)
679 pciehp_handle_disable_request(slot);
680 else if (events & (PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC))
681 pciehp_handle_presence_or_link_change(slot, events);
682 up_read(&ctrl->reset_lock);
686 pci_config_pm_runtime_put(pdev);
687 ctrl->ist_running = false;
688 wake_up(&ctrl->requester);
692 static int pciehp_poll(void *data)
694 struct controller *ctrl = data;
696 schedule_timeout_idle(10 * HZ); /* start with 10 sec delay */
698 while (!kthread_should_stop()) {
699 /* poll for interrupt events or user requests */
700 while (pciehp_isr(IRQ_NOTCONNECTED, ctrl) == IRQ_WAKE_THREAD ||
701 atomic_read(&ctrl->pending_events))
702 pciehp_ist(IRQ_NOTCONNECTED, ctrl);
704 if (pciehp_poll_time <= 0 || pciehp_poll_time > 60)
705 pciehp_poll_time = 2; /* clamp to sane value */
707 schedule_timeout_idle(pciehp_poll_time * HZ);
713 static void pcie_enable_notification(struct controller *ctrl)
718 * TBD: Power fault detected software notification support.
720 * Power fault detected software notification is not enabled
721 * now, because it caused power fault detected interrupt storm
722 * on some machines. On those machines, power fault detected
723 * bit in the slot status register was set again immediately
724 * when it is cleared in the interrupt service routine, and
725 * next power fault detected interrupt was notified again.
729 * Always enable link events: thus link-up and link-down shall
730 * always be treated as hotplug and unplug respectively. Enable
731 * presence detect only if Attention Button is not present.
733 cmd = PCI_EXP_SLTCTL_DLLSCE;
734 if (ATTN_BUTTN(ctrl))
735 cmd |= PCI_EXP_SLTCTL_ABPE;
737 cmd |= PCI_EXP_SLTCTL_PDCE;
738 if (!pciehp_poll_mode)
739 cmd |= PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE;
741 mask = (PCI_EXP_SLTCTL_PDCE | PCI_EXP_SLTCTL_ABPE |
742 PCI_EXP_SLTCTL_PFDE |
743 PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE |
744 PCI_EXP_SLTCTL_DLLSCE);
746 pcie_write_cmd_nowait(ctrl, cmd, mask);
747 ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
748 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, cmd);
751 static void pcie_disable_notification(struct controller *ctrl)
755 mask = (PCI_EXP_SLTCTL_PDCE | PCI_EXP_SLTCTL_ABPE |
756 PCI_EXP_SLTCTL_MRLSCE | PCI_EXP_SLTCTL_PFDE |
757 PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE |
758 PCI_EXP_SLTCTL_DLLSCE);
759 pcie_write_cmd(ctrl, 0, mask);
760 ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
761 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, 0);
764 void pcie_clear_hotplug_events(struct controller *ctrl)
766 pcie_capability_write_word(ctrl_dev(ctrl), PCI_EXP_SLTSTA,
767 PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC);
771 * pciehp has a 1:1 bus:slot relationship so we ultimately want a secondary
772 * bus reset of the bridge, but at the same time we want to ensure that it is
773 * not seen as a hot-unplug, followed by the hot-plug of the device. Thus,
774 * disable link state notification and presence detection change notification
775 * momentarily, if we see that they could interfere. Also, clear any spurious
778 int pciehp_reset_slot(struct slot *slot, int probe)
780 struct controller *ctrl = slot->ctrl;
781 struct pci_dev *pdev = ctrl_dev(ctrl);
782 u16 stat_mask = 0, ctrl_mask = 0;
788 down_write_nested(&ctrl->reset_lock, ctrl->depth);
790 if (!ATTN_BUTTN(ctrl)) {
791 ctrl_mask |= PCI_EXP_SLTCTL_PDCE;
792 stat_mask |= PCI_EXP_SLTSTA_PDC;
794 ctrl_mask |= PCI_EXP_SLTCTL_DLLSCE;
795 stat_mask |= PCI_EXP_SLTSTA_DLLSC;
797 pcie_write_cmd(ctrl, 0, ctrl_mask);
798 ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
799 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, 0);
801 rc = pci_bridge_secondary_bus_reset(ctrl->pcie->port);
803 pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, stat_mask);
804 pcie_write_cmd_nowait(ctrl, ctrl_mask, ctrl_mask);
805 ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
806 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, ctrl_mask);
808 up_write(&ctrl->reset_lock);
812 int pcie_init_notification(struct controller *ctrl)
814 if (pciehp_request_irq(ctrl))
816 pcie_enable_notification(ctrl);
817 ctrl->notification_enabled = 1;
821 void pcie_shutdown_notification(struct controller *ctrl)
823 if (ctrl->notification_enabled) {
824 pcie_disable_notification(ctrl);
825 pciehp_free_irq(ctrl);
826 ctrl->notification_enabled = 0;
830 static int pcie_init_slot(struct controller *ctrl)
832 struct pci_bus *subordinate = ctrl_dev(ctrl)->subordinate;
835 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
839 down_read(&pci_bus_sem);
840 slot->state = list_empty(&subordinate->devices) ? OFF_STATE : ON_STATE;
841 up_read(&pci_bus_sem);
844 mutex_init(&slot->lock);
845 INIT_DELAYED_WORK(&slot->work, pciehp_queue_pushbutton_work);
850 static void pcie_cleanup_slot(struct controller *ctrl)
852 struct slot *slot = ctrl->slot;
854 cancel_delayed_work_sync(&slot->work);
858 static inline void dbg_ctrl(struct controller *ctrl)
860 struct pci_dev *pdev = ctrl->pcie->port;
866 ctrl_info(ctrl, "Slot Capabilities : 0x%08x\n", ctrl->slot_cap);
867 pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, ®16);
868 ctrl_info(ctrl, "Slot Status : 0x%04x\n", reg16);
869 pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, ®16);
870 ctrl_info(ctrl, "Slot Control : 0x%04x\n", reg16);
873 #define FLAG(x, y) (((x) & (y)) ? '+' : '-')
875 static inline int pcie_hotplug_depth(struct pci_dev *dev)
877 struct pci_bus *bus = dev->bus;
880 while (bus->parent) {
882 if (bus->self && bus->self->is_hotplug_bridge)
889 struct controller *pcie_init(struct pcie_device *dev)
891 struct controller *ctrl;
892 u32 slot_cap, link_cap;
893 u8 occupied, poweron;
894 struct pci_dev *pdev = dev->port;
896 ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
901 ctrl->depth = pcie_hotplug_depth(dev->port);
902 pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &slot_cap);
904 if (pdev->hotplug_user_indicators)
905 slot_cap &= ~(PCI_EXP_SLTCAP_AIP | PCI_EXP_SLTCAP_PIP);
908 * We assume no Thunderbolt controllers support Command Complete events,
909 * but some controllers falsely claim they do.
911 if (pdev->is_thunderbolt)
912 slot_cap |= PCI_EXP_SLTCAP_NCCS;
914 ctrl->slot_cap = slot_cap;
915 mutex_init(&ctrl->ctrl_lock);
916 init_rwsem(&ctrl->reset_lock);
917 init_waitqueue_head(&ctrl->requester);
918 init_waitqueue_head(&ctrl->queue);
921 /* Check if Data Link Layer Link Active Reporting is implemented */
922 pcie_capability_read_dword(pdev, PCI_EXP_LNKCAP, &link_cap);
923 if (link_cap & PCI_EXP_LNKCAP_DLLLARC)
924 ctrl->link_active_reporting = 1;
926 /* Clear all remaining event bits in Slot Status register. */
927 pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
928 PCI_EXP_SLTSTA_ABP | PCI_EXP_SLTSTA_PFD |
929 PCI_EXP_SLTSTA_MRLSC | PCI_EXP_SLTSTA_CC |
930 PCI_EXP_SLTSTA_DLLSC | PCI_EXP_SLTSTA_PDC);
932 ctrl_info(ctrl, "Slot #%d AttnBtn%c PwrCtrl%c MRL%c AttnInd%c PwrInd%c HotPlug%c Surprise%c Interlock%c NoCompl%c LLActRep%c%s\n",
933 (slot_cap & PCI_EXP_SLTCAP_PSN) >> 19,
934 FLAG(slot_cap, PCI_EXP_SLTCAP_ABP),
935 FLAG(slot_cap, PCI_EXP_SLTCAP_PCP),
936 FLAG(slot_cap, PCI_EXP_SLTCAP_MRLSP),
937 FLAG(slot_cap, PCI_EXP_SLTCAP_AIP),
938 FLAG(slot_cap, PCI_EXP_SLTCAP_PIP),
939 FLAG(slot_cap, PCI_EXP_SLTCAP_HPC),
940 FLAG(slot_cap, PCI_EXP_SLTCAP_HPS),
941 FLAG(slot_cap, PCI_EXP_SLTCAP_EIP),
942 FLAG(slot_cap, PCI_EXP_SLTCAP_NCCS),
943 FLAG(link_cap, PCI_EXP_LNKCAP_DLLLARC),
944 pdev->broken_cmd_compl ? " (with Cmd Compl erratum)" : "");
946 if (pcie_init_slot(ctrl))
950 * If empty slot's power status is on, turn power off. The IRQ isn't
951 * requested yet, so avoid triggering a notification with this command.
953 if (POWER_CTRL(ctrl)) {
954 pciehp_get_adapter_status(ctrl->slot, &occupied);
955 pciehp_get_power_status(ctrl->slot, &poweron);
956 if (!occupied && poweron) {
957 pcie_disable_notification(ctrl);
958 pciehp_power_off_slot(ctrl->slot);
970 void pciehp_release_ctrl(struct controller *ctrl)
972 pcie_cleanup_slot(ctrl);
976 static void quirk_cmd_compl(struct pci_dev *pdev)
980 if (pci_is_pcie(pdev)) {
981 pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &slot_cap);
982 if (slot_cap & PCI_EXP_SLTCAP_HPC &&
983 !(slot_cap & PCI_EXP_SLTCAP_NCCS))
984 pdev->broken_cmd_compl = 1;
987 DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_INTEL, PCI_ANY_ID,
988 PCI_CLASS_BRIDGE_PCI, 8, quirk_cmd_compl);
989 DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_QCOM, 0x0110,
990 PCI_CLASS_BRIDGE_PCI, 8, quirk_cmd_compl);
991 DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_QCOM, 0x0400,
992 PCI_CLASS_BRIDGE_PCI, 8, quirk_cmd_compl);
993 DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_QCOM, 0x0401,
994 PCI_CLASS_BRIDGE_PCI, 8, quirk_cmd_compl);