blob: d6560c45637bb06afaee30d56cbef3c52fe1d151 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * eeh.c
3 * Copyright (C) 2001 Dave Engebretsen & Todd Inglett IBM Corporation
Linas Vepstas69376502005-11-03 18:47:50 -06004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
Linas Vepstas69376502005-11-03 18:47:50 -06009 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Linas Vepstas69376502005-11-03 18:47:50 -060014 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
Linas Vepstas6dee3fb2005-11-03 18:50:10 -060020#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/init.h>
22#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/pci.h>
24#include <linux/proc_fs.h>
25#include <linux/rbtree.h>
26#include <linux/seq_file.h>
27#include <linux/spinlock.h>
Linas Vepstas69376502005-11-03 18:47:50 -060028#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/eeh.h>
Linas Vepstas172ca922005-11-03 18:50:04 -060030#include <asm/eeh_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/io.h>
32#include <asm/machdep.h>
Stephen Rothwelld3878992005-09-28 02:50:25 +100033#include <asm/ppc-pci.h>
Linas Vepstas172ca922005-11-03 18:50:04 -060034#include <asm/rtas.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#undef DEBUG
37
38/** Overview:
39 * EEH, or "Extended Error Handling" is a PCI bridge technology for
40 * dealing with PCI bus errors that can't be dealt with within the
41 * usual PCI framework, except by check-stopping the CPU. Systems
42 * that are designed for high-availability/reliability cannot afford
43 * to crash due to a "mere" PCI error, thus the need for EEH.
44 * An EEH-capable bridge operates by converting a detected error
45 * into a "slot freeze", taking the PCI adapter off-line, making
46 * the slot behave, from the OS'es point of view, as if the slot
47 * were "empty": all reads return 0xff's and all writes are silently
48 * ignored. EEH slot isolation events can be triggered by parity
49 * errors on the address or data busses (e.g. during posted writes),
Linas Vepstas69376502005-11-03 18:47:50 -060050 * which in turn might be caused by low voltage on the bus, dust,
51 * vibration, humidity, radioactivity or plain-old failed hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 *
53 * Note, however, that one of the leading causes of EEH slot
54 * freeze events are buggy device drivers, buggy device microcode,
55 * or buggy device hardware. This is because any attempt by the
56 * device to bus-master data to a memory address that is not
57 * assigned to the device will trigger a slot freeze. (The idea
58 * is to prevent devices-gone-wild from corrupting system memory).
59 * Buggy hardware/drivers will have a miserable time co-existing
60 * with EEH.
61 *
62 * Ideally, a PCI device driver, when suspecting that an isolation
63 * event has occured (e.g. by reading 0xff's), will then ask EEH
64 * whether this is the case, and then take appropriate steps to
65 * reset the PCI slot, the PCI device, and then resume operations.
66 * However, until that day, the checking is done here, with the
67 * eeh_check_failure() routine embedded in the MMIO macros. If
68 * the slot is found to be isolated, an "EEH Event" is synthesized
69 * and sent out for processing.
70 */
71
Linas Vepstas5c1344e2005-11-03 18:49:31 -060072/* If a device driver keeps reading an MMIO register in an interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 * handler after a slot isolation event has occurred, we assume it
74 * is broken and panic. This sets the threshold for how many read
75 * attempts we allow before panicking.
76 */
Linas Vepstas5c1344e2005-11-03 18:49:31 -060077#define EEH_MAX_FAILS 100000
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Linas Vepstas8b553f32005-11-03 18:50:17 -060079/* Misc forward declaraions */
80static void eeh_save_bars(struct pci_dev * pdev, struct pci_dn *pdn);
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/* RTAS tokens */
83static int ibm_set_eeh_option;
84static int ibm_set_slot_reset;
85static int ibm_read_slot_reset_state;
86static int ibm_read_slot_reset_state2;
87static int ibm_slot_error_detail;
88
David Woodhouse1e28a7d2005-11-17 00:44:03 +000089int eeh_subsystem_enabled;
90EXPORT_SYMBOL(eeh_subsystem_enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Linas Vepstasfd761fd2005-11-03 18:49:23 -060092/* Lock to avoid races due to multiple reports of an error */
93static DEFINE_SPINLOCK(confirm_error_lock);
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/* Buffer for reporting slot-error-detail rtas calls */
96static unsigned char slot_errbuf[RTAS_ERROR_LOG_MAX];
97static DEFINE_SPINLOCK(slot_errbuf_lock);
98static int eeh_error_buf_size;
99
100/* System monitoring statistics */
Linas Vepstas177bc932005-11-03 18:48:52 -0600101static DEFINE_PER_CPU(unsigned long, no_device);
102static DEFINE_PER_CPU(unsigned long, no_dn);
103static DEFINE_PER_CPU(unsigned long, no_cfg_addr);
104static DEFINE_PER_CPU(unsigned long, ignored_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static DEFINE_PER_CPU(unsigned long, total_mmio_ffs);
106static DEFINE_PER_CPU(unsigned long, false_positives);
107static DEFINE_PER_CPU(unsigned long, ignored_failures);
108static DEFINE_PER_CPU(unsigned long, slot_resets);
109
110/**
111 * The pci address cache subsystem. This subsystem places
112 * PCI device address resources into a red-black tree, sorted
113 * according to the address range, so that given only an i/o
114 * address, the corresponding PCI device can be **quickly**
115 * found. It is safe to perform an address lookup in an interrupt
116 * context; this ability is an important feature.
117 *
118 * Currently, the only customer of this code is the EEH subsystem;
119 * thus, this code has been somewhat tailored to suit EEH better.
120 * In particular, the cache does *not* hold the addresses of devices
121 * for which EEH is not enabled.
122 *
123 * (Implementation Note: The RB tree seems to be better/faster
124 * than any hash algo I could think of for this problem, even
125 * with the penalty of slow pointer chases for d-cache misses).
126 */
127struct pci_io_addr_range
128{
129 struct rb_node rb_node;
130 unsigned long addr_lo;
131 unsigned long addr_hi;
132 struct pci_dev *pcidev;
133 unsigned int flags;
134};
135
136static struct pci_io_addr_cache
137{
138 struct rb_root rb_root;
139 spinlock_t piar_lock;
140} pci_io_addr_cache_root;
141
142static inline struct pci_dev *__pci_get_device_by_addr(unsigned long addr)
143{
144 struct rb_node *n = pci_io_addr_cache_root.rb_root.rb_node;
145
146 while (n) {
147 struct pci_io_addr_range *piar;
148 piar = rb_entry(n, struct pci_io_addr_range, rb_node);
149
150 if (addr < piar->addr_lo) {
151 n = n->rb_left;
152 } else {
153 if (addr > piar->addr_hi) {
154 n = n->rb_right;
155 } else {
156 pci_dev_get(piar->pcidev);
157 return piar->pcidev;
158 }
159 }
160 }
161
162 return NULL;
163}
164
165/**
166 * pci_get_device_by_addr - Get device, given only address
167 * @addr: mmio (PIO) phys address or i/o port number
168 *
169 * Given an mmio phys address, or a port number, find a pci device
170 * that implements this address. Be sure to pci_dev_put the device
171 * when finished. I/O port numbers are assumed to be offset
172 * from zero (that is, they do *not* have pci_io_addr added in).
173 * It is safe to call this function within an interrupt.
174 */
175static struct pci_dev *pci_get_device_by_addr(unsigned long addr)
176{
177 struct pci_dev *dev;
178 unsigned long flags;
179
180 spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
181 dev = __pci_get_device_by_addr(addr);
182 spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
183 return dev;
184}
185
186#ifdef DEBUG
187/*
188 * Handy-dandy debug print routine, does nothing more
189 * than print out the contents of our addr cache.
190 */
191static void pci_addr_cache_print(struct pci_io_addr_cache *cache)
192{
193 struct rb_node *n;
194 int cnt = 0;
195
196 n = rb_first(&cache->rb_root);
197 while (n) {
198 struct pci_io_addr_range *piar;
199 piar = rb_entry(n, struct pci_io_addr_range, rb_node);
Adrian Bunk982245f2005-07-17 04:22:20 +0200200 printk(KERN_DEBUG "PCI: %s addr range %d [%lx-%lx]: %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 (piar->flags & IORESOURCE_IO) ? "i/o" : "mem", cnt,
Adrian Bunk982245f2005-07-17 04:22:20 +0200202 piar->addr_lo, piar->addr_hi, pci_name(piar->pcidev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 cnt++;
204 n = rb_next(n);
205 }
206}
207#endif
208
209/* Insert address range into the rb tree. */
210static struct pci_io_addr_range *
211pci_addr_cache_insert(struct pci_dev *dev, unsigned long alo,
212 unsigned long ahi, unsigned int flags)
213{
214 struct rb_node **p = &pci_io_addr_cache_root.rb_root.rb_node;
215 struct rb_node *parent = NULL;
216 struct pci_io_addr_range *piar;
217
218 /* Walk tree, find a place to insert into tree */
219 while (*p) {
220 parent = *p;
221 piar = rb_entry(parent, struct pci_io_addr_range, rb_node);
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600222 if (ahi < piar->addr_lo) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 p = &parent->rb_left;
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600224 } else if (alo > piar->addr_hi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 p = &parent->rb_right;
226 } else {
227 if (dev != piar->pcidev ||
228 alo != piar->addr_lo || ahi != piar->addr_hi) {
229 printk(KERN_WARNING "PIAR: overlapping address range\n");
230 }
231 return piar;
232 }
233 }
234 piar = (struct pci_io_addr_range *)kmalloc(sizeof(struct pci_io_addr_range), GFP_ATOMIC);
235 if (!piar)
236 return NULL;
237
238 piar->addr_lo = alo;
239 piar->addr_hi = ahi;
240 piar->pcidev = dev;
241 piar->flags = flags;
242
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600243#ifdef DEBUG
244 printk(KERN_DEBUG "PIAR: insert range=[%lx:%lx] dev=%s\n",
245 alo, ahi, pci_name (dev));
246#endif
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 rb_link_node(&piar->rb_node, parent, p);
249 rb_insert_color(&piar->rb_node, &pci_io_addr_cache_root.rb_root);
250
251 return piar;
252}
253
254static void __pci_addr_cache_insert_device(struct pci_dev *dev)
255{
256 struct device_node *dn;
Paul Mackerras16353172005-09-06 13:17:54 +1000257 struct pci_dn *pdn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 int i;
259 int inserted = 0;
260
261 dn = pci_device_to_OF_node(dev);
262 if (!dn) {
Linas Vepstas69376502005-11-03 18:47:50 -0600263 printk(KERN_WARNING "PCI: no pci dn found for dev=%s\n", pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 return;
265 }
266
267 /* Skip any devices for which EEH is not enabled. */
Linas Vepstas69376502005-11-03 18:47:50 -0600268 pdn = PCI_DN(dn);
Paul Mackerras16353172005-09-06 13:17:54 +1000269 if (!(pdn->eeh_mode & EEH_MODE_SUPPORTED) ||
270 pdn->eeh_mode & EEH_MODE_NOCHECK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271#ifdef DEBUG
Linas Vepstas69376502005-11-03 18:47:50 -0600272 printk(KERN_INFO "PCI: skip building address cache for=%s - %s\n",
273 pci_name(dev), pdn->node->full_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274#endif
275 return;
276 }
277
278 /* The cache holds a reference to the device... */
279 pci_dev_get(dev);
280
281 /* Walk resources on this device, poke them into the tree */
282 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
283 unsigned long start = pci_resource_start(dev,i);
284 unsigned long end = pci_resource_end(dev,i);
285 unsigned int flags = pci_resource_flags(dev,i);
286
287 /* We are interested only bus addresses, not dma or other stuff */
288 if (0 == (flags & (IORESOURCE_IO | IORESOURCE_MEM)))
289 continue;
290 if (start == 0 || ~start == 0 || end == 0 || ~end == 0)
291 continue;
292 pci_addr_cache_insert(dev, start, end, flags);
293 inserted = 1;
294 }
295
296 /* If there was nothing to add, the cache has no reference... */
297 if (!inserted)
298 pci_dev_put(dev);
299}
300
301/**
302 * pci_addr_cache_insert_device - Add a device to the address cache
303 * @dev: PCI device whose I/O addresses we are interested in.
304 *
305 * In order to support the fast lookup of devices based on addresses,
306 * we maintain a cache of devices that can be quickly searched.
307 * This routine adds a device to that cache.
308 */
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600309static void pci_addr_cache_insert_device(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 unsigned long flags;
312
313 spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
314 __pci_addr_cache_insert_device(dev);
315 spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
316}
317
318static inline void __pci_addr_cache_remove_device(struct pci_dev *dev)
319{
320 struct rb_node *n;
321 int removed = 0;
322
323restart:
324 n = rb_first(&pci_io_addr_cache_root.rb_root);
325 while (n) {
326 struct pci_io_addr_range *piar;
327 piar = rb_entry(n, struct pci_io_addr_range, rb_node);
328
329 if (piar->pcidev == dev) {
330 rb_erase(n, &pci_io_addr_cache_root.rb_root);
331 removed = 1;
332 kfree(piar);
333 goto restart;
334 }
335 n = rb_next(n);
336 }
337
338 /* The cache no longer holds its reference to this device... */
339 if (removed)
340 pci_dev_put(dev);
341}
342
343/**
344 * pci_addr_cache_remove_device - remove pci device from addr cache
345 * @dev: device to remove
346 *
347 * Remove a device from the addr-cache tree.
348 * This is potentially expensive, since it will walk
349 * the tree multiple times (once per resource).
350 * But so what; device removal doesn't need to be that fast.
351 */
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600352static void pci_addr_cache_remove_device(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 unsigned long flags;
355
356 spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
357 __pci_addr_cache_remove_device(dev);
358 spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
359}
360
361/**
362 * pci_addr_cache_build - Build a cache of I/O addresses
363 *
364 * Build a cache of pci i/o addresses. This cache will be used to
365 * find the pci device that corresponds to a given address.
366 * This routine scans all pci busses to build the cache.
367 * Must be run late in boot process, after the pci controllers
368 * have been scaned for devices (after all device resources are known).
369 */
370void __init pci_addr_cache_build(void)
371{
Linas Vepstas8b553f32005-11-03 18:50:17 -0600372 struct device_node *dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 struct pci_dev *dev = NULL;
374
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600375 if (!eeh_subsystem_enabled)
376 return;
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 spin_lock_init(&pci_io_addr_cache_root.piar_lock);
379
380 while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
381 /* Ignore PCI bridges ( XXX why ??) */
382 if ((dev->class >> 16) == PCI_BASE_CLASS_BRIDGE) {
383 continue;
384 }
385 pci_addr_cache_insert_device(dev);
Linas Vepstas8b553f32005-11-03 18:50:17 -0600386
387 /* Save the BAR's; firmware doesn't restore these after EEH reset */
388 dn = pci_device_to_OF_node(dev);
389 eeh_save_bars(dev, PCI_DN(dn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391
392#ifdef DEBUG
393 /* Verify tree built up above, echo back the list of addrs. */
394 pci_addr_cache_print(&pci_io_addr_cache_root);
395#endif
396}
397
398/* --------------------------------------------------------------- */
399/* Above lies the PCI Address Cache. Below lies the EEH event infrastructure */
400
Linas Vepstasdf7242b2005-11-03 18:49:01 -0600401void eeh_slot_error_detail (struct pci_dn *pdn, int severity)
402{
403 unsigned long flags;
404 int rc;
405
406 /* Log the error with the rtas logger */
407 spin_lock_irqsave(&slot_errbuf_lock, flags);
408 memset(slot_errbuf, 0, eeh_error_buf_size);
409
410 rc = rtas_call(ibm_slot_error_detail,
411 8, 1, NULL, pdn->eeh_config_addr,
412 BUID_HI(pdn->phb->buid),
413 BUID_LO(pdn->phb->buid), NULL, 0,
414 virt_to_phys(slot_errbuf),
415 eeh_error_buf_size,
416 severity);
417
418 if (rc == 0)
419 log_error(slot_errbuf, ERR_TYPE_RTAS_LOG, 0);
420 spin_unlock_irqrestore(&slot_errbuf_lock, flags);
421}
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 * read_slot_reset_state - Read the reset state of a device node's slot
425 * @dn: device node to read
426 * @rets: array to return results in
427 */
Linas Vepstas69376502005-11-03 18:47:50 -0600428static int read_slot_reset_state(struct pci_dn *pdn, int rets[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
430 int token, outputs;
431
432 if (ibm_read_slot_reset_state2 != RTAS_UNKNOWN_SERVICE) {
433 token = ibm_read_slot_reset_state2;
434 outputs = 4;
435 } else {
436 token = ibm_read_slot_reset_state;
Linas Vepstas69376502005-11-03 18:47:50 -0600437 rets[2] = 0; /* fake PE Unavailable info */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 outputs = 3;
439 }
440
Paul Mackerras16353172005-09-06 13:17:54 +1000441 return rtas_call(token, 3, outputs, rets, pdn->eeh_config_addr,
442 BUID_HI(pdn->phb->buid), BUID_LO(pdn->phb->buid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
445/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 * eeh_token_to_phys - convert EEH address token to phys address
Linas Vepstas69376502005-11-03 18:47:50 -0600447 * @token i/o token, should be address in the form 0xA....
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 */
449static inline unsigned long eeh_token_to_phys(unsigned long token)
450{
451 pte_t *ptep;
452 unsigned long pa;
453
David Gibson20cee162005-06-21 17:15:31 -0700454 ptep = find_linux_pte(init_mm.pgd, token);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (!ptep)
456 return token;
457 pa = pte_pfn(*ptep) << PAGE_SHIFT;
458
459 return pa | (token & (PAGE_SIZE-1));
460}
461
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600462/**
463 * Return the "partitionable endpoint" (pe) under which this device lies
464 */
465static struct device_node * find_device_pe(struct device_node *dn)
466{
467 while ((dn->parent) && PCI_DN(dn->parent) &&
468 (PCI_DN(dn->parent)->eeh_mode & EEH_MODE_SUPPORTED)) {
469 dn = dn->parent;
470 }
471 return dn;
472}
473
474/** Mark all devices that are peers of this device as failed.
475 * Mark the device driver too, so that it can see the failure
476 * immediately; this is critical, since some drivers poll
477 * status registers in interrupts ... If a driver is polling,
478 * and the slot is frozen, then the driver can deadlock in
479 * an interrupt context, which is bad.
480 */
481
Linas Vepstasd9564ad2005-11-03 18:50:48 -0600482static void __eeh_mark_slot (struct device_node *dn, int mode_flag)
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600483{
484 while (dn) {
Linas Vepstasd9564ad2005-11-03 18:50:48 -0600485 if (PCI_DN(dn)) {
486 PCI_DN(dn)->eeh_mode |= mode_flag;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600487
Linas Vepstas77bd7412005-11-03 18:52:49 -0600488 /* Mark the pci device driver too */
489 struct pci_dev *dev = PCI_DN(dn)->pcidev;
490 if (dev && dev->driver)
491 dev->error_state = pci_channel_io_frozen;
492
Linas Vepstasd9564ad2005-11-03 18:50:48 -0600493 if (dn->child)
494 __eeh_mark_slot (dn->child, mode_flag);
495 }
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600496 dn = dn->sibling;
497 }
498}
499
Linas Vepstasd9564ad2005-11-03 18:50:48 -0600500void eeh_mark_slot (struct device_node *dn, int mode_flag)
501{
502 dn = find_device_pe (dn);
503 PCI_DN(dn)->eeh_mode |= mode_flag;
504 __eeh_mark_slot (dn->child, mode_flag);
505}
506
507static void __eeh_clear_slot (struct device_node *dn, int mode_flag)
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600508{
509 while (dn) {
Linas Vepstasd9564ad2005-11-03 18:50:48 -0600510 if (PCI_DN(dn)) {
511 PCI_DN(dn)->eeh_mode &= ~mode_flag;
512 PCI_DN(dn)->eeh_check_count = 0;
513 if (dn->child)
514 __eeh_clear_slot (dn->child, mode_flag);
515 }
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600516 dn = dn->sibling;
517 }
518}
519
Linas Vepstasd9564ad2005-11-03 18:50:48 -0600520void eeh_clear_slot (struct device_node *dn, int mode_flag)
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600521{
522 unsigned long flags;
523 spin_lock_irqsave(&confirm_error_lock, flags);
Linas Vepstasd9564ad2005-11-03 18:50:48 -0600524 dn = find_device_pe (dn);
525 PCI_DN(dn)->eeh_mode &= ~mode_flag;
526 PCI_DN(dn)->eeh_check_count = 0;
527 __eeh_clear_slot (dn->child, mode_flag);
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600528 spin_unlock_irqrestore(&confirm_error_lock, flags);
529}
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531/**
532 * eeh_dn_check_failure - check if all 1's data is due to EEH slot freeze
533 * @dn device node
534 * @dev pci device, if known
535 *
536 * Check for an EEH failure for the given device node. Call this
537 * routine if the result of a read was all 0xff's and you want to
538 * find out if this is due to an EEH slot freeze. This routine
539 * will query firmware for the EEH status.
540 *
541 * Returns 0 if there has not been an EEH error; otherwise returns
Linas Vepstas69376502005-11-03 18:47:50 -0600542 * a non-zero value and queues up a slot isolation event notification.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 *
544 * It is safe to call this routine in an interrupt context.
545 */
546int eeh_dn_check_failure(struct device_node *dn, struct pci_dev *dev)
547{
548 int ret;
549 int rets[3];
550 unsigned long flags;
Paul Mackerras16353172005-09-06 13:17:54 +1000551 struct pci_dn *pdn;
Linas Vepstas77bd7412005-11-03 18:52:49 -0600552 enum pci_channel_state state;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600553 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 __get_cpu_var(total_mmio_ffs)++;
556
557 if (!eeh_subsystem_enabled)
558 return 0;
559
Linas Vepstas177bc932005-11-03 18:48:52 -0600560 if (!dn) {
561 __get_cpu_var(no_dn)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return 0;
Linas Vepstas177bc932005-11-03 18:48:52 -0600563 }
Linas Vepstas69376502005-11-03 18:47:50 -0600564 pdn = PCI_DN(dn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 /* Access to IO BARs might get this far and still not want checking. */
Linas Vepstasf8632c82005-11-03 18:49:45 -0600567 if (!(pdn->eeh_mode & EEH_MODE_SUPPORTED) ||
Paul Mackerras16353172005-09-06 13:17:54 +1000568 pdn->eeh_mode & EEH_MODE_NOCHECK) {
Linas Vepstas177bc932005-11-03 18:48:52 -0600569 __get_cpu_var(ignored_check)++;
570#ifdef DEBUG
Linas Vepstasf8632c82005-11-03 18:49:45 -0600571 printk ("EEH:ignored check (%x) for %s %s\n",
572 pdn->eeh_mode, pci_name (dev), dn->full_name);
Linas Vepstas177bc932005-11-03 18:48:52 -0600573#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return 0;
575 }
576
Paul Mackerras16353172005-09-06 13:17:54 +1000577 if (!pdn->eeh_config_addr) {
Linas Vepstas177bc932005-11-03 18:48:52 -0600578 __get_cpu_var(no_cfg_addr)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return 0;
580 }
581
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600582 /* If we already have a pending isolation event for this
583 * slot, we know it's bad already, we don't need to check.
584 * Do this checking under a lock; as multiple PCI devices
585 * in one slot might report errors simultaneously, and we
586 * only want one error recovery routine running.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 */
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600588 spin_lock_irqsave(&confirm_error_lock, flags);
589 rc = 1;
Paul Mackerras16353172005-09-06 13:17:54 +1000590 if (pdn->eeh_mode & EEH_MODE_ISOLATED) {
Linas Vepstas5c1344e2005-11-03 18:49:31 -0600591 pdn->eeh_check_count ++;
592 if (pdn->eeh_check_count >= EEH_MAX_FAILS) {
593 printk (KERN_ERR "EEH: Device driver ignored %d bad reads, panicing\n",
594 pdn->eeh_check_count);
595 dump_stack();
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 /* re-read the slot reset state */
Linas Vepstas69376502005-11-03 18:47:50 -0600598 if (read_slot_reset_state(pdn, rets) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 rets[0] = -1; /* reset state unknown */
Linas Vepstas5c1344e2005-11-03 18:49:31 -0600600
601 /* If we are here, then we hit an infinite loop. Stop. */
602 panic("EEH: MMIO halt (%d) on device:%s\n", rets[0], pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600604 goto dn_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
606
607 /*
608 * Now test for an EEH failure. This is VERY expensive.
609 * Note that the eeh_config_addr may be a parent device
610 * in the case of a device behind a bridge, or it may be
611 * function zero of a multi-function device.
612 * In any case they must share a common PHB.
613 */
Linas Vepstas69376502005-11-03 18:47:50 -0600614 ret = read_slot_reset_state(pdn, rets);
Linas Vepstas76e6faf2005-11-03 18:49:15 -0600615
616 /* If the call to firmware failed, punt */
617 if (ret != 0) {
618 printk(KERN_WARNING "EEH: read_slot_reset_state() failed; rc=%d dn=%s\n",
619 ret, dn->full_name);
620 __get_cpu_var(false_positives)++;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600621 rc = 0;
622 goto dn_unlock;
Linas Vepstas76e6faf2005-11-03 18:49:15 -0600623 }
624
625 /* If EEH is not supported on this device, punt. */
626 if (rets[1] != 1) {
627 printk(KERN_WARNING "EEH: event on unsupported device, rc=%d dn=%s\n",
628 ret, dn->full_name);
629 __get_cpu_var(false_positives)++;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600630 rc = 0;
631 goto dn_unlock;
Linas Vepstas76e6faf2005-11-03 18:49:15 -0600632 }
633
634 /* If not the kind of error we know about, punt. */
635 if (rets[0] != 2 && rets[0] != 4 && rets[0] != 5) {
636 __get_cpu_var(false_positives)++;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600637 rc = 0;
638 goto dn_unlock;
Linas Vepstas76e6faf2005-11-03 18:49:15 -0600639 }
640
641 /* Note that config-io to empty slots may fail;
642 * we recognize empty because they don't have children. */
643 if ((rets[0] == 5) && (dn->child == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 __get_cpu_var(false_positives)++;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600645 rc = 0;
646 goto dn_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600649 __get_cpu_var(slot_resets)++;
650
651 /* Avoid repeated reports of this failure, including problems
652 * with other functions on this device, and functions under
653 * bridges. */
Linas Vepstasd9564ad2005-11-03 18:50:48 -0600654 eeh_mark_slot (dn, EEH_MODE_ISOLATED);
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600655 spin_unlock_irqrestore(&confirm_error_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Linas Vepstas77bd7412005-11-03 18:52:49 -0600657 state = pci_channel_io_normal;
658 if ((rets[0] == 2) || (rets[0] == 4))
659 state = pci_channel_io_frozen;
660 if (rets[0] == 5)
661 state = pci_channel_io_perm_failure;
662 eeh_send_failure_event (dn, dev, state, rets[2]);
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 /* Most EEH events are due to device driver bugs. Having
665 * a stack trace will help the device-driver authors figure
666 * out what happened. So print that out. */
Linas Vepstas76e6faf2005-11-03 18:49:15 -0600667 if (rets[0] != 5) dump_stack();
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600668 return 1;
669
670dn_unlock:
671 spin_unlock_irqrestore(&confirm_error_lock, flags);
672 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600675EXPORT_SYMBOL_GPL(eeh_dn_check_failure);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677/**
678 * eeh_check_failure - check if all 1's data is due to EEH slot freeze
679 * @token i/o token, should be address in the form 0xA....
680 * @val value, should be all 1's (XXX why do we need this arg??)
681 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 * Check for an EEH failure at the given token address. Call this
683 * routine if the result of a read was all 0xff's and you want to
684 * find out if this is due to an EEH slot freeze event. This routine
685 * will query firmware for the EEH status.
686 *
687 * Note this routine is safe to call in an interrupt context.
688 */
689unsigned long eeh_check_failure(const volatile void __iomem *token, unsigned long val)
690{
691 unsigned long addr;
692 struct pci_dev *dev;
693 struct device_node *dn;
694
695 /* Finding the phys addr + pci device; this is pretty quick. */
696 addr = eeh_token_to_phys((unsigned long __force) token);
697 dev = pci_get_device_by_addr(addr);
Linas Vepstas177bc932005-11-03 18:48:52 -0600698 if (!dev) {
699 __get_cpu_var(no_device)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return val;
Linas Vepstas177bc932005-11-03 18:48:52 -0600701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 dn = pci_device_to_OF_node(dev);
704 eeh_dn_check_failure (dn, dev);
705
706 pci_dev_put(dev);
707 return val;
708}
709
710EXPORT_SYMBOL(eeh_check_failure);
711
Linas Vepstas172ca922005-11-03 18:50:04 -0600712/* ------------------------------------------------------------- */
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600713/* The code below deals with error recovery */
714
715/** Return negative value if a permanent error, else return
716 * a number of milliseconds to wait until the PCI slot is
717 * ready to be used.
718 */
719static int
720eeh_slot_availability(struct pci_dn *pdn)
721{
722 int rc;
723 int rets[3];
724
725 rc = read_slot_reset_state(pdn, rets);
726
727 if (rc) return rc;
728
729 if (rets[1] == 0) return -1; /* EEH is not supported */
730 if (rets[0] == 0) return 0; /* Oll Korrect */
731 if (rets[0] == 5) {
732 if (rets[2] == 0) return -1; /* permanently unavailable */
733 return rets[2]; /* number of millisecs to wait */
734 }
735 return -1;
736}
737
738/** rtas_pci_slot_reset raises/lowers the pci #RST line
739 * state: 1/0 to raise/lower the #RST
740 *
741 * Clear the EEH-frozen condition on a slot. This routine
742 * asserts the PCI #RST line if the 'state' argument is '1',
743 * and drops the #RST line if 'state is '0'. This routine is
744 * safe to call in an interrupt context.
745 *
746 */
747
748static void
749rtas_pci_slot_reset(struct pci_dn *pdn, int state)
750{
751 int rc;
752
753 BUG_ON (pdn==NULL);
754
755 if (!pdn->phb) {
756 printk (KERN_WARNING "EEH: in slot reset, device node %s has no phb\n",
757 pdn->node->full_name);
758 return;
759 }
760
761 rc = rtas_call(ibm_set_slot_reset,4,1, NULL,
762 pdn->eeh_config_addr,
763 BUID_HI(pdn->phb->buid),
764 BUID_LO(pdn->phb->buid),
765 state);
766 if (rc) {
767 printk (KERN_WARNING "EEH: Unable to reset the failed slot, (%d) #RST=%d dn=%s\n",
768 rc, state, pdn->node->full_name);
769 return;
770 }
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600771}
772
773/** rtas_set_slot_reset -- assert the pci #RST line for 1/4 second
774 * dn -- device node to be reset.
775 */
776
777void
778rtas_set_slot_reset(struct pci_dn *pdn)
779{
780 int i, rc;
781
782 rtas_pci_slot_reset (pdn, 1);
783
784 /* The PCI bus requires that the reset be held high for at least
785 * a 100 milliseconds. We wait a bit longer 'just in case'. */
786
787#define PCI_BUS_RST_HOLD_TIME_MSEC 250
788 msleep (PCI_BUS_RST_HOLD_TIME_MSEC);
Linas Vepstasd9564ad2005-11-03 18:50:48 -0600789
790 /* We might get hit with another EEH freeze as soon as the
791 * pci slot reset line is dropped. Make sure we don't miss
792 * these, and clear the flag now. */
793 eeh_clear_slot (pdn->node, EEH_MODE_ISOLATED);
794
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600795 rtas_pci_slot_reset (pdn, 0);
796
797 /* After a PCI slot has been reset, the PCI Express spec requires
798 * a 1.5 second idle time for the bus to stabilize, before starting
799 * up traffic. */
800#define PCI_BUS_SETTLE_TIME_MSEC 1800
801 msleep (PCI_BUS_SETTLE_TIME_MSEC);
802
803 /* Now double check with the firmware to make sure the device is
804 * ready to be used; if not, wait for recovery. */
805 for (i=0; i<10; i++) {
806 rc = eeh_slot_availability (pdn);
807 if (rc <= 0) break;
808
809 msleep (rc+100);
810 }
811}
812
Linas Vepstas8b553f32005-11-03 18:50:17 -0600813/* ------------------------------------------------------- */
814/** Save and restore of PCI BARs
815 *
816 * Although firmware will set up BARs during boot, it doesn't
817 * set up device BAR's after a device reset, although it will,
818 * if requested, set up bridge configuration. Thus, we need to
819 * configure the PCI devices ourselves.
820 */
821
822/**
823 * __restore_bars - Restore the Base Address Registers
824 * Loads the PCI configuration space base address registers,
825 * the expansion ROM base address, the latency timer, and etc.
826 * from the saved values in the device node.
827 */
828static inline void __restore_bars (struct pci_dn *pdn)
829{
830 int i;
831
832 if (NULL==pdn->phb) return;
833 for (i=4; i<10; i++) {
834 rtas_write_config(pdn, i*4, 4, pdn->config_space[i]);
835 }
836
837 /* 12 == Expansion ROM Address */
838 rtas_write_config(pdn, 12*4, 4, pdn->config_space[12]);
839
840#define BYTE_SWAP(OFF) (8*((OFF)/4)+3-(OFF))
841#define SAVED_BYTE(OFF) (((u8 *)(pdn->config_space))[BYTE_SWAP(OFF)])
842
843 rtas_write_config (pdn, PCI_CACHE_LINE_SIZE, 1,
844 SAVED_BYTE(PCI_CACHE_LINE_SIZE));
845
846 rtas_write_config (pdn, PCI_LATENCY_TIMER, 1,
847 SAVED_BYTE(PCI_LATENCY_TIMER));
848
849 /* max latency, min grant, interrupt pin and line */
850 rtas_write_config(pdn, 15*4, 4, pdn->config_space[15]);
851}
852
853/**
854 * eeh_restore_bars - restore the PCI config space info
855 *
856 * This routine performs a recursive walk to the children
857 * of this device as well.
858 */
859void eeh_restore_bars(struct pci_dn *pdn)
860{
861 struct device_node *dn;
862 if (!pdn)
863 return;
864
865 if (! pdn->eeh_is_bridge)
866 __restore_bars (pdn);
867
868 dn = pdn->node->child;
869 while (dn) {
870 eeh_restore_bars (PCI_DN(dn));
871 dn = dn->sibling;
872 }
873}
874
875/**
876 * eeh_save_bars - save device bars
877 *
878 * Save the values of the device bars. Unlike the restore
879 * routine, this routine is *not* recursive. This is because
880 * PCI devices are added individuallly; but, for the restore,
881 * an entire slot is reset at a time.
882 */
883static void eeh_save_bars(struct pci_dev * pdev, struct pci_dn *pdn)
884{
885 int i;
886
887 if (!pdev || !pdn )
888 return;
889
890 for (i = 0; i < 16; i++)
891 pci_read_config_dword(pdev, i * 4, &pdn->config_space[i]);
892
893 if (pdev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
894 pdn->eeh_is_bridge = 1;
895}
896
897void
898rtas_configure_bridge(struct pci_dn *pdn)
899{
900 int token = rtas_token ("ibm,configure-bridge");
901 int rc;
902
903 if (token == RTAS_UNKNOWN_SERVICE)
904 return;
905 rc = rtas_call(token,3,1, NULL,
906 pdn->eeh_config_addr,
907 BUID_HI(pdn->phb->buid),
908 BUID_LO(pdn->phb->buid));
909 if (rc) {
910 printk (KERN_WARNING "EEH: Unable to configure device bridge (%d) for %s\n",
911 rc, pdn->node->full_name);
912 }
913}
914
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600915/* ------------------------------------------------------------- */
Linas Vepstas172ca922005-11-03 18:50:04 -0600916/* The code below deals with enabling EEH for devices during the
917 * early boot sequence. EEH must be enabled before any PCI probing
918 * can be done.
919 */
920
921#define EEH_ENABLE 1
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923struct eeh_early_enable_info {
924 unsigned int buid_hi;
925 unsigned int buid_lo;
926};
927
928/* Enable eeh for the given device node. */
929static void *early_enable_eeh(struct device_node *dn, void *data)
930{
931 struct eeh_early_enable_info *info = data;
932 int ret;
933 char *status = get_property(dn, "status", NULL);
934 u32 *class_code = (u32 *)get_property(dn, "class-code", NULL);
935 u32 *vendor_id = (u32 *)get_property(dn, "vendor-id", NULL);
936 u32 *device_id = (u32 *)get_property(dn, "device-id", NULL);
937 u32 *regs;
938 int enable;
Linas Vepstas69376502005-11-03 18:47:50 -0600939 struct pci_dn *pdn = PCI_DN(dn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Paul Mackerras16353172005-09-06 13:17:54 +1000941 pdn->eeh_mode = 0;
Linas Vepstas5c1344e2005-11-03 18:49:31 -0600942 pdn->eeh_check_count = 0;
943 pdn->eeh_freeze_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 if (status && strcmp(status, "ok") != 0)
946 return NULL; /* ignore devices with bad status */
947
948 /* Ignore bad nodes. */
949 if (!class_code || !vendor_id || !device_id)
950 return NULL;
951
952 /* There is nothing to check on PCI to ISA bridges */
953 if (dn->type && !strcmp(dn->type, "isa")) {
Paul Mackerras16353172005-09-06 13:17:54 +1000954 pdn->eeh_mode |= EEH_MODE_NOCHECK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return NULL;
956 }
957
958 /*
959 * Now decide if we are going to "Disable" EEH checking
960 * for this device. We still run with the EEH hardware active,
961 * but we won't be checking for ff's. This means a driver
962 * could return bad data (very bad!), an interrupt handler could
963 * hang waiting on status bits that won't change, etc.
964 * But there are a few cases like display devices that make sense.
965 */
966 enable = 1; /* i.e. we will do checking */
Linas Vepstas77bd7412005-11-03 18:52:49 -0600967#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if ((*class_code >> 16) == PCI_BASE_CLASS_DISPLAY)
969 enable = 0;
Linas Vepstas77bd7412005-11-03 18:52:49 -0600970#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 if (!enable)
Paul Mackerras16353172005-09-06 13:17:54 +1000973 pdn->eeh_mode |= EEH_MODE_NOCHECK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 /* Ok... see if this device supports EEH. Some do, some don't,
976 * and the only way to find out is to check each and every one. */
977 regs = (u32 *)get_property(dn, "reg", NULL);
978 if (regs) {
979 /* First register entry is addr (00BBSS00) */
980 /* Try to enable eeh */
981 ret = rtas_call(ibm_set_eeh_option, 4, 1, NULL,
Linas Vepstas172ca922005-11-03 18:50:04 -0600982 regs[0], info->buid_hi, info->buid_lo,
983 EEH_ENABLE);
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (ret == 0) {
986 eeh_subsystem_enabled = 1;
Paul Mackerras16353172005-09-06 13:17:54 +1000987 pdn->eeh_mode |= EEH_MODE_SUPPORTED;
988 pdn->eeh_config_addr = regs[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989#ifdef DEBUG
990 printk(KERN_DEBUG "EEH: %s: eeh enabled\n", dn->full_name);
991#endif
992 } else {
993
994 /* This device doesn't support EEH, but it may have an
995 * EEH parent, in which case we mark it as supported. */
Linas Vepstas69376502005-11-03 18:47:50 -0600996 if (dn->parent && PCI_DN(dn->parent)
Paul Mackerras16353172005-09-06 13:17:54 +1000997 && (PCI_DN(dn->parent)->eeh_mode & EEH_MODE_SUPPORTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 /* Parent supports EEH. */
Paul Mackerras16353172005-09-06 13:17:54 +1000999 pdn->eeh_mode |= EEH_MODE_SUPPORTED;
1000 pdn->eeh_config_addr = PCI_DN(dn->parent)->eeh_config_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 return NULL;
1002 }
1003 }
1004 } else {
1005 printk(KERN_WARNING "EEH: %s: unable to get reg property.\n",
1006 dn->full_name);
1007 }
1008
Linas Vepstas69376502005-11-03 18:47:50 -06001009 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010}
1011
1012/*
1013 * Initialize EEH by trying to enable it for all of the adapters in the system.
1014 * As a side effect we can determine here if eeh is supported at all.
1015 * Note that we leave EEH on so failed config cycles won't cause a machine
1016 * check. If a user turns off EEH for a particular adapter they are really
1017 * telling Linux to ignore errors. Some hardware (e.g. POWER5) won't
1018 * grant access to a slot if EEH isn't enabled, and so we always enable
1019 * EEH for all slots/all devices.
1020 *
1021 * The eeh-force-off option disables EEH checking globally, for all slots.
1022 * Even if force-off is set, the EEH hardware is still enabled, so that
1023 * newer systems can boot.
1024 */
1025void __init eeh_init(void)
1026{
1027 struct device_node *phb, *np;
1028 struct eeh_early_enable_info info;
1029
Linas Vepstasfd761fd2005-11-03 18:49:23 -06001030 spin_lock_init(&confirm_error_lock);
Linas Vepstasdf7242b2005-11-03 18:49:01 -06001031 spin_lock_init(&slot_errbuf_lock);
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 np = of_find_node_by_path("/rtas");
1034 if (np == NULL)
1035 return;
1036
1037 ibm_set_eeh_option = rtas_token("ibm,set-eeh-option");
1038 ibm_set_slot_reset = rtas_token("ibm,set-slot-reset");
1039 ibm_read_slot_reset_state2 = rtas_token("ibm,read-slot-reset-state2");
1040 ibm_read_slot_reset_state = rtas_token("ibm,read-slot-reset-state");
1041 ibm_slot_error_detail = rtas_token("ibm,slot-error-detail");
1042
1043 if (ibm_set_eeh_option == RTAS_UNKNOWN_SERVICE)
1044 return;
1045
1046 eeh_error_buf_size = rtas_token("rtas-error-log-max");
1047 if (eeh_error_buf_size == RTAS_UNKNOWN_SERVICE) {
1048 eeh_error_buf_size = 1024;
1049 }
1050 if (eeh_error_buf_size > RTAS_ERROR_LOG_MAX) {
1051 printk(KERN_WARNING "EEH: rtas-error-log-max is bigger than allocated "
1052 "buffer ! (%d vs %d)", eeh_error_buf_size, RTAS_ERROR_LOG_MAX);
1053 eeh_error_buf_size = RTAS_ERROR_LOG_MAX;
1054 }
1055
1056 /* Enable EEH for all adapters. Note that eeh requires buid's */
1057 for (phb = of_find_node_by_name(NULL, "pci"); phb;
1058 phb = of_find_node_by_name(phb, "pci")) {
1059 unsigned long buid;
1060
1061 buid = get_phb_buid(phb);
Linas Vepstas69376502005-11-03 18:47:50 -06001062 if (buid == 0 || PCI_DN(phb) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 continue;
1064
1065 info.buid_lo = BUID_LO(buid);
1066 info.buid_hi = BUID_HI(buid);
1067 traverse_pci_devices(phb, early_enable_eeh, &info);
1068 }
1069
1070 if (eeh_subsystem_enabled)
1071 printk(KERN_INFO "EEH: PCI Enhanced I/O Error Handling Enabled\n");
1072 else
1073 printk(KERN_WARNING "EEH: No capable adapters found\n");
1074}
1075
1076/**
1077 * eeh_add_device_early - enable EEH for the indicated device_node
1078 * @dn: device node for which to set up EEH
1079 *
1080 * This routine must be used to perform EEH initialization for PCI
1081 * devices that were added after system boot (e.g. hotplug, dlpar).
1082 * This routine must be called before any i/o is performed to the
1083 * adapter (inluding any config-space i/o).
1084 * Whether this actually enables EEH or not for this device depends
1085 * on the CEC architecture, type of the device, on earlier boot
1086 * command-line arguments & etc.
1087 */
1088void eeh_add_device_early(struct device_node *dn)
1089{
1090 struct pci_controller *phb;
1091 struct eeh_early_enable_info info;
1092
Linas Vepstas69376502005-11-03 18:47:50 -06001093 if (!dn || !PCI_DN(dn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return;
Paul Mackerras16353172005-09-06 13:17:54 +10001095 phb = PCI_DN(dn)->phb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (NULL == phb || 0 == phb->buid) {
Linas Vepstas69376502005-11-03 18:47:50 -06001097 printk(KERN_WARNING "EEH: Expected buid but found none for %s\n",
1098 dn->full_name);
1099 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 return;
1101 }
1102
1103 info.buid_hi = BUID_HI(phb->buid);
1104 info.buid_lo = BUID_LO(phb->buid);
1105 early_enable_eeh(dn, &info);
1106}
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001107EXPORT_SYMBOL_GPL(eeh_add_device_early);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Linas Vepstase2a296e2005-11-03 18:51:31 -06001109void eeh_add_device_tree_early(struct device_node *dn)
1110{
1111 struct device_node *sib;
1112 for (sib = dn->child; sib; sib = sib->sibling)
1113 eeh_add_device_tree_early(sib);
1114 eeh_add_device_early(dn);
1115}
1116EXPORT_SYMBOL_GPL(eeh_add_device_tree_early);
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118/**
1119 * eeh_add_device_late - perform EEH initialization for the indicated pci device
1120 * @dev: pci device for which to set up EEH
1121 *
1122 * This routine must be used to complete EEH initialization for PCI
1123 * devices that were added after system boot (e.g. hotplug, dlpar).
1124 */
1125void eeh_add_device_late(struct pci_dev *dev)
1126{
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001127 struct device_node *dn;
Linas Vepstas8b553f32005-11-03 18:50:17 -06001128 struct pci_dn *pdn;
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (!dev || !eeh_subsystem_enabled)
1131 return;
1132
1133#ifdef DEBUG
Adrian Bunk982245f2005-07-17 04:22:20 +02001134 printk(KERN_DEBUG "EEH: adding device %s\n", pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135#endif
1136
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001137 pci_dev_get (dev);
1138 dn = pci_device_to_OF_node(dev);
Linas Vepstas8b553f32005-11-03 18:50:17 -06001139 pdn = PCI_DN(dn);
1140 pdn->pcidev = dev;
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 pci_addr_cache_insert_device (dev);
Linas Vepstas8b553f32005-11-03 18:50:17 -06001143 eeh_save_bars(dev, pdn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001145EXPORT_SYMBOL_GPL(eeh_add_device_late);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147/**
1148 * eeh_remove_device - undo EEH setup for the indicated pci device
1149 * @dev: pci device to be removed
1150 *
1151 * This routine should be when a device is removed from a running
1152 * system (e.g. by hotplug or dlpar).
1153 */
1154void eeh_remove_device(struct pci_dev *dev)
1155{
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001156 struct device_node *dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (!dev || !eeh_subsystem_enabled)
1158 return;
1159
1160 /* Unregister the device with the EEH/PCI address search system */
1161#ifdef DEBUG
Adrian Bunk982245f2005-07-17 04:22:20 +02001162 printk(KERN_DEBUG "EEH: remove device %s\n", pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163#endif
1164 pci_addr_cache_remove_device(dev);
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001165
1166 dn = pci_device_to_OF_node(dev);
1167 PCI_DN(dn)->pcidev = NULL;
1168 pci_dev_put (dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001170EXPORT_SYMBOL_GPL(eeh_remove_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Linas Vepstase2a296e2005-11-03 18:51:31 -06001172void eeh_remove_bus_device(struct pci_dev *dev)
1173{
1174 eeh_remove_device(dev);
1175 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
1176 struct pci_bus *bus = dev->subordinate;
1177 struct list_head *ln;
1178 if (!bus)
1179 return;
1180 for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
1181 struct pci_dev *pdev = pci_dev_b(ln);
1182 if (pdev)
1183 eeh_remove_bus_device(pdev);
1184 }
1185 }
1186}
1187EXPORT_SYMBOL_GPL(eeh_remove_bus_device);
1188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189static int proc_eeh_show(struct seq_file *m, void *v)
1190{
1191 unsigned int cpu;
1192 unsigned long ffs = 0, positives = 0, failures = 0;
1193 unsigned long resets = 0;
Linas Vepstas177bc932005-11-03 18:48:52 -06001194 unsigned long no_dev = 0, no_dn = 0, no_cfg = 0, no_check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 for_each_cpu(cpu) {
1197 ffs += per_cpu(total_mmio_ffs, cpu);
1198 positives += per_cpu(false_positives, cpu);
1199 failures += per_cpu(ignored_failures, cpu);
1200 resets += per_cpu(slot_resets, cpu);
Linas Vepstas177bc932005-11-03 18:48:52 -06001201 no_dev += per_cpu(no_device, cpu);
1202 no_dn += per_cpu(no_dn, cpu);
1203 no_cfg += per_cpu(no_cfg_addr, cpu);
1204 no_check += per_cpu(ignored_check, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
1206
1207 if (0 == eeh_subsystem_enabled) {
1208 seq_printf(m, "EEH Subsystem is globally disabled\n");
1209 seq_printf(m, "eeh_total_mmio_ffs=%ld\n", ffs);
1210 } else {
1211 seq_printf(m, "EEH Subsystem is enabled\n");
Linas Vepstas177bc932005-11-03 18:48:52 -06001212 seq_printf(m,
1213 "no device=%ld\n"
1214 "no device node=%ld\n"
1215 "no config address=%ld\n"
1216 "check not wanted=%ld\n"
1217 "eeh_total_mmio_ffs=%ld\n"
1218 "eeh_false_positives=%ld\n"
1219 "eeh_ignored_failures=%ld\n"
1220 "eeh_slot_resets=%ld\n",
1221 no_dev, no_dn, no_cfg, no_check,
1222 ffs, positives, failures, resets);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
1224
1225 return 0;
1226}
1227
1228static int proc_eeh_open(struct inode *inode, struct file *file)
1229{
1230 return single_open(file, proc_eeh_show, NULL);
1231}
1232
1233static struct file_operations proc_eeh_operations = {
1234 .open = proc_eeh_open,
1235 .read = seq_read,
1236 .llseek = seq_lseek,
1237 .release = single_release,
1238};
1239
1240static int __init eeh_init_proc(void)
1241{
1242 struct proc_dir_entry *e;
1243
Paul Mackerras799d6042005-11-10 13:37:51 +11001244 if (platform_is_pseries()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 e = create_proc_entry("ppc64/eeh", 0, NULL);
1246 if (e)
1247 e->proc_fops = &proc_eeh_operations;
1248 }
1249
1250 return 0;
1251}
1252__initcall(eeh_init_proc);