powerpc/eeh: Introduce EEH_PE_INVALID type PE
[deliverable/linux.git] / arch / powerpc / platforms / pseries / eeh_driver.c
CommitLineData
77bd7415
LV
1/*
2 * PCI Error Recovery Driver for RPA-compliant PPC64 platform.
3c8c90ab
LV
3 * Copyright IBM Corp. 2004 2005
4 * Copyright Linas Vepstas <linas@linas.org> 2004, 2005
77bd7415
LV
5 *
6 * All rights reserved.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
16 * NON INFRINGEMENT. See the GNU General Public License for more
17 * details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
3c8c90ab 23 * Send comments and feedback to Linas Vepstas <linas@austin.ibm.com>
77bd7415
LV
24 */
25#include <linux/delay.h>
77bd7415 26#include <linux/interrupt.h>
ac325acd 27#include <linux/irq.h>
77bd7415
LV
28#include <linux/pci.h>
29#include <asm/eeh.h>
30#include <asm/eeh_event.h>
31#include <asm/ppc-pci.h>
32#include <asm/pci-bridge.h>
33#include <asm/prom.h>
34#include <asm/rtas.h>
35
29f8bf1b
GS
36/**
37 * eeh_pcid_name - Retrieve name of PCI device driver
38 * @pdev: PCI device
39 *
40 * This routine is used to retrieve the name of PCI device driver
41 * if that's valid.
42 */
40a7cd92 43static inline const char *eeh_pcid_name(struct pci_dev *pdev)
77bd7415 44{
273d2803 45 if (pdev && pdev->dev.driver)
77bd7415
LV
46 return pdev->dev.driver->name;
47 return "";
48}
49
dcfcfe75 50#if 0
8e01520c 51static void print_device_node_tree(struct pci_dn *pdn, int dent)
77bd7415
LV
52{
53 int i;
8e01520c
AM
54 struct device_node *pc;
55
56 if (!pdn)
57 return;
58 for (i = 0; i < dent; i++)
77bd7415
LV
59 printk(" ");
60 printk("dn=%s mode=%x \tcfg_addr=%x pe_addr=%x \tfull=%s\n",
61 pdn->node->name, pdn->eeh_mode, pdn->eeh_config_addr,
62 pdn->eeh_pe_config_addr, pdn->node->full_name);
63 dent += 3;
8e01520c 64 pc = pdn->node->child;
77bd7415
LV
65 while (pc) {
66 print_device_node_tree(PCI_DN(pc), dent);
67 pc = pc->sibling;
68 }
69}
70#endif
71
8535ef05 72/**
29f8bf1b
GS
73 * eeh_disable_irq - Disable interrupt for the recovering device
74 * @dev: PCI device
75 *
76 * This routine must be called when reporting temporary or permanent
77 * error to the particular PCI device to disable interrupt of that
78 * device. If the device has enabled MSI or MSI-X interrupt, we needn't
79 * do real work because EEH should freeze DMA transfers for those PCI
80 * devices encountering EEH errors, which includes MSI or MSI-X.
8535ef05
MM
81 */
82static void eeh_disable_irq(struct pci_dev *dev)
83{
40a7cd92 84 struct eeh_dev *edev = pci_dev_to_eeh_dev(dev);
8535ef05
MM
85
86 /* Don't disable MSI and MSI-X interrupts. They are
87 * effectively disabled by the DMA Stopped state
88 * when an EEH error occurs.
29f8bf1b 89 */
8535ef05
MM
90 if (dev->msi_enabled || dev->msix_enabled)
91 return;
92
59e3f837 93 if (!irq_has_action(dev->irq))
8535ef05
MM
94 return;
95
dbbceee1 96 edev->mode |= EEH_DEV_IRQ_DISABLED;
8535ef05
MM
97 disable_irq_nosync(dev->irq);
98}
99
100/**
29f8bf1b
GS
101 * eeh_enable_irq - Enable interrupt for the recovering device
102 * @dev: PCI device
103 *
104 * This routine must be called to enable interrupt while failed
105 * device could be resumed.
8535ef05
MM
106 */
107static void eeh_enable_irq(struct pci_dev *dev)
108{
40a7cd92 109 struct eeh_dev *edev = pci_dev_to_eeh_dev(dev);
8535ef05 110
dbbceee1
GS
111 if ((edev->mode) & EEH_DEV_IRQ_DISABLED) {
112 edev->mode &= ~EEH_DEV_IRQ_DISABLED;
8535ef05
MM
113 enable_irq(dev->irq);
114 }
115}
116
cb5b5624 117/**
29f8bf1b 118 * eeh_report_error - Report pci error to each device driver
9b3c76f0 119 * @data: eeh device
29f8bf1b 120 * @userdata: return value
cb5b5624
LV
121 *
122 * Report an EEH error to each device driver, collect up and
123 * merge the device driver responses. Cumulative response
124 * passed back in "userdata".
77bd7415 125 */
9b3c76f0 126static void *eeh_report_error(void *data, void *userdata)
77bd7415 127{
9b3c76f0
GS
128 struct eeh_dev *edev = (struct eeh_dev *)data;
129 struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
18eb3b39 130 enum pci_ers_result rc, *res = userdata;
77bd7415
LV
131 struct pci_driver *driver = dev->driver;
132
9b3c76f0
GS
133 /* We might not have the associated PCI device,
134 * then we should continue for next one.
135 */
136 if (!dev) return NULL;
137
77bd7415
LV
138 dev->error_state = pci_channel_io_frozen;
139
140 if (!driver)
9b3c76f0 141 return NULL;
77bd7415 142
8535ef05
MM
143 eeh_disable_irq(dev);
144
6a1ca373
LV
145 if (!driver->err_handler ||
146 !driver->err_handler->error_detected)
9b3c76f0 147 return NULL;
77bd7415 148
29f8bf1b 149 rc = driver->err_handler->error_detected(dev, pci_channel_io_frozen);
2a50f144
LV
150
151 /* A driver that needs a reset trumps all others */
152 if (rc == PCI_ERS_RESULT_NEED_RESET) *res = rc;
18eb3b39 153 if (*res == PCI_ERS_RESULT_NONE) *res = rc;
70298c6e 154
9b3c76f0 155 return NULL;
6a1ca373
LV
156}
157
158/**
29f8bf1b 159 * eeh_report_mmio_enabled - Tell drivers that MMIO has been enabled
9b3c76f0 160 * @data: eeh device
29f8bf1b 161 * @userdata: return value
6a1ca373 162 *
638799b3
LV
163 * Tells each device driver that IO ports, MMIO and config space I/O
164 * are now enabled. Collects up and merges the device driver responses.
165 * Cumulative response passed back in "userdata".
6a1ca373 166 */
9b3c76f0 167static void *eeh_report_mmio_enabled(void *data, void *userdata)
6a1ca373 168{
9b3c76f0
GS
169 struct eeh_dev *edev = (struct eeh_dev *)data;
170 struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
6a1ca373 171 enum pci_ers_result rc, *res = userdata;
9b3c76f0 172 struct pci_driver *driver;
6a1ca373 173
9b3c76f0
GS
174 if (!dev) return NULL;
175
176 if (!(driver = dev->driver) ||
6a1ca373
LV
177 !driver->err_handler ||
178 !driver->err_handler->mmio_enabled)
9b3c76f0 179 return NULL;
6a1ca373 180
29f8bf1b 181 rc = driver->err_handler->mmio_enabled(dev);
2a50f144
LV
182
183 /* A driver that needs a reset trumps all others */
184 if (rc == PCI_ERS_RESULT_NEED_RESET) *res = rc;
6a1ca373 185 if (*res == PCI_ERS_RESULT_NONE) *res = rc;
70298c6e 186
9b3c76f0 187 return NULL;
77bd7415
LV
188}
189
cb5b5624 190/**
29f8bf1b 191 * eeh_report_reset - Tell device that slot has been reset
9b3c76f0 192 * @data: eeh device
29f8bf1b
GS
193 * @userdata: return value
194 *
195 * This routine must be called while EEH tries to reset particular
196 * PCI device so that the associated PCI device driver could take
197 * some actions, usually to save data the driver needs so that the
198 * driver can work again while the device is recovered.
77bd7415 199 */
9b3c76f0 200static void *eeh_report_reset(void *data, void *userdata)
77bd7415 201{
9b3c76f0
GS
202 struct eeh_dev *edev = (struct eeh_dev *)data;
203 struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
6a1ca373 204 enum pci_ers_result rc, *res = userdata;
9b3c76f0 205 struct pci_driver *driver;
77bd7415 206
9b3c76f0
GS
207 if (!dev || !(driver = dev->driver))
208 return NULL;
77bd7415 209
c58dc575
MM
210 dev->error_state = pci_channel_io_normal;
211
8535ef05
MM
212 eeh_enable_irq(dev);
213
6a1ca373
LV
214 if (!driver->err_handler ||
215 !driver->err_handler->slot_reset)
9b3c76f0 216 return NULL;
77bd7415 217
6a1ca373 218 rc = driver->err_handler->slot_reset(dev);
5794dbcb
LV
219 if ((*res == PCI_ERS_RESULT_NONE) ||
220 (*res == PCI_ERS_RESULT_RECOVERED)) *res = rc;
6a1ca373
LV
221 if (*res == PCI_ERS_RESULT_DISCONNECT &&
222 rc == PCI_ERS_RESULT_NEED_RESET) *res = rc;
70298c6e 223
9b3c76f0 224 return NULL;
77bd7415
LV
225}
226
cb5b5624 227/**
29f8bf1b 228 * eeh_report_resume - Tell device to resume normal operations
9b3c76f0 229 * @data: eeh device
29f8bf1b
GS
230 * @userdata: return value
231 *
232 * This routine must be called to notify the device driver that it
233 * could resume so that the device driver can do some initialization
234 * to make the recovered device work again.
cb5b5624 235 */
9b3c76f0 236static void *eeh_report_resume(void *data, void *userdata)
77bd7415 237{
9b3c76f0
GS
238 struct eeh_dev *edev = (struct eeh_dev *)data;
239 struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
240 struct pci_driver *driver;
241
242 if (!dev) return NULL;
77bd7415
LV
243
244 dev->error_state = pci_channel_io_normal;
245
9b3c76f0
GS
246 if (!(driver = dev->driver))
247 return NULL;
d0e70341 248
8535ef05
MM
249 eeh_enable_irq(dev);
250
d0e70341
LV
251 if (!driver->err_handler ||
252 !driver->err_handler->resume)
9b3c76f0 253 return NULL;
77bd7415
LV
254
255 driver->err_handler->resume(dev);
70298c6e 256
9b3c76f0 257 return NULL;
77bd7415
LV
258}
259
cb5b5624 260/**
29f8bf1b 261 * eeh_report_failure - Tell device driver that device is dead.
9b3c76f0 262 * @data: eeh device
29f8bf1b 263 * @userdata: return value
cb5b5624
LV
264 *
265 * This informs the device driver that the device is permanently
266 * dead, and that no further recovery attempts will be made on it.
267 */
9b3c76f0 268static void *eeh_report_failure(void *data, void *userdata)
77bd7415 269{
9b3c76f0
GS
270 struct eeh_dev *edev = (struct eeh_dev *)data;
271 struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
272 struct pci_driver *driver;
273
274 if (!dev) return NULL;
77bd7415
LV
275
276 dev->error_state = pci_channel_io_perm_failure;
277
9b3c76f0
GS
278 if (!(driver = dev->driver))
279 return NULL;
77bd7415 280
8535ef05
MM
281 eeh_disable_irq(dev);
282
283 if (!driver->err_handler ||
284 !driver->err_handler->error_detected)
9b3c76f0 285 return NULL;
8535ef05 286
77bd7415 287 driver->err_handler->error_detected(dev, pci_channel_io_perm_failure);
70298c6e 288
9b3c76f0 289 return NULL;
77bd7415
LV
290}
291
77bd7415 292/**
29f8bf1b 293 * eeh_reset_device - Perform actual reset of a pci slot
9b3c76f0 294 * @pe: EEH PE
29f8bf1b 295 * @bus: PCI bus corresponding to the isolcated slot
77bd7415 296 *
29f8bf1b
GS
297 * This routine must be called to do reset on the indicated PE.
298 * During the reset, udev might be invoked because those affected
299 * PCI devices will be removed and then added.
77bd7415 300 */
9b3c76f0 301static int eeh_reset_device(struct eeh_pe *pe, struct pci_bus *bus)
77bd7415 302{
42405456
LV
303 int cnt, rc;
304
305 /* pcibios will clear the counter; save the value */
9b3c76f0 306 cnt = pe->freeze_count;
42405456 307
77bd7415
LV
308 if (bus)
309 pcibios_remove_pci_devices(bus);
310
311 /* Reset the pci controller. (Asserts RST#; resets config space).
b6495c0c 312 * Reconfigure bridges and devices. Don't try to bring the system
29f8bf1b
GS
313 * up if the reset failed for some reason.
314 */
9b3c76f0 315 rc = eeh_reset_pe(pe);
b6495c0c
LV
316 if (rc)
317 return rc;
77bd7415 318
9b3c76f0
GS
319 /* Restore PE */
320 eeh_ops->configure_bridge(pe);
321 eeh_pe_restore_bars(pe);
77bd7415
LV
322
323 /* Give the system 5 seconds to finish running the user-space
324 * hotplug shutdown scripts, e.g. ifdown for ethernet. Yes,
325 * this is a hack, but if we don't do this, and try to bring
326 * the device up before the scripts have taken it down,
327 * potentially weird things happen.
328 */
329 if (bus) {
29f8bf1b 330 ssleep(5);
77bd7415
LV
331 pcibios_add_pci_devices(bus);
332 }
9b3c76f0 333 pe->freeze_count = cnt;
b6495c0c
LV
334
335 return 0;
77bd7415
LV
336}
337
338/* The longest amount of time to wait for a pci device
339 * to come back on line, in seconds.
340 */
fa1be476 341#define MAX_WAIT_FOR_RECOVERY 150
77bd7415 342
29f8bf1b
GS
343/**
344 * eeh_handle_event - Reset a PCI device after hard lockup.
9b3c76f0 345 * @pe: EEH PE
29f8bf1b
GS
346 *
347 * While PHB detects address or data parity errors on particular PCI
348 * slot, the associated PE will be frozen. Besides, DMA's occurring
349 * to wild addresses (which usually happen due to bugs in device
350 * drivers or in PCI adapter firmware) can cause EEH error. #SERR,
351 * #PERR or other misc PCI-related errors also can trigger EEH errors.
352 *
353 * Recovery process consists of unplugging the device driver (which
354 * generated hotplug events to userspace), then issuing a PCI #RST to
355 * the device, then reconfiguring the PCI config space for all bridges
356 * & devices under this slot, and then finally restarting the device
357 * drivers (which cause a second set of hotplug events to go out to
358 * userspace).
359 */
9b3c76f0 360void eeh_handle_event(struct eeh_pe *pe)
77bd7415 361{
77bd7415 362 struct pci_bus *frozen_bus;
b6495c0c 363 int rc = 0;
18eb3b39 364 enum pci_ers_result result = PCI_ERS_RESULT_NONE;
77bd7415 365
9b3c76f0 366 frozen_bus = eeh_pe_bus_get(pe);
77bd7415 367 if (!frozen_bus) {
9b3c76f0
GS
368 pr_err("%s: Cannot find PCI bus for PHB#%d-PE#%x\n",
369 __func__, pe->phb->global_number, pe->addr);
370 return;
77bd7415
LV
371 }
372
9b3c76f0
GS
373 pe->freeze_count++;
374 if (pe->freeze_count > EEH_MAX_ALLOWED_FREEZES)
8df83028 375 goto excess_failures;
9b3c76f0
GS
376 pr_warning("EEH: This PCI device has failed %d times in the last hour\n",
377 pe->freeze_count);
77bd7415
LV
378
379 /* Walk the various device drivers attached to this slot through
380 * a reset sequence, giving each an opportunity to do what it needs
381 * to accomplish the reset. Each child gets a report of the
382 * status ... if any child can't handle the reset, then the entire
383 * slot is dlpar removed and added.
384 */
9b3c76f0 385 eeh_pe_dev_traverse(pe, eeh_report_error, &result);
77bd7415 386
5f1a7c81 387 /* Get the current PCI slot state. This can take a long time,
29f8bf1b
GS
388 * sometimes over 3 seconds for certain systems.
389 */
9b3c76f0 390 rc = eeh_ops->wait_state(pe, MAX_WAIT_FOR_RECOVERY*1000);
eb594a47 391 if (rc < 0 || rc == EEH_STATE_NOT_SUPPORT) {
5f1a7c81
LV
392 printk(KERN_WARNING "EEH: Permanent failure\n");
393 goto hard_fail;
394 }
395
ede8ca26
LV
396 /* Since rtas may enable MMIO when posting the error log,
397 * don't post the error log until after all dev drivers
17213c3b
LV
398 * have been informed.
399 */
9b3c76f0 400 eeh_slot_error_detail(pe, EEH_LOG_TEMP);
ede8ca26 401
77bd7415
LV
402 /* If all device drivers were EEH-unaware, then shut
403 * down all of the device drivers, and hope they
404 * go down willingly, without panicing the system.
405 */
18eb3b39 406 if (result == PCI_ERS_RESULT_NONE) {
9b3c76f0 407 rc = eeh_reset_device(pe, frozen_bus);
e0f90b64
LV
408 if (rc) {
409 printk(KERN_WARNING "EEH: Unable to reset, rc=%d\n", rc);
b6495c0c 410 goto hard_fail;
e0f90b64 411 }
77bd7415
LV
412 }
413
6a1ca373
LV
414 /* If all devices reported they can proceed, then re-enable MMIO */
415 if (result == PCI_ERS_RESULT_CAN_RECOVER) {
9b3c76f0 416 rc = eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
6a1ca373 417
fa1be476
LV
418 if (rc < 0)
419 goto hard_fail;
6a1ca373
LV
420 if (rc) {
421 result = PCI_ERS_RESULT_NEED_RESET;
422 } else {
423 result = PCI_ERS_RESULT_NONE;
9b3c76f0 424 eeh_pe_dev_traverse(pe, eeh_report_mmio_enabled, &result);
6a1ca373 425 }
77bd7415
LV
426 }
427
6a1ca373 428 /* If all devices reported they can proceed, then re-enable DMA */
18eb3b39 429 if (result == PCI_ERS_RESULT_CAN_RECOVER) {
9b3c76f0 430 rc = eeh_pci_enable(pe, EEH_OPT_THAW_DMA);
6a1ca373 431
fa1be476
LV
432 if (rc < 0)
433 goto hard_fail;
6a1ca373
LV
434 if (rc)
435 result = PCI_ERS_RESULT_NEED_RESET;
d0e70341
LV
436 else
437 result = PCI_ERS_RESULT_RECOVERED;
6a1ca373
LV
438 }
439
440 /* If any device has a hard failure, then shut off everything. */
e0f90b64
LV
441 if (result == PCI_ERS_RESULT_DISCONNECT) {
442 printk(KERN_WARNING "EEH: Device driver gave up\n");
6a1ca373 443 goto hard_fail;
e0f90b64 444 }
6a1ca373
LV
445
446 /* If any device called out for a reset, then reset the slot */
447 if (result == PCI_ERS_RESULT_NEED_RESET) {
9b3c76f0 448 rc = eeh_reset_device(pe, NULL);
e0f90b64
LV
449 if (rc) {
450 printk(KERN_WARNING "EEH: Cannot reset, rc=%d\n", rc);
b6495c0c 451 goto hard_fail;
e0f90b64 452 }
6a1ca373 453 result = PCI_ERS_RESULT_NONE;
9b3c76f0 454 eeh_pe_dev_traverse(pe, eeh_report_reset, &result);
77bd7415
LV
455 }
456
6a1ca373 457 /* All devices should claim they have recovered by now. */
90fdd613
LV
458 if ((result != PCI_ERS_RESULT_RECOVERED) &&
459 (result != PCI_ERS_RESULT_NONE)) {
e0f90b64 460 printk(KERN_WARNING "EEH: Not recovered\n");
6a1ca373 461 goto hard_fail;
e0f90b64 462 }
6a1ca373 463
77bd7415 464 /* Tell all device drivers that they can resume operations */
9b3c76f0 465 eeh_pe_dev_traverse(pe, eeh_report_resume, NULL);
b6495c0c 466
9b3c76f0 467 return;
b6495c0c 468
8df83028 469excess_failures:
b6495c0c
LV
470 /*
471 * About 90% of all real-life EEH failures in the field
472 * are due to poorly seated PCI cards. Only 10% or so are
473 * due to actual, failed cards.
474 */
9b3c76f0
GS
475 pr_err("EEH: PHB#%d-PE#%x has failed %d times in the\n"
476 "last hour and has been permanently disabled.\n"
477 "Please try reseating or replacing it.\n",
478 pe->phb->global_number, pe->addr,
479 pe->freeze_count);
8df83028
LV
480 goto perm_error;
481
482hard_fail:
9b3c76f0
GS
483 pr_err("EEH: Unable to recover from failure from PHB#%d-PE#%x.\n"
484 "Please try reseating or replacing it\n",
485 pe->phb->global_number, pe->addr);
b6495c0c 486
8df83028 487perm_error:
9b3c76f0 488 eeh_slot_error_detail(pe, EEH_LOG_PERM);
b6495c0c
LV
489
490 /* Notify all devices that they're about to go down. */
9b3c76f0 491 eeh_pe_dev_traverse(pe, eeh_report_failure, NULL);
b6495c0c
LV
492
493 /* Shut down the device drivers for good. */
9b3c76f0
GS
494 if (frozen_bus)
495 pcibios_remove_pci_devices(frozen_bus);
77bd7415
LV
496}
497
This page took 1.781281 seconds and 5 git commands to generate.