blob: 04a5d9db5499d7b8d5434e5aa709125bbe5a441e [file] [log] [blame]
Linas Vepstas77bd7412005-11-03 18:52:49 -06001/*
2 * PCI Error Recovery Driver for RPA-compliant PPC64 platform.
Linas Vepstas3c8c90a2007-05-24 03:28:01 +10003 * Copyright IBM Corp. 2004 2005
4 * Copyright Linas Vepstas <linas@linas.org> 2004, 2005
Linas Vepstas77bd7412005-11-03 18:52:49 -06005 *
6 * All rights reserved.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
16 * NON INFRINGEMENT. See the GNU General Public License for more
17 * details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
Linas Vepstas3c8c90a2007-05-24 03:28:01 +100023 * Send comments and feedback to Linas Vepstas <linas@austin.ibm.com>
Linas Vepstas77bd7412005-11-03 18:52:49 -060024 */
25#include <linux/delay.h>
Linas Vepstas77bd7412005-11-03 18:52:49 -060026#include <linux/interrupt.h>
Linas Vepstasac325ac2006-04-18 21:05:21 -070027#include <linux/irq.h>
Gavin Shanfeadf7c2012-09-17 04:34:27 +000028#include <linux/module.h>
Linas Vepstas77bd7412005-11-03 18:52:49 -060029#include <linux/pci.h>
30#include <asm/eeh.h>
31#include <asm/eeh_event.h>
32#include <asm/ppc-pci.h>
33#include <asm/pci-bridge.h>
34#include <asm/prom.h>
35#include <asm/rtas.h>
36
Wei Yang67086e32016-03-04 10:53:11 +110037struct eeh_rmv_data {
38 struct list_head edev_list;
39 int removed;
40};
41
Gavin Shan29f8bf12012-02-27 20:04:02 +000042/**
43 * eeh_pcid_name - Retrieve name of PCI device driver
44 * @pdev: PCI device
45 *
46 * This routine is used to retrieve the name of PCI device driver
47 * if that's valid.
48 */
Gavin Shan40a7cd92012-02-27 20:04:08 +000049static inline const char *eeh_pcid_name(struct pci_dev *pdev)
Linas Vepstas77bd7412005-11-03 18:52:49 -060050{
Olaf Hering273d2802006-02-27 15:52:59 +010051 if (pdev && pdev->dev.driver)
Linas Vepstas77bd7412005-11-03 18:52:49 -060052 return pdev->dev.driver->name;
53 return "";
54}
55
Gavin Shanfeadf7c2012-09-17 04:34:27 +000056/**
57 * eeh_pcid_get - Get the PCI device driver
58 * @pdev: PCI device
59 *
60 * The function is used to retrieve the PCI device driver for
61 * the indicated PCI device. Besides, we will increase the reference
62 * of the PCI device driver to prevent that being unloaded on
63 * the fly. Otherwise, kernel crash would be seen.
64 */
65static inline struct pci_driver *eeh_pcid_get(struct pci_dev *pdev)
66{
67 if (!pdev || !pdev->driver)
68 return NULL;
69
70 if (!try_module_get(pdev->driver->driver.owner))
71 return NULL;
72
73 return pdev->driver;
74}
75
76/**
77 * eeh_pcid_put - Dereference on the PCI device driver
78 * @pdev: PCI device
79 *
80 * The function is called to do dereference on the PCI device
81 * driver of the indicated PCI device.
82 */
83static inline void eeh_pcid_put(struct pci_dev *pdev)
84{
85 if (!pdev || !pdev->driver)
86 return;
87
88 module_put(pdev->driver->driver.owner);
89}
90
Mike Mason8535ef02009-02-10 11:12:21 +000091/**
Gavin Shan29f8bf12012-02-27 20:04:02 +000092 * eeh_disable_irq - Disable interrupt for the recovering device
93 * @dev: PCI device
94 *
95 * This routine must be called when reporting temporary or permanent
96 * error to the particular PCI device to disable interrupt of that
97 * device. If the device has enabled MSI or MSI-X interrupt, we needn't
98 * do real work because EEH should freeze DMA transfers for those PCI
99 * devices encountering EEH errors, which includes MSI or MSI-X.
Mike Mason8535ef02009-02-10 11:12:21 +0000100 */
101static void eeh_disable_irq(struct pci_dev *dev)
102{
Gavin Shan40a7cd92012-02-27 20:04:08 +0000103 struct eeh_dev *edev = pci_dev_to_eeh_dev(dev);
Mike Mason8535ef02009-02-10 11:12:21 +0000104
105 /* Don't disable MSI and MSI-X interrupts. They are
106 * effectively disabled by the DMA Stopped state
107 * when an EEH error occurs.
Gavin Shan29f8bf12012-02-27 20:04:02 +0000108 */
Mike Mason8535ef02009-02-10 11:12:21 +0000109 if (dev->msi_enabled || dev->msix_enabled)
110 return;
111
Michael Ellerman59e3f832009-10-13 19:44:47 +0000112 if (!irq_has_action(dev->irq))
Mike Mason8535ef02009-02-10 11:12:21 +0000113 return;
114
Gavin Shandbbceee2012-09-07 22:44:20 +0000115 edev->mode |= EEH_DEV_IRQ_DISABLED;
Mike Mason8535ef02009-02-10 11:12:21 +0000116 disable_irq_nosync(dev->irq);
117}
118
119/**
Gavin Shan29f8bf12012-02-27 20:04:02 +0000120 * eeh_enable_irq - Enable interrupt for the recovering device
121 * @dev: PCI device
122 *
123 * This routine must be called to enable interrupt while failed
124 * device could be resumed.
Mike Mason8535ef02009-02-10 11:12:21 +0000125 */
126static void eeh_enable_irq(struct pci_dev *dev)
127{
Gavin Shan40a7cd92012-02-27 20:04:08 +0000128 struct eeh_dev *edev = pci_dev_to_eeh_dev(dev);
Mike Mason8535ef02009-02-10 11:12:21 +0000129
Gavin Shandbbceee2012-09-07 22:44:20 +0000130 if ((edev->mode) & EEH_DEV_IRQ_DISABLED) {
131 edev->mode &= ~EEH_DEV_IRQ_DISABLED;
Thomas Gleixnerb8a9a112014-02-23 21:40:09 +0000132 /*
133 * FIXME !!!!!
134 *
135 * This is just ass backwards. This maze has
136 * unbalanced irq_enable/disable calls. So instead of
137 * finding the root cause it works around the warning
138 * in the irq_enable code by conditionally calling
139 * into it.
140 *
141 * That's just wrong.The warning in the core code is
Michael Ellerman027dfac2016-06-01 16:34:37 +1000142 * there to tell people to fix their asymmetries in
Thomas Gleixnerb8a9a112014-02-23 21:40:09 +0000143 * their own code, not by abusing the core information
144 * to avoid it.
145 *
146 * I so wish that the assymetry would be the other way
147 * round and a few more irq_disable calls render that
148 * shit unusable forever.
149 *
150 * tglx
151 */
Thomas Gleixner57310c32014-03-05 00:06:11 +0100152 if (irqd_irq_disabled(irq_get_irq_data(dev->irq)))
Gavin Shan91150af2013-07-24 10:25:00 +0800153 enable_irq(dev->irq);
Thomas Gleixner57310c32014-03-05 00:06:11 +0100154 }
Mike Mason8535ef02009-02-10 11:12:21 +0000155}
156
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000157static bool eeh_dev_removed(struct eeh_dev *edev)
158{
159 /* EEH device removed ? */
160 if (!edev || (edev->mode & EEH_DEV_REMOVED))
161 return true;
162
163 return false;
164}
165
Gavin Shan5cfb20b2014-09-30 12:39:07 +1000166static void *eeh_dev_save_state(void *data, void *userdata)
167{
168 struct eeh_dev *edev = data;
169 struct pci_dev *pdev;
170
171 if (!edev)
172 return NULL;
173
Gavin Shan5a0cdbf2016-04-27 11:14:51 +1000174 /*
175 * We cannot access the config space on some adapters.
176 * Otherwise, it will cause fenced PHB. We don't save
177 * the content in their config space and will restore
178 * from the initial config space saved when the EEH
179 * device is created.
180 */
181 if (edev->pe && (edev->pe->state & EEH_PE_CFG_RESTRICTED))
182 return NULL;
183
Gavin Shan5cfb20b2014-09-30 12:39:07 +1000184 pdev = eeh_dev_to_pci_dev(edev);
185 if (!pdev)
186 return NULL;
187
188 pci_save_state(pdev);
189 return NULL;
190}
191
Linas Vepstascb5b56242006-09-15 18:56:35 -0500192/**
Gavin Shan29f8bf12012-02-27 20:04:02 +0000193 * eeh_report_error - Report pci error to each device driver
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000194 * @data: eeh device
Gavin Shan29f8bf12012-02-27 20:04:02 +0000195 * @userdata: return value
Gavin Shana84f2732013-06-20 13:20:51 +0800196 *
197 * Report an EEH error to each device driver, collect up and
198 * merge the device driver responses. Cumulative response
Linas Vepstascb5b56242006-09-15 18:56:35 -0500199 * passed back in "userdata".
Linas Vepstas77bd7412005-11-03 18:52:49 -0600200 */
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000201static void *eeh_report_error(void *data, void *userdata)
Linas Vepstas77bd7412005-11-03 18:52:49 -0600202{
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000203 struct eeh_dev *edev = (struct eeh_dev *)data;
204 struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
Paul Mackerras18eb3b32005-11-29 17:17:02 +1100205 enum pci_ers_result rc, *res = userdata;
Gavin Shanfeadf7c2012-09-17 04:34:27 +0000206 struct pci_driver *driver;
Linas Vepstas77bd7412005-11-03 18:52:49 -0600207
Gavin Shan2311cca2016-03-04 10:53:12 +1100208 if (!dev || eeh_dev_removed(edev) || eeh_pe_passed(edev->pe))
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000209 return NULL;
Linas Vepstas77bd7412005-11-03 18:52:49 -0600210 dev->error_state = pci_channel_io_frozen;
211
Gavin Shanfeadf7c2012-09-17 04:34:27 +0000212 driver = eeh_pcid_get(dev);
213 if (!driver) return NULL;
Linas Vepstas77bd7412005-11-03 18:52:49 -0600214
Mike Mason8535ef02009-02-10 11:12:21 +0000215 eeh_disable_irq(dev);
216
Linas Vepstas6a1ca372006-09-15 18:58:59 -0500217 if (!driver->err_handler ||
Gavin Shanfeadf7c2012-09-17 04:34:27 +0000218 !driver->err_handler->error_detected) {
219 eeh_pcid_put(dev);
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000220 return NULL;
Gavin Shanfeadf7c2012-09-17 04:34:27 +0000221 }
Linas Vepstas77bd7412005-11-03 18:52:49 -0600222
Gavin Shan29f8bf12012-02-27 20:04:02 +0000223 rc = driver->err_handler->error_detected(dev, pci_channel_io_frozen);
Linas Vepstas2a50f142007-11-03 07:27:50 +1100224
225 /* A driver that needs a reset trumps all others */
226 if (rc == PCI_ERS_RESULT_NEED_RESET) *res = rc;
Paul Mackerras18eb3b32005-11-29 17:17:02 +1100227 if (*res == PCI_ERS_RESULT_NONE) *res = rc;
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800228
Wei Yang67086e32016-03-04 10:53:11 +1100229 edev->in_error = true;
Gavin Shanfeadf7c2012-09-17 04:34:27 +0000230 eeh_pcid_put(dev);
Bryant G. Ly856e1eb2018-01-05 10:45:47 -0600231 pci_uevent_ers(dev, PCI_ERS_RESULT_NONE);
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000232 return NULL;
Linas Vepstas6a1ca372006-09-15 18:58:59 -0500233}
234
235/**
Gavin Shan29f8bf12012-02-27 20:04:02 +0000236 * eeh_report_mmio_enabled - Tell drivers that MMIO has been enabled
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000237 * @data: eeh device
Gavin Shan29f8bf12012-02-27 20:04:02 +0000238 * @userdata: return value
Linas Vepstas6a1ca372006-09-15 18:58:59 -0500239 *
Linas Vepstas638799b2007-11-03 07:25:55 +1100240 * Tells each device driver that IO ports, MMIO and config space I/O
241 * are now enabled. Collects up and merges the device driver responses.
242 * Cumulative response passed back in "userdata".
Linas Vepstas6a1ca372006-09-15 18:58:59 -0500243 */
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000244static void *eeh_report_mmio_enabled(void *data, void *userdata)
Linas Vepstas6a1ca372006-09-15 18:58:59 -0500245{
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000246 struct eeh_dev *edev = (struct eeh_dev *)data;
247 struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
Linas Vepstas6a1ca372006-09-15 18:58:59 -0500248 enum pci_ers_result rc, *res = userdata;
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000249 struct pci_driver *driver;
Linas Vepstas6a1ca372006-09-15 18:58:59 -0500250
Gavin Shan2311cca2016-03-04 10:53:12 +1100251 if (!dev || eeh_dev_removed(edev) || eeh_pe_passed(edev->pe))
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000252 return NULL;
253
Gavin Shanfeadf7c2012-09-17 04:34:27 +0000254 driver = eeh_pcid_get(dev);
255 if (!driver) return NULL;
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000256
Gavin Shanfeadf7c2012-09-17 04:34:27 +0000257 if (!driver->err_handler ||
Gavin Shanf26c7a02014-01-12 14:13:45 +0800258 !driver->err_handler->mmio_enabled ||
259 (edev->mode & EEH_DEV_NO_HANDLER)) {
Gavin Shanfeadf7c2012-09-17 04:34:27 +0000260 eeh_pcid_put(dev);
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000261 return NULL;
Gavin Shanfeadf7c2012-09-17 04:34:27 +0000262 }
Linas Vepstas6a1ca372006-09-15 18:58:59 -0500263
Gavin Shan29f8bf12012-02-27 20:04:02 +0000264 rc = driver->err_handler->mmio_enabled(dev);
Linas Vepstas2a50f142007-11-03 07:27:50 +1100265
266 /* A driver that needs a reset trumps all others */
267 if (rc == PCI_ERS_RESULT_NEED_RESET) *res = rc;
Linas Vepstas6a1ca372006-09-15 18:58:59 -0500268 if (*res == PCI_ERS_RESULT_NONE) *res = rc;
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800269
Gavin Shanfeadf7c2012-09-17 04:34:27 +0000270 eeh_pcid_put(dev);
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000271 return NULL;
Linas Vepstas77bd7412005-11-03 18:52:49 -0600272}
273
Linas Vepstascb5b56242006-09-15 18:56:35 -0500274/**
Gavin Shan29f8bf12012-02-27 20:04:02 +0000275 * eeh_report_reset - Tell device that slot has been reset
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000276 * @data: eeh device
Gavin Shan29f8bf12012-02-27 20:04:02 +0000277 * @userdata: return value
278 *
279 * This routine must be called while EEH tries to reset particular
280 * PCI device so that the associated PCI device driver could take
281 * some actions, usually to save data the driver needs so that the
282 * driver can work again while the device is recovered.
Linas Vepstas77bd7412005-11-03 18:52:49 -0600283 */
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000284static void *eeh_report_reset(void *data, void *userdata)
Linas Vepstas77bd7412005-11-03 18:52:49 -0600285{
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000286 struct eeh_dev *edev = (struct eeh_dev *)data;
287 struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
Linas Vepstas6a1ca372006-09-15 18:58:59 -0500288 enum pci_ers_result rc, *res = userdata;
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000289 struct pci_driver *driver;
Linas Vepstas77bd7412005-11-03 18:52:49 -0600290
Gavin Shan2311cca2016-03-04 10:53:12 +1100291 if (!dev || eeh_dev_removed(edev) || eeh_pe_passed(edev->pe))
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000292 return NULL;
Mike Masonc58dc572009-04-10 08:57:03 +0000293 dev->error_state = pci_channel_io_normal;
294
Gavin Shanfeadf7c2012-09-17 04:34:27 +0000295 driver = eeh_pcid_get(dev);
296 if (!driver) return NULL;
297
Mike Mason8535ef02009-02-10 11:12:21 +0000298 eeh_enable_irq(dev);
299
Linas Vepstas6a1ca372006-09-15 18:58:59 -0500300 if (!driver->err_handler ||
Gavin Shanf26c7a02014-01-12 14:13:45 +0800301 !driver->err_handler->slot_reset ||
Wei Yang67086e32016-03-04 10:53:11 +1100302 (edev->mode & EEH_DEV_NO_HANDLER) ||
303 (!edev->in_error)) {
Gavin Shanfeadf7c2012-09-17 04:34:27 +0000304 eeh_pcid_put(dev);
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000305 return NULL;
Gavin Shanfeadf7c2012-09-17 04:34:27 +0000306 }
Linas Vepstas77bd7412005-11-03 18:52:49 -0600307
Linas Vepstas6a1ca372006-09-15 18:58:59 -0500308 rc = driver->err_handler->slot_reset(dev);
Linas Vepstas5794dbc2007-03-19 14:55:51 -0500309 if ((*res == PCI_ERS_RESULT_NONE) ||
310 (*res == PCI_ERS_RESULT_RECOVERED)) *res = rc;
Linas Vepstas6a1ca372006-09-15 18:58:59 -0500311 if (*res == PCI_ERS_RESULT_DISCONNECT &&
312 rc == PCI_ERS_RESULT_NEED_RESET) *res = rc;
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800313
Gavin Shanfeadf7c2012-09-17 04:34:27 +0000314 eeh_pcid_put(dev);
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000315 return NULL;
Linas Vepstas77bd7412005-11-03 18:52:49 -0600316}
317
Gavin Shan5cfb20b2014-09-30 12:39:07 +1000318static void *eeh_dev_restore_state(void *data, void *userdata)
319{
320 struct eeh_dev *edev = data;
321 struct pci_dev *pdev;
322
323 if (!edev)
324 return NULL;
325
Gavin Shan5a0cdbf2016-04-27 11:14:51 +1000326 /*
327 * The content in the config space isn't saved because
328 * the blocked config space on some adapters. We have
329 * to restore the initial saved config space when the
330 * EEH device is created.
331 */
332 if (edev->pe && (edev->pe->state & EEH_PE_CFG_RESTRICTED)) {
333 if (list_is_last(&edev->list, &edev->pe->edevs))
334 eeh_pe_restore_bars(edev->pe);
335
336 return NULL;
337 }
338
Gavin Shan5cfb20b2014-09-30 12:39:07 +1000339 pdev = eeh_dev_to_pci_dev(edev);
340 if (!pdev)
341 return NULL;
342
343 pci_restore_state(pdev);
344 return NULL;
345}
346
Linas Vepstascb5b56242006-09-15 18:56:35 -0500347/**
Gavin Shan29f8bf12012-02-27 20:04:02 +0000348 * eeh_report_resume - Tell device to resume normal operations
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000349 * @data: eeh device
Gavin Shan29f8bf12012-02-27 20:04:02 +0000350 * @userdata: return value
351 *
352 * This routine must be called to notify the device driver that it
353 * could resume so that the device driver can do some initialization
354 * to make the recovered device work again.
Linas Vepstascb5b56242006-09-15 18:56:35 -0500355 */
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000356static void *eeh_report_resume(void *data, void *userdata)
Linas Vepstas77bd7412005-11-03 18:52:49 -0600357{
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000358 struct eeh_dev *edev = (struct eeh_dev *)data;
359 struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
Wei Yang67086e32016-03-04 10:53:11 +1100360 bool was_in_error;
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000361 struct pci_driver *driver;
362
Gavin Shan2311cca2016-03-04 10:53:12 +1100363 if (!dev || eeh_dev_removed(edev) || eeh_pe_passed(edev->pe))
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000364 return NULL;
Linas Vepstas77bd7412005-11-03 18:52:49 -0600365 dev->error_state = pci_channel_io_normal;
366
Gavin Shanfeadf7c2012-09-17 04:34:27 +0000367 driver = eeh_pcid_get(dev);
368 if (!driver) return NULL;
Linas Vepstasd0e70342006-12-06 12:32:20 -0600369
Wei Yang67086e32016-03-04 10:53:11 +1100370 was_in_error = edev->in_error;
371 edev->in_error = false;
Mike Mason8535ef02009-02-10 11:12:21 +0000372 eeh_enable_irq(dev);
373
Linas Vepstasd0e70342006-12-06 12:32:20 -0600374 if (!driver->err_handler ||
Gavin Shanf26c7a02014-01-12 14:13:45 +0800375 !driver->err_handler->resume ||
Wei Yang67086e32016-03-04 10:53:11 +1100376 (edev->mode & EEH_DEV_NO_HANDLER) || !was_in_error) {
Gavin Shanf26c7a02014-01-12 14:13:45 +0800377 edev->mode &= ~EEH_DEV_NO_HANDLER;
Gavin Shanfeadf7c2012-09-17 04:34:27 +0000378 eeh_pcid_put(dev);
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000379 return NULL;
Gavin Shanfeadf7c2012-09-17 04:34:27 +0000380 }
Linas Vepstas77bd7412005-11-03 18:52:49 -0600381
382 driver->err_handler->resume(dev);
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800383
Gavin Shanfeadf7c2012-09-17 04:34:27 +0000384 eeh_pcid_put(dev);
Bryant G. Ly856e1eb2018-01-05 10:45:47 -0600385 pci_uevent_ers(dev, PCI_ERS_RESULT_RECOVERED);
386#ifdef CONFIG_PCI_IOV
Juan J. Alvarez521ca5a2018-02-15 12:49:51 -0600387 if (eeh_ops->notify_resume && eeh_dev_to_pdn(edev))
388 eeh_ops->notify_resume(eeh_dev_to_pdn(edev));
Bryant G. Ly856e1eb2018-01-05 10:45:47 -0600389#endif
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000390 return NULL;
Linas Vepstas77bd7412005-11-03 18:52:49 -0600391}
392
Linas Vepstascb5b56242006-09-15 18:56:35 -0500393/**
Gavin Shan29f8bf12012-02-27 20:04:02 +0000394 * eeh_report_failure - Tell device driver that device is dead.
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000395 * @data: eeh device
Gavin Shan29f8bf12012-02-27 20:04:02 +0000396 * @userdata: return value
Linas Vepstascb5b56242006-09-15 18:56:35 -0500397 *
398 * This informs the device driver that the device is permanently
399 * dead, and that no further recovery attempts will be made on it.
400 */
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000401static void *eeh_report_failure(void *data, void *userdata)
Linas Vepstas77bd7412005-11-03 18:52:49 -0600402{
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000403 struct eeh_dev *edev = (struct eeh_dev *)data;
404 struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
405 struct pci_driver *driver;
406
Gavin Shan2311cca2016-03-04 10:53:12 +1100407 if (!dev || eeh_dev_removed(edev) || eeh_pe_passed(edev->pe))
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000408 return NULL;
Linas Vepstas77bd7412005-11-03 18:52:49 -0600409 dev->error_state = pci_channel_io_perm_failure;
410
Gavin Shanfeadf7c2012-09-17 04:34:27 +0000411 driver = eeh_pcid_get(dev);
412 if (!driver) return NULL;
Linas Vepstas77bd7412005-11-03 18:52:49 -0600413
Mike Mason8535ef02009-02-10 11:12:21 +0000414 eeh_disable_irq(dev);
415
416 if (!driver->err_handler ||
Gavin Shanfeadf7c2012-09-17 04:34:27 +0000417 !driver->err_handler->error_detected) {
418 eeh_pcid_put(dev);
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000419 return NULL;
Gavin Shanfeadf7c2012-09-17 04:34:27 +0000420 }
Mike Mason8535ef02009-02-10 11:12:21 +0000421
Linas Vepstas77bd7412005-11-03 18:52:49 -0600422 driver->err_handler->error_detected(dev, pci_channel_io_perm_failure);
Zhang, Yanmin70298c62009-06-16 13:34:38 +0800423
Gavin Shanfeadf7c2012-09-17 04:34:27 +0000424 eeh_pcid_put(dev);
Bryant G. Ly856e1eb2018-01-05 10:45:47 -0600425 pci_uevent_ers(dev, PCI_ERS_RESULT_DISCONNECT);
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000426 return NULL;
Linas Vepstas77bd7412005-11-03 18:52:49 -0600427}
428
Wei Yang67086e32016-03-04 10:53:11 +1100429static void *eeh_add_virt_device(void *data, void *userdata)
430{
431 struct pci_driver *driver;
432 struct eeh_dev *edev = (struct eeh_dev *)data;
433 struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
434 struct pci_dn *pdn = eeh_dev_to_pdn(edev);
435
436 if (!(edev->physfn)) {
437 pr_warn("%s: EEH dev %04x:%02x:%02x.%01x not for VF\n",
Alexey Kardashevskiy69672bd2017-08-29 17:34:01 +1000438 __func__, pdn->phb->global_number, pdn->busno,
Wei Yang67086e32016-03-04 10:53:11 +1100439 PCI_SLOT(pdn->devfn), PCI_FUNC(pdn->devfn));
440 return NULL;
441 }
442
443 driver = eeh_pcid_get(dev);
444 if (driver) {
445 eeh_pcid_put(dev);
446 if (driver->err_handler)
447 return NULL;
448 }
449
Bryant G. Ly988fc3b2017-11-09 08:00:33 -0600450#ifdef CONFIG_PCI_IOV
Jan H. Schönherr753f6122017-09-26 12:53:23 -0500451 pci_iov_add_virtfn(edev->physfn, pdn->vf_index);
Wei Yang67086e32016-03-04 10:53:11 +1100452#endif
453 return NULL;
454}
455
Gavin Shanf5c57712013-07-24 10:24:58 +0800456static void *eeh_rmv_device(void *data, void *userdata)
457{
458 struct pci_driver *driver;
459 struct eeh_dev *edev = (struct eeh_dev *)data;
460 struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
Wei Yang67086e32016-03-04 10:53:11 +1100461 struct eeh_rmv_data *rmv_data = (struct eeh_rmv_data *)userdata;
462 int *removed = rmv_data ? &rmv_data->removed : NULL;
Gavin Shanf5c57712013-07-24 10:24:58 +0800463
464 /*
465 * Actually, we should remove the PCI bridges as well.
466 * However, that's lots of complexity to do that,
467 * particularly some of devices under the bridge might
468 * support EEH. So we just care about PCI devices for
469 * simplicity here.
470 */
Bjorn Helgaas93de6902015-12-03 13:18:18 -0600471 if (!dev || (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE))
Gavin Shanf5c57712013-07-24 10:24:58 +0800472 return NULL;
Thadeu Lima de Souza Cascardo8cc6b6c2014-02-05 16:20:45 -0200473
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000474 /*
475 * We rely on count-based pcibios_release_device() to
476 * detach permanently offlined PEs. Unfortunately, that's
477 * not reliable enough. We might have the permanently
478 * offlined PEs attached, but we needn't take care of
479 * them and their child devices.
480 */
481 if (eeh_dev_removed(edev))
482 return NULL;
483
Gavin Shanf5c57712013-07-24 10:24:58 +0800484 driver = eeh_pcid_get(dev);
Thadeu Lima de Souza Cascardo8cc6b6c2014-02-05 16:20:45 -0200485 if (driver) {
486 eeh_pcid_put(dev);
Wei Yang67086e32016-03-04 10:53:11 +1100487 if (removed &&
Gavin Shan3fa7bf72016-03-04 10:53:13 +1100488 eeh_pe_passed(edev->pe))
489 return NULL;
490 if (removed &&
Wei Yang67086e32016-03-04 10:53:11 +1100491 driver->err_handler &&
Gavin Shanf2da4cc2015-10-08 14:58:53 +1100492 driver->err_handler->error_detected &&
Gavin Shanf6bf0fa2016-02-12 16:03:05 +1100493 driver->err_handler->slot_reset)
Thadeu Lima de Souza Cascardo8cc6b6c2014-02-05 16:20:45 -0200494 return NULL;
495 }
Gavin Shanf5c57712013-07-24 10:24:58 +0800496
497 /* Remove it from PCI subsystem */
498 pr_debug("EEH: Removing %s without EEH sensitive driver\n",
499 pci_name(dev));
500 edev->bus = dev->bus;
501 edev->mode |= EEH_DEV_DISCONNECTED;
Wei Yang67086e32016-03-04 10:53:11 +1100502 if (removed)
503 (*removed)++;
Gavin Shanf5c57712013-07-24 10:24:58 +0800504
Wei Yang67086e32016-03-04 10:53:11 +1100505 if (edev->physfn) {
Bryant G. Ly988fc3b2017-11-09 08:00:33 -0600506#ifdef CONFIG_PCI_IOV
Wei Yang67086e32016-03-04 10:53:11 +1100507 struct pci_dn *pdn = eeh_dev_to_pdn(edev);
508
Jan H. Schönherr753f6122017-09-26 12:53:23 -0500509 pci_iov_remove_virtfn(edev->physfn, pdn->vf_index);
Wei Yang67086e32016-03-04 10:53:11 +1100510 edev->pdev = NULL;
511
512 /*
513 * We have to set the VF PE number to invalid one, which is
514 * required to plug the VF successfully.
515 */
516 pdn->pe_number = IODA_INVALID_PE;
517#endif
518 if (rmv_data)
519 list_add(&edev->rmv_list, &rmv_data->edev_list);
520 } else {
521 pci_lock_rescan_remove();
522 pci_stop_and_remove_bus_device(dev);
523 pci_unlock_rescan_remove();
524 }
Gavin Shanf5c57712013-07-24 10:24:58 +0800525
526 return NULL;
527}
528
529static void *eeh_pe_detach_dev(void *data, void *userdata)
530{
531 struct eeh_pe *pe = (struct eeh_pe *)data;
532 struct eeh_dev *edev, *tmp;
533
534 eeh_pe_for_each_dev(pe, edev, tmp) {
535 if (!(edev->mode & EEH_DEV_DISCONNECTED))
536 continue;
537
538 edev->mode &= ~(EEH_DEV_DISCONNECTED | EEH_DEV_IRQ_DISABLED);
539 eeh_rmv_from_parent_pe(edev);
540 }
541
542 return NULL;
543}
544
Gavin Shan78954702014-04-24 18:00:14 +1000545/*
546 * Explicitly clear PE's frozen state for PowerNV where
547 * we have frozen PE until BAR restore is completed. It's
548 * harmless to clear it for pSeries. To be consistent with
549 * PE reset (for 3 times), we try to clear the frozen state
550 * for 3 times as well.
551 */
Gavin Shan2c665992014-05-05 09:29:02 +1000552static void *__eeh_clear_pe_frozen_state(void *data, void *flag)
Gavin Shan78954702014-04-24 18:00:14 +1000553{
Gavin Shan2c665992014-05-05 09:29:02 +1000554 struct eeh_pe *pe = (struct eeh_pe *)data;
Gavin Shanf05fea52017-01-19 10:10:16 +1100555 bool clear_sw_state = *(bool *)flag;
Gavin Shanc9dd0142014-09-30 12:39:02 +1000556 int i, rc = 1;
Gavin Shan78954702014-04-24 18:00:14 +1000557
Gavin Shanc9dd0142014-09-30 12:39:02 +1000558 for (i = 0; rc && i < 3; i++)
Gavin Shan5cfb20b2014-09-30 12:39:07 +1000559 rc = eeh_unfreeze_pe(pe, clear_sw_state);
Gavin Shan78954702014-04-24 18:00:14 +1000560
Gavin Shanc9dd0142014-09-30 12:39:02 +1000561 /* Stop immediately on any errors */
Gavin Shan2c665992014-05-05 09:29:02 +1000562 if (rc) {
Gavin Shanc9dd0142014-09-30 12:39:02 +1000563 pr_warn("%s: Failure %d unfreezing PHB#%x-PE#%x\n",
564 __func__, rc, pe->phb->global_number, pe->addr);
Gavin Shan2c665992014-05-05 09:29:02 +1000565 return (void *)pe;
566 }
567
568 return NULL;
569}
570
Gavin Shan5cfb20b2014-09-30 12:39:07 +1000571static int eeh_clear_pe_frozen_state(struct eeh_pe *pe,
572 bool clear_sw_state)
Gavin Shan2c665992014-05-05 09:29:02 +1000573{
574 void *rc;
575
Gavin Shan5cfb20b2014-09-30 12:39:07 +1000576 rc = eeh_pe_traverse(pe, __eeh_clear_pe_frozen_state, &clear_sw_state);
Gavin Shan2c665992014-05-05 09:29:02 +1000577 if (!rc)
Gavin Shan78954702014-04-24 18:00:14 +1000578 eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
579
Gavin Shan2c665992014-05-05 09:29:02 +1000580 return rc ? -EIO : 0;
Gavin Shan78954702014-04-24 18:00:14 +1000581}
582
Gavin Shan5cfb20b2014-09-30 12:39:07 +1000583int eeh_pe_reset_and_recover(struct eeh_pe *pe)
584{
Gavin Shan2efc7712016-04-27 11:14:52 +1000585 int ret;
Gavin Shan5cfb20b2014-09-30 12:39:07 +1000586
587 /* Bail if the PE is being recovered */
588 if (pe->state & EEH_PE_RECOVERING)
589 return 0;
590
591 /* Put the PE into recovery mode */
592 eeh_pe_state_mark(pe, EEH_PE_RECOVERING);
593
594 /* Save states */
595 eeh_pe_dev_traverse(pe, eeh_dev_save_state, NULL);
596
Gavin Shan5cfb20b2014-09-30 12:39:07 +1000597 /* Issue reset */
Russell Currey6654c932016-11-17 16:07:47 +1100598 ret = eeh_pe_reset_full(pe);
Gavin Shan5cfb20b2014-09-30 12:39:07 +1000599 if (ret) {
Gavin Shan28bf36f2014-11-14 10:47:29 +1100600 eeh_pe_state_clear(pe, EEH_PE_RECOVERING);
Gavin Shan5cfb20b2014-09-30 12:39:07 +1000601 return ret;
602 }
Gavin Shan5cfb20b2014-09-30 12:39:07 +1000603
604 /* Unfreeze the PE */
605 ret = eeh_clear_pe_frozen_state(pe, true);
606 if (ret) {
607 eeh_pe_state_clear(pe, EEH_PE_RECOVERING);
608 return ret;
609 }
610
Gavin Shan5cfb20b2014-09-30 12:39:07 +1000611 /* Restore device state */
612 eeh_pe_dev_traverse(pe, eeh_dev_restore_state, NULL);
613
Gavin Shan5cfb20b2014-09-30 12:39:07 +1000614 /* Clear recovery mode */
615 eeh_pe_state_clear(pe, EEH_PE_RECOVERING);
616
617 return 0;
618}
619
Linas Vepstas77bd7412005-11-03 18:52:49 -0600620/**
Gavin Shan29f8bf12012-02-27 20:04:02 +0000621 * eeh_reset_device - Perform actual reset of a pci slot
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000622 * @pe: EEH PE
Gavin Shan29f8bf12012-02-27 20:04:02 +0000623 * @bus: PCI bus corresponding to the isolcated slot
Linas Vepstas77bd7412005-11-03 18:52:49 -0600624 *
Gavin Shan29f8bf12012-02-27 20:04:02 +0000625 * This routine must be called to do reset on the indicated PE.
626 * During the reset, udev might be invoked because those affected
627 * PCI devices will be removed and then added.
Linas Vepstas77bd7412005-11-03 18:52:49 -0600628 */
Wei Yang67086e32016-03-04 10:53:11 +1100629static int eeh_reset_device(struct eeh_pe *pe, struct pci_bus *bus,
630 struct eeh_rmv_data *rmv_data)
Linas Vepstas77bd7412005-11-03 18:52:49 -0600631{
Gavin Shanf5c57712013-07-24 10:24:58 +0800632 struct pci_bus *frozen_bus = eeh_pe_bus_get(pe);
Arnd Bergmannedfd17f2017-11-04 22:26:52 +0100633 time64_t tstamp;
Wei Yang67086e32016-03-04 10:53:11 +1100634 int cnt, rc;
635 struct eeh_dev *edev;
Linas Vepstas42405452006-04-28 17:39:38 -0500636
637 /* pcibios will clear the counter; save the value */
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000638 cnt = pe->freeze_count;
Gavin Shan5a719782013-06-20 13:21:01 +0800639 tstamp = pe->tstamp;
Linas Vepstas42405452006-04-28 17:39:38 -0500640
Gavin Shan20ee6a92012-09-11 19:16:17 +0000641 /*
642 * We don't remove the corresponding PE instances because
643 * we need the information afterwords. The attached EEH
644 * devices are expected to be attached soon when calling
Gavin Shanbd251b82016-05-03 15:41:37 +1000645 * into pci_hp_add_devices().
Gavin Shan20ee6a92012-09-11 19:16:17 +0000646 */
Gavin Shanf5c57712013-07-24 10:24:58 +0800647 eeh_pe_state_mark(pe, EEH_PE_KEEP);
Rafael J. Wysocki1c2042c2014-01-15 14:33:20 +0100648 if (bus) {
Wei Yang67086e32016-03-04 10:53:11 +1100649 if (pe->type & EEH_PE_VF) {
650 eeh_pe_dev_traverse(pe, eeh_rmv_device, NULL);
651 } else {
Wei Yang67086e32016-03-04 10:53:11 +1100652 pci_lock_rescan_remove();
Gavin Shanbd251b82016-05-03 15:41:37 +1000653 pci_hp_remove_devices(bus);
Wei Yang67086e32016-03-04 10:53:11 +1100654 pci_unlock_rescan_remove();
655 }
Rafael J. Wysocki1c2042c2014-01-15 14:33:20 +0100656 } else if (frozen_bus) {
Gavin Shancca0e5422016-06-24 14:49:02 +1000657 eeh_pe_dev_traverse(pe, eeh_rmv_device, rmv_data);
Rafael J. Wysocki1c2042c2014-01-15 14:33:20 +0100658 }
Linas Vepstas77bd7412005-11-03 18:52:49 -0600659
Gavin Shand0914f52014-04-24 18:00:12 +1000660 /*
661 * Reset the pci controller. (Asserts RST#; resets config space).
Linas Vepstasb6495c02005-11-03 18:54:54 -0600662 * Reconfigure bridges and devices. Don't try to bring the system
Gavin Shan29f8bf12012-02-27 20:04:02 +0000663 * up if the reset failed for some reason.
Gavin Shand0914f52014-04-24 18:00:12 +1000664 *
665 * During the reset, it's very dangerous to have uncontrolled PCI
666 * config accesses. So we prefer to block them. However, controlled
667 * PCI config accesses initiated from EEH itself are allowed.
Gavin Shan29f8bf12012-02-27 20:04:02 +0000668 */
Russell Currey6654c932016-11-17 16:07:47 +1100669 rc = eeh_pe_reset_full(pe);
Gavin Shan28bf36f2014-11-14 10:47:29 +1100670 if (rc)
Linas Vepstasb6495c02005-11-03 18:54:54 -0600671 return rc;
Linas Vepstas77bd7412005-11-03 18:52:49 -0600672
Rafael J. Wysocki1c2042c2014-01-15 14:33:20 +0100673 pci_lock_rescan_remove();
674
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000675 /* Restore PE */
676 eeh_ops->configure_bridge(pe);
677 eeh_pe_restore_bars(pe);
Linas Vepstas77bd7412005-11-03 18:52:49 -0600678
Andrew Donnellandc9c41b2015-12-08 16:59:25 +1100679 /* Clear frozen state */
680 rc = eeh_clear_pe_frozen_state(pe, false);
Andrew Donnellan409bf7f2016-12-01 11:23:05 +1100681 if (rc) {
682 pci_unlock_rescan_remove();
Andrew Donnellandc9c41b2015-12-08 16:59:25 +1100683 return rc;
Andrew Donnellan409bf7f2016-12-01 11:23:05 +1100684 }
Gavin Shan78954702014-04-24 18:00:14 +1000685
Linas Vepstas77bd7412005-11-03 18:52:49 -0600686 /* Give the system 5 seconds to finish running the user-space
Gavin Shana84f2732013-06-20 13:20:51 +0800687 * hotplug shutdown scripts, e.g. ifdown for ethernet. Yes,
688 * this is a hack, but if we don't do this, and try to bring
689 * the device up before the scripts have taken it down,
Linas Vepstas77bd7412005-11-03 18:52:49 -0600690 * potentially weird things happen.
691 */
692 if (bus) {
Gavin Shanf5c57712013-07-24 10:24:58 +0800693 pr_info("EEH: Sleep 5s ahead of complete hotplug\n");
Gavin Shan29f8bf12012-02-27 20:04:02 +0000694 ssleep(5);
Gavin Shanf5c57712013-07-24 10:24:58 +0800695
696 /*
697 * The EEH device is still connected with its parent
698 * PE. We should disconnect it so the binding can be
699 * rebuilt when adding PCI devices.
700 */
Wei Yang67086e32016-03-04 10:53:11 +1100701 edev = list_first_entry(&pe->edevs, struct eeh_dev, list);
Gavin Shanf5c57712013-07-24 10:24:58 +0800702 eeh_pe_traverse(pe, eeh_pe_detach_dev, NULL);
Gavin Shana3aa2562016-06-17 13:05:11 +1000703 if (pe->type & EEH_PE_VF) {
Wei Yang67086e32016-03-04 10:53:11 +1100704 eeh_add_virt_device(edev, NULL);
Gavin Shana3aa2562016-06-17 13:05:11 +1000705 } else {
706 eeh_pe_state_clear(pe, EEH_PE_PRI_BUS);
Gavin Shanbd251b82016-05-03 15:41:37 +1000707 pci_hp_add_devices(bus);
Gavin Shana3aa2562016-06-17 13:05:11 +1000708 }
Wei Yang67086e32016-03-04 10:53:11 +1100709 } else if (frozen_bus && rmv_data->removed) {
Gavin Shanf5c57712013-07-24 10:24:58 +0800710 pr_info("EEH: Sleep 5s ahead of partial hotplug\n");
711 ssleep(5);
712
Wei Yang67086e32016-03-04 10:53:11 +1100713 edev = list_first_entry(&pe->edevs, struct eeh_dev, list);
Gavin Shanf5c57712013-07-24 10:24:58 +0800714 eeh_pe_traverse(pe, eeh_pe_detach_dev, NULL);
Wei Yang67086e32016-03-04 10:53:11 +1100715 if (pe->type & EEH_PE_VF)
716 eeh_add_virt_device(edev, NULL);
717 else
Gavin Shanbd251b82016-05-03 15:41:37 +1000718 pci_hp_add_devices(frozen_bus);
Linas Vepstas77bd7412005-11-03 18:52:49 -0600719 }
Gavin Shanf5c57712013-07-24 10:24:58 +0800720 eeh_pe_state_clear(pe, EEH_PE_KEEP);
Gavin Shan5a719782013-06-20 13:21:01 +0800721
722 pe->tstamp = tstamp;
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000723 pe->freeze_count = cnt;
Linas Vepstasb6495c02005-11-03 18:54:54 -0600724
Rafael J. Wysocki1c2042c2014-01-15 14:33:20 +0100725 pci_unlock_rescan_remove();
Linas Vepstasb6495c02005-11-03 18:54:54 -0600726 return 0;
Linas Vepstas77bd7412005-11-03 18:52:49 -0600727}
728
729/* The longest amount of time to wait for a pci device
730 * to come back on line, in seconds.
731 */
Brian Kingfb48dc22013-11-25 16:27:54 -0600732#define MAX_WAIT_FOR_RECOVERY 300
Linas Vepstas77bd7412005-11-03 18:52:49 -0600733
Russell Curreyc0b64972017-04-19 17:39:27 +1000734/**
735 * eeh_handle_normal_event - Handle EEH events on a specific PE
Sam Bobroff37fd8122018-03-19 13:46:30 +1100736 * @pe: EEH PE - which should not be used after we return, as it may
737 * have been invalidated.
Russell Curreyc0b64972017-04-19 17:39:27 +1000738 *
739 * Attempts to recover the given PE. If recovery fails or the PE has failed
740 * too many times, remove the PE.
741 *
Sam Bobroff68701782018-03-19 13:46:20 +1100742 * While PHB detects address or data parity errors on particular PCI
743 * slot, the associated PE will be frozen. Besides, DMA's occurring
744 * to wild addresses (which usually happen due to bugs in device
745 * drivers or in PCI adapter firmware) can cause EEH error. #SERR,
746 * #PERR or other misc PCI-related errors also can trigger EEH errors.
747 *
748 * Recovery process consists of unplugging the device driver (which
749 * generated hotplug events to userspace), then issuing a PCI #RST to
750 * the device, then reconfiguring the PCI config space for all bridges
751 * & devices under this slot, and then finally restarting the device
752 * drivers (which cause a second set of hotplug events to go out to
753 * userspace).
Russell Curreyc0b64972017-04-19 17:39:27 +1000754 */
Sam Bobroff37fd8122018-03-19 13:46:30 +1100755void eeh_handle_normal_event(struct eeh_pe *pe)
Linas Vepstas77bd7412005-11-03 18:52:49 -0600756{
Linas Vepstas77bd7412005-11-03 18:52:49 -0600757 struct pci_bus *frozen_bus;
Wei Yang67086e32016-03-04 10:53:11 +1100758 struct eeh_dev *edev, *tmp;
Linas Vepstasb6495c02005-11-03 18:54:54 -0600759 int rc = 0;
Paul Mackerras18eb3b32005-11-29 17:17:02 +1100760 enum pci_ers_result result = PCI_ERS_RESULT_NONE;
Wei Yang67086e32016-03-04 10:53:11 +1100761 struct eeh_rmv_data rmv_data = {LIST_HEAD_INIT(rmv_data.edev_list), 0};
Linas Vepstas77bd7412005-11-03 18:52:49 -0600762
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000763 frozen_bus = eeh_pe_bus_get(pe);
Linas Vepstas77bd7412005-11-03 18:52:49 -0600764 if (!frozen_bus) {
Russell Currey1f52f172016-11-16 14:02:15 +1100765 pr_err("%s: Cannot find PCI bus for PHB#%x-PE#%x\n",
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000766 __func__, pe->phb->global_number, pe->addr);
Sam Bobroff37fd8122018-03-19 13:46:30 +1100767 return;
Linas Vepstas77bd7412005-11-03 18:52:49 -0600768 }
769
Sam Bobroff37fd8122018-03-19 13:46:30 +1100770 eeh_pe_state_mark(pe, EEH_PE_RECOVERING);
771
Gavin Shan5a719782013-06-20 13:21:01 +0800772 eeh_pe_update_time_stamp(pe);
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000773 pe->freeze_count++;
Russell Curreyc0b64972017-04-19 17:39:27 +1000774 if (pe->freeze_count > eeh_max_freezes) {
775 pr_err("EEH: PHB#%x-PE#%x has failed %d times in the\n"
776 "last hour and has been permanently disabled.\n",
777 pe->phb->global_number, pe->addr,
778 pe->freeze_count);
779 goto hard_fail;
780 }
Gavin Shan0dae2742014-07-17 14:41:41 +1000781 pr_warn("EEH: This PCI device has failed %d times in the last hour\n",
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000782 pe->freeze_count);
Linas Vepstas77bd7412005-11-03 18:52:49 -0600783
784 /* Walk the various device drivers attached to this slot through
785 * a reset sequence, giving each an opportunity to do what it needs
786 * to accomplish the reset. Each child gets a report of the
787 * status ... if any child can't handle the reset, then the entire
788 * slot is dlpar removed and added.
Gavin Shan8234fce2015-10-08 14:58:54 +1100789 *
790 * When the PHB is fenced, we have to issue a reset to recover from
791 * the error. Override the result if necessary to have partially
792 * hotplug for this case.
Linas Vepstas77bd7412005-11-03 18:52:49 -0600793 */
Gavin Shan56ca4fd2013-06-27 13:46:46 +0800794 pr_info("EEH: Notify device drivers to shutdown\n");
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000795 eeh_pe_dev_traverse(pe, eeh_report_error, &result);
Gavin Shan8234fce2015-10-08 14:58:54 +1100796 if ((pe->type & EEH_PE_PHB) &&
797 result != PCI_ERS_RESULT_NONE &&
798 result != PCI_ERS_RESULT_NEED_RESET)
799 result = PCI_ERS_RESULT_NEED_RESET;
Linas Vepstas77bd7412005-11-03 18:52:49 -0600800
Linas Vepstas5f1a7c82007-11-16 05:58:36 +1100801 /* Get the current PCI slot state. This can take a long time,
Wei Yang2ac39902015-04-27 09:25:10 +0800802 * sometimes over 300 seconds for certain systems.
Gavin Shan29f8bf12012-02-27 20:04:02 +0000803 */
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000804 rc = eeh_ops->wait_state(pe, MAX_WAIT_FOR_RECOVERY*1000);
Gavin Shaneb594a42012-02-27 20:03:57 +0000805 if (rc < 0 || rc == EEH_STATE_NOT_SUPPORT) {
Gavin Shan0dae2742014-07-17 14:41:41 +1000806 pr_warn("EEH: Permanent failure\n");
Linas Vepstas5f1a7c82007-11-16 05:58:36 +1100807 goto hard_fail;
808 }
809
Linas Vepstasede8ca22007-05-09 09:33:29 +1000810 /* Since rtas may enable MMIO when posting the error log,
811 * don't post the error log until after all dev drivers
Linas Vepstas17213c32007-05-10 02:38:11 +1000812 * have been informed.
813 */
Gavin Shan56ca4fd2013-06-27 13:46:46 +0800814 pr_info("EEH: Collect temporary log\n");
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000815 eeh_slot_error_detail(pe, EEH_LOG_TEMP);
Linas Vepstasede8ca22007-05-09 09:33:29 +1000816
Linas Vepstas77bd7412005-11-03 18:52:49 -0600817 /* If all device drivers were EEH-unaware, then shut
818 * down all of the device drivers, and hope they
819 * go down willingly, without panicing the system.
820 */
Paul Mackerras18eb3b32005-11-29 17:17:02 +1100821 if (result == PCI_ERS_RESULT_NONE) {
Gavin Shan56ca4fd2013-06-27 13:46:46 +0800822 pr_info("EEH: Reset with hotplug activity\n");
Wei Yang67086e32016-03-04 10:53:11 +1100823 rc = eeh_reset_device(pe, frozen_bus, NULL);
Linas Vepstase0f90b62007-03-19 14:52:04 -0500824 if (rc) {
Gavin Shan0dae2742014-07-17 14:41:41 +1000825 pr_warn("%s: Unable to reset, err=%d\n",
826 __func__, rc);
Linas Vepstasb6495c02005-11-03 18:54:54 -0600827 goto hard_fail;
Linas Vepstase0f90b62007-03-19 14:52:04 -0500828 }
Linas Vepstas77bd7412005-11-03 18:52:49 -0600829 }
830
Linas Vepstas6a1ca372006-09-15 18:58:59 -0500831 /* If all devices reported they can proceed, then re-enable MMIO */
832 if (result == PCI_ERS_RESULT_CAN_RECOVER) {
Gavin Shan56ca4fd2013-06-27 13:46:46 +0800833 pr_info("EEH: Enable I/O for affected devices\n");
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000834 rc = eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
Linas Vepstas6a1ca372006-09-15 18:58:59 -0500835
Linas Vepstasfa1be472007-03-19 14:59:59 -0500836 if (rc < 0)
837 goto hard_fail;
Linas Vepstas6a1ca372006-09-15 18:58:59 -0500838 if (rc) {
839 result = PCI_ERS_RESULT_NEED_RESET;
840 } else {
Gavin Shan56ca4fd2013-06-27 13:46:46 +0800841 pr_info("EEH: Notify device drivers to resume I/O\n");
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000842 eeh_pe_dev_traverse(pe, eeh_report_mmio_enabled, &result);
Linas Vepstas6a1ca372006-09-15 18:58:59 -0500843 }
844 }
845
846 /* If all devices reported they can proceed, then re-enable DMA */
847 if (result == PCI_ERS_RESULT_CAN_RECOVER) {
Gavin Shan56ca4fd2013-06-27 13:46:46 +0800848 pr_info("EEH: Enabled DMA for affected devices\n");
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000849 rc = eeh_pci_enable(pe, EEH_OPT_THAW_DMA);
Linas Vepstas6a1ca372006-09-15 18:58:59 -0500850
Linas Vepstasfa1be472007-03-19 14:59:59 -0500851 if (rc < 0)
852 goto hard_fail;
Gavin Shan35845a72014-04-24 18:00:26 +1000853 if (rc) {
Linas Vepstas6a1ca372006-09-15 18:58:59 -0500854 result = PCI_ERS_RESULT_NEED_RESET;
Gavin Shan35845a72014-04-24 18:00:26 +1000855 } else {
856 /*
857 * We didn't do PE reset for the case. The PE
858 * is still in frozen state. Clear it before
859 * resuming the PE.
860 */
861 eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
Linas Vepstasd0e70342006-12-06 12:32:20 -0600862 result = PCI_ERS_RESULT_RECOVERED;
Gavin Shan35845a72014-04-24 18:00:26 +1000863 }
Linas Vepstas6a1ca372006-09-15 18:58:59 -0500864 }
865
866 /* If any device has a hard failure, then shut off everything. */
Linas Vepstase0f90b62007-03-19 14:52:04 -0500867 if (result == PCI_ERS_RESULT_DISCONNECT) {
Gavin Shan0dae2742014-07-17 14:41:41 +1000868 pr_warn("EEH: Device driver gave up\n");
Linas Vepstas6a1ca372006-09-15 18:58:59 -0500869 goto hard_fail;
Linas Vepstase0f90b62007-03-19 14:52:04 -0500870 }
Linas Vepstas6a1ca372006-09-15 18:58:59 -0500871
Linas Vepstas77bd7412005-11-03 18:52:49 -0600872 /* If any device called out for a reset, then reset the slot */
Paul Mackerras18eb3b32005-11-29 17:17:02 +1100873 if (result == PCI_ERS_RESULT_NEED_RESET) {
Gavin Shan56ca4fd2013-06-27 13:46:46 +0800874 pr_info("EEH: Reset without hotplug activity\n");
Wei Yang67086e32016-03-04 10:53:11 +1100875 rc = eeh_reset_device(pe, NULL, &rmv_data);
Linas Vepstase0f90b62007-03-19 14:52:04 -0500876 if (rc) {
Gavin Shan0dae2742014-07-17 14:41:41 +1000877 pr_warn("%s: Cannot reset, err=%d\n",
878 __func__, rc);
Linas Vepstasb6495c02005-11-03 18:54:54 -0600879 goto hard_fail;
Linas Vepstase0f90b62007-03-19 14:52:04 -0500880 }
Gavin Shan56ca4fd2013-06-27 13:46:46 +0800881
882 pr_info("EEH: Notify device drivers "
883 "the completion of reset\n");
Linas Vepstas6a1ca372006-09-15 18:58:59 -0500884 result = PCI_ERS_RESULT_NONE;
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000885 eeh_pe_dev_traverse(pe, eeh_report_reset, &result);
Linas Vepstas77bd7412005-11-03 18:52:49 -0600886 }
887
Linas Vepstas6a1ca372006-09-15 18:58:59 -0500888 /* All devices should claim they have recovered by now. */
Linas Vepstas90fdd612007-03-19 14:55:10 -0500889 if ((result != PCI_ERS_RESULT_RECOVERED) &&
890 (result != PCI_ERS_RESULT_NONE)) {
Gavin Shan0dae2742014-07-17 14:41:41 +1000891 pr_warn("EEH: Not recovered\n");
Linas Vepstas6a1ca372006-09-15 18:58:59 -0500892 goto hard_fail;
Linas Vepstase0f90b62007-03-19 14:52:04 -0500893 }
Linas Vepstas77bd7412005-11-03 18:52:49 -0600894
Wei Yang67086e32016-03-04 10:53:11 +1100895 /*
896 * For those hot removed VFs, we should add back them after PF get
897 * recovered properly.
898 */
899 list_for_each_entry_safe(edev, tmp, &rmv_data.edev_list, rmv_list) {
900 eeh_add_virt_device(edev, NULL);
901 list_del(&edev->rmv_list);
902 }
903
Linas Vepstas77bd7412005-11-03 18:52:49 -0600904 /* Tell all device drivers that they can resume operations */
Gavin Shan56ca4fd2013-06-27 13:46:46 +0800905 pr_info("EEH: Notify device driver to resume\n");
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000906 eeh_pe_dev_traverse(pe, eeh_report_resume, NULL);
Linas Vepstasb6495c02005-11-03 18:54:54 -0600907
Sam Bobroff37fd8122018-03-19 13:46:30 +1100908 goto final;
Gavin Shana84f2732013-06-20 13:20:51 +0800909
Russell Curreyc0b64972017-04-19 17:39:27 +1000910hard_fail:
Linas Vepstasb6495c02005-11-03 18:54:54 -0600911 /*
912 * About 90% of all real-life EEH failures in the field
913 * are due to poorly seated PCI cards. Only 10% or so are
914 * due to actual, failed cards.
915 */
Russell Currey1f52f172016-11-16 14:02:15 +1100916 pr_err("EEH: Unable to recover from failure from PHB#%x-PE#%x.\n"
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000917 "Please try reseating or replacing it\n",
918 pe->phb->global_number, pe->addr);
Linas Vepstas8df83022006-03-29 15:31:04 -0600919
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000920 eeh_slot_error_detail(pe, EEH_LOG_PERM);
Linas Vepstasb6495c02005-11-03 18:54:54 -0600921
922 /* Notify all devices that they're about to go down. */
Gavin Shan9b3c76f2012-09-07 22:44:19 +0000923 eeh_pe_dev_traverse(pe, eeh_report_failure, NULL);
Linas Vepstasb6495c02005-11-03 18:54:54 -0600924
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000925 /* Mark the PE to be removed permanently */
Gavin Shan432227e2014-12-11 14:28:55 +1100926 eeh_pe_state_mark(pe, EEH_PE_REMOVED);
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000927
928 /*
929 * Shut down the device drivers for good. We mark
930 * all removed devices correctly to avoid access
931 * the their PCI config any more.
932 */
Sam Bobroff5b86ac92018-03-19 13:46:51 +1100933 if (pe->type & EEH_PE_VF) {
934 eeh_pe_dev_traverse(pe, eeh_rmv_device, NULL);
935 eeh_pe_dev_mode_mark(pe, EEH_DEV_REMOVED);
936 } else {
937 eeh_pe_state_clear(pe, EEH_PE_PRI_BUS);
938 eeh_pe_dev_mode_mark(pe, EEH_DEV_REMOVED);
Gavin Shand2b0f6f2014-04-24 18:00:19 +1000939
Sam Bobroff5b86ac92018-03-19 13:46:51 +1100940 pci_lock_rescan_remove();
941 pci_hp_remove_devices(frozen_bus);
942 pci_unlock_rescan_remove();
943 /* The passed PE should no longer be used */
944 return;
Rafael J. Wysocki1c2042c2014-01-15 14:33:20 +0100945 }
Sam Bobroff37fd8122018-03-19 13:46:30 +1100946final:
947 eeh_pe_state_clear(pe, EEH_PE_RECOVERING);
Linas Vepstas77bd7412005-11-03 18:52:49 -0600948}
Gavin Shan8a6b1bc2013-06-20 13:21:04 +0800949
Russell Curreyc0b64972017-04-19 17:39:27 +1000950/**
951 * eeh_handle_special_event - Handle EEH events without a specific failing PE
952 *
953 * Called when an EEH event is detected but can't be narrowed down to a
954 * specific PE. Iterates through possible failures and handles them as
955 * necessary.
956 */
Sam Bobroff68701782018-03-19 13:46:20 +1100957void eeh_handle_special_event(void)
Gavin Shan8a6b1bc2013-06-20 13:21:04 +0800958{
959 struct eeh_pe *pe, *phb_pe;
960 struct pci_bus *bus;
Gavin Shan7e4e7862014-01-15 13:16:11 +0800961 struct pci_controller *hose;
Gavin Shan8a6b1bc2013-06-20 13:21:04 +0800962 unsigned long flags;
Gavin Shan7e4e7862014-01-15 13:16:11 +0800963 int rc;
Gavin Shan8a6b1bc2013-06-20 13:21:04 +0800964
Gavin Shan8a6b1bc2013-06-20 13:21:04 +0800965
Gavin Shan7e4e7862014-01-15 13:16:11 +0800966 do {
967 rc = eeh_ops->next_error(&pe);
Gavin Shan8a6b1bc2013-06-20 13:21:04 +0800968
Gavin Shan7e4e7862014-01-15 13:16:11 +0800969 switch (rc) {
970 case EEH_NEXT_ERR_DEAD_IOC:
971 /* Mark all PHBs in dead state */
972 eeh_serialize_lock(&flags);
973
974 /* Purge all events */
Gavin Shan5c7a35e2014-06-04 17:31:52 +1000975 eeh_remove_event(NULL, true);
Gavin Shan7e4e7862014-01-15 13:16:11 +0800976
977 list_for_each_entry(hose, &hose_list, list_node) {
978 phb_pe = eeh_phb_pe_get(hose);
979 if (!phb_pe) continue;
980
Gavin Shan9e049372014-04-24 18:00:07 +1000981 eeh_pe_state_mark(phb_pe, EEH_PE_ISOLATED);
Gavin Shan7e4e7862014-01-15 13:16:11 +0800982 }
983
984 eeh_serialize_unlock(flags);
985
986 break;
987 case EEH_NEXT_ERR_FROZEN_PE:
988 case EEH_NEXT_ERR_FENCED_PHB:
989 case EEH_NEXT_ERR_DEAD_PHB:
990 /* Mark the PE in fenced state */
991 eeh_serialize_lock(&flags);
992
993 /* Purge all events of the PHB */
Gavin Shan5c7a35e2014-06-04 17:31:52 +1000994 eeh_remove_event(pe, true);
Gavin Shan7e4e7862014-01-15 13:16:11 +0800995
996 if (rc == EEH_NEXT_ERR_DEAD_PHB)
Gavin Shan9e049372014-04-24 18:00:07 +1000997 eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
Gavin Shan7e4e7862014-01-15 13:16:11 +0800998 else
999 eeh_pe_state_mark(pe,
1000 EEH_PE_ISOLATED | EEH_PE_RECOVERING);
1001
1002 eeh_serialize_unlock(flags);
1003
1004 break;
1005 case EEH_NEXT_ERR_NONE:
1006 return;
1007 default:
1008 pr_warn("%s: Invalid value %d from next_error()\n",
1009 __func__, rc);
1010 return;
Gavin Shan8a6b1bc2013-06-20 13:21:04 +08001011 }
Gavin Shan8a6b1bc2013-06-20 13:21:04 +08001012
Gavin Shan7e4e7862014-01-15 13:16:11 +08001013 /*
1014 * For fenced PHB and frozen PE, it's handled as normal
1015 * event. We have to remove the affected PHBs for dead
1016 * PHB and IOC
1017 */
1018 if (rc == EEH_NEXT_ERR_FROZEN_PE ||
1019 rc == EEH_NEXT_ERR_FENCED_PHB) {
Sam Bobroff37fd8122018-03-19 13:46:30 +11001020 eeh_handle_normal_event(pe);
Gavin Shan7e4e7862014-01-15 13:16:11 +08001021 } else {
Linus Torvalds1b173662014-01-27 21:11:26 -08001022 pci_lock_rescan_remove();
Gavin Shan7e4e7862014-01-15 13:16:11 +08001023 list_for_each_entry(hose, &hose_list, list_node) {
1024 phb_pe = eeh_phb_pe_get(hose);
1025 if (!phb_pe ||
Gavin Shan9e049372014-04-24 18:00:07 +10001026 !(phb_pe->state & EEH_PE_ISOLATED) ||
1027 (phb_pe->state & EEH_PE_RECOVERING))
Gavin Shan7e4e7862014-01-15 13:16:11 +08001028 continue;
Gavin Shan8a6b1bc2013-06-20 13:21:04 +08001029
Gavin Shan7e4e7862014-01-15 13:16:11 +08001030 /* Notify all devices to be down */
Gavin Shan05ba75f2016-02-09 15:50:21 +11001031 eeh_pe_state_clear(pe, EEH_PE_PRI_BUS);
Russell Curreyaf2e3a02016-09-12 14:17:24 +10001032 eeh_pe_dev_traverse(pe,
1033 eeh_report_failure, NULL);
Gavin Shan7e4e7862014-01-15 13:16:11 +08001034 bus = eeh_pe_bus_get(phb_pe);
Russell Currey04fec21c2016-09-12 14:17:22 +10001035 if (!bus) {
1036 pr_err("%s: Cannot find PCI bus for "
Russell Currey1f52f172016-11-16 14:02:15 +11001037 "PHB#%x-PE#%x\n",
Russell Currey04fec21c2016-09-12 14:17:22 +10001038 __func__,
1039 pe->phb->global_number,
1040 pe->addr);
1041 break;
1042 }
Gavin Shanbd251b82016-05-03 15:41:37 +10001043 pci_hp_remove_devices(bus);
Gavin Shan7e4e7862014-01-15 13:16:11 +08001044 }
Linus Torvalds1b173662014-01-27 21:11:26 -08001045 pci_unlock_rescan_remove();
Gavin Shan8a6b1bc2013-06-20 13:21:04 +08001046 }
Gavin Shan7e4e7862014-01-15 13:16:11 +08001047
1048 /*
1049 * If we have detected dead IOC, we needn't proceed
1050 * any more since all PHBs would have been removed
1051 */
1052 if (rc == EEH_NEXT_ERR_DEAD_IOC)
1053 break;
1054 } while (rc != EEH_NEXT_ERR_NONE);
Gavin Shan8a6b1bc2013-06-20 13:21:04 +08001055}