1317f06deSGavin Shan /* 2317f06deSGavin Shan * PCI Error Recovery Driver for RPA-compliant PPC64 platform. 3317f06deSGavin Shan * Copyright IBM Corp. 2004 2005 4317f06deSGavin Shan * Copyright Linas Vepstas <linas@linas.org> 2004, 2005 5317f06deSGavin Shan * 6317f06deSGavin Shan * All rights reserved. 7317f06deSGavin Shan * 8317f06deSGavin Shan * This program is free software; you can redistribute it and/or modify 9317f06deSGavin Shan * it under the terms of the GNU General Public License as published by 10317f06deSGavin Shan * the Free Software Foundation; either version 2 of the License, or (at 11317f06deSGavin Shan * your option) any later version. 12317f06deSGavin Shan * 13317f06deSGavin Shan * This program is distributed in the hope that it will be useful, but 14317f06deSGavin Shan * WITHOUT ANY WARRANTY; without even the implied warranty of 15317f06deSGavin Shan * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or 16317f06deSGavin Shan * NON INFRINGEMENT. See the GNU General Public License for more 17317f06deSGavin Shan * details. 18317f06deSGavin Shan * 19317f06deSGavin Shan * You should have received a copy of the GNU General Public License 20317f06deSGavin Shan * along with this program; if not, write to the Free Software 21317f06deSGavin Shan * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 22317f06deSGavin Shan * 23317f06deSGavin Shan * Send comments and feedback to Linas Vepstas <linas@austin.ibm.com> 24317f06deSGavin Shan */ 25317f06deSGavin Shan #include <linux/delay.h> 26317f06deSGavin Shan #include <linux/interrupt.h> 27317f06deSGavin Shan #include <linux/irq.h> 28317f06deSGavin Shan #include <linux/module.h> 29317f06deSGavin Shan #include <linux/pci.h> 30317f06deSGavin Shan #include <asm/eeh.h> 31317f06deSGavin Shan #include <asm/eeh_event.h> 32317f06deSGavin Shan #include <asm/ppc-pci.h> 33317f06deSGavin Shan #include <asm/pci-bridge.h> 34317f06deSGavin Shan #include <asm/prom.h> 35317f06deSGavin Shan #include <asm/rtas.h> 36317f06deSGavin Shan 3767086e32SWei Yang struct eeh_rmv_data { 381c5c533bSSam Bobroff struct list_head removed_vf_list; 391c5c533bSSam Bobroff int removed_dev_count; 4067086e32SWei Yang }; 4167086e32SWei Yang 4230424e38SSam Bobroff static int eeh_result_priority(enum pci_ers_result result) 4330424e38SSam Bobroff { 4430424e38SSam Bobroff switch (result) { 4530424e38SSam Bobroff case PCI_ERS_RESULT_NONE: 4630424e38SSam Bobroff return 1; 4730424e38SSam Bobroff case PCI_ERS_RESULT_NO_AER_DRIVER: 4830424e38SSam Bobroff return 2; 4930424e38SSam Bobroff case PCI_ERS_RESULT_RECOVERED: 5030424e38SSam Bobroff return 3; 5130424e38SSam Bobroff case PCI_ERS_RESULT_CAN_RECOVER: 5230424e38SSam Bobroff return 4; 5330424e38SSam Bobroff case PCI_ERS_RESULT_DISCONNECT: 5430424e38SSam Bobroff return 5; 5530424e38SSam Bobroff case PCI_ERS_RESULT_NEED_RESET: 5630424e38SSam Bobroff return 6; 5730424e38SSam Bobroff default: 5830424e38SSam Bobroff WARN_ONCE(1, "Unknown pci_ers_result value: %d\n", (int)result); 5930424e38SSam Bobroff return 0; 6030424e38SSam Bobroff } 6130424e38SSam Bobroff }; 6230424e38SSam Bobroff 63c36c5ffdSBreno Leitao static const char *pci_ers_result_name(enum pci_ers_result result) 6420b34497SSam Bobroff { 6520b34497SSam Bobroff switch (result) { 6620b34497SSam Bobroff case PCI_ERS_RESULT_NONE: 6720b34497SSam Bobroff return "none"; 6820b34497SSam Bobroff case PCI_ERS_RESULT_CAN_RECOVER: 6920b34497SSam Bobroff return "can recover"; 7020b34497SSam Bobroff case PCI_ERS_RESULT_NEED_RESET: 7120b34497SSam Bobroff return "need reset"; 7220b34497SSam Bobroff case PCI_ERS_RESULT_DISCONNECT: 7320b34497SSam Bobroff return "disconnect"; 7420b34497SSam Bobroff case PCI_ERS_RESULT_RECOVERED: 7520b34497SSam Bobroff return "recovered"; 7620b34497SSam Bobroff case PCI_ERS_RESULT_NO_AER_DRIVER: 7720b34497SSam Bobroff return "no AER driver"; 7820b34497SSam Bobroff default: 7920b34497SSam Bobroff WARN_ONCE(1, "Unknown result type: %d\n", (int)result); 8020b34497SSam Bobroff return "unknown"; 8120b34497SSam Bobroff } 8220b34497SSam Bobroff }; 8320b34497SSam Bobroff 8430424e38SSam Bobroff static enum pci_ers_result pci_ers_merge_result(enum pci_ers_result old, 8530424e38SSam Bobroff enum pci_ers_result new) 8630424e38SSam Bobroff { 8730424e38SSam Bobroff if (eeh_result_priority(new) > eeh_result_priority(old)) 8830424e38SSam Bobroff return new; 8930424e38SSam Bobroff return old; 9030424e38SSam Bobroff } 9130424e38SSam Bobroff 92e2b810d5SSam Bobroff static bool eeh_dev_removed(struct eeh_dev *edev) 93e2b810d5SSam Bobroff { 94e2b810d5SSam Bobroff return !edev || (edev->mode & EEH_DEV_REMOVED); 95e2b810d5SSam Bobroff } 96e2b810d5SSam Bobroff 97e2b810d5SSam Bobroff static bool eeh_edev_actionable(struct eeh_dev *edev) 98e2b810d5SSam Bobroff { 99e2b810d5SSam Bobroff return (edev->pdev && !eeh_dev_removed(edev) && 100e2b810d5SSam Bobroff !eeh_pe_passed(edev->pe)); 101e2b810d5SSam Bobroff } 102e2b810d5SSam Bobroff 103317f06deSGavin Shan /** 104317f06deSGavin Shan * eeh_pcid_get - Get the PCI device driver 105317f06deSGavin Shan * @pdev: PCI device 106317f06deSGavin Shan * 107317f06deSGavin Shan * The function is used to retrieve the PCI device driver for 108317f06deSGavin Shan * the indicated PCI device. Besides, we will increase the reference 109317f06deSGavin Shan * of the PCI device driver to prevent that being unloaded on 110317f06deSGavin Shan * the fly. Otherwise, kernel crash would be seen. 111317f06deSGavin Shan */ 112317f06deSGavin Shan static inline struct pci_driver *eeh_pcid_get(struct pci_dev *pdev) 113317f06deSGavin Shan { 114317f06deSGavin Shan if (!pdev || !pdev->driver) 115317f06deSGavin Shan return NULL; 116317f06deSGavin Shan 117317f06deSGavin Shan if (!try_module_get(pdev->driver->driver.owner)) 118317f06deSGavin Shan return NULL; 119317f06deSGavin Shan 120317f06deSGavin Shan return pdev->driver; 121317f06deSGavin Shan } 122317f06deSGavin Shan 123317f06deSGavin Shan /** 124317f06deSGavin Shan * eeh_pcid_put - Dereference on the PCI device driver 125317f06deSGavin Shan * @pdev: PCI device 126317f06deSGavin Shan * 127317f06deSGavin Shan * The function is called to do dereference on the PCI device 128317f06deSGavin Shan * driver of the indicated PCI device. 129317f06deSGavin Shan */ 130317f06deSGavin Shan static inline void eeh_pcid_put(struct pci_dev *pdev) 131317f06deSGavin Shan { 132317f06deSGavin Shan if (!pdev || !pdev->driver) 133317f06deSGavin Shan return; 134317f06deSGavin Shan 135317f06deSGavin Shan module_put(pdev->driver->driver.owner); 136317f06deSGavin Shan } 137317f06deSGavin Shan 138317f06deSGavin Shan /** 139317f06deSGavin Shan * eeh_disable_irq - Disable interrupt for the recovering device 140317f06deSGavin Shan * @dev: PCI device 141317f06deSGavin Shan * 142317f06deSGavin Shan * This routine must be called when reporting temporary or permanent 143317f06deSGavin Shan * error to the particular PCI device to disable interrupt of that 144317f06deSGavin Shan * device. If the device has enabled MSI or MSI-X interrupt, we needn't 145317f06deSGavin Shan * do real work because EEH should freeze DMA transfers for those PCI 146317f06deSGavin Shan * devices encountering EEH errors, which includes MSI or MSI-X. 147317f06deSGavin Shan */ 148010acfa1SSam Bobroff static void eeh_disable_irq(struct eeh_dev *edev) 149317f06deSGavin Shan { 150317f06deSGavin Shan /* Don't disable MSI and MSI-X interrupts. They are 151317f06deSGavin Shan * effectively disabled by the DMA Stopped state 152317f06deSGavin Shan * when an EEH error occurs. 153317f06deSGavin Shan */ 154010acfa1SSam Bobroff if (edev->pdev->msi_enabled || edev->pdev->msix_enabled) 155317f06deSGavin Shan return; 156317f06deSGavin Shan 157010acfa1SSam Bobroff if (!irq_has_action(edev->pdev->irq)) 158317f06deSGavin Shan return; 159317f06deSGavin Shan 160317f06deSGavin Shan edev->mode |= EEH_DEV_IRQ_DISABLED; 161010acfa1SSam Bobroff disable_irq_nosync(edev->pdev->irq); 162317f06deSGavin Shan } 163317f06deSGavin Shan 164317f06deSGavin Shan /** 165317f06deSGavin Shan * eeh_enable_irq - Enable interrupt for the recovering device 166317f06deSGavin Shan * @dev: PCI device 167317f06deSGavin Shan * 168317f06deSGavin Shan * This routine must be called to enable interrupt while failed 169317f06deSGavin Shan * device could be resumed. 170317f06deSGavin Shan */ 171010acfa1SSam Bobroff static void eeh_enable_irq(struct eeh_dev *edev) 172317f06deSGavin Shan { 173317f06deSGavin Shan if ((edev->mode) & EEH_DEV_IRQ_DISABLED) { 174317f06deSGavin Shan edev->mode &= ~EEH_DEV_IRQ_DISABLED; 175b8a9a11bSThomas Gleixner /* 176b8a9a11bSThomas Gleixner * FIXME !!!!! 177b8a9a11bSThomas Gleixner * 178b8a9a11bSThomas Gleixner * This is just ass backwards. This maze has 179b8a9a11bSThomas Gleixner * unbalanced irq_enable/disable calls. So instead of 180b8a9a11bSThomas Gleixner * finding the root cause it works around the warning 181b8a9a11bSThomas Gleixner * in the irq_enable code by conditionally calling 182b8a9a11bSThomas Gleixner * into it. 183b8a9a11bSThomas Gleixner * 184b8a9a11bSThomas Gleixner * That's just wrong.The warning in the core code is 185027dfac6SMichael Ellerman * there to tell people to fix their asymmetries in 186b8a9a11bSThomas Gleixner * their own code, not by abusing the core information 187b8a9a11bSThomas Gleixner * to avoid it. 188b8a9a11bSThomas Gleixner * 189b8a9a11bSThomas Gleixner * I so wish that the assymetry would be the other way 190b8a9a11bSThomas Gleixner * round and a few more irq_disable calls render that 191b8a9a11bSThomas Gleixner * shit unusable forever. 192b8a9a11bSThomas Gleixner * 193b8a9a11bSThomas Gleixner * tglx 194b8a9a11bSThomas Gleixner */ 195010acfa1SSam Bobroff if (irqd_irq_disabled(irq_get_irq_data(edev->pdev->irq))) 196010acfa1SSam Bobroff enable_irq(edev->pdev->irq); 197317f06deSGavin Shan } 19857310c3cSThomas Gleixner } 199317f06deSGavin Shan 200cef50c67SSam Bobroff static void eeh_dev_save_state(struct eeh_dev *edev, void *userdata) 2015cfb20b9SGavin Shan { 2025cfb20b9SGavin Shan struct pci_dev *pdev; 2035cfb20b9SGavin Shan 2045cfb20b9SGavin Shan if (!edev) 205cef50c67SSam Bobroff return; 2065cfb20b9SGavin Shan 2075a0cdbfdSGavin Shan /* 2085a0cdbfdSGavin Shan * We cannot access the config space on some adapters. 2095a0cdbfdSGavin Shan * Otherwise, it will cause fenced PHB. We don't save 2105a0cdbfdSGavin Shan * the content in their config space and will restore 2115a0cdbfdSGavin Shan * from the initial config space saved when the EEH 2125a0cdbfdSGavin Shan * device is created. 2135a0cdbfdSGavin Shan */ 2145a0cdbfdSGavin Shan if (edev->pe && (edev->pe->state & EEH_PE_CFG_RESTRICTED)) 215cef50c67SSam Bobroff return; 2165a0cdbfdSGavin Shan 2175cfb20b9SGavin Shan pdev = eeh_dev_to_pci_dev(edev); 2185cfb20b9SGavin Shan if (!pdev) 219cef50c67SSam Bobroff return; 2205cfb20b9SGavin Shan 2215cfb20b9SGavin Shan pci_save_state(pdev); 2225cfb20b9SGavin Shan } 2235cfb20b9SGavin Shan 22447cc8c1cSSam Bobroff static void eeh_set_channel_state(struct eeh_pe *root, enum pci_channel_state s) 22547cc8c1cSSam Bobroff { 22647cc8c1cSSam Bobroff struct eeh_pe *pe; 22747cc8c1cSSam Bobroff struct eeh_dev *edev, *tmp; 22847cc8c1cSSam Bobroff 22947cc8c1cSSam Bobroff eeh_for_each_pe(root, pe) 23047cc8c1cSSam Bobroff eeh_pe_for_each_dev(pe, edev, tmp) 23147cc8c1cSSam Bobroff if (eeh_edev_actionable(edev)) 23247cc8c1cSSam Bobroff edev->pdev->error_state = s; 23347cc8c1cSSam Bobroff } 23447cc8c1cSSam Bobroff 235010acfa1SSam Bobroff static void eeh_set_irq_state(struct eeh_pe *root, bool enable) 236010acfa1SSam Bobroff { 237010acfa1SSam Bobroff struct eeh_pe *pe; 238010acfa1SSam Bobroff struct eeh_dev *edev, *tmp; 239010acfa1SSam Bobroff 240010acfa1SSam Bobroff eeh_for_each_pe(root, pe) { 241010acfa1SSam Bobroff eeh_pe_for_each_dev(pe, edev, tmp) { 242010acfa1SSam Bobroff if (!eeh_edev_actionable(edev)) 243010acfa1SSam Bobroff continue; 244010acfa1SSam Bobroff 245010acfa1SSam Bobroff if (!eeh_pcid_get(edev->pdev)) 246010acfa1SSam Bobroff continue; 247010acfa1SSam Bobroff 248010acfa1SSam Bobroff if (enable) 249010acfa1SSam Bobroff eeh_enable_irq(edev); 250010acfa1SSam Bobroff else 251010acfa1SSam Bobroff eeh_disable_irq(edev); 252010acfa1SSam Bobroff 253010acfa1SSam Bobroff eeh_pcid_put(edev->pdev); 254010acfa1SSam Bobroff } 255010acfa1SSam Bobroff } 256010acfa1SSam Bobroff } 257010acfa1SSam Bobroff 25820b34497SSam Bobroff typedef enum pci_ers_result (*eeh_report_fn)(struct eeh_dev *, 2592e255051SSam Bobroff struct pci_dev *, 26020b34497SSam Bobroff struct pci_driver *); 26120b34497SSam Bobroff static void eeh_pe_report_edev(struct eeh_dev *edev, eeh_report_fn fn, 26220b34497SSam Bobroff enum pci_ers_result *result) 26320b34497SSam Bobroff { 2642e255051SSam Bobroff struct pci_dev *pdev; 26520b34497SSam Bobroff struct pci_driver *driver; 26620b34497SSam Bobroff enum pci_ers_result new_result; 26720b34497SSam Bobroff 2682e255051SSam Bobroff pci_lock_rescan_remove(); 2692e255051SSam Bobroff pdev = edev->pdev; 2702e255051SSam Bobroff if (pdev) 2712e255051SSam Bobroff get_device(&pdev->dev); 2722e255051SSam Bobroff pci_unlock_rescan_remove(); 2732e255051SSam Bobroff if (!pdev) { 274bcbe3730SSam Bobroff eeh_edev_info(edev, "no device"); 275bcbe3730SSam Bobroff return; 276bcbe3730SSam Bobroff } 2772e255051SSam Bobroff device_lock(&pdev->dev); 27820b34497SSam Bobroff if (eeh_edev_actionable(edev)) { 2792e255051SSam Bobroff driver = eeh_pcid_get(pdev); 28020b34497SSam Bobroff 28120b34497SSam Bobroff if (!driver) 28220b34497SSam Bobroff eeh_edev_info(edev, "no driver"); 28320b34497SSam Bobroff else if (!driver->err_handler) 28420b34497SSam Bobroff eeh_edev_info(edev, "driver not EEH aware"); 28520b34497SSam Bobroff else if (edev->mode & EEH_DEV_NO_HANDLER) 28620b34497SSam Bobroff eeh_edev_info(edev, "driver bound too late"); 28720b34497SSam Bobroff else { 2882e255051SSam Bobroff new_result = fn(edev, pdev, driver); 28920b34497SSam Bobroff eeh_edev_info(edev, "%s driver reports: '%s'", 29020b34497SSam Bobroff driver->name, 29120b34497SSam Bobroff pci_ers_result_name(new_result)); 29220b34497SSam Bobroff if (result) 29320b34497SSam Bobroff *result = pci_ers_merge_result(*result, 29420b34497SSam Bobroff new_result); 29520b34497SSam Bobroff } 29620b34497SSam Bobroff if (driver) 2972e255051SSam Bobroff eeh_pcid_put(pdev); 29820b34497SSam Bobroff } else { 2992e255051SSam Bobroff eeh_edev_info(edev, "not actionable (%d,%d,%d)", !!pdev, 30020b34497SSam Bobroff !eeh_dev_removed(edev), !eeh_pe_passed(edev->pe)); 30120b34497SSam Bobroff } 3022e255051SSam Bobroff device_unlock(&pdev->dev); 3032e255051SSam Bobroff if (edev->pdev != pdev) 3042e255051SSam Bobroff eeh_edev_warn(edev, "Device changed during processing!\n"); 3052e255051SSam Bobroff put_device(&pdev->dev); 30620b34497SSam Bobroff } 30720b34497SSam Bobroff 30820b34497SSam Bobroff static void eeh_pe_report(const char *name, struct eeh_pe *root, 30920b34497SSam Bobroff eeh_report_fn fn, enum pci_ers_result *result) 31020b34497SSam Bobroff { 31120b34497SSam Bobroff struct eeh_pe *pe; 31220b34497SSam Bobroff struct eeh_dev *edev, *tmp; 31320b34497SSam Bobroff 31420b34497SSam Bobroff pr_info("EEH: Beginning: '%s'\n", name); 31520b34497SSam Bobroff eeh_for_each_pe(root, pe) eeh_pe_for_each_dev(pe, edev, tmp) 31620b34497SSam Bobroff eeh_pe_report_edev(edev, fn, result); 31720b34497SSam Bobroff if (result) 31820b34497SSam Bobroff pr_info("EEH: Finished:'%s' with aggregate recovery state:'%s'\n", 31920b34497SSam Bobroff name, pci_ers_result_name(*result)); 32020b34497SSam Bobroff else 32120b34497SSam Bobroff pr_info("EEH: Finished:'%s'", name); 32220b34497SSam Bobroff } 32320b34497SSam Bobroff 324317f06deSGavin Shan /** 325317f06deSGavin Shan * eeh_report_error - Report pci error to each device driver 32620b34497SSam Bobroff * @edev: eeh device 32720b34497SSam Bobroff * @driver: device's PCI driver 328317f06deSGavin Shan * 32920b34497SSam Bobroff * Report an EEH error to each device driver. 330317f06deSGavin Shan */ 33120b34497SSam Bobroff static enum pci_ers_result eeh_report_error(struct eeh_dev *edev, 3322e255051SSam Bobroff struct pci_dev *pdev, 33320b34497SSam Bobroff struct pci_driver *driver) 334317f06deSGavin Shan { 33520b34497SSam Bobroff enum pci_ers_result rc; 336317f06deSGavin Shan 33720b34497SSam Bobroff if (!driver->err_handler->error_detected) 33820b34497SSam Bobroff return PCI_ERS_RESULT_NONE; 339f0295e04SMichael Neuling 34020b34497SSam Bobroff eeh_edev_info(edev, "Invoking %s->error_detected(IO frozen)", 34120b34497SSam Bobroff driver->name); 3422e255051SSam Bobroff rc = driver->err_handler->error_detected(pdev, pci_channel_io_frozen); 343317f06deSGavin Shan 34467086e32SWei Yang edev->in_error = true; 3452e255051SSam Bobroff pci_uevent_ers(pdev, PCI_ERS_RESULT_NONE); 34620b34497SSam Bobroff return rc; 347317f06deSGavin Shan } 348317f06deSGavin Shan 349317f06deSGavin Shan /** 350317f06deSGavin Shan * eeh_report_mmio_enabled - Tell drivers that MMIO has been enabled 35120b34497SSam Bobroff * @edev: eeh device 35220b34497SSam Bobroff * @driver: device's PCI driver 353317f06deSGavin Shan * 354317f06deSGavin Shan * Tells each device driver that IO ports, MMIO and config space I/O 35520b34497SSam Bobroff * are now enabled. 356317f06deSGavin Shan */ 35720b34497SSam Bobroff static enum pci_ers_result eeh_report_mmio_enabled(struct eeh_dev *edev, 3582e255051SSam Bobroff struct pci_dev *pdev, 35920b34497SSam Bobroff struct pci_driver *driver) 360317f06deSGavin Shan { 36120b34497SSam Bobroff if (!driver->err_handler->mmio_enabled) 36220b34497SSam Bobroff return PCI_ERS_RESULT_NONE; 36320b34497SSam Bobroff eeh_edev_info(edev, "Invoking %s->mmio_enabled()", driver->name); 3642e255051SSam Bobroff return driver->err_handler->mmio_enabled(pdev); 365317f06deSGavin Shan } 366317f06deSGavin Shan 367317f06deSGavin Shan /** 368317f06deSGavin Shan * eeh_report_reset - Tell device that slot has been reset 36920b34497SSam Bobroff * @edev: eeh device 37020b34497SSam Bobroff * @driver: device's PCI driver 371317f06deSGavin Shan * 372317f06deSGavin Shan * This routine must be called while EEH tries to reset particular 373317f06deSGavin Shan * PCI device so that the associated PCI device driver could take 374317f06deSGavin Shan * some actions, usually to save data the driver needs so that the 375317f06deSGavin Shan * driver can work again while the device is recovered. 376317f06deSGavin Shan */ 37720b34497SSam Bobroff static enum pci_ers_result eeh_report_reset(struct eeh_dev *edev, 3782e255051SSam Bobroff struct pci_dev *pdev, 37920b34497SSam Bobroff struct pci_driver *driver) 380317f06deSGavin Shan { 38120b34497SSam Bobroff if (!driver->err_handler->slot_reset || !edev->in_error) 38220b34497SSam Bobroff return PCI_ERS_RESULT_NONE; 38320b34497SSam Bobroff eeh_edev_info(edev, "Invoking %s->slot_reset()", driver->name); 3842e255051SSam Bobroff return driver->err_handler->slot_reset(pdev); 385317f06deSGavin Shan } 386317f06deSGavin Shan 387cef50c67SSam Bobroff static void eeh_dev_restore_state(struct eeh_dev *edev, void *userdata) 3885cfb20b9SGavin Shan { 3895cfb20b9SGavin Shan struct pci_dev *pdev; 3905cfb20b9SGavin Shan 3915cfb20b9SGavin Shan if (!edev) 392cef50c67SSam Bobroff return; 3935cfb20b9SGavin Shan 3945a0cdbfdSGavin Shan /* 3955a0cdbfdSGavin Shan * The content in the config space isn't saved because 3965a0cdbfdSGavin Shan * the blocked config space on some adapters. We have 3975a0cdbfdSGavin Shan * to restore the initial saved config space when the 3985a0cdbfdSGavin Shan * EEH device is created. 3995a0cdbfdSGavin Shan */ 4005a0cdbfdSGavin Shan if (edev->pe && (edev->pe->state & EEH_PE_CFG_RESTRICTED)) { 40180e65b00SSam Bobroff if (list_is_last(&edev->entry, &edev->pe->edevs)) 4025a0cdbfdSGavin Shan eeh_pe_restore_bars(edev->pe); 4035a0cdbfdSGavin Shan 404cef50c67SSam Bobroff return; 4055a0cdbfdSGavin Shan } 4065a0cdbfdSGavin Shan 4075cfb20b9SGavin Shan pdev = eeh_dev_to_pci_dev(edev); 4085cfb20b9SGavin Shan if (!pdev) 409cef50c67SSam Bobroff return; 4105cfb20b9SGavin Shan 4115cfb20b9SGavin Shan pci_restore_state(pdev); 4125cfb20b9SGavin Shan } 4135cfb20b9SGavin Shan 414317f06deSGavin Shan /** 415317f06deSGavin Shan * eeh_report_resume - Tell device to resume normal operations 41620b34497SSam Bobroff * @edev: eeh device 41720b34497SSam Bobroff * @driver: device's PCI driver 418317f06deSGavin Shan * 419317f06deSGavin Shan * This routine must be called to notify the device driver that it 420317f06deSGavin Shan * could resume so that the device driver can do some initialization 421317f06deSGavin Shan * to make the recovered device work again. 422317f06deSGavin Shan */ 42320b34497SSam Bobroff static enum pci_ers_result eeh_report_resume(struct eeh_dev *edev, 4242e255051SSam Bobroff struct pci_dev *pdev, 42520b34497SSam Bobroff struct pci_driver *driver) 426317f06deSGavin Shan { 42720b34497SSam Bobroff if (!driver->err_handler->resume || !edev->in_error) 42820b34497SSam Bobroff return PCI_ERS_RESULT_NONE; 429317f06deSGavin Shan 43020b34497SSam Bobroff eeh_edev_info(edev, "Invoking %s->resume()", driver->name); 4312e255051SSam Bobroff driver->err_handler->resume(pdev); 432f0295e04SMichael Neuling 43320b34497SSam Bobroff pci_uevent_ers(edev->pdev, PCI_ERS_RESULT_RECOVERED); 434856e1eb9SBryant G. Ly #ifdef CONFIG_PCI_IOV 435521ca5a9SJuan J. Alvarez if (eeh_ops->notify_resume && eeh_dev_to_pdn(edev)) 436856e1eb9SBryant G. Ly eeh_ops->notify_resume(eeh_dev_to_pdn(edev)); 437856e1eb9SBryant G. Ly #endif 43820b34497SSam Bobroff return PCI_ERS_RESULT_NONE; 439317f06deSGavin Shan } 440317f06deSGavin Shan 441317f06deSGavin Shan /** 442317f06deSGavin Shan * eeh_report_failure - Tell device driver that device is dead. 44320b34497SSam Bobroff * @edev: eeh device 44420b34497SSam Bobroff * @driver: device's PCI driver 445317f06deSGavin Shan * 446317f06deSGavin Shan * This informs the device driver that the device is permanently 447317f06deSGavin Shan * dead, and that no further recovery attempts will be made on it. 448317f06deSGavin Shan */ 44920b34497SSam Bobroff static enum pci_ers_result eeh_report_failure(struct eeh_dev *edev, 4502e255051SSam Bobroff struct pci_dev *pdev, 45120b34497SSam Bobroff struct pci_driver *driver) 452317f06deSGavin Shan { 45320b34497SSam Bobroff enum pci_ers_result rc; 454317f06deSGavin Shan 45520b34497SSam Bobroff if (!driver->err_handler->error_detected) 45620b34497SSam Bobroff return PCI_ERS_RESULT_NONE; 457f0295e04SMichael Neuling 45820b34497SSam Bobroff eeh_edev_info(edev, "Invoking %s->error_detected(permanent failure)", 45920b34497SSam Bobroff driver->name); 4602e255051SSam Bobroff rc = driver->err_handler->error_detected(pdev, 46120b34497SSam Bobroff pci_channel_io_perm_failure); 462317f06deSGavin Shan 4632e255051SSam Bobroff pci_uevent_ers(pdev, PCI_ERS_RESULT_DISCONNECT); 46420b34497SSam Bobroff return rc; 465317f06deSGavin Shan } 466317f06deSGavin Shan 467bf773df9SSam Bobroff static void *eeh_add_virt_device(struct eeh_dev *edev) 46867086e32SWei Yang { 46967086e32SWei Yang struct pci_driver *driver; 47067086e32SWei Yang struct pci_dev *dev = eeh_dev_to_pci_dev(edev); 47167086e32SWei Yang 47267086e32SWei Yang if (!(edev->physfn)) { 4731ff8f36fSSam Bobroff eeh_edev_warn(edev, "Not for VF\n"); 47467086e32SWei Yang return NULL; 47567086e32SWei Yang } 47667086e32SWei Yang 47767086e32SWei Yang driver = eeh_pcid_get(dev); 47867086e32SWei Yang if (driver) { 47946d4be41SSam Bobroff if (driver->err_handler) { 48067086e32SWei Yang eeh_pcid_put(dev); 48167086e32SWei Yang return NULL; 48267086e32SWei Yang } 48346d4be41SSam Bobroff eeh_pcid_put(dev); 48446d4be41SSam Bobroff } 48567086e32SWei Yang 486988fc3baSBryant G. Ly #ifdef CONFIG_PCI_IOV 4871ff8f36fSSam Bobroff pci_iov_add_virtfn(edev->physfn, eeh_dev_to_pdn(edev)->vf_index); 48867086e32SWei Yang #endif 48967086e32SWei Yang return NULL; 49067086e32SWei Yang } 49167086e32SWei Yang 492cef50c67SSam Bobroff static void eeh_rmv_device(struct eeh_dev *edev, void *userdata) 493f5c57710SGavin Shan { 494f5c57710SGavin Shan struct pci_driver *driver; 495f5c57710SGavin Shan struct pci_dev *dev = eeh_dev_to_pci_dev(edev); 49667086e32SWei Yang struct eeh_rmv_data *rmv_data = (struct eeh_rmv_data *)userdata; 497f5c57710SGavin Shan 498f5c57710SGavin Shan /* 499f5c57710SGavin Shan * Actually, we should remove the PCI bridges as well. 500f5c57710SGavin Shan * However, that's lots of complexity to do that, 501f5c57710SGavin Shan * particularly some of devices under the bridge might 502f5c57710SGavin Shan * support EEH. So we just care about PCI devices for 503f5c57710SGavin Shan * simplicity here. 504f5c57710SGavin Shan */ 5051ef52073SSam Bobroff if (!eeh_edev_actionable(edev) || 5061ef52073SSam Bobroff (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE)) 507cef50c67SSam Bobroff return; 508d2b0f6f7SGavin Shan 5091c5c533bSSam Bobroff if (rmv_data) { 510f5c57710SGavin Shan driver = eeh_pcid_get(dev); 5118cc6b6cdSThadeu Lima de Souza Cascardo if (driver) { 51246d4be41SSam Bobroff if (driver->err_handler && 513f2da4ccfSGavin Shan driver->err_handler->error_detected && 51446d4be41SSam Bobroff driver->err_handler->slot_reset) { 51546d4be41SSam Bobroff eeh_pcid_put(dev); 516cef50c67SSam Bobroff return; 5178cc6b6cdSThadeu Lima de Souza Cascardo } 51846d4be41SSam Bobroff eeh_pcid_put(dev); 51946d4be41SSam Bobroff } 52046d4be41SSam Bobroff } 521f5c57710SGavin Shan 522f5c57710SGavin Shan /* Remove it from PCI subsystem */ 5231ef52073SSam Bobroff pr_info("EEH: Removing %s without EEH sensitive driver\n", 524f5c57710SGavin Shan pci_name(dev)); 525f5c57710SGavin Shan edev->mode |= EEH_DEV_DISCONNECTED; 5261c5c533bSSam Bobroff if (rmv_data) 5271c5c533bSSam Bobroff rmv_data->removed_dev_count++; 528f5c57710SGavin Shan 52967086e32SWei Yang if (edev->physfn) { 530988fc3baSBryant G. Ly #ifdef CONFIG_PCI_IOV 53167086e32SWei Yang struct pci_dn *pdn = eeh_dev_to_pdn(edev); 53267086e32SWei Yang 533753f6124SJan H. Schönherr pci_iov_remove_virtfn(edev->physfn, pdn->vf_index); 53467086e32SWei Yang edev->pdev = NULL; 53567086e32SWei Yang 53667086e32SWei Yang /* 53767086e32SWei Yang * We have to set the VF PE number to invalid one, which is 53867086e32SWei Yang * required to plug the VF successfully. 53967086e32SWei Yang */ 54067086e32SWei Yang pdn->pe_number = IODA_INVALID_PE; 54167086e32SWei Yang #endif 54267086e32SWei Yang if (rmv_data) 5431c5c533bSSam Bobroff list_add(&edev->rmv_entry, &rmv_data->removed_vf_list); 54467086e32SWei Yang } else { 5451c2042c8SRafael J. Wysocki pci_lock_rescan_remove(); 546f5c57710SGavin Shan pci_stop_and_remove_bus_device(dev); 5471c2042c8SRafael J. Wysocki pci_unlock_rescan_remove(); 54867086e32SWei Yang } 549f5c57710SGavin Shan } 550f5c57710SGavin Shan 551d6c4932fSSam Bobroff static void *eeh_pe_detach_dev(struct eeh_pe *pe, void *userdata) 552f5c57710SGavin Shan { 553f5c57710SGavin Shan struct eeh_dev *edev, *tmp; 554f5c57710SGavin Shan 555f5c57710SGavin Shan eeh_pe_for_each_dev(pe, edev, tmp) { 556f5c57710SGavin Shan if (!(edev->mode & EEH_DEV_DISCONNECTED)) 557f5c57710SGavin Shan continue; 558f5c57710SGavin Shan 559f5c57710SGavin Shan edev->mode &= ~(EEH_DEV_DISCONNECTED | EEH_DEV_IRQ_DISABLED); 560f5c57710SGavin Shan eeh_rmv_from_parent_pe(edev); 561f5c57710SGavin Shan } 562f5c57710SGavin Shan 563f5c57710SGavin Shan return NULL; 564f5c57710SGavin Shan } 565f5c57710SGavin Shan 56678954700SGavin Shan /* 56778954700SGavin Shan * Explicitly clear PE's frozen state for PowerNV where 56878954700SGavin Shan * we have frozen PE until BAR restore is completed. It's 56978954700SGavin Shan * harmless to clear it for pSeries. To be consistent with 57078954700SGavin Shan * PE reset (for 3 times), we try to clear the frozen state 57178954700SGavin Shan * for 3 times as well. 57278954700SGavin Shan */ 5734d8e325dSSam Bobroff static int eeh_clear_pe_frozen_state(struct eeh_pe *root, bool include_passed) 57478954700SGavin Shan { 5753376cb91SSam Bobroff struct eeh_pe *pe; 5763376cb91SSam Bobroff int i; 57778954700SGavin Shan 5783376cb91SSam Bobroff eeh_for_each_pe(root, pe) { 5794d8e325dSSam Bobroff if (include_passed || !eeh_pe_passed(pe)) { 5803376cb91SSam Bobroff for (i = 0; i < 3; i++) 581188fdea6SSam Bobroff if (!eeh_unfreeze_pe(pe)) 5823376cb91SSam Bobroff break; 5833376cb91SSam Bobroff if (i >= 3) 5843376cb91SSam Bobroff return -EIO; 5852c665992SGavin Shan } 5864d8e325dSSam Bobroff } 5874d8e325dSSam Bobroff eeh_pe_state_clear(root, EEH_PE_ISOLATED, include_passed); 5883376cb91SSam Bobroff return 0; 58978954700SGavin Shan } 59078954700SGavin Shan 5915cfb20b9SGavin Shan int eeh_pe_reset_and_recover(struct eeh_pe *pe) 5925cfb20b9SGavin Shan { 5932efc771fSGavin Shan int ret; 5945cfb20b9SGavin Shan 5955cfb20b9SGavin Shan /* Bail if the PE is being recovered */ 5965cfb20b9SGavin Shan if (pe->state & EEH_PE_RECOVERING) 5975cfb20b9SGavin Shan return 0; 5985cfb20b9SGavin Shan 5995cfb20b9SGavin Shan /* Put the PE into recovery mode */ 6005cfb20b9SGavin Shan eeh_pe_state_mark(pe, EEH_PE_RECOVERING); 6015cfb20b9SGavin Shan 6025cfb20b9SGavin Shan /* Save states */ 6035cfb20b9SGavin Shan eeh_pe_dev_traverse(pe, eeh_dev_save_state, NULL); 6045cfb20b9SGavin Shan 6055cfb20b9SGavin Shan /* Issue reset */ 6061ef52073SSam Bobroff ret = eeh_pe_reset_full(pe, true); 6075cfb20b9SGavin Shan if (ret) { 6089ed5ca66SSam Bobroff eeh_pe_state_clear(pe, EEH_PE_RECOVERING, true); 6095cfb20b9SGavin Shan return ret; 6105cfb20b9SGavin Shan } 6115cfb20b9SGavin Shan 6125cfb20b9SGavin Shan /* Unfreeze the PE */ 6134d8e325dSSam Bobroff ret = eeh_clear_pe_frozen_state(pe, true); 6145cfb20b9SGavin Shan if (ret) { 6159ed5ca66SSam Bobroff eeh_pe_state_clear(pe, EEH_PE_RECOVERING, true); 6165cfb20b9SGavin Shan return ret; 6175cfb20b9SGavin Shan } 6185cfb20b9SGavin Shan 6195cfb20b9SGavin Shan /* Restore device state */ 6205cfb20b9SGavin Shan eeh_pe_dev_traverse(pe, eeh_dev_restore_state, NULL); 6215cfb20b9SGavin Shan 6225cfb20b9SGavin Shan /* Clear recovery mode */ 6239ed5ca66SSam Bobroff eeh_pe_state_clear(pe, EEH_PE_RECOVERING, true); 6245cfb20b9SGavin Shan 6255cfb20b9SGavin Shan return 0; 6265cfb20b9SGavin Shan } 6275cfb20b9SGavin Shan 628317f06deSGavin Shan /** 629317f06deSGavin Shan * eeh_reset_device - Perform actual reset of a pci slot 6305fd13460SSam Bobroff * @driver_eeh_aware: Does the device's driver provide EEH support? 631317f06deSGavin Shan * @pe: EEH PE 632317f06deSGavin Shan * @bus: PCI bus corresponding to the isolcated slot 6335fd13460SSam Bobroff * @rmv_data: Optional, list to record removed devices 634317f06deSGavin Shan * 635317f06deSGavin Shan * This routine must be called to do reset on the indicated PE. 636317f06deSGavin Shan * During the reset, udev might be invoked because those affected 637317f06deSGavin Shan * PCI devices will be removed and then added. 638317f06deSGavin Shan */ 63967086e32SWei Yang static int eeh_reset_device(struct eeh_pe *pe, struct pci_bus *bus, 6405fd13460SSam Bobroff struct eeh_rmv_data *rmv_data, 6415fd13460SSam Bobroff bool driver_eeh_aware) 642317f06deSGavin Shan { 643edfd17ffSArnd Bergmann time64_t tstamp; 64467086e32SWei Yang int cnt, rc; 64567086e32SWei Yang struct eeh_dev *edev; 6461ef52073SSam Bobroff struct eeh_pe *tmp_pe; 6471ef52073SSam Bobroff bool any_passed = false; 6481ef52073SSam Bobroff 6491ef52073SSam Bobroff eeh_for_each_pe(pe, tmp_pe) 6501ef52073SSam Bobroff any_passed |= eeh_pe_passed(tmp_pe); 651317f06deSGavin Shan 652317f06deSGavin Shan /* pcibios will clear the counter; save the value */ 653317f06deSGavin Shan cnt = pe->freeze_count; 6545a71978eSGavin Shan tstamp = pe->tstamp; 655317f06deSGavin Shan 656317f06deSGavin Shan /* 657317f06deSGavin Shan * We don't remove the corresponding PE instances because 658317f06deSGavin Shan * we need the information afterwords. The attached EEH 659317f06deSGavin Shan * devices are expected to be attached soon when calling 660bd251b89SGavin Shan * into pci_hp_add_devices(). 661317f06deSGavin Shan */ 662807a827dSGavin Shan eeh_pe_state_mark(pe, EEH_PE_KEEP); 6631ef52073SSam Bobroff if (any_passed || driver_eeh_aware || (pe->type & EEH_PE_VF)) { 66454048cf8SSam Bobroff eeh_pe_dev_traverse(pe, eeh_rmv_device, rmv_data); 66567086e32SWei Yang } else { 6661c2042c8SRafael J. Wysocki pci_lock_rescan_remove(); 667bd251b89SGavin Shan pci_hp_remove_devices(bus); 6681c2042c8SRafael J. Wysocki pci_unlock_rescan_remove(); 66967086e32SWei Yang } 670317f06deSGavin Shan 671d0914f50SGavin Shan /* 672d0914f50SGavin Shan * Reset the pci controller. (Asserts RST#; resets config space). 673317f06deSGavin Shan * Reconfigure bridges and devices. Don't try to bring the system 674317f06deSGavin Shan * up if the reset failed for some reason. 675d0914f50SGavin Shan * 676d0914f50SGavin Shan * During the reset, it's very dangerous to have uncontrolled PCI 677d0914f50SGavin Shan * config accesses. So we prefer to block them. However, controlled 678d0914f50SGavin Shan * PCI config accesses initiated from EEH itself are allowed. 679317f06deSGavin Shan */ 6801ef52073SSam Bobroff rc = eeh_pe_reset_full(pe, false); 68128bf36f9SGavin Shan if (rc) 682317f06deSGavin Shan return rc; 683317f06deSGavin Shan 6841c2042c8SRafael J. Wysocki pci_lock_rescan_remove(); 6851c2042c8SRafael J. Wysocki 686317f06deSGavin Shan /* Restore PE */ 687317f06deSGavin Shan eeh_ops->configure_bridge(pe); 688317f06deSGavin Shan eeh_pe_restore_bars(pe); 689317f06deSGavin Shan 690dc9c41bdSAndrew Donnellan /* Clear frozen state */ 6911ef52073SSam Bobroff rc = eeh_clear_pe_frozen_state(pe, false); 692409bf7f8SAndrew Donnellan if (rc) { 693409bf7f8SAndrew Donnellan pci_unlock_rescan_remove(); 69478954700SGavin Shan return rc; 695409bf7f8SAndrew Donnellan } 69678954700SGavin Shan 697317f06deSGavin Shan /* Give the system 5 seconds to finish running the user-space 698317f06deSGavin Shan * hotplug shutdown scripts, e.g. ifdown for ethernet. Yes, 699317f06deSGavin Shan * this is a hack, but if we don't do this, and try to bring 700317f06deSGavin Shan * the device up before the scripts have taken it down, 701317f06deSGavin Shan * potentially weird things happen. 702317f06deSGavin Shan */ 7031c5c533bSSam Bobroff if (!driver_eeh_aware || rmv_data->removed_dev_count) { 70454048cf8SSam Bobroff pr_info("EEH: Sleep 5s ahead of %s hotplug\n", 70554048cf8SSam Bobroff (driver_eeh_aware ? "partial" : "complete")); 706317f06deSGavin Shan ssleep(5); 707f5c57710SGavin Shan 708f5c57710SGavin Shan /* 709f5c57710SGavin Shan * The EEH device is still connected with its parent 710f5c57710SGavin Shan * PE. We should disconnect it so the binding can be 711f5c57710SGavin Shan * rebuilt when adding PCI devices. 712f5c57710SGavin Shan */ 71380e65b00SSam Bobroff edev = list_first_entry(&pe->edevs, struct eeh_dev, entry); 714f5c57710SGavin Shan eeh_pe_traverse(pe, eeh_pe_detach_dev, NULL); 715a3aa256bSGavin Shan if (pe->type & EEH_PE_VF) { 716bf773df9SSam Bobroff eeh_add_virt_device(edev); 717a3aa256bSGavin Shan } else { 71854048cf8SSam Bobroff if (!driver_eeh_aware) 7199ed5ca66SSam Bobroff eeh_pe_state_clear(pe, EEH_PE_PRI_BUS, true); 720bd251b89SGavin Shan pci_hp_add_devices(bus); 721a3aa256bSGavin Shan } 722317f06deSGavin Shan } 7239ed5ca66SSam Bobroff eeh_pe_state_clear(pe, EEH_PE_KEEP, true); 7245a71978eSGavin Shan 7255a71978eSGavin Shan pe->tstamp = tstamp; 726317f06deSGavin Shan pe->freeze_count = cnt; 727317f06deSGavin Shan 7281c2042c8SRafael J. Wysocki pci_unlock_rescan_remove(); 729317f06deSGavin Shan return 0; 730317f06deSGavin Shan } 731317f06deSGavin Shan 732317f06deSGavin Shan /* The longest amount of time to wait for a pci device 733317f06deSGavin Shan * to come back on line, in seconds. 734317f06deSGavin Shan */ 735fb48dc22SBrian King #define MAX_WAIT_FOR_RECOVERY 300 736317f06deSGavin Shan 737799abe28SOliver O'Halloran 738799abe28SOliver O'Halloran /* Walks the PE tree after processing an event to remove any stale PEs. 739799abe28SOliver O'Halloran * 740799abe28SOliver O'Halloran * NB: This needs to be recursive to ensure the leaf PEs get removed 741799abe28SOliver O'Halloran * before their parents do. Although this is possible to do recursively 742799abe28SOliver O'Halloran * we don't since this is easier to read and we need to garantee 743799abe28SOliver O'Halloran * the leaf nodes will be handled first. 744799abe28SOliver O'Halloran */ 745799abe28SOliver O'Halloran static void eeh_pe_cleanup(struct eeh_pe *pe) 746799abe28SOliver O'Halloran { 747799abe28SOliver O'Halloran struct eeh_pe *child_pe, *tmp; 748799abe28SOliver O'Halloran 749799abe28SOliver O'Halloran list_for_each_entry_safe(child_pe, tmp, &pe->child_list, child) 750799abe28SOliver O'Halloran eeh_pe_cleanup(child_pe); 751799abe28SOliver O'Halloran 752799abe28SOliver O'Halloran if (pe->state & EEH_PE_KEEP) 753799abe28SOliver O'Halloran return; 754799abe28SOliver O'Halloran 755799abe28SOliver O'Halloran if (!(pe->state & EEH_PE_INVALID)) 756799abe28SOliver O'Halloran return; 757799abe28SOliver O'Halloran 758799abe28SOliver O'Halloran if (list_empty(&pe->edevs) && list_empty(&pe->child_list)) { 759799abe28SOliver O'Halloran list_del(&pe->child); 760799abe28SOliver O'Halloran kfree(pe); 761799abe28SOliver O'Halloran } 762799abe28SOliver O'Halloran } 763799abe28SOliver O'Halloran 764c0b64978SRussell Currey /** 765c0b64978SRussell Currey * eeh_handle_normal_event - Handle EEH events on a specific PE 76637fd8125SSam Bobroff * @pe: EEH PE - which should not be used after we return, as it may 76737fd8125SSam Bobroff * have been invalidated. 768c0b64978SRussell Currey * 769c0b64978SRussell Currey * Attempts to recover the given PE. If recovery fails or the PE has failed 770c0b64978SRussell Currey * too many times, remove the PE. 771c0b64978SRussell Currey * 77268701780SSam Bobroff * While PHB detects address or data parity errors on particular PCI 77368701780SSam Bobroff * slot, the associated PE will be frozen. Besides, DMA's occurring 77468701780SSam Bobroff * to wild addresses (which usually happen due to bugs in device 77568701780SSam Bobroff * drivers or in PCI adapter firmware) can cause EEH error. #SERR, 77668701780SSam Bobroff * #PERR or other misc PCI-related errors also can trigger EEH errors. 77768701780SSam Bobroff * 77868701780SSam Bobroff * Recovery process consists of unplugging the device driver (which 77968701780SSam Bobroff * generated hotplug events to userspace), then issuing a PCI #RST to 78068701780SSam Bobroff * the device, then reconfiguring the PCI config space for all bridges 78168701780SSam Bobroff * & devices under this slot, and then finally restarting the device 78268701780SSam Bobroff * drivers (which cause a second set of hotplug events to go out to 78368701780SSam Bobroff * userspace). 784c0b64978SRussell Currey */ 78537fd8125SSam Bobroff void eeh_handle_normal_event(struct eeh_pe *pe) 786317f06deSGavin Shan { 787cd95f804SSam Bobroff struct pci_bus *bus; 78867086e32SWei Yang struct eeh_dev *edev, *tmp; 789665012c5SSam Bobroff struct eeh_pe *tmp_pe; 790317f06deSGavin Shan int rc = 0; 791317f06deSGavin Shan enum pci_ers_result result = PCI_ERS_RESULT_NONE; 7921c5c533bSSam Bobroff struct eeh_rmv_data rmv_data = 7931c5c533bSSam Bobroff {LIST_HEAD_INIT(rmv_data.removed_vf_list), 0}; 794317f06deSGavin Shan 795cd95f804SSam Bobroff bus = eeh_pe_bus_get(pe); 796cd95f804SSam Bobroff if (!bus) { 7971f52f176SRussell Currey pr_err("%s: Cannot find PCI bus for PHB#%x-PE#%x\n", 798317f06deSGavin Shan __func__, pe->phb->global_number, pe->addr); 79937fd8125SSam Bobroff return; 800317f06deSGavin Shan } 801317f06deSGavin Shan 8025a71978eSGavin Shan eeh_pe_update_time_stamp(pe); 803317f06deSGavin Shan pe->freeze_count++; 804c0b64978SRussell Currey if (pe->freeze_count > eeh_max_freezes) { 805796b9f5bSSam Bobroff pr_err("EEH: PHB#%x-PE#%x has failed %d times in the last hour and has been permanently disabled.\n", 806c0b64978SRussell Currey pe->phb->global_number, pe->addr, 807c0b64978SRussell Currey pe->freeze_count); 808b90484ecSSam Bobroff result = PCI_ERS_RESULT_DISCONNECT; 809c0b64978SRussell Currey } 810317f06deSGavin Shan 811aa06e3d6SSam Bobroff eeh_for_each_pe(pe, tmp_pe) 812aa06e3d6SSam Bobroff eeh_pe_for_each_dev(tmp_pe, edev, tmp) 813aa06e3d6SSam Bobroff edev->mode &= ~EEH_DEV_NO_HANDLER; 814aa06e3d6SSam Bobroff 815317f06deSGavin Shan /* Walk the various device drivers attached to this slot through 816317f06deSGavin Shan * a reset sequence, giving each an opportunity to do what it needs 817317f06deSGavin Shan * to accomplish the reset. Each child gets a report of the 818317f06deSGavin Shan * status ... if any child can't handle the reset, then the entire 819317f06deSGavin Shan * slot is dlpar removed and added. 8208234fcedSGavin Shan * 8218234fcedSGavin Shan * When the PHB is fenced, we have to issue a reset to recover from 8228234fcedSGavin Shan * the error. Override the result if necessary to have partially 8238234fcedSGavin Shan * hotplug for this case. 824317f06deSGavin Shan */ 825b90484ecSSam Bobroff if (result != PCI_ERS_RESULT_DISCONNECT) { 826b90484ecSSam Bobroff pr_warn("EEH: This PCI device has failed %d times in the last hour and will be permanently disabled after %d failures.\n", 827b90484ecSSam Bobroff pe->freeze_count, eeh_max_freezes); 82856ca4fdeSGavin Shan pr_info("EEH: Notify device drivers to shutdown\n"); 82947cc8c1cSSam Bobroff eeh_set_channel_state(pe, pci_channel_io_frozen); 830010acfa1SSam Bobroff eeh_set_irq_state(pe, false); 831b90484ecSSam Bobroff eeh_pe_report("error_detected(IO frozen)", pe, 832b90484ecSSam Bobroff eeh_report_error, &result); 8338234fcedSGavin Shan if ((pe->type & EEH_PE_PHB) && 8348234fcedSGavin Shan result != PCI_ERS_RESULT_NONE && 8358234fcedSGavin Shan result != PCI_ERS_RESULT_NEED_RESET) 8368234fcedSGavin Shan result = PCI_ERS_RESULT_NEED_RESET; 837b90484ecSSam Bobroff } 838317f06deSGavin Shan 839317f06deSGavin Shan /* Get the current PCI slot state. This can take a long time, 8402ac3990cSWei Yang * sometimes over 300 seconds for certain systems. 841317f06deSGavin Shan */ 842b90484ecSSam Bobroff if (result != PCI_ERS_RESULT_DISCONNECT) { 843fef7f905SSam Bobroff rc = eeh_wait_state(pe, MAX_WAIT_FOR_RECOVERY*1000); 844317f06deSGavin Shan if (rc < 0 || rc == EEH_STATE_NOT_SUPPORT) { 8450dae2743SGavin Shan pr_warn("EEH: Permanent failure\n"); 846b90484ecSSam Bobroff result = PCI_ERS_RESULT_DISCONNECT; 847b90484ecSSam Bobroff } 848317f06deSGavin Shan } 849317f06deSGavin Shan 850317f06deSGavin Shan /* Since rtas may enable MMIO when posting the error log, 851317f06deSGavin Shan * don't post the error log until after all dev drivers 852317f06deSGavin Shan * have been informed. 853317f06deSGavin Shan */ 854b90484ecSSam Bobroff if (result != PCI_ERS_RESULT_DISCONNECT) { 85556ca4fdeSGavin Shan pr_info("EEH: Collect temporary log\n"); 856317f06deSGavin Shan eeh_slot_error_detail(pe, EEH_LOG_TEMP); 857b90484ecSSam Bobroff } 858317f06deSGavin Shan 859317f06deSGavin Shan /* If all device drivers were EEH-unaware, then shut 860317f06deSGavin Shan * down all of the device drivers, and hope they 861317f06deSGavin Shan * go down willingly, without panicing the system. 862317f06deSGavin Shan */ 863317f06deSGavin Shan if (result == PCI_ERS_RESULT_NONE) { 86456ca4fdeSGavin Shan pr_info("EEH: Reset with hotplug activity\n"); 8655fd13460SSam Bobroff rc = eeh_reset_device(pe, bus, NULL, false); 866317f06deSGavin Shan if (rc) { 8670dae2743SGavin Shan pr_warn("%s: Unable to reset, err=%d\n", 86856ca4fdeSGavin Shan __func__, rc); 869b90484ecSSam Bobroff result = PCI_ERS_RESULT_DISCONNECT; 870317f06deSGavin Shan } 871317f06deSGavin Shan } 872317f06deSGavin Shan 873317f06deSGavin Shan /* If all devices reported they can proceed, then re-enable MMIO */ 874317f06deSGavin Shan if (result == PCI_ERS_RESULT_CAN_RECOVER) { 87556ca4fdeSGavin Shan pr_info("EEH: Enable I/O for affected devices\n"); 876317f06deSGavin Shan rc = eeh_pci_enable(pe, EEH_OPT_THAW_MMIO); 877317f06deSGavin Shan 878b90484ecSSam Bobroff if (rc < 0) { 879b90484ecSSam Bobroff result = PCI_ERS_RESULT_DISCONNECT; 880b90484ecSSam Bobroff } else if (rc) { 881317f06deSGavin Shan result = PCI_ERS_RESULT_NEED_RESET; 882317f06deSGavin Shan } else { 88356ca4fdeSGavin Shan pr_info("EEH: Notify device drivers to resume I/O\n"); 88420b34497SSam Bobroff eeh_pe_report("mmio_enabled", pe, 88520b34497SSam Bobroff eeh_report_mmio_enabled, &result); 886317f06deSGavin Shan } 887317f06deSGavin Shan } 888317f06deSGavin Shan 889317f06deSGavin Shan /* If all devices reported they can proceed, then re-enable DMA */ 890317f06deSGavin Shan if (result == PCI_ERS_RESULT_CAN_RECOVER) { 89156ca4fdeSGavin Shan pr_info("EEH: Enabled DMA for affected devices\n"); 892317f06deSGavin Shan rc = eeh_pci_enable(pe, EEH_OPT_THAW_DMA); 893317f06deSGavin Shan 894b90484ecSSam Bobroff if (rc < 0) { 895b90484ecSSam Bobroff result = PCI_ERS_RESULT_DISCONNECT; 896b90484ecSSam Bobroff } else if (rc) { 897317f06deSGavin Shan result = PCI_ERS_RESULT_NEED_RESET; 89835845a78SGavin Shan } else { 89935845a78SGavin Shan /* 90035845a78SGavin Shan * We didn't do PE reset for the case. The PE 90135845a78SGavin Shan * is still in frozen state. Clear it before 90235845a78SGavin Shan * resuming the PE. 90335845a78SGavin Shan */ 9049ed5ca66SSam Bobroff eeh_pe_state_clear(pe, EEH_PE_ISOLATED, true); 905317f06deSGavin Shan result = PCI_ERS_RESULT_RECOVERED; 906317f06deSGavin Shan } 90735845a78SGavin Shan } 908317f06deSGavin Shan 909317f06deSGavin Shan /* If any device called out for a reset, then reset the slot */ 910317f06deSGavin Shan if (result == PCI_ERS_RESULT_NEED_RESET) { 91156ca4fdeSGavin Shan pr_info("EEH: Reset without hotplug activity\n"); 9125fd13460SSam Bobroff rc = eeh_reset_device(pe, bus, &rmv_data, true); 913317f06deSGavin Shan if (rc) { 9140dae2743SGavin Shan pr_warn("%s: Cannot reset, err=%d\n", 91556ca4fdeSGavin Shan __func__, rc); 916b90484ecSSam Bobroff result = PCI_ERS_RESULT_DISCONNECT; 917b90484ecSSam Bobroff } else { 918317f06deSGavin Shan result = PCI_ERS_RESULT_NONE; 91947cc8c1cSSam Bobroff eeh_set_channel_state(pe, pci_channel_io_normal); 920010acfa1SSam Bobroff eeh_set_irq_state(pe, true); 921b90484ecSSam Bobroff eeh_pe_report("slot_reset", pe, eeh_report_reset, 922b90484ecSSam Bobroff &result); 923b90484ecSSam Bobroff } 924317f06deSGavin Shan } 925317f06deSGavin Shan 926b90484ecSSam Bobroff if ((result == PCI_ERS_RESULT_RECOVERED) || 927b90484ecSSam Bobroff (result == PCI_ERS_RESULT_NONE)) { 92867086e32SWei Yang /* 929b90484ecSSam Bobroff * For those hot removed VFs, we should add back them after PF 930b90484ecSSam Bobroff * get recovered properly. 93167086e32SWei Yang */ 9321c5c533bSSam Bobroff list_for_each_entry_safe(edev, tmp, &rmv_data.removed_vf_list, 9331c5c533bSSam Bobroff rmv_entry) { 934bf773df9SSam Bobroff eeh_add_virt_device(edev); 93580e65b00SSam Bobroff list_del(&edev->rmv_entry); 93667086e32SWei Yang } 93767086e32SWei Yang 938317f06deSGavin Shan /* Tell all device drivers that they can resume operations */ 93956ca4fdeSGavin Shan pr_info("EEH: Notify device driver to resume\n"); 94047cc8c1cSSam Bobroff eeh_set_channel_state(pe, pci_channel_io_normal); 941010acfa1SSam Bobroff eeh_set_irq_state(pe, true); 94220b34497SSam Bobroff eeh_pe_report("resume", pe, eeh_report_resume, NULL); 94320b34497SSam Bobroff eeh_for_each_pe(pe, tmp_pe) { 94420b34497SSam Bobroff eeh_pe_for_each_dev(tmp_pe, edev, tmp) { 945665012c5SSam Bobroff edev->mode &= ~EEH_DEV_NO_HANDLER; 94620b34497SSam Bobroff edev->in_error = false; 94720b34497SSam Bobroff } 94820b34497SSam Bobroff } 949665012c5SSam Bobroff 950796b9f5bSSam Bobroff pr_info("EEH: Recovery successful.\n"); 951b90484ecSSam Bobroff } else { 952317f06deSGavin Shan /* 953317f06deSGavin Shan * About 90% of all real-life EEH failures in the field 954317f06deSGavin Shan * are due to poorly seated PCI cards. Only 10% or so are 955317f06deSGavin Shan * due to actual, failed cards. 956317f06deSGavin Shan */ 9571f52f176SRussell Currey pr_err("EEH: Unable to recover from failure from PHB#%x-PE#%x.\n" 958317f06deSGavin Shan "Please try reseating or replacing it\n", 959317f06deSGavin Shan pe->phb->global_number, pe->addr); 960317f06deSGavin Shan 961317f06deSGavin Shan eeh_slot_error_detail(pe, EEH_LOG_PERM); 962317f06deSGavin Shan 963317f06deSGavin Shan /* Notify all devices that they're about to go down. */ 96447cc8c1cSSam Bobroff eeh_set_channel_state(pe, pci_channel_io_perm_failure); 965010acfa1SSam Bobroff eeh_set_irq_state(pe, false); 96620b34497SSam Bobroff eeh_pe_report("error_detected(permanent failure)", pe, 96720b34497SSam Bobroff eeh_report_failure, NULL); 968317f06deSGavin Shan 969d2b0f6f7SGavin Shan /* Mark the PE to be removed permanently */ 970432227e9SGavin Shan eeh_pe_state_mark(pe, EEH_PE_REMOVED); 971d2b0f6f7SGavin Shan 972d2b0f6f7SGavin Shan /* 973d2b0f6f7SGavin Shan * Shut down the device drivers for good. We mark 974d2b0f6f7SGavin Shan * all removed devices correctly to avoid access 975d2b0f6f7SGavin Shan * the their PCI config any more. 976d2b0f6f7SGavin Shan */ 97767086e32SWei Yang if (pe->type & EEH_PE_VF) { 97867086e32SWei Yang eeh_pe_dev_traverse(pe, eeh_rmv_device, NULL); 97967086e32SWei Yang eeh_pe_dev_mode_mark(pe, EEH_DEV_REMOVED); 98067086e32SWei Yang } else { 9819ed5ca66SSam Bobroff eeh_pe_state_clear(pe, EEH_PE_PRI_BUS, true); 982d2b0f6f7SGavin Shan eeh_pe_dev_mode_mark(pe, EEH_DEV_REMOVED); 983d2b0f6f7SGavin Shan 9841c2042c8SRafael J. Wysocki pci_lock_rescan_remove(); 985cd95f804SSam Bobroff pci_hp_remove_devices(bus); 9861c2042c8SRafael J. Wysocki pci_unlock_rescan_remove(); 987daeba295SRussell Currey /* The passed PE should no longer be used */ 98837fd8125SSam Bobroff return; 9891c2042c8SRafael J. Wysocki } 990b90484ecSSam Bobroff } 991799abe28SOliver O'Halloran 992799abe28SOliver O'Halloran /* 993799abe28SOliver O'Halloran * Clean up any PEs without devices. While marked as EEH_PE_RECOVERYING 994799abe28SOliver O'Halloran * we don't want to modify the PE tree structure so we do it here. 995799abe28SOliver O'Halloran */ 996799abe28SOliver O'Halloran eeh_pe_cleanup(pe); 9979ed5ca66SSam Bobroff eeh_pe_state_clear(pe, EEH_PE_RECOVERING, true); 99867086e32SWei Yang } 9998a6b1bc7SGavin Shan 1000c0b64978SRussell Currey /** 1001c0b64978SRussell Currey * eeh_handle_special_event - Handle EEH events without a specific failing PE 1002c0b64978SRussell Currey * 1003c0b64978SRussell Currey * Called when an EEH event is detected but can't be narrowed down to a 1004c0b64978SRussell Currey * specific PE. Iterates through possible failures and handles them as 1005c0b64978SRussell Currey * necessary. 1006c0b64978SRussell Currey */ 100768701780SSam Bobroff void eeh_handle_special_event(void) 10088a6b1bc7SGavin Shan { 1009aa06e3d6SSam Bobroff struct eeh_pe *pe, *phb_pe, *tmp_pe; 1010aa06e3d6SSam Bobroff struct eeh_dev *edev, *tmp_edev; 10118a6b1bc7SGavin Shan struct pci_bus *bus; 10127e4e7867SGavin Shan struct pci_controller *hose; 10138a6b1bc7SGavin Shan unsigned long flags; 10147e4e7867SGavin Shan int rc; 10158a6b1bc7SGavin Shan 10167e4e7867SGavin Shan 10177e4e7867SGavin Shan do { 10188a6b1bc7SGavin Shan rc = eeh_ops->next_error(&pe); 10198a6b1bc7SGavin Shan 10208a6b1bc7SGavin Shan switch (rc) { 10217e4e7867SGavin Shan case EEH_NEXT_ERR_DEAD_IOC: 10228a6b1bc7SGavin Shan /* Mark all PHBs in dead state */ 10238a6b1bc7SGavin Shan eeh_serialize_lock(&flags); 10247e4e7867SGavin Shan 10257e4e7867SGavin Shan /* Purge all events */ 10265c7a35e3SGavin Shan eeh_remove_event(NULL, true); 10277e4e7867SGavin Shan 10287e4e7867SGavin Shan list_for_each_entry(hose, &hose_list, list_node) { 10298a6b1bc7SGavin Shan phb_pe = eeh_phb_pe_get(hose); 10308a6b1bc7SGavin Shan if (!phb_pe) continue; 10318a6b1bc7SGavin Shan 1032e762bb89SSam Bobroff eeh_pe_mark_isolated(phb_pe); 10338a6b1bc7SGavin Shan } 10347e4e7867SGavin Shan 10358a6b1bc7SGavin Shan eeh_serialize_unlock(flags); 10368a6b1bc7SGavin Shan 10378a6b1bc7SGavin Shan break; 10387e4e7867SGavin Shan case EEH_NEXT_ERR_FROZEN_PE: 10397e4e7867SGavin Shan case EEH_NEXT_ERR_FENCED_PHB: 10407e4e7867SGavin Shan case EEH_NEXT_ERR_DEAD_PHB: 10418a6b1bc7SGavin Shan /* Mark the PE in fenced state */ 10428a6b1bc7SGavin Shan eeh_serialize_lock(&flags); 10437e4e7867SGavin Shan 10447e4e7867SGavin Shan /* Purge all events of the PHB */ 10455c7a35e3SGavin Shan eeh_remove_event(pe, true); 10467e4e7867SGavin Shan 1047e762bb89SSam Bobroff if (rc != EEH_NEXT_ERR_DEAD_PHB) 1048e762bb89SSam Bobroff eeh_pe_state_mark(pe, EEH_PE_RECOVERING); 1049e762bb89SSam Bobroff eeh_pe_mark_isolated(pe); 10507e4e7867SGavin Shan 10518a6b1bc7SGavin Shan eeh_serialize_unlock(flags); 10528a6b1bc7SGavin Shan 10538a6b1bc7SGavin Shan break; 10547e4e7867SGavin Shan case EEH_NEXT_ERR_NONE: 10557e4e7867SGavin Shan return; 10568a6b1bc7SGavin Shan default: 10577e4e7867SGavin Shan pr_warn("%s: Invalid value %d from next_error()\n", 10588a6b1bc7SGavin Shan __func__, rc); 10598a6b1bc7SGavin Shan return; 10608a6b1bc7SGavin Shan } 10618a6b1bc7SGavin Shan 10628a6b1bc7SGavin Shan /* 10638a6b1bc7SGavin Shan * For fenced PHB and frozen PE, it's handled as normal 10648a6b1bc7SGavin Shan * event. We have to remove the affected PHBs for dead 10658a6b1bc7SGavin Shan * PHB and IOC 10668a6b1bc7SGavin Shan */ 10677e4e7867SGavin Shan if (rc == EEH_NEXT_ERR_FROZEN_PE || 10687e4e7867SGavin Shan rc == EEH_NEXT_ERR_FENCED_PHB) { 1069799abe28SOliver O'Halloran eeh_pe_state_mark(pe, EEH_PE_RECOVERING); 107037fd8125SSam Bobroff eeh_handle_normal_event(pe); 10717e4e7867SGavin Shan } else { 10721c2042c8SRafael J. Wysocki pci_lock_rescan_remove(); 10737e4e7867SGavin Shan list_for_each_entry(hose, &hose_list, list_node) { 10748a6b1bc7SGavin Shan phb_pe = eeh_phb_pe_get(hose); 10757e4e7867SGavin Shan if (!phb_pe || 10769e049375SGavin Shan !(phb_pe->state & EEH_PE_ISOLATED) || 10779e049375SGavin Shan (phb_pe->state & EEH_PE_RECOVERING)) 10788a6b1bc7SGavin Shan continue; 10798a6b1bc7SGavin Shan 1080aa06e3d6SSam Bobroff eeh_for_each_pe(pe, tmp_pe) 1081aa06e3d6SSam Bobroff eeh_pe_for_each_dev(tmp_pe, edev, tmp_edev) 1082aa06e3d6SSam Bobroff edev->mode &= ~EEH_DEV_NO_HANDLER; 1083aa06e3d6SSam Bobroff 10847e4e7867SGavin Shan /* Notify all devices to be down */ 10859ed5ca66SSam Bobroff eeh_pe_state_clear(pe, EEH_PE_PRI_BUS, true); 108647cc8c1cSSam Bobroff eeh_set_channel_state(pe, pci_channel_io_perm_failure); 108720b34497SSam Bobroff eeh_pe_report( 108820b34497SSam Bobroff "error_detected(permanent failure)", pe, 1089af2e3a00SRussell Currey eeh_report_failure, NULL); 10908a6b1bc7SGavin Shan bus = eeh_pe_bus_get(phb_pe); 109104fec21cSRussell Currey if (!bus) { 109204fec21cSRussell Currey pr_err("%s: Cannot find PCI bus for " 10931f52f176SRussell Currey "PHB#%x-PE#%x\n", 109404fec21cSRussell Currey __func__, 109504fec21cSRussell Currey pe->phb->global_number, 109604fec21cSRussell Currey pe->addr); 109704fec21cSRussell Currey break; 109804fec21cSRussell Currey } 1099bd251b89SGavin Shan pci_hp_remove_devices(bus); 11008a6b1bc7SGavin Shan } 11011c2042c8SRafael J. Wysocki pci_unlock_rescan_remove(); 11028a6b1bc7SGavin Shan } 11037e4e7867SGavin Shan 11047e4e7867SGavin Shan /* 11057e4e7867SGavin Shan * If we have detected dead IOC, we needn't proceed 11067e4e7867SGavin Shan * any more since all PHBs would have been removed 11077e4e7867SGavin Shan */ 11087e4e7867SGavin Shan if (rc == EEH_NEXT_ERR_DEAD_IOC) 11097e4e7867SGavin Shan break; 11107e4e7867SGavin Shan } while (rc != EEH_NEXT_ERR_NONE); 11118a6b1bc7SGavin Shan } 1112