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>
32 #include <linux/blkdev.h>
33 #include <linux/bsg.h>
35 #include <scsi/scsi.h>
36 #include <scsi/scsi_device.h>
37 #include <scsi/scsi_host.h>
38 #include <scsi/scsi_transport.h>
39 #include <scsi/scsi_transport_sas.h>
41 #include "scsi_sas_internal.h"
42 struct sas_host_attrs
{
43 struct list_head rphy_list
;
49 #define to_sas_host_attrs(host) ((struct sas_host_attrs *)(host)->shost_data)
53 * Hack to allow attributes of the same name in different objects.
55 #define SAS_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
56 struct class_device_attribute class_device_attr_##_prefix##_##_name = \
57 __ATTR(_name,_mode,_show,_store)
61 * Pretty printing helpers
64 #define sas_bitfield_name_match(title, table) \
66 get_sas_##title##_names(u32 table_key, char *buf) \
72 for (i = 0; i < ARRAY_SIZE(table); i++) { \
73 if (table[i].value & table_key) { \
74 len += sprintf(buf + len, "%s%s", \
75 prefix, table[i].name); \
79 len += sprintf(buf + len, "\n"); \
83 #define sas_bitfield_name_set(title, table) \
85 set_sas_##title##_names(u32 *table_key, const char *buf) \
90 for (i = 0; i < ARRAY_SIZE(table); i++) { \
91 len = strlen(table[i].name); \
92 if (strncmp(buf, table[i].name, len) == 0 && \
93 (buf[len] == '\n' || buf[len] == '\0')) { \
94 *table_key = table[i].value; \
101 #define sas_bitfield_name_search(title, table) \
103 get_sas_##title##_names(u32 table_key, char *buf) \
108 for (i = 0; i < ARRAY_SIZE(table); i++) { \
109 if (table[i].value == table_key) { \
110 len += sprintf(buf + len, "%s", \
115 len += sprintf(buf + len, "\n"); \
122 } sas_device_type_names
[] = {
123 { SAS_PHY_UNUSED
, "unused" },
124 { SAS_END_DEVICE
, "end device" },
125 { SAS_EDGE_EXPANDER_DEVICE
, "edge expander" },
126 { SAS_FANOUT_EXPANDER_DEVICE
, "fanout expander" },
128 sas_bitfield_name_search(device_type
, sas_device_type_names
)
134 } sas_protocol_names
[] = {
135 { SAS_PROTOCOL_SATA
, "sata" },
136 { SAS_PROTOCOL_SMP
, "smp" },
137 { SAS_PROTOCOL_STP
, "stp" },
138 { SAS_PROTOCOL_SSP
, "ssp" },
140 sas_bitfield_name_match(protocol
, sas_protocol_names
)
145 } sas_linkspeed_names
[] = {
146 { SAS_LINK_RATE_UNKNOWN
, "Unknown" },
147 { SAS_PHY_DISABLED
, "Phy disabled" },
148 { SAS_LINK_RATE_FAILED
, "Link Rate failed" },
149 { SAS_SATA_SPINUP_HOLD
, "Spin-up hold" },
150 { SAS_LINK_RATE_1_5_GBPS
, "1.5 Gbit" },
151 { SAS_LINK_RATE_3_0_GBPS
, "3.0 Gbit" },
152 { SAS_LINK_RATE_6_0_GBPS
, "6.0 Gbit" },
154 sas_bitfield_name_search(linkspeed
, sas_linkspeed_names
)
155 sas_bitfield_name_set(linkspeed
, sas_linkspeed_names
)
157 static void sas_smp_request(struct request_queue
*q
, struct Scsi_Host
*shost
,
158 struct sas_rphy
*rphy
)
162 int (*handler
)(struct Scsi_Host
*, struct sas_rphy
*, struct request
*);
164 while (!blk_queue_plugged(q
)) {
165 req
= elv_next_request(q
);
169 blkdev_dequeue_request(req
);
171 spin_unlock_irq(q
->queue_lock
);
173 handler
= to_sas_internal(shost
->transportt
)->f
->smp_handler
;
174 ret
= handler(shost
, rphy
, req
);
176 spin_lock_irq(q
->queue_lock
);
178 req
->end_io(req
, ret
);
182 static void sas_host_smp_request(struct request_queue
*q
)
184 sas_smp_request(q
, (struct Scsi_Host
*)q
->queuedata
, NULL
);
187 static void sas_non_host_smp_request(struct request_queue
*q
)
189 struct sas_rphy
*rphy
= q
->queuedata
;
190 sas_smp_request(q
, rphy_to_shost(rphy
), rphy
);
193 static int sas_bsg_initialize(struct Scsi_Host
*shost
, struct sas_rphy
*rphy
,
196 struct request_queue
*q
;
199 if (!to_sas_internal(shost
->transportt
)->f
->smp_handler
) {
200 printk("%s can't handle SMP requests\n", shost
->hostt
->name
);
205 q
= blk_init_queue(sas_non_host_smp_request
, NULL
);
207 q
= blk_init_queue(sas_host_smp_request
, NULL
);
211 error
= bsg_register_queue(q
, name
);
213 blk_cleanup_queue(q
);
220 q
->queuedata
= shost
;
222 set_bit(QUEUE_FLAG_BIDI
, &q
->queue_flags
);
228 * SAS host attributes
231 static int sas_host_setup(struct transport_container
*tc
, struct device
*dev
,
232 struct class_device
*cdev
)
234 struct Scsi_Host
*shost
= dev_to_shost(dev
);
235 struct sas_host_attrs
*sas_host
= to_sas_host_attrs(shost
);
236 char name
[BUS_ID_SIZE
];
238 INIT_LIST_HEAD(&sas_host
->rphy_list
);
239 mutex_init(&sas_host
->lock
);
240 sas_host
->next_target_id
= 0;
241 sas_host
->next_expander_id
= 0;
242 sas_host
->next_port_id
= 0;
244 snprintf(name
, sizeof(name
), "sas_host%d", shost
->host_no
);
245 if (sas_bsg_initialize(shost
, NULL
, name
))
246 dev_printk(KERN_ERR
, dev
, "fail to a bsg device %d\n",
252 static DECLARE_TRANSPORT_CLASS(sas_host_class
,
253 "sas_host", sas_host_setup
, NULL
, NULL
);
255 static int sas_host_match(struct attribute_container
*cont
,
258 struct Scsi_Host
*shost
;
259 struct sas_internal
*i
;
261 if (!scsi_is_host_device(dev
))
263 shost
= dev_to_shost(dev
);
265 if (!shost
->transportt
)
267 if (shost
->transportt
->host_attrs
.ac
.class !=
268 &sas_host_class
.class)
271 i
= to_sas_internal(shost
->transportt
);
272 return &i
->t
.host_attrs
.ac
== cont
;
275 static int do_sas_phy_delete(struct device
*dev
, void *data
)
277 int pass
= (int)(unsigned long)data
;
279 if (pass
== 0 && scsi_is_sas_port(dev
))
280 sas_port_delete(dev_to_sas_port(dev
));
281 else if (pass
== 1 && scsi_is_sas_phy(dev
))
282 sas_phy_delete(dev_to_phy(dev
));
287 * sas_remove_children -- tear down a devices SAS data structures
288 * @dev: device belonging to the sas object
290 * Removes all SAS PHYs and remote PHYs for a given object
292 void sas_remove_children(struct device
*dev
)
294 device_for_each_child(dev
, (void *)0, do_sas_phy_delete
);
295 device_for_each_child(dev
, (void *)1, do_sas_phy_delete
);
297 EXPORT_SYMBOL(sas_remove_children
);
300 * sas_remove_host -- tear down a Scsi_Host's SAS data structures
301 * @shost: Scsi Host that is torn down
303 * Removes all SAS PHYs and remote PHYs for a given Scsi_Host.
304 * Must be called just before scsi_remove_host for SAS HBAs.
306 void sas_remove_host(struct Scsi_Host
*shost
)
308 sas_remove_children(&shost
->shost_gendev
);
310 EXPORT_SYMBOL(sas_remove_host
);
317 #define sas_phy_show_simple(field, name, format_string, cast) \
319 show_sas_phy_##name(struct class_device *cdev, char *buf) \
321 struct sas_phy *phy = transport_class_to_phy(cdev); \
323 return snprintf(buf, 20, format_string, cast phy->field); \
326 #define sas_phy_simple_attr(field, name, format_string, type) \
327 sas_phy_show_simple(field, name, format_string, (type)) \
328 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
330 #define sas_phy_show_protocol(field, name) \
332 show_sas_phy_##name(struct class_device *cdev, char *buf) \
334 struct sas_phy *phy = transport_class_to_phy(cdev); \
337 return snprintf(buf, 20, "none\n"); \
338 return get_sas_protocol_names(phy->field, buf); \
341 #define sas_phy_protocol_attr(field, name) \
342 sas_phy_show_protocol(field, name) \
343 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_phy_##name, NULL)
345 #define sas_phy_show_linkspeed(field) \
347 show_sas_phy_##field(struct class_device *cdev, char *buf) \
349 struct sas_phy *phy = transport_class_to_phy(cdev); \
351 return get_sas_linkspeed_names(phy->field, buf); \
354 /* Fudge to tell if we're minimum or maximum */
355 #define sas_phy_store_linkspeed(field) \
357 store_sas_phy_##field(struct class_device *cdev, const char *buf, \
360 struct sas_phy *phy = transport_class_to_phy(cdev); \
361 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \
362 struct sas_internal *i = to_sas_internal(shost->transportt); \
364 struct sas_phy_linkrates rates = {0}; \
367 error = set_sas_linkspeed_names(&value, buf); \
370 rates.field = value; \
371 error = i->f->set_phy_speed(phy, &rates); \
373 return error ? error : count; \
376 #define sas_phy_linkspeed_rw_attr(field) \
377 sas_phy_show_linkspeed(field) \
378 sas_phy_store_linkspeed(field) \
379 static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, \
380 store_sas_phy_##field)
382 #define sas_phy_linkspeed_attr(field) \
383 sas_phy_show_linkspeed(field) \
384 static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
387 #define sas_phy_show_linkerror(field) \
389 show_sas_phy_##field(struct class_device *cdev, char *buf) \
391 struct sas_phy *phy = transport_class_to_phy(cdev); \
392 struct Scsi_Host *shost = dev_to_shost(phy->dev.parent); \
393 struct sas_internal *i = to_sas_internal(shost->transportt); \
396 error = i->f->get_linkerrors ? i->f->get_linkerrors(phy) : 0; \
399 return snprintf(buf, 20, "%u\n", phy->field); \
402 #define sas_phy_linkerror_attr(field) \
403 sas_phy_show_linkerror(field) \
404 static CLASS_DEVICE_ATTR(field, S_IRUGO, show_sas_phy_##field, NULL)
408 show_sas_device_type(struct class_device
*cdev
, char *buf
)
410 struct sas_phy
*phy
= transport_class_to_phy(cdev
);
412 if (!phy
->identify
.device_type
)
413 return snprintf(buf
, 20, "none\n");
414 return get_sas_device_type_names(phy
->identify
.device_type
, buf
);
416 static CLASS_DEVICE_ATTR(device_type
, S_IRUGO
, show_sas_device_type
, NULL
);
418 static ssize_t
do_sas_phy_enable(struct class_device
*cdev
,
419 size_t count
, int enable
)
421 struct sas_phy
*phy
= transport_class_to_phy(cdev
);
422 struct Scsi_Host
*shost
= dev_to_shost(phy
->dev
.parent
);
423 struct sas_internal
*i
= to_sas_internal(shost
->transportt
);
426 error
= i
->f
->phy_enable(phy
, enable
);
429 phy
->enabled
= enable
;
433 static ssize_t
store_sas_phy_enable(struct class_device
*cdev
,
434 const char *buf
, size_t count
)
441 do_sas_phy_enable(cdev
, count
, 0);
444 do_sas_phy_enable(cdev
, count
, 1);
453 static ssize_t
show_sas_phy_enable(struct class_device
*cdev
, char *buf
)
455 struct sas_phy
*phy
= transport_class_to_phy(cdev
);
457 return snprintf(buf
, 20, "%d", phy
->enabled
);
460 static CLASS_DEVICE_ATTR(enable
, S_IRUGO
| S_IWUSR
, show_sas_phy_enable
,
461 store_sas_phy_enable
);
463 static ssize_t
do_sas_phy_reset(struct class_device
*cdev
,
464 size_t count
, int hard_reset
)
466 struct sas_phy
*phy
= transport_class_to_phy(cdev
);
467 struct Scsi_Host
*shost
= dev_to_shost(phy
->dev
.parent
);
468 struct sas_internal
*i
= to_sas_internal(shost
->transportt
);
471 error
= i
->f
->phy_reset(phy
, hard_reset
);
477 static ssize_t
store_sas_link_reset(struct class_device
*cdev
,
478 const char *buf
, size_t count
)
480 return do_sas_phy_reset(cdev
, count
, 0);
482 static CLASS_DEVICE_ATTR(link_reset
, S_IWUSR
, NULL
, store_sas_link_reset
);
484 static ssize_t
store_sas_hard_reset(struct class_device
*cdev
,
485 const char *buf
, size_t count
)
487 return do_sas_phy_reset(cdev
, count
, 1);
489 static CLASS_DEVICE_ATTR(hard_reset
, S_IWUSR
, NULL
, store_sas_hard_reset
);
491 sas_phy_protocol_attr(identify
.initiator_port_protocols
,
492 initiator_port_protocols
);
493 sas_phy_protocol_attr(identify
.target_port_protocols
,
494 target_port_protocols
);
495 sas_phy_simple_attr(identify
.sas_address
, sas_address
, "0x%016llx\n",
497 sas_phy_simple_attr(identify
.phy_identifier
, phy_identifier
, "%d\n", u8
);
498 //sas_phy_simple_attr(port_identifier, port_identifier, "%d\n", int);
499 sas_phy_linkspeed_attr(negotiated_linkrate
);
500 sas_phy_linkspeed_attr(minimum_linkrate_hw
);
501 sas_phy_linkspeed_rw_attr(minimum_linkrate
);
502 sas_phy_linkspeed_attr(maximum_linkrate_hw
);
503 sas_phy_linkspeed_rw_attr(maximum_linkrate
);
504 sas_phy_linkerror_attr(invalid_dword_count
);
505 sas_phy_linkerror_attr(running_disparity_error_count
);
506 sas_phy_linkerror_attr(loss_of_dword_sync_count
);
507 sas_phy_linkerror_attr(phy_reset_problem_count
);
510 static DECLARE_TRANSPORT_CLASS(sas_phy_class
,
511 "sas_phy", NULL
, NULL
, NULL
);
513 static int sas_phy_match(struct attribute_container
*cont
, struct device
*dev
)
515 struct Scsi_Host
*shost
;
516 struct sas_internal
*i
;
518 if (!scsi_is_sas_phy(dev
))
520 shost
= dev_to_shost(dev
->parent
);
522 if (!shost
->transportt
)
524 if (shost
->transportt
->host_attrs
.ac
.class !=
525 &sas_host_class
.class)
528 i
= to_sas_internal(shost
->transportt
);
529 return &i
->phy_attr_cont
.ac
== cont
;
532 static void sas_phy_release(struct device
*dev
)
534 struct sas_phy
*phy
= dev_to_phy(dev
);
536 put_device(dev
->parent
);
541 * sas_phy_alloc -- allocates and initialize a SAS PHY structure
542 * @parent: Parent device
545 * Allocates an SAS PHY structure. It will be added in the device tree
546 * below the device specified by @parent, which has to be either a Scsi_Host
550 * SAS PHY allocated or %NULL if the allocation failed.
552 struct sas_phy
*sas_phy_alloc(struct device
*parent
, int number
)
554 struct Scsi_Host
*shost
= dev_to_shost(parent
);
557 phy
= kzalloc(sizeof(*phy
), GFP_KERNEL
);
561 phy
->number
= number
;
564 device_initialize(&phy
->dev
);
565 phy
->dev
.parent
= get_device(parent
);
566 phy
->dev
.release
= sas_phy_release
;
567 INIT_LIST_HEAD(&phy
->port_siblings
);
568 if (scsi_is_sas_expander_device(parent
)) {
569 struct sas_rphy
*rphy
= dev_to_rphy(parent
);
570 sprintf(phy
->dev
.bus_id
, "phy-%d:%d:%d", shost
->host_no
,
571 rphy
->scsi_target_id
, number
);
573 sprintf(phy
->dev
.bus_id
, "phy-%d:%d", shost
->host_no
, number
);
575 transport_setup_device(&phy
->dev
);
579 EXPORT_SYMBOL(sas_phy_alloc
);
582 * sas_phy_add -- add a SAS PHY to the device hierarchy
583 * @phy: The PHY to be added
585 * Publishes a SAS PHY to the rest of the system.
587 int sas_phy_add(struct sas_phy
*phy
)
591 error
= device_add(&phy
->dev
);
593 transport_add_device(&phy
->dev
);
594 transport_configure_device(&phy
->dev
);
599 EXPORT_SYMBOL(sas_phy_add
);
602 * sas_phy_free -- free a SAS PHY
603 * @phy: SAS PHY to free
605 * Frees the specified SAS PHY.
608 * This function must only be called on a PHY that has not
609 * sucessfully been added using sas_phy_add().
611 void sas_phy_free(struct sas_phy
*phy
)
613 transport_destroy_device(&phy
->dev
);
614 put_device(&phy
->dev
);
616 EXPORT_SYMBOL(sas_phy_free
);
619 * sas_phy_delete -- remove SAS PHY
620 * @phy: SAS PHY to remove
622 * Removes the specified SAS PHY. If the SAS PHY has an
623 * associated remote PHY it is removed before.
626 sas_phy_delete(struct sas_phy
*phy
)
628 struct device
*dev
= &phy
->dev
;
630 /* this happens if the phy is still part of a port when deleted */
631 BUG_ON(!list_empty(&phy
->port_siblings
));
633 transport_remove_device(dev
);
635 transport_destroy_device(dev
);
638 EXPORT_SYMBOL(sas_phy_delete
);
641 * scsi_is_sas_phy -- check if a struct device represents a SAS PHY
642 * @dev: device to check
645 * %1 if the device represents a SAS PHY, %0 else
647 int scsi_is_sas_phy(const struct device
*dev
)
649 return dev
->release
== sas_phy_release
;
651 EXPORT_SYMBOL(scsi_is_sas_phy
);
654 * SAS Port attributes
656 #define sas_port_show_simple(field, name, format_string, cast) \
658 show_sas_port_##name(struct class_device *cdev, char *buf) \
660 struct sas_port *port = transport_class_to_sas_port(cdev); \
662 return snprintf(buf, 20, format_string, cast port->field); \
665 #define sas_port_simple_attr(field, name, format_string, type) \
666 sas_port_show_simple(field, name, format_string, (type)) \
667 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_sas_port_##name, NULL)
669 sas_port_simple_attr(num_phys
, num_phys
, "%d\n", int);
671 static DECLARE_TRANSPORT_CLASS(sas_port_class
,
672 "sas_port", NULL
, NULL
, NULL
);
674 static int sas_port_match(struct attribute_container
*cont
, struct device
*dev
)
676 struct Scsi_Host
*shost
;
677 struct sas_internal
*i
;
679 if (!scsi_is_sas_port(dev
))
681 shost
= dev_to_shost(dev
->parent
);
683 if (!shost
->transportt
)
685 if (shost
->transportt
->host_attrs
.ac
.class !=
686 &sas_host_class
.class)
689 i
= to_sas_internal(shost
->transportt
);
690 return &i
->port_attr_cont
.ac
== cont
;
694 static void sas_port_release(struct device
*dev
)
696 struct sas_port
*port
= dev_to_sas_port(dev
);
698 BUG_ON(!list_empty(&port
->phy_list
));
700 put_device(dev
->parent
);
704 static void sas_port_create_link(struct sas_port
*port
,
709 res
= sysfs_create_link(&port
->dev
.kobj
, &phy
->dev
.kobj
,
713 res
= sysfs_create_link(&phy
->dev
.kobj
, &port
->dev
.kobj
, "port");
718 printk(KERN_ERR
"%s: Cannot create port links, err=%d\n",
722 static void sas_port_delete_link(struct sas_port
*port
,
725 sysfs_remove_link(&port
->dev
.kobj
, phy
->dev
.bus_id
);
726 sysfs_remove_link(&phy
->dev
.kobj
, "port");
729 /** sas_port_alloc - allocate and initialize a SAS port structure
731 * @parent: parent device
732 * @port_id: port number
734 * Allocates a SAS port structure. It will be added to the device tree
735 * below the device specified by @parent which must be either a Scsi_Host
736 * or a sas_expander_device.
738 * Returns %NULL on error
740 struct sas_port
*sas_port_alloc(struct device
*parent
, int port_id
)
742 struct Scsi_Host
*shost
= dev_to_shost(parent
);
743 struct sas_port
*port
;
745 port
= kzalloc(sizeof(*port
), GFP_KERNEL
);
749 port
->port_identifier
= port_id
;
751 device_initialize(&port
->dev
);
753 port
->dev
.parent
= get_device(parent
);
754 port
->dev
.release
= sas_port_release
;
756 mutex_init(&port
->phy_list_mutex
);
757 INIT_LIST_HEAD(&port
->phy_list
);
759 if (scsi_is_sas_expander_device(parent
)) {
760 struct sas_rphy
*rphy
= dev_to_rphy(parent
);
761 sprintf(port
->dev
.bus_id
, "port-%d:%d:%d", shost
->host_no
,
762 rphy
->scsi_target_id
, port
->port_identifier
);
764 sprintf(port
->dev
.bus_id
, "port-%d:%d", shost
->host_no
,
765 port
->port_identifier
);
767 transport_setup_device(&port
->dev
);
771 EXPORT_SYMBOL(sas_port_alloc
);
773 /** sas_port_alloc_num - allocate and initialize a SAS port structure
775 * @parent: parent device
777 * Allocates a SAS port structure and a number to go with it. This
778 * interface is really for adapters where the port number has no
779 * meansing, so the sas class should manage them. It will be added to
780 * the device tree below the device specified by @parent which must be
781 * either a Scsi_Host or a sas_expander_device.
783 * Returns %NULL on error
785 struct sas_port
*sas_port_alloc_num(struct device
*parent
)
788 struct Scsi_Host
*shost
= dev_to_shost(parent
);
789 struct sas_host_attrs
*sas_host
= to_sas_host_attrs(shost
);
791 /* FIXME: use idr for this eventually */
792 mutex_lock(&sas_host
->lock
);
793 if (scsi_is_sas_expander_device(parent
)) {
794 struct sas_rphy
*rphy
= dev_to_rphy(parent
);
795 struct sas_expander_device
*exp
= rphy_to_expander_device(rphy
);
797 index
= exp
->next_port_id
++;
799 index
= sas_host
->next_port_id
++;
800 mutex_unlock(&sas_host
->lock
);
801 return sas_port_alloc(parent
, index
);
803 EXPORT_SYMBOL(sas_port_alloc_num
);
806 * sas_port_add - add a SAS port to the device hierarchy
808 * @port: port to be added
810 * publishes a port to the rest of the system
812 int sas_port_add(struct sas_port
*port
)
816 /* No phys should be added until this is made visible */
817 BUG_ON(!list_empty(&port
->phy_list
));
819 error
= device_add(&port
->dev
);
824 transport_add_device(&port
->dev
);
825 transport_configure_device(&port
->dev
);
829 EXPORT_SYMBOL(sas_port_add
);
832 * sas_port_free -- free a SAS PORT
833 * @port: SAS PORT to free
835 * Frees the specified SAS PORT.
838 * This function must only be called on a PORT that has not
839 * sucessfully been added using sas_port_add().
841 void sas_port_free(struct sas_port
*port
)
843 transport_destroy_device(&port
->dev
);
844 put_device(&port
->dev
);
846 EXPORT_SYMBOL(sas_port_free
);
849 * sas_port_delete -- remove SAS PORT
850 * @port: SAS PORT to remove
852 * Removes the specified SAS PORT. If the SAS PORT has an
853 * associated phys, unlink them from the port as well.
855 void sas_port_delete(struct sas_port
*port
)
857 struct device
*dev
= &port
->dev
;
858 struct sas_phy
*phy
, *tmp_phy
;
861 sas_rphy_delete(port
->rphy
);
865 mutex_lock(&port
->phy_list_mutex
);
866 list_for_each_entry_safe(phy
, tmp_phy
, &port
->phy_list
,
868 sas_port_delete_link(port
, phy
);
869 list_del_init(&phy
->port_siblings
);
871 mutex_unlock(&port
->phy_list_mutex
);
873 if (port
->is_backlink
) {
874 struct device
*parent
= port
->dev
.parent
;
876 sysfs_remove_link(&port
->dev
.kobj
, parent
->bus_id
);
877 port
->is_backlink
= 0;
880 transport_remove_device(dev
);
882 transport_destroy_device(dev
);
885 EXPORT_SYMBOL(sas_port_delete
);
888 * scsi_is_sas_port -- check if a struct device represents a SAS port
889 * @dev: device to check
892 * %1 if the device represents a SAS Port, %0 else
894 int scsi_is_sas_port(const struct device
*dev
)
896 return dev
->release
== sas_port_release
;
898 EXPORT_SYMBOL(scsi_is_sas_port
);
901 * sas_port_add_phy - add another phy to a port to form a wide port
902 * @port: port to add the phy to
905 * When a port is initially created, it is empty (has no phys). All
906 * ports must have at least one phy to operated, and all wide ports
907 * must have at least two. The current code makes no difference
908 * between ports and wide ports, but the only object that can be
909 * connected to a remote device is a port, so ports must be formed on
910 * all devices with phys if they're connected to anything.
912 void sas_port_add_phy(struct sas_port
*port
, struct sas_phy
*phy
)
914 mutex_lock(&port
->phy_list_mutex
);
915 if (unlikely(!list_empty(&phy
->port_siblings
))) {
916 /* make sure we're already on this port */
919 list_for_each_entry(tmp
, &port
->phy_list
, port_siblings
)
922 /* If this trips, you added a phy that was already
923 * part of a different port */
924 if (unlikely(tmp
!= phy
)) {
925 dev_printk(KERN_ERR
, &port
->dev
, "trying to add phy %s fails: it's already part of another port\n", phy
->dev
.bus_id
);
929 sas_port_create_link(port
, phy
);
930 list_add_tail(&phy
->port_siblings
, &port
->phy_list
);
933 mutex_unlock(&port
->phy_list_mutex
);
935 EXPORT_SYMBOL(sas_port_add_phy
);
938 * sas_port_delete_phy - remove a phy from a port or wide port
939 * @port: port to remove the phy from
940 * @phy: phy to remove
942 * This operation is used for tearing down ports again. It must be
943 * done to every port or wide port before calling sas_port_delete.
945 void sas_port_delete_phy(struct sas_port
*port
, struct sas_phy
*phy
)
947 mutex_lock(&port
->phy_list_mutex
);
948 sas_port_delete_link(port
, phy
);
949 list_del_init(&phy
->port_siblings
);
951 mutex_unlock(&port
->phy_list_mutex
);
953 EXPORT_SYMBOL(sas_port_delete_phy
);
955 void sas_port_mark_backlink(struct sas_port
*port
)
958 struct device
*parent
= port
->dev
.parent
->parent
->parent
;
960 if (port
->is_backlink
)
962 port
->is_backlink
= 1;
963 res
= sysfs_create_link(&port
->dev
.kobj
, &parent
->kobj
,
969 printk(KERN_ERR
"%s: Cannot create port backlink, err=%d\n",
973 EXPORT_SYMBOL(sas_port_mark_backlink
);
976 * SAS remote PHY attributes.
979 #define sas_rphy_show_simple(field, name, format_string, cast) \
981 show_sas_rphy_##name(struct class_device *cdev, char *buf) \
983 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
985 return snprintf(buf, 20, format_string, cast rphy->field); \
988 #define sas_rphy_simple_attr(field, name, format_string, type) \
989 sas_rphy_show_simple(field, name, format_string, (type)) \
990 static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \
991 show_sas_rphy_##name, NULL)
993 #define sas_rphy_show_protocol(field, name) \
995 show_sas_rphy_##name(struct class_device *cdev, char *buf) \
997 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
1000 return snprintf(buf, 20, "none\n"); \
1001 return get_sas_protocol_names(rphy->field, buf); \
1004 #define sas_rphy_protocol_attr(field, name) \
1005 sas_rphy_show_protocol(field, name) \
1006 static SAS_CLASS_DEVICE_ATTR(rphy, name, S_IRUGO, \
1007 show_sas_rphy_##name, NULL)
1010 show_sas_rphy_device_type(struct class_device
*cdev
, char *buf
)
1012 struct sas_rphy
*rphy
= transport_class_to_rphy(cdev
);
1014 if (!rphy
->identify
.device_type
)
1015 return snprintf(buf
, 20, "none\n");
1016 return get_sas_device_type_names(
1017 rphy
->identify
.device_type
, buf
);
1020 static SAS_CLASS_DEVICE_ATTR(rphy
, device_type
, S_IRUGO
,
1021 show_sas_rphy_device_type
, NULL
);
1024 show_sas_rphy_enclosure_identifier(struct class_device
*cdev
, char *buf
)
1026 struct sas_rphy
*rphy
= transport_class_to_rphy(cdev
);
1027 struct sas_phy
*phy
= dev_to_phy(rphy
->dev
.parent
);
1028 struct Scsi_Host
*shost
= dev_to_shost(phy
->dev
.parent
);
1029 struct sas_internal
*i
= to_sas_internal(shost
->transportt
);
1034 * Only devices behind an expander are supported, because the
1035 * enclosure identifier is a SMP feature.
1037 if (scsi_is_sas_phy_local(phy
))
1040 error
= i
->f
->get_enclosure_identifier(rphy
, &identifier
);
1043 return sprintf(buf
, "0x%llx\n", (unsigned long long)identifier
);
1046 static SAS_CLASS_DEVICE_ATTR(rphy
, enclosure_identifier
, S_IRUGO
,
1047 show_sas_rphy_enclosure_identifier
, NULL
);
1050 show_sas_rphy_bay_identifier(struct class_device
*cdev
, char *buf
)
1052 struct sas_rphy
*rphy
= transport_class_to_rphy(cdev
);
1053 struct sas_phy
*phy
= dev_to_phy(rphy
->dev
.parent
);
1054 struct Scsi_Host
*shost
= dev_to_shost(phy
->dev
.parent
);
1055 struct sas_internal
*i
= to_sas_internal(shost
->transportt
);
1058 if (scsi_is_sas_phy_local(phy
))
1061 val
= i
->f
->get_bay_identifier(rphy
);
1064 return sprintf(buf
, "%d\n", val
);
1067 static SAS_CLASS_DEVICE_ATTR(rphy
, bay_identifier
, S_IRUGO
,
1068 show_sas_rphy_bay_identifier
, NULL
);
1070 sas_rphy_protocol_attr(identify
.initiator_port_protocols
,
1071 initiator_port_protocols
);
1072 sas_rphy_protocol_attr(identify
.target_port_protocols
, target_port_protocols
);
1073 sas_rphy_simple_attr(identify
.sas_address
, sas_address
, "0x%016llx\n",
1074 unsigned long long);
1075 sas_rphy_simple_attr(identify
.phy_identifier
, phy_identifier
, "%d\n", u8
);
1077 /* only need 8 bytes of data plus header (4 or 8) */
1080 int sas_read_port_mode_page(struct scsi_device
*sdev
)
1082 char *buffer
= kzalloc(BUF_SIZE
, GFP_KERNEL
), *msdata
;
1083 struct sas_rphy
*rphy
= target_to_rphy(sdev
->sdev_target
);
1084 struct sas_end_device
*rdev
;
1085 struct scsi_mode_data mode_data
;
1088 BUG_ON(rphy
->identify
.device_type
!= SAS_END_DEVICE
);
1090 rdev
= rphy_to_end_device(rphy
);
1095 res
= scsi_mode_sense(sdev
, 1, 0x19, buffer
, BUF_SIZE
, 30*HZ
, 3,
1099 if (!scsi_status_is_good(res
))
1102 msdata
= buffer
+ mode_data
.header_length
+
1103 mode_data
.block_descriptor_length
;
1105 if (msdata
- buffer
> BUF_SIZE
- 8)
1110 rdev
->ready_led_meaning
= msdata
[2] & 0x10 ? 1 : 0;
1111 rdev
->I_T_nexus_loss_timeout
= (msdata
[4] << 8) + msdata
[5];
1112 rdev
->initiator_response_timeout
= (msdata
[6] << 8) + msdata
[7];
1118 EXPORT_SYMBOL(sas_read_port_mode_page
);
1120 static DECLARE_TRANSPORT_CLASS(sas_end_dev_class
,
1121 "sas_end_device", NULL
, NULL
, NULL
);
1123 #define sas_end_dev_show_simple(field, name, format_string, cast) \
1125 show_sas_end_dev_##name(struct class_device *cdev, char *buf) \
1127 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
1128 struct sas_end_device *rdev = rphy_to_end_device(rphy); \
1130 return snprintf(buf, 20, format_string, cast rdev->field); \
1133 #define sas_end_dev_simple_attr(field, name, format_string, type) \
1134 sas_end_dev_show_simple(field, name, format_string, (type)) \
1135 static SAS_CLASS_DEVICE_ATTR(end_dev, name, S_IRUGO, \
1136 show_sas_end_dev_##name, NULL)
1138 sas_end_dev_simple_attr(ready_led_meaning
, ready_led_meaning
, "%d\n", int);
1139 sas_end_dev_simple_attr(I_T_nexus_loss_timeout
, I_T_nexus_loss_timeout
,
1141 sas_end_dev_simple_attr(initiator_response_timeout
, initiator_response_timeout
,
1144 static DECLARE_TRANSPORT_CLASS(sas_expander_class
,
1145 "sas_expander", NULL
, NULL
, NULL
);
1147 #define sas_expander_show_simple(field, name, format_string, cast) \
1149 show_sas_expander_##name(struct class_device *cdev, char *buf) \
1151 struct sas_rphy *rphy = transport_class_to_rphy(cdev); \
1152 struct sas_expander_device *edev = rphy_to_expander_device(rphy); \
1154 return snprintf(buf, 20, format_string, cast edev->field); \
1157 #define sas_expander_simple_attr(field, name, format_string, type) \
1158 sas_expander_show_simple(field, name, format_string, (type)) \
1159 static SAS_CLASS_DEVICE_ATTR(expander, name, S_IRUGO, \
1160 show_sas_expander_##name, NULL)
1162 sas_expander_simple_attr(vendor_id
, vendor_id
, "%s\n", char *);
1163 sas_expander_simple_attr(product_id
, product_id
, "%s\n", char *);
1164 sas_expander_simple_attr(product_rev
, product_rev
, "%s\n", char *);
1165 sas_expander_simple_attr(component_vendor_id
, component_vendor_id
,
1167 sas_expander_simple_attr(component_id
, component_id
, "%u\n", unsigned int);
1168 sas_expander_simple_attr(component_revision_id
, component_revision_id
, "%u\n",
1170 sas_expander_simple_attr(level
, level
, "%d\n", int);
1172 static DECLARE_TRANSPORT_CLASS(sas_rphy_class
,
1173 "sas_device", NULL
, NULL
, NULL
);
1175 static int sas_rphy_match(struct attribute_container
*cont
, struct device
*dev
)
1177 struct Scsi_Host
*shost
;
1178 struct sas_internal
*i
;
1180 if (!scsi_is_sas_rphy(dev
))
1182 shost
= dev_to_shost(dev
->parent
->parent
);
1184 if (!shost
->transportt
)
1186 if (shost
->transportt
->host_attrs
.ac
.class !=
1187 &sas_host_class
.class)
1190 i
= to_sas_internal(shost
->transportt
);
1191 return &i
->rphy_attr_cont
.ac
== cont
;
1194 static int sas_end_dev_match(struct attribute_container
*cont
,
1197 struct Scsi_Host
*shost
;
1198 struct sas_internal
*i
;
1199 struct sas_rphy
*rphy
;
1201 if (!scsi_is_sas_rphy(dev
))
1203 shost
= dev_to_shost(dev
->parent
->parent
);
1204 rphy
= dev_to_rphy(dev
);
1206 if (!shost
->transportt
)
1208 if (shost
->transportt
->host_attrs
.ac
.class !=
1209 &sas_host_class
.class)
1212 i
= to_sas_internal(shost
->transportt
);
1213 return &i
->end_dev_attr_cont
.ac
== cont
&&
1214 rphy
->identify
.device_type
== SAS_END_DEVICE
;
1217 static int sas_expander_match(struct attribute_container
*cont
,
1220 struct Scsi_Host
*shost
;
1221 struct sas_internal
*i
;
1222 struct sas_rphy
*rphy
;
1224 if (!scsi_is_sas_rphy(dev
))
1226 shost
= dev_to_shost(dev
->parent
->parent
);
1227 rphy
= dev_to_rphy(dev
);
1229 if (!shost
->transportt
)
1231 if (shost
->transportt
->host_attrs
.ac
.class !=
1232 &sas_host_class
.class)
1235 i
= to_sas_internal(shost
->transportt
);
1236 return &i
->expander_attr_cont
.ac
== cont
&&
1237 (rphy
->identify
.device_type
== SAS_EDGE_EXPANDER_DEVICE
||
1238 rphy
->identify
.device_type
== SAS_FANOUT_EXPANDER_DEVICE
);
1241 static void sas_expander_release(struct device
*dev
)
1243 struct sas_rphy
*rphy
= dev_to_rphy(dev
);
1244 struct sas_expander_device
*edev
= rphy_to_expander_device(rphy
);
1246 put_device(dev
->parent
);
1250 static void sas_end_device_release(struct device
*dev
)
1252 struct sas_rphy
*rphy
= dev_to_rphy(dev
);
1253 struct sas_end_device
*edev
= rphy_to_end_device(rphy
);
1255 put_device(dev
->parent
);
1260 * sas_rphy_initialize - common rphy intialization
1261 * @rphy: rphy to initialise
1263 * Used by both sas_end_device_alloc() and sas_expander_alloc() to
1264 * initialise the common rphy component of each.
1266 static void sas_rphy_initialize(struct sas_rphy
*rphy
)
1268 INIT_LIST_HEAD(&rphy
->list
);
1272 * sas_end_device_alloc - allocate an rphy for an end device
1274 * Allocates an SAS remote PHY structure, connected to @parent.
1277 * SAS PHY allocated or %NULL if the allocation failed.
1279 struct sas_rphy
*sas_end_device_alloc(struct sas_port
*parent
)
1281 struct Scsi_Host
*shost
= dev_to_shost(&parent
->dev
);
1282 struct sas_end_device
*rdev
;
1284 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
1289 device_initialize(&rdev
->rphy
.dev
);
1290 rdev
->rphy
.dev
.parent
= get_device(&parent
->dev
);
1291 rdev
->rphy
.dev
.release
= sas_end_device_release
;
1292 if (scsi_is_sas_expander_device(parent
->dev
.parent
)) {
1293 struct sas_rphy
*rphy
= dev_to_rphy(parent
->dev
.parent
);
1294 sprintf(rdev
->rphy
.dev
.bus_id
, "end_device-%d:%d:%d",
1295 shost
->host_no
, rphy
->scsi_target_id
, parent
->port_identifier
);
1297 sprintf(rdev
->rphy
.dev
.bus_id
, "end_device-%d:%d",
1298 shost
->host_no
, parent
->port_identifier
);
1299 rdev
->rphy
.identify
.device_type
= SAS_END_DEVICE
;
1300 sas_rphy_initialize(&rdev
->rphy
);
1301 transport_setup_device(&rdev
->rphy
.dev
);
1303 if (sas_bsg_initialize(shost
, &rdev
->rphy
, rdev
->rphy
.dev
.bus_id
))
1304 printk("fail to a bsg device %s\n", rdev
->rphy
.dev
.bus_id
);
1308 EXPORT_SYMBOL(sas_end_device_alloc
);
1311 * sas_expander_alloc - allocate an rphy for an end device
1313 * Allocates an SAS remote PHY structure, connected to @parent.
1316 * SAS PHY allocated or %NULL if the allocation failed.
1318 struct sas_rphy
*sas_expander_alloc(struct sas_port
*parent
,
1319 enum sas_device_type type
)
1321 struct Scsi_Host
*shost
= dev_to_shost(&parent
->dev
);
1322 struct sas_expander_device
*rdev
;
1323 struct sas_host_attrs
*sas_host
= to_sas_host_attrs(shost
);
1325 BUG_ON(type
!= SAS_EDGE_EXPANDER_DEVICE
&&
1326 type
!= SAS_FANOUT_EXPANDER_DEVICE
);
1328 rdev
= kzalloc(sizeof(*rdev
), GFP_KERNEL
);
1333 device_initialize(&rdev
->rphy
.dev
);
1334 rdev
->rphy
.dev
.parent
= get_device(&parent
->dev
);
1335 rdev
->rphy
.dev
.release
= sas_expander_release
;
1336 mutex_lock(&sas_host
->lock
);
1337 rdev
->rphy
.scsi_target_id
= sas_host
->next_expander_id
++;
1338 mutex_unlock(&sas_host
->lock
);
1339 sprintf(rdev
->rphy
.dev
.bus_id
, "expander-%d:%d",
1340 shost
->host_no
, rdev
->rphy
.scsi_target_id
);
1341 rdev
->rphy
.identify
.device_type
= type
;
1342 sas_rphy_initialize(&rdev
->rphy
);
1343 transport_setup_device(&rdev
->rphy
.dev
);
1345 if (sas_bsg_initialize(shost
, &rdev
->rphy
, rdev
->rphy
.dev
.bus_id
))
1346 printk("fail to a bsg device %s\n", rdev
->rphy
.dev
.bus_id
);
1350 EXPORT_SYMBOL(sas_expander_alloc
);
1353 * sas_rphy_add -- add a SAS remote PHY to the device hierarchy
1354 * @rphy: The remote PHY to be added
1356 * Publishes a SAS remote PHY to the rest of the system.
1358 int sas_rphy_add(struct sas_rphy
*rphy
)
1360 struct sas_port
*parent
= dev_to_sas_port(rphy
->dev
.parent
);
1361 struct Scsi_Host
*shost
= dev_to_shost(parent
->dev
.parent
);
1362 struct sas_host_attrs
*sas_host
= to_sas_host_attrs(shost
);
1363 struct sas_identify
*identify
= &rphy
->identify
;
1368 parent
->rphy
= rphy
;
1370 error
= device_add(&rphy
->dev
);
1373 transport_add_device(&rphy
->dev
);
1374 transport_configure_device(&rphy
->dev
);
1376 mutex_lock(&sas_host
->lock
);
1377 list_add_tail(&rphy
->list
, &sas_host
->rphy_list
);
1378 if (identify
->device_type
== SAS_END_DEVICE
&&
1379 (identify
->target_port_protocols
&
1380 (SAS_PROTOCOL_SSP
|SAS_PROTOCOL_STP
|SAS_PROTOCOL_SATA
)))
1381 rphy
->scsi_target_id
= sas_host
->next_target_id
++;
1382 else if (identify
->device_type
== SAS_END_DEVICE
)
1383 rphy
->scsi_target_id
= -1;
1384 mutex_unlock(&sas_host
->lock
);
1386 if (identify
->device_type
== SAS_END_DEVICE
&&
1387 rphy
->scsi_target_id
!= -1) {
1388 scsi_scan_target(&rphy
->dev
, 0,
1389 rphy
->scsi_target_id
, SCAN_WILD_CARD
, 0);
1394 EXPORT_SYMBOL(sas_rphy_add
);
1397 * sas_rphy_free -- free a SAS remote PHY
1398 * @rphy SAS remote PHY to free
1400 * Frees the specified SAS remote PHY.
1403 * This function must only be called on a remote
1404 * PHY that has not sucessfully been added using
1405 * sas_rphy_add() (or has been sas_rphy_remove()'d)
1407 void sas_rphy_free(struct sas_rphy
*rphy
)
1409 struct device
*dev
= &rphy
->dev
;
1410 struct Scsi_Host
*shost
= dev_to_shost(rphy
->dev
.parent
->parent
);
1411 struct sas_host_attrs
*sas_host
= to_sas_host_attrs(shost
);
1413 mutex_lock(&sas_host
->lock
);
1414 list_del(&rphy
->list
);
1415 mutex_unlock(&sas_host
->lock
);
1417 transport_destroy_device(dev
);
1421 EXPORT_SYMBOL(sas_rphy_free
);
1424 * sas_rphy_delete -- remove and free SAS remote PHY
1425 * @rphy: SAS remote PHY to remove and free
1427 * Removes the specified SAS remote PHY and frees it.
1430 sas_rphy_delete(struct sas_rphy
*rphy
)
1432 sas_rphy_remove(rphy
);
1433 sas_rphy_free(rphy
);
1435 EXPORT_SYMBOL(sas_rphy_delete
);
1438 * sas_rphy_remove -- remove SAS remote PHY
1439 * @rphy: SAS remote phy to remove
1441 * Removes the specified SAS remote PHY.
1444 sas_rphy_remove(struct sas_rphy
*rphy
)
1446 struct device
*dev
= &rphy
->dev
;
1447 struct sas_port
*parent
= dev_to_sas_port(dev
->parent
);
1449 switch (rphy
->identify
.device_type
) {
1450 case SAS_END_DEVICE
:
1451 scsi_remove_target(dev
);
1453 case SAS_EDGE_EXPANDER_DEVICE
:
1454 case SAS_FANOUT_EXPANDER_DEVICE
:
1455 sas_remove_children(dev
);
1461 transport_remove_device(dev
);
1464 parent
->rphy
= NULL
;
1466 EXPORT_SYMBOL(sas_rphy_remove
);
1469 * scsi_is_sas_rphy -- check if a struct device represents a SAS remote PHY
1470 * @dev: device to check
1473 * %1 if the device represents a SAS remote PHY, %0 else
1475 int scsi_is_sas_rphy(const struct device
*dev
)
1477 return dev
->release
== sas_end_device_release
||
1478 dev
->release
== sas_expander_release
;
1480 EXPORT_SYMBOL(scsi_is_sas_rphy
);
1487 static int sas_user_scan(struct Scsi_Host
*shost
, uint channel
,
1490 struct sas_host_attrs
*sas_host
= to_sas_host_attrs(shost
);
1491 struct sas_rphy
*rphy
;
1493 mutex_lock(&sas_host
->lock
);
1494 list_for_each_entry(rphy
, &sas_host
->rphy_list
, list
) {
1495 if (rphy
->identify
.device_type
!= SAS_END_DEVICE
||
1496 rphy
->scsi_target_id
== -1)
1499 if ((channel
== SCAN_WILD_CARD
|| channel
== 0) &&
1500 (id
== SCAN_WILD_CARD
|| id
== rphy
->scsi_target_id
)) {
1501 scsi_scan_target(&rphy
->dev
, 0,
1502 rphy
->scsi_target_id
, lun
, 1);
1505 mutex_unlock(&sas_host
->lock
);
1512 * Setup / Teardown code
1515 #define SETUP_TEMPLATE(attrb, field, perm, test) \
1516 i->private_##attrb[count] = class_device_attr_##field; \
1517 i->private_##attrb[count].attr.mode = perm; \
1518 i->attrb[count] = &i->private_##attrb[count]; \
1522 #define SETUP_TEMPLATE_RW(attrb, field, perm, test, ro_test, ro_perm) \
1523 i->private_##attrb[count] = class_device_attr_##field; \
1524 i->private_##attrb[count].attr.mode = perm; \
1526 i->private_##attrb[count].attr.mode = ro_perm; \
1527 i->private_##attrb[count].store = NULL; \
1529 i->attrb[count] = &i->private_##attrb[count]; \
1533 #define SETUP_RPORT_ATTRIBUTE(field) \
1534 SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, 1)
1536 #define SETUP_OPTIONAL_RPORT_ATTRIBUTE(field, func) \
1537 SETUP_TEMPLATE(rphy_attrs, field, S_IRUGO, i->f->func)
1539 #define SETUP_PHY_ATTRIBUTE(field) \
1540 SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, 1)
1542 #define SETUP_PHY_ATTRIBUTE_RW(field) \
1543 SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1, \
1544 !i->f->set_phy_speed, S_IRUGO)
1546 #define SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(field, func) \
1547 SETUP_TEMPLATE_RW(phy_attrs, field, S_IRUGO | S_IWUSR, 1, \
1548 !i->f->func, S_IRUGO)
1550 #define SETUP_PORT_ATTRIBUTE(field) \
1551 SETUP_TEMPLATE(port_attrs, field, S_IRUGO, 1)
1553 #define SETUP_OPTIONAL_PHY_ATTRIBUTE(field, func) \
1554 SETUP_TEMPLATE(phy_attrs, field, S_IRUGO, i->f->func)
1556 #define SETUP_PHY_ATTRIBUTE_WRONLY(field) \
1557 SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, 1)
1559 #define SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(field, func) \
1560 SETUP_TEMPLATE(phy_attrs, field, S_IWUSR, i->f->func)
1562 #define SETUP_END_DEV_ATTRIBUTE(field) \
1563 SETUP_TEMPLATE(end_dev_attrs, field, S_IRUGO, 1)
1565 #define SETUP_EXPANDER_ATTRIBUTE(field) \
1566 SETUP_TEMPLATE(expander_attrs, expander_##field, S_IRUGO, 1)
1569 * sas_attach_transport -- instantiate SAS transport template
1570 * @ft: SAS transport class function template
1572 struct scsi_transport_template
*
1573 sas_attach_transport(struct sas_function_template
*ft
)
1575 struct sas_internal
*i
;
1578 i
= kzalloc(sizeof(struct sas_internal
), GFP_KERNEL
);
1582 i
->t
.user_scan
= sas_user_scan
;
1584 i
->t
.host_attrs
.ac
.attrs
= &i
->host_attrs
[0];
1585 i
->t
.host_attrs
.ac
.class = &sas_host_class
.class;
1586 i
->t
.host_attrs
.ac
.match
= sas_host_match
;
1587 transport_container_register(&i
->t
.host_attrs
);
1588 i
->t
.host_size
= sizeof(struct sas_host_attrs
);
1590 i
->phy_attr_cont
.ac
.class = &sas_phy_class
.class;
1591 i
->phy_attr_cont
.ac
.attrs
= &i
->phy_attrs
[0];
1592 i
->phy_attr_cont
.ac
.match
= sas_phy_match
;
1593 transport_container_register(&i
->phy_attr_cont
);
1595 i
->port_attr_cont
.ac
.class = &sas_port_class
.class;
1596 i
->port_attr_cont
.ac
.attrs
= &i
->port_attrs
[0];
1597 i
->port_attr_cont
.ac
.match
= sas_port_match
;
1598 transport_container_register(&i
->port_attr_cont
);
1600 i
->rphy_attr_cont
.ac
.class = &sas_rphy_class
.class;
1601 i
->rphy_attr_cont
.ac
.attrs
= &i
->rphy_attrs
[0];
1602 i
->rphy_attr_cont
.ac
.match
= sas_rphy_match
;
1603 transport_container_register(&i
->rphy_attr_cont
);
1605 i
->end_dev_attr_cont
.ac
.class = &sas_end_dev_class
.class;
1606 i
->end_dev_attr_cont
.ac
.attrs
= &i
->end_dev_attrs
[0];
1607 i
->end_dev_attr_cont
.ac
.match
= sas_end_dev_match
;
1608 transport_container_register(&i
->end_dev_attr_cont
);
1610 i
->expander_attr_cont
.ac
.class = &sas_expander_class
.class;
1611 i
->expander_attr_cont
.ac
.attrs
= &i
->expander_attrs
[0];
1612 i
->expander_attr_cont
.ac
.match
= sas_expander_match
;
1613 transport_container_register(&i
->expander_attr_cont
);
1618 SETUP_PORT_ATTRIBUTE(num_phys
);
1619 i
->host_attrs
[count
] = NULL
;
1622 SETUP_PHY_ATTRIBUTE(initiator_port_protocols
);
1623 SETUP_PHY_ATTRIBUTE(target_port_protocols
);
1624 SETUP_PHY_ATTRIBUTE(device_type
);
1625 SETUP_PHY_ATTRIBUTE(sas_address
);
1626 SETUP_PHY_ATTRIBUTE(phy_identifier
);
1627 //SETUP_PHY_ATTRIBUTE(port_identifier);
1628 SETUP_PHY_ATTRIBUTE(negotiated_linkrate
);
1629 SETUP_PHY_ATTRIBUTE(minimum_linkrate_hw
);
1630 SETUP_PHY_ATTRIBUTE_RW(minimum_linkrate
);
1631 SETUP_PHY_ATTRIBUTE(maximum_linkrate_hw
);
1632 SETUP_PHY_ATTRIBUTE_RW(maximum_linkrate
);
1634 SETUP_PHY_ATTRIBUTE(invalid_dword_count
);
1635 SETUP_PHY_ATTRIBUTE(running_disparity_error_count
);
1636 SETUP_PHY_ATTRIBUTE(loss_of_dword_sync_count
);
1637 SETUP_PHY_ATTRIBUTE(phy_reset_problem_count
);
1638 SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(link_reset
, phy_reset
);
1639 SETUP_OPTIONAL_PHY_ATTRIBUTE_WRONLY(hard_reset
, phy_reset
);
1640 SETUP_OPTIONAL_PHY_ATTRIBUTE_RW(enable
, phy_enable
);
1641 i
->phy_attrs
[count
] = NULL
;
1644 SETUP_PORT_ATTRIBUTE(num_phys
);
1645 i
->port_attrs
[count
] = NULL
;
1648 SETUP_RPORT_ATTRIBUTE(rphy_initiator_port_protocols
);
1649 SETUP_RPORT_ATTRIBUTE(rphy_target_port_protocols
);
1650 SETUP_RPORT_ATTRIBUTE(rphy_device_type
);
1651 SETUP_RPORT_ATTRIBUTE(rphy_sas_address
);
1652 SETUP_RPORT_ATTRIBUTE(rphy_phy_identifier
);
1653 SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_enclosure_identifier
,
1654 get_enclosure_identifier
);
1655 SETUP_OPTIONAL_RPORT_ATTRIBUTE(rphy_bay_identifier
,
1656 get_bay_identifier
);
1657 i
->rphy_attrs
[count
] = NULL
;
1660 SETUP_END_DEV_ATTRIBUTE(end_dev_ready_led_meaning
);
1661 SETUP_END_DEV_ATTRIBUTE(end_dev_I_T_nexus_loss_timeout
);
1662 SETUP_END_DEV_ATTRIBUTE(end_dev_initiator_response_timeout
);
1663 i
->end_dev_attrs
[count
] = NULL
;
1666 SETUP_EXPANDER_ATTRIBUTE(vendor_id
);
1667 SETUP_EXPANDER_ATTRIBUTE(product_id
);
1668 SETUP_EXPANDER_ATTRIBUTE(product_rev
);
1669 SETUP_EXPANDER_ATTRIBUTE(component_vendor_id
);
1670 SETUP_EXPANDER_ATTRIBUTE(component_id
);
1671 SETUP_EXPANDER_ATTRIBUTE(component_revision_id
);
1672 SETUP_EXPANDER_ATTRIBUTE(level
);
1673 i
->expander_attrs
[count
] = NULL
;
1677 EXPORT_SYMBOL(sas_attach_transport
);
1680 * sas_release_transport -- release SAS transport template instance
1681 * @t: transport template instance
1683 void sas_release_transport(struct scsi_transport_template
*t
)
1685 struct sas_internal
*i
= to_sas_internal(t
);
1687 transport_container_unregister(&i
->t
.host_attrs
);
1688 transport_container_unregister(&i
->phy_attr_cont
);
1689 transport_container_unregister(&i
->port_attr_cont
);
1690 transport_container_unregister(&i
->rphy_attr_cont
);
1691 transport_container_unregister(&i
->end_dev_attr_cont
);
1692 transport_container_unregister(&i
->expander_attr_cont
);
1696 EXPORT_SYMBOL(sas_release_transport
);
1698 static __init
int sas_transport_init(void)
1702 error
= transport_class_register(&sas_host_class
);
1705 error
= transport_class_register(&sas_phy_class
);
1707 goto out_unregister_transport
;
1708 error
= transport_class_register(&sas_port_class
);
1710 goto out_unregister_phy
;
1711 error
= transport_class_register(&sas_rphy_class
);
1713 goto out_unregister_port
;
1714 error
= transport_class_register(&sas_end_dev_class
);
1716 goto out_unregister_rphy
;
1717 error
= transport_class_register(&sas_expander_class
);
1719 goto out_unregister_end_dev
;
1723 out_unregister_end_dev
:
1724 transport_class_unregister(&sas_end_dev_class
);
1725 out_unregister_rphy
:
1726 transport_class_unregister(&sas_rphy_class
);
1727 out_unregister_port
:
1728 transport_class_unregister(&sas_port_class
);
1730 transport_class_unregister(&sas_phy_class
);
1731 out_unregister_transport
:
1732 transport_class_unregister(&sas_host_class
);
1738 static void __exit
sas_transport_exit(void)
1740 transport_class_unregister(&sas_host_class
);
1741 transport_class_unregister(&sas_phy_class
);
1742 transport_class_unregister(&sas_port_class
);
1743 transport_class_unregister(&sas_rphy_class
);
1744 transport_class_unregister(&sas_end_dev_class
);
1745 transport_class_unregister(&sas_expander_class
);
1748 MODULE_AUTHOR("Christoph Hellwig");
1749 MODULE_DESCRIPTION("SAS Transport Attributes");
1750 MODULE_LICENSE("GPL");
1752 module_init(sas_transport_init
);
1753 module_exit(sas_transport_exit
);