xref: /openbmc/linux/arch/powerpc/kernel/eeh_driver.c (revision 9efcdaac)
1bbbd7f11SThomas Huth // SPDX-License-Identifier: GPL-2.0-or-later
2317f06deSGavin Shan /*
3317f06deSGavin Shan  * PCI Error Recovery Driver for RPA-compliant PPC64 platform.
4317f06deSGavin Shan  * Copyright IBM Corp. 2004 2005
5317f06deSGavin Shan  * Copyright Linas Vepstas <linas@linas.org> 2004, 2005
6317f06deSGavin Shan  *
7317f06deSGavin Shan  * Send comments and feedback to Linas Vepstas <linas@austin.ibm.com>
8317f06deSGavin Shan  */
9317f06deSGavin Shan #include <linux/delay.h>
10317f06deSGavin Shan #include <linux/interrupt.h>
11317f06deSGavin Shan #include <linux/irq.h>
12317f06deSGavin Shan #include <linux/module.h>
13317f06deSGavin Shan #include <linux/pci.h>
14b104af5aSOliver O'Halloran #include <linux/pci_hotplug.h>
15317f06deSGavin Shan #include <asm/eeh.h>
16317f06deSGavin Shan #include <asm/eeh_event.h>
17317f06deSGavin Shan #include <asm/ppc-pci.h>
18317f06deSGavin Shan #include <asm/pci-bridge.h>
19317f06deSGavin Shan #include <asm/rtas.h>
20317f06deSGavin Shan 
2167086e32SWei Yang struct eeh_rmv_data {
221c5c533bSSam Bobroff 	struct list_head removed_vf_list;
231c5c533bSSam Bobroff 	int removed_dev_count;
2467086e32SWei Yang };
2567086e32SWei Yang 
eeh_result_priority(enum pci_ers_result result)2630424e38SSam Bobroff static int eeh_result_priority(enum pci_ers_result result)
2730424e38SSam Bobroff {
2830424e38SSam Bobroff 	switch (result) {
2930424e38SSam Bobroff 	case PCI_ERS_RESULT_NONE:
3030424e38SSam Bobroff 		return 1;
3130424e38SSam Bobroff 	case PCI_ERS_RESULT_NO_AER_DRIVER:
3230424e38SSam Bobroff 		return 2;
3330424e38SSam Bobroff 	case PCI_ERS_RESULT_RECOVERED:
3430424e38SSam Bobroff 		return 3;
3530424e38SSam Bobroff 	case PCI_ERS_RESULT_CAN_RECOVER:
3630424e38SSam Bobroff 		return 4;
3730424e38SSam Bobroff 	case PCI_ERS_RESULT_DISCONNECT:
3830424e38SSam Bobroff 		return 5;
3930424e38SSam Bobroff 	case PCI_ERS_RESULT_NEED_RESET:
4030424e38SSam Bobroff 		return 6;
4130424e38SSam Bobroff 	default:
4230424e38SSam Bobroff 		WARN_ONCE(1, "Unknown pci_ers_result value: %d\n", (int)result);
4330424e38SSam Bobroff 		return 0;
4430424e38SSam Bobroff 	}
4530424e38SSam Bobroff };
4630424e38SSam Bobroff 
pci_ers_result_name(enum pci_ers_result result)47c36c5ffdSBreno Leitao static const char *pci_ers_result_name(enum pci_ers_result result)
4820b34497SSam Bobroff {
4920b34497SSam Bobroff 	switch (result) {
5020b34497SSam Bobroff 	case PCI_ERS_RESULT_NONE:
5120b34497SSam Bobroff 		return "none";
5220b34497SSam Bobroff 	case PCI_ERS_RESULT_CAN_RECOVER:
5320b34497SSam Bobroff 		return "can recover";
5420b34497SSam Bobroff 	case PCI_ERS_RESULT_NEED_RESET:
5520b34497SSam Bobroff 		return "need reset";
5620b34497SSam Bobroff 	case PCI_ERS_RESULT_DISCONNECT:
5720b34497SSam Bobroff 		return "disconnect";
5820b34497SSam Bobroff 	case PCI_ERS_RESULT_RECOVERED:
5920b34497SSam Bobroff 		return "recovered";
6020b34497SSam Bobroff 	case PCI_ERS_RESULT_NO_AER_DRIVER:
6120b34497SSam Bobroff 		return "no AER driver";
6220b34497SSam Bobroff 	default:
6320b34497SSam Bobroff 		WARN_ONCE(1, "Unknown result type: %d\n", (int)result);
6420b34497SSam Bobroff 		return "unknown";
6520b34497SSam Bobroff 	}
6620b34497SSam Bobroff };
6720b34497SSam Bobroff 
pci_ers_merge_result(enum pci_ers_result old,enum pci_ers_result new)6830424e38SSam Bobroff static enum pci_ers_result pci_ers_merge_result(enum pci_ers_result old,
6930424e38SSam Bobroff 						enum pci_ers_result new)
7030424e38SSam Bobroff {
7130424e38SSam Bobroff 	if (eeh_result_priority(new) > eeh_result_priority(old))
7230424e38SSam Bobroff 		return new;
7330424e38SSam Bobroff 	return old;
7430424e38SSam Bobroff }
7530424e38SSam Bobroff 
eeh_dev_removed(struct eeh_dev * edev)76e2b810d5SSam Bobroff static bool eeh_dev_removed(struct eeh_dev *edev)
77e2b810d5SSam Bobroff {
78e2b810d5SSam Bobroff 	return !edev || (edev->mode & EEH_DEV_REMOVED);
79e2b810d5SSam Bobroff }
80e2b810d5SSam Bobroff 
eeh_edev_actionable(struct eeh_dev * edev)81e2b810d5SSam Bobroff static bool eeh_edev_actionable(struct eeh_dev *edev)
82e2b810d5SSam Bobroff {
8338ddc011SOliver O'Halloran 	if (!edev->pdev)
8438ddc011SOliver O'Halloran 		return false;
8538ddc011SOliver O'Halloran 	if (edev->pdev->error_state == pci_channel_io_perm_failure)
8638ddc011SOliver O'Halloran 		return false;
8738ddc011SOliver O'Halloran 	if (eeh_dev_removed(edev))
8838ddc011SOliver O'Halloran 		return false;
8938ddc011SOliver O'Halloran 	if (eeh_pe_passed(edev->pe))
9038ddc011SOliver O'Halloran 		return false;
9138ddc011SOliver O'Halloran 
9238ddc011SOliver O'Halloran 	return true;
93e2b810d5SSam Bobroff }
94e2b810d5SSam Bobroff 
95317f06deSGavin Shan /**
96317f06deSGavin Shan  * eeh_pcid_get - Get the PCI device driver
97317f06deSGavin Shan  * @pdev: PCI device
98317f06deSGavin Shan  *
99317f06deSGavin Shan  * The function is used to retrieve the PCI device driver for
100317f06deSGavin Shan  * the indicated PCI device. Besides, we will increase the reference
101317f06deSGavin Shan  * of the PCI device driver to prevent that being unloaded on
102317f06deSGavin Shan  * the fly. Otherwise, kernel crash would be seen.
103317f06deSGavin Shan  */
eeh_pcid_get(struct pci_dev * pdev)104317f06deSGavin Shan static inline struct pci_driver *eeh_pcid_get(struct pci_dev *pdev)
105317f06deSGavin Shan {
1064141127cSUwe Kleine-König 	if (!pdev || !pdev->dev.driver)
107317f06deSGavin Shan 		return NULL;
108317f06deSGavin Shan 
1094141127cSUwe Kleine-König 	if (!try_module_get(pdev->dev.driver->owner))
110317f06deSGavin Shan 		return NULL;
111317f06deSGavin Shan 
1124141127cSUwe Kleine-König 	return to_pci_driver(pdev->dev.driver);
113317f06deSGavin Shan }
114317f06deSGavin Shan 
115317f06deSGavin Shan /**
116317f06deSGavin Shan  * eeh_pcid_put - Dereference on the PCI device driver
117317f06deSGavin Shan  * @pdev: PCI device
118317f06deSGavin Shan  *
119317f06deSGavin Shan  * The function is called to do dereference on the PCI device
120317f06deSGavin Shan  * driver of the indicated PCI device.
121317f06deSGavin Shan  */
eeh_pcid_put(struct pci_dev * pdev)122317f06deSGavin Shan static inline void eeh_pcid_put(struct pci_dev *pdev)
123317f06deSGavin Shan {
1244141127cSUwe Kleine-König 	if (!pdev || !pdev->dev.driver)
125317f06deSGavin Shan 		return;
126317f06deSGavin Shan 
1274141127cSUwe Kleine-König 	module_put(pdev->dev.driver->owner);
128317f06deSGavin Shan }
129317f06deSGavin Shan 
130317f06deSGavin Shan /**
131317f06deSGavin Shan  * eeh_disable_irq - Disable interrupt for the recovering device
132317f06deSGavin Shan  * @dev: PCI device
133317f06deSGavin Shan  *
134317f06deSGavin Shan  * This routine must be called when reporting temporary or permanent
135317f06deSGavin Shan  * error to the particular PCI device to disable interrupt of that
136317f06deSGavin Shan  * device. If the device has enabled MSI or MSI-X interrupt, we needn't
137317f06deSGavin Shan  * do real work because EEH should freeze DMA transfers for those PCI
138317f06deSGavin Shan  * devices encountering EEH errors, which includes MSI or MSI-X.
139317f06deSGavin Shan  */
eeh_disable_irq(struct eeh_dev * edev)140010acfa1SSam Bobroff static void eeh_disable_irq(struct eeh_dev *edev)
141317f06deSGavin Shan {
142317f06deSGavin Shan 	/* Don't disable MSI and MSI-X interrupts. They are
143317f06deSGavin Shan 	 * effectively disabled by the DMA Stopped state
144317f06deSGavin Shan 	 * when an EEH error occurs.
145317f06deSGavin Shan 	 */
146010acfa1SSam Bobroff 	if (edev->pdev->msi_enabled || edev->pdev->msix_enabled)
147317f06deSGavin Shan 		return;
148317f06deSGavin Shan 
149010acfa1SSam Bobroff 	if (!irq_has_action(edev->pdev->irq))
150317f06deSGavin Shan 		return;
151317f06deSGavin Shan 
152317f06deSGavin Shan 	edev->mode |= EEH_DEV_IRQ_DISABLED;
153010acfa1SSam Bobroff 	disable_irq_nosync(edev->pdev->irq);
154317f06deSGavin Shan }
155317f06deSGavin Shan 
156317f06deSGavin Shan /**
157317f06deSGavin Shan  * eeh_enable_irq - Enable interrupt for the recovering device
158317f06deSGavin Shan  * @dev: PCI device
159317f06deSGavin Shan  *
160317f06deSGavin Shan  * This routine must be called to enable interrupt while failed
161317f06deSGavin Shan  * device could be resumed.
162317f06deSGavin Shan  */
eeh_enable_irq(struct eeh_dev * edev)163010acfa1SSam Bobroff static void eeh_enable_irq(struct eeh_dev *edev)
164317f06deSGavin Shan {
165317f06deSGavin Shan 	if ((edev->mode) & EEH_DEV_IRQ_DISABLED) {
166317f06deSGavin Shan 		edev->mode &= ~EEH_DEV_IRQ_DISABLED;
167b8a9a11bSThomas Gleixner 		/*
168b8a9a11bSThomas Gleixner 		 * FIXME !!!!!
169b8a9a11bSThomas Gleixner 		 *
170b8a9a11bSThomas Gleixner 		 * This is just ass backwards. This maze has
171b8a9a11bSThomas Gleixner 		 * unbalanced irq_enable/disable calls. So instead of
172b8a9a11bSThomas Gleixner 		 * finding the root cause it works around the warning
173b8a9a11bSThomas Gleixner 		 * in the irq_enable code by conditionally calling
174b8a9a11bSThomas Gleixner 		 * into it.
175b8a9a11bSThomas Gleixner 		 *
176b8a9a11bSThomas Gleixner 		 * That's just wrong.The warning in the core code is
177027dfac6SMichael Ellerman 		 * there to tell people to fix their asymmetries in
178b8a9a11bSThomas Gleixner 		 * their own code, not by abusing the core information
179b8a9a11bSThomas Gleixner 		 * to avoid it.
180b8a9a11bSThomas Gleixner 		 *
181b8a9a11bSThomas Gleixner 		 * I so wish that the assymetry would be the other way
182b8a9a11bSThomas Gleixner 		 * round and a few more irq_disable calls render that
183b8a9a11bSThomas Gleixner 		 * shit unusable forever.
184b8a9a11bSThomas Gleixner 		 *
185b8a9a11bSThomas Gleixner 		 *	tglx
186b8a9a11bSThomas Gleixner 		 */
187010acfa1SSam Bobroff 		if (irqd_irq_disabled(irq_get_irq_data(edev->pdev->irq)))
188010acfa1SSam Bobroff 			enable_irq(edev->pdev->irq);
189317f06deSGavin Shan 	}
19057310c3cSThomas Gleixner }
191317f06deSGavin Shan 
eeh_dev_save_state(struct eeh_dev * edev,void * userdata)192cef50c67SSam Bobroff static void eeh_dev_save_state(struct eeh_dev *edev, void *userdata)
1935cfb20b9SGavin Shan {
1945cfb20b9SGavin Shan 	struct pci_dev *pdev;
1955cfb20b9SGavin Shan 
1965cfb20b9SGavin Shan 	if (!edev)
197cef50c67SSam Bobroff 		return;
1985cfb20b9SGavin Shan 
1995a0cdbfdSGavin Shan 	/*
2005a0cdbfdSGavin Shan 	 * We cannot access the config space on some adapters.
2015a0cdbfdSGavin Shan 	 * Otherwise, it will cause fenced PHB. We don't save
2025a0cdbfdSGavin Shan 	 * the content in their config space and will restore
2035a0cdbfdSGavin Shan 	 * from the initial config space saved when the EEH
2045a0cdbfdSGavin Shan 	 * device is created.
2055a0cdbfdSGavin Shan 	 */
2065a0cdbfdSGavin Shan 	if (edev->pe && (edev->pe->state & EEH_PE_CFG_RESTRICTED))
207cef50c67SSam Bobroff 		return;
2085a0cdbfdSGavin Shan 
2095cfb20b9SGavin Shan 	pdev = eeh_dev_to_pci_dev(edev);
2105cfb20b9SGavin Shan 	if (!pdev)
211cef50c67SSam Bobroff 		return;
2125cfb20b9SGavin Shan 
2135cfb20b9SGavin Shan 	pci_save_state(pdev);
2145cfb20b9SGavin Shan }
2155cfb20b9SGavin Shan 
eeh_set_channel_state(struct eeh_pe * root,pci_channel_state_t s)21616d79cd4SLuc Van Oostenryck static void eeh_set_channel_state(struct eeh_pe *root, pci_channel_state_t s)
21747cc8c1cSSam Bobroff {
21847cc8c1cSSam Bobroff 	struct eeh_pe *pe;
21947cc8c1cSSam Bobroff 	struct eeh_dev *edev, *tmp;
22047cc8c1cSSam Bobroff 
22147cc8c1cSSam Bobroff 	eeh_for_each_pe(root, pe)
22247cc8c1cSSam Bobroff 		eeh_pe_for_each_dev(pe, edev, tmp)
22347cc8c1cSSam Bobroff 			if (eeh_edev_actionable(edev))
22447cc8c1cSSam Bobroff 				edev->pdev->error_state = s;
22547cc8c1cSSam Bobroff }
22647cc8c1cSSam Bobroff 
eeh_set_irq_state(struct eeh_pe * root,bool enable)227010acfa1SSam Bobroff static void eeh_set_irq_state(struct eeh_pe *root, bool enable)
228010acfa1SSam Bobroff {
229010acfa1SSam Bobroff 	struct eeh_pe *pe;
230010acfa1SSam Bobroff 	struct eeh_dev *edev, *tmp;
231010acfa1SSam Bobroff 
232010acfa1SSam Bobroff 	eeh_for_each_pe(root, pe) {
233010acfa1SSam Bobroff 		eeh_pe_for_each_dev(pe, edev, tmp) {
234010acfa1SSam Bobroff 			if (!eeh_edev_actionable(edev))
235010acfa1SSam Bobroff 				continue;
236010acfa1SSam Bobroff 
237010acfa1SSam Bobroff 			if (!eeh_pcid_get(edev->pdev))
238010acfa1SSam Bobroff 				continue;
239010acfa1SSam Bobroff 
240010acfa1SSam Bobroff 			if (enable)
241010acfa1SSam Bobroff 				eeh_enable_irq(edev);
242010acfa1SSam Bobroff 			else
243010acfa1SSam Bobroff 				eeh_disable_irq(edev);
244010acfa1SSam Bobroff 
245010acfa1SSam Bobroff 			eeh_pcid_put(edev->pdev);
246010acfa1SSam Bobroff 		}
247010acfa1SSam Bobroff 	}
248010acfa1SSam Bobroff }
249010acfa1SSam Bobroff 
25020b34497SSam Bobroff typedef enum pci_ers_result (*eeh_report_fn)(struct eeh_dev *,
2512e255051SSam Bobroff 					     struct pci_dev *,
25220b34497SSam Bobroff 					     struct pci_driver *);
25320b34497SSam Bobroff static void eeh_pe_report_edev(struct eeh_dev *edev, eeh_report_fn fn,
25420b34497SSam Bobroff 			       enum pci_ers_result *result)
25520b34497SSam Bobroff {
2562e255051SSam Bobroff 	struct pci_dev *pdev;
25720b34497SSam Bobroff 	struct pci_driver *driver;
25820b34497SSam Bobroff 	enum pci_ers_result new_result;
25920b34497SSam Bobroff 
2602e255051SSam Bobroff 	pci_lock_rescan_remove();
2612e255051SSam Bobroff 	pdev = edev->pdev;
2622e255051SSam Bobroff 	if (pdev)
2632e255051SSam Bobroff 		get_device(&pdev->dev);
2642e255051SSam Bobroff 	pci_unlock_rescan_remove();
2652e255051SSam Bobroff 	if (!pdev) {
266bcbe3730SSam Bobroff 		eeh_edev_info(edev, "no device");
267bcbe3730SSam Bobroff 		return;
268bcbe3730SSam Bobroff 	}
2692e255051SSam Bobroff 	device_lock(&pdev->dev);
27020b34497SSam Bobroff 	if (eeh_edev_actionable(edev)) {
2712e255051SSam Bobroff 		driver = eeh_pcid_get(pdev);
27220b34497SSam Bobroff 
27320b34497SSam Bobroff 		if (!driver)
27420b34497SSam Bobroff 			eeh_edev_info(edev, "no driver");
27520b34497SSam Bobroff 		else if (!driver->err_handler)
27620b34497SSam Bobroff 			eeh_edev_info(edev, "driver not EEH aware");
27720b34497SSam Bobroff 		else if (edev->mode & EEH_DEV_NO_HANDLER)
27820b34497SSam Bobroff 			eeh_edev_info(edev, "driver bound too late");
27920b34497SSam Bobroff 		else {
2802e255051SSam Bobroff 			new_result = fn(edev, pdev, driver);
28120b34497SSam Bobroff 			eeh_edev_info(edev, "%s driver reports: '%s'",
28220b34497SSam Bobroff 				      driver->name,
28320b34497SSam Bobroff 				      pci_ers_result_name(new_result));
28420b34497SSam Bobroff 			if (result)
28520b34497SSam Bobroff 				*result = pci_ers_merge_result(*result,
28620b34497SSam Bobroff 							       new_result);
28720b34497SSam Bobroff 		}
28820b34497SSam Bobroff 		if (driver)
2892e255051SSam Bobroff 			eeh_pcid_put(pdev);
29020b34497SSam Bobroff 	} else {
2912e255051SSam Bobroff 		eeh_edev_info(edev, "not actionable (%d,%d,%d)", !!pdev,
29220b34497SSam Bobroff 			      !eeh_dev_removed(edev), !eeh_pe_passed(edev->pe));
29320b34497SSam Bobroff 	}
2942e255051SSam Bobroff 	device_unlock(&pdev->dev);
2952e255051SSam Bobroff 	if (edev->pdev != pdev)
2962e255051SSam Bobroff 		eeh_edev_warn(edev, "Device changed during processing!\n");
2972e255051SSam Bobroff 	put_device(&pdev->dev);
29820b34497SSam Bobroff }
29920b34497SSam Bobroff 
eeh_pe_report(const char * name,struct eeh_pe * root,eeh_report_fn fn,enum pci_ers_result * result)30020b34497SSam Bobroff static void eeh_pe_report(const char *name, struct eeh_pe *root,
30120b34497SSam Bobroff 			  eeh_report_fn fn, enum pci_ers_result *result)
30220b34497SSam Bobroff {
30320b34497SSam Bobroff 	struct eeh_pe *pe;
30420b34497SSam Bobroff 	struct eeh_dev *edev, *tmp;
30520b34497SSam Bobroff 
30620b34497SSam Bobroff 	pr_info("EEH: Beginning: '%s'\n", name);
30720b34497SSam Bobroff 	eeh_for_each_pe(root, pe) eeh_pe_for_each_dev(pe, edev, tmp)
30820b34497SSam Bobroff 		eeh_pe_report_edev(edev, fn, result);
30920b34497SSam Bobroff 	if (result)
31020b34497SSam Bobroff 		pr_info("EEH: Finished:'%s' with aggregate recovery state:'%s'\n",
31120b34497SSam Bobroff 			name, pci_ers_result_name(*result));
31220b34497SSam Bobroff 	else
31320b34497SSam Bobroff 		pr_info("EEH: Finished:'%s'", name);
31420b34497SSam Bobroff }
31520b34497SSam Bobroff 
316317f06deSGavin Shan /**
317317f06deSGavin Shan  * eeh_report_error - Report pci error to each device driver
31820b34497SSam Bobroff  * @edev: eeh device
31920b34497SSam Bobroff  * @driver: device's PCI driver
320317f06deSGavin Shan  *
32120b34497SSam Bobroff  * Report an EEH error to each device driver.
322317f06deSGavin Shan  */
eeh_report_error(struct eeh_dev * edev,struct pci_dev * pdev,struct pci_driver * driver)32320b34497SSam Bobroff static enum pci_ers_result eeh_report_error(struct eeh_dev *edev,
3242e255051SSam Bobroff 					    struct pci_dev *pdev,
32520b34497SSam Bobroff 					    struct pci_driver *driver)
326317f06deSGavin Shan {
32720b34497SSam Bobroff 	enum pci_ers_result rc;
328317f06deSGavin Shan 
32920b34497SSam Bobroff 	if (!driver->err_handler->error_detected)
33020b34497SSam Bobroff 		return PCI_ERS_RESULT_NONE;
331f0295e04SMichael Neuling 
33220b34497SSam Bobroff 	eeh_edev_info(edev, "Invoking %s->error_detected(IO frozen)",
33320b34497SSam Bobroff 		      driver->name);
3342e255051SSam Bobroff 	rc = driver->err_handler->error_detected(pdev, pci_channel_io_frozen);
335317f06deSGavin Shan 
33667086e32SWei Yang 	edev->in_error = true;
3372e255051SSam Bobroff 	pci_uevent_ers(pdev, PCI_ERS_RESULT_NONE);
33820b34497SSam Bobroff 	return rc;
339317f06deSGavin Shan }
340317f06deSGavin Shan 
341317f06deSGavin Shan /**
342317f06deSGavin Shan  * eeh_report_mmio_enabled - Tell drivers that MMIO has been enabled
34320b34497SSam Bobroff  * @edev: eeh device
34420b34497SSam Bobroff  * @driver: device's PCI driver
345317f06deSGavin Shan  *
346317f06deSGavin Shan  * Tells each device driver that IO ports, MMIO and config space I/O
34720b34497SSam Bobroff  * are now enabled.
348317f06deSGavin Shan  */
eeh_report_mmio_enabled(struct eeh_dev * edev,struct pci_dev * pdev,struct pci_driver * driver)34920b34497SSam Bobroff static enum pci_ers_result eeh_report_mmio_enabled(struct eeh_dev *edev,
3502e255051SSam Bobroff 						   struct pci_dev *pdev,
35120b34497SSam Bobroff 						   struct pci_driver *driver)
352317f06deSGavin Shan {
35320b34497SSam Bobroff 	if (!driver->err_handler->mmio_enabled)
35420b34497SSam Bobroff 		return PCI_ERS_RESULT_NONE;
35520b34497SSam Bobroff 	eeh_edev_info(edev, "Invoking %s->mmio_enabled()", driver->name);
3562e255051SSam Bobroff 	return driver->err_handler->mmio_enabled(pdev);
357317f06deSGavin Shan }
358317f06deSGavin Shan 
359317f06deSGavin Shan /**
360317f06deSGavin Shan  * eeh_report_reset - Tell device that slot has been reset
36120b34497SSam Bobroff  * @edev: eeh device
36220b34497SSam Bobroff  * @driver: device's PCI driver
363317f06deSGavin Shan  *
364317f06deSGavin Shan  * This routine must be called while EEH tries to reset particular
365317f06deSGavin Shan  * PCI device so that the associated PCI device driver could take
366317f06deSGavin Shan  * some actions, usually to save data the driver needs so that the
367317f06deSGavin Shan  * driver can work again while the device is recovered.
368317f06deSGavin Shan  */
eeh_report_reset(struct eeh_dev * edev,struct pci_dev * pdev,struct pci_driver * driver)36920b34497SSam Bobroff static enum pci_ers_result eeh_report_reset(struct eeh_dev *edev,
3702e255051SSam Bobroff 					    struct pci_dev *pdev,
37120b34497SSam Bobroff 					    struct pci_driver *driver)
372317f06deSGavin Shan {
37320b34497SSam Bobroff 	if (!driver->err_handler->slot_reset || !edev->in_error)
37420b34497SSam Bobroff 		return PCI_ERS_RESULT_NONE;
37520b34497SSam Bobroff 	eeh_edev_info(edev, "Invoking %s->slot_reset()", driver->name);
3762e255051SSam Bobroff 	return driver->err_handler->slot_reset(pdev);
377317f06deSGavin Shan }
378317f06deSGavin Shan 
eeh_dev_restore_state(struct eeh_dev * edev,void * userdata)379cef50c67SSam Bobroff static void eeh_dev_restore_state(struct eeh_dev *edev, void *userdata)
3805cfb20b9SGavin Shan {
3815cfb20b9SGavin Shan 	struct pci_dev *pdev;
3825cfb20b9SGavin Shan 
3835cfb20b9SGavin Shan 	if (!edev)
384cef50c67SSam Bobroff 		return;
3855cfb20b9SGavin Shan 
3865a0cdbfdSGavin Shan 	/*
3875a0cdbfdSGavin Shan 	 * The content in the config space isn't saved because
3885a0cdbfdSGavin Shan 	 * the blocked config space on some adapters. We have
3895a0cdbfdSGavin Shan 	 * to restore the initial saved config space when the
3905a0cdbfdSGavin Shan 	 * EEH device is created.
3915a0cdbfdSGavin Shan 	 */
3925a0cdbfdSGavin Shan 	if (edev->pe && (edev->pe->state & EEH_PE_CFG_RESTRICTED)) {
39380e65b00SSam Bobroff 		if (list_is_last(&edev->entry, &edev->pe->edevs))
3945a0cdbfdSGavin Shan 			eeh_pe_restore_bars(edev->pe);
3955a0cdbfdSGavin Shan 
396cef50c67SSam Bobroff 		return;
3975a0cdbfdSGavin Shan 	}
3985a0cdbfdSGavin Shan 
3995cfb20b9SGavin Shan 	pdev = eeh_dev_to_pci_dev(edev);
4005cfb20b9SGavin Shan 	if (!pdev)
401cef50c67SSam Bobroff 		return;
4025cfb20b9SGavin Shan 
4035cfb20b9SGavin Shan 	pci_restore_state(pdev);
4045cfb20b9SGavin Shan }
4055cfb20b9SGavin Shan 
406317f06deSGavin Shan /**
407317f06deSGavin Shan  * eeh_report_resume - Tell device to resume normal operations
40820b34497SSam Bobroff  * @edev: eeh device
40920b34497SSam Bobroff  * @driver: device's PCI driver
410317f06deSGavin Shan  *
411317f06deSGavin Shan  * This routine must be called to notify the device driver that it
412317f06deSGavin Shan  * could resume so that the device driver can do some initialization
413317f06deSGavin Shan  * to make the recovered device work again.
414317f06deSGavin Shan  */
eeh_report_resume(struct eeh_dev * edev,struct pci_dev * pdev,struct pci_driver * driver)41520b34497SSam Bobroff static enum pci_ers_result eeh_report_resume(struct eeh_dev *edev,
4162e255051SSam Bobroff 					     struct pci_dev *pdev,
41720b34497SSam Bobroff 					     struct pci_driver *driver)
418317f06deSGavin Shan {
41920b34497SSam Bobroff 	if (!driver->err_handler->resume || !edev->in_error)
42020b34497SSam Bobroff 		return PCI_ERS_RESULT_NONE;
421317f06deSGavin Shan 
42220b34497SSam Bobroff 	eeh_edev_info(edev, "Invoking %s->resume()", driver->name);
4232e255051SSam Bobroff 	driver->err_handler->resume(pdev);
424f0295e04SMichael Neuling 
42520b34497SSam Bobroff 	pci_uevent_ers(edev->pdev, PCI_ERS_RESULT_RECOVERED);
426856e1eb9SBryant G. Ly #ifdef CONFIG_PCI_IOV
4278225d543SOliver O'Halloran 	if (eeh_ops->notify_resume)
4288225d543SOliver O'Halloran 		eeh_ops->notify_resume(edev);
429856e1eb9SBryant G. Ly #endif
43020b34497SSam Bobroff 	return PCI_ERS_RESULT_NONE;
431317f06deSGavin Shan }
432317f06deSGavin Shan 
433317f06deSGavin Shan /**
434317f06deSGavin Shan  * eeh_report_failure - Tell device driver that device is dead.
43520b34497SSam Bobroff  * @edev: eeh device
43620b34497SSam Bobroff  * @driver: device's PCI driver
437317f06deSGavin Shan  *
438317f06deSGavin Shan  * This informs the device driver that the device is permanently
439317f06deSGavin Shan  * dead, and that no further recovery attempts will be made on it.
440317f06deSGavin Shan  */
eeh_report_failure(struct eeh_dev * edev,struct pci_dev * pdev,struct pci_driver * driver)44120b34497SSam Bobroff static enum pci_ers_result eeh_report_failure(struct eeh_dev *edev,
4422e255051SSam Bobroff 					      struct pci_dev *pdev,
44320b34497SSam Bobroff 					      struct pci_driver *driver)
444317f06deSGavin Shan {
44520b34497SSam Bobroff 	enum pci_ers_result rc;
446317f06deSGavin Shan 
44720b34497SSam Bobroff 	if (!driver->err_handler->error_detected)
44820b34497SSam Bobroff 		return PCI_ERS_RESULT_NONE;
449f0295e04SMichael Neuling 
45020b34497SSam Bobroff 	eeh_edev_info(edev, "Invoking %s->error_detected(permanent failure)",
45120b34497SSam Bobroff 		      driver->name);
4522e255051SSam Bobroff 	rc = driver->err_handler->error_detected(pdev,
45320b34497SSam Bobroff 						 pci_channel_io_perm_failure);
454317f06deSGavin Shan 
4552e255051SSam Bobroff 	pci_uevent_ers(pdev, PCI_ERS_RESULT_DISCONNECT);
45620b34497SSam Bobroff 	return rc;
457317f06deSGavin Shan }
458317f06deSGavin Shan 
eeh_add_virt_device(struct eeh_dev * edev)459bf773df9SSam Bobroff static void *eeh_add_virt_device(struct eeh_dev *edev)
46067086e32SWei Yang {
46167086e32SWei Yang 	struct pci_driver *driver;
46267086e32SWei Yang 	struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
46367086e32SWei Yang 
46467086e32SWei Yang 	if (!(edev->physfn)) {
4651ff8f36fSSam Bobroff 		eeh_edev_warn(edev, "Not for VF\n");
46667086e32SWei Yang 		return NULL;
46767086e32SWei Yang 	}
46867086e32SWei Yang 
46967086e32SWei Yang 	driver = eeh_pcid_get(dev);
47067086e32SWei Yang 	if (driver) {
47146d4be41SSam Bobroff 		if (driver->err_handler) {
47267086e32SWei Yang 			eeh_pcid_put(dev);
47367086e32SWei Yang 			return NULL;
47467086e32SWei Yang 		}
47546d4be41SSam Bobroff 		eeh_pcid_put(dev);
47646d4be41SSam Bobroff 	}
47767086e32SWei Yang 
478988fc3baSBryant G. Ly #ifdef CONFIG_PCI_IOV
479dffa9153SOliver O'Halloran 	pci_iov_add_virtfn(edev->physfn, edev->vf_index);
48067086e32SWei Yang #endif
48167086e32SWei Yang 	return NULL;
48267086e32SWei Yang }
48367086e32SWei Yang 
eeh_rmv_device(struct eeh_dev * edev,void * userdata)484cef50c67SSam Bobroff static void eeh_rmv_device(struct eeh_dev *edev, void *userdata)
485f5c57710SGavin Shan {
486f5c57710SGavin Shan 	struct pci_driver *driver;
487f5c57710SGavin Shan 	struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
48867086e32SWei Yang 	struct eeh_rmv_data *rmv_data = (struct eeh_rmv_data *)userdata;
489f5c57710SGavin Shan 
490f5c57710SGavin Shan 	/*
491f5c57710SGavin Shan 	 * Actually, we should remove the PCI bridges as well.
492f5c57710SGavin Shan 	 * However, that's lots of complexity to do that,
493f5c57710SGavin Shan 	 * particularly some of devices under the bridge might
494f5c57710SGavin Shan 	 * support EEH. So we just care about PCI devices for
495f5c57710SGavin Shan 	 * simplicity here.
496f5c57710SGavin Shan 	 */
4971ef52073SSam Bobroff 	if (!eeh_edev_actionable(edev) ||
4981ef52073SSam Bobroff 	    (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE))
499cef50c67SSam Bobroff 		return;
500d2b0f6f7SGavin Shan 
5011c5c533bSSam Bobroff 	if (rmv_data) {
502f5c57710SGavin Shan 		driver = eeh_pcid_get(dev);
5038cc6b6cdSThadeu Lima de Souza Cascardo 		if (driver) {
50446d4be41SSam Bobroff 			if (driver->err_handler &&
505f2da4ccfSGavin Shan 			    driver->err_handler->error_detected &&
50646d4be41SSam Bobroff 			    driver->err_handler->slot_reset) {
50746d4be41SSam Bobroff 				eeh_pcid_put(dev);
508cef50c67SSam Bobroff 				return;
5098cc6b6cdSThadeu Lima de Souza Cascardo 			}
51046d4be41SSam Bobroff 			eeh_pcid_put(dev);
51146d4be41SSam Bobroff 		}
51246d4be41SSam Bobroff 	}
513f5c57710SGavin Shan 
514f5c57710SGavin Shan 	/* Remove it from PCI subsystem */
5151ef52073SSam Bobroff 	pr_info("EEH: Removing %s without EEH sensitive driver\n",
516f5c57710SGavin Shan 		pci_name(dev));
517f5c57710SGavin Shan 	edev->mode |= EEH_DEV_DISCONNECTED;
5181c5c533bSSam Bobroff 	if (rmv_data)
5191c5c533bSSam Bobroff 		rmv_data->removed_dev_count++;
520f5c57710SGavin Shan 
52167086e32SWei Yang 	if (edev->physfn) {
522988fc3baSBryant G. Ly #ifdef CONFIG_PCI_IOV
523dffa9153SOliver O'Halloran 		pci_iov_remove_virtfn(edev->physfn, edev->vf_index);
52467086e32SWei Yang 		edev->pdev = NULL;
52567086e32SWei Yang #endif
52667086e32SWei Yang 		if (rmv_data)
5271c5c533bSSam Bobroff 			list_add(&edev->rmv_entry, &rmv_data->removed_vf_list);
52867086e32SWei Yang 	} else {
5291c2042c8SRafael J. Wysocki 		pci_lock_rescan_remove();
530f5c57710SGavin Shan 		pci_stop_and_remove_bus_device(dev);
5311c2042c8SRafael J. Wysocki 		pci_unlock_rescan_remove();
53267086e32SWei Yang 	}
533f5c57710SGavin Shan }
534f5c57710SGavin Shan 
eeh_pe_detach_dev(struct eeh_pe * pe,void * userdata)535d6c4932fSSam Bobroff static void *eeh_pe_detach_dev(struct eeh_pe *pe, void *userdata)
536f5c57710SGavin Shan {
537f5c57710SGavin Shan 	struct eeh_dev *edev, *tmp;
538f5c57710SGavin Shan 
539f5c57710SGavin Shan 	eeh_pe_for_each_dev(pe, edev, tmp) {
540f5c57710SGavin Shan 		if (!(edev->mode & EEH_DEV_DISCONNECTED))
541f5c57710SGavin Shan 			continue;
542f5c57710SGavin Shan 
543f5c57710SGavin Shan 		edev->mode &= ~(EEH_DEV_DISCONNECTED | EEH_DEV_IRQ_DISABLED);
544d923ab7aSOliver O'Halloran 		eeh_pe_tree_remove(edev);
545f5c57710SGavin Shan 	}
546f5c57710SGavin Shan 
547f5c57710SGavin Shan 	return NULL;
548f5c57710SGavin Shan }
549f5c57710SGavin Shan 
55078954700SGavin Shan /*
55178954700SGavin Shan  * Explicitly clear PE's frozen state for PowerNV where
55278954700SGavin Shan  * we have frozen PE until BAR restore is completed. It's
55378954700SGavin Shan  * harmless to clear it for pSeries. To be consistent with
55478954700SGavin Shan  * PE reset (for 3 times), we try to clear the frozen state
55578954700SGavin Shan  * for 3 times as well.
55678954700SGavin Shan  */
eeh_clear_pe_frozen_state(struct eeh_pe * root,bool include_passed)5574d8e325dSSam Bobroff static int eeh_clear_pe_frozen_state(struct eeh_pe *root, bool include_passed)
55878954700SGavin Shan {
5593376cb91SSam Bobroff 	struct eeh_pe *pe;
5603376cb91SSam Bobroff 	int i;
56178954700SGavin Shan 
5623376cb91SSam Bobroff 	eeh_for_each_pe(root, pe) {
5634d8e325dSSam Bobroff 		if (include_passed || !eeh_pe_passed(pe)) {
5643376cb91SSam Bobroff 			for (i = 0; i < 3; i++)
565188fdea6SSam Bobroff 				if (!eeh_unfreeze_pe(pe))
5663376cb91SSam Bobroff 					break;
5673376cb91SSam Bobroff 			if (i >= 3)
5683376cb91SSam Bobroff 				return -EIO;
5692c665992SGavin Shan 		}
5704d8e325dSSam Bobroff 	}
5714d8e325dSSam Bobroff 	eeh_pe_state_clear(root, EEH_PE_ISOLATED, include_passed);
5723376cb91SSam Bobroff 	return 0;
57378954700SGavin Shan }
57478954700SGavin Shan 
eeh_pe_reset_and_recover(struct eeh_pe * pe)5755cfb20b9SGavin Shan int eeh_pe_reset_and_recover(struct eeh_pe *pe)
5765cfb20b9SGavin Shan {
5772efc771fSGavin Shan 	int ret;
5785cfb20b9SGavin Shan 
5795cfb20b9SGavin Shan 	/* Bail if the PE is being recovered */
5805cfb20b9SGavin Shan 	if (pe->state & EEH_PE_RECOVERING)
5815cfb20b9SGavin Shan 		return 0;
5825cfb20b9SGavin Shan 
5835cfb20b9SGavin Shan 	/* Put the PE into recovery mode */
5845cfb20b9SGavin Shan 	eeh_pe_state_mark(pe, EEH_PE_RECOVERING);
5855cfb20b9SGavin Shan 
5865cfb20b9SGavin Shan 	/* Save states */
5875cfb20b9SGavin Shan 	eeh_pe_dev_traverse(pe, eeh_dev_save_state, NULL);
5885cfb20b9SGavin Shan 
5895cfb20b9SGavin Shan 	/* Issue reset */
5901ef52073SSam Bobroff 	ret = eeh_pe_reset_full(pe, true);
5915cfb20b9SGavin Shan 	if (ret) {
5929ed5ca66SSam Bobroff 		eeh_pe_state_clear(pe, EEH_PE_RECOVERING, true);
5935cfb20b9SGavin Shan 		return ret;
5945cfb20b9SGavin Shan 	}
5955cfb20b9SGavin Shan 
5965cfb20b9SGavin Shan 	/* Unfreeze the PE */
5974d8e325dSSam Bobroff 	ret = eeh_clear_pe_frozen_state(pe, true);
5985cfb20b9SGavin Shan 	if (ret) {
5999ed5ca66SSam Bobroff 		eeh_pe_state_clear(pe, EEH_PE_RECOVERING, true);
6005cfb20b9SGavin Shan 		return ret;
6015cfb20b9SGavin Shan 	}
6025cfb20b9SGavin Shan 
6035cfb20b9SGavin Shan 	/* Restore device state */
6045cfb20b9SGavin Shan 	eeh_pe_dev_traverse(pe, eeh_dev_restore_state, NULL);
6055cfb20b9SGavin Shan 
6065cfb20b9SGavin Shan 	/* Clear recovery mode */
6079ed5ca66SSam Bobroff 	eeh_pe_state_clear(pe, EEH_PE_RECOVERING, true);
6085cfb20b9SGavin Shan 
6095cfb20b9SGavin Shan 	return 0;
6105cfb20b9SGavin Shan }
6115cfb20b9SGavin Shan 
612317f06deSGavin Shan /**
613317f06deSGavin Shan  * eeh_reset_device - Perform actual reset of a pci slot
6145fd13460SSam Bobroff  * @driver_eeh_aware: Does the device's driver provide EEH support?
615317f06deSGavin Shan  * @pe: EEH PE
616317f06deSGavin Shan  * @bus: PCI bus corresponding to the isolcated slot
6175fd13460SSam Bobroff  * @rmv_data: Optional, list to record removed devices
618317f06deSGavin Shan  *
619317f06deSGavin Shan  * This routine must be called to do reset on the indicated PE.
620317f06deSGavin Shan  * During the reset, udev might be invoked because those affected
621317f06deSGavin Shan  * PCI devices will be removed and then added.
622317f06deSGavin Shan  */
eeh_reset_device(struct eeh_pe * pe,struct pci_bus * bus,struct eeh_rmv_data * rmv_data,bool driver_eeh_aware)62367086e32SWei Yang static int eeh_reset_device(struct eeh_pe *pe, struct pci_bus *bus,
6245fd13460SSam Bobroff 			    struct eeh_rmv_data *rmv_data,
6255fd13460SSam Bobroff 			    bool driver_eeh_aware)
626317f06deSGavin Shan {
627edfd17ffSArnd Bergmann 	time64_t tstamp;
62867086e32SWei Yang 	int cnt, rc;
62967086e32SWei Yang 	struct eeh_dev *edev;
6301ef52073SSam Bobroff 	struct eeh_pe *tmp_pe;
6311ef52073SSam Bobroff 	bool any_passed = false;
6321ef52073SSam Bobroff 
6331ef52073SSam Bobroff 	eeh_for_each_pe(pe, tmp_pe)
6341ef52073SSam Bobroff 		any_passed |= eeh_pe_passed(tmp_pe);
635317f06deSGavin Shan 
636317f06deSGavin Shan 	/* pcibios will clear the counter; save the value */
637317f06deSGavin Shan 	cnt = pe->freeze_count;
6385a71978eSGavin Shan 	tstamp = pe->tstamp;
639317f06deSGavin Shan 
640317f06deSGavin Shan 	/*
641317f06deSGavin Shan 	 * We don't remove the corresponding PE instances because
642317f06deSGavin Shan 	 * we need the information afterwords. The attached EEH
643317f06deSGavin Shan 	 * devices are expected to be attached soon when calling
644bd251b89SGavin Shan 	 * into pci_hp_add_devices().
645317f06deSGavin Shan 	 */
646807a827dSGavin Shan 	eeh_pe_state_mark(pe, EEH_PE_KEEP);
6471ef52073SSam Bobroff 	if (any_passed || driver_eeh_aware || (pe->type & EEH_PE_VF)) {
64854048cf8SSam Bobroff 		eeh_pe_dev_traverse(pe, eeh_rmv_device, rmv_data);
64967086e32SWei Yang 	} else {
6501c2042c8SRafael J. Wysocki 		pci_lock_rescan_remove();
651bd251b89SGavin Shan 		pci_hp_remove_devices(bus);
6521c2042c8SRafael J. Wysocki 		pci_unlock_rescan_remove();
65367086e32SWei Yang 	}
654317f06deSGavin Shan 
655d0914f50SGavin Shan 	/*
656d0914f50SGavin Shan 	 * Reset the pci controller. (Asserts RST#; resets config space).
657317f06deSGavin Shan 	 * Reconfigure bridges and devices. Don't try to bring the system
658317f06deSGavin Shan 	 * up if the reset failed for some reason.
659d0914f50SGavin Shan 	 *
660d0914f50SGavin Shan 	 * During the reset, it's very dangerous to have uncontrolled PCI
661d0914f50SGavin Shan 	 * config accesses. So we prefer to block them. However, controlled
662d0914f50SGavin Shan 	 * PCI config accesses initiated from EEH itself are allowed.
663317f06deSGavin Shan 	 */
6641ef52073SSam Bobroff 	rc = eeh_pe_reset_full(pe, false);
66528bf36f9SGavin Shan 	if (rc)
666317f06deSGavin Shan 		return rc;
667317f06deSGavin Shan 
6681c2042c8SRafael J. Wysocki 	pci_lock_rescan_remove();
6691c2042c8SRafael J. Wysocki 
670317f06deSGavin Shan 	/* Restore PE */
671317f06deSGavin Shan 	eeh_ops->configure_bridge(pe);
672317f06deSGavin Shan 	eeh_pe_restore_bars(pe);
673317f06deSGavin Shan 
674dc9c41bdSAndrew Donnellan 	/* Clear frozen state */
6751ef52073SSam Bobroff 	rc = eeh_clear_pe_frozen_state(pe, false);
676409bf7f8SAndrew Donnellan 	if (rc) {
677409bf7f8SAndrew Donnellan 		pci_unlock_rescan_remove();
67878954700SGavin Shan 		return rc;
679409bf7f8SAndrew Donnellan 	}
68078954700SGavin Shan 
681317f06deSGavin Shan 	/* Give the system 5 seconds to finish running the user-space
682317f06deSGavin Shan 	 * hotplug shutdown scripts, e.g. ifdown for ethernet.  Yes,
683317f06deSGavin Shan 	 * this is a hack, but if we don't do this, and try to bring
684317f06deSGavin Shan 	 * the device up before the scripts have taken it down,
685317f06deSGavin Shan 	 * potentially weird things happen.
686317f06deSGavin Shan 	 */
6871c5c533bSSam Bobroff 	if (!driver_eeh_aware || rmv_data->removed_dev_count) {
68854048cf8SSam Bobroff 		pr_info("EEH: Sleep 5s ahead of %s hotplug\n",
68954048cf8SSam Bobroff 			(driver_eeh_aware ? "partial" : "complete"));
690317f06deSGavin Shan 		ssleep(5);
691f5c57710SGavin Shan 
692f5c57710SGavin Shan 		/*
693f5c57710SGavin Shan 		 * The EEH device is still connected with its parent
694f5c57710SGavin Shan 		 * PE. We should disconnect it so the binding can be
695f5c57710SGavin Shan 		 * rebuilt when adding PCI devices.
696f5c57710SGavin Shan 		 */
69780e65b00SSam Bobroff 		edev = list_first_entry(&pe->edevs, struct eeh_dev, entry);
698f5c57710SGavin Shan 		eeh_pe_traverse(pe, eeh_pe_detach_dev, NULL);
699a3aa256bSGavin Shan 		if (pe->type & EEH_PE_VF) {
700bf773df9SSam Bobroff 			eeh_add_virt_device(edev);
701a3aa256bSGavin Shan 		} else {
70254048cf8SSam Bobroff 			if (!driver_eeh_aware)
7039ed5ca66SSam Bobroff 				eeh_pe_state_clear(pe, EEH_PE_PRI_BUS, true);
704bd251b89SGavin Shan 			pci_hp_add_devices(bus);
705a3aa256bSGavin Shan 		}
706317f06deSGavin Shan 	}
7079ed5ca66SSam Bobroff 	eeh_pe_state_clear(pe, EEH_PE_KEEP, true);
7085a71978eSGavin Shan 
7095a71978eSGavin Shan 	pe->tstamp = tstamp;
710317f06deSGavin Shan 	pe->freeze_count = cnt;
711317f06deSGavin Shan 
7121c2042c8SRafael J. Wysocki 	pci_unlock_rescan_remove();
713317f06deSGavin Shan 	return 0;
714317f06deSGavin Shan }
715317f06deSGavin Shan 
716317f06deSGavin Shan /* The longest amount of time to wait for a pci device
717317f06deSGavin Shan  * to come back on line, in seconds.
718317f06deSGavin Shan  */
719fb48dc22SBrian King #define MAX_WAIT_FOR_RECOVERY 300
720317f06deSGavin Shan 
721799abe28SOliver O'Halloran 
722799abe28SOliver O'Halloran /* Walks the PE tree after processing an event to remove any stale PEs.
723799abe28SOliver O'Halloran  *
724799abe28SOliver O'Halloran  * NB: This needs to be recursive to ensure the leaf PEs get removed
725799abe28SOliver O'Halloran  * before their parents do. Although this is possible to do recursively
726799abe28SOliver O'Halloran  * we don't since this is easier to read and we need to garantee
727799abe28SOliver O'Halloran  * the leaf nodes will be handled first.
728799abe28SOliver O'Halloran  */
eeh_pe_cleanup(struct eeh_pe * pe)729799abe28SOliver O'Halloran static void eeh_pe_cleanup(struct eeh_pe *pe)
730799abe28SOliver O'Halloran {
731799abe28SOliver O'Halloran 	struct eeh_pe *child_pe, *tmp;
732799abe28SOliver O'Halloran 
733799abe28SOliver O'Halloran 	list_for_each_entry_safe(child_pe, tmp, &pe->child_list, child)
734799abe28SOliver O'Halloran 		eeh_pe_cleanup(child_pe);
735799abe28SOliver O'Halloran 
736799abe28SOliver O'Halloran 	if (pe->state & EEH_PE_KEEP)
737799abe28SOliver O'Halloran 		return;
738799abe28SOliver O'Halloran 
739799abe28SOliver O'Halloran 	if (!(pe->state & EEH_PE_INVALID))
740799abe28SOliver O'Halloran 		return;
741799abe28SOliver O'Halloran 
742799abe28SOliver O'Halloran 	if (list_empty(&pe->edevs) && list_empty(&pe->child_list)) {
743799abe28SOliver O'Halloran 		list_del(&pe->child);
744799abe28SOliver O'Halloran 		kfree(pe);
745799abe28SOliver O'Halloran 	}
746799abe28SOliver O'Halloran }
747799abe28SOliver O'Halloran 
748c0b64978SRussell Currey /**
749b104af5aSOliver O'Halloran  * eeh_check_slot_presence - Check if a device is still present in a slot
750b104af5aSOliver O'Halloran  * @pdev: pci_dev to check
751b104af5aSOliver O'Halloran  *
752b104af5aSOliver O'Halloran  * This function may return a false positive if we can't determine the slot's
7532b461880SMichael Ellerman  * presence state. This might happen for PCIe slots if the PE containing
754b104af5aSOliver O'Halloran  * the upstream bridge is also frozen, or the bridge is part of the same PE
755b104af5aSOliver O'Halloran  * as the device.
756b104af5aSOliver O'Halloran  *
757b104af5aSOliver O'Halloran  * This shouldn't happen often, but you might see it if you hotplug a PCIe
758b104af5aSOliver O'Halloran  * switch.
759b104af5aSOliver O'Halloran  */
eeh_slot_presence_check(struct pci_dev * pdev)760b104af5aSOliver O'Halloran static bool eeh_slot_presence_check(struct pci_dev *pdev)
761b104af5aSOliver O'Halloran {
762b104af5aSOliver O'Halloran 	const struct hotplug_slot_ops *ops;
763b104af5aSOliver O'Halloran 	struct pci_slot *slot;
764b104af5aSOliver O'Halloran 	u8 state;
765b104af5aSOliver O'Halloran 	int rc;
766b104af5aSOliver O'Halloran 
767b104af5aSOliver O'Halloran 	if (!pdev)
768b104af5aSOliver O'Halloran 		return false;
769b104af5aSOliver O'Halloran 
770b104af5aSOliver O'Halloran 	if (pdev->error_state == pci_channel_io_perm_failure)
771b104af5aSOliver O'Halloran 		return false;
772b104af5aSOliver O'Halloran 
773b104af5aSOliver O'Halloran 	slot = pdev->slot;
774b104af5aSOliver O'Halloran 	if (!slot || !slot->hotplug)
775b104af5aSOliver O'Halloran 		return true;
776b104af5aSOliver O'Halloran 
777b104af5aSOliver O'Halloran 	ops = slot->hotplug->ops;
778b104af5aSOliver O'Halloran 	if (!ops || !ops->get_adapter_status)
779b104af5aSOliver O'Halloran 		return true;
780b104af5aSOliver O'Halloran 
781aeff27c1SOliver O'Halloran 	/* set the attention indicator while we've got the slot ops */
782aeff27c1SOliver O'Halloran 	if (ops->set_attention_status)
783aeff27c1SOliver O'Halloran 		ops->set_attention_status(slot->hotplug, 1);
784aeff27c1SOliver O'Halloran 
785b104af5aSOliver O'Halloran 	rc = ops->get_adapter_status(slot->hotplug, &state);
786b104af5aSOliver O'Halloran 	if (rc)
787b104af5aSOliver O'Halloran 		return true;
788b104af5aSOliver O'Halloran 
789b104af5aSOliver O'Halloran 	return !!state;
790b104af5aSOliver O'Halloran }
791b104af5aSOliver O'Halloran 
eeh_clear_slot_attention(struct pci_dev * pdev)792aeff27c1SOliver O'Halloran static void eeh_clear_slot_attention(struct pci_dev *pdev)
793aeff27c1SOliver O'Halloran {
794aeff27c1SOliver O'Halloran 	const struct hotplug_slot_ops *ops;
795aeff27c1SOliver O'Halloran 	struct pci_slot *slot;
796aeff27c1SOliver O'Halloran 
797aeff27c1SOliver O'Halloran 	if (!pdev)
798aeff27c1SOliver O'Halloran 		return;
799aeff27c1SOliver O'Halloran 
800aeff27c1SOliver O'Halloran 	if (pdev->error_state == pci_channel_io_perm_failure)
801aeff27c1SOliver O'Halloran 		return;
802aeff27c1SOliver O'Halloran 
803aeff27c1SOliver O'Halloran 	slot = pdev->slot;
804aeff27c1SOliver O'Halloran 	if (!slot || !slot->hotplug)
805aeff27c1SOliver O'Halloran 		return;
806aeff27c1SOliver O'Halloran 
807aeff27c1SOliver O'Halloran 	ops = slot->hotplug->ops;
808aeff27c1SOliver O'Halloran 	if (!ops || !ops->set_attention_status)
809aeff27c1SOliver O'Halloran 		return;
810aeff27c1SOliver O'Halloran 
811aeff27c1SOliver O'Halloran 	ops->set_attention_status(slot->hotplug, 0);
812aeff27c1SOliver O'Halloran }
813aeff27c1SOliver O'Halloran 
814b104af5aSOliver O'Halloran /**
815c0b64978SRussell Currey  * eeh_handle_normal_event - Handle EEH events on a specific PE
81637fd8125SSam Bobroff  * @pe: EEH PE - which should not be used after we return, as it may
81737fd8125SSam Bobroff  * have been invalidated.
818c0b64978SRussell Currey  *
819c0b64978SRussell Currey  * Attempts to recover the given PE.  If recovery fails or the PE has failed
820c0b64978SRussell Currey  * too many times, remove the PE.
821c0b64978SRussell Currey  *
82268701780SSam Bobroff  * While PHB detects address or data parity errors on particular PCI
82368701780SSam Bobroff  * slot, the associated PE will be frozen. Besides, DMA's occurring
82468701780SSam Bobroff  * to wild addresses (which usually happen due to bugs in device
82568701780SSam Bobroff  * drivers or in PCI adapter firmware) can cause EEH error. #SERR,
82668701780SSam Bobroff  * #PERR or other misc PCI-related errors also can trigger EEH errors.
82768701780SSam Bobroff  *
82868701780SSam Bobroff  * Recovery process consists of unplugging the device driver (which
82968701780SSam Bobroff  * generated hotplug events to userspace), then issuing a PCI #RST to
83068701780SSam Bobroff  * the device, then reconfiguring the PCI config space for all bridges
83168701780SSam Bobroff  * & devices under this slot, and then finally restarting the device
83268701780SSam Bobroff  * drivers (which cause a second set of hotplug events to go out to
83368701780SSam Bobroff  * userspace).
834c0b64978SRussell Currey  */
eeh_handle_normal_event(struct eeh_pe * pe)83537fd8125SSam Bobroff void eeh_handle_normal_event(struct eeh_pe *pe)
836317f06deSGavin Shan {
837cd95f804SSam Bobroff 	struct pci_bus *bus;
83867086e32SWei Yang 	struct eeh_dev *edev, *tmp;
839665012c5SSam Bobroff 	struct eeh_pe *tmp_pe;
840317f06deSGavin Shan 	int rc = 0;
841317f06deSGavin Shan 	enum pci_ers_result result = PCI_ERS_RESULT_NONE;
8421c5c533bSSam Bobroff 	struct eeh_rmv_data rmv_data =
8431c5c533bSSam Bobroff 		{LIST_HEAD_INIT(rmv_data.removed_vf_list), 0};
844b104af5aSOliver O'Halloran 	int devices = 0;
845317f06deSGavin Shan 
846cd95f804SSam Bobroff 	bus = eeh_pe_bus_get(pe);
847cd95f804SSam Bobroff 	if (!bus) {
8481f52f176SRussell Currey 		pr_err("%s: Cannot find PCI bus for PHB#%x-PE#%x\n",
849317f06deSGavin Shan 			__func__, pe->phb->global_number, pe->addr);
85037fd8125SSam Bobroff 		return;
851317f06deSGavin Shan 	}
852317f06deSGavin Shan 
853b104af5aSOliver O'Halloran 	/*
854b104af5aSOliver O'Halloran 	 * When devices are hot-removed we might get an EEH due to
855b104af5aSOliver O'Halloran 	 * a driver attempting to touch the MMIO space of a removed
856b104af5aSOliver O'Halloran 	 * device. In this case we don't have a device to recover
857b104af5aSOliver O'Halloran 	 * so suppress the event if we can't find any present devices.
858b104af5aSOliver O'Halloran 	 *
859b104af5aSOliver O'Halloran 	 * The hotplug driver should take care of tearing down the
860b104af5aSOliver O'Halloran 	 * device itself.
861b104af5aSOliver O'Halloran 	 */
862b104af5aSOliver O'Halloran 	eeh_for_each_pe(pe, tmp_pe)
863b104af5aSOliver O'Halloran 		eeh_pe_for_each_dev(tmp_pe, edev, tmp)
864b104af5aSOliver O'Halloran 			if (eeh_slot_presence_check(edev->pdev))
865b104af5aSOliver O'Halloran 				devices++;
866b104af5aSOliver O'Halloran 
86725baf3d8SOliver O'Halloran 	if (!devices) {
86825baf3d8SOliver O'Halloran 		pr_debug("EEH: Frozen PHB#%x-PE#%x is empty!\n",
86925baf3d8SOliver O'Halloran 			pe->phb->global_number, pe->addr);
870b104af5aSOliver O'Halloran 		goto out; /* nothing to recover */
87125baf3d8SOliver O'Halloran 	}
87225baf3d8SOliver O'Halloran 
87325baf3d8SOliver O'Halloran 	/* Log the event */
87425baf3d8SOliver O'Halloran 	if (pe->type & EEH_PE_PHB) {
875de84ffc3SSam Bobroff 		pr_err("EEH: Recovering PHB#%x, location: %s\n",
87625baf3d8SOliver O'Halloran 			pe->phb->global_number, eeh_pe_loc_get(pe));
87725baf3d8SOliver O'Halloran 	} else {
87825baf3d8SOliver O'Halloran 		struct eeh_pe *phb_pe = eeh_phb_pe_get(pe->phb);
87925baf3d8SOliver O'Halloran 
880de84ffc3SSam Bobroff 		pr_err("EEH: Recovering PHB#%x-PE#%x\n",
88125baf3d8SOliver O'Halloran 		       pe->phb->global_number, pe->addr);
88225baf3d8SOliver O'Halloran 		pr_err("EEH: PE location: %s, PHB location: %s\n",
88325baf3d8SOliver O'Halloran 		       eeh_pe_loc_get(pe), eeh_pe_loc_get(phb_pe));
88425baf3d8SOliver O'Halloran 	}
88525baf3d8SOliver O'Halloran 
8861b7f3b6cSMichael Ellerman #ifdef CONFIG_STACKTRACE
88725baf3d8SOliver O'Halloran 	/*
88825baf3d8SOliver O'Halloran 	 * Print the saved stack trace now that we've verified there's
88925baf3d8SOliver O'Halloran 	 * something to recover.
89025baf3d8SOliver O'Halloran 	 */
89125baf3d8SOliver O'Halloran 	if (pe->trace_entries) {
89225baf3d8SOliver O'Halloran 		void **ptrs = (void **) pe->stack_trace;
89325baf3d8SOliver O'Halloran 		int i;
89425baf3d8SOliver O'Halloran 
89525baf3d8SOliver O'Halloran 		pr_err("EEH: Frozen PHB#%x-PE#%x detected\n",
89625baf3d8SOliver O'Halloran 		       pe->phb->global_number, pe->addr);
89725baf3d8SOliver O'Halloran 
89825baf3d8SOliver O'Halloran 		/* FIXME: Use the same format as dump_stack() */
89925baf3d8SOliver O'Halloran 		pr_err("EEH: Call Trace:\n");
90025baf3d8SOliver O'Halloran 		for (i = 0; i < pe->trace_entries; i++)
90125baf3d8SOliver O'Halloran 			pr_err("EEH: [%pK] %pS\n", ptrs[i], ptrs[i]);
90225baf3d8SOliver O'Halloran 
90325baf3d8SOliver O'Halloran 		pe->trace_entries = 0;
90425baf3d8SOliver O'Halloran 	}
9051b7f3b6cSMichael Ellerman #endif /* CONFIG_STACKTRACE */
906b104af5aSOliver O'Halloran 
907157616f3SOliver O'Halloran 	eeh_for_each_pe(pe, tmp_pe)
908157616f3SOliver O'Halloran 		eeh_pe_for_each_dev(tmp_pe, edev, tmp)
909157616f3SOliver O'Halloran 			edev->mode &= ~EEH_DEV_NO_HANDLER;
910157616f3SOliver O'Halloran 
9115a71978eSGavin Shan 	eeh_pe_update_time_stamp(pe);
912317f06deSGavin Shan 	pe->freeze_count++;
913c0b64978SRussell Currey 	if (pe->freeze_count > eeh_max_freezes) {
914796b9f5bSSam Bobroff 		pr_err("EEH: PHB#%x-PE#%x has failed %d times in the last hour and has been permanently disabled.\n",
915c0b64978SRussell Currey 		       pe->phb->global_number, pe->addr,
916c0b64978SRussell Currey 		       pe->freeze_count);
917317f06deSGavin Shan 
918157616f3SOliver O'Halloran 		goto recover_failed;
919157616f3SOliver O'Halloran 	}
920aa06e3d6SSam Bobroff 
921317f06deSGavin Shan 	/* Walk the various device drivers attached to this slot through
922317f06deSGavin Shan 	 * a reset sequence, giving each an opportunity to do what it needs
923317f06deSGavin Shan 	 * to accomplish the reset.  Each child gets a report of the
924317f06deSGavin Shan 	 * status ... if any child can't handle the reset, then the entire
925317f06deSGavin Shan 	 * slot is dlpar removed and added.
9268234fcedSGavin Shan 	 *
9278234fcedSGavin Shan 	 * When the PHB is fenced, we have to issue a reset to recover from
9288234fcedSGavin Shan 	 * the error. Override the result if necessary to have partially
9298234fcedSGavin Shan 	 * hotplug for this case.
930317f06deSGavin Shan 	 */
931b90484ecSSam Bobroff 	pr_warn("EEH: This PCI device has failed %d times in the last hour and will be permanently disabled after %d failures.\n",
932b90484ecSSam Bobroff 		pe->freeze_count, eeh_max_freezes);
93356ca4fdeSGavin Shan 	pr_info("EEH: Notify device drivers to shutdown\n");
93447cc8c1cSSam Bobroff 	eeh_set_channel_state(pe, pci_channel_io_frozen);
935010acfa1SSam Bobroff 	eeh_set_irq_state(pe, false);
936b90484ecSSam Bobroff 	eeh_pe_report("error_detected(IO frozen)", pe,
937b90484ecSSam Bobroff 		      eeh_report_error, &result);
938157616f3SOliver O'Halloran 	if (result == PCI_ERS_RESULT_DISCONNECT)
939157616f3SOliver O'Halloran 		goto recover_failed;
940157616f3SOliver O'Halloran 
941157616f3SOliver O'Halloran 	/*
942157616f3SOliver O'Halloran 	 * Error logged on a PHB are always fences which need a full
943157616f3SOliver O'Halloran 	 * PHB reset to clear so force that to happen.
944157616f3SOliver O'Halloran 	 */
945157616f3SOliver O'Halloran 	if ((pe->type & EEH_PE_PHB) && result != PCI_ERS_RESULT_NONE)
9468234fcedSGavin Shan 		result = PCI_ERS_RESULT_NEED_RESET;
947317f06deSGavin Shan 
948317f06deSGavin Shan 	/* Get the current PCI slot state. This can take a long time,
9492ac3990cSWei Yang 	 * sometimes over 300 seconds for certain systems.
950317f06deSGavin Shan 	 */
951fef7f905SSam Bobroff 	rc = eeh_wait_state(pe, MAX_WAIT_FOR_RECOVERY * 1000);
952317f06deSGavin Shan 	if (rc < 0 || rc == EEH_STATE_NOT_SUPPORT) {
9530dae2743SGavin Shan 		pr_warn("EEH: Permanent failure\n");
954157616f3SOliver O'Halloran 		goto recover_failed;
955317f06deSGavin Shan 	}
956317f06deSGavin Shan 
957317f06deSGavin Shan 	/* Since rtas may enable MMIO when posting the error log,
958317f06deSGavin Shan 	 * don't post the error log until after all dev drivers
959317f06deSGavin Shan 	 * have been informed.
960317f06deSGavin Shan 	 */
96156ca4fdeSGavin Shan 	pr_info("EEH: Collect temporary log\n");
962317f06deSGavin Shan 	eeh_slot_error_detail(pe, EEH_LOG_TEMP);
963317f06deSGavin Shan 
964317f06deSGavin Shan 	/* If all device drivers were EEH-unaware, then shut
965317f06deSGavin Shan 	 * down all of the device drivers, and hope they
966317f06deSGavin Shan 	 * go down willingly, without panicing the system.
967317f06deSGavin Shan 	 */
968317f06deSGavin Shan 	if (result == PCI_ERS_RESULT_NONE) {
96956ca4fdeSGavin Shan 		pr_info("EEH: Reset with hotplug activity\n");
9705fd13460SSam Bobroff 		rc = eeh_reset_device(pe, bus, NULL, false);
971317f06deSGavin Shan 		if (rc) {
972157616f3SOliver O'Halloran 			pr_warn("%s: Unable to reset, err=%d\n", __func__, rc);
973157616f3SOliver O'Halloran 			goto recover_failed;
974317f06deSGavin Shan 		}
975317f06deSGavin Shan 	}
976317f06deSGavin Shan 
977317f06deSGavin Shan 	/* If all devices reported they can proceed, then re-enable MMIO */
978317f06deSGavin Shan 	if (result == PCI_ERS_RESULT_CAN_RECOVER) {
97956ca4fdeSGavin Shan 		pr_info("EEH: Enable I/O for affected devices\n");
980317f06deSGavin Shan 		rc = eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
981157616f3SOliver O'Halloran 		if (rc < 0)
982157616f3SOliver O'Halloran 			goto recover_failed;
983317f06deSGavin Shan 
984157616f3SOliver O'Halloran 		if (rc) {
985317f06deSGavin Shan 			result = PCI_ERS_RESULT_NEED_RESET;
986317f06deSGavin Shan 		} else {
98756ca4fdeSGavin Shan 			pr_info("EEH: Notify device drivers to resume I/O\n");
98820b34497SSam Bobroff 			eeh_pe_report("mmio_enabled", pe,
98920b34497SSam Bobroff 				      eeh_report_mmio_enabled, &result);
990317f06deSGavin Shan 		}
991317f06deSGavin Shan 	}
992317f06deSGavin Shan 	if (result == PCI_ERS_RESULT_CAN_RECOVER) {
99356ca4fdeSGavin Shan 		pr_info("EEH: Enabled DMA for affected devices\n");
994317f06deSGavin Shan 		rc = eeh_pci_enable(pe, EEH_OPT_THAW_DMA);
995157616f3SOliver O'Halloran 		if (rc < 0)
996157616f3SOliver O'Halloran 			goto recover_failed;
997317f06deSGavin Shan 
998157616f3SOliver O'Halloran 		if (rc) {
999317f06deSGavin Shan 			result = PCI_ERS_RESULT_NEED_RESET;
100035845a78SGavin Shan 		} else {
100135845a78SGavin Shan 			/*
100235845a78SGavin Shan 			 * We didn't do PE reset for the case. The PE
100335845a78SGavin Shan 			 * is still in frozen state. Clear it before
100435845a78SGavin Shan 			 * resuming the PE.
100535845a78SGavin Shan 			 */
10069ed5ca66SSam Bobroff 			eeh_pe_state_clear(pe, EEH_PE_ISOLATED, true);
1007317f06deSGavin Shan 			result = PCI_ERS_RESULT_RECOVERED;
1008317f06deSGavin Shan 		}
100935845a78SGavin Shan 	}
1010317f06deSGavin Shan 
1011317f06deSGavin Shan 	/* If any device called out for a reset, then reset the slot */
1012317f06deSGavin Shan 	if (result == PCI_ERS_RESULT_NEED_RESET) {
101356ca4fdeSGavin Shan 		pr_info("EEH: Reset without hotplug activity\n");
10145fd13460SSam Bobroff 		rc = eeh_reset_device(pe, bus, &rmv_data, true);
1015317f06deSGavin Shan 		if (rc) {
1016157616f3SOliver O'Halloran 			pr_warn("%s: Cannot reset, err=%d\n", __func__, rc);
1017157616f3SOliver O'Halloran 			goto recover_failed;
1018157616f3SOliver O'Halloran 		}
1019157616f3SOliver O'Halloran 
1020317f06deSGavin Shan 		result = PCI_ERS_RESULT_NONE;
102147cc8c1cSSam Bobroff 		eeh_set_channel_state(pe, pci_channel_io_normal);
1022010acfa1SSam Bobroff 		eeh_set_irq_state(pe, true);
1023b90484ecSSam Bobroff 		eeh_pe_report("slot_reset", pe, eeh_report_reset,
1024b90484ecSSam Bobroff 			      &result);
1025b90484ecSSam Bobroff 	}
1026317f06deSGavin Shan 
1027b90484ecSSam Bobroff 	if ((result == PCI_ERS_RESULT_RECOVERED) ||
1028b90484ecSSam Bobroff 	    (result == PCI_ERS_RESULT_NONE)) {
102967086e32SWei Yang 		/*
1030b90484ecSSam Bobroff 		 * For those hot removed VFs, we should add back them after PF
1031b90484ecSSam Bobroff 		 * get recovered properly.
103267086e32SWei Yang 		 */
10331c5c533bSSam Bobroff 		list_for_each_entry_safe(edev, tmp, &rmv_data.removed_vf_list,
10341c5c533bSSam Bobroff 					 rmv_entry) {
1035bf773df9SSam Bobroff 			eeh_add_virt_device(edev);
103680e65b00SSam Bobroff 			list_del(&edev->rmv_entry);
103767086e32SWei Yang 		}
103867086e32SWei Yang 
1039317f06deSGavin Shan 		/* Tell all device drivers that they can resume operations */
104056ca4fdeSGavin Shan 		pr_info("EEH: Notify device driver to resume\n");
104147cc8c1cSSam Bobroff 		eeh_set_channel_state(pe, pci_channel_io_normal);
1042010acfa1SSam Bobroff 		eeh_set_irq_state(pe, true);
104320b34497SSam Bobroff 		eeh_pe_report("resume", pe, eeh_report_resume, NULL);
104420b34497SSam Bobroff 		eeh_for_each_pe(pe, tmp_pe) {
104520b34497SSam Bobroff 			eeh_pe_for_each_dev(tmp_pe, edev, tmp) {
1046665012c5SSam Bobroff 				edev->mode &= ~EEH_DEV_NO_HANDLER;
104720b34497SSam Bobroff 				edev->in_error = false;
104820b34497SSam Bobroff 			}
104920b34497SSam Bobroff 		}
1050665012c5SSam Bobroff 
1051796b9f5bSSam Bobroff 		pr_info("EEH: Recovery successful.\n");
105210b34eceSDaniel Axtens 		goto out;
105310b34eceSDaniel Axtens 	}
105410b34eceSDaniel Axtens 
1055157616f3SOliver O'Halloran recover_failed:
1056317f06deSGavin Shan 	/*
1057317f06deSGavin Shan 	 * About 90% of all real-life EEH failures in the field
1058317f06deSGavin Shan 	 * are due to poorly seated PCI cards. Only 10% or so are
1059317f06deSGavin Shan 	 * due to actual, failed cards.
1060317f06deSGavin Shan 	 */
10611f52f176SRussell Currey 	pr_err("EEH: Unable to recover from failure from PHB#%x-PE#%x.\n"
1062317f06deSGavin Shan 		"Please try reseating or replacing it\n",
1063317f06deSGavin Shan 		pe->phb->global_number, pe->addr);
1064317f06deSGavin Shan 
1065317f06deSGavin Shan 	eeh_slot_error_detail(pe, EEH_LOG_PERM);
1066317f06deSGavin Shan 
1067317f06deSGavin Shan 	/* Notify all devices that they're about to go down. */
1068010acfa1SSam Bobroff 	eeh_set_irq_state(pe, false);
106920b34497SSam Bobroff 	eeh_pe_report("error_detected(permanent failure)", pe,
107020b34497SSam Bobroff 		      eeh_report_failure, NULL);
1071*9efcdaacSGanesh Goudar 	eeh_set_channel_state(pe, pci_channel_io_perm_failure);
1072317f06deSGavin Shan 
1073d2b0f6f7SGavin Shan 	/* Mark the PE to be removed permanently */
1074432227e9SGavin Shan 	eeh_pe_state_mark(pe, EEH_PE_REMOVED);
1075d2b0f6f7SGavin Shan 
1076d2b0f6f7SGavin Shan 	/*
1077d2b0f6f7SGavin Shan 	 * Shut down the device drivers for good. We mark
1078d2b0f6f7SGavin Shan 	 * all removed devices correctly to avoid access
1079d2b0f6f7SGavin Shan 	 * the their PCI config any more.
1080d2b0f6f7SGavin Shan 	 */
108167086e32SWei Yang 	if (pe->type & EEH_PE_VF) {
108267086e32SWei Yang 		eeh_pe_dev_traverse(pe, eeh_rmv_device, NULL);
108367086e32SWei Yang 		eeh_pe_dev_mode_mark(pe, EEH_DEV_REMOVED);
108467086e32SWei Yang 	} else {
10859ed5ca66SSam Bobroff 		eeh_pe_state_clear(pe, EEH_PE_PRI_BUS, true);
1086d2b0f6f7SGavin Shan 		eeh_pe_dev_mode_mark(pe, EEH_DEV_REMOVED);
1087d2b0f6f7SGavin Shan 
10881c2042c8SRafael J. Wysocki 		pci_lock_rescan_remove();
1089cd95f804SSam Bobroff 		pci_hp_remove_devices(bus);
10901c2042c8SRafael J. Wysocki 		pci_unlock_rescan_remove();
1091daeba295SRussell Currey 		/* The passed PE should no longer be used */
109237fd8125SSam Bobroff 		return;
10931c2042c8SRafael J. Wysocki 	}
1094799abe28SOliver O'Halloran 
1095b104af5aSOliver O'Halloran out:
1096799abe28SOliver O'Halloran 	/*
1097799abe28SOliver O'Halloran 	 * Clean up any PEs without devices. While marked as EEH_PE_RECOVERYING
1098799abe28SOliver O'Halloran 	 * we don't want to modify the PE tree structure so we do it here.
1099799abe28SOliver O'Halloran 	 */
1100799abe28SOliver O'Halloran 	eeh_pe_cleanup(pe);
1101aeff27c1SOliver O'Halloran 
1102aeff27c1SOliver O'Halloran 	/* clear the slot attention LED for all recovered devices */
1103aeff27c1SOliver O'Halloran 	eeh_for_each_pe(pe, tmp_pe)
1104aeff27c1SOliver O'Halloran 		eeh_pe_for_each_dev(tmp_pe, edev, tmp)
1105aeff27c1SOliver O'Halloran 			eeh_clear_slot_attention(edev->pdev);
1106aeff27c1SOliver O'Halloran 
11079ed5ca66SSam Bobroff 	eeh_pe_state_clear(pe, EEH_PE_RECOVERING, true);
110867086e32SWei Yang }
11098a6b1bc7SGavin Shan 
1110c0b64978SRussell Currey /**
1111c0b64978SRussell Currey  * eeh_handle_special_event - Handle EEH events without a specific failing PE
1112c0b64978SRussell Currey  *
1113c0b64978SRussell Currey  * Called when an EEH event is detected but can't be narrowed down to a
1114c0b64978SRussell Currey  * specific PE.  Iterates through possible failures and handles them as
1115c0b64978SRussell Currey  * necessary.
1116c0b64978SRussell Currey  */
eeh_handle_special_event(void)111768701780SSam Bobroff void eeh_handle_special_event(void)
11188a6b1bc7SGavin Shan {
1119aa06e3d6SSam Bobroff 	struct eeh_pe *pe, *phb_pe, *tmp_pe;
1120aa06e3d6SSam Bobroff 	struct eeh_dev *edev, *tmp_edev;
11218a6b1bc7SGavin Shan 	struct pci_bus *bus;
11227e4e7867SGavin Shan 	struct pci_controller *hose;
11238a6b1bc7SGavin Shan 	unsigned long flags;
11247e4e7867SGavin Shan 	int rc;
11258a6b1bc7SGavin Shan 
11267e4e7867SGavin Shan 
11277e4e7867SGavin Shan 	do {
11288a6b1bc7SGavin Shan 		rc = eeh_ops->next_error(&pe);
11298a6b1bc7SGavin Shan 
11308a6b1bc7SGavin Shan 		switch (rc) {
11317e4e7867SGavin Shan 		case EEH_NEXT_ERR_DEAD_IOC:
11328a6b1bc7SGavin Shan 			/* Mark all PHBs in dead state */
11338a6b1bc7SGavin Shan 			eeh_serialize_lock(&flags);
11347e4e7867SGavin Shan 
11357e4e7867SGavin Shan 			/* Purge all events */
11365c7a35e3SGavin Shan 			eeh_remove_event(NULL, true);
11377e4e7867SGavin Shan 
11387e4e7867SGavin Shan 			list_for_each_entry(hose, &hose_list, list_node) {
11398a6b1bc7SGavin Shan 				phb_pe = eeh_phb_pe_get(hose);
11408a6b1bc7SGavin Shan 				if (!phb_pe) continue;
11418a6b1bc7SGavin Shan 
1142e762bb89SSam Bobroff 				eeh_pe_mark_isolated(phb_pe);
11438a6b1bc7SGavin Shan 			}
11447e4e7867SGavin Shan 
11458a6b1bc7SGavin Shan 			eeh_serialize_unlock(flags);
11468a6b1bc7SGavin Shan 
11478a6b1bc7SGavin Shan 			break;
11487e4e7867SGavin Shan 		case EEH_NEXT_ERR_FROZEN_PE:
11497e4e7867SGavin Shan 		case EEH_NEXT_ERR_FENCED_PHB:
11507e4e7867SGavin Shan 		case EEH_NEXT_ERR_DEAD_PHB:
11518a6b1bc7SGavin Shan 			/* Mark the PE in fenced state */
11528a6b1bc7SGavin Shan 			eeh_serialize_lock(&flags);
11537e4e7867SGavin Shan 
11547e4e7867SGavin Shan 			/* Purge all events of the PHB */
11555c7a35e3SGavin Shan 			eeh_remove_event(pe, true);
11567e4e7867SGavin Shan 
1157e762bb89SSam Bobroff 			if (rc != EEH_NEXT_ERR_DEAD_PHB)
1158e762bb89SSam Bobroff 				eeh_pe_state_mark(pe, EEH_PE_RECOVERING);
1159e762bb89SSam Bobroff 			eeh_pe_mark_isolated(pe);
11607e4e7867SGavin Shan 
11618a6b1bc7SGavin Shan 			eeh_serialize_unlock(flags);
11628a6b1bc7SGavin Shan 
11638a6b1bc7SGavin Shan 			break;
11647e4e7867SGavin Shan 		case EEH_NEXT_ERR_NONE:
11657e4e7867SGavin Shan 			return;
11668a6b1bc7SGavin Shan 		default:
11677e4e7867SGavin Shan 			pr_warn("%s: Invalid value %d from next_error()\n",
11688a6b1bc7SGavin Shan 				__func__, rc);
11698a6b1bc7SGavin Shan 			return;
11708a6b1bc7SGavin Shan 		}
11718a6b1bc7SGavin Shan 
11728a6b1bc7SGavin Shan 		/*
11738a6b1bc7SGavin Shan 		 * For fenced PHB and frozen PE, it's handled as normal
11748a6b1bc7SGavin Shan 		 * event. We have to remove the affected PHBs for dead
11758a6b1bc7SGavin Shan 		 * PHB and IOC
11768a6b1bc7SGavin Shan 		 */
11777e4e7867SGavin Shan 		if (rc == EEH_NEXT_ERR_FROZEN_PE ||
11787e4e7867SGavin Shan 		    rc == EEH_NEXT_ERR_FENCED_PHB) {
1179799abe28SOliver O'Halloran 			eeh_pe_state_mark(pe, EEH_PE_RECOVERING);
118037fd8125SSam Bobroff 			eeh_handle_normal_event(pe);
11817e4e7867SGavin Shan 		} else {
1182aa06e3d6SSam Bobroff 			eeh_for_each_pe(pe, tmp_pe)
1183aa06e3d6SSam Bobroff 				eeh_pe_for_each_dev(tmp_pe, edev, tmp_edev)
1184aa06e3d6SSam Bobroff 					edev->mode &= ~EEH_DEV_NO_HANDLER;
1185aa06e3d6SSam Bobroff 
11867e4e7867SGavin Shan 			/* Notify all devices to be down */
11879ed5ca66SSam Bobroff 			eeh_pe_state_clear(pe, EEH_PE_PRI_BUS, true);
118820b34497SSam Bobroff 			eeh_pe_report(
118920b34497SSam Bobroff 				"error_detected(permanent failure)", pe,
1190af2e3a00SRussell Currey 				eeh_report_failure, NULL);
1191*9efcdaacSGanesh Goudar 			eeh_set_channel_state(pe, pci_channel_io_perm_failure);
1192d4f194edSSam Bobroff 
1193d4f194edSSam Bobroff 			pci_lock_rescan_remove();
1194d4f194edSSam Bobroff 			list_for_each_entry(hose, &hose_list, list_node) {
1195d4f194edSSam Bobroff 				phb_pe = eeh_phb_pe_get(hose);
1196d4f194edSSam Bobroff 				if (!phb_pe ||
1197d4f194edSSam Bobroff 				    !(phb_pe->state & EEH_PE_ISOLATED) ||
1198d4f194edSSam Bobroff 				    (phb_pe->state & EEH_PE_RECOVERING))
1199d4f194edSSam Bobroff 					continue;
1200d4f194edSSam Bobroff 
12018a6b1bc7SGavin Shan 				bus = eeh_pe_bus_get(phb_pe);
120204fec21cSRussell Currey 				if (!bus) {
120304fec21cSRussell Currey 					pr_err("%s: Cannot find PCI bus for "
12041f52f176SRussell Currey 					       "PHB#%x-PE#%x\n",
120504fec21cSRussell Currey 					       __func__,
120604fec21cSRussell Currey 					       pe->phb->global_number,
120704fec21cSRussell Currey 					       pe->addr);
120804fec21cSRussell Currey 					break;
120904fec21cSRussell Currey 				}
1210bd251b89SGavin Shan 				pci_hp_remove_devices(bus);
12118a6b1bc7SGavin Shan 			}
12121c2042c8SRafael J. Wysocki 			pci_unlock_rescan_remove();
12138a6b1bc7SGavin Shan 		}
12147e4e7867SGavin Shan 
12157e4e7867SGavin Shan 		/*
12167e4e7867SGavin Shan 		 * If we have detected dead IOC, we needn't proceed
12177e4e7867SGavin Shan 		 * any more since all PHBs would have been removed
12187e4e7867SGavin Shan 		 */
12197e4e7867SGavin Shan 		if (rc == EEH_NEXT_ERR_DEAD_IOC)
12207e4e7867SGavin Shan 			break;
12217e4e7867SGavin Shan 	} while (rc != EEH_NEXT_ERR_NONE);
12228a6b1bc7SGavin Shan }
1223