]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/powerpc/platforms/pseries/eeh_driver.c
powerpc/pseries: Remove RTAS_POWERMGM_EVENTS
[karo-tx-linux.git] / arch / powerpc / platforms / pseries / eeh_driver.c
1 /*
2  * PCI Error Recovery Driver for RPA-compliant PPC64 platform.
3  * Copyright IBM Corp. 2004 2005
4  * Copyright Linas Vepstas <linas@linas.org> 2004, 2005
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  *
23  * Send comments and feedback to Linas Vepstas <linas@austin.ibm.com>
24  */
25 #include <linux/delay.h>
26 #include <linux/interrupt.h>
27 #include <linux/irq.h>
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
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  */
43 static inline const char *eeh_pcid_name(struct pci_dev *pdev)
44 {
45         if (pdev && pdev->dev.driver)
46                 return pdev->dev.driver->name;
47         return "";
48 }
49
50 #if 0
51 static void print_device_node_tree(struct pci_dn *pdn, int dent)
52 {
53         int i;
54         struct device_node *pc;
55
56         if (!pdn)
57                 return;
58         for (i = 0; i < dent; i++)
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;
64         pc = pdn->node->child;
65         while (pc) {
66                 print_device_node_tree(PCI_DN(pc), dent);
67                 pc = pc->sibling;
68         }
69 }
70 #endif
71
72 /**
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.
81  */
82 static void eeh_disable_irq(struct pci_dev *dev)
83 {
84         struct eeh_dev *edev = pci_dev_to_eeh_dev(dev);
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.
89          */
90         if (dev->msi_enabled || dev->msix_enabled)
91                 return;
92
93         if (!irq_has_action(dev->irq))
94                 return;
95
96         edev->mode |= EEH_MODE_IRQ_DISABLED;
97         disable_irq_nosync(dev->irq);
98 }
99
100 /**
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.
106  */
107 static void eeh_enable_irq(struct pci_dev *dev)
108 {
109         struct eeh_dev *edev = pci_dev_to_eeh_dev(dev);
110
111         if ((edev->mode) & EEH_MODE_IRQ_DISABLED) {
112                 edev->mode &= ~EEH_MODE_IRQ_DISABLED;
113                 enable_irq(dev->irq);
114         }
115 }
116
117 /**
118  * eeh_report_error - Report pci error to each device driver
119  * @dev: PCI device
120  * @userdata: return value
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".
125  */
126 static int eeh_report_error(struct pci_dev *dev, void *userdata)
127 {
128         enum pci_ers_result rc, *res = userdata;
129         struct pci_driver *driver = dev->driver;
130
131         dev->error_state = pci_channel_io_frozen;
132
133         if (!driver)
134                 return 0;
135
136         eeh_disable_irq(dev);
137
138         if (!driver->err_handler ||
139             !driver->err_handler->error_detected)
140                 return 0;
141
142         rc = driver->err_handler->error_detected(dev, pci_channel_io_frozen);
143
144         /* A driver that needs a reset trumps all others */
145         if (rc == PCI_ERS_RESULT_NEED_RESET) *res = rc;
146         if (*res == PCI_ERS_RESULT_NONE) *res = rc;
147
148         return 0;
149 }
150
151 /**
152  * eeh_report_mmio_enabled - Tell drivers that MMIO has been enabled
153  * @dev: PCI device
154  * @userdata: return value
155  *
156  * Tells each device driver that IO ports, MMIO and config space I/O
157  * are now enabled. Collects up and merges the device driver responses.
158  * Cumulative response passed back in "userdata".
159  */
160 static int eeh_report_mmio_enabled(struct pci_dev *dev, void *userdata)
161 {
162         enum pci_ers_result rc, *res = userdata;
163         struct pci_driver *driver = dev->driver;
164
165         if (!driver ||
166             !driver->err_handler ||
167             !driver->err_handler->mmio_enabled)
168                 return 0;
169
170         rc = driver->err_handler->mmio_enabled(dev);
171
172         /* A driver that needs a reset trumps all others */
173         if (rc == PCI_ERS_RESULT_NEED_RESET) *res = rc;
174         if (*res == PCI_ERS_RESULT_NONE) *res = rc;
175
176         return 0;
177 }
178
179 /**
180  * eeh_report_reset - Tell device that slot has been reset
181  * @dev: PCI device
182  * @userdata: return value
183  *
184  * This routine must be called while EEH tries to reset particular
185  * PCI device so that the associated PCI device driver could take
186  * some actions, usually to save data the driver needs so that the
187  * driver can work again while the device is recovered.
188  */
189 static int eeh_report_reset(struct pci_dev *dev, void *userdata)
190 {
191         enum pci_ers_result rc, *res = userdata;
192         struct pci_driver *driver = dev->driver;
193
194         if (!driver)
195                 return 0;
196
197         dev->error_state = pci_channel_io_normal;
198
199         eeh_enable_irq(dev);
200
201         if (!driver->err_handler ||
202             !driver->err_handler->slot_reset)
203                 return 0;
204
205         rc = driver->err_handler->slot_reset(dev);
206         if ((*res == PCI_ERS_RESULT_NONE) ||
207             (*res == PCI_ERS_RESULT_RECOVERED)) *res = rc;
208         if (*res == PCI_ERS_RESULT_DISCONNECT &&
209              rc == PCI_ERS_RESULT_NEED_RESET) *res = rc;
210
211         return 0;
212 }
213
214 /**
215  * eeh_report_resume - Tell device to resume normal operations
216  * @dev: PCI device
217  * @userdata: return value
218  *
219  * This routine must be called to notify the device driver that it
220  * could resume so that the device driver can do some initialization
221  * to make the recovered device work again.
222  */
223 static int eeh_report_resume(struct pci_dev *dev, void *userdata)
224 {
225         struct pci_driver *driver = dev->driver;
226
227         dev->error_state = pci_channel_io_normal;
228
229         if (!driver)
230                 return 0;
231
232         eeh_enable_irq(dev);
233
234         if (!driver->err_handler ||
235             !driver->err_handler->resume)
236                 return 0;
237
238         driver->err_handler->resume(dev);
239
240         return 0;
241 }
242
243 /**
244  * eeh_report_failure - Tell device driver that device is dead.
245  * @dev: PCI device
246  * @userdata: return value
247  *
248  * This informs the device driver that the device is permanently
249  * dead, and that no further recovery attempts will be made on it.
250  */
251 static int eeh_report_failure(struct pci_dev *dev, void *userdata)
252 {
253         struct pci_driver *driver = dev->driver;
254
255         dev->error_state = pci_channel_io_perm_failure;
256
257         if (!driver)
258                 return 0;
259
260         eeh_disable_irq(dev);
261
262         if (!driver->err_handler ||
263             !driver->err_handler->error_detected)
264                 return 0;
265
266         driver->err_handler->error_detected(dev, pci_channel_io_perm_failure);
267
268         return 0;
269 }
270
271 /**
272  * eeh_reset_device - Perform actual reset of a pci slot
273  * @edev: PE associated EEH device
274  * @bus: PCI bus corresponding to the isolcated slot
275  *
276  * This routine must be called to do reset on the indicated PE.
277  * During the reset, udev might be invoked because those affected
278  * PCI devices will be removed and then added.
279  */
280 static int eeh_reset_device(struct eeh_dev *edev, struct pci_bus *bus)
281 {
282         struct device_node *dn;
283         int cnt, rc;
284
285         /* pcibios will clear the counter; save the value */
286         cnt = edev->freeze_count;
287
288         if (bus)
289                 pcibios_remove_pci_devices(bus);
290
291         /* Reset the pci controller. (Asserts RST#; resets config space).
292          * Reconfigure bridges and devices. Don't try to bring the system
293          * up if the reset failed for some reason.
294          */
295         rc = eeh_reset_pe(edev);
296         if (rc)
297                 return rc;
298
299         /* Walk over all functions on this device. */
300         dn = eeh_dev_to_of_node(edev);
301         if (!pcibios_find_pci_bus(dn) && of_node_to_eeh_dev(dn->parent))
302                 dn = dn->parent->child;
303
304         while (dn) {
305                 struct eeh_dev *pedev = of_node_to_eeh_dev(dn);
306
307                 /* On Power4, always true because eeh_pe_config_addr=0 */
308                 if (edev->pe_config_addr == pedev->pe_config_addr) {
309                         eeh_ops->configure_bridge(dn);
310                         eeh_restore_bars(pedev);
311                 }
312                 dn = dn->sibling;
313         }
314
315         /* Give the system 5 seconds to finish running the user-space
316          * hotplug shutdown scripts, e.g. ifdown for ethernet.  Yes, 
317          * this is a hack, but if we don't do this, and try to bring 
318          * the device up before the scripts have taken it down, 
319          * potentially weird things happen.
320          */
321         if (bus) {
322                 ssleep(5);
323                 pcibios_add_pci_devices(bus);
324         }
325         edev->freeze_count = cnt;
326
327         return 0;
328 }
329
330 /* The longest amount of time to wait for a pci device
331  * to come back on line, in seconds.
332  */
333 #define MAX_WAIT_FOR_RECOVERY 150
334
335 /**
336  * eeh_handle_event - Reset a PCI device after hard lockup.
337  * @event: EEH event
338  *
339  * While PHB detects address or data parity errors on particular PCI
340  * slot, the associated PE will be frozen. Besides, DMA's occurring
341  * to wild addresses (which usually happen due to bugs in device
342  * drivers or in PCI adapter firmware) can cause EEH error. #SERR,
343  * #PERR or other misc PCI-related errors also can trigger EEH errors.
344  *
345  * Recovery process consists of unplugging the device driver (which
346  * generated hotplug events to userspace), then issuing a PCI #RST to
347  * the device, then reconfiguring the PCI config space for all bridges
348  * & devices under this slot, and then finally restarting the device
349  * drivers (which cause a second set of hotplug events to go out to
350  * userspace).
351  */
352 struct eeh_dev *handle_eeh_events(struct eeh_event *event)
353 {
354         struct device_node *frozen_dn;
355         struct eeh_dev *frozen_edev;
356         struct pci_bus *frozen_bus;
357         int rc = 0;
358         enum pci_ers_result result = PCI_ERS_RESULT_NONE;
359         const char *location, *pci_str, *drv_str, *bus_pci_str, *bus_drv_str;
360
361         frozen_dn = eeh_find_device_pe(eeh_dev_to_of_node(event->edev));
362         if (!frozen_dn) {
363                 location = of_get_property(eeh_dev_to_of_node(event->edev), "ibm,loc-code", NULL);
364                 location = location ? location : "unknown";
365                 printk(KERN_ERR "EEH: Error: Cannot find partition endpoint "
366                                 "for location=%s pci addr=%s\n",
367                         location, eeh_pci_name(eeh_dev_to_pci_dev(event->edev)));
368                 return NULL;
369         }
370
371         frozen_bus = pcibios_find_pci_bus(frozen_dn);
372         location = of_get_property(frozen_dn, "ibm,loc-code", NULL);
373         location = location ? location : "unknown";
374
375         /* There are two different styles for coming up with the PE.
376          * In the old style, it was the highest EEH-capable device
377          * which was always an EADS pci bridge.  In the new style,
378          * there might not be any EADS bridges, and even when there are,
379          * the firmware marks them as "EEH incapable". So another
380          * two-step is needed to find the pci bus..
381          */
382         if (!frozen_bus)
383                 frozen_bus = pcibios_find_pci_bus(frozen_dn->parent);
384
385         if (!frozen_bus) {
386                 printk(KERN_ERR "EEH: Cannot find PCI bus "
387                         "for location=%s dn=%s\n",
388                         location, frozen_dn->full_name);
389                 return NULL;
390         }
391
392         frozen_edev = of_node_to_eeh_dev(frozen_dn);
393         frozen_edev->freeze_count++;
394         pci_str = eeh_pci_name(eeh_dev_to_pci_dev(event->edev));
395         drv_str = eeh_pcid_name(eeh_dev_to_pci_dev(event->edev));
396
397         if (frozen_edev->freeze_count > EEH_MAX_ALLOWED_FREEZES)
398                 goto excess_failures;
399
400         printk(KERN_WARNING
401            "EEH: This PCI device has failed %d times in the last hour:\n",
402                 frozen_edev->freeze_count);
403
404         if (frozen_edev->pdev) {
405                 bus_pci_str = pci_name(frozen_edev->pdev);
406                 bus_drv_str = eeh_pcid_name(frozen_edev->pdev);
407                 printk(KERN_WARNING
408                         "EEH: Bus location=%s driver=%s pci addr=%s\n",
409                         location, bus_drv_str, bus_pci_str);
410         }
411
412         printk(KERN_WARNING
413                 "EEH: Device location=%s driver=%s pci addr=%s\n",
414                 location, drv_str, pci_str);
415
416         /* Walk the various device drivers attached to this slot through
417          * a reset sequence, giving each an opportunity to do what it needs
418          * to accomplish the reset.  Each child gets a report of the
419          * status ... if any child can't handle the reset, then the entire
420          * slot is dlpar removed and added.
421          */
422         pci_walk_bus(frozen_bus, eeh_report_error, &result);
423
424         /* Get the current PCI slot state. This can take a long time,
425          * sometimes over 3 seconds for certain systems.
426          */
427         rc = eeh_ops->wait_state(eeh_dev_to_of_node(frozen_edev), MAX_WAIT_FOR_RECOVERY*1000);
428         if (rc < 0 || rc == EEH_STATE_NOT_SUPPORT) {
429                 printk(KERN_WARNING "EEH: Permanent failure\n");
430                 goto hard_fail;
431         }
432
433         /* Since rtas may enable MMIO when posting the error log,
434          * don't post the error log until after all dev drivers
435          * have been informed.
436          */
437         eeh_slot_error_detail(frozen_edev, EEH_LOG_TEMP);
438
439         /* If all device drivers were EEH-unaware, then shut
440          * down all of the device drivers, and hope they
441          * go down willingly, without panicing the system.
442          */
443         if (result == PCI_ERS_RESULT_NONE) {
444                 rc = eeh_reset_device(frozen_edev, frozen_bus);
445                 if (rc) {
446                         printk(KERN_WARNING "EEH: Unable to reset, rc=%d\n", rc);
447                         goto hard_fail;
448                 }
449         }
450
451         /* If all devices reported they can proceed, then re-enable MMIO */
452         if (result == PCI_ERS_RESULT_CAN_RECOVER) {
453                 rc = eeh_pci_enable(frozen_edev, EEH_OPT_THAW_MMIO);
454
455                 if (rc < 0)
456                         goto hard_fail;
457                 if (rc) {
458                         result = PCI_ERS_RESULT_NEED_RESET;
459                 } else {
460                         result = PCI_ERS_RESULT_NONE;
461                         pci_walk_bus(frozen_bus, eeh_report_mmio_enabled, &result);
462                 }
463         }
464
465         /* If all devices reported they can proceed, then re-enable DMA */
466         if (result == PCI_ERS_RESULT_CAN_RECOVER) {
467                 rc = eeh_pci_enable(frozen_edev, EEH_OPT_THAW_DMA);
468
469                 if (rc < 0)
470                         goto hard_fail;
471                 if (rc)
472                         result = PCI_ERS_RESULT_NEED_RESET;
473                 else
474                         result = PCI_ERS_RESULT_RECOVERED;
475         }
476
477         /* If any device has a hard failure, then shut off everything. */
478         if (result == PCI_ERS_RESULT_DISCONNECT) {
479                 printk(KERN_WARNING "EEH: Device driver gave up\n");
480                 goto hard_fail;
481         }
482
483         /* If any device called out for a reset, then reset the slot */
484         if (result == PCI_ERS_RESULT_NEED_RESET) {
485                 rc = eeh_reset_device(frozen_edev, NULL);
486                 if (rc) {
487                         printk(KERN_WARNING "EEH: Cannot reset, rc=%d\n", rc);
488                         goto hard_fail;
489                 }
490                 result = PCI_ERS_RESULT_NONE;
491                 pci_walk_bus(frozen_bus, eeh_report_reset, &result);
492         }
493
494         /* All devices should claim they have recovered by now. */
495         if ((result != PCI_ERS_RESULT_RECOVERED) &&
496             (result != PCI_ERS_RESULT_NONE)) {
497                 printk(KERN_WARNING "EEH: Not recovered\n");
498                 goto hard_fail;
499         }
500
501         /* Tell all device drivers that they can resume operations */
502         pci_walk_bus(frozen_bus, eeh_report_resume, NULL);
503
504         return frozen_edev;
505         
506 excess_failures:
507         /*
508          * About 90% of all real-life EEH failures in the field
509          * are due to poorly seated PCI cards. Only 10% or so are
510          * due to actual, failed cards.
511          */
512         printk(KERN_ERR
513            "EEH: PCI device at location=%s driver=%s pci addr=%s\n"
514                 "has failed %d times in the last hour "
515                 "and has been permanently disabled.\n"
516                 "Please try reseating this device or replacing it.\n",
517                 location, drv_str, pci_str, frozen_edev->freeze_count);
518         goto perm_error;
519
520 hard_fail:
521         printk(KERN_ERR
522            "EEH: Unable to recover from failure of PCI device "
523            "at location=%s driver=%s pci addr=%s\n"
524            "Please try reseating this device or replacing it.\n",
525                 location, drv_str, pci_str);
526
527 perm_error:
528         eeh_slot_error_detail(frozen_edev, EEH_LOG_PERM);
529
530         /* Notify all devices that they're about to go down. */
531         pci_walk_bus(frozen_bus, eeh_report_failure, NULL);
532
533         /* Shut down the device drivers for good. */
534         pcibios_remove_pci_devices(frozen_bus);
535
536         return NULL;
537 }
538