blob: e1b6d8e172893c3fba28371eaf0b641b59f3f955 [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 Shan7f52a522014-04-24 18:00:18 +100025#include <linux/debugfs.h>
Gavin Shancb3bc9d2012-02-27 20:03:51 +000026#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/init.h>
28#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/pci.h>
Gavin Shana3032ca2014-07-15 17:00:56 +100030#include <linux/iommu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/proc_fs.h>
32#include <linux/rbtree.h>
Gavin Shan66f9af832014-02-12 15:24:56 +080033#include <linux/reboot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/seq_file.h>
35#include <linux/spinlock.h>
Paul Gortmaker66b15db2011-05-27 10:46:24 -040036#include <linux/export.h>
Stephen Rothwellacaa6172007-12-21 15:52:07 +110037#include <linux/of.h>
38
Arun Sharma600634972011-07-26 16:09:06 -070039#include <linux/atomic.h>
Gavin Shan1e54b932014-05-05 12:09:05 +100040#include <asm/debug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/eeh.h>
Linas Vepstas172ca922005-11-03 18:50:04 -060042#include <asm/eeh_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/io.h>
Gavin Shan212d16c2014-06-10 11:41:56 +100044#include <asm/iommu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/machdep.h>
Stephen Rothwelld3878992005-09-28 02:50:25 +100046#include <asm/ppc-pci.h>
Linas Vepstas172ca922005-11-03 18:50:04 -060047#include <asm/rtas.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/** Overview:
51 * EEH, or "Extended Error Handling" is a PCI bridge technology for
52 * dealing with PCI bus errors that can't be dealt with within the
53 * usual PCI framework, except by check-stopping the CPU. Systems
54 * that are designed for high-availability/reliability cannot afford
55 * to crash due to a "mere" PCI error, thus the need for EEH.
56 * An EEH-capable bridge operates by converting a detected error
57 * into a "slot freeze", taking the PCI adapter off-line, making
58 * the slot behave, from the OS'es point of view, as if the slot
59 * were "empty": all reads return 0xff's and all writes are silently
60 * ignored. EEH slot isolation events can be triggered by parity
61 * errors on the address or data busses (e.g. during posted writes),
Linas Vepstas69376502005-11-03 18:47:50 -060062 * which in turn might be caused by low voltage on the bus, dust,
63 * vibration, humidity, radioactivity or plain-old failed hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 *
65 * Note, however, that one of the leading causes of EEH slot
66 * freeze events are buggy device drivers, buggy device microcode,
67 * or buggy device hardware. This is because any attempt by the
68 * device to bus-master data to a memory address that is not
69 * assigned to the device will trigger a slot freeze. (The idea
70 * is to prevent devices-gone-wild from corrupting system memory).
71 * Buggy hardware/drivers will have a miserable time co-existing
72 * with EEH.
73 *
74 * Ideally, a PCI device driver, when suspecting that an isolation
Lucas De Marchi25985ed2011-03-30 22:57:33 -030075 * event has occurred (e.g. by reading 0xff's), will then ask EEH
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 * whether this is the case, and then take appropriate steps to
77 * reset the PCI slot, the PCI device, and then resume operations.
78 * However, until that day, the checking is done here, with the
79 * eeh_check_failure() routine embedded in the MMIO macros. If
80 * the slot is found to be isolated, an "EEH Event" is synthesized
81 * and sent out for processing.
82 */
83
Linas Vepstas5c1344e2005-11-03 18:49:31 -060084/* If a device driver keeps reading an MMIO register in an interrupt
Mike Masonf36c5222008-07-22 02:40:17 +100085 * handler after a slot isolation event, it might be broken.
86 * This sets the threshold for how many read attempts we allow
87 * before printing an error message.
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 */
Linas Vepstas2fd30be2007-03-19 14:53:22 -050089#define EEH_MAX_FAILS 2100000
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Linas Vepstas17213c32007-05-10 02:38:11 +100091/* Time to wait for a PCI slot to report status, in milliseconds */
Brian Kingfb48dc22013-11-25 16:27:54 -060092#define PCI_BUS_RESET_WAIT_MSEC (5*60*1000)
Linas Vepstas9c547762007-03-19 14:58:07 -050093
Gavin Shan8a5ad352014-04-24 18:00:17 +100094/*
95 * EEH probe mode support, which is part of the flags,
96 * is to support multiple platforms for EEH. Some platforms
97 * like pSeries do PCI emunation based on device tree.
98 * However, other platforms like powernv probe PCI devices
99 * from hardware. The flag is used to distinguish that.
100 * In addition, struct eeh_ops::probe would be invoked for
101 * particular OF node or PCI device so that the corresponding
102 * PE would be created there.
103 */
104int eeh_subsystem_flags;
105EXPORT_SYMBOL(eeh_subsystem_flags);
106
Gavin Shanaa1e6372012-02-27 20:03:53 +0000107/* Platform dependent EEH operations */
108struct eeh_ops *eeh_ops = NULL;
109
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600110/* Lock to avoid races due to multiple reports of an error */
Gavin Shan49075812013-06-20 13:21:03 +0800111DEFINE_RAW_SPINLOCK(confirm_error_lock);
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600112
Gavin Shan212d16c2014-06-10 11:41:56 +1000113/* Lock to protect passed flags */
114static DEFINE_MUTEX(eeh_dev_mutex);
115
Linas Vepstas17213c32007-05-10 02:38:11 +1000116/* Buffer for reporting pci register dumps. Its here in BSS, and
117 * not dynamically alloced, so that it ends up in RMO where RTAS
118 * can access it.
119 */
Gavin Shanf2e0be52014-09-30 12:39:08 +1000120#define EEH_PCI_REGS_LOG_LEN 8192
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000121static unsigned char pci_regs_buf[EEH_PCI_REGS_LOG_LEN];
122
Gavin Shane575f8d2012-02-29 15:47:45 +0000123/*
124 * The struct is used to maintain the EEH global statistic
125 * information. Besides, the EEH global statistics will be
126 * exported to user space through procfs
127 */
128struct eeh_stats {
129 u64 no_device; /* PCI device not found */
130 u64 no_dn; /* OF node not found */
131 u64 no_cfg_addr; /* Config address not found */
132 u64 ignored_check; /* EEH check skipped */
133 u64 total_mmio_ffs; /* Total EEH checks */
134 u64 false_positives; /* Unnecessary EEH checks */
135 u64 slot_resets; /* PE reset */
136};
137
138static struct eeh_stats eeh_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Linas Vepstas7684b402005-11-03 18:55:19 -0600140#define IS_BRIDGE(class_code) (((class_code)<<16) == PCI_BASE_CLASS_BRIDGE)
141
Gavin Shan7f52a522014-04-24 18:00:18 +1000142static int __init eeh_setup(char *str)
143{
144 if (!strcmp(str, "off"))
Gavin Shan05b17212014-07-17 14:41:38 +1000145 eeh_add_flag(EEH_FORCE_DISABLED);
Gavin Shana450e8f2014-11-22 21:58:09 +1100146 else if (!strcmp(str, "early_log"))
147 eeh_add_flag(EEH_EARLY_DUMP_LOG);
Gavin Shan7f52a522014-04-24 18:00:18 +1000148
149 return 1;
150}
151__setup("eeh=", eeh_setup);
152
Gavin Shanf2e0be52014-09-30 12:39:08 +1000153/*
154 * This routine captures assorted PCI configuration space data
155 * for the indicated PCI device, and puts them into a buffer
156 * for RTAS error logging.
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000157 */
Gavin Shanf2e0be52014-09-30 12:39:08 +1000158static size_t eeh_dump_dev_log(struct eeh_dev *edev, char *buf, size_t len)
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000159{
Gavin Shanf631acd2012-02-27 20:04:07 +0000160 struct device_node *dn = eeh_dev_to_of_node(edev);
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000161 u32 cfg;
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000162 int cap, i;
Gavin Shan0ed352d2014-07-17 14:41:40 +1000163 int n = 0, l = 0;
164 char buffer[128];
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000165
Gavin Shanf631acd2012-02-27 20:04:07 +0000166 n += scnprintf(buf+n, len-n, "%s\n", dn->full_name);
Gavin Shan2d86c382014-04-24 18:00:15 +1000167 pr_warn("EEH: of node=%s\n", dn->full_name);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000168
Gavin Shan37804442012-02-27 20:04:11 +0000169 eeh_ops->read_config(dn, PCI_VENDOR_ID, 4, &cfg);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000170 n += scnprintf(buf+n, len-n, "dev/vend:%08x\n", cfg);
Gavin Shan2d86c382014-04-24 18:00:15 +1000171 pr_warn("EEH: PCI device/vendor: %08x\n", cfg);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000172
Gavin Shan37804442012-02-27 20:04:11 +0000173 eeh_ops->read_config(dn, PCI_COMMAND, 4, &cfg);
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000174 n += scnprintf(buf+n, len-n, "cmd/stat:%x\n", cfg);
Gavin Shan2d86c382014-04-24 18:00:15 +1000175 pr_warn("EEH: PCI cmd/status register: %08x\n", cfg);
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000176
Linas Vepstas0b9369f2007-07-27 08:35:40 +1000177 /* Gather bridge-specific registers */
Gavin Shan2a18dfc2014-04-24 18:00:16 +1000178 if (edev->mode & EEH_DEV_BRIDGE) {
Gavin Shan37804442012-02-27 20:04:11 +0000179 eeh_ops->read_config(dn, PCI_SEC_STATUS, 2, &cfg);
Linas Vepstas0b9369f2007-07-27 08:35:40 +1000180 n += scnprintf(buf+n, len-n, "sec stat:%x\n", cfg);
Gavin Shan2d86c382014-04-24 18:00:15 +1000181 pr_warn("EEH: Bridge secondary status: %04x\n", cfg);
Linas Vepstas0b9369f2007-07-27 08:35:40 +1000182
Gavin Shan37804442012-02-27 20:04:11 +0000183 eeh_ops->read_config(dn, PCI_BRIDGE_CONTROL, 2, &cfg);
Linas Vepstas0b9369f2007-07-27 08:35:40 +1000184 n += scnprintf(buf+n, len-n, "brdg ctl:%x\n", cfg);
Gavin Shan2d86c382014-04-24 18:00:15 +1000185 pr_warn("EEH: Bridge control: %04x\n", cfg);
Linas Vepstas0b9369f2007-07-27 08:35:40 +1000186 }
187
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000188 /* Dump out the PCI-X command and status regs */
Gavin Shan2a18dfc2014-04-24 18:00:16 +1000189 cap = edev->pcix_cap;
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000190 if (cap) {
Gavin Shan37804442012-02-27 20:04:11 +0000191 eeh_ops->read_config(dn, cap, 4, &cfg);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000192 n += scnprintf(buf+n, len-n, "pcix-cmd:%x\n", cfg);
Gavin Shan2d86c382014-04-24 18:00:15 +1000193 pr_warn("EEH: PCI-X cmd: %08x\n", cfg);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000194
Gavin Shan37804442012-02-27 20:04:11 +0000195 eeh_ops->read_config(dn, cap+4, 4, &cfg);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000196 n += scnprintf(buf+n, len-n, "pcix-stat:%x\n", cfg);
Gavin Shan2d86c382014-04-24 18:00:15 +1000197 pr_warn("EEH: PCI-X status: %08x\n", cfg);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000198 }
199
Gavin Shan2a18dfc2014-04-24 18:00:16 +1000200 /* If PCI-E capable, dump PCI-E cap 10 */
201 cap = edev->pcie_cap;
202 if (cap) {
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000203 n += scnprintf(buf+n, len-n, "pci-e cap10:\n");
Gavin Shan2d86c382014-04-24 18:00:15 +1000204 pr_warn("EEH: PCI-E capabilities and status follow:\n");
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000205
206 for (i=0; i<=8; i++) {
Gavin Shan2a18dfc2014-04-24 18:00:16 +1000207 eeh_ops->read_config(dn, cap+4*i, 4, &cfg);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000208 n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
Gavin Shan0ed352d2014-07-17 14:41:40 +1000209
210 if ((i % 4) == 0) {
211 if (i != 0)
212 pr_warn("%s\n", buffer);
213
214 l = scnprintf(buffer, sizeof(buffer),
215 "EEH: PCI-E %02x: %08x ",
216 4*i, cfg);
217 } else {
218 l += scnprintf(buffer+l, sizeof(buffer)-l,
219 "%08x ", cfg);
220 }
221
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000222 }
Gavin Shan0ed352d2014-07-17 14:41:40 +1000223
224 pr_warn("%s\n", buffer);
Gavin Shan2a18dfc2014-04-24 18:00:16 +1000225 }
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000226
Gavin Shan2a18dfc2014-04-24 18:00:16 +1000227 /* If AER capable, dump it */
228 cap = edev->aer_cap;
229 if (cap) {
230 n += scnprintf(buf+n, len-n, "pci-e AER:\n");
231 pr_warn("EEH: PCI-E AER capability register set follows:\n");
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000232
Gavin Shan0ed352d2014-07-17 14:41:40 +1000233 for (i=0; i<=13; i++) {
Gavin Shan2a18dfc2014-04-24 18:00:16 +1000234 eeh_ops->read_config(dn, cap+4*i, 4, &cfg);
235 n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
Gavin Shan0ed352d2014-07-17 14:41:40 +1000236
237 if ((i % 4) == 0) {
238 if (i != 0)
239 pr_warn("%s\n", buffer);
240
241 l = scnprintf(buffer, sizeof(buffer),
242 "EEH: PCI-E AER %02x: %08x ",
243 4*i, cfg);
244 } else {
245 l += scnprintf(buffer+l, sizeof(buffer)-l,
246 "%08x ", cfg);
247 }
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000248 }
Gavin Shan0ed352d2014-07-17 14:41:40 +1000249
250 pr_warn("%s\n", buffer);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000251 }
Linas Vepstas0b9369f2007-07-27 08:35:40 +1000252
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000253 return n;
254}
255
Gavin Shanf2e0be52014-09-30 12:39:08 +1000256static void *eeh_dump_pe_log(void *data, void *flag)
257{
258 struct eeh_pe *pe = data;
259 struct eeh_dev *edev, *tmp;
260 size_t *plen = flag;
261
Gavin Shanc59004c2014-10-01 17:07:54 +1000262 /* If the PE's config space is blocked, 0xFF's will be
263 * returned. It's pointless to collect the log in this
264 * case.
265 */
266 if (pe->state & EEH_PE_CFG_BLOCKED)
267 return NULL;
268
Gavin Shanf2e0be52014-09-30 12:39:08 +1000269 eeh_pe_for_each_dev(pe, edev, tmp)
270 *plen += eeh_dump_dev_log(edev, pci_regs_buf + *plen,
271 EEH_PCI_REGS_LOG_LEN - *plen);
272
273 return NULL;
274}
275
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000276/**
277 * eeh_slot_error_detail - Generate combined log including driver log and error log
Gavin Shanff477962012-09-07 22:44:16 +0000278 * @pe: EEH PE
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000279 * @severity: temporary or permanent error log
280 *
281 * This routine should be called to generate the combined log, which
282 * is comprised of driver log and error log. The driver log is figured
283 * out from the config space of the corresponding PCI device, while
284 * the error log is fetched through platform dependent function call.
285 */
Gavin Shanff477962012-09-07 22:44:16 +0000286void eeh_slot_error_detail(struct eeh_pe *pe, int severity)
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000287{
288 size_t loglen = 0;
Gavin Shanff477962012-09-07 22:44:16 +0000289
Gavin Shanc35ae172013-06-27 13:46:42 +0800290 /*
291 * When the PHB is fenced or dead, it's pointless to collect
292 * the data from PCI config space because it should return
293 * 0xFF's. For ER, we still retrieve the data from the PCI
294 * config space.
Gavin Shan78954702014-04-24 18:00:14 +1000295 *
296 * For pHyp, we have to enable IO for log retrieval. Otherwise,
297 * 0xFF's is always returned from PCI config space.
Gavin Shanc35ae172013-06-27 13:46:42 +0800298 */
Gavin Shan9e049372014-04-24 18:00:07 +1000299 if (!(pe->type & EEH_PE_PHB)) {
Gavin Shandc561fb2014-07-17 14:41:39 +1000300 if (eeh_has_flag(EEH_ENABLE_IO_FOR_LOG))
Gavin Shan78954702014-04-24 18:00:14 +1000301 eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
Gavin Shanc35ae172013-06-27 13:46:42 +0800302 eeh_ops->configure_bridge(pe);
303 eeh_pe_restore_bars(pe);
304
305 pci_regs_buf[0] = 0;
Gavin Shanf2e0be52014-09-30 12:39:08 +1000306 eeh_pe_traverse(pe, eeh_dump_pe_log, &loglen);
Gavin Shanc35ae172013-06-27 13:46:42 +0800307 }
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000308
Gavin Shanff477962012-09-07 22:44:16 +0000309 eeh_ops->get_log(pe, severity, pci_regs_buf, loglen);
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000310}
311
312/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000313 * eeh_token_to_phys - Convert EEH address token to phys address
314 * @token: I/O token, should be address in the form 0xA....
315 *
316 * This routine should be called to convert virtual I/O address
317 * to physical one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 */
319static inline unsigned long eeh_token_to_phys(unsigned long token)
320{
321 pte_t *ptep;
322 unsigned long pa;
Aneesh Kumar K.V12bc9f62013-06-20 14:30:18 +0530323 int hugepage_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Aneesh Kumar K.V12bc9f62013-06-20 14:30:18 +0530325 /*
326 * We won't find hugepages here, iomem
327 */
328 ptep = find_linux_pte_or_hugepte(init_mm.pgd, token, &hugepage_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (!ptep)
330 return token;
Aneesh Kumar K.V12bc9f62013-06-20 14:30:18 +0530331 WARN_ON(hugepage_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 pa = pte_pfn(*ptep) << PAGE_SHIFT;
333
334 return pa | (token & (PAGE_SIZE-1));
335}
336
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800337/*
338 * On PowerNV platform, we might already have fenced PHB there.
339 * For that case, it's meaningless to recover frozen PE. Intead,
340 * We have to handle fenced PHB firstly.
341 */
342static int eeh_phb_check_failure(struct eeh_pe *pe)
343{
344 struct eeh_pe *phb_pe;
345 unsigned long flags;
346 int ret;
347
Gavin Shan05b17212014-07-17 14:41:38 +1000348 if (!eeh_has_flag(EEH_PROBE_MODE_DEV))
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800349 return -EPERM;
350
351 /* Find the PHB PE */
352 phb_pe = eeh_phb_pe_get(pe->phb);
353 if (!phb_pe) {
Gavin Shan0dae2742014-07-17 14:41:41 +1000354 pr_warn("%s Can't find PE for PHB#%d\n",
355 __func__, pe->phb->global_number);
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800356 return -EEXIST;
357 }
358
359 /* If the PHB has been in problematic state */
360 eeh_serialize_lock(&flags);
Gavin Shan9e049372014-04-24 18:00:07 +1000361 if (phb_pe->state & EEH_PE_ISOLATED) {
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800362 ret = 0;
363 goto out;
364 }
365
366 /* Check PHB state */
367 ret = eeh_ops->get_state(phb_pe, NULL);
368 if ((ret < 0) ||
369 (ret == EEH_STATE_NOT_SUPPORT) ||
370 (ret & (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) ==
371 (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) {
372 ret = 0;
373 goto out;
374 }
375
376 /* Isolate the PHB and send event */
377 eeh_pe_state_mark(phb_pe, EEH_PE_ISOLATED);
378 eeh_serialize_unlock(flags);
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800379
Gavin Shan357b2f32014-06-11 18:26:44 +1000380 pr_err("EEH: PHB#%x failure detected, location: %s\n",
381 phb_pe->phb->global_number, eeh_pe_loc_get(phb_pe));
Gavin Shan56ca4fd2013-06-27 13:46:46 +0800382 dump_stack();
Gavin Shan5293bf92013-09-06 09:00:05 +0800383 eeh_send_failure_event(phb_pe);
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800384
385 return 1;
386out:
387 eeh_serialize_unlock(flags);
388 return ret;
389}
390
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000391/**
Gavin Shanf8f7d632012-09-07 22:44:22 +0000392 * eeh_dev_check_failure - Check if all 1's data is due to EEH slot freeze
393 * @edev: eeh device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 *
395 * Check for an EEH failure for the given device node. Call this
396 * routine if the result of a read was all 0xff's and you want to
397 * find out if this is due to an EEH slot freeze. This routine
398 * will query firmware for the EEH status.
399 *
400 * Returns 0 if there has not been an EEH error; otherwise returns
Linas Vepstas69376502005-11-03 18:47:50 -0600401 * a non-zero value and queues up a slot isolation event notification.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 *
403 * It is safe to call this routine in an interrupt context.
404 */
Gavin Shanf8f7d632012-09-07 22:44:22 +0000405int eeh_dev_check_failure(struct eeh_dev *edev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 int ret;
Gavin Shan1ad7a722014-05-05 09:29:03 +1000408 int active_flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 unsigned long flags;
Gavin Shanf8f7d632012-09-07 22:44:22 +0000410 struct device_node *dn;
411 struct pci_dev *dev;
Gavin Shan357b2f32014-06-11 18:26:44 +1000412 struct eeh_pe *pe, *parent_pe, *phb_pe;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600413 int rc = 0;
Mike Masonf36c5222008-07-22 02:40:17 +1000414 const char *location;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Gavin Shane575f8d2012-02-29 15:47:45 +0000416 eeh_stats.total_mmio_ffs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Gavin Shan2ec5a0a2014-02-12 15:24:55 +0800418 if (!eeh_enabled())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return 0;
420
Gavin Shanf8f7d632012-09-07 22:44:22 +0000421 if (!edev) {
Gavin Shane575f8d2012-02-29 15:47:45 +0000422 eeh_stats.no_dn++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return 0;
Linas Vepstas177bc932005-11-03 18:48:52 -0600424 }
Gavin Shanf8f7d632012-09-07 22:44:22 +0000425 dn = eeh_dev_to_of_node(edev);
426 dev = eeh_dev_to_pci_dev(edev);
Wei Yang2a582222014-09-17 10:48:26 +0800427 pe = eeh_dev_to_pe(edev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 /* Access to IO BARs might get this far and still not want checking. */
Gavin Shan66523d92012-09-07 22:44:13 +0000430 if (!pe) {
Gavin Shane575f8d2012-02-29 15:47:45 +0000431 eeh_stats.ignored_check++;
Gavin Shan66523d92012-09-07 22:44:13 +0000432 pr_debug("EEH: Ignored check for %s %s\n",
433 eeh_pci_name(dev), dn->full_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return 0;
435 }
436
Gavin Shan66523d92012-09-07 22:44:13 +0000437 if (!pe->addr && !pe->config_addr) {
Gavin Shane575f8d2012-02-29 15:47:45 +0000438 eeh_stats.no_cfg_addr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return 0;
440 }
441
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800442 /*
443 * On PowerNV platform, we might already have fenced PHB
444 * there and we need take care of that firstly.
445 */
446 ret = eeh_phb_check_failure(pe);
447 if (ret > 0)
448 return ret;
449
Gavin Shan05ec4242014-06-10 11:41:55 +1000450 /*
451 * If the PE isn't owned by us, we shouldn't check the
452 * state. Instead, let the owner handle it if the PE has
453 * been frozen.
454 */
455 if (eeh_pe_passed(pe))
456 return 0;
457
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600458 /* If we already have a pending isolation event for this
459 * slot, we know it's bad already, we don't need to check.
460 * Do this checking under a lock; as multiple PCI devices
461 * in one slot might report errors simultaneously, and we
462 * only want one error recovery routine running.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 */
Gavin Shan49075812013-06-20 13:21:03 +0800464 eeh_serialize_lock(&flags);
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600465 rc = 1;
Gavin Shan66523d92012-09-07 22:44:13 +0000466 if (pe->state & EEH_PE_ISOLATED) {
467 pe->check_count++;
468 if (pe->check_count % EEH_MAX_FAILS == 0) {
Mike Masonf36c5222008-07-22 02:40:17 +1000469 location = of_get_property(dn, "ibm,loc-code", NULL);
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000470 printk(KERN_ERR "EEH: %d reads ignored for recovering device at "
Mike Masonf36c5222008-07-22 02:40:17 +1000471 "location=%s driver=%s pci addr=%s\n",
Gavin Shan66523d92012-09-07 22:44:13 +0000472 pe->check_count, location,
Thadeu Lima de Souza Cascardo778a7852012-01-11 09:09:58 +0000473 eeh_driver_name(dev), eeh_pci_name(dev));
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000474 printk(KERN_ERR "EEH: Might be infinite loop in %s driver\n",
Thadeu Lima de Souza Cascardo778a7852012-01-11 09:09:58 +0000475 eeh_driver_name(dev));
Linas Vepstas5c1344e2005-11-03 18:49:31 -0600476 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600478 goto dn_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
480
481 /*
482 * Now test for an EEH failure. This is VERY expensive.
483 * Note that the eeh_config_addr may be a parent device
484 * in the case of a device behind a bridge, or it may be
485 * function zero of a multi-function device.
486 * In any case they must share a common PHB.
487 */
Gavin Shan66523d92012-09-07 22:44:13 +0000488 ret = eeh_ops->get_state(pe, NULL);
Linas Vepstas76e6faf2005-11-03 18:49:15 -0600489
Linas Vepstas39d16e22007-03-19 14:51:00 -0500490 /* Note that config-io to empty slots may fail;
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000491 * they are empty when they don't have children.
Gavin Shaneb594a42012-02-27 20:03:57 +0000492 * We will punt with the following conditions: Failure to get
493 * PE's state, EEH not support and Permanently unavailable
494 * state, PE is in good state.
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000495 */
Gavin Shaneb594a42012-02-27 20:03:57 +0000496 if ((ret < 0) ||
497 (ret == EEH_STATE_NOT_SUPPORT) ||
Gavin Shan1ad7a722014-05-05 09:29:03 +1000498 ((ret & active_flags) == active_flags)) {
Gavin Shane575f8d2012-02-29 15:47:45 +0000499 eeh_stats.false_positives++;
Gavin Shan66523d92012-09-07 22:44:13 +0000500 pe->false_positives++;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600501 rc = 0;
502 goto dn_unlock;
Linas Vepstas76e6faf2005-11-03 18:49:15 -0600503 }
504
Gavin Shan1ad7a722014-05-05 09:29:03 +1000505 /*
506 * It should be corner case that the parent PE has been
507 * put into frozen state as well. We should take care
508 * that at first.
509 */
510 parent_pe = pe->parent;
511 while (parent_pe) {
512 /* Hit the ceiling ? */
513 if (parent_pe->type & EEH_PE_PHB)
514 break;
515
516 /* Frozen parent PE ? */
517 ret = eeh_ops->get_state(parent_pe, NULL);
518 if (ret > 0 &&
519 (ret & active_flags) != active_flags)
520 pe = parent_pe;
521
522 /* Next parent level */
523 parent_pe = parent_pe->parent;
524 }
525
Gavin Shane575f8d2012-02-29 15:47:45 +0000526 eeh_stats.slot_resets++;
Gavin Shana84f2732013-06-20 13:20:51 +0800527
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600528 /* Avoid repeated reports of this failure, including problems
529 * with other functions on this device, and functions under
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000530 * bridges.
531 */
Gavin Shan66523d92012-09-07 22:44:13 +0000532 eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
Gavin Shan49075812013-06-20 13:21:03 +0800533 eeh_serialize_unlock(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 /* Most EEH events are due to device driver bugs. Having
536 * a stack trace will help the device-driver authors figure
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000537 * out what happened. So print that out.
538 */
Gavin Shan357b2f32014-06-11 18:26:44 +1000539 phb_pe = eeh_phb_pe_get(pe->phb);
540 pr_err("EEH: Frozen PHB#%x-PE#%x detected\n",
541 pe->phb->global_number, pe->addr);
542 pr_err("EEH: PE location: %s, PHB location: %s\n",
543 eeh_pe_loc_get(pe), eeh_pe_loc_get(phb_pe));
Gavin Shan56ca4fd2013-06-27 13:46:46 +0800544 dump_stack();
545
Gavin Shan5293bf92013-09-06 09:00:05 +0800546 eeh_send_failure_event(pe);
547
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600548 return 1;
549
550dn_unlock:
Gavin Shan49075812013-06-20 13:21:03 +0800551 eeh_serialize_unlock(flags);
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600552 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
Gavin Shanf8f7d632012-09-07 22:44:22 +0000555EXPORT_SYMBOL_GPL(eeh_dev_check_failure);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000558 * eeh_check_failure - Check if all 1's data is due to EEH slot freeze
Gavin Shan3e938052014-09-30 12:38:50 +1000559 * @token: I/O address
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 *
Gavin Shan3e938052014-09-30 12:38:50 +1000561 * Check for an EEH failure at the given I/O address. Call this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 * routine if the result of a read was all 0xff's and you want to
Gavin Shan3e938052014-09-30 12:38:50 +1000563 * find out if this is due to an EEH slot freeze event. This routine
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 * will query firmware for the EEH status.
565 *
566 * Note this routine is safe to call in an interrupt context.
567 */
Gavin Shan3e938052014-09-30 12:38:50 +1000568int eeh_check_failure(const volatile void __iomem *token)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 unsigned long addr;
Gavin Shanf8f7d632012-09-07 22:44:22 +0000571 struct eeh_dev *edev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 /* Finding the phys addr + pci device; this is pretty quick. */
574 addr = eeh_token_to_phys((unsigned long __force) token);
Gavin Shan3ab96a02012-09-07 22:44:23 +0000575 edev = eeh_addr_cache_get_dev(addr);
Gavin Shanf8f7d632012-09-07 22:44:22 +0000576 if (!edev) {
Gavin Shane575f8d2012-02-29 15:47:45 +0000577 eeh_stats.no_device++;
Gavin Shan3e938052014-09-30 12:38:50 +1000578 return 0;
Linas Vepstas177bc932005-11-03 18:48:52 -0600579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Gavin Shan3e938052014-09-30 12:38:50 +1000581 return eeh_dev_check_failure(edev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583EXPORT_SYMBOL(eeh_check_failure);
584
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600585
Linas Vepstascb5b56242006-09-15 18:56:35 -0500586/**
Gavin Shancce4b2d2012-02-27 20:03:52 +0000587 * eeh_pci_enable - Enable MMIO or DMA transfers for this slot
Gavin Shanff477962012-09-07 22:44:16 +0000588 * @pe: EEH PE
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000589 *
590 * This routine should be called to reenable frozen MMIO or DMA
591 * so that it would work correctly again. It's useful while doing
592 * recovery or log collection on the indicated device.
Linas Vepstas47b5c832006-09-15 18:57:42 -0500593 */
Gavin Shanff477962012-09-07 22:44:16 +0000594int eeh_pci_enable(struct eeh_pe *pe, int function)
Linas Vepstas47b5c832006-09-15 18:57:42 -0500595{
Gavin Shan4d4f5772014-09-30 12:39:00 +1000596 int active_flag, rc;
Gavin Shan78954702014-04-24 18:00:14 +1000597
598 /*
599 * pHyp doesn't allow to enable IO or DMA on unfrozen PE.
600 * Also, it's pointless to enable them on unfrozen PE. So
Gavin Shan4d4f5772014-09-30 12:39:00 +1000601 * we have to check before enabling IO or DMA.
Gavin Shan78954702014-04-24 18:00:14 +1000602 */
Gavin Shan4d4f5772014-09-30 12:39:00 +1000603 switch (function) {
604 case EEH_OPT_THAW_MMIO:
605 active_flag = EEH_STATE_MMIO_ACTIVE;
606 break;
607 case EEH_OPT_THAW_DMA:
608 active_flag = EEH_STATE_DMA_ACTIVE;
609 break;
610 case EEH_OPT_DISABLE:
611 case EEH_OPT_ENABLE:
612 case EEH_OPT_FREEZE_PE:
613 active_flag = 0;
614 break;
615 default:
616 pr_warn("%s: Invalid function %d\n",
617 __func__, function);
618 return -EINVAL;
619 }
620
621 /*
622 * Check if IO or DMA has been enabled before
623 * enabling them.
624 */
625 if (active_flag) {
Gavin Shan78954702014-04-24 18:00:14 +1000626 rc = eeh_ops->get_state(pe, NULL);
627 if (rc < 0)
628 return rc;
629
Gavin Shan4d4f5772014-09-30 12:39:00 +1000630 /* Needn't enable it at all */
631 if (rc == EEH_STATE_NOT_SUPPORT)
632 return 0;
633
634 /* It's already enabled */
635 if (rc & active_flag)
Gavin Shan78954702014-04-24 18:00:14 +1000636 return 0;
637 }
Linas Vepstas47b5c832006-09-15 18:57:42 -0500638
Gavin Shan4d4f5772014-09-30 12:39:00 +1000639
640 /* Issue the request */
Gavin Shanff477962012-09-07 22:44:16 +0000641 rc = eeh_ops->set_option(pe, function);
Linas Vepstas47b5c832006-09-15 18:57:42 -0500642 if (rc)
Gavin Shan78954702014-04-24 18:00:14 +1000643 pr_warn("%s: Unexpected state change %d on "
644 "PHB#%d-PE#%x, err=%d\n",
645 __func__, function, pe->phb->global_number,
646 pe->addr, rc);
Linas Vepstas47b5c832006-09-15 18:57:42 -0500647
Gavin Shan4d4f5772014-09-30 12:39:00 +1000648 /* Check if the request is finished successfully */
649 if (active_flag) {
650 rc = eeh_ops->wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
651 if (rc <= 0)
652 return rc;
Gavin Shan78954702014-04-24 18:00:14 +1000653
Gavin Shan4d4f5772014-09-30 12:39:00 +1000654 if (rc & active_flag)
655 return 0;
Gavin Shan78954702014-04-24 18:00:14 +1000656
Gavin Shan4d4f5772014-09-30 12:39:00 +1000657 return -EIO;
658 }
Linas Vepstasfa1be472007-03-19 14:59:59 -0500659
Linas Vepstas47b5c832006-09-15 18:57:42 -0500660 return rc;
661}
662
663/**
Brian King00c2ae32007-05-08 08:04:05 +1000664 * pcibios_set_pcie_slot_reset - Set PCI-E reset state
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000665 * @dev: pci device struct
666 * @state: reset state to enter
Brian King00c2ae32007-05-08 08:04:05 +1000667 *
668 * Return value:
669 * 0 if success
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000670 */
Brian King00c2ae32007-05-08 08:04:05 +1000671int pcibios_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
672{
Gavin Shanc270a242012-09-07 22:44:17 +0000673 struct eeh_dev *edev = pci_dev_to_eeh_dev(dev);
Wei Yang2a582222014-09-17 10:48:26 +0800674 struct eeh_pe *pe = eeh_dev_to_pe(edev);
Gavin Shanc270a242012-09-07 22:44:17 +0000675
676 if (!pe) {
677 pr_err("%s: No PE found on PCI device %s\n",
678 __func__, pci_name(dev));
679 return -EINVAL;
680 }
Brian King00c2ae32007-05-08 08:04:05 +1000681
682 switch (state) {
683 case pcie_deassert_reset:
Gavin Shanc270a242012-09-07 22:44:17 +0000684 eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);
Gavin Shan8a6b3712014-10-01 17:07:50 +1000685 eeh_pe_state_clear(pe, EEH_PE_CFG_BLOCKED);
Brian King00c2ae32007-05-08 08:04:05 +1000686 break;
687 case pcie_hot_reset:
Gavin Shan8a6b3712014-10-01 17:07:50 +1000688 eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
Gavin Shanc270a242012-09-07 22:44:17 +0000689 eeh_ops->reset(pe, EEH_RESET_HOT);
Brian King00c2ae32007-05-08 08:04:05 +1000690 break;
691 case pcie_warm_reset:
Gavin Shan8a6b3712014-10-01 17:07:50 +1000692 eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
Gavin Shanc270a242012-09-07 22:44:17 +0000693 eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
Brian King00c2ae32007-05-08 08:04:05 +1000694 break;
695 default:
Gavin Shan8a6b3712014-10-01 17:07:50 +1000696 eeh_pe_state_clear(pe, EEH_PE_CFG_BLOCKED);
Brian King00c2ae32007-05-08 08:04:05 +1000697 return -EINVAL;
698 };
699
700 return 0;
701}
702
703/**
Gavin Shanc270a242012-09-07 22:44:17 +0000704 * eeh_set_pe_freset - Check the required reset for the indicated device
705 * @data: EEH device
706 * @flag: return value
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000707 *
708 * Each device might have its preferred reset type: fundamental or
709 * hot reset. The routine is used to collected the information for
710 * the indicated device and its children so that the bunch of the
711 * devices could be reset properly.
712 */
Gavin Shanc270a242012-09-07 22:44:17 +0000713static void *eeh_set_dev_freset(void *data, void *flag)
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000714{
715 struct pci_dev *dev;
Gavin Shanc270a242012-09-07 22:44:17 +0000716 unsigned int *freset = (unsigned int *)flag;
717 struct eeh_dev *edev = (struct eeh_dev *)data;
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000718
Gavin Shanc270a242012-09-07 22:44:17 +0000719 dev = eeh_dev_to_pci_dev(edev);
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000720 if (dev)
721 *freset |= dev->needs_freset;
722
Gavin Shanc270a242012-09-07 22:44:17 +0000723 return NULL;
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000724}
725
726/**
Gavin Shancce4b2d2012-02-27 20:03:52 +0000727 * eeh_reset_pe_once - Assert the pci #RST line for 1/4 second
Gavin Shanc270a242012-09-07 22:44:17 +0000728 * @pe: EEH PE
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000729 *
730 * Assert the PCI #RST line for 1/4 second.
731 */
Gavin Shanc270a242012-09-07 22:44:17 +0000732static void eeh_reset_pe_once(struct eeh_pe *pe)
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600733{
Richard A Lary308fc4f2011-04-22 09:59:47 +0000734 unsigned int freset = 0;
Mike Mason6e193142009-07-30 15:42:39 -0700735
Richard A Lary308fc4f2011-04-22 09:59:47 +0000736 /* Determine type of EEH reset required for
737 * Partitionable Endpoint, a hot-reset (1)
738 * or a fundamental reset (3).
739 * A fundamental reset required by any device under
740 * Partitionable Endpoint trumps hot-reset.
Gavin Shana84f2732013-06-20 13:20:51 +0800741 */
Gavin Shanc270a242012-09-07 22:44:17 +0000742 eeh_pe_dev_traverse(pe, eeh_set_dev_freset, &freset);
Richard A Lary308fc4f2011-04-22 09:59:47 +0000743
744 if (freset)
Gavin Shanc270a242012-09-07 22:44:17 +0000745 eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
Mike Mason6e193142009-07-30 15:42:39 -0700746 else
Gavin Shanc270a242012-09-07 22:44:17 +0000747 eeh_ops->reset(pe, EEH_RESET_HOT);
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600748
Gavin Shanc270a242012-09-07 22:44:17 +0000749 eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);
Linas Vepstase1029262006-09-21 18:25:56 -0500750}
751
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000752/**
Gavin Shancce4b2d2012-02-27 20:03:52 +0000753 * eeh_reset_pe - Reset the indicated PE
Gavin Shanc270a242012-09-07 22:44:17 +0000754 * @pe: EEH PE
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000755 *
756 * This routine should be called to reset indicated device, including
757 * PE. A PE might include multiple PCI devices and sometimes PCI bridges
758 * might be involved as well.
759 */
Gavin Shanc270a242012-09-07 22:44:17 +0000760int eeh_reset_pe(struct eeh_pe *pe)
Linas Vepstase1029262006-09-21 18:25:56 -0500761{
Gavin Shan326a98e2013-06-20 13:20:58 +0800762 int flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
Gavin Shanb85743e2014-11-14 10:47:28 +1100763 int i, state, ret;
Linas Vepstase1029262006-09-21 18:25:56 -0500764
Gavin Shan28bf36f2014-11-14 10:47:29 +1100765 /* Mark as reset and block config space */
766 eeh_pe_state_mark(pe, EEH_PE_RESET | EEH_PE_CFG_BLOCKED);
767
Linas Vepstas9c547762007-03-19 14:58:07 -0500768 /* Take three shots at resetting the bus */
Gavin Shanb85743e2014-11-14 10:47:28 +1100769 for (i = 0; i < 3; i++) {
Gavin Shanc270a242012-09-07 22:44:17 +0000770 eeh_reset_pe_once(pe);
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600771
Gavin Shan78954702014-04-24 18:00:14 +1000772 /*
773 * EEH_PE_ISOLATED is expected to be removed after
774 * BAR restore.
775 */
Gavin Shanb85743e2014-11-14 10:47:28 +1100776 state = eeh_ops->wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
777 if ((state & flags) == flags) {
778 ret = 0;
779 goto out;
Linas Vepstase1029262006-09-21 18:25:56 -0500780 }
Gavin Shanb85743e2014-11-14 10:47:28 +1100781
782 if (state < 0) {
783 pr_warn("%s: Unrecoverable slot failure on PHB#%d-PE#%x",
784 __func__, pe->phb->global_number, pe->addr);
785 ret = -ENOTRECOVERABLE;
786 goto out;
787 }
788
789 /* We might run out of credits */
790 ret = -EIO;
791 pr_warn("%s: Failure %d resetting PHB#%x-PE#%x\n (%d)\n",
792 __func__, state, pe->phb->global_number, pe->addr, (i + 1));
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600793 }
Linas Vepstasb6495c02005-11-03 18:54:54 -0600794
Gavin Shanb85743e2014-11-14 10:47:28 +1100795out:
Gavin Shan28bf36f2014-11-14 10:47:29 +1100796 eeh_pe_state_clear(pe, EEH_PE_RESET | EEH_PE_CFG_BLOCKED);
Gavin Shanb85743e2014-11-14 10:47:28 +1100797 return ret;
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600798}
799
Linas Vepstas8b553f32005-11-03 18:50:17 -0600800/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000801 * eeh_save_bars - Save device bars
Gavin Shanf631acd2012-02-27 20:04:07 +0000802 * @edev: PCI device associated EEH device
Linas Vepstas8b553f32005-11-03 18:50:17 -0600803 *
804 * Save the values of the device bars. Unlike the restore
805 * routine, this routine is *not* recursive. This is because
Justin Mattock31116f02011-02-24 20:10:18 +0000806 * PCI devices are added individually; but, for the restore,
Linas Vepstas8b553f32005-11-03 18:50:17 -0600807 * an entire slot is reset at a time.
808 */
Gavin Shand7bb8862012-09-07 22:44:21 +0000809void eeh_save_bars(struct eeh_dev *edev)
Linas Vepstas8b553f32005-11-03 18:50:17 -0600810{
811 int i;
Gavin Shanf631acd2012-02-27 20:04:07 +0000812 struct device_node *dn;
Linas Vepstas8b553f32005-11-03 18:50:17 -0600813
Gavin Shanf631acd2012-02-27 20:04:07 +0000814 if (!edev)
Linas Vepstas8b553f32005-11-03 18:50:17 -0600815 return;
Gavin Shanf631acd2012-02-27 20:04:07 +0000816 dn = eeh_dev_to_of_node(edev);
Gavin Shana84f2732013-06-20 13:20:51 +0800817
Linas Vepstas8b553f32005-11-03 18:50:17 -0600818 for (i = 0; i < 16; i++)
Gavin Shan37804442012-02-27 20:04:11 +0000819 eeh_ops->read_config(dn, i * 4, 4, &edev->config_space[i]);
Gavin Shanbf898ec2013-11-12 14:49:21 +0800820
821 /*
822 * For PCI bridges including root port, we need enable bus
823 * master explicitly. Otherwise, it can't fetch IODA table
824 * entries correctly. So we cache the bit in advance so that
825 * we can restore it after reset, either PHB range or PE range.
826 */
827 if (edev->mode & EEH_DEV_BRIDGE)
828 edev->config_space[1] |= PCI_COMMAND_MASTER;
Linas Vepstas8b553f32005-11-03 18:50:17 -0600829}
830
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000831/**
Gavin Shanaa1e6372012-02-27 20:03:53 +0000832 * eeh_ops_register - Register platform dependent EEH operations
833 * @ops: platform dependent EEH operations
834 *
835 * Register the platform dependent EEH operation callback
836 * functions. The platform should call this function before
837 * any other EEH operations.
838 */
839int __init eeh_ops_register(struct eeh_ops *ops)
840{
841 if (!ops->name) {
Gavin Shan0dae2742014-07-17 14:41:41 +1000842 pr_warn("%s: Invalid EEH ops name for %p\n",
Gavin Shanaa1e6372012-02-27 20:03:53 +0000843 __func__, ops);
844 return -EINVAL;
845 }
846
847 if (eeh_ops && eeh_ops != ops) {
Gavin Shan0dae2742014-07-17 14:41:41 +1000848 pr_warn("%s: EEH ops of platform %s already existing (%s)\n",
Gavin Shanaa1e6372012-02-27 20:03:53 +0000849 __func__, eeh_ops->name, ops->name);
850 return -EEXIST;
851 }
852
853 eeh_ops = ops;
854
855 return 0;
856}
857
858/**
859 * eeh_ops_unregister - Unreigster platform dependent EEH operations
860 * @name: name of EEH platform operations
861 *
862 * Unregister the platform dependent EEH operation callback
863 * functions.
864 */
865int __exit eeh_ops_unregister(const char *name)
866{
867 if (!name || !strlen(name)) {
Gavin Shan0dae2742014-07-17 14:41:41 +1000868 pr_warn("%s: Invalid EEH ops name\n",
Gavin Shanaa1e6372012-02-27 20:03:53 +0000869 __func__);
870 return -EINVAL;
871 }
872
873 if (eeh_ops && !strcmp(eeh_ops->name, name)) {
874 eeh_ops = NULL;
875 return 0;
876 }
877
878 return -EEXIST;
879}
880
Gavin Shan66f9af832014-02-12 15:24:56 +0800881static int eeh_reboot_notifier(struct notifier_block *nb,
882 unsigned long action, void *unused)
883{
Gavin Shan05b17212014-07-17 14:41:38 +1000884 eeh_clear_flag(EEH_ENABLED);
Gavin Shan66f9af832014-02-12 15:24:56 +0800885 return NOTIFY_DONE;
886}
887
888static struct notifier_block eeh_reboot_nb = {
889 .notifier_call = eeh_reboot_notifier,
890};
891
Gavin Shanaa1e6372012-02-27 20:03:53 +0000892/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000893 * eeh_init - EEH initialization
894 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 * Initialize EEH by trying to enable it for all of the adapters in the system.
896 * As a side effect we can determine here if eeh is supported at all.
897 * Note that we leave EEH on so failed config cycles won't cause a machine
898 * check. If a user turns off EEH for a particular adapter they are really
899 * telling Linux to ignore errors. Some hardware (e.g. POWER5) won't
900 * grant access to a slot if EEH isn't enabled, and so we always enable
901 * EEH for all slots/all devices.
902 *
903 * The eeh-force-off option disables EEH checking globally, for all slots.
904 * Even if force-off is set, the EEH hardware is still enabled, so that
905 * newer systems can boot.
906 */
Gavin Shaneeb63612013-06-27 13:46:47 +0800907int eeh_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Gavin Shan1a5c2e62012-03-20 21:30:29 +0000909 struct pci_controller *hose, *tmp;
910 struct device_node *phb;
Gavin Shan51fb5f52013-06-20 13:20:56 +0800911 static int cnt = 0;
912 int ret = 0;
913
914 /*
915 * We have to delay the initialization on PowerNV after
916 * the PCI hierarchy tree has been built because the PEs
917 * are figured out based on PCI devices instead of device
918 * tree nodes
919 */
920 if (machine_is(powernv) && cnt++ <= 0)
921 return ret;
Gavin Shane2af1552012-02-27 20:03:54 +0000922
Gavin Shan66f9af832014-02-12 15:24:56 +0800923 /* Register reboot notifier */
924 ret = register_reboot_notifier(&eeh_reboot_nb);
925 if (ret) {
926 pr_warn("%s: Failed to register notifier (%d)\n",
927 __func__, ret);
928 return ret;
929 }
930
Gavin Shane2af1552012-02-27 20:03:54 +0000931 /* call platform initialization function */
932 if (!eeh_ops) {
Gavin Shan0dae2742014-07-17 14:41:41 +1000933 pr_warn("%s: Platform EEH operation not found\n",
Gavin Shane2af1552012-02-27 20:03:54 +0000934 __func__);
Gavin Shan35e5cfe2012-09-07 22:44:02 +0000935 return -EEXIST;
Greg Kurz221195f2014-11-25 17:10:06 +0100936 } else if ((ret = eeh_ops->init()))
Gavin Shan35e5cfe2012-09-07 22:44:02 +0000937 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Gavin Shanc8608552013-06-20 13:21:00 +0800939 /* Initialize EEH event */
940 ret = eeh_event_init();
941 if (ret)
942 return ret;
943
Gavin Shan1a5c2e62012-03-20 21:30:29 +0000944 /* Enable EEH for all adapters */
Gavin Shan05b17212014-07-17 14:41:38 +1000945 if (eeh_has_flag(EEH_PROBE_MODE_DEVTREE)) {
Gavin Shand7bb8862012-09-07 22:44:21 +0000946 list_for_each_entry_safe(hose, tmp,
947 &hose_list, list_node) {
948 phb = hose->dn;
949 traverse_pci_devices(phb, eeh_ops->of_probe, NULL);
950 }
Gavin Shan05b17212014-07-17 14:41:38 +1000951 } else if (eeh_has_flag(EEH_PROBE_MODE_DEV)) {
Gavin Shan51fb5f52013-06-20 13:20:56 +0800952 list_for_each_entry_safe(hose, tmp,
953 &hose_list, list_node)
954 pci_walk_bus(hose->bus, eeh_ops->dev_probe, NULL);
955 } else {
Gavin Shan8a5ad352014-04-24 18:00:17 +1000956 pr_warn("%s: Invalid probe mode %x",
957 __func__, eeh_subsystem_flags);
Gavin Shan51fb5f52013-06-20 13:20:56 +0800958 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 }
960
Gavin Shan21fd21f2013-06-20 13:20:57 +0800961 /*
962 * Call platform post-initialization. Actually, It's good chance
963 * to inform platform that EEH is ready to supply service if the
964 * I/O cache stuff has been built up.
965 */
966 if (eeh_ops->post_init) {
967 ret = eeh_ops->post_init();
968 if (ret)
969 return ret;
970 }
971
Gavin Shan2ec5a0a2014-02-12 15:24:55 +0800972 if (eeh_enabled())
Gavin Shand7bb8862012-09-07 22:44:21 +0000973 pr_info("EEH: PCI Enhanced I/O Error Handling Enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 else
Gavin Shan0dae2742014-07-17 14:41:41 +1000975 pr_warn("EEH: No capable adapters found\n");
Gavin Shan35e5cfe2012-09-07 22:44:02 +0000976
977 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979
Gavin Shan35e5cfe2012-09-07 22:44:02 +0000980core_initcall_sync(eeh_init);
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000983 * eeh_add_device_early - Enable EEH for the indicated device_node
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 * @dn: device node for which to set up EEH
985 *
986 * This routine must be used to perform EEH initialization for PCI
987 * devices that were added after system boot (e.g. hotplug, dlpar).
988 * This routine must be called before any i/o is performed to the
989 * adapter (inluding any config-space i/o).
990 * Whether this actually enables EEH or not for this device depends
991 * on the CEC architecture, type of the device, on earlier boot
992 * command-line arguments & etc.
993 */
Gavin Shanf2856492013-07-24 10:24:52 +0800994void eeh_add_device_early(struct device_node *dn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
996 struct pci_controller *phb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Gavin Shan26a74852013-06-20 13:20:59 +0800998 /*
999 * If we're doing EEH probe based on PCI device, we
1000 * would delay the probe until late stage because
1001 * the PCI device isn't available this moment.
1002 */
Gavin Shan05b17212014-07-17 14:41:38 +10001003 if (!eeh_has_flag(EEH_PROBE_MODE_DEVTREE))
Gavin Shan26a74852013-06-20 13:20:59 +08001004 return;
1005
Gavin Shan1e38b712012-09-17 04:34:28 +00001006 if (!of_node_to_eeh_dev(dn))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 return;
Gavin Shanf631acd2012-02-27 20:04:07 +00001008 phb = of_node_to_eeh_dev(dn)->phb;
Linas Vepstasf751f842005-11-03 18:54:23 -06001009
1010 /* USB Bus children of PCI devices will not have BUID's */
1011 if (NULL == phb || 0 == phb->buid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Gavin Shand7bb8862012-09-07 22:44:21 +00001014 eeh_ops->of_probe(dn, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001017/**
1018 * eeh_add_device_tree_early - Enable EEH for the indicated device
1019 * @dn: device node
1020 *
1021 * This routine must be used to perform EEH initialization for the
1022 * indicated PCI device that was added after system boot (e.g.
1023 * hotplug, dlpar).
1024 */
Linas Vepstase2a296e2005-11-03 18:51:31 -06001025void eeh_add_device_tree_early(struct device_node *dn)
1026{
1027 struct device_node *sib;
Stephen Rothwellacaa6172007-12-21 15:52:07 +11001028
1029 for_each_child_of_node(dn, sib)
Linas Vepstase2a296e2005-11-03 18:51:31 -06001030 eeh_add_device_tree_early(sib);
1031 eeh_add_device_early(dn);
1032}
1033EXPORT_SYMBOL_GPL(eeh_add_device_tree_early);
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001036 * eeh_add_device_late - Perform EEH initialization for the indicated pci device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 * @dev: pci device for which to set up EEH
1038 *
1039 * This routine must be used to complete EEH initialization for PCI
1040 * devices that were added after system boot (e.g. hotplug, dlpar).
1041 */
Gavin Shanf2856492013-07-24 10:24:52 +08001042void eeh_add_device_late(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001044 struct device_node *dn;
Gavin Shanf631acd2012-02-27 20:04:07 +00001045 struct eeh_dev *edev;
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001046
Gavin Shan2ec5a0a2014-02-12 15:24:55 +08001047 if (!dev || !eeh_enabled())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 return;
1049
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001050 pr_debug("EEH: Adding device %s\n", pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001052 dn = pci_device_to_OF_node(dev);
Gavin Shan2ef822c2012-04-16 19:55:39 +00001053 edev = of_node_to_eeh_dev(dn);
Gavin Shanf631acd2012-02-27 20:04:07 +00001054 if (edev->pdev == dev) {
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001055 pr_debug("EEH: Already referenced !\n");
1056 return;
1057 }
Gavin Shanf5c57712013-07-24 10:24:58 +08001058
1059 /*
1060 * The EEH cache might not be removed correctly because of
1061 * unbalanced kref to the device during unplug time, which
1062 * relies on pcibios_release_device(). So we have to remove
1063 * that here explicitly.
1064 */
1065 if (edev->pdev) {
1066 eeh_rmv_from_parent_pe(edev);
1067 eeh_addr_cache_rmv_dev(edev->pdev);
1068 eeh_sysfs_remove_device(edev->pdev);
Gavin Shanab55d212013-07-24 10:25:01 +08001069 edev->mode &= ~EEH_DEV_SYSFS;
Gavin Shanf5c57712013-07-24 10:24:58 +08001070
Gavin Shanf26c7a02014-01-12 14:13:45 +08001071 /*
1072 * We definitely should have the PCI device removed
1073 * though it wasn't correctly. So we needn't call
1074 * into error handler afterwards.
1075 */
1076 edev->mode |= EEH_DEV_NO_HANDLER;
1077
Gavin Shanf5c57712013-07-24 10:24:58 +08001078 edev->pdev = NULL;
1079 dev->dev.archdata.edev = NULL;
1080 }
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001081
Gavin Shanf631acd2012-02-27 20:04:07 +00001082 edev->pdev = dev;
1083 dev->dev.archdata.edev = edev;
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001084
Gavin Shan26a74852013-06-20 13:20:59 +08001085 /*
1086 * We have to do the EEH probe here because the PCI device
1087 * hasn't been created yet in the early stage.
1088 */
Gavin Shan05b17212014-07-17 14:41:38 +10001089 if (eeh_has_flag(EEH_PROBE_MODE_DEV))
Gavin Shan26a74852013-06-20 13:20:59 +08001090 eeh_ops->dev_probe(dev, NULL);
1091
Gavin Shan3ab96a02012-09-07 22:44:23 +00001092 eeh_addr_cache_insert_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
Nathan Fontenot794e0852006-03-31 12:04:52 -06001094
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001095/**
1096 * eeh_add_device_tree_late - Perform EEH initialization for the indicated PCI bus
1097 * @bus: PCI bus
1098 *
1099 * This routine must be used to perform EEH initialization for PCI
1100 * devices which are attached to the indicated PCI bus. The PCI bus
1101 * is added after system boot through hotplug or dlpar.
1102 */
Nathan Fontenot794e0852006-03-31 12:04:52 -06001103void eeh_add_device_tree_late(struct pci_bus *bus)
1104{
1105 struct pci_dev *dev;
1106
1107 list_for_each_entry(dev, &bus->devices, bus_list) {
Gavin Shana84f2732013-06-20 13:20:51 +08001108 eeh_add_device_late(dev);
1109 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
1110 struct pci_bus *subbus = dev->subordinate;
1111 if (subbus)
1112 eeh_add_device_tree_late(subbus);
1113 }
Nathan Fontenot794e0852006-03-31 12:04:52 -06001114 }
1115}
1116EXPORT_SYMBOL_GPL(eeh_add_device_tree_late);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118/**
Thadeu Lima de Souza Cascardo6a040ce2012-12-28 09:13:19 +00001119 * eeh_add_sysfs_files - Add EEH sysfs files for the indicated PCI bus
1120 * @bus: PCI bus
1121 *
1122 * This routine must be used to add EEH sysfs files for PCI
1123 * devices which are attached to the indicated PCI bus. The PCI bus
1124 * is added after system boot through hotplug or dlpar.
1125 */
1126void eeh_add_sysfs_files(struct pci_bus *bus)
1127{
1128 struct pci_dev *dev;
1129
1130 list_for_each_entry(dev, &bus->devices, bus_list) {
1131 eeh_sysfs_add_device(dev);
1132 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
1133 struct pci_bus *subbus = dev->subordinate;
1134 if (subbus)
1135 eeh_add_sysfs_files(subbus);
1136 }
1137 }
1138}
1139EXPORT_SYMBOL_GPL(eeh_add_sysfs_files);
1140
1141/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001142 * eeh_remove_device - Undo EEH setup for the indicated pci device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 * @dev: pci device to be removed
1144 *
Nathan Fontenot794e0852006-03-31 12:04:52 -06001145 * This routine should be called when a device is removed from
1146 * a running system (e.g. by hotplug or dlpar). It unregisters
1147 * the PCI device from the EEH subsystem. I/O errors affecting
1148 * this device will no longer be detected after this call; thus,
1149 * i/o errors affecting this slot may leave this device unusable.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 */
Gavin Shan807a8272013-07-24 10:24:55 +08001151void eeh_remove_device(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
Gavin Shanf631acd2012-02-27 20:04:07 +00001153 struct eeh_dev *edev;
1154
Gavin Shan2ec5a0a2014-02-12 15:24:55 +08001155 if (!dev || !eeh_enabled())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 return;
Gavin Shanf631acd2012-02-27 20:04:07 +00001157 edev = pci_dev_to_eeh_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 /* Unregister the device with the EEH/PCI address search system */
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001160 pr_debug("EEH: Removing device %s\n", pci_name(dev));
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001161
Gavin Shanf5c57712013-07-24 10:24:58 +08001162 if (!edev || !edev->pdev || !edev->pe) {
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001163 pr_debug("EEH: Not referenced !\n");
1164 return;
Linas Vepstasb055a9e2006-04-06 15:41:41 -05001165 }
Gavin Shanf5c57712013-07-24 10:24:58 +08001166
1167 /*
1168 * During the hotplug for EEH error recovery, we need the EEH
1169 * device attached to the parent PE in order for BAR restore
1170 * a bit later. So we keep it for BAR restore and remove it
1171 * from the parent PE during the BAR resotre.
1172 */
Gavin Shanf631acd2012-02-27 20:04:07 +00001173 edev->pdev = NULL;
1174 dev->dev.archdata.edev = NULL;
Gavin Shanf5c57712013-07-24 10:24:58 +08001175 if (!(edev->pe->state & EEH_PE_KEEP))
1176 eeh_rmv_from_parent_pe(edev);
1177 else
1178 edev->mode |= EEH_DEV_DISCONNECTED;
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001179
Gavin Shanf26c7a02014-01-12 14:13:45 +08001180 /*
1181 * We're removing from the PCI subsystem, that means
1182 * the PCI device driver can't support EEH or not
1183 * well. So we rely on hotplug completely to do recovery
1184 * for the specific PCI device.
1185 */
1186 edev->mode |= EEH_DEV_NO_HANDLER;
1187
Gavin Shan3ab96a02012-09-07 22:44:23 +00001188 eeh_addr_cache_rmv_dev(dev);
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001189 eeh_sysfs_remove_device(dev);
Gavin Shanab55d212013-07-24 10:25:01 +08001190 edev->mode &= ~EEH_DEV_SYSFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Gavin Shan4eeeff02014-09-30 12:39:01 +10001193int eeh_unfreeze_pe(struct eeh_pe *pe, bool sw_state)
1194{
1195 int ret;
1196
1197 ret = eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
1198 if (ret) {
1199 pr_warn("%s: Failure %d enabling IO on PHB#%x-PE#%x\n",
1200 __func__, ret, pe->phb->global_number, pe->addr);
1201 return ret;
1202 }
1203
1204 ret = eeh_pci_enable(pe, EEH_OPT_THAW_DMA);
1205 if (ret) {
1206 pr_warn("%s: Failure %d enabling DMA on PHB#%x-PE#%x\n",
1207 __func__, ret, pe->phb->global_number, pe->addr);
1208 return ret;
1209 }
1210
1211 /* Clear software isolated state */
1212 if (sw_state && (pe->state & EEH_PE_ISOLATED))
1213 eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
1214
1215 return ret;
1216}
1217
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001218
1219static struct pci_device_id eeh_reset_ids[] = {
1220 { PCI_DEVICE(0x19a2, 0x0710) }, /* Emulex, BE */
1221 { PCI_DEVICE(0x10df, 0xe220) }, /* Emulex, Lancer */
Gavin Shanb1d76a72014-11-14 10:47:30 +11001222 { PCI_DEVICE(0x14e4, 0x1657) }, /* Broadcom BCM5719 */
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001223 { 0 }
1224};
1225
1226static int eeh_pe_change_owner(struct eeh_pe *pe)
1227{
1228 struct eeh_dev *edev, *tmp;
1229 struct pci_dev *pdev;
1230 struct pci_device_id *id;
1231 int flags, ret;
1232
1233 /* Check PE state */
1234 flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
1235 ret = eeh_ops->get_state(pe, NULL);
1236 if (ret < 0 || ret == EEH_STATE_NOT_SUPPORT)
1237 return 0;
1238
1239 /* Unfrozen PE, nothing to do */
1240 if ((ret & flags) == flags)
1241 return 0;
1242
1243 /* Frozen PE, check if it needs PE level reset */
1244 eeh_pe_for_each_dev(pe, edev, tmp) {
1245 pdev = eeh_dev_to_pci_dev(edev);
1246 if (!pdev)
1247 continue;
1248
1249 for (id = &eeh_reset_ids[0]; id->vendor != 0; id++) {
1250 if (id->vendor != PCI_ANY_ID &&
1251 id->vendor != pdev->vendor)
1252 continue;
1253 if (id->device != PCI_ANY_ID &&
1254 id->device != pdev->device)
1255 continue;
1256 if (id->subvendor != PCI_ANY_ID &&
1257 id->subvendor != pdev->subsystem_vendor)
1258 continue;
1259 if (id->subdevice != PCI_ANY_ID &&
1260 id->subdevice != pdev->subsystem_device)
1261 continue;
1262
1263 goto reset;
1264 }
1265 }
1266
1267 return eeh_unfreeze_pe(pe, true);
1268
1269reset:
1270 return eeh_pe_reset_and_recover(pe);
1271}
1272
Gavin Shan212d16c2014-06-10 11:41:56 +10001273/**
1274 * eeh_dev_open - Increase count of pass through devices for PE
1275 * @pdev: PCI device
1276 *
1277 * Increase count of passed through devices for the indicated
1278 * PE. In the result, the EEH errors detected on the PE won't be
1279 * reported. The PE owner will be responsible for detection
1280 * and recovery.
1281 */
1282int eeh_dev_open(struct pci_dev *pdev)
1283{
1284 struct eeh_dev *edev;
Gavin Shan404079c2014-09-30 12:38:54 +10001285 int ret = -ENODEV;
Gavin Shan212d16c2014-06-10 11:41:56 +10001286
1287 mutex_lock(&eeh_dev_mutex);
1288
1289 /* No PCI device ? */
1290 if (!pdev)
1291 goto out;
1292
1293 /* No EEH device or PE ? */
1294 edev = pci_dev_to_eeh_dev(pdev);
1295 if (!edev || !edev->pe)
1296 goto out;
1297
Gavin Shan404079c2014-09-30 12:38:54 +10001298 /*
1299 * The PE might have been put into frozen state, but we
1300 * didn't detect that yet. The passed through PCI devices
1301 * in frozen PE won't work properly. Clear the frozen state
1302 * in advance.
1303 */
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001304 ret = eeh_pe_change_owner(edev->pe);
Gavin Shan4eeeff02014-09-30 12:39:01 +10001305 if (ret)
1306 goto out;
Gavin Shan404079c2014-09-30 12:38:54 +10001307
Gavin Shan212d16c2014-06-10 11:41:56 +10001308 /* Increase PE's pass through count */
1309 atomic_inc(&edev->pe->pass_dev_cnt);
1310 mutex_unlock(&eeh_dev_mutex);
1311
1312 return 0;
1313out:
1314 mutex_unlock(&eeh_dev_mutex);
Gavin Shan404079c2014-09-30 12:38:54 +10001315 return ret;
Gavin Shan212d16c2014-06-10 11:41:56 +10001316}
1317EXPORT_SYMBOL_GPL(eeh_dev_open);
1318
1319/**
1320 * eeh_dev_release - Decrease count of pass through devices for PE
1321 * @pdev: PCI device
1322 *
1323 * Decrease count of pass through devices for the indicated PE. If
1324 * there is no passed through device in PE, the EEH errors detected
1325 * on the PE will be reported and handled as usual.
1326 */
1327void eeh_dev_release(struct pci_dev *pdev)
1328{
1329 struct eeh_dev *edev;
1330
1331 mutex_lock(&eeh_dev_mutex);
1332
1333 /* No PCI device ? */
1334 if (!pdev)
1335 goto out;
1336
1337 /* No EEH device ? */
1338 edev = pci_dev_to_eeh_dev(pdev);
1339 if (!edev || !edev->pe || !eeh_pe_passed(edev->pe))
1340 goto out;
1341
1342 /* Decrease PE's pass through count */
1343 atomic_dec(&edev->pe->pass_dev_cnt);
1344 WARN_ON(atomic_read(&edev->pe->pass_dev_cnt) < 0);
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001345 eeh_pe_change_owner(edev->pe);
Gavin Shan212d16c2014-06-10 11:41:56 +10001346out:
1347 mutex_unlock(&eeh_dev_mutex);
1348}
1349EXPORT_SYMBOL(eeh_dev_release);
1350
Benjamin Herrenschmidt2194dc22014-08-05 18:52:59 +10001351#ifdef CONFIG_IOMMU_API
1352
Gavin Shana3032ca2014-07-15 17:00:56 +10001353static int dev_has_iommu_table(struct device *dev, void *data)
1354{
1355 struct pci_dev *pdev = to_pci_dev(dev);
1356 struct pci_dev **ppdev = data;
1357 struct iommu_table *tbl;
1358
1359 if (!dev)
1360 return 0;
1361
1362 tbl = get_iommu_table_base(dev);
1363 if (tbl && tbl->it_group) {
1364 *ppdev = pdev;
1365 return 1;
1366 }
1367
1368 return 0;
1369}
1370
Gavin Shan212d16c2014-06-10 11:41:56 +10001371/**
1372 * eeh_iommu_group_to_pe - Convert IOMMU group to EEH PE
1373 * @group: IOMMU group
1374 *
1375 * The routine is called to convert IOMMU group to EEH PE.
1376 */
1377struct eeh_pe *eeh_iommu_group_to_pe(struct iommu_group *group)
1378{
Gavin Shan212d16c2014-06-10 11:41:56 +10001379 struct pci_dev *pdev = NULL;
1380 struct eeh_dev *edev;
Gavin Shana3032ca2014-07-15 17:00:56 +10001381 int ret;
Gavin Shan212d16c2014-06-10 11:41:56 +10001382
1383 /* No IOMMU group ? */
1384 if (!group)
1385 return NULL;
1386
Gavin Shana3032ca2014-07-15 17:00:56 +10001387 ret = iommu_group_for_each_dev(group, &pdev, dev_has_iommu_table);
1388 if (!ret || !pdev)
Gavin Shan212d16c2014-06-10 11:41:56 +10001389 return NULL;
1390
1391 /* No EEH device or PE ? */
1392 edev = pci_dev_to_eeh_dev(pdev);
1393 if (!edev || !edev->pe)
1394 return NULL;
1395
1396 return edev->pe;
1397}
Gavin Shan537e5402014-08-07 12:47:16 +10001398EXPORT_SYMBOL_GPL(eeh_iommu_group_to_pe);
Gavin Shan212d16c2014-06-10 11:41:56 +10001399
Benjamin Herrenschmidt2194dc22014-08-05 18:52:59 +10001400#endif /* CONFIG_IOMMU_API */
1401
Gavin Shan212d16c2014-06-10 11:41:56 +10001402/**
1403 * eeh_pe_set_option - Set options for the indicated PE
1404 * @pe: EEH PE
1405 * @option: requested option
1406 *
1407 * The routine is called to enable or disable EEH functionality
1408 * on the indicated PE, to enable IO or DMA for the frozen PE.
1409 */
1410int eeh_pe_set_option(struct eeh_pe *pe, int option)
1411{
1412 int ret = 0;
1413
1414 /* Invalid PE ? */
1415 if (!pe)
1416 return -ENODEV;
1417
1418 /*
1419 * EEH functionality could possibly be disabled, just
1420 * return error for the case. And the EEH functinality
1421 * isn't expected to be disabled on one specific PE.
1422 */
1423 switch (option) {
1424 case EEH_OPT_ENABLE:
Gavin Shan4eeeff02014-09-30 12:39:01 +10001425 if (eeh_enabled()) {
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001426 ret = eeh_pe_change_owner(pe);
Gavin Shan212d16c2014-06-10 11:41:56 +10001427 break;
Gavin Shan4eeeff02014-09-30 12:39:01 +10001428 }
Gavin Shan212d16c2014-06-10 11:41:56 +10001429 ret = -EIO;
1430 break;
1431 case EEH_OPT_DISABLE:
1432 break;
1433 case EEH_OPT_THAW_MMIO:
1434 case EEH_OPT_THAW_DMA:
1435 if (!eeh_ops || !eeh_ops->set_option) {
1436 ret = -ENOENT;
1437 break;
1438 }
1439
Gavin Shan4eeeff02014-09-30 12:39:01 +10001440 ret = eeh_pci_enable(pe, option);
Gavin Shan212d16c2014-06-10 11:41:56 +10001441 break;
1442 default:
1443 pr_debug("%s: Option %d out of range (%d, %d)\n",
1444 __func__, option, EEH_OPT_DISABLE, EEH_OPT_THAW_DMA);
1445 ret = -EINVAL;
1446 }
1447
1448 return ret;
1449}
1450EXPORT_SYMBOL_GPL(eeh_pe_set_option);
1451
1452/**
1453 * eeh_pe_get_state - Retrieve PE's state
1454 * @pe: EEH PE
1455 *
1456 * Retrieve the PE's state, which includes 3 aspects: enabled
1457 * DMA, enabled IO and asserted reset.
1458 */
1459int eeh_pe_get_state(struct eeh_pe *pe)
1460{
1461 int result, ret = 0;
1462 bool rst_active, dma_en, mmio_en;
1463
1464 /* Existing PE ? */
1465 if (!pe)
1466 return -ENODEV;
1467
1468 if (!eeh_ops || !eeh_ops->get_state)
1469 return -ENOENT;
1470
1471 result = eeh_ops->get_state(pe, NULL);
1472 rst_active = !!(result & EEH_STATE_RESET_ACTIVE);
1473 dma_en = !!(result & EEH_STATE_DMA_ENABLED);
1474 mmio_en = !!(result & EEH_STATE_MMIO_ENABLED);
1475
1476 if (rst_active)
1477 ret = EEH_PE_STATE_RESET;
1478 else if (dma_en && mmio_en)
1479 ret = EEH_PE_STATE_NORMAL;
1480 else if (!dma_en && !mmio_en)
1481 ret = EEH_PE_STATE_STOPPED_IO_DMA;
1482 else if (!dma_en && mmio_en)
1483 ret = EEH_PE_STATE_STOPPED_DMA;
1484 else
1485 ret = EEH_PE_STATE_UNAVAIL;
1486
1487 return ret;
1488}
1489EXPORT_SYMBOL_GPL(eeh_pe_get_state);
1490
Gavin Shan316233f2014-09-30 12:38:53 +10001491static int eeh_pe_reenable_devices(struct eeh_pe *pe)
1492{
1493 struct eeh_dev *edev, *tmp;
1494 struct pci_dev *pdev;
1495 int ret = 0;
1496
1497 /* Restore config space */
1498 eeh_pe_restore_bars(pe);
1499
1500 /*
1501 * Reenable PCI devices as the devices passed
1502 * through are always enabled before the reset.
1503 */
1504 eeh_pe_for_each_dev(pe, edev, tmp) {
1505 pdev = eeh_dev_to_pci_dev(edev);
1506 if (!pdev)
1507 continue;
1508
1509 ret = pci_reenable_device(pdev);
1510 if (ret) {
1511 pr_warn("%s: Failure %d reenabling %s\n",
1512 __func__, ret, pci_name(pdev));
1513 return ret;
1514 }
1515 }
1516
1517 /* The PE is still in frozen state */
Gavin Shanc9dd0142014-09-30 12:39:02 +10001518 return eeh_unfreeze_pe(pe, true);
Gavin Shan316233f2014-09-30 12:38:53 +10001519}
1520
Gavin Shan212d16c2014-06-10 11:41:56 +10001521/**
1522 * eeh_pe_reset - Issue PE reset according to specified type
1523 * @pe: EEH PE
1524 * @option: reset type
1525 *
1526 * The routine is called to reset the specified PE with the
1527 * indicated type, either fundamental reset or hot reset.
1528 * PE reset is the most important part for error recovery.
1529 */
1530int eeh_pe_reset(struct eeh_pe *pe, int option)
1531{
1532 int ret = 0;
1533
1534 /* Invalid PE ? */
1535 if (!pe)
1536 return -ENODEV;
1537
1538 if (!eeh_ops || !eeh_ops->set_option || !eeh_ops->reset)
1539 return -ENOENT;
1540
1541 switch (option) {
1542 case EEH_RESET_DEACTIVATE:
1543 ret = eeh_ops->reset(pe, option);
Gavin Shan8a6b3712014-10-01 17:07:50 +10001544 eeh_pe_state_clear(pe, EEH_PE_CFG_BLOCKED);
Gavin Shan212d16c2014-06-10 11:41:56 +10001545 if (ret)
1546 break;
1547
Gavin Shan316233f2014-09-30 12:38:53 +10001548 ret = eeh_pe_reenable_devices(pe);
Gavin Shan212d16c2014-06-10 11:41:56 +10001549 break;
1550 case EEH_RESET_HOT:
1551 case EEH_RESET_FUNDAMENTAL:
Gavin Shan0d5ee522014-09-30 12:38:52 +10001552 /*
1553 * Proactively freeze the PE to drop all MMIO access
1554 * during reset, which should be banned as it's always
1555 * cause recursive EEH error.
1556 */
1557 eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);
1558
Gavin Shan8a6b3712014-10-01 17:07:50 +10001559 eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
Gavin Shan212d16c2014-06-10 11:41:56 +10001560 ret = eeh_ops->reset(pe, option);
1561 break;
1562 default:
1563 pr_debug("%s: Unsupported option %d\n",
1564 __func__, option);
1565 ret = -EINVAL;
1566 }
1567
1568 return ret;
1569}
1570EXPORT_SYMBOL_GPL(eeh_pe_reset);
1571
1572/**
1573 * eeh_pe_configure - Configure PCI bridges after PE reset
1574 * @pe: EEH PE
1575 *
1576 * The routine is called to restore the PCI config space for
1577 * those PCI devices, especially PCI bridges affected by PE
1578 * reset issued previously.
1579 */
1580int eeh_pe_configure(struct eeh_pe *pe)
1581{
1582 int ret = 0;
1583
1584 /* Invalid PE ? */
1585 if (!pe)
1586 return -ENODEV;
1587
Gavin Shan212d16c2014-06-10 11:41:56 +10001588 return ret;
1589}
1590EXPORT_SYMBOL_GPL(eeh_pe_configure);
1591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592static int proc_eeh_show(struct seq_file *m, void *v)
1593{
Gavin Shan2ec5a0a2014-02-12 15:24:55 +08001594 if (!eeh_enabled()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 seq_printf(m, "EEH Subsystem is globally disabled\n");
Gavin Shane575f8d2012-02-29 15:47:45 +00001596 seq_printf(m, "eeh_total_mmio_ffs=%llu\n", eeh_stats.total_mmio_ffs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 } else {
1598 seq_printf(m, "EEH Subsystem is enabled\n");
Linas Vepstas177bc932005-11-03 18:48:52 -06001599 seq_printf(m,
Gavin Shane575f8d2012-02-29 15:47:45 +00001600 "no device=%llu\n"
1601 "no device node=%llu\n"
1602 "no config address=%llu\n"
1603 "check not wanted=%llu\n"
1604 "eeh_total_mmio_ffs=%llu\n"
1605 "eeh_false_positives=%llu\n"
1606 "eeh_slot_resets=%llu\n",
1607 eeh_stats.no_device,
1608 eeh_stats.no_dn,
1609 eeh_stats.no_cfg_addr,
1610 eeh_stats.ignored_check,
1611 eeh_stats.total_mmio_ffs,
1612 eeh_stats.false_positives,
1613 eeh_stats.slot_resets);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 }
1615
1616 return 0;
1617}
1618
1619static int proc_eeh_open(struct inode *inode, struct file *file)
1620{
1621 return single_open(file, proc_eeh_show, NULL);
1622}
1623
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001624static const struct file_operations proc_eeh_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 .open = proc_eeh_open,
1626 .read = seq_read,
1627 .llseek = seq_lseek,
1628 .release = single_release,
1629};
1630
Gavin Shan7f52a522014-04-24 18:00:18 +10001631#ifdef CONFIG_DEBUG_FS
1632static int eeh_enable_dbgfs_set(void *data, u64 val)
1633{
1634 if (val)
Gavin Shan05b17212014-07-17 14:41:38 +10001635 eeh_clear_flag(EEH_FORCE_DISABLED);
Gavin Shan7f52a522014-04-24 18:00:18 +10001636 else
Gavin Shan05b17212014-07-17 14:41:38 +10001637 eeh_add_flag(EEH_FORCE_DISABLED);
Gavin Shan7f52a522014-04-24 18:00:18 +10001638
1639 /* Notify the backend */
1640 if (eeh_ops->post_init)
1641 eeh_ops->post_init();
1642
1643 return 0;
1644}
1645
1646static int eeh_enable_dbgfs_get(void *data, u64 *val)
1647{
1648 if (eeh_enabled())
1649 *val = 0x1ul;
1650 else
1651 *val = 0x0ul;
1652 return 0;
1653}
1654
1655DEFINE_SIMPLE_ATTRIBUTE(eeh_enable_dbgfs_ops, eeh_enable_dbgfs_get,
1656 eeh_enable_dbgfs_set, "0x%llx\n");
1657#endif
1658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659static int __init eeh_init_proc(void)
1660{
Gavin Shan7f52a522014-04-24 18:00:18 +10001661 if (machine_is(pseries) || machine_is(powernv)) {
Thadeu Lima de Souza Cascardo8feaa432011-08-26 10:36:31 +00001662 proc_create("powerpc/eeh", 0, NULL, &proc_eeh_operations);
Gavin Shan7f52a522014-04-24 18:00:18 +10001663#ifdef CONFIG_DEBUG_FS
1664 debugfs_create_file("eeh_enable", 0600,
1665 powerpc_debugfs_root, NULL,
1666 &eeh_enable_dbgfs_ops);
1667#endif
1668 }
1669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 return 0;
1671}
1672__initcall(eeh_init_proc);