Merge branch 'upstream' of git://lost.foo-projects.org/~ahkok/git/netdev-2.6 into tmp
[safe/jmp/linux-2.6] / drivers / scsi / scsi_transport_sas.c
index 5a70d04..f3b1606 100644 (file)
 #include <scsi/scsi_transport.h>
 #include <scsi/scsi_transport_sas.h>
 
-
-#define SAS_HOST_ATTRS         0
-#define SAS_PORT_ATTRS         17
-#define SAS_RPORT_ATTRS                7
-#define SAS_END_DEV_ATTRS      3
-#define SAS_EXPANDER_ATTRS     7
-
-struct sas_internal {
-       struct scsi_transport_template t;
-       struct sas_function_template *f;
-
-       struct class_device_attribute private_host_attrs[SAS_HOST_ATTRS];
-       struct class_device_attribute private_phy_attrs[SAS_PORT_ATTRS];
-       struct class_device_attribute private_rphy_attrs[SAS_RPORT_ATTRS];
-       struct class_device_attribute private_end_dev_attrs[SAS_END_DEV_ATTRS];
-       struct class_device_attribute private_expander_attrs[SAS_EXPANDER_ATTRS];
-
-       struct transport_container phy_attr_cont;
-       struct transport_container rphy_attr_cont;
-       struct transport_container end_dev_attr_cont;
-       struct transport_container expander_attr_cont;
-
-       /*
-        * The array of null terminated pointers to attributes
-        * needed by scsi_sysfs.c
-        */
-       struct class_device_attribute *host_attrs[SAS_HOST_ATTRS + 1];
-       struct class_device_attribute *phy_attrs[SAS_PORT_ATTRS + 1];
-       struct class_device_attribute *rphy_attrs[SAS_RPORT_ATTRS + 1];
-       struct class_device_attribute *end_dev_attrs[SAS_END_DEV_ATTRS + 1];
-       struct class_device_attribute *expander_attrs[SAS_EXPANDER_ATTRS + 1];
-};
-#define to_sas_internal(tmpl)  container_of(tmpl, struct sas_internal, t)
-
+#include "scsi_sas_internal.h"
 struct sas_host_attrs {
        struct list_head rphy_list;
        struct mutex lock;
@@ -406,8 +373,6 @@ struct sas_phy *sas_phy_alloc(struct device *parent, int number)
        if (!phy)
                return NULL;
 
-       get_device(parent);
-
        phy->number = number;
 
        device_initialize(&phy->dev);
@@ -459,10 +424,7 @@ EXPORT_SYMBOL(sas_phy_add);
 void sas_phy_free(struct sas_phy *phy)
 {
        transport_destroy_device(&phy->dev);
-       put_device(phy->dev.parent);
-       put_device(phy->dev.parent);
-       put_device(phy->dev.parent);
-       kfree(phy);
+       put_device(&phy->dev);
 }
 EXPORT_SYMBOL(sas_phy_free);
 
@@ -484,7 +446,7 @@ sas_phy_delete(struct sas_phy *phy)
        transport_remove_device(dev);
        device_del(dev);
        transport_destroy_device(dev);
-       put_device(dev->parent);
+       put_device(dev);
 }
 EXPORT_SYMBOL(sas_phy_delete);
 
@@ -699,7 +661,7 @@ sas_expander_simple_attr(component_revision_id, component_revision_id, "%u\n",
 sas_expander_simple_attr(level, level, "%d\n", int);
 
 static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
-               "sas_rphy", NULL, NULL, NULL);
+               "sas_device", NULL, NULL, NULL);
 
 static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
 {
@@ -740,9 +702,7 @@ static int sas_end_dev_match(struct attribute_container *cont,
 
        i = to_sas_internal(shost->transportt);
        return &i->end_dev_attr_cont.ac == cont &&
-               rphy->identify.device_type == SAS_END_DEVICE &&
-               /* FIXME: remove contained eventually */
-               rphy->contained;
+               rphy->identify.device_type == SAS_END_DEVICE;
 }
 
 static int sas_expander_match(struct attribute_container *cont,
@@ -766,49 +726,26 @@ static int sas_expander_match(struct attribute_container *cont,
        i = to_sas_internal(shost->transportt);
        return &i->expander_attr_cont.ac == cont &&
                (rphy->identify.device_type == SAS_EDGE_EXPANDER_DEVICE ||
-                rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE) &&
-               /* FIXME: remove contained eventually */
-               rphy->contained;
+                rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE);
 }
 
-static void sas_rphy_release(struct device *dev)
+static void sas_expander_release(struct device *dev)
 {
        struct sas_rphy *rphy = dev_to_rphy(dev);
+       struct sas_expander_device *edev = rphy_to_expander_device(rphy);
 
        put_device(dev->parent);
-       kfree(rphy);
+       kfree(edev);
 }
 
-/**
- * sas_rphy_alloc  --  allocates and initialize a SAS remote PHY structure
- * @parent:            SAS PHY this remote PHY is conneted to
- *
- * Allocates an SAS remote PHY structure, connected to @parent.
- *
- * Returns:
- *     SAS PHY allocated or %NULL if the allocation failed.
- */
-struct sas_rphy *sas_rphy_alloc(struct sas_phy *parent)
+static void sas_end_device_release(struct device *dev)
 {
-       struct Scsi_Host *shost = dev_to_shost(&parent->dev);
-       struct sas_rphy *rphy;
-
-       rphy = kzalloc(sizeof(*rphy), GFP_KERNEL);
-       if (!rphy) {
-               put_device(&parent->dev);
-               return NULL;
-       }
-
-       device_initialize(&rphy->dev);
-       rphy->dev.parent = get_device(&parent->dev);
-       rphy->dev.release = sas_rphy_release;
-       sprintf(rphy->dev.bus_id, "rphy-%d:%d-%d",
-               shost->host_no, parent->port_identifier, parent->number);
-       transport_setup_device(&rphy->dev);
+       struct sas_rphy *rphy = dev_to_rphy(dev);
+       struct sas_end_device *edev = rphy_to_end_device(rphy);
 
-       return rphy;
+       put_device(dev->parent);
+       kfree(edev);
 }
-EXPORT_SYMBOL(sas_rphy_alloc);
 
 /**
  * sas_end_device_alloc - allocate an rphy for an end device
@@ -825,18 +762,15 @@ struct sas_rphy *sas_end_device_alloc(struct sas_phy *parent)
 
        rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
        if (!rdev) {
-               put_device(&parent->dev);
                return NULL;
        }
 
        device_initialize(&rdev->rphy.dev);
        rdev->rphy.dev.parent = get_device(&parent->dev);
-       rdev->rphy.dev.release = sas_rphy_release;
-       sprintf(rdev->rphy.dev.bus_id, "rphy-%d:%d-%d",
+       rdev->rphy.dev.release = sas_end_device_release;
+       sprintf(rdev->rphy.dev.bus_id, "end_device-%d:%d-%d",
                shost->host_no, parent->port_identifier, parent->number);
        rdev->rphy.identify.device_type = SAS_END_DEVICE;
-       /* FIXME: mark the rphy as being contained in a larger structure */
-       rdev->rphy.contained = 1;
        transport_setup_device(&rdev->rphy.dev);
 
        return &rdev->rphy;
@@ -863,21 +797,18 @@ struct sas_rphy *sas_expander_alloc(struct sas_phy *parent,
 
        rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
        if (!rdev) {
-               put_device(&parent->dev);
                return NULL;
        }
 
        device_initialize(&rdev->rphy.dev);
        rdev->rphy.dev.parent = get_device(&parent->dev);
-       rdev->rphy.dev.release = sas_rphy_release;
+       rdev->rphy.dev.release = sas_expander_release;
        mutex_lock(&sas_host->lock);
        rdev->rphy.scsi_target_id = sas_host->next_expander_id++;
        mutex_unlock(&sas_host->lock);
        sprintf(rdev->rphy.dev.bus_id, "expander-%d:%d",
                shost->host_no, rdev->rphy.scsi_target_id);
        rdev->rphy.identify.device_type = type;
-       /* FIXME: mark the rphy as being contained in a larger structure */
-       rdev->rphy.contained = 1;
        transport_setup_device(&rdev->rphy.dev);
 
        return &rdev->rphy;
@@ -914,6 +845,8 @@ int sas_rphy_add(struct sas_rphy *rphy)
            (identify->target_port_protocols &
             (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA)))
                rphy->scsi_target_id = sas_host->next_target_id++;
+       else if (identify->device_type == SAS_END_DEVICE)
+               rphy->scsi_target_id = -1;
        mutex_unlock(&sas_host->lock);
 
        if (identify->device_type == SAS_END_DEVICE &&
@@ -939,6 +872,7 @@ EXPORT_SYMBOL(sas_rphy_add);
  */
 void sas_rphy_free(struct sas_rphy *rphy)
 {
+       struct device *dev = &rphy->dev;
        struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
        struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
 
@@ -946,11 +880,9 @@ void sas_rphy_free(struct sas_rphy *rphy)
        list_del(&rphy->list);
        mutex_unlock(&sas_host->lock);
 
-       transport_destroy_device(&rphy->dev);
-       put_device(rphy->dev.parent);
-       put_device(rphy->dev.parent);
-       put_device(rphy->dev.parent);
-       kfree(rphy);
+       transport_destroy_device(dev);
+
+       put_device(dev);
 }
 EXPORT_SYMBOL(sas_rphy_free);
 
@@ -990,7 +922,7 @@ sas_rphy_delete(struct sas_rphy *rphy)
 
        parent->rphy = NULL;
 
-       put_device(&parent->dev);
+       put_device(dev);
 }
 EXPORT_SYMBOL(sas_rphy_delete);
 
@@ -1003,7 +935,8 @@ EXPORT_SYMBOL(sas_rphy_delete);
  */
 int scsi_is_sas_rphy(const struct device *dev)
 {
-       return dev->release == sas_rphy_release;
+       return dev->release == sas_end_device_release ||
+               dev->release == sas_expander_release;
 }
 EXPORT_SYMBOL(scsi_is_sas_rphy);
 
@@ -1022,7 +955,8 @@ static int sas_user_scan(struct Scsi_Host *shost, uint channel,
        list_for_each_entry(rphy, &sas_host->rphy_list, list) {
                struct sas_phy *parent = dev_to_phy(rphy->dev.parent);
 
-               if (rphy->scsi_target_id == -1)
+               if (rphy->identify.device_type != SAS_END_DEVICE ||
+                   rphy->scsi_target_id == -1)
                        continue;
 
                if ((channel == SCAN_WILD_CARD || channel == parent->port_identifier) &&
@@ -1044,7 +978,6 @@ static int sas_user_scan(struct Scsi_Host *shost, uint channel,
 #define SETUP_TEMPLATE(attrb, field, perm, test)                               \
        i->private_##attrb[count] = class_device_attr_##field;          \
        i->private_##attrb[count].attr.mode = perm;                     \
-       i->private_##attrb[count].store = NULL;                         \
        i->attrb[count] = &i->private_##attrb[count];                   \
        if (test)                                                       \
                count++