blob: 309d38ef732209ce68ec99380ef09b97d5bfc41f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linas Vepstas3c8c90a2007-05-24 03:28:01 +10002 * Copyright IBM Corporation 2001, 2005, 2006
3 * Copyright Dave Engebretsen & Todd Inglett 2001
4 * Copyright Linas Vepstas 2005, 2006
Gavin Shancb3bc9d2012-02-27 20:03:51 +00005 * Copyright 2001-2012 IBM Corporation.
Linas Vepstas69376502005-11-03 18:47:50 -06006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
Linas Vepstas69376502005-11-03 18:47:50 -060011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Linas Vepstas69376502005-11-03 18:47:50 -060016 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Linas Vepstas3c8c90a2007-05-24 03:28:01 +100020 *
21 * Please address comments and feedback to Linas Vepstas <linas@austin.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
23
Linas Vepstas6dee3fb2005-11-03 18:50:10 -060024#include <linux/delay.h>
Gavin Shancb3bc9d2012-02-27 20:03:51 +000025#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/init.h>
27#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/pci.h>
29#include <linux/proc_fs.h>
30#include <linux/rbtree.h>
31#include <linux/seq_file.h>
32#include <linux/spinlock.h>
Paul Gortmaker66b15db2011-05-27 10:46:24 -040033#include <linux/export.h>
Stephen Rothwellacaa6172007-12-21 15:52:07 +110034#include <linux/of.h>
35
Arun Sharma600634972011-07-26 16:09:06 -070036#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/eeh.h>
Linas Vepstas172ca922005-11-03 18:50:04 -060038#include <asm/eeh_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/io.h>
40#include <asm/machdep.h>
Stephen Rothwelld3878992005-09-28 02:50:25 +100041#include <asm/ppc-pci.h>
Linas Vepstas172ca922005-11-03 18:50:04 -060042#include <asm/rtas.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45/** Overview:
46 * EEH, or "Extended Error Handling" is a PCI bridge technology for
47 * dealing with PCI bus errors that can't be dealt with within the
48 * usual PCI framework, except by check-stopping the CPU. Systems
49 * that are designed for high-availability/reliability cannot afford
50 * to crash due to a "mere" PCI error, thus the need for EEH.
51 * An EEH-capable bridge operates by converting a detected error
52 * into a "slot freeze", taking the PCI adapter off-line, making
53 * the slot behave, from the OS'es point of view, as if the slot
54 * were "empty": all reads return 0xff's and all writes are silently
55 * ignored. EEH slot isolation events can be triggered by parity
56 * errors on the address or data busses (e.g. during posted writes),
Linas Vepstas69376502005-11-03 18:47:50 -060057 * which in turn might be caused by low voltage on the bus, dust,
58 * vibration, humidity, radioactivity or plain-old failed hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 *
60 * Note, however, that one of the leading causes of EEH slot
61 * freeze events are buggy device drivers, buggy device microcode,
62 * or buggy device hardware. This is because any attempt by the
63 * device to bus-master data to a memory address that is not
64 * assigned to the device will trigger a slot freeze. (The idea
65 * is to prevent devices-gone-wild from corrupting system memory).
66 * Buggy hardware/drivers will have a miserable time co-existing
67 * with EEH.
68 *
69 * Ideally, a PCI device driver, when suspecting that an isolation
Lucas De Marchi25985ed2011-03-30 22:57:33 -030070 * event has occurred (e.g. by reading 0xff's), will then ask EEH
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 * whether this is the case, and then take appropriate steps to
72 * reset the PCI slot, the PCI device, and then resume operations.
73 * However, until that day, the checking is done here, with the
74 * eeh_check_failure() routine embedded in the MMIO macros. If
75 * the slot is found to be isolated, an "EEH Event" is synthesized
76 * and sent out for processing.
77 */
78
Linas Vepstas5c1344e2005-11-03 18:49:31 -060079/* If a device driver keeps reading an MMIO register in an interrupt
Mike Masonf36c5222008-07-22 02:40:17 +100080 * handler after a slot isolation event, it might be broken.
81 * This sets the threshold for how many read attempts we allow
82 * before printing an error message.
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 */
Linas Vepstas2fd30be2007-03-19 14:53:22 -050084#define EEH_MAX_FAILS 2100000
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Linas Vepstas17213c32007-05-10 02:38:11 +100086/* Time to wait for a PCI slot to report status, in milliseconds */
Linas Vepstas9c547762007-03-19 14:58:07 -050087#define PCI_BUS_RESET_WAIT_MSEC (60*1000)
88
Gavin Shanaa1e6372012-02-27 20:03:53 +000089/* Platform dependent EEH operations */
90struct eeh_ops *eeh_ops = NULL;
91
David Woodhouse1e28a7d2005-11-17 00:44:03 +000092int eeh_subsystem_enabled;
93EXPORT_SYMBOL(eeh_subsystem_enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Linas Vepstasfd761fd2005-11-03 18:49:23 -060095/* Lock to avoid races due to multiple reports of an error */
Thomas Gleixner3d372622010-02-18 02:23:07 +000096static DEFINE_RAW_SPINLOCK(confirm_error_lock);
Linas Vepstasfd761fd2005-11-03 18:49:23 -060097
Linas Vepstas17213c32007-05-10 02:38:11 +100098/* Buffer for reporting pci register dumps. Its here in BSS, and
99 * not dynamically alloced, so that it ends up in RMO where RTAS
100 * can access it.
101 */
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000102#define EEH_PCI_REGS_LOG_LEN 4096
103static unsigned char pci_regs_buf[EEH_PCI_REGS_LOG_LEN];
104
Gavin Shane575f8d2012-02-29 15:47:45 +0000105/*
106 * The struct is used to maintain the EEH global statistic
107 * information. Besides, the EEH global statistics will be
108 * exported to user space through procfs
109 */
110struct eeh_stats {
111 u64 no_device; /* PCI device not found */
112 u64 no_dn; /* OF node not found */
113 u64 no_cfg_addr; /* Config address not found */
114 u64 ignored_check; /* EEH check skipped */
115 u64 total_mmio_ffs; /* Total EEH checks */
116 u64 false_positives; /* Unnecessary EEH checks */
117 u64 slot_resets; /* PE reset */
118};
119
120static struct eeh_stats eeh_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Linas Vepstas7684b402005-11-03 18:55:19 -0600122#define IS_BRIDGE(class_code) (((class_code)<<16) == PCI_BASE_CLASS_BRIDGE)
123
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000124/**
Gavin Shancce4b2d2012-02-27 20:03:52 +0000125 * eeh_gather_pci_data - Copy assorted PCI config space registers to buff
Gavin Shanf631acd2012-02-27 20:04:07 +0000126 * @edev: device to report data for
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000127 * @buf: point to buffer in which to log
128 * @len: amount of room in buffer
129 *
130 * This routine captures assorted PCI configuration space data,
131 * and puts them into a buffer for RTAS error logging.
132 */
Gavin Shanf631acd2012-02-27 20:04:07 +0000133static size_t eeh_gather_pci_data(struct eeh_dev *edev, char * buf, size_t len)
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000134{
Gavin Shanf631acd2012-02-27 20:04:07 +0000135 struct device_node *dn = eeh_dev_to_of_node(edev);
136 struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000137 u32 cfg;
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000138 int cap, i;
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000139 int n = 0;
140
Gavin Shanf631acd2012-02-27 20:04:07 +0000141 n += scnprintf(buf+n, len-n, "%s\n", dn->full_name);
142 printk(KERN_WARNING "EEH: of node=%s\n", dn->full_name);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000143
Gavin Shan37804442012-02-27 20:04:11 +0000144 eeh_ops->read_config(dn, PCI_VENDOR_ID, 4, &cfg);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000145 n += scnprintf(buf+n, len-n, "dev/vend:%08x\n", cfg);
146 printk(KERN_WARNING "EEH: PCI device/vendor: %08x\n", cfg);
147
Gavin Shan37804442012-02-27 20:04:11 +0000148 eeh_ops->read_config(dn, PCI_COMMAND, 4, &cfg);
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000149 n += scnprintf(buf+n, len-n, "cmd/stat:%x\n", cfg);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000150 printk(KERN_WARNING "EEH: PCI cmd/status register: %08x\n", cfg);
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000151
Linas Vepstasb37ceef2007-11-03 07:29:01 +1100152 if (!dev) {
153 printk(KERN_WARNING "EEH: no PCI device for this of node\n");
154 return n;
155 }
156
Linas Vepstas0b9369f2007-07-27 08:35:40 +1000157 /* Gather bridge-specific registers */
158 if (dev->class >> 16 == PCI_BASE_CLASS_BRIDGE) {
Gavin Shan37804442012-02-27 20:04:11 +0000159 eeh_ops->read_config(dn, PCI_SEC_STATUS, 2, &cfg);
Linas Vepstas0b9369f2007-07-27 08:35:40 +1000160 n += scnprintf(buf+n, len-n, "sec stat:%x\n", cfg);
161 printk(KERN_WARNING "EEH: Bridge secondary status: %04x\n", cfg);
162
Gavin Shan37804442012-02-27 20:04:11 +0000163 eeh_ops->read_config(dn, PCI_BRIDGE_CONTROL, 2, &cfg);
Linas Vepstas0b9369f2007-07-27 08:35:40 +1000164 n += scnprintf(buf+n, len-n, "brdg ctl:%x\n", cfg);
165 printk(KERN_WARNING "EEH: Bridge control: %04x\n", cfg);
166 }
167
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000168 /* Dump out the PCI-X command and status regs */
Linas Vepstasb37ceef2007-11-03 07:29:01 +1100169 cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000170 if (cap) {
Gavin Shan37804442012-02-27 20:04:11 +0000171 eeh_ops->read_config(dn, cap, 4, &cfg);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000172 n += scnprintf(buf+n, len-n, "pcix-cmd:%x\n", cfg);
173 printk(KERN_WARNING "EEH: PCI-X cmd: %08x\n", cfg);
174
Gavin Shan37804442012-02-27 20:04:11 +0000175 eeh_ops->read_config(dn, cap+4, 4, &cfg);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000176 n += scnprintf(buf+n, len-n, "pcix-stat:%x\n", cfg);
177 printk(KERN_WARNING "EEH: PCI-X status: %08x\n", cfg);
178 }
179
180 /* If PCI-E capable, dump PCI-E cap 10, and the AER */
Linas Vepstasb37ceef2007-11-03 07:29:01 +1100181 cap = pci_find_capability(dev, PCI_CAP_ID_EXP);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000182 if (cap) {
183 n += scnprintf(buf+n, len-n, "pci-e cap10:\n");
184 printk(KERN_WARNING
185 "EEH: PCI-E capabilities and status follow:\n");
186
187 for (i=0; i<=8; i++) {
Gavin Shan37804442012-02-27 20:04:11 +0000188 eeh_ops->read_config(dn, cap+4*i, 4, &cfg);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000189 n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
190 printk(KERN_WARNING "EEH: PCI-E %02x: %08x\n", i, cfg);
191 }
192
Linas Vepstasb37ceef2007-11-03 07:29:01 +1100193 cap = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000194 if (cap) {
195 n += scnprintf(buf+n, len-n, "pci-e AER:\n");
196 printk(KERN_WARNING
197 "EEH: PCI-E AER capability register set follows:\n");
198
199 for (i=0; i<14; i++) {
Gavin Shan37804442012-02-27 20:04:11 +0000200 eeh_ops->read_config(dn, cap+4*i, 4, &cfg);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000201 n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
202 printk(KERN_WARNING "EEH: PCI-E AER %02x: %08x\n", i, cfg);
203 }
204 }
205 }
Linas Vepstas0b9369f2007-07-27 08:35:40 +1000206
207 /* Gather status on devices under the bridge */
208 if (dev->class >> 16 == PCI_BASE_CLASS_BRIDGE) {
Gavin Shanf631acd2012-02-27 20:04:07 +0000209 struct device_node *child;
Stephen Rothwellacaa6172007-12-21 15:52:07 +1100210
Gavin Shanf631acd2012-02-27 20:04:07 +0000211 for_each_child_of_node(dn, child) {
212 if (of_node_to_eeh_dev(child))
213 n += eeh_gather_pci_data(of_node_to_eeh_dev(child), buf+n, len-n);
Linas Vepstas0b9369f2007-07-27 08:35:40 +1000214 }
215 }
216
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000217 return n;
218}
219
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000220/**
221 * eeh_slot_error_detail - Generate combined log including driver log and error log
Gavin Shanf631acd2012-02-27 20:04:07 +0000222 * @edev: device to report error log for
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000223 * @severity: temporary or permanent error log
224 *
225 * This routine should be called to generate the combined log, which
226 * is comprised of driver log and error log. The driver log is figured
227 * out from the config space of the corresponding PCI device, while
228 * the error log is fetched through platform dependent function call.
229 */
Gavin Shanf631acd2012-02-27 20:04:07 +0000230void eeh_slot_error_detail(struct eeh_dev *edev, int severity)
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000231{
232 size_t loglen = 0;
Linas Vepstas17213c32007-05-10 02:38:11 +1000233 pci_regs_buf[0] = 0;
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000234
Gavin Shanf631acd2012-02-27 20:04:07 +0000235 eeh_pci_enable(edev, EEH_OPT_THAW_MMIO);
236 eeh_ops->configure_bridge(eeh_dev_to_of_node(edev));
237 eeh_restore_bars(edev);
238 loglen = eeh_gather_pci_data(edev, pci_regs_buf, EEH_PCI_REGS_LOG_LEN);
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000239
Gavin Shanf631acd2012-02-27 20:04:07 +0000240 eeh_ops->get_log(eeh_dev_to_of_node(edev), severity, pci_regs_buf, loglen);
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000241}
242
243/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000244 * eeh_token_to_phys - Convert EEH address token to phys address
245 * @token: I/O token, should be address in the form 0xA....
246 *
247 * This routine should be called to convert virtual I/O address
248 * to physical one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 */
250static inline unsigned long eeh_token_to_phys(unsigned long token)
251{
252 pte_t *ptep;
253 unsigned long pa;
254
David Gibson20cee162005-06-21 17:15:31 -0700255 ptep = find_linux_pte(init_mm.pgd, token);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (!ptep)
257 return token;
258 pa = pte_pfn(*ptep) << PAGE_SHIFT;
259
260 return pa | (token & (PAGE_SIZE-1));
261}
262
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000263/**
Gavin Shancce4b2d2012-02-27 20:03:52 +0000264 * eeh_find_device_pe - Retrieve the PE for the given device
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000265 * @dn: device node
266 *
267 * Return the PE under which this device lies
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600268 */
Gavin Shancce4b2d2012-02-27 20:03:52 +0000269struct device_node *eeh_find_device_pe(struct device_node *dn)
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600270{
Gavin Shanf631acd2012-02-27 20:04:07 +0000271 while (dn->parent && of_node_to_eeh_dev(dn->parent) &&
272 (of_node_to_eeh_dev(dn->parent)->mode & EEH_MODE_SUPPORTED)) {
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600273 dn = dn->parent;
274 }
275 return dn;
276}
277
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000278/**
279 * __eeh_mark_slot - Mark all child devices as failed
280 * @parent: parent device
281 * @mode_flag: failure flag
282 *
283 * Mark all devices that are children of this device as failed.
284 * Mark the device driver too, so that it can see the failure
285 * immediately; this is critical, since some drivers poll
286 * status registers in interrupts ... If a driver is polling,
287 * and the slot is frozen, then the driver can deadlock in
288 * an interrupt context, which is bad.
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600289 */
Stephen Rothwellacaa6172007-12-21 15:52:07 +1100290static void __eeh_mark_slot(struct device_node *parent, int mode_flag)
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600291{
Stephen Rothwellacaa6172007-12-21 15:52:07 +1100292 struct device_node *dn;
293
294 for_each_child_of_node(parent, dn) {
Gavin Shanf631acd2012-02-27 20:04:07 +0000295 if (of_node_to_eeh_dev(dn)) {
Linas Vepstas77bd7412005-11-03 18:52:49 -0600296 /* Mark the pci device driver too */
Gavin Shanf631acd2012-02-27 20:04:07 +0000297 struct pci_dev *dev = of_node_to_eeh_dev(dn)->pdev;
Olof Johanssonea183a952006-01-11 14:02:58 -0600298
Gavin Shanf631acd2012-02-27 20:04:07 +0000299 of_node_to_eeh_dev(dn)->mode |= mode_flag;
Olof Johanssonea183a952006-01-11 14:02:58 -0600300
Linas Vepstas77bd7412005-11-03 18:52:49 -0600301 if (dev && dev->driver)
302 dev->error_state = pci_channel_io_frozen;
303
Stephen Rothwellacaa6172007-12-21 15:52:07 +1100304 __eeh_mark_slot(dn, mode_flag);
Linas Vepstasd9564ad2005-11-03 18:50:48 -0600305 }
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600306 }
307}
308
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000309/**
310 * eeh_mark_slot - Mark the indicated device and its children as failed
311 * @dn: parent device
312 * @mode_flag: failure flag
313 *
314 * Mark the indicated device and its child devices as failed.
315 * The device drivers are marked as failed as well.
316 */
317void eeh_mark_slot(struct device_node *dn, int mode_flag)
Linas Vepstasd9564ad2005-11-03 18:50:48 -0600318{
Linas Vepstas022d51b2006-09-25 18:01:42 -0500319 struct pci_dev *dev;
Gavin Shancce4b2d2012-02-27 20:03:52 +0000320 dn = eeh_find_device_pe(dn);
Linas Vepstas3914ac72005-11-03 18:55:01 -0600321
322 /* Back up one, since config addrs might be shared */
Gavin Shanf631acd2012-02-27 20:04:07 +0000323 if (!pcibios_find_pci_bus(dn) && of_node_to_eeh_dev(dn->parent))
Linas Vepstas3914ac72005-11-03 18:55:01 -0600324 dn = dn->parent;
325
Gavin Shanf631acd2012-02-27 20:04:07 +0000326 of_node_to_eeh_dev(dn)->mode |= mode_flag;
Linas Vepstas022d51b2006-09-25 18:01:42 -0500327
328 /* Mark the pci device too */
Gavin Shanf631acd2012-02-27 20:04:07 +0000329 dev = of_node_to_eeh_dev(dn)->pdev;
Linas Vepstas022d51b2006-09-25 18:01:42 -0500330 if (dev)
331 dev->error_state = pci_channel_io_frozen;
332
Stephen Rothwellacaa6172007-12-21 15:52:07 +1100333 __eeh_mark_slot(dn, mode_flag);
Linas Vepstasd9564ad2005-11-03 18:50:48 -0600334}
335
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000336/**
337 * __eeh_clear_slot - Clear failure flag for the child devices
338 * @parent: parent device
339 * @mode_flag: flag to be cleared
340 *
341 * Clear failure flag for the child devices.
342 */
Stephen Rothwellacaa6172007-12-21 15:52:07 +1100343static void __eeh_clear_slot(struct device_node *parent, int mode_flag)
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600344{
Stephen Rothwellacaa6172007-12-21 15:52:07 +1100345 struct device_node *dn;
346
347 for_each_child_of_node(parent, dn) {
Gavin Shanf631acd2012-02-27 20:04:07 +0000348 if (of_node_to_eeh_dev(dn)) {
349 of_node_to_eeh_dev(dn)->mode &= ~mode_flag;
350 of_node_to_eeh_dev(dn)->check_count = 0;
Stephen Rothwellacaa6172007-12-21 15:52:07 +1100351 __eeh_clear_slot(dn, mode_flag);
Linas Vepstasd9564ad2005-11-03 18:50:48 -0600352 }
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600353 }
354}
355
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000356/**
357 * eeh_clear_slot - Clear failure flag for the indicated device and its children
358 * @dn: parent device
359 * @mode_flag: flag to be cleared
360 *
361 * Clear failure flag for the indicated device and its children.
362 */
363void eeh_clear_slot(struct device_node *dn, int mode_flag)
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600364{
365 unsigned long flags;
Thomas Gleixner3d372622010-02-18 02:23:07 +0000366 raw_spin_lock_irqsave(&confirm_error_lock, flags);
Linas Vepstas3914ac72005-11-03 18:55:01 -0600367
Gavin Shancce4b2d2012-02-27 20:03:52 +0000368 dn = eeh_find_device_pe(dn);
Linas Vepstas3914ac72005-11-03 18:55:01 -0600369
370 /* Back up one, since config addrs might be shared */
Gavin Shanf631acd2012-02-27 20:04:07 +0000371 if (!pcibios_find_pci_bus(dn) && of_node_to_eeh_dev(dn->parent))
Linas Vepstas3914ac72005-11-03 18:55:01 -0600372 dn = dn->parent;
373
Gavin Shanf631acd2012-02-27 20:04:07 +0000374 of_node_to_eeh_dev(dn)->mode &= ~mode_flag;
375 of_node_to_eeh_dev(dn)->check_count = 0;
Stephen Rothwellacaa6172007-12-21 15:52:07 +1100376 __eeh_clear_slot(dn, mode_flag);
Thomas Gleixner3d372622010-02-18 02:23:07 +0000377 raw_spin_unlock_irqrestore(&confirm_error_lock, flags);
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600378}
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000381 * eeh_dn_check_failure - Check if all 1's data is due to EEH slot freeze
382 * @dn: device node
383 * @dev: pci device, if known
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 *
385 * Check for an EEH failure for the given device node. Call this
386 * routine if the result of a read was all 0xff's and you want to
387 * find out if this is due to an EEH slot freeze. This routine
388 * will query firmware for the EEH status.
389 *
390 * Returns 0 if there has not been an EEH error; otherwise returns
Linas Vepstas69376502005-11-03 18:47:50 -0600391 * a non-zero value and queues up a slot isolation event notification.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 *
393 * It is safe to call this routine in an interrupt context.
394 */
395int eeh_dn_check_failure(struct device_node *dn, struct pci_dev *dev)
396{
397 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 unsigned long flags;
Gavin Shanf631acd2012-02-27 20:04:07 +0000399 struct eeh_dev *edev;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600400 int rc = 0;
Mike Masonf36c5222008-07-22 02:40:17 +1000401 const char *location;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Gavin Shane575f8d2012-02-29 15:47:45 +0000403 eeh_stats.total_mmio_ffs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 if (!eeh_subsystem_enabled)
406 return 0;
407
Linas Vepstas177bc932005-11-03 18:48:52 -0600408 if (!dn) {
Gavin Shane575f8d2012-02-29 15:47:45 +0000409 eeh_stats.no_dn++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return 0;
Linas Vepstas177bc932005-11-03 18:48:52 -0600411 }
Gavin Shancce4b2d2012-02-27 20:03:52 +0000412 dn = eeh_find_device_pe(dn);
Gavin Shanf631acd2012-02-27 20:04:07 +0000413 edev = of_node_to_eeh_dev(dn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 /* Access to IO BARs might get this far and still not want checking. */
Gavin Shanf631acd2012-02-27 20:04:07 +0000416 if (!(edev->mode & EEH_MODE_SUPPORTED) ||
417 edev->mode & EEH_MODE_NOCHECK) {
Gavin Shane575f8d2012-02-29 15:47:45 +0000418 eeh_stats.ignored_check++;
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +0000419 pr_debug("EEH: Ignored check (%x) for %s %s\n",
Gavin Shanf631acd2012-02-27 20:04:07 +0000420 edev->mode, eeh_pci_name(dev), dn->full_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return 0;
422 }
423
Gavin Shanf631acd2012-02-27 20:04:07 +0000424 if (!edev->config_addr && !edev->pe_config_addr) {
Gavin Shane575f8d2012-02-29 15:47:45 +0000425 eeh_stats.no_cfg_addr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return 0;
427 }
428
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600429 /* If we already have a pending isolation event for this
430 * slot, we know it's bad already, we don't need to check.
431 * Do this checking under a lock; as multiple PCI devices
432 * in one slot might report errors simultaneously, and we
433 * only want one error recovery routine running.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 */
Thomas Gleixner3d372622010-02-18 02:23:07 +0000435 raw_spin_lock_irqsave(&confirm_error_lock, flags);
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600436 rc = 1;
Gavin Shanf631acd2012-02-27 20:04:07 +0000437 if (edev->mode & EEH_MODE_ISOLATED) {
438 edev->check_count++;
439 if (edev->check_count % EEH_MAX_FAILS == 0) {
Mike Masonf36c5222008-07-22 02:40:17 +1000440 location = of_get_property(dn, "ibm,loc-code", NULL);
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000441 printk(KERN_ERR "EEH: %d reads ignored for recovering device at "
Mike Masonf36c5222008-07-22 02:40:17 +1000442 "location=%s driver=%s pci addr=%s\n",
Gavin Shanf631acd2012-02-27 20:04:07 +0000443 edev->check_count, location,
Thadeu Lima de Souza Cascardo778a7852012-01-11 09:09:58 +0000444 eeh_driver_name(dev), eeh_pci_name(dev));
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000445 printk(KERN_ERR "EEH: Might be infinite loop in %s driver\n",
Thadeu Lima de Souza Cascardo778a7852012-01-11 09:09:58 +0000446 eeh_driver_name(dev));
Linas Vepstas5c1344e2005-11-03 18:49:31 -0600447 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600449 goto dn_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
451
452 /*
453 * Now test for an EEH failure. This is VERY expensive.
454 * Note that the eeh_config_addr may be a parent device
455 * in the case of a device behind a bridge, or it may be
456 * function zero of a multi-function device.
457 * In any case they must share a common PHB.
458 */
Gavin Shanf631acd2012-02-27 20:04:07 +0000459 ret = eeh_ops->get_state(dn, NULL);
Linas Vepstas76e6faf2005-11-03 18:49:15 -0600460
Linas Vepstas39d16e22007-03-19 14:51:00 -0500461 /* Note that config-io to empty slots may fail;
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000462 * they are empty when they don't have children.
Gavin Shaneb594a42012-02-27 20:03:57 +0000463 * We will punt with the following conditions: Failure to get
464 * PE's state, EEH not support and Permanently unavailable
465 * state, PE is in good state.
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000466 */
Gavin Shaneb594a42012-02-27 20:03:57 +0000467 if ((ret < 0) ||
468 (ret == EEH_STATE_NOT_SUPPORT) ||
469 (ret & (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) ==
470 (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) {
Gavin Shane575f8d2012-02-29 15:47:45 +0000471 eeh_stats.false_positives++;
Gavin Shanf631acd2012-02-27 20:04:07 +0000472 edev->false_positives ++;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600473 rc = 0;
474 goto dn_unlock;
Linas Vepstas76e6faf2005-11-03 18:49:15 -0600475 }
476
Gavin Shane575f8d2012-02-29 15:47:45 +0000477 eeh_stats.slot_resets++;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600478
479 /* Avoid repeated reports of this failure, including problems
480 * with other functions on this device, and functions under
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000481 * bridges.
482 */
483 eeh_mark_slot(dn, EEH_MODE_ISOLATED);
Thomas Gleixner3d372622010-02-18 02:23:07 +0000484 raw_spin_unlock_irqrestore(&confirm_error_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Gavin Shan40a7cd92012-02-27 20:04:08 +0000486 eeh_send_failure_event(edev);
Linas Vepstas77bd7412005-11-03 18:52:49 -0600487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 /* Most EEH events are due to device driver bugs. Having
489 * a stack trace will help the device-driver authors figure
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000490 * out what happened. So print that out.
491 */
Linas Vepstas90375f52007-03-19 14:56:43 -0500492 dump_stack();
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600493 return 1;
494
495dn_unlock:
Thomas Gleixner3d372622010-02-18 02:23:07 +0000496 raw_spin_unlock_irqrestore(&confirm_error_lock, flags);
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600497 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600500EXPORT_SYMBOL_GPL(eeh_dn_check_failure);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000503 * eeh_check_failure - Check if all 1's data is due to EEH slot freeze
504 * @token: I/O token, should be address in the form 0xA....
505 * @val: value, should be all 1's (XXX why do we need this arg??)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 * Check for an EEH failure at the given token address. Call this
508 * routine if the result of a read was all 0xff's and you want to
509 * find out if this is due to an EEH slot freeze event. This routine
510 * will query firmware for the EEH status.
511 *
512 * Note this routine is safe to call in an interrupt context.
513 */
514unsigned long eeh_check_failure(const volatile void __iomem *token, unsigned long val)
515{
516 unsigned long addr;
517 struct pci_dev *dev;
518 struct device_node *dn;
519
520 /* Finding the phys addr + pci device; this is pretty quick. */
521 addr = eeh_token_to_phys((unsigned long __force) token);
Gavin Shandef9d832012-02-27 20:04:03 +0000522 dev = pci_addr_cache_get_device(addr);
Linas Vepstas177bc932005-11-03 18:48:52 -0600523 if (!dev) {
Gavin Shane575f8d2012-02-29 15:47:45 +0000524 eeh_stats.no_device++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return val;
Linas Vepstas177bc932005-11-03 18:48:52 -0600526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 dn = pci_device_to_OF_node(dev);
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000529 eeh_dn_check_failure(dn, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 pci_dev_put(dev);
532 return val;
533}
534
535EXPORT_SYMBOL(eeh_check_failure);
536
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600537
Linas Vepstascb5b56242006-09-15 18:56:35 -0500538/**
Gavin Shancce4b2d2012-02-27 20:03:52 +0000539 * eeh_pci_enable - Enable MMIO or DMA transfers for this slot
Gavin Shanf631acd2012-02-27 20:04:07 +0000540 * @edev: pci device node
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000541 *
542 * This routine should be called to reenable frozen MMIO or DMA
543 * so that it would work correctly again. It's useful while doing
544 * recovery or log collection on the indicated device.
Linas Vepstas47b5c832006-09-15 18:57:42 -0500545 */
Gavin Shanf631acd2012-02-27 20:04:07 +0000546int eeh_pci_enable(struct eeh_dev *edev, int function)
Linas Vepstas47b5c832006-09-15 18:57:42 -0500547{
Linas Vepstas47b5c832006-09-15 18:57:42 -0500548 int rc;
Gavin Shanf631acd2012-02-27 20:04:07 +0000549 struct device_node *dn = eeh_dev_to_of_node(edev);
Linas Vepstas47b5c832006-09-15 18:57:42 -0500550
Gavin Shanf631acd2012-02-27 20:04:07 +0000551 rc = eeh_ops->set_option(dn, function);
Linas Vepstas47b5c832006-09-15 18:57:42 -0500552 if (rc)
Linas Vepstasfa1be472007-03-19 14:59:59 -0500553 printk(KERN_WARNING "EEH: Unexpected state change %d, err=%d dn=%s\n",
Gavin Shanf631acd2012-02-27 20:04:07 +0000554 function, rc, dn->full_name);
Linas Vepstas47b5c832006-09-15 18:57:42 -0500555
Gavin Shanf631acd2012-02-27 20:04:07 +0000556 rc = eeh_ops->wait_state(dn, PCI_BUS_RESET_WAIT_MSEC);
Gavin Shaneb594a42012-02-27 20:03:57 +0000557 if (rc > 0 && (rc & EEH_STATE_MMIO_ENABLED) &&
558 (function == EEH_OPT_THAW_MMIO))
Linas Vepstasfa1be472007-03-19 14:59:59 -0500559 return 0;
560
Linas Vepstas47b5c832006-09-15 18:57:42 -0500561 return rc;
562}
563
564/**
Brian King00c2ae32007-05-08 08:04:05 +1000565 * pcibios_set_pcie_slot_reset - Set PCI-E reset state
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000566 * @dev: pci device struct
567 * @state: reset state to enter
Brian King00c2ae32007-05-08 08:04:05 +1000568 *
569 * Return value:
570 * 0 if success
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000571 */
Brian King00c2ae32007-05-08 08:04:05 +1000572int pcibios_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
573{
574 struct device_node *dn = pci_device_to_OF_node(dev);
Brian King00c2ae32007-05-08 08:04:05 +1000575
576 switch (state) {
577 case pcie_deassert_reset:
Gavin Shan26524812012-02-27 20:03:59 +0000578 eeh_ops->reset(dn, EEH_RESET_DEACTIVATE);
Brian King00c2ae32007-05-08 08:04:05 +1000579 break;
580 case pcie_hot_reset:
Gavin Shan26524812012-02-27 20:03:59 +0000581 eeh_ops->reset(dn, EEH_RESET_HOT);
Brian King00c2ae32007-05-08 08:04:05 +1000582 break;
583 case pcie_warm_reset:
Gavin Shan26524812012-02-27 20:03:59 +0000584 eeh_ops->reset(dn, EEH_RESET_FUNDAMENTAL);
Brian King00c2ae32007-05-08 08:04:05 +1000585 break;
586 default:
587 return -EINVAL;
588 };
589
590 return 0;
591}
592
593/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000594 * __eeh_set_pe_freset - Check the required reset for child devices
595 * @parent: parent device
596 * @freset: return value
597 *
598 * Each device might have its preferred reset type: fundamental or
599 * hot reset. The routine is used to collect the information from
600 * the child devices so that they could be reset accordingly.
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600601 */
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000602void __eeh_set_pe_freset(struct device_node *parent, unsigned int *freset)
603{
604 struct device_node *dn;
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600605
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000606 for_each_child_of_node(parent, dn) {
Gavin Shanf631acd2012-02-27 20:04:07 +0000607 if (of_node_to_eeh_dev(dn)) {
608 struct pci_dev *dev = of_node_to_eeh_dev(dn)->pdev;
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000609
610 if (dev && dev->driver)
611 *freset |= dev->needs_freset;
612
613 __eeh_set_pe_freset(dn, freset);
614 }
615 }
616}
617
618/**
619 * eeh_set_pe_freset - Check the required reset for the indicated device and its children
620 * @dn: parent device
621 * @freset: return value
622 *
623 * Each device might have its preferred reset type: fundamental or
624 * hot reset. The routine is used to collected the information for
625 * the indicated device and its children so that the bunch of the
626 * devices could be reset properly.
627 */
628void eeh_set_pe_freset(struct device_node *dn, unsigned int *freset)
629{
630 struct pci_dev *dev;
Gavin Shancce4b2d2012-02-27 20:03:52 +0000631 dn = eeh_find_device_pe(dn);
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000632
633 /* Back up one, since config addrs might be shared */
Gavin Shanf631acd2012-02-27 20:04:07 +0000634 if (!pcibios_find_pci_bus(dn) && of_node_to_eeh_dev(dn->parent))
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000635 dn = dn->parent;
636
Gavin Shanf631acd2012-02-27 20:04:07 +0000637 dev = of_node_to_eeh_dev(dn)->pdev;
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000638 if (dev)
639 *freset |= dev->needs_freset;
640
641 __eeh_set_pe_freset(dn, freset);
642}
643
644/**
Gavin Shancce4b2d2012-02-27 20:03:52 +0000645 * eeh_reset_pe_once - Assert the pci #RST line for 1/4 second
Gavin Shanf631acd2012-02-27 20:04:07 +0000646 * @edev: pci device node to be reset.
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000647 *
648 * Assert the PCI #RST line for 1/4 second.
649 */
Gavin Shanf631acd2012-02-27 20:04:07 +0000650static void eeh_reset_pe_once(struct eeh_dev *edev)
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600651{
Richard A Lary308fc4f2011-04-22 09:59:47 +0000652 unsigned int freset = 0;
Gavin Shanf631acd2012-02-27 20:04:07 +0000653 struct device_node *dn = eeh_dev_to_of_node(edev);
Mike Mason6e193142009-07-30 15:42:39 -0700654
Richard A Lary308fc4f2011-04-22 09:59:47 +0000655 /* Determine type of EEH reset required for
656 * Partitionable Endpoint, a hot-reset (1)
657 * or a fundamental reset (3).
658 * A fundamental reset required by any device under
659 * Partitionable Endpoint trumps hot-reset.
660 */
Gavin Shanf631acd2012-02-27 20:04:07 +0000661 eeh_set_pe_freset(dn, &freset);
Richard A Lary308fc4f2011-04-22 09:59:47 +0000662
663 if (freset)
Gavin Shanf631acd2012-02-27 20:04:07 +0000664 eeh_ops->reset(dn, EEH_RESET_FUNDAMENTAL);
Mike Mason6e193142009-07-30 15:42:39 -0700665 else
Gavin Shanf631acd2012-02-27 20:04:07 +0000666 eeh_ops->reset(dn, EEH_RESET_HOT);
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600667
668 /* The PCI bus requires that the reset be held high for at least
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000669 * a 100 milliseconds. We wait a bit longer 'just in case'.
670 */
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600671#define PCI_BUS_RST_HOLD_TIME_MSEC 250
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000672 msleep(PCI_BUS_RST_HOLD_TIME_MSEC);
Linas Vepstasd9564ad2005-11-03 18:50:48 -0600673
674 /* We might get hit with another EEH freeze as soon as the
675 * pci slot reset line is dropped. Make sure we don't miss
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000676 * these, and clear the flag now.
677 */
Gavin Shanf631acd2012-02-27 20:04:07 +0000678 eeh_clear_slot(dn, EEH_MODE_ISOLATED);
Linas Vepstasd9564ad2005-11-03 18:50:48 -0600679
Gavin Shanf631acd2012-02-27 20:04:07 +0000680 eeh_ops->reset(dn, EEH_RESET_DEACTIVATE);
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600681
682 /* After a PCI slot has been reset, the PCI Express spec requires
683 * a 1.5 second idle time for the bus to stabilize, before starting
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000684 * up traffic.
685 */
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600686#define PCI_BUS_SETTLE_TIME_MSEC 1800
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000687 msleep(PCI_BUS_SETTLE_TIME_MSEC);
Linas Vepstase1029262006-09-21 18:25:56 -0500688}
689
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000690/**
Gavin Shancce4b2d2012-02-27 20:03:52 +0000691 * eeh_reset_pe - Reset the indicated PE
Gavin Shanf631acd2012-02-27 20:04:07 +0000692 * @edev: PCI device associated EEH device
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000693 *
694 * This routine should be called to reset indicated device, including
695 * PE. A PE might include multiple PCI devices and sometimes PCI bridges
696 * might be involved as well.
697 */
Gavin Shanf631acd2012-02-27 20:04:07 +0000698int eeh_reset_pe(struct eeh_dev *edev)
Linas Vepstase1029262006-09-21 18:25:56 -0500699{
700 int i, rc;
Gavin Shanf631acd2012-02-27 20:04:07 +0000701 struct device_node *dn = eeh_dev_to_of_node(edev);
Linas Vepstase1029262006-09-21 18:25:56 -0500702
Linas Vepstas9c547762007-03-19 14:58:07 -0500703 /* Take three shots at resetting the bus */
704 for (i=0; i<3; i++) {
Gavin Shanf631acd2012-02-27 20:04:07 +0000705 eeh_reset_pe_once(edev);
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600706
Gavin Shanf631acd2012-02-27 20:04:07 +0000707 rc = eeh_ops->wait_state(dn, PCI_BUS_RESET_WAIT_MSEC);
Gavin Shaneb594a42012-02-27 20:03:57 +0000708 if (rc == (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE))
Linas Vepstasb6495c02005-11-03 18:54:54 -0600709 return 0;
Linas Vepstase1029262006-09-21 18:25:56 -0500710
Linas Vepstase1029262006-09-21 18:25:56 -0500711 if (rc < 0) {
Linas Vepstas12588da2007-07-27 08:30:26 +1000712 printk(KERN_ERR "EEH: unrecoverable slot failure %s\n",
Gavin Shanf631acd2012-02-27 20:04:07 +0000713 dn->full_name);
Linas Vepstasb6495c02005-11-03 18:54:54 -0600714 return -1;
Linas Vepstase1029262006-09-21 18:25:56 -0500715 }
Linas Vepstas12588da2007-07-27 08:30:26 +1000716 printk(KERN_ERR "EEH: bus reset %d failed on slot %s, rc=%d\n",
Gavin Shanf631acd2012-02-27 20:04:07 +0000717 i+1, dn->full_name, rc);
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600718 }
Linas Vepstasb6495c02005-11-03 18:54:54 -0600719
Linas Vepstas9c547762007-03-19 14:58:07 -0500720 return -1;
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600721}
722
Linas Vepstas8b553f32005-11-03 18:50:17 -0600723/** Save and restore of PCI BARs
724 *
725 * Although firmware will set up BARs during boot, it doesn't
726 * set up device BAR's after a device reset, although it will,
727 * if requested, set up bridge configuration. Thus, we need to
728 * configure the PCI devices ourselves.
729 */
730
731/**
Gavin Shancce4b2d2012-02-27 20:03:52 +0000732 * eeh_restore_one_device_bars - Restore the Base Address Registers for one device
Gavin Shanf631acd2012-02-27 20:04:07 +0000733 * @edev: PCI device associated EEH device
Linas Vepstascb5b56242006-09-15 18:56:35 -0500734 *
Linas Vepstas8b553f32005-11-03 18:50:17 -0600735 * Loads the PCI configuration space base address registers,
736 * the expansion ROM base address, the latency timer, and etc.
737 * from the saved values in the device node.
738 */
Gavin Shanf631acd2012-02-27 20:04:07 +0000739static inline void eeh_restore_one_device_bars(struct eeh_dev *edev)
Linas Vepstas8b553f32005-11-03 18:50:17 -0600740{
741 int i;
Mike Masoncde274c2008-07-09 02:04:35 +1000742 u32 cmd;
Gavin Shanf631acd2012-02-27 20:04:07 +0000743 struct device_node *dn = eeh_dev_to_of_node(edev);
Linas Vepstas8b553f32005-11-03 18:50:17 -0600744
Gavin Shanf631acd2012-02-27 20:04:07 +0000745 if (!edev->phb)
746 return;
747
Linas Vepstas8b553f32005-11-03 18:50:17 -0600748 for (i=4; i<10; i++) {
Gavin Shan37804442012-02-27 20:04:11 +0000749 eeh_ops->write_config(dn, i*4, 4, edev->config_space[i]);
Linas Vepstas8b553f32005-11-03 18:50:17 -0600750 }
751
752 /* 12 == Expansion ROM Address */
Gavin Shan37804442012-02-27 20:04:11 +0000753 eeh_ops->write_config(dn, 12*4, 4, edev->config_space[12]);
Linas Vepstas8b553f32005-11-03 18:50:17 -0600754
755#define BYTE_SWAP(OFF) (8*((OFF)/4)+3-(OFF))
Gavin Shanf631acd2012-02-27 20:04:07 +0000756#define SAVED_BYTE(OFF) (((u8 *)(edev->config_space))[BYTE_SWAP(OFF)])
Linas Vepstas8b553f32005-11-03 18:50:17 -0600757
Gavin Shan37804442012-02-27 20:04:11 +0000758 eeh_ops->write_config(dn, PCI_CACHE_LINE_SIZE, 1,
Linas Vepstas8b553f32005-11-03 18:50:17 -0600759 SAVED_BYTE(PCI_CACHE_LINE_SIZE));
760
Gavin Shan37804442012-02-27 20:04:11 +0000761 eeh_ops->write_config(dn, PCI_LATENCY_TIMER, 1,
Linas Vepstas8b553f32005-11-03 18:50:17 -0600762 SAVED_BYTE(PCI_LATENCY_TIMER));
763
764 /* max latency, min grant, interrupt pin and line */
Gavin Shan37804442012-02-27 20:04:11 +0000765 eeh_ops->write_config(dn, 15*4, 4, edev->config_space[15]);
Mike Masoncde274c2008-07-09 02:04:35 +1000766
767 /* Restore PERR & SERR bits, some devices require it,
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000768 * don't touch the other command bits
769 */
Gavin Shan37804442012-02-27 20:04:11 +0000770 eeh_ops->read_config(dn, PCI_COMMAND, 4, &cmd);
Gavin Shanf631acd2012-02-27 20:04:07 +0000771 if (edev->config_space[1] & PCI_COMMAND_PARITY)
Mike Masoncde274c2008-07-09 02:04:35 +1000772 cmd |= PCI_COMMAND_PARITY;
773 else
774 cmd &= ~PCI_COMMAND_PARITY;
Gavin Shanf631acd2012-02-27 20:04:07 +0000775 if (edev->config_space[1] & PCI_COMMAND_SERR)
Mike Masoncde274c2008-07-09 02:04:35 +1000776 cmd |= PCI_COMMAND_SERR;
777 else
778 cmd &= ~PCI_COMMAND_SERR;
Gavin Shan37804442012-02-27 20:04:11 +0000779 eeh_ops->write_config(dn, PCI_COMMAND, 4, cmd);
Linas Vepstas8b553f32005-11-03 18:50:17 -0600780}
781
782/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000783 * eeh_restore_bars - Restore the PCI config space info
Gavin Shanf631acd2012-02-27 20:04:07 +0000784 * @edev: EEH device
Linas Vepstas8b553f32005-11-03 18:50:17 -0600785 *
786 * This routine performs a recursive walk to the children
787 * of this device as well.
788 */
Gavin Shanf631acd2012-02-27 20:04:07 +0000789void eeh_restore_bars(struct eeh_dev *edev)
Linas Vepstas8b553f32005-11-03 18:50:17 -0600790{
791 struct device_node *dn;
Gavin Shanf631acd2012-02-27 20:04:07 +0000792 if (!edev)
Linas Vepstas8b553f32005-11-03 18:50:17 -0600793 return;
794
Gavin Shanf631acd2012-02-27 20:04:07 +0000795 if ((edev->mode & EEH_MODE_SUPPORTED) && !IS_BRIDGE(edev->class_code))
796 eeh_restore_one_device_bars(edev);
Linas Vepstas8b553f32005-11-03 18:50:17 -0600797
Gavin Shanf631acd2012-02-27 20:04:07 +0000798 for_each_child_of_node(eeh_dev_to_of_node(edev), dn)
799 eeh_restore_bars(of_node_to_eeh_dev(dn));
Linas Vepstas8b553f32005-11-03 18:50:17 -0600800}
801
802/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000803 * eeh_save_bars - Save device bars
Gavin Shanf631acd2012-02-27 20:04:07 +0000804 * @edev: PCI device associated EEH device
Linas Vepstas8b553f32005-11-03 18:50:17 -0600805 *
806 * Save the values of the device bars. Unlike the restore
807 * routine, this routine is *not* recursive. This is because
Justin Mattock31116f02011-02-24 20:10:18 +0000808 * PCI devices are added individually; but, for the restore,
Linas Vepstas8b553f32005-11-03 18:50:17 -0600809 * an entire slot is reset at a time.
810 */
Gavin Shanf631acd2012-02-27 20:04:07 +0000811static void eeh_save_bars(struct eeh_dev *edev)
Linas Vepstas8b553f32005-11-03 18:50:17 -0600812{
813 int i;
Gavin Shanf631acd2012-02-27 20:04:07 +0000814 struct device_node *dn;
Linas Vepstas8b553f32005-11-03 18:50:17 -0600815
Gavin Shanf631acd2012-02-27 20:04:07 +0000816 if (!edev)
Linas Vepstas8b553f32005-11-03 18:50:17 -0600817 return;
Gavin Shanf631acd2012-02-27 20:04:07 +0000818 dn = eeh_dev_to_of_node(edev);
Linas Vepstas8b553f32005-11-03 18:50:17 -0600819
820 for (i = 0; i < 16; i++)
Gavin Shan37804442012-02-27 20:04:11 +0000821 eeh_ops->read_config(dn, i * 4, 4, &edev->config_space[i]);
Linas Vepstas8b553f32005-11-03 18:50:17 -0600822}
823
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000824/**
Gavin Shancce4b2d2012-02-27 20:03:52 +0000825 * eeh_early_enable - Early enable EEH on the indicated device
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000826 * @dn: device node
827 * @data: BUID
828 *
829 * Enable EEH functionality on the specified PCI device. The function
830 * is expected to be called before real PCI probing is done. However,
831 * the PHBs have been initialized at this point.
832 */
Gavin Shancce4b2d2012-02-27 20:03:52 +0000833static void *eeh_early_enable(struct device_node *dn, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 int ret;
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000836 const u32 *class_code = of_get_property(dn, "class-code", NULL);
837 const u32 *vendor_id = of_get_property(dn, "vendor-id", NULL);
838 const u32 *device_id = of_get_property(dn, "device-id", NULL);
Jeremy Kerr954a46e2006-07-12 15:39:43 +1000839 const u32 *regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 int enable;
Gavin Shanf631acd2012-02-27 20:04:07 +0000841 struct eeh_dev *edev = of_node_to_eeh_dev(dn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Gavin Shanf631acd2012-02-27 20:04:07 +0000843 edev->class_code = 0;
844 edev->mode = 0;
845 edev->check_count = 0;
846 edev->freeze_count = 0;
847 edev->false_positives = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Nathan Lynchc6d4d5a2008-03-14 06:52:10 +1100849 if (!of_device_is_available(dn))
850 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 /* Ignore bad nodes. */
853 if (!class_code || !vendor_id || !device_id)
854 return NULL;
855
856 /* There is nothing to check on PCI to ISA bridges */
857 if (dn->type && !strcmp(dn->type, "isa")) {
Gavin Shanf631acd2012-02-27 20:04:07 +0000858 edev->mode |= EEH_MODE_NOCHECK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return NULL;
860 }
Gavin Shanf631acd2012-02-27 20:04:07 +0000861 edev->class_code = *class_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 /* Ok... see if this device supports EEH. Some do, some don't,
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000864 * and the only way to find out is to check each and every one.
865 */
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000866 regs = of_get_property(dn, "reg", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (regs) {
868 /* First register entry is addr (00BBSS00) */
869 /* Try to enable eeh */
Gavin Shan8fb8f702012-02-27 20:03:55 +0000870 ret = eeh_ops->set_option(dn, EEH_OPT_ENABLE);
Linas Vepstas172ca922005-11-03 18:50:04 -0600871
Linas Vepstas25c4a462007-01-26 14:55:03 -0600872 enable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (ret == 0) {
Gavin Shanf631acd2012-02-27 20:04:07 +0000874 edev->config_addr = regs[0];
Linas Vepstas25e591f2005-11-03 18:53:20 -0600875
876 /* If the newer, better, ibm,get-config-addr-info is supported,
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000877 * then use that instead.
878 */
Gavin Shanf631acd2012-02-27 20:04:07 +0000879 edev->pe_config_addr = eeh_ops->get_pe_addr(dn);
Linas Vepstas25c4a462007-01-26 14:55:03 -0600880
881 /* Some older systems (Power4) allow the
882 * ibm,set-eeh-option call to succeed even on nodes
883 * where EEH is not supported. Verify support
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000884 * explicitly.
885 */
Gavin Shanf631acd2012-02-27 20:04:07 +0000886 ret = eeh_ops->get_state(dn, NULL);
Gavin Shaneb594a42012-02-27 20:03:57 +0000887 if (ret > 0 && ret != EEH_STATE_NOT_SUPPORT)
Linas Vepstas25c4a462007-01-26 14:55:03 -0600888 enable = 1;
889 }
890
891 if (enable) {
892 eeh_subsystem_enabled = 1;
Gavin Shanf631acd2012-02-27 20:04:07 +0000893 edev->mode |= EEH_MODE_SUPPORTED;
Linas Vepstas25c4a462007-01-26 14:55:03 -0600894
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +0000895 pr_debug("EEH: %s: eeh enabled, config=%x pe_config=%x\n",
Gavin Shanf631acd2012-02-27 20:04:07 +0000896 dn->full_name, edev->config_addr,
897 edev->pe_config_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 } else {
899
900 /* This device doesn't support EEH, but it may have an
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000901 * EEH parent, in which case we mark it as supported.
902 */
Gavin Shanf631acd2012-02-27 20:04:07 +0000903 if (dn->parent && of_node_to_eeh_dev(dn->parent) &&
904 (of_node_to_eeh_dev(dn->parent)->mode & EEH_MODE_SUPPORTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 /* Parent supports EEH. */
Gavin Shanf631acd2012-02-27 20:04:07 +0000906 edev->mode |= EEH_MODE_SUPPORTED;
907 edev->config_addr = of_node_to_eeh_dev(dn->parent)->config_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return NULL;
909 }
910 }
911 } else {
912 printk(KERN_WARNING "EEH: %s: unable to get reg property.\n",
913 dn->full_name);
914 }
915
Gavin Shanf631acd2012-02-27 20:04:07 +0000916 eeh_save_bars(edev);
Linas Vepstas69376502005-11-03 18:47:50 -0600917 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000920/**
Gavin Shanaa1e6372012-02-27 20:03:53 +0000921 * eeh_ops_register - Register platform dependent EEH operations
922 * @ops: platform dependent EEH operations
923 *
924 * Register the platform dependent EEH operation callback
925 * functions. The platform should call this function before
926 * any other EEH operations.
927 */
928int __init eeh_ops_register(struct eeh_ops *ops)
929{
930 if (!ops->name) {
931 pr_warning("%s: Invalid EEH ops name for %p\n",
932 __func__, ops);
933 return -EINVAL;
934 }
935
936 if (eeh_ops && eeh_ops != ops) {
937 pr_warning("%s: EEH ops of platform %s already existing (%s)\n",
938 __func__, eeh_ops->name, ops->name);
939 return -EEXIST;
940 }
941
942 eeh_ops = ops;
943
944 return 0;
945}
946
947/**
948 * eeh_ops_unregister - Unreigster platform dependent EEH operations
949 * @name: name of EEH platform operations
950 *
951 * Unregister the platform dependent EEH operation callback
952 * functions.
953 */
954int __exit eeh_ops_unregister(const char *name)
955{
956 if (!name || !strlen(name)) {
957 pr_warning("%s: Invalid EEH ops name\n",
958 __func__);
959 return -EINVAL;
960 }
961
962 if (eeh_ops && !strcmp(eeh_ops->name, name)) {
963 eeh_ops = NULL;
964 return 0;
965 }
966
967 return -EEXIST;
968}
969
970/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000971 * eeh_init - EEH initialization
972 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 * Initialize EEH by trying to enable it for all of the adapters in the system.
974 * As a side effect we can determine here if eeh is supported at all.
975 * Note that we leave EEH on so failed config cycles won't cause a machine
976 * check. If a user turns off EEH for a particular adapter they are really
977 * telling Linux to ignore errors. Some hardware (e.g. POWER5) won't
978 * grant access to a slot if EEH isn't enabled, and so we always enable
979 * EEH for all slots/all devices.
980 *
981 * The eeh-force-off option disables EEH checking globally, for all slots.
982 * Even if force-off is set, the EEH hardware is still enabled, so that
983 * newer systems can boot.
984 */
985void __init eeh_init(void)
986{
Gavin Shan1a5c2e62012-03-20 21:30:29 +0000987 struct pci_controller *hose, *tmp;
988 struct device_node *phb;
Gavin Shane2af1552012-02-27 20:03:54 +0000989 int ret;
990
991 /* call platform initialization function */
992 if (!eeh_ops) {
993 pr_warning("%s: Platform EEH operation not found\n",
994 __func__);
995 return;
996 } else if ((ret = eeh_ops->init())) {
997 pr_warning("%s: Failed to call platform init function (%d)\n",
998 __func__, ret);
999 return;
1000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Thomas Gleixner3d372622010-02-18 02:23:07 +00001002 raw_spin_lock_init(&confirm_error_lock);
Linas Vepstasdf7242b2005-11-03 18:49:01 -06001003
Gavin Shan1a5c2e62012-03-20 21:30:29 +00001004 /* Enable EEH for all adapters */
1005 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
1006 phb = hose->dn;
Gavin Shanc8c29b32012-02-27 20:03:56 +00001007 traverse_pci_devices(phb, eeh_early_enable, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
1009
1010 if (eeh_subsystem_enabled)
1011 printk(KERN_INFO "EEH: PCI Enhanced I/O Error Handling Enabled\n");
1012 else
1013 printk(KERN_WARNING "EEH: No capable adapters found\n");
1014}
1015
1016/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001017 * eeh_add_device_early - Enable EEH for the indicated device_node
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 * @dn: device node for which to set up EEH
1019 *
1020 * This routine must be used to perform EEH initialization for PCI
1021 * devices that were added after system boot (e.g. hotplug, dlpar).
1022 * This routine must be called before any i/o is performed to the
1023 * adapter (inluding any config-space i/o).
1024 * Whether this actually enables EEH or not for this device depends
1025 * on the CEC architecture, type of the device, on earlier boot
1026 * command-line arguments & etc.
1027 */
Nathan Fontenot794e0852006-03-31 12:04:52 -06001028static void eeh_add_device_early(struct device_node *dn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
1030 struct pci_controller *phb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Gavin Shanf631acd2012-02-27 20:04:07 +00001032 if (!dn || !of_node_to_eeh_dev(dn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 return;
Gavin Shanf631acd2012-02-27 20:04:07 +00001034 phb = of_node_to_eeh_dev(dn)->phb;
Linas Vepstasf751f842005-11-03 18:54:23 -06001035
1036 /* USB Bus children of PCI devices will not have BUID's */
1037 if (NULL == phb || 0 == phb->buid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Gavin Shanc8c29b32012-02-27 20:03:56 +00001040 eeh_early_enable(dn, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001043/**
1044 * eeh_add_device_tree_early - Enable EEH for the indicated device
1045 * @dn: device node
1046 *
1047 * This routine must be used to perform EEH initialization for the
1048 * indicated PCI device that was added after system boot (e.g.
1049 * hotplug, dlpar).
1050 */
Linas Vepstase2a296e2005-11-03 18:51:31 -06001051void eeh_add_device_tree_early(struct device_node *dn)
1052{
1053 struct device_node *sib;
Stephen Rothwellacaa6172007-12-21 15:52:07 +11001054
1055 for_each_child_of_node(dn, sib)
Linas Vepstase2a296e2005-11-03 18:51:31 -06001056 eeh_add_device_tree_early(sib);
1057 eeh_add_device_early(dn);
1058}
1059EXPORT_SYMBOL_GPL(eeh_add_device_tree_early);
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001062 * eeh_add_device_late - Perform EEH initialization for the indicated pci device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 * @dev: pci device for which to set up EEH
1064 *
1065 * This routine must be used to complete EEH initialization for PCI
1066 * devices that were added after system boot (e.g. hotplug, dlpar).
1067 */
Nathan Fontenot794e0852006-03-31 12:04:52 -06001068static void eeh_add_device_late(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001070 struct device_node *dn;
Gavin Shanf631acd2012-02-27 20:04:07 +00001071 struct eeh_dev *edev;
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (!dev || !eeh_subsystem_enabled)
1074 return;
1075
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001076 pr_debug("EEH: Adding device %s\n", pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001078 dn = pci_device_to_OF_node(dev);
Gavin Shanf631acd2012-02-27 20:04:07 +00001079 edev = pci_dev_to_eeh_dev(dev);
1080 if (edev->pdev == dev) {
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001081 pr_debug("EEH: Already referenced !\n");
1082 return;
1083 }
Gavin Shanf631acd2012-02-27 20:04:07 +00001084 WARN_ON(edev->pdev);
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001085
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001086 pci_dev_get(dev);
Gavin Shanf631acd2012-02-27 20:04:07 +00001087 edev->pdev = dev;
1088 dev->dev.archdata.edev = edev;
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001089
Linas Vepstase1d04c92007-05-24 03:16:46 +10001090 pci_addr_cache_insert_device(dev);
1091 eeh_sysfs_add_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092}
Nathan Fontenot794e0852006-03-31 12:04:52 -06001093
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001094/**
1095 * eeh_add_device_tree_late - Perform EEH initialization for the indicated PCI bus
1096 * @bus: PCI bus
1097 *
1098 * This routine must be used to perform EEH initialization for PCI
1099 * devices which are attached to the indicated PCI bus. The PCI bus
1100 * is added after system boot through hotplug or dlpar.
1101 */
Nathan Fontenot794e0852006-03-31 12:04:52 -06001102void eeh_add_device_tree_late(struct pci_bus *bus)
1103{
1104 struct pci_dev *dev;
1105
1106 list_for_each_entry(dev, &bus->devices, bus_list) {
1107 eeh_add_device_late(dev);
1108 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
1109 struct pci_bus *subbus = dev->subordinate;
1110 if (subbus)
1111 eeh_add_device_tree_late(subbus);
1112 }
1113 }
1114}
1115EXPORT_SYMBOL_GPL(eeh_add_device_tree_late);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001118 * eeh_remove_device - Undo EEH setup for the indicated pci device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 * @dev: pci device to be removed
1120 *
Nathan Fontenot794e0852006-03-31 12:04:52 -06001121 * This routine should be called when a device is removed from
1122 * a running system (e.g. by hotplug or dlpar). It unregisters
1123 * the PCI device from the EEH subsystem. I/O errors affecting
1124 * this device will no longer be detected after this call; thus,
1125 * i/o errors affecting this slot may leave this device unusable.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 */
Nathan Fontenot794e0852006-03-31 12:04:52 -06001127static void eeh_remove_device(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
Gavin Shanf631acd2012-02-27 20:04:07 +00001129 struct eeh_dev *edev;
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (!dev || !eeh_subsystem_enabled)
1132 return;
Gavin Shanf631acd2012-02-27 20:04:07 +00001133 edev = pci_dev_to_eeh_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 /* Unregister the device with the EEH/PCI address search system */
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001136 pr_debug("EEH: Removing device %s\n", pci_name(dev));
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001137
Gavin Shanf631acd2012-02-27 20:04:07 +00001138 if (!edev || !edev->pdev) {
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001139 pr_debug("EEH: Not referenced !\n");
1140 return;
Linas Vepstasb055a9e2006-04-06 15:41:41 -05001141 }
Gavin Shanf631acd2012-02-27 20:04:07 +00001142 edev->pdev = NULL;
1143 dev->dev.archdata.edev = NULL;
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001144 pci_dev_put(dev);
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001145
1146 pci_addr_cache_remove_device(dev);
1147 eeh_sysfs_remove_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001150/**
1151 * eeh_remove_bus_device - Undo EEH setup for the indicated PCI device
1152 * @dev: PCI device
1153 *
1154 * This routine must be called when a device is removed from the
1155 * running system through hotplug or dlpar. The corresponding
1156 * PCI address cache will be removed.
1157 */
Linas Vepstase2a296e2005-11-03 18:51:31 -06001158void eeh_remove_bus_device(struct pci_dev *dev)
1159{
Nathan Fontenot794e0852006-03-31 12:04:52 -06001160 struct pci_bus *bus = dev->subordinate;
1161 struct pci_dev *child, *tmp;
1162
Linas Vepstase2a296e2005-11-03 18:51:31 -06001163 eeh_remove_device(dev);
Nathan Fontenot794e0852006-03-31 12:04:52 -06001164
1165 if (bus && dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
1166 list_for_each_entry_safe(child, tmp, &bus->devices, bus_list)
1167 eeh_remove_bus_device(child);
Linas Vepstase2a296e2005-11-03 18:51:31 -06001168 }
1169}
1170EXPORT_SYMBOL_GPL(eeh_remove_bus_device);
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172static int proc_eeh_show(struct seq_file *m, void *v)
1173{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 if (0 == eeh_subsystem_enabled) {
1175 seq_printf(m, "EEH Subsystem is globally disabled\n");
Gavin Shane575f8d2012-02-29 15:47:45 +00001176 seq_printf(m, "eeh_total_mmio_ffs=%llu\n", eeh_stats.total_mmio_ffs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 } else {
1178 seq_printf(m, "EEH Subsystem is enabled\n");
Linas Vepstas177bc932005-11-03 18:48:52 -06001179 seq_printf(m,
Gavin Shane575f8d2012-02-29 15:47:45 +00001180 "no device=%llu\n"
1181 "no device node=%llu\n"
1182 "no config address=%llu\n"
1183 "check not wanted=%llu\n"
1184 "eeh_total_mmio_ffs=%llu\n"
1185 "eeh_false_positives=%llu\n"
1186 "eeh_slot_resets=%llu\n",
1187 eeh_stats.no_device,
1188 eeh_stats.no_dn,
1189 eeh_stats.no_cfg_addr,
1190 eeh_stats.ignored_check,
1191 eeh_stats.total_mmio_ffs,
1192 eeh_stats.false_positives,
1193 eeh_stats.slot_resets);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
1195
1196 return 0;
1197}
1198
1199static int proc_eeh_open(struct inode *inode, struct file *file)
1200{
1201 return single_open(file, proc_eeh_show, NULL);
1202}
1203
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001204static const struct file_operations proc_eeh_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 .open = proc_eeh_open,
1206 .read = seq_read,
1207 .llseek = seq_lseek,
1208 .release = single_release,
1209};
1210
1211static int __init eeh_init_proc(void)
1212{
Denis V. Lunev66747132008-04-29 01:02:26 -07001213 if (machine_is(pseries))
Thadeu Lima de Souza Cascardo8feaa432011-08-26 10:36:31 +00001214 proc_create("powerpc/eeh", 0, NULL, &proc_eeh_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 return 0;
1216}
1217__initcall(eeh_init_proc);