blob: 8336b9016ca9b47f5aa43c4f1a0aaea39d1a9dc0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linas Vepstas3c8c90a2007-05-24 03:28:01 +10002 * Copyright IBM Corporation 2001, 2005, 2006
3 * Copyright Dave Engebretsen & Todd Inglett 2001
4 * Copyright Linas Vepstas 2005, 2006
Gavin Shancb3bc9d2012-02-27 20:03:51 +00005 * Copyright 2001-2012 IBM Corporation.
Linas Vepstas69376502005-11-03 18:47:50 -06006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
Linas Vepstas69376502005-11-03 18:47:50 -060011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Linas Vepstas69376502005-11-03 18:47:50 -060016 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Linas Vepstas3c8c90a2007-05-24 03:28:01 +100020 *
21 * Please address comments and feedback to Linas Vepstas <linas@austin.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
23
Linas Vepstas6dee3fb2005-11-03 18:50:10 -060024#include <linux/delay.h>
Gavin Shan7f52a522014-04-24 18:00:18 +100025#include <linux/debugfs.h>
Gavin Shancb3bc9d2012-02-27 20:03:51 +000026#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/init.h>
28#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/pci.h>
Gavin Shana3032ca2014-07-15 17:00:56 +100030#include <linux/iommu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/proc_fs.h>
32#include <linux/rbtree.h>
Gavin Shan66f9af832014-02-12 15:24:56 +080033#include <linux/reboot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/seq_file.h>
35#include <linux/spinlock.h>
Paul Gortmaker66b15db2011-05-27 10:46:24 -040036#include <linux/export.h>
Stephen Rothwellacaa6172007-12-21 15:52:07 +110037#include <linux/of.h>
38
Arun Sharma600634972011-07-26 16:09:06 -070039#include <linux/atomic.h>
Gavin Shan1e54b932014-05-05 12:09:05 +100040#include <asm/debug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/eeh.h>
Linas Vepstas172ca922005-11-03 18:50:04 -060042#include <asm/eeh_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/io.h>
Gavin Shan212d16c2014-06-10 11:41:56 +100044#include <asm/iommu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/machdep.h>
Stephen Rothwelld3878992005-09-28 02:50:25 +100046#include <asm/ppc-pci.h>
Linas Vepstas172ca922005-11-03 18:50:04 -060047#include <asm/rtas.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/** Overview:
Russell Currey8ee26532016-02-16 23:06:05 +110051 * EEH, or "Enhanced Error Handling" is a PCI bridge technology for
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * 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);
Gavin Shan35066c02016-09-28 14:34:54 +1000119EXPORT_SYMBOL_GPL(confirm_error_lock);
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600120
Gavin Shan212d16c2014-06-10 11:41:56 +1000121/* Lock to protect passed flags */
122static DEFINE_MUTEX(eeh_dev_mutex);
123
Linas Vepstas17213c32007-05-10 02:38:11 +1000124/* Buffer for reporting pci register dumps. Its here in BSS, and
125 * not dynamically alloced, so that it ends up in RMO where RTAS
126 * can access it.
127 */
Gavin Shanf2e0be52014-09-30 12:39:08 +1000128#define EEH_PCI_REGS_LOG_LEN 8192
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000129static unsigned char pci_regs_buf[EEH_PCI_REGS_LOG_LEN];
130
Gavin Shane575f8d2012-02-29 15:47:45 +0000131/*
132 * The struct is used to maintain the EEH global statistic
133 * information. Besides, the EEH global statistics will be
134 * exported to user space through procfs
135 */
136struct eeh_stats {
137 u64 no_device; /* PCI device not found */
138 u64 no_dn; /* OF node not found */
139 u64 no_cfg_addr; /* Config address not found */
140 u64 ignored_check; /* EEH check skipped */
141 u64 total_mmio_ffs; /* Total EEH checks */
142 u64 false_positives; /* Unnecessary EEH checks */
143 u64 slot_resets; /* PE reset */
144};
145
146static struct eeh_stats eeh_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Gavin Shan7f52a522014-04-24 18:00:18 +1000148static int __init eeh_setup(char *str)
149{
150 if (!strcmp(str, "off"))
Gavin Shan05b17212014-07-17 14:41:38 +1000151 eeh_add_flag(EEH_FORCE_DISABLED);
Gavin Shana450e8f2014-11-22 21:58:09 +1100152 else if (!strcmp(str, "early_log"))
153 eeh_add_flag(EEH_EARLY_DUMP_LOG);
Gavin Shan7f52a522014-04-24 18:00:18 +1000154
155 return 1;
156}
157__setup("eeh=", eeh_setup);
158
Gavin Shanf2e0be52014-09-30 12:39:08 +1000159/*
160 * This routine captures assorted PCI configuration space data
161 * for the indicated PCI device, and puts them into a buffer
162 * for RTAS error logging.
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000163 */
Gavin Shanf2e0be52014-09-30 12:39:08 +1000164static size_t eeh_dump_dev_log(struct eeh_dev *edev, char *buf, size_t len)
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000165{
Gavin Shan0bd78582015-03-17 16:15:07 +1100166 struct pci_dn *pdn = eeh_dev_to_pdn(edev);
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000167 u32 cfg;
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000168 int cap, i;
Gavin Shan0ed352d2014-07-17 14:41:40 +1000169 int n = 0, l = 0;
170 char buffer[128];
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000171
Sam Bobroff9bed31c2018-09-12 11:23:20 +1000172 if (!pdn) {
173 pr_warn("EEH: Note: No error log for absent device.\n");
174 return 0;
175 }
176
Guilherme G. Piccoli10560b92016-07-22 14:05:29 -0300177 n += scnprintf(buf+n, len-n, "%04x:%02x:%02x.%01x\n",
Gavin Shan0bd78582015-03-17 16:15:07 +1100178 edev->phb->global_number, pdn->busno,
179 PCI_SLOT(pdn->devfn), PCI_FUNC(pdn->devfn));
Guilherme G. Piccoli10560b92016-07-22 14:05:29 -0300180 pr_warn("EEH: of node=%04x:%02x:%02x.%01x\n",
Gavin Shan0bd78582015-03-17 16:15:07 +1100181 edev->phb->global_number, pdn->busno,
182 PCI_SLOT(pdn->devfn), PCI_FUNC(pdn->devfn));
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000183
Gavin Shan0bd78582015-03-17 16:15:07 +1100184 eeh_ops->read_config(pdn, PCI_VENDOR_ID, 4, &cfg);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000185 n += scnprintf(buf+n, len-n, "dev/vend:%08x\n", cfg);
Gavin Shan2d86c382014-04-24 18:00:15 +1000186 pr_warn("EEH: PCI device/vendor: %08x\n", cfg);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000187
Gavin Shan0bd78582015-03-17 16:15:07 +1100188 eeh_ops->read_config(pdn, PCI_COMMAND, 4, &cfg);
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000189 n += scnprintf(buf+n, len-n, "cmd/stat:%x\n", cfg);
Gavin Shan2d86c382014-04-24 18:00:15 +1000190 pr_warn("EEH: PCI cmd/status register: %08x\n", cfg);
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000191
Linas Vepstas0b9369f2007-07-27 08:35:40 +1000192 /* Gather bridge-specific registers */
Gavin Shan2a18dfc2014-04-24 18:00:16 +1000193 if (edev->mode & EEH_DEV_BRIDGE) {
Gavin Shan0bd78582015-03-17 16:15:07 +1100194 eeh_ops->read_config(pdn, PCI_SEC_STATUS, 2, &cfg);
Linas Vepstas0b9369f2007-07-27 08:35:40 +1000195 n += scnprintf(buf+n, len-n, "sec stat:%x\n", cfg);
Gavin Shan2d86c382014-04-24 18:00:15 +1000196 pr_warn("EEH: Bridge secondary status: %04x\n", cfg);
Linas Vepstas0b9369f2007-07-27 08:35:40 +1000197
Gavin Shan0bd78582015-03-17 16:15:07 +1100198 eeh_ops->read_config(pdn, PCI_BRIDGE_CONTROL, 2, &cfg);
Linas Vepstas0b9369f2007-07-27 08:35:40 +1000199 n += scnprintf(buf+n, len-n, "brdg ctl:%x\n", cfg);
Gavin Shan2d86c382014-04-24 18:00:15 +1000200 pr_warn("EEH: Bridge control: %04x\n", cfg);
Linas Vepstas0b9369f2007-07-27 08:35:40 +1000201 }
202
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000203 /* Dump out the PCI-X command and status regs */
Gavin Shan2a18dfc2014-04-24 18:00:16 +1000204 cap = edev->pcix_cap;
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000205 if (cap) {
Gavin Shan0bd78582015-03-17 16:15:07 +1100206 eeh_ops->read_config(pdn, cap, 4, &cfg);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000207 n += scnprintf(buf+n, len-n, "pcix-cmd:%x\n", cfg);
Gavin Shan2d86c382014-04-24 18:00:15 +1000208 pr_warn("EEH: PCI-X cmd: %08x\n", cfg);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000209
Gavin Shan0bd78582015-03-17 16:15:07 +1100210 eeh_ops->read_config(pdn, cap+4, 4, &cfg);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000211 n += scnprintf(buf+n, len-n, "pcix-stat:%x\n", cfg);
Gavin Shan2d86c382014-04-24 18:00:15 +1000212 pr_warn("EEH: PCI-X status: %08x\n", cfg);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000213 }
214
Gavin Shan2a18dfc2014-04-24 18:00:16 +1000215 /* If PCI-E capable, dump PCI-E cap 10 */
216 cap = edev->pcie_cap;
217 if (cap) {
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000218 n += scnprintf(buf+n, len-n, "pci-e cap10:\n");
Gavin Shan2d86c382014-04-24 18:00:15 +1000219 pr_warn("EEH: PCI-E capabilities and status follow:\n");
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000220
221 for (i=0; i<=8; i++) {
Gavin Shan0bd78582015-03-17 16:15:07 +1100222 eeh_ops->read_config(pdn, cap+4*i, 4, &cfg);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000223 n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
Gavin Shan0ed352d2014-07-17 14:41:40 +1000224
225 if ((i % 4) == 0) {
226 if (i != 0)
227 pr_warn("%s\n", buffer);
228
229 l = scnprintf(buffer, sizeof(buffer),
230 "EEH: PCI-E %02x: %08x ",
231 4*i, cfg);
232 } else {
233 l += scnprintf(buffer+l, sizeof(buffer)-l,
234 "%08x ", cfg);
235 }
236
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000237 }
Gavin Shan0ed352d2014-07-17 14:41:40 +1000238
239 pr_warn("%s\n", buffer);
Gavin Shan2a18dfc2014-04-24 18:00:16 +1000240 }
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000241
Gavin Shan2a18dfc2014-04-24 18:00:16 +1000242 /* If AER capable, dump it */
243 cap = edev->aer_cap;
244 if (cap) {
245 n += scnprintf(buf+n, len-n, "pci-e AER:\n");
246 pr_warn("EEH: PCI-E AER capability register set follows:\n");
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000247
Gavin Shan0ed352d2014-07-17 14:41:40 +1000248 for (i=0; i<=13; i++) {
Gavin Shan0bd78582015-03-17 16:15:07 +1100249 eeh_ops->read_config(pdn, cap+4*i, 4, &cfg);
Gavin Shan2a18dfc2014-04-24 18:00:16 +1000250 n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
Gavin Shan0ed352d2014-07-17 14:41:40 +1000251
252 if ((i % 4) == 0) {
253 if (i != 0)
254 pr_warn("%s\n", buffer);
255
256 l = scnprintf(buffer, sizeof(buffer),
257 "EEH: PCI-E AER %02x: %08x ",
258 4*i, cfg);
259 } else {
260 l += scnprintf(buffer+l, sizeof(buffer)-l,
261 "%08x ", cfg);
262 }
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000263 }
Gavin Shan0ed352d2014-07-17 14:41:40 +1000264
265 pr_warn("%s\n", buffer);
Linas Vepstasfcf9892b2007-05-09 09:36:21 +1000266 }
Linas Vepstas0b9369f2007-07-27 08:35:40 +1000267
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000268 return n;
269}
270
Gavin Shanf2e0be52014-09-30 12:39:08 +1000271static void *eeh_dump_pe_log(void *data, void *flag)
272{
273 struct eeh_pe *pe = data;
274 struct eeh_dev *edev, *tmp;
275 size_t *plen = flag;
276
277 eeh_pe_for_each_dev(pe, edev, tmp)
278 *plen += eeh_dump_dev_log(edev, pci_regs_buf + *plen,
279 EEH_PCI_REGS_LOG_LEN - *plen);
280
281 return NULL;
282}
283
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000284/**
285 * eeh_slot_error_detail - Generate combined log including driver log and error log
Gavin Shanff477962012-09-07 22:44:16 +0000286 * @pe: EEH PE
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000287 * @severity: temporary or permanent error log
288 *
289 * This routine should be called to generate the combined log, which
290 * is comprised of driver log and error log. The driver log is figured
291 * out from the config space of the corresponding PCI device, while
292 * the error log is fetched through platform dependent function call.
293 */
Gavin Shanff477962012-09-07 22:44:16 +0000294void eeh_slot_error_detail(struct eeh_pe *pe, int severity)
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000295{
296 size_t loglen = 0;
Gavin Shanff477962012-09-07 22:44:16 +0000297
Gavin Shanc35ae172013-06-27 13:46:42 +0800298 /*
299 * When the PHB is fenced or dead, it's pointless to collect
300 * the data from PCI config space because it should return
301 * 0xFF's. For ER, we still retrieve the data from the PCI
302 * config space.
Gavin Shan78954702014-04-24 18:00:14 +1000303 *
304 * For pHyp, we have to enable IO for log retrieval. Otherwise,
305 * 0xFF's is always returned from PCI config space.
Gavin Shan6e315b22017-01-06 10:39:49 +1100306 *
307 * When the @severity is EEH_LOG_PERM, the PE is going to be
308 * removed. Prior to that, the drivers for devices included in
309 * the PE will be closed. The drivers rely on working IO path
310 * to bring the devices to quiet state. Otherwise, PCI traffic
311 * from those devices after they are removed is like to cause
312 * another unexpected EEH error.
Gavin Shanc35ae172013-06-27 13:46:42 +0800313 */
Gavin Shan9e049372014-04-24 18:00:07 +1000314 if (!(pe->type & EEH_PE_PHB)) {
Gavin Shan6e315b22017-01-06 10:39:49 +1100315 if (eeh_has_flag(EEH_ENABLE_IO_FOR_LOG) ||
316 severity == EEH_LOG_PERM)
Gavin Shan78954702014-04-24 18:00:14 +1000317 eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
Gavin Shanc35ae172013-06-27 13:46:42 +0800318
Gavin Shan25980012015-08-28 11:57:00 +1000319 /*
320 * The config space of some PCI devices can't be accessed
321 * when their PEs are in frozen state. Otherwise, fenced
322 * PHB might be seen. Those PEs are identified with flag
323 * EEH_PE_CFG_RESTRICTED, indicating EEH_PE_CFG_BLOCKED
324 * is set automatically when the PE is put to EEH_PE_ISOLATED.
325 *
326 * Restoring BARs possibly triggers PCI config access in
327 * (OPAL) firmware and then causes fenced PHB. If the
328 * PCI config is blocked with flag EEH_PE_CFG_BLOCKED, it's
329 * pointless to restore BARs and dump config space.
330 */
331 eeh_ops->configure_bridge(pe);
332 if (!(pe->state & EEH_PE_CFG_BLOCKED)) {
333 eeh_pe_restore_bars(pe);
334
335 pci_regs_buf[0] = 0;
336 eeh_pe_traverse(pe, eeh_dump_pe_log, &loglen);
337 }
Gavin Shanc35ae172013-06-27 13:46:42 +0800338 }
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000339
Gavin Shanff477962012-09-07 22:44:16 +0000340 eeh_ops->get_log(pe, severity, pci_regs_buf, loglen);
Linas Vepstasd99bb1d2007-05-09 09:35:32 +1000341}
342
343/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000344 * eeh_token_to_phys - Convert EEH address token to phys address
345 * @token: I/O token, should be address in the form 0xA....
346 *
347 * This routine should be called to convert virtual I/O address
348 * to physical one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 */
350static inline unsigned long eeh_token_to_phys(unsigned long token)
351{
352 pte_t *ptep;
353 unsigned long pa;
Aneesh Kumar K.V12bc9f62013-06-20 14:30:18 +0530354 int hugepage_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Aneesh Kumar K.V12bc9f62013-06-20 14:30:18 +0530356 /*
Aneesh Kumar K.V691e95f2015-03-30 10:41:03 +0530357 * We won't find hugepages here(this is iomem). Hence we are not
358 * worried about _PAGE_SPLITTING/collapse. Also we will not hit
359 * page table free, because of init_mm.
Aneesh Kumar K.V12bc9f62013-06-20 14:30:18 +0530360 */
Aneesh Kumar K.V891121e2015-10-09 08:32:21 +0530361 ptep = __find_linux_pte_or_hugepte(init_mm.pgd, token,
362 NULL, &hugepage_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (!ptep)
364 return token;
Aneesh Kumar K.V12bc9f62013-06-20 14:30:18 +0530365 WARN_ON(hugepage_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 pa = pte_pfn(*ptep) << PAGE_SHIFT;
367
368 return pa | (token & (PAGE_SIZE-1));
369}
370
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800371/*
372 * On PowerNV platform, we might already have fenced PHB there.
373 * For that case, it's meaningless to recover frozen PE. Intead,
374 * We have to handle fenced PHB firstly.
375 */
376static int eeh_phb_check_failure(struct eeh_pe *pe)
377{
378 struct eeh_pe *phb_pe;
379 unsigned long flags;
380 int ret;
381
Gavin Shan05b17212014-07-17 14:41:38 +1000382 if (!eeh_has_flag(EEH_PROBE_MODE_DEV))
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800383 return -EPERM;
384
385 /* Find the PHB PE */
386 phb_pe = eeh_phb_pe_get(pe->phb);
387 if (!phb_pe) {
Gavin Shan0dae2742014-07-17 14:41:41 +1000388 pr_warn("%s Can't find PE for PHB#%d\n",
389 __func__, pe->phb->global_number);
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800390 return -EEXIST;
391 }
392
393 /* If the PHB has been in problematic state */
394 eeh_serialize_lock(&flags);
Gavin Shan9e049372014-04-24 18:00:07 +1000395 if (phb_pe->state & EEH_PE_ISOLATED) {
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800396 ret = 0;
397 goto out;
398 }
399
400 /* Check PHB state */
401 ret = eeh_ops->get_state(phb_pe, NULL);
402 if ((ret < 0) ||
403 (ret == EEH_STATE_NOT_SUPPORT) ||
404 (ret & (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) ==
405 (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) {
406 ret = 0;
407 goto out;
408 }
409
410 /* Isolate the PHB and send event */
411 eeh_pe_state_mark(phb_pe, EEH_PE_ISOLATED);
412 eeh_serialize_unlock(flags);
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800413
Gavin Shan357b2f32014-06-11 18:26:44 +1000414 pr_err("EEH: PHB#%x failure detected, location: %s\n",
415 phb_pe->phb->global_number, eeh_pe_loc_get(phb_pe));
Gavin Shan56ca4fd2013-06-27 13:46:46 +0800416 dump_stack();
Gavin Shan5293bf92013-09-06 09:00:05 +0800417 eeh_send_failure_event(phb_pe);
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800418
419 return 1;
420out:
421 eeh_serialize_unlock(flags);
422 return ret;
423}
424
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000425/**
Gavin Shanf8f7d632012-09-07 22:44:22 +0000426 * eeh_dev_check_failure - Check if all 1's data is due to EEH slot freeze
427 * @edev: eeh device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 *
429 * Check for an EEH failure for the given device node. Call this
430 * routine if the result of a read was all 0xff's and you want to
431 * find out if this is due to an EEH slot freeze. This routine
432 * will query firmware for the EEH status.
433 *
434 * Returns 0 if there has not been an EEH error; otherwise returns
Linas Vepstas69376502005-11-03 18:47:50 -0600435 * a non-zero value and queues up a slot isolation event notification.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 *
437 * It is safe to call this routine in an interrupt context.
438 */
Gavin Shanf8f7d632012-09-07 22:44:22 +0000439int eeh_dev_check_failure(struct eeh_dev *edev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 int ret;
Gavin Shan1ad7a722014-05-05 09:29:03 +1000442 int active_flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 unsigned long flags;
Gavin Shanc6406d82015-03-17 16:15:08 +1100444 struct pci_dn *pdn;
Gavin Shanf8f7d632012-09-07 22:44:22 +0000445 struct pci_dev *dev;
Gavin Shan357b2f32014-06-11 18:26:44 +1000446 struct eeh_pe *pe, *parent_pe, *phb_pe;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600447 int rc = 0;
Gavin Shanc6406d82015-03-17 16:15:08 +1100448 const char *location = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Gavin Shane575f8d2012-02-29 15:47:45 +0000450 eeh_stats.total_mmio_ffs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Gavin Shan2ec5a0a2014-02-12 15:24:55 +0800452 if (!eeh_enabled())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return 0;
454
Gavin Shanf8f7d632012-09-07 22:44:22 +0000455 if (!edev) {
Gavin Shane575f8d2012-02-29 15:47:45 +0000456 eeh_stats.no_dn++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return 0;
Linas Vepstas177bc932005-11-03 18:48:52 -0600458 }
Gavin Shanf8f7d632012-09-07 22:44:22 +0000459 dev = eeh_dev_to_pci_dev(edev);
Wei Yang2a582222014-09-17 10:48:26 +0800460 pe = eeh_dev_to_pe(edev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 /* Access to IO BARs might get this far and still not want checking. */
Gavin Shan66523d92012-09-07 22:44:13 +0000463 if (!pe) {
Gavin Shane575f8d2012-02-29 15:47:45 +0000464 eeh_stats.ignored_check++;
Gavin Shanc6406d82015-03-17 16:15:08 +1100465 pr_debug("EEH: Ignored check for %s\n",
466 eeh_pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return 0;
468 }
469
Gavin Shan66523d92012-09-07 22:44:13 +0000470 if (!pe->addr && !pe->config_addr) {
Gavin Shane575f8d2012-02-29 15:47:45 +0000471 eeh_stats.no_cfg_addr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return 0;
473 }
474
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800475 /*
476 * On PowerNV platform, we might already have fenced PHB
477 * there and we need take care of that firstly.
478 */
479 ret = eeh_phb_check_failure(pe);
480 if (ret > 0)
481 return ret;
482
Gavin Shan05ec4242014-06-10 11:41:55 +1000483 /*
484 * If the PE isn't owned by us, we shouldn't check the
485 * state. Instead, let the owner handle it if the PE has
486 * been frozen.
487 */
488 if (eeh_pe_passed(pe))
489 return 0;
490
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600491 /* If we already have a pending isolation event for this
492 * slot, we know it's bad already, we don't need to check.
493 * Do this checking under a lock; as multiple PCI devices
494 * in one slot might report errors simultaneously, and we
495 * only want one error recovery routine running.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 */
Gavin Shan49075812013-06-20 13:21:03 +0800497 eeh_serialize_lock(&flags);
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600498 rc = 1;
Gavin Shan66523d92012-09-07 22:44:13 +0000499 if (pe->state & EEH_PE_ISOLATED) {
500 pe->check_count++;
501 if (pe->check_count % EEH_MAX_FAILS == 0) {
Gavin Shanc6406d82015-03-17 16:15:08 +1100502 pdn = eeh_dev_to_pdn(edev);
503 if (pdn->node)
504 location = of_get_property(pdn->node, "ibm,loc-code", NULL);
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000505 printk(KERN_ERR "EEH: %d reads ignored for recovering device at "
Mike Masonf36c5222008-07-22 02:40:17 +1000506 "location=%s driver=%s pci addr=%s\n",
Gavin Shanc6406d82015-03-17 16:15:08 +1100507 pe->check_count,
508 location ? location : "unknown",
Thadeu Lima de Souza Cascardo778a7852012-01-11 09:09:58 +0000509 eeh_driver_name(dev), eeh_pci_name(dev));
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000510 printk(KERN_ERR "EEH: Might be infinite loop in %s driver\n",
Thadeu Lima de Souza Cascardo778a7852012-01-11 09:09:58 +0000511 eeh_driver_name(dev));
Linas Vepstas5c1344e2005-11-03 18:49:31 -0600512 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600514 goto dn_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
516
517 /*
518 * Now test for an EEH failure. This is VERY expensive.
519 * Note that the eeh_config_addr may be a parent device
520 * in the case of a device behind a bridge, or it may be
521 * function zero of a multi-function device.
522 * In any case they must share a common PHB.
523 */
Gavin Shan66523d92012-09-07 22:44:13 +0000524 ret = eeh_ops->get_state(pe, NULL);
Linas Vepstas76e6faf2005-11-03 18:49:15 -0600525
Linas Vepstas39d16e22007-03-19 14:51:00 -0500526 /* Note that config-io to empty slots may fail;
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000527 * they are empty when they don't have children.
Gavin Shaneb594a42012-02-27 20:03:57 +0000528 * We will punt with the following conditions: Failure to get
529 * PE's state, EEH not support and Permanently unavailable
530 * state, PE is in good state.
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000531 */
Gavin Shaneb594a42012-02-27 20:03:57 +0000532 if ((ret < 0) ||
533 (ret == EEH_STATE_NOT_SUPPORT) ||
Gavin Shan1ad7a722014-05-05 09:29:03 +1000534 ((ret & active_flags) == active_flags)) {
Gavin Shane575f8d2012-02-29 15:47:45 +0000535 eeh_stats.false_positives++;
Gavin Shan66523d92012-09-07 22:44:13 +0000536 pe->false_positives++;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600537 rc = 0;
538 goto dn_unlock;
Linas Vepstas76e6faf2005-11-03 18:49:15 -0600539 }
540
Gavin Shan1ad7a722014-05-05 09:29:03 +1000541 /*
542 * It should be corner case that the parent PE has been
543 * put into frozen state as well. We should take care
544 * that at first.
545 */
546 parent_pe = pe->parent;
547 while (parent_pe) {
548 /* Hit the ceiling ? */
549 if (parent_pe->type & EEH_PE_PHB)
550 break;
551
552 /* Frozen parent PE ? */
553 ret = eeh_ops->get_state(parent_pe, NULL);
554 if (ret > 0 &&
555 (ret & active_flags) != active_flags)
556 pe = parent_pe;
557
558 /* Next parent level */
559 parent_pe = parent_pe->parent;
560 }
561
Gavin Shane575f8d2012-02-29 15:47:45 +0000562 eeh_stats.slot_resets++;
Gavin Shana84f2732013-06-20 13:20:51 +0800563
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600564 /* Avoid repeated reports of this failure, including problems
565 * with other functions on this device, and functions under
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000566 * bridges.
567 */
Gavin Shan66523d92012-09-07 22:44:13 +0000568 eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
Gavin Shan49075812013-06-20 13:21:03 +0800569 eeh_serialize_unlock(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 /* Most EEH events are due to device driver bugs. Having
572 * a stack trace will help the device-driver authors figure
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000573 * out what happened. So print that out.
574 */
Gavin Shan357b2f32014-06-11 18:26:44 +1000575 phb_pe = eeh_phb_pe_get(pe->phb);
576 pr_err("EEH: Frozen PHB#%x-PE#%x detected\n",
577 pe->phb->global_number, pe->addr);
578 pr_err("EEH: PE location: %s, PHB location: %s\n",
579 eeh_pe_loc_get(pe), eeh_pe_loc_get(phb_pe));
Gavin Shan56ca4fd2013-06-27 13:46:46 +0800580 dump_stack();
581
Gavin Shan5293bf92013-09-06 09:00:05 +0800582 eeh_send_failure_event(pe);
583
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600584 return 1;
585
586dn_unlock:
Gavin Shan49075812013-06-20 13:21:03 +0800587 eeh_serialize_unlock(flags);
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600588 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
Gavin Shanf8f7d632012-09-07 22:44:22 +0000591EXPORT_SYMBOL_GPL(eeh_dev_check_failure);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000594 * eeh_check_failure - Check if all 1's data is due to EEH slot freeze
Gavin Shan3e938052014-09-30 12:38:50 +1000595 * @token: I/O address
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 *
Gavin Shan3e938052014-09-30 12:38:50 +1000597 * Check for an EEH failure at the given I/O address. Call this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 * routine if the result of a read was all 0xff's and you want to
Gavin Shan3e938052014-09-30 12:38:50 +1000599 * find out if this is due to an EEH slot freeze event. This routine
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 * will query firmware for the EEH status.
601 *
602 * Note this routine is safe to call in an interrupt context.
603 */
Gavin Shan3e938052014-09-30 12:38:50 +1000604int eeh_check_failure(const volatile void __iomem *token)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
606 unsigned long addr;
Gavin Shanf8f7d632012-09-07 22:44:22 +0000607 struct eeh_dev *edev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 /* Finding the phys addr + pci device; this is pretty quick. */
610 addr = eeh_token_to_phys((unsigned long __force) token);
Gavin Shan3ab96a02012-09-07 22:44:23 +0000611 edev = eeh_addr_cache_get_dev(addr);
Gavin Shanf8f7d632012-09-07 22:44:22 +0000612 if (!edev) {
Gavin Shane575f8d2012-02-29 15:47:45 +0000613 eeh_stats.no_device++;
Gavin Shan3e938052014-09-30 12:38:50 +1000614 return 0;
Linas Vepstas177bc932005-11-03 18:48:52 -0600615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Gavin Shan3e938052014-09-30 12:38:50 +1000617 return eeh_dev_check_failure(edev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619EXPORT_SYMBOL(eeh_check_failure);
620
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600621
Linas Vepstascb5b56242006-09-15 18:56:35 -0500622/**
Gavin Shancce4b2d2012-02-27 20:03:52 +0000623 * eeh_pci_enable - Enable MMIO or DMA transfers for this slot
Gavin Shanff477962012-09-07 22:44:16 +0000624 * @pe: EEH PE
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000625 *
626 * This routine should be called to reenable frozen MMIO or DMA
627 * so that it would work correctly again. It's useful while doing
628 * recovery or log collection on the indicated device.
Linas Vepstas47b5c832006-09-15 18:57:42 -0500629 */
Gavin Shanff477962012-09-07 22:44:16 +0000630int eeh_pci_enable(struct eeh_pe *pe, int function)
Linas Vepstas47b5c832006-09-15 18:57:42 -0500631{
Gavin Shan4d4f5772014-09-30 12:39:00 +1000632 int active_flag, rc;
Gavin Shan78954702014-04-24 18:00:14 +1000633
634 /*
635 * pHyp doesn't allow to enable IO or DMA on unfrozen PE.
636 * Also, it's pointless to enable them on unfrozen PE. So
Gavin Shan4d4f5772014-09-30 12:39:00 +1000637 * we have to check before enabling IO or DMA.
Gavin Shan78954702014-04-24 18:00:14 +1000638 */
Gavin Shan4d4f5772014-09-30 12:39:00 +1000639 switch (function) {
640 case EEH_OPT_THAW_MMIO:
Gavin Shan872ee2d2015-10-08 14:58:55 +1100641 active_flag = EEH_STATE_MMIO_ACTIVE | EEH_STATE_MMIO_ENABLED;
Gavin Shan4d4f5772014-09-30 12:39:00 +1000642 break;
643 case EEH_OPT_THAW_DMA:
644 active_flag = EEH_STATE_DMA_ACTIVE;
645 break;
646 case EEH_OPT_DISABLE:
647 case EEH_OPT_ENABLE:
648 case EEH_OPT_FREEZE_PE:
649 active_flag = 0;
650 break;
651 default:
652 pr_warn("%s: Invalid function %d\n",
653 __func__, function);
654 return -EINVAL;
655 }
656
657 /*
658 * Check if IO or DMA has been enabled before
659 * enabling them.
660 */
661 if (active_flag) {
Gavin Shan78954702014-04-24 18:00:14 +1000662 rc = eeh_ops->get_state(pe, NULL);
663 if (rc < 0)
664 return rc;
665
Gavin Shan4d4f5772014-09-30 12:39:00 +1000666 /* Needn't enable it at all */
667 if (rc == EEH_STATE_NOT_SUPPORT)
668 return 0;
669
670 /* It's already enabled */
671 if (rc & active_flag)
Gavin Shan78954702014-04-24 18:00:14 +1000672 return 0;
673 }
Linas Vepstas47b5c832006-09-15 18:57:42 -0500674
Gavin Shan4d4f5772014-09-30 12:39:00 +1000675
676 /* Issue the request */
Gavin Shanff477962012-09-07 22:44:16 +0000677 rc = eeh_ops->set_option(pe, function);
Linas Vepstas47b5c832006-09-15 18:57:42 -0500678 if (rc)
Gavin Shan78954702014-04-24 18:00:14 +1000679 pr_warn("%s: Unexpected state change %d on "
680 "PHB#%d-PE#%x, err=%d\n",
681 __func__, function, pe->phb->global_number,
682 pe->addr, rc);
Linas Vepstas47b5c832006-09-15 18:57:42 -0500683
Gavin Shan4d4f5772014-09-30 12:39:00 +1000684 /* Check if the request is finished successfully */
685 if (active_flag) {
686 rc = eeh_ops->wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
Andrew Donnellan949e9b82015-10-23 17:19:46 +1100687 if (rc < 0)
Gavin Shan4d4f5772014-09-30 12:39:00 +1000688 return rc;
Gavin Shan78954702014-04-24 18:00:14 +1000689
Gavin Shan4d4f5772014-09-30 12:39:00 +1000690 if (rc & active_flag)
691 return 0;
Gavin Shan78954702014-04-24 18:00:14 +1000692
Gavin Shan4d4f5772014-09-30 12:39:00 +1000693 return -EIO;
694 }
Linas Vepstasfa1be472007-03-19 14:59:59 -0500695
Linas Vepstas47b5c832006-09-15 18:57:42 -0500696 return rc;
697}
698
Gavin Shan28158cd2015-02-11 10:20:49 +1100699static void *eeh_disable_and_save_dev_state(void *data, void *userdata)
700{
701 struct eeh_dev *edev = data;
702 struct pci_dev *pdev = eeh_dev_to_pci_dev(edev);
703 struct pci_dev *dev = userdata;
704
705 /*
706 * The caller should have disabled and saved the
707 * state for the specified device
708 */
709 if (!pdev || pdev == dev)
710 return NULL;
711
712 /* Ensure we have D0 power state */
713 pci_set_power_state(pdev, PCI_D0);
714
715 /* Save device state */
716 pci_save_state(pdev);
717
718 /*
719 * Disable device to avoid any DMA traffic and
720 * interrupt from the device
721 */
722 pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
723
724 return NULL;
725}
726
727static void *eeh_restore_dev_state(void *data, void *userdata)
728{
729 struct eeh_dev *edev = data;
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
Linas Vepstas47b5c832006-09-15 18:57:42 -0500748/**
Andrew Donnellan31f6a4a2016-02-08 14:39:19 +1100749 * pcibios_set_pcie_reset_state - Set PCI-E reset state
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000750 * @dev: pci device struct
751 * @state: reset state to enter
Brian King00c2ae32007-05-08 08:04:05 +1000752 *
753 * Return value:
754 * 0 if success
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000755 */
Brian King00c2ae32007-05-08 08:04:05 +1000756int pcibios_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
757{
Gavin Shanc270a242012-09-07 22:44:17 +0000758 struct eeh_dev *edev = pci_dev_to_eeh_dev(dev);
Wei Yang2a582222014-09-17 10:48:26 +0800759 struct eeh_pe *pe = eeh_dev_to_pe(edev);
Gavin Shanc270a242012-09-07 22:44:17 +0000760
761 if (!pe) {
762 pr_err("%s: No PE found on PCI device %s\n",
763 __func__, pci_name(dev));
764 return -EINVAL;
765 }
Brian King00c2ae32007-05-08 08:04:05 +1000766
767 switch (state) {
768 case pcie_deassert_reset:
Gavin Shanc270a242012-09-07 22:44:17 +0000769 eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);
Gavin Shan28158cd2015-02-11 10:20:49 +1100770 eeh_unfreeze_pe(pe, false);
Wei Yang9312bc52016-03-04 10:53:09 +1100771 if (!(pe->type & EEH_PE_VF))
772 eeh_pe_state_clear(pe, EEH_PE_CFG_BLOCKED);
Gavin Shan28158cd2015-02-11 10:20:49 +1100773 eeh_pe_dev_traverse(pe, eeh_restore_dev_state, dev);
Gavin Shan1ae79b72015-05-01 09:14:11 +1000774 eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
Brian King00c2ae32007-05-08 08:04:05 +1000775 break;
776 case pcie_hot_reset:
Gavin Shan39bfd712015-07-30 09:26:51 +1000777 eeh_pe_state_mark_with_cfg(pe, EEH_PE_ISOLATED);
Gavin Shan28158cd2015-02-11 10:20:49 +1100778 eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);
779 eeh_pe_dev_traverse(pe, eeh_disable_and_save_dev_state, dev);
Wei Yang9312bc52016-03-04 10:53:09 +1100780 if (!(pe->type & EEH_PE_VF))
781 eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
Gavin Shanc270a242012-09-07 22:44:17 +0000782 eeh_ops->reset(pe, EEH_RESET_HOT);
Brian King00c2ae32007-05-08 08:04:05 +1000783 break;
784 case pcie_warm_reset:
Gavin Shan39bfd712015-07-30 09:26:51 +1000785 eeh_pe_state_mark_with_cfg(pe, EEH_PE_ISOLATED);
Gavin Shan28158cd2015-02-11 10:20:49 +1100786 eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);
787 eeh_pe_dev_traverse(pe, eeh_disable_and_save_dev_state, dev);
Wei Yang9312bc52016-03-04 10:53:09 +1100788 if (!(pe->type & EEH_PE_VF))
789 eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
Gavin Shanc270a242012-09-07 22:44:17 +0000790 eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
Brian King00c2ae32007-05-08 08:04:05 +1000791 break;
792 default:
Gavin Shan1ae79b72015-05-01 09:14:11 +1000793 eeh_pe_state_clear(pe, EEH_PE_ISOLATED | EEH_PE_CFG_BLOCKED);
Brian King00c2ae32007-05-08 08:04:05 +1000794 return -EINVAL;
795 };
796
797 return 0;
798}
799
800/**
Gavin Shanc270a242012-09-07 22:44:17 +0000801 * eeh_set_pe_freset - Check the required reset for the indicated device
802 * @data: EEH device
803 * @flag: return value
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000804 *
805 * Each device might have its preferred reset type: fundamental or
806 * hot reset. The routine is used to collected the information for
807 * the indicated device and its children so that the bunch of the
808 * devices could be reset properly.
809 */
Gavin Shanc270a242012-09-07 22:44:17 +0000810static void *eeh_set_dev_freset(void *data, void *flag)
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000811{
812 struct pci_dev *dev;
Gavin Shanc270a242012-09-07 22:44:17 +0000813 unsigned int *freset = (unsigned int *)flag;
814 struct eeh_dev *edev = (struct eeh_dev *)data;
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000815
Gavin Shanc270a242012-09-07 22:44:17 +0000816 dev = eeh_dev_to_pci_dev(edev);
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000817 if (dev)
818 *freset |= dev->needs_freset;
819
Gavin Shanc270a242012-09-07 22:44:17 +0000820 return NULL;
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000821}
822
823/**
Gavin Shancce4b2d2012-02-27 20:03:52 +0000824 * eeh_reset_pe_once - Assert the pci #RST line for 1/4 second
Gavin Shanc270a242012-09-07 22:44:17 +0000825 * @pe: EEH PE
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000826 *
827 * Assert the PCI #RST line for 1/4 second.
828 */
Gavin Shanc270a242012-09-07 22:44:17 +0000829static void eeh_reset_pe_once(struct eeh_pe *pe)
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600830{
Richard A Lary308fc4f2011-04-22 09:59:47 +0000831 unsigned int freset = 0;
Mike Mason6e193142009-07-30 15:42:39 -0700832
Richard A Lary308fc4f2011-04-22 09:59:47 +0000833 /* Determine type of EEH reset required for
834 * Partitionable Endpoint, a hot-reset (1)
835 * or a fundamental reset (3).
836 * A fundamental reset required by any device under
837 * Partitionable Endpoint trumps hot-reset.
Gavin Shana84f2732013-06-20 13:20:51 +0800838 */
Gavin Shanc270a242012-09-07 22:44:17 +0000839 eeh_pe_dev_traverse(pe, eeh_set_dev_freset, &freset);
Richard A Lary308fc4f2011-04-22 09:59:47 +0000840
841 if (freset)
Gavin Shanc270a242012-09-07 22:44:17 +0000842 eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
Mike Mason6e193142009-07-30 15:42:39 -0700843 else
Gavin Shanc270a242012-09-07 22:44:17 +0000844 eeh_ops->reset(pe, EEH_RESET_HOT);
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600845
Gavin Shanc270a242012-09-07 22:44:17 +0000846 eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);
Linas Vepstase1029262006-09-21 18:25:56 -0500847}
848
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000849/**
Gavin Shancce4b2d2012-02-27 20:03:52 +0000850 * eeh_reset_pe - Reset the indicated PE
Gavin Shanc270a242012-09-07 22:44:17 +0000851 * @pe: EEH PE
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000852 *
853 * This routine should be called to reset indicated device, including
854 * PE. A PE might include multiple PCI devices and sometimes PCI bridges
855 * might be involved as well.
856 */
Gavin Shanc270a242012-09-07 22:44:17 +0000857int eeh_reset_pe(struct eeh_pe *pe)
Linas Vepstase1029262006-09-21 18:25:56 -0500858{
Gavin Shan326a98e2013-06-20 13:20:58 +0800859 int flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
Gavin Shanb85743e2014-11-14 10:47:28 +1100860 int i, state, ret;
Linas Vepstase1029262006-09-21 18:25:56 -0500861
Gavin Shan28bf36f2014-11-14 10:47:29 +1100862 /* Mark as reset and block config space */
863 eeh_pe_state_mark(pe, EEH_PE_RESET | EEH_PE_CFG_BLOCKED);
864
Linas Vepstas9c547762007-03-19 14:58:07 -0500865 /* Take three shots at resetting the bus */
Gavin Shanb85743e2014-11-14 10:47:28 +1100866 for (i = 0; i < 3; i++) {
Gavin Shanc270a242012-09-07 22:44:17 +0000867 eeh_reset_pe_once(pe);
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600868
Gavin Shan78954702014-04-24 18:00:14 +1000869 /*
870 * EEH_PE_ISOLATED is expected to be removed after
871 * BAR restore.
872 */
Gavin Shanb85743e2014-11-14 10:47:28 +1100873 state = eeh_ops->wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
874 if ((state & flags) == flags) {
875 ret = 0;
876 goto out;
Linas Vepstase1029262006-09-21 18:25:56 -0500877 }
Gavin Shanb85743e2014-11-14 10:47:28 +1100878
879 if (state < 0) {
880 pr_warn("%s: Unrecoverable slot failure on PHB#%d-PE#%x",
881 __func__, pe->phb->global_number, pe->addr);
882 ret = -ENOTRECOVERABLE;
883 goto out;
884 }
885
886 /* We might run out of credits */
887 ret = -EIO;
888 pr_warn("%s: Failure %d resetting PHB#%x-PE#%x\n (%d)\n",
889 __func__, state, pe->phb->global_number, pe->addr, (i + 1));
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600890 }
Linas Vepstasb6495c02005-11-03 18:54:54 -0600891
Gavin Shanb85743e2014-11-14 10:47:28 +1100892out:
Gavin Shan28bf36f2014-11-14 10:47:29 +1100893 eeh_pe_state_clear(pe, EEH_PE_RESET | EEH_PE_CFG_BLOCKED);
Gavin Shanb85743e2014-11-14 10:47:28 +1100894 return ret;
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600895}
896
Linas Vepstas8b553f32005-11-03 18:50:17 -0600897/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000898 * eeh_save_bars - Save device bars
Gavin Shanf631acd2012-02-27 20:04:07 +0000899 * @edev: PCI device associated EEH device
Linas Vepstas8b553f32005-11-03 18:50:17 -0600900 *
901 * Save the values of the device bars. Unlike the restore
902 * routine, this routine is *not* recursive. This is because
Justin Mattock31116f02011-02-24 20:10:18 +0000903 * PCI devices are added individually; but, for the restore,
Linas Vepstas8b553f32005-11-03 18:50:17 -0600904 * an entire slot is reset at a time.
905 */
Gavin Shand7bb8862012-09-07 22:44:21 +0000906void eeh_save_bars(struct eeh_dev *edev)
Linas Vepstas8b553f32005-11-03 18:50:17 -0600907{
Gavin Shan0bd78582015-03-17 16:15:07 +1100908 struct pci_dn *pdn;
Linas Vepstas8b553f32005-11-03 18:50:17 -0600909 int i;
910
Gavin Shan0bd78582015-03-17 16:15:07 +1100911 pdn = eeh_dev_to_pdn(edev);
912 if (!pdn)
Linas Vepstas8b553f32005-11-03 18:50:17 -0600913 return;
Gavin Shana84f2732013-06-20 13:20:51 +0800914
Linas Vepstas8b553f32005-11-03 18:50:17 -0600915 for (i = 0; i < 16; i++)
Gavin Shan0bd78582015-03-17 16:15:07 +1100916 eeh_ops->read_config(pdn, i * 4, 4, &edev->config_space[i]);
Gavin Shanbf898ec2013-11-12 14:49:21 +0800917
918 /*
919 * For PCI bridges including root port, we need enable bus
920 * master explicitly. Otherwise, it can't fetch IODA table
921 * entries correctly. So we cache the bit in advance so that
922 * we can restore it after reset, either PHB range or PE range.
923 */
924 if (edev->mode & EEH_DEV_BRIDGE)
925 edev->config_space[1] |= PCI_COMMAND_MASTER;
Linas Vepstas8b553f32005-11-03 18:50:17 -0600926}
927
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000928/**
Gavin Shanaa1e6372012-02-27 20:03:53 +0000929 * eeh_ops_register - Register platform dependent EEH operations
930 * @ops: platform dependent EEH operations
931 *
932 * Register the platform dependent EEH operation callback
933 * functions. The platform should call this function before
934 * any other EEH operations.
935 */
936int __init eeh_ops_register(struct eeh_ops *ops)
937{
938 if (!ops->name) {
Gavin Shan0dae2742014-07-17 14:41:41 +1000939 pr_warn("%s: Invalid EEH ops name for %p\n",
Gavin Shanaa1e6372012-02-27 20:03:53 +0000940 __func__, ops);
941 return -EINVAL;
942 }
943
944 if (eeh_ops && eeh_ops != ops) {
Gavin Shan0dae2742014-07-17 14:41:41 +1000945 pr_warn("%s: EEH ops of platform %s already existing (%s)\n",
Gavin Shanaa1e6372012-02-27 20:03:53 +0000946 __func__, eeh_ops->name, ops->name);
947 return -EEXIST;
948 }
949
950 eeh_ops = ops;
951
952 return 0;
953}
954
955/**
956 * eeh_ops_unregister - Unreigster platform dependent EEH operations
957 * @name: name of EEH platform operations
958 *
959 * Unregister the platform dependent EEH operation callback
960 * functions.
961 */
962int __exit eeh_ops_unregister(const char *name)
963{
964 if (!name || !strlen(name)) {
Gavin Shan0dae2742014-07-17 14:41:41 +1000965 pr_warn("%s: Invalid EEH ops name\n",
Gavin Shanaa1e6372012-02-27 20:03:53 +0000966 __func__);
967 return -EINVAL;
968 }
969
970 if (eeh_ops && !strcmp(eeh_ops->name, name)) {
971 eeh_ops = NULL;
972 return 0;
973 }
974
975 return -EEXIST;
976}
977
Gavin Shan66f9af832014-02-12 15:24:56 +0800978static int eeh_reboot_notifier(struct notifier_block *nb,
979 unsigned long action, void *unused)
980{
Gavin Shan05b17212014-07-17 14:41:38 +1000981 eeh_clear_flag(EEH_ENABLED);
Gavin Shan66f9af832014-02-12 15:24:56 +0800982 return NOTIFY_DONE;
983}
984
985static struct notifier_block eeh_reboot_nb = {
986 .notifier_call = eeh_reboot_notifier,
987};
988
Gavin Shanaa1e6372012-02-27 20:03:53 +0000989/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000990 * eeh_init - EEH initialization
991 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 * Initialize EEH by trying to enable it for all of the adapters in the system.
993 * As a side effect we can determine here if eeh is supported at all.
994 * Note that we leave EEH on so failed config cycles won't cause a machine
995 * check. If a user turns off EEH for a particular adapter they are really
996 * telling Linux to ignore errors. Some hardware (e.g. POWER5) won't
997 * grant access to a slot if EEH isn't enabled, and so we always enable
998 * EEH for all slots/all devices.
999 *
1000 * The eeh-force-off option disables EEH checking globally, for all slots.
1001 * Even if force-off is set, the EEH hardware is still enabled, so that
1002 * newer systems can boot.
1003 */
Gavin Shaneeb63612013-06-27 13:46:47 +08001004int eeh_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
Gavin Shan1a5c2e62012-03-20 21:30:29 +00001006 struct pci_controller *hose, *tmp;
Gavin Shanff57b452015-03-17 16:15:06 +11001007 struct pci_dn *pdn;
Gavin Shan51fb5f52013-06-20 13:20:56 +08001008 static int cnt = 0;
1009 int ret = 0;
1010
1011 /*
1012 * We have to delay the initialization on PowerNV after
1013 * the PCI hierarchy tree has been built because the PEs
1014 * are figured out based on PCI devices instead of device
1015 * tree nodes
1016 */
1017 if (machine_is(powernv) && cnt++ <= 0)
1018 return ret;
Gavin Shane2af1552012-02-27 20:03:54 +00001019
Gavin Shan66f9af832014-02-12 15:24:56 +08001020 /* Register reboot notifier */
1021 ret = register_reboot_notifier(&eeh_reboot_nb);
1022 if (ret) {
1023 pr_warn("%s: Failed to register notifier (%d)\n",
1024 __func__, ret);
1025 return ret;
1026 }
1027
Gavin Shane2af1552012-02-27 20:03:54 +00001028 /* call platform initialization function */
1029 if (!eeh_ops) {
Gavin Shan0dae2742014-07-17 14:41:41 +10001030 pr_warn("%s: Platform EEH operation not found\n",
Gavin Shane2af1552012-02-27 20:03:54 +00001031 __func__);
Gavin Shan35e5cfe2012-09-07 22:44:02 +00001032 return -EEXIST;
Greg Kurz221195f2014-11-25 17:10:06 +01001033 } else if ((ret = eeh_ops->init()))
Gavin Shan35e5cfe2012-09-07 22:44:02 +00001034 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Gavin Shanc8608552013-06-20 13:21:00 +08001036 /* Initialize EEH event */
1037 ret = eeh_event_init();
1038 if (ret)
1039 return ret;
1040
Gavin Shan1a5c2e62012-03-20 21:30:29 +00001041 /* Enable EEH for all adapters */
Gavin Shanff57b452015-03-17 16:15:06 +11001042 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
1043 pdn = hose->pci_data;
1044 traverse_pci_dn(pdn, eeh_ops->probe, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
1046
Gavin Shan21fd21f2013-06-20 13:20:57 +08001047 /*
1048 * Call platform post-initialization. Actually, It's good chance
1049 * to inform platform that EEH is ready to supply service if the
1050 * I/O cache stuff has been built up.
1051 */
1052 if (eeh_ops->post_init) {
1053 ret = eeh_ops->post_init();
1054 if (ret)
1055 return ret;
1056 }
1057
Gavin Shan2ec5a0a2014-02-12 15:24:55 +08001058 if (eeh_enabled())
Gavin Shand7bb8862012-09-07 22:44:21 +00001059 pr_info("EEH: PCI Enhanced I/O Error Handling Enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 else
Anton Blanchard91ac7302016-10-02 11:09:38 +11001061 pr_info("EEH: No capable adapters found\n");
Gavin Shan35e5cfe2012-09-07 22:44:02 +00001062
1063 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
Gavin Shan35e5cfe2012-09-07 22:44:02 +00001066core_initcall_sync(eeh_init);
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068/**
Gavin Shanc6406d82015-03-17 16:15:08 +11001069 * eeh_add_device_early - Enable EEH for the indicated device node
Gavin Shanff57b452015-03-17 16:15:06 +11001070 * @pdn: PCI device node for which to set up EEH
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 *
1072 * This routine must be used to perform EEH initialization for PCI
1073 * devices that were added after system boot (e.g. hotplug, dlpar).
1074 * This routine must be called before any i/o is performed to the
1075 * adapter (inluding any config-space i/o).
1076 * Whether this actually enables EEH or not for this device depends
1077 * on the CEC architecture, type of the device, on earlier boot
1078 * command-line arguments & etc.
1079 */
Gavin Shanff57b452015-03-17 16:15:06 +11001080void eeh_add_device_early(struct pci_dn *pdn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
1082 struct pci_controller *phb;
Gavin Shanff57b452015-03-17 16:15:06 +11001083 struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Guilherme G. Piccolic2078d92016-04-11 16:17:22 -03001085 if (!edev)
Gavin Shan26a74852013-06-20 13:20:59 +08001086 return;
1087
Gavin Shand91dafc2015-05-01 09:22:15 +10001088 if (!eeh_has_flag(EEH_PROBE_MODE_DEVTREE))
1089 return;
1090
Linas Vepstasf751f842005-11-03 18:54:23 -06001091 /* USB Bus children of PCI devices will not have BUID's */
Gavin Shanff57b452015-03-17 16:15:06 +11001092 phb = edev->phb;
1093 if (NULL == phb ||
1094 (eeh_has_flag(EEH_PROBE_MODE_DEVTREE) && 0 == phb->buid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Gavin Shanff57b452015-03-17 16:15:06 +11001097 eeh_ops->probe(pdn, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001100/**
1101 * eeh_add_device_tree_early - Enable EEH for the indicated device
Gavin Shanff57b452015-03-17 16:15:06 +11001102 * @pdn: PCI device node
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001103 *
1104 * This routine must be used to perform EEH initialization for the
1105 * indicated PCI device that was added after system boot (e.g.
1106 * hotplug, dlpar).
1107 */
Gavin Shanff57b452015-03-17 16:15:06 +11001108void eeh_add_device_tree_early(struct pci_dn *pdn)
Linas Vepstase2a296e2005-11-03 18:51:31 -06001109{
Gavin Shanff57b452015-03-17 16:15:06 +11001110 struct pci_dn *n;
Stephen Rothwellacaa6172007-12-21 15:52:07 +11001111
Gavin Shanff57b452015-03-17 16:15:06 +11001112 if (!pdn)
1113 return;
1114
1115 list_for_each_entry(n, &pdn->child_list, list)
1116 eeh_add_device_tree_early(n);
1117 eeh_add_device_early(pdn);
Linas Vepstase2a296e2005-11-03 18:51:31 -06001118}
1119EXPORT_SYMBOL_GPL(eeh_add_device_tree_early);
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001122 * eeh_add_device_late - Perform EEH initialization for the indicated pci device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 * @dev: pci device for which to set up EEH
1124 *
1125 * This routine must be used to complete EEH initialization for PCI
1126 * devices that were added after system boot (e.g. hotplug, dlpar).
1127 */
Gavin Shanf2856492013-07-24 10:24:52 +08001128void eeh_add_device_late(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
Gavin Shanc6406d82015-03-17 16:15:08 +11001130 struct pci_dn *pdn;
Gavin Shanf631acd2012-02-27 20:04:07 +00001131 struct eeh_dev *edev;
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001132
Gavin Shan2ec5a0a2014-02-12 15:24:55 +08001133 if (!dev || !eeh_enabled())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return;
1135
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001136 pr_debug("EEH: Adding device %s\n", pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Gavin Shanc6406d82015-03-17 16:15:08 +11001138 pdn = pci_get_pdn_by_devfn(dev->bus, dev->devfn);
1139 edev = pdn_to_eeh_dev(pdn);
Gavin Shanf631acd2012-02-27 20:04:07 +00001140 if (edev->pdev == dev) {
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001141 pr_debug("EEH: Already referenced !\n");
1142 return;
1143 }
Gavin Shanf5c57712013-07-24 10:24:58 +08001144
1145 /*
1146 * The EEH cache might not be removed correctly because of
1147 * unbalanced kref to the device during unplug time, which
1148 * relies on pcibios_release_device(). So we have to remove
1149 * that here explicitly.
1150 */
1151 if (edev->pdev) {
1152 eeh_rmv_from_parent_pe(edev);
1153 eeh_addr_cache_rmv_dev(edev->pdev);
1154 eeh_sysfs_remove_device(edev->pdev);
Gavin Shanab55d212013-07-24 10:25:01 +08001155 edev->mode &= ~EEH_DEV_SYSFS;
Gavin Shanf5c57712013-07-24 10:24:58 +08001156
Gavin Shanf26c7a02014-01-12 14:13:45 +08001157 /*
1158 * We definitely should have the PCI device removed
1159 * though it wasn't correctly. So we needn't call
1160 * into error handler afterwards.
1161 */
1162 edev->mode |= EEH_DEV_NO_HANDLER;
1163
Gavin Shanf5c57712013-07-24 10:24:58 +08001164 edev->pdev = NULL;
1165 dev->dev.archdata.edev = NULL;
1166 }
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001167
Daniel Axtense642d112015-08-14 16:03:19 +10001168 if (eeh_has_flag(EEH_PROBE_MODE_DEV))
1169 eeh_ops->probe(pdn, NULL);
1170
Gavin Shanf631acd2012-02-27 20:04:07 +00001171 edev->pdev = dev;
1172 dev->dev.archdata.edev = edev;
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001173
Gavin Shan3ab96a02012-09-07 22:44:23 +00001174 eeh_addr_cache_insert_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
Nathan Fontenot794e0852006-03-31 12:04:52 -06001176
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001177/**
1178 * eeh_add_device_tree_late - Perform EEH initialization for the indicated PCI bus
1179 * @bus: PCI bus
1180 *
1181 * This routine must be used to perform EEH initialization for PCI
1182 * devices which are attached to the indicated PCI bus. The PCI bus
1183 * is added after system boot through hotplug or dlpar.
1184 */
Nathan Fontenot794e0852006-03-31 12:04:52 -06001185void eeh_add_device_tree_late(struct pci_bus *bus)
1186{
1187 struct pci_dev *dev;
1188
1189 list_for_each_entry(dev, &bus->devices, bus_list) {
Gavin Shana84f2732013-06-20 13:20:51 +08001190 eeh_add_device_late(dev);
1191 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
1192 struct pci_bus *subbus = dev->subordinate;
1193 if (subbus)
1194 eeh_add_device_tree_late(subbus);
1195 }
Nathan Fontenot794e0852006-03-31 12:04:52 -06001196 }
1197}
1198EXPORT_SYMBOL_GPL(eeh_add_device_tree_late);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200/**
Thadeu Lima de Souza Cascardo6a040ce2012-12-28 09:13:19 +00001201 * eeh_add_sysfs_files - Add EEH sysfs files for the indicated PCI bus
1202 * @bus: PCI bus
1203 *
1204 * This routine must be used to add EEH sysfs files for PCI
1205 * devices which are attached to the indicated PCI bus. The PCI bus
1206 * is added after system boot through hotplug or dlpar.
1207 */
1208void eeh_add_sysfs_files(struct pci_bus *bus)
1209{
1210 struct pci_dev *dev;
1211
1212 list_for_each_entry(dev, &bus->devices, bus_list) {
1213 eeh_sysfs_add_device(dev);
1214 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
1215 struct pci_bus *subbus = dev->subordinate;
1216 if (subbus)
1217 eeh_add_sysfs_files(subbus);
1218 }
1219 }
1220}
1221EXPORT_SYMBOL_GPL(eeh_add_sysfs_files);
1222
1223/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001224 * eeh_remove_device - Undo EEH setup for the indicated pci device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 * @dev: pci device to be removed
1226 *
Nathan Fontenot794e0852006-03-31 12:04:52 -06001227 * This routine should be called when a device is removed from
1228 * a running system (e.g. by hotplug or dlpar). It unregisters
1229 * the PCI device from the EEH subsystem. I/O errors affecting
1230 * this device will no longer be detected after this call; thus,
1231 * i/o errors affecting this slot may leave this device unusable.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 */
Gavin Shan807a8272013-07-24 10:24:55 +08001233void eeh_remove_device(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Gavin Shanf631acd2012-02-27 20:04:07 +00001235 struct eeh_dev *edev;
1236
Gavin Shan2ec5a0a2014-02-12 15:24:55 +08001237 if (!dev || !eeh_enabled())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return;
Gavin Shanf631acd2012-02-27 20:04:07 +00001239 edev = pci_dev_to_eeh_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 /* Unregister the device with the EEH/PCI address search system */
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001242 pr_debug("EEH: Removing device %s\n", pci_name(dev));
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001243
Gavin Shanf5c57712013-07-24 10:24:58 +08001244 if (!edev || !edev->pdev || !edev->pe) {
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001245 pr_debug("EEH: Not referenced !\n");
1246 return;
Linas Vepstasb055a9e2006-04-06 15:41:41 -05001247 }
Gavin Shanf5c57712013-07-24 10:24:58 +08001248
1249 /*
1250 * During the hotplug for EEH error recovery, we need the EEH
1251 * device attached to the parent PE in order for BAR restore
1252 * a bit later. So we keep it for BAR restore and remove it
1253 * from the parent PE during the BAR resotre.
1254 */
Gavin Shanf631acd2012-02-27 20:04:07 +00001255 edev->pdev = NULL;
Wei Yang67086e32016-03-04 10:53:11 +11001256
1257 /*
1258 * The flag "in_error" is used to trace EEH devices for VFs
1259 * in error state or not. It's set in eeh_report_error(). If
1260 * it's not set, eeh_report_{reset,resume}() won't be called
1261 * for the VF EEH device.
1262 */
1263 edev->in_error = false;
Gavin Shanf631acd2012-02-27 20:04:07 +00001264 dev->dev.archdata.edev = NULL;
Gavin Shanf5c57712013-07-24 10:24:58 +08001265 if (!(edev->pe->state & EEH_PE_KEEP))
1266 eeh_rmv_from_parent_pe(edev);
1267 else
1268 edev->mode |= EEH_DEV_DISCONNECTED;
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001269
Gavin Shanf26c7a02014-01-12 14:13:45 +08001270 /*
1271 * We're removing from the PCI subsystem, that means
1272 * the PCI device driver can't support EEH or not
1273 * well. So we rely on hotplug completely to do recovery
1274 * for the specific PCI device.
1275 */
1276 edev->mode |= EEH_DEV_NO_HANDLER;
1277
Gavin Shan3ab96a02012-09-07 22:44:23 +00001278 eeh_addr_cache_rmv_dev(dev);
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001279 eeh_sysfs_remove_device(dev);
Gavin Shanab55d212013-07-24 10:25:01 +08001280 edev->mode &= ~EEH_DEV_SYSFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Gavin Shan4eeeff02014-09-30 12:39:01 +10001283int eeh_unfreeze_pe(struct eeh_pe *pe, bool sw_state)
1284{
1285 int ret;
1286
1287 ret = eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
1288 if (ret) {
1289 pr_warn("%s: Failure %d enabling IO on PHB#%x-PE#%x\n",
1290 __func__, ret, pe->phb->global_number, pe->addr);
1291 return ret;
1292 }
1293
1294 ret = eeh_pci_enable(pe, EEH_OPT_THAW_DMA);
1295 if (ret) {
1296 pr_warn("%s: Failure %d enabling DMA on PHB#%x-PE#%x\n",
1297 __func__, ret, pe->phb->global_number, pe->addr);
1298 return ret;
1299 }
1300
1301 /* Clear software isolated state */
1302 if (sw_state && (pe->state & EEH_PE_ISOLATED))
1303 eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
1304
1305 return ret;
1306}
1307
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001308
1309static struct pci_device_id eeh_reset_ids[] = {
1310 { PCI_DEVICE(0x19a2, 0x0710) }, /* Emulex, BE */
1311 { PCI_DEVICE(0x10df, 0xe220) }, /* Emulex, Lancer */
Gavin Shanb1d76a72014-11-14 10:47:30 +11001312 { PCI_DEVICE(0x14e4, 0x1657) }, /* Broadcom BCM5719 */
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001313 { 0 }
1314};
1315
1316static int eeh_pe_change_owner(struct eeh_pe *pe)
1317{
1318 struct eeh_dev *edev, *tmp;
1319 struct pci_dev *pdev;
1320 struct pci_device_id *id;
1321 int flags, ret;
1322
1323 /* Check PE state */
1324 flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
1325 ret = eeh_ops->get_state(pe, NULL);
1326 if (ret < 0 || ret == EEH_STATE_NOT_SUPPORT)
1327 return 0;
1328
1329 /* Unfrozen PE, nothing to do */
1330 if ((ret & flags) == flags)
1331 return 0;
1332
1333 /* Frozen PE, check if it needs PE level reset */
1334 eeh_pe_for_each_dev(pe, edev, tmp) {
1335 pdev = eeh_dev_to_pci_dev(edev);
1336 if (!pdev)
1337 continue;
1338
1339 for (id = &eeh_reset_ids[0]; id->vendor != 0; id++) {
1340 if (id->vendor != PCI_ANY_ID &&
1341 id->vendor != pdev->vendor)
1342 continue;
1343 if (id->device != PCI_ANY_ID &&
1344 id->device != pdev->device)
1345 continue;
1346 if (id->subvendor != PCI_ANY_ID &&
1347 id->subvendor != pdev->subsystem_vendor)
1348 continue;
1349 if (id->subdevice != PCI_ANY_ID &&
1350 id->subdevice != pdev->subsystem_device)
1351 continue;
1352
Gavin Shand6d63d72016-04-27 11:14:53 +10001353 return eeh_pe_reset_and_recover(pe);
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001354 }
1355 }
1356
1357 return eeh_unfreeze_pe(pe, true);
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001358}
1359
Gavin Shan212d16c2014-06-10 11:41:56 +10001360/**
1361 * eeh_dev_open - Increase count of pass through devices for PE
1362 * @pdev: PCI device
1363 *
1364 * Increase count of passed through devices for the indicated
1365 * PE. In the result, the EEH errors detected on the PE won't be
1366 * reported. The PE owner will be responsible for detection
1367 * and recovery.
1368 */
1369int eeh_dev_open(struct pci_dev *pdev)
1370{
1371 struct eeh_dev *edev;
Gavin Shan404079c2014-09-30 12:38:54 +10001372 int ret = -ENODEV;
Gavin Shan212d16c2014-06-10 11:41:56 +10001373
1374 mutex_lock(&eeh_dev_mutex);
1375
1376 /* No PCI device ? */
1377 if (!pdev)
1378 goto out;
1379
1380 /* No EEH device or PE ? */
1381 edev = pci_dev_to_eeh_dev(pdev);
1382 if (!edev || !edev->pe)
1383 goto out;
1384
Gavin Shan404079c2014-09-30 12:38:54 +10001385 /*
1386 * The PE might have been put into frozen state, but we
1387 * didn't detect that yet. The passed through PCI devices
1388 * in frozen PE won't work properly. Clear the frozen state
1389 * in advance.
1390 */
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001391 ret = eeh_pe_change_owner(edev->pe);
Gavin Shan4eeeff02014-09-30 12:39:01 +10001392 if (ret)
1393 goto out;
Gavin Shan404079c2014-09-30 12:38:54 +10001394
Gavin Shan212d16c2014-06-10 11:41:56 +10001395 /* Increase PE's pass through count */
1396 atomic_inc(&edev->pe->pass_dev_cnt);
1397 mutex_unlock(&eeh_dev_mutex);
1398
1399 return 0;
1400out:
1401 mutex_unlock(&eeh_dev_mutex);
Gavin Shan404079c2014-09-30 12:38:54 +10001402 return ret;
Gavin Shan212d16c2014-06-10 11:41:56 +10001403}
1404EXPORT_SYMBOL_GPL(eeh_dev_open);
1405
1406/**
1407 * eeh_dev_release - Decrease count of pass through devices for PE
1408 * @pdev: PCI device
1409 *
1410 * Decrease count of pass through devices for the indicated PE. If
1411 * there is no passed through device in PE, the EEH errors detected
1412 * on the PE will be reported and handled as usual.
1413 */
1414void eeh_dev_release(struct pci_dev *pdev)
1415{
1416 struct eeh_dev *edev;
1417
1418 mutex_lock(&eeh_dev_mutex);
1419
1420 /* No PCI device ? */
1421 if (!pdev)
1422 goto out;
1423
1424 /* No EEH device ? */
1425 edev = pci_dev_to_eeh_dev(pdev);
1426 if (!edev || !edev->pe || !eeh_pe_passed(edev->pe))
1427 goto out;
1428
1429 /* Decrease PE's pass through count */
Gavin Shan54f9a642015-08-27 15:58:27 +10001430 WARN_ON(atomic_dec_if_positive(&edev->pe->pass_dev_cnt) < 0);
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001431 eeh_pe_change_owner(edev->pe);
Gavin Shan212d16c2014-06-10 11:41:56 +10001432out:
1433 mutex_unlock(&eeh_dev_mutex);
1434}
1435EXPORT_SYMBOL(eeh_dev_release);
1436
Benjamin Herrenschmidt2194dc22014-08-05 18:52:59 +10001437#ifdef CONFIG_IOMMU_API
1438
Gavin Shana3032ca2014-07-15 17:00:56 +10001439static int dev_has_iommu_table(struct device *dev, void *data)
1440{
1441 struct pci_dev *pdev = to_pci_dev(dev);
1442 struct pci_dev **ppdev = data;
Gavin Shana3032ca2014-07-15 17:00:56 +10001443
1444 if (!dev)
1445 return 0;
1446
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10001447 if (dev->iommu_group) {
Gavin Shana3032ca2014-07-15 17:00:56 +10001448 *ppdev = pdev;
1449 return 1;
1450 }
1451
1452 return 0;
1453}
1454
Gavin Shan212d16c2014-06-10 11:41:56 +10001455/**
1456 * eeh_iommu_group_to_pe - Convert IOMMU group to EEH PE
1457 * @group: IOMMU group
1458 *
1459 * The routine is called to convert IOMMU group to EEH PE.
1460 */
1461struct eeh_pe *eeh_iommu_group_to_pe(struct iommu_group *group)
1462{
Gavin Shan212d16c2014-06-10 11:41:56 +10001463 struct pci_dev *pdev = NULL;
1464 struct eeh_dev *edev;
Gavin Shana3032ca2014-07-15 17:00:56 +10001465 int ret;
Gavin Shan212d16c2014-06-10 11:41:56 +10001466
1467 /* No IOMMU group ? */
1468 if (!group)
1469 return NULL;
1470
Gavin Shana3032ca2014-07-15 17:00:56 +10001471 ret = iommu_group_for_each_dev(group, &pdev, dev_has_iommu_table);
1472 if (!ret || !pdev)
Gavin Shan212d16c2014-06-10 11:41:56 +10001473 return NULL;
1474
1475 /* No EEH device or PE ? */
1476 edev = pci_dev_to_eeh_dev(pdev);
1477 if (!edev || !edev->pe)
1478 return NULL;
1479
1480 return edev->pe;
1481}
Gavin Shan537e5402014-08-07 12:47:16 +10001482EXPORT_SYMBOL_GPL(eeh_iommu_group_to_pe);
Gavin Shan212d16c2014-06-10 11:41:56 +10001483
Benjamin Herrenschmidt2194dc22014-08-05 18:52:59 +10001484#endif /* CONFIG_IOMMU_API */
1485
Gavin Shan212d16c2014-06-10 11:41:56 +10001486/**
1487 * eeh_pe_set_option - Set options for the indicated PE
1488 * @pe: EEH PE
1489 * @option: requested option
1490 *
1491 * The routine is called to enable or disable EEH functionality
1492 * on the indicated PE, to enable IO or DMA for the frozen PE.
1493 */
1494int eeh_pe_set_option(struct eeh_pe *pe, int option)
1495{
1496 int ret = 0;
1497
1498 /* Invalid PE ? */
1499 if (!pe)
1500 return -ENODEV;
1501
1502 /*
1503 * EEH functionality could possibly be disabled, just
1504 * return error for the case. And the EEH functinality
1505 * isn't expected to be disabled on one specific PE.
1506 */
1507 switch (option) {
1508 case EEH_OPT_ENABLE:
Gavin Shan4eeeff02014-09-30 12:39:01 +10001509 if (eeh_enabled()) {
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001510 ret = eeh_pe_change_owner(pe);
Gavin Shan212d16c2014-06-10 11:41:56 +10001511 break;
Gavin Shan4eeeff02014-09-30 12:39:01 +10001512 }
Gavin Shan212d16c2014-06-10 11:41:56 +10001513 ret = -EIO;
1514 break;
1515 case EEH_OPT_DISABLE:
1516 break;
1517 case EEH_OPT_THAW_MMIO:
1518 case EEH_OPT_THAW_DMA:
Gavin Shande5a6622016-09-28 14:34:53 +10001519 case EEH_OPT_FREEZE_PE:
Gavin Shan212d16c2014-06-10 11:41:56 +10001520 if (!eeh_ops || !eeh_ops->set_option) {
1521 ret = -ENOENT;
1522 break;
1523 }
1524
Gavin Shan4eeeff02014-09-30 12:39:01 +10001525 ret = eeh_pci_enable(pe, option);
Gavin Shan212d16c2014-06-10 11:41:56 +10001526 break;
1527 default:
1528 pr_debug("%s: Option %d out of range (%d, %d)\n",
1529 __func__, option, EEH_OPT_DISABLE, EEH_OPT_THAW_DMA);
1530 ret = -EINVAL;
1531 }
1532
1533 return ret;
1534}
1535EXPORT_SYMBOL_GPL(eeh_pe_set_option);
1536
1537/**
1538 * eeh_pe_get_state - Retrieve PE's state
1539 * @pe: EEH PE
1540 *
1541 * Retrieve the PE's state, which includes 3 aspects: enabled
1542 * DMA, enabled IO and asserted reset.
1543 */
1544int eeh_pe_get_state(struct eeh_pe *pe)
1545{
1546 int result, ret = 0;
1547 bool rst_active, dma_en, mmio_en;
1548
1549 /* Existing PE ? */
1550 if (!pe)
1551 return -ENODEV;
1552
1553 if (!eeh_ops || !eeh_ops->get_state)
1554 return -ENOENT;
1555
Gavin Shaneca036e2016-03-04 10:53:14 +11001556 /*
1557 * If the parent PE is owned by the host kernel and is undergoing
1558 * error recovery, we should return the PE state as temporarily
1559 * unavailable so that the error recovery on the guest is suspended
1560 * until the recovery completes on the host.
1561 */
1562 if (pe->parent &&
1563 !(pe->state & EEH_PE_REMOVED) &&
1564 (pe->parent->state & (EEH_PE_ISOLATED | EEH_PE_RECOVERING)))
1565 return EEH_PE_STATE_UNAVAIL;
1566
Gavin Shan212d16c2014-06-10 11:41:56 +10001567 result = eeh_ops->get_state(pe, NULL);
1568 rst_active = !!(result & EEH_STATE_RESET_ACTIVE);
1569 dma_en = !!(result & EEH_STATE_DMA_ENABLED);
1570 mmio_en = !!(result & EEH_STATE_MMIO_ENABLED);
1571
1572 if (rst_active)
1573 ret = EEH_PE_STATE_RESET;
1574 else if (dma_en && mmio_en)
1575 ret = EEH_PE_STATE_NORMAL;
1576 else if (!dma_en && !mmio_en)
1577 ret = EEH_PE_STATE_STOPPED_IO_DMA;
1578 else if (!dma_en && mmio_en)
1579 ret = EEH_PE_STATE_STOPPED_DMA;
1580 else
1581 ret = EEH_PE_STATE_UNAVAIL;
1582
1583 return ret;
1584}
1585EXPORT_SYMBOL_GPL(eeh_pe_get_state);
1586
Gavin Shan316233f2014-09-30 12:38:53 +10001587static int eeh_pe_reenable_devices(struct eeh_pe *pe)
1588{
1589 struct eeh_dev *edev, *tmp;
1590 struct pci_dev *pdev;
1591 int ret = 0;
1592
1593 /* Restore config space */
1594 eeh_pe_restore_bars(pe);
1595
1596 /*
1597 * Reenable PCI devices as the devices passed
1598 * through are always enabled before the reset.
1599 */
1600 eeh_pe_for_each_dev(pe, edev, tmp) {
1601 pdev = eeh_dev_to_pci_dev(edev);
1602 if (!pdev)
1603 continue;
1604
1605 ret = pci_reenable_device(pdev);
1606 if (ret) {
1607 pr_warn("%s: Failure %d reenabling %s\n",
1608 __func__, ret, pci_name(pdev));
1609 return ret;
1610 }
1611 }
1612
1613 /* The PE is still in frozen state */
Gavin Shanc9dd0142014-09-30 12:39:02 +10001614 return eeh_unfreeze_pe(pe, true);
Gavin Shan316233f2014-09-30 12:38:53 +10001615}
1616
Gavin Shan212d16c2014-06-10 11:41:56 +10001617/**
1618 * eeh_pe_reset - Issue PE reset according to specified type
1619 * @pe: EEH PE
1620 * @option: reset type
1621 *
1622 * The routine is called to reset the specified PE with the
1623 * indicated type, either fundamental reset or hot reset.
1624 * PE reset is the most important part for error recovery.
1625 */
1626int eeh_pe_reset(struct eeh_pe *pe, int option)
1627{
1628 int ret = 0;
1629
1630 /* Invalid PE ? */
1631 if (!pe)
1632 return -ENODEV;
1633
1634 if (!eeh_ops || !eeh_ops->set_option || !eeh_ops->reset)
1635 return -ENOENT;
1636
1637 switch (option) {
1638 case EEH_RESET_DEACTIVATE:
1639 ret = eeh_ops->reset(pe, option);
Gavin Shan8a6b3712014-10-01 17:07:50 +10001640 eeh_pe_state_clear(pe, EEH_PE_CFG_BLOCKED);
Gavin Shan212d16c2014-06-10 11:41:56 +10001641 if (ret)
1642 break;
1643
Gavin Shan316233f2014-09-30 12:38:53 +10001644 ret = eeh_pe_reenable_devices(pe);
Gavin Shan212d16c2014-06-10 11:41:56 +10001645 break;
1646 case EEH_RESET_HOT:
1647 case EEH_RESET_FUNDAMENTAL:
Gavin Shan0d5ee522014-09-30 12:38:52 +10001648 /*
1649 * Proactively freeze the PE to drop all MMIO access
1650 * during reset, which should be banned as it's always
1651 * cause recursive EEH error.
1652 */
1653 eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);
1654
Gavin Shan8a6b3712014-10-01 17:07:50 +10001655 eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
Gavin Shan212d16c2014-06-10 11:41:56 +10001656 ret = eeh_ops->reset(pe, option);
1657 break;
1658 default:
1659 pr_debug("%s: Unsupported option %d\n",
1660 __func__, option);
1661 ret = -EINVAL;
1662 }
1663
1664 return ret;
1665}
1666EXPORT_SYMBOL_GPL(eeh_pe_reset);
1667
1668/**
1669 * eeh_pe_configure - Configure PCI bridges after PE reset
1670 * @pe: EEH PE
1671 *
1672 * The routine is called to restore the PCI config space for
1673 * those PCI devices, especially PCI bridges affected by PE
1674 * reset issued previously.
1675 */
1676int eeh_pe_configure(struct eeh_pe *pe)
1677{
1678 int ret = 0;
1679
1680 /* Invalid PE ? */
1681 if (!pe)
1682 return -ENODEV;
1683
Gavin Shan212d16c2014-06-10 11:41:56 +10001684 return ret;
1685}
1686EXPORT_SYMBOL_GPL(eeh_pe_configure);
1687
Gavin Shanec33d362015-03-26 16:42:08 +11001688/**
1689 * eeh_pe_inject_err - Injecting the specified PCI error to the indicated PE
1690 * @pe: the indicated PE
1691 * @type: error type
1692 * @function: error function
1693 * @addr: address
1694 * @mask: address mask
1695 *
1696 * The routine is called to inject the specified PCI error, which
1697 * is determined by @type and @function, to the indicated PE for
1698 * testing purpose.
1699 */
1700int eeh_pe_inject_err(struct eeh_pe *pe, int type, int func,
1701 unsigned long addr, unsigned long mask)
1702{
1703 /* Invalid PE ? */
1704 if (!pe)
1705 return -ENODEV;
1706
1707 /* Unsupported operation ? */
1708 if (!eeh_ops || !eeh_ops->err_inject)
1709 return -ENOENT;
1710
1711 /* Check on PCI error type */
1712 if (type != EEH_ERR_TYPE_32 && type != EEH_ERR_TYPE_64)
1713 return -EINVAL;
1714
1715 /* Check on PCI error function */
1716 if (func < EEH_ERR_FUNC_MIN || func > EEH_ERR_FUNC_MAX)
1717 return -EINVAL;
1718
1719 return eeh_ops->err_inject(pe, type, func, addr, mask);
1720}
1721EXPORT_SYMBOL_GPL(eeh_pe_inject_err);
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723static int proc_eeh_show(struct seq_file *m, void *v)
1724{
Gavin Shan2ec5a0a2014-02-12 15:24:55 +08001725 if (!eeh_enabled()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 seq_printf(m, "EEH Subsystem is globally disabled\n");
Gavin Shane575f8d2012-02-29 15:47:45 +00001727 seq_printf(m, "eeh_total_mmio_ffs=%llu\n", eeh_stats.total_mmio_ffs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 } else {
1729 seq_printf(m, "EEH Subsystem is enabled\n");
Linas Vepstas177bc932005-11-03 18:48:52 -06001730 seq_printf(m,
Gavin Shane575f8d2012-02-29 15:47:45 +00001731 "no device=%llu\n"
1732 "no device node=%llu\n"
1733 "no config address=%llu\n"
1734 "check not wanted=%llu\n"
1735 "eeh_total_mmio_ffs=%llu\n"
1736 "eeh_false_positives=%llu\n"
1737 "eeh_slot_resets=%llu\n",
1738 eeh_stats.no_device,
1739 eeh_stats.no_dn,
1740 eeh_stats.no_cfg_addr,
1741 eeh_stats.ignored_check,
1742 eeh_stats.total_mmio_ffs,
1743 eeh_stats.false_positives,
1744 eeh_stats.slot_resets);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
1746
1747 return 0;
1748}
1749
1750static int proc_eeh_open(struct inode *inode, struct file *file)
1751{
1752 return single_open(file, proc_eeh_show, NULL);
1753}
1754
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001755static const struct file_operations proc_eeh_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 .open = proc_eeh_open,
1757 .read = seq_read,
1758 .llseek = seq_lseek,
1759 .release = single_release,
1760};
1761
Gavin Shan7f52a522014-04-24 18:00:18 +10001762#ifdef CONFIG_DEBUG_FS
1763static int eeh_enable_dbgfs_set(void *data, u64 val)
1764{
1765 if (val)
Gavin Shan05b17212014-07-17 14:41:38 +10001766 eeh_clear_flag(EEH_FORCE_DISABLED);
Gavin Shan7f52a522014-04-24 18:00:18 +10001767 else
Gavin Shan05b17212014-07-17 14:41:38 +10001768 eeh_add_flag(EEH_FORCE_DISABLED);
Gavin Shan7f52a522014-04-24 18:00:18 +10001769
1770 /* Notify the backend */
1771 if (eeh_ops->post_init)
1772 eeh_ops->post_init();
1773
1774 return 0;
1775}
1776
1777static int eeh_enable_dbgfs_get(void *data, u64 *val)
1778{
1779 if (eeh_enabled())
1780 *val = 0x1ul;
1781 else
1782 *val = 0x0ul;
1783 return 0;
1784}
1785
Gavin Shan1b28f172014-12-11 14:28:56 +11001786static int eeh_freeze_dbgfs_set(void *data, u64 val)
1787{
1788 eeh_max_freezes = val;
1789 return 0;
1790}
1791
1792static int eeh_freeze_dbgfs_get(void *data, u64 *val)
1793{
1794 *val = eeh_max_freezes;
1795 return 0;
1796}
1797
Gavin Shan7f52a522014-04-24 18:00:18 +10001798DEFINE_SIMPLE_ATTRIBUTE(eeh_enable_dbgfs_ops, eeh_enable_dbgfs_get,
1799 eeh_enable_dbgfs_set, "0x%llx\n");
Gavin Shan1b28f172014-12-11 14:28:56 +11001800DEFINE_SIMPLE_ATTRIBUTE(eeh_freeze_dbgfs_ops, eeh_freeze_dbgfs_get,
1801 eeh_freeze_dbgfs_set, "0x%llx\n");
Gavin Shan7f52a522014-04-24 18:00:18 +10001802#endif
1803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804static int __init eeh_init_proc(void)
1805{
Gavin Shan7f52a522014-04-24 18:00:18 +10001806 if (machine_is(pseries) || machine_is(powernv)) {
Thadeu Lima de Souza Cascardo8feaa432011-08-26 10:36:31 +00001807 proc_create("powerpc/eeh", 0, NULL, &proc_eeh_operations);
Gavin Shan7f52a522014-04-24 18:00:18 +10001808#ifdef CONFIG_DEBUG_FS
1809 debugfs_create_file("eeh_enable", 0600,
1810 powerpc_debugfs_root, NULL,
1811 &eeh_enable_dbgfs_ops);
Gavin Shan1b28f172014-12-11 14:28:56 +11001812 debugfs_create_file("eeh_max_freezes", 0600,
1813 powerpc_debugfs_root, NULL,
1814 &eeh_freeze_dbgfs_ops);
Gavin Shan7f52a522014-04-24 18:00:18 +10001815#endif
1816 }
1817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 return 0;
1819}
1820__initcall(eeh_init_proc);