Merge master.kernel.org:/pub/scm/linux/kernel/git/jejb/scsi-misc-2.6
[powerpc.git] / drivers / scsi / scsi_transport_sas.c
index dd07562..b5b0c2c 100644 (file)
@@ -41,6 +41,7 @@ struct sas_host_attrs {
        struct mutex lock;
        u32 next_target_id;
        u32 next_expander_id;
+       int next_port_id;
 };
 #define to_sas_host_attrs(host)        ((struct sas_host_attrs *)(host)->shost_data)
 
@@ -76,6 +77,24 @@ get_sas_##title##_names(u32 table_key, char *buf)            \
        return len;                                             \
 }
 
+#define sas_bitfield_name_set(title, table)                    \
+static ssize_t                                                 \
+set_sas_##title##_names(u32 *table_key, const char *buf)       \
+{                                                              \
+       ssize_t len = 0;                                        \
+       int i;                                                  \
+                                                               \
+       for (i = 0; i < ARRAY_SIZE(table); i++) {               \
+               len = strlen(table[i].name);                    \
+               if (strncmp(buf, table[i].name, len) == 0 &&    \
+                   (buf[len] == '\n' || buf[len] == '\0')) {   \
+                       *table_key = table[i].value;            \
+                       return 0;                               \
+               }                                               \
+       }                                                       \
+       return -EINVAL;                                         \
+}
+
 #define sas_bitfield_name_search(title, table)                 \
 static ssize_t                                                 \
 get_sas_##title##_names(u32 table_key, char *buf)              \
@@ -130,7 +149,7 @@ static struct {
        { SAS_LINK_RATE_6_0_GBPS,       "6.0 Gbit" },
 };
 sas_bitfield_name_search(linkspeed, sas_linkspeed_names)
-
+sas_bitfield_name_set(linkspeed, sas_linkspeed_names)
 
 /*
  * SAS host attributes
@@ -146,6 +165,7 @@ static int sas_host_setup(struct transport_container *tc, struct device *dev,
        mutex_init(&sas_host->lock);
        sas_host->next_target_id = 0;
        sas_host->next_expander_id = 0;
+       sas_host->next_port_id = 0;
        return 0;
 }
 
@@ -251,10 +271,39 @@ show_sas_phy_##field(struct class_device *cdev, char *buf)                \
        return get_sas_linkspeed_names(phy->field, buf);                \
 }
 
+/* Fudge to tell if we're minimum or maximum */
+#define sas_phy_store_linkspeed(field)                                 \
+static ssize_t                                                         \
+store_sas_phy_##field(struct class_device *cdev, const char *buf,      \
+                     size_t count)                                     \
+{                                                                      \
+       struct sas_phy *phy = transport_class_to_phy(cdev);             \
+       struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);        \
+       struct sas_internal *i = to_sas_internal(shost->transportt);    \
+       u32 value;                                                      \
+       struct sas_phy_linkrates rates = {0};                           \
+       int error;                                                      \
+                                                                       \
+       error = set_sas_linkspeed_names(&value, buf);                   \
+       if (error)                                                      \
+               return error;                                           \
+       rates.field = value;                                            \
+       error = i->f->set_phy_speed(phy, &rates);                       \
+                                                                       \
+       return error ? error : count;                                   \
+}
+
+#define sas_phy_linkspeed_rw_attr(field)                               \
+       sas_phy_show_linkspeed(field)                                   \
+       sas_phy_store_linkspeed(field)                                  \
+static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field,         \
+       store_sas_phy_##field)
+
 #define sas_phy_linkspeed_attr(field)                                  \
        sas_phy_show_linkspeed(field)                                   \
 static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
 
+
 #define sas_phy_show_linkerror(field)                                  \
 static ssize_t                                                         \
 show_sas_phy_##field(struct class_device *cdev, char *buf)             \
@@ -264,9 +313,6 @@ show_sas_phy_##field(struct class_device *cdev, char *buf)          \
        struct sas_internal *i = to_sas_internal(shost->transportt);    \
        int error;                                                      \
                                                                        \
-       if (!phy->local_attached)                                       \
-               return -EINVAL;                                         \
-                                                                       \
        error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0;   \
        if (error)                                                      \
                return error;                                           \
@@ -297,9 +343,6 @@ static ssize_t do_sas_phy_reset(struct class_device *cdev,
        struct sas_internal *i = to_sas_internal(shost->transportt);
        int error;
 
-       if (!phy->local_attached)
-               return -EINVAL;
-
        error = i->f->phy_reset(phy, hard_reset);
        if (error)
                return error;
@@ -327,12 +370,12 @@ sas_phy_protocol_attr(identify.target_port_protocols,
 sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
                unsigned long long);
 sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
-//sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", u8);
+//sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", int);
 sas_phy_linkspeed_attr(negotiated_linkrate);
 sas_phy_linkspeed_attr(minimum_linkrate_hw);
-sas_phy_linkspeed_attr(minimum_linkrate);
+sas_phy_linkspeed_rw_attr(minimum_linkrate);
 sas_phy_linkspeed_attr(maximum_linkrate_hw);
-sas_phy_linkspeed_attr(maximum_linkrate);
+sas_phy_linkspeed_rw_attr(maximum_linkrate);
 sas_phy_linkerror_attr(invalid_dword_count);
 sas_phy_linkerror_attr(running_disparity_error_count);
 sas_phy_linkerror_attr(loss_of_dword_sync_count);
@@ -590,6 +633,38 @@ struct sas_port *sas_port_alloc(struct device *parent, int port_id)
 }
 EXPORT_SYMBOL(sas_port_alloc);
 
+/** sas_port_alloc_num - allocate and initialize a SAS port structure
+ *
+ * @parent:    parent device
+ *
+ * Allocates a SAS port structure and a number to go with it.  This
+ * interface is really for adapters where the port number has no
+ * meansing, so the sas class should manage them.  It will be added to
+ * the device tree below the device specified by @parent which must be
+ * either a Scsi_Host or a sas_expander_device.
+ *
+ * Returns %NULL on error
+ */
+struct sas_port *sas_port_alloc_num(struct device *parent)
+{
+       int index;
+       struct Scsi_Host *shost = dev_to_shost(parent);
+       struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
+
+       /* FIXME: use idr for this eventually */
+       mutex_lock(&sas_host->lock);
+       if (scsi_is_sas_expander_device(parent)) {
+               struct sas_rphy *rphy = dev_to_rphy(parent);
+               struct sas_expander_device *exp = rphy_to_expander_device(rphy);
+
+               index = exp->next_port_id++;
+       } else
+               index = sas_host->next_port_id++;
+       mutex_unlock(&sas_host->lock);
+       return sas_port_alloc(parent, index);
+}
+EXPORT_SYMBOL(sas_port_alloc_num);
+
 /**
  * sas_port_add - add a SAS port to the device hierarchy
  *
@@ -658,6 +733,13 @@ void sas_port_delete(struct sas_port *port)
        }
        mutex_unlock(&port->phy_list_mutex);
 
+       if (port->is_backlink) {
+               struct device *parent = port->dev.parent;
+
+               sysfs_remove_link(&port->dev.kobj, parent->bus_id);
+               port->is_backlink = 0;
+       }
+
        transport_remove_device(dev);
        device_del(dev);
        transport_destroy_device(dev);
@@ -733,6 +815,19 @@ void sas_port_delete_phy(struct sas_port *port, struct sas_phy *phy)
 }
 EXPORT_SYMBOL(sas_port_delete_phy);
 
+void sas_port_mark_backlink(struct sas_port *port)
+{
+       struct device *parent = port->dev.parent->parent->parent;
+
+       if (port->is_backlink)
+               return;
+       port->is_backlink = 1;
+       sysfs_create_link(&port->dev.kobj, &parent->kobj,
+                         parent->bus_id);
+
+}
+EXPORT_SYMBOL(sas_port_mark_backlink);
+
 /*
  * SAS remote PHY attributes.
  */
@@ -795,7 +890,7 @@ show_sas_rphy_enclosure_identifier(struct class_device *cdev, char *buf)
         * Only devices behind an expander are supported, because the
         * enclosure identifier is a SMP feature.
         */
-       if (phy->local_attached)
+       if (scsi_is_sas_phy_local(phy))
                return -EINVAL;
 
        error = i->f->get_enclosure_identifier(rphy, &identifier);
@@ -816,7 +911,7 @@ show_sas_rphy_bay_identifier(struct class_device *cdev, char *buf)
        struct sas_internal *i = to_sas_internal(shost->transportt);
        int val;
 
-       if (phy->local_attached)
+       if (scsi_is_sas_phy_local(phy))
                return -EINVAL;
 
        val = i->f->get_bay_identifier(rphy);
@@ -1140,7 +1235,7 @@ int sas_rphy_add(struct sas_rphy *rphy)
 
        if (identify->device_type == SAS_END_DEVICE &&
            rphy->scsi_target_id != -1) {
-               scsi_scan_target(&rphy->dev, parent->port_identifier,
+               scsi_scan_target(&rphy->dev, 0,
                                rphy->scsi_target_id, ~0, 0);
        }
 
@@ -1242,15 +1337,13 @@ static int sas_user_scan(struct Scsi_Host *shost, uint channel,
 
        mutex_lock(&sas_host->lock);
        list_for_each_entry(rphy, &sas_host->rphy_list, list) {
-               struct sas_port *parent = dev_to_sas_port(rphy->dev.parent);
-
                if (rphy->identify.device_type != SAS_END_DEVICE ||
                    rphy->scsi_target_id == -1)
                        continue;
 
-               if ((channel == SCAN_WILD_CARD || channel == parent->port_identifier) &&
+               if ((channel == SCAN_WILD_CARD || channel == 0) &&
                    (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) {
-                       scsi_scan_target(&rphy->dev, parent->port_identifier,
+                       scsi_scan_target(&rphy->dev, 0,
                                         rphy->scsi_target_id, lun, 1);
                }
        }
@@ -1264,13 +1357,23 @@ static int sas_user_scan(struct Scsi_Host *shost, uint channel,
  * Setup / Teardown code
  */
 
-#define SETUP_TEMPLATE(attrb, field, perm, test)                               \
+#define SETUP_TEMPLATE(attrb, field, perm, test)                       \
        i->private_##attrb[count] = class_device_attr_##field;          \
        i->private_##attrb[count].attr.mode = perm;                     \
        i->attrb[count] = &i->private_##attrb[count];                   \
        if (test)                                                       \
                count++
 
+#define SETUP_TEMPLATE_RW(attrb, field, perm, test, ro_test, ro_perm)  \
+       i->private_##attrb[count] = class_device_attr_##field;          \
+       i->private_##attrb[count].attr.mode = perm;                     \
+       if (ro_test) {                                                  \
+               i->private_##attrb[count].attr.mode = ro_perm;          \
+               i->private_##attrb[count].store = NULL;                 \
+       }                                                               \
+       i->attrb[count] = &i->private_##attrb[count];                   \
+       if (test)                                                       \
+               count++
 
 #define SETUP_RPORT_ATTRIBUTE(field)                                   \
        SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1)
@@ -1281,6 +1384,10 @@ static int sas_user_scan(struct Scsi_Host *shost, uint channel,
 #define SETUP_PHY_ATTRIBUTE(field)                                     \
        SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1)
 
+#define SETUP_PHY_ATTRIBUTE_RW(field)                                  \
+       SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1,       \
+                       !i->f->set_phy_speed, S_IRUGO)
+
 #define SETUP_PORT_ATTRIBUTE(field)                                    \
        SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
 
@@ -1361,9 +1468,9 @@ sas_attach_transport(struct sas_function_template *ft)
        //SETUP_PHY_ATTRIBUTE(port_identifier);
        SETUP_PHY_ATTRIBUTE(negotiated_linkrate);
        SETUP_PHY_ATTRIBUTE(minimum_linkrate_hw);
-       SETUP_PHY_ATTRIBUTE(minimum_linkrate);
+       SETUP_PHY_ATTRIBUTE_RW(minimum_linkrate);
        SETUP_PHY_ATTRIBUTE(maximum_linkrate_hw);
-       SETUP_PHY_ATTRIBUTE(maximum_linkrate);
+       SETUP_PHY_ATTRIBUTE_RW(maximum_linkrate);
 
        SETUP_PHY_ATTRIBUTE(invalid_dword_count);
        SETUP_PHY_ATTRIBUTE(running_disparity_error_count);