12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
229310e5eSGavin Shan /*
341732bdcSStewart Smith  * PowerNV Platform dependent EEH operations
429310e5eSGavin Shan  *
529310e5eSGavin Shan  * Copyright Benjamin Herrenschmidt & Gavin Shan, IBM Corporation 2013.
629310e5eSGavin Shan  */
729310e5eSGavin Shan 
829310e5eSGavin Shan #include <linux/atomic.h>
94cf17445SGavin Shan #include <linux/debugfs.h>
1029310e5eSGavin Shan #include <linux/delay.h>
1129310e5eSGavin Shan #include <linux/export.h>
1229310e5eSGavin Shan #include <linux/init.h>
1379231448SAlistair Popple #include <linux/interrupt.h>
1429310e5eSGavin Shan #include <linux/list.h>
1529310e5eSGavin Shan #include <linux/msi.h>
1629310e5eSGavin Shan #include <linux/of.h>
1729310e5eSGavin Shan #include <linux/pci.h>
1829310e5eSGavin Shan #include <linux/proc_fs.h>
1929310e5eSGavin Shan #include <linux/rbtree.h>
2029310e5eSGavin Shan #include <linux/sched.h>
2129310e5eSGavin Shan #include <linux/seq_file.h>
2229310e5eSGavin Shan #include <linux/spinlock.h>
2329310e5eSGavin Shan 
2429310e5eSGavin Shan #include <asm/eeh.h>
2529310e5eSGavin Shan #include <asm/eeh_event.h>
2629310e5eSGavin Shan #include <asm/firmware.h>
2729310e5eSGavin Shan #include <asm/io.h>
2829310e5eSGavin Shan #include <asm/iommu.h>
2929310e5eSGavin Shan #include <asm/machdep.h>
3029310e5eSGavin Shan #include <asm/msi_bitmap.h>
3129310e5eSGavin Shan #include <asm/opal.h>
3229310e5eSGavin Shan #include <asm/ppc-pci.h>
339c0e1ecbSGavin Shan #include <asm/pnv-pci.h>
3429310e5eSGavin Shan 
3529310e5eSGavin Shan #include "powernv.h"
3629310e5eSGavin Shan #include "pci.h"
3798fd32cdSOliver O'Halloran #include "../../../../drivers/pci/pci.h"
3829310e5eSGavin Shan 
3979231448SAlistair Popple static int eeh_event_irq = -EINVAL;
404cf17445SGavin Shan 
413b70464aSOliver O'Halloran static void pnv_pcibios_bus_add_device(struct pci_dev *pdev)
42988fc3baSBryant G. Ly {
431ff8f36fSSam Bobroff 	dev_dbg(&pdev->dev, "EEH: Setting up device\n");
44e86350f7SOliver O'Halloran 	eeh_probe_device(pdev);
45988fc3baSBryant G. Ly }
46988fc3baSBryant G. Ly 
4701f3bfb7SGavin Shan static int pnv_eeh_init(void)
4829310e5eSGavin Shan {
49dc561fb9SGavin Shan 	struct pci_controller *hose;
50dc561fb9SGavin Shan 	struct pnv_phb *phb;
515cb1f8fdSRussell Currey 	int max_diag_size = PNV_PCI_DIAG_BUF_SIZE;
52dc561fb9SGavin Shan 
53e4d54f71SStewart Smith 	if (!firmware_has_feature(FW_FEATURE_OPAL)) {
54e4d54f71SStewart Smith 		pr_warn("%s: OPAL is required !\n",
550dae2743SGavin Shan 			__func__);
5629310e5eSGavin Shan 		return -EINVAL;
5729310e5eSGavin Shan 	}
5829310e5eSGavin Shan 
5905b1721dSGavin Shan 	/* Set probe mode */
6005b1721dSGavin Shan 	eeh_add_flag(EEH_PROBE_MODE_DEV);
6129310e5eSGavin Shan 
62dc561fb9SGavin Shan 	/*
63dc561fb9SGavin Shan 	 * P7IOC blocks PCI config access to frozen PE, but PHB3
64dc561fb9SGavin Shan 	 * doesn't do that. So we have to selectively enable I/O
65dc561fb9SGavin Shan 	 * prior to collecting error log.
66dc561fb9SGavin Shan 	 */
67dc561fb9SGavin Shan 	list_for_each_entry(hose, &hose_list, list_node) {
68dc561fb9SGavin Shan 		phb = hose->private_data;
69dc561fb9SGavin Shan 
70dc561fb9SGavin Shan 		if (phb->model == PNV_PHB_MODEL_P7IOC)
71dc561fb9SGavin Shan 			eeh_add_flag(EEH_ENABLE_IO_FOR_LOG);
722aa5cf9eSGavin Shan 
735cb1f8fdSRussell Currey 		if (phb->diag_data_size > max_diag_size)
745cb1f8fdSRussell Currey 			max_diag_size = phb->diag_data_size;
755cb1f8fdSRussell Currey 
762aa5cf9eSGavin Shan 		/*
772aa5cf9eSGavin Shan 		 * PE#0 should be regarded as valid by EEH core
782aa5cf9eSGavin Shan 		 * if it's not the reserved one. Currently, we
79608fb9c2SGavin Shan 		 * have the reserved PE#255 and PE#127 for PHB3
802aa5cf9eSGavin Shan 		 * and P7IOC separately. So we should regard
81608fb9c2SGavin Shan 		 * PE#0 as valid for PHB3 and P7IOC.
822aa5cf9eSGavin Shan 		 */
8392b8f137SGavin Shan 		if (phb->ioda.reserved_pe_idx != 0)
842aa5cf9eSGavin Shan 			eeh_add_flag(EEH_VALID_PE_ZERO);
852aa5cf9eSGavin Shan 
86dc561fb9SGavin Shan 		break;
87dc561fb9SGavin Shan 	}
88dc561fb9SGavin Shan 
895cb1f8fdSRussell Currey 	eeh_set_pe_aux_size(max_diag_size);
90988fc3baSBryant G. Ly 	ppc_md.pcibios_bus_add_device = pnv_pcibios_bus_add_device;
915cb1f8fdSRussell Currey 
9229310e5eSGavin Shan 	return 0;
9329310e5eSGavin Shan }
9429310e5eSGavin Shan 
9579231448SAlistair Popple static irqreturn_t pnv_eeh_event(int irq, void *data)
964cf17445SGavin Shan {
974cf17445SGavin Shan 	/*
9879231448SAlistair Popple 	 * We simply send a special EEH event if EEH has been
9979231448SAlistair Popple 	 * enabled. We don't care about EEH events until we've
10079231448SAlistair Popple 	 * finished processing the outstanding ones. Event processing
10179231448SAlistair Popple 	 * gets unmasked in next_error() if EEH is enabled.
1024cf17445SGavin Shan 	 */
10379231448SAlistair Popple 	disable_irq_nosync(irq);
1044cf17445SGavin Shan 
1054cf17445SGavin Shan 	if (eeh_enabled())
1064cf17445SGavin Shan 		eeh_send_failure_event(NULL);
1074cf17445SGavin Shan 
10879231448SAlistair Popple 	return IRQ_HANDLED;
1094cf17445SGavin Shan }
1104cf17445SGavin Shan 
1114cf17445SGavin Shan #ifdef CONFIG_DEBUG_FS
1124cf17445SGavin Shan static ssize_t pnv_eeh_ei_write(struct file *filp,
1134cf17445SGavin Shan 				const char __user *user_buf,
1144cf17445SGavin Shan 				size_t count, loff_t *ppos)
1154cf17445SGavin Shan {
1164cf17445SGavin Shan 	struct pci_controller *hose = filp->private_data;
1174cf17445SGavin Shan 	struct eeh_pe *pe;
1184cf17445SGavin Shan 	int pe_no, type, func;
1194cf17445SGavin Shan 	unsigned long addr, mask;
1204cf17445SGavin Shan 	char buf[50];
1214cf17445SGavin Shan 	int ret;
1224cf17445SGavin Shan 
1234cf17445SGavin Shan 	if (!eeh_ops || !eeh_ops->err_inject)
1244cf17445SGavin Shan 		return -ENXIO;
1254cf17445SGavin Shan 
1264cf17445SGavin Shan 	/* Copy over argument buffer */
1274cf17445SGavin Shan 	ret = simple_write_to_buffer(buf, sizeof(buf), ppos, user_buf, count);
1284cf17445SGavin Shan 	if (!ret)
1294cf17445SGavin Shan 		return -EFAULT;
1304cf17445SGavin Shan 
1314cf17445SGavin Shan 	/* Retrieve parameters */
1324cf17445SGavin Shan 	ret = sscanf(buf, "%x:%x:%x:%lx:%lx",
1334cf17445SGavin Shan 		     &pe_no, &type, &func, &addr, &mask);
1344cf17445SGavin Shan 	if (ret != 5)
1354cf17445SGavin Shan 		return -EINVAL;
1364cf17445SGavin Shan 
1374cf17445SGavin Shan 	/* Retrieve PE */
1388bae6a23SAlexey Kardashevskiy 	pe = eeh_pe_get(hose, pe_no, 0);
1394cf17445SGavin Shan 	if (!pe)
1404cf17445SGavin Shan 		return -ENODEV;
1414cf17445SGavin Shan 
1424cf17445SGavin Shan 	/* Do error injection */
1434cf17445SGavin Shan 	ret = eeh_ops->err_inject(pe, type, func, addr, mask);
1444cf17445SGavin Shan 	return ret < 0 ? ret : count;
1454cf17445SGavin Shan }
1464cf17445SGavin Shan 
1474cf17445SGavin Shan static const struct file_operations pnv_eeh_ei_fops = {
1484cf17445SGavin Shan 	.open	= simple_open,
1494cf17445SGavin Shan 	.llseek	= no_llseek,
1504cf17445SGavin Shan 	.write	= pnv_eeh_ei_write,
1514cf17445SGavin Shan };
1524cf17445SGavin Shan 
1534cf17445SGavin Shan static int pnv_eeh_dbgfs_set(void *data, int offset, u64 val)
1544cf17445SGavin Shan {
1554cf17445SGavin Shan 	struct pci_controller *hose = data;
1564cf17445SGavin Shan 	struct pnv_phb *phb = hose->private_data;
1574cf17445SGavin Shan 
1584cf17445SGavin Shan 	out_be64(phb->regs + offset, val);
1594cf17445SGavin Shan 	return 0;
1604cf17445SGavin Shan }
1614cf17445SGavin Shan 
1624cf17445SGavin Shan static int pnv_eeh_dbgfs_get(void *data, int offset, u64 *val)
1634cf17445SGavin Shan {
1644cf17445SGavin Shan 	struct pci_controller *hose = data;
1654cf17445SGavin Shan 	struct pnv_phb *phb = hose->private_data;
1664cf17445SGavin Shan 
1674cf17445SGavin Shan 	*val = in_be64(phb->regs + offset);
1684cf17445SGavin Shan 	return 0;
1694cf17445SGavin Shan }
1704cf17445SGavin Shan 
171ccc9662dSGavin Shan #define PNV_EEH_DBGFS_ENTRY(name, reg)				\
172ccc9662dSGavin Shan static int pnv_eeh_dbgfs_set_##name(void *data, u64 val)	\
173ccc9662dSGavin Shan {								\
174ccc9662dSGavin Shan 	return pnv_eeh_dbgfs_set(data, reg, val);		\
175ccc9662dSGavin Shan }								\
176ccc9662dSGavin Shan 								\
177ccc9662dSGavin Shan static int pnv_eeh_dbgfs_get_##name(void *data, u64 *val)	\
178ccc9662dSGavin Shan {								\
179ccc9662dSGavin Shan 	return pnv_eeh_dbgfs_get(data, reg, val);		\
180ccc9662dSGavin Shan }								\
181ccc9662dSGavin Shan 								\
182ccc9662dSGavin Shan DEFINE_SIMPLE_ATTRIBUTE(pnv_eeh_dbgfs_ops_##name,		\
183ccc9662dSGavin Shan 			pnv_eeh_dbgfs_get_##name,		\
184ccc9662dSGavin Shan                         pnv_eeh_dbgfs_set_##name,		\
185ccc9662dSGavin Shan 			"0x%llx\n")
1864cf17445SGavin Shan 
187ccc9662dSGavin Shan PNV_EEH_DBGFS_ENTRY(outb, 0xD10);
188ccc9662dSGavin Shan PNV_EEH_DBGFS_ENTRY(inbA, 0xD90);
189ccc9662dSGavin Shan PNV_EEH_DBGFS_ENTRY(inbB, 0xE10);
1904cf17445SGavin Shan 
1914cf17445SGavin Shan #endif /* CONFIG_DEBUG_FS */
1924cf17445SGavin Shan 
1933b70464aSOliver O'Halloran static void pnv_eeh_enable_phbs(void)
194b905f8cdSSam Bobroff {
195b905f8cdSSam Bobroff 	struct pci_controller *hose;
196b905f8cdSSam Bobroff 	struct pnv_phb *phb;
197b905f8cdSSam Bobroff 
198b905f8cdSSam Bobroff 	list_for_each_entry(hose, &hose_list, list_node) {
199b905f8cdSSam Bobroff 		phb = hose->private_data;
200b905f8cdSSam Bobroff 		/*
201b905f8cdSSam Bobroff 		 * If EEH is enabled, we're going to rely on that.
202b905f8cdSSam Bobroff 		 * Otherwise, we restore to conventional mechanism
203b905f8cdSSam Bobroff 		 * to clear frozen PE during PCI config access.
204b905f8cdSSam Bobroff 		 */
205b905f8cdSSam Bobroff 		if (eeh_enabled())
206b905f8cdSSam Bobroff 			phb->flags |= PNV_PHB_FLAG_EEH;
207b905f8cdSSam Bobroff 		else
208b905f8cdSSam Bobroff 			phb->flags &= ~PNV_PHB_FLAG_EEH;
209b905f8cdSSam Bobroff 	}
210b905f8cdSSam Bobroff }
211b905f8cdSSam Bobroff 
21229310e5eSGavin Shan /**
21301f3bfb7SGavin Shan  * pnv_eeh_post_init - EEH platform dependent post initialization
21429310e5eSGavin Shan  *
21529310e5eSGavin Shan  * EEH platform dependent post initialization on powernv. When
21629310e5eSGavin Shan  * the function is called, the EEH PEs and devices should have
21729310e5eSGavin Shan  * been built. If the I/O cache staff has been built, EEH is
21829310e5eSGavin Shan  * ready to supply service.
21929310e5eSGavin Shan  */
220b9fde58dSBenjamin Herrenschmidt int pnv_eeh_post_init(void)
22129310e5eSGavin Shan {
22229310e5eSGavin Shan 	struct pci_controller *hose;
22329310e5eSGavin Shan 	struct pnv_phb *phb;
22429310e5eSGavin Shan 	int ret = 0;
22529310e5eSGavin Shan 
226c44e4ccaSSam Bobroff 	eeh_show_enabled();
227b9fde58dSBenjamin Herrenschmidt 
2284cf17445SGavin Shan 	/* Register OPAL event notifier */
22979231448SAlistair Popple 	eeh_event_irq = opal_event_request(ilog2(OPAL_EVENT_PCI_ERROR));
23079231448SAlistair Popple 	if (eeh_event_irq < 0) {
23179231448SAlistair Popple 		pr_err("%s: Can't register OPAL event interrupt (%d)\n",
23279231448SAlistair Popple 		       __func__, eeh_event_irq);
23379231448SAlistair Popple 		return eeh_event_irq;
23479231448SAlistair Popple 	}
23579231448SAlistair Popple 
23679231448SAlistair Popple 	ret = request_irq(eeh_event_irq, pnv_eeh_event,
23779231448SAlistair Popple 			  IRQ_TYPE_LEVEL_HIGH, "opal-eeh", NULL);
23879231448SAlistair Popple 	if (ret < 0) {
23979231448SAlistair Popple 		irq_dispose_mapping(eeh_event_irq);
24079231448SAlistair Popple 		pr_err("%s: Can't request OPAL event interrupt (%d)\n",
24179231448SAlistair Popple 		       __func__, eeh_event_irq);
2424cf17445SGavin Shan 		return ret;
2434cf17445SGavin Shan 	}
2444cf17445SGavin Shan 
24579231448SAlistair Popple 	if (!eeh_enabled())
24679231448SAlistair Popple 		disable_irq(eeh_event_irq);
24779231448SAlistair Popple 
248b905f8cdSSam Bobroff 	pnv_eeh_enable_phbs();
249b905f8cdSSam Bobroff 
25029310e5eSGavin Shan 	list_for_each_entry(hose, &hose_list, list_node) {
25129310e5eSGavin Shan 		phb = hose->private_data;
25229310e5eSGavin Shan 
2534cf17445SGavin Shan 		/* Create debugfs entries */
2544cf17445SGavin Shan #ifdef CONFIG_DEBUG_FS
2554cf17445SGavin Shan 		if (phb->has_dbgfs || !phb->dbgfs)
2564cf17445SGavin Shan 			continue;
2574cf17445SGavin Shan 
2584cf17445SGavin Shan 		phb->has_dbgfs = 1;
2594cf17445SGavin Shan 		debugfs_create_file("err_injct", 0200,
2604cf17445SGavin Shan 				    phb->dbgfs, hose,
2614cf17445SGavin Shan 				    &pnv_eeh_ei_fops);
2624cf17445SGavin Shan 
2634cf17445SGavin Shan 		debugfs_create_file("err_injct_outbound", 0600,
2644cf17445SGavin Shan 				    phb->dbgfs, hose,
265ccc9662dSGavin Shan 				    &pnv_eeh_dbgfs_ops_outb);
2664cf17445SGavin Shan 		debugfs_create_file("err_injct_inboundA", 0600,
2674cf17445SGavin Shan 				    phb->dbgfs, hose,
268ccc9662dSGavin Shan 				    &pnv_eeh_dbgfs_ops_inbA);
2694cf17445SGavin Shan 		debugfs_create_file("err_injct_inboundB", 0600,
2704cf17445SGavin Shan 				    phb->dbgfs, hose,
271ccc9662dSGavin Shan 				    &pnv_eeh_dbgfs_ops_inbB);
2724cf17445SGavin Shan #endif /* CONFIG_DEBUG_FS */
27329310e5eSGavin Shan 	}
2744cf17445SGavin Shan 
27529310e5eSGavin Shan 	return ret;
27629310e5eSGavin Shan }
27729310e5eSGavin Shan 
2784d6186caSGavin Shan static int pnv_eeh_find_cap(struct pci_dn *pdn, int cap)
279ff57b454SGavin Shan {
2804d6186caSGavin Shan 	int pos = PCI_CAPABILITY_LIST;
2814d6186caSGavin Shan 	int cnt = 48;   /* Maximal number of capabilities */
2824d6186caSGavin Shan 	u32 status, id;
283ff57b454SGavin Shan 
284ff57b454SGavin Shan 	if (!pdn)
285ff57b454SGavin Shan 		return 0;
286ff57b454SGavin Shan 
2874d6186caSGavin Shan 	/* Check if the device supports capabilities */
288ff57b454SGavin Shan 	pnv_pci_cfg_read(pdn, PCI_STATUS, 2, &status);
289ff57b454SGavin Shan 	if (!(status & PCI_STATUS_CAP_LIST))
290ff57b454SGavin Shan 		return 0;
291ff57b454SGavin Shan 
292ff57b454SGavin Shan 	while (cnt--) {
293ff57b454SGavin Shan 		pnv_pci_cfg_read(pdn, pos, 1, &pos);
294ff57b454SGavin Shan 		if (pos < 0x40)
295ff57b454SGavin Shan 			break;
296ff57b454SGavin Shan 
297ff57b454SGavin Shan 		pos &= ~3;
298ff57b454SGavin Shan 		pnv_pci_cfg_read(pdn, pos + PCI_CAP_LIST_ID, 1, &id);
299ff57b454SGavin Shan 		if (id == 0xff)
300ff57b454SGavin Shan 			break;
301ff57b454SGavin Shan 
302ff57b454SGavin Shan 		/* Found */
303ff57b454SGavin Shan 		if (id == cap)
304ff57b454SGavin Shan 			return pos;
305ff57b454SGavin Shan 
306ff57b454SGavin Shan 		/* Next one */
307ff57b454SGavin Shan 		pos += PCI_CAP_LIST_NEXT;
308ff57b454SGavin Shan 	}
309ff57b454SGavin Shan 
310ff57b454SGavin Shan 	return 0;
311ff57b454SGavin Shan }
312ff57b454SGavin Shan 
313ff57b454SGavin Shan static int pnv_eeh_find_ecap(struct pci_dn *pdn, int cap)
314ff57b454SGavin Shan {
315ff57b454SGavin Shan 	struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
316ff57b454SGavin Shan 	u32 header;
317ff57b454SGavin Shan 	int pos = 256, ttl = (4096 - 256) / 8;
318ff57b454SGavin Shan 
319ff57b454SGavin Shan 	if (!edev || !edev->pcie_cap)
320ff57b454SGavin Shan 		return 0;
321ff57b454SGavin Shan 	if (pnv_pci_cfg_read(pdn, pos, 4, &header) != PCIBIOS_SUCCESSFUL)
322ff57b454SGavin Shan 		return 0;
323ff57b454SGavin Shan 	else if (!header)
324ff57b454SGavin Shan 		return 0;
325ff57b454SGavin Shan 
326ff57b454SGavin Shan 	while (ttl-- > 0) {
327ff57b454SGavin Shan 		if (PCI_EXT_CAP_ID(header) == cap && pos)
328ff57b454SGavin Shan 			return pos;
329ff57b454SGavin Shan 
330ff57b454SGavin Shan 		pos = PCI_EXT_CAP_NEXT(header);
331ff57b454SGavin Shan 		if (pos < 256)
332ff57b454SGavin Shan 			break;
333ff57b454SGavin Shan 
334ff57b454SGavin Shan 		if (pnv_pci_cfg_read(pdn, pos, 4, &header) != PCIBIOS_SUCCESSFUL)
335ff57b454SGavin Shan 			break;
336ff57b454SGavin Shan 	}
337ff57b454SGavin Shan 
338ff57b454SGavin Shan 	return 0;
339ff57b454SGavin Shan }
340ff57b454SGavin Shan 
341a131bfc6SOliver O'Halloran static struct eeh_pe *pnv_eeh_get_upstream_pe(struct pci_dev *pdev)
342a131bfc6SOliver O'Halloran {
343a131bfc6SOliver O'Halloran 	struct pci_controller *hose = pdev->bus->sysdata;
344a131bfc6SOliver O'Halloran 	struct pnv_phb *phb = hose->private_data;
345a131bfc6SOliver O'Halloran 	struct pci_dev *parent = pdev->bus->self;
346a131bfc6SOliver O'Halloran 
347a131bfc6SOliver O'Halloran #ifdef CONFIG_PCI_IOV
348a131bfc6SOliver O'Halloran 	/* for VFs we use the PF's PE as the upstream PE */
349a131bfc6SOliver O'Halloran 	if (pdev->is_virtfn)
350a131bfc6SOliver O'Halloran 		parent = pdev->physfn;
351a131bfc6SOliver O'Halloran #endif
352a131bfc6SOliver O'Halloran 
353a131bfc6SOliver O'Halloran 	/* otherwise use the PE of our parent bridge */
354a131bfc6SOliver O'Halloran 	if (parent) {
355a131bfc6SOliver O'Halloran 		struct pnv_ioda_pe *ioda_pe = pnv_ioda_get_pe(parent);
356a131bfc6SOliver O'Halloran 
357a131bfc6SOliver O'Halloran 		return eeh_pe_get(phb->hose, ioda_pe->pe_number, 0);
358a131bfc6SOliver O'Halloran 	}
359a131bfc6SOliver O'Halloran 
360a131bfc6SOliver O'Halloran 	return NULL;
361a131bfc6SOliver O'Halloran }
362a131bfc6SOliver O'Halloran 
36329310e5eSGavin Shan /**
364ff57b454SGavin Shan  * pnv_eeh_probe - Do probe on PCI device
365e86350f7SOliver O'Halloran  * @pdev: pci_dev to probe
36629310e5eSGavin Shan  *
367e86350f7SOliver O'Halloran  * Create, or find the existing, eeh_dev for this pci_dev.
36829310e5eSGavin Shan  */
369e86350f7SOliver O'Halloran static struct eeh_dev *pnv_eeh_probe(struct pci_dev *pdev)
37029310e5eSGavin Shan {
371e86350f7SOliver O'Halloran 	struct pci_dn *pdn = pci_get_pdn(pdev);
372ff57b454SGavin Shan 	struct pci_controller *hose = pdn->phb;
37329310e5eSGavin Shan 	struct pnv_phb *phb = hose->private_data;
374ff57b454SGavin Shan 	struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
375a131bfc6SOliver O'Halloran 	struct eeh_pe *upstream_pe;
376ff57b454SGavin Shan 	uint32_t pcie_flags;
377dadcd6d6SMike Qiu 	int ret;
378405b33a7SAlexey Kardashevskiy 	int config_addr = (pdn->busno << 8) | (pdn->devfn);
37929310e5eSGavin Shan 
38029310e5eSGavin Shan 	/*
38129310e5eSGavin Shan 	 * When probing the root bridge, which doesn't have any
38229310e5eSGavin Shan 	 * subordinate PCI devices. We don't have OF node for
38329310e5eSGavin Shan 	 * the root bridge. So it's not reasonable to continue
38429310e5eSGavin Shan 	 * the probing.
38529310e5eSGavin Shan 	 */
386ff57b454SGavin Shan 	if (!edev || edev->pe)
387ff57b454SGavin Shan 		return NULL;
38829310e5eSGavin Shan 
389e86350f7SOliver O'Halloran 	/* already configured? */
390e86350f7SOliver O'Halloran 	if (edev->pdev) {
391e86350f7SOliver O'Halloran 		pr_debug("%s: found existing edev for %04x:%02x:%02x.%01x\n",
392e86350f7SOliver O'Halloran 			__func__, hose->global_number, config_addr >> 8,
393e86350f7SOliver O'Halloran 			PCI_SLOT(config_addr), PCI_FUNC(config_addr));
394e86350f7SOliver O'Halloran 		return edev;
395e86350f7SOliver O'Halloran 	}
396e86350f7SOliver O'Halloran 
39729310e5eSGavin Shan 	/* Skip for PCI-ISA bridge */
398768a4284SOliver O'Halloran 	if ((pdev->class >> 8) == PCI_CLASS_BRIDGE_ISA)
399ff57b454SGavin Shan 		return NULL;
40029310e5eSGavin Shan 
4011ff8f36fSSam Bobroff 	eeh_edev_dbg(edev, "Probing device\n");
4021ff8f36fSSam Bobroff 
40329310e5eSGavin Shan 	/* Initialize eeh device */
404ab55d218SGavin Shan 	edev->mode	&= 0xFFFFFF00;
405ff57b454SGavin Shan 	edev->pcix_cap = pnv_eeh_find_cap(pdn, PCI_CAP_ID_PCIX);
406ff57b454SGavin Shan 	edev->pcie_cap = pnv_eeh_find_cap(pdn, PCI_CAP_ID_EXP);
4079312bc5bSWei Yang 	edev->af_cap   = pnv_eeh_find_cap(pdn, PCI_CAP_ID_AF);
408ff57b454SGavin Shan 	edev->aer_cap  = pnv_eeh_find_ecap(pdn, PCI_EXT_CAP_ID_ERR);
409768a4284SOliver O'Halloran 	if ((pdev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
4104b83bd45SGavin Shan 		edev->mode |= EEH_DEV_BRIDGE;
411ff57b454SGavin Shan 		if (edev->pcie_cap) {
412ff57b454SGavin Shan 			pnv_pci_cfg_read(pdn, edev->pcie_cap + PCI_EXP_FLAGS,
413ff57b454SGavin Shan 					 2, &pcie_flags);
414ff57b454SGavin Shan 			pcie_flags = (pcie_flags & PCI_EXP_FLAGS_TYPE) >> 4;
415ff57b454SGavin Shan 			if (pcie_flags == PCI_EXP_TYPE_ROOT_PORT)
4164b83bd45SGavin Shan 				edev->mode |= EEH_DEV_ROOT_PORT;
417ff57b454SGavin Shan 			else if (pcie_flags == PCI_EXP_TYPE_DOWNSTREAM)
4184b83bd45SGavin Shan 				edev->mode |= EEH_DEV_DS_PORT;
419ff57b454SGavin Shan 		}
4204b83bd45SGavin Shan 	}
4214b83bd45SGavin Shan 
422405b33a7SAlexey Kardashevskiy 	edev->pe_config_addr = phb->ioda.pe_rmap[config_addr];
42329310e5eSGavin Shan 
424a131bfc6SOliver O'Halloran 	upstream_pe = pnv_eeh_get_upstream_pe(pdev);
425a131bfc6SOliver O'Halloran 
42629310e5eSGavin Shan 	/* Create PE */
427a131bfc6SOliver O'Halloran 	ret = eeh_pe_tree_insert(edev, upstream_pe);
428dadcd6d6SMike Qiu 	if (ret) {
4291ff8f36fSSam Bobroff 		eeh_edev_warn(edev, "Failed to add device to PE (code %d)\n", ret);
430ff57b454SGavin Shan 		return NULL;
431dadcd6d6SMike Qiu 	}
432dadcd6d6SMike Qiu 
433dadcd6d6SMike Qiu 	/*
434b6541db1SGavin Shan 	 * If the PE contains any one of following adapters, the
435b6541db1SGavin Shan 	 * PCI config space can't be accessed when dumping EEH log.
436b6541db1SGavin Shan 	 * Otherwise, we will run into fenced PHB caused by shortage
437b6541db1SGavin Shan 	 * of outbound credits in the adapter. The PCI config access
438b6541db1SGavin Shan 	 * should be blocked until PE reset. MMIO access is dropped
439b6541db1SGavin Shan 	 * by hardware certainly. In order to drop PCI config requests,
440b6541db1SGavin Shan 	 * one more flag (EEH_PE_CFG_RESTRICTED) is introduced, which
441b6541db1SGavin Shan 	 * will be checked in the backend for PE state retrival. If
442b6541db1SGavin Shan 	 * the PE becomes frozen for the first time and the flag has
443b6541db1SGavin Shan 	 * been set for the PE, we will set EEH_PE_CFG_BLOCKED for
444b6541db1SGavin Shan 	 * that PE to block its config space.
445b6541db1SGavin Shan 	 *
446c374ed27SGavin Shan 	 * Broadcom BCM5718 2-ports NICs (14e4:1656)
447b6541db1SGavin Shan 	 * Broadcom Austin 4-ports NICs (14e4:1657)
448353169acSGavin Shan 	 * Broadcom Shiner 4-ports 1G NICs (14e4:168a)
449179ea48bSGavin Shan 	 * Broadcom Shiner 2-ports 10G NICs (14e4:168e)
450b6541db1SGavin Shan 	 */
451ff57b454SGavin Shan 	if ((pdn->vendor_id == PCI_VENDOR_ID_BROADCOM &&
452c374ed27SGavin Shan 	     pdn->device_id == 0x1656) ||
453c374ed27SGavin Shan 	    (pdn->vendor_id == PCI_VENDOR_ID_BROADCOM &&
454ff57b454SGavin Shan 	     pdn->device_id == 0x1657) ||
455ff57b454SGavin Shan 	    (pdn->vendor_id == PCI_VENDOR_ID_BROADCOM &&
456353169acSGavin Shan 	     pdn->device_id == 0x168a) ||
457353169acSGavin Shan 	    (pdn->vendor_id == PCI_VENDOR_ID_BROADCOM &&
458ff57b454SGavin Shan 	     pdn->device_id == 0x168e))
459b6541db1SGavin Shan 		edev->pe->state |= EEH_PE_CFG_RESTRICTED;
460b6541db1SGavin Shan 
461b6541db1SGavin Shan 	/*
462dadcd6d6SMike Qiu 	 * Cache the PE primary bus, which can't be fetched when
463dadcd6d6SMike Qiu 	 * full hotplug is in progress. In that case, all child
464dadcd6d6SMike Qiu 	 * PCI devices of the PE are expected to be removed prior
465dadcd6d6SMike Qiu 	 * to PE reset.
466dadcd6d6SMike Qiu 	 */
46705ba75f8SGavin Shan 	if (!(edev->pe->state & EEH_PE_PRI_BUS)) {
468ff57b454SGavin Shan 		edev->pe->bus = pci_find_bus(hose->global_number,
469ff57b454SGavin Shan 					     pdn->busno);
47005ba75f8SGavin Shan 		if (edev->pe->bus)
47105ba75f8SGavin Shan 			edev->pe->state |= EEH_PE_PRI_BUS;
47205ba75f8SGavin Shan 	}
47329310e5eSGavin Shan 
47429310e5eSGavin Shan 	/*
47529310e5eSGavin Shan 	 * Enable EEH explicitly so that we will do EEH check
47629310e5eSGavin Shan 	 * while accessing I/O stuff
47729310e5eSGavin Shan 	 */
478b905f8cdSSam Bobroff 	if (!eeh_has_flag(EEH_ENABLED)) {
479b905f8cdSSam Bobroff 		enable_irq(eeh_event_irq);
480b905f8cdSSam Bobroff 		pnv_eeh_enable_phbs();
48105b1721dSGavin Shan 		eeh_add_flag(EEH_ENABLED);
482b905f8cdSSam Bobroff 	}
48329310e5eSGavin Shan 
48429310e5eSGavin Shan 	/* Save memory bars */
48529310e5eSGavin Shan 	eeh_save_bars(edev);
48629310e5eSGavin Shan 
4871ff8f36fSSam Bobroff 	eeh_edev_dbg(edev, "EEH enabled on device\n");
488617082a4SSam Bobroff 
489e86350f7SOliver O'Halloran 	return edev;
49029310e5eSGavin Shan }
49129310e5eSGavin Shan 
49229310e5eSGavin Shan /**
49301f3bfb7SGavin Shan  * pnv_eeh_set_option - Initialize EEH or MMIO/DMA reenable
49429310e5eSGavin Shan  * @pe: EEH PE
49529310e5eSGavin Shan  * @option: operation to be issued
49629310e5eSGavin Shan  *
49729310e5eSGavin Shan  * The function is used to control the EEH functionality globally.
49829310e5eSGavin Shan  * Currently, following options are support according to PAPR:
49929310e5eSGavin Shan  * Enable EEH, Disable EEH, Enable MMIO and Enable DMA
50029310e5eSGavin Shan  */
50101f3bfb7SGavin Shan static int pnv_eeh_set_option(struct eeh_pe *pe, int option)
50229310e5eSGavin Shan {
50329310e5eSGavin Shan 	struct pci_controller *hose = pe->phb;
50429310e5eSGavin Shan 	struct pnv_phb *phb = hose->private_data;
5057e3e4f8dSGavin Shan 	bool freeze_pe = false;
506f9433718SGavin Shan 	int opt;
5077e3e4f8dSGavin Shan 	s64 rc;
50829310e5eSGavin Shan 
5097e3e4f8dSGavin Shan 	switch (option) {
5107e3e4f8dSGavin Shan 	case EEH_OPT_DISABLE:
5117e3e4f8dSGavin Shan 		return -EPERM;
5127e3e4f8dSGavin Shan 	case EEH_OPT_ENABLE:
5137e3e4f8dSGavin Shan 		return 0;
5147e3e4f8dSGavin Shan 	case EEH_OPT_THAW_MMIO:
5157e3e4f8dSGavin Shan 		opt = OPAL_EEH_ACTION_CLEAR_FREEZE_MMIO;
5167e3e4f8dSGavin Shan 		break;
5177e3e4f8dSGavin Shan 	case EEH_OPT_THAW_DMA:
5187e3e4f8dSGavin Shan 		opt = OPAL_EEH_ACTION_CLEAR_FREEZE_DMA;
5197e3e4f8dSGavin Shan 		break;
5207e3e4f8dSGavin Shan 	case EEH_OPT_FREEZE_PE:
5217e3e4f8dSGavin Shan 		freeze_pe = true;
5227e3e4f8dSGavin Shan 		opt = OPAL_EEH_ACTION_SET_FREEZE_ALL;
5237e3e4f8dSGavin Shan 		break;
5247e3e4f8dSGavin Shan 	default:
5257e3e4f8dSGavin Shan 		pr_warn("%s: Invalid option %d\n", __func__, option);
5267e3e4f8dSGavin Shan 		return -EINVAL;
5277e3e4f8dSGavin Shan 	}
5287e3e4f8dSGavin Shan 
529f9433718SGavin Shan 	/* Freeze master and slave PEs if PHB supports compound PEs */
5307e3e4f8dSGavin Shan 	if (freeze_pe) {
5317e3e4f8dSGavin Shan 		if (phb->freeze_pe) {
5327e3e4f8dSGavin Shan 			phb->freeze_pe(phb, pe->addr);
533f9433718SGavin Shan 			return 0;
5347e3e4f8dSGavin Shan 		}
53529310e5eSGavin Shan 
536f9433718SGavin Shan 		rc = opal_pci_eeh_freeze_set(phb->opal_id, pe->addr, opt);
537f9433718SGavin Shan 		if (rc != OPAL_SUCCESS) {
538f9433718SGavin Shan 			pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
539f9433718SGavin Shan 				__func__, rc, phb->hose->global_number,
540f9433718SGavin Shan 				pe->addr);
541f9433718SGavin Shan 			return -EIO;
542f9433718SGavin Shan 		}
543f9433718SGavin Shan 
544f9433718SGavin Shan 		return 0;
545f9433718SGavin Shan 	}
546f9433718SGavin Shan 
547f9433718SGavin Shan 	/* Unfreeze master and slave PEs if PHB supports */
548f9433718SGavin Shan 	if (phb->unfreeze_pe)
549f9433718SGavin Shan 		return phb->unfreeze_pe(phb, pe->addr, opt);
550f9433718SGavin Shan 
551f9433718SGavin Shan 	rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe->addr, opt);
552f9433718SGavin Shan 	if (rc != OPAL_SUCCESS) {
553f9433718SGavin Shan 		pr_warn("%s: Failure %lld enable %d for PHB#%x-PE#%x\n",
554f9433718SGavin Shan 			__func__, rc, option, phb->hose->global_number,
555f9433718SGavin Shan 			pe->addr);
556f9433718SGavin Shan 		return -EIO;
557f9433718SGavin Shan 	}
558f9433718SGavin Shan 
559f9433718SGavin Shan 	return 0;
56029310e5eSGavin Shan }
56129310e5eSGavin Shan 
56240ae5f69SGavin Shan static void pnv_eeh_get_phb_diag(struct eeh_pe *pe)
56340ae5f69SGavin Shan {
56440ae5f69SGavin Shan 	struct pnv_phb *phb = pe->phb->private_data;
56540ae5f69SGavin Shan 	s64 rc;
56640ae5f69SGavin Shan 
56740ae5f69SGavin Shan 	rc = opal_pci_get_phb_diag_data2(phb->opal_id, pe->data,
5685cb1f8fdSRussell Currey 					 phb->diag_data_size);
56940ae5f69SGavin Shan 	if (rc != OPAL_SUCCESS)
57040ae5f69SGavin Shan 		pr_warn("%s: Failure %lld getting PHB#%x diag-data\n",
57140ae5f69SGavin Shan 			__func__, rc, pe->phb->global_number);
57240ae5f69SGavin Shan }
57340ae5f69SGavin Shan 
57440ae5f69SGavin Shan static int pnv_eeh_get_phb_state(struct eeh_pe *pe)
57540ae5f69SGavin Shan {
57640ae5f69SGavin Shan 	struct pnv_phb *phb = pe->phb->private_data;
577c2057701SAlexey Kardashevskiy 	u8 fstate = 0;
578c2057701SAlexey Kardashevskiy 	__be16 pcierr = 0;
57940ae5f69SGavin Shan 	s64 rc;
58040ae5f69SGavin Shan 	int result = 0;
58140ae5f69SGavin Shan 
58240ae5f69SGavin Shan 	rc = opal_pci_eeh_freeze_status(phb->opal_id,
58340ae5f69SGavin Shan 					pe->addr,
58440ae5f69SGavin Shan 					&fstate,
58540ae5f69SGavin Shan 					&pcierr,
58640ae5f69SGavin Shan 					NULL);
58740ae5f69SGavin Shan 	if (rc != OPAL_SUCCESS) {
58840ae5f69SGavin Shan 		pr_warn("%s: Failure %lld getting PHB#%x state\n",
58940ae5f69SGavin Shan 			__func__, rc, phb->hose->global_number);
59040ae5f69SGavin Shan 		return EEH_STATE_NOT_SUPPORT;
59140ae5f69SGavin Shan 	}
59240ae5f69SGavin Shan 
59340ae5f69SGavin Shan 	/*
59440ae5f69SGavin Shan 	 * Check PHB state. If the PHB is frozen for the
59540ae5f69SGavin Shan 	 * first time, to dump the PHB diag-data.
59640ae5f69SGavin Shan 	 */
59740ae5f69SGavin Shan 	if (be16_to_cpu(pcierr) != OPAL_EEH_PHB_ERROR) {
59840ae5f69SGavin Shan 		result = (EEH_STATE_MMIO_ACTIVE  |
59940ae5f69SGavin Shan 			  EEH_STATE_DMA_ACTIVE   |
60040ae5f69SGavin Shan 			  EEH_STATE_MMIO_ENABLED |
60140ae5f69SGavin Shan 			  EEH_STATE_DMA_ENABLED);
60240ae5f69SGavin Shan 	} else if (!(pe->state & EEH_PE_ISOLATED)) {
603e762bb89SSam Bobroff 		eeh_pe_mark_isolated(pe);
60440ae5f69SGavin Shan 		pnv_eeh_get_phb_diag(pe);
60540ae5f69SGavin Shan 
60640ae5f69SGavin Shan 		if (eeh_has_flag(EEH_EARLY_DUMP_LOG))
60740ae5f69SGavin Shan 			pnv_pci_dump_phb_diag_data(pe->phb, pe->data);
60840ae5f69SGavin Shan 	}
60940ae5f69SGavin Shan 
61040ae5f69SGavin Shan 	return result;
61140ae5f69SGavin Shan }
61240ae5f69SGavin Shan 
61340ae5f69SGavin Shan static int pnv_eeh_get_pe_state(struct eeh_pe *pe)
61440ae5f69SGavin Shan {
61540ae5f69SGavin Shan 	struct pnv_phb *phb = pe->phb->private_data;
616c2057701SAlexey Kardashevskiy 	u8 fstate = 0;
617c2057701SAlexey Kardashevskiy 	__be16 pcierr = 0;
61840ae5f69SGavin Shan 	s64 rc;
61940ae5f69SGavin Shan 	int result;
62040ae5f69SGavin Shan 
62140ae5f69SGavin Shan 	/*
62240ae5f69SGavin Shan 	 * We don't clobber hardware frozen state until PE
62340ae5f69SGavin Shan 	 * reset is completed. In order to keep EEH core
62440ae5f69SGavin Shan 	 * moving forward, we have to return operational
62540ae5f69SGavin Shan 	 * state during PE reset.
62640ae5f69SGavin Shan 	 */
62740ae5f69SGavin Shan 	if (pe->state & EEH_PE_RESET) {
62840ae5f69SGavin Shan 		result = (EEH_STATE_MMIO_ACTIVE  |
62940ae5f69SGavin Shan 			  EEH_STATE_DMA_ACTIVE   |
63040ae5f69SGavin Shan 			  EEH_STATE_MMIO_ENABLED |
63140ae5f69SGavin Shan 			  EEH_STATE_DMA_ENABLED);
63240ae5f69SGavin Shan 		return result;
63340ae5f69SGavin Shan 	}
63440ae5f69SGavin Shan 
63540ae5f69SGavin Shan 	/*
63640ae5f69SGavin Shan 	 * Fetch PE state from hardware. If the PHB
63740ae5f69SGavin Shan 	 * supports compound PE, let it handle that.
63840ae5f69SGavin Shan 	 */
63940ae5f69SGavin Shan 	if (phb->get_pe_state) {
64040ae5f69SGavin Shan 		fstate = phb->get_pe_state(phb, pe->addr);
64140ae5f69SGavin Shan 	} else {
64240ae5f69SGavin Shan 		rc = opal_pci_eeh_freeze_status(phb->opal_id,
64340ae5f69SGavin Shan 						pe->addr,
64440ae5f69SGavin Shan 						&fstate,
64540ae5f69SGavin Shan 						&pcierr,
64640ae5f69SGavin Shan 						NULL);
64740ae5f69SGavin Shan 		if (rc != OPAL_SUCCESS) {
64840ae5f69SGavin Shan 			pr_warn("%s: Failure %lld getting PHB#%x-PE%x state\n",
64940ae5f69SGavin Shan 				__func__, rc, phb->hose->global_number,
65040ae5f69SGavin Shan 				pe->addr);
65140ae5f69SGavin Shan 			return EEH_STATE_NOT_SUPPORT;
65240ae5f69SGavin Shan 		}
65340ae5f69SGavin Shan 	}
65440ae5f69SGavin Shan 
65540ae5f69SGavin Shan 	/* Figure out state */
65640ae5f69SGavin Shan 	switch (fstate) {
65740ae5f69SGavin Shan 	case OPAL_EEH_STOPPED_NOT_FROZEN:
65840ae5f69SGavin Shan 		result = (EEH_STATE_MMIO_ACTIVE  |
65940ae5f69SGavin Shan 			  EEH_STATE_DMA_ACTIVE   |
66040ae5f69SGavin Shan 			  EEH_STATE_MMIO_ENABLED |
66140ae5f69SGavin Shan 			  EEH_STATE_DMA_ENABLED);
66240ae5f69SGavin Shan 		break;
66340ae5f69SGavin Shan 	case OPAL_EEH_STOPPED_MMIO_FREEZE:
66440ae5f69SGavin Shan 		result = (EEH_STATE_DMA_ACTIVE |
66540ae5f69SGavin Shan 			  EEH_STATE_DMA_ENABLED);
66640ae5f69SGavin Shan 		break;
66740ae5f69SGavin Shan 	case OPAL_EEH_STOPPED_DMA_FREEZE:
66840ae5f69SGavin Shan 		result = (EEH_STATE_MMIO_ACTIVE |
66940ae5f69SGavin Shan 			  EEH_STATE_MMIO_ENABLED);
67040ae5f69SGavin Shan 		break;
67140ae5f69SGavin Shan 	case OPAL_EEH_STOPPED_MMIO_DMA_FREEZE:
67240ae5f69SGavin Shan 		result = 0;
67340ae5f69SGavin Shan 		break;
67440ae5f69SGavin Shan 	case OPAL_EEH_STOPPED_RESET:
67540ae5f69SGavin Shan 		result = EEH_STATE_RESET_ACTIVE;
67640ae5f69SGavin Shan 		break;
67740ae5f69SGavin Shan 	case OPAL_EEH_STOPPED_TEMP_UNAVAIL:
67840ae5f69SGavin Shan 		result = EEH_STATE_UNAVAILABLE;
67940ae5f69SGavin Shan 		break;
68040ae5f69SGavin Shan 	case OPAL_EEH_STOPPED_PERM_UNAVAIL:
68140ae5f69SGavin Shan 		result = EEH_STATE_NOT_SUPPORT;
68240ae5f69SGavin Shan 		break;
68340ae5f69SGavin Shan 	default:
68440ae5f69SGavin Shan 		result = EEH_STATE_NOT_SUPPORT;
68540ae5f69SGavin Shan 		pr_warn("%s: Invalid PHB#%x-PE#%x state %x\n",
68640ae5f69SGavin Shan 			__func__, phb->hose->global_number,
68740ae5f69SGavin Shan 			pe->addr, fstate);
68840ae5f69SGavin Shan 	}
68940ae5f69SGavin Shan 
69040ae5f69SGavin Shan 	/*
69140ae5f69SGavin Shan 	 * If PHB supports compound PE, to freeze all
69240ae5f69SGavin Shan 	 * slave PEs for consistency.
69340ae5f69SGavin Shan 	 *
69440ae5f69SGavin Shan 	 * If the PE is switching to frozen state for the
69540ae5f69SGavin Shan 	 * first time, to dump the PHB diag-data.
69640ae5f69SGavin Shan 	 */
69740ae5f69SGavin Shan 	if (!(result & EEH_STATE_NOT_SUPPORT) &&
69840ae5f69SGavin Shan 	    !(result & EEH_STATE_UNAVAILABLE) &&
69940ae5f69SGavin Shan 	    !(result & EEH_STATE_MMIO_ACTIVE) &&
70040ae5f69SGavin Shan 	    !(result & EEH_STATE_DMA_ACTIVE)  &&
70140ae5f69SGavin Shan 	    !(pe->state & EEH_PE_ISOLATED)) {
70240ae5f69SGavin Shan 		if (phb->freeze_pe)
70340ae5f69SGavin Shan 			phb->freeze_pe(phb, pe->addr);
70440ae5f69SGavin Shan 
705e762bb89SSam Bobroff 		eeh_pe_mark_isolated(pe);
70640ae5f69SGavin Shan 		pnv_eeh_get_phb_diag(pe);
70740ae5f69SGavin Shan 
70840ae5f69SGavin Shan 		if (eeh_has_flag(EEH_EARLY_DUMP_LOG))
70940ae5f69SGavin Shan 			pnv_pci_dump_phb_diag_data(pe->phb, pe->data);
71040ae5f69SGavin Shan 	}
71140ae5f69SGavin Shan 
71240ae5f69SGavin Shan 	return result;
71340ae5f69SGavin Shan }
71440ae5f69SGavin Shan 
71529310e5eSGavin Shan /**
71601f3bfb7SGavin Shan  * pnv_eeh_get_state - Retrieve PE state
71729310e5eSGavin Shan  * @pe: EEH PE
71829310e5eSGavin Shan  * @delay: delay while PE state is temporarily unavailable
71929310e5eSGavin Shan  *
72029310e5eSGavin Shan  * Retrieve the state of the specified PE. For IODA-compitable
72129310e5eSGavin Shan  * platform, it should be retrieved from IODA table. Therefore,
72229310e5eSGavin Shan  * we prefer passing down to hardware implementation to handle
72329310e5eSGavin Shan  * it.
72429310e5eSGavin Shan  */
72501f3bfb7SGavin Shan static int pnv_eeh_get_state(struct eeh_pe *pe, int *delay)
72629310e5eSGavin Shan {
72740ae5f69SGavin Shan 	int ret;
72829310e5eSGavin Shan 
72940ae5f69SGavin Shan 	if (pe->type & EEH_PE_PHB)
73040ae5f69SGavin Shan 		ret = pnv_eeh_get_phb_state(pe);
73140ae5f69SGavin Shan 	else
73240ae5f69SGavin Shan 		ret = pnv_eeh_get_pe_state(pe);
73340ae5f69SGavin Shan 
73440ae5f69SGavin Shan 	if (!delay)
73540ae5f69SGavin Shan 		return ret;
73629310e5eSGavin Shan 
73729310e5eSGavin Shan 	/*
73829310e5eSGavin Shan 	 * If the PE state is temporarily unavailable,
73929310e5eSGavin Shan 	 * to inform the EEH core delay for default
74029310e5eSGavin Shan 	 * period (1 second)
74129310e5eSGavin Shan 	 */
74229310e5eSGavin Shan 	*delay = 0;
74329310e5eSGavin Shan 	if (ret & EEH_STATE_UNAVAILABLE)
74429310e5eSGavin Shan 		*delay = 1000;
74529310e5eSGavin Shan 
74629310e5eSGavin Shan 	return ret;
74729310e5eSGavin Shan }
74829310e5eSGavin Shan 
749ebe22531SGavin Shan static s64 pnv_eeh_poll(unsigned long id)
750cadf364dSGavin Shan {
751cadf364dSGavin Shan 	s64 rc = OPAL_HARDWARE;
752cadf364dSGavin Shan 
753cadf364dSGavin Shan 	while (1) {
754ebe22531SGavin Shan 		rc = opal_pci_poll(id);
755cadf364dSGavin Shan 		if (rc <= 0)
756cadf364dSGavin Shan 			break;
757cadf364dSGavin Shan 
758cadf364dSGavin Shan 		if (system_state < SYSTEM_RUNNING)
759cadf364dSGavin Shan 			udelay(1000 * rc);
760cadf364dSGavin Shan 		else
761cadf364dSGavin Shan 			msleep(rc);
762cadf364dSGavin Shan 	}
763cadf364dSGavin Shan 
764cadf364dSGavin Shan 	return rc;
765cadf364dSGavin Shan }
766cadf364dSGavin Shan 
767cadf364dSGavin Shan int pnv_eeh_phb_reset(struct pci_controller *hose, int option)
768cadf364dSGavin Shan {
769cadf364dSGavin Shan 	struct pnv_phb *phb = hose->private_data;
770cadf364dSGavin Shan 	s64 rc = OPAL_HARDWARE;
771cadf364dSGavin Shan 
772cadf364dSGavin Shan 	pr_debug("%s: Reset PHB#%x, option=%d\n",
773cadf364dSGavin Shan 		 __func__, hose->global_number, option);
774cadf364dSGavin Shan 
775cadf364dSGavin Shan 	/* Issue PHB complete reset request */
776cadf364dSGavin Shan 	if (option == EEH_RESET_FUNDAMENTAL ||
777cadf364dSGavin Shan 	    option == EEH_RESET_HOT)
778cadf364dSGavin Shan 		rc = opal_pci_reset(phb->opal_id,
779cadf364dSGavin Shan 				    OPAL_RESET_PHB_COMPLETE,
780cadf364dSGavin Shan 				    OPAL_ASSERT_RESET);
781cadf364dSGavin Shan 	else if (option == EEH_RESET_DEACTIVATE)
782cadf364dSGavin Shan 		rc = opal_pci_reset(phb->opal_id,
783cadf364dSGavin Shan 				    OPAL_RESET_PHB_COMPLETE,
784cadf364dSGavin Shan 				    OPAL_DEASSERT_RESET);
785cadf364dSGavin Shan 	if (rc < 0)
786cadf364dSGavin Shan 		goto out;
787cadf364dSGavin Shan 
788cadf364dSGavin Shan 	/*
789cadf364dSGavin Shan 	 * Poll state of the PHB until the request is done
790cadf364dSGavin Shan 	 * successfully. The PHB reset is usually PHB complete
791cadf364dSGavin Shan 	 * reset followed by hot reset on root bus. So we also
792cadf364dSGavin Shan 	 * need the PCI bus settlement delay.
793cadf364dSGavin Shan 	 */
794fbce44d0SGavin Shan 	if (rc > 0)
795ebe22531SGavin Shan 		rc = pnv_eeh_poll(phb->opal_id);
796cadf364dSGavin Shan 	if (option == EEH_RESET_DEACTIVATE) {
797cadf364dSGavin Shan 		if (system_state < SYSTEM_RUNNING)
798cadf364dSGavin Shan 			udelay(1000 * EEH_PE_RST_SETTLE_TIME);
799cadf364dSGavin Shan 		else
800cadf364dSGavin Shan 			msleep(EEH_PE_RST_SETTLE_TIME);
801cadf364dSGavin Shan 	}
802cadf364dSGavin Shan out:
803cadf364dSGavin Shan 	if (rc != OPAL_SUCCESS)
804cadf364dSGavin Shan 		return -EIO;
805cadf364dSGavin Shan 
806cadf364dSGavin Shan 	return 0;
807cadf364dSGavin Shan }
808cadf364dSGavin Shan 
809cadf364dSGavin Shan static int pnv_eeh_root_reset(struct pci_controller *hose, int option)
810cadf364dSGavin Shan {
811cadf364dSGavin Shan 	struct pnv_phb *phb = hose->private_data;
812cadf364dSGavin Shan 	s64 rc = OPAL_HARDWARE;
813cadf364dSGavin Shan 
814cadf364dSGavin Shan 	pr_debug("%s: Reset PHB#%x, option=%d\n",
815cadf364dSGavin Shan 		 __func__, hose->global_number, option);
816cadf364dSGavin Shan 
817cadf364dSGavin Shan 	/*
818cadf364dSGavin Shan 	 * During the reset deassert time, we needn't care
819cadf364dSGavin Shan 	 * the reset scope because the firmware does nothing
820cadf364dSGavin Shan 	 * for fundamental or hot reset during deassert phase.
821cadf364dSGavin Shan 	 */
822cadf364dSGavin Shan 	if (option == EEH_RESET_FUNDAMENTAL)
823cadf364dSGavin Shan 		rc = opal_pci_reset(phb->opal_id,
824cadf364dSGavin Shan 				    OPAL_RESET_PCI_FUNDAMENTAL,
825cadf364dSGavin Shan 				    OPAL_ASSERT_RESET);
826cadf364dSGavin Shan 	else if (option == EEH_RESET_HOT)
827cadf364dSGavin Shan 		rc = opal_pci_reset(phb->opal_id,
828cadf364dSGavin Shan 				    OPAL_RESET_PCI_HOT,
829cadf364dSGavin Shan 				    OPAL_ASSERT_RESET);
830cadf364dSGavin Shan 	else if (option == EEH_RESET_DEACTIVATE)
831cadf364dSGavin Shan 		rc = opal_pci_reset(phb->opal_id,
832cadf364dSGavin Shan 				    OPAL_RESET_PCI_HOT,
833cadf364dSGavin Shan 				    OPAL_DEASSERT_RESET);
834cadf364dSGavin Shan 	if (rc < 0)
835cadf364dSGavin Shan 		goto out;
836cadf364dSGavin Shan 
837cadf364dSGavin Shan 	/* Poll state of the PHB until the request is done */
838fbce44d0SGavin Shan 	if (rc > 0)
839ebe22531SGavin Shan 		rc = pnv_eeh_poll(phb->opal_id);
840cadf364dSGavin Shan 	if (option == EEH_RESET_DEACTIVATE)
841cadf364dSGavin Shan 		msleep(EEH_PE_RST_SETTLE_TIME);
842cadf364dSGavin Shan out:
843cadf364dSGavin Shan 	if (rc != OPAL_SUCCESS)
844cadf364dSGavin Shan 		return -EIO;
845cadf364dSGavin Shan 
846cadf364dSGavin Shan 	return 0;
847cadf364dSGavin Shan }
848cadf364dSGavin Shan 
8499c0e1ecbSGavin Shan static int __pnv_eeh_bridge_reset(struct pci_dev *dev, int option)
850cadf364dSGavin Shan {
8510bd78587SGavin Shan 	struct pci_dn *pdn = pci_get_pdn_by_devfn(dev->bus, dev->devfn);
8520bd78587SGavin Shan 	struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
853cadf364dSGavin Shan 	int aer = edev ? edev->aer_cap : 0;
854cadf364dSGavin Shan 	u32 ctrl;
855cadf364dSGavin Shan 
85698fd32cdSOliver O'Halloran 	pr_debug("%s: Secondary Reset PCI bus %04x:%02x with option %d\n",
857cadf364dSGavin Shan 		 __func__, pci_domain_nr(dev->bus),
858cadf364dSGavin Shan 		 dev->bus->number, option);
859cadf364dSGavin Shan 
860cadf364dSGavin Shan 	switch (option) {
861cadf364dSGavin Shan 	case EEH_RESET_FUNDAMENTAL:
862cadf364dSGavin Shan 	case EEH_RESET_HOT:
863cadf364dSGavin Shan 		/* Don't report linkDown event */
864cadf364dSGavin Shan 		if (aer) {
86517d2a487SOliver O'Halloran 			eeh_ops->read_config(edev, aer + PCI_ERR_UNCOR_MASK,
866cadf364dSGavin Shan 					     4, &ctrl);
867cadf364dSGavin Shan 			ctrl |= PCI_ERR_UNC_SURPDN;
86817d2a487SOliver O'Halloran 			eeh_ops->write_config(edev, aer + PCI_ERR_UNCOR_MASK,
869cadf364dSGavin Shan 					      4, ctrl);
870cadf364dSGavin Shan 		}
871cadf364dSGavin Shan 
87217d2a487SOliver O'Halloran 		eeh_ops->read_config(edev, PCI_BRIDGE_CONTROL, 2, &ctrl);
873cadf364dSGavin Shan 		ctrl |= PCI_BRIDGE_CTL_BUS_RESET;
87417d2a487SOliver O'Halloran 		eeh_ops->write_config(edev, PCI_BRIDGE_CONTROL, 2, ctrl);
875cadf364dSGavin Shan 
876cadf364dSGavin Shan 		msleep(EEH_PE_RST_HOLD_TIME);
877cadf364dSGavin Shan 		break;
878cadf364dSGavin Shan 	case EEH_RESET_DEACTIVATE:
87917d2a487SOliver O'Halloran 		eeh_ops->read_config(edev, PCI_BRIDGE_CONTROL, 2, &ctrl);
880cadf364dSGavin Shan 		ctrl &= ~PCI_BRIDGE_CTL_BUS_RESET;
88117d2a487SOliver O'Halloran 		eeh_ops->write_config(edev, PCI_BRIDGE_CONTROL, 2, ctrl);
882cadf364dSGavin Shan 
883cadf364dSGavin Shan 		msleep(EEH_PE_RST_SETTLE_TIME);
884cadf364dSGavin Shan 
885cadf364dSGavin Shan 		/* Continue reporting linkDown event */
886cadf364dSGavin Shan 		if (aer) {
88717d2a487SOliver O'Halloran 			eeh_ops->read_config(edev, aer + PCI_ERR_UNCOR_MASK,
888cadf364dSGavin Shan 					     4, &ctrl);
889cadf364dSGavin Shan 			ctrl &= ~PCI_ERR_UNC_SURPDN;
89017d2a487SOliver O'Halloran 			eeh_ops->write_config(edev, aer + PCI_ERR_UNCOR_MASK,
891cadf364dSGavin Shan 					      4, ctrl);
892cadf364dSGavin Shan 		}
893cadf364dSGavin Shan 
894cadf364dSGavin Shan 		break;
895cadf364dSGavin Shan 	}
896cadf364dSGavin Shan 
897cadf364dSGavin Shan 	return 0;
898cadf364dSGavin Shan }
899cadf364dSGavin Shan 
9009c0e1ecbSGavin Shan static int pnv_eeh_bridge_reset(struct pci_dev *pdev, int option)
9019c0e1ecbSGavin Shan {
9029c0e1ecbSGavin Shan 	struct pci_controller *hose = pci_bus_to_host(pdev->bus);
9039c0e1ecbSGavin Shan 	struct pnv_phb *phb = hose->private_data;
9049c0e1ecbSGavin Shan 	struct device_node *dn = pci_device_to_OF_node(pdev);
9059c0e1ecbSGavin Shan 	uint64_t id = PCI_SLOT_ID(phb->opal_id,
9069c0e1ecbSGavin Shan 				  (pdev->bus->number << 8) | pdev->devfn);
9079c0e1ecbSGavin Shan 	uint8_t scope;
9089c0e1ecbSGavin Shan 	int64_t rc;
9099c0e1ecbSGavin Shan 
9109c0e1ecbSGavin Shan 	/* Hot reset to the bus if firmware cannot handle */
9119c0e1ecbSGavin Shan 	if (!dn || !of_get_property(dn, "ibm,reset-by-firmware", NULL))
9129c0e1ecbSGavin Shan 		return __pnv_eeh_bridge_reset(pdev, option);
9139c0e1ecbSGavin Shan 
91498fd32cdSOliver O'Halloran 	pr_debug("%s: FW reset PCI bus %04x:%02x with option %d\n",
91598fd32cdSOliver O'Halloran 		 __func__, pci_domain_nr(pdev->bus),
91698fd32cdSOliver O'Halloran 		 pdev->bus->number, option);
91798fd32cdSOliver O'Halloran 
9189c0e1ecbSGavin Shan 	switch (option) {
9199c0e1ecbSGavin Shan 	case EEH_RESET_FUNDAMENTAL:
9209c0e1ecbSGavin Shan 		scope = OPAL_RESET_PCI_FUNDAMENTAL;
9219c0e1ecbSGavin Shan 		break;
9229c0e1ecbSGavin Shan 	case EEH_RESET_HOT:
9239c0e1ecbSGavin Shan 		scope = OPAL_RESET_PCI_HOT;
9249c0e1ecbSGavin Shan 		break;
9259c0e1ecbSGavin Shan 	case EEH_RESET_DEACTIVATE:
9269c0e1ecbSGavin Shan 		return 0;
9279c0e1ecbSGavin Shan 	default:
9289c0e1ecbSGavin Shan 		dev_dbg(&pdev->dev, "%s: Unsupported reset %d\n",
9299c0e1ecbSGavin Shan 			__func__, option);
9309c0e1ecbSGavin Shan 		return -EINVAL;
9319c0e1ecbSGavin Shan 	}
9329c0e1ecbSGavin Shan 
9339c0e1ecbSGavin Shan 	rc = opal_pci_reset(id, scope, OPAL_ASSERT_RESET);
9349c0e1ecbSGavin Shan 	if (rc <= OPAL_SUCCESS)
9359c0e1ecbSGavin Shan 		goto out;
9369c0e1ecbSGavin Shan 
9379c0e1ecbSGavin Shan 	rc = pnv_eeh_poll(id);
9389c0e1ecbSGavin Shan out:
9399c0e1ecbSGavin Shan 	return (rc == OPAL_SUCCESS) ? 0 : -EIO;
9409c0e1ecbSGavin Shan }
9419c0e1ecbSGavin Shan 
942cadf364dSGavin Shan void pnv_pci_reset_secondary_bus(struct pci_dev *dev)
943cadf364dSGavin Shan {
944848912e5SMichael Ellerman 	struct pci_controller *hose;
945848912e5SMichael Ellerman 
946848912e5SMichael Ellerman 	if (pci_is_root_bus(dev->bus)) {
947848912e5SMichael Ellerman 		hose = pci_bus_to_host(dev->bus);
948848912e5SMichael Ellerman 		pnv_eeh_root_reset(hose, EEH_RESET_HOT);
949848912e5SMichael Ellerman 		pnv_eeh_root_reset(hose, EEH_RESET_DEACTIVATE);
950848912e5SMichael Ellerman 	} else {
951cadf364dSGavin Shan 		pnv_eeh_bridge_reset(dev, EEH_RESET_HOT);
952cadf364dSGavin Shan 		pnv_eeh_bridge_reset(dev, EEH_RESET_DEACTIVATE);
953cadf364dSGavin Shan 	}
954848912e5SMichael Ellerman }
955cadf364dSGavin Shan 
9569312bc5bSWei Yang static void pnv_eeh_wait_for_pending(struct pci_dn *pdn, const char *type,
9579312bc5bSWei Yang 				     int pos, u16 mask)
9589312bc5bSWei Yang {
95917d2a487SOliver O'Halloran 	struct eeh_dev *edev = pdn->edev;
9609312bc5bSWei Yang 	int i, status = 0;
9619312bc5bSWei Yang 
9629312bc5bSWei Yang 	/* Wait for Transaction Pending bit to be cleared */
9639312bc5bSWei Yang 	for (i = 0; i < 4; i++) {
96417d2a487SOliver O'Halloran 		eeh_ops->read_config(edev, pos, 2, &status);
9659312bc5bSWei Yang 		if (!(status & mask))
9669312bc5bSWei Yang 			return;
9679312bc5bSWei Yang 
9689312bc5bSWei Yang 		msleep((1 << i) * 100);
9699312bc5bSWei Yang 	}
9709312bc5bSWei Yang 
9719312bc5bSWei Yang 	pr_warn("%s: Pending transaction while issuing %sFLR to %04x:%02x:%02x.%01x\n",
9729312bc5bSWei Yang 		__func__, type,
97369672bd7SAlexey Kardashevskiy 		pdn->phb->global_number, pdn->busno,
9749312bc5bSWei Yang 		PCI_SLOT(pdn->devfn), PCI_FUNC(pdn->devfn));
9759312bc5bSWei Yang }
9769312bc5bSWei Yang 
9779312bc5bSWei Yang static int pnv_eeh_do_flr(struct pci_dn *pdn, int option)
9789312bc5bSWei Yang {
9799312bc5bSWei Yang 	struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
9809312bc5bSWei Yang 	u32 reg = 0;
9819312bc5bSWei Yang 
9829312bc5bSWei Yang 	if (WARN_ON(!edev->pcie_cap))
9839312bc5bSWei Yang 		return -ENOTTY;
9849312bc5bSWei Yang 
98517d2a487SOliver O'Halloran 	eeh_ops->read_config(edev, edev->pcie_cap + PCI_EXP_DEVCAP, 4, &reg);
9869312bc5bSWei Yang 	if (!(reg & PCI_EXP_DEVCAP_FLR))
9879312bc5bSWei Yang 		return -ENOTTY;
9889312bc5bSWei Yang 
9899312bc5bSWei Yang 	switch (option) {
9909312bc5bSWei Yang 	case EEH_RESET_HOT:
9919312bc5bSWei Yang 	case EEH_RESET_FUNDAMENTAL:
9929312bc5bSWei Yang 		pnv_eeh_wait_for_pending(pdn, "",
9939312bc5bSWei Yang 					 edev->pcie_cap + PCI_EXP_DEVSTA,
9949312bc5bSWei Yang 					 PCI_EXP_DEVSTA_TRPND);
99517d2a487SOliver O'Halloran 		eeh_ops->read_config(edev, edev->pcie_cap + PCI_EXP_DEVCTL,
9969312bc5bSWei Yang 				     4, &reg);
9979312bc5bSWei Yang 		reg |= PCI_EXP_DEVCTL_BCR_FLR;
99817d2a487SOliver O'Halloran 		eeh_ops->write_config(edev, edev->pcie_cap + PCI_EXP_DEVCTL,
9999312bc5bSWei Yang 				      4, reg);
10009312bc5bSWei Yang 		msleep(EEH_PE_RST_HOLD_TIME);
10019312bc5bSWei Yang 		break;
10029312bc5bSWei Yang 	case EEH_RESET_DEACTIVATE:
100317d2a487SOliver O'Halloran 		eeh_ops->read_config(edev, edev->pcie_cap + PCI_EXP_DEVCTL,
10049312bc5bSWei Yang 				     4, &reg);
10059312bc5bSWei Yang 		reg &= ~PCI_EXP_DEVCTL_BCR_FLR;
100617d2a487SOliver O'Halloran 		eeh_ops->write_config(edev, edev->pcie_cap + PCI_EXP_DEVCTL,
10079312bc5bSWei Yang 				      4, reg);
10089312bc5bSWei Yang 		msleep(EEH_PE_RST_SETTLE_TIME);
10099312bc5bSWei Yang 		break;
10109312bc5bSWei Yang 	}
10119312bc5bSWei Yang 
10129312bc5bSWei Yang 	return 0;
10139312bc5bSWei Yang }
10149312bc5bSWei Yang 
10159312bc5bSWei Yang static int pnv_eeh_do_af_flr(struct pci_dn *pdn, int option)
10169312bc5bSWei Yang {
10179312bc5bSWei Yang 	struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
10189312bc5bSWei Yang 	u32 cap = 0;
10199312bc5bSWei Yang 
10209312bc5bSWei Yang 	if (WARN_ON(!edev->af_cap))
10219312bc5bSWei Yang 		return -ENOTTY;
10229312bc5bSWei Yang 
102317d2a487SOliver O'Halloran 	eeh_ops->read_config(edev, edev->af_cap + PCI_AF_CAP, 1, &cap);
10249312bc5bSWei Yang 	if (!(cap & PCI_AF_CAP_TP) || !(cap & PCI_AF_CAP_FLR))
10259312bc5bSWei Yang 		return -ENOTTY;
10269312bc5bSWei Yang 
10279312bc5bSWei Yang 	switch (option) {
10289312bc5bSWei Yang 	case EEH_RESET_HOT:
10299312bc5bSWei Yang 	case EEH_RESET_FUNDAMENTAL:
10309312bc5bSWei Yang 		/*
10319312bc5bSWei Yang 		 * Wait for Transaction Pending bit to clear. A word-aligned
10329312bc5bSWei Yang 		 * test is used, so we use the conrol offset rather than status
10339312bc5bSWei Yang 		 * and shift the test bit to match.
10349312bc5bSWei Yang 		 */
10359312bc5bSWei Yang 		pnv_eeh_wait_for_pending(pdn, "AF",
10369312bc5bSWei Yang 					 edev->af_cap + PCI_AF_CTRL,
10379312bc5bSWei Yang 					 PCI_AF_STATUS_TP << 8);
103817d2a487SOliver O'Halloran 		eeh_ops->write_config(edev, edev->af_cap + PCI_AF_CTRL,
10399312bc5bSWei Yang 				      1, PCI_AF_CTRL_FLR);
10409312bc5bSWei Yang 		msleep(EEH_PE_RST_HOLD_TIME);
10419312bc5bSWei Yang 		break;
10429312bc5bSWei Yang 	case EEH_RESET_DEACTIVATE:
104317d2a487SOliver O'Halloran 		eeh_ops->write_config(edev, edev->af_cap + PCI_AF_CTRL, 1, 0);
10449312bc5bSWei Yang 		msleep(EEH_PE_RST_SETTLE_TIME);
10459312bc5bSWei Yang 		break;
10469312bc5bSWei Yang 	}
10479312bc5bSWei Yang 
10489312bc5bSWei Yang 	return 0;
10499312bc5bSWei Yang }
10509312bc5bSWei Yang 
10519312bc5bSWei Yang static int pnv_eeh_reset_vf_pe(struct eeh_pe *pe, int option)
10529312bc5bSWei Yang {
10539312bc5bSWei Yang 	struct eeh_dev *edev;
10549312bc5bSWei Yang 	struct pci_dn *pdn;
10559312bc5bSWei Yang 	int ret;
10569312bc5bSWei Yang 
10579312bc5bSWei Yang 	/* The VF PE should have only one child device */
105880e65b00SSam Bobroff 	edev = list_first_entry_or_null(&pe->edevs, struct eeh_dev, entry);
10599312bc5bSWei Yang 	pdn = eeh_dev_to_pdn(edev);
10609312bc5bSWei Yang 	if (!pdn)
10619312bc5bSWei Yang 		return -ENXIO;
10629312bc5bSWei Yang 
10639312bc5bSWei Yang 	ret = pnv_eeh_do_flr(pdn, option);
10649312bc5bSWei Yang 	if (!ret)
10659312bc5bSWei Yang 		return ret;
10669312bc5bSWei Yang 
10679312bc5bSWei Yang 	return pnv_eeh_do_af_flr(pdn, option);
10689312bc5bSWei Yang }
10699312bc5bSWei Yang 
107029310e5eSGavin Shan /**
107101f3bfb7SGavin Shan  * pnv_eeh_reset - Reset the specified PE
107229310e5eSGavin Shan  * @pe: EEH PE
107329310e5eSGavin Shan  * @option: reset option
107429310e5eSGavin Shan  *
1075cadf364dSGavin Shan  * Do reset on the indicated PE. For PCI bus sensitive PE,
1076cadf364dSGavin Shan  * we need to reset the parent p2p bridge. The PHB has to
1077cadf364dSGavin Shan  * be reinitialized if the p2p bridge is root bridge. For
1078cadf364dSGavin Shan  * PCI device sensitive PE, we will try to reset the device
1079cadf364dSGavin Shan  * through FLR. For now, we don't have OPAL APIs to do HARD
1080cadf364dSGavin Shan  * reset yet, so all reset would be SOFT (HOT) reset.
108129310e5eSGavin Shan  */
108201f3bfb7SGavin Shan static int pnv_eeh_reset(struct eeh_pe *pe, int option)
108329310e5eSGavin Shan {
108429310e5eSGavin Shan 	struct pci_controller *hose = pe->phb;
10854fad4943SGavin Shan 	struct pnv_phb *phb;
1086cadf364dSGavin Shan 	struct pci_bus *bus;
10874fad4943SGavin Shan 	int64_t rc;
108829310e5eSGavin Shan 
1089cadf364dSGavin Shan 	/*
1090cadf364dSGavin Shan 	 * For PHB reset, we always have complete reset. For those PEs whose
1091cadf364dSGavin Shan 	 * primary bus derived from root complex (root bus) or root port
1092cadf364dSGavin Shan 	 * (usually bus#1), we apply hot or fundamental reset on the root port.
1093cadf364dSGavin Shan 	 * For other PEs, we always have hot reset on the PE primary bus.
1094cadf364dSGavin Shan 	 *
1095cadf364dSGavin Shan 	 * Here, we have different design to pHyp, which always clear the
1096cadf364dSGavin Shan 	 * frozen state during PE reset. However, the good idea here from
1097cadf364dSGavin Shan 	 * benh is to keep frozen state before we get PE reset done completely
1098cadf364dSGavin Shan 	 * (until BAR restore). With the frozen state, HW drops illegal IO
1099cadf364dSGavin Shan 	 * or MMIO access, which can incur recrusive frozen PE during PE
1100cadf364dSGavin Shan 	 * reset. The side effect is that EEH core has to clear the frozen
1101cadf364dSGavin Shan 	 * state explicitly after BAR restore.
1102cadf364dSGavin Shan 	 */
11034fad4943SGavin Shan 	if (pe->type & EEH_PE_PHB)
11044fad4943SGavin Shan 		return pnv_eeh_phb_reset(hose, option);
1105cadf364dSGavin Shan 
1106cadf364dSGavin Shan 	/*
1107cadf364dSGavin Shan 	 * The frozen PE might be caused by PAPR error injection
1108cadf364dSGavin Shan 	 * registers, which are expected to be cleared after hitting
1109cadf364dSGavin Shan 	 * frozen PE as stated in the hardware spec. Unfortunately,
1110cadf364dSGavin Shan 	 * that's not true on P7IOC. So we have to clear it manually
1111cadf364dSGavin Shan 	 * to avoid recursive EEH errors during recovery.
1112cadf364dSGavin Shan 	 */
1113cadf364dSGavin Shan 	phb = hose->private_data;
1114cadf364dSGavin Shan 	if (phb->model == PNV_PHB_MODEL_P7IOC &&
1115cadf364dSGavin Shan 	    (option == EEH_RESET_HOT ||
1116cadf364dSGavin Shan 	     option == EEH_RESET_FUNDAMENTAL)) {
1117cadf364dSGavin Shan 		rc = opal_pci_reset(phb->opal_id,
1118cadf364dSGavin Shan 				    OPAL_RESET_PHB_ERROR,
1119cadf364dSGavin Shan 				    OPAL_ASSERT_RESET);
1120cadf364dSGavin Shan 		if (rc != OPAL_SUCCESS) {
11214fad4943SGavin Shan 			pr_warn("%s: Failure %lld clearing error injection registers\n",
1122cadf364dSGavin Shan 				__func__, rc);
1123cadf364dSGavin Shan 			return -EIO;
1124cadf364dSGavin Shan 		}
1125cadf364dSGavin Shan 	}
1126cadf364dSGavin Shan 
1127e98ddb77SRussell Currey 	if (pe->type & EEH_PE_VF)
1128e98ddb77SRussell Currey 		return pnv_eeh_reset_vf_pe(pe, option);
1129e98ddb77SRussell Currey 
1130cadf364dSGavin Shan 	bus = eeh_pe_bus_get(pe);
113104fec21cSRussell Currey 	if (!bus) {
11321f52f176SRussell Currey 		pr_err("%s: Cannot find PCI bus for PHB#%x-PE#%x\n",
113304fec21cSRussell Currey 			__func__, pe->phb->global_number, pe->addr);
113404fec21cSRussell Currey 		return -EIO;
113504fec21cSRussell Currey 	}
113629310e5eSGavin Shan 
113798fd32cdSOliver O'Halloran 	if (pci_is_root_bus(bus))
11384fad4943SGavin Shan 		return pnv_eeh_root_reset(hose, option);
11394fad4943SGavin Shan 
114098fd32cdSOliver O'Halloran 	/*
114198fd32cdSOliver O'Halloran 	 * For hot resets try use the generic PCI error recovery reset
114298fd32cdSOliver O'Halloran 	 * functions. These correctly handles the case where the secondary
114398fd32cdSOliver O'Halloran 	 * bus is behind a hotplug slot and it will use the slot provided
114498fd32cdSOliver O'Halloran 	 * reset methods to prevent spurious hotplug events during the reset.
114598fd32cdSOliver O'Halloran 	 *
114698fd32cdSOliver O'Halloran 	 * Fundemental resets need to be handled internally to EEH since the
114798fd32cdSOliver O'Halloran 	 * PCI core doesn't really have a concept of a fundemental reset,
114898fd32cdSOliver O'Halloran 	 * mainly because there's no standard way to generate one. Only a
114998fd32cdSOliver O'Halloran 	 * few devices require an FRESET so it should be fine.
115098fd32cdSOliver O'Halloran 	 */
115198fd32cdSOliver O'Halloran 	if (option != EEH_RESET_FUNDAMENTAL) {
115298fd32cdSOliver O'Halloran 		/*
115398fd32cdSOliver O'Halloran 		 * NB: Skiboot and pnv_eeh_bridge_reset() also no-op the
115498fd32cdSOliver O'Halloran 		 *     de-assert step. It's like the OPAL reset API was
115598fd32cdSOliver O'Halloran 		 *     poorly designed or something...
115698fd32cdSOliver O'Halloran 		 */
115798fd32cdSOliver O'Halloran 		if (option == EEH_RESET_DEACTIVATE)
115898fd32cdSOliver O'Halloran 			return 0;
115998fd32cdSOliver O'Halloran 
116098fd32cdSOliver O'Halloran 		rc = pci_bus_error_reset(bus->self);
116198fd32cdSOliver O'Halloran 		if (!rc)
116298fd32cdSOliver O'Halloran 			return 0;
116398fd32cdSOliver O'Halloran 	}
116498fd32cdSOliver O'Halloran 
116598fd32cdSOliver O'Halloran 	/* otherwise, use the generic bridge reset. this might call into FW */
116698fd32cdSOliver O'Halloran 	if (pci_is_root_bus(bus->parent))
116798fd32cdSOliver O'Halloran 		return pnv_eeh_root_reset(hose, option);
11684fad4943SGavin Shan 	return pnv_eeh_bridge_reset(bus->self, option);
116929310e5eSGavin Shan }
117029310e5eSGavin Shan 
117129310e5eSGavin Shan /**
117201f3bfb7SGavin Shan  * pnv_eeh_get_log - Retrieve error log
117329310e5eSGavin Shan  * @pe: EEH PE
117429310e5eSGavin Shan  * @severity: temporary or permanent error log
117529310e5eSGavin Shan  * @drv_log: driver log to be combined with retrieved error log
117629310e5eSGavin Shan  * @len: length of driver log
117729310e5eSGavin Shan  *
117829310e5eSGavin Shan  * Retrieve the temporary or permanent error from the PE.
117929310e5eSGavin Shan  */
118001f3bfb7SGavin Shan static int pnv_eeh_get_log(struct eeh_pe *pe, int severity,
118129310e5eSGavin Shan 			   char *drv_log, unsigned long len)
118229310e5eSGavin Shan {
118395edcdeaSGavin Shan 	if (!eeh_has_flag(EEH_EARLY_DUMP_LOG))
118495edcdeaSGavin Shan 		pnv_pci_dump_phb_diag_data(pe->phb, pe->data);
118529310e5eSGavin Shan 
118695edcdeaSGavin Shan 	return 0;
118729310e5eSGavin Shan }
118829310e5eSGavin Shan 
118929310e5eSGavin Shan /**
119001f3bfb7SGavin Shan  * pnv_eeh_configure_bridge - Configure PCI bridges in the indicated PE
119129310e5eSGavin Shan  * @pe: EEH PE
119229310e5eSGavin Shan  *
119329310e5eSGavin Shan  * The function will be called to reconfigure the bridges included
119429310e5eSGavin Shan  * in the specified PE so that the mulfunctional PE would be recovered
119529310e5eSGavin Shan  * again.
119629310e5eSGavin Shan  */
119701f3bfb7SGavin Shan static int pnv_eeh_configure_bridge(struct eeh_pe *pe)
119829310e5eSGavin Shan {
1199bbe170edSGavin Shan 	return 0;
120029310e5eSGavin Shan }
120129310e5eSGavin Shan 
120229310e5eSGavin Shan /**
120301f3bfb7SGavin Shan  * pnv_pe_err_inject - Inject specified error to the indicated PE
1204131c123aSGavin Shan  * @pe: the indicated PE
1205131c123aSGavin Shan  * @type: error type
1206131c123aSGavin Shan  * @func: specific error type
1207131c123aSGavin Shan  * @addr: address
1208131c123aSGavin Shan  * @mask: address mask
1209131c123aSGavin Shan  *
1210131c123aSGavin Shan  * The routine is called to inject specified error, which is
1211131c123aSGavin Shan  * determined by @type and @func, to the indicated PE for
1212131c123aSGavin Shan  * testing purpose.
1213131c123aSGavin Shan  */
121401f3bfb7SGavin Shan static int pnv_eeh_err_inject(struct eeh_pe *pe, int type, int func,
1215131c123aSGavin Shan 			      unsigned long addr, unsigned long mask)
1216131c123aSGavin Shan {
1217131c123aSGavin Shan 	struct pci_controller *hose = pe->phb;
1218131c123aSGavin Shan 	struct pnv_phb *phb = hose->private_data;
1219fa646c3cSGavin Shan 	s64 rc;
1220131c123aSGavin Shan 
1221fa646c3cSGavin Shan 	if (type != OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR &&
1222fa646c3cSGavin Shan 	    type != OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR64) {
1223fa646c3cSGavin Shan 		pr_warn("%s: Invalid error type %d\n",
1224fa646c3cSGavin Shan 			__func__, type);
1225fa646c3cSGavin Shan 		return -ERANGE;
1226fa646c3cSGavin Shan 	}
1227131c123aSGavin Shan 
1228fa646c3cSGavin Shan 	if (func < OPAL_ERR_INJECT_FUNC_IOA_LD_MEM_ADDR ||
1229fa646c3cSGavin Shan 	    func > OPAL_ERR_INJECT_FUNC_IOA_DMA_WR_TARGET) {
1230fa646c3cSGavin Shan 		pr_warn("%s: Invalid error function %d\n",
1231fa646c3cSGavin Shan 			__func__, func);
1232fa646c3cSGavin Shan 		return -ERANGE;
1233fa646c3cSGavin Shan 	}
1234fa646c3cSGavin Shan 
1235fa646c3cSGavin Shan 	/* Firmware supports error injection ? */
1236fa646c3cSGavin Shan 	if (!opal_check_token(OPAL_PCI_ERR_INJECT)) {
1237fa646c3cSGavin Shan 		pr_warn("%s: Firmware doesn't support error injection\n",
1238fa646c3cSGavin Shan 			__func__);
1239fa646c3cSGavin Shan 		return -ENXIO;
1240fa646c3cSGavin Shan 	}
1241fa646c3cSGavin Shan 
1242fa646c3cSGavin Shan 	/* Do error injection */
1243fa646c3cSGavin Shan 	rc = opal_pci_err_inject(phb->opal_id, pe->addr,
1244fa646c3cSGavin Shan 				 type, func, addr, mask);
1245fa646c3cSGavin Shan 	if (rc != OPAL_SUCCESS) {
1246fa646c3cSGavin Shan 		pr_warn("%s: Failure %lld injecting error "
1247fa646c3cSGavin Shan 			"%d-%d to PHB#%x-PE#%x\n",
1248fa646c3cSGavin Shan 			__func__, rc, type, func,
1249fa646c3cSGavin Shan 			hose->global_number, pe->addr);
1250fa646c3cSGavin Shan 		return -EIO;
1251fa646c3cSGavin Shan 	}
1252fa646c3cSGavin Shan 
1253fa646c3cSGavin Shan 	return 0;
1254131c123aSGavin Shan }
1255131c123aSGavin Shan 
12560bd78587SGavin Shan static inline bool pnv_eeh_cfg_blocked(struct pci_dn *pdn)
1257d2cfbcd7SGavin Shan {
12580bd78587SGavin Shan 	struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
1259d2cfbcd7SGavin Shan 
1260d2cfbcd7SGavin Shan 	if (!edev || !edev->pe)
1261d2cfbcd7SGavin Shan 		return false;
1262d2cfbcd7SGavin Shan 
12639312bc5bSWei Yang 	/*
12649312bc5bSWei Yang 	 * We will issue FLR or AF FLR to all VFs, which are contained
12659312bc5bSWei Yang 	 * in VF PE. It relies on the EEH PCI config accessors. So we
12669312bc5bSWei Yang 	 * can't block them during the window.
12679312bc5bSWei Yang 	 */
12689312bc5bSWei Yang 	if (edev->physfn && (edev->pe->state & EEH_PE_RESET))
12699312bc5bSWei Yang 		return false;
12709312bc5bSWei Yang 
1271d2cfbcd7SGavin Shan 	if (edev->pe->state & EEH_PE_CFG_BLOCKED)
1272d2cfbcd7SGavin Shan 		return true;
1273d2cfbcd7SGavin Shan 
1274d2cfbcd7SGavin Shan 	return false;
1275d2cfbcd7SGavin Shan }
1276d2cfbcd7SGavin Shan 
127717d2a487SOliver O'Halloran static int pnv_eeh_read_config(struct eeh_dev *edev,
1278d2cfbcd7SGavin Shan 			       int where, int size, u32 *val)
1279d2cfbcd7SGavin Shan {
128017d2a487SOliver O'Halloran 	struct pci_dn *pdn = eeh_dev_to_pdn(edev);
128117d2a487SOliver O'Halloran 
12823532a741SGavin Shan 	if (!pdn)
12833532a741SGavin Shan 		return PCIBIOS_DEVICE_NOT_FOUND;
12843532a741SGavin Shan 
12850bd78587SGavin Shan 	if (pnv_eeh_cfg_blocked(pdn)) {
1286d2cfbcd7SGavin Shan 		*val = 0xFFFFFFFF;
1287d2cfbcd7SGavin Shan 		return PCIBIOS_SET_FAILED;
1288d2cfbcd7SGavin Shan 	}
1289d2cfbcd7SGavin Shan 
12903532a741SGavin Shan 	return pnv_pci_cfg_read(pdn, where, size, val);
1291d2cfbcd7SGavin Shan }
1292d2cfbcd7SGavin Shan 
129317d2a487SOliver O'Halloran static int pnv_eeh_write_config(struct eeh_dev *edev,
1294d2cfbcd7SGavin Shan 				int where, int size, u32 val)
1295d2cfbcd7SGavin Shan {
129617d2a487SOliver O'Halloran 	struct pci_dn *pdn = eeh_dev_to_pdn(edev);
129717d2a487SOliver O'Halloran 
12983532a741SGavin Shan 	if (!pdn)
12993532a741SGavin Shan 		return PCIBIOS_DEVICE_NOT_FOUND;
13003532a741SGavin Shan 
13010bd78587SGavin Shan 	if (pnv_eeh_cfg_blocked(pdn))
1302d2cfbcd7SGavin Shan 		return PCIBIOS_SET_FAILED;
1303d2cfbcd7SGavin Shan 
13043532a741SGavin Shan 	return pnv_pci_cfg_write(pdn, where, size, val);
1305d2cfbcd7SGavin Shan }
1306d2cfbcd7SGavin Shan 
13072a485ad7SGavin Shan static void pnv_eeh_dump_hub_diag_common(struct OpalIoP7IOCErrorData *data)
13082a485ad7SGavin Shan {
13092a485ad7SGavin Shan 	/* GEM */
13102a485ad7SGavin Shan 	if (data->gemXfir || data->gemRfir ||
13112a485ad7SGavin Shan 	    data->gemRirqfir || data->gemMask || data->gemRwof)
13122a485ad7SGavin Shan 		pr_info("  GEM: %016llx %016llx %016llx %016llx %016llx\n",
13132a485ad7SGavin Shan 			be64_to_cpu(data->gemXfir),
13142a485ad7SGavin Shan 			be64_to_cpu(data->gemRfir),
13152a485ad7SGavin Shan 			be64_to_cpu(data->gemRirqfir),
13162a485ad7SGavin Shan 			be64_to_cpu(data->gemMask),
13172a485ad7SGavin Shan 			be64_to_cpu(data->gemRwof));
13182a485ad7SGavin Shan 
13192a485ad7SGavin Shan 	/* LEM */
13202a485ad7SGavin Shan 	if (data->lemFir || data->lemErrMask ||
13212a485ad7SGavin Shan 	    data->lemAction0 || data->lemAction1 || data->lemWof)
13222a485ad7SGavin Shan 		pr_info("  LEM: %016llx %016llx %016llx %016llx %016llx\n",
13232a485ad7SGavin Shan 			be64_to_cpu(data->lemFir),
13242a485ad7SGavin Shan 			be64_to_cpu(data->lemErrMask),
13252a485ad7SGavin Shan 			be64_to_cpu(data->lemAction0),
13262a485ad7SGavin Shan 			be64_to_cpu(data->lemAction1),
13272a485ad7SGavin Shan 			be64_to_cpu(data->lemWof));
13282a485ad7SGavin Shan }
13292a485ad7SGavin Shan 
13302a485ad7SGavin Shan static void pnv_eeh_get_and_dump_hub_diag(struct pci_controller *hose)
13312a485ad7SGavin Shan {
13322a485ad7SGavin Shan 	struct pnv_phb *phb = hose->private_data;
13335cb1f8fdSRussell Currey 	struct OpalIoP7IOCErrorData *data =
13345cb1f8fdSRussell Currey 		(struct OpalIoP7IOCErrorData*)phb->diag_data;
13352a485ad7SGavin Shan 	long rc;
13362a485ad7SGavin Shan 
13372a485ad7SGavin Shan 	rc = opal_pci_get_hub_diag_data(phb->hub_id, data, sizeof(*data));
13382a485ad7SGavin Shan 	if (rc != OPAL_SUCCESS) {
13392a485ad7SGavin Shan 		pr_warn("%s: Failed to get HUB#%llx diag-data (%ld)\n",
13402a485ad7SGavin Shan 			__func__, phb->hub_id, rc);
13412a485ad7SGavin Shan 		return;
13422a485ad7SGavin Shan 	}
13432a485ad7SGavin Shan 
1344a7032132SGavin Shan 	switch (be16_to_cpu(data->type)) {
13452a485ad7SGavin Shan 	case OPAL_P7IOC_DIAG_TYPE_RGC:
13462a485ad7SGavin Shan 		pr_info("P7IOC diag-data for RGC\n\n");
13472a485ad7SGavin Shan 		pnv_eeh_dump_hub_diag_common(data);
13482a485ad7SGavin Shan 		if (data->rgc.rgcStatus || data->rgc.rgcLdcp)
13492a485ad7SGavin Shan 			pr_info("  RGC: %016llx %016llx\n",
13502a485ad7SGavin Shan 				be64_to_cpu(data->rgc.rgcStatus),
13512a485ad7SGavin Shan 				be64_to_cpu(data->rgc.rgcLdcp));
13522a485ad7SGavin Shan 		break;
13532a485ad7SGavin Shan 	case OPAL_P7IOC_DIAG_TYPE_BI:
13542a485ad7SGavin Shan 		pr_info("P7IOC diag-data for BI %s\n\n",
13552a485ad7SGavin Shan 			data->bi.biDownbound ? "Downbound" : "Upbound");
13562a485ad7SGavin Shan 		pnv_eeh_dump_hub_diag_common(data);
13572a485ad7SGavin Shan 		if (data->bi.biLdcp0 || data->bi.biLdcp1 ||
13582a485ad7SGavin Shan 		    data->bi.biLdcp2 || data->bi.biFenceStatus)
13592a485ad7SGavin Shan 			pr_info("  BI:  %016llx %016llx %016llx %016llx\n",
13602a485ad7SGavin Shan 				be64_to_cpu(data->bi.biLdcp0),
13612a485ad7SGavin Shan 				be64_to_cpu(data->bi.biLdcp1),
13622a485ad7SGavin Shan 				be64_to_cpu(data->bi.biLdcp2),
13632a485ad7SGavin Shan 				be64_to_cpu(data->bi.biFenceStatus));
13642a485ad7SGavin Shan 		break;
13652a485ad7SGavin Shan 	case OPAL_P7IOC_DIAG_TYPE_CI:
13662a485ad7SGavin Shan 		pr_info("P7IOC diag-data for CI Port %d\n\n",
13672a485ad7SGavin Shan 			data->ci.ciPort);
13682a485ad7SGavin Shan 		pnv_eeh_dump_hub_diag_common(data);
13692a485ad7SGavin Shan 		if (data->ci.ciPortStatus || data->ci.ciPortLdcp)
13702a485ad7SGavin Shan 			pr_info("  CI:  %016llx %016llx\n",
13712a485ad7SGavin Shan 				be64_to_cpu(data->ci.ciPortStatus),
13722a485ad7SGavin Shan 				be64_to_cpu(data->ci.ciPortLdcp));
13732a485ad7SGavin Shan 		break;
13742a485ad7SGavin Shan 	case OPAL_P7IOC_DIAG_TYPE_MISC:
13752a485ad7SGavin Shan 		pr_info("P7IOC diag-data for MISC\n\n");
13762a485ad7SGavin Shan 		pnv_eeh_dump_hub_diag_common(data);
13772a485ad7SGavin Shan 		break;
13782a485ad7SGavin Shan 	case OPAL_P7IOC_DIAG_TYPE_I2C:
13792a485ad7SGavin Shan 		pr_info("P7IOC diag-data for I2C\n\n");
13802a485ad7SGavin Shan 		pnv_eeh_dump_hub_diag_common(data);
13812a485ad7SGavin Shan 		break;
13822a485ad7SGavin Shan 	default:
13832a485ad7SGavin Shan 		pr_warn("%s: Invalid type of HUB#%llx diag-data (%d)\n",
13842a485ad7SGavin Shan 			__func__, phb->hub_id, data->type);
13852a485ad7SGavin Shan 	}
13862a485ad7SGavin Shan }
13872a485ad7SGavin Shan 
13882a485ad7SGavin Shan static int pnv_eeh_get_pe(struct pci_controller *hose,
13892a485ad7SGavin Shan 			  u16 pe_no, struct eeh_pe **pe)
13902a485ad7SGavin Shan {
13912a485ad7SGavin Shan 	struct pnv_phb *phb = hose->private_data;
13922a485ad7SGavin Shan 	struct pnv_ioda_pe *pnv_pe;
13932a485ad7SGavin Shan 	struct eeh_pe *dev_pe;
13942a485ad7SGavin Shan 
13952a485ad7SGavin Shan 	/*
13962a485ad7SGavin Shan 	 * If PHB supports compound PE, to fetch
13972a485ad7SGavin Shan 	 * the master PE because slave PE is invisible
13982a485ad7SGavin Shan 	 * to EEH core.
13992a485ad7SGavin Shan 	 */
14002a485ad7SGavin Shan 	pnv_pe = &phb->ioda.pe_array[pe_no];
14012a485ad7SGavin Shan 	if (pnv_pe->flags & PNV_IODA_PE_SLAVE) {
14022a485ad7SGavin Shan 		pnv_pe = pnv_pe->master;
14032a485ad7SGavin Shan 		WARN_ON(!pnv_pe ||
14042a485ad7SGavin Shan 			!(pnv_pe->flags & PNV_IODA_PE_MASTER));
14052a485ad7SGavin Shan 		pe_no = pnv_pe->pe_number;
14062a485ad7SGavin Shan 	}
14072a485ad7SGavin Shan 
14082a485ad7SGavin Shan 	/* Find the PE according to PE# */
14098bae6a23SAlexey Kardashevskiy 	dev_pe = eeh_pe_get(hose, pe_no, 0);
14102a485ad7SGavin Shan 	if (!dev_pe)
14112a485ad7SGavin Shan 		return -EEXIST;
14122a485ad7SGavin Shan 
14132a485ad7SGavin Shan 	/* Freeze the (compound) PE */
14142a485ad7SGavin Shan 	*pe = dev_pe;
14152a485ad7SGavin Shan 	if (!(dev_pe->state & EEH_PE_ISOLATED))
14162a485ad7SGavin Shan 		phb->freeze_pe(phb, pe_no);
14172a485ad7SGavin Shan 
14182a485ad7SGavin Shan 	/*
14192a485ad7SGavin Shan 	 * At this point, we're sure the (compound) PE should
14202a485ad7SGavin Shan 	 * have been frozen. However, we still need poke until
14212a485ad7SGavin Shan 	 * hitting the frozen PE on top level.
14222a485ad7SGavin Shan 	 */
14232a485ad7SGavin Shan 	dev_pe = dev_pe->parent;
14242a485ad7SGavin Shan 	while (dev_pe && !(dev_pe->type & EEH_PE_PHB)) {
14252a485ad7SGavin Shan 		int ret;
14262a485ad7SGavin Shan 		ret = eeh_ops->get_state(dev_pe, NULL);
142734a286a4SSam Bobroff 		if (ret <= 0 || eeh_state_active(ret)) {
14282a485ad7SGavin Shan 			dev_pe = dev_pe->parent;
14292a485ad7SGavin Shan 			continue;
14302a485ad7SGavin Shan 		}
14312a485ad7SGavin Shan 
14322a485ad7SGavin Shan 		/* Frozen parent PE */
14332a485ad7SGavin Shan 		*pe = dev_pe;
14342a485ad7SGavin Shan 		if (!(dev_pe->state & EEH_PE_ISOLATED))
14352a485ad7SGavin Shan 			phb->freeze_pe(phb, dev_pe->addr);
14362a485ad7SGavin Shan 
14372a485ad7SGavin Shan 		/* Next one */
14382a485ad7SGavin Shan 		dev_pe = dev_pe->parent;
14392a485ad7SGavin Shan 	}
14402a485ad7SGavin Shan 
14412a485ad7SGavin Shan 	return 0;
14422a485ad7SGavin Shan }
14432a485ad7SGavin Shan 
1444131c123aSGavin Shan /**
144501f3bfb7SGavin Shan  * pnv_eeh_next_error - Retrieve next EEH error to handle
144629310e5eSGavin Shan  * @pe: Affected PE
144729310e5eSGavin Shan  *
14482a485ad7SGavin Shan  * The function is expected to be called by EEH core while it gets
14492a485ad7SGavin Shan  * special EEH event (without binding PE). The function calls to
14502a485ad7SGavin Shan  * OPAL APIs for next error to handle. The informational error is
14512a485ad7SGavin Shan  * handled internally by platform. However, the dead IOC, dead PHB,
14522a485ad7SGavin Shan  * fenced PHB and frozen PE should be handled by EEH core eventually.
145329310e5eSGavin Shan  */
145401f3bfb7SGavin Shan static int pnv_eeh_next_error(struct eeh_pe **pe)
145529310e5eSGavin Shan {
145629310e5eSGavin Shan 	struct pci_controller *hose;
14572a485ad7SGavin Shan 	struct pnv_phb *phb;
14582a485ad7SGavin Shan 	struct eeh_pe *phb_pe, *parent_pe;
14592a485ad7SGavin Shan 	__be64 frozen_pe_no;
14602a485ad7SGavin Shan 	__be16 err_type, severity;
14612a485ad7SGavin Shan 	long rc;
14622a485ad7SGavin Shan 	int state, ret = EEH_NEXT_ERR_NONE;
14632a485ad7SGavin Shan 
14642a485ad7SGavin Shan 	/*
146579231448SAlistair Popple 	 * While running here, it's safe to purge the event queue. The
146679231448SAlistair Popple 	 * event should still be masked.
14672a485ad7SGavin Shan 	 */
14682a485ad7SGavin Shan 	eeh_remove_event(NULL, false);
146929310e5eSGavin Shan 
147029310e5eSGavin Shan 	list_for_each_entry(hose, &hose_list, list_node) {
14712a485ad7SGavin Shan 		/*
14722a485ad7SGavin Shan 		 * If the subordinate PCI buses of the PHB has been
14732a485ad7SGavin Shan 		 * removed or is exactly under error recovery, we
14742a485ad7SGavin Shan 		 * needn't take care of it any more.
14752a485ad7SGavin Shan 		 */
147629310e5eSGavin Shan 		phb = hose->private_data;
14772a485ad7SGavin Shan 		phb_pe = eeh_phb_pe_get(hose);
14782a485ad7SGavin Shan 		if (!phb_pe || (phb_pe->state & EEH_PE_ISOLATED))
14792a485ad7SGavin Shan 			continue;
14802a485ad7SGavin Shan 
14812a485ad7SGavin Shan 		rc = opal_pci_next_error(phb->opal_id,
14822a485ad7SGavin Shan 					 &frozen_pe_no, &err_type, &severity);
14832a485ad7SGavin Shan 		if (rc != OPAL_SUCCESS) {
14842a485ad7SGavin Shan 			pr_devel("%s: Invalid return value on "
14852a485ad7SGavin Shan 				 "PHB#%x (0x%lx) from opal_pci_next_error",
14862a485ad7SGavin Shan 				 __func__, hose->global_number, rc);
14872a485ad7SGavin Shan 			continue;
14882a485ad7SGavin Shan 		}
14892a485ad7SGavin Shan 
14902a485ad7SGavin Shan 		/* If the PHB doesn't have error, stop processing */
14912a485ad7SGavin Shan 		if (be16_to_cpu(err_type) == OPAL_EEH_NO_ERROR ||
14922a485ad7SGavin Shan 		    be16_to_cpu(severity) == OPAL_EEH_SEV_NO_ERROR) {
14932a485ad7SGavin Shan 			pr_devel("%s: No error found on PHB#%x\n",
14942a485ad7SGavin Shan 				 __func__, hose->global_number);
14952a485ad7SGavin Shan 			continue;
14962a485ad7SGavin Shan 		}
14972a485ad7SGavin Shan 
14982a485ad7SGavin Shan 		/*
14992a485ad7SGavin Shan 		 * Processing the error. We're expecting the error with
15002a485ad7SGavin Shan 		 * highest priority reported upon multiple errors on the
15012a485ad7SGavin Shan 		 * specific PHB.
15022a485ad7SGavin Shan 		 */
15032a485ad7SGavin Shan 		pr_devel("%s: Error (%d, %d, %llu) on PHB#%x\n",
15042a485ad7SGavin Shan 			__func__, be16_to_cpu(err_type),
15052a485ad7SGavin Shan 			be16_to_cpu(severity), be64_to_cpu(frozen_pe_no),
15062a485ad7SGavin Shan 			hose->global_number);
15072a485ad7SGavin Shan 		switch (be16_to_cpu(err_type)) {
15082a485ad7SGavin Shan 		case OPAL_EEH_IOC_ERROR:
15092a485ad7SGavin Shan 			if (be16_to_cpu(severity) == OPAL_EEH_SEV_IOC_DEAD) {
15102a485ad7SGavin Shan 				pr_err("EEH: dead IOC detected\n");
15112a485ad7SGavin Shan 				ret = EEH_NEXT_ERR_DEAD_IOC;
15122a485ad7SGavin Shan 			} else if (be16_to_cpu(severity) == OPAL_EEH_SEV_INF) {
15132a485ad7SGavin Shan 				pr_info("EEH: IOC informative error "
15142a485ad7SGavin Shan 					"detected\n");
15152a485ad7SGavin Shan 				pnv_eeh_get_and_dump_hub_diag(hose);
15162a485ad7SGavin Shan 				ret = EEH_NEXT_ERR_NONE;
15172a485ad7SGavin Shan 			}
15182a485ad7SGavin Shan 
15192a485ad7SGavin Shan 			break;
15202a485ad7SGavin Shan 		case OPAL_EEH_PHB_ERROR:
15212a485ad7SGavin Shan 			if (be16_to_cpu(severity) == OPAL_EEH_SEV_PHB_DEAD) {
15222a485ad7SGavin Shan 				*pe = phb_pe;
15232a485ad7SGavin Shan 				pr_err("EEH: dead PHB#%x detected, "
15242a485ad7SGavin Shan 				       "location: %s\n",
15252a485ad7SGavin Shan 					hose->global_number,
15262a485ad7SGavin Shan 					eeh_pe_loc_get(phb_pe));
15272a485ad7SGavin Shan 				ret = EEH_NEXT_ERR_DEAD_PHB;
15282a485ad7SGavin Shan 			} else if (be16_to_cpu(severity) ==
15292a485ad7SGavin Shan 				   OPAL_EEH_SEV_PHB_FENCED) {
15302a485ad7SGavin Shan 				*pe = phb_pe;
15312a485ad7SGavin Shan 				pr_err("EEH: Fenced PHB#%x detected, "
15322a485ad7SGavin Shan 				       "location: %s\n",
15332a485ad7SGavin Shan 					hose->global_number,
15342a485ad7SGavin Shan 					eeh_pe_loc_get(phb_pe));
15352a485ad7SGavin Shan 				ret = EEH_NEXT_ERR_FENCED_PHB;
15362a485ad7SGavin Shan 			} else if (be16_to_cpu(severity) == OPAL_EEH_SEV_INF) {
15372a485ad7SGavin Shan 				pr_info("EEH: PHB#%x informative error "
15382a485ad7SGavin Shan 					"detected, location: %s\n",
15392a485ad7SGavin Shan 					hose->global_number,
15402a485ad7SGavin Shan 					eeh_pe_loc_get(phb_pe));
15412a485ad7SGavin Shan 				pnv_eeh_get_phb_diag(phb_pe);
15422a485ad7SGavin Shan 				pnv_pci_dump_phb_diag_data(hose, phb_pe->data);
15432a485ad7SGavin Shan 				ret = EEH_NEXT_ERR_NONE;
15442a485ad7SGavin Shan 			}
15452a485ad7SGavin Shan 
15462a485ad7SGavin Shan 			break;
15472a485ad7SGavin Shan 		case OPAL_EEH_PE_ERROR:
15482a485ad7SGavin Shan 			/*
15492a485ad7SGavin Shan 			 * If we can't find the corresponding PE, we
15502a485ad7SGavin Shan 			 * just try to unfreeze.
15512a485ad7SGavin Shan 			 */
15522a485ad7SGavin Shan 			if (pnv_eeh_get_pe(hose,
15532a485ad7SGavin Shan 				be64_to_cpu(frozen_pe_no), pe)) {
15542a485ad7SGavin Shan 				pr_info("EEH: Clear non-existing PHB#%x-PE#%llx\n",
15550f36db77SGavin Shan 					hose->global_number, be64_to_cpu(frozen_pe_no));
15562a485ad7SGavin Shan 				pr_info("EEH: PHB location: %s\n",
15572a485ad7SGavin Shan 					eeh_pe_loc_get(phb_pe));
155879cd9520SGavin Shan 
155979cd9520SGavin Shan 				/* Dump PHB diag-data */
156079cd9520SGavin Shan 				rc = opal_pci_get_phb_diag_data2(phb->opal_id,
15615cb1f8fdSRussell Currey 					phb->diag_data, phb->diag_data_size);
156279cd9520SGavin Shan 				if (rc == OPAL_SUCCESS)
156379cd9520SGavin Shan 					pnv_pci_dump_phb_diag_data(hose,
15645cb1f8fdSRussell Currey 							phb->diag_data);
156579cd9520SGavin Shan 
156679cd9520SGavin Shan 				/* Try best to clear it */
15672a485ad7SGavin Shan 				opal_pci_eeh_freeze_clear(phb->opal_id,
1568d63e51b3SGavin Shan 					be64_to_cpu(frozen_pe_no),
15692a485ad7SGavin Shan 					OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
15702a485ad7SGavin Shan 				ret = EEH_NEXT_ERR_NONE;
15712a485ad7SGavin Shan 			} else if ((*pe)->state & EEH_PE_ISOLATED ||
15722a485ad7SGavin Shan 				   eeh_pe_passed(*pe)) {
15732a485ad7SGavin Shan 				ret = EEH_NEXT_ERR_NONE;
15742a485ad7SGavin Shan 			} else {
15752a485ad7SGavin Shan 				pr_err("EEH: Frozen PE#%x "
15762a485ad7SGavin Shan 				       "on PHB#%x detected\n",
15772a485ad7SGavin Shan 				       (*pe)->addr,
15782a485ad7SGavin Shan 					(*pe)->phb->global_number);
15792a485ad7SGavin Shan 				pr_err("EEH: PE location: %s, "
15802a485ad7SGavin Shan 				       "PHB location: %s\n",
15812a485ad7SGavin Shan 				       eeh_pe_loc_get(*pe),
15822a485ad7SGavin Shan 				       eeh_pe_loc_get(phb_pe));
15832a485ad7SGavin Shan 				ret = EEH_NEXT_ERR_FROZEN_PE;
15842a485ad7SGavin Shan 			}
15852a485ad7SGavin Shan 
15862a485ad7SGavin Shan 			break;
15872a485ad7SGavin Shan 		default:
15882a485ad7SGavin Shan 			pr_warn("%s: Unexpected error type %d\n",
15892a485ad7SGavin Shan 				__func__, be16_to_cpu(err_type));
15902a485ad7SGavin Shan 		}
15912a485ad7SGavin Shan 
15922a485ad7SGavin Shan 		/*
15932a485ad7SGavin Shan 		 * EEH core will try recover from fenced PHB or
15942a485ad7SGavin Shan 		 * frozen PE. In the time for frozen PE, EEH core
15952a485ad7SGavin Shan 		 * enable IO path for that before collecting logs,
15962a485ad7SGavin Shan 		 * but it ruins the site. So we have to dump the
15972a485ad7SGavin Shan 		 * log in advance here.
15982a485ad7SGavin Shan 		 */
15992a485ad7SGavin Shan 		if ((ret == EEH_NEXT_ERR_FROZEN_PE  ||
16002a485ad7SGavin Shan 		    ret == EEH_NEXT_ERR_FENCED_PHB) &&
16012a485ad7SGavin Shan 		    !((*pe)->state & EEH_PE_ISOLATED)) {
1602e762bb89SSam Bobroff 			eeh_pe_mark_isolated(*pe);
16032a485ad7SGavin Shan 			pnv_eeh_get_phb_diag(*pe);
16042a485ad7SGavin Shan 
16052a485ad7SGavin Shan 			if (eeh_has_flag(EEH_EARLY_DUMP_LOG))
16062a485ad7SGavin Shan 				pnv_pci_dump_phb_diag_data((*pe)->phb,
16072a485ad7SGavin Shan 							   (*pe)->data);
16082a485ad7SGavin Shan 		}
16092a485ad7SGavin Shan 
16102a485ad7SGavin Shan 		/*
16112a485ad7SGavin Shan 		 * We probably have the frozen parent PE out there and
16122a485ad7SGavin Shan 		 * we need have to handle frozen parent PE firstly.
16132a485ad7SGavin Shan 		 */
16142a485ad7SGavin Shan 		if (ret == EEH_NEXT_ERR_FROZEN_PE) {
16152a485ad7SGavin Shan 			parent_pe = (*pe)->parent;
16162a485ad7SGavin Shan 			while (parent_pe) {
16172a485ad7SGavin Shan 				/* Hit the ceiling ? */
16182a485ad7SGavin Shan 				if (parent_pe->type & EEH_PE_PHB)
16192a485ad7SGavin Shan 					break;
16202a485ad7SGavin Shan 
16212a485ad7SGavin Shan 				/* Frozen parent PE ? */
16222a485ad7SGavin Shan 				state = eeh_ops->get_state(parent_pe, NULL);
162334a286a4SSam Bobroff 				if (state > 0 && !eeh_state_active(state))
16242a485ad7SGavin Shan 					*pe = parent_pe;
16252a485ad7SGavin Shan 
16262a485ad7SGavin Shan 				/* Next parent level */
16272a485ad7SGavin Shan 				parent_pe = parent_pe->parent;
16282a485ad7SGavin Shan 			}
16292a485ad7SGavin Shan 
16302a485ad7SGavin Shan 			/* We possibly migrate to another PE */
1631e762bb89SSam Bobroff 			eeh_pe_mark_isolated(*pe);
16322a485ad7SGavin Shan 		}
16332a485ad7SGavin Shan 
16342a485ad7SGavin Shan 		/*
16352a485ad7SGavin Shan 		 * If we have no errors on the specific PHB or only
16362a485ad7SGavin Shan 		 * informative error there, we continue poking it.
16372a485ad7SGavin Shan 		 * Otherwise, we need actions to be taken by upper
16382a485ad7SGavin Shan 		 * layer.
16392a485ad7SGavin Shan 		 */
16402a485ad7SGavin Shan 		if (ret > EEH_NEXT_ERR_INF)
164129310e5eSGavin Shan 			break;
164229310e5eSGavin Shan 	}
164329310e5eSGavin Shan 
164479231448SAlistair Popple 	/* Unmask the event */
1645b8d65e96SAlistair Popple 	if (ret == EEH_NEXT_ERR_NONE && eeh_enabled())
164679231448SAlistair Popple 		enable_irq(eeh_event_irq);
164779231448SAlistair Popple 
16482a485ad7SGavin Shan 	return ret;
164929310e5eSGavin Shan }
165029310e5eSGavin Shan 
16510c2c7652SOliver O'Halloran static int pnv_eeh_restore_config(struct eeh_dev *edev)
16529be3beccSGavin Shan {
16539be3beccSGavin Shan 	struct pnv_phb *phb;
165464ba3dc7SBryant G. Ly 	s64 ret = 0;
16559be3beccSGavin Shan 
16569be3beccSGavin Shan 	if (!edev)
16579be3beccSGavin Shan 		return -EEXIST;
16589be3beccSGavin Shan 
165921b43bd5SOliver O'Halloran 	if (edev->physfn)
166021b43bd5SOliver O'Halloran 		return 0;
166121b43bd5SOliver O'Halloran 
166221b43bd5SOliver O'Halloran 	phb = edev->controller->private_data;
16639be3beccSGavin Shan 	ret = opal_pci_reinit(phb->opal_id,
166421b43bd5SOliver O'Halloran 			      OPAL_REINIT_PCI_DEV, edev->bdfn);
16650dc2830eSWei Yang 
16669be3beccSGavin Shan 	if (ret) {
16679be3beccSGavin Shan 		pr_warn("%s: Can't reinit PCI dev 0x%x (%lld)\n",
16680c2c7652SOliver O'Halloran 			__func__, edev->bdfn, ret);
16699be3beccSGavin Shan 		return -EIO;
16709be3beccSGavin Shan 	}
16719be3beccSGavin Shan 
167264ba3dc7SBryant G. Ly 	return ret;
16739be3beccSGavin Shan }
16749be3beccSGavin Shan 
167501f3bfb7SGavin Shan static struct eeh_ops pnv_eeh_ops = {
167629310e5eSGavin Shan 	.name                   = "powernv",
167701f3bfb7SGavin Shan 	.init                   = pnv_eeh_init,
1678ff57b454SGavin Shan 	.probe			= pnv_eeh_probe,
167901f3bfb7SGavin Shan 	.set_option             = pnv_eeh_set_option,
168001f3bfb7SGavin Shan 	.get_state              = pnv_eeh_get_state,
168101f3bfb7SGavin Shan 	.reset                  = pnv_eeh_reset,
168201f3bfb7SGavin Shan 	.get_log                = pnv_eeh_get_log,
168301f3bfb7SGavin Shan 	.configure_bridge       = pnv_eeh_configure_bridge,
168401f3bfb7SGavin Shan 	.err_inject		= pnv_eeh_err_inject,
168501f3bfb7SGavin Shan 	.read_config            = pnv_eeh_read_config,
168601f3bfb7SGavin Shan 	.write_config           = pnv_eeh_write_config,
168701f3bfb7SGavin Shan 	.next_error		= pnv_eeh_next_error,
168867923cfcSBryant G. Ly 	.restore_config		= pnv_eeh_restore_config,
168967923cfcSBryant G. Ly 	.notify_resume		= NULL
169029310e5eSGavin Shan };
169129310e5eSGavin Shan 
16920dc2830eSWei Yang #ifdef CONFIG_PCI_IOV
16930dc2830eSWei Yang static void pnv_pci_fixup_vf_mps(struct pci_dev *pdev)
16940dc2830eSWei Yang {
16950dc2830eSWei Yang 	struct pci_dn *pdn = pci_get_pdn(pdev);
16960dc2830eSWei Yang 	int parent_mps;
16970dc2830eSWei Yang 
16980dc2830eSWei Yang 	if (!pdev->is_virtfn)
16990dc2830eSWei Yang 		return;
17000dc2830eSWei Yang 
17010dc2830eSWei Yang 	/* Synchronize MPS for VF and PF */
17020dc2830eSWei Yang 	parent_mps = pcie_get_mps(pdev->physfn);
17030dc2830eSWei Yang 	if ((128 << pdev->pcie_mpss) >= parent_mps)
17040dc2830eSWei Yang 		pcie_set_mps(pdev, parent_mps);
17050dc2830eSWei Yang 	pdn->mps = pcie_get_mps(pdev);
17060dc2830eSWei Yang }
17070dc2830eSWei Yang DECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pnv_pci_fixup_vf_mps);
17080dc2830eSWei Yang #endif /* CONFIG_PCI_IOV */
17090dc2830eSWei Yang 
171029310e5eSGavin Shan /**
171129310e5eSGavin Shan  * eeh_powernv_init - Register platform dependent EEH operations
171229310e5eSGavin Shan  *
171329310e5eSGavin Shan  * EEH initialization on powernv platform. This function should be
171429310e5eSGavin Shan  * called before any EEH related functions.
171529310e5eSGavin Shan  */
171629310e5eSGavin Shan static int __init eeh_powernv_init(void)
171729310e5eSGavin Shan {
171829310e5eSGavin Shan 	int ret = -EINVAL;
171929310e5eSGavin Shan 
1720d125aedbSOliver O'Halloran 	ret = eeh_init(&pnv_eeh_ops);
172129310e5eSGavin Shan 	if (!ret)
172229310e5eSGavin Shan 		pr_info("EEH: PowerNV platform initialized\n");
172329310e5eSGavin Shan 	else
172429310e5eSGavin Shan 		pr_info("EEH: Failed to initialize PowerNV platform (%d)\n", ret);
172529310e5eSGavin Shan 
172629310e5eSGavin Shan 	return ret;
172729310e5eSGavin Shan }
1728d125aedbSOliver O'Halloran machine_core_initcall_sync(powernv, eeh_powernv_init);
1729