2 * Copyright IBM Corporation 2001, 2005, 2006
3 * Copyright Dave Engebretsen & Todd Inglett 2001
4 * Copyright Linas Vepstas 2005, 2006
5 * Copyright 2001-2012 IBM Corporation.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * Please address comments and feedback to Linas Vepstas <linas@austin.ibm.com>
24 #include <linux/delay.h>
25 #include <linux/debugfs.h>
26 #include <linux/sched.h>
27 #include <linux/init.h>
28 #include <linux/list.h>
29 #include <linux/pci.h>
30 #include <linux/iommu.h>
31 #include <linux/proc_fs.h>
32 #include <linux/rbtree.h>
33 #include <linux/reboot.h>
34 #include <linux/seq_file.h>
35 #include <linux/spinlock.h>
36 #include <linux/export.h>
39 #include <linux/atomic.h>
40 #include <asm/debug.h>
42 #include <asm/eeh_event.h>
44 #include <asm/iommu.h>
45 #include <asm/machdep.h>
46 #include <asm/ppc-pci.h>
51 * EEH, or "Extended Error Handling" is a PCI bridge technology for
52 * dealing with PCI bus errors that can't be dealt with within the
53 * usual PCI framework, except by check-stopping the CPU. Systems
54 * that are designed for high-availability/reliability cannot afford
55 * to crash due to a "mere" PCI error, thus the need for EEH.
56 * An EEH-capable bridge operates by converting a detected error
57 * into a "slot freeze", taking the PCI adapter off-line, making
58 * the slot behave, from the OS'es point of view, as if the slot
59 * were "empty": all reads return 0xff's and all writes are silently
60 * ignored. EEH slot isolation events can be triggered by parity
61 * errors on the address or data busses (e.g. during posted writes),
62 * which in turn might be caused by low voltage on the bus, dust,
63 * vibration, humidity, radioactivity or plain-old failed hardware.
65 * Note, however, that one of the leading causes of EEH slot
66 * freeze events are buggy device drivers, buggy device microcode,
67 * or buggy device hardware. This is because any attempt by the
68 * device to bus-master data to a memory address that is not
69 * assigned to the device will trigger a slot freeze. (The idea
70 * is to prevent devices-gone-wild from corrupting system memory).
71 * Buggy hardware/drivers will have a miserable time co-existing
74 * Ideally, a PCI device driver, when suspecting that an isolation
75 * event has occurred (e.g. by reading 0xff's), will then ask EEH
76 * whether this is the case, and then take appropriate steps to
77 * reset the PCI slot, the PCI device, and then resume operations.
78 * However, until that day, the checking is done here, with the
79 * eeh_check_failure() routine embedded in the MMIO macros. If
80 * the slot is found to be isolated, an "EEH Event" is synthesized
81 * and sent out for processing.
84 /* If a device driver keeps reading an MMIO register in an interrupt
85 * handler after a slot isolation event, it might be broken.
86 * This sets the threshold for how many read attempts we allow
87 * before printing an error message.
89 #define EEH_MAX_FAILS 2100000
91 /* Time to wait for a PCI slot to report status, in milliseconds */
92 #define PCI_BUS_RESET_WAIT_MSEC (5*60*1000)
95 * EEH probe mode support, which is part of the flags,
96 * is to support multiple platforms for EEH. Some platforms
97 * like pSeries do PCI emunation based on device tree.
98 * However, other platforms like powernv probe PCI devices
99 * from hardware. The flag is used to distinguish that.
100 * In addition, struct eeh_ops::probe would be invoked for
101 * particular OF node or PCI device so that the corresponding
102 * PE would be created there.
104 int eeh_subsystem_flags;
105 EXPORT_SYMBOL(eeh_subsystem_flags);
107 /* Platform dependent EEH operations */
108 struct eeh_ops *eeh_ops = NULL;
110 /* Lock to avoid races due to multiple reports of an error */
111 DEFINE_RAW_SPINLOCK(confirm_error_lock);
113 /* Lock to protect passed flags */
114 static DEFINE_MUTEX(eeh_dev_mutex);
116 /* Buffer for reporting pci register dumps. Its here in BSS, and
117 * not dynamically alloced, so that it ends up in RMO where RTAS
120 #define EEH_PCI_REGS_LOG_LEN 8192
121 static unsigned char pci_regs_buf[EEH_PCI_REGS_LOG_LEN];
124 * The struct is used to maintain the EEH global statistic
125 * information. Besides, the EEH global statistics will be
126 * exported to user space through procfs
129 u64 no_device; /* PCI device not found */
130 u64 no_dn; /* OF node not found */
131 u64 no_cfg_addr; /* Config address not found */
132 u64 ignored_check; /* EEH check skipped */
133 u64 total_mmio_ffs; /* Total EEH checks */
134 u64 false_positives; /* Unnecessary EEH checks */
135 u64 slot_resets; /* PE reset */
138 static struct eeh_stats eeh_stats;
140 #define IS_BRIDGE(class_code) (((class_code)<<16) == PCI_BASE_CLASS_BRIDGE)
142 static int __init eeh_setup(char *str)
144 if (!strcmp(str, "off"))
145 eeh_add_flag(EEH_FORCE_DISABLED);
149 __setup("eeh=", eeh_setup);
152 * This routine captures assorted PCI configuration space data
153 * for the indicated PCI device, and puts them into a buffer
154 * for RTAS error logging.
156 static size_t eeh_dump_dev_log(struct eeh_dev *edev, char *buf, size_t len)
158 struct device_node *dn = eeh_dev_to_of_node(edev);
164 n += scnprintf(buf+n, len-n, "%s\n", dn->full_name);
165 pr_warn("EEH: of node=%s\n", dn->full_name);
167 eeh_ops->read_config(dn, PCI_VENDOR_ID, 4, &cfg);
168 n += scnprintf(buf+n, len-n, "dev/vend:%08x\n", cfg);
169 pr_warn("EEH: PCI device/vendor: %08x\n", cfg);
171 eeh_ops->read_config(dn, PCI_COMMAND, 4, &cfg);
172 n += scnprintf(buf+n, len-n, "cmd/stat:%x\n", cfg);
173 pr_warn("EEH: PCI cmd/status register: %08x\n", cfg);
175 /* Gather bridge-specific registers */
176 if (edev->mode & EEH_DEV_BRIDGE) {
177 eeh_ops->read_config(dn, PCI_SEC_STATUS, 2, &cfg);
178 n += scnprintf(buf+n, len-n, "sec stat:%x\n", cfg);
179 pr_warn("EEH: Bridge secondary status: %04x\n", cfg);
181 eeh_ops->read_config(dn, PCI_BRIDGE_CONTROL, 2, &cfg);
182 n += scnprintf(buf+n, len-n, "brdg ctl:%x\n", cfg);
183 pr_warn("EEH: Bridge control: %04x\n", cfg);
186 /* Dump out the PCI-X command and status regs */
187 cap = edev->pcix_cap;
189 eeh_ops->read_config(dn, cap, 4, &cfg);
190 n += scnprintf(buf+n, len-n, "pcix-cmd:%x\n", cfg);
191 pr_warn("EEH: PCI-X cmd: %08x\n", cfg);
193 eeh_ops->read_config(dn, cap+4, 4, &cfg);
194 n += scnprintf(buf+n, len-n, "pcix-stat:%x\n", cfg);
195 pr_warn("EEH: PCI-X status: %08x\n", cfg);
198 /* If PCI-E capable, dump PCI-E cap 10 */
199 cap = edev->pcie_cap;
201 n += scnprintf(buf+n, len-n, "pci-e cap10:\n");
202 pr_warn("EEH: PCI-E capabilities and status follow:\n");
204 for (i=0; i<=8; i++) {
205 eeh_ops->read_config(dn, cap+4*i, 4, &cfg);
206 n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
210 pr_warn("%s\n", buffer);
212 l = scnprintf(buffer, sizeof(buffer),
213 "EEH: PCI-E %02x: %08x ",
216 l += scnprintf(buffer+l, sizeof(buffer)-l,
222 pr_warn("%s\n", buffer);
225 /* If AER capable, dump it */
228 n += scnprintf(buf+n, len-n, "pci-e AER:\n");
229 pr_warn("EEH: PCI-E AER capability register set follows:\n");
231 for (i=0; i<=13; i++) {
232 eeh_ops->read_config(dn, cap+4*i, 4, &cfg);
233 n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
237 pr_warn("%s\n", buffer);
239 l = scnprintf(buffer, sizeof(buffer),
240 "EEH: PCI-E AER %02x: %08x ",
243 l += scnprintf(buffer+l, sizeof(buffer)-l,
248 pr_warn("%s\n", buffer);
254 static void *eeh_dump_pe_log(void *data, void *flag)
256 struct eeh_pe *pe = data;
257 struct eeh_dev *edev, *tmp;
260 /* If the PE's config space is blocked, 0xFF's will be
261 * returned. It's pointless to collect the log in this
264 if (pe->state & EEH_PE_CFG_BLOCKED)
267 eeh_pe_for_each_dev(pe, edev, tmp)
268 *plen += eeh_dump_dev_log(edev, pci_regs_buf + *plen,
269 EEH_PCI_REGS_LOG_LEN - *plen);
275 * eeh_slot_error_detail - Generate combined log including driver log and error log
277 * @severity: temporary or permanent error log
279 * This routine should be called to generate the combined log, which
280 * is comprised of driver log and error log. The driver log is figured
281 * out from the config space of the corresponding PCI device, while
282 * the error log is fetched through platform dependent function call.
284 void eeh_slot_error_detail(struct eeh_pe *pe, int severity)
289 * When the PHB is fenced or dead, it's pointless to collect
290 * the data from PCI config space because it should return
291 * 0xFF's. For ER, we still retrieve the data from the PCI
294 * For pHyp, we have to enable IO for log retrieval. Otherwise,
295 * 0xFF's is always returned from PCI config space.
297 if (!(pe->type & EEH_PE_PHB)) {
298 if (eeh_has_flag(EEH_ENABLE_IO_FOR_LOG))
299 eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
300 eeh_ops->configure_bridge(pe);
301 eeh_pe_restore_bars(pe);
304 eeh_pe_traverse(pe, eeh_dump_pe_log, &loglen);
307 eeh_ops->get_log(pe, severity, pci_regs_buf, loglen);
311 * eeh_token_to_phys - Convert EEH address token to phys address
312 * @token: I/O token, should be address in the form 0xA....
314 * This routine should be called to convert virtual I/O address
317 static inline unsigned long eeh_token_to_phys(unsigned long token)
324 * We won't find hugepages here, iomem
326 ptep = find_linux_pte_or_hugepte(init_mm.pgd, token, &hugepage_shift);
329 WARN_ON(hugepage_shift);
330 pa = pte_pfn(*ptep) << PAGE_SHIFT;
332 return pa | (token & (PAGE_SIZE-1));
336 * On PowerNV platform, we might already have fenced PHB there.
337 * For that case, it's meaningless to recover frozen PE. Intead,
338 * We have to handle fenced PHB firstly.
340 static int eeh_phb_check_failure(struct eeh_pe *pe)
342 struct eeh_pe *phb_pe;
346 if (!eeh_has_flag(EEH_PROBE_MODE_DEV))
349 /* Find the PHB PE */
350 phb_pe = eeh_phb_pe_get(pe->phb);
352 pr_warn("%s Can't find PE for PHB#%d\n",
353 __func__, pe->phb->global_number);
357 /* If the PHB has been in problematic state */
358 eeh_serialize_lock(&flags);
359 if (phb_pe->state & EEH_PE_ISOLATED) {
364 /* Check PHB state */
365 ret = eeh_ops->get_state(phb_pe, NULL);
367 (ret == EEH_STATE_NOT_SUPPORT) ||
368 (ret & (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) ==
369 (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) {
374 /* Isolate the PHB and send event */
375 eeh_pe_state_mark(phb_pe, EEH_PE_ISOLATED);
376 eeh_serialize_unlock(flags);
378 pr_err("EEH: PHB#%x failure detected, location: %s\n",
379 phb_pe->phb->global_number, eeh_pe_loc_get(phb_pe));
381 eeh_send_failure_event(phb_pe);
385 eeh_serialize_unlock(flags);
390 * eeh_dev_check_failure - Check if all 1's data is due to EEH slot freeze
393 * Check for an EEH failure for the given device node. Call this
394 * routine if the result of a read was all 0xff's and you want to
395 * find out if this is due to an EEH slot freeze. This routine
396 * will query firmware for the EEH status.
398 * Returns 0 if there has not been an EEH error; otherwise returns
399 * a non-zero value and queues up a slot isolation event notification.
401 * It is safe to call this routine in an interrupt context.
403 int eeh_dev_check_failure(struct eeh_dev *edev)
406 int active_flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
408 struct device_node *dn;
410 struct eeh_pe *pe, *parent_pe, *phb_pe;
412 const char *location;
414 eeh_stats.total_mmio_ffs++;
423 dn = eeh_dev_to_of_node(edev);
424 dev = eeh_dev_to_pci_dev(edev);
425 pe = eeh_dev_to_pe(edev);
427 /* Access to IO BARs might get this far and still not want checking. */
429 eeh_stats.ignored_check++;
430 pr_debug("EEH: Ignored check for %s %s\n",
431 eeh_pci_name(dev), dn->full_name);
435 if (!pe->addr && !pe->config_addr) {
436 eeh_stats.no_cfg_addr++;
441 * On PowerNV platform, we might already have fenced PHB
442 * there and we need take care of that firstly.
444 ret = eeh_phb_check_failure(pe);
449 * If the PE isn't owned by us, we shouldn't check the
450 * state. Instead, let the owner handle it if the PE has
453 if (eeh_pe_passed(pe))
456 /* If we already have a pending isolation event for this
457 * slot, we know it's bad already, we don't need to check.
458 * Do this checking under a lock; as multiple PCI devices
459 * in one slot might report errors simultaneously, and we
460 * only want one error recovery routine running.
462 eeh_serialize_lock(&flags);
464 if (pe->state & EEH_PE_ISOLATED) {
466 if (pe->check_count % EEH_MAX_FAILS == 0) {
467 location = of_get_property(dn, "ibm,loc-code", NULL);
468 printk(KERN_ERR "EEH: %d reads ignored for recovering device at "
469 "location=%s driver=%s pci addr=%s\n",
470 pe->check_count, location,
471 eeh_driver_name(dev), eeh_pci_name(dev));
472 printk(KERN_ERR "EEH: Might be infinite loop in %s driver\n",
473 eeh_driver_name(dev));
480 * Now test for an EEH failure. This is VERY expensive.
481 * Note that the eeh_config_addr may be a parent device
482 * in the case of a device behind a bridge, or it may be
483 * function zero of a multi-function device.
484 * In any case they must share a common PHB.
486 ret = eeh_ops->get_state(pe, NULL);
488 /* Note that config-io to empty slots may fail;
489 * they are empty when they don't have children.
490 * We will punt with the following conditions: Failure to get
491 * PE's state, EEH not support and Permanently unavailable
492 * state, PE is in good state.
495 (ret == EEH_STATE_NOT_SUPPORT) ||
496 ((ret & active_flags) == active_flags)) {
497 eeh_stats.false_positives++;
498 pe->false_positives++;
504 * It should be corner case that the parent PE has been
505 * put into frozen state as well. We should take care
508 parent_pe = pe->parent;
510 /* Hit the ceiling ? */
511 if (parent_pe->type & EEH_PE_PHB)
514 /* Frozen parent PE ? */
515 ret = eeh_ops->get_state(parent_pe, NULL);
517 (ret & active_flags) != active_flags)
520 /* Next parent level */
521 parent_pe = parent_pe->parent;
524 eeh_stats.slot_resets++;
526 /* Avoid repeated reports of this failure, including problems
527 * with other functions on this device, and functions under
530 eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
531 eeh_serialize_unlock(flags);
533 /* Most EEH events are due to device driver bugs. Having
534 * a stack trace will help the device-driver authors figure
535 * out what happened. So print that out.
537 phb_pe = eeh_phb_pe_get(pe->phb);
538 pr_err("EEH: Frozen PHB#%x-PE#%x detected\n",
539 pe->phb->global_number, pe->addr);
540 pr_err("EEH: PE location: %s, PHB location: %s\n",
541 eeh_pe_loc_get(pe), eeh_pe_loc_get(phb_pe));
544 eeh_send_failure_event(pe);
549 eeh_serialize_unlock(flags);
553 EXPORT_SYMBOL_GPL(eeh_dev_check_failure);
556 * eeh_check_failure - Check if all 1's data is due to EEH slot freeze
557 * @token: I/O address
559 * Check for an EEH failure at the given I/O address. Call this
560 * routine if the result of a read was all 0xff's and you want to
561 * find out if this is due to an EEH slot freeze event. This routine
562 * will query firmware for the EEH status.
564 * Note this routine is safe to call in an interrupt context.
566 int eeh_check_failure(const volatile void __iomem *token)
569 struct eeh_dev *edev;
571 /* Finding the phys addr + pci device; this is pretty quick. */
572 addr = eeh_token_to_phys((unsigned long __force) token);
573 edev = eeh_addr_cache_get_dev(addr);
575 eeh_stats.no_device++;
579 return eeh_dev_check_failure(edev);
581 EXPORT_SYMBOL(eeh_check_failure);
585 * eeh_pci_enable - Enable MMIO or DMA transfers for this slot
588 * This routine should be called to reenable frozen MMIO or DMA
589 * so that it would work correctly again. It's useful while doing
590 * recovery or log collection on the indicated device.
592 int eeh_pci_enable(struct eeh_pe *pe, int function)
597 * pHyp doesn't allow to enable IO or DMA on unfrozen PE.
598 * Also, it's pointless to enable them on unfrozen PE. So
599 * we have to check before enabling IO or DMA.
602 case EEH_OPT_THAW_MMIO:
603 active_flag = EEH_STATE_MMIO_ACTIVE;
605 case EEH_OPT_THAW_DMA:
606 active_flag = EEH_STATE_DMA_ACTIVE;
608 case EEH_OPT_DISABLE:
610 case EEH_OPT_FREEZE_PE:
614 pr_warn("%s: Invalid function %d\n",
620 * Check if IO or DMA has been enabled before
624 rc = eeh_ops->get_state(pe, NULL);
628 /* Needn't enable it at all */
629 if (rc == EEH_STATE_NOT_SUPPORT)
632 /* It's already enabled */
633 if (rc & active_flag)
638 /* Issue the request */
639 rc = eeh_ops->set_option(pe, function);
641 pr_warn("%s: Unexpected state change %d on "
642 "PHB#%d-PE#%x, err=%d\n",
643 __func__, function, pe->phb->global_number,
646 /* Check if the request is finished successfully */
648 rc = eeh_ops->wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
652 if (rc & active_flag)
662 * pcibios_set_pcie_slot_reset - Set PCI-E reset state
663 * @dev: pci device struct
664 * @state: reset state to enter
669 int pcibios_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
671 struct eeh_dev *edev = pci_dev_to_eeh_dev(dev);
672 struct eeh_pe *pe = eeh_dev_to_pe(edev);
675 pr_err("%s: No PE found on PCI device %s\n",
676 __func__, pci_name(dev));
681 case pcie_deassert_reset:
682 eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);
683 eeh_pe_state_clear(pe, EEH_PE_CFG_BLOCKED);
686 eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
687 eeh_ops->reset(pe, EEH_RESET_HOT);
689 case pcie_warm_reset:
690 eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
691 eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
694 eeh_pe_state_clear(pe, EEH_PE_CFG_BLOCKED);
702 * eeh_set_pe_freset - Check the required reset for the indicated device
704 * @flag: return value
706 * Each device might have its preferred reset type: fundamental or
707 * hot reset. The routine is used to collected the information for
708 * the indicated device and its children so that the bunch of the
709 * devices could be reset properly.
711 static void *eeh_set_dev_freset(void *data, void *flag)
714 unsigned int *freset = (unsigned int *)flag;
715 struct eeh_dev *edev = (struct eeh_dev *)data;
717 dev = eeh_dev_to_pci_dev(edev);
719 *freset |= dev->needs_freset;
725 * eeh_reset_pe_once - Assert the pci #RST line for 1/4 second
728 * Assert the PCI #RST line for 1/4 second.
730 static void eeh_reset_pe_once(struct eeh_pe *pe)
732 unsigned int freset = 0;
734 /* Determine type of EEH reset required for
735 * Partitionable Endpoint, a hot-reset (1)
736 * or a fundamental reset (3).
737 * A fundamental reset required by any device under
738 * Partitionable Endpoint trumps hot-reset.
740 eeh_pe_dev_traverse(pe, eeh_set_dev_freset, &freset);
743 eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
745 eeh_ops->reset(pe, EEH_RESET_HOT);
747 eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);
751 * eeh_reset_pe - Reset the indicated PE
754 * This routine should be called to reset indicated device, including
755 * PE. A PE might include multiple PCI devices and sometimes PCI bridges
756 * might be involved as well.
758 int eeh_reset_pe(struct eeh_pe *pe)
760 int flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
763 /* Take three shots at resetting the bus */
764 for (i=0; i<3; i++) {
765 eeh_reset_pe_once(pe);
768 * EEH_PE_ISOLATED is expected to be removed after
771 rc = eeh_ops->wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
772 if ((rc & flags) == flags)
776 pr_err("%s: Unrecoverable slot failure on PHB#%d-PE#%x",
777 __func__, pe->phb->global_number, pe->addr);
780 pr_err("EEH: bus reset %d failed on PHB#%d-PE#%x, rc=%d\n",
781 i+1, pe->phb->global_number, pe->addr, rc);
788 * eeh_save_bars - Save device bars
789 * @edev: PCI device associated EEH device
791 * Save the values of the device bars. Unlike the restore
792 * routine, this routine is *not* recursive. This is because
793 * PCI devices are added individually; but, for the restore,
794 * an entire slot is reset at a time.
796 void eeh_save_bars(struct eeh_dev *edev)
799 struct device_node *dn;
803 dn = eeh_dev_to_of_node(edev);
805 for (i = 0; i < 16; i++)
806 eeh_ops->read_config(dn, i * 4, 4, &edev->config_space[i]);
809 * For PCI bridges including root port, we need enable bus
810 * master explicitly. Otherwise, it can't fetch IODA table
811 * entries correctly. So we cache the bit in advance so that
812 * we can restore it after reset, either PHB range or PE range.
814 if (edev->mode & EEH_DEV_BRIDGE)
815 edev->config_space[1] |= PCI_COMMAND_MASTER;
819 * eeh_ops_register - Register platform dependent EEH operations
820 * @ops: platform dependent EEH operations
822 * Register the platform dependent EEH operation callback
823 * functions. The platform should call this function before
824 * any other EEH operations.
826 int __init eeh_ops_register(struct eeh_ops *ops)
829 pr_warn("%s: Invalid EEH ops name for %p\n",
834 if (eeh_ops && eeh_ops != ops) {
835 pr_warn("%s: EEH ops of platform %s already existing (%s)\n",
836 __func__, eeh_ops->name, ops->name);
846 * eeh_ops_unregister - Unreigster platform dependent EEH operations
847 * @name: name of EEH platform operations
849 * Unregister the platform dependent EEH operation callback
852 int __exit eeh_ops_unregister(const char *name)
854 if (!name || !strlen(name)) {
855 pr_warn("%s: Invalid EEH ops name\n",
860 if (eeh_ops && !strcmp(eeh_ops->name, name)) {
868 static int eeh_reboot_notifier(struct notifier_block *nb,
869 unsigned long action, void *unused)
871 eeh_clear_flag(EEH_ENABLED);
875 static struct notifier_block eeh_reboot_nb = {
876 .notifier_call = eeh_reboot_notifier,
880 * eeh_init - EEH initialization
882 * Initialize EEH by trying to enable it for all of the adapters in the system.
883 * As a side effect we can determine here if eeh is supported at all.
884 * Note that we leave EEH on so failed config cycles won't cause a machine
885 * check. If a user turns off EEH for a particular adapter they are really
886 * telling Linux to ignore errors. Some hardware (e.g. POWER5) won't
887 * grant access to a slot if EEH isn't enabled, and so we always enable
888 * EEH for all slots/all devices.
890 * The eeh-force-off option disables EEH checking globally, for all slots.
891 * Even if force-off is set, the EEH hardware is still enabled, so that
892 * newer systems can boot.
896 struct pci_controller *hose, *tmp;
897 struct device_node *phb;
902 * We have to delay the initialization on PowerNV after
903 * the PCI hierarchy tree has been built because the PEs
904 * are figured out based on PCI devices instead of device
907 if (machine_is(powernv) && cnt++ <= 0)
910 /* Register reboot notifier */
911 ret = register_reboot_notifier(&eeh_reboot_nb);
913 pr_warn("%s: Failed to register notifier (%d)\n",
918 /* call platform initialization function */
920 pr_warn("%s: Platform EEH operation not found\n",
923 } else if ((ret = eeh_ops->init())) {
924 pr_warn("%s: Failed to call platform init function (%d)\n",
929 /* Initialize EEH event */
930 ret = eeh_event_init();
934 /* Enable EEH for all adapters */
935 if (eeh_has_flag(EEH_PROBE_MODE_DEVTREE)) {
936 list_for_each_entry_safe(hose, tmp,
937 &hose_list, list_node) {
939 traverse_pci_devices(phb, eeh_ops->of_probe, NULL);
941 } else if (eeh_has_flag(EEH_PROBE_MODE_DEV)) {
942 list_for_each_entry_safe(hose, tmp,
943 &hose_list, list_node)
944 pci_walk_bus(hose->bus, eeh_ops->dev_probe, NULL);
946 pr_warn("%s: Invalid probe mode %x",
947 __func__, eeh_subsystem_flags);
952 * Call platform post-initialization. Actually, It's good chance
953 * to inform platform that EEH is ready to supply service if the
954 * I/O cache stuff has been built up.
956 if (eeh_ops->post_init) {
957 ret = eeh_ops->post_init();
963 pr_info("EEH: PCI Enhanced I/O Error Handling Enabled\n");
965 pr_warn("EEH: No capable adapters found\n");
970 core_initcall_sync(eeh_init);
973 * eeh_add_device_early - Enable EEH for the indicated device_node
974 * @dn: device node for which to set up EEH
976 * This routine must be used to perform EEH initialization for PCI
977 * devices that were added after system boot (e.g. hotplug, dlpar).
978 * This routine must be called before any i/o is performed to the
979 * adapter (inluding any config-space i/o).
980 * Whether this actually enables EEH or not for this device depends
981 * on the CEC architecture, type of the device, on earlier boot
982 * command-line arguments & etc.
984 void eeh_add_device_early(struct device_node *dn)
986 struct pci_controller *phb;
989 * If we're doing EEH probe based on PCI device, we
990 * would delay the probe until late stage because
991 * the PCI device isn't available this moment.
993 if (!eeh_has_flag(EEH_PROBE_MODE_DEVTREE))
996 if (!of_node_to_eeh_dev(dn))
998 phb = of_node_to_eeh_dev(dn)->phb;
1000 /* USB Bus children of PCI devices will not have BUID's */
1001 if (NULL == phb || 0 == phb->buid)
1004 eeh_ops->of_probe(dn, NULL);
1008 * eeh_add_device_tree_early - Enable EEH for the indicated device
1011 * This routine must be used to perform EEH initialization for the
1012 * indicated PCI device that was added after system boot (e.g.
1015 void eeh_add_device_tree_early(struct device_node *dn)
1017 struct device_node *sib;
1019 for_each_child_of_node(dn, sib)
1020 eeh_add_device_tree_early(sib);
1021 eeh_add_device_early(dn);
1023 EXPORT_SYMBOL_GPL(eeh_add_device_tree_early);
1026 * eeh_add_device_late - Perform EEH initialization for the indicated pci device
1027 * @dev: pci device for which to set up EEH
1029 * This routine must be used to complete EEH initialization for PCI
1030 * devices that were added after system boot (e.g. hotplug, dlpar).
1032 void eeh_add_device_late(struct pci_dev *dev)
1034 struct device_node *dn;
1035 struct eeh_dev *edev;
1037 if (!dev || !eeh_enabled())
1040 pr_debug("EEH: Adding device %s\n", pci_name(dev));
1042 dn = pci_device_to_OF_node(dev);
1043 edev = of_node_to_eeh_dev(dn);
1044 if (edev->pdev == dev) {
1045 pr_debug("EEH: Already referenced !\n");
1050 * The EEH cache might not be removed correctly because of
1051 * unbalanced kref to the device during unplug time, which
1052 * relies on pcibios_release_device(). So we have to remove
1053 * that here explicitly.
1056 eeh_rmv_from_parent_pe(edev);
1057 eeh_addr_cache_rmv_dev(edev->pdev);
1058 eeh_sysfs_remove_device(edev->pdev);
1059 edev->mode &= ~EEH_DEV_SYSFS;
1062 * We definitely should have the PCI device removed
1063 * though it wasn't correctly. So we needn't call
1064 * into error handler afterwards.
1066 edev->mode |= EEH_DEV_NO_HANDLER;
1069 dev->dev.archdata.edev = NULL;
1073 dev->dev.archdata.edev = edev;
1076 * We have to do the EEH probe here because the PCI device
1077 * hasn't been created yet in the early stage.
1079 if (eeh_has_flag(EEH_PROBE_MODE_DEV))
1080 eeh_ops->dev_probe(dev, NULL);
1082 eeh_addr_cache_insert_dev(dev);
1086 * eeh_add_device_tree_late - Perform EEH initialization for the indicated PCI bus
1089 * This routine must be used to perform EEH initialization for PCI
1090 * devices which are attached to the indicated PCI bus. The PCI bus
1091 * is added after system boot through hotplug or dlpar.
1093 void eeh_add_device_tree_late(struct pci_bus *bus)
1095 struct pci_dev *dev;
1097 list_for_each_entry(dev, &bus->devices, bus_list) {
1098 eeh_add_device_late(dev);
1099 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
1100 struct pci_bus *subbus = dev->subordinate;
1102 eeh_add_device_tree_late(subbus);
1106 EXPORT_SYMBOL_GPL(eeh_add_device_tree_late);
1109 * eeh_add_sysfs_files - Add EEH sysfs files for the indicated PCI bus
1112 * This routine must be used to add EEH sysfs files for PCI
1113 * devices which are attached to the indicated PCI bus. The PCI bus
1114 * is added after system boot through hotplug or dlpar.
1116 void eeh_add_sysfs_files(struct pci_bus *bus)
1118 struct pci_dev *dev;
1120 list_for_each_entry(dev, &bus->devices, bus_list) {
1121 eeh_sysfs_add_device(dev);
1122 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
1123 struct pci_bus *subbus = dev->subordinate;
1125 eeh_add_sysfs_files(subbus);
1129 EXPORT_SYMBOL_GPL(eeh_add_sysfs_files);
1132 * eeh_remove_device - Undo EEH setup for the indicated pci device
1133 * @dev: pci device to be removed
1135 * This routine should be called when a device is removed from
1136 * a running system (e.g. by hotplug or dlpar). It unregisters
1137 * the PCI device from the EEH subsystem. I/O errors affecting
1138 * this device will no longer be detected after this call; thus,
1139 * i/o errors affecting this slot may leave this device unusable.
1141 void eeh_remove_device(struct pci_dev *dev)
1143 struct eeh_dev *edev;
1145 if (!dev || !eeh_enabled())
1147 edev = pci_dev_to_eeh_dev(dev);
1149 /* Unregister the device with the EEH/PCI address search system */
1150 pr_debug("EEH: Removing device %s\n", pci_name(dev));
1152 if (!edev || !edev->pdev || !edev->pe) {
1153 pr_debug("EEH: Not referenced !\n");
1158 * During the hotplug for EEH error recovery, we need the EEH
1159 * device attached to the parent PE in order for BAR restore
1160 * a bit later. So we keep it for BAR restore and remove it
1161 * from the parent PE during the BAR resotre.
1164 dev->dev.archdata.edev = NULL;
1165 if (!(edev->pe->state & EEH_PE_KEEP))
1166 eeh_rmv_from_parent_pe(edev);
1168 edev->mode |= EEH_DEV_DISCONNECTED;
1171 * We're removing from the PCI subsystem, that means
1172 * the PCI device driver can't support EEH or not
1173 * well. So we rely on hotplug completely to do recovery
1174 * for the specific PCI device.
1176 edev->mode |= EEH_DEV_NO_HANDLER;
1178 eeh_addr_cache_rmv_dev(dev);
1179 eeh_sysfs_remove_device(dev);
1180 edev->mode &= ~EEH_DEV_SYSFS;
1183 int eeh_unfreeze_pe(struct eeh_pe *pe, bool sw_state)
1187 ret = eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
1189 pr_warn("%s: Failure %d enabling IO on PHB#%x-PE#%x\n",
1190 __func__, ret, pe->phb->global_number, pe->addr);
1194 ret = eeh_pci_enable(pe, EEH_OPT_THAW_DMA);
1196 pr_warn("%s: Failure %d enabling DMA on PHB#%x-PE#%x\n",
1197 __func__, ret, pe->phb->global_number, pe->addr);
1201 /* Clear software isolated state */
1202 if (sw_state && (pe->state & EEH_PE_ISOLATED))
1203 eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
1209 static struct pci_device_id eeh_reset_ids[] = {
1210 { PCI_DEVICE(0x19a2, 0x0710) }, /* Emulex, BE */
1211 { PCI_DEVICE(0x10df, 0xe220) }, /* Emulex, Lancer */
1215 static int eeh_pe_change_owner(struct eeh_pe *pe)
1217 struct eeh_dev *edev, *tmp;
1218 struct pci_dev *pdev;
1219 struct pci_device_id *id;
1222 /* Check PE state */
1223 flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
1224 ret = eeh_ops->get_state(pe, NULL);
1225 if (ret < 0 || ret == EEH_STATE_NOT_SUPPORT)
1228 /* Unfrozen PE, nothing to do */
1229 if ((ret & flags) == flags)
1232 /* Frozen PE, check if it needs PE level reset */
1233 eeh_pe_for_each_dev(pe, edev, tmp) {
1234 pdev = eeh_dev_to_pci_dev(edev);
1238 for (id = &eeh_reset_ids[0]; id->vendor != 0; id++) {
1239 if (id->vendor != PCI_ANY_ID &&
1240 id->vendor != pdev->vendor)
1242 if (id->device != PCI_ANY_ID &&
1243 id->device != pdev->device)
1245 if (id->subvendor != PCI_ANY_ID &&
1246 id->subvendor != pdev->subsystem_vendor)
1248 if (id->subdevice != PCI_ANY_ID &&
1249 id->subdevice != pdev->subsystem_device)
1256 return eeh_unfreeze_pe(pe, true);
1259 return eeh_pe_reset_and_recover(pe);
1263 * eeh_dev_open - Increase count of pass through devices for PE
1266 * Increase count of passed through devices for the indicated
1267 * PE. In the result, the EEH errors detected on the PE won't be
1268 * reported. The PE owner will be responsible for detection
1271 int eeh_dev_open(struct pci_dev *pdev)
1273 struct eeh_dev *edev;
1276 mutex_lock(&eeh_dev_mutex);
1278 /* No PCI device ? */
1282 /* No EEH device or PE ? */
1283 edev = pci_dev_to_eeh_dev(pdev);
1284 if (!edev || !edev->pe)
1288 * The PE might have been put into frozen state, but we
1289 * didn't detect that yet. The passed through PCI devices
1290 * in frozen PE won't work properly. Clear the frozen state
1293 ret = eeh_pe_change_owner(edev->pe);
1297 /* Increase PE's pass through count */
1298 atomic_inc(&edev->pe->pass_dev_cnt);
1299 mutex_unlock(&eeh_dev_mutex);
1303 mutex_unlock(&eeh_dev_mutex);
1306 EXPORT_SYMBOL_GPL(eeh_dev_open);
1309 * eeh_dev_release - Decrease count of pass through devices for PE
1312 * Decrease count of pass through devices for the indicated PE. If
1313 * there is no passed through device in PE, the EEH errors detected
1314 * on the PE will be reported and handled as usual.
1316 void eeh_dev_release(struct pci_dev *pdev)
1318 struct eeh_dev *edev;
1320 mutex_lock(&eeh_dev_mutex);
1322 /* No PCI device ? */
1326 /* No EEH device ? */
1327 edev = pci_dev_to_eeh_dev(pdev);
1328 if (!edev || !edev->pe || !eeh_pe_passed(edev->pe))
1331 /* Decrease PE's pass through count */
1332 atomic_dec(&edev->pe->pass_dev_cnt);
1333 WARN_ON(atomic_read(&edev->pe->pass_dev_cnt) < 0);
1334 eeh_pe_change_owner(edev->pe);
1336 mutex_unlock(&eeh_dev_mutex);
1338 EXPORT_SYMBOL(eeh_dev_release);
1340 #ifdef CONFIG_IOMMU_API
1342 static int dev_has_iommu_table(struct device *dev, void *data)
1344 struct pci_dev *pdev = to_pci_dev(dev);
1345 struct pci_dev **ppdev = data;
1346 struct iommu_table *tbl;
1351 tbl = get_iommu_table_base(dev);
1352 if (tbl && tbl->it_group) {
1361 * eeh_iommu_group_to_pe - Convert IOMMU group to EEH PE
1362 * @group: IOMMU group
1364 * The routine is called to convert IOMMU group to EEH PE.
1366 struct eeh_pe *eeh_iommu_group_to_pe(struct iommu_group *group)
1368 struct pci_dev *pdev = NULL;
1369 struct eeh_dev *edev;
1372 /* No IOMMU group ? */
1376 ret = iommu_group_for_each_dev(group, &pdev, dev_has_iommu_table);
1380 /* No EEH device or PE ? */
1381 edev = pci_dev_to_eeh_dev(pdev);
1382 if (!edev || !edev->pe)
1387 EXPORT_SYMBOL_GPL(eeh_iommu_group_to_pe);
1389 #endif /* CONFIG_IOMMU_API */
1392 * eeh_pe_set_option - Set options for the indicated PE
1394 * @option: requested option
1396 * The routine is called to enable or disable EEH functionality
1397 * on the indicated PE, to enable IO or DMA for the frozen PE.
1399 int eeh_pe_set_option(struct eeh_pe *pe, int option)
1408 * EEH functionality could possibly be disabled, just
1409 * return error for the case. And the EEH functinality
1410 * isn't expected to be disabled on one specific PE.
1413 case EEH_OPT_ENABLE:
1414 if (eeh_enabled()) {
1415 ret = eeh_pe_change_owner(pe);
1420 case EEH_OPT_DISABLE:
1422 case EEH_OPT_THAW_MMIO:
1423 case EEH_OPT_THAW_DMA:
1424 if (!eeh_ops || !eeh_ops->set_option) {
1429 ret = eeh_pci_enable(pe, option);
1432 pr_debug("%s: Option %d out of range (%d, %d)\n",
1433 __func__, option, EEH_OPT_DISABLE, EEH_OPT_THAW_DMA);
1439 EXPORT_SYMBOL_GPL(eeh_pe_set_option);
1442 * eeh_pe_get_state - Retrieve PE's state
1445 * Retrieve the PE's state, which includes 3 aspects: enabled
1446 * DMA, enabled IO and asserted reset.
1448 int eeh_pe_get_state(struct eeh_pe *pe)
1450 int result, ret = 0;
1451 bool rst_active, dma_en, mmio_en;
1457 if (!eeh_ops || !eeh_ops->get_state)
1460 result = eeh_ops->get_state(pe, NULL);
1461 rst_active = !!(result & EEH_STATE_RESET_ACTIVE);
1462 dma_en = !!(result & EEH_STATE_DMA_ENABLED);
1463 mmio_en = !!(result & EEH_STATE_MMIO_ENABLED);
1466 ret = EEH_PE_STATE_RESET;
1467 else if (dma_en && mmio_en)
1468 ret = EEH_PE_STATE_NORMAL;
1469 else if (!dma_en && !mmio_en)
1470 ret = EEH_PE_STATE_STOPPED_IO_DMA;
1471 else if (!dma_en && mmio_en)
1472 ret = EEH_PE_STATE_STOPPED_DMA;
1474 ret = EEH_PE_STATE_UNAVAIL;
1478 EXPORT_SYMBOL_GPL(eeh_pe_get_state);
1480 static int eeh_pe_reenable_devices(struct eeh_pe *pe)
1482 struct eeh_dev *edev, *tmp;
1483 struct pci_dev *pdev;
1486 /* Restore config space */
1487 eeh_pe_restore_bars(pe);
1490 * Reenable PCI devices as the devices passed
1491 * through are always enabled before the reset.
1493 eeh_pe_for_each_dev(pe, edev, tmp) {
1494 pdev = eeh_dev_to_pci_dev(edev);
1498 ret = pci_reenable_device(pdev);
1500 pr_warn("%s: Failure %d reenabling %s\n",
1501 __func__, ret, pci_name(pdev));
1506 /* The PE is still in frozen state */
1507 return eeh_unfreeze_pe(pe, true);
1511 * eeh_pe_reset - Issue PE reset according to specified type
1513 * @option: reset type
1515 * The routine is called to reset the specified PE with the
1516 * indicated type, either fundamental reset or hot reset.
1517 * PE reset is the most important part for error recovery.
1519 int eeh_pe_reset(struct eeh_pe *pe, int option)
1527 if (!eeh_ops || !eeh_ops->set_option || !eeh_ops->reset)
1531 case EEH_RESET_DEACTIVATE:
1532 ret = eeh_ops->reset(pe, option);
1533 eeh_pe_state_clear(pe, EEH_PE_CFG_BLOCKED);
1537 ret = eeh_pe_reenable_devices(pe);
1540 case EEH_RESET_FUNDAMENTAL:
1542 * Proactively freeze the PE to drop all MMIO access
1543 * during reset, which should be banned as it's always
1544 * cause recursive EEH error.
1546 eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);
1548 eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
1549 ret = eeh_ops->reset(pe, option);
1552 pr_debug("%s: Unsupported option %d\n",
1559 EXPORT_SYMBOL_GPL(eeh_pe_reset);
1562 * eeh_pe_configure - Configure PCI bridges after PE reset
1565 * The routine is called to restore the PCI config space for
1566 * those PCI devices, especially PCI bridges affected by PE
1567 * reset issued previously.
1569 int eeh_pe_configure(struct eeh_pe *pe)
1579 EXPORT_SYMBOL_GPL(eeh_pe_configure);
1581 static int proc_eeh_show(struct seq_file *m, void *v)
1583 if (!eeh_enabled()) {
1584 seq_printf(m, "EEH Subsystem is globally disabled\n");
1585 seq_printf(m, "eeh_total_mmio_ffs=%llu\n", eeh_stats.total_mmio_ffs);
1587 seq_printf(m, "EEH Subsystem is enabled\n");
1590 "no device node=%llu\n"
1591 "no config address=%llu\n"
1592 "check not wanted=%llu\n"
1593 "eeh_total_mmio_ffs=%llu\n"
1594 "eeh_false_positives=%llu\n"
1595 "eeh_slot_resets=%llu\n",
1596 eeh_stats.no_device,
1598 eeh_stats.no_cfg_addr,
1599 eeh_stats.ignored_check,
1600 eeh_stats.total_mmio_ffs,
1601 eeh_stats.false_positives,
1602 eeh_stats.slot_resets);
1608 static int proc_eeh_open(struct inode *inode, struct file *file)
1610 return single_open(file, proc_eeh_show, NULL);
1613 static const struct file_operations proc_eeh_operations = {
1614 .open = proc_eeh_open,
1616 .llseek = seq_lseek,
1617 .release = single_release,
1620 #ifdef CONFIG_DEBUG_FS
1621 static int eeh_enable_dbgfs_set(void *data, u64 val)
1624 eeh_clear_flag(EEH_FORCE_DISABLED);
1626 eeh_add_flag(EEH_FORCE_DISABLED);
1628 /* Notify the backend */
1629 if (eeh_ops->post_init)
1630 eeh_ops->post_init();
1635 static int eeh_enable_dbgfs_get(void *data, u64 *val)
1644 DEFINE_SIMPLE_ATTRIBUTE(eeh_enable_dbgfs_ops, eeh_enable_dbgfs_get,
1645 eeh_enable_dbgfs_set, "0x%llx\n");
1648 static int __init eeh_init_proc(void)
1650 if (machine_is(pseries) || machine_is(powernv)) {
1651 proc_create("powerpc/eeh", 0, NULL, &proc_eeh_operations);
1652 #ifdef CONFIG_DEBUG_FS
1653 debugfs_create_file("eeh_enable", 0600,
1654 powerpc_debugfs_root, NULL,
1655 &eeh_enable_dbgfs_ops);
1661 __initcall(eeh_init_proc);