Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / drivers / pci / hotplug / rpaphp_core.c
index 2fe394a..6380045 100644 (file)
@@ -54,37 +54,7 @@ MODULE_AUTHOR(DRIVER_AUTHOR);
 MODULE_DESCRIPTION(DRIVER_DESC);
 MODULE_LICENSE("GPL");
 
-module_param(debug, int, 0644);
-
-static int enable_slot(struct hotplug_slot *slot);
-static int disable_slot(struct hotplug_slot *slot);
-static int set_attention_status(struct hotplug_slot *slot, u8 value);
-static int get_power_status(struct hotplug_slot *slot, u8 * value);
-static int get_attention_status(struct hotplug_slot *slot, u8 * value);
-static int get_adapter_status(struct hotplug_slot *slot, u8 * value);
-static int get_max_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value);
-static int get_cur_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value);
-
-struct hotplug_slot_ops rpaphp_hotplug_slot_ops = {
-       .owner = THIS_MODULE,
-       .enable_slot = enable_slot,
-       .disable_slot = disable_slot,
-       .set_attention_status = set_attention_status,
-       .get_power_status = get_power_status,
-       .get_attention_status = get_attention_status,
-       .get_adapter_status = get_adapter_status,
-       .get_max_bus_speed = get_max_bus_speed,
-       .get_cur_bus_speed = get_cur_bus_speed,
-};
-
-static inline struct slot *get_slot (struct hotplug_slot *hotplug_slot, const char *function)
-{
-       if (!hotplug_slot) {
-               dbg("%s - hotplug_slot == NULL\n", function);
-               return NULL;
-       }
-       return (struct slot *)hotplug_slot->private;
-}
+module_param(debug, bool, 0644);
 
 static int rpaphp_get_attention_status(struct slot *slot)
 {
@@ -101,10 +71,7 @@ static int rpaphp_get_attention_status(struct slot *slot)
 static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 value)
 {
        int retval = 0;
-       struct slot *slot = get_slot(hotplug_slot, __FUNCTION__);
-
-       if (slot == NULL)
-               return -ENODEV;
+       struct slot *slot = (struct slot *)hotplug_slot->private;
 
        down(&rpaphp_sem);
        switch (value) {
@@ -136,10 +103,7 @@ static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 value)
 static int get_power_status(struct hotplug_slot *hotplug_slot, u8 * value)
 {
        int retval;
-       struct slot *slot = get_slot(hotplug_slot, __FUNCTION__);
-
-       if (slot == NULL)
-               return -ENODEV;
+       struct slot *slot = (struct slot *)hotplug_slot->private;
 
        down(&rpaphp_sem);
        retval = rpaphp_get_power_status(slot, value);
@@ -155,10 +119,7 @@ static int get_power_status(struct hotplug_slot *hotplug_slot, u8 * value)
 static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 * value)
 {
        int retval = 0;
-       struct slot *slot = get_slot(hotplug_slot, __FUNCTION__);
-
-       if (slot == NULL)
-               return -ENODEV;
+       struct slot *slot = (struct slot *)hotplug_slot->private;
 
        down(&rpaphp_sem);
        *value = rpaphp_get_attention_status(slot);
@@ -168,33 +129,18 @@ static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 * value)
 
 static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 * value)
 {
-       struct slot *slot = get_slot(hotplug_slot, __FUNCTION__);
+       struct slot *slot = (struct slot *)hotplug_slot->private;
        int retval = 0;
 
-       if (slot == NULL)
-               return -ENODEV;
        down(&rpaphp_sem);
-       /*  have to go through this */
-       switch (slot->dev_type) {
-       case PCI_DEV:
-               retval = rpaphp_get_pci_adapter_status(slot, 0, value);
-               break;
-       case VIO_DEV:
-               retval = rpaphp_get_vio_adapter_status(slot, 0, value);
-               break;
-       default:
-               retval = -EINVAL;
-       }
+       retval = rpaphp_get_pci_adapter_status(slot, 0, value);
        up(&rpaphp_sem);
        return retval;
 }
 
 static int get_max_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
 {
-       struct slot *slot = get_slot(hotplug_slot, __FUNCTION__);
-
-       if (slot == NULL)
-               return -ENODEV;
+       struct slot *slot = (struct slot *)hotplug_slot->private;
 
        down(&rpaphp_sem);
        switch (slot->type) {
@@ -231,72 +177,121 @@ static int get_max_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_spe
        return 0;
 }
 
-/* return dummy value because not sure if PRA provides any method... */
-static int get_cur_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
+static int get_children_props(struct device_node *dn, int **drc_indexes,
+               int **drc_names, int **drc_types, int **drc_power_domains)
 {
-       struct slot *slot = get_slot(hotplug_slot, __FUNCTION__);
+       int *indexes, *names;
+       int *types, *domains;
 
-       if (slot == NULL)
-               return -ENODEV;
+       indexes = (int *) get_property(dn, "ibm,drc-indexes", NULL);
+       names = (int *) get_property(dn, "ibm,drc-names", NULL);
+       types = (int *) get_property(dn, "ibm,drc-types", NULL);
+       domains = (int *) get_property(dn, "ibm,drc-power-domains", NULL);
+
+       if (!indexes || !names || !types || !domains) {
+               /* Slot does not have dynamically-removable children */
+               return -EINVAL;
+       }
+       if (drc_indexes)
+               *drc_indexes = indexes;
+       if (drc_names)
+               /* &drc_names[1] contains NULL terminated slot names */
+               *drc_names = names;
+       if (drc_types)
+               /* &drc_types[1] contains NULL terminated slot types */
+               *drc_types = types;
+       if (drc_power_domains)
+               *drc_power_domains = domains;
 
-       *value = PCI_SPEED_UNKNOWN;
        return 0;
 }
 
-int rpaphp_remove_slot(struct slot *slot)
+/* To get the DRC props describing the current node, first obtain it's
+ * my-drc-index property.  Next obtain the DRC list from it's parent.  Use
+ * the my-drc-index for correlation, and obtain the requested properties.
+ */
+int rpaphp_get_drc_props(struct device_node *dn, int *drc_index,
+               char **drc_name, char **drc_type, int *drc_power_domain)
 {
-       int retval = 0;
-       struct hotplug_slot *php_slot = slot->hotplug_slot;
-
-       list_del(&slot->rpaphp_slot_list);
-       
-       /* remove "php_location" file */
-       rpaphp_sysfs_remove_attr_location(php_slot);
+       int *indexes, *names;
+       int *types, *domains;
+       unsigned int *my_index;
+       char *name_tmp, *type_tmp;
+       int i, rc;
+
+       my_index = (int *) get_property(dn, "ibm,my-drc-index", NULL);
+       if (!my_index) {
+               /* Node isn't DLPAR/hotplug capable */
+               return -EINVAL;
+       }
 
-       retval = pci_hp_deregister(php_slot);
-       if (retval)
-               err("Problem unregistering a slot %s\n", slot->name);
+       rc = get_children_props(dn->parent, &indexes, &names, &types, &domains);
+       if (rc < 0) {
+               return -EINVAL;
+       }
 
-       num_slots--;
+       name_tmp = (char *) &names[1];
+       type_tmp = (char *) &types[1];
+
+       /* Iterate through parent properties, looking for my-drc-index */
+       for (i = 0; i < indexes[0]; i++) {
+               if ((unsigned int) indexes[i + 1] == *my_index) {
+                       if (drc_name)
+                               *drc_name = name_tmp;
+                       if (drc_type)
+                               *drc_type = type_tmp;
+                       if (drc_index)
+                               *drc_index = *my_index;
+                       if (drc_power_domain)
+                               *drc_power_domain = domains[i+1];
+                       return 0;
+               }
+               name_tmp += (strlen(name_tmp) + 1);
+               type_tmp += (strlen(type_tmp) + 1);
+       }
 
-       dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
-       return retval;
+       return -EINVAL;
 }
 
-static int is_php_dn(struct device_node *dn, int **indexes, int **names, int **types,
-         int **power_domains)
+static int is_php_type(char *drc_type)
 {
-       *indexes = (int *) get_property(dn, "ibm,drc-indexes", NULL);
-       if (!*indexes)
-               return (0);
-       /* &names[1] contains NULL terminated slot names */
-       *names = (int *) get_property(dn, "ibm,drc-names", NULL);
-       if (!*names)
-               return (0);
-       /* &types[1] contains NULL terminated slot types */
-       *types = (int *) get_property(dn, "ibm,drc-types", NULL);
-       if (!*types)
-               return (0);
-       /* power_domains[1...n] are the slot power domains */
-       *power_domains = (int *) get_property(dn,
-                                             "ibm,drc-power-domains", NULL);
-       if (!*power_domains)
-               return (0);
-       if (strcmp(dn->name, "pci") == 0 &&
-           !get_property(dn, "ibm,fw-pci-hot-plug-ctrl", NULL))
-               return (0);
-       return (1);
+       unsigned long value;
+       char *endptr;
+
+       /* PCI Hotplug nodes have an integer for drc_type */
+       value = simple_strtoul(drc_type, &endptr, 10);
+       if (endptr == drc_type)
+               return 0;
+
+       return 1;
 }
 
-static inline int is_vdevice_root(struct device_node *dn)
+static int is_php_dn(struct device_node *dn, int **indexes, int **names,
+               int **types, int **power_domains)
 {
-       return !strcmp(dn->name, "vdevice");
+       int *drc_types;
+       int rc;
+
+       rc = get_children_props(dn, indexes, names, &drc_types, power_domains);
+       if (rc >= 0) {
+               if (is_php_type((char *) &drc_types[1])) {
+                       *types = drc_types;
+                       return 1;
+               }
+       }
+
+       return 0;
 }
 
-/*************************************
- * Add  Hot Plug slot(s) to sysfs
+/**
+ * rpaphp_add_slot -- add hotplug or dlpar slot
  *
- ************************************/
+ *     rpaphp not only registers PCI hotplug slots(HOTPLUG), 
+ *     but also logical DR slots(EMBEDDED).
+ *     HOTPLUG slot: An adapter can be physically added/removed. 
+ *     EMBEDDED slot: An adapter can be logically removed/added
+ *               from/to a partition with the slot.
+ */
 int rpaphp_add_slot(struct device_node *dn)
 {
        struct slot *slot;
@@ -307,65 +302,36 @@ int rpaphp_add_slot(struct device_node *dn)
 
        dbg("Entry %s: dn->full_name=%s\n", __FUNCTION__, dn->full_name);
 
-       if (dn->parent && is_vdevice_root(dn->parent)) {
-               /* register a VIO device */
-               retval = register_vio_slot(dn);
-               goto exit;
-       }
-
        /* register PCI devices */
-       if (dn->name != 0 && strcmp(dn->name, "pci") == 0 &&
-           is_php_dn(dn, &indexes, &names, &types, &power_domains)) {
+       if (dn->name != 0 && strcmp(dn->name, "pci") == 0) {
+               if (!is_php_dn(dn, &indexes, &names, &types, &power_domains))
+                       goto exit;
 
                name = (char *) &names[1];
                type = (char *) &types[1];
-               for (i = 0; i < indexes[0];
-                    i++,
-                    name += (strlen(name) + 1), type += (strlen(type) + 1)) {
+               for (i = 0; i < indexes[0]; i++,
+                       name += (strlen(name) + 1), type += (strlen(type) + 1))                 {
+
                        if (!(slot = alloc_slot_struct(dn, indexes[i + 1], name,
-                                                      power_domains[i + 1]))) {
+                                      power_domains[i + 1]))) {
                                retval = -ENOMEM;
                                goto exit;
                        }
                        slot->type = simple_strtoul(type, NULL, 10);
-                       if (slot->type < 1 || slot->type > 16)
-                               slot->type = 0;
-                       retval = register_pci_slot(slot);
+                               
+                       dbg("Found drc-index:0x%x drc-name:%s drc-type:%s\n",
+                                       indexes[i + 1], name, type);
 
-               }               /* for indexes */
-       }                       /* end of PCI device_node */
-      exit:
+                       retval = rpaphp_register_pci_slot(slot);
+               }
+       }
+exit:
        dbg("%s - Exit: num_slots=%d rc[%d]\n",
            __FUNCTION__, num_slots, retval);
        return retval;
 }
 
-/*
- * init_slots - initialize 'struct slot' structures for each slot
- *
- */
-static void init_slots(void)
-{
-       struct device_node *dn;
-
-       for (dn = find_all_nodes(); dn; dn = dn->next)
-               rpaphp_add_slot(dn);
-}
-
-static int init_rpa(void)
-{
-
-       init_MUTEX(&rpaphp_sem);
-
-       /* initialize internal data structure etc. */
-       init_slots();
-       if (!num_slots)
-               return -ENODEV;
-
-       return 0;
-}
-
-static void cleanup_slots(void)
+static void __exit cleanup_slots(void)
 {
        struct list_head *tmp, *n;
        struct slot *slot;
@@ -386,10 +352,15 @@ static void cleanup_slots(void)
 
 static int __init rpaphp_init(void)
 {
+       struct device_node *dn = NULL;
+
        info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
+       init_MUTEX(&rpaphp_sem);
+
+       while ((dn = of_find_node_by_type(dn, "pci")))
+               rpaphp_add_slot(dn);
 
-       /* read all the PRA info from the system */
-       return init_rpa();
+       return 0;
 }
 
 static void __exit rpaphp_exit(void)
@@ -397,75 +368,85 @@ static void __exit rpaphp_exit(void)
        cleanup_slots();
 }
 
-static int enable_slot(struct hotplug_slot *hotplug_slot)
+static int __enable_slot(struct slot *slot)
 {
-       int retval = 0;
-       struct slot *slot = get_slot(hotplug_slot, __FUNCTION__);
+       int state;
+       int retval;
 
-       if (slot == NULL)
-               return -ENODEV;
+       if (slot->state == CONFIGURED)
+               return 0;
 
-       if (slot->state == CONFIGURED) {
-               dbg("%s: %s is already enabled\n", __FUNCTION__, slot->name);
-               goto exit;
+       retval = rpaphp_get_sensor_state(slot, &state);
+       if (retval)
+               return retval;
+
+       if (state == PRESENT) {
+               pcibios_add_pci_devices(slot->bus);
+               slot->state = CONFIGURED;
+       } else if (state == EMPTY) {
+               slot->state = EMPTY;
+       } else {
+               err("%s: slot[%s] is in invalid state\n", __FUNCTION__, slot->name);
+               slot->state = NOT_VALID;
+               return -EINVAL;
        }
+       return 0;
+}
+
+static int enable_slot(struct hotplug_slot *hotplug_slot)
+{
+       int retval;
+       struct slot *slot = (struct slot *)hotplug_slot->private;
 
-       dbg("ENABLING SLOT %s\n", slot->name);
        down(&rpaphp_sem);
-       switch (slot->dev_type) {
-       case PCI_DEV:
-               retval = rpaphp_enable_pci_slot(slot);
-               break;
-       case VIO_DEV:
-               retval = rpaphp_enable_vio_slot(slot);
-               break;
-       default:
-               retval = -EINVAL;
-       }
+       retval = __enable_slot(slot);
        up(&rpaphp_sem);
-      exit:
-       dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
+
        return retval;
 }
 
-static int disable_slot(struct hotplug_slot *hotplug_slot)
+static int __disable_slot(struct slot *slot)
 {
-       int retval;
-       struct slot *slot = get_slot(hotplug_slot, __FUNCTION__);
+       struct pci_dev *dev, *tmp;
 
-       if (slot == NULL)
-               return -ENODEV;
+       if (slot->state == NOT_CONFIGURED)
+               return -EINVAL;
 
-       dbg("%s - Entry: slot[%s]\n", __FUNCTION__, slot->name);
-
-       if (slot->state == NOT_CONFIGURED) {
-               dbg("%s: %s is already disabled\n", __FUNCTION__, slot->name);
-               goto exit;
+       list_for_each_entry_safe(dev, tmp, &slot->bus->devices, bus_list) {
+               eeh_remove_bus_device(dev);
+               pci_remove_bus_device(dev);
        }
 
-       dbg("DISABLING SLOT %s\n", slot->name);
+       slot->state = NOT_CONFIGURED;
+       return 0;
+}
+
+static int disable_slot(struct hotplug_slot *hotplug_slot)
+{
+       struct slot *slot = (struct slot *)hotplug_slot->private;
+       int retval;
+
        down(&rpaphp_sem);
-       switch (slot->dev_type) {
-       case PCI_DEV:
-               rpaphp_set_attention_status(slot, LED_ID);
-               retval = rpaphp_unconfig_pci_adapter(slot);
-               rpaphp_set_attention_status(slot, LED_OFF);
-               break;
-       case VIO_DEV:
-               retval = rpaphp_unconfig_vio_adapter(slot);
-               break;
-       default:
-               retval = -ENODEV;
-       }
+       retval = __disable_slot (slot);
        up(&rpaphp_sem);
-      exit:
-       dbg("%s - Exit: rc[%d]\n", __FUNCTION__, retval);
+
        return retval;
 }
 
+struct hotplug_slot_ops rpaphp_hotplug_slot_ops = {
+       .owner = THIS_MODULE,
+       .enable_slot = enable_slot,
+       .disable_slot = disable_slot,
+       .set_attention_status = set_attention_status,
+       .get_power_status = get_power_status,
+       .get_attention_status = get_attention_status,
+       .get_adapter_status = get_adapter_status,
+       .get_max_bus_speed = get_max_bus_speed,
+};
+
 module_init(rpaphp_init);
 module_exit(rpaphp_exit);
 
 EXPORT_SYMBOL_GPL(rpaphp_add_slot);
-EXPORT_SYMBOL_GPL(rpaphp_remove_slot);
 EXPORT_SYMBOL_GPL(rpaphp_slot_head);
+EXPORT_SYMBOL_GPL(rpaphp_get_drc_props);