18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * PowerNV Platform dependent EEH operations
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright Benjamin Herrenschmidt & Gavin Shan, IBM Corporation 2013.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/atomic.h>
98c2ecf20Sopenharmony_ci#include <linux/debugfs.h>
108c2ecf20Sopenharmony_ci#include <linux/delay.h>
118c2ecf20Sopenharmony_ci#include <linux/export.h>
128c2ecf20Sopenharmony_ci#include <linux/init.h>
138c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
148c2ecf20Sopenharmony_ci#include <linux/list.h>
158c2ecf20Sopenharmony_ci#include <linux/msi.h>
168c2ecf20Sopenharmony_ci#include <linux/of.h>
178c2ecf20Sopenharmony_ci#include <linux/pci.h>
188c2ecf20Sopenharmony_ci#include <linux/proc_fs.h>
198c2ecf20Sopenharmony_ci#include <linux/rbtree.h>
208c2ecf20Sopenharmony_ci#include <linux/sched.h>
218c2ecf20Sopenharmony_ci#include <linux/seq_file.h>
228c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#include <asm/eeh.h>
258c2ecf20Sopenharmony_ci#include <asm/eeh_event.h>
268c2ecf20Sopenharmony_ci#include <asm/firmware.h>
278c2ecf20Sopenharmony_ci#include <asm/io.h>
288c2ecf20Sopenharmony_ci#include <asm/iommu.h>
298c2ecf20Sopenharmony_ci#include <asm/machdep.h>
308c2ecf20Sopenharmony_ci#include <asm/msi_bitmap.h>
318c2ecf20Sopenharmony_ci#include <asm/opal.h>
328c2ecf20Sopenharmony_ci#include <asm/ppc-pci.h>
338c2ecf20Sopenharmony_ci#include <asm/pnv-pci.h>
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#include "powernv.h"
368c2ecf20Sopenharmony_ci#include "pci.h"
378c2ecf20Sopenharmony_ci#include "../../../../drivers/pci/pci.h"
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistatic int eeh_event_irq = -EINVAL;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistatic void pnv_pcibios_bus_add_device(struct pci_dev *pdev)
428c2ecf20Sopenharmony_ci{
438c2ecf20Sopenharmony_ci	dev_dbg(&pdev->dev, "EEH: Setting up device\n");
448c2ecf20Sopenharmony_ci	eeh_probe_device(pdev);
458c2ecf20Sopenharmony_ci}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistatic irqreturn_t pnv_eeh_event(int irq, void *data)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	/*
508c2ecf20Sopenharmony_ci	 * We simply send a special EEH event if EEH has been
518c2ecf20Sopenharmony_ci	 * enabled. We don't care about EEH events until we've
528c2ecf20Sopenharmony_ci	 * finished processing the outstanding ones. Event processing
538c2ecf20Sopenharmony_ci	 * gets unmasked in next_error() if EEH is enabled.
548c2ecf20Sopenharmony_ci	 */
558c2ecf20Sopenharmony_ci	disable_irq_nosync(irq);
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	if (eeh_enabled())
588c2ecf20Sopenharmony_ci		eeh_send_failure_event(NULL);
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci#ifdef CONFIG_DEBUG_FS
648c2ecf20Sopenharmony_cistatic ssize_t pnv_eeh_ei_write(struct file *filp,
658c2ecf20Sopenharmony_ci				const char __user *user_buf,
668c2ecf20Sopenharmony_ci				size_t count, loff_t *ppos)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci	struct pci_controller *hose = filp->private_data;
698c2ecf20Sopenharmony_ci	struct eeh_pe *pe;
708c2ecf20Sopenharmony_ci	int pe_no, type, func;
718c2ecf20Sopenharmony_ci	unsigned long addr, mask;
728c2ecf20Sopenharmony_ci	char buf[50];
738c2ecf20Sopenharmony_ci	int ret;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	if (!eeh_ops || !eeh_ops->err_inject)
768c2ecf20Sopenharmony_ci		return -ENXIO;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	/* Copy over argument buffer */
798c2ecf20Sopenharmony_ci	ret = simple_write_to_buffer(buf, sizeof(buf), ppos, user_buf, count);
808c2ecf20Sopenharmony_ci	if (!ret)
818c2ecf20Sopenharmony_ci		return -EFAULT;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	/* Retrieve parameters */
848c2ecf20Sopenharmony_ci	ret = sscanf(buf, "%x:%x:%x:%lx:%lx",
858c2ecf20Sopenharmony_ci		     &pe_no, &type, &func, &addr, &mask);
868c2ecf20Sopenharmony_ci	if (ret != 5)
878c2ecf20Sopenharmony_ci		return -EINVAL;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	/* Retrieve PE */
908c2ecf20Sopenharmony_ci	pe = eeh_pe_get(hose, pe_no);
918c2ecf20Sopenharmony_ci	if (!pe)
928c2ecf20Sopenharmony_ci		return -ENODEV;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	/* Do error injection */
958c2ecf20Sopenharmony_ci	ret = eeh_ops->err_inject(pe, type, func, addr, mask);
968c2ecf20Sopenharmony_ci	return ret < 0 ? ret : count;
978c2ecf20Sopenharmony_ci}
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_cistatic const struct file_operations pnv_eeh_ei_fops = {
1008c2ecf20Sopenharmony_ci	.open	= simple_open,
1018c2ecf20Sopenharmony_ci	.llseek	= no_llseek,
1028c2ecf20Sopenharmony_ci	.write	= pnv_eeh_ei_write,
1038c2ecf20Sopenharmony_ci};
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_cistatic int pnv_eeh_dbgfs_set(void *data, int offset, u64 val)
1068c2ecf20Sopenharmony_ci{
1078c2ecf20Sopenharmony_ci	struct pci_controller *hose = data;
1088c2ecf20Sopenharmony_ci	struct pnv_phb *phb = hose->private_data;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	out_be64(phb->regs + offset, val);
1118c2ecf20Sopenharmony_ci	return 0;
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic int pnv_eeh_dbgfs_get(void *data, int offset, u64 *val)
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	struct pci_controller *hose = data;
1178c2ecf20Sopenharmony_ci	struct pnv_phb *phb = hose->private_data;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	*val = in_be64(phb->regs + offset);
1208c2ecf20Sopenharmony_ci	return 0;
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci#define PNV_EEH_DBGFS_ENTRY(name, reg)				\
1248c2ecf20Sopenharmony_cistatic int pnv_eeh_dbgfs_set_##name(void *data, u64 val)	\
1258c2ecf20Sopenharmony_ci{								\
1268c2ecf20Sopenharmony_ci	return pnv_eeh_dbgfs_set(data, reg, val);		\
1278c2ecf20Sopenharmony_ci}								\
1288c2ecf20Sopenharmony_ci								\
1298c2ecf20Sopenharmony_cistatic int pnv_eeh_dbgfs_get_##name(void *data, u64 *val)	\
1308c2ecf20Sopenharmony_ci{								\
1318c2ecf20Sopenharmony_ci	return pnv_eeh_dbgfs_get(data, reg, val);		\
1328c2ecf20Sopenharmony_ci}								\
1338c2ecf20Sopenharmony_ci								\
1348c2ecf20Sopenharmony_ciDEFINE_SIMPLE_ATTRIBUTE(pnv_eeh_dbgfs_ops_##name,		\
1358c2ecf20Sopenharmony_ci			pnv_eeh_dbgfs_get_##name,		\
1368c2ecf20Sopenharmony_ci                        pnv_eeh_dbgfs_set_##name,		\
1378c2ecf20Sopenharmony_ci			"0x%llx\n")
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ciPNV_EEH_DBGFS_ENTRY(outb, 0xD10);
1408c2ecf20Sopenharmony_ciPNV_EEH_DBGFS_ENTRY(inbA, 0xD90);
1418c2ecf20Sopenharmony_ciPNV_EEH_DBGFS_ENTRY(inbB, 0xE10);
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci#endif /* CONFIG_DEBUG_FS */
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_cistatic void pnv_eeh_enable_phbs(void)
1468c2ecf20Sopenharmony_ci{
1478c2ecf20Sopenharmony_ci	struct pci_controller *hose;
1488c2ecf20Sopenharmony_ci	struct pnv_phb *phb;
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	list_for_each_entry(hose, &hose_list, list_node) {
1518c2ecf20Sopenharmony_ci		phb = hose->private_data;
1528c2ecf20Sopenharmony_ci		/*
1538c2ecf20Sopenharmony_ci		 * If EEH is enabled, we're going to rely on that.
1548c2ecf20Sopenharmony_ci		 * Otherwise, we restore to conventional mechanism
1558c2ecf20Sopenharmony_ci		 * to clear frozen PE during PCI config access.
1568c2ecf20Sopenharmony_ci		 */
1578c2ecf20Sopenharmony_ci		if (eeh_enabled())
1588c2ecf20Sopenharmony_ci			phb->flags |= PNV_PHB_FLAG_EEH;
1598c2ecf20Sopenharmony_ci		else
1608c2ecf20Sopenharmony_ci			phb->flags &= ~PNV_PHB_FLAG_EEH;
1618c2ecf20Sopenharmony_ci	}
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci/**
1658c2ecf20Sopenharmony_ci * pnv_eeh_post_init - EEH platform dependent post initialization
1668c2ecf20Sopenharmony_ci *
1678c2ecf20Sopenharmony_ci * EEH platform dependent post initialization on powernv. When
1688c2ecf20Sopenharmony_ci * the function is called, the EEH PEs and devices should have
1698c2ecf20Sopenharmony_ci * been built. If the I/O cache staff has been built, EEH is
1708c2ecf20Sopenharmony_ci * ready to supply service.
1718c2ecf20Sopenharmony_ci */
1728c2ecf20Sopenharmony_ciint pnv_eeh_post_init(void)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	struct pci_controller *hose;
1758c2ecf20Sopenharmony_ci	struct pnv_phb *phb;
1768c2ecf20Sopenharmony_ci	int ret = 0;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	eeh_show_enabled();
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	/* Register OPAL event notifier */
1818c2ecf20Sopenharmony_ci	eeh_event_irq = opal_event_request(ilog2(OPAL_EVENT_PCI_ERROR));
1828c2ecf20Sopenharmony_ci	if (eeh_event_irq < 0) {
1838c2ecf20Sopenharmony_ci		pr_err("%s: Can't register OPAL event interrupt (%d)\n",
1848c2ecf20Sopenharmony_ci		       __func__, eeh_event_irq);
1858c2ecf20Sopenharmony_ci		return eeh_event_irq;
1868c2ecf20Sopenharmony_ci	}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	ret = request_irq(eeh_event_irq, pnv_eeh_event,
1898c2ecf20Sopenharmony_ci			  IRQ_TYPE_LEVEL_HIGH, "opal-eeh", NULL);
1908c2ecf20Sopenharmony_ci	if (ret < 0) {
1918c2ecf20Sopenharmony_ci		irq_dispose_mapping(eeh_event_irq);
1928c2ecf20Sopenharmony_ci		pr_err("%s: Can't request OPAL event interrupt (%d)\n",
1938c2ecf20Sopenharmony_ci		       __func__, eeh_event_irq);
1948c2ecf20Sopenharmony_ci		return ret;
1958c2ecf20Sopenharmony_ci	}
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	if (!eeh_enabled())
1988c2ecf20Sopenharmony_ci		disable_irq(eeh_event_irq);
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	pnv_eeh_enable_phbs();
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	list_for_each_entry(hose, &hose_list, list_node) {
2038c2ecf20Sopenharmony_ci		phb = hose->private_data;
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci		/* Create debugfs entries */
2068c2ecf20Sopenharmony_ci#ifdef CONFIG_DEBUG_FS
2078c2ecf20Sopenharmony_ci		if (phb->has_dbgfs || !phb->dbgfs)
2088c2ecf20Sopenharmony_ci			continue;
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci		phb->has_dbgfs = 1;
2118c2ecf20Sopenharmony_ci		debugfs_create_file("err_injct", 0200,
2128c2ecf20Sopenharmony_ci				    phb->dbgfs, hose,
2138c2ecf20Sopenharmony_ci				    &pnv_eeh_ei_fops);
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci		debugfs_create_file("err_injct_outbound", 0600,
2168c2ecf20Sopenharmony_ci				    phb->dbgfs, hose,
2178c2ecf20Sopenharmony_ci				    &pnv_eeh_dbgfs_ops_outb);
2188c2ecf20Sopenharmony_ci		debugfs_create_file("err_injct_inboundA", 0600,
2198c2ecf20Sopenharmony_ci				    phb->dbgfs, hose,
2208c2ecf20Sopenharmony_ci				    &pnv_eeh_dbgfs_ops_inbA);
2218c2ecf20Sopenharmony_ci		debugfs_create_file("err_injct_inboundB", 0600,
2228c2ecf20Sopenharmony_ci				    phb->dbgfs, hose,
2238c2ecf20Sopenharmony_ci				    &pnv_eeh_dbgfs_ops_inbB);
2248c2ecf20Sopenharmony_ci#endif /* CONFIG_DEBUG_FS */
2258c2ecf20Sopenharmony_ci	}
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	return ret;
2288c2ecf20Sopenharmony_ci}
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_cistatic int pnv_eeh_find_cap(struct pci_dn *pdn, int cap)
2318c2ecf20Sopenharmony_ci{
2328c2ecf20Sopenharmony_ci	int pos = PCI_CAPABILITY_LIST;
2338c2ecf20Sopenharmony_ci	int cnt = 48;   /* Maximal number of capabilities */
2348c2ecf20Sopenharmony_ci	u32 status, id;
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	if (!pdn)
2378c2ecf20Sopenharmony_ci		return 0;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	/* Check if the device supports capabilities */
2408c2ecf20Sopenharmony_ci	pnv_pci_cfg_read(pdn, PCI_STATUS, 2, &status);
2418c2ecf20Sopenharmony_ci	if (!(status & PCI_STATUS_CAP_LIST))
2428c2ecf20Sopenharmony_ci		return 0;
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	while (cnt--) {
2458c2ecf20Sopenharmony_ci		pnv_pci_cfg_read(pdn, pos, 1, &pos);
2468c2ecf20Sopenharmony_ci		if (pos < 0x40)
2478c2ecf20Sopenharmony_ci			break;
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci		pos &= ~3;
2508c2ecf20Sopenharmony_ci		pnv_pci_cfg_read(pdn, pos + PCI_CAP_LIST_ID, 1, &id);
2518c2ecf20Sopenharmony_ci		if (id == 0xff)
2528c2ecf20Sopenharmony_ci			break;
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci		/* Found */
2558c2ecf20Sopenharmony_ci		if (id == cap)
2568c2ecf20Sopenharmony_ci			return pos;
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci		/* Next one */
2598c2ecf20Sopenharmony_ci		pos += PCI_CAP_LIST_NEXT;
2608c2ecf20Sopenharmony_ci	}
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	return 0;
2638c2ecf20Sopenharmony_ci}
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_cistatic int pnv_eeh_find_ecap(struct pci_dn *pdn, int cap)
2668c2ecf20Sopenharmony_ci{
2678c2ecf20Sopenharmony_ci	struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
2688c2ecf20Sopenharmony_ci	u32 header;
2698c2ecf20Sopenharmony_ci	int pos = 256, ttl = (4096 - 256) / 8;
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci	if (!edev || !edev->pcie_cap)
2728c2ecf20Sopenharmony_ci		return 0;
2738c2ecf20Sopenharmony_ci	if (pnv_pci_cfg_read(pdn, pos, 4, &header) != PCIBIOS_SUCCESSFUL)
2748c2ecf20Sopenharmony_ci		return 0;
2758c2ecf20Sopenharmony_ci	else if (!header)
2768c2ecf20Sopenharmony_ci		return 0;
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	while (ttl-- > 0) {
2798c2ecf20Sopenharmony_ci		if (PCI_EXT_CAP_ID(header) == cap && pos)
2808c2ecf20Sopenharmony_ci			return pos;
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci		pos = PCI_EXT_CAP_NEXT(header);
2838c2ecf20Sopenharmony_ci		if (pos < 256)
2848c2ecf20Sopenharmony_ci			break;
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci		if (pnv_pci_cfg_read(pdn, pos, 4, &header) != PCIBIOS_SUCCESSFUL)
2878c2ecf20Sopenharmony_ci			break;
2888c2ecf20Sopenharmony_ci	}
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	return 0;
2918c2ecf20Sopenharmony_ci}
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_cistatic struct eeh_pe *pnv_eeh_get_upstream_pe(struct pci_dev *pdev)
2948c2ecf20Sopenharmony_ci{
2958c2ecf20Sopenharmony_ci	struct pci_controller *hose = pdev->bus->sysdata;
2968c2ecf20Sopenharmony_ci	struct pnv_phb *phb = hose->private_data;
2978c2ecf20Sopenharmony_ci	struct pci_dev *parent = pdev->bus->self;
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci#ifdef CONFIG_PCI_IOV
3008c2ecf20Sopenharmony_ci	/* for VFs we use the PF's PE as the upstream PE */
3018c2ecf20Sopenharmony_ci	if (pdev->is_virtfn)
3028c2ecf20Sopenharmony_ci		parent = pdev->physfn;
3038c2ecf20Sopenharmony_ci#endif
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	/* otherwise use the PE of our parent bridge */
3068c2ecf20Sopenharmony_ci	if (parent) {
3078c2ecf20Sopenharmony_ci		struct pnv_ioda_pe *ioda_pe = pnv_ioda_get_pe(parent);
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci		return eeh_pe_get(phb->hose, ioda_pe->pe_number);
3108c2ecf20Sopenharmony_ci	}
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	return NULL;
3138c2ecf20Sopenharmony_ci}
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci/**
3168c2ecf20Sopenharmony_ci * pnv_eeh_probe - Do probe on PCI device
3178c2ecf20Sopenharmony_ci * @pdev: pci_dev to probe
3188c2ecf20Sopenharmony_ci *
3198c2ecf20Sopenharmony_ci * Create, or find the existing, eeh_dev for this pci_dev.
3208c2ecf20Sopenharmony_ci */
3218c2ecf20Sopenharmony_cistatic struct eeh_dev *pnv_eeh_probe(struct pci_dev *pdev)
3228c2ecf20Sopenharmony_ci{
3238c2ecf20Sopenharmony_ci	struct pci_dn *pdn = pci_get_pdn(pdev);
3248c2ecf20Sopenharmony_ci	struct pci_controller *hose = pdn->phb;
3258c2ecf20Sopenharmony_ci	struct pnv_phb *phb = hose->private_data;
3268c2ecf20Sopenharmony_ci	struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
3278c2ecf20Sopenharmony_ci	struct eeh_pe *upstream_pe;
3288c2ecf20Sopenharmony_ci	uint32_t pcie_flags;
3298c2ecf20Sopenharmony_ci	int ret;
3308c2ecf20Sopenharmony_ci	int config_addr = (pdn->busno << 8) | (pdn->devfn);
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	/*
3338c2ecf20Sopenharmony_ci	 * When probing the root bridge, which doesn't have any
3348c2ecf20Sopenharmony_ci	 * subordinate PCI devices. We don't have OF node for
3358c2ecf20Sopenharmony_ci	 * the root bridge. So it's not reasonable to continue
3368c2ecf20Sopenharmony_ci	 * the probing.
3378c2ecf20Sopenharmony_ci	 */
3388c2ecf20Sopenharmony_ci	if (!edev || edev->pe)
3398c2ecf20Sopenharmony_ci		return NULL;
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	/* already configured? */
3428c2ecf20Sopenharmony_ci	if (edev->pdev) {
3438c2ecf20Sopenharmony_ci		pr_debug("%s: found existing edev for %04x:%02x:%02x.%01x\n",
3448c2ecf20Sopenharmony_ci			__func__, hose->global_number, config_addr >> 8,
3458c2ecf20Sopenharmony_ci			PCI_SLOT(config_addr), PCI_FUNC(config_addr));
3468c2ecf20Sopenharmony_ci		return edev;
3478c2ecf20Sopenharmony_ci	}
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	/* Skip for PCI-ISA bridge */
3508c2ecf20Sopenharmony_ci	if ((pdev->class >> 8) == PCI_CLASS_BRIDGE_ISA)
3518c2ecf20Sopenharmony_ci		return NULL;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	eeh_edev_dbg(edev, "Probing device\n");
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	/* Initialize eeh device */
3568c2ecf20Sopenharmony_ci	edev->mode	&= 0xFFFFFF00;
3578c2ecf20Sopenharmony_ci	edev->pcix_cap = pnv_eeh_find_cap(pdn, PCI_CAP_ID_PCIX);
3588c2ecf20Sopenharmony_ci	edev->pcie_cap = pnv_eeh_find_cap(pdn, PCI_CAP_ID_EXP);
3598c2ecf20Sopenharmony_ci	edev->af_cap   = pnv_eeh_find_cap(pdn, PCI_CAP_ID_AF);
3608c2ecf20Sopenharmony_ci	edev->aer_cap  = pnv_eeh_find_ecap(pdn, PCI_EXT_CAP_ID_ERR);
3618c2ecf20Sopenharmony_ci	if ((pdev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
3628c2ecf20Sopenharmony_ci		edev->mode |= EEH_DEV_BRIDGE;
3638c2ecf20Sopenharmony_ci		if (edev->pcie_cap) {
3648c2ecf20Sopenharmony_ci			pnv_pci_cfg_read(pdn, edev->pcie_cap + PCI_EXP_FLAGS,
3658c2ecf20Sopenharmony_ci					 2, &pcie_flags);
3668c2ecf20Sopenharmony_ci			pcie_flags = (pcie_flags & PCI_EXP_FLAGS_TYPE) >> 4;
3678c2ecf20Sopenharmony_ci			if (pcie_flags == PCI_EXP_TYPE_ROOT_PORT)
3688c2ecf20Sopenharmony_ci				edev->mode |= EEH_DEV_ROOT_PORT;
3698c2ecf20Sopenharmony_ci			else if (pcie_flags == PCI_EXP_TYPE_DOWNSTREAM)
3708c2ecf20Sopenharmony_ci				edev->mode |= EEH_DEV_DS_PORT;
3718c2ecf20Sopenharmony_ci		}
3728c2ecf20Sopenharmony_ci	}
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	edev->pe_config_addr = phb->ioda.pe_rmap[config_addr];
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	upstream_pe = pnv_eeh_get_upstream_pe(pdev);
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	/* Create PE */
3798c2ecf20Sopenharmony_ci	ret = eeh_pe_tree_insert(edev, upstream_pe);
3808c2ecf20Sopenharmony_ci	if (ret) {
3818c2ecf20Sopenharmony_ci		eeh_edev_warn(edev, "Failed to add device to PE (code %d)\n", ret);
3828c2ecf20Sopenharmony_ci		return NULL;
3838c2ecf20Sopenharmony_ci	}
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	/*
3868c2ecf20Sopenharmony_ci	 * If the PE contains any one of following adapters, the
3878c2ecf20Sopenharmony_ci	 * PCI config space can't be accessed when dumping EEH log.
3888c2ecf20Sopenharmony_ci	 * Otherwise, we will run into fenced PHB caused by shortage
3898c2ecf20Sopenharmony_ci	 * of outbound credits in the adapter. The PCI config access
3908c2ecf20Sopenharmony_ci	 * should be blocked until PE reset. MMIO access is dropped
3918c2ecf20Sopenharmony_ci	 * by hardware certainly. In order to drop PCI config requests,
3928c2ecf20Sopenharmony_ci	 * one more flag (EEH_PE_CFG_RESTRICTED) is introduced, which
3938c2ecf20Sopenharmony_ci	 * will be checked in the backend for PE state retrival. If
3948c2ecf20Sopenharmony_ci	 * the PE becomes frozen for the first time and the flag has
3958c2ecf20Sopenharmony_ci	 * been set for the PE, we will set EEH_PE_CFG_BLOCKED for
3968c2ecf20Sopenharmony_ci	 * that PE to block its config space.
3978c2ecf20Sopenharmony_ci	 *
3988c2ecf20Sopenharmony_ci	 * Broadcom BCM5718 2-ports NICs (14e4:1656)
3998c2ecf20Sopenharmony_ci	 * Broadcom Austin 4-ports NICs (14e4:1657)
4008c2ecf20Sopenharmony_ci	 * Broadcom Shiner 4-ports 1G NICs (14e4:168a)
4018c2ecf20Sopenharmony_ci	 * Broadcom Shiner 2-ports 10G NICs (14e4:168e)
4028c2ecf20Sopenharmony_ci	 */
4038c2ecf20Sopenharmony_ci	if ((pdn->vendor_id == PCI_VENDOR_ID_BROADCOM &&
4048c2ecf20Sopenharmony_ci	     pdn->device_id == 0x1656) ||
4058c2ecf20Sopenharmony_ci	    (pdn->vendor_id == PCI_VENDOR_ID_BROADCOM &&
4068c2ecf20Sopenharmony_ci	     pdn->device_id == 0x1657) ||
4078c2ecf20Sopenharmony_ci	    (pdn->vendor_id == PCI_VENDOR_ID_BROADCOM &&
4088c2ecf20Sopenharmony_ci	     pdn->device_id == 0x168a) ||
4098c2ecf20Sopenharmony_ci	    (pdn->vendor_id == PCI_VENDOR_ID_BROADCOM &&
4108c2ecf20Sopenharmony_ci	     pdn->device_id == 0x168e))
4118c2ecf20Sopenharmony_ci		edev->pe->state |= EEH_PE_CFG_RESTRICTED;
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	/*
4148c2ecf20Sopenharmony_ci	 * Cache the PE primary bus, which can't be fetched when
4158c2ecf20Sopenharmony_ci	 * full hotplug is in progress. In that case, all child
4168c2ecf20Sopenharmony_ci	 * PCI devices of the PE are expected to be removed prior
4178c2ecf20Sopenharmony_ci	 * to PE reset.
4188c2ecf20Sopenharmony_ci	 */
4198c2ecf20Sopenharmony_ci	if (!(edev->pe->state & EEH_PE_PRI_BUS)) {
4208c2ecf20Sopenharmony_ci		edev->pe->bus = pci_find_bus(hose->global_number,
4218c2ecf20Sopenharmony_ci					     pdn->busno);
4228c2ecf20Sopenharmony_ci		if (edev->pe->bus)
4238c2ecf20Sopenharmony_ci			edev->pe->state |= EEH_PE_PRI_BUS;
4248c2ecf20Sopenharmony_ci	}
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	/*
4278c2ecf20Sopenharmony_ci	 * Enable EEH explicitly so that we will do EEH check
4288c2ecf20Sopenharmony_ci	 * while accessing I/O stuff
4298c2ecf20Sopenharmony_ci	 */
4308c2ecf20Sopenharmony_ci	if (!eeh_has_flag(EEH_ENABLED)) {
4318c2ecf20Sopenharmony_ci		enable_irq(eeh_event_irq);
4328c2ecf20Sopenharmony_ci		pnv_eeh_enable_phbs();
4338c2ecf20Sopenharmony_ci		eeh_add_flag(EEH_ENABLED);
4348c2ecf20Sopenharmony_ci	}
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci	/* Save memory bars */
4378c2ecf20Sopenharmony_ci	eeh_save_bars(edev);
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci	eeh_edev_dbg(edev, "EEH enabled on device\n");
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci	return edev;
4428c2ecf20Sopenharmony_ci}
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci/**
4458c2ecf20Sopenharmony_ci * pnv_eeh_set_option - Initialize EEH or MMIO/DMA reenable
4468c2ecf20Sopenharmony_ci * @pe: EEH PE
4478c2ecf20Sopenharmony_ci * @option: operation to be issued
4488c2ecf20Sopenharmony_ci *
4498c2ecf20Sopenharmony_ci * The function is used to control the EEH functionality globally.
4508c2ecf20Sopenharmony_ci * Currently, following options are support according to PAPR:
4518c2ecf20Sopenharmony_ci * Enable EEH, Disable EEH, Enable MMIO and Enable DMA
4528c2ecf20Sopenharmony_ci */
4538c2ecf20Sopenharmony_cistatic int pnv_eeh_set_option(struct eeh_pe *pe, int option)
4548c2ecf20Sopenharmony_ci{
4558c2ecf20Sopenharmony_ci	struct pci_controller *hose = pe->phb;
4568c2ecf20Sopenharmony_ci	struct pnv_phb *phb = hose->private_data;
4578c2ecf20Sopenharmony_ci	bool freeze_pe = false;
4588c2ecf20Sopenharmony_ci	int opt;
4598c2ecf20Sopenharmony_ci	s64 rc;
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_ci	switch (option) {
4628c2ecf20Sopenharmony_ci	case EEH_OPT_DISABLE:
4638c2ecf20Sopenharmony_ci		return -EPERM;
4648c2ecf20Sopenharmony_ci	case EEH_OPT_ENABLE:
4658c2ecf20Sopenharmony_ci		return 0;
4668c2ecf20Sopenharmony_ci	case EEH_OPT_THAW_MMIO:
4678c2ecf20Sopenharmony_ci		opt = OPAL_EEH_ACTION_CLEAR_FREEZE_MMIO;
4688c2ecf20Sopenharmony_ci		break;
4698c2ecf20Sopenharmony_ci	case EEH_OPT_THAW_DMA:
4708c2ecf20Sopenharmony_ci		opt = OPAL_EEH_ACTION_CLEAR_FREEZE_DMA;
4718c2ecf20Sopenharmony_ci		break;
4728c2ecf20Sopenharmony_ci	case EEH_OPT_FREEZE_PE:
4738c2ecf20Sopenharmony_ci		freeze_pe = true;
4748c2ecf20Sopenharmony_ci		opt = OPAL_EEH_ACTION_SET_FREEZE_ALL;
4758c2ecf20Sopenharmony_ci		break;
4768c2ecf20Sopenharmony_ci	default:
4778c2ecf20Sopenharmony_ci		pr_warn("%s: Invalid option %d\n", __func__, option);
4788c2ecf20Sopenharmony_ci		return -EINVAL;
4798c2ecf20Sopenharmony_ci	}
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci	/* Freeze master and slave PEs if PHB supports compound PEs */
4828c2ecf20Sopenharmony_ci	if (freeze_pe) {
4838c2ecf20Sopenharmony_ci		if (phb->freeze_pe) {
4848c2ecf20Sopenharmony_ci			phb->freeze_pe(phb, pe->addr);
4858c2ecf20Sopenharmony_ci			return 0;
4868c2ecf20Sopenharmony_ci		}
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci		rc = opal_pci_eeh_freeze_set(phb->opal_id, pe->addr, opt);
4898c2ecf20Sopenharmony_ci		if (rc != OPAL_SUCCESS) {
4908c2ecf20Sopenharmony_ci			pr_warn("%s: Failure %lld freezing PHB#%x-PE#%x\n",
4918c2ecf20Sopenharmony_ci				__func__, rc, phb->hose->global_number,
4928c2ecf20Sopenharmony_ci				pe->addr);
4938c2ecf20Sopenharmony_ci			return -EIO;
4948c2ecf20Sopenharmony_ci		}
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci		return 0;
4978c2ecf20Sopenharmony_ci	}
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci	/* Unfreeze master and slave PEs if PHB supports */
5008c2ecf20Sopenharmony_ci	if (phb->unfreeze_pe)
5018c2ecf20Sopenharmony_ci		return phb->unfreeze_pe(phb, pe->addr, opt);
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	rc = opal_pci_eeh_freeze_clear(phb->opal_id, pe->addr, opt);
5048c2ecf20Sopenharmony_ci	if (rc != OPAL_SUCCESS) {
5058c2ecf20Sopenharmony_ci		pr_warn("%s: Failure %lld enable %d for PHB#%x-PE#%x\n",
5068c2ecf20Sopenharmony_ci			__func__, rc, option, phb->hose->global_number,
5078c2ecf20Sopenharmony_ci			pe->addr);
5088c2ecf20Sopenharmony_ci		return -EIO;
5098c2ecf20Sopenharmony_ci	}
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci	return 0;
5128c2ecf20Sopenharmony_ci}
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_cistatic void pnv_eeh_get_phb_diag(struct eeh_pe *pe)
5158c2ecf20Sopenharmony_ci{
5168c2ecf20Sopenharmony_ci	struct pnv_phb *phb = pe->phb->private_data;
5178c2ecf20Sopenharmony_ci	s64 rc;
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci	rc = opal_pci_get_phb_diag_data2(phb->opal_id, pe->data,
5208c2ecf20Sopenharmony_ci					 phb->diag_data_size);
5218c2ecf20Sopenharmony_ci	if (rc != OPAL_SUCCESS)
5228c2ecf20Sopenharmony_ci		pr_warn("%s: Failure %lld getting PHB#%x diag-data\n",
5238c2ecf20Sopenharmony_ci			__func__, rc, pe->phb->global_number);
5248c2ecf20Sopenharmony_ci}
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_cistatic int pnv_eeh_get_phb_state(struct eeh_pe *pe)
5278c2ecf20Sopenharmony_ci{
5288c2ecf20Sopenharmony_ci	struct pnv_phb *phb = pe->phb->private_data;
5298c2ecf20Sopenharmony_ci	u8 fstate = 0;
5308c2ecf20Sopenharmony_ci	__be16 pcierr = 0;
5318c2ecf20Sopenharmony_ci	s64 rc;
5328c2ecf20Sopenharmony_ci	int result = 0;
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci	rc = opal_pci_eeh_freeze_status(phb->opal_id,
5358c2ecf20Sopenharmony_ci					pe->addr,
5368c2ecf20Sopenharmony_ci					&fstate,
5378c2ecf20Sopenharmony_ci					&pcierr,
5388c2ecf20Sopenharmony_ci					NULL);
5398c2ecf20Sopenharmony_ci	if (rc != OPAL_SUCCESS) {
5408c2ecf20Sopenharmony_ci		pr_warn("%s: Failure %lld getting PHB#%x state\n",
5418c2ecf20Sopenharmony_ci			__func__, rc, phb->hose->global_number);
5428c2ecf20Sopenharmony_ci		return EEH_STATE_NOT_SUPPORT;
5438c2ecf20Sopenharmony_ci	}
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci	/*
5468c2ecf20Sopenharmony_ci	 * Check PHB state. If the PHB is frozen for the
5478c2ecf20Sopenharmony_ci	 * first time, to dump the PHB diag-data.
5488c2ecf20Sopenharmony_ci	 */
5498c2ecf20Sopenharmony_ci	if (be16_to_cpu(pcierr) != OPAL_EEH_PHB_ERROR) {
5508c2ecf20Sopenharmony_ci		result = (EEH_STATE_MMIO_ACTIVE  |
5518c2ecf20Sopenharmony_ci			  EEH_STATE_DMA_ACTIVE   |
5528c2ecf20Sopenharmony_ci			  EEH_STATE_MMIO_ENABLED |
5538c2ecf20Sopenharmony_ci			  EEH_STATE_DMA_ENABLED);
5548c2ecf20Sopenharmony_ci	} else if (!(pe->state & EEH_PE_ISOLATED)) {
5558c2ecf20Sopenharmony_ci		eeh_pe_mark_isolated(pe);
5568c2ecf20Sopenharmony_ci		pnv_eeh_get_phb_diag(pe);
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci		if (eeh_has_flag(EEH_EARLY_DUMP_LOG))
5598c2ecf20Sopenharmony_ci			pnv_pci_dump_phb_diag_data(pe->phb, pe->data);
5608c2ecf20Sopenharmony_ci	}
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci	return result;
5638c2ecf20Sopenharmony_ci}
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_cistatic int pnv_eeh_get_pe_state(struct eeh_pe *pe)
5668c2ecf20Sopenharmony_ci{
5678c2ecf20Sopenharmony_ci	struct pnv_phb *phb = pe->phb->private_data;
5688c2ecf20Sopenharmony_ci	u8 fstate = 0;
5698c2ecf20Sopenharmony_ci	__be16 pcierr = 0;
5708c2ecf20Sopenharmony_ci	s64 rc;
5718c2ecf20Sopenharmony_ci	int result;
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci	/*
5748c2ecf20Sopenharmony_ci	 * We don't clobber hardware frozen state until PE
5758c2ecf20Sopenharmony_ci	 * reset is completed. In order to keep EEH core
5768c2ecf20Sopenharmony_ci	 * moving forward, we have to return operational
5778c2ecf20Sopenharmony_ci	 * state during PE reset.
5788c2ecf20Sopenharmony_ci	 */
5798c2ecf20Sopenharmony_ci	if (pe->state & EEH_PE_RESET) {
5808c2ecf20Sopenharmony_ci		result = (EEH_STATE_MMIO_ACTIVE  |
5818c2ecf20Sopenharmony_ci			  EEH_STATE_DMA_ACTIVE   |
5828c2ecf20Sopenharmony_ci			  EEH_STATE_MMIO_ENABLED |
5838c2ecf20Sopenharmony_ci			  EEH_STATE_DMA_ENABLED);
5848c2ecf20Sopenharmony_ci		return result;
5858c2ecf20Sopenharmony_ci	}
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci	/*
5888c2ecf20Sopenharmony_ci	 * Fetch PE state from hardware. If the PHB
5898c2ecf20Sopenharmony_ci	 * supports compound PE, let it handle that.
5908c2ecf20Sopenharmony_ci	 */
5918c2ecf20Sopenharmony_ci	if (phb->get_pe_state) {
5928c2ecf20Sopenharmony_ci		fstate = phb->get_pe_state(phb, pe->addr);
5938c2ecf20Sopenharmony_ci	} else {
5948c2ecf20Sopenharmony_ci		rc = opal_pci_eeh_freeze_status(phb->opal_id,
5958c2ecf20Sopenharmony_ci						pe->addr,
5968c2ecf20Sopenharmony_ci						&fstate,
5978c2ecf20Sopenharmony_ci						&pcierr,
5988c2ecf20Sopenharmony_ci						NULL);
5998c2ecf20Sopenharmony_ci		if (rc != OPAL_SUCCESS) {
6008c2ecf20Sopenharmony_ci			pr_warn("%s: Failure %lld getting PHB#%x-PE%x state\n",
6018c2ecf20Sopenharmony_ci				__func__, rc, phb->hose->global_number,
6028c2ecf20Sopenharmony_ci				pe->addr);
6038c2ecf20Sopenharmony_ci			return EEH_STATE_NOT_SUPPORT;
6048c2ecf20Sopenharmony_ci		}
6058c2ecf20Sopenharmony_ci	}
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci	/* Figure out state */
6088c2ecf20Sopenharmony_ci	switch (fstate) {
6098c2ecf20Sopenharmony_ci	case OPAL_EEH_STOPPED_NOT_FROZEN:
6108c2ecf20Sopenharmony_ci		result = (EEH_STATE_MMIO_ACTIVE  |
6118c2ecf20Sopenharmony_ci			  EEH_STATE_DMA_ACTIVE   |
6128c2ecf20Sopenharmony_ci			  EEH_STATE_MMIO_ENABLED |
6138c2ecf20Sopenharmony_ci			  EEH_STATE_DMA_ENABLED);
6148c2ecf20Sopenharmony_ci		break;
6158c2ecf20Sopenharmony_ci	case OPAL_EEH_STOPPED_MMIO_FREEZE:
6168c2ecf20Sopenharmony_ci		result = (EEH_STATE_DMA_ACTIVE |
6178c2ecf20Sopenharmony_ci			  EEH_STATE_DMA_ENABLED);
6188c2ecf20Sopenharmony_ci		break;
6198c2ecf20Sopenharmony_ci	case OPAL_EEH_STOPPED_DMA_FREEZE:
6208c2ecf20Sopenharmony_ci		result = (EEH_STATE_MMIO_ACTIVE |
6218c2ecf20Sopenharmony_ci			  EEH_STATE_MMIO_ENABLED);
6228c2ecf20Sopenharmony_ci		break;
6238c2ecf20Sopenharmony_ci	case OPAL_EEH_STOPPED_MMIO_DMA_FREEZE:
6248c2ecf20Sopenharmony_ci		result = 0;
6258c2ecf20Sopenharmony_ci		break;
6268c2ecf20Sopenharmony_ci	case OPAL_EEH_STOPPED_RESET:
6278c2ecf20Sopenharmony_ci		result = EEH_STATE_RESET_ACTIVE;
6288c2ecf20Sopenharmony_ci		break;
6298c2ecf20Sopenharmony_ci	case OPAL_EEH_STOPPED_TEMP_UNAVAIL:
6308c2ecf20Sopenharmony_ci		result = EEH_STATE_UNAVAILABLE;
6318c2ecf20Sopenharmony_ci		break;
6328c2ecf20Sopenharmony_ci	case OPAL_EEH_STOPPED_PERM_UNAVAIL:
6338c2ecf20Sopenharmony_ci		result = EEH_STATE_NOT_SUPPORT;
6348c2ecf20Sopenharmony_ci		break;
6358c2ecf20Sopenharmony_ci	default:
6368c2ecf20Sopenharmony_ci		result = EEH_STATE_NOT_SUPPORT;
6378c2ecf20Sopenharmony_ci		pr_warn("%s: Invalid PHB#%x-PE#%x state %x\n",
6388c2ecf20Sopenharmony_ci			__func__, phb->hose->global_number,
6398c2ecf20Sopenharmony_ci			pe->addr, fstate);
6408c2ecf20Sopenharmony_ci	}
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_ci	/*
6438c2ecf20Sopenharmony_ci	 * If PHB supports compound PE, to freeze all
6448c2ecf20Sopenharmony_ci	 * slave PEs for consistency.
6458c2ecf20Sopenharmony_ci	 *
6468c2ecf20Sopenharmony_ci	 * If the PE is switching to frozen state for the
6478c2ecf20Sopenharmony_ci	 * first time, to dump the PHB diag-data.
6488c2ecf20Sopenharmony_ci	 */
6498c2ecf20Sopenharmony_ci	if (!(result & EEH_STATE_NOT_SUPPORT) &&
6508c2ecf20Sopenharmony_ci	    !(result & EEH_STATE_UNAVAILABLE) &&
6518c2ecf20Sopenharmony_ci	    !(result & EEH_STATE_MMIO_ACTIVE) &&
6528c2ecf20Sopenharmony_ci	    !(result & EEH_STATE_DMA_ACTIVE)  &&
6538c2ecf20Sopenharmony_ci	    !(pe->state & EEH_PE_ISOLATED)) {
6548c2ecf20Sopenharmony_ci		if (phb->freeze_pe)
6558c2ecf20Sopenharmony_ci			phb->freeze_pe(phb, pe->addr);
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci		eeh_pe_mark_isolated(pe);
6588c2ecf20Sopenharmony_ci		pnv_eeh_get_phb_diag(pe);
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci		if (eeh_has_flag(EEH_EARLY_DUMP_LOG))
6618c2ecf20Sopenharmony_ci			pnv_pci_dump_phb_diag_data(pe->phb, pe->data);
6628c2ecf20Sopenharmony_ci	}
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci	return result;
6658c2ecf20Sopenharmony_ci}
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_ci/**
6688c2ecf20Sopenharmony_ci * pnv_eeh_get_state - Retrieve PE state
6698c2ecf20Sopenharmony_ci * @pe: EEH PE
6708c2ecf20Sopenharmony_ci * @delay: delay while PE state is temporarily unavailable
6718c2ecf20Sopenharmony_ci *
6728c2ecf20Sopenharmony_ci * Retrieve the state of the specified PE. For IODA-compitable
6738c2ecf20Sopenharmony_ci * platform, it should be retrieved from IODA table. Therefore,
6748c2ecf20Sopenharmony_ci * we prefer passing down to hardware implementation to handle
6758c2ecf20Sopenharmony_ci * it.
6768c2ecf20Sopenharmony_ci */
6778c2ecf20Sopenharmony_cistatic int pnv_eeh_get_state(struct eeh_pe *pe, int *delay)
6788c2ecf20Sopenharmony_ci{
6798c2ecf20Sopenharmony_ci	int ret;
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci	if (pe->type & EEH_PE_PHB)
6828c2ecf20Sopenharmony_ci		ret = pnv_eeh_get_phb_state(pe);
6838c2ecf20Sopenharmony_ci	else
6848c2ecf20Sopenharmony_ci		ret = pnv_eeh_get_pe_state(pe);
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci	if (!delay)
6878c2ecf20Sopenharmony_ci		return ret;
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_ci	/*
6908c2ecf20Sopenharmony_ci	 * If the PE state is temporarily unavailable,
6918c2ecf20Sopenharmony_ci	 * to inform the EEH core delay for default
6928c2ecf20Sopenharmony_ci	 * period (1 second)
6938c2ecf20Sopenharmony_ci	 */
6948c2ecf20Sopenharmony_ci	*delay = 0;
6958c2ecf20Sopenharmony_ci	if (ret & EEH_STATE_UNAVAILABLE)
6968c2ecf20Sopenharmony_ci		*delay = 1000;
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci	return ret;
6998c2ecf20Sopenharmony_ci}
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_cistatic s64 pnv_eeh_poll(unsigned long id)
7028c2ecf20Sopenharmony_ci{
7038c2ecf20Sopenharmony_ci	s64 rc = OPAL_HARDWARE;
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci	while (1) {
7068c2ecf20Sopenharmony_ci		rc = opal_pci_poll(id);
7078c2ecf20Sopenharmony_ci		if (rc <= 0)
7088c2ecf20Sopenharmony_ci			break;
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci		if (system_state < SYSTEM_RUNNING)
7118c2ecf20Sopenharmony_ci			udelay(1000 * rc);
7128c2ecf20Sopenharmony_ci		else
7138c2ecf20Sopenharmony_ci			msleep(rc);
7148c2ecf20Sopenharmony_ci	}
7158c2ecf20Sopenharmony_ci
7168c2ecf20Sopenharmony_ci	return rc;
7178c2ecf20Sopenharmony_ci}
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ciint pnv_eeh_phb_reset(struct pci_controller *hose, int option)
7208c2ecf20Sopenharmony_ci{
7218c2ecf20Sopenharmony_ci	struct pnv_phb *phb = hose->private_data;
7228c2ecf20Sopenharmony_ci	s64 rc = OPAL_HARDWARE;
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci	pr_debug("%s: Reset PHB#%x, option=%d\n",
7258c2ecf20Sopenharmony_ci		 __func__, hose->global_number, option);
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci	/* Issue PHB complete reset request */
7288c2ecf20Sopenharmony_ci	if (option == EEH_RESET_FUNDAMENTAL ||
7298c2ecf20Sopenharmony_ci	    option == EEH_RESET_HOT)
7308c2ecf20Sopenharmony_ci		rc = opal_pci_reset(phb->opal_id,
7318c2ecf20Sopenharmony_ci				    OPAL_RESET_PHB_COMPLETE,
7328c2ecf20Sopenharmony_ci				    OPAL_ASSERT_RESET);
7338c2ecf20Sopenharmony_ci	else if (option == EEH_RESET_DEACTIVATE)
7348c2ecf20Sopenharmony_ci		rc = opal_pci_reset(phb->opal_id,
7358c2ecf20Sopenharmony_ci				    OPAL_RESET_PHB_COMPLETE,
7368c2ecf20Sopenharmony_ci				    OPAL_DEASSERT_RESET);
7378c2ecf20Sopenharmony_ci	if (rc < 0)
7388c2ecf20Sopenharmony_ci		goto out;
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ci	/*
7418c2ecf20Sopenharmony_ci	 * Poll state of the PHB until the request is done
7428c2ecf20Sopenharmony_ci	 * successfully. The PHB reset is usually PHB complete
7438c2ecf20Sopenharmony_ci	 * reset followed by hot reset on root bus. So we also
7448c2ecf20Sopenharmony_ci	 * need the PCI bus settlement delay.
7458c2ecf20Sopenharmony_ci	 */
7468c2ecf20Sopenharmony_ci	if (rc > 0)
7478c2ecf20Sopenharmony_ci		rc = pnv_eeh_poll(phb->opal_id);
7488c2ecf20Sopenharmony_ci	if (option == EEH_RESET_DEACTIVATE) {
7498c2ecf20Sopenharmony_ci		if (system_state < SYSTEM_RUNNING)
7508c2ecf20Sopenharmony_ci			udelay(1000 * EEH_PE_RST_SETTLE_TIME);
7518c2ecf20Sopenharmony_ci		else
7528c2ecf20Sopenharmony_ci			msleep(EEH_PE_RST_SETTLE_TIME);
7538c2ecf20Sopenharmony_ci	}
7548c2ecf20Sopenharmony_ciout:
7558c2ecf20Sopenharmony_ci	if (rc != OPAL_SUCCESS)
7568c2ecf20Sopenharmony_ci		return -EIO;
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci	return 0;
7598c2ecf20Sopenharmony_ci}
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_cistatic int pnv_eeh_root_reset(struct pci_controller *hose, int option)
7628c2ecf20Sopenharmony_ci{
7638c2ecf20Sopenharmony_ci	struct pnv_phb *phb = hose->private_data;
7648c2ecf20Sopenharmony_ci	s64 rc = OPAL_HARDWARE;
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ci	pr_debug("%s: Reset PHB#%x, option=%d\n",
7678c2ecf20Sopenharmony_ci		 __func__, hose->global_number, option);
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ci	/*
7708c2ecf20Sopenharmony_ci	 * During the reset deassert time, we needn't care
7718c2ecf20Sopenharmony_ci	 * the reset scope because the firmware does nothing
7728c2ecf20Sopenharmony_ci	 * for fundamental or hot reset during deassert phase.
7738c2ecf20Sopenharmony_ci	 */
7748c2ecf20Sopenharmony_ci	if (option == EEH_RESET_FUNDAMENTAL)
7758c2ecf20Sopenharmony_ci		rc = opal_pci_reset(phb->opal_id,
7768c2ecf20Sopenharmony_ci				    OPAL_RESET_PCI_FUNDAMENTAL,
7778c2ecf20Sopenharmony_ci				    OPAL_ASSERT_RESET);
7788c2ecf20Sopenharmony_ci	else if (option == EEH_RESET_HOT)
7798c2ecf20Sopenharmony_ci		rc = opal_pci_reset(phb->opal_id,
7808c2ecf20Sopenharmony_ci				    OPAL_RESET_PCI_HOT,
7818c2ecf20Sopenharmony_ci				    OPAL_ASSERT_RESET);
7828c2ecf20Sopenharmony_ci	else if (option == EEH_RESET_DEACTIVATE)
7838c2ecf20Sopenharmony_ci		rc = opal_pci_reset(phb->opal_id,
7848c2ecf20Sopenharmony_ci				    OPAL_RESET_PCI_HOT,
7858c2ecf20Sopenharmony_ci				    OPAL_DEASSERT_RESET);
7868c2ecf20Sopenharmony_ci	if (rc < 0)
7878c2ecf20Sopenharmony_ci		goto out;
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci	/* Poll state of the PHB until the request is done */
7908c2ecf20Sopenharmony_ci	if (rc > 0)
7918c2ecf20Sopenharmony_ci		rc = pnv_eeh_poll(phb->opal_id);
7928c2ecf20Sopenharmony_ci	if (option == EEH_RESET_DEACTIVATE)
7938c2ecf20Sopenharmony_ci		msleep(EEH_PE_RST_SETTLE_TIME);
7948c2ecf20Sopenharmony_ciout:
7958c2ecf20Sopenharmony_ci	if (rc != OPAL_SUCCESS)
7968c2ecf20Sopenharmony_ci		return -EIO;
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci	return 0;
7998c2ecf20Sopenharmony_ci}
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_cistatic int __pnv_eeh_bridge_reset(struct pci_dev *dev, int option)
8028c2ecf20Sopenharmony_ci{
8038c2ecf20Sopenharmony_ci	struct pci_dn *pdn = pci_get_pdn_by_devfn(dev->bus, dev->devfn);
8048c2ecf20Sopenharmony_ci	struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
8058c2ecf20Sopenharmony_ci	int aer = edev ? edev->aer_cap : 0;
8068c2ecf20Sopenharmony_ci	u32 ctrl;
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_ci	pr_debug("%s: Secondary Reset PCI bus %04x:%02x with option %d\n",
8098c2ecf20Sopenharmony_ci		 __func__, pci_domain_nr(dev->bus),
8108c2ecf20Sopenharmony_ci		 dev->bus->number, option);
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ci	switch (option) {
8138c2ecf20Sopenharmony_ci	case EEH_RESET_FUNDAMENTAL:
8148c2ecf20Sopenharmony_ci	case EEH_RESET_HOT:
8158c2ecf20Sopenharmony_ci		/* Don't report linkDown event */
8168c2ecf20Sopenharmony_ci		if (aer) {
8178c2ecf20Sopenharmony_ci			eeh_ops->read_config(edev, aer + PCI_ERR_UNCOR_MASK,
8188c2ecf20Sopenharmony_ci					     4, &ctrl);
8198c2ecf20Sopenharmony_ci			ctrl |= PCI_ERR_UNC_SURPDN;
8208c2ecf20Sopenharmony_ci			eeh_ops->write_config(edev, aer + PCI_ERR_UNCOR_MASK,
8218c2ecf20Sopenharmony_ci					      4, ctrl);
8228c2ecf20Sopenharmony_ci		}
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_ci		eeh_ops->read_config(edev, PCI_BRIDGE_CONTROL, 2, &ctrl);
8258c2ecf20Sopenharmony_ci		ctrl |= PCI_BRIDGE_CTL_BUS_RESET;
8268c2ecf20Sopenharmony_ci		eeh_ops->write_config(edev, PCI_BRIDGE_CONTROL, 2, ctrl);
8278c2ecf20Sopenharmony_ci
8288c2ecf20Sopenharmony_ci		msleep(EEH_PE_RST_HOLD_TIME);
8298c2ecf20Sopenharmony_ci		break;
8308c2ecf20Sopenharmony_ci	case EEH_RESET_DEACTIVATE:
8318c2ecf20Sopenharmony_ci		eeh_ops->read_config(edev, PCI_BRIDGE_CONTROL, 2, &ctrl);
8328c2ecf20Sopenharmony_ci		ctrl &= ~PCI_BRIDGE_CTL_BUS_RESET;
8338c2ecf20Sopenharmony_ci		eeh_ops->write_config(edev, PCI_BRIDGE_CONTROL, 2, ctrl);
8348c2ecf20Sopenharmony_ci
8358c2ecf20Sopenharmony_ci		msleep(EEH_PE_RST_SETTLE_TIME);
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ci		/* Continue reporting linkDown event */
8388c2ecf20Sopenharmony_ci		if (aer) {
8398c2ecf20Sopenharmony_ci			eeh_ops->read_config(edev, aer + PCI_ERR_UNCOR_MASK,
8408c2ecf20Sopenharmony_ci					     4, &ctrl);
8418c2ecf20Sopenharmony_ci			ctrl &= ~PCI_ERR_UNC_SURPDN;
8428c2ecf20Sopenharmony_ci			eeh_ops->write_config(edev, aer + PCI_ERR_UNCOR_MASK,
8438c2ecf20Sopenharmony_ci					      4, ctrl);
8448c2ecf20Sopenharmony_ci		}
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_ci		break;
8478c2ecf20Sopenharmony_ci	}
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci	return 0;
8508c2ecf20Sopenharmony_ci}
8518c2ecf20Sopenharmony_ci
8528c2ecf20Sopenharmony_cistatic int pnv_eeh_bridge_reset(struct pci_dev *pdev, int option)
8538c2ecf20Sopenharmony_ci{
8548c2ecf20Sopenharmony_ci	struct pci_controller *hose = pci_bus_to_host(pdev->bus);
8558c2ecf20Sopenharmony_ci	struct pnv_phb *phb = hose->private_data;
8568c2ecf20Sopenharmony_ci	struct device_node *dn = pci_device_to_OF_node(pdev);
8578c2ecf20Sopenharmony_ci	uint64_t id = PCI_SLOT_ID(phb->opal_id,
8588c2ecf20Sopenharmony_ci				  (pdev->bus->number << 8) | pdev->devfn);
8598c2ecf20Sopenharmony_ci	uint8_t scope;
8608c2ecf20Sopenharmony_ci	int64_t rc;
8618c2ecf20Sopenharmony_ci
8628c2ecf20Sopenharmony_ci	/* Hot reset to the bus if firmware cannot handle */
8638c2ecf20Sopenharmony_ci	if (!dn || !of_get_property(dn, "ibm,reset-by-firmware", NULL))
8648c2ecf20Sopenharmony_ci		return __pnv_eeh_bridge_reset(pdev, option);
8658c2ecf20Sopenharmony_ci
8668c2ecf20Sopenharmony_ci	pr_debug("%s: FW reset PCI bus %04x:%02x with option %d\n",
8678c2ecf20Sopenharmony_ci		 __func__, pci_domain_nr(pdev->bus),
8688c2ecf20Sopenharmony_ci		 pdev->bus->number, option);
8698c2ecf20Sopenharmony_ci
8708c2ecf20Sopenharmony_ci	switch (option) {
8718c2ecf20Sopenharmony_ci	case EEH_RESET_FUNDAMENTAL:
8728c2ecf20Sopenharmony_ci		scope = OPAL_RESET_PCI_FUNDAMENTAL;
8738c2ecf20Sopenharmony_ci		break;
8748c2ecf20Sopenharmony_ci	case EEH_RESET_HOT:
8758c2ecf20Sopenharmony_ci		scope = OPAL_RESET_PCI_HOT;
8768c2ecf20Sopenharmony_ci		break;
8778c2ecf20Sopenharmony_ci	case EEH_RESET_DEACTIVATE:
8788c2ecf20Sopenharmony_ci		return 0;
8798c2ecf20Sopenharmony_ci	default:
8808c2ecf20Sopenharmony_ci		dev_dbg(&pdev->dev, "%s: Unsupported reset %d\n",
8818c2ecf20Sopenharmony_ci			__func__, option);
8828c2ecf20Sopenharmony_ci		return -EINVAL;
8838c2ecf20Sopenharmony_ci	}
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci	rc = opal_pci_reset(id, scope, OPAL_ASSERT_RESET);
8868c2ecf20Sopenharmony_ci	if (rc <= OPAL_SUCCESS)
8878c2ecf20Sopenharmony_ci		goto out;
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_ci	rc = pnv_eeh_poll(id);
8908c2ecf20Sopenharmony_ciout:
8918c2ecf20Sopenharmony_ci	return (rc == OPAL_SUCCESS) ? 0 : -EIO;
8928c2ecf20Sopenharmony_ci}
8938c2ecf20Sopenharmony_ci
8948c2ecf20Sopenharmony_civoid pnv_pci_reset_secondary_bus(struct pci_dev *dev)
8958c2ecf20Sopenharmony_ci{
8968c2ecf20Sopenharmony_ci	struct pci_controller *hose;
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci	if (pci_is_root_bus(dev->bus)) {
8998c2ecf20Sopenharmony_ci		hose = pci_bus_to_host(dev->bus);
9008c2ecf20Sopenharmony_ci		pnv_eeh_root_reset(hose, EEH_RESET_HOT);
9018c2ecf20Sopenharmony_ci		pnv_eeh_root_reset(hose, EEH_RESET_DEACTIVATE);
9028c2ecf20Sopenharmony_ci	} else {
9038c2ecf20Sopenharmony_ci		pnv_eeh_bridge_reset(dev, EEH_RESET_HOT);
9048c2ecf20Sopenharmony_ci		pnv_eeh_bridge_reset(dev, EEH_RESET_DEACTIVATE);
9058c2ecf20Sopenharmony_ci	}
9068c2ecf20Sopenharmony_ci}
9078c2ecf20Sopenharmony_ci
9088c2ecf20Sopenharmony_cistatic void pnv_eeh_wait_for_pending(struct pci_dn *pdn, const char *type,
9098c2ecf20Sopenharmony_ci				     int pos, u16 mask)
9108c2ecf20Sopenharmony_ci{
9118c2ecf20Sopenharmony_ci	struct eeh_dev *edev = pdn->edev;
9128c2ecf20Sopenharmony_ci	int i, status = 0;
9138c2ecf20Sopenharmony_ci
9148c2ecf20Sopenharmony_ci	/* Wait for Transaction Pending bit to be cleared */
9158c2ecf20Sopenharmony_ci	for (i = 0; i < 4; i++) {
9168c2ecf20Sopenharmony_ci		eeh_ops->read_config(edev, pos, 2, &status);
9178c2ecf20Sopenharmony_ci		if (!(status & mask))
9188c2ecf20Sopenharmony_ci			return;
9198c2ecf20Sopenharmony_ci
9208c2ecf20Sopenharmony_ci		msleep((1 << i) * 100);
9218c2ecf20Sopenharmony_ci	}
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci	pr_warn("%s: Pending transaction while issuing %sFLR to %04x:%02x:%02x.%01x\n",
9248c2ecf20Sopenharmony_ci		__func__, type,
9258c2ecf20Sopenharmony_ci		pdn->phb->global_number, pdn->busno,
9268c2ecf20Sopenharmony_ci		PCI_SLOT(pdn->devfn), PCI_FUNC(pdn->devfn));
9278c2ecf20Sopenharmony_ci}
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_cistatic int pnv_eeh_do_flr(struct pci_dn *pdn, int option)
9308c2ecf20Sopenharmony_ci{
9318c2ecf20Sopenharmony_ci	struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
9328c2ecf20Sopenharmony_ci	u32 reg = 0;
9338c2ecf20Sopenharmony_ci
9348c2ecf20Sopenharmony_ci	if (WARN_ON(!edev->pcie_cap))
9358c2ecf20Sopenharmony_ci		return -ENOTTY;
9368c2ecf20Sopenharmony_ci
9378c2ecf20Sopenharmony_ci	eeh_ops->read_config(edev, edev->pcie_cap + PCI_EXP_DEVCAP, 4, &reg);
9388c2ecf20Sopenharmony_ci	if (!(reg & PCI_EXP_DEVCAP_FLR))
9398c2ecf20Sopenharmony_ci		return -ENOTTY;
9408c2ecf20Sopenharmony_ci
9418c2ecf20Sopenharmony_ci	switch (option) {
9428c2ecf20Sopenharmony_ci	case EEH_RESET_HOT:
9438c2ecf20Sopenharmony_ci	case EEH_RESET_FUNDAMENTAL:
9448c2ecf20Sopenharmony_ci		pnv_eeh_wait_for_pending(pdn, "",
9458c2ecf20Sopenharmony_ci					 edev->pcie_cap + PCI_EXP_DEVSTA,
9468c2ecf20Sopenharmony_ci					 PCI_EXP_DEVSTA_TRPND);
9478c2ecf20Sopenharmony_ci		eeh_ops->read_config(edev, edev->pcie_cap + PCI_EXP_DEVCTL,
9488c2ecf20Sopenharmony_ci				     4, &reg);
9498c2ecf20Sopenharmony_ci		reg |= PCI_EXP_DEVCTL_BCR_FLR;
9508c2ecf20Sopenharmony_ci		eeh_ops->write_config(edev, edev->pcie_cap + PCI_EXP_DEVCTL,
9518c2ecf20Sopenharmony_ci				      4, reg);
9528c2ecf20Sopenharmony_ci		msleep(EEH_PE_RST_HOLD_TIME);
9538c2ecf20Sopenharmony_ci		break;
9548c2ecf20Sopenharmony_ci	case EEH_RESET_DEACTIVATE:
9558c2ecf20Sopenharmony_ci		eeh_ops->read_config(edev, edev->pcie_cap + PCI_EXP_DEVCTL,
9568c2ecf20Sopenharmony_ci				     4, &reg);
9578c2ecf20Sopenharmony_ci		reg &= ~PCI_EXP_DEVCTL_BCR_FLR;
9588c2ecf20Sopenharmony_ci		eeh_ops->write_config(edev, edev->pcie_cap + PCI_EXP_DEVCTL,
9598c2ecf20Sopenharmony_ci				      4, reg);
9608c2ecf20Sopenharmony_ci		msleep(EEH_PE_RST_SETTLE_TIME);
9618c2ecf20Sopenharmony_ci		break;
9628c2ecf20Sopenharmony_ci	}
9638c2ecf20Sopenharmony_ci
9648c2ecf20Sopenharmony_ci	return 0;
9658c2ecf20Sopenharmony_ci}
9668c2ecf20Sopenharmony_ci
9678c2ecf20Sopenharmony_cistatic int pnv_eeh_do_af_flr(struct pci_dn *pdn, int option)
9688c2ecf20Sopenharmony_ci{
9698c2ecf20Sopenharmony_ci	struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
9708c2ecf20Sopenharmony_ci	u32 cap = 0;
9718c2ecf20Sopenharmony_ci
9728c2ecf20Sopenharmony_ci	if (WARN_ON(!edev->af_cap))
9738c2ecf20Sopenharmony_ci		return -ENOTTY;
9748c2ecf20Sopenharmony_ci
9758c2ecf20Sopenharmony_ci	eeh_ops->read_config(edev, edev->af_cap + PCI_AF_CAP, 1, &cap);
9768c2ecf20Sopenharmony_ci	if (!(cap & PCI_AF_CAP_TP) || !(cap & PCI_AF_CAP_FLR))
9778c2ecf20Sopenharmony_ci		return -ENOTTY;
9788c2ecf20Sopenharmony_ci
9798c2ecf20Sopenharmony_ci	switch (option) {
9808c2ecf20Sopenharmony_ci	case EEH_RESET_HOT:
9818c2ecf20Sopenharmony_ci	case EEH_RESET_FUNDAMENTAL:
9828c2ecf20Sopenharmony_ci		/*
9838c2ecf20Sopenharmony_ci		 * Wait for Transaction Pending bit to clear. A word-aligned
9848c2ecf20Sopenharmony_ci		 * test is used, so we use the conrol offset rather than status
9858c2ecf20Sopenharmony_ci		 * and shift the test bit to match.
9868c2ecf20Sopenharmony_ci		 */
9878c2ecf20Sopenharmony_ci		pnv_eeh_wait_for_pending(pdn, "AF",
9888c2ecf20Sopenharmony_ci					 edev->af_cap + PCI_AF_CTRL,
9898c2ecf20Sopenharmony_ci					 PCI_AF_STATUS_TP << 8);
9908c2ecf20Sopenharmony_ci		eeh_ops->write_config(edev, edev->af_cap + PCI_AF_CTRL,
9918c2ecf20Sopenharmony_ci				      1, PCI_AF_CTRL_FLR);
9928c2ecf20Sopenharmony_ci		msleep(EEH_PE_RST_HOLD_TIME);
9938c2ecf20Sopenharmony_ci		break;
9948c2ecf20Sopenharmony_ci	case EEH_RESET_DEACTIVATE:
9958c2ecf20Sopenharmony_ci		eeh_ops->write_config(edev, edev->af_cap + PCI_AF_CTRL, 1, 0);
9968c2ecf20Sopenharmony_ci		msleep(EEH_PE_RST_SETTLE_TIME);
9978c2ecf20Sopenharmony_ci		break;
9988c2ecf20Sopenharmony_ci	}
9998c2ecf20Sopenharmony_ci
10008c2ecf20Sopenharmony_ci	return 0;
10018c2ecf20Sopenharmony_ci}
10028c2ecf20Sopenharmony_ci
10038c2ecf20Sopenharmony_cistatic int pnv_eeh_reset_vf_pe(struct eeh_pe *pe, int option)
10048c2ecf20Sopenharmony_ci{
10058c2ecf20Sopenharmony_ci	struct eeh_dev *edev;
10068c2ecf20Sopenharmony_ci	struct pci_dn *pdn;
10078c2ecf20Sopenharmony_ci	int ret;
10088c2ecf20Sopenharmony_ci
10098c2ecf20Sopenharmony_ci	/* The VF PE should have only one child device */
10108c2ecf20Sopenharmony_ci	edev = list_first_entry_or_null(&pe->edevs, struct eeh_dev, entry);
10118c2ecf20Sopenharmony_ci	pdn = eeh_dev_to_pdn(edev);
10128c2ecf20Sopenharmony_ci	if (!pdn)
10138c2ecf20Sopenharmony_ci		return -ENXIO;
10148c2ecf20Sopenharmony_ci
10158c2ecf20Sopenharmony_ci	ret = pnv_eeh_do_flr(pdn, option);
10168c2ecf20Sopenharmony_ci	if (!ret)
10178c2ecf20Sopenharmony_ci		return ret;
10188c2ecf20Sopenharmony_ci
10198c2ecf20Sopenharmony_ci	return pnv_eeh_do_af_flr(pdn, option);
10208c2ecf20Sopenharmony_ci}
10218c2ecf20Sopenharmony_ci
10228c2ecf20Sopenharmony_ci/**
10238c2ecf20Sopenharmony_ci * pnv_eeh_reset - Reset the specified PE
10248c2ecf20Sopenharmony_ci * @pe: EEH PE
10258c2ecf20Sopenharmony_ci * @option: reset option
10268c2ecf20Sopenharmony_ci *
10278c2ecf20Sopenharmony_ci * Do reset on the indicated PE. For PCI bus sensitive PE,
10288c2ecf20Sopenharmony_ci * we need to reset the parent p2p bridge. The PHB has to
10298c2ecf20Sopenharmony_ci * be reinitialized if the p2p bridge is root bridge. For
10308c2ecf20Sopenharmony_ci * PCI device sensitive PE, we will try to reset the device
10318c2ecf20Sopenharmony_ci * through FLR. For now, we don't have OPAL APIs to do HARD
10328c2ecf20Sopenharmony_ci * reset yet, so all reset would be SOFT (HOT) reset.
10338c2ecf20Sopenharmony_ci */
10348c2ecf20Sopenharmony_cistatic int pnv_eeh_reset(struct eeh_pe *pe, int option)
10358c2ecf20Sopenharmony_ci{
10368c2ecf20Sopenharmony_ci	struct pci_controller *hose = pe->phb;
10378c2ecf20Sopenharmony_ci	struct pnv_phb *phb;
10388c2ecf20Sopenharmony_ci	struct pci_bus *bus;
10398c2ecf20Sopenharmony_ci	int64_t rc;
10408c2ecf20Sopenharmony_ci
10418c2ecf20Sopenharmony_ci	/*
10428c2ecf20Sopenharmony_ci	 * For PHB reset, we always have complete reset. For those PEs whose
10438c2ecf20Sopenharmony_ci	 * primary bus derived from root complex (root bus) or root port
10448c2ecf20Sopenharmony_ci	 * (usually bus#1), we apply hot or fundamental reset on the root port.
10458c2ecf20Sopenharmony_ci	 * For other PEs, we always have hot reset on the PE primary bus.
10468c2ecf20Sopenharmony_ci	 *
10478c2ecf20Sopenharmony_ci	 * Here, we have different design to pHyp, which always clear the
10488c2ecf20Sopenharmony_ci	 * frozen state during PE reset. However, the good idea here from
10498c2ecf20Sopenharmony_ci	 * benh is to keep frozen state before we get PE reset done completely
10508c2ecf20Sopenharmony_ci	 * (until BAR restore). With the frozen state, HW drops illegal IO
10518c2ecf20Sopenharmony_ci	 * or MMIO access, which can incur recrusive frozen PE during PE
10528c2ecf20Sopenharmony_ci	 * reset. The side effect is that EEH core has to clear the frozen
10538c2ecf20Sopenharmony_ci	 * state explicitly after BAR restore.
10548c2ecf20Sopenharmony_ci	 */
10558c2ecf20Sopenharmony_ci	if (pe->type & EEH_PE_PHB)
10568c2ecf20Sopenharmony_ci		return pnv_eeh_phb_reset(hose, option);
10578c2ecf20Sopenharmony_ci
10588c2ecf20Sopenharmony_ci	/*
10598c2ecf20Sopenharmony_ci	 * The frozen PE might be caused by PAPR error injection
10608c2ecf20Sopenharmony_ci	 * registers, which are expected to be cleared after hitting
10618c2ecf20Sopenharmony_ci	 * frozen PE as stated in the hardware spec. Unfortunately,
10628c2ecf20Sopenharmony_ci	 * that's not true on P7IOC. So we have to clear it manually
10638c2ecf20Sopenharmony_ci	 * to avoid recursive EEH errors during recovery.
10648c2ecf20Sopenharmony_ci	 */
10658c2ecf20Sopenharmony_ci	phb = hose->private_data;
10668c2ecf20Sopenharmony_ci	if (phb->model == PNV_PHB_MODEL_P7IOC &&
10678c2ecf20Sopenharmony_ci	    (option == EEH_RESET_HOT ||
10688c2ecf20Sopenharmony_ci	     option == EEH_RESET_FUNDAMENTAL)) {
10698c2ecf20Sopenharmony_ci		rc = opal_pci_reset(phb->opal_id,
10708c2ecf20Sopenharmony_ci				    OPAL_RESET_PHB_ERROR,
10718c2ecf20Sopenharmony_ci				    OPAL_ASSERT_RESET);
10728c2ecf20Sopenharmony_ci		if (rc != OPAL_SUCCESS) {
10738c2ecf20Sopenharmony_ci			pr_warn("%s: Failure %lld clearing error injection registers\n",
10748c2ecf20Sopenharmony_ci				__func__, rc);
10758c2ecf20Sopenharmony_ci			return -EIO;
10768c2ecf20Sopenharmony_ci		}
10778c2ecf20Sopenharmony_ci	}
10788c2ecf20Sopenharmony_ci
10798c2ecf20Sopenharmony_ci	if (pe->type & EEH_PE_VF)
10808c2ecf20Sopenharmony_ci		return pnv_eeh_reset_vf_pe(pe, option);
10818c2ecf20Sopenharmony_ci
10828c2ecf20Sopenharmony_ci	bus = eeh_pe_bus_get(pe);
10838c2ecf20Sopenharmony_ci	if (!bus) {
10848c2ecf20Sopenharmony_ci		pr_err("%s: Cannot find PCI bus for PHB#%x-PE#%x\n",
10858c2ecf20Sopenharmony_ci			__func__, pe->phb->global_number, pe->addr);
10868c2ecf20Sopenharmony_ci		return -EIO;
10878c2ecf20Sopenharmony_ci	}
10888c2ecf20Sopenharmony_ci
10898c2ecf20Sopenharmony_ci	if (pci_is_root_bus(bus))
10908c2ecf20Sopenharmony_ci		return pnv_eeh_root_reset(hose, option);
10918c2ecf20Sopenharmony_ci
10928c2ecf20Sopenharmony_ci	/*
10938c2ecf20Sopenharmony_ci	 * For hot resets try use the generic PCI error recovery reset
10948c2ecf20Sopenharmony_ci	 * functions. These correctly handles the case where the secondary
10958c2ecf20Sopenharmony_ci	 * bus is behind a hotplug slot and it will use the slot provided
10968c2ecf20Sopenharmony_ci	 * reset methods to prevent spurious hotplug events during the reset.
10978c2ecf20Sopenharmony_ci	 *
10988c2ecf20Sopenharmony_ci	 * Fundemental resets need to be handled internally to EEH since the
10998c2ecf20Sopenharmony_ci	 * PCI core doesn't really have a concept of a fundemental reset,
11008c2ecf20Sopenharmony_ci	 * mainly because there's no standard way to generate one. Only a
11018c2ecf20Sopenharmony_ci	 * few devices require an FRESET so it should be fine.
11028c2ecf20Sopenharmony_ci	 */
11038c2ecf20Sopenharmony_ci	if (option != EEH_RESET_FUNDAMENTAL) {
11048c2ecf20Sopenharmony_ci		/*
11058c2ecf20Sopenharmony_ci		 * NB: Skiboot and pnv_eeh_bridge_reset() also no-op the
11068c2ecf20Sopenharmony_ci		 *     de-assert step. It's like the OPAL reset API was
11078c2ecf20Sopenharmony_ci		 *     poorly designed or something...
11088c2ecf20Sopenharmony_ci		 */
11098c2ecf20Sopenharmony_ci		if (option == EEH_RESET_DEACTIVATE)
11108c2ecf20Sopenharmony_ci			return 0;
11118c2ecf20Sopenharmony_ci
11128c2ecf20Sopenharmony_ci		rc = pci_bus_error_reset(bus->self);
11138c2ecf20Sopenharmony_ci		if (!rc)
11148c2ecf20Sopenharmony_ci			return 0;
11158c2ecf20Sopenharmony_ci	}
11168c2ecf20Sopenharmony_ci
11178c2ecf20Sopenharmony_ci	/* otherwise, use the generic bridge reset. this might call into FW */
11188c2ecf20Sopenharmony_ci	if (pci_is_root_bus(bus->parent))
11198c2ecf20Sopenharmony_ci		return pnv_eeh_root_reset(hose, option);
11208c2ecf20Sopenharmony_ci	return pnv_eeh_bridge_reset(bus->self, option);
11218c2ecf20Sopenharmony_ci}
11228c2ecf20Sopenharmony_ci
11238c2ecf20Sopenharmony_ci/**
11248c2ecf20Sopenharmony_ci * pnv_eeh_get_log - Retrieve error log
11258c2ecf20Sopenharmony_ci * @pe: EEH PE
11268c2ecf20Sopenharmony_ci * @severity: temporary or permanent error log
11278c2ecf20Sopenharmony_ci * @drv_log: driver log to be combined with retrieved error log
11288c2ecf20Sopenharmony_ci * @len: length of driver log
11298c2ecf20Sopenharmony_ci *
11308c2ecf20Sopenharmony_ci * Retrieve the temporary or permanent error from the PE.
11318c2ecf20Sopenharmony_ci */
11328c2ecf20Sopenharmony_cistatic int pnv_eeh_get_log(struct eeh_pe *pe, int severity,
11338c2ecf20Sopenharmony_ci			   char *drv_log, unsigned long len)
11348c2ecf20Sopenharmony_ci{
11358c2ecf20Sopenharmony_ci	if (!eeh_has_flag(EEH_EARLY_DUMP_LOG))
11368c2ecf20Sopenharmony_ci		pnv_pci_dump_phb_diag_data(pe->phb, pe->data);
11378c2ecf20Sopenharmony_ci
11388c2ecf20Sopenharmony_ci	return 0;
11398c2ecf20Sopenharmony_ci}
11408c2ecf20Sopenharmony_ci
11418c2ecf20Sopenharmony_ci/**
11428c2ecf20Sopenharmony_ci * pnv_eeh_configure_bridge - Configure PCI bridges in the indicated PE
11438c2ecf20Sopenharmony_ci * @pe: EEH PE
11448c2ecf20Sopenharmony_ci *
11458c2ecf20Sopenharmony_ci * The function will be called to reconfigure the bridges included
11468c2ecf20Sopenharmony_ci * in the specified PE so that the mulfunctional PE would be recovered
11478c2ecf20Sopenharmony_ci * again.
11488c2ecf20Sopenharmony_ci */
11498c2ecf20Sopenharmony_cistatic int pnv_eeh_configure_bridge(struct eeh_pe *pe)
11508c2ecf20Sopenharmony_ci{
11518c2ecf20Sopenharmony_ci	return 0;
11528c2ecf20Sopenharmony_ci}
11538c2ecf20Sopenharmony_ci
11548c2ecf20Sopenharmony_ci/**
11558c2ecf20Sopenharmony_ci * pnv_pe_err_inject - Inject specified error to the indicated PE
11568c2ecf20Sopenharmony_ci * @pe: the indicated PE
11578c2ecf20Sopenharmony_ci * @type: error type
11588c2ecf20Sopenharmony_ci * @func: specific error type
11598c2ecf20Sopenharmony_ci * @addr: address
11608c2ecf20Sopenharmony_ci * @mask: address mask
11618c2ecf20Sopenharmony_ci *
11628c2ecf20Sopenharmony_ci * The routine is called to inject specified error, which is
11638c2ecf20Sopenharmony_ci * determined by @type and @func, to the indicated PE for
11648c2ecf20Sopenharmony_ci * testing purpose.
11658c2ecf20Sopenharmony_ci */
11668c2ecf20Sopenharmony_cistatic int pnv_eeh_err_inject(struct eeh_pe *pe, int type, int func,
11678c2ecf20Sopenharmony_ci			      unsigned long addr, unsigned long mask)
11688c2ecf20Sopenharmony_ci{
11698c2ecf20Sopenharmony_ci	struct pci_controller *hose = pe->phb;
11708c2ecf20Sopenharmony_ci	struct pnv_phb *phb = hose->private_data;
11718c2ecf20Sopenharmony_ci	s64 rc;
11728c2ecf20Sopenharmony_ci
11738c2ecf20Sopenharmony_ci	if (type != OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR &&
11748c2ecf20Sopenharmony_ci	    type != OPAL_ERR_INJECT_TYPE_IOA_BUS_ERR64) {
11758c2ecf20Sopenharmony_ci		pr_warn("%s: Invalid error type %d\n",
11768c2ecf20Sopenharmony_ci			__func__, type);
11778c2ecf20Sopenharmony_ci		return -ERANGE;
11788c2ecf20Sopenharmony_ci	}
11798c2ecf20Sopenharmony_ci
11808c2ecf20Sopenharmony_ci	if (func < OPAL_ERR_INJECT_FUNC_IOA_LD_MEM_ADDR ||
11818c2ecf20Sopenharmony_ci	    func > OPAL_ERR_INJECT_FUNC_IOA_DMA_WR_TARGET) {
11828c2ecf20Sopenharmony_ci		pr_warn("%s: Invalid error function %d\n",
11838c2ecf20Sopenharmony_ci			__func__, func);
11848c2ecf20Sopenharmony_ci		return -ERANGE;
11858c2ecf20Sopenharmony_ci	}
11868c2ecf20Sopenharmony_ci
11878c2ecf20Sopenharmony_ci	/* Firmware supports error injection ? */
11888c2ecf20Sopenharmony_ci	if (!opal_check_token(OPAL_PCI_ERR_INJECT)) {
11898c2ecf20Sopenharmony_ci		pr_warn("%s: Firmware doesn't support error injection\n",
11908c2ecf20Sopenharmony_ci			__func__);
11918c2ecf20Sopenharmony_ci		return -ENXIO;
11928c2ecf20Sopenharmony_ci	}
11938c2ecf20Sopenharmony_ci
11948c2ecf20Sopenharmony_ci	/* Do error injection */
11958c2ecf20Sopenharmony_ci	rc = opal_pci_err_inject(phb->opal_id, pe->addr,
11968c2ecf20Sopenharmony_ci				 type, func, addr, mask);
11978c2ecf20Sopenharmony_ci	if (rc != OPAL_SUCCESS) {
11988c2ecf20Sopenharmony_ci		pr_warn("%s: Failure %lld injecting error "
11998c2ecf20Sopenharmony_ci			"%d-%d to PHB#%x-PE#%x\n",
12008c2ecf20Sopenharmony_ci			__func__, rc, type, func,
12018c2ecf20Sopenharmony_ci			hose->global_number, pe->addr);
12028c2ecf20Sopenharmony_ci		return -EIO;
12038c2ecf20Sopenharmony_ci	}
12048c2ecf20Sopenharmony_ci
12058c2ecf20Sopenharmony_ci	return 0;
12068c2ecf20Sopenharmony_ci}
12078c2ecf20Sopenharmony_ci
12088c2ecf20Sopenharmony_cistatic inline bool pnv_eeh_cfg_blocked(struct pci_dn *pdn)
12098c2ecf20Sopenharmony_ci{
12108c2ecf20Sopenharmony_ci	struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
12118c2ecf20Sopenharmony_ci
12128c2ecf20Sopenharmony_ci	if (!edev || !edev->pe)
12138c2ecf20Sopenharmony_ci		return false;
12148c2ecf20Sopenharmony_ci
12158c2ecf20Sopenharmony_ci	/*
12168c2ecf20Sopenharmony_ci	 * We will issue FLR or AF FLR to all VFs, which are contained
12178c2ecf20Sopenharmony_ci	 * in VF PE. It relies on the EEH PCI config accessors. So we
12188c2ecf20Sopenharmony_ci	 * can't block them during the window.
12198c2ecf20Sopenharmony_ci	 */
12208c2ecf20Sopenharmony_ci	if (edev->physfn && (edev->pe->state & EEH_PE_RESET))
12218c2ecf20Sopenharmony_ci		return false;
12228c2ecf20Sopenharmony_ci
12238c2ecf20Sopenharmony_ci	if (edev->pe->state & EEH_PE_CFG_BLOCKED)
12248c2ecf20Sopenharmony_ci		return true;
12258c2ecf20Sopenharmony_ci
12268c2ecf20Sopenharmony_ci	return false;
12278c2ecf20Sopenharmony_ci}
12288c2ecf20Sopenharmony_ci
12298c2ecf20Sopenharmony_cistatic int pnv_eeh_read_config(struct eeh_dev *edev,
12308c2ecf20Sopenharmony_ci			       int where, int size, u32 *val)
12318c2ecf20Sopenharmony_ci{
12328c2ecf20Sopenharmony_ci	struct pci_dn *pdn = eeh_dev_to_pdn(edev);
12338c2ecf20Sopenharmony_ci
12348c2ecf20Sopenharmony_ci	if (!pdn)
12358c2ecf20Sopenharmony_ci		return PCIBIOS_DEVICE_NOT_FOUND;
12368c2ecf20Sopenharmony_ci
12378c2ecf20Sopenharmony_ci	if (pnv_eeh_cfg_blocked(pdn)) {
12388c2ecf20Sopenharmony_ci		*val = 0xFFFFFFFF;
12398c2ecf20Sopenharmony_ci		return PCIBIOS_SET_FAILED;
12408c2ecf20Sopenharmony_ci	}
12418c2ecf20Sopenharmony_ci
12428c2ecf20Sopenharmony_ci	return pnv_pci_cfg_read(pdn, where, size, val);
12438c2ecf20Sopenharmony_ci}
12448c2ecf20Sopenharmony_ci
12458c2ecf20Sopenharmony_cistatic int pnv_eeh_write_config(struct eeh_dev *edev,
12468c2ecf20Sopenharmony_ci				int where, int size, u32 val)
12478c2ecf20Sopenharmony_ci{
12488c2ecf20Sopenharmony_ci	struct pci_dn *pdn = eeh_dev_to_pdn(edev);
12498c2ecf20Sopenharmony_ci
12508c2ecf20Sopenharmony_ci	if (!pdn)
12518c2ecf20Sopenharmony_ci		return PCIBIOS_DEVICE_NOT_FOUND;
12528c2ecf20Sopenharmony_ci
12538c2ecf20Sopenharmony_ci	if (pnv_eeh_cfg_blocked(pdn))
12548c2ecf20Sopenharmony_ci		return PCIBIOS_SET_FAILED;
12558c2ecf20Sopenharmony_ci
12568c2ecf20Sopenharmony_ci	return pnv_pci_cfg_write(pdn, where, size, val);
12578c2ecf20Sopenharmony_ci}
12588c2ecf20Sopenharmony_ci
12598c2ecf20Sopenharmony_cistatic void pnv_eeh_dump_hub_diag_common(struct OpalIoP7IOCErrorData *data)
12608c2ecf20Sopenharmony_ci{
12618c2ecf20Sopenharmony_ci	/* GEM */
12628c2ecf20Sopenharmony_ci	if (data->gemXfir || data->gemRfir ||
12638c2ecf20Sopenharmony_ci	    data->gemRirqfir || data->gemMask || data->gemRwof)
12648c2ecf20Sopenharmony_ci		pr_info("  GEM: %016llx %016llx %016llx %016llx %016llx\n",
12658c2ecf20Sopenharmony_ci			be64_to_cpu(data->gemXfir),
12668c2ecf20Sopenharmony_ci			be64_to_cpu(data->gemRfir),
12678c2ecf20Sopenharmony_ci			be64_to_cpu(data->gemRirqfir),
12688c2ecf20Sopenharmony_ci			be64_to_cpu(data->gemMask),
12698c2ecf20Sopenharmony_ci			be64_to_cpu(data->gemRwof));
12708c2ecf20Sopenharmony_ci
12718c2ecf20Sopenharmony_ci	/* LEM */
12728c2ecf20Sopenharmony_ci	if (data->lemFir || data->lemErrMask ||
12738c2ecf20Sopenharmony_ci	    data->lemAction0 || data->lemAction1 || data->lemWof)
12748c2ecf20Sopenharmony_ci		pr_info("  LEM: %016llx %016llx %016llx %016llx %016llx\n",
12758c2ecf20Sopenharmony_ci			be64_to_cpu(data->lemFir),
12768c2ecf20Sopenharmony_ci			be64_to_cpu(data->lemErrMask),
12778c2ecf20Sopenharmony_ci			be64_to_cpu(data->lemAction0),
12788c2ecf20Sopenharmony_ci			be64_to_cpu(data->lemAction1),
12798c2ecf20Sopenharmony_ci			be64_to_cpu(data->lemWof));
12808c2ecf20Sopenharmony_ci}
12818c2ecf20Sopenharmony_ci
12828c2ecf20Sopenharmony_cistatic void pnv_eeh_get_and_dump_hub_diag(struct pci_controller *hose)
12838c2ecf20Sopenharmony_ci{
12848c2ecf20Sopenharmony_ci	struct pnv_phb *phb = hose->private_data;
12858c2ecf20Sopenharmony_ci	struct OpalIoP7IOCErrorData *data =
12868c2ecf20Sopenharmony_ci		(struct OpalIoP7IOCErrorData*)phb->diag_data;
12878c2ecf20Sopenharmony_ci	long rc;
12888c2ecf20Sopenharmony_ci
12898c2ecf20Sopenharmony_ci	rc = opal_pci_get_hub_diag_data(phb->hub_id, data, sizeof(*data));
12908c2ecf20Sopenharmony_ci	if (rc != OPAL_SUCCESS) {
12918c2ecf20Sopenharmony_ci		pr_warn("%s: Failed to get HUB#%llx diag-data (%ld)\n",
12928c2ecf20Sopenharmony_ci			__func__, phb->hub_id, rc);
12938c2ecf20Sopenharmony_ci		return;
12948c2ecf20Sopenharmony_ci	}
12958c2ecf20Sopenharmony_ci
12968c2ecf20Sopenharmony_ci	switch (be16_to_cpu(data->type)) {
12978c2ecf20Sopenharmony_ci	case OPAL_P7IOC_DIAG_TYPE_RGC:
12988c2ecf20Sopenharmony_ci		pr_info("P7IOC diag-data for RGC\n\n");
12998c2ecf20Sopenharmony_ci		pnv_eeh_dump_hub_diag_common(data);
13008c2ecf20Sopenharmony_ci		if (data->rgc.rgcStatus || data->rgc.rgcLdcp)
13018c2ecf20Sopenharmony_ci			pr_info("  RGC: %016llx %016llx\n",
13028c2ecf20Sopenharmony_ci				be64_to_cpu(data->rgc.rgcStatus),
13038c2ecf20Sopenharmony_ci				be64_to_cpu(data->rgc.rgcLdcp));
13048c2ecf20Sopenharmony_ci		break;
13058c2ecf20Sopenharmony_ci	case OPAL_P7IOC_DIAG_TYPE_BI:
13068c2ecf20Sopenharmony_ci		pr_info("P7IOC diag-data for BI %s\n\n",
13078c2ecf20Sopenharmony_ci			data->bi.biDownbound ? "Downbound" : "Upbound");
13088c2ecf20Sopenharmony_ci		pnv_eeh_dump_hub_diag_common(data);
13098c2ecf20Sopenharmony_ci		if (data->bi.biLdcp0 || data->bi.biLdcp1 ||
13108c2ecf20Sopenharmony_ci		    data->bi.biLdcp2 || data->bi.biFenceStatus)
13118c2ecf20Sopenharmony_ci			pr_info("  BI:  %016llx %016llx %016llx %016llx\n",
13128c2ecf20Sopenharmony_ci				be64_to_cpu(data->bi.biLdcp0),
13138c2ecf20Sopenharmony_ci				be64_to_cpu(data->bi.biLdcp1),
13148c2ecf20Sopenharmony_ci				be64_to_cpu(data->bi.biLdcp2),
13158c2ecf20Sopenharmony_ci				be64_to_cpu(data->bi.biFenceStatus));
13168c2ecf20Sopenharmony_ci		break;
13178c2ecf20Sopenharmony_ci	case OPAL_P7IOC_DIAG_TYPE_CI:
13188c2ecf20Sopenharmony_ci		pr_info("P7IOC diag-data for CI Port %d\n\n",
13198c2ecf20Sopenharmony_ci			data->ci.ciPort);
13208c2ecf20Sopenharmony_ci		pnv_eeh_dump_hub_diag_common(data);
13218c2ecf20Sopenharmony_ci		if (data->ci.ciPortStatus || data->ci.ciPortLdcp)
13228c2ecf20Sopenharmony_ci			pr_info("  CI:  %016llx %016llx\n",
13238c2ecf20Sopenharmony_ci				be64_to_cpu(data->ci.ciPortStatus),
13248c2ecf20Sopenharmony_ci				be64_to_cpu(data->ci.ciPortLdcp));
13258c2ecf20Sopenharmony_ci		break;
13268c2ecf20Sopenharmony_ci	case OPAL_P7IOC_DIAG_TYPE_MISC:
13278c2ecf20Sopenharmony_ci		pr_info("P7IOC diag-data for MISC\n\n");
13288c2ecf20Sopenharmony_ci		pnv_eeh_dump_hub_diag_common(data);
13298c2ecf20Sopenharmony_ci		break;
13308c2ecf20Sopenharmony_ci	case OPAL_P7IOC_DIAG_TYPE_I2C:
13318c2ecf20Sopenharmony_ci		pr_info("P7IOC diag-data for I2C\n\n");
13328c2ecf20Sopenharmony_ci		pnv_eeh_dump_hub_diag_common(data);
13338c2ecf20Sopenharmony_ci		break;
13348c2ecf20Sopenharmony_ci	default:
13358c2ecf20Sopenharmony_ci		pr_warn("%s: Invalid type of HUB#%llx diag-data (%d)\n",
13368c2ecf20Sopenharmony_ci			__func__, phb->hub_id, data->type);
13378c2ecf20Sopenharmony_ci	}
13388c2ecf20Sopenharmony_ci}
13398c2ecf20Sopenharmony_ci
13408c2ecf20Sopenharmony_cistatic int pnv_eeh_get_pe(struct pci_controller *hose,
13418c2ecf20Sopenharmony_ci			  u16 pe_no, struct eeh_pe **pe)
13428c2ecf20Sopenharmony_ci{
13438c2ecf20Sopenharmony_ci	struct pnv_phb *phb = hose->private_data;
13448c2ecf20Sopenharmony_ci	struct pnv_ioda_pe *pnv_pe;
13458c2ecf20Sopenharmony_ci	struct eeh_pe *dev_pe;
13468c2ecf20Sopenharmony_ci
13478c2ecf20Sopenharmony_ci	/*
13488c2ecf20Sopenharmony_ci	 * If PHB supports compound PE, to fetch
13498c2ecf20Sopenharmony_ci	 * the master PE because slave PE is invisible
13508c2ecf20Sopenharmony_ci	 * to EEH core.
13518c2ecf20Sopenharmony_ci	 */
13528c2ecf20Sopenharmony_ci	pnv_pe = &phb->ioda.pe_array[pe_no];
13538c2ecf20Sopenharmony_ci	if (pnv_pe->flags & PNV_IODA_PE_SLAVE) {
13548c2ecf20Sopenharmony_ci		pnv_pe = pnv_pe->master;
13558c2ecf20Sopenharmony_ci		WARN_ON(!pnv_pe ||
13568c2ecf20Sopenharmony_ci			!(pnv_pe->flags & PNV_IODA_PE_MASTER));
13578c2ecf20Sopenharmony_ci		pe_no = pnv_pe->pe_number;
13588c2ecf20Sopenharmony_ci	}
13598c2ecf20Sopenharmony_ci
13608c2ecf20Sopenharmony_ci	/* Find the PE according to PE# */
13618c2ecf20Sopenharmony_ci	dev_pe = eeh_pe_get(hose, pe_no);
13628c2ecf20Sopenharmony_ci	if (!dev_pe)
13638c2ecf20Sopenharmony_ci		return -EEXIST;
13648c2ecf20Sopenharmony_ci
13658c2ecf20Sopenharmony_ci	/* Freeze the (compound) PE */
13668c2ecf20Sopenharmony_ci	*pe = dev_pe;
13678c2ecf20Sopenharmony_ci	if (!(dev_pe->state & EEH_PE_ISOLATED))
13688c2ecf20Sopenharmony_ci		phb->freeze_pe(phb, pe_no);
13698c2ecf20Sopenharmony_ci
13708c2ecf20Sopenharmony_ci	/*
13718c2ecf20Sopenharmony_ci	 * At this point, we're sure the (compound) PE should
13728c2ecf20Sopenharmony_ci	 * have been frozen. However, we still need poke until
13738c2ecf20Sopenharmony_ci	 * hitting the frozen PE on top level.
13748c2ecf20Sopenharmony_ci	 */
13758c2ecf20Sopenharmony_ci	dev_pe = dev_pe->parent;
13768c2ecf20Sopenharmony_ci	while (dev_pe && !(dev_pe->type & EEH_PE_PHB)) {
13778c2ecf20Sopenharmony_ci		int ret;
13788c2ecf20Sopenharmony_ci		ret = eeh_ops->get_state(dev_pe, NULL);
13798c2ecf20Sopenharmony_ci		if (ret <= 0 || eeh_state_active(ret)) {
13808c2ecf20Sopenharmony_ci			dev_pe = dev_pe->parent;
13818c2ecf20Sopenharmony_ci			continue;
13828c2ecf20Sopenharmony_ci		}
13838c2ecf20Sopenharmony_ci
13848c2ecf20Sopenharmony_ci		/* Frozen parent PE */
13858c2ecf20Sopenharmony_ci		*pe = dev_pe;
13868c2ecf20Sopenharmony_ci		if (!(dev_pe->state & EEH_PE_ISOLATED))
13878c2ecf20Sopenharmony_ci			phb->freeze_pe(phb, dev_pe->addr);
13888c2ecf20Sopenharmony_ci
13898c2ecf20Sopenharmony_ci		/* Next one */
13908c2ecf20Sopenharmony_ci		dev_pe = dev_pe->parent;
13918c2ecf20Sopenharmony_ci	}
13928c2ecf20Sopenharmony_ci
13938c2ecf20Sopenharmony_ci	return 0;
13948c2ecf20Sopenharmony_ci}
13958c2ecf20Sopenharmony_ci
13968c2ecf20Sopenharmony_ci/**
13978c2ecf20Sopenharmony_ci * pnv_eeh_next_error - Retrieve next EEH error to handle
13988c2ecf20Sopenharmony_ci * @pe: Affected PE
13998c2ecf20Sopenharmony_ci *
14008c2ecf20Sopenharmony_ci * The function is expected to be called by EEH core while it gets
14018c2ecf20Sopenharmony_ci * special EEH event (without binding PE). The function calls to
14028c2ecf20Sopenharmony_ci * OPAL APIs for next error to handle. The informational error is
14038c2ecf20Sopenharmony_ci * handled internally by platform. However, the dead IOC, dead PHB,
14048c2ecf20Sopenharmony_ci * fenced PHB and frozen PE should be handled by EEH core eventually.
14058c2ecf20Sopenharmony_ci */
14068c2ecf20Sopenharmony_cistatic int pnv_eeh_next_error(struct eeh_pe **pe)
14078c2ecf20Sopenharmony_ci{
14088c2ecf20Sopenharmony_ci	struct pci_controller *hose;
14098c2ecf20Sopenharmony_ci	struct pnv_phb *phb;
14108c2ecf20Sopenharmony_ci	struct eeh_pe *phb_pe, *parent_pe;
14118c2ecf20Sopenharmony_ci	__be64 frozen_pe_no;
14128c2ecf20Sopenharmony_ci	__be16 err_type, severity;
14138c2ecf20Sopenharmony_ci	long rc;
14148c2ecf20Sopenharmony_ci	int state, ret = EEH_NEXT_ERR_NONE;
14158c2ecf20Sopenharmony_ci
14168c2ecf20Sopenharmony_ci	/*
14178c2ecf20Sopenharmony_ci	 * While running here, it's safe to purge the event queue. The
14188c2ecf20Sopenharmony_ci	 * event should still be masked.
14198c2ecf20Sopenharmony_ci	 */
14208c2ecf20Sopenharmony_ci	eeh_remove_event(NULL, false);
14218c2ecf20Sopenharmony_ci
14228c2ecf20Sopenharmony_ci	list_for_each_entry(hose, &hose_list, list_node) {
14238c2ecf20Sopenharmony_ci		/*
14248c2ecf20Sopenharmony_ci		 * If the subordinate PCI buses of the PHB has been
14258c2ecf20Sopenharmony_ci		 * removed or is exactly under error recovery, we
14268c2ecf20Sopenharmony_ci		 * needn't take care of it any more.
14278c2ecf20Sopenharmony_ci		 */
14288c2ecf20Sopenharmony_ci		phb = hose->private_data;
14298c2ecf20Sopenharmony_ci		phb_pe = eeh_phb_pe_get(hose);
14308c2ecf20Sopenharmony_ci		if (!phb_pe || (phb_pe->state & EEH_PE_ISOLATED))
14318c2ecf20Sopenharmony_ci			continue;
14328c2ecf20Sopenharmony_ci
14338c2ecf20Sopenharmony_ci		rc = opal_pci_next_error(phb->opal_id,
14348c2ecf20Sopenharmony_ci					 &frozen_pe_no, &err_type, &severity);
14358c2ecf20Sopenharmony_ci		if (rc != OPAL_SUCCESS) {
14368c2ecf20Sopenharmony_ci			pr_devel("%s: Invalid return value on "
14378c2ecf20Sopenharmony_ci				 "PHB#%x (0x%lx) from opal_pci_next_error",
14388c2ecf20Sopenharmony_ci				 __func__, hose->global_number, rc);
14398c2ecf20Sopenharmony_ci			continue;
14408c2ecf20Sopenharmony_ci		}
14418c2ecf20Sopenharmony_ci
14428c2ecf20Sopenharmony_ci		/* If the PHB doesn't have error, stop processing */
14438c2ecf20Sopenharmony_ci		if (be16_to_cpu(err_type) == OPAL_EEH_NO_ERROR ||
14448c2ecf20Sopenharmony_ci		    be16_to_cpu(severity) == OPAL_EEH_SEV_NO_ERROR) {
14458c2ecf20Sopenharmony_ci			pr_devel("%s: No error found on PHB#%x\n",
14468c2ecf20Sopenharmony_ci				 __func__, hose->global_number);
14478c2ecf20Sopenharmony_ci			continue;
14488c2ecf20Sopenharmony_ci		}
14498c2ecf20Sopenharmony_ci
14508c2ecf20Sopenharmony_ci		/*
14518c2ecf20Sopenharmony_ci		 * Processing the error. We're expecting the error with
14528c2ecf20Sopenharmony_ci		 * highest priority reported upon multiple errors on the
14538c2ecf20Sopenharmony_ci		 * specific PHB.
14548c2ecf20Sopenharmony_ci		 */
14558c2ecf20Sopenharmony_ci		pr_devel("%s: Error (%d, %d, %llu) on PHB#%x\n",
14568c2ecf20Sopenharmony_ci			__func__, be16_to_cpu(err_type),
14578c2ecf20Sopenharmony_ci			be16_to_cpu(severity), be64_to_cpu(frozen_pe_no),
14588c2ecf20Sopenharmony_ci			hose->global_number);
14598c2ecf20Sopenharmony_ci		switch (be16_to_cpu(err_type)) {
14608c2ecf20Sopenharmony_ci		case OPAL_EEH_IOC_ERROR:
14618c2ecf20Sopenharmony_ci			if (be16_to_cpu(severity) == OPAL_EEH_SEV_IOC_DEAD) {
14628c2ecf20Sopenharmony_ci				pr_err("EEH: dead IOC detected\n");
14638c2ecf20Sopenharmony_ci				ret = EEH_NEXT_ERR_DEAD_IOC;
14648c2ecf20Sopenharmony_ci			} else if (be16_to_cpu(severity) == OPAL_EEH_SEV_INF) {
14658c2ecf20Sopenharmony_ci				pr_info("EEH: IOC informative error "
14668c2ecf20Sopenharmony_ci					"detected\n");
14678c2ecf20Sopenharmony_ci				pnv_eeh_get_and_dump_hub_diag(hose);
14688c2ecf20Sopenharmony_ci				ret = EEH_NEXT_ERR_NONE;
14698c2ecf20Sopenharmony_ci			}
14708c2ecf20Sopenharmony_ci
14718c2ecf20Sopenharmony_ci			break;
14728c2ecf20Sopenharmony_ci		case OPAL_EEH_PHB_ERROR:
14738c2ecf20Sopenharmony_ci			if (be16_to_cpu(severity) == OPAL_EEH_SEV_PHB_DEAD) {
14748c2ecf20Sopenharmony_ci				*pe = phb_pe;
14758c2ecf20Sopenharmony_ci				pr_err("EEH: dead PHB#%x detected, "
14768c2ecf20Sopenharmony_ci				       "location: %s\n",
14778c2ecf20Sopenharmony_ci					hose->global_number,
14788c2ecf20Sopenharmony_ci					eeh_pe_loc_get(phb_pe));
14798c2ecf20Sopenharmony_ci				ret = EEH_NEXT_ERR_DEAD_PHB;
14808c2ecf20Sopenharmony_ci			} else if (be16_to_cpu(severity) ==
14818c2ecf20Sopenharmony_ci				   OPAL_EEH_SEV_PHB_FENCED) {
14828c2ecf20Sopenharmony_ci				*pe = phb_pe;
14838c2ecf20Sopenharmony_ci				pr_err("EEH: Fenced PHB#%x detected, "
14848c2ecf20Sopenharmony_ci				       "location: %s\n",
14858c2ecf20Sopenharmony_ci					hose->global_number,
14868c2ecf20Sopenharmony_ci					eeh_pe_loc_get(phb_pe));
14878c2ecf20Sopenharmony_ci				ret = EEH_NEXT_ERR_FENCED_PHB;
14888c2ecf20Sopenharmony_ci			} else if (be16_to_cpu(severity) == OPAL_EEH_SEV_INF) {
14898c2ecf20Sopenharmony_ci				pr_info("EEH: PHB#%x informative error "
14908c2ecf20Sopenharmony_ci					"detected, location: %s\n",
14918c2ecf20Sopenharmony_ci					hose->global_number,
14928c2ecf20Sopenharmony_ci					eeh_pe_loc_get(phb_pe));
14938c2ecf20Sopenharmony_ci				pnv_eeh_get_phb_diag(phb_pe);
14948c2ecf20Sopenharmony_ci				pnv_pci_dump_phb_diag_data(hose, phb_pe->data);
14958c2ecf20Sopenharmony_ci				ret = EEH_NEXT_ERR_NONE;
14968c2ecf20Sopenharmony_ci			}
14978c2ecf20Sopenharmony_ci
14988c2ecf20Sopenharmony_ci			break;
14998c2ecf20Sopenharmony_ci		case OPAL_EEH_PE_ERROR:
15008c2ecf20Sopenharmony_ci			/*
15018c2ecf20Sopenharmony_ci			 * If we can't find the corresponding PE, we
15028c2ecf20Sopenharmony_ci			 * just try to unfreeze.
15038c2ecf20Sopenharmony_ci			 */
15048c2ecf20Sopenharmony_ci			if (pnv_eeh_get_pe(hose,
15058c2ecf20Sopenharmony_ci				be64_to_cpu(frozen_pe_no), pe)) {
15068c2ecf20Sopenharmony_ci				pr_info("EEH: Clear non-existing PHB#%x-PE#%llx\n",
15078c2ecf20Sopenharmony_ci					hose->global_number, be64_to_cpu(frozen_pe_no));
15088c2ecf20Sopenharmony_ci				pr_info("EEH: PHB location: %s\n",
15098c2ecf20Sopenharmony_ci					eeh_pe_loc_get(phb_pe));
15108c2ecf20Sopenharmony_ci
15118c2ecf20Sopenharmony_ci				/* Dump PHB diag-data */
15128c2ecf20Sopenharmony_ci				rc = opal_pci_get_phb_diag_data2(phb->opal_id,
15138c2ecf20Sopenharmony_ci					phb->diag_data, phb->diag_data_size);
15148c2ecf20Sopenharmony_ci				if (rc == OPAL_SUCCESS)
15158c2ecf20Sopenharmony_ci					pnv_pci_dump_phb_diag_data(hose,
15168c2ecf20Sopenharmony_ci							phb->diag_data);
15178c2ecf20Sopenharmony_ci
15188c2ecf20Sopenharmony_ci				/* Try best to clear it */
15198c2ecf20Sopenharmony_ci				opal_pci_eeh_freeze_clear(phb->opal_id,
15208c2ecf20Sopenharmony_ci					be64_to_cpu(frozen_pe_no),
15218c2ecf20Sopenharmony_ci					OPAL_EEH_ACTION_CLEAR_FREEZE_ALL);
15228c2ecf20Sopenharmony_ci				ret = EEH_NEXT_ERR_NONE;
15238c2ecf20Sopenharmony_ci			} else if ((*pe)->state & EEH_PE_ISOLATED ||
15248c2ecf20Sopenharmony_ci				   eeh_pe_passed(*pe)) {
15258c2ecf20Sopenharmony_ci				ret = EEH_NEXT_ERR_NONE;
15268c2ecf20Sopenharmony_ci			} else {
15278c2ecf20Sopenharmony_ci				pr_err("EEH: Frozen PE#%x "
15288c2ecf20Sopenharmony_ci				       "on PHB#%x detected\n",
15298c2ecf20Sopenharmony_ci				       (*pe)->addr,
15308c2ecf20Sopenharmony_ci					(*pe)->phb->global_number);
15318c2ecf20Sopenharmony_ci				pr_err("EEH: PE location: %s, "
15328c2ecf20Sopenharmony_ci				       "PHB location: %s\n",
15338c2ecf20Sopenharmony_ci				       eeh_pe_loc_get(*pe),
15348c2ecf20Sopenharmony_ci				       eeh_pe_loc_get(phb_pe));
15358c2ecf20Sopenharmony_ci				ret = EEH_NEXT_ERR_FROZEN_PE;
15368c2ecf20Sopenharmony_ci			}
15378c2ecf20Sopenharmony_ci
15388c2ecf20Sopenharmony_ci			break;
15398c2ecf20Sopenharmony_ci		default:
15408c2ecf20Sopenharmony_ci			pr_warn("%s: Unexpected error type %d\n",
15418c2ecf20Sopenharmony_ci				__func__, be16_to_cpu(err_type));
15428c2ecf20Sopenharmony_ci		}
15438c2ecf20Sopenharmony_ci
15448c2ecf20Sopenharmony_ci		/*
15458c2ecf20Sopenharmony_ci		 * EEH core will try recover from fenced PHB or
15468c2ecf20Sopenharmony_ci		 * frozen PE. In the time for frozen PE, EEH core
15478c2ecf20Sopenharmony_ci		 * enable IO path for that before collecting logs,
15488c2ecf20Sopenharmony_ci		 * but it ruins the site. So we have to dump the
15498c2ecf20Sopenharmony_ci		 * log in advance here.
15508c2ecf20Sopenharmony_ci		 */
15518c2ecf20Sopenharmony_ci		if ((ret == EEH_NEXT_ERR_FROZEN_PE  ||
15528c2ecf20Sopenharmony_ci		    ret == EEH_NEXT_ERR_FENCED_PHB) &&
15538c2ecf20Sopenharmony_ci		    !((*pe)->state & EEH_PE_ISOLATED)) {
15548c2ecf20Sopenharmony_ci			eeh_pe_mark_isolated(*pe);
15558c2ecf20Sopenharmony_ci			pnv_eeh_get_phb_diag(*pe);
15568c2ecf20Sopenharmony_ci
15578c2ecf20Sopenharmony_ci			if (eeh_has_flag(EEH_EARLY_DUMP_LOG))
15588c2ecf20Sopenharmony_ci				pnv_pci_dump_phb_diag_data((*pe)->phb,
15598c2ecf20Sopenharmony_ci							   (*pe)->data);
15608c2ecf20Sopenharmony_ci		}
15618c2ecf20Sopenharmony_ci
15628c2ecf20Sopenharmony_ci		/*
15638c2ecf20Sopenharmony_ci		 * We probably have the frozen parent PE out there and
15648c2ecf20Sopenharmony_ci		 * we need have to handle frozen parent PE firstly.
15658c2ecf20Sopenharmony_ci		 */
15668c2ecf20Sopenharmony_ci		if (ret == EEH_NEXT_ERR_FROZEN_PE) {
15678c2ecf20Sopenharmony_ci			parent_pe = (*pe)->parent;
15688c2ecf20Sopenharmony_ci			while (parent_pe) {
15698c2ecf20Sopenharmony_ci				/* Hit the ceiling ? */
15708c2ecf20Sopenharmony_ci				if (parent_pe->type & EEH_PE_PHB)
15718c2ecf20Sopenharmony_ci					break;
15728c2ecf20Sopenharmony_ci
15738c2ecf20Sopenharmony_ci				/* Frozen parent PE ? */
15748c2ecf20Sopenharmony_ci				state = eeh_ops->get_state(parent_pe, NULL);
15758c2ecf20Sopenharmony_ci				if (state > 0 && !eeh_state_active(state))
15768c2ecf20Sopenharmony_ci					*pe = parent_pe;
15778c2ecf20Sopenharmony_ci
15788c2ecf20Sopenharmony_ci				/* Next parent level */
15798c2ecf20Sopenharmony_ci				parent_pe = parent_pe->parent;
15808c2ecf20Sopenharmony_ci			}
15818c2ecf20Sopenharmony_ci
15828c2ecf20Sopenharmony_ci			/* We possibly migrate to another PE */
15838c2ecf20Sopenharmony_ci			eeh_pe_mark_isolated(*pe);
15848c2ecf20Sopenharmony_ci		}
15858c2ecf20Sopenharmony_ci
15868c2ecf20Sopenharmony_ci		/*
15878c2ecf20Sopenharmony_ci		 * If we have no errors on the specific PHB or only
15888c2ecf20Sopenharmony_ci		 * informative error there, we continue poking it.
15898c2ecf20Sopenharmony_ci		 * Otherwise, we need actions to be taken by upper
15908c2ecf20Sopenharmony_ci		 * layer.
15918c2ecf20Sopenharmony_ci		 */
15928c2ecf20Sopenharmony_ci		if (ret > EEH_NEXT_ERR_INF)
15938c2ecf20Sopenharmony_ci			break;
15948c2ecf20Sopenharmony_ci	}
15958c2ecf20Sopenharmony_ci
15968c2ecf20Sopenharmony_ci	/* Unmask the event */
15978c2ecf20Sopenharmony_ci	if (ret == EEH_NEXT_ERR_NONE && eeh_enabled())
15988c2ecf20Sopenharmony_ci		enable_irq(eeh_event_irq);
15998c2ecf20Sopenharmony_ci
16008c2ecf20Sopenharmony_ci	return ret;
16018c2ecf20Sopenharmony_ci}
16028c2ecf20Sopenharmony_ci
16038c2ecf20Sopenharmony_cistatic int pnv_eeh_restore_config(struct eeh_dev *edev)
16048c2ecf20Sopenharmony_ci{
16058c2ecf20Sopenharmony_ci	struct pnv_phb *phb;
16068c2ecf20Sopenharmony_ci	s64 ret = 0;
16078c2ecf20Sopenharmony_ci
16088c2ecf20Sopenharmony_ci	if (!edev)
16098c2ecf20Sopenharmony_ci		return -EEXIST;
16108c2ecf20Sopenharmony_ci
16118c2ecf20Sopenharmony_ci	if (edev->physfn)
16128c2ecf20Sopenharmony_ci		return 0;
16138c2ecf20Sopenharmony_ci
16148c2ecf20Sopenharmony_ci	phb = edev->controller->private_data;
16158c2ecf20Sopenharmony_ci	ret = opal_pci_reinit(phb->opal_id,
16168c2ecf20Sopenharmony_ci			      OPAL_REINIT_PCI_DEV, edev->bdfn);
16178c2ecf20Sopenharmony_ci
16188c2ecf20Sopenharmony_ci	if (ret) {
16198c2ecf20Sopenharmony_ci		pr_warn("%s: Can't reinit PCI dev 0x%x (%lld)\n",
16208c2ecf20Sopenharmony_ci			__func__, edev->bdfn, ret);
16218c2ecf20Sopenharmony_ci		return -EIO;
16228c2ecf20Sopenharmony_ci	}
16238c2ecf20Sopenharmony_ci
16248c2ecf20Sopenharmony_ci	return ret;
16258c2ecf20Sopenharmony_ci}
16268c2ecf20Sopenharmony_ci
16278c2ecf20Sopenharmony_cistatic struct eeh_ops pnv_eeh_ops = {
16288c2ecf20Sopenharmony_ci	.name                   = "powernv",
16298c2ecf20Sopenharmony_ci	.probe			= pnv_eeh_probe,
16308c2ecf20Sopenharmony_ci	.set_option             = pnv_eeh_set_option,
16318c2ecf20Sopenharmony_ci	.get_state              = pnv_eeh_get_state,
16328c2ecf20Sopenharmony_ci	.reset                  = pnv_eeh_reset,
16338c2ecf20Sopenharmony_ci	.get_log                = pnv_eeh_get_log,
16348c2ecf20Sopenharmony_ci	.configure_bridge       = pnv_eeh_configure_bridge,
16358c2ecf20Sopenharmony_ci	.err_inject		= pnv_eeh_err_inject,
16368c2ecf20Sopenharmony_ci	.read_config            = pnv_eeh_read_config,
16378c2ecf20Sopenharmony_ci	.write_config           = pnv_eeh_write_config,
16388c2ecf20Sopenharmony_ci	.next_error		= pnv_eeh_next_error,
16398c2ecf20Sopenharmony_ci	.restore_config		= pnv_eeh_restore_config,
16408c2ecf20Sopenharmony_ci	.notify_resume		= NULL
16418c2ecf20Sopenharmony_ci};
16428c2ecf20Sopenharmony_ci
16438c2ecf20Sopenharmony_ci#ifdef CONFIG_PCI_IOV
16448c2ecf20Sopenharmony_cistatic void pnv_pci_fixup_vf_mps(struct pci_dev *pdev)
16458c2ecf20Sopenharmony_ci{
16468c2ecf20Sopenharmony_ci	struct pci_dn *pdn = pci_get_pdn(pdev);
16478c2ecf20Sopenharmony_ci	int parent_mps;
16488c2ecf20Sopenharmony_ci
16498c2ecf20Sopenharmony_ci	if (!pdev->is_virtfn)
16508c2ecf20Sopenharmony_ci		return;
16518c2ecf20Sopenharmony_ci
16528c2ecf20Sopenharmony_ci	/* Synchronize MPS for VF and PF */
16538c2ecf20Sopenharmony_ci	parent_mps = pcie_get_mps(pdev->physfn);
16548c2ecf20Sopenharmony_ci	if ((128 << pdev->pcie_mpss) >= parent_mps)
16558c2ecf20Sopenharmony_ci		pcie_set_mps(pdev, parent_mps);
16568c2ecf20Sopenharmony_ci	pdn->mps = pcie_get_mps(pdev);
16578c2ecf20Sopenharmony_ci}
16588c2ecf20Sopenharmony_ciDECLARE_PCI_FIXUP_HEADER(PCI_ANY_ID, PCI_ANY_ID, pnv_pci_fixup_vf_mps);
16598c2ecf20Sopenharmony_ci#endif /* CONFIG_PCI_IOV */
16608c2ecf20Sopenharmony_ci
16618c2ecf20Sopenharmony_ci/**
16628c2ecf20Sopenharmony_ci * eeh_powernv_init - Register platform dependent EEH operations
16638c2ecf20Sopenharmony_ci *
16648c2ecf20Sopenharmony_ci * EEH initialization on powernv platform. This function should be
16658c2ecf20Sopenharmony_ci * called before any EEH related functions.
16668c2ecf20Sopenharmony_ci */
16678c2ecf20Sopenharmony_cistatic int __init eeh_powernv_init(void)
16688c2ecf20Sopenharmony_ci{
16698c2ecf20Sopenharmony_ci	int max_diag_size = PNV_PCI_DIAG_BUF_SIZE;
16708c2ecf20Sopenharmony_ci	struct pci_controller *hose;
16718c2ecf20Sopenharmony_ci	struct pnv_phb *phb;
16728c2ecf20Sopenharmony_ci	int ret = -EINVAL;
16738c2ecf20Sopenharmony_ci
16748c2ecf20Sopenharmony_ci	if (!firmware_has_feature(FW_FEATURE_OPAL)) {
16758c2ecf20Sopenharmony_ci		pr_warn("%s: OPAL is required !\n", __func__);
16768c2ecf20Sopenharmony_ci		return -EINVAL;
16778c2ecf20Sopenharmony_ci	}
16788c2ecf20Sopenharmony_ci
16798c2ecf20Sopenharmony_ci	/* Set probe mode */
16808c2ecf20Sopenharmony_ci	eeh_add_flag(EEH_PROBE_MODE_DEV);
16818c2ecf20Sopenharmony_ci
16828c2ecf20Sopenharmony_ci	/*
16838c2ecf20Sopenharmony_ci	 * P7IOC blocks PCI config access to frozen PE, but PHB3
16848c2ecf20Sopenharmony_ci	 * doesn't do that. So we have to selectively enable I/O
16858c2ecf20Sopenharmony_ci	 * prior to collecting error log.
16868c2ecf20Sopenharmony_ci	 */
16878c2ecf20Sopenharmony_ci	list_for_each_entry(hose, &hose_list, list_node) {
16888c2ecf20Sopenharmony_ci		phb = hose->private_data;
16898c2ecf20Sopenharmony_ci
16908c2ecf20Sopenharmony_ci		if (phb->model == PNV_PHB_MODEL_P7IOC)
16918c2ecf20Sopenharmony_ci			eeh_add_flag(EEH_ENABLE_IO_FOR_LOG);
16928c2ecf20Sopenharmony_ci
16938c2ecf20Sopenharmony_ci		if (phb->diag_data_size > max_diag_size)
16948c2ecf20Sopenharmony_ci			max_diag_size = phb->diag_data_size;
16958c2ecf20Sopenharmony_ci
16968c2ecf20Sopenharmony_ci		break;
16978c2ecf20Sopenharmony_ci	}
16988c2ecf20Sopenharmony_ci
16998c2ecf20Sopenharmony_ci	/*
17008c2ecf20Sopenharmony_ci	 * eeh_init() allocates the eeh_pe and its aux data buf so the
17018c2ecf20Sopenharmony_ci	 * size needs to be set before calling eeh_init().
17028c2ecf20Sopenharmony_ci	 */
17038c2ecf20Sopenharmony_ci	eeh_set_pe_aux_size(max_diag_size);
17048c2ecf20Sopenharmony_ci	ppc_md.pcibios_bus_add_device = pnv_pcibios_bus_add_device;
17058c2ecf20Sopenharmony_ci
17068c2ecf20Sopenharmony_ci	ret = eeh_init(&pnv_eeh_ops);
17078c2ecf20Sopenharmony_ci	if (!ret)
17088c2ecf20Sopenharmony_ci		pr_info("EEH: PowerNV platform initialized\n");
17098c2ecf20Sopenharmony_ci	else
17108c2ecf20Sopenharmony_ci		pr_info("EEH: Failed to initialize PowerNV platform (%d)\n", ret);
17118c2ecf20Sopenharmony_ci
17128c2ecf20Sopenharmony_ci	return ret;
17138c2ecf20Sopenharmony_ci}
17148c2ecf20Sopenharmony_cimachine_arch_initcall(powernv, eeh_powernv_init);
1715