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/sched.h>
26 #include <linux/init.h>
27 #include <linux/list.h>
28 #include <linux/pci.h>
29 #include <linux/proc_fs.h>
30 #include <linux/rbtree.h>
31 #include <linux/seq_file.h>
32 #include <linux/spinlock.h>
33 #include <linux/export.h>
36 #include <linux/atomic.h>
38 #include <asm/eeh_event.h>
40 #include <asm/machdep.h>
41 #include <asm/ppc-pci.h>
46 * EEH, or "Extended Error Handling" is a PCI bridge technology for
47 * dealing with PCI bus errors that can't be dealt with within the
48 * usual PCI framework, except by check-stopping the CPU. Systems
49 * that are designed for high-availability/reliability cannot afford
50 * to crash due to a "mere" PCI error, thus the need for EEH.
51 * An EEH-capable bridge operates by converting a detected error
52 * into a "slot freeze", taking the PCI adapter off-line, making
53 * the slot behave, from the OS'es point of view, as if the slot
54 * were "empty": all reads return 0xff's and all writes are silently
55 * ignored. EEH slot isolation events can be triggered by parity
56 * errors on the address or data busses (e.g. during posted writes),
57 * which in turn might be caused by low voltage on the bus, dust,
58 * vibration, humidity, radioactivity or plain-old failed hardware.
60 * Note, however, that one of the leading causes of EEH slot
61 * freeze events are buggy device drivers, buggy device microcode,
62 * or buggy device hardware. This is because any attempt by the
63 * device to bus-master data to a memory address that is not
64 * assigned to the device will trigger a slot freeze. (The idea
65 * is to prevent devices-gone-wild from corrupting system memory).
66 * Buggy hardware/drivers will have a miserable time co-existing
69 * Ideally, a PCI device driver, when suspecting that an isolation
70 * event has occurred (e.g. by reading 0xff's), will then ask EEH
71 * whether this is the case, and then take appropriate steps to
72 * reset the PCI slot, the PCI device, and then resume operations.
73 * However, until that day, the checking is done here, with the
74 * eeh_check_failure() routine embedded in the MMIO macros. If
75 * the slot is found to be isolated, an "EEH Event" is synthesized
76 * and sent out for processing.
79 /* If a device driver keeps reading an MMIO register in an interrupt
80 * handler after a slot isolation event, it might be broken.
81 * This sets the threshold for how many read attempts we allow
82 * before printing an error message.
84 #define EEH_MAX_FAILS 2100000
86 /* Time to wait for a PCI slot to report status, in milliseconds */
87 #define PCI_BUS_RESET_WAIT_MSEC (60*1000)
89 /* Platform dependent EEH operations */
90 struct eeh_ops *eeh_ops = NULL;
92 int eeh_subsystem_enabled;
93 EXPORT_SYMBOL(eeh_subsystem_enabled);
95 /* Global EEH mutex */
96 DEFINE_MUTEX(eeh_mutex);
98 /* Lock to avoid races due to multiple reports of an error */
99 static DEFINE_RAW_SPINLOCK(confirm_error_lock);
101 /* Buffer for reporting pci register dumps. Its here in BSS, and
102 * not dynamically alloced, so that it ends up in RMO where RTAS
105 #define EEH_PCI_REGS_LOG_LEN 4096
106 static unsigned char pci_regs_buf[EEH_PCI_REGS_LOG_LEN];
109 * The struct is used to maintain the EEH global statistic
110 * information. Besides, the EEH global statistics will be
111 * exported to user space through procfs
114 u64 no_device; /* PCI device not found */
115 u64 no_dn; /* OF node not found */
116 u64 no_cfg_addr; /* Config address not found */
117 u64 ignored_check; /* EEH check skipped */
118 u64 total_mmio_ffs; /* Total EEH checks */
119 u64 false_positives; /* Unnecessary EEH checks */
120 u64 slot_resets; /* PE reset */
123 static struct eeh_stats eeh_stats;
125 #define IS_BRIDGE(class_code) (((class_code)<<16) == PCI_BASE_CLASS_BRIDGE)
128 * eeh_gather_pci_data - Copy assorted PCI config space registers to buff
129 * @edev: device to report data for
130 * @buf: point to buffer in which to log
131 * @len: amount of room in buffer
133 * This routine captures assorted PCI configuration space data,
134 * and puts them into a buffer for RTAS error logging.
136 static size_t eeh_gather_pci_data(struct eeh_dev *edev, char * buf, size_t len)
138 struct device_node *dn = eeh_dev_to_of_node(edev);
139 struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
144 n += scnprintf(buf+n, len-n, "%s\n", dn->full_name);
145 printk(KERN_WARNING "EEH: of node=%s\n", dn->full_name);
147 eeh_ops->read_config(dn, PCI_VENDOR_ID, 4, &cfg);
148 n += scnprintf(buf+n, len-n, "dev/vend:%08x\n", cfg);
149 printk(KERN_WARNING "EEH: PCI device/vendor: %08x\n", cfg);
151 eeh_ops->read_config(dn, PCI_COMMAND, 4, &cfg);
152 n += scnprintf(buf+n, len-n, "cmd/stat:%x\n", cfg);
153 printk(KERN_WARNING "EEH: PCI cmd/status register: %08x\n", cfg);
156 printk(KERN_WARNING "EEH: no PCI device for this of node\n");
160 /* Gather bridge-specific registers */
161 if (dev->class >> 16 == PCI_BASE_CLASS_BRIDGE) {
162 eeh_ops->read_config(dn, PCI_SEC_STATUS, 2, &cfg);
163 n += scnprintf(buf+n, len-n, "sec stat:%x\n", cfg);
164 printk(KERN_WARNING "EEH: Bridge secondary status: %04x\n", cfg);
166 eeh_ops->read_config(dn, PCI_BRIDGE_CONTROL, 2, &cfg);
167 n += scnprintf(buf+n, len-n, "brdg ctl:%x\n", cfg);
168 printk(KERN_WARNING "EEH: Bridge control: %04x\n", cfg);
171 /* Dump out the PCI-X command and status regs */
172 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
174 eeh_ops->read_config(dn, cap, 4, &cfg);
175 n += scnprintf(buf+n, len-n, "pcix-cmd:%x\n", cfg);
176 printk(KERN_WARNING "EEH: PCI-X cmd: %08x\n", cfg);
178 eeh_ops->read_config(dn, cap+4, 4, &cfg);
179 n += scnprintf(buf+n, len-n, "pcix-stat:%x\n", cfg);
180 printk(KERN_WARNING "EEH: PCI-X status: %08x\n", cfg);
183 /* If PCI-E capable, dump PCI-E cap 10, and the AER */
184 cap = pci_find_capability(dev, PCI_CAP_ID_EXP);
186 n += scnprintf(buf+n, len-n, "pci-e cap10:\n");
188 "EEH: PCI-E capabilities and status follow:\n");
190 for (i=0; i<=8; i++) {
191 eeh_ops->read_config(dn, cap+4*i, 4, &cfg);
192 n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
193 printk(KERN_WARNING "EEH: PCI-E %02x: %08x\n", i, cfg);
196 cap = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
198 n += scnprintf(buf+n, len-n, "pci-e AER:\n");
200 "EEH: PCI-E AER capability register set follows:\n");
202 for (i=0; i<14; i++) {
203 eeh_ops->read_config(dn, cap+4*i, 4, &cfg);
204 n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
205 printk(KERN_WARNING "EEH: PCI-E AER %02x: %08x\n", i, cfg);
210 /* Gather status on devices under the bridge */
211 if (dev->class >> 16 == PCI_BASE_CLASS_BRIDGE) {
212 struct device_node *child;
214 for_each_child_of_node(dn, child) {
215 if (of_node_to_eeh_dev(child))
216 n += eeh_gather_pci_data(of_node_to_eeh_dev(child), buf+n, len-n);
224 * eeh_slot_error_detail - Generate combined log including driver log and error log
225 * @edev: device to report error log for
226 * @severity: temporary or permanent error log
228 * This routine should be called to generate the combined log, which
229 * is comprised of driver log and error log. The driver log is figured
230 * out from the config space of the corresponding PCI device, while
231 * the error log is fetched through platform dependent function call.
233 void eeh_slot_error_detail(struct eeh_dev *edev, int severity)
238 eeh_pci_enable(edev, EEH_OPT_THAW_MMIO);
239 eeh_ops->configure_bridge(eeh_dev_to_of_node(edev));
240 eeh_restore_bars(edev);
241 loglen = eeh_gather_pci_data(edev, pci_regs_buf, EEH_PCI_REGS_LOG_LEN);
243 eeh_ops->get_log(eeh_dev_to_of_node(edev), severity, pci_regs_buf, loglen);
247 * eeh_token_to_phys - Convert EEH address token to phys address
248 * @token: I/O token, should be address in the form 0xA....
250 * This routine should be called to convert virtual I/O address
253 static inline unsigned long eeh_token_to_phys(unsigned long token)
258 ptep = find_linux_pte(init_mm.pgd, token);
261 pa = pte_pfn(*ptep) << PAGE_SHIFT;
263 return pa | (token & (PAGE_SIZE-1));
267 * eeh_dn_check_failure - Check if all 1's data is due to EEH slot freeze
269 * @dev: pci device, if known
271 * Check for an EEH failure for the given device node. Call this
272 * routine if the result of a read was all 0xff's and you want to
273 * find out if this is due to an EEH slot freeze. This routine
274 * will query firmware for the EEH status.
276 * Returns 0 if there has not been an EEH error; otherwise returns
277 * a non-zero value and queues up a slot isolation event notification.
279 * It is safe to call this routine in an interrupt context.
281 int eeh_dn_check_failure(struct device_node *dn, struct pci_dev *dev)
286 struct eeh_dev *edev;
288 const char *location;
290 eeh_stats.total_mmio_ffs++;
292 if (!eeh_subsystem_enabled)
296 edev = of_node_to_eeh_dev(dn);
298 edev = pci_dev_to_eeh_dev(dev);
299 dn = pci_device_to_OF_node(dev);
306 /* Access to IO BARs might get this far and still not want checking. */
308 eeh_stats.ignored_check++;
309 pr_debug("EEH: Ignored check for %s %s\n",
310 eeh_pci_name(dev), dn->full_name);
314 if (!pe->addr && !pe->config_addr) {
315 eeh_stats.no_cfg_addr++;
319 /* If we already have a pending isolation event for this
320 * slot, we know it's bad already, we don't need to check.
321 * Do this checking under a lock; as multiple PCI devices
322 * in one slot might report errors simultaneously, and we
323 * only want one error recovery routine running.
325 raw_spin_lock_irqsave(&confirm_error_lock, flags);
327 if (pe->state & EEH_PE_ISOLATED) {
329 if (pe->check_count % EEH_MAX_FAILS == 0) {
330 location = of_get_property(dn, "ibm,loc-code", NULL);
331 printk(KERN_ERR "EEH: %d reads ignored for recovering device at "
332 "location=%s driver=%s pci addr=%s\n",
333 pe->check_count, location,
334 eeh_driver_name(dev), eeh_pci_name(dev));
335 printk(KERN_ERR "EEH: Might be infinite loop in %s driver\n",
336 eeh_driver_name(dev));
343 * Now test for an EEH failure. This is VERY expensive.
344 * Note that the eeh_config_addr may be a parent device
345 * in the case of a device behind a bridge, or it may be
346 * function zero of a multi-function device.
347 * In any case they must share a common PHB.
349 ret = eeh_ops->get_state(pe, NULL);
351 /* Note that config-io to empty slots may fail;
352 * they are empty when they don't have children.
353 * We will punt with the following conditions: Failure to get
354 * PE's state, EEH not support and Permanently unavailable
355 * state, PE is in good state.
358 (ret == EEH_STATE_NOT_SUPPORT) ||
359 (ret & (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) ==
360 (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) {
361 eeh_stats.false_positives++;
362 pe->false_positives++;
367 eeh_stats.slot_resets++;
369 /* Avoid repeated reports of this failure, including problems
370 * with other functions on this device, and functions under
373 eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
374 raw_spin_unlock_irqrestore(&confirm_error_lock, flags);
376 eeh_send_failure_event(pe);
378 /* Most EEH events are due to device driver bugs. Having
379 * a stack trace will help the device-driver authors figure
380 * out what happened. So print that out.
382 WARN(1, "EEH: failure detected\n");
386 raw_spin_unlock_irqrestore(&confirm_error_lock, flags);
390 EXPORT_SYMBOL_GPL(eeh_dn_check_failure);
393 * eeh_check_failure - Check if all 1's data is due to EEH slot freeze
394 * @token: I/O token, should be address in the form 0xA....
395 * @val: value, should be all 1's (XXX why do we need this arg??)
397 * Check for an EEH failure at the given token address. Call this
398 * routine if the result of a read was all 0xff's and you want to
399 * find out if this is due to an EEH slot freeze event. This routine
400 * will query firmware for the EEH status.
402 * Note this routine is safe to call in an interrupt context.
404 unsigned long eeh_check_failure(const volatile void __iomem *token, unsigned long val)
408 struct device_node *dn;
410 /* Finding the phys addr + pci device; this is pretty quick. */
411 addr = eeh_token_to_phys((unsigned long __force) token);
412 dev = pci_addr_cache_get_device(addr);
414 eeh_stats.no_device++;
418 dn = pci_device_to_OF_node(dev);
419 eeh_dn_check_failure(dn, dev);
425 EXPORT_SYMBOL(eeh_check_failure);
429 * eeh_pci_enable - Enable MMIO or DMA transfers for this slot
430 * @edev: pci device node
432 * This routine should be called to reenable frozen MMIO or DMA
433 * so that it would work correctly again. It's useful while doing
434 * recovery or log collection on the indicated device.
436 int eeh_pci_enable(struct eeh_dev *edev, int function)
439 struct device_node *dn = eeh_dev_to_of_node(edev);
441 rc = eeh_ops->set_option(dn, function);
443 printk(KERN_WARNING "EEH: Unexpected state change %d, err=%d dn=%s\n",
444 function, rc, dn->full_name);
446 rc = eeh_ops->wait_state(dn, PCI_BUS_RESET_WAIT_MSEC);
447 if (rc > 0 && (rc & EEH_STATE_MMIO_ENABLED) &&
448 (function == EEH_OPT_THAW_MMIO))
455 * pcibios_set_pcie_slot_reset - Set PCI-E reset state
456 * @dev: pci device struct
457 * @state: reset state to enter
462 int pcibios_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
464 struct device_node *dn = pci_device_to_OF_node(dev);
467 case pcie_deassert_reset:
468 eeh_ops->reset(dn, EEH_RESET_DEACTIVATE);
471 eeh_ops->reset(dn, EEH_RESET_HOT);
473 case pcie_warm_reset:
474 eeh_ops->reset(dn, EEH_RESET_FUNDAMENTAL);
484 * __eeh_set_pe_freset - Check the required reset for child devices
485 * @parent: parent device
486 * @freset: return value
488 * Each device might have its preferred reset type: fundamental or
489 * hot reset. The routine is used to collect the information from
490 * the child devices so that they could be reset accordingly.
492 void __eeh_set_pe_freset(struct device_node *parent, unsigned int *freset)
494 struct device_node *dn;
496 for_each_child_of_node(parent, dn) {
497 if (of_node_to_eeh_dev(dn)) {
498 struct pci_dev *dev = of_node_to_eeh_dev(dn)->pdev;
500 if (dev && dev->driver)
501 *freset |= dev->needs_freset;
503 __eeh_set_pe_freset(dn, freset);
509 * eeh_set_pe_freset - Check the required reset for the indicated device and its children
511 * @freset: return value
513 * Each device might have its preferred reset type: fundamental or
514 * hot reset. The routine is used to collected the information for
515 * the indicated device and its children so that the bunch of the
516 * devices could be reset properly.
518 void eeh_set_pe_freset(struct device_node *dn, unsigned int *freset)
521 dn = eeh_find_device_pe(dn);
523 /* Back up one, since config addrs might be shared */
524 if (!pcibios_find_pci_bus(dn) && of_node_to_eeh_dev(dn->parent))
527 dev = of_node_to_eeh_dev(dn)->pdev;
529 *freset |= dev->needs_freset;
531 __eeh_set_pe_freset(dn, freset);
535 * eeh_reset_pe_once - Assert the pci #RST line for 1/4 second
536 * @edev: pci device node to be reset.
538 * Assert the PCI #RST line for 1/4 second.
540 static void eeh_reset_pe_once(struct eeh_dev *edev)
542 unsigned int freset = 0;
543 struct device_node *dn = eeh_dev_to_of_node(edev);
545 /* Determine type of EEH reset required for
546 * Partitionable Endpoint, a hot-reset (1)
547 * or a fundamental reset (3).
548 * A fundamental reset required by any device under
549 * Partitionable Endpoint trumps hot-reset.
551 eeh_set_pe_freset(dn, &freset);
554 eeh_ops->reset(dn, EEH_RESET_FUNDAMENTAL);
556 eeh_ops->reset(dn, EEH_RESET_HOT);
558 /* The PCI bus requires that the reset be held high for at least
559 * a 100 milliseconds. We wait a bit longer 'just in case'.
561 #define PCI_BUS_RST_HOLD_TIME_MSEC 250
562 msleep(PCI_BUS_RST_HOLD_TIME_MSEC);
564 /* We might get hit with another EEH freeze as soon as the
565 * pci slot reset line is dropped. Make sure we don't miss
566 * these, and clear the flag now.
568 eeh_clear_slot(dn, EEH_MODE_ISOLATED);
570 eeh_ops->reset(dn, EEH_RESET_DEACTIVATE);
572 /* After a PCI slot has been reset, the PCI Express spec requires
573 * a 1.5 second idle time for the bus to stabilize, before starting
576 #define PCI_BUS_SETTLE_TIME_MSEC 1800
577 msleep(PCI_BUS_SETTLE_TIME_MSEC);
581 * eeh_reset_pe - Reset the indicated PE
582 * @edev: PCI device associated EEH device
584 * This routine should be called to reset indicated device, including
585 * PE. A PE might include multiple PCI devices and sometimes PCI bridges
586 * might be involved as well.
588 int eeh_reset_pe(struct eeh_dev *edev)
591 struct device_node *dn = eeh_dev_to_of_node(edev);
593 /* Take three shots at resetting the bus */
594 for (i=0; i<3; i++) {
595 eeh_reset_pe_once(edev);
597 rc = eeh_ops->wait_state(dn, PCI_BUS_RESET_WAIT_MSEC);
598 if (rc == (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE))
602 printk(KERN_ERR "EEH: unrecoverable slot failure %s\n",
606 printk(KERN_ERR "EEH: bus reset %d failed on slot %s, rc=%d\n",
607 i+1, dn->full_name, rc);
613 /** Save and restore of PCI BARs
615 * Although firmware will set up BARs during boot, it doesn't
616 * set up device BAR's after a device reset, although it will,
617 * if requested, set up bridge configuration. Thus, we need to
618 * configure the PCI devices ourselves.
622 * eeh_restore_one_device_bars - Restore the Base Address Registers for one device
623 * @edev: PCI device associated EEH device
625 * Loads the PCI configuration space base address registers,
626 * the expansion ROM base address, the latency timer, and etc.
627 * from the saved values in the device node.
629 static inline void eeh_restore_one_device_bars(struct eeh_dev *edev)
633 struct device_node *dn = eeh_dev_to_of_node(edev);
638 for (i=4; i<10; i++) {
639 eeh_ops->write_config(dn, i*4, 4, edev->config_space[i]);
642 /* 12 == Expansion ROM Address */
643 eeh_ops->write_config(dn, 12*4, 4, edev->config_space[12]);
645 #define BYTE_SWAP(OFF) (8*((OFF)/4)+3-(OFF))
646 #define SAVED_BYTE(OFF) (((u8 *)(edev->config_space))[BYTE_SWAP(OFF)])
648 eeh_ops->write_config(dn, PCI_CACHE_LINE_SIZE, 1,
649 SAVED_BYTE(PCI_CACHE_LINE_SIZE));
651 eeh_ops->write_config(dn, PCI_LATENCY_TIMER, 1,
652 SAVED_BYTE(PCI_LATENCY_TIMER));
654 /* max latency, min grant, interrupt pin and line */
655 eeh_ops->write_config(dn, 15*4, 4, edev->config_space[15]);
657 /* Restore PERR & SERR bits, some devices require it,
658 * don't touch the other command bits
660 eeh_ops->read_config(dn, PCI_COMMAND, 4, &cmd);
661 if (edev->config_space[1] & PCI_COMMAND_PARITY)
662 cmd |= PCI_COMMAND_PARITY;
664 cmd &= ~PCI_COMMAND_PARITY;
665 if (edev->config_space[1] & PCI_COMMAND_SERR)
666 cmd |= PCI_COMMAND_SERR;
668 cmd &= ~PCI_COMMAND_SERR;
669 eeh_ops->write_config(dn, PCI_COMMAND, 4, cmd);
673 * eeh_restore_bars - Restore the PCI config space info
676 * This routine performs a recursive walk to the children
677 * of this device as well.
679 void eeh_restore_bars(struct eeh_dev *edev)
681 struct device_node *dn;
685 if ((edev->mode & EEH_MODE_SUPPORTED) && !IS_BRIDGE(edev->class_code))
686 eeh_restore_one_device_bars(edev);
688 for_each_child_of_node(eeh_dev_to_of_node(edev), dn)
689 eeh_restore_bars(of_node_to_eeh_dev(dn));
693 * eeh_save_bars - Save device bars
694 * @edev: PCI device associated EEH device
696 * Save the values of the device bars. Unlike the restore
697 * routine, this routine is *not* recursive. This is because
698 * PCI devices are added individually; but, for the restore,
699 * an entire slot is reset at a time.
701 static void eeh_save_bars(struct eeh_dev *edev)
704 struct device_node *dn;
708 dn = eeh_dev_to_of_node(edev);
710 for (i = 0; i < 16; i++)
711 eeh_ops->read_config(dn, i * 4, 4, &edev->config_space[i]);
715 * eeh_early_enable - Early enable EEH on the indicated device
719 * Enable EEH functionality on the specified PCI device. The function
720 * is expected to be called before real PCI probing is done. However,
721 * the PHBs have been initialized at this point.
723 static void *eeh_early_enable(struct device_node *dn, void *data)
726 const u32 *class_code = of_get_property(dn, "class-code", NULL);
727 const u32 *vendor_id = of_get_property(dn, "vendor-id", NULL);
728 const u32 *device_id = of_get_property(dn, "device-id", NULL);
731 struct eeh_dev *edev = of_node_to_eeh_dev(dn);
734 edev->class_code = 0;
736 edev->check_count = 0;
737 edev->freeze_count = 0;
738 edev->false_positives = 0;
740 if (!of_device_is_available(dn))
743 /* Ignore bad nodes. */
744 if (!class_code || !vendor_id || !device_id)
747 /* There is nothing to check on PCI to ISA bridges */
748 if (dn->type && !strcmp(dn->type, "isa")) {
749 edev->mode |= EEH_MODE_NOCHECK;
752 edev->class_code = *class_code;
754 /* Ok... see if this device supports EEH. Some do, some don't,
755 * and the only way to find out is to check each and every one.
757 regs = of_get_property(dn, "reg", NULL);
759 /* Initialize the fake PE */
760 memset(&pe, 0, sizeof(struct eeh_pe));
762 pe.config_addr = regs[0];
764 /* First register entry is addr (00BBSS00) */
765 /* Try to enable eeh */
766 ret = eeh_ops->set_option(&pe, EEH_OPT_ENABLE);
770 edev->config_addr = regs[0];
772 /* If the newer, better, ibm,get-config-addr-info is supported,
773 * then use that instead.
775 edev->pe_config_addr = eeh_ops->get_pe_addr(&pe);
776 pe.addr = edev->pe_config_addr;
778 /* Some older systems (Power4) allow the
779 * ibm,set-eeh-option call to succeed even on nodes
780 * where EEH is not supported. Verify support
783 ret = eeh_ops->get_state(&pe, NULL);
784 if (ret > 0 && ret != EEH_STATE_NOT_SUPPORT)
789 eeh_subsystem_enabled = 1;
790 edev->mode |= EEH_MODE_SUPPORTED;
792 eeh_add_to_parent_pe(edev);
794 pr_debug("EEH: %s: eeh enabled, config=%x pe_config=%x\n",
795 dn->full_name, edev->config_addr,
796 edev->pe_config_addr);
799 /* This device doesn't support EEH, but it may have an
800 * EEH parent, in which case we mark it as supported.
802 if (dn->parent && of_node_to_eeh_dev(dn->parent) &&
803 (of_node_to_eeh_dev(dn->parent)->mode & EEH_MODE_SUPPORTED)) {
804 /* Parent supports EEH. */
805 edev->mode |= EEH_MODE_SUPPORTED;
806 edev->config_addr = of_node_to_eeh_dev(dn->parent)->config_addr;
807 edev->pe_config_addr = of_node_to_eeh_dev(dn->parent)->pe_config_addr;
809 eeh_add_to_parent_pe(edev);
815 printk(KERN_WARNING "EEH: %s: unable to get reg property.\n",
824 * eeh_ops_register - Register platform dependent EEH operations
825 * @ops: platform dependent EEH operations
827 * Register the platform dependent EEH operation callback
828 * functions. The platform should call this function before
829 * any other EEH operations.
831 int __init eeh_ops_register(struct eeh_ops *ops)
834 pr_warning("%s: Invalid EEH ops name for %p\n",
839 if (eeh_ops && eeh_ops != ops) {
840 pr_warning("%s: EEH ops of platform %s already existing (%s)\n",
841 __func__, eeh_ops->name, ops->name);
851 * eeh_ops_unregister - Unreigster platform dependent EEH operations
852 * @name: name of EEH platform operations
854 * Unregister the platform dependent EEH operation callback
857 int __exit eeh_ops_unregister(const char *name)
859 if (!name || !strlen(name)) {
860 pr_warning("%s: Invalid EEH ops name\n",
865 if (eeh_ops && !strcmp(eeh_ops->name, name)) {
874 * eeh_init - EEH initialization
876 * Initialize EEH by trying to enable it for all of the adapters in the system.
877 * As a side effect we can determine here if eeh is supported at all.
878 * Note that we leave EEH on so failed config cycles won't cause a machine
879 * check. If a user turns off EEH for a particular adapter they are really
880 * telling Linux to ignore errors. Some hardware (e.g. POWER5) won't
881 * grant access to a slot if EEH isn't enabled, and so we always enable
882 * EEH for all slots/all devices.
884 * The eeh-force-off option disables EEH checking globally, for all slots.
885 * Even if force-off is set, the EEH hardware is still enabled, so that
886 * newer systems can boot.
888 static int __init eeh_init(void)
890 struct pci_controller *hose, *tmp;
891 struct device_node *phb;
894 /* call platform initialization function */
896 pr_warning("%s: Platform EEH operation not found\n",
899 } else if ((ret = eeh_ops->init())) {
900 pr_warning("%s: Failed to call platform init function (%d)\n",
905 raw_spin_lock_init(&confirm_error_lock);
907 /* Enable EEH for all adapters */
908 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
910 traverse_pci_devices(phb, eeh_early_enable, NULL);
913 if (eeh_subsystem_enabled)
914 printk(KERN_INFO "EEH: PCI Enhanced I/O Error Handling Enabled\n");
916 printk(KERN_WARNING "EEH: No capable adapters found\n");
921 core_initcall_sync(eeh_init);
924 * eeh_add_device_early - Enable EEH for the indicated device_node
925 * @dn: device node for which to set up EEH
927 * This routine must be used to perform EEH initialization for PCI
928 * devices that were added after system boot (e.g. hotplug, dlpar).
929 * This routine must be called before any i/o is performed to the
930 * adapter (inluding any config-space i/o).
931 * Whether this actually enables EEH or not for this device depends
932 * on the CEC architecture, type of the device, on earlier boot
933 * command-line arguments & etc.
935 static void eeh_add_device_early(struct device_node *dn)
937 struct pci_controller *phb;
939 if (!dn || !of_node_to_eeh_dev(dn))
941 phb = of_node_to_eeh_dev(dn)->phb;
943 /* USB Bus children of PCI devices will not have BUID's */
944 if (NULL == phb || 0 == phb->buid)
947 eeh_early_enable(dn, NULL);
951 * eeh_add_device_tree_early - Enable EEH for the indicated device
954 * This routine must be used to perform EEH initialization for the
955 * indicated PCI device that was added after system boot (e.g.
958 void eeh_add_device_tree_early(struct device_node *dn)
960 struct device_node *sib;
962 for_each_child_of_node(dn, sib)
963 eeh_add_device_tree_early(sib);
964 eeh_add_device_early(dn);
966 EXPORT_SYMBOL_GPL(eeh_add_device_tree_early);
969 * eeh_add_device_late - Perform EEH initialization for the indicated pci device
970 * @dev: pci device for which to set up EEH
972 * This routine must be used to complete EEH initialization for PCI
973 * devices that were added after system boot (e.g. hotplug, dlpar).
975 static void eeh_add_device_late(struct pci_dev *dev)
977 struct device_node *dn;
978 struct eeh_dev *edev;
980 if (!dev || !eeh_subsystem_enabled)
983 pr_debug("EEH: Adding device %s\n", pci_name(dev));
985 dn = pci_device_to_OF_node(dev);
986 edev = of_node_to_eeh_dev(dn);
987 if (edev->pdev == dev) {
988 pr_debug("EEH: Already referenced !\n");
995 dev->dev.archdata.edev = edev;
997 pci_addr_cache_insert_device(dev);
998 eeh_sysfs_add_device(dev);
1002 * eeh_add_device_tree_late - Perform EEH initialization for the indicated PCI bus
1005 * This routine must be used to perform EEH initialization for PCI
1006 * devices which are attached to the indicated PCI bus. The PCI bus
1007 * is added after system boot through hotplug or dlpar.
1009 void eeh_add_device_tree_late(struct pci_bus *bus)
1011 struct pci_dev *dev;
1013 list_for_each_entry(dev, &bus->devices, bus_list) {
1014 eeh_add_device_late(dev);
1015 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
1016 struct pci_bus *subbus = dev->subordinate;
1018 eeh_add_device_tree_late(subbus);
1022 EXPORT_SYMBOL_GPL(eeh_add_device_tree_late);
1025 * eeh_remove_device - Undo EEH setup for the indicated pci device
1026 * @dev: pci device to be removed
1028 * This routine should be called when a device is removed from
1029 * a running system (e.g. by hotplug or dlpar). It unregisters
1030 * the PCI device from the EEH subsystem. I/O errors affecting
1031 * this device will no longer be detected after this call; thus,
1032 * i/o errors affecting this slot may leave this device unusable.
1034 static void eeh_remove_device(struct pci_dev *dev)
1036 struct eeh_dev *edev;
1038 if (!dev || !eeh_subsystem_enabled)
1040 edev = pci_dev_to_eeh_dev(dev);
1042 /* Unregister the device with the EEH/PCI address search system */
1043 pr_debug("EEH: Removing device %s\n", pci_name(dev));
1045 if (!edev || !edev->pdev) {
1046 pr_debug("EEH: Not referenced !\n");
1050 dev->dev.archdata.edev = NULL;
1053 eeh_rmv_from_parent_pe(edev);
1054 pci_addr_cache_remove_device(dev);
1055 eeh_sysfs_remove_device(dev);
1059 * eeh_remove_bus_device - Undo EEH setup for the indicated PCI device
1062 * This routine must be called when a device is removed from the
1063 * running system through hotplug or dlpar. The corresponding
1064 * PCI address cache will be removed.
1066 void eeh_remove_bus_device(struct pci_dev *dev)
1068 struct pci_bus *bus = dev->subordinate;
1069 struct pci_dev *child, *tmp;
1071 eeh_remove_device(dev);
1073 if (bus && dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
1074 list_for_each_entry_safe(child, tmp, &bus->devices, bus_list)
1075 eeh_remove_bus_device(child);
1078 EXPORT_SYMBOL_GPL(eeh_remove_bus_device);
1080 static int proc_eeh_show(struct seq_file *m, void *v)
1082 if (0 == eeh_subsystem_enabled) {
1083 seq_printf(m, "EEH Subsystem is globally disabled\n");
1084 seq_printf(m, "eeh_total_mmio_ffs=%llu\n", eeh_stats.total_mmio_ffs);
1086 seq_printf(m, "EEH Subsystem is enabled\n");
1089 "no device node=%llu\n"
1090 "no config address=%llu\n"
1091 "check not wanted=%llu\n"
1092 "eeh_total_mmio_ffs=%llu\n"
1093 "eeh_false_positives=%llu\n"
1094 "eeh_slot_resets=%llu\n",
1095 eeh_stats.no_device,
1097 eeh_stats.no_cfg_addr,
1098 eeh_stats.ignored_check,
1099 eeh_stats.total_mmio_ffs,
1100 eeh_stats.false_positives,
1101 eeh_stats.slot_resets);
1107 static int proc_eeh_open(struct inode *inode, struct file *file)
1109 return single_open(file, proc_eeh_show, NULL);
1112 static const struct file_operations proc_eeh_operations = {
1113 .open = proc_eeh_open,
1115 .llseek = seq_lseek,
1116 .release = single_release,
1119 static int __init eeh_init_proc(void)
1121 if (machine_is(pseries))
1122 proc_create("powerpc/eeh", 0, NULL, &proc_eeh_operations);
1125 __initcall(eeh_init_proc);