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/timer.h>
21 #include <linux/pci.h>
22 #include <linux/interrupt.h>
23 #include <linux/time.h>
24 #include <linux/slab.h>
29 static inline struct pci_dev
*ctrl_dev(struct controller
*ctrl
)
31 return ctrl
->pcie
->port
;
34 static irqreturn_t
pcie_isr(int irq
, void *dev_id
);
35 static void start_int_poll_timer(struct controller
*ctrl
, int sec
);
37 /* This is the interrupt polling timeout function. */
38 static void int_poll_timeout(struct timer_list
*t
)
40 struct controller
*ctrl
= from_timer(ctrl
, t
, poll_timer
);
42 /* Poll for interrupt events. regs == NULL => polling */
45 if (!pciehp_poll_time
)
46 pciehp_poll_time
= 2; /* default polling interval is 2 sec */
48 start_int_poll_timer(ctrl
, pciehp_poll_time
);
51 /* This function starts the interrupt polling timer. */
52 static void start_int_poll_timer(struct controller
*ctrl
, int sec
)
54 /* Clamp to sane value */
55 if ((sec
<= 0) || (sec
> 60))
58 ctrl
->poll_timer
.expires
= jiffies
+ sec
* HZ
;
59 add_timer(&ctrl
->poll_timer
);
62 static inline int pciehp_request_irq(struct controller
*ctrl
)
64 int retval
, irq
= ctrl
->pcie
->irq
;
66 /* Install interrupt polling timer. Start with 10 sec delay */
67 if (pciehp_poll_mode
) {
68 timer_setup(&ctrl
->poll_timer
, int_poll_timeout
, 0);
69 start_int_poll_timer(ctrl
, 10);
73 /* Installs the interrupt handler */
74 retval
= request_irq(irq
, pcie_isr
, IRQF_SHARED
, MY_NAME
, ctrl
);
76 ctrl_err(ctrl
, "Cannot get irq %d for the hotplug controller\n",
81 static inline void pciehp_free_irq(struct controller
*ctrl
)
84 del_timer_sync(&ctrl
->poll_timer
);
86 free_irq(ctrl
->pcie
->irq
, ctrl
);
89 static int pcie_poll_cmd(struct controller
*ctrl
, int timeout
)
91 struct pci_dev
*pdev
= ctrl_dev(ctrl
);
95 pcie_capability_read_word(pdev
, PCI_EXP_SLTSTA
, &slot_status
);
96 if (slot_status
== (u16
) ~0) {
97 ctrl_info(ctrl
, "%s: no response from device\n",
102 if (slot_status
& PCI_EXP_SLTSTA_CC
) {
103 pcie_capability_write_word(pdev
, PCI_EXP_SLTSTA
,
112 return 0; /* timeout */
115 static void pcie_wait_cmd(struct controller
*ctrl
)
117 unsigned int msecs
= pciehp_poll_mode
? 2500 : 1000;
118 unsigned long duration
= msecs_to_jiffies(msecs
);
119 unsigned long cmd_timeout
= ctrl
->cmd_started
+ duration
;
120 unsigned long now
, timeout
;
124 * If the controller does not generate notifications for command
125 * completions, we never need to wait between writes.
127 if (NO_CMD_CMPL(ctrl
))
134 * Even if the command has already timed out, we want to call
135 * pcie_poll_cmd() so it can clear PCI_EXP_SLTSTA_CC.
138 if (time_before_eq(cmd_timeout
, now
))
141 timeout
= cmd_timeout
- now
;
143 if (ctrl
->slot_ctrl
& PCI_EXP_SLTCTL_HPIE
&&
144 ctrl
->slot_ctrl
& PCI_EXP_SLTCTL_CCIE
)
145 rc
= wait_event_timeout(ctrl
->queue
, !ctrl
->cmd_busy
, timeout
);
147 rc
= pcie_poll_cmd(ctrl
, jiffies_to_msecs(timeout
));
150 ctrl_info(ctrl
, "Timeout on hotplug command %#06x (issued %u msec ago)\n",
152 jiffies_to_msecs(jiffies
- ctrl
->cmd_started
));
155 #define CC_ERRATUM_MASK (PCI_EXP_SLTCTL_PCC | \
156 PCI_EXP_SLTCTL_PIC | \
157 PCI_EXP_SLTCTL_AIC | \
160 static void pcie_do_write_cmd(struct controller
*ctrl
, u16 cmd
,
163 struct pci_dev
*pdev
= ctrl_dev(ctrl
);
164 u16 slot_ctrl_orig
, slot_ctrl
;
166 mutex_lock(&ctrl
->ctrl_lock
);
169 * Always wait for any previous command that might still be in progress
173 pcie_capability_read_word(pdev
, PCI_EXP_SLTCTL
, &slot_ctrl
);
174 if (slot_ctrl
== (u16
) ~0) {
175 ctrl_info(ctrl
, "%s: no response from device\n", __func__
);
179 slot_ctrl_orig
= slot_ctrl
;
181 slot_ctrl
|= (cmd
& mask
);
184 pcie_capability_write_word(pdev
, PCI_EXP_SLTCTL
, slot_ctrl
);
185 ctrl
->cmd_started
= jiffies
;
186 ctrl
->slot_ctrl
= slot_ctrl
;
189 * Controllers with the Intel CF118 and similar errata advertise
190 * Command Completed support, but they only set Command Completed
191 * if we change the "Control" bits for power, power indicator,
192 * attention indicator, or interlock. If we only change the
193 * "Enable" bits, they never set the Command Completed bit.
195 if (pdev
->broken_cmd_compl
&&
196 (slot_ctrl_orig
& CC_ERRATUM_MASK
) == (slot_ctrl
& CC_ERRATUM_MASK
))
200 * Optionally wait for the hardware to be ready for a new command,
201 * indicating completion of the above issued command.
207 mutex_unlock(&ctrl
->ctrl_lock
);
211 * pcie_write_cmd - Issue controller command
212 * @ctrl: controller to which the command is issued
213 * @cmd: command value written to slot control register
214 * @mask: bitmask of slot control register to be modified
216 static void pcie_write_cmd(struct controller
*ctrl
, u16 cmd
, u16 mask
)
218 pcie_do_write_cmd(ctrl
, cmd
, mask
, true);
221 /* Same as above without waiting for the hardware to latch */
222 static void pcie_write_cmd_nowait(struct controller
*ctrl
, u16 cmd
, u16 mask
)
224 pcie_do_write_cmd(ctrl
, cmd
, mask
, false);
227 bool pciehp_check_link_active(struct controller
*ctrl
)
229 struct pci_dev
*pdev
= ctrl_dev(ctrl
);
233 pcie_capability_read_word(pdev
, PCI_EXP_LNKSTA
, &lnk_status
);
234 ret
= !!(lnk_status
& PCI_EXP_LNKSTA_DLLLA
);
237 ctrl_dbg(ctrl
, "%s: lnk_status = %x\n", __func__
, lnk_status
);
242 static void pcie_wait_link_active(struct controller
*ctrl
)
244 struct pci_dev
*pdev
= ctrl_dev(ctrl
);
246 pcie_wait_for_link(pdev
, true);
249 static bool pci_bus_check_dev(struct pci_bus
*bus
, int devfn
)
253 int delay
= 1000, step
= 20;
257 found
= pci_bus_read_dev_vendor_id(bus
, devfn
, &l
, 0);
267 if (count
> 1 && pciehp_debug
)
268 printk(KERN_DEBUG
"pci %04x:%02x:%02x.%d id reading try %d times with interval %d ms to get %08x\n",
269 pci_domain_nr(bus
), bus
->number
, PCI_SLOT(devfn
),
270 PCI_FUNC(devfn
), count
, step
, l
);
275 int pciehp_check_link_status(struct controller
*ctrl
)
277 struct pci_dev
*pdev
= ctrl_dev(ctrl
);
282 * Data Link Layer Link Active Reporting must be capable for
283 * hot-plug capable downstream port. But old controller might
284 * not implement it. In this case, we wait for 1000 ms.
286 if (ctrl
->link_active_reporting
)
287 pcie_wait_link_active(ctrl
);
291 /* wait 100ms before read pci conf, and try in 1s */
293 found
= pci_bus_check_dev(ctrl
->pcie
->port
->subordinate
,
296 pcie_capability_read_word(pdev
, PCI_EXP_LNKSTA
, &lnk_status
);
297 ctrl_dbg(ctrl
, "%s: lnk_status = %x\n", __func__
, lnk_status
);
298 if ((lnk_status
& PCI_EXP_LNKSTA_LT
) ||
299 !(lnk_status
& PCI_EXP_LNKSTA_NLW
)) {
300 ctrl_err(ctrl
, "link training error: status %#06x\n",
305 pcie_update_link_speed(ctrl
->pcie
->port
->subordinate
, lnk_status
);
313 static int __pciehp_link_set(struct controller
*ctrl
, bool enable
)
315 struct pci_dev
*pdev
= ctrl_dev(ctrl
);
318 pcie_capability_read_word(pdev
, PCI_EXP_LNKCTL
, &lnk_ctrl
);
321 lnk_ctrl
&= ~PCI_EXP_LNKCTL_LD
;
323 lnk_ctrl
|= PCI_EXP_LNKCTL_LD
;
325 pcie_capability_write_word(pdev
, PCI_EXP_LNKCTL
, lnk_ctrl
);
326 ctrl_dbg(ctrl
, "%s: lnk_ctrl = %x\n", __func__
, lnk_ctrl
);
330 static int pciehp_link_enable(struct controller
*ctrl
)
332 return __pciehp_link_set(ctrl
, true);
335 int pciehp_get_raw_indicator_status(struct hotplug_slot
*hotplug_slot
,
338 struct slot
*slot
= hotplug_slot
->private;
339 struct pci_dev
*pdev
= ctrl_dev(slot
->ctrl
);
342 pcie_capability_read_word(pdev
, PCI_EXP_SLTCTL
, &slot_ctrl
);
343 *status
= (slot_ctrl
& (PCI_EXP_SLTCTL_AIC
| PCI_EXP_SLTCTL_PIC
)) >> 6;
347 void pciehp_get_attention_status(struct slot
*slot
, u8
*status
)
349 struct controller
*ctrl
= slot
->ctrl
;
350 struct pci_dev
*pdev
= ctrl_dev(ctrl
);
353 pcie_capability_read_word(pdev
, PCI_EXP_SLTCTL
, &slot_ctrl
);
354 ctrl_dbg(ctrl
, "%s: SLOTCTRL %x, value read %x\n", __func__
,
355 pci_pcie_cap(ctrl
->pcie
->port
) + PCI_EXP_SLTCTL
, slot_ctrl
);
357 switch (slot_ctrl
& PCI_EXP_SLTCTL_AIC
) {
358 case PCI_EXP_SLTCTL_ATTN_IND_ON
:
359 *status
= 1; /* On */
361 case PCI_EXP_SLTCTL_ATTN_IND_BLINK
:
362 *status
= 2; /* Blink */
364 case PCI_EXP_SLTCTL_ATTN_IND_OFF
:
365 *status
= 0; /* Off */
373 void pciehp_get_power_status(struct slot
*slot
, u8
*status
)
375 struct controller
*ctrl
= slot
->ctrl
;
376 struct pci_dev
*pdev
= ctrl_dev(ctrl
);
379 pcie_capability_read_word(pdev
, PCI_EXP_SLTCTL
, &slot_ctrl
);
380 ctrl_dbg(ctrl
, "%s: SLOTCTRL %x value read %x\n", __func__
,
381 pci_pcie_cap(ctrl
->pcie
->port
) + PCI_EXP_SLTCTL
, slot_ctrl
);
383 switch (slot_ctrl
& PCI_EXP_SLTCTL_PCC
) {
384 case PCI_EXP_SLTCTL_PWR_ON
:
385 *status
= 1; /* On */
387 case PCI_EXP_SLTCTL_PWR_OFF
:
388 *status
= 0; /* Off */
396 void pciehp_get_latch_status(struct slot
*slot
, u8
*status
)
398 struct pci_dev
*pdev
= ctrl_dev(slot
->ctrl
);
401 pcie_capability_read_word(pdev
, PCI_EXP_SLTSTA
, &slot_status
);
402 *status
= !!(slot_status
& PCI_EXP_SLTSTA_MRLSS
);
405 void pciehp_get_adapter_status(struct slot
*slot
, u8
*status
)
407 struct pci_dev
*pdev
= ctrl_dev(slot
->ctrl
);
410 pcie_capability_read_word(pdev
, PCI_EXP_SLTSTA
, &slot_status
);
411 *status
= !!(slot_status
& PCI_EXP_SLTSTA_PDS
);
414 int pciehp_query_power_fault(struct slot
*slot
)
416 struct pci_dev
*pdev
= ctrl_dev(slot
->ctrl
);
419 pcie_capability_read_word(pdev
, PCI_EXP_SLTSTA
, &slot_status
);
420 return !!(slot_status
& PCI_EXP_SLTSTA_PFD
);
423 int pciehp_set_raw_indicator_status(struct hotplug_slot
*hotplug_slot
,
426 struct slot
*slot
= hotplug_slot
->private;
427 struct controller
*ctrl
= slot
->ctrl
;
429 pcie_write_cmd_nowait(ctrl
, status
<< 6,
430 PCI_EXP_SLTCTL_AIC
| PCI_EXP_SLTCTL_PIC
);
434 void pciehp_set_attention_status(struct slot
*slot
, u8 value
)
436 struct controller
*ctrl
= slot
->ctrl
;
443 case 0: /* turn off */
444 slot_cmd
= PCI_EXP_SLTCTL_ATTN_IND_OFF
;
446 case 1: /* turn on */
447 slot_cmd
= PCI_EXP_SLTCTL_ATTN_IND_ON
;
449 case 2: /* turn blink */
450 slot_cmd
= PCI_EXP_SLTCTL_ATTN_IND_BLINK
;
455 pcie_write_cmd_nowait(ctrl
, slot_cmd
, PCI_EXP_SLTCTL_AIC
);
456 ctrl_dbg(ctrl
, "%s: SLOTCTRL %x write cmd %x\n", __func__
,
457 pci_pcie_cap(ctrl
->pcie
->port
) + PCI_EXP_SLTCTL
, slot_cmd
);
460 void pciehp_green_led_on(struct slot
*slot
)
462 struct controller
*ctrl
= slot
->ctrl
;
467 pcie_write_cmd_nowait(ctrl
, PCI_EXP_SLTCTL_PWR_IND_ON
,
469 ctrl_dbg(ctrl
, "%s: SLOTCTRL %x write cmd %x\n", __func__
,
470 pci_pcie_cap(ctrl
->pcie
->port
) + PCI_EXP_SLTCTL
,
471 PCI_EXP_SLTCTL_PWR_IND_ON
);
474 void pciehp_green_led_off(struct slot
*slot
)
476 struct controller
*ctrl
= slot
->ctrl
;
481 pcie_write_cmd_nowait(ctrl
, PCI_EXP_SLTCTL_PWR_IND_OFF
,
483 ctrl_dbg(ctrl
, "%s: SLOTCTRL %x write cmd %x\n", __func__
,
484 pci_pcie_cap(ctrl
->pcie
->port
) + PCI_EXP_SLTCTL
,
485 PCI_EXP_SLTCTL_PWR_IND_OFF
);
488 void pciehp_green_led_blink(struct slot
*slot
)
490 struct controller
*ctrl
= slot
->ctrl
;
495 pcie_write_cmd_nowait(ctrl
, PCI_EXP_SLTCTL_PWR_IND_BLINK
,
497 ctrl_dbg(ctrl
, "%s: SLOTCTRL %x write cmd %x\n", __func__
,
498 pci_pcie_cap(ctrl
->pcie
->port
) + PCI_EXP_SLTCTL
,
499 PCI_EXP_SLTCTL_PWR_IND_BLINK
);
502 int pciehp_power_on_slot(struct slot
*slot
)
504 struct controller
*ctrl
= slot
->ctrl
;
505 struct pci_dev
*pdev
= ctrl_dev(ctrl
);
509 /* Clear sticky power-fault bit from previous power failures */
510 pcie_capability_read_word(pdev
, PCI_EXP_SLTSTA
, &slot_status
);
511 if (slot_status
& PCI_EXP_SLTSTA_PFD
)
512 pcie_capability_write_word(pdev
, PCI_EXP_SLTSTA
,
514 ctrl
->power_fault_detected
= 0;
516 pcie_write_cmd(ctrl
, PCI_EXP_SLTCTL_PWR_ON
, PCI_EXP_SLTCTL_PCC
);
517 ctrl_dbg(ctrl
, "%s: SLOTCTRL %x write cmd %x\n", __func__
,
518 pci_pcie_cap(ctrl
->pcie
->port
) + PCI_EXP_SLTCTL
,
519 PCI_EXP_SLTCTL_PWR_ON
);
521 retval
= pciehp_link_enable(ctrl
);
523 ctrl_err(ctrl
, "%s: Can not enable the link!\n", __func__
);
528 void pciehp_power_off_slot(struct slot
*slot
)
530 struct controller
*ctrl
= slot
->ctrl
;
532 pcie_write_cmd(ctrl
, PCI_EXP_SLTCTL_PWR_OFF
, PCI_EXP_SLTCTL_PCC
);
533 ctrl_dbg(ctrl
, "%s: SLOTCTRL %x write cmd %x\n", __func__
,
534 pci_pcie_cap(ctrl
->pcie
->port
) + PCI_EXP_SLTCTL
,
535 PCI_EXP_SLTCTL_PWR_OFF
);
538 static irqreturn_t
pciehp_isr(int irq
, void *dev_id
)
540 struct controller
*ctrl
= (struct controller
*)dev_id
;
541 struct pci_dev
*pdev
= ctrl_dev(ctrl
);
542 struct pci_bus
*subordinate
= pdev
->subordinate
;
544 struct slot
*slot
= ctrl
->slot
;
549 /* Interrupts cannot originate from a controller that's asleep */
550 if (pdev
->current_state
== PCI_D3cold
)
553 pcie_capability_read_word(pdev
, PCI_EXP_SLTSTA
, &status
);
554 if (status
== (u16
) ~0) {
555 ctrl_info(ctrl
, "%s: no response from device\n", __func__
);
560 * Slot Status contains plain status bits as well as event
561 * notification bits; right now we only want the event bits.
563 events
= status
& (PCI_EXP_SLTSTA_ABP
| PCI_EXP_SLTSTA_PFD
|
564 PCI_EXP_SLTSTA_PDC
| PCI_EXP_SLTSTA_CC
|
565 PCI_EXP_SLTSTA_DLLSC
);
568 * If we've already reported a power fault, don't report it again
569 * until we've done something to handle it.
571 if (ctrl
->power_fault_detected
)
572 events
&= ~PCI_EXP_SLTSTA_PFD
;
577 /* Capture link status before clearing interrupts */
578 if (events
& PCI_EXP_SLTSTA_DLLSC
)
579 link
= pciehp_check_link_active(ctrl
);
581 pcie_capability_write_word(pdev
, PCI_EXP_SLTSTA
, events
);
582 ctrl_dbg(ctrl
, "pending interrupts %#06x from Slot Status\n", events
);
584 /* Check Command Complete Interrupt Pending */
585 if (events
& PCI_EXP_SLTSTA_CC
) {
588 wake_up(&ctrl
->queue
);
592 list_for_each_entry(dev
, &subordinate
->devices
, bus_list
) {
593 if (dev
->ignore_hotplug
) {
594 ctrl_dbg(ctrl
, "ignoring hotplug event %#06x (%s requested no hotplug)\n",
595 events
, pci_name(dev
));
601 /* Check Attention Button Pressed */
602 if (events
& PCI_EXP_SLTSTA_ABP
) {
603 ctrl_info(ctrl
, "Slot(%s): Attention button pressed\n",
605 pciehp_queue_interrupt_event(slot
, INT_BUTTON_PRESS
);
609 * Check Link Status Changed at higher precedence than Presence
610 * Detect Changed. The PDS value may be set to "card present" from
611 * out-of-band detection, which may be in conflict with a Link Down
612 * and cause the wrong event to queue.
614 if (events
& PCI_EXP_SLTSTA_DLLSC
) {
615 ctrl_info(ctrl
, "Slot(%s): Link %s\n", slot_name(slot
),
616 link
? "Up" : "Down");
617 pciehp_queue_interrupt_event(slot
, link
? INT_LINK_UP
:
619 } else if (events
& PCI_EXP_SLTSTA_PDC
) {
620 present
= !!(status
& PCI_EXP_SLTSTA_PDS
);
621 ctrl_info(ctrl
, "Slot(%s): Card %spresent\n", slot_name(slot
),
622 present
? "" : "not ");
623 pciehp_queue_interrupt_event(slot
, present
? INT_PRESENCE_ON
:
627 /* Check Power Fault Detected */
628 if ((events
& PCI_EXP_SLTSTA_PFD
) && !ctrl
->power_fault_detected
) {
629 ctrl
->power_fault_detected
= 1;
630 ctrl_err(ctrl
, "Slot(%s): Power fault\n", slot_name(slot
));
631 pciehp_queue_interrupt_event(slot
, INT_POWER_FAULT
);
637 static irqreturn_t
pcie_isr(int irq
, void *dev_id
)
639 irqreturn_t rc
, handled
= IRQ_NONE
;
642 * To guarantee that all interrupt events are serviced, we need to
643 * re-inspect Slot Status register after clearing what is presumed
644 * to be the last pending interrupt.
647 rc
= pciehp_isr(irq
, dev_id
);
648 if (rc
== IRQ_HANDLED
)
649 handled
= IRQ_HANDLED
;
650 } while (rc
== IRQ_HANDLED
);
652 /* Return IRQ_HANDLED if we handled one or more events */
656 static void pcie_enable_notification(struct controller
*ctrl
)
661 * TBD: Power fault detected software notification support.
663 * Power fault detected software notification is not enabled
664 * now, because it caused power fault detected interrupt storm
665 * on some machines. On those machines, power fault detected
666 * bit in the slot status register was set again immediately
667 * when it is cleared in the interrupt service routine, and
668 * next power fault detected interrupt was notified again.
672 * Always enable link events: thus link-up and link-down shall
673 * always be treated as hotplug and unplug respectively. Enable
674 * presence detect only if Attention Button is not present.
676 cmd
= PCI_EXP_SLTCTL_DLLSCE
;
677 if (ATTN_BUTTN(ctrl
))
678 cmd
|= PCI_EXP_SLTCTL_ABPE
;
680 cmd
|= PCI_EXP_SLTCTL_PDCE
;
681 if (!pciehp_poll_mode
)
682 cmd
|= PCI_EXP_SLTCTL_HPIE
| PCI_EXP_SLTCTL_CCIE
;
684 mask
= (PCI_EXP_SLTCTL_PDCE
| PCI_EXP_SLTCTL_ABPE
|
685 PCI_EXP_SLTCTL_PFDE
|
686 PCI_EXP_SLTCTL_HPIE
| PCI_EXP_SLTCTL_CCIE
|
687 PCI_EXP_SLTCTL_DLLSCE
);
689 pcie_write_cmd_nowait(ctrl
, cmd
, mask
);
690 ctrl_dbg(ctrl
, "%s: SLOTCTRL %x write cmd %x\n", __func__
,
691 pci_pcie_cap(ctrl
->pcie
->port
) + PCI_EXP_SLTCTL
, cmd
);
694 void pcie_reenable_notification(struct controller
*ctrl
)
697 * Clear both Presence and Data Link Layer Changed to make sure
698 * those events still fire after we have re-enabled them.
700 pcie_capability_write_word(ctrl
->pcie
->port
, PCI_EXP_SLTSTA
,
701 PCI_EXP_SLTSTA_PDC
| PCI_EXP_SLTSTA_DLLSC
);
702 pcie_enable_notification(ctrl
);
705 static void pcie_disable_notification(struct controller
*ctrl
)
709 mask
= (PCI_EXP_SLTCTL_PDCE
| PCI_EXP_SLTCTL_ABPE
|
710 PCI_EXP_SLTCTL_MRLSCE
| PCI_EXP_SLTCTL_PFDE
|
711 PCI_EXP_SLTCTL_HPIE
| PCI_EXP_SLTCTL_CCIE
|
712 PCI_EXP_SLTCTL_DLLSCE
);
713 pcie_write_cmd(ctrl
, 0, mask
);
714 ctrl_dbg(ctrl
, "%s: SLOTCTRL %x write cmd %x\n", __func__
,
715 pci_pcie_cap(ctrl
->pcie
->port
) + PCI_EXP_SLTCTL
, 0);
719 * pciehp has a 1:1 bus:slot relationship so we ultimately want a secondary
720 * bus reset of the bridge, but at the same time we want to ensure that it is
721 * not seen as a hot-unplug, followed by the hot-plug of the device. Thus,
722 * disable link state notification and presence detection change notification
723 * momentarily, if we see that they could interfere. Also, clear any spurious
726 int pciehp_reset_slot(struct slot
*slot
, int probe
)
728 struct controller
*ctrl
= slot
->ctrl
;
729 struct pci_dev
*pdev
= ctrl_dev(ctrl
);
730 u16 stat_mask
= 0, ctrl_mask
= 0;
735 if (!ATTN_BUTTN(ctrl
)) {
736 ctrl_mask
|= PCI_EXP_SLTCTL_PDCE
;
737 stat_mask
|= PCI_EXP_SLTSTA_PDC
;
739 ctrl_mask
|= PCI_EXP_SLTCTL_DLLSCE
;
740 stat_mask
|= PCI_EXP_SLTSTA_DLLSC
;
742 pcie_write_cmd(ctrl
, 0, ctrl_mask
);
743 ctrl_dbg(ctrl
, "%s: SLOTCTRL %x write cmd %x\n", __func__
,
744 pci_pcie_cap(ctrl
->pcie
->port
) + PCI_EXP_SLTCTL
, 0);
745 if (pciehp_poll_mode
)
746 del_timer_sync(&ctrl
->poll_timer
);
748 pci_reset_bridge_secondary_bus(ctrl
->pcie
->port
);
750 pcie_capability_write_word(pdev
, PCI_EXP_SLTSTA
, stat_mask
);
751 pcie_write_cmd_nowait(ctrl
, ctrl_mask
, ctrl_mask
);
752 ctrl_dbg(ctrl
, "%s: SLOTCTRL %x write cmd %x\n", __func__
,
753 pci_pcie_cap(ctrl
->pcie
->port
) + PCI_EXP_SLTCTL
, ctrl_mask
);
754 if (pciehp_poll_mode
)
755 int_poll_timeout(&ctrl
->poll_timer
);
759 int pcie_init_notification(struct controller
*ctrl
)
761 if (pciehp_request_irq(ctrl
))
763 pcie_enable_notification(ctrl
);
764 ctrl
->notification_enabled
= 1;
768 static void pcie_shutdown_notification(struct controller
*ctrl
)
770 if (ctrl
->notification_enabled
) {
771 pcie_disable_notification(ctrl
);
772 pciehp_free_irq(ctrl
);
773 ctrl
->notification_enabled
= 0;
777 static int pcie_init_slot(struct controller
*ctrl
)
781 slot
= kzalloc(sizeof(*slot
), GFP_KERNEL
);
785 slot
->wq
= alloc_ordered_workqueue("pciehp-%u", 0, PSN(ctrl
));
790 mutex_init(&slot
->lock
);
791 mutex_init(&slot
->hotplug_lock
);
792 INIT_DELAYED_WORK(&slot
->work
, pciehp_queue_pushbutton_work
);
800 static void pcie_cleanup_slot(struct controller
*ctrl
)
802 struct slot
*slot
= ctrl
->slot
;
803 cancel_delayed_work(&slot
->work
);
804 destroy_workqueue(slot
->wq
);
808 static inline void dbg_ctrl(struct controller
*ctrl
)
810 struct pci_dev
*pdev
= ctrl
->pcie
->port
;
816 ctrl_info(ctrl
, "Slot Capabilities : 0x%08x\n", ctrl
->slot_cap
);
817 pcie_capability_read_word(pdev
, PCI_EXP_SLTSTA
, ®16
);
818 ctrl_info(ctrl
, "Slot Status : 0x%04x\n", reg16
);
819 pcie_capability_read_word(pdev
, PCI_EXP_SLTCTL
, ®16
);
820 ctrl_info(ctrl
, "Slot Control : 0x%04x\n", reg16
);
823 #define FLAG(x, y) (((x) & (y)) ? '+' : '-')
825 struct controller
*pcie_init(struct pcie_device
*dev
)
827 struct controller
*ctrl
;
828 u32 slot_cap
, link_cap
;
829 struct pci_dev
*pdev
= dev
->port
;
831 ctrl
= kzalloc(sizeof(*ctrl
), GFP_KERNEL
);
836 pcie_capability_read_dword(pdev
, PCI_EXP_SLTCAP
, &slot_cap
);
838 if (pdev
->hotplug_user_indicators
)
839 slot_cap
&= ~(PCI_EXP_SLTCAP_AIP
| PCI_EXP_SLTCAP_PIP
);
842 * We assume no Thunderbolt controllers support Command Complete events,
843 * but some controllers falsely claim they do.
845 if (pdev
->is_thunderbolt
)
846 slot_cap
|= PCI_EXP_SLTCAP_NCCS
;
848 ctrl
->slot_cap
= slot_cap
;
849 mutex_init(&ctrl
->ctrl_lock
);
850 init_waitqueue_head(&ctrl
->queue
);
853 /* Check if Data Link Layer Link Active Reporting is implemented */
854 pcie_capability_read_dword(pdev
, PCI_EXP_LNKCAP
, &link_cap
);
855 if (link_cap
& PCI_EXP_LNKCAP_DLLLARC
)
856 ctrl
->link_active_reporting
= 1;
859 * Clear all remaining event bits in Slot Status register except
860 * Presence Detect Changed. We want to make sure possible
861 * hotplug event is triggered when the interrupt is unmasked so
862 * that we don't lose that event.
864 pcie_capability_write_word(pdev
, PCI_EXP_SLTSTA
,
865 PCI_EXP_SLTSTA_ABP
| PCI_EXP_SLTSTA_PFD
|
866 PCI_EXP_SLTSTA_MRLSC
| PCI_EXP_SLTSTA_CC
|
867 PCI_EXP_SLTSTA_DLLSC
);
869 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",
870 (slot_cap
& PCI_EXP_SLTCAP_PSN
) >> 19,
871 FLAG(slot_cap
, PCI_EXP_SLTCAP_ABP
),
872 FLAG(slot_cap
, PCI_EXP_SLTCAP_PCP
),
873 FLAG(slot_cap
, PCI_EXP_SLTCAP_MRLSP
),
874 FLAG(slot_cap
, PCI_EXP_SLTCAP_AIP
),
875 FLAG(slot_cap
, PCI_EXP_SLTCAP_PIP
),
876 FLAG(slot_cap
, PCI_EXP_SLTCAP_HPC
),
877 FLAG(slot_cap
, PCI_EXP_SLTCAP_HPS
),
878 FLAG(slot_cap
, PCI_EXP_SLTCAP_EIP
),
879 FLAG(slot_cap
, PCI_EXP_SLTCAP_NCCS
),
880 FLAG(link_cap
, PCI_EXP_LNKCAP_DLLLARC
),
881 pdev
->broken_cmd_compl
? " (with Cmd Compl erratum)" : "");
883 if (pcie_init_slot(ctrl
))
894 void pciehp_release_ctrl(struct controller
*ctrl
)
896 pcie_shutdown_notification(ctrl
);
897 pcie_cleanup_slot(ctrl
);
901 static void quirk_cmd_compl(struct pci_dev
*pdev
)
905 if (pci_is_pcie(pdev
)) {
906 pcie_capability_read_dword(pdev
, PCI_EXP_SLTCAP
, &slot_cap
);
907 if (slot_cap
& PCI_EXP_SLTCAP_HPC
&&
908 !(slot_cap
& PCI_EXP_SLTCAP_NCCS
))
909 pdev
->broken_cmd_compl
= 1;
912 DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_INTEL
, PCI_ANY_ID
,
913 PCI_CLASS_BRIDGE_PCI
, 8, quirk_cmd_compl
);
914 DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_QCOM
, 0x0400,
915 PCI_CLASS_BRIDGE_PCI
, 8, quirk_cmd_compl
);
916 DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_QCOM
, 0x0401,
917 PCI_CLASS_BRIDGE_PCI
, 8, quirk_cmd_compl
);