Subject: PCI Hotplug: rpaphp: Purify hotplug

From: John Rose <[email-masked]>
Date: Mon, 25 Jul 2005 10:17:03 -0500

Currently rpaphp registers the following bus types as hotplug slots:
1) Actual PCI Hotplug slots
2) Embedded/Internal PCI slots
3) PCI Host Bridges

The second and third bus types are not actually direct parents of
removable adapters.  As such, the rpaphp has special case code to fake
results for attributes like power, adapter status, etc.  This patch
removes types 2 and 3 from the rpaphp module.

This patch also changes the DLPAR module so that slots can be
DLPAR-added/removed without having been designated as hotplug-capable.

Signed-off-by: John Rose <[email-masked]>
Signed-off-by: Greg Kroah-Hartman <[email-masked]>

---
 drivers/pci/hotplug/rpadlpar_core.c |  154 ++++++++++++++++++------------------
 drivers/pci/hotplug/rpaphp.h        |    6 -
 drivers/pci/hotplug/rpaphp_core.c   |   67 ++-------------
 drivers/pci/hotplug/rpaphp_pci.c    |   11 --
 drivers/pci/hotplug/rpaphp_slot.c   |   57 ++-----------
 5 files changed, 103 insertions(+), 192 deletions(-)

--- gregkh-2.6.orig/drivers/pci/hotplug/rpaphp.h	2005-07-26 16:10:27.000000000 -0700
+++ gregkh-2.6/drivers/pci/hotplug/rpaphp.h	2005-07-26 16:10:30.000000000 -0700
@@ -30,10 +30,6 @@
 #include <linux/pci.h>
 #include "pci_hotplug.h"
 
-#define	PHB     2
-#define	HOTPLUG	1
-#define	EMBEDDED 0
-
 #define DR_INDICATOR 9002
 #define DR_ENTITY_SENSE 9003
 
@@ -79,7 +75,6 @@
 	u32 power_domain;
 	char *name;
 	char *location;
-	u8 removable;
 	struct device_node *dn;
 	struct pci_bus *bus;
 	struct list_head *pci_devs;
@@ -93,6 +88,7 @@
 /* function prototypes */
 
 /* rpaphp_pci.c */
+extern struct pci_bus *rpaphp_find_pci_bus(struct device_node *dn);
 extern int rpaphp_claim_resource(struct pci_dev *dev, int resource);
 extern int rpaphp_enable_pci_slot(struct slot *slot);
 extern int register_pci_slot(struct slot *slot);
--- gregkh-2.6.orig/drivers/pci/hotplug/rpaphp_pci.c	2005-07-26 16:10:27.000000000 -0700
+++ gregkh-2.6/drivers/pci/hotplug/rpaphp_pci.c	2005-07-26 16:10:30.000000000 -0700
@@ -49,13 +49,14 @@
 	return child;
 }
 
-static struct pci_bus *rpaphp_find_pci_bus(struct device_node *dn)
+struct pci_bus *rpaphp_find_pci_bus(struct device_node *dn)
 {
 	if (!dn->phb || !dn->phb->bus)
 		return NULL;
 
 	return find_bus_among_children(dn->phb->bus, dn);
 }
+EXPORT_SYMBOL_GPL(rpaphp_find_pci_bus);
 
 int rpaphp_claim_resource(struct pci_dev *dev, int resource)
 {
@@ -129,10 +130,8 @@
 	if (rc)
 		goto exit;
 
- 	if ((state == EMPTY) || (slot->type == PHB)) {
- 		dbg("slot is empty\n");
+ 	if (state == EMPTY)
  		*value = EMPTY;
- 	}
  	else if (state == PRESENT) {
 		if (!is_init) {
 			/* at run-time slot->state can be changed by */
@@ -423,10 +422,6 @@
 {
 	int rc = -EINVAL;
 
-	if ((slot->type == EMBEDDED) || (slot->type == PHB))
-		slot->removable = 0;
-	else
-		slot->removable = 1;
 	if (setup_pci_hotplug_slot_info(slot))
 		goto exit_rc;
 	if (setup_pci_slot(slot))
--- gregkh-2.6.orig/drivers/pci/hotplug/rpaphp_core.c	2005-07-26 16:10:01.000000000 -0700
+++ gregkh-2.6/drivers/pci/hotplug/rpaphp_core.c	2005-07-26 16:10:30.000000000 -0700
@@ -307,34 +307,6 @@
 	return 0;
 }
 
-static int is_dr_dn(struct device_node *dn, int **indexes, int **names,
-		int **types, int **power_domains, int **my_drc_index)
-{
-	int rc;
-
-	*my_drc_index = (int *) get_property(dn, "ibm,my-drc-index", NULL);
-	if(!*my_drc_index)
-		return (0);
-
-	if (!dn->parent)
-		return (0);
-
-	rc = get_children_props(dn->parent, indexes, names, types,
-				power_domains);
-	return (rc >= 0);
-}
-
-static inline int is_vdevice_root(struct device_node *dn)
-{
-	return !strcmp(dn->name, "vdevice");
-}
-
-int is_dlpar_type(const char *type_str)
-{
-	/* Only register DLPAR-capable nodes of drc-type PHB or SLOT */
-	return (!strcmp(type_str, "PHB") || !strcmp(type_str, "SLOT"));
-}
-
 /****************************************************************
  *	rpaphp not only registers PCI hotplug slots(HOTPLUG), 
  *	but also logical DR slots(EMBEDDED).
@@ -346,7 +318,7 @@
 {
 	struct slot *slot;
 	int retval = 0;
-	int i, *my_drc_index, slot_type;
+	int i;
 	int *indexes, *names, *types, *power_domains;
 	char *name, *type;
 
@@ -354,40 +326,25 @@
 
 	/* register PCI devices */
 	if (dn->name != 0 && strcmp(dn->name, "pci") == 0) {
-		if (is_php_dn(dn, &indexes, &names, &types, &power_domains))  
-			slot_type = HOTPLUG;
-		else if (is_dr_dn(dn, &indexes, &names, &types, &power_domains, &my_drc_index)) 
-			slot_type = EMBEDDED;
-		else goto exit;
+		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)) {
+	     		name += (strlen(name) + 1), type += (strlen(type) + 1)) 		{
 
-			if (slot_type == HOTPLUG ||
-			    (slot_type == EMBEDDED &&
-			     indexes[i + 1] == my_drc_index[0] &&
-			     is_dlpar_type(type))) {
-				if (!(slot = alloc_slot_struct(dn, indexes[i + 1], name,
-					       power_domains[i + 1]))) {
-					retval = -ENOMEM;
-					goto exit;
-				}
-				if (!strcmp(type, "PHB"))
-					slot->type = PHB;
-				else if (slot_type == EMBEDDED)
-					slot->type = EMBEDDED;
-				else
-					slot->type = simple_strtoul(type, NULL, 10);
+			if (!(slot = alloc_slot_struct(dn, indexes[i + 1], name,
+				       power_domains[i + 1]))) {
+				retval = -ENOMEM;
+				goto exit;
+			}
+			slot->type = simple_strtoul(type, NULL, 10);
 				
-				dbg("    Found drc-index:0x%x drc-name:%s drc-type:%s\n",
+			dbg("Found drc-index:0x%x drc-name:%s drc-type:%s\n",
 					indexes[i + 1], name, type);
 
-				retval = register_pci_slot(slot);
-				if (slot_type == EMBEDDED)
-					goto exit;
-			}
+			retval = register_pci_slot(slot);
 		}
 	}
 exit:
--- gregkh-2.6.orig/drivers/pci/hotplug/rpaphp_slot.c	2005-07-26 16:10:01.000000000 -0700
+++ gregkh-2.6/drivers/pci/hotplug/rpaphp_slot.c	2005-07-26 16:10:30.000000000 -0700
@@ -30,35 +30,6 @@
 #include <asm/rtas.h>
 #include "rpaphp.h"
 
-static ssize_t removable_read_file (struct hotplug_slot *php_slot, char *buf)
-{
-	u8 value;
-	int retval = -ENOENT;
-	struct slot *slot = (struct slot *)php_slot->private;
-
-	if (!slot)
-		return retval;
-
-	value = slot->removable;
-	retval = sprintf (buf, "%d\n", value);
-	return retval;
-}
-
-static struct hotplug_slot_attribute hotplug_slot_attr_removable = {
-	.attr = {.name = "phy_removable", .mode = S_IFREG | S_IRUGO},
-	.show = removable_read_file,
-};
-
-static void rpaphp_sysfs_add_attr_removable (struct hotplug_slot *slot)
-{
-	sysfs_create_file(&slot->kobj, &hotplug_slot_attr_removable.attr);
-}
-
-static void rpaphp_sysfs_remove_attr_removable (struct hotplug_slot *slot)
-{
-	sysfs_remove_file(&slot->kobj, &hotplug_slot_attr_removable.attr);
-}
-
 static ssize_t location_read_file (struct hotplug_slot *php_slot, char *buf)
 {
         char *value;
@@ -176,9 +147,6 @@
 	/* remove "phy_location" file */
 	rpaphp_sysfs_remove_attr_location(php_slot);
 
-	/* remove "phy_removable" file */
-	rpaphp_sysfs_remove_attr_removable(php_slot);
-
 	retval = pci_hp_deregister(php_slot);
 	if (retval)
 		err("Problem unregistering a slot %s\n", slot->name);
@@ -212,9 +180,6 @@
 	/* create "phy_locatoin" file */
 	rpaphp_sysfs_add_attr_location(slot->hotplug_slot);	
 
-	/* create "phy_removable" file */
-	rpaphp_sysfs_add_attr_removable(slot->hotplug_slot);	
-
 	/* add slot to our internal list */
 	dbg("%s adding slot[%s] to rpaphp_slot_list\n",
 	    __FUNCTION__, slot->name);
@@ -230,21 +195,17 @@
 {
 	int rc = 0, level;
 	
-	if (slot->type == HOTPLUG) {
-		rc = rtas_get_power_level(slot->power_domain, &level);
-		if (!rc) {
-			dbg("%s the power level of slot %s(pwd-domain:0x%x) is %d\n",
-				__FUNCTION__, slot->name, slot->power_domain, level);
-			*value = level;
-		} else
-			err("failed to get power-level for slot(%s), rc=0x%x\n",
-				slot->location, rc);
-	} else {
-		dbg("%s report POWER_ON for EMBEDDED or PHB slot %s\n",
-			__FUNCTION__, slot->location);
-		*value = (u8) POWER_ON;
+	rc = rtas_get_power_level(slot->power_domain, &level);
+	if (rc < 0) {
+		err("failed to get power-level for slot(%s), rc=0x%x\n",
+			slot->location, rc);
+		return rc;
 	}
 
+	dbg("%s the power level of slot %s(pwd-domain:0x%x) is %d\n",
+		__FUNCTION__, slot->name, slot->power_domain, level);
+	*value = level;
+
 	return rc;
 }
 
--- gregkh-2.6.orig/drivers/pci/hotplug/rpadlpar_core.c	2005-07-26 16:10:27.000000000 -0700
+++ gregkh-2.6/drivers/pci/hotplug/rpadlpar_core.c	2005-07-26 16:10:30.000000000 -0700
@@ -26,6 +26,8 @@
 
 static DECLARE_MUTEX(rpadlpar_sem);
 
+#define DLPAR_MODULE_NAME "rpadlpar_io"
+
 #define NODE_TYPE_VIO  1
 #define NODE_TYPE_SLOT 2
 #define NODE_TYPE_PHB  3
@@ -93,14 +95,14 @@
 	return NULL;
 }
 
-static struct slot *find_slot(char *drc_name)
+static struct slot *find_slot(struct device_node *dn)
 {
 	struct list_head *tmp, *n;
 	struct slot *slot;
 
         list_for_each_safe(tmp, n, &rpaphp_slot_head) {
                 slot = list_entry(tmp, struct slot, rpaphp_slot_list);
-                if (strcmp(slot->location, drc_name) == 0)
+                if (slot->dn == dn)
                         return slot;
         }
 
@@ -214,6 +216,9 @@
 	struct pci_dev *dev;
 	int rc;
 
+	if (rpaphp_find_pci_bus(dn))
+		return -EINVAL;
+
 	/* Add pci bus */
 	dev = dlpar_pci_add_bus(dn);
 	if (!dev) {
@@ -261,36 +266,32 @@
 	return 0;
 }
 
-static int dlpar_remove_phb(struct slot *slot)
+static int dlpar_remove_phb(char *drc_name, struct device_node *dn)
 {
-	struct pci_controller *phb;
-	struct device_node *dn;
+	struct slot *slot;
 	int rc = 0;
 
-	dn = slot->dn;
-	if (!dn) {
-		printk(KERN_ERR "%s: unexpected NULL slot device node\n",
-				__FUNCTION__);
-		return -EIO;
-	}
-
-	phb = dn->phb;
-	if (!phb) {
-		printk(KERN_ERR "%s: unexpected NULL phb pointer\n",
-				__FUNCTION__);
-		return -EIO;
-	}
+	if (!rpaphp_find_pci_bus(dn))
+		return -EINVAL;
 
-	if (rpaphp_remove_slot(slot)) {
-		printk(KERN_ERR "%s: unable to remove hotplug slot %s\n",
-			__FUNCTION__, slot->location);
-		return -EIO;
+	slot = find_slot(dn);
+	if (slot) {
+		/* Remove hotplug slot */
+		if (rpaphp_remove_slot(slot)) {
+			printk(KERN_ERR
+				"%s: unable to remove hotplug slot %s\n",
+				__FUNCTION__, drc_name);
+			return -EIO;
+		}
 	}
 
-	rc = dlpar_remove_root_bus(phb);
+	BUG_ON(!dn->phb);
+	rc = dlpar_remove_root_bus(dn->phb);
 	if (rc < 0)
 		return rc;
 
+	dn->phb = NULL;
+
 	return 0;
 }
 
@@ -298,9 +299,14 @@
 {
 	struct pci_controller *phb;
 
+	if (dn->phb) {
+		/* PHB already exists */
+		return -EINVAL;
+	}
+
 	phb = init_phb_dynamic(dn);
 	if (!phb)
-		return -EINVAL;
+		return -EIO;
 
 	if (rpaphp_add_slot(dn)) {
 		printk(KERN_ERR "%s: unable to add hotplug slot %s\n",
@@ -310,6 +316,20 @@
 	return 0;
 }
 
+static int dlpar_add_vio_slot(char *drc_name, struct device_node *dn)
+{
+	if (vio_find_node(dn))
+		return -EINVAL;
+
+	if (!vio_register_device_node(dn)) {
+		printk(KERN_ERR
+			"%s: failed to register vio node %s\n",
+			__FUNCTION__, drc_name);
+		return -EIO;
+	}
+	return 0;
+}
+
 /**
  * dlpar_add_slot - DLPAR add an I/O Slot
  * @drc_name: drc-name of newly added slot
@@ -327,17 +347,11 @@
 {
 	struct device_node *dn = NULL;
 	int node_type;
-	int rc;
+	int rc = -EIO;
 
 	if (down_interruptible(&rpadlpar_sem))
 		return -ERESTARTSYS;
 
-	/* Check for existing hotplug slot */
-	if (find_slot(drc_name)) {
-		rc = -EINVAL;
-		goto exit;
-	}
-
 	/* Find newly added node */
 	dn = find_dlpar_node(drc_name, &node_type);
 	if (!dn) {
@@ -345,32 +359,19 @@
 		goto exit;
 	}
 
-	rc = -EIO;
 	switch (node_type) {
 		case NODE_TYPE_VIO:
-			if (!vio_register_device_node(dn)) {
-				printk(KERN_ERR
-					"%s: failed to register vio node %s\n",
-					__FUNCTION__, drc_name);
-				goto exit;
-			}
+			rc = dlpar_add_vio_slot(drc_name, dn);
 			break;
 		case NODE_TYPE_SLOT:
 			rc = dlpar_add_pci_slot(drc_name, dn);
-			if (rc)
-				goto exit;
 			break;
 		case NODE_TYPE_PHB:
 			rc = dlpar_add_phb(drc_name, dn);
-			if (rc)
-				goto exit;
 			break;
-		default:
-			printk("%s: unexpected node type\n", __FUNCTION__);
-			goto exit;
 	}
 
-	rc = 0;
+	printk(KERN_INFO "%s: slot %s added\n", DLPAR_MODULE_NAME, drc_name);
 exit:
 	up(&rpadlpar_sem);
 	return rc;
@@ -384,18 +385,15 @@
  * of an I/O Slot.
  * Return Codes:
  * 0			Success
- * -EIO			Internal  Error
+ * -EINVAL		Vio dev doesn't exist
  */
-static int dlpar_remove_vio_slot(struct device_node *dn, char *drc_name)
+static int dlpar_remove_vio_slot(char *drc_name, struct device_node *dn)
 {
 	struct vio_dev *vio_dev;
 
 	vio_dev = vio_find_node(dn);
-	if (!vio_dev) {
-		printk(KERN_ERR "%s: %s does not correspond to a vio dev\n",
-				__FUNCTION__, drc_name);
-		return -EIO;
-	}
+	if (!vio_dev)
+		return -EINVAL;
 
 	vio_unregister_device(vio_dev);
 	return 0;
@@ -412,15 +410,24 @@
  * -ENODEV		Not a valid drc_name
  * -EIO			Internal PCI Error
  */
-int dlpar_remove_pci_slot(struct slot *slot, char *drc_name)
+int dlpar_remove_pci_slot(char *drc_name, struct device_node *dn)
 {
-	struct pci_bus *bus = slot->bus;
+	struct pci_bus *bus;
+	struct slot *slot;
 
-	/* Remove hotplug slot */
-	if (rpaphp_remove_slot(slot)) {
-		printk(KERN_ERR "%s: unable to remove hotplug slot %s\n",
-			__FUNCTION__, drc_name);
-		return -EIO;
+	bus = rpaphp_find_pci_bus(dn);
+	if (!bus)
+		return -EINVAL;
+
+	slot = find_slot(dn);
+	if (slot) {
+		/* Remove hotplug slot */
+		if (rpaphp_remove_slot(slot)) {
+			printk(KERN_ERR
+				"%s: unable to remove hotplug slot %s\n",
+				__FUNCTION__, drc_name);
+			return -EIO;
+		}
 	}
 
 	if (unmap_bus_range(bus)) {
@@ -450,7 +457,6 @@
 int dlpar_remove_slot(char *drc_name)
 {
 	struct device_node *dn;
-	struct slot *slot;
 	int node_type;
 	int rc = 0;
 
@@ -463,22 +469,18 @@
 		goto exit;
 	}
 
-	if (node_type == NODE_TYPE_VIO) {
-		rc = dlpar_remove_vio_slot(dn, drc_name);
-	} else {
-		slot = find_slot(drc_name);
-		if (!slot) {
-			rc = -EINVAL;
-			goto exit;
-		}
-
-		if (node_type == NODE_TYPE_PHB)
-			rc = dlpar_remove_phb(slot);
-		else {
-			/* NODE_TYPE_SLOT */
-			rc = dlpar_remove_pci_slot(slot, drc_name);
-		}
+	switch (node_type) {
+		case NODE_TYPE_VIO:
+			rc = dlpar_remove_vio_slot(drc_name, dn);
+			break;
+		case NODE_TYPE_PHB:
+			rc = dlpar_remove_phb(drc_name, dn);
+			break;
+		case NODE_TYPE_SLOT:
+			rc = dlpar_remove_pci_slot(drc_name, dn);
+			break;
 	}
+	printk(KERN_INFO "%s: slot %s removed\n", DLPAR_MODULE_NAME, drc_name);
 exit:
 	up(&rpadlpar_sem);
 	return rc;