linux/arch/powerpc/platforms/pseries/eeh_pseries.c
<<
>>
Prefs
   1/*
   2 * The file intends to implement the platform dependent EEH operations on pseries.
   3 * Actually, the pseries platform is built based on RTAS heavily. That means the
   4 * pseries platform dependent EEH operations will be built on RTAS calls. The functions
   5 * are derived from arch/powerpc/platforms/pseries/eeh.c and necessary cleanup has
   6 * been done.
   7 *
   8 * Copyright Benjamin Herrenschmidt & Gavin Shan, IBM Corporation 2011.
   9 * Copyright IBM Corporation 2001, 2005, 2006
  10 * Copyright Dave Engebretsen & Todd Inglett 2001
  11 * Copyright Linas Vepstas 2005, 2006
  12 *
  13 * This program is free software; you can redistribute it and/or modify
  14 * it under the terms of the GNU General Public License as published by
  15 * the Free Software Foundation; either version 2 of the License, or
  16 * (at your option) any later version.
  17 *
  18 * This program is distributed in the hope that it will be useful,
  19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21 * GNU General Public License for more details.
  22 *
  23 * You should have received a copy of the GNU General Public License
  24 * along with this program; if not, write to the Free Software
  25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  26 */
  27
  28#include <linux/atomic.h>
  29#include <linux/delay.h>
  30#include <linux/export.h>
  31#include <linux/init.h>
  32#include <linux/list.h>
  33#include <linux/of.h>
  34#include <linux/pci.h>
  35#include <linux/proc_fs.h>
  36#include <linux/rbtree.h>
  37#include <linux/sched.h>
  38#include <linux/seq_file.h>
  39#include <linux/spinlock.h>
  40#include <linux/crash_dump.h>
  41
  42#include <asm/eeh.h>
  43#include <asm/eeh_event.h>
  44#include <asm/io.h>
  45#include <asm/machdep.h>
  46#include <asm/ppc-pci.h>
  47#include <asm/rtas.h>
  48
  49/* RTAS tokens */
  50static int ibm_set_eeh_option;
  51static int ibm_set_slot_reset;
  52static int ibm_read_slot_reset_state;
  53static int ibm_read_slot_reset_state2;
  54static int ibm_slot_error_detail;
  55static int ibm_get_config_addr_info;
  56static int ibm_get_config_addr_info2;
  57static int ibm_configure_pe;
  58
  59static void pseries_pcibios_bus_add_device(struct pci_dev *pdev)
  60{
  61        struct pci_dn *pdn = pci_get_pdn(pdev);
  62
  63        if (eeh_has_flag(EEH_FORCE_DISABLED))
  64                return;
  65
  66        dev_dbg(&pdev->dev, "EEH: Setting up device\n");
  67#ifdef CONFIG_PCI_IOV
  68        if (pdev->is_virtfn) {
  69                pdn->device_id  =  pdev->device;
  70                pdn->vendor_id  =  pdev->vendor;
  71                pdn->class_code =  pdev->class;
  72                /*
  73                 * Last allow unfreeze return code used for retrieval
  74                 * by user space in eeh-sysfs to show the last command
  75                 * completion from platform.
  76                 */
  77                pdn->last_allow_rc =  0;
  78        }
  79#endif
  80        pseries_eeh_init_edev(pdn);
  81#ifdef CONFIG_PCI_IOV
  82        if (pdev->is_virtfn) {
  83                /*
  84                 * FIXME: This really should be handled by choosing the right
  85                 *        parent PE in in pseries_eeh_init_edev().
  86                 */
  87                struct eeh_pe *physfn_pe = pci_dev_to_eeh_dev(pdev->physfn)->pe;
  88                struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
  89
  90                edev->pe_config_addr =  (pdn->busno << 16) | (pdn->devfn << 8);
  91                eeh_pe_tree_remove(edev); /* Remove as it is adding to bus pe */
  92                eeh_pe_tree_insert(edev, physfn_pe);   /* Add as VF PE type */
  93        }
  94#endif
  95        eeh_probe_device(pdev);
  96}
  97
  98
  99/**
 100 * pseries_eeh_get_pe_config_addr - Find the pe_config_addr for a device
 101 * @pdn: pci_dn of the input device
 102 *
 103 * The EEH RTAS calls use a tuple consisting of: (buid_hi, buid_lo,
 104 * pe_config_addr) as a handle to a given PE. This function finds the
 105 * pe_config_addr based on the device's config addr.
 106 *
 107 * Keep in mind that the pe_config_addr *might* be numerically identical to the
 108 * device's config addr, but the two are conceptually distinct.
 109 *
 110 * Returns the pe_config_addr, or a negative error code.
 111 */
 112static int pseries_eeh_get_pe_config_addr(struct pci_dn *pdn)
 113{
 114        int config_addr = rtas_config_addr(pdn->busno, pdn->devfn, 0);
 115        struct pci_controller *phb = pdn->phb;
 116        int ret, rets[3];
 117
 118        if (ibm_get_config_addr_info2 != RTAS_UNKNOWN_SERVICE) {
 119                /*
 120                 * First of all, use function 1 to determine if this device is
 121                 * part of a PE or not. ret[0] being zero indicates it's not.
 122                 */
 123                ret = rtas_call(ibm_get_config_addr_info2, 4, 2, rets,
 124                                config_addr, BUID_HI(phb->buid),
 125                                BUID_LO(phb->buid), 1);
 126                if (ret || (rets[0] == 0))
 127                        return -ENOENT;
 128
 129                /* Retrieve the associated PE config address with function 0 */
 130                ret = rtas_call(ibm_get_config_addr_info2, 4, 2, rets,
 131                                config_addr, BUID_HI(phb->buid),
 132                                BUID_LO(phb->buid), 0);
 133                if (ret) {
 134                        pr_warn("%s: Failed to get address for PHB#%x-PE#%x\n",
 135                                __func__, phb->global_number, config_addr);
 136                        return -ENXIO;
 137                }
 138
 139                return rets[0];
 140        }
 141
 142        if (ibm_get_config_addr_info != RTAS_UNKNOWN_SERVICE) {
 143                ret = rtas_call(ibm_get_config_addr_info, 4, 2, rets,
 144                                config_addr, BUID_HI(phb->buid),
 145                                BUID_LO(phb->buid), 0);
 146                if (ret) {
 147                        pr_warn("%s: Failed to get address for PHB#%x-PE#%x\n",
 148                                __func__, phb->global_number, config_addr);
 149                        return -ENXIO;
 150                }
 151
 152                return rets[0];
 153        }
 154
 155        /*
 156         * PAPR does describe a process for finding the pe_config_addr that was
 157         * used before the ibm,get-config-addr-info calls were added. However,
 158         * I haven't found *any* systems that don't have that RTAS call
 159         * implemented. If you happen to find one that needs the old DT based
 160         * process, patches are welcome!
 161         */
 162        return -ENOENT;
 163}
 164
 165/**
 166 * pseries_eeh_phb_reset - Reset the specified PHB
 167 * @phb: PCI controller
 168 * @config_adddr: the associated config address
 169 * @option: reset option
 170 *
 171 * Reset the specified PHB/PE
 172 */
 173static int pseries_eeh_phb_reset(struct pci_controller *phb, int config_addr, int option)
 174{
 175        int ret;
 176
 177        /* Reset PE through RTAS call */
 178        ret = rtas_call(ibm_set_slot_reset, 4, 1, NULL,
 179                        config_addr, BUID_HI(phb->buid),
 180                        BUID_LO(phb->buid), option);
 181
 182        /* If fundamental-reset not supported, try hot-reset */
 183        if (option == EEH_RESET_FUNDAMENTAL && ret == -8) {
 184                option = EEH_RESET_HOT;
 185                ret = rtas_call(ibm_set_slot_reset, 4, 1, NULL,
 186                                config_addr, BUID_HI(phb->buid),
 187                                BUID_LO(phb->buid), option);
 188        }
 189
 190        /* We need reset hold or settlement delay */
 191        if (option == EEH_RESET_FUNDAMENTAL || option == EEH_RESET_HOT)
 192                msleep(EEH_PE_RST_HOLD_TIME);
 193        else
 194                msleep(EEH_PE_RST_SETTLE_TIME);
 195
 196        return ret;
 197}
 198
 199/**
 200 * pseries_eeh_phb_configure_bridge - Configure PCI bridges in the indicated PE
 201 * @phb: PCI controller
 202 * @config_adddr: the associated config address
 203 *
 204 * The function will be called to reconfigure the bridges included
 205 * in the specified PE so that the mulfunctional PE would be recovered
 206 * again.
 207 */
 208static int pseries_eeh_phb_configure_bridge(struct pci_controller *phb, int config_addr)
 209{
 210        int ret;
 211        /* Waiting 0.2s maximum before skipping configuration */
 212        int max_wait = 200;
 213
 214        while (max_wait > 0) {
 215                ret = rtas_call(ibm_configure_pe, 3, 1, NULL,
 216                                config_addr, BUID_HI(phb->buid),
 217                                BUID_LO(phb->buid));
 218
 219                if (!ret)
 220                        return ret;
 221                if (ret < 0)
 222                        break;
 223
 224                /*
 225                 * If RTAS returns a delay value that's above 100ms, cut it
 226                 * down to 100ms in case firmware made a mistake.  For more
 227                 * on how these delay values work see rtas_busy_delay_time
 228                 */
 229                if (ret > RTAS_EXTENDED_DELAY_MIN+2 &&
 230                    ret <= RTAS_EXTENDED_DELAY_MAX)
 231                        ret = RTAS_EXTENDED_DELAY_MIN+2;
 232
 233                max_wait -= rtas_busy_delay_time(ret);
 234
 235                if (max_wait < 0)
 236                        break;
 237
 238                rtas_busy_delay(ret);
 239        }
 240
 241        pr_warn("%s: Unable to configure bridge PHB#%x-PE#%x (%d)\n",
 242                __func__, phb->global_number, config_addr, ret);
 243        /* PAPR defines -3 as "Parameter Error" for this function: */
 244        if (ret == -3)
 245                return -EINVAL;
 246        else
 247                return -EIO;
 248}
 249
 250/*
 251 * Buffer for reporting slot-error-detail rtas calls. Its here
 252 * in BSS, and not dynamically alloced, so that it ends up in
 253 * RMO where RTAS can access it.
 254 */
 255static unsigned char slot_errbuf[RTAS_ERROR_LOG_MAX];
 256static DEFINE_SPINLOCK(slot_errbuf_lock);
 257static int eeh_error_buf_size;
 258
 259static int pseries_eeh_cap_start(struct pci_dn *pdn)
 260{
 261        u32 status;
 262
 263        if (!pdn)
 264                return 0;
 265
 266        rtas_read_config(pdn, PCI_STATUS, 2, &status);
 267        if (!(status & PCI_STATUS_CAP_LIST))
 268                return 0;
 269
 270        return PCI_CAPABILITY_LIST;
 271}
 272
 273
 274static int pseries_eeh_find_cap(struct pci_dn *pdn, int cap)
 275{
 276        int pos = pseries_eeh_cap_start(pdn);
 277        int cnt = 48;   /* Maximal number of capabilities */
 278        u32 id;
 279
 280        if (!pos)
 281                return 0;
 282
 283        while (cnt--) {
 284                rtas_read_config(pdn, pos, 1, &pos);
 285                if (pos < 0x40)
 286                        break;
 287                pos &= ~3;
 288                rtas_read_config(pdn, pos + PCI_CAP_LIST_ID, 1, &id);
 289                if (id == 0xff)
 290                        break;
 291                if (id == cap)
 292                        return pos;
 293                pos += PCI_CAP_LIST_NEXT;
 294        }
 295
 296        return 0;
 297}
 298
 299static int pseries_eeh_find_ecap(struct pci_dn *pdn, int cap)
 300{
 301        struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
 302        u32 header;
 303        int pos = 256;
 304        int ttl = (4096 - 256) / 8;
 305
 306        if (!edev || !edev->pcie_cap)
 307                return 0;
 308        if (rtas_read_config(pdn, pos, 4, &header) != PCIBIOS_SUCCESSFUL)
 309                return 0;
 310        else if (!header)
 311                return 0;
 312
 313        while (ttl-- > 0) {
 314                if (PCI_EXT_CAP_ID(header) == cap && pos)
 315                        return pos;
 316
 317                pos = PCI_EXT_CAP_NEXT(header);
 318                if (pos < 256)
 319                        break;
 320
 321                if (rtas_read_config(pdn, pos, 4, &header) != PCIBIOS_SUCCESSFUL)
 322                        break;
 323        }
 324
 325        return 0;
 326}
 327
 328/**
 329 * pseries_eeh_pe_get_parent - Retrieve the parent PE
 330 * @edev: EEH device
 331 *
 332 * The whole PEs existing in the system are organized as hierarchy
 333 * tree. The function is used to retrieve the parent PE according
 334 * to the parent EEH device.
 335 */
 336static struct eeh_pe *pseries_eeh_pe_get_parent(struct eeh_dev *edev)
 337{
 338        struct eeh_dev *parent;
 339        struct pci_dn *pdn = eeh_dev_to_pdn(edev);
 340
 341        /*
 342         * It might have the case for the indirect parent
 343         * EEH device already having associated PE, but
 344         * the direct parent EEH device doesn't have yet.
 345         */
 346        if (edev->physfn)
 347                pdn = pci_get_pdn(edev->physfn);
 348        else
 349                pdn = pdn ? pdn->parent : NULL;
 350        while (pdn) {
 351                /* We're poking out of PCI territory */
 352                parent = pdn_to_eeh_dev(pdn);
 353                if (!parent)
 354                        return NULL;
 355
 356                if (parent->pe)
 357                        return parent->pe;
 358
 359                pdn = pdn->parent;
 360        }
 361
 362        return NULL;
 363}
 364
 365/**
 366 * pseries_eeh_init_edev - initialise the eeh_dev and eeh_pe for a pci_dn
 367 *
 368 * @pdn: PCI device node
 369 *
 370 * When we discover a new PCI device via the device-tree we create a
 371 * corresponding pci_dn and we allocate, but don't initialise, an eeh_dev.
 372 * This function takes care of the initialisation and inserts the eeh_dev
 373 * into the correct eeh_pe. If no eeh_pe exists we'll allocate one.
 374 */
 375void pseries_eeh_init_edev(struct pci_dn *pdn)
 376{
 377        struct eeh_pe pe, *parent;
 378        struct eeh_dev *edev;
 379        u32 pcie_flags;
 380        int ret;
 381
 382        if (WARN_ON_ONCE(!eeh_has_flag(EEH_PROBE_MODE_DEVTREE)))
 383                return;
 384
 385        /*
 386         * Find the eeh_dev for this pdn. The storage for the eeh_dev was
 387         * allocated at the same time as the pci_dn.
 388         *
 389         * XXX: We should probably re-visit that.
 390         */
 391        edev = pdn_to_eeh_dev(pdn);
 392        if (!edev)
 393                return;
 394
 395        /*
 396         * If ->pe is set then we've already probed this device. We hit
 397         * this path when a pci_dev is removed and rescanned while recovering
 398         * a PE (i.e. for devices where the driver doesn't support error
 399         * recovery).
 400         */
 401        if (edev->pe)
 402                return;
 403
 404        /* Check class/vendor/device IDs */
 405        if (!pdn->vendor_id || !pdn->device_id || !pdn->class_code)
 406                return;
 407
 408        /* Skip for PCI-ISA bridge */
 409        if ((pdn->class_code >> 8) == PCI_CLASS_BRIDGE_ISA)
 410                return;
 411
 412        eeh_edev_dbg(edev, "Probing device\n");
 413
 414        /*
 415         * Update class code and mode of eeh device. We need
 416         * correctly reflects that current device is root port
 417         * or PCIe switch downstream port.
 418         */
 419        edev->pcix_cap = pseries_eeh_find_cap(pdn, PCI_CAP_ID_PCIX);
 420        edev->pcie_cap = pseries_eeh_find_cap(pdn, PCI_CAP_ID_EXP);
 421        edev->aer_cap = pseries_eeh_find_ecap(pdn, PCI_EXT_CAP_ID_ERR);
 422        edev->mode &= 0xFFFFFF00;
 423        if ((pdn->class_code >> 8) == PCI_CLASS_BRIDGE_PCI) {
 424                edev->mode |= EEH_DEV_BRIDGE;
 425                if (edev->pcie_cap) {
 426                        rtas_read_config(pdn, edev->pcie_cap + PCI_EXP_FLAGS,
 427                                         2, &pcie_flags);
 428                        pcie_flags = (pcie_flags & PCI_EXP_FLAGS_TYPE) >> 4;
 429                        if (pcie_flags == PCI_EXP_TYPE_ROOT_PORT)
 430                                edev->mode |= EEH_DEV_ROOT_PORT;
 431                        else if (pcie_flags == PCI_EXP_TYPE_DOWNSTREAM)
 432                                edev->mode |= EEH_DEV_DS_PORT;
 433                }
 434        }
 435
 436        /* first up, find the pe_config_addr for the PE containing the device */
 437        ret = pseries_eeh_get_pe_config_addr(pdn);
 438        if (ret < 0) {
 439                eeh_edev_dbg(edev, "Unable to find pe_config_addr\n");
 440                goto err;
 441        }
 442
 443        /* Try enable EEH on the fake PE */
 444        memset(&pe, 0, sizeof(struct eeh_pe));
 445        pe.phb = pdn->phb;
 446        pe.addr = ret;
 447
 448        eeh_edev_dbg(edev, "Enabling EEH on device\n");
 449        ret = eeh_ops->set_option(&pe, EEH_OPT_ENABLE);
 450        if (ret) {
 451                eeh_edev_dbg(edev, "EEH failed to enable on device (code %d)\n", ret);
 452                goto err;
 453        }
 454
 455        edev->pe_config_addr = pe.addr;
 456
 457        eeh_add_flag(EEH_ENABLED);
 458
 459        parent = pseries_eeh_pe_get_parent(edev);
 460        eeh_pe_tree_insert(edev, parent);
 461        eeh_save_bars(edev);
 462        eeh_edev_dbg(edev, "EEH enabled for device");
 463
 464        return;
 465
 466err:
 467        eeh_edev_dbg(edev, "EEH is unsupported on device (code = %d)\n", ret);
 468}
 469
 470static struct eeh_dev *pseries_eeh_probe(struct pci_dev *pdev)
 471{
 472        struct eeh_dev *edev;
 473        struct pci_dn *pdn;
 474
 475        pdn = pci_get_pdn_by_devfn(pdev->bus, pdev->devfn);
 476        if (!pdn)
 477                return NULL;
 478
 479        /*
 480         * If the system supports EEH on this device then the eeh_dev was
 481         * configured and inserted into a PE in pseries_eeh_init_edev()
 482         */
 483        edev = pdn_to_eeh_dev(pdn);
 484        if (!edev || !edev->pe)
 485                return NULL;
 486
 487        return edev;
 488}
 489
 490/**
 491 * pseries_eeh_init_edev_recursive - Enable EEH for the indicated device
 492 * @pdn: PCI device node
 493 *
 494 * This routine must be used to perform EEH initialization for the
 495 * indicated PCI device that was added after system boot (e.g.
 496 * hotplug, dlpar).
 497 */
 498void pseries_eeh_init_edev_recursive(struct pci_dn *pdn)
 499{
 500        struct pci_dn *n;
 501
 502        if (!pdn)
 503                return;
 504
 505        list_for_each_entry(n, &pdn->child_list, list)
 506                pseries_eeh_init_edev_recursive(n);
 507
 508        pseries_eeh_init_edev(pdn);
 509}
 510EXPORT_SYMBOL_GPL(pseries_eeh_init_edev_recursive);
 511
 512/**
 513 * pseries_eeh_set_option - Initialize EEH or MMIO/DMA reenable
 514 * @pe: EEH PE
 515 * @option: operation to be issued
 516 *
 517 * The function is used to control the EEH functionality globally.
 518 * Currently, following options are support according to PAPR:
 519 * Enable EEH, Disable EEH, Enable MMIO and Enable DMA
 520 */
 521static int pseries_eeh_set_option(struct eeh_pe *pe, int option)
 522{
 523        int ret = 0;
 524
 525        /*
 526         * When we're enabling or disabling EEH functioality on
 527         * the particular PE, the PE config address is possibly
 528         * unavailable. Therefore, we have to figure it out from
 529         * the FDT node.
 530         */
 531        switch (option) {
 532        case EEH_OPT_DISABLE:
 533        case EEH_OPT_ENABLE:
 534        case EEH_OPT_THAW_MMIO:
 535        case EEH_OPT_THAW_DMA:
 536                break;
 537        case EEH_OPT_FREEZE_PE:
 538                /* Not support */
 539                return 0;
 540        default:
 541                pr_err("%s: Invalid option %d\n", __func__, option);
 542                return -EINVAL;
 543        }
 544
 545        ret = rtas_call(ibm_set_eeh_option, 4, 1, NULL,
 546                        pe->addr, BUID_HI(pe->phb->buid),
 547                        BUID_LO(pe->phb->buid), option);
 548
 549        return ret;
 550}
 551
 552/**
 553 * pseries_eeh_get_state - Retrieve PE state
 554 * @pe: EEH PE
 555 * @delay: suggested time to wait if state is unavailable
 556 *
 557 * Retrieve the state of the specified PE. On RTAS compliant
 558 * pseries platform, there already has one dedicated RTAS function
 559 * for the purpose. It's notable that the associated PE config address
 560 * might be ready when calling the function. Therefore, endeavour to
 561 * use the PE config address if possible. Further more, there're 2
 562 * RTAS calls for the purpose, we need to try the new one and back
 563 * to the old one if the new one couldn't work properly.
 564 */
 565static int pseries_eeh_get_state(struct eeh_pe *pe, int *delay)
 566{
 567        int ret;
 568        int rets[4];
 569        int result;
 570
 571        if (ibm_read_slot_reset_state2 != RTAS_UNKNOWN_SERVICE) {
 572                ret = rtas_call(ibm_read_slot_reset_state2, 3, 4, rets,
 573                                pe->addr, BUID_HI(pe->phb->buid),
 574                                BUID_LO(pe->phb->buid));
 575        } else if (ibm_read_slot_reset_state != RTAS_UNKNOWN_SERVICE) {
 576                /* Fake PE unavailable info */
 577                rets[2] = 0;
 578                ret = rtas_call(ibm_read_slot_reset_state, 3, 3, rets,
 579                                pe->addr, BUID_HI(pe->phb->buid),
 580                                BUID_LO(pe->phb->buid));
 581        } else {
 582                return EEH_STATE_NOT_SUPPORT;
 583        }
 584
 585        if (ret)
 586                return ret;
 587
 588        /* Parse the result out */
 589        if (!rets[1])
 590                return EEH_STATE_NOT_SUPPORT;
 591
 592        switch(rets[0]) {
 593        case 0:
 594                result = EEH_STATE_MMIO_ACTIVE |
 595                         EEH_STATE_DMA_ACTIVE;
 596                break;
 597        case 1:
 598                result = EEH_STATE_RESET_ACTIVE |
 599                         EEH_STATE_MMIO_ACTIVE  |
 600                         EEH_STATE_DMA_ACTIVE;
 601                break;
 602        case 2:
 603                result = 0;
 604                break;
 605        case 4:
 606                result = EEH_STATE_MMIO_ENABLED;
 607                break;
 608        case 5:
 609                if (rets[2]) {
 610                        if (delay)
 611                                *delay = rets[2];
 612                        result = EEH_STATE_UNAVAILABLE;
 613                } else {
 614                        result = EEH_STATE_NOT_SUPPORT;
 615                }
 616                break;
 617        default:
 618                result = EEH_STATE_NOT_SUPPORT;
 619        }
 620
 621        return result;
 622}
 623
 624/**
 625 * pseries_eeh_reset - Reset the specified PE
 626 * @pe: EEH PE
 627 * @option: reset option
 628 *
 629 * Reset the specified PE
 630 */
 631static int pseries_eeh_reset(struct eeh_pe *pe, int option)
 632{
 633        return pseries_eeh_phb_reset(pe->phb, pe->addr, option);
 634}
 635
 636/**
 637 * pseries_eeh_get_log - Retrieve error log
 638 * @pe: EEH PE
 639 * @severity: temporary or permanent error log
 640 * @drv_log: driver log to be combined with retrieved error log
 641 * @len: length of driver log
 642 *
 643 * Retrieve the temporary or permanent error from the PE.
 644 * Actually, the error will be retrieved through the dedicated
 645 * RTAS call.
 646 */
 647static int pseries_eeh_get_log(struct eeh_pe *pe, int severity, char *drv_log, unsigned long len)
 648{
 649        unsigned long flags;
 650        int ret;
 651
 652        spin_lock_irqsave(&slot_errbuf_lock, flags);
 653        memset(slot_errbuf, 0, eeh_error_buf_size);
 654
 655        ret = rtas_call(ibm_slot_error_detail, 8, 1, NULL, pe->addr,
 656                        BUID_HI(pe->phb->buid), BUID_LO(pe->phb->buid),
 657                        virt_to_phys(drv_log), len,
 658                        virt_to_phys(slot_errbuf), eeh_error_buf_size,
 659                        severity);
 660        if (!ret)
 661                log_error(slot_errbuf, ERR_TYPE_RTAS_LOG, 0);
 662        spin_unlock_irqrestore(&slot_errbuf_lock, flags);
 663
 664        return ret;
 665}
 666
 667/**
 668 * pseries_eeh_configure_bridge - Configure PCI bridges in the indicated PE
 669 * @pe: EEH PE
 670 *
 671 */
 672static int pseries_eeh_configure_bridge(struct eeh_pe *pe)
 673{
 674        return pseries_eeh_phb_configure_bridge(pe->phb, pe->addr);
 675}
 676
 677/**
 678 * pseries_eeh_read_config - Read PCI config space
 679 * @edev: EEH device handle
 680 * @where: PCI config space offset
 681 * @size: size to read
 682 * @val: return value
 683 *
 684 * Read config space from the speicifed device
 685 */
 686static int pseries_eeh_read_config(struct eeh_dev *edev, int where, int size, u32 *val)
 687{
 688        struct pci_dn *pdn = eeh_dev_to_pdn(edev);
 689
 690        return rtas_read_config(pdn, where, size, val);
 691}
 692
 693/**
 694 * pseries_eeh_write_config - Write PCI config space
 695 * @edev: EEH device handle
 696 * @where: PCI config space offset
 697 * @size: size to write
 698 * @val: value to be written
 699 *
 700 * Write config space to the specified device
 701 */
 702static int pseries_eeh_write_config(struct eeh_dev *edev, int where, int size, u32 val)
 703{
 704        struct pci_dn *pdn = eeh_dev_to_pdn(edev);
 705
 706        return rtas_write_config(pdn, where, size, val);
 707}
 708
 709#ifdef CONFIG_PCI_IOV
 710static int pseries_send_allow_unfreeze(struct pci_dn *pdn, u16 *vf_pe_array, int cur_vfs)
 711{
 712        int rc;
 713        int ibm_allow_unfreeze = rtas_token("ibm,open-sriov-allow-unfreeze");
 714        unsigned long buid, addr;
 715
 716        addr = rtas_config_addr(pdn->busno, pdn->devfn, 0);
 717        buid = pdn->phb->buid;
 718        spin_lock(&rtas_data_buf_lock);
 719        memcpy(rtas_data_buf, vf_pe_array, RTAS_DATA_BUF_SIZE);
 720        rc = rtas_call(ibm_allow_unfreeze, 5, 1, NULL,
 721                       addr,
 722                       BUID_HI(buid),
 723                       BUID_LO(buid),
 724                       rtas_data_buf, cur_vfs * sizeof(u16));
 725        spin_unlock(&rtas_data_buf_lock);
 726        if (rc)
 727                pr_warn("%s: Failed to allow unfreeze for PHB#%x-PE#%lx, rc=%x\n",
 728                        __func__,
 729                        pdn->phb->global_number, addr, rc);
 730        return rc;
 731}
 732
 733static int pseries_call_allow_unfreeze(struct eeh_dev *edev)
 734{
 735        int cur_vfs = 0, rc = 0, vf_index, bus, devfn, vf_pe_num;
 736        struct pci_dn *pdn, *tmp, *parent, *physfn_pdn;
 737        u16 *vf_pe_array;
 738
 739        vf_pe_array = kzalloc(RTAS_DATA_BUF_SIZE, GFP_KERNEL);
 740        if (!vf_pe_array)
 741                return -ENOMEM;
 742        if (pci_num_vf(edev->physfn ? edev->physfn : edev->pdev)) {
 743                if (edev->pdev->is_physfn) {
 744                        cur_vfs = pci_num_vf(edev->pdev);
 745                        pdn = eeh_dev_to_pdn(edev);
 746                        parent = pdn->parent;
 747                        for (vf_index = 0; vf_index < cur_vfs; vf_index++)
 748                                vf_pe_array[vf_index] =
 749                                        cpu_to_be16(pdn->pe_num_map[vf_index]);
 750                        rc = pseries_send_allow_unfreeze(pdn, vf_pe_array,
 751                                                         cur_vfs);
 752                        pdn->last_allow_rc = rc;
 753                        for (vf_index = 0; vf_index < cur_vfs; vf_index++) {
 754                                list_for_each_entry_safe(pdn, tmp,
 755                                                         &parent->child_list,
 756                                                         list) {
 757                                        bus = pci_iov_virtfn_bus(edev->pdev,
 758                                                                 vf_index);
 759                                        devfn = pci_iov_virtfn_devfn(edev->pdev,
 760                                                                     vf_index);
 761                                        if (pdn->busno != bus ||
 762                                            pdn->devfn != devfn)
 763                                                continue;
 764                                        pdn->last_allow_rc = rc;
 765                                }
 766                        }
 767                } else {
 768                        pdn = pci_get_pdn(edev->pdev);
 769                        physfn_pdn = pci_get_pdn(edev->physfn);
 770
 771                        vf_pe_num = physfn_pdn->pe_num_map[edev->vf_index];
 772                        vf_pe_array[0] = cpu_to_be16(vf_pe_num);
 773                        rc = pseries_send_allow_unfreeze(physfn_pdn,
 774                                                         vf_pe_array, 1);
 775                        pdn->last_allow_rc = rc;
 776                }
 777        }
 778
 779        kfree(vf_pe_array);
 780        return rc;
 781}
 782
 783static int pseries_notify_resume(struct eeh_dev *edev)
 784{
 785        if (!edev)
 786                return -EEXIST;
 787
 788        if (rtas_token("ibm,open-sriov-allow-unfreeze") == RTAS_UNKNOWN_SERVICE)
 789                return -EINVAL;
 790
 791        if (edev->pdev->is_physfn || edev->pdev->is_virtfn)
 792                return pseries_call_allow_unfreeze(edev);
 793
 794        return 0;
 795}
 796#endif
 797
 798static struct eeh_ops pseries_eeh_ops = {
 799        .name                   = "pseries",
 800        .probe                  = pseries_eeh_probe,
 801        .set_option             = pseries_eeh_set_option,
 802        .get_state              = pseries_eeh_get_state,
 803        .reset                  = pseries_eeh_reset,
 804        .get_log                = pseries_eeh_get_log,
 805        .configure_bridge       = pseries_eeh_configure_bridge,
 806        .err_inject             = NULL,
 807        .read_config            = pseries_eeh_read_config,
 808        .write_config           = pseries_eeh_write_config,
 809        .next_error             = NULL,
 810        .restore_config         = NULL, /* NB: configure_bridge() does this */
 811#ifdef CONFIG_PCI_IOV
 812        .notify_resume          = pseries_notify_resume
 813#endif
 814};
 815
 816/**
 817 * eeh_pseries_init - Register platform dependent EEH operations
 818 *
 819 * EEH initialization on pseries platform. This function should be
 820 * called before any EEH related functions.
 821 */
 822static int __init eeh_pseries_init(void)
 823{
 824        struct pci_controller *phb;
 825        struct pci_dn *pdn;
 826        int ret, config_addr;
 827
 828        /* figure out EEH RTAS function call tokens */
 829        ibm_set_eeh_option              = rtas_token("ibm,set-eeh-option");
 830        ibm_set_slot_reset              = rtas_token("ibm,set-slot-reset");
 831        ibm_read_slot_reset_state2      = rtas_token("ibm,read-slot-reset-state2");
 832        ibm_read_slot_reset_state       = rtas_token("ibm,read-slot-reset-state");
 833        ibm_slot_error_detail           = rtas_token("ibm,slot-error-detail");
 834        ibm_get_config_addr_info2       = rtas_token("ibm,get-config-addr-info2");
 835        ibm_get_config_addr_info        = rtas_token("ibm,get-config-addr-info");
 836        ibm_configure_pe                = rtas_token("ibm,configure-pe");
 837
 838        /*
 839         * ibm,configure-pe and ibm,configure-bridge have the same semantics,
 840         * however ibm,configure-pe can be faster.  If we can't find
 841         * ibm,configure-pe then fall back to using ibm,configure-bridge.
 842         */
 843        if (ibm_configure_pe == RTAS_UNKNOWN_SERVICE)
 844                ibm_configure_pe        = rtas_token("ibm,configure-bridge");
 845
 846        /*
 847         * Necessary sanity check. We needn't check "get-config-addr-info"
 848         * and its variant since the old firmware probably support address
 849         * of domain/bus/slot/function for EEH RTAS operations.
 850         */
 851        if (ibm_set_eeh_option == RTAS_UNKNOWN_SERVICE          ||
 852            ibm_set_slot_reset == RTAS_UNKNOWN_SERVICE          ||
 853            (ibm_read_slot_reset_state2 == RTAS_UNKNOWN_SERVICE &&
 854             ibm_read_slot_reset_state == RTAS_UNKNOWN_SERVICE) ||
 855            ibm_slot_error_detail == RTAS_UNKNOWN_SERVICE       ||
 856            ibm_configure_pe == RTAS_UNKNOWN_SERVICE) {
 857                pr_info("EEH functionality not supported\n");
 858                return -EINVAL;
 859        }
 860
 861        /* Initialize error log lock and size */
 862        spin_lock_init(&slot_errbuf_lock);
 863        eeh_error_buf_size = rtas_token("rtas-error-log-max");
 864        if (eeh_error_buf_size == RTAS_UNKNOWN_SERVICE) {
 865                pr_info("%s: unknown EEH error log size\n",
 866                        __func__);
 867                eeh_error_buf_size = 1024;
 868        } else if (eeh_error_buf_size > RTAS_ERROR_LOG_MAX) {
 869                pr_info("%s: EEH error log size %d exceeds the maximal %d\n",
 870                        __func__, eeh_error_buf_size, RTAS_ERROR_LOG_MAX);
 871                eeh_error_buf_size = RTAS_ERROR_LOG_MAX;
 872        }
 873
 874        /* Set EEH probe mode */
 875        eeh_add_flag(EEH_PROBE_MODE_DEVTREE | EEH_ENABLE_IO_FOR_LOG);
 876
 877        /* Set EEH machine dependent code */
 878        ppc_md.pcibios_bus_add_device = pseries_pcibios_bus_add_device;
 879
 880        if (is_kdump_kernel() || reset_devices) {
 881                pr_info("Issue PHB reset ...\n");
 882                list_for_each_entry(phb, &hose_list, list_node) {
 883                        // Skip if the slot is empty
 884                        if (list_empty(&PCI_DN(phb->dn)->child_list))
 885                                continue;
 886
 887                        pdn = list_first_entry(&PCI_DN(phb->dn)->child_list, struct pci_dn, list);
 888                        config_addr = pseries_eeh_get_pe_config_addr(pdn);
 889
 890                        /* invalid PE config addr */
 891                        if (config_addr < 0)
 892                                continue;
 893
 894                        pseries_eeh_phb_reset(phb, config_addr, EEH_RESET_FUNDAMENTAL);
 895                        pseries_eeh_phb_reset(phb, config_addr, EEH_RESET_DEACTIVATE);
 896                        pseries_eeh_phb_configure_bridge(phb, config_addr);
 897                }
 898        }
 899
 900        ret = eeh_init(&pseries_eeh_ops);
 901        if (!ret)
 902                pr_info("EEH: pSeries platform initialized\n");
 903        else
 904                pr_info("EEH: pSeries platform initialization failure (%d)\n",
 905                        ret);
 906        return ret;
 907}
 908machine_arch_initcall(pseries, eeh_pseries_init);
 909