162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Implement the AER root port service driver. The driver registers an IRQ
462306a36Sopenharmony_ci * handler. When a root port triggers an AER interrupt, the IRQ handler
562306a36Sopenharmony_ci * collects root port status and schedules work.
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Copyright (C) 2006 Intel Corp.
862306a36Sopenharmony_ci *	Tom Long Nguyen (tom.l.nguyen@intel.com)
962306a36Sopenharmony_ci *	Zhang Yanmin (yanmin.zhang@intel.com)
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci * (C) Copyright 2009 Hewlett-Packard Development Company, L.P.
1262306a36Sopenharmony_ci *    Andrew Patterson <andrew.patterson@hp.com>
1362306a36Sopenharmony_ci */
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#define pr_fmt(fmt) "AER: " fmt
1662306a36Sopenharmony_ci#define dev_fmt pr_fmt
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci#include <linux/bitops.h>
1962306a36Sopenharmony_ci#include <linux/cper.h>
2062306a36Sopenharmony_ci#include <linux/pci.h>
2162306a36Sopenharmony_ci#include <linux/pci-acpi.h>
2262306a36Sopenharmony_ci#include <linux/sched.h>
2362306a36Sopenharmony_ci#include <linux/kernel.h>
2462306a36Sopenharmony_ci#include <linux/errno.h>
2562306a36Sopenharmony_ci#include <linux/pm.h>
2662306a36Sopenharmony_ci#include <linux/init.h>
2762306a36Sopenharmony_ci#include <linux/interrupt.h>
2862306a36Sopenharmony_ci#include <linux/delay.h>
2962306a36Sopenharmony_ci#include <linux/kfifo.h>
3062306a36Sopenharmony_ci#include <linux/slab.h>
3162306a36Sopenharmony_ci#include <acpi/apei.h>
3262306a36Sopenharmony_ci#include <acpi/ghes.h>
3362306a36Sopenharmony_ci#include <ras/ras_event.h>
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci#include "../pci.h"
3662306a36Sopenharmony_ci#include "portdrv.h"
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci#define AER_ERROR_SOURCES_MAX		128
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci#define AER_MAX_TYPEOF_COR_ERRS		16	/* as per PCI_ERR_COR_STATUS */
4162306a36Sopenharmony_ci#define AER_MAX_TYPEOF_UNCOR_ERRS	27	/* as per PCI_ERR_UNCOR_STATUS*/
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_cistruct aer_err_source {
4462306a36Sopenharmony_ci	unsigned int status;
4562306a36Sopenharmony_ci	unsigned int id;
4662306a36Sopenharmony_ci};
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_cistruct aer_rpc {
4962306a36Sopenharmony_ci	struct pci_dev *rpd;		/* Root Port device */
5062306a36Sopenharmony_ci	DECLARE_KFIFO(aer_fifo, struct aer_err_source, AER_ERROR_SOURCES_MAX);
5162306a36Sopenharmony_ci};
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci/* AER stats for the device */
5462306a36Sopenharmony_cistruct aer_stats {
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci	/*
5762306a36Sopenharmony_ci	 * Fields for all AER capable devices. They indicate the errors
5862306a36Sopenharmony_ci	 * "as seen by this device". Note that this may mean that if an
5962306a36Sopenharmony_ci	 * end point is causing problems, the AER counters may increment
6062306a36Sopenharmony_ci	 * at its link partner (e.g. root port) because the errors will be
6162306a36Sopenharmony_ci	 * "seen" by the link partner and not the problematic end point
6262306a36Sopenharmony_ci	 * itself (which may report all counters as 0 as it never saw any
6362306a36Sopenharmony_ci	 * problems).
6462306a36Sopenharmony_ci	 */
6562306a36Sopenharmony_ci	/* Counters for different type of correctable errors */
6662306a36Sopenharmony_ci	u64 dev_cor_errs[AER_MAX_TYPEOF_COR_ERRS];
6762306a36Sopenharmony_ci	/* Counters for different type of fatal uncorrectable errors */
6862306a36Sopenharmony_ci	u64 dev_fatal_errs[AER_MAX_TYPEOF_UNCOR_ERRS];
6962306a36Sopenharmony_ci	/* Counters for different type of nonfatal uncorrectable errors */
7062306a36Sopenharmony_ci	u64 dev_nonfatal_errs[AER_MAX_TYPEOF_UNCOR_ERRS];
7162306a36Sopenharmony_ci	/* Total number of ERR_COR sent by this device */
7262306a36Sopenharmony_ci	u64 dev_total_cor_errs;
7362306a36Sopenharmony_ci	/* Total number of ERR_FATAL sent by this device */
7462306a36Sopenharmony_ci	u64 dev_total_fatal_errs;
7562306a36Sopenharmony_ci	/* Total number of ERR_NONFATAL sent by this device */
7662306a36Sopenharmony_ci	u64 dev_total_nonfatal_errs;
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci	/*
7962306a36Sopenharmony_ci	 * Fields for Root ports & root complex event collectors only, these
8062306a36Sopenharmony_ci	 * indicate the total number of ERR_COR, ERR_FATAL, and ERR_NONFATAL
8162306a36Sopenharmony_ci	 * messages received by the root port / event collector, INCLUDING the
8262306a36Sopenharmony_ci	 * ones that are generated internally (by the rootport itself)
8362306a36Sopenharmony_ci	 */
8462306a36Sopenharmony_ci	u64 rootport_total_cor_errs;
8562306a36Sopenharmony_ci	u64 rootport_total_fatal_errs;
8662306a36Sopenharmony_ci	u64 rootport_total_nonfatal_errs;
8762306a36Sopenharmony_ci};
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci#define AER_LOG_TLP_MASKS		(PCI_ERR_UNC_POISON_TLP|	\
9062306a36Sopenharmony_ci					PCI_ERR_UNC_ECRC|		\
9162306a36Sopenharmony_ci					PCI_ERR_UNC_UNSUP|		\
9262306a36Sopenharmony_ci					PCI_ERR_UNC_COMP_ABORT|		\
9362306a36Sopenharmony_ci					PCI_ERR_UNC_UNX_COMP|		\
9462306a36Sopenharmony_ci					PCI_ERR_UNC_MALF_TLP)
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci#define SYSTEM_ERROR_INTR_ON_MESG_MASK	(PCI_EXP_RTCTL_SECEE|	\
9762306a36Sopenharmony_ci					PCI_EXP_RTCTL_SENFEE|	\
9862306a36Sopenharmony_ci					PCI_EXP_RTCTL_SEFEE)
9962306a36Sopenharmony_ci#define ROOT_PORT_INTR_ON_MESG_MASK	(PCI_ERR_ROOT_CMD_COR_EN|	\
10062306a36Sopenharmony_ci					PCI_ERR_ROOT_CMD_NONFATAL_EN|	\
10162306a36Sopenharmony_ci					PCI_ERR_ROOT_CMD_FATAL_EN)
10262306a36Sopenharmony_ci#define ERR_COR_ID(d)			(d & 0xffff)
10362306a36Sopenharmony_ci#define ERR_UNCOR_ID(d)			(d >> 16)
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci#define AER_ERR_STATUS_MASK		(PCI_ERR_ROOT_UNCOR_RCV |	\
10662306a36Sopenharmony_ci					PCI_ERR_ROOT_COR_RCV |		\
10762306a36Sopenharmony_ci					PCI_ERR_ROOT_MULTI_COR_RCV |	\
10862306a36Sopenharmony_ci					PCI_ERR_ROOT_MULTI_UNCOR_RCV)
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_cistatic int pcie_aer_disable;
11162306a36Sopenharmony_cistatic pci_ers_result_t aer_root_reset(struct pci_dev *dev);
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_civoid pci_no_aer(void)
11462306a36Sopenharmony_ci{
11562306a36Sopenharmony_ci	pcie_aer_disable = 1;
11662306a36Sopenharmony_ci}
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_cibool pci_aer_available(void)
11962306a36Sopenharmony_ci{
12062306a36Sopenharmony_ci	return !pcie_aer_disable && pci_msi_enabled();
12162306a36Sopenharmony_ci}
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci#ifdef CONFIG_PCIE_ECRC
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci#define ECRC_POLICY_DEFAULT 0		/* ECRC set by BIOS */
12662306a36Sopenharmony_ci#define ECRC_POLICY_OFF     1		/* ECRC off for performance */
12762306a36Sopenharmony_ci#define ECRC_POLICY_ON      2		/* ECRC on for data integrity */
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_cistatic int ecrc_policy = ECRC_POLICY_DEFAULT;
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_cistatic const char * const ecrc_policy_str[] = {
13262306a36Sopenharmony_ci	[ECRC_POLICY_DEFAULT] = "bios",
13362306a36Sopenharmony_ci	[ECRC_POLICY_OFF] = "off",
13462306a36Sopenharmony_ci	[ECRC_POLICY_ON] = "on"
13562306a36Sopenharmony_ci};
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci/**
13862306a36Sopenharmony_ci * enable_ecrc_checking - enable PCIe ECRC checking for a device
13962306a36Sopenharmony_ci * @dev: the PCI device
14062306a36Sopenharmony_ci *
14162306a36Sopenharmony_ci * Returns 0 on success, or negative on failure.
14262306a36Sopenharmony_ci */
14362306a36Sopenharmony_cistatic int enable_ecrc_checking(struct pci_dev *dev)
14462306a36Sopenharmony_ci{
14562306a36Sopenharmony_ci	int aer = dev->aer_cap;
14662306a36Sopenharmony_ci	u32 reg32;
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci	if (!aer)
14962306a36Sopenharmony_ci		return -ENODEV;
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ci	pci_read_config_dword(dev, aer + PCI_ERR_CAP, &reg32);
15262306a36Sopenharmony_ci	if (reg32 & PCI_ERR_CAP_ECRC_GENC)
15362306a36Sopenharmony_ci		reg32 |= PCI_ERR_CAP_ECRC_GENE;
15462306a36Sopenharmony_ci	if (reg32 & PCI_ERR_CAP_ECRC_CHKC)
15562306a36Sopenharmony_ci		reg32 |= PCI_ERR_CAP_ECRC_CHKE;
15662306a36Sopenharmony_ci	pci_write_config_dword(dev, aer + PCI_ERR_CAP, reg32);
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci	return 0;
15962306a36Sopenharmony_ci}
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci/**
16262306a36Sopenharmony_ci * disable_ecrc_checking - disables PCIe ECRC checking for a device
16362306a36Sopenharmony_ci * @dev: the PCI device
16462306a36Sopenharmony_ci *
16562306a36Sopenharmony_ci * Returns 0 on success, or negative on failure.
16662306a36Sopenharmony_ci */
16762306a36Sopenharmony_cistatic int disable_ecrc_checking(struct pci_dev *dev)
16862306a36Sopenharmony_ci{
16962306a36Sopenharmony_ci	int aer = dev->aer_cap;
17062306a36Sopenharmony_ci	u32 reg32;
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci	if (!aer)
17362306a36Sopenharmony_ci		return -ENODEV;
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci	pci_read_config_dword(dev, aer + PCI_ERR_CAP, &reg32);
17662306a36Sopenharmony_ci	reg32 &= ~(PCI_ERR_CAP_ECRC_GENE | PCI_ERR_CAP_ECRC_CHKE);
17762306a36Sopenharmony_ci	pci_write_config_dword(dev, aer + PCI_ERR_CAP, reg32);
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci	return 0;
18062306a36Sopenharmony_ci}
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci/**
18362306a36Sopenharmony_ci * pcie_set_ecrc_checking - set/unset PCIe ECRC checking for a device based on global policy
18462306a36Sopenharmony_ci * @dev: the PCI device
18562306a36Sopenharmony_ci */
18662306a36Sopenharmony_civoid pcie_set_ecrc_checking(struct pci_dev *dev)
18762306a36Sopenharmony_ci{
18862306a36Sopenharmony_ci	if (!pcie_aer_is_native(dev))
18962306a36Sopenharmony_ci		return;
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_ci	switch (ecrc_policy) {
19262306a36Sopenharmony_ci	case ECRC_POLICY_DEFAULT:
19362306a36Sopenharmony_ci		return;
19462306a36Sopenharmony_ci	case ECRC_POLICY_OFF:
19562306a36Sopenharmony_ci		disable_ecrc_checking(dev);
19662306a36Sopenharmony_ci		break;
19762306a36Sopenharmony_ci	case ECRC_POLICY_ON:
19862306a36Sopenharmony_ci		enable_ecrc_checking(dev);
19962306a36Sopenharmony_ci		break;
20062306a36Sopenharmony_ci	default:
20162306a36Sopenharmony_ci		return;
20262306a36Sopenharmony_ci	}
20362306a36Sopenharmony_ci}
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci/**
20662306a36Sopenharmony_ci * pcie_ecrc_get_policy - parse kernel command-line ecrc option
20762306a36Sopenharmony_ci * @str: ECRC policy from kernel command line to use
20862306a36Sopenharmony_ci */
20962306a36Sopenharmony_civoid pcie_ecrc_get_policy(char *str)
21062306a36Sopenharmony_ci{
21162306a36Sopenharmony_ci	int i;
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci	i = match_string(ecrc_policy_str, ARRAY_SIZE(ecrc_policy_str), str);
21462306a36Sopenharmony_ci	if (i < 0)
21562306a36Sopenharmony_ci		return;
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_ci	ecrc_policy = i;
21862306a36Sopenharmony_ci}
21962306a36Sopenharmony_ci#endif	/* CONFIG_PCIE_ECRC */
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ci#define	PCI_EXP_AER_FLAGS	(PCI_EXP_DEVCTL_CERE | PCI_EXP_DEVCTL_NFERE | \
22262306a36Sopenharmony_ci				 PCI_EXP_DEVCTL_FERE | PCI_EXP_DEVCTL_URRE)
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_ciint pcie_aer_is_native(struct pci_dev *dev)
22562306a36Sopenharmony_ci{
22662306a36Sopenharmony_ci	struct pci_host_bridge *host = pci_find_host_bridge(dev->bus);
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_ci	if (!dev->aer_cap)
22962306a36Sopenharmony_ci		return 0;
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_ci	return pcie_ports_native || host->native_aer;
23262306a36Sopenharmony_ci}
23362306a36Sopenharmony_ciEXPORT_SYMBOL_NS_GPL(pcie_aer_is_native, CXL);
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_cistatic int pci_enable_pcie_error_reporting(struct pci_dev *dev)
23662306a36Sopenharmony_ci{
23762306a36Sopenharmony_ci	int rc;
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci	if (!pcie_aer_is_native(dev))
24062306a36Sopenharmony_ci		return -EIO;
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_ci	rc = pcie_capability_set_word(dev, PCI_EXP_DEVCTL, PCI_EXP_AER_FLAGS);
24362306a36Sopenharmony_ci	return pcibios_err_to_errno(rc);
24462306a36Sopenharmony_ci}
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ciint pci_aer_clear_nonfatal_status(struct pci_dev *dev)
24762306a36Sopenharmony_ci{
24862306a36Sopenharmony_ci	int aer = dev->aer_cap;
24962306a36Sopenharmony_ci	u32 status, sev;
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci	if (!pcie_aer_is_native(dev))
25262306a36Sopenharmony_ci		return -EIO;
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ci	/* Clear status bits for ERR_NONFATAL errors only */
25562306a36Sopenharmony_ci	pci_read_config_dword(dev, aer + PCI_ERR_UNCOR_STATUS, &status);
25662306a36Sopenharmony_ci	pci_read_config_dword(dev, aer + PCI_ERR_UNCOR_SEVER, &sev);
25762306a36Sopenharmony_ci	status &= ~sev;
25862306a36Sopenharmony_ci	if (status)
25962306a36Sopenharmony_ci		pci_write_config_dword(dev, aer + PCI_ERR_UNCOR_STATUS, status);
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ci	return 0;
26262306a36Sopenharmony_ci}
26362306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(pci_aer_clear_nonfatal_status);
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_civoid pci_aer_clear_fatal_status(struct pci_dev *dev)
26662306a36Sopenharmony_ci{
26762306a36Sopenharmony_ci	int aer = dev->aer_cap;
26862306a36Sopenharmony_ci	u32 status, sev;
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_ci	if (!pcie_aer_is_native(dev))
27162306a36Sopenharmony_ci		return;
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ci	/* Clear status bits for ERR_FATAL errors only */
27462306a36Sopenharmony_ci	pci_read_config_dword(dev, aer + PCI_ERR_UNCOR_STATUS, &status);
27562306a36Sopenharmony_ci	pci_read_config_dword(dev, aer + PCI_ERR_UNCOR_SEVER, &sev);
27662306a36Sopenharmony_ci	status &= sev;
27762306a36Sopenharmony_ci	if (status)
27862306a36Sopenharmony_ci		pci_write_config_dword(dev, aer + PCI_ERR_UNCOR_STATUS, status);
27962306a36Sopenharmony_ci}
28062306a36Sopenharmony_ci
28162306a36Sopenharmony_ci/**
28262306a36Sopenharmony_ci * pci_aer_raw_clear_status - Clear AER error registers.
28362306a36Sopenharmony_ci * @dev: the PCI device
28462306a36Sopenharmony_ci *
28562306a36Sopenharmony_ci * Clearing AER error status registers unconditionally, regardless of
28662306a36Sopenharmony_ci * whether they're owned by firmware or the OS.
28762306a36Sopenharmony_ci *
28862306a36Sopenharmony_ci * Returns 0 on success, or negative on failure.
28962306a36Sopenharmony_ci */
29062306a36Sopenharmony_ciint pci_aer_raw_clear_status(struct pci_dev *dev)
29162306a36Sopenharmony_ci{
29262306a36Sopenharmony_ci	int aer = dev->aer_cap;
29362306a36Sopenharmony_ci	u32 status;
29462306a36Sopenharmony_ci	int port_type;
29562306a36Sopenharmony_ci
29662306a36Sopenharmony_ci	if (!aer)
29762306a36Sopenharmony_ci		return -EIO;
29862306a36Sopenharmony_ci
29962306a36Sopenharmony_ci	port_type = pci_pcie_type(dev);
30062306a36Sopenharmony_ci	if (port_type == PCI_EXP_TYPE_ROOT_PORT ||
30162306a36Sopenharmony_ci	    port_type == PCI_EXP_TYPE_RC_EC) {
30262306a36Sopenharmony_ci		pci_read_config_dword(dev, aer + PCI_ERR_ROOT_STATUS, &status);
30362306a36Sopenharmony_ci		pci_write_config_dword(dev, aer + PCI_ERR_ROOT_STATUS, status);
30462306a36Sopenharmony_ci	}
30562306a36Sopenharmony_ci
30662306a36Sopenharmony_ci	pci_read_config_dword(dev, aer + PCI_ERR_COR_STATUS, &status);
30762306a36Sopenharmony_ci	pci_write_config_dword(dev, aer + PCI_ERR_COR_STATUS, status);
30862306a36Sopenharmony_ci
30962306a36Sopenharmony_ci	pci_read_config_dword(dev, aer + PCI_ERR_UNCOR_STATUS, &status);
31062306a36Sopenharmony_ci	pci_write_config_dword(dev, aer + PCI_ERR_UNCOR_STATUS, status);
31162306a36Sopenharmony_ci
31262306a36Sopenharmony_ci	return 0;
31362306a36Sopenharmony_ci}
31462306a36Sopenharmony_ci
31562306a36Sopenharmony_ciint pci_aer_clear_status(struct pci_dev *dev)
31662306a36Sopenharmony_ci{
31762306a36Sopenharmony_ci	if (!pcie_aer_is_native(dev))
31862306a36Sopenharmony_ci		return -EIO;
31962306a36Sopenharmony_ci
32062306a36Sopenharmony_ci	return pci_aer_raw_clear_status(dev);
32162306a36Sopenharmony_ci}
32262306a36Sopenharmony_ci
32362306a36Sopenharmony_civoid pci_save_aer_state(struct pci_dev *dev)
32462306a36Sopenharmony_ci{
32562306a36Sopenharmony_ci	int aer = dev->aer_cap;
32662306a36Sopenharmony_ci	struct pci_cap_saved_state *save_state;
32762306a36Sopenharmony_ci	u32 *cap;
32862306a36Sopenharmony_ci
32962306a36Sopenharmony_ci	if (!aer)
33062306a36Sopenharmony_ci		return;
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_ci	save_state = pci_find_saved_ext_cap(dev, PCI_EXT_CAP_ID_ERR);
33362306a36Sopenharmony_ci	if (!save_state)
33462306a36Sopenharmony_ci		return;
33562306a36Sopenharmony_ci
33662306a36Sopenharmony_ci	cap = &save_state->cap.data[0];
33762306a36Sopenharmony_ci	pci_read_config_dword(dev, aer + PCI_ERR_UNCOR_MASK, cap++);
33862306a36Sopenharmony_ci	pci_read_config_dword(dev, aer + PCI_ERR_UNCOR_SEVER, cap++);
33962306a36Sopenharmony_ci	pci_read_config_dword(dev, aer + PCI_ERR_COR_MASK, cap++);
34062306a36Sopenharmony_ci	pci_read_config_dword(dev, aer + PCI_ERR_CAP, cap++);
34162306a36Sopenharmony_ci	if (pcie_cap_has_rtctl(dev))
34262306a36Sopenharmony_ci		pci_read_config_dword(dev, aer + PCI_ERR_ROOT_COMMAND, cap++);
34362306a36Sopenharmony_ci}
34462306a36Sopenharmony_ci
34562306a36Sopenharmony_civoid pci_restore_aer_state(struct pci_dev *dev)
34662306a36Sopenharmony_ci{
34762306a36Sopenharmony_ci	int aer = dev->aer_cap;
34862306a36Sopenharmony_ci	struct pci_cap_saved_state *save_state;
34962306a36Sopenharmony_ci	u32 *cap;
35062306a36Sopenharmony_ci
35162306a36Sopenharmony_ci	if (!aer)
35262306a36Sopenharmony_ci		return;
35362306a36Sopenharmony_ci
35462306a36Sopenharmony_ci	save_state = pci_find_saved_ext_cap(dev, PCI_EXT_CAP_ID_ERR);
35562306a36Sopenharmony_ci	if (!save_state)
35662306a36Sopenharmony_ci		return;
35762306a36Sopenharmony_ci
35862306a36Sopenharmony_ci	cap = &save_state->cap.data[0];
35962306a36Sopenharmony_ci	pci_write_config_dword(dev, aer + PCI_ERR_UNCOR_MASK, *cap++);
36062306a36Sopenharmony_ci	pci_write_config_dword(dev, aer + PCI_ERR_UNCOR_SEVER, *cap++);
36162306a36Sopenharmony_ci	pci_write_config_dword(dev, aer + PCI_ERR_COR_MASK, *cap++);
36262306a36Sopenharmony_ci	pci_write_config_dword(dev, aer + PCI_ERR_CAP, *cap++);
36362306a36Sopenharmony_ci	if (pcie_cap_has_rtctl(dev))
36462306a36Sopenharmony_ci		pci_write_config_dword(dev, aer + PCI_ERR_ROOT_COMMAND, *cap++);
36562306a36Sopenharmony_ci}
36662306a36Sopenharmony_ci
36762306a36Sopenharmony_civoid pci_aer_init(struct pci_dev *dev)
36862306a36Sopenharmony_ci{
36962306a36Sopenharmony_ci	int n;
37062306a36Sopenharmony_ci
37162306a36Sopenharmony_ci	dev->aer_cap = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
37262306a36Sopenharmony_ci	if (!dev->aer_cap)
37362306a36Sopenharmony_ci		return;
37462306a36Sopenharmony_ci
37562306a36Sopenharmony_ci	dev->aer_stats = kzalloc(sizeof(struct aer_stats), GFP_KERNEL);
37662306a36Sopenharmony_ci
37762306a36Sopenharmony_ci	/*
37862306a36Sopenharmony_ci	 * We save/restore PCI_ERR_UNCOR_MASK, PCI_ERR_UNCOR_SEVER,
37962306a36Sopenharmony_ci	 * PCI_ERR_COR_MASK, and PCI_ERR_CAP.  Root and Root Complex Event
38062306a36Sopenharmony_ci	 * Collectors also implement PCI_ERR_ROOT_COMMAND (PCIe r5.0, sec
38162306a36Sopenharmony_ci	 * 7.8.4).
38262306a36Sopenharmony_ci	 */
38362306a36Sopenharmony_ci	n = pcie_cap_has_rtctl(dev) ? 5 : 4;
38462306a36Sopenharmony_ci	pci_add_ext_cap_save_buffer(dev, PCI_EXT_CAP_ID_ERR, sizeof(u32) * n);
38562306a36Sopenharmony_ci
38662306a36Sopenharmony_ci	pci_aer_clear_status(dev);
38762306a36Sopenharmony_ci
38862306a36Sopenharmony_ci	if (pci_aer_available())
38962306a36Sopenharmony_ci		pci_enable_pcie_error_reporting(dev);
39062306a36Sopenharmony_ci
39162306a36Sopenharmony_ci	pcie_set_ecrc_checking(dev);
39262306a36Sopenharmony_ci}
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_civoid pci_aer_exit(struct pci_dev *dev)
39562306a36Sopenharmony_ci{
39662306a36Sopenharmony_ci	kfree(dev->aer_stats);
39762306a36Sopenharmony_ci	dev->aer_stats = NULL;
39862306a36Sopenharmony_ci}
39962306a36Sopenharmony_ci
40062306a36Sopenharmony_ci#define AER_AGENT_RECEIVER		0
40162306a36Sopenharmony_ci#define AER_AGENT_REQUESTER		1
40262306a36Sopenharmony_ci#define AER_AGENT_COMPLETER		2
40362306a36Sopenharmony_ci#define AER_AGENT_TRANSMITTER		3
40462306a36Sopenharmony_ci
40562306a36Sopenharmony_ci#define AER_AGENT_REQUESTER_MASK(t)	((t == AER_CORRECTABLE) ?	\
40662306a36Sopenharmony_ci	0 : (PCI_ERR_UNC_COMP_TIME|PCI_ERR_UNC_UNSUP))
40762306a36Sopenharmony_ci#define AER_AGENT_COMPLETER_MASK(t)	((t == AER_CORRECTABLE) ?	\
40862306a36Sopenharmony_ci	0 : PCI_ERR_UNC_COMP_ABORT)
40962306a36Sopenharmony_ci#define AER_AGENT_TRANSMITTER_MASK(t)	((t == AER_CORRECTABLE) ?	\
41062306a36Sopenharmony_ci	(PCI_ERR_COR_REP_ROLL|PCI_ERR_COR_REP_TIMER) : 0)
41162306a36Sopenharmony_ci
41262306a36Sopenharmony_ci#define AER_GET_AGENT(t, e)						\
41362306a36Sopenharmony_ci	((e & AER_AGENT_COMPLETER_MASK(t)) ? AER_AGENT_COMPLETER :	\
41462306a36Sopenharmony_ci	(e & AER_AGENT_REQUESTER_MASK(t)) ? AER_AGENT_REQUESTER :	\
41562306a36Sopenharmony_ci	(e & AER_AGENT_TRANSMITTER_MASK(t)) ? AER_AGENT_TRANSMITTER :	\
41662306a36Sopenharmony_ci	AER_AGENT_RECEIVER)
41762306a36Sopenharmony_ci
41862306a36Sopenharmony_ci#define AER_PHYSICAL_LAYER_ERROR	0
41962306a36Sopenharmony_ci#define AER_DATA_LINK_LAYER_ERROR	1
42062306a36Sopenharmony_ci#define AER_TRANSACTION_LAYER_ERROR	2
42162306a36Sopenharmony_ci
42262306a36Sopenharmony_ci#define AER_PHYSICAL_LAYER_ERROR_MASK(t) ((t == AER_CORRECTABLE) ?	\
42362306a36Sopenharmony_ci	PCI_ERR_COR_RCVR : 0)
42462306a36Sopenharmony_ci#define AER_DATA_LINK_LAYER_ERROR_MASK(t) ((t == AER_CORRECTABLE) ?	\
42562306a36Sopenharmony_ci	(PCI_ERR_COR_BAD_TLP|						\
42662306a36Sopenharmony_ci	PCI_ERR_COR_BAD_DLLP|						\
42762306a36Sopenharmony_ci	PCI_ERR_COR_REP_ROLL|						\
42862306a36Sopenharmony_ci	PCI_ERR_COR_REP_TIMER) : PCI_ERR_UNC_DLP)
42962306a36Sopenharmony_ci
43062306a36Sopenharmony_ci#define AER_GET_LAYER_ERROR(t, e)					\
43162306a36Sopenharmony_ci	((e & AER_PHYSICAL_LAYER_ERROR_MASK(t)) ? AER_PHYSICAL_LAYER_ERROR : \
43262306a36Sopenharmony_ci	(e & AER_DATA_LINK_LAYER_ERROR_MASK(t)) ? AER_DATA_LINK_LAYER_ERROR : \
43362306a36Sopenharmony_ci	AER_TRANSACTION_LAYER_ERROR)
43462306a36Sopenharmony_ci
43562306a36Sopenharmony_ci/*
43662306a36Sopenharmony_ci * AER error strings
43762306a36Sopenharmony_ci */
43862306a36Sopenharmony_cistatic const char *aer_error_severity_string[] = {
43962306a36Sopenharmony_ci	"Uncorrected (Non-Fatal)",
44062306a36Sopenharmony_ci	"Uncorrected (Fatal)",
44162306a36Sopenharmony_ci	"Corrected"
44262306a36Sopenharmony_ci};
44362306a36Sopenharmony_ci
44462306a36Sopenharmony_cistatic const char *aer_error_layer[] = {
44562306a36Sopenharmony_ci	"Physical Layer",
44662306a36Sopenharmony_ci	"Data Link Layer",
44762306a36Sopenharmony_ci	"Transaction Layer"
44862306a36Sopenharmony_ci};
44962306a36Sopenharmony_ci
45062306a36Sopenharmony_cistatic const char *aer_correctable_error_string[] = {
45162306a36Sopenharmony_ci	"RxErr",			/* Bit Position 0	*/
45262306a36Sopenharmony_ci	NULL,
45362306a36Sopenharmony_ci	NULL,
45462306a36Sopenharmony_ci	NULL,
45562306a36Sopenharmony_ci	NULL,
45662306a36Sopenharmony_ci	NULL,
45762306a36Sopenharmony_ci	"BadTLP",			/* Bit Position 6	*/
45862306a36Sopenharmony_ci	"BadDLLP",			/* Bit Position 7	*/
45962306a36Sopenharmony_ci	"Rollover",			/* Bit Position 8	*/
46062306a36Sopenharmony_ci	NULL,
46162306a36Sopenharmony_ci	NULL,
46262306a36Sopenharmony_ci	NULL,
46362306a36Sopenharmony_ci	"Timeout",			/* Bit Position 12	*/
46462306a36Sopenharmony_ci	"NonFatalErr",			/* Bit Position 13	*/
46562306a36Sopenharmony_ci	"CorrIntErr",			/* Bit Position 14	*/
46662306a36Sopenharmony_ci	"HeaderOF",			/* Bit Position 15	*/
46762306a36Sopenharmony_ci	NULL,				/* Bit Position 16	*/
46862306a36Sopenharmony_ci	NULL,				/* Bit Position 17	*/
46962306a36Sopenharmony_ci	NULL,				/* Bit Position 18	*/
47062306a36Sopenharmony_ci	NULL,				/* Bit Position 19	*/
47162306a36Sopenharmony_ci	NULL,				/* Bit Position 20	*/
47262306a36Sopenharmony_ci	NULL,				/* Bit Position 21	*/
47362306a36Sopenharmony_ci	NULL,				/* Bit Position 22	*/
47462306a36Sopenharmony_ci	NULL,				/* Bit Position 23	*/
47562306a36Sopenharmony_ci	NULL,				/* Bit Position 24	*/
47662306a36Sopenharmony_ci	NULL,				/* Bit Position 25	*/
47762306a36Sopenharmony_ci	NULL,				/* Bit Position 26	*/
47862306a36Sopenharmony_ci	NULL,				/* Bit Position 27	*/
47962306a36Sopenharmony_ci	NULL,				/* Bit Position 28	*/
48062306a36Sopenharmony_ci	NULL,				/* Bit Position 29	*/
48162306a36Sopenharmony_ci	NULL,				/* Bit Position 30	*/
48262306a36Sopenharmony_ci	NULL,				/* Bit Position 31	*/
48362306a36Sopenharmony_ci};
48462306a36Sopenharmony_ci
48562306a36Sopenharmony_cistatic const char *aer_uncorrectable_error_string[] = {
48662306a36Sopenharmony_ci	"Undefined",			/* Bit Position 0	*/
48762306a36Sopenharmony_ci	NULL,
48862306a36Sopenharmony_ci	NULL,
48962306a36Sopenharmony_ci	NULL,
49062306a36Sopenharmony_ci	"DLP",				/* Bit Position 4	*/
49162306a36Sopenharmony_ci	"SDES",				/* Bit Position 5	*/
49262306a36Sopenharmony_ci	NULL,
49362306a36Sopenharmony_ci	NULL,
49462306a36Sopenharmony_ci	NULL,
49562306a36Sopenharmony_ci	NULL,
49662306a36Sopenharmony_ci	NULL,
49762306a36Sopenharmony_ci	NULL,
49862306a36Sopenharmony_ci	"TLP",				/* Bit Position 12	*/
49962306a36Sopenharmony_ci	"FCP",				/* Bit Position 13	*/
50062306a36Sopenharmony_ci	"CmpltTO",			/* Bit Position 14	*/
50162306a36Sopenharmony_ci	"CmpltAbrt",			/* Bit Position 15	*/
50262306a36Sopenharmony_ci	"UnxCmplt",			/* Bit Position 16	*/
50362306a36Sopenharmony_ci	"RxOF",				/* Bit Position 17	*/
50462306a36Sopenharmony_ci	"MalfTLP",			/* Bit Position 18	*/
50562306a36Sopenharmony_ci	"ECRC",				/* Bit Position 19	*/
50662306a36Sopenharmony_ci	"UnsupReq",			/* Bit Position 20	*/
50762306a36Sopenharmony_ci	"ACSViol",			/* Bit Position 21	*/
50862306a36Sopenharmony_ci	"UncorrIntErr",			/* Bit Position 22	*/
50962306a36Sopenharmony_ci	"BlockedTLP",			/* Bit Position 23	*/
51062306a36Sopenharmony_ci	"AtomicOpBlocked",		/* Bit Position 24	*/
51162306a36Sopenharmony_ci	"TLPBlockedErr",		/* Bit Position 25	*/
51262306a36Sopenharmony_ci	"PoisonTLPBlocked",		/* Bit Position 26	*/
51362306a36Sopenharmony_ci	NULL,				/* Bit Position 27	*/
51462306a36Sopenharmony_ci	NULL,				/* Bit Position 28	*/
51562306a36Sopenharmony_ci	NULL,				/* Bit Position 29	*/
51662306a36Sopenharmony_ci	NULL,				/* Bit Position 30	*/
51762306a36Sopenharmony_ci	NULL,				/* Bit Position 31	*/
51862306a36Sopenharmony_ci};
51962306a36Sopenharmony_ci
52062306a36Sopenharmony_cistatic const char *aer_agent_string[] = {
52162306a36Sopenharmony_ci	"Receiver ID",
52262306a36Sopenharmony_ci	"Requester ID",
52362306a36Sopenharmony_ci	"Completer ID",
52462306a36Sopenharmony_ci	"Transmitter ID"
52562306a36Sopenharmony_ci};
52662306a36Sopenharmony_ci
52762306a36Sopenharmony_ci#define aer_stats_dev_attr(name, stats_array, strings_array,		\
52862306a36Sopenharmony_ci			   total_string, total_field)			\
52962306a36Sopenharmony_ci	static ssize_t							\
53062306a36Sopenharmony_ci	name##_show(struct device *dev, struct device_attribute *attr,	\
53162306a36Sopenharmony_ci		     char *buf)						\
53262306a36Sopenharmony_ci{									\
53362306a36Sopenharmony_ci	unsigned int i;							\
53462306a36Sopenharmony_ci	struct pci_dev *pdev = to_pci_dev(dev);				\
53562306a36Sopenharmony_ci	u64 *stats = pdev->aer_stats->stats_array;			\
53662306a36Sopenharmony_ci	size_t len = 0;							\
53762306a36Sopenharmony_ci									\
53862306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(pdev->aer_stats->stats_array); i++) {\
53962306a36Sopenharmony_ci		if (strings_array[i])					\
54062306a36Sopenharmony_ci			len += sysfs_emit_at(buf, len, "%s %llu\n",	\
54162306a36Sopenharmony_ci					     strings_array[i],		\
54262306a36Sopenharmony_ci					     stats[i]);			\
54362306a36Sopenharmony_ci		else if (stats[i])					\
54462306a36Sopenharmony_ci			len += sysfs_emit_at(buf, len,			\
54562306a36Sopenharmony_ci					     #stats_array "_bit[%d] %llu\n",\
54662306a36Sopenharmony_ci					     i, stats[i]);		\
54762306a36Sopenharmony_ci	}								\
54862306a36Sopenharmony_ci	len += sysfs_emit_at(buf, len, "TOTAL_%s %llu\n", total_string,	\
54962306a36Sopenharmony_ci			     pdev->aer_stats->total_field);		\
55062306a36Sopenharmony_ci	return len;							\
55162306a36Sopenharmony_ci}									\
55262306a36Sopenharmony_cistatic DEVICE_ATTR_RO(name)
55362306a36Sopenharmony_ci
55462306a36Sopenharmony_ciaer_stats_dev_attr(aer_dev_correctable, dev_cor_errs,
55562306a36Sopenharmony_ci		   aer_correctable_error_string, "ERR_COR",
55662306a36Sopenharmony_ci		   dev_total_cor_errs);
55762306a36Sopenharmony_ciaer_stats_dev_attr(aer_dev_fatal, dev_fatal_errs,
55862306a36Sopenharmony_ci		   aer_uncorrectable_error_string, "ERR_FATAL",
55962306a36Sopenharmony_ci		   dev_total_fatal_errs);
56062306a36Sopenharmony_ciaer_stats_dev_attr(aer_dev_nonfatal, dev_nonfatal_errs,
56162306a36Sopenharmony_ci		   aer_uncorrectable_error_string, "ERR_NONFATAL",
56262306a36Sopenharmony_ci		   dev_total_nonfatal_errs);
56362306a36Sopenharmony_ci
56462306a36Sopenharmony_ci#define aer_stats_rootport_attr(name, field)				\
56562306a36Sopenharmony_ci	static ssize_t							\
56662306a36Sopenharmony_ci	name##_show(struct device *dev, struct device_attribute *attr,	\
56762306a36Sopenharmony_ci		     char *buf)						\
56862306a36Sopenharmony_ci{									\
56962306a36Sopenharmony_ci	struct pci_dev *pdev = to_pci_dev(dev);				\
57062306a36Sopenharmony_ci	return sysfs_emit(buf, "%llu\n", pdev->aer_stats->field);	\
57162306a36Sopenharmony_ci}									\
57262306a36Sopenharmony_cistatic DEVICE_ATTR_RO(name)
57362306a36Sopenharmony_ci
57462306a36Sopenharmony_ciaer_stats_rootport_attr(aer_rootport_total_err_cor,
57562306a36Sopenharmony_ci			 rootport_total_cor_errs);
57662306a36Sopenharmony_ciaer_stats_rootport_attr(aer_rootport_total_err_fatal,
57762306a36Sopenharmony_ci			 rootport_total_fatal_errs);
57862306a36Sopenharmony_ciaer_stats_rootport_attr(aer_rootport_total_err_nonfatal,
57962306a36Sopenharmony_ci			 rootport_total_nonfatal_errs);
58062306a36Sopenharmony_ci
58162306a36Sopenharmony_cistatic struct attribute *aer_stats_attrs[] __ro_after_init = {
58262306a36Sopenharmony_ci	&dev_attr_aer_dev_correctable.attr,
58362306a36Sopenharmony_ci	&dev_attr_aer_dev_fatal.attr,
58462306a36Sopenharmony_ci	&dev_attr_aer_dev_nonfatal.attr,
58562306a36Sopenharmony_ci	&dev_attr_aer_rootport_total_err_cor.attr,
58662306a36Sopenharmony_ci	&dev_attr_aer_rootport_total_err_fatal.attr,
58762306a36Sopenharmony_ci	&dev_attr_aer_rootport_total_err_nonfatal.attr,
58862306a36Sopenharmony_ci	NULL
58962306a36Sopenharmony_ci};
59062306a36Sopenharmony_ci
59162306a36Sopenharmony_cistatic umode_t aer_stats_attrs_are_visible(struct kobject *kobj,
59262306a36Sopenharmony_ci					   struct attribute *a, int n)
59362306a36Sopenharmony_ci{
59462306a36Sopenharmony_ci	struct device *dev = kobj_to_dev(kobj);
59562306a36Sopenharmony_ci	struct pci_dev *pdev = to_pci_dev(dev);
59662306a36Sopenharmony_ci
59762306a36Sopenharmony_ci	if (!pdev->aer_stats)
59862306a36Sopenharmony_ci		return 0;
59962306a36Sopenharmony_ci
60062306a36Sopenharmony_ci	if ((a == &dev_attr_aer_rootport_total_err_cor.attr ||
60162306a36Sopenharmony_ci	     a == &dev_attr_aer_rootport_total_err_fatal.attr ||
60262306a36Sopenharmony_ci	     a == &dev_attr_aer_rootport_total_err_nonfatal.attr) &&
60362306a36Sopenharmony_ci	    ((pci_pcie_type(pdev) != PCI_EXP_TYPE_ROOT_PORT) &&
60462306a36Sopenharmony_ci	     (pci_pcie_type(pdev) != PCI_EXP_TYPE_RC_EC)))
60562306a36Sopenharmony_ci		return 0;
60662306a36Sopenharmony_ci
60762306a36Sopenharmony_ci	return a->mode;
60862306a36Sopenharmony_ci}
60962306a36Sopenharmony_ci
61062306a36Sopenharmony_ciconst struct attribute_group aer_stats_attr_group = {
61162306a36Sopenharmony_ci	.attrs  = aer_stats_attrs,
61262306a36Sopenharmony_ci	.is_visible = aer_stats_attrs_are_visible,
61362306a36Sopenharmony_ci};
61462306a36Sopenharmony_ci
61562306a36Sopenharmony_cistatic void pci_dev_aer_stats_incr(struct pci_dev *pdev,
61662306a36Sopenharmony_ci				   struct aer_err_info *info)
61762306a36Sopenharmony_ci{
61862306a36Sopenharmony_ci	unsigned long status = info->status & ~info->mask;
61962306a36Sopenharmony_ci	int i, max = -1;
62062306a36Sopenharmony_ci	u64 *counter = NULL;
62162306a36Sopenharmony_ci	struct aer_stats *aer_stats = pdev->aer_stats;
62262306a36Sopenharmony_ci
62362306a36Sopenharmony_ci	if (!aer_stats)
62462306a36Sopenharmony_ci		return;
62562306a36Sopenharmony_ci
62662306a36Sopenharmony_ci	switch (info->severity) {
62762306a36Sopenharmony_ci	case AER_CORRECTABLE:
62862306a36Sopenharmony_ci		aer_stats->dev_total_cor_errs++;
62962306a36Sopenharmony_ci		counter = &aer_stats->dev_cor_errs[0];
63062306a36Sopenharmony_ci		max = AER_MAX_TYPEOF_COR_ERRS;
63162306a36Sopenharmony_ci		break;
63262306a36Sopenharmony_ci	case AER_NONFATAL:
63362306a36Sopenharmony_ci		aer_stats->dev_total_nonfatal_errs++;
63462306a36Sopenharmony_ci		counter = &aer_stats->dev_nonfatal_errs[0];
63562306a36Sopenharmony_ci		max = AER_MAX_TYPEOF_UNCOR_ERRS;
63662306a36Sopenharmony_ci		break;
63762306a36Sopenharmony_ci	case AER_FATAL:
63862306a36Sopenharmony_ci		aer_stats->dev_total_fatal_errs++;
63962306a36Sopenharmony_ci		counter = &aer_stats->dev_fatal_errs[0];
64062306a36Sopenharmony_ci		max = AER_MAX_TYPEOF_UNCOR_ERRS;
64162306a36Sopenharmony_ci		break;
64262306a36Sopenharmony_ci	}
64362306a36Sopenharmony_ci
64462306a36Sopenharmony_ci	for_each_set_bit(i, &status, max)
64562306a36Sopenharmony_ci		counter[i]++;
64662306a36Sopenharmony_ci}
64762306a36Sopenharmony_ci
64862306a36Sopenharmony_cistatic void pci_rootport_aer_stats_incr(struct pci_dev *pdev,
64962306a36Sopenharmony_ci				 struct aer_err_source *e_src)
65062306a36Sopenharmony_ci{
65162306a36Sopenharmony_ci	struct aer_stats *aer_stats = pdev->aer_stats;
65262306a36Sopenharmony_ci
65362306a36Sopenharmony_ci	if (!aer_stats)
65462306a36Sopenharmony_ci		return;
65562306a36Sopenharmony_ci
65662306a36Sopenharmony_ci	if (e_src->status & PCI_ERR_ROOT_COR_RCV)
65762306a36Sopenharmony_ci		aer_stats->rootport_total_cor_errs++;
65862306a36Sopenharmony_ci
65962306a36Sopenharmony_ci	if (e_src->status & PCI_ERR_ROOT_UNCOR_RCV) {
66062306a36Sopenharmony_ci		if (e_src->status & PCI_ERR_ROOT_FATAL_RCV)
66162306a36Sopenharmony_ci			aer_stats->rootport_total_fatal_errs++;
66262306a36Sopenharmony_ci		else
66362306a36Sopenharmony_ci			aer_stats->rootport_total_nonfatal_errs++;
66462306a36Sopenharmony_ci	}
66562306a36Sopenharmony_ci}
66662306a36Sopenharmony_ci
66762306a36Sopenharmony_cistatic void __print_tlp_header(struct pci_dev *dev,
66862306a36Sopenharmony_ci			       struct aer_header_log_regs *t)
66962306a36Sopenharmony_ci{
67062306a36Sopenharmony_ci	pci_err(dev, "  TLP Header: %08x %08x %08x %08x\n",
67162306a36Sopenharmony_ci		t->dw0, t->dw1, t->dw2, t->dw3);
67262306a36Sopenharmony_ci}
67362306a36Sopenharmony_ci
67462306a36Sopenharmony_cistatic void __aer_print_error(struct pci_dev *dev,
67562306a36Sopenharmony_ci			      struct aer_err_info *info)
67662306a36Sopenharmony_ci{
67762306a36Sopenharmony_ci	const char **strings;
67862306a36Sopenharmony_ci	unsigned long status = info->status & ~info->mask;
67962306a36Sopenharmony_ci	const char *level, *errmsg;
68062306a36Sopenharmony_ci	int i;
68162306a36Sopenharmony_ci
68262306a36Sopenharmony_ci	if (info->severity == AER_CORRECTABLE) {
68362306a36Sopenharmony_ci		strings = aer_correctable_error_string;
68462306a36Sopenharmony_ci		level = KERN_WARNING;
68562306a36Sopenharmony_ci	} else {
68662306a36Sopenharmony_ci		strings = aer_uncorrectable_error_string;
68762306a36Sopenharmony_ci		level = KERN_ERR;
68862306a36Sopenharmony_ci	}
68962306a36Sopenharmony_ci
69062306a36Sopenharmony_ci	for_each_set_bit(i, &status, 32) {
69162306a36Sopenharmony_ci		errmsg = strings[i];
69262306a36Sopenharmony_ci		if (!errmsg)
69362306a36Sopenharmony_ci			errmsg = "Unknown Error Bit";
69462306a36Sopenharmony_ci
69562306a36Sopenharmony_ci		pci_printk(level, dev, "   [%2d] %-22s%s\n", i, errmsg,
69662306a36Sopenharmony_ci				info->first_error == i ? " (First)" : "");
69762306a36Sopenharmony_ci	}
69862306a36Sopenharmony_ci	pci_dev_aer_stats_incr(dev, info);
69962306a36Sopenharmony_ci}
70062306a36Sopenharmony_ci
70162306a36Sopenharmony_civoid aer_print_error(struct pci_dev *dev, struct aer_err_info *info)
70262306a36Sopenharmony_ci{
70362306a36Sopenharmony_ci	int layer, agent;
70462306a36Sopenharmony_ci	int id = pci_dev_id(dev);
70562306a36Sopenharmony_ci	const char *level;
70662306a36Sopenharmony_ci
70762306a36Sopenharmony_ci	if (!info->status) {
70862306a36Sopenharmony_ci		pci_err(dev, "PCIe Bus Error: severity=%s, type=Inaccessible, (Unregistered Agent ID)\n",
70962306a36Sopenharmony_ci			aer_error_severity_string[info->severity]);
71062306a36Sopenharmony_ci		goto out;
71162306a36Sopenharmony_ci	}
71262306a36Sopenharmony_ci
71362306a36Sopenharmony_ci	layer = AER_GET_LAYER_ERROR(info->severity, info->status);
71462306a36Sopenharmony_ci	agent = AER_GET_AGENT(info->severity, info->status);
71562306a36Sopenharmony_ci
71662306a36Sopenharmony_ci	level = (info->severity == AER_CORRECTABLE) ? KERN_WARNING : KERN_ERR;
71762306a36Sopenharmony_ci
71862306a36Sopenharmony_ci	pci_printk(level, dev, "PCIe Bus Error: severity=%s, type=%s, (%s)\n",
71962306a36Sopenharmony_ci		   aer_error_severity_string[info->severity],
72062306a36Sopenharmony_ci		   aer_error_layer[layer], aer_agent_string[agent]);
72162306a36Sopenharmony_ci
72262306a36Sopenharmony_ci	pci_printk(level, dev, "  device [%04x:%04x] error status/mask=%08x/%08x\n",
72362306a36Sopenharmony_ci		   dev->vendor, dev->device, info->status, info->mask);
72462306a36Sopenharmony_ci
72562306a36Sopenharmony_ci	__aer_print_error(dev, info);
72662306a36Sopenharmony_ci
72762306a36Sopenharmony_ci	if (info->tlp_header_valid)
72862306a36Sopenharmony_ci		__print_tlp_header(dev, &info->tlp);
72962306a36Sopenharmony_ci
73062306a36Sopenharmony_ciout:
73162306a36Sopenharmony_ci	if (info->id && info->error_dev_num > 1 && info->id == id)
73262306a36Sopenharmony_ci		pci_err(dev, "  Error of this Agent is reported first\n");
73362306a36Sopenharmony_ci
73462306a36Sopenharmony_ci	trace_aer_event(dev_name(&dev->dev), (info->status & ~info->mask),
73562306a36Sopenharmony_ci			info->severity, info->tlp_header_valid, &info->tlp);
73662306a36Sopenharmony_ci}
73762306a36Sopenharmony_ci
73862306a36Sopenharmony_cistatic void aer_print_port_info(struct pci_dev *dev, struct aer_err_info *info)
73962306a36Sopenharmony_ci{
74062306a36Sopenharmony_ci	u8 bus = info->id >> 8;
74162306a36Sopenharmony_ci	u8 devfn = info->id & 0xff;
74262306a36Sopenharmony_ci
74362306a36Sopenharmony_ci	pci_info(dev, "%s%s error message received from %04x:%02x:%02x.%d\n",
74462306a36Sopenharmony_ci		 info->multi_error_valid ? "Multiple " : "",
74562306a36Sopenharmony_ci		 aer_error_severity_string[info->severity],
74662306a36Sopenharmony_ci		 pci_domain_nr(dev->bus), bus, PCI_SLOT(devfn),
74762306a36Sopenharmony_ci		 PCI_FUNC(devfn));
74862306a36Sopenharmony_ci}
74962306a36Sopenharmony_ci
75062306a36Sopenharmony_ci#ifdef CONFIG_ACPI_APEI_PCIEAER
75162306a36Sopenharmony_ciint cper_severity_to_aer(int cper_severity)
75262306a36Sopenharmony_ci{
75362306a36Sopenharmony_ci	switch (cper_severity) {
75462306a36Sopenharmony_ci	case CPER_SEV_RECOVERABLE:
75562306a36Sopenharmony_ci		return AER_NONFATAL;
75662306a36Sopenharmony_ci	case CPER_SEV_FATAL:
75762306a36Sopenharmony_ci		return AER_FATAL;
75862306a36Sopenharmony_ci	default:
75962306a36Sopenharmony_ci		return AER_CORRECTABLE;
76062306a36Sopenharmony_ci	}
76162306a36Sopenharmony_ci}
76262306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(cper_severity_to_aer);
76362306a36Sopenharmony_ci
76462306a36Sopenharmony_civoid cper_print_aer(struct pci_dev *dev, int aer_severity,
76562306a36Sopenharmony_ci		    struct aer_capability_regs *aer)
76662306a36Sopenharmony_ci{
76762306a36Sopenharmony_ci	int layer, agent, tlp_header_valid = 0;
76862306a36Sopenharmony_ci	u32 status, mask;
76962306a36Sopenharmony_ci	struct aer_err_info info;
77062306a36Sopenharmony_ci
77162306a36Sopenharmony_ci	if (aer_severity == AER_CORRECTABLE) {
77262306a36Sopenharmony_ci		status = aer->cor_status;
77362306a36Sopenharmony_ci		mask = aer->cor_mask;
77462306a36Sopenharmony_ci	} else {
77562306a36Sopenharmony_ci		status = aer->uncor_status;
77662306a36Sopenharmony_ci		mask = aer->uncor_mask;
77762306a36Sopenharmony_ci		tlp_header_valid = status & AER_LOG_TLP_MASKS;
77862306a36Sopenharmony_ci	}
77962306a36Sopenharmony_ci
78062306a36Sopenharmony_ci	layer = AER_GET_LAYER_ERROR(aer_severity, status);
78162306a36Sopenharmony_ci	agent = AER_GET_AGENT(aer_severity, status);
78262306a36Sopenharmony_ci
78362306a36Sopenharmony_ci	memset(&info, 0, sizeof(info));
78462306a36Sopenharmony_ci	info.severity = aer_severity;
78562306a36Sopenharmony_ci	info.status = status;
78662306a36Sopenharmony_ci	info.mask = mask;
78762306a36Sopenharmony_ci	info.first_error = PCI_ERR_CAP_FEP(aer->cap_control);
78862306a36Sopenharmony_ci
78962306a36Sopenharmony_ci	pci_err(dev, "aer_status: 0x%08x, aer_mask: 0x%08x\n", status, mask);
79062306a36Sopenharmony_ci	__aer_print_error(dev, &info);
79162306a36Sopenharmony_ci	pci_err(dev, "aer_layer=%s, aer_agent=%s\n",
79262306a36Sopenharmony_ci		aer_error_layer[layer], aer_agent_string[agent]);
79362306a36Sopenharmony_ci
79462306a36Sopenharmony_ci	if (aer_severity != AER_CORRECTABLE)
79562306a36Sopenharmony_ci		pci_err(dev, "aer_uncor_severity: 0x%08x\n",
79662306a36Sopenharmony_ci			aer->uncor_severity);
79762306a36Sopenharmony_ci
79862306a36Sopenharmony_ci	if (tlp_header_valid)
79962306a36Sopenharmony_ci		__print_tlp_header(dev, &aer->header_log);
80062306a36Sopenharmony_ci
80162306a36Sopenharmony_ci	trace_aer_event(dev_name(&dev->dev), (status & ~mask),
80262306a36Sopenharmony_ci			aer_severity, tlp_header_valid, &aer->header_log);
80362306a36Sopenharmony_ci}
80462306a36Sopenharmony_ci#endif
80562306a36Sopenharmony_ci
80662306a36Sopenharmony_ci/**
80762306a36Sopenharmony_ci * add_error_device - list device to be handled
80862306a36Sopenharmony_ci * @e_info: pointer to error info
80962306a36Sopenharmony_ci * @dev: pointer to pci_dev to be added
81062306a36Sopenharmony_ci */
81162306a36Sopenharmony_cistatic int add_error_device(struct aer_err_info *e_info, struct pci_dev *dev)
81262306a36Sopenharmony_ci{
81362306a36Sopenharmony_ci	if (e_info->error_dev_num < AER_MAX_MULTI_ERR_DEVICES) {
81462306a36Sopenharmony_ci		e_info->dev[e_info->error_dev_num] = pci_dev_get(dev);
81562306a36Sopenharmony_ci		e_info->error_dev_num++;
81662306a36Sopenharmony_ci		return 0;
81762306a36Sopenharmony_ci	}
81862306a36Sopenharmony_ci	return -ENOSPC;
81962306a36Sopenharmony_ci}
82062306a36Sopenharmony_ci
82162306a36Sopenharmony_ci/**
82262306a36Sopenharmony_ci * is_error_source - check whether the device is source of reported error
82362306a36Sopenharmony_ci * @dev: pointer to pci_dev to be checked
82462306a36Sopenharmony_ci * @e_info: pointer to reported error info
82562306a36Sopenharmony_ci */
82662306a36Sopenharmony_cistatic bool is_error_source(struct pci_dev *dev, struct aer_err_info *e_info)
82762306a36Sopenharmony_ci{
82862306a36Sopenharmony_ci	int aer = dev->aer_cap;
82962306a36Sopenharmony_ci	u32 status, mask;
83062306a36Sopenharmony_ci	u16 reg16;
83162306a36Sopenharmony_ci
83262306a36Sopenharmony_ci	/*
83362306a36Sopenharmony_ci	 * When bus id is equal to 0, it might be a bad id
83462306a36Sopenharmony_ci	 * reported by root port.
83562306a36Sopenharmony_ci	 */
83662306a36Sopenharmony_ci	if ((PCI_BUS_NUM(e_info->id) != 0) &&
83762306a36Sopenharmony_ci	    !(dev->bus->bus_flags & PCI_BUS_FLAGS_NO_AERSID)) {
83862306a36Sopenharmony_ci		/* Device ID match? */
83962306a36Sopenharmony_ci		if (e_info->id == pci_dev_id(dev))
84062306a36Sopenharmony_ci			return true;
84162306a36Sopenharmony_ci
84262306a36Sopenharmony_ci		/* Continue id comparing if there is no multiple error */
84362306a36Sopenharmony_ci		if (!e_info->multi_error_valid)
84462306a36Sopenharmony_ci			return false;
84562306a36Sopenharmony_ci	}
84662306a36Sopenharmony_ci
84762306a36Sopenharmony_ci	/*
84862306a36Sopenharmony_ci	 * When either
84962306a36Sopenharmony_ci	 *      1) bus id is equal to 0. Some ports might lose the bus
85062306a36Sopenharmony_ci	 *              id of error source id;
85162306a36Sopenharmony_ci	 *      2) bus flag PCI_BUS_FLAGS_NO_AERSID is set
85262306a36Sopenharmony_ci	 *      3) There are multiple errors and prior ID comparing fails;
85362306a36Sopenharmony_ci	 * We check AER status registers to find possible reporter.
85462306a36Sopenharmony_ci	 */
85562306a36Sopenharmony_ci	if (atomic_read(&dev->enable_cnt) == 0)
85662306a36Sopenharmony_ci		return false;
85762306a36Sopenharmony_ci
85862306a36Sopenharmony_ci	/* Check if AER is enabled */
85962306a36Sopenharmony_ci	pcie_capability_read_word(dev, PCI_EXP_DEVCTL, &reg16);
86062306a36Sopenharmony_ci	if (!(reg16 & PCI_EXP_AER_FLAGS))
86162306a36Sopenharmony_ci		return false;
86262306a36Sopenharmony_ci
86362306a36Sopenharmony_ci	if (!aer)
86462306a36Sopenharmony_ci		return false;
86562306a36Sopenharmony_ci
86662306a36Sopenharmony_ci	/* Check if error is recorded */
86762306a36Sopenharmony_ci	if (e_info->severity == AER_CORRECTABLE) {
86862306a36Sopenharmony_ci		pci_read_config_dword(dev, aer + PCI_ERR_COR_STATUS, &status);
86962306a36Sopenharmony_ci		pci_read_config_dword(dev, aer + PCI_ERR_COR_MASK, &mask);
87062306a36Sopenharmony_ci	} else {
87162306a36Sopenharmony_ci		pci_read_config_dword(dev, aer + PCI_ERR_UNCOR_STATUS, &status);
87262306a36Sopenharmony_ci		pci_read_config_dword(dev, aer + PCI_ERR_UNCOR_MASK, &mask);
87362306a36Sopenharmony_ci	}
87462306a36Sopenharmony_ci	if (status & ~mask)
87562306a36Sopenharmony_ci		return true;
87662306a36Sopenharmony_ci
87762306a36Sopenharmony_ci	return false;
87862306a36Sopenharmony_ci}
87962306a36Sopenharmony_ci
88062306a36Sopenharmony_cistatic int find_device_iter(struct pci_dev *dev, void *data)
88162306a36Sopenharmony_ci{
88262306a36Sopenharmony_ci	struct aer_err_info *e_info = (struct aer_err_info *)data;
88362306a36Sopenharmony_ci
88462306a36Sopenharmony_ci	if (is_error_source(dev, e_info)) {
88562306a36Sopenharmony_ci		/* List this device */
88662306a36Sopenharmony_ci		if (add_error_device(e_info, dev)) {
88762306a36Sopenharmony_ci			/* We cannot handle more... Stop iteration */
88862306a36Sopenharmony_ci			/* TODO: Should print error message here? */
88962306a36Sopenharmony_ci			return 1;
89062306a36Sopenharmony_ci		}
89162306a36Sopenharmony_ci
89262306a36Sopenharmony_ci		/* If there is only a single error, stop iteration */
89362306a36Sopenharmony_ci		if (!e_info->multi_error_valid)
89462306a36Sopenharmony_ci			return 1;
89562306a36Sopenharmony_ci	}
89662306a36Sopenharmony_ci	return 0;
89762306a36Sopenharmony_ci}
89862306a36Sopenharmony_ci
89962306a36Sopenharmony_ci/**
90062306a36Sopenharmony_ci * find_source_device - search through device hierarchy for source device
90162306a36Sopenharmony_ci * @parent: pointer to Root Port pci_dev data structure
90262306a36Sopenharmony_ci * @e_info: including detailed error information such like id
90362306a36Sopenharmony_ci *
90462306a36Sopenharmony_ci * Return true if found.
90562306a36Sopenharmony_ci *
90662306a36Sopenharmony_ci * Invoked by DPC when error is detected at the Root Port.
90762306a36Sopenharmony_ci * Caller of this function must set id, severity, and multi_error_valid of
90862306a36Sopenharmony_ci * struct aer_err_info pointed by @e_info properly.  This function must fill
90962306a36Sopenharmony_ci * e_info->error_dev_num and e_info->dev[], based on the given information.
91062306a36Sopenharmony_ci */
91162306a36Sopenharmony_cistatic bool find_source_device(struct pci_dev *parent,
91262306a36Sopenharmony_ci		struct aer_err_info *e_info)
91362306a36Sopenharmony_ci{
91462306a36Sopenharmony_ci	struct pci_dev *dev = parent;
91562306a36Sopenharmony_ci	int result;
91662306a36Sopenharmony_ci
91762306a36Sopenharmony_ci	/* Must reset in this function */
91862306a36Sopenharmony_ci	e_info->error_dev_num = 0;
91962306a36Sopenharmony_ci
92062306a36Sopenharmony_ci	/* Is Root Port an agent that sends error message? */
92162306a36Sopenharmony_ci	result = find_device_iter(dev, e_info);
92262306a36Sopenharmony_ci	if (result)
92362306a36Sopenharmony_ci		return true;
92462306a36Sopenharmony_ci
92562306a36Sopenharmony_ci	if (pci_pcie_type(parent) == PCI_EXP_TYPE_RC_EC)
92662306a36Sopenharmony_ci		pcie_walk_rcec(parent, find_device_iter, e_info);
92762306a36Sopenharmony_ci	else
92862306a36Sopenharmony_ci		pci_walk_bus(parent->subordinate, find_device_iter, e_info);
92962306a36Sopenharmony_ci
93062306a36Sopenharmony_ci	if (!e_info->error_dev_num) {
93162306a36Sopenharmony_ci		u8 bus = e_info->id >> 8;
93262306a36Sopenharmony_ci		u8 devfn = e_info->id & 0xff;
93362306a36Sopenharmony_ci
93462306a36Sopenharmony_ci		pci_info(parent, "found no error details for %04x:%02x:%02x.%d\n",
93562306a36Sopenharmony_ci			 pci_domain_nr(parent->bus), bus, PCI_SLOT(devfn),
93662306a36Sopenharmony_ci			 PCI_FUNC(devfn));
93762306a36Sopenharmony_ci		return false;
93862306a36Sopenharmony_ci	}
93962306a36Sopenharmony_ci	return true;
94062306a36Sopenharmony_ci}
94162306a36Sopenharmony_ci
94262306a36Sopenharmony_ci/**
94362306a36Sopenharmony_ci * handle_error_source - handle logging error into an event log
94462306a36Sopenharmony_ci * @dev: pointer to pci_dev data structure of error source device
94562306a36Sopenharmony_ci * @info: comprehensive error information
94662306a36Sopenharmony_ci *
94762306a36Sopenharmony_ci * Invoked when an error being detected by Root Port.
94862306a36Sopenharmony_ci */
94962306a36Sopenharmony_cistatic void handle_error_source(struct pci_dev *dev, struct aer_err_info *info)
95062306a36Sopenharmony_ci{
95162306a36Sopenharmony_ci	int aer = dev->aer_cap;
95262306a36Sopenharmony_ci
95362306a36Sopenharmony_ci	if (info->severity == AER_CORRECTABLE) {
95462306a36Sopenharmony_ci		/*
95562306a36Sopenharmony_ci		 * Correctable error does not need software intervention.
95662306a36Sopenharmony_ci		 * No need to go through error recovery process.
95762306a36Sopenharmony_ci		 */
95862306a36Sopenharmony_ci		if (aer)
95962306a36Sopenharmony_ci			pci_write_config_dword(dev, aer + PCI_ERR_COR_STATUS,
96062306a36Sopenharmony_ci					info->status);
96162306a36Sopenharmony_ci		if (pcie_aer_is_native(dev)) {
96262306a36Sopenharmony_ci			struct pci_driver *pdrv = dev->driver;
96362306a36Sopenharmony_ci
96462306a36Sopenharmony_ci			if (pdrv && pdrv->err_handler &&
96562306a36Sopenharmony_ci			    pdrv->err_handler->cor_error_detected)
96662306a36Sopenharmony_ci				pdrv->err_handler->cor_error_detected(dev);
96762306a36Sopenharmony_ci			pcie_clear_device_status(dev);
96862306a36Sopenharmony_ci		}
96962306a36Sopenharmony_ci	} else if (info->severity == AER_NONFATAL)
97062306a36Sopenharmony_ci		pcie_do_recovery(dev, pci_channel_io_normal, aer_root_reset);
97162306a36Sopenharmony_ci	else if (info->severity == AER_FATAL)
97262306a36Sopenharmony_ci		pcie_do_recovery(dev, pci_channel_io_frozen, aer_root_reset);
97362306a36Sopenharmony_ci	pci_dev_put(dev);
97462306a36Sopenharmony_ci}
97562306a36Sopenharmony_ci
97662306a36Sopenharmony_ci#ifdef CONFIG_ACPI_APEI_PCIEAER
97762306a36Sopenharmony_ci
97862306a36Sopenharmony_ci#define AER_RECOVER_RING_SIZE		16
97962306a36Sopenharmony_ci
98062306a36Sopenharmony_cistruct aer_recover_entry {
98162306a36Sopenharmony_ci	u8	bus;
98262306a36Sopenharmony_ci	u8	devfn;
98362306a36Sopenharmony_ci	u16	domain;
98462306a36Sopenharmony_ci	int	severity;
98562306a36Sopenharmony_ci	struct aer_capability_regs *regs;
98662306a36Sopenharmony_ci};
98762306a36Sopenharmony_ci
98862306a36Sopenharmony_cistatic DEFINE_KFIFO(aer_recover_ring, struct aer_recover_entry,
98962306a36Sopenharmony_ci		    AER_RECOVER_RING_SIZE);
99062306a36Sopenharmony_ci
99162306a36Sopenharmony_cistatic void aer_recover_work_func(struct work_struct *work)
99262306a36Sopenharmony_ci{
99362306a36Sopenharmony_ci	struct aer_recover_entry entry;
99462306a36Sopenharmony_ci	struct pci_dev *pdev;
99562306a36Sopenharmony_ci
99662306a36Sopenharmony_ci	while (kfifo_get(&aer_recover_ring, &entry)) {
99762306a36Sopenharmony_ci		pdev = pci_get_domain_bus_and_slot(entry.domain, entry.bus,
99862306a36Sopenharmony_ci						   entry.devfn);
99962306a36Sopenharmony_ci		if (!pdev) {
100062306a36Sopenharmony_ci			pr_err("no pci_dev for %04x:%02x:%02x.%x\n",
100162306a36Sopenharmony_ci			       entry.domain, entry.bus,
100262306a36Sopenharmony_ci			       PCI_SLOT(entry.devfn), PCI_FUNC(entry.devfn));
100362306a36Sopenharmony_ci			continue;
100462306a36Sopenharmony_ci		}
100562306a36Sopenharmony_ci		cper_print_aer(pdev, entry.severity, entry.regs);
100662306a36Sopenharmony_ci		/*
100762306a36Sopenharmony_ci		 * Memory for aer_capability_regs(entry.regs) is being allocated from the
100862306a36Sopenharmony_ci		 * ghes_estatus_pool to protect it from overwriting when multiple sections
100962306a36Sopenharmony_ci		 * are present in the error status. Thus free the same after processing
101062306a36Sopenharmony_ci		 * the data.
101162306a36Sopenharmony_ci		 */
101262306a36Sopenharmony_ci		ghes_estatus_pool_region_free((unsigned long)entry.regs,
101362306a36Sopenharmony_ci					      sizeof(struct aer_capability_regs));
101462306a36Sopenharmony_ci
101562306a36Sopenharmony_ci		if (entry.severity == AER_NONFATAL)
101662306a36Sopenharmony_ci			pcie_do_recovery(pdev, pci_channel_io_normal,
101762306a36Sopenharmony_ci					 aer_root_reset);
101862306a36Sopenharmony_ci		else if (entry.severity == AER_FATAL)
101962306a36Sopenharmony_ci			pcie_do_recovery(pdev, pci_channel_io_frozen,
102062306a36Sopenharmony_ci					 aer_root_reset);
102162306a36Sopenharmony_ci		pci_dev_put(pdev);
102262306a36Sopenharmony_ci	}
102362306a36Sopenharmony_ci}
102462306a36Sopenharmony_ci
102562306a36Sopenharmony_ci/*
102662306a36Sopenharmony_ci * Mutual exclusion for writers of aer_recover_ring, reader side don't
102762306a36Sopenharmony_ci * need lock, because there is only one reader and lock is not needed
102862306a36Sopenharmony_ci * between reader and writer.
102962306a36Sopenharmony_ci */
103062306a36Sopenharmony_cistatic DEFINE_SPINLOCK(aer_recover_ring_lock);
103162306a36Sopenharmony_cistatic DECLARE_WORK(aer_recover_work, aer_recover_work_func);
103262306a36Sopenharmony_ci
103362306a36Sopenharmony_civoid aer_recover_queue(int domain, unsigned int bus, unsigned int devfn,
103462306a36Sopenharmony_ci		       int severity, struct aer_capability_regs *aer_regs)
103562306a36Sopenharmony_ci{
103662306a36Sopenharmony_ci	struct aer_recover_entry entry = {
103762306a36Sopenharmony_ci		.bus		= bus,
103862306a36Sopenharmony_ci		.devfn		= devfn,
103962306a36Sopenharmony_ci		.domain		= domain,
104062306a36Sopenharmony_ci		.severity	= severity,
104162306a36Sopenharmony_ci		.regs		= aer_regs,
104262306a36Sopenharmony_ci	};
104362306a36Sopenharmony_ci
104462306a36Sopenharmony_ci	if (kfifo_in_spinlocked(&aer_recover_ring, &entry, 1,
104562306a36Sopenharmony_ci				 &aer_recover_ring_lock))
104662306a36Sopenharmony_ci		schedule_work(&aer_recover_work);
104762306a36Sopenharmony_ci	else
104862306a36Sopenharmony_ci		pr_err("buffer overflow in recovery for %04x:%02x:%02x.%x\n",
104962306a36Sopenharmony_ci		       domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
105062306a36Sopenharmony_ci}
105162306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(aer_recover_queue);
105262306a36Sopenharmony_ci#endif
105362306a36Sopenharmony_ci
105462306a36Sopenharmony_ci/**
105562306a36Sopenharmony_ci * aer_get_device_error_info - read error status from dev and store it to info
105662306a36Sopenharmony_ci * @dev: pointer to the device expected to have a error record
105762306a36Sopenharmony_ci * @info: pointer to structure to store the error record
105862306a36Sopenharmony_ci *
105962306a36Sopenharmony_ci * Return 1 on success, 0 on error.
106062306a36Sopenharmony_ci *
106162306a36Sopenharmony_ci * Note that @info is reused among all error devices. Clear fields properly.
106262306a36Sopenharmony_ci */
106362306a36Sopenharmony_ciint aer_get_device_error_info(struct pci_dev *dev, struct aer_err_info *info)
106462306a36Sopenharmony_ci{
106562306a36Sopenharmony_ci	int type = pci_pcie_type(dev);
106662306a36Sopenharmony_ci	int aer = dev->aer_cap;
106762306a36Sopenharmony_ci	int temp;
106862306a36Sopenharmony_ci
106962306a36Sopenharmony_ci	/* Must reset in this function */
107062306a36Sopenharmony_ci	info->status = 0;
107162306a36Sopenharmony_ci	info->tlp_header_valid = 0;
107262306a36Sopenharmony_ci
107362306a36Sopenharmony_ci	/* The device might not support AER */
107462306a36Sopenharmony_ci	if (!aer)
107562306a36Sopenharmony_ci		return 0;
107662306a36Sopenharmony_ci
107762306a36Sopenharmony_ci	if (info->severity == AER_CORRECTABLE) {
107862306a36Sopenharmony_ci		pci_read_config_dword(dev, aer + PCI_ERR_COR_STATUS,
107962306a36Sopenharmony_ci			&info->status);
108062306a36Sopenharmony_ci		pci_read_config_dword(dev, aer + PCI_ERR_COR_MASK,
108162306a36Sopenharmony_ci			&info->mask);
108262306a36Sopenharmony_ci		if (!(info->status & ~info->mask))
108362306a36Sopenharmony_ci			return 0;
108462306a36Sopenharmony_ci	} else if (type == PCI_EXP_TYPE_ROOT_PORT ||
108562306a36Sopenharmony_ci		   type == PCI_EXP_TYPE_RC_EC ||
108662306a36Sopenharmony_ci		   type == PCI_EXP_TYPE_DOWNSTREAM ||
108762306a36Sopenharmony_ci		   info->severity == AER_NONFATAL) {
108862306a36Sopenharmony_ci
108962306a36Sopenharmony_ci		/* Link is still healthy for IO reads */
109062306a36Sopenharmony_ci		pci_read_config_dword(dev, aer + PCI_ERR_UNCOR_STATUS,
109162306a36Sopenharmony_ci			&info->status);
109262306a36Sopenharmony_ci		pci_read_config_dword(dev, aer + PCI_ERR_UNCOR_MASK,
109362306a36Sopenharmony_ci			&info->mask);
109462306a36Sopenharmony_ci		if (!(info->status & ~info->mask))
109562306a36Sopenharmony_ci			return 0;
109662306a36Sopenharmony_ci
109762306a36Sopenharmony_ci		/* Get First Error Pointer */
109862306a36Sopenharmony_ci		pci_read_config_dword(dev, aer + PCI_ERR_CAP, &temp);
109962306a36Sopenharmony_ci		info->first_error = PCI_ERR_CAP_FEP(temp);
110062306a36Sopenharmony_ci
110162306a36Sopenharmony_ci		if (info->status & AER_LOG_TLP_MASKS) {
110262306a36Sopenharmony_ci			info->tlp_header_valid = 1;
110362306a36Sopenharmony_ci			pci_read_config_dword(dev,
110462306a36Sopenharmony_ci				aer + PCI_ERR_HEADER_LOG, &info->tlp.dw0);
110562306a36Sopenharmony_ci			pci_read_config_dword(dev,
110662306a36Sopenharmony_ci				aer + PCI_ERR_HEADER_LOG + 4, &info->tlp.dw1);
110762306a36Sopenharmony_ci			pci_read_config_dword(dev,
110862306a36Sopenharmony_ci				aer + PCI_ERR_HEADER_LOG + 8, &info->tlp.dw2);
110962306a36Sopenharmony_ci			pci_read_config_dword(dev,
111062306a36Sopenharmony_ci				aer + PCI_ERR_HEADER_LOG + 12, &info->tlp.dw3);
111162306a36Sopenharmony_ci		}
111262306a36Sopenharmony_ci	}
111362306a36Sopenharmony_ci
111462306a36Sopenharmony_ci	return 1;
111562306a36Sopenharmony_ci}
111662306a36Sopenharmony_ci
111762306a36Sopenharmony_cistatic inline void aer_process_err_devices(struct aer_err_info *e_info)
111862306a36Sopenharmony_ci{
111962306a36Sopenharmony_ci	int i;
112062306a36Sopenharmony_ci
112162306a36Sopenharmony_ci	/* Report all before handle them, not to lost records by reset etc. */
112262306a36Sopenharmony_ci	for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
112362306a36Sopenharmony_ci		if (aer_get_device_error_info(e_info->dev[i], e_info))
112462306a36Sopenharmony_ci			aer_print_error(e_info->dev[i], e_info);
112562306a36Sopenharmony_ci	}
112662306a36Sopenharmony_ci	for (i = 0; i < e_info->error_dev_num && e_info->dev[i]; i++) {
112762306a36Sopenharmony_ci		if (aer_get_device_error_info(e_info->dev[i], e_info))
112862306a36Sopenharmony_ci			handle_error_source(e_info->dev[i], e_info);
112962306a36Sopenharmony_ci	}
113062306a36Sopenharmony_ci}
113162306a36Sopenharmony_ci
113262306a36Sopenharmony_ci/**
113362306a36Sopenharmony_ci * aer_isr_one_error - consume an error detected by root port
113462306a36Sopenharmony_ci * @rpc: pointer to the root port which holds an error
113562306a36Sopenharmony_ci * @e_src: pointer to an error source
113662306a36Sopenharmony_ci */
113762306a36Sopenharmony_cistatic void aer_isr_one_error(struct aer_rpc *rpc,
113862306a36Sopenharmony_ci		struct aer_err_source *e_src)
113962306a36Sopenharmony_ci{
114062306a36Sopenharmony_ci	struct pci_dev *pdev = rpc->rpd;
114162306a36Sopenharmony_ci	struct aer_err_info e_info;
114262306a36Sopenharmony_ci
114362306a36Sopenharmony_ci	pci_rootport_aer_stats_incr(pdev, e_src);
114462306a36Sopenharmony_ci
114562306a36Sopenharmony_ci	/*
114662306a36Sopenharmony_ci	 * There is a possibility that both correctable error and
114762306a36Sopenharmony_ci	 * uncorrectable error being logged. Report correctable error first.
114862306a36Sopenharmony_ci	 */
114962306a36Sopenharmony_ci	if (e_src->status & PCI_ERR_ROOT_COR_RCV) {
115062306a36Sopenharmony_ci		e_info.id = ERR_COR_ID(e_src->id);
115162306a36Sopenharmony_ci		e_info.severity = AER_CORRECTABLE;
115262306a36Sopenharmony_ci
115362306a36Sopenharmony_ci		if (e_src->status & PCI_ERR_ROOT_MULTI_COR_RCV)
115462306a36Sopenharmony_ci			e_info.multi_error_valid = 1;
115562306a36Sopenharmony_ci		else
115662306a36Sopenharmony_ci			e_info.multi_error_valid = 0;
115762306a36Sopenharmony_ci		aer_print_port_info(pdev, &e_info);
115862306a36Sopenharmony_ci
115962306a36Sopenharmony_ci		if (find_source_device(pdev, &e_info))
116062306a36Sopenharmony_ci			aer_process_err_devices(&e_info);
116162306a36Sopenharmony_ci	}
116262306a36Sopenharmony_ci
116362306a36Sopenharmony_ci	if (e_src->status & PCI_ERR_ROOT_UNCOR_RCV) {
116462306a36Sopenharmony_ci		e_info.id = ERR_UNCOR_ID(e_src->id);
116562306a36Sopenharmony_ci
116662306a36Sopenharmony_ci		if (e_src->status & PCI_ERR_ROOT_FATAL_RCV)
116762306a36Sopenharmony_ci			e_info.severity = AER_FATAL;
116862306a36Sopenharmony_ci		else
116962306a36Sopenharmony_ci			e_info.severity = AER_NONFATAL;
117062306a36Sopenharmony_ci
117162306a36Sopenharmony_ci		if (e_src->status & PCI_ERR_ROOT_MULTI_UNCOR_RCV)
117262306a36Sopenharmony_ci			e_info.multi_error_valid = 1;
117362306a36Sopenharmony_ci		else
117462306a36Sopenharmony_ci			e_info.multi_error_valid = 0;
117562306a36Sopenharmony_ci
117662306a36Sopenharmony_ci		aer_print_port_info(pdev, &e_info);
117762306a36Sopenharmony_ci
117862306a36Sopenharmony_ci		if (find_source_device(pdev, &e_info))
117962306a36Sopenharmony_ci			aer_process_err_devices(&e_info);
118062306a36Sopenharmony_ci	}
118162306a36Sopenharmony_ci}
118262306a36Sopenharmony_ci
118362306a36Sopenharmony_ci/**
118462306a36Sopenharmony_ci * aer_isr - consume errors detected by root port
118562306a36Sopenharmony_ci * @irq: IRQ assigned to Root Port
118662306a36Sopenharmony_ci * @context: pointer to Root Port data structure
118762306a36Sopenharmony_ci *
118862306a36Sopenharmony_ci * Invoked, as DPC, when root port records new detected error
118962306a36Sopenharmony_ci */
119062306a36Sopenharmony_cistatic irqreturn_t aer_isr(int irq, void *context)
119162306a36Sopenharmony_ci{
119262306a36Sopenharmony_ci	struct pcie_device *dev = (struct pcie_device *)context;
119362306a36Sopenharmony_ci	struct aer_rpc *rpc = get_service_data(dev);
119462306a36Sopenharmony_ci	struct aer_err_source e_src;
119562306a36Sopenharmony_ci
119662306a36Sopenharmony_ci	if (kfifo_is_empty(&rpc->aer_fifo))
119762306a36Sopenharmony_ci		return IRQ_NONE;
119862306a36Sopenharmony_ci
119962306a36Sopenharmony_ci	while (kfifo_get(&rpc->aer_fifo, &e_src))
120062306a36Sopenharmony_ci		aer_isr_one_error(rpc, &e_src);
120162306a36Sopenharmony_ci	return IRQ_HANDLED;
120262306a36Sopenharmony_ci}
120362306a36Sopenharmony_ci
120462306a36Sopenharmony_ci/**
120562306a36Sopenharmony_ci * aer_irq - Root Port's ISR
120662306a36Sopenharmony_ci * @irq: IRQ assigned to Root Port
120762306a36Sopenharmony_ci * @context: pointer to Root Port data structure
120862306a36Sopenharmony_ci *
120962306a36Sopenharmony_ci * Invoked when Root Port detects AER messages.
121062306a36Sopenharmony_ci */
121162306a36Sopenharmony_cistatic irqreturn_t aer_irq(int irq, void *context)
121262306a36Sopenharmony_ci{
121362306a36Sopenharmony_ci	struct pcie_device *pdev = (struct pcie_device *)context;
121462306a36Sopenharmony_ci	struct aer_rpc *rpc = get_service_data(pdev);
121562306a36Sopenharmony_ci	struct pci_dev *rp = rpc->rpd;
121662306a36Sopenharmony_ci	int aer = rp->aer_cap;
121762306a36Sopenharmony_ci	struct aer_err_source e_src = {};
121862306a36Sopenharmony_ci
121962306a36Sopenharmony_ci	pci_read_config_dword(rp, aer + PCI_ERR_ROOT_STATUS, &e_src.status);
122062306a36Sopenharmony_ci	if (!(e_src.status & AER_ERR_STATUS_MASK))
122162306a36Sopenharmony_ci		return IRQ_NONE;
122262306a36Sopenharmony_ci
122362306a36Sopenharmony_ci	pci_read_config_dword(rp, aer + PCI_ERR_ROOT_ERR_SRC, &e_src.id);
122462306a36Sopenharmony_ci	pci_write_config_dword(rp, aer + PCI_ERR_ROOT_STATUS, e_src.status);
122562306a36Sopenharmony_ci
122662306a36Sopenharmony_ci	if (!kfifo_put(&rpc->aer_fifo, e_src))
122762306a36Sopenharmony_ci		return IRQ_HANDLED;
122862306a36Sopenharmony_ci
122962306a36Sopenharmony_ci	return IRQ_WAKE_THREAD;
123062306a36Sopenharmony_ci}
123162306a36Sopenharmony_ci
123262306a36Sopenharmony_ci/**
123362306a36Sopenharmony_ci * aer_enable_rootport - enable Root Port's interrupts when receiving messages
123462306a36Sopenharmony_ci * @rpc: pointer to a Root Port data structure
123562306a36Sopenharmony_ci *
123662306a36Sopenharmony_ci * Invoked when PCIe bus loads AER service driver.
123762306a36Sopenharmony_ci */
123862306a36Sopenharmony_cistatic void aer_enable_rootport(struct aer_rpc *rpc)
123962306a36Sopenharmony_ci{
124062306a36Sopenharmony_ci	struct pci_dev *pdev = rpc->rpd;
124162306a36Sopenharmony_ci	int aer = pdev->aer_cap;
124262306a36Sopenharmony_ci	u16 reg16;
124362306a36Sopenharmony_ci	u32 reg32;
124462306a36Sopenharmony_ci
124562306a36Sopenharmony_ci	/* Clear PCIe Capability's Device Status */
124662306a36Sopenharmony_ci	pcie_capability_read_word(pdev, PCI_EXP_DEVSTA, &reg16);
124762306a36Sopenharmony_ci	pcie_capability_write_word(pdev, PCI_EXP_DEVSTA, reg16);
124862306a36Sopenharmony_ci
124962306a36Sopenharmony_ci	/* Disable system error generation in response to error messages */
125062306a36Sopenharmony_ci	pcie_capability_clear_word(pdev, PCI_EXP_RTCTL,
125162306a36Sopenharmony_ci				   SYSTEM_ERROR_INTR_ON_MESG_MASK);
125262306a36Sopenharmony_ci
125362306a36Sopenharmony_ci	/* Clear error status */
125462306a36Sopenharmony_ci	pci_read_config_dword(pdev, aer + PCI_ERR_ROOT_STATUS, &reg32);
125562306a36Sopenharmony_ci	pci_write_config_dword(pdev, aer + PCI_ERR_ROOT_STATUS, reg32);
125662306a36Sopenharmony_ci	pci_read_config_dword(pdev, aer + PCI_ERR_COR_STATUS, &reg32);
125762306a36Sopenharmony_ci	pci_write_config_dword(pdev, aer + PCI_ERR_COR_STATUS, reg32);
125862306a36Sopenharmony_ci	pci_read_config_dword(pdev, aer + PCI_ERR_UNCOR_STATUS, &reg32);
125962306a36Sopenharmony_ci	pci_write_config_dword(pdev, aer + PCI_ERR_UNCOR_STATUS, reg32);
126062306a36Sopenharmony_ci
126162306a36Sopenharmony_ci	/* Enable Root Port's interrupt in response to error messages */
126262306a36Sopenharmony_ci	pci_read_config_dword(pdev, aer + PCI_ERR_ROOT_COMMAND, &reg32);
126362306a36Sopenharmony_ci	reg32 |= ROOT_PORT_INTR_ON_MESG_MASK;
126462306a36Sopenharmony_ci	pci_write_config_dword(pdev, aer + PCI_ERR_ROOT_COMMAND, reg32);
126562306a36Sopenharmony_ci}
126662306a36Sopenharmony_ci
126762306a36Sopenharmony_ci/**
126862306a36Sopenharmony_ci * aer_disable_rootport - disable Root Port's interrupts when receiving messages
126962306a36Sopenharmony_ci * @rpc: pointer to a Root Port data structure
127062306a36Sopenharmony_ci *
127162306a36Sopenharmony_ci * Invoked when PCIe bus unloads AER service driver.
127262306a36Sopenharmony_ci */
127362306a36Sopenharmony_cistatic void aer_disable_rootport(struct aer_rpc *rpc)
127462306a36Sopenharmony_ci{
127562306a36Sopenharmony_ci	struct pci_dev *pdev = rpc->rpd;
127662306a36Sopenharmony_ci	int aer = pdev->aer_cap;
127762306a36Sopenharmony_ci	u32 reg32;
127862306a36Sopenharmony_ci
127962306a36Sopenharmony_ci	/* Disable Root's interrupt in response to error messages */
128062306a36Sopenharmony_ci	pci_read_config_dword(pdev, aer + PCI_ERR_ROOT_COMMAND, &reg32);
128162306a36Sopenharmony_ci	reg32 &= ~ROOT_PORT_INTR_ON_MESG_MASK;
128262306a36Sopenharmony_ci	pci_write_config_dword(pdev, aer + PCI_ERR_ROOT_COMMAND, reg32);
128362306a36Sopenharmony_ci
128462306a36Sopenharmony_ci	/* Clear Root's error status reg */
128562306a36Sopenharmony_ci	pci_read_config_dword(pdev, aer + PCI_ERR_ROOT_STATUS, &reg32);
128662306a36Sopenharmony_ci	pci_write_config_dword(pdev, aer + PCI_ERR_ROOT_STATUS, reg32);
128762306a36Sopenharmony_ci}
128862306a36Sopenharmony_ci
128962306a36Sopenharmony_ci/**
129062306a36Sopenharmony_ci * aer_remove - clean up resources
129162306a36Sopenharmony_ci * @dev: pointer to the pcie_dev data structure
129262306a36Sopenharmony_ci *
129362306a36Sopenharmony_ci * Invoked when PCI Express bus unloads or AER probe fails.
129462306a36Sopenharmony_ci */
129562306a36Sopenharmony_cistatic void aer_remove(struct pcie_device *dev)
129662306a36Sopenharmony_ci{
129762306a36Sopenharmony_ci	struct aer_rpc *rpc = get_service_data(dev);
129862306a36Sopenharmony_ci
129962306a36Sopenharmony_ci	aer_disable_rootport(rpc);
130062306a36Sopenharmony_ci}
130162306a36Sopenharmony_ci
130262306a36Sopenharmony_ci/**
130362306a36Sopenharmony_ci * aer_probe - initialize resources
130462306a36Sopenharmony_ci * @dev: pointer to the pcie_dev data structure
130562306a36Sopenharmony_ci *
130662306a36Sopenharmony_ci * Invoked when PCI Express bus loads AER service driver.
130762306a36Sopenharmony_ci */
130862306a36Sopenharmony_cistatic int aer_probe(struct pcie_device *dev)
130962306a36Sopenharmony_ci{
131062306a36Sopenharmony_ci	int status;
131162306a36Sopenharmony_ci	struct aer_rpc *rpc;
131262306a36Sopenharmony_ci	struct device *device = &dev->device;
131362306a36Sopenharmony_ci	struct pci_dev *port = dev->port;
131462306a36Sopenharmony_ci
131562306a36Sopenharmony_ci	BUILD_BUG_ON(ARRAY_SIZE(aer_correctable_error_string) <
131662306a36Sopenharmony_ci		     AER_MAX_TYPEOF_COR_ERRS);
131762306a36Sopenharmony_ci	BUILD_BUG_ON(ARRAY_SIZE(aer_uncorrectable_error_string) <
131862306a36Sopenharmony_ci		     AER_MAX_TYPEOF_UNCOR_ERRS);
131962306a36Sopenharmony_ci
132062306a36Sopenharmony_ci	/* Limit to Root Ports or Root Complex Event Collectors */
132162306a36Sopenharmony_ci	if ((pci_pcie_type(port) != PCI_EXP_TYPE_RC_EC) &&
132262306a36Sopenharmony_ci	    (pci_pcie_type(port) != PCI_EXP_TYPE_ROOT_PORT))
132362306a36Sopenharmony_ci		return -ENODEV;
132462306a36Sopenharmony_ci
132562306a36Sopenharmony_ci	rpc = devm_kzalloc(device, sizeof(struct aer_rpc), GFP_KERNEL);
132662306a36Sopenharmony_ci	if (!rpc)
132762306a36Sopenharmony_ci		return -ENOMEM;
132862306a36Sopenharmony_ci
132962306a36Sopenharmony_ci	rpc->rpd = port;
133062306a36Sopenharmony_ci	INIT_KFIFO(rpc->aer_fifo);
133162306a36Sopenharmony_ci	set_service_data(dev, rpc);
133262306a36Sopenharmony_ci
133362306a36Sopenharmony_ci	status = devm_request_threaded_irq(device, dev->irq, aer_irq, aer_isr,
133462306a36Sopenharmony_ci					   IRQF_SHARED, "aerdrv", dev);
133562306a36Sopenharmony_ci	if (status) {
133662306a36Sopenharmony_ci		pci_err(port, "request AER IRQ %d failed\n", dev->irq);
133762306a36Sopenharmony_ci		return status;
133862306a36Sopenharmony_ci	}
133962306a36Sopenharmony_ci
134062306a36Sopenharmony_ci	aer_enable_rootport(rpc);
134162306a36Sopenharmony_ci	pci_info(port, "enabled with IRQ %d\n", dev->irq);
134262306a36Sopenharmony_ci	return 0;
134362306a36Sopenharmony_ci}
134462306a36Sopenharmony_ci
134562306a36Sopenharmony_ci/**
134662306a36Sopenharmony_ci * aer_root_reset - reset Root Port hierarchy, RCEC, or RCiEP
134762306a36Sopenharmony_ci * @dev: pointer to Root Port, RCEC, or RCiEP
134862306a36Sopenharmony_ci *
134962306a36Sopenharmony_ci * Invoked by Port Bus driver when performing reset.
135062306a36Sopenharmony_ci */
135162306a36Sopenharmony_cistatic pci_ers_result_t aer_root_reset(struct pci_dev *dev)
135262306a36Sopenharmony_ci{
135362306a36Sopenharmony_ci	int type = pci_pcie_type(dev);
135462306a36Sopenharmony_ci	struct pci_dev *root;
135562306a36Sopenharmony_ci	int aer;
135662306a36Sopenharmony_ci	struct pci_host_bridge *host = pci_find_host_bridge(dev->bus);
135762306a36Sopenharmony_ci	u32 reg32;
135862306a36Sopenharmony_ci	int rc;
135962306a36Sopenharmony_ci
136062306a36Sopenharmony_ci	/*
136162306a36Sopenharmony_ci	 * Only Root Ports and RCECs have AER Root Command and Root Status
136262306a36Sopenharmony_ci	 * registers.  If "dev" is an RCiEP, the relevant registers are in
136362306a36Sopenharmony_ci	 * the RCEC.
136462306a36Sopenharmony_ci	 */
136562306a36Sopenharmony_ci	if (type == PCI_EXP_TYPE_RC_END)
136662306a36Sopenharmony_ci		root = dev->rcec;
136762306a36Sopenharmony_ci	else
136862306a36Sopenharmony_ci		root = pcie_find_root_port(dev);
136962306a36Sopenharmony_ci
137062306a36Sopenharmony_ci	/*
137162306a36Sopenharmony_ci	 * If the platform retained control of AER, an RCiEP may not have
137262306a36Sopenharmony_ci	 * an RCEC visible to us, so dev->rcec ("root") may be NULL.  In
137362306a36Sopenharmony_ci	 * that case, firmware is responsible for these registers.
137462306a36Sopenharmony_ci	 */
137562306a36Sopenharmony_ci	aer = root ? root->aer_cap : 0;
137662306a36Sopenharmony_ci
137762306a36Sopenharmony_ci	if ((host->native_aer || pcie_ports_native) && aer) {
137862306a36Sopenharmony_ci		/* Disable Root's interrupt in response to error messages */
137962306a36Sopenharmony_ci		pci_read_config_dword(root, aer + PCI_ERR_ROOT_COMMAND, &reg32);
138062306a36Sopenharmony_ci		reg32 &= ~ROOT_PORT_INTR_ON_MESG_MASK;
138162306a36Sopenharmony_ci		pci_write_config_dword(root, aer + PCI_ERR_ROOT_COMMAND, reg32);
138262306a36Sopenharmony_ci	}
138362306a36Sopenharmony_ci
138462306a36Sopenharmony_ci	if (type == PCI_EXP_TYPE_RC_EC || type == PCI_EXP_TYPE_RC_END) {
138562306a36Sopenharmony_ci		rc = pcie_reset_flr(dev, PCI_RESET_DO_RESET);
138662306a36Sopenharmony_ci		if (!rc)
138762306a36Sopenharmony_ci			pci_info(dev, "has been reset\n");
138862306a36Sopenharmony_ci		else
138962306a36Sopenharmony_ci			pci_info(dev, "not reset (no FLR support: %d)\n", rc);
139062306a36Sopenharmony_ci	} else {
139162306a36Sopenharmony_ci		rc = pci_bus_error_reset(dev);
139262306a36Sopenharmony_ci		pci_info(dev, "%s Port link has been reset (%d)\n",
139362306a36Sopenharmony_ci			pci_is_root_bus(dev->bus) ? "Root" : "Downstream", rc);
139462306a36Sopenharmony_ci	}
139562306a36Sopenharmony_ci
139662306a36Sopenharmony_ci	if ((host->native_aer || pcie_ports_native) && aer) {
139762306a36Sopenharmony_ci		/* Clear Root Error Status */
139862306a36Sopenharmony_ci		pci_read_config_dword(root, aer + PCI_ERR_ROOT_STATUS, &reg32);
139962306a36Sopenharmony_ci		pci_write_config_dword(root, aer + PCI_ERR_ROOT_STATUS, reg32);
140062306a36Sopenharmony_ci
140162306a36Sopenharmony_ci		/* Enable Root Port's interrupt in response to error messages */
140262306a36Sopenharmony_ci		pci_read_config_dword(root, aer + PCI_ERR_ROOT_COMMAND, &reg32);
140362306a36Sopenharmony_ci		reg32 |= ROOT_PORT_INTR_ON_MESG_MASK;
140462306a36Sopenharmony_ci		pci_write_config_dword(root, aer + PCI_ERR_ROOT_COMMAND, reg32);
140562306a36Sopenharmony_ci	}
140662306a36Sopenharmony_ci
140762306a36Sopenharmony_ci	return rc ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
140862306a36Sopenharmony_ci}
140962306a36Sopenharmony_ci
141062306a36Sopenharmony_cistatic struct pcie_port_service_driver aerdriver = {
141162306a36Sopenharmony_ci	.name		= "aer",
141262306a36Sopenharmony_ci	.port_type	= PCIE_ANY_PORT,
141362306a36Sopenharmony_ci	.service	= PCIE_PORT_SERVICE_AER,
141462306a36Sopenharmony_ci
141562306a36Sopenharmony_ci	.probe		= aer_probe,
141662306a36Sopenharmony_ci	.remove		= aer_remove,
141762306a36Sopenharmony_ci};
141862306a36Sopenharmony_ci
141962306a36Sopenharmony_ci/**
142062306a36Sopenharmony_ci * pcie_aer_init - register AER root service driver
142162306a36Sopenharmony_ci *
142262306a36Sopenharmony_ci * Invoked when AER root service driver is loaded.
142362306a36Sopenharmony_ci */
142462306a36Sopenharmony_ciint __init pcie_aer_init(void)
142562306a36Sopenharmony_ci{
142662306a36Sopenharmony_ci	if (!pci_aer_available())
142762306a36Sopenharmony_ci		return -ENXIO;
142862306a36Sopenharmony_ci	return pcie_port_service_register(&aerdriver);
142962306a36Sopenharmony_ci}
1430