linux/arch/powerpc/platforms/pseries/eeh.c
<<
>>
Prefs
   1/*
   2 * Copyright IBM Corporation 2001, 2005, 2006
   3 * Copyright Dave Engebretsen & Todd Inglett 2001
   4 * Copyright Linas Vepstas 2005, 2006
   5 * Copyright 2001-2012 IBM Corporation.
   6 *
   7 * This program is free software; you can redistribute it and/or modify
   8 * it under the terms of the GNU General Public License as published by
   9 * the Free Software Foundation; either version 2 of the License, or
  10 * (at your option) any later version.
  11 *
  12 * This program is distributed in the hope that it will be useful,
  13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15 * GNU General Public License for more details.
  16 *
  17 * You should have received a copy of the GNU General Public License
  18 * along with this program; if not, write to the Free Software
  19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  20 *
  21 * Please address comments and feedback to Linas Vepstas <linas@austin.ibm.com>
  22 */
  23
  24#include <linux/delay.h>
  25#include <linux/sched.h>
  26#include <linux/init.h>
  27#include <linux/list.h>
  28#include <linux/pci.h>
  29#include <linux/proc_fs.h>
  30#include <linux/rbtree.h>
  31#include <linux/seq_file.h>
  32#include <linux/spinlock.h>
  33#include <linux/export.h>
  34#include <linux/of.h>
  35
  36#include <linux/atomic.h>
  37#include <asm/eeh.h>
  38#include <asm/eeh_event.h>
  39#include <asm/io.h>
  40#include <asm/machdep.h>
  41#include <asm/ppc-pci.h>
  42#include <asm/rtas.h>
  43
  44
  45/** Overview:
  46 *  EEH, or "Extended Error Handling" is a PCI bridge technology for
  47 *  dealing with PCI bus errors that can't be dealt with within the
  48 *  usual PCI framework, except by check-stopping the CPU.  Systems
  49 *  that are designed for high-availability/reliability cannot afford
  50 *  to crash due to a "mere" PCI error, thus the need for EEH.
  51 *  An EEH-capable bridge operates by converting a detected error
  52 *  into a "slot freeze", taking the PCI adapter off-line, making
  53 *  the slot behave, from the OS'es point of view, as if the slot
  54 *  were "empty": all reads return 0xff's and all writes are silently
  55 *  ignored.  EEH slot isolation events can be triggered by parity
  56 *  errors on the address or data busses (e.g. during posted writes),
  57 *  which in turn might be caused by low voltage on the bus, dust,
  58 *  vibration, humidity, radioactivity or plain-old failed hardware.
  59 *
  60 *  Note, however, that one of the leading causes of EEH slot
  61 *  freeze events are buggy device drivers, buggy device microcode,
  62 *  or buggy device hardware.  This is because any attempt by the
  63 *  device to bus-master data to a memory address that is not
  64 *  assigned to the device will trigger a slot freeze.   (The idea
  65 *  is to prevent devices-gone-wild from corrupting system memory).
  66 *  Buggy hardware/drivers will have a miserable time co-existing
  67 *  with EEH.
  68 *
  69 *  Ideally, a PCI device driver, when suspecting that an isolation
  70 *  event has occurred (e.g. by reading 0xff's), will then ask EEH
  71 *  whether this is the case, and then take appropriate steps to
  72 *  reset the PCI slot, the PCI device, and then resume operations.
  73 *  However, until that day,  the checking is done here, with the
  74 *  eeh_check_failure() routine embedded in the MMIO macros.  If
  75 *  the slot is found to be isolated, an "EEH Event" is synthesized
  76 *  and sent out for processing.
  77 */
  78
  79/* If a device driver keeps reading an MMIO register in an interrupt
  80 * handler after a slot isolation event, it might be broken.
  81 * This sets the threshold for how many read attempts we allow
  82 * before printing an error message.
  83 */
  84#define EEH_MAX_FAILS   2100000
  85
  86/* Time to wait for a PCI slot to report status, in milliseconds */
  87#define PCI_BUS_RESET_WAIT_MSEC (60*1000)
  88
  89/* Platform dependent EEH operations */
  90struct eeh_ops *eeh_ops = NULL;
  91
  92int eeh_subsystem_enabled;
  93EXPORT_SYMBOL(eeh_subsystem_enabled);
  94
  95/*
  96 * EEH probe mode support. The intention is to support multiple
  97 * platforms for EEH. Some platforms like pSeries do PCI emunation
  98 * based on device tree. However, other platforms like powernv probe
  99 * PCI devices from hardware. The flag is used to distinguish that.
 100 * In addition, struct eeh_ops::probe would be invoked for particular
 101 * OF node or PCI device so that the corresponding PE would be created
 102 * there.
 103 */
 104int eeh_probe_mode;
 105
 106/* Global EEH mutex */
 107DEFINE_MUTEX(eeh_mutex);
 108
 109/* Lock to avoid races due to multiple reports of an error */
 110static DEFINE_RAW_SPINLOCK(confirm_error_lock);
 111
 112/* Buffer for reporting pci register dumps. Its here in BSS, and
 113 * not dynamically alloced, so that it ends up in RMO where RTAS
 114 * can access it.
 115 */
 116#define EEH_PCI_REGS_LOG_LEN 4096
 117static unsigned char pci_regs_buf[EEH_PCI_REGS_LOG_LEN];
 118
 119/*
 120 * The struct is used to maintain the EEH global statistic
 121 * information. Besides, the EEH global statistics will be
 122 * exported to user space through procfs
 123 */
 124struct eeh_stats {
 125        u64 no_device;          /* PCI device not found         */
 126        u64 no_dn;              /* OF node not found            */
 127        u64 no_cfg_addr;        /* Config address not found     */
 128        u64 ignored_check;      /* EEH check skipped            */
 129        u64 total_mmio_ffs;     /* Total EEH checks             */
 130        u64 false_positives;    /* Unnecessary EEH checks       */
 131        u64 slot_resets;        /* PE reset                     */
 132};
 133
 134static struct eeh_stats eeh_stats;
 135
 136#define IS_BRIDGE(class_code) (((class_code)<<16) == PCI_BASE_CLASS_BRIDGE)
 137
 138/**
 139 * eeh_gather_pci_data - Copy assorted PCI config space registers to buff
 140 * @edev: device to report data for
 141 * @buf: point to buffer in which to log
 142 * @len: amount of room in buffer
 143 *
 144 * This routine captures assorted PCI configuration space data,
 145 * and puts them into a buffer for RTAS error logging.
 146 */
 147static size_t eeh_gather_pci_data(struct eeh_dev *edev, char * buf, size_t len)
 148{
 149        struct device_node *dn = eeh_dev_to_of_node(edev);
 150        struct pci_dev *dev = eeh_dev_to_pci_dev(edev);
 151        u32 cfg;
 152        int cap, i;
 153        int n = 0;
 154
 155        n += scnprintf(buf+n, len-n, "%s\n", dn->full_name);
 156        printk(KERN_WARNING "EEH: of node=%s\n", dn->full_name);
 157
 158        eeh_ops->read_config(dn, PCI_VENDOR_ID, 4, &cfg);
 159        n += scnprintf(buf+n, len-n, "dev/vend:%08x\n", cfg);
 160        printk(KERN_WARNING "EEH: PCI device/vendor: %08x\n", cfg);
 161
 162        eeh_ops->read_config(dn, PCI_COMMAND, 4, &cfg);
 163        n += scnprintf(buf+n, len-n, "cmd/stat:%x\n", cfg);
 164        printk(KERN_WARNING "EEH: PCI cmd/status register: %08x\n", cfg);
 165
 166        if (!dev) {
 167                printk(KERN_WARNING "EEH: no PCI device for this of node\n");
 168                return n;
 169        }
 170
 171        /* Gather bridge-specific registers */
 172        if (dev->class >> 16 == PCI_BASE_CLASS_BRIDGE) {
 173                eeh_ops->read_config(dn, PCI_SEC_STATUS, 2, &cfg);
 174                n += scnprintf(buf+n, len-n, "sec stat:%x\n", cfg);
 175                printk(KERN_WARNING "EEH: Bridge secondary status: %04x\n", cfg);
 176
 177                eeh_ops->read_config(dn, PCI_BRIDGE_CONTROL, 2, &cfg);
 178                n += scnprintf(buf+n, len-n, "brdg ctl:%x\n", cfg);
 179                printk(KERN_WARNING "EEH: Bridge control: %04x\n", cfg);
 180        }
 181
 182        /* Dump out the PCI-X command and status regs */
 183        cap = pci_find_capability(dev, PCI_CAP_ID_PCIX);
 184        if (cap) {
 185                eeh_ops->read_config(dn, cap, 4, &cfg);
 186                n += scnprintf(buf+n, len-n, "pcix-cmd:%x\n", cfg);
 187                printk(KERN_WARNING "EEH: PCI-X cmd: %08x\n", cfg);
 188
 189                eeh_ops->read_config(dn, cap+4, 4, &cfg);
 190                n += scnprintf(buf+n, len-n, "pcix-stat:%x\n", cfg);
 191                printk(KERN_WARNING "EEH: PCI-X status: %08x\n", cfg);
 192        }
 193
 194        /* If PCI-E capable, dump PCI-E cap 10, and the AER */
 195        if (pci_is_pcie(dev)) {
 196                n += scnprintf(buf+n, len-n, "pci-e cap10:\n");
 197                printk(KERN_WARNING
 198                       "EEH: PCI-E capabilities and status follow:\n");
 199
 200                for (i=0; i<=8; i++) {
 201                        eeh_ops->read_config(dn, dev->pcie_cap+4*i, 4, &cfg);
 202                        n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
 203                        printk(KERN_WARNING "EEH: PCI-E %02x: %08x\n", i, cfg);
 204                }
 205
 206                cap = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
 207                if (cap) {
 208                        n += scnprintf(buf+n, len-n, "pci-e AER:\n");
 209                        printk(KERN_WARNING
 210                               "EEH: PCI-E AER capability register set follows:\n");
 211
 212                        for (i=0; i<14; i++) {
 213                                eeh_ops->read_config(dn, cap+4*i, 4, &cfg);
 214                                n += scnprintf(buf+n, len-n, "%02x:%x\n", 4*i, cfg);
 215                                printk(KERN_WARNING "EEH: PCI-E AER %02x: %08x\n", i, cfg);
 216                        }
 217                }
 218        }
 219
 220        return n;
 221}
 222
 223/**
 224 * eeh_slot_error_detail - Generate combined log including driver log and error log
 225 * @pe: EEH PE
 226 * @severity: temporary or permanent error log
 227 *
 228 * This routine should be called to generate the combined log, which
 229 * is comprised of driver log and error log. The driver log is figured
 230 * out from the config space of the corresponding PCI device, while
 231 * the error log is fetched through platform dependent function call.
 232 */
 233void eeh_slot_error_detail(struct eeh_pe *pe, int severity)
 234{
 235        size_t loglen = 0;
 236        struct eeh_dev *edev;
 237
 238        eeh_pci_enable(pe, EEH_OPT_THAW_MMIO);
 239        eeh_ops->configure_bridge(pe);
 240        eeh_pe_restore_bars(pe);
 241
 242        pci_regs_buf[0] = 0;
 243        eeh_pe_for_each_dev(pe, edev) {
 244                loglen += eeh_gather_pci_data(edev, pci_regs_buf,
 245                                EEH_PCI_REGS_LOG_LEN);
 246        }
 247
 248        eeh_ops->get_log(pe, severity, pci_regs_buf, loglen);
 249}
 250
 251/**
 252 * eeh_token_to_phys - Convert EEH address token to phys address
 253 * @token: I/O token, should be address in the form 0xA....
 254 *
 255 * This routine should be called to convert virtual I/O address
 256 * to physical one.
 257 */
 258static inline unsigned long eeh_token_to_phys(unsigned long token)
 259{
 260        pte_t *ptep;
 261        unsigned long pa;
 262        int hugepage_shift;
 263
 264        /*
 265         * We won't find hugepages here, iomem
 266         */
 267        ptep = find_linux_pte_or_hugepte(init_mm.pgd, token, &hugepage_shift);
 268        if (!ptep)
 269                return token;
 270        WARN_ON(hugepage_shift);
 271        pa = pte_pfn(*ptep) << PAGE_SHIFT;
 272
 273        return pa | (token & (PAGE_SIZE-1));
 274}
 275
 276/**
 277 * eeh_dev_check_failure - Check if all 1's data is due to EEH slot freeze
 278 * @edev: eeh device
 279 *
 280 * Check for an EEH failure for the given device node.  Call this
 281 * routine if the result of a read was all 0xff's and you want to
 282 * find out if this is due to an EEH slot freeze.  This routine
 283 * will query firmware for the EEH status.
 284 *
 285 * Returns 0 if there has not been an EEH error; otherwise returns
 286 * a non-zero value and queues up a slot isolation event notification.
 287 *
 288 * It is safe to call this routine in an interrupt context.
 289 */
 290int eeh_dev_check_failure(struct eeh_dev *edev)
 291{
 292        int ret;
 293        unsigned long flags;
 294        struct device_node *dn;
 295        struct pci_dev *dev;
 296        struct eeh_pe *pe;
 297        int rc = 0;
 298        const char *location;
 299
 300        eeh_stats.total_mmio_ffs++;
 301
 302        if (!eeh_subsystem_enabled)
 303                return 0;
 304
 305        if (!edev) {
 306                eeh_stats.no_dn++;
 307                return 0;
 308        }
 309        dn = eeh_dev_to_of_node(edev);
 310        dev = eeh_dev_to_pci_dev(edev);
 311        pe = edev->pe;
 312
 313        /* Access to IO BARs might get this far and still not want checking. */
 314        if (!pe) {
 315                eeh_stats.ignored_check++;
 316                pr_debug("EEH: Ignored check for %s %s\n",
 317                        eeh_pci_name(dev), dn->full_name);
 318                return 0;
 319        }
 320
 321        if (!pe->addr && !pe->config_addr) {
 322                eeh_stats.no_cfg_addr++;
 323                return 0;
 324        }
 325
 326        /* If we already have a pending isolation event for this
 327         * slot, we know it's bad already, we don't need to check.
 328         * Do this checking under a lock; as multiple PCI devices
 329         * in one slot might report errors simultaneously, and we
 330         * only want one error recovery routine running.
 331         */
 332        raw_spin_lock_irqsave(&confirm_error_lock, flags);
 333        rc = 1;
 334        if (pe->state & EEH_PE_ISOLATED) {
 335                pe->check_count++;
 336                if (pe->check_count % EEH_MAX_FAILS == 0) {
 337                        location = of_get_property(dn, "ibm,loc-code", NULL);
 338                        printk(KERN_ERR "EEH: %d reads ignored for recovering device at "
 339                                "location=%s driver=%s pci addr=%s\n",
 340                                pe->check_count, location,
 341                                eeh_driver_name(dev), eeh_pci_name(dev));
 342                        printk(KERN_ERR "EEH: Might be infinite loop in %s driver\n",
 343                                eeh_driver_name(dev));
 344                        dump_stack();
 345                }
 346                goto dn_unlock;
 347        }
 348
 349        /*
 350         * Now test for an EEH failure.  This is VERY expensive.
 351         * Note that the eeh_config_addr may be a parent device
 352         * in the case of a device behind a bridge, or it may be
 353         * function zero of a multi-function device.
 354         * In any case they must share a common PHB.
 355         */
 356        ret = eeh_ops->get_state(pe, NULL);
 357
 358        /* Note that config-io to empty slots may fail;
 359         * they are empty when they don't have children.
 360         * We will punt with the following conditions: Failure to get
 361         * PE's state, EEH not support and Permanently unavailable
 362         * state, PE is in good state.
 363         */
 364        if ((ret < 0) ||
 365            (ret == EEH_STATE_NOT_SUPPORT) ||
 366            (ret & (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) ==
 367            (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE)) {
 368                eeh_stats.false_positives++;
 369                pe->false_positives++;
 370                rc = 0;
 371                goto dn_unlock;
 372        }
 373
 374        eeh_stats.slot_resets++;
 375
 376        /* Avoid repeated reports of this failure, including problems
 377         * with other functions on this device, and functions under
 378         * bridges.
 379         */
 380        eeh_pe_state_mark(pe, EEH_PE_ISOLATED);
 381        raw_spin_unlock_irqrestore(&confirm_error_lock, flags);
 382
 383        eeh_send_failure_event(pe);
 384
 385        /* Most EEH events are due to device driver bugs.  Having
 386         * a stack trace will help the device-driver authors figure
 387         * out what happened.  So print that out.
 388         */
 389        WARN(1, "EEH: failure detected\n");
 390        return 1;
 391
 392dn_unlock:
 393        raw_spin_unlock_irqrestore(&confirm_error_lock, flags);
 394        return rc;
 395}
 396
 397EXPORT_SYMBOL_GPL(eeh_dev_check_failure);
 398
 399/**
 400 * eeh_check_failure - Check if all 1's data is due to EEH slot freeze
 401 * @token: I/O token, should be address in the form 0xA....
 402 * @val: value, should be all 1's (XXX why do we need this arg??)
 403 *
 404 * Check for an EEH failure at the given token address.  Call this
 405 * routine if the result of a read was all 0xff's and you want to
 406 * find out if this is due to an EEH slot freeze event.  This routine
 407 * will query firmware for the EEH status.
 408 *
 409 * Note this routine is safe to call in an interrupt context.
 410 */
 411unsigned long eeh_check_failure(const volatile void __iomem *token, unsigned long val)
 412{
 413        unsigned long addr;
 414        struct eeh_dev *edev;
 415
 416        /* Finding the phys addr + pci device; this is pretty quick. */
 417        addr = eeh_token_to_phys((unsigned long __force) token);
 418        edev = eeh_addr_cache_get_dev(addr);
 419        if (!edev) {
 420                eeh_stats.no_device++;
 421                return val;
 422        }
 423
 424        eeh_dev_check_failure(edev);
 425
 426        pci_dev_put(eeh_dev_to_pci_dev(edev));
 427        return val;
 428}
 429
 430EXPORT_SYMBOL(eeh_check_failure);
 431
 432
 433/**
 434 * eeh_pci_enable - Enable MMIO or DMA transfers for this slot
 435 * @pe: EEH PE
 436 *
 437 * This routine should be called to reenable frozen MMIO or DMA
 438 * so that it would work correctly again. It's useful while doing
 439 * recovery or log collection on the indicated device.
 440 */
 441int eeh_pci_enable(struct eeh_pe *pe, int function)
 442{
 443        int rc;
 444
 445        rc = eeh_ops->set_option(pe, function);
 446        if (rc)
 447                pr_warning("%s: Unexpected state change %d on PHB#%d-PE#%x, err=%d\n",
 448                        __func__, function, pe->phb->global_number, pe->addr, rc);
 449
 450        rc = eeh_ops->wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
 451        if (rc > 0 && (rc & EEH_STATE_MMIO_ENABLED) &&
 452           (function == EEH_OPT_THAW_MMIO))
 453                return 0;
 454
 455        return rc;
 456}
 457
 458/**
 459 * pcibios_set_pcie_slot_reset - Set PCI-E reset state
 460 * @dev: pci device struct
 461 * @state: reset state to enter
 462 *
 463 * Return value:
 464 *      0 if success
 465 */
 466int pcibios_set_pcie_reset_state(struct pci_dev *dev, enum pcie_reset_state state)
 467{
 468        struct eeh_dev *edev = pci_dev_to_eeh_dev(dev);
 469        struct eeh_pe *pe = edev->pe;
 470
 471        if (!pe) {
 472                pr_err("%s: No PE found on PCI device %s\n",
 473                        __func__, pci_name(dev));
 474                return -EINVAL;
 475        }
 476
 477        switch (state) {
 478        case pcie_deassert_reset:
 479                eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);
 480                break;
 481        case pcie_hot_reset:
 482                eeh_ops->reset(pe, EEH_RESET_HOT);
 483                break;
 484        case pcie_warm_reset:
 485                eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
 486                break;
 487        default:
 488                return -EINVAL;
 489        };
 490
 491        return 0;
 492}
 493
 494/**
 495 * eeh_set_pe_freset - Check the required reset for the indicated device
 496 * @data: EEH device
 497 * @flag: return value
 498 *
 499 * Each device might have its preferred reset type: fundamental or
 500 * hot reset. The routine is used to collected the information for
 501 * the indicated device and its children so that the bunch of the
 502 * devices could be reset properly.
 503 */
 504static void *eeh_set_dev_freset(void *data, void *flag)
 505{
 506        struct pci_dev *dev;
 507        unsigned int *freset = (unsigned int *)flag;
 508        struct eeh_dev *edev = (struct eeh_dev *)data;
 509
 510        dev = eeh_dev_to_pci_dev(edev);
 511        if (dev)
 512                *freset |= dev->needs_freset;
 513
 514        return NULL;
 515}
 516
 517/**
 518 * eeh_reset_pe_once - Assert the pci #RST line for 1/4 second
 519 * @pe: EEH PE
 520 *
 521 * Assert the PCI #RST line for 1/4 second.
 522 */
 523static void eeh_reset_pe_once(struct eeh_pe *pe)
 524{
 525        unsigned int freset = 0;
 526
 527        /* Determine type of EEH reset required for
 528         * Partitionable Endpoint, a hot-reset (1)
 529         * or a fundamental reset (3).
 530         * A fundamental reset required by any device under
 531         * Partitionable Endpoint trumps hot-reset.
 532         */
 533        eeh_pe_dev_traverse(pe, eeh_set_dev_freset, &freset);
 534
 535        if (freset)
 536                eeh_ops->reset(pe, EEH_RESET_FUNDAMENTAL);
 537        else
 538                eeh_ops->reset(pe, EEH_RESET_HOT);
 539
 540        /* The PCI bus requires that the reset be held high for at least
 541         * a 100 milliseconds. We wait a bit longer 'just in case'.
 542         */
 543#define PCI_BUS_RST_HOLD_TIME_MSEC 250
 544        msleep(PCI_BUS_RST_HOLD_TIME_MSEC);
 545
 546        /* We might get hit with another EEH freeze as soon as the
 547         * pci slot reset line is dropped. Make sure we don't miss
 548         * these, and clear the flag now.
 549         */
 550        eeh_pe_state_clear(pe, EEH_PE_ISOLATED);
 551
 552        eeh_ops->reset(pe, EEH_RESET_DEACTIVATE);
 553
 554        /* After a PCI slot has been reset, the PCI Express spec requires
 555         * a 1.5 second idle time for the bus to stabilize, before starting
 556         * up traffic.
 557         */
 558#define PCI_BUS_SETTLE_TIME_MSEC 1800
 559        msleep(PCI_BUS_SETTLE_TIME_MSEC);
 560}
 561
 562/**
 563 * eeh_reset_pe - Reset the indicated PE
 564 * @pe: EEH PE
 565 *
 566 * This routine should be called to reset indicated device, including
 567 * PE. A PE might include multiple PCI devices and sometimes PCI bridges
 568 * might be involved as well.
 569 */
 570int eeh_reset_pe(struct eeh_pe *pe)
 571{
 572        int i, rc;
 573
 574        /* Take three shots at resetting the bus */
 575        for (i=0; i<3; i++) {
 576                eeh_reset_pe_once(pe);
 577
 578                rc = eeh_ops->wait_state(pe, PCI_BUS_RESET_WAIT_MSEC);
 579                if (rc == (EEH_STATE_MMIO_ACTIVE | EEH_STATE_DMA_ACTIVE))
 580                        return 0;
 581
 582                if (rc < 0) {
 583                        pr_err("%s: Unrecoverable slot failure on PHB#%d-PE#%x",
 584                                __func__, pe->phb->global_number, pe->addr);
 585                        return -1;
 586                }
 587                pr_err("EEH: bus reset %d failed on PHB#%d-PE#%x, rc=%d\n",
 588                        i+1, pe->phb->global_number, pe->addr, rc);
 589        }
 590
 591        return -1;
 592}
 593
 594/**
 595 * eeh_save_bars - Save device bars
 596 * @edev: PCI device associated EEH device
 597 *
 598 * Save the values of the device bars. Unlike the restore
 599 * routine, this routine is *not* recursive. This is because
 600 * PCI devices are added individually; but, for the restore,
 601 * an entire slot is reset at a time.
 602 */
 603void eeh_save_bars(struct eeh_dev *edev)
 604{
 605        int i;
 606        struct device_node *dn;
 607
 608        if (!edev)
 609                return;
 610        dn = eeh_dev_to_of_node(edev);
 611
 612        for (i = 0; i < 16; i++)
 613                eeh_ops->read_config(dn, i * 4, 4, &edev->config_space[i]);
 614}
 615
 616/**
 617 * eeh_ops_register - Register platform dependent EEH operations
 618 * @ops: platform dependent EEH operations
 619 *
 620 * Register the platform dependent EEH operation callback
 621 * functions. The platform should call this function before
 622 * any other EEH operations.
 623 */
 624int __init eeh_ops_register(struct eeh_ops *ops)
 625{
 626        if (!ops->name) {
 627                pr_warning("%s: Invalid EEH ops name for %p\n",
 628                        __func__, ops);
 629                return -EINVAL;
 630        }
 631
 632        if (eeh_ops && eeh_ops != ops) {
 633                pr_warning("%s: EEH ops of platform %s already existing (%s)\n",
 634                        __func__, eeh_ops->name, ops->name);
 635                return -EEXIST;
 636        }
 637
 638        eeh_ops = ops;
 639
 640        return 0;
 641}
 642
 643/**
 644 * eeh_ops_unregister - Unreigster platform dependent EEH operations
 645 * @name: name of EEH platform operations
 646 *
 647 * Unregister the platform dependent EEH operation callback
 648 * functions.
 649 */
 650int __exit eeh_ops_unregister(const char *name)
 651{
 652        if (!name || !strlen(name)) {
 653                pr_warning("%s: Invalid EEH ops name\n",
 654                        __func__);
 655                return -EINVAL;
 656        }
 657
 658        if (eeh_ops && !strcmp(eeh_ops->name, name)) {
 659                eeh_ops = NULL;
 660                return 0;
 661        }
 662
 663        return -EEXIST;
 664}
 665
 666/**
 667 * eeh_init - EEH initialization
 668 *
 669 * Initialize EEH by trying to enable it for all of the adapters in the system.
 670 * As a side effect we can determine here if eeh is supported at all.
 671 * Note that we leave EEH on so failed config cycles won't cause a machine
 672 * check.  If a user turns off EEH for a particular adapter they are really
 673 * telling Linux to ignore errors.  Some hardware (e.g. POWER5) won't
 674 * grant access to a slot if EEH isn't enabled, and so we always enable
 675 * EEH for all slots/all devices.
 676 *
 677 * The eeh-force-off option disables EEH checking globally, for all slots.
 678 * Even if force-off is set, the EEH hardware is still enabled, so that
 679 * newer systems can boot.
 680 */
 681static int __init eeh_init(void)
 682{
 683        struct pci_controller *hose, *tmp;
 684        struct device_node *phb;
 685        int ret;
 686
 687        /* call platform initialization function */
 688        if (!eeh_ops) {
 689                pr_warning("%s: Platform EEH operation not found\n",
 690                        __func__);
 691                return -EEXIST;
 692        } else if ((ret = eeh_ops->init())) {
 693                pr_warning("%s: Failed to call platform init function (%d)\n",
 694                        __func__, ret);
 695                return ret;
 696        }
 697
 698        raw_spin_lock_init(&confirm_error_lock);
 699
 700        /* Enable EEH for all adapters */
 701        if (eeh_probe_mode_devtree()) {
 702                list_for_each_entry_safe(hose, tmp,
 703                        &hose_list, list_node) {
 704                        phb = hose->dn;
 705                        traverse_pci_devices(phb, eeh_ops->of_probe, NULL);
 706                }
 707        }
 708
 709        if (eeh_subsystem_enabled)
 710                pr_info("EEH: PCI Enhanced I/O Error Handling Enabled\n");
 711        else
 712                pr_warning("EEH: No capable adapters found\n");
 713
 714        return ret;
 715}
 716
 717core_initcall_sync(eeh_init);
 718
 719/**
 720 * eeh_add_device_early - Enable EEH for the indicated device_node
 721 * @dn: device node for which to set up EEH
 722 *
 723 * This routine must be used to perform EEH initialization for PCI
 724 * devices that were added after system boot (e.g. hotplug, dlpar).
 725 * This routine must be called before any i/o is performed to the
 726 * adapter (inluding any config-space i/o).
 727 * Whether this actually enables EEH or not for this device depends
 728 * on the CEC architecture, type of the device, on earlier boot
 729 * command-line arguments & etc.
 730 */
 731static void eeh_add_device_early(struct device_node *dn)
 732{
 733        struct pci_controller *phb;
 734
 735        if (!of_node_to_eeh_dev(dn))
 736                return;
 737        phb = of_node_to_eeh_dev(dn)->phb;
 738
 739        /* USB Bus children of PCI devices will not have BUID's */
 740        if (NULL == phb || 0 == phb->buid)
 741                return;
 742
 743        /* FIXME: hotplug support on POWERNV */
 744        eeh_ops->of_probe(dn, NULL);
 745}
 746
 747/**
 748 * eeh_add_device_tree_early - Enable EEH for the indicated device
 749 * @dn: device node
 750 *
 751 * This routine must be used to perform EEH initialization for the
 752 * indicated PCI device that was added after system boot (e.g.
 753 * hotplug, dlpar).
 754 */
 755void eeh_add_device_tree_early(struct device_node *dn)
 756{
 757        struct device_node *sib;
 758
 759        for_each_child_of_node(dn, sib)
 760                eeh_add_device_tree_early(sib);
 761        eeh_add_device_early(dn);
 762}
 763EXPORT_SYMBOL_GPL(eeh_add_device_tree_early);
 764
 765/**
 766 * eeh_add_device_late - Perform EEH initialization for the indicated pci device
 767 * @dev: pci device for which to set up EEH
 768 *
 769 * This routine must be used to complete EEH initialization for PCI
 770 * devices that were added after system boot (e.g. hotplug, dlpar).
 771 */
 772static void eeh_add_device_late(struct pci_dev *dev)
 773{
 774        struct device_node *dn;
 775        struct eeh_dev *edev;
 776
 777        if (!dev || !eeh_subsystem_enabled)
 778                return;
 779
 780        pr_debug("EEH: Adding device %s\n", pci_name(dev));
 781
 782        dn = pci_device_to_OF_node(dev);
 783        edev = of_node_to_eeh_dev(dn);
 784        if (edev->pdev == dev) {
 785                pr_debug("EEH: Already referenced !\n");
 786                return;
 787        }
 788        WARN_ON(edev->pdev);
 789
 790        pci_dev_get(dev);
 791        edev->pdev = dev;
 792        dev->dev.archdata.edev = edev;
 793
 794        eeh_addr_cache_insert_dev(dev);
 795}
 796
 797/**
 798 * eeh_add_device_tree_late - Perform EEH initialization for the indicated PCI bus
 799 * @bus: PCI bus
 800 *
 801 * This routine must be used to perform EEH initialization for PCI
 802 * devices which are attached to the indicated PCI bus. The PCI bus
 803 * is added after system boot through hotplug or dlpar.
 804 */
 805void eeh_add_device_tree_late(struct pci_bus *bus)
 806{
 807        struct pci_dev *dev;
 808
 809        list_for_each_entry(dev, &bus->devices, bus_list) {
 810                eeh_add_device_late(dev);
 811                if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
 812                        struct pci_bus *subbus = dev->subordinate;
 813                        if (subbus)
 814                                eeh_add_device_tree_late(subbus);
 815                }
 816        }
 817}
 818EXPORT_SYMBOL_GPL(eeh_add_device_tree_late);
 819
 820/**
 821 * eeh_add_sysfs_files - Add EEH sysfs files for the indicated PCI bus
 822 * @bus: PCI bus
 823 *
 824 * This routine must be used to add EEH sysfs files for PCI
 825 * devices which are attached to the indicated PCI bus. The PCI bus
 826 * is added after system boot through hotplug or dlpar.
 827 */
 828void eeh_add_sysfs_files(struct pci_bus *bus)
 829{
 830        struct pci_dev *dev;
 831
 832        list_for_each_entry(dev, &bus->devices, bus_list) {
 833                eeh_sysfs_add_device(dev);
 834                if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
 835                        struct pci_bus *subbus = dev->subordinate;
 836                        if (subbus)
 837                                eeh_add_sysfs_files(subbus);
 838                }
 839        }
 840}
 841EXPORT_SYMBOL_GPL(eeh_add_sysfs_files);
 842
 843/**
 844 * eeh_remove_device - Undo EEH setup for the indicated pci device
 845 * @dev: pci device to be removed
 846 * @purge_pe: remove the PE or not
 847 *
 848 * This routine should be called when a device is removed from
 849 * a running system (e.g. by hotplug or dlpar).  It unregisters
 850 * the PCI device from the EEH subsystem.  I/O errors affecting
 851 * this device will no longer be detected after this call; thus,
 852 * i/o errors affecting this slot may leave this device unusable.
 853 */
 854static void eeh_remove_device(struct pci_dev *dev, int purge_pe)
 855{
 856        struct eeh_dev *edev;
 857
 858        if (!dev || !eeh_subsystem_enabled)
 859                return;
 860        edev = pci_dev_to_eeh_dev(dev);
 861
 862        /* Unregister the device with the EEH/PCI address search system */
 863        pr_debug("EEH: Removing device %s\n", pci_name(dev));
 864
 865        if (!edev || !edev->pdev) {
 866                pr_debug("EEH: Not referenced !\n");
 867                return;
 868        }
 869        edev->pdev = NULL;
 870        dev->dev.archdata.edev = NULL;
 871        pci_dev_put(dev);
 872
 873        eeh_rmv_from_parent_pe(edev, purge_pe);
 874        eeh_addr_cache_rmv_dev(dev);
 875        eeh_sysfs_remove_device(dev);
 876}
 877
 878/**
 879 * eeh_remove_bus_device - Undo EEH setup for the indicated PCI device
 880 * @dev: PCI device
 881 * @purge_pe: remove the corresponding PE or not
 882 *
 883 * This routine must be called when a device is removed from the
 884 * running system through hotplug or dlpar. The corresponding
 885 * PCI address cache will be removed.
 886 */
 887void eeh_remove_bus_device(struct pci_dev *dev, int purge_pe)
 888{
 889        struct pci_bus *bus = dev->subordinate;
 890        struct pci_dev *child, *tmp;
 891
 892        eeh_remove_device(dev, purge_pe);
 893
 894        if (bus && dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
 895                list_for_each_entry_safe(child, tmp, &bus->devices, bus_list)
 896                         eeh_remove_bus_device(child, purge_pe);
 897        }
 898}
 899EXPORT_SYMBOL_GPL(eeh_remove_bus_device);
 900
 901static int proc_eeh_show(struct seq_file *m, void *v)
 902{
 903        if (0 == eeh_subsystem_enabled) {
 904                seq_printf(m, "EEH Subsystem is globally disabled\n");
 905                seq_printf(m, "eeh_total_mmio_ffs=%llu\n", eeh_stats.total_mmio_ffs);
 906        } else {
 907                seq_printf(m, "EEH Subsystem is enabled\n");
 908                seq_printf(m,
 909                                "no device=%llu\n"
 910                                "no device node=%llu\n"
 911                                "no config address=%llu\n"
 912                                "check not wanted=%llu\n"
 913                                "eeh_total_mmio_ffs=%llu\n"
 914                                "eeh_false_positives=%llu\n"
 915                                "eeh_slot_resets=%llu\n",
 916                                eeh_stats.no_device,
 917                                eeh_stats.no_dn,
 918                                eeh_stats.no_cfg_addr,
 919                                eeh_stats.ignored_check,
 920                                eeh_stats.total_mmio_ffs,
 921                                eeh_stats.false_positives,
 922                                eeh_stats.slot_resets);
 923        }
 924
 925        return 0;
 926}
 927
 928static int proc_eeh_open(struct inode *inode, struct file *file)
 929{
 930        return single_open(file, proc_eeh_show, NULL);
 931}
 932
 933static const struct file_operations proc_eeh_operations = {
 934        .open      = proc_eeh_open,
 935        .read      = seq_read,
 936        .llseek    = seq_lseek,
 937        .release   = single_release,
 938};
 939
 940static int __init eeh_init_proc(void)
 941{
 942        if (machine_is(pseries))
 943                proc_create("powerpc/eeh", 0, NULL, &proc_eeh_operations);
 944        return 0;
 945}
 946__initcall(eeh_init_proc);
 947