2  * Copyright (C) 2005-2006 Dell Inc.
 
   3  *      Released under GPL v2.
 
   5  * Serial Attached SCSI (SAS) transport class.
 
   7  * The SAS transport class contains common code to deal with SAS HBAs,
 
   8  * an aproximated representation of SAS topologies in the driver model,
 
   9  * and various sysfs attributes to expose these topologies and managment
 
  10  * interfaces to userspace.
 
  12  * In addition to the basic SCSI core objects this transport class
 
  13  * introduces two additional intermediate objects:  The SAS PHY
 
  14  * as represented by struct sas_phy defines an "outgoing" PHY on
 
  15  * a SAS HBA or Expander, and the SAS remote PHY represented by
 
  16  * struct sas_rphy defines an "incoming" PHY on a SAS Expander or
 
  17  * end device.  Note that this is purely a software concept, the
 
  18  * underlying hardware for a PHY and a remote PHY is the exactly
 
  21  * There is no concept of a SAS port in this code, users can see
 
  22  * what PHYs form a wide port based on the port_identifier attribute,
 
  23  * which is the same for all PHYs in a port.
 
  26 #include <linux/init.h>
 
  27 #include <linux/module.h>
 
  28 #include <linux/jiffies.h>
 
  29 #include <linux/err.h>
 
  30 #include <linux/slab.h>
 
  31 #include <linux/string.h>
 
  33 #include <scsi/scsi.h>
 
  34 #include <scsi/scsi_device.h>
 
  35 #include <scsi/scsi_host.h>
 
  36 #include <scsi/scsi_transport.h>
 
  37 #include <scsi/scsi_transport_sas.h>
 
  39 #include "scsi_sas_internal.h"
 
  40 struct sas_host_attrs {
 
  41         struct list_head rphy_list;
 
  47 #define to_sas_host_attrs(host) ((struct sas_host_attrs *)(host)->shost_data)
 
  51  * Hack to allow attributes of the same name in different objects.
 
  53 #define SAS_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
 
  54         struct class_device_attribute class_device_attr_##_prefix##_##_name = \
 
  55         __ATTR(_name,_mode,_show,_store)
 
  59  * Pretty printing helpers
 
  62 #define sas_bitfield_name_match(title, table)                   \
 
  64 get_sas_##title##_names(u32 table_key, char *buf)               \
 
  70         for (i = 0; i < ARRAY_SIZE(table); i++) {               \
 
  71                 if (table[i].value & table_key) {               \
 
  72                         len += sprintf(buf + len, "%s%s",       \
 
  73                                 prefix, table[i].name);         \
 
  77         len += sprintf(buf + len, "\n");                        \
 
  81 #define sas_bitfield_name_set(title, table)                     \
 
  83 set_sas_##title##_names(u32 *table_key, const char *buf)        \
 
  88         for (i = 0; i < ARRAY_SIZE(table); i++) {               \
 
  89                 len = strlen(table[i].name);                    \
 
  90                 if (strncmp(buf, table[i].name, len) == 0 &&    \
 
  91                     (buf[len] == '\n' || buf[len] == '\0')) {   \
 
  92                         *table_key = table[i].value;            \
 
  99 #define sas_bitfield_name_search(title, table)                  \
 
 101 get_sas_##title##_names(u32 table_key, char *buf)               \
 
 106         for (i = 0; i < ARRAY_SIZE(table); i++) {               \
 
 107                 if (table[i].value == table_key) {              \
 
 108                         len += sprintf(buf + len, "%s",         \
 
 113         len += sprintf(buf + len, "\n");                        \
 
 120 } sas_device_type_names[] = {
 
 121         { SAS_PHY_UNUSED,               "unused" },
 
 122         { SAS_END_DEVICE,               "end device" },
 
 123         { SAS_EDGE_EXPANDER_DEVICE,     "edge expander" },
 
 124         { SAS_FANOUT_EXPANDER_DEVICE,   "fanout expander" },
 
 126 sas_bitfield_name_search(device_type, sas_device_type_names)
 
 132 } sas_protocol_names[] = {
 
 133         { SAS_PROTOCOL_SATA,            "sata" },
 
 134         { SAS_PROTOCOL_SMP,             "smp" },
 
 135         { SAS_PROTOCOL_STP,             "stp" },
 
 136         { SAS_PROTOCOL_SSP,             "ssp" },
 
 138 sas_bitfield_name_match(protocol, sas_protocol_names)
 
 143 } sas_linkspeed_names[] = {
 
 144         { SAS_LINK_RATE_UNKNOWN,        "Unknown" },
 
 145         { SAS_PHY_DISABLED,             "Phy disabled" },
 
 146         { SAS_LINK_RATE_FAILED,         "Link Rate failed" },
 
 147         { SAS_SATA_SPINUP_HOLD,         "Spin-up hold" },
 
 148         { SAS_LINK_RATE_1_5_GBPS,       "1.5 Gbit" },
 
 149         { SAS_LINK_RATE_3_0_GBPS,       "3.0 Gbit" },
 
 150         { SAS_LINK_RATE_6_0_GBPS,       "6.0 Gbit" },
 
 152 sas_bitfield_name_search(linkspeed, sas_linkspeed_names)
 
 153 sas_bitfield_name_set(linkspeed, sas_linkspeed_names)
 
 156  * SAS host attributes
 
 159 static int sas_host_setup(struct transport_container *tc, struct device *dev,
 
 160                           struct class_device *cdev)
 
 162         struct Scsi_Host *shost = dev_to_shost(dev);
 
 163         struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
 
 165         INIT_LIST_HEAD(&sas_host->rphy_list);
 
 166         mutex_init(&sas_host->lock);
 
 167         sas_host->next_target_id = 0;
 
 168         sas_host->next_expander_id = 0;
 
 169         sas_host->next_port_id = 0;
 
 173 static DECLARE_TRANSPORT_CLASS(sas_host_class,
 
 174                 "sas_host", sas_host_setup, NULL, NULL);
 
 176 static int sas_host_match(struct attribute_container *cont,
 
 179         struct Scsi_Host *shost;
 
 180         struct sas_internal *i;
 
 182         if (!scsi_is_host_device(dev))
 
 184         shost = dev_to_shost(dev);
 
 186         if (!shost->transportt)
 
 188         if (shost->transportt->host_attrs.ac.class !=
 
 189                         &sas_host_class.class)
 
 192         i = to_sas_internal(shost->transportt);
 
 193         return &i->t.host_attrs.ac == cont;
 
 196 static int do_sas_phy_delete(struct device *dev, void *data)
 
 198         int pass = (int)(unsigned long)data;
 
 200         if (pass == 0 && scsi_is_sas_port(dev))
 
 201                 sas_port_delete(dev_to_sas_port(dev));
 
 202         else if (pass == 1 && scsi_is_sas_phy(dev))
 
 203                 sas_phy_delete(dev_to_phy(dev));
 
 208  * sas_remove_children  --  tear down a devices SAS data structures
 
 209  * @dev:        device belonging to the sas object
 
 211  * Removes all SAS PHYs and remote PHYs for a given object
 
 213 void sas_remove_children(struct device *dev)
 
 215         device_for_each_child(dev, (void *)0, do_sas_phy_delete);
 
 216         device_for_each_child(dev, (void *)1, do_sas_phy_delete);
 
 218 EXPORT_SYMBOL(sas_remove_children);
 
 221  * sas_remove_host  --  tear down a Scsi_Host's SAS data structures
 
 222  * @shost:      Scsi Host that is torn down
 
 224  * Removes all SAS PHYs and remote PHYs for a given Scsi_Host.
 
 225  * Must be called just before scsi_remove_host for SAS HBAs.
 
 227 void sas_remove_host(struct Scsi_Host *shost)
 
 229         sas_remove_children(&shost->shost_gendev);
 
 231 EXPORT_SYMBOL(sas_remove_host);
 
 238 #define sas_phy_show_simple(field, name, format_string, cast)           \
 
 240 show_sas_phy_##name(struct class_device *cdev, char *buf)               \
 
 242         struct sas_phy *phy = transport_class_to_phy(cdev);             \
 
 244         return snprintf(buf, 20, format_string, cast phy->field);       \
 
 247 #define sas_phy_simple_attr(field, name, format_string, type)           \
 
 248         sas_phy_show_simple(field, name, format_string, (type)) \
 
 249 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
 
 251 #define sas_phy_show_protocol(field, name)                              \
 
 253 show_sas_phy_##name(struct class_device *cdev, char *buf)               \
 
 255         struct sas_phy *phy = transport_class_to_phy(cdev);             \
 
 258                 return snprintf(buf, 20, "none\n");                     \
 
 259         return get_sas_protocol_names(phy->field, buf);         \
 
 262 #define sas_phy_protocol_attr(field, name)                              \
 
 263         sas_phy_show_protocol(field, name)                              \
 
 264 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
 
 266 #define sas_phy_show_linkspeed(field)                                   \
 
 268 show_sas_phy_##field(struct class_device *cdev, char *buf)              \
 
 270         struct sas_phy *phy = transport_class_to_phy(cdev);             \
 
 272         return get_sas_linkspeed_names(phy->field, buf);                \
 
 275 /* Fudge to tell if we're minimum or maximum */
 
 276 #define sas_phy_store_linkspeed(field)                                  \
 
 278 store_sas_phy_##field(struct class_device *cdev, const char *buf,       \
 
 281         struct sas_phy *phy = transport_class_to_phy(cdev);             \
 
 282         struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);        \
 
 283         struct sas_internal *i = to_sas_internal(shost->transportt);    \
 
 285         struct sas_phy_linkrates rates = {0};                           \
 
 288         error = set_sas_linkspeed_names(&value, buf);                   \
 
 291         rates.field = value;                                            \
 
 292         error = i->f->set_phy_speed(phy, &rates);                       \
 
 294         return error ? error : count;                                   \
 
 297 #define sas_phy_linkspeed_rw_attr(field)                                \
 
 298         sas_phy_show_linkspeed(field)                                   \
 
 299         sas_phy_store_linkspeed(field)                                  \
 
 300 static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field,          \
 
 301         store_sas_phy_##field)
 
 303 #define sas_phy_linkspeed_attr(field)                                   \
 
 304         sas_phy_show_linkspeed(field)                                   \
 
 305 static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
 
 308 #define sas_phy_show_linkerror(field)                                   \
 
 310 show_sas_phy_##field(struct class_device *cdev, char *buf)              \
 
 312         struct sas_phy *phy = transport_class_to_phy(cdev);             \
 
 313         struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);        \
 
 314         struct sas_internal *i = to_sas_internal(shost->transportt);    \
 
 317         error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0;   \
 
 320         return snprintf(buf, 20, "%u\n", phy->field);                   \
 
 323 #define sas_phy_linkerror_attr(field)                                   \
 
 324         sas_phy_show_linkerror(field)                                   \
 
 325 static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
 
 329 show_sas_device_type(struct class_device *cdev, char *buf)
 
 331         struct sas_phy *phy = transport_class_to_phy(cdev);
 
 333         if (!phy->identify.device_type)
 
 334                 return snprintf(buf, 20, "none\n");
 
 335         return get_sas_device_type_names(phy->identify.device_type, buf);
 
 337 static CLASS_DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
 
 339 static ssize_t do_sas_phy_enable(struct class_device *cdev,
 
 340                 size_t count, int enable)
 
 342         struct sas_phy *phy = transport_class_to_phy(cdev);
 
 343         struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
 
 344         struct sas_internal *i = to_sas_internal(shost->transportt);
 
 347         error = i->f->phy_enable(phy, enable);
 
 350         phy->enabled = enable;
 
 354 static ssize_t store_sas_phy_enable(struct class_device *cdev,
 
 355                 const char *buf, size_t count)
 
 362                 do_sas_phy_enable(cdev, count, 0);
 
 365                 do_sas_phy_enable(cdev, count, 1);
 
 374 static ssize_t show_sas_phy_enable(struct class_device *cdev, char *buf)
 
 376         struct sas_phy *phy = transport_class_to_phy(cdev);
 
 378         return snprintf(buf, 20, "%d", phy->enabled);
 
 381 static CLASS_DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, show_sas_phy_enable,
 
 382                          store_sas_phy_enable);
 
 384 static ssize_t do_sas_phy_reset(struct class_device *cdev,
 
 385                 size_t count, int hard_reset)
 
 387         struct sas_phy *phy = transport_class_to_phy(cdev);
 
 388         struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
 
 389         struct sas_internal *i = to_sas_internal(shost->transportt);
 
 392         error = i->f->phy_reset(phy, hard_reset);
 
 398 static ssize_t store_sas_link_reset(struct class_device *cdev,
 
 399                 const char *buf, size_t count)
 
 401         return do_sas_phy_reset(cdev, count, 0);
 
 403 static CLASS_DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset);
 
 405 static ssize_t store_sas_hard_reset(struct class_device *cdev,
 
 406                 const char *buf, size_t count)
 
 408         return do_sas_phy_reset(cdev, count, 1);
 
 410 static CLASS_DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset);
 
 412 sas_phy_protocol_attr(identify.initiator_port_protocols,
 
 413                 initiator_port_protocols);
 
 414 sas_phy_protocol_attr(identify.target_port_protocols,
 
 415                 target_port_protocols);
 
 416 sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
 
 418 sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
 
 419 //sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", int);
 
 420 sas_phy_linkspeed_attr(negotiated_linkrate);
 
 421 sas_phy_linkspeed_attr(minimum_linkrate_hw);
 
 422 sas_phy_linkspeed_rw_attr(minimum_linkrate);
 
 423 sas_phy_linkspeed_attr(maximum_linkrate_hw);
 
 424 sas_phy_linkspeed_rw_attr(maximum_linkrate);
 
 425 sas_phy_linkerror_attr(invalid_dword_count);
 
 426 sas_phy_linkerror_attr(running_disparity_error_count);
 
 427 sas_phy_linkerror_attr(loss_of_dword_sync_count);
 
 428 sas_phy_linkerror_attr(phy_reset_problem_count);
 
 431 static DECLARE_TRANSPORT_CLASS(sas_phy_class,
 
 432                 "sas_phy", NULL, NULL, NULL);
 
 434 static int sas_phy_match(struct attribute_container *cont, struct device *dev)
 
 436         struct Scsi_Host *shost;
 
 437         struct sas_internal *i;
 
 439         if (!scsi_is_sas_phy(dev))
 
 441         shost = dev_to_shost(dev->parent);
 
 443         if (!shost->transportt)
 
 445         if (shost->transportt->host_attrs.ac.class !=
 
 446                         &sas_host_class.class)
 
 449         i = to_sas_internal(shost->transportt);
 
 450         return &i->phy_attr_cont.ac == cont;
 
 453 static void sas_phy_release(struct device *dev)
 
 455         struct sas_phy *phy = dev_to_phy(dev);
 
 457         put_device(dev->parent);
 
 462  * sas_phy_alloc  --  allocates and initialize a SAS PHY structure
 
 463  * @parent:     Parent device
 
 466  * Allocates an SAS PHY structure.  It will be added in the device tree
 
 467  * below the device specified by @parent, which has to be either a Scsi_Host
 
 471  *      SAS PHY allocated or %NULL if the allocation failed.
 
 473 struct sas_phy *sas_phy_alloc(struct device *parent, int number)
 
 475         struct Scsi_Host *shost = dev_to_shost(parent);
 
 478         phy = kzalloc(sizeof(*phy), GFP_KERNEL);
 
 482         phy->number = number;
 
 485         device_initialize(&phy->dev);
 
 486         phy->dev.parent = get_device(parent);
 
 487         phy->dev.release = sas_phy_release;
 
 488         INIT_LIST_HEAD(&phy->port_siblings);
 
 489         if (scsi_is_sas_expander_device(parent)) {
 
 490                 struct sas_rphy *rphy = dev_to_rphy(parent);
 
 491                 sprintf(phy->dev.bus_id, "phy-%d:%d:%d", shost->host_no,
 
 492                         rphy->scsi_target_id, number);
 
 494                 sprintf(phy->dev.bus_id, "phy-%d:%d", shost->host_no, number);
 
 496         transport_setup_device(&phy->dev);
 
 500 EXPORT_SYMBOL(sas_phy_alloc);
 
 503  * sas_phy_add  --  add a SAS PHY to the device hierarchy
 
 504  * @phy:        The PHY to be added
 
 506  * Publishes a SAS PHY to the rest of the system.
 
 508 int sas_phy_add(struct sas_phy *phy)
 
 512         error = device_add(&phy->dev);
 
 514                 transport_add_device(&phy->dev);
 
 515                 transport_configure_device(&phy->dev);
 
 520 EXPORT_SYMBOL(sas_phy_add);
 
 523  * sas_phy_free  --  free a SAS PHY
 
 524  * @phy:        SAS PHY to free
 
 526  * Frees the specified SAS PHY.
 
 529  *   This function must only be called on a PHY that has not
 
 530  *   sucessfully been added using sas_phy_add().
 
 532 void sas_phy_free(struct sas_phy *phy)
 
 534         transport_destroy_device(&phy->dev);
 
 535         put_device(&phy->dev);
 
 537 EXPORT_SYMBOL(sas_phy_free);
 
 540  * sas_phy_delete  --  remove SAS PHY
 
 541  * @phy:        SAS PHY to remove
 
 543  * Removes the specified SAS PHY.  If the SAS PHY has an
 
 544  * associated remote PHY it is removed before.
 
 547 sas_phy_delete(struct sas_phy *phy)
 
 549         struct device *dev = &phy->dev;
 
 551         /* this happens if the phy is still part of a port when deleted */
 
 552         BUG_ON(!list_empty(&phy->port_siblings));
 
 554         transport_remove_device(dev);
 
 556         transport_destroy_device(dev);
 
 559 EXPORT_SYMBOL(sas_phy_delete);
 
 562  * scsi_is_sas_phy  --  check if a struct device represents a SAS PHY
 
 563  * @dev:        device to check
 
 566  *      %1 if the device represents a SAS PHY, %0 else
 
 568 int scsi_is_sas_phy(const struct device *dev)
 
 570         return dev->release == sas_phy_release;
 
 572 EXPORT_SYMBOL(scsi_is_sas_phy);
 
 575  * SAS Port attributes
 
 577 #define sas_port_show_simple(field, name, format_string, cast)          \
 
 579 show_sas_port_##name(struct class_device *cdev, char *buf)              \
 
 581         struct sas_port *port = transport_class_to_sas_port(cdev);      \
 
 583         return snprintf(buf, 20, format_string, cast port->field);      \
 
 586 #define sas_port_simple_attr(field, name, format_string, type)          \
 
 587         sas_port_show_simple(field, name, format_string, (type))        \
 
 588 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_port_##name, NULL)
 
 590 sas_port_simple_attr(num_phys, num_phys, "%d\n", int);
 
 592 static DECLARE_TRANSPORT_CLASS(sas_port_class,
 
 593                                "sas_port", NULL, NULL, NULL);
 
 595 static int sas_port_match(struct attribute_container *cont, struct device *dev)
 
 597         struct Scsi_Host *shost;
 
 598         struct sas_internal *i;
 
 600         if (!scsi_is_sas_port(dev))
 
 602         shost = dev_to_shost(dev->parent);
 
 604         if (!shost->transportt)
 
 606         if (shost->transportt->host_attrs.ac.class !=
 
 607                         &sas_host_class.class)
 
 610         i = to_sas_internal(shost->transportt);
 
 611         return &i->port_attr_cont.ac == cont;
 
 615 static void sas_port_release(struct device *dev)
 
 617         struct sas_port *port = dev_to_sas_port(dev);
 
 619         BUG_ON(!list_empty(&port->phy_list));
 
 621         put_device(dev->parent);
 
 625 static void sas_port_create_link(struct sas_port *port,
 
 630         res = sysfs_create_link(&port->dev.kobj, &phy->dev.kobj,
 
 634         res = sysfs_create_link(&phy->dev.kobj, &port->dev.kobj, "port");
 
 639         printk(KERN_ERR "%s: Cannot create port links, err=%d\n",
 
 643 static void sas_port_delete_link(struct sas_port *port,
 
 646         sysfs_remove_link(&port->dev.kobj, phy->dev.bus_id);
 
 647         sysfs_remove_link(&phy->dev.kobj, "port");
 
 650 /** sas_port_alloc - allocate and initialize a SAS port structure
 
 652  * @parent:     parent device
 
 653  * @port_id:    port number
 
 655  * Allocates a SAS port structure.  It will be added to the device tree
 
 656  * below the device specified by @parent which must be either a Scsi_Host
 
 657  * or a sas_expander_device.
 
 659  * Returns %NULL on error
 
 661 struct sas_port *sas_port_alloc(struct device *parent, int port_id)
 
 663         struct Scsi_Host *shost = dev_to_shost(parent);
 
 664         struct sas_port *port;
 
 666         port = kzalloc(sizeof(*port), GFP_KERNEL);
 
 670         port->port_identifier = port_id;
 
 672         device_initialize(&port->dev);
 
 674         port->dev.parent = get_device(parent);
 
 675         port->dev.release = sas_port_release;
 
 677         mutex_init(&port->phy_list_mutex);
 
 678         INIT_LIST_HEAD(&port->phy_list);
 
 680         if (scsi_is_sas_expander_device(parent)) {
 
 681                 struct sas_rphy *rphy = dev_to_rphy(parent);
 
 682                 sprintf(port->dev.bus_id, "port-%d:%d:%d", shost->host_no,
 
 683                         rphy->scsi_target_id, port->port_identifier);
 
 685                 sprintf(port->dev.bus_id, "port-%d:%d", shost->host_no,
 
 686                         port->port_identifier);
 
 688         transport_setup_device(&port->dev);
 
 692 EXPORT_SYMBOL(sas_port_alloc);
 
 694 /** sas_port_alloc_num - allocate and initialize a SAS port structure
 
 696  * @parent:     parent device
 
 698  * Allocates a SAS port structure and a number to go with it.  This
 
 699  * interface is really for adapters where the port number has no
 
 700  * meansing, so the sas class should manage them.  It will be added to
 
 701  * the device tree below the device specified by @parent which must be
 
 702  * either a Scsi_Host or a sas_expander_device.
 
 704  * Returns %NULL on error
 
 706 struct sas_port *sas_port_alloc_num(struct device *parent)
 
 709         struct Scsi_Host *shost = dev_to_shost(parent);
 
 710         struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
 
 712         /* FIXME: use idr for this eventually */
 
 713         mutex_lock(&sas_host->lock);
 
 714         if (scsi_is_sas_expander_device(parent)) {
 
 715                 struct sas_rphy *rphy = dev_to_rphy(parent);
 
 716                 struct sas_expander_device *exp = rphy_to_expander_device(rphy);
 
 718                 index = exp->next_port_id++;
 
 720                 index = sas_host->next_port_id++;
 
 721         mutex_unlock(&sas_host->lock);
 
 722         return sas_port_alloc(parent, index);
 
 724 EXPORT_SYMBOL(sas_port_alloc_num);
 
 727  * sas_port_add - add a SAS port to the device hierarchy
 
 729  * @port:       port to be added
 
 731  * publishes a port to the rest of the system
 
 733 int sas_port_add(struct sas_port *port)
 
 737         /* No phys should be added until this is made visible */
 
 738         BUG_ON(!list_empty(&port->phy_list));
 
 740         error = device_add(&port->dev);
 
 745         transport_add_device(&port->dev);
 
 746         transport_configure_device(&port->dev);
 
 750 EXPORT_SYMBOL(sas_port_add);
 
 753  * sas_port_free  --  free a SAS PORT
 
 754  * @port:       SAS PORT to free
 
 756  * Frees the specified SAS PORT.
 
 759  *   This function must only be called on a PORT that has not
 
 760  *   sucessfully been added using sas_port_add().
 
 762 void sas_port_free(struct sas_port *port)
 
 764         transport_destroy_device(&port->dev);
 
 765         put_device(&port->dev);
 
 767 EXPORT_SYMBOL(sas_port_free);
 
 770  * sas_port_delete  --  remove SAS PORT
 
 771  * @port:       SAS PORT to remove
 
 773  * Removes the specified SAS PORT.  If the SAS PORT has an
 
 774  * associated phys, unlink them from the port as well.
 
 776 void sas_port_delete(struct sas_port *port)
 
 778         struct device *dev = &port->dev;
 
 779         struct sas_phy *phy, *tmp_phy;
 
 782                 sas_rphy_delete(port->rphy);
 
 786         mutex_lock(&port->phy_list_mutex);
 
 787         list_for_each_entry_safe(phy, tmp_phy, &port->phy_list,
 
 789                 sas_port_delete_link(port, phy);
 
 790                 list_del_init(&phy->port_siblings);
 
 792         mutex_unlock(&port->phy_list_mutex);
 
 794         if (port->is_backlink) {
 
 795                 struct device *parent = port->dev.parent;
 
 797                 sysfs_remove_link(&port->dev.kobj, parent->bus_id);
 
 798                 port->is_backlink = 0;
 
 801         transport_remove_device(dev);
 
 803         transport_destroy_device(dev);
 
 806 EXPORT_SYMBOL(sas_port_delete);
 
 809  * scsi_is_sas_port --  check if a struct device represents a SAS port
 
 810  * @dev:        device to check
 
 813  *      %1 if the device represents a SAS Port, %0 else
 
 815 int scsi_is_sas_port(const struct device *dev)
 
 817         return dev->release == sas_port_release;
 
 819 EXPORT_SYMBOL(scsi_is_sas_port);
 
 822  * sas_port_add_phy - add another phy to a port to form a wide port
 
 823  * @port:       port to add the phy to
 
 826  * When a port is initially created, it is empty (has no phys).  All
 
 827  * ports must have at least one phy to operated, and all wide ports
 
 828  * must have at least two.  The current code makes no difference
 
 829  * between ports and wide ports, but the only object that can be
 
 830  * connected to a remote device is a port, so ports must be formed on
 
 831  * all devices with phys if they're connected to anything.
 
 833 void sas_port_add_phy(struct sas_port *port, struct sas_phy *phy)
 
 835         mutex_lock(&port->phy_list_mutex);
 
 836         if (unlikely(!list_empty(&phy->port_siblings))) {
 
 837                 /* make sure we're already on this port */
 
 840                 list_for_each_entry(tmp, &port->phy_list, port_siblings)
 
 843                 /* If this trips, you added a phy that was already
 
 844                  * part of a different port */
 
 845                 if (unlikely(tmp != phy)) {
 
 846                         dev_printk(KERN_ERR, &port->dev, "trying to add phy %s fails: it's already part of another port\n", phy->dev.bus_id);
 
 850                 sas_port_create_link(port, phy);
 
 851                 list_add_tail(&phy->port_siblings, &port->phy_list);
 
 854         mutex_unlock(&port->phy_list_mutex);
 
 856 EXPORT_SYMBOL(sas_port_add_phy);
 
 859  * sas_port_delete_phy - remove a phy from a port or wide port
 
 860  * @port:       port to remove the phy from
 
 861  * @phy:        phy to remove
 
 863  * This operation is used for tearing down ports again.  It must be
 
 864  * done to every port or wide port before calling sas_port_delete.
 
 866 void sas_port_delete_phy(struct sas_port *port, struct sas_phy *phy)
 
 868         mutex_lock(&port->phy_list_mutex);
 
 869         sas_port_delete_link(port, phy);
 
 870         list_del_init(&phy->port_siblings);
 
 872         mutex_unlock(&port->phy_list_mutex);
 
 874 EXPORT_SYMBOL(sas_port_delete_phy);
 
 876 void sas_port_mark_backlink(struct sas_port *port)
 
 879         struct device *parent = port->dev.parent->parent->parent;
 
 881         if (port->is_backlink)
 
 883         port->is_backlink = 1;
 
 884         res = sysfs_create_link(&port->dev.kobj, &parent->kobj,
 
 890         printk(KERN_ERR "%s: Cannot create port backlink, err=%d\n",
 
 894 EXPORT_SYMBOL(sas_port_mark_backlink);
 
 897  * SAS remote PHY attributes.
 
 900 #define sas_rphy_show_simple(field, name, format_string, cast)          \
 
 902 show_sas_rphy_##name(struct class_device *cdev, char *buf)              \
 
 904         struct sas_rphy *rphy = transport_class_to_rphy(cdev);  \
 
 906         return snprintf(buf, 20, format_string, cast rphy->field);      \
 
 909 #define sas_rphy_simple_attr(field, name, format_string, type)          \
 
 910         sas_rphy_show_simple(field, name, format_string, (type))        \
 
 911 static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO,                       \
 
 912                 show_sas_rphy_##name, NULL)
 
 914 #define sas_rphy_show_protocol(field, name)                             \
 
 916 show_sas_rphy_##name(struct class_device *cdev, char *buf)              \
 
 918         struct sas_rphy *rphy = transport_class_to_rphy(cdev);  \
 
 921                 return snprintf(buf, 20, "none\n");                     \
 
 922         return get_sas_protocol_names(rphy->field, buf);        \
 
 925 #define sas_rphy_protocol_attr(field, name)                             \
 
 926         sas_rphy_show_protocol(field, name)                             \
 
 927 static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO,                       \
 
 928                 show_sas_rphy_##name, NULL)
 
 931 show_sas_rphy_device_type(struct class_device *cdev, char *buf)
 
 933         struct sas_rphy *rphy = transport_class_to_rphy(cdev);
 
 935         if (!rphy->identify.device_type)
 
 936                 return snprintf(buf, 20, "none\n");
 
 937         return get_sas_device_type_names(
 
 938                         rphy->identify.device_type, buf);
 
 941 static SAS_CLASS_DEVICE_ATTR(rphy, device_type, S_IRUGO,
 
 942                 show_sas_rphy_device_type, NULL);
 
 945 show_sas_rphy_enclosure_identifier(struct class_device *cdev, char *buf)
 
 947         struct sas_rphy *rphy = transport_class_to_rphy(cdev);
 
 948         struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
 
 949         struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
 
 950         struct sas_internal *i = to_sas_internal(shost->transportt);
 
 955          * Only devices behind an expander are supported, because the
 
 956          * enclosure identifier is a SMP feature.
 
 958         if (scsi_is_sas_phy_local(phy))
 
 961         error = i->f->get_enclosure_identifier(rphy, &identifier);
 
 964         return sprintf(buf, "0x%llx\n", (unsigned long long)identifier);
 
 967 static SAS_CLASS_DEVICE_ATTR(rphy, enclosure_identifier, S_IRUGO,
 
 968                 show_sas_rphy_enclosure_identifier, NULL);
 
 971 show_sas_rphy_bay_identifier(struct class_device *cdev, char *buf)
 
 973         struct sas_rphy *rphy = transport_class_to_rphy(cdev);
 
 974         struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
 
 975         struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
 
 976         struct sas_internal *i = to_sas_internal(shost->transportt);
 
 979         if (scsi_is_sas_phy_local(phy))
 
 982         val = i->f->get_bay_identifier(rphy);
 
 985         return sprintf(buf, "%d\n", val);
 
 988 static SAS_CLASS_DEVICE_ATTR(rphy, bay_identifier, S_IRUGO,
 
 989                 show_sas_rphy_bay_identifier, NULL);
 
 991 sas_rphy_protocol_attr(identify.initiator_port_protocols,
 
 992                 initiator_port_protocols);
 
 993 sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
 
 994 sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
 
 996 sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
 
 998 /* only need 8 bytes of data plus header (4 or 8) */
 
1001 int sas_read_port_mode_page(struct scsi_device *sdev)
 
1003         char *buffer = kzalloc(BUF_SIZE, GFP_KERNEL), *msdata;
 
1004         struct sas_rphy *rphy = target_to_rphy(sdev->sdev_target);
 
1005         struct sas_end_device *rdev;
 
1006         struct scsi_mode_data mode_data;
 
1009         BUG_ON(rphy->identify.device_type != SAS_END_DEVICE);
 
1011         rdev = rphy_to_end_device(rphy);
 
1016         res = scsi_mode_sense(sdev, 1, 0x19, buffer, BUF_SIZE, 30*HZ, 3,
 
1020         if (!scsi_status_is_good(res))
 
1023         msdata = buffer +  mode_data.header_length +
 
1024                 mode_data.block_descriptor_length;
 
1026         if (msdata - buffer > BUF_SIZE - 8)
 
1031         rdev->ready_led_meaning = msdata[2] & 0x10 ? 1 : 0;
 
1032         rdev->I_T_nexus_loss_timeout = (msdata[4] << 8) + msdata[5];
 
1033         rdev->initiator_response_timeout = (msdata[6] << 8) + msdata[7];
 
1039 EXPORT_SYMBOL(sas_read_port_mode_page);
 
1041 static DECLARE_TRANSPORT_CLASS(sas_end_dev_class,
 
1042                                "sas_end_device", NULL, NULL, NULL);
 
1044 #define sas_end_dev_show_simple(field, name, format_string, cast)       \
 
1046 show_sas_end_dev_##name(struct class_device *cdev, char *buf)           \
 
1048         struct sas_rphy *rphy = transport_class_to_rphy(cdev);          \
 
1049         struct sas_end_device *rdev = rphy_to_end_device(rphy);         \
 
1051         return snprintf(buf, 20, format_string, cast rdev->field);      \
 
1054 #define sas_end_dev_simple_attr(field, name, format_string, type)       \
 
1055         sas_end_dev_show_simple(field, name, format_string, (type))     \
 
1056 static SAS_CLASS_DEVICE_ATTR(end_dev, name, S_IRUGO,                    \
 
1057                 show_sas_end_dev_##name, NULL)
 
1059 sas_end_dev_simple_attr(ready_led_meaning, ready_led_meaning, "%d\n", int);
 
1060 sas_end_dev_simple_attr(I_T_nexus_loss_timeout, I_T_nexus_loss_timeout,
 
1062 sas_end_dev_simple_attr(initiator_response_timeout, initiator_response_timeout,
 
1065 static DECLARE_TRANSPORT_CLASS(sas_expander_class,
 
1066                                "sas_expander", NULL, NULL, NULL);
 
1068 #define sas_expander_show_simple(field, name, format_string, cast)      \
 
1070 show_sas_expander_##name(struct class_device *cdev, char *buf)          \
 
1072         struct sas_rphy *rphy = transport_class_to_rphy(cdev);          \
 
1073         struct sas_expander_device *edev = rphy_to_expander_device(rphy); \
 
1075         return snprintf(buf, 20, format_string, cast edev->field);      \
 
1078 #define sas_expander_simple_attr(field, name, format_string, type)      \
 
1079         sas_expander_show_simple(field, name, format_string, (type))    \
 
1080 static SAS_CLASS_DEVICE_ATTR(expander, name, S_IRUGO,                   \
 
1081                 show_sas_expander_##name, NULL)
 
1083 sas_expander_simple_attr(vendor_id, vendor_id, "%s\n", char *);
 
1084 sas_expander_simple_attr(product_id, product_id, "%s\n", char *);
 
1085 sas_expander_simple_attr(product_rev, product_rev, "%s\n", char *);
 
1086 sas_expander_simple_attr(component_vendor_id, component_vendor_id,
 
1088 sas_expander_simple_attr(component_id, component_id, "%u\n", unsigned int);
 
1089 sas_expander_simple_attr(component_revision_id, component_revision_id, "%u\n",
 
1091 sas_expander_simple_attr(level, level, "%d\n", int);
 
1093 static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
 
1094                 "sas_device", NULL, NULL, NULL);
 
1096 static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
 
1098         struct Scsi_Host *shost;
 
1099         struct sas_internal *i;
 
1101         if (!scsi_is_sas_rphy(dev))
 
1103         shost = dev_to_shost(dev->parent->parent);
 
1105         if (!shost->transportt)
 
1107         if (shost->transportt->host_attrs.ac.class !=
 
1108                         &sas_host_class.class)
 
1111         i = to_sas_internal(shost->transportt);
 
1112         return &i->rphy_attr_cont.ac == cont;
 
1115 static int sas_end_dev_match(struct attribute_container *cont,
 
1118         struct Scsi_Host *shost;
 
1119         struct sas_internal *i;
 
1120         struct sas_rphy *rphy;
 
1122         if (!scsi_is_sas_rphy(dev))
 
1124         shost = dev_to_shost(dev->parent->parent);
 
1125         rphy = dev_to_rphy(dev);
 
1127         if (!shost->transportt)
 
1129         if (shost->transportt->host_attrs.ac.class !=
 
1130                         &sas_host_class.class)
 
1133         i = to_sas_internal(shost->transportt);
 
1134         return &i->end_dev_attr_cont.ac == cont &&
 
1135                 rphy->identify.device_type == SAS_END_DEVICE;
 
1138 static int sas_expander_match(struct attribute_container *cont,
 
1141         struct Scsi_Host *shost;
 
1142         struct sas_internal *i;
 
1143         struct sas_rphy *rphy;
 
1145         if (!scsi_is_sas_rphy(dev))
 
1147         shost = dev_to_shost(dev->parent->parent);
 
1148         rphy = dev_to_rphy(dev);
 
1150         if (!shost->transportt)
 
1152         if (shost->transportt->host_attrs.ac.class !=
 
1153                         &sas_host_class.class)
 
1156         i = to_sas_internal(shost->transportt);
 
1157         return &i->expander_attr_cont.ac == cont &&
 
1158                 (rphy->identify.device_type == SAS_EDGE_EXPANDER_DEVICE ||
 
1159                  rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE);
 
1162 static void sas_expander_release(struct device *dev)
 
1164         struct sas_rphy *rphy = dev_to_rphy(dev);
 
1165         struct sas_expander_device *edev = rphy_to_expander_device(rphy);
 
1167         put_device(dev->parent);
 
1171 static void sas_end_device_release(struct device *dev)
 
1173         struct sas_rphy *rphy = dev_to_rphy(dev);
 
1174         struct sas_end_device *edev = rphy_to_end_device(rphy);
 
1176         put_device(dev->parent);
 
1181  * sas_rphy_initialize - common rphy intialization
 
1182  * @rphy:       rphy to initialise
 
1184  * Used by both sas_end_device_alloc() and sas_expander_alloc() to
 
1185  * initialise the common rphy component of each.
 
1187 static void sas_rphy_initialize(struct sas_rphy *rphy)
 
1189         INIT_LIST_HEAD(&rphy->list);
 
1193  * sas_end_device_alloc - allocate an rphy for an end device
 
1195  * Allocates an SAS remote PHY structure, connected to @parent.
 
1198  *      SAS PHY allocated or %NULL if the allocation failed.
 
1200 struct sas_rphy *sas_end_device_alloc(struct sas_port *parent)
 
1202         struct Scsi_Host *shost = dev_to_shost(&parent->dev);
 
1203         struct sas_end_device *rdev;
 
1205         rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
 
1210         device_initialize(&rdev->rphy.dev);
 
1211         rdev->rphy.dev.parent = get_device(&parent->dev);
 
1212         rdev->rphy.dev.release = sas_end_device_release;
 
1213         if (scsi_is_sas_expander_device(parent->dev.parent)) {
 
1214                 struct sas_rphy *rphy = dev_to_rphy(parent->dev.parent);
 
1215                 sprintf(rdev->rphy.dev.bus_id, "end_device-%d:%d:%d",
 
1216                         shost->host_no, rphy->scsi_target_id, parent->port_identifier);
 
1218                 sprintf(rdev->rphy.dev.bus_id, "end_device-%d:%d",
 
1219                         shost->host_no, parent->port_identifier);
 
1220         rdev->rphy.identify.device_type = SAS_END_DEVICE;
 
1221         sas_rphy_initialize(&rdev->rphy);
 
1222         transport_setup_device(&rdev->rphy.dev);
 
1226 EXPORT_SYMBOL(sas_end_device_alloc);
 
1229  * sas_expander_alloc - allocate an rphy for an end device
 
1231  * Allocates an SAS remote PHY structure, connected to @parent.
 
1234  *      SAS PHY allocated or %NULL if the allocation failed.
 
1236 struct sas_rphy *sas_expander_alloc(struct sas_port *parent,
 
1237                                     enum sas_device_type type)
 
1239         struct Scsi_Host *shost = dev_to_shost(&parent->dev);
 
1240         struct sas_expander_device *rdev;
 
1241         struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
 
1243         BUG_ON(type != SAS_EDGE_EXPANDER_DEVICE &&
 
1244                type != SAS_FANOUT_EXPANDER_DEVICE);
 
1246         rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
 
1251         device_initialize(&rdev->rphy.dev);
 
1252         rdev->rphy.dev.parent = get_device(&parent->dev);
 
1253         rdev->rphy.dev.release = sas_expander_release;
 
1254         mutex_lock(&sas_host->lock);
 
1255         rdev->rphy.scsi_target_id = sas_host->next_expander_id++;
 
1256         mutex_unlock(&sas_host->lock);
 
1257         sprintf(rdev->rphy.dev.bus_id, "expander-%d:%d",
 
1258                 shost->host_no, rdev->rphy.scsi_target_id);
 
1259         rdev->rphy.identify.device_type = type;
 
1260         sas_rphy_initialize(&rdev->rphy);
 
1261         transport_setup_device(&rdev->rphy.dev);
 
1265 EXPORT_SYMBOL(sas_expander_alloc);
 
1268  * sas_rphy_add  --  add a SAS remote PHY to the device hierarchy
 
1269  * @rphy:       The remote PHY to be added
 
1271  * Publishes a SAS remote PHY to the rest of the system.
 
1273 int sas_rphy_add(struct sas_rphy *rphy)
 
1275         struct sas_port *parent = dev_to_sas_port(rphy->dev.parent);
 
1276         struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
 
1277         struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
 
1278         struct sas_identify *identify = &rphy->identify;
 
1283         parent->rphy = rphy;
 
1285         error = device_add(&rphy->dev);
 
1288         transport_add_device(&rphy->dev);
 
1289         transport_configure_device(&rphy->dev);
 
1291         mutex_lock(&sas_host->lock);
 
1292         list_add_tail(&rphy->list, &sas_host->rphy_list);
 
1293         if (identify->device_type == SAS_END_DEVICE &&
 
1294             (identify->target_port_protocols &
 
1295              (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA)))
 
1296                 rphy->scsi_target_id = sas_host->next_target_id++;
 
1297         else if (identify->device_type == SAS_END_DEVICE)
 
1298                 rphy->scsi_target_id = -1;
 
1299         mutex_unlock(&sas_host->lock);
 
1301         if (identify->device_type == SAS_END_DEVICE &&
 
1302             rphy->scsi_target_id != -1) {
 
1303                 scsi_scan_target(&rphy->dev, 0,
 
1304                                 rphy->scsi_target_id, SCAN_WILD_CARD, 0);
 
1309 EXPORT_SYMBOL(sas_rphy_add);
 
1312  * sas_rphy_free  --  free a SAS remote PHY
 
1313  * @rphy        SAS remote PHY to free
 
1315  * Frees the specified SAS remote PHY.
 
1318  *   This function must only be called on a remote
 
1319  *   PHY that has not sucessfully been added using
 
1320  *   sas_rphy_add() (or has been sas_rphy_remove()'d)
 
1322 void sas_rphy_free(struct sas_rphy *rphy)
 
1324         struct device *dev = &rphy->dev;
 
1325         struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
 
1326         struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
 
1328         mutex_lock(&sas_host->lock);
 
1329         list_del(&rphy->list);
 
1330         mutex_unlock(&sas_host->lock);
 
1332         transport_destroy_device(dev);
 
1336 EXPORT_SYMBOL(sas_rphy_free);
 
1339  * sas_rphy_delete  --  remove and free SAS remote PHY
 
1340  * @rphy:       SAS remote PHY to remove and free
 
1342  * Removes the specified SAS remote PHY and frees it.
 
1345 sas_rphy_delete(struct sas_rphy *rphy)
 
1347         sas_rphy_remove(rphy);
 
1348         sas_rphy_free(rphy);
 
1350 EXPORT_SYMBOL(sas_rphy_delete);
 
1353  * sas_rphy_remove  --  remove SAS remote PHY
 
1354  * @rphy:       SAS remote phy to remove
 
1356  * Removes the specified SAS remote PHY.
 
1359 sas_rphy_remove(struct sas_rphy *rphy)
 
1361         struct device *dev = &rphy->dev;
 
1362         struct sas_port *parent = dev_to_sas_port(dev->parent);
 
1364         switch (rphy->identify.device_type) {
 
1365         case SAS_END_DEVICE:
 
1366                 scsi_remove_target(dev);
 
1368         case SAS_EDGE_EXPANDER_DEVICE:
 
1369         case SAS_FANOUT_EXPANDER_DEVICE:
 
1370                 sas_remove_children(dev);
 
1376         transport_remove_device(dev);
 
1379         parent->rphy = NULL;
 
1381 EXPORT_SYMBOL(sas_rphy_remove);
 
1384  * scsi_is_sas_rphy  --  check if a struct device represents a SAS remote PHY
 
1385  * @dev:        device to check
 
1388  *      %1 if the device represents a SAS remote PHY, %0 else
 
1390 int scsi_is_sas_rphy(const struct device *dev)
 
1392         return dev->release == sas_end_device_release ||
 
1393                 dev->release == sas_expander_release;
 
1395 EXPORT_SYMBOL(scsi_is_sas_rphy);
 
1402 static int sas_user_scan(struct Scsi_Host *shost, uint channel,
 
1405         struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
 
1406         struct sas_rphy *rphy;
 
1408         mutex_lock(&sas_host->lock);
 
1409         list_for_each_entry(rphy, &sas_host->rphy_list, list) {
 
1410                 if (rphy->identify.device_type != SAS_END_DEVICE ||
 
1411                     rphy->scsi_target_id == -1)
 
1414                 if ((channel == SCAN_WILD_CARD || channel == 0) &&
 
1415                     (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) {
 
1416                         scsi_scan_target(&rphy->dev, 0,
 
1417                                          rphy->scsi_target_id, lun, 1);
 
1420         mutex_unlock(&sas_host->lock);
 
1427  * Setup / Teardown code
 
1430 #define SETUP_TEMPLATE(attrb, field, perm, test)                        \
 
1431         i->private_##attrb[count] = class_device_attr_##field;          \
 
1432         i->private_##attrb[count].attr.mode = perm;                     \
 
1433         i->attrb[count] = &i->private_##attrb[count];                   \
 
1437 #define SETUP_TEMPLATE_RW(attrb, field, perm, test, ro_test, ro_perm)   \
 
1438         i->private_##attrb[count] = class_device_attr_##field;          \
 
1439         i->private_##attrb[count].attr.mode = perm;                     \
 
1441                 i->private_##attrb[count].attr.mode = ro_perm;          \
 
1442                 i->private_##attrb[count].store = NULL;                 \
 
1444         i->attrb[count] = &i->private_##attrb[count];                   \
 
1448 #define SETUP_RPORT_ATTRIBUTE(field)                                    \
 
1449         SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1)
 
1451 #define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func)                     \
 
1452         SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, i->f->func)
 
1454 #define SETUP_PHY_ATTRIBUTE(field)                                      \
 
1455         SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1)
 
1457 #define SETUP_PHY_ATTRIBUTE_RW(field)                                   \
 
1458         SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1,       \
 
1459                         !i->f->set_phy_speed, S_IRUGO)
 
1461 #define SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(field, func)                    \
 
1462         SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1,       \
 
1463                           !i->f->func, S_IRUGO)
 
1465 #define SETUP_PORT_ATTRIBUTE(field)                                     \
 
1466         SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
 
1468 #define SETUP_OPTIONAL_PHY_ATTRIBUTE(field, func)                       \
 
1469         SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, i->f->func)
 
1471 #define SETUP_PHY_ATTRIBUTE_WRONLY(field)                               \
 
1472         SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, 1)
 
1474 #define SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(field, func)                \
 
1475         SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, i->f->func)
 
1477 #define SETUP_END_DEV_ATTRIBUTE(field)                                  \
 
1478         SETUP_TEMPLATE(end_dev_attrs, field, S_IRUGO, 1)
 
1480 #define SETUP_EXPANDER_ATTRIBUTE(field)                                 \
 
1481         SETUP_TEMPLATE(expander_attrs, expander_##field, S_IRUGO, 1)
 
1484  * sas_attach_transport  --  instantiate SAS transport template
 
1485  * @ft:         SAS transport class function template
 
1487 struct scsi_transport_template *
 
1488 sas_attach_transport(struct sas_function_template *ft)
 
1490         struct sas_internal *i;
 
1493         i = kzalloc(sizeof(struct sas_internal), GFP_KERNEL);
 
1497         i->t.user_scan = sas_user_scan;
 
1499         i->t.host_attrs.ac.attrs = &i->host_attrs[0];
 
1500         i->t.host_attrs.ac.class = &sas_host_class.class;
 
1501         i->t.host_attrs.ac.match = sas_host_match;
 
1502         transport_container_register(&i->t.host_attrs);
 
1503         i->t.host_size = sizeof(struct sas_host_attrs);
 
1505         i->phy_attr_cont.ac.class = &sas_phy_class.class;
 
1506         i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
 
1507         i->phy_attr_cont.ac.match = sas_phy_match;
 
1508         transport_container_register(&i->phy_attr_cont);
 
1510         i->port_attr_cont.ac.class = &sas_port_class.class;
 
1511         i->port_attr_cont.ac.attrs = &i->port_attrs[0];
 
1512         i->port_attr_cont.ac.match = sas_port_match;
 
1513         transport_container_register(&i->port_attr_cont);
 
1515         i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
 
1516         i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
 
1517         i->rphy_attr_cont.ac.match = sas_rphy_match;
 
1518         transport_container_register(&i->rphy_attr_cont);
 
1520         i->end_dev_attr_cont.ac.class = &sas_end_dev_class.class;
 
1521         i->end_dev_attr_cont.ac.attrs = &i->end_dev_attrs[0];
 
1522         i->end_dev_attr_cont.ac.match = sas_end_dev_match;
 
1523         transport_container_register(&i->end_dev_attr_cont);
 
1525         i->expander_attr_cont.ac.class = &sas_expander_class.class;
 
1526         i->expander_attr_cont.ac.attrs = &i->expander_attrs[0];
 
1527         i->expander_attr_cont.ac.match = sas_expander_match;
 
1528         transport_container_register(&i->expander_attr_cont);
 
1533         SETUP_PORT_ATTRIBUTE(num_phys);
 
1534         i->host_attrs[count] = NULL;
 
1537         SETUP_PHY_ATTRIBUTE(initiator_port_protocols);
 
1538         SETUP_PHY_ATTRIBUTE(target_port_protocols);
 
1539         SETUP_PHY_ATTRIBUTE(device_type);
 
1540         SETUP_PHY_ATTRIBUTE(sas_address);
 
1541         SETUP_PHY_ATTRIBUTE(phy_identifier);
 
1542         //SETUP_PHY_ATTRIBUTE(port_identifier);
 
1543         SETUP_PHY_ATTRIBUTE(negotiated_linkrate);
 
1544         SETUP_PHY_ATTRIBUTE(minimum_linkrate_hw);
 
1545         SETUP_PHY_ATTRIBUTE_RW(minimum_linkrate);
 
1546         SETUP_PHY_ATTRIBUTE(maximum_linkrate_hw);
 
1547         SETUP_PHY_ATTRIBUTE_RW(maximum_linkrate);
 
1549         SETUP_PHY_ATTRIBUTE(invalid_dword_count);
 
1550         SETUP_PHY_ATTRIBUTE(running_disparity_error_count);
 
1551         SETUP_PHY_ATTRIBUTE(loss_of_dword_sync_count);
 
1552         SETUP_PHY_ATTRIBUTE(phy_reset_problem_count);
 
1553         SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(link_reset, phy_reset);
 
1554         SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(hard_reset, phy_reset);
 
1555         SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(enable, phy_enable);
 
1556         i->phy_attrs[count] = NULL;
 
1559         SETUP_PORT_ATTRIBUTE(num_phys);
 
1560         i->port_attrs[count] = NULL;
 
1563         SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
 
1564         SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
 
1565         SETUP_RPORT_ATTRIBUTE(rphy_device_type);
 
1566         SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
 
1567         SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
 
1568         SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_enclosure_identifier,
 
1569                                        get_enclosure_identifier);
 
1570         SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_bay_identifier,
 
1571                                        get_bay_identifier);
 
1572         i->rphy_attrs[count] = NULL;
 
1575         SETUP_END_DEV_ATTRIBUTE(end_dev_ready_led_meaning);
 
1576         SETUP_END_DEV_ATTRIBUTE(end_dev_I_T_nexus_loss_timeout);
 
1577         SETUP_END_DEV_ATTRIBUTE(end_dev_initiator_response_timeout);
 
1578         i->end_dev_attrs[count] = NULL;
 
1581         SETUP_EXPANDER_ATTRIBUTE(vendor_id);
 
1582         SETUP_EXPANDER_ATTRIBUTE(product_id);
 
1583         SETUP_EXPANDER_ATTRIBUTE(product_rev);
 
1584         SETUP_EXPANDER_ATTRIBUTE(component_vendor_id);
 
1585         SETUP_EXPANDER_ATTRIBUTE(component_id);
 
1586         SETUP_EXPANDER_ATTRIBUTE(component_revision_id);
 
1587         SETUP_EXPANDER_ATTRIBUTE(level);
 
1588         i->expander_attrs[count] = NULL;
 
1592 EXPORT_SYMBOL(sas_attach_transport);
 
1595  * sas_release_transport  --  release SAS transport template instance
 
1596  * @t:          transport template instance
 
1598 void sas_release_transport(struct scsi_transport_template *t)
 
1600         struct sas_internal *i = to_sas_internal(t);
 
1602         transport_container_unregister(&i->t.host_attrs);
 
1603         transport_container_unregister(&i->phy_attr_cont);
 
1604         transport_container_unregister(&i->port_attr_cont);
 
1605         transport_container_unregister(&i->rphy_attr_cont);
 
1606         transport_container_unregister(&i->end_dev_attr_cont);
 
1607         transport_container_unregister(&i->expander_attr_cont);
 
1611 EXPORT_SYMBOL(sas_release_transport);
 
1613 static __init int sas_transport_init(void)
 
1617         error = transport_class_register(&sas_host_class);
 
1620         error = transport_class_register(&sas_phy_class);
 
1622                 goto out_unregister_transport;
 
1623         error = transport_class_register(&sas_port_class);
 
1625                 goto out_unregister_phy;
 
1626         error = transport_class_register(&sas_rphy_class);
 
1628                 goto out_unregister_port;
 
1629         error = transport_class_register(&sas_end_dev_class);
 
1631                 goto out_unregister_rphy;
 
1632         error = transport_class_register(&sas_expander_class);
 
1634                 goto out_unregister_end_dev;
 
1638  out_unregister_end_dev:
 
1639         transport_class_unregister(&sas_end_dev_class);
 
1640  out_unregister_rphy:
 
1641         transport_class_unregister(&sas_rphy_class);
 
1642  out_unregister_port:
 
1643         transport_class_unregister(&sas_port_class);
 
1645         transport_class_unregister(&sas_phy_class);
 
1646  out_unregister_transport:
 
1647         transport_class_unregister(&sas_host_class);
 
1653 static void __exit sas_transport_exit(void)
 
1655         transport_class_unregister(&sas_host_class);
 
1656         transport_class_unregister(&sas_phy_class);
 
1657         transport_class_unregister(&sas_port_class);
 
1658         transport_class_unregister(&sas_rphy_class);
 
1659         transport_class_unregister(&sas_end_dev_class);
 
1660         transport_class_unregister(&sas_expander_class);
 
1663 MODULE_AUTHOR("Christoph Hellwig");
 
1664 MODULE_DESCRIPTION("SAS Transport Attributes");
 
1665 MODULE_LICENSE("GPL");
 
1667 module_init(sas_transport_init);
 
1668 module_exit(sas_transport_exit);