Pull thermal into release branch
[linux-drm-fsl-dcu.git] / arch / powerpc / platforms / pseries / 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/delay.h>
21 #include <linux/init.h>
22 #include <linux/list.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/eeh_event.h>
31 #include <asm/io.h>
32 #include <asm/machdep.h>
33 #include <asm/ppc-pci.h>
34 #include <asm/rtas.h>
35
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),
50  *  which in turn might be caused by low voltage on the bus, dust,
51  *  vibration, humidity, radioactivity or plain-old failed hardware.
52  *
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
72 /* If a device driver keeps reading an MMIO register in an interrupt
73  * 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  */
77 #define EEH_MAX_FAILS   2100000
78
79 /* Time to wait for a PCI slot to retport status, in milliseconds */
80 #define PCI_BUS_RESET_WAIT_MSEC (60*1000)
81
82 /* RTAS tokens */
83 static int ibm_set_eeh_option;
84 static int ibm_set_slot_reset;
85 static int ibm_read_slot_reset_state;
86 static int ibm_read_slot_reset_state2;
87 static int ibm_slot_error_detail;
88 static int ibm_get_config_addr_info;
89 static int ibm_get_config_addr_info2;
90 static int ibm_configure_bridge;
91
92 int eeh_subsystem_enabled;
93 EXPORT_SYMBOL(eeh_subsystem_enabled);
94
95 /* Lock to avoid races due to multiple reports of an error */
96 static DEFINE_SPINLOCK(confirm_error_lock);
97
98 /* Buffer for reporting slot-error-detail rtas calls */
99 static unsigned char slot_errbuf[RTAS_ERROR_LOG_MAX];
100 static DEFINE_SPINLOCK(slot_errbuf_lock);
101 static int eeh_error_buf_size;
102
103 #define EEH_PCI_REGS_LOG_LEN 4096
104 static unsigned char pci_regs_buf[EEH_PCI_REGS_LOG_LEN];
105
106 /* System monitoring statistics */
107 static unsigned long no_device;
108 static unsigned long no_dn;
109 static unsigned long no_cfg_addr;
110 static unsigned long ignored_check;
111 static unsigned long total_mmio_ffs;
112 static unsigned long false_positives;
113 static unsigned long ignored_failures;
114 static unsigned long slot_resets;
115
116 #define IS_BRIDGE(class_code) (((class_code)<<16) == PCI_BASE_CLASS_BRIDGE)
117
118 /* --------------------------------------------------------------- */
119 /* Below lies the EEH event infrastructure */
120
121 static void rtas_slot_error_detail(struct pci_dn *pdn, int severity,
122                                    char *driver_log, size_t loglen)
123 {
124         int config_addr;
125         unsigned long flags;
126         int rc;
127
128         /* Log the error with the rtas logger */
129         spin_lock_irqsave(&slot_errbuf_lock, flags);
130         memset(slot_errbuf, 0, eeh_error_buf_size);
131
132         /* Use PE configuration address, if present */
133         config_addr = pdn->eeh_config_addr;
134         if (pdn->eeh_pe_config_addr)
135                 config_addr = pdn->eeh_pe_config_addr;
136
137         rc = rtas_call(ibm_slot_error_detail,
138                        8, 1, NULL, config_addr,
139                        BUID_HI(pdn->phb->buid),
140                        BUID_LO(pdn->phb->buid),
141                        virt_to_phys(driver_log), loglen,
142                        virt_to_phys(slot_errbuf),
143                        eeh_error_buf_size,
144                        severity);
145
146         if (rc == 0)
147                 log_error(slot_errbuf, ERR_TYPE_RTAS_LOG, 0);
148         spin_unlock_irqrestore(&slot_errbuf_lock, flags);
149 }
150
151 /**
152  * gather_pci_data - copy assorted PCI config space registers to buff
153  * @pdn: device to report data for
154  * @buf: point to buffer in which to log
155  * @len: amount of room in buffer
156  *
157  * This routine captures assorted PCI configuration space data,
158  * and puts them into a buffer for RTAS error logging.
159  */
160 static size_t gather_pci_data(struct pci_dn *pdn, char * buf, size_t len)
161 {
162         u32 cfg;
163         int cap, i;
164         int n = 0;
165
166         n += scnprintf(buf+n, len-n, "%s\n", pdn->node->full_name);
167         printk(KERN_WARNING "EEH: of node=%s\n", pdn->node->full_name);
168
169         rtas_read_config(pdn, PCI_VENDOR_ID, 4, &cfg);
170         n += scnprintf(buf+n, len-n, "dev/vend:%08x\n", cfg);
171         printk(KERN_WARNING "EEH: PCI device/vendor: %08x\n", cfg);
172
173         rtas_read_config(pdn, PCI_COMMAND, 4, &cfg);
174         n += scnprintf(buf+n, len-n, "cmd/stat:%x\n", cfg);
175         printk(KERN_WARNING "EEH: PCI cmd/status register: %08x\n", cfg);
176
177         /* Dump out the PCI-X command and status regs */
178         cap = pci_find_capability(pdn->pcidev, PCI_CAP_ID_PCIX);
179         if (cap) {
180                 rtas_read_config(pdn, cap, 4, &cfg);
181                 n += scnprintf(buf+n, len-n, "pcix-cmd:%x\n", cfg);
182                 printk(KERN_WARNING "EEH: PCI-X cmd: %08x\n", cfg);
183
184                 rtas_read_config(pdn, cap+4, 4, &cfg);
185                 n += scnprintf(buf+n, len-n, "pcix-stat:%x\n", cfg);
186                 printk(KERN_WARNING "EEH: PCI-X status: %08x\n", cfg);
187         }
188
189         /* If PCI-E capable, dump PCI-E cap 10, and the AER */
190         cap = pci_find_capability(pdn->pcidev, PCI_CAP_ID_EXP);
191         if (cap) {
192                 n += scnprintf(buf+n, len-n, "pci-e cap10:\n");
193                 printk(KERN_WARNING
194                        "EEH: PCI-E capabilities and status follow:\n");
195
196                 for (i=0; i<=8; i++) {
197                         rtas_read_config(pdn, cap+4*i, 4, &cfg);
198                         n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
199                         printk(KERN_WARNING "EEH: PCI-E %02x: %08x\n", i, cfg);
200                 }
201
202                 cap = pci_find_ext_capability(pdn->pcidev,PCI_EXT_CAP_ID_ERR);
203                 if (cap) {
204                         n += scnprintf(buf+n, len-n, "pci-e AER:\n");
205                         printk(KERN_WARNING
206                                "EEH: PCI-E AER capability register set follows:\n");
207
208                         for (i=0; i<14; i++) {
209                                 rtas_read_config(pdn, cap+4*i, 4, &cfg);
210                                 n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
211                                 printk(KERN_WARNING "EEH: PCI-E AER %02x: %08x\n", i, cfg);
212                         }
213                 }
214         }
215         return n;
216 }
217
218 void eeh_slot_error_detail(struct pci_dn *pdn, int severity)
219 {
220         size_t loglen = 0;
221         memset(pci_regs_buf, 0, EEH_PCI_REGS_LOG_LEN);
222
223         rtas_pci_enable(pdn, EEH_THAW_MMIO);
224         loglen = gather_pci_data(pdn, pci_regs_buf, EEH_PCI_REGS_LOG_LEN);
225
226         rtas_slot_error_detail(pdn, severity, pci_regs_buf, loglen);
227 }
228
229 /**
230  * read_slot_reset_state - Read the reset state of a device node's slot
231  * @dn: device node to read
232  * @rets: array to return results in
233  */
234 static int read_slot_reset_state(struct pci_dn *pdn, int rets[])
235 {
236         int token, outputs;
237         int config_addr;
238
239         if (ibm_read_slot_reset_state2 != RTAS_UNKNOWN_SERVICE) {
240                 token = ibm_read_slot_reset_state2;
241                 outputs = 4;
242         } else {
243                 token = ibm_read_slot_reset_state;
244                 rets[2] = 0; /* fake PE Unavailable info */
245                 outputs = 3;
246         }
247
248         /* Use PE configuration address, if present */
249         config_addr = pdn->eeh_config_addr;
250         if (pdn->eeh_pe_config_addr)
251                 config_addr = pdn->eeh_pe_config_addr;
252
253         return rtas_call(token, 3, outputs, rets, config_addr,
254                          BUID_HI(pdn->phb->buid), BUID_LO(pdn->phb->buid));
255 }
256
257 /**
258  * eeh_wait_for_slot_status - returns error status of slot
259  * @pdn pci device node
260  * @max_wait_msecs maximum number to millisecs to wait
261  *
262  * Return negative value if a permanent error, else return
263  * Partition Endpoint (PE) status value.
264  *
265  * If @max_wait_msecs is positive, then this routine will
266  * sleep until a valid status can be obtained, or until
267  * the max allowed wait time is exceeded, in which case
268  * a -2 is returned.
269  */
270 int
271 eeh_wait_for_slot_status(struct pci_dn *pdn, int max_wait_msecs)
272 {
273         int rc;
274         int rets[3];
275         int mwait;
276
277         while (1) {
278                 rc = read_slot_reset_state(pdn, rets);
279                 if (rc) return rc;
280                 if (rets[1] == 0) return -1;  /* EEH is not supported */
281
282                 if (rets[0] != 5) return rets[0]; /* return actual status */
283
284                 if (rets[2] == 0) return -1; /* permanently unavailable */
285
286                 if (max_wait_msecs <= 0) return -1;
287
288                 mwait = rets[2];
289                 if (mwait <= 0) {
290                         printk (KERN_WARNING
291                                 "EEH: Firmware returned bad wait value=%d\n", mwait);
292                         mwait = 1000;
293                 } else if (mwait > 300*1000) {
294                         printk (KERN_WARNING
295                                 "EEH: Firmware is taking too long, time=%d\n", mwait);
296                         mwait = 300*1000;
297                 }
298                 max_wait_msecs -= mwait;
299                 msleep (mwait);
300         }
301
302         printk(KERN_WARNING "EEH: Timed out waiting for slot status\n");
303         return -2;
304 }
305
306 /**
307  * eeh_token_to_phys - convert EEH address token to phys address
308  * @token i/o token, should be address in the form 0xA....
309  */
310 static inline unsigned long eeh_token_to_phys(unsigned long token)
311 {
312         pte_t *ptep;
313         unsigned long pa;
314
315         ptep = find_linux_pte(init_mm.pgd, token);
316         if (!ptep)
317                 return token;
318         pa = pte_pfn(*ptep) << PAGE_SHIFT;
319
320         return pa | (token & (PAGE_SIZE-1));
321 }
322
323 /** 
324  * Return the "partitionable endpoint" (pe) under which this device lies
325  */
326 struct device_node * find_device_pe(struct device_node *dn)
327 {
328         while ((dn->parent) && PCI_DN(dn->parent) &&
329               (PCI_DN(dn->parent)->eeh_mode & EEH_MODE_SUPPORTED)) {
330                 dn = dn->parent;
331         }
332         return dn;
333 }
334
335 /** Mark all devices that are peers of this device as failed.
336  *  Mark the device driver too, so that it can see the failure
337  *  immediately; this is critical, since some drivers poll
338  *  status registers in interrupts ... If a driver is polling,
339  *  and the slot is frozen, then the driver can deadlock in
340  *  an interrupt context, which is bad.
341  */
342
343 static void __eeh_mark_slot (struct device_node *dn, int mode_flag)
344 {
345         while (dn) {
346                 if (PCI_DN(dn)) {
347                         /* Mark the pci device driver too */
348                         struct pci_dev *dev = PCI_DN(dn)->pcidev;
349
350                         PCI_DN(dn)->eeh_mode |= mode_flag;
351
352                         if (dev && dev->driver)
353                                 dev->error_state = pci_channel_io_frozen;
354
355                         if (dn->child)
356                                 __eeh_mark_slot (dn->child, mode_flag);
357                 }
358                 dn = dn->sibling;
359         }
360 }
361
362 void eeh_mark_slot (struct device_node *dn, int mode_flag)
363 {
364         struct pci_dev *dev;
365         dn = find_device_pe (dn);
366
367         /* Back up one, since config addrs might be shared */
368         if (!pcibios_find_pci_bus(dn) && PCI_DN(dn->parent))
369                 dn = dn->parent;
370
371         PCI_DN(dn)->eeh_mode |= mode_flag;
372
373         /* Mark the pci device too */
374         dev = PCI_DN(dn)->pcidev;
375         if (dev)
376                 dev->error_state = pci_channel_io_frozen;
377
378         __eeh_mark_slot (dn->child, mode_flag);
379 }
380
381 static void __eeh_clear_slot (struct device_node *dn, int mode_flag)
382 {
383         while (dn) {
384                 if (PCI_DN(dn)) {
385                         PCI_DN(dn)->eeh_mode &= ~mode_flag;
386                         PCI_DN(dn)->eeh_check_count = 0;
387                         if (dn->child)
388                                 __eeh_clear_slot (dn->child, mode_flag);
389                 }
390                 dn = dn->sibling;
391         }
392 }
393
394 void eeh_clear_slot (struct device_node *dn, int mode_flag)
395 {
396         unsigned long flags;
397         spin_lock_irqsave(&confirm_error_lock, flags);
398         
399         dn = find_device_pe (dn);
400         
401         /* Back up one, since config addrs might be shared */
402         if (!pcibios_find_pci_bus(dn) && PCI_DN(dn->parent))
403                 dn = dn->parent;
404
405         PCI_DN(dn)->eeh_mode &= ~mode_flag;
406         PCI_DN(dn)->eeh_check_count = 0;
407         __eeh_clear_slot (dn->child, mode_flag);
408         spin_unlock_irqrestore(&confirm_error_lock, flags);
409 }
410
411 /**
412  * eeh_dn_check_failure - check if all 1's data is due to EEH slot freeze
413  * @dn device node
414  * @dev pci device, if known
415  *
416  * Check for an EEH failure for the given device node.  Call this
417  * routine if the result of a read was all 0xff's and you want to
418  * find out if this is due to an EEH slot freeze.  This routine
419  * will query firmware for the EEH status.
420  *
421  * Returns 0 if there has not been an EEH error; otherwise returns
422  * a non-zero value and queues up a slot isolation event notification.
423  *
424  * It is safe to call this routine in an interrupt context.
425  */
426 int eeh_dn_check_failure(struct device_node *dn, struct pci_dev *dev)
427 {
428         int ret;
429         int rets[3];
430         unsigned long flags;
431         struct pci_dn *pdn;
432         int rc = 0;
433
434         total_mmio_ffs++;
435
436         if (!eeh_subsystem_enabled)
437                 return 0;
438
439         if (!dn) {
440                 no_dn++;
441                 return 0;
442         }
443         pdn = PCI_DN(dn);
444
445         /* Access to IO BARs might get this far and still not want checking. */
446         if (!(pdn->eeh_mode & EEH_MODE_SUPPORTED) ||
447             pdn->eeh_mode & EEH_MODE_NOCHECK) {
448                 ignored_check++;
449 #ifdef DEBUG
450                 printk ("EEH:ignored check (%x) for %s %s\n", 
451                         pdn->eeh_mode, pci_name (dev), dn->full_name);
452 #endif
453                 return 0;
454         }
455
456         if (!pdn->eeh_config_addr && !pdn->eeh_pe_config_addr) {
457                 no_cfg_addr++;
458                 return 0;
459         }
460
461         /* If we already have a pending isolation event for this
462          * slot, we know it's bad already, we don't need to check.
463          * Do this checking under a lock; as multiple PCI devices
464          * in one slot might report errors simultaneously, and we
465          * only want one error recovery routine running.
466          */
467         spin_lock_irqsave(&confirm_error_lock, flags);
468         rc = 1;
469         if (pdn->eeh_mode & EEH_MODE_ISOLATED) {
470                 pdn->eeh_check_count ++;
471                 if (pdn->eeh_check_count >= EEH_MAX_FAILS) {
472                         printk (KERN_ERR "EEH: Device driver ignored %d bad reads, panicing\n",
473                                 pdn->eeh_check_count);
474                         dump_stack();
475                         msleep(5000);
476                         
477                         /* re-read the slot reset state */
478                         if (read_slot_reset_state(pdn, rets) != 0)
479                                 rets[0] = -1;   /* reset state unknown */
480
481                         /* If we are here, then we hit an infinite loop. Stop. */
482                         panic("EEH: MMIO halt (%d) on device:%s\n", rets[0], pci_name(dev));
483                 }
484                 goto dn_unlock;
485         }
486
487         /*
488          * Now test for an EEH failure.  This is VERY expensive.
489          * Note that the eeh_config_addr may be a parent device
490          * in the case of a device behind a bridge, or it may be
491          * function zero of a multi-function device.
492          * In any case they must share a common PHB.
493          */
494         ret = read_slot_reset_state(pdn, rets);
495
496         /* If the call to firmware failed, punt */
497         if (ret != 0) {
498                 printk(KERN_WARNING "EEH: read_slot_reset_state() failed; rc=%d dn=%s\n",
499                        ret, dn->full_name);
500                 false_positives++;
501                 rc = 0;
502                 goto dn_unlock;
503         }
504
505         /* Note that config-io to empty slots may fail;
506          * they are empty when they don't have children. */
507         if ((rets[0] == 5) && (dn->child == NULL)) {
508                 false_positives++;
509                 rc = 0;
510                 goto dn_unlock;
511         }
512
513         /* If EEH is not supported on this device, punt. */
514         if (rets[1] != 1) {
515                 printk(KERN_WARNING "EEH: event on unsupported device, rc=%d dn=%s\n",
516                        ret, dn->full_name);
517                 false_positives++;
518                 rc = 0;
519                 goto dn_unlock;
520         }
521
522         /* If not the kind of error we know about, punt. */
523         if (rets[0] != 1 && rets[0] != 2 && rets[0] != 4 && rets[0] != 5) {
524                 false_positives++;
525                 rc = 0;
526                 goto dn_unlock;
527         }
528
529         slot_resets++;
530  
531         /* Avoid repeated reports of this failure, including problems
532          * with other functions on this device, and functions under
533          * bridges. */
534         eeh_mark_slot (dn, EEH_MODE_ISOLATED);
535         spin_unlock_irqrestore(&confirm_error_lock, flags);
536
537         eeh_send_failure_event (dn, dev);
538
539         /* Most EEH events are due to device driver bugs.  Having
540          * a stack trace will help the device-driver authors figure
541          * out what happened.  So print that out. */
542         dump_stack();
543         return 1;
544
545 dn_unlock:
546         spin_unlock_irqrestore(&confirm_error_lock, flags);
547         return rc;
548 }
549
550 EXPORT_SYMBOL_GPL(eeh_dn_check_failure);
551
552 /**
553  * eeh_check_failure - check if all 1's data is due to EEH slot freeze
554  * @token i/o token, should be address in the form 0xA....
555  * @val value, should be all 1's (XXX why do we need this arg??)
556  *
557  * Check for an EEH failure at the given token address.  Call this
558  * routine if the result of a read was all 0xff's and you want to
559  * find out if this is due to an EEH slot freeze event.  This routine
560  * will query firmware for the EEH status.
561  *
562  * Note this routine is safe to call in an interrupt context.
563  */
564 unsigned long eeh_check_failure(const volatile void __iomem *token, unsigned long val)
565 {
566         unsigned long addr;
567         struct pci_dev *dev;
568         struct device_node *dn;
569
570         /* Finding the phys addr + pci device; this is pretty quick. */
571         addr = eeh_token_to_phys((unsigned long __force) token);
572         dev = pci_get_device_by_addr(addr);
573         if (!dev) {
574                 no_device++;
575                 return val;
576         }
577
578         dn = pci_device_to_OF_node(dev);
579         eeh_dn_check_failure (dn, dev);
580
581         pci_dev_put(dev);
582         return val;
583 }
584
585 EXPORT_SYMBOL(eeh_check_failure);
586
587 /* ------------------------------------------------------------- */
588 /* The code below deals with error recovery */
589
590 /**
591  * rtas_pci_enable - enable MMIO or DMA transfers for this slot
592  * @pdn pci device node
593  */
594
595 int
596 rtas_pci_enable(struct pci_dn *pdn, int function)
597 {
598         int config_addr;
599         int rc;
600
601         /* Use PE configuration address, if present */
602         config_addr = pdn->eeh_config_addr;
603         if (pdn->eeh_pe_config_addr)
604                 config_addr = pdn->eeh_pe_config_addr;
605
606         rc = rtas_call(ibm_set_eeh_option, 4, 1, NULL,
607                        config_addr,
608                        BUID_HI(pdn->phb->buid),
609                        BUID_LO(pdn->phb->buid),
610                             function);
611
612         if (rc)
613                 printk(KERN_WARNING "EEH: Unexpected state change %d, err=%d dn=%s\n",
614                         function, rc, pdn->node->full_name);
615
616         rc = eeh_wait_for_slot_status (pdn, PCI_BUS_RESET_WAIT_MSEC);
617         if ((rc == 4) && (function == EEH_THAW_MMIO))
618                 return 0;
619
620         return rc;
621 }
622
623 /**
624  * rtas_pci_slot_reset - raises/lowers the pci #RST line
625  * @pdn pci device node
626  * @state: 1/0 to raise/lower the #RST
627  *
628  * Clear the EEH-frozen condition on a slot.  This routine
629  * asserts the PCI #RST line if the 'state' argument is '1',
630  * and drops the #RST line if 'state is '0'.  This routine is
631  * safe to call in an interrupt context.
632  *
633  */
634
635 static void
636 rtas_pci_slot_reset(struct pci_dn *pdn, int state)
637 {
638         int config_addr;
639         int rc;
640
641         BUG_ON (pdn==NULL); 
642
643         if (!pdn->phb) {
644                 printk (KERN_WARNING "EEH: in slot reset, device node %s has no phb\n",
645                         pdn->node->full_name);
646                 return;
647         }
648
649         /* Use PE configuration address, if present */
650         config_addr = pdn->eeh_config_addr;
651         if (pdn->eeh_pe_config_addr)
652                 config_addr = pdn->eeh_pe_config_addr;
653
654         rc = rtas_call(ibm_set_slot_reset,4,1, NULL,
655                        config_addr,
656                        BUID_HI(pdn->phb->buid),
657                        BUID_LO(pdn->phb->buid),
658                        state);
659         if (rc)
660                 printk (KERN_WARNING "EEH: Unable to reset the failed slot,"
661                         " (%d) #RST=%d dn=%s\n",
662                         rc, state, pdn->node->full_name);
663 }
664
665 /**
666  * pcibios_set_pcie_slot_reset - Set PCI-E reset state
667  * @dev:        pci device struct
668  * @state:      reset state to enter
669  *
670  * Return value:
671  *      0 if success
672  **/
673 int pcibios_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
674 {
675         struct device_node *dn = pci_device_to_OF_node(dev);
676         struct pci_dn *pdn = PCI_DN(dn);
677
678         switch (state) {
679         case pcie_deassert_reset:
680                 rtas_pci_slot_reset(pdn, 0);
681                 break;
682         case pcie_hot_reset:
683                 rtas_pci_slot_reset(pdn, 1);
684                 break;
685         case pcie_warm_reset:
686                 rtas_pci_slot_reset(pdn, 3);
687                 break;
688         default:
689                 return -EINVAL;
690         };
691
692         return 0;
693 }
694
695 /**
696  * rtas_set_slot_reset -- assert the pci #RST line for 1/4 second
697  * @pdn: pci device node to be reset.
698  *
699  *  Return 0 if success, else a non-zero value.
700  */
701
702 static void __rtas_set_slot_reset(struct pci_dn *pdn)
703 {
704         rtas_pci_slot_reset (pdn, 1);
705
706         /* The PCI bus requires that the reset be held high for at least
707          * a 100 milliseconds. We wait a bit longer 'just in case'.  */
708
709 #define PCI_BUS_RST_HOLD_TIME_MSEC 250
710         msleep (PCI_BUS_RST_HOLD_TIME_MSEC);
711         
712         /* We might get hit with another EEH freeze as soon as the 
713          * pci slot reset line is dropped. Make sure we don't miss
714          * these, and clear the flag now. */
715         eeh_clear_slot (pdn->node, EEH_MODE_ISOLATED);
716
717         rtas_pci_slot_reset (pdn, 0);
718
719         /* After a PCI slot has been reset, the PCI Express spec requires
720          * a 1.5 second idle time for the bus to stabilize, before starting
721          * up traffic. */
722 #define PCI_BUS_SETTLE_TIME_MSEC 1800
723         msleep (PCI_BUS_SETTLE_TIME_MSEC);
724 }
725
726 int rtas_set_slot_reset(struct pci_dn *pdn)
727 {
728         int i, rc;
729
730         /* Take three shots at resetting the bus */
731         for (i=0; i<3; i++) {
732                 __rtas_set_slot_reset(pdn);
733
734                 rc = eeh_wait_for_slot_status(pdn, PCI_BUS_RESET_WAIT_MSEC);
735                 if (rc == 0)
736                         return 0;
737
738                 if (rc < 0) {
739                         printk (KERN_ERR "EEH: unrecoverable slot failure %s\n",
740                                 pdn->node->full_name);
741                         return -1;
742                 }
743                 printk (KERN_ERR "EEH: bus reset %d failed on slot %s\n",
744                         i+1, pdn->node->full_name);
745         }
746
747         return -1;
748 }
749
750 /* ------------------------------------------------------- */
751 /** Save and restore of PCI BARs
752  *
753  * Although firmware will set up BARs during boot, it doesn't
754  * set up device BAR's after a device reset, although it will,
755  * if requested, set up bridge configuration. Thus, we need to
756  * configure the PCI devices ourselves.  
757  */
758
759 /**
760  * __restore_bars - Restore the Base Address Registers
761  * @pdn: pci device node
762  *
763  * Loads the PCI configuration space base address registers,
764  * the expansion ROM base address, the latency timer, and etc.
765  * from the saved values in the device node.
766  */
767 static inline void __restore_bars (struct pci_dn *pdn)
768 {
769         int i;
770
771         if (NULL==pdn->phb) return;
772         for (i=4; i<10; i++) {
773                 rtas_write_config(pdn, i*4, 4, pdn->config_space[i]);
774         }
775
776         /* 12 == Expansion ROM Address */
777         rtas_write_config(pdn, 12*4, 4, pdn->config_space[12]);
778
779 #define BYTE_SWAP(OFF) (8*((OFF)/4)+3-(OFF))
780 #define SAVED_BYTE(OFF) (((u8 *)(pdn->config_space))[BYTE_SWAP(OFF)])
781
782         rtas_write_config (pdn, PCI_CACHE_LINE_SIZE, 1,
783                     SAVED_BYTE(PCI_CACHE_LINE_SIZE));
784
785         rtas_write_config (pdn, PCI_LATENCY_TIMER, 1,
786                     SAVED_BYTE(PCI_LATENCY_TIMER));
787
788         /* max latency, min grant, interrupt pin and line */
789         rtas_write_config(pdn, 15*4, 4, pdn->config_space[15]);
790 }
791
792 /**
793  * eeh_restore_bars - restore the PCI config space info
794  *
795  * This routine performs a recursive walk to the children
796  * of this device as well.
797  */
798 void eeh_restore_bars(struct pci_dn *pdn)
799 {
800         struct device_node *dn;
801         if (!pdn) 
802                 return;
803         
804         if ((pdn->eeh_mode & EEH_MODE_SUPPORTED) && !IS_BRIDGE(pdn->class_code))
805                 __restore_bars (pdn);
806
807         dn = pdn->node->child;
808         while (dn) {
809                 eeh_restore_bars (PCI_DN(dn));
810                 dn = dn->sibling;
811         }
812 }
813
814 /**
815  * eeh_save_bars - save device bars
816  *
817  * Save the values of the device bars. Unlike the restore
818  * routine, this routine is *not* recursive. This is because
819  * PCI devices are added individuallly; but, for the restore,
820  * an entire slot is reset at a time.
821  */
822 static void eeh_save_bars(struct pci_dn *pdn)
823 {
824         int i;
825
826         if (!pdn )
827                 return;
828         
829         for (i = 0; i < 16; i++)
830                 rtas_read_config(pdn, i * 4, 4, &pdn->config_space[i]);
831 }
832
833 void
834 rtas_configure_bridge(struct pci_dn *pdn)
835 {
836         int config_addr;
837         int rc;
838
839         /* Use PE configuration address, if present */
840         config_addr = pdn->eeh_config_addr;
841         if (pdn->eeh_pe_config_addr)
842                 config_addr = pdn->eeh_pe_config_addr;
843
844         rc = rtas_call(ibm_configure_bridge,3,1, NULL,
845                        config_addr,
846                        BUID_HI(pdn->phb->buid),
847                        BUID_LO(pdn->phb->buid));
848         if (rc) {
849                 printk (KERN_WARNING "EEH: Unable to configure device bridge (%d) for %s\n",
850                         rc, pdn->node->full_name);
851         }
852 }
853
854 /* ------------------------------------------------------------- */
855 /* The code below deals with enabling EEH for devices during  the
856  * early boot sequence.  EEH must be enabled before any PCI probing
857  * can be done.
858  */
859
860 #define EEH_ENABLE 1
861
862 struct eeh_early_enable_info {
863         unsigned int buid_hi;
864         unsigned int buid_lo;
865 };
866
867 static int get_pe_addr (int config_addr,
868                         struct eeh_early_enable_info *info)
869 {
870         unsigned int rets[3];
871         int ret;
872
873         /* Use latest config-addr token on power6 */
874         if (ibm_get_config_addr_info2 != RTAS_UNKNOWN_SERVICE) {
875                 /* Make sure we have a PE in hand */
876                 ret = rtas_call (ibm_get_config_addr_info2, 4, 2, rets,
877                         config_addr, info->buid_hi, info->buid_lo, 1);
878                 if (ret || (rets[0]==0))
879                         return 0;
880
881                 ret = rtas_call (ibm_get_config_addr_info2, 4, 2, rets,
882                         config_addr, info->buid_hi, info->buid_lo, 0);
883                 if (ret)
884                         return 0;
885                 return rets[0];
886         }
887
888         /* Use older config-addr token on power5 */
889         if (ibm_get_config_addr_info != RTAS_UNKNOWN_SERVICE) {
890                 ret = rtas_call (ibm_get_config_addr_info, 4, 2, rets,
891                         config_addr, info->buid_hi, info->buid_lo, 0);
892                 if (ret)
893                         return 0;
894                 return rets[0];
895         }
896         return 0;
897 }
898
899 /* Enable eeh for the given device node. */
900 static void *early_enable_eeh(struct device_node *dn, void *data)
901 {
902         unsigned int rets[3];
903         struct eeh_early_enable_info *info = data;
904         int ret;
905         const char *status = of_get_property(dn, "status", NULL);
906         const u32 *class_code = of_get_property(dn, "class-code", NULL);
907         const u32 *vendor_id = of_get_property(dn, "vendor-id", NULL);
908         const u32 *device_id = of_get_property(dn, "device-id", NULL);
909         const u32 *regs;
910         int enable;
911         struct pci_dn *pdn = PCI_DN(dn);
912
913         pdn->class_code = 0;
914         pdn->eeh_mode = 0;
915         pdn->eeh_check_count = 0;
916         pdn->eeh_freeze_count = 0;
917
918         if (status && strcmp(status, "ok") != 0)
919                 return NULL;    /* ignore devices with bad status */
920
921         /* Ignore bad nodes. */
922         if (!class_code || !vendor_id || !device_id)
923                 return NULL;
924
925         /* There is nothing to check on PCI to ISA bridges */
926         if (dn->type && !strcmp(dn->type, "isa")) {
927                 pdn->eeh_mode |= EEH_MODE_NOCHECK;
928                 return NULL;
929         }
930         pdn->class_code = *class_code;
931
932         /*
933          * Now decide if we are going to "Disable" EEH checking
934          * for this device.  We still run with the EEH hardware active,
935          * but we won't be checking for ff's.  This means a driver
936          * could return bad data (very bad!), an interrupt handler could
937          * hang waiting on status bits that won't change, etc.
938          * But there are a few cases like display devices that make sense.
939          */
940         enable = 1;     /* i.e. we will do checking */
941 #if 0
942         if ((*class_code >> 16) == PCI_BASE_CLASS_DISPLAY)
943                 enable = 0;
944 #endif
945
946         if (!enable)
947                 pdn->eeh_mode |= EEH_MODE_NOCHECK;
948
949         /* Ok... see if this device supports EEH.  Some do, some don't,
950          * and the only way to find out is to check each and every one. */
951         regs = of_get_property(dn, "reg", NULL);
952         if (regs) {
953                 /* First register entry is addr (00BBSS00)  */
954                 /* Try to enable eeh */
955                 ret = rtas_call(ibm_set_eeh_option, 4, 1, NULL,
956                                 regs[0], info->buid_hi, info->buid_lo,
957                                 EEH_ENABLE);
958
959                 enable = 0;
960                 if (ret == 0) {
961                         pdn->eeh_config_addr = regs[0];
962
963                         /* If the newer, better, ibm,get-config-addr-info is supported, 
964                          * then use that instead. */
965                         pdn->eeh_pe_config_addr = get_pe_addr(pdn->eeh_config_addr, info);
966
967                         /* Some older systems (Power4) allow the
968                          * ibm,set-eeh-option call to succeed even on nodes
969                          * where EEH is not supported. Verify support
970                          * explicitly. */
971                         ret = read_slot_reset_state(pdn, rets);
972                         if ((ret == 0) && (rets[1] == 1))
973                                 enable = 1;
974                 }
975
976                 if (enable) {
977                         eeh_subsystem_enabled = 1;
978                         pdn->eeh_mode |= EEH_MODE_SUPPORTED;
979
980 #ifdef DEBUG
981                         printk(KERN_DEBUG "EEH: %s: eeh enabled, config=%x pe_config=%x\n",
982                                dn->full_name, pdn->eeh_config_addr, pdn->eeh_pe_config_addr);
983 #endif
984                 } else {
985
986                         /* This device doesn't support EEH, but it may have an
987                          * EEH parent, in which case we mark it as supported. */
988                         if (dn->parent && PCI_DN(dn->parent)
989                             && (PCI_DN(dn->parent)->eeh_mode & EEH_MODE_SUPPORTED)) {
990                                 /* Parent supports EEH. */
991                                 pdn->eeh_mode |= EEH_MODE_SUPPORTED;
992                                 pdn->eeh_config_addr = PCI_DN(dn->parent)->eeh_config_addr;
993                                 return NULL;
994                         }
995                 }
996         } else {
997                 printk(KERN_WARNING "EEH: %s: unable to get reg property.\n",
998                        dn->full_name);
999         }
1000
1001         eeh_save_bars(pdn);
1002         return NULL;
1003 }
1004
1005 /*
1006  * Initialize EEH by trying to enable it for all of the adapters in the system.
1007  * As a side effect we can determine here if eeh is supported at all.
1008  * Note that we leave EEH on so failed config cycles won't cause a machine
1009  * check.  If a user turns off EEH for a particular adapter they are really
1010  * telling Linux to ignore errors.  Some hardware (e.g. POWER5) won't
1011  * grant access to a slot if EEH isn't enabled, and so we always enable
1012  * EEH for all slots/all devices.
1013  *
1014  * The eeh-force-off option disables EEH checking globally, for all slots.
1015  * Even if force-off is set, the EEH hardware is still enabled, so that
1016  * newer systems can boot.
1017  */
1018 void __init eeh_init(void)
1019 {
1020         struct device_node *phb, *np;
1021         struct eeh_early_enable_info info;
1022
1023         spin_lock_init(&confirm_error_lock);
1024         spin_lock_init(&slot_errbuf_lock);
1025
1026         np = of_find_node_by_path("/rtas");
1027         if (np == NULL)
1028                 return;
1029
1030         ibm_set_eeh_option = rtas_token("ibm,set-eeh-option");
1031         ibm_set_slot_reset = rtas_token("ibm,set-slot-reset");
1032         ibm_read_slot_reset_state2 = rtas_token("ibm,read-slot-reset-state2");
1033         ibm_read_slot_reset_state = rtas_token("ibm,read-slot-reset-state");
1034         ibm_slot_error_detail = rtas_token("ibm,slot-error-detail");
1035         ibm_get_config_addr_info = rtas_token("ibm,get-config-addr-info");
1036         ibm_get_config_addr_info2 = rtas_token("ibm,get-config-addr-info2");
1037         ibm_configure_bridge = rtas_token ("ibm,configure-bridge");
1038
1039         if (ibm_set_eeh_option == RTAS_UNKNOWN_SERVICE)
1040                 return;
1041
1042         eeh_error_buf_size = rtas_token("rtas-error-log-max");
1043         if (eeh_error_buf_size == RTAS_UNKNOWN_SERVICE) {
1044                 eeh_error_buf_size = 1024;
1045         }
1046         if (eeh_error_buf_size > RTAS_ERROR_LOG_MAX) {
1047                 printk(KERN_WARNING "EEH: rtas-error-log-max is bigger than allocated "
1048                       "buffer ! (%d vs %d)", eeh_error_buf_size, RTAS_ERROR_LOG_MAX);
1049                 eeh_error_buf_size = RTAS_ERROR_LOG_MAX;
1050         }
1051
1052         /* Enable EEH for all adapters.  Note that eeh requires buid's */
1053         for (phb = of_find_node_by_name(NULL, "pci"); phb;
1054              phb = of_find_node_by_name(phb, "pci")) {
1055                 unsigned long buid;
1056
1057                 buid = get_phb_buid(phb);
1058                 if (buid == 0 || PCI_DN(phb) == NULL)
1059                         continue;
1060
1061                 info.buid_lo = BUID_LO(buid);
1062                 info.buid_hi = BUID_HI(buid);
1063                 traverse_pci_devices(phb, early_enable_eeh, &info);
1064         }
1065
1066         if (eeh_subsystem_enabled)
1067                 printk(KERN_INFO "EEH: PCI Enhanced I/O Error Handling Enabled\n");
1068         else
1069                 printk(KERN_WARNING "EEH: No capable adapters found\n");
1070 }
1071
1072 /**
1073  * eeh_add_device_early - enable EEH for the indicated device_node
1074  * @dn: device node for which to set up EEH
1075  *
1076  * This routine must be used to perform EEH initialization for PCI
1077  * devices that were added after system boot (e.g. hotplug, dlpar).
1078  * This routine must be called before any i/o is performed to the
1079  * adapter (inluding any config-space i/o).
1080  * Whether this actually enables EEH or not for this device depends
1081  * on the CEC architecture, type of the device, on earlier boot
1082  * command-line arguments & etc.
1083  */
1084 static void eeh_add_device_early(struct device_node *dn)
1085 {
1086         struct pci_controller *phb;
1087         struct eeh_early_enable_info info;
1088
1089         if (!dn || !PCI_DN(dn))
1090                 return;
1091         phb = PCI_DN(dn)->phb;
1092
1093         /* USB Bus children of PCI devices will not have BUID's */
1094         if (NULL == phb || 0 == phb->buid)
1095                 return;
1096
1097         info.buid_hi = BUID_HI(phb->buid);
1098         info.buid_lo = BUID_LO(phb->buid);
1099         early_enable_eeh(dn, &info);
1100 }
1101
1102 void eeh_add_device_tree_early(struct device_node *dn)
1103 {
1104         struct device_node *sib;
1105         for (sib = dn->child; sib; sib = sib->sibling)
1106                 eeh_add_device_tree_early(sib);
1107         eeh_add_device_early(dn);
1108 }
1109 EXPORT_SYMBOL_GPL(eeh_add_device_tree_early);
1110
1111 /**
1112  * eeh_add_device_late - perform EEH initialization for the indicated pci device
1113  * @dev: pci device for which to set up EEH
1114  *
1115  * This routine must be used to complete EEH initialization for PCI
1116  * devices that were added after system boot (e.g. hotplug, dlpar).
1117  */
1118 static void eeh_add_device_late(struct pci_dev *dev)
1119 {
1120         struct device_node *dn;
1121         struct pci_dn *pdn;
1122
1123         if (!dev || !eeh_subsystem_enabled)
1124                 return;
1125
1126 #ifdef DEBUG
1127         printk(KERN_DEBUG "EEH: adding device %s\n", pci_name(dev));
1128 #endif
1129
1130         pci_dev_get (dev);
1131         dn = pci_device_to_OF_node(dev);
1132         pdn = PCI_DN(dn);
1133         pdn->pcidev = dev;
1134
1135         pci_addr_cache_insert_device (dev);
1136 }
1137
1138 void eeh_add_device_tree_late(struct pci_bus *bus)
1139 {
1140         struct pci_dev *dev;
1141
1142         list_for_each_entry(dev, &bus->devices, bus_list) {
1143                 eeh_add_device_late(dev);
1144                 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
1145                         struct pci_bus *subbus = dev->subordinate;
1146                         if (subbus)
1147                                 eeh_add_device_tree_late(subbus);
1148                 }
1149         }
1150 }
1151 EXPORT_SYMBOL_GPL(eeh_add_device_tree_late);
1152
1153 /**
1154  * eeh_remove_device - undo EEH setup for the indicated pci device
1155  * @dev: pci device to be removed
1156  *
1157  * This routine should be called when a device is removed from
1158  * a running system (e.g. by hotplug or dlpar).  It unregisters
1159  * the PCI device from the EEH subsystem.  I/O errors affecting
1160  * this device will no longer be detected after this call; thus,
1161  * i/o errors affecting this slot may leave this device unusable.
1162  */
1163 static void eeh_remove_device(struct pci_dev *dev)
1164 {
1165         struct device_node *dn;
1166         if (!dev || !eeh_subsystem_enabled)
1167                 return;
1168
1169         /* Unregister the device with the EEH/PCI address search system */
1170 #ifdef DEBUG
1171         printk(KERN_DEBUG "EEH: remove device %s\n", pci_name(dev));
1172 #endif
1173         pci_addr_cache_remove_device(dev);
1174
1175         dn = pci_device_to_OF_node(dev);
1176         if (PCI_DN(dn)->pcidev) {
1177                 PCI_DN(dn)->pcidev = NULL;
1178                 pci_dev_put (dev);
1179         }
1180 }
1181
1182 void eeh_remove_bus_device(struct pci_dev *dev)
1183 {
1184         struct pci_bus *bus = dev->subordinate;
1185         struct pci_dev *child, *tmp;
1186
1187         eeh_remove_device(dev);
1188
1189         if (bus && dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
1190                 list_for_each_entry_safe(child, tmp, &bus->devices, bus_list)
1191                          eeh_remove_bus_device(child);
1192         }
1193 }
1194 EXPORT_SYMBOL_GPL(eeh_remove_bus_device);
1195
1196 static int proc_eeh_show(struct seq_file *m, void *v)
1197 {
1198         if (0 == eeh_subsystem_enabled) {
1199                 seq_printf(m, "EEH Subsystem is globally disabled\n");
1200                 seq_printf(m, "eeh_total_mmio_ffs=%ld\n", total_mmio_ffs);
1201         } else {
1202                 seq_printf(m, "EEH Subsystem is enabled\n");
1203                 seq_printf(m,
1204                                 "no device=%ld\n"
1205                                 "no device node=%ld\n"
1206                                 "no config address=%ld\n"
1207                                 "check not wanted=%ld\n"
1208                                 "eeh_total_mmio_ffs=%ld\n"
1209                                 "eeh_false_positives=%ld\n"
1210                                 "eeh_ignored_failures=%ld\n"
1211                                 "eeh_slot_resets=%ld\n",
1212                                 no_device, no_dn, no_cfg_addr, 
1213                                 ignored_check, total_mmio_ffs, 
1214                                 false_positives, ignored_failures, 
1215                                 slot_resets);
1216         }
1217
1218         return 0;
1219 }
1220
1221 static int proc_eeh_open(struct inode *inode, struct file *file)
1222 {
1223         return single_open(file, proc_eeh_show, NULL);
1224 }
1225
1226 static const struct file_operations proc_eeh_operations = {
1227         .open      = proc_eeh_open,
1228         .read      = seq_read,
1229         .llseek    = seq_lseek,
1230         .release   = single_release,
1231 };
1232
1233 static int __init eeh_init_proc(void)
1234 {
1235         struct proc_dir_entry *e;
1236
1237         if (machine_is(pseries)) {
1238                 e = create_proc_entry("ppc64/eeh", 0, NULL);
1239                 if (e)
1240                         e->proc_fops = &proc_eeh_operations;
1241         }
1242
1243         return 0;
1244 }
1245 __initcall(eeh_init_proc);