xref: /openbmc/linux/arch/powerpc/kernel/eeh.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2317f06deSGavin Shan /*
3317f06deSGavin Shan  * Copyright IBM Corporation 2001, 2005, 2006
4317f06deSGavin Shan  * Copyright Dave Engebretsen & Todd Inglett 2001
5317f06deSGavin Shan  * Copyright Linas Vepstas 2005, 2006
6317f06deSGavin Shan  * Copyright 2001-2012 IBM Corporation.
7317f06deSGavin Shan  *
8317f06deSGavin Shan  * Please address comments and feedback to Linas Vepstas <linas@austin.ibm.com>
9317f06deSGavin Shan  */
10317f06deSGavin Shan 
11317f06deSGavin Shan #include <linux/delay.h>
12317f06deSGavin Shan #include <linux/sched.h>
13317f06deSGavin Shan #include <linux/init.h>
14317f06deSGavin Shan #include <linux/list.h>
15317f06deSGavin Shan #include <linux/pci.h>
16a3032ca9SGavin Shan #include <linux/iommu.h>
17317f06deSGavin Shan #include <linux/proc_fs.h>
18317f06deSGavin Shan #include <linux/rbtree.h>
1966f9af83SGavin Shan #include <linux/reboot.h>
20317f06deSGavin Shan #include <linux/seq_file.h>
21317f06deSGavin Shan #include <linux/spinlock.h>
22317f06deSGavin Shan #include <linux/export.h>
23317f06deSGavin Shan #include <linux/of.h>
24dbf77fedSAneesh Kumar K.V #include <linux/debugfs.h>
25317f06deSGavin Shan 
26317f06deSGavin Shan #include <linux/atomic.h>
27317f06deSGavin Shan #include <asm/eeh.h>
28317f06deSGavin Shan #include <asm/eeh_event.h>
29317f06deSGavin Shan #include <asm/io.h>
30212d16cdSGavin Shan #include <asm/iommu.h>
31317f06deSGavin Shan #include <asm/machdep.h>
32317f06deSGavin Shan #include <asm/ppc-pci.h>
33317f06deSGavin Shan #include <asm/rtas.h>
3494171b19SAneesh Kumar K.V #include <asm/pte-walk.h>
35317f06deSGavin Shan 
36317f06deSGavin Shan 
37317f06deSGavin Shan /** Overview:
388ee26530SRussell Currey  *  EEH, or "Enhanced Error Handling" is a PCI bridge technology for
39317f06deSGavin Shan  *  dealing with PCI bus errors that can't be dealt with within the
40317f06deSGavin Shan  *  usual PCI framework, except by check-stopping the CPU.  Systems
41317f06deSGavin Shan  *  that are designed for high-availability/reliability cannot afford
42317f06deSGavin Shan  *  to crash due to a "mere" PCI error, thus the need for EEH.
43317f06deSGavin Shan  *  An EEH-capable bridge operates by converting a detected error
44317f06deSGavin Shan  *  into a "slot freeze", taking the PCI adapter off-line, making
45317f06deSGavin Shan  *  the slot behave, from the OS'es point of view, as if the slot
46317f06deSGavin Shan  *  were "empty": all reads return 0xff's and all writes are silently
47317f06deSGavin Shan  *  ignored.  EEH slot isolation events can be triggered by parity
48317f06deSGavin Shan  *  errors on the address or data busses (e.g. during posted writes),
49317f06deSGavin Shan  *  which in turn might be caused by low voltage on the bus, dust,
50317f06deSGavin Shan  *  vibration, humidity, radioactivity or plain-old failed hardware.
51317f06deSGavin Shan  *
52317f06deSGavin Shan  *  Note, however, that one of the leading causes of EEH slot
53317f06deSGavin Shan  *  freeze events are buggy device drivers, buggy device microcode,
54317f06deSGavin Shan  *  or buggy device hardware.  This is because any attempt by the
55317f06deSGavin Shan  *  device to bus-master data to a memory address that is not
56317f06deSGavin Shan  *  assigned to the device will trigger a slot freeze.   (The idea
57317f06deSGavin Shan  *  is to prevent devices-gone-wild from corrupting system memory).
58317f06deSGavin Shan  *  Buggy hardware/drivers will have a miserable time co-existing
59317f06deSGavin Shan  *  with EEH.
60317f06deSGavin Shan  *
61317f06deSGavin Shan  *  Ideally, a PCI device driver, when suspecting that an isolation
62317f06deSGavin Shan  *  event has occurred (e.g. by reading 0xff's), will then ask EEH
63317f06deSGavin Shan  *  whether this is the case, and then take appropriate steps to
64317f06deSGavin Shan  *  reset the PCI slot, the PCI device, and then resume operations.
65317f06deSGavin Shan  *  However, until that day,  the checking is done here, with the
66317f06deSGavin Shan  *  eeh_check_failure() routine embedded in the MMIO macros.  If
67317f06deSGavin Shan  *  the slot is found to be isolated, an "EEH Event" is synthesized
68317f06deSGavin Shan  *  and sent out for processing.
69317f06deSGavin Shan  */
70317f06deSGavin Shan 
71317f06deSGavin Shan /* If a device driver keeps reading an MMIO register in an interrupt
72317f06deSGavin Shan  * handler after a slot isolation event, it might be broken.
73317f06deSGavin Shan  * This sets the threshold for how many read attempts we allow
74317f06deSGavin Shan  * before printing an error message.
75317f06deSGavin Shan  */
76317f06deSGavin Shan #define EEH_MAX_FAILS	2100000
77317f06deSGavin Shan 
78317f06deSGavin Shan /* Time to wait for a PCI slot to report status, in milliseconds */
79fb48dc22SBrian King #define PCI_BUS_RESET_WAIT_MSEC (5*60*1000)
80317f06deSGavin Shan 
818a5ad356SGavin Shan /*
828a5ad356SGavin Shan  * EEH probe mode support, which is part of the flags,
838a5ad356SGavin Shan  * is to support multiple platforms for EEH. Some platforms
848a5ad356SGavin Shan  * like pSeries do PCI emunation based on device tree.
858a5ad356SGavin Shan  * However, other platforms like powernv probe PCI devices
868a5ad356SGavin Shan  * from hardware. The flag is used to distinguish that.
878a5ad356SGavin Shan  * In addition, struct eeh_ops::probe would be invoked for
888a5ad356SGavin Shan  * particular OF node or PCI device so that the corresponding
898a5ad356SGavin Shan  * PE would be created there.
908a5ad356SGavin Shan  */
918a5ad356SGavin Shan int eeh_subsystem_flags;
928a5ad356SGavin Shan EXPORT_SYMBOL(eeh_subsystem_flags);
938a5ad356SGavin Shan 
941b28f170SGavin Shan /*
951b28f170SGavin Shan  * EEH allowed maximal frozen times. If one particular PE's
961b28f170SGavin Shan  * frozen count in last hour exceeds this limit, the PE will
971b28f170SGavin Shan  * be forced to be offline permanently.
981b28f170SGavin Shan  */
9946ee7c3cSOliver O'Halloran u32 eeh_max_freezes = 5;
1001b28f170SGavin Shan 
1016b493f60SOliver O'Halloran /*
1026b493f60SOliver O'Halloran  * Controls whether a recovery event should be scheduled when an
1036b493f60SOliver O'Halloran  * isolated device is discovered. This is only really useful for
1046b493f60SOliver O'Halloran  * debugging problems with the EEH core.
1056b493f60SOliver O'Halloran  */
1066b493f60SOliver O'Halloran bool eeh_debugfs_no_recover;
1076b493f60SOliver O'Halloran 
108317f06deSGavin Shan /* Platform dependent EEH operations */
109317f06deSGavin Shan struct eeh_ops *eeh_ops = NULL;
110317f06deSGavin Shan 
111317f06deSGavin Shan /* Lock to avoid races due to multiple reports of an error */
1124907581dSGavin Shan DEFINE_RAW_SPINLOCK(confirm_error_lock);
11335066c0dSGavin Shan EXPORT_SYMBOL_GPL(confirm_error_lock);
114317f06deSGavin Shan 
115212d16cdSGavin Shan /* Lock to protect passed flags */
116212d16cdSGavin Shan static DEFINE_MUTEX(eeh_dev_mutex);
117212d16cdSGavin Shan 
118317f06deSGavin Shan /* Buffer for reporting pci register dumps. Its here in BSS, and
119317f06deSGavin Shan  * not dynamically alloced, so that it ends up in RMO where RTAS
120317f06deSGavin Shan  * can access it.
121317f06deSGavin Shan  */
122f2e0be5eSGavin Shan #define EEH_PCI_REGS_LOG_LEN 8192
123317f06deSGavin Shan static unsigned char pci_regs_buf[EEH_PCI_REGS_LOG_LEN];
124317f06deSGavin Shan 
125317f06deSGavin Shan /*
126317f06deSGavin Shan  * The struct is used to maintain the EEH global statistic
127317f06deSGavin Shan  * information. Besides, the EEH global statistics will be
128317f06deSGavin Shan  * exported to user space through procfs
129317f06deSGavin Shan  */
130317f06deSGavin Shan struct eeh_stats {
131317f06deSGavin Shan 	u64 no_device;		/* PCI device not found		*/
132317f06deSGavin Shan 	u64 no_dn;		/* OF node not found		*/
133317f06deSGavin Shan 	u64 no_cfg_addr;	/* Config address not found	*/
134317f06deSGavin Shan 	u64 ignored_check;	/* EEH check skipped		*/
135317f06deSGavin Shan 	u64 total_mmio_ffs;	/* Total EEH checks		*/
136317f06deSGavin Shan 	u64 false_positives;	/* Unnecessary EEH checks	*/
137317f06deSGavin Shan 	u64 slot_resets;	/* PE reset			*/
138317f06deSGavin Shan };
139317f06deSGavin Shan 
140317f06deSGavin Shan static struct eeh_stats eeh_stats;
141317f06deSGavin Shan 
eeh_setup(char * str)1427f52a526SGavin Shan static int __init eeh_setup(char *str)
1437f52a526SGavin Shan {
1447f52a526SGavin Shan 	if (!strcmp(str, "off"))
14505b1721dSGavin Shan 		eeh_add_flag(EEH_FORCE_DISABLED);
146a450e8f5SGavin Shan 	else if (!strcmp(str, "early_log"))
147a450e8f5SGavin Shan 		eeh_add_flag(EEH_EARLY_DUMP_LOG);
1487f52a526SGavin Shan 
1497f52a526SGavin Shan 	return 1;
1507f52a526SGavin Shan }
1517f52a526SGavin Shan __setup("eeh=", eeh_setup);
1527f52a526SGavin Shan 
eeh_show_enabled(void)153c44e4ccaSSam Bobroff void eeh_show_enabled(void)
154c44e4ccaSSam Bobroff {
155c44e4ccaSSam Bobroff 	if (eeh_has_flag(EEH_FORCE_DISABLED))
156c44e4ccaSSam Bobroff 		pr_info("EEH: Recovery disabled by kernel parameter.\n");
157c44e4ccaSSam Bobroff 	else if (eeh_has_flag(EEH_ENABLED))
158c44e4ccaSSam Bobroff 		pr_info("EEH: Capable adapter found: recovery enabled.\n");
159c44e4ccaSSam Bobroff 	else
160c44e4ccaSSam Bobroff 		pr_info("EEH: No capable adapters found: recovery disabled.\n");
161c44e4ccaSSam Bobroff }
162c44e4ccaSSam Bobroff 
163f2e0be5eSGavin Shan /*
164f2e0be5eSGavin Shan  * This routine captures assorted PCI configuration space data
165f2e0be5eSGavin Shan  * for the indicated PCI device, and puts them into a buffer
166f2e0be5eSGavin Shan  * for RTAS error logging.
167317f06deSGavin Shan  */
eeh_dump_dev_log(struct eeh_dev * edev,char * buf,size_t len)168f2e0be5eSGavin Shan static size_t eeh_dump_dev_log(struct eeh_dev *edev, char *buf, size_t len)
169317f06deSGavin Shan {
170317f06deSGavin Shan 	u32 cfg;
171317f06deSGavin Shan 	int cap, i;
1720ed352ddSGavin Shan 	int n = 0, l = 0;
1730ed352ddSGavin Shan 	char buffer[128];
174317f06deSGavin Shan 
17510560b9aSGuilherme G. Piccoli 	n += scnprintf(buf+n, len-n, "%04x:%02x:%02x.%01x\n",
1761a303d88SOliver O'Halloran 			edev->pe->phb->global_number, edev->bdfn >> 8,
1771a303d88SOliver O'Halloran 			PCI_SLOT(edev->bdfn), PCI_FUNC(edev->bdfn));
17810560b9aSGuilherme G. Piccoli 	pr_warn("EEH: of node=%04x:%02x:%02x.%01x\n",
1791a303d88SOliver O'Halloran 		edev->pe->phb->global_number, edev->bdfn >> 8,
1801a303d88SOliver O'Halloran 		PCI_SLOT(edev->bdfn), PCI_FUNC(edev->bdfn));
181317f06deSGavin Shan 
18217d2a487SOliver O'Halloran 	eeh_ops->read_config(edev, PCI_VENDOR_ID, 4, &cfg);
183317f06deSGavin Shan 	n += scnprintf(buf+n, len-n, "dev/vend:%08x\n", cfg);
1842d86c385SGavin Shan 	pr_warn("EEH: PCI device/vendor: %08x\n", cfg);
185317f06deSGavin Shan 
18617d2a487SOliver O'Halloran 	eeh_ops->read_config(edev, PCI_COMMAND, 4, &cfg);
187317f06deSGavin Shan 	n += scnprintf(buf+n, len-n, "cmd/stat:%x\n", cfg);
1882d86c385SGavin Shan 	pr_warn("EEH: PCI cmd/status register: %08x\n", cfg);
189317f06deSGavin Shan 
190317f06deSGavin Shan 	/* Gather bridge-specific registers */
1912a18dfc6SGavin Shan 	if (edev->mode & EEH_DEV_BRIDGE) {
19217d2a487SOliver O'Halloran 		eeh_ops->read_config(edev, PCI_SEC_STATUS, 2, &cfg);
193317f06deSGavin Shan 		n += scnprintf(buf+n, len-n, "sec stat:%x\n", cfg);
1942d86c385SGavin Shan 		pr_warn("EEH: Bridge secondary status: %04x\n", cfg);
195317f06deSGavin Shan 
19617d2a487SOliver O'Halloran 		eeh_ops->read_config(edev, PCI_BRIDGE_CONTROL, 2, &cfg);
197317f06deSGavin Shan 		n += scnprintf(buf+n, len-n, "brdg ctl:%x\n", cfg);
1982d86c385SGavin Shan 		pr_warn("EEH: Bridge control: %04x\n", cfg);
199317f06deSGavin Shan 	}
200317f06deSGavin Shan 
201317f06deSGavin Shan 	/* Dump out the PCI-X command and status regs */
2022a18dfc6SGavin Shan 	cap = edev->pcix_cap;
203317f06deSGavin Shan 	if (cap) {
20417d2a487SOliver O'Halloran 		eeh_ops->read_config(edev, cap, 4, &cfg);
205317f06deSGavin Shan 		n += scnprintf(buf+n, len-n, "pcix-cmd:%x\n", cfg);
2062d86c385SGavin Shan 		pr_warn("EEH: PCI-X cmd: %08x\n", cfg);
207317f06deSGavin Shan 
20817d2a487SOliver O'Halloran 		eeh_ops->read_config(edev, cap+4, 4, &cfg);
209317f06deSGavin Shan 		n += scnprintf(buf+n, len-n, "pcix-stat:%x\n", cfg);
2102d86c385SGavin Shan 		pr_warn("EEH: PCI-X status: %08x\n", cfg);
211317f06deSGavin Shan 	}
212317f06deSGavin Shan 
2132a18dfc6SGavin Shan 	/* If PCI-E capable, dump PCI-E cap 10 */
2142a18dfc6SGavin Shan 	cap = edev->pcie_cap;
2152a18dfc6SGavin Shan 	if (cap) {
216317f06deSGavin Shan 		n += scnprintf(buf+n, len-n, "pci-e cap10:\n");
2172d86c385SGavin Shan 		pr_warn("EEH: PCI-E capabilities and status follow:\n");
218317f06deSGavin Shan 
219317f06deSGavin Shan 		for (i=0; i<=8; i++) {
22017d2a487SOliver O'Halloran 			eeh_ops->read_config(edev, cap+4*i, 4, &cfg);
221317f06deSGavin Shan 			n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
2220ed352ddSGavin Shan 
2230ed352ddSGavin Shan 			if ((i % 4) == 0) {
2240ed352ddSGavin Shan 				if (i != 0)
2250ed352ddSGavin Shan 					pr_warn("%s\n", buffer);
2260ed352ddSGavin Shan 
2270ed352ddSGavin Shan 				l = scnprintf(buffer, sizeof(buffer),
2280ed352ddSGavin Shan 					      "EEH: PCI-E %02x: %08x ",
2290ed352ddSGavin Shan 					      4*i, cfg);
2300ed352ddSGavin Shan 			} else {
2310ed352ddSGavin Shan 				l += scnprintf(buffer+l, sizeof(buffer)-l,
2320ed352ddSGavin Shan 					       "%08x ", cfg);
233317f06deSGavin Shan 			}
2340ed352ddSGavin Shan 
2350ed352ddSGavin Shan 		}
2360ed352ddSGavin Shan 
2370ed352ddSGavin Shan 		pr_warn("%s\n", buffer);
2382a18dfc6SGavin Shan 	}
239317f06deSGavin Shan 
2402a18dfc6SGavin Shan 	/* If AER capable, dump it */
2412a18dfc6SGavin Shan 	cap = edev->aer_cap;
242317f06deSGavin Shan 	if (cap) {
243317f06deSGavin Shan 		n += scnprintf(buf+n, len-n, "pci-e AER:\n");
2442a18dfc6SGavin Shan 		pr_warn("EEH: PCI-E AER capability register set follows:\n");
245317f06deSGavin Shan 
2460ed352ddSGavin Shan 		for (i=0; i<=13; i++) {
24717d2a487SOliver O'Halloran 			eeh_ops->read_config(edev, cap+4*i, 4, &cfg);
248317f06deSGavin Shan 			n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
2490ed352ddSGavin Shan 
2500ed352ddSGavin Shan 			if ((i % 4) == 0) {
2510ed352ddSGavin Shan 				if (i != 0)
2520ed352ddSGavin Shan 					pr_warn("%s\n", buffer);
2530ed352ddSGavin Shan 
2540ed352ddSGavin Shan 				l = scnprintf(buffer, sizeof(buffer),
2550ed352ddSGavin Shan 					      "EEH: PCI-E AER %02x: %08x ",
2560ed352ddSGavin Shan 					      4*i, cfg);
2570ed352ddSGavin Shan 			} else {
2580ed352ddSGavin Shan 				l += scnprintf(buffer+l, sizeof(buffer)-l,
2590ed352ddSGavin Shan 					       "%08x ", cfg);
260317f06deSGavin Shan 			}
261317f06deSGavin Shan 		}
262317f06deSGavin Shan 
2630ed352ddSGavin Shan 		pr_warn("%s\n", buffer);
2640ed352ddSGavin Shan 	}
2650ed352ddSGavin Shan 
266317f06deSGavin Shan 	return n;
267317f06deSGavin Shan }
268317f06deSGavin Shan 
eeh_dump_pe_log(struct eeh_pe * pe,void * flag)269d6c4932fSSam Bobroff static void *eeh_dump_pe_log(struct eeh_pe *pe, void *flag)
270f2e0be5eSGavin Shan {
271f2e0be5eSGavin Shan 	struct eeh_dev *edev, *tmp;
272f2e0be5eSGavin Shan 	size_t *plen = flag;
273f2e0be5eSGavin Shan 
274f2e0be5eSGavin Shan 	eeh_pe_for_each_dev(pe, edev, tmp)
275f2e0be5eSGavin Shan 		*plen += eeh_dump_dev_log(edev, pci_regs_buf + *plen,
276f2e0be5eSGavin Shan 					  EEH_PCI_REGS_LOG_LEN - *plen);
277f2e0be5eSGavin Shan 
278f2e0be5eSGavin Shan 	return NULL;
279f2e0be5eSGavin Shan }
280f2e0be5eSGavin Shan 
281317f06deSGavin Shan /**
282317f06deSGavin Shan  * eeh_slot_error_detail - Generate combined log including driver log and error log
283317f06deSGavin Shan  * @pe: EEH PE
284317f06deSGavin Shan  * @severity: temporary or permanent error log
285317f06deSGavin Shan  *
286317f06deSGavin Shan  * This routine should be called to generate the combined log, which
287317f06deSGavin Shan  * is comprised of driver log and error log. The driver log is figured
288317f06deSGavin Shan  * out from the config space of the corresponding PCI device, while
289317f06deSGavin Shan  * the error log is fetched through platform dependent function call.
290317f06deSGavin Shan  */
eeh_slot_error_detail(struct eeh_pe * pe,int severity)291317f06deSGavin Shan void eeh_slot_error_detail(struct eeh_pe *pe, int severity)
292317f06deSGavin Shan {
293317f06deSGavin Shan 	size_t loglen = 0;
294317f06deSGavin Shan 
295c35ae179SGavin Shan 	/*
296c35ae179SGavin Shan 	 * When the PHB is fenced or dead, it's pointless to collect
297c35ae179SGavin Shan 	 * the data from PCI config space because it should return
298c35ae179SGavin Shan 	 * 0xFF's. For ER, we still retrieve the data from the PCI
299c35ae179SGavin Shan 	 * config space.
30078954700SGavin Shan 	 *
30178954700SGavin Shan 	 * For pHyp, we have to enable IO for log retrieval. Otherwise,
30278954700SGavin Shan 	 * 0xFF's is always returned from PCI config space.
303387bbc97SGavin Shan 	 *
304387bbc97SGavin Shan 	 * When the @severity is EEH_LOG_PERM, the PE is going to be
305387bbc97SGavin Shan 	 * removed. Prior to that, the drivers for devices included in
306387bbc97SGavin Shan 	 * the PE will be closed. The drivers rely on working IO path
307387bbc97SGavin Shan 	 * to bring the devices to quiet state. Otherwise, PCI traffic
308387bbc97SGavin Shan 	 * from those devices after they are removed is like to cause
309387bbc97SGavin Shan 	 * another unexpected EEH error.
310c35ae179SGavin Shan 	 */
3119e049375SGavin Shan 	if (!(pe->type & EEH_PE_PHB)) {
312387bbc97SGavin Shan 		if (eeh_has_flag(EEH_ENABLE_IO_FOR_LOG) ||
313387bbc97SGavin Shan 		    severity == EEH_LOG_PERM)
314317f06deSGavin Shan 			eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
31525980013SGavin Shan 
31625980013SGavin Shan 		/*
31725980013SGavin Shan 		 * The config space of some PCI devices can't be accessed
31825980013SGavin Shan 		 * when their PEs are in frozen state. Otherwise, fenced
31925980013SGavin Shan 		 * PHB might be seen. Those PEs are identified with flag
32025980013SGavin Shan 		 * EEH_PE_CFG_RESTRICTED, indicating EEH_PE_CFG_BLOCKED
32125980013SGavin Shan 		 * is set automatically when the PE is put to EEH_PE_ISOLATED.
32225980013SGavin Shan 		 *
32325980013SGavin Shan 		 * Restoring BARs possibly triggers PCI config access in
32425980013SGavin Shan 		 * (OPAL) firmware and then causes fenced PHB. If the
32525980013SGavin Shan 		 * PCI config is blocked with flag EEH_PE_CFG_BLOCKED, it's
32625980013SGavin Shan 		 * pointless to restore BARs and dump config space.
32725980013SGavin Shan 		 */
328317f06deSGavin Shan 		eeh_ops->configure_bridge(pe);
32925980013SGavin Shan 		if (!(pe->state & EEH_PE_CFG_BLOCKED)) {
330317f06deSGavin Shan 			eeh_pe_restore_bars(pe);
331317f06deSGavin Shan 
332317f06deSGavin Shan 			pci_regs_buf[0] = 0;
333f2e0be5eSGavin Shan 			eeh_pe_traverse(pe, eeh_dump_pe_log, &loglen);
334317f06deSGavin Shan 		}
33525980013SGavin Shan 	}
336317f06deSGavin Shan 
337317f06deSGavin Shan 	eeh_ops->get_log(pe, severity, pci_regs_buf, loglen);
338317f06deSGavin Shan }
339317f06deSGavin Shan 
340317f06deSGavin Shan /**
341317f06deSGavin Shan  * eeh_token_to_phys - Convert EEH address token to phys address
342317f06deSGavin Shan  * @token: I/O token, should be address in the form 0xA....
343317f06deSGavin Shan  *
344317f06deSGavin Shan  * This routine should be called to convert virtual I/O address
345317f06deSGavin Shan  * to physical one.
346317f06deSGavin Shan  */
eeh_token_to_phys(unsigned long token)347317f06deSGavin Shan static inline unsigned long eeh_token_to_phys(unsigned long token)
348317f06deSGavin Shan {
3495362a4b6SNicholas Piggin 	return ppc_find_vmap_phys(token);
350317f06deSGavin Shan }
351317f06deSGavin Shan 
352b95cd2cdSGavin Shan /*
353b95cd2cdSGavin Shan  * On PowerNV platform, we might already have fenced PHB there.
354b95cd2cdSGavin Shan  * For that case, it's meaningless to recover frozen PE. Intead,
355b95cd2cdSGavin Shan  * We have to handle fenced PHB firstly.
356b95cd2cdSGavin Shan  */
eeh_phb_check_failure(struct eeh_pe * pe)357b95cd2cdSGavin Shan static int eeh_phb_check_failure(struct eeh_pe *pe)
358b95cd2cdSGavin Shan {
359b95cd2cdSGavin Shan 	struct eeh_pe *phb_pe;
360b95cd2cdSGavin Shan 	unsigned long flags;
361b95cd2cdSGavin Shan 	int ret;
362b95cd2cdSGavin Shan 
36305b1721dSGavin Shan 	if (!eeh_has_flag(EEH_PROBE_MODE_DEV))
364b95cd2cdSGavin Shan 		return -EPERM;
365b95cd2cdSGavin Shan 
366b95cd2cdSGavin Shan 	/* Find the PHB PE */
367b95cd2cdSGavin Shan 	phb_pe = eeh_phb_pe_get(pe->phb);
368b95cd2cdSGavin Shan 	if (!phb_pe) {
3691f52f176SRussell Currey 		pr_warn("%s Can't find PE for PHB#%x\n",
370b95cd2cdSGavin Shan 			__func__, pe->phb->global_number);
371b95cd2cdSGavin Shan 		return -EEXIST;
372b95cd2cdSGavin Shan 	}
373b95cd2cdSGavin Shan 
374b95cd2cdSGavin Shan 	/* If the PHB has been in problematic state */
375b95cd2cdSGavin Shan 	eeh_serialize_lock(&flags);
3769e049375SGavin Shan 	if (phb_pe->state & EEH_PE_ISOLATED) {
377b95cd2cdSGavin Shan 		ret = 0;
378b95cd2cdSGavin Shan 		goto out;
379b95cd2cdSGavin Shan 	}
380b95cd2cdSGavin Shan 
381b95cd2cdSGavin Shan 	/* Check PHB state */
382b95cd2cdSGavin Shan 	ret = eeh_ops->get_state(phb_pe, NULL);
383b95cd2cdSGavin Shan 	if ((ret < 0) ||
38434a286a4SSam Bobroff 	    (ret == EEH_STATE_NOT_SUPPORT) || eeh_state_active(ret)) {
385b95cd2cdSGavin Shan 		ret = 0;
386b95cd2cdSGavin Shan 		goto out;
387b95cd2cdSGavin Shan 	}
388b95cd2cdSGavin Shan 
389b95cd2cdSGavin Shan 	/* Isolate the PHB and send event */
390e762bb89SSam Bobroff 	eeh_pe_mark_isolated(phb_pe);
391b95cd2cdSGavin Shan 	eeh_serialize_unlock(flags);
392b95cd2cdSGavin Shan 
39325baf3d8SOliver O'Halloran 	pr_debug("EEH: PHB#%x failure detected, location: %s\n",
394357b2f3dSGavin Shan 		phb_pe->phb->global_number, eeh_pe_loc_get(phb_pe));
3955293bf97SGavin Shan 	eeh_send_failure_event(phb_pe);
396b95cd2cdSGavin Shan 	return 1;
397b95cd2cdSGavin Shan out:
398b95cd2cdSGavin Shan 	eeh_serialize_unlock(flags);
399b95cd2cdSGavin Shan 	return ret;
400b95cd2cdSGavin Shan }
401b95cd2cdSGavin Shan 
eeh_driver_name(struct pci_dev * pdev)4025a72431eSUwe Kleine-König static inline const char *eeh_driver_name(struct pci_dev *pdev)
4035a72431eSUwe Kleine-König {
4045a72431eSUwe Kleine-König 	if (pdev)
4055a72431eSUwe Kleine-König 		return dev_driver_string(&pdev->dev);
4065a72431eSUwe Kleine-König 
4075a72431eSUwe Kleine-König 	return "<null>";
4085a72431eSUwe Kleine-König }
4095a72431eSUwe Kleine-König 
410317f06deSGavin Shan /**
411317f06deSGavin Shan  * eeh_dev_check_failure - Check if all 1's data is due to EEH slot freeze
412317f06deSGavin Shan  * @edev: eeh device
413317f06deSGavin Shan  *
414317f06deSGavin Shan  * Check for an EEH failure for the given device node.  Call this
415317f06deSGavin Shan  * routine if the result of a read was all 0xff's and you want to
416317f06deSGavin Shan  * find out if this is due to an EEH slot freeze.  This routine
417317f06deSGavin Shan  * will query firmware for the EEH status.
418317f06deSGavin Shan  *
419317f06deSGavin Shan  * Returns 0 if there has not been an EEH error; otherwise returns
420317f06deSGavin Shan  * a non-zero value and queues up a slot isolation event notification.
421317f06deSGavin Shan  *
422317f06deSGavin Shan  * It is safe to call this routine in an interrupt context.
423317f06deSGavin Shan  */
eeh_dev_check_failure(struct eeh_dev * edev)424317f06deSGavin Shan int eeh_dev_check_failure(struct eeh_dev *edev)
425317f06deSGavin Shan {
426317f06deSGavin Shan 	int ret;
427317f06deSGavin Shan 	unsigned long flags;
42814db3d52SAlexey Kardashevskiy 	struct device_node *dn;
429317f06deSGavin Shan 	struct pci_dev *dev;
43025baf3d8SOliver O'Halloran 	struct eeh_pe *pe, *parent_pe;
431317f06deSGavin Shan 	int rc = 0;
432c6406d8fSGavin Shan 	const char *location = NULL;
433317f06deSGavin Shan 
434317f06deSGavin Shan 	eeh_stats.total_mmio_ffs++;
435317f06deSGavin Shan 
4362ec5a0adSGavin Shan 	if (!eeh_enabled())
437317f06deSGavin Shan 		return 0;
438317f06deSGavin Shan 
439317f06deSGavin Shan 	if (!edev) {
440317f06deSGavin Shan 		eeh_stats.no_dn++;
441317f06deSGavin Shan 		return 0;
442317f06deSGavin Shan 	}
443317f06deSGavin Shan 	dev = eeh_dev_to_pci_dev(edev);
4442a58222fSWei Yang 	pe = eeh_dev_to_pe(edev);
445317f06deSGavin Shan 
446317f06deSGavin Shan 	/* Access to IO BARs might get this far and still not want checking. */
447317f06deSGavin Shan 	if (!pe) {
448317f06deSGavin Shan 		eeh_stats.ignored_check++;
4491ff8f36fSSam Bobroff 		eeh_edev_dbg(edev, "Ignored check\n");
450317f06deSGavin Shan 		return 0;
451317f06deSGavin Shan 	}
452317f06deSGavin Shan 
453b95cd2cdSGavin Shan 	/*
454b95cd2cdSGavin Shan 	 * On PowerNV platform, we might already have fenced PHB
455b95cd2cdSGavin Shan 	 * there and we need take care of that firstly.
456b95cd2cdSGavin Shan 	 */
457b95cd2cdSGavin Shan 	ret = eeh_phb_check_failure(pe);
458b95cd2cdSGavin Shan 	if (ret > 0)
459b95cd2cdSGavin Shan 		return ret;
460b95cd2cdSGavin Shan 
46105ec424eSGavin Shan 	/*
46205ec424eSGavin Shan 	 * If the PE isn't owned by us, we shouldn't check the
46305ec424eSGavin Shan 	 * state. Instead, let the owner handle it if the PE has
46405ec424eSGavin Shan 	 * been frozen.
46505ec424eSGavin Shan 	 */
46605ec424eSGavin Shan 	if (eeh_pe_passed(pe))
46705ec424eSGavin Shan 		return 0;
46805ec424eSGavin Shan 
469317f06deSGavin Shan 	/* If we already have a pending isolation event for this
470317f06deSGavin Shan 	 * slot, we know it's bad already, we don't need to check.
471317f06deSGavin Shan 	 * Do this checking under a lock; as multiple PCI devices
472317f06deSGavin Shan 	 * in one slot might report errors simultaneously, and we
473317f06deSGavin Shan 	 * only want one error recovery routine running.
474317f06deSGavin Shan 	 */
4754907581dSGavin Shan 	eeh_serialize_lock(&flags);
476317f06deSGavin Shan 	rc = 1;
477317f06deSGavin Shan 	if (pe->state & EEH_PE_ISOLATED) {
478317f06deSGavin Shan 		pe->check_count++;
4794e0942c0SOliver O'Halloran 		if (pe->check_count == EEH_MAX_FAILS) {
48014db3d52SAlexey Kardashevskiy 			dn = pci_device_to_OF_node(dev);
48114db3d52SAlexey Kardashevskiy 			if (dn)
48214db3d52SAlexey Kardashevskiy 				location = of_get_property(dn, "ibm,loc-code",
48314db3d52SAlexey Kardashevskiy 						NULL);
4841ff8f36fSSam Bobroff 			eeh_edev_err(edev, "%d reads ignored for recovering device at location=%s driver=%s\n",
485c6406d8fSGavin Shan 				pe->check_count,
486c6406d8fSGavin Shan 				location ? location : "unknown",
4871ff8f36fSSam Bobroff 				eeh_driver_name(dev));
4881ff8f36fSSam Bobroff 			eeh_edev_err(edev, "Might be infinite loop in %s driver\n",
489317f06deSGavin Shan 				eeh_driver_name(dev));
490317f06deSGavin Shan 			dump_stack();
491317f06deSGavin Shan 		}
492317f06deSGavin Shan 		goto dn_unlock;
493317f06deSGavin Shan 	}
494317f06deSGavin Shan 
495317f06deSGavin Shan 	/*
496317f06deSGavin Shan 	 * Now test for an EEH failure.  This is VERY expensive.
497317f06deSGavin Shan 	 * Note that the eeh_config_addr may be a parent device
498317f06deSGavin Shan 	 * in the case of a device behind a bridge, or it may be
499317f06deSGavin Shan 	 * function zero of a multi-function device.
500317f06deSGavin Shan 	 * In any case they must share a common PHB.
501317f06deSGavin Shan 	 */
502317f06deSGavin Shan 	ret = eeh_ops->get_state(pe, NULL);
503317f06deSGavin Shan 
504317f06deSGavin Shan 	/* Note that config-io to empty slots may fail;
505317f06deSGavin Shan 	 * they are empty when they don't have children.
506317f06deSGavin Shan 	 * We will punt with the following conditions: Failure to get
507317f06deSGavin Shan 	 * PE's state, EEH not support and Permanently unavailable
508317f06deSGavin Shan 	 * state, PE is in good state.
509317f06deSGavin Shan 	 */
510317f06deSGavin Shan 	if ((ret < 0) ||
51134a286a4SSam Bobroff 	    (ret == EEH_STATE_NOT_SUPPORT) || eeh_state_active(ret)) {
512317f06deSGavin Shan 		eeh_stats.false_positives++;
513317f06deSGavin Shan 		pe->false_positives++;
514317f06deSGavin Shan 		rc = 0;
515317f06deSGavin Shan 		goto dn_unlock;
516317f06deSGavin Shan 	}
517317f06deSGavin Shan 
5181ad7a72cSGavin Shan 	/*
5191ad7a72cSGavin Shan 	 * It should be corner case that the parent PE has been
5201ad7a72cSGavin Shan 	 * put into frozen state as well. We should take care
5211ad7a72cSGavin Shan 	 * that at first.
5221ad7a72cSGavin Shan 	 */
5231ad7a72cSGavin Shan 	parent_pe = pe->parent;
5241ad7a72cSGavin Shan 	while (parent_pe) {
5251ad7a72cSGavin Shan 		/* Hit the ceiling ? */
5261ad7a72cSGavin Shan 		if (parent_pe->type & EEH_PE_PHB)
5271ad7a72cSGavin Shan 			break;
5281ad7a72cSGavin Shan 
5291ad7a72cSGavin Shan 		/* Frozen parent PE ? */
5301ad7a72cSGavin Shan 		ret = eeh_ops->get_state(parent_pe, NULL);
5312eae39f2SSam Bobroff 		if (ret > 0 && !eeh_state_active(ret)) {
5321ad7a72cSGavin Shan 			pe = parent_pe;
5332eae39f2SSam Bobroff 			pr_err("EEH: Failure of PHB#%x-PE#%x will be handled at parent PHB#%x-PE#%x.\n",
5342eae39f2SSam Bobroff 			       pe->phb->global_number, pe->addr,
5352eae39f2SSam Bobroff 			       pe->phb->global_number, parent_pe->addr);
5362eae39f2SSam Bobroff 		}
5371ad7a72cSGavin Shan 
5381ad7a72cSGavin Shan 		/* Next parent level */
5391ad7a72cSGavin Shan 		parent_pe = parent_pe->parent;
5401ad7a72cSGavin Shan 	}
5411ad7a72cSGavin Shan 
542317f06deSGavin Shan 	eeh_stats.slot_resets++;
543317f06deSGavin Shan 
544317f06deSGavin Shan 	/* Avoid repeated reports of this failure, including problems
545317f06deSGavin Shan 	 * with other functions on this device, and functions under
546317f06deSGavin Shan 	 * bridges.
547317f06deSGavin Shan 	 */
548e762bb89SSam Bobroff 	eeh_pe_mark_isolated(pe);
5494907581dSGavin Shan 	eeh_serialize_unlock(flags);
550317f06deSGavin Shan 
551317f06deSGavin Shan 	/* Most EEH events are due to device driver bugs.  Having
552317f06deSGavin Shan 	 * a stack trace will help the device-driver authors figure
553317f06deSGavin Shan 	 * out what happened.  So print that out.
554317f06deSGavin Shan 	 */
55525baf3d8SOliver O'Halloran 	pr_debug("EEH: %s: Frozen PHB#%x-PE#%x detected\n",
55625baf3d8SOliver O'Halloran 		__func__, pe->phb->global_number, pe->addr);
5575293bf97SGavin Shan 	eeh_send_failure_event(pe);
5585293bf97SGavin Shan 
559317f06deSGavin Shan 	return 1;
560317f06deSGavin Shan 
561317f06deSGavin Shan dn_unlock:
5624907581dSGavin Shan 	eeh_serialize_unlock(flags);
563317f06deSGavin Shan 	return rc;
564317f06deSGavin Shan }
565317f06deSGavin Shan 
566317f06deSGavin Shan EXPORT_SYMBOL_GPL(eeh_dev_check_failure);
567317f06deSGavin Shan 
568317f06deSGavin Shan /**
569317f06deSGavin Shan  * eeh_check_failure - Check if all 1's data is due to EEH slot freeze
5703e938052SGavin Shan  * @token: I/O address
571317f06deSGavin Shan  *
5723e938052SGavin Shan  * Check for an EEH failure at the given I/O address. Call this
573317f06deSGavin Shan  * routine if the result of a read was all 0xff's and you want to
574317f06deSGavin Shan  * find out if this is due to an EEH slot freeze event. This routine
575317f06deSGavin Shan  * will query firmware for the EEH status.
576317f06deSGavin Shan  *
577317f06deSGavin Shan  * Note this routine is safe to call in an interrupt context.
578317f06deSGavin Shan  */
eeh_check_failure(const volatile void __iomem * token)5793e938052SGavin Shan int eeh_check_failure(const volatile void __iomem *token)
580317f06deSGavin Shan {
581317f06deSGavin Shan 	unsigned long addr;
582317f06deSGavin Shan 	struct eeh_dev *edev;
583317f06deSGavin Shan 
584317f06deSGavin Shan 	/* Finding the phys addr + pci device; this is pretty quick. */
585317f06deSGavin Shan 	addr = eeh_token_to_phys((unsigned long __force) token);
586317f06deSGavin Shan 	edev = eeh_addr_cache_get_dev(addr);
587317f06deSGavin Shan 	if (!edev) {
588317f06deSGavin Shan 		eeh_stats.no_device++;
5893e938052SGavin Shan 		return 0;
590317f06deSGavin Shan 	}
591317f06deSGavin Shan 
5923e938052SGavin Shan 	return eeh_dev_check_failure(edev);
593317f06deSGavin Shan }
594317f06deSGavin Shan EXPORT_SYMBOL(eeh_check_failure);
595317f06deSGavin Shan 
596317f06deSGavin Shan 
597317f06deSGavin Shan /**
598317f06deSGavin Shan  * eeh_pci_enable - Enable MMIO or DMA transfers for this slot
599317f06deSGavin Shan  * @pe: EEH PE
600b616230eSKai Song  * @function: EEH option
601317f06deSGavin Shan  *
602317f06deSGavin Shan  * This routine should be called to reenable frozen MMIO or DMA
603317f06deSGavin Shan  * so that it would work correctly again. It's useful while doing
604317f06deSGavin Shan  * recovery or log collection on the indicated device.
605317f06deSGavin Shan  */
eeh_pci_enable(struct eeh_pe * pe,int function)606317f06deSGavin Shan int eeh_pci_enable(struct eeh_pe *pe, int function)
607317f06deSGavin Shan {
6084d4f577eSGavin Shan 	int active_flag, rc;
60978954700SGavin Shan 
61078954700SGavin Shan 	/*
61178954700SGavin Shan 	 * pHyp doesn't allow to enable IO or DMA on unfrozen PE.
61278954700SGavin Shan 	 * Also, it's pointless to enable them on unfrozen PE. So
6134d4f577eSGavin Shan 	 * we have to check before enabling IO or DMA.
61478954700SGavin Shan 	 */
6154d4f577eSGavin Shan 	switch (function) {
6164d4f577eSGavin Shan 	case EEH_OPT_THAW_MMIO:
617872ee2d6SGavin Shan 		active_flag = EEH_STATE_MMIO_ACTIVE | EEH_STATE_MMIO_ENABLED;
6184d4f577eSGavin Shan 		break;
6194d4f577eSGavin Shan 	case EEH_OPT_THAW_DMA:
6204d4f577eSGavin Shan 		active_flag = EEH_STATE_DMA_ACTIVE;
6214d4f577eSGavin Shan 		break;
6224d4f577eSGavin Shan 	case EEH_OPT_DISABLE:
6234d4f577eSGavin Shan 	case EEH_OPT_ENABLE:
6244d4f577eSGavin Shan 	case EEH_OPT_FREEZE_PE:
6254d4f577eSGavin Shan 		active_flag = 0;
6264d4f577eSGavin Shan 		break;
6274d4f577eSGavin Shan 	default:
6284d4f577eSGavin Shan 		pr_warn("%s: Invalid function %d\n",
6294d4f577eSGavin Shan 			__func__, function);
6304d4f577eSGavin Shan 		return -EINVAL;
6314d4f577eSGavin Shan 	}
6324d4f577eSGavin Shan 
6334d4f577eSGavin Shan 	/*
6344d4f577eSGavin Shan 	 * Check if IO or DMA has been enabled before
6354d4f577eSGavin Shan 	 * enabling them.
6364d4f577eSGavin Shan 	 */
6374d4f577eSGavin Shan 	if (active_flag) {
63878954700SGavin Shan 		rc = eeh_ops->get_state(pe, NULL);
63978954700SGavin Shan 		if (rc < 0)
64078954700SGavin Shan 			return rc;
64178954700SGavin Shan 
6424d4f577eSGavin Shan 		/* Needn't enable it at all */
6434d4f577eSGavin Shan 		if (rc == EEH_STATE_NOT_SUPPORT)
6444d4f577eSGavin Shan 			return 0;
6454d4f577eSGavin Shan 
6464d4f577eSGavin Shan 		/* It's already enabled */
6474d4f577eSGavin Shan 		if (rc & active_flag)
64878954700SGavin Shan 			return 0;
64978954700SGavin Shan 	}
650317f06deSGavin Shan 
6514d4f577eSGavin Shan 
6524d4f577eSGavin Shan 	/* Issue the request */
653317f06deSGavin Shan 	rc = eeh_ops->set_option(pe, function);
654317f06deSGavin Shan 	if (rc)
65578954700SGavin Shan 		pr_warn("%s: Unexpected state change %d on "
6561f52f176SRussell Currey 			"PHB#%x-PE#%x, err=%d\n",
65778954700SGavin Shan 			__func__, function, pe->phb->global_number,
65878954700SGavin Shan 			pe->addr, rc);
659317f06deSGavin Shan 
6604d4f577eSGavin Shan 	/* Check if the request is finished successfully */
6614d4f577eSGavin Shan 	if (active_flag) {
662fef7f905SSam Bobroff 		rc = eeh_wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
663949e9b82SAndrew Donnellan 		if (rc < 0)
66478954700SGavin Shan 			return rc;
66578954700SGavin Shan 
6664d4f577eSGavin Shan 		if (rc & active_flag)
66778954700SGavin Shan 			return 0;
66878954700SGavin Shan 
6694d4f577eSGavin Shan 		return -EIO;
6704d4f577eSGavin Shan 	}
671317f06deSGavin Shan 
672317f06deSGavin Shan 	return rc;
673317f06deSGavin Shan }
674317f06deSGavin Shan 
eeh_disable_and_save_dev_state(struct eeh_dev * edev,void * userdata)675cef50c67SSam Bobroff static void eeh_disable_and_save_dev_state(struct eeh_dev *edev,
676d6c4932fSSam Bobroff 					    void *userdata)
67728158cd1SGavin Shan {
67828158cd1SGavin Shan 	struct pci_dev *pdev = eeh_dev_to_pci_dev(edev);
67928158cd1SGavin Shan 	struct pci_dev *dev = userdata;
68028158cd1SGavin Shan 
68128158cd1SGavin Shan 	/*
68228158cd1SGavin Shan 	 * The caller should have disabled and saved the
68328158cd1SGavin Shan 	 * state for the specified device
68428158cd1SGavin Shan 	 */
68528158cd1SGavin Shan 	if (!pdev || pdev == dev)
686cef50c67SSam Bobroff 		return;
68728158cd1SGavin Shan 
68828158cd1SGavin Shan 	/* Ensure we have D0 power state */
68928158cd1SGavin Shan 	pci_set_power_state(pdev, PCI_D0);
69028158cd1SGavin Shan 
69128158cd1SGavin Shan 	/* Save device state */
69228158cd1SGavin Shan 	pci_save_state(pdev);
69328158cd1SGavin Shan 
69428158cd1SGavin Shan 	/*
69528158cd1SGavin Shan 	 * Disable device to avoid any DMA traffic and
69628158cd1SGavin Shan 	 * interrupt from the device
69728158cd1SGavin Shan 	 */
69828158cd1SGavin Shan 	pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
69928158cd1SGavin Shan }
70028158cd1SGavin Shan 
eeh_restore_dev_state(struct eeh_dev * edev,void * userdata)701cef50c67SSam Bobroff static void eeh_restore_dev_state(struct eeh_dev *edev, void *userdata)
70228158cd1SGavin Shan {
70328158cd1SGavin Shan 	struct pci_dev *pdev = eeh_dev_to_pci_dev(edev);
70428158cd1SGavin Shan 	struct pci_dev *dev = userdata;
70528158cd1SGavin Shan 
70628158cd1SGavin Shan 	if (!pdev)
707cef50c67SSam Bobroff 		return;
70828158cd1SGavin Shan 
70928158cd1SGavin Shan 	/* Apply customization from firmware */
7100c2c7652SOliver O'Halloran 	if (eeh_ops->restore_config)
7110c2c7652SOliver O'Halloran 		eeh_ops->restore_config(edev);
71228158cd1SGavin Shan 
71328158cd1SGavin Shan 	/* The caller should restore state for the specified device */
71428158cd1SGavin Shan 	if (pdev != dev)
715502f159cSDavid Gibson 		pci_restore_state(pdev);
71628158cd1SGavin Shan }
71728158cd1SGavin Shan 
718317f06deSGavin Shan /**
71931f6a4adSAndrew Donnellan  * pcibios_set_pcie_reset_state - Set PCI-E reset state
720317f06deSGavin Shan  * @dev: pci device struct
721317f06deSGavin Shan  * @state: reset state to enter
722317f06deSGavin Shan  *
723317f06deSGavin Shan  * Return value:
724317f06deSGavin Shan  * 	0 if success
725317f06deSGavin Shan  */
pcibios_set_pcie_reset_state(struct pci_dev * dev,enum pcie_reset_state state)726317f06deSGavin Shan int pcibios_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
727317f06deSGavin Shan {
728317f06deSGavin Shan 	struct eeh_dev *edev = pci_dev_to_eeh_dev(dev);
7292a58222fSWei Yang 	struct eeh_pe *pe = eeh_dev_to_pe(edev);
730317f06deSGavin Shan 
731317f06deSGavin Shan 	if (!pe) {
732317f06deSGavin Shan 		pr_err("%s: No PE found on PCI device %s\n",
733317f06deSGavin Shan 			__func__, pci_name(dev));
734317f06deSGavin Shan 		return -EINVAL;
735317f06deSGavin Shan 	}
736317f06deSGavin Shan 
737317f06deSGavin Shan 	switch (state) {
738317f06deSGavin Shan 	case pcie_deassert_reset:
739317f06deSGavin Shan 		eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);
740188fdea6SSam Bobroff 		eeh_unfreeze_pe(pe);
7419312bc5bSWei Yang 		if (!(pe->type & EEH_PE_VF))
7429ed5ca66SSam Bobroff 			eeh_pe_state_clear(pe, EEH_PE_CFG_BLOCKED, true);
74328158cd1SGavin Shan 		eeh_pe_dev_traverse(pe, eeh_restore_dev_state, dev);
7449ed5ca66SSam Bobroff 		eeh_pe_state_clear(pe, EEH_PE_ISOLATED, true);
745317f06deSGavin Shan 		break;
746317f06deSGavin Shan 	case pcie_hot_reset:
747e762bb89SSam Bobroff 		eeh_pe_mark_isolated(pe);
7489ed5ca66SSam Bobroff 		eeh_pe_state_clear(pe, EEH_PE_CFG_BLOCKED, true);
74928158cd1SGavin Shan 		eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);
75028158cd1SGavin Shan 		eeh_pe_dev_traverse(pe, eeh_disable_and_save_dev_state, dev);
7519312bc5bSWei Yang 		if (!(pe->type & EEH_PE_VF))
7528a6b3710SGavin Shan 			eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
753317f06deSGavin Shan 		eeh_ops->reset(pe, EEH_RESET_HOT);
754317f06deSGavin Shan 		break;
755317f06deSGavin Shan 	case pcie_warm_reset:
756e762bb89SSam Bobroff 		eeh_pe_mark_isolated(pe);
7579ed5ca66SSam Bobroff 		eeh_pe_state_clear(pe, EEH_PE_CFG_BLOCKED, true);
75828158cd1SGavin Shan 		eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);
75928158cd1SGavin Shan 		eeh_pe_dev_traverse(pe, eeh_disable_and_save_dev_state, dev);
7609312bc5bSWei Yang 		if (!(pe->type & EEH_PE_VF))
7618a6b3710SGavin Shan 			eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
762317f06deSGavin Shan 		eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
763317f06deSGavin Shan 		break;
764317f06deSGavin Shan 	default:
7659ed5ca66SSam Bobroff 		eeh_pe_state_clear(pe, EEH_PE_ISOLATED | EEH_PE_CFG_BLOCKED, true);
766317f06deSGavin Shan 		return -EINVAL;
767f3d03fc7SYang Li 	}
768317f06deSGavin Shan 
769317f06deSGavin Shan 	return 0;
770317f06deSGavin Shan }
771317f06deSGavin Shan 
772317f06deSGavin Shan /**
773b616230eSKai Song  * eeh_set_dev_freset - Check the required reset for the indicated device
774b616230eSKai Song  * @edev: EEH device
775317f06deSGavin Shan  * @flag: return value
776317f06deSGavin Shan  *
777317f06deSGavin Shan  * Each device might have its preferred reset type: fundamental or
778317f06deSGavin Shan  * hot reset. The routine is used to collected the information for
779317f06deSGavin Shan  * the indicated device and its children so that the bunch of the
780317f06deSGavin Shan  * devices could be reset properly.
781317f06deSGavin Shan  */
eeh_set_dev_freset(struct eeh_dev * edev,void * flag)782cef50c67SSam Bobroff static void eeh_set_dev_freset(struct eeh_dev *edev, void *flag)
783317f06deSGavin Shan {
784317f06deSGavin Shan 	struct pci_dev *dev;
785317f06deSGavin Shan 	unsigned int *freset = (unsigned int *)flag;
786317f06deSGavin Shan 
787317f06deSGavin Shan 	dev = eeh_dev_to_pci_dev(edev);
788317f06deSGavin Shan 	if (dev)
789317f06deSGavin Shan 		*freset |= dev->needs_freset;
790317f06deSGavin Shan }
791317f06deSGavin Shan 
eeh_pe_refreeze_passed(struct eeh_pe * root)7921ef52073SSam Bobroff static void eeh_pe_refreeze_passed(struct eeh_pe *root)
7931ef52073SSam Bobroff {
7941ef52073SSam Bobroff 	struct eeh_pe *pe;
7951ef52073SSam Bobroff 	int state;
7961ef52073SSam Bobroff 
7971ef52073SSam Bobroff 	eeh_for_each_pe(root, pe) {
7981ef52073SSam Bobroff 		if (eeh_pe_passed(pe)) {
7991ef52073SSam Bobroff 			state = eeh_ops->get_state(pe, NULL);
8001ef52073SSam Bobroff 			if (state &
8011ef52073SSam Bobroff 			   (EEH_STATE_MMIO_ACTIVE | EEH_STATE_MMIO_ENABLED)) {
8021ef52073SSam Bobroff 				pr_info("EEH: Passed-through PE PHB#%x-PE#%x was thawed by reset, re-freezing for safety.\n",
8031ef52073SSam Bobroff 					pe->phb->global_number, pe->addr);
8041ef52073SSam Bobroff 				eeh_pe_set_option(pe, EEH_OPT_FREEZE_PE);
8051ef52073SSam Bobroff 			}
8061ef52073SSam Bobroff 		}
8071ef52073SSam Bobroff 	}
8081ef52073SSam Bobroff }
8091ef52073SSam Bobroff 
810317f06deSGavin Shan /**
8116654c936SRussell Currey  * eeh_pe_reset_full - Complete a full reset process on the indicated PE
812317f06deSGavin Shan  * @pe: EEH PE
813b616230eSKai Song  * @include_passed: include passed-through devices?
814317f06deSGavin Shan  *
8156654c936SRussell Currey  * This function executes a full reset procedure on a PE, including setting
8166654c936SRussell Currey  * the appropriate flags, performing a fundamental or hot reset, and then
8176654c936SRussell Currey  * deactivating the reset status.  It is designed to be used within the EEH
8186654c936SRussell Currey  * subsystem, as opposed to eeh_pe_reset which is exported to drivers and
8196654c936SRussell Currey  * only performs a single operation at a time.
8206654c936SRussell Currey  *
8216654c936SRussell Currey  * This function will attempt to reset a PE three times before failing.
822317f06deSGavin Shan  */
eeh_pe_reset_full(struct eeh_pe * pe,bool include_passed)8231ef52073SSam Bobroff int eeh_pe_reset_full(struct eeh_pe *pe, bool include_passed)
824317f06deSGavin Shan {
8256654c936SRussell Currey 	int reset_state = (EEH_PE_RESET | EEH_PE_CFG_BLOCKED);
8266654c936SRussell Currey 	int type = EEH_RESET_HOT;
827317f06deSGavin Shan 	unsigned int freset = 0;
828195482c3SSam Bobroff 	int i, state = 0, ret;
829317f06deSGavin Shan 
8306654c936SRussell Currey 	/*
8316654c936SRussell Currey 	 * Determine the type of reset to perform - hot or fundamental.
8326654c936SRussell Currey 	 * Hot reset is the default operation, unless any device under the
8336654c936SRussell Currey 	 * PE requires a fundamental reset.
834317f06deSGavin Shan 	 */
835317f06deSGavin Shan 	eeh_pe_dev_traverse(pe, eeh_set_dev_freset, &freset);
836317f06deSGavin Shan 
837317f06deSGavin Shan 	if (freset)
8386654c936SRussell Currey 		type = EEH_RESET_FUNDAMENTAL;
839317f06deSGavin Shan 
8406654c936SRussell Currey 	/* Mark the PE as in reset state and block config space accesses */
8416654c936SRussell Currey 	eeh_pe_state_mark(pe, reset_state);
842317f06deSGavin Shan 
8436654c936SRussell Currey 	/* Make three attempts at resetting the bus */
844317f06deSGavin Shan 	for (i = 0; i < 3; i++) {
8451ef52073SSam Bobroff 		ret = eeh_pe_reset(pe, type, include_passed);
846195482c3SSam Bobroff 		if (!ret)
847195482c3SSam Bobroff 			ret = eeh_pe_reset(pe, EEH_RESET_DEACTIVATE,
848195482c3SSam Bobroff 					   include_passed);
849195482c3SSam Bobroff 		if (ret) {
850195482c3SSam Bobroff 			ret = -EIO;
851195482c3SSam Bobroff 			pr_warn("EEH: Failure %d resetting PHB#%x-PE#%x (attempt %d)\n\n",
852195482c3SSam Bobroff 				state, pe->phb->global_number, pe->addr, i + 1);
853195482c3SSam Bobroff 			continue;
854195482c3SSam Bobroff 		}
855195482c3SSam Bobroff 		if (i)
856195482c3SSam Bobroff 			pr_warn("EEH: PHB#%x-PE#%x: Successful reset (attempt %d)\n",
857195482c3SSam Bobroff 				pe->phb->global_number, pe->addr, i + 1);
8586654c936SRussell Currey 
8596654c936SRussell Currey 		/* Wait until the PE is in a functioning state */
860fef7f905SSam Bobroff 		state = eeh_wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
861b85743eeSGavin Shan 		if (state < 0) {
862195482c3SSam Bobroff 			pr_warn("EEH: Unrecoverable slot failure on PHB#%x-PE#%x",
863195482c3SSam Bobroff 				pe->phb->global_number, pe->addr);
864b85743eeSGavin Shan 			ret = -ENOTRECOVERABLE;
8656654c936SRussell Currey 			break;
866317f06deSGavin Shan 		}
867fef7f905SSam Bobroff 		if (eeh_state_active(state))
868fef7f905SSam Bobroff 			break;
869195482c3SSam Bobroff 		else
870195482c3SSam Bobroff 			pr_warn("EEH: PHB#%x-PE#%x: Slot inactive after reset: 0x%x (attempt %d)\n",
871195482c3SSam Bobroff 				pe->phb->global_number, pe->addr, state, i + 1);
872b85743eeSGavin Shan 	}
873b85743eeSGavin Shan 
8741ef52073SSam Bobroff 	/* Resetting the PE may have unfrozen child PEs. If those PEs have been
8751ef52073SSam Bobroff 	 * (potentially) passed through to a guest, re-freeze them:
8761ef52073SSam Bobroff 	 */
8771ef52073SSam Bobroff 	if (!include_passed)
8781ef52073SSam Bobroff 		eeh_pe_refreeze_passed(pe);
8791ef52073SSam Bobroff 
8809ed5ca66SSam Bobroff 	eeh_pe_state_clear(pe, reset_state, true);
881b85743eeSGavin Shan 	return ret;
882317f06deSGavin Shan }
883317f06deSGavin Shan 
884317f06deSGavin Shan /**
885317f06deSGavin Shan  * eeh_save_bars - Save device bars
886317f06deSGavin Shan  * @edev: PCI device associated EEH device
887317f06deSGavin Shan  *
888317f06deSGavin Shan  * Save the values of the device bars. Unlike the restore
889317f06deSGavin Shan  * routine, this routine is *not* recursive. This is because
890317f06deSGavin Shan  * PCI devices are added individually; but, for the restore,
891317f06deSGavin Shan  * an entire slot is reset at a time.
892317f06deSGavin Shan  */
eeh_save_bars(struct eeh_dev * edev)893317f06deSGavin Shan void eeh_save_bars(struct eeh_dev *edev)
894317f06deSGavin Shan {
895317f06deSGavin Shan 	int i;
896317f06deSGavin Shan 
89717d2a487SOliver O'Halloran 	if (!edev)
898317f06deSGavin Shan 		return;
899317f06deSGavin Shan 
900317f06deSGavin Shan 	for (i = 0; i < 16; i++)
90117d2a487SOliver O'Halloran 		eeh_ops->read_config(edev, i * 4, 4, &edev->config_space[i]);
902bf898ec5SGavin Shan 
903bf898ec5SGavin Shan 	/*
904bf898ec5SGavin Shan 	 * For PCI bridges including root port, we need enable bus
905bf898ec5SGavin Shan 	 * master explicitly. Otherwise, it can't fetch IODA table
906bf898ec5SGavin Shan 	 * entries correctly. So we cache the bit in advance so that
907bf898ec5SGavin Shan 	 * we can restore it after reset, either PHB range or PE range.
908bf898ec5SGavin Shan 	 */
909bf898ec5SGavin Shan 	if (edev->mode & EEH_DEV_BRIDGE)
910bf898ec5SGavin Shan 		edev->config_space[1] |= PCI_COMMAND_MASTER;
911317f06deSGavin Shan }
912317f06deSGavin Shan 
eeh_reboot_notifier(struct notifier_block * nb,unsigned long action,void * unused)91366f9af83SGavin Shan static int eeh_reboot_notifier(struct notifier_block *nb,
91466f9af83SGavin Shan 			       unsigned long action, void *unused)
91566f9af83SGavin Shan {
91605b1721dSGavin Shan 	eeh_clear_flag(EEH_ENABLED);
91766f9af83SGavin Shan 	return NOTIFY_DONE;
91866f9af83SGavin Shan }
91966f9af83SGavin Shan 
92066f9af83SGavin Shan static struct notifier_block eeh_reboot_nb = {
92166f9af83SGavin Shan 	.notifier_call = eeh_reboot_notifier,
92266f9af83SGavin Shan };
92366f9af83SGavin Shan 
eeh_device_notifier(struct notifier_block * nb,unsigned long action,void * data)924466381ecSSam Bobroff static int eeh_device_notifier(struct notifier_block *nb,
925466381ecSSam Bobroff 			       unsigned long action, void *data)
926466381ecSSam Bobroff {
927466381ecSSam Bobroff 	struct device *dev = data;
928466381ecSSam Bobroff 
929466381ecSSam Bobroff 	switch (action) {
930466381ecSSam Bobroff 	/*
931466381ecSSam Bobroff 	 * Note: It's not possible to perform EEH device addition (i.e.
932466381ecSSam Bobroff 	 * {pseries,pnv}_pcibios_bus_add_device()) here because it depends on
933466381ecSSam Bobroff 	 * the device's resources, which have not yet been set up.
934466381ecSSam Bobroff 	 */
935466381ecSSam Bobroff 	case BUS_NOTIFY_DEL_DEVICE:
936466381ecSSam Bobroff 		eeh_remove_device(to_pci_dev(dev));
937466381ecSSam Bobroff 		break;
938466381ecSSam Bobroff 	default:
939466381ecSSam Bobroff 		break;
940466381ecSSam Bobroff 	}
941466381ecSSam Bobroff 	return NOTIFY_DONE;
942466381ecSSam Bobroff }
943466381ecSSam Bobroff 
944466381ecSSam Bobroff static struct notifier_block eeh_device_nb = {
945466381ecSSam Bobroff 	.notifier_call = eeh_device_notifier,
946466381ecSSam Bobroff };
947466381ecSSam Bobroff 
948395ee2a2SOliver O'Halloran /**
949395ee2a2SOliver O'Halloran  * eeh_init - System wide EEH initialization
950b616230eSKai Song  * @ops: struct to trace EEH operation callback functions
951395ee2a2SOliver O'Halloran  *
952395ee2a2SOliver O'Halloran  * It's the platform's job to call this from an arch_initcall().
953395ee2a2SOliver O'Halloran  */
eeh_init(struct eeh_ops * ops)954395ee2a2SOliver O'Halloran int eeh_init(struct eeh_ops *ops)
955466381ecSSam Bobroff {
956395ee2a2SOliver O'Halloran 	struct pci_controller *hose, *tmp;
957395ee2a2SOliver O'Halloran 	int ret = 0;
958395ee2a2SOliver O'Halloran 
959395ee2a2SOliver O'Halloran 	/* the platform should only initialise EEH once */
960395ee2a2SOliver O'Halloran 	if (WARN_ON(eeh_ops))
961395ee2a2SOliver O'Halloran 		return -EEXIST;
962395ee2a2SOliver O'Halloran 	if (WARN_ON(!ops))
963395ee2a2SOliver O'Halloran 		return -ENOENT;
964395ee2a2SOliver O'Halloran 	eeh_ops = ops;
965395ee2a2SOliver O'Halloran 
966395ee2a2SOliver O'Halloran 	/* Register reboot notifier */
967395ee2a2SOliver O'Halloran 	ret = register_reboot_notifier(&eeh_reboot_nb);
968395ee2a2SOliver O'Halloran 	if (ret) {
969395ee2a2SOliver O'Halloran 		pr_warn("%s: Failed to register reboot notifier (%d)\n",
970395ee2a2SOliver O'Halloran 			__func__, ret);
971395ee2a2SOliver O'Halloran 		return ret;
972466381ecSSam Bobroff 	}
973395ee2a2SOliver O'Halloran 
974395ee2a2SOliver O'Halloran 	ret = bus_register_notifier(&pci_bus_type, &eeh_device_nb);
975395ee2a2SOliver O'Halloran 	if (ret) {
976395ee2a2SOliver O'Halloran 		pr_warn("%s: Failed to register bus notifier (%d)\n",
977395ee2a2SOliver O'Halloran 			__func__, ret);
978395ee2a2SOliver O'Halloran 		return ret;
979395ee2a2SOliver O'Halloran 	}
980395ee2a2SOliver O'Halloran 
981395ee2a2SOliver O'Halloran 	/* Initialize PHB PEs */
982395ee2a2SOliver O'Halloran 	list_for_each_entry_safe(hose, tmp, &hose_list, list_node)
983395ee2a2SOliver O'Halloran 		eeh_phb_pe_create(hose);
984395ee2a2SOliver O'Halloran 
985395ee2a2SOliver O'Halloran 	eeh_addr_cache_init();
986395ee2a2SOliver O'Halloran 
987395ee2a2SOliver O'Halloran 	/* Initialize EEH event */
988395ee2a2SOliver O'Halloran 	return eeh_event_init();
989395ee2a2SOliver O'Halloran }
990466381ecSSam Bobroff 
991317f06deSGavin Shan /**
992e86350f7SOliver O'Halloran  * eeh_probe_device() - Perform EEH initialization for the indicated pci device
993317f06deSGavin Shan  * @dev: pci device for which to set up EEH
994317f06deSGavin Shan  *
995317f06deSGavin Shan  * This routine must be used to complete EEH initialization for PCI
996317f06deSGavin Shan  * devices that were added after system boot (e.g. hotplug, dlpar).
997317f06deSGavin Shan  */
eeh_probe_device(struct pci_dev * dev)998e86350f7SOliver O'Halloran void eeh_probe_device(struct pci_dev *dev)
999317f06deSGavin Shan {
1000317f06deSGavin Shan 	struct eeh_dev *edev;
1001317f06deSGavin Shan 
1002e86350f7SOliver O'Halloran 	pr_debug("EEH: Adding device %s\n", pci_name(dev));
1003317f06deSGavin Shan 
1004e86350f7SOliver O'Halloran 	/*
1005e86350f7SOliver O'Halloran 	 * pci_dev_to_eeh_dev() can only work if eeh_probe_dev() was
1006e86350f7SOliver O'Halloran 	 * already called for this device.
1007e86350f7SOliver O'Halloran 	 */
1008e86350f7SOliver O'Halloran 	if (WARN_ON_ONCE(pci_dev_to_eeh_dev(dev))) {
1009e86350f7SOliver O'Halloran 		pci_dbg(dev, "Already bound to an eeh_dev!\n");
1010e86350f7SOliver O'Halloran 		return;
1011e86350f7SOliver O'Halloran 	}
1012e86350f7SOliver O'Halloran 
1013e86350f7SOliver O'Halloran 	edev = eeh_ops->probe(dev);
1014e86350f7SOliver O'Halloran 	if (!edev) {
1015e86350f7SOliver O'Halloran 		pr_debug("EEH: Adding device failed\n");
1016317f06deSGavin Shan 		return;
1017317f06deSGavin Shan 	}
1018f5c57710SGavin Shan 
1019f5c57710SGavin Shan 	/*
1020e86350f7SOliver O'Halloran 	 * FIXME: We rely on pcibios_release_device() to remove the
1021e86350f7SOliver O'Halloran 	 * existing EEH state. The release function is only called if
1022e86350f7SOliver O'Halloran 	 * the pci_dev's refcount drops to zero so if something is
1023e86350f7SOliver O'Halloran 	 * keeping a ref to a device (e.g. a filesystem) we need to
1024e86350f7SOliver O'Halloran 	 * remove the old EEH state.
1025e86350f7SOliver O'Halloran 	 *
1026e86350f7SOliver O'Halloran 	 * FIXME: HEY MA, LOOK AT ME, NO LOCKING!
1027f5c57710SGavin Shan 	 */
1028e86350f7SOliver O'Halloran 	if (edev->pdev && edev->pdev != dev) {
1029d923ab7aSOliver O'Halloran 		eeh_pe_tree_remove(edev);
1030f5c57710SGavin Shan 		eeh_addr_cache_rmv_dev(edev->pdev);
1031f5c57710SGavin Shan 		eeh_sysfs_remove_device(edev->pdev);
1032f5c57710SGavin Shan 
1033f26c7a03SGavin Shan 		/*
1034f26c7a03SGavin Shan 		 * We definitely should have the PCI device removed
1035f26c7a03SGavin Shan 		 * though it wasn't correctly. So we needn't call
1036f26c7a03SGavin Shan 		 * into error handler afterwards.
1037f26c7a03SGavin Shan 		 */
1038f26c7a03SGavin Shan 		edev->mode |= EEH_DEV_NO_HANDLER;
1039f5c57710SGavin Shan 	}
1040317f06deSGavin Shan 
1041e86350f7SOliver O'Halloran 	/* bind the pdev and the edev together */
1042317f06deSGavin Shan 	edev->pdev = dev;
1043317f06deSGavin Shan 	dev->dev.archdata.edev = edev;
1044317f06deSGavin Shan 	eeh_addr_cache_insert_dev(dev);
10458645aaa8SOliver O'Halloran 	eeh_sysfs_add_device(dev);
1046317f06deSGavin Shan }
1047317f06deSGavin Shan 
1048317f06deSGavin Shan /**
1049317f06deSGavin Shan  * eeh_remove_device - Undo EEH setup for the indicated pci device
1050317f06deSGavin Shan  * @dev: pci device to be removed
1051317f06deSGavin Shan  *
1052317f06deSGavin Shan  * This routine should be called when a device is removed from
1053317f06deSGavin Shan  * a running system (e.g. by hotplug or dlpar).  It unregisters
1054317f06deSGavin Shan  * the PCI device from the EEH subsystem.  I/O errors affecting
1055317f06deSGavin Shan  * this device will no longer be detected after this call; thus,
1056317f06deSGavin Shan  * i/o errors affecting this slot may leave this device unusable.
1057317f06deSGavin Shan  */
eeh_remove_device(struct pci_dev * dev)1058807a827dSGavin Shan void eeh_remove_device(struct pci_dev *dev)
1059317f06deSGavin Shan {
1060317f06deSGavin Shan 	struct eeh_dev *edev;
1061317f06deSGavin Shan 
10622ec5a0adSGavin Shan 	if (!dev || !eeh_enabled())
1063317f06deSGavin Shan 		return;
1064317f06deSGavin Shan 	edev = pci_dev_to_eeh_dev(dev);
1065317f06deSGavin Shan 
1066317f06deSGavin Shan 	/* Unregister the device with the EEH/PCI address search system */
10671ff8f36fSSam Bobroff 	dev_dbg(&dev->dev, "EEH: Removing device\n");
1068317f06deSGavin Shan 
1069f5c57710SGavin Shan 	if (!edev || !edev->pdev || !edev->pe) {
10701ff8f36fSSam Bobroff 		dev_dbg(&dev->dev, "EEH: Device not referenced!\n");
1071317f06deSGavin Shan 		return;
1072317f06deSGavin Shan 	}
1073f5c57710SGavin Shan 
1074f5c57710SGavin Shan 	/*
1075f5c57710SGavin Shan 	 * During the hotplug for EEH error recovery, we need the EEH
1076f5c57710SGavin Shan 	 * device attached to the parent PE in order for BAR restore
1077f5c57710SGavin Shan 	 * a bit later. So we keep it for BAR restore and remove it
1078f5c57710SGavin Shan 	 * from the parent PE during the BAR resotre.
1079f5c57710SGavin Shan 	 */
1080317f06deSGavin Shan 	edev->pdev = NULL;
108167086e32SWei Yang 
108267086e32SWei Yang 	/*
10833489cdc4SOliver O'Halloran 	 * eeh_sysfs_remove_device() uses pci_dev_to_eeh_dev() so we need to
10843489cdc4SOliver O'Halloran 	 * remove the sysfs files before clearing dev.archdata.edev
10853489cdc4SOliver O'Halloran 	 */
10863489cdc4SOliver O'Halloran 	if (edev->mode & EEH_DEV_SYSFS)
10873489cdc4SOliver O'Halloran 		eeh_sysfs_remove_device(dev);
10883489cdc4SOliver O'Halloran 
10893489cdc4SOliver O'Halloran 	/*
10903489cdc4SOliver O'Halloran 	 * We're removing from the PCI subsystem, that means
10913489cdc4SOliver O'Halloran 	 * the PCI device driver can't support EEH or not
10923489cdc4SOliver O'Halloran 	 * well. So we rely on hotplug completely to do recovery
10933489cdc4SOliver O'Halloran 	 * for the specific PCI device.
10943489cdc4SOliver O'Halloran 	 */
10953489cdc4SOliver O'Halloran 	edev->mode |= EEH_DEV_NO_HANDLER;
10963489cdc4SOliver O'Halloran 
10973489cdc4SOliver O'Halloran 	eeh_addr_cache_rmv_dev(dev);
10983489cdc4SOliver O'Halloran 
10993489cdc4SOliver O'Halloran 	/*
110067086e32SWei Yang 	 * The flag "in_error" is used to trace EEH devices for VFs
110167086e32SWei Yang 	 * in error state or not. It's set in eeh_report_error(). If
110267086e32SWei Yang 	 * it's not set, eeh_report_{reset,resume}() won't be called
110367086e32SWei Yang 	 * for the VF EEH device.
110467086e32SWei Yang 	 */
110567086e32SWei Yang 	edev->in_error = false;
1106317f06deSGavin Shan 	dev->dev.archdata.edev = NULL;
1107f5c57710SGavin Shan 	if (!(edev->pe->state & EEH_PE_KEEP))
1108d923ab7aSOliver O'Halloran 		eeh_pe_tree_remove(edev);
1109f5c57710SGavin Shan 	else
1110f5c57710SGavin Shan 		edev->mode |= EEH_DEV_DISCONNECTED;
1111317f06deSGavin Shan }
1112317f06deSGavin Shan 
eeh_unfreeze_pe(struct eeh_pe * pe)1113188fdea6SSam Bobroff int eeh_unfreeze_pe(struct eeh_pe *pe)
11144eeeff0eSGavin Shan {
11154eeeff0eSGavin Shan 	int ret;
11164eeeff0eSGavin Shan 
11174eeeff0eSGavin Shan 	ret = eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
11184eeeff0eSGavin Shan 	if (ret) {
11194eeeff0eSGavin Shan 		pr_warn("%s: Failure %d enabling IO on PHB#%x-PE#%x\n",
11204eeeff0eSGavin Shan 			__func__, ret, pe->phb->global_number, pe->addr);
11214eeeff0eSGavin Shan 		return ret;
11224eeeff0eSGavin Shan 	}
11234eeeff0eSGavin Shan 
11244eeeff0eSGavin Shan 	ret = eeh_pci_enable(pe, EEH_OPT_THAW_DMA);
11254eeeff0eSGavin Shan 	if (ret) {
11264eeeff0eSGavin Shan 		pr_warn("%s: Failure %d enabling DMA on PHB#%x-PE#%x\n",
11274eeeff0eSGavin Shan 			__func__, ret, pe->phb->global_number, pe->addr);
11284eeeff0eSGavin Shan 		return ret;
11294eeeff0eSGavin Shan 	}
11304eeeff0eSGavin Shan 
11314eeeff0eSGavin Shan 	return ret;
11324eeeff0eSGavin Shan }
11334eeeff0eSGavin Shan 
11345cfb20b9SGavin Shan 
11355cfb20b9SGavin Shan static struct pci_device_id eeh_reset_ids[] = {
11365cfb20b9SGavin Shan 	{ PCI_DEVICE(0x19a2, 0x0710) },	/* Emulex, BE     */
11375cfb20b9SGavin Shan 	{ PCI_DEVICE(0x10df, 0xe220) },	/* Emulex, Lancer */
1138b1d76a7dSGavin Shan 	{ PCI_DEVICE(0x14e4, 0x1657) }, /* Broadcom BCM5719 */
11395cfb20b9SGavin Shan 	{ 0 }
11405cfb20b9SGavin Shan };
11415cfb20b9SGavin Shan 
eeh_pe_change_owner(struct eeh_pe * pe)11425cfb20b9SGavin Shan static int eeh_pe_change_owner(struct eeh_pe *pe)
11435cfb20b9SGavin Shan {
11445cfb20b9SGavin Shan 	struct eeh_dev *edev, *tmp;
11455cfb20b9SGavin Shan 	struct pci_dev *pdev;
11465cfb20b9SGavin Shan 	struct pci_device_id *id;
114734a286a4SSam Bobroff 	int ret;
11485cfb20b9SGavin Shan 
11495cfb20b9SGavin Shan 	/* Check PE state */
11505cfb20b9SGavin Shan 	ret = eeh_ops->get_state(pe, NULL);
11515cfb20b9SGavin Shan 	if (ret < 0 || ret == EEH_STATE_NOT_SUPPORT)
11525cfb20b9SGavin Shan 		return 0;
11535cfb20b9SGavin Shan 
11545cfb20b9SGavin Shan 	/* Unfrozen PE, nothing to do */
115534a286a4SSam Bobroff 	if (eeh_state_active(ret))
11565cfb20b9SGavin Shan 		return 0;
11575cfb20b9SGavin Shan 
11585cfb20b9SGavin Shan 	/* Frozen PE, check if it needs PE level reset */
11595cfb20b9SGavin Shan 	eeh_pe_for_each_dev(pe, edev, tmp) {
11605cfb20b9SGavin Shan 		pdev = eeh_dev_to_pci_dev(edev);
11615cfb20b9SGavin Shan 		if (!pdev)
11625cfb20b9SGavin Shan 			continue;
11635cfb20b9SGavin Shan 
11645cfb20b9SGavin Shan 		for (id = &eeh_reset_ids[0]; id->vendor != 0; id++) {
11655cfb20b9SGavin Shan 			if (id->vendor != PCI_ANY_ID &&
11665cfb20b9SGavin Shan 			    id->vendor != pdev->vendor)
11675cfb20b9SGavin Shan 				continue;
11685cfb20b9SGavin Shan 			if (id->device != PCI_ANY_ID &&
11695cfb20b9SGavin Shan 			    id->device != pdev->device)
11705cfb20b9SGavin Shan 				continue;
11715cfb20b9SGavin Shan 			if (id->subvendor != PCI_ANY_ID &&
11725cfb20b9SGavin Shan 			    id->subvendor != pdev->subsystem_vendor)
11735cfb20b9SGavin Shan 				continue;
11745cfb20b9SGavin Shan 			if (id->subdevice != PCI_ANY_ID &&
11755cfb20b9SGavin Shan 			    id->subdevice != pdev->subsystem_device)
11765cfb20b9SGavin Shan 				continue;
11775cfb20b9SGavin Shan 
1178d6d63d72SGavin Shan 			return eeh_pe_reset_and_recover(pe);
11795cfb20b9SGavin Shan 		}
11805cfb20b9SGavin Shan 	}
11815cfb20b9SGavin Shan 
1182188fdea6SSam Bobroff 	ret = eeh_unfreeze_pe(pe);
1183188fdea6SSam Bobroff 	if (!ret)
11849ed5ca66SSam Bobroff 		eeh_pe_state_clear(pe, EEH_PE_ISOLATED, true);
1185188fdea6SSam Bobroff 	return ret;
11865cfb20b9SGavin Shan }
11875cfb20b9SGavin Shan 
1188212d16cdSGavin Shan /**
1189212d16cdSGavin Shan  * eeh_dev_open - Increase count of pass through devices for PE
1190212d16cdSGavin Shan  * @pdev: PCI device
1191212d16cdSGavin Shan  *
1192212d16cdSGavin Shan  * Increase count of passed through devices for the indicated
1193212d16cdSGavin Shan  * PE. In the result, the EEH errors detected on the PE won't be
1194212d16cdSGavin Shan  * reported. The PE owner will be responsible for detection
1195212d16cdSGavin Shan  * and recovery.
1196212d16cdSGavin Shan  */
eeh_dev_open(struct pci_dev * pdev)1197212d16cdSGavin Shan int eeh_dev_open(struct pci_dev *pdev)
1198212d16cdSGavin Shan {
1199212d16cdSGavin Shan 	struct eeh_dev *edev;
1200404079c8SGavin Shan 	int ret = -ENODEV;
1201212d16cdSGavin Shan 
1202212d16cdSGavin Shan 	mutex_lock(&eeh_dev_mutex);
1203212d16cdSGavin Shan 
1204212d16cdSGavin Shan 	/* No PCI device ? */
1205212d16cdSGavin Shan 	if (!pdev)
1206212d16cdSGavin Shan 		goto out;
1207212d16cdSGavin Shan 
1208212d16cdSGavin Shan 	/* No EEH device or PE ? */
1209212d16cdSGavin Shan 	edev = pci_dev_to_eeh_dev(pdev);
1210212d16cdSGavin Shan 	if (!edev || !edev->pe)
1211212d16cdSGavin Shan 		goto out;
1212212d16cdSGavin Shan 
1213404079c8SGavin Shan 	/*
1214404079c8SGavin Shan 	 * The PE might have been put into frozen state, but we
1215404079c8SGavin Shan 	 * didn't detect that yet. The passed through PCI devices
1216404079c8SGavin Shan 	 * in frozen PE won't work properly. Clear the frozen state
1217404079c8SGavin Shan 	 * in advance.
1218404079c8SGavin Shan 	 */
12195cfb20b9SGavin Shan 	ret = eeh_pe_change_owner(edev->pe);
12204eeeff0eSGavin Shan 	if (ret)
1221404079c8SGavin Shan 		goto out;
1222404079c8SGavin Shan 
1223212d16cdSGavin Shan 	/* Increase PE's pass through count */
1224212d16cdSGavin Shan 	atomic_inc(&edev->pe->pass_dev_cnt);
1225212d16cdSGavin Shan 	mutex_unlock(&eeh_dev_mutex);
1226212d16cdSGavin Shan 
1227212d16cdSGavin Shan 	return 0;
1228212d16cdSGavin Shan out:
1229212d16cdSGavin Shan 	mutex_unlock(&eeh_dev_mutex);
1230404079c8SGavin Shan 	return ret;
1231212d16cdSGavin Shan }
1232212d16cdSGavin Shan EXPORT_SYMBOL_GPL(eeh_dev_open);
1233212d16cdSGavin Shan 
1234212d16cdSGavin Shan /**
1235212d16cdSGavin Shan  * eeh_dev_release - Decrease count of pass through devices for PE
1236212d16cdSGavin Shan  * @pdev: PCI device
1237212d16cdSGavin Shan  *
1238212d16cdSGavin Shan  * Decrease count of pass through devices for the indicated PE. If
1239212d16cdSGavin Shan  * there is no passed through device in PE, the EEH errors detected
1240212d16cdSGavin Shan  * on the PE will be reported and handled as usual.
1241212d16cdSGavin Shan  */
eeh_dev_release(struct pci_dev * pdev)1242212d16cdSGavin Shan void eeh_dev_release(struct pci_dev *pdev)
1243212d16cdSGavin Shan {
1244212d16cdSGavin Shan 	struct eeh_dev *edev;
1245212d16cdSGavin Shan 
1246212d16cdSGavin Shan 	mutex_lock(&eeh_dev_mutex);
1247212d16cdSGavin Shan 
1248212d16cdSGavin Shan 	/* No PCI device ? */
1249212d16cdSGavin Shan 	if (!pdev)
1250212d16cdSGavin Shan 		goto out;
1251212d16cdSGavin Shan 
1252212d16cdSGavin Shan 	/* No EEH device ? */
1253212d16cdSGavin Shan 	edev = pci_dev_to_eeh_dev(pdev);
1254212d16cdSGavin Shan 	if (!edev || !edev->pe || !eeh_pe_passed(edev->pe))
1255212d16cdSGavin Shan 		goto out;
1256212d16cdSGavin Shan 
1257212d16cdSGavin Shan 	/* Decrease PE's pass through count */
125854f9a64aSGavin Shan 	WARN_ON(atomic_dec_if_positive(&edev->pe->pass_dev_cnt) < 0);
12595cfb20b9SGavin Shan 	eeh_pe_change_owner(edev->pe);
1260212d16cdSGavin Shan out:
1261212d16cdSGavin Shan 	mutex_unlock(&eeh_dev_mutex);
1262212d16cdSGavin Shan }
1263212d16cdSGavin Shan EXPORT_SYMBOL(eeh_dev_release);
1264212d16cdSGavin Shan 
12652194dc27SBenjamin Herrenschmidt #ifdef CONFIG_IOMMU_API
12662194dc27SBenjamin Herrenschmidt 
dev_has_iommu_table(struct device * dev,void * data)1267a3032ca9SGavin Shan static int dev_has_iommu_table(struct device *dev, void *data)
1268a3032ca9SGavin Shan {
1269a3032ca9SGavin Shan 	struct pci_dev *pdev = to_pci_dev(dev);
1270a3032ca9SGavin Shan 	struct pci_dev **ppdev = data;
1271a3032ca9SGavin Shan 
1272a3032ca9SGavin Shan 	if (!dev)
1273a3032ca9SGavin Shan 		return 0;
1274a3032ca9SGavin Shan 
1275bf8763d8SJoerg Roedel 	if (device_iommu_mapped(dev)) {
1276a3032ca9SGavin Shan 		*ppdev = pdev;
1277a3032ca9SGavin Shan 		return 1;
1278a3032ca9SGavin Shan 	}
1279a3032ca9SGavin Shan 
1280a3032ca9SGavin Shan 	return 0;
1281a3032ca9SGavin Shan }
1282a3032ca9SGavin Shan 
1283212d16cdSGavin Shan /**
1284212d16cdSGavin Shan  * eeh_iommu_group_to_pe - Convert IOMMU group to EEH PE
1285212d16cdSGavin Shan  * @group: IOMMU group
1286212d16cdSGavin Shan  *
1287212d16cdSGavin Shan  * The routine is called to convert IOMMU group to EEH PE.
1288212d16cdSGavin Shan  */
eeh_iommu_group_to_pe(struct iommu_group * group)1289212d16cdSGavin Shan struct eeh_pe *eeh_iommu_group_to_pe(struct iommu_group *group)
1290212d16cdSGavin Shan {
1291212d16cdSGavin Shan 	struct pci_dev *pdev = NULL;
1292212d16cdSGavin Shan 	struct eeh_dev *edev;
1293a3032ca9SGavin Shan 	int ret;
1294212d16cdSGavin Shan 
1295212d16cdSGavin Shan 	/* No IOMMU group ? */
1296212d16cdSGavin Shan 	if (!group)
1297212d16cdSGavin Shan 		return NULL;
1298212d16cdSGavin Shan 
1299a3032ca9SGavin Shan 	ret = iommu_group_for_each_dev(group, &pdev, dev_has_iommu_table);
1300a3032ca9SGavin Shan 	if (!ret || !pdev)
1301212d16cdSGavin Shan 		return NULL;
1302212d16cdSGavin Shan 
1303212d16cdSGavin Shan 	/* No EEH device or PE ? */
1304212d16cdSGavin Shan 	edev = pci_dev_to_eeh_dev(pdev);
1305212d16cdSGavin Shan 	if (!edev || !edev->pe)
1306212d16cdSGavin Shan 		return NULL;
1307212d16cdSGavin Shan 
1308212d16cdSGavin Shan 	return edev->pe;
1309212d16cdSGavin Shan }
1310537e5400SGavin Shan EXPORT_SYMBOL_GPL(eeh_iommu_group_to_pe);
1311212d16cdSGavin Shan 
13122194dc27SBenjamin Herrenschmidt #endif /* CONFIG_IOMMU_API */
13132194dc27SBenjamin Herrenschmidt 
1314212d16cdSGavin Shan /**
1315212d16cdSGavin Shan  * eeh_pe_set_option - Set options for the indicated PE
1316212d16cdSGavin Shan  * @pe: EEH PE
1317212d16cdSGavin Shan  * @option: requested option
1318212d16cdSGavin Shan  *
1319212d16cdSGavin Shan  * The routine is called to enable or disable EEH functionality
1320212d16cdSGavin Shan  * on the indicated PE, to enable IO or DMA for the frozen PE.
1321212d16cdSGavin Shan  */
eeh_pe_set_option(struct eeh_pe * pe,int option)1322212d16cdSGavin Shan int eeh_pe_set_option(struct eeh_pe *pe, int option)
1323212d16cdSGavin Shan {
1324212d16cdSGavin Shan 	int ret = 0;
1325212d16cdSGavin Shan 
1326212d16cdSGavin Shan 	/* Invalid PE ? */
1327212d16cdSGavin Shan 	if (!pe)
1328212d16cdSGavin Shan 		return -ENODEV;
1329212d16cdSGavin Shan 
1330212d16cdSGavin Shan 	/*
1331212d16cdSGavin Shan 	 * EEH functionality could possibly be disabled, just
1332*1fd02f66SJulia Lawall 	 * return error for the case. And the EEH functionality
1333212d16cdSGavin Shan 	 * isn't expected to be disabled on one specific PE.
1334212d16cdSGavin Shan 	 */
1335212d16cdSGavin Shan 	switch (option) {
1336212d16cdSGavin Shan 	case EEH_OPT_ENABLE:
13374eeeff0eSGavin Shan 		if (eeh_enabled()) {
13385cfb20b9SGavin Shan 			ret = eeh_pe_change_owner(pe);
1339212d16cdSGavin Shan 			break;
13404eeeff0eSGavin Shan 		}
1341212d16cdSGavin Shan 		ret = -EIO;
1342212d16cdSGavin Shan 		break;
1343212d16cdSGavin Shan 	case EEH_OPT_DISABLE:
1344212d16cdSGavin Shan 		break;
1345212d16cdSGavin Shan 	case EEH_OPT_THAW_MMIO:
1346212d16cdSGavin Shan 	case EEH_OPT_THAW_DMA:
1347de5a6622SGavin Shan 	case EEH_OPT_FREEZE_PE:
1348212d16cdSGavin Shan 		if (!eeh_ops || !eeh_ops->set_option) {
1349212d16cdSGavin Shan 			ret = -ENOENT;
1350212d16cdSGavin Shan 			break;
1351212d16cdSGavin Shan 		}
1352212d16cdSGavin Shan 
13534eeeff0eSGavin Shan 		ret = eeh_pci_enable(pe, option);
1354212d16cdSGavin Shan 		break;
1355212d16cdSGavin Shan 	default:
1356212d16cdSGavin Shan 		pr_debug("%s: Option %d out of range (%d, %d)\n",
1357212d16cdSGavin Shan 			__func__, option, EEH_OPT_DISABLE, EEH_OPT_THAW_DMA);
1358212d16cdSGavin Shan 		ret = -EINVAL;
1359212d16cdSGavin Shan 	}
1360212d16cdSGavin Shan 
1361212d16cdSGavin Shan 	return ret;
1362212d16cdSGavin Shan }
1363212d16cdSGavin Shan EXPORT_SYMBOL_GPL(eeh_pe_set_option);
1364212d16cdSGavin Shan 
1365212d16cdSGavin Shan /**
1366212d16cdSGavin Shan  * eeh_pe_get_state - Retrieve PE's state
1367212d16cdSGavin Shan  * @pe: EEH PE
1368212d16cdSGavin Shan  *
1369212d16cdSGavin Shan  * Retrieve the PE's state, which includes 3 aspects: enabled
1370212d16cdSGavin Shan  * DMA, enabled IO and asserted reset.
1371212d16cdSGavin Shan  */
eeh_pe_get_state(struct eeh_pe * pe)1372212d16cdSGavin Shan int eeh_pe_get_state(struct eeh_pe *pe)
1373212d16cdSGavin Shan {
1374212d16cdSGavin Shan 	int result, ret = 0;
1375212d16cdSGavin Shan 	bool rst_active, dma_en, mmio_en;
1376212d16cdSGavin Shan 
1377212d16cdSGavin Shan 	/* Existing PE ? */
1378212d16cdSGavin Shan 	if (!pe)
1379212d16cdSGavin Shan 		return -ENODEV;
1380212d16cdSGavin Shan 
1381212d16cdSGavin Shan 	if (!eeh_ops || !eeh_ops->get_state)
1382212d16cdSGavin Shan 		return -ENOENT;
1383212d16cdSGavin Shan 
1384eca036eeSGavin Shan 	/*
1385eca036eeSGavin Shan 	 * If the parent PE is owned by the host kernel and is undergoing
1386eca036eeSGavin Shan 	 * error recovery, we should return the PE state as temporarily
1387eca036eeSGavin Shan 	 * unavailable so that the error recovery on the guest is suspended
1388eca036eeSGavin Shan 	 * until the recovery completes on the host.
1389eca036eeSGavin Shan 	 */
1390eca036eeSGavin Shan 	if (pe->parent &&
1391eca036eeSGavin Shan 	    !(pe->state & EEH_PE_REMOVED) &&
1392eca036eeSGavin Shan 	    (pe->parent->state & (EEH_PE_ISOLATED | EEH_PE_RECOVERING)))
1393eca036eeSGavin Shan 		return EEH_PE_STATE_UNAVAIL;
1394eca036eeSGavin Shan 
1395212d16cdSGavin Shan 	result = eeh_ops->get_state(pe, NULL);
1396212d16cdSGavin Shan 	rst_active = !!(result & EEH_STATE_RESET_ACTIVE);
1397212d16cdSGavin Shan 	dma_en = !!(result & EEH_STATE_DMA_ENABLED);
1398212d16cdSGavin Shan 	mmio_en = !!(result & EEH_STATE_MMIO_ENABLED);
1399212d16cdSGavin Shan 
1400212d16cdSGavin Shan 	if (rst_active)
1401212d16cdSGavin Shan 		ret = EEH_PE_STATE_RESET;
1402212d16cdSGavin Shan 	else if (dma_en && mmio_en)
1403212d16cdSGavin Shan 		ret = EEH_PE_STATE_NORMAL;
1404212d16cdSGavin Shan 	else if (!dma_en && !mmio_en)
1405212d16cdSGavin Shan 		ret = EEH_PE_STATE_STOPPED_IO_DMA;
1406212d16cdSGavin Shan 	else if (!dma_en && mmio_en)
1407212d16cdSGavin Shan 		ret = EEH_PE_STATE_STOPPED_DMA;
1408212d16cdSGavin Shan 	else
1409212d16cdSGavin Shan 		ret = EEH_PE_STATE_UNAVAIL;
1410212d16cdSGavin Shan 
1411212d16cdSGavin Shan 	return ret;
1412212d16cdSGavin Shan }
1413212d16cdSGavin Shan EXPORT_SYMBOL_GPL(eeh_pe_get_state);
1414212d16cdSGavin Shan 
eeh_pe_reenable_devices(struct eeh_pe * pe,bool include_passed)14151ef52073SSam Bobroff static int eeh_pe_reenable_devices(struct eeh_pe *pe, bool include_passed)
1416316233ffSGavin Shan {
1417316233ffSGavin Shan 	struct eeh_dev *edev, *tmp;
1418316233ffSGavin Shan 	struct pci_dev *pdev;
1419316233ffSGavin Shan 	int ret = 0;
1420316233ffSGavin Shan 
1421316233ffSGavin Shan 	eeh_pe_restore_bars(pe);
1422316233ffSGavin Shan 
1423316233ffSGavin Shan 	/*
1424316233ffSGavin Shan 	 * Reenable PCI devices as the devices passed
1425316233ffSGavin Shan 	 * through are always enabled before the reset.
1426316233ffSGavin Shan 	 */
1427316233ffSGavin Shan 	eeh_pe_for_each_dev(pe, edev, tmp) {
1428316233ffSGavin Shan 		pdev = eeh_dev_to_pci_dev(edev);
1429316233ffSGavin Shan 		if (!pdev)
1430316233ffSGavin Shan 			continue;
1431316233ffSGavin Shan 
1432316233ffSGavin Shan 		ret = pci_reenable_device(pdev);
1433316233ffSGavin Shan 		if (ret) {
1434316233ffSGavin Shan 			pr_warn("%s: Failure %d reenabling %s\n",
1435316233ffSGavin Shan 				__func__, ret, pci_name(pdev));
1436316233ffSGavin Shan 			return ret;
1437316233ffSGavin Shan 		}
1438316233ffSGavin Shan 	}
1439316233ffSGavin Shan 
1440316233ffSGavin Shan 	/* The PE is still in frozen state */
14411ef52073SSam Bobroff 	if (include_passed || !eeh_pe_passed(pe)) {
1442188fdea6SSam Bobroff 		ret = eeh_unfreeze_pe(pe);
14431ef52073SSam Bobroff 	} else
14441ef52073SSam Bobroff 		pr_info("EEH: Note: Leaving passthrough PHB#%x-PE#%x frozen.\n",
14451ef52073SSam Bobroff 			pe->phb->global_number, pe->addr);
1446188fdea6SSam Bobroff 	if (!ret)
14471ef52073SSam Bobroff 		eeh_pe_state_clear(pe, EEH_PE_ISOLATED, include_passed);
1448188fdea6SSam Bobroff 	return ret;
1449316233ffSGavin Shan }
1450316233ffSGavin Shan 
14516654c936SRussell Currey 
1452212d16cdSGavin Shan /**
1453212d16cdSGavin Shan  * eeh_pe_reset - Issue PE reset according to specified type
1454212d16cdSGavin Shan  * @pe: EEH PE
1455212d16cdSGavin Shan  * @option: reset type
1456b616230eSKai Song  * @include_passed: include passed-through devices?
1457212d16cdSGavin Shan  *
1458212d16cdSGavin Shan  * The routine is called to reset the specified PE with the
1459212d16cdSGavin Shan  * indicated type, either fundamental reset or hot reset.
1460212d16cdSGavin Shan  * PE reset is the most important part for error recovery.
1461212d16cdSGavin Shan  */
eeh_pe_reset(struct eeh_pe * pe,int option,bool include_passed)14621ef52073SSam Bobroff int eeh_pe_reset(struct eeh_pe *pe, int option, bool include_passed)
1463212d16cdSGavin Shan {
1464212d16cdSGavin Shan 	int ret = 0;
1465212d16cdSGavin Shan 
1466212d16cdSGavin Shan 	/* Invalid PE ? */
1467212d16cdSGavin Shan 	if (!pe)
1468212d16cdSGavin Shan 		return -ENODEV;
1469212d16cdSGavin Shan 
1470212d16cdSGavin Shan 	if (!eeh_ops || !eeh_ops->set_option || !eeh_ops->reset)
1471212d16cdSGavin Shan 		return -ENOENT;
1472212d16cdSGavin Shan 
1473212d16cdSGavin Shan 	switch (option) {
1474212d16cdSGavin Shan 	case EEH_RESET_DEACTIVATE:
1475212d16cdSGavin Shan 		ret = eeh_ops->reset(pe, option);
14761ef52073SSam Bobroff 		eeh_pe_state_clear(pe, EEH_PE_CFG_BLOCKED, include_passed);
1477212d16cdSGavin Shan 		if (ret)
1478212d16cdSGavin Shan 			break;
1479212d16cdSGavin Shan 
14801ef52073SSam Bobroff 		ret = eeh_pe_reenable_devices(pe, include_passed);
1481212d16cdSGavin Shan 		break;
1482212d16cdSGavin Shan 	case EEH_RESET_HOT:
1483212d16cdSGavin Shan 	case EEH_RESET_FUNDAMENTAL:
14840d5ee520SGavin Shan 		/*
14850d5ee520SGavin Shan 		 * Proactively freeze the PE to drop all MMIO access
14860d5ee520SGavin Shan 		 * during reset, which should be banned as it's always
14870d5ee520SGavin Shan 		 * cause recursive EEH error.
14880d5ee520SGavin Shan 		 */
14890d5ee520SGavin Shan 		eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);
14900d5ee520SGavin Shan 
14918a6b3710SGavin Shan 		eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);
1492212d16cdSGavin Shan 		ret = eeh_ops->reset(pe, option);
1493212d16cdSGavin Shan 		break;
1494212d16cdSGavin Shan 	default:
1495212d16cdSGavin Shan 		pr_debug("%s: Unsupported option %d\n",
1496212d16cdSGavin Shan 			__func__, option);
1497212d16cdSGavin Shan 		ret = -EINVAL;
1498212d16cdSGavin Shan 	}
1499212d16cdSGavin Shan 
1500212d16cdSGavin Shan 	return ret;
1501212d16cdSGavin Shan }
1502212d16cdSGavin Shan EXPORT_SYMBOL_GPL(eeh_pe_reset);
1503212d16cdSGavin Shan 
1504212d16cdSGavin Shan /**
1505212d16cdSGavin Shan  * eeh_pe_configure - Configure PCI bridges after PE reset
1506212d16cdSGavin Shan  * @pe: EEH PE
1507212d16cdSGavin Shan  *
1508212d16cdSGavin Shan  * The routine is called to restore the PCI config space for
1509212d16cdSGavin Shan  * those PCI devices, especially PCI bridges affected by PE
1510212d16cdSGavin Shan  * reset issued previously.
1511212d16cdSGavin Shan  */
eeh_pe_configure(struct eeh_pe * pe)1512212d16cdSGavin Shan int eeh_pe_configure(struct eeh_pe *pe)
1513212d16cdSGavin Shan {
1514212d16cdSGavin Shan 	int ret = 0;
1515212d16cdSGavin Shan 
1516212d16cdSGavin Shan 	/* Invalid PE ? */
1517212d16cdSGavin Shan 	if (!pe)
1518212d16cdSGavin Shan 		return -ENODEV;
1519212d16cdSGavin Shan 
1520212d16cdSGavin Shan 	return ret;
1521212d16cdSGavin Shan }
1522212d16cdSGavin Shan EXPORT_SYMBOL_GPL(eeh_pe_configure);
1523212d16cdSGavin Shan 
1524ec33d36eSGavin Shan /**
1525ec33d36eSGavin Shan  * eeh_pe_inject_err - Injecting the specified PCI error to the indicated PE
1526ec33d36eSGavin Shan  * @pe: the indicated PE
1527ec33d36eSGavin Shan  * @type: error type
1528b616230eSKai Song  * @func: error function
1529ec33d36eSGavin Shan  * @addr: address
1530ec33d36eSGavin Shan  * @mask: address mask
1531ec33d36eSGavin Shan  *
1532ec33d36eSGavin Shan  * The routine is called to inject the specified PCI error, which
1533b616230eSKai Song  * is determined by @type and @func, to the indicated PE for
1534ec33d36eSGavin Shan  * testing purpose.
1535ec33d36eSGavin Shan  */
eeh_pe_inject_err(struct eeh_pe * pe,int type,int func,unsigned long addr,unsigned long mask)1536ec33d36eSGavin Shan int eeh_pe_inject_err(struct eeh_pe *pe, int type, int func,
1537ec33d36eSGavin Shan 		      unsigned long addr, unsigned long mask)
1538ec33d36eSGavin Shan {
1539ec33d36eSGavin Shan 	/* Invalid PE ? */
1540ec33d36eSGavin Shan 	if (!pe)
1541ec33d36eSGavin Shan 		return -ENODEV;
1542ec33d36eSGavin Shan 
1543ec33d36eSGavin Shan 	/* Unsupported operation ? */
1544ec33d36eSGavin Shan 	if (!eeh_ops || !eeh_ops->err_inject)
1545ec33d36eSGavin Shan 		return -ENOENT;
1546ec33d36eSGavin Shan 
1547ec33d36eSGavin Shan 	/* Check on PCI error type */
1548ec33d36eSGavin Shan 	if (type != EEH_ERR_TYPE_32 && type != EEH_ERR_TYPE_64)
1549ec33d36eSGavin Shan 		return -EINVAL;
1550ec33d36eSGavin Shan 
1551ec33d36eSGavin Shan 	/* Check on PCI error function */
1552ec33d36eSGavin Shan 	if (func < EEH_ERR_FUNC_MIN || func > EEH_ERR_FUNC_MAX)
1553ec33d36eSGavin Shan 		return -EINVAL;
1554ec33d36eSGavin Shan 
1555ec33d36eSGavin Shan 	return eeh_ops->err_inject(pe, type, func, addr, mask);
1556ec33d36eSGavin Shan }
1557ec33d36eSGavin Shan EXPORT_SYMBOL_GPL(eeh_pe_inject_err);
1558ec33d36eSGavin Shan 
15597a7685acSMichael Ellerman #ifdef CONFIG_PROC_FS
proc_eeh_show(struct seq_file * m,void * v)1560317f06deSGavin Shan static int proc_eeh_show(struct seq_file *m, void *v)
1561317f06deSGavin Shan {
15622ec5a0adSGavin Shan 	if (!eeh_enabled()) {
1563317f06deSGavin Shan 		seq_printf(m, "EEH Subsystem is globally disabled\n");
1564317f06deSGavin Shan 		seq_printf(m, "eeh_total_mmio_ffs=%llu\n", eeh_stats.total_mmio_ffs);
1565317f06deSGavin Shan 	} else {
1566317f06deSGavin Shan 		seq_printf(m, "EEH Subsystem is enabled\n");
1567317f06deSGavin Shan 		seq_printf(m,
1568317f06deSGavin Shan 				"no device=%llu\n"
1569317f06deSGavin Shan 				"no device node=%llu\n"
1570317f06deSGavin Shan 				"no config address=%llu\n"
1571317f06deSGavin Shan 				"check not wanted=%llu\n"
1572317f06deSGavin Shan 				"eeh_total_mmio_ffs=%llu\n"
1573317f06deSGavin Shan 				"eeh_false_positives=%llu\n"
1574317f06deSGavin Shan 				"eeh_slot_resets=%llu\n",
1575317f06deSGavin Shan 				eeh_stats.no_device,
1576317f06deSGavin Shan 				eeh_stats.no_dn,
1577317f06deSGavin Shan 				eeh_stats.no_cfg_addr,
1578317f06deSGavin Shan 				eeh_stats.ignored_check,
1579317f06deSGavin Shan 				eeh_stats.total_mmio_ffs,
1580317f06deSGavin Shan 				eeh_stats.false_positives,
1581317f06deSGavin Shan 				eeh_stats.slot_resets);
1582317f06deSGavin Shan 	}
1583317f06deSGavin Shan 
1584317f06deSGavin Shan 	return 0;
1585317f06deSGavin Shan }
15867a7685acSMichael Ellerman #endif /* CONFIG_PROC_FS */
1587317f06deSGavin Shan 
15887f52a526SGavin Shan #ifdef CONFIG_DEBUG_FS
1589b5e904b8SOliver O'Halloran 
1590b5e904b8SOliver O'Halloran 
eeh_debug_lookup_pdev(struct file * filp,const char __user * user_buf,size_t count,loff_t * ppos)1591b5e904b8SOliver O'Halloran static struct pci_dev *eeh_debug_lookup_pdev(struct file *filp,
1592b5e904b8SOliver O'Halloran 					     const char __user *user_buf,
1593b5e904b8SOliver O'Halloran 					     size_t count, loff_t *ppos)
1594b5e904b8SOliver O'Halloran {
1595b5e904b8SOliver O'Halloran 	uint32_t domain, bus, dev, fn;
1596b5e904b8SOliver O'Halloran 	struct pci_dev *pdev;
1597b5e904b8SOliver O'Halloran 	char buf[20];
1598b5e904b8SOliver O'Halloran 	int ret;
1599b5e904b8SOliver O'Halloran 
1600b5e904b8SOliver O'Halloran 	memset(buf, 0, sizeof(buf));
1601b5e904b8SOliver O'Halloran 	ret = simple_write_to_buffer(buf, sizeof(buf)-1, ppos, user_buf, count);
1602b5e904b8SOliver O'Halloran 	if (!ret)
1603b5e904b8SOliver O'Halloran 		return ERR_PTR(-EFAULT);
1604b5e904b8SOliver O'Halloran 
1605b5e904b8SOliver O'Halloran 	ret = sscanf(buf, "%x:%x:%x.%x", &domain, &bus, &dev, &fn);
1606b5e904b8SOliver O'Halloran 	if (ret != 4) {
1607b5e904b8SOliver O'Halloran 		pr_err("%s: expected 4 args, got %d\n", __func__, ret);
1608b5e904b8SOliver O'Halloran 		return ERR_PTR(-EINVAL);
1609b5e904b8SOliver O'Halloran 	}
1610b5e904b8SOliver O'Halloran 
1611b5e904b8SOliver O'Halloran 	pdev = pci_get_domain_bus_and_slot(domain, bus, (dev << 3) | fn);
1612b5e904b8SOliver O'Halloran 	if (!pdev)
1613b5e904b8SOliver O'Halloran 		return ERR_PTR(-ENODEV);
1614b5e904b8SOliver O'Halloran 
1615b5e904b8SOliver O'Halloran 	return pdev;
1616b5e904b8SOliver O'Halloran }
1617b5e904b8SOliver O'Halloran 
eeh_enable_dbgfs_set(void * data,u64 val)16187f52a526SGavin Shan static int eeh_enable_dbgfs_set(void *data, u64 val)
16197f52a526SGavin Shan {
16207f52a526SGavin Shan 	if (val)
162105b1721dSGavin Shan 		eeh_clear_flag(EEH_FORCE_DISABLED);
16227f52a526SGavin Shan 	else
162305b1721dSGavin Shan 		eeh_add_flag(EEH_FORCE_DISABLED);
16247f52a526SGavin Shan 
16257f52a526SGavin Shan 	return 0;
16267f52a526SGavin Shan }
16277f52a526SGavin Shan 
eeh_enable_dbgfs_get(void * data,u64 * val)16287f52a526SGavin Shan static int eeh_enable_dbgfs_get(void *data, u64 *val)
16297f52a526SGavin Shan {
16307f52a526SGavin Shan 	if (eeh_enabled())
16317f52a526SGavin Shan 		*val = 0x1ul;
16327f52a526SGavin Shan 	else
16337f52a526SGavin Shan 		*val = 0x0ul;
16347f52a526SGavin Shan 	return 0;
16357f52a526SGavin Shan }
16367f52a526SGavin Shan 
16378c6c942dSYueHaibing DEFINE_DEBUGFS_ATTRIBUTE(eeh_enable_dbgfs_ops, eeh_enable_dbgfs_get,
16387f52a526SGavin Shan 			 eeh_enable_dbgfs_set, "0x%llx\n");
1639954bd994SOliver O'Halloran 
eeh_force_recover_write(struct file * filp,const char __user * user_buf,size_t count,loff_t * ppos)1640954bd994SOliver O'Halloran static ssize_t eeh_force_recover_write(struct file *filp,
1641954bd994SOliver O'Halloran 				const char __user *user_buf,
1642954bd994SOliver O'Halloran 				size_t count, loff_t *ppos)
1643954bd994SOliver O'Halloran {
1644954bd994SOliver O'Halloran 	struct pci_controller *hose;
1645954bd994SOliver O'Halloran 	uint32_t phbid, pe_no;
1646954bd994SOliver O'Halloran 	struct eeh_pe *pe;
1647954bd994SOliver O'Halloran 	char buf[20];
1648954bd994SOliver O'Halloran 	int ret;
1649954bd994SOliver O'Halloran 
1650954bd994SOliver O'Halloran 	ret = simple_write_to_buffer(buf, sizeof(buf), ppos, user_buf, count);
1651954bd994SOliver O'Halloran 	if (!ret)
1652954bd994SOliver O'Halloran 		return -EFAULT;
1653954bd994SOliver O'Halloran 
1654954bd994SOliver O'Halloran 	/*
1655954bd994SOliver O'Halloran 	 * When PE is NULL the event is a "special" event. Rather than
1656954bd994SOliver O'Halloran 	 * recovering a specific PE it forces the EEH core to scan for failed
1657954bd994SOliver O'Halloran 	 * PHBs and recovers each. This needs to be done before any device
1658954bd994SOliver O'Halloran 	 * recoveries can occur.
1659954bd994SOliver O'Halloran 	 */
1660954bd994SOliver O'Halloran 	if (!strncmp(buf, "hwcheck", 7)) {
1661954bd994SOliver O'Halloran 		__eeh_send_failure_event(NULL);
1662954bd994SOliver O'Halloran 		return count;
1663954bd994SOliver O'Halloran 	}
1664954bd994SOliver O'Halloran 
1665954bd994SOliver O'Halloran 	ret = sscanf(buf, "%x:%x", &phbid, &pe_no);
1666954bd994SOliver O'Halloran 	if (ret != 2)
1667954bd994SOliver O'Halloran 		return -EINVAL;
1668954bd994SOliver O'Halloran 
1669954bd994SOliver O'Halloran 	hose = pci_find_controller_for_domain(phbid);
1670954bd994SOliver O'Halloran 	if (!hose)
1671954bd994SOliver O'Halloran 		return -ENODEV;
1672954bd994SOliver O'Halloran 
1673954bd994SOliver O'Halloran 	/* Retrieve PE */
167435d64734SOliver O'Halloran 	pe = eeh_pe_get(hose, pe_no);
1675954bd994SOliver O'Halloran 	if (!pe)
1676954bd994SOliver O'Halloran 		return -ENODEV;
1677954bd994SOliver O'Halloran 
1678954bd994SOliver O'Halloran 	/*
1679954bd994SOliver O'Halloran 	 * We don't do any state checking here since the detection
1680954bd994SOliver O'Halloran 	 * process is async to the recovery process. The recovery
1681954bd994SOliver O'Halloran 	 * thread *should* not break even if we schedule a recovery
1682954bd994SOliver O'Halloran 	 * from an odd state (e.g. PE removed, or recovery of a
1683954bd994SOliver O'Halloran 	 * non-isolated PE)
1684954bd994SOliver O'Halloran 	 */
1685954bd994SOliver O'Halloran 	__eeh_send_failure_event(pe);
1686954bd994SOliver O'Halloran 
1687954bd994SOliver O'Halloran 	return ret < 0 ? ret : count;
1688954bd994SOliver O'Halloran }
1689954bd994SOliver O'Halloran 
1690954bd994SOliver O'Halloran static const struct file_operations eeh_force_recover_fops = {
1691954bd994SOliver O'Halloran 	.open	= simple_open,
1692954bd994SOliver O'Halloran 	.llseek	= no_llseek,
1693954bd994SOliver O'Halloran 	.write	= eeh_force_recover_write,
1694954bd994SOliver O'Halloran };
169522cda7c1SOliver O'Halloran 
eeh_debugfs_dev_usage(struct file * filp,char __user * user_buf,size_t count,loff_t * ppos)169622cda7c1SOliver O'Halloran static ssize_t eeh_debugfs_dev_usage(struct file *filp,
169722cda7c1SOliver O'Halloran 				char __user *user_buf,
169822cda7c1SOliver O'Halloran 				size_t count, loff_t *ppos)
169922cda7c1SOliver O'Halloran {
170022cda7c1SOliver O'Halloran 	static const char usage[] = "input format: <domain>:<bus>:<dev>.<fn>\n";
170122cda7c1SOliver O'Halloran 
170222cda7c1SOliver O'Halloran 	return simple_read_from_buffer(user_buf, count, ppos,
170322cda7c1SOliver O'Halloran 				       usage, sizeof(usage) - 1);
170422cda7c1SOliver O'Halloran }
170522cda7c1SOliver O'Halloran 
eeh_dev_check_write(struct file * filp,const char __user * user_buf,size_t count,loff_t * ppos)170622cda7c1SOliver O'Halloran static ssize_t eeh_dev_check_write(struct file *filp,
170722cda7c1SOliver O'Halloran 				const char __user *user_buf,
170822cda7c1SOliver O'Halloran 				size_t count, loff_t *ppos)
170922cda7c1SOliver O'Halloran {
171022cda7c1SOliver O'Halloran 	struct pci_dev *pdev;
171122cda7c1SOliver O'Halloran 	struct eeh_dev *edev;
171222cda7c1SOliver O'Halloran 	int ret;
171322cda7c1SOliver O'Halloran 
1714b5e904b8SOliver O'Halloran 	pdev = eeh_debug_lookup_pdev(filp, user_buf, count, ppos);
1715b5e904b8SOliver O'Halloran 	if (IS_ERR(pdev))
1716b5e904b8SOliver O'Halloran 		return PTR_ERR(pdev);
171722cda7c1SOliver O'Halloran 
171822cda7c1SOliver O'Halloran 	edev = pci_dev_to_eeh_dev(pdev);
171922cda7c1SOliver O'Halloran 	if (!edev) {
172022cda7c1SOliver O'Halloran 		pci_err(pdev, "No eeh_dev for this device!\n");
172122cda7c1SOliver O'Halloran 		pci_dev_put(pdev);
172222cda7c1SOliver O'Halloran 		return -ENODEV;
172322cda7c1SOliver O'Halloran 	}
172422cda7c1SOliver O'Halloran 
172522cda7c1SOliver O'Halloran 	ret = eeh_dev_check_failure(edev);
1726b5e904b8SOliver O'Halloran 	pci_info(pdev, "eeh_dev_check_failure(%s) = %d\n",
1727b5e904b8SOliver O'Halloran 			pci_name(pdev), ret);
172822cda7c1SOliver O'Halloran 
172922cda7c1SOliver O'Halloran 	pci_dev_put(pdev);
173022cda7c1SOliver O'Halloran 
173122cda7c1SOliver O'Halloran 	return count;
173222cda7c1SOliver O'Halloran }
173322cda7c1SOliver O'Halloran 
173422cda7c1SOliver O'Halloran static const struct file_operations eeh_dev_check_fops = {
173522cda7c1SOliver O'Halloran 	.open	= simple_open,
173622cda7c1SOliver O'Halloran 	.llseek	= no_llseek,
173722cda7c1SOliver O'Halloran 	.write	= eeh_dev_check_write,
173822cda7c1SOliver O'Halloran 	.read   = eeh_debugfs_dev_usage,
173922cda7c1SOliver O'Halloran };
174022cda7c1SOliver O'Halloran 
eeh_debugfs_break_device(struct pci_dev * pdev)1741bd6461ccSOliver O'Halloran static int eeh_debugfs_break_device(struct pci_dev *pdev)
1742bd6461ccSOliver O'Halloran {
1743bd6461ccSOliver O'Halloran 	struct resource *bar = NULL;
1744bd6461ccSOliver O'Halloran 	void __iomem *mapped;
1745bd6461ccSOliver O'Halloran 	u16 old, bit;
1746bd6461ccSOliver O'Halloran 	int i, pos;
1747bd6461ccSOliver O'Halloran 
1748bd6461ccSOliver O'Halloran 	/* Do we have an MMIO BAR to disable? */
1749bd6461ccSOliver O'Halloran 	for (i = 0; i <= PCI_STD_RESOURCE_END; i++) {
1750bd6461ccSOliver O'Halloran 		struct resource *r = &pdev->resource[i];
1751bd6461ccSOliver O'Halloran 
1752bd6461ccSOliver O'Halloran 		if (!r->flags || !r->start)
1753bd6461ccSOliver O'Halloran 			continue;
1754bd6461ccSOliver O'Halloran 		if (r->flags & IORESOURCE_IO)
1755bd6461ccSOliver O'Halloran 			continue;
1756bd6461ccSOliver O'Halloran 		if (r->flags & IORESOURCE_UNSET)
1757bd6461ccSOliver O'Halloran 			continue;
1758bd6461ccSOliver O'Halloran 
1759bd6461ccSOliver O'Halloran 		bar = r;
1760bd6461ccSOliver O'Halloran 		break;
1761bd6461ccSOliver O'Halloran 	}
1762bd6461ccSOliver O'Halloran 
1763bd6461ccSOliver O'Halloran 	if (!bar) {
1764bd6461ccSOliver O'Halloran 		pci_err(pdev, "Unable to find Memory BAR to cause EEH with\n");
1765bd6461ccSOliver O'Halloran 		return -ENXIO;
1766bd6461ccSOliver O'Halloran 	}
1767bd6461ccSOliver O'Halloran 
1768bd6461ccSOliver O'Halloran 	pci_err(pdev, "Going to break: %pR\n", bar);
1769bd6461ccSOliver O'Halloran 
1770bd6461ccSOliver O'Halloran 	if (pdev->is_virtfn) {
1771253c8921SOliver O'Halloran #ifndef CONFIG_PCI_IOV
1772bd6461ccSOliver O'Halloran 		return -ENXIO;
1773bd6461ccSOliver O'Halloran #else
1774bd6461ccSOliver O'Halloran 		/*
1775bd6461ccSOliver O'Halloran 		 * VFs don't have a per-function COMMAND register, so the best
1776bd6461ccSOliver O'Halloran 		 * we can do is clear the Memory Space Enable bit in the PF's
1777bd6461ccSOliver O'Halloran 		 * SRIOV control reg.
1778bd6461ccSOliver O'Halloran 		 *
1779bd6461ccSOliver O'Halloran 		 * Unfortunately, this requires that we have a PF (i.e doesn't
1780bd6461ccSOliver O'Halloran 		 * work for a passed-through VF) and it has the potential side
1781bd6461ccSOliver O'Halloran 		 * effect of also causing an EEH on every other VF under the
1782bd6461ccSOliver O'Halloran 		 * PF. Oh well.
1783bd6461ccSOliver O'Halloran 		 */
1784bd6461ccSOliver O'Halloran 		pdev = pdev->physfn;
1785bd6461ccSOliver O'Halloran 		if (!pdev)
1786bd6461ccSOliver O'Halloran 			return -ENXIO; /* passed through VFs have no PF */
1787bd6461ccSOliver O'Halloran 
1788bd6461ccSOliver O'Halloran 		pos  = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
1789bd6461ccSOliver O'Halloran 		pos += PCI_SRIOV_CTRL;
1790bd6461ccSOliver O'Halloran 		bit  = PCI_SRIOV_CTRL_MSE;
1791253c8921SOliver O'Halloran #endif /* !CONFIG_PCI_IOV */
1792bd6461ccSOliver O'Halloran 	} else {
1793bd6461ccSOliver O'Halloran 		bit = PCI_COMMAND_MEMORY;
1794bd6461ccSOliver O'Halloran 		pos = PCI_COMMAND;
1795bd6461ccSOliver O'Halloran 	}
1796bd6461ccSOliver O'Halloran 
1797bd6461ccSOliver O'Halloran 	/*
1798bd6461ccSOliver O'Halloran 	 * Process here is:
1799bd6461ccSOliver O'Halloran 	 *
1800bd6461ccSOliver O'Halloran 	 * 1. Disable Memory space.
1801bd6461ccSOliver O'Halloran 	 *
1802bd6461ccSOliver O'Halloran 	 * 2. Perform an MMIO to the device. This should result in an error
1803bd6461ccSOliver O'Halloran 	 *    (CA  / UR) being raised by the device which results in an EEH
1804bd6461ccSOliver O'Halloran 	 *    PE freeze. Using the in_8() accessor skips the eeh detection hook
1805bd6461ccSOliver O'Halloran 	 *    so the freeze hook so the EEH Detection machinery won't be
1806bd6461ccSOliver O'Halloran 	 *    triggered here. This is to match the usual behaviour of EEH
1807*1fd02f66SJulia Lawall 	 *    where the HW will asynchronously freeze a PE and it's up to
1808bd6461ccSOliver O'Halloran 	 *    the kernel to notice and deal with it.
1809bd6461ccSOliver O'Halloran 	 *
1810bd6461ccSOliver O'Halloran 	 * 3. Turn Memory space back on. This is more important for VFs
1811bd6461ccSOliver O'Halloran 	 *    since recovery will probably fail if we don't. For normal
1812bd6461ccSOliver O'Halloran 	 *    the COMMAND register is reset as a part of re-initialising
1813bd6461ccSOliver O'Halloran 	 *    the device.
1814bd6461ccSOliver O'Halloran 	 *
1815bd6461ccSOliver O'Halloran 	 * Breaking stuff is the point so who cares if it's racy ;)
1816bd6461ccSOliver O'Halloran 	 */
1817bd6461ccSOliver O'Halloran 	pci_read_config_word(pdev, pos, &old);
1818bd6461ccSOliver O'Halloran 
1819bd6461ccSOliver O'Halloran 	mapped = ioremap(bar->start, PAGE_SIZE);
1820bd6461ccSOliver O'Halloran 	if (!mapped) {
1821bd6461ccSOliver O'Halloran 		pci_err(pdev, "Unable to map MMIO BAR %pR\n", bar);
1822bd6461ccSOliver O'Halloran 		return -ENXIO;
1823bd6461ccSOliver O'Halloran 	}
1824bd6461ccSOliver O'Halloran 
1825bd6461ccSOliver O'Halloran 	pci_write_config_word(pdev, pos, old & ~bit);
1826bd6461ccSOliver O'Halloran 	in_8(mapped);
1827bd6461ccSOliver O'Halloran 	pci_write_config_word(pdev, pos, old);
1828bd6461ccSOliver O'Halloran 
1829bd6461ccSOliver O'Halloran 	iounmap(mapped);
1830bd6461ccSOliver O'Halloran 
1831bd6461ccSOliver O'Halloran 	return 0;
1832bd6461ccSOliver O'Halloran }
1833bd6461ccSOliver O'Halloran 
eeh_dev_break_write(struct file * filp,const char __user * user_buf,size_t count,loff_t * ppos)1834bd6461ccSOliver O'Halloran static ssize_t eeh_dev_break_write(struct file *filp,
1835bd6461ccSOliver O'Halloran 				const char __user *user_buf,
1836bd6461ccSOliver O'Halloran 				size_t count, loff_t *ppos)
1837bd6461ccSOliver O'Halloran {
1838bd6461ccSOliver O'Halloran 	struct pci_dev *pdev;
1839bd6461ccSOliver O'Halloran 	int ret;
1840bd6461ccSOliver O'Halloran 
1841b5e904b8SOliver O'Halloran 	pdev = eeh_debug_lookup_pdev(filp, user_buf, count, ppos);
1842b5e904b8SOliver O'Halloran 	if (IS_ERR(pdev))
1843b5e904b8SOliver O'Halloran 		return PTR_ERR(pdev);
1844bd6461ccSOliver O'Halloran 
1845bd6461ccSOliver O'Halloran 	ret = eeh_debugfs_break_device(pdev);
1846bd6461ccSOliver O'Halloran 	pci_dev_put(pdev);
1847bd6461ccSOliver O'Halloran 
1848bd6461ccSOliver O'Halloran 	if (ret < 0)
1849bd6461ccSOliver O'Halloran 		return ret;
1850bd6461ccSOliver O'Halloran 
1851bd6461ccSOliver O'Halloran 	return count;
1852bd6461ccSOliver O'Halloran }
1853bd6461ccSOliver O'Halloran 
1854bd6461ccSOliver O'Halloran static const struct file_operations eeh_dev_break_fops = {
1855bd6461ccSOliver O'Halloran 	.open	= simple_open,
1856bd6461ccSOliver O'Halloran 	.llseek	= no_llseek,
1857bd6461ccSOliver O'Halloran 	.write	= eeh_dev_break_write,
1858bd6461ccSOliver O'Halloran 	.read   = eeh_debugfs_dev_usage,
1859bd6461ccSOliver O'Halloran };
1860bd6461ccSOliver O'Halloran 
eeh_dev_can_recover(struct file * filp,const char __user * user_buf,size_t count,loff_t * ppos)18619e857416SOliver O'Halloran static ssize_t eeh_dev_can_recover(struct file *filp,
18629e857416SOliver O'Halloran 				   const char __user *user_buf,
18639e857416SOliver O'Halloran 				   size_t count, loff_t *ppos)
18649e857416SOliver O'Halloran {
18659e857416SOliver O'Halloran 	struct pci_driver *drv;
18669e857416SOliver O'Halloran 	struct pci_dev *pdev;
18679e857416SOliver O'Halloran 	size_t ret;
18689e857416SOliver O'Halloran 
18699e857416SOliver O'Halloran 	pdev = eeh_debug_lookup_pdev(filp, user_buf, count, ppos);
18709e857416SOliver O'Halloran 	if (IS_ERR(pdev))
18719e857416SOliver O'Halloran 		return PTR_ERR(pdev);
18729e857416SOliver O'Halloran 
18739e857416SOliver O'Halloran 	/*
18749e857416SOliver O'Halloran 	 * In order for error recovery to work the driver needs to implement
18759e857416SOliver O'Halloran 	 * .error_detected(), so it can quiesce IO to the device, and
18769e857416SOliver O'Halloran 	 * .slot_reset() so it can re-initialise the device after a reset.
18779e857416SOliver O'Halloran 	 *
18789e857416SOliver O'Halloran 	 * Ideally they'd implement .resume() too, but some drivers which
18799e857416SOliver O'Halloran 	 * we need to support (notably IPR) don't so I guess we can tolerate
18809e857416SOliver O'Halloran 	 * that.
18819e857416SOliver O'Halloran 	 *
18829e857416SOliver O'Halloran 	 * .mmio_enabled() is mostly there as a work-around for devices which
18839e857416SOliver O'Halloran 	 * take forever to re-init after a hot reset. Implementing that is
18849e857416SOliver O'Halloran 	 * strictly optional.
18859e857416SOliver O'Halloran 	 */
18869e857416SOliver O'Halloran 	drv = pci_dev_driver(pdev);
18879e857416SOliver O'Halloran 	if (drv &&
18889e857416SOliver O'Halloran 	    drv->err_handler &&
18899e857416SOliver O'Halloran 	    drv->err_handler->error_detected &&
18909e857416SOliver O'Halloran 	    drv->err_handler->slot_reset) {
18919e857416SOliver O'Halloran 		ret = count;
18929e857416SOliver O'Halloran 	} else {
18939e857416SOliver O'Halloran 		ret = -EOPNOTSUPP;
18949e857416SOliver O'Halloran 	}
18959e857416SOliver O'Halloran 
18969e857416SOliver O'Halloran 	pci_dev_put(pdev);
18979e857416SOliver O'Halloran 
18989e857416SOliver O'Halloran 	return ret;
18999e857416SOliver O'Halloran }
19009e857416SOliver O'Halloran 
19019e857416SOliver O'Halloran static const struct file_operations eeh_dev_can_recover_fops = {
19029e857416SOliver O'Halloran 	.open	= simple_open,
19039e857416SOliver O'Halloran 	.llseek	= no_llseek,
19049e857416SOliver O'Halloran 	.write	= eeh_dev_can_recover,
19059e857416SOliver O'Halloran 	.read   = eeh_debugfs_dev_usage,
19069e857416SOliver O'Halloran };
19079e857416SOliver O'Halloran 
19087f52a526SGavin Shan #endif
19097f52a526SGavin Shan 
eeh_init_proc(void)1910317f06deSGavin Shan static int __init eeh_init_proc(void)
1911317f06deSGavin Shan {
19127f52a526SGavin Shan 	if (machine_is(pseries) || machine_is(powernv)) {
19133f3942acSChristoph Hellwig 		proc_create_single("powerpc/eeh", 0, NULL, proc_eeh_show);
19147f52a526SGavin Shan #ifdef CONFIG_DEBUG_FS
19158c6c942dSYueHaibing 		debugfs_create_file_unsafe("eeh_enable", 0600,
1916dbf77fedSAneesh Kumar K.V 					   arch_debugfs_dir, NULL,
19177f52a526SGavin Shan 					   &eeh_enable_dbgfs_ops);
191846ee7c3cSOliver O'Halloran 		debugfs_create_u32("eeh_max_freezes", 0600,
1919dbf77fedSAneesh Kumar K.V 				arch_debugfs_dir, &eeh_max_freezes);
19206b493f60SOliver O'Halloran 		debugfs_create_bool("eeh_disable_recovery", 0600,
1921dbf77fedSAneesh Kumar K.V 				arch_debugfs_dir,
19226b493f60SOliver O'Halloran 				&eeh_debugfs_no_recover);
192322cda7c1SOliver O'Halloran 		debugfs_create_file_unsafe("eeh_dev_check", 0600,
1924dbf77fedSAneesh Kumar K.V 				arch_debugfs_dir, NULL,
192522cda7c1SOliver O'Halloran 				&eeh_dev_check_fops);
1926bd6461ccSOliver O'Halloran 		debugfs_create_file_unsafe("eeh_dev_break", 0600,
1927dbf77fedSAneesh Kumar K.V 				arch_debugfs_dir, NULL,
1928bd6461ccSOliver O'Halloran 				&eeh_dev_break_fops);
1929954bd994SOliver O'Halloran 		debugfs_create_file_unsafe("eeh_force_recover", 0600,
1930dbf77fedSAneesh Kumar K.V 				arch_debugfs_dir, NULL,
1931954bd994SOliver O'Halloran 				&eeh_force_recover_fops);
19329e857416SOliver O'Halloran 		debugfs_create_file_unsafe("eeh_dev_can_recover", 0600,
1933dbf77fedSAneesh Kumar K.V 				arch_debugfs_dir, NULL,
19349e857416SOliver O'Halloran 				&eeh_dev_can_recover_fops);
19355ca85ae6SOliver O'Halloran 		eeh_cache_debugfs_init();
19367f52a526SGavin Shan #endif
19377f52a526SGavin Shan 	}
19387f52a526SGavin Shan 
1939317f06deSGavin Shan 	return 0;
1940317f06deSGavin Shan }
1941317f06deSGavin Shan __initcall(eeh_init_proc);
1942