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/err.h>
29 #include <linux/slab.h>
30 #include <linux/string.h>
32 #include <scsi/scsi.h>
33 #include <scsi/scsi_device.h>
34 #include <scsi/scsi_host.h>
35 #include <scsi/scsi_transport.h>
36 #include <scsi/scsi_transport_sas.h>
38 #include "scsi_sas_internal.h"
39 struct sas_host_attrs {
40 struct list_head rphy_list;
45 #define to_sas_host_attrs(host) ((struct sas_host_attrs *)(host)->shost_data)
49 * Hack to allow attributes of the same name in different objects.
51 #define SAS_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
52 struct class_device_attribute class_device_attr_##_prefix##_##_name = \
53 __ATTR(_name,_mode,_show,_store)
57 * Pretty printing helpers
60 #define sas_bitfield_name_match(title, table) \
62 get_sas_##title##_names(u32 table_key, char *buf) \
68 for (i = 0; i < ARRAY_SIZE(table); i++) { \
69 if (table[i].value & table_key) { \
70 len += sprintf(buf + len, "%s%s", \
71 prefix, table[i].name); \
75 len += sprintf(buf + len, "\n"); \
79 #define sas_bitfield_name_search(title, table) \
81 get_sas_##title##_names(u32 table_key, char *buf) \
86 for (i = 0; i < ARRAY_SIZE(table); i++) { \
87 if (table[i].value == table_key) { \
88 len += sprintf(buf + len, "%s", \
93 len += sprintf(buf + len, "\n"); \
100 } sas_device_type_names[] = {
101 { SAS_PHY_UNUSED, "unused" },
102 { SAS_END_DEVICE, "end device" },
103 { SAS_EDGE_EXPANDER_DEVICE, "edge expander" },
104 { SAS_FANOUT_EXPANDER_DEVICE, "fanout expander" },
106 sas_bitfield_name_search(device_type, sas_device_type_names)
112 } sas_protocol_names[] = {
113 { SAS_PROTOCOL_SATA, "sata" },
114 { SAS_PROTOCOL_SMP, "smp" },
115 { SAS_PROTOCOL_STP, "stp" },
116 { SAS_PROTOCOL_SSP, "ssp" },
118 sas_bitfield_name_match(protocol, sas_protocol_names)
123 } sas_linkspeed_names[] = {
124 { SAS_LINK_RATE_UNKNOWN, "Unknown" },
125 { SAS_PHY_DISABLED, "Phy disabled" },
126 { SAS_LINK_RATE_FAILED, "Link Rate failed" },
127 { SAS_SATA_SPINUP_HOLD, "Spin-up hold" },
128 { SAS_LINK_RATE_1_5_GBPS, "1.5 Gbit" },
129 { SAS_LINK_RATE_3_0_GBPS, "3.0 Gbit" },
130 { SAS_LINK_RATE_6_0_GBPS, "6.0 Gbit" },
132 sas_bitfield_name_search(linkspeed, sas_linkspeed_names)
136 * SAS host attributes
139 static int sas_host_setup(struct transport_container *tc, struct device *dev,
140 struct class_device *cdev)
142 struct Scsi_Host *shost = dev_to_shost(dev);
143 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
145 INIT_LIST_HEAD(&sas_host->rphy_list);
146 mutex_init(&sas_host->lock);
147 sas_host->next_target_id = 0;
148 sas_host->next_expander_id = 0;
152 static DECLARE_TRANSPORT_CLASS(sas_host_class,
153 "sas_host", sas_host_setup, NULL, NULL);
155 static int sas_host_match(struct attribute_container *cont,
158 struct Scsi_Host *shost;
159 struct sas_internal *i;
161 if (!scsi_is_host_device(dev))
163 shost = dev_to_shost(dev);
165 if (!shost->transportt)
167 if (shost->transportt->host_attrs.ac.class !=
168 &sas_host_class.class)
171 i = to_sas_internal(shost->transportt);
172 return &i->t.host_attrs.ac == cont;
175 static int do_sas_phy_delete(struct device *dev, void *data)
177 int pass = (int)(unsigned long)data;
179 if (pass == 0 && scsi_is_sas_port(dev))
180 sas_port_delete(dev_to_sas_port(dev));
181 else if (pass == 1 && scsi_is_sas_phy(dev))
182 sas_phy_delete(dev_to_phy(dev));
187 * sas_remove_children -- tear down a devices SAS data structures
188 * @dev: device belonging to the sas object
190 * Removes all SAS PHYs and remote PHYs for a given object
192 void sas_remove_children(struct device *dev)
194 device_for_each_child(dev, (void *)0, do_sas_phy_delete);
195 device_for_each_child(dev, (void *)1, do_sas_phy_delete);
197 EXPORT_SYMBOL(sas_remove_children);
200 * sas_remove_host -- tear down a Scsi_Host's SAS data structures
201 * @shost: Scsi Host that is torn down
203 * Removes all SAS PHYs and remote PHYs for a given Scsi_Host.
204 * Must be called just before scsi_remove_host for SAS HBAs.
206 void sas_remove_host(struct Scsi_Host *shost)
208 sas_remove_children(&shost->shost_gendev);
210 EXPORT_SYMBOL(sas_remove_host);
217 #define sas_phy_show_simple(field, name, format_string, cast) \
219 show_sas_phy_##name(struct class_device *cdev, char *buf) \
221 struct sas_phy *phy = transport_class_to_phy(cdev); \
223 return snprintf(buf, 20, format_string, cast phy->field); \
226 #define sas_phy_simple_attr(field, name, format_string, type) \
227 sas_phy_show_simple(field, name, format_string, (type)) \
228 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
230 #define sas_phy_show_protocol(field, name) \
232 show_sas_phy_##name(struct class_device *cdev, char *buf) \
234 struct sas_phy *phy = transport_class_to_phy(cdev); \
237 return snprintf(buf, 20, "none\n"); \
238 return get_sas_protocol_names(phy->field, buf); \
241 #define sas_phy_protocol_attr(field, name) \
242 sas_phy_show_protocol(field, name) \
243 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
245 #define sas_phy_show_linkspeed(field) \
247 show_sas_phy_##field(struct class_device *cdev, char *buf) \
249 struct sas_phy *phy = transport_class_to_phy(cdev); \
251 return get_sas_linkspeed_names(phy->field, buf); \
254 #define sas_phy_linkspeed_attr(field) \
255 sas_phy_show_linkspeed(field) \
256 static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
258 #define sas_phy_show_linkerror(field) \
260 show_sas_phy_##field(struct class_device *cdev, char *buf) \
262 struct sas_phy *phy = transport_class_to_phy(cdev); \
263 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \
264 struct sas_internal *i = to_sas_internal(shost->transportt); \
267 if (!phy->local_attached) \
270 error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0; \
273 return snprintf(buf, 20, "%u\n", phy->field); \
276 #define sas_phy_linkerror_attr(field) \
277 sas_phy_show_linkerror(field) \
278 static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
282 show_sas_device_type(struct class_device *cdev, char *buf)
284 struct sas_phy *phy = transport_class_to_phy(cdev);
286 if (!phy->identify.device_type)
287 return snprintf(buf, 20, "none\n");
288 return get_sas_device_type_names(phy->identify.device_type, buf);
290 static CLASS_DEVICE_ATTR(device_type, S_IRUGO, show_sas_device_type, NULL);
292 static ssize_t do_sas_phy_reset(struct class_device *cdev,
293 size_t count, int hard_reset)
295 struct sas_phy *phy = transport_class_to_phy(cdev);
296 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
297 struct sas_internal *i = to_sas_internal(shost->transportt);
300 if (!phy->local_attached)
303 error = i->f->phy_reset(phy, hard_reset);
309 static ssize_t store_sas_link_reset(struct class_device *cdev,
310 const char *buf, size_t count)
312 return do_sas_phy_reset(cdev, count, 0);
314 static CLASS_DEVICE_ATTR(link_reset, S_IWUSR, NULL, store_sas_link_reset);
316 static ssize_t store_sas_hard_reset(struct class_device *cdev,
317 const char *buf, size_t count)
319 return do_sas_phy_reset(cdev, count, 1);
321 static CLASS_DEVICE_ATTR(hard_reset, S_IWUSR, NULL, store_sas_hard_reset);
323 sas_phy_protocol_attr(identify.initiator_port_protocols,
324 initiator_port_protocols);
325 sas_phy_protocol_attr(identify.target_port_protocols,
326 target_port_protocols);
327 sas_phy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
329 sas_phy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
330 //sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", u8);
331 sas_phy_linkspeed_attr(negotiated_linkrate);
332 sas_phy_linkspeed_attr(minimum_linkrate_hw);
333 sas_phy_linkspeed_attr(minimum_linkrate);
334 sas_phy_linkspeed_attr(maximum_linkrate_hw);
335 sas_phy_linkspeed_attr(maximum_linkrate);
336 sas_phy_linkerror_attr(invalid_dword_count);
337 sas_phy_linkerror_attr(running_disparity_error_count);
338 sas_phy_linkerror_attr(loss_of_dword_sync_count);
339 sas_phy_linkerror_attr(phy_reset_problem_count);
342 static DECLARE_TRANSPORT_CLASS(sas_phy_class,
343 "sas_phy", NULL, NULL, NULL);
345 static int sas_phy_match(struct attribute_container *cont, struct device *dev)
347 struct Scsi_Host *shost;
348 struct sas_internal *i;
350 if (!scsi_is_sas_phy(dev))
352 shost = dev_to_shost(dev->parent);
354 if (!shost->transportt)
356 if (shost->transportt->host_attrs.ac.class !=
357 &sas_host_class.class)
360 i = to_sas_internal(shost->transportt);
361 return &i->phy_attr_cont.ac == cont;
364 static void sas_phy_release(struct device *dev)
366 struct sas_phy *phy = dev_to_phy(dev);
368 put_device(dev->parent);
373 * sas_phy_alloc -- allocates and initialize a SAS PHY structure
374 * @parent: Parent device
377 * Allocates an SAS PHY structure. It will be added in the device tree
378 * below the device specified by @parent, which has to be either a Scsi_Host
382 * SAS PHY allocated or %NULL if the allocation failed.
384 struct sas_phy *sas_phy_alloc(struct device *parent, int number)
386 struct Scsi_Host *shost = dev_to_shost(parent);
389 phy = kzalloc(sizeof(*phy), GFP_KERNEL);
393 phy->number = number;
395 device_initialize(&phy->dev);
396 phy->dev.parent = get_device(parent);
397 phy->dev.release = sas_phy_release;
398 INIT_LIST_HEAD(&phy->port_siblings);
399 if (scsi_is_sas_expander_device(parent)) {
400 struct sas_rphy *rphy = dev_to_rphy(parent);
401 sprintf(phy->dev.bus_id, "phy-%d:%d:%d", shost->host_no,
402 rphy->scsi_target_id, number);
404 sprintf(phy->dev.bus_id, "phy-%d:%d", shost->host_no, number);
406 transport_setup_device(&phy->dev);
410 EXPORT_SYMBOL(sas_phy_alloc);
413 * sas_phy_add -- add a SAS PHY to the device hierachy
414 * @phy: The PHY to be added
416 * Publishes a SAS PHY to the rest of the system.
418 int sas_phy_add(struct sas_phy *phy)
422 error = device_add(&phy->dev);
424 transport_add_device(&phy->dev);
425 transport_configure_device(&phy->dev);
430 EXPORT_SYMBOL(sas_phy_add);
433 * sas_phy_free -- free a SAS PHY
434 * @phy: SAS PHY to free
436 * Frees the specified SAS PHY.
439 * This function must only be called on a PHY that has not
440 * sucessfully been added using sas_phy_add().
442 void sas_phy_free(struct sas_phy *phy)
444 transport_destroy_device(&phy->dev);
445 put_device(&phy->dev);
447 EXPORT_SYMBOL(sas_phy_free);
450 * sas_phy_delete -- remove SAS PHY
451 * @phy: SAS PHY to remove
453 * Removes the specified SAS PHY. If the SAS PHY has an
454 * associated remote PHY it is removed before.
457 sas_phy_delete(struct sas_phy *phy)
459 struct device *dev = &phy->dev;
461 /* this happens if the phy is still part of a port when deleted */
462 BUG_ON(!list_empty(&phy->port_siblings));
464 transport_remove_device(dev);
466 transport_destroy_device(dev);
469 EXPORT_SYMBOL(sas_phy_delete);
472 * scsi_is_sas_phy -- check if a struct device represents a SAS PHY
473 * @dev: device to check
476 * %1 if the device represents a SAS PHY, %0 else
478 int scsi_is_sas_phy(const struct device *dev)
480 return dev->release == sas_phy_release;
482 EXPORT_SYMBOL(scsi_is_sas_phy);
485 * SAS Port attributes
487 #define sas_port_show_simple(field, name, format_string, cast) \
489 show_sas_port_##name(struct class_device *cdev, char *buf) \
491 struct sas_port *port = transport_class_to_sas_port(cdev); \
493 return snprintf(buf, 20, format_string, cast port->field); \
496 #define sas_port_simple_attr(field, name, format_string, type) \
497 sas_port_show_simple(field, name, format_string, (type)) \
498 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_port_##name, NULL)
500 sas_port_simple_attr(num_phys, num_phys, "%d\n", int);
502 static DECLARE_TRANSPORT_CLASS(sas_port_class,
503 "sas_port", NULL, NULL, NULL);
505 static int sas_port_match(struct attribute_container *cont, struct device *dev)
507 struct Scsi_Host *shost;
508 struct sas_internal *i;
510 if (!scsi_is_sas_port(dev))
512 shost = dev_to_shost(dev->parent);
514 if (!shost->transportt)
516 if (shost->transportt->host_attrs.ac.class !=
517 &sas_host_class.class)
520 i = to_sas_internal(shost->transportt);
521 return &i->port_attr_cont.ac == cont;
525 static void sas_port_release(struct device *dev)
527 struct sas_port *port = dev_to_sas_port(dev);
529 BUG_ON(!list_empty(&port->phy_list));
531 put_device(dev->parent);
535 static void sas_port_create_link(struct sas_port *port,
538 sysfs_create_link(&port->dev.kobj, &phy->dev.kobj, phy->dev.bus_id);
539 sysfs_create_link(&phy->dev.kobj, &port->dev.kobj, "port");
542 static void sas_port_delete_link(struct sas_port *port,
545 sysfs_remove_link(&port->dev.kobj, phy->dev.bus_id);
546 sysfs_remove_link(&phy->dev.kobj, "port");
549 /** sas_port_alloc - allocate and initialize a SAS port structure
551 * @parent: parent device
552 * @port_id: port number
554 * Allocates a SAS port structure. It will be added to the device tree
555 * below the device specified by @parent which must be either a Scsi_Host
556 * or a sas_expander_device.
558 * Returns %NULL on error
560 struct sas_port *sas_port_alloc(struct device *parent, int port_id)
562 struct Scsi_Host *shost = dev_to_shost(parent);
563 struct sas_port *port;
565 port = kzalloc(sizeof(*port), GFP_KERNEL);
569 port->port_identifier = port_id;
571 device_initialize(&port->dev);
573 port->dev.parent = get_device(parent);
574 port->dev.release = sas_port_release;
576 mutex_init(&port->phy_list_mutex);
577 INIT_LIST_HEAD(&port->phy_list);
579 if (scsi_is_sas_expander_device(parent)) {
580 struct sas_rphy *rphy = dev_to_rphy(parent);
581 sprintf(port->dev.bus_id, "port-%d:%d:%d", shost->host_no,
582 rphy->scsi_target_id, port->port_identifier);
584 sprintf(port->dev.bus_id, "port-%d:%d", shost->host_no,
585 port->port_identifier);
587 transport_setup_device(&port->dev);
591 EXPORT_SYMBOL(sas_port_alloc);
594 * sas_port_add - add a SAS port to the device hierarchy
596 * @port: port to be added
598 * publishes a port to the rest of the system
600 int sas_port_add(struct sas_port *port)
604 /* No phys should be added until this is made visible */
605 BUG_ON(!list_empty(&port->phy_list));
607 error = device_add(&port->dev);
612 transport_add_device(&port->dev);
613 transport_configure_device(&port->dev);
617 EXPORT_SYMBOL(sas_port_add);
620 * sas_port_free -- free a SAS PORT
621 * @port: SAS PORT to free
623 * Frees the specified SAS PORT.
626 * This function must only be called on a PORT that has not
627 * sucessfully been added using sas_port_add().
629 void sas_port_free(struct sas_port *port)
631 transport_destroy_device(&port->dev);
632 put_device(&port->dev);
634 EXPORT_SYMBOL(sas_port_free);
637 * sas_port_delete -- remove SAS PORT
638 * @port: SAS PORT to remove
640 * Removes the specified SAS PORT. If the SAS PORT has an
641 * associated phys, unlink them from the port as well.
643 void sas_port_delete(struct sas_port *port)
645 struct device *dev = &port->dev;
646 struct sas_phy *phy, *tmp_phy;
649 sas_rphy_delete(port->rphy);
653 mutex_lock(&port->phy_list_mutex);
654 list_for_each_entry_safe(phy, tmp_phy, &port->phy_list,
656 sas_port_delete_link(port, phy);
657 list_del_init(&phy->port_siblings);
659 mutex_unlock(&port->phy_list_mutex);
661 transport_remove_device(dev);
663 transport_destroy_device(dev);
666 EXPORT_SYMBOL(sas_port_delete);
669 * scsi_is_sas_port -- check if a struct device represents a SAS port
670 * @dev: device to check
673 * %1 if the device represents a SAS Port, %0 else
675 int scsi_is_sas_port(const struct device *dev)
677 return dev->release == sas_port_release;
679 EXPORT_SYMBOL(scsi_is_sas_port);
682 * sas_port_add_phy - add another phy to a port to form a wide port
683 * @port: port to add the phy to
686 * When a port is initially created, it is empty (has no phys). All
687 * ports must have at least one phy to operated, and all wide ports
688 * must have at least two. The current code makes no difference
689 * between ports and wide ports, but the only object that can be
690 * connected to a remote device is a port, so ports must be formed on
691 * all devices with phys if they're connected to anything.
693 void sas_port_add_phy(struct sas_port *port, struct sas_phy *phy)
695 mutex_lock(&port->phy_list_mutex);
696 if (unlikely(!list_empty(&phy->port_siblings))) {
697 /* make sure we're already on this port */
700 list_for_each_entry(tmp, &port->phy_list, port_siblings)
703 /* If this trips, you added a phy that was already
704 * part of a different port */
705 if (unlikely(tmp != phy)) {
706 dev_printk(KERN_ERR, &port->dev, "trying to add phy %s fails: it's already part of another port\n", phy->dev.bus_id);
710 sas_port_create_link(port, phy);
711 list_add_tail(&phy->port_siblings, &port->phy_list);
714 mutex_unlock(&port->phy_list_mutex);
716 EXPORT_SYMBOL(sas_port_add_phy);
719 * sas_port_delete_phy - remove a phy from a port or wide port
720 * @port: port to remove the phy from
721 * @phy: phy to remove
723 * This operation is used for tearing down ports again. It must be
724 * done to every port or wide port before calling sas_port_delete.
726 void sas_port_delete_phy(struct sas_port *port, struct sas_phy *phy)
728 mutex_lock(&port->phy_list_mutex);
729 sas_port_delete_link(port, phy);
730 list_del_init(&phy->port_siblings);
732 mutex_unlock(&port->phy_list_mutex);
734 EXPORT_SYMBOL(sas_port_delete_phy);
737 * SAS remote PHY attributes.
740 #define sas_rphy_show_simple(field, name, format_string, cast) \
742 show_sas_rphy_##name(struct class_device *cdev, char *buf) \
744 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
746 return snprintf(buf, 20, format_string, cast rphy->field); \
749 #define sas_rphy_simple_attr(field, name, format_string, type) \
750 sas_rphy_show_simple(field, name, format_string, (type)) \
751 static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \
752 show_sas_rphy_##name, NULL)
754 #define sas_rphy_show_protocol(field, name) \
756 show_sas_rphy_##name(struct class_device *cdev, char *buf) \
758 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
761 return snprintf(buf, 20, "none\n"); \
762 return get_sas_protocol_names(rphy->field, buf); \
765 #define sas_rphy_protocol_attr(field, name) \
766 sas_rphy_show_protocol(field, name) \
767 static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \
768 show_sas_rphy_##name, NULL)
771 show_sas_rphy_device_type(struct class_device *cdev, char *buf)
773 struct sas_rphy *rphy = transport_class_to_rphy(cdev);
775 if (!rphy->identify.device_type)
776 return snprintf(buf, 20, "none\n");
777 return get_sas_device_type_names(
778 rphy->identify.device_type, buf);
781 static SAS_CLASS_DEVICE_ATTR(rphy, device_type, S_IRUGO,
782 show_sas_rphy_device_type, NULL);
785 show_sas_rphy_enclosure_identifier(struct class_device *cdev, char *buf)
787 struct sas_rphy *rphy = transport_class_to_rphy(cdev);
788 struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
789 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
790 struct sas_internal *i = to_sas_internal(shost->transportt);
795 * Only devices behind an expander are supported, because the
796 * enclosure identifier is a SMP feature.
798 if (phy->local_attached)
801 error = i->f->get_enclosure_identifier(rphy, &identifier);
804 return sprintf(buf, "0x%llx\n", (unsigned long long)identifier);
807 static SAS_CLASS_DEVICE_ATTR(rphy, enclosure_identifier, S_IRUGO,
808 show_sas_rphy_enclosure_identifier, NULL);
811 show_sas_rphy_bay_identifier(struct class_device *cdev, char *buf)
813 struct sas_rphy *rphy = transport_class_to_rphy(cdev);
814 struct sas_phy *phy = dev_to_phy(rphy->dev.parent);
815 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent);
816 struct sas_internal *i = to_sas_internal(shost->transportt);
819 if (phy->local_attached)
822 val = i->f->get_bay_identifier(rphy);
825 return sprintf(buf, "%d\n", val);
828 static SAS_CLASS_DEVICE_ATTR(rphy, bay_identifier, S_IRUGO,
829 show_sas_rphy_bay_identifier, NULL);
831 sas_rphy_protocol_attr(identify.initiator_port_protocols,
832 initiator_port_protocols);
833 sas_rphy_protocol_attr(identify.target_port_protocols, target_port_protocols);
834 sas_rphy_simple_attr(identify.sas_address, sas_address, "0x%016llx\n",
836 sas_rphy_simple_attr(identify.phy_identifier, phy_identifier, "%d\n", u8);
838 /* only need 8 bytes of data plus header (4 or 8) */
841 int sas_read_port_mode_page(struct scsi_device *sdev)
843 char *buffer = kzalloc(BUF_SIZE, GFP_KERNEL), *msdata;
844 struct sas_rphy *rphy = target_to_rphy(sdev->sdev_target);
845 struct sas_end_device *rdev;
846 struct scsi_mode_data mode_data;
849 BUG_ON(rphy->identify.device_type != SAS_END_DEVICE);
851 rdev = rphy_to_end_device(rphy);
856 res = scsi_mode_sense(sdev, 1, 0x19, buffer, BUF_SIZE, 30*HZ, 3,
860 if (!scsi_status_is_good(res))
863 msdata = buffer + mode_data.header_length +
864 mode_data.block_descriptor_length;
866 if (msdata - buffer > BUF_SIZE - 8)
871 rdev->ready_led_meaning = msdata[2] & 0x10 ? 1 : 0;
872 rdev->I_T_nexus_loss_timeout = (msdata[4] << 8) + msdata[5];
873 rdev->initiator_response_timeout = (msdata[6] << 8) + msdata[7];
879 EXPORT_SYMBOL(sas_read_port_mode_page);
881 static DECLARE_TRANSPORT_CLASS(sas_end_dev_class,
882 "sas_end_device", NULL, NULL, NULL);
884 #define sas_end_dev_show_simple(field, name, format_string, cast) \
886 show_sas_end_dev_##name(struct class_device *cdev, char *buf) \
888 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
889 struct sas_end_device *rdev = rphy_to_end_device(rphy); \
891 return snprintf(buf, 20, format_string, cast rdev->field); \
894 #define sas_end_dev_simple_attr(field, name, format_string, type) \
895 sas_end_dev_show_simple(field, name, format_string, (type)) \
896 static SAS_CLASS_DEVICE_ATTR(end_dev, name, S_IRUGO, \
897 show_sas_end_dev_##name, NULL)
899 sas_end_dev_simple_attr(ready_led_meaning, ready_led_meaning, "%d\n", int);
900 sas_end_dev_simple_attr(I_T_nexus_loss_timeout, I_T_nexus_loss_timeout,
902 sas_end_dev_simple_attr(initiator_response_timeout, initiator_response_timeout,
905 static DECLARE_TRANSPORT_CLASS(sas_expander_class,
906 "sas_expander", NULL, NULL, NULL);
908 #define sas_expander_show_simple(field, name, format_string, cast) \
910 show_sas_expander_##name(struct class_device *cdev, char *buf) \
912 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
913 struct sas_expander_device *edev = rphy_to_expander_device(rphy); \
915 return snprintf(buf, 20, format_string, cast edev->field); \
918 #define sas_expander_simple_attr(field, name, format_string, type) \
919 sas_expander_show_simple(field, name, format_string, (type)) \
920 static SAS_CLASS_DEVICE_ATTR(expander, name, S_IRUGO, \
921 show_sas_expander_##name, NULL)
923 sas_expander_simple_attr(vendor_id, vendor_id, "%s\n", char *);
924 sas_expander_simple_attr(product_id, product_id, "%s\n", char *);
925 sas_expander_simple_attr(product_rev, product_rev, "%s\n", char *);
926 sas_expander_simple_attr(component_vendor_id, component_vendor_id,
928 sas_expander_simple_attr(component_id, component_id, "%u\n", unsigned int);
929 sas_expander_simple_attr(component_revision_id, component_revision_id, "%u\n",
931 sas_expander_simple_attr(level, level, "%d\n", int);
933 static DECLARE_TRANSPORT_CLASS(sas_rphy_class,
934 "sas_device", NULL, NULL, NULL);
936 static int sas_rphy_match(struct attribute_container *cont, struct device *dev)
938 struct Scsi_Host *shost;
939 struct sas_internal *i;
941 if (!scsi_is_sas_rphy(dev))
943 shost = dev_to_shost(dev->parent->parent);
945 if (!shost->transportt)
947 if (shost->transportt->host_attrs.ac.class !=
948 &sas_host_class.class)
951 i = to_sas_internal(shost->transportt);
952 return &i->rphy_attr_cont.ac == cont;
955 static int sas_end_dev_match(struct attribute_container *cont,
958 struct Scsi_Host *shost;
959 struct sas_internal *i;
960 struct sas_rphy *rphy;
962 if (!scsi_is_sas_rphy(dev))
964 shost = dev_to_shost(dev->parent->parent);
965 rphy = dev_to_rphy(dev);
967 if (!shost->transportt)
969 if (shost->transportt->host_attrs.ac.class !=
970 &sas_host_class.class)
973 i = to_sas_internal(shost->transportt);
974 return &i->end_dev_attr_cont.ac == cont &&
975 rphy->identify.device_type == SAS_END_DEVICE;
978 static int sas_expander_match(struct attribute_container *cont,
981 struct Scsi_Host *shost;
982 struct sas_internal *i;
983 struct sas_rphy *rphy;
985 if (!scsi_is_sas_rphy(dev))
987 shost = dev_to_shost(dev->parent->parent);
988 rphy = dev_to_rphy(dev);
990 if (!shost->transportt)
992 if (shost->transportt->host_attrs.ac.class !=
993 &sas_host_class.class)
996 i = to_sas_internal(shost->transportt);
997 return &i->expander_attr_cont.ac == cont &&
998 (rphy->identify.device_type == SAS_EDGE_EXPANDER_DEVICE ||
999 rphy->identify.device_type == SAS_FANOUT_EXPANDER_DEVICE);
1002 static void sas_expander_release(struct device *dev)
1004 struct sas_rphy *rphy = dev_to_rphy(dev);
1005 struct sas_expander_device *edev = rphy_to_expander_device(rphy);
1007 put_device(dev->parent);
1011 static void sas_end_device_release(struct device *dev)
1013 struct sas_rphy *rphy = dev_to_rphy(dev);
1014 struct sas_end_device *edev = rphy_to_end_device(rphy);
1016 put_device(dev->parent);
1021 * sas_rphy_initialize - common rphy intialization
1022 * @rphy: rphy to initialise
1024 * Used by both sas_end_device_alloc() and sas_expander_alloc() to
1025 * initialise the common rphy component of each.
1027 static void sas_rphy_initialize(struct sas_rphy *rphy)
1029 INIT_LIST_HEAD(&rphy->list);
1033 * sas_end_device_alloc - allocate an rphy for an end device
1035 * Allocates an SAS remote PHY structure, connected to @parent.
1038 * SAS PHY allocated or %NULL if the allocation failed.
1040 struct sas_rphy *sas_end_device_alloc(struct sas_port *parent)
1042 struct Scsi_Host *shost = dev_to_shost(&parent->dev);
1043 struct sas_end_device *rdev;
1045 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1050 device_initialize(&rdev->rphy.dev);
1051 rdev->rphy.dev.parent = get_device(&parent->dev);
1052 rdev->rphy.dev.release = sas_end_device_release;
1053 if (scsi_is_sas_expander_device(parent->dev.parent)) {
1054 struct sas_rphy *rphy = dev_to_rphy(parent->dev.parent);
1055 sprintf(rdev->rphy.dev.bus_id, "end_device-%d:%d:%d",
1056 shost->host_no, rphy->scsi_target_id, parent->port_identifier);
1058 sprintf(rdev->rphy.dev.bus_id, "end_device-%d:%d",
1059 shost->host_no, parent->port_identifier);
1060 rdev->rphy.identify.device_type = SAS_END_DEVICE;
1061 sas_rphy_initialize(&rdev->rphy);
1062 transport_setup_device(&rdev->rphy.dev);
1066 EXPORT_SYMBOL(sas_end_device_alloc);
1069 * sas_expander_alloc - allocate an rphy for an end device
1071 * Allocates an SAS remote PHY structure, connected to @parent.
1074 * SAS PHY allocated or %NULL if the allocation failed.
1076 struct sas_rphy *sas_expander_alloc(struct sas_port *parent,
1077 enum sas_device_type type)
1079 struct Scsi_Host *shost = dev_to_shost(&parent->dev);
1080 struct sas_expander_device *rdev;
1081 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1083 BUG_ON(type != SAS_EDGE_EXPANDER_DEVICE &&
1084 type != SAS_FANOUT_EXPANDER_DEVICE);
1086 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1091 device_initialize(&rdev->rphy.dev);
1092 rdev->rphy.dev.parent = get_device(&parent->dev);
1093 rdev->rphy.dev.release = sas_expander_release;
1094 mutex_lock(&sas_host->lock);
1095 rdev->rphy.scsi_target_id = sas_host->next_expander_id++;
1096 mutex_unlock(&sas_host->lock);
1097 sprintf(rdev->rphy.dev.bus_id, "expander-%d:%d",
1098 shost->host_no, rdev->rphy.scsi_target_id);
1099 rdev->rphy.identify.device_type = type;
1100 sas_rphy_initialize(&rdev->rphy);
1101 transport_setup_device(&rdev->rphy.dev);
1105 EXPORT_SYMBOL(sas_expander_alloc);
1108 * sas_rphy_add -- add a SAS remote PHY to the device hierachy
1109 * @rphy: The remote PHY to be added
1111 * Publishes a SAS remote PHY to the rest of the system.
1113 int sas_rphy_add(struct sas_rphy *rphy)
1115 struct sas_port *parent = dev_to_sas_port(rphy->dev.parent);
1116 struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
1117 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1118 struct sas_identify *identify = &rphy->identify;
1123 parent->rphy = rphy;
1125 error = device_add(&rphy->dev);
1128 transport_add_device(&rphy->dev);
1129 transport_configure_device(&rphy->dev);
1131 mutex_lock(&sas_host->lock);
1132 list_add_tail(&rphy->list, &sas_host->rphy_list);
1133 if (identify->device_type == SAS_END_DEVICE &&
1134 (identify->target_port_protocols &
1135 (SAS_PROTOCOL_SSP|SAS_PROTOCOL_STP|SAS_PROTOCOL_SATA)))
1136 rphy->scsi_target_id = sas_host->next_target_id++;
1137 else if (identify->device_type == SAS_END_DEVICE)
1138 rphy->scsi_target_id = -1;
1139 mutex_unlock(&sas_host->lock);
1141 if (identify->device_type == SAS_END_DEVICE &&
1142 rphy->scsi_target_id != -1) {
1143 scsi_scan_target(&rphy->dev, parent->port_identifier,
1144 rphy->scsi_target_id, ~0, 0);
1149 EXPORT_SYMBOL(sas_rphy_add);
1152 * sas_rphy_free -- free a SAS remote PHY
1153 * @rphy SAS remote PHY to free
1155 * Frees the specified SAS remote PHY.
1158 * This function must only be called on a remote
1159 * PHY that has not sucessfully been added using
1162 void sas_rphy_free(struct sas_rphy *rphy)
1164 struct device *dev = &rphy->dev;
1165 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent->parent);
1166 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1168 mutex_lock(&sas_host->lock);
1169 list_del(&rphy->list);
1170 mutex_unlock(&sas_host->lock);
1172 transport_destroy_device(dev);
1176 EXPORT_SYMBOL(sas_rphy_free);
1179 * sas_rphy_delete -- remove SAS remote PHY
1180 * @rphy: SAS remote PHY to remove
1182 * Removes the specified SAS remote PHY.
1185 sas_rphy_delete(struct sas_rphy *rphy)
1187 struct device *dev = &rphy->dev;
1188 struct sas_port *parent = dev_to_sas_port(dev->parent);
1189 struct Scsi_Host *shost = dev_to_shost(parent->dev.parent);
1190 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1192 switch (rphy->identify.device_type) {
1193 case SAS_END_DEVICE:
1194 scsi_remove_target(dev);
1196 case SAS_EDGE_EXPANDER_DEVICE:
1197 case SAS_FANOUT_EXPANDER_DEVICE:
1198 sas_remove_children(dev);
1204 transport_remove_device(dev);
1206 transport_destroy_device(dev);
1208 mutex_lock(&sas_host->lock);
1209 list_del(&rphy->list);
1210 mutex_unlock(&sas_host->lock);
1212 parent->rphy = NULL;
1216 EXPORT_SYMBOL(sas_rphy_delete);
1219 * scsi_is_sas_rphy -- check if a struct device represents a SAS remote PHY
1220 * @dev: device to check
1223 * %1 if the device represents a SAS remote PHY, %0 else
1225 int scsi_is_sas_rphy(const struct device *dev)
1227 return dev->release == sas_end_device_release ||
1228 dev->release == sas_expander_release;
1230 EXPORT_SYMBOL(scsi_is_sas_rphy);
1237 static int sas_user_scan(struct Scsi_Host *shost, uint channel,
1240 struct sas_host_attrs *sas_host = to_sas_host_attrs(shost);
1241 struct sas_rphy *rphy;
1243 mutex_lock(&sas_host->lock);
1244 list_for_each_entry(rphy, &sas_host->rphy_list, list) {
1245 struct sas_port *parent = dev_to_sas_port(rphy->dev.parent);
1247 if (rphy->identify.device_type != SAS_END_DEVICE ||
1248 rphy->scsi_target_id == -1)
1251 if ((channel == SCAN_WILD_CARD || channel == parent->port_identifier) &&
1252 (id == SCAN_WILD_CARD || id == rphy->scsi_target_id)) {
1253 scsi_scan_target(&rphy->dev, parent->port_identifier,
1254 rphy->scsi_target_id, lun, 1);
1257 mutex_unlock(&sas_host->lock);
1264 * Setup / Teardown code
1267 #define SETUP_TEMPLATE(attrb, field, perm, test) \
1268 i->private_##attrb[count] = class_device_attr_##field; \
1269 i->private_##attrb[count].attr.mode = perm; \
1270 i->attrb[count] = &i->private_##attrb[count]; \
1275 #define SETUP_RPORT_ATTRIBUTE(field) \
1276 SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1)
1278 #define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func) \
1279 SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, i->f->func)
1281 #define SETUP_PHY_ATTRIBUTE(field) \
1282 SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1)
1284 #define SETUP_PORT_ATTRIBUTE(field) \
1285 SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
1287 #define SETUP_OPTIONAL_PHY_ATTRIBUTE(field, func) \
1288 SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, i->f->func)
1290 #define SETUP_PHY_ATTRIBUTE_WRONLY(field) \
1291 SETUP_TEMPLATE(phy_attrs, field, S_IWUGO, 1)
1293 #define SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(field, func) \
1294 SETUP_TEMPLATE(phy_attrs, field, S_IWUGO, i->f->func)
1296 #define SETUP_END_DEV_ATTRIBUTE(field) \
1297 SETUP_TEMPLATE(end_dev_attrs, field, S_IRUGO, 1)
1299 #define SETUP_EXPANDER_ATTRIBUTE(field) \
1300 SETUP_TEMPLATE(expander_attrs, expander_##field, S_IRUGO, 1)
1303 * sas_attach_transport -- instantiate SAS transport template
1304 * @ft: SAS transport class function template
1306 struct scsi_transport_template *
1307 sas_attach_transport(struct sas_function_template *ft)
1309 struct sas_internal *i;
1312 i = kzalloc(sizeof(struct sas_internal), GFP_KERNEL);
1316 i->t.user_scan = sas_user_scan;
1318 i->t.host_attrs.ac.attrs = &i->host_attrs[0];
1319 i->t.host_attrs.ac.class = &sas_host_class.class;
1320 i->t.host_attrs.ac.match = sas_host_match;
1321 transport_container_register(&i->t.host_attrs);
1322 i->t.host_size = sizeof(struct sas_host_attrs);
1324 i->phy_attr_cont.ac.class = &sas_phy_class.class;
1325 i->phy_attr_cont.ac.attrs = &i->phy_attrs[0];
1326 i->phy_attr_cont.ac.match = sas_phy_match;
1327 transport_container_register(&i->phy_attr_cont);
1329 i->port_attr_cont.ac.class = &sas_port_class.class;
1330 i->port_attr_cont.ac.attrs = &i->port_attrs[0];
1331 i->port_attr_cont.ac.match = sas_port_match;
1332 transport_container_register(&i->port_attr_cont);
1334 i->rphy_attr_cont.ac.class = &sas_rphy_class.class;
1335 i->rphy_attr_cont.ac.attrs = &i->rphy_attrs[0];
1336 i->rphy_attr_cont.ac.match = sas_rphy_match;
1337 transport_container_register(&i->rphy_attr_cont);
1339 i->end_dev_attr_cont.ac.class = &sas_end_dev_class.class;
1340 i->end_dev_attr_cont.ac.attrs = &i->end_dev_attrs[0];
1341 i->end_dev_attr_cont.ac.match = sas_end_dev_match;
1342 transport_container_register(&i->end_dev_attr_cont);
1344 i->expander_attr_cont.ac.class = &sas_expander_class.class;
1345 i->expander_attr_cont.ac.attrs = &i->expander_attrs[0];
1346 i->expander_attr_cont.ac.match = sas_expander_match;
1347 transport_container_register(&i->expander_attr_cont);
1352 SETUP_PORT_ATTRIBUTE(num_phys);
1353 i->host_attrs[count] = NULL;
1356 SETUP_PHY_ATTRIBUTE(initiator_port_protocols);
1357 SETUP_PHY_ATTRIBUTE(target_port_protocols);
1358 SETUP_PHY_ATTRIBUTE(device_type);
1359 SETUP_PHY_ATTRIBUTE(sas_address);
1360 SETUP_PHY_ATTRIBUTE(phy_identifier);
1361 //SETUP_PHY_ATTRIBUTE(port_identifier);
1362 SETUP_PHY_ATTRIBUTE(negotiated_linkrate);
1363 SETUP_PHY_ATTRIBUTE(minimum_linkrate_hw);
1364 SETUP_PHY_ATTRIBUTE(minimum_linkrate);
1365 SETUP_PHY_ATTRIBUTE(maximum_linkrate_hw);
1366 SETUP_PHY_ATTRIBUTE(maximum_linkrate);
1368 SETUP_PHY_ATTRIBUTE(invalid_dword_count);
1369 SETUP_PHY_ATTRIBUTE(running_disparity_error_count);
1370 SETUP_PHY_ATTRIBUTE(loss_of_dword_sync_count);
1371 SETUP_PHY_ATTRIBUTE(phy_reset_problem_count);
1372 SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(link_reset, phy_reset);
1373 SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(hard_reset, phy_reset);
1374 i->phy_attrs[count] = NULL;
1377 SETUP_PORT_ATTRIBUTE(num_phys);
1378 i->port_attrs[count] = NULL;
1381 SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols);
1382 SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols);
1383 SETUP_RPORT_ATTRIBUTE(rphy_device_type);
1384 SETUP_RPORT_ATTRIBUTE(rphy_sas_address);
1385 SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier);
1386 SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_enclosure_identifier,
1387 get_enclosure_identifier);
1388 SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_bay_identifier,
1389 get_bay_identifier);
1390 i->rphy_attrs[count] = NULL;
1393 SETUP_END_DEV_ATTRIBUTE(end_dev_ready_led_meaning);
1394 SETUP_END_DEV_ATTRIBUTE(end_dev_I_T_nexus_loss_timeout);
1395 SETUP_END_DEV_ATTRIBUTE(end_dev_initiator_response_timeout);
1396 i->end_dev_attrs[count] = NULL;
1399 SETUP_EXPANDER_ATTRIBUTE(vendor_id);
1400 SETUP_EXPANDER_ATTRIBUTE(product_id);
1401 SETUP_EXPANDER_ATTRIBUTE(product_rev);
1402 SETUP_EXPANDER_ATTRIBUTE(component_vendor_id);
1403 SETUP_EXPANDER_ATTRIBUTE(component_id);
1404 SETUP_EXPANDER_ATTRIBUTE(component_revision_id);
1405 SETUP_EXPANDER_ATTRIBUTE(level);
1406 i->expander_attrs[count] = NULL;
1410 EXPORT_SYMBOL(sas_attach_transport);
1413 * sas_release_transport -- release SAS transport template instance
1414 * @t: transport template instance
1416 void sas_release_transport(struct scsi_transport_template *t)
1418 struct sas_internal *i = to_sas_internal(t);
1420 transport_container_unregister(&i->t.host_attrs);
1421 transport_container_unregister(&i->phy_attr_cont);
1422 transport_container_unregister(&i->port_attr_cont);
1423 transport_container_unregister(&i->rphy_attr_cont);
1424 transport_container_unregister(&i->end_dev_attr_cont);
1425 transport_container_unregister(&i->expander_attr_cont);
1429 EXPORT_SYMBOL(sas_release_transport);
1431 static __init int sas_transport_init(void)
1435 error = transport_class_register(&sas_host_class);
1438 error = transport_class_register(&sas_phy_class);
1440 goto out_unregister_transport;
1441 error = transport_class_register(&sas_port_class);
1443 goto out_unregister_phy;
1444 error = transport_class_register(&sas_rphy_class);
1446 goto out_unregister_port;
1447 error = transport_class_register(&sas_end_dev_class);
1449 goto out_unregister_rphy;
1450 error = transport_class_register(&sas_expander_class);
1452 goto out_unregister_end_dev;
1456 out_unregister_end_dev:
1457 transport_class_unregister(&sas_end_dev_class);
1458 out_unregister_rphy:
1459 transport_class_unregister(&sas_rphy_class);
1460 out_unregister_port:
1461 transport_class_unregister(&sas_port_class);
1463 transport_class_unregister(&sas_phy_class);
1464 out_unregister_transport:
1465 transport_class_unregister(&sas_host_class);
1471 static void __exit sas_transport_exit(void)
1473 transport_class_unregister(&sas_host_class);
1474 transport_class_unregister(&sas_phy_class);
1475 transport_class_unregister(&sas_port_class);
1476 transport_class_unregister(&sas_rphy_class);
1477 transport_class_unregister(&sas_end_dev_class);
1478 transport_class_unregister(&sas_expander_class);
1481 MODULE_AUTHOR("Christoph Hellwig");
1482 MODULE_DESCRIPTION("SAS Transphy Attributes");
1483 MODULE_LICENSE("GPL");
1485 module_init(sas_transport_init);
1486 module_exit(sas_transport_exit);