blob: 22f646176abb7fbea07dff62e6e83f814927436c [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linas Vepstas3c8c90a2007-05-24 03:28:01 +10003 * Copyright IBM Corporation 2001, 2005, 2006
4 * Copyright Dave Engebretsen & Todd Inglett 2001
5 * Copyright Linas Vepstas 2005, 2006
Gavin Shancb3bc9d2012-02-27 20:03:51 +00006 * Copyright 2001-2012 IBM Corporation.
Linas Vepstas69376502005-11-03 18:47:50 -06007 *
Linas Vepstas3c8c90a2007-05-24 03:28:01 +10008 * Please address comments and feedback to Linas Vepstas <linas@austin.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Linas Vepstas6dee3fb2005-11-03 18:50:10 -060011#include <linux/delay.h>
Gavin Shancb3bc9d2012-02-27 20:03:51 +000012#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/init.h>
14#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/pci.h>
Gavin Shana3032ca2014-07-15 17:00:56 +100016#include <linux/iommu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/proc_fs.h>
18#include <linux/rbtree.h>
Gavin Shan66f9af832014-02-12 15:24:56 +080019#include <linux/reboot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/seq_file.h>
21#include <linux/spinlock.h>
Paul Gortmaker66b15db2011-05-27 10:46:24 -040022#include <linux/export.h>
Stephen Rothwellacaa6172007-12-21 15:52:07 +110023#include <linux/of.h>
24
Arun Sharma600634972011-07-26 16:09:06 -070025#include <linux/atomic.h>
Michael Ellerman7644d582017-02-10 12:04:56 +110026#include <asm/debugfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/eeh.h>
Linas Vepstas172ca922005-11-03 18:50:04 -060028#include <asm/eeh_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/io.h>
Gavin Shan212d16c2014-06-10 11:41:56 +100030#include <asm/iommu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/machdep.h>
Stephen Rothwelld3878992005-09-28 02:50:25 +100032#include <asm/ppc-pci.h>
Linas Vepstas172ca922005-11-03 18:50:04 -060033#include <asm/rtas.h>
Aneesh Kumar K.V94171b12017-07-27 11:54:53 +053034#include <asm/pte-walk.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/** Overview:
Russell Currey8ee26532016-02-16 23:06:05 +110038 * EEH, or "Enhanced Error Handling" is a PCI bridge technology for
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * dealing with PCI bus errors that can't be dealt with within the
40 * usual PCI framework, except by check-stopping the CPU. Systems
41 * that are designed for high-availability/reliability cannot afford
42 * to crash due to a "mere" PCI error, thus the need for EEH.
43 * An EEH-capable bridge operates by converting a detected error
44 * into a "slot freeze", taking the PCI adapter off-line, making
45 * the slot behave, from the OS'es point of view, as if the slot
46 * were "empty": all reads return 0xff's and all writes are silently
47 * ignored. EEH slot isolation events can be triggered by parity
48 * errors on the address or data busses (e.g. during posted writes),
Linas Vepstas69376502005-11-03 18:47:50 -060049 * which in turn might be caused by low voltage on the bus, dust,
50 * vibration, humidity, radioactivity or plain-old failed hardware.
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 *
52 * Note, however, that one of the leading causes of EEH slot
53 * freeze events are buggy device drivers, buggy device microcode,
54 * or buggy device hardware. This is because any attempt by the
55 * device to bus-master data to a memory address that is not
56 * assigned to the device will trigger a slot freeze. (The idea
57 * is to prevent devices-gone-wild from corrupting system memory).
58 * Buggy hardware/drivers will have a miserable time co-existing
59 * with EEH.
60 *
61 * Ideally, a PCI device driver, when suspecting that an isolation
Lucas De Marchi25985ed2011-03-30 22:57:33 -030062 * event has occurred (e.g. by reading 0xff's), will then ask EEH
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * whether this is the case, and then take appropriate steps to
64 * reset the PCI slot, the PCI device, and then resume operations.
65 * However, until that day, the checking is done here, with the
66 * eeh_check_failure() routine embedded in the MMIO macros. If
67 * the slot is found to be isolated, an "EEH Event" is synthesized
68 * and sent out for processing.
69 */
70
Linas Vepstas5c1344e2005-11-03 18:49:31 -060071/* If a device driver keeps reading an MMIO register in an interrupt
Mike Masonf36c5222008-07-22 02:40:17 +100072 * handler after a slot isolation event, it might be broken.
73 * This sets the threshold for how many read attempts we allow
74 * before printing an error message.
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 */
Linas Vepstas2fd30be2007-03-19 14:53:22 -050076#define EEH_MAX_FAILS 2100000
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Linas Vepstas17213c32007-05-10 02:38:11 +100078/* Time to wait for a PCI slot to report status, in milliseconds */
Brian Kingfb48dc22013-11-25 16:27:54 -060079#define PCI_BUS_RESET_WAIT_MSEC (5*60*1000)
Linas Vepstas9c547762007-03-19 14:58:07 -050080
Gavin Shan8a5ad352014-04-24 18:00:17 +100081/*
82 * EEH probe mode support, which is part of the flags,
83 * is to support multiple platforms for EEH. Some platforms
84 * like pSeries do PCI emunation based on device tree.
85 * However, other platforms like powernv probe PCI devices
86 * from hardware. The flag is used to distinguish that.
87 * In addition, struct eeh_ops::probe would be invoked for
88 * particular OF node or PCI device so that the corresponding
89 * PE would be created there.
90 */
91int eeh_subsystem_flags;
92EXPORT_SYMBOL(eeh_subsystem_flags);
93
Gavin Shan1b28f172014-12-11 14:28:56 +110094/*
95 * EEH allowed maximal frozen times. If one particular PE's
96 * frozen count in last hour exceeds this limit, the PE will
97 * be forced to be offline permanently.
98 */
Oliver O'Halloran46ee7c32019-02-15 11:48:11 +110099u32 eeh_max_freezes = 5;
Gavin Shan1b28f172014-12-11 14:28:56 +1100100
Oliver O'Halloran6b493f62019-02-15 11:48:16 +1100101/*
102 * Controls whether a recovery event should be scheduled when an
103 * isolated device is discovered. This is only really useful for
104 * debugging problems with the EEH core.
105 */
106bool eeh_debugfs_no_recover;
107
Gavin Shanaa1e6372012-02-27 20:03:53 +0000108/* Platform dependent EEH operations */
109struct eeh_ops *eeh_ops = NULL;
110
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600111/* Lock to avoid races due to multiple reports of an error */
Gavin Shan49075812013-06-20 13:21:03 +0800112DEFINE_RAW_SPINLOCK(confirm_error_lock);
Gavin Shan35066c02016-09-28 14:34:54 +1000113EXPORT_SYMBOL_GPL(confirm_error_lock);
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600114
Gavin Shan212d16c2014-06-10 11:41:56 +1000115/* Lock to protect passed flags */
116static DEFINE_MUTEX(eeh_dev_mutex);
117
Linas Vepstas17213c32007-05-10 02:38:11 +1000118/* Buffer for reporting pci register dumps. Its here in BSS, and
119 * not dynamically alloced, so that it ends up in RMO where RTAS
120 * can access it.
121 */
Gavin Shanf2e0be52014-09-30 12:39:08 +1000122#define EEH_PCI_REGS_LOG_LEN 8192
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000123static unsigned char pci_regs_buf[EEH_PCI_REGS_LOG_LEN];
124
Gavin Shane575f8d2012-02-29 15:47:45 +0000125/*
126 * The struct is used to maintain the EEH global statistic
127 * information. Besides, the EEH global statistics will be
128 * exported to user space through procfs
129 */
130struct eeh_stats {
131 u64 no_device; /* PCI device not found */
132 u64 no_dn; /* OF node not found */
133 u64 no_cfg_addr; /* Config address not found */
134 u64 ignored_check; /* EEH check skipped */
135 u64 total_mmio_ffs; /* Total EEH checks */
136 u64 false_positives; /* Unnecessary EEH checks */
137 u64 slot_resets; /* PE reset */
138};
139
140static struct eeh_stats eeh_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Gavin Shan7f52a5262014-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 Shan7f52a5262014-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 Shan0bd78582015-03-17 16:15:07 +1100160 struct pci_dn *pdn = eeh_dev_to_pdn(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
Sam Bobrofff9bc28a2018-09-12 11:23:20 +1000166 if (!pdn) {
167 pr_warn("EEH: Note: No error log for absent device.\n");
168 return 0;
169 }
170
Guilherme G. Piccoli10560b92016-07-22 14:05:29 -0300171 n += scnprintf(buf+n, len-n, "%04x:%02x:%02x.%01x\n",
Alexey Kardashevskiy69672bd2017-08-29 17:34:01 +1000172 pdn->phb->global_number, pdn->busno,
Gavin Shan0bd78582015-03-17 16:15:07 +1100173 PCI_SLOT(pdn->devfn), PCI_FUNC(pdn->devfn));
Guilherme G. Piccoli10560b92016-07-22 14:05:29 -0300174 pr_warn("EEH: of node=%04x:%02x:%02x.%01x\n",
Alexey Kardashevskiy69672bd2017-08-29 17:34:01 +1000175 pdn->phb->global_number, pdn->busno,
Gavin Shan0bd78582015-03-17 16:15:07 +1100176 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
Sam Bobroffd6c49322018-05-25 13:11:32 +1000265static void *eeh_dump_pe_log(struct eeh_pe *pe, void *flag)
Gavin Shanf2e0be52014-09-30 12:39:08 +1000266{
Gavin Shanf2e0be52014-09-30 12:39:08 +1000267 struct eeh_dev *edev, *tmp;
268 size_t *plen = flag;
269
270 eeh_pe_for_each_dev(pe, edev, tmp)
271 *plen += eeh_dump_dev_log(edev, pci_regs_buf + *plen,
272 EEH_PCI_REGS_LOG_LEN - *plen);
273
274 return NULL;
275}
276
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000277/**
278 * eeh_slot_error_detail - Generate combined log including driver log and error log
Gavin Shanff477962012-09-07 22:44:16 +0000279 * @pe: EEH PE
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000280 * @severity: temporary or permanent error log
281 *
282 * This routine should be called to generate the combined log, which
283 * is comprised of driver log and error log. The driver log is figured
284 * out from the config space of the corresponding PCI device, while
285 * the error log is fetched through platform dependent function call.
286 */
Gavin Shanff477962012-09-07 22:44:16 +0000287void eeh_slot_error_detail(struct eeh_pe *pe, int severity)
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000288{
289 size_t loglen = 0;
Gavin Shanff477962012-09-07 22:44:16 +0000290
Gavin Shanc35ae172013-06-27 13:46:42 +0800291 /*
292 * When the PHB is fenced or dead, it's pointless to collect
293 * the data from PCI config space because it should return
294 * 0xFF's. For ER, we still retrieve the data from the PCI
295 * config space.
Gavin Shan78954702014-04-24 18:00:14 +1000296 *
297 * For pHyp, we have to enable IO for log retrieval. Otherwise,
298 * 0xFF's is always returned from PCI config space.
Gavin Shan387bbc92017-01-06 10:39:49 +1100299 *
300 * When the @severity is EEH_LOG_PERM, the PE is going to be
301 * removed. Prior to that, the drivers for devices included in
302 * the PE will be closed. The drivers rely on working IO path
303 * to bring the devices to quiet state. Otherwise, PCI traffic
304 * from those devices after they are removed is like to cause
305 * another unexpected EEH error.
Gavin Shanc35ae172013-06-27 13:46:42 +0800306 */
Gavin Shan9e049372014-04-24 18:00:07 +1000307 if (!(pe->type & EEH_PE_PHB)) {
Gavin Shan387bbc92017-01-06 10:39:49 +1100308 if (eeh_has_flag(EEH_ENABLE_IO_FOR_LOG) ||
309 severity == EEH_LOG_PERM)
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.V94171b12017-07-27 11:54:53 +0530354 ptep = find_init_mm_pte(token, &hugepage_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (!ptep)
356 return token;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Oliver O'Halloran33439622019-07-11 01:05:17 +1000358 pa = pte_pfn(*ptep);
359
360 /* On radix we can do hugepage mappings for io, so handle that */
361 if (hugepage_shift) {
362 pa <<= hugepage_shift;
363 pa |= token & ((1ul << hugepage_shift) - 1);
364 } else {
365 pa <<= PAGE_SHIFT;
366 pa |= token & (PAGE_SIZE - 1);
367 }
368
369 return pa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800372/*
373 * On PowerNV platform, we might already have fenced PHB there.
374 * For that case, it's meaningless to recover frozen PE. Intead,
375 * We have to handle fenced PHB firstly.
376 */
377static int eeh_phb_check_failure(struct eeh_pe *pe)
378{
379 struct eeh_pe *phb_pe;
380 unsigned long flags;
381 int ret;
382
Gavin Shan05b17212014-07-17 14:41:38 +1000383 if (!eeh_has_flag(EEH_PROBE_MODE_DEV))
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800384 return -EPERM;
385
386 /* Find the PHB PE */
387 phb_pe = eeh_phb_pe_get(pe->phb);
388 if (!phb_pe) {
Russell Currey1f52f172016-11-16 14:02:15 +1100389 pr_warn("%s Can't find PE for PHB#%x\n",
Gavin Shan0dae2742014-07-17 14:41:41 +1000390 __func__, pe->phb->global_number);
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800391 return -EEXIST;
392 }
393
394 /* If the PHB has been in problematic state */
395 eeh_serialize_lock(&flags);
Gavin Shan9e049372014-04-24 18:00:07 +1000396 if (phb_pe->state & EEH_PE_ISOLATED) {
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800397 ret = 0;
398 goto out;
399 }
400
401 /* Check PHB state */
402 ret = eeh_ops->get_state(phb_pe, NULL);
403 if ((ret < 0) ||
Sam Bobroff34a286a2018-03-19 13:49:23 +1100404 (ret == EEH_STATE_NOT_SUPPORT) || eeh_state_active(ret)) {
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800405 ret = 0;
406 goto out;
407 }
408
409 /* Isolate the PHB and send event */
Sam Bobroffe762bb82018-09-12 11:23:31 +1000410 eeh_pe_mark_isolated(phb_pe);
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800411 eeh_serialize_unlock(flags);
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800412
Gavin Shan357b2f32014-06-11 18:26:44 +1000413 pr_err("EEH: PHB#%x failure detected, location: %s\n",
414 phb_pe->phb->global_number, eeh_pe_loc_get(phb_pe));
Gavin Shan56ca4fd2013-06-27 13:46:46 +0800415 dump_stack();
Gavin Shan5293bf92013-09-06 09:00:05 +0800416 eeh_send_failure_event(phb_pe);
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800417
418 return 1;
419out:
420 eeh_serialize_unlock(flags);
421 return ret;
422}
423
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000424/**
Gavin Shanf8f7d632012-09-07 22:44:22 +0000425 * eeh_dev_check_failure - Check if all 1's data is due to EEH slot freeze
426 * @edev: eeh device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 *
428 * Check for an EEH failure for the given device node. Call this
429 * routine if the result of a read was all 0xff's and you want to
430 * find out if this is due to an EEH slot freeze. This routine
431 * will query firmware for the EEH status.
432 *
433 * Returns 0 if there has not been an EEH error; otherwise returns
Linas Vepstas69376502005-11-03 18:47:50 -0600434 * a non-zero value and queues up a slot isolation event notification.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 *
436 * It is safe to call this routine in an interrupt context.
437 */
Gavin Shanf8f7d632012-09-07 22:44:22 +0000438int eeh_dev_check_failure(struct eeh_dev *edev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
440 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 unsigned long flags;
Alexey Kardashevskiy14db3d52017-08-29 17:34:03 +1000442 struct device_node *dn;
Gavin Shanf8f7d632012-09-07 22:44:22 +0000443 struct pci_dev *dev;
Gavin Shan357b2f32014-06-11 18:26:44 +1000444 struct eeh_pe *pe, *parent_pe, *phb_pe;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600445 int rc = 0;
Gavin Shanc6406d82015-03-17 16:15:08 +1100446 const char *location = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Gavin Shane575f8d2012-02-29 15:47:45 +0000448 eeh_stats.total_mmio_ffs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Gavin Shan2ec5a0a2014-02-12 15:24:55 +0800450 if (!eeh_enabled())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return 0;
452
Gavin Shanf8f7d632012-09-07 22:44:22 +0000453 if (!edev) {
Gavin Shane575f8d2012-02-29 15:47:45 +0000454 eeh_stats.no_dn++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return 0;
Linas Vepstas177bc932005-11-03 18:48:52 -0600456 }
Gavin Shanf8f7d632012-09-07 22:44:22 +0000457 dev = eeh_dev_to_pci_dev(edev);
Wei Yang2a582222014-09-17 10:48:26 +0800458 pe = eeh_dev_to_pe(edev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 /* Access to IO BARs might get this far and still not want checking. */
Gavin Shan66523d92012-09-07 22:44:13 +0000461 if (!pe) {
Gavin Shane575f8d2012-02-29 15:47:45 +0000462 eeh_stats.ignored_check++;
Gavin Shanc6406d82015-03-17 16:15:08 +1100463 pr_debug("EEH: Ignored check for %s\n",
464 eeh_pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return 0;
466 }
467
Gavin Shan66523d92012-09-07 22:44:13 +0000468 if (!pe->addr && !pe->config_addr) {
Gavin Shane575f8d2012-02-29 15:47:45 +0000469 eeh_stats.no_cfg_addr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return 0;
471 }
472
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800473 /*
474 * On PowerNV platform, we might already have fenced PHB
475 * there and we need take care of that firstly.
476 */
477 ret = eeh_phb_check_failure(pe);
478 if (ret > 0)
479 return ret;
480
Gavin Shan05ec4242014-06-10 11:41:55 +1000481 /*
482 * If the PE isn't owned by us, we shouldn't check the
483 * state. Instead, let the owner handle it if the PE has
484 * been frozen.
485 */
486 if (eeh_pe_passed(pe))
487 return 0;
488
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600489 /* If we already have a pending isolation event for this
490 * slot, we know it's bad already, we don't need to check.
491 * Do this checking under a lock; as multiple PCI devices
492 * in one slot might report errors simultaneously, and we
493 * only want one error recovery routine running.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 */
Gavin Shan49075812013-06-20 13:21:03 +0800495 eeh_serialize_lock(&flags);
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600496 rc = 1;
Gavin Shan66523d92012-09-07 22:44:13 +0000497 if (pe->state & EEH_PE_ISOLATED) {
498 pe->check_count++;
499 if (pe->check_count % EEH_MAX_FAILS == 0) {
Alexey Kardashevskiy14db3d52017-08-29 17:34:03 +1000500 dn = pci_device_to_OF_node(dev);
501 if (dn)
502 location = of_get_property(dn, "ibm,loc-code",
503 NULL);
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000504 printk(KERN_ERR "EEH: %d reads ignored for recovering device at "
Mike Masonf36c5222008-07-22 02:40:17 +1000505 "location=%s driver=%s pci addr=%s\n",
Gavin Shanc6406d82015-03-17 16:15:08 +1100506 pe->check_count,
507 location ? location : "unknown",
Thadeu Lima de Souza Cascardo778a7852012-01-11 09:09:58 +0000508 eeh_driver_name(dev), eeh_pci_name(dev));
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000509 printk(KERN_ERR "EEH: Might be infinite loop in %s driver\n",
Thadeu Lima de Souza Cascardo778a7852012-01-11 09:09:58 +0000510 eeh_driver_name(dev));
Linas Vepstas5c1344e2005-11-03 18:49:31 -0600511 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600513 goto dn_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515
516 /*
517 * Now test for an EEH failure. This is VERY expensive.
518 * Note that the eeh_config_addr may be a parent device
519 * in the case of a device behind a bridge, or it may be
520 * function zero of a multi-function device.
521 * In any case they must share a common PHB.
522 */
Gavin Shan66523d92012-09-07 22:44:13 +0000523 ret = eeh_ops->get_state(pe, NULL);
Linas Vepstas76e6faf2005-11-03 18:49:15 -0600524
Linas Vepstas39d16e22007-03-19 14:51:00 -0500525 /* Note that config-io to empty slots may fail;
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000526 * they are empty when they don't have children.
Gavin Shaneb594a42012-02-27 20:03:57 +0000527 * We will punt with the following conditions: Failure to get
528 * PE's state, EEH not support and Permanently unavailable
529 * state, PE is in good state.
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000530 */
Gavin Shaneb594a42012-02-27 20:03:57 +0000531 if ((ret < 0) ||
Sam Bobroff34a286a2018-03-19 13:49:23 +1100532 (ret == EEH_STATE_NOT_SUPPORT) || eeh_state_active(ret)) {
Gavin Shane575f8d2012-02-29 15:47:45 +0000533 eeh_stats.false_positives++;
Gavin Shan66523d92012-09-07 22:44:13 +0000534 pe->false_positives++;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600535 rc = 0;
536 goto dn_unlock;
Linas Vepstas76e6faf2005-11-03 18:49:15 -0600537 }
538
Gavin Shan1ad7a722014-05-05 09:29:03 +1000539 /*
540 * It should be corner case that the parent PE has been
541 * put into frozen state as well. We should take care
542 * that at first.
543 */
544 parent_pe = pe->parent;
545 while (parent_pe) {
546 /* Hit the ceiling ? */
547 if (parent_pe->type & EEH_PE_PHB)
548 break;
549
550 /* Frozen parent PE ? */
551 ret = eeh_ops->get_state(parent_pe, NULL);
Sam Bobroff2eae39f2018-05-25 13:11:33 +1000552 if (ret > 0 && !eeh_state_active(ret)) {
Gavin Shan1ad7a722014-05-05 09:29:03 +1000553 pe = parent_pe;
Sam Bobroff2eae39f2018-05-25 13:11:33 +1000554 pr_err("EEH: Failure of PHB#%x-PE#%x will be handled at parent PHB#%x-PE#%x.\n",
555 pe->phb->global_number, pe->addr,
556 pe->phb->global_number, parent_pe->addr);
557 }
Gavin Shan1ad7a722014-05-05 09:29:03 +1000558
559 /* Next parent level */
560 parent_pe = parent_pe->parent;
561 }
562
Gavin Shane575f8d2012-02-29 15:47:45 +0000563 eeh_stats.slot_resets++;
Gavin Shana84f2732013-06-20 13:20:51 +0800564
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600565 /* Avoid repeated reports of this failure, including problems
566 * with other functions on this device, and functions under
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000567 * bridges.
568 */
Sam Bobroffe762bb82018-09-12 11:23:31 +1000569 eeh_pe_mark_isolated(pe);
Gavin Shan49075812013-06-20 13:21:03 +0800570 eeh_serialize_unlock(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 /* Most EEH events are due to device driver bugs. Having
573 * a stack trace will help the device-driver authors figure
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000574 * out what happened. So print that out.
575 */
Gavin Shan357b2f32014-06-11 18:26:44 +1000576 phb_pe = eeh_phb_pe_get(pe->phb);
577 pr_err("EEH: Frozen PHB#%x-PE#%x detected\n",
578 pe->phb->global_number, pe->addr);
579 pr_err("EEH: PE location: %s, PHB location: %s\n",
580 eeh_pe_loc_get(pe), eeh_pe_loc_get(phb_pe));
Gavin Shan56ca4fd2013-06-27 13:46:46 +0800581 dump_stack();
582
Gavin Shan5293bf92013-09-06 09:00:05 +0800583 eeh_send_failure_event(pe);
584
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600585 return 1;
586
587dn_unlock:
Gavin Shan49075812013-06-20 13:21:03 +0800588 eeh_serialize_unlock(flags);
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600589 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
Gavin Shanf8f7d632012-09-07 22:44:22 +0000592EXPORT_SYMBOL_GPL(eeh_dev_check_failure);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000595 * eeh_check_failure - Check if all 1's data is due to EEH slot freeze
Gavin Shan3e938052014-09-30 12:38:50 +1000596 * @token: I/O address
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 *
Gavin Shan3e938052014-09-30 12:38:50 +1000598 * Check for an EEH failure at the given I/O address. Call this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 * routine if the result of a read was all 0xff's and you want to
Gavin Shan3e938052014-09-30 12:38:50 +1000600 * find out if this is due to an EEH slot freeze event. This routine
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 * will query firmware for the EEH status.
602 *
603 * Note this routine is safe to call in an interrupt context.
604 */
Gavin Shan3e938052014-09-30 12:38:50 +1000605int eeh_check_failure(const volatile void __iomem *token)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 unsigned long addr;
Gavin Shanf8f7d632012-09-07 22:44:22 +0000608 struct eeh_dev *edev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 /* Finding the phys addr + pci device; this is pretty quick. */
611 addr = eeh_token_to_phys((unsigned long __force) token);
Gavin Shan3ab96a02012-09-07 22:44:23 +0000612 edev = eeh_addr_cache_get_dev(addr);
Gavin Shanf8f7d632012-09-07 22:44:22 +0000613 if (!edev) {
Gavin Shane575f8d2012-02-29 15:47:45 +0000614 eeh_stats.no_device++;
Gavin Shan3e938052014-09-30 12:38:50 +1000615 return 0;
Linas Vepstas177bc932005-11-03 18:48:52 -0600616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Gavin Shan3e938052014-09-30 12:38:50 +1000618 return eeh_dev_check_failure(edev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620EXPORT_SYMBOL(eeh_check_failure);
621
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600622
Linas Vepstascb5b56242006-09-15 18:56:35 -0500623/**
Gavin Shancce4b2d2012-02-27 20:03:52 +0000624 * eeh_pci_enable - Enable MMIO or DMA transfers for this slot
Gavin Shanff477962012-09-07 22:44:16 +0000625 * @pe: EEH PE
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000626 *
627 * This routine should be called to reenable frozen MMIO or DMA
628 * so that it would work correctly again. It's useful while doing
629 * recovery or log collection on the indicated device.
Linas Vepstas47b5c832006-09-15 18:57:42 -0500630 */
Gavin Shanff477962012-09-07 22:44:16 +0000631int eeh_pci_enable(struct eeh_pe *pe, int function)
Linas Vepstas47b5c832006-09-15 18:57:42 -0500632{
Gavin Shan4d4f5772014-09-30 12:39:00 +1000633 int active_flag, rc;
Gavin Shan78954702014-04-24 18:00:14 +1000634
635 /*
636 * pHyp doesn't allow to enable IO or DMA on unfrozen PE.
637 * Also, it's pointless to enable them on unfrozen PE. So
Gavin Shan4d4f5772014-09-30 12:39:00 +1000638 * we have to check before enabling IO or DMA.
Gavin Shan78954702014-04-24 18:00:14 +1000639 */
Gavin Shan4d4f5772014-09-30 12:39:00 +1000640 switch (function) {
641 case EEH_OPT_THAW_MMIO:
Gavin Shan872ee2d2015-10-08 14:58:55 +1100642 active_flag = EEH_STATE_MMIO_ACTIVE | EEH_STATE_MMIO_ENABLED;
Gavin Shan4d4f5772014-09-30 12:39:00 +1000643 break;
644 case EEH_OPT_THAW_DMA:
645 active_flag = EEH_STATE_DMA_ACTIVE;
646 break;
647 case EEH_OPT_DISABLE:
648 case EEH_OPT_ENABLE:
649 case EEH_OPT_FREEZE_PE:
650 active_flag = 0;
651 break;
652 default:
653 pr_warn("%s: Invalid function %d\n",
654 __func__, function);
655 return -EINVAL;
656 }
657
658 /*
659 * Check if IO or DMA has been enabled before
660 * enabling them.
661 */
662 if (active_flag) {
Gavin Shan78954702014-04-24 18:00:14 +1000663 rc = eeh_ops->get_state(pe, NULL);
664 if (rc < 0)
665 return rc;
666
Gavin Shan4d4f5772014-09-30 12:39:00 +1000667 /* Needn't enable it at all */
668 if (rc == EEH_STATE_NOT_SUPPORT)
669 return 0;
670
671 /* It's already enabled */
672 if (rc & active_flag)
Gavin Shan78954702014-04-24 18:00:14 +1000673 return 0;
674 }
Linas Vepstas47b5c832006-09-15 18:57:42 -0500675
Gavin Shan4d4f5772014-09-30 12:39:00 +1000676
677 /* Issue the request */
Gavin Shanff477962012-09-07 22:44:16 +0000678 rc = eeh_ops->set_option(pe, function);
Linas Vepstas47b5c832006-09-15 18:57:42 -0500679 if (rc)
Gavin Shan78954702014-04-24 18:00:14 +1000680 pr_warn("%s: Unexpected state change %d on "
Russell Currey1f52f172016-11-16 14:02:15 +1100681 "PHB#%x-PE#%x, err=%d\n",
Gavin Shan78954702014-04-24 18:00:14 +1000682 __func__, function, pe->phb->global_number,
683 pe->addr, rc);
Linas Vepstas47b5c832006-09-15 18:57:42 -0500684
Gavin Shan4d4f5772014-09-30 12:39:00 +1000685 /* Check if the request is finished successfully */
686 if (active_flag) {
Sam Bobrofffef7f902018-09-12 11:23:32 +1000687 rc = eeh_wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
Andrew Donnellan949e9b82015-10-23 17:19:46 +1100688 if (rc < 0)
Gavin Shan4d4f5772014-09-30 12:39:00 +1000689 return rc;
Gavin Shan78954702014-04-24 18:00:14 +1000690
Gavin Shan4d4f5772014-09-30 12:39:00 +1000691 if (rc & active_flag)
692 return 0;
Gavin Shan78954702014-04-24 18:00:14 +1000693
Gavin Shan4d4f5772014-09-30 12:39:00 +1000694 return -EIO;
695 }
Linas Vepstasfa1be472007-03-19 14:59:59 -0500696
Linas Vepstas47b5c832006-09-15 18:57:42 -0500697 return rc;
698}
699
Sam Bobroffd6c49322018-05-25 13:11:32 +1000700static void *eeh_disable_and_save_dev_state(struct eeh_dev *edev,
701 void *userdata)
Gavin Shan28158cd2015-02-11 10:20:49 +1100702{
Gavin Shan28158cd2015-02-11 10:20:49 +1100703 struct pci_dev *pdev = eeh_dev_to_pci_dev(edev);
704 struct pci_dev *dev = userdata;
705
706 /*
707 * The caller should have disabled and saved the
708 * state for the specified device
709 */
710 if (!pdev || pdev == dev)
711 return NULL;
712
713 /* Ensure we have D0 power state */
714 pci_set_power_state(pdev, PCI_D0);
715
716 /* Save device state */
717 pci_save_state(pdev);
718
719 /*
720 * Disable device to avoid any DMA traffic and
721 * interrupt from the device
722 */
723 pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
724
725 return NULL;
726}
727
Sam Bobroffd6c49322018-05-25 13:11:32 +1000728static void *eeh_restore_dev_state(struct eeh_dev *edev, void *userdata)
Gavin Shan28158cd2015-02-11 10:20:49 +1100729{
Gavin Shan0bd78582015-03-17 16:15:07 +1100730 struct pci_dn *pdn = eeh_dev_to_pdn(edev);
Gavin Shan28158cd2015-02-11 10:20:49 +1100731 struct pci_dev *pdev = eeh_dev_to_pci_dev(edev);
732 struct pci_dev *dev = userdata;
733
734 if (!pdev)
735 return NULL;
736
737 /* Apply customization from firmware */
Gavin Shan0bd78582015-03-17 16:15:07 +1100738 if (pdn && eeh_ops->restore_config)
739 eeh_ops->restore_config(pdn);
Gavin Shan28158cd2015-02-11 10:20:49 +1100740
741 /* The caller should restore state for the specified device */
742 if (pdev != dev)
David Gibson502f1592015-06-03 14:52:59 +1000743 pci_restore_state(pdev);
Gavin Shan28158cd2015-02-11 10:20:49 +1100744
745 return NULL;
746}
747
Bryant G. Ly64ba3dc72018-01-05 10:45:46 -0600748int eeh_restore_vf_config(struct pci_dn *pdn)
749{
750 struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
751 u32 devctl, cmd, cap2, aer_capctl;
752 int old_mps;
753
754 if (edev->pcie_cap) {
755 /* Restore MPS */
756 old_mps = (ffs(pdn->mps) - 8) << 5;
757 eeh_ops->read_config(pdn, edev->pcie_cap + PCI_EXP_DEVCTL,
758 2, &devctl);
759 devctl &= ~PCI_EXP_DEVCTL_PAYLOAD;
760 devctl |= old_mps;
761 eeh_ops->write_config(pdn, edev->pcie_cap + PCI_EXP_DEVCTL,
762 2, devctl);
763
Linus Torvalds105cf3c2018-02-06 09:59:40 -0800764 /* Disable Completion Timeout if possible */
Bryant G. Ly64ba3dc72018-01-05 10:45:46 -0600765 eeh_ops->read_config(pdn, edev->pcie_cap + PCI_EXP_DEVCAP2,
766 4, &cap2);
Linus Torvalds105cf3c2018-02-06 09:59:40 -0800767 if (cap2 & PCI_EXP_DEVCAP2_COMP_TMOUT_DIS) {
Bryant G. Ly64ba3dc72018-01-05 10:45:46 -0600768 eeh_ops->read_config(pdn,
769 edev->pcie_cap + PCI_EXP_DEVCTL2,
770 4, &cap2);
Linus Torvalds105cf3c2018-02-06 09:59:40 -0800771 cap2 |= PCI_EXP_DEVCTL2_COMP_TMOUT_DIS;
Bryant G. Ly64ba3dc72018-01-05 10:45:46 -0600772 eeh_ops->write_config(pdn,
773 edev->pcie_cap + PCI_EXP_DEVCTL2,
774 4, cap2);
775 }
776 }
777
778 /* Enable SERR and parity checking */
779 eeh_ops->read_config(pdn, PCI_COMMAND, 2, &cmd);
780 cmd |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
781 eeh_ops->write_config(pdn, PCI_COMMAND, 2, cmd);
782
783 /* Enable report various errors */
784 if (edev->pcie_cap) {
785 eeh_ops->read_config(pdn, edev->pcie_cap + PCI_EXP_DEVCTL,
786 2, &devctl);
787 devctl &= ~PCI_EXP_DEVCTL_CERE;
788 devctl |= (PCI_EXP_DEVCTL_NFERE |
789 PCI_EXP_DEVCTL_FERE |
790 PCI_EXP_DEVCTL_URRE);
791 eeh_ops->write_config(pdn, edev->pcie_cap + PCI_EXP_DEVCTL,
792 2, devctl);
793 }
794
795 /* Enable ECRC generation and check */
796 if (edev->pcie_cap && edev->aer_cap) {
797 eeh_ops->read_config(pdn, edev->aer_cap + PCI_ERR_CAP,
798 4, &aer_capctl);
799 aer_capctl |= (PCI_ERR_CAP_ECRC_GENE | PCI_ERR_CAP_ECRC_CHKE);
800 eeh_ops->write_config(pdn, edev->aer_cap + PCI_ERR_CAP,
801 4, aer_capctl);
802 }
803
804 return 0;
805}
806
Linas Vepstas47b5c832006-09-15 18:57:42 -0500807/**
Andrew Donnellan31f6a4a2016-02-08 14:39:19 +1100808 * pcibios_set_pcie_reset_state - Set PCI-E reset state
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000809 * @dev: pci device struct
810 * @state: reset state to enter
Brian King00c2ae32007-05-08 08:04:05 +1000811 *
812 * Return value:
813 * 0 if success
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000814 */
Brian King00c2ae32007-05-08 08:04:05 +1000815int pcibios_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
816{
Gavin Shanc270a242012-09-07 22:44:17 +0000817 struct eeh_dev *edev = pci_dev_to_eeh_dev(dev);
Wei Yang2a582222014-09-17 10:48:26 +0800818 struct eeh_pe *pe = eeh_dev_to_pe(edev);
Gavin Shanc270a242012-09-07 22:44:17 +0000819
820 if (!pe) {
821 pr_err("%s: No PE found on PCI device %s\n",
822 __func__, pci_name(dev));
823 return -EINVAL;
824 }
Brian King00c2ae32007-05-08 08:04:05 +1000825
826 switch (state) {
827 case pcie_deassert_reset:
Gavin Shanc270a242012-09-07 22:44:17 +0000828 eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);
Sam Bobroff188fdea2018-11-29 14:16:38 +1100829 eeh_unfreeze_pe(pe);
Wei Yang9312bc52016-03-04 10:53:09 +1100830 if (!(pe->type & EEH_PE_VF))
Sam Bobroff9ed5ca62018-11-29 14:16:39 +1100831 eeh_pe_state_clear(pe, EEH_PE_CFG_BLOCKED, true);
Gavin Shan28158cd2015-02-11 10:20:49 +1100832 eeh_pe_dev_traverse(pe, eeh_restore_dev_state, dev);
Sam Bobroff9ed5ca62018-11-29 14:16:39 +1100833 eeh_pe_state_clear(pe, EEH_PE_ISOLATED, true);
Brian King00c2ae32007-05-08 08:04:05 +1000834 break;
835 case pcie_hot_reset:
Sam Bobroffe762bb82018-09-12 11:23:31 +1000836 eeh_pe_mark_isolated(pe);
Sam Bobroff9ed5ca62018-11-29 14:16:39 +1100837 eeh_pe_state_clear(pe, EEH_PE_CFG_BLOCKED, true);
Gavin Shan28158cd2015-02-11 10:20:49 +1100838 eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);
839 eeh_pe_dev_traverse(pe, eeh_disable_and_save_dev_state, dev);
Wei Yang9312bc52016-03-04 10:53:09 +1100840 if (!(pe->type & EEH_PE_VF))
841 eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
Gavin Shanc270a242012-09-07 22:44:17 +0000842 eeh_ops->reset(pe, EEH_RESET_HOT);
Brian King00c2ae32007-05-08 08:04:05 +1000843 break;
844 case pcie_warm_reset:
Sam Bobroffe762bb82018-09-12 11:23:31 +1000845 eeh_pe_mark_isolated(pe);
Sam Bobroff9ed5ca62018-11-29 14:16:39 +1100846 eeh_pe_state_clear(pe, EEH_PE_CFG_BLOCKED, true);
Gavin Shan28158cd2015-02-11 10:20:49 +1100847 eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);
848 eeh_pe_dev_traverse(pe, eeh_disable_and_save_dev_state, dev);
Wei Yang9312bc52016-03-04 10:53:09 +1100849 if (!(pe->type & EEH_PE_VF))
850 eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
Gavin Shanc270a242012-09-07 22:44:17 +0000851 eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
Brian King00c2ae32007-05-08 08:04:05 +1000852 break;
853 default:
Sam Bobroff9ed5ca62018-11-29 14:16:39 +1100854 eeh_pe_state_clear(pe, EEH_PE_ISOLATED | EEH_PE_CFG_BLOCKED, true);
Brian King00c2ae32007-05-08 08:04:05 +1000855 return -EINVAL;
856 };
857
858 return 0;
859}
860
861/**
Gavin Shanc270a242012-09-07 22:44:17 +0000862 * eeh_set_pe_freset - Check the required reset for the indicated device
863 * @data: EEH device
864 * @flag: return value
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000865 *
866 * Each device might have its preferred reset type: fundamental or
867 * hot reset. The routine is used to collected the information for
868 * the indicated device and its children so that the bunch of the
869 * devices could be reset properly.
870 */
Sam Bobroffd6c49322018-05-25 13:11:32 +1000871static void *eeh_set_dev_freset(struct eeh_dev *edev, void *flag)
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000872{
873 struct pci_dev *dev;
Gavin Shanc270a242012-09-07 22:44:17 +0000874 unsigned int *freset = (unsigned int *)flag;
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000875
Gavin Shanc270a242012-09-07 22:44:17 +0000876 dev = eeh_dev_to_pci_dev(edev);
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000877 if (dev)
878 *freset |= dev->needs_freset;
879
Gavin Shanc270a242012-09-07 22:44:17 +0000880 return NULL;
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000881}
882
Sam Bobroff1ef52072018-11-29 14:16:41 +1100883static void eeh_pe_refreeze_passed(struct eeh_pe *root)
884{
885 struct eeh_pe *pe;
886 int state;
887
888 eeh_for_each_pe(root, pe) {
889 if (eeh_pe_passed(pe)) {
890 state = eeh_ops->get_state(pe, NULL);
891 if (state &
892 (EEH_STATE_MMIO_ACTIVE | EEH_STATE_MMIO_ENABLED)) {
893 pr_info("EEH: Passed-through PE PHB#%x-PE#%x was thawed by reset, re-freezing for safety.\n",
894 pe->phb->global_number, pe->addr);
895 eeh_pe_set_option(pe, EEH_OPT_FREEZE_PE);
896 }
897 }
898 }
899}
900
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000901/**
Russell Currey6654c932016-11-17 16:07:47 +1100902 * eeh_pe_reset_full - Complete a full reset process on the indicated PE
Gavin Shanc270a242012-09-07 22:44:17 +0000903 * @pe: EEH PE
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000904 *
Russell Currey6654c932016-11-17 16:07:47 +1100905 * This function executes a full reset procedure on a PE, including setting
906 * the appropriate flags, performing a fundamental or hot reset, and then
907 * deactivating the reset status. It is designed to be used within the EEH
908 * subsystem, as opposed to eeh_pe_reset which is exported to drivers and
909 * only performs a single operation at a time.
910 *
911 * This function will attempt to reset a PE three times before failing.
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000912 */
Sam Bobroff1ef52072018-11-29 14:16:41 +1100913int eeh_pe_reset_full(struct eeh_pe *pe, bool include_passed)
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600914{
Russell Currey6654c932016-11-17 16:07:47 +1100915 int reset_state = (EEH_PE_RESET | EEH_PE_CFG_BLOCKED);
916 int type = EEH_RESET_HOT;
Richard A Lary308fc4f2011-04-22 09:59:47 +0000917 unsigned int freset = 0;
Sam Bobroff195482c2018-11-29 14:16:42 +1100918 int i, state = 0, ret;
Mike Mason6e193142009-07-30 15:42:39 -0700919
Russell Currey6654c932016-11-17 16:07:47 +1100920 /*
921 * Determine the type of reset to perform - hot or fundamental.
922 * Hot reset is the default operation, unless any device under the
923 * PE requires a fundamental reset.
Gavin Shana84f2732013-06-20 13:20:51 +0800924 */
Gavin Shanc270a242012-09-07 22:44:17 +0000925 eeh_pe_dev_traverse(pe, eeh_set_dev_freset, &freset);
Richard A Lary308fc4f2011-04-22 09:59:47 +0000926
927 if (freset)
Russell Currey6654c932016-11-17 16:07:47 +1100928 type = EEH_RESET_FUNDAMENTAL;
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600929
Russell Currey6654c932016-11-17 16:07:47 +1100930 /* Mark the PE as in reset state and block config space accesses */
931 eeh_pe_state_mark(pe, reset_state);
Linas Vepstase1029262006-09-21 18:25:56 -0500932
Russell Currey6654c932016-11-17 16:07:47 +1100933 /* Make three attempts at resetting the bus */
Gavin Shanb85743e2014-11-14 10:47:28 +1100934 for (i = 0; i < 3; i++) {
Sam Bobroff1ef52072018-11-29 14:16:41 +1100935 ret = eeh_pe_reset(pe, type, include_passed);
Sam Bobroff195482c2018-11-29 14:16:42 +1100936 if (!ret)
937 ret = eeh_pe_reset(pe, EEH_RESET_DEACTIVATE,
938 include_passed);
939 if (ret) {
940 ret = -EIO;
941 pr_warn("EEH: Failure %d resetting PHB#%x-PE#%x (attempt %d)\n\n",
942 state, pe->phb->global_number, pe->addr, i + 1);
943 continue;
944 }
945 if (i)
946 pr_warn("EEH: PHB#%x-PE#%x: Successful reset (attempt %d)\n",
947 pe->phb->global_number, pe->addr, i + 1);
Russell Currey6654c932016-11-17 16:07:47 +1100948
949 /* Wait until the PE is in a functioning state */
Sam Bobrofffef7f902018-09-12 11:23:32 +1000950 state = eeh_wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
Gavin Shanb85743e2014-11-14 10:47:28 +1100951 if (state < 0) {
Sam Bobroff195482c2018-11-29 14:16:42 +1100952 pr_warn("EEH: Unrecoverable slot failure on PHB#%x-PE#%x",
953 pe->phb->global_number, pe->addr);
Gavin Shanb85743e2014-11-14 10:47:28 +1100954 ret = -ENOTRECOVERABLE;
Russell Currey6654c932016-11-17 16:07:47 +1100955 break;
Gavin Shanb85743e2014-11-14 10:47:28 +1100956 }
Sam Bobrofffef7f902018-09-12 11:23:32 +1000957 if (eeh_state_active(state))
958 break;
Sam Bobroff195482c2018-11-29 14:16:42 +1100959 else
960 pr_warn("EEH: PHB#%x-PE#%x: Slot inactive after reset: 0x%x (attempt %d)\n",
961 pe->phb->global_number, pe->addr, state, i + 1);
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600962 }
Linas Vepstasb6495c02005-11-03 18:54:54 -0600963
Sam Bobroff1ef52072018-11-29 14:16:41 +1100964 /* Resetting the PE may have unfrozen child PEs. If those PEs have been
965 * (potentially) passed through to a guest, re-freeze them:
966 */
967 if (!include_passed)
968 eeh_pe_refreeze_passed(pe);
969
Sam Bobroff9ed5ca62018-11-29 14:16:39 +1100970 eeh_pe_state_clear(pe, reset_state, true);
Gavin Shanb85743e2014-11-14 10:47:28 +1100971 return ret;
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600972}
973
Linas Vepstas8b553f32005-11-03 18:50:17 -0600974/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000975 * eeh_save_bars - Save device bars
Gavin Shanf631acd2012-02-27 20:04:07 +0000976 * @edev: PCI device associated EEH device
Linas Vepstas8b553f32005-11-03 18:50:17 -0600977 *
978 * Save the values of the device bars. Unlike the restore
979 * routine, this routine is *not* recursive. This is because
Justin Mattock31116f02011-02-24 20:10:18 +0000980 * PCI devices are added individually; but, for the restore,
Linas Vepstas8b553f32005-11-03 18:50:17 -0600981 * an entire slot is reset at a time.
982 */
Gavin Shand7bb8862012-09-07 22:44:21 +0000983void eeh_save_bars(struct eeh_dev *edev)
Linas Vepstas8b553f32005-11-03 18:50:17 -0600984{
Gavin Shan0bd78582015-03-17 16:15:07 +1100985 struct pci_dn *pdn;
Linas Vepstas8b553f32005-11-03 18:50:17 -0600986 int i;
987
Gavin Shan0bd78582015-03-17 16:15:07 +1100988 pdn = eeh_dev_to_pdn(edev);
989 if (!pdn)
Linas Vepstas8b553f32005-11-03 18:50:17 -0600990 return;
Gavin Shana84f2732013-06-20 13:20:51 +0800991
Linas Vepstas8b553f32005-11-03 18:50:17 -0600992 for (i = 0; i < 16; i++)
Gavin Shan0bd78582015-03-17 16:15:07 +1100993 eeh_ops->read_config(pdn, i * 4, 4, &edev->config_space[i]);
Gavin Shanbf898ec2013-11-12 14:49:21 +0800994
995 /*
996 * For PCI bridges including root port, we need enable bus
997 * master explicitly. Otherwise, it can't fetch IODA table
998 * entries correctly. So we cache the bit in advance so that
999 * we can restore it after reset, either PHB range or PE range.
1000 */
1001 if (edev->mode & EEH_DEV_BRIDGE)
1002 edev->config_space[1] |= PCI_COMMAND_MASTER;
Linas Vepstas8b553f32005-11-03 18:50:17 -06001003}
1004
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001005/**
Gavin Shanaa1e6372012-02-27 20:03:53 +00001006 * eeh_ops_register - Register platform dependent EEH operations
1007 * @ops: platform dependent EEH operations
1008 *
1009 * Register the platform dependent EEH operation callback
1010 * functions. The platform should call this function before
1011 * any other EEH operations.
1012 */
1013int __init eeh_ops_register(struct eeh_ops *ops)
1014{
1015 if (!ops->name) {
Gavin Shan0dae2742014-07-17 14:41:41 +10001016 pr_warn("%s: Invalid EEH ops name for %p\n",
Gavin Shanaa1e6372012-02-27 20:03:53 +00001017 __func__, ops);
1018 return -EINVAL;
1019 }
1020
1021 if (eeh_ops && eeh_ops != ops) {
Gavin Shan0dae2742014-07-17 14:41:41 +10001022 pr_warn("%s: EEH ops of platform %s already existing (%s)\n",
Gavin Shanaa1e6372012-02-27 20:03:53 +00001023 __func__, eeh_ops->name, ops->name);
1024 return -EEXIST;
1025 }
1026
1027 eeh_ops = ops;
1028
1029 return 0;
1030}
1031
1032/**
1033 * eeh_ops_unregister - Unreigster platform dependent EEH operations
1034 * @name: name of EEH platform operations
1035 *
1036 * Unregister the platform dependent EEH operation callback
1037 * functions.
1038 */
1039int __exit eeh_ops_unregister(const char *name)
1040{
1041 if (!name || !strlen(name)) {
Gavin Shan0dae2742014-07-17 14:41:41 +10001042 pr_warn("%s: Invalid EEH ops name\n",
Gavin Shanaa1e6372012-02-27 20:03:53 +00001043 __func__);
1044 return -EINVAL;
1045 }
1046
1047 if (eeh_ops && !strcmp(eeh_ops->name, name)) {
1048 eeh_ops = NULL;
1049 return 0;
1050 }
1051
1052 return -EEXIST;
1053}
1054
Gavin Shan66f9af832014-02-12 15:24:56 +08001055static int eeh_reboot_notifier(struct notifier_block *nb,
1056 unsigned long action, void *unused)
1057{
Gavin Shan05b17212014-07-17 14:41:38 +10001058 eeh_clear_flag(EEH_ENABLED);
Gavin Shan66f9af832014-02-12 15:24:56 +08001059 return NOTIFY_DONE;
1060}
1061
1062static struct notifier_block eeh_reboot_nb = {
1063 .notifier_call = eeh_reboot_notifier,
1064};
1065
Benjamin Herrenschmidtb9fde582017-09-07 16:35:44 +10001066void eeh_probe_devices(void)
1067{
1068 struct pci_controller *hose, *tmp;
1069 struct pci_dn *pdn;
1070
1071 /* Enable EEH for all adapters */
1072 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
1073 pdn = hose->pci_data;
1074 traverse_pci_dn(pdn, eeh_ops->probe, NULL);
1075 }
Sam Bobroffbffc0172018-09-12 11:23:23 +10001076 if (eeh_enabled())
1077 pr_info("EEH: PCI Enhanced I/O Error Handling Enabled\n");
1078 else
1079 pr_info("EEH: No capable adapters found\n");
1080
Benjamin Herrenschmidtb9fde582017-09-07 16:35:44 +10001081}
1082
Gavin Shanaa1e6372012-02-27 20:03:53 +00001083/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001084 * eeh_init - EEH initialization
1085 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 * Initialize EEH by trying to enable it for all of the adapters in the system.
1087 * As a side effect we can determine here if eeh is supported at all.
1088 * Note that we leave EEH on so failed config cycles won't cause a machine
1089 * check. If a user turns off EEH for a particular adapter they are really
1090 * telling Linux to ignore errors. Some hardware (e.g. POWER5) won't
1091 * grant access to a slot if EEH isn't enabled, and so we always enable
1092 * EEH for all slots/all devices.
1093 *
1094 * The eeh-force-off option disables EEH checking globally, for all slots.
1095 * Even if force-off is set, the EEH hardware is still enabled, so that
1096 * newer systems can boot.
1097 */
Benjamin Herrenschmidtb9fde582017-09-07 16:35:44 +10001098static int eeh_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
Gavin Shan1a5c2e62012-03-20 21:30:29 +00001100 struct pci_controller *hose, *tmp;
Gavin Shan51fb5f52013-06-20 13:20:56 +08001101 int ret = 0;
1102
Gavin Shan66f9af832014-02-12 15:24:56 +08001103 /* Register reboot notifier */
1104 ret = register_reboot_notifier(&eeh_reboot_nb);
1105 if (ret) {
1106 pr_warn("%s: Failed to register notifier (%d)\n",
1107 __func__, ret);
1108 return ret;
1109 }
1110
Gavin Shane2af1552012-02-27 20:03:54 +00001111 /* call platform initialization function */
1112 if (!eeh_ops) {
Gavin Shan0dae2742014-07-17 14:41:41 +10001113 pr_warn("%s: Platform EEH operation not found\n",
Gavin Shane2af1552012-02-27 20:03:54 +00001114 __func__);
Gavin Shan35e5cfe2012-09-07 22:44:02 +00001115 return -EEXIST;
Greg Kurz221195f2014-11-25 17:10:06 +01001116 } else if ((ret = eeh_ops->init()))
Gavin Shan35e5cfe2012-09-07 22:44:02 +00001117 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Benjamin Herrenschmidt3e77ade2017-09-07 16:35:40 +10001119 /* Initialize PHB PEs */
1120 list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
1121 eeh_dev_phb_init_dynamic(hose);
1122
Sam Bobroff685a0bc2019-08-16 14:48:08 +10001123 eeh_addr_cache_init();
1124
Gavin Shanc8608552013-06-20 13:21:00 +08001125 /* Initialize EEH event */
Sam Bobroffbffc0172018-09-12 11:23:23 +10001126 return eeh_event_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
1128
Gavin Shan35e5cfe2012-09-07 22:44:02 +00001129core_initcall_sync(eeh_init);
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131/**
Gavin Shanc6406d82015-03-17 16:15:08 +11001132 * eeh_add_device_early - Enable EEH for the indicated device node
Gavin Shanff57b452015-03-17 16:15:06 +11001133 * @pdn: PCI device node for which to set up EEH
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 *
1135 * This routine must be used to perform EEH initialization for PCI
1136 * devices that were added after system boot (e.g. hotplug, dlpar).
1137 * This routine must be called before any i/o is performed to the
1138 * adapter (inluding any config-space i/o).
1139 * Whether this actually enables EEH or not for this device depends
1140 * on the CEC architecture, type of the device, on earlier boot
1141 * command-line arguments & etc.
1142 */
Gavin Shanff57b452015-03-17 16:15:06 +11001143void eeh_add_device_early(struct pci_dn *pdn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
Alexey Kardashevskiy69672bd2017-08-29 17:34:01 +10001145 struct pci_controller *phb = pdn ? pdn->phb : NULL;
Gavin Shanff57b452015-03-17 16:15:06 +11001146 struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Guilherme G. Piccolic2078d92016-04-11 16:17:22 -03001148 if (!edev)
Gavin Shan26a74852013-06-20 13:20:59 +08001149 return;
1150
Gavin Shand91dafc2015-05-01 09:22:15 +10001151 if (!eeh_has_flag(EEH_PROBE_MODE_DEVTREE))
1152 return;
1153
Linas Vepstasf751f842005-11-03 18:54:23 -06001154 /* USB Bus children of PCI devices will not have BUID's */
Gavin Shanff57b452015-03-17 16:15:06 +11001155 if (NULL == phb ||
1156 (eeh_has_flag(EEH_PROBE_MODE_DEVTREE) && 0 == phb->buid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Gavin Shanff57b452015-03-17 16:15:06 +11001159 eeh_ops->probe(pdn, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001162/**
1163 * eeh_add_device_tree_early - Enable EEH for the indicated device
Gavin Shanff57b452015-03-17 16:15:06 +11001164 * @pdn: PCI device node
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001165 *
1166 * This routine must be used to perform EEH initialization for the
1167 * indicated PCI device that was added after system boot (e.g.
1168 * hotplug, dlpar).
1169 */
Gavin Shanff57b452015-03-17 16:15:06 +11001170void eeh_add_device_tree_early(struct pci_dn *pdn)
Linas Vepstase2a296e2005-11-03 18:51:31 -06001171{
Gavin Shanff57b452015-03-17 16:15:06 +11001172 struct pci_dn *n;
Stephen Rothwellacaa6172007-12-21 15:52:07 +11001173
Gavin Shanff57b452015-03-17 16:15:06 +11001174 if (!pdn)
1175 return;
1176
1177 list_for_each_entry(n, &pdn->child_list, list)
1178 eeh_add_device_tree_early(n);
1179 eeh_add_device_early(pdn);
Linas Vepstase2a296e2005-11-03 18:51:31 -06001180}
1181EXPORT_SYMBOL_GPL(eeh_add_device_tree_early);
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001184 * eeh_add_device_late - Perform EEH initialization for the indicated pci device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 * @dev: pci device for which to set up EEH
1186 *
1187 * This routine must be used to complete EEH initialization for PCI
1188 * devices that were added after system boot (e.g. hotplug, dlpar).
1189 */
Gavin Shanf2856492013-07-24 10:24:52 +08001190void eeh_add_device_late(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
Gavin Shanc6406d82015-03-17 16:15:08 +11001192 struct pci_dn *pdn;
Gavin Shanf631acd2012-02-27 20:04:07 +00001193 struct eeh_dev *edev;
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001194
Gavin Shan2ec5a0a2014-02-12 15:24:55 +08001195 if (!dev || !eeh_enabled())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return;
1197
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001198 pr_debug("EEH: Adding device %s\n", pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Gavin Shanc6406d82015-03-17 16:15:08 +11001200 pdn = pci_get_pdn_by_devfn(dev->bus, dev->devfn);
1201 edev = pdn_to_eeh_dev(pdn);
Gavin Shanf631acd2012-02-27 20:04:07 +00001202 if (edev->pdev == dev) {
Sam Bobroff617082a2019-08-16 14:48:07 +10001203 pr_debug("EEH: Device %s already referenced!\n", pci_name(dev));
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001204 return;
1205 }
Gavin Shanf5c57712013-07-24 10:24:58 +08001206
1207 /*
1208 * The EEH cache might not be removed correctly because of
1209 * unbalanced kref to the device during unplug time, which
1210 * relies on pcibios_release_device(). So we have to remove
1211 * that here explicitly.
1212 */
1213 if (edev->pdev) {
1214 eeh_rmv_from_parent_pe(edev);
1215 eeh_addr_cache_rmv_dev(edev->pdev);
1216 eeh_sysfs_remove_device(edev->pdev);
Gavin Shanab55d212013-07-24 10:25:01 +08001217 edev->mode &= ~EEH_DEV_SYSFS;
Gavin Shanf5c57712013-07-24 10:24:58 +08001218
Gavin Shanf26c7a02014-01-12 14:13:45 +08001219 /*
1220 * We definitely should have the PCI device removed
1221 * though it wasn't correctly. So we needn't call
1222 * into error handler afterwards.
1223 */
1224 edev->mode |= EEH_DEV_NO_HANDLER;
1225
Gavin Shanf5c57712013-07-24 10:24:58 +08001226 edev->pdev = NULL;
1227 dev->dev.archdata.edev = NULL;
1228 }
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001229
Daniel Axtense642d112015-08-14 16:03:19 +10001230 if (eeh_has_flag(EEH_PROBE_MODE_DEV))
1231 eeh_ops->probe(pdn, NULL);
1232
Gavin Shanf631acd2012-02-27 20:04:07 +00001233 edev->pdev = dev;
1234 dev->dev.archdata.edev = edev;
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001235
Gavin Shan3ab96a02012-09-07 22:44:23 +00001236 eeh_addr_cache_insert_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
Nathan Fontenot794e0852006-03-31 12:04:52 -06001238
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001239/**
1240 * eeh_add_device_tree_late - Perform EEH initialization for the indicated PCI bus
1241 * @bus: PCI bus
1242 *
1243 * This routine must be used to perform EEH initialization for PCI
1244 * devices which are attached to the indicated PCI bus. The PCI bus
1245 * is added after system boot through hotplug or dlpar.
1246 */
Nathan Fontenot794e0852006-03-31 12:04:52 -06001247void eeh_add_device_tree_late(struct pci_bus *bus)
1248{
1249 struct pci_dev *dev;
1250
1251 list_for_each_entry(dev, &bus->devices, bus_list) {
Gavin Shana84f2732013-06-20 13:20:51 +08001252 eeh_add_device_late(dev);
1253 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
1254 struct pci_bus *subbus = dev->subordinate;
1255 if (subbus)
1256 eeh_add_device_tree_late(subbus);
1257 }
Nathan Fontenot794e0852006-03-31 12:04:52 -06001258 }
1259}
1260EXPORT_SYMBOL_GPL(eeh_add_device_tree_late);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262/**
Thadeu Lima de Souza Cascardo6a040ce2012-12-28 09:13:19 +00001263 * eeh_add_sysfs_files - Add EEH sysfs files for the indicated PCI bus
1264 * @bus: PCI bus
1265 *
1266 * This routine must be used to add EEH sysfs files for PCI
1267 * devices which are attached to the indicated PCI bus. The PCI bus
1268 * is added after system boot through hotplug or dlpar.
1269 */
1270void eeh_add_sysfs_files(struct pci_bus *bus)
1271{
1272 struct pci_dev *dev;
1273
1274 list_for_each_entry(dev, &bus->devices, bus_list) {
1275 eeh_sysfs_add_device(dev);
1276 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
1277 struct pci_bus *subbus = dev->subordinate;
1278 if (subbus)
1279 eeh_add_sysfs_files(subbus);
1280 }
1281 }
1282}
1283EXPORT_SYMBOL_GPL(eeh_add_sysfs_files);
1284
1285/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001286 * eeh_remove_device - Undo EEH setup for the indicated pci device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 * @dev: pci device to be removed
1288 *
Nathan Fontenot794e0852006-03-31 12:04:52 -06001289 * This routine should be called when a device is removed from
1290 * a running system (e.g. by hotplug or dlpar). It unregisters
1291 * the PCI device from the EEH subsystem. I/O errors affecting
1292 * this device will no longer be detected after this call; thus,
1293 * i/o errors affecting this slot may leave this device unusable.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 */
Gavin Shan807a8272013-07-24 10:24:55 +08001295void eeh_remove_device(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
Gavin Shanf631acd2012-02-27 20:04:07 +00001297 struct eeh_dev *edev;
1298
Gavin Shan2ec5a0a2014-02-12 15:24:55 +08001299 if (!dev || !eeh_enabled())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 return;
Gavin Shanf631acd2012-02-27 20:04:07 +00001301 edev = pci_dev_to_eeh_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 /* Unregister the device with the EEH/PCI address search system */
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001304 pr_debug("EEH: Removing device %s\n", pci_name(dev));
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001305
Gavin Shanf5c57712013-07-24 10:24:58 +08001306 if (!edev || !edev->pdev || !edev->pe) {
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001307 pr_debug("EEH: Not referenced !\n");
1308 return;
Linas Vepstasb055a9e2006-04-06 15:41:41 -05001309 }
Gavin Shanf5c57712013-07-24 10:24:58 +08001310
1311 /*
1312 * During the hotplug for EEH error recovery, we need the EEH
1313 * device attached to the parent PE in order for BAR restore
1314 * a bit later. So we keep it for BAR restore and remove it
1315 * from the parent PE during the BAR resotre.
1316 */
Gavin Shanf631acd2012-02-27 20:04:07 +00001317 edev->pdev = NULL;
Wei Yang67086e32016-03-04 10:53:11 +11001318
1319 /*
1320 * The flag "in_error" is used to trace EEH devices for VFs
1321 * in error state or not. It's set in eeh_report_error(). If
1322 * it's not set, eeh_report_{reset,resume}() won't be called
1323 * for the VF EEH device.
1324 */
1325 edev->in_error = false;
Gavin Shanf631acd2012-02-27 20:04:07 +00001326 dev->dev.archdata.edev = NULL;
Gavin Shanf5c57712013-07-24 10:24:58 +08001327 if (!(edev->pe->state & EEH_PE_KEEP))
1328 eeh_rmv_from_parent_pe(edev);
1329 else
1330 edev->mode |= EEH_DEV_DISCONNECTED;
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001331
Gavin Shanf26c7a02014-01-12 14:13:45 +08001332 /*
1333 * We're removing from the PCI subsystem, that means
1334 * the PCI device driver can't support EEH or not
1335 * well. So we rely on hotplug completely to do recovery
1336 * for the specific PCI device.
1337 */
1338 edev->mode |= EEH_DEV_NO_HANDLER;
1339
Gavin Shan3ab96a02012-09-07 22:44:23 +00001340 eeh_addr_cache_rmv_dev(dev);
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001341 eeh_sysfs_remove_device(dev);
Gavin Shanab55d212013-07-24 10:25:01 +08001342 edev->mode &= ~EEH_DEV_SYSFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Sam Bobroff188fdea2018-11-29 14:16:38 +11001345int eeh_unfreeze_pe(struct eeh_pe *pe)
Gavin Shan4eeeff02014-09-30 12:39:01 +10001346{
1347 int ret;
1348
1349 ret = eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
1350 if (ret) {
1351 pr_warn("%s: Failure %d enabling IO on PHB#%x-PE#%x\n",
1352 __func__, ret, pe->phb->global_number, pe->addr);
1353 return ret;
1354 }
1355
1356 ret = eeh_pci_enable(pe, EEH_OPT_THAW_DMA);
1357 if (ret) {
1358 pr_warn("%s: Failure %d enabling DMA on PHB#%x-PE#%x\n",
1359 __func__, ret, pe->phb->global_number, pe->addr);
1360 return ret;
1361 }
1362
Gavin Shan4eeeff02014-09-30 12:39:01 +10001363 return ret;
1364}
1365
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001366
1367static struct pci_device_id eeh_reset_ids[] = {
1368 { PCI_DEVICE(0x19a2, 0x0710) }, /* Emulex, BE */
1369 { PCI_DEVICE(0x10df, 0xe220) }, /* Emulex, Lancer */
Gavin Shanb1d76a72014-11-14 10:47:30 +11001370 { PCI_DEVICE(0x14e4, 0x1657) }, /* Broadcom BCM5719 */
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001371 { 0 }
1372};
1373
1374static int eeh_pe_change_owner(struct eeh_pe *pe)
1375{
1376 struct eeh_dev *edev, *tmp;
1377 struct pci_dev *pdev;
1378 struct pci_device_id *id;
Sam Bobroff34a286a2018-03-19 13:49:23 +11001379 int ret;
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001380
1381 /* Check PE state */
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001382 ret = eeh_ops->get_state(pe, NULL);
1383 if (ret < 0 || ret == EEH_STATE_NOT_SUPPORT)
1384 return 0;
1385
1386 /* Unfrozen PE, nothing to do */
Sam Bobroff34a286a2018-03-19 13:49:23 +11001387 if (eeh_state_active(ret))
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001388 return 0;
1389
1390 /* Frozen PE, check if it needs PE level reset */
1391 eeh_pe_for_each_dev(pe, edev, tmp) {
1392 pdev = eeh_dev_to_pci_dev(edev);
1393 if (!pdev)
1394 continue;
1395
1396 for (id = &eeh_reset_ids[0]; id->vendor != 0; id++) {
1397 if (id->vendor != PCI_ANY_ID &&
1398 id->vendor != pdev->vendor)
1399 continue;
1400 if (id->device != PCI_ANY_ID &&
1401 id->device != pdev->device)
1402 continue;
1403 if (id->subvendor != PCI_ANY_ID &&
1404 id->subvendor != pdev->subsystem_vendor)
1405 continue;
1406 if (id->subdevice != PCI_ANY_ID &&
1407 id->subdevice != pdev->subsystem_device)
1408 continue;
1409
Gavin Shand6d63d72016-04-27 11:14:53 +10001410 return eeh_pe_reset_and_recover(pe);
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001411 }
1412 }
1413
Sam Bobroff188fdea2018-11-29 14:16:38 +11001414 ret = eeh_unfreeze_pe(pe);
1415 if (!ret)
Sam Bobroff9ed5ca62018-11-29 14:16:39 +11001416 eeh_pe_state_clear(pe, EEH_PE_ISOLATED, true);
Sam Bobroff188fdea2018-11-29 14:16:38 +11001417 return ret;
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001418}
1419
Gavin Shan212d16c2014-06-10 11:41:56 +10001420/**
1421 * eeh_dev_open - Increase count of pass through devices for PE
1422 * @pdev: PCI device
1423 *
1424 * Increase count of passed through devices for the indicated
1425 * PE. In the result, the EEH errors detected on the PE won't be
1426 * reported. The PE owner will be responsible for detection
1427 * and recovery.
1428 */
1429int eeh_dev_open(struct pci_dev *pdev)
1430{
1431 struct eeh_dev *edev;
Gavin Shan404079c2014-09-30 12:38:54 +10001432 int ret = -ENODEV;
Gavin Shan212d16c2014-06-10 11:41:56 +10001433
1434 mutex_lock(&eeh_dev_mutex);
1435
1436 /* No PCI device ? */
1437 if (!pdev)
1438 goto out;
1439
1440 /* No EEH device or PE ? */
1441 edev = pci_dev_to_eeh_dev(pdev);
1442 if (!edev || !edev->pe)
1443 goto out;
1444
Gavin Shan404079c2014-09-30 12:38:54 +10001445 /*
1446 * The PE might have been put into frozen state, but we
1447 * didn't detect that yet. The passed through PCI devices
1448 * in frozen PE won't work properly. Clear the frozen state
1449 * in advance.
1450 */
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001451 ret = eeh_pe_change_owner(edev->pe);
Gavin Shan4eeeff02014-09-30 12:39:01 +10001452 if (ret)
1453 goto out;
Gavin Shan404079c2014-09-30 12:38:54 +10001454
Gavin Shan212d16c2014-06-10 11:41:56 +10001455 /* Increase PE's pass through count */
1456 atomic_inc(&edev->pe->pass_dev_cnt);
1457 mutex_unlock(&eeh_dev_mutex);
1458
1459 return 0;
1460out:
1461 mutex_unlock(&eeh_dev_mutex);
Gavin Shan404079c2014-09-30 12:38:54 +10001462 return ret;
Gavin Shan212d16c2014-06-10 11:41:56 +10001463}
1464EXPORT_SYMBOL_GPL(eeh_dev_open);
1465
1466/**
1467 * eeh_dev_release - Decrease count of pass through devices for PE
1468 * @pdev: PCI device
1469 *
1470 * Decrease count of pass through devices for the indicated PE. If
1471 * there is no passed through device in PE, the EEH errors detected
1472 * on the PE will be reported and handled as usual.
1473 */
1474void eeh_dev_release(struct pci_dev *pdev)
1475{
1476 struct eeh_dev *edev;
1477
1478 mutex_lock(&eeh_dev_mutex);
1479
1480 /* No PCI device ? */
1481 if (!pdev)
1482 goto out;
1483
1484 /* No EEH device ? */
1485 edev = pci_dev_to_eeh_dev(pdev);
1486 if (!edev || !edev->pe || !eeh_pe_passed(edev->pe))
1487 goto out;
1488
1489 /* Decrease PE's pass through count */
Gavin Shan54f9a642015-08-27 15:58:27 +10001490 WARN_ON(atomic_dec_if_positive(&edev->pe->pass_dev_cnt) < 0);
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001491 eeh_pe_change_owner(edev->pe);
Gavin Shan212d16c2014-06-10 11:41:56 +10001492out:
1493 mutex_unlock(&eeh_dev_mutex);
1494}
1495EXPORT_SYMBOL(eeh_dev_release);
1496
Benjamin Herrenschmidt2194dc22014-08-05 18:52:59 +10001497#ifdef CONFIG_IOMMU_API
1498
Gavin Shana3032ca2014-07-15 17:00:56 +10001499static int dev_has_iommu_table(struct device *dev, void *data)
1500{
1501 struct pci_dev *pdev = to_pci_dev(dev);
1502 struct pci_dev **ppdev = data;
Gavin Shana3032ca2014-07-15 17:00:56 +10001503
1504 if (!dev)
1505 return 0;
1506
Joerg Roedelbf8763d2018-11-30 14:23:19 +01001507 if (device_iommu_mapped(dev)) {
Gavin Shana3032ca2014-07-15 17:00:56 +10001508 *ppdev = pdev;
1509 return 1;
1510 }
1511
1512 return 0;
1513}
1514
Gavin Shan212d16c2014-06-10 11:41:56 +10001515/**
1516 * eeh_iommu_group_to_pe - Convert IOMMU group to EEH PE
1517 * @group: IOMMU group
1518 *
1519 * The routine is called to convert IOMMU group to EEH PE.
1520 */
1521struct eeh_pe *eeh_iommu_group_to_pe(struct iommu_group *group)
1522{
Gavin Shan212d16c2014-06-10 11:41:56 +10001523 struct pci_dev *pdev = NULL;
1524 struct eeh_dev *edev;
Gavin Shana3032ca2014-07-15 17:00:56 +10001525 int ret;
Gavin Shan212d16c2014-06-10 11:41:56 +10001526
1527 /* No IOMMU group ? */
1528 if (!group)
1529 return NULL;
1530
Gavin Shana3032ca2014-07-15 17:00:56 +10001531 ret = iommu_group_for_each_dev(group, &pdev, dev_has_iommu_table);
1532 if (!ret || !pdev)
Gavin Shan212d16c2014-06-10 11:41:56 +10001533 return NULL;
1534
1535 /* No EEH device or PE ? */
1536 edev = pci_dev_to_eeh_dev(pdev);
1537 if (!edev || !edev->pe)
1538 return NULL;
1539
1540 return edev->pe;
1541}
Gavin Shan537e5402014-08-07 12:47:16 +10001542EXPORT_SYMBOL_GPL(eeh_iommu_group_to_pe);
Gavin Shan212d16c2014-06-10 11:41:56 +10001543
Benjamin Herrenschmidt2194dc22014-08-05 18:52:59 +10001544#endif /* CONFIG_IOMMU_API */
1545
Gavin Shan212d16c2014-06-10 11:41:56 +10001546/**
1547 * eeh_pe_set_option - Set options for the indicated PE
1548 * @pe: EEH PE
1549 * @option: requested option
1550 *
1551 * The routine is called to enable or disable EEH functionality
1552 * on the indicated PE, to enable IO or DMA for the frozen PE.
1553 */
1554int eeh_pe_set_option(struct eeh_pe *pe, int option)
1555{
1556 int ret = 0;
1557
1558 /* Invalid PE ? */
1559 if (!pe)
1560 return -ENODEV;
1561
1562 /*
1563 * EEH functionality could possibly be disabled, just
1564 * return error for the case. And the EEH functinality
1565 * isn't expected to be disabled on one specific PE.
1566 */
1567 switch (option) {
1568 case EEH_OPT_ENABLE:
Gavin Shan4eeeff02014-09-30 12:39:01 +10001569 if (eeh_enabled()) {
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001570 ret = eeh_pe_change_owner(pe);
Gavin Shan212d16c2014-06-10 11:41:56 +10001571 break;
Gavin Shan4eeeff02014-09-30 12:39:01 +10001572 }
Gavin Shan212d16c2014-06-10 11:41:56 +10001573 ret = -EIO;
1574 break;
1575 case EEH_OPT_DISABLE:
1576 break;
1577 case EEH_OPT_THAW_MMIO:
1578 case EEH_OPT_THAW_DMA:
Gavin Shande5a6622016-09-28 14:34:53 +10001579 case EEH_OPT_FREEZE_PE:
Gavin Shan212d16c2014-06-10 11:41:56 +10001580 if (!eeh_ops || !eeh_ops->set_option) {
1581 ret = -ENOENT;
1582 break;
1583 }
1584
Gavin Shan4eeeff02014-09-30 12:39:01 +10001585 ret = eeh_pci_enable(pe, option);
Gavin Shan212d16c2014-06-10 11:41:56 +10001586 break;
1587 default:
1588 pr_debug("%s: Option %d out of range (%d, %d)\n",
1589 __func__, option, EEH_OPT_DISABLE, EEH_OPT_THAW_DMA);
1590 ret = -EINVAL;
1591 }
1592
1593 return ret;
1594}
1595EXPORT_SYMBOL_GPL(eeh_pe_set_option);
1596
1597/**
1598 * eeh_pe_get_state - Retrieve PE's state
1599 * @pe: EEH PE
1600 *
1601 * Retrieve the PE's state, which includes 3 aspects: enabled
1602 * DMA, enabled IO and asserted reset.
1603 */
1604int eeh_pe_get_state(struct eeh_pe *pe)
1605{
1606 int result, ret = 0;
1607 bool rst_active, dma_en, mmio_en;
1608
1609 /* Existing PE ? */
1610 if (!pe)
1611 return -ENODEV;
1612
1613 if (!eeh_ops || !eeh_ops->get_state)
1614 return -ENOENT;
1615
Gavin Shaneca036e2016-03-04 10:53:14 +11001616 /*
1617 * If the parent PE is owned by the host kernel and is undergoing
1618 * error recovery, we should return the PE state as temporarily
1619 * unavailable so that the error recovery on the guest is suspended
1620 * until the recovery completes on the host.
1621 */
1622 if (pe->parent &&
1623 !(pe->state & EEH_PE_REMOVED) &&
1624 (pe->parent->state & (EEH_PE_ISOLATED | EEH_PE_RECOVERING)))
1625 return EEH_PE_STATE_UNAVAIL;
1626
Gavin Shan212d16c2014-06-10 11:41:56 +10001627 result = eeh_ops->get_state(pe, NULL);
1628 rst_active = !!(result & EEH_STATE_RESET_ACTIVE);
1629 dma_en = !!(result & EEH_STATE_DMA_ENABLED);
1630 mmio_en = !!(result & EEH_STATE_MMIO_ENABLED);
1631
1632 if (rst_active)
1633 ret = EEH_PE_STATE_RESET;
1634 else if (dma_en && mmio_en)
1635 ret = EEH_PE_STATE_NORMAL;
1636 else if (!dma_en && !mmio_en)
1637 ret = EEH_PE_STATE_STOPPED_IO_DMA;
1638 else if (!dma_en && mmio_en)
1639 ret = EEH_PE_STATE_STOPPED_DMA;
1640 else
1641 ret = EEH_PE_STATE_UNAVAIL;
1642
1643 return ret;
1644}
1645EXPORT_SYMBOL_GPL(eeh_pe_get_state);
1646
Sam Bobroff1ef52072018-11-29 14:16:41 +11001647static int eeh_pe_reenable_devices(struct eeh_pe *pe, bool include_passed)
Gavin Shan316233f2014-09-30 12:38:53 +10001648{
1649 struct eeh_dev *edev, *tmp;
1650 struct pci_dev *pdev;
1651 int ret = 0;
1652
Gavin Shan316233f2014-09-30 12:38:53 +10001653 eeh_pe_restore_bars(pe);
1654
1655 /*
1656 * Reenable PCI devices as the devices passed
1657 * through are always enabled before the reset.
1658 */
1659 eeh_pe_for_each_dev(pe, edev, tmp) {
1660 pdev = eeh_dev_to_pci_dev(edev);
1661 if (!pdev)
1662 continue;
1663
1664 ret = pci_reenable_device(pdev);
1665 if (ret) {
1666 pr_warn("%s: Failure %d reenabling %s\n",
1667 __func__, ret, pci_name(pdev));
1668 return ret;
1669 }
1670 }
1671
1672 /* The PE is still in frozen state */
Sam Bobroff1ef52072018-11-29 14:16:41 +11001673 if (include_passed || !eeh_pe_passed(pe)) {
1674 ret = eeh_unfreeze_pe(pe);
1675 } else
1676 pr_info("EEH: Note: Leaving passthrough PHB#%x-PE#%x frozen.\n",
1677 pe->phb->global_number, pe->addr);
Sam Bobroff188fdea2018-11-29 14:16:38 +11001678 if (!ret)
Sam Bobroff1ef52072018-11-29 14:16:41 +11001679 eeh_pe_state_clear(pe, EEH_PE_ISOLATED, include_passed);
Sam Bobroff188fdea2018-11-29 14:16:38 +11001680 return ret;
Gavin Shan316233f2014-09-30 12:38:53 +10001681}
1682
Russell Currey6654c932016-11-17 16:07:47 +11001683
Gavin Shan212d16c2014-06-10 11:41:56 +10001684/**
1685 * eeh_pe_reset - Issue PE reset according to specified type
1686 * @pe: EEH PE
1687 * @option: reset type
1688 *
1689 * The routine is called to reset the specified PE with the
1690 * indicated type, either fundamental reset or hot reset.
1691 * PE reset is the most important part for error recovery.
1692 */
Sam Bobroff1ef52072018-11-29 14:16:41 +11001693int eeh_pe_reset(struct eeh_pe *pe, int option, bool include_passed)
Gavin Shan212d16c2014-06-10 11:41:56 +10001694{
1695 int ret = 0;
1696
1697 /* Invalid PE ? */
1698 if (!pe)
1699 return -ENODEV;
1700
1701 if (!eeh_ops || !eeh_ops->set_option || !eeh_ops->reset)
1702 return -ENOENT;
1703
1704 switch (option) {
1705 case EEH_RESET_DEACTIVATE:
1706 ret = eeh_ops->reset(pe, option);
Sam Bobroff1ef52072018-11-29 14:16:41 +11001707 eeh_pe_state_clear(pe, EEH_PE_CFG_BLOCKED, include_passed);
Gavin Shan212d16c2014-06-10 11:41:56 +10001708 if (ret)
1709 break;
1710
Sam Bobroff1ef52072018-11-29 14:16:41 +11001711 ret = eeh_pe_reenable_devices(pe, include_passed);
Gavin Shan212d16c2014-06-10 11:41:56 +10001712 break;
1713 case EEH_RESET_HOT:
1714 case EEH_RESET_FUNDAMENTAL:
Gavin Shan0d5ee522014-09-30 12:38:52 +10001715 /*
1716 * Proactively freeze the PE to drop all MMIO access
1717 * during reset, which should be banned as it's always
1718 * cause recursive EEH error.
1719 */
1720 eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);
1721
Gavin Shan8a6b3712014-10-01 17:07:50 +10001722 eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
Gavin Shan212d16c2014-06-10 11:41:56 +10001723 ret = eeh_ops->reset(pe, option);
1724 break;
1725 default:
1726 pr_debug("%s: Unsupported option %d\n",
1727 __func__, option);
1728 ret = -EINVAL;
1729 }
1730
1731 return ret;
1732}
1733EXPORT_SYMBOL_GPL(eeh_pe_reset);
1734
1735/**
1736 * eeh_pe_configure - Configure PCI bridges after PE reset
1737 * @pe: EEH PE
1738 *
1739 * The routine is called to restore the PCI config space for
1740 * those PCI devices, especially PCI bridges affected by PE
1741 * reset issued previously.
1742 */
1743int eeh_pe_configure(struct eeh_pe *pe)
1744{
1745 int ret = 0;
1746
1747 /* Invalid PE ? */
1748 if (!pe)
1749 return -ENODEV;
1750
Gavin Shan212d16c2014-06-10 11:41:56 +10001751 return ret;
1752}
1753EXPORT_SYMBOL_GPL(eeh_pe_configure);
1754
Gavin Shanec33d362015-03-26 16:42:08 +11001755/**
1756 * eeh_pe_inject_err - Injecting the specified PCI error to the indicated PE
1757 * @pe: the indicated PE
1758 * @type: error type
1759 * @function: error function
1760 * @addr: address
1761 * @mask: address mask
1762 *
1763 * The routine is called to inject the specified PCI error, which
1764 * is determined by @type and @function, to the indicated PE for
1765 * testing purpose.
1766 */
1767int eeh_pe_inject_err(struct eeh_pe *pe, int type, int func,
1768 unsigned long addr, unsigned long mask)
1769{
1770 /* Invalid PE ? */
1771 if (!pe)
1772 return -ENODEV;
1773
1774 /* Unsupported operation ? */
1775 if (!eeh_ops || !eeh_ops->err_inject)
1776 return -ENOENT;
1777
1778 /* Check on PCI error type */
1779 if (type != EEH_ERR_TYPE_32 && type != EEH_ERR_TYPE_64)
1780 return -EINVAL;
1781
1782 /* Check on PCI error function */
1783 if (func < EEH_ERR_FUNC_MIN || func > EEH_ERR_FUNC_MAX)
1784 return -EINVAL;
1785
1786 return eeh_ops->err_inject(pe, type, func, addr, mask);
1787}
1788EXPORT_SYMBOL_GPL(eeh_pe_inject_err);
1789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790static int proc_eeh_show(struct seq_file *m, void *v)
1791{
Gavin Shan2ec5a0a2014-02-12 15:24:55 +08001792 if (!eeh_enabled()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 seq_printf(m, "EEH Subsystem is globally disabled\n");
Gavin Shane575f8d2012-02-29 15:47:45 +00001794 seq_printf(m, "eeh_total_mmio_ffs=%llu\n", eeh_stats.total_mmio_ffs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 } else {
1796 seq_printf(m, "EEH Subsystem is enabled\n");
Linas Vepstas177bc932005-11-03 18:48:52 -06001797 seq_printf(m,
Gavin Shane575f8d2012-02-29 15:47:45 +00001798 "no device=%llu\n"
1799 "no device node=%llu\n"
1800 "no config address=%llu\n"
1801 "check not wanted=%llu\n"
1802 "eeh_total_mmio_ffs=%llu\n"
1803 "eeh_false_positives=%llu\n"
1804 "eeh_slot_resets=%llu\n",
1805 eeh_stats.no_device,
1806 eeh_stats.no_dn,
1807 eeh_stats.no_cfg_addr,
1808 eeh_stats.ignored_check,
1809 eeh_stats.total_mmio_ffs,
1810 eeh_stats.false_positives,
1811 eeh_stats.slot_resets);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 }
1813
1814 return 0;
1815}
1816
Gavin Shan7f52a5262014-04-24 18:00:18 +10001817#ifdef CONFIG_DEBUG_FS
1818static int eeh_enable_dbgfs_set(void *data, u64 val)
1819{
1820 if (val)
Gavin Shan05b17212014-07-17 14:41:38 +10001821 eeh_clear_flag(EEH_FORCE_DISABLED);
Gavin Shan7f52a5262014-04-24 18:00:18 +10001822 else
Gavin Shan05b17212014-07-17 14:41:38 +10001823 eeh_add_flag(EEH_FORCE_DISABLED);
Gavin Shan7f52a5262014-04-24 18:00:18 +10001824
Gavin Shan7f52a5262014-04-24 18:00:18 +10001825 return 0;
1826}
1827
1828static int eeh_enable_dbgfs_get(void *data, u64 *val)
1829{
1830 if (eeh_enabled())
1831 *val = 0x1ul;
1832 else
1833 *val = 0x0ul;
1834 return 0;
1835}
1836
YueHaibing8c6c9422018-12-20 02:42:51 +00001837DEFINE_DEBUGFS_ATTRIBUTE(eeh_enable_dbgfs_ops, eeh_enable_dbgfs_get,
1838 eeh_enable_dbgfs_set, "0x%llx\n");
Oliver O'Halloran954bd992019-02-15 11:48:17 +11001839
1840static ssize_t eeh_force_recover_write(struct file *filp,
1841 const char __user *user_buf,
1842 size_t count, loff_t *ppos)
1843{
1844 struct pci_controller *hose;
1845 uint32_t phbid, pe_no;
1846 struct eeh_pe *pe;
1847 char buf[20];
1848 int ret;
1849
1850 ret = simple_write_to_buffer(buf, sizeof(buf), ppos, user_buf, count);
1851 if (!ret)
1852 return -EFAULT;
1853
1854 /*
1855 * When PE is NULL the event is a "special" event. Rather than
1856 * recovering a specific PE it forces the EEH core to scan for failed
1857 * PHBs and recovers each. This needs to be done before any device
1858 * recoveries can occur.
1859 */
1860 if (!strncmp(buf, "hwcheck", 7)) {
1861 __eeh_send_failure_event(NULL);
1862 return count;
1863 }
1864
1865 ret = sscanf(buf, "%x:%x", &phbid, &pe_no);
1866 if (ret != 2)
1867 return -EINVAL;
1868
1869 hose = pci_find_controller_for_domain(phbid);
1870 if (!hose)
1871 return -ENODEV;
1872
1873 /* Retrieve PE */
1874 pe = eeh_pe_get(hose, pe_no, 0);
1875 if (!pe)
1876 return -ENODEV;
1877
1878 /*
1879 * We don't do any state checking here since the detection
1880 * process is async to the recovery process. The recovery
1881 * thread *should* not break even if we schedule a recovery
1882 * from an odd state (e.g. PE removed, or recovery of a
1883 * non-isolated PE)
1884 */
1885 __eeh_send_failure_event(pe);
1886
1887 return ret < 0 ? ret : count;
1888}
1889
1890static const struct file_operations eeh_force_recover_fops = {
1891 .open = simple_open,
1892 .llseek = no_llseek,
1893 .write = eeh_force_recover_write,
1894};
Gavin Shan7f52a5262014-04-24 18:00:18 +10001895#endif
1896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897static int __init eeh_init_proc(void)
1898{
Gavin Shan7f52a5262014-04-24 18:00:18 +10001899 if (machine_is(pseries) || machine_is(powernv)) {
Christoph Hellwig3f3942a2018-05-15 15:57:23 +02001900 proc_create_single("powerpc/eeh", 0, NULL, proc_eeh_show);
Gavin Shan7f52a5262014-04-24 18:00:18 +10001901#ifdef CONFIG_DEBUG_FS
YueHaibing8c6c9422018-12-20 02:42:51 +00001902 debugfs_create_file_unsafe("eeh_enable", 0600,
1903 powerpc_debugfs_root, NULL,
1904 &eeh_enable_dbgfs_ops);
Oliver O'Halloran46ee7c32019-02-15 11:48:11 +11001905 debugfs_create_u32("eeh_max_freezes", 0600,
1906 powerpc_debugfs_root, &eeh_max_freezes);
Oliver O'Halloran6b493f62019-02-15 11:48:16 +11001907 debugfs_create_bool("eeh_disable_recovery", 0600,
1908 powerpc_debugfs_root,
1909 &eeh_debugfs_no_recover);
Oliver O'Halloran954bd992019-02-15 11:48:17 +11001910 debugfs_create_file_unsafe("eeh_force_recover", 0600,
1911 powerpc_debugfs_root, NULL,
1912 &eeh_force_recover_fops);
Oliver O'Halloran5ca85ae2019-02-15 11:48:13 +11001913 eeh_cache_debugfs_init();
Gavin Shan7f52a5262014-04-24 18:00:18 +10001914#endif
1915 }
1916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 return 0;
1918}
1919__initcall(eeh_init_proc);