// SPDX-License-Identifier: GPL-2.0-or-later1/*2* Copyright IBM Corporation 2001, 2005, 20063* Copyright Dave Engebretsen & Todd Inglett 20014* Copyright Linas Vepstas 2005, 20065* Copyright 2001-2012 IBM Corporation.6*7* Please address comments and feedback to Linas Vepstas <[email protected]>8*/910#include <linux/delay.h>11#include <linux/sched.h>12#include <linux/init.h>13#include <linux/list.h>14#include <linux/pci.h>15#include <linux/iommu.h>16#include <linux/proc_fs.h>17#include <linux/rbtree.h>18#include <linux/reboot.h>19#include <linux/seq_file.h>20#include <linux/spinlock.h>21#include <linux/export.h>22#include <linux/of.h>23#include <linux/debugfs.h>2425#include <linux/atomic.h>26#include <asm/eeh.h>27#include <asm/eeh_event.h>28#include <asm/io.h>29#include <asm/iommu.h>30#include <asm/machdep.h>31#include <asm/ppc-pci.h>32#include <asm/rtas.h>33#include <asm/pte-walk.h>343536/** Overview:37* EEH, or "Enhanced Error Handling" is a PCI bridge technology for38* dealing with PCI bus errors that can't be dealt with within the39* usual PCI framework, except by check-stopping the CPU. Systems40* that are designed for high-availability/reliability cannot afford41* to crash due to a "mere" PCI error, thus the need for EEH.42* An EEH-capable bridge operates by converting a detected error43* into a "slot freeze", taking the PCI adapter off-line, making44* the slot behave, from the OS'es point of view, as if the slot45* were "empty": all reads return 0xff's and all writes are silently46* ignored. EEH slot isolation events can be triggered by parity47* errors on the address or data busses (e.g. during posted writes),48* which in turn might be caused by low voltage on the bus, dust,49* vibration, humidity, radioactivity or plain-old failed hardware.50*51* Note, however, that one of the leading causes of EEH slot52* freeze events are buggy device drivers, buggy device microcode,53* or buggy device hardware. This is because any attempt by the54* device to bus-master data to a memory address that is not55* assigned to the device will trigger a slot freeze. (The idea56* is to prevent devices-gone-wild from corrupting system memory).57* Buggy hardware/drivers will have a miserable time co-existing58* with EEH.59*60* Ideally, a PCI device driver, when suspecting that an isolation61* event has occurred (e.g. by reading 0xff's), will then ask EEH62* whether this is the case, and then take appropriate steps to63* reset the PCI slot, the PCI device, and then resume operations.64* However, until that day, the checking is done here, with the65* eeh_check_failure() routine embedded in the MMIO macros. If66* the slot is found to be isolated, an "EEH Event" is synthesized67* and sent out for processing.68*/6970/* If a device driver keeps reading an MMIO register in an interrupt71* handler after a slot isolation event, it might be broken.72* This sets the threshold for how many read attempts we allow73* before printing an error message.74*/75#define EEH_MAX_FAILS 21000007677/* Time to wait for a PCI slot to report status, in milliseconds */78#define PCI_BUS_RESET_WAIT_MSEC (5*60*1000)7980/*81* EEH probe mode support, which is part of the flags,82* is to support multiple platforms for EEH. Some platforms83* like pSeries do PCI emunation based on device tree.84* However, other platforms like powernv probe PCI devices85* from hardware. The flag is used to distinguish that.86* In addition, struct eeh_ops::probe would be invoked for87* particular OF node or PCI device so that the corresponding88* PE would be created there.89*/90int eeh_subsystem_flags;91EXPORT_SYMBOL(eeh_subsystem_flags);9293/*94* EEH allowed maximal frozen times. If one particular PE's95* frozen count in last hour exceeds this limit, the PE will96* be forced to be offline permanently.97*/98u32 eeh_max_freezes = 5;99100/*101* Controls whether a recovery event should be scheduled when an102* isolated device is discovered. This is only really useful for103* debugging problems with the EEH core.104*/105bool eeh_debugfs_no_recover;106107/* Platform dependent EEH operations */108struct eeh_ops *eeh_ops = NULL;109110/* Lock to avoid races due to multiple reports of an error */111DEFINE_RAW_SPINLOCK(confirm_error_lock);112EXPORT_SYMBOL_GPL(confirm_error_lock);113114/* Lock to protect passed flags */115static DEFINE_MUTEX(eeh_dev_mutex);116117/* Buffer for reporting pci register dumps. Its here in BSS, and118* not dynamically alloced, so that it ends up in RMO where RTAS119* can access it.120*/121#define EEH_PCI_REGS_LOG_LEN 8192122static unsigned char pci_regs_buf[EEH_PCI_REGS_LOG_LEN];123124/*125* The struct is used to maintain the EEH global statistic126* information. Besides, the EEH global statistics will be127* exported to user space through procfs128*/129struct eeh_stats {130u64 no_device; /* PCI device not found */131u64 no_dn; /* OF node not found */132u64 no_cfg_addr; /* Config address not found */133u64 ignored_check; /* EEH check skipped */134u64 total_mmio_ffs; /* Total EEH checks */135u64 false_positives; /* Unnecessary EEH checks */136u64 slot_resets; /* PE reset */137};138139static struct eeh_stats eeh_stats;140141static int __init eeh_setup(char *str)142{143if (!strcmp(str, "off"))144eeh_add_flag(EEH_FORCE_DISABLED);145else if (!strcmp(str, "early_log"))146eeh_add_flag(EEH_EARLY_DUMP_LOG);147148return 1;149}150__setup("eeh=", eeh_setup);151152void eeh_show_enabled(void)153{154if (eeh_has_flag(EEH_FORCE_DISABLED))155pr_info("EEH: Recovery disabled by kernel parameter.\n");156else if (eeh_has_flag(EEH_ENABLED))157pr_info("EEH: Capable adapter found: recovery enabled.\n");158else159pr_info("EEH: No capable adapters found: recovery disabled.\n");160}161162/*163* This routine captures assorted PCI configuration space data164* for the indicated PCI device, and puts them into a buffer165* for RTAS error logging.166*/167static size_t eeh_dump_dev_log(struct eeh_dev *edev, char *buf, size_t len)168{169u32 cfg;170int cap, i;171int n = 0, l = 0;172char buffer[128];173174n += scnprintf(buf+n, len-n, "%04x:%02x:%02x.%01x\n",175edev->pe->phb->global_number, edev->bdfn >> 8,176PCI_SLOT(edev->bdfn), PCI_FUNC(edev->bdfn));177pr_warn("EEH: of node=%04x:%02x:%02x.%01x\n",178edev->pe->phb->global_number, edev->bdfn >> 8,179PCI_SLOT(edev->bdfn), PCI_FUNC(edev->bdfn));180181eeh_ops->read_config(edev, PCI_VENDOR_ID, 4, &cfg);182n += scnprintf(buf+n, len-n, "dev/vend:%08x\n", cfg);183pr_warn("EEH: PCI device/vendor: %08x\n", cfg);184185eeh_ops->read_config(edev, PCI_COMMAND, 4, &cfg);186n += scnprintf(buf+n, len-n, "cmd/stat:%x\n", cfg);187pr_warn("EEH: PCI cmd/status register: %08x\n", cfg);188189/* Gather bridge-specific registers */190if (edev->mode & EEH_DEV_BRIDGE) {191eeh_ops->read_config(edev, PCI_SEC_STATUS, 2, &cfg);192n += scnprintf(buf+n, len-n, "sec stat:%x\n", cfg);193pr_warn("EEH: Bridge secondary status: %04x\n", cfg);194195eeh_ops->read_config(edev, PCI_BRIDGE_CONTROL, 2, &cfg);196n += scnprintf(buf+n, len-n, "brdg ctl:%x\n", cfg);197pr_warn("EEH: Bridge control: %04x\n", cfg);198}199200/* Dump out the PCI-X command and status regs */201cap = edev->pcix_cap;202if (cap) {203eeh_ops->read_config(edev, cap, 4, &cfg);204n += scnprintf(buf+n, len-n, "pcix-cmd:%x\n", cfg);205pr_warn("EEH: PCI-X cmd: %08x\n", cfg);206207eeh_ops->read_config(edev, cap+4, 4, &cfg);208n += scnprintf(buf+n, len-n, "pcix-stat:%x\n", cfg);209pr_warn("EEH: PCI-X status: %08x\n", cfg);210}211212/* If PCI-E capable, dump PCI-E cap 10 */213cap = edev->pcie_cap;214if (cap) {215n += scnprintf(buf+n, len-n, "pci-e cap10:\n");216pr_warn("EEH: PCI-E capabilities and status follow:\n");217218for (i=0; i<=8; i++) {219eeh_ops->read_config(edev, cap+4*i, 4, &cfg);220n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);221222if ((i % 4) == 0) {223if (i != 0)224pr_warn("%s\n", buffer);225226l = scnprintf(buffer, sizeof(buffer),227"EEH: PCI-E %02x: %08x ",2284*i, cfg);229} else {230l += scnprintf(buffer+l, sizeof(buffer)-l,231"%08x ", cfg);232}233234}235236pr_warn("%s\n", buffer);237}238239/* If AER capable, dump it */240cap = edev->aer_cap;241if (cap) {242n += scnprintf(buf+n, len-n, "pci-e AER:\n");243pr_warn("EEH: PCI-E AER capability register set follows:\n");244245for (i=0; i<=13; i++) {246eeh_ops->read_config(edev, cap+4*i, 4, &cfg);247n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);248249if ((i % 4) == 0) {250if (i != 0)251pr_warn("%s\n", buffer);252253l = scnprintf(buffer, sizeof(buffer),254"EEH: PCI-E AER %02x: %08x ",2554*i, cfg);256} else {257l += scnprintf(buffer+l, sizeof(buffer)-l,258"%08x ", cfg);259}260}261262pr_warn("%s\n", buffer);263}264265return n;266}267268static void *eeh_dump_pe_log(struct eeh_pe *pe, void *flag)269{270struct eeh_dev *edev, *tmp;271size_t *plen = flag;272273eeh_pe_for_each_dev(pe, edev, tmp)274*plen += eeh_dump_dev_log(edev, pci_regs_buf + *plen,275EEH_PCI_REGS_LOG_LEN - *plen);276277return NULL;278}279280/**281* eeh_slot_error_detail - Generate combined log including driver log and error log282* @pe: EEH PE283* @severity: temporary or permanent error log284*285* This routine should be called to generate the combined log, which286* is comprised of driver log and error log. The driver log is figured287* out from the config space of the corresponding PCI device, while288* the error log is fetched through platform dependent function call.289*/290void eeh_slot_error_detail(struct eeh_pe *pe, int severity)291{292size_t loglen = 0;293294/*295* When the PHB is fenced or dead, it's pointless to collect296* the data from PCI config space because it should return297* 0xFF's. For ER, we still retrieve the data from the PCI298* config space.299*300* For pHyp, we have to enable IO for log retrieval. Otherwise,301* 0xFF's is always returned from PCI config space.302*303* When the @severity is EEH_LOG_PERM, the PE is going to be304* removed. Prior to that, the drivers for devices included in305* the PE will be closed. The drivers rely on working IO path306* to bring the devices to quiet state. Otherwise, PCI traffic307* from those devices after they are removed is like to cause308* another unexpected EEH error.309*/310if (!(pe->type & EEH_PE_PHB)) {311if (eeh_has_flag(EEH_ENABLE_IO_FOR_LOG) ||312severity == EEH_LOG_PERM)313eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);314315/*316* The config space of some PCI devices can't be accessed317* when their PEs are in frozen state. Otherwise, fenced318* PHB might be seen. Those PEs are identified with flag319* EEH_PE_CFG_RESTRICTED, indicating EEH_PE_CFG_BLOCKED320* is set automatically when the PE is put to EEH_PE_ISOLATED.321*322* Restoring BARs possibly triggers PCI config access in323* (OPAL) firmware and then causes fenced PHB. If the324* PCI config is blocked with flag EEH_PE_CFG_BLOCKED, it's325* pointless to restore BARs and dump config space.326*/327eeh_ops->configure_bridge(pe);328if (!(pe->state & EEH_PE_CFG_BLOCKED)) {329eeh_pe_restore_bars(pe);330331pci_regs_buf[0] = 0;332eeh_pe_traverse(pe, eeh_dump_pe_log, &loglen);333}334}335336eeh_ops->get_log(pe, severity, pci_regs_buf, loglen);337}338339/**340* eeh_token_to_phys - Convert EEH address token to phys address341* @token: I/O token, should be address in the form 0xA....342*343* This routine should be called to convert virtual I/O address344* to physical one.345*/346static inline unsigned long eeh_token_to_phys(unsigned long token)347{348return ppc_find_vmap_phys(token);349}350351/*352* On PowerNV platform, we might already have fenced PHB there.353* For that case, it's meaningless to recover frozen PE. Intead,354* We have to handle fenced PHB firstly.355*/356static int eeh_phb_check_failure(struct eeh_pe *pe)357{358struct eeh_pe *phb_pe;359unsigned long flags;360int ret;361362if (!eeh_has_flag(EEH_PROBE_MODE_DEV))363return -EPERM;364365/* Find the PHB PE */366phb_pe = eeh_phb_pe_get(pe->phb);367if (!phb_pe) {368pr_warn("%s Can't find PE for PHB#%x\n",369__func__, pe->phb->global_number);370return -EEXIST;371}372373/* If the PHB has been in problematic state */374eeh_serialize_lock(&flags);375if (phb_pe->state & EEH_PE_ISOLATED) {376ret = 0;377goto out;378}379380/* Check PHB state */381ret = eeh_ops->get_state(phb_pe, NULL);382if ((ret < 0) ||383(ret == EEH_STATE_NOT_SUPPORT) || eeh_state_active(ret)) {384ret = 0;385goto out;386}387388/* Isolate the PHB and send event */389eeh_pe_mark_isolated(phb_pe);390eeh_serialize_unlock(flags);391392pr_debug("EEH: PHB#%x failure detected, location: %s\n",393phb_pe->phb->global_number, eeh_pe_loc_get(phb_pe));394eeh_send_failure_event(phb_pe);395return 1;396out:397eeh_serialize_unlock(flags);398return ret;399}400401static inline const char *eeh_driver_name(struct pci_dev *pdev)402{403if (pdev)404return dev_driver_string(&pdev->dev);405406return "<null>";407}408409/**410* eeh_dev_check_failure - Check if all 1's data is due to EEH slot freeze411* @edev: eeh device412*413* Check for an EEH failure for the given device node. Call this414* routine if the result of a read was all 0xff's and you want to415* find out if this is due to an EEH slot freeze. This routine416* will query firmware for the EEH status.417*418* Returns 0 if there has not been an EEH error; otherwise returns419* a non-zero value and queues up a slot isolation event notification.420*421* It is safe to call this routine in an interrupt context.422*/423int eeh_dev_check_failure(struct eeh_dev *edev)424{425int ret;426unsigned long flags;427struct device_node *dn;428struct pci_dev *dev;429struct eeh_pe *pe, *parent_pe;430int rc = 0;431const char *location = NULL;432433eeh_stats.total_mmio_ffs++;434435if (!eeh_enabled())436return 0;437438if (!edev) {439eeh_stats.no_dn++;440return 0;441}442dev = eeh_dev_to_pci_dev(edev);443pe = eeh_dev_to_pe(edev);444445/* Access to IO BARs might get this far and still not want checking. */446if (!pe) {447eeh_stats.ignored_check++;448eeh_edev_dbg(edev, "Ignored check\n");449return 0;450}451452/*453* On PowerNV platform, we might already have fenced PHB454* there and we need take care of that firstly.455*/456ret = eeh_phb_check_failure(pe);457if (ret > 0)458return ret;459460/*461* If the PE isn't owned by us, we shouldn't check the462* state. Instead, let the owner handle it if the PE has463* been frozen.464*/465if (eeh_pe_passed(pe))466return 0;467468/* If we already have a pending isolation event for this469* slot, we know it's bad already, we don't need to check.470* Do this checking under a lock; as multiple PCI devices471* in one slot might report errors simultaneously, and we472* only want one error recovery routine running.473*/474eeh_serialize_lock(&flags);475rc = 1;476if (pe->state & EEH_PE_ISOLATED) {477pe->check_count++;478if (pe->check_count == EEH_MAX_FAILS) {479dn = pci_device_to_OF_node(dev);480if (dn)481location = of_get_property(dn, "ibm,loc-code",482NULL);483eeh_edev_err(edev, "%d reads ignored for recovering device at location=%s driver=%s\n",484pe->check_count,485location ? location : "unknown",486eeh_driver_name(dev));487eeh_edev_err(edev, "Might be infinite loop in %s driver\n",488eeh_driver_name(dev));489dump_stack();490}491goto dn_unlock;492}493494/*495* Now test for an EEH failure. This is VERY expensive.496* Note that the eeh_config_addr may be a parent device497* in the case of a device behind a bridge, or it may be498* function zero of a multi-function device.499* In any case they must share a common PHB.500*/501ret = eeh_ops->get_state(pe, NULL);502503/* Note that config-io to empty slots may fail;504* they are empty when they don't have children.505* We will punt with the following conditions: Failure to get506* PE's state, EEH not support and Permanently unavailable507* state, PE is in good state.508*509* On the pSeries, after reaching the threshold, get_state might510* return EEH_STATE_NOT_SUPPORT. However, it's possible that the511* device state remains uncleared if the device is not marked512* pci_channel_io_perm_failure. Therefore, consider logging the513* event to let device removal happen.514*515*/516if ((ret < 0) ||517(ret == EEH_STATE_NOT_SUPPORT &&518dev->error_state == pci_channel_io_perm_failure) ||519eeh_state_active(ret)) {520eeh_stats.false_positives++;521pe->false_positives++;522rc = 0;523goto dn_unlock;524}525526/*527* It should be corner case that the parent PE has been528* put into frozen state as well. We should take care529* that at first.530*/531parent_pe = pe->parent;532while (parent_pe) {533/* Hit the ceiling ? */534if (parent_pe->type & EEH_PE_PHB)535break;536537/* Frozen parent PE ? */538ret = eeh_ops->get_state(parent_pe, NULL);539if (ret > 0 && !eeh_state_active(ret)) {540pe = parent_pe;541pr_err("EEH: Failure of PHB#%x-PE#%x will be handled at parent PHB#%x-PE#%x.\n",542pe->phb->global_number, pe->addr,543pe->phb->global_number, parent_pe->addr);544}545546/* Next parent level */547parent_pe = parent_pe->parent;548}549550eeh_stats.slot_resets++;551552/* Avoid repeated reports of this failure, including problems553* with other functions on this device, and functions under554* bridges.555*/556eeh_pe_mark_isolated(pe);557eeh_serialize_unlock(flags);558559/* Most EEH events are due to device driver bugs. Having560* a stack trace will help the device-driver authors figure561* out what happened. So print that out.562*/563pr_debug("EEH: %s: Frozen PHB#%x-PE#%x detected\n",564__func__, pe->phb->global_number, pe->addr);565eeh_send_failure_event(pe);566567return 1;568569dn_unlock:570eeh_serialize_unlock(flags);571return rc;572}573574EXPORT_SYMBOL_GPL(eeh_dev_check_failure);575576/**577* eeh_check_failure - Check if all 1's data is due to EEH slot freeze578* @token: I/O address579*580* Check for an EEH failure at the given I/O address. Call this581* routine if the result of a read was all 0xff's and you want to582* find out if this is due to an EEH slot freeze event. This routine583* will query firmware for the EEH status.584*585* Note this routine is safe to call in an interrupt context.586*/587int eeh_check_failure(const volatile void __iomem *token)588{589unsigned long addr;590struct eeh_dev *edev;591592/* Finding the phys addr + pci device; this is pretty quick. */593addr = eeh_token_to_phys((unsigned long __force) token);594edev = eeh_addr_cache_get_dev(addr);595if (!edev) {596eeh_stats.no_device++;597return 0;598}599600return eeh_dev_check_failure(edev);601}602EXPORT_SYMBOL(eeh_check_failure);603604605/**606* eeh_pci_enable - Enable MMIO or DMA transfers for this slot607* @pe: EEH PE608* @function: EEH option609*610* This routine should be called to reenable frozen MMIO or DMA611* so that it would work correctly again. It's useful while doing612* recovery or log collection on the indicated device.613*/614int eeh_pci_enable(struct eeh_pe *pe, int function)615{616int active_flag, rc;617618/*619* pHyp doesn't allow to enable IO or DMA on unfrozen PE.620* Also, it's pointless to enable them on unfrozen PE. So621* we have to check before enabling IO or DMA.622*/623switch (function) {624case EEH_OPT_THAW_MMIO:625active_flag = EEH_STATE_MMIO_ACTIVE | EEH_STATE_MMIO_ENABLED;626break;627case EEH_OPT_THAW_DMA:628active_flag = EEH_STATE_DMA_ACTIVE;629break;630case EEH_OPT_DISABLE:631case EEH_OPT_ENABLE:632case EEH_OPT_FREEZE_PE:633active_flag = 0;634break;635default:636pr_warn("%s: Invalid function %d\n",637__func__, function);638return -EINVAL;639}640641/*642* Check if IO or DMA has been enabled before643* enabling them.644*/645if (active_flag) {646rc = eeh_ops->get_state(pe, NULL);647if (rc < 0)648return rc;649650/* Needn't enable it at all */651if (rc == EEH_STATE_NOT_SUPPORT)652return 0;653654/* It's already enabled */655if (rc & active_flag)656return 0;657}658659660/* Issue the request */661rc = eeh_ops->set_option(pe, function);662if (rc)663pr_warn("%s: Unexpected state change %d on "664"PHB#%x-PE#%x, err=%d\n",665__func__, function, pe->phb->global_number,666pe->addr, rc);667668/* Check if the request is finished successfully */669if (active_flag) {670rc = eeh_wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);671if (rc < 0)672return rc;673674if (rc & active_flag)675return 0;676677return -EIO;678}679680return rc;681}682683static void eeh_disable_and_save_dev_state(struct eeh_dev *edev,684void *userdata)685{686struct pci_dev *pdev = eeh_dev_to_pci_dev(edev);687struct pci_dev *dev = userdata;688689/*690* The caller should have disabled and saved the691* state for the specified device692*/693if (!pdev || pdev == dev)694return;695696/* Ensure we have D0 power state */697pci_set_power_state(pdev, PCI_D0);698699/* Save device state */700pci_save_state(pdev);701702/*703* Disable device to avoid any DMA traffic and704* interrupt from the device705*/706pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);707}708709static void eeh_restore_dev_state(struct eeh_dev *edev, void *userdata)710{711struct pci_dev *pdev = eeh_dev_to_pci_dev(edev);712struct pci_dev *dev = userdata;713714if (!pdev)715return;716717/* Apply customization from firmware */718if (eeh_ops->restore_config)719eeh_ops->restore_config(edev);720721/* The caller should restore state for the specified device */722if (pdev != dev)723pci_restore_state(pdev);724}725726/**727* pcibios_set_pcie_reset_state - Set PCI-E reset state728* @dev: pci device struct729* @state: reset state to enter730*731* Return value:732* 0 if success733*/734int pcibios_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)735{736struct eeh_dev *edev = pci_dev_to_eeh_dev(dev);737struct eeh_pe *pe = eeh_dev_to_pe(edev);738739if (!pe) {740pr_err("%s: No PE found on PCI device %s\n",741__func__, pci_name(dev));742return -EINVAL;743}744745switch (state) {746case pcie_deassert_reset:747eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);748eeh_unfreeze_pe(pe);749if (!(pe->type & EEH_PE_VF))750eeh_pe_state_clear(pe, EEH_PE_CFG_BLOCKED, true);751eeh_pe_dev_traverse(pe, eeh_restore_dev_state, dev);752eeh_pe_state_clear(pe, EEH_PE_ISOLATED, true);753break;754case pcie_hot_reset:755eeh_pe_mark_isolated(pe);756eeh_pe_state_clear(pe, EEH_PE_CFG_BLOCKED, true);757eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);758eeh_pe_dev_traverse(pe, eeh_disable_and_save_dev_state, dev);759if (!(pe->type & EEH_PE_VF))760eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);761eeh_ops->reset(pe, EEH_RESET_HOT);762break;763case pcie_warm_reset:764eeh_pe_mark_isolated(pe);765eeh_pe_state_clear(pe, EEH_PE_CFG_BLOCKED, true);766eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);767eeh_pe_dev_traverse(pe, eeh_disable_and_save_dev_state, dev);768if (!(pe->type & EEH_PE_VF))769eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);770eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);771break;772default:773eeh_pe_state_clear(pe, EEH_PE_ISOLATED | EEH_PE_CFG_BLOCKED, true);774return -EINVAL;775}776777return 0;778}779780/**781* eeh_set_dev_freset - Check the required reset for the indicated device782* @edev: EEH device783* @flag: return value784*785* Each device might have its preferred reset type: fundamental or786* hot reset. The routine is used to collected the information for787* the indicated device and its children so that the bunch of the788* devices could be reset properly.789*/790static void eeh_set_dev_freset(struct eeh_dev *edev, void *flag)791{792struct pci_dev *dev;793unsigned int *freset = (unsigned int *)flag;794795dev = eeh_dev_to_pci_dev(edev);796if (dev)797*freset |= dev->needs_freset;798}799800static void eeh_pe_refreeze_passed(struct eeh_pe *root)801{802struct eeh_pe *pe;803int state;804805eeh_for_each_pe(root, pe) {806if (eeh_pe_passed(pe)) {807state = eeh_ops->get_state(pe, NULL);808if (state &809(EEH_STATE_MMIO_ACTIVE | EEH_STATE_MMIO_ENABLED)) {810pr_info("EEH: Passed-through PE PHB#%x-PE#%x was thawed by reset, re-freezing for safety.\n",811pe->phb->global_number, pe->addr);812eeh_pe_set_option(pe, EEH_OPT_FREEZE_PE);813}814}815}816}817818/**819* eeh_pe_reset_full - Complete a full reset process on the indicated PE820* @pe: EEH PE821* @include_passed: include passed-through devices?822*823* This function executes a full reset procedure on a PE, including setting824* the appropriate flags, performing a fundamental or hot reset, and then825* deactivating the reset status. It is designed to be used within the EEH826* subsystem, as opposed to eeh_pe_reset which is exported to drivers and827* only performs a single operation at a time.828*829* This function will attempt to reset a PE three times before failing.830*/831int eeh_pe_reset_full(struct eeh_pe *pe, bool include_passed)832{833int reset_state = (EEH_PE_RESET | EEH_PE_CFG_BLOCKED);834int type = EEH_RESET_HOT;835unsigned int freset = 0;836int i, state = 0, ret;837838/*839* Determine the type of reset to perform - hot or fundamental.840* Hot reset is the default operation, unless any device under the841* PE requires a fundamental reset.842*/843eeh_pe_dev_traverse(pe, eeh_set_dev_freset, &freset);844845if (freset)846type = EEH_RESET_FUNDAMENTAL;847848/* Mark the PE as in reset state and block config space accesses */849eeh_pe_state_mark(pe, reset_state);850851/* Make three attempts at resetting the bus */852for (i = 0; i < 3; i++) {853ret = eeh_pe_reset(pe, type, include_passed);854if (!ret)855ret = eeh_pe_reset(pe, EEH_RESET_DEACTIVATE,856include_passed);857if (ret) {858ret = -EIO;859pr_warn("EEH: Failure %d resetting PHB#%x-PE#%x (attempt %d)\n\n",860state, pe->phb->global_number, pe->addr, i + 1);861continue;862}863if (i)864pr_warn("EEH: PHB#%x-PE#%x: Successful reset (attempt %d)\n",865pe->phb->global_number, pe->addr, i + 1);866867/* Wait until the PE is in a functioning state */868state = eeh_wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);869if (state < 0) {870pr_warn("EEH: Unrecoverable slot failure on PHB#%x-PE#%x",871pe->phb->global_number, pe->addr);872ret = -ENOTRECOVERABLE;873break;874}875if (eeh_state_active(state))876break;877else878pr_warn("EEH: PHB#%x-PE#%x: Slot inactive after reset: 0x%x (attempt %d)\n",879pe->phb->global_number, pe->addr, state, i + 1);880}881882/* Resetting the PE may have unfrozen child PEs. If those PEs have been883* (potentially) passed through to a guest, re-freeze them:884*/885if (!include_passed)886eeh_pe_refreeze_passed(pe);887888eeh_pe_state_clear(pe, reset_state, true);889return ret;890}891892/**893* eeh_save_bars - Save device bars894* @edev: PCI device associated EEH device895*896* Save the values of the device bars. Unlike the restore897* routine, this routine is *not* recursive. This is because898* PCI devices are added individually; but, for the restore,899* an entire slot is reset at a time.900*/901void eeh_save_bars(struct eeh_dev *edev)902{903int i;904905if (!edev)906return;907908for (i = 0; i < 16; i++)909eeh_ops->read_config(edev, i * 4, 4, &edev->config_space[i]);910911/*912* For PCI bridges including root port, we need enable bus913* master explicitly. Otherwise, it can't fetch IODA table914* entries correctly. So we cache the bit in advance so that915* we can restore it after reset, either PHB range or PE range.916*/917if (edev->mode & EEH_DEV_BRIDGE)918edev->config_space[1] |= PCI_COMMAND_MASTER;919}920921static int eeh_reboot_notifier(struct notifier_block *nb,922unsigned long action, void *unused)923{924eeh_clear_flag(EEH_ENABLED);925return NOTIFY_DONE;926}927928static struct notifier_block eeh_reboot_nb = {929.notifier_call = eeh_reboot_notifier,930};931932static int eeh_device_notifier(struct notifier_block *nb,933unsigned long action, void *data)934{935struct device *dev = data;936937switch (action) {938/*939* Note: It's not possible to perform EEH device addition (i.e.940* {pseries,pnv}_pcibios_bus_add_device()) here because it depends on941* the device's resources, which have not yet been set up.942*/943case BUS_NOTIFY_DEL_DEVICE:944eeh_remove_device(to_pci_dev(dev));945break;946default:947break;948}949return NOTIFY_DONE;950}951952static struct notifier_block eeh_device_nb = {953.notifier_call = eeh_device_notifier,954};955956/**957* eeh_init - System wide EEH initialization958* @ops: struct to trace EEH operation callback functions959*960* It's the platform's job to call this from an arch_initcall().961*/962int eeh_init(struct eeh_ops *ops)963{964struct pci_controller *hose, *tmp;965int ret = 0;966967/* the platform should only initialise EEH once */968if (WARN_ON(eeh_ops))969return -EEXIST;970if (WARN_ON(!ops))971return -ENOENT;972eeh_ops = ops;973974/* Register reboot notifier */975ret = register_reboot_notifier(&eeh_reboot_nb);976if (ret) {977pr_warn("%s: Failed to register reboot notifier (%d)\n",978__func__, ret);979return ret;980}981982ret = bus_register_notifier(&pci_bus_type, &eeh_device_nb);983if (ret) {984pr_warn("%s: Failed to register bus notifier (%d)\n",985__func__, ret);986return ret;987}988989/* Initialize PHB PEs */990list_for_each_entry_safe(hose, tmp, &hose_list, list_node)991eeh_phb_pe_create(hose);992993eeh_addr_cache_init();994995/* Initialize EEH event */996return eeh_event_init();997}998999/**1000* eeh_probe_device() - Perform EEH initialization for the indicated pci device1001* @dev: pci device for which to set up EEH1002*1003* This routine must be used to complete EEH initialization for PCI1004* devices that were added after system boot (e.g. hotplug, dlpar).1005*/1006void eeh_probe_device(struct pci_dev *dev)1007{1008struct eeh_dev *edev;10091010pr_debug("EEH: Adding device %s\n", pci_name(dev));10111012/*1013* pci_dev_to_eeh_dev() can only work if eeh_probe_dev() was1014* already called for this device.1015*/1016if (WARN_ON_ONCE(pci_dev_to_eeh_dev(dev))) {1017pci_dbg(dev, "Already bound to an eeh_dev!\n");1018return;1019}10201021edev = eeh_ops->probe(dev);1022if (!edev) {1023pr_debug("EEH: Adding device failed\n");1024return;1025}10261027/*1028* FIXME: We rely on pcibios_release_device() to remove the1029* existing EEH state. The release function is only called if1030* the pci_dev's refcount drops to zero so if something is1031* keeping a ref to a device (e.g. a filesystem) we need to1032* remove the old EEH state.1033*1034* FIXME: HEY MA, LOOK AT ME, NO LOCKING!1035*/1036if (edev->pdev && edev->pdev != dev) {1037eeh_pe_tree_remove(edev);1038eeh_addr_cache_rmv_dev(edev->pdev);1039eeh_sysfs_remove_device(edev->pdev);10401041/*1042* We definitely should have the PCI device removed1043* though it wasn't correctly. So we needn't call1044* into error handler afterwards.1045*/1046edev->mode |= EEH_DEV_NO_HANDLER;1047}10481049/* bind the pdev and the edev together */1050edev->pdev = dev;1051dev->dev.archdata.edev = edev;1052eeh_addr_cache_insert_dev(dev);1053eeh_sysfs_add_device(dev);1054}10551056/**1057* eeh_remove_device - Undo EEH setup for the indicated pci device1058* @dev: pci device to be removed1059*1060* This routine should be called when a device is removed from1061* a running system (e.g. by hotplug or dlpar). It unregisters1062* the PCI device from the EEH subsystem. I/O errors affecting1063* this device will no longer be detected after this call; thus,1064* i/o errors affecting this slot may leave this device unusable.1065*/1066void eeh_remove_device(struct pci_dev *dev)1067{1068struct eeh_dev *edev;10691070if (!dev || !eeh_enabled())1071return;1072edev = pci_dev_to_eeh_dev(dev);10731074/* Unregister the device with the EEH/PCI address search system */1075dev_dbg(&dev->dev, "EEH: Removing device\n");10761077if (!edev || !edev->pdev || !edev->pe) {1078dev_dbg(&dev->dev, "EEH: Device not referenced!\n");1079return;1080}10811082/*1083* During the hotplug for EEH error recovery, we need the EEH1084* device attached to the parent PE in order for BAR restore1085* a bit later. So we keep it for BAR restore and remove it1086* from the parent PE during the BAR resotre.1087*/1088edev->pdev = NULL;10891090/*1091* eeh_sysfs_remove_device() uses pci_dev_to_eeh_dev() so we need to1092* remove the sysfs files before clearing dev.archdata.edev1093*/1094if (edev->mode & EEH_DEV_SYSFS)1095eeh_sysfs_remove_device(dev);10961097/*1098* We're removing from the PCI subsystem, that means1099* the PCI device driver can't support EEH or not1100* well. So we rely on hotplug completely to do recovery1101* for the specific PCI device.1102*/1103edev->mode |= EEH_DEV_NO_HANDLER;11041105eeh_addr_cache_rmv_dev(dev);11061107/*1108* The flag "in_error" is used to trace EEH devices for VFs1109* in error state or not. It's set in eeh_report_error(). If1110* it's not set, eeh_report_{reset,resume}() won't be called1111* for the VF EEH device.1112*/1113edev->in_error = false;1114dev->dev.archdata.edev = NULL;1115if (!(edev->pe->state & EEH_PE_KEEP))1116eeh_pe_tree_remove(edev);1117else1118edev->mode |= EEH_DEV_DISCONNECTED;1119}11201121int eeh_unfreeze_pe(struct eeh_pe *pe)1122{1123int ret;11241125ret = eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);1126if (ret) {1127pr_warn("%s: Failure %d enabling IO on PHB#%x-PE#%x\n",1128__func__, ret, pe->phb->global_number, pe->addr);1129return ret;1130}11311132ret = eeh_pci_enable(pe, EEH_OPT_THAW_DMA);1133if (ret) {1134pr_warn("%s: Failure %d enabling DMA on PHB#%x-PE#%x\n",1135__func__, ret, pe->phb->global_number, pe->addr);1136return ret;1137}11381139return ret;1140}1141EXPORT_SYMBOL_GPL(eeh_unfreeze_pe);114211431144static struct pci_device_id eeh_reset_ids[] = {1145{ PCI_DEVICE(0x19a2, 0x0710) }, /* Emulex, BE */1146{ PCI_DEVICE(0x10df, 0xe220) }, /* Emulex, Lancer */1147{ PCI_DEVICE(0x14e4, 0x1657) }, /* Broadcom BCM5719 */1148{ 0 }1149};11501151static int eeh_pe_change_owner(struct eeh_pe *pe)1152{1153struct eeh_dev *edev, *tmp;1154struct pci_dev *pdev;1155struct pci_device_id *id;1156int ret;11571158/* Check PE state */1159ret = eeh_ops->get_state(pe, NULL);1160if (ret < 0 || ret == EEH_STATE_NOT_SUPPORT)1161return 0;11621163/* Unfrozen PE, nothing to do */1164if (eeh_state_active(ret))1165return 0;11661167/* Frozen PE, check if it needs PE level reset */1168eeh_pe_for_each_dev(pe, edev, tmp) {1169pdev = eeh_dev_to_pci_dev(edev);1170if (!pdev)1171continue;11721173for (id = &eeh_reset_ids[0]; id->vendor != 0; id++) {1174if (id->vendor != PCI_ANY_ID &&1175id->vendor != pdev->vendor)1176continue;1177if (id->device != PCI_ANY_ID &&1178id->device != pdev->device)1179continue;1180if (id->subvendor != PCI_ANY_ID &&1181id->subvendor != pdev->subsystem_vendor)1182continue;1183if (id->subdevice != PCI_ANY_ID &&1184id->subdevice != pdev->subsystem_device)1185continue;11861187return eeh_pe_reset_and_recover(pe);1188}1189}11901191ret = eeh_unfreeze_pe(pe);1192if (!ret)1193eeh_pe_state_clear(pe, EEH_PE_ISOLATED, true);1194return ret;1195}11961197/**1198* eeh_dev_open - Increase count of pass through devices for PE1199* @pdev: PCI device1200*1201* Increase count of passed through devices for the indicated1202* PE. In the result, the EEH errors detected on the PE won't be1203* reported. The PE owner will be responsible for detection1204* and recovery.1205*/1206int eeh_dev_open(struct pci_dev *pdev)1207{1208struct eeh_dev *edev;1209int ret = -ENODEV;12101211guard(mutex)(&eeh_dev_mutex);12121213/* No PCI device ? */1214if (!pdev)1215return ret;12161217/* No EEH device or PE ? */1218edev = pci_dev_to_eeh_dev(pdev);1219if (!edev || !edev->pe)1220return ret;12211222/*1223* The PE might have been put into frozen state, but we1224* didn't detect that yet. The passed through PCI devices1225* in frozen PE won't work properly. Clear the frozen state1226* in advance.1227*/1228ret = eeh_pe_change_owner(edev->pe);1229if (ret)1230return ret;12311232/* Increase PE's pass through count */1233atomic_inc(&edev->pe->pass_dev_cnt);12341235return 0;1236}1237EXPORT_SYMBOL_GPL(eeh_dev_open);12381239/**1240* eeh_dev_release - Decrease count of pass through devices for PE1241* @pdev: PCI device1242*1243* Decrease count of pass through devices for the indicated PE. If1244* there is no passed through device in PE, the EEH errors detected1245* on the PE will be reported and handled as usual.1246*/1247void eeh_dev_release(struct pci_dev *pdev)1248{1249struct eeh_dev *edev;12501251guard(mutex)(&eeh_dev_mutex);12521253/* No PCI device ? */1254if (!pdev)1255return;12561257/* No EEH device ? */1258edev = pci_dev_to_eeh_dev(pdev);1259if (!edev || !edev->pe || !eeh_pe_passed(edev->pe))1260return;12611262/* Decrease PE's pass through count */1263WARN_ON(atomic_dec_if_positive(&edev->pe->pass_dev_cnt) < 0);1264eeh_pe_change_owner(edev->pe);1265}1266EXPORT_SYMBOL(eeh_dev_release);12671268#ifdef CONFIG_IOMMU_API12691270/**1271* eeh_iommu_group_to_pe - Convert IOMMU group to EEH PE1272* @group: IOMMU group1273*1274* The routine is called to convert IOMMU group to EEH PE.1275*/1276struct eeh_pe *eeh_iommu_group_to_pe(struct iommu_group *group)1277{1278struct pci_dev *pdev = NULL;1279struct eeh_dev *edev;1280int ret;12811282/* No IOMMU group ? */1283if (!group)1284return NULL;12851286ret = iommu_group_for_each_dev(group, &pdev, dev_has_iommu_table);1287if (!ret || !pdev)1288return NULL;12891290/* No EEH device or PE ? */1291edev = pci_dev_to_eeh_dev(pdev);1292if (!edev || !edev->pe)1293return NULL;12941295return edev->pe;1296}1297EXPORT_SYMBOL_GPL(eeh_iommu_group_to_pe);12981299#endif /* CONFIG_IOMMU_API */13001301/**1302* eeh_pe_set_option - Set options for the indicated PE1303* @pe: EEH PE1304* @option: requested option1305*1306* The routine is called to enable or disable EEH functionality1307* on the indicated PE, to enable IO or DMA for the frozen PE.1308*/1309int eeh_pe_set_option(struct eeh_pe *pe, int option)1310{1311int ret = 0;13121313/* Invalid PE ? */1314if (!pe)1315return -ENODEV;13161317/*1318* EEH functionality could possibly be disabled, just1319* return error for the case. And the EEH functionality1320* isn't expected to be disabled on one specific PE.1321*/1322switch (option) {1323case EEH_OPT_ENABLE:1324if (eeh_enabled()) {1325ret = eeh_pe_change_owner(pe);1326break;1327}1328ret = -EIO;1329break;1330case EEH_OPT_DISABLE:1331break;1332case EEH_OPT_THAW_MMIO:1333case EEH_OPT_THAW_DMA:1334case EEH_OPT_FREEZE_PE:1335if (!eeh_ops || !eeh_ops->set_option) {1336ret = -ENOENT;1337break;1338}13391340ret = eeh_pci_enable(pe, option);1341break;1342default:1343pr_debug("%s: Option %d out of range (%d, %d)\n",1344__func__, option, EEH_OPT_DISABLE, EEH_OPT_THAW_DMA);1345ret = -EINVAL;1346}13471348return ret;1349}1350EXPORT_SYMBOL_GPL(eeh_pe_set_option);13511352/**1353* eeh_pe_get_state - Retrieve PE's state1354* @pe: EEH PE1355*1356* Retrieve the PE's state, which includes 3 aspects: enabled1357* DMA, enabled IO and asserted reset.1358*/1359int eeh_pe_get_state(struct eeh_pe *pe)1360{1361int result, ret = 0;1362bool rst_active, dma_en, mmio_en;13631364/* Existing PE ? */1365if (!pe)1366return -ENODEV;13671368if (!eeh_ops || !eeh_ops->get_state)1369return -ENOENT;13701371/*1372* If the parent PE is owned by the host kernel and is undergoing1373* error recovery, we should return the PE state as temporarily1374* unavailable so that the error recovery on the guest is suspended1375* until the recovery completes on the host.1376*/1377if (pe->parent &&1378!(pe->state & EEH_PE_REMOVED) &&1379(pe->parent->state & (EEH_PE_ISOLATED | EEH_PE_RECOVERING)))1380return EEH_PE_STATE_UNAVAIL;13811382result = eeh_ops->get_state(pe, NULL);1383rst_active = !!(result & EEH_STATE_RESET_ACTIVE);1384dma_en = !!(result & EEH_STATE_DMA_ENABLED);1385mmio_en = !!(result & EEH_STATE_MMIO_ENABLED);13861387if (rst_active)1388ret = EEH_PE_STATE_RESET;1389else if (dma_en && mmio_en)1390ret = EEH_PE_STATE_NORMAL;1391else if (!dma_en && !mmio_en)1392ret = EEH_PE_STATE_STOPPED_IO_DMA;1393else if (!dma_en && mmio_en)1394ret = EEH_PE_STATE_STOPPED_DMA;1395else1396ret = EEH_PE_STATE_UNAVAIL;13971398return ret;1399}1400EXPORT_SYMBOL_GPL(eeh_pe_get_state);14011402static int eeh_pe_reenable_devices(struct eeh_pe *pe, bool include_passed)1403{1404struct eeh_dev *edev, *tmp;1405struct pci_dev *pdev;1406int ret = 0;14071408eeh_pe_restore_bars(pe);14091410/*1411* Reenable PCI devices as the devices passed1412* through are always enabled before the reset.1413*/1414eeh_pe_for_each_dev(pe, edev, tmp) {1415pdev = eeh_dev_to_pci_dev(edev);1416if (!pdev)1417continue;14181419ret = pci_reenable_device(pdev);1420if (ret) {1421pr_warn("%s: Failure %d reenabling %s\n",1422__func__, ret, pci_name(pdev));1423return ret;1424}1425}14261427/* The PE is still in frozen state */1428if (include_passed || !eeh_pe_passed(pe)) {1429ret = eeh_unfreeze_pe(pe);1430} else1431pr_info("EEH: Note: Leaving passthrough PHB#%x-PE#%x frozen.\n",1432pe->phb->global_number, pe->addr);1433if (!ret)1434eeh_pe_state_clear(pe, EEH_PE_ISOLATED, include_passed);1435return ret;1436}143714381439/**1440* eeh_pe_reset - Issue PE reset according to specified type1441* @pe: EEH PE1442* @option: reset type1443* @include_passed: include passed-through devices?1444*1445* The routine is called to reset the specified PE with the1446* indicated type, either fundamental reset or hot reset.1447* PE reset is the most important part for error recovery.1448*/1449int eeh_pe_reset(struct eeh_pe *pe, int option, bool include_passed)1450{1451int ret = 0;14521453/* Invalid PE ? */1454if (!pe)1455return -ENODEV;14561457if (!eeh_ops || !eeh_ops->set_option || !eeh_ops->reset)1458return -ENOENT;14591460switch (option) {1461case EEH_RESET_DEACTIVATE:1462ret = eeh_ops->reset(pe, option);1463eeh_pe_state_clear(pe, EEH_PE_CFG_BLOCKED, include_passed);1464if (ret)1465break;14661467ret = eeh_pe_reenable_devices(pe, include_passed);1468break;1469case EEH_RESET_HOT:1470case EEH_RESET_FUNDAMENTAL:1471/*1472* Proactively freeze the PE to drop all MMIO access1473* during reset, which should be banned as it's always1474* cause recursive EEH error.1475*/1476eeh_ops->set_option(pe, EEH_OPT_FREEZE_PE);14771478eeh_pe_state_mark(pe, EEH_PE_CFG_BLOCKED);1479ret = eeh_ops->reset(pe, option);1480break;1481default:1482pr_debug("%s: Unsupported option %d\n",1483__func__, option);1484ret = -EINVAL;1485}14861487return ret;1488}1489EXPORT_SYMBOL_GPL(eeh_pe_reset);14901491/**1492* eeh_pe_configure - Configure PCI bridges after PE reset1493* @pe: EEH PE1494*1495* The routine is called to restore the PCI config space for1496* those PCI devices, especially PCI bridges affected by PE1497* reset issued previously.1498*/1499int eeh_pe_configure(struct eeh_pe *pe)1500{1501int ret = 0;15021503/* Invalid PE ? */1504if (!pe)1505return -ENODEV;1506else1507ret = eeh_ops->configure_bridge(pe);15081509return ret;1510}1511EXPORT_SYMBOL_GPL(eeh_pe_configure);15121513/**1514* eeh_pe_inject_err - Injecting the specified PCI error to the indicated PE1515* @pe: the indicated PE1516* @type: error type1517* @func: error function1518* @addr: address1519* @mask: address mask1520*1521* The routine is called to inject the specified PCI error, which1522* is determined by @type and @func, to the indicated PE for1523* testing purpose.1524*/1525int eeh_pe_inject_err(struct eeh_pe *pe, int type, int func,1526unsigned long addr, unsigned long mask)1527{1528/* Invalid PE ? */1529if (!pe)1530return -ENODEV;15311532/* Unsupported operation ? */1533if (!eeh_ops || !eeh_ops->err_inject)1534return -ENOENT;15351536/* Check on PCI error function */1537if (func < EEH_ERR_FUNC_MIN || func > EEH_ERR_FUNC_MAX)1538return -EINVAL;15391540return eeh_ops->err_inject(pe, type, func, addr, mask);1541}1542EXPORT_SYMBOL_GPL(eeh_pe_inject_err);15431544#ifdef CONFIG_PROC_FS1545static int proc_eeh_show(struct seq_file *m, void *v)1546{1547if (!eeh_enabled()) {1548seq_printf(m, "EEH Subsystem is globally disabled\n");1549seq_printf(m, "eeh_total_mmio_ffs=%llu\n", eeh_stats.total_mmio_ffs);1550} else {1551seq_printf(m, "EEH Subsystem is enabled\n");1552seq_printf(m,1553"no device=%llu\n"1554"no device node=%llu\n"1555"no config address=%llu\n"1556"check not wanted=%llu\n"1557"eeh_total_mmio_ffs=%llu\n"1558"eeh_false_positives=%llu\n"1559"eeh_slot_resets=%llu\n",1560eeh_stats.no_device,1561eeh_stats.no_dn,1562eeh_stats.no_cfg_addr,1563eeh_stats.ignored_check,1564eeh_stats.total_mmio_ffs,1565eeh_stats.false_positives,1566eeh_stats.slot_resets);1567}15681569return 0;1570}1571#endif /* CONFIG_PROC_FS */15721573static int eeh_break_device(struct pci_dev *pdev)1574{1575struct resource *bar = NULL;1576void __iomem *mapped;1577u16 old, bit;1578int i, pos;15791580/* Do we have an MMIO BAR to disable? */1581for (i = 0; i <= PCI_STD_RESOURCE_END; i++) {1582struct resource *r = &pdev->resource[i];15831584if (!r->flags || !r->start)1585continue;1586if (r->flags & IORESOURCE_IO)1587continue;1588if (r->flags & IORESOURCE_UNSET)1589continue;15901591bar = r;1592break;1593}15941595if (!bar) {1596pci_err(pdev, "Unable to find Memory BAR to cause EEH with\n");1597return -ENXIO;1598}15991600pci_err(pdev, "Going to break: %pR\n", bar);16011602if (pdev->is_virtfn) {1603#ifndef CONFIG_PCI_IOV1604return -ENXIO;1605#else1606/*1607* VFs don't have a per-function COMMAND register, so the best1608* we can do is clear the Memory Space Enable bit in the PF's1609* SRIOV control reg.1610*1611* Unfortunately, this requires that we have a PF (i.e doesn't1612* work for a passed-through VF) and it has the potential side1613* effect of also causing an EEH on every other VF under the1614* PF. Oh well.1615*/1616pdev = pdev->physfn;1617if (!pdev)1618return -ENXIO; /* passed through VFs have no PF */16191620pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);1621pos += PCI_SRIOV_CTRL;1622bit = PCI_SRIOV_CTRL_MSE;1623#endif /* !CONFIG_PCI_IOV */1624} else {1625bit = PCI_COMMAND_MEMORY;1626pos = PCI_COMMAND;1627}16281629/*1630* Process here is:1631*1632* 1. Disable Memory space.1633*1634* 2. Perform an MMIO to the device. This should result in an error1635* (CA / UR) being raised by the device which results in an EEH1636* PE freeze. Using the in_8() accessor skips the eeh detection hook1637* so the freeze hook so the EEH Detection machinery won't be1638* triggered here. This is to match the usual behaviour of EEH1639* where the HW will asynchronously freeze a PE and it's up to1640* the kernel to notice and deal with it.1641*1642* 3. Turn Memory space back on. This is more important for VFs1643* since recovery will probably fail if we don't. For normal1644* the COMMAND register is reset as a part of re-initialising1645* the device.1646*1647* Breaking stuff is the point so who cares if it's racy ;)1648*/1649pci_read_config_word(pdev, pos, &old);16501651mapped = ioremap(bar->start, PAGE_SIZE);1652if (!mapped) {1653pci_err(pdev, "Unable to map MMIO BAR %pR\n", bar);1654return -ENXIO;1655}16561657pci_write_config_word(pdev, pos, old & ~bit);1658in_8(mapped);1659pci_write_config_word(pdev, pos, old);16601661iounmap(mapped);16621663return 0;1664}16651666int eeh_pe_inject_mmio_error(struct pci_dev *pdev)1667{1668return eeh_break_device(pdev);1669}16701671#ifdef CONFIG_DEBUG_FS167216731674static struct pci_dev *eeh_debug_lookup_pdev(struct file *filp,1675const char __user *user_buf,1676size_t count, loff_t *ppos)1677{1678uint32_t domain, bus, dev, fn;1679struct pci_dev *pdev;1680char buf[20];1681int ret;16821683memset(buf, 0, sizeof(buf));1684ret = simple_write_to_buffer(buf, sizeof(buf)-1, ppos, user_buf, count);1685if (!ret)1686return ERR_PTR(-EFAULT);16871688ret = sscanf(buf, "%x:%x:%x.%x", &domain, &bus, &dev, &fn);1689if (ret != 4) {1690pr_err("%s: expected 4 args, got %d\n", __func__, ret);1691return ERR_PTR(-EINVAL);1692}16931694pdev = pci_get_domain_bus_and_slot(domain, bus, (dev << 3) | fn);1695if (!pdev)1696return ERR_PTR(-ENODEV);16971698return pdev;1699}17001701static int eeh_enable_dbgfs_set(void *data, u64 val)1702{1703if (val)1704eeh_clear_flag(EEH_FORCE_DISABLED);1705else1706eeh_add_flag(EEH_FORCE_DISABLED);17071708return 0;1709}17101711static int eeh_enable_dbgfs_get(void *data, u64 *val)1712{1713if (eeh_enabled())1714*val = 0x1ul;1715else1716*val = 0x0ul;1717return 0;1718}17191720DEFINE_DEBUGFS_ATTRIBUTE(eeh_enable_dbgfs_ops, eeh_enable_dbgfs_get,1721eeh_enable_dbgfs_set, "0x%llx\n");17221723static ssize_t eeh_force_recover_write(struct file *filp,1724const char __user *user_buf,1725size_t count, loff_t *ppos)1726{1727struct pci_controller *hose;1728uint32_t phbid, pe_no;1729struct eeh_pe *pe;1730char buf[20];1731int ret;17321733ret = simple_write_to_buffer(buf, sizeof(buf), ppos, user_buf, count);1734if (!ret)1735return -EFAULT;17361737/*1738* When PE is NULL the event is a "special" event. Rather than1739* recovering a specific PE it forces the EEH core to scan for failed1740* PHBs and recovers each. This needs to be done before any device1741* recoveries can occur.1742*/1743if (!strncmp(buf, "hwcheck", 7)) {1744__eeh_send_failure_event(NULL);1745return count;1746}17471748ret = sscanf(buf, "%x:%x", &phbid, &pe_no);1749if (ret != 2)1750return -EINVAL;17511752hose = pci_find_controller_for_domain(phbid);1753if (!hose)1754return -ENODEV;17551756/* Retrieve PE */1757pe = eeh_pe_get(hose, pe_no);1758if (!pe)1759return -ENODEV;17601761/*1762* We don't do any state checking here since the detection1763* process is async to the recovery process. The recovery1764* thread *should* not break even if we schedule a recovery1765* from an odd state (e.g. PE removed, or recovery of a1766* non-isolated PE)1767*/1768__eeh_send_failure_event(pe);17691770return ret < 0 ? ret : count;1771}17721773static const struct file_operations eeh_force_recover_fops = {1774.open = simple_open,1775.write = eeh_force_recover_write,1776};17771778static ssize_t eeh_debugfs_dev_usage(struct file *filp,1779char __user *user_buf,1780size_t count, loff_t *ppos)1781{1782static const char usage[] = "input format: <domain>:<bus>:<dev>.<fn>\n";17831784return simple_read_from_buffer(user_buf, count, ppos,1785usage, sizeof(usage) - 1);1786}17871788static ssize_t eeh_dev_check_write(struct file *filp,1789const char __user *user_buf,1790size_t count, loff_t *ppos)1791{1792struct pci_dev *pdev;1793struct eeh_dev *edev;1794int ret;17951796pdev = eeh_debug_lookup_pdev(filp, user_buf, count, ppos);1797if (IS_ERR(pdev))1798return PTR_ERR(pdev);17991800edev = pci_dev_to_eeh_dev(pdev);1801if (!edev) {1802pci_err(pdev, "No eeh_dev for this device!\n");1803pci_dev_put(pdev);1804return -ENODEV;1805}18061807ret = eeh_dev_check_failure(edev);1808pci_info(pdev, "eeh_dev_check_failure(%s) = %d\n",1809pci_name(pdev), ret);18101811pci_dev_put(pdev);18121813return count;1814}18151816static const struct file_operations eeh_dev_check_fops = {1817.open = simple_open,1818.write = eeh_dev_check_write,1819.read = eeh_debugfs_dev_usage,1820};18211822static ssize_t eeh_dev_break_write(struct file *filp,1823const char __user *user_buf,1824size_t count, loff_t *ppos)1825{1826struct pci_dev *pdev;1827int ret;18281829pdev = eeh_debug_lookup_pdev(filp, user_buf, count, ppos);1830if (IS_ERR(pdev))1831return PTR_ERR(pdev);18321833ret = eeh_break_device(pdev);1834pci_dev_put(pdev);18351836if (ret < 0)1837return ret;18381839return count;1840}18411842static const struct file_operations eeh_dev_break_fops = {1843.open = simple_open,1844.write = eeh_dev_break_write,1845.read = eeh_debugfs_dev_usage,1846};18471848static ssize_t eeh_dev_can_recover(struct file *filp,1849const char __user *user_buf,1850size_t count, loff_t *ppos)1851{1852struct pci_driver *drv;1853struct pci_dev *pdev;1854size_t ret;18551856pdev = eeh_debug_lookup_pdev(filp, user_buf, count, ppos);1857if (IS_ERR(pdev))1858return PTR_ERR(pdev);18591860/*1861* In order for error recovery to work the driver needs to implement1862* .error_detected(), so it can quiesce IO to the device, and1863* .slot_reset() so it can re-initialise the device after a reset.1864*1865* Ideally they'd implement .resume() too, but some drivers which1866* we need to support (notably IPR) don't so I guess we can tolerate1867* that.1868*1869* .mmio_enabled() is mostly there as a work-around for devices which1870* take forever to re-init after a hot reset. Implementing that is1871* strictly optional.1872*/1873drv = pci_dev_driver(pdev);1874if (drv &&1875drv->err_handler &&1876drv->err_handler->error_detected &&1877drv->err_handler->slot_reset) {1878ret = count;1879} else {1880ret = -EOPNOTSUPP;1881}18821883pci_dev_put(pdev);18841885return ret;1886}18871888static const struct file_operations eeh_dev_can_recover_fops = {1889.open = simple_open,1890.write = eeh_dev_can_recover,1891.read = eeh_debugfs_dev_usage,1892};18931894#endif18951896static int __init eeh_init_proc(void)1897{1898if (machine_is(pseries) || machine_is(powernv)) {1899proc_create_single("powerpc/eeh", 0, NULL, proc_eeh_show);1900#ifdef CONFIG_DEBUG_FS1901debugfs_create_file_unsafe("eeh_enable", 0600,1902arch_debugfs_dir, NULL,1903&eeh_enable_dbgfs_ops);1904debugfs_create_u32("eeh_max_freezes", 0600,1905arch_debugfs_dir, &eeh_max_freezes);1906debugfs_create_bool("eeh_disable_recovery", 0600,1907arch_debugfs_dir,1908&eeh_debugfs_no_recover);1909debugfs_create_file_unsafe("eeh_dev_check", 0600,1910arch_debugfs_dir, NULL,1911&eeh_dev_check_fops);1912debugfs_create_file_unsafe("eeh_dev_break", 0600,1913arch_debugfs_dir, NULL,1914&eeh_dev_break_fops);1915debugfs_create_file_unsafe("eeh_force_recover", 0600,1916arch_debugfs_dir, NULL,1917&eeh_force_recover_fops);1918debugfs_create_file_unsafe("eeh_dev_can_recover", 0600,1919arch_debugfs_dir, NULL,1920&eeh_dev_can_recover_fops);1921eeh_cache_debugfs_init();1922#endif1923}19241925return 0;1926}1927__initcall(eeh_init_proc);192819291930