27 #include <linux/module.h>
30 #include <linux/slab.h>
31 #include <linux/string.h>
33 #include <linux/bsg.h>
35 #include <scsi/scsi.h>
50 #define to_sas_host_attrs(host) ((struct sas_host_attrs *)(host)->shost_data)
56 #define SAS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
57 struct device_attribute dev_attr_##_prefix##_##_name = \
58 __ATTR(_name,_mode,_show,_store)
65 #define sas_bitfield_name_match(title, table) \
67 get_sas_##title##_names(u32 table_key, char *buf) \
73 for (i = 0; i < ARRAY_SIZE(table); i++) { \
74 if (table[i].value & table_key) { \
75 len += sprintf(buf + len, "%s%s", \
76 prefix, table[i].name); \
80 len += sprintf(buf + len, "\n"); \
84 #define sas_bitfield_name_set(title, table) \
86 set_sas_##title##_names(u32 *table_key, const char *buf) \
91 for (i = 0; i < ARRAY_SIZE(table); i++) { \
92 len = strlen(table[i].name); \
93 if (strncmp(buf, table[i].name, len) == 0 && \
94 (buf[len] == '\n' || buf[len] == '\0')) { \
95 *table_key = table[i].value; \
102 #define sas_bitfield_name_search(title, table) \
104 get_sas_##title##_names(u32 table_key, char *buf) \
109 for (i = 0; i < ARRAY_SIZE(table); i++) { \
110 if (table[i].value == table_key) { \
111 len += sprintf(buf + len, "%s", \
116 len += sprintf(buf + len, "\n"); \
123 } sas_device_type_names[] = {
177 spin_unlock_irq(q->queue_lock);
180 ret =
handler(shost, rphy, req);
185 spin_lock_irq(q->queue_lock);
194 static void sas_non_host_smp_request(
struct request_queue *q)
196 struct sas_rphy *rphy = q->queuedata;
200 static void sas_host_release(
struct device *
dev)
202 struct Scsi_Host *shost = dev_to_shost(dev);
220 printk(
"%s can't handle SMP requests\n", shost->
hostt->name);
227 name = dev_name(dev);
231 dev = &shost->shost_gendev;
254 q->queuedata =
shost;
256 queue_flag_set_unlocked(QUEUE_FLAG_BIDI, q);
282 struct Scsi_Host *shost = dev_to_shost(dev);
291 if (sas_bsg_initialize(shost,
NULL))
292 dev_printk(
KERN_ERR, dev,
"fail to a bsg device %d\n",
301 struct Scsi_Host *shost = dev_to_shost(dev);
303 sas_bsg_remove(shost,
NULL);
309 "sas_host", sas_host_setup, sas_host_remove,
NULL);
319 shost = dev_to_shost(dev);
324 &sas_host_class.class)
328 return &i->
t.host_attrs.ac ==
cont;
331 static int do_sas_phy_delete(
struct device *dev,
void *
data)
333 int pass = (
int)(
unsigned long)
data;
379 const int vpd_len = 32;
393 ret = buffer[12] & 0x01;
452 #define sas_phy_show_simple(field, name, format_string, cast) \
454 show_sas_phy_##name(struct device *dev, \
455 struct device_attribute *attr, char *buf) \
457 struct sas_phy *phy = transport_class_to_phy(dev); \
459 return snprintf(buf, 20, format_string, cast phy->field); \
462 #define sas_phy_simple_attr(field, name, format_string, type) \
463 sas_phy_show_simple(field, name, format_string, (type)) \
464 static DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
466 #define sas_phy_show_protocol(field, name) \
468 show_sas_phy_##name(struct device *dev, \
469 struct device_attribute *attr, char *buf) \
471 struct sas_phy *phy = transport_class_to_phy(dev); \
474 return snprintf(buf, 20, "none\n"); \
475 return get_sas_protocol_names(phy->field, buf); \
478 #define sas_phy_protocol_attr(field, name) \
479 sas_phy_show_protocol(field, name) \
480 static DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
482 #define sas_phy_show_linkspeed(field) \
484 show_sas_phy_##field(struct device *dev, \
485 struct device_attribute *attr, char *buf) \
487 struct sas_phy *phy = transport_class_to_phy(dev); \
489 return get_sas_linkspeed_names(phy->field, buf); \
493 #define sas_phy_store_linkspeed(field) \
495 store_sas_phy_##field(struct device *dev, \
496 struct device_attribute *attr, \
497 const char *buf, size_t count) \
499 struct sas_phy *phy = transport_class_to_phy(dev); \
500 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \
501 struct sas_internal *i = to_sas_internal(shost->transportt); \
503 struct sas_phy_linkrates rates = {0}; \
506 error = set_sas_linkspeed_names(&value, buf); \
509 rates.field = value; \
510 error = i->f->set_phy_speed(phy, &rates); \
512 return error ? error : count; \
515 #define sas_phy_linkspeed_rw_attr(field) \
516 sas_phy_show_linkspeed(field) \
517 sas_phy_store_linkspeed(field) \
518 static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, \
519 store_sas_phy_##field)
521 #define sas_phy_linkspeed_attr(field) \
522 sas_phy_show_linkspeed(field) \
523 static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
526 #define sas_phy_show_linkerror(field) \
528 show_sas_phy_##field(struct device *dev, \
529 struct device_attribute *attr, char *buf) \
531 struct sas_phy *phy = transport_class_to_phy(dev); \
532 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \
533 struct sas_internal *i = to_sas_internal(shost->transportt); \
536 error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0; \
539 return snprintf(buf, 20, "%u\n", phy->field); \
542 #define sas_phy_linkerror_attr(field) \
543 sas_phy_show_linkerror(field) \
544 static DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
548 show_sas_device_type(
struct device *dev,
555 return get_sas_device_type_names(phy->
identify.device_type, buf);
563 struct Scsi_Host *shost = dev_to_shost(phy->
dev.parent);
567 error = i->
f->phy_enable(phy, enable);
576 const char *
buf,
size_t count)
583 do_sas_phy_enable(dev, count, 0);
586 do_sas_phy_enable(dev, count, 1);
605 store_sas_phy_enable);
608 do_sas_phy_reset(
struct device *dev,
size_t count,
int hard_reset)
611 struct Scsi_Host *shost = dev_to_shost(phy->
dev.parent);
615 error = i->
f->phy_reset(phy, hard_reset);
624 const char *buf,
size_t count)
626 return do_sas_phy_reset(dev, count, 0);
632 const char *buf,
size_t count)
634 return do_sas_phy_reset(dev, count, 1);
639 initiator_port_protocols);
641 target_port_protocols);
660 struct Scsi_Host *shost = dev_to_shost(phy->
dev.parent);
664 i->
f->phy_setup(phy);
670 "sas_phy", sas_phy_setup,
NULL,
NULL);
679 shost = dev_to_shost(dev->
parent);
684 &sas_host_class.class)
691 static void sas_phy_release(
struct device *dev)
694 struct Scsi_Host *shost = dev_to_shost(phy->
dev.parent);
697 if (i->
f->phy_release)
698 i->
f->phy_release(phy);
717 struct Scsi_Host *shost = dev_to_shost(parent);
729 phy->
dev.release = sas_phy_release;
731 if (scsi_is_sas_expander_device(parent)) {
812 return dev->
release == sas_phy_release;
819 #define sas_port_show_simple(field, name, format_string, cast) \
821 show_sas_port_##name(struct device *dev, \
822 struct device_attribute *attr, char *buf) \
824 struct sas_port *port = transport_class_to_sas_port(dev); \
826 return snprintf(buf, 20, format_string, cast port->field); \
829 #define sas_port_simple_attr(field, name, format_string, type) \
830 sas_port_show_simple(field, name, format_string, (type)) \
831 static DEVICE_ATTR(name, S_IRUGO, show_sas_port_##name, NULL)
845 shost = dev_to_shost(dev->
parent);
850 &sas_host_class.class)
858 static void sas_port_release(
struct device *dev)
868 static void sas_port_create_link(
struct sas_port *port,
874 dev_name(&phy->
dev));
886 static void sas_port_delete_link(
struct sas_port *port,
906 struct Scsi_Host *shost = dev_to_shost(parent);
918 port->
dev.release = sas_port_release;
923 if (scsi_is_sas_expander_device(parent)) {
952 struct Scsi_Host *shost = dev_to_shost(parent);
957 if (scsi_is_sas_expander_device(parent)) {
1031 sas_port_delete_link(port, phy);
1059 return dev->
release == sas_port_release;
1111 dev_printk(
KERN_ERR, &port->
dev,
"trying to add phy %s fails: it's already part of another port\n",
1112 dev_name(&phy->
dev));
1116 sas_port_create_link(port, phy);
1135 sas_port_delete_link(port, phy);
1166 #define sas_rphy_show_simple(field, name, format_string, cast) \
1168 show_sas_rphy_##name(struct device *dev, \
1169 struct device_attribute *attr, char *buf) \
1171 struct sas_rphy *rphy = transport_class_to_rphy(dev); \
1173 return snprintf(buf, 20, format_string, cast rphy->field); \
1176 #define sas_rphy_simple_attr(field, name, format_string, type) \
1177 sas_rphy_show_simple(field, name, format_string, (type)) \
1178 static SAS_DEVICE_ATTR(rphy, name, S_IRUGO, \
1179 show_sas_rphy_##name, NULL)
1181 #define sas_rphy_show_protocol(field, name) \
1183 show_sas_rphy_##name(struct device *dev, \
1184 struct device_attribute *attr, char *buf) \
1186 struct sas_rphy *rphy = transport_class_to_rphy(dev); \
1189 return snprintf(buf, 20, "none\n"); \
1190 return get_sas_protocol_names(rphy->field, buf); \
1193 #define sas_rphy_protocol_attr(field, name) \
1194 sas_rphy_show_protocol(field, name) \
1195 static SAS_DEVICE_ATTR(rphy, name, S_IRUGO, \
1196 show_sas_rphy_##name, NULL)
1199 show_sas_rphy_device_type(
struct device *dev,
1205 return snprintf(buf, 20,
"none\n");
1206 return get_sas_device_type_names(
1211 show_sas_rphy_device_type,
NULL);
1214 show_sas_rphy_enclosure_identifier(
struct device *dev,
1219 struct Scsi_Host *shost = dev_to_shost(phy->
dev.parent);
1231 error = i->
f->get_enclosure_identifier(rphy, &identifier);
1234 return sprintf(buf,
"0x%llx\n", (
unsigned long long)identifier);
1238 show_sas_rphy_enclosure_identifier,
NULL);
1241 show_sas_rphy_bay_identifier(
struct device *dev,
1246 struct Scsi_Host *shost = dev_to_shost(phy->
dev.parent);
1253 val = i->
f->get_bay_identifier(rphy);
1256 return sprintf(buf,
"%d\n", val);
1260 show_sas_rphy_bay_identifier,
NULL);
1263 initiator_port_protocols);
1266 unsigned long long);
1286 if (!scsi_status_is_good(res))
1292 if (msdata - buffer >
BUF_SIZE - 8)
1310 #define sas_end_dev_show_simple(field, name, format_string, cast) \
1312 show_sas_end_dev_##name(struct device *dev, \
1313 struct device_attribute *attr, char *buf) \
1315 struct sas_rphy *rphy = transport_class_to_rphy(dev); \
1316 struct sas_end_device *rdev = rphy_to_end_device(rphy); \
1318 return snprintf(buf, 20, format_string, cast rdev->field); \
1321 #define sas_end_dev_simple_attr(field, name, format_string, type) \
1322 sas_end_dev_show_simple(field, name, format_string, (type)) \
1323 static SAS_DEVICE_ATTR(end_dev, name, S_IRUGO, \
1324 show_sas_end_dev_##name, NULL)
1339 #define sas_expander_show_simple(field, name, format_string, cast) \
1341 show_sas_expander_##name(struct device *dev, \
1342 struct device_attribute *attr, char *buf) \
1344 struct sas_rphy *rphy = transport_class_to_rphy(dev); \
1345 struct sas_expander_device *edev = rphy_to_expander_device(rphy); \
1347 return snprintf(buf, 20, format_string, cast edev->field); \
1350 #define sas_expander_simple_attr(field, name, format_string, type) \
1351 sas_expander_show_simple(field, name, format_string, (type)) \
1352 static SAS_DEVICE_ATTR(expander, name, S_IRUGO, \
1353 show_sas_expander_##name, NULL)
1375 shost = dev_to_shost(dev->
parent->parent);
1379 if (shost->
transportt->host_attrs.ac.class !=
1380 &sas_host_class.class)
1396 shost = dev_to_shost(dev->
parent->parent);
1401 if (shost->
transportt->host_attrs.ac.class !=
1402 &sas_host_class.class)
1419 shost = dev_to_shost(dev->
parent->parent);
1424 if (shost->
transportt->host_attrs.ac.class !=
1425 &sas_host_class.class)
1434 static void sas_expander_release(
struct device *dev)
1446 static void sas_end_device_release(
struct device *dev)
1465 static void sas_rphy_initialize(
struct sas_rphy *rphy)
1467 INIT_LIST_HEAD(&rphy->
list);
1491 rdev->
rphy.dev.release = sas_end_device_release;
1492 if (scsi_is_sas_expander_device(parent->
dev.parent)) {
1501 sas_rphy_initialize(&rdev->
rphy);
1535 rdev->
rphy.dev.release = sas_expander_release;
1541 rdev->
rphy.identify.device_type =
type;
1542 sas_rphy_initialize(&rdev->
rphy);
1558 struct Scsi_Host *shost = dev_to_shost(parent->
dev.parent);
1565 parent->
rphy = rphy;
1572 if (sas_bsg_initialize(shost, rphy))
1573 printk(
"fail to a bsg device %s\n", dev_name(&rphy->
dev));
1616 struct Scsi_Host *shost = dev_to_shost(rphy->
dev.parent->parent);
1623 sas_bsg_remove(shost, rphy);
1670 switch (rphy->
identify.device_type) {
1697 return dev->
release == sas_end_device_release ||
1698 dev->
release == sas_expander_release;
1735 #define SETUP_TEMPLATE(attrb, field, perm, test) \
1736 i->private_##attrb[count] = dev_attr_##field; \
1737 i->private_##attrb[count].attr.mode = perm; \
1738 i->attrb[count] = &i->private_##attrb[count]; \
1742 #define SETUP_TEMPLATE_RW(attrb, field, perm, test, ro_test, ro_perm) \
1743 i->private_##attrb[count] = dev_attr_##field; \
1744 i->private_##attrb[count].attr.mode = perm; \
1746 i->private_##attrb[count].attr.mode = ro_perm; \
1747 i->private_##attrb[count].store = NULL; \
1749 i->attrb[count] = &i->private_##attrb[count]; \
1753 #define SETUP_RPORT_ATTRIBUTE(field) \
1754 SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1)
1756 #define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func) \
1757 SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, i->f->func)
1759 #define SETUP_PHY_ATTRIBUTE(field) \
1760 SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1)
1762 #define SETUP_PHY_ATTRIBUTE_RW(field) \
1763 SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1, \
1764 !i->f->set_phy_speed, S_IRUGO)
1766 #define SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(field, func) \
1767 SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1, \
1768 !i->f->func, S_IRUGO)
1770 #define SETUP_PORT_ATTRIBUTE(field) \
1771 SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
1773 #define SETUP_OPTIONAL_PHY_ATTRIBUTE(field, func) \
1774 SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, i->f->func)
1776 #define SETUP_PHY_ATTRIBUTE_WRONLY(field) \
1777 SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, 1)
1779 #define SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(field, func) \
1780 SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, i->f->func)
1782 #define SETUP_END_DEV_ATTRIBUTE(field) \
1783 SETUP_TEMPLATE(end_dev_attrs, field, S_IRUGO, 1)
1785 #define SETUP_EXPANDER_ATTRIBUTE(field) \
1786 SETUP_TEMPLATE(expander_attrs, expander_##field, S_IRUGO, 1)
1802 i->
t.user_scan = sas_user_scan;
1805 i->
t.host_attrs.ac.class = &sas_host_class.class;
1806 i->
t.host_attrs.ac.match = sas_host_match;
1807 transport_container_register(&i->
t.host_attrs);
1870 get_enclosure_identifier);
1872 get_bay_identifier);
1905 transport_container_unregister(&i->
t.host_attrs);
1916 static __init int sas_transport_init(
void)
1925 goto out_unregister_transport;
1928 goto out_unregister_phy;
1931 goto out_unregister_port;
1934 goto out_unregister_rphy;
1937 goto out_unregister_end_dev;
1941 out_unregister_end_dev:
1943 out_unregister_rphy:
1945 out_unregister_port:
1949 out_unregister_transport:
1956 static void __exit sas_transport_exit(
void)