blob: 57bef2c2f325f45bc1faa27b9c1acf055aa04452 [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
79/* RTAS tokens */
80static int ibm_set_eeh_option;
81static int ibm_set_slot_reset;
82static int ibm_read_slot_reset_state;
83static int ibm_read_slot_reset_state2;
84static int ibm_slot_error_detail;
85
David Woodhouse1e28a7d2005-11-17 00:44:03 +000086int eeh_subsystem_enabled;
87EXPORT_SYMBOL(eeh_subsystem_enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Linas Vepstasfd761fd2005-11-03 18:49:23 -060089/* Lock to avoid races due to multiple reports of an error */
90static DEFINE_SPINLOCK(confirm_error_lock);
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/* Buffer for reporting slot-error-detail rtas calls */
93static unsigned char slot_errbuf[RTAS_ERROR_LOG_MAX];
94static DEFINE_SPINLOCK(slot_errbuf_lock);
95static int eeh_error_buf_size;
96
97/* System monitoring statistics */
Linas Vepstas177bc932005-11-03 18:48:52 -060098static DEFINE_PER_CPU(unsigned long, no_device);
99static DEFINE_PER_CPU(unsigned long, no_dn);
100static DEFINE_PER_CPU(unsigned long, no_cfg_addr);
101static DEFINE_PER_CPU(unsigned long, ignored_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static DEFINE_PER_CPU(unsigned long, total_mmio_ffs);
103static DEFINE_PER_CPU(unsigned long, false_positives);
104static DEFINE_PER_CPU(unsigned long, ignored_failures);
105static DEFINE_PER_CPU(unsigned long, slot_resets);
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107/* --------------------------------------------------------------- */
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600108/* Below lies the EEH event infrastructure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Linas Vepstasdf7242b2005-11-03 18:49:01 -0600110void eeh_slot_error_detail (struct pci_dn *pdn, int severity)
111{
112 unsigned long flags;
113 int rc;
114
115 /* Log the error with the rtas logger */
116 spin_lock_irqsave(&slot_errbuf_lock, flags);
117 memset(slot_errbuf, 0, eeh_error_buf_size);
118
119 rc = rtas_call(ibm_slot_error_detail,
120 8, 1, NULL, pdn->eeh_config_addr,
121 BUID_HI(pdn->phb->buid),
122 BUID_LO(pdn->phb->buid), NULL, 0,
123 virt_to_phys(slot_errbuf),
124 eeh_error_buf_size,
125 severity);
126
127 if (rc == 0)
128 log_error(slot_errbuf, ERR_TYPE_RTAS_LOG, 0);
129 spin_unlock_irqrestore(&slot_errbuf_lock, flags);
130}
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 * read_slot_reset_state - Read the reset state of a device node's slot
134 * @dn: device node to read
135 * @rets: array to return results in
136 */
Linas Vepstas69376502005-11-03 18:47:50 -0600137static int read_slot_reset_state(struct pci_dn *pdn, int rets[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 int token, outputs;
140
141 if (ibm_read_slot_reset_state2 != RTAS_UNKNOWN_SERVICE) {
142 token = ibm_read_slot_reset_state2;
143 outputs = 4;
144 } else {
145 token = ibm_read_slot_reset_state;
Linas Vepstas69376502005-11-03 18:47:50 -0600146 rets[2] = 0; /* fake PE Unavailable info */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 outputs = 3;
148 }
149
Paul Mackerras16353172005-09-06 13:17:54 +1000150 return rtas_call(token, 3, outputs, rets, pdn->eeh_config_addr,
151 BUID_HI(pdn->phb->buid), BUID_LO(pdn->phb->buid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
154/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 * eeh_token_to_phys - convert EEH address token to phys address
Linas Vepstas69376502005-11-03 18:47:50 -0600156 * @token i/o token, should be address in the form 0xA....
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 */
158static inline unsigned long eeh_token_to_phys(unsigned long token)
159{
160 pte_t *ptep;
161 unsigned long pa;
162
David Gibson20cee162005-06-21 17:15:31 -0700163 ptep = find_linux_pte(init_mm.pgd, token);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (!ptep)
165 return token;
166 pa = pte_pfn(*ptep) << PAGE_SHIFT;
167
168 return pa | (token & (PAGE_SIZE-1));
169}
170
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600171/**
172 * Return the "partitionable endpoint" (pe) under which this device lies
173 */
174static struct device_node * find_device_pe(struct device_node *dn)
175{
176 while ((dn->parent) && PCI_DN(dn->parent) &&
177 (PCI_DN(dn->parent)->eeh_mode & EEH_MODE_SUPPORTED)) {
178 dn = dn->parent;
179 }
180 return dn;
181}
182
183/** Mark all devices that are peers of this device as failed.
184 * Mark the device driver too, so that it can see the failure
185 * immediately; this is critical, since some drivers poll
186 * status registers in interrupts ... If a driver is polling,
187 * and the slot is frozen, then the driver can deadlock in
188 * an interrupt context, which is bad.
189 */
190
Linas Vepstasd9564ad2005-11-03 18:50:48 -0600191static void __eeh_mark_slot (struct device_node *dn, int mode_flag)
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600192{
193 while (dn) {
Linas Vepstasd9564ad2005-11-03 18:50:48 -0600194 if (PCI_DN(dn)) {
195 PCI_DN(dn)->eeh_mode |= mode_flag;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600196
Linas Vepstas77bd7412005-11-03 18:52:49 -0600197 /* Mark the pci device driver too */
198 struct pci_dev *dev = PCI_DN(dn)->pcidev;
199 if (dev && dev->driver)
200 dev->error_state = pci_channel_io_frozen;
201
Linas Vepstasd9564ad2005-11-03 18:50:48 -0600202 if (dn->child)
203 __eeh_mark_slot (dn->child, mode_flag);
204 }
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600205 dn = dn->sibling;
206 }
207}
208
Linas Vepstasd9564ad2005-11-03 18:50:48 -0600209void eeh_mark_slot (struct device_node *dn, int mode_flag)
210{
211 dn = find_device_pe (dn);
212 PCI_DN(dn)->eeh_mode |= mode_flag;
213 __eeh_mark_slot (dn->child, mode_flag);
214}
215
216static void __eeh_clear_slot (struct device_node *dn, int mode_flag)
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600217{
218 while (dn) {
Linas Vepstasd9564ad2005-11-03 18:50:48 -0600219 if (PCI_DN(dn)) {
220 PCI_DN(dn)->eeh_mode &= ~mode_flag;
221 PCI_DN(dn)->eeh_check_count = 0;
222 if (dn->child)
223 __eeh_clear_slot (dn->child, mode_flag);
224 }
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600225 dn = dn->sibling;
226 }
227}
228
Linas Vepstasd9564ad2005-11-03 18:50:48 -0600229void eeh_clear_slot (struct device_node *dn, int mode_flag)
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600230{
231 unsigned long flags;
232 spin_lock_irqsave(&confirm_error_lock, flags);
Linas Vepstasd9564ad2005-11-03 18:50:48 -0600233 dn = find_device_pe (dn);
234 PCI_DN(dn)->eeh_mode &= ~mode_flag;
235 PCI_DN(dn)->eeh_check_count = 0;
236 __eeh_clear_slot (dn->child, mode_flag);
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600237 spin_unlock_irqrestore(&confirm_error_lock, flags);
238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/**
241 * eeh_dn_check_failure - check if all 1's data is due to EEH slot freeze
242 * @dn device node
243 * @dev pci device, if known
244 *
245 * Check for an EEH failure for the given device node. Call this
246 * routine if the result of a read was all 0xff's and you want to
247 * find out if this is due to an EEH slot freeze. This routine
248 * will query firmware for the EEH status.
249 *
250 * Returns 0 if there has not been an EEH error; otherwise returns
Linas Vepstas69376502005-11-03 18:47:50 -0600251 * a non-zero value and queues up a slot isolation event notification.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 *
253 * It is safe to call this routine in an interrupt context.
254 */
255int eeh_dn_check_failure(struct device_node *dn, struct pci_dev *dev)
256{
257 int ret;
258 int rets[3];
259 unsigned long flags;
Paul Mackerras16353172005-09-06 13:17:54 +1000260 struct pci_dn *pdn;
Linas Vepstas77bd7412005-11-03 18:52:49 -0600261 enum pci_channel_state state;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600262 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 __get_cpu_var(total_mmio_ffs)++;
265
266 if (!eeh_subsystem_enabled)
267 return 0;
268
Linas Vepstas177bc932005-11-03 18:48:52 -0600269 if (!dn) {
270 __get_cpu_var(no_dn)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return 0;
Linas Vepstas177bc932005-11-03 18:48:52 -0600272 }
Linas Vepstas69376502005-11-03 18:47:50 -0600273 pdn = PCI_DN(dn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 /* Access to IO BARs might get this far and still not want checking. */
Linas Vepstasf8632c82005-11-03 18:49:45 -0600276 if (!(pdn->eeh_mode & EEH_MODE_SUPPORTED) ||
Paul Mackerras16353172005-09-06 13:17:54 +1000277 pdn->eeh_mode & EEH_MODE_NOCHECK) {
Linas Vepstas177bc932005-11-03 18:48:52 -0600278 __get_cpu_var(ignored_check)++;
279#ifdef DEBUG
Linas Vepstasf8632c82005-11-03 18:49:45 -0600280 printk ("EEH:ignored check (%x) for %s %s\n",
281 pdn->eeh_mode, pci_name (dev), dn->full_name);
Linas Vepstas177bc932005-11-03 18:48:52 -0600282#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return 0;
284 }
285
Paul Mackerras16353172005-09-06 13:17:54 +1000286 if (!pdn->eeh_config_addr) {
Linas Vepstas177bc932005-11-03 18:48:52 -0600287 __get_cpu_var(no_cfg_addr)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return 0;
289 }
290
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600291 /* If we already have a pending isolation event for this
292 * slot, we know it's bad already, we don't need to check.
293 * Do this checking under a lock; as multiple PCI devices
294 * in one slot might report errors simultaneously, and we
295 * only want one error recovery routine running.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 */
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600297 spin_lock_irqsave(&confirm_error_lock, flags);
298 rc = 1;
Paul Mackerras16353172005-09-06 13:17:54 +1000299 if (pdn->eeh_mode & EEH_MODE_ISOLATED) {
Linas Vepstas5c1344e2005-11-03 18:49:31 -0600300 pdn->eeh_check_count ++;
301 if (pdn->eeh_check_count >= EEH_MAX_FAILS) {
302 printk (KERN_ERR "EEH: Device driver ignored %d bad reads, panicing\n",
303 pdn->eeh_check_count);
304 dump_stack();
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 /* re-read the slot reset state */
Linas Vepstas69376502005-11-03 18:47:50 -0600307 if (read_slot_reset_state(pdn, rets) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 rets[0] = -1; /* reset state unknown */
Linas Vepstas5c1344e2005-11-03 18:49:31 -0600309
310 /* If we are here, then we hit an infinite loop. Stop. */
311 panic("EEH: MMIO halt (%d) on device:%s\n", rets[0], pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600313 goto dn_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
315
316 /*
317 * Now test for an EEH failure. This is VERY expensive.
318 * Note that the eeh_config_addr may be a parent device
319 * in the case of a device behind a bridge, or it may be
320 * function zero of a multi-function device.
321 * In any case they must share a common PHB.
322 */
Linas Vepstas69376502005-11-03 18:47:50 -0600323 ret = read_slot_reset_state(pdn, rets);
Linas Vepstas76e6faf2005-11-03 18:49:15 -0600324
325 /* If the call to firmware failed, punt */
326 if (ret != 0) {
327 printk(KERN_WARNING "EEH: read_slot_reset_state() failed; rc=%d dn=%s\n",
328 ret, dn->full_name);
329 __get_cpu_var(false_positives)++;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600330 rc = 0;
331 goto dn_unlock;
Linas Vepstas76e6faf2005-11-03 18:49:15 -0600332 }
333
334 /* If EEH is not supported on this device, punt. */
335 if (rets[1] != 1) {
336 printk(KERN_WARNING "EEH: event on unsupported device, rc=%d dn=%s\n",
337 ret, dn->full_name);
338 __get_cpu_var(false_positives)++;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600339 rc = 0;
340 goto dn_unlock;
Linas Vepstas76e6faf2005-11-03 18:49:15 -0600341 }
342
343 /* If not the kind of error we know about, punt. */
344 if (rets[0] != 2 && rets[0] != 4 && rets[0] != 5) {
345 __get_cpu_var(false_positives)++;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600346 rc = 0;
347 goto dn_unlock;
Linas Vepstas76e6faf2005-11-03 18:49:15 -0600348 }
349
350 /* Note that config-io to empty slots may fail;
351 * we recognize empty because they don't have children. */
352 if ((rets[0] == 5) && (dn->child == NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 __get_cpu_var(false_positives)++;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600354 rc = 0;
355 goto dn_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
357
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600358 __get_cpu_var(slot_resets)++;
359
360 /* Avoid repeated reports of this failure, including problems
361 * with other functions on this device, and functions under
362 * bridges. */
Linas Vepstasd9564ad2005-11-03 18:50:48 -0600363 eeh_mark_slot (dn, EEH_MODE_ISOLATED);
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600364 spin_unlock_irqrestore(&confirm_error_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Linas Vepstas77bd7412005-11-03 18:52:49 -0600366 state = pci_channel_io_normal;
367 if ((rets[0] == 2) || (rets[0] == 4))
368 state = pci_channel_io_frozen;
369 if (rets[0] == 5)
370 state = pci_channel_io_perm_failure;
371 eeh_send_failure_event (dn, dev, state, rets[2]);
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 /* Most EEH events are due to device driver bugs. Having
374 * a stack trace will help the device-driver authors figure
375 * out what happened. So print that out. */
Linas Vepstas76e6faf2005-11-03 18:49:15 -0600376 if (rets[0] != 5) dump_stack();
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600377 return 1;
378
379dn_unlock:
380 spin_unlock_irqrestore(&confirm_error_lock, flags);
381 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600384EXPORT_SYMBOL_GPL(eeh_dn_check_failure);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386/**
387 * eeh_check_failure - check if all 1's data is due to EEH slot freeze
388 * @token i/o token, should be address in the form 0xA....
389 * @val value, should be all 1's (XXX why do we need this arg??)
390 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 * Check for an EEH failure at the given token address. Call this
392 * routine if the result of a read was all 0xff's and you want to
393 * find out if this is due to an EEH slot freeze event. This routine
394 * will query firmware for the EEH status.
395 *
396 * Note this routine is safe to call in an interrupt context.
397 */
398unsigned long eeh_check_failure(const volatile void __iomem *token, unsigned long val)
399{
400 unsigned long addr;
401 struct pci_dev *dev;
402 struct device_node *dn;
403
404 /* Finding the phys addr + pci device; this is pretty quick. */
405 addr = eeh_token_to_phys((unsigned long __force) token);
406 dev = pci_get_device_by_addr(addr);
Linas Vepstas177bc932005-11-03 18:48:52 -0600407 if (!dev) {
408 __get_cpu_var(no_device)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return val;
Linas Vepstas177bc932005-11-03 18:48:52 -0600410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 dn = pci_device_to_OF_node(dev);
413 eeh_dn_check_failure (dn, dev);
414
415 pci_dev_put(dev);
416 return val;
417}
418
419EXPORT_SYMBOL(eeh_check_failure);
420
Linas Vepstas172ca922005-11-03 18:50:04 -0600421/* ------------------------------------------------------------- */
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600422/* The code below deals with error recovery */
423
424/** Return negative value if a permanent error, else return
425 * a number of milliseconds to wait until the PCI slot is
426 * ready to be used.
427 */
428static int
429eeh_slot_availability(struct pci_dn *pdn)
430{
431 int rc;
432 int rets[3];
433
434 rc = read_slot_reset_state(pdn, rets);
435
436 if (rc) return rc;
437
438 if (rets[1] == 0) return -1; /* EEH is not supported */
439 if (rets[0] == 0) return 0; /* Oll Korrect */
440 if (rets[0] == 5) {
441 if (rets[2] == 0) return -1; /* permanently unavailable */
442 return rets[2]; /* number of millisecs to wait */
443 }
444 return -1;
445}
446
447/** rtas_pci_slot_reset raises/lowers the pci #RST line
448 * state: 1/0 to raise/lower the #RST
449 *
450 * Clear the EEH-frozen condition on a slot. This routine
451 * asserts the PCI #RST line if the 'state' argument is '1',
452 * and drops the #RST line if 'state is '0'. This routine is
453 * safe to call in an interrupt context.
454 *
455 */
456
457static void
458rtas_pci_slot_reset(struct pci_dn *pdn, int state)
459{
460 int rc;
461
462 BUG_ON (pdn==NULL);
463
464 if (!pdn->phb) {
465 printk (KERN_WARNING "EEH: in slot reset, device node %s has no phb\n",
466 pdn->node->full_name);
467 return;
468 }
469
470 rc = rtas_call(ibm_set_slot_reset,4,1, NULL,
471 pdn->eeh_config_addr,
472 BUID_HI(pdn->phb->buid),
473 BUID_LO(pdn->phb->buid),
474 state);
475 if (rc) {
476 printk (KERN_WARNING "EEH: Unable to reset the failed slot, (%d) #RST=%d dn=%s\n",
477 rc, state, pdn->node->full_name);
478 return;
479 }
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600480}
481
482/** rtas_set_slot_reset -- assert the pci #RST line for 1/4 second
483 * dn -- device node to be reset.
484 */
485
486void
487rtas_set_slot_reset(struct pci_dn *pdn)
488{
489 int i, rc;
490
491 rtas_pci_slot_reset (pdn, 1);
492
493 /* The PCI bus requires that the reset be held high for at least
494 * a 100 milliseconds. We wait a bit longer 'just in case'. */
495
496#define PCI_BUS_RST_HOLD_TIME_MSEC 250
497 msleep (PCI_BUS_RST_HOLD_TIME_MSEC);
Linas Vepstasd9564ad2005-11-03 18:50:48 -0600498
499 /* We might get hit with another EEH freeze as soon as the
500 * pci slot reset line is dropped. Make sure we don't miss
501 * these, and clear the flag now. */
502 eeh_clear_slot (pdn->node, EEH_MODE_ISOLATED);
503
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600504 rtas_pci_slot_reset (pdn, 0);
505
506 /* After a PCI slot has been reset, the PCI Express spec requires
507 * a 1.5 second idle time for the bus to stabilize, before starting
508 * up traffic. */
509#define PCI_BUS_SETTLE_TIME_MSEC 1800
510 msleep (PCI_BUS_SETTLE_TIME_MSEC);
511
512 /* Now double check with the firmware to make sure the device is
513 * ready to be used; if not, wait for recovery. */
514 for (i=0; i<10; i++) {
515 rc = eeh_slot_availability (pdn);
516 if (rc <= 0) break;
517
518 msleep (rc+100);
519 }
520}
521
Linas Vepstas8b553f32005-11-03 18:50:17 -0600522/* ------------------------------------------------------- */
523/** Save and restore of PCI BARs
524 *
525 * Although firmware will set up BARs during boot, it doesn't
526 * set up device BAR's after a device reset, although it will,
527 * if requested, set up bridge configuration. Thus, we need to
528 * configure the PCI devices ourselves.
529 */
530
531/**
532 * __restore_bars - Restore the Base Address Registers
533 * Loads the PCI configuration space base address registers,
534 * the expansion ROM base address, the latency timer, and etc.
535 * from the saved values in the device node.
536 */
537static inline void __restore_bars (struct pci_dn *pdn)
538{
539 int i;
540
541 if (NULL==pdn->phb) return;
542 for (i=4; i<10; i++) {
543 rtas_write_config(pdn, i*4, 4, pdn->config_space[i]);
544 }
545
546 /* 12 == Expansion ROM Address */
547 rtas_write_config(pdn, 12*4, 4, pdn->config_space[12]);
548
549#define BYTE_SWAP(OFF) (8*((OFF)/4)+3-(OFF))
550#define SAVED_BYTE(OFF) (((u8 *)(pdn->config_space))[BYTE_SWAP(OFF)])
551
552 rtas_write_config (pdn, PCI_CACHE_LINE_SIZE, 1,
553 SAVED_BYTE(PCI_CACHE_LINE_SIZE));
554
555 rtas_write_config (pdn, PCI_LATENCY_TIMER, 1,
556 SAVED_BYTE(PCI_LATENCY_TIMER));
557
558 /* max latency, min grant, interrupt pin and line */
559 rtas_write_config(pdn, 15*4, 4, pdn->config_space[15]);
560}
561
562/**
563 * eeh_restore_bars - restore the PCI config space info
564 *
565 * This routine performs a recursive walk to the children
566 * of this device as well.
567 */
568void eeh_restore_bars(struct pci_dn *pdn)
569{
570 struct device_node *dn;
571 if (!pdn)
572 return;
573
574 if (! pdn->eeh_is_bridge)
575 __restore_bars (pdn);
576
577 dn = pdn->node->child;
578 while (dn) {
579 eeh_restore_bars (PCI_DN(dn));
580 dn = dn->sibling;
581 }
582}
583
584/**
585 * eeh_save_bars - save device bars
586 *
587 * Save the values of the device bars. Unlike the restore
588 * routine, this routine is *not* recursive. This is because
589 * PCI devices are added individuallly; but, for the restore,
590 * an entire slot is reset at a time.
591 */
Linas Vepstas5d5a0932005-11-03 18:53:07 -0600592void eeh_save_bars(struct pci_dev * pdev, struct pci_dn *pdn)
Linas Vepstas8b553f32005-11-03 18:50:17 -0600593{
594 int i;
595
596 if (!pdev || !pdn )
597 return;
598
599 for (i = 0; i < 16; i++)
600 pci_read_config_dword(pdev, i * 4, &pdn->config_space[i]);
601
602 if (pdev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
603 pdn->eeh_is_bridge = 1;
604}
605
606void
607rtas_configure_bridge(struct pci_dn *pdn)
608{
609 int token = rtas_token ("ibm,configure-bridge");
610 int rc;
611
612 if (token == RTAS_UNKNOWN_SERVICE)
613 return;
614 rc = rtas_call(token,3,1, NULL,
615 pdn->eeh_config_addr,
616 BUID_HI(pdn->phb->buid),
617 BUID_LO(pdn->phb->buid));
618 if (rc) {
619 printk (KERN_WARNING "EEH: Unable to configure device bridge (%d) for %s\n",
620 rc, pdn->node->full_name);
621 }
622}
623
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600624/* ------------------------------------------------------------- */
Linas Vepstas172ca922005-11-03 18:50:04 -0600625/* The code below deals with enabling EEH for devices during the
626 * early boot sequence. EEH must be enabled before any PCI probing
627 * can be done.
628 */
629
630#define EEH_ENABLE 1
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632struct eeh_early_enable_info {
633 unsigned int buid_hi;
634 unsigned int buid_lo;
635};
636
637/* Enable eeh for the given device node. */
638static void *early_enable_eeh(struct device_node *dn, void *data)
639{
640 struct eeh_early_enable_info *info = data;
641 int ret;
642 char *status = get_property(dn, "status", NULL);
643 u32 *class_code = (u32 *)get_property(dn, "class-code", NULL);
644 u32 *vendor_id = (u32 *)get_property(dn, "vendor-id", NULL);
645 u32 *device_id = (u32 *)get_property(dn, "device-id", NULL);
646 u32 *regs;
647 int enable;
Linas Vepstas69376502005-11-03 18:47:50 -0600648 struct pci_dn *pdn = PCI_DN(dn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Paul Mackerras16353172005-09-06 13:17:54 +1000650 pdn->eeh_mode = 0;
Linas Vepstas5c1344e2005-11-03 18:49:31 -0600651 pdn->eeh_check_count = 0;
652 pdn->eeh_freeze_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 if (status && strcmp(status, "ok") != 0)
655 return NULL; /* ignore devices with bad status */
656
657 /* Ignore bad nodes. */
658 if (!class_code || !vendor_id || !device_id)
659 return NULL;
660
661 /* There is nothing to check on PCI to ISA bridges */
662 if (dn->type && !strcmp(dn->type, "isa")) {
Paul Mackerras16353172005-09-06 13:17:54 +1000663 pdn->eeh_mode |= EEH_MODE_NOCHECK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return NULL;
665 }
666
667 /*
668 * Now decide if we are going to "Disable" EEH checking
669 * for this device. We still run with the EEH hardware active,
670 * but we won't be checking for ff's. This means a driver
671 * could return bad data (very bad!), an interrupt handler could
672 * hang waiting on status bits that won't change, etc.
673 * But there are a few cases like display devices that make sense.
674 */
675 enable = 1; /* i.e. we will do checking */
Linas Vepstas77bd7412005-11-03 18:52:49 -0600676#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if ((*class_code >> 16) == PCI_BASE_CLASS_DISPLAY)
678 enable = 0;
Linas Vepstas77bd7412005-11-03 18:52:49 -0600679#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 if (!enable)
Paul Mackerras16353172005-09-06 13:17:54 +1000682 pdn->eeh_mode |= EEH_MODE_NOCHECK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 /* Ok... see if this device supports EEH. Some do, some don't,
685 * and the only way to find out is to check each and every one. */
686 regs = (u32 *)get_property(dn, "reg", NULL);
687 if (regs) {
688 /* First register entry is addr (00BBSS00) */
689 /* Try to enable eeh */
690 ret = rtas_call(ibm_set_eeh_option, 4, 1, NULL,
Linas Vepstas172ca922005-11-03 18:50:04 -0600691 regs[0], info->buid_hi, info->buid_lo,
692 EEH_ENABLE);
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (ret == 0) {
695 eeh_subsystem_enabled = 1;
Paul Mackerras16353172005-09-06 13:17:54 +1000696 pdn->eeh_mode |= EEH_MODE_SUPPORTED;
697 pdn->eeh_config_addr = regs[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698#ifdef DEBUG
699 printk(KERN_DEBUG "EEH: %s: eeh enabled\n", dn->full_name);
700#endif
701 } else {
702
703 /* This device doesn't support EEH, but it may have an
704 * EEH parent, in which case we mark it as supported. */
Linas Vepstas69376502005-11-03 18:47:50 -0600705 if (dn->parent && PCI_DN(dn->parent)
Paul Mackerras16353172005-09-06 13:17:54 +1000706 && (PCI_DN(dn->parent)->eeh_mode & EEH_MODE_SUPPORTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 /* Parent supports EEH. */
Paul Mackerras16353172005-09-06 13:17:54 +1000708 pdn->eeh_mode |= EEH_MODE_SUPPORTED;
709 pdn->eeh_config_addr = PCI_DN(dn->parent)->eeh_config_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return NULL;
711 }
712 }
713 } else {
714 printk(KERN_WARNING "EEH: %s: unable to get reg property.\n",
715 dn->full_name);
716 }
717
Linas Vepstas69376502005-11-03 18:47:50 -0600718 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
721/*
722 * Initialize EEH by trying to enable it for all of the adapters in the system.
723 * As a side effect we can determine here if eeh is supported at all.
724 * Note that we leave EEH on so failed config cycles won't cause a machine
725 * check. If a user turns off EEH for a particular adapter they are really
726 * telling Linux to ignore errors. Some hardware (e.g. POWER5) won't
727 * grant access to a slot if EEH isn't enabled, and so we always enable
728 * EEH for all slots/all devices.
729 *
730 * The eeh-force-off option disables EEH checking globally, for all slots.
731 * Even if force-off is set, the EEH hardware is still enabled, so that
732 * newer systems can boot.
733 */
734void __init eeh_init(void)
735{
736 struct device_node *phb, *np;
737 struct eeh_early_enable_info info;
738
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600739 spin_lock_init(&confirm_error_lock);
Linas Vepstasdf7242b2005-11-03 18:49:01 -0600740 spin_lock_init(&slot_errbuf_lock);
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 np = of_find_node_by_path("/rtas");
743 if (np == NULL)
744 return;
745
746 ibm_set_eeh_option = rtas_token("ibm,set-eeh-option");
747 ibm_set_slot_reset = rtas_token("ibm,set-slot-reset");
748 ibm_read_slot_reset_state2 = rtas_token("ibm,read-slot-reset-state2");
749 ibm_read_slot_reset_state = rtas_token("ibm,read-slot-reset-state");
750 ibm_slot_error_detail = rtas_token("ibm,slot-error-detail");
751
752 if (ibm_set_eeh_option == RTAS_UNKNOWN_SERVICE)
753 return;
754
755 eeh_error_buf_size = rtas_token("rtas-error-log-max");
756 if (eeh_error_buf_size == RTAS_UNKNOWN_SERVICE) {
757 eeh_error_buf_size = 1024;
758 }
759 if (eeh_error_buf_size > RTAS_ERROR_LOG_MAX) {
760 printk(KERN_WARNING "EEH: rtas-error-log-max is bigger than allocated "
761 "buffer ! (%d vs %d)", eeh_error_buf_size, RTAS_ERROR_LOG_MAX);
762 eeh_error_buf_size = RTAS_ERROR_LOG_MAX;
763 }
764
765 /* Enable EEH for all adapters. Note that eeh requires buid's */
766 for (phb = of_find_node_by_name(NULL, "pci"); phb;
767 phb = of_find_node_by_name(phb, "pci")) {
768 unsigned long buid;
769
770 buid = get_phb_buid(phb);
Linas Vepstas69376502005-11-03 18:47:50 -0600771 if (buid == 0 || PCI_DN(phb) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 continue;
773
774 info.buid_lo = BUID_LO(buid);
775 info.buid_hi = BUID_HI(buid);
776 traverse_pci_devices(phb, early_enable_eeh, &info);
777 }
778
779 if (eeh_subsystem_enabled)
780 printk(KERN_INFO "EEH: PCI Enhanced I/O Error Handling Enabled\n");
781 else
782 printk(KERN_WARNING "EEH: No capable adapters found\n");
783}
784
785/**
786 * eeh_add_device_early - enable EEH for the indicated device_node
787 * @dn: device node for which to set up EEH
788 *
789 * This routine must be used to perform EEH initialization for PCI
790 * devices that were added after system boot (e.g. hotplug, dlpar).
791 * This routine must be called before any i/o is performed to the
792 * adapter (inluding any config-space i/o).
793 * Whether this actually enables EEH or not for this device depends
794 * on the CEC architecture, type of the device, on earlier boot
795 * command-line arguments & etc.
796 */
797void eeh_add_device_early(struct device_node *dn)
798{
799 struct pci_controller *phb;
800 struct eeh_early_enable_info info;
801
Linas Vepstas69376502005-11-03 18:47:50 -0600802 if (!dn || !PCI_DN(dn))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return;
Paul Mackerras16353172005-09-06 13:17:54 +1000804 phb = PCI_DN(dn)->phb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (NULL == phb || 0 == phb->buid) {
Linas Vepstas69376502005-11-03 18:47:50 -0600806 printk(KERN_WARNING "EEH: Expected buid but found none for %s\n",
807 dn->full_name);
808 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return;
810 }
811
812 info.buid_hi = BUID_HI(phb->buid);
813 info.buid_lo = BUID_LO(phb->buid);
814 early_enable_eeh(dn, &info);
815}
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600816EXPORT_SYMBOL_GPL(eeh_add_device_early);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Linas Vepstase2a296e2005-11-03 18:51:31 -0600818void eeh_add_device_tree_early(struct device_node *dn)
819{
820 struct device_node *sib;
821 for (sib = dn->child; sib; sib = sib->sibling)
822 eeh_add_device_tree_early(sib);
823 eeh_add_device_early(dn);
824}
825EXPORT_SYMBOL_GPL(eeh_add_device_tree_early);
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827/**
828 * eeh_add_device_late - perform EEH initialization for the indicated pci device
829 * @dev: pci device for which to set up EEH
830 *
831 * This routine must be used to complete EEH initialization for PCI
832 * devices that were added after system boot (e.g. hotplug, dlpar).
833 */
834void eeh_add_device_late(struct pci_dev *dev)
835{
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600836 struct device_node *dn;
Linas Vepstas8b553f32005-11-03 18:50:17 -0600837 struct pci_dn *pdn;
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if (!dev || !eeh_subsystem_enabled)
840 return;
841
842#ifdef DEBUG
Adrian Bunk982245f2005-07-17 04:22:20 +0200843 printk(KERN_DEBUG "EEH: adding device %s\n", pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844#endif
845
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600846 pci_dev_get (dev);
847 dn = pci_device_to_OF_node(dev);
Linas Vepstas8b553f32005-11-03 18:50:17 -0600848 pdn = PCI_DN(dn);
849 pdn->pcidev = dev;
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 pci_addr_cache_insert_device (dev);
Linas Vepstas8b553f32005-11-03 18:50:17 -0600852 eeh_save_bars(dev, pdn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853}
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600854EXPORT_SYMBOL_GPL(eeh_add_device_late);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856/**
857 * eeh_remove_device - undo EEH setup for the indicated pci device
858 * @dev: pci device to be removed
859 *
860 * This routine should be when a device is removed from a running
861 * system (e.g. by hotplug or dlpar).
862 */
863void eeh_remove_device(struct pci_dev *dev)
864{
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600865 struct device_node *dn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (!dev || !eeh_subsystem_enabled)
867 return;
868
869 /* Unregister the device with the EEH/PCI address search system */
870#ifdef DEBUG
Adrian Bunk982245f2005-07-17 04:22:20 +0200871 printk(KERN_DEBUG "EEH: remove device %s\n", pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872#endif
873 pci_addr_cache_remove_device(dev);
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600874
875 dn = pci_device_to_OF_node(dev);
876 PCI_DN(dn)->pcidev = NULL;
877 pci_dev_put (dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
Linas Vepstas56b0fca2005-11-03 18:48:45 -0600879EXPORT_SYMBOL_GPL(eeh_remove_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Linas Vepstase2a296e2005-11-03 18:51:31 -0600881void eeh_remove_bus_device(struct pci_dev *dev)
882{
883 eeh_remove_device(dev);
884 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
885 struct pci_bus *bus = dev->subordinate;
886 struct list_head *ln;
887 if (!bus)
888 return;
889 for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
890 struct pci_dev *pdev = pci_dev_b(ln);
891 if (pdev)
892 eeh_remove_bus_device(pdev);
893 }
894 }
895}
896EXPORT_SYMBOL_GPL(eeh_remove_bus_device);
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898static int proc_eeh_show(struct seq_file *m, void *v)
899{
900 unsigned int cpu;
901 unsigned long ffs = 0, positives = 0, failures = 0;
902 unsigned long resets = 0;
Linas Vepstas177bc932005-11-03 18:48:52 -0600903 unsigned long no_dev = 0, no_dn = 0, no_cfg = 0, no_check = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 for_each_cpu(cpu) {
906 ffs += per_cpu(total_mmio_ffs, cpu);
907 positives += per_cpu(false_positives, cpu);
908 failures += per_cpu(ignored_failures, cpu);
909 resets += per_cpu(slot_resets, cpu);
Linas Vepstas177bc932005-11-03 18:48:52 -0600910 no_dev += per_cpu(no_device, cpu);
911 no_dn += per_cpu(no_dn, cpu);
912 no_cfg += per_cpu(no_cfg_addr, cpu);
913 no_check += per_cpu(ignored_check, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
915
916 if (0 == eeh_subsystem_enabled) {
917 seq_printf(m, "EEH Subsystem is globally disabled\n");
918 seq_printf(m, "eeh_total_mmio_ffs=%ld\n", ffs);
919 } else {
920 seq_printf(m, "EEH Subsystem is enabled\n");
Linas Vepstas177bc932005-11-03 18:48:52 -0600921 seq_printf(m,
922 "no device=%ld\n"
923 "no device node=%ld\n"
924 "no config address=%ld\n"
925 "check not wanted=%ld\n"
926 "eeh_total_mmio_ffs=%ld\n"
927 "eeh_false_positives=%ld\n"
928 "eeh_ignored_failures=%ld\n"
929 "eeh_slot_resets=%ld\n",
930 no_dev, no_dn, no_cfg, no_check,
931 ffs, positives, failures, resets);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
933
934 return 0;
935}
936
937static int proc_eeh_open(struct inode *inode, struct file *file)
938{
939 return single_open(file, proc_eeh_show, NULL);
940}
941
942static struct file_operations proc_eeh_operations = {
943 .open = proc_eeh_open,
944 .read = seq_read,
945 .llseek = seq_lseek,
946 .release = single_release,
947};
948
949static int __init eeh_init_proc(void)
950{
951 struct proc_dir_entry *e;
952
Paul Mackerras799d6042005-11-10 13:37:51 +1100953 if (platform_is_pseries()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 e = create_proc_entry("ppc64/eeh", 0, NULL);
955 if (e)
956 e->proc_fops = &proc_eeh_operations;
957 }
958
959 return 0;
960}
961__initcall(eeh_init_proc);