powerpc/eeh: Don't check RTAS token to get PE addr
[linux-3.10.git] / arch / powerpc / platforms / pseries / eeh_pseries.c
index 39567b2..b456b15 100644 (file)
@@ -56,6 +56,15 @@ static int ibm_get_config_addr_info2;
 static int ibm_configure_bridge;
 static int ibm_configure_pe;
 
+/*
+ * Buffer for reporting slot-error-detail rtas calls. Its here
+ * in BSS, and not dynamically alloced, so that it ends up in
+ * RMO where RTAS can access it.
+ */
+static unsigned char slot_errbuf[RTAS_ERROR_LOG_MAX];
+static DEFINE_SPINLOCK(slot_errbuf_lock);
+static int eeh_error_buf_size;
+
 /**
  * pseries_eeh_init - EEH platform dependent initialization
  *
@@ -72,15 +81,19 @@ static int pseries_eeh_init(void)
        ibm_get_config_addr_info2       = rtas_token("ibm,get-config-addr-info2");
        ibm_get_config_addr_info        = rtas_token("ibm,get-config-addr-info");
        ibm_configure_pe                = rtas_token("ibm,configure-pe");
-       ibm_configure_bridge            = rtas_token ("ibm,configure-bridge");
+       ibm_configure_bridge            = rtas_token("ibm,configure-bridge");
 
-       /* necessary sanity check */
+       /*
+        * Necessary sanity check. We needn't check "get-config-addr-info"
+        * and its variant since the old firmware probably support address
+        * of domain/bus/slot/function for EEH RTAS operations.
+        */
        if (ibm_set_eeh_option == RTAS_UNKNOWN_SERVICE) {
                pr_warning("%s: RTAS service <ibm,set-eeh-option> invalid\n",
                        __func__);
                return -EINVAL;
        } else if (ibm_set_slot_reset == RTAS_UNKNOWN_SERVICE) {
-               pr_warning("%s: RTAS service <ibm, set-slot-reset> invalid\n",
+               pr_warning("%s: RTAS service <ibm,set-slot-reset> invalid\n",
                        __func__);
                return -EINVAL;
        } else if (ibm_read_slot_reset_state2 == RTAS_UNKNOWN_SERVICE &&
@@ -93,12 +106,6 @@ static int pseries_eeh_init(void)
                pr_warning("%s: RTAS service <ibm,slot-error-detail> invalid\n",
                        __func__);
                return -EINVAL;
-       } else if (ibm_get_config_addr_info2 == RTAS_UNKNOWN_SERVICE &&
-                  ibm_get_config_addr_info == RTAS_UNKNOWN_SERVICE) {
-               pr_warning("%s: RTAS service <ibm,get-config-addr-info2> and "
-                       "<ibm,get-config-addr-info> invalid\n",
-                       __func__);
-               return -EINVAL;
        } else if (ibm_configure_pe == RTAS_UNKNOWN_SERVICE &&
                   ibm_configure_bridge == RTAS_UNKNOWN_SERVICE) {
                pr_warning("%s: RTAS service <ibm,configure-pe> and "
@@ -107,27 +114,130 @@ static int pseries_eeh_init(void)
                return -EINVAL;
        }
 
+       /* Initialize error log lock and size */
+       spin_lock_init(&slot_errbuf_lock);
+       eeh_error_buf_size = rtas_token("rtas-error-log-max");
+       if (eeh_error_buf_size == RTAS_UNKNOWN_SERVICE) {
+               pr_warning("%s: unknown EEH error log size\n",
+                       __func__);
+               eeh_error_buf_size = 1024;
+       } else if (eeh_error_buf_size > RTAS_ERROR_LOG_MAX) {
+               pr_warning("%s: EEH error log size %d exceeds the maximal %d\n",
+                       __func__, eeh_error_buf_size, RTAS_ERROR_LOG_MAX);
+               eeh_error_buf_size = RTAS_ERROR_LOG_MAX;
+       }
+
+       /* Set EEH probe mode */
+       eeh_probe_mode_set(EEH_PROBE_MODE_DEVTREE);
+
        return 0;
 }
 
 /**
+ * pseries_eeh_of_probe - EEH probe on the given device
+ * @dn: OF node
+ * @flag: Unused
+ *
+ * When EEH module is installed during system boot, all PCI devices
+ * are checked one by one to see if it supports EEH. The function
+ * is introduced for the purpose.
+ */
+static void *pseries_eeh_of_probe(struct device_node *dn, void *flag)
+{
+       struct eeh_dev *edev;
+       struct eeh_pe pe;
+       const u32 *class_code, *vendor_id, *device_id;
+       const u32 *regs;
+       int enable = 0;
+       int ret;
+
+       /* Retrieve OF node and eeh device */
+       edev = of_node_to_eeh_dev(dn);
+       if (!of_device_is_available(dn))
+               return NULL;
+
+       /* Retrieve class/vendor/device IDs */
+       class_code = of_get_property(dn, "class-code", NULL);
+       vendor_id  = of_get_property(dn, "vendor-id", NULL);
+       device_id  = of_get_property(dn, "device-id", NULL);
+
+       /* Skip for bad OF node or PCI-ISA bridge */
+       if (!class_code || !vendor_id || !device_id)
+               return NULL;
+       if (dn->type && !strcmp(dn->type, "isa"))
+               return NULL;
+
+       /* Update class code and mode of eeh device */
+       edev->class_code = *class_code;
+       edev->mode = 0;
+
+       /* Retrieve the device address */
+       regs = of_get_property(dn, "reg", NULL);
+       if (!regs) {
+               pr_warning("%s: OF node property %s::reg not found\n",
+                       __func__, dn->full_name);
+               return NULL;
+       }
+
+       /* Initialize the fake PE */
+       memset(&pe, 0, sizeof(struct eeh_pe));
+       pe.phb = edev->phb;
+       pe.config_addr = regs[0];
+
+       /* Enable EEH on the device */
+       ret = eeh_ops->set_option(&pe, EEH_OPT_ENABLE);
+       if (!ret) {
+               edev->config_addr = regs[0];
+               /* Retrieve PE address */
+               edev->pe_config_addr = eeh_ops->get_pe_addr(&pe);
+               pe.addr = edev->pe_config_addr;
+
+               /* Some older systems (Power4) allow the ibm,set-eeh-option
+                * call to succeed even on nodes where EEH is not supported.
+                * Verify support explicitly.
+                */
+               ret = eeh_ops->get_state(&pe, NULL);
+               if (ret > 0 && ret != EEH_STATE_NOT_SUPPORT)
+                       enable = 1;
+
+               if (enable) {
+                       eeh_subsystem_enabled = 1;
+                       eeh_add_to_parent_pe(edev);
+
+                       pr_debug("%s: EEH enabled on %s PHB#%d-PE#%x, config addr#%x\n",
+                               __func__, dn->full_name, pe.phb->global_number,
+                               pe.addr, pe.config_addr);
+               } else if (dn->parent && of_node_to_eeh_dev(dn->parent) &&
+                          (of_node_to_eeh_dev(dn->parent))->pe) {
+                       /* This device doesn't support EEH, but it may have an
+                        * EEH parent, in which case we mark it as supported.
+                        */
+                       edev->config_addr = of_node_to_eeh_dev(dn->parent)->config_addr;
+                       edev->pe_config_addr = of_node_to_eeh_dev(dn->parent)->pe_config_addr;
+                       eeh_add_to_parent_pe(edev);
+               }
+       }
+
+       /* Save memory bars */
+       eeh_save_bars(edev);
+
+       return NULL;
+}
+
+/**
  * pseries_eeh_set_option - Initialize EEH or MMIO/DMA reenable
- * @dn: device node
+ * @pe: EEH PE
  * @option: operation to be issued
  *
  * The function is used to control the EEH functionality globally.
  * Currently, following options are support according to PAPR:
  * Enable EEH, Disable EEH, Enable MMIO and Enable DMA
  */
-static int pseries_eeh_set_option(struct device_node *dn, int option)
+static int pseries_eeh_set_option(struct eeh_pe *pe, int option)
 {
        int ret = 0;
-       struct pci_dn *pdn;
-       const u32 *reg;
        int config_addr;
 
-       pdn = PCI_DN(dn);
-
        /*
         * When we're enabling or disabling EEH functioality on
         * the particular PE, the PE config address is possibly
@@ -137,15 +247,11 @@ static int pseries_eeh_set_option(struct device_node *dn, int option)
        switch (option) {
        case EEH_OPT_DISABLE:
        case EEH_OPT_ENABLE:
-               reg = of_get_property(dn, "reg", NULL);
-               config_addr = reg[0];
-               break;
-
        case EEH_OPT_THAW_MMIO:
        case EEH_OPT_THAW_DMA:
-               config_addr = pdn->eeh_config_addr;
-               if (pdn->eeh_pe_config_addr)
-                       config_addr = pdn->eeh_pe_config_addr;
+               config_addr = pe->config_addr;
+               if (pe->addr)
+                       config_addr = pe->addr;
                break;
 
        default:
@@ -155,15 +261,15 @@ static int pseries_eeh_set_option(struct device_node *dn, int option)
        }
 
        ret = rtas_call(ibm_set_eeh_option, 4, 1, NULL,
-                       config_addr, BUID_HI(pdn->phb->buid),
-                       BUID_LO(pdn->phb->buid), option);
+                       config_addr, BUID_HI(pe->phb->buid),
+                       BUID_LO(pe->phb->buid), option);
 
        return ret;
 }
 
 /**
  * pseries_eeh_get_pe_addr - Retrieve PE address
- * @dn: device node
+ * @pe: EEH PE
  *
  * Retrieve the assocated PE address. Actually, there're 2 RTAS
  * function calls dedicated for the purpose. We need implement
@@ -174,14 +280,11 @@ static int pseries_eeh_set_option(struct device_node *dn, int option)
  * It's notable that zero'ed return value means invalid PE config
  * address.
  */
-static int pseries_eeh_get_pe_addr(struct device_node *dn)
+static int pseries_eeh_get_pe_addr(struct eeh_pe *pe)
 {
-       struct pci_dn *pdn;
        int ret = 0;
        int rets[3];
 
-       pdn = PCI_DN(dn);
-
        if (ibm_get_config_addr_info2 != RTAS_UNKNOWN_SERVICE) {
                /*
                 * First of all, we need to make sure there has one PE
@@ -189,18 +292,18 @@ static int pseries_eeh_get_pe_addr(struct device_node *dn)
                 * meaningless.
                 */
                ret = rtas_call(ibm_get_config_addr_info2, 4, 2, rets,
-                               pdn->eeh_config_addr, BUID_HI(pdn->phb->buid),
-                               BUID_LO(pdn->phb->buid), 1);
+                               pe->config_addr, BUID_HI(pe->phb->buid),
+                               BUID_LO(pe->phb->buid), 1);
                if (ret || (rets[0] == 0))
                        return 0;
 
                /* Retrieve the associated PE config address */
                ret = rtas_call(ibm_get_config_addr_info2, 4, 2, rets,
-                               pdn->eeh_config_addr, BUID_HI(pdn->phb->buid),
-                               BUID_LO(pdn->phb->buid), 0);
+                               pe->config_addr, BUID_HI(pe->phb->buid),
+                               BUID_LO(pe->phb->buid), 0);
                if (ret) {
-                       pr_warning("%s: Failed to get PE address for %s\n",
-                               __func__, dn->full_name);
+                       pr_warning("%s: Failed to get address for PHB#%d-PE#%x\n",
+                               __func__, pe->phb->global_number, pe->config_addr);
                        return 0;
                }
 
@@ -209,11 +312,11 @@ static int pseries_eeh_get_pe_addr(struct device_node *dn)
 
        if (ibm_get_config_addr_info != RTAS_UNKNOWN_SERVICE) {
                ret = rtas_call(ibm_get_config_addr_info, 4, 2, rets,
-                               pdn->eeh_config_addr, BUID_HI(pdn->phb->buid),
-                               BUID_LO(pdn->phb->buid), 0);
+                               pe->config_addr, BUID_HI(pe->phb->buid),
+                               BUID_LO(pe->phb->buid), 0);
                if (ret) {
-                       pr_warning("%s: Failed to get PE address for %s\n",
-                               __func__, dn->full_name);
+                       pr_warning("%s: Failed to get address for PHB#%d-PE#%x\n",
+                               __func__, pe->phb->global_number, pe->config_addr);
                        return 0;
                }
 
@@ -225,7 +328,7 @@ static int pseries_eeh_get_pe_addr(struct device_node *dn)
 
 /**
  * pseries_eeh_get_state - Retrieve PE state
- * @dn: PE associated device node
+ * @pe: EEH PE
  * @state: return value
  *
  * Retrieve the state of the specified PE. On RTAS compliant
@@ -236,30 +339,28 @@ static int pseries_eeh_get_pe_addr(struct device_node *dn)
  * RTAS calls for the purpose, we need to try the new one and back
  * to the old one if the new one couldn't work properly.
  */
-static int pseries_eeh_get_state(struct device_node *dn, int *state)
+static int pseries_eeh_get_state(struct eeh_pe *pe, int *state)
 {
-       struct pci_dn *pdn;
        int config_addr;
        int ret;
        int rets[4];
        int result;
 
        /* Figure out PE config address if possible */
-       pdn = PCI_DN(dn);
-       config_addr = pdn->eeh_config_addr;
-       if (pdn->eeh_pe_config_addr)
-               config_addr = pdn->eeh_pe_config_addr;
+       config_addr = pe->config_addr;
+       if (pe->addr)
+               config_addr = pe->addr;
 
        if (ibm_read_slot_reset_state2 != RTAS_UNKNOWN_SERVICE) {
                ret = rtas_call(ibm_read_slot_reset_state2, 3, 4, rets,
-                               config_addr, BUID_HI(pdn->phb->buid),
-                               BUID_LO(pdn->phb->buid));
+                               config_addr, BUID_HI(pe->phb->buid),
+                               BUID_LO(pe->phb->buid));
        } else if (ibm_read_slot_reset_state != RTAS_UNKNOWN_SERVICE) {
                /* Fake PE unavailable info */
                rets[2] = 0;
                ret = rtas_call(ibm_read_slot_reset_state, 3, 3, rets,
-                               config_addr, BUID_HI(pdn->phb->buid),
-                               BUID_LO(pdn->phb->buid));
+                               config_addr, BUID_HI(pe->phb->buid),
+                               BUID_LO(pe->phb->buid));
        } else {
                return EEH_STATE_NOT_SUPPORT;
        }
@@ -311,32 +412,98 @@ static int pseries_eeh_get_state(struct device_node *dn, int *state)
 
 /**
  * pseries_eeh_reset - Reset the specified PE
- * @dn: PE associated device node
+ * @pe: EEH PE
  * @option: reset option
  *
  * Reset the specified PE
  */
-static int pseries_eeh_reset(struct device_node *dn, int option)
+static int pseries_eeh_reset(struct eeh_pe *pe, int option)
 {
-       return 0;
+       int config_addr;
+       int ret;
+
+       /* Figure out PE address */
+       config_addr = pe->config_addr;
+       if (pe->addr)
+               config_addr = pe->addr;
+
+       /* Reset PE through RTAS call */
+       ret = rtas_call(ibm_set_slot_reset, 4, 1, NULL,
+                       config_addr, BUID_HI(pe->phb->buid),
+                       BUID_LO(pe->phb->buid), option);
+
+       /* If fundamental-reset not supported, try hot-reset */
+       if (option == EEH_RESET_FUNDAMENTAL &&
+           ret == -8) {
+               ret = rtas_call(ibm_set_slot_reset, 4, 1, NULL,
+                               config_addr, BUID_HI(pe->phb->buid),
+                               BUID_LO(pe->phb->buid), EEH_RESET_HOT);
+       }
+
+       return ret;
 }
 
 /**
  * pseries_eeh_wait_state - Wait for PE state
- * @dn: PE associated device node
+ * @pe: EEH PE
  * @max_wait: maximal period in microsecond
  *
  * Wait for the state of associated PE. It might take some time
  * to retrieve the PE's state.
  */
-static int pseries_eeh_wait_state(struct device_node *dn, int max_wait)
+static int pseries_eeh_wait_state(struct eeh_pe *pe, int max_wait)
 {
-       return 0;
+       int ret;
+       int mwait;
+
+       /*
+        * According to PAPR, the state of PE might be temporarily
+        * unavailable. Under the circumstance, we have to wait
+        * for indicated time determined by firmware. The maximal
+        * wait time is 5 minutes, which is acquired from the original
+        * EEH implementation. Also, the original implementation
+        * also defined the minimal wait time as 1 second.
+        */
+#define EEH_STATE_MIN_WAIT_TIME        (1000)
+#define EEH_STATE_MAX_WAIT_TIME        (300 * 1000)
+
+       while (1) {
+               ret = pseries_eeh_get_state(pe, &mwait);
+
+               /*
+                * If the PE's state is temporarily unavailable,
+                * we have to wait for the specified time. Otherwise,
+                * the PE's state will be returned immediately.
+                */
+               if (ret != EEH_STATE_UNAVAILABLE)
+                       return ret;
+
+               if (max_wait <= 0) {
+                       pr_warning("%s: Timeout when getting PE's state (%d)\n",
+                               __func__, max_wait);
+                       return EEH_STATE_NOT_SUPPORT;
+               }
+
+               if (mwait <= 0) {
+                       pr_warning("%s: Firmware returned bad wait value %d\n",
+                               __func__, mwait);
+                       mwait = EEH_STATE_MIN_WAIT_TIME;
+               } else if (mwait > EEH_STATE_MAX_WAIT_TIME) {
+                       pr_warning("%s: Firmware returned too long wait value %d\n",
+                               __func__, mwait);
+                       mwait = EEH_STATE_MAX_WAIT_TIME;
+               }
+
+               max_wait -= mwait;
+               msleep(mwait);
+       }
+
+       return EEH_STATE_NOT_SUPPORT;
 }
 
 /**
  * pseries_eeh_get_log - Retrieve error log
- * @dn: device node
+ * @pe: EEH PE
  * @severity: temporary or permanent error log
  * @drv_log: driver log to be combined with retrieved error log
  * @len: length of driver log
@@ -345,34 +512,120 @@ static int pseries_eeh_wait_state(struct device_node *dn, int max_wait)
  * Actually, the error will be retrieved through the dedicated
  * RTAS call.
  */
-static int pseries_eeh_get_log(struct device_node *dn, int severity, char *drv_log, unsigned long len)
+static int pseries_eeh_get_log(struct eeh_pe *pe, int severity, char *drv_log, unsigned long len)
 {
-       return 0;
+       int config_addr;
+       unsigned long flags;
+       int ret;
+
+       spin_lock_irqsave(&slot_errbuf_lock, flags);
+       memset(slot_errbuf, 0, eeh_error_buf_size);
+
+       /* Figure out the PE address */
+       config_addr = pe->config_addr;
+       if (pe->addr)
+               config_addr = pe->addr;
+
+       ret = rtas_call(ibm_slot_error_detail, 8, 1, NULL, config_addr,
+                       BUID_HI(pe->phb->buid), BUID_LO(pe->phb->buid),
+                       virt_to_phys(drv_log), len,
+                       virt_to_phys(slot_errbuf), eeh_error_buf_size,
+                       severity);
+       if (!ret)
+               log_error(slot_errbuf, ERR_TYPE_RTAS_LOG, 0);
+       spin_unlock_irqrestore(&slot_errbuf_lock, flags);
+
+       return ret;
 }
 
 /**
  * pseries_eeh_configure_bridge - Configure PCI bridges in the indicated PE
- * @dn: PE associated device node
+ * @pe: EEH PE
  *
  * The function will be called to reconfigure the bridges included
  * in the specified PE so that the mulfunctional PE would be recovered
  * again.
  */
-static int pseries_eeh_configure_bridge(struct device_node *dn)
+static int pseries_eeh_configure_bridge(struct eeh_pe *pe)
 {
-       return 0;
+       int config_addr;
+       int ret;
+
+       /* Figure out the PE address */
+       config_addr = pe->config_addr;
+       if (pe->addr)
+               config_addr = pe->addr;
+
+       /* Use new configure-pe function, if supported */
+       if (ibm_configure_pe != RTAS_UNKNOWN_SERVICE) {
+               ret = rtas_call(ibm_configure_pe, 3, 1, NULL,
+                               config_addr, BUID_HI(pe->phb->buid),
+                               BUID_LO(pe->phb->buid));
+       } else if (ibm_configure_bridge != RTAS_UNKNOWN_SERVICE) {
+               ret = rtas_call(ibm_configure_bridge, 3, 1, NULL,
+                               config_addr, BUID_HI(pe->phb->buid),
+                               BUID_LO(pe->phb->buid));
+       } else {
+               return -EFAULT;
+       }
+
+       if (ret)
+               pr_warning("%s: Unable to configure bridge PHB#%d-PE#%x (%d)\n",
+                       __func__, pe->phb->global_number, pe->addr, ret);
+
+       return ret;
+}
+
+/**
+ * pseries_eeh_read_config - Read PCI config space
+ * @dn: device node
+ * @where: PCI address
+ * @size: size to read
+ * @val: return value
+ *
+ * Read config space from the speicifed device
+ */
+static int pseries_eeh_read_config(struct device_node *dn, int where, int size, u32 *val)
+{
+       struct pci_dn *pdn;
+
+       pdn = PCI_DN(dn);
+
+       return rtas_read_config(pdn, where, size, val);
+}
+
+/**
+ * pseries_eeh_write_config - Write PCI config space
+ * @dn: device node
+ * @where: PCI address
+ * @size: size to write
+ * @val: value to be written
+ *
+ * Write config space to the specified device
+ */
+static int pseries_eeh_write_config(struct device_node *dn, int where, int size, u32 val)
+{
+       struct pci_dn *pdn;
+
+       pdn = PCI_DN(dn);
+
+       return rtas_write_config(pdn, where, size, val);
 }
 
 static struct eeh_ops pseries_eeh_ops = {
        .name                   = "pseries",
        .init                   = pseries_eeh_init,
+       .of_probe               = pseries_eeh_of_probe,
+       .dev_probe              = NULL,
        .set_option             = pseries_eeh_set_option,
        .get_pe_addr            = pseries_eeh_get_pe_addr,
        .get_state              = pseries_eeh_get_state,
        .reset                  = pseries_eeh_reset,
        .wait_state             = pseries_eeh_wait_state,
        .get_log                = pseries_eeh_get_log,
-       .configure_bridge       = pseries_eeh_configure_bridge
+       .configure_bridge       = pseries_eeh_configure_bridge,
+       .read_config            = pseries_eeh_read_config,
+       .write_config           = pseries_eeh_write_config
 };
 
 /**
@@ -381,7 +634,21 @@ static struct eeh_ops pseries_eeh_ops = {
  * EEH initialization on pseries platform. This function should be
  * called before any EEH related functions.
  */
-int __init eeh_pseries_init(void)
+static int __init eeh_pseries_init(void)
 {
-       return eeh_ops_register(&pseries_eeh_ops);
+       int ret = -EINVAL;
+
+       if (!machine_is(pseries))
+               return ret;
+
+       ret = eeh_ops_register(&pseries_eeh_ops);
+       if (!ret)
+               pr_info("EEH: pSeries platform initialized\n");
+       else
+               pr_info("EEH: pSeries platform initialization failure (%d)\n",
+                       ret);
+
+       return ret;
 }
+
+early_initcall(eeh_pseries_init);