blob: a7f229e598926ea552a659602dbdbd188d4e0f70 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Oliver O'Halloranbce3e3e2019-07-11 01:05:17 +1000366 pa = pte_pfn(*ptep);
367
368 /* On radix we can do hugepage mappings for io, so handle that */
369 if (hugepage_shift) {
370 pa <<= hugepage_shift;
371 pa |= token & ((1ul << hugepage_shift) - 1);
372 } else {
373 pa <<= PAGE_SHIFT;
374 pa |= token & (PAGE_SIZE - 1);
375 }
376
377 return pa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800380/*
381 * On PowerNV platform, we might already have fenced PHB there.
382 * For that case, it's meaningless to recover frozen PE. Intead,
383 * We have to handle fenced PHB firstly.
384 */
385static int eeh_phb_check_failure(struct eeh_pe *pe)
386{
387 struct eeh_pe *phb_pe;
388 unsigned long flags;
389 int ret;
390
Gavin Shan05b17212014-07-17 14:41:38 +1000391 if (!eeh_has_flag(EEH_PROBE_MODE_DEV))
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800392 return -EPERM;
393
394 /* Find the PHB PE */
395 phb_pe = eeh_phb_pe_get(pe->phb);
396 if (!phb_pe) {
Gavin Shan0dae2742014-07-17 14:41:41 +1000397 pr_warn("%s Can't find PE for PHB#%d\n",
398 __func__, pe->phb->global_number);
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800399 return -EEXIST;
400 }
401
402 /* If the PHB has been in problematic state */
403 eeh_serialize_lock(&flags);
Gavin Shan9e049372014-04-24 18:00:07 +1000404 if (phb_pe->state & EEH_PE_ISOLATED) {
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800405 ret = 0;
406 goto out;
407 }
408
409 /* Check PHB state */
410 ret = eeh_ops->get_state(phb_pe, NULL);
411 if ((ret < 0) ||
412 (ret == EEH_STATE_NOT_SUPPORT) ||
413 (ret & (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) ==
414 (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) {
415 ret = 0;
416 goto out;
417 }
418
419 /* Isolate the PHB and send event */
420 eeh_pe_state_mark(phb_pe, EEH_PE_ISOLATED);
421 eeh_serialize_unlock(flags);
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800422
Gavin Shan357b2f32014-06-11 18:26:44 +1000423 pr_err("EEH: PHB#%x failure detected, location: %s\n",
424 phb_pe->phb->global_number, eeh_pe_loc_get(phb_pe));
Gavin Shan56ca4fd2013-06-27 13:46:46 +0800425 dump_stack();
Gavin Shan5293bf92013-09-06 09:00:05 +0800426 eeh_send_failure_event(phb_pe);
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800427
428 return 1;
429out:
430 eeh_serialize_unlock(flags);
431 return ret;
432}
433
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000434/**
Gavin Shanf8f7d632012-09-07 22:44:22 +0000435 * eeh_dev_check_failure - Check if all 1's data is due to EEH slot freeze
436 * @edev: eeh device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 *
438 * Check for an EEH failure for the given device node. Call this
439 * routine if the result of a read was all 0xff's and you want to
440 * find out if this is due to an EEH slot freeze. This routine
441 * will query firmware for the EEH status.
442 *
443 * Returns 0 if there has not been an EEH error; otherwise returns
Linas Vepstas69376502005-11-03 18:47:50 -0600444 * a non-zero value and queues up a slot isolation event notification.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 *
446 * It is safe to call this routine in an interrupt context.
447 */
Gavin Shanf8f7d632012-09-07 22:44:22 +0000448int eeh_dev_check_failure(struct eeh_dev *edev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 int ret;
Gavin Shan1ad7a722014-05-05 09:29:03 +1000451 int active_flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 unsigned long flags;
Gavin Shanc6406d82015-03-17 16:15:08 +1100453 struct pci_dn *pdn;
Gavin Shanf8f7d632012-09-07 22:44:22 +0000454 struct pci_dev *dev;
Gavin Shan357b2f32014-06-11 18:26:44 +1000455 struct eeh_pe *pe, *parent_pe, *phb_pe;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600456 int rc = 0;
Gavin Shanc6406d82015-03-17 16:15:08 +1100457 const char *location = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Gavin Shane575f8d2012-02-29 15:47:45 +0000459 eeh_stats.total_mmio_ffs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Gavin Shan2ec5a0a2014-02-12 15:24:55 +0800461 if (!eeh_enabled())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return 0;
463
Gavin Shanf8f7d632012-09-07 22:44:22 +0000464 if (!edev) {
Gavin Shane575f8d2012-02-29 15:47:45 +0000465 eeh_stats.no_dn++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return 0;
Linas Vepstas177bc932005-11-03 18:48:52 -0600467 }
Gavin Shanf8f7d632012-09-07 22:44:22 +0000468 dev = eeh_dev_to_pci_dev(edev);
Wei Yang2a582222014-09-17 10:48:26 +0800469 pe = eeh_dev_to_pe(edev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 /* Access to IO BARs might get this far and still not want checking. */
Gavin Shan66523d92012-09-07 22:44:13 +0000472 if (!pe) {
Gavin Shane575f8d2012-02-29 15:47:45 +0000473 eeh_stats.ignored_check++;
Gavin Shanc6406d82015-03-17 16:15:08 +1100474 pr_debug("EEH: Ignored check for %s\n",
475 eeh_pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return 0;
477 }
478
Gavin Shan66523d92012-09-07 22:44:13 +0000479 if (!pe->addr && !pe->config_addr) {
Gavin Shane575f8d2012-02-29 15:47:45 +0000480 eeh_stats.no_cfg_addr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return 0;
482 }
483
Gavin Shanb95cd2c2013-06-20 13:21:16 +0800484 /*
485 * On PowerNV platform, we might already have fenced PHB
486 * there and we need take care of that firstly.
487 */
488 ret = eeh_phb_check_failure(pe);
489 if (ret > 0)
490 return ret;
491
Gavin Shan05ec4242014-06-10 11:41:55 +1000492 /*
493 * If the PE isn't owned by us, we shouldn't check the
494 * state. Instead, let the owner handle it if the PE has
495 * been frozen.
496 */
497 if (eeh_pe_passed(pe))
498 return 0;
499
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600500 /* If we already have a pending isolation event for this
501 * slot, we know it's bad already, we don't need to check.
502 * Do this checking under a lock; as multiple PCI devices
503 * in one slot might report errors simultaneously, and we
504 * only want one error recovery routine running.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 */
Gavin Shan49075812013-06-20 13:21:03 +0800506 eeh_serialize_lock(&flags);
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600507 rc = 1;
Gavin Shan66523d92012-09-07 22:44:13 +0000508 if (pe->state & EEH_PE_ISOLATED) {
509 pe->check_count++;
510 if (pe->check_count % EEH_MAX_FAILS == 0) {
Gavin Shanc6406d82015-03-17 16:15:08 +1100511 pdn = eeh_dev_to_pdn(edev);
512 if (pdn->node)
513 location = of_get_property(pdn->node, "ibm,loc-code", NULL);
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000514 printk(KERN_ERR "EEH: %d reads ignored for recovering device at "
Mike Masonf36c5222008-07-22 02:40:17 +1000515 "location=%s driver=%s pci addr=%s\n",
Gavin Shanc6406d82015-03-17 16:15:08 +1100516 pe->check_count,
517 location ? location : "unknown",
Thadeu Lima de Souza Cascardo778a7852012-01-11 09:09:58 +0000518 eeh_driver_name(dev), eeh_pci_name(dev));
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000519 printk(KERN_ERR "EEH: Might be infinite loop in %s driver\n",
Thadeu Lima de Souza Cascardo778a7852012-01-11 09:09:58 +0000520 eeh_driver_name(dev));
Linas Vepstas5c1344e2005-11-03 18:49:31 -0600521 dump_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600523 goto dn_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
525
526 /*
527 * Now test for an EEH failure. This is VERY expensive.
528 * Note that the eeh_config_addr may be a parent device
529 * in the case of a device behind a bridge, or it may be
530 * function zero of a multi-function device.
531 * In any case they must share a common PHB.
532 */
Gavin Shan66523d92012-09-07 22:44:13 +0000533 ret = eeh_ops->get_state(pe, NULL);
Linas Vepstas76e6faf2005-11-03 18:49:15 -0600534
Linas Vepstas39d16e22007-03-19 14:51:00 -0500535 /* Note that config-io to empty slots may fail;
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000536 * they are empty when they don't have children.
Gavin Shaneb594a42012-02-27 20:03:57 +0000537 * We will punt with the following conditions: Failure to get
538 * PE's state, EEH not support and Permanently unavailable
539 * state, PE is in good state.
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000540 */
Gavin Shaneb594a42012-02-27 20:03:57 +0000541 if ((ret < 0) ||
542 (ret == EEH_STATE_NOT_SUPPORT) ||
Gavin Shan1ad7a722014-05-05 09:29:03 +1000543 ((ret & active_flags) == active_flags)) {
Gavin Shane575f8d2012-02-29 15:47:45 +0000544 eeh_stats.false_positives++;
Gavin Shan66523d92012-09-07 22:44:13 +0000545 pe->false_positives++;
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600546 rc = 0;
547 goto dn_unlock;
Linas Vepstas76e6faf2005-11-03 18:49:15 -0600548 }
549
Gavin Shan1ad7a722014-05-05 09:29:03 +1000550 /*
551 * It should be corner case that the parent PE has been
552 * put into frozen state as well. We should take care
553 * that at first.
554 */
555 parent_pe = pe->parent;
556 while (parent_pe) {
557 /* Hit the ceiling ? */
558 if (parent_pe->type & EEH_PE_PHB)
559 break;
560
561 /* Frozen parent PE ? */
562 ret = eeh_ops->get_state(parent_pe, NULL);
563 if (ret > 0 &&
564 (ret & active_flags) != active_flags)
565 pe = parent_pe;
566
567 /* Next parent level */
568 parent_pe = parent_pe->parent;
569 }
570
Gavin Shane575f8d2012-02-29 15:47:45 +0000571 eeh_stats.slot_resets++;
Gavin Shana84f2732013-06-20 13:20:51 +0800572
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600573 /* Avoid repeated reports of this failure, including problems
574 * with other functions on this device, and functions under
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000575 * bridges.
576 */
Gavin Shan66523d92012-09-07 22:44:13 +0000577 eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
Gavin Shan49075812013-06-20 13:21:03 +0800578 eeh_serialize_unlock(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 /* Most EEH events are due to device driver bugs. Having
581 * a stack trace will help the device-driver authors figure
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000582 * out what happened. So print that out.
583 */
Gavin Shan357b2f32014-06-11 18:26:44 +1000584 phb_pe = eeh_phb_pe_get(pe->phb);
585 pr_err("EEH: Frozen PHB#%x-PE#%x detected\n",
586 pe->phb->global_number, pe->addr);
587 pr_err("EEH: PE location: %s, PHB location: %s\n",
588 eeh_pe_loc_get(pe), eeh_pe_loc_get(phb_pe));
Gavin Shan56ca4fd2013-06-27 13:46:46 +0800589 dump_stack();
590
Gavin Shan5293bf92013-09-06 09:00:05 +0800591 eeh_send_failure_event(pe);
592
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600593 return 1;
594
595dn_unlock:
Gavin Shan49075812013-06-20 13:21:03 +0800596 eeh_serialize_unlock(flags);
Linas Vepstasfd761fd2005-11-03 18:49:23 -0600597 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
Gavin Shanf8f7d632012-09-07 22:44:22 +0000600EXPORT_SYMBOL_GPL(eeh_dev_check_failure);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000603 * eeh_check_failure - Check if all 1's data is due to EEH slot freeze
Gavin Shan3e938052014-09-30 12:38:50 +1000604 * @token: I/O address
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 *
Gavin Shan3e938052014-09-30 12:38:50 +1000606 * Check for an EEH failure at the given I/O address. Call this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 * routine if the result of a read was all 0xff's and you want to
Gavin Shan3e938052014-09-30 12:38:50 +1000608 * find out if this is due to an EEH slot freeze event. This routine
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 * will query firmware for the EEH status.
610 *
611 * Note this routine is safe to call in an interrupt context.
612 */
Gavin Shan3e938052014-09-30 12:38:50 +1000613int eeh_check_failure(const volatile void __iomem *token)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614{
615 unsigned long addr;
Gavin Shanf8f7d632012-09-07 22:44:22 +0000616 struct eeh_dev *edev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 /* Finding the phys addr + pci device; this is pretty quick. */
619 addr = eeh_token_to_phys((unsigned long __force) token);
Gavin Shan3ab96a02012-09-07 22:44:23 +0000620 edev = eeh_addr_cache_get_dev(addr);
Gavin Shanf8f7d632012-09-07 22:44:22 +0000621 if (!edev) {
Gavin Shane575f8d2012-02-29 15:47:45 +0000622 eeh_stats.no_device++;
Gavin Shan3e938052014-09-30 12:38:50 +1000623 return 0;
Linas Vepstas177bc932005-11-03 18:48:52 -0600624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Gavin Shan3e938052014-09-30 12:38:50 +1000626 return eeh_dev_check_failure(edev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628EXPORT_SYMBOL(eeh_check_failure);
629
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600630
Linas Vepstascb5b56242006-09-15 18:56:35 -0500631/**
Gavin Shancce4b2d2012-02-27 20:03:52 +0000632 * eeh_pci_enable - Enable MMIO or DMA transfers for this slot
Gavin Shanff477962012-09-07 22:44:16 +0000633 * @pe: EEH PE
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000634 *
635 * This routine should be called to reenable frozen MMIO or DMA
636 * so that it would work correctly again. It's useful while doing
637 * recovery or log collection on the indicated device.
Linas Vepstas47b5c832006-09-15 18:57:42 -0500638 */
Gavin Shanff477962012-09-07 22:44:16 +0000639int eeh_pci_enable(struct eeh_pe *pe, int function)
Linas Vepstas47b5c832006-09-15 18:57:42 -0500640{
Gavin Shan4d4f5772014-09-30 12:39:00 +1000641 int active_flag, rc;
Gavin Shan78954702014-04-24 18:00:14 +1000642
643 /*
644 * pHyp doesn't allow to enable IO or DMA on unfrozen PE.
645 * Also, it's pointless to enable them on unfrozen PE. So
Gavin Shan4d4f5772014-09-30 12:39:00 +1000646 * we have to check before enabling IO or DMA.
Gavin Shan78954702014-04-24 18:00:14 +1000647 */
Gavin Shan4d4f5772014-09-30 12:39:00 +1000648 switch (function) {
649 case EEH_OPT_THAW_MMIO:
Gavin Shan872ee2d2015-10-08 14:58:55 +1100650 active_flag = EEH_STATE_MMIO_ACTIVE | EEH_STATE_MMIO_ENABLED;
Gavin Shan4d4f5772014-09-30 12:39:00 +1000651 break;
652 case EEH_OPT_THAW_DMA:
653 active_flag = EEH_STATE_DMA_ACTIVE;
654 break;
655 case EEH_OPT_DISABLE:
656 case EEH_OPT_ENABLE:
657 case EEH_OPT_FREEZE_PE:
658 active_flag = 0;
659 break;
660 default:
661 pr_warn("%s: Invalid function %d\n",
662 __func__, function);
663 return -EINVAL;
664 }
665
666 /*
667 * Check if IO or DMA has been enabled before
668 * enabling them.
669 */
670 if (active_flag) {
Gavin Shan78954702014-04-24 18:00:14 +1000671 rc = eeh_ops->get_state(pe, NULL);
672 if (rc < 0)
673 return rc;
674
Gavin Shan4d4f5772014-09-30 12:39:00 +1000675 /* Needn't enable it at all */
676 if (rc == EEH_STATE_NOT_SUPPORT)
677 return 0;
678
679 /* It's already enabled */
680 if (rc & active_flag)
Gavin Shan78954702014-04-24 18:00:14 +1000681 return 0;
682 }
Linas Vepstas47b5c832006-09-15 18:57:42 -0500683
Gavin Shan4d4f5772014-09-30 12:39:00 +1000684
685 /* Issue the request */
Gavin Shanff477962012-09-07 22:44:16 +0000686 rc = eeh_ops->set_option(pe, function);
Linas Vepstas47b5c832006-09-15 18:57:42 -0500687 if (rc)
Gavin Shan78954702014-04-24 18:00:14 +1000688 pr_warn("%s: Unexpected state change %d on "
689 "PHB#%d-PE#%x, err=%d\n",
690 __func__, function, pe->phb->global_number,
691 pe->addr, rc);
Linas Vepstas47b5c832006-09-15 18:57:42 -0500692
Gavin Shan4d4f5772014-09-30 12:39:00 +1000693 /* Check if the request is finished successfully */
694 if (active_flag) {
695 rc = eeh_ops->wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
Andrew Donnellan949e9b82015-10-23 17:19:46 +1100696 if (rc < 0)
Gavin Shan4d4f5772014-09-30 12:39:00 +1000697 return rc;
Gavin Shan78954702014-04-24 18:00:14 +1000698
Gavin Shan4d4f5772014-09-30 12:39:00 +1000699 if (rc & active_flag)
700 return 0;
Gavin Shan78954702014-04-24 18:00:14 +1000701
Gavin Shan4d4f5772014-09-30 12:39:00 +1000702 return -EIO;
703 }
Linas Vepstasfa1be472007-03-19 14:59:59 -0500704
Linas Vepstas47b5c832006-09-15 18:57:42 -0500705 return rc;
706}
707
Gavin Shan28158cd2015-02-11 10:20:49 +1100708static void *eeh_disable_and_save_dev_state(void *data, void *userdata)
709{
710 struct eeh_dev *edev = data;
711 struct pci_dev *pdev = eeh_dev_to_pci_dev(edev);
712 struct pci_dev *dev = userdata;
713
714 /*
715 * The caller should have disabled and saved the
716 * state for the specified device
717 */
718 if (!pdev || pdev == dev)
719 return NULL;
720
721 /* Ensure we have D0 power state */
722 pci_set_power_state(pdev, PCI_D0);
723
724 /* Save device state */
725 pci_save_state(pdev);
726
727 /*
728 * Disable device to avoid any DMA traffic and
729 * interrupt from the device
730 */
731 pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
732
733 return NULL;
734}
735
736static void *eeh_restore_dev_state(void *data, void *userdata)
737{
738 struct eeh_dev *edev = data;
Gavin Shan0bd78582015-03-17 16:15:07 +1100739 struct pci_dn *pdn = eeh_dev_to_pdn(edev);
Gavin Shan28158cd2015-02-11 10:20:49 +1100740 struct pci_dev *pdev = eeh_dev_to_pci_dev(edev);
741 struct pci_dev *dev = userdata;
742
743 if (!pdev)
744 return NULL;
745
746 /* Apply customization from firmware */
Gavin Shan0bd78582015-03-17 16:15:07 +1100747 if (pdn && eeh_ops->restore_config)
748 eeh_ops->restore_config(pdn);
Gavin Shan28158cd2015-02-11 10:20:49 +1100749
750 /* The caller should restore state for the specified device */
751 if (pdev != dev)
David Gibson502f1592015-06-03 14:52:59 +1000752 pci_restore_state(pdev);
Gavin Shan28158cd2015-02-11 10:20:49 +1100753
754 return NULL;
755}
756
Linas Vepstas47b5c832006-09-15 18:57:42 -0500757/**
Andrew Donnellan31f6a4a2016-02-08 14:39:19 +1100758 * pcibios_set_pcie_reset_state - Set PCI-E reset state
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000759 * @dev: pci device struct
760 * @state: reset state to enter
Brian King00c2ae32007-05-08 08:04:05 +1000761 *
762 * Return value:
763 * 0 if success
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000764 */
Brian King00c2ae32007-05-08 08:04:05 +1000765int pcibios_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
766{
Gavin Shanc270a242012-09-07 22:44:17 +0000767 struct eeh_dev *edev = pci_dev_to_eeh_dev(dev);
Wei Yang2a582222014-09-17 10:48:26 +0800768 struct eeh_pe *pe = eeh_dev_to_pe(edev);
Gavin Shanc270a242012-09-07 22:44:17 +0000769
770 if (!pe) {
771 pr_err("%s: No PE found on PCI device %s\n",
772 __func__, pci_name(dev));
773 return -EINVAL;
774 }
Brian King00c2ae32007-05-08 08:04:05 +1000775
776 switch (state) {
777 case pcie_deassert_reset:
Gavin Shanc270a242012-09-07 22:44:17 +0000778 eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);
Gavin Shan28158cd2015-02-11 10:20:49 +1100779 eeh_unfreeze_pe(pe, false);
Wei Yang9312bc52016-03-04 10:53:09 +1100780 if (!(pe->type & EEH_PE_VF))
781 eeh_pe_state_clear(pe, EEH_PE_CFG_BLOCKED);
Gavin Shan28158cd2015-02-11 10:20:49 +1100782 eeh_pe_dev_traverse(pe, eeh_restore_dev_state, dev);
Gavin Shan1ae79b72015-05-01 09:14:11 +1000783 eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
Brian King00c2ae32007-05-08 08:04:05 +1000784 break;
785 case pcie_hot_reset:
Gavin Shan39bfd712015-07-30 09:26:51 +1000786 eeh_pe_state_mark_with_cfg(pe, EEH_PE_ISOLATED);
Gavin Shan28158cd2015-02-11 10:20:49 +1100787 eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);
788 eeh_pe_dev_traverse(pe, eeh_disable_and_save_dev_state, dev);
Wei Yang9312bc52016-03-04 10:53:09 +1100789 if (!(pe->type & EEH_PE_VF))
790 eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
Gavin Shanc270a242012-09-07 22:44:17 +0000791 eeh_ops->reset(pe, EEH_RESET_HOT);
Brian King00c2ae32007-05-08 08:04:05 +1000792 break;
793 case pcie_warm_reset:
Gavin Shan39bfd712015-07-30 09:26:51 +1000794 eeh_pe_state_mark_with_cfg(pe, EEH_PE_ISOLATED);
Gavin Shan28158cd2015-02-11 10:20:49 +1100795 eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);
796 eeh_pe_dev_traverse(pe, eeh_disable_and_save_dev_state, dev);
Wei Yang9312bc52016-03-04 10:53:09 +1100797 if (!(pe->type & EEH_PE_VF))
798 eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
Gavin Shanc270a242012-09-07 22:44:17 +0000799 eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
Brian King00c2ae32007-05-08 08:04:05 +1000800 break;
801 default:
Gavin Shan1ae79b72015-05-01 09:14:11 +1000802 eeh_pe_state_clear(pe, EEH_PE_ISOLATED | EEH_PE_CFG_BLOCKED);
Brian King00c2ae32007-05-08 08:04:05 +1000803 return -EINVAL;
804 };
805
806 return 0;
807}
808
809/**
Gavin Shanc270a242012-09-07 22:44:17 +0000810 * eeh_set_pe_freset - Check the required reset for the indicated device
811 * @data: EEH device
812 * @flag: return value
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000813 *
814 * Each device might have its preferred reset type: fundamental or
815 * hot reset. The routine is used to collected the information for
816 * the indicated device and its children so that the bunch of the
817 * devices could be reset properly.
818 */
Gavin Shanc270a242012-09-07 22:44:17 +0000819static void *eeh_set_dev_freset(void *data, void *flag)
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000820{
821 struct pci_dev *dev;
Gavin Shanc270a242012-09-07 22:44:17 +0000822 unsigned int *freset = (unsigned int *)flag;
823 struct eeh_dev *edev = (struct eeh_dev *)data;
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000824
Gavin Shanc270a242012-09-07 22:44:17 +0000825 dev = eeh_dev_to_pci_dev(edev);
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000826 if (dev)
827 *freset |= dev->needs_freset;
828
Gavin Shanc270a242012-09-07 22:44:17 +0000829 return NULL;
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000830}
831
832/**
Gavin Shancce4b2d2012-02-27 20:03:52 +0000833 * eeh_reset_pe_once - Assert the pci #RST line for 1/4 second
Gavin Shanc270a242012-09-07 22:44:17 +0000834 * @pe: EEH PE
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000835 *
836 * Assert the PCI #RST line for 1/4 second.
837 */
Gavin Shanc270a242012-09-07 22:44:17 +0000838static void eeh_reset_pe_once(struct eeh_pe *pe)
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600839{
Richard A Lary308fc4f2011-04-22 09:59:47 +0000840 unsigned int freset = 0;
Mike Mason6e193142009-07-30 15:42:39 -0700841
Richard A Lary308fc4f2011-04-22 09:59:47 +0000842 /* Determine type of EEH reset required for
843 * Partitionable Endpoint, a hot-reset (1)
844 * or a fundamental reset (3).
845 * A fundamental reset required by any device under
846 * Partitionable Endpoint trumps hot-reset.
Gavin Shana84f2732013-06-20 13:20:51 +0800847 */
Gavin Shanc270a242012-09-07 22:44:17 +0000848 eeh_pe_dev_traverse(pe, eeh_set_dev_freset, &freset);
Richard A Lary308fc4f2011-04-22 09:59:47 +0000849
850 if (freset)
Gavin Shanc270a242012-09-07 22:44:17 +0000851 eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
Mike Mason6e193142009-07-30 15:42:39 -0700852 else
Gavin Shanc270a242012-09-07 22:44:17 +0000853 eeh_ops->reset(pe, EEH_RESET_HOT);
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600854
Gavin Shanc270a242012-09-07 22:44:17 +0000855 eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);
Linas Vepstase1029262006-09-21 18:25:56 -0500856}
857
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000858/**
Gavin Shancce4b2d2012-02-27 20:03:52 +0000859 * eeh_reset_pe - Reset the indicated PE
Gavin Shanc270a242012-09-07 22:44:17 +0000860 * @pe: EEH PE
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000861 *
862 * This routine should be called to reset indicated device, including
863 * PE. A PE might include multiple PCI devices and sometimes PCI bridges
864 * might be involved as well.
865 */
Gavin Shanc270a242012-09-07 22:44:17 +0000866int eeh_reset_pe(struct eeh_pe *pe)
Linas Vepstase1029262006-09-21 18:25:56 -0500867{
Gavin Shan326a98e2013-06-20 13:20:58 +0800868 int flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
Gavin Shanb85743e2014-11-14 10:47:28 +1100869 int i, state, ret;
Linas Vepstase1029262006-09-21 18:25:56 -0500870
Gavin Shan28bf36f2014-11-14 10:47:29 +1100871 /* Mark as reset and block config space */
872 eeh_pe_state_mark(pe, EEH_PE_RESET | EEH_PE_CFG_BLOCKED);
873
Linas Vepstas9c547762007-03-19 14:58:07 -0500874 /* Take three shots at resetting the bus */
Gavin Shanb85743e2014-11-14 10:47:28 +1100875 for (i = 0; i < 3; i++) {
Gavin Shanc270a242012-09-07 22:44:17 +0000876 eeh_reset_pe_once(pe);
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600877
Gavin Shan78954702014-04-24 18:00:14 +1000878 /*
879 * EEH_PE_ISOLATED is expected to be removed after
880 * BAR restore.
881 */
Gavin Shanb85743e2014-11-14 10:47:28 +1100882 state = eeh_ops->wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
883 if ((state & flags) == flags) {
884 ret = 0;
885 goto out;
Linas Vepstase1029262006-09-21 18:25:56 -0500886 }
Gavin Shanb85743e2014-11-14 10:47:28 +1100887
888 if (state < 0) {
889 pr_warn("%s: Unrecoverable slot failure on PHB#%d-PE#%x",
890 __func__, pe->phb->global_number, pe->addr);
891 ret = -ENOTRECOVERABLE;
892 goto out;
893 }
894
895 /* We might run out of credits */
896 ret = -EIO;
897 pr_warn("%s: Failure %d resetting PHB#%x-PE#%x\n (%d)\n",
898 __func__, state, pe->phb->global_number, pe->addr, (i + 1));
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600899 }
Linas Vepstasb6495c02005-11-03 18:54:54 -0600900
Gavin Shanb85743e2014-11-14 10:47:28 +1100901out:
Gavin Shan28bf36f2014-11-14 10:47:29 +1100902 eeh_pe_state_clear(pe, EEH_PE_RESET | EEH_PE_CFG_BLOCKED);
Gavin Shanb85743e2014-11-14 10:47:28 +1100903 return ret;
Linas Vepstas6dee3fb2005-11-03 18:50:10 -0600904}
905
Linas Vepstas8b553f32005-11-03 18:50:17 -0600906/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000907 * eeh_save_bars - Save device bars
Gavin Shanf631acd2012-02-27 20:04:07 +0000908 * @edev: PCI device associated EEH device
Linas Vepstas8b553f32005-11-03 18:50:17 -0600909 *
910 * Save the values of the device bars. Unlike the restore
911 * routine, this routine is *not* recursive. This is because
Justin Mattock31116f02011-02-24 20:10:18 +0000912 * PCI devices are added individually; but, for the restore,
Linas Vepstas8b553f32005-11-03 18:50:17 -0600913 * an entire slot is reset at a time.
914 */
Gavin Shand7bb8862012-09-07 22:44:21 +0000915void eeh_save_bars(struct eeh_dev *edev)
Linas Vepstas8b553f32005-11-03 18:50:17 -0600916{
Gavin Shan0bd78582015-03-17 16:15:07 +1100917 struct pci_dn *pdn;
Linas Vepstas8b553f32005-11-03 18:50:17 -0600918 int i;
919
Gavin Shan0bd78582015-03-17 16:15:07 +1100920 pdn = eeh_dev_to_pdn(edev);
921 if (!pdn)
Linas Vepstas8b553f32005-11-03 18:50:17 -0600922 return;
Gavin Shana84f2732013-06-20 13:20:51 +0800923
Linas Vepstas8b553f32005-11-03 18:50:17 -0600924 for (i = 0; i < 16; i++)
Gavin Shan0bd78582015-03-17 16:15:07 +1100925 eeh_ops->read_config(pdn, i * 4, 4, &edev->config_space[i]);
Gavin Shanbf898ec2013-11-12 14:49:21 +0800926
927 /*
928 * For PCI bridges including root port, we need enable bus
929 * master explicitly. Otherwise, it can't fetch IODA table
930 * entries correctly. So we cache the bit in advance so that
931 * we can restore it after reset, either PHB range or PE range.
932 */
933 if (edev->mode & EEH_DEV_BRIDGE)
934 edev->config_space[1] |= PCI_COMMAND_MASTER;
Linas Vepstas8b553f32005-11-03 18:50:17 -0600935}
936
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000937/**
Gavin Shanaa1e6372012-02-27 20:03:53 +0000938 * eeh_ops_register - Register platform dependent EEH operations
939 * @ops: platform dependent EEH operations
940 *
941 * Register the platform dependent EEH operation callback
942 * functions. The platform should call this function before
943 * any other EEH operations.
944 */
945int __init eeh_ops_register(struct eeh_ops *ops)
946{
947 if (!ops->name) {
Gavin Shan0dae2742014-07-17 14:41:41 +1000948 pr_warn("%s: Invalid EEH ops name for %p\n",
Gavin Shanaa1e6372012-02-27 20:03:53 +0000949 __func__, ops);
950 return -EINVAL;
951 }
952
953 if (eeh_ops && eeh_ops != ops) {
Gavin Shan0dae2742014-07-17 14:41:41 +1000954 pr_warn("%s: EEH ops of platform %s already existing (%s)\n",
Gavin Shanaa1e6372012-02-27 20:03:53 +0000955 __func__, eeh_ops->name, ops->name);
956 return -EEXIST;
957 }
958
959 eeh_ops = ops;
960
961 return 0;
962}
963
964/**
965 * eeh_ops_unregister - Unreigster platform dependent EEH operations
966 * @name: name of EEH platform operations
967 *
968 * Unregister the platform dependent EEH operation callback
969 * functions.
970 */
971int __exit eeh_ops_unregister(const char *name)
972{
973 if (!name || !strlen(name)) {
Gavin Shan0dae2742014-07-17 14:41:41 +1000974 pr_warn("%s: Invalid EEH ops name\n",
Gavin Shanaa1e6372012-02-27 20:03:53 +0000975 __func__);
976 return -EINVAL;
977 }
978
979 if (eeh_ops && !strcmp(eeh_ops->name, name)) {
980 eeh_ops = NULL;
981 return 0;
982 }
983
984 return -EEXIST;
985}
986
Gavin Shan66f9af832014-02-12 15:24:56 +0800987static int eeh_reboot_notifier(struct notifier_block *nb,
988 unsigned long action, void *unused)
989{
Gavin Shan05b17212014-07-17 14:41:38 +1000990 eeh_clear_flag(EEH_ENABLED);
Gavin Shan66f9af832014-02-12 15:24:56 +0800991 return NOTIFY_DONE;
992}
993
994static struct notifier_block eeh_reboot_nb = {
995 .notifier_call = eeh_reboot_notifier,
996};
997
Gavin Shanaa1e6372012-02-27 20:03:53 +0000998/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +0000999 * eeh_init - EEH initialization
1000 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 * Initialize EEH by trying to enable it for all of the adapters in the system.
1002 * As a side effect we can determine here if eeh is supported at all.
1003 * Note that we leave EEH on so failed config cycles won't cause a machine
1004 * check. If a user turns off EEH for a particular adapter they are really
1005 * telling Linux to ignore errors. Some hardware (e.g. POWER5) won't
1006 * grant access to a slot if EEH isn't enabled, and so we always enable
1007 * EEH for all slots/all devices.
1008 *
1009 * The eeh-force-off option disables EEH checking globally, for all slots.
1010 * Even if force-off is set, the EEH hardware is still enabled, so that
1011 * newer systems can boot.
1012 */
Gavin Shaneeb63612013-06-27 13:46:47 +08001013int eeh_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
Gavin Shan1a5c2e62012-03-20 21:30:29 +00001015 struct pci_controller *hose, *tmp;
Gavin Shanff57b452015-03-17 16:15:06 +11001016 struct pci_dn *pdn;
Gavin Shan51fb5f52013-06-20 13:20:56 +08001017 static int cnt = 0;
1018 int ret = 0;
1019
1020 /*
1021 * We have to delay the initialization on PowerNV after
1022 * the PCI hierarchy tree has been built because the PEs
1023 * are figured out based on PCI devices instead of device
1024 * tree nodes
1025 */
1026 if (machine_is(powernv) && cnt++ <= 0)
1027 return ret;
Gavin Shane2af1552012-02-27 20:03:54 +00001028
Gavin Shan66f9af832014-02-12 15:24:56 +08001029 /* Register reboot notifier */
1030 ret = register_reboot_notifier(&eeh_reboot_nb);
1031 if (ret) {
1032 pr_warn("%s: Failed to register notifier (%d)\n",
1033 __func__, ret);
1034 return ret;
1035 }
1036
Gavin Shane2af1552012-02-27 20:03:54 +00001037 /* call platform initialization function */
1038 if (!eeh_ops) {
Gavin Shan0dae2742014-07-17 14:41:41 +10001039 pr_warn("%s: Platform EEH operation not found\n",
Gavin Shane2af1552012-02-27 20:03:54 +00001040 __func__);
Gavin Shan35e5cfe2012-09-07 22:44:02 +00001041 return -EEXIST;
Greg Kurz221195f2014-11-25 17:10:06 +01001042 } else if ((ret = eeh_ops->init()))
Gavin Shan35e5cfe2012-09-07 22:44:02 +00001043 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Gavin Shanc8608552013-06-20 13:21:00 +08001045 /* Initialize EEH event */
1046 ret = eeh_event_init();
1047 if (ret)
1048 return ret;
1049
Gavin Shan1a5c2e62012-03-20 21:30:29 +00001050 /* Enable EEH for all adapters */
Gavin Shanff57b452015-03-17 16:15:06 +11001051 list_for_each_entry_safe(hose, tmp, &hose_list, list_node) {
1052 pdn = hose->pci_data;
1053 traverse_pci_dn(pdn, eeh_ops->probe, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
1055
Gavin Shan21fd21f2013-06-20 13:20:57 +08001056 /*
1057 * Call platform post-initialization. Actually, It's good chance
1058 * to inform platform that EEH is ready to supply service if the
1059 * I/O cache stuff has been built up.
1060 */
1061 if (eeh_ops->post_init) {
1062 ret = eeh_ops->post_init();
1063 if (ret)
1064 return ret;
1065 }
1066
Gavin Shan2ec5a0a2014-02-12 15:24:55 +08001067 if (eeh_enabled())
Gavin Shand7bb8862012-09-07 22:44:21 +00001068 pr_info("EEH: PCI Enhanced I/O Error Handling Enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 else
Anton Blanchard91ac7302016-10-02 11:09:38 +11001070 pr_info("EEH: No capable adapters found\n");
Gavin Shan35e5cfe2012-09-07 22:44:02 +00001071
1072 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
Gavin Shan35e5cfe2012-09-07 22:44:02 +00001075core_initcall_sync(eeh_init);
1076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077/**
Gavin Shanc6406d82015-03-17 16:15:08 +11001078 * eeh_add_device_early - Enable EEH for the indicated device node
Gavin Shanff57b452015-03-17 16:15:06 +11001079 * @pdn: PCI device node for which to set up EEH
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 *
1081 * This routine must be used to perform EEH initialization for PCI
1082 * devices that were added after system boot (e.g. hotplug, dlpar).
1083 * This routine must be called before any i/o is performed to the
1084 * adapter (inluding any config-space i/o).
1085 * Whether this actually enables EEH or not for this device depends
1086 * on the CEC architecture, type of the device, on earlier boot
1087 * command-line arguments & etc.
1088 */
Gavin Shanff57b452015-03-17 16:15:06 +11001089void eeh_add_device_early(struct pci_dn *pdn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 struct pci_controller *phb;
Gavin Shanff57b452015-03-17 16:15:06 +11001092 struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Guilherme G. Piccolic2078d92016-04-11 16:17:22 -03001094 if (!edev)
Gavin Shan26a74852013-06-20 13:20:59 +08001095 return;
1096
Gavin Shand91dafc2015-05-01 09:22:15 +10001097 if (!eeh_has_flag(EEH_PROBE_MODE_DEVTREE))
1098 return;
1099
Linas Vepstasf751f842005-11-03 18:54:23 -06001100 /* USB Bus children of PCI devices will not have BUID's */
Gavin Shanff57b452015-03-17 16:15:06 +11001101 phb = edev->phb;
1102 if (NULL == phb ||
1103 (eeh_has_flag(EEH_PROBE_MODE_DEVTREE) && 0 == phb->buid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Gavin Shanff57b452015-03-17 16:15:06 +11001106 eeh_ops->probe(pdn, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001109/**
1110 * eeh_add_device_tree_early - Enable EEH for the indicated device
Gavin Shanff57b452015-03-17 16:15:06 +11001111 * @pdn: PCI device node
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001112 *
1113 * This routine must be used to perform EEH initialization for the
1114 * indicated PCI device that was added after system boot (e.g.
1115 * hotplug, dlpar).
1116 */
Gavin Shanff57b452015-03-17 16:15:06 +11001117void eeh_add_device_tree_early(struct pci_dn *pdn)
Linas Vepstase2a296e2005-11-03 18:51:31 -06001118{
Gavin Shanff57b452015-03-17 16:15:06 +11001119 struct pci_dn *n;
Stephen Rothwellacaa6172007-12-21 15:52:07 +11001120
Gavin Shanff57b452015-03-17 16:15:06 +11001121 if (!pdn)
1122 return;
1123
1124 list_for_each_entry(n, &pdn->child_list, list)
1125 eeh_add_device_tree_early(n);
1126 eeh_add_device_early(pdn);
Linas Vepstase2a296e2005-11-03 18:51:31 -06001127}
1128EXPORT_SYMBOL_GPL(eeh_add_device_tree_early);
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001131 * eeh_add_device_late - Perform EEH initialization for the indicated pci device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 * @dev: pci device for which to set up EEH
1133 *
1134 * This routine must be used to complete EEH initialization for PCI
1135 * devices that were added after system boot (e.g. hotplug, dlpar).
1136 */
Gavin Shanf2856492013-07-24 10:24:52 +08001137void eeh_add_device_late(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Gavin Shanc6406d82015-03-17 16:15:08 +11001139 struct pci_dn *pdn;
Gavin Shanf631acd2012-02-27 20:04:07 +00001140 struct eeh_dev *edev;
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001141
Gavin Shan2ec5a0a2014-02-12 15:24:55 +08001142 if (!dev || !eeh_enabled())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 return;
1144
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001145 pr_debug("EEH: Adding device %s\n", pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Gavin Shanc6406d82015-03-17 16:15:08 +11001147 pdn = pci_get_pdn_by_devfn(dev->bus, dev->devfn);
1148 edev = pdn_to_eeh_dev(pdn);
Gavin Shanf631acd2012-02-27 20:04:07 +00001149 if (edev->pdev == dev) {
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001150 pr_debug("EEH: Already referenced !\n");
1151 return;
1152 }
Gavin Shanf5c57712013-07-24 10:24:58 +08001153
1154 /*
1155 * The EEH cache might not be removed correctly because of
1156 * unbalanced kref to the device during unplug time, which
1157 * relies on pcibios_release_device(). So we have to remove
1158 * that here explicitly.
1159 */
1160 if (edev->pdev) {
1161 eeh_rmv_from_parent_pe(edev);
1162 eeh_addr_cache_rmv_dev(edev->pdev);
1163 eeh_sysfs_remove_device(edev->pdev);
Gavin Shanab55d212013-07-24 10:25:01 +08001164 edev->mode &= ~EEH_DEV_SYSFS;
Gavin Shanf5c57712013-07-24 10:24:58 +08001165
Gavin Shanf26c7a02014-01-12 14:13:45 +08001166 /*
1167 * We definitely should have the PCI device removed
1168 * though it wasn't correctly. So we needn't call
1169 * into error handler afterwards.
1170 */
1171 edev->mode |= EEH_DEV_NO_HANDLER;
1172
Gavin Shanf5c57712013-07-24 10:24:58 +08001173 edev->pdev = NULL;
1174 dev->dev.archdata.edev = NULL;
1175 }
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001176
Daniel Axtense642d112015-08-14 16:03:19 +10001177 if (eeh_has_flag(EEH_PROBE_MODE_DEV))
1178 eeh_ops->probe(pdn, NULL);
1179
Gavin Shanf631acd2012-02-27 20:04:07 +00001180 edev->pdev = dev;
1181 dev->dev.archdata.edev = edev;
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001182
Gavin Shan3ab96a02012-09-07 22:44:23 +00001183 eeh_addr_cache_insert_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184}
Nathan Fontenot794e0852006-03-31 12:04:52 -06001185
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001186/**
1187 * eeh_add_device_tree_late - Perform EEH initialization for the indicated PCI bus
1188 * @bus: PCI bus
1189 *
1190 * This routine must be used to perform EEH initialization for PCI
1191 * devices which are attached to the indicated PCI bus. The PCI bus
1192 * is added after system boot through hotplug or dlpar.
1193 */
Nathan Fontenot794e0852006-03-31 12:04:52 -06001194void eeh_add_device_tree_late(struct pci_bus *bus)
1195{
1196 struct pci_dev *dev;
1197
1198 list_for_each_entry(dev, &bus->devices, bus_list) {
Gavin Shana84f2732013-06-20 13:20:51 +08001199 eeh_add_device_late(dev);
1200 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
1201 struct pci_bus *subbus = dev->subordinate;
1202 if (subbus)
1203 eeh_add_device_tree_late(subbus);
1204 }
Nathan Fontenot794e0852006-03-31 12:04:52 -06001205 }
1206}
1207EXPORT_SYMBOL_GPL(eeh_add_device_tree_late);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209/**
Thadeu Lima de Souza Cascardo6a040ce2012-12-28 09:13:19 +00001210 * eeh_add_sysfs_files - Add EEH sysfs files for the indicated PCI bus
1211 * @bus: PCI bus
1212 *
1213 * This routine must be used to add EEH sysfs files for PCI
1214 * devices which are attached to the indicated PCI bus. The PCI bus
1215 * is added after system boot through hotplug or dlpar.
1216 */
1217void eeh_add_sysfs_files(struct pci_bus *bus)
1218{
1219 struct pci_dev *dev;
1220
1221 list_for_each_entry(dev, &bus->devices, bus_list) {
1222 eeh_sysfs_add_device(dev);
1223 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
1224 struct pci_bus *subbus = dev->subordinate;
1225 if (subbus)
1226 eeh_add_sysfs_files(subbus);
1227 }
1228 }
1229}
1230EXPORT_SYMBOL_GPL(eeh_add_sysfs_files);
1231
1232/**
Gavin Shancb3bc9d2012-02-27 20:03:51 +00001233 * eeh_remove_device - Undo EEH setup for the indicated pci device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 * @dev: pci device to be removed
1235 *
Nathan Fontenot794e0852006-03-31 12:04:52 -06001236 * This routine should be called when a device is removed from
1237 * a running system (e.g. by hotplug or dlpar). It unregisters
1238 * the PCI device from the EEH subsystem. I/O errors affecting
1239 * this device will no longer be detected after this call; thus,
1240 * i/o errors affecting this slot may leave this device unusable.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 */
Gavin Shan807a8272013-07-24 10:24:55 +08001242void eeh_remove_device(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
Gavin Shanf631acd2012-02-27 20:04:07 +00001244 struct eeh_dev *edev;
1245
Gavin Shan2ec5a0a2014-02-12 15:24:55 +08001246 if (!dev || !eeh_enabled())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 return;
Gavin Shanf631acd2012-02-27 20:04:07 +00001248 edev = pci_dev_to_eeh_dev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 /* Unregister the device with the EEH/PCI address search system */
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001251 pr_debug("EEH: Removing device %s\n", pci_name(dev));
Linas Vepstas56b0fca2005-11-03 18:48:45 -06001252
Gavin Shanf5c57712013-07-24 10:24:58 +08001253 if (!edev || !edev->pdev || !edev->pe) {
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001254 pr_debug("EEH: Not referenced !\n");
1255 return;
Linas Vepstasb055a9e2006-04-06 15:41:41 -05001256 }
Gavin Shanf5c57712013-07-24 10:24:58 +08001257
1258 /*
1259 * During the hotplug for EEH error recovery, we need the EEH
1260 * device attached to the parent PE in order for BAR restore
1261 * a bit later. So we keep it for BAR restore and remove it
1262 * from the parent PE during the BAR resotre.
1263 */
Gavin Shanf631acd2012-02-27 20:04:07 +00001264 edev->pdev = NULL;
Wei Yang67086e32016-03-04 10:53:11 +11001265
1266 /*
1267 * The flag "in_error" is used to trace EEH devices for VFs
1268 * in error state or not. It's set in eeh_report_error(). If
1269 * it's not set, eeh_report_{reset,resume}() won't be called
1270 * for the VF EEH device.
1271 */
1272 edev->in_error = false;
Gavin Shanf631acd2012-02-27 20:04:07 +00001273 dev->dev.archdata.edev = NULL;
Gavin Shanf5c57712013-07-24 10:24:58 +08001274 if (!(edev->pe->state & EEH_PE_KEEP))
1275 eeh_rmv_from_parent_pe(edev);
1276 else
1277 edev->mode |= EEH_DEV_DISCONNECTED;
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001278
Gavin Shanf26c7a02014-01-12 14:13:45 +08001279 /*
1280 * We're removing from the PCI subsystem, that means
1281 * the PCI device driver can't support EEH or not
1282 * well. So we rely on hotplug completely to do recovery
1283 * for the specific PCI device.
1284 */
1285 edev->mode |= EEH_DEV_NO_HANDLER;
1286
Gavin Shan3ab96a02012-09-07 22:44:23 +00001287 eeh_addr_cache_rmv_dev(dev);
Benjamin Herrenschmidt57b066f2008-10-27 19:48:41 +00001288 eeh_sysfs_remove_device(dev);
Gavin Shanab55d212013-07-24 10:25:01 +08001289 edev->mode &= ~EEH_DEV_SYSFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Gavin Shan4eeeff02014-09-30 12:39:01 +10001292int eeh_unfreeze_pe(struct eeh_pe *pe, bool sw_state)
1293{
1294 int ret;
1295
1296 ret = eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
1297 if (ret) {
1298 pr_warn("%s: Failure %d enabling IO on PHB#%x-PE#%x\n",
1299 __func__, ret, pe->phb->global_number, pe->addr);
1300 return ret;
1301 }
1302
1303 ret = eeh_pci_enable(pe, EEH_OPT_THAW_DMA);
1304 if (ret) {
1305 pr_warn("%s: Failure %d enabling DMA on PHB#%x-PE#%x\n",
1306 __func__, ret, pe->phb->global_number, pe->addr);
1307 return ret;
1308 }
1309
1310 /* Clear software isolated state */
1311 if (sw_state && (pe->state & EEH_PE_ISOLATED))
1312 eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
1313
1314 return ret;
1315}
1316
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001317
1318static struct pci_device_id eeh_reset_ids[] = {
1319 { PCI_DEVICE(0x19a2, 0x0710) }, /* Emulex, BE */
1320 { PCI_DEVICE(0x10df, 0xe220) }, /* Emulex, Lancer */
Gavin Shanb1d76a72014-11-14 10:47:30 +11001321 { PCI_DEVICE(0x14e4, 0x1657) }, /* Broadcom BCM5719 */
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001322 { 0 }
1323};
1324
1325static int eeh_pe_change_owner(struct eeh_pe *pe)
1326{
1327 struct eeh_dev *edev, *tmp;
1328 struct pci_dev *pdev;
1329 struct pci_device_id *id;
1330 int flags, ret;
1331
1332 /* Check PE state */
1333 flags = (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE);
1334 ret = eeh_ops->get_state(pe, NULL);
1335 if (ret < 0 || ret == EEH_STATE_NOT_SUPPORT)
1336 return 0;
1337
1338 /* Unfrozen PE, nothing to do */
1339 if ((ret & flags) == flags)
1340 return 0;
1341
1342 /* Frozen PE, check if it needs PE level reset */
1343 eeh_pe_for_each_dev(pe, edev, tmp) {
1344 pdev = eeh_dev_to_pci_dev(edev);
1345 if (!pdev)
1346 continue;
1347
1348 for (id = &eeh_reset_ids[0]; id->vendor != 0; id++) {
1349 if (id->vendor != PCI_ANY_ID &&
1350 id->vendor != pdev->vendor)
1351 continue;
1352 if (id->device != PCI_ANY_ID &&
1353 id->device != pdev->device)
1354 continue;
1355 if (id->subvendor != PCI_ANY_ID &&
1356 id->subvendor != pdev->subsystem_vendor)
1357 continue;
1358 if (id->subdevice != PCI_ANY_ID &&
1359 id->subdevice != pdev->subsystem_device)
1360 continue;
1361
Gavin Shand6d63d72016-04-27 11:14:53 +10001362 return eeh_pe_reset_and_recover(pe);
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001363 }
1364 }
1365
1366 return eeh_unfreeze_pe(pe, true);
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001367}
1368
Gavin Shan212d16c2014-06-10 11:41:56 +10001369/**
1370 * eeh_dev_open - Increase count of pass through devices for PE
1371 * @pdev: PCI device
1372 *
1373 * Increase count of passed through devices for the indicated
1374 * PE. In the result, the EEH errors detected on the PE won't be
1375 * reported. The PE owner will be responsible for detection
1376 * and recovery.
1377 */
1378int eeh_dev_open(struct pci_dev *pdev)
1379{
1380 struct eeh_dev *edev;
Gavin Shan404079c2014-09-30 12:38:54 +10001381 int ret = -ENODEV;
Gavin Shan212d16c2014-06-10 11:41:56 +10001382
1383 mutex_lock(&eeh_dev_mutex);
1384
1385 /* No PCI device ? */
1386 if (!pdev)
1387 goto out;
1388
1389 /* No EEH device or PE ? */
1390 edev = pci_dev_to_eeh_dev(pdev);
1391 if (!edev || !edev->pe)
1392 goto out;
1393
Gavin Shan404079c2014-09-30 12:38:54 +10001394 /*
1395 * The PE might have been put into frozen state, but we
1396 * didn't detect that yet. The passed through PCI devices
1397 * in frozen PE won't work properly. Clear the frozen state
1398 * in advance.
1399 */
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001400 ret = eeh_pe_change_owner(edev->pe);
Gavin Shan4eeeff02014-09-30 12:39:01 +10001401 if (ret)
1402 goto out;
Gavin Shan404079c2014-09-30 12:38:54 +10001403
Gavin Shan212d16c2014-06-10 11:41:56 +10001404 /* Increase PE's pass through count */
1405 atomic_inc(&edev->pe->pass_dev_cnt);
1406 mutex_unlock(&eeh_dev_mutex);
1407
1408 return 0;
1409out:
1410 mutex_unlock(&eeh_dev_mutex);
Gavin Shan404079c2014-09-30 12:38:54 +10001411 return ret;
Gavin Shan212d16c2014-06-10 11:41:56 +10001412}
1413EXPORT_SYMBOL_GPL(eeh_dev_open);
1414
1415/**
1416 * eeh_dev_release - Decrease count of pass through devices for PE
1417 * @pdev: PCI device
1418 *
1419 * Decrease count of pass through devices for the indicated PE. If
1420 * there is no passed through device in PE, the EEH errors detected
1421 * on the PE will be reported and handled as usual.
1422 */
1423void eeh_dev_release(struct pci_dev *pdev)
1424{
1425 struct eeh_dev *edev;
1426
1427 mutex_lock(&eeh_dev_mutex);
1428
1429 /* No PCI device ? */
1430 if (!pdev)
1431 goto out;
1432
1433 /* No EEH device ? */
1434 edev = pci_dev_to_eeh_dev(pdev);
1435 if (!edev || !edev->pe || !eeh_pe_passed(edev->pe))
1436 goto out;
1437
1438 /* Decrease PE's pass through count */
Gavin Shan54f9a642015-08-27 15:58:27 +10001439 WARN_ON(atomic_dec_if_positive(&edev->pe->pass_dev_cnt) < 0);
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001440 eeh_pe_change_owner(edev->pe);
Gavin Shan212d16c2014-06-10 11:41:56 +10001441out:
1442 mutex_unlock(&eeh_dev_mutex);
1443}
1444EXPORT_SYMBOL(eeh_dev_release);
1445
Benjamin Herrenschmidt2194dc22014-08-05 18:52:59 +10001446#ifdef CONFIG_IOMMU_API
1447
Gavin Shana3032ca2014-07-15 17:00:56 +10001448static int dev_has_iommu_table(struct device *dev, void *data)
1449{
1450 struct pci_dev *pdev = to_pci_dev(dev);
1451 struct pci_dev **ppdev = data;
Gavin Shana3032ca2014-07-15 17:00:56 +10001452
1453 if (!dev)
1454 return 0;
1455
Alexey Kardashevskiyea30e992015-06-05 16:34:53 +10001456 if (dev->iommu_group) {
Gavin Shana3032ca2014-07-15 17:00:56 +10001457 *ppdev = pdev;
1458 return 1;
1459 }
1460
1461 return 0;
1462}
1463
Gavin Shan212d16c2014-06-10 11:41:56 +10001464/**
1465 * eeh_iommu_group_to_pe - Convert IOMMU group to EEH PE
1466 * @group: IOMMU group
1467 *
1468 * The routine is called to convert IOMMU group to EEH PE.
1469 */
1470struct eeh_pe *eeh_iommu_group_to_pe(struct iommu_group *group)
1471{
Gavin Shan212d16c2014-06-10 11:41:56 +10001472 struct pci_dev *pdev = NULL;
1473 struct eeh_dev *edev;
Gavin Shana3032ca2014-07-15 17:00:56 +10001474 int ret;
Gavin Shan212d16c2014-06-10 11:41:56 +10001475
1476 /* No IOMMU group ? */
1477 if (!group)
1478 return NULL;
1479
Gavin Shana3032ca2014-07-15 17:00:56 +10001480 ret = iommu_group_for_each_dev(group, &pdev, dev_has_iommu_table);
1481 if (!ret || !pdev)
Gavin Shan212d16c2014-06-10 11:41:56 +10001482 return NULL;
1483
1484 /* No EEH device or PE ? */
1485 edev = pci_dev_to_eeh_dev(pdev);
1486 if (!edev || !edev->pe)
1487 return NULL;
1488
1489 return edev->pe;
1490}
Gavin Shan537e5402014-08-07 12:47:16 +10001491EXPORT_SYMBOL_GPL(eeh_iommu_group_to_pe);
Gavin Shan212d16c2014-06-10 11:41:56 +10001492
Benjamin Herrenschmidt2194dc22014-08-05 18:52:59 +10001493#endif /* CONFIG_IOMMU_API */
1494
Gavin Shan212d16c2014-06-10 11:41:56 +10001495/**
1496 * eeh_pe_set_option - Set options for the indicated PE
1497 * @pe: EEH PE
1498 * @option: requested option
1499 *
1500 * The routine is called to enable or disable EEH functionality
1501 * on the indicated PE, to enable IO or DMA for the frozen PE.
1502 */
1503int eeh_pe_set_option(struct eeh_pe *pe, int option)
1504{
1505 int ret = 0;
1506
1507 /* Invalid PE ? */
1508 if (!pe)
1509 return -ENODEV;
1510
1511 /*
1512 * EEH functionality could possibly be disabled, just
1513 * return error for the case. And the EEH functinality
1514 * isn't expected to be disabled on one specific PE.
1515 */
1516 switch (option) {
1517 case EEH_OPT_ENABLE:
Gavin Shan4eeeff02014-09-30 12:39:01 +10001518 if (eeh_enabled()) {
Gavin Shan5cfb20b2014-09-30 12:39:07 +10001519 ret = eeh_pe_change_owner(pe);
Gavin Shan212d16c2014-06-10 11:41:56 +10001520 break;
Gavin Shan4eeeff02014-09-30 12:39:01 +10001521 }
Gavin Shan212d16c2014-06-10 11:41:56 +10001522 ret = -EIO;
1523 break;
1524 case EEH_OPT_DISABLE:
1525 break;
1526 case EEH_OPT_THAW_MMIO:
1527 case EEH_OPT_THAW_DMA:
Gavin Shande5a6622016-09-28 14:34:53 +10001528 case EEH_OPT_FREEZE_PE:
Gavin Shan212d16c2014-06-10 11:41:56 +10001529 if (!eeh_ops || !eeh_ops->set_option) {
1530 ret = -ENOENT;
1531 break;
1532 }
1533
Gavin Shan4eeeff02014-09-30 12:39:01 +10001534 ret = eeh_pci_enable(pe, option);
Gavin Shan212d16c2014-06-10 11:41:56 +10001535 break;
1536 default:
1537 pr_debug("%s: Option %d out of range (%d, %d)\n",
1538 __func__, option, EEH_OPT_DISABLE, EEH_OPT_THAW_DMA);
1539 ret = -EINVAL;
1540 }
1541
1542 return ret;
1543}
1544EXPORT_SYMBOL_GPL(eeh_pe_set_option);
1545
1546/**
1547 * eeh_pe_get_state - Retrieve PE's state
1548 * @pe: EEH PE
1549 *
1550 * Retrieve the PE's state, which includes 3 aspects: enabled
1551 * DMA, enabled IO and asserted reset.
1552 */
1553int eeh_pe_get_state(struct eeh_pe *pe)
1554{
1555 int result, ret = 0;
1556 bool rst_active, dma_en, mmio_en;
1557
1558 /* Existing PE ? */
1559 if (!pe)
1560 return -ENODEV;
1561
1562 if (!eeh_ops || !eeh_ops->get_state)
1563 return -ENOENT;
1564
Gavin Shaneca036e2016-03-04 10:53:14 +11001565 /*
1566 * If the parent PE is owned by the host kernel and is undergoing
1567 * error recovery, we should return the PE state as temporarily
1568 * unavailable so that the error recovery on the guest is suspended
1569 * until the recovery completes on the host.
1570 */
1571 if (pe->parent &&
1572 !(pe->state & EEH_PE_REMOVED) &&
1573 (pe->parent->state & (EEH_PE_ISOLATED | EEH_PE_RECOVERING)))
1574 return EEH_PE_STATE_UNAVAIL;
1575
Gavin Shan212d16c2014-06-10 11:41:56 +10001576 result = eeh_ops->get_state(pe, NULL);
1577 rst_active = !!(result & EEH_STATE_RESET_ACTIVE);
1578 dma_en = !!(result & EEH_STATE_DMA_ENABLED);
1579 mmio_en = !!(result & EEH_STATE_MMIO_ENABLED);
1580
1581 if (rst_active)
1582 ret = EEH_PE_STATE_RESET;
1583 else if (dma_en && mmio_en)
1584 ret = EEH_PE_STATE_NORMAL;
1585 else if (!dma_en && !mmio_en)
1586 ret = EEH_PE_STATE_STOPPED_IO_DMA;
1587 else if (!dma_en && mmio_en)
1588 ret = EEH_PE_STATE_STOPPED_DMA;
1589 else
1590 ret = EEH_PE_STATE_UNAVAIL;
1591
1592 return ret;
1593}
1594EXPORT_SYMBOL_GPL(eeh_pe_get_state);
1595
Gavin Shan316233f2014-09-30 12:38:53 +10001596static int eeh_pe_reenable_devices(struct eeh_pe *pe)
1597{
1598 struct eeh_dev *edev, *tmp;
1599 struct pci_dev *pdev;
1600 int ret = 0;
1601
1602 /* Restore config space */
1603 eeh_pe_restore_bars(pe);
1604
1605 /*
1606 * Reenable PCI devices as the devices passed
1607 * through are always enabled before the reset.
1608 */
1609 eeh_pe_for_each_dev(pe, edev, tmp) {
1610 pdev = eeh_dev_to_pci_dev(edev);
1611 if (!pdev)
1612 continue;
1613
1614 ret = pci_reenable_device(pdev);
1615 if (ret) {
1616 pr_warn("%s: Failure %d reenabling %s\n",
1617 __func__, ret, pci_name(pdev));
1618 return ret;
1619 }
1620 }
1621
1622 /* The PE is still in frozen state */
Gavin Shanc9dd0142014-09-30 12:39:02 +10001623 return eeh_unfreeze_pe(pe, true);
Gavin Shan316233f2014-09-30 12:38:53 +10001624}
1625
Gavin Shan212d16c2014-06-10 11:41:56 +10001626/**
1627 * eeh_pe_reset - Issue PE reset according to specified type
1628 * @pe: EEH PE
1629 * @option: reset type
1630 *
1631 * The routine is called to reset the specified PE with the
1632 * indicated type, either fundamental reset or hot reset.
1633 * PE reset is the most important part for error recovery.
1634 */
1635int eeh_pe_reset(struct eeh_pe *pe, int option)
1636{
1637 int ret = 0;
1638
1639 /* Invalid PE ? */
1640 if (!pe)
1641 return -ENODEV;
1642
1643 if (!eeh_ops || !eeh_ops->set_option || !eeh_ops->reset)
1644 return -ENOENT;
1645
1646 switch (option) {
1647 case EEH_RESET_DEACTIVATE:
1648 ret = eeh_ops->reset(pe, option);
Gavin Shan8a6b3712014-10-01 17:07:50 +10001649 eeh_pe_state_clear(pe, EEH_PE_CFG_BLOCKED);
Gavin Shan212d16c2014-06-10 11:41:56 +10001650 if (ret)
1651 break;
1652
Gavin Shan316233f2014-09-30 12:38:53 +10001653 ret = eeh_pe_reenable_devices(pe);
Gavin Shan212d16c2014-06-10 11:41:56 +10001654 break;
1655 case EEH_RESET_HOT:
1656 case EEH_RESET_FUNDAMENTAL:
Gavin Shan0d5ee522014-09-30 12:38:52 +10001657 /*
1658 * Proactively freeze the PE to drop all MMIO access
1659 * during reset, which should be banned as it's always
1660 * cause recursive EEH error.
1661 */
1662 eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);
1663
Gavin Shan8a6b3712014-10-01 17:07:50 +10001664 eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
Gavin Shan212d16c2014-06-10 11:41:56 +10001665 ret = eeh_ops->reset(pe, option);
1666 break;
1667 default:
1668 pr_debug("%s: Unsupported option %d\n",
1669 __func__, option);
1670 ret = -EINVAL;
1671 }
1672
1673 return ret;
1674}
1675EXPORT_SYMBOL_GPL(eeh_pe_reset);
1676
1677/**
1678 * eeh_pe_configure - Configure PCI bridges after PE reset
1679 * @pe: EEH PE
1680 *
1681 * The routine is called to restore the PCI config space for
1682 * those PCI devices, especially PCI bridges affected by PE
1683 * reset issued previously.
1684 */
1685int eeh_pe_configure(struct eeh_pe *pe)
1686{
1687 int ret = 0;
1688
1689 /* Invalid PE ? */
1690 if (!pe)
1691 return -ENODEV;
1692
Gavin Shan212d16c2014-06-10 11:41:56 +10001693 return ret;
1694}
1695EXPORT_SYMBOL_GPL(eeh_pe_configure);
1696
Gavin Shanec33d362015-03-26 16:42:08 +11001697/**
1698 * eeh_pe_inject_err - Injecting the specified PCI error to the indicated PE
1699 * @pe: the indicated PE
1700 * @type: error type
1701 * @function: error function
1702 * @addr: address
1703 * @mask: address mask
1704 *
1705 * The routine is called to inject the specified PCI error, which
1706 * is determined by @type and @function, to the indicated PE for
1707 * testing purpose.
1708 */
1709int eeh_pe_inject_err(struct eeh_pe *pe, int type, int func,
1710 unsigned long addr, unsigned long mask)
1711{
1712 /* Invalid PE ? */
1713 if (!pe)
1714 return -ENODEV;
1715
1716 /* Unsupported operation ? */
1717 if (!eeh_ops || !eeh_ops->err_inject)
1718 return -ENOENT;
1719
1720 /* Check on PCI error type */
1721 if (type != EEH_ERR_TYPE_32 && type != EEH_ERR_TYPE_64)
1722 return -EINVAL;
1723
1724 /* Check on PCI error function */
1725 if (func < EEH_ERR_FUNC_MIN || func > EEH_ERR_FUNC_MAX)
1726 return -EINVAL;
1727
1728 return eeh_ops->err_inject(pe, type, func, addr, mask);
1729}
1730EXPORT_SYMBOL_GPL(eeh_pe_inject_err);
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732static int proc_eeh_show(struct seq_file *m, void *v)
1733{
Gavin Shan2ec5a0a2014-02-12 15:24:55 +08001734 if (!eeh_enabled()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 seq_printf(m, "EEH Subsystem is globally disabled\n");
Gavin Shane575f8d2012-02-29 15:47:45 +00001736 seq_printf(m, "eeh_total_mmio_ffs=%llu\n", eeh_stats.total_mmio_ffs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 } else {
1738 seq_printf(m, "EEH Subsystem is enabled\n");
Linas Vepstas177bc932005-11-03 18:48:52 -06001739 seq_printf(m,
Gavin Shane575f8d2012-02-29 15:47:45 +00001740 "no device=%llu\n"
1741 "no device node=%llu\n"
1742 "no config address=%llu\n"
1743 "check not wanted=%llu\n"
1744 "eeh_total_mmio_ffs=%llu\n"
1745 "eeh_false_positives=%llu\n"
1746 "eeh_slot_resets=%llu\n",
1747 eeh_stats.no_device,
1748 eeh_stats.no_dn,
1749 eeh_stats.no_cfg_addr,
1750 eeh_stats.ignored_check,
1751 eeh_stats.total_mmio_ffs,
1752 eeh_stats.false_positives,
1753 eeh_stats.slot_resets);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
1755
1756 return 0;
1757}
1758
1759static int proc_eeh_open(struct inode *inode, struct file *file)
1760{
1761 return single_open(file, proc_eeh_show, NULL);
1762}
1763
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -08001764static const struct file_operations proc_eeh_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 .open = proc_eeh_open,
1766 .read = seq_read,
1767 .llseek = seq_lseek,
1768 .release = single_release,
1769};
1770
Gavin Shan7f52a522014-04-24 18:00:18 +10001771#ifdef CONFIG_DEBUG_FS
1772static int eeh_enable_dbgfs_set(void *data, u64 val)
1773{
1774 if (val)
Gavin Shan05b17212014-07-17 14:41:38 +10001775 eeh_clear_flag(EEH_FORCE_DISABLED);
Gavin Shan7f52a522014-04-24 18:00:18 +10001776 else
Gavin Shan05b17212014-07-17 14:41:38 +10001777 eeh_add_flag(EEH_FORCE_DISABLED);
Gavin Shan7f52a522014-04-24 18:00:18 +10001778
1779 /* Notify the backend */
1780 if (eeh_ops->post_init)
1781 eeh_ops->post_init();
1782
1783 return 0;
1784}
1785
1786static int eeh_enable_dbgfs_get(void *data, u64 *val)
1787{
1788 if (eeh_enabled())
1789 *val = 0x1ul;
1790 else
1791 *val = 0x0ul;
1792 return 0;
1793}
1794
Gavin Shan1b28f172014-12-11 14:28:56 +11001795static int eeh_freeze_dbgfs_set(void *data, u64 val)
1796{
1797 eeh_max_freezes = val;
1798 return 0;
1799}
1800
1801static int eeh_freeze_dbgfs_get(void *data, u64 *val)
1802{
1803 *val = eeh_max_freezes;
1804 return 0;
1805}
1806
Gavin Shan7f52a522014-04-24 18:00:18 +10001807DEFINE_SIMPLE_ATTRIBUTE(eeh_enable_dbgfs_ops, eeh_enable_dbgfs_get,
1808 eeh_enable_dbgfs_set, "0x%llx\n");
Gavin Shan1b28f172014-12-11 14:28:56 +11001809DEFINE_SIMPLE_ATTRIBUTE(eeh_freeze_dbgfs_ops, eeh_freeze_dbgfs_get,
1810 eeh_freeze_dbgfs_set, "0x%llx\n");
Gavin Shan7f52a522014-04-24 18:00:18 +10001811#endif
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813static int __init eeh_init_proc(void)
1814{
Gavin Shan7f52a522014-04-24 18:00:18 +10001815 if (machine_is(pseries) || machine_is(powernv)) {
Thadeu Lima de Souza Cascardo8feaa432011-08-26 10:36:31 +00001816 proc_create("powerpc/eeh", 0, NULL, &proc_eeh_operations);
Gavin Shan7f52a522014-04-24 18:00:18 +10001817#ifdef CONFIG_DEBUG_FS
1818 debugfs_create_file("eeh_enable", 0600,
1819 powerpc_debugfs_root, NULL,
1820 &eeh_enable_dbgfs_ops);
Gavin Shan1b28f172014-12-11 14:28:56 +11001821 debugfs_create_file("eeh_max_freezes", 0600,
1822 powerpc_debugfs_root, NULL,
1823 &eeh_freeze_dbgfs_ops);
Gavin Shan7f52a522014-04-24 18:00:18 +10001824#endif
1825 }
1826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 return 0;
1828}
1829__initcall(eeh_init_proc);