xref: /openbmc/linux/arch/powerpc/kernel/eeh_driver.c (revision af2e3a00)
1317f06deSGavin Shan /*
2317f06deSGavin Shan  * PCI Error Recovery Driver for RPA-compliant PPC64 platform.
3317f06deSGavin Shan  * Copyright IBM Corp. 2004 2005
4317f06deSGavin Shan  * Copyright Linas Vepstas <linas@linas.org> 2004, 2005
5317f06deSGavin Shan  *
6317f06deSGavin Shan  * All rights reserved.
7317f06deSGavin Shan  *
8317f06deSGavin Shan  * This program is free software; you can redistribute it and/or modify
9317f06deSGavin Shan  * it under the terms of the GNU General Public License as published by
10317f06deSGavin Shan  * the Free Software Foundation; either version 2 of the License, or (at
11317f06deSGavin Shan  * your option) any later version.
12317f06deSGavin Shan  *
13317f06deSGavin Shan  * This program is distributed in the hope that it will be useful, but
14317f06deSGavin Shan  * WITHOUT ANY WARRANTY; without even the implied warranty of
15317f06deSGavin Shan  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
16317f06deSGavin Shan  * NON INFRINGEMENT.  See the GNU General Public License for more
17317f06deSGavin Shan  * details.
18317f06deSGavin Shan  *
19317f06deSGavin Shan  * You should have received a copy of the GNU General Public License
20317f06deSGavin Shan  * along with this program; if not, write to the Free Software
21317f06deSGavin Shan  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22317f06deSGavin Shan  *
23317f06deSGavin Shan  * Send comments and feedback to Linas Vepstas <linas@austin.ibm.com>
24317f06deSGavin Shan  */
25317f06deSGavin Shan #include <linux/delay.h>
26317f06deSGavin Shan #include <linux/interrupt.h>
27317f06deSGavin Shan #include <linux/irq.h>
28317f06deSGavin Shan #include <linux/module.h>
29317f06deSGavin Shan #include <linux/pci.h>
30317f06deSGavin Shan #include <asm/eeh.h>
31317f06deSGavin Shan #include <asm/eeh_event.h>
32317f06deSGavin Shan #include <asm/ppc-pci.h>
33317f06deSGavin Shan #include <asm/pci-bridge.h>
34317f06deSGavin Shan #include <asm/prom.h>
35317f06deSGavin Shan #include <asm/rtas.h>
36317f06deSGavin Shan 
3767086e32SWei Yang struct eeh_rmv_data {
3867086e32SWei Yang 	struct list_head edev_list;
3967086e32SWei Yang 	int removed;
4067086e32SWei Yang };
4167086e32SWei Yang 
42317f06deSGavin Shan /**
43317f06deSGavin Shan  * eeh_pcid_name - Retrieve name of PCI device driver
44317f06deSGavin Shan  * @pdev: PCI device
45317f06deSGavin Shan  *
46317f06deSGavin Shan  * This routine is used to retrieve the name of PCI device driver
47317f06deSGavin Shan  * if that's valid.
48317f06deSGavin Shan  */
49317f06deSGavin Shan static inline const char *eeh_pcid_name(struct pci_dev *pdev)
50317f06deSGavin Shan {
51317f06deSGavin Shan 	if (pdev && pdev->dev.driver)
52317f06deSGavin Shan 		return pdev->dev.driver->name;
53317f06deSGavin Shan 	return "";
54317f06deSGavin Shan }
55317f06deSGavin Shan 
56317f06deSGavin Shan /**
57317f06deSGavin Shan  * eeh_pcid_get - Get the PCI device driver
58317f06deSGavin Shan  * @pdev: PCI device
59317f06deSGavin Shan  *
60317f06deSGavin Shan  * The function is used to retrieve the PCI device driver for
61317f06deSGavin Shan  * the indicated PCI device. Besides, we will increase the reference
62317f06deSGavin Shan  * of the PCI device driver to prevent that being unloaded on
63317f06deSGavin Shan  * the fly. Otherwise, kernel crash would be seen.
64317f06deSGavin Shan  */
65317f06deSGavin Shan static inline struct pci_driver *eeh_pcid_get(struct pci_dev *pdev)
66317f06deSGavin Shan {
67317f06deSGavin Shan 	if (!pdev || !pdev->driver)
68317f06deSGavin Shan 		return NULL;
69317f06deSGavin Shan 
70317f06deSGavin Shan 	if (!try_module_get(pdev->driver->driver.owner))
71317f06deSGavin Shan 		return NULL;
72317f06deSGavin Shan 
73317f06deSGavin Shan 	return pdev->driver;
74317f06deSGavin Shan }
75317f06deSGavin Shan 
76317f06deSGavin Shan /**
77317f06deSGavin Shan  * eeh_pcid_put - Dereference on the PCI device driver
78317f06deSGavin Shan  * @pdev: PCI device
79317f06deSGavin Shan  *
80317f06deSGavin Shan  * The function is called to do dereference on the PCI device
81317f06deSGavin Shan  * driver of the indicated PCI device.
82317f06deSGavin Shan  */
83317f06deSGavin Shan static inline void eeh_pcid_put(struct pci_dev *pdev)
84317f06deSGavin Shan {
85317f06deSGavin Shan 	if (!pdev || !pdev->driver)
86317f06deSGavin Shan 		return;
87317f06deSGavin Shan 
88317f06deSGavin Shan 	module_put(pdev->driver->driver.owner);
89317f06deSGavin Shan }
90317f06deSGavin Shan 
91317f06deSGavin Shan /**
92317f06deSGavin Shan  * eeh_disable_irq - Disable interrupt for the recovering device
93317f06deSGavin Shan  * @dev: PCI device
94317f06deSGavin Shan  *
95317f06deSGavin Shan  * This routine must be called when reporting temporary or permanent
96317f06deSGavin Shan  * error to the particular PCI device to disable interrupt of that
97317f06deSGavin Shan  * device. If the device has enabled MSI or MSI-X interrupt, we needn't
98317f06deSGavin Shan  * do real work because EEH should freeze DMA transfers for those PCI
99317f06deSGavin Shan  * devices encountering EEH errors, which includes MSI or MSI-X.
100317f06deSGavin Shan  */
101317f06deSGavin Shan static void eeh_disable_irq(struct pci_dev *dev)
102317f06deSGavin Shan {
103317f06deSGavin Shan 	struct eeh_dev *edev = pci_dev_to_eeh_dev(dev);
104317f06deSGavin Shan 
105317f06deSGavin Shan 	/* Don't disable MSI and MSI-X interrupts. They are
106317f06deSGavin Shan 	 * effectively disabled by the DMA Stopped state
107317f06deSGavin Shan 	 * when an EEH error occurs.
108317f06deSGavin Shan 	 */
109317f06deSGavin Shan 	if (dev->msi_enabled || dev->msix_enabled)
110317f06deSGavin Shan 		return;
111317f06deSGavin Shan 
112317f06deSGavin Shan 	if (!irq_has_action(dev->irq))
113317f06deSGavin Shan 		return;
114317f06deSGavin Shan 
115317f06deSGavin Shan 	edev->mode |= EEH_DEV_IRQ_DISABLED;
116317f06deSGavin Shan 	disable_irq_nosync(dev->irq);
117317f06deSGavin Shan }
118317f06deSGavin Shan 
119317f06deSGavin Shan /**
120317f06deSGavin Shan  * eeh_enable_irq - Enable interrupt for the recovering device
121317f06deSGavin Shan  * @dev: PCI device
122317f06deSGavin Shan  *
123317f06deSGavin Shan  * This routine must be called to enable interrupt while failed
124317f06deSGavin Shan  * device could be resumed.
125317f06deSGavin Shan  */
126317f06deSGavin Shan static void eeh_enable_irq(struct pci_dev *dev)
127317f06deSGavin Shan {
128317f06deSGavin Shan 	struct eeh_dev *edev = pci_dev_to_eeh_dev(dev);
129317f06deSGavin Shan 
130317f06deSGavin Shan 	if ((edev->mode) & EEH_DEV_IRQ_DISABLED) {
131317f06deSGavin Shan 		edev->mode &= ~EEH_DEV_IRQ_DISABLED;
132b8a9a11bSThomas Gleixner 		/*
133b8a9a11bSThomas Gleixner 		 * FIXME !!!!!
134b8a9a11bSThomas Gleixner 		 *
135b8a9a11bSThomas Gleixner 		 * This is just ass backwards. This maze has
136b8a9a11bSThomas Gleixner 		 * unbalanced irq_enable/disable calls. So instead of
137b8a9a11bSThomas Gleixner 		 * finding the root cause it works around the warning
138b8a9a11bSThomas Gleixner 		 * in the irq_enable code by conditionally calling
139b8a9a11bSThomas Gleixner 		 * into it.
140b8a9a11bSThomas Gleixner 		 *
141b8a9a11bSThomas Gleixner 		 * That's just wrong.The warning in the core code is
142027dfac6SMichael Ellerman 		 * there to tell people to fix their asymmetries in
143b8a9a11bSThomas Gleixner 		 * their own code, not by abusing the core information
144b8a9a11bSThomas Gleixner 		 * to avoid it.
145b8a9a11bSThomas Gleixner 		 *
146b8a9a11bSThomas Gleixner 		 * I so wish that the assymetry would be the other way
147b8a9a11bSThomas Gleixner 		 * round and a few more irq_disable calls render that
148b8a9a11bSThomas Gleixner 		 * shit unusable forever.
149b8a9a11bSThomas Gleixner 		 *
150b8a9a11bSThomas Gleixner 		 *	tglx
151b8a9a11bSThomas Gleixner 		 */
15257310c3cSThomas Gleixner 		if (irqd_irq_disabled(irq_get_irq_data(dev->irq)))
153317f06deSGavin Shan 			enable_irq(dev->irq);
154317f06deSGavin Shan 	}
15557310c3cSThomas Gleixner }
156317f06deSGavin Shan 
157d2b0f6f7SGavin Shan static bool eeh_dev_removed(struct eeh_dev *edev)
158d2b0f6f7SGavin Shan {
159d2b0f6f7SGavin Shan 	/* EEH device removed ? */
160d2b0f6f7SGavin Shan 	if (!edev || (edev->mode & EEH_DEV_REMOVED))
161d2b0f6f7SGavin Shan 		return true;
162d2b0f6f7SGavin Shan 
163d2b0f6f7SGavin Shan 	return false;
164d2b0f6f7SGavin Shan }
165d2b0f6f7SGavin Shan 
1665cfb20b9SGavin Shan static void *eeh_dev_save_state(void *data, void *userdata)
1675cfb20b9SGavin Shan {
1685cfb20b9SGavin Shan 	struct eeh_dev *edev = data;
1695cfb20b9SGavin Shan 	struct pci_dev *pdev;
1705cfb20b9SGavin Shan 
1715cfb20b9SGavin Shan 	if (!edev)
1725cfb20b9SGavin Shan 		return NULL;
1735cfb20b9SGavin Shan 
1745a0cdbfdSGavin Shan 	/*
1755a0cdbfdSGavin Shan 	 * We cannot access the config space on some adapters.
1765a0cdbfdSGavin Shan 	 * Otherwise, it will cause fenced PHB. We don't save
1775a0cdbfdSGavin Shan 	 * the content in their config space and will restore
1785a0cdbfdSGavin Shan 	 * from the initial config space saved when the EEH
1795a0cdbfdSGavin Shan 	 * device is created.
1805a0cdbfdSGavin Shan 	 */
1815a0cdbfdSGavin Shan 	if (edev->pe && (edev->pe->state & EEH_PE_CFG_RESTRICTED))
1825a0cdbfdSGavin Shan 		return NULL;
1835a0cdbfdSGavin Shan 
1845cfb20b9SGavin Shan 	pdev = eeh_dev_to_pci_dev(edev);
1855cfb20b9SGavin Shan 	if (!pdev)
1865cfb20b9SGavin Shan 		return NULL;
1875cfb20b9SGavin Shan 
1885cfb20b9SGavin Shan 	pci_save_state(pdev);
1895cfb20b9SGavin Shan 	return NULL;
1905cfb20b9SGavin Shan }
1915cfb20b9SGavin Shan 
192317f06deSGavin Shan /**
193317f06deSGavin Shan  * eeh_report_error - Report pci error to each device driver
194317f06deSGavin Shan  * @data: eeh device
195317f06deSGavin Shan  * @userdata: return value
196317f06deSGavin Shan  *
197317f06deSGavin Shan  * Report an EEH error to each device driver, collect up and
198317f06deSGavin Shan  * merge the device driver responses. Cumulative response
199317f06deSGavin Shan  * passed back in "userdata".
200317f06deSGavin Shan  */
201317f06deSGavin Shan static void *eeh_report_error(void *data, void *userdata)
202317f06deSGavin Shan {
203317f06deSGavin Shan 	struct eeh_dev *edev = (struct eeh_dev *)data;
204317f06deSGavin Shan 	struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
205317f06deSGavin Shan 	enum pci_ers_result rc, *res = userdata;
206317f06deSGavin Shan 	struct pci_driver *driver;
207317f06deSGavin Shan 
2082311cca5SGavin Shan 	if (!dev || eeh_dev_removed(edev) || eeh_pe_passed(edev->pe))
209d2b0f6f7SGavin Shan 		return NULL;
210317f06deSGavin Shan 	dev->error_state = pci_channel_io_frozen;
211317f06deSGavin Shan 
212317f06deSGavin Shan 	driver = eeh_pcid_get(dev);
213317f06deSGavin Shan 	if (!driver) return NULL;
214317f06deSGavin Shan 
215317f06deSGavin Shan 	eeh_disable_irq(dev);
216317f06deSGavin Shan 
217317f06deSGavin Shan 	if (!driver->err_handler ||
218317f06deSGavin Shan 	    !driver->err_handler->error_detected) {
219317f06deSGavin Shan 		eeh_pcid_put(dev);
220317f06deSGavin Shan 		return NULL;
221317f06deSGavin Shan 	}
222317f06deSGavin Shan 
223317f06deSGavin Shan 	rc = driver->err_handler->error_detected(dev, pci_channel_io_frozen);
224317f06deSGavin Shan 
225317f06deSGavin Shan 	/* A driver that needs a reset trumps all others */
226317f06deSGavin Shan 	if (rc == PCI_ERS_RESULT_NEED_RESET) *res = rc;
227317f06deSGavin Shan 	if (*res == PCI_ERS_RESULT_NONE) *res = rc;
228317f06deSGavin Shan 
22967086e32SWei Yang 	edev->in_error = true;
230317f06deSGavin Shan 	eeh_pcid_put(dev);
231317f06deSGavin Shan 	return NULL;
232317f06deSGavin Shan }
233317f06deSGavin Shan 
234317f06deSGavin Shan /**
235317f06deSGavin Shan  * eeh_report_mmio_enabled - Tell drivers that MMIO has been enabled
236317f06deSGavin Shan  * @data: eeh device
237317f06deSGavin Shan  * @userdata: return value
238317f06deSGavin Shan  *
239317f06deSGavin Shan  * Tells each device driver that IO ports, MMIO and config space I/O
240317f06deSGavin Shan  * are now enabled. Collects up and merges the device driver responses.
241317f06deSGavin Shan  * Cumulative response passed back in "userdata".
242317f06deSGavin Shan  */
243317f06deSGavin Shan static void *eeh_report_mmio_enabled(void *data, void *userdata)
244317f06deSGavin Shan {
245317f06deSGavin Shan 	struct eeh_dev *edev = (struct eeh_dev *)data;
246317f06deSGavin Shan 	struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
247317f06deSGavin Shan 	enum pci_ers_result rc, *res = userdata;
248317f06deSGavin Shan 	struct pci_driver *driver;
249317f06deSGavin Shan 
2502311cca5SGavin Shan 	if (!dev || eeh_dev_removed(edev) || eeh_pe_passed(edev->pe))
251d2b0f6f7SGavin Shan 		return NULL;
252d2b0f6f7SGavin Shan 
253317f06deSGavin Shan 	driver = eeh_pcid_get(dev);
254317f06deSGavin Shan 	if (!driver) return NULL;
255317f06deSGavin Shan 
256317f06deSGavin Shan 	if (!driver->err_handler ||
257f26c7a03SGavin Shan 	    !driver->err_handler->mmio_enabled ||
258f26c7a03SGavin Shan 	    (edev->mode & EEH_DEV_NO_HANDLER)) {
259317f06deSGavin Shan 		eeh_pcid_put(dev);
260317f06deSGavin Shan 		return NULL;
261317f06deSGavin Shan 	}
262317f06deSGavin Shan 
263317f06deSGavin Shan 	rc = driver->err_handler->mmio_enabled(dev);
264317f06deSGavin Shan 
265317f06deSGavin Shan 	/* A driver that needs a reset trumps all others */
266317f06deSGavin Shan 	if (rc == PCI_ERS_RESULT_NEED_RESET) *res = rc;
267317f06deSGavin Shan 	if (*res == PCI_ERS_RESULT_NONE) *res = rc;
268317f06deSGavin Shan 
269317f06deSGavin Shan 	eeh_pcid_put(dev);
270317f06deSGavin Shan 	return NULL;
271317f06deSGavin Shan }
272317f06deSGavin Shan 
273317f06deSGavin Shan /**
274317f06deSGavin Shan  * eeh_report_reset - Tell device that slot has been reset
275317f06deSGavin Shan  * @data: eeh device
276317f06deSGavin Shan  * @userdata: return value
277317f06deSGavin Shan  *
278317f06deSGavin Shan  * This routine must be called while EEH tries to reset particular
279317f06deSGavin Shan  * PCI device so that the associated PCI device driver could take
280317f06deSGavin Shan  * some actions, usually to save data the driver needs so that the
281317f06deSGavin Shan  * driver can work again while the device is recovered.
282317f06deSGavin Shan  */
283317f06deSGavin Shan static void *eeh_report_reset(void *data, void *userdata)
284317f06deSGavin Shan {
285317f06deSGavin Shan 	struct eeh_dev *edev = (struct eeh_dev *)data;
286317f06deSGavin Shan 	struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
287317f06deSGavin Shan 	enum pci_ers_result rc, *res = userdata;
288317f06deSGavin Shan 	struct pci_driver *driver;
289317f06deSGavin Shan 
2902311cca5SGavin Shan 	if (!dev || eeh_dev_removed(edev) || eeh_pe_passed(edev->pe))
291d2b0f6f7SGavin Shan 		return NULL;
292317f06deSGavin Shan 	dev->error_state = pci_channel_io_normal;
293317f06deSGavin Shan 
294317f06deSGavin Shan 	driver = eeh_pcid_get(dev);
295317f06deSGavin Shan 	if (!driver) return NULL;
296317f06deSGavin Shan 
297317f06deSGavin Shan 	eeh_enable_irq(dev);
298317f06deSGavin Shan 
299317f06deSGavin Shan 	if (!driver->err_handler ||
300f26c7a03SGavin Shan 	    !driver->err_handler->slot_reset ||
30167086e32SWei Yang 	    (edev->mode & EEH_DEV_NO_HANDLER) ||
30267086e32SWei Yang 	    (!edev->in_error)) {
303317f06deSGavin Shan 		eeh_pcid_put(dev);
304317f06deSGavin Shan 		return NULL;
305317f06deSGavin Shan 	}
306317f06deSGavin Shan 
307317f06deSGavin Shan 	rc = driver->err_handler->slot_reset(dev);
308317f06deSGavin Shan 	if ((*res == PCI_ERS_RESULT_NONE) ||
309317f06deSGavin Shan 	    (*res == PCI_ERS_RESULT_RECOVERED)) *res = rc;
310317f06deSGavin Shan 	if (*res == PCI_ERS_RESULT_DISCONNECT &&
311317f06deSGavin Shan 	     rc == PCI_ERS_RESULT_NEED_RESET) *res = rc;
312317f06deSGavin Shan 
313317f06deSGavin Shan 	eeh_pcid_put(dev);
314317f06deSGavin Shan 	return NULL;
315317f06deSGavin Shan }
316317f06deSGavin Shan 
3175cfb20b9SGavin Shan static void *eeh_dev_restore_state(void *data, void *userdata)
3185cfb20b9SGavin Shan {
3195cfb20b9SGavin Shan 	struct eeh_dev *edev = data;
3205cfb20b9SGavin Shan 	struct pci_dev *pdev;
3215cfb20b9SGavin Shan 
3225cfb20b9SGavin Shan 	if (!edev)
3235cfb20b9SGavin Shan 		return NULL;
3245cfb20b9SGavin Shan 
3255a0cdbfdSGavin Shan 	/*
3265a0cdbfdSGavin Shan 	 * The content in the config space isn't saved because
3275a0cdbfdSGavin Shan 	 * the blocked config space on some adapters. We have
3285a0cdbfdSGavin Shan 	 * to restore the initial saved config space when the
3295a0cdbfdSGavin Shan 	 * EEH device is created.
3305a0cdbfdSGavin Shan 	 */
3315a0cdbfdSGavin Shan 	if (edev->pe && (edev->pe->state & EEH_PE_CFG_RESTRICTED)) {
3325a0cdbfdSGavin Shan 		if (list_is_last(&edev->list, &edev->pe->edevs))
3335a0cdbfdSGavin Shan 			eeh_pe_restore_bars(edev->pe);
3345a0cdbfdSGavin Shan 
3355a0cdbfdSGavin Shan 		return NULL;
3365a0cdbfdSGavin Shan 	}
3375a0cdbfdSGavin Shan 
3385cfb20b9SGavin Shan 	pdev = eeh_dev_to_pci_dev(edev);
3395cfb20b9SGavin Shan 	if (!pdev)
3405cfb20b9SGavin Shan 		return NULL;
3415cfb20b9SGavin Shan 
3425cfb20b9SGavin Shan 	pci_restore_state(pdev);
3435cfb20b9SGavin Shan 	return NULL;
3445cfb20b9SGavin Shan }
3455cfb20b9SGavin Shan 
346317f06deSGavin Shan /**
347317f06deSGavin Shan  * eeh_report_resume - Tell device to resume normal operations
348317f06deSGavin Shan  * @data: eeh device
349317f06deSGavin Shan  * @userdata: return value
350317f06deSGavin Shan  *
351317f06deSGavin Shan  * This routine must be called to notify the device driver that it
352317f06deSGavin Shan  * could resume so that the device driver can do some initialization
353317f06deSGavin Shan  * to make the recovered device work again.
354317f06deSGavin Shan  */
355317f06deSGavin Shan static void *eeh_report_resume(void *data, void *userdata)
356317f06deSGavin Shan {
357317f06deSGavin Shan 	struct eeh_dev *edev = (struct eeh_dev *)data;
358317f06deSGavin Shan 	struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
35967086e32SWei Yang 	bool was_in_error;
360317f06deSGavin Shan 	struct pci_driver *driver;
361317f06deSGavin Shan 
3622311cca5SGavin Shan 	if (!dev || eeh_dev_removed(edev) || eeh_pe_passed(edev->pe))
363d2b0f6f7SGavin Shan 		return NULL;
364317f06deSGavin Shan 	dev->error_state = pci_channel_io_normal;
365317f06deSGavin Shan 
366317f06deSGavin Shan 	driver = eeh_pcid_get(dev);
367317f06deSGavin Shan 	if (!driver) return NULL;
368317f06deSGavin Shan 
36967086e32SWei Yang 	was_in_error = edev->in_error;
37067086e32SWei Yang 	edev->in_error = false;
371317f06deSGavin Shan 	eeh_enable_irq(dev);
372317f06deSGavin Shan 
373317f06deSGavin Shan 	if (!driver->err_handler ||
374f26c7a03SGavin Shan 	    !driver->err_handler->resume ||
37567086e32SWei Yang 	    (edev->mode & EEH_DEV_NO_HANDLER) || !was_in_error) {
376f26c7a03SGavin Shan 		edev->mode &= ~EEH_DEV_NO_HANDLER;
377317f06deSGavin Shan 		eeh_pcid_put(dev);
378317f06deSGavin Shan 		return NULL;
379317f06deSGavin Shan 	}
380317f06deSGavin Shan 
381317f06deSGavin Shan 	driver->err_handler->resume(dev);
382317f06deSGavin Shan 
383317f06deSGavin Shan 	eeh_pcid_put(dev);
384317f06deSGavin Shan 	return NULL;
385317f06deSGavin Shan }
386317f06deSGavin Shan 
387317f06deSGavin Shan /**
388317f06deSGavin Shan  * eeh_report_failure - Tell device driver that device is dead.
389317f06deSGavin Shan  * @data: eeh device
390317f06deSGavin Shan  * @userdata: return value
391317f06deSGavin Shan  *
392317f06deSGavin Shan  * This informs the device driver that the device is permanently
393317f06deSGavin Shan  * dead, and that no further recovery attempts will be made on it.
394317f06deSGavin Shan  */
395317f06deSGavin Shan static void *eeh_report_failure(void *data, void *userdata)
396317f06deSGavin Shan {
397317f06deSGavin Shan 	struct eeh_dev *edev = (struct eeh_dev *)data;
398317f06deSGavin Shan 	struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
399317f06deSGavin Shan 	struct pci_driver *driver;
400317f06deSGavin Shan 
4012311cca5SGavin Shan 	if (!dev || eeh_dev_removed(edev) || eeh_pe_passed(edev->pe))
402d2b0f6f7SGavin Shan 		return NULL;
403317f06deSGavin Shan 	dev->error_state = pci_channel_io_perm_failure;
404317f06deSGavin Shan 
405317f06deSGavin Shan 	driver = eeh_pcid_get(dev);
406317f06deSGavin Shan 	if (!driver) return NULL;
407317f06deSGavin Shan 
408317f06deSGavin Shan 	eeh_disable_irq(dev);
409317f06deSGavin Shan 
410317f06deSGavin Shan 	if (!driver->err_handler ||
411317f06deSGavin Shan 	    !driver->err_handler->error_detected) {
412317f06deSGavin Shan 		eeh_pcid_put(dev);
413317f06deSGavin Shan 		return NULL;
414317f06deSGavin Shan 	}
415317f06deSGavin Shan 
416317f06deSGavin Shan 	driver->err_handler->error_detected(dev, pci_channel_io_perm_failure);
417317f06deSGavin Shan 
418317f06deSGavin Shan 	eeh_pcid_put(dev);
419317f06deSGavin Shan 	return NULL;
420317f06deSGavin Shan }
421317f06deSGavin Shan 
42267086e32SWei Yang static void *eeh_add_virt_device(void *data, void *userdata)
42367086e32SWei Yang {
42467086e32SWei Yang 	struct pci_driver *driver;
42567086e32SWei Yang 	struct eeh_dev *edev = (struct eeh_dev *)data;
42667086e32SWei Yang 	struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
42767086e32SWei Yang 	struct pci_dn *pdn = eeh_dev_to_pdn(edev);
42867086e32SWei Yang 
42967086e32SWei Yang 	if (!(edev->physfn)) {
43067086e32SWei Yang 		pr_warn("%s: EEH dev %04x:%02x:%02x.%01x not for VF\n",
43167086e32SWei Yang 			__func__, edev->phb->global_number, pdn->busno,
43267086e32SWei Yang 			PCI_SLOT(pdn->devfn), PCI_FUNC(pdn->devfn));
43367086e32SWei Yang 		return NULL;
43467086e32SWei Yang 	}
43567086e32SWei Yang 
43667086e32SWei Yang 	driver = eeh_pcid_get(dev);
43767086e32SWei Yang 	if (driver) {
43867086e32SWei Yang 		eeh_pcid_put(dev);
43967086e32SWei Yang 		if (driver->err_handler)
44067086e32SWei Yang 			return NULL;
44167086e32SWei Yang 	}
44267086e32SWei Yang 
44367086e32SWei Yang #ifdef CONFIG_PPC_POWERNV
44467086e32SWei Yang 	pci_iov_add_virtfn(edev->physfn, pdn->vf_index, 0);
44567086e32SWei Yang #endif
44667086e32SWei Yang 	return NULL;
44767086e32SWei Yang }
44867086e32SWei Yang 
449f5c57710SGavin Shan static void *eeh_rmv_device(void *data, void *userdata)
450f5c57710SGavin Shan {
451f5c57710SGavin Shan 	struct pci_driver *driver;
452f5c57710SGavin Shan 	struct eeh_dev *edev = (struct eeh_dev *)data;
453f5c57710SGavin Shan 	struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
45467086e32SWei Yang 	struct eeh_rmv_data *rmv_data = (struct eeh_rmv_data *)userdata;
45567086e32SWei Yang 	int *removed = rmv_data ? &rmv_data->removed : NULL;
456f5c57710SGavin Shan 
457f5c57710SGavin Shan 	/*
458f5c57710SGavin Shan 	 * Actually, we should remove the PCI bridges as well.
459f5c57710SGavin Shan 	 * However, that's lots of complexity to do that,
460f5c57710SGavin Shan 	 * particularly some of devices under the bridge might
461f5c57710SGavin Shan 	 * support EEH. So we just care about PCI devices for
462f5c57710SGavin Shan 	 * simplicity here.
463f5c57710SGavin Shan 	 */
46493de6901SBjorn Helgaas 	if (!dev || (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE))
465f5c57710SGavin Shan 		return NULL;
4668cc6b6cdSThadeu Lima de Souza Cascardo 
467d2b0f6f7SGavin Shan 	/*
468d2b0f6f7SGavin Shan 	 * We rely on count-based pcibios_release_device() to
469d2b0f6f7SGavin Shan 	 * detach permanently offlined PEs. Unfortunately, that's
470d2b0f6f7SGavin Shan 	 * not reliable enough. We might have the permanently
471d2b0f6f7SGavin Shan 	 * offlined PEs attached, but we needn't take care of
472d2b0f6f7SGavin Shan 	 * them and their child devices.
473d2b0f6f7SGavin Shan 	 */
474d2b0f6f7SGavin Shan 	if (eeh_dev_removed(edev))
475d2b0f6f7SGavin Shan 		return NULL;
476d2b0f6f7SGavin Shan 
477f5c57710SGavin Shan 	driver = eeh_pcid_get(dev);
4788cc6b6cdSThadeu Lima de Souza Cascardo 	if (driver) {
4798cc6b6cdSThadeu Lima de Souza Cascardo 		eeh_pcid_put(dev);
48067086e32SWei Yang 		if (removed &&
4813fa7bf72SGavin Shan 		    eeh_pe_passed(edev->pe))
4823fa7bf72SGavin Shan 			return NULL;
4833fa7bf72SGavin Shan 		if (removed &&
48467086e32SWei Yang 		    driver->err_handler &&
485f2da4ccfSGavin Shan 		    driver->err_handler->error_detected &&
486f6bf0fa1SGavin Shan 		    driver->err_handler->slot_reset)
487f5c57710SGavin Shan 			return NULL;
4888cc6b6cdSThadeu Lima de Souza Cascardo 	}
489f5c57710SGavin Shan 
490f5c57710SGavin Shan 	/* Remove it from PCI subsystem */
491f5c57710SGavin Shan 	pr_debug("EEH: Removing %s without EEH sensitive driver\n",
492f5c57710SGavin Shan 		 pci_name(dev));
493f5c57710SGavin Shan 	edev->bus = dev->bus;
494f5c57710SGavin Shan 	edev->mode |= EEH_DEV_DISCONNECTED;
49567086e32SWei Yang 	if (removed)
496f5c57710SGavin Shan 		(*removed)++;
497f5c57710SGavin Shan 
49867086e32SWei Yang 	if (edev->physfn) {
49967086e32SWei Yang #ifdef CONFIG_PPC_POWERNV
50067086e32SWei Yang 		struct pci_dn *pdn = eeh_dev_to_pdn(edev);
50167086e32SWei Yang 
50267086e32SWei Yang 		pci_iov_remove_virtfn(edev->physfn, pdn->vf_index, 0);
50367086e32SWei Yang 		edev->pdev = NULL;
50467086e32SWei Yang 
50567086e32SWei Yang 		/*
50667086e32SWei Yang 		 * We have to set the VF PE number to invalid one, which is
50767086e32SWei Yang 		 * required to plug the VF successfully.
50867086e32SWei Yang 		 */
50967086e32SWei Yang 		pdn->pe_number = IODA_INVALID_PE;
51067086e32SWei Yang #endif
51167086e32SWei Yang 		if (rmv_data)
51267086e32SWei Yang 			list_add(&edev->rmv_list, &rmv_data->edev_list);
51367086e32SWei Yang 	} else {
5141c2042c8SRafael J. Wysocki 		pci_lock_rescan_remove();
515f5c57710SGavin Shan 		pci_stop_and_remove_bus_device(dev);
5161c2042c8SRafael J. Wysocki 		pci_unlock_rescan_remove();
51767086e32SWei Yang 	}
518f5c57710SGavin Shan 
519f5c57710SGavin Shan 	return NULL;
520f5c57710SGavin Shan }
521f5c57710SGavin Shan 
522f5c57710SGavin Shan static void *eeh_pe_detach_dev(void *data, void *userdata)
523f5c57710SGavin Shan {
524f5c57710SGavin Shan 	struct eeh_pe *pe = (struct eeh_pe *)data;
525f5c57710SGavin Shan 	struct eeh_dev *edev, *tmp;
526f5c57710SGavin Shan 
527f5c57710SGavin Shan 	eeh_pe_for_each_dev(pe, edev, tmp) {
528f5c57710SGavin Shan 		if (!(edev->mode & EEH_DEV_DISCONNECTED))
529f5c57710SGavin Shan 			continue;
530f5c57710SGavin Shan 
531f5c57710SGavin Shan 		edev->mode &= ~(EEH_DEV_DISCONNECTED | EEH_DEV_IRQ_DISABLED);
532f5c57710SGavin Shan 		eeh_rmv_from_parent_pe(edev);
533f5c57710SGavin Shan 	}
534f5c57710SGavin Shan 
535f5c57710SGavin Shan 	return NULL;
536f5c57710SGavin Shan }
537f5c57710SGavin Shan 
53878954700SGavin Shan /*
53978954700SGavin Shan  * Explicitly clear PE's frozen state for PowerNV where
54078954700SGavin Shan  * we have frozen PE until BAR restore is completed. It's
54178954700SGavin Shan  * harmless to clear it for pSeries. To be consistent with
54278954700SGavin Shan  * PE reset (for 3 times), we try to clear the frozen state
54378954700SGavin Shan  * for 3 times as well.
54478954700SGavin Shan  */
5452c665992SGavin Shan static void *__eeh_clear_pe_frozen_state(void *data, void *flag)
54678954700SGavin Shan {
5472c665992SGavin Shan 	struct eeh_pe *pe = (struct eeh_pe *)data;
5485cfb20b9SGavin Shan 	bool *clear_sw_state = flag;
549c9dd0143SGavin Shan 	int i, rc = 1;
55078954700SGavin Shan 
551c9dd0143SGavin Shan 	for (i = 0; rc && i < 3; i++)
5525cfb20b9SGavin Shan 		rc = eeh_unfreeze_pe(pe, clear_sw_state);
55378954700SGavin Shan 
554c9dd0143SGavin Shan 	/* Stop immediately on any errors */
5552c665992SGavin Shan 	if (rc) {
556c9dd0143SGavin Shan 		pr_warn("%s: Failure %d unfreezing PHB#%x-PE#%x\n",
557c9dd0143SGavin Shan 			__func__, rc, pe->phb->global_number, pe->addr);
5582c665992SGavin Shan 		return (void *)pe;
5592c665992SGavin Shan 	}
5602c665992SGavin Shan 
5612c665992SGavin Shan 	return NULL;
5622c665992SGavin Shan }
5632c665992SGavin Shan 
5645cfb20b9SGavin Shan static int eeh_clear_pe_frozen_state(struct eeh_pe *pe,
5655cfb20b9SGavin Shan 				     bool clear_sw_state)
5662c665992SGavin Shan {
5672c665992SGavin Shan 	void *rc;
5682c665992SGavin Shan 
5695cfb20b9SGavin Shan 	rc = eeh_pe_traverse(pe, __eeh_clear_pe_frozen_state, &clear_sw_state);
5702c665992SGavin Shan 	if (!rc)
57178954700SGavin Shan 		eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
57278954700SGavin Shan 
5732c665992SGavin Shan 	return rc ? -EIO : 0;
57478954700SGavin Shan }
57578954700SGavin Shan 
5765cfb20b9SGavin Shan int eeh_pe_reset_and_recover(struct eeh_pe *pe)
5775cfb20b9SGavin Shan {
5782efc771fSGavin Shan 	int ret;
5795cfb20b9SGavin Shan 
5805cfb20b9SGavin Shan 	/* Bail if the PE is being recovered */
5815cfb20b9SGavin Shan 	if (pe->state & EEH_PE_RECOVERING)
5825cfb20b9SGavin Shan 		return 0;
5835cfb20b9SGavin Shan 
5845cfb20b9SGavin Shan 	/* Put the PE into recovery mode */
5855cfb20b9SGavin Shan 	eeh_pe_state_mark(pe, EEH_PE_RECOVERING);
5865cfb20b9SGavin Shan 
5875cfb20b9SGavin Shan 	/* Save states */
5885cfb20b9SGavin Shan 	eeh_pe_dev_traverse(pe, eeh_dev_save_state, NULL);
5895cfb20b9SGavin Shan 
5905cfb20b9SGavin Shan 	/* Issue reset */
5915cfb20b9SGavin Shan 	ret = eeh_reset_pe(pe);
5925cfb20b9SGavin Shan 	if (ret) {
59328bf36f9SGavin Shan 		eeh_pe_state_clear(pe, EEH_PE_RECOVERING);
5945cfb20b9SGavin Shan 		return ret;
5955cfb20b9SGavin Shan 	}
5965cfb20b9SGavin Shan 
5975cfb20b9SGavin Shan 	/* Unfreeze the PE */
5985cfb20b9SGavin Shan 	ret = eeh_clear_pe_frozen_state(pe, true);
5995cfb20b9SGavin Shan 	if (ret) {
6005cfb20b9SGavin Shan 		eeh_pe_state_clear(pe, EEH_PE_RECOVERING);
6015cfb20b9SGavin Shan 		return ret;
6025cfb20b9SGavin Shan 	}
6035cfb20b9SGavin Shan 
6045cfb20b9SGavin Shan 	/* Restore device state */
6055cfb20b9SGavin Shan 	eeh_pe_dev_traverse(pe, eeh_dev_restore_state, NULL);
6065cfb20b9SGavin Shan 
6075cfb20b9SGavin Shan 	/* Clear recovery mode */
6085cfb20b9SGavin Shan 	eeh_pe_state_clear(pe, EEH_PE_RECOVERING);
6095cfb20b9SGavin Shan 
6105cfb20b9SGavin Shan 	return 0;
6115cfb20b9SGavin Shan }
6125cfb20b9SGavin Shan 
613317f06deSGavin Shan /**
614317f06deSGavin Shan  * eeh_reset_device - Perform actual reset of a pci slot
615317f06deSGavin Shan  * @pe: EEH PE
616317f06deSGavin Shan  * @bus: PCI bus corresponding to the isolcated slot
617317f06deSGavin Shan  *
618317f06deSGavin Shan  * This routine must be called to do reset on the indicated PE.
619317f06deSGavin Shan  * During the reset, udev might be invoked because those affected
620317f06deSGavin Shan  * PCI devices will be removed and then added.
621317f06deSGavin Shan  */
62267086e32SWei Yang static int eeh_reset_device(struct eeh_pe *pe, struct pci_bus *bus,
62367086e32SWei Yang 				struct eeh_rmv_data *rmv_data)
624317f06deSGavin Shan {
625f5c57710SGavin Shan 	struct pci_bus *frozen_bus = eeh_pe_bus_get(pe);
6265a71978eSGavin Shan 	struct timeval tstamp;
62767086e32SWei Yang 	int cnt, rc;
62867086e32SWei Yang 	struct eeh_dev *edev;
629317f06deSGavin Shan 
630317f06deSGavin Shan 	/* pcibios will clear the counter; save the value */
631317f06deSGavin Shan 	cnt = pe->freeze_count;
6325a71978eSGavin Shan 	tstamp = pe->tstamp;
633317f06deSGavin Shan 
634317f06deSGavin Shan 	/*
635317f06deSGavin Shan 	 * We don't remove the corresponding PE instances because
636317f06deSGavin Shan 	 * we need the information afterwords. The attached EEH
637317f06deSGavin Shan 	 * devices are expected to be attached soon when calling
638bd251b89SGavin Shan 	 * into pci_hp_add_devices().
639317f06deSGavin Shan 	 */
640807a827dSGavin Shan 	eeh_pe_state_mark(pe, EEH_PE_KEEP);
6411c2042c8SRafael J. Wysocki 	if (bus) {
64267086e32SWei Yang 		if (pe->type & EEH_PE_VF) {
64367086e32SWei Yang 			eeh_pe_dev_traverse(pe, eeh_rmv_device, NULL);
64467086e32SWei Yang 		} else {
6451c2042c8SRafael J. Wysocki 			pci_lock_rescan_remove();
646bd251b89SGavin Shan 			pci_hp_remove_devices(bus);
6471c2042c8SRafael J. Wysocki 			pci_unlock_rescan_remove();
64867086e32SWei Yang 		}
6491c2042c8SRafael J. Wysocki 	} else if (frozen_bus) {
650cca0e542SGavin Shan 		eeh_pe_dev_traverse(pe, eeh_rmv_device, rmv_data);
6511c2042c8SRafael J. Wysocki 	}
652317f06deSGavin Shan 
653d0914f50SGavin Shan 	/*
654d0914f50SGavin Shan 	 * Reset the pci controller. (Asserts RST#; resets config space).
655317f06deSGavin Shan 	 * Reconfigure bridges and devices. Don't try to bring the system
656317f06deSGavin Shan 	 * up if the reset failed for some reason.
657d0914f50SGavin Shan 	 *
658d0914f50SGavin Shan 	 * During the reset, it's very dangerous to have uncontrolled PCI
659d0914f50SGavin Shan 	 * config accesses. So we prefer to block them. However, controlled
660d0914f50SGavin Shan 	 * PCI config accesses initiated from EEH itself are allowed.
661317f06deSGavin Shan 	 */
662317f06deSGavin Shan 	rc = eeh_reset_pe(pe);
66328bf36f9SGavin Shan 	if (rc)
664317f06deSGavin Shan 		return rc;
665317f06deSGavin Shan 
6661c2042c8SRafael J. Wysocki 	pci_lock_rescan_remove();
6671c2042c8SRafael J. Wysocki 
668317f06deSGavin Shan 	/* Restore PE */
669317f06deSGavin Shan 	eeh_ops->configure_bridge(pe);
670317f06deSGavin Shan 	eeh_pe_restore_bars(pe);
671317f06deSGavin Shan 
672dc9c41bdSAndrew Donnellan 	/* Clear frozen state */
6735cfb20b9SGavin Shan 	rc = eeh_clear_pe_frozen_state(pe, false);
67478954700SGavin Shan 	if (rc)
67578954700SGavin Shan 		return rc;
67678954700SGavin Shan 
677317f06deSGavin Shan 	/* Give the system 5 seconds to finish running the user-space
678317f06deSGavin Shan 	 * hotplug shutdown scripts, e.g. ifdown for ethernet.  Yes,
679317f06deSGavin Shan 	 * this is a hack, but if we don't do this, and try to bring
680317f06deSGavin Shan 	 * the device up before the scripts have taken it down,
681317f06deSGavin Shan 	 * potentially weird things happen.
682317f06deSGavin Shan 	 */
683317f06deSGavin Shan 	if (bus) {
684f5c57710SGavin Shan 		pr_info("EEH: Sleep 5s ahead of complete hotplug\n");
685317f06deSGavin Shan 		ssleep(5);
686f5c57710SGavin Shan 
687f5c57710SGavin Shan 		/*
688f5c57710SGavin Shan 		 * The EEH device is still connected with its parent
689f5c57710SGavin Shan 		 * PE. We should disconnect it so the binding can be
690f5c57710SGavin Shan 		 * rebuilt when adding PCI devices.
691f5c57710SGavin Shan 		 */
69267086e32SWei Yang 		edev = list_first_entry(&pe->edevs, struct eeh_dev, list);
693f5c57710SGavin Shan 		eeh_pe_traverse(pe, eeh_pe_detach_dev, NULL);
694a3aa256bSGavin Shan 		if (pe->type & EEH_PE_VF) {
69567086e32SWei Yang 			eeh_add_virt_device(edev, NULL);
696a3aa256bSGavin Shan 		} else {
697a3aa256bSGavin Shan 			eeh_pe_state_clear(pe, EEH_PE_PRI_BUS);
698bd251b89SGavin Shan 			pci_hp_add_devices(bus);
699a3aa256bSGavin Shan 		}
70067086e32SWei Yang 	} else if (frozen_bus && rmv_data->removed) {
701f5c57710SGavin Shan 		pr_info("EEH: Sleep 5s ahead of partial hotplug\n");
702f5c57710SGavin Shan 		ssleep(5);
703f5c57710SGavin Shan 
70467086e32SWei Yang 		edev = list_first_entry(&pe->edevs, struct eeh_dev, list);
705f5c57710SGavin Shan 		eeh_pe_traverse(pe, eeh_pe_detach_dev, NULL);
70667086e32SWei Yang 		if (pe->type & EEH_PE_VF)
70767086e32SWei Yang 			eeh_add_virt_device(edev, NULL);
70867086e32SWei Yang 		else
709bd251b89SGavin Shan 			pci_hp_add_devices(frozen_bus);
710317f06deSGavin Shan 	}
711f5c57710SGavin Shan 	eeh_pe_state_clear(pe, EEH_PE_KEEP);
7125a71978eSGavin Shan 
7135a71978eSGavin Shan 	pe->tstamp = tstamp;
714317f06deSGavin Shan 	pe->freeze_count = cnt;
715317f06deSGavin Shan 
7161c2042c8SRafael J. Wysocki 	pci_unlock_rescan_remove();
717317f06deSGavin Shan 	return 0;
718317f06deSGavin Shan }
719317f06deSGavin Shan 
720317f06deSGavin Shan /* The longest amount of time to wait for a pci device
721317f06deSGavin Shan  * to come back on line, in seconds.
722317f06deSGavin Shan  */
723fb48dc22SBrian King #define MAX_WAIT_FOR_RECOVERY 300
724317f06deSGavin Shan 
7258a6b1bc7SGavin Shan static void eeh_handle_normal_event(struct eeh_pe *pe)
726317f06deSGavin Shan {
727317f06deSGavin Shan 	struct pci_bus *frozen_bus;
72867086e32SWei Yang 	struct eeh_dev *edev, *tmp;
729317f06deSGavin Shan 	int rc = 0;
730317f06deSGavin Shan 	enum pci_ers_result result = PCI_ERS_RESULT_NONE;
73167086e32SWei Yang 	struct eeh_rmv_data rmv_data = {LIST_HEAD_INIT(rmv_data.edev_list), 0};
732317f06deSGavin Shan 
733317f06deSGavin Shan 	frozen_bus = eeh_pe_bus_get(pe);
734317f06deSGavin Shan 	if (!frozen_bus) {
735317f06deSGavin Shan 		pr_err("%s: Cannot find PCI bus for PHB#%d-PE#%x\n",
736317f06deSGavin Shan 			__func__, pe->phb->global_number, pe->addr);
737317f06deSGavin Shan 		return;
738317f06deSGavin Shan 	}
739317f06deSGavin Shan 
7405a71978eSGavin Shan 	eeh_pe_update_time_stamp(pe);
741317f06deSGavin Shan 	pe->freeze_count++;
7421b28f170SGavin Shan 	if (pe->freeze_count > eeh_max_freezes)
743317f06deSGavin Shan 		goto excess_failures;
7440dae2743SGavin Shan 	pr_warn("EEH: This PCI device has failed %d times in the last hour\n",
745317f06deSGavin Shan 		pe->freeze_count);
746317f06deSGavin Shan 
747317f06deSGavin Shan 	/* Walk the various device drivers attached to this slot through
748317f06deSGavin Shan 	 * a reset sequence, giving each an opportunity to do what it needs
749317f06deSGavin Shan 	 * to accomplish the reset.  Each child gets a report of the
750317f06deSGavin Shan 	 * status ... if any child can't handle the reset, then the entire
751317f06deSGavin Shan 	 * slot is dlpar removed and added.
7528234fcedSGavin Shan 	 *
7538234fcedSGavin Shan 	 * When the PHB is fenced, we have to issue a reset to recover from
7548234fcedSGavin Shan 	 * the error. Override the result if necessary to have partially
7558234fcedSGavin Shan 	 * hotplug for this case.
756317f06deSGavin Shan 	 */
75756ca4fdeSGavin Shan 	pr_info("EEH: Notify device drivers to shutdown\n");
758317f06deSGavin Shan 	eeh_pe_dev_traverse(pe, eeh_report_error, &result);
7598234fcedSGavin Shan 	if ((pe->type & EEH_PE_PHB) &&
7608234fcedSGavin Shan 	    result != PCI_ERS_RESULT_NONE &&
7618234fcedSGavin Shan 	    result != PCI_ERS_RESULT_NEED_RESET)
7628234fcedSGavin Shan 		result = PCI_ERS_RESULT_NEED_RESET;
763317f06deSGavin Shan 
764317f06deSGavin Shan 	/* Get the current PCI slot state. This can take a long time,
7652ac3990cSWei Yang 	 * sometimes over 300 seconds for certain systems.
766317f06deSGavin Shan 	 */
767317f06deSGavin Shan 	rc = eeh_ops->wait_state(pe, MAX_WAIT_FOR_RECOVERY*1000);
768317f06deSGavin Shan 	if (rc < 0 || rc == EEH_STATE_NOT_SUPPORT) {
7690dae2743SGavin Shan 		pr_warn("EEH: Permanent failure\n");
770317f06deSGavin Shan 		goto hard_fail;
771317f06deSGavin Shan 	}
772317f06deSGavin Shan 
773317f06deSGavin Shan 	/* Since rtas may enable MMIO when posting the error log,
774317f06deSGavin Shan 	 * don't post the error log until after all dev drivers
775317f06deSGavin Shan 	 * have been informed.
776317f06deSGavin Shan 	 */
77756ca4fdeSGavin Shan 	pr_info("EEH: Collect temporary log\n");
778317f06deSGavin Shan 	eeh_slot_error_detail(pe, EEH_LOG_TEMP);
779317f06deSGavin Shan 
780317f06deSGavin Shan 	/* If all device drivers were EEH-unaware, then shut
781317f06deSGavin Shan 	 * down all of the device drivers, and hope they
782317f06deSGavin Shan 	 * go down willingly, without panicing the system.
783317f06deSGavin Shan 	 */
784317f06deSGavin Shan 	if (result == PCI_ERS_RESULT_NONE) {
78556ca4fdeSGavin Shan 		pr_info("EEH: Reset with hotplug activity\n");
78667086e32SWei Yang 		rc = eeh_reset_device(pe, frozen_bus, NULL);
787317f06deSGavin Shan 		if (rc) {
7880dae2743SGavin Shan 			pr_warn("%s: Unable to reset, err=%d\n",
78956ca4fdeSGavin Shan 				__func__, rc);
790317f06deSGavin Shan 			goto hard_fail;
791317f06deSGavin Shan 		}
792317f06deSGavin Shan 	}
793317f06deSGavin Shan 
794317f06deSGavin Shan 	/* If all devices reported they can proceed, then re-enable MMIO */
795317f06deSGavin Shan 	if (result == PCI_ERS_RESULT_CAN_RECOVER) {
79656ca4fdeSGavin Shan 		pr_info("EEH: Enable I/O for affected devices\n");
797317f06deSGavin Shan 		rc = eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
798317f06deSGavin Shan 
799317f06deSGavin Shan 		if (rc < 0)
800317f06deSGavin Shan 			goto hard_fail;
801317f06deSGavin Shan 		if (rc) {
802317f06deSGavin Shan 			result = PCI_ERS_RESULT_NEED_RESET;
803317f06deSGavin Shan 		} else {
80456ca4fdeSGavin Shan 			pr_info("EEH: Notify device drivers to resume I/O\n");
805317f06deSGavin Shan 			eeh_pe_dev_traverse(pe, eeh_report_mmio_enabled, &result);
806317f06deSGavin Shan 		}
807317f06deSGavin Shan 	}
808317f06deSGavin Shan 
809317f06deSGavin Shan 	/* If all devices reported they can proceed, then re-enable DMA */
810317f06deSGavin Shan 	if (result == PCI_ERS_RESULT_CAN_RECOVER) {
81156ca4fdeSGavin Shan 		pr_info("EEH: Enabled DMA for affected devices\n");
812317f06deSGavin Shan 		rc = eeh_pci_enable(pe, EEH_OPT_THAW_DMA);
813317f06deSGavin Shan 
814317f06deSGavin Shan 		if (rc < 0)
815317f06deSGavin Shan 			goto hard_fail;
81635845a78SGavin Shan 		if (rc) {
817317f06deSGavin Shan 			result = PCI_ERS_RESULT_NEED_RESET;
81835845a78SGavin Shan 		} else {
81935845a78SGavin Shan 			/*
82035845a78SGavin Shan 			 * We didn't do PE reset for the case. The PE
82135845a78SGavin Shan 			 * is still in frozen state. Clear it before
82235845a78SGavin Shan 			 * resuming the PE.
82335845a78SGavin Shan 			 */
82435845a78SGavin Shan 			eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
825317f06deSGavin Shan 			result = PCI_ERS_RESULT_RECOVERED;
826317f06deSGavin Shan 		}
82735845a78SGavin Shan 	}
828317f06deSGavin Shan 
829317f06deSGavin Shan 	/* If any device has a hard failure, then shut off everything. */
830317f06deSGavin Shan 	if (result == PCI_ERS_RESULT_DISCONNECT) {
8310dae2743SGavin Shan 		pr_warn("EEH: Device driver gave up\n");
832317f06deSGavin Shan 		goto hard_fail;
833317f06deSGavin Shan 	}
834317f06deSGavin Shan 
835317f06deSGavin Shan 	/* If any device called out for a reset, then reset the slot */
836317f06deSGavin Shan 	if (result == PCI_ERS_RESULT_NEED_RESET) {
83756ca4fdeSGavin Shan 		pr_info("EEH: Reset without hotplug activity\n");
83867086e32SWei Yang 		rc = eeh_reset_device(pe, NULL, &rmv_data);
839317f06deSGavin Shan 		if (rc) {
8400dae2743SGavin Shan 			pr_warn("%s: Cannot reset, err=%d\n",
84156ca4fdeSGavin Shan 				__func__, rc);
842317f06deSGavin Shan 			goto hard_fail;
843317f06deSGavin Shan 		}
84456ca4fdeSGavin Shan 
84556ca4fdeSGavin Shan 		pr_info("EEH: Notify device drivers "
84656ca4fdeSGavin Shan 			"the completion of reset\n");
847317f06deSGavin Shan 		result = PCI_ERS_RESULT_NONE;
848317f06deSGavin Shan 		eeh_pe_dev_traverse(pe, eeh_report_reset, &result);
849317f06deSGavin Shan 	}
850317f06deSGavin Shan 
851317f06deSGavin Shan 	/* All devices should claim they have recovered by now. */
852317f06deSGavin Shan 	if ((result != PCI_ERS_RESULT_RECOVERED) &&
853317f06deSGavin Shan 	    (result != PCI_ERS_RESULT_NONE)) {
8540dae2743SGavin Shan 		pr_warn("EEH: Not recovered\n");
855317f06deSGavin Shan 		goto hard_fail;
856317f06deSGavin Shan 	}
857317f06deSGavin Shan 
85867086e32SWei Yang 	/*
85967086e32SWei Yang 	 * For those hot removed VFs, we should add back them after PF get
86067086e32SWei Yang 	 * recovered properly.
86167086e32SWei Yang 	 */
86267086e32SWei Yang 	list_for_each_entry_safe(edev, tmp, &rmv_data.edev_list, rmv_list) {
86367086e32SWei Yang 		eeh_add_virt_device(edev, NULL);
86467086e32SWei Yang 		list_del(&edev->rmv_list);
86567086e32SWei Yang 	}
86667086e32SWei Yang 
867317f06deSGavin Shan 	/* Tell all device drivers that they can resume operations */
86856ca4fdeSGavin Shan 	pr_info("EEH: Notify device driver to resume\n");
869317f06deSGavin Shan 	eeh_pe_dev_traverse(pe, eeh_report_resume, NULL);
870317f06deSGavin Shan 
871317f06deSGavin Shan 	return;
872317f06deSGavin Shan 
873317f06deSGavin Shan excess_failures:
874317f06deSGavin Shan 	/*
875317f06deSGavin Shan 	 * About 90% of all real-life EEH failures in the field
876317f06deSGavin Shan 	 * are due to poorly seated PCI cards. Only 10% or so are
877317f06deSGavin Shan 	 * due to actual, failed cards.
878317f06deSGavin Shan 	 */
879317f06deSGavin Shan 	pr_err("EEH: PHB#%d-PE#%x has failed %d times in the\n"
880317f06deSGavin Shan 	       "last hour and has been permanently disabled.\n"
881317f06deSGavin Shan 	       "Please try reseating or replacing it.\n",
882317f06deSGavin Shan 		pe->phb->global_number, pe->addr,
883317f06deSGavin Shan 		pe->freeze_count);
884317f06deSGavin Shan 	goto perm_error;
885317f06deSGavin Shan 
886317f06deSGavin Shan hard_fail:
887317f06deSGavin Shan 	pr_err("EEH: Unable to recover from failure from PHB#%d-PE#%x.\n"
888317f06deSGavin Shan 	       "Please try reseating or replacing it\n",
889317f06deSGavin Shan 		pe->phb->global_number, pe->addr);
890317f06deSGavin Shan 
891317f06deSGavin Shan perm_error:
892317f06deSGavin Shan 	eeh_slot_error_detail(pe, EEH_LOG_PERM);
893317f06deSGavin Shan 
894317f06deSGavin Shan 	/* Notify all devices that they're about to go down. */
895317f06deSGavin Shan 	eeh_pe_dev_traverse(pe, eeh_report_failure, NULL);
896317f06deSGavin Shan 
897d2b0f6f7SGavin Shan 	/* Mark the PE to be removed permanently */
898432227e9SGavin Shan 	eeh_pe_state_mark(pe, EEH_PE_REMOVED);
899d2b0f6f7SGavin Shan 
900d2b0f6f7SGavin Shan 	/*
901d2b0f6f7SGavin Shan 	 * Shut down the device drivers for good. We mark
902d2b0f6f7SGavin Shan 	 * all removed devices correctly to avoid access
903d2b0f6f7SGavin Shan 	 * the their PCI config any more.
904d2b0f6f7SGavin Shan 	 */
9051c2042c8SRafael J. Wysocki 	if (frozen_bus) {
90667086e32SWei Yang 		if (pe->type & EEH_PE_VF) {
90767086e32SWei Yang 			eeh_pe_dev_traverse(pe, eeh_rmv_device, NULL);
90867086e32SWei Yang 			eeh_pe_dev_mode_mark(pe, EEH_DEV_REMOVED);
90967086e32SWei Yang 		} else {
91005ba75f8SGavin Shan 			eeh_pe_state_clear(pe, EEH_PE_PRI_BUS);
911d2b0f6f7SGavin Shan 			eeh_pe_dev_mode_mark(pe, EEH_DEV_REMOVED);
912d2b0f6f7SGavin Shan 
9131c2042c8SRafael J. Wysocki 			pci_lock_rescan_remove();
914bd251b89SGavin Shan 			pci_hp_remove_devices(frozen_bus);
9151c2042c8SRafael J. Wysocki 			pci_unlock_rescan_remove();
9161c2042c8SRafael J. Wysocki 		}
917317f06deSGavin Shan 	}
91867086e32SWei Yang }
9198a6b1bc7SGavin Shan 
9208a6b1bc7SGavin Shan static void eeh_handle_special_event(void)
9218a6b1bc7SGavin Shan {
9228a6b1bc7SGavin Shan 	struct eeh_pe *pe, *phb_pe;
9238a6b1bc7SGavin Shan 	struct pci_bus *bus;
9247e4e7867SGavin Shan 	struct pci_controller *hose;
9258a6b1bc7SGavin Shan 	unsigned long flags;
9267e4e7867SGavin Shan 	int rc;
9278a6b1bc7SGavin Shan 
9287e4e7867SGavin Shan 
9297e4e7867SGavin Shan 	do {
9308a6b1bc7SGavin Shan 		rc = eeh_ops->next_error(&pe);
9318a6b1bc7SGavin Shan 
9328a6b1bc7SGavin Shan 		switch (rc) {
9337e4e7867SGavin Shan 		case EEH_NEXT_ERR_DEAD_IOC:
9348a6b1bc7SGavin Shan 			/* Mark all PHBs in dead state */
9358a6b1bc7SGavin Shan 			eeh_serialize_lock(&flags);
9367e4e7867SGavin Shan 
9377e4e7867SGavin Shan 			/* Purge all events */
9385c7a35e3SGavin Shan 			eeh_remove_event(NULL, true);
9397e4e7867SGavin Shan 
9407e4e7867SGavin Shan 			list_for_each_entry(hose, &hose_list, list_node) {
9418a6b1bc7SGavin Shan 				phb_pe = eeh_phb_pe_get(hose);
9428a6b1bc7SGavin Shan 				if (!phb_pe) continue;
9438a6b1bc7SGavin Shan 
9449e049375SGavin Shan 				eeh_pe_state_mark(phb_pe, EEH_PE_ISOLATED);
9458a6b1bc7SGavin Shan 			}
9467e4e7867SGavin Shan 
9478a6b1bc7SGavin Shan 			eeh_serialize_unlock(flags);
9488a6b1bc7SGavin Shan 
9498a6b1bc7SGavin Shan 			break;
9507e4e7867SGavin Shan 		case EEH_NEXT_ERR_FROZEN_PE:
9517e4e7867SGavin Shan 		case EEH_NEXT_ERR_FENCED_PHB:
9527e4e7867SGavin Shan 		case EEH_NEXT_ERR_DEAD_PHB:
9538a6b1bc7SGavin Shan 			/* Mark the PE in fenced state */
9548a6b1bc7SGavin Shan 			eeh_serialize_lock(&flags);
9557e4e7867SGavin Shan 
9567e4e7867SGavin Shan 			/* Purge all events of the PHB */
9575c7a35e3SGavin Shan 			eeh_remove_event(pe, true);
9587e4e7867SGavin Shan 
9597e4e7867SGavin Shan 			if (rc == EEH_NEXT_ERR_DEAD_PHB)
9609e049375SGavin Shan 				eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
9618a6b1bc7SGavin Shan 			else
9628a6b1bc7SGavin Shan 				eeh_pe_state_mark(pe,
9638a6b1bc7SGavin Shan 					EEH_PE_ISOLATED | EEH_PE_RECOVERING);
9647e4e7867SGavin Shan 
9658a6b1bc7SGavin Shan 			eeh_serialize_unlock(flags);
9668a6b1bc7SGavin Shan 
9678a6b1bc7SGavin Shan 			break;
9687e4e7867SGavin Shan 		case EEH_NEXT_ERR_NONE:
9697e4e7867SGavin Shan 			return;
9708a6b1bc7SGavin Shan 		default:
9717e4e7867SGavin Shan 			pr_warn("%s: Invalid value %d from next_error()\n",
9728a6b1bc7SGavin Shan 				__func__, rc);
9738a6b1bc7SGavin Shan 			return;
9748a6b1bc7SGavin Shan 		}
9758a6b1bc7SGavin Shan 
9768a6b1bc7SGavin Shan 		/*
9778a6b1bc7SGavin Shan 		 * For fenced PHB and frozen PE, it's handled as normal
9788a6b1bc7SGavin Shan 		 * event. We have to remove the affected PHBs for dead
9798a6b1bc7SGavin Shan 		 * PHB and IOC
9808a6b1bc7SGavin Shan 		 */
9817e4e7867SGavin Shan 		if (rc == EEH_NEXT_ERR_FROZEN_PE ||
9827e4e7867SGavin Shan 		    rc == EEH_NEXT_ERR_FENCED_PHB) {
9838a6b1bc7SGavin Shan 			eeh_handle_normal_event(pe);
9849e049375SGavin Shan 			eeh_pe_state_clear(pe, EEH_PE_RECOVERING);
9857e4e7867SGavin Shan 		} else {
9861c2042c8SRafael J. Wysocki 			pci_lock_rescan_remove();
9877e4e7867SGavin Shan 			list_for_each_entry(hose, &hose_list, list_node) {
9888a6b1bc7SGavin Shan 				phb_pe = eeh_phb_pe_get(hose);
9897e4e7867SGavin Shan 				if (!phb_pe ||
9909e049375SGavin Shan 				    !(phb_pe->state & EEH_PE_ISOLATED) ||
9919e049375SGavin Shan 				    (phb_pe->state & EEH_PE_RECOVERING))
9928a6b1bc7SGavin Shan 					continue;
9938a6b1bc7SGavin Shan 
9947e4e7867SGavin Shan 				/* Notify all devices to be down */
99505ba75f8SGavin Shan 				eeh_pe_state_clear(pe, EEH_PE_PRI_BUS);
996af2e3a00SRussell Currey 				eeh_pe_dev_traverse(pe,
997af2e3a00SRussell Currey 					eeh_report_failure, NULL);
9988a6b1bc7SGavin Shan 				bus = eeh_pe_bus_get(phb_pe);
99904fec21cSRussell Currey 				if (!bus) {
100004fec21cSRussell Currey 					pr_err("%s: Cannot find PCI bus for "
100104fec21cSRussell Currey 					       "PHB#%d-PE#%x\n",
100204fec21cSRussell Currey 					       __func__,
100304fec21cSRussell Currey 					       pe->phb->global_number,
100404fec21cSRussell Currey 					       pe->addr);
100504fec21cSRussell Currey 					break;
100604fec21cSRussell Currey 				}
1007bd251b89SGavin Shan 				pci_hp_remove_devices(bus);
10088a6b1bc7SGavin Shan 			}
10091c2042c8SRafael J. Wysocki 			pci_unlock_rescan_remove();
10108a6b1bc7SGavin Shan 		}
10117e4e7867SGavin Shan 
10127e4e7867SGavin Shan 		/*
10137e4e7867SGavin Shan 		 * If we have detected dead IOC, we needn't proceed
10147e4e7867SGavin Shan 		 * any more since all PHBs would have been removed
10157e4e7867SGavin Shan 		 */
10167e4e7867SGavin Shan 		if (rc == EEH_NEXT_ERR_DEAD_IOC)
10177e4e7867SGavin Shan 			break;
10187e4e7867SGavin Shan 	} while (rc != EEH_NEXT_ERR_NONE);
10198a6b1bc7SGavin Shan }
10208a6b1bc7SGavin Shan 
10218a6b1bc7SGavin Shan /**
10228a6b1bc7SGavin Shan  * eeh_handle_event - Reset a PCI device after hard lockup.
10238a6b1bc7SGavin Shan  * @pe: EEH PE
10248a6b1bc7SGavin Shan  *
10258a6b1bc7SGavin Shan  * While PHB detects address or data parity errors on particular PCI
10268a6b1bc7SGavin Shan  * slot, the associated PE will be frozen. Besides, DMA's occurring
10278a6b1bc7SGavin Shan  * to wild addresses (which usually happen due to bugs in device
10288a6b1bc7SGavin Shan  * drivers or in PCI adapter firmware) can cause EEH error. #SERR,
10298a6b1bc7SGavin Shan  * #PERR or other misc PCI-related errors also can trigger EEH errors.
10308a6b1bc7SGavin Shan  *
10318a6b1bc7SGavin Shan  * Recovery process consists of unplugging the device driver (which
10328a6b1bc7SGavin Shan  * generated hotplug events to userspace), then issuing a PCI #RST to
10338a6b1bc7SGavin Shan  * the device, then reconfiguring the PCI config space for all bridges
10348a6b1bc7SGavin Shan  * & devices under this slot, and then finally restarting the device
10358a6b1bc7SGavin Shan  * drivers (which cause a second set of hotplug events to go out to
10368a6b1bc7SGavin Shan  * userspace).
10378a6b1bc7SGavin Shan  */
10388a6b1bc7SGavin Shan void eeh_handle_event(struct eeh_pe *pe)
10398a6b1bc7SGavin Shan {
10408a6b1bc7SGavin Shan 	if (pe)
10418a6b1bc7SGavin Shan 		eeh_handle_normal_event(pe);
10428a6b1bc7SGavin Shan 	else
10438a6b1bc7SGavin Shan 		eeh_handle_special_event();
10448a6b1bc7SGavin Shan }
1045