Linux-libre 5.3.12-gnu
[librecmc/linux-libre.git] / arch / powerpc / kernel / 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/module.h>
29 #include <linux/pci.h>
30 #include <asm/eeh.h>
31 #include <asm/eeh_event.h>
32 #include <asm/ppc-pci.h>
33 #include <asm/pci-bridge.h>
34 #include <asm/prom.h>
35 #include <asm/rtas.h>
36
37 struct eeh_rmv_data {
38         struct list_head removed_vf_list;
39         int removed_dev_count;
40 };
41
42 static int eeh_result_priority(enum pci_ers_result result)
43 {
44         switch (result) {
45         case PCI_ERS_RESULT_NONE:
46                 return 1;
47         case PCI_ERS_RESULT_NO_AER_DRIVER:
48                 return 2;
49         case PCI_ERS_RESULT_RECOVERED:
50                 return 3;
51         case PCI_ERS_RESULT_CAN_RECOVER:
52                 return 4;
53         case PCI_ERS_RESULT_DISCONNECT:
54                 return 5;
55         case PCI_ERS_RESULT_NEED_RESET:
56                 return 6;
57         default:
58                 WARN_ONCE(1, "Unknown pci_ers_result value: %d\n", (int)result);
59                 return 0;
60         }
61 };
62
63 static const char *pci_ers_result_name(enum pci_ers_result result)
64 {
65         switch (result) {
66         case PCI_ERS_RESULT_NONE:
67                 return "none";
68         case PCI_ERS_RESULT_CAN_RECOVER:
69                 return "can recover";
70         case PCI_ERS_RESULT_NEED_RESET:
71                 return "need reset";
72         case PCI_ERS_RESULT_DISCONNECT:
73                 return "disconnect";
74         case PCI_ERS_RESULT_RECOVERED:
75                 return "recovered";
76         case PCI_ERS_RESULT_NO_AER_DRIVER:
77                 return "no AER driver";
78         default:
79                 WARN_ONCE(1, "Unknown result type: %d\n", (int)result);
80                 return "unknown";
81         }
82 };
83
84 static __printf(2, 3) void eeh_edev_info(const struct eeh_dev *edev,
85                                          const char *fmt, ...)
86 {
87         struct va_format vaf;
88         va_list args;
89
90         va_start(args, fmt);
91
92         vaf.fmt = fmt;
93         vaf.va = &args;
94
95         printk(KERN_INFO "EEH: PE#%x (PCI %s): %pV\n", edev->pe_config_addr,
96                edev->pdev ? dev_name(&edev->pdev->dev) : "none", &vaf);
97
98         va_end(args);
99 }
100
101 static enum pci_ers_result pci_ers_merge_result(enum pci_ers_result old,
102                                                 enum pci_ers_result new)
103 {
104         if (eeh_result_priority(new) > eeh_result_priority(old))
105                 return new;
106         return old;
107 }
108
109 static bool eeh_dev_removed(struct eeh_dev *edev)
110 {
111         return !edev || (edev->mode & EEH_DEV_REMOVED);
112 }
113
114 static bool eeh_edev_actionable(struct eeh_dev *edev)
115 {
116         return (edev->pdev && !eeh_dev_removed(edev) &&
117                 !eeh_pe_passed(edev->pe));
118 }
119
120 /**
121  * eeh_pcid_get - Get the PCI device driver
122  * @pdev: PCI device
123  *
124  * The function is used to retrieve the PCI device driver for
125  * the indicated PCI device. Besides, we will increase the reference
126  * of the PCI device driver to prevent that being unloaded on
127  * the fly. Otherwise, kernel crash would be seen.
128  */
129 static inline struct pci_driver *eeh_pcid_get(struct pci_dev *pdev)
130 {
131         if (!pdev || !pdev->driver)
132                 return NULL;
133
134         if (!try_module_get(pdev->driver->driver.owner))
135                 return NULL;
136
137         return pdev->driver;
138 }
139
140 /**
141  * eeh_pcid_put - Dereference on the PCI device driver
142  * @pdev: PCI device
143  *
144  * The function is called to do dereference on the PCI device
145  * driver of the indicated PCI device.
146  */
147 static inline void eeh_pcid_put(struct pci_dev *pdev)
148 {
149         if (!pdev || !pdev->driver)
150                 return;
151
152         module_put(pdev->driver->driver.owner);
153 }
154
155 /**
156  * eeh_disable_irq - Disable interrupt for the recovering device
157  * @dev: PCI device
158  *
159  * This routine must be called when reporting temporary or permanent
160  * error to the particular PCI device to disable interrupt of that
161  * device. If the device has enabled MSI or MSI-X interrupt, we needn't
162  * do real work because EEH should freeze DMA transfers for those PCI
163  * devices encountering EEH errors, which includes MSI or MSI-X.
164  */
165 static void eeh_disable_irq(struct eeh_dev *edev)
166 {
167         /* Don't disable MSI and MSI-X interrupts. They are
168          * effectively disabled by the DMA Stopped state
169          * when an EEH error occurs.
170          */
171         if (edev->pdev->msi_enabled || edev->pdev->msix_enabled)
172                 return;
173
174         if (!irq_has_action(edev->pdev->irq))
175                 return;
176
177         edev->mode |= EEH_DEV_IRQ_DISABLED;
178         disable_irq_nosync(edev->pdev->irq);
179 }
180
181 /**
182  * eeh_enable_irq - Enable interrupt for the recovering device
183  * @dev: PCI device
184  *
185  * This routine must be called to enable interrupt while failed
186  * device could be resumed.
187  */
188 static void eeh_enable_irq(struct eeh_dev *edev)
189 {
190         if ((edev->mode) & EEH_DEV_IRQ_DISABLED) {
191                 edev->mode &= ~EEH_DEV_IRQ_DISABLED;
192                 /*
193                  * FIXME !!!!!
194                  *
195                  * This is just ass backwards. This maze has
196                  * unbalanced irq_enable/disable calls. So instead of
197                  * finding the root cause it works around the warning
198                  * in the irq_enable code by conditionally calling
199                  * into it.
200                  *
201                  * That's just wrong.The warning in the core code is
202                  * there to tell people to fix their asymmetries in
203                  * their own code, not by abusing the core information
204                  * to avoid it.
205                  *
206                  * I so wish that the assymetry would be the other way
207                  * round and a few more irq_disable calls render that
208                  * shit unusable forever.
209                  *
210                  *      tglx
211                  */
212                 if (irqd_irq_disabled(irq_get_irq_data(edev->pdev->irq)))
213                         enable_irq(edev->pdev->irq);
214         }
215 }
216
217 static void *eeh_dev_save_state(struct eeh_dev *edev, void *userdata)
218 {
219         struct pci_dev *pdev;
220
221         if (!edev)
222                 return NULL;
223
224         /*
225          * We cannot access the config space on some adapters.
226          * Otherwise, it will cause fenced PHB. We don't save
227          * the content in their config space and will restore
228          * from the initial config space saved when the EEH
229          * device is created.
230          */
231         if (edev->pe && (edev->pe->state & EEH_PE_CFG_RESTRICTED))
232                 return NULL;
233
234         pdev = eeh_dev_to_pci_dev(edev);
235         if (!pdev)
236                 return NULL;
237
238         pci_save_state(pdev);
239         return NULL;
240 }
241
242 static void eeh_set_channel_state(struct eeh_pe *root, enum pci_channel_state s)
243 {
244         struct eeh_pe *pe;
245         struct eeh_dev *edev, *tmp;
246
247         eeh_for_each_pe(root, pe)
248                 eeh_pe_for_each_dev(pe, edev, tmp)
249                         if (eeh_edev_actionable(edev))
250                                 edev->pdev->error_state = s;
251 }
252
253 static void eeh_set_irq_state(struct eeh_pe *root, bool enable)
254 {
255         struct eeh_pe *pe;
256         struct eeh_dev *edev, *tmp;
257
258         eeh_for_each_pe(root, pe) {
259                 eeh_pe_for_each_dev(pe, edev, tmp) {
260                         if (!eeh_edev_actionable(edev))
261                                 continue;
262
263                         if (!eeh_pcid_get(edev->pdev))
264                                 continue;
265
266                         if (enable)
267                                 eeh_enable_irq(edev);
268                         else
269                                 eeh_disable_irq(edev);
270
271                         eeh_pcid_put(edev->pdev);
272                 }
273         }
274 }
275
276 typedef enum pci_ers_result (*eeh_report_fn)(struct eeh_dev *,
277                                              struct pci_driver *);
278 static void eeh_pe_report_edev(struct eeh_dev *edev, eeh_report_fn fn,
279                                enum pci_ers_result *result)
280 {
281         struct pci_driver *driver;
282         enum pci_ers_result new_result;
283
284         if (!edev->pdev) {
285                 eeh_edev_info(edev, "no device");
286                 return;
287         }
288         device_lock(&edev->pdev->dev);
289         if (eeh_edev_actionable(edev)) {
290                 driver = eeh_pcid_get(edev->pdev);
291
292                 if (!driver)
293                         eeh_edev_info(edev, "no driver");
294                 else if (!driver->err_handler)
295                         eeh_edev_info(edev, "driver not EEH aware");
296                 else if (edev->mode & EEH_DEV_NO_HANDLER)
297                         eeh_edev_info(edev, "driver bound too late");
298                 else {
299                         new_result = fn(edev, driver);
300                         eeh_edev_info(edev, "%s driver reports: '%s'",
301                                       driver->name,
302                                       pci_ers_result_name(new_result));
303                         if (result)
304                                 *result = pci_ers_merge_result(*result,
305                                                                new_result);
306                 }
307                 if (driver)
308                         eeh_pcid_put(edev->pdev);
309         } else {
310                 eeh_edev_info(edev, "not actionable (%d,%d,%d)", !!edev->pdev,
311                               !eeh_dev_removed(edev), !eeh_pe_passed(edev->pe));
312         }
313         device_unlock(&edev->pdev->dev);
314 }
315
316 static void eeh_pe_report(const char *name, struct eeh_pe *root,
317                           eeh_report_fn fn, enum pci_ers_result *result)
318 {
319         struct eeh_pe *pe;
320         struct eeh_dev *edev, *tmp;
321
322         pr_info("EEH: Beginning: '%s'\n", name);
323         eeh_for_each_pe(root, pe) eeh_pe_for_each_dev(pe, edev, tmp)
324                 eeh_pe_report_edev(edev, fn, result);
325         if (result)
326                 pr_info("EEH: Finished:'%s' with aggregate recovery state:'%s'\n",
327                         name, pci_ers_result_name(*result));
328         else
329                 pr_info("EEH: Finished:'%s'", name);
330 }
331
332 /**
333  * eeh_report_error - Report pci error to each device driver
334  * @edev: eeh device
335  * @driver: device's PCI driver
336  *
337  * Report an EEH error to each device driver.
338  */
339 static enum pci_ers_result eeh_report_error(struct eeh_dev *edev,
340                                             struct pci_driver *driver)
341 {
342         enum pci_ers_result rc;
343         struct pci_dev *dev = edev->pdev;
344
345         if (!driver->err_handler->error_detected)
346                 return PCI_ERS_RESULT_NONE;
347
348         eeh_edev_info(edev, "Invoking %s->error_detected(IO frozen)",
349                       driver->name);
350         rc = driver->err_handler->error_detected(dev, pci_channel_io_frozen);
351
352         edev->in_error = true;
353         pci_uevent_ers(dev, PCI_ERS_RESULT_NONE);
354         return rc;
355 }
356
357 /**
358  * eeh_report_mmio_enabled - Tell drivers that MMIO has been enabled
359  * @edev: eeh device
360  * @driver: device's PCI driver
361  *
362  * Tells each device driver that IO ports, MMIO and config space I/O
363  * are now enabled.
364  */
365 static enum pci_ers_result eeh_report_mmio_enabled(struct eeh_dev *edev,
366                                                    struct pci_driver *driver)
367 {
368         if (!driver->err_handler->mmio_enabled)
369                 return PCI_ERS_RESULT_NONE;
370         eeh_edev_info(edev, "Invoking %s->mmio_enabled()", driver->name);
371         return driver->err_handler->mmio_enabled(edev->pdev);
372 }
373
374 /**
375  * eeh_report_reset - Tell device that slot has been reset
376  * @edev: eeh device
377  * @driver: device's PCI driver
378  *
379  * This routine must be called while EEH tries to reset particular
380  * PCI device so that the associated PCI device driver could take
381  * some actions, usually to save data the driver needs so that the
382  * driver can work again while the device is recovered.
383  */
384 static enum pci_ers_result eeh_report_reset(struct eeh_dev *edev,
385                                             struct pci_driver *driver)
386 {
387         if (!driver->err_handler->slot_reset || !edev->in_error)
388                 return PCI_ERS_RESULT_NONE;
389         eeh_edev_info(edev, "Invoking %s->slot_reset()", driver->name);
390         return driver->err_handler->slot_reset(edev->pdev);
391 }
392
393 static void *eeh_dev_restore_state(struct eeh_dev *edev, void *userdata)
394 {
395         struct pci_dev *pdev;
396
397         if (!edev)
398                 return NULL;
399
400         /*
401          * The content in the config space isn't saved because
402          * the blocked config space on some adapters. We have
403          * to restore the initial saved config space when the
404          * EEH device is created.
405          */
406         if (edev->pe && (edev->pe->state & EEH_PE_CFG_RESTRICTED)) {
407                 if (list_is_last(&edev->entry, &edev->pe->edevs))
408                         eeh_pe_restore_bars(edev->pe);
409
410                 return NULL;
411         }
412
413         pdev = eeh_dev_to_pci_dev(edev);
414         if (!pdev)
415                 return NULL;
416
417         pci_restore_state(pdev);
418         return NULL;
419 }
420
421 /**
422  * eeh_report_resume - Tell device to resume normal operations
423  * @edev: eeh device
424  * @driver: device's PCI driver
425  *
426  * This routine must be called to notify the device driver that it
427  * could resume so that the device driver can do some initialization
428  * to make the recovered device work again.
429  */
430 static enum pci_ers_result eeh_report_resume(struct eeh_dev *edev,
431                                              struct pci_driver *driver)
432 {
433         if (!driver->err_handler->resume || !edev->in_error)
434                 return PCI_ERS_RESULT_NONE;
435
436         eeh_edev_info(edev, "Invoking %s->resume()", driver->name);
437         driver->err_handler->resume(edev->pdev);
438
439         pci_uevent_ers(edev->pdev, PCI_ERS_RESULT_RECOVERED);
440 #ifdef CONFIG_PCI_IOV
441         if (eeh_ops->notify_resume && eeh_dev_to_pdn(edev))
442                 eeh_ops->notify_resume(eeh_dev_to_pdn(edev));
443 #endif
444         return PCI_ERS_RESULT_NONE;
445 }
446
447 /**
448  * eeh_report_failure - Tell device driver that device is dead.
449  * @edev: eeh device
450  * @driver: device's PCI driver
451  *
452  * This informs the device driver that the device is permanently
453  * dead, and that no further recovery attempts will be made on it.
454  */
455 static enum pci_ers_result eeh_report_failure(struct eeh_dev *edev,
456                                               struct pci_driver *driver)
457 {
458         enum pci_ers_result rc;
459
460         if (!driver->err_handler->error_detected)
461                 return PCI_ERS_RESULT_NONE;
462
463         eeh_edev_info(edev, "Invoking %s->error_detected(permanent failure)",
464                       driver->name);
465         rc = driver->err_handler->error_detected(edev->pdev,
466                                                  pci_channel_io_perm_failure);
467
468         pci_uevent_ers(edev->pdev, PCI_ERS_RESULT_DISCONNECT);
469         return rc;
470 }
471
472 static void *eeh_add_virt_device(struct eeh_dev *edev)
473 {
474         struct pci_driver *driver;
475         struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
476         struct pci_dn *pdn = eeh_dev_to_pdn(edev);
477
478         if (!(edev->physfn)) {
479                 pr_warn("%s: EEH dev %04x:%02x:%02x.%01x not for VF\n",
480                         __func__, pdn->phb->global_number, pdn->busno,
481                         PCI_SLOT(pdn->devfn), PCI_FUNC(pdn->devfn));
482                 return NULL;
483         }
484
485         driver = eeh_pcid_get(dev);
486         if (driver) {
487                 if (driver->err_handler) {
488                         eeh_pcid_put(dev);
489                         return NULL;
490                 }
491                 eeh_pcid_put(dev);
492         }
493
494 #ifdef CONFIG_PCI_IOV
495         pci_iov_add_virtfn(edev->physfn, pdn->vf_index);
496 #endif
497         return NULL;
498 }
499
500 static void *eeh_rmv_device(struct eeh_dev *edev, void *userdata)
501 {
502         struct pci_driver *driver;
503         struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
504         struct eeh_rmv_data *rmv_data = (struct eeh_rmv_data *)userdata;
505
506         /*
507          * Actually, we should remove the PCI bridges as well.
508          * However, that's lots of complexity to do that,
509          * particularly some of devices under the bridge might
510          * support EEH. So we just care about PCI devices for
511          * simplicity here.
512          */
513         if (!eeh_edev_actionable(edev) ||
514             (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE))
515                 return NULL;
516
517         if (rmv_data) {
518                 driver = eeh_pcid_get(dev);
519                 if (driver) {
520                         if (driver->err_handler &&
521                             driver->err_handler->error_detected &&
522                             driver->err_handler->slot_reset) {
523                                 eeh_pcid_put(dev);
524                                 return NULL;
525                         }
526                         eeh_pcid_put(dev);
527                 }
528         }
529
530         /* Remove it from PCI subsystem */
531         pr_info("EEH: Removing %s without EEH sensitive driver\n",
532                 pci_name(dev));
533         edev->mode |= EEH_DEV_DISCONNECTED;
534         if (rmv_data)
535                 rmv_data->removed_dev_count++;
536
537         if (edev->physfn) {
538 #ifdef CONFIG_PCI_IOV
539                 struct pci_dn *pdn = eeh_dev_to_pdn(edev);
540
541                 pci_iov_remove_virtfn(edev->physfn, pdn->vf_index);
542                 edev->pdev = NULL;
543
544                 /*
545                  * We have to set the VF PE number to invalid one, which is
546                  * required to plug the VF successfully.
547                  */
548                 pdn->pe_number = IODA_INVALID_PE;
549 #endif
550                 if (rmv_data)
551                         list_add(&edev->rmv_entry, &rmv_data->removed_vf_list);
552         } else {
553                 pci_lock_rescan_remove();
554                 pci_stop_and_remove_bus_device(dev);
555                 pci_unlock_rescan_remove();
556         }
557
558         return NULL;
559 }
560
561 static void *eeh_pe_detach_dev(struct eeh_pe *pe, void *userdata)
562 {
563         struct eeh_dev *edev, *tmp;
564
565         eeh_pe_for_each_dev(pe, edev, tmp) {
566                 if (!(edev->mode & EEH_DEV_DISCONNECTED))
567                         continue;
568
569                 edev->mode &= ~(EEH_DEV_DISCONNECTED | EEH_DEV_IRQ_DISABLED);
570                 eeh_rmv_from_parent_pe(edev);
571         }
572
573         return NULL;
574 }
575
576 /*
577  * Explicitly clear PE's frozen state for PowerNV where
578  * we have frozen PE until BAR restore is completed. It's
579  * harmless to clear it for pSeries. To be consistent with
580  * PE reset (for 3 times), we try to clear the frozen state
581  * for 3 times as well.
582  */
583 static int eeh_clear_pe_frozen_state(struct eeh_pe *root, bool include_passed)
584 {
585         struct eeh_pe *pe;
586         int i;
587
588         eeh_for_each_pe(root, pe) {
589                 if (include_passed || !eeh_pe_passed(pe)) {
590                         for (i = 0; i < 3; i++)
591                                 if (!eeh_unfreeze_pe(pe))
592                                         break;
593                         if (i >= 3)
594                                 return -EIO;
595                 }
596         }
597         eeh_pe_state_clear(root, EEH_PE_ISOLATED, include_passed);
598         return 0;
599 }
600
601 int eeh_pe_reset_and_recover(struct eeh_pe *pe)
602 {
603         int ret;
604
605         /* Bail if the PE is being recovered */
606         if (pe->state & EEH_PE_RECOVERING)
607                 return 0;
608
609         /* Put the PE into recovery mode */
610         eeh_pe_state_mark(pe, EEH_PE_RECOVERING);
611
612         /* Save states */
613         eeh_pe_dev_traverse(pe, eeh_dev_save_state, NULL);
614
615         /* Issue reset */
616         ret = eeh_pe_reset_full(pe, true);
617         if (ret) {
618                 eeh_pe_state_clear(pe, EEH_PE_RECOVERING, true);
619                 return ret;
620         }
621
622         /* Unfreeze the PE */
623         ret = eeh_clear_pe_frozen_state(pe, true);
624         if (ret) {
625                 eeh_pe_state_clear(pe, EEH_PE_RECOVERING, true);
626                 return ret;
627         }
628
629         /* Restore device state */
630         eeh_pe_dev_traverse(pe, eeh_dev_restore_state, NULL);
631
632         /* Clear recovery mode */
633         eeh_pe_state_clear(pe, EEH_PE_RECOVERING, true);
634
635         return 0;
636 }
637
638 /**
639  * eeh_reset_device - Perform actual reset of a pci slot
640  * @driver_eeh_aware: Does the device's driver provide EEH support?
641  * @pe: EEH PE
642  * @bus: PCI bus corresponding to the isolcated slot
643  * @rmv_data: Optional, list to record removed devices
644  *
645  * This routine must be called to do reset on the indicated PE.
646  * During the reset, udev might be invoked because those affected
647  * PCI devices will be removed and then added.
648  */
649 static int eeh_reset_device(struct eeh_pe *pe, struct pci_bus *bus,
650                             struct eeh_rmv_data *rmv_data,
651                             bool driver_eeh_aware)
652 {
653         time64_t tstamp;
654         int cnt, rc;
655         struct eeh_dev *edev;
656         struct eeh_pe *tmp_pe;
657         bool any_passed = false;
658
659         eeh_for_each_pe(pe, tmp_pe)
660                 any_passed |= eeh_pe_passed(tmp_pe);
661
662         /* pcibios will clear the counter; save the value */
663         cnt = pe->freeze_count;
664         tstamp = pe->tstamp;
665
666         /*
667          * We don't remove the corresponding PE instances because
668          * we need the information afterwords. The attached EEH
669          * devices are expected to be attached soon when calling
670          * into pci_hp_add_devices().
671          */
672         eeh_pe_state_mark(pe, EEH_PE_KEEP);
673         if (any_passed || driver_eeh_aware || (pe->type & EEH_PE_VF)) {
674                 eeh_pe_dev_traverse(pe, eeh_rmv_device, rmv_data);
675         } else {
676                 pci_lock_rescan_remove();
677                 pci_hp_remove_devices(bus);
678                 pci_unlock_rescan_remove();
679         }
680
681         /*
682          * Reset the pci controller. (Asserts RST#; resets config space).
683          * Reconfigure bridges and devices. Don't try to bring the system
684          * up if the reset failed for some reason.
685          *
686          * During the reset, it's very dangerous to have uncontrolled PCI
687          * config accesses. So we prefer to block them. However, controlled
688          * PCI config accesses initiated from EEH itself are allowed.
689          */
690         rc = eeh_pe_reset_full(pe, false);
691         if (rc)
692                 return rc;
693
694         pci_lock_rescan_remove();
695
696         /* Restore PE */
697         eeh_ops->configure_bridge(pe);
698         eeh_pe_restore_bars(pe);
699
700         /* Clear frozen state */
701         rc = eeh_clear_pe_frozen_state(pe, false);
702         if (rc) {
703                 pci_unlock_rescan_remove();
704                 return rc;
705         }
706
707         /* Give the system 5 seconds to finish running the user-space
708          * hotplug shutdown scripts, e.g. ifdown for ethernet.  Yes,
709          * this is a hack, but if we don't do this, and try to bring
710          * the device up before the scripts have taken it down,
711          * potentially weird things happen.
712          */
713         if (!driver_eeh_aware || rmv_data->removed_dev_count) {
714                 pr_info("EEH: Sleep 5s ahead of %s hotplug\n",
715                         (driver_eeh_aware ? "partial" : "complete"));
716                 ssleep(5);
717
718                 /*
719                  * The EEH device is still connected with its parent
720                  * PE. We should disconnect it so the binding can be
721                  * rebuilt when adding PCI devices.
722                  */
723                 edev = list_first_entry(&pe->edevs, struct eeh_dev, entry);
724                 eeh_pe_traverse(pe, eeh_pe_detach_dev, NULL);
725                 if (pe->type & EEH_PE_VF) {
726                         eeh_add_virt_device(edev);
727                 } else {
728                         if (!driver_eeh_aware)
729                                 eeh_pe_state_clear(pe, EEH_PE_PRI_BUS, true);
730                         pci_hp_add_devices(bus);
731                 }
732         }
733         eeh_pe_state_clear(pe, EEH_PE_KEEP, true);
734
735         pe->tstamp = tstamp;
736         pe->freeze_count = cnt;
737
738         pci_unlock_rescan_remove();
739         return 0;
740 }
741
742 /* The longest amount of time to wait for a pci device
743  * to come back on line, in seconds.
744  */
745 #define MAX_WAIT_FOR_RECOVERY 300
746
747
748 /* Walks the PE tree after processing an event to remove any stale PEs.
749  *
750  * NB: This needs to be recursive to ensure the leaf PEs get removed
751  * before their parents do. Although this is possible to do recursively
752  * we don't since this is easier to read and we need to garantee
753  * the leaf nodes will be handled first.
754  */
755 static void eeh_pe_cleanup(struct eeh_pe *pe)
756 {
757         struct eeh_pe *child_pe, *tmp;
758
759         list_for_each_entry_safe(child_pe, tmp, &pe->child_list, child)
760                 eeh_pe_cleanup(child_pe);
761
762         if (pe->state & EEH_PE_KEEP)
763                 return;
764
765         if (!(pe->state & EEH_PE_INVALID))
766                 return;
767
768         if (list_empty(&pe->edevs) && list_empty(&pe->child_list)) {
769                 list_del(&pe->child);
770                 kfree(pe);
771         }
772 }
773
774 /**
775  * eeh_handle_normal_event - Handle EEH events on a specific PE
776  * @pe: EEH PE - which should not be used after we return, as it may
777  * have been invalidated.
778  *
779  * Attempts to recover the given PE.  If recovery fails or the PE has failed
780  * too many times, remove the PE.
781  *
782  * While PHB detects address or data parity errors on particular PCI
783  * slot, the associated PE will be frozen. Besides, DMA's occurring
784  * to wild addresses (which usually happen due to bugs in device
785  * drivers or in PCI adapter firmware) can cause EEH error. #SERR,
786  * #PERR or other misc PCI-related errors also can trigger EEH errors.
787  *
788  * Recovery process consists of unplugging the device driver (which
789  * generated hotplug events to userspace), then issuing a PCI #RST to
790  * the device, then reconfiguring the PCI config space for all bridges
791  * & devices under this slot, and then finally restarting the device
792  * drivers (which cause a second set of hotplug events to go out to
793  * userspace).
794  */
795 void eeh_handle_normal_event(struct eeh_pe *pe)
796 {
797         struct pci_bus *bus;
798         struct eeh_dev *edev, *tmp;
799         struct eeh_pe *tmp_pe;
800         int rc = 0;
801         enum pci_ers_result result = PCI_ERS_RESULT_NONE;
802         struct eeh_rmv_data rmv_data =
803                 {LIST_HEAD_INIT(rmv_data.removed_vf_list), 0};
804
805         bus = eeh_pe_bus_get(pe);
806         if (!bus) {
807                 pr_err("%s: Cannot find PCI bus for PHB#%x-PE#%x\n",
808                         __func__, pe->phb->global_number, pe->addr);
809                 return;
810         }
811
812         eeh_pe_update_time_stamp(pe);
813         pe->freeze_count++;
814         if (pe->freeze_count > eeh_max_freezes) {
815                 pr_err("EEH: PHB#%x-PE#%x has failed %d times in the last hour and has been permanently disabled.\n",
816                        pe->phb->global_number, pe->addr,
817                        pe->freeze_count);
818                 result = PCI_ERS_RESULT_DISCONNECT;
819         }
820
821         eeh_for_each_pe(pe, tmp_pe)
822                 eeh_pe_for_each_dev(tmp_pe, edev, tmp)
823                         edev->mode &= ~EEH_DEV_NO_HANDLER;
824
825         /* Walk the various device drivers attached to this slot through
826          * a reset sequence, giving each an opportunity to do what it needs
827          * to accomplish the reset.  Each child gets a report of the
828          * status ... if any child can't handle the reset, then the entire
829          * slot is dlpar removed and added.
830          *
831          * When the PHB is fenced, we have to issue a reset to recover from
832          * the error. Override the result if necessary to have partially
833          * hotplug for this case.
834          */
835         if (result != PCI_ERS_RESULT_DISCONNECT) {
836                 pr_warn("EEH: This PCI device has failed %d times in the last hour and will be permanently disabled after %d failures.\n",
837                         pe->freeze_count, eeh_max_freezes);
838                 pr_info("EEH: Notify device drivers to shutdown\n");
839                 eeh_set_channel_state(pe, pci_channel_io_frozen);
840                 eeh_set_irq_state(pe, false);
841                 eeh_pe_report("error_detected(IO frozen)", pe,
842                               eeh_report_error, &result);
843                 if ((pe->type & EEH_PE_PHB) &&
844                     result != PCI_ERS_RESULT_NONE &&
845                     result != PCI_ERS_RESULT_NEED_RESET)
846                         result = PCI_ERS_RESULT_NEED_RESET;
847         }
848
849         /* Get the current PCI slot state. This can take a long time,
850          * sometimes over 300 seconds for certain systems.
851          */
852         if (result != PCI_ERS_RESULT_DISCONNECT) {
853                 rc = eeh_wait_state(pe, MAX_WAIT_FOR_RECOVERY*1000);
854                 if (rc < 0 || rc == EEH_STATE_NOT_SUPPORT) {
855                         pr_warn("EEH: Permanent failure\n");
856                         result = PCI_ERS_RESULT_DISCONNECT;
857                 }
858         }
859
860         /* Since rtas may enable MMIO when posting the error log,
861          * don't post the error log until after all dev drivers
862          * have been informed.
863          */
864         if (result != PCI_ERS_RESULT_DISCONNECT) {
865                 pr_info("EEH: Collect temporary log\n");
866                 eeh_slot_error_detail(pe, EEH_LOG_TEMP);
867         }
868
869         /* If all device drivers were EEH-unaware, then shut
870          * down all of the device drivers, and hope they
871          * go down willingly, without panicing the system.
872          */
873         if (result == PCI_ERS_RESULT_NONE) {
874                 pr_info("EEH: Reset with hotplug activity\n");
875                 rc = eeh_reset_device(pe, bus, NULL, false);
876                 if (rc) {
877                         pr_warn("%s: Unable to reset, err=%d\n",
878                                 __func__, rc);
879                         result = PCI_ERS_RESULT_DISCONNECT;
880                 }
881         }
882
883         /* If all devices reported they can proceed, then re-enable MMIO */
884         if (result == PCI_ERS_RESULT_CAN_RECOVER) {
885                 pr_info("EEH: Enable I/O for affected devices\n");
886                 rc = eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
887
888                 if (rc < 0) {
889                         result = PCI_ERS_RESULT_DISCONNECT;
890                 } else if (rc) {
891                         result = PCI_ERS_RESULT_NEED_RESET;
892                 } else {
893                         pr_info("EEH: Notify device drivers to resume I/O\n");
894                         eeh_pe_report("mmio_enabled", pe,
895                                       eeh_report_mmio_enabled, &result);
896                 }
897         }
898
899         /* If all devices reported they can proceed, then re-enable DMA */
900         if (result == PCI_ERS_RESULT_CAN_RECOVER) {
901                 pr_info("EEH: Enabled DMA for affected devices\n");
902                 rc = eeh_pci_enable(pe, EEH_OPT_THAW_DMA);
903
904                 if (rc < 0) {
905                         result = PCI_ERS_RESULT_DISCONNECT;
906                 } else if (rc) {
907                         result = PCI_ERS_RESULT_NEED_RESET;
908                 } else {
909                         /*
910                          * We didn't do PE reset for the case. The PE
911                          * is still in frozen state. Clear it before
912                          * resuming the PE.
913                          */
914                         eeh_pe_state_clear(pe, EEH_PE_ISOLATED, true);
915                         result = PCI_ERS_RESULT_RECOVERED;
916                 }
917         }
918
919         /* If any device called out for a reset, then reset the slot */
920         if (result == PCI_ERS_RESULT_NEED_RESET) {
921                 pr_info("EEH: Reset without hotplug activity\n");
922                 rc = eeh_reset_device(pe, bus, &rmv_data, true);
923                 if (rc) {
924                         pr_warn("%s: Cannot reset, err=%d\n",
925                                 __func__, rc);
926                         result = PCI_ERS_RESULT_DISCONNECT;
927                 } else {
928                         result = PCI_ERS_RESULT_NONE;
929                         eeh_set_channel_state(pe, pci_channel_io_normal);
930                         eeh_set_irq_state(pe, true);
931                         eeh_pe_report("slot_reset", pe, eeh_report_reset,
932                                       &result);
933                 }
934         }
935
936         if ((result == PCI_ERS_RESULT_RECOVERED) ||
937             (result == PCI_ERS_RESULT_NONE)) {
938                 /*
939                  * For those hot removed VFs, we should add back them after PF
940                  * get recovered properly.
941                  */
942                 list_for_each_entry_safe(edev, tmp, &rmv_data.removed_vf_list,
943                                          rmv_entry) {
944                         eeh_add_virt_device(edev);
945                         list_del(&edev->rmv_entry);
946                 }
947
948                 /* Tell all device drivers that they can resume operations */
949                 pr_info("EEH: Notify device driver to resume\n");
950                 eeh_set_channel_state(pe, pci_channel_io_normal);
951                 eeh_set_irq_state(pe, true);
952                 eeh_pe_report("resume", pe, eeh_report_resume, NULL);
953                 eeh_for_each_pe(pe, tmp_pe) {
954                         eeh_pe_for_each_dev(tmp_pe, edev, tmp) {
955                                 edev->mode &= ~EEH_DEV_NO_HANDLER;
956                                 edev->in_error = false;
957                         }
958                 }
959
960                 pr_info("EEH: Recovery successful.\n");
961         } else  {
962                 /*
963                  * About 90% of all real-life EEH failures in the field
964                  * are due to poorly seated PCI cards. Only 10% or so are
965                  * due to actual, failed cards.
966                  */
967                 pr_err("EEH: Unable to recover from failure from PHB#%x-PE#%x.\n"
968                        "Please try reseating or replacing it\n",
969                         pe->phb->global_number, pe->addr);
970
971                 eeh_slot_error_detail(pe, EEH_LOG_PERM);
972
973                 /* Notify all devices that they're about to go down. */
974                 eeh_set_channel_state(pe, pci_channel_io_perm_failure);
975                 eeh_set_irq_state(pe, false);
976                 eeh_pe_report("error_detected(permanent failure)", pe,
977                               eeh_report_failure, NULL);
978
979                 /* Mark the PE to be removed permanently */
980                 eeh_pe_state_mark(pe, EEH_PE_REMOVED);
981
982                 /*
983                  * Shut down the device drivers for good. We mark
984                  * all removed devices correctly to avoid access
985                  * the their PCI config any more.
986                  */
987                 if (pe->type & EEH_PE_VF) {
988                         eeh_pe_dev_traverse(pe, eeh_rmv_device, NULL);
989                         eeh_pe_dev_mode_mark(pe, EEH_DEV_REMOVED);
990                 } else {
991                         eeh_pe_state_clear(pe, EEH_PE_PRI_BUS, true);
992                         eeh_pe_dev_mode_mark(pe, EEH_DEV_REMOVED);
993
994                         pci_lock_rescan_remove();
995                         pci_hp_remove_devices(bus);
996                         pci_unlock_rescan_remove();
997                         /* The passed PE should no longer be used */
998                         return;
999                 }
1000         }
1001
1002         /*
1003          * Clean up any PEs without devices. While marked as EEH_PE_RECOVERYING
1004          * we don't want to modify the PE tree structure so we do it here.
1005          */
1006         eeh_pe_cleanup(pe);
1007         eeh_pe_state_clear(pe, EEH_PE_RECOVERING, true);
1008 }
1009
1010 /**
1011  * eeh_handle_special_event - Handle EEH events without a specific failing PE
1012  *
1013  * Called when an EEH event is detected but can't be narrowed down to a
1014  * specific PE.  Iterates through possible failures and handles them as
1015  * necessary.
1016  */
1017 void eeh_handle_special_event(void)
1018 {
1019         struct eeh_pe *pe, *phb_pe, *tmp_pe;
1020         struct eeh_dev *edev, *tmp_edev;
1021         struct pci_bus *bus;
1022         struct pci_controller *hose;
1023         unsigned long flags;
1024         int rc;
1025
1026
1027         do {
1028                 rc = eeh_ops->next_error(&pe);
1029
1030                 switch (rc) {
1031                 case EEH_NEXT_ERR_DEAD_IOC:
1032                         /* Mark all PHBs in dead state */
1033                         eeh_serialize_lock(&flags);
1034
1035                         /* Purge all events */
1036                         eeh_remove_event(NULL, true);
1037
1038                         list_for_each_entry(hose, &hose_list, list_node) {
1039                                 phb_pe = eeh_phb_pe_get(hose);
1040                                 if (!phb_pe) continue;
1041
1042                                 eeh_pe_mark_isolated(phb_pe);
1043                         }
1044
1045                         eeh_serialize_unlock(flags);
1046
1047                         break;
1048                 case EEH_NEXT_ERR_FROZEN_PE:
1049                 case EEH_NEXT_ERR_FENCED_PHB:
1050                 case EEH_NEXT_ERR_DEAD_PHB:
1051                         /* Mark the PE in fenced state */
1052                         eeh_serialize_lock(&flags);
1053
1054                         /* Purge all events of the PHB */
1055                         eeh_remove_event(pe, true);
1056
1057                         if (rc != EEH_NEXT_ERR_DEAD_PHB)
1058                                 eeh_pe_state_mark(pe, EEH_PE_RECOVERING);
1059                         eeh_pe_mark_isolated(pe);
1060
1061                         eeh_serialize_unlock(flags);
1062
1063                         break;
1064                 case EEH_NEXT_ERR_NONE:
1065                         return;
1066                 default:
1067                         pr_warn("%s: Invalid value %d from next_error()\n",
1068                                 __func__, rc);
1069                         return;
1070                 }
1071
1072                 /*
1073                  * For fenced PHB and frozen PE, it's handled as normal
1074                  * event. We have to remove the affected PHBs for dead
1075                  * PHB and IOC
1076                  */
1077                 if (rc == EEH_NEXT_ERR_FROZEN_PE ||
1078                     rc == EEH_NEXT_ERR_FENCED_PHB) {
1079                         eeh_pe_state_mark(pe, EEH_PE_RECOVERING);
1080                         eeh_handle_normal_event(pe);
1081                 } else {
1082                         pci_lock_rescan_remove();
1083                         list_for_each_entry(hose, &hose_list, list_node) {
1084                                 phb_pe = eeh_phb_pe_get(hose);
1085                                 if (!phb_pe ||
1086                                     !(phb_pe->state & EEH_PE_ISOLATED) ||
1087                                     (phb_pe->state & EEH_PE_RECOVERING))
1088                                         continue;
1089
1090                                 eeh_for_each_pe(pe, tmp_pe)
1091                                         eeh_pe_for_each_dev(tmp_pe, edev, tmp_edev)
1092                                                 edev->mode &= ~EEH_DEV_NO_HANDLER;
1093
1094                                 /* Notify all devices to be down */
1095                                 eeh_pe_state_clear(pe, EEH_PE_PRI_BUS, true);
1096                                 eeh_set_channel_state(pe, pci_channel_io_perm_failure);
1097                                 eeh_pe_report(
1098                                         "error_detected(permanent failure)", pe,
1099                                         eeh_report_failure, NULL);
1100                                 bus = eeh_pe_bus_get(phb_pe);
1101                                 if (!bus) {
1102                                         pr_err("%s: Cannot find PCI bus for "
1103                                                "PHB#%x-PE#%x\n",
1104                                                __func__,
1105                                                pe->phb->global_number,
1106                                                pe->addr);
1107                                         break;
1108                                 }
1109                                 pci_hp_remove_devices(bus);
1110                         }
1111                         pci_unlock_rescan_remove();
1112                 }
1113
1114                 /*
1115                  * If we have detected dead IOC, we needn't proceed
1116                  * any more since all PHBs would have been removed
1117                  */
1118                 if (rc == EEH_NEXT_ERR_DEAD_IOC)
1119                         break;
1120         } while (rc != EEH_NEXT_ERR_NONE);
1121 }