Path: blob/master/arch/powerpc/platforms/pseries/eeh.c
10818 views
/*1* eeh.c2* Copyright IBM Corporation 2001, 2005, 20063* Copyright Dave Engebretsen & Todd Inglett 20014* Copyright Linas Vepstas 2005, 20065*6* This program is free software; you can redistribute it and/or modify7* it under the terms of the GNU General Public License as published by8* the Free Software Foundation; either version 2 of the License, or9* (at your option) any later version.10*11* This program is distributed in the hope that it will be useful,12* but WITHOUT ANY WARRANTY; without even the implied warranty of13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14* GNU General Public License for more details.15*16* You should have received a copy of the GNU General Public License17* along with this program; if not, write to the Free Software18* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA19*20* Please address comments and feedback to Linas Vepstas <[email protected]>21*/2223#include <linux/delay.h>24#include <linux/init.h>25#include <linux/list.h>26#include <linux/pci.h>27#include <linux/proc_fs.h>28#include <linux/rbtree.h>29#include <linux/seq_file.h>30#include <linux/spinlock.h>31#include <linux/of.h>3233#include <asm/atomic.h>34#include <asm/eeh.h>35#include <asm/eeh_event.h>36#include <asm/io.h>37#include <asm/machdep.h>38#include <asm/ppc-pci.h>39#include <asm/rtas.h>404142/** Overview:43* EEH, or "Extended Error Handling" is a PCI bridge technology for44* dealing with PCI bus errors that can't be dealt with within the45* usual PCI framework, except by check-stopping the CPU. Systems46* that are designed for high-availability/reliability cannot afford47* to crash due to a "mere" PCI error, thus the need for EEH.48* An EEH-capable bridge operates by converting a detected error49* into a "slot freeze", taking the PCI adapter off-line, making50* the slot behave, from the OS'es point of view, as if the slot51* were "empty": all reads return 0xff's and all writes are silently52* ignored. EEH slot isolation events can be triggered by parity53* errors on the address or data busses (e.g. during posted writes),54* which in turn might be caused by low voltage on the bus, dust,55* vibration, humidity, radioactivity or plain-old failed hardware.56*57* Note, however, that one of the leading causes of EEH slot58* freeze events are buggy device drivers, buggy device microcode,59* or buggy device hardware. This is because any attempt by the60* device to bus-master data to a memory address that is not61* assigned to the device will trigger a slot freeze. (The idea62* is to prevent devices-gone-wild from corrupting system memory).63* Buggy hardware/drivers will have a miserable time co-existing64* with EEH.65*66* Ideally, a PCI device driver, when suspecting that an isolation67* event has occurred (e.g. by reading 0xff's), will then ask EEH68* whether this is the case, and then take appropriate steps to69* reset the PCI slot, the PCI device, and then resume operations.70* However, until that day, the checking is done here, with the71* eeh_check_failure() routine embedded in the MMIO macros. If72* the slot is found to be isolated, an "EEH Event" is synthesized73* and sent out for processing.74*/7576/* If a device driver keeps reading an MMIO register in an interrupt77* handler after a slot isolation event, it might be broken.78* This sets the threshold for how many read attempts we allow79* before printing an error message.80*/81#define EEH_MAX_FAILS 21000008283/* Time to wait for a PCI slot to report status, in milliseconds */84#define PCI_BUS_RESET_WAIT_MSEC (60*1000)8586/* RTAS tokens */87static int ibm_set_eeh_option;88static int ibm_set_slot_reset;89static int ibm_read_slot_reset_state;90static int ibm_read_slot_reset_state2;91static int ibm_slot_error_detail;92static int ibm_get_config_addr_info;93static int ibm_get_config_addr_info2;94static int ibm_configure_bridge;95static int ibm_configure_pe;9697int eeh_subsystem_enabled;98EXPORT_SYMBOL(eeh_subsystem_enabled);99100/* Lock to avoid races due to multiple reports of an error */101static DEFINE_RAW_SPINLOCK(confirm_error_lock);102103/* Buffer for reporting slot-error-detail rtas calls. Its here104* in BSS, and not dynamically alloced, so that it ends up in105* RMO where RTAS can access it.106*/107static unsigned char slot_errbuf[RTAS_ERROR_LOG_MAX];108static DEFINE_SPINLOCK(slot_errbuf_lock);109static int eeh_error_buf_size;110111/* Buffer for reporting pci register dumps. Its here in BSS, and112* not dynamically alloced, so that it ends up in RMO where RTAS113* can access it.114*/115#define EEH_PCI_REGS_LOG_LEN 4096116static unsigned char pci_regs_buf[EEH_PCI_REGS_LOG_LEN];117118/* System monitoring statistics */119static unsigned long no_device;120static unsigned long no_dn;121static unsigned long no_cfg_addr;122static unsigned long ignored_check;123static unsigned long total_mmio_ffs;124static unsigned long false_positives;125static unsigned long slot_resets;126127#define IS_BRIDGE(class_code) (((class_code)<<16) == PCI_BASE_CLASS_BRIDGE)128129/* --------------------------------------------------------------- */130/* Below lies the EEH event infrastructure */131132static void rtas_slot_error_detail(struct pci_dn *pdn, int severity,133char *driver_log, size_t loglen)134{135int config_addr;136unsigned long flags;137int rc;138139/* Log the error with the rtas logger */140spin_lock_irqsave(&slot_errbuf_lock, flags);141memset(slot_errbuf, 0, eeh_error_buf_size);142143/* Use PE configuration address, if present */144config_addr = pdn->eeh_config_addr;145if (pdn->eeh_pe_config_addr)146config_addr = pdn->eeh_pe_config_addr;147148rc = rtas_call(ibm_slot_error_detail,1498, 1, NULL, config_addr,150BUID_HI(pdn->phb->buid),151BUID_LO(pdn->phb->buid),152virt_to_phys(driver_log), loglen,153virt_to_phys(slot_errbuf),154eeh_error_buf_size,155severity);156157if (rc == 0)158log_error(slot_errbuf, ERR_TYPE_RTAS_LOG, 0);159spin_unlock_irqrestore(&slot_errbuf_lock, flags);160}161162/**163* gather_pci_data - copy assorted PCI config space registers to buff164* @pdn: device to report data for165* @buf: point to buffer in which to log166* @len: amount of room in buffer167*168* This routine captures assorted PCI configuration space data,169* and puts them into a buffer for RTAS error logging.170*/171static size_t gather_pci_data(struct pci_dn *pdn, char * buf, size_t len)172{173struct pci_dev *dev = pdn->pcidev;174u32 cfg;175int cap, i;176int n = 0;177178n += scnprintf(buf+n, len-n, "%s\n", pdn->node->full_name);179printk(KERN_WARNING "EEH: of node=%s\n", pdn->node->full_name);180181rtas_read_config(pdn, PCI_VENDOR_ID, 4, &cfg);182n += scnprintf(buf+n, len-n, "dev/vend:%08x\n", cfg);183printk(KERN_WARNING "EEH: PCI device/vendor: %08x\n", cfg);184185rtas_read_config(pdn, PCI_COMMAND, 4, &cfg);186n += scnprintf(buf+n, len-n, "cmd/stat:%x\n", cfg);187printk(KERN_WARNING "EEH: PCI cmd/status register: %08x\n", cfg);188189if (!dev) {190printk(KERN_WARNING "EEH: no PCI device for this of node\n");191return n;192}193194/* Gather bridge-specific registers */195if (dev->class >> 16 == PCI_BASE_CLASS_BRIDGE) {196rtas_read_config(pdn, PCI_SEC_STATUS, 2, &cfg);197n += scnprintf(buf+n, len-n, "sec stat:%x\n", cfg);198printk(KERN_WARNING "EEH: Bridge secondary status: %04x\n", cfg);199200rtas_read_config(pdn, PCI_BRIDGE_CONTROL, 2, &cfg);201n += scnprintf(buf+n, len-n, "brdg ctl:%x\n", cfg);202printk(KERN_WARNING "EEH: Bridge control: %04x\n", cfg);203}204205/* Dump out the PCI-X command and status regs */206cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);207if (cap) {208rtas_read_config(pdn, cap, 4, &cfg);209n += scnprintf(buf+n, len-n, "pcix-cmd:%x\n", cfg);210printk(KERN_WARNING "EEH: PCI-X cmd: %08x\n", cfg);211212rtas_read_config(pdn, cap+4, 4, &cfg);213n += scnprintf(buf+n, len-n, "pcix-stat:%x\n", cfg);214printk(KERN_WARNING "EEH: PCI-X status: %08x\n", cfg);215}216217/* If PCI-E capable, dump PCI-E cap 10, and the AER */218cap = pci_find_capability(dev, PCI_CAP_ID_EXP);219if (cap) {220n += scnprintf(buf+n, len-n, "pci-e cap10:\n");221printk(KERN_WARNING222"EEH: PCI-E capabilities and status follow:\n");223224for (i=0; i<=8; i++) {225rtas_read_config(pdn, cap+4*i, 4, &cfg);226n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);227printk(KERN_WARNING "EEH: PCI-E %02x: %08x\n", i, cfg);228}229230cap = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);231if (cap) {232n += scnprintf(buf+n, len-n, "pci-e AER:\n");233printk(KERN_WARNING234"EEH: PCI-E AER capability register set follows:\n");235236for (i=0; i<14; i++) {237rtas_read_config(pdn, cap+4*i, 4, &cfg);238n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);239printk(KERN_WARNING "EEH: PCI-E AER %02x: %08x\n", i, cfg);240}241}242}243244/* Gather status on devices under the bridge */245if (dev->class >> 16 == PCI_BASE_CLASS_BRIDGE) {246struct device_node *dn;247248for_each_child_of_node(pdn->node, dn) {249pdn = PCI_DN(dn);250if (pdn)251n += gather_pci_data(pdn, buf+n, len-n);252}253}254255return n;256}257258void eeh_slot_error_detail(struct pci_dn *pdn, int severity)259{260size_t loglen = 0;261pci_regs_buf[0] = 0;262263rtas_pci_enable(pdn, EEH_THAW_MMIO);264rtas_configure_bridge(pdn);265eeh_restore_bars(pdn);266loglen = gather_pci_data(pdn, pci_regs_buf, EEH_PCI_REGS_LOG_LEN);267268rtas_slot_error_detail(pdn, severity, pci_regs_buf, loglen);269}270271/**272* read_slot_reset_state - Read the reset state of a device node's slot273* @dn: device node to read274* @rets: array to return results in275*/276static int read_slot_reset_state(struct pci_dn *pdn, int rets[])277{278int token, outputs;279int config_addr;280281if (ibm_read_slot_reset_state2 != RTAS_UNKNOWN_SERVICE) {282token = ibm_read_slot_reset_state2;283outputs = 4;284} else {285token = ibm_read_slot_reset_state;286rets[2] = 0; /* fake PE Unavailable info */287outputs = 3;288}289290/* Use PE configuration address, if present */291config_addr = pdn->eeh_config_addr;292if (pdn->eeh_pe_config_addr)293config_addr = pdn->eeh_pe_config_addr;294295return rtas_call(token, 3, outputs, rets, config_addr,296BUID_HI(pdn->phb->buid), BUID_LO(pdn->phb->buid));297}298299/**300* eeh_wait_for_slot_status - returns error status of slot301* @pdn pci device node302* @max_wait_msecs maximum number to millisecs to wait303*304* Return negative value if a permanent error, else return305* Partition Endpoint (PE) status value.306*307* If @max_wait_msecs is positive, then this routine will308* sleep until a valid status can be obtained, or until309* the max allowed wait time is exceeded, in which case310* a -2 is returned.311*/312int313eeh_wait_for_slot_status(struct pci_dn *pdn, int max_wait_msecs)314{315int rc;316int rets[3];317int mwait;318319while (1) {320rc = read_slot_reset_state(pdn, rets);321if (rc) return rc;322if (rets[1] == 0) return -1; /* EEH is not supported */323324if (rets[0] != 5) return rets[0]; /* return actual status */325326if (rets[2] == 0) return -1; /* permanently unavailable */327328if (max_wait_msecs <= 0) break;329330mwait = rets[2];331if (mwait <= 0) {332printk (KERN_WARNING333"EEH: Firmware returned bad wait value=%d\n", mwait);334mwait = 1000;335} else if (mwait > 300*1000) {336printk (KERN_WARNING337"EEH: Firmware is taking too long, time=%d\n", mwait);338mwait = 300*1000;339}340max_wait_msecs -= mwait;341msleep (mwait);342}343344printk(KERN_WARNING "EEH: Timed out waiting for slot status\n");345return -2;346}347348/**349* eeh_token_to_phys - convert EEH address token to phys address350* @token i/o token, should be address in the form 0xA....351*/352static inline unsigned long eeh_token_to_phys(unsigned long token)353{354pte_t *ptep;355unsigned long pa;356357ptep = find_linux_pte(init_mm.pgd, token);358if (!ptep)359return token;360pa = pte_pfn(*ptep) << PAGE_SHIFT;361362return pa | (token & (PAGE_SIZE-1));363}364365/**366* Return the "partitionable endpoint" (pe) under which this device lies367*/368struct device_node * find_device_pe(struct device_node *dn)369{370while ((dn->parent) && PCI_DN(dn->parent) &&371(PCI_DN(dn->parent)->eeh_mode & EEH_MODE_SUPPORTED)) {372dn = dn->parent;373}374return dn;375}376377/** Mark all devices that are children of this device as failed.378* Mark the device driver too, so that it can see the failure379* immediately; this is critical, since some drivers poll380* status registers in interrupts ... If a driver is polling,381* and the slot is frozen, then the driver can deadlock in382* an interrupt context, which is bad.383*/384385static void __eeh_mark_slot(struct device_node *parent, int mode_flag)386{387struct device_node *dn;388389for_each_child_of_node(parent, dn) {390if (PCI_DN(dn)) {391/* Mark the pci device driver too */392struct pci_dev *dev = PCI_DN(dn)->pcidev;393394PCI_DN(dn)->eeh_mode |= mode_flag;395396if (dev && dev->driver)397dev->error_state = pci_channel_io_frozen;398399__eeh_mark_slot(dn, mode_flag);400}401}402}403404void eeh_mark_slot (struct device_node *dn, int mode_flag)405{406struct pci_dev *dev;407dn = find_device_pe (dn);408409/* Back up one, since config addrs might be shared */410if (!pcibios_find_pci_bus(dn) && PCI_DN(dn->parent))411dn = dn->parent;412413PCI_DN(dn)->eeh_mode |= mode_flag;414415/* Mark the pci device too */416dev = PCI_DN(dn)->pcidev;417if (dev)418dev->error_state = pci_channel_io_frozen;419420__eeh_mark_slot(dn, mode_flag);421}422423static void __eeh_clear_slot(struct device_node *parent, int mode_flag)424{425struct device_node *dn;426427for_each_child_of_node(parent, dn) {428if (PCI_DN(dn)) {429PCI_DN(dn)->eeh_mode &= ~mode_flag;430PCI_DN(dn)->eeh_check_count = 0;431__eeh_clear_slot(dn, mode_flag);432}433}434}435436void eeh_clear_slot (struct device_node *dn, int mode_flag)437{438unsigned long flags;439raw_spin_lock_irqsave(&confirm_error_lock, flags);440441dn = find_device_pe (dn);442443/* Back up one, since config addrs might be shared */444if (!pcibios_find_pci_bus(dn) && PCI_DN(dn->parent))445dn = dn->parent;446447PCI_DN(dn)->eeh_mode &= ~mode_flag;448PCI_DN(dn)->eeh_check_count = 0;449__eeh_clear_slot(dn, mode_flag);450raw_spin_unlock_irqrestore(&confirm_error_lock, flags);451}452453void __eeh_set_pe_freset(struct device_node *parent, unsigned int *freset)454{455struct device_node *dn;456457for_each_child_of_node(parent, dn) {458if (PCI_DN(dn)) {459460struct pci_dev *dev = PCI_DN(dn)->pcidev;461462if (dev && dev->driver)463*freset |= dev->needs_freset;464465__eeh_set_pe_freset(dn, freset);466}467}468}469470void eeh_set_pe_freset(struct device_node *dn, unsigned int *freset)471{472struct pci_dev *dev;473dn = find_device_pe(dn);474475/* Back up one, since config addrs might be shared */476if (!pcibios_find_pci_bus(dn) && PCI_DN(dn->parent))477dn = dn->parent;478479dev = PCI_DN(dn)->pcidev;480if (dev)481*freset |= dev->needs_freset;482483__eeh_set_pe_freset(dn, freset);484}485486/**487* eeh_dn_check_failure - check if all 1's data is due to EEH slot freeze488* @dn device node489* @dev pci device, if known490*491* Check for an EEH failure for the given device node. Call this492* routine if the result of a read was all 0xff's and you want to493* find out if this is due to an EEH slot freeze. This routine494* will query firmware for the EEH status.495*496* Returns 0 if there has not been an EEH error; otherwise returns497* a non-zero value and queues up a slot isolation event notification.498*499* It is safe to call this routine in an interrupt context.500*/501int eeh_dn_check_failure(struct device_node *dn, struct pci_dev *dev)502{503int ret;504int rets[3];505unsigned long flags;506struct pci_dn *pdn;507int rc = 0;508const char *location;509510total_mmio_ffs++;511512if (!eeh_subsystem_enabled)513return 0;514515if (!dn) {516no_dn++;517return 0;518}519dn = find_device_pe(dn);520pdn = PCI_DN(dn);521522/* Access to IO BARs might get this far and still not want checking. */523if (!(pdn->eeh_mode & EEH_MODE_SUPPORTED) ||524pdn->eeh_mode & EEH_MODE_NOCHECK) {525ignored_check++;526pr_debug("EEH: Ignored check (%x) for %s %s\n",527pdn->eeh_mode, eeh_pci_name(dev), dn->full_name);528return 0;529}530531if (!pdn->eeh_config_addr && !pdn->eeh_pe_config_addr) {532no_cfg_addr++;533return 0;534}535536/* If we already have a pending isolation event for this537* slot, we know it's bad already, we don't need to check.538* Do this checking under a lock; as multiple PCI devices539* in one slot might report errors simultaneously, and we540* only want one error recovery routine running.541*/542raw_spin_lock_irqsave(&confirm_error_lock, flags);543rc = 1;544if (pdn->eeh_mode & EEH_MODE_ISOLATED) {545pdn->eeh_check_count ++;546if (pdn->eeh_check_count % EEH_MAX_FAILS == 0) {547location = of_get_property(dn, "ibm,loc-code", NULL);548printk (KERN_ERR "EEH: %d reads ignored for recovering device at "549"location=%s driver=%s pci addr=%s\n",550pdn->eeh_check_count, location,551dev->driver->name, eeh_pci_name(dev));552printk (KERN_ERR "EEH: Might be infinite loop in %s driver\n",553dev->driver->name);554dump_stack();555}556goto dn_unlock;557}558559/*560* Now test for an EEH failure. This is VERY expensive.561* Note that the eeh_config_addr may be a parent device562* in the case of a device behind a bridge, or it may be563* function zero of a multi-function device.564* In any case they must share a common PHB.565*/566ret = read_slot_reset_state(pdn, rets);567568/* If the call to firmware failed, punt */569if (ret != 0) {570printk(KERN_WARNING "EEH: read_slot_reset_state() failed; rc=%d dn=%s\n",571ret, dn->full_name);572false_positives++;573pdn->eeh_false_positives ++;574rc = 0;575goto dn_unlock;576}577578/* Note that config-io to empty slots may fail;579* they are empty when they don't have children. */580if ((rets[0] == 5) && (rets[2] == 0) && (dn->child == NULL)) {581false_positives++;582pdn->eeh_false_positives ++;583rc = 0;584goto dn_unlock;585}586587/* If EEH is not supported on this device, punt. */588if (rets[1] != 1) {589printk(KERN_WARNING "EEH: event on unsupported device, rc=%d dn=%s\n",590ret, dn->full_name);591false_positives++;592pdn->eeh_false_positives ++;593rc = 0;594goto dn_unlock;595}596597/* If not the kind of error we know about, punt. */598if (rets[0] != 1 && rets[0] != 2 && rets[0] != 4 && rets[0] != 5) {599false_positives++;600pdn->eeh_false_positives ++;601rc = 0;602goto dn_unlock;603}604605slot_resets++;606607/* Avoid repeated reports of this failure, including problems608* with other functions on this device, and functions under609* bridges. */610eeh_mark_slot (dn, EEH_MODE_ISOLATED);611raw_spin_unlock_irqrestore(&confirm_error_lock, flags);612613eeh_send_failure_event (dn, dev);614615/* Most EEH events are due to device driver bugs. Having616* a stack trace will help the device-driver authors figure617* out what happened. So print that out. */618dump_stack();619return 1;620621dn_unlock:622raw_spin_unlock_irqrestore(&confirm_error_lock, flags);623return rc;624}625626EXPORT_SYMBOL_GPL(eeh_dn_check_failure);627628/**629* eeh_check_failure - check if all 1's data is due to EEH slot freeze630* @token i/o token, should be address in the form 0xA....631* @val value, should be all 1's (XXX why do we need this arg??)632*633* Check for an EEH failure at the given token address. Call this634* routine if the result of a read was all 0xff's and you want to635* find out if this is due to an EEH slot freeze event. This routine636* will query firmware for the EEH status.637*638* Note this routine is safe to call in an interrupt context.639*/640unsigned long eeh_check_failure(const volatile void __iomem *token, unsigned long val)641{642unsigned long addr;643struct pci_dev *dev;644struct device_node *dn;645646/* Finding the phys addr + pci device; this is pretty quick. */647addr = eeh_token_to_phys((unsigned long __force) token);648dev = pci_get_device_by_addr(addr);649if (!dev) {650no_device++;651return val;652}653654dn = pci_device_to_OF_node(dev);655eeh_dn_check_failure (dn, dev);656657pci_dev_put(dev);658return val;659}660661EXPORT_SYMBOL(eeh_check_failure);662663/* ------------------------------------------------------------- */664/* The code below deals with error recovery */665666/**667* rtas_pci_enable - enable MMIO or DMA transfers for this slot668* @pdn pci device node669*/670671int672rtas_pci_enable(struct pci_dn *pdn, int function)673{674int config_addr;675int rc;676677/* Use PE configuration address, if present */678config_addr = pdn->eeh_config_addr;679if (pdn->eeh_pe_config_addr)680config_addr = pdn->eeh_pe_config_addr;681682rc = rtas_call(ibm_set_eeh_option, 4, 1, NULL,683config_addr,684BUID_HI(pdn->phb->buid),685BUID_LO(pdn->phb->buid),686function);687688if (rc)689printk(KERN_WARNING "EEH: Unexpected state change %d, err=%d dn=%s\n",690function, rc, pdn->node->full_name);691692rc = eeh_wait_for_slot_status (pdn, PCI_BUS_RESET_WAIT_MSEC);693if ((rc == 4) && (function == EEH_THAW_MMIO))694return 0;695696return rc;697}698699/**700* rtas_pci_slot_reset - raises/lowers the pci #RST line701* @pdn pci device node702* @state: 1/0 to raise/lower the #RST703*704* Clear the EEH-frozen condition on a slot. This routine705* asserts the PCI #RST line if the 'state' argument is '1',706* and drops the #RST line if 'state is '0'. This routine is707* safe to call in an interrupt context.708*709*/710711static void712rtas_pci_slot_reset(struct pci_dn *pdn, int state)713{714int config_addr;715int rc;716717BUG_ON (pdn==NULL);718719if (!pdn->phb) {720printk (KERN_WARNING "EEH: in slot reset, device node %s has no phb\n",721pdn->node->full_name);722return;723}724725/* Use PE configuration address, if present */726config_addr = pdn->eeh_config_addr;727if (pdn->eeh_pe_config_addr)728config_addr = pdn->eeh_pe_config_addr;729730rc = rtas_call(ibm_set_slot_reset, 4, 1, NULL,731config_addr,732BUID_HI(pdn->phb->buid),733BUID_LO(pdn->phb->buid),734state);735736/* Fundamental-reset not supported on this PE, try hot-reset */737if (rc == -8 && state == 3) {738rc = rtas_call(ibm_set_slot_reset, 4, 1, NULL,739config_addr,740BUID_HI(pdn->phb->buid),741BUID_LO(pdn->phb->buid), 1);742if (rc)743printk(KERN_WARNING744"EEH: Unable to reset the failed slot,"745" #RST=%d dn=%s\n",746rc, pdn->node->full_name);747}748}749750/**751* pcibios_set_pcie_slot_reset - Set PCI-E reset state752* @dev: pci device struct753* @state: reset state to enter754*755* Return value:756* 0 if success757**/758int pcibios_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)759{760struct device_node *dn = pci_device_to_OF_node(dev);761struct pci_dn *pdn = PCI_DN(dn);762763switch (state) {764case pcie_deassert_reset:765rtas_pci_slot_reset(pdn, 0);766break;767case pcie_hot_reset:768rtas_pci_slot_reset(pdn, 1);769break;770case pcie_warm_reset:771rtas_pci_slot_reset(pdn, 3);772break;773default:774return -EINVAL;775};776777return 0;778}779780/**781* rtas_set_slot_reset -- assert the pci #RST line for 1/4 second782* @pdn: pci device node to be reset.783*/784785static void __rtas_set_slot_reset(struct pci_dn *pdn)786{787unsigned int freset = 0;788789/* Determine type of EEH reset required for790* Partitionable Endpoint, a hot-reset (1)791* or a fundamental reset (3).792* A fundamental reset required by any device under793* Partitionable Endpoint trumps hot-reset.794*/795eeh_set_pe_freset(pdn->node, &freset);796797if (freset)798rtas_pci_slot_reset(pdn, 3);799else800rtas_pci_slot_reset(pdn, 1);801802/* The PCI bus requires that the reset be held high for at least803* a 100 milliseconds. We wait a bit longer 'just in case'. */804805#define PCI_BUS_RST_HOLD_TIME_MSEC 250806msleep (PCI_BUS_RST_HOLD_TIME_MSEC);807808/* We might get hit with another EEH freeze as soon as the809* pci slot reset line is dropped. Make sure we don't miss810* these, and clear the flag now. */811eeh_clear_slot (pdn->node, EEH_MODE_ISOLATED);812813rtas_pci_slot_reset (pdn, 0);814815/* After a PCI slot has been reset, the PCI Express spec requires816* a 1.5 second idle time for the bus to stabilize, before starting817* up traffic. */818#define PCI_BUS_SETTLE_TIME_MSEC 1800819msleep (PCI_BUS_SETTLE_TIME_MSEC);820}821822int rtas_set_slot_reset(struct pci_dn *pdn)823{824int i, rc;825826/* Take three shots at resetting the bus */827for (i=0; i<3; i++) {828__rtas_set_slot_reset(pdn);829830rc = eeh_wait_for_slot_status(pdn, PCI_BUS_RESET_WAIT_MSEC);831if (rc == 0)832return 0;833834if (rc < 0) {835printk(KERN_ERR "EEH: unrecoverable slot failure %s\n",836pdn->node->full_name);837return -1;838}839printk(KERN_ERR "EEH: bus reset %d failed on slot %s, rc=%d\n",840i+1, pdn->node->full_name, rc);841}842843return -1;844}845846/* ------------------------------------------------------- */847/** Save and restore of PCI BARs848*849* Although firmware will set up BARs during boot, it doesn't850* set up device BAR's after a device reset, although it will,851* if requested, set up bridge configuration. Thus, we need to852* configure the PCI devices ourselves.853*/854855/**856* __restore_bars - Restore the Base Address Registers857* @pdn: pci device node858*859* Loads the PCI configuration space base address registers,860* the expansion ROM base address, the latency timer, and etc.861* from the saved values in the device node.862*/863static inline void __restore_bars (struct pci_dn *pdn)864{865int i;866u32 cmd;867868if (NULL==pdn->phb) return;869for (i=4; i<10; i++) {870rtas_write_config(pdn, i*4, 4, pdn->config_space[i]);871}872873/* 12 == Expansion ROM Address */874rtas_write_config(pdn, 12*4, 4, pdn->config_space[12]);875876#define BYTE_SWAP(OFF) (8*((OFF)/4)+3-(OFF))877#define SAVED_BYTE(OFF) (((u8 *)(pdn->config_space))[BYTE_SWAP(OFF)])878879rtas_write_config (pdn, PCI_CACHE_LINE_SIZE, 1,880SAVED_BYTE(PCI_CACHE_LINE_SIZE));881882rtas_write_config (pdn, PCI_LATENCY_TIMER, 1,883SAVED_BYTE(PCI_LATENCY_TIMER));884885/* max latency, min grant, interrupt pin and line */886rtas_write_config(pdn, 15*4, 4, pdn->config_space[15]);887888/* Restore PERR & SERR bits, some devices require it,889don't touch the other command bits */890rtas_read_config(pdn, PCI_COMMAND, 4, &cmd);891if (pdn->config_space[1] & PCI_COMMAND_PARITY)892cmd |= PCI_COMMAND_PARITY;893else894cmd &= ~PCI_COMMAND_PARITY;895if (pdn->config_space[1] & PCI_COMMAND_SERR)896cmd |= PCI_COMMAND_SERR;897else898cmd &= ~PCI_COMMAND_SERR;899rtas_write_config(pdn, PCI_COMMAND, 4, cmd);900}901902/**903* eeh_restore_bars - restore the PCI config space info904*905* This routine performs a recursive walk to the children906* of this device as well.907*/908void eeh_restore_bars(struct pci_dn *pdn)909{910struct device_node *dn;911if (!pdn)912return;913914if ((pdn->eeh_mode & EEH_MODE_SUPPORTED) && !IS_BRIDGE(pdn->class_code))915__restore_bars (pdn);916917for_each_child_of_node(pdn->node, dn)918eeh_restore_bars (PCI_DN(dn));919}920921/**922* eeh_save_bars - save device bars923*924* Save the values of the device bars. Unlike the restore925* routine, this routine is *not* recursive. This is because926* PCI devices are added individually; but, for the restore,927* an entire slot is reset at a time.928*/929static void eeh_save_bars(struct pci_dn *pdn)930{931int i;932933if (!pdn )934return;935936for (i = 0; i < 16; i++)937rtas_read_config(pdn, i * 4, 4, &pdn->config_space[i]);938}939940void941rtas_configure_bridge(struct pci_dn *pdn)942{943int config_addr;944int rc;945int token;946947/* Use PE configuration address, if present */948config_addr = pdn->eeh_config_addr;949if (pdn->eeh_pe_config_addr)950config_addr = pdn->eeh_pe_config_addr;951952/* Use new configure-pe function, if supported */953if (ibm_configure_pe != RTAS_UNKNOWN_SERVICE)954token = ibm_configure_pe;955else956token = ibm_configure_bridge;957958rc = rtas_call(token, 3, 1, NULL,959config_addr,960BUID_HI(pdn->phb->buid),961BUID_LO(pdn->phb->buid));962if (rc) {963printk (KERN_WARNING "EEH: Unable to configure device bridge (%d) for %s\n",964rc, pdn->node->full_name);965}966}967968/* ------------------------------------------------------------- */969/* The code below deals with enabling EEH for devices during the970* early boot sequence. EEH must be enabled before any PCI probing971* can be done.972*/973974#define EEH_ENABLE 1975976struct eeh_early_enable_info {977unsigned int buid_hi;978unsigned int buid_lo;979};980981static int get_pe_addr (int config_addr,982struct eeh_early_enable_info *info)983{984unsigned int rets[3];985int ret;986987/* Use latest config-addr token on power6 */988if (ibm_get_config_addr_info2 != RTAS_UNKNOWN_SERVICE) {989/* Make sure we have a PE in hand */990ret = rtas_call (ibm_get_config_addr_info2, 4, 2, rets,991config_addr, info->buid_hi, info->buid_lo, 1);992if (ret || (rets[0]==0))993return 0;994995ret = rtas_call (ibm_get_config_addr_info2, 4, 2, rets,996config_addr, info->buid_hi, info->buid_lo, 0);997if (ret)998return 0;999return rets[0];1000}10011002/* Use older config-addr token on power5 */1003if (ibm_get_config_addr_info != RTAS_UNKNOWN_SERVICE) {1004ret = rtas_call (ibm_get_config_addr_info, 4, 2, rets,1005config_addr, info->buid_hi, info->buid_lo, 0);1006if (ret)1007return 0;1008return rets[0];1009}1010return 0;1011}10121013/* Enable eeh for the given device node. */1014static void *early_enable_eeh(struct device_node *dn, void *data)1015{1016unsigned int rets[3];1017struct eeh_early_enable_info *info = data;1018int ret;1019const u32 *class_code = of_get_property(dn, "class-code", NULL);1020const u32 *vendor_id = of_get_property(dn, "vendor-id", NULL);1021const u32 *device_id = of_get_property(dn, "device-id", NULL);1022const u32 *regs;1023int enable;1024struct pci_dn *pdn = PCI_DN(dn);10251026pdn->class_code = 0;1027pdn->eeh_mode = 0;1028pdn->eeh_check_count = 0;1029pdn->eeh_freeze_count = 0;1030pdn->eeh_false_positives = 0;10311032if (!of_device_is_available(dn))1033return NULL;10341035/* Ignore bad nodes. */1036if (!class_code || !vendor_id || !device_id)1037return NULL;10381039/* There is nothing to check on PCI to ISA bridges */1040if (dn->type && !strcmp(dn->type, "isa")) {1041pdn->eeh_mode |= EEH_MODE_NOCHECK;1042return NULL;1043}1044pdn->class_code = *class_code;10451046/* Ok... see if this device supports EEH. Some do, some don't,1047* and the only way to find out is to check each and every one. */1048regs = of_get_property(dn, "reg", NULL);1049if (regs) {1050/* First register entry is addr (00BBSS00) */1051/* Try to enable eeh */1052ret = rtas_call(ibm_set_eeh_option, 4, 1, NULL,1053regs[0], info->buid_hi, info->buid_lo,1054EEH_ENABLE);10551056enable = 0;1057if (ret == 0) {1058pdn->eeh_config_addr = regs[0];10591060/* If the newer, better, ibm,get-config-addr-info is supported,1061* then use that instead. */1062pdn->eeh_pe_config_addr = get_pe_addr(pdn->eeh_config_addr, info);10631064/* Some older systems (Power4) allow the1065* ibm,set-eeh-option call to succeed even on nodes1066* where EEH is not supported. Verify support1067* explicitly. */1068ret = read_slot_reset_state(pdn, rets);1069if ((ret == 0) && (rets[1] == 1))1070enable = 1;1071}10721073if (enable) {1074eeh_subsystem_enabled = 1;1075pdn->eeh_mode |= EEH_MODE_SUPPORTED;10761077pr_debug("EEH: %s: eeh enabled, config=%x pe_config=%x\n",1078dn->full_name, pdn->eeh_config_addr,1079pdn->eeh_pe_config_addr);1080} else {10811082/* This device doesn't support EEH, but it may have an1083* EEH parent, in which case we mark it as supported. */1084if (dn->parent && PCI_DN(dn->parent)1085&& (PCI_DN(dn->parent)->eeh_mode & EEH_MODE_SUPPORTED)) {1086/* Parent supports EEH. */1087pdn->eeh_mode |= EEH_MODE_SUPPORTED;1088pdn->eeh_config_addr = PCI_DN(dn->parent)->eeh_config_addr;1089return NULL;1090}1091}1092} else {1093printk(KERN_WARNING "EEH: %s: unable to get reg property.\n",1094dn->full_name);1095}10961097eeh_save_bars(pdn);1098return NULL;1099}11001101/*1102* Initialize EEH by trying to enable it for all of the adapters in the system.1103* As a side effect we can determine here if eeh is supported at all.1104* Note that we leave EEH on so failed config cycles won't cause a machine1105* check. If a user turns off EEH for a particular adapter they are really1106* telling Linux to ignore errors. Some hardware (e.g. POWER5) won't1107* grant access to a slot if EEH isn't enabled, and so we always enable1108* EEH for all slots/all devices.1109*1110* The eeh-force-off option disables EEH checking globally, for all slots.1111* Even if force-off is set, the EEH hardware is still enabled, so that1112* newer systems can boot.1113*/1114void __init eeh_init(void)1115{1116struct device_node *phb, *np;1117struct eeh_early_enable_info info;11181119raw_spin_lock_init(&confirm_error_lock);1120spin_lock_init(&slot_errbuf_lock);11211122np = of_find_node_by_path("/rtas");1123if (np == NULL)1124return;11251126ibm_set_eeh_option = rtas_token("ibm,set-eeh-option");1127ibm_set_slot_reset = rtas_token("ibm,set-slot-reset");1128ibm_read_slot_reset_state2 = rtas_token("ibm,read-slot-reset-state2");1129ibm_read_slot_reset_state = rtas_token("ibm,read-slot-reset-state");1130ibm_slot_error_detail = rtas_token("ibm,slot-error-detail");1131ibm_get_config_addr_info = rtas_token("ibm,get-config-addr-info");1132ibm_get_config_addr_info2 = rtas_token("ibm,get-config-addr-info2");1133ibm_configure_bridge = rtas_token ("ibm,configure-bridge");1134ibm_configure_pe = rtas_token("ibm,configure-pe");11351136if (ibm_set_eeh_option == RTAS_UNKNOWN_SERVICE)1137return;11381139eeh_error_buf_size = rtas_token("rtas-error-log-max");1140if (eeh_error_buf_size == RTAS_UNKNOWN_SERVICE) {1141eeh_error_buf_size = 1024;1142}1143if (eeh_error_buf_size > RTAS_ERROR_LOG_MAX) {1144printk(KERN_WARNING "EEH: rtas-error-log-max is bigger than allocated "1145"buffer ! (%d vs %d)", eeh_error_buf_size, RTAS_ERROR_LOG_MAX);1146eeh_error_buf_size = RTAS_ERROR_LOG_MAX;1147}11481149/* Enable EEH for all adapters. Note that eeh requires buid's */1150for (phb = of_find_node_by_name(NULL, "pci"); phb;1151phb = of_find_node_by_name(phb, "pci")) {1152unsigned long buid;11531154buid = get_phb_buid(phb);1155if (buid == 0 || PCI_DN(phb) == NULL)1156continue;11571158info.buid_lo = BUID_LO(buid);1159info.buid_hi = BUID_HI(buid);1160traverse_pci_devices(phb, early_enable_eeh, &info);1161}11621163if (eeh_subsystem_enabled)1164printk(KERN_INFO "EEH: PCI Enhanced I/O Error Handling Enabled\n");1165else1166printk(KERN_WARNING "EEH: No capable adapters found\n");1167}11681169/**1170* eeh_add_device_early - enable EEH for the indicated device_node1171* @dn: device node for which to set up EEH1172*1173* This routine must be used to perform EEH initialization for PCI1174* devices that were added after system boot (e.g. hotplug, dlpar).1175* This routine must be called before any i/o is performed to the1176* adapter (inluding any config-space i/o).1177* Whether this actually enables EEH or not for this device depends1178* on the CEC architecture, type of the device, on earlier boot1179* command-line arguments & etc.1180*/1181static void eeh_add_device_early(struct device_node *dn)1182{1183struct pci_controller *phb;1184struct eeh_early_enable_info info;11851186if (!dn || !PCI_DN(dn))1187return;1188phb = PCI_DN(dn)->phb;11891190/* USB Bus children of PCI devices will not have BUID's */1191if (NULL == phb || 0 == phb->buid)1192return;11931194info.buid_hi = BUID_HI(phb->buid);1195info.buid_lo = BUID_LO(phb->buid);1196early_enable_eeh(dn, &info);1197}11981199void eeh_add_device_tree_early(struct device_node *dn)1200{1201struct device_node *sib;12021203for_each_child_of_node(dn, sib)1204eeh_add_device_tree_early(sib);1205eeh_add_device_early(dn);1206}1207EXPORT_SYMBOL_GPL(eeh_add_device_tree_early);12081209/**1210* eeh_add_device_late - perform EEH initialization for the indicated pci device1211* @dev: pci device for which to set up EEH1212*1213* This routine must be used to complete EEH initialization for PCI1214* devices that were added after system boot (e.g. hotplug, dlpar).1215*/1216static void eeh_add_device_late(struct pci_dev *dev)1217{1218struct device_node *dn;1219struct pci_dn *pdn;12201221if (!dev || !eeh_subsystem_enabled)1222return;12231224pr_debug("EEH: Adding device %s\n", pci_name(dev));12251226dn = pci_device_to_OF_node(dev);1227pdn = PCI_DN(dn);1228if (pdn->pcidev == dev) {1229pr_debug("EEH: Already referenced !\n");1230return;1231}1232WARN_ON(pdn->pcidev);12331234pci_dev_get (dev);1235pdn->pcidev = dev;12361237pci_addr_cache_insert_device(dev);1238eeh_sysfs_add_device(dev);1239}12401241void eeh_add_device_tree_late(struct pci_bus *bus)1242{1243struct pci_dev *dev;12441245list_for_each_entry(dev, &bus->devices, bus_list) {1246eeh_add_device_late(dev);1247if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {1248struct pci_bus *subbus = dev->subordinate;1249if (subbus)1250eeh_add_device_tree_late(subbus);1251}1252}1253}1254EXPORT_SYMBOL_GPL(eeh_add_device_tree_late);12551256/**1257* eeh_remove_device - undo EEH setup for the indicated pci device1258* @dev: pci device to be removed1259*1260* This routine should be called when a device is removed from1261* a running system (e.g. by hotplug or dlpar). It unregisters1262* the PCI device from the EEH subsystem. I/O errors affecting1263* this device will no longer be detected after this call; thus,1264* i/o errors affecting this slot may leave this device unusable.1265*/1266static void eeh_remove_device(struct pci_dev *dev)1267{1268struct device_node *dn;1269if (!dev || !eeh_subsystem_enabled)1270return;12711272/* Unregister the device with the EEH/PCI address search system */1273pr_debug("EEH: Removing device %s\n", pci_name(dev));12741275dn = pci_device_to_OF_node(dev);1276if (PCI_DN(dn)->pcidev == NULL) {1277pr_debug("EEH: Not referenced !\n");1278return;1279}1280PCI_DN(dn)->pcidev = NULL;1281pci_dev_put (dev);12821283pci_addr_cache_remove_device(dev);1284eeh_sysfs_remove_device(dev);1285}12861287void eeh_remove_bus_device(struct pci_dev *dev)1288{1289struct pci_bus *bus = dev->subordinate;1290struct pci_dev *child, *tmp;12911292eeh_remove_device(dev);12931294if (bus && dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {1295list_for_each_entry_safe(child, tmp, &bus->devices, bus_list)1296eeh_remove_bus_device(child);1297}1298}1299EXPORT_SYMBOL_GPL(eeh_remove_bus_device);13001301static int proc_eeh_show(struct seq_file *m, void *v)1302{1303if (0 == eeh_subsystem_enabled) {1304seq_printf(m, "EEH Subsystem is globally disabled\n");1305seq_printf(m, "eeh_total_mmio_ffs=%ld\n", total_mmio_ffs);1306} else {1307seq_printf(m, "EEH Subsystem is enabled\n");1308seq_printf(m,1309"no device=%ld\n"1310"no device node=%ld\n"1311"no config address=%ld\n"1312"check not wanted=%ld\n"1313"eeh_total_mmio_ffs=%ld\n"1314"eeh_false_positives=%ld\n"1315"eeh_slot_resets=%ld\n",1316no_device, no_dn, no_cfg_addr,1317ignored_check, total_mmio_ffs,1318false_positives,1319slot_resets);1320}13211322return 0;1323}13241325static int proc_eeh_open(struct inode *inode, struct file *file)1326{1327return single_open(file, proc_eeh_show, NULL);1328}13291330static const struct file_operations proc_eeh_operations = {1331.open = proc_eeh_open,1332.read = seq_read,1333.llseek = seq_lseek,1334.release = single_release,1335};13361337static int __init eeh_init_proc(void)1338{1339if (machine_is(pseries))1340proc_create("ppc64/eeh", 0, NULL, &proc_eeh_operations);1341return 0;1342}1343__initcall(eeh_init_proc);134413451346