blob: b7338a9426dfe1a8f72c769b6b952d9bac86599f [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 Shan7f52a5262014-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 Shan1b28f172014-12-11 14:28:56 +1100107/*
108 * EEH allowed maximal frozen times. If one particular PE's
109 * frozen count in last hour exceeds this limit, the PE will
110 * be forced to be offline permanently.
111 */
112int eeh_max_freezes = 5;
113
Gavin Shanaa1e6372012-02-27 20:03:53 +0000114/* Platform dependent EEH operations */
115struct eeh_ops *eeh_ops = NULL;
116
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600117/* Lock to avoid races due to multiple reports of an error */
Gavin Shan49075812013-06-20 13:21:03 +0800118DEFINE_RAW_SPINLOCK(confirm_error_lock);
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600119
Gavin Shan212d16c2014-06-10 11:41:56 +1000120/* Lock to protect passed flags */
121static DEFINE_MUTEX(eeh_dev_mutex);
122
Linas Vepstas17213c32007-05-10 02:38:11 +1000123/* Buffer for reporting pci register dumps. Its here in BSS, and
124 * not dynamically alloced, so that it ends up in RMO where RTAS
125 * can access it.
126 */
Gavin Shanf2e0be52014-09-30 12:39:08 +1000127#define EEH_PCI_REGS_LOG_LEN 8192
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000128static unsigned char pci_regs_buf[EEH_PCI_REGS_LOG_LEN];
129
Gavin Shane575f8d2012-02-29 15:47:45 +0000130/*
131 * The struct is used to maintain the EEH global statistic
132 * information. Besides, the EEH global statistics will be
133 * exported to user space through procfs
134 */
135struct eeh_stats {
136 u64 no_device; /* PCI device not found */
137 u64 no_dn; /* OF node not found */
138 u64 no_cfg_addr; /* Config address not found */
139 u64 ignored_check; /* EEH check skipped */
140 u64 total_mmio_ffs; /* Total EEH checks */
141 u64 false_positives; /* Unnecessary EEH checks */
142 u64 slot_resets; /* PE reset */
143};
144
145static struct eeh_stats eeh_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Gavin Shan7f52a5262014-04-24 18:00:18 +1000147static int __init eeh_setup(char *str)
148{
149 if (!strcmp(str, "off"))
Gavin Shan05b17212014-07-17 14:41:38 +1000150 eeh_add_flag(EEH_FORCE_DISABLED);
Gavin Shana450e8f2014-11-22 21:58:09 +1100151 else if (!strcmp(str, "early_log"))
152 eeh_add_flag(EEH_EARLY_DUMP_LOG);
Gavin Shan7f52a5262014-04-24 18:00:18 +1000153
154 return 1;
155}
156__setup("eeh=", eeh_setup);
157
Gavin Shanf2e0be52014-09-30 12:39:08 +1000158/*
159 * This routine captures assorted PCI configuration space data
160 * for the indicated PCI device, and puts them into a buffer
161 * for RTAS error logging.
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000162 */
Gavin Shanf2e0be52014-09-30 12:39:08 +1000163static size_t eeh_dump_dev_log(struct eeh_dev *edev, char *buf, size_t len)
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000164{
Gavin Shan0bd78582015-03-17 16:15:07 +1100165 struct pci_dn *pdn = eeh_dev_to_pdn(edev);
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000166 u32 cfg;
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000167 int cap, i;
Gavin Shan0ed352d2014-07-17 14:41:40 +1000168 int n = 0, l = 0;
169 char buffer[128];
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000170
Gavin Shan0bd78582015-03-17 16:15:07 +1100171 n += scnprintf(buf+n, len-n, "%04x:%02x:%02x:%01x\n",
172 edev->phb->global_number, pdn->busno,
173 PCI_SLOT(pdn->devfn), PCI_FUNC(pdn->devfn));
174 pr_warn("EEH: of node=%04x:%02x:%02x:%01x\n",
175 edev->phb->global_number, pdn->busno,
176 PCI_SLOT(pdn->devfn), PCI_FUNC(pdn->devfn));
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000177
Gavin Shan0bd78582015-03-17 16:15:07 +1100178 eeh_ops->read_config(pdn, PCI_VENDOR_ID, 4, &cfg);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000179 n += scnprintf(buf+n, len-n, "dev/vend:%08x\n", cfg);
Gavin Shan2d86c382014-04-24 18:00:15 +1000180 pr_warn("EEH: PCI device/vendor: %08x\n", cfg);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000181
Gavin Shan0bd78582015-03-17 16:15:07 +1100182 eeh_ops->read_config(pdn, PCI_COMMAND, 4, &cfg);
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000183 n += scnprintf(buf+n, len-n, "cmd/stat:%x\n", cfg);
Gavin Shan2d86c382014-04-24 18:00:15 +1000184 pr_warn("EEH: PCI cmd/status register: %08x\n", cfg);
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000185
Linas Vepstas0b9369f2007-07-27 08:35:40 +1000186 /* Gather bridge-specific registers */
Gavin Shan2a18dfc2014-04-24 18:00:16 +1000187 if (edev->mode & EEH_DEV_BRIDGE) {
Gavin Shan0bd78582015-03-17 16:15:07 +1100188 eeh_ops->read_config(pdn, PCI_SEC_STATUS, 2, &cfg);
Linas Vepstas0b9369f2007-07-27 08:35:40 +1000189 n += scnprintf(buf+n, len-n, "sec stat:%x\n", cfg);
Gavin Shan2d86c382014-04-24 18:00:15 +1000190 pr_warn("EEH: Bridge secondary status: %04x\n", cfg);
Linas Vepstas0b9369f2007-07-27 08:35:40 +1000191
Gavin Shan0bd78582015-03-17 16:15:07 +1100192 eeh_ops->read_config(pdn, PCI_BRIDGE_CONTROL, 2, &cfg);
Linas Vepstas0b9369f2007-07-27 08:35:40 +1000193 n += scnprintf(buf+n, len-n, "brdg ctl:%x\n", cfg);
Gavin Shan2d86c382014-04-24 18:00:15 +1000194 pr_warn("EEH: Bridge control: %04x\n", cfg);
Linas Vepstas0b9369f2007-07-27 08:35:40 +1000195 }
196
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000197 /* Dump out the PCI-X command and status regs */
Gavin Shan2a18dfc2014-04-24 18:00:16 +1000198 cap = edev->pcix_cap;
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000199 if (cap) {
Gavin Shan0bd78582015-03-17 16:15:07 +1100200 eeh_ops->read_config(pdn, cap, 4, &cfg);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000201 n += scnprintf(buf+n, len-n, "pcix-cmd:%x\n", cfg);
Gavin Shan2d86c382014-04-24 18:00:15 +1000202 pr_warn("EEH: PCI-X cmd: %08x\n", cfg);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000203
Gavin Shan0bd78582015-03-17 16:15:07 +1100204 eeh_ops->read_config(pdn, cap+4, 4, &cfg);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000205 n += scnprintf(buf+n, len-n, "pcix-stat:%x\n", cfg);
Gavin Shan2d86c382014-04-24 18:00:15 +1000206 pr_warn("EEH: PCI-X status: %08x\n", cfg);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000207 }
208
Gavin Shan2a18dfc2014-04-24 18:00:16 +1000209 /* If PCI-E capable, dump PCI-E cap 10 */
210 cap = edev->pcie_cap;
211 if (cap) {
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000212 n += scnprintf(buf+n, len-n, "pci-e cap10:\n");
Gavin Shan2d86c382014-04-24 18:00:15 +1000213 pr_warn("EEH: PCI-E capabilities and status follow:\n");
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000214
215 for (i=0; i<=8; i++) {
Gavin Shan0bd78582015-03-17 16:15:07 +1100216 eeh_ops->read_config(pdn, cap+4*i, 4, &cfg);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000217 n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
Gavin Shan0ed352d2014-07-17 14:41:40 +1000218
219 if ((i % 4) == 0) {
220 if (i != 0)
221 pr_warn("%s\n", buffer);
222
223 l = scnprintf(buffer, sizeof(buffer),
224 "EEH: PCI-E %02x: %08x ",
225 4*i, cfg);
226 } else {
227 l += scnprintf(buffer+l, sizeof(buffer)-l,
228 "%08x ", cfg);
229 }
230
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000231 }
Gavin Shan0ed352d2014-07-17 14:41:40 +1000232
233 pr_warn("%s\n", buffer);
Gavin Shan2a18dfc2014-04-24 18:00:16 +1000234 }
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000235
Gavin Shan2a18dfc2014-04-24 18:00:16 +1000236 /* If AER capable, dump it */
237 cap = edev->aer_cap;
238 if (cap) {
239 n += scnprintf(buf+n, len-n, "pci-e AER:\n");
240 pr_warn("EEH: PCI-E AER capability register set follows:\n");
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000241
Gavin Shan0ed352d2014-07-17 14:41:40 +1000242 for (i=0; i<=13; i++) {
Gavin Shan0bd78582015-03-17 16:15:07 +1100243 eeh_ops->read_config(pdn, cap+4*i, 4, &cfg);
Gavin Shan2a18dfc2014-04-24 18:00:16 +1000244 n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
Gavin Shan0ed352d2014-07-17 14:41:40 +1000245
246 if ((i % 4) == 0) {
247 if (i != 0)
248 pr_warn("%s\n", buffer);
249
250 l = scnprintf(buffer, sizeof(buffer),
251 "EEH: PCI-E AER %02x: %08x ",
252 4*i, cfg);
253 } else {
254 l += scnprintf(buffer+l, sizeof(buffer)-l,
255 "%08x ", cfg);
256 }
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000257 }
Gavin Shan0ed352d2014-07-17 14:41:40 +1000258
259 pr_warn("%s\n", buffer);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000260 }
Linas Vepstas0b9369f2007-07-27 08:35:40 +1000261
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000262 return n;
263}
264
Gavin Shanf2e0be52014-09-30 12:39:08 +1000265static void *eeh_dump_pe_log(void *data, void *flag)
266{
267 struct eeh_pe *pe = data;
268 struct eeh_dev *edev, *tmp;
269 size_t *plen = flag;
270
Gavin Shanc59004c2014-10-01 17:07:54 +1000271 /* If the PE's config space is blocked, 0xFF's will be
272 * returned. It's pointless to collect the log in this
273 * case.
274 */
275 if (pe->state & EEH_PE_CFG_BLOCKED)
276 return NULL;
277
Gavin Shanf2e0be52014-09-30 12:39:08 +1000278 eeh_pe_for_each_dev(pe, edev, tmp)
279 *plen += eeh_dump_dev_log(edev, pci_regs_buf + *plen,
280 EEH_PCI_REGS_LOG_LEN - *plen);
281
282 return NULL;
283}
284
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000285/**
286 * eeh_slot_error_detail - Generate combined log including driver log and error log
Gavin Shanff477962012-09-07 22:44:16 +0000287 * @pe: EEH PE
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000288 * @severity: temporary or permanent error log
289 *
290 * This routine should be called to generate the combined log, which
291 * is comprised of driver log and error log. The driver log is figured
292 * out from the config space of the corresponding PCI device, while
293 * the error log is fetched through platform dependent function call.
294 */
Gavin Shanff477962012-09-07 22:44:16 +0000295void eeh_slot_error_detail(struct eeh_pe *pe, int severity)
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000296{
297 size_t loglen = 0;
Gavin Shanff477962012-09-07 22:44:16 +0000298
Gavin Shanc35ae172013-06-27 13:46:42 +0800299 /*
300 * When the PHB is fenced or dead, it's pointless to collect
301 * the data from PCI config space because it should return
302 * 0xFF's. For ER, we still retrieve the data from the PCI
303 * config space.
Gavin Shan78954702014-04-24 18:00:14 +1000304 *
305 * For pHyp, we have to enable IO for log retrieval. Otherwise,
306 * 0xFF's is always returned from PCI config space.
Gavin Shanc35ae172013-06-27 13:46:42 +0800307 */
Gavin Shan9e049372014-04-24 18:00:07 +1000308 if (!(pe->type & EEH_PE_PHB)) {
Gavin Shandc561fb2014-07-17 14:41:39 +1000309 if (eeh_has_flag(EEH_ENABLE_IO_FOR_LOG))
Gavin Shan78954702014-04-24 18:00:14 +1000310 eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
Gavin Shanc35ae172013-06-27 13:46:42 +0800311
Gavin Shan25980012015-08-28 11:57:00 +1000312 /*
313 * The config space of some PCI devices can't be accessed
314 * when their PEs are in frozen state. Otherwise, fenced
315 * PHB might be seen. Those PEs are identified with flag
316 * EEH_PE_CFG_RESTRICTED, indicating EEH_PE_CFG_BLOCKED
317 * is set automatically when the PE is put to EEH_PE_ISOLATED.
318 *
319 * Restoring BARs possibly triggers PCI config access in
320 * (OPAL) firmware and then causes fenced PHB. If the
321 * PCI config is blocked with flag EEH_PE_CFG_BLOCKED, it's
322 * pointless to restore BARs and dump config space.
323 */
324 eeh_ops->configure_bridge(pe);
325 if (!(pe->state & EEH_PE_CFG_BLOCKED)) {
326 eeh_pe_restore_bars(pe);
327
328 pci_regs_buf[0] = 0;
329 eeh_pe_traverse(pe, eeh_dump_pe_log, &loglen);
330 }
Gavin Shanc35ae172013-06-27 13:46:42 +0800331 }
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000332
Gavin Shanff477962012-09-07 22:44:16 +0000333 eeh_ops->get_log(pe, severity, pci_regs_buf, loglen);
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000334}
335
336/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000337 * eeh_token_to_phys - Convert EEH address token to phys address
338 * @token: I/O token, should be address in the form 0xA....
339 *
340 * This routine should be called to convert virtual I/O address
341 * to physical one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 */
343static inline unsigned long eeh_token_to_phys(unsigned long token)
344{
345 pte_t *ptep;
346 unsigned long pa;
Aneesh Kumar K.V12bc9f62013-06-20 14:30:18 +0530347 int hugepage_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Aneesh Kumar K.V12bc9f62013-06-20 14:30:18 +0530349 /*
Aneesh Kumar K.V691e95f2015-03-30 10:41:03 +0530350 * We won't find hugepages here(this is iomem). Hence we are not
351 * worried about _PAGE_SPLITTING/collapse. Also we will not hit
352 * page table free, because of init_mm.
Aneesh Kumar K.V12bc9f62013-06-20 14:30:18 +0530353 */
Aneesh Kumar K.V891121e2015-10-09 08:32:21 +0530354 ptep = __find_linux_pte_or_hugepte(init_mm.pgd, token,
355 NULL, &hugepage_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (!ptep)
357 return token;
Aneesh Kumar K.V12bc9f62013-06-20 14:30:18 +0530358 WARN_ON(hugepage_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 pa = pte_pfn(*ptep) << PAGE_SHIFT;
360
361 return pa | (token & (PAGE_SIZE-1));
362}
363
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800364/*
365 * On PowerNV platform, we might already have fenced PHB there.
366 * For that case, it's meaningless to recover frozen PE. Intead,
367 * We have to handle fenced PHB firstly.
368 */
369static int eeh_phb_check_failure(struct eeh_pe *pe)
370{
371 struct eeh_pe *phb_pe;
372 unsigned long flags;
373 int ret;
374
Gavin Shan05b17212014-07-17 14:41:38 +1000375 if (!eeh_has_flag(EEH_PROBE_MODE_DEV))
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800376 return -EPERM;
377
378 /* Find the PHB PE */
379 phb_pe = eeh_phb_pe_get(pe->phb);
380 if (!phb_pe) {
Gavin Shan0dae2742014-07-17 14:41:41 +1000381 pr_warn("%s Can't find PE for PHB#%d\n",
382 __func__, pe->phb->global_number);
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800383 return -EEXIST;
384 }
385
386 /* If the PHB has been in problematic state */
387 eeh_serialize_lock(&flags);
Gavin Shan9e049372014-04-24 18:00:07 +1000388 if (phb_pe->state & EEH_PE_ISOLATED) {
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800389 ret = 0;
390 goto out;
391 }
392
393 /* Check PHB state */
394 ret = eeh_ops->get_state(phb_pe, NULL);
395 if ((ret < 0) ||
396 (ret == EEH_STATE_NOT_SUPPORT) ||
397 (ret & (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) ==
398 (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) {
399 ret = 0;
400 goto out;
401 }
402
403 /* Isolate the PHB and send event */
404 eeh_pe_state_mark(phb_pe, EEH_PE_ISOLATED);
405 eeh_serialize_unlock(flags);
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800406
Gavin Shan357b2f32014-06-11 18:26:44 +1000407 pr_err("EEH: PHB#%x failure detected, location: %s\n",
408 phb_pe->phb->global_number, eeh_pe_loc_get(phb_pe));
Gavin Shan56ca4fd2013-06-27 13:46:46 +0800409 dump_stack();
Gavin Shan5293bf92013-09-06 09:00:05 +0800410 eeh_send_failure_event(phb_pe);
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800411
412 return 1;
413out:
414 eeh_serialize_unlock(flags);
415 return ret;
416}
417
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000418/**
Gavin Shanf8f7d632012-09-07 22:44:22 +0000419 * eeh_dev_check_failure - Check if all 1's data is due to EEH slot freeze
420 * @edev: eeh device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 *
422 * Check for an EEH failure for the given device node. Call this
423 * routine if the result of a read was all 0xff's and you want to
424 * find out if this is due to an EEH slot freeze. This routine
425 * will query firmware for the EEH status.
426 *
427 * Returns 0 if there has not been an EEH error; otherwise returns
Linas Vepstas69376502005-11-03 18:47:50 -0600428 * a non-zero value and queues up a slot isolation event notification.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 *
430 * It is safe to call this routine in an interrupt context.
431 */
Gavin Shanf8f7d632012-09-07 22:44:22 +0000432int eeh_dev_check_failure(struct eeh_dev *edev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
434 int ret;
Gavin Shan1ad7a722014-05-05 09:29:03 +1000435 int active_flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 unsigned long flags;
Gavin Shanc6406d82015-03-17 16:15:08 +1100437 struct pci_dn *pdn;
Gavin Shanf8f7d632012-09-07 22:44:22 +0000438 struct pci_dev *dev;
Gavin Shan357b2f32014-06-11 18:26:44 +1000439 struct eeh_pe *pe, *parent_pe, *phb_pe;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600440 int rc = 0;
Gavin Shanc6406d82015-03-17 16:15:08 +1100441 const char *location = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Gavin Shane575f8d2012-02-29 15:47:45 +0000443 eeh_stats.total_mmio_ffs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Gavin Shan2ec5a0a2014-02-12 15:24:55 +0800445 if (!eeh_enabled())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return 0;
447
Gavin Shanf8f7d632012-09-07 22:44:22 +0000448 if (!edev) {
Gavin Shane575f8d2012-02-29 15:47:45 +0000449 eeh_stats.no_dn++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return 0;
Linas Vepstas177bc932005-11-03 18:48:52 -0600451 }
Gavin Shanf8f7d632012-09-07 22:44:22 +0000452 dev = eeh_dev_to_pci_dev(edev);
Wei Yang2a582222014-09-17 10:48:26 +0800453 pe = eeh_dev_to_pe(edev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 /* Access to IO BARs might get this far and still not want checking. */
Gavin Shan66523d92012-09-07 22:44:13 +0000456 if (!pe) {
Gavin Shane575f8d2012-02-29 15:47:45 +0000457 eeh_stats.ignored_check++;
Gavin Shanc6406d82015-03-17 16:15:08 +1100458 pr_debug("EEH: Ignored check for %s\n",
459 eeh_pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return 0;
461 }
462
Gavin Shan66523d92012-09-07 22:44:13 +0000463 if (!pe->addr && !pe->config_addr) {
Gavin Shane575f8d2012-02-29 15:47:45 +0000464 eeh_stats.no_cfg_addr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return 0;
466 }
467
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800468 /*
469 * On PowerNV platform, we might already have fenced PHB
470 * there and we need take care of that firstly.
471 */
472 ret = eeh_phb_check_failure(pe);
473 if (ret > 0)
474 return ret;
475
Gavin Shan05ec4242014-06-10 11:41:55 +1000476 /*
477 * If the PE isn't owned by us, we shouldn't check the
478 * state. Instead, let the owner handle it if the PE has
479 * been frozen.
480 */
481 if (eeh_pe_passed(pe))
482 return 0;
483
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600484 /* If we already have a pending isolation event for this
485 * slot, we know it's bad already, we don't need to check.
486 * Do this checking under a lock; as multiple PCI devices
487 * in one slot might report errors simultaneously, and we
488 * only want one error recovery routine running.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 */
Gavin Shan49075812013-06-20 13:21:03 +0800490 eeh_serialize_lock(&flags);
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600491 rc = 1;
Gavin Shan66523d92012-09-07 22:44:13 +0000492 if (pe->state & EEH_PE_ISOLATED) {
493 pe->check_count++;
494 if (pe->check_count % EEH_MAX_FAILS == 0) {
Gavin Shanc6406d82015-03-17 16:15:08 +1100495 pdn = eeh_dev_to_pdn(edev);
496 if (pdn->node)
497 location = of_get_property(pdn->node, "ibm,loc-code", NULL);
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000498 printk(KERN_ERR "EEH: %d reads ignored for recovering device at "
Mike Masonf36c5222008-07-22 02:40:17 +1000499 "location=%s driver=%s pci addr=%s\n",
Gavin Shanc6406d82015-03-17 16:15:08 +1100500 pe->check_count,
501 location ? location : "unknown",
Thadeu Lima de Souza Cascardo778a7852012-01-11 09:09:58 +0000502 eeh_driver_name(dev), eeh_pci_name(dev));
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000503 printk(KERN_ERR "EEH: Might be infinite loop in %s driver\n",
Thadeu Lima de Souza Cascardo778a7852012-01-11 09:09:58 +0000504 eeh_driver_name(dev));
Linas Vepstas5c1344e2005-11-03 18:49:31 -0600505 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600507 goto dn_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509
510 /*
511 * Now test for an EEH failure. This is VERY expensive.
512 * Note that the eeh_config_addr may be a parent device
513 * in the case of a device behind a bridge, or it may be
514 * function zero of a multi-function device.
515 * In any case they must share a common PHB.
516 */
Gavin Shan66523d92012-09-07 22:44:13 +0000517 ret = eeh_ops->get_state(pe, NULL);
Linas Vepstas76e6faf2005-11-03 18:49:15 -0600518
Linas Vepstas39d16e22007-03-19 14:51:00 -0500519 /* Note that config-io to empty slots may fail;
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000520 * they are empty when they don't have children.
Gavin Shaneb594a42012-02-27 20:03:57 +0000521 * We will punt with the following conditions: Failure to get
522 * PE's state, EEH not support and Permanently unavailable
523 * state, PE is in good state.
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000524 */
Gavin Shaneb594a42012-02-27 20:03:57 +0000525 if ((ret < 0) ||
526 (ret == EEH_STATE_NOT_SUPPORT) ||
Gavin Shan1ad7a722014-05-05 09:29:03 +1000527 ((ret & active_flags) == active_flags)) {
Gavin Shane575f8d2012-02-29 15:47:45 +0000528 eeh_stats.false_positives++;
Gavin Shan66523d92012-09-07 22:44:13 +0000529 pe->false_positives++;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600530 rc = 0;
531 goto dn_unlock;
Linas Vepstas76e6faf2005-11-03 18:49:15 -0600532 }
533
Gavin Shan1ad7a722014-05-05 09:29:03 +1000534 /*
535 * It should be corner case that the parent PE has been
536 * put into frozen state as well. We should take care
537 * that at first.
538 */
539 parent_pe = pe->parent;
540 while (parent_pe) {
541 /* Hit the ceiling ? */
542 if (parent_pe->type & EEH_PE_PHB)
543 break;
544
545 /* Frozen parent PE ? */
546 ret = eeh_ops->get_state(parent_pe, NULL);
547 if (ret > 0 &&
548 (ret & active_flags) != active_flags)
549 pe = parent_pe;
550
551 /* Next parent level */
552 parent_pe = parent_pe->parent;
553 }
554
Gavin Shane575f8d2012-02-29 15:47:45 +0000555 eeh_stats.slot_resets++;
Gavin Shana84f2732013-06-20 13:20:51 +0800556
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600557 /* Avoid repeated reports of this failure, including problems
558 * with other functions on this device, and functions under
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000559 * bridges.
560 */
Gavin Shan66523d92012-09-07 22:44:13 +0000561 eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
Gavin Shan49075812013-06-20 13:21:03 +0800562 eeh_serialize_unlock(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /* Most EEH events are due to device driver bugs. Having
565 * a stack trace will help the device-driver authors figure
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000566 * out what happened. So print that out.
567 */
Gavin Shan357b2f32014-06-11 18:26:44 +1000568 phb_pe = eeh_phb_pe_get(pe->phb);
569 pr_err("EEH: Frozen PHB#%x-PE#%x detected\n",
570 pe->phb->global_number, pe->addr);
571 pr_err("EEH: PE location: %s, PHB location: %s\n",
572 eeh_pe_loc_get(pe), eeh_pe_loc_get(phb_pe));
Gavin Shan56ca4fd2013-06-27 13:46:46 +0800573 dump_stack();
574
Gavin Shan5293bf92013-09-06 09:00:05 +0800575 eeh_send_failure_event(pe);
576
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600577 return 1;
578
579dn_unlock:
Gavin Shan49075812013-06-20 13:21:03 +0800580 eeh_serialize_unlock(flags);
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600581 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
Gavin Shanf8f7d632012-09-07 22:44:22 +0000584EXPORT_SYMBOL_GPL(eeh_dev_check_failure);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000587 * eeh_check_failure - Check if all 1's data is due to EEH slot freeze
Gavin Shan3e938052014-09-30 12:38:50 +1000588 * @token: I/O address
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 *
Gavin Shan3e938052014-09-30 12:38:50 +1000590 * Check for an EEH failure at the given I/O address. Call this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 * routine if the result of a read was all 0xff's and you want to
Gavin Shan3e938052014-09-30 12:38:50 +1000592 * find out if this is due to an EEH slot freeze event. This routine
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 * will query firmware for the EEH status.
594 *
595 * Note this routine is safe to call in an interrupt context.
596 */
Gavin Shan3e938052014-09-30 12:38:50 +1000597int eeh_check_failure(const volatile void __iomem *token)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
599 unsigned long addr;
Gavin Shanf8f7d632012-09-07 22:44:22 +0000600 struct eeh_dev *edev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 /* Finding the phys addr + pci device; this is pretty quick. */
603 addr = eeh_token_to_phys((unsigned long __force) token);
Gavin Shan3ab96a02012-09-07 22:44:23 +0000604 edev = eeh_addr_cache_get_dev(addr);
Gavin Shanf8f7d632012-09-07 22:44:22 +0000605 if (!edev) {
Gavin Shane575f8d2012-02-29 15:47:45 +0000606 eeh_stats.no_device++;
Gavin Shan3e938052014-09-30 12:38:50 +1000607 return 0;
Linas Vepstas177bc932005-11-03 18:48:52 -0600608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Gavin Shan3e938052014-09-30 12:38:50 +1000610 return eeh_dev_check_failure(edev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612EXPORT_SYMBOL(eeh_check_failure);
613
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600614
Linas Vepstascb5b56242006-09-15 18:56:35 -0500615/**
Gavin Shancce4b2d2012-02-27 20:03:52 +0000616 * eeh_pci_enable - Enable MMIO or DMA transfers for this slot
Gavin Shanff477962012-09-07 22:44:16 +0000617 * @pe: EEH PE
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000618 *
619 * This routine should be called to reenable frozen MMIO or DMA
620 * so that it would work correctly again. It's useful while doing
621 * recovery or log collection on the indicated device.
Linas Vepstas47b5c832006-09-15 18:57:42 -0500622 */
Gavin Shanff477962012-09-07 22:44:16 +0000623int eeh_pci_enable(struct eeh_pe *pe, int function)
Linas Vepstas47b5c832006-09-15 18:57:42 -0500624{
Gavin Shan4d4f5772014-09-30 12:39:00 +1000625 int active_flag, rc;
Gavin Shan78954702014-04-24 18:00:14 +1000626
627 /*
628 * pHyp doesn't allow to enable IO or DMA on unfrozen PE.
629 * Also, it's pointless to enable them on unfrozen PE. So
Gavin Shan4d4f5772014-09-30 12:39:00 +1000630 * we have to check before enabling IO or DMA.
Gavin Shan78954702014-04-24 18:00:14 +1000631 */
Gavin Shan4d4f5772014-09-30 12:39:00 +1000632 switch (function) {
633 case EEH_OPT_THAW_MMIO:
Gavin Shan872ee2d2015-10-08 14:58:55 +1100634 active_flag = EEH_STATE_MMIO_ACTIVE | EEH_STATE_MMIO_ENABLED;
Gavin Shan4d4f5772014-09-30 12:39:00 +1000635 break;
636 case EEH_OPT_THAW_DMA:
637 active_flag = EEH_STATE_DMA_ACTIVE;
638 break;
639 case EEH_OPT_DISABLE:
640 case EEH_OPT_ENABLE:
641 case EEH_OPT_FREEZE_PE:
642 active_flag = 0;
643 break;
644 default:
645 pr_warn("%s: Invalid function %d\n",
646 __func__, function);
647 return -EINVAL;
648 }
649
650 /*
651 * Check if IO or DMA has been enabled before
652 * enabling them.
653 */
654 if (active_flag) {
Gavin Shan78954702014-04-24 18:00:14 +1000655 rc = eeh_ops->get_state(pe, NULL);
656 if (rc < 0)
657 return rc;
658
Gavin Shan4d4f5772014-09-30 12:39:00 +1000659 /* Needn't enable it at all */
660 if (rc == EEH_STATE_NOT_SUPPORT)
661 return 0;
662
663 /* It's already enabled */
664 if (rc & active_flag)
Gavin Shan78954702014-04-24 18:00:14 +1000665 return 0;
666 }
Linas Vepstas47b5c832006-09-15 18:57:42 -0500667
Gavin Shan4d4f5772014-09-30 12:39:00 +1000668
669 /* Issue the request */
Gavin Shanff477962012-09-07 22:44:16 +0000670 rc = eeh_ops->set_option(pe, function);
Linas Vepstas47b5c832006-09-15 18:57:42 -0500671 if (rc)
Gavin Shan78954702014-04-24 18:00:14 +1000672 pr_warn("%s: Unexpected state change %d on "
673 "PHB#%d-PE#%x, err=%d\n",
674 __func__, function, pe->phb->global_number,
675 pe->addr, rc);
Linas Vepstas47b5c832006-09-15 18:57:42 -0500676
Gavin Shan4d4f5772014-09-30 12:39:00 +1000677 /* Check if the request is finished successfully */
678 if (active_flag) {
679 rc = eeh_ops->wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
680 if (rc <= 0)
681 return rc;
Gavin Shan78954702014-04-24 18:00:14 +1000682
Gavin Shan4d4f5772014-09-30 12:39:00 +1000683 if (rc & active_flag)
684 return 0;
Gavin Shan78954702014-04-24 18:00:14 +1000685
Gavin Shan4d4f5772014-09-30 12:39:00 +1000686 return -EIO;
687 }
Linas Vepstasfa1be472007-03-19 14:59:59 -0500688
Linas Vepstas47b5c832006-09-15 18:57:42 -0500689 return rc;
690}
691
Gavin Shan28158cd2015-02-11 10:20:49 +1100692static void *eeh_disable_and_save_dev_state(void *data, void *userdata)
693{
694 struct eeh_dev *edev = data;
695 struct pci_dev *pdev = eeh_dev_to_pci_dev(edev);
696 struct pci_dev *dev = userdata;
697
698 /*
699 * The caller should have disabled and saved the
700 * state for the specified device
701 */
702 if (!pdev || pdev == dev)
703 return NULL;
704
705 /* Ensure we have D0 power state */
706 pci_set_power_state(pdev, PCI_D0);
707
708 /* Save device state */
709 pci_save_state(pdev);
710
711 /*
712 * Disable device to avoid any DMA traffic and
713 * interrupt from the device
714 */
715 pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
716
717 return NULL;
718}
719
720static void *eeh_restore_dev_state(void *data, void *userdata)
721{
722 struct eeh_dev *edev = data;
Gavin Shan0bd78582015-03-17 16:15:07 +1100723 struct pci_dn *pdn = eeh_dev_to_pdn(edev);
Gavin Shan28158cd2015-02-11 10:20:49 +1100724 struct pci_dev *pdev = eeh_dev_to_pci_dev(edev);
725 struct pci_dev *dev = userdata;
726
727 if (!pdev)
728 return NULL;
729
730 /* Apply customization from firmware */
Gavin Shan0bd78582015-03-17 16:15:07 +1100731 if (pdn && eeh_ops->restore_config)
732 eeh_ops->restore_config(pdn);
Gavin Shan28158cd2015-02-11 10:20:49 +1100733
734 /* The caller should restore state for the specified device */
735 if (pdev != dev)
David Gibson502f1592015-06-03 14:52:59 +1000736 pci_restore_state(pdev);
Gavin Shan28158cd2015-02-11 10:20:49 +1100737
738 return NULL;
739}
740
Linas Vepstas47b5c832006-09-15 18:57:42 -0500741/**
Andrew Donnellan31f6a4a2016-02-08 14:39:19 +1100742 * pcibios_set_pcie_reset_state - Set PCI-E reset state
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000743 * @dev: pci device struct
744 * @state: reset state to enter
Brian King00c2ae32007-05-08 08:04:05 +1000745 *
746 * Return value:
747 * 0 if success
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000748 */
Brian King00c2ae32007-05-08 08:04:05 +1000749int pcibios_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
750{
Gavin Shanc270a242012-09-07 22:44:17 +0000751 struct eeh_dev *edev = pci_dev_to_eeh_dev(dev);
Wei Yang2a582222014-09-17 10:48:26 +0800752 struct eeh_pe *pe = eeh_dev_to_pe(edev);
Gavin Shanc270a242012-09-07 22:44:17 +0000753
754 if (!pe) {
755 pr_err("%s: No PE found on PCI device %s\n",
756 __func__, pci_name(dev));
757 return -EINVAL;
758 }
Brian King00c2ae32007-05-08 08:04:05 +1000759
760 switch (state) {
761 case pcie_deassert_reset:
Gavin Shanc270a242012-09-07 22:44:17 +0000762 eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);
Gavin Shan28158cd2015-02-11 10:20:49 +1100763 eeh_unfreeze_pe(pe, false);
Wei Yang9312bc52016-03-04 10:53:09 +1100764 if (!(pe->type & EEH_PE_VF))
765 eeh_pe_state_clear(pe, EEH_PE_CFG_BLOCKED);
Gavin Shan28158cd2015-02-11 10:20:49 +1100766 eeh_pe_dev_traverse(pe, eeh_restore_dev_state, dev);
Gavin Shan1ae79b72015-05-01 09:14:11 +1000767 eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
Brian King00c2ae32007-05-08 08:04:05 +1000768 break;
769 case pcie_hot_reset:
Gavin Shan39bfd712015-07-30 09:26:51 +1000770 eeh_pe_state_mark_with_cfg(pe, EEH_PE_ISOLATED);
Gavin Shan28158cd2015-02-11 10:20:49 +1100771 eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);
772 eeh_pe_dev_traverse(pe, eeh_disable_and_save_dev_state, dev);
Wei Yang9312bc52016-03-04 10:53:09 +1100773 if (!(pe->type & EEH_PE_VF))
774 eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
Gavin Shanc270a242012-09-07 22:44:17 +0000775 eeh_ops->reset(pe, EEH_RESET_HOT);
Brian King00c2ae32007-05-08 08:04:05 +1000776 break;
777 case pcie_warm_reset:
Gavin Shan39bfd712015-07-30 09:26:51 +1000778 eeh_pe_state_mark_with_cfg(pe, EEH_PE_ISOLATED);
Gavin Shan28158cd2015-02-11 10:20:49 +1100779 eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);
780 eeh_pe_dev_traverse(pe, eeh_disable_and_save_dev_state, dev);
Wei Yang9312bc52016-03-04 10:53:09 +1100781 if (!(pe->type & EEH_PE_VF))
782 eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
Gavin Shanc270a242012-09-07 22:44:17 +0000783 eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
Brian King00c2ae32007-05-08 08:04:05 +1000784 break;
785 default:
Gavin Shan1ae79b72015-05-01 09:14:11 +1000786 eeh_pe_state_clear(pe, EEH_PE_ISOLATED | EEH_PE_CFG_BLOCKED);
Brian King00c2ae32007-05-08 08:04:05 +1000787 return -EINVAL;
788 };
789
790 return 0;
791}
792
793/**
Gavin Shanc270a242012-09-07 22:44:17 +0000794 * eeh_set_pe_freset - Check the required reset for the indicated device
795 * @data: EEH device
796 * @flag: return value
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000797 *
798 * Each device might have its preferred reset type: fundamental or
799 * hot reset. The routine is used to collected the information for
800 * the indicated device and its children so that the bunch of the
801 * devices could be reset properly.
802 */
Gavin Shanc270a242012-09-07 22:44:17 +0000803static void *eeh_set_dev_freset(void *data, void *flag)
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000804{
805 struct pci_dev *dev;
Gavin Shanc270a242012-09-07 22:44:17 +0000806 unsigned int *freset = (unsigned int *)flag;
807 struct eeh_dev *edev = (struct eeh_dev *)data;
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000808
Gavin Shanc270a242012-09-07 22:44:17 +0000809 dev = eeh_dev_to_pci_dev(edev);
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000810 if (dev)
811 *freset |= dev->needs_freset;
812
Gavin Shanc270a242012-09-07 22:44:17 +0000813 return NULL;
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000814}
815
816/**
Gavin Shancce4b2d2012-02-27 20:03:52 +0000817 * eeh_reset_pe_once - Assert the pci #RST line for 1/4 second
Gavin Shanc270a242012-09-07 22:44:17 +0000818 * @pe: EEH PE
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000819 *
820 * Assert the PCI #RST line for 1/4 second.
821 */
Gavin Shanc270a242012-09-07 22:44:17 +0000822static void eeh_reset_pe_once(struct eeh_pe *pe)
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600823{
Richard A Lary308fc4f2011-04-22 09:59:47 +0000824 unsigned int freset = 0;
Mike Mason6e193142009-07-30 15:42:39 -0700825
Richard A Lary308fc4f2011-04-22 09:59:47 +0000826 /* Determine type of EEH reset required for
827 * Partitionable Endpoint, a hot-reset (1)
828 * or a fundamental reset (3).
829 * A fundamental reset required by any device under
830 * Partitionable Endpoint trumps hot-reset.
Gavin Shana84f2732013-06-20 13:20:51 +0800831 */
Gavin Shanc270a242012-09-07 22:44:17 +0000832 eeh_pe_dev_traverse(pe, eeh_set_dev_freset, &freset);
Richard A Lary308fc4f2011-04-22 09:59:47 +0000833
834 if (freset)
Gavin Shanc270a242012-09-07 22:44:17 +0000835 eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
Mike Mason6e193142009-07-30 15:42:39 -0700836 else
Gavin Shanc270a242012-09-07 22:44:17 +0000837 eeh_ops->reset(pe, EEH_RESET_HOT);
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600838
Gavin Shanc270a242012-09-07 22:44:17 +0000839 eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);
Linas Vepstase1029262006-09-21 18:25:56 -0500840}
841
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000842/**
Gavin Shancce4b2d2012-02-27 20:03:52 +0000843 * eeh_reset_pe - Reset the indicated PE
Gavin Shanc270a242012-09-07 22:44:17 +0000844 * @pe: EEH PE
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000845 *
846 * This routine should be called to reset indicated device, including
847 * PE. A PE might include multiple PCI devices and sometimes PCI bridges
848 * might be involved as well.
849 */
Gavin Shanc270a242012-09-07 22:44:17 +0000850int eeh_reset_pe(struct eeh_pe *pe)
Linas Vepstase1029262006-09-21 18:25:56 -0500851{
Gavin Shan326a98e2013-06-20 13:20:58 +0800852 int flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
Gavin Shanb85743e2014-11-14 10:47:28 +1100853 int i, state, ret;
Linas Vepstase1029262006-09-21 18:25:56 -0500854
Gavin Shan28bf36f2014-11-14 10:47:29 +1100855 /* Mark as reset and block config space */
856 eeh_pe_state_mark(pe, EEH_PE_RESET | EEH_PE_CFG_BLOCKED);
857
Linas Vepstas9c547762007-03-19 14:58:07 -0500858 /* Take three shots at resetting the bus */
Gavin Shanb85743e2014-11-14 10:47:28 +1100859 for (i = 0; i < 3; i++) {
Gavin Shanc270a242012-09-07 22:44:17 +0000860 eeh_reset_pe_once(pe);
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600861
Gavin Shan78954702014-04-24 18:00:14 +1000862 /*
863 * EEH_PE_ISOLATED is expected to be removed after
864 * BAR restore.
865 */
Gavin Shanb85743e2014-11-14 10:47:28 +1100866 state = eeh_ops->wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
867 if ((state & flags) == flags) {
868 ret = 0;
869 goto out;
Linas Vepstase1029262006-09-21 18:25:56 -0500870 }
Gavin Shanb85743e2014-11-14 10:47:28 +1100871
872 if (state < 0) {
873 pr_warn("%s: Unrecoverable slot failure on PHB#%d-PE#%x",
874 __func__, pe->phb->global_number, pe->addr);
875 ret = -ENOTRECOVERABLE;
876 goto out;
877 }
878
879 /* We might run out of credits */
880 ret = -EIO;
881 pr_warn("%s: Failure %d resetting PHB#%x-PE#%x\n (%d)\n",
882 __func__, state, pe->phb->global_number, pe->addr, (i + 1));
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600883 }
Linas Vepstasb6495c02005-11-03 18:54:54 -0600884
Gavin Shanb85743e2014-11-14 10:47:28 +1100885out:
Gavin Shan28bf36f2014-11-14 10:47:29 +1100886 eeh_pe_state_clear(pe, EEH_PE_RESET | EEH_PE_CFG_BLOCKED);
Gavin Shanb85743e2014-11-14 10:47:28 +1100887 return ret;
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600888}
889
Linas Vepstas8b553f32005-11-03 18:50:17 -0600890/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000891 * eeh_save_bars - Save device bars
Gavin Shanf631acd2012-02-27 20:04:07 +0000892 * @edev: PCI device associated EEH device
Linas Vepstas8b553f32005-11-03 18:50:17 -0600893 *
894 * Save the values of the device bars. Unlike the restore
895 * routine, this routine is *not* recursive. This is because
Justin Mattock31116f02011-02-24 20:10:18 +0000896 * PCI devices are added individually; but, for the restore,
Linas Vepstas8b553f32005-11-03 18:50:17 -0600897 * an entire slot is reset at a time.
898 */
Gavin Shand7bb8862012-09-07 22:44:21 +0000899void eeh_save_bars(struct eeh_dev *edev)
Linas Vepstas8b553f32005-11-03 18:50:17 -0600900{
Gavin Shan0bd78582015-03-17 16:15:07 +1100901 struct pci_dn *pdn;
Linas Vepstas8b553f32005-11-03 18:50:17 -0600902 int i;
903
Gavin Shan0bd78582015-03-17 16:15:07 +1100904 pdn = eeh_dev_to_pdn(edev);
905 if (!pdn)
Linas Vepstas8b553f32005-11-03 18:50:17 -0600906 return;
Gavin Shana84f2732013-06-20 13:20:51 +0800907
Linas Vepstas8b553f32005-11-03 18:50:17 -0600908 for (i = 0; i < 16; i++)
Gavin Shan0bd78582015-03-17 16:15:07 +1100909 eeh_ops->read_config(pdn, i * 4, 4, &edev->config_space[i]);
Gavin Shanbf898ec2013-11-12 14:49:21 +0800910
911 /*
912 * For PCI bridges including root port, we need enable bus
913 * master explicitly. Otherwise, it can't fetch IODA table
914 * entries correctly. So we cache the bit in advance so that
915 * we can restore it after reset, either PHB range or PE range.
916 */
917 if (edev->mode & EEH_DEV_BRIDGE)
918 edev->config_space[1] |= PCI_COMMAND_MASTER;
Linas Vepstas8b553f32005-11-03 18:50:17 -0600919}
920
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000921/**
Gavin Shanaa1e6372012-02-27 20:03:53 +0000922 * eeh_ops_register - Register platform dependent EEH operations
923 * @ops: platform dependent EEH operations
924 *
925 * Register the platform dependent EEH operation callback
926 * functions. The platform should call this function before
927 * any other EEH operations.
928 */
929int __init eeh_ops_register(struct eeh_ops *ops)
930{
931 if (!ops->name) {
Gavin Shan0dae2742014-07-17 14:41:41 +1000932 pr_warn("%s: Invalid EEH ops name for %p\n",
Gavin Shanaa1e6372012-02-27 20:03:53 +0000933 __func__, ops);
934 return -EINVAL;
935 }
936
937 if (eeh_ops && eeh_ops != ops) {
Gavin Shan0dae2742014-07-17 14:41:41 +1000938 pr_warn("%s: EEH ops of platform %s already existing (%s)\n",
Gavin Shanaa1e6372012-02-27 20:03:53 +0000939 __func__, eeh_ops->name, ops->name);
940 return -EEXIST;
941 }
942
943 eeh_ops = ops;
944
945 return 0;
946}
947
948/**
949 * eeh_ops_unregister - Unreigster platform dependent EEH operations
950 * @name: name of EEH platform operations
951 *
952 * Unregister the platform dependent EEH operation callback
953 * functions.
954 */
955int __exit eeh_ops_unregister(const char *name)
956{
957 if (!name || !strlen(name)) {
Gavin Shan0dae2742014-07-17 14:41:41 +1000958 pr_warn("%s: Invalid EEH ops name\n",
Gavin Shanaa1e6372012-02-27 20:03:53 +0000959 __func__);
960 return -EINVAL;
961 }
962
963 if (eeh_ops && !strcmp(eeh_ops->name, name)) {
964 eeh_ops = NULL;
965 return 0;
966 }
967
968 return -EEXIST;
969}
970
Gavin Shan66f9af832014-02-12 15:24:56 +0800971static int eeh_reboot_notifier(struct notifier_block *nb,
972 unsigned long action, void *unused)
973{
Gavin Shan05b17212014-07-17 14:41:38 +1000974 eeh_clear_flag(EEH_ENABLED);
Gavin Shan66f9af832014-02-12 15:24:56 +0800975 return NOTIFY_DONE;
976}
977
978static struct notifier_block eeh_reboot_nb = {
979 .notifier_call = eeh_reboot_notifier,
980};
981
Gavin Shanaa1e6372012-02-27 20:03:53 +0000982/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000983 * eeh_init - EEH initialization
984 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 * Initialize EEH by trying to enable it for all of the adapters in the system.
986 * As a side effect we can determine here if eeh is supported at all.
987 * Note that we leave EEH on so failed config cycles won't cause a machine
988 * check. If a user turns off EEH for a particular adapter they are really
989 * telling Linux to ignore errors. Some hardware (e.g. POWER5) won't
990 * grant access to a slot if EEH isn't enabled, and so we always enable
991 * EEH for all slots/all devices.
992 *
993 * The eeh-force-off option disables EEH checking globally, for all slots.
994 * Even if force-off is set, the EEH hardware is still enabled, so that
995 * newer systems can boot.
996 */
Gavin Shaneeb63612013-06-27 13:46:47 +0800997int eeh_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
Gavin Shan1a5c2e62012-03-20 21:30:29 +0000999 struct pci_controller *hose, *tmp;
Gavin Shanff57b452015-03-17 16:15:06 +11001000 struct pci_dn *pdn;
Gavin Shan51fb5f52013-06-20 13:20:56 +08001001 static int cnt = 0;
1002 int ret = 0;
1003
1004 /*
1005 * We have to delay the initialization on PowerNV after
1006 * the PCI hierarchy tree has been built because the PEs
1007 * are figured out based on PCI devices instead of device
1008 * tree nodes
1009 */
1010 if (machine_is(powernv) && cnt++ <= 0)
1011 return ret;
Gavin Shane2af1552012-02-27 20:03:54 +00001012
Gavin Shan66f9af832014-02-12 15:24:56 +08001013 /* Register reboot notifier */
1014 ret = register_reboot_notifier(&eeh_reboot_nb);
1015 if (ret) {
1016 pr_warn("%s: Failed to register notifier (%d)\n",
1017 __func__, ret);
1018 return ret;
1019 }
1020
Gavin Shane2af1552012-02-27 20:03:54 +00001021 /* call platform initialization function */
1022 if (!eeh_ops) {
Gavin Shan0dae2742014-07-17 14:41:41 +10001023 pr_warn("%s: Platform EEH operation not found\n",
Gavin Shane2af1552012-02-27 20:03:54 +00001024 __func__);
Gavin Shan35e5cfe2012-09-07 22:44:02 +00001025 return -EEXIST;
Greg Kurz221195f2014-11-25 17:10:06 +01001026 } else if ((ret = eeh_ops->init()))
Gavin Shan35e5cfe2012-09-07 22:44:02 +00001027 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Gavin Shanc8608552013-06-20 13:21:00 +08001029 /* Initialize EEH event */
1030 ret = eeh_event_init();
1031 if (ret)
1032 return ret;
1033
Gavin Shan1a5c2e62012-03-20 21:30:29 +00001034 /* Enable EEH for all adapters */
Gavin Shanff57b452015-03-17 16:15:06 +11001035 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
1036 pdn = hose->pci_data;
1037 traverse_pci_dn(pdn, eeh_ops->probe, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 }
1039
Gavin Shan21fd21f2013-06-20 13:20:57 +08001040 /*
1041 * Call platform post-initialization. Actually, It's good chance
1042 * to inform platform that EEH is ready to supply service if the
1043 * I/O cache stuff has been built up.
1044 */
1045 if (eeh_ops->post_init) {
1046 ret = eeh_ops->post_init();
1047 if (ret)
1048 return ret;
1049 }
1050
Gavin Shan2ec5a0a2014-02-12 15:24:55 +08001051 if (eeh_enabled())
Gavin Shand7bb8862012-09-07 22:44:21 +00001052 pr_info("EEH: PCI Enhanced I/O Error Handling Enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 else
Gavin Shan0dae2742014-07-17 14:41:41 +10001054 pr_warn("EEH: No capable adapters found\n");
Gavin Shan35e5cfe2012-09-07 22:44:02 +00001055
1056 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057}
1058
Gavin Shan35e5cfe2012-09-07 22:44:02 +00001059core_initcall_sync(eeh_init);
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061/**
Gavin Shanc6406d82015-03-17 16:15:08 +11001062 * eeh_add_device_early - Enable EEH for the indicated device node
Gavin Shanff57b452015-03-17 16:15:06 +11001063 * @pdn: PCI device node for which to set up EEH
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 *
1065 * This routine must be used to perform EEH initialization for PCI
1066 * devices that were added after system boot (e.g. hotplug, dlpar).
1067 * This routine must be called before any i/o is performed to the
1068 * adapter (inluding any config-space i/o).
1069 * Whether this actually enables EEH or not for this device depends
1070 * on the CEC architecture, type of the device, on earlier boot
1071 * command-line arguments & etc.
1072 */
Gavin Shanff57b452015-03-17 16:15:06 +11001073void eeh_add_device_early(struct pci_dn *pdn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
1075 struct pci_controller *phb;
Gavin Shanff57b452015-03-17 16:15:06 +11001076 struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Michael Ellerman89a51df2015-04-14 16:49:05 +10001078 if (!edev || !eeh_enabled())
Gavin Shan26a74852013-06-20 13:20:59 +08001079 return;
1080
Gavin Shand91dafc2015-05-01 09:22:15 +10001081 if (!eeh_has_flag(EEH_PROBE_MODE_DEVTREE))
1082 return;
1083
Linas Vepstasf751f842005-11-03 18:54:23 -06001084 /* USB Bus children of PCI devices will not have BUID's */
Gavin Shanff57b452015-03-17 16:15:06 +11001085 phb = edev->phb;
1086 if (NULL == phb ||
1087 (eeh_has_flag(EEH_PROBE_MODE_DEVTREE) && 0 == phb->buid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Gavin Shanff57b452015-03-17 16:15:06 +11001090 eeh_ops->probe(pdn, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001093/**
1094 * eeh_add_device_tree_early - Enable EEH for the indicated device
Gavin Shanff57b452015-03-17 16:15:06 +11001095 * @pdn: PCI device node
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001096 *
1097 * This routine must be used to perform EEH initialization for the
1098 * indicated PCI device that was added after system boot (e.g.
1099 * hotplug, dlpar).
1100 */
Gavin Shanff57b452015-03-17 16:15:06 +11001101void eeh_add_device_tree_early(struct pci_dn *pdn)
Linas Vepstase2a296e2005-11-03 18:51:31 -06001102{
Gavin Shanff57b452015-03-17 16:15:06 +11001103 struct pci_dn *n;
Stephen Rothwellacaa6172007-12-21 15:52:07 +11001104
Gavin Shanff57b452015-03-17 16:15:06 +11001105 if (!pdn)
1106 return;
1107
1108 list_for_each_entry(n, &pdn->child_list, list)
1109 eeh_add_device_tree_early(n);
1110 eeh_add_device_early(pdn);
Linas Vepstase2a296e2005-11-03 18:51:31 -06001111}
1112EXPORT_SYMBOL_GPL(eeh_add_device_tree_early);
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001115 * eeh_add_device_late - Perform EEH initialization for the indicated pci device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 * @dev: pci device for which to set up EEH
1117 *
1118 * This routine must be used to complete EEH initialization for PCI
1119 * devices that were added after system boot (e.g. hotplug, dlpar).
1120 */
Gavin Shanf2856492013-07-24 10:24:52 +08001121void eeh_add_device_late(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
Gavin Shanc6406d82015-03-17 16:15:08 +11001123 struct pci_dn *pdn;
Gavin Shanf631acd2012-02-27 20:04:07 +00001124 struct eeh_dev *edev;
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001125
Gavin Shan2ec5a0a2014-02-12 15:24:55 +08001126 if (!dev || !eeh_enabled())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return;
1128
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001129 pr_debug("EEH: Adding device %s\n", pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Gavin Shanc6406d82015-03-17 16:15:08 +11001131 pdn = pci_get_pdn_by_devfn(dev->bus, dev->devfn);
1132 edev = pdn_to_eeh_dev(pdn);
Gavin Shanf631acd2012-02-27 20:04:07 +00001133 if (edev->pdev == dev) {
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001134 pr_debug("EEH: Already referenced !\n");
1135 return;
1136 }
Gavin Shanf5c57712013-07-24 10:24:58 +08001137
1138 /*
1139 * The EEH cache might not be removed correctly because of
1140 * unbalanced kref to the device during unplug time, which
1141 * relies on pcibios_release_device(). So we have to remove
1142 * that here explicitly.
1143 */
1144 if (edev->pdev) {
1145 eeh_rmv_from_parent_pe(edev);
1146 eeh_addr_cache_rmv_dev(edev->pdev);
1147 eeh_sysfs_remove_device(edev->pdev);
Gavin Shanab55d212013-07-24 10:25:01 +08001148 edev->mode &= ~EEH_DEV_SYSFS;
Gavin Shanf5c57712013-07-24 10:24:58 +08001149
Gavin Shanf26c7a02014-01-12 14:13:45 +08001150 /*
1151 * We definitely should have the PCI device removed
1152 * though it wasn't correctly. So we needn't call
1153 * into error handler afterwards.
1154 */
1155 edev->mode |= EEH_DEV_NO_HANDLER;
1156
Gavin Shanf5c57712013-07-24 10:24:58 +08001157 edev->pdev = NULL;
1158 dev->dev.archdata.edev = NULL;
1159 }
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001160
Daniel Axtense642d112015-08-14 16:03:19 +10001161 if (eeh_has_flag(EEH_PROBE_MODE_DEV))
1162 eeh_ops->probe(pdn, NULL);
1163
Gavin Shanf631acd2012-02-27 20:04:07 +00001164 edev->pdev = dev;
1165 dev->dev.archdata.edev = edev;
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001166
Gavin Shan3ab96a02012-09-07 22:44:23 +00001167 eeh_addr_cache_insert_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
Nathan Fontenot794e0852006-03-31 12:04:52 -06001169
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001170/**
1171 * eeh_add_device_tree_late - Perform EEH initialization for the indicated PCI bus
1172 * @bus: PCI bus
1173 *
1174 * This routine must be used to perform EEH initialization for PCI
1175 * devices which are attached to the indicated PCI bus. The PCI bus
1176 * is added after system boot through hotplug or dlpar.
1177 */
Nathan Fontenot794e0852006-03-31 12:04:52 -06001178void eeh_add_device_tree_late(struct pci_bus *bus)
1179{
1180 struct pci_dev *dev;
1181
1182 list_for_each_entry(dev, &bus->devices, bus_list) {
Gavin Shana84f2732013-06-20 13:20:51 +08001183 eeh_add_device_late(dev);
1184 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
1185 struct pci_bus *subbus = dev->subordinate;
1186 if (subbus)
1187 eeh_add_device_tree_late(subbus);
1188 }
Nathan Fontenot794e0852006-03-31 12:04:52 -06001189 }
1190}
1191EXPORT_SYMBOL_GPL(eeh_add_device_tree_late);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193/**
Thadeu Lima de Souza Cascardo6a040ce2012-12-28 09:13:19 +00001194 * eeh_add_sysfs_files - Add EEH sysfs files for the indicated PCI bus
1195 * @bus: PCI bus
1196 *
1197 * This routine must be used to add EEH sysfs files for PCI
1198 * devices which are attached to the indicated PCI bus. The PCI bus
1199 * is added after system boot through hotplug or dlpar.
1200 */
1201void eeh_add_sysfs_files(struct pci_bus *bus)
1202{
1203 struct pci_dev *dev;
1204
1205 list_for_each_entry(dev, &bus->devices, bus_list) {
1206 eeh_sysfs_add_device(dev);
1207 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
1208 struct pci_bus *subbus = dev->subordinate;
1209 if (subbus)
1210 eeh_add_sysfs_files(subbus);
1211 }
1212 }
1213}
1214EXPORT_SYMBOL_GPL(eeh_add_sysfs_files);
1215
1216/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001217 * eeh_remove_device - Undo EEH setup for the indicated pci device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 * @dev: pci device to be removed
1219 *
Nathan Fontenot794e0852006-03-31 12:04:52 -06001220 * This routine should be called when a device is removed from
1221 * a running system (e.g. by hotplug or dlpar). It unregisters
1222 * the PCI device from the EEH subsystem. I/O errors affecting
1223 * this device will no longer be detected after this call; thus,
1224 * i/o errors affecting this slot may leave this device unusable.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 */
Gavin Shan807a8272013-07-24 10:24:55 +08001226void eeh_remove_device(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
Gavin Shanf631acd2012-02-27 20:04:07 +00001228 struct eeh_dev *edev;
1229
Gavin Shan2ec5a0a2014-02-12 15:24:55 +08001230 if (!dev || !eeh_enabled())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return;
Gavin Shanf631acd2012-02-27 20:04:07 +00001232 edev = pci_dev_to_eeh_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 /* Unregister the device with the EEH/PCI address search system */
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001235 pr_debug("EEH: Removing device %s\n", pci_name(dev));
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001236
Gavin Shanf5c57712013-07-24 10:24:58 +08001237 if (!edev || !edev->pdev || !edev->pe) {
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001238 pr_debug("EEH: Not referenced !\n");
1239 return;
Linas Vepstasb055a9e2006-04-06 15:41:41 -05001240 }
Gavin Shanf5c57712013-07-24 10:24:58 +08001241
1242 /*
1243 * During the hotplug for EEH error recovery, we need the EEH
1244 * device attached to the parent PE in order for BAR restore
1245 * a bit later. So we keep it for BAR restore and remove it
1246 * from the parent PE during the BAR resotre.
1247 */
Gavin Shanf631acd2012-02-27 20:04:07 +00001248 edev->pdev = NULL;
Wei Yang67086e32016-03-04 10:53:11 +11001249
1250 /*
1251 * The flag "in_error" is used to trace EEH devices for VFs
1252 * in error state or not. It's set in eeh_report_error(). If
1253 * it's not set, eeh_report_{reset,resume}() won't be called
1254 * for the VF EEH device.
1255 */
1256 edev->in_error = false;
Gavin Shanf631acd2012-02-27 20:04:07 +00001257 dev->dev.archdata.edev = NULL;
Gavin Shanf5c57712013-07-24 10:24:58 +08001258 if (!(edev->pe->state & EEH_PE_KEEP))
1259 eeh_rmv_from_parent_pe(edev);
1260 else
1261 edev->mode |= EEH_DEV_DISCONNECTED;
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001262
Gavin Shanf26c7a02014-01-12 14:13:45 +08001263 /*
1264 * We're removing from the PCI subsystem, that means
1265 * the PCI device driver can't support EEH or not
1266 * well. So we rely on hotplug completely to do recovery
1267 * for the specific PCI device.
1268 */
1269 edev->mode |= EEH_DEV_NO_HANDLER;
1270
Gavin Shan3ab96a02012-09-07 22:44:23 +00001271 eeh_addr_cache_rmv_dev(dev);
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001272 eeh_sysfs_remove_device(dev);
Gavin Shanab55d212013-07-24 10:25:01 +08001273 edev->mode &= ~EEH_DEV_SYSFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Gavin Shan4eeeff02014-09-30 12:39:01 +10001276int eeh_unfreeze_pe(struct eeh_pe *pe, bool sw_state)
1277{
1278 int ret;
1279
1280 ret = eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
1281 if (ret) {
1282 pr_warn("%s: Failure %d enabling IO on PHB#%x-PE#%x\n",
1283 __func__, ret, pe->phb->global_number, pe->addr);
1284 return ret;
1285 }
1286
1287 ret = eeh_pci_enable(pe, EEH_OPT_THAW_DMA);
1288 if (ret) {
1289 pr_warn("%s: Failure %d enabling DMA on PHB#%x-PE#%x\n",
1290 __func__, ret, pe->phb->global_number, pe->addr);
1291 return ret;
1292 }
1293
1294 /* Clear software isolated state */
1295 if (sw_state && (pe->state & EEH_PE_ISOLATED))
1296 eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
1297
1298 return ret;
1299}
1300
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001301
1302static struct pci_device_id eeh_reset_ids[] = {
1303 { PCI_DEVICE(0x19a2, 0x0710) }, /* Emulex, BE */
1304 { PCI_DEVICE(0x10df, 0xe220) }, /* Emulex, Lancer */
Gavin Shanb1d76a72014-11-14 10:47:30 +11001305 { PCI_DEVICE(0x14e4, 0x1657) }, /* Broadcom BCM5719 */
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001306 { 0 }
1307};
1308
1309static int eeh_pe_change_owner(struct eeh_pe *pe)
1310{
1311 struct eeh_dev *edev, *tmp;
1312 struct pci_dev *pdev;
1313 struct pci_device_id *id;
1314 int flags, ret;
1315
1316 /* Check PE state */
1317 flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
1318 ret = eeh_ops->get_state(pe, NULL);
1319 if (ret < 0 || ret == EEH_STATE_NOT_SUPPORT)
1320 return 0;
1321
1322 /* Unfrozen PE, nothing to do */
1323 if ((ret & flags) == flags)
1324 return 0;
1325
1326 /* Frozen PE, check if it needs PE level reset */
1327 eeh_pe_for_each_dev(pe, edev, tmp) {
1328 pdev = eeh_dev_to_pci_dev(edev);
1329 if (!pdev)
1330 continue;
1331
1332 for (id = &eeh_reset_ids[0]; id->vendor != 0; id++) {
1333 if (id->vendor != PCI_ANY_ID &&
1334 id->vendor != pdev->vendor)
1335 continue;
1336 if (id->device != PCI_ANY_ID &&
1337 id->device != pdev->device)
1338 continue;
1339 if (id->subvendor != PCI_ANY_ID &&
1340 id->subvendor != pdev->subsystem_vendor)
1341 continue;
1342 if (id->subdevice != PCI_ANY_ID &&
1343 id->subdevice != pdev->subsystem_device)
1344 continue;
1345
1346 goto reset;
1347 }
1348 }
1349
1350 return eeh_unfreeze_pe(pe, true);
1351
1352reset:
1353 return eeh_pe_reset_and_recover(pe);
1354}
1355
Gavin Shan212d16c2014-06-10 11:41:56 +10001356/**
1357 * eeh_dev_open - Increase count of pass through devices for PE
1358 * @pdev: PCI device
1359 *
1360 * Increase count of passed through devices for the indicated
1361 * PE. In the result, the EEH errors detected on the PE won't be
1362 * reported. The PE owner will be responsible for detection
1363 * and recovery.
1364 */
1365int eeh_dev_open(struct pci_dev *pdev)
1366{
1367 struct eeh_dev *edev;
Gavin Shan404079c2014-09-30 12:38:54 +10001368 int ret = -ENODEV;
Gavin Shan212d16c2014-06-10 11:41:56 +10001369
1370 mutex_lock(&eeh_dev_mutex);
1371
1372 /* No PCI device ? */
1373 if (!pdev)
1374 goto out;
1375
1376 /* No EEH device or PE ? */
1377 edev = pci_dev_to_eeh_dev(pdev);
1378 if (!edev || !edev->pe)
1379 goto out;
1380
Gavin Shan404079c2014-09-30 12:38:54 +10001381 /*
1382 * The PE might have been put into frozen state, but we
1383 * didn't detect that yet. The passed through PCI devices
1384 * in frozen PE won't work properly. Clear the frozen state
1385 * in advance.
1386 */
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001387 ret = eeh_pe_change_owner(edev->pe);
Gavin Shan4eeeff02014-09-30 12:39:01 +10001388 if (ret)
1389 goto out;
Gavin Shan404079c2014-09-30 12:38:54 +10001390
Gavin Shan212d16c2014-06-10 11:41:56 +10001391 /* Increase PE's pass through count */
1392 atomic_inc(&edev->pe->pass_dev_cnt);
1393 mutex_unlock(&eeh_dev_mutex);
1394
1395 return 0;
1396out:
1397 mutex_unlock(&eeh_dev_mutex);
Gavin Shan404079c2014-09-30 12:38:54 +10001398 return ret;
Gavin Shan212d16c2014-06-10 11:41:56 +10001399}
1400EXPORT_SYMBOL_GPL(eeh_dev_open);
1401
1402/**
1403 * eeh_dev_release - Decrease count of pass through devices for PE
1404 * @pdev: PCI device
1405 *
1406 * Decrease count of pass through devices for the indicated PE. If
1407 * there is no passed through device in PE, the EEH errors detected
1408 * on the PE will be reported and handled as usual.
1409 */
1410void eeh_dev_release(struct pci_dev *pdev)
1411{
1412 struct eeh_dev *edev;
1413
1414 mutex_lock(&eeh_dev_mutex);
1415
1416 /* No PCI device ? */
1417 if (!pdev)
1418 goto out;
1419
1420 /* No EEH device ? */
1421 edev = pci_dev_to_eeh_dev(pdev);
1422 if (!edev || !edev->pe || !eeh_pe_passed(edev->pe))
1423 goto out;
1424
1425 /* Decrease PE's pass through count */
Gavin Shan54f9a642015-08-27 15:58:27 +10001426 WARN_ON(atomic_dec_if_positive(&edev->pe->pass_dev_cnt) < 0);
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001427 eeh_pe_change_owner(edev->pe);
Gavin Shan212d16c2014-06-10 11:41:56 +10001428out:
1429 mutex_unlock(&eeh_dev_mutex);
1430}
1431EXPORT_SYMBOL(eeh_dev_release);
1432
Benjamin Herrenschmidt2194dc22014-08-05 18:52:59 +10001433#ifdef CONFIG_IOMMU_API
1434
Gavin Shana3032ca2014-07-15 17:00:56 +10001435static int dev_has_iommu_table(struct device *dev, void *data)
1436{
1437 struct pci_dev *pdev = to_pci_dev(dev);
1438 struct pci_dev **ppdev = data;
Gavin Shana3032ca2014-07-15 17:00:56 +10001439
1440 if (!dev)
1441 return 0;
1442
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10001443 if (dev->iommu_group) {
Gavin Shana3032ca2014-07-15 17:00:56 +10001444 *ppdev = pdev;
1445 return 1;
1446 }
1447
1448 return 0;
1449}
1450
Gavin Shan212d16c2014-06-10 11:41:56 +10001451/**
1452 * eeh_iommu_group_to_pe - Convert IOMMU group to EEH PE
1453 * @group: IOMMU group
1454 *
1455 * The routine is called to convert IOMMU group to EEH PE.
1456 */
1457struct eeh_pe *eeh_iommu_group_to_pe(struct iommu_group *group)
1458{
Gavin Shan212d16c2014-06-10 11:41:56 +10001459 struct pci_dev *pdev = NULL;
1460 struct eeh_dev *edev;
Gavin Shana3032ca2014-07-15 17:00:56 +10001461 int ret;
Gavin Shan212d16c2014-06-10 11:41:56 +10001462
1463 /* No IOMMU group ? */
1464 if (!group)
1465 return NULL;
1466
Gavin Shana3032ca2014-07-15 17:00:56 +10001467 ret = iommu_group_for_each_dev(group, &pdev, dev_has_iommu_table);
1468 if (!ret || !pdev)
Gavin Shan212d16c2014-06-10 11:41:56 +10001469 return NULL;
1470
1471 /* No EEH device or PE ? */
1472 edev = pci_dev_to_eeh_dev(pdev);
1473 if (!edev || !edev->pe)
1474 return NULL;
1475
1476 return edev->pe;
1477}
Gavin Shan537e5402014-08-07 12:47:16 +10001478EXPORT_SYMBOL_GPL(eeh_iommu_group_to_pe);
Gavin Shan212d16c2014-06-10 11:41:56 +10001479
Benjamin Herrenschmidt2194dc22014-08-05 18:52:59 +10001480#endif /* CONFIG_IOMMU_API */
1481
Gavin Shan212d16c2014-06-10 11:41:56 +10001482/**
1483 * eeh_pe_set_option - Set options for the indicated PE
1484 * @pe: EEH PE
1485 * @option: requested option
1486 *
1487 * The routine is called to enable or disable EEH functionality
1488 * on the indicated PE, to enable IO or DMA for the frozen PE.
1489 */
1490int eeh_pe_set_option(struct eeh_pe *pe, int option)
1491{
1492 int ret = 0;
1493
1494 /* Invalid PE ? */
1495 if (!pe)
1496 return -ENODEV;
1497
1498 /*
1499 * EEH functionality could possibly be disabled, just
1500 * return error for the case. And the EEH functinality
1501 * isn't expected to be disabled on one specific PE.
1502 */
1503 switch (option) {
1504 case EEH_OPT_ENABLE:
Gavin Shan4eeeff02014-09-30 12:39:01 +10001505 if (eeh_enabled()) {
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001506 ret = eeh_pe_change_owner(pe);
Gavin Shan212d16c2014-06-10 11:41:56 +10001507 break;
Gavin Shan4eeeff02014-09-30 12:39:01 +10001508 }
Gavin Shan212d16c2014-06-10 11:41:56 +10001509 ret = -EIO;
1510 break;
1511 case EEH_OPT_DISABLE:
1512 break;
1513 case EEH_OPT_THAW_MMIO:
1514 case EEH_OPT_THAW_DMA:
1515 if (!eeh_ops || !eeh_ops->set_option) {
1516 ret = -ENOENT;
1517 break;
1518 }
1519
Gavin Shan4eeeff02014-09-30 12:39:01 +10001520 ret = eeh_pci_enable(pe, option);
Gavin Shan212d16c2014-06-10 11:41:56 +10001521 break;
1522 default:
1523 pr_debug("%s: Option %d out of range (%d, %d)\n",
1524 __func__, option, EEH_OPT_DISABLE, EEH_OPT_THAW_DMA);
1525 ret = -EINVAL;
1526 }
1527
1528 return ret;
1529}
1530EXPORT_SYMBOL_GPL(eeh_pe_set_option);
1531
1532/**
1533 * eeh_pe_get_state - Retrieve PE's state
1534 * @pe: EEH PE
1535 *
1536 * Retrieve the PE's state, which includes 3 aspects: enabled
1537 * DMA, enabled IO and asserted reset.
1538 */
1539int eeh_pe_get_state(struct eeh_pe *pe)
1540{
1541 int result, ret = 0;
1542 bool rst_active, dma_en, mmio_en;
1543
1544 /* Existing PE ? */
1545 if (!pe)
1546 return -ENODEV;
1547
1548 if (!eeh_ops || !eeh_ops->get_state)
1549 return -ENOENT;
1550
1551 result = eeh_ops->get_state(pe, NULL);
1552 rst_active = !!(result & EEH_STATE_RESET_ACTIVE);
1553 dma_en = !!(result & EEH_STATE_DMA_ENABLED);
1554 mmio_en = !!(result & EEH_STATE_MMIO_ENABLED);
1555
1556 if (rst_active)
1557 ret = EEH_PE_STATE_RESET;
1558 else if (dma_en && mmio_en)
1559 ret = EEH_PE_STATE_NORMAL;
1560 else if (!dma_en && !mmio_en)
1561 ret = EEH_PE_STATE_STOPPED_IO_DMA;
1562 else if (!dma_en && mmio_en)
1563 ret = EEH_PE_STATE_STOPPED_DMA;
1564 else
1565 ret = EEH_PE_STATE_UNAVAIL;
1566
1567 return ret;
1568}
1569EXPORT_SYMBOL_GPL(eeh_pe_get_state);
1570
Gavin Shan316233f2014-09-30 12:38:53 +10001571static int eeh_pe_reenable_devices(struct eeh_pe *pe)
1572{
1573 struct eeh_dev *edev, *tmp;
1574 struct pci_dev *pdev;
1575 int ret = 0;
1576
1577 /* Restore config space */
1578 eeh_pe_restore_bars(pe);
1579
1580 /*
1581 * Reenable PCI devices as the devices passed
1582 * through are always enabled before the reset.
1583 */
1584 eeh_pe_for_each_dev(pe, edev, tmp) {
1585 pdev = eeh_dev_to_pci_dev(edev);
1586 if (!pdev)
1587 continue;
1588
1589 ret = pci_reenable_device(pdev);
1590 if (ret) {
1591 pr_warn("%s: Failure %d reenabling %s\n",
1592 __func__, ret, pci_name(pdev));
1593 return ret;
1594 }
1595 }
1596
1597 /* The PE is still in frozen state */
Gavin Shanc9dd0142014-09-30 12:39:02 +10001598 return eeh_unfreeze_pe(pe, true);
Gavin Shan316233f2014-09-30 12:38:53 +10001599}
1600
Gavin Shan212d16c2014-06-10 11:41:56 +10001601/**
1602 * eeh_pe_reset - Issue PE reset according to specified type
1603 * @pe: EEH PE
1604 * @option: reset type
1605 *
1606 * The routine is called to reset the specified PE with the
1607 * indicated type, either fundamental reset or hot reset.
1608 * PE reset is the most important part for error recovery.
1609 */
1610int eeh_pe_reset(struct eeh_pe *pe, int option)
1611{
1612 int ret = 0;
1613
1614 /* Invalid PE ? */
1615 if (!pe)
1616 return -ENODEV;
1617
1618 if (!eeh_ops || !eeh_ops->set_option || !eeh_ops->reset)
1619 return -ENOENT;
1620
1621 switch (option) {
1622 case EEH_RESET_DEACTIVATE:
1623 ret = eeh_ops->reset(pe, option);
Gavin Shan8a6b3712014-10-01 17:07:50 +10001624 eeh_pe_state_clear(pe, EEH_PE_CFG_BLOCKED);
Gavin Shan212d16c2014-06-10 11:41:56 +10001625 if (ret)
1626 break;
1627
Gavin Shan316233f2014-09-30 12:38:53 +10001628 ret = eeh_pe_reenable_devices(pe);
Gavin Shan212d16c2014-06-10 11:41:56 +10001629 break;
1630 case EEH_RESET_HOT:
1631 case EEH_RESET_FUNDAMENTAL:
Gavin Shan0d5ee522014-09-30 12:38:52 +10001632 /*
1633 * Proactively freeze the PE to drop all MMIO access
1634 * during reset, which should be banned as it's always
1635 * cause recursive EEH error.
1636 */
1637 eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);
1638
Gavin Shan8a6b3712014-10-01 17:07:50 +10001639 eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
Gavin Shan212d16c2014-06-10 11:41:56 +10001640 ret = eeh_ops->reset(pe, option);
1641 break;
1642 default:
1643 pr_debug("%s: Unsupported option %d\n",
1644 __func__, option);
1645 ret = -EINVAL;
1646 }
1647
1648 return ret;
1649}
1650EXPORT_SYMBOL_GPL(eeh_pe_reset);
1651
1652/**
1653 * eeh_pe_configure - Configure PCI bridges after PE reset
1654 * @pe: EEH PE
1655 *
1656 * The routine is called to restore the PCI config space for
1657 * those PCI devices, especially PCI bridges affected by PE
1658 * reset issued previously.
1659 */
1660int eeh_pe_configure(struct eeh_pe *pe)
1661{
1662 int ret = 0;
1663
1664 /* Invalid PE ? */
1665 if (!pe)
1666 return -ENODEV;
1667
Gavin Shan212d16c2014-06-10 11:41:56 +10001668 return ret;
1669}
1670EXPORT_SYMBOL_GPL(eeh_pe_configure);
1671
Gavin Shanec33d362015-03-26 16:42:08 +11001672/**
1673 * eeh_pe_inject_err - Injecting the specified PCI error to the indicated PE
1674 * @pe: the indicated PE
1675 * @type: error type
1676 * @function: error function
1677 * @addr: address
1678 * @mask: address mask
1679 *
1680 * The routine is called to inject the specified PCI error, which
1681 * is determined by @type and @function, to the indicated PE for
1682 * testing purpose.
1683 */
1684int eeh_pe_inject_err(struct eeh_pe *pe, int type, int func,
1685 unsigned long addr, unsigned long mask)
1686{
1687 /* Invalid PE ? */
1688 if (!pe)
1689 return -ENODEV;
1690
1691 /* Unsupported operation ? */
1692 if (!eeh_ops || !eeh_ops->err_inject)
1693 return -ENOENT;
1694
1695 /* Check on PCI error type */
1696 if (type != EEH_ERR_TYPE_32 && type != EEH_ERR_TYPE_64)
1697 return -EINVAL;
1698
1699 /* Check on PCI error function */
1700 if (func < EEH_ERR_FUNC_MIN || func > EEH_ERR_FUNC_MAX)
1701 return -EINVAL;
1702
1703 return eeh_ops->err_inject(pe, type, func, addr, mask);
1704}
1705EXPORT_SYMBOL_GPL(eeh_pe_inject_err);
1706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707static int proc_eeh_show(struct seq_file *m, void *v)
1708{
Gavin Shan2ec5a0a2014-02-12 15:24:55 +08001709 if (!eeh_enabled()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 seq_printf(m, "EEH Subsystem is globally disabled\n");
Gavin Shane575f8d2012-02-29 15:47:45 +00001711 seq_printf(m, "eeh_total_mmio_ffs=%llu\n", eeh_stats.total_mmio_ffs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 } else {
1713 seq_printf(m, "EEH Subsystem is enabled\n");
Linas Vepstas177bc932005-11-03 18:48:52 -06001714 seq_printf(m,
Gavin Shane575f8d2012-02-29 15:47:45 +00001715 "no device=%llu\n"
1716 "no device node=%llu\n"
1717 "no config address=%llu\n"
1718 "check not wanted=%llu\n"
1719 "eeh_total_mmio_ffs=%llu\n"
1720 "eeh_false_positives=%llu\n"
1721 "eeh_slot_resets=%llu\n",
1722 eeh_stats.no_device,
1723 eeh_stats.no_dn,
1724 eeh_stats.no_cfg_addr,
1725 eeh_stats.ignored_check,
1726 eeh_stats.total_mmio_ffs,
1727 eeh_stats.false_positives,
1728 eeh_stats.slot_resets);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 }
1730
1731 return 0;
1732}
1733
1734static int proc_eeh_open(struct inode *inode, struct file *file)
1735{
1736 return single_open(file, proc_eeh_show, NULL);
1737}
1738
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001739static const struct file_operations proc_eeh_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 .open = proc_eeh_open,
1741 .read = seq_read,
1742 .llseek = seq_lseek,
1743 .release = single_release,
1744};
1745
Gavin Shan7f52a5262014-04-24 18:00:18 +10001746#ifdef CONFIG_DEBUG_FS
1747static int eeh_enable_dbgfs_set(void *data, u64 val)
1748{
1749 if (val)
Gavin Shan05b17212014-07-17 14:41:38 +10001750 eeh_clear_flag(EEH_FORCE_DISABLED);
Gavin Shan7f52a5262014-04-24 18:00:18 +10001751 else
Gavin Shan05b17212014-07-17 14:41:38 +10001752 eeh_add_flag(EEH_FORCE_DISABLED);
Gavin Shan7f52a5262014-04-24 18:00:18 +10001753
1754 /* Notify the backend */
1755 if (eeh_ops->post_init)
1756 eeh_ops->post_init();
1757
1758 return 0;
1759}
1760
1761static int eeh_enable_dbgfs_get(void *data, u64 *val)
1762{
1763 if (eeh_enabled())
1764 *val = 0x1ul;
1765 else
1766 *val = 0x0ul;
1767 return 0;
1768}
1769
Gavin Shan1b28f172014-12-11 14:28:56 +11001770static int eeh_freeze_dbgfs_set(void *data, u64 val)
1771{
1772 eeh_max_freezes = val;
1773 return 0;
1774}
1775
1776static int eeh_freeze_dbgfs_get(void *data, u64 *val)
1777{
1778 *val = eeh_max_freezes;
1779 return 0;
1780}
1781
Gavin Shan7f52a5262014-04-24 18:00:18 +10001782DEFINE_SIMPLE_ATTRIBUTE(eeh_enable_dbgfs_ops, eeh_enable_dbgfs_get,
1783 eeh_enable_dbgfs_set, "0x%llx\n");
Gavin Shan1b28f172014-12-11 14:28:56 +11001784DEFINE_SIMPLE_ATTRIBUTE(eeh_freeze_dbgfs_ops, eeh_freeze_dbgfs_get,
1785 eeh_freeze_dbgfs_set, "0x%llx\n");
Gavin Shan7f52a5262014-04-24 18:00:18 +10001786#endif
1787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788static int __init eeh_init_proc(void)
1789{
Gavin Shan7f52a5262014-04-24 18:00:18 +10001790 if (machine_is(pseries) || machine_is(powernv)) {
Thadeu Lima de Souza Cascardo8feaa432011-08-26 10:36:31 +00001791 proc_create("powerpc/eeh", 0, NULL, &proc_eeh_operations);
Gavin Shan7f52a5262014-04-24 18:00:18 +10001792#ifdef CONFIG_DEBUG_FS
1793 debugfs_create_file("eeh_enable", 0600,
1794 powerpc_debugfs_root, NULL,
1795 &eeh_enable_dbgfs_ops);
Gavin Shan1b28f172014-12-11 14:28:56 +11001796 debugfs_create_file("eeh_max_freezes", 0600,
1797 powerpc_debugfs_root, NULL,
1798 &eeh_freeze_dbgfs_ops);
Gavin Shan7f52a5262014-04-24 18:00:18 +10001799#endif
1800 }
1801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 return 0;
1803}
1804__initcall(eeh_init_proc);