]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - arch/ppc64/kernel/eeh.c
[PATCH] ppc64: PCI address cache minor fixes
[mirror_ubuntu-bionic-kernel.git] / arch / ppc64 / kernel / eeh.c
1 /*
2 * eeh.c
3 * Copyright (C) 2001 Dave Engebretsen & Todd Inglett IBM Corporation
4 *
5 * 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.
9 *
10 * 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.
14 *
15 * 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
20 #include <linux/init.h>
21 #include <linux/list.h>
22 #include <linux/notifier.h>
23 #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>
28 #include <asm/atomic.h>
29 #include <asm/eeh.h>
30 #include <asm/io.h>
31 #include <asm/machdep.h>
32 #include <asm/rtas.h>
33 #include <asm/atomic.h>
34 #include <asm/systemcfg.h>
35 #include <asm/ppc-pci.h>
36
37 #undef DEBUG
38
39 /** Overview:
40 * EEH, or "Extended Error Handling" is a PCI bridge technology for
41 * dealing with PCI bus errors that can't be dealt with within the
42 * usual PCI framework, except by check-stopping the CPU. Systems
43 * that are designed for high-availability/reliability cannot afford
44 * to crash due to a "mere" PCI error, thus the need for EEH.
45 * An EEH-capable bridge operates by converting a detected error
46 * into a "slot freeze", taking the PCI adapter off-line, making
47 * the slot behave, from the OS'es point of view, as if the slot
48 * were "empty": all reads return 0xff's and all writes are silently
49 * ignored. EEH slot isolation events can be triggered by parity
50 * errors on the address or data busses (e.g. during posted writes),
51 * which in turn might be caused by low voltage on the bus, dust,
52 * vibration, humidity, radioactivity or plain-old failed hardware.
53 *
54 * Note, however, that one of the leading causes of EEH slot
55 * freeze events are buggy device drivers, buggy device microcode,
56 * or buggy device hardware. This is because any attempt by the
57 * device to bus-master data to a memory address that is not
58 * assigned to the device will trigger a slot freeze. (The idea
59 * is to prevent devices-gone-wild from corrupting system memory).
60 * Buggy hardware/drivers will have a miserable time co-existing
61 * with EEH.
62 *
63 * Ideally, a PCI device driver, when suspecting that an isolation
64 * event has occured (e.g. by reading 0xff's), will then ask EEH
65 * whether this is the case, and then take appropriate steps to
66 * reset the PCI slot, the PCI device, and then resume operations.
67 * However, until that day, the checking is done here, with the
68 * eeh_check_failure() routine embedded in the MMIO macros. If
69 * the slot is found to be isolated, an "EEH Event" is synthesized
70 * and sent out for processing.
71 */
72
73 /* EEH event workqueue setup. */
74 static DEFINE_SPINLOCK(eeh_eventlist_lock);
75 LIST_HEAD(eeh_eventlist);
76 static void eeh_event_handler(void *);
77 DECLARE_WORK(eeh_event_wq, eeh_event_handler, NULL);
78
79 static struct notifier_block *eeh_notifier_chain;
80
81 /*
82 * If a device driver keeps reading an MMIO register in an interrupt
83 * handler after a slot isolation event has occurred, we assume it
84 * is broken and panic. This sets the threshold for how many read
85 * attempts we allow before panicking.
86 */
87 #define EEH_MAX_FAILS 1000
88 static atomic_t eeh_fail_count;
89
90 /* RTAS tokens */
91 static int ibm_set_eeh_option;
92 static int ibm_set_slot_reset;
93 static int ibm_read_slot_reset_state;
94 static int ibm_read_slot_reset_state2;
95 static int ibm_slot_error_detail;
96
97 static int eeh_subsystem_enabled;
98
99 /* Buffer for reporting slot-error-detail rtas calls */
100 static unsigned char slot_errbuf[RTAS_ERROR_LOG_MAX];
101 static DEFINE_SPINLOCK(slot_errbuf_lock);
102 static int eeh_error_buf_size;
103
104 /* System monitoring statistics */
105 static DEFINE_PER_CPU(unsigned long, total_mmio_ffs);
106 static DEFINE_PER_CPU(unsigned long, false_positives);
107 static DEFINE_PER_CPU(unsigned long, ignored_failures);
108 static 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 */
127 struct 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
136 static struct pci_io_addr_cache
137 {
138 struct rb_root rb_root;
139 spinlock_t piar_lock;
140 } pci_io_addr_cache_root;
141
142 static 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 */
175 static 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 */
191 static 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);
200 printk(KERN_DEBUG "PCI: %s addr range %d [%lx-%lx]: %s\n",
201 (piar->flags & IORESOURCE_IO) ? "i/o" : "mem", cnt,
202 piar->addr_lo, piar->addr_hi, pci_name(piar->pcidev));
203 cnt++;
204 n = rb_next(n);
205 }
206 }
207 #endif
208
209 /* Insert address range into the rb tree. */
210 static struct pci_io_addr_range *
211 pci_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);
222 if (ahi < piar->addr_lo) {
223 p = &parent->rb_left;
224 } else if (alo > piar->addr_hi) {
225 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
243 #ifdef DEBUG
244 printk(KERN_DEBUG "PIAR: insert range=[%lx:%lx] dev=%s\n",
245 alo, ahi, pci_name (dev));
246 #endif
247
248 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
254 static void __pci_addr_cache_insert_device(struct pci_dev *dev)
255 {
256 struct device_node *dn;
257 struct pci_dn *pdn;
258 int i;
259 int inserted = 0;
260
261 dn = pci_device_to_OF_node(dev);
262 if (!dn) {
263 printk(KERN_WARNING "PCI: no pci dn found for dev=%s\n", pci_name(dev));
264 return;
265 }
266
267 /* Skip any devices for which EEH is not enabled. */
268 pdn = PCI_DN(dn);
269 if (!(pdn->eeh_mode & EEH_MODE_SUPPORTED) ||
270 pdn->eeh_mode & EEH_MODE_NOCHECK) {
271 #ifdef DEBUG
272 printk(KERN_INFO "PCI: skip building address cache for=%s - %s\n",
273 pci_name(dev), pdn->node->full_name);
274 #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 */
309 static void pci_addr_cache_insert_device(struct pci_dev *dev)
310 {
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
318 static inline void __pci_addr_cache_remove_device(struct pci_dev *dev)
319 {
320 struct rb_node *n;
321 int removed = 0;
322
323 restart:
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 */
352 static void pci_addr_cache_remove_device(struct pci_dev *dev)
353 {
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 */
370 void __init pci_addr_cache_build(void)
371 {
372 struct pci_dev *dev = NULL;
373
374 if (!eeh_subsystem_enabled)
375 return;
376
377 spin_lock_init(&pci_io_addr_cache_root.piar_lock);
378
379 while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
380 /* Ignore PCI bridges ( XXX why ??) */
381 if ((dev->class >> 16) == PCI_BASE_CLASS_BRIDGE) {
382 continue;
383 }
384 pci_addr_cache_insert_device(dev);
385 }
386
387 #ifdef DEBUG
388 /* Verify tree built up above, echo back the list of addrs. */
389 pci_addr_cache_print(&pci_io_addr_cache_root);
390 #endif
391 }
392
393 /* --------------------------------------------------------------- */
394 /* Above lies the PCI Address Cache. Below lies the EEH event infrastructure */
395
396 /**
397 * eeh_register_notifier - Register to find out about EEH events.
398 * @nb: notifier block to callback on events
399 */
400 int eeh_register_notifier(struct notifier_block *nb)
401 {
402 return notifier_chain_register(&eeh_notifier_chain, nb);
403 }
404
405 /**
406 * eeh_unregister_notifier - Unregister to an EEH event notifier.
407 * @nb: notifier block to callback on events
408 */
409 int eeh_unregister_notifier(struct notifier_block *nb)
410 {
411 return notifier_chain_unregister(&eeh_notifier_chain, nb);
412 }
413
414 /**
415 * read_slot_reset_state - Read the reset state of a device node's slot
416 * @dn: device node to read
417 * @rets: array to return results in
418 */
419 static int read_slot_reset_state(struct pci_dn *pdn, int rets[])
420 {
421 int token, outputs;
422
423 if (ibm_read_slot_reset_state2 != RTAS_UNKNOWN_SERVICE) {
424 token = ibm_read_slot_reset_state2;
425 outputs = 4;
426 } else {
427 token = ibm_read_slot_reset_state;
428 rets[2] = 0; /* fake PE Unavailable info */
429 outputs = 3;
430 }
431
432 return rtas_call(token, 3, outputs, rets, pdn->eeh_config_addr,
433 BUID_HI(pdn->phb->buid), BUID_LO(pdn->phb->buid));
434 }
435
436 /**
437 * eeh_panic - call panic() for an eeh event that cannot be handled.
438 * The philosophy of this routine is that it is better to panic and
439 * halt the OS than it is to risk possible data corruption by
440 * oblivious device drivers that don't know better.
441 *
442 * @dev pci device that had an eeh event
443 * @reset_state current reset state of the device slot
444 */
445 static void eeh_panic(struct pci_dev *dev, int reset_state)
446 {
447 /*
448 * XXX We should create a separate sysctl for this.
449 *
450 * Since the panic_on_oops sysctl is used to halt the system
451 * in light of potential corruption, we can use it here.
452 */
453 if (panic_on_oops)
454 panic("EEH: MMIO failure (%d) on device:%s\n", reset_state,
455 pci_name(dev));
456 else {
457 __get_cpu_var(ignored_failures)++;
458 printk(KERN_INFO "EEH: Ignored MMIO failure (%d) on device:%s\n",
459 reset_state, pci_name(dev));
460 }
461 }
462
463 /**
464 * eeh_event_handler - dispatch EEH events. The detection of a frozen
465 * slot can occur inside an interrupt, where it can be hard to do
466 * anything about it. The goal of this routine is to pull these
467 * detection events out of the context of the interrupt handler, and
468 * re-dispatch them for processing at a later time in a normal context.
469 *
470 * @dummy - unused
471 */
472 static void eeh_event_handler(void *dummy)
473 {
474 unsigned long flags;
475 struct eeh_event *event;
476
477 while (1) {
478 spin_lock_irqsave(&eeh_eventlist_lock, flags);
479 event = NULL;
480 if (!list_empty(&eeh_eventlist)) {
481 event = list_entry(eeh_eventlist.next, struct eeh_event, list);
482 list_del(&event->list);
483 }
484 spin_unlock_irqrestore(&eeh_eventlist_lock, flags);
485 if (event == NULL)
486 break;
487
488 printk(KERN_INFO "EEH: MMIO failure (%d), notifiying device "
489 "%s\n", event->reset_state,
490 pci_name(event->dev));
491
492 atomic_set(&eeh_fail_count, 0);
493 notifier_call_chain (&eeh_notifier_chain,
494 EEH_NOTIFY_FREEZE, event);
495
496 __get_cpu_var(slot_resets)++;
497
498 pci_dev_put(event->dev);
499 kfree(event);
500 }
501 }
502
503 /**
504 * eeh_token_to_phys - convert EEH address token to phys address
505 * @token i/o token, should be address in the form 0xA....
506 */
507 static inline unsigned long eeh_token_to_phys(unsigned long token)
508 {
509 pte_t *ptep;
510 unsigned long pa;
511
512 ptep = find_linux_pte(init_mm.pgd, token);
513 if (!ptep)
514 return token;
515 pa = pte_pfn(*ptep) << PAGE_SHIFT;
516
517 return pa | (token & (PAGE_SIZE-1));
518 }
519
520 /**
521 * eeh_dn_check_failure - check if all 1's data is due to EEH slot freeze
522 * @dn device node
523 * @dev pci device, if known
524 *
525 * Check for an EEH failure for the given device node. Call this
526 * routine if the result of a read was all 0xff's and you want to
527 * find out if this is due to an EEH slot freeze. This routine
528 * will query firmware for the EEH status.
529 *
530 * Returns 0 if there has not been an EEH error; otherwise returns
531 * a non-zero value and queues up a slot isolation event notification.
532 *
533 * It is safe to call this routine in an interrupt context.
534 */
535 int eeh_dn_check_failure(struct device_node *dn, struct pci_dev *dev)
536 {
537 int ret;
538 int rets[3];
539 unsigned long flags;
540 int rc, reset_state;
541 struct eeh_event *event;
542 struct pci_dn *pdn;
543
544 __get_cpu_var(total_mmio_ffs)++;
545
546 if (!eeh_subsystem_enabled)
547 return 0;
548
549 if (!dn)
550 return 0;
551 pdn = PCI_DN(dn);
552
553 /* Access to IO BARs might get this far and still not want checking. */
554 if (!pdn->eeh_capable || !(pdn->eeh_mode & EEH_MODE_SUPPORTED) ||
555 pdn->eeh_mode & EEH_MODE_NOCHECK) {
556 return 0;
557 }
558
559 if (!pdn->eeh_config_addr) {
560 return 0;
561 }
562
563 /*
564 * If we already have a pending isolation event for this
565 * slot, we know it's bad already, we don't need to check...
566 */
567 if (pdn->eeh_mode & EEH_MODE_ISOLATED) {
568 atomic_inc(&eeh_fail_count);
569 if (atomic_read(&eeh_fail_count) >= EEH_MAX_FAILS) {
570 /* re-read the slot reset state */
571 if (read_slot_reset_state(pdn, rets) != 0)
572 rets[0] = -1; /* reset state unknown */
573 eeh_panic(dev, rets[0]);
574 }
575 return 0;
576 }
577
578 /*
579 * Now test for an EEH failure. This is VERY expensive.
580 * Note that the eeh_config_addr may be a parent device
581 * in the case of a device behind a bridge, or it may be
582 * function zero of a multi-function device.
583 * In any case they must share a common PHB.
584 */
585 ret = read_slot_reset_state(pdn, rets);
586 if (!(ret == 0 && rets[1] == 1 && (rets[0] == 2 || rets[0] == 4))) {
587 __get_cpu_var(false_positives)++;
588 return 0;
589 }
590
591 /* prevent repeated reports of this failure */
592 pdn->eeh_mode |= EEH_MODE_ISOLATED;
593
594 reset_state = rets[0];
595
596 spin_lock_irqsave(&slot_errbuf_lock, flags);
597 memset(slot_errbuf, 0, eeh_error_buf_size);
598
599 rc = rtas_call(ibm_slot_error_detail,
600 8, 1, NULL, pdn->eeh_config_addr,
601 BUID_HI(pdn->phb->buid),
602 BUID_LO(pdn->phb->buid), NULL, 0,
603 virt_to_phys(slot_errbuf),
604 eeh_error_buf_size,
605 1 /* Temporary Error */);
606
607 if (rc == 0)
608 log_error(slot_errbuf, ERR_TYPE_RTAS_LOG, 0);
609 spin_unlock_irqrestore(&slot_errbuf_lock, flags);
610
611 printk(KERN_INFO "EEH: MMIO failure (%d) on device: %s %s\n",
612 rets[0], dn->name, dn->full_name);
613 event = kmalloc(sizeof(*event), GFP_ATOMIC);
614 if (event == NULL) {
615 eeh_panic(dev, reset_state);
616 return 1;
617 }
618
619 event->dev = dev;
620 event->dn = dn;
621 event->reset_state = reset_state;
622
623 /* We may or may not be called in an interrupt context */
624 spin_lock_irqsave(&eeh_eventlist_lock, flags);
625 list_add(&event->list, &eeh_eventlist);
626 spin_unlock_irqrestore(&eeh_eventlist_lock, flags);
627
628 /* Most EEH events are due to device driver bugs. Having
629 * a stack trace will help the device-driver authors figure
630 * out what happened. So print that out. */
631 dump_stack();
632 schedule_work(&eeh_event_wq);
633
634 return 0;
635 }
636
637 EXPORT_SYMBOL(eeh_dn_check_failure);
638
639 /**
640 * eeh_check_failure - check if all 1's data is due to EEH slot freeze
641 * @token i/o token, should be address in the form 0xA....
642 * @val value, should be all 1's (XXX why do we need this arg??)
643 *
644 * Check for an EEH failure at the given token address. Call this
645 * routine if the result of a read was all 0xff's and you want to
646 * find out if this is due to an EEH slot freeze event. This routine
647 * will query firmware for the EEH status.
648 *
649 * Note this routine is safe to call in an interrupt context.
650 */
651 unsigned long eeh_check_failure(const volatile void __iomem *token, unsigned long val)
652 {
653 unsigned long addr;
654 struct pci_dev *dev;
655 struct device_node *dn;
656
657 /* Finding the phys addr + pci device; this is pretty quick. */
658 addr = eeh_token_to_phys((unsigned long __force) token);
659 dev = pci_get_device_by_addr(addr);
660 if (!dev)
661 return val;
662
663 dn = pci_device_to_OF_node(dev);
664 eeh_dn_check_failure (dn, dev);
665
666 pci_dev_put(dev);
667 return val;
668 }
669
670 EXPORT_SYMBOL(eeh_check_failure);
671
672 struct eeh_early_enable_info {
673 unsigned int buid_hi;
674 unsigned int buid_lo;
675 };
676
677 /* Enable eeh for the given device node. */
678 static void *early_enable_eeh(struct device_node *dn, void *data)
679 {
680 struct eeh_early_enable_info *info = data;
681 int ret;
682 char *status = get_property(dn, "status", NULL);
683 u32 *class_code = (u32 *)get_property(dn, "class-code", NULL);
684 u32 *vendor_id = (u32 *)get_property(dn, "vendor-id", NULL);
685 u32 *device_id = (u32 *)get_property(dn, "device-id", NULL);
686 u32 *regs;
687 int enable;
688 struct pci_dn *pdn = PCI_DN(dn);
689
690 pdn->eeh_mode = 0;
691
692 if (status && strcmp(status, "ok") != 0)
693 return NULL; /* ignore devices with bad status */
694
695 /* Ignore bad nodes. */
696 if (!class_code || !vendor_id || !device_id)
697 return NULL;
698
699 /* There is nothing to check on PCI to ISA bridges */
700 if (dn->type && !strcmp(dn->type, "isa")) {
701 pdn->eeh_mode |= EEH_MODE_NOCHECK;
702 return NULL;
703 }
704
705 /*
706 * Now decide if we are going to "Disable" EEH checking
707 * for this device. We still run with the EEH hardware active,
708 * but we won't be checking for ff's. This means a driver
709 * could return bad data (very bad!), an interrupt handler could
710 * hang waiting on status bits that won't change, etc.
711 * But there are a few cases like display devices that make sense.
712 */
713 enable = 1; /* i.e. we will do checking */
714 if ((*class_code >> 16) == PCI_BASE_CLASS_DISPLAY)
715 enable = 0;
716
717 if (!enable)
718 pdn->eeh_mode |= EEH_MODE_NOCHECK;
719
720 /* Ok... see if this device supports EEH. Some do, some don't,
721 * and the only way to find out is to check each and every one. */
722 regs = (u32 *)get_property(dn, "reg", NULL);
723 if (regs) {
724 /* First register entry is addr (00BBSS00) */
725 /* Try to enable eeh */
726 ret = rtas_call(ibm_set_eeh_option, 4, 1, NULL,
727 regs[0], info->buid_hi, info->buid_lo,
728 EEH_ENABLE);
729 if (ret == 0) {
730 eeh_subsystem_enabled = 1;
731 pdn->eeh_mode |= EEH_MODE_SUPPORTED;
732 pdn->eeh_config_addr = regs[0];
733 #ifdef DEBUG
734 printk(KERN_DEBUG "EEH: %s: eeh enabled\n", dn->full_name);
735 #endif
736 } else {
737
738 /* This device doesn't support EEH, but it may have an
739 * EEH parent, in which case we mark it as supported. */
740 if (dn->parent && PCI_DN(dn->parent)
741 && (PCI_DN(dn->parent)->eeh_mode & EEH_MODE_SUPPORTED)) {
742 /* Parent supports EEH. */
743 pdn->eeh_mode |= EEH_MODE_SUPPORTED;
744 pdn->eeh_config_addr = PCI_DN(dn->parent)->eeh_config_addr;
745 return NULL;
746 }
747 }
748 } else {
749 printk(KERN_WARNING "EEH: %s: unable to get reg property.\n",
750 dn->full_name);
751 }
752
753 return NULL;
754 }
755
756 /*
757 * Initialize EEH by trying to enable it for all of the adapters in the system.
758 * As a side effect we can determine here if eeh is supported at all.
759 * Note that we leave EEH on so failed config cycles won't cause a machine
760 * check. If a user turns off EEH for a particular adapter they are really
761 * telling Linux to ignore errors. Some hardware (e.g. POWER5) won't
762 * grant access to a slot if EEH isn't enabled, and so we always enable
763 * EEH for all slots/all devices.
764 *
765 * The eeh-force-off option disables EEH checking globally, for all slots.
766 * Even if force-off is set, the EEH hardware is still enabled, so that
767 * newer systems can boot.
768 */
769 void __init eeh_init(void)
770 {
771 struct device_node *phb, *np;
772 struct eeh_early_enable_info info;
773
774 np = of_find_node_by_path("/rtas");
775 if (np == NULL)
776 return;
777
778 ibm_set_eeh_option = rtas_token("ibm,set-eeh-option");
779 ibm_set_slot_reset = rtas_token("ibm,set-slot-reset");
780 ibm_read_slot_reset_state2 = rtas_token("ibm,read-slot-reset-state2");
781 ibm_read_slot_reset_state = rtas_token("ibm,read-slot-reset-state");
782 ibm_slot_error_detail = rtas_token("ibm,slot-error-detail");
783
784 if (ibm_set_eeh_option == RTAS_UNKNOWN_SERVICE)
785 return;
786
787 eeh_error_buf_size = rtas_token("rtas-error-log-max");
788 if (eeh_error_buf_size == RTAS_UNKNOWN_SERVICE) {
789 eeh_error_buf_size = 1024;
790 }
791 if (eeh_error_buf_size > RTAS_ERROR_LOG_MAX) {
792 printk(KERN_WARNING "EEH: rtas-error-log-max is bigger than allocated "
793 "buffer ! (%d vs %d)", eeh_error_buf_size, RTAS_ERROR_LOG_MAX);
794 eeh_error_buf_size = RTAS_ERROR_LOG_MAX;
795 }
796
797 /* Enable EEH for all adapters. Note that eeh requires buid's */
798 for (phb = of_find_node_by_name(NULL, "pci"); phb;
799 phb = of_find_node_by_name(phb, "pci")) {
800 unsigned long buid;
801
802 buid = get_phb_buid(phb);
803 if (buid == 0 || PCI_DN(phb) == NULL)
804 continue;
805
806 info.buid_lo = BUID_LO(buid);
807 info.buid_hi = BUID_HI(buid);
808 traverse_pci_devices(phb, early_enable_eeh, &info);
809 }
810
811 if (eeh_subsystem_enabled)
812 printk(KERN_INFO "EEH: PCI Enhanced I/O Error Handling Enabled\n");
813 else
814 printk(KERN_WARNING "EEH: No capable adapters found\n");
815 }
816
817 /**
818 * eeh_add_device_early - enable EEH for the indicated device_node
819 * @dn: device node for which to set up EEH
820 *
821 * This routine must be used to perform EEH initialization for PCI
822 * devices that were added after system boot (e.g. hotplug, dlpar).
823 * This routine must be called before any i/o is performed to the
824 * adapter (inluding any config-space i/o).
825 * Whether this actually enables EEH or not for this device depends
826 * on the CEC architecture, type of the device, on earlier boot
827 * command-line arguments & etc.
828 */
829 void eeh_add_device_early(struct device_node *dn)
830 {
831 struct pci_controller *phb;
832 struct eeh_early_enable_info info;
833
834 if (!dn || !PCI_DN(dn))
835 return;
836 phb = PCI_DN(dn)->phb;
837 if (NULL == phb || 0 == phb->buid) {
838 printk(KERN_WARNING "EEH: Expected buid but found none for %s\n",
839 dn->full_name);
840 dump_stack();
841 return;
842 }
843
844 info.buid_hi = BUID_HI(phb->buid);
845 info.buid_lo = BUID_LO(phb->buid);
846 early_enable_eeh(dn, &info);
847 }
848 EXPORT_SYMBOL_GPL(eeh_add_device_early);
849
850 /**
851 * eeh_add_device_late - perform EEH initialization for the indicated pci device
852 * @dev: pci device for which to set up EEH
853 *
854 * This routine must be used to complete EEH initialization for PCI
855 * devices that were added after system boot (e.g. hotplug, dlpar).
856 */
857 void eeh_add_device_late(struct pci_dev *dev)
858 {
859 struct device_node *dn;
860
861 if (!dev || !eeh_subsystem_enabled)
862 return;
863
864 #ifdef DEBUG
865 printk(KERN_DEBUG "EEH: adding device %s\n", pci_name(dev));
866 #endif
867
868 pci_dev_get (dev);
869 dn = pci_device_to_OF_node(dev);
870 PCI_DN(dn)->pcidev = dev;
871
872 pci_addr_cache_insert_device (dev);
873 }
874 EXPORT_SYMBOL_GPL(eeh_add_device_late);
875
876 /**
877 * eeh_remove_device - undo EEH setup for the indicated pci device
878 * @dev: pci device to be removed
879 *
880 * This routine should be when a device is removed from a running
881 * system (e.g. by hotplug or dlpar).
882 */
883 void eeh_remove_device(struct pci_dev *dev)
884 {
885 struct device_node *dn;
886 if (!dev || !eeh_subsystem_enabled)
887 return;
888
889 /* Unregister the device with the EEH/PCI address search system */
890 #ifdef DEBUG
891 printk(KERN_DEBUG "EEH: remove device %s\n", pci_name(dev));
892 #endif
893 pci_addr_cache_remove_device(dev);
894
895 dn = pci_device_to_OF_node(dev);
896 PCI_DN(dn)->pcidev = NULL;
897 pci_dev_put (dev);
898 }
899 EXPORT_SYMBOL_GPL(eeh_remove_device);
900
901 static int proc_eeh_show(struct seq_file *m, void *v)
902 {
903 unsigned int cpu;
904 unsigned long ffs = 0, positives = 0, failures = 0;
905 unsigned long resets = 0;
906
907 for_each_cpu(cpu) {
908 ffs += per_cpu(total_mmio_ffs, cpu);
909 positives += per_cpu(false_positives, cpu);
910 failures += per_cpu(ignored_failures, cpu);
911 resets += per_cpu(slot_resets, cpu);
912 }
913
914 if (0 == eeh_subsystem_enabled) {
915 seq_printf(m, "EEH Subsystem is globally disabled\n");
916 seq_printf(m, "eeh_total_mmio_ffs=%ld\n", ffs);
917 } else {
918 seq_printf(m, "EEH Subsystem is enabled\n");
919 seq_printf(m, "eeh_total_mmio_ffs=%ld\n"
920 "eeh_false_positives=%ld\n"
921 "eeh_ignored_failures=%ld\n"
922 "eeh_slot_resets=%ld\n"
923 "eeh_fail_count=%d\n",
924 ffs, positives, failures, resets,
925 eeh_fail_count.counter);
926 }
927
928 return 0;
929 }
930
931 static int proc_eeh_open(struct inode *inode, struct file *file)
932 {
933 return single_open(file, proc_eeh_show, NULL);
934 }
935
936 static struct file_operations proc_eeh_operations = {
937 .open = proc_eeh_open,
938 .read = seq_read,
939 .llseek = seq_lseek,
940 .release = single_release,
941 };
942
943 static int __init eeh_init_proc(void)
944 {
945 struct proc_dir_entry *e;
946
947 if (systemcfg->platform & PLATFORM_PSERIES) {
948 e = create_proc_entry("ppc64/eeh", 0, NULL);
949 if (e)
950 e->proc_fops = &proc_eeh_operations;
951 }
952
953 return 0;
954 }
955 __initcall(eeh_init_proc);