xref: /openbmc/linux/arch/powerpc/kernel/eeh_driver.c (revision 1b7f3b6c)
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>
30b104af5aSOliver O'Halloran #include <linux/pci_hotplug.h>
31317f06deSGavin Shan #include <asm/eeh.h>
32317f06deSGavin Shan #include <asm/eeh_event.h>
33317f06deSGavin Shan #include <asm/ppc-pci.h>
34317f06deSGavin Shan #include <asm/pci-bridge.h>
35317f06deSGavin Shan #include <asm/prom.h>
36317f06deSGavin Shan #include <asm/rtas.h>
37317f06deSGavin Shan 
3867086e32SWei Yang struct eeh_rmv_data {
391c5c533bSSam Bobroff 	struct list_head removed_vf_list;
401c5c533bSSam Bobroff 	int removed_dev_count;
4167086e32SWei Yang };
4267086e32SWei Yang 
4330424e38SSam Bobroff static int eeh_result_priority(enum pci_ers_result result)
4430424e38SSam Bobroff {
4530424e38SSam Bobroff 	switch (result) {
4630424e38SSam Bobroff 	case PCI_ERS_RESULT_NONE:
4730424e38SSam Bobroff 		return 1;
4830424e38SSam Bobroff 	case PCI_ERS_RESULT_NO_AER_DRIVER:
4930424e38SSam Bobroff 		return 2;
5030424e38SSam Bobroff 	case PCI_ERS_RESULT_RECOVERED:
5130424e38SSam Bobroff 		return 3;
5230424e38SSam Bobroff 	case PCI_ERS_RESULT_CAN_RECOVER:
5330424e38SSam Bobroff 		return 4;
5430424e38SSam Bobroff 	case PCI_ERS_RESULT_DISCONNECT:
5530424e38SSam Bobroff 		return 5;
5630424e38SSam Bobroff 	case PCI_ERS_RESULT_NEED_RESET:
5730424e38SSam Bobroff 		return 6;
5830424e38SSam Bobroff 	default:
5930424e38SSam Bobroff 		WARN_ONCE(1, "Unknown pci_ers_result value: %d\n", (int)result);
6030424e38SSam Bobroff 		return 0;
6130424e38SSam Bobroff 	}
6230424e38SSam Bobroff };
6330424e38SSam Bobroff 
64c36c5ffdSBreno Leitao static const char *pci_ers_result_name(enum pci_ers_result result)
6520b34497SSam Bobroff {
6620b34497SSam Bobroff 	switch (result) {
6720b34497SSam Bobroff 	case PCI_ERS_RESULT_NONE:
6820b34497SSam Bobroff 		return "none";
6920b34497SSam Bobroff 	case PCI_ERS_RESULT_CAN_RECOVER:
7020b34497SSam Bobroff 		return "can recover";
7120b34497SSam Bobroff 	case PCI_ERS_RESULT_NEED_RESET:
7220b34497SSam Bobroff 		return "need reset";
7320b34497SSam Bobroff 	case PCI_ERS_RESULT_DISCONNECT:
7420b34497SSam Bobroff 		return "disconnect";
7520b34497SSam Bobroff 	case PCI_ERS_RESULT_RECOVERED:
7620b34497SSam Bobroff 		return "recovered";
7720b34497SSam Bobroff 	case PCI_ERS_RESULT_NO_AER_DRIVER:
7820b34497SSam Bobroff 		return "no AER driver";
7920b34497SSam Bobroff 	default:
8020b34497SSam Bobroff 		WARN_ONCE(1, "Unknown result type: %d\n", (int)result);
8120b34497SSam Bobroff 		return "unknown";
8220b34497SSam Bobroff 	}
8320b34497SSam Bobroff };
8420b34497SSam Bobroff 
8530424e38SSam Bobroff static enum pci_ers_result pci_ers_merge_result(enum pci_ers_result old,
8630424e38SSam Bobroff 						enum pci_ers_result new)
8730424e38SSam Bobroff {
8830424e38SSam Bobroff 	if (eeh_result_priority(new) > eeh_result_priority(old))
8930424e38SSam Bobroff 		return new;
9030424e38SSam Bobroff 	return old;
9130424e38SSam Bobroff }
9230424e38SSam Bobroff 
93e2b810d5SSam Bobroff static bool eeh_dev_removed(struct eeh_dev *edev)
94e2b810d5SSam Bobroff {
95e2b810d5SSam Bobroff 	return !edev || (edev->mode & EEH_DEV_REMOVED);
96e2b810d5SSam Bobroff }
97e2b810d5SSam Bobroff 
98e2b810d5SSam Bobroff static bool eeh_edev_actionable(struct eeh_dev *edev)
99e2b810d5SSam Bobroff {
10038ddc011SOliver O'Halloran 	if (!edev->pdev)
10138ddc011SOliver O'Halloran 		return false;
10238ddc011SOliver O'Halloran 	if (edev->pdev->error_state == pci_channel_io_perm_failure)
10338ddc011SOliver O'Halloran 		return false;
10438ddc011SOliver O'Halloran 	if (eeh_dev_removed(edev))
10538ddc011SOliver O'Halloran 		return false;
10638ddc011SOliver O'Halloran 	if (eeh_pe_passed(edev->pe))
10738ddc011SOliver O'Halloran 		return false;
10838ddc011SOliver O'Halloran 
10938ddc011SOliver O'Halloran 	return true;
110e2b810d5SSam Bobroff }
111e2b810d5SSam Bobroff 
112317f06deSGavin Shan /**
113317f06deSGavin Shan  * eeh_pcid_get - Get the PCI device driver
114317f06deSGavin Shan  * @pdev: PCI device
115317f06deSGavin Shan  *
116317f06deSGavin Shan  * The function is used to retrieve the PCI device driver for
117317f06deSGavin Shan  * the indicated PCI device. Besides, we will increase the reference
118317f06deSGavin Shan  * of the PCI device driver to prevent that being unloaded on
119317f06deSGavin Shan  * the fly. Otherwise, kernel crash would be seen.
120317f06deSGavin Shan  */
121317f06deSGavin Shan static inline struct pci_driver *eeh_pcid_get(struct pci_dev *pdev)
122317f06deSGavin Shan {
123317f06deSGavin Shan 	if (!pdev || !pdev->driver)
124317f06deSGavin Shan 		return NULL;
125317f06deSGavin Shan 
126317f06deSGavin Shan 	if (!try_module_get(pdev->driver->driver.owner))
127317f06deSGavin Shan 		return NULL;
128317f06deSGavin Shan 
129317f06deSGavin Shan 	return pdev->driver;
130317f06deSGavin Shan }
131317f06deSGavin Shan 
132317f06deSGavin Shan /**
133317f06deSGavin Shan  * eeh_pcid_put - Dereference on the PCI device driver
134317f06deSGavin Shan  * @pdev: PCI device
135317f06deSGavin Shan  *
136317f06deSGavin Shan  * The function is called to do dereference on the PCI device
137317f06deSGavin Shan  * driver of the indicated PCI device.
138317f06deSGavin Shan  */
139317f06deSGavin Shan static inline void eeh_pcid_put(struct pci_dev *pdev)
140317f06deSGavin Shan {
141317f06deSGavin Shan 	if (!pdev || !pdev->driver)
142317f06deSGavin Shan 		return;
143317f06deSGavin Shan 
144317f06deSGavin Shan 	module_put(pdev->driver->driver.owner);
145317f06deSGavin Shan }
146317f06deSGavin Shan 
147317f06deSGavin Shan /**
148317f06deSGavin Shan  * eeh_disable_irq - Disable interrupt for the recovering device
149317f06deSGavin Shan  * @dev: PCI device
150317f06deSGavin Shan  *
151317f06deSGavin Shan  * This routine must be called when reporting temporary or permanent
152317f06deSGavin Shan  * error to the particular PCI device to disable interrupt of that
153317f06deSGavin Shan  * device. If the device has enabled MSI or MSI-X interrupt, we needn't
154317f06deSGavin Shan  * do real work because EEH should freeze DMA transfers for those PCI
155317f06deSGavin Shan  * devices encountering EEH errors, which includes MSI or MSI-X.
156317f06deSGavin Shan  */
157010acfa1SSam Bobroff static void eeh_disable_irq(struct eeh_dev *edev)
158317f06deSGavin Shan {
159317f06deSGavin Shan 	/* Don't disable MSI and MSI-X interrupts. They are
160317f06deSGavin Shan 	 * effectively disabled by the DMA Stopped state
161317f06deSGavin Shan 	 * when an EEH error occurs.
162317f06deSGavin Shan 	 */
163010acfa1SSam Bobroff 	if (edev->pdev->msi_enabled || edev->pdev->msix_enabled)
164317f06deSGavin Shan 		return;
165317f06deSGavin Shan 
166010acfa1SSam Bobroff 	if (!irq_has_action(edev->pdev->irq))
167317f06deSGavin Shan 		return;
168317f06deSGavin Shan 
169317f06deSGavin Shan 	edev->mode |= EEH_DEV_IRQ_DISABLED;
170010acfa1SSam Bobroff 	disable_irq_nosync(edev->pdev->irq);
171317f06deSGavin Shan }
172317f06deSGavin Shan 
173317f06deSGavin Shan /**
174317f06deSGavin Shan  * eeh_enable_irq - Enable interrupt for the recovering device
175317f06deSGavin Shan  * @dev: PCI device
176317f06deSGavin Shan  *
177317f06deSGavin Shan  * This routine must be called to enable interrupt while failed
178317f06deSGavin Shan  * device could be resumed.
179317f06deSGavin Shan  */
180010acfa1SSam Bobroff static void eeh_enable_irq(struct eeh_dev *edev)
181317f06deSGavin Shan {
182317f06deSGavin Shan 	if ((edev->mode) & EEH_DEV_IRQ_DISABLED) {
183317f06deSGavin Shan 		edev->mode &= ~EEH_DEV_IRQ_DISABLED;
184b8a9a11bSThomas Gleixner 		/*
185b8a9a11bSThomas Gleixner 		 * FIXME !!!!!
186b8a9a11bSThomas Gleixner 		 *
187b8a9a11bSThomas Gleixner 		 * This is just ass backwards. This maze has
188b8a9a11bSThomas Gleixner 		 * unbalanced irq_enable/disable calls. So instead of
189b8a9a11bSThomas Gleixner 		 * finding the root cause it works around the warning
190b8a9a11bSThomas Gleixner 		 * in the irq_enable code by conditionally calling
191b8a9a11bSThomas Gleixner 		 * into it.
192b8a9a11bSThomas Gleixner 		 *
193b8a9a11bSThomas Gleixner 		 * That's just wrong.The warning in the core code is
194027dfac6SMichael Ellerman 		 * there to tell people to fix their asymmetries in
195b8a9a11bSThomas Gleixner 		 * their own code, not by abusing the core information
196b8a9a11bSThomas Gleixner 		 * to avoid it.
197b8a9a11bSThomas Gleixner 		 *
198b8a9a11bSThomas Gleixner 		 * I so wish that the assymetry would be the other way
199b8a9a11bSThomas Gleixner 		 * round and a few more irq_disable calls render that
200b8a9a11bSThomas Gleixner 		 * shit unusable forever.
201b8a9a11bSThomas Gleixner 		 *
202b8a9a11bSThomas Gleixner 		 *	tglx
203b8a9a11bSThomas Gleixner 		 */
204010acfa1SSam Bobroff 		if (irqd_irq_disabled(irq_get_irq_data(edev->pdev->irq)))
205010acfa1SSam Bobroff 			enable_irq(edev->pdev->irq);
206317f06deSGavin Shan 	}
20757310c3cSThomas Gleixner }
208317f06deSGavin Shan 
209cef50c67SSam Bobroff static void eeh_dev_save_state(struct eeh_dev *edev, void *userdata)
2105cfb20b9SGavin Shan {
2115cfb20b9SGavin Shan 	struct pci_dev *pdev;
2125cfb20b9SGavin Shan 
2135cfb20b9SGavin Shan 	if (!edev)
214cef50c67SSam Bobroff 		return;
2155cfb20b9SGavin Shan 
2165a0cdbfdSGavin Shan 	/*
2175a0cdbfdSGavin Shan 	 * We cannot access the config space on some adapters.
2185a0cdbfdSGavin Shan 	 * Otherwise, it will cause fenced PHB. We don't save
2195a0cdbfdSGavin Shan 	 * the content in their config space and will restore
2205a0cdbfdSGavin Shan 	 * from the initial config space saved when the EEH
2215a0cdbfdSGavin Shan 	 * device is created.
2225a0cdbfdSGavin Shan 	 */
2235a0cdbfdSGavin Shan 	if (edev->pe && (edev->pe->state & EEH_PE_CFG_RESTRICTED))
224cef50c67SSam Bobroff 		return;
2255a0cdbfdSGavin Shan 
2265cfb20b9SGavin Shan 	pdev = eeh_dev_to_pci_dev(edev);
2275cfb20b9SGavin Shan 	if (!pdev)
228cef50c67SSam Bobroff 		return;
2295cfb20b9SGavin Shan 
2305cfb20b9SGavin Shan 	pci_save_state(pdev);
2315cfb20b9SGavin Shan }
2325cfb20b9SGavin Shan 
23347cc8c1cSSam Bobroff static void eeh_set_channel_state(struct eeh_pe *root, enum pci_channel_state s)
23447cc8c1cSSam Bobroff {
23547cc8c1cSSam Bobroff 	struct eeh_pe *pe;
23647cc8c1cSSam Bobroff 	struct eeh_dev *edev, *tmp;
23747cc8c1cSSam Bobroff 
23847cc8c1cSSam Bobroff 	eeh_for_each_pe(root, pe)
23947cc8c1cSSam Bobroff 		eeh_pe_for_each_dev(pe, edev, tmp)
24047cc8c1cSSam Bobroff 			if (eeh_edev_actionable(edev))
24147cc8c1cSSam Bobroff 				edev->pdev->error_state = s;
24247cc8c1cSSam Bobroff }
24347cc8c1cSSam Bobroff 
244010acfa1SSam Bobroff static void eeh_set_irq_state(struct eeh_pe *root, bool enable)
245010acfa1SSam Bobroff {
246010acfa1SSam Bobroff 	struct eeh_pe *pe;
247010acfa1SSam Bobroff 	struct eeh_dev *edev, *tmp;
248010acfa1SSam Bobroff 
249010acfa1SSam Bobroff 	eeh_for_each_pe(root, pe) {
250010acfa1SSam Bobroff 		eeh_pe_for_each_dev(pe, edev, tmp) {
251010acfa1SSam Bobroff 			if (!eeh_edev_actionable(edev))
252010acfa1SSam Bobroff 				continue;
253010acfa1SSam Bobroff 
254010acfa1SSam Bobroff 			if (!eeh_pcid_get(edev->pdev))
255010acfa1SSam Bobroff 				continue;
256010acfa1SSam Bobroff 
257010acfa1SSam Bobroff 			if (enable)
258010acfa1SSam Bobroff 				eeh_enable_irq(edev);
259010acfa1SSam Bobroff 			else
260010acfa1SSam Bobroff 				eeh_disable_irq(edev);
261010acfa1SSam Bobroff 
262010acfa1SSam Bobroff 			eeh_pcid_put(edev->pdev);
263010acfa1SSam Bobroff 		}
264010acfa1SSam Bobroff 	}
265010acfa1SSam Bobroff }
266010acfa1SSam Bobroff 
26720b34497SSam Bobroff typedef enum pci_ers_result (*eeh_report_fn)(struct eeh_dev *,
2682e255051SSam Bobroff 					     struct pci_dev *,
26920b34497SSam Bobroff 					     struct pci_driver *);
27020b34497SSam Bobroff static void eeh_pe_report_edev(struct eeh_dev *edev, eeh_report_fn fn,
27120b34497SSam Bobroff 			       enum pci_ers_result *result)
27220b34497SSam Bobroff {
2732e255051SSam Bobroff 	struct pci_dev *pdev;
27420b34497SSam Bobroff 	struct pci_driver *driver;
27520b34497SSam Bobroff 	enum pci_ers_result new_result;
27620b34497SSam Bobroff 
2772e255051SSam Bobroff 	pci_lock_rescan_remove();
2782e255051SSam Bobroff 	pdev = edev->pdev;
2792e255051SSam Bobroff 	if (pdev)
2802e255051SSam Bobroff 		get_device(&pdev->dev);
2812e255051SSam Bobroff 	pci_unlock_rescan_remove();
2822e255051SSam Bobroff 	if (!pdev) {
283bcbe3730SSam Bobroff 		eeh_edev_info(edev, "no device");
284bcbe3730SSam Bobroff 		return;
285bcbe3730SSam Bobroff 	}
2862e255051SSam Bobroff 	device_lock(&pdev->dev);
28720b34497SSam Bobroff 	if (eeh_edev_actionable(edev)) {
2882e255051SSam Bobroff 		driver = eeh_pcid_get(pdev);
28920b34497SSam Bobroff 
29020b34497SSam Bobroff 		if (!driver)
29120b34497SSam Bobroff 			eeh_edev_info(edev, "no driver");
29220b34497SSam Bobroff 		else if (!driver->err_handler)
29320b34497SSam Bobroff 			eeh_edev_info(edev, "driver not EEH aware");
29420b34497SSam Bobroff 		else if (edev->mode & EEH_DEV_NO_HANDLER)
29520b34497SSam Bobroff 			eeh_edev_info(edev, "driver bound too late");
29620b34497SSam Bobroff 		else {
2972e255051SSam Bobroff 			new_result = fn(edev, pdev, driver);
29820b34497SSam Bobroff 			eeh_edev_info(edev, "%s driver reports: '%s'",
29920b34497SSam Bobroff 				      driver->name,
30020b34497SSam Bobroff 				      pci_ers_result_name(new_result));
30120b34497SSam Bobroff 			if (result)
30220b34497SSam Bobroff 				*result = pci_ers_merge_result(*result,
30320b34497SSam Bobroff 							       new_result);
30420b34497SSam Bobroff 		}
30520b34497SSam Bobroff 		if (driver)
3062e255051SSam Bobroff 			eeh_pcid_put(pdev);
30720b34497SSam Bobroff 	} else {
3082e255051SSam Bobroff 		eeh_edev_info(edev, "not actionable (%d,%d,%d)", !!pdev,
30920b34497SSam Bobroff 			      !eeh_dev_removed(edev), !eeh_pe_passed(edev->pe));
31020b34497SSam Bobroff 	}
3112e255051SSam Bobroff 	device_unlock(&pdev->dev);
3122e255051SSam Bobroff 	if (edev->pdev != pdev)
3132e255051SSam Bobroff 		eeh_edev_warn(edev, "Device changed during processing!\n");
3142e255051SSam Bobroff 	put_device(&pdev->dev);
31520b34497SSam Bobroff }
31620b34497SSam Bobroff 
31720b34497SSam Bobroff static void eeh_pe_report(const char *name, struct eeh_pe *root,
31820b34497SSam Bobroff 			  eeh_report_fn fn, enum pci_ers_result *result)
31920b34497SSam Bobroff {
32020b34497SSam Bobroff 	struct eeh_pe *pe;
32120b34497SSam Bobroff 	struct eeh_dev *edev, *tmp;
32220b34497SSam Bobroff 
32320b34497SSam Bobroff 	pr_info("EEH: Beginning: '%s'\n", name);
32420b34497SSam Bobroff 	eeh_for_each_pe(root, pe) eeh_pe_for_each_dev(pe, edev, tmp)
32520b34497SSam Bobroff 		eeh_pe_report_edev(edev, fn, result);
32620b34497SSam Bobroff 	if (result)
32720b34497SSam Bobroff 		pr_info("EEH: Finished:'%s' with aggregate recovery state:'%s'\n",
32820b34497SSam Bobroff 			name, pci_ers_result_name(*result));
32920b34497SSam Bobroff 	else
33020b34497SSam Bobroff 		pr_info("EEH: Finished:'%s'", name);
33120b34497SSam Bobroff }
33220b34497SSam Bobroff 
333317f06deSGavin Shan /**
334317f06deSGavin Shan  * eeh_report_error - Report pci error to each device driver
33520b34497SSam Bobroff  * @edev: eeh device
33620b34497SSam Bobroff  * @driver: device's PCI driver
337317f06deSGavin Shan  *
33820b34497SSam Bobroff  * Report an EEH error to each device driver.
339317f06deSGavin Shan  */
34020b34497SSam Bobroff static enum pci_ers_result eeh_report_error(struct eeh_dev *edev,
3412e255051SSam Bobroff 					    struct pci_dev *pdev,
34220b34497SSam Bobroff 					    struct pci_driver *driver)
343317f06deSGavin Shan {
34420b34497SSam Bobroff 	enum pci_ers_result rc;
345317f06deSGavin Shan 
34620b34497SSam Bobroff 	if (!driver->err_handler->error_detected)
34720b34497SSam Bobroff 		return PCI_ERS_RESULT_NONE;
348f0295e04SMichael Neuling 
34920b34497SSam Bobroff 	eeh_edev_info(edev, "Invoking %s->error_detected(IO frozen)",
35020b34497SSam Bobroff 		      driver->name);
3512e255051SSam Bobroff 	rc = driver->err_handler->error_detected(pdev, pci_channel_io_frozen);
352317f06deSGavin Shan 
35367086e32SWei Yang 	edev->in_error = true;
3542e255051SSam Bobroff 	pci_uevent_ers(pdev, PCI_ERS_RESULT_NONE);
35520b34497SSam Bobroff 	return rc;
356317f06deSGavin Shan }
357317f06deSGavin Shan 
358317f06deSGavin Shan /**
359317f06deSGavin Shan  * eeh_report_mmio_enabled - Tell drivers that MMIO has been enabled
36020b34497SSam Bobroff  * @edev: eeh device
36120b34497SSam Bobroff  * @driver: device's PCI driver
362317f06deSGavin Shan  *
363317f06deSGavin Shan  * Tells each device driver that IO ports, MMIO and config space I/O
36420b34497SSam Bobroff  * are now enabled.
365317f06deSGavin Shan  */
36620b34497SSam Bobroff static enum pci_ers_result eeh_report_mmio_enabled(struct eeh_dev *edev,
3672e255051SSam Bobroff 						   struct pci_dev *pdev,
36820b34497SSam Bobroff 						   struct pci_driver *driver)
369317f06deSGavin Shan {
37020b34497SSam Bobroff 	if (!driver->err_handler->mmio_enabled)
37120b34497SSam Bobroff 		return PCI_ERS_RESULT_NONE;
37220b34497SSam Bobroff 	eeh_edev_info(edev, "Invoking %s->mmio_enabled()", driver->name);
3732e255051SSam Bobroff 	return driver->err_handler->mmio_enabled(pdev);
374317f06deSGavin Shan }
375317f06deSGavin Shan 
376317f06deSGavin Shan /**
377317f06deSGavin Shan  * eeh_report_reset - Tell device that slot has been reset
37820b34497SSam Bobroff  * @edev: eeh device
37920b34497SSam Bobroff  * @driver: device's PCI driver
380317f06deSGavin Shan  *
381317f06deSGavin Shan  * This routine must be called while EEH tries to reset particular
382317f06deSGavin Shan  * PCI device so that the associated PCI device driver could take
383317f06deSGavin Shan  * some actions, usually to save data the driver needs so that the
384317f06deSGavin Shan  * driver can work again while the device is recovered.
385317f06deSGavin Shan  */
38620b34497SSam Bobroff static enum pci_ers_result eeh_report_reset(struct eeh_dev *edev,
3872e255051SSam Bobroff 					    struct pci_dev *pdev,
38820b34497SSam Bobroff 					    struct pci_driver *driver)
389317f06deSGavin Shan {
39020b34497SSam Bobroff 	if (!driver->err_handler->slot_reset || !edev->in_error)
39120b34497SSam Bobroff 		return PCI_ERS_RESULT_NONE;
39220b34497SSam Bobroff 	eeh_edev_info(edev, "Invoking %s->slot_reset()", driver->name);
3932e255051SSam Bobroff 	return driver->err_handler->slot_reset(pdev);
394317f06deSGavin Shan }
395317f06deSGavin Shan 
396cef50c67SSam Bobroff static void eeh_dev_restore_state(struct eeh_dev *edev, void *userdata)
3975cfb20b9SGavin Shan {
3985cfb20b9SGavin Shan 	struct pci_dev *pdev;
3995cfb20b9SGavin Shan 
4005cfb20b9SGavin Shan 	if (!edev)
401cef50c67SSam Bobroff 		return;
4025cfb20b9SGavin Shan 
4035a0cdbfdSGavin Shan 	/*
4045a0cdbfdSGavin Shan 	 * The content in the config space isn't saved because
4055a0cdbfdSGavin Shan 	 * the blocked config space on some adapters. We have
4065a0cdbfdSGavin Shan 	 * to restore the initial saved config space when the
4075a0cdbfdSGavin Shan 	 * EEH device is created.
4085a0cdbfdSGavin Shan 	 */
4095a0cdbfdSGavin Shan 	if (edev->pe && (edev->pe->state & EEH_PE_CFG_RESTRICTED)) {
41080e65b00SSam Bobroff 		if (list_is_last(&edev->entry, &edev->pe->edevs))
4115a0cdbfdSGavin Shan 			eeh_pe_restore_bars(edev->pe);
4125a0cdbfdSGavin Shan 
413cef50c67SSam Bobroff 		return;
4145a0cdbfdSGavin Shan 	}
4155a0cdbfdSGavin Shan 
4165cfb20b9SGavin Shan 	pdev = eeh_dev_to_pci_dev(edev);
4175cfb20b9SGavin Shan 	if (!pdev)
418cef50c67SSam Bobroff 		return;
4195cfb20b9SGavin Shan 
4205cfb20b9SGavin Shan 	pci_restore_state(pdev);
4215cfb20b9SGavin Shan }
4225cfb20b9SGavin Shan 
423317f06deSGavin Shan /**
424317f06deSGavin Shan  * eeh_report_resume - Tell device to resume normal operations
42520b34497SSam Bobroff  * @edev: eeh device
42620b34497SSam Bobroff  * @driver: device's PCI driver
427317f06deSGavin Shan  *
428317f06deSGavin Shan  * This routine must be called to notify the device driver that it
429317f06deSGavin Shan  * could resume so that the device driver can do some initialization
430317f06deSGavin Shan  * to make the recovered device work again.
431317f06deSGavin Shan  */
43220b34497SSam Bobroff static enum pci_ers_result eeh_report_resume(struct eeh_dev *edev,
4332e255051SSam Bobroff 					     struct pci_dev *pdev,
43420b34497SSam Bobroff 					     struct pci_driver *driver)
435317f06deSGavin Shan {
43620b34497SSam Bobroff 	if (!driver->err_handler->resume || !edev->in_error)
43720b34497SSam Bobroff 		return PCI_ERS_RESULT_NONE;
438317f06deSGavin Shan 
43920b34497SSam Bobroff 	eeh_edev_info(edev, "Invoking %s->resume()", driver->name);
4402e255051SSam Bobroff 	driver->err_handler->resume(pdev);
441f0295e04SMichael Neuling 
44220b34497SSam Bobroff 	pci_uevent_ers(edev->pdev, PCI_ERS_RESULT_RECOVERED);
443856e1eb9SBryant G. Ly #ifdef CONFIG_PCI_IOV
444521ca5a9SJuan J. Alvarez 	if (eeh_ops->notify_resume && eeh_dev_to_pdn(edev))
445856e1eb9SBryant G. Ly 		eeh_ops->notify_resume(eeh_dev_to_pdn(edev));
446856e1eb9SBryant G. Ly #endif
44720b34497SSam Bobroff 	return PCI_ERS_RESULT_NONE;
448317f06deSGavin Shan }
449317f06deSGavin Shan 
450317f06deSGavin Shan /**
451317f06deSGavin Shan  * eeh_report_failure - Tell device driver that device is dead.
45220b34497SSam Bobroff  * @edev: eeh device
45320b34497SSam Bobroff  * @driver: device's PCI driver
454317f06deSGavin Shan  *
455317f06deSGavin Shan  * This informs the device driver that the device is permanently
456317f06deSGavin Shan  * dead, and that no further recovery attempts will be made on it.
457317f06deSGavin Shan  */
45820b34497SSam Bobroff static enum pci_ers_result eeh_report_failure(struct eeh_dev *edev,
4592e255051SSam Bobroff 					      struct pci_dev *pdev,
46020b34497SSam Bobroff 					      struct pci_driver *driver)
461317f06deSGavin Shan {
46220b34497SSam Bobroff 	enum pci_ers_result rc;
463317f06deSGavin Shan 
46420b34497SSam Bobroff 	if (!driver->err_handler->error_detected)
46520b34497SSam Bobroff 		return PCI_ERS_RESULT_NONE;
466f0295e04SMichael Neuling 
46720b34497SSam Bobroff 	eeh_edev_info(edev, "Invoking %s->error_detected(permanent failure)",
46820b34497SSam Bobroff 		      driver->name);
4692e255051SSam Bobroff 	rc = driver->err_handler->error_detected(pdev,
47020b34497SSam Bobroff 						 pci_channel_io_perm_failure);
471317f06deSGavin Shan 
4722e255051SSam Bobroff 	pci_uevent_ers(pdev, PCI_ERS_RESULT_DISCONNECT);
47320b34497SSam Bobroff 	return rc;
474317f06deSGavin Shan }
475317f06deSGavin Shan 
476bf773df9SSam Bobroff static void *eeh_add_virt_device(struct eeh_dev *edev)
47767086e32SWei Yang {
47867086e32SWei Yang 	struct pci_driver *driver;
47967086e32SWei Yang 	struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
48067086e32SWei Yang 
48167086e32SWei Yang 	if (!(edev->physfn)) {
4821ff8f36fSSam Bobroff 		eeh_edev_warn(edev, "Not for VF\n");
48367086e32SWei Yang 		return NULL;
48467086e32SWei Yang 	}
48567086e32SWei Yang 
48667086e32SWei Yang 	driver = eeh_pcid_get(dev);
48767086e32SWei Yang 	if (driver) {
48846d4be41SSam Bobroff 		if (driver->err_handler) {
48967086e32SWei Yang 			eeh_pcid_put(dev);
49067086e32SWei Yang 			return NULL;
49167086e32SWei Yang 		}
49246d4be41SSam Bobroff 		eeh_pcid_put(dev);
49346d4be41SSam Bobroff 	}
49467086e32SWei Yang 
495988fc3baSBryant G. Ly #ifdef CONFIG_PCI_IOV
4961ff8f36fSSam Bobroff 	pci_iov_add_virtfn(edev->physfn, eeh_dev_to_pdn(edev)->vf_index);
49767086e32SWei Yang #endif
49867086e32SWei Yang 	return NULL;
49967086e32SWei Yang }
50067086e32SWei Yang 
501cef50c67SSam Bobroff static void eeh_rmv_device(struct eeh_dev *edev, void *userdata)
502f5c57710SGavin Shan {
503f5c57710SGavin Shan 	struct pci_driver *driver;
504f5c57710SGavin Shan 	struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
50567086e32SWei Yang 	struct eeh_rmv_data *rmv_data = (struct eeh_rmv_data *)userdata;
506f5c57710SGavin Shan 
507f5c57710SGavin Shan 	/*
508f5c57710SGavin Shan 	 * Actually, we should remove the PCI bridges as well.
509f5c57710SGavin Shan 	 * However, that's lots of complexity to do that,
510f5c57710SGavin Shan 	 * particularly some of devices under the bridge might
511f5c57710SGavin Shan 	 * support EEH. So we just care about PCI devices for
512f5c57710SGavin Shan 	 * simplicity here.
513f5c57710SGavin Shan 	 */
5141ef52073SSam Bobroff 	if (!eeh_edev_actionable(edev) ||
5151ef52073SSam Bobroff 	    (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE))
516cef50c67SSam Bobroff 		return;
517d2b0f6f7SGavin Shan 
5181c5c533bSSam Bobroff 	if (rmv_data) {
519f5c57710SGavin Shan 		driver = eeh_pcid_get(dev);
5208cc6b6cdSThadeu Lima de Souza Cascardo 		if (driver) {
52146d4be41SSam Bobroff 			if (driver->err_handler &&
522f2da4ccfSGavin Shan 			    driver->err_handler->error_detected &&
52346d4be41SSam Bobroff 			    driver->err_handler->slot_reset) {
52446d4be41SSam Bobroff 				eeh_pcid_put(dev);
525cef50c67SSam Bobroff 				return;
5268cc6b6cdSThadeu Lima de Souza Cascardo 			}
52746d4be41SSam Bobroff 			eeh_pcid_put(dev);
52846d4be41SSam Bobroff 		}
52946d4be41SSam Bobroff 	}
530f5c57710SGavin Shan 
531f5c57710SGavin Shan 	/* Remove it from PCI subsystem */
5321ef52073SSam Bobroff 	pr_info("EEH: Removing %s without EEH sensitive driver\n",
533f5c57710SGavin Shan 		pci_name(dev));
534f5c57710SGavin Shan 	edev->mode |= EEH_DEV_DISCONNECTED;
5351c5c533bSSam Bobroff 	if (rmv_data)
5361c5c533bSSam Bobroff 		rmv_data->removed_dev_count++;
537f5c57710SGavin Shan 
53867086e32SWei Yang 	if (edev->physfn) {
539988fc3baSBryant G. Ly #ifdef CONFIG_PCI_IOV
54067086e32SWei Yang 		struct pci_dn *pdn = eeh_dev_to_pdn(edev);
54167086e32SWei Yang 
542753f6124SJan H. Schönherr 		pci_iov_remove_virtfn(edev->physfn, pdn->vf_index);
54367086e32SWei Yang 		edev->pdev = NULL;
54467086e32SWei Yang 
54567086e32SWei Yang 		/*
54667086e32SWei Yang 		 * We have to set the VF PE number to invalid one, which is
54767086e32SWei Yang 		 * required to plug the VF successfully.
54867086e32SWei Yang 		 */
54967086e32SWei Yang 		pdn->pe_number = IODA_INVALID_PE;
55067086e32SWei Yang #endif
55167086e32SWei Yang 		if (rmv_data)
5521c5c533bSSam Bobroff 			list_add(&edev->rmv_entry, &rmv_data->removed_vf_list);
55367086e32SWei Yang 	} else {
5541c2042c8SRafael J. Wysocki 		pci_lock_rescan_remove();
555f5c57710SGavin Shan 		pci_stop_and_remove_bus_device(dev);
5561c2042c8SRafael J. Wysocki 		pci_unlock_rescan_remove();
55767086e32SWei Yang 	}
558f5c57710SGavin Shan }
559f5c57710SGavin Shan 
560d6c4932fSSam Bobroff static void *eeh_pe_detach_dev(struct eeh_pe *pe, void *userdata)
561f5c57710SGavin Shan {
562f5c57710SGavin Shan 	struct eeh_dev *edev, *tmp;
563f5c57710SGavin Shan 
564f5c57710SGavin Shan 	eeh_pe_for_each_dev(pe, edev, tmp) {
565f5c57710SGavin Shan 		if (!(edev->mode & EEH_DEV_DISCONNECTED))
566f5c57710SGavin Shan 			continue;
567f5c57710SGavin Shan 
568f5c57710SGavin Shan 		edev->mode &= ~(EEH_DEV_DISCONNECTED | EEH_DEV_IRQ_DISABLED);
569f5c57710SGavin Shan 		eeh_rmv_from_parent_pe(edev);
570f5c57710SGavin Shan 	}
571f5c57710SGavin Shan 
572f5c57710SGavin Shan 	return NULL;
573f5c57710SGavin Shan }
574f5c57710SGavin Shan 
57578954700SGavin Shan /*
57678954700SGavin Shan  * Explicitly clear PE's frozen state for PowerNV where
57778954700SGavin Shan  * we have frozen PE until BAR restore is completed. It's
57878954700SGavin Shan  * harmless to clear it for pSeries. To be consistent with
57978954700SGavin Shan  * PE reset (for 3 times), we try to clear the frozen state
58078954700SGavin Shan  * for 3 times as well.
58178954700SGavin Shan  */
5824d8e325dSSam Bobroff static int eeh_clear_pe_frozen_state(struct eeh_pe *root, bool include_passed)
58378954700SGavin Shan {
5843376cb91SSam Bobroff 	struct eeh_pe *pe;
5853376cb91SSam Bobroff 	int i;
58678954700SGavin Shan 
5873376cb91SSam Bobroff 	eeh_for_each_pe(root, pe) {
5884d8e325dSSam Bobroff 		if (include_passed || !eeh_pe_passed(pe)) {
5893376cb91SSam Bobroff 			for (i = 0; i < 3; i++)
590188fdea6SSam Bobroff 				if (!eeh_unfreeze_pe(pe))
5913376cb91SSam Bobroff 					break;
5923376cb91SSam Bobroff 			if (i >= 3)
5933376cb91SSam Bobroff 				return -EIO;
5942c665992SGavin Shan 		}
5954d8e325dSSam Bobroff 	}
5964d8e325dSSam Bobroff 	eeh_pe_state_clear(root, EEH_PE_ISOLATED, include_passed);
5973376cb91SSam Bobroff 	return 0;
59878954700SGavin Shan }
59978954700SGavin Shan 
6005cfb20b9SGavin Shan int eeh_pe_reset_and_recover(struct eeh_pe *pe)
6015cfb20b9SGavin Shan {
6022efc771fSGavin Shan 	int ret;
6035cfb20b9SGavin Shan 
6045cfb20b9SGavin Shan 	/* Bail if the PE is being recovered */
6055cfb20b9SGavin Shan 	if (pe->state & EEH_PE_RECOVERING)
6065cfb20b9SGavin Shan 		return 0;
6075cfb20b9SGavin Shan 
6085cfb20b9SGavin Shan 	/* Put the PE into recovery mode */
6095cfb20b9SGavin Shan 	eeh_pe_state_mark(pe, EEH_PE_RECOVERING);
6105cfb20b9SGavin Shan 
6115cfb20b9SGavin Shan 	/* Save states */
6125cfb20b9SGavin Shan 	eeh_pe_dev_traverse(pe, eeh_dev_save_state, NULL);
6135cfb20b9SGavin Shan 
6145cfb20b9SGavin Shan 	/* Issue reset */
6151ef52073SSam Bobroff 	ret = eeh_pe_reset_full(pe, true);
6165cfb20b9SGavin Shan 	if (ret) {
6179ed5ca66SSam Bobroff 		eeh_pe_state_clear(pe, EEH_PE_RECOVERING, true);
6185cfb20b9SGavin Shan 		return ret;
6195cfb20b9SGavin Shan 	}
6205cfb20b9SGavin Shan 
6215cfb20b9SGavin Shan 	/* Unfreeze the PE */
6224d8e325dSSam Bobroff 	ret = eeh_clear_pe_frozen_state(pe, true);
6235cfb20b9SGavin Shan 	if (ret) {
6249ed5ca66SSam Bobroff 		eeh_pe_state_clear(pe, EEH_PE_RECOVERING, true);
6255cfb20b9SGavin Shan 		return ret;
6265cfb20b9SGavin Shan 	}
6275cfb20b9SGavin Shan 
6285cfb20b9SGavin Shan 	/* Restore device state */
6295cfb20b9SGavin Shan 	eeh_pe_dev_traverse(pe, eeh_dev_restore_state, NULL);
6305cfb20b9SGavin Shan 
6315cfb20b9SGavin Shan 	/* Clear recovery mode */
6329ed5ca66SSam Bobroff 	eeh_pe_state_clear(pe, EEH_PE_RECOVERING, true);
6335cfb20b9SGavin Shan 
6345cfb20b9SGavin Shan 	return 0;
6355cfb20b9SGavin Shan }
6365cfb20b9SGavin Shan 
637317f06deSGavin Shan /**
638317f06deSGavin Shan  * eeh_reset_device - Perform actual reset of a pci slot
6395fd13460SSam Bobroff  * @driver_eeh_aware: Does the device's driver provide EEH support?
640317f06deSGavin Shan  * @pe: EEH PE
641317f06deSGavin Shan  * @bus: PCI bus corresponding to the isolcated slot
6425fd13460SSam Bobroff  * @rmv_data: Optional, list to record removed devices
643317f06deSGavin Shan  *
644317f06deSGavin Shan  * This routine must be called to do reset on the indicated PE.
645317f06deSGavin Shan  * During the reset, udev might be invoked because those affected
646317f06deSGavin Shan  * PCI devices will be removed and then added.
647317f06deSGavin Shan  */
64867086e32SWei Yang static int eeh_reset_device(struct eeh_pe *pe, struct pci_bus *bus,
6495fd13460SSam Bobroff 			    struct eeh_rmv_data *rmv_data,
6505fd13460SSam Bobroff 			    bool driver_eeh_aware)
651317f06deSGavin Shan {
652edfd17ffSArnd Bergmann 	time64_t tstamp;
65367086e32SWei Yang 	int cnt, rc;
65467086e32SWei Yang 	struct eeh_dev *edev;
6551ef52073SSam Bobroff 	struct eeh_pe *tmp_pe;
6561ef52073SSam Bobroff 	bool any_passed = false;
6571ef52073SSam Bobroff 
6581ef52073SSam Bobroff 	eeh_for_each_pe(pe, tmp_pe)
6591ef52073SSam Bobroff 		any_passed |= eeh_pe_passed(tmp_pe);
660317f06deSGavin Shan 
661317f06deSGavin Shan 	/* pcibios will clear the counter; save the value */
662317f06deSGavin Shan 	cnt = pe->freeze_count;
6635a71978eSGavin Shan 	tstamp = pe->tstamp;
664317f06deSGavin Shan 
665317f06deSGavin Shan 	/*
666317f06deSGavin Shan 	 * We don't remove the corresponding PE instances because
667317f06deSGavin Shan 	 * we need the information afterwords. The attached EEH
668317f06deSGavin Shan 	 * devices are expected to be attached soon when calling
669bd251b89SGavin Shan 	 * into pci_hp_add_devices().
670317f06deSGavin Shan 	 */
671807a827dSGavin Shan 	eeh_pe_state_mark(pe, EEH_PE_KEEP);
6721ef52073SSam Bobroff 	if (any_passed || driver_eeh_aware || (pe->type & EEH_PE_VF)) {
67354048cf8SSam Bobroff 		eeh_pe_dev_traverse(pe, eeh_rmv_device, rmv_data);
67467086e32SWei Yang 	} else {
6751c2042c8SRafael J. Wysocki 		pci_lock_rescan_remove();
676bd251b89SGavin Shan 		pci_hp_remove_devices(bus);
6771c2042c8SRafael J. Wysocki 		pci_unlock_rescan_remove();
67867086e32SWei Yang 	}
679317f06deSGavin Shan 
680d0914f50SGavin Shan 	/*
681d0914f50SGavin Shan 	 * Reset the pci controller. (Asserts RST#; resets config space).
682317f06deSGavin Shan 	 * Reconfigure bridges and devices. Don't try to bring the system
683317f06deSGavin Shan 	 * up if the reset failed for some reason.
684d0914f50SGavin Shan 	 *
685d0914f50SGavin Shan 	 * During the reset, it's very dangerous to have uncontrolled PCI
686d0914f50SGavin Shan 	 * config accesses. So we prefer to block them. However, controlled
687d0914f50SGavin Shan 	 * PCI config accesses initiated from EEH itself are allowed.
688317f06deSGavin Shan 	 */
6891ef52073SSam Bobroff 	rc = eeh_pe_reset_full(pe, false);
69028bf36f9SGavin Shan 	if (rc)
691317f06deSGavin Shan 		return rc;
692317f06deSGavin Shan 
6931c2042c8SRafael J. Wysocki 	pci_lock_rescan_remove();
6941c2042c8SRafael J. Wysocki 
695317f06deSGavin Shan 	/* Restore PE */
696317f06deSGavin Shan 	eeh_ops->configure_bridge(pe);
697317f06deSGavin Shan 	eeh_pe_restore_bars(pe);
698317f06deSGavin Shan 
699dc9c41bdSAndrew Donnellan 	/* Clear frozen state */
7001ef52073SSam Bobroff 	rc = eeh_clear_pe_frozen_state(pe, false);
701409bf7f8SAndrew Donnellan 	if (rc) {
702409bf7f8SAndrew Donnellan 		pci_unlock_rescan_remove();
70378954700SGavin Shan 		return rc;
704409bf7f8SAndrew Donnellan 	}
70578954700SGavin Shan 
706317f06deSGavin Shan 	/* Give the system 5 seconds to finish running the user-space
707317f06deSGavin Shan 	 * hotplug shutdown scripts, e.g. ifdown for ethernet.  Yes,
708317f06deSGavin Shan 	 * this is a hack, but if we don't do this, and try to bring
709317f06deSGavin Shan 	 * the device up before the scripts have taken it down,
710317f06deSGavin Shan 	 * potentially weird things happen.
711317f06deSGavin Shan 	 */
7121c5c533bSSam Bobroff 	if (!driver_eeh_aware || rmv_data->removed_dev_count) {
71354048cf8SSam Bobroff 		pr_info("EEH: Sleep 5s ahead of %s hotplug\n",
71454048cf8SSam Bobroff 			(driver_eeh_aware ? "partial" : "complete"));
715317f06deSGavin Shan 		ssleep(5);
716f5c57710SGavin Shan 
717f5c57710SGavin Shan 		/*
718f5c57710SGavin Shan 		 * The EEH device is still connected with its parent
719f5c57710SGavin Shan 		 * PE. We should disconnect it so the binding can be
720f5c57710SGavin Shan 		 * rebuilt when adding PCI devices.
721f5c57710SGavin Shan 		 */
72280e65b00SSam Bobroff 		edev = list_first_entry(&pe->edevs, struct eeh_dev, entry);
723f5c57710SGavin Shan 		eeh_pe_traverse(pe, eeh_pe_detach_dev, NULL);
724a3aa256bSGavin Shan 		if (pe->type & EEH_PE_VF) {
725bf773df9SSam Bobroff 			eeh_add_virt_device(edev);
726a3aa256bSGavin Shan 		} else {
72754048cf8SSam Bobroff 			if (!driver_eeh_aware)
7289ed5ca66SSam Bobroff 				eeh_pe_state_clear(pe, EEH_PE_PRI_BUS, true);
729bd251b89SGavin Shan 			pci_hp_add_devices(bus);
730a3aa256bSGavin Shan 		}
731317f06deSGavin Shan 	}
7329ed5ca66SSam Bobroff 	eeh_pe_state_clear(pe, EEH_PE_KEEP, true);
7335a71978eSGavin Shan 
7345a71978eSGavin Shan 	pe->tstamp = tstamp;
735317f06deSGavin Shan 	pe->freeze_count = cnt;
736317f06deSGavin Shan 
7371c2042c8SRafael J. Wysocki 	pci_unlock_rescan_remove();
738317f06deSGavin Shan 	return 0;
739317f06deSGavin Shan }
740317f06deSGavin Shan 
741317f06deSGavin Shan /* The longest amount of time to wait for a pci device
742317f06deSGavin Shan  * to come back on line, in seconds.
743317f06deSGavin Shan  */
744fb48dc22SBrian King #define MAX_WAIT_FOR_RECOVERY 300
745317f06deSGavin Shan 
746799abe28SOliver O'Halloran 
747799abe28SOliver O'Halloran /* Walks the PE tree after processing an event to remove any stale PEs.
748799abe28SOliver O'Halloran  *
749799abe28SOliver O'Halloran  * NB: This needs to be recursive to ensure the leaf PEs get removed
750799abe28SOliver O'Halloran  * before their parents do. Although this is possible to do recursively
751799abe28SOliver O'Halloran  * we don't since this is easier to read and we need to garantee
752799abe28SOliver O'Halloran  * the leaf nodes will be handled first.
753799abe28SOliver O'Halloran  */
754799abe28SOliver O'Halloran static void eeh_pe_cleanup(struct eeh_pe *pe)
755799abe28SOliver O'Halloran {
756799abe28SOliver O'Halloran 	struct eeh_pe *child_pe, *tmp;
757799abe28SOliver O'Halloran 
758799abe28SOliver O'Halloran 	list_for_each_entry_safe(child_pe, tmp, &pe->child_list, child)
759799abe28SOliver O'Halloran 		eeh_pe_cleanup(child_pe);
760799abe28SOliver O'Halloran 
761799abe28SOliver O'Halloran 	if (pe->state & EEH_PE_KEEP)
762799abe28SOliver O'Halloran 		return;
763799abe28SOliver O'Halloran 
764799abe28SOliver O'Halloran 	if (!(pe->state & EEH_PE_INVALID))
765799abe28SOliver O'Halloran 		return;
766799abe28SOliver O'Halloran 
767799abe28SOliver O'Halloran 	if (list_empty(&pe->edevs) && list_empty(&pe->child_list)) {
768799abe28SOliver O'Halloran 		list_del(&pe->child);
769799abe28SOliver O'Halloran 		kfree(pe);
770799abe28SOliver O'Halloran 	}
771799abe28SOliver O'Halloran }
772799abe28SOliver O'Halloran 
773c0b64978SRussell Currey /**
774b104af5aSOliver O'Halloran  * eeh_check_slot_presence - Check if a device is still present in a slot
775b104af5aSOliver O'Halloran  * @pdev: pci_dev to check
776b104af5aSOliver O'Halloran  *
777b104af5aSOliver O'Halloran  * This function may return a false positive if we can't determine the slot's
778b104af5aSOliver O'Halloran  * presence state. This might happen for for PCIe slots if the PE containing
779b104af5aSOliver O'Halloran  * the upstream bridge is also frozen, or the bridge is part of the same PE
780b104af5aSOliver O'Halloran  * as the device.
781b104af5aSOliver O'Halloran  *
782b104af5aSOliver O'Halloran  * This shouldn't happen often, but you might see it if you hotplug a PCIe
783b104af5aSOliver O'Halloran  * switch.
784b104af5aSOliver O'Halloran  */
785b104af5aSOliver O'Halloran static bool eeh_slot_presence_check(struct pci_dev *pdev)
786b104af5aSOliver O'Halloran {
787b104af5aSOliver O'Halloran 	const struct hotplug_slot_ops *ops;
788b104af5aSOliver O'Halloran 	struct pci_slot *slot;
789b104af5aSOliver O'Halloran 	u8 state;
790b104af5aSOliver O'Halloran 	int rc;
791b104af5aSOliver O'Halloran 
792b104af5aSOliver O'Halloran 	if (!pdev)
793b104af5aSOliver O'Halloran 		return false;
794b104af5aSOliver O'Halloran 
795b104af5aSOliver O'Halloran 	if (pdev->error_state == pci_channel_io_perm_failure)
796b104af5aSOliver O'Halloran 		return false;
797b104af5aSOliver O'Halloran 
798b104af5aSOliver O'Halloran 	slot = pdev->slot;
799b104af5aSOliver O'Halloran 	if (!slot || !slot->hotplug)
800b104af5aSOliver O'Halloran 		return true;
801b104af5aSOliver O'Halloran 
802b104af5aSOliver O'Halloran 	ops = slot->hotplug->ops;
803b104af5aSOliver O'Halloran 	if (!ops || !ops->get_adapter_status)
804b104af5aSOliver O'Halloran 		return true;
805b104af5aSOliver O'Halloran 
806aeff27c1SOliver O'Halloran 	/* set the attention indicator while we've got the slot ops */
807aeff27c1SOliver O'Halloran 	if (ops->set_attention_status)
808aeff27c1SOliver O'Halloran 		ops->set_attention_status(slot->hotplug, 1);
809aeff27c1SOliver O'Halloran 
810b104af5aSOliver O'Halloran 	rc = ops->get_adapter_status(slot->hotplug, &state);
811b104af5aSOliver O'Halloran 	if (rc)
812b104af5aSOliver O'Halloran 		return true;
813b104af5aSOliver O'Halloran 
814b104af5aSOliver O'Halloran 	return !!state;
815b104af5aSOliver O'Halloran }
816b104af5aSOliver O'Halloran 
817aeff27c1SOliver O'Halloran static void eeh_clear_slot_attention(struct pci_dev *pdev)
818aeff27c1SOliver O'Halloran {
819aeff27c1SOliver O'Halloran 	const struct hotplug_slot_ops *ops;
820aeff27c1SOliver O'Halloran 	struct pci_slot *slot;
821aeff27c1SOliver O'Halloran 
822aeff27c1SOliver O'Halloran 	if (!pdev)
823aeff27c1SOliver O'Halloran 		return;
824aeff27c1SOliver O'Halloran 
825aeff27c1SOliver O'Halloran 	if (pdev->error_state == pci_channel_io_perm_failure)
826aeff27c1SOliver O'Halloran 		return;
827aeff27c1SOliver O'Halloran 
828aeff27c1SOliver O'Halloran 	slot = pdev->slot;
829aeff27c1SOliver O'Halloran 	if (!slot || !slot->hotplug)
830aeff27c1SOliver O'Halloran 		return;
831aeff27c1SOliver O'Halloran 
832aeff27c1SOliver O'Halloran 	ops = slot->hotplug->ops;
833aeff27c1SOliver O'Halloran 	if (!ops || !ops->set_attention_status)
834aeff27c1SOliver O'Halloran 		return;
835aeff27c1SOliver O'Halloran 
836aeff27c1SOliver O'Halloran 	ops->set_attention_status(slot->hotplug, 0);
837aeff27c1SOliver O'Halloran }
838aeff27c1SOliver O'Halloran 
839b104af5aSOliver O'Halloran /**
840c0b64978SRussell Currey  * eeh_handle_normal_event - Handle EEH events on a specific PE
84137fd8125SSam Bobroff  * @pe: EEH PE - which should not be used after we return, as it may
84237fd8125SSam Bobroff  * have been invalidated.
843c0b64978SRussell Currey  *
844c0b64978SRussell Currey  * Attempts to recover the given PE.  If recovery fails or the PE has failed
845c0b64978SRussell Currey  * too many times, remove the PE.
846c0b64978SRussell Currey  *
84768701780SSam Bobroff  * While PHB detects address or data parity errors on particular PCI
84868701780SSam Bobroff  * slot, the associated PE will be frozen. Besides, DMA's occurring
84968701780SSam Bobroff  * to wild addresses (which usually happen due to bugs in device
85068701780SSam Bobroff  * drivers or in PCI adapter firmware) can cause EEH error. #SERR,
85168701780SSam Bobroff  * #PERR or other misc PCI-related errors also can trigger EEH errors.
85268701780SSam Bobroff  *
85368701780SSam Bobroff  * Recovery process consists of unplugging the device driver (which
85468701780SSam Bobroff  * generated hotplug events to userspace), then issuing a PCI #RST to
85568701780SSam Bobroff  * the device, then reconfiguring the PCI config space for all bridges
85668701780SSam Bobroff  * & devices under this slot, and then finally restarting the device
85768701780SSam Bobroff  * drivers (which cause a second set of hotplug events to go out to
85868701780SSam Bobroff  * userspace).
859c0b64978SRussell Currey  */
86037fd8125SSam Bobroff void eeh_handle_normal_event(struct eeh_pe *pe)
861317f06deSGavin Shan {
862cd95f804SSam Bobroff 	struct pci_bus *bus;
86367086e32SWei Yang 	struct eeh_dev *edev, *tmp;
864665012c5SSam Bobroff 	struct eeh_pe *tmp_pe;
865317f06deSGavin Shan 	int rc = 0;
866317f06deSGavin Shan 	enum pci_ers_result result = PCI_ERS_RESULT_NONE;
8671c5c533bSSam Bobroff 	struct eeh_rmv_data rmv_data =
8681c5c533bSSam Bobroff 		{LIST_HEAD_INIT(rmv_data.removed_vf_list), 0};
869b104af5aSOliver O'Halloran 	int devices = 0;
870317f06deSGavin Shan 
871cd95f804SSam Bobroff 	bus = eeh_pe_bus_get(pe);
872cd95f804SSam Bobroff 	if (!bus) {
8731f52f176SRussell Currey 		pr_err("%s: Cannot find PCI bus for PHB#%x-PE#%x\n",
874317f06deSGavin Shan 			__func__, pe->phb->global_number, pe->addr);
87537fd8125SSam Bobroff 		return;
876317f06deSGavin Shan 	}
877317f06deSGavin Shan 
878b104af5aSOliver O'Halloran 	/*
879b104af5aSOliver O'Halloran 	 * When devices are hot-removed we might get an EEH due to
880b104af5aSOliver O'Halloran 	 * a driver attempting to touch the MMIO space of a removed
881b104af5aSOliver O'Halloran 	 * device. In this case we don't have a device to recover
882b104af5aSOliver O'Halloran 	 * so suppress the event if we can't find any present devices.
883b104af5aSOliver O'Halloran 	 *
884b104af5aSOliver O'Halloran 	 * The hotplug driver should take care of tearing down the
885b104af5aSOliver O'Halloran 	 * device itself.
886b104af5aSOliver O'Halloran 	 */
887b104af5aSOliver O'Halloran 	eeh_for_each_pe(pe, tmp_pe)
888b104af5aSOliver O'Halloran 		eeh_pe_for_each_dev(tmp_pe, edev, tmp)
889b104af5aSOliver O'Halloran 			if (eeh_slot_presence_check(edev->pdev))
890b104af5aSOliver O'Halloran 				devices++;
891b104af5aSOliver O'Halloran 
89225baf3d8SOliver O'Halloran 	if (!devices) {
89325baf3d8SOliver O'Halloran 		pr_debug("EEH: Frozen PHB#%x-PE#%x is empty!\n",
89425baf3d8SOliver O'Halloran 			pe->phb->global_number, pe->addr);
895b104af5aSOliver O'Halloran 		goto out; /* nothing to recover */
89625baf3d8SOliver O'Halloran 	}
89725baf3d8SOliver O'Halloran 
89825baf3d8SOliver O'Halloran 	/* Log the event */
89925baf3d8SOliver O'Halloran 	if (pe->type & EEH_PE_PHB) {
90025baf3d8SOliver O'Halloran 		pr_err("EEH: PHB#%x failure detected, location: %s\n",
90125baf3d8SOliver O'Halloran 			pe->phb->global_number, eeh_pe_loc_get(pe));
90225baf3d8SOliver O'Halloran 	} else {
90325baf3d8SOliver O'Halloran 		struct eeh_pe *phb_pe = eeh_phb_pe_get(pe->phb);
90425baf3d8SOliver O'Halloran 
90525baf3d8SOliver O'Halloran 		pr_err("EEH: Frozen PHB#%x-PE#%x detected\n",
90625baf3d8SOliver O'Halloran 		       pe->phb->global_number, pe->addr);
90725baf3d8SOliver O'Halloran 		pr_err("EEH: PE location: %s, PHB location: %s\n",
90825baf3d8SOliver O'Halloran 		       eeh_pe_loc_get(pe), eeh_pe_loc_get(phb_pe));
90925baf3d8SOliver O'Halloran 	}
91025baf3d8SOliver O'Halloran 
9111b7f3b6cSMichael Ellerman #ifdef CONFIG_STACKTRACE
91225baf3d8SOliver O'Halloran 	/*
91325baf3d8SOliver O'Halloran 	 * Print the saved stack trace now that we've verified there's
91425baf3d8SOliver O'Halloran 	 * something to recover.
91525baf3d8SOliver O'Halloran 	 */
91625baf3d8SOliver O'Halloran 	if (pe->trace_entries) {
91725baf3d8SOliver O'Halloran 		void **ptrs = (void **) pe->stack_trace;
91825baf3d8SOliver O'Halloran 		int i;
91925baf3d8SOliver O'Halloran 
92025baf3d8SOliver O'Halloran 		pr_err("EEH: Frozen PHB#%x-PE#%x detected\n",
92125baf3d8SOliver O'Halloran 		       pe->phb->global_number, pe->addr);
92225baf3d8SOliver O'Halloran 
92325baf3d8SOliver O'Halloran 		/* FIXME: Use the same format as dump_stack() */
92425baf3d8SOliver O'Halloran 		pr_err("EEH: Call Trace:\n");
92525baf3d8SOliver O'Halloran 		for (i = 0; i < pe->trace_entries; i++)
92625baf3d8SOliver O'Halloran 			pr_err("EEH: [%pK] %pS\n", ptrs[i], ptrs[i]);
92725baf3d8SOliver O'Halloran 
92825baf3d8SOliver O'Halloran 		pe->trace_entries = 0;
92925baf3d8SOliver O'Halloran 	}
9301b7f3b6cSMichael Ellerman #endif /* CONFIG_STACKTRACE */
931b104af5aSOliver O'Halloran 
9325a71978eSGavin Shan 	eeh_pe_update_time_stamp(pe);
933317f06deSGavin Shan 	pe->freeze_count++;
934c0b64978SRussell Currey 	if (pe->freeze_count > eeh_max_freezes) {
935796b9f5bSSam Bobroff 		pr_err("EEH: PHB#%x-PE#%x has failed %d times in the last hour and has been permanently disabled.\n",
936c0b64978SRussell Currey 		       pe->phb->global_number, pe->addr,
937c0b64978SRussell Currey 		       pe->freeze_count);
938b90484ecSSam Bobroff 		result = PCI_ERS_RESULT_DISCONNECT;
939c0b64978SRussell Currey 	}
940317f06deSGavin Shan 
941aa06e3d6SSam Bobroff 	eeh_for_each_pe(pe, tmp_pe)
942aa06e3d6SSam Bobroff 		eeh_pe_for_each_dev(tmp_pe, edev, tmp)
943aa06e3d6SSam Bobroff 			edev->mode &= ~EEH_DEV_NO_HANDLER;
944aa06e3d6SSam Bobroff 
945317f06deSGavin Shan 	/* Walk the various device drivers attached to this slot through
946317f06deSGavin Shan 	 * a reset sequence, giving each an opportunity to do what it needs
947317f06deSGavin Shan 	 * to accomplish the reset.  Each child gets a report of the
948317f06deSGavin Shan 	 * status ... if any child can't handle the reset, then the entire
949317f06deSGavin Shan 	 * slot is dlpar removed and added.
9508234fcedSGavin Shan 	 *
9518234fcedSGavin Shan 	 * When the PHB is fenced, we have to issue a reset to recover from
9528234fcedSGavin Shan 	 * the error. Override the result if necessary to have partially
9538234fcedSGavin Shan 	 * hotplug for this case.
954317f06deSGavin Shan 	 */
955b90484ecSSam Bobroff 	if (result != PCI_ERS_RESULT_DISCONNECT) {
956b90484ecSSam Bobroff 		pr_warn("EEH: This PCI device has failed %d times in the last hour and will be permanently disabled after %d failures.\n",
957b90484ecSSam Bobroff 			pe->freeze_count, eeh_max_freezes);
95856ca4fdeSGavin Shan 		pr_info("EEH: Notify device drivers to shutdown\n");
95947cc8c1cSSam Bobroff 		eeh_set_channel_state(pe, pci_channel_io_frozen);
960010acfa1SSam Bobroff 		eeh_set_irq_state(pe, false);
961b90484ecSSam Bobroff 		eeh_pe_report("error_detected(IO frozen)", pe,
962b90484ecSSam Bobroff 			      eeh_report_error, &result);
9638234fcedSGavin Shan 		if ((pe->type & EEH_PE_PHB) &&
9648234fcedSGavin Shan 		    result != PCI_ERS_RESULT_NONE &&
9658234fcedSGavin Shan 		    result != PCI_ERS_RESULT_NEED_RESET)
9668234fcedSGavin Shan 			result = PCI_ERS_RESULT_NEED_RESET;
967b90484ecSSam Bobroff 	}
968317f06deSGavin Shan 
969317f06deSGavin Shan 	/* Get the current PCI slot state. This can take a long time,
9702ac3990cSWei Yang 	 * sometimes over 300 seconds for certain systems.
971317f06deSGavin Shan 	 */
972b90484ecSSam Bobroff 	if (result != PCI_ERS_RESULT_DISCONNECT) {
973fef7f905SSam Bobroff 		rc = eeh_wait_state(pe, MAX_WAIT_FOR_RECOVERY*1000);
974317f06deSGavin Shan 		if (rc < 0 || rc == EEH_STATE_NOT_SUPPORT) {
9750dae2743SGavin Shan 			pr_warn("EEH: Permanent failure\n");
976b90484ecSSam Bobroff 			result = PCI_ERS_RESULT_DISCONNECT;
977b90484ecSSam Bobroff 		}
978317f06deSGavin Shan 	}
979317f06deSGavin Shan 
980317f06deSGavin Shan 	/* Since rtas may enable MMIO when posting the error log,
981317f06deSGavin Shan 	 * don't post the error log until after all dev drivers
982317f06deSGavin Shan 	 * have been informed.
983317f06deSGavin Shan 	 */
984b90484ecSSam Bobroff 	if (result != PCI_ERS_RESULT_DISCONNECT) {
98556ca4fdeSGavin Shan 		pr_info("EEH: Collect temporary log\n");
986317f06deSGavin Shan 		eeh_slot_error_detail(pe, EEH_LOG_TEMP);
987b90484ecSSam Bobroff 	}
988317f06deSGavin Shan 
989317f06deSGavin Shan 	/* If all device drivers were EEH-unaware, then shut
990317f06deSGavin Shan 	 * down all of the device drivers, and hope they
991317f06deSGavin Shan 	 * go down willingly, without panicing the system.
992317f06deSGavin Shan 	 */
993317f06deSGavin Shan 	if (result == PCI_ERS_RESULT_NONE) {
99456ca4fdeSGavin Shan 		pr_info("EEH: Reset with hotplug activity\n");
9955fd13460SSam Bobroff 		rc = eeh_reset_device(pe, bus, NULL, false);
996317f06deSGavin Shan 		if (rc) {
9970dae2743SGavin Shan 			pr_warn("%s: Unable to reset, err=%d\n",
99856ca4fdeSGavin Shan 				__func__, rc);
999b90484ecSSam Bobroff 			result = PCI_ERS_RESULT_DISCONNECT;
1000317f06deSGavin Shan 		}
1001317f06deSGavin Shan 	}
1002317f06deSGavin Shan 
1003317f06deSGavin Shan 	/* If all devices reported they can proceed, then re-enable MMIO */
1004317f06deSGavin Shan 	if (result == PCI_ERS_RESULT_CAN_RECOVER) {
100556ca4fdeSGavin Shan 		pr_info("EEH: Enable I/O for affected devices\n");
1006317f06deSGavin Shan 		rc = eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
1007317f06deSGavin Shan 
1008b90484ecSSam Bobroff 		if (rc < 0) {
1009b90484ecSSam Bobroff 			result = PCI_ERS_RESULT_DISCONNECT;
1010b90484ecSSam Bobroff 		} else if (rc) {
1011317f06deSGavin Shan 			result = PCI_ERS_RESULT_NEED_RESET;
1012317f06deSGavin Shan 		} else {
101356ca4fdeSGavin Shan 			pr_info("EEH: Notify device drivers to resume I/O\n");
101420b34497SSam Bobroff 			eeh_pe_report("mmio_enabled", pe,
101520b34497SSam Bobroff 				      eeh_report_mmio_enabled, &result);
1016317f06deSGavin Shan 		}
1017317f06deSGavin Shan 	}
1018317f06deSGavin Shan 
1019317f06deSGavin Shan 	/* If all devices reported they can proceed, then re-enable DMA */
1020317f06deSGavin Shan 	if (result == PCI_ERS_RESULT_CAN_RECOVER) {
102156ca4fdeSGavin Shan 		pr_info("EEH: Enabled DMA for affected devices\n");
1022317f06deSGavin Shan 		rc = eeh_pci_enable(pe, EEH_OPT_THAW_DMA);
1023317f06deSGavin Shan 
1024b90484ecSSam Bobroff 		if (rc < 0) {
1025b90484ecSSam Bobroff 			result = PCI_ERS_RESULT_DISCONNECT;
1026b90484ecSSam Bobroff 		} else if (rc) {
1027317f06deSGavin Shan 			result = PCI_ERS_RESULT_NEED_RESET;
102835845a78SGavin Shan 		} else {
102935845a78SGavin Shan 			/*
103035845a78SGavin Shan 			 * We didn't do PE reset for the case. The PE
103135845a78SGavin Shan 			 * is still in frozen state. Clear it before
103235845a78SGavin Shan 			 * resuming the PE.
103335845a78SGavin Shan 			 */
10349ed5ca66SSam Bobroff 			eeh_pe_state_clear(pe, EEH_PE_ISOLATED, true);
1035317f06deSGavin Shan 			result = PCI_ERS_RESULT_RECOVERED;
1036317f06deSGavin Shan 		}
103735845a78SGavin Shan 	}
1038317f06deSGavin Shan 
1039317f06deSGavin Shan 	/* If any device called out for a reset, then reset the slot */
1040317f06deSGavin Shan 	if (result == PCI_ERS_RESULT_NEED_RESET) {
104156ca4fdeSGavin Shan 		pr_info("EEH: Reset without hotplug activity\n");
10425fd13460SSam Bobroff 		rc = eeh_reset_device(pe, bus, &rmv_data, true);
1043317f06deSGavin Shan 		if (rc) {
10440dae2743SGavin Shan 			pr_warn("%s: Cannot reset, err=%d\n",
104556ca4fdeSGavin Shan 				__func__, rc);
1046b90484ecSSam Bobroff 			result = PCI_ERS_RESULT_DISCONNECT;
1047b90484ecSSam Bobroff 		} else {
1048317f06deSGavin Shan 			result = PCI_ERS_RESULT_NONE;
104947cc8c1cSSam Bobroff 			eeh_set_channel_state(pe, pci_channel_io_normal);
1050010acfa1SSam Bobroff 			eeh_set_irq_state(pe, true);
1051b90484ecSSam Bobroff 			eeh_pe_report("slot_reset", pe, eeh_report_reset,
1052b90484ecSSam Bobroff 				      &result);
1053b90484ecSSam Bobroff 		}
1054317f06deSGavin Shan 	}
1055317f06deSGavin Shan 
1056b90484ecSSam Bobroff 	if ((result == PCI_ERS_RESULT_RECOVERED) ||
1057b90484ecSSam Bobroff 	    (result == PCI_ERS_RESULT_NONE)) {
105867086e32SWei Yang 		/*
1059b90484ecSSam Bobroff 		 * For those hot removed VFs, we should add back them after PF
1060b90484ecSSam Bobroff 		 * get recovered properly.
106167086e32SWei Yang 		 */
10621c5c533bSSam Bobroff 		list_for_each_entry_safe(edev, tmp, &rmv_data.removed_vf_list,
10631c5c533bSSam Bobroff 					 rmv_entry) {
1064bf773df9SSam Bobroff 			eeh_add_virt_device(edev);
106580e65b00SSam Bobroff 			list_del(&edev->rmv_entry);
106667086e32SWei Yang 		}
106767086e32SWei Yang 
1068317f06deSGavin Shan 		/* Tell all device drivers that they can resume operations */
106956ca4fdeSGavin Shan 		pr_info("EEH: Notify device driver to resume\n");
107047cc8c1cSSam Bobroff 		eeh_set_channel_state(pe, pci_channel_io_normal);
1071010acfa1SSam Bobroff 		eeh_set_irq_state(pe, true);
107220b34497SSam Bobroff 		eeh_pe_report("resume", pe, eeh_report_resume, NULL);
107320b34497SSam Bobroff 		eeh_for_each_pe(pe, tmp_pe) {
107420b34497SSam Bobroff 			eeh_pe_for_each_dev(tmp_pe, edev, tmp) {
1075665012c5SSam Bobroff 				edev->mode &= ~EEH_DEV_NO_HANDLER;
107620b34497SSam Bobroff 				edev->in_error = false;
107720b34497SSam Bobroff 			}
107820b34497SSam Bobroff 		}
1079665012c5SSam Bobroff 
1080796b9f5bSSam Bobroff 		pr_info("EEH: Recovery successful.\n");
1081b90484ecSSam Bobroff 	} else  {
1082317f06deSGavin Shan 		/*
1083317f06deSGavin Shan 		 * About 90% of all real-life EEH failures in the field
1084317f06deSGavin Shan 		 * are due to poorly seated PCI cards. Only 10% or so are
1085317f06deSGavin Shan 		 * due to actual, failed cards.
1086317f06deSGavin Shan 		 */
10871f52f176SRussell Currey 		pr_err("EEH: Unable to recover from failure from PHB#%x-PE#%x.\n"
1088317f06deSGavin Shan 		       "Please try reseating or replacing it\n",
1089317f06deSGavin Shan 			pe->phb->global_number, pe->addr);
1090317f06deSGavin Shan 
1091317f06deSGavin Shan 		eeh_slot_error_detail(pe, EEH_LOG_PERM);
1092317f06deSGavin Shan 
1093317f06deSGavin Shan 		/* Notify all devices that they're about to go down. */
109447cc8c1cSSam Bobroff 		eeh_set_channel_state(pe, pci_channel_io_perm_failure);
1095010acfa1SSam Bobroff 		eeh_set_irq_state(pe, false);
109620b34497SSam Bobroff 		eeh_pe_report("error_detected(permanent failure)", pe,
109720b34497SSam Bobroff 			      eeh_report_failure, NULL);
1098317f06deSGavin Shan 
1099d2b0f6f7SGavin Shan 		/* Mark the PE to be removed permanently */
1100432227e9SGavin Shan 		eeh_pe_state_mark(pe, EEH_PE_REMOVED);
1101d2b0f6f7SGavin Shan 
1102d2b0f6f7SGavin Shan 		/*
1103d2b0f6f7SGavin Shan 		 * Shut down the device drivers for good. We mark
1104d2b0f6f7SGavin Shan 		 * all removed devices correctly to avoid access
1105d2b0f6f7SGavin Shan 		 * the their PCI config any more.
1106d2b0f6f7SGavin Shan 		 */
110767086e32SWei Yang 		if (pe->type & EEH_PE_VF) {
110867086e32SWei Yang 			eeh_pe_dev_traverse(pe, eeh_rmv_device, NULL);
110967086e32SWei Yang 			eeh_pe_dev_mode_mark(pe, EEH_DEV_REMOVED);
111067086e32SWei Yang 		} else {
11119ed5ca66SSam Bobroff 			eeh_pe_state_clear(pe, EEH_PE_PRI_BUS, true);
1112d2b0f6f7SGavin Shan 			eeh_pe_dev_mode_mark(pe, EEH_DEV_REMOVED);
1113d2b0f6f7SGavin Shan 
11141c2042c8SRafael J. Wysocki 			pci_lock_rescan_remove();
1115cd95f804SSam Bobroff 			pci_hp_remove_devices(bus);
11161c2042c8SRafael J. Wysocki 			pci_unlock_rescan_remove();
1117daeba295SRussell Currey 			/* The passed PE should no longer be used */
111837fd8125SSam Bobroff 			return;
11191c2042c8SRafael J. Wysocki 		}
1120b90484ecSSam Bobroff 	}
1121799abe28SOliver O'Halloran 
1122b104af5aSOliver O'Halloran out:
1123799abe28SOliver O'Halloran 	/*
1124799abe28SOliver O'Halloran 	 * Clean up any PEs without devices. While marked as EEH_PE_RECOVERYING
1125799abe28SOliver O'Halloran 	 * we don't want to modify the PE tree structure so we do it here.
1126799abe28SOliver O'Halloran 	 */
1127799abe28SOliver O'Halloran 	eeh_pe_cleanup(pe);
1128aeff27c1SOliver O'Halloran 
1129aeff27c1SOliver O'Halloran 	/* clear the slot attention LED for all recovered devices */
1130aeff27c1SOliver O'Halloran 	eeh_for_each_pe(pe, tmp_pe)
1131aeff27c1SOliver O'Halloran 		eeh_pe_for_each_dev(tmp_pe, edev, tmp)
1132aeff27c1SOliver O'Halloran 			eeh_clear_slot_attention(edev->pdev);
1133aeff27c1SOliver O'Halloran 
11349ed5ca66SSam Bobroff 	eeh_pe_state_clear(pe, EEH_PE_RECOVERING, true);
113567086e32SWei Yang }
11368a6b1bc7SGavin Shan 
1137c0b64978SRussell Currey /**
1138c0b64978SRussell Currey  * eeh_handle_special_event - Handle EEH events without a specific failing PE
1139c0b64978SRussell Currey  *
1140c0b64978SRussell Currey  * Called when an EEH event is detected but can't be narrowed down to a
1141c0b64978SRussell Currey  * specific PE.  Iterates through possible failures and handles them as
1142c0b64978SRussell Currey  * necessary.
1143c0b64978SRussell Currey  */
114468701780SSam Bobroff void eeh_handle_special_event(void)
11458a6b1bc7SGavin Shan {
1146aa06e3d6SSam Bobroff 	struct eeh_pe *pe, *phb_pe, *tmp_pe;
1147aa06e3d6SSam Bobroff 	struct eeh_dev *edev, *tmp_edev;
11488a6b1bc7SGavin Shan 	struct pci_bus *bus;
11497e4e7867SGavin Shan 	struct pci_controller *hose;
11508a6b1bc7SGavin Shan 	unsigned long flags;
11517e4e7867SGavin Shan 	int rc;
11528a6b1bc7SGavin Shan 
11537e4e7867SGavin Shan 
11547e4e7867SGavin Shan 	do {
11558a6b1bc7SGavin Shan 		rc = eeh_ops->next_error(&pe);
11568a6b1bc7SGavin Shan 
11578a6b1bc7SGavin Shan 		switch (rc) {
11587e4e7867SGavin Shan 		case EEH_NEXT_ERR_DEAD_IOC:
11598a6b1bc7SGavin Shan 			/* Mark all PHBs in dead state */
11608a6b1bc7SGavin Shan 			eeh_serialize_lock(&flags);
11617e4e7867SGavin Shan 
11627e4e7867SGavin Shan 			/* Purge all events */
11635c7a35e3SGavin Shan 			eeh_remove_event(NULL, true);
11647e4e7867SGavin Shan 
11657e4e7867SGavin Shan 			list_for_each_entry(hose, &hose_list, list_node) {
11668a6b1bc7SGavin Shan 				phb_pe = eeh_phb_pe_get(hose);
11678a6b1bc7SGavin Shan 				if (!phb_pe) continue;
11688a6b1bc7SGavin Shan 
1169e762bb89SSam Bobroff 				eeh_pe_mark_isolated(phb_pe);
11708a6b1bc7SGavin Shan 			}
11717e4e7867SGavin Shan 
11728a6b1bc7SGavin Shan 			eeh_serialize_unlock(flags);
11738a6b1bc7SGavin Shan 
11748a6b1bc7SGavin Shan 			break;
11757e4e7867SGavin Shan 		case EEH_NEXT_ERR_FROZEN_PE:
11767e4e7867SGavin Shan 		case EEH_NEXT_ERR_FENCED_PHB:
11777e4e7867SGavin Shan 		case EEH_NEXT_ERR_DEAD_PHB:
11788a6b1bc7SGavin Shan 			/* Mark the PE in fenced state */
11798a6b1bc7SGavin Shan 			eeh_serialize_lock(&flags);
11807e4e7867SGavin Shan 
11817e4e7867SGavin Shan 			/* Purge all events of the PHB */
11825c7a35e3SGavin Shan 			eeh_remove_event(pe, true);
11837e4e7867SGavin Shan 
1184e762bb89SSam Bobroff 			if (rc != EEH_NEXT_ERR_DEAD_PHB)
1185e762bb89SSam Bobroff 				eeh_pe_state_mark(pe, EEH_PE_RECOVERING);
1186e762bb89SSam Bobroff 			eeh_pe_mark_isolated(pe);
11877e4e7867SGavin Shan 
11888a6b1bc7SGavin Shan 			eeh_serialize_unlock(flags);
11898a6b1bc7SGavin Shan 
11908a6b1bc7SGavin Shan 			break;
11917e4e7867SGavin Shan 		case EEH_NEXT_ERR_NONE:
11927e4e7867SGavin Shan 			return;
11938a6b1bc7SGavin Shan 		default:
11947e4e7867SGavin Shan 			pr_warn("%s: Invalid value %d from next_error()\n",
11958a6b1bc7SGavin Shan 				__func__, rc);
11968a6b1bc7SGavin Shan 			return;
11978a6b1bc7SGavin Shan 		}
11988a6b1bc7SGavin Shan 
11998a6b1bc7SGavin Shan 		/*
12008a6b1bc7SGavin Shan 		 * For fenced PHB and frozen PE, it's handled as normal
12018a6b1bc7SGavin Shan 		 * event. We have to remove the affected PHBs for dead
12028a6b1bc7SGavin Shan 		 * PHB and IOC
12038a6b1bc7SGavin Shan 		 */
12047e4e7867SGavin Shan 		if (rc == EEH_NEXT_ERR_FROZEN_PE ||
12057e4e7867SGavin Shan 		    rc == EEH_NEXT_ERR_FENCED_PHB) {
1206799abe28SOliver O'Halloran 			eeh_pe_state_mark(pe, EEH_PE_RECOVERING);
120737fd8125SSam Bobroff 			eeh_handle_normal_event(pe);
12087e4e7867SGavin Shan 		} else {
12091c2042c8SRafael J. Wysocki 			pci_lock_rescan_remove();
12107e4e7867SGavin Shan 			list_for_each_entry(hose, &hose_list, list_node) {
12118a6b1bc7SGavin Shan 				phb_pe = eeh_phb_pe_get(hose);
12127e4e7867SGavin Shan 				if (!phb_pe ||
12139e049375SGavin Shan 				    !(phb_pe->state & EEH_PE_ISOLATED) ||
12149e049375SGavin Shan 				    (phb_pe->state & EEH_PE_RECOVERING))
12158a6b1bc7SGavin Shan 					continue;
12168a6b1bc7SGavin Shan 
1217aa06e3d6SSam Bobroff 				eeh_for_each_pe(pe, tmp_pe)
1218aa06e3d6SSam Bobroff 					eeh_pe_for_each_dev(tmp_pe, edev, tmp_edev)
1219aa06e3d6SSam Bobroff 						edev->mode &= ~EEH_DEV_NO_HANDLER;
1220aa06e3d6SSam Bobroff 
12217e4e7867SGavin Shan 				/* Notify all devices to be down */
12229ed5ca66SSam Bobroff 				eeh_pe_state_clear(pe, EEH_PE_PRI_BUS, true);
122347cc8c1cSSam Bobroff 				eeh_set_channel_state(pe, pci_channel_io_perm_failure);
122420b34497SSam Bobroff 				eeh_pe_report(
122520b34497SSam Bobroff 					"error_detected(permanent failure)", pe,
1226af2e3a00SRussell Currey 					eeh_report_failure, NULL);
12278a6b1bc7SGavin Shan 				bus = eeh_pe_bus_get(phb_pe);
122804fec21cSRussell Currey 				if (!bus) {
122904fec21cSRussell Currey 					pr_err("%s: Cannot find PCI bus for "
12301f52f176SRussell Currey 					       "PHB#%x-PE#%x\n",
123104fec21cSRussell Currey 					       __func__,
123204fec21cSRussell Currey 					       pe->phb->global_number,
123304fec21cSRussell Currey 					       pe->addr);
123404fec21cSRussell Currey 					break;
123504fec21cSRussell Currey 				}
1236bd251b89SGavin Shan 				pci_hp_remove_devices(bus);
12378a6b1bc7SGavin Shan 			}
12381c2042c8SRafael J. Wysocki 			pci_unlock_rescan_remove();
12398a6b1bc7SGavin Shan 		}
12407e4e7867SGavin Shan 
12417e4e7867SGavin Shan 		/*
12427e4e7867SGavin Shan 		 * If we have detected dead IOC, we needn't proceed
12437e4e7867SGavin Shan 		 * any more since all PHBs would have been removed
12447e4e7867SGavin Shan 		 */
12457e4e7867SGavin Shan 		if (rc == EEH_NEXT_ERR_DEAD_IOC)
12467e4e7867SGavin Shan 			break;
12477e4e7867SGavin Shan 	} while (rc != EEH_NEXT_ERR_NONE);
12488a6b1bc7SGavin Shan }
1249